xref: /openbmc/linux/drivers/scsi/hpsa.c (revision 73153fe533bc6209802521fc62d3e3648581a121)
1edd16368SStephen M. Cameron /*
2edd16368SStephen M. Cameron  *    Disk Array driver for HP Smart Array SAS controllers
351c35139SScott Teel  *    Copyright 2000, 2014 Hewlett-Packard Development Company, L.P.
4edd16368SStephen M. Cameron  *
5edd16368SStephen M. Cameron  *    This program is free software; you can redistribute it and/or modify
6edd16368SStephen M. Cameron  *    it under the terms of the GNU General Public License as published by
7edd16368SStephen M. Cameron  *    the Free Software Foundation; version 2 of the License.
8edd16368SStephen M. Cameron  *
9edd16368SStephen M. Cameron  *    This program is distributed in the hope that it will be useful,
10edd16368SStephen M. Cameron  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
11edd16368SStephen M. Cameron  *    MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
12edd16368SStephen M. Cameron  *    NON INFRINGEMENT.  See the GNU General Public License for more details.
13edd16368SStephen M. Cameron  *
14edd16368SStephen M. Cameron  *    You should have received a copy of the GNU General Public License
15edd16368SStephen M. Cameron  *    along with this program; if not, write to the Free Software
16edd16368SStephen M. Cameron  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17edd16368SStephen M. Cameron  *
18edd16368SStephen M. Cameron  *    Questions/Comments/Bugfixes to iss_storagedev@hp.com
19edd16368SStephen M. Cameron  *
20edd16368SStephen M. Cameron  */
21edd16368SStephen M. Cameron 
22edd16368SStephen M. Cameron #include <linux/module.h>
23edd16368SStephen M. Cameron #include <linux/interrupt.h>
24edd16368SStephen M. Cameron #include <linux/types.h>
25edd16368SStephen M. Cameron #include <linux/pci.h>
26e5a44df8SMatthew Garrett #include <linux/pci-aspm.h>
27edd16368SStephen M. Cameron #include <linux/kernel.h>
28edd16368SStephen M. Cameron #include <linux/slab.h>
29edd16368SStephen M. Cameron #include <linux/delay.h>
30edd16368SStephen M. Cameron #include <linux/fs.h>
31edd16368SStephen M. Cameron #include <linux/timer.h>
32edd16368SStephen M. Cameron #include <linux/init.h>
33edd16368SStephen M. Cameron #include <linux/spinlock.h>
34edd16368SStephen M. Cameron #include <linux/compat.h>
35edd16368SStephen M. Cameron #include <linux/blktrace_api.h>
36edd16368SStephen M. Cameron #include <linux/uaccess.h>
37edd16368SStephen M. Cameron #include <linux/io.h>
38edd16368SStephen M. Cameron #include <linux/dma-mapping.h>
39edd16368SStephen M. Cameron #include <linux/completion.h>
40edd16368SStephen M. Cameron #include <linux/moduleparam.h>
41edd16368SStephen M. Cameron #include <scsi/scsi.h>
42edd16368SStephen M. Cameron #include <scsi/scsi_cmnd.h>
43edd16368SStephen M. Cameron #include <scsi/scsi_device.h>
44edd16368SStephen M. Cameron #include <scsi/scsi_host.h>
45667e23d4SStephen M. Cameron #include <scsi/scsi_tcq.h>
469437ac43SStephen Cameron #include <scsi/scsi_eh.h>
47*73153fe5SWebb Scales #include <scsi/scsi_dbg.h>
48edd16368SStephen M. Cameron #include <linux/cciss_ioctl.h>
49edd16368SStephen M. Cameron #include <linux/string.h>
50edd16368SStephen M. Cameron #include <linux/bitmap.h>
5160063497SArun Sharma #include <linux/atomic.h>
52a0c12413SStephen M. Cameron #include <linux/jiffies.h>
5342a91641SDon Brace #include <linux/percpu-defs.h>
54094963daSStephen M. Cameron #include <linux/percpu.h>
552b08b3e9SDon Brace #include <asm/unaligned.h>
56283b4a9bSStephen M. Cameron #include <asm/div64.h>
57edd16368SStephen M. Cameron #include "hpsa_cmd.h"
58edd16368SStephen M. Cameron #include "hpsa.h"
59edd16368SStephen M. Cameron 
60edd16368SStephen M. Cameron /* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */
619a993302SStephen M. Cameron #define HPSA_DRIVER_VERSION "3.4.4-1"
62edd16368SStephen M. Cameron #define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
63f79cfec6SStephen M. Cameron #define HPSA "hpsa"
64edd16368SStephen M. Cameron 
65007e7aa9SRobert Elliott /* How long to wait for CISS doorbell communication */
66007e7aa9SRobert Elliott #define CLEAR_EVENT_WAIT_INTERVAL 20	/* ms for each msleep() call */
67007e7aa9SRobert Elliott #define MODE_CHANGE_WAIT_INTERVAL 10	/* ms for each msleep() call */
68007e7aa9SRobert Elliott #define MAX_CLEAR_EVENT_WAIT 30000	/* times 20 ms = 600 s */
69007e7aa9SRobert Elliott #define MAX_MODE_CHANGE_WAIT 2000	/* times 10 ms = 20 s */
70edd16368SStephen M. Cameron #define MAX_IOCTL_CONFIG_WAIT 1000
71edd16368SStephen M. Cameron 
72edd16368SStephen M. Cameron /*define how many times we will try a command because of bus resets */
73edd16368SStephen M. Cameron #define MAX_CMD_RETRIES 3
74edd16368SStephen M. Cameron 
75edd16368SStephen M. Cameron /* Embedded module documentation macros - see modules.h */
76edd16368SStephen M. Cameron MODULE_AUTHOR("Hewlett-Packard Company");
77edd16368SStephen M. Cameron MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
78edd16368SStephen M. Cameron 	HPSA_DRIVER_VERSION);
79edd16368SStephen M. Cameron MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
80edd16368SStephen M. Cameron MODULE_VERSION(HPSA_DRIVER_VERSION);
81edd16368SStephen M. Cameron MODULE_LICENSE("GPL");
82edd16368SStephen M. Cameron 
83edd16368SStephen M. Cameron static int hpsa_allow_any;
84edd16368SStephen M. Cameron module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
85edd16368SStephen M. Cameron MODULE_PARM_DESC(hpsa_allow_any,
86edd16368SStephen M. Cameron 		"Allow hpsa driver to access unknown HP Smart Array hardware");
8702ec19c8SStephen M. Cameron static int hpsa_simple_mode;
8802ec19c8SStephen M. Cameron module_param(hpsa_simple_mode, int, S_IRUGO|S_IWUSR);
8902ec19c8SStephen M. Cameron MODULE_PARM_DESC(hpsa_simple_mode,
9002ec19c8SStephen M. Cameron 	"Use 'simple mode' rather than 'performant mode'");
91edd16368SStephen M. Cameron 
92edd16368SStephen M. Cameron /* define the PCI info for the cards we can control */
93edd16368SStephen M. Cameron static const struct pci_device_id hpsa_pci_device_id[] = {
94edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3241},
95edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3243},
96edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3245},
97edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3247},
98edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3249},
99163dbcd8SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x324A},
100163dbcd8SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x324B},
101f8b01eb9SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3233},
1029143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3350},
1039143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3351},
1049143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3352},
1059143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3353},
1069143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3354},
1079143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3355},
1089143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3356},
109fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1921},
110fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1922},
111fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1923},
112fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1924},
113fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1926},
114fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1928},
11597b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1929},
11697b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BD},
11797b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BE},
11897b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BF},
11997b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C0},
12097b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C1},
12197b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C2},
12297b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C3},
12397b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C4},
12497b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C5},
1253b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C6},
12697b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C7},
12797b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C8},
12897b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C9},
1293b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CA},
1303b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CB},
1313b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CC},
1323b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CD},
1333b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CE},
1348e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0076},
1358e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0087},
1368e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x007D},
1378e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0088},
1388e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP, 0x333f, 0x103c, 0x333f},
139edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_ANY_ID,	PCI_ANY_ID, PCI_ANY_ID,
140edd16368SStephen M. Cameron 		PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
141edd16368SStephen M. Cameron 	{0,}
142edd16368SStephen M. Cameron };
143edd16368SStephen M. Cameron 
144edd16368SStephen M. Cameron MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
145edd16368SStephen M. Cameron 
146edd16368SStephen M. Cameron /*  board_id = Subsystem Device ID & Vendor ID
147edd16368SStephen M. Cameron  *  product = Marketing Name for the board
148edd16368SStephen M. Cameron  *  access = Address of the struct of function pointers
149edd16368SStephen M. Cameron  */
150edd16368SStephen M. Cameron static struct board_type products[] = {
151edd16368SStephen M. Cameron 	{0x3241103C, "Smart Array P212", &SA5_access},
152edd16368SStephen M. Cameron 	{0x3243103C, "Smart Array P410", &SA5_access},
153edd16368SStephen M. Cameron 	{0x3245103C, "Smart Array P410i", &SA5_access},
154edd16368SStephen M. Cameron 	{0x3247103C, "Smart Array P411", &SA5_access},
155edd16368SStephen M. Cameron 	{0x3249103C, "Smart Array P812", &SA5_access},
156163dbcd8SMike Miller 	{0x324A103C, "Smart Array P712m", &SA5_access},
157163dbcd8SMike Miller 	{0x324B103C, "Smart Array P711m", &SA5_access},
1587d2cce58SStephen M. Cameron 	{0x3233103C, "HP StorageWorks 1210m", &SA5_access}, /* alias of 333f */
159fe0c9610SMike Miller 	{0x3350103C, "Smart Array P222", &SA5_access},
160fe0c9610SMike Miller 	{0x3351103C, "Smart Array P420", &SA5_access},
161fe0c9610SMike Miller 	{0x3352103C, "Smart Array P421", &SA5_access},
162fe0c9610SMike Miller 	{0x3353103C, "Smart Array P822", &SA5_access},
163fe0c9610SMike Miller 	{0x3354103C, "Smart Array P420i", &SA5_access},
164fe0c9610SMike Miller 	{0x3355103C, "Smart Array P220i", &SA5_access},
165fe0c9610SMike Miller 	{0x3356103C, "Smart Array P721m", &SA5_access},
1661fd6c8e3SMike Miller 	{0x1921103C, "Smart Array P830i", &SA5_access},
1671fd6c8e3SMike Miller 	{0x1922103C, "Smart Array P430", &SA5_access},
1681fd6c8e3SMike Miller 	{0x1923103C, "Smart Array P431", &SA5_access},
1691fd6c8e3SMike Miller 	{0x1924103C, "Smart Array P830", &SA5_access},
1701fd6c8e3SMike Miller 	{0x1926103C, "Smart Array P731m", &SA5_access},
1711fd6c8e3SMike Miller 	{0x1928103C, "Smart Array P230i", &SA5_access},
1721fd6c8e3SMike Miller 	{0x1929103C, "Smart Array P530", &SA5_access},
17327fb8137SDon Brace 	{0x21BD103C, "Smart Array P244br", &SA5_access},
17427fb8137SDon Brace 	{0x21BE103C, "Smart Array P741m", &SA5_access},
17527fb8137SDon Brace 	{0x21BF103C, "Smart HBA H240ar", &SA5_access},
17627fb8137SDon Brace 	{0x21C0103C, "Smart Array P440ar", &SA5_access},
177c8ae0ab1SDon Brace 	{0x21C1103C, "Smart Array P840ar", &SA5_access},
17827fb8137SDon Brace 	{0x21C2103C, "Smart Array P440", &SA5_access},
17927fb8137SDon Brace 	{0x21C3103C, "Smart Array P441", &SA5_access},
18097b9f53dSMike Miller 	{0x21C4103C, "Smart Array", &SA5_access},
18127fb8137SDon Brace 	{0x21C5103C, "Smart Array P841", &SA5_access},
18227fb8137SDon Brace 	{0x21C6103C, "Smart HBA H244br", &SA5_access},
18327fb8137SDon Brace 	{0x21C7103C, "Smart HBA H240", &SA5_access},
18427fb8137SDon Brace 	{0x21C8103C, "Smart HBA H241", &SA5_access},
18597b9f53dSMike Miller 	{0x21C9103C, "Smart Array", &SA5_access},
18627fb8137SDon Brace 	{0x21CA103C, "Smart Array P246br", &SA5_access},
18727fb8137SDon Brace 	{0x21CB103C, "Smart Array P840", &SA5_access},
1883b7a45e5SJoe Handzik 	{0x21CC103C, "Smart Array", &SA5_access},
1893b7a45e5SJoe Handzik 	{0x21CD103C, "Smart Array", &SA5_access},
19027fb8137SDon Brace 	{0x21CE103C, "Smart HBA", &SA5_access},
1918e616a5eSStephen M. Cameron 	{0x00761590, "HP Storage P1224 Array Controller", &SA5_access},
1928e616a5eSStephen M. Cameron 	{0x00871590, "HP Storage P1224e Array Controller", &SA5_access},
1938e616a5eSStephen M. Cameron 	{0x007D1590, "HP Storage P1228 Array Controller", &SA5_access},
1948e616a5eSStephen M. Cameron 	{0x00881590, "HP Storage P1228e Array Controller", &SA5_access},
1958e616a5eSStephen M. Cameron 	{0x333f103c, "HP StorageWorks 1210m Array Controller", &SA5_access},
196edd16368SStephen M. Cameron 	{0xFFFF103C, "Unknown Smart Array", &SA5_access},
197edd16368SStephen M. Cameron };
198edd16368SStephen M. Cameron 
199a58e7e53SWebb Scales #define SCSI_CMD_BUSY ((struct scsi_cmnd *)&hpsa_cmd_busy)
200a58e7e53SWebb Scales static const struct scsi_cmnd hpsa_cmd_busy;
201a58e7e53SWebb Scales #define SCSI_CMD_IDLE ((struct scsi_cmnd *)&hpsa_cmd_idle)
202a58e7e53SWebb Scales static const struct scsi_cmnd hpsa_cmd_idle;
203edd16368SStephen M. Cameron static int number_of_controllers;
204edd16368SStephen M. Cameron 
20510f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id);
20610f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id);
20742a91641SDon Brace static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg);
208edd16368SStephen M. Cameron 
209edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
21042a91641SDon Brace static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd,
21142a91641SDon Brace 	void __user *arg);
212edd16368SStephen M. Cameron #endif
213edd16368SStephen M. Cameron 
214edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c);
215edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h);
216*73153fe5SWebb Scales static void cmd_tagged_free(struct ctlr_info *h, struct CommandList *c);
217*73153fe5SWebb Scales static struct CommandList *cmd_tagged_alloc(struct ctlr_info *h,
218*73153fe5SWebb Scales 					    struct scsi_cmnd *scmd);
219a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
220b7bb24ebSStephen M. Cameron 	void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
221edd16368SStephen M. Cameron 	int cmd_type);
2222c143342SRobert Elliott static void hpsa_free_cmd_pool(struct ctlr_info *h);
223b7bb24ebSStephen M. Cameron #define VPD_PAGE (1 << 8)
224edd16368SStephen M. Cameron 
225f281233dSJeff Garzik static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
226a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *);
227a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
228a08a8471SStephen M. Cameron 	unsigned long elapsed_time);
2297c0a0229SDon Brace static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth);
230edd16368SStephen M. Cameron 
231edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
23275167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *scsicmd);
233edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev);
23441ce4c35SStephen Cameron static int hpsa_slave_configure(struct scsi_device *sdev);
235edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev);
236edd16368SStephen M. Cameron 
237edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
238edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
239edd16368SStephen M. Cameron 	struct CommandList *c);
240edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
241edd16368SStephen M. Cameron 	struct CommandList *c);
242303932fdSDon Brace /* performant mode helper functions */
243303932fdSDon Brace static void calc_bucket_map(int *bucket, int num_buckets,
2442b08b3e9SDon Brace 	int nsgs, int min_blocks, u32 *bucket_map);
245105a3dbcSRobert Elliott static void hpsa_free_performant_mode(struct ctlr_info *h);
246105a3dbcSRobert Elliott static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
247254f796bSMatt Gates static inline u32 next_command(struct ctlr_info *h, u8 q);
2486f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
2496f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
2501df8552aSStephen M. Cameron 			       u64 *cfg_offset);
2516f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
2521df8552aSStephen M. Cameron 				    unsigned long *memory_bar);
2536f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
2546f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
2556f039790SGreg Kroah-Hartman 				     int wait_for_ready);
25675167d2cSStephen M. Cameron static inline void finish_cmd(struct CommandList *c);
257c706a795SRobert Elliott static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h);
258fe5389c8SStephen M. Cameron #define BOARD_NOT_READY 0
259fe5389c8SStephen M. Cameron #define BOARD_READY 1
26023100dd9SStephen M. Cameron static void hpsa_drain_accel_commands(struct ctlr_info *h);
26176438d08SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h);
262c349775eSScott Teel static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
263c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
26403383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk);
265080ef1ccSDon Brace static void hpsa_command_resubmit_worker(struct work_struct *work);
26625163bd5SWebb Scales static u32 lockup_detected(struct ctlr_info *h);
26725163bd5SWebb Scales static int detect_controller_lockup(struct ctlr_info *h);
268edd16368SStephen M. Cameron 
269edd16368SStephen M. Cameron static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
270edd16368SStephen M. Cameron {
271edd16368SStephen M. Cameron 	unsigned long *priv = shost_priv(sdev->host);
272edd16368SStephen M. Cameron 	return (struct ctlr_info *) *priv;
273edd16368SStephen M. Cameron }
274edd16368SStephen M. Cameron 
275a23513e8SStephen M. Cameron static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
276a23513e8SStephen M. Cameron {
277a23513e8SStephen M. Cameron 	unsigned long *priv = shost_priv(sh);
278a23513e8SStephen M. Cameron 	return (struct ctlr_info *) *priv;
279a23513e8SStephen M. Cameron }
280a23513e8SStephen M. Cameron 
281a58e7e53SWebb Scales static inline bool hpsa_is_cmd_idle(struct CommandList *c)
282a58e7e53SWebb Scales {
283a58e7e53SWebb Scales 	return c->scsi_cmd == SCSI_CMD_IDLE;
284a58e7e53SWebb Scales }
285a58e7e53SWebb Scales 
2869437ac43SStephen Cameron /* extract sense key, asc, and ascq from sense data.  -1 means invalid. */
2879437ac43SStephen Cameron static void decode_sense_data(const u8 *sense_data, int sense_data_len,
2889437ac43SStephen Cameron 			u8 *sense_key, u8 *asc, u8 *ascq)
2899437ac43SStephen Cameron {
2909437ac43SStephen Cameron 	struct scsi_sense_hdr sshdr;
2919437ac43SStephen Cameron 	bool rc;
2929437ac43SStephen Cameron 
2939437ac43SStephen Cameron 	*sense_key = -1;
2949437ac43SStephen Cameron 	*asc = -1;
2959437ac43SStephen Cameron 	*ascq = -1;
2969437ac43SStephen Cameron 
2979437ac43SStephen Cameron 	if (sense_data_len < 1)
2989437ac43SStephen Cameron 		return;
2999437ac43SStephen Cameron 
3009437ac43SStephen Cameron 	rc = scsi_normalize_sense(sense_data, sense_data_len, &sshdr);
3019437ac43SStephen Cameron 	if (rc) {
3029437ac43SStephen Cameron 		*sense_key = sshdr.sense_key;
3039437ac43SStephen Cameron 		*asc = sshdr.asc;
3049437ac43SStephen Cameron 		*ascq = sshdr.ascq;
3059437ac43SStephen Cameron 	}
3069437ac43SStephen Cameron }
3079437ac43SStephen Cameron 
308edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
309edd16368SStephen M. Cameron 	struct CommandList *c)
310edd16368SStephen M. Cameron {
3119437ac43SStephen Cameron 	u8 sense_key, asc, ascq;
3129437ac43SStephen Cameron 	int sense_len;
3139437ac43SStephen Cameron 
3149437ac43SStephen Cameron 	if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
3159437ac43SStephen Cameron 		sense_len = sizeof(c->err_info->SenseInfo);
3169437ac43SStephen Cameron 	else
3179437ac43SStephen Cameron 		sense_len = c->err_info->SenseLen;
3189437ac43SStephen Cameron 
3199437ac43SStephen Cameron 	decode_sense_data(c->err_info->SenseInfo, sense_len,
3209437ac43SStephen Cameron 				&sense_key, &asc, &ascq);
3219437ac43SStephen Cameron 	if (sense_key != UNIT_ATTENTION || asc == -1)
322edd16368SStephen M. Cameron 		return 0;
323edd16368SStephen M. Cameron 
3249437ac43SStephen Cameron 	switch (asc) {
325edd16368SStephen M. Cameron 	case STATE_CHANGED:
3269437ac43SStephen Cameron 		dev_warn(&h->pdev->dev,
3279437ac43SStephen Cameron 			HPSA "%d: a state change detected, command retried\n",
3289437ac43SStephen Cameron 			h->ctlr);
329edd16368SStephen M. Cameron 		break;
330edd16368SStephen M. Cameron 	case LUN_FAILED:
3317f73695aSStephen M. Cameron 		dev_warn(&h->pdev->dev,
3327f73695aSStephen M. Cameron 			HPSA "%d: LUN failure detected\n", h->ctlr);
333edd16368SStephen M. Cameron 		break;
334edd16368SStephen M. Cameron 	case REPORT_LUNS_CHANGED:
3357f73695aSStephen M. Cameron 		dev_warn(&h->pdev->dev,
3367f73695aSStephen M. Cameron 			HPSA "%d: report LUN data changed\n", h->ctlr);
337edd16368SStephen M. Cameron 	/*
3384f4eb9f1SScott Teel 	 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
3394f4eb9f1SScott Teel 	 * target (array) devices.
340edd16368SStephen M. Cameron 	 */
341edd16368SStephen M. Cameron 		break;
342edd16368SStephen M. Cameron 	case POWER_OR_RESET:
343f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: a power on "
344edd16368SStephen M. Cameron 			"or device reset detected\n", h->ctlr);
345edd16368SStephen M. Cameron 		break;
346edd16368SStephen M. Cameron 	case UNIT_ATTENTION_CLEARED:
347f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: unit attention "
348edd16368SStephen M. Cameron 		    "cleared by another initiator\n", h->ctlr);
349edd16368SStephen M. Cameron 		break;
350edd16368SStephen M. Cameron 	default:
351f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: unknown "
352edd16368SStephen M. Cameron 			"unit attention detected\n", h->ctlr);
353edd16368SStephen M. Cameron 		break;
354edd16368SStephen M. Cameron 	}
355edd16368SStephen M. Cameron 	return 1;
356edd16368SStephen M. Cameron }
357edd16368SStephen M. Cameron 
358852af20aSMatt Bondurant static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
359852af20aSMatt Bondurant {
360852af20aSMatt Bondurant 	if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
361852af20aSMatt Bondurant 		(c->err_info->ScsiStatus != SAM_STAT_BUSY &&
362852af20aSMatt Bondurant 		 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
363852af20aSMatt Bondurant 		return 0;
364852af20aSMatt Bondurant 	dev_warn(&h->pdev->dev, HPSA "device busy");
365852af20aSMatt Bondurant 	return 1;
366852af20aSMatt Bondurant }
367852af20aSMatt Bondurant 
368e985c58fSStephen Cameron static u32 lockup_detected(struct ctlr_info *h);
369e985c58fSStephen Cameron static ssize_t host_show_lockup_detected(struct device *dev,
370e985c58fSStephen Cameron 		struct device_attribute *attr, char *buf)
371e985c58fSStephen Cameron {
372e985c58fSStephen Cameron 	int ld;
373e985c58fSStephen Cameron 	struct ctlr_info *h;
374e985c58fSStephen Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
375e985c58fSStephen Cameron 
376e985c58fSStephen Cameron 	h = shost_to_hba(shost);
377e985c58fSStephen Cameron 	ld = lockup_detected(h);
378e985c58fSStephen Cameron 
379e985c58fSStephen Cameron 	return sprintf(buf, "ld=%d\n", ld);
380e985c58fSStephen Cameron }
381e985c58fSStephen Cameron 
382da0697bdSScott Teel static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
383da0697bdSScott Teel 					 struct device_attribute *attr,
384da0697bdSScott Teel 					 const char *buf, size_t count)
385da0697bdSScott Teel {
386da0697bdSScott Teel 	int status, len;
387da0697bdSScott Teel 	struct ctlr_info *h;
388da0697bdSScott Teel 	struct Scsi_Host *shost = class_to_shost(dev);
389da0697bdSScott Teel 	char tmpbuf[10];
390da0697bdSScott Teel 
391da0697bdSScott Teel 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
392da0697bdSScott Teel 		return -EACCES;
393da0697bdSScott Teel 	len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
394da0697bdSScott Teel 	strncpy(tmpbuf, buf, len);
395da0697bdSScott Teel 	tmpbuf[len] = '\0';
396da0697bdSScott Teel 	if (sscanf(tmpbuf, "%d", &status) != 1)
397da0697bdSScott Teel 		return -EINVAL;
398da0697bdSScott Teel 	h = shost_to_hba(shost);
399da0697bdSScott Teel 	h->acciopath_status = !!status;
400da0697bdSScott Teel 	dev_warn(&h->pdev->dev,
401da0697bdSScott Teel 		"hpsa: HP SSD Smart Path %s via sysfs update.\n",
402da0697bdSScott Teel 		h->acciopath_status ? "enabled" : "disabled");
403da0697bdSScott Teel 	return count;
404da0697bdSScott Teel }
405da0697bdSScott Teel 
4062ba8bfc8SStephen M. Cameron static ssize_t host_store_raid_offload_debug(struct device *dev,
4072ba8bfc8SStephen M. Cameron 					 struct device_attribute *attr,
4082ba8bfc8SStephen M. Cameron 					 const char *buf, size_t count)
4092ba8bfc8SStephen M. Cameron {
4102ba8bfc8SStephen M. Cameron 	int debug_level, len;
4112ba8bfc8SStephen M. Cameron 	struct ctlr_info *h;
4122ba8bfc8SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
4132ba8bfc8SStephen M. Cameron 	char tmpbuf[10];
4142ba8bfc8SStephen M. Cameron 
4152ba8bfc8SStephen M. Cameron 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
4162ba8bfc8SStephen M. Cameron 		return -EACCES;
4172ba8bfc8SStephen M. Cameron 	len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
4182ba8bfc8SStephen M. Cameron 	strncpy(tmpbuf, buf, len);
4192ba8bfc8SStephen M. Cameron 	tmpbuf[len] = '\0';
4202ba8bfc8SStephen M. Cameron 	if (sscanf(tmpbuf, "%d", &debug_level) != 1)
4212ba8bfc8SStephen M. Cameron 		return -EINVAL;
4222ba8bfc8SStephen M. Cameron 	if (debug_level < 0)
4232ba8bfc8SStephen M. Cameron 		debug_level = 0;
4242ba8bfc8SStephen M. Cameron 	h = shost_to_hba(shost);
4252ba8bfc8SStephen M. Cameron 	h->raid_offload_debug = debug_level;
4262ba8bfc8SStephen M. Cameron 	dev_warn(&h->pdev->dev, "hpsa: Set raid_offload_debug level = %d\n",
4272ba8bfc8SStephen M. Cameron 		h->raid_offload_debug);
4282ba8bfc8SStephen M. Cameron 	return count;
4292ba8bfc8SStephen M. Cameron }
4302ba8bfc8SStephen M. Cameron 
431edd16368SStephen M. Cameron static ssize_t host_store_rescan(struct device *dev,
432edd16368SStephen M. Cameron 				 struct device_attribute *attr,
433edd16368SStephen M. Cameron 				 const char *buf, size_t count)
434edd16368SStephen M. Cameron {
435edd16368SStephen M. Cameron 	struct ctlr_info *h;
436edd16368SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
437a23513e8SStephen M. Cameron 	h = shost_to_hba(shost);
43831468401SMike Miller 	hpsa_scan_start(h->scsi_host);
439edd16368SStephen M. Cameron 	return count;
440edd16368SStephen M. Cameron }
441edd16368SStephen M. Cameron 
442d28ce020SStephen M. Cameron static ssize_t host_show_firmware_revision(struct device *dev,
443d28ce020SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
444d28ce020SStephen M. Cameron {
445d28ce020SStephen M. Cameron 	struct ctlr_info *h;
446d28ce020SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
447d28ce020SStephen M. Cameron 	unsigned char *fwrev;
448d28ce020SStephen M. Cameron 
449d28ce020SStephen M. Cameron 	h = shost_to_hba(shost);
450d28ce020SStephen M. Cameron 	if (!h->hba_inquiry_data)
451d28ce020SStephen M. Cameron 		return 0;
452d28ce020SStephen M. Cameron 	fwrev = &h->hba_inquiry_data[32];
453d28ce020SStephen M. Cameron 	return snprintf(buf, 20, "%c%c%c%c\n",
454d28ce020SStephen M. Cameron 		fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
455d28ce020SStephen M. Cameron }
456d28ce020SStephen M. Cameron 
45794a13649SStephen M. Cameron static ssize_t host_show_commands_outstanding(struct device *dev,
45894a13649SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
45994a13649SStephen M. Cameron {
46094a13649SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
46194a13649SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(shost);
46294a13649SStephen M. Cameron 
4630cbf768eSStephen M. Cameron 	return snprintf(buf, 20, "%d\n",
4640cbf768eSStephen M. Cameron 			atomic_read(&h->commands_outstanding));
46594a13649SStephen M. Cameron }
46694a13649SStephen M. Cameron 
467745a7a25SStephen M. Cameron static ssize_t host_show_transport_mode(struct device *dev,
468745a7a25SStephen M. Cameron 	struct device_attribute *attr, char *buf)
469745a7a25SStephen M. Cameron {
470745a7a25SStephen M. Cameron 	struct ctlr_info *h;
471745a7a25SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
472745a7a25SStephen M. Cameron 
473745a7a25SStephen M. Cameron 	h = shost_to_hba(shost);
474745a7a25SStephen M. Cameron 	return snprintf(buf, 20, "%s\n",
475960a30e7SStephen M. Cameron 		h->transMethod & CFGTBL_Trans_Performant ?
476745a7a25SStephen M. Cameron 			"performant" : "simple");
477745a7a25SStephen M. Cameron }
478745a7a25SStephen M. Cameron 
479da0697bdSScott Teel static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
480da0697bdSScott Teel 	struct device_attribute *attr, char *buf)
481da0697bdSScott Teel {
482da0697bdSScott Teel 	struct ctlr_info *h;
483da0697bdSScott Teel 	struct Scsi_Host *shost = class_to_shost(dev);
484da0697bdSScott Teel 
485da0697bdSScott Teel 	h = shost_to_hba(shost);
486da0697bdSScott Teel 	return snprintf(buf, 30, "HP SSD Smart Path %s\n",
487da0697bdSScott Teel 		(h->acciopath_status == 1) ?  "enabled" : "disabled");
488da0697bdSScott Teel }
489da0697bdSScott Teel 
49046380786SStephen M. Cameron /* List of controllers which cannot be hard reset on kexec with reset_devices */
491941b1cdaSStephen M. Cameron static u32 unresettable_controller[] = {
492941b1cdaSStephen M. Cameron 	0x324a103C, /* Smart Array P712m */
493941b1cdaSStephen M. Cameron 	0x324b103C, /* Smart Array P711m */
494941b1cdaSStephen M. Cameron 	0x3223103C, /* Smart Array P800 */
495941b1cdaSStephen M. Cameron 	0x3234103C, /* Smart Array P400 */
496941b1cdaSStephen M. Cameron 	0x3235103C, /* Smart Array P400i */
497941b1cdaSStephen M. Cameron 	0x3211103C, /* Smart Array E200i */
498941b1cdaSStephen M. Cameron 	0x3212103C, /* Smart Array E200 */
499941b1cdaSStephen M. Cameron 	0x3213103C, /* Smart Array E200i */
500941b1cdaSStephen M. Cameron 	0x3214103C, /* Smart Array E200i */
501941b1cdaSStephen M. Cameron 	0x3215103C, /* Smart Array E200i */
502941b1cdaSStephen M. Cameron 	0x3237103C, /* Smart Array E500 */
503941b1cdaSStephen M. Cameron 	0x323D103C, /* Smart Array P700m */
5047af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
505941b1cdaSStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
506941b1cdaSStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
5075a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
5085a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
5095a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
5105a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
5115a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
5125a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
513941b1cdaSStephen M. Cameron };
514941b1cdaSStephen M. Cameron 
51546380786SStephen M. Cameron /* List of controllers which cannot even be soft reset */
51646380786SStephen M. Cameron static u32 soft_unresettable_controller[] = {
5177af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
5185a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
5195a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
5205a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
5215a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
5225a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
5235a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
52446380786SStephen M. Cameron 	/* Exclude 640x boards.  These are two pci devices in one slot
52546380786SStephen M. Cameron 	 * which share a battery backed cache module.  One controls the
52646380786SStephen M. Cameron 	 * cache, the other accesses the cache through the one that controls
52746380786SStephen M. Cameron 	 * it.  If we reset the one controlling the cache, the other will
52846380786SStephen M. Cameron 	 * likely not be happy.  Just forbid resetting this conjoined mess.
52946380786SStephen M. Cameron 	 * The 640x isn't really supported by hpsa anyway.
53046380786SStephen M. Cameron 	 */
53146380786SStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
53246380786SStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
53346380786SStephen M. Cameron };
53446380786SStephen M. Cameron 
5359b5c48c2SStephen Cameron static u32 needs_abort_tags_swizzled[] = {
5369b5c48c2SStephen Cameron 	0x323D103C, /* Smart Array P700m */
5379b5c48c2SStephen Cameron 	0x324a103C, /* Smart Array P712m */
5389b5c48c2SStephen Cameron 	0x324b103C, /* SmartArray P711m */
5399b5c48c2SStephen Cameron };
5409b5c48c2SStephen Cameron 
5419b5c48c2SStephen Cameron static int board_id_in_array(u32 a[], int nelems, u32 board_id)
542941b1cdaSStephen M. Cameron {
543941b1cdaSStephen M. Cameron 	int i;
544941b1cdaSStephen M. Cameron 
5459b5c48c2SStephen Cameron 	for (i = 0; i < nelems; i++)
5469b5c48c2SStephen Cameron 		if (a[i] == board_id)
547941b1cdaSStephen M. Cameron 			return 1;
5489b5c48c2SStephen Cameron 	return 0;
5499b5c48c2SStephen Cameron }
5509b5c48c2SStephen Cameron 
5519b5c48c2SStephen Cameron static int ctlr_is_hard_resettable(u32 board_id)
5529b5c48c2SStephen Cameron {
5539b5c48c2SStephen Cameron 	return !board_id_in_array(unresettable_controller,
5549b5c48c2SStephen Cameron 			ARRAY_SIZE(unresettable_controller), board_id);
555941b1cdaSStephen M. Cameron }
556941b1cdaSStephen M. Cameron 
55746380786SStephen M. Cameron static int ctlr_is_soft_resettable(u32 board_id)
55846380786SStephen M. Cameron {
5599b5c48c2SStephen Cameron 	return !board_id_in_array(soft_unresettable_controller,
5609b5c48c2SStephen Cameron 			ARRAY_SIZE(soft_unresettable_controller), board_id);
56146380786SStephen M. Cameron }
56246380786SStephen M. Cameron 
56346380786SStephen M. Cameron static int ctlr_is_resettable(u32 board_id)
56446380786SStephen M. Cameron {
56546380786SStephen M. Cameron 	return ctlr_is_hard_resettable(board_id) ||
56646380786SStephen M. Cameron 		ctlr_is_soft_resettable(board_id);
56746380786SStephen M. Cameron }
56846380786SStephen M. Cameron 
5699b5c48c2SStephen Cameron static int ctlr_needs_abort_tags_swizzled(u32 board_id)
5709b5c48c2SStephen Cameron {
5719b5c48c2SStephen Cameron 	return board_id_in_array(needs_abort_tags_swizzled,
5729b5c48c2SStephen Cameron 			ARRAY_SIZE(needs_abort_tags_swizzled), board_id);
5739b5c48c2SStephen Cameron }
5749b5c48c2SStephen Cameron 
575941b1cdaSStephen M. Cameron static ssize_t host_show_resettable(struct device *dev,
576941b1cdaSStephen M. Cameron 	struct device_attribute *attr, char *buf)
577941b1cdaSStephen M. Cameron {
578941b1cdaSStephen M. Cameron 	struct ctlr_info *h;
579941b1cdaSStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
580941b1cdaSStephen M. Cameron 
581941b1cdaSStephen M. Cameron 	h = shost_to_hba(shost);
58246380786SStephen M. Cameron 	return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
583941b1cdaSStephen M. Cameron }
584941b1cdaSStephen M. Cameron 
585edd16368SStephen M. Cameron static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
586edd16368SStephen M. Cameron {
587edd16368SStephen M. Cameron 	return (scsi3addr[3] & 0xC0) == 0x40;
588edd16368SStephen M. Cameron }
589edd16368SStephen M. Cameron 
590f2ef0ce7SRobert Elliott static const char * const raid_label[] = { "0", "4", "1(+0)", "5", "5+1", "6",
591f2ef0ce7SRobert Elliott 	"1(+0)ADM", "UNKNOWN"
592edd16368SStephen M. Cameron };
5936b80b18fSScott Teel #define HPSA_RAID_0	0
5946b80b18fSScott Teel #define HPSA_RAID_4	1
5956b80b18fSScott Teel #define HPSA_RAID_1	2	/* also used for RAID 10 */
5966b80b18fSScott Teel #define HPSA_RAID_5	3	/* also used for RAID 50 */
5976b80b18fSScott Teel #define HPSA_RAID_51	4
5986b80b18fSScott Teel #define HPSA_RAID_6	5	/* also used for RAID 60 */
5996b80b18fSScott Teel #define HPSA_RAID_ADM	6	/* also used for RAID 1+0 ADM */
600edd16368SStephen M. Cameron #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
601edd16368SStephen M. Cameron 
602edd16368SStephen M. Cameron static ssize_t raid_level_show(struct device *dev,
603edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
604edd16368SStephen M. Cameron {
605edd16368SStephen M. Cameron 	ssize_t l = 0;
60682a72c0aSStephen M. Cameron 	unsigned char rlevel;
607edd16368SStephen M. Cameron 	struct ctlr_info *h;
608edd16368SStephen M. Cameron 	struct scsi_device *sdev;
609edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
610edd16368SStephen M. Cameron 	unsigned long flags;
611edd16368SStephen M. Cameron 
612edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
613edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
614edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
615edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
616edd16368SStephen M. Cameron 	if (!hdev) {
617edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
618edd16368SStephen M. Cameron 		return -ENODEV;
619edd16368SStephen M. Cameron 	}
620edd16368SStephen M. Cameron 
621edd16368SStephen M. Cameron 	/* Is this even a logical drive? */
622edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
623edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
624edd16368SStephen M. Cameron 		l = snprintf(buf, PAGE_SIZE, "N/A\n");
625edd16368SStephen M. Cameron 		return l;
626edd16368SStephen M. Cameron 	}
627edd16368SStephen M. Cameron 
628edd16368SStephen M. Cameron 	rlevel = hdev->raid_level;
629edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
63082a72c0aSStephen M. Cameron 	if (rlevel > RAID_UNKNOWN)
631edd16368SStephen M. Cameron 		rlevel = RAID_UNKNOWN;
632edd16368SStephen M. Cameron 	l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
633edd16368SStephen M. Cameron 	return l;
634edd16368SStephen M. Cameron }
635edd16368SStephen M. Cameron 
636edd16368SStephen M. Cameron static ssize_t lunid_show(struct device *dev,
637edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
638edd16368SStephen M. Cameron {
639edd16368SStephen M. Cameron 	struct ctlr_info *h;
640edd16368SStephen M. Cameron 	struct scsi_device *sdev;
641edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
642edd16368SStephen M. Cameron 	unsigned long flags;
643edd16368SStephen M. Cameron 	unsigned char lunid[8];
644edd16368SStephen M. Cameron 
645edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
646edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
647edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
648edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
649edd16368SStephen M. Cameron 	if (!hdev) {
650edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
651edd16368SStephen M. Cameron 		return -ENODEV;
652edd16368SStephen M. Cameron 	}
653edd16368SStephen M. Cameron 	memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
654edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
655edd16368SStephen M. Cameron 	return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
656edd16368SStephen M. Cameron 		lunid[0], lunid[1], lunid[2], lunid[3],
657edd16368SStephen M. Cameron 		lunid[4], lunid[5], lunid[6], lunid[7]);
658edd16368SStephen M. Cameron }
659edd16368SStephen M. Cameron 
660edd16368SStephen M. Cameron static ssize_t unique_id_show(struct device *dev,
661edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
662edd16368SStephen M. Cameron {
663edd16368SStephen M. Cameron 	struct ctlr_info *h;
664edd16368SStephen M. Cameron 	struct scsi_device *sdev;
665edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
666edd16368SStephen M. Cameron 	unsigned long flags;
667edd16368SStephen M. Cameron 	unsigned char sn[16];
668edd16368SStephen M. Cameron 
669edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
670edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
671edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
672edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
673edd16368SStephen M. Cameron 	if (!hdev) {
674edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
675edd16368SStephen M. Cameron 		return -ENODEV;
676edd16368SStephen M. Cameron 	}
677edd16368SStephen M. Cameron 	memcpy(sn, hdev->device_id, sizeof(sn));
678edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
679edd16368SStephen M. Cameron 	return snprintf(buf, 16 * 2 + 2,
680edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X"
681edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X\n",
682edd16368SStephen M. Cameron 			sn[0], sn[1], sn[2], sn[3],
683edd16368SStephen M. Cameron 			sn[4], sn[5], sn[6], sn[7],
684edd16368SStephen M. Cameron 			sn[8], sn[9], sn[10], sn[11],
685edd16368SStephen M. Cameron 			sn[12], sn[13], sn[14], sn[15]);
686edd16368SStephen M. Cameron }
687edd16368SStephen M. Cameron 
688c1988684SScott Teel static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
689c1988684SScott Teel 	     struct device_attribute *attr, char *buf)
690c1988684SScott Teel {
691c1988684SScott Teel 	struct ctlr_info *h;
692c1988684SScott Teel 	struct scsi_device *sdev;
693c1988684SScott Teel 	struct hpsa_scsi_dev_t *hdev;
694c1988684SScott Teel 	unsigned long flags;
695c1988684SScott Teel 	int offload_enabled;
696c1988684SScott Teel 
697c1988684SScott Teel 	sdev = to_scsi_device(dev);
698c1988684SScott Teel 	h = sdev_to_hba(sdev);
699c1988684SScott Teel 	spin_lock_irqsave(&h->lock, flags);
700c1988684SScott Teel 	hdev = sdev->hostdata;
701c1988684SScott Teel 	if (!hdev) {
702c1988684SScott Teel 		spin_unlock_irqrestore(&h->lock, flags);
703c1988684SScott Teel 		return -ENODEV;
704c1988684SScott Teel 	}
705c1988684SScott Teel 	offload_enabled = hdev->offload_enabled;
706c1988684SScott Teel 	spin_unlock_irqrestore(&h->lock, flags);
707c1988684SScott Teel 	return snprintf(buf, 20, "%d\n", offload_enabled);
708c1988684SScott Teel }
709c1988684SScott Teel 
7103f5eac3aSStephen M. Cameron static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
7113f5eac3aSStephen M. Cameron static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
7123f5eac3aSStephen M. Cameron static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
7133f5eac3aSStephen M. Cameron static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
714c1988684SScott Teel static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
715c1988684SScott Teel 			host_show_hp_ssd_smart_path_enabled, NULL);
716da0697bdSScott Teel static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
717da0697bdSScott Teel 		host_show_hp_ssd_smart_path_status,
718da0697bdSScott Teel 		host_store_hp_ssd_smart_path_status);
7192ba8bfc8SStephen M. Cameron static DEVICE_ATTR(raid_offload_debug, S_IWUSR, NULL,
7202ba8bfc8SStephen M. Cameron 			host_store_raid_offload_debug);
7213f5eac3aSStephen M. Cameron static DEVICE_ATTR(firmware_revision, S_IRUGO,
7223f5eac3aSStephen M. Cameron 	host_show_firmware_revision, NULL);
7233f5eac3aSStephen M. Cameron static DEVICE_ATTR(commands_outstanding, S_IRUGO,
7243f5eac3aSStephen M. Cameron 	host_show_commands_outstanding, NULL);
7253f5eac3aSStephen M. Cameron static DEVICE_ATTR(transport_mode, S_IRUGO,
7263f5eac3aSStephen M. Cameron 	host_show_transport_mode, NULL);
727941b1cdaSStephen M. Cameron static DEVICE_ATTR(resettable, S_IRUGO,
728941b1cdaSStephen M. Cameron 	host_show_resettable, NULL);
729e985c58fSStephen Cameron static DEVICE_ATTR(lockup_detected, S_IRUGO,
730e985c58fSStephen Cameron 	host_show_lockup_detected, NULL);
7313f5eac3aSStephen M. Cameron 
7323f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_sdev_attrs[] = {
7333f5eac3aSStephen M. Cameron 	&dev_attr_raid_level,
7343f5eac3aSStephen M. Cameron 	&dev_attr_lunid,
7353f5eac3aSStephen M. Cameron 	&dev_attr_unique_id,
736c1988684SScott Teel 	&dev_attr_hp_ssd_smart_path_enabled,
737e985c58fSStephen Cameron 	&dev_attr_lockup_detected,
7383f5eac3aSStephen M. Cameron 	NULL,
7393f5eac3aSStephen M. Cameron };
7403f5eac3aSStephen M. Cameron 
7413f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_shost_attrs[] = {
7423f5eac3aSStephen M. Cameron 	&dev_attr_rescan,
7433f5eac3aSStephen M. Cameron 	&dev_attr_firmware_revision,
7443f5eac3aSStephen M. Cameron 	&dev_attr_commands_outstanding,
7453f5eac3aSStephen M. Cameron 	&dev_attr_transport_mode,
746941b1cdaSStephen M. Cameron 	&dev_attr_resettable,
747da0697bdSScott Teel 	&dev_attr_hp_ssd_smart_path_status,
7482ba8bfc8SStephen M. Cameron 	&dev_attr_raid_offload_debug,
7493f5eac3aSStephen M. Cameron 	NULL,
7503f5eac3aSStephen M. Cameron };
7513f5eac3aSStephen M. Cameron 
75241ce4c35SStephen Cameron #define HPSA_NRESERVED_CMDS	(HPSA_CMDS_RESERVED_FOR_ABORTS + \
75341ce4c35SStephen Cameron 		HPSA_CMDS_RESERVED_FOR_DRIVER + HPSA_MAX_CONCURRENT_PASSTHRUS)
75441ce4c35SStephen Cameron 
7553f5eac3aSStephen M. Cameron static struct scsi_host_template hpsa_driver_template = {
7563f5eac3aSStephen M. Cameron 	.module			= THIS_MODULE,
757f79cfec6SStephen M. Cameron 	.name			= HPSA,
758f79cfec6SStephen M. Cameron 	.proc_name		= HPSA,
7593f5eac3aSStephen M. Cameron 	.queuecommand		= hpsa_scsi_queue_command,
7603f5eac3aSStephen M. Cameron 	.scan_start		= hpsa_scan_start,
7613f5eac3aSStephen M. Cameron 	.scan_finished		= hpsa_scan_finished,
7627c0a0229SDon Brace 	.change_queue_depth	= hpsa_change_queue_depth,
7633f5eac3aSStephen M. Cameron 	.this_id		= -1,
7643f5eac3aSStephen M. Cameron 	.use_clustering		= ENABLE_CLUSTERING,
76575167d2cSStephen M. Cameron 	.eh_abort_handler	= hpsa_eh_abort_handler,
7663f5eac3aSStephen M. Cameron 	.eh_device_reset_handler = hpsa_eh_device_reset_handler,
7673f5eac3aSStephen M. Cameron 	.ioctl			= hpsa_ioctl,
7683f5eac3aSStephen M. Cameron 	.slave_alloc		= hpsa_slave_alloc,
76941ce4c35SStephen Cameron 	.slave_configure	= hpsa_slave_configure,
7703f5eac3aSStephen M. Cameron 	.slave_destroy		= hpsa_slave_destroy,
7713f5eac3aSStephen M. Cameron #ifdef CONFIG_COMPAT
7723f5eac3aSStephen M. Cameron 	.compat_ioctl		= hpsa_compat_ioctl,
7733f5eac3aSStephen M. Cameron #endif
7743f5eac3aSStephen M. Cameron 	.sdev_attrs = hpsa_sdev_attrs,
7753f5eac3aSStephen M. Cameron 	.shost_attrs = hpsa_shost_attrs,
776c0d6a4d1SStephen M. Cameron 	.max_sectors = 8192,
77754b2b50cSMartin K. Petersen 	.no_write_same = 1,
7783f5eac3aSStephen M. Cameron };
7793f5eac3aSStephen M. Cameron 
780254f796bSMatt Gates static inline u32 next_command(struct ctlr_info *h, u8 q)
7813f5eac3aSStephen M. Cameron {
7823f5eac3aSStephen M. Cameron 	u32 a;
783072b0518SStephen M. Cameron 	struct reply_queue_buffer *rq = &h->reply_queue[q];
7843f5eac3aSStephen M. Cameron 
785e1f7de0cSMatt Gates 	if (h->transMethod & CFGTBL_Trans_io_accel1)
786e1f7de0cSMatt Gates 		return h->access.command_completed(h, q);
787e1f7de0cSMatt Gates 
7883f5eac3aSStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
789254f796bSMatt Gates 		return h->access.command_completed(h, q);
7903f5eac3aSStephen M. Cameron 
791254f796bSMatt Gates 	if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
792254f796bSMatt Gates 		a = rq->head[rq->current_entry];
793254f796bSMatt Gates 		rq->current_entry++;
7940cbf768eSStephen M. Cameron 		atomic_dec(&h->commands_outstanding);
7953f5eac3aSStephen M. Cameron 	} else {
7963f5eac3aSStephen M. Cameron 		a = FIFO_EMPTY;
7973f5eac3aSStephen M. Cameron 	}
7983f5eac3aSStephen M. Cameron 	/* Check for wraparound */
799254f796bSMatt Gates 	if (rq->current_entry == h->max_commands) {
800254f796bSMatt Gates 		rq->current_entry = 0;
801254f796bSMatt Gates 		rq->wraparound ^= 1;
8023f5eac3aSStephen M. Cameron 	}
8033f5eac3aSStephen M. Cameron 	return a;
8043f5eac3aSStephen M. Cameron }
8053f5eac3aSStephen M. Cameron 
806c349775eSScott Teel /*
807c349775eSScott Teel  * There are some special bits in the bus address of the
808c349775eSScott Teel  * command that we have to set for the controller to know
809c349775eSScott Teel  * how to process the command:
810c349775eSScott Teel  *
811c349775eSScott Teel  * Normal performant mode:
812c349775eSScott Teel  * bit 0: 1 means performant mode, 0 means simple mode.
813c349775eSScott Teel  * bits 1-3 = block fetch table entry
814c349775eSScott Teel  * bits 4-6 = command type (== 0)
815c349775eSScott Teel  *
816c349775eSScott Teel  * ioaccel1 mode:
817c349775eSScott Teel  * bit 0 = "performant mode" bit.
818c349775eSScott Teel  * bits 1-3 = block fetch table entry
819c349775eSScott Teel  * bits 4-6 = command type (== 110)
820c349775eSScott Teel  * (command type is needed because ioaccel1 mode
821c349775eSScott Teel  * commands are submitted through the same register as normal
822c349775eSScott Teel  * mode commands, so this is how the controller knows whether
823c349775eSScott Teel  * the command is normal mode or ioaccel1 mode.)
824c349775eSScott Teel  *
825c349775eSScott Teel  * ioaccel2 mode:
826c349775eSScott Teel  * bit 0 = "performant mode" bit.
827c349775eSScott Teel  * bits 1-4 = block fetch table entry (note extra bit)
828c349775eSScott Teel  * bits 4-6 = not needed, because ioaccel2 mode has
829c349775eSScott Teel  * a separate special register for submitting commands.
830c349775eSScott Teel  */
831c349775eSScott Teel 
83225163bd5SWebb Scales /*
83325163bd5SWebb Scales  * set_performant_mode: Modify the tag for cciss performant
8343f5eac3aSStephen M. Cameron  * set bit 0 for pull model, bits 3-1 for block fetch
8353f5eac3aSStephen M. Cameron  * register number
8363f5eac3aSStephen M. Cameron  */
83725163bd5SWebb Scales #define DEFAULT_REPLY_QUEUE (-1)
83825163bd5SWebb Scales static void set_performant_mode(struct ctlr_info *h, struct CommandList *c,
83925163bd5SWebb Scales 					int reply_queue)
8403f5eac3aSStephen M. Cameron {
841254f796bSMatt Gates 	if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
8423f5eac3aSStephen M. Cameron 		c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
84325163bd5SWebb Scales 		if (unlikely(!h->msix_vector))
84425163bd5SWebb Scales 			return;
84525163bd5SWebb Scales 		if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
846254f796bSMatt Gates 			c->Header.ReplyQueue =
847804a5cb5SJohn Kacur 				raw_smp_processor_id() % h->nreply_queues;
84825163bd5SWebb Scales 		else
84925163bd5SWebb Scales 			c->Header.ReplyQueue = reply_queue % h->nreply_queues;
850254f796bSMatt Gates 	}
8513f5eac3aSStephen M. Cameron }
8523f5eac3aSStephen M. Cameron 
853c349775eSScott Teel static void set_ioaccel1_performant_mode(struct ctlr_info *h,
85425163bd5SWebb Scales 						struct CommandList *c,
85525163bd5SWebb Scales 						int reply_queue)
856c349775eSScott Teel {
857c349775eSScott Teel 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
858c349775eSScott Teel 
85925163bd5SWebb Scales 	/*
86025163bd5SWebb Scales 	 * Tell the controller to post the reply to the queue for this
861c349775eSScott Teel 	 * processor.  This seems to give the best I/O throughput.
862c349775eSScott Teel 	 */
86325163bd5SWebb Scales 	if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
864c349775eSScott Teel 		cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
86525163bd5SWebb Scales 	else
86625163bd5SWebb Scales 		cp->ReplyQueue = reply_queue % h->nreply_queues;
86725163bd5SWebb Scales 	/*
86825163bd5SWebb Scales 	 * Set the bits in the address sent down to include:
869c349775eSScott Teel 	 *  - performant mode bit (bit 0)
870c349775eSScott Teel 	 *  - pull count (bits 1-3)
871c349775eSScott Teel 	 *  - command type (bits 4-6)
872c349775eSScott Teel 	 */
873c349775eSScott Teel 	c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
874c349775eSScott Teel 					IOACCEL1_BUSADDR_CMDTYPE;
875c349775eSScott Teel }
876c349775eSScott Teel 
8778be986ccSStephen Cameron static void set_ioaccel2_tmf_performant_mode(struct ctlr_info *h,
8788be986ccSStephen Cameron 						struct CommandList *c,
8798be986ccSStephen Cameron 						int reply_queue)
8808be986ccSStephen Cameron {
8818be986ccSStephen Cameron 	struct hpsa_tmf_struct *cp = (struct hpsa_tmf_struct *)
8828be986ccSStephen Cameron 		&h->ioaccel2_cmd_pool[c->cmdindex];
8838be986ccSStephen Cameron 
8848be986ccSStephen Cameron 	/* Tell the controller to post the reply to the queue for this
8858be986ccSStephen Cameron 	 * processor.  This seems to give the best I/O throughput.
8868be986ccSStephen Cameron 	 */
8878be986ccSStephen Cameron 	if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
8888be986ccSStephen Cameron 		cp->reply_queue = smp_processor_id() % h->nreply_queues;
8898be986ccSStephen Cameron 	else
8908be986ccSStephen Cameron 		cp->reply_queue = reply_queue % h->nreply_queues;
8918be986ccSStephen Cameron 	/* Set the bits in the address sent down to include:
8928be986ccSStephen Cameron 	 *  - performant mode bit not used in ioaccel mode 2
8938be986ccSStephen Cameron 	 *  - pull count (bits 0-3)
8948be986ccSStephen Cameron 	 *  - command type isn't needed for ioaccel2
8958be986ccSStephen Cameron 	 */
8968be986ccSStephen Cameron 	c->busaddr |= h->ioaccel2_blockFetchTable[0];
8978be986ccSStephen Cameron }
8988be986ccSStephen Cameron 
899c349775eSScott Teel static void set_ioaccel2_performant_mode(struct ctlr_info *h,
90025163bd5SWebb Scales 						struct CommandList *c,
90125163bd5SWebb Scales 						int reply_queue)
902c349775eSScott Teel {
903c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
904c349775eSScott Teel 
90525163bd5SWebb Scales 	/*
90625163bd5SWebb Scales 	 * Tell the controller to post the reply to the queue for this
907c349775eSScott Teel 	 * processor.  This seems to give the best I/O throughput.
908c349775eSScott Teel 	 */
90925163bd5SWebb Scales 	if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
910c349775eSScott Teel 		cp->reply_queue = smp_processor_id() % h->nreply_queues;
91125163bd5SWebb Scales 	else
91225163bd5SWebb Scales 		cp->reply_queue = reply_queue % h->nreply_queues;
91325163bd5SWebb Scales 	/*
91425163bd5SWebb Scales 	 * Set the bits in the address sent down to include:
915c349775eSScott Teel 	 *  - performant mode bit not used in ioaccel mode 2
916c349775eSScott Teel 	 *  - pull count (bits 0-3)
917c349775eSScott Teel 	 *  - command type isn't needed for ioaccel2
918c349775eSScott Teel 	 */
919c349775eSScott Teel 	c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
920c349775eSScott Teel }
921c349775eSScott Teel 
922e85c5974SStephen M. Cameron static int is_firmware_flash_cmd(u8 *cdb)
923e85c5974SStephen M. Cameron {
924e85c5974SStephen M. Cameron 	return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
925e85c5974SStephen M. Cameron }
926e85c5974SStephen M. Cameron 
927e85c5974SStephen M. Cameron /*
928e85c5974SStephen M. Cameron  * During firmware flash, the heartbeat register may not update as frequently
929e85c5974SStephen M. Cameron  * as it should.  So we dial down lockup detection during firmware flash. and
930e85c5974SStephen M. Cameron  * dial it back up when firmware flash completes.
931e85c5974SStephen M. Cameron  */
932e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
933e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
934e85c5974SStephen M. Cameron static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
935e85c5974SStephen M. Cameron 		struct CommandList *c)
936e85c5974SStephen M. Cameron {
937e85c5974SStephen M. Cameron 	if (!is_firmware_flash_cmd(c->Request.CDB))
938e85c5974SStephen M. Cameron 		return;
939e85c5974SStephen M. Cameron 	atomic_inc(&h->firmware_flash_in_progress);
940e85c5974SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
941e85c5974SStephen M. Cameron }
942e85c5974SStephen M. Cameron 
943e85c5974SStephen M. Cameron static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
944e85c5974SStephen M. Cameron 		struct CommandList *c)
945e85c5974SStephen M. Cameron {
946e85c5974SStephen M. Cameron 	if (is_firmware_flash_cmd(c->Request.CDB) &&
947e85c5974SStephen M. Cameron 		atomic_dec_and_test(&h->firmware_flash_in_progress))
948e85c5974SStephen M. Cameron 		h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
949e85c5974SStephen M. Cameron }
950e85c5974SStephen M. Cameron 
95125163bd5SWebb Scales static void __enqueue_cmd_and_start_io(struct ctlr_info *h,
95225163bd5SWebb Scales 	struct CommandList *c, int reply_queue)
9533f5eac3aSStephen M. Cameron {
954c05e8866SStephen Cameron 	dial_down_lockup_detection_during_fw_flash(h, c);
955c05e8866SStephen Cameron 	atomic_inc(&h->commands_outstanding);
956c349775eSScott Teel 	switch (c->cmd_type) {
957c349775eSScott Teel 	case CMD_IOACCEL1:
95825163bd5SWebb Scales 		set_ioaccel1_performant_mode(h, c, reply_queue);
959c05e8866SStephen Cameron 		writel(c->busaddr, h->vaddr + SA5_REQUEST_PORT_OFFSET);
960c349775eSScott Teel 		break;
961c349775eSScott Teel 	case CMD_IOACCEL2:
96225163bd5SWebb Scales 		set_ioaccel2_performant_mode(h, c, reply_queue);
963c05e8866SStephen Cameron 		writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
964c349775eSScott Teel 		break;
9658be986ccSStephen Cameron 	case IOACCEL2_TMF:
9668be986ccSStephen Cameron 		set_ioaccel2_tmf_performant_mode(h, c, reply_queue);
9678be986ccSStephen Cameron 		writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
9688be986ccSStephen Cameron 		break;
969c349775eSScott Teel 	default:
97025163bd5SWebb Scales 		set_performant_mode(h, c, reply_queue);
971f2405db8SDon Brace 		h->access.submit_command(h, c);
9723f5eac3aSStephen M. Cameron 	}
973c05e8866SStephen Cameron }
9743f5eac3aSStephen M. Cameron 
975a58e7e53SWebb Scales static void enqueue_cmd_and_start_io(struct ctlr_info *h, struct CommandList *c)
97625163bd5SWebb Scales {
977a58e7e53SWebb Scales 	if (unlikely(c->abort_pending))
978a58e7e53SWebb Scales 		return finish_cmd(c);
979a58e7e53SWebb Scales 
98025163bd5SWebb Scales 	__enqueue_cmd_and_start_io(h, c, DEFAULT_REPLY_QUEUE);
98125163bd5SWebb Scales }
98225163bd5SWebb Scales 
9833f5eac3aSStephen M. Cameron static inline int is_hba_lunid(unsigned char scsi3addr[])
9843f5eac3aSStephen M. Cameron {
9853f5eac3aSStephen M. Cameron 	return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
9863f5eac3aSStephen M. Cameron }
9873f5eac3aSStephen M. Cameron 
9883f5eac3aSStephen M. Cameron static inline int is_scsi_rev_5(struct ctlr_info *h)
9893f5eac3aSStephen M. Cameron {
9903f5eac3aSStephen M. Cameron 	if (!h->hba_inquiry_data)
9913f5eac3aSStephen M. Cameron 		return 0;
9923f5eac3aSStephen M. Cameron 	if ((h->hba_inquiry_data[2] & 0x07) == 5)
9933f5eac3aSStephen M. Cameron 		return 1;
9943f5eac3aSStephen M. Cameron 	return 0;
9953f5eac3aSStephen M. Cameron }
9963f5eac3aSStephen M. Cameron 
997edd16368SStephen M. Cameron static int hpsa_find_target_lun(struct ctlr_info *h,
998edd16368SStephen M. Cameron 	unsigned char scsi3addr[], int bus, int *target, int *lun)
999edd16368SStephen M. Cameron {
1000edd16368SStephen M. Cameron 	/* finds an unused bus, target, lun for a new physical device
1001edd16368SStephen M. Cameron 	 * assumes h->devlock is held
1002edd16368SStephen M. Cameron 	 */
1003edd16368SStephen M. Cameron 	int i, found = 0;
1004cfe5badcSScott Teel 	DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
1005edd16368SStephen M. Cameron 
1006263d9401SAkinobu Mita 	bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
1007edd16368SStephen M. Cameron 
1008edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1009edd16368SStephen M. Cameron 		if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
1010263d9401SAkinobu Mita 			__set_bit(h->dev[i]->target, lun_taken);
1011edd16368SStephen M. Cameron 	}
1012edd16368SStephen M. Cameron 
1013263d9401SAkinobu Mita 	i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
1014263d9401SAkinobu Mita 	if (i < HPSA_MAX_DEVICES) {
1015edd16368SStephen M. Cameron 		/* *bus = 1; */
1016edd16368SStephen M. Cameron 		*target = i;
1017edd16368SStephen M. Cameron 		*lun = 0;
1018edd16368SStephen M. Cameron 		found = 1;
1019edd16368SStephen M. Cameron 	}
1020edd16368SStephen M. Cameron 	return !found;
1021edd16368SStephen M. Cameron }
1022edd16368SStephen M. Cameron 
10230d96ef5fSWebb Scales static inline void hpsa_show_dev_msg(const char *level, struct ctlr_info *h,
10240d96ef5fSWebb Scales 	struct hpsa_scsi_dev_t *dev, char *description)
10250d96ef5fSWebb Scales {
10260d96ef5fSWebb Scales 	dev_printk(level, &h->pdev->dev,
10270d96ef5fSWebb Scales 			"scsi %d:%d:%d:%d: %s %s %.8s %.16s RAID-%s SSDSmartPathCap%c En%c Exp=%d\n",
10280d96ef5fSWebb Scales 			h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
10290d96ef5fSWebb Scales 			description,
10300d96ef5fSWebb Scales 			scsi_device_type(dev->devtype),
10310d96ef5fSWebb Scales 			dev->vendor,
10320d96ef5fSWebb Scales 			dev->model,
10330d96ef5fSWebb Scales 			dev->raid_level > RAID_UNKNOWN ?
10340d96ef5fSWebb Scales 				"RAID-?" : raid_label[dev->raid_level],
10350d96ef5fSWebb Scales 			dev->offload_config ? '+' : '-',
10360d96ef5fSWebb Scales 			dev->offload_enabled ? '+' : '-',
10370d96ef5fSWebb Scales 			dev->expose_state);
10380d96ef5fSWebb Scales }
10390d96ef5fSWebb Scales 
1040edd16368SStephen M. Cameron /* Add an entry into h->dev[] array. */
1041edd16368SStephen M. Cameron static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
1042edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *device,
1043edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *added[], int *nadded)
1044edd16368SStephen M. Cameron {
1045edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
1046edd16368SStephen M. Cameron 	int n = h->ndevices;
1047edd16368SStephen M. Cameron 	int i;
1048edd16368SStephen M. Cameron 	unsigned char addr1[8], addr2[8];
1049edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1050edd16368SStephen M. Cameron 
1051cfe5badcSScott Teel 	if (n >= HPSA_MAX_DEVICES) {
1052edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "too many devices, some will be "
1053edd16368SStephen M. Cameron 			"inaccessible.\n");
1054edd16368SStephen M. Cameron 		return -1;
1055edd16368SStephen M. Cameron 	}
1056edd16368SStephen M. Cameron 
1057edd16368SStephen M. Cameron 	/* physical devices do not have lun or target assigned until now. */
1058edd16368SStephen M. Cameron 	if (device->lun != -1)
1059edd16368SStephen M. Cameron 		/* Logical device, lun is already assigned. */
1060edd16368SStephen M. Cameron 		goto lun_assigned;
1061edd16368SStephen M. Cameron 
1062edd16368SStephen M. Cameron 	/* If this device a non-zero lun of a multi-lun device
1063edd16368SStephen M. Cameron 	 * byte 4 of the 8-byte LUN addr will contain the logical
10642b08b3e9SDon Brace 	 * unit no, zero otherwise.
1065edd16368SStephen M. Cameron 	 */
1066edd16368SStephen M. Cameron 	if (device->scsi3addr[4] == 0) {
1067edd16368SStephen M. Cameron 		/* This is not a non-zero lun of a multi-lun device */
1068edd16368SStephen M. Cameron 		if (hpsa_find_target_lun(h, device->scsi3addr,
1069edd16368SStephen M. Cameron 			device->bus, &device->target, &device->lun) != 0)
1070edd16368SStephen M. Cameron 			return -1;
1071edd16368SStephen M. Cameron 		goto lun_assigned;
1072edd16368SStephen M. Cameron 	}
1073edd16368SStephen M. Cameron 
1074edd16368SStephen M. Cameron 	/* This is a non-zero lun of a multi-lun device.
1075edd16368SStephen M. Cameron 	 * Search through our list and find the device which
1076edd16368SStephen M. Cameron 	 * has the same 8 byte LUN address, excepting byte 4.
1077edd16368SStephen M. Cameron 	 * Assign the same bus and target for this new LUN.
1078edd16368SStephen M. Cameron 	 * Use the logical unit number from the firmware.
1079edd16368SStephen M. Cameron 	 */
1080edd16368SStephen M. Cameron 	memcpy(addr1, device->scsi3addr, 8);
1081edd16368SStephen M. Cameron 	addr1[4] = 0;
1082edd16368SStephen M. Cameron 	for (i = 0; i < n; i++) {
1083edd16368SStephen M. Cameron 		sd = h->dev[i];
1084edd16368SStephen M. Cameron 		memcpy(addr2, sd->scsi3addr, 8);
1085edd16368SStephen M. Cameron 		addr2[4] = 0;
1086edd16368SStephen M. Cameron 		/* differ only in byte 4? */
1087edd16368SStephen M. Cameron 		if (memcmp(addr1, addr2, 8) == 0) {
1088edd16368SStephen M. Cameron 			device->bus = sd->bus;
1089edd16368SStephen M. Cameron 			device->target = sd->target;
1090edd16368SStephen M. Cameron 			device->lun = device->scsi3addr[4];
1091edd16368SStephen M. Cameron 			break;
1092edd16368SStephen M. Cameron 		}
1093edd16368SStephen M. Cameron 	}
1094edd16368SStephen M. Cameron 	if (device->lun == -1) {
1095edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
1096edd16368SStephen M. Cameron 			" suspect firmware bug or unsupported hardware "
1097edd16368SStephen M. Cameron 			"configuration.\n");
1098edd16368SStephen M. Cameron 			return -1;
1099edd16368SStephen M. Cameron 	}
1100edd16368SStephen M. Cameron 
1101edd16368SStephen M. Cameron lun_assigned:
1102edd16368SStephen M. Cameron 
1103edd16368SStephen M. Cameron 	h->dev[n] = device;
1104edd16368SStephen M. Cameron 	h->ndevices++;
1105edd16368SStephen M. Cameron 	added[*nadded] = device;
1106edd16368SStephen M. Cameron 	(*nadded)++;
11070d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_INFO, h, device,
11080d96ef5fSWebb Scales 		device->expose_state & HPSA_SCSI_ADD ? "added" : "masked");
1109a473d86cSRobert Elliott 	device->offload_to_be_enabled = device->offload_enabled;
1110a473d86cSRobert Elliott 	device->offload_enabled = 0;
1111edd16368SStephen M. Cameron 	return 0;
1112edd16368SStephen M. Cameron }
1113edd16368SStephen M. Cameron 
1114bd9244f7SScott Teel /* Update an entry in h->dev[] array. */
1115bd9244f7SScott Teel static void hpsa_scsi_update_entry(struct ctlr_info *h, int hostno,
1116bd9244f7SScott Teel 	int entry, struct hpsa_scsi_dev_t *new_entry)
1117bd9244f7SScott Teel {
1118a473d86cSRobert Elliott 	int offload_enabled;
1119bd9244f7SScott Teel 	/* assumes h->devlock is held */
1120bd9244f7SScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
1121bd9244f7SScott Teel 
1122bd9244f7SScott Teel 	/* Raid level changed. */
1123bd9244f7SScott Teel 	h->dev[entry]->raid_level = new_entry->raid_level;
1124250fb125SStephen M. Cameron 
112503383736SDon Brace 	/* Raid offload parameters changed.  Careful about the ordering. */
112603383736SDon Brace 	if (new_entry->offload_config && new_entry->offload_enabled) {
112703383736SDon Brace 		/*
112803383736SDon Brace 		 * if drive is newly offload_enabled, we want to copy the
112903383736SDon Brace 		 * raid map data first.  If previously offload_enabled and
113003383736SDon Brace 		 * offload_config were set, raid map data had better be
113103383736SDon Brace 		 * the same as it was before.  if raid map data is changed
113203383736SDon Brace 		 * then it had better be the case that
113303383736SDon Brace 		 * h->dev[entry]->offload_enabled is currently 0.
113403383736SDon Brace 		 */
11359fb0de2dSStephen M. Cameron 		h->dev[entry]->raid_map = new_entry->raid_map;
113603383736SDon Brace 		h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
113703383736SDon Brace 	}
1138a3144e0bSJoe Handzik 	if (new_entry->hba_ioaccel_enabled) {
1139a3144e0bSJoe Handzik 		h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
1140a3144e0bSJoe Handzik 		wmb(); /* set ioaccel_handle *before* hba_ioaccel_enabled */
1141a3144e0bSJoe Handzik 	}
1142a3144e0bSJoe Handzik 	h->dev[entry]->hba_ioaccel_enabled = new_entry->hba_ioaccel_enabled;
114303383736SDon Brace 	h->dev[entry]->offload_config = new_entry->offload_config;
114403383736SDon Brace 	h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
114503383736SDon Brace 	h->dev[entry]->queue_depth = new_entry->queue_depth;
1146250fb125SStephen M. Cameron 
114741ce4c35SStephen Cameron 	/*
114841ce4c35SStephen Cameron 	 * We can turn off ioaccel offload now, but need to delay turning
114941ce4c35SStephen Cameron 	 * it on until we can update h->dev[entry]->phys_disk[], but we
115041ce4c35SStephen Cameron 	 * can't do that until all the devices are updated.
115141ce4c35SStephen Cameron 	 */
115241ce4c35SStephen Cameron 	h->dev[entry]->offload_to_be_enabled = new_entry->offload_enabled;
115341ce4c35SStephen Cameron 	if (!new_entry->offload_enabled)
115441ce4c35SStephen Cameron 		h->dev[entry]->offload_enabled = 0;
115541ce4c35SStephen Cameron 
1156a473d86cSRobert Elliott 	offload_enabled = h->dev[entry]->offload_enabled;
1157a473d86cSRobert Elliott 	h->dev[entry]->offload_enabled = h->dev[entry]->offload_to_be_enabled;
11580d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_INFO, h, h->dev[entry], "updated");
1159a473d86cSRobert Elliott 	h->dev[entry]->offload_enabled = offload_enabled;
1160bd9244f7SScott Teel }
1161bd9244f7SScott Teel 
11622a8ccf31SStephen M. Cameron /* Replace an entry from h->dev[] array. */
11632a8ccf31SStephen M. Cameron static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
11642a8ccf31SStephen M. Cameron 	int entry, struct hpsa_scsi_dev_t *new_entry,
11652a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *added[], int *nadded,
11662a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
11672a8ccf31SStephen M. Cameron {
11682a8ccf31SStephen M. Cameron 	/* assumes h->devlock is held */
1169cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
11702a8ccf31SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
11712a8ccf31SStephen M. Cameron 	(*nremoved)++;
117201350d05SStephen M. Cameron 
117301350d05SStephen M. Cameron 	/*
117401350d05SStephen M. Cameron 	 * New physical devices won't have target/lun assigned yet
117501350d05SStephen M. Cameron 	 * so we need to preserve the values in the slot we are replacing.
117601350d05SStephen M. Cameron 	 */
117701350d05SStephen M. Cameron 	if (new_entry->target == -1) {
117801350d05SStephen M. Cameron 		new_entry->target = h->dev[entry]->target;
117901350d05SStephen M. Cameron 		new_entry->lun = h->dev[entry]->lun;
118001350d05SStephen M. Cameron 	}
118101350d05SStephen M. Cameron 
11822a8ccf31SStephen M. Cameron 	h->dev[entry] = new_entry;
11832a8ccf31SStephen M. Cameron 	added[*nadded] = new_entry;
11842a8ccf31SStephen M. Cameron 	(*nadded)++;
11850d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_INFO, h, new_entry, "replaced");
1186a473d86cSRobert Elliott 	new_entry->offload_to_be_enabled = new_entry->offload_enabled;
1187a473d86cSRobert Elliott 	new_entry->offload_enabled = 0;
11882a8ccf31SStephen M. Cameron }
11892a8ccf31SStephen M. Cameron 
1190edd16368SStephen M. Cameron /* Remove an entry from h->dev[] array. */
1191edd16368SStephen M. Cameron static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
1192edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
1193edd16368SStephen M. Cameron {
1194edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
1195edd16368SStephen M. Cameron 	int i;
1196edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1197edd16368SStephen M. Cameron 
1198cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
1199edd16368SStephen M. Cameron 
1200edd16368SStephen M. Cameron 	sd = h->dev[entry];
1201edd16368SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
1202edd16368SStephen M. Cameron 	(*nremoved)++;
1203edd16368SStephen M. Cameron 
1204edd16368SStephen M. Cameron 	for (i = entry; i < h->ndevices-1; i++)
1205edd16368SStephen M. Cameron 		h->dev[i] = h->dev[i+1];
1206edd16368SStephen M. Cameron 	h->ndevices--;
12070d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_INFO, h, sd, "removed");
1208edd16368SStephen M. Cameron }
1209edd16368SStephen M. Cameron 
1210edd16368SStephen M. Cameron #define SCSI3ADDR_EQ(a, b) ( \
1211edd16368SStephen M. Cameron 	(a)[7] == (b)[7] && \
1212edd16368SStephen M. Cameron 	(a)[6] == (b)[6] && \
1213edd16368SStephen M. Cameron 	(a)[5] == (b)[5] && \
1214edd16368SStephen M. Cameron 	(a)[4] == (b)[4] && \
1215edd16368SStephen M. Cameron 	(a)[3] == (b)[3] && \
1216edd16368SStephen M. Cameron 	(a)[2] == (b)[2] && \
1217edd16368SStephen M. Cameron 	(a)[1] == (b)[1] && \
1218edd16368SStephen M. Cameron 	(a)[0] == (b)[0])
1219edd16368SStephen M. Cameron 
1220edd16368SStephen M. Cameron static void fixup_botched_add(struct ctlr_info *h,
1221edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *added)
1222edd16368SStephen M. Cameron {
1223edd16368SStephen M. Cameron 	/* called when scsi_add_device fails in order to re-adjust
1224edd16368SStephen M. Cameron 	 * h->dev[] to match the mid layer's view.
1225edd16368SStephen M. Cameron 	 */
1226edd16368SStephen M. Cameron 	unsigned long flags;
1227edd16368SStephen M. Cameron 	int i, j;
1228edd16368SStephen M. Cameron 
1229edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
1230edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1231edd16368SStephen M. Cameron 		if (h->dev[i] == added) {
1232edd16368SStephen M. Cameron 			for (j = i; j < h->ndevices-1; j++)
1233edd16368SStephen M. Cameron 				h->dev[j] = h->dev[j+1];
1234edd16368SStephen M. Cameron 			h->ndevices--;
1235edd16368SStephen M. Cameron 			break;
1236edd16368SStephen M. Cameron 		}
1237edd16368SStephen M. Cameron 	}
1238edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
1239edd16368SStephen M. Cameron 	kfree(added);
1240edd16368SStephen M. Cameron }
1241edd16368SStephen M. Cameron 
1242edd16368SStephen M. Cameron static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
1243edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev2)
1244edd16368SStephen M. Cameron {
1245edd16368SStephen M. Cameron 	/* we compare everything except lun and target as these
1246edd16368SStephen M. Cameron 	 * are not yet assigned.  Compare parts likely
1247edd16368SStephen M. Cameron 	 * to differ first
1248edd16368SStephen M. Cameron 	 */
1249edd16368SStephen M. Cameron 	if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
1250edd16368SStephen M. Cameron 		sizeof(dev1->scsi3addr)) != 0)
1251edd16368SStephen M. Cameron 		return 0;
1252edd16368SStephen M. Cameron 	if (memcmp(dev1->device_id, dev2->device_id,
1253edd16368SStephen M. Cameron 		sizeof(dev1->device_id)) != 0)
1254edd16368SStephen M. Cameron 		return 0;
1255edd16368SStephen M. Cameron 	if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
1256edd16368SStephen M. Cameron 		return 0;
1257edd16368SStephen M. Cameron 	if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
1258edd16368SStephen M. Cameron 		return 0;
1259edd16368SStephen M. Cameron 	if (dev1->devtype != dev2->devtype)
1260edd16368SStephen M. Cameron 		return 0;
1261edd16368SStephen M. Cameron 	if (dev1->bus != dev2->bus)
1262edd16368SStephen M. Cameron 		return 0;
1263edd16368SStephen M. Cameron 	return 1;
1264edd16368SStephen M. Cameron }
1265edd16368SStephen M. Cameron 
1266bd9244f7SScott Teel static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
1267bd9244f7SScott Teel 	struct hpsa_scsi_dev_t *dev2)
1268bd9244f7SScott Teel {
1269bd9244f7SScott Teel 	/* Device attributes that can change, but don't mean
1270bd9244f7SScott Teel 	 * that the device is a different device, nor that the OS
1271bd9244f7SScott Teel 	 * needs to be told anything about the change.
1272bd9244f7SScott Teel 	 */
1273bd9244f7SScott Teel 	if (dev1->raid_level != dev2->raid_level)
1274bd9244f7SScott Teel 		return 1;
1275250fb125SStephen M. Cameron 	if (dev1->offload_config != dev2->offload_config)
1276250fb125SStephen M. Cameron 		return 1;
1277250fb125SStephen M. Cameron 	if (dev1->offload_enabled != dev2->offload_enabled)
1278250fb125SStephen M. Cameron 		return 1;
127903383736SDon Brace 	if (dev1->queue_depth != dev2->queue_depth)
128003383736SDon Brace 		return 1;
1281bd9244f7SScott Teel 	return 0;
1282bd9244f7SScott Teel }
1283bd9244f7SScott Teel 
1284edd16368SStephen M. Cameron /* Find needle in haystack.  If exact match found, return DEVICE_SAME,
1285edd16368SStephen M. Cameron  * and return needle location in *index.  If scsi3addr matches, but not
1286edd16368SStephen M. Cameron  * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
1287bd9244f7SScott Teel  * location in *index.
1288bd9244f7SScott Teel  * In the case of a minor device attribute change, such as RAID level, just
1289bd9244f7SScott Teel  * return DEVICE_UPDATED, along with the updated device's location in index.
1290bd9244f7SScott Teel  * If needle not found, return DEVICE_NOT_FOUND.
1291edd16368SStephen M. Cameron  */
1292edd16368SStephen M. Cameron static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
1293edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *haystack[], int haystack_size,
1294edd16368SStephen M. Cameron 	int *index)
1295edd16368SStephen M. Cameron {
1296edd16368SStephen M. Cameron 	int i;
1297edd16368SStephen M. Cameron #define DEVICE_NOT_FOUND 0
1298edd16368SStephen M. Cameron #define DEVICE_CHANGED 1
1299edd16368SStephen M. Cameron #define DEVICE_SAME 2
1300bd9244f7SScott Teel #define DEVICE_UPDATED 3
1301edd16368SStephen M. Cameron 	for (i = 0; i < haystack_size; i++) {
130223231048SStephen M. Cameron 		if (haystack[i] == NULL) /* previously removed. */
130323231048SStephen M. Cameron 			continue;
1304edd16368SStephen M. Cameron 		if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
1305edd16368SStephen M. Cameron 			*index = i;
1306bd9244f7SScott Teel 			if (device_is_the_same(needle, haystack[i])) {
1307bd9244f7SScott Teel 				if (device_updated(needle, haystack[i]))
1308bd9244f7SScott Teel 					return DEVICE_UPDATED;
1309edd16368SStephen M. Cameron 				return DEVICE_SAME;
1310bd9244f7SScott Teel 			} else {
13119846590eSStephen M. Cameron 				/* Keep offline devices offline */
13129846590eSStephen M. Cameron 				if (needle->volume_offline)
13139846590eSStephen M. Cameron 					return DEVICE_NOT_FOUND;
1314edd16368SStephen M. Cameron 				return DEVICE_CHANGED;
1315edd16368SStephen M. Cameron 			}
1316edd16368SStephen M. Cameron 		}
1317bd9244f7SScott Teel 	}
1318edd16368SStephen M. Cameron 	*index = -1;
1319edd16368SStephen M. Cameron 	return DEVICE_NOT_FOUND;
1320edd16368SStephen M. Cameron }
1321edd16368SStephen M. Cameron 
13229846590eSStephen M. Cameron static void hpsa_monitor_offline_device(struct ctlr_info *h,
13239846590eSStephen M. Cameron 					unsigned char scsi3addr[])
13249846590eSStephen M. Cameron {
13259846590eSStephen M. Cameron 	struct offline_device_entry *device;
13269846590eSStephen M. Cameron 	unsigned long flags;
13279846590eSStephen M. Cameron 
13289846590eSStephen M. Cameron 	/* Check to see if device is already on the list */
13299846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
13309846590eSStephen M. Cameron 	list_for_each_entry(device, &h->offline_device_list, offline_list) {
13319846590eSStephen M. Cameron 		if (memcmp(device->scsi3addr, scsi3addr,
13329846590eSStephen M. Cameron 			sizeof(device->scsi3addr)) == 0) {
13339846590eSStephen M. Cameron 			spin_unlock_irqrestore(&h->offline_device_lock, flags);
13349846590eSStephen M. Cameron 			return;
13359846590eSStephen M. Cameron 		}
13369846590eSStephen M. Cameron 	}
13379846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
13389846590eSStephen M. Cameron 
13399846590eSStephen M. Cameron 	/* Device is not on the list, add it. */
13409846590eSStephen M. Cameron 	device = kmalloc(sizeof(*device), GFP_KERNEL);
13419846590eSStephen M. Cameron 	if (!device) {
13429846590eSStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in %s\n", __func__);
13439846590eSStephen M. Cameron 		return;
13449846590eSStephen M. Cameron 	}
13459846590eSStephen M. Cameron 	memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
13469846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
13479846590eSStephen M. Cameron 	list_add_tail(&device->offline_list, &h->offline_device_list);
13489846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
13499846590eSStephen M. Cameron }
13509846590eSStephen M. Cameron 
13519846590eSStephen M. Cameron /* Print a message explaining various offline volume states */
13529846590eSStephen M. Cameron static void hpsa_show_volume_status(struct ctlr_info *h,
13539846590eSStephen M. Cameron 	struct hpsa_scsi_dev_t *sd)
13549846590eSStephen M. Cameron {
13559846590eSStephen M. Cameron 	if (sd->volume_offline == HPSA_VPD_LV_STATUS_UNSUPPORTED)
13569846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13579846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume status is not available through vital product data pages.\n",
13589846590eSStephen M. Cameron 			h->scsi_host->host_no,
13599846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13609846590eSStephen M. Cameron 	switch (sd->volume_offline) {
13619846590eSStephen M. Cameron 	case HPSA_LV_OK:
13629846590eSStephen M. Cameron 		break;
13639846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ERASE:
13649846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13659846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing background erase process.\n",
13669846590eSStephen M. Cameron 			h->scsi_host->host_no,
13679846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13689846590eSStephen M. Cameron 		break;
13699846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_RPI:
13709846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13719846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing rapid parity initialization process.\n",
13729846590eSStephen M. Cameron 			h->scsi_host->host_no,
13739846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13749846590eSStephen M. Cameron 		break;
13759846590eSStephen M. Cameron 	case HPSA_LV_PENDING_RPI:
13769846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13779846590eSStephen M. Cameron 				"C%d:B%d:T%d:L%d Volume is queued for rapid parity initialization process.\n",
13789846590eSStephen M. Cameron 				h->scsi_host->host_no,
13799846590eSStephen M. Cameron 				sd->bus, sd->target, sd->lun);
13809846590eSStephen M. Cameron 		break;
13819846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_NO_KEY:
13829846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13839846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because key is not present.\n",
13849846590eSStephen M. Cameron 			h->scsi_host->host_no,
13859846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13869846590eSStephen M. Cameron 		break;
13879846590eSStephen M. Cameron 	case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
13889846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13899846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is not encrypted and cannot be accessed because controller is in encryption-only mode.\n",
13909846590eSStephen M. Cameron 			h->scsi_host->host_no,
13919846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13929846590eSStephen M. Cameron 		break;
13939846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION:
13949846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13959846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing encryption process.\n",
13969846590eSStephen M. Cameron 			h->scsi_host->host_no,
13979846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13989846590eSStephen M. Cameron 		break;
13999846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
14009846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
14019846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing encryption re-keying process.\n",
14029846590eSStephen M. Cameron 			h->scsi_host->host_no,
14039846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
14049846590eSStephen M. Cameron 		break;
14059846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
14069846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
14079846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because controller does not have encryption enabled.\n",
14089846590eSStephen M. Cameron 			h->scsi_host->host_no,
14099846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
14109846590eSStephen M. Cameron 		break;
14119846590eSStephen M. Cameron 	case HPSA_LV_PENDING_ENCRYPTION:
14129846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
14139846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is pending migration to encrypted state, but process has not started.\n",
14149846590eSStephen M. Cameron 			h->scsi_host->host_no,
14159846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
14169846590eSStephen M. Cameron 		break;
14179846590eSStephen M. Cameron 	case HPSA_LV_PENDING_ENCRYPTION_REKEYING:
14189846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
14199846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is encrypted and is pending encryption rekeying.\n",
14209846590eSStephen M. Cameron 			h->scsi_host->host_no,
14219846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
14229846590eSStephen M. Cameron 		break;
14239846590eSStephen M. Cameron 	}
14249846590eSStephen M. Cameron }
14259846590eSStephen M. Cameron 
142603383736SDon Brace /*
142703383736SDon Brace  * Figure the list of physical drive pointers for a logical drive with
142803383736SDon Brace  * raid offload configured.
142903383736SDon Brace  */
143003383736SDon Brace static void hpsa_figure_phys_disk_ptrs(struct ctlr_info *h,
143103383736SDon Brace 				struct hpsa_scsi_dev_t *dev[], int ndevices,
143203383736SDon Brace 				struct hpsa_scsi_dev_t *logical_drive)
143303383736SDon Brace {
143403383736SDon Brace 	struct raid_map_data *map = &logical_drive->raid_map;
143503383736SDon Brace 	struct raid_map_disk_data *dd = &map->data[0];
143603383736SDon Brace 	int i, j;
143703383736SDon Brace 	int total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
143803383736SDon Brace 				le16_to_cpu(map->metadata_disks_per_row);
143903383736SDon Brace 	int nraid_map_entries = le16_to_cpu(map->row_cnt) *
144003383736SDon Brace 				le16_to_cpu(map->layout_map_count) *
144103383736SDon Brace 				total_disks_per_row;
144203383736SDon Brace 	int nphys_disk = le16_to_cpu(map->layout_map_count) *
144303383736SDon Brace 				total_disks_per_row;
144403383736SDon Brace 	int qdepth;
144503383736SDon Brace 
144603383736SDon Brace 	if (nraid_map_entries > RAID_MAP_MAX_ENTRIES)
144703383736SDon Brace 		nraid_map_entries = RAID_MAP_MAX_ENTRIES;
144803383736SDon Brace 
144903383736SDon Brace 	qdepth = 0;
145003383736SDon Brace 	for (i = 0; i < nraid_map_entries; i++) {
145103383736SDon Brace 		logical_drive->phys_disk[i] = NULL;
145203383736SDon Brace 		if (!logical_drive->offload_config)
145303383736SDon Brace 			continue;
145403383736SDon Brace 		for (j = 0; j < ndevices; j++) {
145503383736SDon Brace 			if (dev[j]->devtype != TYPE_DISK)
145603383736SDon Brace 				continue;
145703383736SDon Brace 			if (is_logical_dev_addr_mode(dev[j]->scsi3addr))
145803383736SDon Brace 				continue;
145903383736SDon Brace 			if (dev[j]->ioaccel_handle != dd[i].ioaccel_handle)
146003383736SDon Brace 				continue;
146103383736SDon Brace 
146203383736SDon Brace 			logical_drive->phys_disk[i] = dev[j];
146303383736SDon Brace 			if (i < nphys_disk)
146403383736SDon Brace 				qdepth = min(h->nr_cmds, qdepth +
146503383736SDon Brace 				    logical_drive->phys_disk[i]->queue_depth);
146603383736SDon Brace 			break;
146703383736SDon Brace 		}
146803383736SDon Brace 
146903383736SDon Brace 		/*
147003383736SDon Brace 		 * This can happen if a physical drive is removed and
147103383736SDon Brace 		 * the logical drive is degraded.  In that case, the RAID
147203383736SDon Brace 		 * map data will refer to a physical disk which isn't actually
147303383736SDon Brace 		 * present.  And in that case offload_enabled should already
147403383736SDon Brace 		 * be 0, but we'll turn it off here just in case
147503383736SDon Brace 		 */
147603383736SDon Brace 		if (!logical_drive->phys_disk[i]) {
147703383736SDon Brace 			logical_drive->offload_enabled = 0;
147841ce4c35SStephen Cameron 			logical_drive->offload_to_be_enabled = 0;
147941ce4c35SStephen Cameron 			logical_drive->queue_depth = 8;
148003383736SDon Brace 		}
148103383736SDon Brace 	}
148203383736SDon Brace 	if (nraid_map_entries)
148303383736SDon Brace 		/*
148403383736SDon Brace 		 * This is correct for reads, too high for full stripe writes,
148503383736SDon Brace 		 * way too high for partial stripe writes
148603383736SDon Brace 		 */
148703383736SDon Brace 		logical_drive->queue_depth = qdepth;
148803383736SDon Brace 	else
148903383736SDon Brace 		logical_drive->queue_depth = h->nr_cmds;
149003383736SDon Brace }
149103383736SDon Brace 
149203383736SDon Brace static void hpsa_update_log_drive_phys_drive_ptrs(struct ctlr_info *h,
149303383736SDon Brace 				struct hpsa_scsi_dev_t *dev[], int ndevices)
149403383736SDon Brace {
149503383736SDon Brace 	int i;
149603383736SDon Brace 
149703383736SDon Brace 	for (i = 0; i < ndevices; i++) {
149803383736SDon Brace 		if (dev[i]->devtype != TYPE_DISK)
149903383736SDon Brace 			continue;
150003383736SDon Brace 		if (!is_logical_dev_addr_mode(dev[i]->scsi3addr))
150103383736SDon Brace 			continue;
150241ce4c35SStephen Cameron 
150341ce4c35SStephen Cameron 		/*
150441ce4c35SStephen Cameron 		 * If offload is currently enabled, the RAID map and
150541ce4c35SStephen Cameron 		 * phys_disk[] assignment *better* not be changing
150641ce4c35SStephen Cameron 		 * and since it isn't changing, we do not need to
150741ce4c35SStephen Cameron 		 * update it.
150841ce4c35SStephen Cameron 		 */
150941ce4c35SStephen Cameron 		if (dev[i]->offload_enabled)
151041ce4c35SStephen Cameron 			continue;
151141ce4c35SStephen Cameron 
151203383736SDon Brace 		hpsa_figure_phys_disk_ptrs(h, dev, ndevices, dev[i]);
151303383736SDon Brace 	}
151403383736SDon Brace }
151503383736SDon Brace 
15164967bd3eSStephen M. Cameron static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
1517edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd[], int nsds)
1518edd16368SStephen M. Cameron {
1519edd16368SStephen M. Cameron 	/* sd contains scsi3 addresses and devtypes, and inquiry
1520edd16368SStephen M. Cameron 	 * data.  This function takes what's in sd to be the current
1521edd16368SStephen M. Cameron 	 * reality and updates h->dev[] to reflect that reality.
1522edd16368SStephen M. Cameron 	 */
1523edd16368SStephen M. Cameron 	int i, entry, device_change, changes = 0;
1524edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *csd;
1525edd16368SStephen M. Cameron 	unsigned long flags;
1526edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **added, **removed;
1527edd16368SStephen M. Cameron 	int nadded, nremoved;
1528edd16368SStephen M. Cameron 	struct Scsi_Host *sh = NULL;
1529edd16368SStephen M. Cameron 
1530cfe5badcSScott Teel 	added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
1531cfe5badcSScott Teel 	removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
1532edd16368SStephen M. Cameron 
1533edd16368SStephen M. Cameron 	if (!added || !removed) {
1534edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in "
1535edd16368SStephen M. Cameron 			"adjust_hpsa_scsi_table\n");
1536edd16368SStephen M. Cameron 		goto free_and_out;
1537edd16368SStephen M. Cameron 	}
1538edd16368SStephen M. Cameron 
1539edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1540edd16368SStephen M. Cameron 
1541edd16368SStephen M. Cameron 	/* find any devices in h->dev[] that are not in
1542edd16368SStephen M. Cameron 	 * sd[] and remove them from h->dev[], and for any
1543edd16368SStephen M. Cameron 	 * devices which have changed, remove the old device
1544edd16368SStephen M. Cameron 	 * info and add the new device info.
1545bd9244f7SScott Teel 	 * If minor device attributes change, just update
1546bd9244f7SScott Teel 	 * the existing device structure.
1547edd16368SStephen M. Cameron 	 */
1548edd16368SStephen M. Cameron 	i = 0;
1549edd16368SStephen M. Cameron 	nremoved = 0;
1550edd16368SStephen M. Cameron 	nadded = 0;
1551edd16368SStephen M. Cameron 	while (i < h->ndevices) {
1552edd16368SStephen M. Cameron 		csd = h->dev[i];
1553edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
1554edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
1555edd16368SStephen M. Cameron 			changes++;
1556edd16368SStephen M. Cameron 			hpsa_scsi_remove_entry(h, hostno, i,
1557edd16368SStephen M. Cameron 				removed, &nremoved);
1558edd16368SStephen M. Cameron 			continue; /* remove ^^^, hence i not incremented */
1559edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
1560edd16368SStephen M. Cameron 			changes++;
15612a8ccf31SStephen M. Cameron 			hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
15622a8ccf31SStephen M. Cameron 				added, &nadded, removed, &nremoved);
1563c7f172dcSStephen M. Cameron 			/* Set it to NULL to prevent it from being freed
1564c7f172dcSStephen M. Cameron 			 * at the bottom of hpsa_update_scsi_devices()
1565c7f172dcSStephen M. Cameron 			 */
1566c7f172dcSStephen M. Cameron 			sd[entry] = NULL;
1567bd9244f7SScott Teel 		} else if (device_change == DEVICE_UPDATED) {
1568bd9244f7SScott Teel 			hpsa_scsi_update_entry(h, hostno, i, sd[entry]);
1569edd16368SStephen M. Cameron 		}
1570edd16368SStephen M. Cameron 		i++;
1571edd16368SStephen M. Cameron 	}
1572edd16368SStephen M. Cameron 
1573edd16368SStephen M. Cameron 	/* Now, make sure every device listed in sd[] is also
1574edd16368SStephen M. Cameron 	 * listed in h->dev[], adding them if they aren't found
1575edd16368SStephen M. Cameron 	 */
1576edd16368SStephen M. Cameron 
1577edd16368SStephen M. Cameron 	for (i = 0; i < nsds; i++) {
1578edd16368SStephen M. Cameron 		if (!sd[i]) /* if already added above. */
1579edd16368SStephen M. Cameron 			continue;
15809846590eSStephen M. Cameron 
15819846590eSStephen M. Cameron 		/* Don't add devices which are NOT READY, FORMAT IN PROGRESS
15829846590eSStephen M. Cameron 		 * as the SCSI mid-layer does not handle such devices well.
15839846590eSStephen M. Cameron 		 * It relentlessly loops sending TUR at 3Hz, then READ(10)
15849846590eSStephen M. Cameron 		 * at 160Hz, and prevents the system from coming up.
15859846590eSStephen M. Cameron 		 */
15869846590eSStephen M. Cameron 		if (sd[i]->volume_offline) {
15879846590eSStephen M. Cameron 			hpsa_show_volume_status(h, sd[i]);
15880d96ef5fSWebb Scales 			hpsa_show_dev_msg(KERN_INFO, h, sd[i], "offline");
15899846590eSStephen M. Cameron 			continue;
15909846590eSStephen M. Cameron 		}
15919846590eSStephen M. Cameron 
1592edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1593edd16368SStephen M. Cameron 					h->ndevices, &entry);
1594edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
1595edd16368SStephen M. Cameron 			changes++;
1596edd16368SStephen M. Cameron 			if (hpsa_scsi_add_entry(h, hostno, sd[i],
1597edd16368SStephen M. Cameron 				added, &nadded) != 0)
1598edd16368SStephen M. Cameron 				break;
1599edd16368SStephen M. Cameron 			sd[i] = NULL; /* prevent from being freed later. */
1600edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
1601edd16368SStephen M. Cameron 			/* should never happen... */
1602edd16368SStephen M. Cameron 			changes++;
1603edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev,
1604edd16368SStephen M. Cameron 				"device unexpectedly changed.\n");
1605edd16368SStephen M. Cameron 			/* but if it does happen, we just ignore that device */
1606edd16368SStephen M. Cameron 		}
1607edd16368SStephen M. Cameron 	}
160841ce4c35SStephen Cameron 	hpsa_update_log_drive_phys_drive_ptrs(h, h->dev, h->ndevices);
160941ce4c35SStephen Cameron 
161041ce4c35SStephen Cameron 	/* Now that h->dev[]->phys_disk[] is coherent, we can enable
161141ce4c35SStephen Cameron 	 * any logical drives that need it enabled.
161241ce4c35SStephen Cameron 	 */
161341ce4c35SStephen Cameron 	for (i = 0; i < h->ndevices; i++)
161441ce4c35SStephen Cameron 		h->dev[i]->offload_enabled = h->dev[i]->offload_to_be_enabled;
161541ce4c35SStephen Cameron 
1616edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1617edd16368SStephen M. Cameron 
16189846590eSStephen M. Cameron 	/* Monitor devices which are in one of several NOT READY states to be
16199846590eSStephen M. Cameron 	 * brought online later. This must be done without holding h->devlock,
16209846590eSStephen M. Cameron 	 * so don't touch h->dev[]
16219846590eSStephen M. Cameron 	 */
16229846590eSStephen M. Cameron 	for (i = 0; i < nsds; i++) {
16239846590eSStephen M. Cameron 		if (!sd[i]) /* if already added above. */
16249846590eSStephen M. Cameron 			continue;
16259846590eSStephen M. Cameron 		if (sd[i]->volume_offline)
16269846590eSStephen M. Cameron 			hpsa_monitor_offline_device(h, sd[i]->scsi3addr);
16279846590eSStephen M. Cameron 	}
16289846590eSStephen M. Cameron 
1629edd16368SStephen M. Cameron 	/* Don't notify scsi mid layer of any changes the first time through
1630edd16368SStephen M. Cameron 	 * (or if there are no changes) scsi_scan_host will do it later the
1631edd16368SStephen M. Cameron 	 * first time through.
1632edd16368SStephen M. Cameron 	 */
1633edd16368SStephen M. Cameron 	if (hostno == -1 || !changes)
1634edd16368SStephen M. Cameron 		goto free_and_out;
1635edd16368SStephen M. Cameron 
1636edd16368SStephen M. Cameron 	sh = h->scsi_host;
1637edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any removed devices */
1638edd16368SStephen M. Cameron 	for (i = 0; i < nremoved; i++) {
163941ce4c35SStephen Cameron 		if (removed[i]->expose_state & HPSA_SCSI_ADD) {
1640edd16368SStephen M. Cameron 			struct scsi_device *sdev =
1641edd16368SStephen M. Cameron 				scsi_device_lookup(sh, removed[i]->bus,
1642edd16368SStephen M. Cameron 					removed[i]->target, removed[i]->lun);
1643edd16368SStephen M. Cameron 			if (sdev != NULL) {
1644edd16368SStephen M. Cameron 				scsi_remove_device(sdev);
1645edd16368SStephen M. Cameron 				scsi_device_put(sdev);
1646edd16368SStephen M. Cameron 			} else {
164741ce4c35SStephen Cameron 				/*
164841ce4c35SStephen Cameron 				 * We don't expect to get here.
1649edd16368SStephen M. Cameron 				 * future cmds to this device will get selection
1650edd16368SStephen M. Cameron 				 * timeout as if the device was gone.
1651edd16368SStephen M. Cameron 				 */
16520d96ef5fSWebb Scales 				hpsa_show_dev_msg(KERN_WARNING, h, removed[i],
16530d96ef5fSWebb Scales 					"didn't find device for removal.");
1654edd16368SStephen M. Cameron 			}
165541ce4c35SStephen Cameron 		}
1656edd16368SStephen M. Cameron 		kfree(removed[i]);
1657edd16368SStephen M. Cameron 		removed[i] = NULL;
1658edd16368SStephen M. Cameron 	}
1659edd16368SStephen M. Cameron 
1660edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any added devices */
1661edd16368SStephen M. Cameron 	for (i = 0; i < nadded; i++) {
166241ce4c35SStephen Cameron 		if (!(added[i]->expose_state & HPSA_SCSI_ADD))
166341ce4c35SStephen Cameron 			continue;
1664edd16368SStephen M. Cameron 		if (scsi_add_device(sh, added[i]->bus,
1665edd16368SStephen M. Cameron 			added[i]->target, added[i]->lun) == 0)
1666edd16368SStephen M. Cameron 			continue;
16670d96ef5fSWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, added[i],
16680d96ef5fSWebb Scales 					"addition failed, device not added.");
1669edd16368SStephen M. Cameron 		/* now we have to remove it from h->dev,
1670edd16368SStephen M. Cameron 		 * since it didn't get added to scsi mid layer
1671edd16368SStephen M. Cameron 		 */
1672edd16368SStephen M. Cameron 		fixup_botched_add(h, added[i]);
1673105a3dbcSRobert Elliott 		added[i] = NULL;
1674edd16368SStephen M. Cameron 	}
1675edd16368SStephen M. Cameron 
1676edd16368SStephen M. Cameron free_and_out:
1677edd16368SStephen M. Cameron 	kfree(added);
1678edd16368SStephen M. Cameron 	kfree(removed);
1679edd16368SStephen M. Cameron }
1680edd16368SStephen M. Cameron 
1681edd16368SStephen M. Cameron /*
16829e03aa2fSJoe Perches  * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
1683edd16368SStephen M. Cameron  * Assume's h->devlock is held.
1684edd16368SStephen M. Cameron  */
1685edd16368SStephen M. Cameron static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1686edd16368SStephen M. Cameron 	int bus, int target, int lun)
1687edd16368SStephen M. Cameron {
1688edd16368SStephen M. Cameron 	int i;
1689edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1690edd16368SStephen M. Cameron 
1691edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1692edd16368SStephen M. Cameron 		sd = h->dev[i];
1693edd16368SStephen M. Cameron 		if (sd->bus == bus && sd->target == target && sd->lun == lun)
1694edd16368SStephen M. Cameron 			return sd;
1695edd16368SStephen M. Cameron 	}
1696edd16368SStephen M. Cameron 	return NULL;
1697edd16368SStephen M. Cameron }
1698edd16368SStephen M. Cameron 
1699edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev)
1700edd16368SStephen M. Cameron {
1701edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1702edd16368SStephen M. Cameron 	unsigned long flags;
1703edd16368SStephen M. Cameron 	struct ctlr_info *h;
1704edd16368SStephen M. Cameron 
1705edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
1706edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1707edd16368SStephen M. Cameron 	sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1708edd16368SStephen M. Cameron 		sdev_id(sdev), sdev->lun);
170941ce4c35SStephen Cameron 	if (likely(sd)) {
171003383736SDon Brace 		atomic_set(&sd->ioaccel_cmds_out, 0);
171141ce4c35SStephen Cameron 		sdev->hostdata = (sd->expose_state & HPSA_SCSI_ADD) ? sd : NULL;
171241ce4c35SStephen Cameron 	} else
171341ce4c35SStephen Cameron 		sdev->hostdata = NULL;
1714edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1715edd16368SStephen M. Cameron 	return 0;
1716edd16368SStephen M. Cameron }
1717edd16368SStephen M. Cameron 
171841ce4c35SStephen Cameron /* configure scsi device based on internal per-device structure */
171941ce4c35SStephen Cameron static int hpsa_slave_configure(struct scsi_device *sdev)
172041ce4c35SStephen Cameron {
172141ce4c35SStephen Cameron 	struct hpsa_scsi_dev_t *sd;
172241ce4c35SStephen Cameron 	int queue_depth;
172341ce4c35SStephen Cameron 
172441ce4c35SStephen Cameron 	sd = sdev->hostdata;
172541ce4c35SStephen Cameron 	sdev->no_uld_attach = !sd || !(sd->expose_state & HPSA_ULD_ATTACH);
172641ce4c35SStephen Cameron 
172741ce4c35SStephen Cameron 	if (sd)
172841ce4c35SStephen Cameron 		queue_depth = sd->queue_depth != 0 ?
172941ce4c35SStephen Cameron 			sd->queue_depth : sdev->host->can_queue;
173041ce4c35SStephen Cameron 	else
173141ce4c35SStephen Cameron 		queue_depth = sdev->host->can_queue;
173241ce4c35SStephen Cameron 
173341ce4c35SStephen Cameron 	scsi_change_queue_depth(sdev, queue_depth);
173441ce4c35SStephen Cameron 
173541ce4c35SStephen Cameron 	return 0;
173641ce4c35SStephen Cameron }
173741ce4c35SStephen Cameron 
1738edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev)
1739edd16368SStephen M. Cameron {
1740bcc44255SStephen M. Cameron 	/* nothing to do. */
1741edd16368SStephen M. Cameron }
1742edd16368SStephen M. Cameron 
1743d9a729f3SWebb Scales static void hpsa_free_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
1744d9a729f3SWebb Scales {
1745d9a729f3SWebb Scales 	int i;
1746d9a729f3SWebb Scales 
1747d9a729f3SWebb Scales 	if (!h->ioaccel2_cmd_sg_list)
1748d9a729f3SWebb Scales 		return;
1749d9a729f3SWebb Scales 	for (i = 0; i < h->nr_cmds; i++) {
1750d9a729f3SWebb Scales 		kfree(h->ioaccel2_cmd_sg_list[i]);
1751d9a729f3SWebb Scales 		h->ioaccel2_cmd_sg_list[i] = NULL;
1752d9a729f3SWebb Scales 	}
1753d9a729f3SWebb Scales 	kfree(h->ioaccel2_cmd_sg_list);
1754d9a729f3SWebb Scales 	h->ioaccel2_cmd_sg_list = NULL;
1755d9a729f3SWebb Scales }
1756d9a729f3SWebb Scales 
1757d9a729f3SWebb Scales static int hpsa_allocate_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
1758d9a729f3SWebb Scales {
1759d9a729f3SWebb Scales 	int i;
1760d9a729f3SWebb Scales 
1761d9a729f3SWebb Scales 	if (h->chainsize <= 0)
1762d9a729f3SWebb Scales 		return 0;
1763d9a729f3SWebb Scales 
1764d9a729f3SWebb Scales 	h->ioaccel2_cmd_sg_list =
1765d9a729f3SWebb Scales 		kzalloc(sizeof(*h->ioaccel2_cmd_sg_list) * h->nr_cmds,
1766d9a729f3SWebb Scales 					GFP_KERNEL);
1767d9a729f3SWebb Scales 	if (!h->ioaccel2_cmd_sg_list)
1768d9a729f3SWebb Scales 		return -ENOMEM;
1769d9a729f3SWebb Scales 	for (i = 0; i < h->nr_cmds; i++) {
1770d9a729f3SWebb Scales 		h->ioaccel2_cmd_sg_list[i] =
1771d9a729f3SWebb Scales 			kmalloc(sizeof(*h->ioaccel2_cmd_sg_list[i]) *
1772d9a729f3SWebb Scales 					h->maxsgentries, GFP_KERNEL);
1773d9a729f3SWebb Scales 		if (!h->ioaccel2_cmd_sg_list[i])
1774d9a729f3SWebb Scales 			goto clean;
1775d9a729f3SWebb Scales 	}
1776d9a729f3SWebb Scales 	return 0;
1777d9a729f3SWebb Scales 
1778d9a729f3SWebb Scales clean:
1779d9a729f3SWebb Scales 	hpsa_free_ioaccel2_sg_chain_blocks(h);
1780d9a729f3SWebb Scales 	return -ENOMEM;
1781d9a729f3SWebb Scales }
1782d9a729f3SWebb Scales 
178333a2ffceSStephen M. Cameron static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
178433a2ffceSStephen M. Cameron {
178533a2ffceSStephen M. Cameron 	int i;
178633a2ffceSStephen M. Cameron 
178733a2ffceSStephen M. Cameron 	if (!h->cmd_sg_list)
178833a2ffceSStephen M. Cameron 		return;
178933a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
179033a2ffceSStephen M. Cameron 		kfree(h->cmd_sg_list[i]);
179133a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = NULL;
179233a2ffceSStephen M. Cameron 	}
179333a2ffceSStephen M. Cameron 	kfree(h->cmd_sg_list);
179433a2ffceSStephen M. Cameron 	h->cmd_sg_list = NULL;
179533a2ffceSStephen M. Cameron }
179633a2ffceSStephen M. Cameron 
1797105a3dbcSRobert Elliott static int hpsa_alloc_sg_chain_blocks(struct ctlr_info *h)
179833a2ffceSStephen M. Cameron {
179933a2ffceSStephen M. Cameron 	int i;
180033a2ffceSStephen M. Cameron 
180133a2ffceSStephen M. Cameron 	if (h->chainsize <= 0)
180233a2ffceSStephen M. Cameron 		return 0;
180333a2ffceSStephen M. Cameron 
180433a2ffceSStephen M. Cameron 	h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
180533a2ffceSStephen M. Cameron 				GFP_KERNEL);
18063d4e6af8SRobert Elliott 	if (!h->cmd_sg_list) {
18073d4e6af8SRobert Elliott 		dev_err(&h->pdev->dev, "Failed to allocate SG list\n");
180833a2ffceSStephen M. Cameron 		return -ENOMEM;
18093d4e6af8SRobert Elliott 	}
181033a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
181133a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
181233a2ffceSStephen M. Cameron 						h->chainsize, GFP_KERNEL);
18133d4e6af8SRobert Elliott 		if (!h->cmd_sg_list[i]) {
18143d4e6af8SRobert Elliott 			dev_err(&h->pdev->dev, "Failed to allocate cmd SG\n");
181533a2ffceSStephen M. Cameron 			goto clean;
181633a2ffceSStephen M. Cameron 		}
18173d4e6af8SRobert Elliott 	}
181833a2ffceSStephen M. Cameron 	return 0;
181933a2ffceSStephen M. Cameron 
182033a2ffceSStephen M. Cameron clean:
182133a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
182233a2ffceSStephen M. Cameron 	return -ENOMEM;
182333a2ffceSStephen M. Cameron }
182433a2ffceSStephen M. Cameron 
1825d9a729f3SWebb Scales static int hpsa_map_ioaccel2_sg_chain_block(struct ctlr_info *h,
1826d9a729f3SWebb Scales 	struct io_accel2_cmd *cp, struct CommandList *c)
1827d9a729f3SWebb Scales {
1828d9a729f3SWebb Scales 	struct ioaccel2_sg_element *chain_block;
1829d9a729f3SWebb Scales 	u64 temp64;
1830d9a729f3SWebb Scales 	u32 chain_size;
1831d9a729f3SWebb Scales 
1832d9a729f3SWebb Scales 	chain_block = h->ioaccel2_cmd_sg_list[c->cmdindex];
1833d9a729f3SWebb Scales 	chain_size = le32_to_cpu(cp->data_len);
1834d9a729f3SWebb Scales 	temp64 = pci_map_single(h->pdev, chain_block, chain_size,
1835d9a729f3SWebb Scales 				PCI_DMA_TODEVICE);
1836d9a729f3SWebb Scales 	if (dma_mapping_error(&h->pdev->dev, temp64)) {
1837d9a729f3SWebb Scales 		/* prevent subsequent unmapping */
1838d9a729f3SWebb Scales 		cp->sg->address = 0;
1839d9a729f3SWebb Scales 		return -1;
1840d9a729f3SWebb Scales 	}
1841d9a729f3SWebb Scales 	cp->sg->address = cpu_to_le64(temp64);
1842d9a729f3SWebb Scales 	return 0;
1843d9a729f3SWebb Scales }
1844d9a729f3SWebb Scales 
1845d9a729f3SWebb Scales static void hpsa_unmap_ioaccel2_sg_chain_block(struct ctlr_info *h,
1846d9a729f3SWebb Scales 	struct io_accel2_cmd *cp)
1847d9a729f3SWebb Scales {
1848d9a729f3SWebb Scales 	struct ioaccel2_sg_element *chain_sg;
1849d9a729f3SWebb Scales 	u64 temp64;
1850d9a729f3SWebb Scales 	u32 chain_size;
1851d9a729f3SWebb Scales 
1852d9a729f3SWebb Scales 	chain_sg = cp->sg;
1853d9a729f3SWebb Scales 	temp64 = le64_to_cpu(chain_sg->address);
1854d9a729f3SWebb Scales 	chain_size = le32_to_cpu(cp->data_len);
1855d9a729f3SWebb Scales 	pci_unmap_single(h->pdev, temp64, chain_size, PCI_DMA_TODEVICE);
1856d9a729f3SWebb Scales }
1857d9a729f3SWebb Scales 
1858e2bea6dfSStephen M. Cameron static int hpsa_map_sg_chain_block(struct ctlr_info *h,
185933a2ffceSStephen M. Cameron 	struct CommandList *c)
186033a2ffceSStephen M. Cameron {
186133a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg, *chain_block;
186233a2ffceSStephen M. Cameron 	u64 temp64;
186350a0decfSStephen M. Cameron 	u32 chain_len;
186433a2ffceSStephen M. Cameron 
186533a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
186633a2ffceSStephen M. Cameron 	chain_block = h->cmd_sg_list[c->cmdindex];
186750a0decfSStephen M. Cameron 	chain_sg->Ext = cpu_to_le32(HPSA_SG_CHAIN);
186850a0decfSStephen M. Cameron 	chain_len = sizeof(*chain_sg) *
18692b08b3e9SDon Brace 		(le16_to_cpu(c->Header.SGTotal) - h->max_cmd_sg_entries);
187050a0decfSStephen M. Cameron 	chain_sg->Len = cpu_to_le32(chain_len);
187150a0decfSStephen M. Cameron 	temp64 = pci_map_single(h->pdev, chain_block, chain_len,
187233a2ffceSStephen M. Cameron 				PCI_DMA_TODEVICE);
1873e2bea6dfSStephen M. Cameron 	if (dma_mapping_error(&h->pdev->dev, temp64)) {
1874e2bea6dfSStephen M. Cameron 		/* prevent subsequent unmapping */
187550a0decfSStephen M. Cameron 		chain_sg->Addr = cpu_to_le64(0);
1876e2bea6dfSStephen M. Cameron 		return -1;
1877e2bea6dfSStephen M. Cameron 	}
187850a0decfSStephen M. Cameron 	chain_sg->Addr = cpu_to_le64(temp64);
1879e2bea6dfSStephen M. Cameron 	return 0;
188033a2ffceSStephen M. Cameron }
188133a2ffceSStephen M. Cameron 
188233a2ffceSStephen M. Cameron static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
188333a2ffceSStephen M. Cameron 	struct CommandList *c)
188433a2ffceSStephen M. Cameron {
188533a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg;
188633a2ffceSStephen M. Cameron 
188750a0decfSStephen M. Cameron 	if (le16_to_cpu(c->Header.SGTotal) <= h->max_cmd_sg_entries)
188833a2ffceSStephen M. Cameron 		return;
188933a2ffceSStephen M. Cameron 
189033a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
189150a0decfSStephen M. Cameron 	pci_unmap_single(h->pdev, le64_to_cpu(chain_sg->Addr),
189250a0decfSStephen M. Cameron 			le32_to_cpu(chain_sg->Len), PCI_DMA_TODEVICE);
189333a2ffceSStephen M. Cameron }
189433a2ffceSStephen M. Cameron 
1895a09c1441SScott Teel 
1896a09c1441SScott Teel /* Decode the various types of errors on ioaccel2 path.
1897a09c1441SScott Teel  * Return 1 for any error that should generate a RAID path retry.
1898a09c1441SScott Teel  * Return 0 for errors that don't require a RAID path retry.
1899a09c1441SScott Teel  */
1900a09c1441SScott Teel static int handle_ioaccel_mode2_error(struct ctlr_info *h,
1901c349775eSScott Teel 					struct CommandList *c,
1902c349775eSScott Teel 					struct scsi_cmnd *cmd,
1903c349775eSScott Teel 					struct io_accel2_cmd *c2)
1904c349775eSScott Teel {
1905c349775eSScott Teel 	int data_len;
1906a09c1441SScott Teel 	int retry = 0;
1907c40820d5SJoe Handzik 	u32 ioaccel2_resid = 0;
1908c349775eSScott Teel 
1909c349775eSScott Teel 	switch (c2->error_data.serv_response) {
1910c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_COMPLETE:
1911c349775eSScott Teel 		switch (c2->error_data.status) {
1912c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
1913c349775eSScott Teel 			break;
1914c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
1915ee6b1889SStephen M. Cameron 			cmd->result |= SAM_STAT_CHECK_CONDITION;
1916c349775eSScott Teel 			if (c2->error_data.data_present !=
1917ee6b1889SStephen M. Cameron 					IOACCEL2_SENSE_DATA_PRESENT) {
1918ee6b1889SStephen M. Cameron 				memset(cmd->sense_buffer, 0,
1919ee6b1889SStephen M. Cameron 					SCSI_SENSE_BUFFERSIZE);
1920c349775eSScott Teel 				break;
1921ee6b1889SStephen M. Cameron 			}
1922c349775eSScott Teel 			/* copy the sense data */
1923c349775eSScott Teel 			data_len = c2->error_data.sense_data_len;
1924c349775eSScott Teel 			if (data_len > SCSI_SENSE_BUFFERSIZE)
1925c349775eSScott Teel 				data_len = SCSI_SENSE_BUFFERSIZE;
1926c349775eSScott Teel 			if (data_len > sizeof(c2->error_data.sense_data_buff))
1927c349775eSScott Teel 				data_len =
1928c349775eSScott Teel 					sizeof(c2->error_data.sense_data_buff);
1929c349775eSScott Teel 			memcpy(cmd->sense_buffer,
1930c349775eSScott Teel 				c2->error_data.sense_data_buff, data_len);
1931a09c1441SScott Teel 			retry = 1;
1932c349775eSScott Teel 			break;
1933c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
1934a09c1441SScott Teel 			retry = 1;
1935c349775eSScott Teel 			break;
1936c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
1937a09c1441SScott Teel 			retry = 1;
1938c349775eSScott Teel 			break;
1939c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
19404a8da22bSStephen Cameron 			retry = 1;
1941c349775eSScott Teel 			break;
1942c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
1943a09c1441SScott Teel 			retry = 1;
1944c349775eSScott Teel 			break;
1945c349775eSScott Teel 		default:
1946a09c1441SScott Teel 			retry = 1;
1947c349775eSScott Teel 			break;
1948c349775eSScott Teel 		}
1949c349775eSScott Teel 		break;
1950c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_FAILURE:
1951c40820d5SJoe Handzik 		switch (c2->error_data.status) {
1952c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_IO_ERROR:
1953c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_IO_ABORTED:
1954c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_OVERRUN:
1955c40820d5SJoe Handzik 			retry = 1;
1956c40820d5SJoe Handzik 			break;
1957c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_UNDERRUN:
1958c40820d5SJoe Handzik 			cmd->result = (DID_OK << 16);		/* host byte */
1959c40820d5SJoe Handzik 			cmd->result |= (COMMAND_COMPLETE << 8);	/* msg byte */
1960c40820d5SJoe Handzik 			ioaccel2_resid = get_unaligned_le32(
1961c40820d5SJoe Handzik 						&c2->error_data.resid_cnt[0]);
1962c40820d5SJoe Handzik 			scsi_set_resid(cmd, ioaccel2_resid);
1963c40820d5SJoe Handzik 			break;
1964c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_NO_PATH_TO_DEVICE:
1965c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_INVALID_DEVICE:
1966c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_IOACCEL_DISABLED:
1967c40820d5SJoe Handzik 			/* We will get an event from ctlr to trigger rescan */
1968c40820d5SJoe Handzik 			retry = 1;
1969c40820d5SJoe Handzik 			break;
1970c40820d5SJoe Handzik 		default:
1971c40820d5SJoe Handzik 			retry = 1;
1972c40820d5SJoe Handzik 		}
1973c349775eSScott Teel 		break;
1974c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
1975c349775eSScott Teel 		break;
1976c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
1977c349775eSScott Teel 		break;
1978c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
1979a09c1441SScott Teel 		retry = 1;
1980c349775eSScott Teel 		break;
1981c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
1982c349775eSScott Teel 		break;
1983c349775eSScott Teel 	default:
1984a09c1441SScott Teel 		retry = 1;
1985c349775eSScott Teel 		break;
1986c349775eSScott Teel 	}
1987a09c1441SScott Teel 
1988a09c1441SScott Teel 	return retry;	/* retry on raid path? */
1989c349775eSScott Teel }
1990c349775eSScott Teel 
1991a58e7e53SWebb Scales static void hpsa_cmd_resolve_events(struct ctlr_info *h,
1992a58e7e53SWebb Scales 		struct CommandList *c)
1993a58e7e53SWebb Scales {
1994a58e7e53SWebb Scales 	/*
1995a58e7e53SWebb Scales 	 * Prevent the following race in the abort handler:
1996a58e7e53SWebb Scales 	 *
1997a58e7e53SWebb Scales 	 * 1. LLD is requested to abort a SCSI command
1998a58e7e53SWebb Scales 	 * 2. The SCSI command completes
1999a58e7e53SWebb Scales 	 * 3. The struct CommandList associated with step 2 is made available
2000a58e7e53SWebb Scales 	 * 4. New I/O request to LLD to another LUN re-uses struct CommandList
2001a58e7e53SWebb Scales 	 * 5. Abort handler follows scsi_cmnd->host_scribble and
2002a58e7e53SWebb Scales 	 *    finds struct CommandList and tries to aborts it
2003a58e7e53SWebb Scales 	 * Now we have aborted the wrong command.
2004a58e7e53SWebb Scales 	 *
2005a58e7e53SWebb Scales 	 * Clear c->scsi_cmd here so that the abort handler will know this
2006a58e7e53SWebb Scales 	 * command has completed.  Then, check to see if the abort handler is
2007a58e7e53SWebb Scales 	 * waiting for this command, and, if so, wake it.
2008a58e7e53SWebb Scales 	 */
2009a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_IDLE;
2010a58e7e53SWebb Scales 	mb(); /* Ensure c->scsi_cmd is set to SCSI_CMD_IDLE */
2011a58e7e53SWebb Scales 	if (c->abort_pending) {
2012a58e7e53SWebb Scales 		c->abort_pending = false;
2013a58e7e53SWebb Scales 		wake_up_all(&h->abort_sync_wait_queue);
2014a58e7e53SWebb Scales 	}
2015a58e7e53SWebb Scales }
2016a58e7e53SWebb Scales 
2017*73153fe5SWebb Scales static void hpsa_cmd_resolve_and_free(struct ctlr_info *h,
2018*73153fe5SWebb Scales 				      struct CommandList *c)
2019*73153fe5SWebb Scales {
2020*73153fe5SWebb Scales 	hpsa_cmd_resolve_events(h, c);
2021*73153fe5SWebb Scales 	cmd_tagged_free(h, c);
2022*73153fe5SWebb Scales }
2023*73153fe5SWebb Scales 
20248a0ff92cSWebb Scales static void hpsa_cmd_free_and_done(struct ctlr_info *h,
20258a0ff92cSWebb Scales 		struct CommandList *c, struct scsi_cmnd *cmd)
20268a0ff92cSWebb Scales {
2027*73153fe5SWebb Scales 	hpsa_cmd_resolve_and_free(h, c);
20288a0ff92cSWebb Scales 	cmd->scsi_done(cmd);
20298a0ff92cSWebb Scales }
20308a0ff92cSWebb Scales 
20318a0ff92cSWebb Scales static void hpsa_retry_cmd(struct ctlr_info *h, struct CommandList *c)
20328a0ff92cSWebb Scales {
20338a0ff92cSWebb Scales 	INIT_WORK(&c->work, hpsa_command_resubmit_worker);
20348a0ff92cSWebb Scales 	queue_work_on(raw_smp_processor_id(), h->resubmit_wq, &c->work);
20358a0ff92cSWebb Scales }
20368a0ff92cSWebb Scales 
2037a58e7e53SWebb Scales static void hpsa_set_scsi_cmd_aborted(struct scsi_cmnd *cmd)
2038a58e7e53SWebb Scales {
2039a58e7e53SWebb Scales 	cmd->result = DID_ABORT << 16;
2040a58e7e53SWebb Scales }
2041a58e7e53SWebb Scales 
2042a58e7e53SWebb Scales static void hpsa_cmd_abort_and_free(struct ctlr_info *h, struct CommandList *c,
2043a58e7e53SWebb Scales 				    struct scsi_cmnd *cmd)
2044a58e7e53SWebb Scales {
2045a58e7e53SWebb Scales 	hpsa_set_scsi_cmd_aborted(cmd);
2046a58e7e53SWebb Scales 	dev_warn(&h->pdev->dev, "CDB %16phN was aborted with status 0x%x\n",
2047a58e7e53SWebb Scales 			 c->Request.CDB, c->err_info->ScsiStatus);
2048*73153fe5SWebb Scales 	hpsa_cmd_resolve_and_free(h, c);
2049a58e7e53SWebb Scales }
2050a58e7e53SWebb Scales 
2051c349775eSScott Teel static void process_ioaccel2_completion(struct ctlr_info *h,
2052c349775eSScott Teel 		struct CommandList *c, struct scsi_cmnd *cmd,
2053c349775eSScott Teel 		struct hpsa_scsi_dev_t *dev)
2054c349775eSScott Teel {
2055c349775eSScott Teel 	struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
2056c349775eSScott Teel 
2057c349775eSScott Teel 	/* check for good status */
2058c349775eSScott Teel 	if (likely(c2->error_data.serv_response == 0 &&
20598a0ff92cSWebb Scales 			c2->error_data.status == 0))
20608a0ff92cSWebb Scales 		return hpsa_cmd_free_and_done(h, c, cmd);
2061c349775eSScott Teel 
2062a58e7e53SWebb Scales 	/* don't requeue a command which is being aborted */
2063a58e7e53SWebb Scales 	if (unlikely(c->abort_pending))
2064a58e7e53SWebb Scales 		return hpsa_cmd_abort_and_free(h, c, cmd);
2065a58e7e53SWebb Scales 
20668a0ff92cSWebb Scales 	/*
20678a0ff92cSWebb Scales 	 * Any RAID offload error results in retry which will use
2068c349775eSScott Teel 	 * the normal I/O path so the controller can handle whatever's
2069c349775eSScott Teel 	 * wrong.
2070c349775eSScott Teel 	 */
2071c349775eSScott Teel 	if (is_logical_dev_addr_mode(dev->scsi3addr) &&
2072c349775eSScott Teel 		c2->error_data.serv_response ==
2073c349775eSScott Teel 			IOACCEL2_SERV_RESPONSE_FAILURE) {
2074080ef1ccSDon Brace 		if (c2->error_data.status ==
2075080ef1ccSDon Brace 			IOACCEL2_STATUS_SR_IOACCEL_DISABLED)
2076c349775eSScott Teel 			dev->offload_enabled = 0;
20778a0ff92cSWebb Scales 
20788a0ff92cSWebb Scales 		return hpsa_retry_cmd(h, c);
2079080ef1ccSDon Brace 	}
2080080ef1ccSDon Brace 
2081080ef1ccSDon Brace 	if (handle_ioaccel_mode2_error(h, c, cmd, c2))
20828a0ff92cSWebb Scales 		return hpsa_retry_cmd(h, c);
2083080ef1ccSDon Brace 
20848a0ff92cSWebb Scales 	return hpsa_cmd_free_and_done(h, c, cmd);
2085c349775eSScott Teel }
2086c349775eSScott Teel 
20879437ac43SStephen Cameron /* Returns 0 on success, < 0 otherwise. */
20889437ac43SStephen Cameron static int hpsa_evaluate_tmf_status(struct ctlr_info *h,
20899437ac43SStephen Cameron 					struct CommandList *cp)
20909437ac43SStephen Cameron {
20919437ac43SStephen Cameron 	u8 tmf_status = cp->err_info->ScsiStatus;
20929437ac43SStephen Cameron 
20939437ac43SStephen Cameron 	switch (tmf_status) {
20949437ac43SStephen Cameron 	case CISS_TMF_COMPLETE:
20959437ac43SStephen Cameron 		/*
20969437ac43SStephen Cameron 		 * CISS_TMF_COMPLETE never happens, instead,
20979437ac43SStephen Cameron 		 * ei->CommandStatus == 0 for this case.
20989437ac43SStephen Cameron 		 */
20999437ac43SStephen Cameron 	case CISS_TMF_SUCCESS:
21009437ac43SStephen Cameron 		return 0;
21019437ac43SStephen Cameron 	case CISS_TMF_INVALID_FRAME:
21029437ac43SStephen Cameron 	case CISS_TMF_NOT_SUPPORTED:
21039437ac43SStephen Cameron 	case CISS_TMF_FAILED:
21049437ac43SStephen Cameron 	case CISS_TMF_WRONG_LUN:
21059437ac43SStephen Cameron 	case CISS_TMF_OVERLAPPED_TAG:
21069437ac43SStephen Cameron 		break;
21079437ac43SStephen Cameron 	default:
21089437ac43SStephen Cameron 		dev_warn(&h->pdev->dev, "Unknown TMF status: 0x%02x\n",
21099437ac43SStephen Cameron 				tmf_status);
21109437ac43SStephen Cameron 		break;
21119437ac43SStephen Cameron 	}
21129437ac43SStephen Cameron 	return -tmf_status;
21139437ac43SStephen Cameron }
21149437ac43SStephen Cameron 
21151fb011fbSStephen M. Cameron static void complete_scsi_command(struct CommandList *cp)
2116edd16368SStephen M. Cameron {
2117edd16368SStephen M. Cameron 	struct scsi_cmnd *cmd;
2118edd16368SStephen M. Cameron 	struct ctlr_info *h;
2119edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2120283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
2121d9a729f3SWebb Scales 	struct io_accel2_cmd *c2;
2122edd16368SStephen M. Cameron 
21239437ac43SStephen Cameron 	u8 sense_key;
21249437ac43SStephen Cameron 	u8 asc;      /* additional sense code */
21259437ac43SStephen Cameron 	u8 ascq;     /* additional sense code qualifier */
2126db111e18SStephen M. Cameron 	unsigned long sense_data_size;
2127edd16368SStephen M. Cameron 
2128edd16368SStephen M. Cameron 	ei = cp->err_info;
21297fa3030cSStephen Cameron 	cmd = cp->scsi_cmd;
2130edd16368SStephen M. Cameron 	h = cp->h;
2131283b4a9bSStephen M. Cameron 	dev = cmd->device->hostdata;
2132d9a729f3SWebb Scales 	c2 = &h->ioaccel2_cmd_pool[cp->cmdindex];
2133edd16368SStephen M. Cameron 
2134edd16368SStephen M. Cameron 	scsi_dma_unmap(cmd); /* undo the DMA mappings */
2135e1f7de0cSMatt Gates 	if ((cp->cmd_type == CMD_SCSI) &&
21362b08b3e9SDon Brace 		(le16_to_cpu(cp->Header.SGTotal) > h->max_cmd_sg_entries))
213733a2ffceSStephen M. Cameron 		hpsa_unmap_sg_chain_block(h, cp);
2138edd16368SStephen M. Cameron 
2139d9a729f3SWebb Scales 	if ((cp->cmd_type == CMD_IOACCEL2) &&
2140d9a729f3SWebb Scales 		(c2->sg[0].chain_indicator == IOACCEL2_CHAIN))
2141d9a729f3SWebb Scales 		hpsa_unmap_ioaccel2_sg_chain_block(h, c2);
2142d9a729f3SWebb Scales 
2143edd16368SStephen M. Cameron 	cmd->result = (DID_OK << 16); 		/* host byte */
2144edd16368SStephen M. Cameron 	cmd->result |= (COMMAND_COMPLETE << 8);	/* msg byte */
2145c349775eSScott Teel 
214603383736SDon Brace 	if (cp->cmd_type == CMD_IOACCEL2 || cp->cmd_type == CMD_IOACCEL1)
214703383736SDon Brace 		atomic_dec(&cp->phys_disk->ioaccel_cmds_out);
214803383736SDon Brace 
214925163bd5SWebb Scales 	/*
215025163bd5SWebb Scales 	 * We check for lockup status here as it may be set for
215125163bd5SWebb Scales 	 * CMD_SCSI, CMD_IOACCEL1 and CMD_IOACCEL2 commands by
215225163bd5SWebb Scales 	 * fail_all_oustanding_cmds()
215325163bd5SWebb Scales 	 */
215425163bd5SWebb Scales 	if (unlikely(ei->CommandStatus == CMD_CTLR_LOCKUP)) {
215525163bd5SWebb Scales 		/* DID_NO_CONNECT will prevent a retry */
215625163bd5SWebb Scales 		cmd->result = DID_NO_CONNECT << 16;
21578a0ff92cSWebb Scales 		return hpsa_cmd_free_and_done(h, cp, cmd);
215825163bd5SWebb Scales 	}
215925163bd5SWebb Scales 
2160c349775eSScott Teel 	if (cp->cmd_type == CMD_IOACCEL2)
2161c349775eSScott Teel 		return process_ioaccel2_completion(h, cp, cmd, dev);
2162c349775eSScott Teel 
21636aa4c361SRobert Elliott 	scsi_set_resid(cmd, ei->ResidualCnt);
21648a0ff92cSWebb Scales 	if (ei->CommandStatus == 0)
21658a0ff92cSWebb Scales 		return hpsa_cmd_free_and_done(h, cp, cmd);
21666aa4c361SRobert Elliott 
2167e1f7de0cSMatt Gates 	/* For I/O accelerator commands, copy over some fields to the normal
2168e1f7de0cSMatt Gates 	 * CISS header used below for error handling.
2169e1f7de0cSMatt Gates 	 */
2170e1f7de0cSMatt Gates 	if (cp->cmd_type == CMD_IOACCEL1) {
2171e1f7de0cSMatt Gates 		struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
21722b08b3e9SDon Brace 		cp->Header.SGList = scsi_sg_count(cmd);
21732b08b3e9SDon Brace 		cp->Header.SGTotal = cpu_to_le16(cp->Header.SGList);
21742b08b3e9SDon Brace 		cp->Request.CDBLen = le16_to_cpu(c->io_flags) &
21752b08b3e9SDon Brace 			IOACCEL1_IOFLAGS_CDBLEN_MASK;
217650a0decfSStephen M. Cameron 		cp->Header.tag = c->tag;
2177e1f7de0cSMatt Gates 		memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
2178e1f7de0cSMatt Gates 		memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
2179283b4a9bSStephen M. Cameron 
2180283b4a9bSStephen M. Cameron 		/* Any RAID offload error results in retry which will use
2181283b4a9bSStephen M. Cameron 		 * the normal I/O path so the controller can handle whatever's
2182283b4a9bSStephen M. Cameron 		 * wrong.
2183283b4a9bSStephen M. Cameron 		 */
2184283b4a9bSStephen M. Cameron 		if (is_logical_dev_addr_mode(dev->scsi3addr)) {
2185283b4a9bSStephen M. Cameron 			if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
2186283b4a9bSStephen M. Cameron 				dev->offload_enabled = 0;
2187a58e7e53SWebb Scales 			if (!cp->abort_pending)
21888a0ff92cSWebb Scales 				return hpsa_retry_cmd(h, cp);
2189283b4a9bSStephen M. Cameron 		}
2190e1f7de0cSMatt Gates 	}
2191e1f7de0cSMatt Gates 
2192a58e7e53SWebb Scales 	if (cp->abort_pending)
2193a58e7e53SWebb Scales 		ei->CommandStatus = CMD_ABORTED;
2194a58e7e53SWebb Scales 
2195edd16368SStephen M. Cameron 	/* an error has occurred */
2196edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
2197edd16368SStephen M. Cameron 
2198edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
21999437ac43SStephen Cameron 		cmd->result |= ei->ScsiStatus;
22009437ac43SStephen Cameron 		/* copy the sense data */
22019437ac43SStephen Cameron 		if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
22029437ac43SStephen Cameron 			sense_data_size = SCSI_SENSE_BUFFERSIZE;
22039437ac43SStephen Cameron 		else
22049437ac43SStephen Cameron 			sense_data_size = sizeof(ei->SenseInfo);
22059437ac43SStephen Cameron 		if (ei->SenseLen < sense_data_size)
22069437ac43SStephen Cameron 			sense_data_size = ei->SenseLen;
22079437ac43SStephen Cameron 		memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
22089437ac43SStephen Cameron 		if (ei->ScsiStatus)
22099437ac43SStephen Cameron 			decode_sense_data(ei->SenseInfo, sense_data_size,
22109437ac43SStephen Cameron 				&sense_key, &asc, &ascq);
2211edd16368SStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
22121d3b3609SMatt Gates 			if (sense_key == ABORTED_COMMAND) {
22132e311fbaSStephen M. Cameron 				cmd->result |= DID_SOFT_ERROR << 16;
22141d3b3609SMatt Gates 				break;
22151d3b3609SMatt Gates 			}
2216edd16368SStephen M. Cameron 			break;
2217edd16368SStephen M. Cameron 		}
2218edd16368SStephen M. Cameron 		/* Problem was not a check condition
2219edd16368SStephen M. Cameron 		 * Pass it up to the upper layers...
2220edd16368SStephen M. Cameron 		 */
2221edd16368SStephen M. Cameron 		if (ei->ScsiStatus) {
2222edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
2223edd16368SStephen M. Cameron 				"Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
2224edd16368SStephen M. Cameron 				"Returning result: 0x%x\n",
2225edd16368SStephen M. Cameron 				cp, ei->ScsiStatus,
2226edd16368SStephen M. Cameron 				sense_key, asc, ascq,
2227edd16368SStephen M. Cameron 				cmd->result);
2228edd16368SStephen M. Cameron 		} else {  /* scsi status is zero??? How??? */
2229edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
2230edd16368SStephen M. Cameron 				"Returning no connection.\n", cp),
2231edd16368SStephen M. Cameron 
2232edd16368SStephen M. Cameron 			/* Ordinarily, this case should never happen,
2233edd16368SStephen M. Cameron 			 * but there is a bug in some released firmware
2234edd16368SStephen M. Cameron 			 * revisions that allows it to happen if, for
2235edd16368SStephen M. Cameron 			 * example, a 4100 backplane loses power and
2236edd16368SStephen M. Cameron 			 * the tape drive is in it.  We assume that
2237edd16368SStephen M. Cameron 			 * it's a fatal error of some kind because we
2238edd16368SStephen M. Cameron 			 * can't show that it wasn't. We will make it
2239edd16368SStephen M. Cameron 			 * look like selection timeout since that is
2240edd16368SStephen M. Cameron 			 * the most common reason for this to occur,
2241edd16368SStephen M. Cameron 			 * and it's severe enough.
2242edd16368SStephen M. Cameron 			 */
2243edd16368SStephen M. Cameron 
2244edd16368SStephen M. Cameron 			cmd->result = DID_NO_CONNECT << 16;
2245edd16368SStephen M. Cameron 		}
2246edd16368SStephen M. Cameron 		break;
2247edd16368SStephen M. Cameron 
2248edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
2249edd16368SStephen M. Cameron 		break;
2250edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
2251f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev,
2252f42e81e1SStephen Cameron 			"CDB %16phN data overrun\n", cp->Request.CDB);
2253edd16368SStephen M. Cameron 		break;
2254edd16368SStephen M. Cameron 	case CMD_INVALID: {
2255edd16368SStephen M. Cameron 		/* print_bytes(cp, sizeof(*cp), 1, 0);
2256edd16368SStephen M. Cameron 		print_cmd(cp); */
2257edd16368SStephen M. Cameron 		/* We get CMD_INVALID if you address a non-existent device
2258edd16368SStephen M. Cameron 		 * instead of a selection timeout (no response).  You will
2259edd16368SStephen M. Cameron 		 * see this if you yank out a drive, then try to access it.
2260edd16368SStephen M. Cameron 		 * This is kind of a shame because it means that any other
2261edd16368SStephen M. Cameron 		 * CMD_INVALID (e.g. driver bug) will get interpreted as a
2262edd16368SStephen M. Cameron 		 * missing target. */
2263edd16368SStephen M. Cameron 		cmd->result = DID_NO_CONNECT << 16;
2264edd16368SStephen M. Cameron 	}
2265edd16368SStephen M. Cameron 		break;
2266edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
2267256d0eaaSStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2268f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : protocol error\n",
2269f42e81e1SStephen Cameron 				cp->Request.CDB);
2270edd16368SStephen M. Cameron 		break;
2271edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
2272edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2273f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : hardware error\n",
2274f42e81e1SStephen Cameron 			cp->Request.CDB);
2275edd16368SStephen M. Cameron 		break;
2276edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
2277edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2278f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : connection lost\n",
2279f42e81e1SStephen Cameron 			cp->Request.CDB);
2280edd16368SStephen M. Cameron 		break;
2281edd16368SStephen M. Cameron 	case CMD_ABORTED:
2282a58e7e53SWebb Scales 		/* Return now to avoid calling scsi_done(). */
2283a58e7e53SWebb Scales 		return hpsa_cmd_abort_and_free(h, cp, cmd);
2284edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
2285edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2286f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : abort failed\n",
2287f42e81e1SStephen Cameron 			cp->Request.CDB);
2288edd16368SStephen M. Cameron 		break;
2289edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
2290f6e76055SStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
2291f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : unsolicited abort\n",
2292f42e81e1SStephen Cameron 			cp->Request.CDB);
2293edd16368SStephen M. Cameron 		break;
2294edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
2295edd16368SStephen M. Cameron 		cmd->result = DID_TIME_OUT << 16;
2296f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN timed out\n",
2297f42e81e1SStephen Cameron 			cp->Request.CDB);
2298edd16368SStephen M. Cameron 		break;
22991d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
23001d5e2ed0SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
23011d5e2ed0SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Command unabortable\n");
23021d5e2ed0SStephen M. Cameron 		break;
23039437ac43SStephen Cameron 	case CMD_TMF_STATUS:
23049437ac43SStephen Cameron 		if (hpsa_evaluate_tmf_status(h, cp)) /* TMF failed? */
23059437ac43SStephen Cameron 			cmd->result = DID_ERROR << 16;
23069437ac43SStephen Cameron 		break;
2307283b4a9bSStephen M. Cameron 	case CMD_IOACCEL_DISABLED:
2308283b4a9bSStephen M. Cameron 		/* This only handles the direct pass-through case since RAID
2309283b4a9bSStephen M. Cameron 		 * offload is handled above.  Just attempt a retry.
2310283b4a9bSStephen M. Cameron 		 */
2311283b4a9bSStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16;
2312283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev,
2313283b4a9bSStephen M. Cameron 				"cp %p had HP SSD Smart Path error\n", cp);
2314283b4a9bSStephen M. Cameron 		break;
2315edd16368SStephen M. Cameron 	default:
2316edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2317edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
2318edd16368SStephen M. Cameron 				cp, ei->CommandStatus);
2319edd16368SStephen M. Cameron 	}
23208a0ff92cSWebb Scales 
23218a0ff92cSWebb Scales 	return hpsa_cmd_free_and_done(h, cp, cmd);
2322edd16368SStephen M. Cameron }
2323edd16368SStephen M. Cameron 
2324edd16368SStephen M. Cameron static void hpsa_pci_unmap(struct pci_dev *pdev,
2325edd16368SStephen M. Cameron 	struct CommandList *c, int sg_used, int data_direction)
2326edd16368SStephen M. Cameron {
2327edd16368SStephen M. Cameron 	int i;
2328edd16368SStephen M. Cameron 
232950a0decfSStephen M. Cameron 	for (i = 0; i < sg_used; i++)
233050a0decfSStephen M. Cameron 		pci_unmap_single(pdev, (dma_addr_t) le64_to_cpu(c->SG[i].Addr),
233150a0decfSStephen M. Cameron 				le32_to_cpu(c->SG[i].Len),
2332edd16368SStephen M. Cameron 				data_direction);
2333edd16368SStephen M. Cameron }
2334edd16368SStephen M. Cameron 
2335a2dac136SStephen M. Cameron static int hpsa_map_one(struct pci_dev *pdev,
2336edd16368SStephen M. Cameron 		struct CommandList *cp,
2337edd16368SStephen M. Cameron 		unsigned char *buf,
2338edd16368SStephen M. Cameron 		size_t buflen,
2339edd16368SStephen M. Cameron 		int data_direction)
2340edd16368SStephen M. Cameron {
234101a02ffcSStephen M. Cameron 	u64 addr64;
2342edd16368SStephen M. Cameron 
2343edd16368SStephen M. Cameron 	if (buflen == 0 || data_direction == PCI_DMA_NONE) {
2344edd16368SStephen M. Cameron 		cp->Header.SGList = 0;
234550a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(0);
2346a2dac136SStephen M. Cameron 		return 0;
2347edd16368SStephen M. Cameron 	}
2348edd16368SStephen M. Cameron 
234950a0decfSStephen M. Cameron 	addr64 = pci_map_single(pdev, buf, buflen, data_direction);
2350eceaae18SShuah Khan 	if (dma_mapping_error(&pdev->dev, addr64)) {
2351a2dac136SStephen M. Cameron 		/* Prevent subsequent unmap of something never mapped */
2352eceaae18SShuah Khan 		cp->Header.SGList = 0;
235350a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(0);
2354a2dac136SStephen M. Cameron 		return -1;
2355eceaae18SShuah Khan 	}
235650a0decfSStephen M. Cameron 	cp->SG[0].Addr = cpu_to_le64(addr64);
235750a0decfSStephen M. Cameron 	cp->SG[0].Len = cpu_to_le32(buflen);
235850a0decfSStephen M. Cameron 	cp->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* we are not chaining */
235950a0decfSStephen M. Cameron 	cp->Header.SGList = 1;   /* no. SGs contig in this cmd */
236050a0decfSStephen M. Cameron 	cp->Header.SGTotal = cpu_to_le16(1); /* total sgs in cmd list */
2361a2dac136SStephen M. Cameron 	return 0;
2362edd16368SStephen M. Cameron }
2363edd16368SStephen M. Cameron 
236425163bd5SWebb Scales #define NO_TIMEOUT ((unsigned long) -1)
236525163bd5SWebb Scales #define DEFAULT_TIMEOUT 30000 /* milliseconds */
236625163bd5SWebb Scales static int hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
236725163bd5SWebb Scales 	struct CommandList *c, int reply_queue, unsigned long timeout_msecs)
2368edd16368SStephen M. Cameron {
2369edd16368SStephen M. Cameron 	DECLARE_COMPLETION_ONSTACK(wait);
2370edd16368SStephen M. Cameron 
2371edd16368SStephen M. Cameron 	c->waiting = &wait;
237225163bd5SWebb Scales 	__enqueue_cmd_and_start_io(h, c, reply_queue);
237325163bd5SWebb Scales 	if (timeout_msecs == NO_TIMEOUT) {
237425163bd5SWebb Scales 		/* TODO: get rid of this no-timeout thing */
237525163bd5SWebb Scales 		wait_for_completion_io(&wait);
237625163bd5SWebb Scales 		return IO_OK;
237725163bd5SWebb Scales 	}
237825163bd5SWebb Scales 	if (!wait_for_completion_io_timeout(&wait,
237925163bd5SWebb Scales 					msecs_to_jiffies(timeout_msecs))) {
238025163bd5SWebb Scales 		dev_warn(&h->pdev->dev, "Command timed out.\n");
238125163bd5SWebb Scales 		return -ETIMEDOUT;
238225163bd5SWebb Scales 	}
238325163bd5SWebb Scales 	return IO_OK;
238425163bd5SWebb Scales }
238525163bd5SWebb Scales 
238625163bd5SWebb Scales static int hpsa_scsi_do_simple_cmd(struct ctlr_info *h, struct CommandList *c,
238725163bd5SWebb Scales 				   int reply_queue, unsigned long timeout_msecs)
238825163bd5SWebb Scales {
238925163bd5SWebb Scales 	if (unlikely(lockup_detected(h))) {
239025163bd5SWebb Scales 		c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
239125163bd5SWebb Scales 		return IO_OK;
239225163bd5SWebb Scales 	}
239325163bd5SWebb Scales 	return hpsa_scsi_do_simple_cmd_core(h, c, reply_queue, timeout_msecs);
2394edd16368SStephen M. Cameron }
2395edd16368SStephen M. Cameron 
2396094963daSStephen M. Cameron static u32 lockup_detected(struct ctlr_info *h)
2397094963daSStephen M. Cameron {
2398094963daSStephen M. Cameron 	int cpu;
2399094963daSStephen M. Cameron 	u32 rc, *lockup_detected;
2400094963daSStephen M. Cameron 
2401094963daSStephen M. Cameron 	cpu = get_cpu();
2402094963daSStephen M. Cameron 	lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
2403094963daSStephen M. Cameron 	rc = *lockup_detected;
2404094963daSStephen M. Cameron 	put_cpu();
2405094963daSStephen M. Cameron 	return rc;
2406094963daSStephen M. Cameron }
2407094963daSStephen M. Cameron 
24089c2fc160SStephen M. Cameron #define MAX_DRIVER_CMD_RETRIES 25
240925163bd5SWebb Scales static int hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
241025163bd5SWebb Scales 	struct CommandList *c, int data_direction, unsigned long timeout_msecs)
2411edd16368SStephen M. Cameron {
24129c2fc160SStephen M. Cameron 	int backoff_time = 10, retry_count = 0;
241325163bd5SWebb Scales 	int rc;
2414edd16368SStephen M. Cameron 
2415edd16368SStephen M. Cameron 	do {
24167630abd0SJoe Perches 		memset(c->err_info, 0, sizeof(*c->err_info));
241725163bd5SWebb Scales 		rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
241825163bd5SWebb Scales 						  timeout_msecs);
241925163bd5SWebb Scales 		if (rc)
242025163bd5SWebb Scales 			break;
2421edd16368SStephen M. Cameron 		retry_count++;
24229c2fc160SStephen M. Cameron 		if (retry_count > 3) {
24239c2fc160SStephen M. Cameron 			msleep(backoff_time);
24249c2fc160SStephen M. Cameron 			if (backoff_time < 1000)
24259c2fc160SStephen M. Cameron 				backoff_time *= 2;
24269c2fc160SStephen M. Cameron 		}
2427852af20aSMatt Bondurant 	} while ((check_for_unit_attention(h, c) ||
24289c2fc160SStephen M. Cameron 			check_for_busy(h, c)) &&
24299c2fc160SStephen M. Cameron 			retry_count <= MAX_DRIVER_CMD_RETRIES);
2430edd16368SStephen M. Cameron 	hpsa_pci_unmap(h->pdev, c, 1, data_direction);
243125163bd5SWebb Scales 	if (retry_count > MAX_DRIVER_CMD_RETRIES)
243225163bd5SWebb Scales 		rc = -EIO;
243325163bd5SWebb Scales 	return rc;
2434edd16368SStephen M. Cameron }
2435edd16368SStephen M. Cameron 
2436d1e8beacSStephen M. Cameron static void hpsa_print_cmd(struct ctlr_info *h, char *txt,
2437d1e8beacSStephen M. Cameron 				struct CommandList *c)
2438edd16368SStephen M. Cameron {
2439d1e8beacSStephen M. Cameron 	const u8 *cdb = c->Request.CDB;
2440d1e8beacSStephen M. Cameron 	const u8 *lun = c->Header.LUN.LunAddrBytes;
2441edd16368SStephen M. Cameron 
2442d1e8beacSStephen M. Cameron 	dev_warn(&h->pdev->dev, "%s: LUN:%02x%02x%02x%02x%02x%02x%02x%02x"
2443d1e8beacSStephen M. Cameron 	" CDB:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
2444d1e8beacSStephen M. Cameron 		txt, lun[0], lun[1], lun[2], lun[3],
2445d1e8beacSStephen M. Cameron 		lun[4], lun[5], lun[6], lun[7],
2446d1e8beacSStephen M. Cameron 		cdb[0], cdb[1], cdb[2], cdb[3],
2447d1e8beacSStephen M. Cameron 		cdb[4], cdb[5], cdb[6], cdb[7],
2448d1e8beacSStephen M. Cameron 		cdb[8], cdb[9], cdb[10], cdb[11],
2449d1e8beacSStephen M. Cameron 		cdb[12], cdb[13], cdb[14], cdb[15]);
2450d1e8beacSStephen M. Cameron }
2451d1e8beacSStephen M. Cameron 
2452d1e8beacSStephen M. Cameron static void hpsa_scsi_interpret_error(struct ctlr_info *h,
2453d1e8beacSStephen M. Cameron 			struct CommandList *cp)
2454d1e8beacSStephen M. Cameron {
2455d1e8beacSStephen M. Cameron 	const struct ErrorInfo *ei = cp->err_info;
2456d1e8beacSStephen M. Cameron 	struct device *d = &cp->h->pdev->dev;
24579437ac43SStephen Cameron 	u8 sense_key, asc, ascq;
24589437ac43SStephen Cameron 	int sense_len;
2459d1e8beacSStephen M. Cameron 
2460edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
2461edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
24629437ac43SStephen Cameron 		if (ei->SenseLen > sizeof(ei->SenseInfo))
24639437ac43SStephen Cameron 			sense_len = sizeof(ei->SenseInfo);
24649437ac43SStephen Cameron 		else
24659437ac43SStephen Cameron 			sense_len = ei->SenseLen;
24669437ac43SStephen Cameron 		decode_sense_data(ei->SenseInfo, sense_len,
24679437ac43SStephen Cameron 					&sense_key, &asc, &ascq);
2468d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "SCSI status", cp);
2469d1e8beacSStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION)
24709437ac43SStephen Cameron 			dev_warn(d, "SCSI Status = 02, Sense key = 0x%02x, ASC = 0x%02x, ASCQ = 0x%02x\n",
24719437ac43SStephen Cameron 				sense_key, asc, ascq);
2472d1e8beacSStephen M. Cameron 		else
24739437ac43SStephen Cameron 			dev_warn(d, "SCSI Status = 0x%02x\n", ei->ScsiStatus);
2474edd16368SStephen M. Cameron 		if (ei->ScsiStatus == 0)
2475edd16368SStephen M. Cameron 			dev_warn(d, "SCSI status is abnormally zero.  "
2476edd16368SStephen M. Cameron 			"(probably indicates selection timeout "
2477edd16368SStephen M. Cameron 			"reported incorrectly due to a known "
2478edd16368SStephen M. Cameron 			"firmware bug, circa July, 2001.)\n");
2479edd16368SStephen M. Cameron 		break;
2480edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
2481edd16368SStephen M. Cameron 		break;
2482edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
2483d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "overrun condition", cp);
2484edd16368SStephen M. Cameron 		break;
2485edd16368SStephen M. Cameron 	case CMD_INVALID: {
2486edd16368SStephen M. Cameron 		/* controller unfortunately reports SCSI passthru's
2487edd16368SStephen M. Cameron 		 * to non-existent targets as invalid commands.
2488edd16368SStephen M. Cameron 		 */
2489d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "invalid command", cp);
2490d1e8beacSStephen M. Cameron 		dev_warn(d, "probably means device no longer present\n");
2491edd16368SStephen M. Cameron 		}
2492edd16368SStephen M. Cameron 		break;
2493edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
2494d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "protocol error", cp);
2495edd16368SStephen M. Cameron 		break;
2496edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
2497d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "hardware error", cp);
2498edd16368SStephen M. Cameron 		break;
2499edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
2500d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "connection lost", cp);
2501edd16368SStephen M. Cameron 		break;
2502edd16368SStephen M. Cameron 	case CMD_ABORTED:
2503d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "aborted", cp);
2504edd16368SStephen M. Cameron 		break;
2505edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
2506d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "abort failed", cp);
2507edd16368SStephen M. Cameron 		break;
2508edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
2509d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unsolicited abort", cp);
2510edd16368SStephen M. Cameron 		break;
2511edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
2512d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "timed out", cp);
2513edd16368SStephen M. Cameron 		break;
25141d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
2515d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unabortable", cp);
25161d5e2ed0SStephen M. Cameron 		break;
251725163bd5SWebb Scales 	case CMD_CTLR_LOCKUP:
251825163bd5SWebb Scales 		hpsa_print_cmd(h, "controller lockup detected", cp);
251925163bd5SWebb Scales 		break;
2520edd16368SStephen M. Cameron 	default:
2521d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unknown status", cp);
2522d1e8beacSStephen M. Cameron 		dev_warn(d, "Unknown command status %x\n",
2523edd16368SStephen M. Cameron 				ei->CommandStatus);
2524edd16368SStephen M. Cameron 	}
2525edd16368SStephen M. Cameron }
2526edd16368SStephen M. Cameron 
2527edd16368SStephen M. Cameron static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
2528b7bb24ebSStephen M. Cameron 			u16 page, unsigned char *buf,
2529edd16368SStephen M. Cameron 			unsigned char bufsize)
2530edd16368SStephen M. Cameron {
2531edd16368SStephen M. Cameron 	int rc = IO_OK;
2532edd16368SStephen M. Cameron 	struct CommandList *c;
2533edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2534edd16368SStephen M. Cameron 
253545fcb86eSStephen Cameron 	c = cmd_alloc(h);
2536edd16368SStephen M. Cameron 
2537a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
2538a2dac136SStephen M. Cameron 			page, scsi3addr, TYPE_CMD)) {
2539a2dac136SStephen M. Cameron 		rc = -1;
2540a2dac136SStephen M. Cameron 		goto out;
2541a2dac136SStephen M. Cameron 	}
254225163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
254325163bd5SWebb Scales 					PCI_DMA_FROMDEVICE, NO_TIMEOUT);
254425163bd5SWebb Scales 	if (rc)
254525163bd5SWebb Scales 		goto out;
2546edd16368SStephen M. Cameron 	ei = c->err_info;
2547edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2548d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2549edd16368SStephen M. Cameron 		rc = -1;
2550edd16368SStephen M. Cameron 	}
2551a2dac136SStephen M. Cameron out:
255245fcb86eSStephen Cameron 	cmd_free(h, c);
2553edd16368SStephen M. Cameron 	return rc;
2554edd16368SStephen M. Cameron }
2555edd16368SStephen M. Cameron 
2556316b221aSStephen M. Cameron static int hpsa_bmic_ctrl_mode_sense(struct ctlr_info *h,
2557316b221aSStephen M. Cameron 		unsigned char *scsi3addr, unsigned char page,
2558316b221aSStephen M. Cameron 		struct bmic_controller_parameters *buf, size_t bufsize)
2559316b221aSStephen M. Cameron {
2560316b221aSStephen M. Cameron 	int rc = IO_OK;
2561316b221aSStephen M. Cameron 	struct CommandList *c;
2562316b221aSStephen M. Cameron 	struct ErrorInfo *ei;
2563316b221aSStephen M. Cameron 
256445fcb86eSStephen Cameron 	c = cmd_alloc(h);
2565316b221aSStephen M. Cameron 	if (fill_cmd(c, BMIC_SENSE_CONTROLLER_PARAMETERS, h, buf, bufsize,
2566316b221aSStephen M. Cameron 			page, scsi3addr, TYPE_CMD)) {
2567316b221aSStephen M. Cameron 		rc = -1;
2568316b221aSStephen M. Cameron 		goto out;
2569316b221aSStephen M. Cameron 	}
257025163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
257125163bd5SWebb Scales 			PCI_DMA_FROMDEVICE, NO_TIMEOUT);
257225163bd5SWebb Scales 	if (rc)
257325163bd5SWebb Scales 		goto out;
2574316b221aSStephen M. Cameron 	ei = c->err_info;
2575316b221aSStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2576316b221aSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2577316b221aSStephen M. Cameron 		rc = -1;
2578316b221aSStephen M. Cameron 	}
2579316b221aSStephen M. Cameron out:
258045fcb86eSStephen Cameron 	cmd_free(h, c);
2581316b221aSStephen M. Cameron 	return rc;
2582316b221aSStephen M. Cameron }
2583316b221aSStephen M. Cameron 
2584bf711ac6SScott Teel static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
258525163bd5SWebb Scales 	u8 reset_type, int reply_queue)
2586edd16368SStephen M. Cameron {
2587edd16368SStephen M. Cameron 	int rc = IO_OK;
2588edd16368SStephen M. Cameron 	struct CommandList *c;
2589edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2590edd16368SStephen M. Cameron 
259145fcb86eSStephen Cameron 	c = cmd_alloc(h);
2592edd16368SStephen M. Cameron 
2593edd16368SStephen M. Cameron 
2594a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map. */
2595bf711ac6SScott Teel 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
2596bf711ac6SScott Teel 			scsi3addr, TYPE_MSG);
2597bf711ac6SScott Teel 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to LUN reset */
259825163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
259925163bd5SWebb Scales 	if (rc) {
260025163bd5SWebb Scales 		dev_warn(&h->pdev->dev, "Failed to send reset command\n");
260125163bd5SWebb Scales 		goto out;
260225163bd5SWebb Scales 	}
2603edd16368SStephen M. Cameron 	/* no unmap needed here because no data xfer. */
2604edd16368SStephen M. Cameron 
2605edd16368SStephen M. Cameron 	ei = c->err_info;
2606edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0) {
2607d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2608edd16368SStephen M. Cameron 		rc = -1;
2609edd16368SStephen M. Cameron 	}
261025163bd5SWebb Scales out:
261145fcb86eSStephen Cameron 	cmd_free(h, c);
2612edd16368SStephen M. Cameron 	return rc;
2613edd16368SStephen M. Cameron }
2614edd16368SStephen M. Cameron 
2615edd16368SStephen M. Cameron static void hpsa_get_raid_level(struct ctlr_info *h,
2616edd16368SStephen M. Cameron 	unsigned char *scsi3addr, unsigned char *raid_level)
2617edd16368SStephen M. Cameron {
2618edd16368SStephen M. Cameron 	int rc;
2619edd16368SStephen M. Cameron 	unsigned char *buf;
2620edd16368SStephen M. Cameron 
2621edd16368SStephen M. Cameron 	*raid_level = RAID_UNKNOWN;
2622edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2623edd16368SStephen M. Cameron 	if (!buf)
2624edd16368SStephen M. Cameron 		return;
2625b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0xC1, buf, 64);
2626edd16368SStephen M. Cameron 	if (rc == 0)
2627edd16368SStephen M. Cameron 		*raid_level = buf[8];
2628edd16368SStephen M. Cameron 	if (*raid_level > RAID_UNKNOWN)
2629edd16368SStephen M. Cameron 		*raid_level = RAID_UNKNOWN;
2630edd16368SStephen M. Cameron 	kfree(buf);
2631edd16368SStephen M. Cameron 	return;
2632edd16368SStephen M. Cameron }
2633edd16368SStephen M. Cameron 
2634283b4a9bSStephen M. Cameron #define HPSA_MAP_DEBUG
2635283b4a9bSStephen M. Cameron #ifdef HPSA_MAP_DEBUG
2636283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
2637283b4a9bSStephen M. Cameron 				struct raid_map_data *map_buff)
2638283b4a9bSStephen M. Cameron {
2639283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map_buff->data[0];
2640283b4a9bSStephen M. Cameron 	int map, row, col;
2641283b4a9bSStephen M. Cameron 	u16 map_cnt, row_cnt, disks_per_row;
2642283b4a9bSStephen M. Cameron 
2643283b4a9bSStephen M. Cameron 	if (rc != 0)
2644283b4a9bSStephen M. Cameron 		return;
2645283b4a9bSStephen M. Cameron 
26462ba8bfc8SStephen M. Cameron 	/* Show details only if debugging has been activated. */
26472ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug < 2)
26482ba8bfc8SStephen M. Cameron 		return;
26492ba8bfc8SStephen M. Cameron 
2650283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "structure_size = %u\n",
2651283b4a9bSStephen M. Cameron 				le32_to_cpu(map_buff->structure_size));
2652283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
2653283b4a9bSStephen M. Cameron 			le32_to_cpu(map_buff->volume_blk_size));
2654283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
2655283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->volume_blk_cnt));
2656283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
2657283b4a9bSStephen M. Cameron 			map_buff->phys_blk_shift);
2658283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
2659283b4a9bSStephen M. Cameron 			map_buff->parity_rotation_shift);
2660283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "strip_size = %u\n",
2661283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->strip_size));
2662283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
2663283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_starting_blk));
2664283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
2665283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_blk_cnt));
2666283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
2667283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->data_disks_per_row));
2668283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
2669283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->metadata_disks_per_row));
2670283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "row_cnt = %u\n",
2671283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->row_cnt));
2672283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "layout_map_count = %u\n",
2673283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->layout_map_count));
26742b08b3e9SDon Brace 	dev_info(&h->pdev->dev, "flags = 0x%x\n",
2675dd0e19f3SScott Teel 			le16_to_cpu(map_buff->flags));
26762b08b3e9SDon Brace 	dev_info(&h->pdev->dev, "encrypytion = %s\n",
26772b08b3e9SDon Brace 			le16_to_cpu(map_buff->flags) &
26782b08b3e9SDon Brace 			RAID_MAP_FLAG_ENCRYPT_ON ?  "ON" : "OFF");
2679dd0e19f3SScott Teel 	dev_info(&h->pdev->dev, "dekindex = %u\n",
2680dd0e19f3SScott Teel 			le16_to_cpu(map_buff->dekindex));
2681283b4a9bSStephen M. Cameron 	map_cnt = le16_to_cpu(map_buff->layout_map_count);
2682283b4a9bSStephen M. Cameron 	for (map = 0; map < map_cnt; map++) {
2683283b4a9bSStephen M. Cameron 		dev_info(&h->pdev->dev, "Map%u:\n", map);
2684283b4a9bSStephen M. Cameron 		row_cnt = le16_to_cpu(map_buff->row_cnt);
2685283b4a9bSStephen M. Cameron 		for (row = 0; row < row_cnt; row++) {
2686283b4a9bSStephen M. Cameron 			dev_info(&h->pdev->dev, "  Row%u:\n", row);
2687283b4a9bSStephen M. Cameron 			disks_per_row =
2688283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->data_disks_per_row);
2689283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2690283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2691283b4a9bSStephen M. Cameron 					"    D%02u: h=0x%04x xor=%u,%u\n",
2692283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2693283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2694283b4a9bSStephen M. Cameron 			disks_per_row =
2695283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->metadata_disks_per_row);
2696283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2697283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2698283b4a9bSStephen M. Cameron 					"    M%02u: h=0x%04x xor=%u,%u\n",
2699283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2700283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2701283b4a9bSStephen M. Cameron 		}
2702283b4a9bSStephen M. Cameron 	}
2703283b4a9bSStephen M. Cameron }
2704283b4a9bSStephen M. Cameron #else
2705283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
2706283b4a9bSStephen M. Cameron 			__attribute__((unused)) int rc,
2707283b4a9bSStephen M. Cameron 			__attribute__((unused)) struct raid_map_data *map_buff)
2708283b4a9bSStephen M. Cameron {
2709283b4a9bSStephen M. Cameron }
2710283b4a9bSStephen M. Cameron #endif
2711283b4a9bSStephen M. Cameron 
2712283b4a9bSStephen M. Cameron static int hpsa_get_raid_map(struct ctlr_info *h,
2713283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2714283b4a9bSStephen M. Cameron {
2715283b4a9bSStephen M. Cameron 	int rc = 0;
2716283b4a9bSStephen M. Cameron 	struct CommandList *c;
2717283b4a9bSStephen M. Cameron 	struct ErrorInfo *ei;
2718283b4a9bSStephen M. Cameron 
271945fcb86eSStephen Cameron 	c = cmd_alloc(h);
2720bf43caf3SRobert Elliott 
2721283b4a9bSStephen M. Cameron 	if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
2722283b4a9bSStephen M. Cameron 			sizeof(this_device->raid_map), 0,
2723283b4a9bSStephen M. Cameron 			scsi3addr, TYPE_CMD)) {
27242dd02d74SRobert Elliott 		dev_warn(&h->pdev->dev, "hpsa_get_raid_map fill_cmd failed\n");
27252dd02d74SRobert Elliott 		cmd_free(h, c);
27262dd02d74SRobert Elliott 		return -1;
2727283b4a9bSStephen M. Cameron 	}
272825163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
272925163bd5SWebb Scales 					PCI_DMA_FROMDEVICE, NO_TIMEOUT);
273025163bd5SWebb Scales 	if (rc)
273125163bd5SWebb Scales 		goto out;
2732283b4a9bSStephen M. Cameron 	ei = c->err_info;
2733283b4a9bSStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2734d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
273525163bd5SWebb Scales 		rc = -1;
273625163bd5SWebb Scales 		goto out;
2737283b4a9bSStephen M. Cameron 	}
273845fcb86eSStephen Cameron 	cmd_free(h, c);
2739283b4a9bSStephen M. Cameron 
2740283b4a9bSStephen M. Cameron 	/* @todo in the future, dynamically allocate RAID map memory */
2741283b4a9bSStephen M. Cameron 	if (le32_to_cpu(this_device->raid_map.structure_size) >
2742283b4a9bSStephen M. Cameron 				sizeof(this_device->raid_map)) {
2743283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
2744283b4a9bSStephen M. Cameron 		rc = -1;
2745283b4a9bSStephen M. Cameron 	}
2746283b4a9bSStephen M. Cameron 	hpsa_debug_map_buff(h, rc, &this_device->raid_map);
2747283b4a9bSStephen M. Cameron 	return rc;
274825163bd5SWebb Scales out:
274925163bd5SWebb Scales 	cmd_free(h, c);
275025163bd5SWebb Scales 	return rc;
2751283b4a9bSStephen M. Cameron }
2752283b4a9bSStephen M. Cameron 
275303383736SDon Brace static int hpsa_bmic_id_physical_device(struct ctlr_info *h,
275403383736SDon Brace 		unsigned char scsi3addr[], u16 bmic_device_index,
275503383736SDon Brace 		struct bmic_identify_physical_device *buf, size_t bufsize)
275603383736SDon Brace {
275703383736SDon Brace 	int rc = IO_OK;
275803383736SDon Brace 	struct CommandList *c;
275903383736SDon Brace 	struct ErrorInfo *ei;
276003383736SDon Brace 
276103383736SDon Brace 	c = cmd_alloc(h);
276203383736SDon Brace 	rc = fill_cmd(c, BMIC_IDENTIFY_PHYSICAL_DEVICE, h, buf, bufsize,
276303383736SDon Brace 		0, RAID_CTLR_LUNID, TYPE_CMD);
276403383736SDon Brace 	if (rc)
276503383736SDon Brace 		goto out;
276603383736SDon Brace 
276703383736SDon Brace 	c->Request.CDB[2] = bmic_device_index & 0xff;
276803383736SDon Brace 	c->Request.CDB[9] = (bmic_device_index >> 8) & 0xff;
276903383736SDon Brace 
277025163bd5SWebb Scales 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE,
277125163bd5SWebb Scales 						NO_TIMEOUT);
277203383736SDon Brace 	ei = c->err_info;
277303383736SDon Brace 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
277403383736SDon Brace 		hpsa_scsi_interpret_error(h, c);
277503383736SDon Brace 		rc = -1;
277603383736SDon Brace 	}
277703383736SDon Brace out:
277803383736SDon Brace 	cmd_free(h, c);
277903383736SDon Brace 	return rc;
278003383736SDon Brace }
278103383736SDon Brace 
27821b70150aSStephen M. Cameron static int hpsa_vpd_page_supported(struct ctlr_info *h,
27831b70150aSStephen M. Cameron 	unsigned char scsi3addr[], u8 page)
27841b70150aSStephen M. Cameron {
27851b70150aSStephen M. Cameron 	int rc;
27861b70150aSStephen M. Cameron 	int i;
27871b70150aSStephen M. Cameron 	int pages;
27881b70150aSStephen M. Cameron 	unsigned char *buf, bufsize;
27891b70150aSStephen M. Cameron 
27901b70150aSStephen M. Cameron 	buf = kzalloc(256, GFP_KERNEL);
27911b70150aSStephen M. Cameron 	if (!buf)
27921b70150aSStephen M. Cameron 		return 0;
27931b70150aSStephen M. Cameron 
27941b70150aSStephen M. Cameron 	/* Get the size of the page list first */
27951b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
27961b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
27971b70150aSStephen M. Cameron 				buf, HPSA_VPD_HEADER_SZ);
27981b70150aSStephen M. Cameron 	if (rc != 0)
27991b70150aSStephen M. Cameron 		goto exit_unsupported;
28001b70150aSStephen M. Cameron 	pages = buf[3];
28011b70150aSStephen M. Cameron 	if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
28021b70150aSStephen M. Cameron 		bufsize = pages + HPSA_VPD_HEADER_SZ;
28031b70150aSStephen M. Cameron 	else
28041b70150aSStephen M. Cameron 		bufsize = 255;
28051b70150aSStephen M. Cameron 
28061b70150aSStephen M. Cameron 	/* Get the whole VPD page list */
28071b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
28081b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
28091b70150aSStephen M. Cameron 				buf, bufsize);
28101b70150aSStephen M. Cameron 	if (rc != 0)
28111b70150aSStephen M. Cameron 		goto exit_unsupported;
28121b70150aSStephen M. Cameron 
28131b70150aSStephen M. Cameron 	pages = buf[3];
28141b70150aSStephen M. Cameron 	for (i = 1; i <= pages; i++)
28151b70150aSStephen M. Cameron 		if (buf[3 + i] == page)
28161b70150aSStephen M. Cameron 			goto exit_supported;
28171b70150aSStephen M. Cameron exit_unsupported:
28181b70150aSStephen M. Cameron 	kfree(buf);
28191b70150aSStephen M. Cameron 	return 0;
28201b70150aSStephen M. Cameron exit_supported:
28211b70150aSStephen M. Cameron 	kfree(buf);
28221b70150aSStephen M. Cameron 	return 1;
28231b70150aSStephen M. Cameron }
28241b70150aSStephen M. Cameron 
2825283b4a9bSStephen M. Cameron static void hpsa_get_ioaccel_status(struct ctlr_info *h,
2826283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2827283b4a9bSStephen M. Cameron {
2828283b4a9bSStephen M. Cameron 	int rc;
2829283b4a9bSStephen M. Cameron 	unsigned char *buf;
2830283b4a9bSStephen M. Cameron 	u8 ioaccel_status;
2831283b4a9bSStephen M. Cameron 
2832283b4a9bSStephen M. Cameron 	this_device->offload_config = 0;
2833283b4a9bSStephen M. Cameron 	this_device->offload_enabled = 0;
283441ce4c35SStephen Cameron 	this_device->offload_to_be_enabled = 0;
2835283b4a9bSStephen M. Cameron 
2836283b4a9bSStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2837283b4a9bSStephen M. Cameron 	if (!buf)
2838283b4a9bSStephen M. Cameron 		return;
28391b70150aSStephen M. Cameron 	if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
28401b70150aSStephen M. Cameron 		goto out;
2841283b4a9bSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
2842b7bb24ebSStephen M. Cameron 			VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
2843283b4a9bSStephen M. Cameron 	if (rc != 0)
2844283b4a9bSStephen M. Cameron 		goto out;
2845283b4a9bSStephen M. Cameron 
2846283b4a9bSStephen M. Cameron #define IOACCEL_STATUS_BYTE 4
2847283b4a9bSStephen M. Cameron #define OFFLOAD_CONFIGURED_BIT 0x01
2848283b4a9bSStephen M. Cameron #define OFFLOAD_ENABLED_BIT 0x02
2849283b4a9bSStephen M. Cameron 	ioaccel_status = buf[IOACCEL_STATUS_BYTE];
2850283b4a9bSStephen M. Cameron 	this_device->offload_config =
2851283b4a9bSStephen M. Cameron 		!!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
2852283b4a9bSStephen M. Cameron 	if (this_device->offload_config) {
2853283b4a9bSStephen M. Cameron 		this_device->offload_enabled =
2854283b4a9bSStephen M. Cameron 			!!(ioaccel_status & OFFLOAD_ENABLED_BIT);
2855283b4a9bSStephen M. Cameron 		if (hpsa_get_raid_map(h, scsi3addr, this_device))
2856283b4a9bSStephen M. Cameron 			this_device->offload_enabled = 0;
2857283b4a9bSStephen M. Cameron 	}
285841ce4c35SStephen Cameron 	this_device->offload_to_be_enabled = this_device->offload_enabled;
2859283b4a9bSStephen M. Cameron out:
2860283b4a9bSStephen M. Cameron 	kfree(buf);
2861283b4a9bSStephen M. Cameron 	return;
2862283b4a9bSStephen M. Cameron }
2863283b4a9bSStephen M. Cameron 
2864edd16368SStephen M. Cameron /* Get the device id from inquiry page 0x83 */
2865edd16368SStephen M. Cameron static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
2866edd16368SStephen M. Cameron 	unsigned char *device_id, int buflen)
2867edd16368SStephen M. Cameron {
2868edd16368SStephen M. Cameron 	int rc;
2869edd16368SStephen M. Cameron 	unsigned char *buf;
2870edd16368SStephen M. Cameron 
2871edd16368SStephen M. Cameron 	if (buflen > 16)
2872edd16368SStephen M. Cameron 		buflen = 16;
2873edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2874edd16368SStephen M. Cameron 	if (!buf)
2875a84d794dSStephen M. Cameron 		return -ENOMEM;
2876b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0x83, buf, 64);
2877edd16368SStephen M. Cameron 	if (rc == 0)
2878edd16368SStephen M. Cameron 		memcpy(device_id, &buf[8], buflen);
2879edd16368SStephen M. Cameron 	kfree(buf);
2880edd16368SStephen M. Cameron 	return rc != 0;
2881edd16368SStephen M. Cameron }
2882edd16368SStephen M. Cameron 
2883edd16368SStephen M. Cameron static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
288403383736SDon Brace 		void *buf, int bufsize,
2885edd16368SStephen M. Cameron 		int extended_response)
2886edd16368SStephen M. Cameron {
2887edd16368SStephen M. Cameron 	int rc = IO_OK;
2888edd16368SStephen M. Cameron 	struct CommandList *c;
2889edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
2890edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2891edd16368SStephen M. Cameron 
289245fcb86eSStephen Cameron 	c = cmd_alloc(h);
2893bf43caf3SRobert Elliott 
2894e89c0ae7SStephen M. Cameron 	/* address the controller */
2895e89c0ae7SStephen M. Cameron 	memset(scsi3addr, 0, sizeof(scsi3addr));
2896a2dac136SStephen M. Cameron 	if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
2897a2dac136SStephen M. Cameron 		buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
2898a2dac136SStephen M. Cameron 		rc = -1;
2899a2dac136SStephen M. Cameron 		goto out;
2900a2dac136SStephen M. Cameron 	}
2901edd16368SStephen M. Cameron 	if (extended_response)
2902edd16368SStephen M. Cameron 		c->Request.CDB[1] = extended_response;
290325163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
290425163bd5SWebb Scales 					PCI_DMA_FROMDEVICE, NO_TIMEOUT);
290525163bd5SWebb Scales 	if (rc)
290625163bd5SWebb Scales 		goto out;
2907edd16368SStephen M. Cameron 	ei = c->err_info;
2908edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 &&
2909edd16368SStephen M. Cameron 	    ei->CommandStatus != CMD_DATA_UNDERRUN) {
2910d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2911edd16368SStephen M. Cameron 		rc = -1;
2912283b4a9bSStephen M. Cameron 	} else {
291303383736SDon Brace 		struct ReportLUNdata *rld = buf;
291403383736SDon Brace 
291503383736SDon Brace 		if (rld->extended_response_flag != extended_response) {
2916283b4a9bSStephen M. Cameron 			dev_err(&h->pdev->dev,
2917283b4a9bSStephen M. Cameron 				"report luns requested format %u, got %u\n",
2918283b4a9bSStephen M. Cameron 				extended_response,
291903383736SDon Brace 				rld->extended_response_flag);
2920283b4a9bSStephen M. Cameron 			rc = -1;
2921283b4a9bSStephen M. Cameron 		}
2922edd16368SStephen M. Cameron 	}
2923a2dac136SStephen M. Cameron out:
292445fcb86eSStephen Cameron 	cmd_free(h, c);
2925edd16368SStephen M. Cameron 	return rc;
2926edd16368SStephen M. Cameron }
2927edd16368SStephen M. Cameron 
2928edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
292903383736SDon Brace 		struct ReportExtendedLUNdata *buf, int bufsize)
2930edd16368SStephen M. Cameron {
293103383736SDon Brace 	return hpsa_scsi_do_report_luns(h, 0, buf, bufsize,
293203383736SDon Brace 						HPSA_REPORT_PHYS_EXTENDED);
2933edd16368SStephen M. Cameron }
2934edd16368SStephen M. Cameron 
2935edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
2936edd16368SStephen M. Cameron 		struct ReportLUNdata *buf, int bufsize)
2937edd16368SStephen M. Cameron {
2938edd16368SStephen M. Cameron 	return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
2939edd16368SStephen M. Cameron }
2940edd16368SStephen M. Cameron 
2941edd16368SStephen M. Cameron static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
2942edd16368SStephen M. Cameron 	int bus, int target, int lun)
2943edd16368SStephen M. Cameron {
2944edd16368SStephen M. Cameron 	device->bus = bus;
2945edd16368SStephen M. Cameron 	device->target = target;
2946edd16368SStephen M. Cameron 	device->lun = lun;
2947edd16368SStephen M. Cameron }
2948edd16368SStephen M. Cameron 
29499846590eSStephen M. Cameron /* Use VPD inquiry to get details of volume status */
29509846590eSStephen M. Cameron static int hpsa_get_volume_status(struct ctlr_info *h,
29519846590eSStephen M. Cameron 					unsigned char scsi3addr[])
29529846590eSStephen M. Cameron {
29539846590eSStephen M. Cameron 	int rc;
29549846590eSStephen M. Cameron 	int status;
29559846590eSStephen M. Cameron 	int size;
29569846590eSStephen M. Cameron 	unsigned char *buf;
29579846590eSStephen M. Cameron 
29589846590eSStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
29599846590eSStephen M. Cameron 	if (!buf)
29609846590eSStephen M. Cameron 		return HPSA_VPD_LV_STATUS_UNSUPPORTED;
29619846590eSStephen M. Cameron 
29629846590eSStephen M. Cameron 	/* Does controller have VPD for logical volume status? */
296324a4b078SStephen M. Cameron 	if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_STATUS))
29649846590eSStephen M. Cameron 		goto exit_failed;
29659846590eSStephen M. Cameron 
29669846590eSStephen M. Cameron 	/* Get the size of the VPD return buffer */
29679846590eSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
29689846590eSStephen M. Cameron 					buf, HPSA_VPD_HEADER_SZ);
296924a4b078SStephen M. Cameron 	if (rc != 0)
29709846590eSStephen M. Cameron 		goto exit_failed;
29719846590eSStephen M. Cameron 	size = buf[3];
29729846590eSStephen M. Cameron 
29739846590eSStephen M. Cameron 	/* Now get the whole VPD buffer */
29749846590eSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
29759846590eSStephen M. Cameron 					buf, size + HPSA_VPD_HEADER_SZ);
297624a4b078SStephen M. Cameron 	if (rc != 0)
29779846590eSStephen M. Cameron 		goto exit_failed;
29789846590eSStephen M. Cameron 	status = buf[4]; /* status byte */
29799846590eSStephen M. Cameron 
29809846590eSStephen M. Cameron 	kfree(buf);
29819846590eSStephen M. Cameron 	return status;
29829846590eSStephen M. Cameron exit_failed:
29839846590eSStephen M. Cameron 	kfree(buf);
29849846590eSStephen M. Cameron 	return HPSA_VPD_LV_STATUS_UNSUPPORTED;
29859846590eSStephen M. Cameron }
29869846590eSStephen M. Cameron 
29879846590eSStephen M. Cameron /* Determine offline status of a volume.
29889846590eSStephen M. Cameron  * Return either:
29899846590eSStephen M. Cameron  *  0 (not offline)
299067955ba3SStephen M. Cameron  *  0xff (offline for unknown reasons)
29919846590eSStephen M. Cameron  *  # (integer code indicating one of several NOT READY states
29929846590eSStephen M. Cameron  *     describing why a volume is to be kept offline)
29939846590eSStephen M. Cameron  */
299467955ba3SStephen M. Cameron static int hpsa_volume_offline(struct ctlr_info *h,
29959846590eSStephen M. Cameron 					unsigned char scsi3addr[])
29969846590eSStephen M. Cameron {
29979846590eSStephen M. Cameron 	struct CommandList *c;
29989437ac43SStephen Cameron 	unsigned char *sense;
29999437ac43SStephen Cameron 	u8 sense_key, asc, ascq;
30009437ac43SStephen Cameron 	int sense_len;
300125163bd5SWebb Scales 	int rc, ldstat = 0;
30029846590eSStephen M. Cameron 	u16 cmd_status;
30039846590eSStephen M. Cameron 	u8 scsi_status;
30049846590eSStephen M. Cameron #define ASC_LUN_NOT_READY 0x04
30059846590eSStephen M. Cameron #define ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS 0x04
30069846590eSStephen M. Cameron #define ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ 0x02
30079846590eSStephen M. Cameron 
30089846590eSStephen M. Cameron 	c = cmd_alloc(h);
3009bf43caf3SRobert Elliott 
30109846590eSStephen M. Cameron 	(void) fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, scsi3addr, TYPE_CMD);
301125163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
301225163bd5SWebb Scales 	if (rc) {
301325163bd5SWebb Scales 		cmd_free(h, c);
301425163bd5SWebb Scales 		return 0;
301525163bd5SWebb Scales 	}
30169846590eSStephen M. Cameron 	sense = c->err_info->SenseInfo;
30179437ac43SStephen Cameron 	if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
30189437ac43SStephen Cameron 		sense_len = sizeof(c->err_info->SenseInfo);
30199437ac43SStephen Cameron 	else
30209437ac43SStephen Cameron 		sense_len = c->err_info->SenseLen;
30219437ac43SStephen Cameron 	decode_sense_data(sense, sense_len, &sense_key, &asc, &ascq);
30229846590eSStephen M. Cameron 	cmd_status = c->err_info->CommandStatus;
30239846590eSStephen M. Cameron 	scsi_status = c->err_info->ScsiStatus;
30249846590eSStephen M. Cameron 	cmd_free(h, c);
30259846590eSStephen M. Cameron 	/* Is the volume 'not ready'? */
30269846590eSStephen M. Cameron 	if (cmd_status != CMD_TARGET_STATUS ||
30279846590eSStephen M. Cameron 		scsi_status != SAM_STAT_CHECK_CONDITION ||
30289846590eSStephen M. Cameron 		sense_key != NOT_READY ||
30299846590eSStephen M. Cameron 		asc != ASC_LUN_NOT_READY)  {
30309846590eSStephen M. Cameron 		return 0;
30319846590eSStephen M. Cameron 	}
30329846590eSStephen M. Cameron 
30339846590eSStephen M. Cameron 	/* Determine the reason for not ready state */
30349846590eSStephen M. Cameron 	ldstat = hpsa_get_volume_status(h, scsi3addr);
30359846590eSStephen M. Cameron 
30369846590eSStephen M. Cameron 	/* Keep volume offline in certain cases: */
30379846590eSStephen M. Cameron 	switch (ldstat) {
30389846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ERASE:
30399846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_RPI:
30409846590eSStephen M. Cameron 	case HPSA_LV_PENDING_RPI:
30419846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_NO_KEY:
30429846590eSStephen M. Cameron 	case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
30439846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION:
30449846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
30459846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
30469846590eSStephen M. Cameron 		return ldstat;
30479846590eSStephen M. Cameron 	case HPSA_VPD_LV_STATUS_UNSUPPORTED:
30489846590eSStephen M. Cameron 		/* If VPD status page isn't available,
30499846590eSStephen M. Cameron 		 * use ASC/ASCQ to determine state
30509846590eSStephen M. Cameron 		 */
30519846590eSStephen M. Cameron 		if ((ascq == ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS) ||
30529846590eSStephen M. Cameron 			(ascq == ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ))
30539846590eSStephen M. Cameron 			return ldstat;
30549846590eSStephen M. Cameron 		break;
30559846590eSStephen M. Cameron 	default:
30569846590eSStephen M. Cameron 		break;
30579846590eSStephen M. Cameron 	}
30589846590eSStephen M. Cameron 	return 0;
30599846590eSStephen M. Cameron }
30609846590eSStephen M. Cameron 
30619b5c48c2SStephen Cameron /*
30629b5c48c2SStephen Cameron  * Find out if a logical device supports aborts by simply trying one.
30639b5c48c2SStephen Cameron  * Smart Array may claim not to support aborts on logical drives, but
30649b5c48c2SStephen Cameron  * if a MSA2000 * is connected, the drives on that will be presented
30659b5c48c2SStephen Cameron  * by the Smart Array as logical drives, and aborts may be sent to
30669b5c48c2SStephen Cameron  * those devices successfully.  So the simplest way to find out is
30679b5c48c2SStephen Cameron  * to simply try an abort and see how the device responds.
30689b5c48c2SStephen Cameron  */
30699b5c48c2SStephen Cameron static int hpsa_device_supports_aborts(struct ctlr_info *h,
30709b5c48c2SStephen Cameron 					unsigned char *scsi3addr)
30719b5c48c2SStephen Cameron {
30729b5c48c2SStephen Cameron 	struct CommandList *c;
30739b5c48c2SStephen Cameron 	struct ErrorInfo *ei;
30749b5c48c2SStephen Cameron 	int rc = 0;
30759b5c48c2SStephen Cameron 
30769b5c48c2SStephen Cameron 	u64 tag = (u64) -1; /* bogus tag */
30779b5c48c2SStephen Cameron 
30789b5c48c2SStephen Cameron 	/* Assume that physical devices support aborts */
30799b5c48c2SStephen Cameron 	if (!is_logical_dev_addr_mode(scsi3addr))
30809b5c48c2SStephen Cameron 		return 1;
30819b5c48c2SStephen Cameron 
30829b5c48c2SStephen Cameron 	c = cmd_alloc(h);
3083bf43caf3SRobert Elliott 
30849b5c48c2SStephen Cameron 	(void) fill_cmd(c, HPSA_ABORT_MSG, h, &tag, 0, 0, scsi3addr, TYPE_MSG);
30859b5c48c2SStephen Cameron 	(void) hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
30869b5c48c2SStephen Cameron 	/* no unmap needed here because no data xfer. */
30879b5c48c2SStephen Cameron 	ei = c->err_info;
30889b5c48c2SStephen Cameron 	switch (ei->CommandStatus) {
30899b5c48c2SStephen Cameron 	case CMD_INVALID:
30909b5c48c2SStephen Cameron 		rc = 0;
30919b5c48c2SStephen Cameron 		break;
30929b5c48c2SStephen Cameron 	case CMD_UNABORTABLE:
30939b5c48c2SStephen Cameron 	case CMD_ABORT_FAILED:
30949b5c48c2SStephen Cameron 		rc = 1;
30959b5c48c2SStephen Cameron 		break;
30969437ac43SStephen Cameron 	case CMD_TMF_STATUS:
30979437ac43SStephen Cameron 		rc = hpsa_evaluate_tmf_status(h, c);
30989437ac43SStephen Cameron 		break;
30999b5c48c2SStephen Cameron 	default:
31009b5c48c2SStephen Cameron 		rc = 0;
31019b5c48c2SStephen Cameron 		break;
31029b5c48c2SStephen Cameron 	}
31039b5c48c2SStephen Cameron 	cmd_free(h, c);
31049b5c48c2SStephen Cameron 	return rc;
31059b5c48c2SStephen Cameron }
31069b5c48c2SStephen Cameron 
3107edd16368SStephen M. Cameron static int hpsa_update_device_info(struct ctlr_info *h,
31080b0e1d6cSStephen M. Cameron 	unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
31090b0e1d6cSStephen M. Cameron 	unsigned char *is_OBDR_device)
3110edd16368SStephen M. Cameron {
31110b0e1d6cSStephen M. Cameron 
31120b0e1d6cSStephen M. Cameron #define OBDR_SIG_OFFSET 43
31130b0e1d6cSStephen M. Cameron #define OBDR_TAPE_SIG "$DR-10"
31140b0e1d6cSStephen M. Cameron #define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
31150b0e1d6cSStephen M. Cameron #define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
31160b0e1d6cSStephen M. Cameron 
3117ea6d3bc3SStephen M. Cameron 	unsigned char *inq_buff;
31180b0e1d6cSStephen M. Cameron 	unsigned char *obdr_sig;
3119edd16368SStephen M. Cameron 
3120ea6d3bc3SStephen M. Cameron 	inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
3121edd16368SStephen M. Cameron 	if (!inq_buff)
3122edd16368SStephen M. Cameron 		goto bail_out;
3123edd16368SStephen M. Cameron 
3124edd16368SStephen M. Cameron 	/* Do an inquiry to the device to see what it is. */
3125edd16368SStephen M. Cameron 	if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
3126edd16368SStephen M. Cameron 		(unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
3127edd16368SStephen M. Cameron 		/* Inquiry failed (msg printed already) */
3128edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev,
3129edd16368SStephen M. Cameron 			"hpsa_update_device_info: inquiry failed\n");
3130edd16368SStephen M. Cameron 		goto bail_out;
3131edd16368SStephen M. Cameron 	}
3132edd16368SStephen M. Cameron 
3133edd16368SStephen M. Cameron 	this_device->devtype = (inq_buff[0] & 0x1f);
3134edd16368SStephen M. Cameron 	memcpy(this_device->scsi3addr, scsi3addr, 8);
3135edd16368SStephen M. Cameron 	memcpy(this_device->vendor, &inq_buff[8],
3136edd16368SStephen M. Cameron 		sizeof(this_device->vendor));
3137edd16368SStephen M. Cameron 	memcpy(this_device->model, &inq_buff[16],
3138edd16368SStephen M. Cameron 		sizeof(this_device->model));
3139edd16368SStephen M. Cameron 	memset(this_device->device_id, 0,
3140edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
3141edd16368SStephen M. Cameron 	hpsa_get_device_id(h, scsi3addr, this_device->device_id,
3142edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
3143edd16368SStephen M. Cameron 
3144edd16368SStephen M. Cameron 	if (this_device->devtype == TYPE_DISK &&
3145283b4a9bSStephen M. Cameron 		is_logical_dev_addr_mode(scsi3addr)) {
314667955ba3SStephen M. Cameron 		int volume_offline;
314767955ba3SStephen M. Cameron 
3148edd16368SStephen M. Cameron 		hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
3149283b4a9bSStephen M. Cameron 		if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
3150283b4a9bSStephen M. Cameron 			hpsa_get_ioaccel_status(h, scsi3addr, this_device);
315167955ba3SStephen M. Cameron 		volume_offline = hpsa_volume_offline(h, scsi3addr);
315267955ba3SStephen M. Cameron 		if (volume_offline < 0 || volume_offline > 0xff)
315367955ba3SStephen M. Cameron 			volume_offline = HPSA_VPD_LV_STATUS_UNSUPPORTED;
315467955ba3SStephen M. Cameron 		this_device->volume_offline = volume_offline & 0xff;
3155283b4a9bSStephen M. Cameron 	} else {
3156edd16368SStephen M. Cameron 		this_device->raid_level = RAID_UNKNOWN;
3157283b4a9bSStephen M. Cameron 		this_device->offload_config = 0;
3158283b4a9bSStephen M. Cameron 		this_device->offload_enabled = 0;
315941ce4c35SStephen Cameron 		this_device->offload_to_be_enabled = 0;
3160a3144e0bSJoe Handzik 		this_device->hba_ioaccel_enabled = 0;
31619846590eSStephen M. Cameron 		this_device->volume_offline = 0;
316203383736SDon Brace 		this_device->queue_depth = h->nr_cmds;
3163283b4a9bSStephen M. Cameron 	}
3164edd16368SStephen M. Cameron 
31650b0e1d6cSStephen M. Cameron 	if (is_OBDR_device) {
31660b0e1d6cSStephen M. Cameron 		/* See if this is a One-Button-Disaster-Recovery device
31670b0e1d6cSStephen M. Cameron 		 * by looking for "$DR-10" at offset 43 in inquiry data.
31680b0e1d6cSStephen M. Cameron 		 */
31690b0e1d6cSStephen M. Cameron 		obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
31700b0e1d6cSStephen M. Cameron 		*is_OBDR_device = (this_device->devtype == TYPE_ROM &&
31710b0e1d6cSStephen M. Cameron 					strncmp(obdr_sig, OBDR_TAPE_SIG,
31720b0e1d6cSStephen M. Cameron 						OBDR_SIG_LEN) == 0);
31730b0e1d6cSStephen M. Cameron 	}
3174edd16368SStephen M. Cameron 	kfree(inq_buff);
3175edd16368SStephen M. Cameron 	return 0;
3176edd16368SStephen M. Cameron 
3177edd16368SStephen M. Cameron bail_out:
3178edd16368SStephen M. Cameron 	kfree(inq_buff);
3179edd16368SStephen M. Cameron 	return 1;
3180edd16368SStephen M. Cameron }
3181edd16368SStephen M. Cameron 
31829b5c48c2SStephen Cameron static void hpsa_update_device_supports_aborts(struct ctlr_info *h,
31839b5c48c2SStephen Cameron 			struct hpsa_scsi_dev_t *dev, u8 *scsi3addr)
31849b5c48c2SStephen Cameron {
31859b5c48c2SStephen Cameron 	unsigned long flags;
31869b5c48c2SStephen Cameron 	int rc, entry;
31879b5c48c2SStephen Cameron 	/*
31889b5c48c2SStephen Cameron 	 * See if this device supports aborts.  If we already know
31899b5c48c2SStephen Cameron 	 * the device, we already know if it supports aborts, otherwise
31909b5c48c2SStephen Cameron 	 * we have to find out if it supports aborts by trying one.
31919b5c48c2SStephen Cameron 	 */
31929b5c48c2SStephen Cameron 	spin_lock_irqsave(&h->devlock, flags);
31939b5c48c2SStephen Cameron 	rc = hpsa_scsi_find_entry(dev, h->dev, h->ndevices, &entry);
31949b5c48c2SStephen Cameron 	if ((rc == DEVICE_SAME || rc == DEVICE_UPDATED) &&
31959b5c48c2SStephen Cameron 		entry >= 0 && entry < h->ndevices) {
31969b5c48c2SStephen Cameron 		dev->supports_aborts = h->dev[entry]->supports_aborts;
31979b5c48c2SStephen Cameron 		spin_unlock_irqrestore(&h->devlock, flags);
31989b5c48c2SStephen Cameron 	} else {
31999b5c48c2SStephen Cameron 		spin_unlock_irqrestore(&h->devlock, flags);
32009b5c48c2SStephen Cameron 		dev->supports_aborts =
32019b5c48c2SStephen Cameron 				hpsa_device_supports_aborts(h, scsi3addr);
32029b5c48c2SStephen Cameron 		if (dev->supports_aborts < 0)
32039b5c48c2SStephen Cameron 			dev->supports_aborts = 0;
32049b5c48c2SStephen Cameron 	}
32059b5c48c2SStephen Cameron }
32069b5c48c2SStephen Cameron 
32074f4eb9f1SScott Teel static unsigned char *ext_target_model[] = {
3208edd16368SStephen M. Cameron 	"MSA2012",
3209edd16368SStephen M. Cameron 	"MSA2024",
3210edd16368SStephen M. Cameron 	"MSA2312",
3211edd16368SStephen M. Cameron 	"MSA2324",
3212fda38518SStephen M. Cameron 	"P2000 G3 SAS",
3213e06c8e5cSStephen M. Cameron 	"MSA 2040 SAS",
3214edd16368SStephen M. Cameron 	NULL,
3215edd16368SStephen M. Cameron };
3216edd16368SStephen M. Cameron 
32174f4eb9f1SScott Teel static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
3218edd16368SStephen M. Cameron {
3219edd16368SStephen M. Cameron 	int i;
3220edd16368SStephen M. Cameron 
32214f4eb9f1SScott Teel 	for (i = 0; ext_target_model[i]; i++)
32224f4eb9f1SScott Teel 		if (strncmp(device->model, ext_target_model[i],
32234f4eb9f1SScott Teel 			strlen(ext_target_model[i])) == 0)
3224edd16368SStephen M. Cameron 			return 1;
3225edd16368SStephen M. Cameron 	return 0;
3226edd16368SStephen M. Cameron }
3227edd16368SStephen M. Cameron 
3228edd16368SStephen M. Cameron /* Helper function to assign bus, target, lun mapping of devices.
32294f4eb9f1SScott Teel  * Puts non-external target logical volumes on bus 0, external target logical
3230edd16368SStephen M. Cameron  * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
3231edd16368SStephen M. Cameron  * Logical drive target and lun are assigned at this time, but
3232edd16368SStephen M. Cameron  * physical device lun and target assignment are deferred (assigned
3233edd16368SStephen M. Cameron  * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
3234edd16368SStephen M. Cameron  */
3235edd16368SStephen M. Cameron static void figure_bus_target_lun(struct ctlr_info *h,
32361f310bdeSStephen M. Cameron 	u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
3237edd16368SStephen M. Cameron {
32381f310bdeSStephen M. Cameron 	u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
3239edd16368SStephen M. Cameron 
32401f310bdeSStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes)) {
32411f310bdeSStephen M. Cameron 		/* physical device, target and lun filled in later */
32421f310bdeSStephen M. Cameron 		if (is_hba_lunid(lunaddrbytes))
32431f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
32441f310bdeSStephen M. Cameron 		else
32451f310bdeSStephen M. Cameron 			/* defer target, lun assignment for physical devices */
32461f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 2, -1, -1);
32471f310bdeSStephen M. Cameron 		return;
32481f310bdeSStephen M. Cameron 	}
32491f310bdeSStephen M. Cameron 	/* It's a logical device */
32504f4eb9f1SScott Teel 	if (is_ext_target(h, device)) {
32514f4eb9f1SScott Teel 		/* external target way, put logicals on bus 1
3252339b2b14SStephen M. Cameron 		 * and match target/lun numbers box
32531f310bdeSStephen M. Cameron 		 * reports, other smart array, bus 0, target 0, match lunid
3254339b2b14SStephen M. Cameron 		 */
32551f310bdeSStephen M. Cameron 		hpsa_set_bus_target_lun(device,
32561f310bdeSStephen M. Cameron 			1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
32571f310bdeSStephen M. Cameron 		return;
3258339b2b14SStephen M. Cameron 	}
32591f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
3260edd16368SStephen M. Cameron }
3261edd16368SStephen M. Cameron 
3262edd16368SStephen M. Cameron /*
3263edd16368SStephen M. Cameron  * If there is no lun 0 on a target, linux won't find any devices.
32644f4eb9f1SScott Teel  * For the external targets (arrays), we have to manually detect the enclosure
3265edd16368SStephen M. Cameron  * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
3266edd16368SStephen M. Cameron  * it for some reason.  *tmpdevice is the target we're adding,
3267edd16368SStephen M. Cameron  * this_device is a pointer into the current element of currentsd[]
3268edd16368SStephen M. Cameron  * that we're building up in update_scsi_devices(), below.
3269edd16368SStephen M. Cameron  * lunzerobits is a bitmap that tracks which targets already have a
3270edd16368SStephen M. Cameron  * lun 0 assigned.
3271edd16368SStephen M. Cameron  * Returns 1 if an enclosure was added, 0 if not.
3272edd16368SStephen M. Cameron  */
32734f4eb9f1SScott Teel static int add_ext_target_dev(struct ctlr_info *h,
3274edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *tmpdevice,
327501a02ffcSStephen M. Cameron 	struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
32764f4eb9f1SScott Teel 	unsigned long lunzerobits[], int *n_ext_target_devs)
3277edd16368SStephen M. Cameron {
3278edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
3279edd16368SStephen M. Cameron 
32801f310bdeSStephen M. Cameron 	if (test_bit(tmpdevice->target, lunzerobits))
3281edd16368SStephen M. Cameron 		return 0; /* There is already a lun 0 on this target. */
3282edd16368SStephen M. Cameron 
3283edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes))
3284edd16368SStephen M. Cameron 		return 0; /* It's the logical targets that may lack lun 0. */
3285edd16368SStephen M. Cameron 
32864f4eb9f1SScott Teel 	if (!is_ext_target(h, tmpdevice))
32874f4eb9f1SScott Teel 		return 0; /* Only external target devices have this problem. */
3288edd16368SStephen M. Cameron 
32891f310bdeSStephen M. Cameron 	if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
3290edd16368SStephen M. Cameron 		return 0;
3291edd16368SStephen M. Cameron 
3292c4f8a299SStephen M. Cameron 	memset(scsi3addr, 0, 8);
32931f310bdeSStephen M. Cameron 	scsi3addr[3] = tmpdevice->target;
3294edd16368SStephen M. Cameron 	if (is_hba_lunid(scsi3addr))
3295edd16368SStephen M. Cameron 		return 0; /* Don't add the RAID controller here. */
3296edd16368SStephen M. Cameron 
3297339b2b14SStephen M. Cameron 	if (is_scsi_rev_5(h))
3298339b2b14SStephen M. Cameron 		return 0; /* p1210m doesn't need to do this. */
3299339b2b14SStephen M. Cameron 
33004f4eb9f1SScott Teel 	if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
3301aca4a520SScott Teel 		dev_warn(&h->pdev->dev, "Maximum number of external "
3302aca4a520SScott Teel 			"target devices exceeded.  Check your hardware "
3303edd16368SStephen M. Cameron 			"configuration.");
3304edd16368SStephen M. Cameron 		return 0;
3305edd16368SStephen M. Cameron 	}
3306edd16368SStephen M. Cameron 
33070b0e1d6cSStephen M. Cameron 	if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
3308edd16368SStephen M. Cameron 		return 0;
33094f4eb9f1SScott Teel 	(*n_ext_target_devs)++;
33101f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(this_device,
33111f310bdeSStephen M. Cameron 				tmpdevice->bus, tmpdevice->target, 0);
33129b5c48c2SStephen Cameron 	hpsa_update_device_supports_aborts(h, this_device, scsi3addr);
33131f310bdeSStephen M. Cameron 	set_bit(tmpdevice->target, lunzerobits);
3314edd16368SStephen M. Cameron 	return 1;
3315edd16368SStephen M. Cameron }
3316edd16368SStephen M. Cameron 
3317edd16368SStephen M. Cameron /*
331854b6e9e9SScott Teel  * Get address of physical disk used for an ioaccel2 mode command:
331954b6e9e9SScott Teel  *	1. Extract ioaccel2 handle from the command.
332054b6e9e9SScott Teel  *	2. Find a matching ioaccel2 handle from list of physical disks.
332154b6e9e9SScott Teel  *	3. Return:
332254b6e9e9SScott Teel  *		1 and set scsi3addr to address of matching physical
332354b6e9e9SScott Teel  *		0 if no matching physical disk was found.
332454b6e9e9SScott Teel  */
332554b6e9e9SScott Teel static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
332654b6e9e9SScott Teel 	struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
332754b6e9e9SScott Teel {
332841ce4c35SStephen Cameron 	struct io_accel2_cmd *c2 =
332941ce4c35SStephen Cameron 			&h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
333041ce4c35SStephen Cameron 	unsigned long flags;
333154b6e9e9SScott Teel 	int i;
333254b6e9e9SScott Teel 
333341ce4c35SStephen Cameron 	spin_lock_irqsave(&h->devlock, flags);
333441ce4c35SStephen Cameron 	for (i = 0; i < h->ndevices; i++)
333541ce4c35SStephen Cameron 		if (h->dev[i]->ioaccel_handle == le32_to_cpu(c2->scsi_nexus)) {
333641ce4c35SStephen Cameron 			memcpy(scsi3addr, h->dev[i]->scsi3addr,
333741ce4c35SStephen Cameron 				sizeof(h->dev[i]->scsi3addr));
333841ce4c35SStephen Cameron 			spin_unlock_irqrestore(&h->devlock, flags);
333954b6e9e9SScott Teel 			return 1;
334054b6e9e9SScott Teel 		}
334141ce4c35SStephen Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
334241ce4c35SStephen Cameron 	return 0;
334341ce4c35SStephen Cameron }
334441ce4c35SStephen Cameron 
334554b6e9e9SScott Teel /*
3346edd16368SStephen M. Cameron  * Do CISS_REPORT_PHYS and CISS_REPORT_LOG.  Data is returned in physdev,
3347edd16368SStephen M. Cameron  * logdev.  The number of luns in physdev and logdev are returned in
3348edd16368SStephen M. Cameron  * *nphysicals and *nlogicals, respectively.
3349edd16368SStephen M. Cameron  * Returns 0 on success, -1 otherwise.
3350edd16368SStephen M. Cameron  */
3351edd16368SStephen M. Cameron static int hpsa_gather_lun_info(struct ctlr_info *h,
335203383736SDon Brace 	struct ReportExtendedLUNdata *physdev, u32 *nphysicals,
335301a02ffcSStephen M. Cameron 	struct ReportLUNdata *logdev, u32 *nlogicals)
3354edd16368SStephen M. Cameron {
335503383736SDon Brace 	if (hpsa_scsi_do_report_phys_luns(h, physdev, sizeof(*physdev))) {
3356edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
3357edd16368SStephen M. Cameron 		return -1;
3358edd16368SStephen M. Cameron 	}
335903383736SDon Brace 	*nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 24;
3360edd16368SStephen M. Cameron 	if (*nphysicals > HPSA_MAX_PHYS_LUN) {
336103383736SDon Brace 		dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded. %d LUNs ignored.\n",
336203383736SDon Brace 			HPSA_MAX_PHYS_LUN, *nphysicals - HPSA_MAX_PHYS_LUN);
3363edd16368SStephen M. Cameron 		*nphysicals = HPSA_MAX_PHYS_LUN;
3364edd16368SStephen M. Cameron 	}
336503383736SDon Brace 	if (hpsa_scsi_do_report_log_luns(h, logdev, sizeof(*logdev))) {
3366edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
3367edd16368SStephen M. Cameron 		return -1;
3368edd16368SStephen M. Cameron 	}
33696df1e954SStephen M. Cameron 	*nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
3370edd16368SStephen M. Cameron 	/* Reject Logicals in excess of our max capability. */
3371edd16368SStephen M. Cameron 	if (*nlogicals > HPSA_MAX_LUN) {
3372edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
3373edd16368SStephen M. Cameron 			"maximum logical LUNs (%d) exceeded.  "
3374edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_LUN,
3375edd16368SStephen M. Cameron 			*nlogicals - HPSA_MAX_LUN);
3376edd16368SStephen M. Cameron 			*nlogicals = HPSA_MAX_LUN;
3377edd16368SStephen M. Cameron 	}
3378edd16368SStephen M. Cameron 	if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
3379edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
3380edd16368SStephen M. Cameron 			"maximum logical + physical LUNs (%d) exceeded. "
3381edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
3382edd16368SStephen M. Cameron 			*nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
3383edd16368SStephen M. Cameron 		*nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
3384edd16368SStephen M. Cameron 	}
3385edd16368SStephen M. Cameron 	return 0;
3386edd16368SStephen M. Cameron }
3387edd16368SStephen M. Cameron 
338842a91641SDon Brace static u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position,
338942a91641SDon Brace 	int i, int nphysicals, int nlogicals,
3390a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list,
3391339b2b14SStephen M. Cameron 	struct ReportLUNdata *logdev_list)
3392339b2b14SStephen M. Cameron {
3393339b2b14SStephen M. Cameron 	/* Helper function, figure out where the LUN ID info is coming from
3394339b2b14SStephen M. Cameron 	 * given index i, lists of physical and logical devices, where in
3395339b2b14SStephen M. Cameron 	 * the list the raid controller is supposed to appear (first or last)
3396339b2b14SStephen M. Cameron 	 */
3397339b2b14SStephen M. Cameron 
3398339b2b14SStephen M. Cameron 	int logicals_start = nphysicals + (raid_ctlr_position == 0);
3399339b2b14SStephen M. Cameron 	int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
3400339b2b14SStephen M. Cameron 
3401339b2b14SStephen M. Cameron 	if (i == raid_ctlr_position)
3402339b2b14SStephen M. Cameron 		return RAID_CTLR_LUNID;
3403339b2b14SStephen M. Cameron 
3404339b2b14SStephen M. Cameron 	if (i < logicals_start)
3405d5b5d964SStephen M. Cameron 		return &physdev_list->LUN[i -
3406d5b5d964SStephen M. Cameron 				(raid_ctlr_position == 0)].lunid[0];
3407339b2b14SStephen M. Cameron 
3408339b2b14SStephen M. Cameron 	if (i < last_device)
3409339b2b14SStephen M. Cameron 		return &logdev_list->LUN[i - nphysicals -
3410339b2b14SStephen M. Cameron 			(raid_ctlr_position == 0)][0];
3411339b2b14SStephen M. Cameron 	BUG();
3412339b2b14SStephen M. Cameron 	return NULL;
3413339b2b14SStephen M. Cameron }
3414339b2b14SStephen M. Cameron 
3415316b221aSStephen M. Cameron static int hpsa_hba_mode_enabled(struct ctlr_info *h)
3416316b221aSStephen M. Cameron {
3417316b221aSStephen M. Cameron 	int rc;
34186e8e8088SJoe Handzik 	int hba_mode_enabled;
3419316b221aSStephen M. Cameron 	struct bmic_controller_parameters *ctlr_params;
3420316b221aSStephen M. Cameron 	ctlr_params = kzalloc(sizeof(struct bmic_controller_parameters),
3421316b221aSStephen M. Cameron 		GFP_KERNEL);
3422316b221aSStephen M. Cameron 
3423316b221aSStephen M. Cameron 	if (!ctlr_params)
342496444fbbSJoe Handzik 		return -ENOMEM;
3425316b221aSStephen M. Cameron 	rc = hpsa_bmic_ctrl_mode_sense(h, RAID_CTLR_LUNID, 0, ctlr_params,
3426316b221aSStephen M. Cameron 		sizeof(struct bmic_controller_parameters));
342796444fbbSJoe Handzik 	if (rc) {
3428316b221aSStephen M. Cameron 		kfree(ctlr_params);
342996444fbbSJoe Handzik 		return rc;
3430316b221aSStephen M. Cameron 	}
34316e8e8088SJoe Handzik 
34326e8e8088SJoe Handzik 	hba_mode_enabled =
34336e8e8088SJoe Handzik 		((ctlr_params->nvram_flags & HBA_MODE_ENABLED_FLAG) != 0);
34346e8e8088SJoe Handzik 	kfree(ctlr_params);
34356e8e8088SJoe Handzik 	return hba_mode_enabled;
3436316b221aSStephen M. Cameron }
3437316b221aSStephen M. Cameron 
343803383736SDon Brace /* get physical drive ioaccel handle and queue depth */
343903383736SDon Brace static void hpsa_get_ioaccel_drive_info(struct ctlr_info *h,
344003383736SDon Brace 		struct hpsa_scsi_dev_t *dev,
344103383736SDon Brace 		u8 *lunaddrbytes,
344203383736SDon Brace 		struct bmic_identify_physical_device *id_phys)
344303383736SDon Brace {
344403383736SDon Brace 	int rc;
344503383736SDon Brace 	struct ext_report_lun_entry *rle =
344603383736SDon Brace 		(struct ext_report_lun_entry *) lunaddrbytes;
344703383736SDon Brace 
344803383736SDon Brace 	dev->ioaccel_handle = rle->ioaccel_handle;
3449a3144e0bSJoe Handzik 	if (PHYS_IOACCEL(lunaddrbytes) && dev->ioaccel_handle)
3450a3144e0bSJoe Handzik 		dev->hba_ioaccel_enabled = 1;
345103383736SDon Brace 	memset(id_phys, 0, sizeof(*id_phys));
345203383736SDon Brace 	rc = hpsa_bmic_id_physical_device(h, lunaddrbytes,
345303383736SDon Brace 			GET_BMIC_DRIVE_NUMBER(lunaddrbytes), id_phys,
345403383736SDon Brace 			sizeof(*id_phys));
345503383736SDon Brace 	if (!rc)
345603383736SDon Brace 		/* Reserve space for FW operations */
345703383736SDon Brace #define DRIVE_CMDS_RESERVED_FOR_FW 2
345803383736SDon Brace #define DRIVE_QUEUE_DEPTH 7
345903383736SDon Brace 		dev->queue_depth =
346003383736SDon Brace 			le16_to_cpu(id_phys->current_queue_depth_limit) -
346103383736SDon Brace 				DRIVE_CMDS_RESERVED_FOR_FW;
346203383736SDon Brace 	else
346303383736SDon Brace 		dev->queue_depth = DRIVE_QUEUE_DEPTH; /* conservative */
346403383736SDon Brace 	atomic_set(&dev->ioaccel_cmds_out, 0);
346503383736SDon Brace }
346603383736SDon Brace 
3467edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
3468edd16368SStephen M. Cameron {
3469edd16368SStephen M. Cameron 	/* the idea here is we could get notified
3470edd16368SStephen M. Cameron 	 * that some devices have changed, so we do a report
3471edd16368SStephen M. Cameron 	 * physical luns and report logical luns cmd, and adjust
3472edd16368SStephen M. Cameron 	 * our list of devices accordingly.
3473edd16368SStephen M. Cameron 	 *
3474edd16368SStephen M. Cameron 	 * The scsi3addr's of devices won't change so long as the
3475edd16368SStephen M. Cameron 	 * adapter is not reset.  That means we can rescan and
3476edd16368SStephen M. Cameron 	 * tell which devices we already know about, vs. new
3477edd16368SStephen M. Cameron 	 * devices, vs.  disappearing devices.
3478edd16368SStephen M. Cameron 	 */
3479a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list = NULL;
3480edd16368SStephen M. Cameron 	struct ReportLUNdata *logdev_list = NULL;
348103383736SDon Brace 	struct bmic_identify_physical_device *id_phys = NULL;
348201a02ffcSStephen M. Cameron 	u32 nphysicals = 0;
348301a02ffcSStephen M. Cameron 	u32 nlogicals = 0;
348401a02ffcSStephen M. Cameron 	u32 ndev_allocated = 0;
3485edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
3486edd16368SStephen M. Cameron 	int ncurrent = 0;
34874f4eb9f1SScott Teel 	int i, n_ext_target_devs, ndevs_to_allocate;
3488339b2b14SStephen M. Cameron 	int raid_ctlr_position;
34892bbf5c7fSJoe Handzik 	int rescan_hba_mode;
3490aca4a520SScott Teel 	DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
3491edd16368SStephen M. Cameron 
3492cfe5badcSScott Teel 	currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
349392084715SStephen M. Cameron 	physdev_list = kzalloc(sizeof(*physdev_list), GFP_KERNEL);
349492084715SStephen M. Cameron 	logdev_list = kzalloc(sizeof(*logdev_list), GFP_KERNEL);
3495edd16368SStephen M. Cameron 	tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
349603383736SDon Brace 	id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
3497edd16368SStephen M. Cameron 
349803383736SDon Brace 	if (!currentsd || !physdev_list || !logdev_list ||
349903383736SDon Brace 		!tmpdevice || !id_phys) {
3500edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory\n");
3501edd16368SStephen M. Cameron 		goto out;
3502edd16368SStephen M. Cameron 	}
3503edd16368SStephen M. Cameron 	memset(lunzerobits, 0, sizeof(lunzerobits));
3504edd16368SStephen M. Cameron 
3505316b221aSStephen M. Cameron 	rescan_hba_mode = hpsa_hba_mode_enabled(h);
350696444fbbSJoe Handzik 	if (rescan_hba_mode < 0)
350796444fbbSJoe Handzik 		goto out;
3508316b221aSStephen M. Cameron 
3509316b221aSStephen M. Cameron 	if (!h->hba_mode_enabled && rescan_hba_mode)
3510316b221aSStephen M. Cameron 		dev_warn(&h->pdev->dev, "HBA mode enabled\n");
3511316b221aSStephen M. Cameron 	else if (h->hba_mode_enabled && !rescan_hba_mode)
3512316b221aSStephen M. Cameron 		dev_warn(&h->pdev->dev, "HBA mode disabled\n");
3513316b221aSStephen M. Cameron 
3514316b221aSStephen M. Cameron 	h->hba_mode_enabled = rescan_hba_mode;
3515316b221aSStephen M. Cameron 
351603383736SDon Brace 	if (hpsa_gather_lun_info(h, physdev_list, &nphysicals,
351703383736SDon Brace 			logdev_list, &nlogicals))
3518edd16368SStephen M. Cameron 		goto out;
3519edd16368SStephen M. Cameron 
3520aca4a520SScott Teel 	/* We might see up to the maximum number of logical and physical disks
3521aca4a520SScott Teel 	 * plus external target devices, and a device for the local RAID
3522aca4a520SScott Teel 	 * controller.
3523edd16368SStephen M. Cameron 	 */
3524aca4a520SScott Teel 	ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
3525edd16368SStephen M. Cameron 
3526edd16368SStephen M. Cameron 	/* Allocate the per device structures */
3527edd16368SStephen M. Cameron 	for (i = 0; i < ndevs_to_allocate; i++) {
3528b7ec021fSScott Teel 		if (i >= HPSA_MAX_DEVICES) {
3529b7ec021fSScott Teel 			dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
3530b7ec021fSScott Teel 				"  %d devices ignored.\n", HPSA_MAX_DEVICES,
3531b7ec021fSScott Teel 				ndevs_to_allocate - HPSA_MAX_DEVICES);
3532b7ec021fSScott Teel 			break;
3533b7ec021fSScott Teel 		}
3534b7ec021fSScott Teel 
3535edd16368SStephen M. Cameron 		currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
3536edd16368SStephen M. Cameron 		if (!currentsd[i]) {
3537edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
3538edd16368SStephen M. Cameron 				__FILE__, __LINE__);
3539edd16368SStephen M. Cameron 			goto out;
3540edd16368SStephen M. Cameron 		}
3541edd16368SStephen M. Cameron 		ndev_allocated++;
3542edd16368SStephen M. Cameron 	}
3543edd16368SStephen M. Cameron 
35448645291bSStephen M. Cameron 	if (is_scsi_rev_5(h))
3545339b2b14SStephen M. Cameron 		raid_ctlr_position = 0;
3546339b2b14SStephen M. Cameron 	else
3547339b2b14SStephen M. Cameron 		raid_ctlr_position = nphysicals + nlogicals;
3548339b2b14SStephen M. Cameron 
3549edd16368SStephen M. Cameron 	/* adjust our table of devices */
35504f4eb9f1SScott Teel 	n_ext_target_devs = 0;
3551edd16368SStephen M. Cameron 	for (i = 0; i < nphysicals + nlogicals + 1; i++) {
35520b0e1d6cSStephen M. Cameron 		u8 *lunaddrbytes, is_OBDR = 0;
3553edd16368SStephen M. Cameron 
3554edd16368SStephen M. Cameron 		/* Figure out where the LUN ID info is coming from */
3555339b2b14SStephen M. Cameron 		lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
3556339b2b14SStephen M. Cameron 			i, nphysicals, nlogicals, physdev_list, logdev_list);
355741ce4c35SStephen Cameron 
355841ce4c35SStephen Cameron 		/* skip masked non-disk devices */
355941ce4c35SStephen Cameron 		if (MASKED_DEVICE(lunaddrbytes))
356041ce4c35SStephen Cameron 			if (i < nphysicals + (raid_ctlr_position == 0) &&
356141ce4c35SStephen Cameron 				NON_DISK_PHYS_DEV(lunaddrbytes))
3562edd16368SStephen M. Cameron 				continue;
3563edd16368SStephen M. Cameron 
3564edd16368SStephen M. Cameron 		/* Get device type, vendor, model, device id */
35650b0e1d6cSStephen M. Cameron 		if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
35660b0e1d6cSStephen M. Cameron 							&is_OBDR))
3567edd16368SStephen M. Cameron 			continue; /* skip it if we can't talk to it. */
35681f310bdeSStephen M. Cameron 		figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
35699b5c48c2SStephen Cameron 		hpsa_update_device_supports_aborts(h, tmpdevice, lunaddrbytes);
3570edd16368SStephen M. Cameron 		this_device = currentsd[ncurrent];
3571edd16368SStephen M. Cameron 
3572edd16368SStephen M. Cameron 		/*
35734f4eb9f1SScott Teel 		 * For external target devices, we have to insert a LUN 0 which
3574edd16368SStephen M. Cameron 		 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
3575edd16368SStephen M. Cameron 		 * is nonetheless an enclosure device there.  We have to
3576edd16368SStephen M. Cameron 		 * present that otherwise linux won't find anything if
3577edd16368SStephen M. Cameron 		 * there is no lun 0.
3578edd16368SStephen M. Cameron 		 */
35794f4eb9f1SScott Teel 		if (add_ext_target_dev(h, tmpdevice, this_device,
35801f310bdeSStephen M. Cameron 				lunaddrbytes, lunzerobits,
35814f4eb9f1SScott Teel 				&n_ext_target_devs)) {
3582edd16368SStephen M. Cameron 			ncurrent++;
3583edd16368SStephen M. Cameron 			this_device = currentsd[ncurrent];
3584edd16368SStephen M. Cameron 		}
3585edd16368SStephen M. Cameron 
3586edd16368SStephen M. Cameron 		*this_device = *tmpdevice;
3587edd16368SStephen M. Cameron 
358841ce4c35SStephen Cameron 		/* do not expose masked devices */
358941ce4c35SStephen Cameron 		if (MASKED_DEVICE(lunaddrbytes) &&
359041ce4c35SStephen Cameron 			i < nphysicals + (raid_ctlr_position == 0)) {
359141ce4c35SStephen Cameron 			if (h->hba_mode_enabled)
359241ce4c35SStephen Cameron 				dev_warn(&h->pdev->dev,
359341ce4c35SStephen Cameron 					"Masked physical device detected\n");
359441ce4c35SStephen Cameron 			this_device->expose_state = HPSA_DO_NOT_EXPOSE;
359541ce4c35SStephen Cameron 		} else {
359641ce4c35SStephen Cameron 			this_device->expose_state =
359741ce4c35SStephen Cameron 					HPSA_SG_ATTACH | HPSA_ULD_ATTACH;
359841ce4c35SStephen Cameron 		}
359941ce4c35SStephen Cameron 
3600edd16368SStephen M. Cameron 		switch (this_device->devtype) {
36010b0e1d6cSStephen M. Cameron 		case TYPE_ROM:
3602edd16368SStephen M. Cameron 			/* We don't *really* support actual CD-ROM devices,
3603edd16368SStephen M. Cameron 			 * just "One Button Disaster Recovery" tape drive
3604edd16368SStephen M. Cameron 			 * which temporarily pretends to be a CD-ROM drive.
3605edd16368SStephen M. Cameron 			 * So we check that the device is really an OBDR tape
3606edd16368SStephen M. Cameron 			 * device by checking for "$DR-10" in bytes 43-48 of
3607edd16368SStephen M. Cameron 			 * the inquiry data.
3608edd16368SStephen M. Cameron 			 */
36090b0e1d6cSStephen M. Cameron 			if (is_OBDR)
3610edd16368SStephen M. Cameron 				ncurrent++;
3611edd16368SStephen M. Cameron 			break;
3612edd16368SStephen M. Cameron 		case TYPE_DISK:
3613283b4a9bSStephen M. Cameron 			if (i >= nphysicals) {
3614283b4a9bSStephen M. Cameron 				ncurrent++;
3615edd16368SStephen M. Cameron 				break;
3616283b4a9bSStephen M. Cameron 			}
3617ecf418d1SJoe Handzik 
3618ecf418d1SJoe Handzik 			if (h->hba_mode_enabled)
3619ecf418d1SJoe Handzik 				/* never use raid mapper in HBA mode */
3620ecf418d1SJoe Handzik 				this_device->offload_enabled = 0;
3621ecf418d1SJoe Handzik 			else if (!(h->transMethod & CFGTBL_Trans_io_accel1 ||
3622ecf418d1SJoe Handzik 				h->transMethod & CFGTBL_Trans_io_accel2))
3623316b221aSStephen M. Cameron 				break;
3624ecf418d1SJoe Handzik 
362503383736SDon Brace 			hpsa_get_ioaccel_drive_info(h, this_device,
362603383736SDon Brace 						lunaddrbytes, id_phys);
362703383736SDon Brace 			atomic_set(&this_device->ioaccel_cmds_out, 0);
3628edd16368SStephen M. Cameron 			ncurrent++;
3629edd16368SStephen M. Cameron 			break;
3630edd16368SStephen M. Cameron 		case TYPE_TAPE:
3631edd16368SStephen M. Cameron 		case TYPE_MEDIUM_CHANGER:
3632edd16368SStephen M. Cameron 			ncurrent++;
3633edd16368SStephen M. Cameron 			break;
363441ce4c35SStephen Cameron 		case TYPE_ENCLOSURE:
363541ce4c35SStephen Cameron 			if (h->hba_mode_enabled)
363641ce4c35SStephen Cameron 				ncurrent++;
363741ce4c35SStephen Cameron 			break;
3638edd16368SStephen M. Cameron 		case TYPE_RAID:
3639edd16368SStephen M. Cameron 			/* Only present the Smartarray HBA as a RAID controller.
3640edd16368SStephen M. Cameron 			 * If it's a RAID controller other than the HBA itself
3641edd16368SStephen M. Cameron 			 * (an external RAID controller, MSA500 or similar)
3642edd16368SStephen M. Cameron 			 * don't present it.
3643edd16368SStephen M. Cameron 			 */
3644edd16368SStephen M. Cameron 			if (!is_hba_lunid(lunaddrbytes))
3645edd16368SStephen M. Cameron 				break;
3646edd16368SStephen M. Cameron 			ncurrent++;
3647edd16368SStephen M. Cameron 			break;
3648edd16368SStephen M. Cameron 		default:
3649edd16368SStephen M. Cameron 			break;
3650edd16368SStephen M. Cameron 		}
3651cfe5badcSScott Teel 		if (ncurrent >= HPSA_MAX_DEVICES)
3652edd16368SStephen M. Cameron 			break;
3653edd16368SStephen M. Cameron 	}
3654edd16368SStephen M. Cameron 	adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
3655edd16368SStephen M. Cameron out:
3656edd16368SStephen M. Cameron 	kfree(tmpdevice);
3657edd16368SStephen M. Cameron 	for (i = 0; i < ndev_allocated; i++)
3658edd16368SStephen M. Cameron 		kfree(currentsd[i]);
3659edd16368SStephen M. Cameron 	kfree(currentsd);
3660edd16368SStephen M. Cameron 	kfree(physdev_list);
3661edd16368SStephen M. Cameron 	kfree(logdev_list);
366203383736SDon Brace 	kfree(id_phys);
3663edd16368SStephen M. Cameron }
3664edd16368SStephen M. Cameron 
3665ec5cbf04SWebb Scales static void hpsa_set_sg_descriptor(struct SGDescriptor *desc,
3666ec5cbf04SWebb Scales 				   struct scatterlist *sg)
3667ec5cbf04SWebb Scales {
3668ec5cbf04SWebb Scales 	u64 addr64 = (u64) sg_dma_address(sg);
3669ec5cbf04SWebb Scales 	unsigned int len = sg_dma_len(sg);
3670ec5cbf04SWebb Scales 
3671ec5cbf04SWebb Scales 	desc->Addr = cpu_to_le64(addr64);
3672ec5cbf04SWebb Scales 	desc->Len = cpu_to_le32(len);
3673ec5cbf04SWebb Scales 	desc->Ext = 0;
3674ec5cbf04SWebb Scales }
3675ec5cbf04SWebb Scales 
3676c7ee65b3SWebb Scales /*
3677c7ee65b3SWebb Scales  * hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
3678edd16368SStephen M. Cameron  * dma mapping  and fills in the scatter gather entries of the
3679edd16368SStephen M. Cameron  * hpsa command, cp.
3680edd16368SStephen M. Cameron  */
368133a2ffceSStephen M. Cameron static int hpsa_scatter_gather(struct ctlr_info *h,
3682edd16368SStephen M. Cameron 		struct CommandList *cp,
3683edd16368SStephen M. Cameron 		struct scsi_cmnd *cmd)
3684edd16368SStephen M. Cameron {
3685edd16368SStephen M. Cameron 	struct scatterlist *sg;
3686b3a7ba7cSWebb Scales 	int use_sg, i, sg_limit, chained, last_sg;
368733a2ffceSStephen M. Cameron 	struct SGDescriptor *curr_sg;
3688edd16368SStephen M. Cameron 
368933a2ffceSStephen M. Cameron 	BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
3690edd16368SStephen M. Cameron 
3691edd16368SStephen M. Cameron 	use_sg = scsi_dma_map(cmd);
3692edd16368SStephen M. Cameron 	if (use_sg < 0)
3693edd16368SStephen M. Cameron 		return use_sg;
3694edd16368SStephen M. Cameron 
3695edd16368SStephen M. Cameron 	if (!use_sg)
3696edd16368SStephen M. Cameron 		goto sglist_finished;
3697edd16368SStephen M. Cameron 
3698b3a7ba7cSWebb Scales 	/*
3699b3a7ba7cSWebb Scales 	 * If the number of entries is greater than the max for a single list,
3700b3a7ba7cSWebb Scales 	 * then we have a chained list; we will set up all but one entry in the
3701b3a7ba7cSWebb Scales 	 * first list (the last entry is saved for link information);
3702b3a7ba7cSWebb Scales 	 * otherwise, we don't have a chained list and we'll set up at each of
3703b3a7ba7cSWebb Scales 	 * the entries in the one list.
3704b3a7ba7cSWebb Scales 	 */
370533a2ffceSStephen M. Cameron 	curr_sg = cp->SG;
3706b3a7ba7cSWebb Scales 	chained = use_sg > h->max_cmd_sg_entries;
3707b3a7ba7cSWebb Scales 	sg_limit = chained ? h->max_cmd_sg_entries - 1 : use_sg;
3708b3a7ba7cSWebb Scales 	last_sg = scsi_sg_count(cmd) - 1;
3709b3a7ba7cSWebb Scales 	scsi_for_each_sg(cmd, sg, sg_limit, i) {
3710ec5cbf04SWebb Scales 		hpsa_set_sg_descriptor(curr_sg, sg);
371133a2ffceSStephen M. Cameron 		curr_sg++;
371233a2ffceSStephen M. Cameron 	}
3713ec5cbf04SWebb Scales 
3714b3a7ba7cSWebb Scales 	if (chained) {
3715b3a7ba7cSWebb Scales 		/*
3716b3a7ba7cSWebb Scales 		 * Continue with the chained list.  Set curr_sg to the chained
3717b3a7ba7cSWebb Scales 		 * list.  Modify the limit to the total count less the entries
3718b3a7ba7cSWebb Scales 		 * we've already set up.  Resume the scan at the list entry
3719b3a7ba7cSWebb Scales 		 * where the previous loop left off.
3720b3a7ba7cSWebb Scales 		 */
3721b3a7ba7cSWebb Scales 		curr_sg = h->cmd_sg_list[cp->cmdindex];
3722b3a7ba7cSWebb Scales 		sg_limit = use_sg - sg_limit;
3723b3a7ba7cSWebb Scales 		for_each_sg(sg, sg, sg_limit, i) {
3724b3a7ba7cSWebb Scales 			hpsa_set_sg_descriptor(curr_sg, sg);
3725b3a7ba7cSWebb Scales 			curr_sg++;
3726b3a7ba7cSWebb Scales 		}
3727b3a7ba7cSWebb Scales 	}
3728b3a7ba7cSWebb Scales 
3729ec5cbf04SWebb Scales 	/* Back the pointer up to the last entry and mark it as "last". */
3730b3a7ba7cSWebb Scales 	(curr_sg - 1)->Ext = cpu_to_le32(HPSA_SG_LAST);
373133a2ffceSStephen M. Cameron 
373233a2ffceSStephen M. Cameron 	if (use_sg + chained > h->maxSG)
373333a2ffceSStephen M. Cameron 		h->maxSG = use_sg + chained;
373433a2ffceSStephen M. Cameron 
373533a2ffceSStephen M. Cameron 	if (chained) {
373633a2ffceSStephen M. Cameron 		cp->Header.SGList = h->max_cmd_sg_entries;
373750a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(use_sg + 1);
3738e2bea6dfSStephen M. Cameron 		if (hpsa_map_sg_chain_block(h, cp)) {
3739e2bea6dfSStephen M. Cameron 			scsi_dma_unmap(cmd);
3740e2bea6dfSStephen M. Cameron 			return -1;
3741e2bea6dfSStephen M. Cameron 		}
374233a2ffceSStephen M. Cameron 		return 0;
3743edd16368SStephen M. Cameron 	}
3744edd16368SStephen M. Cameron 
3745edd16368SStephen M. Cameron sglist_finished:
3746edd16368SStephen M. Cameron 
374701a02ffcSStephen M. Cameron 	cp->Header.SGList = (u8) use_sg;   /* no. SGs contig in this cmd */
3748c7ee65b3SWebb Scales 	cp->Header.SGTotal = cpu_to_le16(use_sg); /* total sgs in cmd list */
3749edd16368SStephen M. Cameron 	return 0;
3750edd16368SStephen M. Cameron }
3751edd16368SStephen M. Cameron 
3752283b4a9bSStephen M. Cameron #define IO_ACCEL_INELIGIBLE (1)
3753283b4a9bSStephen M. Cameron static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
3754283b4a9bSStephen M. Cameron {
3755283b4a9bSStephen M. Cameron 	int is_write = 0;
3756283b4a9bSStephen M. Cameron 	u32 block;
3757283b4a9bSStephen M. Cameron 	u32 block_cnt;
3758283b4a9bSStephen M. Cameron 
3759283b4a9bSStephen M. Cameron 	/* Perform some CDB fixups if needed using 10 byte reads/writes only */
3760283b4a9bSStephen M. Cameron 	switch (cdb[0]) {
3761283b4a9bSStephen M. Cameron 	case WRITE_6:
3762283b4a9bSStephen M. Cameron 	case WRITE_12:
3763283b4a9bSStephen M. Cameron 		is_write = 1;
3764283b4a9bSStephen M. Cameron 	case READ_6:
3765283b4a9bSStephen M. Cameron 	case READ_12:
3766283b4a9bSStephen M. Cameron 		if (*cdb_len == 6) {
3767283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 8) | cdb[3];
3768283b4a9bSStephen M. Cameron 			block_cnt = cdb[4];
3769283b4a9bSStephen M. Cameron 		} else {
3770283b4a9bSStephen M. Cameron 			BUG_ON(*cdb_len != 12);
3771283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 24) |
3772283b4a9bSStephen M. Cameron 				(((u32) cdb[3]) << 16) |
3773283b4a9bSStephen M. Cameron 				(((u32) cdb[4]) << 8) |
3774283b4a9bSStephen M. Cameron 				cdb[5];
3775283b4a9bSStephen M. Cameron 			block_cnt =
3776283b4a9bSStephen M. Cameron 				(((u32) cdb[6]) << 24) |
3777283b4a9bSStephen M. Cameron 				(((u32) cdb[7]) << 16) |
3778283b4a9bSStephen M. Cameron 				(((u32) cdb[8]) << 8) |
3779283b4a9bSStephen M. Cameron 				cdb[9];
3780283b4a9bSStephen M. Cameron 		}
3781283b4a9bSStephen M. Cameron 		if (block_cnt > 0xffff)
3782283b4a9bSStephen M. Cameron 			return IO_ACCEL_INELIGIBLE;
3783283b4a9bSStephen M. Cameron 
3784283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
3785283b4a9bSStephen M. Cameron 		cdb[1] = 0;
3786283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (block >> 24);
3787283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (block >> 16);
3788283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (block >> 8);
3789283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (block);
3790283b4a9bSStephen M. Cameron 		cdb[6] = 0;
3791283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (block_cnt >> 8);
3792283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (block_cnt);
3793283b4a9bSStephen M. Cameron 		cdb[9] = 0;
3794283b4a9bSStephen M. Cameron 		*cdb_len = 10;
3795283b4a9bSStephen M. Cameron 		break;
3796283b4a9bSStephen M. Cameron 	}
3797283b4a9bSStephen M. Cameron 	return 0;
3798283b4a9bSStephen M. Cameron }
3799283b4a9bSStephen M. Cameron 
3800c349775eSScott Teel static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
3801283b4a9bSStephen M. Cameron 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
380203383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
3803e1f7de0cSMatt Gates {
3804e1f7de0cSMatt Gates 	struct scsi_cmnd *cmd = c->scsi_cmd;
3805e1f7de0cSMatt Gates 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
3806e1f7de0cSMatt Gates 	unsigned int len;
3807e1f7de0cSMatt Gates 	unsigned int total_len = 0;
3808e1f7de0cSMatt Gates 	struct scatterlist *sg;
3809e1f7de0cSMatt Gates 	u64 addr64;
3810e1f7de0cSMatt Gates 	int use_sg, i;
3811e1f7de0cSMatt Gates 	struct SGDescriptor *curr_sg;
3812e1f7de0cSMatt Gates 	u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
3813e1f7de0cSMatt Gates 
3814283b4a9bSStephen M. Cameron 	/* TODO: implement chaining support */
381503383736SDon Brace 	if (scsi_sg_count(cmd) > h->ioaccel_maxsg) {
381603383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3817283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
381803383736SDon Brace 	}
3819283b4a9bSStephen M. Cameron 
3820e1f7de0cSMatt Gates 	BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
3821e1f7de0cSMatt Gates 
382203383736SDon Brace 	if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
382303383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3824283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
382503383736SDon Brace 	}
3826283b4a9bSStephen M. Cameron 
3827e1f7de0cSMatt Gates 	c->cmd_type = CMD_IOACCEL1;
3828e1f7de0cSMatt Gates 
3829e1f7de0cSMatt Gates 	/* Adjust the DMA address to point to the accelerated command buffer */
3830e1f7de0cSMatt Gates 	c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
3831e1f7de0cSMatt Gates 				(c->cmdindex * sizeof(*cp));
3832e1f7de0cSMatt Gates 	BUG_ON(c->busaddr & 0x0000007F);
3833e1f7de0cSMatt Gates 
3834e1f7de0cSMatt Gates 	use_sg = scsi_dma_map(cmd);
383503383736SDon Brace 	if (use_sg < 0) {
383603383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3837e1f7de0cSMatt Gates 		return use_sg;
383803383736SDon Brace 	}
3839e1f7de0cSMatt Gates 
3840e1f7de0cSMatt Gates 	if (use_sg) {
3841e1f7de0cSMatt Gates 		curr_sg = cp->SG;
3842e1f7de0cSMatt Gates 		scsi_for_each_sg(cmd, sg, use_sg, i) {
3843e1f7de0cSMatt Gates 			addr64 = (u64) sg_dma_address(sg);
3844e1f7de0cSMatt Gates 			len  = sg_dma_len(sg);
3845e1f7de0cSMatt Gates 			total_len += len;
384650a0decfSStephen M. Cameron 			curr_sg->Addr = cpu_to_le64(addr64);
384750a0decfSStephen M. Cameron 			curr_sg->Len = cpu_to_le32(len);
384850a0decfSStephen M. Cameron 			curr_sg->Ext = cpu_to_le32(0);
3849e1f7de0cSMatt Gates 			curr_sg++;
3850e1f7de0cSMatt Gates 		}
385150a0decfSStephen M. Cameron 		(--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
3852e1f7de0cSMatt Gates 
3853e1f7de0cSMatt Gates 		switch (cmd->sc_data_direction) {
3854e1f7de0cSMatt Gates 		case DMA_TO_DEVICE:
3855e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_OUT;
3856e1f7de0cSMatt Gates 			break;
3857e1f7de0cSMatt Gates 		case DMA_FROM_DEVICE:
3858e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_IN;
3859e1f7de0cSMatt Gates 			break;
3860e1f7de0cSMatt Gates 		case DMA_NONE:
3861e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_NODATAXFER;
3862e1f7de0cSMatt Gates 			break;
3863e1f7de0cSMatt Gates 		default:
3864e1f7de0cSMatt Gates 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3865e1f7de0cSMatt Gates 			cmd->sc_data_direction);
3866e1f7de0cSMatt Gates 			BUG();
3867e1f7de0cSMatt Gates 			break;
3868e1f7de0cSMatt Gates 		}
3869e1f7de0cSMatt Gates 	} else {
3870e1f7de0cSMatt Gates 		control |= IOACCEL1_CONTROL_NODATAXFER;
3871e1f7de0cSMatt Gates 	}
3872e1f7de0cSMatt Gates 
3873c349775eSScott Teel 	c->Header.SGList = use_sg;
3874e1f7de0cSMatt Gates 	/* Fill out the command structure to submit */
38752b08b3e9SDon Brace 	cp->dev_handle = cpu_to_le16(ioaccel_handle & 0xFFFF);
38762b08b3e9SDon Brace 	cp->transfer_len = cpu_to_le32(total_len);
38772b08b3e9SDon Brace 	cp->io_flags = cpu_to_le16(IOACCEL1_IOFLAGS_IO_REQ |
38782b08b3e9SDon Brace 			(cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK));
38792b08b3e9SDon Brace 	cp->control = cpu_to_le32(control);
3880283b4a9bSStephen M. Cameron 	memcpy(cp->CDB, cdb, cdb_len);
3881283b4a9bSStephen M. Cameron 	memcpy(cp->CISS_LUN, scsi3addr, 8);
3882c349775eSScott Teel 	/* Tag was already set at init time. */
3883e1f7de0cSMatt Gates 	enqueue_cmd_and_start_io(h, c);
3884e1f7de0cSMatt Gates 	return 0;
3885e1f7de0cSMatt Gates }
3886edd16368SStephen M. Cameron 
3887283b4a9bSStephen M. Cameron /*
3888283b4a9bSStephen M. Cameron  * Queue a command directly to a device behind the controller using the
3889283b4a9bSStephen M. Cameron  * I/O accelerator path.
3890283b4a9bSStephen M. Cameron  */
3891283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
3892283b4a9bSStephen M. Cameron 	struct CommandList *c)
3893283b4a9bSStephen M. Cameron {
3894283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
3895283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3896283b4a9bSStephen M. Cameron 
389703383736SDon Brace 	c->phys_disk = dev;
389803383736SDon Brace 
3899283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
390003383736SDon Brace 		cmd->cmnd, cmd->cmd_len, dev->scsi3addr, dev);
3901283b4a9bSStephen M. Cameron }
3902283b4a9bSStephen M. Cameron 
3903dd0e19f3SScott Teel /*
3904dd0e19f3SScott Teel  * Set encryption parameters for the ioaccel2 request
3905dd0e19f3SScott Teel  */
3906dd0e19f3SScott Teel static void set_encrypt_ioaccel2(struct ctlr_info *h,
3907dd0e19f3SScott Teel 	struct CommandList *c, struct io_accel2_cmd *cp)
3908dd0e19f3SScott Teel {
3909dd0e19f3SScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
3910dd0e19f3SScott Teel 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3911dd0e19f3SScott Teel 	struct raid_map_data *map = &dev->raid_map;
3912dd0e19f3SScott Teel 	u64 first_block;
3913dd0e19f3SScott Teel 
3914dd0e19f3SScott Teel 	/* Are we doing encryption on this device */
39152b08b3e9SDon Brace 	if (!(le16_to_cpu(map->flags) & RAID_MAP_FLAG_ENCRYPT_ON))
3916dd0e19f3SScott Teel 		return;
3917dd0e19f3SScott Teel 	/* Set the data encryption key index. */
3918dd0e19f3SScott Teel 	cp->dekindex = map->dekindex;
3919dd0e19f3SScott Teel 
3920dd0e19f3SScott Teel 	/* Set the encryption enable flag, encoded into direction field. */
3921dd0e19f3SScott Teel 	cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
3922dd0e19f3SScott Teel 
3923dd0e19f3SScott Teel 	/* Set encryption tweak values based on logical block address
3924dd0e19f3SScott Teel 	 * If block size is 512, tweak value is LBA.
3925dd0e19f3SScott Teel 	 * For other block sizes, tweak is (LBA * block size)/ 512)
3926dd0e19f3SScott Teel 	 */
3927dd0e19f3SScott Teel 	switch (cmd->cmnd[0]) {
3928dd0e19f3SScott Teel 	/* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
3929dd0e19f3SScott Teel 	case WRITE_6:
3930dd0e19f3SScott Teel 	case READ_6:
39312b08b3e9SDon Brace 		first_block = get_unaligned_be16(&cmd->cmnd[2]);
3932dd0e19f3SScott Teel 		break;
3933dd0e19f3SScott Teel 	case WRITE_10:
3934dd0e19f3SScott Teel 	case READ_10:
3935dd0e19f3SScott Teel 	/* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
3936dd0e19f3SScott Teel 	case WRITE_12:
3937dd0e19f3SScott Teel 	case READ_12:
39382b08b3e9SDon Brace 		first_block = get_unaligned_be32(&cmd->cmnd[2]);
3939dd0e19f3SScott Teel 		break;
3940dd0e19f3SScott Teel 	case WRITE_16:
3941dd0e19f3SScott Teel 	case READ_16:
39422b08b3e9SDon Brace 		first_block = get_unaligned_be64(&cmd->cmnd[2]);
3943dd0e19f3SScott Teel 		break;
3944dd0e19f3SScott Teel 	default:
3945dd0e19f3SScott Teel 		dev_err(&h->pdev->dev,
39462b08b3e9SDon Brace 			"ERROR: %s: size (0x%x) not supported for encryption\n",
39472b08b3e9SDon Brace 			__func__, cmd->cmnd[0]);
3948dd0e19f3SScott Teel 		BUG();
3949dd0e19f3SScott Teel 		break;
3950dd0e19f3SScott Teel 	}
39512b08b3e9SDon Brace 
39522b08b3e9SDon Brace 	if (le32_to_cpu(map->volume_blk_size) != 512)
39532b08b3e9SDon Brace 		first_block = first_block *
39542b08b3e9SDon Brace 				le32_to_cpu(map->volume_blk_size)/512;
39552b08b3e9SDon Brace 
39562b08b3e9SDon Brace 	cp->tweak_lower = cpu_to_le32(first_block);
39572b08b3e9SDon Brace 	cp->tweak_upper = cpu_to_le32(first_block >> 32);
3958dd0e19f3SScott Teel }
3959dd0e19f3SScott Teel 
3960c349775eSScott Teel static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
3961c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
396203383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
3963c349775eSScott Teel {
3964c349775eSScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
3965c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
3966c349775eSScott Teel 	struct ioaccel2_sg_element *curr_sg;
3967c349775eSScott Teel 	int use_sg, i;
3968c349775eSScott Teel 	struct scatterlist *sg;
3969c349775eSScott Teel 	u64 addr64;
3970c349775eSScott Teel 	u32 len;
3971c349775eSScott Teel 	u32 total_len = 0;
3972c349775eSScott Teel 
3973d9a729f3SWebb Scales 	BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
3974c349775eSScott Teel 
397503383736SDon Brace 	if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
397603383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3977c349775eSScott Teel 		return IO_ACCEL_INELIGIBLE;
397803383736SDon Brace 	}
397903383736SDon Brace 
3980c349775eSScott Teel 	c->cmd_type = CMD_IOACCEL2;
3981c349775eSScott Teel 	/* Adjust the DMA address to point to the accelerated command buffer */
3982c349775eSScott Teel 	c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
3983c349775eSScott Teel 				(c->cmdindex * sizeof(*cp));
3984c349775eSScott Teel 	BUG_ON(c->busaddr & 0x0000007F);
3985c349775eSScott Teel 
3986c349775eSScott Teel 	memset(cp, 0, sizeof(*cp));
3987c349775eSScott Teel 	cp->IU_type = IOACCEL2_IU_TYPE;
3988c349775eSScott Teel 
3989c349775eSScott Teel 	use_sg = scsi_dma_map(cmd);
399003383736SDon Brace 	if (use_sg < 0) {
399103383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3992c349775eSScott Teel 		return use_sg;
399303383736SDon Brace 	}
3994c349775eSScott Teel 
3995c349775eSScott Teel 	if (use_sg) {
3996c349775eSScott Teel 		curr_sg = cp->sg;
3997d9a729f3SWebb Scales 		if (use_sg > h->ioaccel_maxsg) {
3998d9a729f3SWebb Scales 			addr64 = le64_to_cpu(
3999d9a729f3SWebb Scales 				h->ioaccel2_cmd_sg_list[c->cmdindex]->address);
4000d9a729f3SWebb Scales 			curr_sg->address = cpu_to_le64(addr64);
4001d9a729f3SWebb Scales 			curr_sg->length = 0;
4002d9a729f3SWebb Scales 			curr_sg->reserved[0] = 0;
4003d9a729f3SWebb Scales 			curr_sg->reserved[1] = 0;
4004d9a729f3SWebb Scales 			curr_sg->reserved[2] = 0;
4005d9a729f3SWebb Scales 			curr_sg->chain_indicator = 0x80;
4006d9a729f3SWebb Scales 
4007d9a729f3SWebb Scales 			curr_sg = h->ioaccel2_cmd_sg_list[c->cmdindex];
4008d9a729f3SWebb Scales 		}
4009c349775eSScott Teel 		scsi_for_each_sg(cmd, sg, use_sg, i) {
4010c349775eSScott Teel 			addr64 = (u64) sg_dma_address(sg);
4011c349775eSScott Teel 			len  = sg_dma_len(sg);
4012c349775eSScott Teel 			total_len += len;
4013c349775eSScott Teel 			curr_sg->address = cpu_to_le64(addr64);
4014c349775eSScott Teel 			curr_sg->length = cpu_to_le32(len);
4015c349775eSScott Teel 			curr_sg->reserved[0] = 0;
4016c349775eSScott Teel 			curr_sg->reserved[1] = 0;
4017c349775eSScott Teel 			curr_sg->reserved[2] = 0;
4018c349775eSScott Teel 			curr_sg->chain_indicator = 0;
4019c349775eSScott Teel 			curr_sg++;
4020c349775eSScott Teel 		}
4021c349775eSScott Teel 
4022c349775eSScott Teel 		switch (cmd->sc_data_direction) {
4023c349775eSScott Teel 		case DMA_TO_DEVICE:
4024dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4025dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_OUT;
4026c349775eSScott Teel 			break;
4027c349775eSScott Teel 		case DMA_FROM_DEVICE:
4028dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4029dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_IN;
4030c349775eSScott Teel 			break;
4031c349775eSScott Teel 		case DMA_NONE:
4032dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4033dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_NO_DATA;
4034c349775eSScott Teel 			break;
4035c349775eSScott Teel 		default:
4036c349775eSScott Teel 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4037c349775eSScott Teel 				cmd->sc_data_direction);
4038c349775eSScott Teel 			BUG();
4039c349775eSScott Teel 			break;
4040c349775eSScott Teel 		}
4041c349775eSScott Teel 	} else {
4042dd0e19f3SScott Teel 		cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4043dd0e19f3SScott Teel 		cp->direction |= IOACCEL2_DIR_NO_DATA;
4044c349775eSScott Teel 	}
4045dd0e19f3SScott Teel 
4046dd0e19f3SScott Teel 	/* Set encryption parameters, if necessary */
4047dd0e19f3SScott Teel 	set_encrypt_ioaccel2(h, c, cp);
4048dd0e19f3SScott Teel 
40492b08b3e9SDon Brace 	cp->scsi_nexus = cpu_to_le32(ioaccel_handle);
4050f2405db8SDon Brace 	cp->Tag = cpu_to_le32(c->cmdindex << DIRECT_LOOKUP_SHIFT);
4051c349775eSScott Teel 	memcpy(cp->cdb, cdb, sizeof(cp->cdb));
4052c349775eSScott Teel 
4053c349775eSScott Teel 	cp->data_len = cpu_to_le32(total_len);
4054c349775eSScott Teel 	cp->err_ptr = cpu_to_le64(c->busaddr +
4055c349775eSScott Teel 			offsetof(struct io_accel2_cmd, error_data));
405650a0decfSStephen M. Cameron 	cp->err_len = cpu_to_le32(sizeof(cp->error_data));
4057c349775eSScott Teel 
4058d9a729f3SWebb Scales 	/* fill in sg elements */
4059d9a729f3SWebb Scales 	if (use_sg > h->ioaccel_maxsg) {
4060d9a729f3SWebb Scales 		cp->sg_count = 1;
4061d9a729f3SWebb Scales 		if (hpsa_map_ioaccel2_sg_chain_block(h, cp, c)) {
4062d9a729f3SWebb Scales 			atomic_dec(&phys_disk->ioaccel_cmds_out);
4063d9a729f3SWebb Scales 			scsi_dma_unmap(cmd);
4064d9a729f3SWebb Scales 			return -1;
4065d9a729f3SWebb Scales 		}
4066d9a729f3SWebb Scales 	} else
4067d9a729f3SWebb Scales 		cp->sg_count = (u8) use_sg;
4068d9a729f3SWebb Scales 
4069c349775eSScott Teel 	enqueue_cmd_and_start_io(h, c);
4070c349775eSScott Teel 	return 0;
4071c349775eSScott Teel }
4072c349775eSScott Teel 
4073c349775eSScott Teel /*
4074c349775eSScott Teel  * Queue a command to the correct I/O accelerator path.
4075c349775eSScott Teel  */
4076c349775eSScott Teel static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
4077c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
407803383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
4079c349775eSScott Teel {
408003383736SDon Brace 	/* Try to honor the device's queue depth */
408103383736SDon Brace 	if (atomic_inc_return(&phys_disk->ioaccel_cmds_out) >
408203383736SDon Brace 					phys_disk->queue_depth) {
408303383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
408403383736SDon Brace 		return IO_ACCEL_INELIGIBLE;
408503383736SDon Brace 	}
4086c349775eSScott Teel 	if (h->transMethod & CFGTBL_Trans_io_accel1)
4087c349775eSScott Teel 		return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
408803383736SDon Brace 						cdb, cdb_len, scsi3addr,
408903383736SDon Brace 						phys_disk);
4090c349775eSScott Teel 	else
4091c349775eSScott Teel 		return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
409203383736SDon Brace 						cdb, cdb_len, scsi3addr,
409303383736SDon Brace 						phys_disk);
4094c349775eSScott Teel }
4095c349775eSScott Teel 
40966b80b18fSScott Teel static void raid_map_helper(struct raid_map_data *map,
40976b80b18fSScott Teel 		int offload_to_mirror, u32 *map_index, u32 *current_group)
40986b80b18fSScott Teel {
40996b80b18fSScott Teel 	if (offload_to_mirror == 0)  {
41006b80b18fSScott Teel 		/* use physical disk in the first mirrored group. */
41012b08b3e9SDon Brace 		*map_index %= le16_to_cpu(map->data_disks_per_row);
41026b80b18fSScott Teel 		return;
41036b80b18fSScott Teel 	}
41046b80b18fSScott Teel 	do {
41056b80b18fSScott Teel 		/* determine mirror group that *map_index indicates */
41062b08b3e9SDon Brace 		*current_group = *map_index /
41072b08b3e9SDon Brace 			le16_to_cpu(map->data_disks_per_row);
41086b80b18fSScott Teel 		if (offload_to_mirror == *current_group)
41096b80b18fSScott Teel 			continue;
41102b08b3e9SDon Brace 		if (*current_group < le16_to_cpu(map->layout_map_count) - 1) {
41116b80b18fSScott Teel 			/* select map index from next group */
41122b08b3e9SDon Brace 			*map_index += le16_to_cpu(map->data_disks_per_row);
41136b80b18fSScott Teel 			(*current_group)++;
41146b80b18fSScott Teel 		} else {
41156b80b18fSScott Teel 			/* select map index from first group */
41162b08b3e9SDon Brace 			*map_index %= le16_to_cpu(map->data_disks_per_row);
41176b80b18fSScott Teel 			*current_group = 0;
41186b80b18fSScott Teel 		}
41196b80b18fSScott Teel 	} while (offload_to_mirror != *current_group);
41206b80b18fSScott Teel }
41216b80b18fSScott Teel 
4122283b4a9bSStephen M. Cameron /*
4123283b4a9bSStephen M. Cameron  * Attempt to perform offload RAID mapping for a logical volume I/O.
4124283b4a9bSStephen M. Cameron  */
4125283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
4126283b4a9bSStephen M. Cameron 	struct CommandList *c)
4127283b4a9bSStephen M. Cameron {
4128283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
4129283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4130283b4a9bSStephen M. Cameron 	struct raid_map_data *map = &dev->raid_map;
4131283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map->data[0];
4132283b4a9bSStephen M. Cameron 	int is_write = 0;
4133283b4a9bSStephen M. Cameron 	u32 map_index;
4134283b4a9bSStephen M. Cameron 	u64 first_block, last_block;
4135283b4a9bSStephen M. Cameron 	u32 block_cnt;
4136283b4a9bSStephen M. Cameron 	u32 blocks_per_row;
4137283b4a9bSStephen M. Cameron 	u64 first_row, last_row;
4138283b4a9bSStephen M. Cameron 	u32 first_row_offset, last_row_offset;
4139283b4a9bSStephen M. Cameron 	u32 first_column, last_column;
41406b80b18fSScott Teel 	u64 r0_first_row, r0_last_row;
41416b80b18fSScott Teel 	u32 r5or6_blocks_per_row;
41426b80b18fSScott Teel 	u64 r5or6_first_row, r5or6_last_row;
41436b80b18fSScott Teel 	u32 r5or6_first_row_offset, r5or6_last_row_offset;
41446b80b18fSScott Teel 	u32 r5or6_first_column, r5or6_last_column;
41456b80b18fSScott Teel 	u32 total_disks_per_row;
41466b80b18fSScott Teel 	u32 stripesize;
41476b80b18fSScott Teel 	u32 first_group, last_group, current_group;
4148283b4a9bSStephen M. Cameron 	u32 map_row;
4149283b4a9bSStephen M. Cameron 	u32 disk_handle;
4150283b4a9bSStephen M. Cameron 	u64 disk_block;
4151283b4a9bSStephen M. Cameron 	u32 disk_block_cnt;
4152283b4a9bSStephen M. Cameron 	u8 cdb[16];
4153283b4a9bSStephen M. Cameron 	u8 cdb_len;
41542b08b3e9SDon Brace 	u16 strip_size;
4155283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
4156283b4a9bSStephen M. Cameron 	u64 tmpdiv;
4157283b4a9bSStephen M. Cameron #endif
41586b80b18fSScott Teel 	int offload_to_mirror;
4159283b4a9bSStephen M. Cameron 
4160283b4a9bSStephen M. Cameron 	/* check for valid opcode, get LBA and block count */
4161283b4a9bSStephen M. Cameron 	switch (cmd->cmnd[0]) {
4162283b4a9bSStephen M. Cameron 	case WRITE_6:
4163283b4a9bSStephen M. Cameron 		is_write = 1;
4164283b4a9bSStephen M. Cameron 	case READ_6:
4165283b4a9bSStephen M. Cameron 		first_block =
4166283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 8) |
4167283b4a9bSStephen M. Cameron 			cmd->cmnd[3];
4168283b4a9bSStephen M. Cameron 		block_cnt = cmd->cmnd[4];
41693fa89a04SStephen M. Cameron 		if (block_cnt == 0)
41703fa89a04SStephen M. Cameron 			block_cnt = 256;
4171283b4a9bSStephen M. Cameron 		break;
4172283b4a9bSStephen M. Cameron 	case WRITE_10:
4173283b4a9bSStephen M. Cameron 		is_write = 1;
4174283b4a9bSStephen M. Cameron 	case READ_10:
4175283b4a9bSStephen M. Cameron 		first_block =
4176283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
4177283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
4178283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
4179283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
4180283b4a9bSStephen M. Cameron 		block_cnt =
4181283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 8) |
4182283b4a9bSStephen M. Cameron 			cmd->cmnd[8];
4183283b4a9bSStephen M. Cameron 		break;
4184283b4a9bSStephen M. Cameron 	case WRITE_12:
4185283b4a9bSStephen M. Cameron 		is_write = 1;
4186283b4a9bSStephen M. Cameron 	case READ_12:
4187283b4a9bSStephen M. Cameron 		first_block =
4188283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
4189283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
4190283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
4191283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
4192283b4a9bSStephen M. Cameron 		block_cnt =
4193283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[6]) << 24) |
4194283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 16) |
4195283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[8]) << 8) |
4196283b4a9bSStephen M. Cameron 		cmd->cmnd[9];
4197283b4a9bSStephen M. Cameron 		break;
4198283b4a9bSStephen M. Cameron 	case WRITE_16:
4199283b4a9bSStephen M. Cameron 		is_write = 1;
4200283b4a9bSStephen M. Cameron 	case READ_16:
4201283b4a9bSStephen M. Cameron 		first_block =
4202283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 56) |
4203283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 48) |
4204283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 40) |
4205283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[5]) << 32) |
4206283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[6]) << 24) |
4207283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[7]) << 16) |
4208283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[8]) << 8) |
4209283b4a9bSStephen M. Cameron 			cmd->cmnd[9];
4210283b4a9bSStephen M. Cameron 		block_cnt =
4211283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[10]) << 24) |
4212283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[11]) << 16) |
4213283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[12]) << 8) |
4214283b4a9bSStephen M. Cameron 			cmd->cmnd[13];
4215283b4a9bSStephen M. Cameron 		break;
4216283b4a9bSStephen M. Cameron 	default:
4217283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
4218283b4a9bSStephen M. Cameron 	}
4219283b4a9bSStephen M. Cameron 	last_block = first_block + block_cnt - 1;
4220283b4a9bSStephen M. Cameron 
4221283b4a9bSStephen M. Cameron 	/* check for write to non-RAID-0 */
4222283b4a9bSStephen M. Cameron 	if (is_write && dev->raid_level != 0)
4223283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
4224283b4a9bSStephen M. Cameron 
4225283b4a9bSStephen M. Cameron 	/* check for invalid block or wraparound */
42262b08b3e9SDon Brace 	if (last_block >= le64_to_cpu(map->volume_blk_cnt) ||
42272b08b3e9SDon Brace 		last_block < first_block)
4228283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
4229283b4a9bSStephen M. Cameron 
4230283b4a9bSStephen M. Cameron 	/* calculate stripe information for the request */
42312b08b3e9SDon Brace 	blocks_per_row = le16_to_cpu(map->data_disks_per_row) *
42322b08b3e9SDon Brace 				le16_to_cpu(map->strip_size);
42332b08b3e9SDon Brace 	strip_size = le16_to_cpu(map->strip_size);
4234283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
4235283b4a9bSStephen M. Cameron 	tmpdiv = first_block;
4236283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
4237283b4a9bSStephen M. Cameron 	first_row = tmpdiv;
4238283b4a9bSStephen M. Cameron 	tmpdiv = last_block;
4239283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
4240283b4a9bSStephen M. Cameron 	last_row = tmpdiv;
4241283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
4242283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
4243283b4a9bSStephen M. Cameron 	tmpdiv = first_row_offset;
42442b08b3e9SDon Brace 	(void) do_div(tmpdiv, strip_size);
4245283b4a9bSStephen M. Cameron 	first_column = tmpdiv;
4246283b4a9bSStephen M. Cameron 	tmpdiv = last_row_offset;
42472b08b3e9SDon Brace 	(void) do_div(tmpdiv, strip_size);
4248283b4a9bSStephen M. Cameron 	last_column = tmpdiv;
4249283b4a9bSStephen M. Cameron #else
4250283b4a9bSStephen M. Cameron 	first_row = first_block / blocks_per_row;
4251283b4a9bSStephen M. Cameron 	last_row = last_block / blocks_per_row;
4252283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
4253283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
42542b08b3e9SDon Brace 	first_column = first_row_offset / strip_size;
42552b08b3e9SDon Brace 	last_column = last_row_offset / strip_size;
4256283b4a9bSStephen M. Cameron #endif
4257283b4a9bSStephen M. Cameron 
4258283b4a9bSStephen M. Cameron 	/* if this isn't a single row/column then give to the controller */
4259283b4a9bSStephen M. Cameron 	if ((first_row != last_row) || (first_column != last_column))
4260283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
4261283b4a9bSStephen M. Cameron 
4262283b4a9bSStephen M. Cameron 	/* proceeding with driver mapping */
42632b08b3e9SDon Brace 	total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
42642b08b3e9SDon Brace 				le16_to_cpu(map->metadata_disks_per_row);
4265283b4a9bSStephen M. Cameron 	map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
42662b08b3e9SDon Brace 				le16_to_cpu(map->row_cnt);
42676b80b18fSScott Teel 	map_index = (map_row * total_disks_per_row) + first_column;
42686b80b18fSScott Teel 
42696b80b18fSScott Teel 	switch (dev->raid_level) {
42706b80b18fSScott Teel 	case HPSA_RAID_0:
42716b80b18fSScott Teel 		break; /* nothing special to do */
42726b80b18fSScott Teel 	case HPSA_RAID_1:
42736b80b18fSScott Teel 		/* Handles load balance across RAID 1 members.
42746b80b18fSScott Teel 		 * (2-drive R1 and R10 with even # of drives.)
42756b80b18fSScott Teel 		 * Appropriate for SSDs, not optimal for HDDs
4276283b4a9bSStephen M. Cameron 		 */
42772b08b3e9SDon Brace 		BUG_ON(le16_to_cpu(map->layout_map_count) != 2);
4278283b4a9bSStephen M. Cameron 		if (dev->offload_to_mirror)
42792b08b3e9SDon Brace 			map_index += le16_to_cpu(map->data_disks_per_row);
4280283b4a9bSStephen M. Cameron 		dev->offload_to_mirror = !dev->offload_to_mirror;
42816b80b18fSScott Teel 		break;
42826b80b18fSScott Teel 	case HPSA_RAID_ADM:
42836b80b18fSScott Teel 		/* Handles N-way mirrors  (R1-ADM)
42846b80b18fSScott Teel 		 * and R10 with # of drives divisible by 3.)
42856b80b18fSScott Teel 		 */
42862b08b3e9SDon Brace 		BUG_ON(le16_to_cpu(map->layout_map_count) != 3);
42876b80b18fSScott Teel 
42886b80b18fSScott Teel 		offload_to_mirror = dev->offload_to_mirror;
42896b80b18fSScott Teel 		raid_map_helper(map, offload_to_mirror,
42906b80b18fSScott Teel 				&map_index, &current_group);
42916b80b18fSScott Teel 		/* set mirror group to use next time */
42926b80b18fSScott Teel 		offload_to_mirror =
42932b08b3e9SDon Brace 			(offload_to_mirror >=
42942b08b3e9SDon Brace 			le16_to_cpu(map->layout_map_count) - 1)
42956b80b18fSScott Teel 			? 0 : offload_to_mirror + 1;
42966b80b18fSScott Teel 		dev->offload_to_mirror = offload_to_mirror;
42976b80b18fSScott Teel 		/* Avoid direct use of dev->offload_to_mirror within this
42986b80b18fSScott Teel 		 * function since multiple threads might simultaneously
42996b80b18fSScott Teel 		 * increment it beyond the range of dev->layout_map_count -1.
43006b80b18fSScott Teel 		 */
43016b80b18fSScott Teel 		break;
43026b80b18fSScott Teel 	case HPSA_RAID_5:
43036b80b18fSScott Teel 	case HPSA_RAID_6:
43042b08b3e9SDon Brace 		if (le16_to_cpu(map->layout_map_count) <= 1)
43056b80b18fSScott Teel 			break;
43066b80b18fSScott Teel 
43076b80b18fSScott Teel 		/* Verify first and last block are in same RAID group */
43086b80b18fSScott Teel 		r5or6_blocks_per_row =
43092b08b3e9SDon Brace 			le16_to_cpu(map->strip_size) *
43102b08b3e9SDon Brace 			le16_to_cpu(map->data_disks_per_row);
43116b80b18fSScott Teel 		BUG_ON(r5or6_blocks_per_row == 0);
43122b08b3e9SDon Brace 		stripesize = r5or6_blocks_per_row *
43132b08b3e9SDon Brace 			le16_to_cpu(map->layout_map_count);
43146b80b18fSScott Teel #if BITS_PER_LONG == 32
43156b80b18fSScott Teel 		tmpdiv = first_block;
43166b80b18fSScott Teel 		first_group = do_div(tmpdiv, stripesize);
43176b80b18fSScott Teel 		tmpdiv = first_group;
43186b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
43196b80b18fSScott Teel 		first_group = tmpdiv;
43206b80b18fSScott Teel 		tmpdiv = last_block;
43216b80b18fSScott Teel 		last_group = do_div(tmpdiv, stripesize);
43226b80b18fSScott Teel 		tmpdiv = last_group;
43236b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
43246b80b18fSScott Teel 		last_group = tmpdiv;
43256b80b18fSScott Teel #else
43266b80b18fSScott Teel 		first_group = (first_block % stripesize) / r5or6_blocks_per_row;
43276b80b18fSScott Teel 		last_group = (last_block % stripesize) / r5or6_blocks_per_row;
43286b80b18fSScott Teel #endif
4329000ff7c2SStephen M. Cameron 		if (first_group != last_group)
43306b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
43316b80b18fSScott Teel 
43326b80b18fSScott Teel 		/* Verify request is in a single row of RAID 5/6 */
43336b80b18fSScott Teel #if BITS_PER_LONG == 32
43346b80b18fSScott Teel 		tmpdiv = first_block;
43356b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
43366b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row = tmpdiv;
43376b80b18fSScott Teel 		tmpdiv = last_block;
43386b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
43396b80b18fSScott Teel 		r5or6_last_row = r0_last_row = tmpdiv;
43406b80b18fSScott Teel #else
43416b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row =
43426b80b18fSScott Teel 						first_block / stripesize;
43436b80b18fSScott Teel 		r5or6_last_row = r0_last_row = last_block / stripesize;
43446b80b18fSScott Teel #endif
43456b80b18fSScott Teel 		if (r5or6_first_row != r5or6_last_row)
43466b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
43476b80b18fSScott Teel 
43486b80b18fSScott Teel 
43496b80b18fSScott Teel 		/* Verify request is in a single column */
43506b80b18fSScott Teel #if BITS_PER_LONG == 32
43516b80b18fSScott Teel 		tmpdiv = first_block;
43526b80b18fSScott Teel 		first_row_offset = do_div(tmpdiv, stripesize);
43536b80b18fSScott Teel 		tmpdiv = first_row_offset;
43546b80b18fSScott Teel 		first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
43556b80b18fSScott Teel 		r5or6_first_row_offset = first_row_offset;
43566b80b18fSScott Teel 		tmpdiv = last_block;
43576b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, stripesize);
43586b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
43596b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
43606b80b18fSScott Teel 		tmpdiv = r5or6_first_row_offset;
43616b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
43626b80b18fSScott Teel 		first_column = r5or6_first_column = tmpdiv;
43636b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
43646b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
43656b80b18fSScott Teel 		r5or6_last_column = tmpdiv;
43666b80b18fSScott Teel #else
43676b80b18fSScott Teel 		first_row_offset = r5or6_first_row_offset =
43686b80b18fSScott Teel 			(u32)((first_block % stripesize) %
43696b80b18fSScott Teel 						r5or6_blocks_per_row);
43706b80b18fSScott Teel 
43716b80b18fSScott Teel 		r5or6_last_row_offset =
43726b80b18fSScott Teel 			(u32)((last_block % stripesize) %
43736b80b18fSScott Teel 						r5or6_blocks_per_row);
43746b80b18fSScott Teel 
43756b80b18fSScott Teel 		first_column = r5or6_first_column =
43762b08b3e9SDon Brace 			r5or6_first_row_offset / le16_to_cpu(map->strip_size);
43776b80b18fSScott Teel 		r5or6_last_column =
43782b08b3e9SDon Brace 			r5or6_last_row_offset / le16_to_cpu(map->strip_size);
43796b80b18fSScott Teel #endif
43806b80b18fSScott Teel 		if (r5or6_first_column != r5or6_last_column)
43816b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
43826b80b18fSScott Teel 
43836b80b18fSScott Teel 		/* Request is eligible */
43846b80b18fSScott Teel 		map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
43852b08b3e9SDon Brace 			le16_to_cpu(map->row_cnt);
43866b80b18fSScott Teel 
43876b80b18fSScott Teel 		map_index = (first_group *
43882b08b3e9SDon Brace 			(le16_to_cpu(map->row_cnt) * total_disks_per_row)) +
43896b80b18fSScott Teel 			(map_row * total_disks_per_row) + first_column;
43906b80b18fSScott Teel 		break;
43916b80b18fSScott Teel 	default:
43926b80b18fSScott Teel 		return IO_ACCEL_INELIGIBLE;
4393283b4a9bSStephen M. Cameron 	}
43946b80b18fSScott Teel 
439507543e0cSStephen Cameron 	if (unlikely(map_index >= RAID_MAP_MAX_ENTRIES))
439607543e0cSStephen Cameron 		return IO_ACCEL_INELIGIBLE;
439707543e0cSStephen Cameron 
439803383736SDon Brace 	c->phys_disk = dev->phys_disk[map_index];
439903383736SDon Brace 
4400283b4a9bSStephen M. Cameron 	disk_handle = dd[map_index].ioaccel_handle;
44012b08b3e9SDon Brace 	disk_block = le64_to_cpu(map->disk_starting_blk) +
44022b08b3e9SDon Brace 			first_row * le16_to_cpu(map->strip_size) +
44032b08b3e9SDon Brace 			(first_row_offset - first_column *
44042b08b3e9SDon Brace 			le16_to_cpu(map->strip_size));
4405283b4a9bSStephen M. Cameron 	disk_block_cnt = block_cnt;
4406283b4a9bSStephen M. Cameron 
4407283b4a9bSStephen M. Cameron 	/* handle differing logical/physical block sizes */
4408283b4a9bSStephen M. Cameron 	if (map->phys_blk_shift) {
4409283b4a9bSStephen M. Cameron 		disk_block <<= map->phys_blk_shift;
4410283b4a9bSStephen M. Cameron 		disk_block_cnt <<= map->phys_blk_shift;
4411283b4a9bSStephen M. Cameron 	}
4412283b4a9bSStephen M. Cameron 	BUG_ON(disk_block_cnt > 0xffff);
4413283b4a9bSStephen M. Cameron 
4414283b4a9bSStephen M. Cameron 	/* build the new CDB for the physical disk I/O */
4415283b4a9bSStephen M. Cameron 	if (disk_block > 0xffffffff) {
4416283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_16 : READ_16;
4417283b4a9bSStephen M. Cameron 		cdb[1] = 0;
4418283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 56);
4419283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 48);
4420283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 40);
4421283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block >> 32);
4422283b4a9bSStephen M. Cameron 		cdb[6] = (u8) (disk_block >> 24);
4423283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block >> 16);
4424283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block >> 8);
4425283b4a9bSStephen M. Cameron 		cdb[9] = (u8) (disk_block);
4426283b4a9bSStephen M. Cameron 		cdb[10] = (u8) (disk_block_cnt >> 24);
4427283b4a9bSStephen M. Cameron 		cdb[11] = (u8) (disk_block_cnt >> 16);
4428283b4a9bSStephen M. Cameron 		cdb[12] = (u8) (disk_block_cnt >> 8);
4429283b4a9bSStephen M. Cameron 		cdb[13] = (u8) (disk_block_cnt);
4430283b4a9bSStephen M. Cameron 		cdb[14] = 0;
4431283b4a9bSStephen M. Cameron 		cdb[15] = 0;
4432283b4a9bSStephen M. Cameron 		cdb_len = 16;
4433283b4a9bSStephen M. Cameron 	} else {
4434283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
4435283b4a9bSStephen M. Cameron 		cdb[1] = 0;
4436283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 24);
4437283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 16);
4438283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 8);
4439283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block);
4440283b4a9bSStephen M. Cameron 		cdb[6] = 0;
4441283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block_cnt >> 8);
4442283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block_cnt);
4443283b4a9bSStephen M. Cameron 		cdb[9] = 0;
4444283b4a9bSStephen M. Cameron 		cdb_len = 10;
4445283b4a9bSStephen M. Cameron 	}
4446283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
444703383736SDon Brace 						dev->scsi3addr,
444803383736SDon Brace 						dev->phys_disk[map_index]);
4449283b4a9bSStephen M. Cameron }
4450283b4a9bSStephen M. Cameron 
445125163bd5SWebb Scales /*
445225163bd5SWebb Scales  * Submit commands down the "normal" RAID stack path
445325163bd5SWebb Scales  * All callers to hpsa_ciss_submit must check lockup_detected
445425163bd5SWebb Scales  * beforehand, before (opt.) and after calling cmd_alloc
445525163bd5SWebb Scales  */
4456574f05d3SStephen Cameron static int hpsa_ciss_submit(struct ctlr_info *h,
4457574f05d3SStephen Cameron 	struct CommandList *c, struct scsi_cmnd *cmd,
4458574f05d3SStephen Cameron 	unsigned char scsi3addr[])
4459edd16368SStephen M. Cameron {
4460edd16368SStephen M. Cameron 	cmd->host_scribble = (unsigned char *) c;
4461edd16368SStephen M. Cameron 	c->cmd_type = CMD_SCSI;
4462edd16368SStephen M. Cameron 	c->scsi_cmd = cmd;
4463edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;  /* unused in simple mode */
4464edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
4465f2405db8SDon Brace 	c->Header.tag = cpu_to_le64((c->cmdindex << DIRECT_LOOKUP_SHIFT));
4466edd16368SStephen M. Cameron 
4467edd16368SStephen M. Cameron 	/* Fill in the request block... */
4468edd16368SStephen M. Cameron 
4469edd16368SStephen M. Cameron 	c->Request.Timeout = 0;
4470edd16368SStephen M. Cameron 	BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
4471edd16368SStephen M. Cameron 	c->Request.CDBLen = cmd->cmd_len;
4472edd16368SStephen M. Cameron 	memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
4473edd16368SStephen M. Cameron 	switch (cmd->sc_data_direction) {
4474edd16368SStephen M. Cameron 	case DMA_TO_DEVICE:
4475a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4476a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_WRITE);
4477edd16368SStephen M. Cameron 		break;
4478edd16368SStephen M. Cameron 	case DMA_FROM_DEVICE:
4479a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4480a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_READ);
4481edd16368SStephen M. Cameron 		break;
4482edd16368SStephen M. Cameron 	case DMA_NONE:
4483a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4484a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_NONE);
4485edd16368SStephen M. Cameron 		break;
4486edd16368SStephen M. Cameron 	case DMA_BIDIRECTIONAL:
4487edd16368SStephen M. Cameron 		/* This can happen if a buggy application does a scsi passthru
4488edd16368SStephen M. Cameron 		 * and sets both inlen and outlen to non-zero. ( see
4489edd16368SStephen M. Cameron 		 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
4490edd16368SStephen M. Cameron 		 */
4491edd16368SStephen M. Cameron 
4492a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4493a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_RSVD);
4494edd16368SStephen M. Cameron 		/* This is technically wrong, and hpsa controllers should
4495edd16368SStephen M. Cameron 		 * reject it with CMD_INVALID, which is the most correct
4496edd16368SStephen M. Cameron 		 * response, but non-fibre backends appear to let it
4497edd16368SStephen M. Cameron 		 * slide by, and give the same results as if this field
4498edd16368SStephen M. Cameron 		 * were set correctly.  Either way is acceptable for
4499edd16368SStephen M. Cameron 		 * our purposes here.
4500edd16368SStephen M. Cameron 		 */
4501edd16368SStephen M. Cameron 
4502edd16368SStephen M. Cameron 		break;
4503edd16368SStephen M. Cameron 
4504edd16368SStephen M. Cameron 	default:
4505edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4506edd16368SStephen M. Cameron 			cmd->sc_data_direction);
4507edd16368SStephen M. Cameron 		BUG();
4508edd16368SStephen M. Cameron 		break;
4509edd16368SStephen M. Cameron 	}
4510edd16368SStephen M. Cameron 
451133a2ffceSStephen M. Cameron 	if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
4512*73153fe5SWebb Scales 		hpsa_cmd_resolve_and_free(h, c);
4513edd16368SStephen M. Cameron 		return SCSI_MLQUEUE_HOST_BUSY;
4514edd16368SStephen M. Cameron 	}
4515edd16368SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
4516edd16368SStephen M. Cameron 	/* the cmd'll come back via intr handler in complete_scsi_command()  */
4517edd16368SStephen M. Cameron 	return 0;
4518edd16368SStephen M. Cameron }
4519edd16368SStephen M. Cameron 
4520360c73bdSStephen Cameron static void hpsa_cmd_init(struct ctlr_info *h, int index,
4521360c73bdSStephen Cameron 				struct CommandList *c)
4522360c73bdSStephen Cameron {
4523360c73bdSStephen Cameron 	dma_addr_t cmd_dma_handle, err_dma_handle;
4524360c73bdSStephen Cameron 
4525360c73bdSStephen Cameron 	/* Zero out all of commandlist except the last field, refcount */
4526360c73bdSStephen Cameron 	memset(c, 0, offsetof(struct CommandList, refcount));
4527360c73bdSStephen Cameron 	c->Header.tag = cpu_to_le64((u64) (index << DIRECT_LOOKUP_SHIFT));
4528360c73bdSStephen Cameron 	cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
4529360c73bdSStephen Cameron 	c->err_info = h->errinfo_pool + index;
4530360c73bdSStephen Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
4531360c73bdSStephen Cameron 	err_dma_handle = h->errinfo_pool_dhandle
4532360c73bdSStephen Cameron 	    + index * sizeof(*c->err_info);
4533360c73bdSStephen Cameron 	c->cmdindex = index;
4534360c73bdSStephen Cameron 	c->busaddr = (u32) cmd_dma_handle;
4535360c73bdSStephen Cameron 	c->ErrDesc.Addr = cpu_to_le64((u64) err_dma_handle);
4536360c73bdSStephen Cameron 	c->ErrDesc.Len = cpu_to_le32((u32) sizeof(*c->err_info));
4537360c73bdSStephen Cameron 	c->h = h;
4538a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_IDLE;
4539360c73bdSStephen Cameron }
4540360c73bdSStephen Cameron 
4541360c73bdSStephen Cameron static void hpsa_preinitialize_commands(struct ctlr_info *h)
4542360c73bdSStephen Cameron {
4543360c73bdSStephen Cameron 	int i;
4544360c73bdSStephen Cameron 
4545360c73bdSStephen Cameron 	for (i = 0; i < h->nr_cmds; i++) {
4546360c73bdSStephen Cameron 		struct CommandList *c = h->cmd_pool + i;
4547360c73bdSStephen Cameron 
4548360c73bdSStephen Cameron 		hpsa_cmd_init(h, i, c);
4549360c73bdSStephen Cameron 		atomic_set(&c->refcount, 0);
4550360c73bdSStephen Cameron 	}
4551360c73bdSStephen Cameron }
4552360c73bdSStephen Cameron 
4553360c73bdSStephen Cameron static inline void hpsa_cmd_partial_init(struct ctlr_info *h, int index,
4554360c73bdSStephen Cameron 				struct CommandList *c)
4555360c73bdSStephen Cameron {
4556360c73bdSStephen Cameron 	dma_addr_t cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
4557360c73bdSStephen Cameron 
4558*73153fe5SWebb Scales 	BUG_ON(c->cmdindex != index);
4559*73153fe5SWebb Scales 
4560360c73bdSStephen Cameron 	memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
4561360c73bdSStephen Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
4562360c73bdSStephen Cameron 	c->busaddr = (u32) cmd_dma_handle;
4563360c73bdSStephen Cameron }
4564360c73bdSStephen Cameron 
4565592a0ad5SWebb Scales static int hpsa_ioaccel_submit(struct ctlr_info *h,
4566592a0ad5SWebb Scales 		struct CommandList *c, struct scsi_cmnd *cmd,
4567592a0ad5SWebb Scales 		unsigned char *scsi3addr)
4568592a0ad5SWebb Scales {
4569592a0ad5SWebb Scales 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4570592a0ad5SWebb Scales 	int rc = IO_ACCEL_INELIGIBLE;
4571592a0ad5SWebb Scales 
4572592a0ad5SWebb Scales 	cmd->host_scribble = (unsigned char *) c;
4573592a0ad5SWebb Scales 
4574592a0ad5SWebb Scales 	if (dev->offload_enabled) {
4575592a0ad5SWebb Scales 		hpsa_cmd_init(h, c->cmdindex, c);
4576592a0ad5SWebb Scales 		c->cmd_type = CMD_SCSI;
4577592a0ad5SWebb Scales 		c->scsi_cmd = cmd;
4578592a0ad5SWebb Scales 		rc = hpsa_scsi_ioaccel_raid_map(h, c);
4579592a0ad5SWebb Scales 		if (rc < 0)     /* scsi_dma_map failed. */
4580592a0ad5SWebb Scales 			rc = SCSI_MLQUEUE_HOST_BUSY;
4581a3144e0bSJoe Handzik 	} else if (dev->hba_ioaccel_enabled) {
4582592a0ad5SWebb Scales 		hpsa_cmd_init(h, c->cmdindex, c);
4583592a0ad5SWebb Scales 		c->cmd_type = CMD_SCSI;
4584592a0ad5SWebb Scales 		c->scsi_cmd = cmd;
4585592a0ad5SWebb Scales 		rc = hpsa_scsi_ioaccel_direct_map(h, c);
4586592a0ad5SWebb Scales 		if (rc < 0)     /* scsi_dma_map failed. */
4587592a0ad5SWebb Scales 			rc = SCSI_MLQUEUE_HOST_BUSY;
4588592a0ad5SWebb Scales 	}
4589592a0ad5SWebb Scales 	return rc;
4590592a0ad5SWebb Scales }
4591592a0ad5SWebb Scales 
4592080ef1ccSDon Brace static void hpsa_command_resubmit_worker(struct work_struct *work)
4593080ef1ccSDon Brace {
4594080ef1ccSDon Brace 	struct scsi_cmnd *cmd;
4595080ef1ccSDon Brace 	struct hpsa_scsi_dev_t *dev;
45968a0ff92cSWebb Scales 	struct CommandList *c = container_of(work, struct CommandList, work);
4597080ef1ccSDon Brace 
4598080ef1ccSDon Brace 	cmd = c->scsi_cmd;
4599080ef1ccSDon Brace 	dev = cmd->device->hostdata;
4600080ef1ccSDon Brace 	if (!dev) {
4601080ef1ccSDon Brace 		cmd->result = DID_NO_CONNECT << 16;
46028a0ff92cSWebb Scales 		return hpsa_cmd_free_and_done(c->h, c, cmd);
4603080ef1ccSDon Brace 	}
4604a58e7e53SWebb Scales 	if (c->abort_pending)
4605a58e7e53SWebb Scales 		return hpsa_cmd_abort_and_free(c->h, c, cmd);
4606592a0ad5SWebb Scales 	if (c->cmd_type == CMD_IOACCEL2) {
4607592a0ad5SWebb Scales 		struct ctlr_info *h = c->h;
4608592a0ad5SWebb Scales 		struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
4609592a0ad5SWebb Scales 		int rc;
4610592a0ad5SWebb Scales 
4611592a0ad5SWebb Scales 		if (c2->error_data.serv_response ==
4612592a0ad5SWebb Scales 				IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL) {
4613592a0ad5SWebb Scales 			rc = hpsa_ioaccel_submit(h, c, cmd, dev->scsi3addr);
4614592a0ad5SWebb Scales 			if (rc == 0)
4615592a0ad5SWebb Scales 				return;
4616592a0ad5SWebb Scales 			if (rc == SCSI_MLQUEUE_HOST_BUSY) {
4617592a0ad5SWebb Scales 				/*
4618592a0ad5SWebb Scales 				 * If we get here, it means dma mapping failed.
4619592a0ad5SWebb Scales 				 * Try again via scsi mid layer, which will
4620592a0ad5SWebb Scales 				 * then get SCSI_MLQUEUE_HOST_BUSY.
4621592a0ad5SWebb Scales 				 */
4622592a0ad5SWebb Scales 				cmd->result = DID_IMM_RETRY << 16;
46238a0ff92cSWebb Scales 				return hpsa_cmd_free_and_done(h, c, cmd);
4624592a0ad5SWebb Scales 			}
4625592a0ad5SWebb Scales 			/* else, fall thru and resubmit down CISS path */
4626592a0ad5SWebb Scales 		}
4627592a0ad5SWebb Scales 	}
4628360c73bdSStephen Cameron 	hpsa_cmd_partial_init(c->h, c->cmdindex, c);
4629080ef1ccSDon Brace 	if (hpsa_ciss_submit(c->h, c, cmd, dev->scsi3addr)) {
4630080ef1ccSDon Brace 		/*
4631080ef1ccSDon Brace 		 * If we get here, it means dma mapping failed. Try
4632080ef1ccSDon Brace 		 * again via scsi mid layer, which will then get
4633080ef1ccSDon Brace 		 * SCSI_MLQUEUE_HOST_BUSY.
4634592a0ad5SWebb Scales 		 *
4635592a0ad5SWebb Scales 		 * hpsa_ciss_submit will have already freed c
4636592a0ad5SWebb Scales 		 * if it encountered a dma mapping failure.
4637080ef1ccSDon Brace 		 */
4638080ef1ccSDon Brace 		cmd->result = DID_IMM_RETRY << 16;
4639080ef1ccSDon Brace 		cmd->scsi_done(cmd);
4640080ef1ccSDon Brace 	}
4641080ef1ccSDon Brace }
4642080ef1ccSDon Brace 
4643574f05d3SStephen Cameron /* Running in struct Scsi_Host->host_lock less mode */
4644574f05d3SStephen Cameron static int hpsa_scsi_queue_command(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
4645574f05d3SStephen Cameron {
4646574f05d3SStephen Cameron 	struct ctlr_info *h;
4647574f05d3SStephen Cameron 	struct hpsa_scsi_dev_t *dev;
4648574f05d3SStephen Cameron 	unsigned char scsi3addr[8];
4649574f05d3SStephen Cameron 	struct CommandList *c;
4650574f05d3SStephen Cameron 	int rc = 0;
4651574f05d3SStephen Cameron 
4652574f05d3SStephen Cameron 	/* Get the ptr to our adapter structure out of cmd->host. */
4653574f05d3SStephen Cameron 	h = sdev_to_hba(cmd->device);
4654*73153fe5SWebb Scales 
4655*73153fe5SWebb Scales 	BUG_ON(cmd->request->tag < 0);
4656*73153fe5SWebb Scales 
4657574f05d3SStephen Cameron 	dev = cmd->device->hostdata;
4658574f05d3SStephen Cameron 	if (!dev) {
4659574f05d3SStephen Cameron 		cmd->result = DID_NO_CONNECT << 16;
4660574f05d3SStephen Cameron 		cmd->scsi_done(cmd);
4661574f05d3SStephen Cameron 		return 0;
4662574f05d3SStephen Cameron 	}
4663*73153fe5SWebb Scales 
4664574f05d3SStephen Cameron 	memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
4665574f05d3SStephen Cameron 
4666574f05d3SStephen Cameron 	if (unlikely(lockup_detected(h))) {
466725163bd5SWebb Scales 		cmd->result = DID_NO_CONNECT << 16;
4668574f05d3SStephen Cameron 		cmd->scsi_done(cmd);
4669574f05d3SStephen Cameron 		return 0;
4670574f05d3SStephen Cameron 	}
4671*73153fe5SWebb Scales 	c = cmd_tagged_alloc(h, cmd);
4672574f05d3SStephen Cameron 
4673407863cbSStephen Cameron 	/*
4674407863cbSStephen Cameron 	 * Call alternate submit routine for I/O accelerated commands.
4675574f05d3SStephen Cameron 	 * Retries always go down the normal I/O path.
4676574f05d3SStephen Cameron 	 */
4677574f05d3SStephen Cameron 	if (likely(cmd->retries == 0 &&
4678574f05d3SStephen Cameron 		cmd->request->cmd_type == REQ_TYPE_FS &&
4679574f05d3SStephen Cameron 		h->acciopath_status)) {
4680592a0ad5SWebb Scales 		rc = hpsa_ioaccel_submit(h, c, cmd, scsi3addr);
4681574f05d3SStephen Cameron 		if (rc == 0)
4682592a0ad5SWebb Scales 			return 0;
4683592a0ad5SWebb Scales 		if (rc == SCSI_MLQUEUE_HOST_BUSY) {
4684*73153fe5SWebb Scales 			hpsa_cmd_resolve_and_free(h, c);
4685574f05d3SStephen Cameron 			return SCSI_MLQUEUE_HOST_BUSY;
4686574f05d3SStephen Cameron 		}
4687574f05d3SStephen Cameron 	}
4688574f05d3SStephen Cameron 	return hpsa_ciss_submit(h, c, cmd, scsi3addr);
4689574f05d3SStephen Cameron }
4690574f05d3SStephen Cameron 
46918ebc9248SWebb Scales static void hpsa_scan_complete(struct ctlr_info *h)
46925f389360SStephen M. Cameron {
46935f389360SStephen M. Cameron 	unsigned long flags;
46945f389360SStephen M. Cameron 
46955f389360SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
46965f389360SStephen M. Cameron 	h->scan_finished = 1;
46975f389360SStephen M. Cameron 	wake_up_all(&h->scan_wait_queue);
46985f389360SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
46995f389360SStephen M. Cameron }
47005f389360SStephen M. Cameron 
4701a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *sh)
4702a08a8471SStephen M. Cameron {
4703a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
4704a08a8471SStephen M. Cameron 	unsigned long flags;
4705a08a8471SStephen M. Cameron 
47068ebc9248SWebb Scales 	/*
47078ebc9248SWebb Scales 	 * Don't let rescans be initiated on a controller known to be locked
47088ebc9248SWebb Scales 	 * up.  If the controller locks up *during* a rescan, that thread is
47098ebc9248SWebb Scales 	 * probably hosed, but at least we can prevent new rescan threads from
47108ebc9248SWebb Scales 	 * piling up on a locked up controller.
47118ebc9248SWebb Scales 	 */
47128ebc9248SWebb Scales 	if (unlikely(lockup_detected(h)))
47138ebc9248SWebb Scales 		return hpsa_scan_complete(h);
47145f389360SStephen M. Cameron 
4715a08a8471SStephen M. Cameron 	/* wait until any scan already in progress is finished. */
4716a08a8471SStephen M. Cameron 	while (1) {
4717a08a8471SStephen M. Cameron 		spin_lock_irqsave(&h->scan_lock, flags);
4718a08a8471SStephen M. Cameron 		if (h->scan_finished)
4719a08a8471SStephen M. Cameron 			break;
4720a08a8471SStephen M. Cameron 		spin_unlock_irqrestore(&h->scan_lock, flags);
4721a08a8471SStephen M. Cameron 		wait_event(h->scan_wait_queue, h->scan_finished);
4722a08a8471SStephen M. Cameron 		/* Note: We don't need to worry about a race between this
4723a08a8471SStephen M. Cameron 		 * thread and driver unload because the midlayer will
4724a08a8471SStephen M. Cameron 		 * have incremented the reference count, so unload won't
4725a08a8471SStephen M. Cameron 		 * happen if we're in here.
4726a08a8471SStephen M. Cameron 		 */
4727a08a8471SStephen M. Cameron 	}
4728a08a8471SStephen M. Cameron 	h->scan_finished = 0; /* mark scan as in progress */
4729a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4730a08a8471SStephen M. Cameron 
47318ebc9248SWebb Scales 	if (unlikely(lockup_detected(h)))
47328ebc9248SWebb Scales 		return hpsa_scan_complete(h);
47335f389360SStephen M. Cameron 
4734a08a8471SStephen M. Cameron 	hpsa_update_scsi_devices(h, h->scsi_host->host_no);
4735a08a8471SStephen M. Cameron 
47368ebc9248SWebb Scales 	hpsa_scan_complete(h);
4737a08a8471SStephen M. Cameron }
4738a08a8471SStephen M. Cameron 
47397c0a0229SDon Brace static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth)
47407c0a0229SDon Brace {
474103383736SDon Brace 	struct hpsa_scsi_dev_t *logical_drive = sdev->hostdata;
474203383736SDon Brace 
474303383736SDon Brace 	if (!logical_drive)
474403383736SDon Brace 		return -ENODEV;
47457c0a0229SDon Brace 
47467c0a0229SDon Brace 	if (qdepth < 1)
47477c0a0229SDon Brace 		qdepth = 1;
474803383736SDon Brace 	else if (qdepth > logical_drive->queue_depth)
474903383736SDon Brace 		qdepth = logical_drive->queue_depth;
475003383736SDon Brace 
475103383736SDon Brace 	return scsi_change_queue_depth(sdev, qdepth);
47527c0a0229SDon Brace }
47537c0a0229SDon Brace 
4754a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
4755a08a8471SStephen M. Cameron 	unsigned long elapsed_time)
4756a08a8471SStephen M. Cameron {
4757a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
4758a08a8471SStephen M. Cameron 	unsigned long flags;
4759a08a8471SStephen M. Cameron 	int finished;
4760a08a8471SStephen M. Cameron 
4761a08a8471SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
4762a08a8471SStephen M. Cameron 	finished = h->scan_finished;
4763a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4764a08a8471SStephen M. Cameron 	return finished;
4765a08a8471SStephen M. Cameron }
4766a08a8471SStephen M. Cameron 
4767edd16368SStephen M. Cameron static void hpsa_unregister_scsi(struct ctlr_info *h)
4768edd16368SStephen M. Cameron {
4769edd16368SStephen M. Cameron 	/* we are being forcibly unloaded, and may not refuse. */
4770edd16368SStephen M. Cameron 	scsi_remove_host(h->scsi_host);
4771edd16368SStephen M. Cameron 	scsi_host_put(h->scsi_host);
4772edd16368SStephen M. Cameron 	h->scsi_host = NULL;
4773edd16368SStephen M. Cameron }
4774edd16368SStephen M. Cameron 
4775edd16368SStephen M. Cameron static int hpsa_register_scsi(struct ctlr_info *h)
4776edd16368SStephen M. Cameron {
4777b705690dSStephen M. Cameron 	struct Scsi_Host *sh;
4778b705690dSStephen M. Cameron 	int error;
4779edd16368SStephen M. Cameron 
4780b705690dSStephen M. Cameron 	sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
4781b705690dSStephen M. Cameron 	if (sh == NULL)
4782b705690dSStephen M. Cameron 		goto fail;
4783b705690dSStephen M. Cameron 
4784b705690dSStephen M. Cameron 	sh->io_port = 0;
4785b705690dSStephen M. Cameron 	sh->n_io_port = 0;
4786b705690dSStephen M. Cameron 	sh->this_id = -1;
4787b705690dSStephen M. Cameron 	sh->max_channel = 3;
4788b705690dSStephen M. Cameron 	sh->max_cmd_len = MAX_COMMAND_SIZE;
4789b705690dSStephen M. Cameron 	sh->max_lun = HPSA_MAX_LUN;
4790b705690dSStephen M. Cameron 	sh->max_id = HPSA_MAX_LUN;
479141ce4c35SStephen Cameron 	sh->can_queue = h->nr_cmds - HPSA_NRESERVED_CMDS;
4792d54c5c24SStephen Cameron 	sh->cmd_per_lun = sh->can_queue;
4793b705690dSStephen M. Cameron 	sh->sg_tablesize = h->maxsgentries;
4794b705690dSStephen M. Cameron 	h->scsi_host = sh;
4795b705690dSStephen M. Cameron 	sh->hostdata[0] = (unsigned long) h;
4796b705690dSStephen M. Cameron 	sh->irq = h->intr[h->intr_mode];
4797b705690dSStephen M. Cameron 	sh->unique_id = sh->irq;
4798*73153fe5SWebb Scales 	error = scsi_init_shared_tag_map(sh, sh->can_queue);
4799*73153fe5SWebb Scales 	if (error) {
4800*73153fe5SWebb Scales 		dev_err(&h->pdev->dev,
4801*73153fe5SWebb Scales 			"%s: scsi_init_shared_tag_map failed for controller %d\n",
4802*73153fe5SWebb Scales 			__func__, h->ctlr);
4803b705690dSStephen M. Cameron 		goto fail_host_put;
4804*73153fe5SWebb Scales 	}
4805*73153fe5SWebb Scales 	error = scsi_add_host(sh, &h->pdev->dev);
4806*73153fe5SWebb Scales 	if (error) {
4807*73153fe5SWebb Scales 		dev_err(&h->pdev->dev, "%s: scsi_add_host failed for controller %d\n",
4808*73153fe5SWebb Scales 			__func__, h->ctlr);
4809*73153fe5SWebb Scales 		goto fail_host_put;
4810*73153fe5SWebb Scales 	}
4811b705690dSStephen M. Cameron 	scsi_scan_host(sh);
4812b705690dSStephen M. Cameron 	return 0;
4813b705690dSStephen M. Cameron 
4814b705690dSStephen M. Cameron  fail_host_put:
4815b705690dSStephen M. Cameron 	scsi_host_put(sh);
4816b705690dSStephen M. Cameron 	return error;
4817b705690dSStephen M. Cameron  fail:
4818b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_host_alloc"
4819b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
4820b705690dSStephen M. Cameron 	return -ENOMEM;
4821edd16368SStephen M. Cameron }
4822edd16368SStephen M. Cameron 
4823b69324ffSWebb Scales /*
4824*73153fe5SWebb Scales  * The block layer has already gone to the trouble of picking out a unique,
4825*73153fe5SWebb Scales  * small-integer tag for this request.  We use an offset from that value as
4826*73153fe5SWebb Scales  * an index to select our command block.  (The offset allows us to reserve the
4827*73153fe5SWebb Scales  * low-numbered entries for our own uses.)
4828*73153fe5SWebb Scales  */
4829*73153fe5SWebb Scales static int hpsa_get_cmd_index(struct scsi_cmnd *scmd)
4830*73153fe5SWebb Scales {
4831*73153fe5SWebb Scales 	int idx = scmd->request->tag;
4832*73153fe5SWebb Scales 
4833*73153fe5SWebb Scales 	if (idx < 0)
4834*73153fe5SWebb Scales 		return idx;
4835*73153fe5SWebb Scales 
4836*73153fe5SWebb Scales 	/* Offset to leave space for internal cmds. */
4837*73153fe5SWebb Scales 	return idx += HPSA_NRESERVED_CMDS;
4838*73153fe5SWebb Scales }
4839*73153fe5SWebb Scales 
4840*73153fe5SWebb Scales /*
4841b69324ffSWebb Scales  * Send a TEST_UNIT_READY command to the specified LUN using the specified
4842b69324ffSWebb Scales  * reply queue; returns zero if the unit is ready, and non-zero otherwise.
4843b69324ffSWebb Scales  */
4844b69324ffSWebb Scales static int hpsa_send_test_unit_ready(struct ctlr_info *h,
4845b69324ffSWebb Scales 				struct CommandList *c, unsigned char lunaddr[],
4846b69324ffSWebb Scales 				int reply_queue)
4847edd16368SStephen M. Cameron {
48488919358eSTomas Henzl 	int rc;
4849edd16368SStephen M. Cameron 
4850a2dac136SStephen M. Cameron 	/* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
4851a2dac136SStephen M. Cameron 	(void) fill_cmd(c, TEST_UNIT_READY, h,
4852a2dac136SStephen M. Cameron 			NULL, 0, 0, lunaddr, TYPE_CMD);
4853b69324ffSWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
485425163bd5SWebb Scales 	if (rc)
4855b69324ffSWebb Scales 		return rc;
4856edd16368SStephen M. Cameron 	/* no unmap needed here because no data xfer. */
4857edd16368SStephen M. Cameron 
4858b69324ffSWebb Scales 	/* Check if the unit is already ready. */
4859edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus == CMD_SUCCESS)
4860b69324ffSWebb Scales 		return 0;
4861edd16368SStephen M. Cameron 
4862b69324ffSWebb Scales 	/*
4863b69324ffSWebb Scales 	 * The first command sent after reset will receive "unit attention" to
4864b69324ffSWebb Scales 	 * indicate that the LUN has been reset...this is actually what we're
4865b69324ffSWebb Scales 	 * looking for (but, success is good too).
4866b69324ffSWebb Scales 	 */
4867edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
4868edd16368SStephen M. Cameron 		c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
4869edd16368SStephen M. Cameron 			(c->err_info->SenseInfo[2] == NO_SENSE ||
4870edd16368SStephen M. Cameron 			 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
4871b69324ffSWebb Scales 		return 0;
4872b69324ffSWebb Scales 
4873b69324ffSWebb Scales 	return 1;
4874b69324ffSWebb Scales }
4875b69324ffSWebb Scales 
4876b69324ffSWebb Scales /*
4877b69324ffSWebb Scales  * Wait for a TEST_UNIT_READY command to complete, retrying as necessary;
4878b69324ffSWebb Scales  * returns zero when the unit is ready, and non-zero when giving up.
4879b69324ffSWebb Scales  */
4880b69324ffSWebb Scales static int hpsa_wait_for_test_unit_ready(struct ctlr_info *h,
4881b69324ffSWebb Scales 				struct CommandList *c,
4882b69324ffSWebb Scales 				unsigned char lunaddr[], int reply_queue)
4883b69324ffSWebb Scales {
4884b69324ffSWebb Scales 	int rc;
4885b69324ffSWebb Scales 	int count = 0;
4886b69324ffSWebb Scales 	int waittime = 1; /* seconds */
4887b69324ffSWebb Scales 
4888b69324ffSWebb Scales 	/* Send test unit ready until device ready, or give up. */
4889b69324ffSWebb Scales 	for (count = 0; count < HPSA_TUR_RETRY_LIMIT; count++) {
4890b69324ffSWebb Scales 
4891b69324ffSWebb Scales 		/*
4892b69324ffSWebb Scales 		 * Wait for a bit.  do this first, because if we send
4893b69324ffSWebb Scales 		 * the TUR right away, the reset will just abort it.
4894b69324ffSWebb Scales 		 */
4895b69324ffSWebb Scales 		msleep(1000 * waittime);
4896b69324ffSWebb Scales 
4897b69324ffSWebb Scales 		rc = hpsa_send_test_unit_ready(h, c, lunaddr, reply_queue);
4898b69324ffSWebb Scales 		if (!rc)
4899edd16368SStephen M. Cameron 			break;
4900b69324ffSWebb Scales 
4901b69324ffSWebb Scales 		/* Increase wait time with each try, up to a point. */
4902b69324ffSWebb Scales 		if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
4903b69324ffSWebb Scales 			waittime *= 2;
4904b69324ffSWebb Scales 
4905b69324ffSWebb Scales 		dev_warn(&h->pdev->dev,
4906b69324ffSWebb Scales 			 "waiting %d secs for device to become ready.\n",
4907b69324ffSWebb Scales 			 waittime);
4908b69324ffSWebb Scales 	}
4909b69324ffSWebb Scales 
4910b69324ffSWebb Scales 	return rc;
4911b69324ffSWebb Scales }
4912b69324ffSWebb Scales 
4913b69324ffSWebb Scales static int wait_for_device_to_become_ready(struct ctlr_info *h,
4914b69324ffSWebb Scales 					   unsigned char lunaddr[],
4915b69324ffSWebb Scales 					   int reply_queue)
4916b69324ffSWebb Scales {
4917b69324ffSWebb Scales 	int first_queue;
4918b69324ffSWebb Scales 	int last_queue;
4919b69324ffSWebb Scales 	int rq;
4920b69324ffSWebb Scales 	int rc = 0;
4921b69324ffSWebb Scales 	struct CommandList *c;
4922b69324ffSWebb Scales 
4923b69324ffSWebb Scales 	c = cmd_alloc(h);
4924b69324ffSWebb Scales 
4925b69324ffSWebb Scales 	/*
4926b69324ffSWebb Scales 	 * If no specific reply queue was requested, then send the TUR
4927b69324ffSWebb Scales 	 * repeatedly, requesting a reply on each reply queue; otherwise execute
4928b69324ffSWebb Scales 	 * the loop exactly once using only the specified queue.
4929b69324ffSWebb Scales 	 */
4930b69324ffSWebb Scales 	if (reply_queue == DEFAULT_REPLY_QUEUE) {
4931b69324ffSWebb Scales 		first_queue = 0;
4932b69324ffSWebb Scales 		last_queue = h->nreply_queues - 1;
4933b69324ffSWebb Scales 	} else {
4934b69324ffSWebb Scales 		first_queue = reply_queue;
4935b69324ffSWebb Scales 		last_queue = reply_queue;
4936b69324ffSWebb Scales 	}
4937b69324ffSWebb Scales 
4938b69324ffSWebb Scales 	for (rq = first_queue; rq <= last_queue; rq++) {
4939b69324ffSWebb Scales 		rc = hpsa_wait_for_test_unit_ready(h, c, lunaddr, rq);
4940b69324ffSWebb Scales 		if (rc)
4941b69324ffSWebb Scales 			break;
4942edd16368SStephen M. Cameron 	}
4943edd16368SStephen M. Cameron 
4944edd16368SStephen M. Cameron 	if (rc)
4945edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "giving up on device.\n");
4946edd16368SStephen M. Cameron 	else
4947edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "device is ready.\n");
4948edd16368SStephen M. Cameron 
494945fcb86eSStephen Cameron 	cmd_free(h, c);
4950edd16368SStephen M. Cameron 	return rc;
4951edd16368SStephen M. Cameron }
4952edd16368SStephen M. Cameron 
4953edd16368SStephen M. Cameron /* Need at least one of these error handlers to keep ../scsi/hosts.c from
4954edd16368SStephen M. Cameron  * complaining.  Doing a host- or bus-reset can't do anything good here.
4955edd16368SStephen M. Cameron  */
4956edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
4957edd16368SStephen M. Cameron {
4958edd16368SStephen M. Cameron 	int rc;
4959edd16368SStephen M. Cameron 	struct ctlr_info *h;
4960edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
4961*73153fe5SWebb Scales 	char msg[40];
4962edd16368SStephen M. Cameron 
4963edd16368SStephen M. Cameron 	/* find the controller to which the command to be aborted was sent */
4964edd16368SStephen M. Cameron 	h = sdev_to_hba(scsicmd->device);
4965edd16368SStephen M. Cameron 	if (h == NULL) /* paranoia */
4966edd16368SStephen M. Cameron 		return FAILED;
4967e345893bSDon Brace 
4968e345893bSDon Brace 	if (lockup_detected(h))
4969e345893bSDon Brace 		return FAILED;
4970e345893bSDon Brace 
4971edd16368SStephen M. Cameron 	dev = scsicmd->device->hostdata;
4972edd16368SStephen M. Cameron 	if (!dev) {
4973edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
4974edd16368SStephen M. Cameron 			"device lookup failed.\n");
4975edd16368SStephen M. Cameron 		return FAILED;
4976edd16368SStephen M. Cameron 	}
497725163bd5SWebb Scales 
497825163bd5SWebb Scales 	/* if controller locked up, we can guarantee command won't complete */
497925163bd5SWebb Scales 	if (lockup_detected(h)) {
4980*73153fe5SWebb Scales 		sprintf(msg, "cmd %d RESET FAILED, lockup detected",
4981*73153fe5SWebb Scales 				hpsa_get_cmd_index(scsicmd));
4982*73153fe5SWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
498325163bd5SWebb Scales 		return FAILED;
498425163bd5SWebb Scales 	}
498525163bd5SWebb Scales 
498625163bd5SWebb Scales 	/* this reset request might be the result of a lockup; check */
498725163bd5SWebb Scales 	if (detect_controller_lockup(h)) {
4988*73153fe5SWebb Scales 		sprintf(msg, "cmd %d RESET FAILED, new lockup detected",
4989*73153fe5SWebb Scales 				hpsa_get_cmd_index(scsicmd));
4990*73153fe5SWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
499125163bd5SWebb Scales 		return FAILED;
499225163bd5SWebb Scales 	}
499325163bd5SWebb Scales 
499425163bd5SWebb Scales 	hpsa_show_dev_msg(KERN_WARNING, h, dev, "resetting");
499525163bd5SWebb Scales 
4996edd16368SStephen M. Cameron 	/* send a reset to the SCSI LUN which the command was sent to */
499725163bd5SWebb Scales 	rc = hpsa_send_reset(h, dev->scsi3addr, HPSA_RESET_TYPE_LUN,
499825163bd5SWebb Scales 			     DEFAULT_REPLY_QUEUE);
4999b69324ffSWebb Scales 	if (rc == 0)
5000edd16368SStephen M. Cameron 		return SUCCESS;
5001edd16368SStephen M. Cameron 
500225163bd5SWebb Scales 	dev_warn(&h->pdev->dev,
500325163bd5SWebb Scales 		"scsi %d:%d:%d:%d reset failed\n",
500425163bd5SWebb Scales 		h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
5005edd16368SStephen M. Cameron 	return FAILED;
5006edd16368SStephen M. Cameron }
5007edd16368SStephen M. Cameron 
50086cba3f19SStephen M. Cameron static void swizzle_abort_tag(u8 *tag)
50096cba3f19SStephen M. Cameron {
50106cba3f19SStephen M. Cameron 	u8 original_tag[8];
50116cba3f19SStephen M. Cameron 
50126cba3f19SStephen M. Cameron 	memcpy(original_tag, tag, 8);
50136cba3f19SStephen M. Cameron 	tag[0] = original_tag[3];
50146cba3f19SStephen M. Cameron 	tag[1] = original_tag[2];
50156cba3f19SStephen M. Cameron 	tag[2] = original_tag[1];
50166cba3f19SStephen M. Cameron 	tag[3] = original_tag[0];
50176cba3f19SStephen M. Cameron 	tag[4] = original_tag[7];
50186cba3f19SStephen M. Cameron 	tag[5] = original_tag[6];
50196cba3f19SStephen M. Cameron 	tag[6] = original_tag[5];
50206cba3f19SStephen M. Cameron 	tag[7] = original_tag[4];
50216cba3f19SStephen M. Cameron }
50226cba3f19SStephen M. Cameron 
502317eb87d2SScott Teel static void hpsa_get_tag(struct ctlr_info *h,
50242b08b3e9SDon Brace 	struct CommandList *c, __le32 *taglower, __le32 *tagupper)
502517eb87d2SScott Teel {
50262b08b3e9SDon Brace 	u64 tag;
502717eb87d2SScott Teel 	if (c->cmd_type == CMD_IOACCEL1) {
502817eb87d2SScott Teel 		struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
502917eb87d2SScott Teel 			&h->ioaccel_cmd_pool[c->cmdindex];
50302b08b3e9SDon Brace 		tag = le64_to_cpu(cm1->tag);
50312b08b3e9SDon Brace 		*tagupper = cpu_to_le32(tag >> 32);
50322b08b3e9SDon Brace 		*taglower = cpu_to_le32(tag);
503354b6e9e9SScott Teel 		return;
503454b6e9e9SScott Teel 	}
503554b6e9e9SScott Teel 	if (c->cmd_type == CMD_IOACCEL2) {
503654b6e9e9SScott Teel 		struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
503754b6e9e9SScott Teel 			&h->ioaccel2_cmd_pool[c->cmdindex];
5038dd0e19f3SScott Teel 		/* upper tag not used in ioaccel2 mode */
5039dd0e19f3SScott Teel 		memset(tagupper, 0, sizeof(*tagupper));
5040dd0e19f3SScott Teel 		*taglower = cm2->Tag;
504154b6e9e9SScott Teel 		return;
504254b6e9e9SScott Teel 	}
50432b08b3e9SDon Brace 	tag = le64_to_cpu(c->Header.tag);
50442b08b3e9SDon Brace 	*tagupper = cpu_to_le32(tag >> 32);
50452b08b3e9SDon Brace 	*taglower = cpu_to_le32(tag);
504617eb87d2SScott Teel }
504754b6e9e9SScott Teel 
504875167d2cSStephen M. Cameron static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
50499b5c48c2SStephen Cameron 	struct CommandList *abort, int reply_queue)
505075167d2cSStephen M. Cameron {
505175167d2cSStephen M. Cameron 	int rc = IO_OK;
505275167d2cSStephen M. Cameron 	struct CommandList *c;
505375167d2cSStephen M. Cameron 	struct ErrorInfo *ei;
50542b08b3e9SDon Brace 	__le32 tagupper, taglower;
505575167d2cSStephen M. Cameron 
505645fcb86eSStephen Cameron 	c = cmd_alloc(h);
505775167d2cSStephen M. Cameron 
5058a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no buffer to map */
50599b5c48c2SStephen Cameron 	(void) fill_cmd(c, HPSA_ABORT_MSG, h, &abort->Header.tag,
5060a2dac136SStephen M. Cameron 		0, 0, scsi3addr, TYPE_MSG);
50619b5c48c2SStephen Cameron 	if (h->needs_abort_tags_swizzled)
50626cba3f19SStephen M. Cameron 		swizzle_abort_tag(&c->Request.CDB[4]);
506325163bd5SWebb Scales 	(void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
506417eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
506525163bd5SWebb Scales 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd(abort) completed.\n",
506617eb87d2SScott Teel 		__func__, tagupper, taglower);
506775167d2cSStephen M. Cameron 	/* no unmap needed here because no data xfer. */
506875167d2cSStephen M. Cameron 
506975167d2cSStephen M. Cameron 	ei = c->err_info;
507075167d2cSStephen M. Cameron 	switch (ei->CommandStatus) {
507175167d2cSStephen M. Cameron 	case CMD_SUCCESS:
507275167d2cSStephen M. Cameron 		break;
50739437ac43SStephen Cameron 	case CMD_TMF_STATUS:
50749437ac43SStephen Cameron 		rc = hpsa_evaluate_tmf_status(h, c);
50759437ac43SStephen Cameron 		break;
507675167d2cSStephen M. Cameron 	case CMD_UNABORTABLE: /* Very common, don't make noise. */
507775167d2cSStephen M. Cameron 		rc = -1;
507875167d2cSStephen M. Cameron 		break;
507975167d2cSStephen M. Cameron 	default:
508075167d2cSStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
508117eb87d2SScott Teel 			__func__, tagupper, taglower);
5082d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
508375167d2cSStephen M. Cameron 		rc = -1;
508475167d2cSStephen M. Cameron 		break;
508575167d2cSStephen M. Cameron 	}
508645fcb86eSStephen Cameron 	cmd_free(h, c);
5087dd0e19f3SScott Teel 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n",
5088dd0e19f3SScott Teel 		__func__, tagupper, taglower);
508975167d2cSStephen M. Cameron 	return rc;
509075167d2cSStephen M. Cameron }
509175167d2cSStephen M. Cameron 
50928be986ccSStephen Cameron static void setup_ioaccel2_abort_cmd(struct CommandList *c, struct ctlr_info *h,
50938be986ccSStephen Cameron 	struct CommandList *command_to_abort, int reply_queue)
50948be986ccSStephen Cameron {
50958be986ccSStephen Cameron 	struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
50968be986ccSStephen Cameron 	struct hpsa_tmf_struct *ac = (struct hpsa_tmf_struct *) c2;
50978be986ccSStephen Cameron 	struct io_accel2_cmd *c2a =
50988be986ccSStephen Cameron 		&h->ioaccel2_cmd_pool[command_to_abort->cmdindex];
5099a58e7e53SWebb Scales 	struct scsi_cmnd *scmd = command_to_abort->scsi_cmd;
51008be986ccSStephen Cameron 	struct hpsa_scsi_dev_t *dev = scmd->device->hostdata;
51018be986ccSStephen Cameron 
51028be986ccSStephen Cameron 	/*
51038be986ccSStephen Cameron 	 * We're overlaying struct hpsa_tmf_struct on top of something which
51048be986ccSStephen Cameron 	 * was allocated as a struct io_accel2_cmd, so we better be sure it
51058be986ccSStephen Cameron 	 * actually fits, and doesn't overrun the error info space.
51068be986ccSStephen Cameron 	 */
51078be986ccSStephen Cameron 	BUILD_BUG_ON(sizeof(struct hpsa_tmf_struct) >
51088be986ccSStephen Cameron 			sizeof(struct io_accel2_cmd));
51098be986ccSStephen Cameron 	BUG_ON(offsetof(struct io_accel2_cmd, error_data) <
51108be986ccSStephen Cameron 			offsetof(struct hpsa_tmf_struct, error_len) +
51118be986ccSStephen Cameron 				sizeof(ac->error_len));
51128be986ccSStephen Cameron 
51138be986ccSStephen Cameron 	c->cmd_type = IOACCEL2_TMF;
5114a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_BUSY;
5115a58e7e53SWebb Scales 
51168be986ccSStephen Cameron 	/* Adjust the DMA address to point to the accelerated command buffer */
51178be986ccSStephen Cameron 	c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
51188be986ccSStephen Cameron 				(c->cmdindex * sizeof(struct io_accel2_cmd));
51198be986ccSStephen Cameron 	BUG_ON(c->busaddr & 0x0000007F);
51208be986ccSStephen Cameron 
51218be986ccSStephen Cameron 	memset(ac, 0, sizeof(*c2)); /* yes this is correct */
51228be986ccSStephen Cameron 	ac->iu_type = IOACCEL2_IU_TMF_TYPE;
51238be986ccSStephen Cameron 	ac->reply_queue = reply_queue;
51248be986ccSStephen Cameron 	ac->tmf = IOACCEL2_TMF_ABORT;
51258be986ccSStephen Cameron 	ac->it_nexus = cpu_to_le32(dev->ioaccel_handle);
51268be986ccSStephen Cameron 	memset(ac->lun_id, 0, sizeof(ac->lun_id));
51278be986ccSStephen Cameron 	ac->tag = cpu_to_le64(c->cmdindex << DIRECT_LOOKUP_SHIFT);
51288be986ccSStephen Cameron 	ac->abort_tag = cpu_to_le64(le32_to_cpu(c2a->Tag));
51298be986ccSStephen Cameron 	ac->error_ptr = cpu_to_le64(c->busaddr +
51308be986ccSStephen Cameron 			offsetof(struct io_accel2_cmd, error_data));
51318be986ccSStephen Cameron 	ac->error_len = cpu_to_le32(sizeof(c2->error_data));
51328be986ccSStephen Cameron }
51338be986ccSStephen Cameron 
513454b6e9e9SScott Teel /* ioaccel2 path firmware cannot handle abort task requests.
513554b6e9e9SScott Teel  * Change abort requests to physical target reset, and send to the
513654b6e9e9SScott Teel  * address of the physical disk used for the ioaccel 2 command.
513754b6e9e9SScott Teel  * Return 0 on success (IO_OK)
513854b6e9e9SScott Teel  *	 -1 on failure
513954b6e9e9SScott Teel  */
514054b6e9e9SScott Teel 
514154b6e9e9SScott Teel static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
514225163bd5SWebb Scales 	unsigned char *scsi3addr, struct CommandList *abort, int reply_queue)
514354b6e9e9SScott Teel {
514454b6e9e9SScott Teel 	int rc = IO_OK;
514554b6e9e9SScott Teel 	struct scsi_cmnd *scmd; /* scsi command within request being aborted */
514654b6e9e9SScott Teel 	struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
514754b6e9e9SScott Teel 	unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
514854b6e9e9SScott Teel 	unsigned char *psa = &phys_scsi3addr[0];
514954b6e9e9SScott Teel 
515054b6e9e9SScott Teel 	/* Get a pointer to the hpsa logical device. */
51517fa3030cSStephen Cameron 	scmd = abort->scsi_cmd;
515254b6e9e9SScott Teel 	dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
515354b6e9e9SScott Teel 	if (dev == NULL) {
515454b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
515554b6e9e9SScott Teel 			"Cannot abort: no device pointer for command.\n");
515654b6e9e9SScott Teel 			return -1; /* not abortable */
515754b6e9e9SScott Teel 	}
515854b6e9e9SScott Teel 
51592ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
51602ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
51610d96ef5fSWebb Scales 			"scsi %d:%d:%d:%d %s scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
51622ba8bfc8SStephen M. Cameron 			h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
51630d96ef5fSWebb Scales 			"Reset as abort",
51642ba8bfc8SStephen M. Cameron 			scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
51652ba8bfc8SStephen M. Cameron 			scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]);
51662ba8bfc8SStephen M. Cameron 
516754b6e9e9SScott Teel 	if (!dev->offload_enabled) {
516854b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
516954b6e9e9SScott Teel 			"Can't abort: device is not operating in HP SSD Smart Path mode.\n");
517054b6e9e9SScott Teel 		return -1; /* not abortable */
517154b6e9e9SScott Teel 	}
517254b6e9e9SScott Teel 
517354b6e9e9SScott Teel 	/* Incoming scsi3addr is logical addr. We need physical disk addr. */
517454b6e9e9SScott Teel 	if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
517554b6e9e9SScott Teel 		dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
517654b6e9e9SScott Teel 		return -1; /* not abortable */
517754b6e9e9SScott Teel 	}
517854b6e9e9SScott Teel 
517954b6e9e9SScott Teel 	/* send the reset */
51802ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
51812ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
51822ba8bfc8SStephen M. Cameron 			"Reset as abort: Resetting physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
51832ba8bfc8SStephen M. Cameron 			psa[0], psa[1], psa[2], psa[3],
51842ba8bfc8SStephen M. Cameron 			psa[4], psa[5], psa[6], psa[7]);
518525163bd5SWebb Scales 	rc = hpsa_send_reset(h, psa, HPSA_RESET_TYPE_TARGET, reply_queue);
518654b6e9e9SScott Teel 	if (rc != 0) {
518754b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
518854b6e9e9SScott Teel 			"Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
518954b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
519054b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
519154b6e9e9SScott Teel 		return rc; /* failed to reset */
519254b6e9e9SScott Teel 	}
519354b6e9e9SScott Teel 
519454b6e9e9SScott Teel 	/* wait for device to recover */
5195b69324ffSWebb Scales 	if (wait_for_device_to_become_ready(h, psa, reply_queue) != 0) {
519654b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
519754b6e9e9SScott Teel 			"Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
519854b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
519954b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
520054b6e9e9SScott Teel 		return -1;  /* failed to recover */
520154b6e9e9SScott Teel 	}
520254b6e9e9SScott Teel 
520354b6e9e9SScott Teel 	/* device recovered */
520454b6e9e9SScott Teel 	dev_info(&h->pdev->dev,
520554b6e9e9SScott Teel 		"Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
520654b6e9e9SScott Teel 		psa[0], psa[1], psa[2], psa[3],
520754b6e9e9SScott Teel 		psa[4], psa[5], psa[6], psa[7]);
520854b6e9e9SScott Teel 
520954b6e9e9SScott Teel 	return rc; /* success */
521054b6e9e9SScott Teel }
521154b6e9e9SScott Teel 
52128be986ccSStephen Cameron static int hpsa_send_abort_ioaccel2(struct ctlr_info *h,
52138be986ccSStephen Cameron 	struct CommandList *abort, int reply_queue)
52148be986ccSStephen Cameron {
52158be986ccSStephen Cameron 	int rc = IO_OK;
52168be986ccSStephen Cameron 	struct CommandList *c;
52178be986ccSStephen Cameron 	__le32 taglower, tagupper;
52188be986ccSStephen Cameron 	struct hpsa_scsi_dev_t *dev;
52198be986ccSStephen Cameron 	struct io_accel2_cmd *c2;
52208be986ccSStephen Cameron 
52218be986ccSStephen Cameron 	dev = abort->scsi_cmd->device->hostdata;
52228be986ccSStephen Cameron 	if (!dev->offload_enabled && !dev->hba_ioaccel_enabled)
52238be986ccSStephen Cameron 		return -1;
52248be986ccSStephen Cameron 
52258be986ccSStephen Cameron 	c = cmd_alloc(h);
52268be986ccSStephen Cameron 	setup_ioaccel2_abort_cmd(c, h, abort, reply_queue);
52278be986ccSStephen Cameron 	c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
52288be986ccSStephen Cameron 	(void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
52298be986ccSStephen Cameron 	hpsa_get_tag(h, abort, &taglower, &tagupper);
52308be986ccSStephen Cameron 	dev_dbg(&h->pdev->dev,
52318be986ccSStephen Cameron 		"%s: Tag:0x%08x:%08x: do_simple_cmd(ioaccel2 abort) completed.\n",
52328be986ccSStephen Cameron 		__func__, tagupper, taglower);
52338be986ccSStephen Cameron 	/* no unmap needed here because no data xfer. */
52348be986ccSStephen Cameron 
52358be986ccSStephen Cameron 	dev_dbg(&h->pdev->dev,
52368be986ccSStephen Cameron 		"%s: Tag:0x%08x:%08x: abort service response = 0x%02x.\n",
52378be986ccSStephen Cameron 		__func__, tagupper, taglower, c2->error_data.serv_response);
52388be986ccSStephen Cameron 	switch (c2->error_data.serv_response) {
52398be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
52408be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
52418be986ccSStephen Cameron 		rc = 0;
52428be986ccSStephen Cameron 		break;
52438be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
52448be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_FAILURE:
52458be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
52468be986ccSStephen Cameron 		rc = -1;
52478be986ccSStephen Cameron 		break;
52488be986ccSStephen Cameron 	default:
52498be986ccSStephen Cameron 		dev_warn(&h->pdev->dev,
52508be986ccSStephen Cameron 			"%s: Tag:0x%08x:%08x: unknown abort service response 0x%02x\n",
52518be986ccSStephen Cameron 			__func__, tagupper, taglower,
52528be986ccSStephen Cameron 			c2->error_data.serv_response);
52538be986ccSStephen Cameron 		rc = -1;
52548be986ccSStephen Cameron 	}
52558be986ccSStephen Cameron 	cmd_free(h, c);
52568be986ccSStephen Cameron 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n", __func__,
52578be986ccSStephen Cameron 		tagupper, taglower);
52588be986ccSStephen Cameron 	return rc;
52598be986ccSStephen Cameron }
52608be986ccSStephen Cameron 
52616cba3f19SStephen M. Cameron static int hpsa_send_abort_both_ways(struct ctlr_info *h,
526225163bd5SWebb Scales 	unsigned char *scsi3addr, struct CommandList *abort, int reply_queue)
52636cba3f19SStephen M. Cameron {
52648be986ccSStephen Cameron 	/*
52658be986ccSStephen Cameron 	 * ioccelerator mode 2 commands should be aborted via the
526654b6e9e9SScott Teel 	 * accelerated path, since RAID path is unaware of these commands,
52678be986ccSStephen Cameron 	 * but not all underlying firmware can handle abort TMF.
52688be986ccSStephen Cameron 	 * Change abort to physical device reset when abort TMF is unsupported.
526954b6e9e9SScott Teel 	 */
52708be986ccSStephen Cameron 	if (abort->cmd_type == CMD_IOACCEL2) {
52718be986ccSStephen Cameron 		if (HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags)
52728be986ccSStephen Cameron 			return hpsa_send_abort_ioaccel2(h, abort,
52738be986ccSStephen Cameron 						reply_queue);
52748be986ccSStephen Cameron 		else
527525163bd5SWebb Scales 			return hpsa_send_reset_as_abort_ioaccel2(h, scsi3addr,
527625163bd5SWebb Scales 							abort, reply_queue);
52778be986ccSStephen Cameron 	}
52789b5c48c2SStephen Cameron 	return hpsa_send_abort(h, scsi3addr, abort, reply_queue);
527925163bd5SWebb Scales }
528025163bd5SWebb Scales 
528125163bd5SWebb Scales /* Find out which reply queue a command was meant to return on */
528225163bd5SWebb Scales static int hpsa_extract_reply_queue(struct ctlr_info *h,
528325163bd5SWebb Scales 					struct CommandList *c)
528425163bd5SWebb Scales {
528525163bd5SWebb Scales 	if (c->cmd_type == CMD_IOACCEL2)
528625163bd5SWebb Scales 		return h->ioaccel2_cmd_pool[c->cmdindex].reply_queue;
528725163bd5SWebb Scales 	return c->Header.ReplyQueue;
52886cba3f19SStephen M. Cameron }
52896cba3f19SStephen M. Cameron 
52909b5c48c2SStephen Cameron /*
52919b5c48c2SStephen Cameron  * Limit concurrency of abort commands to prevent
52929b5c48c2SStephen Cameron  * over-subscription of commands
52939b5c48c2SStephen Cameron  */
52949b5c48c2SStephen Cameron static inline int wait_for_available_abort_cmd(struct ctlr_info *h)
52959b5c48c2SStephen Cameron {
52969b5c48c2SStephen Cameron #define ABORT_CMD_WAIT_MSECS 5000
52979b5c48c2SStephen Cameron 	return !wait_event_timeout(h->abort_cmd_wait_queue,
52989b5c48c2SStephen Cameron 			atomic_dec_if_positive(&h->abort_cmds_available) >= 0,
52999b5c48c2SStephen Cameron 			msecs_to_jiffies(ABORT_CMD_WAIT_MSECS));
53009b5c48c2SStephen Cameron }
53019b5c48c2SStephen Cameron 
530275167d2cSStephen M. Cameron /* Send an abort for the specified command.
530375167d2cSStephen M. Cameron  *	If the device and controller support it,
530475167d2cSStephen M. Cameron  *		send a task abort request.
530575167d2cSStephen M. Cameron  */
530675167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
530775167d2cSStephen M. Cameron {
530875167d2cSStephen M. Cameron 
5309a58e7e53SWebb Scales 	int rc;
531075167d2cSStephen M. Cameron 	struct ctlr_info *h;
531175167d2cSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
531275167d2cSStephen M. Cameron 	struct CommandList *abort; /* pointer to command to be aborted */
531375167d2cSStephen M. Cameron 	struct scsi_cmnd *as;	/* ptr to scsi cmd inside aborted command. */
531475167d2cSStephen M. Cameron 	char msg[256];		/* For debug messaging. */
531575167d2cSStephen M. Cameron 	int ml = 0;
53162b08b3e9SDon Brace 	__le32 tagupper, taglower;
531725163bd5SWebb Scales 	int refcount, reply_queue;
531825163bd5SWebb Scales 
531925163bd5SWebb Scales 	if (sc == NULL)
532025163bd5SWebb Scales 		return FAILED;
532175167d2cSStephen M. Cameron 
53229b5c48c2SStephen Cameron 	if (sc->device == NULL)
53239b5c48c2SStephen Cameron 		return FAILED;
53249b5c48c2SStephen Cameron 
532575167d2cSStephen M. Cameron 	/* Find the controller of the command to be aborted */
532675167d2cSStephen M. Cameron 	h = sdev_to_hba(sc->device);
53279b5c48c2SStephen Cameron 	if (h == NULL)
532875167d2cSStephen M. Cameron 		return FAILED;
532975167d2cSStephen M. Cameron 
533025163bd5SWebb Scales 	/* Find the device of the command to be aborted */
533125163bd5SWebb Scales 	dev = sc->device->hostdata;
533225163bd5SWebb Scales 	if (!dev) {
533325163bd5SWebb Scales 		dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
533425163bd5SWebb Scales 				msg);
5335e345893bSDon Brace 		return FAILED;
533625163bd5SWebb Scales 	}
533725163bd5SWebb Scales 
533825163bd5SWebb Scales 	/* If controller locked up, we can guarantee command won't complete */
533925163bd5SWebb Scales 	if (lockup_detected(h)) {
534025163bd5SWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev,
534125163bd5SWebb Scales 					"ABORT FAILED, lockup detected");
534225163bd5SWebb Scales 		return FAILED;
534325163bd5SWebb Scales 	}
534425163bd5SWebb Scales 
534525163bd5SWebb Scales 	/* This is a good time to check if controller lockup has occurred */
534625163bd5SWebb Scales 	if (detect_controller_lockup(h)) {
534725163bd5SWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev,
534825163bd5SWebb Scales 					"ABORT FAILED, new lockup detected");
534925163bd5SWebb Scales 		return FAILED;
535025163bd5SWebb Scales 	}
5351e345893bSDon Brace 
535275167d2cSStephen M. Cameron 	/* Check that controller supports some kind of task abort */
535375167d2cSStephen M. Cameron 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
535475167d2cSStephen M. Cameron 		!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
535575167d2cSStephen M. Cameron 		return FAILED;
535675167d2cSStephen M. Cameron 
535775167d2cSStephen M. Cameron 	memset(msg, 0, sizeof(msg));
53584b761557SRobert Elliott 	ml += sprintf(msg+ml, "scsi %d:%d:%d:%llu %s %p",
535975167d2cSStephen M. Cameron 		h->scsi_host->host_no, sc->device->channel,
53600d96ef5fSWebb Scales 		sc->device->id, sc->device->lun,
53614b761557SRobert Elliott 		"Aborting command", sc);
536275167d2cSStephen M. Cameron 
536375167d2cSStephen M. Cameron 	/* Get SCSI command to be aborted */
536475167d2cSStephen M. Cameron 	abort = (struct CommandList *) sc->host_scribble;
536575167d2cSStephen M. Cameron 	if (abort == NULL) {
5366281a7fd0SWebb Scales 		/* This can happen if the command already completed. */
5367281a7fd0SWebb Scales 		return SUCCESS;
5368281a7fd0SWebb Scales 	}
5369281a7fd0SWebb Scales 	refcount = atomic_inc_return(&abort->refcount);
5370281a7fd0SWebb Scales 	if (refcount == 1) { /* Command is done already. */
5371281a7fd0SWebb Scales 		cmd_free(h, abort);
5372281a7fd0SWebb Scales 		return SUCCESS;
537375167d2cSStephen M. Cameron 	}
53749b5c48c2SStephen Cameron 
53759b5c48c2SStephen Cameron 	/* Don't bother trying the abort if we know it won't work. */
53769b5c48c2SStephen Cameron 	if (abort->cmd_type != CMD_IOACCEL2 &&
53779b5c48c2SStephen Cameron 		abort->cmd_type != CMD_IOACCEL1 && !dev->supports_aborts) {
53789b5c48c2SStephen Cameron 		cmd_free(h, abort);
53799b5c48c2SStephen Cameron 		return FAILED;
53809b5c48c2SStephen Cameron 	}
53819b5c48c2SStephen Cameron 
5382a58e7e53SWebb Scales 	/*
5383a58e7e53SWebb Scales 	 * Check that we're aborting the right command.
5384a58e7e53SWebb Scales 	 * It's possible the CommandList already completed and got re-used.
5385a58e7e53SWebb Scales 	 */
5386a58e7e53SWebb Scales 	if (abort->scsi_cmd != sc) {
5387a58e7e53SWebb Scales 		cmd_free(h, abort);
5388a58e7e53SWebb Scales 		return SUCCESS;
5389a58e7e53SWebb Scales 	}
5390a58e7e53SWebb Scales 
5391a58e7e53SWebb Scales 	abort->abort_pending = true;
539217eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
539325163bd5SWebb Scales 	reply_queue = hpsa_extract_reply_queue(h, abort);
539417eb87d2SScott Teel 	ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
53957fa3030cSStephen Cameron 	as  = abort->scsi_cmd;
539675167d2cSStephen M. Cameron 	if (as != NULL)
53974b761557SRobert Elliott 		ml += sprintf(msg+ml,
53984b761557SRobert Elliott 			"CDBLen: %d CDB: 0x%02x%02x... SN: 0x%lx ",
53994b761557SRobert Elliott 			as->cmd_len, as->cmnd[0], as->cmnd[1],
54004b761557SRobert Elliott 			as->serial_number);
54014b761557SRobert Elliott 	dev_warn(&h->pdev->dev, "%s BEING SENT\n", msg);
54020d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_WARNING, h, dev, "Aborting command");
54034b761557SRobert Elliott 
540475167d2cSStephen M. Cameron 	/*
540575167d2cSStephen M. Cameron 	 * Command is in flight, or possibly already completed
540675167d2cSStephen M. Cameron 	 * by the firmware (but not to the scsi mid layer) but we can't
540775167d2cSStephen M. Cameron 	 * distinguish which.  Send the abort down.
540875167d2cSStephen M. Cameron 	 */
54099b5c48c2SStephen Cameron 	if (wait_for_available_abort_cmd(h)) {
54109b5c48c2SStephen Cameron 		dev_warn(&h->pdev->dev,
54114b761557SRobert Elliott 			"%s FAILED, timeout waiting for an abort command to become available.\n",
54124b761557SRobert Elliott 			msg);
54139b5c48c2SStephen Cameron 		cmd_free(h, abort);
54149b5c48c2SStephen Cameron 		return FAILED;
54159b5c48c2SStephen Cameron 	}
541625163bd5SWebb Scales 	rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort, reply_queue);
54179b5c48c2SStephen Cameron 	atomic_inc(&h->abort_cmds_available);
54189b5c48c2SStephen Cameron 	wake_up_all(&h->abort_cmd_wait_queue);
541975167d2cSStephen M. Cameron 	if (rc != 0) {
54204b761557SRobert Elliott 		dev_warn(&h->pdev->dev, "%s SENT, FAILED\n", msg);
54210d96ef5fSWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev,
54220d96ef5fSWebb Scales 				"FAILED to abort command");
5423281a7fd0SWebb Scales 		cmd_free(h, abort);
542475167d2cSStephen M. Cameron 		return FAILED;
542575167d2cSStephen M. Cameron 	}
54264b761557SRobert Elliott 	dev_info(&h->pdev->dev, "%s SENT, SUCCESS\n", msg);
5427a58e7e53SWebb Scales 	wait_event(h->abort_sync_wait_queue,
5428a58e7e53SWebb Scales 		   abort->scsi_cmd != sc || lockup_detected(h));
5429281a7fd0SWebb Scales 	cmd_free(h, abort);
5430a58e7e53SWebb Scales 	return !lockup_detected(h) ? SUCCESS : FAILED;
543175167d2cSStephen M. Cameron }
543275167d2cSStephen M. Cameron 
5433edd16368SStephen M. Cameron /*
5434*73153fe5SWebb Scales  * For operations with an associated SCSI command, a command block is allocated
5435*73153fe5SWebb Scales  * at init, and managed by cmd_tagged_alloc() and cmd_tagged_free() using the
5436*73153fe5SWebb Scales  * block request tag as an index into a table of entries.  cmd_tagged_free() is
5437*73153fe5SWebb Scales  * the complement, although cmd_free() may be called instead.
5438*73153fe5SWebb Scales  */
5439*73153fe5SWebb Scales static struct CommandList *cmd_tagged_alloc(struct ctlr_info *h,
5440*73153fe5SWebb Scales 					    struct scsi_cmnd *scmd)
5441*73153fe5SWebb Scales {
5442*73153fe5SWebb Scales 	int idx = hpsa_get_cmd_index(scmd);
5443*73153fe5SWebb Scales 	struct CommandList *c = h->cmd_pool + idx;
5444*73153fe5SWebb Scales 
5445*73153fe5SWebb Scales 	if (idx < HPSA_NRESERVED_CMDS || idx >= h->nr_cmds) {
5446*73153fe5SWebb Scales 		dev_err(&h->pdev->dev, "Bad block tag: %d not in [%d..%d]\n",
5447*73153fe5SWebb Scales 			idx, HPSA_NRESERVED_CMDS, h->nr_cmds - 1);
5448*73153fe5SWebb Scales 		/* The index value comes from the block layer, so if it's out of
5449*73153fe5SWebb Scales 		 * bounds, it's probably not our bug.
5450*73153fe5SWebb Scales 		 */
5451*73153fe5SWebb Scales 		BUG();
5452*73153fe5SWebb Scales 	}
5453*73153fe5SWebb Scales 
5454*73153fe5SWebb Scales 	atomic_inc(&c->refcount);
5455*73153fe5SWebb Scales 	if (unlikely(!hpsa_is_cmd_idle(c))) {
5456*73153fe5SWebb Scales 		/*
5457*73153fe5SWebb Scales 		 * We expect that the SCSI layer will hand us a unique tag
5458*73153fe5SWebb Scales 		 * value.  Thus, there should never be a collision here between
5459*73153fe5SWebb Scales 		 * two requests...because if the selected command isn't idle
5460*73153fe5SWebb Scales 		 * then someone is going to be very disappointed.
5461*73153fe5SWebb Scales 		 */
5462*73153fe5SWebb Scales 		dev_err(&h->pdev->dev,
5463*73153fe5SWebb Scales 			"tag collision (tag=%d) in cmd_tagged_alloc().\n",
5464*73153fe5SWebb Scales 			idx);
5465*73153fe5SWebb Scales 		if (c->scsi_cmd != NULL)
5466*73153fe5SWebb Scales 			scsi_print_command(c->scsi_cmd);
5467*73153fe5SWebb Scales 		scsi_print_command(scmd);
5468*73153fe5SWebb Scales 	}
5469*73153fe5SWebb Scales 
5470*73153fe5SWebb Scales 	hpsa_cmd_partial_init(h, idx, c);
5471*73153fe5SWebb Scales 	return c;
5472*73153fe5SWebb Scales }
5473*73153fe5SWebb Scales 
5474*73153fe5SWebb Scales static void cmd_tagged_free(struct ctlr_info *h, struct CommandList *c)
5475*73153fe5SWebb Scales {
5476*73153fe5SWebb Scales 	/*
5477*73153fe5SWebb Scales 	 * Release our reference to the block.  We don't need to do anything
5478*73153fe5SWebb Scales 	 * else to free it, because it is accessed by index.  (There's no point
5479*73153fe5SWebb Scales 	 * in checking the result of the decrement, since we cannot guarantee
5480*73153fe5SWebb Scales 	 * that there isn't a concurrent abort which is also accessing it.)
5481*73153fe5SWebb Scales 	 */
5482*73153fe5SWebb Scales 	(void)atomic_dec(&c->refcount);
5483*73153fe5SWebb Scales }
5484*73153fe5SWebb Scales 
5485*73153fe5SWebb Scales /*
5486edd16368SStephen M. Cameron  * For operations that cannot sleep, a command block is allocated at init,
5487edd16368SStephen M. Cameron  * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
5488edd16368SStephen M. Cameron  * which ones are free or in use.  Lock must be held when calling this.
5489edd16368SStephen M. Cameron  * cmd_free() is the complement.
5490bf43caf3SRobert Elliott  * This function never gives up and returns NULL.  If it hangs,
5491bf43caf3SRobert Elliott  * another thread must call cmd_free() to free some tags.
5492edd16368SStephen M. Cameron  */
5493281a7fd0SWebb Scales 
5494edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h)
5495edd16368SStephen M. Cameron {
5496edd16368SStephen M. Cameron 	struct CommandList *c;
5497360c73bdSStephen Cameron 	int refcount, i;
5498*73153fe5SWebb Scales 	int offset = 0;
5499edd16368SStephen M. Cameron 
550033811026SRobert Elliott 	/*
550133811026SRobert Elliott 	 * There is some *extremely* small but non-zero chance that that
55024c413128SStephen M. Cameron 	 * multiple threads could get in here, and one thread could
55034c413128SStephen M. Cameron 	 * be scanning through the list of bits looking for a free
55044c413128SStephen M. Cameron 	 * one, but the free ones are always behind him, and other
55054c413128SStephen M. Cameron 	 * threads sneak in behind him and eat them before he can
55064c413128SStephen M. Cameron 	 * get to them, so that while there is always a free one, a
55074c413128SStephen M. Cameron 	 * very unlucky thread might be starved anyway, never able to
55084c413128SStephen M. Cameron 	 * beat the other threads.  In reality, this happens so
55094c413128SStephen M. Cameron 	 * infrequently as to be indistinguishable from never.
5510*73153fe5SWebb Scales 	 *
5511*73153fe5SWebb Scales 	 * Note that we start allocating commands before the SCSI host structure
5512*73153fe5SWebb Scales 	 * is initialized.  Since the search starts at bit zero, this
5513*73153fe5SWebb Scales 	 * all works, since we have at least one command structure available;
5514*73153fe5SWebb Scales 	 * however, it means that the structures with the low indexes have to be
5515*73153fe5SWebb Scales 	 * reserved for driver-initiated requests, while requests from the block
5516*73153fe5SWebb Scales 	 * layer will use the higher indexes.
55174c413128SStephen M. Cameron 	 */
55184c413128SStephen M. Cameron 
5519281a7fd0SWebb Scales 	for (;;) {
5520*73153fe5SWebb Scales 		i = find_next_zero_bit(h->cmd_pool_bits,
5521*73153fe5SWebb Scales 					HPSA_NRESERVED_CMDS,
5522*73153fe5SWebb Scales 					offset);
5523*73153fe5SWebb Scales 		if (unlikely(i >= HPSA_NRESERVED_CMDS)) {
5524281a7fd0SWebb Scales 			offset = 0;
5525281a7fd0SWebb Scales 			continue;
5526281a7fd0SWebb Scales 		}
5527edd16368SStephen M. Cameron 		c = h->cmd_pool + i;
5528281a7fd0SWebb Scales 		refcount = atomic_inc_return(&c->refcount);
5529281a7fd0SWebb Scales 		if (unlikely(refcount > 1)) {
5530281a7fd0SWebb Scales 			cmd_free(h, c); /* already in use */
5531*73153fe5SWebb Scales 			offset = (i + 1) % HPSA_NRESERVED_CMDS;
5532281a7fd0SWebb Scales 			continue;
5533281a7fd0SWebb Scales 		}
5534281a7fd0SWebb Scales 		set_bit(i & (BITS_PER_LONG - 1),
5535281a7fd0SWebb Scales 			h->cmd_pool_bits + (i / BITS_PER_LONG));
5536281a7fd0SWebb Scales 		break; /* it's ours now. */
5537281a7fd0SWebb Scales 	}
5538360c73bdSStephen Cameron 	hpsa_cmd_partial_init(h, i, c);
5539edd16368SStephen M. Cameron 	return c;
5540edd16368SStephen M. Cameron }
5541edd16368SStephen M. Cameron 
5542*73153fe5SWebb Scales /*
5543*73153fe5SWebb Scales  * This is the complementary operation to cmd_alloc().  Note, however, in some
5544*73153fe5SWebb Scales  * corner cases it may also be used to free blocks allocated by
5545*73153fe5SWebb Scales  * cmd_tagged_alloc() in which case the ref-count decrement does the trick and
5546*73153fe5SWebb Scales  * the clear-bit is harmless.
5547*73153fe5SWebb Scales  */
5548edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c)
5549edd16368SStephen M. Cameron {
5550281a7fd0SWebb Scales 	if (atomic_dec_and_test(&c->refcount)) {
5551edd16368SStephen M. Cameron 		int i;
5552edd16368SStephen M. Cameron 
5553edd16368SStephen M. Cameron 		i = c - h->cmd_pool;
5554edd16368SStephen M. Cameron 		clear_bit(i & (BITS_PER_LONG - 1),
5555edd16368SStephen M. Cameron 			  h->cmd_pool_bits + (i / BITS_PER_LONG));
5556edd16368SStephen M. Cameron 	}
5557281a7fd0SWebb Scales }
5558edd16368SStephen M. Cameron 
5559edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
5560edd16368SStephen M. Cameron 
556142a91641SDon Brace static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd,
556242a91641SDon Brace 	void __user *arg)
5563edd16368SStephen M. Cameron {
5564edd16368SStephen M. Cameron 	IOCTL32_Command_struct __user *arg32 =
5565edd16368SStephen M. Cameron 	    (IOCTL32_Command_struct __user *) arg;
5566edd16368SStephen M. Cameron 	IOCTL_Command_struct arg64;
5567edd16368SStephen M. Cameron 	IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
5568edd16368SStephen M. Cameron 	int err;
5569edd16368SStephen M. Cameron 	u32 cp;
5570edd16368SStephen M. Cameron 
5571938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
5572edd16368SStephen M. Cameron 	err = 0;
5573edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
5574edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
5575edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
5576edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
5577edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
5578edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
5579edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
5580edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
5581edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
5582edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
5583edd16368SStephen M. Cameron 
5584edd16368SStephen M. Cameron 	if (err)
5585edd16368SStephen M. Cameron 		return -EFAULT;
5586edd16368SStephen M. Cameron 
558742a91641SDon Brace 	err = hpsa_ioctl(dev, CCISS_PASSTHRU, p);
5588edd16368SStephen M. Cameron 	if (err)
5589edd16368SStephen M. Cameron 		return err;
5590edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
5591edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
5592edd16368SStephen M. Cameron 	if (err)
5593edd16368SStephen M. Cameron 		return -EFAULT;
5594edd16368SStephen M. Cameron 	return err;
5595edd16368SStephen M. Cameron }
5596edd16368SStephen M. Cameron 
5597edd16368SStephen M. Cameron static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
559842a91641SDon Brace 	int cmd, void __user *arg)
5599edd16368SStephen M. Cameron {
5600edd16368SStephen M. Cameron 	BIG_IOCTL32_Command_struct __user *arg32 =
5601edd16368SStephen M. Cameron 	    (BIG_IOCTL32_Command_struct __user *) arg;
5602edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct arg64;
5603edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct __user *p =
5604edd16368SStephen M. Cameron 	    compat_alloc_user_space(sizeof(arg64));
5605edd16368SStephen M. Cameron 	int err;
5606edd16368SStephen M. Cameron 	u32 cp;
5607edd16368SStephen M. Cameron 
5608938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
5609edd16368SStephen M. Cameron 	err = 0;
5610edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
5611edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
5612edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
5613edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
5614edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
5615edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
5616edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
5617edd16368SStephen M. Cameron 	err |= get_user(arg64.malloc_size, &arg32->malloc_size);
5618edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
5619edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
5620edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
5621edd16368SStephen M. Cameron 
5622edd16368SStephen M. Cameron 	if (err)
5623edd16368SStephen M. Cameron 		return -EFAULT;
5624edd16368SStephen M. Cameron 
562542a91641SDon Brace 	err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, p);
5626edd16368SStephen M. Cameron 	if (err)
5627edd16368SStephen M. Cameron 		return err;
5628edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
5629edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
5630edd16368SStephen M. Cameron 	if (err)
5631edd16368SStephen M. Cameron 		return -EFAULT;
5632edd16368SStephen M. Cameron 	return err;
5633edd16368SStephen M. Cameron }
563471fe75a7SStephen M. Cameron 
563542a91641SDon Brace static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
563671fe75a7SStephen M. Cameron {
563771fe75a7SStephen M. Cameron 	switch (cmd) {
563871fe75a7SStephen M. Cameron 	case CCISS_GETPCIINFO:
563971fe75a7SStephen M. Cameron 	case CCISS_GETINTINFO:
564071fe75a7SStephen M. Cameron 	case CCISS_SETINTINFO:
564171fe75a7SStephen M. Cameron 	case CCISS_GETNODENAME:
564271fe75a7SStephen M. Cameron 	case CCISS_SETNODENAME:
564371fe75a7SStephen M. Cameron 	case CCISS_GETHEARTBEAT:
564471fe75a7SStephen M. Cameron 	case CCISS_GETBUSTYPES:
564571fe75a7SStephen M. Cameron 	case CCISS_GETFIRMVER:
564671fe75a7SStephen M. Cameron 	case CCISS_GETDRIVVER:
564771fe75a7SStephen M. Cameron 	case CCISS_REVALIDVOLS:
564871fe75a7SStephen M. Cameron 	case CCISS_DEREGDISK:
564971fe75a7SStephen M. Cameron 	case CCISS_REGNEWDISK:
565071fe75a7SStephen M. Cameron 	case CCISS_REGNEWD:
565171fe75a7SStephen M. Cameron 	case CCISS_RESCANDISK:
565271fe75a7SStephen M. Cameron 	case CCISS_GETLUNINFO:
565371fe75a7SStephen M. Cameron 		return hpsa_ioctl(dev, cmd, arg);
565471fe75a7SStephen M. Cameron 
565571fe75a7SStephen M. Cameron 	case CCISS_PASSTHRU32:
565671fe75a7SStephen M. Cameron 		return hpsa_ioctl32_passthru(dev, cmd, arg);
565771fe75a7SStephen M. Cameron 	case CCISS_BIG_PASSTHRU32:
565871fe75a7SStephen M. Cameron 		return hpsa_ioctl32_big_passthru(dev, cmd, arg);
565971fe75a7SStephen M. Cameron 
566071fe75a7SStephen M. Cameron 	default:
566171fe75a7SStephen M. Cameron 		return -ENOIOCTLCMD;
566271fe75a7SStephen M. Cameron 	}
566371fe75a7SStephen M. Cameron }
5664edd16368SStephen M. Cameron #endif
5665edd16368SStephen M. Cameron 
5666edd16368SStephen M. Cameron static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
5667edd16368SStephen M. Cameron {
5668edd16368SStephen M. Cameron 	struct hpsa_pci_info pciinfo;
5669edd16368SStephen M. Cameron 
5670edd16368SStephen M. Cameron 	if (!argp)
5671edd16368SStephen M. Cameron 		return -EINVAL;
5672edd16368SStephen M. Cameron 	pciinfo.domain = pci_domain_nr(h->pdev->bus);
5673edd16368SStephen M. Cameron 	pciinfo.bus = h->pdev->bus->number;
5674edd16368SStephen M. Cameron 	pciinfo.dev_fn = h->pdev->devfn;
5675edd16368SStephen M. Cameron 	pciinfo.board_id = h->board_id;
5676edd16368SStephen M. Cameron 	if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
5677edd16368SStephen M. Cameron 		return -EFAULT;
5678edd16368SStephen M. Cameron 	return 0;
5679edd16368SStephen M. Cameron }
5680edd16368SStephen M. Cameron 
5681edd16368SStephen M. Cameron static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
5682edd16368SStephen M. Cameron {
5683edd16368SStephen M. Cameron 	DriverVer_type DriverVer;
5684edd16368SStephen M. Cameron 	unsigned char vmaj, vmin, vsubmin;
5685edd16368SStephen M. Cameron 	int rc;
5686edd16368SStephen M. Cameron 
5687edd16368SStephen M. Cameron 	rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
5688edd16368SStephen M. Cameron 		&vmaj, &vmin, &vsubmin);
5689edd16368SStephen M. Cameron 	if (rc != 3) {
5690edd16368SStephen M. Cameron 		dev_info(&h->pdev->dev, "driver version string '%s' "
5691edd16368SStephen M. Cameron 			"unrecognized.", HPSA_DRIVER_VERSION);
5692edd16368SStephen M. Cameron 		vmaj = 0;
5693edd16368SStephen M. Cameron 		vmin = 0;
5694edd16368SStephen M. Cameron 		vsubmin = 0;
5695edd16368SStephen M. Cameron 	}
5696edd16368SStephen M. Cameron 	DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
5697edd16368SStephen M. Cameron 	if (!argp)
5698edd16368SStephen M. Cameron 		return -EINVAL;
5699edd16368SStephen M. Cameron 	if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
5700edd16368SStephen M. Cameron 		return -EFAULT;
5701edd16368SStephen M. Cameron 	return 0;
5702edd16368SStephen M. Cameron }
5703edd16368SStephen M. Cameron 
5704edd16368SStephen M. Cameron static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
5705edd16368SStephen M. Cameron {
5706edd16368SStephen M. Cameron 	IOCTL_Command_struct iocommand;
5707edd16368SStephen M. Cameron 	struct CommandList *c;
5708edd16368SStephen M. Cameron 	char *buff = NULL;
570950a0decfSStephen M. Cameron 	u64 temp64;
5710c1f63c8fSStephen M. Cameron 	int rc = 0;
5711edd16368SStephen M. Cameron 
5712edd16368SStephen M. Cameron 	if (!argp)
5713edd16368SStephen M. Cameron 		return -EINVAL;
5714edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
5715edd16368SStephen M. Cameron 		return -EPERM;
5716edd16368SStephen M. Cameron 	if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
5717edd16368SStephen M. Cameron 		return -EFAULT;
5718edd16368SStephen M. Cameron 	if ((iocommand.buf_size < 1) &&
5719edd16368SStephen M. Cameron 	    (iocommand.Request.Type.Direction != XFER_NONE)) {
5720edd16368SStephen M. Cameron 		return -EINVAL;
5721edd16368SStephen M. Cameron 	}
5722edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
5723edd16368SStephen M. Cameron 		buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
5724edd16368SStephen M. Cameron 		if (buff == NULL)
57252dd02d74SRobert Elliott 			return -ENOMEM;
57269233fb10SStephen M. Cameron 		if (iocommand.Request.Type.Direction & XFER_WRITE) {
5727edd16368SStephen M. Cameron 			/* Copy the data into the buffer we created */
5728b03a7771SStephen M. Cameron 			if (copy_from_user(buff, iocommand.buf,
5729b03a7771SStephen M. Cameron 				iocommand.buf_size)) {
5730c1f63c8fSStephen M. Cameron 				rc = -EFAULT;
5731c1f63c8fSStephen M. Cameron 				goto out_kfree;
5732edd16368SStephen M. Cameron 			}
5733b03a7771SStephen M. Cameron 		} else {
5734edd16368SStephen M. Cameron 			memset(buff, 0, iocommand.buf_size);
5735b03a7771SStephen M. Cameron 		}
5736b03a7771SStephen M. Cameron 	}
573745fcb86eSStephen Cameron 	c = cmd_alloc(h);
5738bf43caf3SRobert Elliott 
5739edd16368SStephen M. Cameron 	/* Fill in the command type */
5740edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5741a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_BUSY;
5742edd16368SStephen M. Cameron 	/* Fill in Command Header */
5743edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0; /* unused in simple mode */
5744edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {	/* buffer to fill */
5745edd16368SStephen M. Cameron 		c->Header.SGList = 1;
574650a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(1);
5747edd16368SStephen M. Cameron 	} else	{ /* no buffers to fill */
5748edd16368SStephen M. Cameron 		c->Header.SGList = 0;
574950a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(0);
5750edd16368SStephen M. Cameron 	}
5751edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
5752edd16368SStephen M. Cameron 
5753edd16368SStephen M. Cameron 	/* Fill in Request block */
5754edd16368SStephen M. Cameron 	memcpy(&c->Request, &iocommand.Request,
5755edd16368SStephen M. Cameron 		sizeof(c->Request));
5756edd16368SStephen M. Cameron 
5757edd16368SStephen M. Cameron 	/* Fill in the scatter gather information */
5758edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
575950a0decfSStephen M. Cameron 		temp64 = pci_map_single(h->pdev, buff,
5760edd16368SStephen M. Cameron 			iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
576150a0decfSStephen M. Cameron 		if (dma_mapping_error(&h->pdev->dev, (dma_addr_t) temp64)) {
576250a0decfSStephen M. Cameron 			c->SG[0].Addr = cpu_to_le64(0);
576350a0decfSStephen M. Cameron 			c->SG[0].Len = cpu_to_le32(0);
5764bcc48ffaSStephen M. Cameron 			rc = -ENOMEM;
5765bcc48ffaSStephen M. Cameron 			goto out;
5766bcc48ffaSStephen M. Cameron 		}
576750a0decfSStephen M. Cameron 		c->SG[0].Addr = cpu_to_le64(temp64);
576850a0decfSStephen M. Cameron 		c->SG[0].Len = cpu_to_le32(iocommand.buf_size);
576950a0decfSStephen M. Cameron 		c->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* not chaining */
5770edd16368SStephen M. Cameron 	}
577125163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
5772c2dd32e0SStephen M. Cameron 	if (iocommand.buf_size > 0)
5773edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
5774edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
577525163bd5SWebb Scales 	if (rc) {
577625163bd5SWebb Scales 		rc = -EIO;
577725163bd5SWebb Scales 		goto out;
577825163bd5SWebb Scales 	}
5779edd16368SStephen M. Cameron 
5780edd16368SStephen M. Cameron 	/* Copy the error information out */
5781edd16368SStephen M. Cameron 	memcpy(&iocommand.error_info, c->err_info,
5782edd16368SStephen M. Cameron 		sizeof(iocommand.error_info));
5783edd16368SStephen M. Cameron 	if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
5784c1f63c8fSStephen M. Cameron 		rc = -EFAULT;
5785c1f63c8fSStephen M. Cameron 		goto out;
5786edd16368SStephen M. Cameron 	}
57879233fb10SStephen M. Cameron 	if ((iocommand.Request.Type.Direction & XFER_READ) &&
5788b03a7771SStephen M. Cameron 		iocommand.buf_size > 0) {
5789edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
5790edd16368SStephen M. Cameron 		if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
5791c1f63c8fSStephen M. Cameron 			rc = -EFAULT;
5792c1f63c8fSStephen M. Cameron 			goto out;
5793edd16368SStephen M. Cameron 		}
5794edd16368SStephen M. Cameron 	}
5795c1f63c8fSStephen M. Cameron out:
579645fcb86eSStephen Cameron 	cmd_free(h, c);
5797c1f63c8fSStephen M. Cameron out_kfree:
5798c1f63c8fSStephen M. Cameron 	kfree(buff);
5799c1f63c8fSStephen M. Cameron 	return rc;
5800edd16368SStephen M. Cameron }
5801edd16368SStephen M. Cameron 
5802edd16368SStephen M. Cameron static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
5803edd16368SStephen M. Cameron {
5804edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct *ioc;
5805edd16368SStephen M. Cameron 	struct CommandList *c;
5806edd16368SStephen M. Cameron 	unsigned char **buff = NULL;
5807edd16368SStephen M. Cameron 	int *buff_size = NULL;
580850a0decfSStephen M. Cameron 	u64 temp64;
5809edd16368SStephen M. Cameron 	BYTE sg_used = 0;
5810edd16368SStephen M. Cameron 	int status = 0;
581101a02ffcSStephen M. Cameron 	u32 left;
581201a02ffcSStephen M. Cameron 	u32 sz;
5813edd16368SStephen M. Cameron 	BYTE __user *data_ptr;
5814edd16368SStephen M. Cameron 
5815edd16368SStephen M. Cameron 	if (!argp)
5816edd16368SStephen M. Cameron 		return -EINVAL;
5817edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
5818edd16368SStephen M. Cameron 		return -EPERM;
5819edd16368SStephen M. Cameron 	ioc = (BIG_IOCTL_Command_struct *)
5820edd16368SStephen M. Cameron 	    kmalloc(sizeof(*ioc), GFP_KERNEL);
5821edd16368SStephen M. Cameron 	if (!ioc) {
5822edd16368SStephen M. Cameron 		status = -ENOMEM;
5823edd16368SStephen M. Cameron 		goto cleanup1;
5824edd16368SStephen M. Cameron 	}
5825edd16368SStephen M. Cameron 	if (copy_from_user(ioc, argp, sizeof(*ioc))) {
5826edd16368SStephen M. Cameron 		status = -EFAULT;
5827edd16368SStephen M. Cameron 		goto cleanup1;
5828edd16368SStephen M. Cameron 	}
5829edd16368SStephen M. Cameron 	if ((ioc->buf_size < 1) &&
5830edd16368SStephen M. Cameron 	    (ioc->Request.Type.Direction != XFER_NONE)) {
5831edd16368SStephen M. Cameron 		status = -EINVAL;
5832edd16368SStephen M. Cameron 		goto cleanup1;
5833edd16368SStephen M. Cameron 	}
5834edd16368SStephen M. Cameron 	/* Check kmalloc limits  using all SGs */
5835edd16368SStephen M. Cameron 	if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
5836edd16368SStephen M. Cameron 		status = -EINVAL;
5837edd16368SStephen M. Cameron 		goto cleanup1;
5838edd16368SStephen M. Cameron 	}
5839d66ae08bSStephen M. Cameron 	if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
5840edd16368SStephen M. Cameron 		status = -EINVAL;
5841edd16368SStephen M. Cameron 		goto cleanup1;
5842edd16368SStephen M. Cameron 	}
5843d66ae08bSStephen M. Cameron 	buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
5844edd16368SStephen M. Cameron 	if (!buff) {
5845edd16368SStephen M. Cameron 		status = -ENOMEM;
5846edd16368SStephen M. Cameron 		goto cleanup1;
5847edd16368SStephen M. Cameron 	}
5848d66ae08bSStephen M. Cameron 	buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
5849edd16368SStephen M. Cameron 	if (!buff_size) {
5850edd16368SStephen M. Cameron 		status = -ENOMEM;
5851edd16368SStephen M. Cameron 		goto cleanup1;
5852edd16368SStephen M. Cameron 	}
5853edd16368SStephen M. Cameron 	left = ioc->buf_size;
5854edd16368SStephen M. Cameron 	data_ptr = ioc->buf;
5855edd16368SStephen M. Cameron 	while (left) {
5856edd16368SStephen M. Cameron 		sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
5857edd16368SStephen M. Cameron 		buff_size[sg_used] = sz;
5858edd16368SStephen M. Cameron 		buff[sg_used] = kmalloc(sz, GFP_KERNEL);
5859edd16368SStephen M. Cameron 		if (buff[sg_used] == NULL) {
5860edd16368SStephen M. Cameron 			status = -ENOMEM;
5861edd16368SStephen M. Cameron 			goto cleanup1;
5862edd16368SStephen M. Cameron 		}
58639233fb10SStephen M. Cameron 		if (ioc->Request.Type.Direction & XFER_WRITE) {
5864edd16368SStephen M. Cameron 			if (copy_from_user(buff[sg_used], data_ptr, sz)) {
58650758f4f7SStephen M. Cameron 				status = -EFAULT;
5866edd16368SStephen M. Cameron 				goto cleanup1;
5867edd16368SStephen M. Cameron 			}
5868edd16368SStephen M. Cameron 		} else
5869edd16368SStephen M. Cameron 			memset(buff[sg_used], 0, sz);
5870edd16368SStephen M. Cameron 		left -= sz;
5871edd16368SStephen M. Cameron 		data_ptr += sz;
5872edd16368SStephen M. Cameron 		sg_used++;
5873edd16368SStephen M. Cameron 	}
587445fcb86eSStephen Cameron 	c = cmd_alloc(h);
5875bf43caf3SRobert Elliott 
5876edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5877a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_BUSY;
5878edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
587950a0decfSStephen M. Cameron 	c->Header.SGList = (u8) sg_used;
588050a0decfSStephen M. Cameron 	c->Header.SGTotal = cpu_to_le16(sg_used);
5881edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
5882edd16368SStephen M. Cameron 	memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
5883edd16368SStephen M. Cameron 	if (ioc->buf_size > 0) {
5884edd16368SStephen M. Cameron 		int i;
5885edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
588650a0decfSStephen M. Cameron 			temp64 = pci_map_single(h->pdev, buff[i],
5887edd16368SStephen M. Cameron 				    buff_size[i], PCI_DMA_BIDIRECTIONAL);
588850a0decfSStephen M. Cameron 			if (dma_mapping_error(&h->pdev->dev,
588950a0decfSStephen M. Cameron 							(dma_addr_t) temp64)) {
589050a0decfSStephen M. Cameron 				c->SG[i].Addr = cpu_to_le64(0);
589150a0decfSStephen M. Cameron 				c->SG[i].Len = cpu_to_le32(0);
5892bcc48ffaSStephen M. Cameron 				hpsa_pci_unmap(h->pdev, c, i,
5893bcc48ffaSStephen M. Cameron 					PCI_DMA_BIDIRECTIONAL);
5894bcc48ffaSStephen M. Cameron 				status = -ENOMEM;
5895e2d4a1f6SStephen M. Cameron 				goto cleanup0;
5896bcc48ffaSStephen M. Cameron 			}
589750a0decfSStephen M. Cameron 			c->SG[i].Addr = cpu_to_le64(temp64);
589850a0decfSStephen M. Cameron 			c->SG[i].Len = cpu_to_le32(buff_size[i]);
589950a0decfSStephen M. Cameron 			c->SG[i].Ext = cpu_to_le32(0);
5900edd16368SStephen M. Cameron 		}
590150a0decfSStephen M. Cameron 		c->SG[--i].Ext = cpu_to_le32(HPSA_SG_LAST);
5902edd16368SStephen M. Cameron 	}
590325163bd5SWebb Scales 	status = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
5904b03a7771SStephen M. Cameron 	if (sg_used)
5905edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
5906edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
590725163bd5SWebb Scales 	if (status) {
590825163bd5SWebb Scales 		status = -EIO;
590925163bd5SWebb Scales 		goto cleanup0;
591025163bd5SWebb Scales 	}
591125163bd5SWebb Scales 
5912edd16368SStephen M. Cameron 	/* Copy the error information out */
5913edd16368SStephen M. Cameron 	memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
5914edd16368SStephen M. Cameron 	if (copy_to_user(argp, ioc, sizeof(*ioc))) {
5915edd16368SStephen M. Cameron 		status = -EFAULT;
5916e2d4a1f6SStephen M. Cameron 		goto cleanup0;
5917edd16368SStephen M. Cameron 	}
59189233fb10SStephen M. Cameron 	if ((ioc->Request.Type.Direction & XFER_READ) && ioc->buf_size > 0) {
59192b08b3e9SDon Brace 		int i;
59202b08b3e9SDon Brace 
5921edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
5922edd16368SStephen M. Cameron 		BYTE __user *ptr = ioc->buf;
5923edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
5924edd16368SStephen M. Cameron 			if (copy_to_user(ptr, buff[i], buff_size[i])) {
5925edd16368SStephen M. Cameron 				status = -EFAULT;
5926e2d4a1f6SStephen M. Cameron 				goto cleanup0;
5927edd16368SStephen M. Cameron 			}
5928edd16368SStephen M. Cameron 			ptr += buff_size[i];
5929edd16368SStephen M. Cameron 		}
5930edd16368SStephen M. Cameron 	}
5931edd16368SStephen M. Cameron 	status = 0;
5932e2d4a1f6SStephen M. Cameron cleanup0:
593345fcb86eSStephen Cameron 	cmd_free(h, c);
5934edd16368SStephen M. Cameron cleanup1:
5935edd16368SStephen M. Cameron 	if (buff) {
59362b08b3e9SDon Brace 		int i;
59372b08b3e9SDon Brace 
5938edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++)
5939edd16368SStephen M. Cameron 			kfree(buff[i]);
5940edd16368SStephen M. Cameron 		kfree(buff);
5941edd16368SStephen M. Cameron 	}
5942edd16368SStephen M. Cameron 	kfree(buff_size);
5943edd16368SStephen M. Cameron 	kfree(ioc);
5944edd16368SStephen M. Cameron 	return status;
5945edd16368SStephen M. Cameron }
5946edd16368SStephen M. Cameron 
5947edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
5948edd16368SStephen M. Cameron 	struct CommandList *c)
5949edd16368SStephen M. Cameron {
5950edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
5951edd16368SStephen M. Cameron 			c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
5952edd16368SStephen M. Cameron 		(void) check_for_unit_attention(h, c);
5953edd16368SStephen M. Cameron }
59540390f0c0SStephen M. Cameron 
5955edd16368SStephen M. Cameron /*
5956edd16368SStephen M. Cameron  * ioctl
5957edd16368SStephen M. Cameron  */
595842a91641SDon Brace static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
5959edd16368SStephen M. Cameron {
5960edd16368SStephen M. Cameron 	struct ctlr_info *h;
5961edd16368SStephen M. Cameron 	void __user *argp = (void __user *)arg;
59620390f0c0SStephen M. Cameron 	int rc;
5963edd16368SStephen M. Cameron 
5964edd16368SStephen M. Cameron 	h = sdev_to_hba(dev);
5965edd16368SStephen M. Cameron 
5966edd16368SStephen M. Cameron 	switch (cmd) {
5967edd16368SStephen M. Cameron 	case CCISS_DEREGDISK:
5968edd16368SStephen M. Cameron 	case CCISS_REGNEWDISK:
5969edd16368SStephen M. Cameron 	case CCISS_REGNEWD:
5970a08a8471SStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
5971edd16368SStephen M. Cameron 		return 0;
5972edd16368SStephen M. Cameron 	case CCISS_GETPCIINFO:
5973edd16368SStephen M. Cameron 		return hpsa_getpciinfo_ioctl(h, argp);
5974edd16368SStephen M. Cameron 	case CCISS_GETDRIVVER:
5975edd16368SStephen M. Cameron 		return hpsa_getdrivver_ioctl(h, argp);
5976edd16368SStephen M. Cameron 	case CCISS_PASSTHRU:
597734f0c627SDon Brace 		if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
59780390f0c0SStephen M. Cameron 			return -EAGAIN;
59790390f0c0SStephen M. Cameron 		rc = hpsa_passthru_ioctl(h, argp);
598034f0c627SDon Brace 		atomic_inc(&h->passthru_cmds_avail);
59810390f0c0SStephen M. Cameron 		return rc;
5982edd16368SStephen M. Cameron 	case CCISS_BIG_PASSTHRU:
598334f0c627SDon Brace 		if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
59840390f0c0SStephen M. Cameron 			return -EAGAIN;
59850390f0c0SStephen M. Cameron 		rc = hpsa_big_passthru_ioctl(h, argp);
598634f0c627SDon Brace 		atomic_inc(&h->passthru_cmds_avail);
59870390f0c0SStephen M. Cameron 		return rc;
5988edd16368SStephen M. Cameron 	default:
5989edd16368SStephen M. Cameron 		return -ENOTTY;
5990edd16368SStephen M. Cameron 	}
5991edd16368SStephen M. Cameron }
5992edd16368SStephen M. Cameron 
5993bf43caf3SRobert Elliott static void hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
59946f039790SGreg Kroah-Hartman 				u8 reset_type)
599564670ac8SStephen M. Cameron {
599664670ac8SStephen M. Cameron 	struct CommandList *c;
599764670ac8SStephen M. Cameron 
599864670ac8SStephen M. Cameron 	c = cmd_alloc(h);
5999bf43caf3SRobert Elliott 
6000a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map */
6001a2dac136SStephen M. Cameron 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
600264670ac8SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_MSG);
600364670ac8SStephen M. Cameron 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
600464670ac8SStephen M. Cameron 	c->waiting = NULL;
600564670ac8SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
600664670ac8SStephen M. Cameron 	/* Don't wait for completion, the reset won't complete.  Don't free
600764670ac8SStephen M. Cameron 	 * the command either.  This is the last command we will send before
600864670ac8SStephen M. Cameron 	 * re-initializing everything, so it doesn't matter and won't leak.
600964670ac8SStephen M. Cameron 	 */
6010bf43caf3SRobert Elliott 	return;
601164670ac8SStephen M. Cameron }
601264670ac8SStephen M. Cameron 
6013a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
6014b7bb24ebSStephen M. Cameron 	void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
6015edd16368SStephen M. Cameron 	int cmd_type)
6016edd16368SStephen M. Cameron {
6017edd16368SStephen M. Cameron 	int pci_dir = XFER_NONE;
60189b5c48c2SStephen Cameron 	u64 tag; /* for commands to be aborted */
6019edd16368SStephen M. Cameron 
6020edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
6021a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_BUSY;
6022edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
6023edd16368SStephen M. Cameron 	if (buff != NULL && size > 0) {
6024edd16368SStephen M. Cameron 		c->Header.SGList = 1;
602550a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(1);
6026edd16368SStephen M. Cameron 	} else {
6027edd16368SStephen M. Cameron 		c->Header.SGList = 0;
602850a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(0);
6029edd16368SStephen M. Cameron 	}
6030edd16368SStephen M. Cameron 	memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
6031edd16368SStephen M. Cameron 
6032edd16368SStephen M. Cameron 	if (cmd_type == TYPE_CMD) {
6033edd16368SStephen M. Cameron 		switch (cmd) {
6034edd16368SStephen M. Cameron 		case HPSA_INQUIRY:
6035edd16368SStephen M. Cameron 			/* are we trying to read a vital product page */
6036b7bb24ebSStephen M. Cameron 			if (page_code & VPD_PAGE) {
6037edd16368SStephen M. Cameron 				c->Request.CDB[1] = 0x01;
6038b7bb24ebSStephen M. Cameron 				c->Request.CDB[2] = (page_code & 0xff);
6039edd16368SStephen M. Cameron 			}
6040edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
6041a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6042a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6043edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
6044edd16368SStephen M. Cameron 			c->Request.CDB[0] = HPSA_INQUIRY;
6045edd16368SStephen M. Cameron 			c->Request.CDB[4] = size & 0xFF;
6046edd16368SStephen M. Cameron 			break;
6047edd16368SStephen M. Cameron 		case HPSA_REPORT_LOG:
6048edd16368SStephen M. Cameron 		case HPSA_REPORT_PHYS:
6049edd16368SStephen M. Cameron 			/* Talking to controller so It's a physical command
6050edd16368SStephen M. Cameron 			   mode = 00 target = 0.  Nothing to write.
6051edd16368SStephen M. Cameron 			 */
6052edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
6053a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6054a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6055edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
6056edd16368SStephen M. Cameron 			c->Request.CDB[0] = cmd;
6057edd16368SStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
6058edd16368SStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
6059edd16368SStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
6060edd16368SStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
6061edd16368SStephen M. Cameron 			break;
6062edd16368SStephen M. Cameron 		case HPSA_CACHE_FLUSH:
6063edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
6064a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6065a505b86fSStephen M. Cameron 					TYPE_ATTR_DIR(cmd_type,
6066a505b86fSStephen M. Cameron 						ATTR_SIMPLE, XFER_WRITE);
6067edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
6068edd16368SStephen M. Cameron 			c->Request.CDB[0] = BMIC_WRITE;
6069edd16368SStephen M. Cameron 			c->Request.CDB[6] = BMIC_CACHE_FLUSH;
6070bb158eabSStephen M. Cameron 			c->Request.CDB[7] = (size >> 8) & 0xFF;
6071bb158eabSStephen M. Cameron 			c->Request.CDB[8] = size & 0xFF;
6072edd16368SStephen M. Cameron 			break;
6073edd16368SStephen M. Cameron 		case TEST_UNIT_READY:
6074edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
6075a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6076a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
6077edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
6078edd16368SStephen M. Cameron 			break;
6079283b4a9bSStephen M. Cameron 		case HPSA_GET_RAID_MAP:
6080283b4a9bSStephen M. Cameron 			c->Request.CDBLen = 12;
6081a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6082a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6083283b4a9bSStephen M. Cameron 			c->Request.Timeout = 0;
6084283b4a9bSStephen M. Cameron 			c->Request.CDB[0] = HPSA_CISS_READ;
6085283b4a9bSStephen M. Cameron 			c->Request.CDB[1] = cmd;
6086283b4a9bSStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
6087283b4a9bSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
6088283b4a9bSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
6089283b4a9bSStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
6090283b4a9bSStephen M. Cameron 			break;
6091316b221aSStephen M. Cameron 		case BMIC_SENSE_CONTROLLER_PARAMETERS:
6092316b221aSStephen M. Cameron 			c->Request.CDBLen = 10;
6093a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6094a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6095316b221aSStephen M. Cameron 			c->Request.Timeout = 0;
6096316b221aSStephen M. Cameron 			c->Request.CDB[0] = BMIC_READ;
6097316b221aSStephen M. Cameron 			c->Request.CDB[6] = BMIC_SENSE_CONTROLLER_PARAMETERS;
6098316b221aSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
6099316b221aSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
6100316b221aSStephen M. Cameron 			break;
610103383736SDon Brace 		case BMIC_IDENTIFY_PHYSICAL_DEVICE:
610203383736SDon Brace 			c->Request.CDBLen = 10;
610303383736SDon Brace 			c->Request.type_attr_dir =
610403383736SDon Brace 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
610503383736SDon Brace 			c->Request.Timeout = 0;
610603383736SDon Brace 			c->Request.CDB[0] = BMIC_READ;
610703383736SDon Brace 			c->Request.CDB[6] = BMIC_IDENTIFY_PHYSICAL_DEVICE;
610803383736SDon Brace 			c->Request.CDB[7] = (size >> 16) & 0xFF;
610903383736SDon Brace 			c->Request.CDB[8] = (size >> 8) & 0XFF;
611003383736SDon Brace 			break;
6111edd16368SStephen M. Cameron 		default:
6112edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
6113edd16368SStephen M. Cameron 			BUG();
6114a2dac136SStephen M. Cameron 			return -1;
6115edd16368SStephen M. Cameron 		}
6116edd16368SStephen M. Cameron 	} else if (cmd_type == TYPE_MSG) {
6117edd16368SStephen M. Cameron 		switch (cmd) {
6118edd16368SStephen M. Cameron 
6119edd16368SStephen M. Cameron 		case  HPSA_DEVICE_RESET_MSG:
6120edd16368SStephen M. Cameron 			c->Request.CDBLen = 16;
6121a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6122a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
6123edd16368SStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
612464670ac8SStephen M. Cameron 			memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
612564670ac8SStephen M. Cameron 			c->Request.CDB[0] =  cmd;
612621e89afdSStephen M. Cameron 			c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
6127edd16368SStephen M. Cameron 			/* If bytes 4-7 are zero, it means reset the */
6128edd16368SStephen M. Cameron 			/* LunID device */
6129edd16368SStephen M. Cameron 			c->Request.CDB[4] = 0x00;
6130edd16368SStephen M. Cameron 			c->Request.CDB[5] = 0x00;
6131edd16368SStephen M. Cameron 			c->Request.CDB[6] = 0x00;
6132edd16368SStephen M. Cameron 			c->Request.CDB[7] = 0x00;
6133edd16368SStephen M. Cameron 			break;
613475167d2cSStephen M. Cameron 		case  HPSA_ABORT_MSG:
61359b5c48c2SStephen Cameron 			memcpy(&tag, buff, sizeof(tag));
61362b08b3e9SDon Brace 			dev_dbg(&h->pdev->dev,
61379b5c48c2SStephen Cameron 				"Abort Tag:0x%016llx using rqst Tag:0x%016llx",
61389b5c48c2SStephen Cameron 				tag, c->Header.tag);
613975167d2cSStephen M. Cameron 			c->Request.CDBLen = 16;
6140a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6141a505b86fSStephen M. Cameron 					TYPE_ATTR_DIR(cmd_type,
6142a505b86fSStephen M. Cameron 						ATTR_SIMPLE, XFER_WRITE);
614375167d2cSStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
614475167d2cSStephen M. Cameron 			c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
614575167d2cSStephen M. Cameron 			c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
614675167d2cSStephen M. Cameron 			c->Request.CDB[2] = 0x00; /* reserved */
614775167d2cSStephen M. Cameron 			c->Request.CDB[3] = 0x00; /* reserved */
614875167d2cSStephen M. Cameron 			/* Tag to abort goes in CDB[4]-CDB[11] */
61499b5c48c2SStephen Cameron 			memcpy(&c->Request.CDB[4], &tag, sizeof(tag));
615075167d2cSStephen M. Cameron 			c->Request.CDB[12] = 0x00; /* reserved */
615175167d2cSStephen M. Cameron 			c->Request.CDB[13] = 0x00; /* reserved */
615275167d2cSStephen M. Cameron 			c->Request.CDB[14] = 0x00; /* reserved */
615375167d2cSStephen M. Cameron 			c->Request.CDB[15] = 0x00; /* reserved */
615475167d2cSStephen M. Cameron 		break;
6155edd16368SStephen M. Cameron 		default:
6156edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown message type %d\n",
6157edd16368SStephen M. Cameron 				cmd);
6158edd16368SStephen M. Cameron 			BUG();
6159edd16368SStephen M. Cameron 		}
6160edd16368SStephen M. Cameron 	} else {
6161edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
6162edd16368SStephen M. Cameron 		BUG();
6163edd16368SStephen M. Cameron 	}
6164edd16368SStephen M. Cameron 
6165a505b86fSStephen M. Cameron 	switch (GET_DIR(c->Request.type_attr_dir)) {
6166edd16368SStephen M. Cameron 	case XFER_READ:
6167edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_FROMDEVICE;
6168edd16368SStephen M. Cameron 		break;
6169edd16368SStephen M. Cameron 	case XFER_WRITE:
6170edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_TODEVICE;
6171edd16368SStephen M. Cameron 		break;
6172edd16368SStephen M. Cameron 	case XFER_NONE:
6173edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_NONE;
6174edd16368SStephen M. Cameron 		break;
6175edd16368SStephen M. Cameron 	default:
6176edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_BIDIRECTIONAL;
6177edd16368SStephen M. Cameron 	}
6178a2dac136SStephen M. Cameron 	if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
6179a2dac136SStephen M. Cameron 		return -1;
6180a2dac136SStephen M. Cameron 	return 0;
6181edd16368SStephen M. Cameron }
6182edd16368SStephen M. Cameron 
6183edd16368SStephen M. Cameron /*
6184edd16368SStephen M. Cameron  * Map (physical) PCI mem into (virtual) kernel space
6185edd16368SStephen M. Cameron  */
6186edd16368SStephen M. Cameron static void __iomem *remap_pci_mem(ulong base, ulong size)
6187edd16368SStephen M. Cameron {
6188edd16368SStephen M. Cameron 	ulong page_base = ((ulong) base) & PAGE_MASK;
6189edd16368SStephen M. Cameron 	ulong page_offs = ((ulong) base) - page_base;
6190088ba34cSStephen M. Cameron 	void __iomem *page_remapped = ioremap_nocache(page_base,
6191088ba34cSStephen M. Cameron 		page_offs + size);
6192edd16368SStephen M. Cameron 
6193edd16368SStephen M. Cameron 	return page_remapped ? (page_remapped + page_offs) : NULL;
6194edd16368SStephen M. Cameron }
6195edd16368SStephen M. Cameron 
6196254f796bSMatt Gates static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
6197edd16368SStephen M. Cameron {
6198254f796bSMatt Gates 	return h->access.command_completed(h, q);
6199edd16368SStephen M. Cameron }
6200edd16368SStephen M. Cameron 
6201900c5440SStephen M. Cameron static inline bool interrupt_pending(struct ctlr_info *h)
6202edd16368SStephen M. Cameron {
6203edd16368SStephen M. Cameron 	return h->access.intr_pending(h);
6204edd16368SStephen M. Cameron }
6205edd16368SStephen M. Cameron 
6206edd16368SStephen M. Cameron static inline long interrupt_not_for_us(struct ctlr_info *h)
6207edd16368SStephen M. Cameron {
620810f66018SStephen M. Cameron 	return (h->access.intr_pending(h) == 0) ||
620910f66018SStephen M. Cameron 		(h->interrupts_enabled == 0);
6210edd16368SStephen M. Cameron }
6211edd16368SStephen M. Cameron 
621201a02ffcSStephen M. Cameron static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
621301a02ffcSStephen M. Cameron 	u32 raw_tag)
6214edd16368SStephen M. Cameron {
6215edd16368SStephen M. Cameron 	if (unlikely(tag_index >= h->nr_cmds)) {
6216edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
6217edd16368SStephen M. Cameron 		return 1;
6218edd16368SStephen M. Cameron 	}
6219edd16368SStephen M. Cameron 	return 0;
6220edd16368SStephen M. Cameron }
6221edd16368SStephen M. Cameron 
62225a3d16f5SStephen M. Cameron static inline void finish_cmd(struct CommandList *c)
6223edd16368SStephen M. Cameron {
6224e85c5974SStephen M. Cameron 	dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
6225c349775eSScott Teel 	if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
6226c349775eSScott Teel 			|| c->cmd_type == CMD_IOACCEL2))
62271fb011fbSStephen M. Cameron 		complete_scsi_command(c);
62288be986ccSStephen Cameron 	else if (c->cmd_type == CMD_IOCTL_PEND || c->cmd_type == IOACCEL2_TMF)
6229edd16368SStephen M. Cameron 		complete(c->waiting);
6230a104c99fSStephen M. Cameron }
6231a104c99fSStephen M. Cameron 
6232a9a3a273SStephen M. Cameron 
6233a9a3a273SStephen M. Cameron static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
6234a104c99fSStephen M. Cameron {
6235a9a3a273SStephen M. Cameron #define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
6236a9a3a273SStephen M. Cameron #define HPSA_SIMPLE_ERROR_BITS 0x03
6237960a30e7SStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
6238a9a3a273SStephen M. Cameron 		return tag & ~HPSA_SIMPLE_ERROR_BITS;
6239a9a3a273SStephen M. Cameron 	return tag & ~HPSA_PERF_ERROR_BITS;
6240a104c99fSStephen M. Cameron }
6241a104c99fSStephen M. Cameron 
6242303932fdSDon Brace /* process completion of an indexed ("direct lookup") command */
62431d94f94dSStephen M. Cameron static inline void process_indexed_cmd(struct ctlr_info *h,
6244303932fdSDon Brace 	u32 raw_tag)
6245303932fdSDon Brace {
6246303932fdSDon Brace 	u32 tag_index;
6247303932fdSDon Brace 	struct CommandList *c;
6248303932fdSDon Brace 
6249f2405db8SDon Brace 	tag_index = raw_tag >> DIRECT_LOOKUP_SHIFT;
62501d94f94dSStephen M. Cameron 	if (!bad_tag(h, tag_index, raw_tag)) {
6251303932fdSDon Brace 		c = h->cmd_pool + tag_index;
62525a3d16f5SStephen M. Cameron 		finish_cmd(c);
62531d94f94dSStephen M. Cameron 	}
6254303932fdSDon Brace }
6255303932fdSDon Brace 
625664670ac8SStephen M. Cameron /* Some controllers, like p400, will give us one interrupt
625764670ac8SStephen M. Cameron  * after a soft reset, even if we turned interrupts off.
625864670ac8SStephen M. Cameron  * Only need to check for this in the hpsa_xxx_discard_completions
625964670ac8SStephen M. Cameron  * functions.
626064670ac8SStephen M. Cameron  */
626164670ac8SStephen M. Cameron static int ignore_bogus_interrupt(struct ctlr_info *h)
626264670ac8SStephen M. Cameron {
626364670ac8SStephen M. Cameron 	if (likely(!reset_devices))
626464670ac8SStephen M. Cameron 		return 0;
626564670ac8SStephen M. Cameron 
626664670ac8SStephen M. Cameron 	if (likely(h->interrupts_enabled))
626764670ac8SStephen M. Cameron 		return 0;
626864670ac8SStephen M. Cameron 
626964670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
627064670ac8SStephen M. Cameron 		"(known firmware bug.)  Ignoring.\n");
627164670ac8SStephen M. Cameron 
627264670ac8SStephen M. Cameron 	return 1;
627364670ac8SStephen M. Cameron }
627464670ac8SStephen M. Cameron 
6275254f796bSMatt Gates /*
6276254f796bSMatt Gates  * Convert &h->q[x] (passed to interrupt handlers) back to h.
6277254f796bSMatt Gates  * Relies on (h-q[x] == x) being true for x such that
6278254f796bSMatt Gates  * 0 <= x < MAX_REPLY_QUEUES.
6279254f796bSMatt Gates  */
6280254f796bSMatt Gates static struct ctlr_info *queue_to_hba(u8 *queue)
628164670ac8SStephen M. Cameron {
6282254f796bSMatt Gates 	return container_of((queue - *queue), struct ctlr_info, q[0]);
6283254f796bSMatt Gates }
6284254f796bSMatt Gates 
6285254f796bSMatt Gates static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
6286254f796bSMatt Gates {
6287254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
6288254f796bSMatt Gates 	u8 q = *(u8 *) queue;
628964670ac8SStephen M. Cameron 	u32 raw_tag;
629064670ac8SStephen M. Cameron 
629164670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
629264670ac8SStephen M. Cameron 		return IRQ_NONE;
629364670ac8SStephen M. Cameron 
629464670ac8SStephen M. Cameron 	if (interrupt_not_for_us(h))
629564670ac8SStephen M. Cameron 		return IRQ_NONE;
6296a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
629764670ac8SStephen M. Cameron 	while (interrupt_pending(h)) {
6298254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
629964670ac8SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY)
6300254f796bSMatt Gates 			raw_tag = next_command(h, q);
630164670ac8SStephen M. Cameron 	}
630264670ac8SStephen M. Cameron 	return IRQ_HANDLED;
630364670ac8SStephen M. Cameron }
630464670ac8SStephen M. Cameron 
6305254f796bSMatt Gates static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
630664670ac8SStephen M. Cameron {
6307254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
630864670ac8SStephen M. Cameron 	u32 raw_tag;
6309254f796bSMatt Gates 	u8 q = *(u8 *) queue;
631064670ac8SStephen M. Cameron 
631164670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
631264670ac8SStephen M. Cameron 		return IRQ_NONE;
631364670ac8SStephen M. Cameron 
6314a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
6315254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
631664670ac8SStephen M. Cameron 	while (raw_tag != FIFO_EMPTY)
6317254f796bSMatt Gates 		raw_tag = next_command(h, q);
631864670ac8SStephen M. Cameron 	return IRQ_HANDLED;
631964670ac8SStephen M. Cameron }
632064670ac8SStephen M. Cameron 
6321254f796bSMatt Gates static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
6322edd16368SStephen M. Cameron {
6323254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba((u8 *) queue);
6324303932fdSDon Brace 	u32 raw_tag;
6325254f796bSMatt Gates 	u8 q = *(u8 *) queue;
6326edd16368SStephen M. Cameron 
6327edd16368SStephen M. Cameron 	if (interrupt_not_for_us(h))
6328edd16368SStephen M. Cameron 		return IRQ_NONE;
6329a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
633010f66018SStephen M. Cameron 	while (interrupt_pending(h)) {
6331254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
633210f66018SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY) {
63331d94f94dSStephen M. Cameron 			process_indexed_cmd(h, raw_tag);
6334254f796bSMatt Gates 			raw_tag = next_command(h, q);
633510f66018SStephen M. Cameron 		}
633610f66018SStephen M. Cameron 	}
633710f66018SStephen M. Cameron 	return IRQ_HANDLED;
633810f66018SStephen M. Cameron }
633910f66018SStephen M. Cameron 
6340254f796bSMatt Gates static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
634110f66018SStephen M. Cameron {
6342254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
634310f66018SStephen M. Cameron 	u32 raw_tag;
6344254f796bSMatt Gates 	u8 q = *(u8 *) queue;
634510f66018SStephen M. Cameron 
6346a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
6347254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
6348303932fdSDon Brace 	while (raw_tag != FIFO_EMPTY) {
63491d94f94dSStephen M. Cameron 		process_indexed_cmd(h, raw_tag);
6350254f796bSMatt Gates 		raw_tag = next_command(h, q);
6351edd16368SStephen M. Cameron 	}
6352edd16368SStephen M. Cameron 	return IRQ_HANDLED;
6353edd16368SStephen M. Cameron }
6354edd16368SStephen M. Cameron 
6355a9a3a273SStephen M. Cameron /* Send a message CDB to the firmware. Careful, this only works
6356a9a3a273SStephen M. Cameron  * in simple mode, not performant mode due to the tag lookup.
6357a9a3a273SStephen M. Cameron  * We only ever use this immediately after a controller reset.
6358a9a3a273SStephen M. Cameron  */
63596f039790SGreg Kroah-Hartman static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
6360edd16368SStephen M. Cameron 			unsigned char type)
6361edd16368SStephen M. Cameron {
6362edd16368SStephen M. Cameron 	struct Command {
6363edd16368SStephen M. Cameron 		struct CommandListHeader CommandHeader;
6364edd16368SStephen M. Cameron 		struct RequestBlock Request;
6365edd16368SStephen M. Cameron 		struct ErrDescriptor ErrorDescriptor;
6366edd16368SStephen M. Cameron 	};
6367edd16368SStephen M. Cameron 	struct Command *cmd;
6368edd16368SStephen M. Cameron 	static const size_t cmd_sz = sizeof(*cmd) +
6369edd16368SStephen M. Cameron 					sizeof(cmd->ErrorDescriptor);
6370edd16368SStephen M. Cameron 	dma_addr_t paddr64;
63712b08b3e9SDon Brace 	__le32 paddr32;
63722b08b3e9SDon Brace 	u32 tag;
6373edd16368SStephen M. Cameron 	void __iomem *vaddr;
6374edd16368SStephen M. Cameron 	int i, err;
6375edd16368SStephen M. Cameron 
6376edd16368SStephen M. Cameron 	vaddr = pci_ioremap_bar(pdev, 0);
6377edd16368SStephen M. Cameron 	if (vaddr == NULL)
6378edd16368SStephen M. Cameron 		return -ENOMEM;
6379edd16368SStephen M. Cameron 
6380edd16368SStephen M. Cameron 	/* The Inbound Post Queue only accepts 32-bit physical addresses for the
6381edd16368SStephen M. Cameron 	 * CCISS commands, so they must be allocated from the lower 4GiB of
6382edd16368SStephen M. Cameron 	 * memory.
6383edd16368SStephen M. Cameron 	 */
6384edd16368SStephen M. Cameron 	err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
6385edd16368SStephen M. Cameron 	if (err) {
6386edd16368SStephen M. Cameron 		iounmap(vaddr);
63871eaec8f3SRobert Elliott 		return err;
6388edd16368SStephen M. Cameron 	}
6389edd16368SStephen M. Cameron 
6390edd16368SStephen M. Cameron 	cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
6391edd16368SStephen M. Cameron 	if (cmd == NULL) {
6392edd16368SStephen M. Cameron 		iounmap(vaddr);
6393edd16368SStephen M. Cameron 		return -ENOMEM;
6394edd16368SStephen M. Cameron 	}
6395edd16368SStephen M. Cameron 
6396edd16368SStephen M. Cameron 	/* This must fit, because of the 32-bit consistent DMA mask.  Also,
6397edd16368SStephen M. Cameron 	 * although there's no guarantee, we assume that the address is at
6398edd16368SStephen M. Cameron 	 * least 4-byte aligned (most likely, it's page-aligned).
6399edd16368SStephen M. Cameron 	 */
64002b08b3e9SDon Brace 	paddr32 = cpu_to_le32(paddr64);
6401edd16368SStephen M. Cameron 
6402edd16368SStephen M. Cameron 	cmd->CommandHeader.ReplyQueue = 0;
6403edd16368SStephen M. Cameron 	cmd->CommandHeader.SGList = 0;
640450a0decfSStephen M. Cameron 	cmd->CommandHeader.SGTotal = cpu_to_le16(0);
64052b08b3e9SDon Brace 	cmd->CommandHeader.tag = cpu_to_le64(paddr64);
6406edd16368SStephen M. Cameron 	memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
6407edd16368SStephen M. Cameron 
6408edd16368SStephen M. Cameron 	cmd->Request.CDBLen = 16;
6409a505b86fSStephen M. Cameron 	cmd->Request.type_attr_dir =
6410a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_MSG, ATTR_HEADOFQUEUE, XFER_NONE);
6411edd16368SStephen M. Cameron 	cmd->Request.Timeout = 0; /* Don't time out */
6412edd16368SStephen M. Cameron 	cmd->Request.CDB[0] = opcode;
6413edd16368SStephen M. Cameron 	cmd->Request.CDB[1] = type;
6414edd16368SStephen M. Cameron 	memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
641550a0decfSStephen M. Cameron 	cmd->ErrorDescriptor.Addr =
64162b08b3e9SDon Brace 			cpu_to_le64((le32_to_cpu(paddr32) + sizeof(*cmd)));
641750a0decfSStephen M. Cameron 	cmd->ErrorDescriptor.Len = cpu_to_le32(sizeof(struct ErrorInfo));
6418edd16368SStephen M. Cameron 
64192b08b3e9SDon Brace 	writel(le32_to_cpu(paddr32), vaddr + SA5_REQUEST_PORT_OFFSET);
6420edd16368SStephen M. Cameron 
6421edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
6422edd16368SStephen M. Cameron 		tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
64232b08b3e9SDon Brace 		if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr64)
6424edd16368SStephen M. Cameron 			break;
6425edd16368SStephen M. Cameron 		msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
6426edd16368SStephen M. Cameron 	}
6427edd16368SStephen M. Cameron 
6428edd16368SStephen M. Cameron 	iounmap(vaddr);
6429edd16368SStephen M. Cameron 
6430edd16368SStephen M. Cameron 	/* we leak the DMA buffer here ... no choice since the controller could
6431edd16368SStephen M. Cameron 	 *  still complete the command.
6432edd16368SStephen M. Cameron 	 */
6433edd16368SStephen M. Cameron 	if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
6434edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
6435edd16368SStephen M. Cameron 			opcode, type);
6436edd16368SStephen M. Cameron 		return -ETIMEDOUT;
6437edd16368SStephen M. Cameron 	}
6438edd16368SStephen M. Cameron 
6439edd16368SStephen M. Cameron 	pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
6440edd16368SStephen M. Cameron 
6441edd16368SStephen M. Cameron 	if (tag & HPSA_ERROR_BIT) {
6442edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
6443edd16368SStephen M. Cameron 			opcode, type);
6444edd16368SStephen M. Cameron 		return -EIO;
6445edd16368SStephen M. Cameron 	}
6446edd16368SStephen M. Cameron 
6447edd16368SStephen M. Cameron 	dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
6448edd16368SStephen M. Cameron 		opcode, type);
6449edd16368SStephen M. Cameron 	return 0;
6450edd16368SStephen M. Cameron }
6451edd16368SStephen M. Cameron 
6452edd16368SStephen M. Cameron #define hpsa_noop(p) hpsa_message(p, 3, 0)
6453edd16368SStephen M. Cameron 
64541df8552aSStephen M. Cameron static int hpsa_controller_hard_reset(struct pci_dev *pdev,
645542a91641SDon Brace 	void __iomem *vaddr, u32 use_doorbell)
6456edd16368SStephen M. Cameron {
6457edd16368SStephen M. Cameron 
64581df8552aSStephen M. Cameron 	if (use_doorbell) {
64591df8552aSStephen M. Cameron 		/* For everything after the P600, the PCI power state method
64601df8552aSStephen M. Cameron 		 * of resetting the controller doesn't work, so we have this
64611df8552aSStephen M. Cameron 		 * other way using the doorbell register.
6462edd16368SStephen M. Cameron 		 */
64631df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using doorbell to reset controller\n");
6464cf0b08d0SStephen M. Cameron 		writel(use_doorbell, vaddr + SA5_DOORBELL);
646585009239SStephen M. Cameron 
646600701a96SJustin Lindley 		/* PMC hardware guys tell us we need a 10 second delay after
646785009239SStephen M. Cameron 		 * doorbell reset and before any attempt to talk to the board
646885009239SStephen M. Cameron 		 * at all to ensure that this actually works and doesn't fall
646985009239SStephen M. Cameron 		 * over in some weird corner cases.
647085009239SStephen M. Cameron 		 */
647100701a96SJustin Lindley 		msleep(10000);
64721df8552aSStephen M. Cameron 	} else { /* Try to do it the PCI power state way */
6473edd16368SStephen M. Cameron 
6474edd16368SStephen M. Cameron 		/* Quoting from the Open CISS Specification: "The Power
6475edd16368SStephen M. Cameron 		 * Management Control/Status Register (CSR) controls the power
6476edd16368SStephen M. Cameron 		 * state of the device.  The normal operating state is D0,
6477edd16368SStephen M. Cameron 		 * CSR=00h.  The software off state is D3, CSR=03h.  To reset
64781df8552aSStephen M. Cameron 		 * the controller, place the interface device in D3 then to D0,
64791df8552aSStephen M. Cameron 		 * this causes a secondary PCI reset which will reset the
64801df8552aSStephen M. Cameron 		 * controller." */
6481edd16368SStephen M. Cameron 
64822662cab8SDon Brace 		int rc = 0;
64832662cab8SDon Brace 
64841df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using PCI PM to reset controller\n");
64852662cab8SDon Brace 
6486edd16368SStephen M. Cameron 		/* enter the D3hot power management state */
64872662cab8SDon Brace 		rc = pci_set_power_state(pdev, PCI_D3hot);
64882662cab8SDon Brace 		if (rc)
64892662cab8SDon Brace 			return rc;
6490edd16368SStephen M. Cameron 
6491edd16368SStephen M. Cameron 		msleep(500);
6492edd16368SStephen M. Cameron 
6493edd16368SStephen M. Cameron 		/* enter the D0 power management state */
64942662cab8SDon Brace 		rc = pci_set_power_state(pdev, PCI_D0);
64952662cab8SDon Brace 		if (rc)
64962662cab8SDon Brace 			return rc;
6497c4853efeSMike Miller 
6498c4853efeSMike Miller 		/*
6499c4853efeSMike Miller 		 * The P600 requires a small delay when changing states.
6500c4853efeSMike Miller 		 * Otherwise we may think the board did not reset and we bail.
6501c4853efeSMike Miller 		 * This for kdump only and is particular to the P600.
6502c4853efeSMike Miller 		 */
6503c4853efeSMike Miller 		msleep(500);
65041df8552aSStephen M. Cameron 	}
65051df8552aSStephen M. Cameron 	return 0;
65061df8552aSStephen M. Cameron }
65071df8552aSStephen M. Cameron 
65086f039790SGreg Kroah-Hartman static void init_driver_version(char *driver_version, int len)
6509580ada3cSStephen M. Cameron {
6510580ada3cSStephen M. Cameron 	memset(driver_version, 0, len);
6511f79cfec6SStephen M. Cameron 	strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
6512580ada3cSStephen M. Cameron }
6513580ada3cSStephen M. Cameron 
65146f039790SGreg Kroah-Hartman static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
6515580ada3cSStephen M. Cameron {
6516580ada3cSStephen M. Cameron 	char *driver_version;
6517580ada3cSStephen M. Cameron 	int i, size = sizeof(cfgtable->driver_version);
6518580ada3cSStephen M. Cameron 
6519580ada3cSStephen M. Cameron 	driver_version = kmalloc(size, GFP_KERNEL);
6520580ada3cSStephen M. Cameron 	if (!driver_version)
6521580ada3cSStephen M. Cameron 		return -ENOMEM;
6522580ada3cSStephen M. Cameron 
6523580ada3cSStephen M. Cameron 	init_driver_version(driver_version, size);
6524580ada3cSStephen M. Cameron 	for (i = 0; i < size; i++)
6525580ada3cSStephen M. Cameron 		writeb(driver_version[i], &cfgtable->driver_version[i]);
6526580ada3cSStephen M. Cameron 	kfree(driver_version);
6527580ada3cSStephen M. Cameron 	return 0;
6528580ada3cSStephen M. Cameron }
6529580ada3cSStephen M. Cameron 
65306f039790SGreg Kroah-Hartman static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
65316f039790SGreg Kroah-Hartman 					  unsigned char *driver_ver)
6532580ada3cSStephen M. Cameron {
6533580ada3cSStephen M. Cameron 	int i;
6534580ada3cSStephen M. Cameron 
6535580ada3cSStephen M. Cameron 	for (i = 0; i < sizeof(cfgtable->driver_version); i++)
6536580ada3cSStephen M. Cameron 		driver_ver[i] = readb(&cfgtable->driver_version[i]);
6537580ada3cSStephen M. Cameron }
6538580ada3cSStephen M. Cameron 
65396f039790SGreg Kroah-Hartman static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
6540580ada3cSStephen M. Cameron {
6541580ada3cSStephen M. Cameron 
6542580ada3cSStephen M. Cameron 	char *driver_ver, *old_driver_ver;
6543580ada3cSStephen M. Cameron 	int rc, size = sizeof(cfgtable->driver_version);
6544580ada3cSStephen M. Cameron 
6545580ada3cSStephen M. Cameron 	old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
6546580ada3cSStephen M. Cameron 	if (!old_driver_ver)
6547580ada3cSStephen M. Cameron 		return -ENOMEM;
6548580ada3cSStephen M. Cameron 	driver_ver = old_driver_ver + size;
6549580ada3cSStephen M. Cameron 
6550580ada3cSStephen M. Cameron 	/* After a reset, the 32 bytes of "driver version" in the cfgtable
6551580ada3cSStephen M. Cameron 	 * should have been changed, otherwise we know the reset failed.
6552580ada3cSStephen M. Cameron 	 */
6553580ada3cSStephen M. Cameron 	init_driver_version(old_driver_ver, size);
6554580ada3cSStephen M. Cameron 	read_driver_ver_from_cfgtable(cfgtable, driver_ver);
6555580ada3cSStephen M. Cameron 	rc = !memcmp(driver_ver, old_driver_ver, size);
6556580ada3cSStephen M. Cameron 	kfree(old_driver_ver);
6557580ada3cSStephen M. Cameron 	return rc;
6558580ada3cSStephen M. Cameron }
65591df8552aSStephen M. Cameron /* This does a hard reset of the controller using PCI power management
65601df8552aSStephen M. Cameron  * states or the using the doorbell register.
65611df8552aSStephen M. Cameron  */
65626b6c1cd7STomas Henzl static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev, u32 board_id)
65631df8552aSStephen M. Cameron {
65641df8552aSStephen M. Cameron 	u64 cfg_offset;
65651df8552aSStephen M. Cameron 	u32 cfg_base_addr;
65661df8552aSStephen M. Cameron 	u64 cfg_base_addr_index;
65671df8552aSStephen M. Cameron 	void __iomem *vaddr;
65681df8552aSStephen M. Cameron 	unsigned long paddr;
6569580ada3cSStephen M. Cameron 	u32 misc_fw_support;
6570270d05deSStephen M. Cameron 	int rc;
65711df8552aSStephen M. Cameron 	struct CfgTable __iomem *cfgtable;
6572cf0b08d0SStephen M. Cameron 	u32 use_doorbell;
6573270d05deSStephen M. Cameron 	u16 command_register;
65741df8552aSStephen M. Cameron 
65751df8552aSStephen M. Cameron 	/* For controllers as old as the P600, this is very nearly
65761df8552aSStephen M. Cameron 	 * the same thing as
65771df8552aSStephen M. Cameron 	 *
65781df8552aSStephen M. Cameron 	 * pci_save_state(pci_dev);
65791df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D3hot);
65801df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D0);
65811df8552aSStephen M. Cameron 	 * pci_restore_state(pci_dev);
65821df8552aSStephen M. Cameron 	 *
65831df8552aSStephen M. Cameron 	 * For controllers newer than the P600, the pci power state
65841df8552aSStephen M. Cameron 	 * method of resetting doesn't work so we have another way
65851df8552aSStephen M. Cameron 	 * using the doorbell register.
65861df8552aSStephen M. Cameron 	 */
658718867659SStephen M. Cameron 
658860f923b9SRobert Elliott 	if (!ctlr_is_resettable(board_id)) {
658960f923b9SRobert Elliott 		dev_warn(&pdev->dev, "Controller not resettable\n");
659025c1e56aSStephen M. Cameron 		return -ENODEV;
659125c1e56aSStephen M. Cameron 	}
659246380786SStephen M. Cameron 
659346380786SStephen M. Cameron 	/* if controller is soft- but not hard resettable... */
659446380786SStephen M. Cameron 	if (!ctlr_is_hard_resettable(board_id))
659546380786SStephen M. Cameron 		return -ENOTSUPP; /* try soft reset later. */
659618867659SStephen M. Cameron 
6597270d05deSStephen M. Cameron 	/* Save the PCI command register */
6598270d05deSStephen M. Cameron 	pci_read_config_word(pdev, 4, &command_register);
6599270d05deSStephen M. Cameron 	pci_save_state(pdev);
66001df8552aSStephen M. Cameron 
66011df8552aSStephen M. Cameron 	/* find the first memory BAR, so we can find the cfg table */
66021df8552aSStephen M. Cameron 	rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
66031df8552aSStephen M. Cameron 	if (rc)
66041df8552aSStephen M. Cameron 		return rc;
66051df8552aSStephen M. Cameron 	vaddr = remap_pci_mem(paddr, 0x250);
66061df8552aSStephen M. Cameron 	if (!vaddr)
66071df8552aSStephen M. Cameron 		return -ENOMEM;
66081df8552aSStephen M. Cameron 
66091df8552aSStephen M. Cameron 	/* find cfgtable in order to check if reset via doorbell is supported */
66101df8552aSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
66111df8552aSStephen M. Cameron 					&cfg_base_addr_index, &cfg_offset);
66121df8552aSStephen M. Cameron 	if (rc)
66131df8552aSStephen M. Cameron 		goto unmap_vaddr;
66141df8552aSStephen M. Cameron 	cfgtable = remap_pci_mem(pci_resource_start(pdev,
66151df8552aSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
66161df8552aSStephen M. Cameron 	if (!cfgtable) {
66171df8552aSStephen M. Cameron 		rc = -ENOMEM;
66181df8552aSStephen M. Cameron 		goto unmap_vaddr;
66191df8552aSStephen M. Cameron 	}
6620580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(cfgtable);
6621580ada3cSStephen M. Cameron 	if (rc)
662203741d95STomas Henzl 		goto unmap_cfgtable;
66231df8552aSStephen M. Cameron 
6624cf0b08d0SStephen M. Cameron 	/* If reset via doorbell register is supported, use that.
6625cf0b08d0SStephen M. Cameron 	 * There are two such methods.  Favor the newest method.
6626cf0b08d0SStephen M. Cameron 	 */
66271df8552aSStephen M. Cameron 	misc_fw_support = readl(&cfgtable->misc_fw_support);
6628cf0b08d0SStephen M. Cameron 	use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
6629cf0b08d0SStephen M. Cameron 	if (use_doorbell) {
6630cf0b08d0SStephen M. Cameron 		use_doorbell = DOORBELL_CTLR_RESET2;
6631cf0b08d0SStephen M. Cameron 	} else {
66321df8552aSStephen M. Cameron 		use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
6633cf0b08d0SStephen M. Cameron 		if (use_doorbell) {
6634050f7147SStephen Cameron 			dev_warn(&pdev->dev,
6635050f7147SStephen Cameron 				"Soft reset not supported. Firmware update is required.\n");
663664670ac8SStephen M. Cameron 			rc = -ENOTSUPP; /* try soft reset */
6637cf0b08d0SStephen M. Cameron 			goto unmap_cfgtable;
6638cf0b08d0SStephen M. Cameron 		}
6639cf0b08d0SStephen M. Cameron 	}
66401df8552aSStephen M. Cameron 
66411df8552aSStephen M. Cameron 	rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
66421df8552aSStephen M. Cameron 	if (rc)
66431df8552aSStephen M. Cameron 		goto unmap_cfgtable;
6644edd16368SStephen M. Cameron 
6645270d05deSStephen M. Cameron 	pci_restore_state(pdev);
6646270d05deSStephen M. Cameron 	pci_write_config_word(pdev, 4, command_register);
6647edd16368SStephen M. Cameron 
66481df8552aSStephen M. Cameron 	/* Some devices (notably the HP Smart Array 5i Controller)
66491df8552aSStephen M. Cameron 	   need a little pause here */
66501df8552aSStephen M. Cameron 	msleep(HPSA_POST_RESET_PAUSE_MSECS);
66511df8552aSStephen M. Cameron 
6652fe5389c8SStephen M. Cameron 	rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
6653fe5389c8SStephen M. Cameron 	if (rc) {
6654fe5389c8SStephen M. Cameron 		dev_warn(&pdev->dev,
6655050f7147SStephen Cameron 			"Failed waiting for board to become ready after hard reset\n");
6656fe5389c8SStephen M. Cameron 		goto unmap_cfgtable;
6657fe5389c8SStephen M. Cameron 	}
6658fe5389c8SStephen M. Cameron 
6659580ada3cSStephen M. Cameron 	rc = controller_reset_failed(vaddr);
6660580ada3cSStephen M. Cameron 	if (rc < 0)
6661580ada3cSStephen M. Cameron 		goto unmap_cfgtable;
6662580ada3cSStephen M. Cameron 	if (rc) {
666364670ac8SStephen M. Cameron 		dev_warn(&pdev->dev, "Unable to successfully reset "
666464670ac8SStephen M. Cameron 			"controller. Will try soft reset.\n");
666564670ac8SStephen M. Cameron 		rc = -ENOTSUPP;
6666580ada3cSStephen M. Cameron 	} else {
666764670ac8SStephen M. Cameron 		dev_info(&pdev->dev, "board ready after hard reset.\n");
66681df8552aSStephen M. Cameron 	}
66691df8552aSStephen M. Cameron 
66701df8552aSStephen M. Cameron unmap_cfgtable:
66711df8552aSStephen M. Cameron 	iounmap(cfgtable);
66721df8552aSStephen M. Cameron 
66731df8552aSStephen M. Cameron unmap_vaddr:
66741df8552aSStephen M. Cameron 	iounmap(vaddr);
66751df8552aSStephen M. Cameron 	return rc;
6676edd16368SStephen M. Cameron }
6677edd16368SStephen M. Cameron 
6678edd16368SStephen M. Cameron /*
6679edd16368SStephen M. Cameron  *  We cannot read the structure directly, for portability we must use
6680edd16368SStephen M. Cameron  *   the io functions.
6681edd16368SStephen M. Cameron  *   This is for debug only.
6682edd16368SStephen M. Cameron  */
668342a91641SDon Brace static void print_cfg_table(struct device *dev, struct CfgTable __iomem *tb)
6684edd16368SStephen M. Cameron {
668558f8665cSStephen M. Cameron #ifdef HPSA_DEBUG
6686edd16368SStephen M. Cameron 	int i;
6687edd16368SStephen M. Cameron 	char temp_name[17];
6688edd16368SStephen M. Cameron 
6689edd16368SStephen M. Cameron 	dev_info(dev, "Controller Configuration information\n");
6690edd16368SStephen M. Cameron 	dev_info(dev, "------------------------------------\n");
6691edd16368SStephen M. Cameron 	for (i = 0; i < 4; i++)
6692edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->Signature[i]));
6693edd16368SStephen M. Cameron 	temp_name[4] = '\0';
6694edd16368SStephen M. Cameron 	dev_info(dev, "   Signature = %s\n", temp_name);
6695edd16368SStephen M. Cameron 	dev_info(dev, "   Spec Number = %d\n", readl(&(tb->SpecValence)));
6696edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods supported = 0x%x\n",
6697edd16368SStephen M. Cameron 	       readl(&(tb->TransportSupport)));
6698edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods active = 0x%x\n",
6699edd16368SStephen M. Cameron 	       readl(&(tb->TransportActive)));
6700edd16368SStephen M. Cameron 	dev_info(dev, "   Requested transport Method = 0x%x\n",
6701edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.TransportRequest)));
6702edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Delay = 0x%x\n",
6703edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntDelay)));
6704edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Count = 0x%x\n",
6705edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntCount)));
670669d6e33dSRobert Elliott 	dev_info(dev, "   Max outstanding commands = %d\n",
6707edd16368SStephen M. Cameron 	       readl(&(tb->CmdsOutMax)));
6708edd16368SStephen M. Cameron 	dev_info(dev, "   Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
6709edd16368SStephen M. Cameron 	for (i = 0; i < 16; i++)
6710edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->ServerName[i]));
6711edd16368SStephen M. Cameron 	temp_name[16] = '\0';
6712edd16368SStephen M. Cameron 	dev_info(dev, "   Server Name = %s\n", temp_name);
6713edd16368SStephen M. Cameron 	dev_info(dev, "   Heartbeat Counter = 0x%x\n\n\n",
6714edd16368SStephen M. Cameron 		readl(&(tb->HeartBeat)));
6715edd16368SStephen M. Cameron #endif				/* HPSA_DEBUG */
671658f8665cSStephen M. Cameron }
6717edd16368SStephen M. Cameron 
6718edd16368SStephen M. Cameron static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
6719edd16368SStephen M. Cameron {
6720edd16368SStephen M. Cameron 	int i, offset, mem_type, bar_type;
6721edd16368SStephen M. Cameron 
6722edd16368SStephen M. Cameron 	if (pci_bar_addr == PCI_BASE_ADDRESS_0)	/* looking for BAR zero? */
6723edd16368SStephen M. Cameron 		return 0;
6724edd16368SStephen M. Cameron 	offset = 0;
6725edd16368SStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
6726edd16368SStephen M. Cameron 		bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
6727edd16368SStephen M. Cameron 		if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
6728edd16368SStephen M. Cameron 			offset += 4;
6729edd16368SStephen M. Cameron 		else {
6730edd16368SStephen M. Cameron 			mem_type = pci_resource_flags(pdev, i) &
6731edd16368SStephen M. Cameron 			    PCI_BASE_ADDRESS_MEM_TYPE_MASK;
6732edd16368SStephen M. Cameron 			switch (mem_type) {
6733edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_32:
6734edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_1M:
6735edd16368SStephen M. Cameron 				offset += 4;	/* 32 bit */
6736edd16368SStephen M. Cameron 				break;
6737edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_64:
6738edd16368SStephen M. Cameron 				offset += 8;
6739edd16368SStephen M. Cameron 				break;
6740edd16368SStephen M. Cameron 			default:	/* reserved in PCI 2.2 */
6741edd16368SStephen M. Cameron 				dev_warn(&pdev->dev,
6742edd16368SStephen M. Cameron 				       "base address is invalid\n");
6743edd16368SStephen M. Cameron 				return -1;
6744edd16368SStephen M. Cameron 				break;
6745edd16368SStephen M. Cameron 			}
6746edd16368SStephen M. Cameron 		}
6747edd16368SStephen M. Cameron 		if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
6748edd16368SStephen M. Cameron 			return i + 1;
6749edd16368SStephen M. Cameron 	}
6750edd16368SStephen M. Cameron 	return -1;
6751edd16368SStephen M. Cameron }
6752edd16368SStephen M. Cameron 
6753cc64c817SRobert Elliott static void hpsa_disable_interrupt_mode(struct ctlr_info *h)
6754cc64c817SRobert Elliott {
6755cc64c817SRobert Elliott 	if (h->msix_vector) {
6756cc64c817SRobert Elliott 		if (h->pdev->msix_enabled)
6757cc64c817SRobert Elliott 			pci_disable_msix(h->pdev);
6758105a3dbcSRobert Elliott 		h->msix_vector = 0;
6759cc64c817SRobert Elliott 	} else if (h->msi_vector) {
6760cc64c817SRobert Elliott 		if (h->pdev->msi_enabled)
6761cc64c817SRobert Elliott 			pci_disable_msi(h->pdev);
6762105a3dbcSRobert Elliott 		h->msi_vector = 0;
6763cc64c817SRobert Elliott 	}
6764cc64c817SRobert Elliott }
6765cc64c817SRobert Elliott 
6766edd16368SStephen M. Cameron /* If MSI/MSI-X is supported by the kernel we will try to enable it on
6767050f7147SStephen Cameron  * controllers that are capable. If not, we use legacy INTx mode.
6768edd16368SStephen M. Cameron  */
67696f039790SGreg Kroah-Hartman static void hpsa_interrupt_mode(struct ctlr_info *h)
6770edd16368SStephen M. Cameron {
6771edd16368SStephen M. Cameron #ifdef CONFIG_PCI_MSI
6772254f796bSMatt Gates 	int err, i;
6773254f796bSMatt Gates 	struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
6774254f796bSMatt Gates 
6775254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++) {
6776254f796bSMatt Gates 		hpsa_msix_entries[i].vector = 0;
6777254f796bSMatt Gates 		hpsa_msix_entries[i].entry = i;
6778254f796bSMatt Gates 	}
6779edd16368SStephen M. Cameron 
6780edd16368SStephen M. Cameron 	/* Some boards advertise MSI but don't really support it */
67816b3f4c52SStephen M. Cameron 	if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
67826b3f4c52SStephen M. Cameron 	    (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
6783edd16368SStephen M. Cameron 		goto default_int_mode;
678455c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
6785050f7147SStephen Cameron 		dev_info(&h->pdev->dev, "MSI-X capable controller\n");
6786eee0f03aSHannes Reinecke 		h->msix_vector = MAX_REPLY_QUEUES;
6787f89439bcSStephen M. Cameron 		if (h->msix_vector > num_online_cpus())
6788f89439bcSStephen M. Cameron 			h->msix_vector = num_online_cpus();
678918fce3c4SAlexander Gordeev 		err = pci_enable_msix_range(h->pdev, hpsa_msix_entries,
679018fce3c4SAlexander Gordeev 					    1, h->msix_vector);
679118fce3c4SAlexander Gordeev 		if (err < 0) {
679218fce3c4SAlexander Gordeev 			dev_warn(&h->pdev->dev, "MSI-X init failed %d\n", err);
679318fce3c4SAlexander Gordeev 			h->msix_vector = 0;
679418fce3c4SAlexander Gordeev 			goto single_msi_mode;
679518fce3c4SAlexander Gordeev 		} else if (err < h->msix_vector) {
679655c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
6797edd16368SStephen M. Cameron 			       "available\n", err);
6798eee0f03aSHannes Reinecke 		}
679918fce3c4SAlexander Gordeev 		h->msix_vector = err;
6800eee0f03aSHannes Reinecke 		for (i = 0; i < h->msix_vector; i++)
6801eee0f03aSHannes Reinecke 			h->intr[i] = hpsa_msix_entries[i].vector;
6802eee0f03aSHannes Reinecke 		return;
6803edd16368SStephen M. Cameron 	}
680418fce3c4SAlexander Gordeev single_msi_mode:
680555c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
6806050f7147SStephen Cameron 		dev_info(&h->pdev->dev, "MSI capable controller\n");
680755c06c71SStephen M. Cameron 		if (!pci_enable_msi(h->pdev))
6808edd16368SStephen M. Cameron 			h->msi_vector = 1;
6809edd16368SStephen M. Cameron 		else
681055c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "MSI init failed\n");
6811edd16368SStephen M. Cameron 	}
6812edd16368SStephen M. Cameron default_int_mode:
6813edd16368SStephen M. Cameron #endif				/* CONFIG_PCI_MSI */
6814edd16368SStephen M. Cameron 	/* if we get here we're going to use the default interrupt mode */
6815a9a3a273SStephen M. Cameron 	h->intr[h->intr_mode] = h->pdev->irq;
6816edd16368SStephen M. Cameron }
6817edd16368SStephen M. Cameron 
68186f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
6819e5c880d1SStephen M. Cameron {
6820e5c880d1SStephen M. Cameron 	int i;
6821e5c880d1SStephen M. Cameron 	u32 subsystem_vendor_id, subsystem_device_id;
6822e5c880d1SStephen M. Cameron 
6823e5c880d1SStephen M. Cameron 	subsystem_vendor_id = pdev->subsystem_vendor;
6824e5c880d1SStephen M. Cameron 	subsystem_device_id = pdev->subsystem_device;
6825e5c880d1SStephen M. Cameron 	*board_id = ((subsystem_device_id << 16) & 0xffff0000) |
6826e5c880d1SStephen M. Cameron 		    subsystem_vendor_id;
6827e5c880d1SStephen M. Cameron 
6828e5c880d1SStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(products); i++)
6829e5c880d1SStephen M. Cameron 		if (*board_id == products[i].board_id)
6830e5c880d1SStephen M. Cameron 			return i;
6831e5c880d1SStephen M. Cameron 
68326798cc0aSStephen M. Cameron 	if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
68336798cc0aSStephen M. Cameron 		subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
68346798cc0aSStephen M. Cameron 		!hpsa_allow_any) {
6835e5c880d1SStephen M. Cameron 		dev_warn(&pdev->dev, "unrecognized board ID: "
6836e5c880d1SStephen M. Cameron 			"0x%08x, ignoring.\n", *board_id);
6837e5c880d1SStephen M. Cameron 			return -ENODEV;
6838e5c880d1SStephen M. Cameron 	}
6839e5c880d1SStephen M. Cameron 	return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
6840e5c880d1SStephen M. Cameron }
6841e5c880d1SStephen M. Cameron 
68426f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
68433a7774ceSStephen M. Cameron 				    unsigned long *memory_bar)
68443a7774ceSStephen M. Cameron {
68453a7774ceSStephen M. Cameron 	int i;
68463a7774ceSStephen M. Cameron 
68473a7774ceSStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
684812d2cd47SStephen M. Cameron 		if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
68493a7774ceSStephen M. Cameron 			/* addressing mode bits already removed */
685012d2cd47SStephen M. Cameron 			*memory_bar = pci_resource_start(pdev, i);
685112d2cd47SStephen M. Cameron 			dev_dbg(&pdev->dev, "memory BAR = %lx\n",
68523a7774ceSStephen M. Cameron 				*memory_bar);
68533a7774ceSStephen M. Cameron 			return 0;
68543a7774ceSStephen M. Cameron 		}
685512d2cd47SStephen M. Cameron 	dev_warn(&pdev->dev, "no memory BAR found\n");
68563a7774ceSStephen M. Cameron 	return -ENODEV;
68573a7774ceSStephen M. Cameron }
68583a7774ceSStephen M. Cameron 
68596f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
68606f039790SGreg Kroah-Hartman 				     int wait_for_ready)
68612c4c8c8bSStephen M. Cameron {
6862fe5389c8SStephen M. Cameron 	int i, iterations;
68632c4c8c8bSStephen M. Cameron 	u32 scratchpad;
6864fe5389c8SStephen M. Cameron 	if (wait_for_ready)
6865fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_READY_ITERATIONS;
6866fe5389c8SStephen M. Cameron 	else
6867fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
68682c4c8c8bSStephen M. Cameron 
6869fe5389c8SStephen M. Cameron 	for (i = 0; i < iterations; i++) {
6870fe5389c8SStephen M. Cameron 		scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
6871fe5389c8SStephen M. Cameron 		if (wait_for_ready) {
68722c4c8c8bSStephen M. Cameron 			if (scratchpad == HPSA_FIRMWARE_READY)
68732c4c8c8bSStephen M. Cameron 				return 0;
6874fe5389c8SStephen M. Cameron 		} else {
6875fe5389c8SStephen M. Cameron 			if (scratchpad != HPSA_FIRMWARE_READY)
6876fe5389c8SStephen M. Cameron 				return 0;
6877fe5389c8SStephen M. Cameron 		}
68782c4c8c8bSStephen M. Cameron 		msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
68792c4c8c8bSStephen M. Cameron 	}
6880fe5389c8SStephen M. Cameron 	dev_warn(&pdev->dev, "board not ready, timed out.\n");
68812c4c8c8bSStephen M. Cameron 	return -ENODEV;
68822c4c8c8bSStephen M. Cameron }
68832c4c8c8bSStephen M. Cameron 
68846f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
68856f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
6886a51fd47fSStephen M. Cameron 			       u64 *cfg_offset)
6887a51fd47fSStephen M. Cameron {
6888a51fd47fSStephen M. Cameron 	*cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
6889a51fd47fSStephen M. Cameron 	*cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
6890a51fd47fSStephen M. Cameron 	*cfg_base_addr &= (u32) 0x0000ffff;
6891a51fd47fSStephen M. Cameron 	*cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
6892a51fd47fSStephen M. Cameron 	if (*cfg_base_addr_index == -1) {
6893a51fd47fSStephen M. Cameron 		dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
6894a51fd47fSStephen M. Cameron 		return -ENODEV;
6895a51fd47fSStephen M. Cameron 	}
6896a51fd47fSStephen M. Cameron 	return 0;
6897a51fd47fSStephen M. Cameron }
6898a51fd47fSStephen M. Cameron 
6899195f2c65SRobert Elliott static void hpsa_free_cfgtables(struct ctlr_info *h)
6900195f2c65SRobert Elliott {
6901105a3dbcSRobert Elliott 	if (h->transtable) {
6902195f2c65SRobert Elliott 		iounmap(h->transtable);
6903105a3dbcSRobert Elliott 		h->transtable = NULL;
6904105a3dbcSRobert Elliott 	}
6905105a3dbcSRobert Elliott 	if (h->cfgtable) {
6906195f2c65SRobert Elliott 		iounmap(h->cfgtable);
6907105a3dbcSRobert Elliott 		h->cfgtable = NULL;
6908105a3dbcSRobert Elliott 	}
6909195f2c65SRobert Elliott }
6910195f2c65SRobert Elliott 
6911195f2c65SRobert Elliott /* Find and map CISS config table and transfer table
6912195f2c65SRobert Elliott + * several items must be unmapped (freed) later
6913195f2c65SRobert Elliott + * */
69146f039790SGreg Kroah-Hartman static int hpsa_find_cfgtables(struct ctlr_info *h)
6915edd16368SStephen M. Cameron {
691601a02ffcSStephen M. Cameron 	u64 cfg_offset;
691701a02ffcSStephen M. Cameron 	u32 cfg_base_addr;
691801a02ffcSStephen M. Cameron 	u64 cfg_base_addr_index;
6919303932fdSDon Brace 	u32 trans_offset;
6920a51fd47fSStephen M. Cameron 	int rc;
692177c4495cSStephen M. Cameron 
6922a51fd47fSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
6923a51fd47fSStephen M. Cameron 		&cfg_base_addr_index, &cfg_offset);
6924a51fd47fSStephen M. Cameron 	if (rc)
6925a51fd47fSStephen M. Cameron 		return rc;
692677c4495cSStephen M. Cameron 	h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
6927a51fd47fSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
6928cd3c81c4SRobert Elliott 	if (!h->cfgtable) {
6929cd3c81c4SRobert Elliott 		dev_err(&h->pdev->dev, "Failed mapping cfgtable\n");
693077c4495cSStephen M. Cameron 		return -ENOMEM;
6931cd3c81c4SRobert Elliott 	}
6932580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(h->cfgtable);
6933580ada3cSStephen M. Cameron 	if (rc)
6934580ada3cSStephen M. Cameron 		return rc;
693577c4495cSStephen M. Cameron 	/* Find performant mode table. */
6936a51fd47fSStephen M. Cameron 	trans_offset = readl(&h->cfgtable->TransMethodOffset);
693777c4495cSStephen M. Cameron 	h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
693877c4495cSStephen M. Cameron 				cfg_base_addr_index)+cfg_offset+trans_offset,
693977c4495cSStephen M. Cameron 				sizeof(*h->transtable));
6940195f2c65SRobert Elliott 	if (!h->transtable) {
6941195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "Failed mapping transfer table\n");
6942195f2c65SRobert Elliott 		hpsa_free_cfgtables(h);
694377c4495cSStephen M. Cameron 		return -ENOMEM;
6944195f2c65SRobert Elliott 	}
694577c4495cSStephen M. Cameron 	return 0;
694677c4495cSStephen M. Cameron }
694777c4495cSStephen M. Cameron 
69486f039790SGreg Kroah-Hartman static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
6949cba3d38bSStephen M. Cameron {
695041ce4c35SStephen Cameron #define MIN_MAX_COMMANDS 16
695141ce4c35SStephen Cameron 	BUILD_BUG_ON(MIN_MAX_COMMANDS <= HPSA_NRESERVED_CMDS);
695241ce4c35SStephen Cameron 
695341ce4c35SStephen Cameron 	h->max_commands = readl(&h->cfgtable->MaxPerformantModeCommands);
695472ceeaecSStephen M. Cameron 
695572ceeaecSStephen M. Cameron 	/* Limit commands in memory limited kdump scenario. */
695672ceeaecSStephen M. Cameron 	if (reset_devices && h->max_commands > 32)
695772ceeaecSStephen M. Cameron 		h->max_commands = 32;
695872ceeaecSStephen M. Cameron 
695941ce4c35SStephen Cameron 	if (h->max_commands < MIN_MAX_COMMANDS) {
696041ce4c35SStephen Cameron 		dev_warn(&h->pdev->dev,
696141ce4c35SStephen Cameron 			"Controller reports max supported commands of %d Using %d instead. Ensure that firmware is up to date.\n",
696241ce4c35SStephen Cameron 			h->max_commands,
696341ce4c35SStephen Cameron 			MIN_MAX_COMMANDS);
696441ce4c35SStephen Cameron 		h->max_commands = MIN_MAX_COMMANDS;
6965cba3d38bSStephen M. Cameron 	}
6966cba3d38bSStephen M. Cameron }
6967cba3d38bSStephen M. Cameron 
6968c7ee65b3SWebb Scales /* If the controller reports that the total max sg entries is greater than 512,
6969c7ee65b3SWebb Scales  * then we know that chained SG blocks work.  (Original smart arrays did not
6970c7ee65b3SWebb Scales  * support chained SG blocks and would return zero for max sg entries.)
6971c7ee65b3SWebb Scales  */
6972c7ee65b3SWebb Scales static int hpsa_supports_chained_sg_blocks(struct ctlr_info *h)
6973c7ee65b3SWebb Scales {
6974c7ee65b3SWebb Scales 	return h->maxsgentries > 512;
6975c7ee65b3SWebb Scales }
6976c7ee65b3SWebb Scales 
6977b93d7536SStephen M. Cameron /* Interrogate the hardware for some limits:
6978b93d7536SStephen M. Cameron  * max commands, max SG elements without chaining, and with chaining,
6979b93d7536SStephen M. Cameron  * SG chain block size, etc.
6980b93d7536SStephen M. Cameron  */
69816f039790SGreg Kroah-Hartman static void hpsa_find_board_params(struct ctlr_info *h)
6982b93d7536SStephen M. Cameron {
6983cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
698445fcb86eSStephen Cameron 	h->nr_cmds = h->max_commands;
6985b93d7536SStephen M. Cameron 	h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
6986283b4a9bSStephen M. Cameron 	h->fw_support = readl(&(h->cfgtable->misc_fw_support));
6987c7ee65b3SWebb Scales 	if (hpsa_supports_chained_sg_blocks(h)) {
6988c7ee65b3SWebb Scales 		/* Limit in-command s/g elements to 32 save dma'able memory. */
6989b93d7536SStephen M. Cameron 		h->max_cmd_sg_entries = 32;
69901a63ea6fSWebb Scales 		h->chainsize = h->maxsgentries - h->max_cmd_sg_entries;
6991b93d7536SStephen M. Cameron 		h->maxsgentries--; /* save one for chain pointer */
6992b93d7536SStephen M. Cameron 	} else {
6993c7ee65b3SWebb Scales 		/*
6994c7ee65b3SWebb Scales 		 * Original smart arrays supported at most 31 s/g entries
6995c7ee65b3SWebb Scales 		 * embedded inline in the command (trying to use more
6996c7ee65b3SWebb Scales 		 * would lock up the controller)
6997c7ee65b3SWebb Scales 		 */
6998c7ee65b3SWebb Scales 		h->max_cmd_sg_entries = 31;
69991a63ea6fSWebb Scales 		h->maxsgentries = 31; /* default to traditional values */
7000c7ee65b3SWebb Scales 		h->chainsize = 0;
7001b93d7536SStephen M. Cameron 	}
700275167d2cSStephen M. Cameron 
700375167d2cSStephen M. Cameron 	/* Find out what task management functions are supported and cache */
700475167d2cSStephen M. Cameron 	h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
70050e7a7fceSScott Teel 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
70060e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
70070e7a7fceSScott Teel 	if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
70080e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
70098be986ccSStephen Cameron 	if (!(HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags))
70108be986ccSStephen Cameron 		dev_warn(&h->pdev->dev, "HP SSD Smart Path aborts not supported\n");
7011b93d7536SStephen M. Cameron }
7012b93d7536SStephen M. Cameron 
701376c46e49SStephen M. Cameron static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
701476c46e49SStephen M. Cameron {
70150fc9fd40SAkinobu Mita 	if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
7016050f7147SStephen Cameron 		dev_err(&h->pdev->dev, "not a valid CISS config table\n");
701776c46e49SStephen M. Cameron 		return false;
701876c46e49SStephen M. Cameron 	}
701976c46e49SStephen M. Cameron 	return true;
702076c46e49SStephen M. Cameron }
702176c46e49SStephen M. Cameron 
702297a5e98cSStephen M. Cameron static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
7023f7c39101SStephen M. Cameron {
702497a5e98cSStephen M. Cameron 	u32 driver_support;
7025f7c39101SStephen M. Cameron 
702697a5e98cSStephen M. Cameron 	driver_support = readl(&(h->cfgtable->driver_support));
70270b9e7b74SArnd Bergmann 	/* Need to enable prefetch in the SCSI core for 6400 in x86 */
70280b9e7b74SArnd Bergmann #ifdef CONFIG_X86
702997a5e98cSStephen M. Cameron 	driver_support |= ENABLE_SCSI_PREFETCH;
7030f7c39101SStephen M. Cameron #endif
703128e13446SStephen M. Cameron 	driver_support |= ENABLE_UNIT_ATTN;
703228e13446SStephen M. Cameron 	writel(driver_support, &(h->cfgtable->driver_support));
7033f7c39101SStephen M. Cameron }
7034f7c39101SStephen M. Cameron 
70353d0eab67SStephen M. Cameron /* Disable DMA prefetch for the P600.  Otherwise an ASIC bug may result
70363d0eab67SStephen M. Cameron  * in a prefetch beyond physical memory.
70373d0eab67SStephen M. Cameron  */
70383d0eab67SStephen M. Cameron static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
70393d0eab67SStephen M. Cameron {
70403d0eab67SStephen M. Cameron 	u32 dma_prefetch;
70413d0eab67SStephen M. Cameron 
70423d0eab67SStephen M. Cameron 	if (h->board_id != 0x3225103C)
70433d0eab67SStephen M. Cameron 		return;
70443d0eab67SStephen M. Cameron 	dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
70453d0eab67SStephen M. Cameron 	dma_prefetch |= 0x8000;
70463d0eab67SStephen M. Cameron 	writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
70473d0eab67SStephen M. Cameron }
70483d0eab67SStephen M. Cameron 
7049c706a795SRobert Elliott static int hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
705076438d08SStephen M. Cameron {
705176438d08SStephen M. Cameron 	int i;
705276438d08SStephen M. Cameron 	u32 doorbell_value;
705376438d08SStephen M. Cameron 	unsigned long flags;
705476438d08SStephen M. Cameron 	/* wait until the clear_event_notify bit 6 is cleared by controller. */
7055007e7aa9SRobert Elliott 	for (i = 0; i < MAX_CLEAR_EVENT_WAIT; i++) {
705676438d08SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
705776438d08SStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
705876438d08SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
705976438d08SStephen M. Cameron 		if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
7060c706a795SRobert Elliott 			goto done;
706176438d08SStephen M. Cameron 		/* delay and try again */
7062007e7aa9SRobert Elliott 		msleep(CLEAR_EVENT_WAIT_INTERVAL);
706376438d08SStephen M. Cameron 	}
7064c706a795SRobert Elliott 	return -ENODEV;
7065c706a795SRobert Elliott done:
7066c706a795SRobert Elliott 	return 0;
706776438d08SStephen M. Cameron }
706876438d08SStephen M. Cameron 
7069c706a795SRobert Elliott static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
7070eb6b2ae9SStephen M. Cameron {
7071eb6b2ae9SStephen M. Cameron 	int i;
70726eaf46fdSStephen M. Cameron 	u32 doorbell_value;
70736eaf46fdSStephen M. Cameron 	unsigned long flags;
7074eb6b2ae9SStephen M. Cameron 
7075eb6b2ae9SStephen M. Cameron 	/* under certain very rare conditions, this can take awhile.
7076eb6b2ae9SStephen M. Cameron 	 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
7077eb6b2ae9SStephen M. Cameron 	 * as we enter this code.)
7078eb6b2ae9SStephen M. Cameron 	 */
7079007e7aa9SRobert Elliott 	for (i = 0; i < MAX_MODE_CHANGE_WAIT; i++) {
708025163bd5SWebb Scales 		if (h->remove_in_progress)
708125163bd5SWebb Scales 			goto done;
70826eaf46fdSStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
70836eaf46fdSStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
70846eaf46fdSStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
7085382be668SDan Carpenter 		if (!(doorbell_value & CFGTBL_ChangeReq))
7086c706a795SRobert Elliott 			goto done;
7087eb6b2ae9SStephen M. Cameron 		/* delay and try again */
7088007e7aa9SRobert Elliott 		msleep(MODE_CHANGE_WAIT_INTERVAL);
7089eb6b2ae9SStephen M. Cameron 	}
7090c706a795SRobert Elliott 	return -ENODEV;
7091c706a795SRobert Elliott done:
7092c706a795SRobert Elliott 	return 0;
70933f4336f3SStephen M. Cameron }
70943f4336f3SStephen M. Cameron 
7095c706a795SRobert Elliott /* return -ENODEV or other reason on error, 0 on success */
70966f039790SGreg Kroah-Hartman static int hpsa_enter_simple_mode(struct ctlr_info *h)
70973f4336f3SStephen M. Cameron {
70983f4336f3SStephen M. Cameron 	u32 trans_support;
70993f4336f3SStephen M. Cameron 
71003f4336f3SStephen M. Cameron 	trans_support = readl(&(h->cfgtable->TransportSupport));
71013f4336f3SStephen M. Cameron 	if (!(trans_support & SIMPLE_MODE))
71023f4336f3SStephen M. Cameron 		return -ENOTSUPP;
71033f4336f3SStephen M. Cameron 
71043f4336f3SStephen M. Cameron 	h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
7105283b4a9bSStephen M. Cameron 
71063f4336f3SStephen M. Cameron 	/* Update the field, and then ring the doorbell */
71073f4336f3SStephen M. Cameron 	writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
7108b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
71093f4336f3SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
7110c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h))
7111c706a795SRobert Elliott 		goto error;
7112eb6b2ae9SStephen M. Cameron 	print_cfg_table(&h->pdev->dev, h->cfgtable);
7113283b4a9bSStephen M. Cameron 	if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
7114283b4a9bSStephen M. Cameron 		goto error;
7115960a30e7SStephen M. Cameron 	h->transMethod = CFGTBL_Trans_Simple;
7116eb6b2ae9SStephen M. Cameron 	return 0;
7117283b4a9bSStephen M. Cameron error:
7118050f7147SStephen Cameron 	dev_err(&h->pdev->dev, "failed to enter simple mode\n");
7119283b4a9bSStephen M. Cameron 	return -ENODEV;
7120eb6b2ae9SStephen M. Cameron }
7121eb6b2ae9SStephen M. Cameron 
7122195f2c65SRobert Elliott /* free items allocated or mapped by hpsa_pci_init */
7123195f2c65SRobert Elliott static void hpsa_free_pci_init(struct ctlr_info *h)
7124195f2c65SRobert Elliott {
7125195f2c65SRobert Elliott 	hpsa_free_cfgtables(h);			/* pci_init 4 */
7126195f2c65SRobert Elliott 	iounmap(h->vaddr);			/* pci_init 3 */
7127105a3dbcSRobert Elliott 	h->vaddr = NULL;
7128195f2c65SRobert Elliott 	hpsa_disable_interrupt_mode(h);		/* pci_init 2 */
7129943a7021SRobert Elliott 	/*
7130943a7021SRobert Elliott 	 * call pci_disable_device before pci_release_regions per
7131943a7021SRobert Elliott 	 * Documentation/PCI/pci.txt
7132943a7021SRobert Elliott 	 */
7133195f2c65SRobert Elliott 	pci_disable_device(h->pdev);		/* pci_init 1 */
7134943a7021SRobert Elliott 	pci_release_regions(h->pdev);		/* pci_init 2 */
7135195f2c65SRobert Elliott }
7136195f2c65SRobert Elliott 
7137195f2c65SRobert Elliott /* several items must be freed later */
71386f039790SGreg Kroah-Hartman static int hpsa_pci_init(struct ctlr_info *h)
713977c4495cSStephen M. Cameron {
7140eb6b2ae9SStephen M. Cameron 	int prod_index, err;
7141edd16368SStephen M. Cameron 
7142e5c880d1SStephen M. Cameron 	prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
7143e5c880d1SStephen M. Cameron 	if (prod_index < 0)
714460f923b9SRobert Elliott 		return prod_index;
7145e5c880d1SStephen M. Cameron 	h->product_name = products[prod_index].product_name;
7146e5c880d1SStephen M. Cameron 	h->access = *(products[prod_index].access);
7147e5c880d1SStephen M. Cameron 
71489b5c48c2SStephen Cameron 	h->needs_abort_tags_swizzled =
71499b5c48c2SStephen Cameron 		ctlr_needs_abort_tags_swizzled(h->board_id);
71509b5c48c2SStephen Cameron 
7151e5a44df8SMatthew Garrett 	pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
7152e5a44df8SMatthew Garrett 			       PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
7153e5a44df8SMatthew Garrett 
715455c06c71SStephen M. Cameron 	err = pci_enable_device(h->pdev);
7155edd16368SStephen M. Cameron 	if (err) {
7156195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "failed to enable PCI device\n");
7157943a7021SRobert Elliott 		pci_disable_device(h->pdev);
7158edd16368SStephen M. Cameron 		return err;
7159edd16368SStephen M. Cameron 	}
7160edd16368SStephen M. Cameron 
7161f79cfec6SStephen M. Cameron 	err = pci_request_regions(h->pdev, HPSA);
7162edd16368SStephen M. Cameron 	if (err) {
716355c06c71SStephen M. Cameron 		dev_err(&h->pdev->dev,
7164195f2c65SRobert Elliott 			"failed to obtain PCI resources\n");
7165943a7021SRobert Elliott 		pci_disable_device(h->pdev);
7166943a7021SRobert Elliott 		return err;
7167edd16368SStephen M. Cameron 	}
71684fa604e1SRobert Elliott 
71694fa604e1SRobert Elliott 	pci_set_master(h->pdev);
71704fa604e1SRobert Elliott 
71716b3f4c52SStephen M. Cameron 	hpsa_interrupt_mode(h);
717212d2cd47SStephen M. Cameron 	err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
71733a7774ceSStephen M. Cameron 	if (err)
7174195f2c65SRobert Elliott 		goto clean2;	/* intmode+region, pci */
7175edd16368SStephen M. Cameron 	h->vaddr = remap_pci_mem(h->paddr, 0x250);
7176204892e9SStephen M. Cameron 	if (!h->vaddr) {
7177195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "failed to remap PCI mem\n");
7178204892e9SStephen M. Cameron 		err = -ENOMEM;
7179195f2c65SRobert Elliott 		goto clean2;	/* intmode+region, pci */
7180204892e9SStephen M. Cameron 	}
7181fe5389c8SStephen M. Cameron 	err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
71822c4c8c8bSStephen M. Cameron 	if (err)
7183195f2c65SRobert Elliott 		goto clean3;	/* vaddr, intmode+region, pci */
718477c4495cSStephen M. Cameron 	err = hpsa_find_cfgtables(h);
718577c4495cSStephen M. Cameron 	if (err)
7186195f2c65SRobert Elliott 		goto clean3;	/* vaddr, intmode+region, pci */
7187b93d7536SStephen M. Cameron 	hpsa_find_board_params(h);
7188edd16368SStephen M. Cameron 
718976c46e49SStephen M. Cameron 	if (!hpsa_CISS_signature_present(h)) {
7190edd16368SStephen M. Cameron 		err = -ENODEV;
7191195f2c65SRobert Elliott 		goto clean4;	/* cfgtables, vaddr, intmode+region, pci */
7192edd16368SStephen M. Cameron 	}
719397a5e98cSStephen M. Cameron 	hpsa_set_driver_support_bits(h);
71943d0eab67SStephen M. Cameron 	hpsa_p600_dma_prefetch_quirk(h);
7195eb6b2ae9SStephen M. Cameron 	err = hpsa_enter_simple_mode(h);
7196eb6b2ae9SStephen M. Cameron 	if (err)
7197195f2c65SRobert Elliott 		goto clean4;	/* cfgtables, vaddr, intmode+region, pci */
7198edd16368SStephen M. Cameron 	return 0;
7199edd16368SStephen M. Cameron 
7200195f2c65SRobert Elliott clean4:	/* cfgtables, vaddr, intmode+region, pci */
7201195f2c65SRobert Elliott 	hpsa_free_cfgtables(h);
7202195f2c65SRobert Elliott clean3:	/* vaddr, intmode+region, pci */
7203204892e9SStephen M. Cameron 	iounmap(h->vaddr);
7204105a3dbcSRobert Elliott 	h->vaddr = NULL;
7205195f2c65SRobert Elliott clean2:	/* intmode+region, pci */
7206195f2c65SRobert Elliott 	hpsa_disable_interrupt_mode(h);
7207943a7021SRobert Elliott 	/*
7208943a7021SRobert Elliott 	 * call pci_disable_device before pci_release_regions per
7209943a7021SRobert Elliott 	 * Documentation/PCI/pci.txt
7210943a7021SRobert Elliott 	 */
7211195f2c65SRobert Elliott 	pci_disable_device(h->pdev);
7212943a7021SRobert Elliott 	pci_release_regions(h->pdev);
7213edd16368SStephen M. Cameron 	return err;
7214edd16368SStephen M. Cameron }
7215edd16368SStephen M. Cameron 
72166f039790SGreg Kroah-Hartman static void hpsa_hba_inquiry(struct ctlr_info *h)
7217339b2b14SStephen M. Cameron {
7218339b2b14SStephen M. Cameron 	int rc;
7219339b2b14SStephen M. Cameron 
7220339b2b14SStephen M. Cameron #define HBA_INQUIRY_BYTE_COUNT 64
7221339b2b14SStephen M. Cameron 	h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
7222339b2b14SStephen M. Cameron 	if (!h->hba_inquiry_data)
7223339b2b14SStephen M. Cameron 		return;
7224339b2b14SStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
7225339b2b14SStephen M. Cameron 		h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
7226339b2b14SStephen M. Cameron 	if (rc != 0) {
7227339b2b14SStephen M. Cameron 		kfree(h->hba_inquiry_data);
7228339b2b14SStephen M. Cameron 		h->hba_inquiry_data = NULL;
7229339b2b14SStephen M. Cameron 	}
7230339b2b14SStephen M. Cameron }
7231339b2b14SStephen M. Cameron 
72326b6c1cd7STomas Henzl static int hpsa_init_reset_devices(struct pci_dev *pdev, u32 board_id)
7233edd16368SStephen M. Cameron {
72341df8552aSStephen M. Cameron 	int rc, i;
72353b747298STomas Henzl 	void __iomem *vaddr;
7236edd16368SStephen M. Cameron 
72374c2a8c40SStephen M. Cameron 	if (!reset_devices)
72384c2a8c40SStephen M. Cameron 		return 0;
72394c2a8c40SStephen M. Cameron 
7240132aa220STomas Henzl 	/* kdump kernel is loading, we don't know in which state is
7241132aa220STomas Henzl 	 * the pci interface. The dev->enable_cnt is equal zero
7242132aa220STomas Henzl 	 * so we call enable+disable, wait a while and switch it on.
7243132aa220STomas Henzl 	 */
7244132aa220STomas Henzl 	rc = pci_enable_device(pdev);
7245132aa220STomas Henzl 	if (rc) {
7246132aa220STomas Henzl 		dev_warn(&pdev->dev, "Failed to enable PCI device\n");
7247132aa220STomas Henzl 		return -ENODEV;
7248132aa220STomas Henzl 	}
7249132aa220STomas Henzl 	pci_disable_device(pdev);
7250132aa220STomas Henzl 	msleep(260);			/* a randomly chosen number */
7251132aa220STomas Henzl 	rc = pci_enable_device(pdev);
7252132aa220STomas Henzl 	if (rc) {
7253132aa220STomas Henzl 		dev_warn(&pdev->dev, "failed to enable device.\n");
7254132aa220STomas Henzl 		return -ENODEV;
7255132aa220STomas Henzl 	}
72564fa604e1SRobert Elliott 
7257859c75abSTomas Henzl 	pci_set_master(pdev);
72584fa604e1SRobert Elliott 
72593b747298STomas Henzl 	vaddr = pci_ioremap_bar(pdev, 0);
72603b747298STomas Henzl 	if (vaddr == NULL) {
72613b747298STomas Henzl 		rc = -ENOMEM;
72623b747298STomas Henzl 		goto out_disable;
72633b747298STomas Henzl 	}
72643b747298STomas Henzl 	writel(SA5_INTR_OFF, vaddr + SA5_REPLY_INTR_MASK_OFFSET);
72653b747298STomas Henzl 	iounmap(vaddr);
72663b747298STomas Henzl 
72671df8552aSStephen M. Cameron 	/* Reset the controller with a PCI power-cycle or via doorbell */
72686b6c1cd7STomas Henzl 	rc = hpsa_kdump_hard_reset_controller(pdev, board_id);
7269edd16368SStephen M. Cameron 
72701df8552aSStephen M. Cameron 	/* -ENOTSUPP here means we cannot reset the controller
72711df8552aSStephen M. Cameron 	 * but it's already (and still) up and running in
727218867659SStephen M. Cameron 	 * "performant mode".  Or, it might be 640x, which can't reset
727318867659SStephen M. Cameron 	 * due to concerns about shared bbwc between 6402/6404 pair.
72741df8552aSStephen M. Cameron 	 */
7275adf1b3a3SRobert Elliott 	if (rc)
7276132aa220STomas Henzl 		goto out_disable;
7277edd16368SStephen M. Cameron 
7278edd16368SStephen M. Cameron 	/* Now try to get the controller to respond to a no-op */
72791ba66c9cSRobert Elliott 	dev_info(&pdev->dev, "Waiting for controller to respond to no-op\n");
7280edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
7281edd16368SStephen M. Cameron 		if (hpsa_noop(pdev) == 0)
7282edd16368SStephen M. Cameron 			break;
7283edd16368SStephen M. Cameron 		else
7284edd16368SStephen M. Cameron 			dev_warn(&pdev->dev, "no-op failed%s\n",
7285edd16368SStephen M. Cameron 					(i < 11 ? "; re-trying" : ""));
7286edd16368SStephen M. Cameron 	}
7287132aa220STomas Henzl 
7288132aa220STomas Henzl out_disable:
7289132aa220STomas Henzl 
7290132aa220STomas Henzl 	pci_disable_device(pdev);
7291132aa220STomas Henzl 	return rc;
7292edd16368SStephen M. Cameron }
7293edd16368SStephen M. Cameron 
72941fb7c98aSRobert Elliott static void hpsa_free_cmd_pool(struct ctlr_info *h)
72951fb7c98aSRobert Elliott {
72961fb7c98aSRobert Elliott 	kfree(h->cmd_pool_bits);
7297105a3dbcSRobert Elliott 	h->cmd_pool_bits = NULL;
7298105a3dbcSRobert Elliott 	if (h->cmd_pool) {
72991fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
73001fb7c98aSRobert Elliott 				h->nr_cmds * sizeof(struct CommandList),
73011fb7c98aSRobert Elliott 				h->cmd_pool,
73021fb7c98aSRobert Elliott 				h->cmd_pool_dhandle);
7303105a3dbcSRobert Elliott 		h->cmd_pool = NULL;
7304105a3dbcSRobert Elliott 		h->cmd_pool_dhandle = 0;
7305105a3dbcSRobert Elliott 	}
7306105a3dbcSRobert Elliott 	if (h->errinfo_pool) {
73071fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
73081fb7c98aSRobert Elliott 				h->nr_cmds * sizeof(struct ErrorInfo),
73091fb7c98aSRobert Elliott 				h->errinfo_pool,
73101fb7c98aSRobert Elliott 				h->errinfo_pool_dhandle);
7311105a3dbcSRobert Elliott 		h->errinfo_pool = NULL;
7312105a3dbcSRobert Elliott 		h->errinfo_pool_dhandle = 0;
7313105a3dbcSRobert Elliott 	}
73141fb7c98aSRobert Elliott }
73151fb7c98aSRobert Elliott 
7316d37ffbe4SRobert Elliott static int hpsa_alloc_cmd_pool(struct ctlr_info *h)
73172e9d1b36SStephen M. Cameron {
73182e9d1b36SStephen M. Cameron 	h->cmd_pool_bits = kzalloc(
73192e9d1b36SStephen M. Cameron 		DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
73202e9d1b36SStephen M. Cameron 		sizeof(unsigned long), GFP_KERNEL);
73212e9d1b36SStephen M. Cameron 	h->cmd_pool = pci_alloc_consistent(h->pdev,
73222e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->cmd_pool),
73232e9d1b36SStephen M. Cameron 		    &(h->cmd_pool_dhandle));
73242e9d1b36SStephen M. Cameron 	h->errinfo_pool = pci_alloc_consistent(h->pdev,
73252e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->errinfo_pool),
73262e9d1b36SStephen M. Cameron 		    &(h->errinfo_pool_dhandle));
73272e9d1b36SStephen M. Cameron 	if ((h->cmd_pool_bits == NULL)
73282e9d1b36SStephen M. Cameron 	    || (h->cmd_pool == NULL)
73292e9d1b36SStephen M. Cameron 	    || (h->errinfo_pool == NULL)) {
73302e9d1b36SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory in %s", __func__);
73312c143342SRobert Elliott 		goto clean_up;
73322e9d1b36SStephen M. Cameron 	}
7333360c73bdSStephen Cameron 	hpsa_preinitialize_commands(h);
73342e9d1b36SStephen M. Cameron 	return 0;
73352c143342SRobert Elliott clean_up:
73362c143342SRobert Elliott 	hpsa_free_cmd_pool(h);
73372c143342SRobert Elliott 	return -ENOMEM;
73382e9d1b36SStephen M. Cameron }
73392e9d1b36SStephen M. Cameron 
734041b3cf08SStephen M. Cameron static void hpsa_irq_affinity_hints(struct ctlr_info *h)
734141b3cf08SStephen M. Cameron {
7342ec429952SFabian Frederick 	int i, cpu;
734341b3cf08SStephen M. Cameron 
734441b3cf08SStephen M. Cameron 	cpu = cpumask_first(cpu_online_mask);
734541b3cf08SStephen M. Cameron 	for (i = 0; i < h->msix_vector; i++) {
7346ec429952SFabian Frederick 		irq_set_affinity_hint(h->intr[i], get_cpu_mask(cpu));
734741b3cf08SStephen M. Cameron 		cpu = cpumask_next(cpu, cpu_online_mask);
734841b3cf08SStephen M. Cameron 	}
734941b3cf08SStephen M. Cameron }
735041b3cf08SStephen M. Cameron 
7351ec501a18SRobert Elliott /* clear affinity hints and free MSI-X, MSI, or legacy INTx vectors */
7352ec501a18SRobert Elliott static void hpsa_free_irqs(struct ctlr_info *h)
7353ec501a18SRobert Elliott {
7354ec501a18SRobert Elliott 	int i;
7355ec501a18SRobert Elliott 
7356ec501a18SRobert Elliott 	if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
7357ec501a18SRobert Elliott 		/* Single reply queue, only one irq to free */
7358ec501a18SRobert Elliott 		i = h->intr_mode;
7359ec501a18SRobert Elliott 		irq_set_affinity_hint(h->intr[i], NULL);
7360ec501a18SRobert Elliott 		free_irq(h->intr[i], &h->q[i]);
7361105a3dbcSRobert Elliott 		h->q[i] = 0;
7362ec501a18SRobert Elliott 		return;
7363ec501a18SRobert Elliott 	}
7364ec501a18SRobert Elliott 
7365ec501a18SRobert Elliott 	for (i = 0; i < h->msix_vector; i++) {
7366ec501a18SRobert Elliott 		irq_set_affinity_hint(h->intr[i], NULL);
7367ec501a18SRobert Elliott 		free_irq(h->intr[i], &h->q[i]);
7368105a3dbcSRobert Elliott 		h->q[i] = 0;
7369ec501a18SRobert Elliott 	}
7370a4e17fc1SRobert Elliott 	for (; i < MAX_REPLY_QUEUES; i++)
7371a4e17fc1SRobert Elliott 		h->q[i] = 0;
7372ec501a18SRobert Elliott }
7373ec501a18SRobert Elliott 
73749ee61794SRobert Elliott /* returns 0 on success; cleans up and returns -Enn on error */
73759ee61794SRobert Elliott static int hpsa_request_irqs(struct ctlr_info *h,
73760ae01a32SStephen M. Cameron 	irqreturn_t (*msixhandler)(int, void *),
73770ae01a32SStephen M. Cameron 	irqreturn_t (*intxhandler)(int, void *))
73780ae01a32SStephen M. Cameron {
7379254f796bSMatt Gates 	int rc, i;
73800ae01a32SStephen M. Cameron 
7381254f796bSMatt Gates 	/*
7382254f796bSMatt Gates 	 * initialize h->q[x] = x so that interrupt handlers know which
7383254f796bSMatt Gates 	 * queue to process.
7384254f796bSMatt Gates 	 */
7385254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++)
7386254f796bSMatt Gates 		h->q[i] = (u8) i;
7387254f796bSMatt Gates 
7388eee0f03aSHannes Reinecke 	if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
7389254f796bSMatt Gates 		/* If performant mode and MSI-X, use multiple reply queues */
7390a4e17fc1SRobert Elliott 		for (i = 0; i < h->msix_vector; i++) {
73918b47004aSRobert Elliott 			sprintf(h->intrname[i], "%s-msix%d", h->devname, i);
7392254f796bSMatt Gates 			rc = request_irq(h->intr[i], msixhandler,
73938b47004aSRobert Elliott 					0, h->intrname[i],
7394254f796bSMatt Gates 					&h->q[i]);
7395a4e17fc1SRobert Elliott 			if (rc) {
7396a4e17fc1SRobert Elliott 				int j;
7397a4e17fc1SRobert Elliott 
7398a4e17fc1SRobert Elliott 				dev_err(&h->pdev->dev,
7399a4e17fc1SRobert Elliott 					"failed to get irq %d for %s\n",
7400a4e17fc1SRobert Elliott 				       h->intr[i], h->devname);
7401a4e17fc1SRobert Elliott 				for (j = 0; j < i; j++) {
7402a4e17fc1SRobert Elliott 					free_irq(h->intr[j], &h->q[j]);
7403a4e17fc1SRobert Elliott 					h->q[j] = 0;
7404a4e17fc1SRobert Elliott 				}
7405a4e17fc1SRobert Elliott 				for (; j < MAX_REPLY_QUEUES; j++)
7406a4e17fc1SRobert Elliott 					h->q[j] = 0;
7407a4e17fc1SRobert Elliott 				return rc;
7408a4e17fc1SRobert Elliott 			}
7409a4e17fc1SRobert Elliott 		}
741041b3cf08SStephen M. Cameron 		hpsa_irq_affinity_hints(h);
7411254f796bSMatt Gates 	} else {
7412254f796bSMatt Gates 		/* Use single reply pool */
7413eee0f03aSHannes Reinecke 		if (h->msix_vector > 0 || h->msi_vector) {
74148b47004aSRobert Elliott 			if (h->msix_vector)
74158b47004aSRobert Elliott 				sprintf(h->intrname[h->intr_mode],
74168b47004aSRobert Elliott 					"%s-msix", h->devname);
74178b47004aSRobert Elliott 			else
74188b47004aSRobert Elliott 				sprintf(h->intrname[h->intr_mode],
74198b47004aSRobert Elliott 					"%s-msi", h->devname);
7420254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
74218b47004aSRobert Elliott 				msixhandler, 0,
74228b47004aSRobert Elliott 				h->intrname[h->intr_mode],
7423254f796bSMatt Gates 				&h->q[h->intr_mode]);
7424254f796bSMatt Gates 		} else {
74258b47004aSRobert Elliott 			sprintf(h->intrname[h->intr_mode],
74268b47004aSRobert Elliott 				"%s-intx", h->devname);
7427254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
74288b47004aSRobert Elliott 				intxhandler, IRQF_SHARED,
74298b47004aSRobert Elliott 				h->intrname[h->intr_mode],
7430254f796bSMatt Gates 				&h->q[h->intr_mode]);
7431254f796bSMatt Gates 		}
7432105a3dbcSRobert Elliott 		irq_set_affinity_hint(h->intr[h->intr_mode], NULL);
7433254f796bSMatt Gates 	}
74340ae01a32SStephen M. Cameron 	if (rc) {
7435195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "failed to get irq %d for %s\n",
74360ae01a32SStephen M. Cameron 		       h->intr[h->intr_mode], h->devname);
7437195f2c65SRobert Elliott 		hpsa_free_irqs(h);
74380ae01a32SStephen M. Cameron 		return -ENODEV;
74390ae01a32SStephen M. Cameron 	}
74400ae01a32SStephen M. Cameron 	return 0;
74410ae01a32SStephen M. Cameron }
74420ae01a32SStephen M. Cameron 
74436f039790SGreg Kroah-Hartman static int hpsa_kdump_soft_reset(struct ctlr_info *h)
744464670ac8SStephen M. Cameron {
7445bf43caf3SRobert Elliott 	hpsa_send_host_reset(h, RAID_CTLR_LUNID, HPSA_RESET_TYPE_CONTROLLER);
744664670ac8SStephen M. Cameron 
744764670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
744864670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) {
744964670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
745064670ac8SStephen M. Cameron 		return -1;
745164670ac8SStephen M. Cameron 	}
745264670ac8SStephen M. Cameron 
745364670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
745464670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) {
745564670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Board failed to become ready "
745664670ac8SStephen M. Cameron 			"after soft reset.\n");
745764670ac8SStephen M. Cameron 		return -1;
745864670ac8SStephen M. Cameron 	}
745964670ac8SStephen M. Cameron 
746064670ac8SStephen M. Cameron 	return 0;
746164670ac8SStephen M. Cameron }
746264670ac8SStephen M. Cameron 
7463072b0518SStephen M. Cameron static void hpsa_free_reply_queues(struct ctlr_info *h)
7464072b0518SStephen M. Cameron {
7465072b0518SStephen M. Cameron 	int i;
7466072b0518SStephen M. Cameron 
7467072b0518SStephen M. Cameron 	for (i = 0; i < h->nreply_queues; i++) {
7468072b0518SStephen M. Cameron 		if (!h->reply_queue[i].head)
7469072b0518SStephen M. Cameron 			continue;
74701fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
74711fb7c98aSRobert Elliott 					h->reply_queue_size,
74721fb7c98aSRobert Elliott 					h->reply_queue[i].head,
74731fb7c98aSRobert Elliott 					h->reply_queue[i].busaddr);
7474072b0518SStephen M. Cameron 		h->reply_queue[i].head = NULL;
7475072b0518SStephen M. Cameron 		h->reply_queue[i].busaddr = 0;
7476072b0518SStephen M. Cameron 	}
7477105a3dbcSRobert Elliott 	h->reply_queue_size = 0;
7478072b0518SStephen M. Cameron }
7479072b0518SStephen M. Cameron 
74800097f0f4SStephen M. Cameron static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
74810097f0f4SStephen M. Cameron {
7482105a3dbcSRobert Elliott 	hpsa_free_performant_mode(h);		/* init_one 7 */
7483105a3dbcSRobert Elliott 	hpsa_free_sg_chain_blocks(h);		/* init_one 6 */
7484105a3dbcSRobert Elliott 	hpsa_free_cmd_pool(h);			/* init_one 5 */
7485105a3dbcSRobert Elliott 	hpsa_free_irqs(h);			/* init_one 4 */
7486105a3dbcSRobert Elliott 	hpsa_free_pci_init(h);			/* init_one 3 */
74879ecd953aSRobert Elliott 	free_percpu(h->lockup_detected);	/* init_one 2 */
74889ecd953aSRobert Elliott 	h->lockup_detected = NULL;		/* init_one 2 */
74899ecd953aSRobert Elliott 	if (h->resubmit_wq) {
74909ecd953aSRobert Elliott 		destroy_workqueue(h->resubmit_wq);	/* init_one 1 */
74919ecd953aSRobert Elliott 		h->resubmit_wq = NULL;
74929ecd953aSRobert Elliott 	}
74939ecd953aSRobert Elliott 	if (h->rescan_ctlr_wq) {
74949ecd953aSRobert Elliott 		destroy_workqueue(h->rescan_ctlr_wq);
74959ecd953aSRobert Elliott 		h->rescan_ctlr_wq = NULL;
74969ecd953aSRobert Elliott 	}
7497105a3dbcSRobert Elliott 	kfree(h);				/* init_one 1 */
749864670ac8SStephen M. Cameron }
749964670ac8SStephen M. Cameron 
7500a0c12413SStephen M. Cameron /* Called when controller lockup detected. */
7501f2405db8SDon Brace static void fail_all_outstanding_cmds(struct ctlr_info *h)
7502a0c12413SStephen M. Cameron {
7503281a7fd0SWebb Scales 	int i, refcount;
7504281a7fd0SWebb Scales 	struct CommandList *c;
750525163bd5SWebb Scales 	int failcount = 0;
7506a0c12413SStephen M. Cameron 
7507080ef1ccSDon Brace 	flush_workqueue(h->resubmit_wq); /* ensure all cmds are fully built */
7508f2405db8SDon Brace 	for (i = 0; i < h->nr_cmds; i++) {
7509f2405db8SDon Brace 		c = h->cmd_pool + i;
7510281a7fd0SWebb Scales 		refcount = atomic_inc_return(&c->refcount);
7511281a7fd0SWebb Scales 		if (refcount > 1) {
751225163bd5SWebb Scales 			c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
75135a3d16f5SStephen M. Cameron 			finish_cmd(c);
7514433b5f4dSStephen Cameron 			atomic_dec(&h->commands_outstanding);
751525163bd5SWebb Scales 			failcount++;
7516a0c12413SStephen M. Cameron 		}
7517281a7fd0SWebb Scales 		cmd_free(h, c);
7518281a7fd0SWebb Scales 	}
751925163bd5SWebb Scales 	dev_warn(&h->pdev->dev,
752025163bd5SWebb Scales 		"failed %d commands in fail_all\n", failcount);
7521a0c12413SStephen M. Cameron }
7522a0c12413SStephen M. Cameron 
7523094963daSStephen M. Cameron static void set_lockup_detected_for_all_cpus(struct ctlr_info *h, u32 value)
7524094963daSStephen M. Cameron {
7525c8ed0010SRusty Russell 	int cpu;
7526094963daSStephen M. Cameron 
7527c8ed0010SRusty Russell 	for_each_online_cpu(cpu) {
7528094963daSStephen M. Cameron 		u32 *lockup_detected;
7529094963daSStephen M. Cameron 		lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
7530094963daSStephen M. Cameron 		*lockup_detected = value;
7531094963daSStephen M. Cameron 	}
7532094963daSStephen M. Cameron 	wmb(); /* be sure the per-cpu variables are out to memory */
7533094963daSStephen M. Cameron }
7534094963daSStephen M. Cameron 
7535a0c12413SStephen M. Cameron static void controller_lockup_detected(struct ctlr_info *h)
7536a0c12413SStephen M. Cameron {
7537a0c12413SStephen M. Cameron 	unsigned long flags;
7538094963daSStephen M. Cameron 	u32 lockup_detected;
7539a0c12413SStephen M. Cameron 
7540a0c12413SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
7541a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
7542094963daSStephen M. Cameron 	lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
7543094963daSStephen M. Cameron 	if (!lockup_detected) {
7544094963daSStephen M. Cameron 		/* no heartbeat, but controller gave us a zero. */
7545094963daSStephen M. Cameron 		dev_warn(&h->pdev->dev,
754625163bd5SWebb Scales 			"lockup detected after %d but scratchpad register is zero\n",
754725163bd5SWebb Scales 			h->heartbeat_sample_interval / HZ);
7548094963daSStephen M. Cameron 		lockup_detected = 0xffffffff;
7549094963daSStephen M. Cameron 	}
7550094963daSStephen M. Cameron 	set_lockup_detected_for_all_cpus(h, lockup_detected);
7551a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
755225163bd5SWebb Scales 	dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x after %d\n",
755325163bd5SWebb Scales 			lockup_detected, h->heartbeat_sample_interval / HZ);
7554a0c12413SStephen M. Cameron 	pci_disable_device(h->pdev);
7555f2405db8SDon Brace 	fail_all_outstanding_cmds(h);
7556a0c12413SStephen M. Cameron }
7557a0c12413SStephen M. Cameron 
755825163bd5SWebb Scales static int detect_controller_lockup(struct ctlr_info *h)
7559a0c12413SStephen M. Cameron {
7560a0c12413SStephen M. Cameron 	u64 now;
7561a0c12413SStephen M. Cameron 	u32 heartbeat;
7562a0c12413SStephen M. Cameron 	unsigned long flags;
7563a0c12413SStephen M. Cameron 
7564a0c12413SStephen M. Cameron 	now = get_jiffies_64();
7565a0c12413SStephen M. Cameron 	/* If we've received an interrupt recently, we're ok. */
7566a0c12413SStephen M. Cameron 	if (time_after64(h->last_intr_timestamp +
7567e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
756825163bd5SWebb Scales 		return false;
7569a0c12413SStephen M. Cameron 
7570a0c12413SStephen M. Cameron 	/*
7571a0c12413SStephen M. Cameron 	 * If we've already checked the heartbeat recently, we're ok.
7572a0c12413SStephen M. Cameron 	 * This could happen if someone sends us a signal. We
7573a0c12413SStephen M. Cameron 	 * otherwise don't care about signals in this thread.
7574a0c12413SStephen M. Cameron 	 */
7575a0c12413SStephen M. Cameron 	if (time_after64(h->last_heartbeat_timestamp +
7576e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
757725163bd5SWebb Scales 		return false;
7578a0c12413SStephen M. Cameron 
7579a0c12413SStephen M. Cameron 	/* If heartbeat has not changed since we last looked, we're not ok. */
7580a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
7581a0c12413SStephen M. Cameron 	heartbeat = readl(&h->cfgtable->HeartBeat);
7582a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
7583a0c12413SStephen M. Cameron 	if (h->last_heartbeat == heartbeat) {
7584a0c12413SStephen M. Cameron 		controller_lockup_detected(h);
758525163bd5SWebb Scales 		return true;
7586a0c12413SStephen M. Cameron 	}
7587a0c12413SStephen M. Cameron 
7588a0c12413SStephen M. Cameron 	/* We're ok. */
7589a0c12413SStephen M. Cameron 	h->last_heartbeat = heartbeat;
7590a0c12413SStephen M. Cameron 	h->last_heartbeat_timestamp = now;
759125163bd5SWebb Scales 	return false;
7592a0c12413SStephen M. Cameron }
7593a0c12413SStephen M. Cameron 
75949846590eSStephen M. Cameron static void hpsa_ack_ctlr_events(struct ctlr_info *h)
759576438d08SStephen M. Cameron {
759676438d08SStephen M. Cameron 	int i;
759776438d08SStephen M. Cameron 	char *event_type;
759876438d08SStephen M. Cameron 
7599e4aa3e6aSStephen Cameron 	if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
7600e4aa3e6aSStephen Cameron 		return;
7601e4aa3e6aSStephen Cameron 
760276438d08SStephen M. Cameron 	/* Ask the controller to clear the events we're handling. */
76031f7cee8cSStephen M. Cameron 	if ((h->transMethod & (CFGTBL_Trans_io_accel1
76041f7cee8cSStephen M. Cameron 			| CFGTBL_Trans_io_accel2)) &&
760576438d08SStephen M. Cameron 		(h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
760676438d08SStephen M. Cameron 		 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
760776438d08SStephen M. Cameron 
760876438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
760976438d08SStephen M. Cameron 			event_type = "state change";
761076438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
761176438d08SStephen M. Cameron 			event_type = "configuration change";
761276438d08SStephen M. Cameron 		/* Stop sending new RAID offload reqs via the IO accelerator */
761376438d08SStephen M. Cameron 		scsi_block_requests(h->scsi_host);
761476438d08SStephen M. Cameron 		for (i = 0; i < h->ndevices; i++)
761576438d08SStephen M. Cameron 			h->dev[i]->offload_enabled = 0;
761623100dd9SStephen M. Cameron 		hpsa_drain_accel_commands(h);
761776438d08SStephen M. Cameron 		/* Set 'accelerator path config change' bit */
761876438d08SStephen M. Cameron 		dev_warn(&h->pdev->dev,
761976438d08SStephen M. Cameron 			"Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
762076438d08SStephen M. Cameron 			h->events, event_type);
762176438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
762276438d08SStephen M. Cameron 		/* Set the "clear event notify field update" bit 6 */
762376438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
762476438d08SStephen M. Cameron 		/* Wait until ctlr clears 'clear event notify field', bit 6 */
762576438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
762676438d08SStephen M. Cameron 		scsi_unblock_requests(h->scsi_host);
762776438d08SStephen M. Cameron 	} else {
762876438d08SStephen M. Cameron 		/* Acknowledge controller notification events. */
762976438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
763076438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
763176438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
763276438d08SStephen M. Cameron #if 0
763376438d08SStephen M. Cameron 		writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
763476438d08SStephen M. Cameron 		hpsa_wait_for_mode_change_ack(h);
763576438d08SStephen M. Cameron #endif
763676438d08SStephen M. Cameron 	}
76379846590eSStephen M. Cameron 	return;
763876438d08SStephen M. Cameron }
763976438d08SStephen M. Cameron 
764076438d08SStephen M. Cameron /* Check a register on the controller to see if there are configuration
764176438d08SStephen M. Cameron  * changes (added/changed/removed logical drives, etc.) which mean that
7642e863d68eSScott Teel  * we should rescan the controller for devices.
7643e863d68eSScott Teel  * Also check flag for driver-initiated rescan.
764476438d08SStephen M. Cameron  */
76459846590eSStephen M. Cameron static int hpsa_ctlr_needs_rescan(struct ctlr_info *h)
764676438d08SStephen M. Cameron {
764776438d08SStephen M. Cameron 	if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
76489846590eSStephen M. Cameron 		return 0;
764976438d08SStephen M. Cameron 
765076438d08SStephen M. Cameron 	h->events = readl(&(h->cfgtable->event_notify));
76519846590eSStephen M. Cameron 	return h->events & RESCAN_REQUIRED_EVENT_BITS;
76529846590eSStephen M. Cameron }
765376438d08SStephen M. Cameron 
765476438d08SStephen M. Cameron /*
76559846590eSStephen M. Cameron  * Check if any of the offline devices have become ready
765676438d08SStephen M. Cameron  */
76579846590eSStephen M. Cameron static int hpsa_offline_devices_ready(struct ctlr_info *h)
76589846590eSStephen M. Cameron {
76599846590eSStephen M. Cameron 	unsigned long flags;
76609846590eSStephen M. Cameron 	struct offline_device_entry *d;
76619846590eSStephen M. Cameron 	struct list_head *this, *tmp;
76629846590eSStephen M. Cameron 
76639846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
76649846590eSStephen M. Cameron 	list_for_each_safe(this, tmp, &h->offline_device_list) {
76659846590eSStephen M. Cameron 		d = list_entry(this, struct offline_device_entry,
76669846590eSStephen M. Cameron 				offline_list);
76679846590eSStephen M. Cameron 		spin_unlock_irqrestore(&h->offline_device_lock, flags);
7668d1fea47cSStephen M. Cameron 		if (!hpsa_volume_offline(h, d->scsi3addr)) {
7669d1fea47cSStephen M. Cameron 			spin_lock_irqsave(&h->offline_device_lock, flags);
7670d1fea47cSStephen M. Cameron 			list_del(&d->offline_list);
7671d1fea47cSStephen M. Cameron 			spin_unlock_irqrestore(&h->offline_device_lock, flags);
76729846590eSStephen M. Cameron 			return 1;
7673d1fea47cSStephen M. Cameron 		}
76749846590eSStephen M. Cameron 		spin_lock_irqsave(&h->offline_device_lock, flags);
767576438d08SStephen M. Cameron 	}
76769846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
76779846590eSStephen M. Cameron 	return 0;
76789846590eSStephen M. Cameron }
76799846590eSStephen M. Cameron 
76806636e7f4SDon Brace static void hpsa_rescan_ctlr_worker(struct work_struct *work)
7681a0c12413SStephen M. Cameron {
7682a0c12413SStephen M. Cameron 	unsigned long flags;
76838a98db73SStephen M. Cameron 	struct ctlr_info *h = container_of(to_delayed_work(work),
76846636e7f4SDon Brace 					struct ctlr_info, rescan_ctlr_work);
76856636e7f4SDon Brace 
76866636e7f4SDon Brace 
76876636e7f4SDon Brace 	if (h->remove_in_progress)
76888a98db73SStephen M. Cameron 		return;
76899846590eSStephen M. Cameron 
76909846590eSStephen M. Cameron 	if (hpsa_ctlr_needs_rescan(h) || hpsa_offline_devices_ready(h)) {
76919846590eSStephen M. Cameron 		scsi_host_get(h->scsi_host);
76929846590eSStephen M. Cameron 		hpsa_ack_ctlr_events(h);
76939846590eSStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
76949846590eSStephen M. Cameron 		scsi_host_put(h->scsi_host);
76959846590eSStephen M. Cameron 	}
76966636e7f4SDon Brace 	spin_lock_irqsave(&h->lock, flags);
76976636e7f4SDon Brace 	if (!h->remove_in_progress)
76986636e7f4SDon Brace 		queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
76996636e7f4SDon Brace 				h->heartbeat_sample_interval);
77006636e7f4SDon Brace 	spin_unlock_irqrestore(&h->lock, flags);
77016636e7f4SDon Brace }
77026636e7f4SDon Brace 
77036636e7f4SDon Brace static void hpsa_monitor_ctlr_worker(struct work_struct *work)
77046636e7f4SDon Brace {
77056636e7f4SDon Brace 	unsigned long flags;
77066636e7f4SDon Brace 	struct ctlr_info *h = container_of(to_delayed_work(work),
77076636e7f4SDon Brace 					struct ctlr_info, monitor_ctlr_work);
77086636e7f4SDon Brace 
77096636e7f4SDon Brace 	detect_controller_lockup(h);
77106636e7f4SDon Brace 	if (lockup_detected(h))
77116636e7f4SDon Brace 		return;
77129846590eSStephen M. Cameron 
77138a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
77146636e7f4SDon Brace 	if (!h->remove_in_progress)
77158a98db73SStephen M. Cameron 		schedule_delayed_work(&h->monitor_ctlr_work,
77168a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
77178a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
7718a0c12413SStephen M. Cameron }
7719a0c12413SStephen M. Cameron 
77206636e7f4SDon Brace static struct workqueue_struct *hpsa_create_controller_wq(struct ctlr_info *h,
77216636e7f4SDon Brace 						char *name)
77226636e7f4SDon Brace {
77236636e7f4SDon Brace 	struct workqueue_struct *wq = NULL;
77246636e7f4SDon Brace 
7725397ea9cbSDon Brace 	wq = alloc_ordered_workqueue("%s_%d_hpsa", 0, name, h->ctlr);
77266636e7f4SDon Brace 	if (!wq)
77276636e7f4SDon Brace 		dev_err(&h->pdev->dev, "failed to create %s workqueue\n", name);
77286636e7f4SDon Brace 
77296636e7f4SDon Brace 	return wq;
77306636e7f4SDon Brace }
77316636e7f4SDon Brace 
77326f039790SGreg Kroah-Hartman static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
77334c2a8c40SStephen M. Cameron {
77344c2a8c40SStephen M. Cameron 	int dac, rc;
77354c2a8c40SStephen M. Cameron 	struct ctlr_info *h;
773664670ac8SStephen M. Cameron 	int try_soft_reset = 0;
773764670ac8SStephen M. Cameron 	unsigned long flags;
77386b6c1cd7STomas Henzl 	u32 board_id;
77394c2a8c40SStephen M. Cameron 
77404c2a8c40SStephen M. Cameron 	if (number_of_controllers == 0)
77414c2a8c40SStephen M. Cameron 		printk(KERN_INFO DRIVER_NAME "\n");
77424c2a8c40SStephen M. Cameron 
77436b6c1cd7STomas Henzl 	rc = hpsa_lookup_board_id(pdev, &board_id);
77446b6c1cd7STomas Henzl 	if (rc < 0) {
77456b6c1cd7STomas Henzl 		dev_warn(&pdev->dev, "Board ID not found\n");
77466b6c1cd7STomas Henzl 		return rc;
77476b6c1cd7STomas Henzl 	}
77486b6c1cd7STomas Henzl 
77496b6c1cd7STomas Henzl 	rc = hpsa_init_reset_devices(pdev, board_id);
775064670ac8SStephen M. Cameron 	if (rc) {
775164670ac8SStephen M. Cameron 		if (rc != -ENOTSUPP)
77524c2a8c40SStephen M. Cameron 			return rc;
775364670ac8SStephen M. Cameron 		/* If the reset fails in a particular way (it has no way to do
775464670ac8SStephen M. Cameron 		 * a proper hard reset, so returns -ENOTSUPP) we can try to do
775564670ac8SStephen M. Cameron 		 * a soft reset once we get the controller configured up to the
775664670ac8SStephen M. Cameron 		 * point that it can accept a command.
775764670ac8SStephen M. Cameron 		 */
775864670ac8SStephen M. Cameron 		try_soft_reset = 1;
775964670ac8SStephen M. Cameron 		rc = 0;
776064670ac8SStephen M. Cameron 	}
776164670ac8SStephen M. Cameron 
776264670ac8SStephen M. Cameron reinit_after_soft_reset:
77634c2a8c40SStephen M. Cameron 
7764303932fdSDon Brace 	/* Command structures must be aligned on a 32-byte boundary because
7765303932fdSDon Brace 	 * the 5 lower bits of the address are used by the hardware. and by
7766303932fdSDon Brace 	 * the driver.  See comments in hpsa.h for more info.
7767303932fdSDon Brace 	 */
7768303932fdSDon Brace 	BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
7769edd16368SStephen M. Cameron 	h = kzalloc(sizeof(*h), GFP_KERNEL);
7770105a3dbcSRobert Elliott 	if (!h) {
7771105a3dbcSRobert Elliott 		dev_err(&pdev->dev, "Failed to allocate controller head\n");
7772ecd9aad4SStephen M. Cameron 		return -ENOMEM;
7773105a3dbcSRobert Elliott 	}
7774edd16368SStephen M. Cameron 
777555c06c71SStephen M. Cameron 	h->pdev = pdev;
7776105a3dbcSRobert Elliott 
7777a9a3a273SStephen M. Cameron 	h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
77789846590eSStephen M. Cameron 	INIT_LIST_HEAD(&h->offline_device_list);
77796eaf46fdSStephen M. Cameron 	spin_lock_init(&h->lock);
77809846590eSStephen M. Cameron 	spin_lock_init(&h->offline_device_lock);
77816eaf46fdSStephen M. Cameron 	spin_lock_init(&h->scan_lock);
778234f0c627SDon Brace 	atomic_set(&h->passthru_cmds_avail, HPSA_MAX_CONCURRENT_PASSTHRUS);
77839b5c48c2SStephen Cameron 	atomic_set(&h->abort_cmds_available, HPSA_CMDS_RESERVED_FOR_ABORTS);
7784094963daSStephen M. Cameron 
7785094963daSStephen M. Cameron 	/* Allocate and clear per-cpu variable lockup_detected */
7786094963daSStephen M. Cameron 	h->lockup_detected = alloc_percpu(u32);
77872a5ac326SStephen M. Cameron 	if (!h->lockup_detected) {
7788105a3dbcSRobert Elliott 		dev_err(&h->pdev->dev, "Failed to allocate lockup detector\n");
77892a5ac326SStephen M. Cameron 		rc = -ENOMEM;
77902efa5929SRobert Elliott 		goto clean1;	/* aer/h */
77912a5ac326SStephen M. Cameron 	}
7792094963daSStephen M. Cameron 	set_lockup_detected_for_all_cpus(h, 0);
7793094963daSStephen M. Cameron 
779455c06c71SStephen M. Cameron 	rc = hpsa_pci_init(h);
7795105a3dbcSRobert Elliott 	if (rc)
77962efa5929SRobert Elliott 		goto clean2;	/* lockup, aer/h */
7797edd16368SStephen M. Cameron 
7798f79cfec6SStephen M. Cameron 	sprintf(h->devname, HPSA "%d", number_of_controllers);
7799edd16368SStephen M. Cameron 	h->ctlr = number_of_controllers;
7800edd16368SStephen M. Cameron 	number_of_controllers++;
7801edd16368SStephen M. Cameron 
7802edd16368SStephen M. Cameron 	/* configure PCI DMA stuff */
7803ecd9aad4SStephen M. Cameron 	rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
7804ecd9aad4SStephen M. Cameron 	if (rc == 0) {
7805edd16368SStephen M. Cameron 		dac = 1;
7806ecd9aad4SStephen M. Cameron 	} else {
7807ecd9aad4SStephen M. Cameron 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7808ecd9aad4SStephen M. Cameron 		if (rc == 0) {
7809edd16368SStephen M. Cameron 			dac = 0;
7810ecd9aad4SStephen M. Cameron 		} else {
7811edd16368SStephen M. Cameron 			dev_err(&pdev->dev, "no suitable DMA available\n");
78122efa5929SRobert Elliott 			goto clean3;	/* pci, lockup, aer/h */
7813edd16368SStephen M. Cameron 		}
7814ecd9aad4SStephen M. Cameron 	}
7815edd16368SStephen M. Cameron 
7816edd16368SStephen M. Cameron 	/* make sure the board interrupts are off */
7817edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
781810f66018SStephen M. Cameron 
7819105a3dbcSRobert Elliott 	rc = hpsa_request_irqs(h, do_hpsa_intr_msi, do_hpsa_intr_intx);
7820105a3dbcSRobert Elliott 	if (rc)
78212efa5929SRobert Elliott 		goto clean3;	/* pci, lockup, aer/h */
7822d37ffbe4SRobert Elliott 	rc = hpsa_alloc_cmd_pool(h);
78238947fd10SRobert Elliott 	if (rc)
78242efa5929SRobert Elliott 		goto clean4;	/* irq, pci, lockup, aer/h */
7825105a3dbcSRobert Elliott 	rc = hpsa_alloc_sg_chain_blocks(h);
7826105a3dbcSRobert Elliott 	if (rc)
78272efa5929SRobert Elliott 		goto clean5;	/* cmd, irq, pci, lockup, aer/h */
7828a08a8471SStephen M. Cameron 	init_waitqueue_head(&h->scan_wait_queue);
78299b5c48c2SStephen Cameron 	init_waitqueue_head(&h->abort_cmd_wait_queue);
7830a58e7e53SWebb Scales 	init_waitqueue_head(&h->abort_sync_wait_queue);
7831a08a8471SStephen M. Cameron 	h->scan_finished = 1; /* no scan currently in progress */
7832edd16368SStephen M. Cameron 
7833edd16368SStephen M. Cameron 	pci_set_drvdata(pdev, h);
78349a41338eSStephen M. Cameron 	h->ndevices = 0;
7835316b221aSStephen M. Cameron 	h->hba_mode_enabled = 0;
78369a41338eSStephen M. Cameron 	h->scsi_host = NULL;
78379a41338eSStephen M. Cameron 	spin_lock_init(&h->devlock);
7838105a3dbcSRobert Elliott 	rc = hpsa_put_ctlr_into_performant_mode(h);
7839105a3dbcSRobert Elliott 	if (rc)
78402efa5929SRobert Elliott 		goto clean6;	/* sg, cmd, irq, pci, lockup, aer/h */
78412efa5929SRobert Elliott 
78422efa5929SRobert Elliott 	/* create the resubmit workqueue */
78432efa5929SRobert Elliott 	h->rescan_ctlr_wq = hpsa_create_controller_wq(h, "rescan");
78442efa5929SRobert Elliott 	if (!h->rescan_ctlr_wq) {
78452efa5929SRobert Elliott 		rc = -ENOMEM;
78462efa5929SRobert Elliott 		goto clean7;
78472efa5929SRobert Elliott 	}
78482efa5929SRobert Elliott 
78492efa5929SRobert Elliott 	h->resubmit_wq = hpsa_create_controller_wq(h, "resubmit");
78502efa5929SRobert Elliott 	if (!h->resubmit_wq) {
78512efa5929SRobert Elliott 		rc = -ENOMEM;
78522efa5929SRobert Elliott 		goto clean7;	/* aer/h */
78532efa5929SRobert Elliott 	}
785464670ac8SStephen M. Cameron 
7855105a3dbcSRobert Elliott 	/*
7856105a3dbcSRobert Elliott 	 * At this point, the controller is ready to take commands.
785764670ac8SStephen M. Cameron 	 * Now, if reset_devices and the hard reset didn't work, try
785864670ac8SStephen M. Cameron 	 * the soft reset and see if that works.
785964670ac8SStephen M. Cameron 	 */
786064670ac8SStephen M. Cameron 	if (try_soft_reset) {
786164670ac8SStephen M. Cameron 
786264670ac8SStephen M. Cameron 		/* This is kind of gross.  We may or may not get a completion
786364670ac8SStephen M. Cameron 		 * from the soft reset command, and if we do, then the value
786464670ac8SStephen M. Cameron 		 * from the fifo may or may not be valid.  So, we wait 10 secs
786564670ac8SStephen M. Cameron 		 * after the reset throwing away any completions we get during
786664670ac8SStephen M. Cameron 		 * that time.  Unregister the interrupt handler and register
786764670ac8SStephen M. Cameron 		 * fake ones to scoop up any residual completions.
786864670ac8SStephen M. Cameron 		 */
786964670ac8SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
787064670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
787164670ac8SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
7872ec501a18SRobert Elliott 		hpsa_free_irqs(h);
78739ee61794SRobert Elliott 		rc = hpsa_request_irqs(h, hpsa_msix_discard_completions,
787464670ac8SStephen M. Cameron 					hpsa_intx_discard_completions);
787564670ac8SStephen M. Cameron 		if (rc) {
78769ee61794SRobert Elliott 			dev_warn(&h->pdev->dev,
78779ee61794SRobert Elliott 				"Failed to request_irq after soft reset.\n");
7878d498757cSRobert Elliott 			/*
7879b2ef480cSRobert Elliott 			 * cannot goto clean7 or free_irqs will be called
7880b2ef480cSRobert Elliott 			 * again. Instead, do its work
7881b2ef480cSRobert Elliott 			 */
7882b2ef480cSRobert Elliott 			hpsa_free_performant_mode(h);	/* clean7 */
7883b2ef480cSRobert Elliott 			hpsa_free_sg_chain_blocks(h);	/* clean6 */
7884b2ef480cSRobert Elliott 			hpsa_free_cmd_pool(h);		/* clean5 */
7885b2ef480cSRobert Elliott 			/*
7886b2ef480cSRobert Elliott 			 * skip hpsa_free_irqs(h) clean4 since that
7887b2ef480cSRobert Elliott 			 * was just called before request_irqs failed
7888d498757cSRobert Elliott 			 */
7889d498757cSRobert Elliott 			goto clean3;
789064670ac8SStephen M. Cameron 		}
789164670ac8SStephen M. Cameron 
789264670ac8SStephen M. Cameron 		rc = hpsa_kdump_soft_reset(h);
789364670ac8SStephen M. Cameron 		if (rc)
789464670ac8SStephen M. Cameron 			/* Neither hard nor soft reset worked, we're hosed. */
78952efa5929SRobert Elliott 			goto clean8;
789664670ac8SStephen M. Cameron 
789764670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev, "Board READY.\n");
789864670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev,
789964670ac8SStephen M. Cameron 			"Waiting for stale completions to drain.\n");
790064670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_ON);
790164670ac8SStephen M. Cameron 		msleep(10000);
790264670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
790364670ac8SStephen M. Cameron 
790464670ac8SStephen M. Cameron 		rc = controller_reset_failed(h->cfgtable);
790564670ac8SStephen M. Cameron 		if (rc)
790664670ac8SStephen M. Cameron 			dev_info(&h->pdev->dev,
790764670ac8SStephen M. Cameron 				"Soft reset appears to have failed.\n");
790864670ac8SStephen M. Cameron 
790964670ac8SStephen M. Cameron 		/* since the controller's reset, we have to go back and re-init
791064670ac8SStephen M. Cameron 		 * everything.  Easiest to just forget what we've done and do it
791164670ac8SStephen M. Cameron 		 * all over again.
791264670ac8SStephen M. Cameron 		 */
791364670ac8SStephen M. Cameron 		hpsa_undo_allocations_after_kdump_soft_reset(h);
791464670ac8SStephen M. Cameron 		try_soft_reset = 0;
791564670ac8SStephen M. Cameron 		if (rc)
7916b2ef480cSRobert Elliott 			/* don't goto clean, we already unallocated */
791764670ac8SStephen M. Cameron 			return -ENODEV;
791864670ac8SStephen M. Cameron 
791964670ac8SStephen M. Cameron 		goto reinit_after_soft_reset;
792064670ac8SStephen M. Cameron 	}
7921edd16368SStephen M. Cameron 
7922da0697bdSScott Teel 	/* Enable Accelerated IO path at driver layer */
7923da0697bdSScott Teel 	h->acciopath_status = 1;
7924da0697bdSScott Teel 
7925e863d68eSScott Teel 
7926edd16368SStephen M. Cameron 	/* Turn the interrupts on so we can service requests */
7927edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_ON);
7928edd16368SStephen M. Cameron 
7929339b2b14SStephen M. Cameron 	hpsa_hba_inquiry(h);
79304a4384ceSStephen Cameron 	rc = hpsa_register_scsi(h);	/* hook ourselves into SCSI subsystem */
79314a4384ceSStephen Cameron 	if (rc)
79322efa5929SRobert Elliott 		goto clean8; /* wq, perf, sg, cmd, irq, pci, lockup, aer/h */
79338a98db73SStephen M. Cameron 
79348a98db73SStephen M. Cameron 	/* Monitor the controller for firmware lockups */
79358a98db73SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
79368a98db73SStephen M. Cameron 	INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
79378a98db73SStephen M. Cameron 	schedule_delayed_work(&h->monitor_ctlr_work,
79388a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
79396636e7f4SDon Brace 	INIT_DELAYED_WORK(&h->rescan_ctlr_work, hpsa_rescan_ctlr_worker);
79406636e7f4SDon Brace 	queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
79416636e7f4SDon Brace 				h->heartbeat_sample_interval);
794288bf6d62SStephen M. Cameron 	return 0;
7943edd16368SStephen M. Cameron 
79442efa5929SRobert Elliott clean8: /* perf, sg, cmd, irq, pci, lockup, aer/h */
7945105a3dbcSRobert Elliott 	kfree(h->hba_inquiry_data);
79462efa5929SRobert Elliott clean7: /* perf, sg, cmd, irq, pci, lockup, aer/h */
7947105a3dbcSRobert Elliott 	hpsa_free_performant_mode(h);
7948105a3dbcSRobert Elliott 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
7949105a3dbcSRobert Elliott clean6: /* sg, cmd, irq, pci, lockup, wq/aer/h */
795033a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
79512efa5929SRobert Elliott clean5: /* cmd, irq, pci, lockup, aer/h */
79522e9d1b36SStephen M. Cameron 	hpsa_free_cmd_pool(h);
79532efa5929SRobert Elliott clean4: /* irq, pci, lockup, aer/h */
7954ec501a18SRobert Elliott 	hpsa_free_irqs(h);
79552efa5929SRobert Elliott clean3: /* pci, lockup, aer/h */
7956195f2c65SRobert Elliott 	hpsa_free_pci_init(h);
79572efa5929SRobert Elliott clean2: /* lockup, aer/h */
7958105a3dbcSRobert Elliott 	if (h->lockup_detected) {
7959094963daSStephen M. Cameron 		free_percpu(h->lockup_detected);
7960105a3dbcSRobert Elliott 		h->lockup_detected = NULL;
7961105a3dbcSRobert Elliott 	}
7962105a3dbcSRobert Elliott clean1:	/* wq/aer/h */
7963105a3dbcSRobert Elliott 	if (h->resubmit_wq) {
7964105a3dbcSRobert Elliott 		destroy_workqueue(h->resubmit_wq);
7965105a3dbcSRobert Elliott 		h->resubmit_wq = NULL;
7966105a3dbcSRobert Elliott 	}
7967105a3dbcSRobert Elliott 	if (h->rescan_ctlr_wq) {
7968105a3dbcSRobert Elliott 		destroy_workqueue(h->rescan_ctlr_wq);
7969105a3dbcSRobert Elliott 		h->rescan_ctlr_wq = NULL;
7970105a3dbcSRobert Elliott 	}
7971edd16368SStephen M. Cameron 	kfree(h);
7972ecd9aad4SStephen M. Cameron 	return rc;
7973edd16368SStephen M. Cameron }
7974edd16368SStephen M. Cameron 
7975edd16368SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h)
7976edd16368SStephen M. Cameron {
7977edd16368SStephen M. Cameron 	char *flush_buf;
7978edd16368SStephen M. Cameron 	struct CommandList *c;
797925163bd5SWebb Scales 	int rc;
7980702890e3SStephen M. Cameron 
7981094963daSStephen M. Cameron 	if (unlikely(lockup_detected(h)))
7982702890e3SStephen M. Cameron 		return;
7983edd16368SStephen M. Cameron 	flush_buf = kzalloc(4, GFP_KERNEL);
7984edd16368SStephen M. Cameron 	if (!flush_buf)
7985edd16368SStephen M. Cameron 		return;
7986edd16368SStephen M. Cameron 
798745fcb86eSStephen Cameron 	c = cmd_alloc(h);
7988bf43caf3SRobert Elliott 
7989a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
7990a2dac136SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_CMD)) {
7991a2dac136SStephen M. Cameron 		goto out;
7992a2dac136SStephen M. Cameron 	}
799325163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
799425163bd5SWebb Scales 					PCI_DMA_TODEVICE, NO_TIMEOUT);
799525163bd5SWebb Scales 	if (rc)
799625163bd5SWebb Scales 		goto out;
7997edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus != 0)
7998a2dac136SStephen M. Cameron out:
7999edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
8000edd16368SStephen M. Cameron 			"error flushing cache on controller\n");
800145fcb86eSStephen Cameron 	cmd_free(h, c);
8002edd16368SStephen M. Cameron 	kfree(flush_buf);
8003edd16368SStephen M. Cameron }
8004edd16368SStephen M. Cameron 
8005edd16368SStephen M. Cameron static void hpsa_shutdown(struct pci_dev *pdev)
8006edd16368SStephen M. Cameron {
8007edd16368SStephen M. Cameron 	struct ctlr_info *h;
8008edd16368SStephen M. Cameron 
8009edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
8010edd16368SStephen M. Cameron 	/* Turn board interrupts off  and send the flush cache command
8011edd16368SStephen M. Cameron 	 * sendcmd will turn off interrupt, and send the flush...
8012edd16368SStephen M. Cameron 	 * To write all data in the battery backed cache to disks
8013edd16368SStephen M. Cameron 	 */
8014edd16368SStephen M. Cameron 	hpsa_flush_cache(h);
8015edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
8016105a3dbcSRobert Elliott 	hpsa_free_irqs(h);			/* init_one 4 */
8017cc64c817SRobert Elliott 	hpsa_disable_interrupt_mode(h);		/* pci_init 2 */
8018edd16368SStephen M. Cameron }
8019edd16368SStephen M. Cameron 
80206f039790SGreg Kroah-Hartman static void hpsa_free_device_info(struct ctlr_info *h)
802155e14e76SStephen M. Cameron {
802255e14e76SStephen M. Cameron 	int i;
802355e14e76SStephen M. Cameron 
8024105a3dbcSRobert Elliott 	for (i = 0; i < h->ndevices; i++) {
802555e14e76SStephen M. Cameron 		kfree(h->dev[i]);
8026105a3dbcSRobert Elliott 		h->dev[i] = NULL;
8027105a3dbcSRobert Elliott 	}
802855e14e76SStephen M. Cameron }
802955e14e76SStephen M. Cameron 
80306f039790SGreg Kroah-Hartman static void hpsa_remove_one(struct pci_dev *pdev)
8031edd16368SStephen M. Cameron {
8032edd16368SStephen M. Cameron 	struct ctlr_info *h;
80338a98db73SStephen M. Cameron 	unsigned long flags;
8034edd16368SStephen M. Cameron 
8035edd16368SStephen M. Cameron 	if (pci_get_drvdata(pdev) == NULL) {
8036edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "unable to remove device\n");
8037edd16368SStephen M. Cameron 		return;
8038edd16368SStephen M. Cameron 	}
8039edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
80408a98db73SStephen M. Cameron 
80418a98db73SStephen M. Cameron 	/* Get rid of any controller monitoring work items */
80428a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
80438a98db73SStephen M. Cameron 	h->remove_in_progress = 1;
80448a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
80456636e7f4SDon Brace 	cancel_delayed_work_sync(&h->monitor_ctlr_work);
80466636e7f4SDon Brace 	cancel_delayed_work_sync(&h->rescan_ctlr_work);
80476636e7f4SDon Brace 	destroy_workqueue(h->rescan_ctlr_wq);
80486636e7f4SDon Brace 	destroy_workqueue(h->resubmit_wq);
8049cc64c817SRobert Elliott 
8050105a3dbcSRobert Elliott 	/* includes hpsa_free_irqs - init_one 4 */
8051195f2c65SRobert Elliott 	/* includes hpsa_disable_interrupt_mode - pci_init 2 */
8052edd16368SStephen M. Cameron 	hpsa_shutdown(pdev);
8053cc64c817SRobert Elliott 
8054105a3dbcSRobert Elliott 	hpsa_free_device_info(h);		/* scan */
8055105a3dbcSRobert Elliott 
80562efa5929SRobert Elliott 	hpsa_unregister_scsi(h);			/* init_one 9 */
80572efa5929SRobert Elliott 	kfree(h->hba_inquiry_data);			/* init_one 9 */
80582efa5929SRobert Elliott 	h->hba_inquiry_data = NULL;			/* init_one 9 */
8059105a3dbcSRobert Elliott 	hpsa_free_performant_mode(h);			/* init_one 7 */
8060105a3dbcSRobert Elliott 	hpsa_free_sg_chain_blocks(h);			/* init_one 6 */
80611fb7c98aSRobert Elliott 	hpsa_free_cmd_pool(h);				/* init_one 5 */
8062105a3dbcSRobert Elliott 
8063105a3dbcSRobert Elliott 	/* hpsa_free_irqs already called via hpsa_shutdown init_one 4 */
8064195f2c65SRobert Elliott 
8065195f2c65SRobert Elliott 	/* includes hpsa_disable_interrupt_mode - pci_init 2 */
8066105a3dbcSRobert Elliott 	hpsa_free_pci_init(h);				/* init_one 3 */
8067195f2c65SRobert Elliott 
8068105a3dbcSRobert Elliott 	free_percpu(h->lockup_detected);		/* init_one 2 */
8069105a3dbcSRobert Elliott 	h->lockup_detected = NULL;			/* init_one 2 */
8070105a3dbcSRobert Elliott 	/* (void) pci_disable_pcie_error_reporting(pdev); */	/* init_one 1 */
8071105a3dbcSRobert Elliott 	kfree(h);					/* init_one 1 */
8072edd16368SStephen M. Cameron }
8073edd16368SStephen M. Cameron 
8074edd16368SStephen M. Cameron static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
8075edd16368SStephen M. Cameron 	__attribute__((unused)) pm_message_t state)
8076edd16368SStephen M. Cameron {
8077edd16368SStephen M. Cameron 	return -ENOSYS;
8078edd16368SStephen M. Cameron }
8079edd16368SStephen M. Cameron 
8080edd16368SStephen M. Cameron static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
8081edd16368SStephen M. Cameron {
8082edd16368SStephen M. Cameron 	return -ENOSYS;
8083edd16368SStephen M. Cameron }
8084edd16368SStephen M. Cameron 
8085edd16368SStephen M. Cameron static struct pci_driver hpsa_pci_driver = {
8086f79cfec6SStephen M. Cameron 	.name = HPSA,
8087edd16368SStephen M. Cameron 	.probe = hpsa_init_one,
80886f039790SGreg Kroah-Hartman 	.remove = hpsa_remove_one,
8089edd16368SStephen M. Cameron 	.id_table = hpsa_pci_device_id,	/* id_table */
8090edd16368SStephen M. Cameron 	.shutdown = hpsa_shutdown,
8091edd16368SStephen M. Cameron 	.suspend = hpsa_suspend,
8092edd16368SStephen M. Cameron 	.resume = hpsa_resume,
8093edd16368SStephen M. Cameron };
8094edd16368SStephen M. Cameron 
8095303932fdSDon Brace /* Fill in bucket_map[], given nsgs (the max number of
8096303932fdSDon Brace  * scatter gather elements supported) and bucket[],
8097303932fdSDon Brace  * which is an array of 8 integers.  The bucket[] array
8098303932fdSDon Brace  * contains 8 different DMA transfer sizes (in 16
8099303932fdSDon Brace  * byte increments) which the controller uses to fetch
8100303932fdSDon Brace  * commands.  This function fills in bucket_map[], which
8101303932fdSDon Brace  * maps a given number of scatter gather elements to one of
8102303932fdSDon Brace  * the 8 DMA transfer sizes.  The point of it is to allow the
8103303932fdSDon Brace  * controller to only do as much DMA as needed to fetch the
8104303932fdSDon Brace  * command, with the DMA transfer size encoded in the lower
8105303932fdSDon Brace  * bits of the command address.
8106303932fdSDon Brace  */
8107303932fdSDon Brace static void  calc_bucket_map(int bucket[], int num_buckets,
81082b08b3e9SDon Brace 	int nsgs, int min_blocks, u32 *bucket_map)
8109303932fdSDon Brace {
8110303932fdSDon Brace 	int i, j, b, size;
8111303932fdSDon Brace 
8112303932fdSDon Brace 	/* Note, bucket_map must have nsgs+1 entries. */
8113303932fdSDon Brace 	for (i = 0; i <= nsgs; i++) {
8114303932fdSDon Brace 		/* Compute size of a command with i SG entries */
8115e1f7de0cSMatt Gates 		size = i + min_blocks;
8116303932fdSDon Brace 		b = num_buckets; /* Assume the biggest bucket */
8117303932fdSDon Brace 		/* Find the bucket that is just big enough */
8118e1f7de0cSMatt Gates 		for (j = 0; j < num_buckets; j++) {
8119303932fdSDon Brace 			if (bucket[j] >= size) {
8120303932fdSDon Brace 				b = j;
8121303932fdSDon Brace 				break;
8122303932fdSDon Brace 			}
8123303932fdSDon Brace 		}
8124303932fdSDon Brace 		/* for a command with i SG entries, use bucket b. */
8125303932fdSDon Brace 		bucket_map[i] = b;
8126303932fdSDon Brace 	}
8127303932fdSDon Brace }
8128303932fdSDon Brace 
8129105a3dbcSRobert Elliott /*
8130105a3dbcSRobert Elliott  * return -ENODEV on err, 0 on success (or no action)
8131105a3dbcSRobert Elliott  * allocates numerous items that must be freed later
8132105a3dbcSRobert Elliott  */
8133c706a795SRobert Elliott static int hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
8134303932fdSDon Brace {
81356c311b57SStephen M. Cameron 	int i;
81366c311b57SStephen M. Cameron 	unsigned long register_value;
8137e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
8138e1f7de0cSMatt Gates 			(trans_support & CFGTBL_Trans_use_short_tags) |
8139e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix |
8140b9af4937SStephen M. Cameron 			(trans_support & (CFGTBL_Trans_io_accel1 |
8141b9af4937SStephen M. Cameron 				CFGTBL_Trans_io_accel2));
8142e1f7de0cSMatt Gates 	struct access_method access = SA5_performant_access;
8143def342bdSStephen M. Cameron 
8144def342bdSStephen M. Cameron 	/* This is a bit complicated.  There are 8 registers on
8145def342bdSStephen M. Cameron 	 * the controller which we write to to tell it 8 different
8146def342bdSStephen M. Cameron 	 * sizes of commands which there may be.  It's a way of
8147def342bdSStephen M. Cameron 	 * reducing the DMA done to fetch each command.  Encoded into
8148def342bdSStephen M. Cameron 	 * each command's tag are 3 bits which communicate to the controller
8149def342bdSStephen M. Cameron 	 * which of the eight sizes that command fits within.  The size of
8150def342bdSStephen M. Cameron 	 * each command depends on how many scatter gather entries there are.
8151def342bdSStephen M. Cameron 	 * Each SG entry requires 16 bytes.  The eight registers are programmed
8152def342bdSStephen M. Cameron 	 * with the number of 16-byte blocks a command of that size requires.
8153def342bdSStephen M. Cameron 	 * The smallest command possible requires 5 such 16 byte blocks.
8154d66ae08bSStephen M. Cameron 	 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
8155def342bdSStephen M. Cameron 	 * blocks.  Note, this only extends to the SG entries contained
8156def342bdSStephen M. Cameron 	 * within the command block, and does not extend to chained blocks
8157def342bdSStephen M. Cameron 	 * of SG elements.   bft[] contains the eight values we write to
8158def342bdSStephen M. Cameron 	 * the registers.  They are not evenly distributed, but have more
8159def342bdSStephen M. Cameron 	 * sizes for small commands, and fewer sizes for larger commands.
8160def342bdSStephen M. Cameron 	 */
8161d66ae08bSStephen M. Cameron 	int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
8162b9af4937SStephen M. Cameron #define MIN_IOACCEL2_BFT_ENTRY 5
8163b9af4937SStephen M. Cameron #define HPSA_IOACCEL2_HEADER_SZ 4
8164b9af4937SStephen M. Cameron 	int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
8165b9af4937SStephen M. Cameron 			13, 14, 15, 16, 17, 18, 19,
8166b9af4937SStephen M. Cameron 			HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
8167b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
8168b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
8169b9af4937SStephen M. Cameron 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
8170b9af4937SStephen M. Cameron 				 16 * MIN_IOACCEL2_BFT_ENTRY);
8171b9af4937SStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
8172d66ae08bSStephen M. Cameron 	BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
8173303932fdSDon Brace 	/*  5 = 1 s/g entry or 4k
8174303932fdSDon Brace 	 *  6 = 2 s/g entry or 8k
8175303932fdSDon Brace 	 *  8 = 4 s/g entry or 16k
8176303932fdSDon Brace 	 * 10 = 6 s/g entry or 24k
8177303932fdSDon Brace 	 */
8178303932fdSDon Brace 
8179b3a52e79SStephen M. Cameron 	/* If the controller supports either ioaccel method then
8180b3a52e79SStephen M. Cameron 	 * we can also use the RAID stack submit path that does not
8181b3a52e79SStephen M. Cameron 	 * perform the superfluous readl() after each command submission.
8182b3a52e79SStephen M. Cameron 	 */
8183b3a52e79SStephen M. Cameron 	if (trans_support & (CFGTBL_Trans_io_accel1 | CFGTBL_Trans_io_accel2))
8184b3a52e79SStephen M. Cameron 		access = SA5_performant_access_no_read;
8185b3a52e79SStephen M. Cameron 
8186303932fdSDon Brace 	/* Controller spec: zero out this buffer. */
8187072b0518SStephen M. Cameron 	for (i = 0; i < h->nreply_queues; i++)
8188072b0518SStephen M. Cameron 		memset(h->reply_queue[i].head, 0, h->reply_queue_size);
8189303932fdSDon Brace 
8190d66ae08bSStephen M. Cameron 	bft[7] = SG_ENTRIES_IN_CMD + 4;
8191d66ae08bSStephen M. Cameron 	calc_bucket_map(bft, ARRAY_SIZE(bft),
8192e1f7de0cSMatt Gates 				SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
8193303932fdSDon Brace 	for (i = 0; i < 8; i++)
8194303932fdSDon Brace 		writel(bft[i], &h->transtable->BlockFetch[i]);
8195303932fdSDon Brace 
8196303932fdSDon Brace 	/* size of controller ring buffer */
8197303932fdSDon Brace 	writel(h->max_commands, &h->transtable->RepQSize);
8198254f796bSMatt Gates 	writel(h->nreply_queues, &h->transtable->RepQCount);
8199303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrLow32);
8200303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrHigh32);
8201254f796bSMatt Gates 
8202254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
8203254f796bSMatt Gates 		writel(0, &h->transtable->RepQAddr[i].upper);
8204072b0518SStephen M. Cameron 		writel(h->reply_queue[i].busaddr,
8205254f796bSMatt Gates 			&h->transtable->RepQAddr[i].lower);
8206254f796bSMatt Gates 	}
8207254f796bSMatt Gates 
8208b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
8209e1f7de0cSMatt Gates 	writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
8210e1f7de0cSMatt Gates 	/*
8211e1f7de0cSMatt Gates 	 * enable outbound interrupt coalescing in accelerator mode;
8212e1f7de0cSMatt Gates 	 */
8213e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
8214e1f7de0cSMatt Gates 		access = SA5_ioaccel_mode1_access;
8215e1f7de0cSMatt Gates 		writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
8216e1f7de0cSMatt Gates 		writel(4, &h->cfgtable->HostWrite.CoalIntCount);
8217c349775eSScott Teel 	} else {
8218c349775eSScott Teel 		if (trans_support & CFGTBL_Trans_io_accel2) {
8219c349775eSScott Teel 			access = SA5_ioaccel_mode2_access;
8220c349775eSScott Teel 			writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
8221c349775eSScott Teel 			writel(4, &h->cfgtable->HostWrite.CoalIntCount);
8222c349775eSScott Teel 		}
8223e1f7de0cSMatt Gates 	}
8224303932fdSDon Brace 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
8225c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h)) {
8226c706a795SRobert Elliott 		dev_err(&h->pdev->dev,
8227c706a795SRobert Elliott 			"performant mode problem - doorbell timeout\n");
8228c706a795SRobert Elliott 		return -ENODEV;
8229c706a795SRobert Elliott 	}
8230303932fdSDon Brace 	register_value = readl(&(h->cfgtable->TransportActive));
8231303932fdSDon Brace 	if (!(register_value & CFGTBL_Trans_Performant)) {
8232050f7147SStephen Cameron 		dev_err(&h->pdev->dev,
8233050f7147SStephen Cameron 			"performant mode problem - transport not active\n");
8234c706a795SRobert Elliott 		return -ENODEV;
8235303932fdSDon Brace 	}
8236960a30e7SStephen M. Cameron 	/* Change the access methods to the performant access methods */
8237e1f7de0cSMatt Gates 	h->access = access;
8238e1f7de0cSMatt Gates 	h->transMethod = transMethod;
8239e1f7de0cSMatt Gates 
8240b9af4937SStephen M. Cameron 	if (!((trans_support & CFGTBL_Trans_io_accel1) ||
8241b9af4937SStephen M. Cameron 		(trans_support & CFGTBL_Trans_io_accel2)))
8242c706a795SRobert Elliott 		return 0;
8243e1f7de0cSMatt Gates 
8244b9af4937SStephen M. Cameron 	if (trans_support & CFGTBL_Trans_io_accel1) {
8245e1f7de0cSMatt Gates 		/* Set up I/O accelerator mode */
8246e1f7de0cSMatt Gates 		for (i = 0; i < h->nreply_queues; i++) {
8247e1f7de0cSMatt Gates 			writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
8248e1f7de0cSMatt Gates 			h->reply_queue[i].current_entry =
8249e1f7de0cSMatt Gates 				readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
8250e1f7de0cSMatt Gates 		}
8251283b4a9bSStephen M. Cameron 		bft[7] = h->ioaccel_maxsg + 8;
8252283b4a9bSStephen M. Cameron 		calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
8253e1f7de0cSMatt Gates 				h->ioaccel1_blockFetchTable);
8254e1f7de0cSMatt Gates 
8255e1f7de0cSMatt Gates 		/* initialize all reply queue entries to unused */
8256072b0518SStephen M. Cameron 		for (i = 0; i < h->nreply_queues; i++)
8257072b0518SStephen M. Cameron 			memset(h->reply_queue[i].head,
8258072b0518SStephen M. Cameron 				(u8) IOACCEL_MODE1_REPLY_UNUSED,
8259072b0518SStephen M. Cameron 				h->reply_queue_size);
8260e1f7de0cSMatt Gates 
8261e1f7de0cSMatt Gates 		/* set all the constant fields in the accelerator command
8262e1f7de0cSMatt Gates 		 * frames once at init time to save CPU cycles later.
8263e1f7de0cSMatt Gates 		 */
8264e1f7de0cSMatt Gates 		for (i = 0; i < h->nr_cmds; i++) {
8265e1f7de0cSMatt Gates 			struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
8266e1f7de0cSMatt Gates 
8267e1f7de0cSMatt Gates 			cp->function = IOACCEL1_FUNCTION_SCSIIO;
8268e1f7de0cSMatt Gates 			cp->err_info = (u32) (h->errinfo_pool_dhandle +
8269e1f7de0cSMatt Gates 					(i * sizeof(struct ErrorInfo)));
8270e1f7de0cSMatt Gates 			cp->err_info_len = sizeof(struct ErrorInfo);
8271e1f7de0cSMatt Gates 			cp->sgl_offset = IOACCEL1_SGLOFFSET;
82722b08b3e9SDon Brace 			cp->host_context_flags =
82732b08b3e9SDon Brace 				cpu_to_le16(IOACCEL1_HCFLAGS_CISS_FORMAT);
8274e1f7de0cSMatt Gates 			cp->timeout_sec = 0;
8275e1f7de0cSMatt Gates 			cp->ReplyQueue = 0;
827650a0decfSStephen M. Cameron 			cp->tag =
8277f2405db8SDon Brace 				cpu_to_le64((i << DIRECT_LOOKUP_SHIFT));
827850a0decfSStephen M. Cameron 			cp->host_addr =
827950a0decfSStephen M. Cameron 				cpu_to_le64(h->ioaccel_cmd_pool_dhandle +
8280e1f7de0cSMatt Gates 					(i * sizeof(struct io_accel1_cmd)));
8281e1f7de0cSMatt Gates 		}
8282b9af4937SStephen M. Cameron 	} else if (trans_support & CFGTBL_Trans_io_accel2) {
8283b9af4937SStephen M. Cameron 		u64 cfg_offset, cfg_base_addr_index;
8284b9af4937SStephen M. Cameron 		u32 bft2_offset, cfg_base_addr;
8285b9af4937SStephen M. Cameron 		int rc;
8286b9af4937SStephen M. Cameron 
8287b9af4937SStephen M. Cameron 		rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
8288b9af4937SStephen M. Cameron 			&cfg_base_addr_index, &cfg_offset);
8289b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
8290b9af4937SStephen M. Cameron 		bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
8291b9af4937SStephen M. Cameron 		calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
8292b9af4937SStephen M. Cameron 				4, h->ioaccel2_blockFetchTable);
8293b9af4937SStephen M. Cameron 		bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
8294b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct CfgTable,
8295b9af4937SStephen M. Cameron 				io_accel_request_size_offset) != 0xb8);
8296b9af4937SStephen M. Cameron 		h->ioaccel2_bft2_regs =
8297b9af4937SStephen M. Cameron 			remap_pci_mem(pci_resource_start(h->pdev,
8298b9af4937SStephen M. Cameron 					cfg_base_addr_index) +
8299b9af4937SStephen M. Cameron 					cfg_offset + bft2_offset,
8300b9af4937SStephen M. Cameron 					ARRAY_SIZE(bft2) *
8301b9af4937SStephen M. Cameron 					sizeof(*h->ioaccel2_bft2_regs));
8302b9af4937SStephen M. Cameron 		for (i = 0; i < ARRAY_SIZE(bft2); i++)
8303b9af4937SStephen M. Cameron 			writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
8304b9af4937SStephen M. Cameron 	}
8305b9af4937SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
8306c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h)) {
8307c706a795SRobert Elliott 		dev_err(&h->pdev->dev,
8308c706a795SRobert Elliott 			"performant mode problem - enabling ioaccel mode\n");
8309c706a795SRobert Elliott 		return -ENODEV;
8310c706a795SRobert Elliott 	}
8311c706a795SRobert Elliott 	return 0;
8312e1f7de0cSMatt Gates }
8313e1f7de0cSMatt Gates 
83141fb7c98aSRobert Elliott /* Free ioaccel1 mode command blocks and block fetch table */
83151fb7c98aSRobert Elliott static void hpsa_free_ioaccel1_cmd_and_bft(struct ctlr_info *h)
83161fb7c98aSRobert Elliott {
8317105a3dbcSRobert Elliott 	if (h->ioaccel_cmd_pool) {
83181fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
83191fb7c98aSRobert Elliott 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
83201fb7c98aSRobert Elliott 			h->ioaccel_cmd_pool,
83211fb7c98aSRobert Elliott 			h->ioaccel_cmd_pool_dhandle);
8322105a3dbcSRobert Elliott 		h->ioaccel_cmd_pool = NULL;
8323105a3dbcSRobert Elliott 		h->ioaccel_cmd_pool_dhandle = 0;
8324105a3dbcSRobert Elliott 	}
83251fb7c98aSRobert Elliott 	kfree(h->ioaccel1_blockFetchTable);
8326105a3dbcSRobert Elliott 	h->ioaccel1_blockFetchTable = NULL;
83271fb7c98aSRobert Elliott }
83281fb7c98aSRobert Elliott 
8329d37ffbe4SRobert Elliott /* Allocate ioaccel1 mode command blocks and block fetch table */
8330d37ffbe4SRobert Elliott static int hpsa_alloc_ioaccel1_cmd_and_bft(struct ctlr_info *h)
8331e1f7de0cSMatt Gates {
8332283b4a9bSStephen M. Cameron 	h->ioaccel_maxsg =
8333283b4a9bSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
8334283b4a9bSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
8335283b4a9bSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
8336283b4a9bSStephen M. Cameron 
8337e1f7de0cSMatt Gates 	/* Command structures must be aligned on a 128-byte boundary
8338e1f7de0cSMatt Gates 	 * because the 7 lower bits of the address are used by the
8339e1f7de0cSMatt Gates 	 * hardware.
8340e1f7de0cSMatt Gates 	 */
8341e1f7de0cSMatt Gates 	BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
8342e1f7de0cSMatt Gates 			IOACCEL1_COMMANDLIST_ALIGNMENT);
8343e1f7de0cSMatt Gates 	h->ioaccel_cmd_pool =
8344e1f7de0cSMatt Gates 		pci_alloc_consistent(h->pdev,
8345e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
8346e1f7de0cSMatt Gates 			&(h->ioaccel_cmd_pool_dhandle));
8347e1f7de0cSMatt Gates 
8348e1f7de0cSMatt Gates 	h->ioaccel1_blockFetchTable =
8349283b4a9bSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
8350e1f7de0cSMatt Gates 				sizeof(u32)), GFP_KERNEL);
8351e1f7de0cSMatt Gates 
8352e1f7de0cSMatt Gates 	if ((h->ioaccel_cmd_pool == NULL) ||
8353e1f7de0cSMatt Gates 		(h->ioaccel1_blockFetchTable == NULL))
8354e1f7de0cSMatt Gates 		goto clean_up;
8355e1f7de0cSMatt Gates 
8356e1f7de0cSMatt Gates 	memset(h->ioaccel_cmd_pool, 0,
8357e1f7de0cSMatt Gates 		h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
8358e1f7de0cSMatt Gates 	return 0;
8359e1f7de0cSMatt Gates 
8360e1f7de0cSMatt Gates clean_up:
83611fb7c98aSRobert Elliott 	hpsa_free_ioaccel1_cmd_and_bft(h);
83622dd02d74SRobert Elliott 	return -ENOMEM;
83636c311b57SStephen M. Cameron }
83646c311b57SStephen M. Cameron 
83651fb7c98aSRobert Elliott /* Free ioaccel2 mode command blocks and block fetch table */
83661fb7c98aSRobert Elliott static void hpsa_free_ioaccel2_cmd_and_bft(struct ctlr_info *h)
83671fb7c98aSRobert Elliott {
8368d9a729f3SWebb Scales 	hpsa_free_ioaccel2_sg_chain_blocks(h);
8369d9a729f3SWebb Scales 
8370105a3dbcSRobert Elliott 	if (h->ioaccel2_cmd_pool) {
83711fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
83721fb7c98aSRobert Elliott 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
83731fb7c98aSRobert Elliott 			h->ioaccel2_cmd_pool,
83741fb7c98aSRobert Elliott 			h->ioaccel2_cmd_pool_dhandle);
8375105a3dbcSRobert Elliott 		h->ioaccel2_cmd_pool = NULL;
8376105a3dbcSRobert Elliott 		h->ioaccel2_cmd_pool_dhandle = 0;
8377105a3dbcSRobert Elliott 	}
83781fb7c98aSRobert Elliott 	kfree(h->ioaccel2_blockFetchTable);
8379105a3dbcSRobert Elliott 	h->ioaccel2_blockFetchTable = NULL;
83801fb7c98aSRobert Elliott }
83811fb7c98aSRobert Elliott 
8382d37ffbe4SRobert Elliott /* Allocate ioaccel2 mode command blocks and block fetch table */
8383d37ffbe4SRobert Elliott static int hpsa_alloc_ioaccel2_cmd_and_bft(struct ctlr_info *h)
8384aca9012aSStephen M. Cameron {
8385d9a729f3SWebb Scales 	int rc;
8386d9a729f3SWebb Scales 
8387aca9012aSStephen M. Cameron 	/* Allocate ioaccel2 mode command blocks and block fetch table */
8388aca9012aSStephen M. Cameron 
8389aca9012aSStephen M. Cameron 	h->ioaccel_maxsg =
8390aca9012aSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
8391aca9012aSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
8392aca9012aSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
8393aca9012aSStephen M. Cameron 
8394aca9012aSStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
8395aca9012aSStephen M. Cameron 			IOACCEL2_COMMANDLIST_ALIGNMENT);
8396aca9012aSStephen M. Cameron 	h->ioaccel2_cmd_pool =
8397aca9012aSStephen M. Cameron 		pci_alloc_consistent(h->pdev,
8398aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
8399aca9012aSStephen M. Cameron 			&(h->ioaccel2_cmd_pool_dhandle));
8400aca9012aSStephen M. Cameron 
8401aca9012aSStephen M. Cameron 	h->ioaccel2_blockFetchTable =
8402aca9012aSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
8403aca9012aSStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
8404aca9012aSStephen M. Cameron 
8405aca9012aSStephen M. Cameron 	if ((h->ioaccel2_cmd_pool == NULL) ||
8406d9a729f3SWebb Scales 		(h->ioaccel2_blockFetchTable == NULL)) {
8407d9a729f3SWebb Scales 		rc = -ENOMEM;
8408d9a729f3SWebb Scales 		goto clean_up;
8409d9a729f3SWebb Scales 	}
8410d9a729f3SWebb Scales 
8411d9a729f3SWebb Scales 	rc = hpsa_allocate_ioaccel2_sg_chain_blocks(h);
8412d9a729f3SWebb Scales 	if (rc)
8413aca9012aSStephen M. Cameron 		goto clean_up;
8414aca9012aSStephen M. Cameron 
8415aca9012aSStephen M. Cameron 	memset(h->ioaccel2_cmd_pool, 0,
8416aca9012aSStephen M. Cameron 		h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
8417aca9012aSStephen M. Cameron 	return 0;
8418aca9012aSStephen M. Cameron 
8419aca9012aSStephen M. Cameron clean_up:
84201fb7c98aSRobert Elliott 	hpsa_free_ioaccel2_cmd_and_bft(h);
8421d9a729f3SWebb Scales 	return rc;
8422aca9012aSStephen M. Cameron }
8423aca9012aSStephen M. Cameron 
8424105a3dbcSRobert Elliott /* Free items allocated by hpsa_put_ctlr_into_performant_mode */
8425105a3dbcSRobert Elliott static void hpsa_free_performant_mode(struct ctlr_info *h)
8426105a3dbcSRobert Elliott {
8427105a3dbcSRobert Elliott 	kfree(h->blockFetchTable);
8428105a3dbcSRobert Elliott 	h->blockFetchTable = NULL;
8429105a3dbcSRobert Elliott 	hpsa_free_reply_queues(h);
8430105a3dbcSRobert Elliott 	hpsa_free_ioaccel1_cmd_and_bft(h);
8431105a3dbcSRobert Elliott 	hpsa_free_ioaccel2_cmd_and_bft(h);
8432105a3dbcSRobert Elliott }
8433105a3dbcSRobert Elliott 
8434105a3dbcSRobert Elliott /* return -ENODEV on error, 0 on success (or no action)
8435105a3dbcSRobert Elliott  * allocates numerous items that must be freed later
8436105a3dbcSRobert Elliott  */
8437105a3dbcSRobert Elliott static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
84386c311b57SStephen M. Cameron {
84396c311b57SStephen M. Cameron 	u32 trans_support;
8440e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
8441e1f7de0cSMatt Gates 					CFGTBL_Trans_use_short_tags;
8442105a3dbcSRobert Elliott 	int i, rc;
84436c311b57SStephen M. Cameron 
844402ec19c8SStephen M. Cameron 	if (hpsa_simple_mode)
8445105a3dbcSRobert Elliott 		return 0;
844602ec19c8SStephen M. Cameron 
844767c99a72Sscameron@beardog.cce.hp.com 	trans_support = readl(&(h->cfgtable->TransportSupport));
844867c99a72Sscameron@beardog.cce.hp.com 	if (!(trans_support & PERFORMANT_MODE))
8449105a3dbcSRobert Elliott 		return 0;
845067c99a72Sscameron@beardog.cce.hp.com 
8451e1f7de0cSMatt Gates 	/* Check for I/O accelerator mode support */
8452e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
8453e1f7de0cSMatt Gates 		transMethod |= CFGTBL_Trans_io_accel1 |
8454e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix;
8455105a3dbcSRobert Elliott 		rc = hpsa_alloc_ioaccel1_cmd_and_bft(h);
8456105a3dbcSRobert Elliott 		if (rc)
8457105a3dbcSRobert Elliott 			return rc;
8458105a3dbcSRobert Elliott 	} else if (trans_support & CFGTBL_Trans_io_accel2) {
8459aca9012aSStephen M. Cameron 		transMethod |= CFGTBL_Trans_io_accel2 |
8460aca9012aSStephen M. Cameron 				CFGTBL_Trans_enable_directed_msix;
8461105a3dbcSRobert Elliott 		rc = hpsa_alloc_ioaccel2_cmd_and_bft(h);
8462105a3dbcSRobert Elliott 		if (rc)
8463105a3dbcSRobert Elliott 			return rc;
8464e1f7de0cSMatt Gates 	}
8465e1f7de0cSMatt Gates 
8466eee0f03aSHannes Reinecke 	h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
8467cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
84686c311b57SStephen M. Cameron 	/* Performant mode ring buffer and supporting data structures */
8469072b0518SStephen M. Cameron 	h->reply_queue_size = h->max_commands * sizeof(u64);
84706c311b57SStephen M. Cameron 
8471254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
8472072b0518SStephen M. Cameron 		h->reply_queue[i].head = pci_alloc_consistent(h->pdev,
8473072b0518SStephen M. Cameron 						h->reply_queue_size,
8474072b0518SStephen M. Cameron 						&(h->reply_queue[i].busaddr));
8475105a3dbcSRobert Elliott 		if (!h->reply_queue[i].head) {
8476105a3dbcSRobert Elliott 			rc = -ENOMEM;
8477105a3dbcSRobert Elliott 			goto clean1;	/* rq, ioaccel */
8478105a3dbcSRobert Elliott 		}
8479254f796bSMatt Gates 		h->reply_queue[i].size = h->max_commands;
8480254f796bSMatt Gates 		h->reply_queue[i].wraparound = 1;  /* spec: init to 1 */
8481254f796bSMatt Gates 		h->reply_queue[i].current_entry = 0;
8482254f796bSMatt Gates 	}
8483254f796bSMatt Gates 
84846c311b57SStephen M. Cameron 	/* Need a block fetch table for performant mode */
8485d66ae08bSStephen M. Cameron 	h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
84866c311b57SStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
8487105a3dbcSRobert Elliott 	if (!h->blockFetchTable) {
8488105a3dbcSRobert Elliott 		rc = -ENOMEM;
8489105a3dbcSRobert Elliott 		goto clean1;	/* rq, ioaccel */
8490105a3dbcSRobert Elliott 	}
84916c311b57SStephen M. Cameron 
8492105a3dbcSRobert Elliott 	rc = hpsa_enter_performant_mode(h, trans_support);
8493105a3dbcSRobert Elliott 	if (rc)
8494105a3dbcSRobert Elliott 		goto clean2;	/* bft, rq, ioaccel */
8495105a3dbcSRobert Elliott 	return 0;
8496303932fdSDon Brace 
8497105a3dbcSRobert Elliott clean2:	/* bft, rq, ioaccel */
8498303932fdSDon Brace 	kfree(h->blockFetchTable);
8499105a3dbcSRobert Elliott 	h->blockFetchTable = NULL;
8500105a3dbcSRobert Elliott clean1:	/* rq, ioaccel */
8501105a3dbcSRobert Elliott 	hpsa_free_reply_queues(h);
8502105a3dbcSRobert Elliott 	hpsa_free_ioaccel1_cmd_and_bft(h);
8503105a3dbcSRobert Elliott 	hpsa_free_ioaccel2_cmd_and_bft(h);
8504105a3dbcSRobert Elliott 	return rc;
8505303932fdSDon Brace }
8506303932fdSDon Brace 
850723100dd9SStephen M. Cameron static int is_accelerated_cmd(struct CommandList *c)
850876438d08SStephen M. Cameron {
850923100dd9SStephen M. Cameron 	return c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_IOACCEL2;
851023100dd9SStephen M. Cameron }
851123100dd9SStephen M. Cameron 
851223100dd9SStephen M. Cameron static void hpsa_drain_accel_commands(struct ctlr_info *h)
851323100dd9SStephen M. Cameron {
851423100dd9SStephen M. Cameron 	struct CommandList *c = NULL;
8515f2405db8SDon Brace 	int i, accel_cmds_out;
8516281a7fd0SWebb Scales 	int refcount;
851776438d08SStephen M. Cameron 
8518f2405db8SDon Brace 	do { /* wait for all outstanding ioaccel commands to drain out */
851923100dd9SStephen M. Cameron 		accel_cmds_out = 0;
8520f2405db8SDon Brace 		for (i = 0; i < h->nr_cmds; i++) {
8521f2405db8SDon Brace 			c = h->cmd_pool + i;
8522281a7fd0SWebb Scales 			refcount = atomic_inc_return(&c->refcount);
8523281a7fd0SWebb Scales 			if (refcount > 1) /* Command is allocated */
852423100dd9SStephen M. Cameron 				accel_cmds_out += is_accelerated_cmd(c);
8525281a7fd0SWebb Scales 			cmd_free(h, c);
8526f2405db8SDon Brace 		}
852723100dd9SStephen M. Cameron 		if (accel_cmds_out <= 0)
852876438d08SStephen M. Cameron 			break;
852976438d08SStephen M. Cameron 		msleep(100);
853076438d08SStephen M. Cameron 	} while (1);
853176438d08SStephen M. Cameron }
853276438d08SStephen M. Cameron 
8533edd16368SStephen M. Cameron /*
8534edd16368SStephen M. Cameron  *  This is it.  Register the PCI driver information for the cards we control
8535edd16368SStephen M. Cameron  *  the OS will call our registered routines when it finds one of our cards.
8536edd16368SStephen M. Cameron  */
8537edd16368SStephen M. Cameron static int __init hpsa_init(void)
8538edd16368SStephen M. Cameron {
853931468401SMike Miller 	return pci_register_driver(&hpsa_pci_driver);
8540edd16368SStephen M. Cameron }
8541edd16368SStephen M. Cameron 
8542edd16368SStephen M. Cameron static void __exit hpsa_cleanup(void)
8543edd16368SStephen M. Cameron {
8544edd16368SStephen M. Cameron 	pci_unregister_driver(&hpsa_pci_driver);
8545edd16368SStephen M. Cameron }
8546edd16368SStephen M. Cameron 
8547e1f7de0cSMatt Gates static void __attribute__((unused)) verify_offsets(void)
8548e1f7de0cSMatt Gates {
8549e1f7de0cSMatt Gates #define VERIFY_OFFSET(member, offset) \
8550dd0e19f3SScott Teel 	BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
8551dd0e19f3SScott Teel 
8552dd0e19f3SScott Teel 	VERIFY_OFFSET(structure_size, 0);
8553dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_size, 4);
8554dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_cnt, 8);
8555dd0e19f3SScott Teel 	VERIFY_OFFSET(phys_blk_shift, 16);
8556dd0e19f3SScott Teel 	VERIFY_OFFSET(parity_rotation_shift, 17);
8557dd0e19f3SScott Teel 	VERIFY_OFFSET(strip_size, 18);
8558dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_starting_blk, 20);
8559dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_blk_cnt, 28);
8560dd0e19f3SScott Teel 	VERIFY_OFFSET(data_disks_per_row, 36);
8561dd0e19f3SScott Teel 	VERIFY_OFFSET(metadata_disks_per_row, 38);
8562dd0e19f3SScott Teel 	VERIFY_OFFSET(row_cnt, 40);
8563dd0e19f3SScott Teel 	VERIFY_OFFSET(layout_map_count, 42);
8564dd0e19f3SScott Teel 	VERIFY_OFFSET(flags, 44);
8565dd0e19f3SScott Teel 	VERIFY_OFFSET(dekindex, 46);
8566dd0e19f3SScott Teel 	/* VERIFY_OFFSET(reserved, 48 */
8567dd0e19f3SScott Teel 	VERIFY_OFFSET(data, 64);
8568dd0e19f3SScott Teel 
8569dd0e19f3SScott Teel #undef VERIFY_OFFSET
8570dd0e19f3SScott Teel 
8571dd0e19f3SScott Teel #define VERIFY_OFFSET(member, offset) \
8572b66cc250SMike Miller 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
8573b66cc250SMike Miller 
8574b66cc250SMike Miller 	VERIFY_OFFSET(IU_type, 0);
8575b66cc250SMike Miller 	VERIFY_OFFSET(direction, 1);
8576b66cc250SMike Miller 	VERIFY_OFFSET(reply_queue, 2);
8577b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved1, 3);  */
8578b66cc250SMike Miller 	VERIFY_OFFSET(scsi_nexus, 4);
8579b66cc250SMike Miller 	VERIFY_OFFSET(Tag, 8);
8580b66cc250SMike Miller 	VERIFY_OFFSET(cdb, 16);
8581b66cc250SMike Miller 	VERIFY_OFFSET(cciss_lun, 32);
8582b66cc250SMike Miller 	VERIFY_OFFSET(data_len, 40);
8583b66cc250SMike Miller 	VERIFY_OFFSET(cmd_priority_task_attr, 44);
8584b66cc250SMike Miller 	VERIFY_OFFSET(sg_count, 45);
8585b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved3 */
8586b66cc250SMike Miller 	VERIFY_OFFSET(err_ptr, 48);
8587b66cc250SMike Miller 	VERIFY_OFFSET(err_len, 56);
8588b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved4  */
8589b66cc250SMike Miller 	VERIFY_OFFSET(sg, 64);
8590b66cc250SMike Miller 
8591b66cc250SMike Miller #undef VERIFY_OFFSET
8592b66cc250SMike Miller 
8593b66cc250SMike Miller #define VERIFY_OFFSET(member, offset) \
8594e1f7de0cSMatt Gates 	BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
8595e1f7de0cSMatt Gates 
8596e1f7de0cSMatt Gates 	VERIFY_OFFSET(dev_handle, 0x00);
8597e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved1, 0x02);
8598e1f7de0cSMatt Gates 	VERIFY_OFFSET(function, 0x03);
8599e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved2, 0x04);
8600e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info, 0x0C);
8601e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved3, 0x10);
8602e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info_len, 0x12);
8603e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved4, 0x13);
8604e1f7de0cSMatt Gates 	VERIFY_OFFSET(sgl_offset, 0x14);
8605e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved5, 0x15);
8606e1f7de0cSMatt Gates 	VERIFY_OFFSET(transfer_len, 0x1C);
8607e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved6, 0x20);
8608e1f7de0cSMatt Gates 	VERIFY_OFFSET(io_flags, 0x24);
8609e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved7, 0x26);
8610e1f7de0cSMatt Gates 	VERIFY_OFFSET(LUN, 0x34);
8611e1f7de0cSMatt Gates 	VERIFY_OFFSET(control, 0x3C);
8612e1f7de0cSMatt Gates 	VERIFY_OFFSET(CDB, 0x40);
8613e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved8, 0x50);
8614e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_context_flags, 0x60);
8615e1f7de0cSMatt Gates 	VERIFY_OFFSET(timeout_sec, 0x62);
8616e1f7de0cSMatt Gates 	VERIFY_OFFSET(ReplyQueue, 0x64);
8617e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved9, 0x65);
861850a0decfSStephen M. Cameron 	VERIFY_OFFSET(tag, 0x68);
8619e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_addr, 0x70);
8620e1f7de0cSMatt Gates 	VERIFY_OFFSET(CISS_LUN, 0x78);
8621e1f7de0cSMatt Gates 	VERIFY_OFFSET(SG, 0x78 + 8);
8622e1f7de0cSMatt Gates #undef VERIFY_OFFSET
8623e1f7de0cSMatt Gates }
8624e1f7de0cSMatt Gates 
8625edd16368SStephen M. Cameron module_init(hpsa_init);
8626edd16368SStephen M. Cameron module_exit(hpsa_cleanup);
8627