18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci *  linux/drivers/scsi/esas2r/esas2r_main.c
38c2ecf20Sopenharmony_ci *      For use with ATTO ExpressSAS R6xx SAS/SATA RAID controllers
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (c) 2001-2013 ATTO Technology, Inc.
68c2ecf20Sopenharmony_ci *  (mailto:linuxdrivers@attotech.com)
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or
98c2ecf20Sopenharmony_ci * modify it under the terms of the GNU General Public License
108c2ecf20Sopenharmony_ci * as published by the Free Software Foundation; either version 2
118c2ecf20Sopenharmony_ci * of the License, or (at your option) any later version.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful,
148c2ecf20Sopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
158c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
168c2ecf20Sopenharmony_ci * GNU General Public License for more details.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * NO WARRANTY
198c2ecf20Sopenharmony_ci * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
208c2ecf20Sopenharmony_ci * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
218c2ecf20Sopenharmony_ci * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
228c2ecf20Sopenharmony_ci * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
238c2ecf20Sopenharmony_ci * solely responsible for determining the appropriateness of using and
248c2ecf20Sopenharmony_ci * distributing the Program and assumes all risks associated with its
258c2ecf20Sopenharmony_ci * exercise of rights under this Agreement, including but not limited to
268c2ecf20Sopenharmony_ci * the risks and costs of program errors, damage to or loss of data,
278c2ecf20Sopenharmony_ci * programs or equipment, and unavailability or interruption of operations.
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci * DISCLAIMER OF LIABILITY
308c2ecf20Sopenharmony_ci * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
318c2ecf20Sopenharmony_ci * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
328c2ecf20Sopenharmony_ci * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
338c2ecf20Sopenharmony_ci * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
348c2ecf20Sopenharmony_ci * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
358c2ecf20Sopenharmony_ci * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
368c2ecf20Sopenharmony_ci * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
378c2ecf20Sopenharmony_ci *
388c2ecf20Sopenharmony_ci * You should have received a copy of the GNU General Public License
398c2ecf20Sopenharmony_ci * along with this program; if not, write to the Free Software
408c2ecf20Sopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
418c2ecf20Sopenharmony_ci * USA.
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#include "esas2r.h"
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ciMODULE_DESCRIPTION(ESAS2R_DRVR_NAME ": " ESAS2R_LONGNAME " driver");
478c2ecf20Sopenharmony_ciMODULE_AUTHOR("ATTO Technology, Inc.");
488c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
498c2ecf20Sopenharmony_ciMODULE_VERSION(ESAS2R_VERSION_STR);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/* global definitions */
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic int found_adapters;
548c2ecf20Sopenharmony_cistruct esas2r_adapter *esas2r_adapters[MAX_ADAPTERS];
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci#define ESAS2R_VDA_EVENT_PORT1       54414
578c2ecf20Sopenharmony_ci#define ESAS2R_VDA_EVENT_PORT2       54415
588c2ecf20Sopenharmony_ci#define ESAS2R_VDA_EVENT_SOCK_COUNT  2
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic struct esas2r_adapter *esas2r_adapter_from_kobj(struct kobject *kobj)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	struct device *dev = container_of(kobj, struct device, kobj);
638c2ecf20Sopenharmony_ci	struct Scsi_Host *host = class_to_shost(dev);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	return (struct esas2r_adapter *)host->hostdata;
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic ssize_t read_fw(struct file *file, struct kobject *kobj,
698c2ecf20Sopenharmony_ci		       struct bin_attribute *attr,
708c2ecf20Sopenharmony_ci		       char *buf, loff_t off, size_t count)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	return esas2r_read_fw(a, buf, off, count);
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic ssize_t write_fw(struct file *file, struct kobject *kobj,
788c2ecf20Sopenharmony_ci			struct bin_attribute *attr,
798c2ecf20Sopenharmony_ci			char *buf, loff_t off, size_t count)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	return esas2r_write_fw(a, buf, off, count);
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic ssize_t read_fs(struct file *file, struct kobject *kobj,
878c2ecf20Sopenharmony_ci		       struct bin_attribute *attr,
888c2ecf20Sopenharmony_ci		       char *buf, loff_t off, size_t count)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	return esas2r_read_fs(a, buf, off, count);
938c2ecf20Sopenharmony_ci}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistatic ssize_t write_fs(struct file *file, struct kobject *kobj,
968c2ecf20Sopenharmony_ci			struct bin_attribute *attr,
978c2ecf20Sopenharmony_ci			char *buf, loff_t off, size_t count)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
1008c2ecf20Sopenharmony_ci	int length = min(sizeof(struct esas2r_ioctl_fs), count);
1018c2ecf20Sopenharmony_ci	int result = 0;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	result = esas2r_write_fs(a, buf, off, count);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	if (result < 0)
1068c2ecf20Sopenharmony_ci		result = 0;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	return length;
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic ssize_t read_vda(struct file *file, struct kobject *kobj,
1128c2ecf20Sopenharmony_ci			struct bin_attribute *attr,
1138c2ecf20Sopenharmony_ci			char *buf, loff_t off, size_t count)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	return esas2r_read_vda(a, buf, off, count);
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic ssize_t write_vda(struct file *file, struct kobject *kobj,
1218c2ecf20Sopenharmony_ci			 struct bin_attribute *attr,
1228c2ecf20Sopenharmony_ci			 char *buf, loff_t off, size_t count)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	return esas2r_write_vda(a, buf, off, count);
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic ssize_t read_live_nvram(struct file *file, struct kobject *kobj,
1308c2ecf20Sopenharmony_ci			       struct bin_attribute *attr,
1318c2ecf20Sopenharmony_ci			       char *buf, loff_t off, size_t count)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
1348c2ecf20Sopenharmony_ci	int length = min_t(size_t, sizeof(struct esas2r_sas_nvram), PAGE_SIZE);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	memcpy(buf, a->nvram, length);
1378c2ecf20Sopenharmony_ci	return length;
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic ssize_t write_live_nvram(struct file *file, struct kobject *kobj,
1418c2ecf20Sopenharmony_ci				struct bin_attribute *attr,
1428c2ecf20Sopenharmony_ci				char *buf, loff_t off, size_t count)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
1458c2ecf20Sopenharmony_ci	struct esas2r_request *rq;
1468c2ecf20Sopenharmony_ci	int result = -EFAULT;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	rq = esas2r_alloc_request(a);
1498c2ecf20Sopenharmony_ci	if (rq == NULL)
1508c2ecf20Sopenharmony_ci		return -ENOMEM;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	if (esas2r_write_params(a, rq, (struct esas2r_sas_nvram *)buf))
1538c2ecf20Sopenharmony_ci		result = count;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	esas2r_free_request(a, rq);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	return result;
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic ssize_t read_default_nvram(struct file *file, struct kobject *kobj,
1618c2ecf20Sopenharmony_ci				  struct bin_attribute *attr,
1628c2ecf20Sopenharmony_ci				  char *buf, loff_t off, size_t count)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	esas2r_nvram_get_defaults(a, (struct esas2r_sas_nvram *)buf);
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	return sizeof(struct esas2r_sas_nvram);
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic ssize_t read_hw(struct file *file, struct kobject *kobj,
1728c2ecf20Sopenharmony_ci		       struct bin_attribute *attr,
1738c2ecf20Sopenharmony_ci		       char *buf, loff_t off, size_t count)
1748c2ecf20Sopenharmony_ci{
1758c2ecf20Sopenharmony_ci	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
1768c2ecf20Sopenharmony_ci	int length = min_t(size_t, sizeof(struct atto_ioctl), PAGE_SIZE);
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	if (!a->local_atto_ioctl)
1798c2ecf20Sopenharmony_ci		return -ENOMEM;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	if (handle_hba_ioctl(a, a->local_atto_ioctl) != IOCTL_SUCCESS)
1828c2ecf20Sopenharmony_ci		return -ENOMEM;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	memcpy(buf, a->local_atto_ioctl, length);
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	return length;
1878c2ecf20Sopenharmony_ci}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cistatic ssize_t write_hw(struct file *file, struct kobject *kobj,
1908c2ecf20Sopenharmony_ci			struct bin_attribute *attr,
1918c2ecf20Sopenharmony_ci			char *buf, loff_t off, size_t count)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
1948c2ecf20Sopenharmony_ci	int length = min(sizeof(struct atto_ioctl), count);
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	if (!a->local_atto_ioctl) {
1978c2ecf20Sopenharmony_ci		a->local_atto_ioctl = kmalloc(sizeof(struct atto_ioctl),
1988c2ecf20Sopenharmony_ci					      GFP_KERNEL);
1998c2ecf20Sopenharmony_ci		if (a->local_atto_ioctl == NULL) {
2008c2ecf20Sopenharmony_ci			esas2r_log(ESAS2R_LOG_WARN,
2018c2ecf20Sopenharmony_ci				   "write_hw kzalloc failed for %zu bytes",
2028c2ecf20Sopenharmony_ci				   sizeof(struct atto_ioctl));
2038c2ecf20Sopenharmony_ci			return -ENOMEM;
2048c2ecf20Sopenharmony_ci		}
2058c2ecf20Sopenharmony_ci	}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	memset(a->local_atto_ioctl, 0, sizeof(struct atto_ioctl));
2088c2ecf20Sopenharmony_ci	memcpy(a->local_atto_ioctl, buf, length);
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	return length;
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci#define ESAS2R_RW_BIN_ATTR(_name) \
2148c2ecf20Sopenharmony_ci	struct bin_attribute bin_attr_ ## _name = { \
2158c2ecf20Sopenharmony_ci		.attr	= \
2168c2ecf20Sopenharmony_ci		{ .name = __stringify(_name), .mode  = S_IRUSR | S_IWUSR }, \
2178c2ecf20Sopenharmony_ci		.size	= 0, \
2188c2ecf20Sopenharmony_ci		.read	= read_ ## _name, \
2198c2ecf20Sopenharmony_ci		.write	= write_ ## _name }
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ciESAS2R_RW_BIN_ATTR(fw);
2228c2ecf20Sopenharmony_ciESAS2R_RW_BIN_ATTR(fs);
2238c2ecf20Sopenharmony_ciESAS2R_RW_BIN_ATTR(vda);
2248c2ecf20Sopenharmony_ciESAS2R_RW_BIN_ATTR(hw);
2258c2ecf20Sopenharmony_ciESAS2R_RW_BIN_ATTR(live_nvram);
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_cistruct bin_attribute bin_attr_default_nvram = {
2288c2ecf20Sopenharmony_ci	.attr	= { .name = "default_nvram", .mode = S_IRUGO },
2298c2ecf20Sopenharmony_ci	.size	= 0,
2308c2ecf20Sopenharmony_ci	.read	= read_default_nvram,
2318c2ecf20Sopenharmony_ci	.write	= NULL
2328c2ecf20Sopenharmony_ci};
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_cistatic struct scsi_host_template driver_template = {
2358c2ecf20Sopenharmony_ci	.module				= THIS_MODULE,
2368c2ecf20Sopenharmony_ci	.show_info			= esas2r_show_info,
2378c2ecf20Sopenharmony_ci	.name				= ESAS2R_LONGNAME,
2388c2ecf20Sopenharmony_ci	.info				= esas2r_info,
2398c2ecf20Sopenharmony_ci	.ioctl				= esas2r_ioctl,
2408c2ecf20Sopenharmony_ci	.queuecommand			= esas2r_queuecommand,
2418c2ecf20Sopenharmony_ci	.eh_abort_handler		= esas2r_eh_abort,
2428c2ecf20Sopenharmony_ci	.eh_device_reset_handler	= esas2r_device_reset,
2438c2ecf20Sopenharmony_ci	.eh_bus_reset_handler		= esas2r_bus_reset,
2448c2ecf20Sopenharmony_ci	.eh_host_reset_handler		= esas2r_host_reset,
2458c2ecf20Sopenharmony_ci	.eh_target_reset_handler	= esas2r_target_reset,
2468c2ecf20Sopenharmony_ci	.can_queue			= 128,
2478c2ecf20Sopenharmony_ci	.this_id			= -1,
2488c2ecf20Sopenharmony_ci	.sg_tablesize			= SG_CHUNK_SIZE,
2498c2ecf20Sopenharmony_ci	.cmd_per_lun			=
2508c2ecf20Sopenharmony_ci		ESAS2R_DEFAULT_CMD_PER_LUN,
2518c2ecf20Sopenharmony_ci	.present			= 0,
2528c2ecf20Sopenharmony_ci	.unchecked_isa_dma		= 0,
2538c2ecf20Sopenharmony_ci	.emulated			= 0,
2548c2ecf20Sopenharmony_ci	.proc_name			= ESAS2R_DRVR_NAME,
2558c2ecf20Sopenharmony_ci	.change_queue_depth		= scsi_change_queue_depth,
2568c2ecf20Sopenharmony_ci	.max_sectors			= 0xFFFF,
2578c2ecf20Sopenharmony_ci};
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ciint sgl_page_size = 512;
2608c2ecf20Sopenharmony_cimodule_param(sgl_page_size, int, 0);
2618c2ecf20Sopenharmony_ciMODULE_PARM_DESC(sgl_page_size,
2628c2ecf20Sopenharmony_ci		 "Scatter/gather list (SGL) page size in number of S/G "
2638c2ecf20Sopenharmony_ci		 "entries.  If your application is doing a lot of very large "
2648c2ecf20Sopenharmony_ci		 "transfers, you may want to increase the SGL page size.  "
2658c2ecf20Sopenharmony_ci		 "Default 512.");
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ciint num_sg_lists = 1024;
2688c2ecf20Sopenharmony_cimodule_param(num_sg_lists, int, 0);
2698c2ecf20Sopenharmony_ciMODULE_PARM_DESC(num_sg_lists,
2708c2ecf20Sopenharmony_ci		 "Number of scatter/gather lists.  Default 1024.");
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ciint sg_tablesize = SG_CHUNK_SIZE;
2738c2ecf20Sopenharmony_cimodule_param(sg_tablesize, int, 0);
2748c2ecf20Sopenharmony_ciMODULE_PARM_DESC(sg_tablesize,
2758c2ecf20Sopenharmony_ci		 "Maximum number of entries in a scatter/gather table.");
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ciint num_requests = 256;
2788c2ecf20Sopenharmony_cimodule_param(num_requests, int, 0);
2798c2ecf20Sopenharmony_ciMODULE_PARM_DESC(num_requests,
2808c2ecf20Sopenharmony_ci		 "Number of requests.  Default 256.");
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ciint num_ae_requests = 4;
2838c2ecf20Sopenharmony_cimodule_param(num_ae_requests, int, 0);
2848c2ecf20Sopenharmony_ciMODULE_PARM_DESC(num_ae_requests,
2858c2ecf20Sopenharmony_ci		 "Number of VDA asynchronous event requests.  Default 4.");
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ciint cmd_per_lun = ESAS2R_DEFAULT_CMD_PER_LUN;
2888c2ecf20Sopenharmony_cimodule_param(cmd_per_lun, int, 0);
2898c2ecf20Sopenharmony_ciMODULE_PARM_DESC(cmd_per_lun,
2908c2ecf20Sopenharmony_ci		 "Maximum number of commands per LUN.  Default "
2918c2ecf20Sopenharmony_ci		 DEFINED_NUM_TO_STR(ESAS2R_DEFAULT_CMD_PER_LUN) ".");
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ciint can_queue = 128;
2948c2ecf20Sopenharmony_cimodule_param(can_queue, int, 0);
2958c2ecf20Sopenharmony_ciMODULE_PARM_DESC(can_queue,
2968c2ecf20Sopenharmony_ci		 "Maximum number of commands per adapter.  Default 128.");
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ciint esas2r_max_sectors = 0xFFFF;
2998c2ecf20Sopenharmony_cimodule_param(esas2r_max_sectors, int, 0);
3008c2ecf20Sopenharmony_ciMODULE_PARM_DESC(esas2r_max_sectors,
3018c2ecf20Sopenharmony_ci		 "Maximum number of disk sectors in a single data transfer.  "
3028c2ecf20Sopenharmony_ci		 "Default 65535 (largest possible setting).");
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ciint interrupt_mode = 1;
3058c2ecf20Sopenharmony_cimodule_param(interrupt_mode, int, 0);
3068c2ecf20Sopenharmony_ciMODULE_PARM_DESC(interrupt_mode,
3078c2ecf20Sopenharmony_ci		 "Defines the interrupt mode to use.  0 for legacy"
3088c2ecf20Sopenharmony_ci		 ", 1 for MSI.  Default is MSI (1).");
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_cistatic const struct pci_device_id
3118c2ecf20Sopenharmony_ci	esas2r_pci_table[] = {
3128c2ecf20Sopenharmony_ci	{ ATTO_VENDOR_ID, 0x0049,	  ATTO_VENDOR_ID, 0x0049,
3138c2ecf20Sopenharmony_ci	  0,
3148c2ecf20Sopenharmony_ci	  0, 0 },
3158c2ecf20Sopenharmony_ci	{ ATTO_VENDOR_ID, 0x0049,	  ATTO_VENDOR_ID, 0x004A,
3168c2ecf20Sopenharmony_ci	  0,
3178c2ecf20Sopenharmony_ci	  0, 0 },
3188c2ecf20Sopenharmony_ci	{ ATTO_VENDOR_ID, 0x0049,	  ATTO_VENDOR_ID, 0x004B,
3198c2ecf20Sopenharmony_ci	  0,
3208c2ecf20Sopenharmony_ci	  0, 0 },
3218c2ecf20Sopenharmony_ci	{ ATTO_VENDOR_ID, 0x0049,	  ATTO_VENDOR_ID, 0x004C,
3228c2ecf20Sopenharmony_ci	  0,
3238c2ecf20Sopenharmony_ci	  0, 0 },
3248c2ecf20Sopenharmony_ci	{ ATTO_VENDOR_ID, 0x0049,	  ATTO_VENDOR_ID, 0x004D,
3258c2ecf20Sopenharmony_ci	  0,
3268c2ecf20Sopenharmony_ci	  0, 0 },
3278c2ecf20Sopenharmony_ci	{ ATTO_VENDOR_ID, 0x0049,	  ATTO_VENDOR_ID, 0x004E,
3288c2ecf20Sopenharmony_ci	  0,
3298c2ecf20Sopenharmony_ci	  0, 0 },
3308c2ecf20Sopenharmony_ci	{ 0,		  0,		  0,		  0,
3318c2ecf20Sopenharmony_ci	  0,
3328c2ecf20Sopenharmony_ci	  0, 0 }
3338c2ecf20Sopenharmony_ci};
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, esas2r_pci_table);
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_cistatic int
3388c2ecf20Sopenharmony_ciesas2r_probe(struct pci_dev *pcid, const struct pci_device_id *id);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_cistatic void
3418c2ecf20Sopenharmony_ciesas2r_remove(struct pci_dev *pcid);
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_cistatic struct pci_driver
3448c2ecf20Sopenharmony_ci	esas2r_pci_driver = {
3458c2ecf20Sopenharmony_ci	.name		= ESAS2R_DRVR_NAME,
3468c2ecf20Sopenharmony_ci	.id_table	= esas2r_pci_table,
3478c2ecf20Sopenharmony_ci	.probe		= esas2r_probe,
3488c2ecf20Sopenharmony_ci	.remove		= esas2r_remove,
3498c2ecf20Sopenharmony_ci	.suspend	= esas2r_suspend,
3508c2ecf20Sopenharmony_ci	.resume		= esas2r_resume,
3518c2ecf20Sopenharmony_ci};
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_cistatic int esas2r_probe(struct pci_dev *pcid,
3548c2ecf20Sopenharmony_ci			const struct pci_device_id *id)
3558c2ecf20Sopenharmony_ci{
3568c2ecf20Sopenharmony_ci	struct Scsi_Host *host = NULL;
3578c2ecf20Sopenharmony_ci	struct esas2r_adapter *a;
3588c2ecf20Sopenharmony_ci	int err;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	size_t host_alloc_size = sizeof(struct esas2r_adapter)
3618c2ecf20Sopenharmony_ci				 + ((num_requests) +
3628c2ecf20Sopenharmony_ci				    1) * sizeof(struct esas2r_request);
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	esas2r_log_dev(ESAS2R_LOG_DEBG, &(pcid->dev),
3658c2ecf20Sopenharmony_ci		       "esas2r_probe() 0x%02x 0x%02x 0x%02x 0x%02x",
3668c2ecf20Sopenharmony_ci		       pcid->vendor,
3678c2ecf20Sopenharmony_ci		       pcid->device,
3688c2ecf20Sopenharmony_ci		       pcid->subsystem_vendor,
3698c2ecf20Sopenharmony_ci		       pcid->subsystem_device);
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
3728c2ecf20Sopenharmony_ci		       "before pci_enable_device() "
3738c2ecf20Sopenharmony_ci		       "enable_cnt: %d",
3748c2ecf20Sopenharmony_ci		       pcid->enable_cnt.counter);
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	err = pci_enable_device(pcid);
3778c2ecf20Sopenharmony_ci	if (err != 0) {
3788c2ecf20Sopenharmony_ci		esas2r_log_dev(ESAS2R_LOG_CRIT, &(pcid->dev),
3798c2ecf20Sopenharmony_ci			       "pci_enable_device() FAIL (%d)",
3808c2ecf20Sopenharmony_ci			       err);
3818c2ecf20Sopenharmony_ci		return -ENODEV;
3828c2ecf20Sopenharmony_ci	}
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
3858c2ecf20Sopenharmony_ci		       "pci_enable_device() OK");
3868c2ecf20Sopenharmony_ci	esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
3878c2ecf20Sopenharmony_ci		       "after pci_enable_device() enable_cnt: %d",
3888c2ecf20Sopenharmony_ci		       pcid->enable_cnt.counter);
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	host = scsi_host_alloc(&driver_template, host_alloc_size);
3918c2ecf20Sopenharmony_ci	if (host == NULL) {
3928c2ecf20Sopenharmony_ci		esas2r_log(ESAS2R_LOG_CRIT, "scsi_host_alloc() FAIL");
3938c2ecf20Sopenharmony_ci		return -ENODEV;
3948c2ecf20Sopenharmony_ci	}
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	memset(host->hostdata, 0, host_alloc_size);
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	a = (struct esas2r_adapter *)host->hostdata;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	esas2r_log(ESAS2R_LOG_INFO, "scsi_host_alloc() OK host: %p", host);
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	/* override max LUN and max target id */
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	host->max_id = ESAS2R_MAX_ID + 1;
4058c2ecf20Sopenharmony_ci	host->max_lun = 255;
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	/* we can handle 16-byte CDbs */
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	host->max_cmd_len = 16;
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	host->can_queue = can_queue;
4128c2ecf20Sopenharmony_ci	host->cmd_per_lun = cmd_per_lun;
4138c2ecf20Sopenharmony_ci	host->this_id = host->max_id + 1;
4148c2ecf20Sopenharmony_ci	host->max_channel = 0;
4158c2ecf20Sopenharmony_ci	host->unique_id = found_adapters;
4168c2ecf20Sopenharmony_ci	host->sg_tablesize = sg_tablesize;
4178c2ecf20Sopenharmony_ci	host->max_sectors = esas2r_max_sectors;
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	/* set to bus master for BIOses that don't do it for us */
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	esas2r_log(ESAS2R_LOG_INFO, "pci_set_master() called");
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	pci_set_master(pcid);
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	if (!esas2r_init_adapter(host, pcid, found_adapters)) {
4268c2ecf20Sopenharmony_ci		esas2r_log(ESAS2R_LOG_CRIT,
4278c2ecf20Sopenharmony_ci			   "unable to initialize device at PCI bus %x:%x",
4288c2ecf20Sopenharmony_ci			   pcid->bus->number,
4298c2ecf20Sopenharmony_ci			   pcid->devfn);
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci		esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
4328c2ecf20Sopenharmony_ci			       "scsi_host_put() called");
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci		scsi_host_put(host);
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci		return 0;
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci	}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	esas2r_log(ESAS2R_LOG_INFO, "pci_set_drvdata(%p, %p) called", pcid,
4418c2ecf20Sopenharmony_ci		   host->hostdata);
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	pci_set_drvdata(pcid, host);
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	esas2r_log(ESAS2R_LOG_INFO, "scsi_add_host() called");
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci	err = scsi_add_host(host, &pcid->dev);
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	if (err) {
4508c2ecf20Sopenharmony_ci		esas2r_log(ESAS2R_LOG_CRIT, "scsi_add_host returned %d", err);
4518c2ecf20Sopenharmony_ci		esas2r_log_dev(ESAS2R_LOG_CRIT, &(host->shost_gendev),
4528c2ecf20Sopenharmony_ci			       "scsi_add_host() FAIL");
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci		esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
4558c2ecf20Sopenharmony_ci			       "scsi_host_put() called");
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci		scsi_host_put(host);
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci		esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
4608c2ecf20Sopenharmony_ci			       "pci_set_drvdata(%p, NULL) called",
4618c2ecf20Sopenharmony_ci			       pcid);
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci		pci_set_drvdata(pcid, NULL);
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci		return -ENODEV;
4668c2ecf20Sopenharmony_ci	}
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	esas2r_fw_event_on(a);
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
4728c2ecf20Sopenharmony_ci		       "scsi_scan_host() called");
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	scsi_scan_host(host);
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	/* Add sysfs binary files */
4778c2ecf20Sopenharmony_ci	if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_fw))
4788c2ecf20Sopenharmony_ci		esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
4798c2ecf20Sopenharmony_ci			       "Failed to create sysfs binary file: fw");
4808c2ecf20Sopenharmony_ci	else
4818c2ecf20Sopenharmony_ci		a->sysfs_fw_created = 1;
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_fs))
4848c2ecf20Sopenharmony_ci		esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
4858c2ecf20Sopenharmony_ci			       "Failed to create sysfs binary file: fs");
4868c2ecf20Sopenharmony_ci	else
4878c2ecf20Sopenharmony_ci		a->sysfs_fs_created = 1;
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_vda))
4908c2ecf20Sopenharmony_ci		esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
4918c2ecf20Sopenharmony_ci			       "Failed to create sysfs binary file: vda");
4928c2ecf20Sopenharmony_ci	else
4938c2ecf20Sopenharmony_ci		a->sysfs_vda_created = 1;
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_hw))
4968c2ecf20Sopenharmony_ci		esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
4978c2ecf20Sopenharmony_ci			       "Failed to create sysfs binary file: hw");
4988c2ecf20Sopenharmony_ci	else
4998c2ecf20Sopenharmony_ci		a->sysfs_hw_created = 1;
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_live_nvram))
5028c2ecf20Sopenharmony_ci		esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
5038c2ecf20Sopenharmony_ci			       "Failed to create sysfs binary file: live_nvram");
5048c2ecf20Sopenharmony_ci	else
5058c2ecf20Sopenharmony_ci		a->sysfs_live_nvram_created = 1;
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	if (sysfs_create_bin_file(&host->shost_dev.kobj,
5088c2ecf20Sopenharmony_ci				  &bin_attr_default_nvram))
5098c2ecf20Sopenharmony_ci		esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
5108c2ecf20Sopenharmony_ci			       "Failed to create sysfs binary file: default_nvram");
5118c2ecf20Sopenharmony_ci	else
5128c2ecf20Sopenharmony_ci		a->sysfs_default_nvram_created = 1;
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	found_adapters++;
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	return 0;
5178c2ecf20Sopenharmony_ci}
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_cistatic void esas2r_remove(struct pci_dev *pdev)
5208c2ecf20Sopenharmony_ci{
5218c2ecf20Sopenharmony_ci	struct Scsi_Host *host = pci_get_drvdata(pdev);
5228c2ecf20Sopenharmony_ci	struct esas2r_adapter *a = (struct esas2r_adapter *)host->hostdata;
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev),
5258c2ecf20Sopenharmony_ci		       "esas2r_remove(%p) called; "
5268c2ecf20Sopenharmony_ci		       "host:%p", pdev,
5278c2ecf20Sopenharmony_ci		       host);
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	esas2r_kill_adapter(a->index);
5308c2ecf20Sopenharmony_ci	found_adapters--;
5318c2ecf20Sopenharmony_ci}
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_cistatic int __init esas2r_init(void)
5348c2ecf20Sopenharmony_ci{
5358c2ecf20Sopenharmony_ci	int i;
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	esas2r_log(ESAS2R_LOG_INFO, "%s called", __func__);
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	/* verify valid parameters */
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	if (can_queue < 1) {
5428c2ecf20Sopenharmony_ci		esas2r_log(ESAS2R_LOG_WARN,
5438c2ecf20Sopenharmony_ci			   "warning: can_queue must be at least 1, value "
5448c2ecf20Sopenharmony_ci			   "forced.");
5458c2ecf20Sopenharmony_ci		can_queue = 1;
5468c2ecf20Sopenharmony_ci	} else if (can_queue > 2048) {
5478c2ecf20Sopenharmony_ci		esas2r_log(ESAS2R_LOG_WARN,
5488c2ecf20Sopenharmony_ci			   "warning: can_queue must be no larger than 2048, "
5498c2ecf20Sopenharmony_ci			   "value forced.");
5508c2ecf20Sopenharmony_ci		can_queue = 2048;
5518c2ecf20Sopenharmony_ci	}
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci	if (cmd_per_lun < 1) {
5548c2ecf20Sopenharmony_ci		esas2r_log(ESAS2R_LOG_WARN,
5558c2ecf20Sopenharmony_ci			   "warning: cmd_per_lun must be at least 1, value "
5568c2ecf20Sopenharmony_ci			   "forced.");
5578c2ecf20Sopenharmony_ci		cmd_per_lun = 1;
5588c2ecf20Sopenharmony_ci	} else if (cmd_per_lun > 2048) {
5598c2ecf20Sopenharmony_ci		esas2r_log(ESAS2R_LOG_WARN,
5608c2ecf20Sopenharmony_ci			   "warning: cmd_per_lun must be no larger than "
5618c2ecf20Sopenharmony_ci			   "2048, value forced.");
5628c2ecf20Sopenharmony_ci		cmd_per_lun = 2048;
5638c2ecf20Sopenharmony_ci	}
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	if (sg_tablesize < 32) {
5668c2ecf20Sopenharmony_ci		esas2r_log(ESAS2R_LOG_WARN,
5678c2ecf20Sopenharmony_ci			   "warning: sg_tablesize must be at least 32, "
5688c2ecf20Sopenharmony_ci			   "value forced.");
5698c2ecf20Sopenharmony_ci		sg_tablesize = 32;
5708c2ecf20Sopenharmony_ci	}
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci	if (esas2r_max_sectors < 1) {
5738c2ecf20Sopenharmony_ci		esas2r_log(ESAS2R_LOG_WARN,
5748c2ecf20Sopenharmony_ci			   "warning: esas2r_max_sectors must be at least "
5758c2ecf20Sopenharmony_ci			   "1, value forced.");
5768c2ecf20Sopenharmony_ci		esas2r_max_sectors = 1;
5778c2ecf20Sopenharmony_ci	} else if (esas2r_max_sectors > 0xffff) {
5788c2ecf20Sopenharmony_ci		esas2r_log(ESAS2R_LOG_WARN,
5798c2ecf20Sopenharmony_ci			   "warning: esas2r_max_sectors must be no larger "
5808c2ecf20Sopenharmony_ci			   "than 0xffff, value forced.");
5818c2ecf20Sopenharmony_ci		esas2r_max_sectors = 0xffff;
5828c2ecf20Sopenharmony_ci	}
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci	sgl_page_size &= ~(ESAS2R_SGL_ALIGN - 1);
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	if (sgl_page_size < SGL_PG_SZ_MIN)
5878c2ecf20Sopenharmony_ci		sgl_page_size = SGL_PG_SZ_MIN;
5888c2ecf20Sopenharmony_ci	else if (sgl_page_size > SGL_PG_SZ_MAX)
5898c2ecf20Sopenharmony_ci		sgl_page_size = SGL_PG_SZ_MAX;
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	if (num_sg_lists < NUM_SGL_MIN)
5928c2ecf20Sopenharmony_ci		num_sg_lists = NUM_SGL_MIN;
5938c2ecf20Sopenharmony_ci	else if (num_sg_lists > NUM_SGL_MAX)
5948c2ecf20Sopenharmony_ci		num_sg_lists = NUM_SGL_MAX;
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	if (num_requests < NUM_REQ_MIN)
5978c2ecf20Sopenharmony_ci		num_requests = NUM_REQ_MIN;
5988c2ecf20Sopenharmony_ci	else if (num_requests > NUM_REQ_MAX)
5998c2ecf20Sopenharmony_ci		num_requests = NUM_REQ_MAX;
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci	if (num_ae_requests < NUM_AE_MIN)
6028c2ecf20Sopenharmony_ci		num_ae_requests = NUM_AE_MIN;
6038c2ecf20Sopenharmony_ci	else if (num_ae_requests > NUM_AE_MAX)
6048c2ecf20Sopenharmony_ci		num_ae_requests = NUM_AE_MAX;
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	/* set up other globals */
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_ADAPTERS; i++)
6098c2ecf20Sopenharmony_ci		esas2r_adapters[i] = NULL;
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci	return pci_register_driver(&esas2r_pci_driver);
6128c2ecf20Sopenharmony_ci}
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci/* Handle ioctl calls to "/proc/scsi/esas2r/ATTOnode" */
6158c2ecf20Sopenharmony_cistatic const struct file_operations esas2r_proc_fops = {
6168c2ecf20Sopenharmony_ci	.compat_ioctl	= compat_ptr_ioctl,
6178c2ecf20Sopenharmony_ci	.unlocked_ioctl = esas2r_proc_ioctl,
6188c2ecf20Sopenharmony_ci};
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_cistatic const struct proc_ops esas2r_proc_ops = {
6218c2ecf20Sopenharmony_ci	.proc_ioctl		= esas2r_proc_ioctl,
6228c2ecf20Sopenharmony_ci#ifdef CONFIG_COMPAT
6238c2ecf20Sopenharmony_ci	.proc_compat_ioctl	= compat_ptr_ioctl,
6248c2ecf20Sopenharmony_ci#endif
6258c2ecf20Sopenharmony_ci};
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_cistatic struct Scsi_Host *esas2r_proc_host;
6288c2ecf20Sopenharmony_cistatic int esas2r_proc_major;
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_cilong esas2r_proc_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
6318c2ecf20Sopenharmony_ci{
6328c2ecf20Sopenharmony_ci	return esas2r_ioctl_handler(esas2r_proc_host->hostdata,
6338c2ecf20Sopenharmony_ci				    cmd, (void __user *)arg);
6348c2ecf20Sopenharmony_ci}
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_cistatic void __exit esas2r_exit(void)
6378c2ecf20Sopenharmony_ci{
6388c2ecf20Sopenharmony_ci	esas2r_log(ESAS2R_LOG_INFO, "%s called", __func__);
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	if (esas2r_proc_major > 0) {
6418c2ecf20Sopenharmony_ci		esas2r_log(ESAS2R_LOG_INFO, "unregister proc");
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci		remove_proc_entry(ATTONODE_NAME,
6448c2ecf20Sopenharmony_ci				  esas2r_proc_host->hostt->proc_dir);
6458c2ecf20Sopenharmony_ci		unregister_chrdev(esas2r_proc_major, ESAS2R_DRVR_NAME);
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci		esas2r_proc_major = 0;
6488c2ecf20Sopenharmony_ci	}
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci	esas2r_log(ESAS2R_LOG_INFO, "pci_unregister_driver() called");
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci	pci_unregister_driver(&esas2r_pci_driver);
6538c2ecf20Sopenharmony_ci}
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ciint esas2r_show_info(struct seq_file *m, struct Scsi_Host *sh)
6568c2ecf20Sopenharmony_ci{
6578c2ecf20Sopenharmony_ci	struct esas2r_adapter *a = (struct esas2r_adapter *)sh->hostdata;
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci	struct esas2r_target *t;
6608c2ecf20Sopenharmony_ci	int dev_count = 0;
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	esas2r_log(ESAS2R_LOG_DEBG, "esas2r_show_info (%p,%d)", m, sh->host_no);
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	seq_printf(m, ESAS2R_LONGNAME "\n"
6658c2ecf20Sopenharmony_ci		   "Driver version: "ESAS2R_VERSION_STR "\n"
6668c2ecf20Sopenharmony_ci		   "Flash version: %s\n"
6678c2ecf20Sopenharmony_ci		   "Firmware version: %s\n"
6688c2ecf20Sopenharmony_ci		   "Copyright "ESAS2R_COPYRIGHT_YEARS "\n"
6698c2ecf20Sopenharmony_ci		   "http://www.attotech.com\n"
6708c2ecf20Sopenharmony_ci		   "\n",
6718c2ecf20Sopenharmony_ci		   a->flash_rev,
6728c2ecf20Sopenharmony_ci		   a->fw_rev[0] ? a->fw_rev : "(none)");
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci	seq_printf(m, "Adapter information:\n"
6768c2ecf20Sopenharmony_ci		   "--------------------\n"
6778c2ecf20Sopenharmony_ci		   "Model: %s\n"
6788c2ecf20Sopenharmony_ci		   "SAS address: %02X%02X%02X%02X:%02X%02X%02X%02X\n",
6798c2ecf20Sopenharmony_ci		   esas2r_get_model_name(a),
6808c2ecf20Sopenharmony_ci		   a->nvram->sas_addr[0],
6818c2ecf20Sopenharmony_ci		   a->nvram->sas_addr[1],
6828c2ecf20Sopenharmony_ci		   a->nvram->sas_addr[2],
6838c2ecf20Sopenharmony_ci		   a->nvram->sas_addr[3],
6848c2ecf20Sopenharmony_ci		   a->nvram->sas_addr[4],
6858c2ecf20Sopenharmony_ci		   a->nvram->sas_addr[5],
6868c2ecf20Sopenharmony_ci		   a->nvram->sas_addr[6],
6878c2ecf20Sopenharmony_ci		   a->nvram->sas_addr[7]);
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci	seq_puts(m, "\n"
6908c2ecf20Sopenharmony_ci		   "Discovered devices:\n"
6918c2ecf20Sopenharmony_ci		   "\n"
6928c2ecf20Sopenharmony_ci		   "   #  Target ID\n"
6938c2ecf20Sopenharmony_ci		   "---------------\n");
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci	for (t = a->targetdb; t < a->targetdb_end; t++)
6968c2ecf20Sopenharmony_ci		if (t->buffered_target_state == TS_PRESENT) {
6978c2ecf20Sopenharmony_ci			seq_printf(m, " %3d   %3d\n",
6988c2ecf20Sopenharmony_ci				   ++dev_count,
6998c2ecf20Sopenharmony_ci				   (u16)(uintptr_t)(t - a->targetdb));
7008c2ecf20Sopenharmony_ci		}
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci	if (dev_count == 0)
7038c2ecf20Sopenharmony_ci		seq_puts(m, "none\n");
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	seq_putc(m, '\n');
7068c2ecf20Sopenharmony_ci	return 0;
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci}
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ciconst char *esas2r_info(struct Scsi_Host *sh)
7118c2ecf20Sopenharmony_ci{
7128c2ecf20Sopenharmony_ci	struct esas2r_adapter *a = (struct esas2r_adapter *)sh->hostdata;
7138c2ecf20Sopenharmony_ci	static char esas2r_info_str[512];
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	esas2r_log_dev(ESAS2R_LOG_INFO, &(sh->shost_gendev),
7168c2ecf20Sopenharmony_ci		       "esas2r_info() called");
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci	/*
7198c2ecf20Sopenharmony_ci	 * if we haven't done so already, register as a char driver
7208c2ecf20Sopenharmony_ci	 * and stick a node under "/proc/scsi/esas2r/ATTOnode"
7218c2ecf20Sopenharmony_ci	 */
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	if (esas2r_proc_major <= 0) {
7248c2ecf20Sopenharmony_ci		esas2r_proc_host = sh;
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci		esas2r_proc_major = register_chrdev(0, ESAS2R_DRVR_NAME,
7278c2ecf20Sopenharmony_ci						    &esas2r_proc_fops);
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci		esas2r_log_dev(ESAS2R_LOG_DEBG, &(sh->shost_gendev),
7308c2ecf20Sopenharmony_ci			       "register_chrdev (major %d)",
7318c2ecf20Sopenharmony_ci			       esas2r_proc_major);
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci		if (esas2r_proc_major > 0) {
7348c2ecf20Sopenharmony_ci			struct proc_dir_entry *pde;
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ci			pde = proc_create(ATTONODE_NAME, 0,
7378c2ecf20Sopenharmony_ci					  sh->hostt->proc_dir,
7388c2ecf20Sopenharmony_ci					  &esas2r_proc_ops);
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci			if (!pde) {
7418c2ecf20Sopenharmony_ci				esas2r_log_dev(ESAS2R_LOG_WARN,
7428c2ecf20Sopenharmony_ci					       &(sh->shost_gendev),
7438c2ecf20Sopenharmony_ci					       "failed to create_proc_entry");
7448c2ecf20Sopenharmony_ci				esas2r_proc_major = -1;
7458c2ecf20Sopenharmony_ci			}
7468c2ecf20Sopenharmony_ci		}
7478c2ecf20Sopenharmony_ci	}
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci	sprintf(esas2r_info_str,
7508c2ecf20Sopenharmony_ci		ESAS2R_LONGNAME " (bus 0x%02X, device 0x%02X, IRQ 0x%02X)"
7518c2ecf20Sopenharmony_ci		" driver version: "ESAS2R_VERSION_STR "  firmware version: "
7528c2ecf20Sopenharmony_ci		"%s\n",
7538c2ecf20Sopenharmony_ci		a->pcid->bus->number, a->pcid->devfn, a->pcid->irq,
7548c2ecf20Sopenharmony_ci		a->fw_rev[0] ? a->fw_rev : "(none)");
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci	return esas2r_info_str;
7578c2ecf20Sopenharmony_ci}
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_ci/* Callback for building a request scatter/gather list */
7608c2ecf20Sopenharmony_cistatic u32 get_physaddr_from_sgc(struct esas2r_sg_context *sgc, u64 *addr)
7618c2ecf20Sopenharmony_ci{
7628c2ecf20Sopenharmony_ci	u32 len;
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci	if (likely(sgc->cur_offset == sgc->exp_offset)) {
7658c2ecf20Sopenharmony_ci		/*
7668c2ecf20Sopenharmony_ci		 * the normal case: caller used all bytes from previous call, so
7678c2ecf20Sopenharmony_ci		 * expected offset is the same as the current offset.
7688c2ecf20Sopenharmony_ci		 */
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci		if (sgc->sgel_count < sgc->num_sgel) {
7718c2ecf20Sopenharmony_ci			/* retrieve next segment, except for first time */
7728c2ecf20Sopenharmony_ci			if (sgc->exp_offset > (u8 *)0) {
7738c2ecf20Sopenharmony_ci				/* advance current segment */
7748c2ecf20Sopenharmony_ci				sgc->cur_sgel = sg_next(sgc->cur_sgel);
7758c2ecf20Sopenharmony_ci				++(sgc->sgel_count);
7768c2ecf20Sopenharmony_ci			}
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci			len = sg_dma_len(sgc->cur_sgel);
7808c2ecf20Sopenharmony_ci			(*addr) = sg_dma_address(sgc->cur_sgel);
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci			/* save the total # bytes returned to caller so far */
7838c2ecf20Sopenharmony_ci			sgc->exp_offset += len;
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci		} else {
7868c2ecf20Sopenharmony_ci			len = 0;
7878c2ecf20Sopenharmony_ci		}
7888c2ecf20Sopenharmony_ci	} else if (sgc->cur_offset < sgc->exp_offset) {
7898c2ecf20Sopenharmony_ci		/*
7908c2ecf20Sopenharmony_ci		 * caller did not use all bytes from previous call. need to
7918c2ecf20Sopenharmony_ci		 * compute the address based on current segment.
7928c2ecf20Sopenharmony_ci		 */
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci		len = sg_dma_len(sgc->cur_sgel);
7958c2ecf20Sopenharmony_ci		(*addr) = sg_dma_address(sgc->cur_sgel);
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci		sgc->exp_offset -= len;
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci		/* calculate PA based on prev segment address and offsets */
8008c2ecf20Sopenharmony_ci		*addr = *addr +
8018c2ecf20Sopenharmony_ci			(sgc->cur_offset - sgc->exp_offset);
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci		sgc->exp_offset += len;
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_ci		/* re-calculate length based on offset */
8068c2ecf20Sopenharmony_ci		len = lower_32_bits(
8078c2ecf20Sopenharmony_ci			sgc->exp_offset - sgc->cur_offset);
8088c2ecf20Sopenharmony_ci	} else {   /* if ( sgc->cur_offset > sgc->exp_offset ) */
8098c2ecf20Sopenharmony_ci		   /*
8108c2ecf20Sopenharmony_ci		    * we don't expect the caller to skip ahead.
8118c2ecf20Sopenharmony_ci		    * cur_offset will never exceed the len we return
8128c2ecf20Sopenharmony_ci		    */
8138c2ecf20Sopenharmony_ci		len = 0;
8148c2ecf20Sopenharmony_ci	}
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	return len;
8178c2ecf20Sopenharmony_ci}
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ciint esas2r_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
8208c2ecf20Sopenharmony_ci{
8218c2ecf20Sopenharmony_ci	struct esas2r_adapter *a =
8228c2ecf20Sopenharmony_ci		(struct esas2r_adapter *)cmd->device->host->hostdata;
8238c2ecf20Sopenharmony_ci	struct esas2r_request *rq;
8248c2ecf20Sopenharmony_ci	struct esas2r_sg_context sgc;
8258c2ecf20Sopenharmony_ci	unsigned bufflen;
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci	/* Assume success, if it fails we will fix the result later. */
8288c2ecf20Sopenharmony_ci	cmd->result = DID_OK << 16;
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	if (unlikely(test_bit(AF_DEGRADED_MODE, &a->flags))) {
8318c2ecf20Sopenharmony_ci		cmd->result = DID_NO_CONNECT << 16;
8328c2ecf20Sopenharmony_ci		cmd->scsi_done(cmd);
8338c2ecf20Sopenharmony_ci		return 0;
8348c2ecf20Sopenharmony_ci	}
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci	rq = esas2r_alloc_request(a);
8378c2ecf20Sopenharmony_ci	if (unlikely(rq == NULL)) {
8388c2ecf20Sopenharmony_ci		esas2r_debug("esas2r_alloc_request failed");
8398c2ecf20Sopenharmony_ci		return SCSI_MLQUEUE_HOST_BUSY;
8408c2ecf20Sopenharmony_ci	}
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci	rq->cmd = cmd;
8438c2ecf20Sopenharmony_ci	bufflen = scsi_bufflen(cmd);
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	if (likely(bufflen != 0)) {
8468c2ecf20Sopenharmony_ci		if (cmd->sc_data_direction == DMA_TO_DEVICE)
8478c2ecf20Sopenharmony_ci			rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_WRD);
8488c2ecf20Sopenharmony_ci		else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
8498c2ecf20Sopenharmony_ci			rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_RDD);
8508c2ecf20Sopenharmony_ci	}
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_ci	memcpy(rq->vrq->scsi.cdb, cmd->cmnd, cmd->cmd_len);
8538c2ecf20Sopenharmony_ci	rq->vrq->scsi.length = cpu_to_le32(bufflen);
8548c2ecf20Sopenharmony_ci	rq->target_id = cmd->device->id;
8558c2ecf20Sopenharmony_ci	rq->vrq->scsi.flags |= cpu_to_le32(cmd->device->lun);
8568c2ecf20Sopenharmony_ci	rq->sense_buf = cmd->sense_buffer;
8578c2ecf20Sopenharmony_ci	rq->sense_len = SCSI_SENSE_BUFFERSIZE;
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_ci	esas2r_sgc_init(&sgc, a, rq, NULL);
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci	sgc.length = bufflen;
8628c2ecf20Sopenharmony_ci	sgc.cur_offset = NULL;
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci	sgc.cur_sgel = scsi_sglist(cmd);
8658c2ecf20Sopenharmony_ci	sgc.exp_offset = NULL;
8668c2ecf20Sopenharmony_ci	sgc.num_sgel = scsi_dma_map(cmd);
8678c2ecf20Sopenharmony_ci	sgc.sgel_count = 0;
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci	if (unlikely(sgc.num_sgel < 0)) {
8708c2ecf20Sopenharmony_ci		esas2r_free_request(a, rq);
8718c2ecf20Sopenharmony_ci		return SCSI_MLQUEUE_HOST_BUSY;
8728c2ecf20Sopenharmony_ci	}
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci	sgc.get_phys_addr = (PGETPHYSADDR)get_physaddr_from_sgc;
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_ci	if (unlikely(!esas2r_build_sg_list(a, rq, &sgc))) {
8778c2ecf20Sopenharmony_ci		scsi_dma_unmap(cmd);
8788c2ecf20Sopenharmony_ci		esas2r_free_request(a, rq);
8798c2ecf20Sopenharmony_ci		return SCSI_MLQUEUE_HOST_BUSY;
8808c2ecf20Sopenharmony_ci	}
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	esas2r_debug("start request %p to %d:%d\n", rq, (int)cmd->device->id,
8838c2ecf20Sopenharmony_ci		     (int)cmd->device->lun);
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	esas2r_start_request(a, rq);
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_ci	return 0;
8888c2ecf20Sopenharmony_ci}
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_cistatic void complete_task_management_request(struct esas2r_adapter *a,
8918c2ecf20Sopenharmony_ci					     struct esas2r_request *rq)
8928c2ecf20Sopenharmony_ci{
8938c2ecf20Sopenharmony_ci	(*rq->task_management_status_ptr) = rq->req_stat;
8948c2ecf20Sopenharmony_ci	esas2r_free_request(a, rq);
8958c2ecf20Sopenharmony_ci}
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci/**
8988c2ecf20Sopenharmony_ci * Searches the specified queue for the specified queue for the command
8998c2ecf20Sopenharmony_ci * to abort.
9008c2ecf20Sopenharmony_ci *
9018c2ecf20Sopenharmony_ci * @param [in] a
9028c2ecf20Sopenharmony_ci * @param [in] abort_request
9038c2ecf20Sopenharmony_ci * @param [in] cmd
9048c2ecf20Sopenharmony_ci * t
9058c2ecf20Sopenharmony_ci * @return 0 on failure, 1 if command was not found, 2 if command was found
9068c2ecf20Sopenharmony_ci */
9078c2ecf20Sopenharmony_cistatic int esas2r_check_active_queue(struct esas2r_adapter *a,
9088c2ecf20Sopenharmony_ci				     struct esas2r_request **abort_request,
9098c2ecf20Sopenharmony_ci				     struct scsi_cmnd *cmd,
9108c2ecf20Sopenharmony_ci				     struct list_head *queue)
9118c2ecf20Sopenharmony_ci{
9128c2ecf20Sopenharmony_ci	bool found = false;
9138c2ecf20Sopenharmony_ci	struct esas2r_request *ar = *abort_request;
9148c2ecf20Sopenharmony_ci	struct esas2r_request *rq;
9158c2ecf20Sopenharmony_ci	struct list_head *element, *next;
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_ci	list_for_each_safe(element, next, queue) {
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci		rq = list_entry(element, struct esas2r_request, req_list);
9208c2ecf20Sopenharmony_ci
9218c2ecf20Sopenharmony_ci		if (rq->cmd == cmd) {
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci			/* Found the request.  See what to do with it. */
9248c2ecf20Sopenharmony_ci			if (queue == &a->active_list) {
9258c2ecf20Sopenharmony_ci				/*
9268c2ecf20Sopenharmony_ci				 * We are searching the active queue, which
9278c2ecf20Sopenharmony_ci				 * means that we need to send an abort request
9288c2ecf20Sopenharmony_ci				 * to the firmware.
9298c2ecf20Sopenharmony_ci				 */
9308c2ecf20Sopenharmony_ci				ar = esas2r_alloc_request(a);
9318c2ecf20Sopenharmony_ci				if (ar == NULL) {
9328c2ecf20Sopenharmony_ci					esas2r_log_dev(ESAS2R_LOG_WARN,
9338c2ecf20Sopenharmony_ci						       &(a->host->shost_gendev),
9348c2ecf20Sopenharmony_ci						       "unable to allocate an abort request for cmd %p",
9358c2ecf20Sopenharmony_ci						       cmd);
9368c2ecf20Sopenharmony_ci					return 0; /* Failure */
9378c2ecf20Sopenharmony_ci				}
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_ci				/*
9408c2ecf20Sopenharmony_ci				 * Task management request must be formatted
9418c2ecf20Sopenharmony_ci				 * with a lock held.
9428c2ecf20Sopenharmony_ci				 */
9438c2ecf20Sopenharmony_ci				ar->sense_len = 0;
9448c2ecf20Sopenharmony_ci				ar->vrq->scsi.length = 0;
9458c2ecf20Sopenharmony_ci				ar->target_id = rq->target_id;
9468c2ecf20Sopenharmony_ci				ar->vrq->scsi.flags |= cpu_to_le32(
9478c2ecf20Sopenharmony_ci					(u8)le32_to_cpu(rq->vrq->scsi.flags));
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_ci				memset(ar->vrq->scsi.cdb, 0,
9508c2ecf20Sopenharmony_ci				       sizeof(ar->vrq->scsi.cdb));
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci				ar->vrq->scsi.flags |= cpu_to_le32(
9538c2ecf20Sopenharmony_ci					FCP_CMND_TRM);
9548c2ecf20Sopenharmony_ci				ar->vrq->scsi.u.abort_handle =
9558c2ecf20Sopenharmony_ci					rq->vrq->scsi.handle;
9568c2ecf20Sopenharmony_ci			} else {
9578c2ecf20Sopenharmony_ci				/*
9588c2ecf20Sopenharmony_ci				 * The request is pending but not active on
9598c2ecf20Sopenharmony_ci				 * the firmware.  Just free it now and we'll
9608c2ecf20Sopenharmony_ci				 * report the successful abort below.
9618c2ecf20Sopenharmony_ci				 */
9628c2ecf20Sopenharmony_ci				list_del_init(&rq->req_list);
9638c2ecf20Sopenharmony_ci				esas2r_free_request(a, rq);
9648c2ecf20Sopenharmony_ci			}
9658c2ecf20Sopenharmony_ci
9668c2ecf20Sopenharmony_ci			found = true;
9678c2ecf20Sopenharmony_ci			break;
9688c2ecf20Sopenharmony_ci		}
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci	}
9718c2ecf20Sopenharmony_ci
9728c2ecf20Sopenharmony_ci	if (!found)
9738c2ecf20Sopenharmony_ci		return 1;       /* Not found */
9748c2ecf20Sopenharmony_ci
9758c2ecf20Sopenharmony_ci	return 2;               /* found */
9768c2ecf20Sopenharmony_ci
9778c2ecf20Sopenharmony_ci
9788c2ecf20Sopenharmony_ci}
9798c2ecf20Sopenharmony_ci
9808c2ecf20Sopenharmony_ciint esas2r_eh_abort(struct scsi_cmnd *cmd)
9818c2ecf20Sopenharmony_ci{
9828c2ecf20Sopenharmony_ci	struct esas2r_adapter *a =
9838c2ecf20Sopenharmony_ci		(struct esas2r_adapter *)cmd->device->host->hostdata;
9848c2ecf20Sopenharmony_ci	struct esas2r_request *abort_request = NULL;
9858c2ecf20Sopenharmony_ci	unsigned long flags;
9868c2ecf20Sopenharmony_ci	struct list_head *queue;
9878c2ecf20Sopenharmony_ci	int result;
9888c2ecf20Sopenharmony_ci
9898c2ecf20Sopenharmony_ci	esas2r_log(ESAS2R_LOG_INFO, "eh_abort (%p)", cmd);
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_ci	if (test_bit(AF_DEGRADED_MODE, &a->flags)) {
9928c2ecf20Sopenharmony_ci		cmd->result = DID_ABORT << 16;
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_ci		scsi_set_resid(cmd, 0);
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_ci		cmd->scsi_done(cmd);
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_ci		return SUCCESS;
9998c2ecf20Sopenharmony_ci	}
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci	spin_lock_irqsave(&a->queue_lock, flags);
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_ci	/*
10048c2ecf20Sopenharmony_ci	 * Run through the defer and active queues looking for the request
10058c2ecf20Sopenharmony_ci	 * to abort.
10068c2ecf20Sopenharmony_ci	 */
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci	queue = &a->defer_list;
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_cicheck_active_queue:
10118c2ecf20Sopenharmony_ci
10128c2ecf20Sopenharmony_ci	result = esas2r_check_active_queue(a, &abort_request, cmd, queue);
10138c2ecf20Sopenharmony_ci
10148c2ecf20Sopenharmony_ci	if (!result) {
10158c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&a->queue_lock, flags);
10168c2ecf20Sopenharmony_ci		return FAILED;
10178c2ecf20Sopenharmony_ci	} else if (result == 2 && (queue == &a->defer_list)) {
10188c2ecf20Sopenharmony_ci		queue = &a->active_list;
10198c2ecf20Sopenharmony_ci		goto check_active_queue;
10208c2ecf20Sopenharmony_ci	}
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&a->queue_lock, flags);
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci	if (abort_request) {
10258c2ecf20Sopenharmony_ci		u8 task_management_status = RS_PENDING;
10268c2ecf20Sopenharmony_ci
10278c2ecf20Sopenharmony_ci		/*
10288c2ecf20Sopenharmony_ci		 * the request is already active, so we need to tell
10298c2ecf20Sopenharmony_ci		 * the firmware to abort it and wait for the response.
10308c2ecf20Sopenharmony_ci		 */
10318c2ecf20Sopenharmony_ci
10328c2ecf20Sopenharmony_ci		abort_request->comp_cb = complete_task_management_request;
10338c2ecf20Sopenharmony_ci		abort_request->task_management_status_ptr =
10348c2ecf20Sopenharmony_ci			&task_management_status;
10358c2ecf20Sopenharmony_ci
10368c2ecf20Sopenharmony_ci		esas2r_start_request(a, abort_request);
10378c2ecf20Sopenharmony_ci
10388c2ecf20Sopenharmony_ci		if (atomic_read(&a->disable_cnt) == 0)
10398c2ecf20Sopenharmony_ci			esas2r_do_deferred_processes(a);
10408c2ecf20Sopenharmony_ci
10418c2ecf20Sopenharmony_ci		while (task_management_status == RS_PENDING)
10428c2ecf20Sopenharmony_ci			msleep(10);
10438c2ecf20Sopenharmony_ci
10448c2ecf20Sopenharmony_ci		/*
10458c2ecf20Sopenharmony_ci		 * Once we get here, the original request will have been
10468c2ecf20Sopenharmony_ci		 * completed by the firmware and the abort request will have
10478c2ecf20Sopenharmony_ci		 * been cleaned up.  we're done!
10488c2ecf20Sopenharmony_ci		 */
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_ci		return SUCCESS;
10518c2ecf20Sopenharmony_ci	}
10528c2ecf20Sopenharmony_ci
10538c2ecf20Sopenharmony_ci	/*
10548c2ecf20Sopenharmony_ci	 * If we get here, either we found the inactive request and
10558c2ecf20Sopenharmony_ci	 * freed it, or we didn't find it at all.  Either way, success!
10568c2ecf20Sopenharmony_ci	 */
10578c2ecf20Sopenharmony_ci
10588c2ecf20Sopenharmony_ci	cmd->result = DID_ABORT << 16;
10598c2ecf20Sopenharmony_ci
10608c2ecf20Sopenharmony_ci	scsi_set_resid(cmd, 0);
10618c2ecf20Sopenharmony_ci
10628c2ecf20Sopenharmony_ci	cmd->scsi_done(cmd);
10638c2ecf20Sopenharmony_ci
10648c2ecf20Sopenharmony_ci	return SUCCESS;
10658c2ecf20Sopenharmony_ci}
10668c2ecf20Sopenharmony_ci
10678c2ecf20Sopenharmony_cistatic int esas2r_host_bus_reset(struct scsi_cmnd *cmd, bool host_reset)
10688c2ecf20Sopenharmony_ci{
10698c2ecf20Sopenharmony_ci	struct esas2r_adapter *a =
10708c2ecf20Sopenharmony_ci		(struct esas2r_adapter *)cmd->device->host->hostdata;
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_ci	if (test_bit(AF_DEGRADED_MODE, &a->flags))
10738c2ecf20Sopenharmony_ci		return FAILED;
10748c2ecf20Sopenharmony_ci
10758c2ecf20Sopenharmony_ci	if (host_reset)
10768c2ecf20Sopenharmony_ci		esas2r_reset_adapter(a);
10778c2ecf20Sopenharmony_ci	else
10788c2ecf20Sopenharmony_ci		esas2r_reset_bus(a);
10798c2ecf20Sopenharmony_ci
10808c2ecf20Sopenharmony_ci	/* above call sets the AF_OS_RESET flag.  wait for it to clear. */
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci	while (test_bit(AF_OS_RESET, &a->flags)) {
10838c2ecf20Sopenharmony_ci		msleep(10);
10848c2ecf20Sopenharmony_ci
10858c2ecf20Sopenharmony_ci		if (test_bit(AF_DEGRADED_MODE, &a->flags))
10868c2ecf20Sopenharmony_ci			return FAILED;
10878c2ecf20Sopenharmony_ci	}
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_ci	if (test_bit(AF_DEGRADED_MODE, &a->flags))
10908c2ecf20Sopenharmony_ci		return FAILED;
10918c2ecf20Sopenharmony_ci
10928c2ecf20Sopenharmony_ci	return SUCCESS;
10938c2ecf20Sopenharmony_ci}
10948c2ecf20Sopenharmony_ci
10958c2ecf20Sopenharmony_ciint esas2r_host_reset(struct scsi_cmnd *cmd)
10968c2ecf20Sopenharmony_ci{
10978c2ecf20Sopenharmony_ci	esas2r_log(ESAS2R_LOG_INFO, "host_reset (%p)", cmd);
10988c2ecf20Sopenharmony_ci
10998c2ecf20Sopenharmony_ci	return esas2r_host_bus_reset(cmd, true);
11008c2ecf20Sopenharmony_ci}
11018c2ecf20Sopenharmony_ci
11028c2ecf20Sopenharmony_ciint esas2r_bus_reset(struct scsi_cmnd *cmd)
11038c2ecf20Sopenharmony_ci{
11048c2ecf20Sopenharmony_ci	esas2r_log(ESAS2R_LOG_INFO, "bus_reset (%p)", cmd);
11058c2ecf20Sopenharmony_ci
11068c2ecf20Sopenharmony_ci	return esas2r_host_bus_reset(cmd, false);
11078c2ecf20Sopenharmony_ci}
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_cistatic int esas2r_dev_targ_reset(struct scsi_cmnd *cmd, bool target_reset)
11108c2ecf20Sopenharmony_ci{
11118c2ecf20Sopenharmony_ci	struct esas2r_adapter *a =
11128c2ecf20Sopenharmony_ci		(struct esas2r_adapter *)cmd->device->host->hostdata;
11138c2ecf20Sopenharmony_ci	struct esas2r_request *rq;
11148c2ecf20Sopenharmony_ci	u8 task_management_status = RS_PENDING;
11158c2ecf20Sopenharmony_ci	bool completed;
11168c2ecf20Sopenharmony_ci
11178c2ecf20Sopenharmony_ci	if (test_bit(AF_DEGRADED_MODE, &a->flags))
11188c2ecf20Sopenharmony_ci		return FAILED;
11198c2ecf20Sopenharmony_ci
11208c2ecf20Sopenharmony_ciretry:
11218c2ecf20Sopenharmony_ci	rq = esas2r_alloc_request(a);
11228c2ecf20Sopenharmony_ci	if (rq == NULL) {
11238c2ecf20Sopenharmony_ci		if (target_reset) {
11248c2ecf20Sopenharmony_ci			esas2r_log(ESAS2R_LOG_CRIT,
11258c2ecf20Sopenharmony_ci				   "unable to allocate a request for a "
11268c2ecf20Sopenharmony_ci				   "target reset (%d)!",
11278c2ecf20Sopenharmony_ci				   cmd->device->id);
11288c2ecf20Sopenharmony_ci		} else {
11298c2ecf20Sopenharmony_ci			esas2r_log(ESAS2R_LOG_CRIT,
11308c2ecf20Sopenharmony_ci				   "unable to allocate a request for a "
11318c2ecf20Sopenharmony_ci				   "device reset (%d:%llu)!",
11328c2ecf20Sopenharmony_ci				   cmd->device->id,
11338c2ecf20Sopenharmony_ci				   cmd->device->lun);
11348c2ecf20Sopenharmony_ci		}
11358c2ecf20Sopenharmony_ci
11368c2ecf20Sopenharmony_ci
11378c2ecf20Sopenharmony_ci		return FAILED;
11388c2ecf20Sopenharmony_ci	}
11398c2ecf20Sopenharmony_ci
11408c2ecf20Sopenharmony_ci	rq->target_id = cmd->device->id;
11418c2ecf20Sopenharmony_ci	rq->vrq->scsi.flags |= cpu_to_le32(cmd->device->lun);
11428c2ecf20Sopenharmony_ci	rq->req_stat = RS_PENDING;
11438c2ecf20Sopenharmony_ci
11448c2ecf20Sopenharmony_ci	rq->comp_cb = complete_task_management_request;
11458c2ecf20Sopenharmony_ci	rq->task_management_status_ptr = &task_management_status;
11468c2ecf20Sopenharmony_ci
11478c2ecf20Sopenharmony_ci	if (target_reset) {
11488c2ecf20Sopenharmony_ci		esas2r_debug("issuing target reset (%p) to id %d", rq,
11498c2ecf20Sopenharmony_ci			     cmd->device->id);
11508c2ecf20Sopenharmony_ci		completed = esas2r_send_task_mgmt(a, rq, 0x20);
11518c2ecf20Sopenharmony_ci	} else {
11528c2ecf20Sopenharmony_ci		esas2r_debug("issuing device reset (%p) to id %d lun %d", rq,
11538c2ecf20Sopenharmony_ci			     cmd->device->id, cmd->device->lun);
11548c2ecf20Sopenharmony_ci		completed = esas2r_send_task_mgmt(a, rq, 0x10);
11558c2ecf20Sopenharmony_ci	}
11568c2ecf20Sopenharmony_ci
11578c2ecf20Sopenharmony_ci	if (completed) {
11588c2ecf20Sopenharmony_ci		/* Task management cmd completed right away, need to free it. */
11598c2ecf20Sopenharmony_ci
11608c2ecf20Sopenharmony_ci		esas2r_free_request(a, rq);
11618c2ecf20Sopenharmony_ci	} else {
11628c2ecf20Sopenharmony_ci		/*
11638c2ecf20Sopenharmony_ci		 * Wait for firmware to complete the request.  Completion
11648c2ecf20Sopenharmony_ci		 * callback will free it.
11658c2ecf20Sopenharmony_ci		 */
11668c2ecf20Sopenharmony_ci		while (task_management_status == RS_PENDING)
11678c2ecf20Sopenharmony_ci			msleep(10);
11688c2ecf20Sopenharmony_ci	}
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_ci	if (test_bit(AF_DEGRADED_MODE, &a->flags))
11718c2ecf20Sopenharmony_ci		return FAILED;
11728c2ecf20Sopenharmony_ci
11738c2ecf20Sopenharmony_ci	if (task_management_status == RS_BUSY) {
11748c2ecf20Sopenharmony_ci		/*
11758c2ecf20Sopenharmony_ci		 * Busy, probably because we are flashing.  Wait a bit and
11768c2ecf20Sopenharmony_ci		 * try again.
11778c2ecf20Sopenharmony_ci		 */
11788c2ecf20Sopenharmony_ci		msleep(100);
11798c2ecf20Sopenharmony_ci		goto retry;
11808c2ecf20Sopenharmony_ci	}
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_ci	return SUCCESS;
11838c2ecf20Sopenharmony_ci}
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_ciint esas2r_device_reset(struct scsi_cmnd *cmd)
11868c2ecf20Sopenharmony_ci{
11878c2ecf20Sopenharmony_ci	esas2r_log(ESAS2R_LOG_INFO, "device_reset (%p)", cmd);
11888c2ecf20Sopenharmony_ci
11898c2ecf20Sopenharmony_ci	return esas2r_dev_targ_reset(cmd, false);
11908c2ecf20Sopenharmony_ci
11918c2ecf20Sopenharmony_ci}
11928c2ecf20Sopenharmony_ci
11938c2ecf20Sopenharmony_ciint esas2r_target_reset(struct scsi_cmnd *cmd)
11948c2ecf20Sopenharmony_ci{
11958c2ecf20Sopenharmony_ci	esas2r_log(ESAS2R_LOG_INFO, "target_reset (%p)", cmd);
11968c2ecf20Sopenharmony_ci
11978c2ecf20Sopenharmony_ci	return esas2r_dev_targ_reset(cmd, true);
11988c2ecf20Sopenharmony_ci}
11998c2ecf20Sopenharmony_ci
12008c2ecf20Sopenharmony_civoid esas2r_log_request_failure(struct esas2r_adapter *a,
12018c2ecf20Sopenharmony_ci				struct esas2r_request *rq)
12028c2ecf20Sopenharmony_ci{
12038c2ecf20Sopenharmony_ci	u8 reqstatus = rq->req_stat;
12048c2ecf20Sopenharmony_ci
12058c2ecf20Sopenharmony_ci	if (reqstatus == RS_SUCCESS)
12068c2ecf20Sopenharmony_ci		return;
12078c2ecf20Sopenharmony_ci
12088c2ecf20Sopenharmony_ci	if (rq->vrq->scsi.function == VDA_FUNC_SCSI) {
12098c2ecf20Sopenharmony_ci		if (reqstatus == RS_SCSI_ERROR) {
12108c2ecf20Sopenharmony_ci			if (rq->func_rsp.scsi_rsp.sense_len >= 13) {
12118c2ecf20Sopenharmony_ci				esas2r_log(ESAS2R_LOG_WARN,
12128c2ecf20Sopenharmony_ci					   "request failure - SCSI error %x ASC:%x ASCQ:%x CDB:%x",
12138c2ecf20Sopenharmony_ci					   rq->sense_buf[2], rq->sense_buf[12],
12148c2ecf20Sopenharmony_ci					   rq->sense_buf[13],
12158c2ecf20Sopenharmony_ci					   rq->vrq->scsi.cdb[0]);
12168c2ecf20Sopenharmony_ci			} else {
12178c2ecf20Sopenharmony_ci				esas2r_log(ESAS2R_LOG_WARN,
12188c2ecf20Sopenharmony_ci					   "request failure - SCSI error CDB:%x\n",
12198c2ecf20Sopenharmony_ci					   rq->vrq->scsi.cdb[0]);
12208c2ecf20Sopenharmony_ci			}
12218c2ecf20Sopenharmony_ci		} else if ((rq->vrq->scsi.cdb[0] != INQUIRY
12228c2ecf20Sopenharmony_ci			    && rq->vrq->scsi.cdb[0] != REPORT_LUNS)
12238c2ecf20Sopenharmony_ci			   || (reqstatus != RS_SEL
12248c2ecf20Sopenharmony_ci			       && reqstatus != RS_SEL2)) {
12258c2ecf20Sopenharmony_ci			if ((reqstatus == RS_UNDERRUN) &&
12268c2ecf20Sopenharmony_ci			    (rq->vrq->scsi.cdb[0] == INQUIRY)) {
12278c2ecf20Sopenharmony_ci				/* Don't log inquiry underruns */
12288c2ecf20Sopenharmony_ci			} else {
12298c2ecf20Sopenharmony_ci				esas2r_log(ESAS2R_LOG_WARN,
12308c2ecf20Sopenharmony_ci					   "request failure - cdb:%x reqstatus:%d target:%d",
12318c2ecf20Sopenharmony_ci					   rq->vrq->scsi.cdb[0], reqstatus,
12328c2ecf20Sopenharmony_ci					   rq->target_id);
12338c2ecf20Sopenharmony_ci			}
12348c2ecf20Sopenharmony_ci		}
12358c2ecf20Sopenharmony_ci	}
12368c2ecf20Sopenharmony_ci}
12378c2ecf20Sopenharmony_ci
12388c2ecf20Sopenharmony_civoid esas2r_wait_request(struct esas2r_adapter *a, struct esas2r_request *rq)
12398c2ecf20Sopenharmony_ci{
12408c2ecf20Sopenharmony_ci	u32 starttime;
12418c2ecf20Sopenharmony_ci	u32 timeout;
12428c2ecf20Sopenharmony_ci
12438c2ecf20Sopenharmony_ci	starttime = jiffies_to_msecs(jiffies);
12448c2ecf20Sopenharmony_ci	timeout = rq->timeout ? rq->timeout : 5000;
12458c2ecf20Sopenharmony_ci
12468c2ecf20Sopenharmony_ci	while (true) {
12478c2ecf20Sopenharmony_ci		esas2r_polled_interrupt(a);
12488c2ecf20Sopenharmony_ci
12498c2ecf20Sopenharmony_ci		if (rq->req_stat != RS_STARTED)
12508c2ecf20Sopenharmony_ci			break;
12518c2ecf20Sopenharmony_ci
12528c2ecf20Sopenharmony_ci		schedule_timeout_interruptible(msecs_to_jiffies(100));
12538c2ecf20Sopenharmony_ci
12548c2ecf20Sopenharmony_ci		if ((jiffies_to_msecs(jiffies) - starttime) > timeout) {
12558c2ecf20Sopenharmony_ci			esas2r_hdebug("request TMO");
12568c2ecf20Sopenharmony_ci			esas2r_bugon();
12578c2ecf20Sopenharmony_ci
12588c2ecf20Sopenharmony_ci			rq->req_stat = RS_TIMEOUT;
12598c2ecf20Sopenharmony_ci
12608c2ecf20Sopenharmony_ci			esas2r_local_reset_adapter(a);
12618c2ecf20Sopenharmony_ci			return;
12628c2ecf20Sopenharmony_ci		}
12638c2ecf20Sopenharmony_ci	}
12648c2ecf20Sopenharmony_ci}
12658c2ecf20Sopenharmony_ci
12668c2ecf20Sopenharmony_ciu32 esas2r_map_data_window(struct esas2r_adapter *a, u32 addr_lo)
12678c2ecf20Sopenharmony_ci{
12688c2ecf20Sopenharmony_ci	u32 offset = addr_lo & (MW_DATA_WINDOW_SIZE - 1);
12698c2ecf20Sopenharmony_ci	u32 base = addr_lo & -(signed int)MW_DATA_WINDOW_SIZE;
12708c2ecf20Sopenharmony_ci
12718c2ecf20Sopenharmony_ci	if (a->window_base != base) {
12728c2ecf20Sopenharmony_ci		esas2r_write_register_dword(a, MVR_PCI_WIN1_REMAP,
12738c2ecf20Sopenharmony_ci					    base | MVRPW1R_ENABLE);
12748c2ecf20Sopenharmony_ci		esas2r_flush_register_dword(a, MVR_PCI_WIN1_REMAP);
12758c2ecf20Sopenharmony_ci		a->window_base = base;
12768c2ecf20Sopenharmony_ci	}
12778c2ecf20Sopenharmony_ci
12788c2ecf20Sopenharmony_ci	return offset;
12798c2ecf20Sopenharmony_ci}
12808c2ecf20Sopenharmony_ci
12818c2ecf20Sopenharmony_ci/* Read a block of data from chip memory */
12828c2ecf20Sopenharmony_cibool esas2r_read_mem_block(struct esas2r_adapter *a,
12838c2ecf20Sopenharmony_ci			   void *to,
12848c2ecf20Sopenharmony_ci			   u32 from,
12858c2ecf20Sopenharmony_ci			   u32 size)
12868c2ecf20Sopenharmony_ci{
12878c2ecf20Sopenharmony_ci	u8 *end = (u8 *)to;
12888c2ecf20Sopenharmony_ci
12898c2ecf20Sopenharmony_ci	while (size) {
12908c2ecf20Sopenharmony_ci		u32 len;
12918c2ecf20Sopenharmony_ci		u32 offset;
12928c2ecf20Sopenharmony_ci		u32 iatvr;
12938c2ecf20Sopenharmony_ci
12948c2ecf20Sopenharmony_ci		iatvr = (from & -(signed int)MW_DATA_WINDOW_SIZE);
12958c2ecf20Sopenharmony_ci
12968c2ecf20Sopenharmony_ci		esas2r_map_data_window(a, iatvr);
12978c2ecf20Sopenharmony_ci
12988c2ecf20Sopenharmony_ci		offset = from & (MW_DATA_WINDOW_SIZE - 1);
12998c2ecf20Sopenharmony_ci		len = size;
13008c2ecf20Sopenharmony_ci
13018c2ecf20Sopenharmony_ci		if (len > MW_DATA_WINDOW_SIZE - offset)
13028c2ecf20Sopenharmony_ci			len = MW_DATA_WINDOW_SIZE - offset;
13038c2ecf20Sopenharmony_ci
13048c2ecf20Sopenharmony_ci		from += len;
13058c2ecf20Sopenharmony_ci		size -= len;
13068c2ecf20Sopenharmony_ci
13078c2ecf20Sopenharmony_ci		while (len--) {
13088c2ecf20Sopenharmony_ci			*end++ = esas2r_read_data_byte(a, offset);
13098c2ecf20Sopenharmony_ci			offset++;
13108c2ecf20Sopenharmony_ci		}
13118c2ecf20Sopenharmony_ci	}
13128c2ecf20Sopenharmony_ci
13138c2ecf20Sopenharmony_ci	return true;
13148c2ecf20Sopenharmony_ci}
13158c2ecf20Sopenharmony_ci
13168c2ecf20Sopenharmony_civoid esas2r_nuxi_mgt_data(u8 function, void *data)
13178c2ecf20Sopenharmony_ci{
13188c2ecf20Sopenharmony_ci	struct atto_vda_grp_info *g;
13198c2ecf20Sopenharmony_ci	struct atto_vda_devinfo *d;
13208c2ecf20Sopenharmony_ci	struct atto_vdapart_info *p;
13218c2ecf20Sopenharmony_ci	struct atto_vda_dh_info *h;
13228c2ecf20Sopenharmony_ci	struct atto_vda_metrics_info *m;
13238c2ecf20Sopenharmony_ci	struct atto_vda_schedule_info *s;
13248c2ecf20Sopenharmony_ci	struct atto_vda_buzzer_info *b;
13258c2ecf20Sopenharmony_ci	u8 i;
13268c2ecf20Sopenharmony_ci
13278c2ecf20Sopenharmony_ci	switch (function) {
13288c2ecf20Sopenharmony_ci	case VDAMGT_BUZZER_INFO:
13298c2ecf20Sopenharmony_ci	case VDAMGT_BUZZER_SET:
13308c2ecf20Sopenharmony_ci
13318c2ecf20Sopenharmony_ci		b = (struct atto_vda_buzzer_info *)data;
13328c2ecf20Sopenharmony_ci
13338c2ecf20Sopenharmony_ci		b->duration = le32_to_cpu(b->duration);
13348c2ecf20Sopenharmony_ci		break;
13358c2ecf20Sopenharmony_ci
13368c2ecf20Sopenharmony_ci	case VDAMGT_SCHEDULE_INFO:
13378c2ecf20Sopenharmony_ci	case VDAMGT_SCHEDULE_EVENT:
13388c2ecf20Sopenharmony_ci
13398c2ecf20Sopenharmony_ci		s = (struct atto_vda_schedule_info *)data;
13408c2ecf20Sopenharmony_ci
13418c2ecf20Sopenharmony_ci		s->id = le32_to_cpu(s->id);
13428c2ecf20Sopenharmony_ci
13438c2ecf20Sopenharmony_ci		break;
13448c2ecf20Sopenharmony_ci
13458c2ecf20Sopenharmony_ci	case VDAMGT_DEV_INFO:
13468c2ecf20Sopenharmony_ci	case VDAMGT_DEV_CLEAN:
13478c2ecf20Sopenharmony_ci	case VDAMGT_DEV_PT_INFO:
13488c2ecf20Sopenharmony_ci	case VDAMGT_DEV_FEATURES:
13498c2ecf20Sopenharmony_ci	case VDAMGT_DEV_PT_FEATURES:
13508c2ecf20Sopenharmony_ci	case VDAMGT_DEV_OPERATION:
13518c2ecf20Sopenharmony_ci
13528c2ecf20Sopenharmony_ci		d = (struct atto_vda_devinfo *)data;
13538c2ecf20Sopenharmony_ci
13548c2ecf20Sopenharmony_ci		d->capacity = le64_to_cpu(d->capacity);
13558c2ecf20Sopenharmony_ci		d->block_size = le32_to_cpu(d->block_size);
13568c2ecf20Sopenharmony_ci		d->ses_dev_index = le16_to_cpu(d->ses_dev_index);
13578c2ecf20Sopenharmony_ci		d->target_id = le16_to_cpu(d->target_id);
13588c2ecf20Sopenharmony_ci		d->lun = le16_to_cpu(d->lun);
13598c2ecf20Sopenharmony_ci		d->features = le16_to_cpu(d->features);
13608c2ecf20Sopenharmony_ci		break;
13618c2ecf20Sopenharmony_ci
13628c2ecf20Sopenharmony_ci	case VDAMGT_GRP_INFO:
13638c2ecf20Sopenharmony_ci	case VDAMGT_GRP_CREATE:
13648c2ecf20Sopenharmony_ci	case VDAMGT_GRP_DELETE:
13658c2ecf20Sopenharmony_ci	case VDAMGT_ADD_STORAGE:
13668c2ecf20Sopenharmony_ci	case VDAMGT_MEMBER_ADD:
13678c2ecf20Sopenharmony_ci	case VDAMGT_GRP_COMMIT:
13688c2ecf20Sopenharmony_ci	case VDAMGT_GRP_REBUILD:
13698c2ecf20Sopenharmony_ci	case VDAMGT_GRP_COMMIT_INIT:
13708c2ecf20Sopenharmony_ci	case VDAMGT_QUICK_RAID:
13718c2ecf20Sopenharmony_ci	case VDAMGT_GRP_FEATURES:
13728c2ecf20Sopenharmony_ci	case VDAMGT_GRP_COMMIT_INIT_AUTOMAP:
13738c2ecf20Sopenharmony_ci	case VDAMGT_QUICK_RAID_INIT_AUTOMAP:
13748c2ecf20Sopenharmony_ci	case VDAMGT_SPARE_LIST:
13758c2ecf20Sopenharmony_ci	case VDAMGT_SPARE_ADD:
13768c2ecf20Sopenharmony_ci	case VDAMGT_SPARE_REMOVE:
13778c2ecf20Sopenharmony_ci	case VDAMGT_LOCAL_SPARE_ADD:
13788c2ecf20Sopenharmony_ci	case VDAMGT_GRP_OPERATION:
13798c2ecf20Sopenharmony_ci
13808c2ecf20Sopenharmony_ci		g = (struct atto_vda_grp_info *)data;
13818c2ecf20Sopenharmony_ci
13828c2ecf20Sopenharmony_ci		g->capacity = le64_to_cpu(g->capacity);
13838c2ecf20Sopenharmony_ci		g->block_size = le32_to_cpu(g->block_size);
13848c2ecf20Sopenharmony_ci		g->interleave = le32_to_cpu(g->interleave);
13858c2ecf20Sopenharmony_ci		g->features = le16_to_cpu(g->features);
13868c2ecf20Sopenharmony_ci
13878c2ecf20Sopenharmony_ci		for (i = 0; i < 32; i++)
13888c2ecf20Sopenharmony_ci			g->members[i] = le16_to_cpu(g->members[i]);
13898c2ecf20Sopenharmony_ci
13908c2ecf20Sopenharmony_ci		break;
13918c2ecf20Sopenharmony_ci
13928c2ecf20Sopenharmony_ci	case VDAMGT_PART_INFO:
13938c2ecf20Sopenharmony_ci	case VDAMGT_PART_MAP:
13948c2ecf20Sopenharmony_ci	case VDAMGT_PART_UNMAP:
13958c2ecf20Sopenharmony_ci	case VDAMGT_PART_AUTOMAP:
13968c2ecf20Sopenharmony_ci	case VDAMGT_PART_SPLIT:
13978c2ecf20Sopenharmony_ci	case VDAMGT_PART_MERGE:
13988c2ecf20Sopenharmony_ci
13998c2ecf20Sopenharmony_ci		p = (struct atto_vdapart_info *)data;
14008c2ecf20Sopenharmony_ci
14018c2ecf20Sopenharmony_ci		p->part_size = le64_to_cpu(p->part_size);
14028c2ecf20Sopenharmony_ci		p->start_lba = le32_to_cpu(p->start_lba);
14038c2ecf20Sopenharmony_ci		p->block_size = le32_to_cpu(p->block_size);
14048c2ecf20Sopenharmony_ci		p->target_id = le16_to_cpu(p->target_id);
14058c2ecf20Sopenharmony_ci		break;
14068c2ecf20Sopenharmony_ci
14078c2ecf20Sopenharmony_ci	case VDAMGT_DEV_HEALTH_REQ:
14088c2ecf20Sopenharmony_ci
14098c2ecf20Sopenharmony_ci		h = (struct atto_vda_dh_info *)data;
14108c2ecf20Sopenharmony_ci
14118c2ecf20Sopenharmony_ci		h->med_defect_cnt = le32_to_cpu(h->med_defect_cnt);
14128c2ecf20Sopenharmony_ci		h->info_exc_cnt = le32_to_cpu(h->info_exc_cnt);
14138c2ecf20Sopenharmony_ci		break;
14148c2ecf20Sopenharmony_ci
14158c2ecf20Sopenharmony_ci	case VDAMGT_DEV_METRICS:
14168c2ecf20Sopenharmony_ci
14178c2ecf20Sopenharmony_ci		m = (struct atto_vda_metrics_info *)data;
14188c2ecf20Sopenharmony_ci
14198c2ecf20Sopenharmony_ci		for (i = 0; i < 32; i++)
14208c2ecf20Sopenharmony_ci			m->dev_indexes[i] = le16_to_cpu(m->dev_indexes[i]);
14218c2ecf20Sopenharmony_ci
14228c2ecf20Sopenharmony_ci		break;
14238c2ecf20Sopenharmony_ci
14248c2ecf20Sopenharmony_ci	default:
14258c2ecf20Sopenharmony_ci		break;
14268c2ecf20Sopenharmony_ci	}
14278c2ecf20Sopenharmony_ci}
14288c2ecf20Sopenharmony_ci
14298c2ecf20Sopenharmony_civoid esas2r_nuxi_cfg_data(u8 function, void *data)
14308c2ecf20Sopenharmony_ci{
14318c2ecf20Sopenharmony_ci	struct atto_vda_cfg_init *ci;
14328c2ecf20Sopenharmony_ci
14338c2ecf20Sopenharmony_ci	switch (function) {
14348c2ecf20Sopenharmony_ci	case VDA_CFG_INIT:
14358c2ecf20Sopenharmony_ci	case VDA_CFG_GET_INIT:
14368c2ecf20Sopenharmony_ci	case VDA_CFG_GET_INIT2:
14378c2ecf20Sopenharmony_ci
14388c2ecf20Sopenharmony_ci		ci = (struct atto_vda_cfg_init *)data;
14398c2ecf20Sopenharmony_ci
14408c2ecf20Sopenharmony_ci		ci->date_time.year = le16_to_cpu(ci->date_time.year);
14418c2ecf20Sopenharmony_ci		ci->sgl_page_size = le32_to_cpu(ci->sgl_page_size);
14428c2ecf20Sopenharmony_ci		ci->vda_version = le32_to_cpu(ci->vda_version);
14438c2ecf20Sopenharmony_ci		ci->epoch_time = le32_to_cpu(ci->epoch_time);
14448c2ecf20Sopenharmony_ci		ci->ioctl_tunnel = le32_to_cpu(ci->ioctl_tunnel);
14458c2ecf20Sopenharmony_ci		ci->num_targets_backend = le32_to_cpu(ci->num_targets_backend);
14468c2ecf20Sopenharmony_ci		break;
14478c2ecf20Sopenharmony_ci
14488c2ecf20Sopenharmony_ci	default:
14498c2ecf20Sopenharmony_ci		break;
14508c2ecf20Sopenharmony_ci	}
14518c2ecf20Sopenharmony_ci}
14528c2ecf20Sopenharmony_ci
14538c2ecf20Sopenharmony_civoid esas2r_nuxi_ae_data(union atto_vda_ae *ae)
14548c2ecf20Sopenharmony_ci{
14558c2ecf20Sopenharmony_ci	struct atto_vda_ae_raid *r = &ae->raid;
14568c2ecf20Sopenharmony_ci	struct atto_vda_ae_lu *l = &ae->lu;
14578c2ecf20Sopenharmony_ci
14588c2ecf20Sopenharmony_ci	switch (ae->hdr.bytype) {
14598c2ecf20Sopenharmony_ci	case VDAAE_HDR_TYPE_RAID:
14608c2ecf20Sopenharmony_ci
14618c2ecf20Sopenharmony_ci		r->dwflags = le32_to_cpu(r->dwflags);
14628c2ecf20Sopenharmony_ci		break;
14638c2ecf20Sopenharmony_ci
14648c2ecf20Sopenharmony_ci	case VDAAE_HDR_TYPE_LU:
14658c2ecf20Sopenharmony_ci
14668c2ecf20Sopenharmony_ci		l->dwevent = le32_to_cpu(l->dwevent);
14678c2ecf20Sopenharmony_ci		l->wphys_target_id = le16_to_cpu(l->wphys_target_id);
14688c2ecf20Sopenharmony_ci		l->id.tgtlun.wtarget_id = le16_to_cpu(l->id.tgtlun.wtarget_id);
14698c2ecf20Sopenharmony_ci
14708c2ecf20Sopenharmony_ci		if (l->hdr.bylength >= offsetof(struct atto_vda_ae_lu, id)
14718c2ecf20Sopenharmony_ci		    + sizeof(struct atto_vda_ae_lu_tgt_lun_raid)) {
14728c2ecf20Sopenharmony_ci			l->id.tgtlun_raid.dwinterleave
14738c2ecf20Sopenharmony_ci				= le32_to_cpu(l->id.tgtlun_raid.dwinterleave);
14748c2ecf20Sopenharmony_ci			l->id.tgtlun_raid.dwblock_size
14758c2ecf20Sopenharmony_ci				= le32_to_cpu(l->id.tgtlun_raid.dwblock_size);
14768c2ecf20Sopenharmony_ci		}
14778c2ecf20Sopenharmony_ci
14788c2ecf20Sopenharmony_ci		break;
14798c2ecf20Sopenharmony_ci
14808c2ecf20Sopenharmony_ci	case VDAAE_HDR_TYPE_DISK:
14818c2ecf20Sopenharmony_ci	default:
14828c2ecf20Sopenharmony_ci		break;
14838c2ecf20Sopenharmony_ci	}
14848c2ecf20Sopenharmony_ci}
14858c2ecf20Sopenharmony_ci
14868c2ecf20Sopenharmony_civoid esas2r_free_request(struct esas2r_adapter *a, struct esas2r_request *rq)
14878c2ecf20Sopenharmony_ci{
14888c2ecf20Sopenharmony_ci	unsigned long flags;
14898c2ecf20Sopenharmony_ci
14908c2ecf20Sopenharmony_ci	esas2r_rq_destroy_request(rq, a);
14918c2ecf20Sopenharmony_ci	spin_lock_irqsave(&a->request_lock, flags);
14928c2ecf20Sopenharmony_ci	list_add(&rq->comp_list, &a->avail_request);
14938c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&a->request_lock, flags);
14948c2ecf20Sopenharmony_ci}
14958c2ecf20Sopenharmony_ci
14968c2ecf20Sopenharmony_cistruct esas2r_request *esas2r_alloc_request(struct esas2r_adapter *a)
14978c2ecf20Sopenharmony_ci{
14988c2ecf20Sopenharmony_ci	struct esas2r_request *rq;
14998c2ecf20Sopenharmony_ci	unsigned long flags;
15008c2ecf20Sopenharmony_ci
15018c2ecf20Sopenharmony_ci	spin_lock_irqsave(&a->request_lock, flags);
15028c2ecf20Sopenharmony_ci
15038c2ecf20Sopenharmony_ci	if (unlikely(list_empty(&a->avail_request))) {
15048c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&a->request_lock, flags);
15058c2ecf20Sopenharmony_ci		return NULL;
15068c2ecf20Sopenharmony_ci	}
15078c2ecf20Sopenharmony_ci
15088c2ecf20Sopenharmony_ci	rq = list_first_entry(&a->avail_request, struct esas2r_request,
15098c2ecf20Sopenharmony_ci			      comp_list);
15108c2ecf20Sopenharmony_ci	list_del(&rq->comp_list);
15118c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&a->request_lock, flags);
15128c2ecf20Sopenharmony_ci	esas2r_rq_init_request(rq, a);
15138c2ecf20Sopenharmony_ci
15148c2ecf20Sopenharmony_ci	return rq;
15158c2ecf20Sopenharmony_ci
15168c2ecf20Sopenharmony_ci}
15178c2ecf20Sopenharmony_ci
15188c2ecf20Sopenharmony_civoid esas2r_complete_request_cb(struct esas2r_adapter *a,
15198c2ecf20Sopenharmony_ci				struct esas2r_request *rq)
15208c2ecf20Sopenharmony_ci{
15218c2ecf20Sopenharmony_ci	esas2r_debug("completing request %p\n", rq);
15228c2ecf20Sopenharmony_ci
15238c2ecf20Sopenharmony_ci	scsi_dma_unmap(rq->cmd);
15248c2ecf20Sopenharmony_ci
15258c2ecf20Sopenharmony_ci	if (unlikely(rq->req_stat != RS_SUCCESS)) {
15268c2ecf20Sopenharmony_ci		esas2r_debug("[%x STATUS %x:%x (%x)]", rq->target_id,
15278c2ecf20Sopenharmony_ci			     rq->req_stat,
15288c2ecf20Sopenharmony_ci			     rq->func_rsp.scsi_rsp.scsi_stat,
15298c2ecf20Sopenharmony_ci			     rq->cmd);
15308c2ecf20Sopenharmony_ci
15318c2ecf20Sopenharmony_ci		rq->cmd->result =
15328c2ecf20Sopenharmony_ci			((esas2r_req_status_to_error(rq->req_stat) << 16)
15338c2ecf20Sopenharmony_ci			 | (rq->func_rsp.scsi_rsp.scsi_stat & STATUS_MASK));
15348c2ecf20Sopenharmony_ci
15358c2ecf20Sopenharmony_ci		if (rq->req_stat == RS_UNDERRUN)
15368c2ecf20Sopenharmony_ci			scsi_set_resid(rq->cmd,
15378c2ecf20Sopenharmony_ci				       le32_to_cpu(rq->func_rsp.scsi_rsp.
15388c2ecf20Sopenharmony_ci						   residual_length));
15398c2ecf20Sopenharmony_ci		else
15408c2ecf20Sopenharmony_ci			scsi_set_resid(rq->cmd, 0);
15418c2ecf20Sopenharmony_ci	}
15428c2ecf20Sopenharmony_ci
15438c2ecf20Sopenharmony_ci	rq->cmd->scsi_done(rq->cmd);
15448c2ecf20Sopenharmony_ci
15458c2ecf20Sopenharmony_ci	esas2r_free_request(a, rq);
15468c2ecf20Sopenharmony_ci}
15478c2ecf20Sopenharmony_ci
15488c2ecf20Sopenharmony_ci/* Run tasklet to handle stuff outside of interrupt context. */
15498c2ecf20Sopenharmony_civoid esas2r_adapter_tasklet(unsigned long context)
15508c2ecf20Sopenharmony_ci{
15518c2ecf20Sopenharmony_ci	struct esas2r_adapter *a = (struct esas2r_adapter *)context;
15528c2ecf20Sopenharmony_ci
15538c2ecf20Sopenharmony_ci	if (unlikely(test_bit(AF2_TIMER_TICK, &a->flags2))) {
15548c2ecf20Sopenharmony_ci		clear_bit(AF2_TIMER_TICK, &a->flags2);
15558c2ecf20Sopenharmony_ci		esas2r_timer_tick(a);
15568c2ecf20Sopenharmony_ci	}
15578c2ecf20Sopenharmony_ci
15588c2ecf20Sopenharmony_ci	if (likely(test_bit(AF2_INT_PENDING, &a->flags2))) {
15598c2ecf20Sopenharmony_ci		clear_bit(AF2_INT_PENDING, &a->flags2);
15608c2ecf20Sopenharmony_ci		esas2r_adapter_interrupt(a);
15618c2ecf20Sopenharmony_ci	}
15628c2ecf20Sopenharmony_ci
15638c2ecf20Sopenharmony_ci	if (esas2r_is_tasklet_pending(a))
15648c2ecf20Sopenharmony_ci		esas2r_do_tasklet_tasks(a);
15658c2ecf20Sopenharmony_ci
15668c2ecf20Sopenharmony_ci	if (esas2r_is_tasklet_pending(a)
15678c2ecf20Sopenharmony_ci	    || (test_bit(AF2_INT_PENDING, &a->flags2))
15688c2ecf20Sopenharmony_ci	    || (test_bit(AF2_TIMER_TICK, &a->flags2))) {
15698c2ecf20Sopenharmony_ci		clear_bit(AF_TASKLET_SCHEDULED, &a->flags);
15708c2ecf20Sopenharmony_ci		esas2r_schedule_tasklet(a);
15718c2ecf20Sopenharmony_ci	} else {
15728c2ecf20Sopenharmony_ci		clear_bit(AF_TASKLET_SCHEDULED, &a->flags);
15738c2ecf20Sopenharmony_ci	}
15748c2ecf20Sopenharmony_ci}
15758c2ecf20Sopenharmony_ci
15768c2ecf20Sopenharmony_cistatic void esas2r_timer_callback(struct timer_list *t);
15778c2ecf20Sopenharmony_ci
15788c2ecf20Sopenharmony_civoid esas2r_kickoff_timer(struct esas2r_adapter *a)
15798c2ecf20Sopenharmony_ci{
15808c2ecf20Sopenharmony_ci	timer_setup(&a->timer, esas2r_timer_callback, 0);
15818c2ecf20Sopenharmony_ci
15828c2ecf20Sopenharmony_ci	a->timer.expires = jiffies +
15838c2ecf20Sopenharmony_ci			   msecs_to_jiffies(100);
15848c2ecf20Sopenharmony_ci
15858c2ecf20Sopenharmony_ci	add_timer(&a->timer);
15868c2ecf20Sopenharmony_ci}
15878c2ecf20Sopenharmony_ci
15888c2ecf20Sopenharmony_cistatic void esas2r_timer_callback(struct timer_list *t)
15898c2ecf20Sopenharmony_ci{
15908c2ecf20Sopenharmony_ci	struct esas2r_adapter *a = from_timer(a, t, timer);
15918c2ecf20Sopenharmony_ci
15928c2ecf20Sopenharmony_ci	set_bit(AF2_TIMER_TICK, &a->flags2);
15938c2ecf20Sopenharmony_ci
15948c2ecf20Sopenharmony_ci	esas2r_schedule_tasklet(a);
15958c2ecf20Sopenharmony_ci
15968c2ecf20Sopenharmony_ci	esas2r_kickoff_timer(a);
15978c2ecf20Sopenharmony_ci}
15988c2ecf20Sopenharmony_ci
15998c2ecf20Sopenharmony_ci/*
16008c2ecf20Sopenharmony_ci * Firmware events need to be handled outside of interrupt context
16018c2ecf20Sopenharmony_ci * so we schedule a delayed_work to handle them.
16028c2ecf20Sopenharmony_ci */
16038c2ecf20Sopenharmony_ci
16048c2ecf20Sopenharmony_cistatic void
16058c2ecf20Sopenharmony_ciesas2r_free_fw_event(struct esas2r_fw_event_work *fw_event)
16068c2ecf20Sopenharmony_ci{
16078c2ecf20Sopenharmony_ci	unsigned long flags;
16088c2ecf20Sopenharmony_ci	struct esas2r_adapter *a = fw_event->a;
16098c2ecf20Sopenharmony_ci
16108c2ecf20Sopenharmony_ci	spin_lock_irqsave(&a->fw_event_lock, flags);
16118c2ecf20Sopenharmony_ci	list_del(&fw_event->list);
16128c2ecf20Sopenharmony_ci	kfree(fw_event);
16138c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&a->fw_event_lock, flags);
16148c2ecf20Sopenharmony_ci}
16158c2ecf20Sopenharmony_ci
16168c2ecf20Sopenharmony_civoid
16178c2ecf20Sopenharmony_ciesas2r_fw_event_off(struct esas2r_adapter *a)
16188c2ecf20Sopenharmony_ci{
16198c2ecf20Sopenharmony_ci	unsigned long flags;
16208c2ecf20Sopenharmony_ci
16218c2ecf20Sopenharmony_ci	spin_lock_irqsave(&a->fw_event_lock, flags);
16228c2ecf20Sopenharmony_ci	a->fw_events_off = 1;
16238c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&a->fw_event_lock, flags);
16248c2ecf20Sopenharmony_ci}
16258c2ecf20Sopenharmony_ci
16268c2ecf20Sopenharmony_civoid
16278c2ecf20Sopenharmony_ciesas2r_fw_event_on(struct esas2r_adapter *a)
16288c2ecf20Sopenharmony_ci{
16298c2ecf20Sopenharmony_ci	unsigned long flags;
16308c2ecf20Sopenharmony_ci
16318c2ecf20Sopenharmony_ci	spin_lock_irqsave(&a->fw_event_lock, flags);
16328c2ecf20Sopenharmony_ci	a->fw_events_off = 0;
16338c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&a->fw_event_lock, flags);
16348c2ecf20Sopenharmony_ci}
16358c2ecf20Sopenharmony_ci
16368c2ecf20Sopenharmony_cistatic void esas2r_add_device(struct esas2r_adapter *a, u16 target_id)
16378c2ecf20Sopenharmony_ci{
16388c2ecf20Sopenharmony_ci	int ret;
16398c2ecf20Sopenharmony_ci	struct scsi_device *scsi_dev;
16408c2ecf20Sopenharmony_ci
16418c2ecf20Sopenharmony_ci	scsi_dev = scsi_device_lookup(a->host, 0, target_id, 0);
16428c2ecf20Sopenharmony_ci
16438c2ecf20Sopenharmony_ci	if (scsi_dev) {
16448c2ecf20Sopenharmony_ci		esas2r_log_dev(
16458c2ecf20Sopenharmony_ci			ESAS2R_LOG_WARN,
16468c2ecf20Sopenharmony_ci			&(scsi_dev->
16478c2ecf20Sopenharmony_ci			  sdev_gendev),
16488c2ecf20Sopenharmony_ci			"scsi device already exists at id %d", target_id);
16498c2ecf20Sopenharmony_ci
16508c2ecf20Sopenharmony_ci		scsi_device_put(scsi_dev);
16518c2ecf20Sopenharmony_ci	} else {
16528c2ecf20Sopenharmony_ci		esas2r_log_dev(
16538c2ecf20Sopenharmony_ci			ESAS2R_LOG_INFO,
16548c2ecf20Sopenharmony_ci			&(a->host->
16558c2ecf20Sopenharmony_ci			  shost_gendev),
16568c2ecf20Sopenharmony_ci			"scsi_add_device() called for 0:%d:0",
16578c2ecf20Sopenharmony_ci			target_id);
16588c2ecf20Sopenharmony_ci
16598c2ecf20Sopenharmony_ci		ret = scsi_add_device(a->host, 0, target_id, 0);
16608c2ecf20Sopenharmony_ci		if (ret) {
16618c2ecf20Sopenharmony_ci			esas2r_log_dev(
16628c2ecf20Sopenharmony_ci				ESAS2R_LOG_CRIT,
16638c2ecf20Sopenharmony_ci				&(a->host->
16648c2ecf20Sopenharmony_ci				  shost_gendev),
16658c2ecf20Sopenharmony_ci				"scsi_add_device failed with %d for id %d",
16668c2ecf20Sopenharmony_ci				ret, target_id);
16678c2ecf20Sopenharmony_ci		}
16688c2ecf20Sopenharmony_ci	}
16698c2ecf20Sopenharmony_ci}
16708c2ecf20Sopenharmony_ci
16718c2ecf20Sopenharmony_cistatic void esas2r_remove_device(struct esas2r_adapter *a, u16 target_id)
16728c2ecf20Sopenharmony_ci{
16738c2ecf20Sopenharmony_ci	struct scsi_device *scsi_dev;
16748c2ecf20Sopenharmony_ci
16758c2ecf20Sopenharmony_ci	scsi_dev = scsi_device_lookup(a->host, 0, target_id, 0);
16768c2ecf20Sopenharmony_ci
16778c2ecf20Sopenharmony_ci	if (scsi_dev) {
16788c2ecf20Sopenharmony_ci		scsi_device_set_state(scsi_dev, SDEV_OFFLINE);
16798c2ecf20Sopenharmony_ci
16808c2ecf20Sopenharmony_ci		esas2r_log_dev(
16818c2ecf20Sopenharmony_ci			ESAS2R_LOG_INFO,
16828c2ecf20Sopenharmony_ci			&(scsi_dev->
16838c2ecf20Sopenharmony_ci			  sdev_gendev),
16848c2ecf20Sopenharmony_ci			"scsi_remove_device() called for 0:%d:0",
16858c2ecf20Sopenharmony_ci			target_id);
16868c2ecf20Sopenharmony_ci
16878c2ecf20Sopenharmony_ci		scsi_remove_device(scsi_dev);
16888c2ecf20Sopenharmony_ci
16898c2ecf20Sopenharmony_ci		esas2r_log_dev(
16908c2ecf20Sopenharmony_ci			ESAS2R_LOG_INFO,
16918c2ecf20Sopenharmony_ci			&(scsi_dev->
16928c2ecf20Sopenharmony_ci			  sdev_gendev),
16938c2ecf20Sopenharmony_ci			"scsi_device_put() called");
16948c2ecf20Sopenharmony_ci
16958c2ecf20Sopenharmony_ci		scsi_device_put(scsi_dev);
16968c2ecf20Sopenharmony_ci	} else {
16978c2ecf20Sopenharmony_ci		esas2r_log_dev(
16988c2ecf20Sopenharmony_ci			ESAS2R_LOG_WARN,
16998c2ecf20Sopenharmony_ci			&(a->host->shost_gendev),
17008c2ecf20Sopenharmony_ci			"no target found at id %d",
17018c2ecf20Sopenharmony_ci			target_id);
17028c2ecf20Sopenharmony_ci	}
17038c2ecf20Sopenharmony_ci}
17048c2ecf20Sopenharmony_ci
17058c2ecf20Sopenharmony_ci/*
17068c2ecf20Sopenharmony_ci * Sends a firmware asynchronous event to anyone who happens to be
17078c2ecf20Sopenharmony_ci * listening on the defined ATTO VDA event ports.
17088c2ecf20Sopenharmony_ci */
17098c2ecf20Sopenharmony_cistatic void esas2r_send_ae_event(struct esas2r_fw_event_work *fw_event)
17108c2ecf20Sopenharmony_ci{
17118c2ecf20Sopenharmony_ci	struct esas2r_vda_ae *ae = (struct esas2r_vda_ae *)fw_event->data;
17128c2ecf20Sopenharmony_ci	char *type;
17138c2ecf20Sopenharmony_ci
17148c2ecf20Sopenharmony_ci	switch (ae->vda_ae.hdr.bytype) {
17158c2ecf20Sopenharmony_ci	case VDAAE_HDR_TYPE_RAID:
17168c2ecf20Sopenharmony_ci		type = "RAID group state change";
17178c2ecf20Sopenharmony_ci		break;
17188c2ecf20Sopenharmony_ci
17198c2ecf20Sopenharmony_ci	case VDAAE_HDR_TYPE_LU:
17208c2ecf20Sopenharmony_ci		type = "Mapped destination LU change";
17218c2ecf20Sopenharmony_ci		break;
17228c2ecf20Sopenharmony_ci
17238c2ecf20Sopenharmony_ci	case VDAAE_HDR_TYPE_DISK:
17248c2ecf20Sopenharmony_ci		type = "Physical disk inventory change";
17258c2ecf20Sopenharmony_ci		break;
17268c2ecf20Sopenharmony_ci
17278c2ecf20Sopenharmony_ci	case VDAAE_HDR_TYPE_RESET:
17288c2ecf20Sopenharmony_ci		type = "Firmware reset";
17298c2ecf20Sopenharmony_ci		break;
17308c2ecf20Sopenharmony_ci
17318c2ecf20Sopenharmony_ci	case VDAAE_HDR_TYPE_LOG_INFO:
17328c2ecf20Sopenharmony_ci		type = "Event Log message (INFO level)";
17338c2ecf20Sopenharmony_ci		break;
17348c2ecf20Sopenharmony_ci
17358c2ecf20Sopenharmony_ci	case VDAAE_HDR_TYPE_LOG_WARN:
17368c2ecf20Sopenharmony_ci		type = "Event Log message (WARN level)";
17378c2ecf20Sopenharmony_ci		break;
17388c2ecf20Sopenharmony_ci
17398c2ecf20Sopenharmony_ci	case VDAAE_HDR_TYPE_LOG_CRIT:
17408c2ecf20Sopenharmony_ci		type = "Event Log message (CRIT level)";
17418c2ecf20Sopenharmony_ci		break;
17428c2ecf20Sopenharmony_ci
17438c2ecf20Sopenharmony_ci	case VDAAE_HDR_TYPE_LOG_FAIL:
17448c2ecf20Sopenharmony_ci		type = "Event Log message (FAIL level)";
17458c2ecf20Sopenharmony_ci		break;
17468c2ecf20Sopenharmony_ci
17478c2ecf20Sopenharmony_ci	case VDAAE_HDR_TYPE_NVC:
17488c2ecf20Sopenharmony_ci		type = "NVCache change";
17498c2ecf20Sopenharmony_ci		break;
17508c2ecf20Sopenharmony_ci
17518c2ecf20Sopenharmony_ci	case VDAAE_HDR_TYPE_TLG_INFO:
17528c2ecf20Sopenharmony_ci		type = "Time stamped log message (INFO level)";
17538c2ecf20Sopenharmony_ci		break;
17548c2ecf20Sopenharmony_ci
17558c2ecf20Sopenharmony_ci	case VDAAE_HDR_TYPE_TLG_WARN:
17568c2ecf20Sopenharmony_ci		type = "Time stamped log message (WARN level)";
17578c2ecf20Sopenharmony_ci		break;
17588c2ecf20Sopenharmony_ci
17598c2ecf20Sopenharmony_ci	case VDAAE_HDR_TYPE_TLG_CRIT:
17608c2ecf20Sopenharmony_ci		type = "Time stamped log message (CRIT level)";
17618c2ecf20Sopenharmony_ci		break;
17628c2ecf20Sopenharmony_ci
17638c2ecf20Sopenharmony_ci	case VDAAE_HDR_TYPE_PWRMGT:
17648c2ecf20Sopenharmony_ci		type = "Power management";
17658c2ecf20Sopenharmony_ci		break;
17668c2ecf20Sopenharmony_ci
17678c2ecf20Sopenharmony_ci	case VDAAE_HDR_TYPE_MUTE:
17688c2ecf20Sopenharmony_ci		type = "Mute button pressed";
17698c2ecf20Sopenharmony_ci		break;
17708c2ecf20Sopenharmony_ci
17718c2ecf20Sopenharmony_ci	case VDAAE_HDR_TYPE_DEV:
17728c2ecf20Sopenharmony_ci		type = "Device attribute change";
17738c2ecf20Sopenharmony_ci		break;
17748c2ecf20Sopenharmony_ci
17758c2ecf20Sopenharmony_ci	default:
17768c2ecf20Sopenharmony_ci		type = "Unknown";
17778c2ecf20Sopenharmony_ci		break;
17788c2ecf20Sopenharmony_ci	}
17798c2ecf20Sopenharmony_ci
17808c2ecf20Sopenharmony_ci	esas2r_log(ESAS2R_LOG_WARN,
17818c2ecf20Sopenharmony_ci		   "An async event of type \"%s\" was received from the firmware.  The event contents are:",
17828c2ecf20Sopenharmony_ci		   type);
17838c2ecf20Sopenharmony_ci	esas2r_log_hexdump(ESAS2R_LOG_WARN, &ae->vda_ae,
17848c2ecf20Sopenharmony_ci			   ae->vda_ae.hdr.bylength);
17858c2ecf20Sopenharmony_ci
17868c2ecf20Sopenharmony_ci}
17878c2ecf20Sopenharmony_ci
17888c2ecf20Sopenharmony_cistatic void
17898c2ecf20Sopenharmony_ciesas2r_firmware_event_work(struct work_struct *work)
17908c2ecf20Sopenharmony_ci{
17918c2ecf20Sopenharmony_ci	struct esas2r_fw_event_work *fw_event =
17928c2ecf20Sopenharmony_ci		container_of(work, struct esas2r_fw_event_work, work.work);
17938c2ecf20Sopenharmony_ci
17948c2ecf20Sopenharmony_ci	struct esas2r_adapter *a = fw_event->a;
17958c2ecf20Sopenharmony_ci
17968c2ecf20Sopenharmony_ci	u16 target_id = *(u16 *)&fw_event->data[0];
17978c2ecf20Sopenharmony_ci
17988c2ecf20Sopenharmony_ci	if (a->fw_events_off)
17998c2ecf20Sopenharmony_ci		goto done;
18008c2ecf20Sopenharmony_ci
18018c2ecf20Sopenharmony_ci	switch (fw_event->type) {
18028c2ecf20Sopenharmony_ci	case fw_event_null:
18038c2ecf20Sopenharmony_ci		break; /* do nothing */
18048c2ecf20Sopenharmony_ci
18058c2ecf20Sopenharmony_ci	case fw_event_lun_change:
18068c2ecf20Sopenharmony_ci		esas2r_remove_device(a, target_id);
18078c2ecf20Sopenharmony_ci		esas2r_add_device(a, target_id);
18088c2ecf20Sopenharmony_ci		break;
18098c2ecf20Sopenharmony_ci
18108c2ecf20Sopenharmony_ci	case fw_event_present:
18118c2ecf20Sopenharmony_ci		esas2r_add_device(a, target_id);
18128c2ecf20Sopenharmony_ci		break;
18138c2ecf20Sopenharmony_ci
18148c2ecf20Sopenharmony_ci	case fw_event_not_present:
18158c2ecf20Sopenharmony_ci		esas2r_remove_device(a, target_id);
18168c2ecf20Sopenharmony_ci		break;
18178c2ecf20Sopenharmony_ci
18188c2ecf20Sopenharmony_ci	case fw_event_vda_ae:
18198c2ecf20Sopenharmony_ci		esas2r_send_ae_event(fw_event);
18208c2ecf20Sopenharmony_ci		break;
18218c2ecf20Sopenharmony_ci	}
18228c2ecf20Sopenharmony_ci
18238c2ecf20Sopenharmony_cidone:
18248c2ecf20Sopenharmony_ci	esas2r_free_fw_event(fw_event);
18258c2ecf20Sopenharmony_ci}
18268c2ecf20Sopenharmony_ci
18278c2ecf20Sopenharmony_civoid esas2r_queue_fw_event(struct esas2r_adapter *a,
18288c2ecf20Sopenharmony_ci			   enum fw_event_type type,
18298c2ecf20Sopenharmony_ci			   void *data,
18308c2ecf20Sopenharmony_ci			   int data_sz)
18318c2ecf20Sopenharmony_ci{
18328c2ecf20Sopenharmony_ci	struct esas2r_fw_event_work *fw_event;
18338c2ecf20Sopenharmony_ci	unsigned long flags;
18348c2ecf20Sopenharmony_ci
18358c2ecf20Sopenharmony_ci	fw_event = kzalloc(sizeof(struct esas2r_fw_event_work), GFP_ATOMIC);
18368c2ecf20Sopenharmony_ci	if (!fw_event) {
18378c2ecf20Sopenharmony_ci		esas2r_log(ESAS2R_LOG_WARN,
18388c2ecf20Sopenharmony_ci			   "esas2r_queue_fw_event failed to alloc");
18398c2ecf20Sopenharmony_ci		return;
18408c2ecf20Sopenharmony_ci	}
18418c2ecf20Sopenharmony_ci
18428c2ecf20Sopenharmony_ci	if (type == fw_event_vda_ae) {
18438c2ecf20Sopenharmony_ci		struct esas2r_vda_ae *ae =
18448c2ecf20Sopenharmony_ci			(struct esas2r_vda_ae *)fw_event->data;
18458c2ecf20Sopenharmony_ci
18468c2ecf20Sopenharmony_ci		ae->signature = ESAS2R_VDA_EVENT_SIG;
18478c2ecf20Sopenharmony_ci		ae->bus_number = a->pcid->bus->number;
18488c2ecf20Sopenharmony_ci		ae->devfn = a->pcid->devfn;
18498c2ecf20Sopenharmony_ci		memcpy(&ae->vda_ae, data, sizeof(ae->vda_ae));
18508c2ecf20Sopenharmony_ci	} else {
18518c2ecf20Sopenharmony_ci		memcpy(fw_event->data, data, data_sz);
18528c2ecf20Sopenharmony_ci	}
18538c2ecf20Sopenharmony_ci
18548c2ecf20Sopenharmony_ci	fw_event->type = type;
18558c2ecf20Sopenharmony_ci	fw_event->a = a;
18568c2ecf20Sopenharmony_ci
18578c2ecf20Sopenharmony_ci	spin_lock_irqsave(&a->fw_event_lock, flags);
18588c2ecf20Sopenharmony_ci	list_add_tail(&fw_event->list, &a->fw_event_list);
18598c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&fw_event->work, esas2r_firmware_event_work);
18608c2ecf20Sopenharmony_ci	queue_delayed_work_on(
18618c2ecf20Sopenharmony_ci		smp_processor_id(), a->fw_event_q, &fw_event->work,
18628c2ecf20Sopenharmony_ci		msecs_to_jiffies(1));
18638c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&a->fw_event_lock, flags);
18648c2ecf20Sopenharmony_ci}
18658c2ecf20Sopenharmony_ci
18668c2ecf20Sopenharmony_civoid esas2r_target_state_changed(struct esas2r_adapter *a, u16 targ_id,
18678c2ecf20Sopenharmony_ci				 u8 state)
18688c2ecf20Sopenharmony_ci{
18698c2ecf20Sopenharmony_ci	if (state == TS_LUN_CHANGE)
18708c2ecf20Sopenharmony_ci		esas2r_queue_fw_event(a, fw_event_lun_change, &targ_id,
18718c2ecf20Sopenharmony_ci				      sizeof(targ_id));
18728c2ecf20Sopenharmony_ci	else if (state == TS_PRESENT)
18738c2ecf20Sopenharmony_ci		esas2r_queue_fw_event(a, fw_event_present, &targ_id,
18748c2ecf20Sopenharmony_ci				      sizeof(targ_id));
18758c2ecf20Sopenharmony_ci	else if (state == TS_NOT_PRESENT)
18768c2ecf20Sopenharmony_ci		esas2r_queue_fw_event(a, fw_event_not_present, &targ_id,
18778c2ecf20Sopenharmony_ci				      sizeof(targ_id));
18788c2ecf20Sopenharmony_ci}
18798c2ecf20Sopenharmony_ci
18808c2ecf20Sopenharmony_ci/* Translate status to a Linux SCSI mid-layer error code */
18818c2ecf20Sopenharmony_ciint esas2r_req_status_to_error(u8 req_stat)
18828c2ecf20Sopenharmony_ci{
18838c2ecf20Sopenharmony_ci	switch (req_stat) {
18848c2ecf20Sopenharmony_ci	case RS_OVERRUN:
18858c2ecf20Sopenharmony_ci	case RS_UNDERRUN:
18868c2ecf20Sopenharmony_ci	case RS_SUCCESS:
18878c2ecf20Sopenharmony_ci	/*
18888c2ecf20Sopenharmony_ci	 * NOTE: SCSI mid-layer wants a good status for a SCSI error, because
18898c2ecf20Sopenharmony_ci	 *       it will check the scsi_stat value in the completion anyway.
18908c2ecf20Sopenharmony_ci	 */
18918c2ecf20Sopenharmony_ci	case RS_SCSI_ERROR:
18928c2ecf20Sopenharmony_ci		return DID_OK;
18938c2ecf20Sopenharmony_ci
18948c2ecf20Sopenharmony_ci	case RS_SEL:
18958c2ecf20Sopenharmony_ci	case RS_SEL2:
18968c2ecf20Sopenharmony_ci		return DID_NO_CONNECT;
18978c2ecf20Sopenharmony_ci
18988c2ecf20Sopenharmony_ci	case RS_RESET:
18998c2ecf20Sopenharmony_ci		return DID_RESET;
19008c2ecf20Sopenharmony_ci
19018c2ecf20Sopenharmony_ci	case RS_ABORTED:
19028c2ecf20Sopenharmony_ci		return DID_ABORT;
19038c2ecf20Sopenharmony_ci
19048c2ecf20Sopenharmony_ci	case RS_BUSY:
19058c2ecf20Sopenharmony_ci		return DID_BUS_BUSY;
19068c2ecf20Sopenharmony_ci	}
19078c2ecf20Sopenharmony_ci
19088c2ecf20Sopenharmony_ci	/* everything else is just an error. */
19098c2ecf20Sopenharmony_ci
19108c2ecf20Sopenharmony_ci	return DID_ERROR;
19118c2ecf20Sopenharmony_ci}
19128c2ecf20Sopenharmony_ci
19138c2ecf20Sopenharmony_cimodule_init(esas2r_init);
19148c2ecf20Sopenharmony_cimodule_exit(esas2r_exit);
1915