xref: /openbmc/linux/drivers/scsi/hpsa.c (revision 9ecd953aa8b07819af0f1f561f52cb4bd43e8735)
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>
47edd16368SStephen M. Cameron #include <linux/cciss_ioctl.h>
48edd16368SStephen M. Cameron #include <linux/string.h>
49edd16368SStephen M. Cameron #include <linux/bitmap.h>
5060063497SArun Sharma #include <linux/atomic.h>
51a0c12413SStephen M. Cameron #include <linux/jiffies.h>
5242a91641SDon Brace #include <linux/percpu-defs.h>
53094963daSStephen M. Cameron #include <linux/percpu.h>
542b08b3e9SDon Brace #include <asm/unaligned.h>
55283b4a9bSStephen M. Cameron #include <asm/div64.h>
56edd16368SStephen M. Cameron #include "hpsa_cmd.h"
57edd16368SStephen M. Cameron #include "hpsa.h"
58edd16368SStephen M. Cameron 
59edd16368SStephen M. Cameron /* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */
609a993302SStephen M. Cameron #define HPSA_DRIVER_VERSION "3.4.4-1"
61edd16368SStephen M. Cameron #define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
62f79cfec6SStephen M. Cameron #define HPSA "hpsa"
63edd16368SStephen M. Cameron 
64007e7aa9SRobert Elliott /* How long to wait for CISS doorbell communication */
65007e7aa9SRobert Elliott #define CLEAR_EVENT_WAIT_INTERVAL 20	/* ms for each msleep() call */
66007e7aa9SRobert Elliott #define MODE_CHANGE_WAIT_INTERVAL 10	/* ms for each msleep() call */
67007e7aa9SRobert Elliott #define MAX_CLEAR_EVENT_WAIT 30000	/* times 20 ms = 600 s */
68007e7aa9SRobert Elliott #define MAX_MODE_CHANGE_WAIT 2000	/* times 10 ms = 20 s */
69edd16368SStephen M. Cameron #define MAX_IOCTL_CONFIG_WAIT 1000
70edd16368SStephen M. Cameron 
71edd16368SStephen M. Cameron /*define how many times we will try a command because of bus resets */
72edd16368SStephen M. Cameron #define MAX_CMD_RETRIES 3
73edd16368SStephen M. Cameron 
74edd16368SStephen M. Cameron /* Embedded module documentation macros - see modules.h */
75edd16368SStephen M. Cameron MODULE_AUTHOR("Hewlett-Packard Company");
76edd16368SStephen M. Cameron MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
77edd16368SStephen M. Cameron 	HPSA_DRIVER_VERSION);
78edd16368SStephen M. Cameron MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
79edd16368SStephen M. Cameron MODULE_VERSION(HPSA_DRIVER_VERSION);
80edd16368SStephen M. Cameron MODULE_LICENSE("GPL");
81edd16368SStephen M. Cameron 
82edd16368SStephen M. Cameron static int hpsa_allow_any;
83edd16368SStephen M. Cameron module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
84edd16368SStephen M. Cameron MODULE_PARM_DESC(hpsa_allow_any,
85edd16368SStephen M. Cameron 		"Allow hpsa driver to access unknown HP Smart Array hardware");
8602ec19c8SStephen M. Cameron static int hpsa_simple_mode;
8702ec19c8SStephen M. Cameron module_param(hpsa_simple_mode, int, S_IRUGO|S_IWUSR);
8802ec19c8SStephen M. Cameron MODULE_PARM_DESC(hpsa_simple_mode,
8902ec19c8SStephen M. Cameron 	"Use 'simple mode' rather than 'performant mode'");
90edd16368SStephen M. Cameron 
91edd16368SStephen M. Cameron /* define the PCI info for the cards we can control */
92edd16368SStephen M. Cameron static const struct pci_device_id hpsa_pci_device_id[] = {
93edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3241},
94edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3243},
95edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3245},
96edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3247},
97edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3249},
98163dbcd8SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x324A},
99163dbcd8SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x324B},
100f8b01eb9SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3233},
1019143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3350},
1029143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3351},
1039143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3352},
1049143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3353},
1059143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3354},
1069143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3355},
1079143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3356},
108fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1921},
109fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1922},
110fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1923},
111fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1924},
112fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1926},
113fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1928},
11497b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1929},
11597b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BD},
11697b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BE},
11797b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BF},
11897b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C0},
11997b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C1},
12097b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C2},
12197b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C3},
12297b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C4},
12397b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C5},
1243b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C6},
12597b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C7},
12697b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C8},
12797b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C9},
1283b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CA},
1293b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CB},
1303b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CC},
1313b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CD},
1323b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CE},
1338e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0076},
1348e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0087},
1358e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x007D},
1368e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0088},
1378e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP, 0x333f, 0x103c, 0x333f},
138edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_ANY_ID,	PCI_ANY_ID, PCI_ANY_ID,
139edd16368SStephen M. Cameron 		PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
140edd16368SStephen M. Cameron 	{0,}
141edd16368SStephen M. Cameron };
142edd16368SStephen M. Cameron 
143edd16368SStephen M. Cameron MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
144edd16368SStephen M. Cameron 
145edd16368SStephen M. Cameron /*  board_id = Subsystem Device ID & Vendor ID
146edd16368SStephen M. Cameron  *  product = Marketing Name for the board
147edd16368SStephen M. Cameron  *  access = Address of the struct of function pointers
148edd16368SStephen M. Cameron  */
149edd16368SStephen M. Cameron static struct board_type products[] = {
150edd16368SStephen M. Cameron 	{0x3241103C, "Smart Array P212", &SA5_access},
151edd16368SStephen M. Cameron 	{0x3243103C, "Smart Array P410", &SA5_access},
152edd16368SStephen M. Cameron 	{0x3245103C, "Smart Array P410i", &SA5_access},
153edd16368SStephen M. Cameron 	{0x3247103C, "Smart Array P411", &SA5_access},
154edd16368SStephen M. Cameron 	{0x3249103C, "Smart Array P812", &SA5_access},
155163dbcd8SMike Miller 	{0x324A103C, "Smart Array P712m", &SA5_access},
156163dbcd8SMike Miller 	{0x324B103C, "Smart Array P711m", &SA5_access},
1577d2cce58SStephen M. Cameron 	{0x3233103C, "HP StorageWorks 1210m", &SA5_access}, /* alias of 333f */
158fe0c9610SMike Miller 	{0x3350103C, "Smart Array P222", &SA5_access},
159fe0c9610SMike Miller 	{0x3351103C, "Smart Array P420", &SA5_access},
160fe0c9610SMike Miller 	{0x3352103C, "Smart Array P421", &SA5_access},
161fe0c9610SMike Miller 	{0x3353103C, "Smart Array P822", &SA5_access},
162fe0c9610SMike Miller 	{0x3354103C, "Smart Array P420i", &SA5_access},
163fe0c9610SMike Miller 	{0x3355103C, "Smart Array P220i", &SA5_access},
164fe0c9610SMike Miller 	{0x3356103C, "Smart Array P721m", &SA5_access},
1651fd6c8e3SMike Miller 	{0x1921103C, "Smart Array P830i", &SA5_access},
1661fd6c8e3SMike Miller 	{0x1922103C, "Smart Array P430", &SA5_access},
1671fd6c8e3SMike Miller 	{0x1923103C, "Smart Array P431", &SA5_access},
1681fd6c8e3SMike Miller 	{0x1924103C, "Smart Array P830", &SA5_access},
1691fd6c8e3SMike Miller 	{0x1926103C, "Smart Array P731m", &SA5_access},
1701fd6c8e3SMike Miller 	{0x1928103C, "Smart Array P230i", &SA5_access},
1711fd6c8e3SMike Miller 	{0x1929103C, "Smart Array P530", &SA5_access},
17227fb8137SDon Brace 	{0x21BD103C, "Smart Array P244br", &SA5_access},
17327fb8137SDon Brace 	{0x21BE103C, "Smart Array P741m", &SA5_access},
17427fb8137SDon Brace 	{0x21BF103C, "Smart HBA H240ar", &SA5_access},
17527fb8137SDon Brace 	{0x21C0103C, "Smart Array P440ar", &SA5_access},
176c8ae0ab1SDon Brace 	{0x21C1103C, "Smart Array P840ar", &SA5_access},
17727fb8137SDon Brace 	{0x21C2103C, "Smart Array P440", &SA5_access},
17827fb8137SDon Brace 	{0x21C3103C, "Smart Array P441", &SA5_access},
17997b9f53dSMike Miller 	{0x21C4103C, "Smart Array", &SA5_access},
18027fb8137SDon Brace 	{0x21C5103C, "Smart Array P841", &SA5_access},
18127fb8137SDon Brace 	{0x21C6103C, "Smart HBA H244br", &SA5_access},
18227fb8137SDon Brace 	{0x21C7103C, "Smart HBA H240", &SA5_access},
18327fb8137SDon Brace 	{0x21C8103C, "Smart HBA H241", &SA5_access},
18497b9f53dSMike Miller 	{0x21C9103C, "Smart Array", &SA5_access},
18527fb8137SDon Brace 	{0x21CA103C, "Smart Array P246br", &SA5_access},
18627fb8137SDon Brace 	{0x21CB103C, "Smart Array P840", &SA5_access},
1873b7a45e5SJoe Handzik 	{0x21CC103C, "Smart Array", &SA5_access},
1883b7a45e5SJoe Handzik 	{0x21CD103C, "Smart Array", &SA5_access},
18927fb8137SDon Brace 	{0x21CE103C, "Smart HBA", &SA5_access},
1908e616a5eSStephen M. Cameron 	{0x00761590, "HP Storage P1224 Array Controller", &SA5_access},
1918e616a5eSStephen M. Cameron 	{0x00871590, "HP Storage P1224e Array Controller", &SA5_access},
1928e616a5eSStephen M. Cameron 	{0x007D1590, "HP Storage P1228 Array Controller", &SA5_access},
1938e616a5eSStephen M. Cameron 	{0x00881590, "HP Storage P1228e Array Controller", &SA5_access},
1948e616a5eSStephen M. Cameron 	{0x333f103c, "HP StorageWorks 1210m Array Controller", &SA5_access},
195edd16368SStephen M. Cameron 	{0xFFFF103C, "Unknown Smart Array", &SA5_access},
196edd16368SStephen M. Cameron };
197edd16368SStephen M. Cameron 
198a58e7e53SWebb Scales #define SCSI_CMD_BUSY ((struct scsi_cmnd *)&hpsa_cmd_busy)
199a58e7e53SWebb Scales static const struct scsi_cmnd hpsa_cmd_busy;
200a58e7e53SWebb Scales #define SCSI_CMD_IDLE ((struct scsi_cmnd *)&hpsa_cmd_idle)
201a58e7e53SWebb Scales static const struct scsi_cmnd hpsa_cmd_idle;
202edd16368SStephen M. Cameron static int number_of_controllers;
203edd16368SStephen M. Cameron 
20410f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id);
20510f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id);
20642a91641SDon Brace static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg);
207edd16368SStephen M. Cameron 
208edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
20942a91641SDon Brace static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd,
21042a91641SDon Brace 	void __user *arg);
211edd16368SStephen M. Cameron #endif
212edd16368SStephen M. Cameron 
213edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c);
214edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h);
215a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
216b7bb24ebSStephen M. Cameron 	void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
217edd16368SStephen M. Cameron 	int cmd_type);
2182c143342SRobert Elliott static void hpsa_free_cmd_pool(struct ctlr_info *h);
219b7bb24ebSStephen M. Cameron #define VPD_PAGE (1 << 8)
220edd16368SStephen M. Cameron 
221f281233dSJeff Garzik static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
222a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *);
223a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
224a08a8471SStephen M. Cameron 	unsigned long elapsed_time);
2257c0a0229SDon Brace static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth);
226edd16368SStephen M. Cameron 
227edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
22875167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *scsicmd);
229edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev);
23041ce4c35SStephen Cameron static int hpsa_slave_configure(struct scsi_device *sdev);
231edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev);
232edd16368SStephen M. Cameron 
233edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
234edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
235edd16368SStephen M. Cameron 	struct CommandList *c);
236edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
237edd16368SStephen M. Cameron 	struct CommandList *c);
238303932fdSDon Brace /* performant mode helper functions */
239303932fdSDon Brace static void calc_bucket_map(int *bucket, int num_buckets,
2402b08b3e9SDon Brace 	int nsgs, int min_blocks, u32 *bucket_map);
241105a3dbcSRobert Elliott static void hpsa_free_performant_mode(struct ctlr_info *h);
242105a3dbcSRobert Elliott static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
243254f796bSMatt Gates static inline u32 next_command(struct ctlr_info *h, u8 q);
2446f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
2456f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
2461df8552aSStephen M. Cameron 			       u64 *cfg_offset);
2476f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
2481df8552aSStephen M. Cameron 				    unsigned long *memory_bar);
2496f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
2506f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
2516f039790SGreg Kroah-Hartman 				     int wait_for_ready);
25275167d2cSStephen M. Cameron static inline void finish_cmd(struct CommandList *c);
253c706a795SRobert Elliott static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h);
254fe5389c8SStephen M. Cameron #define BOARD_NOT_READY 0
255fe5389c8SStephen M. Cameron #define BOARD_READY 1
25623100dd9SStephen M. Cameron static void hpsa_drain_accel_commands(struct ctlr_info *h);
25776438d08SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h);
258c349775eSScott Teel static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
259c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
26003383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk);
261080ef1ccSDon Brace static void hpsa_command_resubmit_worker(struct work_struct *work);
26225163bd5SWebb Scales static u32 lockup_detected(struct ctlr_info *h);
26325163bd5SWebb Scales static int detect_controller_lockup(struct ctlr_info *h);
264edd16368SStephen M. Cameron 
265edd16368SStephen M. Cameron static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
266edd16368SStephen M. Cameron {
267edd16368SStephen M. Cameron 	unsigned long *priv = shost_priv(sdev->host);
268edd16368SStephen M. Cameron 	return (struct ctlr_info *) *priv;
269edd16368SStephen M. Cameron }
270edd16368SStephen M. Cameron 
271a23513e8SStephen M. Cameron static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
272a23513e8SStephen M. Cameron {
273a23513e8SStephen M. Cameron 	unsigned long *priv = shost_priv(sh);
274a23513e8SStephen M. Cameron 	return (struct ctlr_info *) *priv;
275a23513e8SStephen M. Cameron }
276a23513e8SStephen M. Cameron 
277a58e7e53SWebb Scales static inline bool hpsa_is_cmd_idle(struct CommandList *c)
278a58e7e53SWebb Scales {
279a58e7e53SWebb Scales 	return c->scsi_cmd == SCSI_CMD_IDLE;
280a58e7e53SWebb Scales }
281a58e7e53SWebb Scales 
2829437ac43SStephen Cameron /* extract sense key, asc, and ascq from sense data.  -1 means invalid. */
2839437ac43SStephen Cameron static void decode_sense_data(const u8 *sense_data, int sense_data_len,
2849437ac43SStephen Cameron 			u8 *sense_key, u8 *asc, u8 *ascq)
2859437ac43SStephen Cameron {
2869437ac43SStephen Cameron 	struct scsi_sense_hdr sshdr;
2879437ac43SStephen Cameron 	bool rc;
2889437ac43SStephen Cameron 
2899437ac43SStephen Cameron 	*sense_key = -1;
2909437ac43SStephen Cameron 	*asc = -1;
2919437ac43SStephen Cameron 	*ascq = -1;
2929437ac43SStephen Cameron 
2939437ac43SStephen Cameron 	if (sense_data_len < 1)
2949437ac43SStephen Cameron 		return;
2959437ac43SStephen Cameron 
2969437ac43SStephen Cameron 	rc = scsi_normalize_sense(sense_data, sense_data_len, &sshdr);
2979437ac43SStephen Cameron 	if (rc) {
2989437ac43SStephen Cameron 		*sense_key = sshdr.sense_key;
2999437ac43SStephen Cameron 		*asc = sshdr.asc;
3009437ac43SStephen Cameron 		*ascq = sshdr.ascq;
3019437ac43SStephen Cameron 	}
3029437ac43SStephen Cameron }
3039437ac43SStephen Cameron 
304edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
305edd16368SStephen M. Cameron 	struct CommandList *c)
306edd16368SStephen M. Cameron {
3079437ac43SStephen Cameron 	u8 sense_key, asc, ascq;
3089437ac43SStephen Cameron 	int sense_len;
3099437ac43SStephen Cameron 
3109437ac43SStephen Cameron 	if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
3119437ac43SStephen Cameron 		sense_len = sizeof(c->err_info->SenseInfo);
3129437ac43SStephen Cameron 	else
3139437ac43SStephen Cameron 		sense_len = c->err_info->SenseLen;
3149437ac43SStephen Cameron 
3159437ac43SStephen Cameron 	decode_sense_data(c->err_info->SenseInfo, sense_len,
3169437ac43SStephen Cameron 				&sense_key, &asc, &ascq);
3179437ac43SStephen Cameron 	if (sense_key != UNIT_ATTENTION || asc == -1)
318edd16368SStephen M. Cameron 		return 0;
319edd16368SStephen M. Cameron 
3209437ac43SStephen Cameron 	switch (asc) {
321edd16368SStephen M. Cameron 	case STATE_CHANGED:
3229437ac43SStephen Cameron 		dev_warn(&h->pdev->dev,
3239437ac43SStephen Cameron 			HPSA "%d: a state change detected, command retried\n",
3249437ac43SStephen Cameron 			h->ctlr);
325edd16368SStephen M. Cameron 		break;
326edd16368SStephen M. Cameron 	case LUN_FAILED:
3277f73695aSStephen M. Cameron 		dev_warn(&h->pdev->dev,
3287f73695aSStephen M. Cameron 			HPSA "%d: LUN failure detected\n", h->ctlr);
329edd16368SStephen M. Cameron 		break;
330edd16368SStephen M. Cameron 	case REPORT_LUNS_CHANGED:
3317f73695aSStephen M. Cameron 		dev_warn(&h->pdev->dev,
3327f73695aSStephen M. Cameron 			HPSA "%d: report LUN data changed\n", h->ctlr);
333edd16368SStephen M. Cameron 	/*
3344f4eb9f1SScott Teel 	 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
3354f4eb9f1SScott Teel 	 * target (array) devices.
336edd16368SStephen M. Cameron 	 */
337edd16368SStephen M. Cameron 		break;
338edd16368SStephen M. Cameron 	case POWER_OR_RESET:
339f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: a power on "
340edd16368SStephen M. Cameron 			"or device reset detected\n", h->ctlr);
341edd16368SStephen M. Cameron 		break;
342edd16368SStephen M. Cameron 	case UNIT_ATTENTION_CLEARED:
343f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: unit attention "
344edd16368SStephen M. Cameron 		    "cleared by another initiator\n", h->ctlr);
345edd16368SStephen M. Cameron 		break;
346edd16368SStephen M. Cameron 	default:
347f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: unknown "
348edd16368SStephen M. Cameron 			"unit attention detected\n", h->ctlr);
349edd16368SStephen M. Cameron 		break;
350edd16368SStephen M. Cameron 	}
351edd16368SStephen M. Cameron 	return 1;
352edd16368SStephen M. Cameron }
353edd16368SStephen M. Cameron 
354852af20aSMatt Bondurant static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
355852af20aSMatt Bondurant {
356852af20aSMatt Bondurant 	if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
357852af20aSMatt Bondurant 		(c->err_info->ScsiStatus != SAM_STAT_BUSY &&
358852af20aSMatt Bondurant 		 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
359852af20aSMatt Bondurant 		return 0;
360852af20aSMatt Bondurant 	dev_warn(&h->pdev->dev, HPSA "device busy");
361852af20aSMatt Bondurant 	return 1;
362852af20aSMatt Bondurant }
363852af20aSMatt Bondurant 
364e985c58fSStephen Cameron static u32 lockup_detected(struct ctlr_info *h);
365e985c58fSStephen Cameron static ssize_t host_show_lockup_detected(struct device *dev,
366e985c58fSStephen Cameron 		struct device_attribute *attr, char *buf)
367e985c58fSStephen Cameron {
368e985c58fSStephen Cameron 	int ld;
369e985c58fSStephen Cameron 	struct ctlr_info *h;
370e985c58fSStephen Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
371e985c58fSStephen Cameron 
372e985c58fSStephen Cameron 	h = shost_to_hba(shost);
373e985c58fSStephen Cameron 	ld = lockup_detected(h);
374e985c58fSStephen Cameron 
375e985c58fSStephen Cameron 	return sprintf(buf, "ld=%d\n", ld);
376e985c58fSStephen Cameron }
377e985c58fSStephen Cameron 
378da0697bdSScott Teel static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
379da0697bdSScott Teel 					 struct device_attribute *attr,
380da0697bdSScott Teel 					 const char *buf, size_t count)
381da0697bdSScott Teel {
382da0697bdSScott Teel 	int status, len;
383da0697bdSScott Teel 	struct ctlr_info *h;
384da0697bdSScott Teel 	struct Scsi_Host *shost = class_to_shost(dev);
385da0697bdSScott Teel 	char tmpbuf[10];
386da0697bdSScott Teel 
387da0697bdSScott Teel 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
388da0697bdSScott Teel 		return -EACCES;
389da0697bdSScott Teel 	len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
390da0697bdSScott Teel 	strncpy(tmpbuf, buf, len);
391da0697bdSScott Teel 	tmpbuf[len] = '\0';
392da0697bdSScott Teel 	if (sscanf(tmpbuf, "%d", &status) != 1)
393da0697bdSScott Teel 		return -EINVAL;
394da0697bdSScott Teel 	h = shost_to_hba(shost);
395da0697bdSScott Teel 	h->acciopath_status = !!status;
396da0697bdSScott Teel 	dev_warn(&h->pdev->dev,
397da0697bdSScott Teel 		"hpsa: HP SSD Smart Path %s via sysfs update.\n",
398da0697bdSScott Teel 		h->acciopath_status ? "enabled" : "disabled");
399da0697bdSScott Teel 	return count;
400da0697bdSScott Teel }
401da0697bdSScott Teel 
4022ba8bfc8SStephen M. Cameron static ssize_t host_store_raid_offload_debug(struct device *dev,
4032ba8bfc8SStephen M. Cameron 					 struct device_attribute *attr,
4042ba8bfc8SStephen M. Cameron 					 const char *buf, size_t count)
4052ba8bfc8SStephen M. Cameron {
4062ba8bfc8SStephen M. Cameron 	int debug_level, len;
4072ba8bfc8SStephen M. Cameron 	struct ctlr_info *h;
4082ba8bfc8SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
4092ba8bfc8SStephen M. Cameron 	char tmpbuf[10];
4102ba8bfc8SStephen M. Cameron 
4112ba8bfc8SStephen M. Cameron 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
4122ba8bfc8SStephen M. Cameron 		return -EACCES;
4132ba8bfc8SStephen M. Cameron 	len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
4142ba8bfc8SStephen M. Cameron 	strncpy(tmpbuf, buf, len);
4152ba8bfc8SStephen M. Cameron 	tmpbuf[len] = '\0';
4162ba8bfc8SStephen M. Cameron 	if (sscanf(tmpbuf, "%d", &debug_level) != 1)
4172ba8bfc8SStephen M. Cameron 		return -EINVAL;
4182ba8bfc8SStephen M. Cameron 	if (debug_level < 0)
4192ba8bfc8SStephen M. Cameron 		debug_level = 0;
4202ba8bfc8SStephen M. Cameron 	h = shost_to_hba(shost);
4212ba8bfc8SStephen M. Cameron 	h->raid_offload_debug = debug_level;
4222ba8bfc8SStephen M. Cameron 	dev_warn(&h->pdev->dev, "hpsa: Set raid_offload_debug level = %d\n",
4232ba8bfc8SStephen M. Cameron 		h->raid_offload_debug);
4242ba8bfc8SStephen M. Cameron 	return count;
4252ba8bfc8SStephen M. Cameron }
4262ba8bfc8SStephen M. Cameron 
427edd16368SStephen M. Cameron static ssize_t host_store_rescan(struct device *dev,
428edd16368SStephen M. Cameron 				 struct device_attribute *attr,
429edd16368SStephen M. Cameron 				 const char *buf, size_t count)
430edd16368SStephen M. Cameron {
431edd16368SStephen M. Cameron 	struct ctlr_info *h;
432edd16368SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
433a23513e8SStephen M. Cameron 	h = shost_to_hba(shost);
43431468401SMike Miller 	hpsa_scan_start(h->scsi_host);
435edd16368SStephen M. Cameron 	return count;
436edd16368SStephen M. Cameron }
437edd16368SStephen M. Cameron 
438d28ce020SStephen M. Cameron static ssize_t host_show_firmware_revision(struct device *dev,
439d28ce020SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
440d28ce020SStephen M. Cameron {
441d28ce020SStephen M. Cameron 	struct ctlr_info *h;
442d28ce020SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
443d28ce020SStephen M. Cameron 	unsigned char *fwrev;
444d28ce020SStephen M. Cameron 
445d28ce020SStephen M. Cameron 	h = shost_to_hba(shost);
446d28ce020SStephen M. Cameron 	if (!h->hba_inquiry_data)
447d28ce020SStephen M. Cameron 		return 0;
448d28ce020SStephen M. Cameron 	fwrev = &h->hba_inquiry_data[32];
449d28ce020SStephen M. Cameron 	return snprintf(buf, 20, "%c%c%c%c\n",
450d28ce020SStephen M. Cameron 		fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
451d28ce020SStephen M. Cameron }
452d28ce020SStephen M. Cameron 
45394a13649SStephen M. Cameron static ssize_t host_show_commands_outstanding(struct device *dev,
45494a13649SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
45594a13649SStephen M. Cameron {
45694a13649SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
45794a13649SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(shost);
45894a13649SStephen M. Cameron 
4590cbf768eSStephen M. Cameron 	return snprintf(buf, 20, "%d\n",
4600cbf768eSStephen M. Cameron 			atomic_read(&h->commands_outstanding));
46194a13649SStephen M. Cameron }
46294a13649SStephen M. Cameron 
463745a7a25SStephen M. Cameron static ssize_t host_show_transport_mode(struct device *dev,
464745a7a25SStephen M. Cameron 	struct device_attribute *attr, char *buf)
465745a7a25SStephen M. Cameron {
466745a7a25SStephen M. Cameron 	struct ctlr_info *h;
467745a7a25SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
468745a7a25SStephen M. Cameron 
469745a7a25SStephen M. Cameron 	h = shost_to_hba(shost);
470745a7a25SStephen M. Cameron 	return snprintf(buf, 20, "%s\n",
471960a30e7SStephen M. Cameron 		h->transMethod & CFGTBL_Trans_Performant ?
472745a7a25SStephen M. Cameron 			"performant" : "simple");
473745a7a25SStephen M. Cameron }
474745a7a25SStephen M. Cameron 
475da0697bdSScott Teel static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
476da0697bdSScott Teel 	struct device_attribute *attr, char *buf)
477da0697bdSScott Teel {
478da0697bdSScott Teel 	struct ctlr_info *h;
479da0697bdSScott Teel 	struct Scsi_Host *shost = class_to_shost(dev);
480da0697bdSScott Teel 
481da0697bdSScott Teel 	h = shost_to_hba(shost);
482da0697bdSScott Teel 	return snprintf(buf, 30, "HP SSD Smart Path %s\n",
483da0697bdSScott Teel 		(h->acciopath_status == 1) ?  "enabled" : "disabled");
484da0697bdSScott Teel }
485da0697bdSScott Teel 
48646380786SStephen M. Cameron /* List of controllers which cannot be hard reset on kexec with reset_devices */
487941b1cdaSStephen M. Cameron static u32 unresettable_controller[] = {
488941b1cdaSStephen M. Cameron 	0x324a103C, /* Smart Array P712m */
489941b1cdaSStephen M. Cameron 	0x324b103C, /* Smart Array P711m */
490941b1cdaSStephen M. Cameron 	0x3223103C, /* Smart Array P800 */
491941b1cdaSStephen M. Cameron 	0x3234103C, /* Smart Array P400 */
492941b1cdaSStephen M. Cameron 	0x3235103C, /* Smart Array P400i */
493941b1cdaSStephen M. Cameron 	0x3211103C, /* Smart Array E200i */
494941b1cdaSStephen M. Cameron 	0x3212103C, /* Smart Array E200 */
495941b1cdaSStephen M. Cameron 	0x3213103C, /* Smart Array E200i */
496941b1cdaSStephen M. Cameron 	0x3214103C, /* Smart Array E200i */
497941b1cdaSStephen M. Cameron 	0x3215103C, /* Smart Array E200i */
498941b1cdaSStephen M. Cameron 	0x3237103C, /* Smart Array E500 */
499941b1cdaSStephen M. Cameron 	0x323D103C, /* Smart Array P700m */
5007af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
501941b1cdaSStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
502941b1cdaSStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
5035a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
5045a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
5055a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
5065a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
5075a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
5085a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
509941b1cdaSStephen M. Cameron };
510941b1cdaSStephen M. Cameron 
51146380786SStephen M. Cameron /* List of controllers which cannot even be soft reset */
51246380786SStephen M. Cameron static u32 soft_unresettable_controller[] = {
5137af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
5145a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
5155a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
5165a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
5175a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
5185a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
5195a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
52046380786SStephen M. Cameron 	/* Exclude 640x boards.  These are two pci devices in one slot
52146380786SStephen M. Cameron 	 * which share a battery backed cache module.  One controls the
52246380786SStephen M. Cameron 	 * cache, the other accesses the cache through the one that controls
52346380786SStephen M. Cameron 	 * it.  If we reset the one controlling the cache, the other will
52446380786SStephen M. Cameron 	 * likely not be happy.  Just forbid resetting this conjoined mess.
52546380786SStephen M. Cameron 	 * The 640x isn't really supported by hpsa anyway.
52646380786SStephen M. Cameron 	 */
52746380786SStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
52846380786SStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
52946380786SStephen M. Cameron };
53046380786SStephen M. Cameron 
5319b5c48c2SStephen Cameron static u32 needs_abort_tags_swizzled[] = {
5329b5c48c2SStephen Cameron 	0x323D103C, /* Smart Array P700m */
5339b5c48c2SStephen Cameron 	0x324a103C, /* Smart Array P712m */
5349b5c48c2SStephen Cameron 	0x324b103C, /* SmartArray P711m */
5359b5c48c2SStephen Cameron };
5369b5c48c2SStephen Cameron 
5379b5c48c2SStephen Cameron static int board_id_in_array(u32 a[], int nelems, u32 board_id)
538941b1cdaSStephen M. Cameron {
539941b1cdaSStephen M. Cameron 	int i;
540941b1cdaSStephen M. Cameron 
5419b5c48c2SStephen Cameron 	for (i = 0; i < nelems; i++)
5429b5c48c2SStephen Cameron 		if (a[i] == board_id)
543941b1cdaSStephen M. Cameron 			return 1;
5449b5c48c2SStephen Cameron 	return 0;
5459b5c48c2SStephen Cameron }
5469b5c48c2SStephen Cameron 
5479b5c48c2SStephen Cameron static int ctlr_is_hard_resettable(u32 board_id)
5489b5c48c2SStephen Cameron {
5499b5c48c2SStephen Cameron 	return !board_id_in_array(unresettable_controller,
5509b5c48c2SStephen Cameron 			ARRAY_SIZE(unresettable_controller), board_id);
551941b1cdaSStephen M. Cameron }
552941b1cdaSStephen M. Cameron 
55346380786SStephen M. Cameron static int ctlr_is_soft_resettable(u32 board_id)
55446380786SStephen M. Cameron {
5559b5c48c2SStephen Cameron 	return !board_id_in_array(soft_unresettable_controller,
5569b5c48c2SStephen Cameron 			ARRAY_SIZE(soft_unresettable_controller), board_id);
55746380786SStephen M. Cameron }
55846380786SStephen M. Cameron 
55946380786SStephen M. Cameron static int ctlr_is_resettable(u32 board_id)
56046380786SStephen M. Cameron {
56146380786SStephen M. Cameron 	return ctlr_is_hard_resettable(board_id) ||
56246380786SStephen M. Cameron 		ctlr_is_soft_resettable(board_id);
56346380786SStephen M. Cameron }
56446380786SStephen M. Cameron 
5659b5c48c2SStephen Cameron static int ctlr_needs_abort_tags_swizzled(u32 board_id)
5669b5c48c2SStephen Cameron {
5679b5c48c2SStephen Cameron 	return board_id_in_array(needs_abort_tags_swizzled,
5689b5c48c2SStephen Cameron 			ARRAY_SIZE(needs_abort_tags_swizzled), board_id);
5699b5c48c2SStephen Cameron }
5709b5c48c2SStephen Cameron 
571941b1cdaSStephen M. Cameron static ssize_t host_show_resettable(struct device *dev,
572941b1cdaSStephen M. Cameron 	struct device_attribute *attr, char *buf)
573941b1cdaSStephen M. Cameron {
574941b1cdaSStephen M. Cameron 	struct ctlr_info *h;
575941b1cdaSStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
576941b1cdaSStephen M. Cameron 
577941b1cdaSStephen M. Cameron 	h = shost_to_hba(shost);
57846380786SStephen M. Cameron 	return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
579941b1cdaSStephen M. Cameron }
580941b1cdaSStephen M. Cameron 
581edd16368SStephen M. Cameron static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
582edd16368SStephen M. Cameron {
583edd16368SStephen M. Cameron 	return (scsi3addr[3] & 0xC0) == 0x40;
584edd16368SStephen M. Cameron }
585edd16368SStephen M. Cameron 
586f2ef0ce7SRobert Elliott static const char * const raid_label[] = { "0", "4", "1(+0)", "5", "5+1", "6",
587f2ef0ce7SRobert Elliott 	"1(+0)ADM", "UNKNOWN"
588edd16368SStephen M. Cameron };
5896b80b18fSScott Teel #define HPSA_RAID_0	0
5906b80b18fSScott Teel #define HPSA_RAID_4	1
5916b80b18fSScott Teel #define HPSA_RAID_1	2	/* also used for RAID 10 */
5926b80b18fSScott Teel #define HPSA_RAID_5	3	/* also used for RAID 50 */
5936b80b18fSScott Teel #define HPSA_RAID_51	4
5946b80b18fSScott Teel #define HPSA_RAID_6	5	/* also used for RAID 60 */
5956b80b18fSScott Teel #define HPSA_RAID_ADM	6	/* also used for RAID 1+0 ADM */
596edd16368SStephen M. Cameron #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
597edd16368SStephen M. Cameron 
598edd16368SStephen M. Cameron static ssize_t raid_level_show(struct device *dev,
599edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
600edd16368SStephen M. Cameron {
601edd16368SStephen M. Cameron 	ssize_t l = 0;
60282a72c0aSStephen M. Cameron 	unsigned char rlevel;
603edd16368SStephen M. Cameron 	struct ctlr_info *h;
604edd16368SStephen M. Cameron 	struct scsi_device *sdev;
605edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
606edd16368SStephen M. Cameron 	unsigned long flags;
607edd16368SStephen M. Cameron 
608edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
609edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
610edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
611edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
612edd16368SStephen M. Cameron 	if (!hdev) {
613edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
614edd16368SStephen M. Cameron 		return -ENODEV;
615edd16368SStephen M. Cameron 	}
616edd16368SStephen M. Cameron 
617edd16368SStephen M. Cameron 	/* Is this even a logical drive? */
618edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
619edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
620edd16368SStephen M. Cameron 		l = snprintf(buf, PAGE_SIZE, "N/A\n");
621edd16368SStephen M. Cameron 		return l;
622edd16368SStephen M. Cameron 	}
623edd16368SStephen M. Cameron 
624edd16368SStephen M. Cameron 	rlevel = hdev->raid_level;
625edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
62682a72c0aSStephen M. Cameron 	if (rlevel > RAID_UNKNOWN)
627edd16368SStephen M. Cameron 		rlevel = RAID_UNKNOWN;
628edd16368SStephen M. Cameron 	l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
629edd16368SStephen M. Cameron 	return l;
630edd16368SStephen M. Cameron }
631edd16368SStephen M. Cameron 
632edd16368SStephen M. Cameron static ssize_t lunid_show(struct device *dev,
633edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
634edd16368SStephen M. Cameron {
635edd16368SStephen M. Cameron 	struct ctlr_info *h;
636edd16368SStephen M. Cameron 	struct scsi_device *sdev;
637edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
638edd16368SStephen M. Cameron 	unsigned long flags;
639edd16368SStephen M. Cameron 	unsigned char lunid[8];
640edd16368SStephen M. Cameron 
641edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
642edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
643edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
644edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
645edd16368SStephen M. Cameron 	if (!hdev) {
646edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
647edd16368SStephen M. Cameron 		return -ENODEV;
648edd16368SStephen M. Cameron 	}
649edd16368SStephen M. Cameron 	memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
650edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
651edd16368SStephen M. Cameron 	return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
652edd16368SStephen M. Cameron 		lunid[0], lunid[1], lunid[2], lunid[3],
653edd16368SStephen M. Cameron 		lunid[4], lunid[5], lunid[6], lunid[7]);
654edd16368SStephen M. Cameron }
655edd16368SStephen M. Cameron 
656edd16368SStephen M. Cameron static ssize_t unique_id_show(struct device *dev,
657edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
658edd16368SStephen M. Cameron {
659edd16368SStephen M. Cameron 	struct ctlr_info *h;
660edd16368SStephen M. Cameron 	struct scsi_device *sdev;
661edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
662edd16368SStephen M. Cameron 	unsigned long flags;
663edd16368SStephen M. Cameron 	unsigned char sn[16];
664edd16368SStephen M. Cameron 
665edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
666edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
667edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
668edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
669edd16368SStephen M. Cameron 	if (!hdev) {
670edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
671edd16368SStephen M. Cameron 		return -ENODEV;
672edd16368SStephen M. Cameron 	}
673edd16368SStephen M. Cameron 	memcpy(sn, hdev->device_id, sizeof(sn));
674edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
675edd16368SStephen M. Cameron 	return snprintf(buf, 16 * 2 + 2,
676edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X"
677edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X\n",
678edd16368SStephen M. Cameron 			sn[0], sn[1], sn[2], sn[3],
679edd16368SStephen M. Cameron 			sn[4], sn[5], sn[6], sn[7],
680edd16368SStephen M. Cameron 			sn[8], sn[9], sn[10], sn[11],
681edd16368SStephen M. Cameron 			sn[12], sn[13], sn[14], sn[15]);
682edd16368SStephen M. Cameron }
683edd16368SStephen M. Cameron 
684c1988684SScott Teel static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
685c1988684SScott Teel 	     struct device_attribute *attr, char *buf)
686c1988684SScott Teel {
687c1988684SScott Teel 	struct ctlr_info *h;
688c1988684SScott Teel 	struct scsi_device *sdev;
689c1988684SScott Teel 	struct hpsa_scsi_dev_t *hdev;
690c1988684SScott Teel 	unsigned long flags;
691c1988684SScott Teel 	int offload_enabled;
692c1988684SScott Teel 
693c1988684SScott Teel 	sdev = to_scsi_device(dev);
694c1988684SScott Teel 	h = sdev_to_hba(sdev);
695c1988684SScott Teel 	spin_lock_irqsave(&h->lock, flags);
696c1988684SScott Teel 	hdev = sdev->hostdata;
697c1988684SScott Teel 	if (!hdev) {
698c1988684SScott Teel 		spin_unlock_irqrestore(&h->lock, flags);
699c1988684SScott Teel 		return -ENODEV;
700c1988684SScott Teel 	}
701c1988684SScott Teel 	offload_enabled = hdev->offload_enabled;
702c1988684SScott Teel 	spin_unlock_irqrestore(&h->lock, flags);
703c1988684SScott Teel 	return snprintf(buf, 20, "%d\n", offload_enabled);
704c1988684SScott Teel }
705c1988684SScott Teel 
7063f5eac3aSStephen M. Cameron static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
7073f5eac3aSStephen M. Cameron static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
7083f5eac3aSStephen M. Cameron static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
7093f5eac3aSStephen M. Cameron static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
710c1988684SScott Teel static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
711c1988684SScott Teel 			host_show_hp_ssd_smart_path_enabled, NULL);
712da0697bdSScott Teel static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
713da0697bdSScott Teel 		host_show_hp_ssd_smart_path_status,
714da0697bdSScott Teel 		host_store_hp_ssd_smart_path_status);
7152ba8bfc8SStephen M. Cameron static DEVICE_ATTR(raid_offload_debug, S_IWUSR, NULL,
7162ba8bfc8SStephen M. Cameron 			host_store_raid_offload_debug);
7173f5eac3aSStephen M. Cameron static DEVICE_ATTR(firmware_revision, S_IRUGO,
7183f5eac3aSStephen M. Cameron 	host_show_firmware_revision, NULL);
7193f5eac3aSStephen M. Cameron static DEVICE_ATTR(commands_outstanding, S_IRUGO,
7203f5eac3aSStephen M. Cameron 	host_show_commands_outstanding, NULL);
7213f5eac3aSStephen M. Cameron static DEVICE_ATTR(transport_mode, S_IRUGO,
7223f5eac3aSStephen M. Cameron 	host_show_transport_mode, NULL);
723941b1cdaSStephen M. Cameron static DEVICE_ATTR(resettable, S_IRUGO,
724941b1cdaSStephen M. Cameron 	host_show_resettable, NULL);
725e985c58fSStephen Cameron static DEVICE_ATTR(lockup_detected, S_IRUGO,
726e985c58fSStephen Cameron 	host_show_lockup_detected, NULL);
7273f5eac3aSStephen M. Cameron 
7283f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_sdev_attrs[] = {
7293f5eac3aSStephen M. Cameron 	&dev_attr_raid_level,
7303f5eac3aSStephen M. Cameron 	&dev_attr_lunid,
7313f5eac3aSStephen M. Cameron 	&dev_attr_unique_id,
732c1988684SScott Teel 	&dev_attr_hp_ssd_smart_path_enabled,
733e985c58fSStephen Cameron 	&dev_attr_lockup_detected,
7343f5eac3aSStephen M. Cameron 	NULL,
7353f5eac3aSStephen M. Cameron };
7363f5eac3aSStephen M. Cameron 
7373f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_shost_attrs[] = {
7383f5eac3aSStephen M. Cameron 	&dev_attr_rescan,
7393f5eac3aSStephen M. Cameron 	&dev_attr_firmware_revision,
7403f5eac3aSStephen M. Cameron 	&dev_attr_commands_outstanding,
7413f5eac3aSStephen M. Cameron 	&dev_attr_transport_mode,
742941b1cdaSStephen M. Cameron 	&dev_attr_resettable,
743da0697bdSScott Teel 	&dev_attr_hp_ssd_smart_path_status,
7442ba8bfc8SStephen M. Cameron 	&dev_attr_raid_offload_debug,
7453f5eac3aSStephen M. Cameron 	NULL,
7463f5eac3aSStephen M. Cameron };
7473f5eac3aSStephen M. Cameron 
74841ce4c35SStephen Cameron #define HPSA_NRESERVED_CMDS	(HPSA_CMDS_RESERVED_FOR_ABORTS + \
74941ce4c35SStephen Cameron 		HPSA_CMDS_RESERVED_FOR_DRIVER + HPSA_MAX_CONCURRENT_PASSTHRUS)
75041ce4c35SStephen Cameron 
7513f5eac3aSStephen M. Cameron static struct scsi_host_template hpsa_driver_template = {
7523f5eac3aSStephen M. Cameron 	.module			= THIS_MODULE,
753f79cfec6SStephen M. Cameron 	.name			= HPSA,
754f79cfec6SStephen M. Cameron 	.proc_name		= HPSA,
7553f5eac3aSStephen M. Cameron 	.queuecommand		= hpsa_scsi_queue_command,
7563f5eac3aSStephen M. Cameron 	.scan_start		= hpsa_scan_start,
7573f5eac3aSStephen M. Cameron 	.scan_finished		= hpsa_scan_finished,
7587c0a0229SDon Brace 	.change_queue_depth	= hpsa_change_queue_depth,
7593f5eac3aSStephen M. Cameron 	.this_id		= -1,
7603f5eac3aSStephen M. Cameron 	.use_clustering		= ENABLE_CLUSTERING,
76175167d2cSStephen M. Cameron 	.eh_abort_handler	= hpsa_eh_abort_handler,
7623f5eac3aSStephen M. Cameron 	.eh_device_reset_handler = hpsa_eh_device_reset_handler,
7633f5eac3aSStephen M. Cameron 	.ioctl			= hpsa_ioctl,
7643f5eac3aSStephen M. Cameron 	.slave_alloc		= hpsa_slave_alloc,
76541ce4c35SStephen Cameron 	.slave_configure	= hpsa_slave_configure,
7663f5eac3aSStephen M. Cameron 	.slave_destroy		= hpsa_slave_destroy,
7673f5eac3aSStephen M. Cameron #ifdef CONFIG_COMPAT
7683f5eac3aSStephen M. Cameron 	.compat_ioctl		= hpsa_compat_ioctl,
7693f5eac3aSStephen M. Cameron #endif
7703f5eac3aSStephen M. Cameron 	.sdev_attrs = hpsa_sdev_attrs,
7713f5eac3aSStephen M. Cameron 	.shost_attrs = hpsa_shost_attrs,
772c0d6a4d1SStephen M. Cameron 	.max_sectors = 8192,
77354b2b50cSMartin K. Petersen 	.no_write_same = 1,
7743f5eac3aSStephen M. Cameron };
7753f5eac3aSStephen M. Cameron 
776254f796bSMatt Gates static inline u32 next_command(struct ctlr_info *h, u8 q)
7773f5eac3aSStephen M. Cameron {
7783f5eac3aSStephen M. Cameron 	u32 a;
779072b0518SStephen M. Cameron 	struct reply_queue_buffer *rq = &h->reply_queue[q];
7803f5eac3aSStephen M. Cameron 
781e1f7de0cSMatt Gates 	if (h->transMethod & CFGTBL_Trans_io_accel1)
782e1f7de0cSMatt Gates 		return h->access.command_completed(h, q);
783e1f7de0cSMatt Gates 
7843f5eac3aSStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
785254f796bSMatt Gates 		return h->access.command_completed(h, q);
7863f5eac3aSStephen M. Cameron 
787254f796bSMatt Gates 	if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
788254f796bSMatt Gates 		a = rq->head[rq->current_entry];
789254f796bSMatt Gates 		rq->current_entry++;
7900cbf768eSStephen M. Cameron 		atomic_dec(&h->commands_outstanding);
7913f5eac3aSStephen M. Cameron 	} else {
7923f5eac3aSStephen M. Cameron 		a = FIFO_EMPTY;
7933f5eac3aSStephen M. Cameron 	}
7943f5eac3aSStephen M. Cameron 	/* Check for wraparound */
795254f796bSMatt Gates 	if (rq->current_entry == h->max_commands) {
796254f796bSMatt Gates 		rq->current_entry = 0;
797254f796bSMatt Gates 		rq->wraparound ^= 1;
7983f5eac3aSStephen M. Cameron 	}
7993f5eac3aSStephen M. Cameron 	return a;
8003f5eac3aSStephen M. Cameron }
8013f5eac3aSStephen M. Cameron 
802c349775eSScott Teel /*
803c349775eSScott Teel  * There are some special bits in the bus address of the
804c349775eSScott Teel  * command that we have to set for the controller to know
805c349775eSScott Teel  * how to process the command:
806c349775eSScott Teel  *
807c349775eSScott Teel  * Normal performant mode:
808c349775eSScott Teel  * bit 0: 1 means performant mode, 0 means simple mode.
809c349775eSScott Teel  * bits 1-3 = block fetch table entry
810c349775eSScott Teel  * bits 4-6 = command type (== 0)
811c349775eSScott Teel  *
812c349775eSScott Teel  * ioaccel1 mode:
813c349775eSScott Teel  * bit 0 = "performant mode" bit.
814c349775eSScott Teel  * bits 1-3 = block fetch table entry
815c349775eSScott Teel  * bits 4-6 = command type (== 110)
816c349775eSScott Teel  * (command type is needed because ioaccel1 mode
817c349775eSScott Teel  * commands are submitted through the same register as normal
818c349775eSScott Teel  * mode commands, so this is how the controller knows whether
819c349775eSScott Teel  * the command is normal mode or ioaccel1 mode.)
820c349775eSScott Teel  *
821c349775eSScott Teel  * ioaccel2 mode:
822c349775eSScott Teel  * bit 0 = "performant mode" bit.
823c349775eSScott Teel  * bits 1-4 = block fetch table entry (note extra bit)
824c349775eSScott Teel  * bits 4-6 = not needed, because ioaccel2 mode has
825c349775eSScott Teel  * a separate special register for submitting commands.
826c349775eSScott Teel  */
827c349775eSScott Teel 
82825163bd5SWebb Scales /*
82925163bd5SWebb Scales  * set_performant_mode: Modify the tag for cciss performant
8303f5eac3aSStephen M. Cameron  * set bit 0 for pull model, bits 3-1 for block fetch
8313f5eac3aSStephen M. Cameron  * register number
8323f5eac3aSStephen M. Cameron  */
83325163bd5SWebb Scales #define DEFAULT_REPLY_QUEUE (-1)
83425163bd5SWebb Scales static void set_performant_mode(struct ctlr_info *h, struct CommandList *c,
83525163bd5SWebb Scales 					int reply_queue)
8363f5eac3aSStephen M. Cameron {
837254f796bSMatt Gates 	if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
8383f5eac3aSStephen M. Cameron 		c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
83925163bd5SWebb Scales 		if (unlikely(!h->msix_vector))
84025163bd5SWebb Scales 			return;
84125163bd5SWebb Scales 		if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
842254f796bSMatt Gates 			c->Header.ReplyQueue =
843804a5cb5SJohn Kacur 				raw_smp_processor_id() % h->nreply_queues;
84425163bd5SWebb Scales 		else
84525163bd5SWebb Scales 			c->Header.ReplyQueue = reply_queue % h->nreply_queues;
846254f796bSMatt Gates 	}
8473f5eac3aSStephen M. Cameron }
8483f5eac3aSStephen M. Cameron 
849c349775eSScott Teel static void set_ioaccel1_performant_mode(struct ctlr_info *h,
85025163bd5SWebb Scales 						struct CommandList *c,
85125163bd5SWebb Scales 						int reply_queue)
852c349775eSScott Teel {
853c349775eSScott Teel 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
854c349775eSScott Teel 
85525163bd5SWebb Scales 	/*
85625163bd5SWebb Scales 	 * Tell the controller to post the reply to the queue for this
857c349775eSScott Teel 	 * processor.  This seems to give the best I/O throughput.
858c349775eSScott Teel 	 */
85925163bd5SWebb Scales 	if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
860c349775eSScott Teel 		cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
86125163bd5SWebb Scales 	else
86225163bd5SWebb Scales 		cp->ReplyQueue = reply_queue % h->nreply_queues;
86325163bd5SWebb Scales 	/*
86425163bd5SWebb Scales 	 * Set the bits in the address sent down to include:
865c349775eSScott Teel 	 *  - performant mode bit (bit 0)
866c349775eSScott Teel 	 *  - pull count (bits 1-3)
867c349775eSScott Teel 	 *  - command type (bits 4-6)
868c349775eSScott Teel 	 */
869c349775eSScott Teel 	c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
870c349775eSScott Teel 					IOACCEL1_BUSADDR_CMDTYPE;
871c349775eSScott Teel }
872c349775eSScott Teel 
8738be986ccSStephen Cameron static void set_ioaccel2_tmf_performant_mode(struct ctlr_info *h,
8748be986ccSStephen Cameron 						struct CommandList *c,
8758be986ccSStephen Cameron 						int reply_queue)
8768be986ccSStephen Cameron {
8778be986ccSStephen Cameron 	struct hpsa_tmf_struct *cp = (struct hpsa_tmf_struct *)
8788be986ccSStephen Cameron 		&h->ioaccel2_cmd_pool[c->cmdindex];
8798be986ccSStephen Cameron 
8808be986ccSStephen Cameron 	/* Tell the controller to post the reply to the queue for this
8818be986ccSStephen Cameron 	 * processor.  This seems to give the best I/O throughput.
8828be986ccSStephen Cameron 	 */
8838be986ccSStephen Cameron 	if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
8848be986ccSStephen Cameron 		cp->reply_queue = smp_processor_id() % h->nreply_queues;
8858be986ccSStephen Cameron 	else
8868be986ccSStephen Cameron 		cp->reply_queue = reply_queue % h->nreply_queues;
8878be986ccSStephen Cameron 	/* Set the bits in the address sent down to include:
8888be986ccSStephen Cameron 	 *  - performant mode bit not used in ioaccel mode 2
8898be986ccSStephen Cameron 	 *  - pull count (bits 0-3)
8908be986ccSStephen Cameron 	 *  - command type isn't needed for ioaccel2
8918be986ccSStephen Cameron 	 */
8928be986ccSStephen Cameron 	c->busaddr |= h->ioaccel2_blockFetchTable[0];
8938be986ccSStephen Cameron }
8948be986ccSStephen Cameron 
895c349775eSScott Teel static void set_ioaccel2_performant_mode(struct ctlr_info *h,
89625163bd5SWebb Scales 						struct CommandList *c,
89725163bd5SWebb Scales 						int reply_queue)
898c349775eSScott Teel {
899c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
900c349775eSScott Teel 
90125163bd5SWebb Scales 	/*
90225163bd5SWebb Scales 	 * Tell the controller to post the reply to the queue for this
903c349775eSScott Teel 	 * processor.  This seems to give the best I/O throughput.
904c349775eSScott Teel 	 */
90525163bd5SWebb Scales 	if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
906c349775eSScott Teel 		cp->reply_queue = smp_processor_id() % h->nreply_queues;
90725163bd5SWebb Scales 	else
90825163bd5SWebb Scales 		cp->reply_queue = reply_queue % h->nreply_queues;
90925163bd5SWebb Scales 	/*
91025163bd5SWebb Scales 	 * Set the bits in the address sent down to include:
911c349775eSScott Teel 	 *  - performant mode bit not used in ioaccel mode 2
912c349775eSScott Teel 	 *  - pull count (bits 0-3)
913c349775eSScott Teel 	 *  - command type isn't needed for ioaccel2
914c349775eSScott Teel 	 */
915c349775eSScott Teel 	c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
916c349775eSScott Teel }
917c349775eSScott Teel 
918e85c5974SStephen M. Cameron static int is_firmware_flash_cmd(u8 *cdb)
919e85c5974SStephen M. Cameron {
920e85c5974SStephen M. Cameron 	return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
921e85c5974SStephen M. Cameron }
922e85c5974SStephen M. Cameron 
923e85c5974SStephen M. Cameron /*
924e85c5974SStephen M. Cameron  * During firmware flash, the heartbeat register may not update as frequently
925e85c5974SStephen M. Cameron  * as it should.  So we dial down lockup detection during firmware flash. and
926e85c5974SStephen M. Cameron  * dial it back up when firmware flash completes.
927e85c5974SStephen M. Cameron  */
928e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
929e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
930e85c5974SStephen M. Cameron static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
931e85c5974SStephen M. Cameron 		struct CommandList *c)
932e85c5974SStephen M. Cameron {
933e85c5974SStephen M. Cameron 	if (!is_firmware_flash_cmd(c->Request.CDB))
934e85c5974SStephen M. Cameron 		return;
935e85c5974SStephen M. Cameron 	atomic_inc(&h->firmware_flash_in_progress);
936e85c5974SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
937e85c5974SStephen M. Cameron }
938e85c5974SStephen M. Cameron 
939e85c5974SStephen M. Cameron static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
940e85c5974SStephen M. Cameron 		struct CommandList *c)
941e85c5974SStephen M. Cameron {
942e85c5974SStephen M. Cameron 	if (is_firmware_flash_cmd(c->Request.CDB) &&
943e85c5974SStephen M. Cameron 		atomic_dec_and_test(&h->firmware_flash_in_progress))
944e85c5974SStephen M. Cameron 		h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
945e85c5974SStephen M. Cameron }
946e85c5974SStephen M. Cameron 
94725163bd5SWebb Scales static void __enqueue_cmd_and_start_io(struct ctlr_info *h,
94825163bd5SWebb Scales 	struct CommandList *c, int reply_queue)
9493f5eac3aSStephen M. Cameron {
950c05e8866SStephen Cameron 	dial_down_lockup_detection_during_fw_flash(h, c);
951c05e8866SStephen Cameron 	atomic_inc(&h->commands_outstanding);
952c349775eSScott Teel 	switch (c->cmd_type) {
953c349775eSScott Teel 	case CMD_IOACCEL1:
95425163bd5SWebb Scales 		set_ioaccel1_performant_mode(h, c, reply_queue);
955c05e8866SStephen Cameron 		writel(c->busaddr, h->vaddr + SA5_REQUEST_PORT_OFFSET);
956c349775eSScott Teel 		break;
957c349775eSScott Teel 	case CMD_IOACCEL2:
95825163bd5SWebb Scales 		set_ioaccel2_performant_mode(h, c, reply_queue);
959c05e8866SStephen Cameron 		writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
960c349775eSScott Teel 		break;
9618be986ccSStephen Cameron 	case IOACCEL2_TMF:
9628be986ccSStephen Cameron 		set_ioaccel2_tmf_performant_mode(h, c, reply_queue);
9638be986ccSStephen Cameron 		writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
9648be986ccSStephen Cameron 		break;
965c349775eSScott Teel 	default:
96625163bd5SWebb Scales 		set_performant_mode(h, c, reply_queue);
967f2405db8SDon Brace 		h->access.submit_command(h, c);
9683f5eac3aSStephen M. Cameron 	}
969c05e8866SStephen Cameron }
9703f5eac3aSStephen M. Cameron 
971a58e7e53SWebb Scales static void enqueue_cmd_and_start_io(struct ctlr_info *h, struct CommandList *c)
97225163bd5SWebb Scales {
973a58e7e53SWebb Scales 	if (unlikely(c->abort_pending))
974a58e7e53SWebb Scales 		return finish_cmd(c);
975a58e7e53SWebb Scales 
97625163bd5SWebb Scales 	__enqueue_cmd_and_start_io(h, c, DEFAULT_REPLY_QUEUE);
97725163bd5SWebb Scales }
97825163bd5SWebb Scales 
9793f5eac3aSStephen M. Cameron static inline int is_hba_lunid(unsigned char scsi3addr[])
9803f5eac3aSStephen M. Cameron {
9813f5eac3aSStephen M. Cameron 	return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
9823f5eac3aSStephen M. Cameron }
9833f5eac3aSStephen M. Cameron 
9843f5eac3aSStephen M. Cameron static inline int is_scsi_rev_5(struct ctlr_info *h)
9853f5eac3aSStephen M. Cameron {
9863f5eac3aSStephen M. Cameron 	if (!h->hba_inquiry_data)
9873f5eac3aSStephen M. Cameron 		return 0;
9883f5eac3aSStephen M. Cameron 	if ((h->hba_inquiry_data[2] & 0x07) == 5)
9893f5eac3aSStephen M. Cameron 		return 1;
9903f5eac3aSStephen M. Cameron 	return 0;
9913f5eac3aSStephen M. Cameron }
9923f5eac3aSStephen M. Cameron 
993edd16368SStephen M. Cameron static int hpsa_find_target_lun(struct ctlr_info *h,
994edd16368SStephen M. Cameron 	unsigned char scsi3addr[], int bus, int *target, int *lun)
995edd16368SStephen M. Cameron {
996edd16368SStephen M. Cameron 	/* finds an unused bus, target, lun for a new physical device
997edd16368SStephen M. Cameron 	 * assumes h->devlock is held
998edd16368SStephen M. Cameron 	 */
999edd16368SStephen M. Cameron 	int i, found = 0;
1000cfe5badcSScott Teel 	DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
1001edd16368SStephen M. Cameron 
1002263d9401SAkinobu Mita 	bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
1003edd16368SStephen M. Cameron 
1004edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1005edd16368SStephen M. Cameron 		if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
1006263d9401SAkinobu Mita 			__set_bit(h->dev[i]->target, lun_taken);
1007edd16368SStephen M. Cameron 	}
1008edd16368SStephen M. Cameron 
1009263d9401SAkinobu Mita 	i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
1010263d9401SAkinobu Mita 	if (i < HPSA_MAX_DEVICES) {
1011edd16368SStephen M. Cameron 		/* *bus = 1; */
1012edd16368SStephen M. Cameron 		*target = i;
1013edd16368SStephen M. Cameron 		*lun = 0;
1014edd16368SStephen M. Cameron 		found = 1;
1015edd16368SStephen M. Cameron 	}
1016edd16368SStephen M. Cameron 	return !found;
1017edd16368SStephen M. Cameron }
1018edd16368SStephen M. Cameron 
10190d96ef5fSWebb Scales static inline void hpsa_show_dev_msg(const char *level, struct ctlr_info *h,
10200d96ef5fSWebb Scales 	struct hpsa_scsi_dev_t *dev, char *description)
10210d96ef5fSWebb Scales {
10220d96ef5fSWebb Scales 	dev_printk(level, &h->pdev->dev,
10230d96ef5fSWebb Scales 			"scsi %d:%d:%d:%d: %s %s %.8s %.16s RAID-%s SSDSmartPathCap%c En%c Exp=%d\n",
10240d96ef5fSWebb Scales 			h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
10250d96ef5fSWebb Scales 			description,
10260d96ef5fSWebb Scales 			scsi_device_type(dev->devtype),
10270d96ef5fSWebb Scales 			dev->vendor,
10280d96ef5fSWebb Scales 			dev->model,
10290d96ef5fSWebb Scales 			dev->raid_level > RAID_UNKNOWN ?
10300d96ef5fSWebb Scales 				"RAID-?" : raid_label[dev->raid_level],
10310d96ef5fSWebb Scales 			dev->offload_config ? '+' : '-',
10320d96ef5fSWebb Scales 			dev->offload_enabled ? '+' : '-',
10330d96ef5fSWebb Scales 			dev->expose_state);
10340d96ef5fSWebb Scales }
10350d96ef5fSWebb Scales 
1036edd16368SStephen M. Cameron /* Add an entry into h->dev[] array. */
1037edd16368SStephen M. Cameron static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
1038edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *device,
1039edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *added[], int *nadded)
1040edd16368SStephen M. Cameron {
1041edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
1042edd16368SStephen M. Cameron 	int n = h->ndevices;
1043edd16368SStephen M. Cameron 	int i;
1044edd16368SStephen M. Cameron 	unsigned char addr1[8], addr2[8];
1045edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1046edd16368SStephen M. Cameron 
1047cfe5badcSScott Teel 	if (n >= HPSA_MAX_DEVICES) {
1048edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "too many devices, some will be "
1049edd16368SStephen M. Cameron 			"inaccessible.\n");
1050edd16368SStephen M. Cameron 		return -1;
1051edd16368SStephen M. Cameron 	}
1052edd16368SStephen M. Cameron 
1053edd16368SStephen M. Cameron 	/* physical devices do not have lun or target assigned until now. */
1054edd16368SStephen M. Cameron 	if (device->lun != -1)
1055edd16368SStephen M. Cameron 		/* Logical device, lun is already assigned. */
1056edd16368SStephen M. Cameron 		goto lun_assigned;
1057edd16368SStephen M. Cameron 
1058edd16368SStephen M. Cameron 	/* If this device a non-zero lun of a multi-lun device
1059edd16368SStephen M. Cameron 	 * byte 4 of the 8-byte LUN addr will contain the logical
10602b08b3e9SDon Brace 	 * unit no, zero otherwise.
1061edd16368SStephen M. Cameron 	 */
1062edd16368SStephen M. Cameron 	if (device->scsi3addr[4] == 0) {
1063edd16368SStephen M. Cameron 		/* This is not a non-zero lun of a multi-lun device */
1064edd16368SStephen M. Cameron 		if (hpsa_find_target_lun(h, device->scsi3addr,
1065edd16368SStephen M. Cameron 			device->bus, &device->target, &device->lun) != 0)
1066edd16368SStephen M. Cameron 			return -1;
1067edd16368SStephen M. Cameron 		goto lun_assigned;
1068edd16368SStephen M. Cameron 	}
1069edd16368SStephen M. Cameron 
1070edd16368SStephen M. Cameron 	/* This is a non-zero lun of a multi-lun device.
1071edd16368SStephen M. Cameron 	 * Search through our list and find the device which
1072edd16368SStephen M. Cameron 	 * has the same 8 byte LUN address, excepting byte 4.
1073edd16368SStephen M. Cameron 	 * Assign the same bus and target for this new LUN.
1074edd16368SStephen M. Cameron 	 * Use the logical unit number from the firmware.
1075edd16368SStephen M. Cameron 	 */
1076edd16368SStephen M. Cameron 	memcpy(addr1, device->scsi3addr, 8);
1077edd16368SStephen M. Cameron 	addr1[4] = 0;
1078edd16368SStephen M. Cameron 	for (i = 0; i < n; i++) {
1079edd16368SStephen M. Cameron 		sd = h->dev[i];
1080edd16368SStephen M. Cameron 		memcpy(addr2, sd->scsi3addr, 8);
1081edd16368SStephen M. Cameron 		addr2[4] = 0;
1082edd16368SStephen M. Cameron 		/* differ only in byte 4? */
1083edd16368SStephen M. Cameron 		if (memcmp(addr1, addr2, 8) == 0) {
1084edd16368SStephen M. Cameron 			device->bus = sd->bus;
1085edd16368SStephen M. Cameron 			device->target = sd->target;
1086edd16368SStephen M. Cameron 			device->lun = device->scsi3addr[4];
1087edd16368SStephen M. Cameron 			break;
1088edd16368SStephen M. Cameron 		}
1089edd16368SStephen M. Cameron 	}
1090edd16368SStephen M. Cameron 	if (device->lun == -1) {
1091edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
1092edd16368SStephen M. Cameron 			" suspect firmware bug or unsupported hardware "
1093edd16368SStephen M. Cameron 			"configuration.\n");
1094edd16368SStephen M. Cameron 			return -1;
1095edd16368SStephen M. Cameron 	}
1096edd16368SStephen M. Cameron 
1097edd16368SStephen M. Cameron lun_assigned:
1098edd16368SStephen M. Cameron 
1099edd16368SStephen M. Cameron 	h->dev[n] = device;
1100edd16368SStephen M. Cameron 	h->ndevices++;
1101edd16368SStephen M. Cameron 	added[*nadded] = device;
1102edd16368SStephen M. Cameron 	(*nadded)++;
11030d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_INFO, h, device,
11040d96ef5fSWebb Scales 		device->expose_state & HPSA_SCSI_ADD ? "added" : "masked");
1105a473d86cSRobert Elliott 	device->offload_to_be_enabled = device->offload_enabled;
1106a473d86cSRobert Elliott 	device->offload_enabled = 0;
1107edd16368SStephen M. Cameron 	return 0;
1108edd16368SStephen M. Cameron }
1109edd16368SStephen M. Cameron 
1110bd9244f7SScott Teel /* Update an entry in h->dev[] array. */
1111bd9244f7SScott Teel static void hpsa_scsi_update_entry(struct ctlr_info *h, int hostno,
1112bd9244f7SScott Teel 	int entry, struct hpsa_scsi_dev_t *new_entry)
1113bd9244f7SScott Teel {
1114a473d86cSRobert Elliott 	int offload_enabled;
1115bd9244f7SScott Teel 	/* assumes h->devlock is held */
1116bd9244f7SScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
1117bd9244f7SScott Teel 
1118bd9244f7SScott Teel 	/* Raid level changed. */
1119bd9244f7SScott Teel 	h->dev[entry]->raid_level = new_entry->raid_level;
1120250fb125SStephen M. Cameron 
112103383736SDon Brace 	/* Raid offload parameters changed.  Careful about the ordering. */
112203383736SDon Brace 	if (new_entry->offload_config && new_entry->offload_enabled) {
112303383736SDon Brace 		/*
112403383736SDon Brace 		 * if drive is newly offload_enabled, we want to copy the
112503383736SDon Brace 		 * raid map data first.  If previously offload_enabled and
112603383736SDon Brace 		 * offload_config were set, raid map data had better be
112703383736SDon Brace 		 * the same as it was before.  if raid map data is changed
112803383736SDon Brace 		 * then it had better be the case that
112903383736SDon Brace 		 * h->dev[entry]->offload_enabled is currently 0.
113003383736SDon Brace 		 */
11319fb0de2dSStephen M. Cameron 		h->dev[entry]->raid_map = new_entry->raid_map;
113203383736SDon Brace 		h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
113303383736SDon Brace 	}
1134a3144e0bSJoe Handzik 	if (new_entry->hba_ioaccel_enabled) {
1135a3144e0bSJoe Handzik 		h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
1136a3144e0bSJoe Handzik 		wmb(); /* set ioaccel_handle *before* hba_ioaccel_enabled */
1137a3144e0bSJoe Handzik 	}
1138a3144e0bSJoe Handzik 	h->dev[entry]->hba_ioaccel_enabled = new_entry->hba_ioaccel_enabled;
113903383736SDon Brace 	h->dev[entry]->offload_config = new_entry->offload_config;
114003383736SDon Brace 	h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
114103383736SDon Brace 	h->dev[entry]->queue_depth = new_entry->queue_depth;
1142250fb125SStephen M. Cameron 
114341ce4c35SStephen Cameron 	/*
114441ce4c35SStephen Cameron 	 * We can turn off ioaccel offload now, but need to delay turning
114541ce4c35SStephen Cameron 	 * it on until we can update h->dev[entry]->phys_disk[], but we
114641ce4c35SStephen Cameron 	 * can't do that until all the devices are updated.
114741ce4c35SStephen Cameron 	 */
114841ce4c35SStephen Cameron 	h->dev[entry]->offload_to_be_enabled = new_entry->offload_enabled;
114941ce4c35SStephen Cameron 	if (!new_entry->offload_enabled)
115041ce4c35SStephen Cameron 		h->dev[entry]->offload_enabled = 0;
115141ce4c35SStephen Cameron 
1152a473d86cSRobert Elliott 	offload_enabled = h->dev[entry]->offload_enabled;
1153a473d86cSRobert Elliott 	h->dev[entry]->offload_enabled = h->dev[entry]->offload_to_be_enabled;
11540d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_INFO, h, h->dev[entry], "updated");
1155a473d86cSRobert Elliott 	h->dev[entry]->offload_enabled = offload_enabled;
1156bd9244f7SScott Teel }
1157bd9244f7SScott Teel 
11582a8ccf31SStephen M. Cameron /* Replace an entry from h->dev[] array. */
11592a8ccf31SStephen M. Cameron static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
11602a8ccf31SStephen M. Cameron 	int entry, struct hpsa_scsi_dev_t *new_entry,
11612a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *added[], int *nadded,
11622a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
11632a8ccf31SStephen M. Cameron {
11642a8ccf31SStephen M. Cameron 	/* assumes h->devlock is held */
1165cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
11662a8ccf31SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
11672a8ccf31SStephen M. Cameron 	(*nremoved)++;
116801350d05SStephen M. Cameron 
116901350d05SStephen M. Cameron 	/*
117001350d05SStephen M. Cameron 	 * New physical devices won't have target/lun assigned yet
117101350d05SStephen M. Cameron 	 * so we need to preserve the values in the slot we are replacing.
117201350d05SStephen M. Cameron 	 */
117301350d05SStephen M. Cameron 	if (new_entry->target == -1) {
117401350d05SStephen M. Cameron 		new_entry->target = h->dev[entry]->target;
117501350d05SStephen M. Cameron 		new_entry->lun = h->dev[entry]->lun;
117601350d05SStephen M. Cameron 	}
117701350d05SStephen M. Cameron 
11782a8ccf31SStephen M. Cameron 	h->dev[entry] = new_entry;
11792a8ccf31SStephen M. Cameron 	added[*nadded] = new_entry;
11802a8ccf31SStephen M. Cameron 	(*nadded)++;
11810d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_INFO, h, new_entry, "replaced");
1182a473d86cSRobert Elliott 	new_entry->offload_to_be_enabled = new_entry->offload_enabled;
1183a473d86cSRobert Elliott 	new_entry->offload_enabled = 0;
11842a8ccf31SStephen M. Cameron }
11852a8ccf31SStephen M. Cameron 
1186edd16368SStephen M. Cameron /* Remove an entry from h->dev[] array. */
1187edd16368SStephen M. Cameron static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
1188edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
1189edd16368SStephen M. Cameron {
1190edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
1191edd16368SStephen M. Cameron 	int i;
1192edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1193edd16368SStephen M. Cameron 
1194cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
1195edd16368SStephen M. Cameron 
1196edd16368SStephen M. Cameron 	sd = h->dev[entry];
1197edd16368SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
1198edd16368SStephen M. Cameron 	(*nremoved)++;
1199edd16368SStephen M. Cameron 
1200edd16368SStephen M. Cameron 	for (i = entry; i < h->ndevices-1; i++)
1201edd16368SStephen M. Cameron 		h->dev[i] = h->dev[i+1];
1202edd16368SStephen M. Cameron 	h->ndevices--;
12030d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_INFO, h, sd, "removed");
1204edd16368SStephen M. Cameron }
1205edd16368SStephen M. Cameron 
1206edd16368SStephen M. Cameron #define SCSI3ADDR_EQ(a, b) ( \
1207edd16368SStephen M. Cameron 	(a)[7] == (b)[7] && \
1208edd16368SStephen M. Cameron 	(a)[6] == (b)[6] && \
1209edd16368SStephen M. Cameron 	(a)[5] == (b)[5] && \
1210edd16368SStephen M. Cameron 	(a)[4] == (b)[4] && \
1211edd16368SStephen M. Cameron 	(a)[3] == (b)[3] && \
1212edd16368SStephen M. Cameron 	(a)[2] == (b)[2] && \
1213edd16368SStephen M. Cameron 	(a)[1] == (b)[1] && \
1214edd16368SStephen M. Cameron 	(a)[0] == (b)[0])
1215edd16368SStephen M. Cameron 
1216edd16368SStephen M. Cameron static void fixup_botched_add(struct ctlr_info *h,
1217edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *added)
1218edd16368SStephen M. Cameron {
1219edd16368SStephen M. Cameron 	/* called when scsi_add_device fails in order to re-adjust
1220edd16368SStephen M. Cameron 	 * h->dev[] to match the mid layer's view.
1221edd16368SStephen M. Cameron 	 */
1222edd16368SStephen M. Cameron 	unsigned long flags;
1223edd16368SStephen M. Cameron 	int i, j;
1224edd16368SStephen M. Cameron 
1225edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
1226edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1227edd16368SStephen M. Cameron 		if (h->dev[i] == added) {
1228edd16368SStephen M. Cameron 			for (j = i; j < h->ndevices-1; j++)
1229edd16368SStephen M. Cameron 				h->dev[j] = h->dev[j+1];
1230edd16368SStephen M. Cameron 			h->ndevices--;
1231edd16368SStephen M. Cameron 			break;
1232edd16368SStephen M. Cameron 		}
1233edd16368SStephen M. Cameron 	}
1234edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
1235edd16368SStephen M. Cameron 	kfree(added);
1236edd16368SStephen M. Cameron }
1237edd16368SStephen M. Cameron 
1238edd16368SStephen M. Cameron static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
1239edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev2)
1240edd16368SStephen M. Cameron {
1241edd16368SStephen M. Cameron 	/* we compare everything except lun and target as these
1242edd16368SStephen M. Cameron 	 * are not yet assigned.  Compare parts likely
1243edd16368SStephen M. Cameron 	 * to differ first
1244edd16368SStephen M. Cameron 	 */
1245edd16368SStephen M. Cameron 	if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
1246edd16368SStephen M. Cameron 		sizeof(dev1->scsi3addr)) != 0)
1247edd16368SStephen M. Cameron 		return 0;
1248edd16368SStephen M. Cameron 	if (memcmp(dev1->device_id, dev2->device_id,
1249edd16368SStephen M. Cameron 		sizeof(dev1->device_id)) != 0)
1250edd16368SStephen M. Cameron 		return 0;
1251edd16368SStephen M. Cameron 	if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
1252edd16368SStephen M. Cameron 		return 0;
1253edd16368SStephen M. Cameron 	if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
1254edd16368SStephen M. Cameron 		return 0;
1255edd16368SStephen M. Cameron 	if (dev1->devtype != dev2->devtype)
1256edd16368SStephen M. Cameron 		return 0;
1257edd16368SStephen M. Cameron 	if (dev1->bus != dev2->bus)
1258edd16368SStephen M. Cameron 		return 0;
1259edd16368SStephen M. Cameron 	return 1;
1260edd16368SStephen M. Cameron }
1261edd16368SStephen M. Cameron 
1262bd9244f7SScott Teel static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
1263bd9244f7SScott Teel 	struct hpsa_scsi_dev_t *dev2)
1264bd9244f7SScott Teel {
1265bd9244f7SScott Teel 	/* Device attributes that can change, but don't mean
1266bd9244f7SScott Teel 	 * that the device is a different device, nor that the OS
1267bd9244f7SScott Teel 	 * needs to be told anything about the change.
1268bd9244f7SScott Teel 	 */
1269bd9244f7SScott Teel 	if (dev1->raid_level != dev2->raid_level)
1270bd9244f7SScott Teel 		return 1;
1271250fb125SStephen M. Cameron 	if (dev1->offload_config != dev2->offload_config)
1272250fb125SStephen M. Cameron 		return 1;
1273250fb125SStephen M. Cameron 	if (dev1->offload_enabled != dev2->offload_enabled)
1274250fb125SStephen M. Cameron 		return 1;
127503383736SDon Brace 	if (dev1->queue_depth != dev2->queue_depth)
127603383736SDon Brace 		return 1;
1277bd9244f7SScott Teel 	return 0;
1278bd9244f7SScott Teel }
1279bd9244f7SScott Teel 
1280edd16368SStephen M. Cameron /* Find needle in haystack.  If exact match found, return DEVICE_SAME,
1281edd16368SStephen M. Cameron  * and return needle location in *index.  If scsi3addr matches, but not
1282edd16368SStephen M. Cameron  * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
1283bd9244f7SScott Teel  * location in *index.
1284bd9244f7SScott Teel  * In the case of a minor device attribute change, such as RAID level, just
1285bd9244f7SScott Teel  * return DEVICE_UPDATED, along with the updated device's location in index.
1286bd9244f7SScott Teel  * If needle not found, return DEVICE_NOT_FOUND.
1287edd16368SStephen M. Cameron  */
1288edd16368SStephen M. Cameron static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
1289edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *haystack[], int haystack_size,
1290edd16368SStephen M. Cameron 	int *index)
1291edd16368SStephen M. Cameron {
1292edd16368SStephen M. Cameron 	int i;
1293edd16368SStephen M. Cameron #define DEVICE_NOT_FOUND 0
1294edd16368SStephen M. Cameron #define DEVICE_CHANGED 1
1295edd16368SStephen M. Cameron #define DEVICE_SAME 2
1296bd9244f7SScott Teel #define DEVICE_UPDATED 3
1297edd16368SStephen M. Cameron 	for (i = 0; i < haystack_size; i++) {
129823231048SStephen M. Cameron 		if (haystack[i] == NULL) /* previously removed. */
129923231048SStephen M. Cameron 			continue;
1300edd16368SStephen M. Cameron 		if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
1301edd16368SStephen M. Cameron 			*index = i;
1302bd9244f7SScott Teel 			if (device_is_the_same(needle, haystack[i])) {
1303bd9244f7SScott Teel 				if (device_updated(needle, haystack[i]))
1304bd9244f7SScott Teel 					return DEVICE_UPDATED;
1305edd16368SStephen M. Cameron 				return DEVICE_SAME;
1306bd9244f7SScott Teel 			} else {
13079846590eSStephen M. Cameron 				/* Keep offline devices offline */
13089846590eSStephen M. Cameron 				if (needle->volume_offline)
13099846590eSStephen M. Cameron 					return DEVICE_NOT_FOUND;
1310edd16368SStephen M. Cameron 				return DEVICE_CHANGED;
1311edd16368SStephen M. Cameron 			}
1312edd16368SStephen M. Cameron 		}
1313bd9244f7SScott Teel 	}
1314edd16368SStephen M. Cameron 	*index = -1;
1315edd16368SStephen M. Cameron 	return DEVICE_NOT_FOUND;
1316edd16368SStephen M. Cameron }
1317edd16368SStephen M. Cameron 
13189846590eSStephen M. Cameron static void hpsa_monitor_offline_device(struct ctlr_info *h,
13199846590eSStephen M. Cameron 					unsigned char scsi3addr[])
13209846590eSStephen M. Cameron {
13219846590eSStephen M. Cameron 	struct offline_device_entry *device;
13229846590eSStephen M. Cameron 	unsigned long flags;
13239846590eSStephen M. Cameron 
13249846590eSStephen M. Cameron 	/* Check to see if device is already on the list */
13259846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
13269846590eSStephen M. Cameron 	list_for_each_entry(device, &h->offline_device_list, offline_list) {
13279846590eSStephen M. Cameron 		if (memcmp(device->scsi3addr, scsi3addr,
13289846590eSStephen M. Cameron 			sizeof(device->scsi3addr)) == 0) {
13299846590eSStephen M. Cameron 			spin_unlock_irqrestore(&h->offline_device_lock, flags);
13309846590eSStephen M. Cameron 			return;
13319846590eSStephen M. Cameron 		}
13329846590eSStephen M. Cameron 	}
13339846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
13349846590eSStephen M. Cameron 
13359846590eSStephen M. Cameron 	/* Device is not on the list, add it. */
13369846590eSStephen M. Cameron 	device = kmalloc(sizeof(*device), GFP_KERNEL);
13379846590eSStephen M. Cameron 	if (!device) {
13389846590eSStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in %s\n", __func__);
13399846590eSStephen M. Cameron 		return;
13409846590eSStephen M. Cameron 	}
13419846590eSStephen M. Cameron 	memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
13429846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
13439846590eSStephen M. Cameron 	list_add_tail(&device->offline_list, &h->offline_device_list);
13449846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
13459846590eSStephen M. Cameron }
13469846590eSStephen M. Cameron 
13479846590eSStephen M. Cameron /* Print a message explaining various offline volume states */
13489846590eSStephen M. Cameron static void hpsa_show_volume_status(struct ctlr_info *h,
13499846590eSStephen M. Cameron 	struct hpsa_scsi_dev_t *sd)
13509846590eSStephen M. Cameron {
13519846590eSStephen M. Cameron 	if (sd->volume_offline == HPSA_VPD_LV_STATUS_UNSUPPORTED)
13529846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13539846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume status is not available through vital product data pages.\n",
13549846590eSStephen M. Cameron 			h->scsi_host->host_no,
13559846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13569846590eSStephen M. Cameron 	switch (sd->volume_offline) {
13579846590eSStephen M. Cameron 	case HPSA_LV_OK:
13589846590eSStephen M. Cameron 		break;
13599846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ERASE:
13609846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13619846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing background erase process.\n",
13629846590eSStephen M. Cameron 			h->scsi_host->host_no,
13639846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13649846590eSStephen M. Cameron 		break;
13659846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_RPI:
13669846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13679846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing rapid parity initialization process.\n",
13689846590eSStephen M. Cameron 			h->scsi_host->host_no,
13699846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13709846590eSStephen M. Cameron 		break;
13719846590eSStephen M. Cameron 	case HPSA_LV_PENDING_RPI:
13729846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13739846590eSStephen M. Cameron 				"C%d:B%d:T%d:L%d Volume is queued for rapid parity initialization process.\n",
13749846590eSStephen M. Cameron 				h->scsi_host->host_no,
13759846590eSStephen M. Cameron 				sd->bus, sd->target, sd->lun);
13769846590eSStephen M. Cameron 		break;
13779846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_NO_KEY:
13789846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13799846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because key is not present.\n",
13809846590eSStephen M. Cameron 			h->scsi_host->host_no,
13819846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13829846590eSStephen M. Cameron 		break;
13839846590eSStephen M. Cameron 	case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
13849846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13859846590eSStephen 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",
13869846590eSStephen M. Cameron 			h->scsi_host->host_no,
13879846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13889846590eSStephen M. Cameron 		break;
13899846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION:
13909846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13919846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing encryption process.\n",
13929846590eSStephen M. Cameron 			h->scsi_host->host_no,
13939846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13949846590eSStephen M. Cameron 		break;
13959846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
13969846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13979846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing encryption re-keying process.\n",
13989846590eSStephen M. Cameron 			h->scsi_host->host_no,
13999846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
14009846590eSStephen M. Cameron 		break;
14019846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
14029846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
14039846590eSStephen 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",
14049846590eSStephen M. Cameron 			h->scsi_host->host_no,
14059846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
14069846590eSStephen M. Cameron 		break;
14079846590eSStephen M. Cameron 	case HPSA_LV_PENDING_ENCRYPTION:
14089846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
14099846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is pending migration to encrypted state, but process has not started.\n",
14109846590eSStephen M. Cameron 			h->scsi_host->host_no,
14119846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
14129846590eSStephen M. Cameron 		break;
14139846590eSStephen M. Cameron 	case HPSA_LV_PENDING_ENCRYPTION_REKEYING:
14149846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
14159846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is encrypted and is pending encryption rekeying.\n",
14169846590eSStephen M. Cameron 			h->scsi_host->host_no,
14179846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
14189846590eSStephen M. Cameron 		break;
14199846590eSStephen M. Cameron 	}
14209846590eSStephen M. Cameron }
14219846590eSStephen M. Cameron 
142203383736SDon Brace /*
142303383736SDon Brace  * Figure the list of physical drive pointers for a logical drive with
142403383736SDon Brace  * raid offload configured.
142503383736SDon Brace  */
142603383736SDon Brace static void hpsa_figure_phys_disk_ptrs(struct ctlr_info *h,
142703383736SDon Brace 				struct hpsa_scsi_dev_t *dev[], int ndevices,
142803383736SDon Brace 				struct hpsa_scsi_dev_t *logical_drive)
142903383736SDon Brace {
143003383736SDon Brace 	struct raid_map_data *map = &logical_drive->raid_map;
143103383736SDon Brace 	struct raid_map_disk_data *dd = &map->data[0];
143203383736SDon Brace 	int i, j;
143303383736SDon Brace 	int total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
143403383736SDon Brace 				le16_to_cpu(map->metadata_disks_per_row);
143503383736SDon Brace 	int nraid_map_entries = le16_to_cpu(map->row_cnt) *
143603383736SDon Brace 				le16_to_cpu(map->layout_map_count) *
143703383736SDon Brace 				total_disks_per_row;
143803383736SDon Brace 	int nphys_disk = le16_to_cpu(map->layout_map_count) *
143903383736SDon Brace 				total_disks_per_row;
144003383736SDon Brace 	int qdepth;
144103383736SDon Brace 
144203383736SDon Brace 	if (nraid_map_entries > RAID_MAP_MAX_ENTRIES)
144303383736SDon Brace 		nraid_map_entries = RAID_MAP_MAX_ENTRIES;
144403383736SDon Brace 
144503383736SDon Brace 	qdepth = 0;
144603383736SDon Brace 	for (i = 0; i < nraid_map_entries; i++) {
144703383736SDon Brace 		logical_drive->phys_disk[i] = NULL;
144803383736SDon Brace 		if (!logical_drive->offload_config)
144903383736SDon Brace 			continue;
145003383736SDon Brace 		for (j = 0; j < ndevices; j++) {
145103383736SDon Brace 			if (dev[j]->devtype != TYPE_DISK)
145203383736SDon Brace 				continue;
145303383736SDon Brace 			if (is_logical_dev_addr_mode(dev[j]->scsi3addr))
145403383736SDon Brace 				continue;
145503383736SDon Brace 			if (dev[j]->ioaccel_handle != dd[i].ioaccel_handle)
145603383736SDon Brace 				continue;
145703383736SDon Brace 
145803383736SDon Brace 			logical_drive->phys_disk[i] = dev[j];
145903383736SDon Brace 			if (i < nphys_disk)
146003383736SDon Brace 				qdepth = min(h->nr_cmds, qdepth +
146103383736SDon Brace 				    logical_drive->phys_disk[i]->queue_depth);
146203383736SDon Brace 			break;
146303383736SDon Brace 		}
146403383736SDon Brace 
146503383736SDon Brace 		/*
146603383736SDon Brace 		 * This can happen if a physical drive is removed and
146703383736SDon Brace 		 * the logical drive is degraded.  In that case, the RAID
146803383736SDon Brace 		 * map data will refer to a physical disk which isn't actually
146903383736SDon Brace 		 * present.  And in that case offload_enabled should already
147003383736SDon Brace 		 * be 0, but we'll turn it off here just in case
147103383736SDon Brace 		 */
147203383736SDon Brace 		if (!logical_drive->phys_disk[i]) {
147303383736SDon Brace 			logical_drive->offload_enabled = 0;
147441ce4c35SStephen Cameron 			logical_drive->offload_to_be_enabled = 0;
147541ce4c35SStephen Cameron 			logical_drive->queue_depth = 8;
147603383736SDon Brace 		}
147703383736SDon Brace 	}
147803383736SDon Brace 	if (nraid_map_entries)
147903383736SDon Brace 		/*
148003383736SDon Brace 		 * This is correct for reads, too high for full stripe writes,
148103383736SDon Brace 		 * way too high for partial stripe writes
148203383736SDon Brace 		 */
148303383736SDon Brace 		logical_drive->queue_depth = qdepth;
148403383736SDon Brace 	else
148503383736SDon Brace 		logical_drive->queue_depth = h->nr_cmds;
148603383736SDon Brace }
148703383736SDon Brace 
148803383736SDon Brace static void hpsa_update_log_drive_phys_drive_ptrs(struct ctlr_info *h,
148903383736SDon Brace 				struct hpsa_scsi_dev_t *dev[], int ndevices)
149003383736SDon Brace {
149103383736SDon Brace 	int i;
149203383736SDon Brace 
149303383736SDon Brace 	for (i = 0; i < ndevices; i++) {
149403383736SDon Brace 		if (dev[i]->devtype != TYPE_DISK)
149503383736SDon Brace 			continue;
149603383736SDon Brace 		if (!is_logical_dev_addr_mode(dev[i]->scsi3addr))
149703383736SDon Brace 			continue;
149841ce4c35SStephen Cameron 
149941ce4c35SStephen Cameron 		/*
150041ce4c35SStephen Cameron 		 * If offload is currently enabled, the RAID map and
150141ce4c35SStephen Cameron 		 * phys_disk[] assignment *better* not be changing
150241ce4c35SStephen Cameron 		 * and since it isn't changing, we do not need to
150341ce4c35SStephen Cameron 		 * update it.
150441ce4c35SStephen Cameron 		 */
150541ce4c35SStephen Cameron 		if (dev[i]->offload_enabled)
150641ce4c35SStephen Cameron 			continue;
150741ce4c35SStephen Cameron 
150803383736SDon Brace 		hpsa_figure_phys_disk_ptrs(h, dev, ndevices, dev[i]);
150903383736SDon Brace 	}
151003383736SDon Brace }
151103383736SDon Brace 
15124967bd3eSStephen M. Cameron static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
1513edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd[], int nsds)
1514edd16368SStephen M. Cameron {
1515edd16368SStephen M. Cameron 	/* sd contains scsi3 addresses and devtypes, and inquiry
1516edd16368SStephen M. Cameron 	 * data.  This function takes what's in sd to be the current
1517edd16368SStephen M. Cameron 	 * reality and updates h->dev[] to reflect that reality.
1518edd16368SStephen M. Cameron 	 */
1519edd16368SStephen M. Cameron 	int i, entry, device_change, changes = 0;
1520edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *csd;
1521edd16368SStephen M. Cameron 	unsigned long flags;
1522edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **added, **removed;
1523edd16368SStephen M. Cameron 	int nadded, nremoved;
1524edd16368SStephen M. Cameron 	struct Scsi_Host *sh = NULL;
1525edd16368SStephen M. Cameron 
1526cfe5badcSScott Teel 	added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
1527cfe5badcSScott Teel 	removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
1528edd16368SStephen M. Cameron 
1529edd16368SStephen M. Cameron 	if (!added || !removed) {
1530edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in "
1531edd16368SStephen M. Cameron 			"adjust_hpsa_scsi_table\n");
1532edd16368SStephen M. Cameron 		goto free_and_out;
1533edd16368SStephen M. Cameron 	}
1534edd16368SStephen M. Cameron 
1535edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1536edd16368SStephen M. Cameron 
1537edd16368SStephen M. Cameron 	/* find any devices in h->dev[] that are not in
1538edd16368SStephen M. Cameron 	 * sd[] and remove them from h->dev[], and for any
1539edd16368SStephen M. Cameron 	 * devices which have changed, remove the old device
1540edd16368SStephen M. Cameron 	 * info and add the new device info.
1541bd9244f7SScott Teel 	 * If minor device attributes change, just update
1542bd9244f7SScott Teel 	 * the existing device structure.
1543edd16368SStephen M. Cameron 	 */
1544edd16368SStephen M. Cameron 	i = 0;
1545edd16368SStephen M. Cameron 	nremoved = 0;
1546edd16368SStephen M. Cameron 	nadded = 0;
1547edd16368SStephen M. Cameron 	while (i < h->ndevices) {
1548edd16368SStephen M. Cameron 		csd = h->dev[i];
1549edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
1550edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
1551edd16368SStephen M. Cameron 			changes++;
1552edd16368SStephen M. Cameron 			hpsa_scsi_remove_entry(h, hostno, i,
1553edd16368SStephen M. Cameron 				removed, &nremoved);
1554edd16368SStephen M. Cameron 			continue; /* remove ^^^, hence i not incremented */
1555edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
1556edd16368SStephen M. Cameron 			changes++;
15572a8ccf31SStephen M. Cameron 			hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
15582a8ccf31SStephen M. Cameron 				added, &nadded, removed, &nremoved);
1559c7f172dcSStephen M. Cameron 			/* Set it to NULL to prevent it from being freed
1560c7f172dcSStephen M. Cameron 			 * at the bottom of hpsa_update_scsi_devices()
1561c7f172dcSStephen M. Cameron 			 */
1562c7f172dcSStephen M. Cameron 			sd[entry] = NULL;
1563bd9244f7SScott Teel 		} else if (device_change == DEVICE_UPDATED) {
1564bd9244f7SScott Teel 			hpsa_scsi_update_entry(h, hostno, i, sd[entry]);
1565edd16368SStephen M. Cameron 		}
1566edd16368SStephen M. Cameron 		i++;
1567edd16368SStephen M. Cameron 	}
1568edd16368SStephen M. Cameron 
1569edd16368SStephen M. Cameron 	/* Now, make sure every device listed in sd[] is also
1570edd16368SStephen M. Cameron 	 * listed in h->dev[], adding them if they aren't found
1571edd16368SStephen M. Cameron 	 */
1572edd16368SStephen M. Cameron 
1573edd16368SStephen M. Cameron 	for (i = 0; i < nsds; i++) {
1574edd16368SStephen M. Cameron 		if (!sd[i]) /* if already added above. */
1575edd16368SStephen M. Cameron 			continue;
15769846590eSStephen M. Cameron 
15779846590eSStephen M. Cameron 		/* Don't add devices which are NOT READY, FORMAT IN PROGRESS
15789846590eSStephen M. Cameron 		 * as the SCSI mid-layer does not handle such devices well.
15799846590eSStephen M. Cameron 		 * It relentlessly loops sending TUR at 3Hz, then READ(10)
15809846590eSStephen M. Cameron 		 * at 160Hz, and prevents the system from coming up.
15819846590eSStephen M. Cameron 		 */
15829846590eSStephen M. Cameron 		if (sd[i]->volume_offline) {
15839846590eSStephen M. Cameron 			hpsa_show_volume_status(h, sd[i]);
15840d96ef5fSWebb Scales 			hpsa_show_dev_msg(KERN_INFO, h, sd[i], "offline");
15859846590eSStephen M. Cameron 			continue;
15869846590eSStephen M. Cameron 		}
15879846590eSStephen M. Cameron 
1588edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1589edd16368SStephen M. Cameron 					h->ndevices, &entry);
1590edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
1591edd16368SStephen M. Cameron 			changes++;
1592edd16368SStephen M. Cameron 			if (hpsa_scsi_add_entry(h, hostno, sd[i],
1593edd16368SStephen M. Cameron 				added, &nadded) != 0)
1594edd16368SStephen M. Cameron 				break;
1595edd16368SStephen M. Cameron 			sd[i] = NULL; /* prevent from being freed later. */
1596edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
1597edd16368SStephen M. Cameron 			/* should never happen... */
1598edd16368SStephen M. Cameron 			changes++;
1599edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev,
1600edd16368SStephen M. Cameron 				"device unexpectedly changed.\n");
1601edd16368SStephen M. Cameron 			/* but if it does happen, we just ignore that device */
1602edd16368SStephen M. Cameron 		}
1603edd16368SStephen M. Cameron 	}
160441ce4c35SStephen Cameron 	hpsa_update_log_drive_phys_drive_ptrs(h, h->dev, h->ndevices);
160541ce4c35SStephen Cameron 
160641ce4c35SStephen Cameron 	/* Now that h->dev[]->phys_disk[] is coherent, we can enable
160741ce4c35SStephen Cameron 	 * any logical drives that need it enabled.
160841ce4c35SStephen Cameron 	 */
160941ce4c35SStephen Cameron 	for (i = 0; i < h->ndevices; i++)
161041ce4c35SStephen Cameron 		h->dev[i]->offload_enabled = h->dev[i]->offload_to_be_enabled;
161141ce4c35SStephen Cameron 
1612edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1613edd16368SStephen M. Cameron 
16149846590eSStephen M. Cameron 	/* Monitor devices which are in one of several NOT READY states to be
16159846590eSStephen M. Cameron 	 * brought online later. This must be done without holding h->devlock,
16169846590eSStephen M. Cameron 	 * so don't touch h->dev[]
16179846590eSStephen M. Cameron 	 */
16189846590eSStephen M. Cameron 	for (i = 0; i < nsds; i++) {
16199846590eSStephen M. Cameron 		if (!sd[i]) /* if already added above. */
16209846590eSStephen M. Cameron 			continue;
16219846590eSStephen M. Cameron 		if (sd[i]->volume_offline)
16229846590eSStephen M. Cameron 			hpsa_monitor_offline_device(h, sd[i]->scsi3addr);
16239846590eSStephen M. Cameron 	}
16249846590eSStephen M. Cameron 
1625edd16368SStephen M. Cameron 	/* Don't notify scsi mid layer of any changes the first time through
1626edd16368SStephen M. Cameron 	 * (or if there are no changes) scsi_scan_host will do it later the
1627edd16368SStephen M. Cameron 	 * first time through.
1628edd16368SStephen M. Cameron 	 */
1629edd16368SStephen M. Cameron 	if (hostno == -1 || !changes)
1630edd16368SStephen M. Cameron 		goto free_and_out;
1631edd16368SStephen M. Cameron 
1632edd16368SStephen M. Cameron 	sh = h->scsi_host;
1633edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any removed devices */
1634edd16368SStephen M. Cameron 	for (i = 0; i < nremoved; i++) {
163541ce4c35SStephen Cameron 		if (removed[i]->expose_state & HPSA_SCSI_ADD) {
1636edd16368SStephen M. Cameron 			struct scsi_device *sdev =
1637edd16368SStephen M. Cameron 				scsi_device_lookup(sh, removed[i]->bus,
1638edd16368SStephen M. Cameron 					removed[i]->target, removed[i]->lun);
1639edd16368SStephen M. Cameron 			if (sdev != NULL) {
1640edd16368SStephen M. Cameron 				scsi_remove_device(sdev);
1641edd16368SStephen M. Cameron 				scsi_device_put(sdev);
1642edd16368SStephen M. Cameron 			} else {
164341ce4c35SStephen Cameron 				/*
164441ce4c35SStephen Cameron 				 * We don't expect to get here.
1645edd16368SStephen M. Cameron 				 * future cmds to this device will get selection
1646edd16368SStephen M. Cameron 				 * timeout as if the device was gone.
1647edd16368SStephen M. Cameron 				 */
16480d96ef5fSWebb Scales 				hpsa_show_dev_msg(KERN_WARNING, h, removed[i],
16490d96ef5fSWebb Scales 					"didn't find device for removal.");
1650edd16368SStephen M. Cameron 			}
165141ce4c35SStephen Cameron 		}
1652edd16368SStephen M. Cameron 		kfree(removed[i]);
1653edd16368SStephen M. Cameron 		removed[i] = NULL;
1654edd16368SStephen M. Cameron 	}
1655edd16368SStephen M. Cameron 
1656edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any added devices */
1657edd16368SStephen M. Cameron 	for (i = 0; i < nadded; i++) {
165841ce4c35SStephen Cameron 		if (!(added[i]->expose_state & HPSA_SCSI_ADD))
165941ce4c35SStephen Cameron 			continue;
1660edd16368SStephen M. Cameron 		if (scsi_add_device(sh, added[i]->bus,
1661edd16368SStephen M. Cameron 			added[i]->target, added[i]->lun) == 0)
1662edd16368SStephen M. Cameron 			continue;
16630d96ef5fSWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, added[i],
16640d96ef5fSWebb Scales 					"addition failed, device not added.");
1665edd16368SStephen M. Cameron 		/* now we have to remove it from h->dev,
1666edd16368SStephen M. Cameron 		 * since it didn't get added to scsi mid layer
1667edd16368SStephen M. Cameron 		 */
1668edd16368SStephen M. Cameron 		fixup_botched_add(h, added[i]);
1669105a3dbcSRobert Elliott 		added[i] = NULL;
1670edd16368SStephen M. Cameron 	}
1671edd16368SStephen M. Cameron 
1672edd16368SStephen M. Cameron free_and_out:
1673edd16368SStephen M. Cameron 	kfree(added);
1674edd16368SStephen M. Cameron 	kfree(removed);
1675edd16368SStephen M. Cameron }
1676edd16368SStephen M. Cameron 
1677edd16368SStephen M. Cameron /*
16789e03aa2fSJoe Perches  * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
1679edd16368SStephen M. Cameron  * Assume's h->devlock is held.
1680edd16368SStephen M. Cameron  */
1681edd16368SStephen M. Cameron static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1682edd16368SStephen M. Cameron 	int bus, int target, int lun)
1683edd16368SStephen M. Cameron {
1684edd16368SStephen M. Cameron 	int i;
1685edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1686edd16368SStephen M. Cameron 
1687edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1688edd16368SStephen M. Cameron 		sd = h->dev[i];
1689edd16368SStephen M. Cameron 		if (sd->bus == bus && sd->target == target && sd->lun == lun)
1690edd16368SStephen M. Cameron 			return sd;
1691edd16368SStephen M. Cameron 	}
1692edd16368SStephen M. Cameron 	return NULL;
1693edd16368SStephen M. Cameron }
1694edd16368SStephen M. Cameron 
1695edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev)
1696edd16368SStephen M. Cameron {
1697edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1698edd16368SStephen M. Cameron 	unsigned long flags;
1699edd16368SStephen M. Cameron 	struct ctlr_info *h;
1700edd16368SStephen M. Cameron 
1701edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
1702edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1703edd16368SStephen M. Cameron 	sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1704edd16368SStephen M. Cameron 		sdev_id(sdev), sdev->lun);
170541ce4c35SStephen Cameron 	if (likely(sd)) {
170603383736SDon Brace 		atomic_set(&sd->ioaccel_cmds_out, 0);
170741ce4c35SStephen Cameron 		sdev->hostdata = (sd->expose_state & HPSA_SCSI_ADD) ? sd : NULL;
170841ce4c35SStephen Cameron 	} else
170941ce4c35SStephen Cameron 		sdev->hostdata = NULL;
1710edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1711edd16368SStephen M. Cameron 	return 0;
1712edd16368SStephen M. Cameron }
1713edd16368SStephen M. Cameron 
171441ce4c35SStephen Cameron /* configure scsi device based on internal per-device structure */
171541ce4c35SStephen Cameron static int hpsa_slave_configure(struct scsi_device *sdev)
171641ce4c35SStephen Cameron {
171741ce4c35SStephen Cameron 	struct hpsa_scsi_dev_t *sd;
171841ce4c35SStephen Cameron 	int queue_depth;
171941ce4c35SStephen Cameron 
172041ce4c35SStephen Cameron 	sd = sdev->hostdata;
172141ce4c35SStephen Cameron 	sdev->no_uld_attach = !sd || !(sd->expose_state & HPSA_ULD_ATTACH);
172241ce4c35SStephen Cameron 
172341ce4c35SStephen Cameron 	if (sd)
172441ce4c35SStephen Cameron 		queue_depth = sd->queue_depth != 0 ?
172541ce4c35SStephen Cameron 			sd->queue_depth : sdev->host->can_queue;
172641ce4c35SStephen Cameron 	else
172741ce4c35SStephen Cameron 		queue_depth = sdev->host->can_queue;
172841ce4c35SStephen Cameron 
172941ce4c35SStephen Cameron 	scsi_change_queue_depth(sdev, queue_depth);
173041ce4c35SStephen Cameron 
173141ce4c35SStephen Cameron 	return 0;
173241ce4c35SStephen Cameron }
173341ce4c35SStephen Cameron 
1734edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev)
1735edd16368SStephen M. Cameron {
1736bcc44255SStephen M. Cameron 	/* nothing to do. */
1737edd16368SStephen M. Cameron }
1738edd16368SStephen M. Cameron 
1739d9a729f3SWebb Scales static void hpsa_free_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
1740d9a729f3SWebb Scales {
1741d9a729f3SWebb Scales 	int i;
1742d9a729f3SWebb Scales 
1743d9a729f3SWebb Scales 	if (!h->ioaccel2_cmd_sg_list)
1744d9a729f3SWebb Scales 		return;
1745d9a729f3SWebb Scales 	for (i = 0; i < h->nr_cmds; i++) {
1746d9a729f3SWebb Scales 		kfree(h->ioaccel2_cmd_sg_list[i]);
1747d9a729f3SWebb Scales 		h->ioaccel2_cmd_sg_list[i] = NULL;
1748d9a729f3SWebb Scales 	}
1749d9a729f3SWebb Scales 	kfree(h->ioaccel2_cmd_sg_list);
1750d9a729f3SWebb Scales 	h->ioaccel2_cmd_sg_list = NULL;
1751d9a729f3SWebb Scales }
1752d9a729f3SWebb Scales 
1753d9a729f3SWebb Scales static int hpsa_allocate_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
1754d9a729f3SWebb Scales {
1755d9a729f3SWebb Scales 	int i;
1756d9a729f3SWebb Scales 
1757d9a729f3SWebb Scales 	if (h->chainsize <= 0)
1758d9a729f3SWebb Scales 		return 0;
1759d9a729f3SWebb Scales 
1760d9a729f3SWebb Scales 	h->ioaccel2_cmd_sg_list =
1761d9a729f3SWebb Scales 		kzalloc(sizeof(*h->ioaccel2_cmd_sg_list) * h->nr_cmds,
1762d9a729f3SWebb Scales 					GFP_KERNEL);
1763d9a729f3SWebb Scales 	if (!h->ioaccel2_cmd_sg_list)
1764d9a729f3SWebb Scales 		return -ENOMEM;
1765d9a729f3SWebb Scales 	for (i = 0; i < h->nr_cmds; i++) {
1766d9a729f3SWebb Scales 		h->ioaccel2_cmd_sg_list[i] =
1767d9a729f3SWebb Scales 			kmalloc(sizeof(*h->ioaccel2_cmd_sg_list[i]) *
1768d9a729f3SWebb Scales 					h->maxsgentries, GFP_KERNEL);
1769d9a729f3SWebb Scales 		if (!h->ioaccel2_cmd_sg_list[i])
1770d9a729f3SWebb Scales 			goto clean;
1771d9a729f3SWebb Scales 	}
1772d9a729f3SWebb Scales 	return 0;
1773d9a729f3SWebb Scales 
1774d9a729f3SWebb Scales clean:
1775d9a729f3SWebb Scales 	hpsa_free_ioaccel2_sg_chain_blocks(h);
1776d9a729f3SWebb Scales 	return -ENOMEM;
1777d9a729f3SWebb Scales }
1778d9a729f3SWebb Scales 
177933a2ffceSStephen M. Cameron static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
178033a2ffceSStephen M. Cameron {
178133a2ffceSStephen M. Cameron 	int i;
178233a2ffceSStephen M. Cameron 
178333a2ffceSStephen M. Cameron 	if (!h->cmd_sg_list)
178433a2ffceSStephen M. Cameron 		return;
178533a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
178633a2ffceSStephen M. Cameron 		kfree(h->cmd_sg_list[i]);
178733a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = NULL;
178833a2ffceSStephen M. Cameron 	}
178933a2ffceSStephen M. Cameron 	kfree(h->cmd_sg_list);
179033a2ffceSStephen M. Cameron 	h->cmd_sg_list = NULL;
179133a2ffceSStephen M. Cameron }
179233a2ffceSStephen M. Cameron 
1793105a3dbcSRobert Elliott static int hpsa_alloc_sg_chain_blocks(struct ctlr_info *h)
179433a2ffceSStephen M. Cameron {
179533a2ffceSStephen M. Cameron 	int i;
179633a2ffceSStephen M. Cameron 
179733a2ffceSStephen M. Cameron 	if (h->chainsize <= 0)
179833a2ffceSStephen M. Cameron 		return 0;
179933a2ffceSStephen M. Cameron 
180033a2ffceSStephen M. Cameron 	h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
180133a2ffceSStephen M. Cameron 				GFP_KERNEL);
18023d4e6af8SRobert Elliott 	if (!h->cmd_sg_list) {
18033d4e6af8SRobert Elliott 		dev_err(&h->pdev->dev, "Failed to allocate SG list\n");
180433a2ffceSStephen M. Cameron 		return -ENOMEM;
18053d4e6af8SRobert Elliott 	}
180633a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
180733a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
180833a2ffceSStephen M. Cameron 						h->chainsize, GFP_KERNEL);
18093d4e6af8SRobert Elliott 		if (!h->cmd_sg_list[i]) {
18103d4e6af8SRobert Elliott 			dev_err(&h->pdev->dev, "Failed to allocate cmd SG\n");
181133a2ffceSStephen M. Cameron 			goto clean;
181233a2ffceSStephen M. Cameron 		}
18133d4e6af8SRobert Elliott 	}
181433a2ffceSStephen M. Cameron 	return 0;
181533a2ffceSStephen M. Cameron 
181633a2ffceSStephen M. Cameron clean:
181733a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
181833a2ffceSStephen M. Cameron 	return -ENOMEM;
181933a2ffceSStephen M. Cameron }
182033a2ffceSStephen M. Cameron 
1821d9a729f3SWebb Scales static int hpsa_map_ioaccel2_sg_chain_block(struct ctlr_info *h,
1822d9a729f3SWebb Scales 	struct io_accel2_cmd *cp, struct CommandList *c)
1823d9a729f3SWebb Scales {
1824d9a729f3SWebb Scales 	struct ioaccel2_sg_element *chain_block;
1825d9a729f3SWebb Scales 	u64 temp64;
1826d9a729f3SWebb Scales 	u32 chain_size;
1827d9a729f3SWebb Scales 
1828d9a729f3SWebb Scales 	chain_block = h->ioaccel2_cmd_sg_list[c->cmdindex];
1829d9a729f3SWebb Scales 	chain_size = le32_to_cpu(cp->data_len);
1830d9a729f3SWebb Scales 	temp64 = pci_map_single(h->pdev, chain_block, chain_size,
1831d9a729f3SWebb Scales 				PCI_DMA_TODEVICE);
1832d9a729f3SWebb Scales 	if (dma_mapping_error(&h->pdev->dev, temp64)) {
1833d9a729f3SWebb Scales 		/* prevent subsequent unmapping */
1834d9a729f3SWebb Scales 		cp->sg->address = 0;
1835d9a729f3SWebb Scales 		return -1;
1836d9a729f3SWebb Scales 	}
1837d9a729f3SWebb Scales 	cp->sg->address = cpu_to_le64(temp64);
1838d9a729f3SWebb Scales 	return 0;
1839d9a729f3SWebb Scales }
1840d9a729f3SWebb Scales 
1841d9a729f3SWebb Scales static void hpsa_unmap_ioaccel2_sg_chain_block(struct ctlr_info *h,
1842d9a729f3SWebb Scales 	struct io_accel2_cmd *cp)
1843d9a729f3SWebb Scales {
1844d9a729f3SWebb Scales 	struct ioaccel2_sg_element *chain_sg;
1845d9a729f3SWebb Scales 	u64 temp64;
1846d9a729f3SWebb Scales 	u32 chain_size;
1847d9a729f3SWebb Scales 
1848d9a729f3SWebb Scales 	chain_sg = cp->sg;
1849d9a729f3SWebb Scales 	temp64 = le64_to_cpu(chain_sg->address);
1850d9a729f3SWebb Scales 	chain_size = le32_to_cpu(cp->data_len);
1851d9a729f3SWebb Scales 	pci_unmap_single(h->pdev, temp64, chain_size, PCI_DMA_TODEVICE);
1852d9a729f3SWebb Scales }
1853d9a729f3SWebb Scales 
1854e2bea6dfSStephen M. Cameron static int hpsa_map_sg_chain_block(struct ctlr_info *h,
185533a2ffceSStephen M. Cameron 	struct CommandList *c)
185633a2ffceSStephen M. Cameron {
185733a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg, *chain_block;
185833a2ffceSStephen M. Cameron 	u64 temp64;
185950a0decfSStephen M. Cameron 	u32 chain_len;
186033a2ffceSStephen M. Cameron 
186133a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
186233a2ffceSStephen M. Cameron 	chain_block = h->cmd_sg_list[c->cmdindex];
186350a0decfSStephen M. Cameron 	chain_sg->Ext = cpu_to_le32(HPSA_SG_CHAIN);
186450a0decfSStephen M. Cameron 	chain_len = sizeof(*chain_sg) *
18652b08b3e9SDon Brace 		(le16_to_cpu(c->Header.SGTotal) - h->max_cmd_sg_entries);
186650a0decfSStephen M. Cameron 	chain_sg->Len = cpu_to_le32(chain_len);
186750a0decfSStephen M. Cameron 	temp64 = pci_map_single(h->pdev, chain_block, chain_len,
186833a2ffceSStephen M. Cameron 				PCI_DMA_TODEVICE);
1869e2bea6dfSStephen M. Cameron 	if (dma_mapping_error(&h->pdev->dev, temp64)) {
1870e2bea6dfSStephen M. Cameron 		/* prevent subsequent unmapping */
187150a0decfSStephen M. Cameron 		chain_sg->Addr = cpu_to_le64(0);
1872e2bea6dfSStephen M. Cameron 		return -1;
1873e2bea6dfSStephen M. Cameron 	}
187450a0decfSStephen M. Cameron 	chain_sg->Addr = cpu_to_le64(temp64);
1875e2bea6dfSStephen M. Cameron 	return 0;
187633a2ffceSStephen M. Cameron }
187733a2ffceSStephen M. Cameron 
187833a2ffceSStephen M. Cameron static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
187933a2ffceSStephen M. Cameron 	struct CommandList *c)
188033a2ffceSStephen M. Cameron {
188133a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg;
188233a2ffceSStephen M. Cameron 
188350a0decfSStephen M. Cameron 	if (le16_to_cpu(c->Header.SGTotal) <= h->max_cmd_sg_entries)
188433a2ffceSStephen M. Cameron 		return;
188533a2ffceSStephen M. Cameron 
188633a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
188750a0decfSStephen M. Cameron 	pci_unmap_single(h->pdev, le64_to_cpu(chain_sg->Addr),
188850a0decfSStephen M. Cameron 			le32_to_cpu(chain_sg->Len), PCI_DMA_TODEVICE);
188933a2ffceSStephen M. Cameron }
189033a2ffceSStephen M. Cameron 
1891a09c1441SScott Teel 
1892a09c1441SScott Teel /* Decode the various types of errors on ioaccel2 path.
1893a09c1441SScott Teel  * Return 1 for any error that should generate a RAID path retry.
1894a09c1441SScott Teel  * Return 0 for errors that don't require a RAID path retry.
1895a09c1441SScott Teel  */
1896a09c1441SScott Teel static int handle_ioaccel_mode2_error(struct ctlr_info *h,
1897c349775eSScott Teel 					struct CommandList *c,
1898c349775eSScott Teel 					struct scsi_cmnd *cmd,
1899c349775eSScott Teel 					struct io_accel2_cmd *c2)
1900c349775eSScott Teel {
1901c349775eSScott Teel 	int data_len;
1902a09c1441SScott Teel 	int retry = 0;
1903c40820d5SJoe Handzik 	u32 ioaccel2_resid = 0;
1904c349775eSScott Teel 
1905c349775eSScott Teel 	switch (c2->error_data.serv_response) {
1906c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_COMPLETE:
1907c349775eSScott Teel 		switch (c2->error_data.status) {
1908c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
1909c349775eSScott Teel 			break;
1910c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
1911ee6b1889SStephen M. Cameron 			cmd->result |= SAM_STAT_CHECK_CONDITION;
1912c349775eSScott Teel 			if (c2->error_data.data_present !=
1913ee6b1889SStephen M. Cameron 					IOACCEL2_SENSE_DATA_PRESENT) {
1914ee6b1889SStephen M. Cameron 				memset(cmd->sense_buffer, 0,
1915ee6b1889SStephen M. Cameron 					SCSI_SENSE_BUFFERSIZE);
1916c349775eSScott Teel 				break;
1917ee6b1889SStephen M. Cameron 			}
1918c349775eSScott Teel 			/* copy the sense data */
1919c349775eSScott Teel 			data_len = c2->error_data.sense_data_len;
1920c349775eSScott Teel 			if (data_len > SCSI_SENSE_BUFFERSIZE)
1921c349775eSScott Teel 				data_len = SCSI_SENSE_BUFFERSIZE;
1922c349775eSScott Teel 			if (data_len > sizeof(c2->error_data.sense_data_buff))
1923c349775eSScott Teel 				data_len =
1924c349775eSScott Teel 					sizeof(c2->error_data.sense_data_buff);
1925c349775eSScott Teel 			memcpy(cmd->sense_buffer,
1926c349775eSScott Teel 				c2->error_data.sense_data_buff, data_len);
1927a09c1441SScott Teel 			retry = 1;
1928c349775eSScott Teel 			break;
1929c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
1930a09c1441SScott Teel 			retry = 1;
1931c349775eSScott Teel 			break;
1932c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
1933a09c1441SScott Teel 			retry = 1;
1934c349775eSScott Teel 			break;
1935c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
19364a8da22bSStephen Cameron 			retry = 1;
1937c349775eSScott Teel 			break;
1938c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
1939a09c1441SScott Teel 			retry = 1;
1940c349775eSScott Teel 			break;
1941c349775eSScott Teel 		default:
1942a09c1441SScott Teel 			retry = 1;
1943c349775eSScott Teel 			break;
1944c349775eSScott Teel 		}
1945c349775eSScott Teel 		break;
1946c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_FAILURE:
1947c40820d5SJoe Handzik 		switch (c2->error_data.status) {
1948c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_IO_ERROR:
1949c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_IO_ABORTED:
1950c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_OVERRUN:
1951c40820d5SJoe Handzik 			retry = 1;
1952c40820d5SJoe Handzik 			break;
1953c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_UNDERRUN:
1954c40820d5SJoe Handzik 			cmd->result = (DID_OK << 16);		/* host byte */
1955c40820d5SJoe Handzik 			cmd->result |= (COMMAND_COMPLETE << 8);	/* msg byte */
1956c40820d5SJoe Handzik 			ioaccel2_resid = get_unaligned_le32(
1957c40820d5SJoe Handzik 						&c2->error_data.resid_cnt[0]);
1958c40820d5SJoe Handzik 			scsi_set_resid(cmd, ioaccel2_resid);
1959c40820d5SJoe Handzik 			break;
1960c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_NO_PATH_TO_DEVICE:
1961c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_INVALID_DEVICE:
1962c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_IOACCEL_DISABLED:
1963c40820d5SJoe Handzik 			/* We will get an event from ctlr to trigger rescan */
1964c40820d5SJoe Handzik 			retry = 1;
1965c40820d5SJoe Handzik 			break;
1966c40820d5SJoe Handzik 		default:
1967c40820d5SJoe Handzik 			retry = 1;
1968c40820d5SJoe Handzik 		}
1969c349775eSScott Teel 		break;
1970c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
1971c349775eSScott Teel 		break;
1972c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
1973c349775eSScott Teel 		break;
1974c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
1975a09c1441SScott Teel 		retry = 1;
1976c349775eSScott Teel 		break;
1977c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
1978c349775eSScott Teel 		break;
1979c349775eSScott Teel 	default:
1980a09c1441SScott Teel 		retry = 1;
1981c349775eSScott Teel 		break;
1982c349775eSScott Teel 	}
1983a09c1441SScott Teel 
1984a09c1441SScott Teel 	return retry;	/* retry on raid path? */
1985c349775eSScott Teel }
1986c349775eSScott Teel 
1987a58e7e53SWebb Scales static void hpsa_cmd_resolve_events(struct ctlr_info *h,
1988a58e7e53SWebb Scales 		struct CommandList *c)
1989a58e7e53SWebb Scales {
1990a58e7e53SWebb Scales 	/*
1991a58e7e53SWebb Scales 	 * Prevent the following race in the abort handler:
1992a58e7e53SWebb Scales 	 *
1993a58e7e53SWebb Scales 	 * 1. LLD is requested to abort a SCSI command
1994a58e7e53SWebb Scales 	 * 2. The SCSI command completes
1995a58e7e53SWebb Scales 	 * 3. The struct CommandList associated with step 2 is made available
1996a58e7e53SWebb Scales 	 * 4. New I/O request to LLD to another LUN re-uses struct CommandList
1997a58e7e53SWebb Scales 	 * 5. Abort handler follows scsi_cmnd->host_scribble and
1998a58e7e53SWebb Scales 	 *    finds struct CommandList and tries to aborts it
1999a58e7e53SWebb Scales 	 * Now we have aborted the wrong command.
2000a58e7e53SWebb Scales 	 *
2001a58e7e53SWebb Scales 	 * Clear c->scsi_cmd here so that the abort handler will know this
2002a58e7e53SWebb Scales 	 * command has completed.  Then, check to see if the abort handler is
2003a58e7e53SWebb Scales 	 * waiting for this command, and, if so, wake it.
2004a58e7e53SWebb Scales 	 */
2005a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_IDLE;
2006a58e7e53SWebb Scales 	mb(); /* Ensure c->scsi_cmd is set to SCSI_CMD_IDLE */
2007a58e7e53SWebb Scales 	if (c->abort_pending) {
2008a58e7e53SWebb Scales 		c->abort_pending = false;
2009a58e7e53SWebb Scales 		wake_up_all(&h->abort_sync_wait_queue);
2010a58e7e53SWebb Scales 	}
2011a58e7e53SWebb Scales }
2012a58e7e53SWebb Scales 
20138a0ff92cSWebb Scales static void hpsa_cmd_free_and_done(struct ctlr_info *h,
20148a0ff92cSWebb Scales 		struct CommandList *c, struct scsi_cmnd *cmd)
20158a0ff92cSWebb Scales {
2016a58e7e53SWebb Scales 	hpsa_cmd_resolve_events(h, c);
20178a0ff92cSWebb Scales 	cmd_free(h, c);
20188a0ff92cSWebb Scales 	cmd->scsi_done(cmd);
20198a0ff92cSWebb Scales }
20208a0ff92cSWebb Scales 
20218a0ff92cSWebb Scales static void hpsa_retry_cmd(struct ctlr_info *h, struct CommandList *c)
20228a0ff92cSWebb Scales {
20238a0ff92cSWebb Scales 	INIT_WORK(&c->work, hpsa_command_resubmit_worker);
20248a0ff92cSWebb Scales 	queue_work_on(raw_smp_processor_id(), h->resubmit_wq, &c->work);
20258a0ff92cSWebb Scales }
20268a0ff92cSWebb Scales 
2027a58e7e53SWebb Scales static void hpsa_set_scsi_cmd_aborted(struct scsi_cmnd *cmd)
2028a58e7e53SWebb Scales {
2029a58e7e53SWebb Scales 	cmd->result = DID_ABORT << 16;
2030a58e7e53SWebb Scales }
2031a58e7e53SWebb Scales 
2032a58e7e53SWebb Scales static void hpsa_cmd_abort_and_free(struct ctlr_info *h, struct CommandList *c,
2033a58e7e53SWebb Scales 				    struct scsi_cmnd *cmd)
2034a58e7e53SWebb Scales {
2035a58e7e53SWebb Scales 	hpsa_set_scsi_cmd_aborted(cmd);
2036a58e7e53SWebb Scales 	dev_warn(&h->pdev->dev, "CDB %16phN was aborted with status 0x%x\n",
2037a58e7e53SWebb Scales 			 c->Request.CDB, c->err_info->ScsiStatus);
2038a58e7e53SWebb Scales 	hpsa_cmd_resolve_events(h, c);
2039a58e7e53SWebb Scales 	cmd_free(h, c);		/* FIX-ME:  change to cmd_tagged_free(h, c) */
2040a58e7e53SWebb Scales }
2041a58e7e53SWebb Scales 
2042c349775eSScott Teel static void process_ioaccel2_completion(struct ctlr_info *h,
2043c349775eSScott Teel 		struct CommandList *c, struct scsi_cmnd *cmd,
2044c349775eSScott Teel 		struct hpsa_scsi_dev_t *dev)
2045c349775eSScott Teel {
2046c349775eSScott Teel 	struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
2047c349775eSScott Teel 
2048c349775eSScott Teel 	/* check for good status */
2049c349775eSScott Teel 	if (likely(c2->error_data.serv_response == 0 &&
20508a0ff92cSWebb Scales 			c2->error_data.status == 0))
20518a0ff92cSWebb Scales 		return hpsa_cmd_free_and_done(h, c, cmd);
2052c349775eSScott Teel 
2053a58e7e53SWebb Scales 	/* don't requeue a command which is being aborted */
2054a58e7e53SWebb Scales 	if (unlikely(c->abort_pending))
2055a58e7e53SWebb Scales 		return hpsa_cmd_abort_and_free(h, c, cmd);
2056a58e7e53SWebb Scales 
20578a0ff92cSWebb Scales 	/*
20588a0ff92cSWebb Scales 	 * Any RAID offload error results in retry which will use
2059c349775eSScott Teel 	 * the normal I/O path so the controller can handle whatever's
2060c349775eSScott Teel 	 * wrong.
2061c349775eSScott Teel 	 */
2062c349775eSScott Teel 	if (is_logical_dev_addr_mode(dev->scsi3addr) &&
2063c349775eSScott Teel 		c2->error_data.serv_response ==
2064c349775eSScott Teel 			IOACCEL2_SERV_RESPONSE_FAILURE) {
2065080ef1ccSDon Brace 		if (c2->error_data.status ==
2066080ef1ccSDon Brace 			IOACCEL2_STATUS_SR_IOACCEL_DISABLED)
2067c349775eSScott Teel 			dev->offload_enabled = 0;
20688a0ff92cSWebb Scales 
20698a0ff92cSWebb Scales 		return hpsa_retry_cmd(h, c);
2070080ef1ccSDon Brace 	}
2071080ef1ccSDon Brace 
2072080ef1ccSDon Brace 	if (handle_ioaccel_mode2_error(h, c, cmd, c2))
20738a0ff92cSWebb Scales 		return hpsa_retry_cmd(h, c);
2074080ef1ccSDon Brace 
20758a0ff92cSWebb Scales 	return hpsa_cmd_free_and_done(h, c, cmd);
2076c349775eSScott Teel }
2077c349775eSScott Teel 
20789437ac43SStephen Cameron /* Returns 0 on success, < 0 otherwise. */
20799437ac43SStephen Cameron static int hpsa_evaluate_tmf_status(struct ctlr_info *h,
20809437ac43SStephen Cameron 					struct CommandList *cp)
20819437ac43SStephen Cameron {
20829437ac43SStephen Cameron 	u8 tmf_status = cp->err_info->ScsiStatus;
20839437ac43SStephen Cameron 
20849437ac43SStephen Cameron 	switch (tmf_status) {
20859437ac43SStephen Cameron 	case CISS_TMF_COMPLETE:
20869437ac43SStephen Cameron 		/*
20879437ac43SStephen Cameron 		 * CISS_TMF_COMPLETE never happens, instead,
20889437ac43SStephen Cameron 		 * ei->CommandStatus == 0 for this case.
20899437ac43SStephen Cameron 		 */
20909437ac43SStephen Cameron 	case CISS_TMF_SUCCESS:
20919437ac43SStephen Cameron 		return 0;
20929437ac43SStephen Cameron 	case CISS_TMF_INVALID_FRAME:
20939437ac43SStephen Cameron 	case CISS_TMF_NOT_SUPPORTED:
20949437ac43SStephen Cameron 	case CISS_TMF_FAILED:
20959437ac43SStephen Cameron 	case CISS_TMF_WRONG_LUN:
20969437ac43SStephen Cameron 	case CISS_TMF_OVERLAPPED_TAG:
20979437ac43SStephen Cameron 		break;
20989437ac43SStephen Cameron 	default:
20999437ac43SStephen Cameron 		dev_warn(&h->pdev->dev, "Unknown TMF status: 0x%02x\n",
21009437ac43SStephen Cameron 				tmf_status);
21019437ac43SStephen Cameron 		break;
21029437ac43SStephen Cameron 	}
21039437ac43SStephen Cameron 	return -tmf_status;
21049437ac43SStephen Cameron }
21059437ac43SStephen Cameron 
21061fb011fbSStephen M. Cameron static void complete_scsi_command(struct CommandList *cp)
2107edd16368SStephen M. Cameron {
2108edd16368SStephen M. Cameron 	struct scsi_cmnd *cmd;
2109edd16368SStephen M. Cameron 	struct ctlr_info *h;
2110edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2111283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
2112d9a729f3SWebb Scales 	struct io_accel2_cmd *c2;
2113edd16368SStephen M. Cameron 
21149437ac43SStephen Cameron 	u8 sense_key;
21159437ac43SStephen Cameron 	u8 asc;      /* additional sense code */
21169437ac43SStephen Cameron 	u8 ascq;     /* additional sense code qualifier */
2117db111e18SStephen M. Cameron 	unsigned long sense_data_size;
2118edd16368SStephen M. Cameron 
2119edd16368SStephen M. Cameron 	ei = cp->err_info;
21207fa3030cSStephen Cameron 	cmd = cp->scsi_cmd;
2121edd16368SStephen M. Cameron 	h = cp->h;
2122283b4a9bSStephen M. Cameron 	dev = cmd->device->hostdata;
2123d9a729f3SWebb Scales 	c2 = &h->ioaccel2_cmd_pool[cp->cmdindex];
2124edd16368SStephen M. Cameron 
2125edd16368SStephen M. Cameron 	scsi_dma_unmap(cmd); /* undo the DMA mappings */
2126e1f7de0cSMatt Gates 	if ((cp->cmd_type == CMD_SCSI) &&
21272b08b3e9SDon Brace 		(le16_to_cpu(cp->Header.SGTotal) > h->max_cmd_sg_entries))
212833a2ffceSStephen M. Cameron 		hpsa_unmap_sg_chain_block(h, cp);
2129edd16368SStephen M. Cameron 
2130d9a729f3SWebb Scales 	if ((cp->cmd_type == CMD_IOACCEL2) &&
2131d9a729f3SWebb Scales 		(c2->sg[0].chain_indicator == IOACCEL2_CHAIN))
2132d9a729f3SWebb Scales 		hpsa_unmap_ioaccel2_sg_chain_block(h, c2);
2133d9a729f3SWebb Scales 
2134edd16368SStephen M. Cameron 	cmd->result = (DID_OK << 16); 		/* host byte */
2135edd16368SStephen M. Cameron 	cmd->result |= (COMMAND_COMPLETE << 8);	/* msg byte */
2136c349775eSScott Teel 
213703383736SDon Brace 	if (cp->cmd_type == CMD_IOACCEL2 || cp->cmd_type == CMD_IOACCEL1)
213803383736SDon Brace 		atomic_dec(&cp->phys_disk->ioaccel_cmds_out);
213903383736SDon Brace 
214025163bd5SWebb Scales 	/*
214125163bd5SWebb Scales 	 * We check for lockup status here as it may be set for
214225163bd5SWebb Scales 	 * CMD_SCSI, CMD_IOACCEL1 and CMD_IOACCEL2 commands by
214325163bd5SWebb Scales 	 * fail_all_oustanding_cmds()
214425163bd5SWebb Scales 	 */
214525163bd5SWebb Scales 	if (unlikely(ei->CommandStatus == CMD_CTLR_LOCKUP)) {
214625163bd5SWebb Scales 		/* DID_NO_CONNECT will prevent a retry */
214725163bd5SWebb Scales 		cmd->result = DID_NO_CONNECT << 16;
21488a0ff92cSWebb Scales 		return hpsa_cmd_free_and_done(h, cp, cmd);
214925163bd5SWebb Scales 	}
215025163bd5SWebb Scales 
2151c349775eSScott Teel 	if (cp->cmd_type == CMD_IOACCEL2)
2152c349775eSScott Teel 		return process_ioaccel2_completion(h, cp, cmd, dev);
2153c349775eSScott Teel 
21546aa4c361SRobert Elliott 	scsi_set_resid(cmd, ei->ResidualCnt);
21558a0ff92cSWebb Scales 	if (ei->CommandStatus == 0)
21568a0ff92cSWebb Scales 		return hpsa_cmd_free_and_done(h, cp, cmd);
21576aa4c361SRobert Elliott 
2158e1f7de0cSMatt Gates 	/* For I/O accelerator commands, copy over some fields to the normal
2159e1f7de0cSMatt Gates 	 * CISS header used below for error handling.
2160e1f7de0cSMatt Gates 	 */
2161e1f7de0cSMatt Gates 	if (cp->cmd_type == CMD_IOACCEL1) {
2162e1f7de0cSMatt Gates 		struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
21632b08b3e9SDon Brace 		cp->Header.SGList = scsi_sg_count(cmd);
21642b08b3e9SDon Brace 		cp->Header.SGTotal = cpu_to_le16(cp->Header.SGList);
21652b08b3e9SDon Brace 		cp->Request.CDBLen = le16_to_cpu(c->io_flags) &
21662b08b3e9SDon Brace 			IOACCEL1_IOFLAGS_CDBLEN_MASK;
216750a0decfSStephen M. Cameron 		cp->Header.tag = c->tag;
2168e1f7de0cSMatt Gates 		memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
2169e1f7de0cSMatt Gates 		memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
2170283b4a9bSStephen M. Cameron 
2171283b4a9bSStephen M. Cameron 		/* Any RAID offload error results in retry which will use
2172283b4a9bSStephen M. Cameron 		 * the normal I/O path so the controller can handle whatever's
2173283b4a9bSStephen M. Cameron 		 * wrong.
2174283b4a9bSStephen M. Cameron 		 */
2175283b4a9bSStephen M. Cameron 		if (is_logical_dev_addr_mode(dev->scsi3addr)) {
2176283b4a9bSStephen M. Cameron 			if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
2177283b4a9bSStephen M. Cameron 				dev->offload_enabled = 0;
2178a58e7e53SWebb Scales 			if (!cp->abort_pending)
21798a0ff92cSWebb Scales 				return hpsa_retry_cmd(h, cp);
2180283b4a9bSStephen M. Cameron 		}
2181e1f7de0cSMatt Gates 	}
2182e1f7de0cSMatt Gates 
2183a58e7e53SWebb Scales 	if (cp->abort_pending)
2184a58e7e53SWebb Scales 		ei->CommandStatus = CMD_ABORTED;
2185a58e7e53SWebb Scales 
2186edd16368SStephen M. Cameron 	/* an error has occurred */
2187edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
2188edd16368SStephen M. Cameron 
2189edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
21909437ac43SStephen Cameron 		cmd->result |= ei->ScsiStatus;
21919437ac43SStephen Cameron 		/* copy the sense data */
21929437ac43SStephen Cameron 		if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
21939437ac43SStephen Cameron 			sense_data_size = SCSI_SENSE_BUFFERSIZE;
21949437ac43SStephen Cameron 		else
21959437ac43SStephen Cameron 			sense_data_size = sizeof(ei->SenseInfo);
21969437ac43SStephen Cameron 		if (ei->SenseLen < sense_data_size)
21979437ac43SStephen Cameron 			sense_data_size = ei->SenseLen;
21989437ac43SStephen Cameron 		memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
21999437ac43SStephen Cameron 		if (ei->ScsiStatus)
22009437ac43SStephen Cameron 			decode_sense_data(ei->SenseInfo, sense_data_size,
22019437ac43SStephen Cameron 				&sense_key, &asc, &ascq);
2202edd16368SStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
22031d3b3609SMatt Gates 			if (sense_key == ABORTED_COMMAND) {
22042e311fbaSStephen M. Cameron 				cmd->result |= DID_SOFT_ERROR << 16;
22051d3b3609SMatt Gates 				break;
22061d3b3609SMatt Gates 			}
2207edd16368SStephen M. Cameron 			break;
2208edd16368SStephen M. Cameron 		}
2209edd16368SStephen M. Cameron 		/* Problem was not a check condition
2210edd16368SStephen M. Cameron 		 * Pass it up to the upper layers...
2211edd16368SStephen M. Cameron 		 */
2212edd16368SStephen M. Cameron 		if (ei->ScsiStatus) {
2213edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
2214edd16368SStephen M. Cameron 				"Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
2215edd16368SStephen M. Cameron 				"Returning result: 0x%x\n",
2216edd16368SStephen M. Cameron 				cp, ei->ScsiStatus,
2217edd16368SStephen M. Cameron 				sense_key, asc, ascq,
2218edd16368SStephen M. Cameron 				cmd->result);
2219edd16368SStephen M. Cameron 		} else {  /* scsi status is zero??? How??? */
2220edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
2221edd16368SStephen M. Cameron 				"Returning no connection.\n", cp),
2222edd16368SStephen M. Cameron 
2223edd16368SStephen M. Cameron 			/* Ordinarily, this case should never happen,
2224edd16368SStephen M. Cameron 			 * but there is a bug in some released firmware
2225edd16368SStephen M. Cameron 			 * revisions that allows it to happen if, for
2226edd16368SStephen M. Cameron 			 * example, a 4100 backplane loses power and
2227edd16368SStephen M. Cameron 			 * the tape drive is in it.  We assume that
2228edd16368SStephen M. Cameron 			 * it's a fatal error of some kind because we
2229edd16368SStephen M. Cameron 			 * can't show that it wasn't. We will make it
2230edd16368SStephen M. Cameron 			 * look like selection timeout since that is
2231edd16368SStephen M. Cameron 			 * the most common reason for this to occur,
2232edd16368SStephen M. Cameron 			 * and it's severe enough.
2233edd16368SStephen M. Cameron 			 */
2234edd16368SStephen M. Cameron 
2235edd16368SStephen M. Cameron 			cmd->result = DID_NO_CONNECT << 16;
2236edd16368SStephen M. Cameron 		}
2237edd16368SStephen M. Cameron 		break;
2238edd16368SStephen M. Cameron 
2239edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
2240edd16368SStephen M. Cameron 		break;
2241edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
2242f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev,
2243f42e81e1SStephen Cameron 			"CDB %16phN data overrun\n", cp->Request.CDB);
2244edd16368SStephen M. Cameron 		break;
2245edd16368SStephen M. Cameron 	case CMD_INVALID: {
2246edd16368SStephen M. Cameron 		/* print_bytes(cp, sizeof(*cp), 1, 0);
2247edd16368SStephen M. Cameron 		print_cmd(cp); */
2248edd16368SStephen M. Cameron 		/* We get CMD_INVALID if you address a non-existent device
2249edd16368SStephen M. Cameron 		 * instead of a selection timeout (no response).  You will
2250edd16368SStephen M. Cameron 		 * see this if you yank out a drive, then try to access it.
2251edd16368SStephen M. Cameron 		 * This is kind of a shame because it means that any other
2252edd16368SStephen M. Cameron 		 * CMD_INVALID (e.g. driver bug) will get interpreted as a
2253edd16368SStephen M. Cameron 		 * missing target. */
2254edd16368SStephen M. Cameron 		cmd->result = DID_NO_CONNECT << 16;
2255edd16368SStephen M. Cameron 	}
2256edd16368SStephen M. Cameron 		break;
2257edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
2258256d0eaaSStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2259f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : protocol error\n",
2260f42e81e1SStephen Cameron 				cp->Request.CDB);
2261edd16368SStephen M. Cameron 		break;
2262edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
2263edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2264f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : hardware error\n",
2265f42e81e1SStephen Cameron 			cp->Request.CDB);
2266edd16368SStephen M. Cameron 		break;
2267edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
2268edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2269f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : connection lost\n",
2270f42e81e1SStephen Cameron 			cp->Request.CDB);
2271edd16368SStephen M. Cameron 		break;
2272edd16368SStephen M. Cameron 	case CMD_ABORTED:
2273a58e7e53SWebb Scales 		/* Return now to avoid calling scsi_done(). */
2274a58e7e53SWebb Scales 		return hpsa_cmd_abort_and_free(h, cp, cmd);
2275edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
2276edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2277f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : abort failed\n",
2278f42e81e1SStephen Cameron 			cp->Request.CDB);
2279edd16368SStephen M. Cameron 		break;
2280edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
2281f6e76055SStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
2282f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : unsolicited abort\n",
2283f42e81e1SStephen Cameron 			cp->Request.CDB);
2284edd16368SStephen M. Cameron 		break;
2285edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
2286edd16368SStephen M. Cameron 		cmd->result = DID_TIME_OUT << 16;
2287f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN timed out\n",
2288f42e81e1SStephen Cameron 			cp->Request.CDB);
2289edd16368SStephen M. Cameron 		break;
22901d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
22911d5e2ed0SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
22921d5e2ed0SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Command unabortable\n");
22931d5e2ed0SStephen M. Cameron 		break;
22949437ac43SStephen Cameron 	case CMD_TMF_STATUS:
22959437ac43SStephen Cameron 		if (hpsa_evaluate_tmf_status(h, cp)) /* TMF failed? */
22969437ac43SStephen Cameron 			cmd->result = DID_ERROR << 16;
22979437ac43SStephen Cameron 		break;
2298283b4a9bSStephen M. Cameron 	case CMD_IOACCEL_DISABLED:
2299283b4a9bSStephen M. Cameron 		/* This only handles the direct pass-through case since RAID
2300283b4a9bSStephen M. Cameron 		 * offload is handled above.  Just attempt a retry.
2301283b4a9bSStephen M. Cameron 		 */
2302283b4a9bSStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16;
2303283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev,
2304283b4a9bSStephen M. Cameron 				"cp %p had HP SSD Smart Path error\n", cp);
2305283b4a9bSStephen M. Cameron 		break;
2306edd16368SStephen M. Cameron 	default:
2307edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2308edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
2309edd16368SStephen M. Cameron 				cp, ei->CommandStatus);
2310edd16368SStephen M. Cameron 	}
23118a0ff92cSWebb Scales 
23128a0ff92cSWebb Scales 	return hpsa_cmd_free_and_done(h, cp, cmd);
2313edd16368SStephen M. Cameron }
2314edd16368SStephen M. Cameron 
2315edd16368SStephen M. Cameron static void hpsa_pci_unmap(struct pci_dev *pdev,
2316edd16368SStephen M. Cameron 	struct CommandList *c, int sg_used, int data_direction)
2317edd16368SStephen M. Cameron {
2318edd16368SStephen M. Cameron 	int i;
2319edd16368SStephen M. Cameron 
232050a0decfSStephen M. Cameron 	for (i = 0; i < sg_used; i++)
232150a0decfSStephen M. Cameron 		pci_unmap_single(pdev, (dma_addr_t) le64_to_cpu(c->SG[i].Addr),
232250a0decfSStephen M. Cameron 				le32_to_cpu(c->SG[i].Len),
2323edd16368SStephen M. Cameron 				data_direction);
2324edd16368SStephen M. Cameron }
2325edd16368SStephen M. Cameron 
2326a2dac136SStephen M. Cameron static int hpsa_map_one(struct pci_dev *pdev,
2327edd16368SStephen M. Cameron 		struct CommandList *cp,
2328edd16368SStephen M. Cameron 		unsigned char *buf,
2329edd16368SStephen M. Cameron 		size_t buflen,
2330edd16368SStephen M. Cameron 		int data_direction)
2331edd16368SStephen M. Cameron {
233201a02ffcSStephen M. Cameron 	u64 addr64;
2333edd16368SStephen M. Cameron 
2334edd16368SStephen M. Cameron 	if (buflen == 0 || data_direction == PCI_DMA_NONE) {
2335edd16368SStephen M. Cameron 		cp->Header.SGList = 0;
233650a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(0);
2337a2dac136SStephen M. Cameron 		return 0;
2338edd16368SStephen M. Cameron 	}
2339edd16368SStephen M. Cameron 
234050a0decfSStephen M. Cameron 	addr64 = pci_map_single(pdev, buf, buflen, data_direction);
2341eceaae18SShuah Khan 	if (dma_mapping_error(&pdev->dev, addr64)) {
2342a2dac136SStephen M. Cameron 		/* Prevent subsequent unmap of something never mapped */
2343eceaae18SShuah Khan 		cp->Header.SGList = 0;
234450a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(0);
2345a2dac136SStephen M. Cameron 		return -1;
2346eceaae18SShuah Khan 	}
234750a0decfSStephen M. Cameron 	cp->SG[0].Addr = cpu_to_le64(addr64);
234850a0decfSStephen M. Cameron 	cp->SG[0].Len = cpu_to_le32(buflen);
234950a0decfSStephen M. Cameron 	cp->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* we are not chaining */
235050a0decfSStephen M. Cameron 	cp->Header.SGList = 1;   /* no. SGs contig in this cmd */
235150a0decfSStephen M. Cameron 	cp->Header.SGTotal = cpu_to_le16(1); /* total sgs in cmd list */
2352a2dac136SStephen M. Cameron 	return 0;
2353edd16368SStephen M. Cameron }
2354edd16368SStephen M. Cameron 
235525163bd5SWebb Scales #define NO_TIMEOUT ((unsigned long) -1)
235625163bd5SWebb Scales #define DEFAULT_TIMEOUT 30000 /* milliseconds */
235725163bd5SWebb Scales static int hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
235825163bd5SWebb Scales 	struct CommandList *c, int reply_queue, unsigned long timeout_msecs)
2359edd16368SStephen M. Cameron {
2360edd16368SStephen M. Cameron 	DECLARE_COMPLETION_ONSTACK(wait);
2361edd16368SStephen M. Cameron 
2362edd16368SStephen M. Cameron 	c->waiting = &wait;
236325163bd5SWebb Scales 	__enqueue_cmd_and_start_io(h, c, reply_queue);
236425163bd5SWebb Scales 	if (timeout_msecs == NO_TIMEOUT) {
236525163bd5SWebb Scales 		/* TODO: get rid of this no-timeout thing */
236625163bd5SWebb Scales 		wait_for_completion_io(&wait);
236725163bd5SWebb Scales 		return IO_OK;
236825163bd5SWebb Scales 	}
236925163bd5SWebb Scales 	if (!wait_for_completion_io_timeout(&wait,
237025163bd5SWebb Scales 					msecs_to_jiffies(timeout_msecs))) {
237125163bd5SWebb Scales 		dev_warn(&h->pdev->dev, "Command timed out.\n");
237225163bd5SWebb Scales 		return -ETIMEDOUT;
237325163bd5SWebb Scales 	}
237425163bd5SWebb Scales 	return IO_OK;
237525163bd5SWebb Scales }
237625163bd5SWebb Scales 
237725163bd5SWebb Scales static int hpsa_scsi_do_simple_cmd(struct ctlr_info *h, struct CommandList *c,
237825163bd5SWebb Scales 				   int reply_queue, unsigned long timeout_msecs)
237925163bd5SWebb Scales {
238025163bd5SWebb Scales 	if (unlikely(lockup_detected(h))) {
238125163bd5SWebb Scales 		c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
238225163bd5SWebb Scales 		return IO_OK;
238325163bd5SWebb Scales 	}
238425163bd5SWebb Scales 	return hpsa_scsi_do_simple_cmd_core(h, c, reply_queue, timeout_msecs);
2385edd16368SStephen M. Cameron }
2386edd16368SStephen M. Cameron 
2387094963daSStephen M. Cameron static u32 lockup_detected(struct ctlr_info *h)
2388094963daSStephen M. Cameron {
2389094963daSStephen M. Cameron 	int cpu;
2390094963daSStephen M. Cameron 	u32 rc, *lockup_detected;
2391094963daSStephen M. Cameron 
2392094963daSStephen M. Cameron 	cpu = get_cpu();
2393094963daSStephen M. Cameron 	lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
2394094963daSStephen M. Cameron 	rc = *lockup_detected;
2395094963daSStephen M. Cameron 	put_cpu();
2396094963daSStephen M. Cameron 	return rc;
2397094963daSStephen M. Cameron }
2398094963daSStephen M. Cameron 
23999c2fc160SStephen M. Cameron #define MAX_DRIVER_CMD_RETRIES 25
240025163bd5SWebb Scales static int hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
240125163bd5SWebb Scales 	struct CommandList *c, int data_direction, unsigned long timeout_msecs)
2402edd16368SStephen M. Cameron {
24039c2fc160SStephen M. Cameron 	int backoff_time = 10, retry_count = 0;
240425163bd5SWebb Scales 	int rc;
2405edd16368SStephen M. Cameron 
2406edd16368SStephen M. Cameron 	do {
24077630abd0SJoe Perches 		memset(c->err_info, 0, sizeof(*c->err_info));
240825163bd5SWebb Scales 		rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
240925163bd5SWebb Scales 						  timeout_msecs);
241025163bd5SWebb Scales 		if (rc)
241125163bd5SWebb Scales 			break;
2412edd16368SStephen M. Cameron 		retry_count++;
24139c2fc160SStephen M. Cameron 		if (retry_count > 3) {
24149c2fc160SStephen M. Cameron 			msleep(backoff_time);
24159c2fc160SStephen M. Cameron 			if (backoff_time < 1000)
24169c2fc160SStephen M. Cameron 				backoff_time *= 2;
24179c2fc160SStephen M. Cameron 		}
2418852af20aSMatt Bondurant 	} while ((check_for_unit_attention(h, c) ||
24199c2fc160SStephen M. Cameron 			check_for_busy(h, c)) &&
24209c2fc160SStephen M. Cameron 			retry_count <= MAX_DRIVER_CMD_RETRIES);
2421edd16368SStephen M. Cameron 	hpsa_pci_unmap(h->pdev, c, 1, data_direction);
242225163bd5SWebb Scales 	if (retry_count > MAX_DRIVER_CMD_RETRIES)
242325163bd5SWebb Scales 		rc = -EIO;
242425163bd5SWebb Scales 	return rc;
2425edd16368SStephen M. Cameron }
2426edd16368SStephen M. Cameron 
2427d1e8beacSStephen M. Cameron static void hpsa_print_cmd(struct ctlr_info *h, char *txt,
2428d1e8beacSStephen M. Cameron 				struct CommandList *c)
2429edd16368SStephen M. Cameron {
2430d1e8beacSStephen M. Cameron 	const u8 *cdb = c->Request.CDB;
2431d1e8beacSStephen M. Cameron 	const u8 *lun = c->Header.LUN.LunAddrBytes;
2432edd16368SStephen M. Cameron 
2433d1e8beacSStephen M. Cameron 	dev_warn(&h->pdev->dev, "%s: LUN:%02x%02x%02x%02x%02x%02x%02x%02x"
2434d1e8beacSStephen M. Cameron 	" CDB:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
2435d1e8beacSStephen M. Cameron 		txt, lun[0], lun[1], lun[2], lun[3],
2436d1e8beacSStephen M. Cameron 		lun[4], lun[5], lun[6], lun[7],
2437d1e8beacSStephen M. Cameron 		cdb[0], cdb[1], cdb[2], cdb[3],
2438d1e8beacSStephen M. Cameron 		cdb[4], cdb[5], cdb[6], cdb[7],
2439d1e8beacSStephen M. Cameron 		cdb[8], cdb[9], cdb[10], cdb[11],
2440d1e8beacSStephen M. Cameron 		cdb[12], cdb[13], cdb[14], cdb[15]);
2441d1e8beacSStephen M. Cameron }
2442d1e8beacSStephen M. Cameron 
2443d1e8beacSStephen M. Cameron static void hpsa_scsi_interpret_error(struct ctlr_info *h,
2444d1e8beacSStephen M. Cameron 			struct CommandList *cp)
2445d1e8beacSStephen M. Cameron {
2446d1e8beacSStephen M. Cameron 	const struct ErrorInfo *ei = cp->err_info;
2447d1e8beacSStephen M. Cameron 	struct device *d = &cp->h->pdev->dev;
24489437ac43SStephen Cameron 	u8 sense_key, asc, ascq;
24499437ac43SStephen Cameron 	int sense_len;
2450d1e8beacSStephen M. Cameron 
2451edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
2452edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
24539437ac43SStephen Cameron 		if (ei->SenseLen > sizeof(ei->SenseInfo))
24549437ac43SStephen Cameron 			sense_len = sizeof(ei->SenseInfo);
24559437ac43SStephen Cameron 		else
24569437ac43SStephen Cameron 			sense_len = ei->SenseLen;
24579437ac43SStephen Cameron 		decode_sense_data(ei->SenseInfo, sense_len,
24589437ac43SStephen Cameron 					&sense_key, &asc, &ascq);
2459d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "SCSI status", cp);
2460d1e8beacSStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION)
24619437ac43SStephen Cameron 			dev_warn(d, "SCSI Status = 02, Sense key = 0x%02x, ASC = 0x%02x, ASCQ = 0x%02x\n",
24629437ac43SStephen Cameron 				sense_key, asc, ascq);
2463d1e8beacSStephen M. Cameron 		else
24649437ac43SStephen Cameron 			dev_warn(d, "SCSI Status = 0x%02x\n", ei->ScsiStatus);
2465edd16368SStephen M. Cameron 		if (ei->ScsiStatus == 0)
2466edd16368SStephen M. Cameron 			dev_warn(d, "SCSI status is abnormally zero.  "
2467edd16368SStephen M. Cameron 			"(probably indicates selection timeout "
2468edd16368SStephen M. Cameron 			"reported incorrectly due to a known "
2469edd16368SStephen M. Cameron 			"firmware bug, circa July, 2001.)\n");
2470edd16368SStephen M. Cameron 		break;
2471edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
2472edd16368SStephen M. Cameron 		break;
2473edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
2474d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "overrun condition", cp);
2475edd16368SStephen M. Cameron 		break;
2476edd16368SStephen M. Cameron 	case CMD_INVALID: {
2477edd16368SStephen M. Cameron 		/* controller unfortunately reports SCSI passthru's
2478edd16368SStephen M. Cameron 		 * to non-existent targets as invalid commands.
2479edd16368SStephen M. Cameron 		 */
2480d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "invalid command", cp);
2481d1e8beacSStephen M. Cameron 		dev_warn(d, "probably means device no longer present\n");
2482edd16368SStephen M. Cameron 		}
2483edd16368SStephen M. Cameron 		break;
2484edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
2485d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "protocol error", cp);
2486edd16368SStephen M. Cameron 		break;
2487edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
2488d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "hardware error", cp);
2489edd16368SStephen M. Cameron 		break;
2490edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
2491d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "connection lost", cp);
2492edd16368SStephen M. Cameron 		break;
2493edd16368SStephen M. Cameron 	case CMD_ABORTED:
2494d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "aborted", cp);
2495edd16368SStephen M. Cameron 		break;
2496edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
2497d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "abort failed", cp);
2498edd16368SStephen M. Cameron 		break;
2499edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
2500d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unsolicited abort", cp);
2501edd16368SStephen M. Cameron 		break;
2502edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
2503d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "timed out", cp);
2504edd16368SStephen M. Cameron 		break;
25051d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
2506d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unabortable", cp);
25071d5e2ed0SStephen M. Cameron 		break;
250825163bd5SWebb Scales 	case CMD_CTLR_LOCKUP:
250925163bd5SWebb Scales 		hpsa_print_cmd(h, "controller lockup detected", cp);
251025163bd5SWebb Scales 		break;
2511edd16368SStephen M. Cameron 	default:
2512d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unknown status", cp);
2513d1e8beacSStephen M. Cameron 		dev_warn(d, "Unknown command status %x\n",
2514edd16368SStephen M. Cameron 				ei->CommandStatus);
2515edd16368SStephen M. Cameron 	}
2516edd16368SStephen M. Cameron }
2517edd16368SStephen M. Cameron 
2518edd16368SStephen M. Cameron static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
2519b7bb24ebSStephen M. Cameron 			u16 page, unsigned char *buf,
2520edd16368SStephen M. Cameron 			unsigned char bufsize)
2521edd16368SStephen M. Cameron {
2522edd16368SStephen M. Cameron 	int rc = IO_OK;
2523edd16368SStephen M. Cameron 	struct CommandList *c;
2524edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2525edd16368SStephen M. Cameron 
252645fcb86eSStephen Cameron 	c = cmd_alloc(h);
2527edd16368SStephen M. Cameron 
2528a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
2529a2dac136SStephen M. Cameron 			page, scsi3addr, TYPE_CMD)) {
2530a2dac136SStephen M. Cameron 		rc = -1;
2531a2dac136SStephen M. Cameron 		goto out;
2532a2dac136SStephen M. Cameron 	}
253325163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
253425163bd5SWebb Scales 					PCI_DMA_FROMDEVICE, NO_TIMEOUT);
253525163bd5SWebb Scales 	if (rc)
253625163bd5SWebb Scales 		goto out;
2537edd16368SStephen M. Cameron 	ei = c->err_info;
2538edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2539d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2540edd16368SStephen M. Cameron 		rc = -1;
2541edd16368SStephen M. Cameron 	}
2542a2dac136SStephen M. Cameron out:
254345fcb86eSStephen Cameron 	cmd_free(h, c);
2544edd16368SStephen M. Cameron 	return rc;
2545edd16368SStephen M. Cameron }
2546edd16368SStephen M. Cameron 
2547316b221aSStephen M. Cameron static int hpsa_bmic_ctrl_mode_sense(struct ctlr_info *h,
2548316b221aSStephen M. Cameron 		unsigned char *scsi3addr, unsigned char page,
2549316b221aSStephen M. Cameron 		struct bmic_controller_parameters *buf, size_t bufsize)
2550316b221aSStephen M. Cameron {
2551316b221aSStephen M. Cameron 	int rc = IO_OK;
2552316b221aSStephen M. Cameron 	struct CommandList *c;
2553316b221aSStephen M. Cameron 	struct ErrorInfo *ei;
2554316b221aSStephen M. Cameron 
255545fcb86eSStephen Cameron 	c = cmd_alloc(h);
2556316b221aSStephen M. Cameron 	if (fill_cmd(c, BMIC_SENSE_CONTROLLER_PARAMETERS, h, buf, bufsize,
2557316b221aSStephen M. Cameron 			page, scsi3addr, TYPE_CMD)) {
2558316b221aSStephen M. Cameron 		rc = -1;
2559316b221aSStephen M. Cameron 		goto out;
2560316b221aSStephen M. Cameron 	}
256125163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
256225163bd5SWebb Scales 			PCI_DMA_FROMDEVICE, NO_TIMEOUT);
256325163bd5SWebb Scales 	if (rc)
256425163bd5SWebb Scales 		goto out;
2565316b221aSStephen M. Cameron 	ei = c->err_info;
2566316b221aSStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2567316b221aSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2568316b221aSStephen M. Cameron 		rc = -1;
2569316b221aSStephen M. Cameron 	}
2570316b221aSStephen M. Cameron out:
257145fcb86eSStephen Cameron 	cmd_free(h, c);
2572316b221aSStephen M. Cameron 	return rc;
2573316b221aSStephen M. Cameron }
2574316b221aSStephen M. Cameron 
2575bf711ac6SScott Teel static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
257625163bd5SWebb Scales 	u8 reset_type, int reply_queue)
2577edd16368SStephen M. Cameron {
2578edd16368SStephen M. Cameron 	int rc = IO_OK;
2579edd16368SStephen M. Cameron 	struct CommandList *c;
2580edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2581edd16368SStephen M. Cameron 
258245fcb86eSStephen Cameron 	c = cmd_alloc(h);
2583edd16368SStephen M. Cameron 
2584edd16368SStephen M. Cameron 
2585a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map. */
2586bf711ac6SScott Teel 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
2587bf711ac6SScott Teel 			scsi3addr, TYPE_MSG);
2588bf711ac6SScott Teel 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to LUN reset */
258925163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
259025163bd5SWebb Scales 	if (rc) {
259125163bd5SWebb Scales 		dev_warn(&h->pdev->dev, "Failed to send reset command\n");
259225163bd5SWebb Scales 		goto out;
259325163bd5SWebb Scales 	}
2594edd16368SStephen M. Cameron 	/* no unmap needed here because no data xfer. */
2595edd16368SStephen M. Cameron 
2596edd16368SStephen M. Cameron 	ei = c->err_info;
2597edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0) {
2598d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2599edd16368SStephen M. Cameron 		rc = -1;
2600edd16368SStephen M. Cameron 	}
260125163bd5SWebb Scales out:
260245fcb86eSStephen Cameron 	cmd_free(h, c);
2603edd16368SStephen M. Cameron 	return rc;
2604edd16368SStephen M. Cameron }
2605edd16368SStephen M. Cameron 
2606edd16368SStephen M. Cameron static void hpsa_get_raid_level(struct ctlr_info *h,
2607edd16368SStephen M. Cameron 	unsigned char *scsi3addr, unsigned char *raid_level)
2608edd16368SStephen M. Cameron {
2609edd16368SStephen M. Cameron 	int rc;
2610edd16368SStephen M. Cameron 	unsigned char *buf;
2611edd16368SStephen M. Cameron 
2612edd16368SStephen M. Cameron 	*raid_level = RAID_UNKNOWN;
2613edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2614edd16368SStephen M. Cameron 	if (!buf)
2615edd16368SStephen M. Cameron 		return;
2616b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0xC1, buf, 64);
2617edd16368SStephen M. Cameron 	if (rc == 0)
2618edd16368SStephen M. Cameron 		*raid_level = buf[8];
2619edd16368SStephen M. Cameron 	if (*raid_level > RAID_UNKNOWN)
2620edd16368SStephen M. Cameron 		*raid_level = RAID_UNKNOWN;
2621edd16368SStephen M. Cameron 	kfree(buf);
2622edd16368SStephen M. Cameron 	return;
2623edd16368SStephen M. Cameron }
2624edd16368SStephen M. Cameron 
2625283b4a9bSStephen M. Cameron #define HPSA_MAP_DEBUG
2626283b4a9bSStephen M. Cameron #ifdef HPSA_MAP_DEBUG
2627283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
2628283b4a9bSStephen M. Cameron 				struct raid_map_data *map_buff)
2629283b4a9bSStephen M. Cameron {
2630283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map_buff->data[0];
2631283b4a9bSStephen M. Cameron 	int map, row, col;
2632283b4a9bSStephen M. Cameron 	u16 map_cnt, row_cnt, disks_per_row;
2633283b4a9bSStephen M. Cameron 
2634283b4a9bSStephen M. Cameron 	if (rc != 0)
2635283b4a9bSStephen M. Cameron 		return;
2636283b4a9bSStephen M. Cameron 
26372ba8bfc8SStephen M. Cameron 	/* Show details only if debugging has been activated. */
26382ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug < 2)
26392ba8bfc8SStephen M. Cameron 		return;
26402ba8bfc8SStephen M. Cameron 
2641283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "structure_size = %u\n",
2642283b4a9bSStephen M. Cameron 				le32_to_cpu(map_buff->structure_size));
2643283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
2644283b4a9bSStephen M. Cameron 			le32_to_cpu(map_buff->volume_blk_size));
2645283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
2646283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->volume_blk_cnt));
2647283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
2648283b4a9bSStephen M. Cameron 			map_buff->phys_blk_shift);
2649283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
2650283b4a9bSStephen M. Cameron 			map_buff->parity_rotation_shift);
2651283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "strip_size = %u\n",
2652283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->strip_size));
2653283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
2654283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_starting_blk));
2655283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
2656283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_blk_cnt));
2657283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
2658283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->data_disks_per_row));
2659283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
2660283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->metadata_disks_per_row));
2661283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "row_cnt = %u\n",
2662283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->row_cnt));
2663283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "layout_map_count = %u\n",
2664283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->layout_map_count));
26652b08b3e9SDon Brace 	dev_info(&h->pdev->dev, "flags = 0x%x\n",
2666dd0e19f3SScott Teel 			le16_to_cpu(map_buff->flags));
26672b08b3e9SDon Brace 	dev_info(&h->pdev->dev, "encrypytion = %s\n",
26682b08b3e9SDon Brace 			le16_to_cpu(map_buff->flags) &
26692b08b3e9SDon Brace 			RAID_MAP_FLAG_ENCRYPT_ON ?  "ON" : "OFF");
2670dd0e19f3SScott Teel 	dev_info(&h->pdev->dev, "dekindex = %u\n",
2671dd0e19f3SScott Teel 			le16_to_cpu(map_buff->dekindex));
2672283b4a9bSStephen M. Cameron 	map_cnt = le16_to_cpu(map_buff->layout_map_count);
2673283b4a9bSStephen M. Cameron 	for (map = 0; map < map_cnt; map++) {
2674283b4a9bSStephen M. Cameron 		dev_info(&h->pdev->dev, "Map%u:\n", map);
2675283b4a9bSStephen M. Cameron 		row_cnt = le16_to_cpu(map_buff->row_cnt);
2676283b4a9bSStephen M. Cameron 		for (row = 0; row < row_cnt; row++) {
2677283b4a9bSStephen M. Cameron 			dev_info(&h->pdev->dev, "  Row%u:\n", row);
2678283b4a9bSStephen M. Cameron 			disks_per_row =
2679283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->data_disks_per_row);
2680283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2681283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2682283b4a9bSStephen M. Cameron 					"    D%02u: h=0x%04x xor=%u,%u\n",
2683283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2684283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2685283b4a9bSStephen M. Cameron 			disks_per_row =
2686283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->metadata_disks_per_row);
2687283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2688283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2689283b4a9bSStephen M. Cameron 					"    M%02u: h=0x%04x xor=%u,%u\n",
2690283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2691283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2692283b4a9bSStephen M. Cameron 		}
2693283b4a9bSStephen M. Cameron 	}
2694283b4a9bSStephen M. Cameron }
2695283b4a9bSStephen M. Cameron #else
2696283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
2697283b4a9bSStephen M. Cameron 			__attribute__((unused)) int rc,
2698283b4a9bSStephen M. Cameron 			__attribute__((unused)) struct raid_map_data *map_buff)
2699283b4a9bSStephen M. Cameron {
2700283b4a9bSStephen M. Cameron }
2701283b4a9bSStephen M. Cameron #endif
2702283b4a9bSStephen M. Cameron 
2703283b4a9bSStephen M. Cameron static int hpsa_get_raid_map(struct ctlr_info *h,
2704283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2705283b4a9bSStephen M. Cameron {
2706283b4a9bSStephen M. Cameron 	int rc = 0;
2707283b4a9bSStephen M. Cameron 	struct CommandList *c;
2708283b4a9bSStephen M. Cameron 	struct ErrorInfo *ei;
2709283b4a9bSStephen M. Cameron 
271045fcb86eSStephen Cameron 	c = cmd_alloc(h);
2711bf43caf3SRobert Elliott 
2712283b4a9bSStephen M. Cameron 	if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
2713283b4a9bSStephen M. Cameron 			sizeof(this_device->raid_map), 0,
2714283b4a9bSStephen M. Cameron 			scsi3addr, TYPE_CMD)) {
27152dd02d74SRobert Elliott 		dev_warn(&h->pdev->dev, "hpsa_get_raid_map fill_cmd failed\n");
27162dd02d74SRobert Elliott 		cmd_free(h, c);
27172dd02d74SRobert Elliott 		return -1;
2718283b4a9bSStephen M. Cameron 	}
271925163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
272025163bd5SWebb Scales 					PCI_DMA_FROMDEVICE, NO_TIMEOUT);
272125163bd5SWebb Scales 	if (rc)
272225163bd5SWebb Scales 		goto out;
2723283b4a9bSStephen M. Cameron 	ei = c->err_info;
2724283b4a9bSStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2725d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
272625163bd5SWebb Scales 		rc = -1;
272725163bd5SWebb Scales 		goto out;
2728283b4a9bSStephen M. Cameron 	}
272945fcb86eSStephen Cameron 	cmd_free(h, c);
2730283b4a9bSStephen M. Cameron 
2731283b4a9bSStephen M. Cameron 	/* @todo in the future, dynamically allocate RAID map memory */
2732283b4a9bSStephen M. Cameron 	if (le32_to_cpu(this_device->raid_map.structure_size) >
2733283b4a9bSStephen M. Cameron 				sizeof(this_device->raid_map)) {
2734283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
2735283b4a9bSStephen M. Cameron 		rc = -1;
2736283b4a9bSStephen M. Cameron 	}
2737283b4a9bSStephen M. Cameron 	hpsa_debug_map_buff(h, rc, &this_device->raid_map);
2738283b4a9bSStephen M. Cameron 	return rc;
273925163bd5SWebb Scales out:
274025163bd5SWebb Scales 	cmd_free(h, c);
274125163bd5SWebb Scales 	return rc;
2742283b4a9bSStephen M. Cameron }
2743283b4a9bSStephen M. Cameron 
274403383736SDon Brace static int hpsa_bmic_id_physical_device(struct ctlr_info *h,
274503383736SDon Brace 		unsigned char scsi3addr[], u16 bmic_device_index,
274603383736SDon Brace 		struct bmic_identify_physical_device *buf, size_t bufsize)
274703383736SDon Brace {
274803383736SDon Brace 	int rc = IO_OK;
274903383736SDon Brace 	struct CommandList *c;
275003383736SDon Brace 	struct ErrorInfo *ei;
275103383736SDon Brace 
275203383736SDon Brace 	c = cmd_alloc(h);
275303383736SDon Brace 	rc = fill_cmd(c, BMIC_IDENTIFY_PHYSICAL_DEVICE, h, buf, bufsize,
275403383736SDon Brace 		0, RAID_CTLR_LUNID, TYPE_CMD);
275503383736SDon Brace 	if (rc)
275603383736SDon Brace 		goto out;
275703383736SDon Brace 
275803383736SDon Brace 	c->Request.CDB[2] = bmic_device_index & 0xff;
275903383736SDon Brace 	c->Request.CDB[9] = (bmic_device_index >> 8) & 0xff;
276003383736SDon Brace 
276125163bd5SWebb Scales 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE,
276225163bd5SWebb Scales 						NO_TIMEOUT);
276303383736SDon Brace 	ei = c->err_info;
276403383736SDon Brace 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
276503383736SDon Brace 		hpsa_scsi_interpret_error(h, c);
276603383736SDon Brace 		rc = -1;
276703383736SDon Brace 	}
276803383736SDon Brace out:
276903383736SDon Brace 	cmd_free(h, c);
277003383736SDon Brace 	return rc;
277103383736SDon Brace }
277203383736SDon Brace 
27731b70150aSStephen M. Cameron static int hpsa_vpd_page_supported(struct ctlr_info *h,
27741b70150aSStephen M. Cameron 	unsigned char scsi3addr[], u8 page)
27751b70150aSStephen M. Cameron {
27761b70150aSStephen M. Cameron 	int rc;
27771b70150aSStephen M. Cameron 	int i;
27781b70150aSStephen M. Cameron 	int pages;
27791b70150aSStephen M. Cameron 	unsigned char *buf, bufsize;
27801b70150aSStephen M. Cameron 
27811b70150aSStephen M. Cameron 	buf = kzalloc(256, GFP_KERNEL);
27821b70150aSStephen M. Cameron 	if (!buf)
27831b70150aSStephen M. Cameron 		return 0;
27841b70150aSStephen M. Cameron 
27851b70150aSStephen M. Cameron 	/* Get the size of the page list first */
27861b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
27871b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
27881b70150aSStephen M. Cameron 				buf, HPSA_VPD_HEADER_SZ);
27891b70150aSStephen M. Cameron 	if (rc != 0)
27901b70150aSStephen M. Cameron 		goto exit_unsupported;
27911b70150aSStephen M. Cameron 	pages = buf[3];
27921b70150aSStephen M. Cameron 	if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
27931b70150aSStephen M. Cameron 		bufsize = pages + HPSA_VPD_HEADER_SZ;
27941b70150aSStephen M. Cameron 	else
27951b70150aSStephen M. Cameron 		bufsize = 255;
27961b70150aSStephen M. Cameron 
27971b70150aSStephen M. Cameron 	/* Get the whole VPD page list */
27981b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
27991b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
28001b70150aSStephen M. Cameron 				buf, bufsize);
28011b70150aSStephen M. Cameron 	if (rc != 0)
28021b70150aSStephen M. Cameron 		goto exit_unsupported;
28031b70150aSStephen M. Cameron 
28041b70150aSStephen M. Cameron 	pages = buf[3];
28051b70150aSStephen M. Cameron 	for (i = 1; i <= pages; i++)
28061b70150aSStephen M. Cameron 		if (buf[3 + i] == page)
28071b70150aSStephen M. Cameron 			goto exit_supported;
28081b70150aSStephen M. Cameron exit_unsupported:
28091b70150aSStephen M. Cameron 	kfree(buf);
28101b70150aSStephen M. Cameron 	return 0;
28111b70150aSStephen M. Cameron exit_supported:
28121b70150aSStephen M. Cameron 	kfree(buf);
28131b70150aSStephen M. Cameron 	return 1;
28141b70150aSStephen M. Cameron }
28151b70150aSStephen M. Cameron 
2816283b4a9bSStephen M. Cameron static void hpsa_get_ioaccel_status(struct ctlr_info *h,
2817283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2818283b4a9bSStephen M. Cameron {
2819283b4a9bSStephen M. Cameron 	int rc;
2820283b4a9bSStephen M. Cameron 	unsigned char *buf;
2821283b4a9bSStephen M. Cameron 	u8 ioaccel_status;
2822283b4a9bSStephen M. Cameron 
2823283b4a9bSStephen M. Cameron 	this_device->offload_config = 0;
2824283b4a9bSStephen M. Cameron 	this_device->offload_enabled = 0;
282541ce4c35SStephen Cameron 	this_device->offload_to_be_enabled = 0;
2826283b4a9bSStephen M. Cameron 
2827283b4a9bSStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2828283b4a9bSStephen M. Cameron 	if (!buf)
2829283b4a9bSStephen M. Cameron 		return;
28301b70150aSStephen M. Cameron 	if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
28311b70150aSStephen M. Cameron 		goto out;
2832283b4a9bSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
2833b7bb24ebSStephen M. Cameron 			VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
2834283b4a9bSStephen M. Cameron 	if (rc != 0)
2835283b4a9bSStephen M. Cameron 		goto out;
2836283b4a9bSStephen M. Cameron 
2837283b4a9bSStephen M. Cameron #define IOACCEL_STATUS_BYTE 4
2838283b4a9bSStephen M. Cameron #define OFFLOAD_CONFIGURED_BIT 0x01
2839283b4a9bSStephen M. Cameron #define OFFLOAD_ENABLED_BIT 0x02
2840283b4a9bSStephen M. Cameron 	ioaccel_status = buf[IOACCEL_STATUS_BYTE];
2841283b4a9bSStephen M. Cameron 	this_device->offload_config =
2842283b4a9bSStephen M. Cameron 		!!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
2843283b4a9bSStephen M. Cameron 	if (this_device->offload_config) {
2844283b4a9bSStephen M. Cameron 		this_device->offload_enabled =
2845283b4a9bSStephen M. Cameron 			!!(ioaccel_status & OFFLOAD_ENABLED_BIT);
2846283b4a9bSStephen M. Cameron 		if (hpsa_get_raid_map(h, scsi3addr, this_device))
2847283b4a9bSStephen M. Cameron 			this_device->offload_enabled = 0;
2848283b4a9bSStephen M. Cameron 	}
284941ce4c35SStephen Cameron 	this_device->offload_to_be_enabled = this_device->offload_enabled;
2850283b4a9bSStephen M. Cameron out:
2851283b4a9bSStephen M. Cameron 	kfree(buf);
2852283b4a9bSStephen M. Cameron 	return;
2853283b4a9bSStephen M. Cameron }
2854283b4a9bSStephen M. Cameron 
2855edd16368SStephen M. Cameron /* Get the device id from inquiry page 0x83 */
2856edd16368SStephen M. Cameron static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
2857edd16368SStephen M. Cameron 	unsigned char *device_id, int buflen)
2858edd16368SStephen M. Cameron {
2859edd16368SStephen M. Cameron 	int rc;
2860edd16368SStephen M. Cameron 	unsigned char *buf;
2861edd16368SStephen M. Cameron 
2862edd16368SStephen M. Cameron 	if (buflen > 16)
2863edd16368SStephen M. Cameron 		buflen = 16;
2864edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2865edd16368SStephen M. Cameron 	if (!buf)
2866a84d794dSStephen M. Cameron 		return -ENOMEM;
2867b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0x83, buf, 64);
2868edd16368SStephen M. Cameron 	if (rc == 0)
2869edd16368SStephen M. Cameron 		memcpy(device_id, &buf[8], buflen);
2870edd16368SStephen M. Cameron 	kfree(buf);
2871edd16368SStephen M. Cameron 	return rc != 0;
2872edd16368SStephen M. Cameron }
2873edd16368SStephen M. Cameron 
2874edd16368SStephen M. Cameron static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
287503383736SDon Brace 		void *buf, int bufsize,
2876edd16368SStephen M. Cameron 		int extended_response)
2877edd16368SStephen M. Cameron {
2878edd16368SStephen M. Cameron 	int rc = IO_OK;
2879edd16368SStephen M. Cameron 	struct CommandList *c;
2880edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
2881edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2882edd16368SStephen M. Cameron 
288345fcb86eSStephen Cameron 	c = cmd_alloc(h);
2884bf43caf3SRobert Elliott 
2885e89c0ae7SStephen M. Cameron 	/* address the controller */
2886e89c0ae7SStephen M. Cameron 	memset(scsi3addr, 0, sizeof(scsi3addr));
2887a2dac136SStephen M. Cameron 	if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
2888a2dac136SStephen M. Cameron 		buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
2889a2dac136SStephen M. Cameron 		rc = -1;
2890a2dac136SStephen M. Cameron 		goto out;
2891a2dac136SStephen M. Cameron 	}
2892edd16368SStephen M. Cameron 	if (extended_response)
2893edd16368SStephen M. Cameron 		c->Request.CDB[1] = extended_response;
289425163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
289525163bd5SWebb Scales 					PCI_DMA_FROMDEVICE, NO_TIMEOUT);
289625163bd5SWebb Scales 	if (rc)
289725163bd5SWebb Scales 		goto out;
2898edd16368SStephen M. Cameron 	ei = c->err_info;
2899edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 &&
2900edd16368SStephen M. Cameron 	    ei->CommandStatus != CMD_DATA_UNDERRUN) {
2901d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2902edd16368SStephen M. Cameron 		rc = -1;
2903283b4a9bSStephen M. Cameron 	} else {
290403383736SDon Brace 		struct ReportLUNdata *rld = buf;
290503383736SDon Brace 
290603383736SDon Brace 		if (rld->extended_response_flag != extended_response) {
2907283b4a9bSStephen M. Cameron 			dev_err(&h->pdev->dev,
2908283b4a9bSStephen M. Cameron 				"report luns requested format %u, got %u\n",
2909283b4a9bSStephen M. Cameron 				extended_response,
291003383736SDon Brace 				rld->extended_response_flag);
2911283b4a9bSStephen M. Cameron 			rc = -1;
2912283b4a9bSStephen M. Cameron 		}
2913edd16368SStephen M. Cameron 	}
2914a2dac136SStephen M. Cameron out:
291545fcb86eSStephen Cameron 	cmd_free(h, c);
2916edd16368SStephen M. Cameron 	return rc;
2917edd16368SStephen M. Cameron }
2918edd16368SStephen M. Cameron 
2919edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
292003383736SDon Brace 		struct ReportExtendedLUNdata *buf, int bufsize)
2921edd16368SStephen M. Cameron {
292203383736SDon Brace 	return hpsa_scsi_do_report_luns(h, 0, buf, bufsize,
292303383736SDon Brace 						HPSA_REPORT_PHYS_EXTENDED);
2924edd16368SStephen M. Cameron }
2925edd16368SStephen M. Cameron 
2926edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
2927edd16368SStephen M. Cameron 		struct ReportLUNdata *buf, int bufsize)
2928edd16368SStephen M. Cameron {
2929edd16368SStephen M. Cameron 	return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
2930edd16368SStephen M. Cameron }
2931edd16368SStephen M. Cameron 
2932edd16368SStephen M. Cameron static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
2933edd16368SStephen M. Cameron 	int bus, int target, int lun)
2934edd16368SStephen M. Cameron {
2935edd16368SStephen M. Cameron 	device->bus = bus;
2936edd16368SStephen M. Cameron 	device->target = target;
2937edd16368SStephen M. Cameron 	device->lun = lun;
2938edd16368SStephen M. Cameron }
2939edd16368SStephen M. Cameron 
29409846590eSStephen M. Cameron /* Use VPD inquiry to get details of volume status */
29419846590eSStephen M. Cameron static int hpsa_get_volume_status(struct ctlr_info *h,
29429846590eSStephen M. Cameron 					unsigned char scsi3addr[])
29439846590eSStephen M. Cameron {
29449846590eSStephen M. Cameron 	int rc;
29459846590eSStephen M. Cameron 	int status;
29469846590eSStephen M. Cameron 	int size;
29479846590eSStephen M. Cameron 	unsigned char *buf;
29489846590eSStephen M. Cameron 
29499846590eSStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
29509846590eSStephen M. Cameron 	if (!buf)
29519846590eSStephen M. Cameron 		return HPSA_VPD_LV_STATUS_UNSUPPORTED;
29529846590eSStephen M. Cameron 
29539846590eSStephen M. Cameron 	/* Does controller have VPD for logical volume status? */
295424a4b078SStephen M. Cameron 	if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_STATUS))
29559846590eSStephen M. Cameron 		goto exit_failed;
29569846590eSStephen M. Cameron 
29579846590eSStephen M. Cameron 	/* Get the size of the VPD return buffer */
29589846590eSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
29599846590eSStephen M. Cameron 					buf, HPSA_VPD_HEADER_SZ);
296024a4b078SStephen M. Cameron 	if (rc != 0)
29619846590eSStephen M. Cameron 		goto exit_failed;
29629846590eSStephen M. Cameron 	size = buf[3];
29639846590eSStephen M. Cameron 
29649846590eSStephen M. Cameron 	/* Now get the whole VPD buffer */
29659846590eSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
29669846590eSStephen M. Cameron 					buf, size + HPSA_VPD_HEADER_SZ);
296724a4b078SStephen M. Cameron 	if (rc != 0)
29689846590eSStephen M. Cameron 		goto exit_failed;
29699846590eSStephen M. Cameron 	status = buf[4]; /* status byte */
29709846590eSStephen M. Cameron 
29719846590eSStephen M. Cameron 	kfree(buf);
29729846590eSStephen M. Cameron 	return status;
29739846590eSStephen M. Cameron exit_failed:
29749846590eSStephen M. Cameron 	kfree(buf);
29759846590eSStephen M. Cameron 	return HPSA_VPD_LV_STATUS_UNSUPPORTED;
29769846590eSStephen M. Cameron }
29779846590eSStephen M. Cameron 
29789846590eSStephen M. Cameron /* Determine offline status of a volume.
29799846590eSStephen M. Cameron  * Return either:
29809846590eSStephen M. Cameron  *  0 (not offline)
298167955ba3SStephen M. Cameron  *  0xff (offline for unknown reasons)
29829846590eSStephen M. Cameron  *  # (integer code indicating one of several NOT READY states
29839846590eSStephen M. Cameron  *     describing why a volume is to be kept offline)
29849846590eSStephen M. Cameron  */
298567955ba3SStephen M. Cameron static int hpsa_volume_offline(struct ctlr_info *h,
29869846590eSStephen M. Cameron 					unsigned char scsi3addr[])
29879846590eSStephen M. Cameron {
29889846590eSStephen M. Cameron 	struct CommandList *c;
29899437ac43SStephen Cameron 	unsigned char *sense;
29909437ac43SStephen Cameron 	u8 sense_key, asc, ascq;
29919437ac43SStephen Cameron 	int sense_len;
299225163bd5SWebb Scales 	int rc, ldstat = 0;
29939846590eSStephen M. Cameron 	u16 cmd_status;
29949846590eSStephen M. Cameron 	u8 scsi_status;
29959846590eSStephen M. Cameron #define ASC_LUN_NOT_READY 0x04
29969846590eSStephen M. Cameron #define ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS 0x04
29979846590eSStephen M. Cameron #define ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ 0x02
29989846590eSStephen M. Cameron 
29999846590eSStephen M. Cameron 	c = cmd_alloc(h);
3000bf43caf3SRobert Elliott 
30019846590eSStephen M. Cameron 	(void) fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, scsi3addr, TYPE_CMD);
300225163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
300325163bd5SWebb Scales 	if (rc) {
300425163bd5SWebb Scales 		cmd_free(h, c);
300525163bd5SWebb Scales 		return 0;
300625163bd5SWebb Scales 	}
30079846590eSStephen M. Cameron 	sense = c->err_info->SenseInfo;
30089437ac43SStephen Cameron 	if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
30099437ac43SStephen Cameron 		sense_len = sizeof(c->err_info->SenseInfo);
30109437ac43SStephen Cameron 	else
30119437ac43SStephen Cameron 		sense_len = c->err_info->SenseLen;
30129437ac43SStephen Cameron 	decode_sense_data(sense, sense_len, &sense_key, &asc, &ascq);
30139846590eSStephen M. Cameron 	cmd_status = c->err_info->CommandStatus;
30149846590eSStephen M. Cameron 	scsi_status = c->err_info->ScsiStatus;
30159846590eSStephen M. Cameron 	cmd_free(h, c);
30169846590eSStephen M. Cameron 	/* Is the volume 'not ready'? */
30179846590eSStephen M. Cameron 	if (cmd_status != CMD_TARGET_STATUS ||
30189846590eSStephen M. Cameron 		scsi_status != SAM_STAT_CHECK_CONDITION ||
30199846590eSStephen M. Cameron 		sense_key != NOT_READY ||
30209846590eSStephen M. Cameron 		asc != ASC_LUN_NOT_READY)  {
30219846590eSStephen M. Cameron 		return 0;
30229846590eSStephen M. Cameron 	}
30239846590eSStephen M. Cameron 
30249846590eSStephen M. Cameron 	/* Determine the reason for not ready state */
30259846590eSStephen M. Cameron 	ldstat = hpsa_get_volume_status(h, scsi3addr);
30269846590eSStephen M. Cameron 
30279846590eSStephen M. Cameron 	/* Keep volume offline in certain cases: */
30289846590eSStephen M. Cameron 	switch (ldstat) {
30299846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ERASE:
30309846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_RPI:
30319846590eSStephen M. Cameron 	case HPSA_LV_PENDING_RPI:
30329846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_NO_KEY:
30339846590eSStephen M. Cameron 	case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
30349846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION:
30359846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
30369846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
30379846590eSStephen M. Cameron 		return ldstat;
30389846590eSStephen M. Cameron 	case HPSA_VPD_LV_STATUS_UNSUPPORTED:
30399846590eSStephen M. Cameron 		/* If VPD status page isn't available,
30409846590eSStephen M. Cameron 		 * use ASC/ASCQ to determine state
30419846590eSStephen M. Cameron 		 */
30429846590eSStephen M. Cameron 		if ((ascq == ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS) ||
30439846590eSStephen M. Cameron 			(ascq == ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ))
30449846590eSStephen M. Cameron 			return ldstat;
30459846590eSStephen M. Cameron 		break;
30469846590eSStephen M. Cameron 	default:
30479846590eSStephen M. Cameron 		break;
30489846590eSStephen M. Cameron 	}
30499846590eSStephen M. Cameron 	return 0;
30509846590eSStephen M. Cameron }
30519846590eSStephen M. Cameron 
30529b5c48c2SStephen Cameron /*
30539b5c48c2SStephen Cameron  * Find out if a logical device supports aborts by simply trying one.
30549b5c48c2SStephen Cameron  * Smart Array may claim not to support aborts on logical drives, but
30559b5c48c2SStephen Cameron  * if a MSA2000 * is connected, the drives on that will be presented
30569b5c48c2SStephen Cameron  * by the Smart Array as logical drives, and aborts may be sent to
30579b5c48c2SStephen Cameron  * those devices successfully.  So the simplest way to find out is
30589b5c48c2SStephen Cameron  * to simply try an abort and see how the device responds.
30599b5c48c2SStephen Cameron  */
30609b5c48c2SStephen Cameron static int hpsa_device_supports_aborts(struct ctlr_info *h,
30619b5c48c2SStephen Cameron 					unsigned char *scsi3addr)
30629b5c48c2SStephen Cameron {
30639b5c48c2SStephen Cameron 	struct CommandList *c;
30649b5c48c2SStephen Cameron 	struct ErrorInfo *ei;
30659b5c48c2SStephen Cameron 	int rc = 0;
30669b5c48c2SStephen Cameron 
30679b5c48c2SStephen Cameron 	u64 tag = (u64) -1; /* bogus tag */
30689b5c48c2SStephen Cameron 
30699b5c48c2SStephen Cameron 	/* Assume that physical devices support aborts */
30709b5c48c2SStephen Cameron 	if (!is_logical_dev_addr_mode(scsi3addr))
30719b5c48c2SStephen Cameron 		return 1;
30729b5c48c2SStephen Cameron 
30739b5c48c2SStephen Cameron 	c = cmd_alloc(h);
3074bf43caf3SRobert Elliott 
30759b5c48c2SStephen Cameron 	(void) fill_cmd(c, HPSA_ABORT_MSG, h, &tag, 0, 0, scsi3addr, TYPE_MSG);
30769b5c48c2SStephen Cameron 	(void) hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
30779b5c48c2SStephen Cameron 	/* no unmap needed here because no data xfer. */
30789b5c48c2SStephen Cameron 	ei = c->err_info;
30799b5c48c2SStephen Cameron 	switch (ei->CommandStatus) {
30809b5c48c2SStephen Cameron 	case CMD_INVALID:
30819b5c48c2SStephen Cameron 		rc = 0;
30829b5c48c2SStephen Cameron 		break;
30839b5c48c2SStephen Cameron 	case CMD_UNABORTABLE:
30849b5c48c2SStephen Cameron 	case CMD_ABORT_FAILED:
30859b5c48c2SStephen Cameron 		rc = 1;
30869b5c48c2SStephen Cameron 		break;
30879437ac43SStephen Cameron 	case CMD_TMF_STATUS:
30889437ac43SStephen Cameron 		rc = hpsa_evaluate_tmf_status(h, c);
30899437ac43SStephen Cameron 		break;
30909b5c48c2SStephen Cameron 	default:
30919b5c48c2SStephen Cameron 		rc = 0;
30929b5c48c2SStephen Cameron 		break;
30939b5c48c2SStephen Cameron 	}
30949b5c48c2SStephen Cameron 	cmd_free(h, c);
30959b5c48c2SStephen Cameron 	return rc;
30969b5c48c2SStephen Cameron }
30979b5c48c2SStephen Cameron 
3098edd16368SStephen M. Cameron static int hpsa_update_device_info(struct ctlr_info *h,
30990b0e1d6cSStephen M. Cameron 	unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
31000b0e1d6cSStephen M. Cameron 	unsigned char *is_OBDR_device)
3101edd16368SStephen M. Cameron {
31020b0e1d6cSStephen M. Cameron 
31030b0e1d6cSStephen M. Cameron #define OBDR_SIG_OFFSET 43
31040b0e1d6cSStephen M. Cameron #define OBDR_TAPE_SIG "$DR-10"
31050b0e1d6cSStephen M. Cameron #define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
31060b0e1d6cSStephen M. Cameron #define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
31070b0e1d6cSStephen M. Cameron 
3108ea6d3bc3SStephen M. Cameron 	unsigned char *inq_buff;
31090b0e1d6cSStephen M. Cameron 	unsigned char *obdr_sig;
3110edd16368SStephen M. Cameron 
3111ea6d3bc3SStephen M. Cameron 	inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
3112edd16368SStephen M. Cameron 	if (!inq_buff)
3113edd16368SStephen M. Cameron 		goto bail_out;
3114edd16368SStephen M. Cameron 
3115edd16368SStephen M. Cameron 	/* Do an inquiry to the device to see what it is. */
3116edd16368SStephen M. Cameron 	if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
3117edd16368SStephen M. Cameron 		(unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
3118edd16368SStephen M. Cameron 		/* Inquiry failed (msg printed already) */
3119edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev,
3120edd16368SStephen M. Cameron 			"hpsa_update_device_info: inquiry failed\n");
3121edd16368SStephen M. Cameron 		goto bail_out;
3122edd16368SStephen M. Cameron 	}
3123edd16368SStephen M. Cameron 
3124edd16368SStephen M. Cameron 	this_device->devtype = (inq_buff[0] & 0x1f);
3125edd16368SStephen M. Cameron 	memcpy(this_device->scsi3addr, scsi3addr, 8);
3126edd16368SStephen M. Cameron 	memcpy(this_device->vendor, &inq_buff[8],
3127edd16368SStephen M. Cameron 		sizeof(this_device->vendor));
3128edd16368SStephen M. Cameron 	memcpy(this_device->model, &inq_buff[16],
3129edd16368SStephen M. Cameron 		sizeof(this_device->model));
3130edd16368SStephen M. Cameron 	memset(this_device->device_id, 0,
3131edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
3132edd16368SStephen M. Cameron 	hpsa_get_device_id(h, scsi3addr, this_device->device_id,
3133edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
3134edd16368SStephen M. Cameron 
3135edd16368SStephen M. Cameron 	if (this_device->devtype == TYPE_DISK &&
3136283b4a9bSStephen M. Cameron 		is_logical_dev_addr_mode(scsi3addr)) {
313767955ba3SStephen M. Cameron 		int volume_offline;
313867955ba3SStephen M. Cameron 
3139edd16368SStephen M. Cameron 		hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
3140283b4a9bSStephen M. Cameron 		if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
3141283b4a9bSStephen M. Cameron 			hpsa_get_ioaccel_status(h, scsi3addr, this_device);
314267955ba3SStephen M. Cameron 		volume_offline = hpsa_volume_offline(h, scsi3addr);
314367955ba3SStephen M. Cameron 		if (volume_offline < 0 || volume_offline > 0xff)
314467955ba3SStephen M. Cameron 			volume_offline = HPSA_VPD_LV_STATUS_UNSUPPORTED;
314567955ba3SStephen M. Cameron 		this_device->volume_offline = volume_offline & 0xff;
3146283b4a9bSStephen M. Cameron 	} else {
3147edd16368SStephen M. Cameron 		this_device->raid_level = RAID_UNKNOWN;
3148283b4a9bSStephen M. Cameron 		this_device->offload_config = 0;
3149283b4a9bSStephen M. Cameron 		this_device->offload_enabled = 0;
315041ce4c35SStephen Cameron 		this_device->offload_to_be_enabled = 0;
3151a3144e0bSJoe Handzik 		this_device->hba_ioaccel_enabled = 0;
31529846590eSStephen M. Cameron 		this_device->volume_offline = 0;
315303383736SDon Brace 		this_device->queue_depth = h->nr_cmds;
3154283b4a9bSStephen M. Cameron 	}
3155edd16368SStephen M. Cameron 
31560b0e1d6cSStephen M. Cameron 	if (is_OBDR_device) {
31570b0e1d6cSStephen M. Cameron 		/* See if this is a One-Button-Disaster-Recovery device
31580b0e1d6cSStephen M. Cameron 		 * by looking for "$DR-10" at offset 43 in inquiry data.
31590b0e1d6cSStephen M. Cameron 		 */
31600b0e1d6cSStephen M. Cameron 		obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
31610b0e1d6cSStephen M. Cameron 		*is_OBDR_device = (this_device->devtype == TYPE_ROM &&
31620b0e1d6cSStephen M. Cameron 					strncmp(obdr_sig, OBDR_TAPE_SIG,
31630b0e1d6cSStephen M. Cameron 						OBDR_SIG_LEN) == 0);
31640b0e1d6cSStephen M. Cameron 	}
3165edd16368SStephen M. Cameron 	kfree(inq_buff);
3166edd16368SStephen M. Cameron 	return 0;
3167edd16368SStephen M. Cameron 
3168edd16368SStephen M. Cameron bail_out:
3169edd16368SStephen M. Cameron 	kfree(inq_buff);
3170edd16368SStephen M. Cameron 	return 1;
3171edd16368SStephen M. Cameron }
3172edd16368SStephen M. Cameron 
31739b5c48c2SStephen Cameron static void hpsa_update_device_supports_aborts(struct ctlr_info *h,
31749b5c48c2SStephen Cameron 			struct hpsa_scsi_dev_t *dev, u8 *scsi3addr)
31759b5c48c2SStephen Cameron {
31769b5c48c2SStephen Cameron 	unsigned long flags;
31779b5c48c2SStephen Cameron 	int rc, entry;
31789b5c48c2SStephen Cameron 	/*
31799b5c48c2SStephen Cameron 	 * See if this device supports aborts.  If we already know
31809b5c48c2SStephen Cameron 	 * the device, we already know if it supports aborts, otherwise
31819b5c48c2SStephen Cameron 	 * we have to find out if it supports aborts by trying one.
31829b5c48c2SStephen Cameron 	 */
31839b5c48c2SStephen Cameron 	spin_lock_irqsave(&h->devlock, flags);
31849b5c48c2SStephen Cameron 	rc = hpsa_scsi_find_entry(dev, h->dev, h->ndevices, &entry);
31859b5c48c2SStephen Cameron 	if ((rc == DEVICE_SAME || rc == DEVICE_UPDATED) &&
31869b5c48c2SStephen Cameron 		entry >= 0 && entry < h->ndevices) {
31879b5c48c2SStephen Cameron 		dev->supports_aborts = h->dev[entry]->supports_aborts;
31889b5c48c2SStephen Cameron 		spin_unlock_irqrestore(&h->devlock, flags);
31899b5c48c2SStephen Cameron 	} else {
31909b5c48c2SStephen Cameron 		spin_unlock_irqrestore(&h->devlock, flags);
31919b5c48c2SStephen Cameron 		dev->supports_aborts =
31929b5c48c2SStephen Cameron 				hpsa_device_supports_aborts(h, scsi3addr);
31939b5c48c2SStephen Cameron 		if (dev->supports_aborts < 0)
31949b5c48c2SStephen Cameron 			dev->supports_aborts = 0;
31959b5c48c2SStephen Cameron 	}
31969b5c48c2SStephen Cameron }
31979b5c48c2SStephen Cameron 
31984f4eb9f1SScott Teel static unsigned char *ext_target_model[] = {
3199edd16368SStephen M. Cameron 	"MSA2012",
3200edd16368SStephen M. Cameron 	"MSA2024",
3201edd16368SStephen M. Cameron 	"MSA2312",
3202edd16368SStephen M. Cameron 	"MSA2324",
3203fda38518SStephen M. Cameron 	"P2000 G3 SAS",
3204e06c8e5cSStephen M. Cameron 	"MSA 2040 SAS",
3205edd16368SStephen M. Cameron 	NULL,
3206edd16368SStephen M. Cameron };
3207edd16368SStephen M. Cameron 
32084f4eb9f1SScott Teel static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
3209edd16368SStephen M. Cameron {
3210edd16368SStephen M. Cameron 	int i;
3211edd16368SStephen M. Cameron 
32124f4eb9f1SScott Teel 	for (i = 0; ext_target_model[i]; i++)
32134f4eb9f1SScott Teel 		if (strncmp(device->model, ext_target_model[i],
32144f4eb9f1SScott Teel 			strlen(ext_target_model[i])) == 0)
3215edd16368SStephen M. Cameron 			return 1;
3216edd16368SStephen M. Cameron 	return 0;
3217edd16368SStephen M. Cameron }
3218edd16368SStephen M. Cameron 
3219edd16368SStephen M. Cameron /* Helper function to assign bus, target, lun mapping of devices.
32204f4eb9f1SScott Teel  * Puts non-external target logical volumes on bus 0, external target logical
3221edd16368SStephen M. Cameron  * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
3222edd16368SStephen M. Cameron  * Logical drive target and lun are assigned at this time, but
3223edd16368SStephen M. Cameron  * physical device lun and target assignment are deferred (assigned
3224edd16368SStephen M. Cameron  * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
3225edd16368SStephen M. Cameron  */
3226edd16368SStephen M. Cameron static void figure_bus_target_lun(struct ctlr_info *h,
32271f310bdeSStephen M. Cameron 	u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
3228edd16368SStephen M. Cameron {
32291f310bdeSStephen M. Cameron 	u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
3230edd16368SStephen M. Cameron 
32311f310bdeSStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes)) {
32321f310bdeSStephen M. Cameron 		/* physical device, target and lun filled in later */
32331f310bdeSStephen M. Cameron 		if (is_hba_lunid(lunaddrbytes))
32341f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
32351f310bdeSStephen M. Cameron 		else
32361f310bdeSStephen M. Cameron 			/* defer target, lun assignment for physical devices */
32371f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 2, -1, -1);
32381f310bdeSStephen M. Cameron 		return;
32391f310bdeSStephen M. Cameron 	}
32401f310bdeSStephen M. Cameron 	/* It's a logical device */
32414f4eb9f1SScott Teel 	if (is_ext_target(h, device)) {
32424f4eb9f1SScott Teel 		/* external target way, put logicals on bus 1
3243339b2b14SStephen M. Cameron 		 * and match target/lun numbers box
32441f310bdeSStephen M. Cameron 		 * reports, other smart array, bus 0, target 0, match lunid
3245339b2b14SStephen M. Cameron 		 */
32461f310bdeSStephen M. Cameron 		hpsa_set_bus_target_lun(device,
32471f310bdeSStephen M. Cameron 			1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
32481f310bdeSStephen M. Cameron 		return;
3249339b2b14SStephen M. Cameron 	}
32501f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
3251edd16368SStephen M. Cameron }
3252edd16368SStephen M. Cameron 
3253edd16368SStephen M. Cameron /*
3254edd16368SStephen M. Cameron  * If there is no lun 0 on a target, linux won't find any devices.
32554f4eb9f1SScott Teel  * For the external targets (arrays), we have to manually detect the enclosure
3256edd16368SStephen M. Cameron  * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
3257edd16368SStephen M. Cameron  * it for some reason.  *tmpdevice is the target we're adding,
3258edd16368SStephen M. Cameron  * this_device is a pointer into the current element of currentsd[]
3259edd16368SStephen M. Cameron  * that we're building up in update_scsi_devices(), below.
3260edd16368SStephen M. Cameron  * lunzerobits is a bitmap that tracks which targets already have a
3261edd16368SStephen M. Cameron  * lun 0 assigned.
3262edd16368SStephen M. Cameron  * Returns 1 if an enclosure was added, 0 if not.
3263edd16368SStephen M. Cameron  */
32644f4eb9f1SScott Teel static int add_ext_target_dev(struct ctlr_info *h,
3265edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *tmpdevice,
326601a02ffcSStephen M. Cameron 	struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
32674f4eb9f1SScott Teel 	unsigned long lunzerobits[], int *n_ext_target_devs)
3268edd16368SStephen M. Cameron {
3269edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
3270edd16368SStephen M. Cameron 
32711f310bdeSStephen M. Cameron 	if (test_bit(tmpdevice->target, lunzerobits))
3272edd16368SStephen M. Cameron 		return 0; /* There is already a lun 0 on this target. */
3273edd16368SStephen M. Cameron 
3274edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes))
3275edd16368SStephen M. Cameron 		return 0; /* It's the logical targets that may lack lun 0. */
3276edd16368SStephen M. Cameron 
32774f4eb9f1SScott Teel 	if (!is_ext_target(h, tmpdevice))
32784f4eb9f1SScott Teel 		return 0; /* Only external target devices have this problem. */
3279edd16368SStephen M. Cameron 
32801f310bdeSStephen M. Cameron 	if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
3281edd16368SStephen M. Cameron 		return 0;
3282edd16368SStephen M. Cameron 
3283c4f8a299SStephen M. Cameron 	memset(scsi3addr, 0, 8);
32841f310bdeSStephen M. Cameron 	scsi3addr[3] = tmpdevice->target;
3285edd16368SStephen M. Cameron 	if (is_hba_lunid(scsi3addr))
3286edd16368SStephen M. Cameron 		return 0; /* Don't add the RAID controller here. */
3287edd16368SStephen M. Cameron 
3288339b2b14SStephen M. Cameron 	if (is_scsi_rev_5(h))
3289339b2b14SStephen M. Cameron 		return 0; /* p1210m doesn't need to do this. */
3290339b2b14SStephen M. Cameron 
32914f4eb9f1SScott Teel 	if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
3292aca4a520SScott Teel 		dev_warn(&h->pdev->dev, "Maximum number of external "
3293aca4a520SScott Teel 			"target devices exceeded.  Check your hardware "
3294edd16368SStephen M. Cameron 			"configuration.");
3295edd16368SStephen M. Cameron 		return 0;
3296edd16368SStephen M. Cameron 	}
3297edd16368SStephen M. Cameron 
32980b0e1d6cSStephen M. Cameron 	if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
3299edd16368SStephen M. Cameron 		return 0;
33004f4eb9f1SScott Teel 	(*n_ext_target_devs)++;
33011f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(this_device,
33021f310bdeSStephen M. Cameron 				tmpdevice->bus, tmpdevice->target, 0);
33039b5c48c2SStephen Cameron 	hpsa_update_device_supports_aborts(h, this_device, scsi3addr);
33041f310bdeSStephen M. Cameron 	set_bit(tmpdevice->target, lunzerobits);
3305edd16368SStephen M. Cameron 	return 1;
3306edd16368SStephen M. Cameron }
3307edd16368SStephen M. Cameron 
3308edd16368SStephen M. Cameron /*
330954b6e9e9SScott Teel  * Get address of physical disk used for an ioaccel2 mode command:
331054b6e9e9SScott Teel  *	1. Extract ioaccel2 handle from the command.
331154b6e9e9SScott Teel  *	2. Find a matching ioaccel2 handle from list of physical disks.
331254b6e9e9SScott Teel  *	3. Return:
331354b6e9e9SScott Teel  *		1 and set scsi3addr to address of matching physical
331454b6e9e9SScott Teel  *		0 if no matching physical disk was found.
331554b6e9e9SScott Teel  */
331654b6e9e9SScott Teel static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
331754b6e9e9SScott Teel 	struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
331854b6e9e9SScott Teel {
331941ce4c35SStephen Cameron 	struct io_accel2_cmd *c2 =
332041ce4c35SStephen Cameron 			&h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
332141ce4c35SStephen Cameron 	unsigned long flags;
332254b6e9e9SScott Teel 	int i;
332354b6e9e9SScott Teel 
332441ce4c35SStephen Cameron 	spin_lock_irqsave(&h->devlock, flags);
332541ce4c35SStephen Cameron 	for (i = 0; i < h->ndevices; i++)
332641ce4c35SStephen Cameron 		if (h->dev[i]->ioaccel_handle == le32_to_cpu(c2->scsi_nexus)) {
332741ce4c35SStephen Cameron 			memcpy(scsi3addr, h->dev[i]->scsi3addr,
332841ce4c35SStephen Cameron 				sizeof(h->dev[i]->scsi3addr));
332941ce4c35SStephen Cameron 			spin_unlock_irqrestore(&h->devlock, flags);
333054b6e9e9SScott Teel 			return 1;
333154b6e9e9SScott Teel 		}
333241ce4c35SStephen Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
333341ce4c35SStephen Cameron 	return 0;
333441ce4c35SStephen Cameron }
333541ce4c35SStephen Cameron 
333654b6e9e9SScott Teel /*
3337edd16368SStephen M. Cameron  * Do CISS_REPORT_PHYS and CISS_REPORT_LOG.  Data is returned in physdev,
3338edd16368SStephen M. Cameron  * logdev.  The number of luns in physdev and logdev are returned in
3339edd16368SStephen M. Cameron  * *nphysicals and *nlogicals, respectively.
3340edd16368SStephen M. Cameron  * Returns 0 on success, -1 otherwise.
3341edd16368SStephen M. Cameron  */
3342edd16368SStephen M. Cameron static int hpsa_gather_lun_info(struct ctlr_info *h,
334303383736SDon Brace 	struct ReportExtendedLUNdata *physdev, u32 *nphysicals,
334401a02ffcSStephen M. Cameron 	struct ReportLUNdata *logdev, u32 *nlogicals)
3345edd16368SStephen M. Cameron {
334603383736SDon Brace 	if (hpsa_scsi_do_report_phys_luns(h, physdev, sizeof(*physdev))) {
3347edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
3348edd16368SStephen M. Cameron 		return -1;
3349edd16368SStephen M. Cameron 	}
335003383736SDon Brace 	*nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 24;
3351edd16368SStephen M. Cameron 	if (*nphysicals > HPSA_MAX_PHYS_LUN) {
335203383736SDon Brace 		dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded. %d LUNs ignored.\n",
335303383736SDon Brace 			HPSA_MAX_PHYS_LUN, *nphysicals - HPSA_MAX_PHYS_LUN);
3354edd16368SStephen M. Cameron 		*nphysicals = HPSA_MAX_PHYS_LUN;
3355edd16368SStephen M. Cameron 	}
335603383736SDon Brace 	if (hpsa_scsi_do_report_log_luns(h, logdev, sizeof(*logdev))) {
3357edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
3358edd16368SStephen M. Cameron 		return -1;
3359edd16368SStephen M. Cameron 	}
33606df1e954SStephen M. Cameron 	*nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
3361edd16368SStephen M. Cameron 	/* Reject Logicals in excess of our max capability. */
3362edd16368SStephen M. Cameron 	if (*nlogicals > HPSA_MAX_LUN) {
3363edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
3364edd16368SStephen M. Cameron 			"maximum logical LUNs (%d) exceeded.  "
3365edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_LUN,
3366edd16368SStephen M. Cameron 			*nlogicals - HPSA_MAX_LUN);
3367edd16368SStephen M. Cameron 			*nlogicals = HPSA_MAX_LUN;
3368edd16368SStephen M. Cameron 	}
3369edd16368SStephen M. Cameron 	if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
3370edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
3371edd16368SStephen M. Cameron 			"maximum logical + physical LUNs (%d) exceeded. "
3372edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
3373edd16368SStephen M. Cameron 			*nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
3374edd16368SStephen M. Cameron 		*nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
3375edd16368SStephen M. Cameron 	}
3376edd16368SStephen M. Cameron 	return 0;
3377edd16368SStephen M. Cameron }
3378edd16368SStephen M. Cameron 
337942a91641SDon Brace static u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position,
338042a91641SDon Brace 	int i, int nphysicals, int nlogicals,
3381a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list,
3382339b2b14SStephen M. Cameron 	struct ReportLUNdata *logdev_list)
3383339b2b14SStephen M. Cameron {
3384339b2b14SStephen M. Cameron 	/* Helper function, figure out where the LUN ID info is coming from
3385339b2b14SStephen M. Cameron 	 * given index i, lists of physical and logical devices, where in
3386339b2b14SStephen M. Cameron 	 * the list the raid controller is supposed to appear (first or last)
3387339b2b14SStephen M. Cameron 	 */
3388339b2b14SStephen M. Cameron 
3389339b2b14SStephen M. Cameron 	int logicals_start = nphysicals + (raid_ctlr_position == 0);
3390339b2b14SStephen M. Cameron 	int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
3391339b2b14SStephen M. Cameron 
3392339b2b14SStephen M. Cameron 	if (i == raid_ctlr_position)
3393339b2b14SStephen M. Cameron 		return RAID_CTLR_LUNID;
3394339b2b14SStephen M. Cameron 
3395339b2b14SStephen M. Cameron 	if (i < logicals_start)
3396d5b5d964SStephen M. Cameron 		return &physdev_list->LUN[i -
3397d5b5d964SStephen M. Cameron 				(raid_ctlr_position == 0)].lunid[0];
3398339b2b14SStephen M. Cameron 
3399339b2b14SStephen M. Cameron 	if (i < last_device)
3400339b2b14SStephen M. Cameron 		return &logdev_list->LUN[i - nphysicals -
3401339b2b14SStephen M. Cameron 			(raid_ctlr_position == 0)][0];
3402339b2b14SStephen M. Cameron 	BUG();
3403339b2b14SStephen M. Cameron 	return NULL;
3404339b2b14SStephen M. Cameron }
3405339b2b14SStephen M. Cameron 
3406316b221aSStephen M. Cameron static int hpsa_hba_mode_enabled(struct ctlr_info *h)
3407316b221aSStephen M. Cameron {
3408316b221aSStephen M. Cameron 	int rc;
34096e8e8088SJoe Handzik 	int hba_mode_enabled;
3410316b221aSStephen M. Cameron 	struct bmic_controller_parameters *ctlr_params;
3411316b221aSStephen M. Cameron 	ctlr_params = kzalloc(sizeof(struct bmic_controller_parameters),
3412316b221aSStephen M. Cameron 		GFP_KERNEL);
3413316b221aSStephen M. Cameron 
3414316b221aSStephen M. Cameron 	if (!ctlr_params)
341596444fbbSJoe Handzik 		return -ENOMEM;
3416316b221aSStephen M. Cameron 	rc = hpsa_bmic_ctrl_mode_sense(h, RAID_CTLR_LUNID, 0, ctlr_params,
3417316b221aSStephen M. Cameron 		sizeof(struct bmic_controller_parameters));
341896444fbbSJoe Handzik 	if (rc) {
3419316b221aSStephen M. Cameron 		kfree(ctlr_params);
342096444fbbSJoe Handzik 		return rc;
3421316b221aSStephen M. Cameron 	}
34226e8e8088SJoe Handzik 
34236e8e8088SJoe Handzik 	hba_mode_enabled =
34246e8e8088SJoe Handzik 		((ctlr_params->nvram_flags & HBA_MODE_ENABLED_FLAG) != 0);
34256e8e8088SJoe Handzik 	kfree(ctlr_params);
34266e8e8088SJoe Handzik 	return hba_mode_enabled;
3427316b221aSStephen M. Cameron }
3428316b221aSStephen M. Cameron 
342903383736SDon Brace /* get physical drive ioaccel handle and queue depth */
343003383736SDon Brace static void hpsa_get_ioaccel_drive_info(struct ctlr_info *h,
343103383736SDon Brace 		struct hpsa_scsi_dev_t *dev,
343203383736SDon Brace 		u8 *lunaddrbytes,
343303383736SDon Brace 		struct bmic_identify_physical_device *id_phys)
343403383736SDon Brace {
343503383736SDon Brace 	int rc;
343603383736SDon Brace 	struct ext_report_lun_entry *rle =
343703383736SDon Brace 		(struct ext_report_lun_entry *) lunaddrbytes;
343803383736SDon Brace 
343903383736SDon Brace 	dev->ioaccel_handle = rle->ioaccel_handle;
3440a3144e0bSJoe Handzik 	if (PHYS_IOACCEL(lunaddrbytes) && dev->ioaccel_handle)
3441a3144e0bSJoe Handzik 		dev->hba_ioaccel_enabled = 1;
344203383736SDon Brace 	memset(id_phys, 0, sizeof(*id_phys));
344303383736SDon Brace 	rc = hpsa_bmic_id_physical_device(h, lunaddrbytes,
344403383736SDon Brace 			GET_BMIC_DRIVE_NUMBER(lunaddrbytes), id_phys,
344503383736SDon Brace 			sizeof(*id_phys));
344603383736SDon Brace 	if (!rc)
344703383736SDon Brace 		/* Reserve space for FW operations */
344803383736SDon Brace #define DRIVE_CMDS_RESERVED_FOR_FW 2
344903383736SDon Brace #define DRIVE_QUEUE_DEPTH 7
345003383736SDon Brace 		dev->queue_depth =
345103383736SDon Brace 			le16_to_cpu(id_phys->current_queue_depth_limit) -
345203383736SDon Brace 				DRIVE_CMDS_RESERVED_FOR_FW;
345303383736SDon Brace 	else
345403383736SDon Brace 		dev->queue_depth = DRIVE_QUEUE_DEPTH; /* conservative */
345503383736SDon Brace 	atomic_set(&dev->ioaccel_cmds_out, 0);
345603383736SDon Brace }
345703383736SDon Brace 
3458edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
3459edd16368SStephen M. Cameron {
3460edd16368SStephen M. Cameron 	/* the idea here is we could get notified
3461edd16368SStephen M. Cameron 	 * that some devices have changed, so we do a report
3462edd16368SStephen M. Cameron 	 * physical luns and report logical luns cmd, and adjust
3463edd16368SStephen M. Cameron 	 * our list of devices accordingly.
3464edd16368SStephen M. Cameron 	 *
3465edd16368SStephen M. Cameron 	 * The scsi3addr's of devices won't change so long as the
3466edd16368SStephen M. Cameron 	 * adapter is not reset.  That means we can rescan and
3467edd16368SStephen M. Cameron 	 * tell which devices we already know about, vs. new
3468edd16368SStephen M. Cameron 	 * devices, vs.  disappearing devices.
3469edd16368SStephen M. Cameron 	 */
3470a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list = NULL;
3471edd16368SStephen M. Cameron 	struct ReportLUNdata *logdev_list = NULL;
347203383736SDon Brace 	struct bmic_identify_physical_device *id_phys = NULL;
347301a02ffcSStephen M. Cameron 	u32 nphysicals = 0;
347401a02ffcSStephen M. Cameron 	u32 nlogicals = 0;
347501a02ffcSStephen M. Cameron 	u32 ndev_allocated = 0;
3476edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
3477edd16368SStephen M. Cameron 	int ncurrent = 0;
34784f4eb9f1SScott Teel 	int i, n_ext_target_devs, ndevs_to_allocate;
3479339b2b14SStephen M. Cameron 	int raid_ctlr_position;
34802bbf5c7fSJoe Handzik 	int rescan_hba_mode;
3481aca4a520SScott Teel 	DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
3482edd16368SStephen M. Cameron 
3483cfe5badcSScott Teel 	currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
348492084715SStephen M. Cameron 	physdev_list = kzalloc(sizeof(*physdev_list), GFP_KERNEL);
348592084715SStephen M. Cameron 	logdev_list = kzalloc(sizeof(*logdev_list), GFP_KERNEL);
3486edd16368SStephen M. Cameron 	tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
348703383736SDon Brace 	id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
3488edd16368SStephen M. Cameron 
348903383736SDon Brace 	if (!currentsd || !physdev_list || !logdev_list ||
349003383736SDon Brace 		!tmpdevice || !id_phys) {
3491edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory\n");
3492edd16368SStephen M. Cameron 		goto out;
3493edd16368SStephen M. Cameron 	}
3494edd16368SStephen M. Cameron 	memset(lunzerobits, 0, sizeof(lunzerobits));
3495edd16368SStephen M. Cameron 
3496316b221aSStephen M. Cameron 	rescan_hba_mode = hpsa_hba_mode_enabled(h);
349796444fbbSJoe Handzik 	if (rescan_hba_mode < 0)
349896444fbbSJoe Handzik 		goto out;
3499316b221aSStephen M. Cameron 
3500316b221aSStephen M. Cameron 	if (!h->hba_mode_enabled && rescan_hba_mode)
3501316b221aSStephen M. Cameron 		dev_warn(&h->pdev->dev, "HBA mode enabled\n");
3502316b221aSStephen M. Cameron 	else if (h->hba_mode_enabled && !rescan_hba_mode)
3503316b221aSStephen M. Cameron 		dev_warn(&h->pdev->dev, "HBA mode disabled\n");
3504316b221aSStephen M. Cameron 
3505316b221aSStephen M. Cameron 	h->hba_mode_enabled = rescan_hba_mode;
3506316b221aSStephen M. Cameron 
350703383736SDon Brace 	if (hpsa_gather_lun_info(h, physdev_list, &nphysicals,
350803383736SDon Brace 			logdev_list, &nlogicals))
3509edd16368SStephen M. Cameron 		goto out;
3510edd16368SStephen M. Cameron 
3511aca4a520SScott Teel 	/* We might see up to the maximum number of logical and physical disks
3512aca4a520SScott Teel 	 * plus external target devices, and a device for the local RAID
3513aca4a520SScott Teel 	 * controller.
3514edd16368SStephen M. Cameron 	 */
3515aca4a520SScott Teel 	ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
3516edd16368SStephen M. Cameron 
3517edd16368SStephen M. Cameron 	/* Allocate the per device structures */
3518edd16368SStephen M. Cameron 	for (i = 0; i < ndevs_to_allocate; i++) {
3519b7ec021fSScott Teel 		if (i >= HPSA_MAX_DEVICES) {
3520b7ec021fSScott Teel 			dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
3521b7ec021fSScott Teel 				"  %d devices ignored.\n", HPSA_MAX_DEVICES,
3522b7ec021fSScott Teel 				ndevs_to_allocate - HPSA_MAX_DEVICES);
3523b7ec021fSScott Teel 			break;
3524b7ec021fSScott Teel 		}
3525b7ec021fSScott Teel 
3526edd16368SStephen M. Cameron 		currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
3527edd16368SStephen M. Cameron 		if (!currentsd[i]) {
3528edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
3529edd16368SStephen M. Cameron 				__FILE__, __LINE__);
3530edd16368SStephen M. Cameron 			goto out;
3531edd16368SStephen M. Cameron 		}
3532edd16368SStephen M. Cameron 		ndev_allocated++;
3533edd16368SStephen M. Cameron 	}
3534edd16368SStephen M. Cameron 
35358645291bSStephen M. Cameron 	if (is_scsi_rev_5(h))
3536339b2b14SStephen M. Cameron 		raid_ctlr_position = 0;
3537339b2b14SStephen M. Cameron 	else
3538339b2b14SStephen M. Cameron 		raid_ctlr_position = nphysicals + nlogicals;
3539339b2b14SStephen M. Cameron 
3540edd16368SStephen M. Cameron 	/* adjust our table of devices */
35414f4eb9f1SScott Teel 	n_ext_target_devs = 0;
3542edd16368SStephen M. Cameron 	for (i = 0; i < nphysicals + nlogicals + 1; i++) {
35430b0e1d6cSStephen M. Cameron 		u8 *lunaddrbytes, is_OBDR = 0;
3544edd16368SStephen M. Cameron 
3545edd16368SStephen M. Cameron 		/* Figure out where the LUN ID info is coming from */
3546339b2b14SStephen M. Cameron 		lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
3547339b2b14SStephen M. Cameron 			i, nphysicals, nlogicals, physdev_list, logdev_list);
354841ce4c35SStephen Cameron 
354941ce4c35SStephen Cameron 		/* skip masked non-disk devices */
355041ce4c35SStephen Cameron 		if (MASKED_DEVICE(lunaddrbytes))
355141ce4c35SStephen Cameron 			if (i < nphysicals + (raid_ctlr_position == 0) &&
355241ce4c35SStephen Cameron 				NON_DISK_PHYS_DEV(lunaddrbytes))
3553edd16368SStephen M. Cameron 				continue;
3554edd16368SStephen M. Cameron 
3555edd16368SStephen M. Cameron 		/* Get device type, vendor, model, device id */
35560b0e1d6cSStephen M. Cameron 		if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
35570b0e1d6cSStephen M. Cameron 							&is_OBDR))
3558edd16368SStephen M. Cameron 			continue; /* skip it if we can't talk to it. */
35591f310bdeSStephen M. Cameron 		figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
35609b5c48c2SStephen Cameron 		hpsa_update_device_supports_aborts(h, tmpdevice, lunaddrbytes);
3561edd16368SStephen M. Cameron 		this_device = currentsd[ncurrent];
3562edd16368SStephen M. Cameron 
3563edd16368SStephen M. Cameron 		/*
35644f4eb9f1SScott Teel 		 * For external target devices, we have to insert a LUN 0 which
3565edd16368SStephen M. Cameron 		 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
3566edd16368SStephen M. Cameron 		 * is nonetheless an enclosure device there.  We have to
3567edd16368SStephen M. Cameron 		 * present that otherwise linux won't find anything if
3568edd16368SStephen M. Cameron 		 * there is no lun 0.
3569edd16368SStephen M. Cameron 		 */
35704f4eb9f1SScott Teel 		if (add_ext_target_dev(h, tmpdevice, this_device,
35711f310bdeSStephen M. Cameron 				lunaddrbytes, lunzerobits,
35724f4eb9f1SScott Teel 				&n_ext_target_devs)) {
3573edd16368SStephen M. Cameron 			ncurrent++;
3574edd16368SStephen M. Cameron 			this_device = currentsd[ncurrent];
3575edd16368SStephen M. Cameron 		}
3576edd16368SStephen M. Cameron 
3577edd16368SStephen M. Cameron 		*this_device = *tmpdevice;
3578edd16368SStephen M. Cameron 
357941ce4c35SStephen Cameron 		/* do not expose masked devices */
358041ce4c35SStephen Cameron 		if (MASKED_DEVICE(lunaddrbytes) &&
358141ce4c35SStephen Cameron 			i < nphysicals + (raid_ctlr_position == 0)) {
358241ce4c35SStephen Cameron 			if (h->hba_mode_enabled)
358341ce4c35SStephen Cameron 				dev_warn(&h->pdev->dev,
358441ce4c35SStephen Cameron 					"Masked physical device detected\n");
358541ce4c35SStephen Cameron 			this_device->expose_state = HPSA_DO_NOT_EXPOSE;
358641ce4c35SStephen Cameron 		} else {
358741ce4c35SStephen Cameron 			this_device->expose_state =
358841ce4c35SStephen Cameron 					HPSA_SG_ATTACH | HPSA_ULD_ATTACH;
358941ce4c35SStephen Cameron 		}
359041ce4c35SStephen Cameron 
3591edd16368SStephen M. Cameron 		switch (this_device->devtype) {
35920b0e1d6cSStephen M. Cameron 		case TYPE_ROM:
3593edd16368SStephen M. Cameron 			/* We don't *really* support actual CD-ROM devices,
3594edd16368SStephen M. Cameron 			 * just "One Button Disaster Recovery" tape drive
3595edd16368SStephen M. Cameron 			 * which temporarily pretends to be a CD-ROM drive.
3596edd16368SStephen M. Cameron 			 * So we check that the device is really an OBDR tape
3597edd16368SStephen M. Cameron 			 * device by checking for "$DR-10" in bytes 43-48 of
3598edd16368SStephen M. Cameron 			 * the inquiry data.
3599edd16368SStephen M. Cameron 			 */
36000b0e1d6cSStephen M. Cameron 			if (is_OBDR)
3601edd16368SStephen M. Cameron 				ncurrent++;
3602edd16368SStephen M. Cameron 			break;
3603edd16368SStephen M. Cameron 		case TYPE_DISK:
3604283b4a9bSStephen M. Cameron 			if (i >= nphysicals) {
3605283b4a9bSStephen M. Cameron 				ncurrent++;
3606edd16368SStephen M. Cameron 				break;
3607283b4a9bSStephen M. Cameron 			}
3608ecf418d1SJoe Handzik 
3609ecf418d1SJoe Handzik 			if (h->hba_mode_enabled)
3610ecf418d1SJoe Handzik 				/* never use raid mapper in HBA mode */
3611ecf418d1SJoe Handzik 				this_device->offload_enabled = 0;
3612ecf418d1SJoe Handzik 			else if (!(h->transMethod & CFGTBL_Trans_io_accel1 ||
3613ecf418d1SJoe Handzik 				h->transMethod & CFGTBL_Trans_io_accel2))
3614316b221aSStephen M. Cameron 				break;
3615ecf418d1SJoe Handzik 
361603383736SDon Brace 			hpsa_get_ioaccel_drive_info(h, this_device,
361703383736SDon Brace 						lunaddrbytes, id_phys);
361803383736SDon Brace 			atomic_set(&this_device->ioaccel_cmds_out, 0);
3619edd16368SStephen M. Cameron 			ncurrent++;
3620edd16368SStephen M. Cameron 			break;
3621edd16368SStephen M. Cameron 		case TYPE_TAPE:
3622edd16368SStephen M. Cameron 		case TYPE_MEDIUM_CHANGER:
3623edd16368SStephen M. Cameron 			ncurrent++;
3624edd16368SStephen M. Cameron 			break;
362541ce4c35SStephen Cameron 		case TYPE_ENCLOSURE:
362641ce4c35SStephen Cameron 			if (h->hba_mode_enabled)
362741ce4c35SStephen Cameron 				ncurrent++;
362841ce4c35SStephen Cameron 			break;
3629edd16368SStephen M. Cameron 		case TYPE_RAID:
3630edd16368SStephen M. Cameron 			/* Only present the Smartarray HBA as a RAID controller.
3631edd16368SStephen M. Cameron 			 * If it's a RAID controller other than the HBA itself
3632edd16368SStephen M. Cameron 			 * (an external RAID controller, MSA500 or similar)
3633edd16368SStephen M. Cameron 			 * don't present it.
3634edd16368SStephen M. Cameron 			 */
3635edd16368SStephen M. Cameron 			if (!is_hba_lunid(lunaddrbytes))
3636edd16368SStephen M. Cameron 				break;
3637edd16368SStephen M. Cameron 			ncurrent++;
3638edd16368SStephen M. Cameron 			break;
3639edd16368SStephen M. Cameron 		default:
3640edd16368SStephen M. Cameron 			break;
3641edd16368SStephen M. Cameron 		}
3642cfe5badcSScott Teel 		if (ncurrent >= HPSA_MAX_DEVICES)
3643edd16368SStephen M. Cameron 			break;
3644edd16368SStephen M. Cameron 	}
3645edd16368SStephen M. Cameron 	adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
3646edd16368SStephen M. Cameron out:
3647edd16368SStephen M. Cameron 	kfree(tmpdevice);
3648edd16368SStephen M. Cameron 	for (i = 0; i < ndev_allocated; i++)
3649edd16368SStephen M. Cameron 		kfree(currentsd[i]);
3650edd16368SStephen M. Cameron 	kfree(currentsd);
3651edd16368SStephen M. Cameron 	kfree(physdev_list);
3652edd16368SStephen M. Cameron 	kfree(logdev_list);
365303383736SDon Brace 	kfree(id_phys);
3654edd16368SStephen M. Cameron }
3655edd16368SStephen M. Cameron 
3656ec5cbf04SWebb Scales static void hpsa_set_sg_descriptor(struct SGDescriptor *desc,
3657ec5cbf04SWebb Scales 				   struct scatterlist *sg)
3658ec5cbf04SWebb Scales {
3659ec5cbf04SWebb Scales 	u64 addr64 = (u64) sg_dma_address(sg);
3660ec5cbf04SWebb Scales 	unsigned int len = sg_dma_len(sg);
3661ec5cbf04SWebb Scales 
3662ec5cbf04SWebb Scales 	desc->Addr = cpu_to_le64(addr64);
3663ec5cbf04SWebb Scales 	desc->Len = cpu_to_le32(len);
3664ec5cbf04SWebb Scales 	desc->Ext = 0;
3665ec5cbf04SWebb Scales }
3666ec5cbf04SWebb Scales 
3667c7ee65b3SWebb Scales /*
3668c7ee65b3SWebb Scales  * hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
3669edd16368SStephen M. Cameron  * dma mapping  and fills in the scatter gather entries of the
3670edd16368SStephen M. Cameron  * hpsa command, cp.
3671edd16368SStephen M. Cameron  */
367233a2ffceSStephen M. Cameron static int hpsa_scatter_gather(struct ctlr_info *h,
3673edd16368SStephen M. Cameron 		struct CommandList *cp,
3674edd16368SStephen M. Cameron 		struct scsi_cmnd *cmd)
3675edd16368SStephen M. Cameron {
3676edd16368SStephen M. Cameron 	struct scatterlist *sg;
3677b3a7ba7cSWebb Scales 	int use_sg, i, sg_limit, chained, last_sg;
367833a2ffceSStephen M. Cameron 	struct SGDescriptor *curr_sg;
3679edd16368SStephen M. Cameron 
368033a2ffceSStephen M. Cameron 	BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
3681edd16368SStephen M. Cameron 
3682edd16368SStephen M. Cameron 	use_sg = scsi_dma_map(cmd);
3683edd16368SStephen M. Cameron 	if (use_sg < 0)
3684edd16368SStephen M. Cameron 		return use_sg;
3685edd16368SStephen M. Cameron 
3686edd16368SStephen M. Cameron 	if (!use_sg)
3687edd16368SStephen M. Cameron 		goto sglist_finished;
3688edd16368SStephen M. Cameron 
3689b3a7ba7cSWebb Scales 	/*
3690b3a7ba7cSWebb Scales 	 * If the number of entries is greater than the max for a single list,
3691b3a7ba7cSWebb Scales 	 * then we have a chained list; we will set up all but one entry in the
3692b3a7ba7cSWebb Scales 	 * first list (the last entry is saved for link information);
3693b3a7ba7cSWebb Scales 	 * otherwise, we don't have a chained list and we'll set up at each of
3694b3a7ba7cSWebb Scales 	 * the entries in the one list.
3695b3a7ba7cSWebb Scales 	 */
369633a2ffceSStephen M. Cameron 	curr_sg = cp->SG;
3697b3a7ba7cSWebb Scales 	chained = use_sg > h->max_cmd_sg_entries;
3698b3a7ba7cSWebb Scales 	sg_limit = chained ? h->max_cmd_sg_entries - 1 : use_sg;
3699b3a7ba7cSWebb Scales 	last_sg = scsi_sg_count(cmd) - 1;
3700b3a7ba7cSWebb Scales 	scsi_for_each_sg(cmd, sg, sg_limit, i) {
3701ec5cbf04SWebb Scales 		hpsa_set_sg_descriptor(curr_sg, sg);
370233a2ffceSStephen M. Cameron 		curr_sg++;
370333a2ffceSStephen M. Cameron 	}
3704ec5cbf04SWebb Scales 
3705b3a7ba7cSWebb Scales 	if (chained) {
3706b3a7ba7cSWebb Scales 		/*
3707b3a7ba7cSWebb Scales 		 * Continue with the chained list.  Set curr_sg to the chained
3708b3a7ba7cSWebb Scales 		 * list.  Modify the limit to the total count less the entries
3709b3a7ba7cSWebb Scales 		 * we've already set up.  Resume the scan at the list entry
3710b3a7ba7cSWebb Scales 		 * where the previous loop left off.
3711b3a7ba7cSWebb Scales 		 */
3712b3a7ba7cSWebb Scales 		curr_sg = h->cmd_sg_list[cp->cmdindex];
3713b3a7ba7cSWebb Scales 		sg_limit = use_sg - sg_limit;
3714b3a7ba7cSWebb Scales 		for_each_sg(sg, sg, sg_limit, i) {
3715b3a7ba7cSWebb Scales 			hpsa_set_sg_descriptor(curr_sg, sg);
3716b3a7ba7cSWebb Scales 			curr_sg++;
3717b3a7ba7cSWebb Scales 		}
3718b3a7ba7cSWebb Scales 	}
3719b3a7ba7cSWebb Scales 
3720ec5cbf04SWebb Scales 	/* Back the pointer up to the last entry and mark it as "last". */
3721b3a7ba7cSWebb Scales 	(curr_sg - 1)->Ext = cpu_to_le32(HPSA_SG_LAST);
372233a2ffceSStephen M. Cameron 
372333a2ffceSStephen M. Cameron 	if (use_sg + chained > h->maxSG)
372433a2ffceSStephen M. Cameron 		h->maxSG = use_sg + chained;
372533a2ffceSStephen M. Cameron 
372633a2ffceSStephen M. Cameron 	if (chained) {
372733a2ffceSStephen M. Cameron 		cp->Header.SGList = h->max_cmd_sg_entries;
372850a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(use_sg + 1);
3729e2bea6dfSStephen M. Cameron 		if (hpsa_map_sg_chain_block(h, cp)) {
3730e2bea6dfSStephen M. Cameron 			scsi_dma_unmap(cmd);
3731e2bea6dfSStephen M. Cameron 			return -1;
3732e2bea6dfSStephen M. Cameron 		}
373333a2ffceSStephen M. Cameron 		return 0;
3734edd16368SStephen M. Cameron 	}
3735edd16368SStephen M. Cameron 
3736edd16368SStephen M. Cameron sglist_finished:
3737edd16368SStephen M. Cameron 
373801a02ffcSStephen M. Cameron 	cp->Header.SGList = (u8) use_sg;   /* no. SGs contig in this cmd */
3739c7ee65b3SWebb Scales 	cp->Header.SGTotal = cpu_to_le16(use_sg); /* total sgs in cmd list */
3740edd16368SStephen M. Cameron 	return 0;
3741edd16368SStephen M. Cameron }
3742edd16368SStephen M. Cameron 
3743283b4a9bSStephen M. Cameron #define IO_ACCEL_INELIGIBLE (1)
3744283b4a9bSStephen M. Cameron static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
3745283b4a9bSStephen M. Cameron {
3746283b4a9bSStephen M. Cameron 	int is_write = 0;
3747283b4a9bSStephen M. Cameron 	u32 block;
3748283b4a9bSStephen M. Cameron 	u32 block_cnt;
3749283b4a9bSStephen M. Cameron 
3750283b4a9bSStephen M. Cameron 	/* Perform some CDB fixups if needed using 10 byte reads/writes only */
3751283b4a9bSStephen M. Cameron 	switch (cdb[0]) {
3752283b4a9bSStephen M. Cameron 	case WRITE_6:
3753283b4a9bSStephen M. Cameron 	case WRITE_12:
3754283b4a9bSStephen M. Cameron 		is_write = 1;
3755283b4a9bSStephen M. Cameron 	case READ_6:
3756283b4a9bSStephen M. Cameron 	case READ_12:
3757283b4a9bSStephen M. Cameron 		if (*cdb_len == 6) {
3758283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 8) | cdb[3];
3759283b4a9bSStephen M. Cameron 			block_cnt = cdb[4];
3760283b4a9bSStephen M. Cameron 		} else {
3761283b4a9bSStephen M. Cameron 			BUG_ON(*cdb_len != 12);
3762283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 24) |
3763283b4a9bSStephen M. Cameron 				(((u32) cdb[3]) << 16) |
3764283b4a9bSStephen M. Cameron 				(((u32) cdb[4]) << 8) |
3765283b4a9bSStephen M. Cameron 				cdb[5];
3766283b4a9bSStephen M. Cameron 			block_cnt =
3767283b4a9bSStephen M. Cameron 				(((u32) cdb[6]) << 24) |
3768283b4a9bSStephen M. Cameron 				(((u32) cdb[7]) << 16) |
3769283b4a9bSStephen M. Cameron 				(((u32) cdb[8]) << 8) |
3770283b4a9bSStephen M. Cameron 				cdb[9];
3771283b4a9bSStephen M. Cameron 		}
3772283b4a9bSStephen M. Cameron 		if (block_cnt > 0xffff)
3773283b4a9bSStephen M. Cameron 			return IO_ACCEL_INELIGIBLE;
3774283b4a9bSStephen M. Cameron 
3775283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
3776283b4a9bSStephen M. Cameron 		cdb[1] = 0;
3777283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (block >> 24);
3778283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (block >> 16);
3779283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (block >> 8);
3780283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (block);
3781283b4a9bSStephen M. Cameron 		cdb[6] = 0;
3782283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (block_cnt >> 8);
3783283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (block_cnt);
3784283b4a9bSStephen M. Cameron 		cdb[9] = 0;
3785283b4a9bSStephen M. Cameron 		*cdb_len = 10;
3786283b4a9bSStephen M. Cameron 		break;
3787283b4a9bSStephen M. Cameron 	}
3788283b4a9bSStephen M. Cameron 	return 0;
3789283b4a9bSStephen M. Cameron }
3790283b4a9bSStephen M. Cameron 
3791c349775eSScott Teel static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
3792283b4a9bSStephen M. Cameron 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
379303383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
3794e1f7de0cSMatt Gates {
3795e1f7de0cSMatt Gates 	struct scsi_cmnd *cmd = c->scsi_cmd;
3796e1f7de0cSMatt Gates 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
3797e1f7de0cSMatt Gates 	unsigned int len;
3798e1f7de0cSMatt Gates 	unsigned int total_len = 0;
3799e1f7de0cSMatt Gates 	struct scatterlist *sg;
3800e1f7de0cSMatt Gates 	u64 addr64;
3801e1f7de0cSMatt Gates 	int use_sg, i;
3802e1f7de0cSMatt Gates 	struct SGDescriptor *curr_sg;
3803e1f7de0cSMatt Gates 	u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
3804e1f7de0cSMatt Gates 
3805283b4a9bSStephen M. Cameron 	/* TODO: implement chaining support */
380603383736SDon Brace 	if (scsi_sg_count(cmd) > h->ioaccel_maxsg) {
380703383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3808283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
380903383736SDon Brace 	}
3810283b4a9bSStephen M. Cameron 
3811e1f7de0cSMatt Gates 	BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
3812e1f7de0cSMatt Gates 
381303383736SDon Brace 	if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
381403383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3815283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
381603383736SDon Brace 	}
3817283b4a9bSStephen M. Cameron 
3818e1f7de0cSMatt Gates 	c->cmd_type = CMD_IOACCEL1;
3819e1f7de0cSMatt Gates 
3820e1f7de0cSMatt Gates 	/* Adjust the DMA address to point to the accelerated command buffer */
3821e1f7de0cSMatt Gates 	c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
3822e1f7de0cSMatt Gates 				(c->cmdindex * sizeof(*cp));
3823e1f7de0cSMatt Gates 	BUG_ON(c->busaddr & 0x0000007F);
3824e1f7de0cSMatt Gates 
3825e1f7de0cSMatt Gates 	use_sg = scsi_dma_map(cmd);
382603383736SDon Brace 	if (use_sg < 0) {
382703383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3828e1f7de0cSMatt Gates 		return use_sg;
382903383736SDon Brace 	}
3830e1f7de0cSMatt Gates 
3831e1f7de0cSMatt Gates 	if (use_sg) {
3832e1f7de0cSMatt Gates 		curr_sg = cp->SG;
3833e1f7de0cSMatt Gates 		scsi_for_each_sg(cmd, sg, use_sg, i) {
3834e1f7de0cSMatt Gates 			addr64 = (u64) sg_dma_address(sg);
3835e1f7de0cSMatt Gates 			len  = sg_dma_len(sg);
3836e1f7de0cSMatt Gates 			total_len += len;
383750a0decfSStephen M. Cameron 			curr_sg->Addr = cpu_to_le64(addr64);
383850a0decfSStephen M. Cameron 			curr_sg->Len = cpu_to_le32(len);
383950a0decfSStephen M. Cameron 			curr_sg->Ext = cpu_to_le32(0);
3840e1f7de0cSMatt Gates 			curr_sg++;
3841e1f7de0cSMatt Gates 		}
384250a0decfSStephen M. Cameron 		(--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
3843e1f7de0cSMatt Gates 
3844e1f7de0cSMatt Gates 		switch (cmd->sc_data_direction) {
3845e1f7de0cSMatt Gates 		case DMA_TO_DEVICE:
3846e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_OUT;
3847e1f7de0cSMatt Gates 			break;
3848e1f7de0cSMatt Gates 		case DMA_FROM_DEVICE:
3849e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_IN;
3850e1f7de0cSMatt Gates 			break;
3851e1f7de0cSMatt Gates 		case DMA_NONE:
3852e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_NODATAXFER;
3853e1f7de0cSMatt Gates 			break;
3854e1f7de0cSMatt Gates 		default:
3855e1f7de0cSMatt Gates 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3856e1f7de0cSMatt Gates 			cmd->sc_data_direction);
3857e1f7de0cSMatt Gates 			BUG();
3858e1f7de0cSMatt Gates 			break;
3859e1f7de0cSMatt Gates 		}
3860e1f7de0cSMatt Gates 	} else {
3861e1f7de0cSMatt Gates 		control |= IOACCEL1_CONTROL_NODATAXFER;
3862e1f7de0cSMatt Gates 	}
3863e1f7de0cSMatt Gates 
3864c349775eSScott Teel 	c->Header.SGList = use_sg;
3865e1f7de0cSMatt Gates 	/* Fill out the command structure to submit */
38662b08b3e9SDon Brace 	cp->dev_handle = cpu_to_le16(ioaccel_handle & 0xFFFF);
38672b08b3e9SDon Brace 	cp->transfer_len = cpu_to_le32(total_len);
38682b08b3e9SDon Brace 	cp->io_flags = cpu_to_le16(IOACCEL1_IOFLAGS_IO_REQ |
38692b08b3e9SDon Brace 			(cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK));
38702b08b3e9SDon Brace 	cp->control = cpu_to_le32(control);
3871283b4a9bSStephen M. Cameron 	memcpy(cp->CDB, cdb, cdb_len);
3872283b4a9bSStephen M. Cameron 	memcpy(cp->CISS_LUN, scsi3addr, 8);
3873c349775eSScott Teel 	/* Tag was already set at init time. */
3874e1f7de0cSMatt Gates 	enqueue_cmd_and_start_io(h, c);
3875e1f7de0cSMatt Gates 	return 0;
3876e1f7de0cSMatt Gates }
3877edd16368SStephen M. Cameron 
3878283b4a9bSStephen M. Cameron /*
3879283b4a9bSStephen M. Cameron  * Queue a command directly to a device behind the controller using the
3880283b4a9bSStephen M. Cameron  * I/O accelerator path.
3881283b4a9bSStephen M. Cameron  */
3882283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
3883283b4a9bSStephen M. Cameron 	struct CommandList *c)
3884283b4a9bSStephen M. Cameron {
3885283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
3886283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3887283b4a9bSStephen M. Cameron 
388803383736SDon Brace 	c->phys_disk = dev;
388903383736SDon Brace 
3890283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
389103383736SDon Brace 		cmd->cmnd, cmd->cmd_len, dev->scsi3addr, dev);
3892283b4a9bSStephen M. Cameron }
3893283b4a9bSStephen M. Cameron 
3894dd0e19f3SScott Teel /*
3895dd0e19f3SScott Teel  * Set encryption parameters for the ioaccel2 request
3896dd0e19f3SScott Teel  */
3897dd0e19f3SScott Teel static void set_encrypt_ioaccel2(struct ctlr_info *h,
3898dd0e19f3SScott Teel 	struct CommandList *c, struct io_accel2_cmd *cp)
3899dd0e19f3SScott Teel {
3900dd0e19f3SScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
3901dd0e19f3SScott Teel 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3902dd0e19f3SScott Teel 	struct raid_map_data *map = &dev->raid_map;
3903dd0e19f3SScott Teel 	u64 first_block;
3904dd0e19f3SScott Teel 
3905dd0e19f3SScott Teel 	/* Are we doing encryption on this device */
39062b08b3e9SDon Brace 	if (!(le16_to_cpu(map->flags) & RAID_MAP_FLAG_ENCRYPT_ON))
3907dd0e19f3SScott Teel 		return;
3908dd0e19f3SScott Teel 	/* Set the data encryption key index. */
3909dd0e19f3SScott Teel 	cp->dekindex = map->dekindex;
3910dd0e19f3SScott Teel 
3911dd0e19f3SScott Teel 	/* Set the encryption enable flag, encoded into direction field. */
3912dd0e19f3SScott Teel 	cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
3913dd0e19f3SScott Teel 
3914dd0e19f3SScott Teel 	/* Set encryption tweak values based on logical block address
3915dd0e19f3SScott Teel 	 * If block size is 512, tweak value is LBA.
3916dd0e19f3SScott Teel 	 * For other block sizes, tweak is (LBA * block size)/ 512)
3917dd0e19f3SScott Teel 	 */
3918dd0e19f3SScott Teel 	switch (cmd->cmnd[0]) {
3919dd0e19f3SScott Teel 	/* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
3920dd0e19f3SScott Teel 	case WRITE_6:
3921dd0e19f3SScott Teel 	case READ_6:
39222b08b3e9SDon Brace 		first_block = get_unaligned_be16(&cmd->cmnd[2]);
3923dd0e19f3SScott Teel 		break;
3924dd0e19f3SScott Teel 	case WRITE_10:
3925dd0e19f3SScott Teel 	case READ_10:
3926dd0e19f3SScott Teel 	/* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
3927dd0e19f3SScott Teel 	case WRITE_12:
3928dd0e19f3SScott Teel 	case READ_12:
39292b08b3e9SDon Brace 		first_block = get_unaligned_be32(&cmd->cmnd[2]);
3930dd0e19f3SScott Teel 		break;
3931dd0e19f3SScott Teel 	case WRITE_16:
3932dd0e19f3SScott Teel 	case READ_16:
39332b08b3e9SDon Brace 		first_block = get_unaligned_be64(&cmd->cmnd[2]);
3934dd0e19f3SScott Teel 		break;
3935dd0e19f3SScott Teel 	default:
3936dd0e19f3SScott Teel 		dev_err(&h->pdev->dev,
39372b08b3e9SDon Brace 			"ERROR: %s: size (0x%x) not supported for encryption\n",
39382b08b3e9SDon Brace 			__func__, cmd->cmnd[0]);
3939dd0e19f3SScott Teel 		BUG();
3940dd0e19f3SScott Teel 		break;
3941dd0e19f3SScott Teel 	}
39422b08b3e9SDon Brace 
39432b08b3e9SDon Brace 	if (le32_to_cpu(map->volume_blk_size) != 512)
39442b08b3e9SDon Brace 		first_block = first_block *
39452b08b3e9SDon Brace 				le32_to_cpu(map->volume_blk_size)/512;
39462b08b3e9SDon Brace 
39472b08b3e9SDon Brace 	cp->tweak_lower = cpu_to_le32(first_block);
39482b08b3e9SDon Brace 	cp->tweak_upper = cpu_to_le32(first_block >> 32);
3949dd0e19f3SScott Teel }
3950dd0e19f3SScott Teel 
3951c349775eSScott Teel static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
3952c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
395303383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
3954c349775eSScott Teel {
3955c349775eSScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
3956c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
3957c349775eSScott Teel 	struct ioaccel2_sg_element *curr_sg;
3958c349775eSScott Teel 	int use_sg, i;
3959c349775eSScott Teel 	struct scatterlist *sg;
3960c349775eSScott Teel 	u64 addr64;
3961c349775eSScott Teel 	u32 len;
3962c349775eSScott Teel 	u32 total_len = 0;
3963c349775eSScott Teel 
3964d9a729f3SWebb Scales 	BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
3965c349775eSScott Teel 
396603383736SDon Brace 	if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
396703383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3968c349775eSScott Teel 		return IO_ACCEL_INELIGIBLE;
396903383736SDon Brace 	}
397003383736SDon Brace 
3971c349775eSScott Teel 	c->cmd_type = CMD_IOACCEL2;
3972c349775eSScott Teel 	/* Adjust the DMA address to point to the accelerated command buffer */
3973c349775eSScott Teel 	c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
3974c349775eSScott Teel 				(c->cmdindex * sizeof(*cp));
3975c349775eSScott Teel 	BUG_ON(c->busaddr & 0x0000007F);
3976c349775eSScott Teel 
3977c349775eSScott Teel 	memset(cp, 0, sizeof(*cp));
3978c349775eSScott Teel 	cp->IU_type = IOACCEL2_IU_TYPE;
3979c349775eSScott Teel 
3980c349775eSScott Teel 	use_sg = scsi_dma_map(cmd);
398103383736SDon Brace 	if (use_sg < 0) {
398203383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3983c349775eSScott Teel 		return use_sg;
398403383736SDon Brace 	}
3985c349775eSScott Teel 
3986c349775eSScott Teel 	if (use_sg) {
3987c349775eSScott Teel 		curr_sg = cp->sg;
3988d9a729f3SWebb Scales 		if (use_sg > h->ioaccel_maxsg) {
3989d9a729f3SWebb Scales 			addr64 = le64_to_cpu(
3990d9a729f3SWebb Scales 				h->ioaccel2_cmd_sg_list[c->cmdindex]->address);
3991d9a729f3SWebb Scales 			curr_sg->address = cpu_to_le64(addr64);
3992d9a729f3SWebb Scales 			curr_sg->length = 0;
3993d9a729f3SWebb Scales 			curr_sg->reserved[0] = 0;
3994d9a729f3SWebb Scales 			curr_sg->reserved[1] = 0;
3995d9a729f3SWebb Scales 			curr_sg->reserved[2] = 0;
3996d9a729f3SWebb Scales 			curr_sg->chain_indicator = 0x80;
3997d9a729f3SWebb Scales 
3998d9a729f3SWebb Scales 			curr_sg = h->ioaccel2_cmd_sg_list[c->cmdindex];
3999d9a729f3SWebb Scales 		}
4000c349775eSScott Teel 		scsi_for_each_sg(cmd, sg, use_sg, i) {
4001c349775eSScott Teel 			addr64 = (u64) sg_dma_address(sg);
4002c349775eSScott Teel 			len  = sg_dma_len(sg);
4003c349775eSScott Teel 			total_len += len;
4004c349775eSScott Teel 			curr_sg->address = cpu_to_le64(addr64);
4005c349775eSScott Teel 			curr_sg->length = cpu_to_le32(len);
4006c349775eSScott Teel 			curr_sg->reserved[0] = 0;
4007c349775eSScott Teel 			curr_sg->reserved[1] = 0;
4008c349775eSScott Teel 			curr_sg->reserved[2] = 0;
4009c349775eSScott Teel 			curr_sg->chain_indicator = 0;
4010c349775eSScott Teel 			curr_sg++;
4011c349775eSScott Teel 		}
4012c349775eSScott Teel 
4013c349775eSScott Teel 		switch (cmd->sc_data_direction) {
4014c349775eSScott Teel 		case DMA_TO_DEVICE:
4015dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4016dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_OUT;
4017c349775eSScott Teel 			break;
4018c349775eSScott Teel 		case DMA_FROM_DEVICE:
4019dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4020dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_IN;
4021c349775eSScott Teel 			break;
4022c349775eSScott Teel 		case DMA_NONE:
4023dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4024dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_NO_DATA;
4025c349775eSScott Teel 			break;
4026c349775eSScott Teel 		default:
4027c349775eSScott Teel 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4028c349775eSScott Teel 				cmd->sc_data_direction);
4029c349775eSScott Teel 			BUG();
4030c349775eSScott Teel 			break;
4031c349775eSScott Teel 		}
4032c349775eSScott Teel 	} else {
4033dd0e19f3SScott Teel 		cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4034dd0e19f3SScott Teel 		cp->direction |= IOACCEL2_DIR_NO_DATA;
4035c349775eSScott Teel 	}
4036dd0e19f3SScott Teel 
4037dd0e19f3SScott Teel 	/* Set encryption parameters, if necessary */
4038dd0e19f3SScott Teel 	set_encrypt_ioaccel2(h, c, cp);
4039dd0e19f3SScott Teel 
40402b08b3e9SDon Brace 	cp->scsi_nexus = cpu_to_le32(ioaccel_handle);
4041f2405db8SDon Brace 	cp->Tag = cpu_to_le32(c->cmdindex << DIRECT_LOOKUP_SHIFT);
4042c349775eSScott Teel 	memcpy(cp->cdb, cdb, sizeof(cp->cdb));
4043c349775eSScott Teel 
4044c349775eSScott Teel 	cp->data_len = cpu_to_le32(total_len);
4045c349775eSScott Teel 	cp->err_ptr = cpu_to_le64(c->busaddr +
4046c349775eSScott Teel 			offsetof(struct io_accel2_cmd, error_data));
404750a0decfSStephen M. Cameron 	cp->err_len = cpu_to_le32(sizeof(cp->error_data));
4048c349775eSScott Teel 
4049d9a729f3SWebb Scales 	/* fill in sg elements */
4050d9a729f3SWebb Scales 	if (use_sg > h->ioaccel_maxsg) {
4051d9a729f3SWebb Scales 		cp->sg_count = 1;
4052d9a729f3SWebb Scales 		if (hpsa_map_ioaccel2_sg_chain_block(h, cp, c)) {
4053d9a729f3SWebb Scales 			atomic_dec(&phys_disk->ioaccel_cmds_out);
4054d9a729f3SWebb Scales 			scsi_dma_unmap(cmd);
4055d9a729f3SWebb Scales 			return -1;
4056d9a729f3SWebb Scales 		}
4057d9a729f3SWebb Scales 	} else
4058d9a729f3SWebb Scales 		cp->sg_count = (u8) use_sg;
4059d9a729f3SWebb Scales 
4060c349775eSScott Teel 	enqueue_cmd_and_start_io(h, c);
4061c349775eSScott Teel 	return 0;
4062c349775eSScott Teel }
4063c349775eSScott Teel 
4064c349775eSScott Teel /*
4065c349775eSScott Teel  * Queue a command to the correct I/O accelerator path.
4066c349775eSScott Teel  */
4067c349775eSScott Teel static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
4068c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
406903383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
4070c349775eSScott Teel {
407103383736SDon Brace 	/* Try to honor the device's queue depth */
407203383736SDon Brace 	if (atomic_inc_return(&phys_disk->ioaccel_cmds_out) >
407303383736SDon Brace 					phys_disk->queue_depth) {
407403383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
407503383736SDon Brace 		return IO_ACCEL_INELIGIBLE;
407603383736SDon Brace 	}
4077c349775eSScott Teel 	if (h->transMethod & CFGTBL_Trans_io_accel1)
4078c349775eSScott Teel 		return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
407903383736SDon Brace 						cdb, cdb_len, scsi3addr,
408003383736SDon Brace 						phys_disk);
4081c349775eSScott Teel 	else
4082c349775eSScott Teel 		return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
408303383736SDon Brace 						cdb, cdb_len, scsi3addr,
408403383736SDon Brace 						phys_disk);
4085c349775eSScott Teel }
4086c349775eSScott Teel 
40876b80b18fSScott Teel static void raid_map_helper(struct raid_map_data *map,
40886b80b18fSScott Teel 		int offload_to_mirror, u32 *map_index, u32 *current_group)
40896b80b18fSScott Teel {
40906b80b18fSScott Teel 	if (offload_to_mirror == 0)  {
40916b80b18fSScott Teel 		/* use physical disk in the first mirrored group. */
40922b08b3e9SDon Brace 		*map_index %= le16_to_cpu(map->data_disks_per_row);
40936b80b18fSScott Teel 		return;
40946b80b18fSScott Teel 	}
40956b80b18fSScott Teel 	do {
40966b80b18fSScott Teel 		/* determine mirror group that *map_index indicates */
40972b08b3e9SDon Brace 		*current_group = *map_index /
40982b08b3e9SDon Brace 			le16_to_cpu(map->data_disks_per_row);
40996b80b18fSScott Teel 		if (offload_to_mirror == *current_group)
41006b80b18fSScott Teel 			continue;
41012b08b3e9SDon Brace 		if (*current_group < le16_to_cpu(map->layout_map_count) - 1) {
41026b80b18fSScott Teel 			/* select map index from next group */
41032b08b3e9SDon Brace 			*map_index += le16_to_cpu(map->data_disks_per_row);
41046b80b18fSScott Teel 			(*current_group)++;
41056b80b18fSScott Teel 		} else {
41066b80b18fSScott Teel 			/* select map index from first group */
41072b08b3e9SDon Brace 			*map_index %= le16_to_cpu(map->data_disks_per_row);
41086b80b18fSScott Teel 			*current_group = 0;
41096b80b18fSScott Teel 		}
41106b80b18fSScott Teel 	} while (offload_to_mirror != *current_group);
41116b80b18fSScott Teel }
41126b80b18fSScott Teel 
4113283b4a9bSStephen M. Cameron /*
4114283b4a9bSStephen M. Cameron  * Attempt to perform offload RAID mapping for a logical volume I/O.
4115283b4a9bSStephen M. Cameron  */
4116283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
4117283b4a9bSStephen M. Cameron 	struct CommandList *c)
4118283b4a9bSStephen M. Cameron {
4119283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
4120283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4121283b4a9bSStephen M. Cameron 	struct raid_map_data *map = &dev->raid_map;
4122283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map->data[0];
4123283b4a9bSStephen M. Cameron 	int is_write = 0;
4124283b4a9bSStephen M. Cameron 	u32 map_index;
4125283b4a9bSStephen M. Cameron 	u64 first_block, last_block;
4126283b4a9bSStephen M. Cameron 	u32 block_cnt;
4127283b4a9bSStephen M. Cameron 	u32 blocks_per_row;
4128283b4a9bSStephen M. Cameron 	u64 first_row, last_row;
4129283b4a9bSStephen M. Cameron 	u32 first_row_offset, last_row_offset;
4130283b4a9bSStephen M. Cameron 	u32 first_column, last_column;
41316b80b18fSScott Teel 	u64 r0_first_row, r0_last_row;
41326b80b18fSScott Teel 	u32 r5or6_blocks_per_row;
41336b80b18fSScott Teel 	u64 r5or6_first_row, r5or6_last_row;
41346b80b18fSScott Teel 	u32 r5or6_first_row_offset, r5or6_last_row_offset;
41356b80b18fSScott Teel 	u32 r5or6_first_column, r5or6_last_column;
41366b80b18fSScott Teel 	u32 total_disks_per_row;
41376b80b18fSScott Teel 	u32 stripesize;
41386b80b18fSScott Teel 	u32 first_group, last_group, current_group;
4139283b4a9bSStephen M. Cameron 	u32 map_row;
4140283b4a9bSStephen M. Cameron 	u32 disk_handle;
4141283b4a9bSStephen M. Cameron 	u64 disk_block;
4142283b4a9bSStephen M. Cameron 	u32 disk_block_cnt;
4143283b4a9bSStephen M. Cameron 	u8 cdb[16];
4144283b4a9bSStephen M. Cameron 	u8 cdb_len;
41452b08b3e9SDon Brace 	u16 strip_size;
4146283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
4147283b4a9bSStephen M. Cameron 	u64 tmpdiv;
4148283b4a9bSStephen M. Cameron #endif
41496b80b18fSScott Teel 	int offload_to_mirror;
4150283b4a9bSStephen M. Cameron 
4151283b4a9bSStephen M. Cameron 	/* check for valid opcode, get LBA and block count */
4152283b4a9bSStephen M. Cameron 	switch (cmd->cmnd[0]) {
4153283b4a9bSStephen M. Cameron 	case WRITE_6:
4154283b4a9bSStephen M. Cameron 		is_write = 1;
4155283b4a9bSStephen M. Cameron 	case READ_6:
4156283b4a9bSStephen M. Cameron 		first_block =
4157283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 8) |
4158283b4a9bSStephen M. Cameron 			cmd->cmnd[3];
4159283b4a9bSStephen M. Cameron 		block_cnt = cmd->cmnd[4];
41603fa89a04SStephen M. Cameron 		if (block_cnt == 0)
41613fa89a04SStephen M. Cameron 			block_cnt = 256;
4162283b4a9bSStephen M. Cameron 		break;
4163283b4a9bSStephen M. Cameron 	case WRITE_10:
4164283b4a9bSStephen M. Cameron 		is_write = 1;
4165283b4a9bSStephen M. Cameron 	case READ_10:
4166283b4a9bSStephen M. Cameron 		first_block =
4167283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
4168283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
4169283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
4170283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
4171283b4a9bSStephen M. Cameron 		block_cnt =
4172283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 8) |
4173283b4a9bSStephen M. Cameron 			cmd->cmnd[8];
4174283b4a9bSStephen M. Cameron 		break;
4175283b4a9bSStephen M. Cameron 	case WRITE_12:
4176283b4a9bSStephen M. Cameron 		is_write = 1;
4177283b4a9bSStephen M. Cameron 	case READ_12:
4178283b4a9bSStephen M. Cameron 		first_block =
4179283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
4180283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
4181283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
4182283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
4183283b4a9bSStephen M. Cameron 		block_cnt =
4184283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[6]) << 24) |
4185283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 16) |
4186283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[8]) << 8) |
4187283b4a9bSStephen M. Cameron 		cmd->cmnd[9];
4188283b4a9bSStephen M. Cameron 		break;
4189283b4a9bSStephen M. Cameron 	case WRITE_16:
4190283b4a9bSStephen M. Cameron 		is_write = 1;
4191283b4a9bSStephen M. Cameron 	case READ_16:
4192283b4a9bSStephen M. Cameron 		first_block =
4193283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 56) |
4194283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 48) |
4195283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 40) |
4196283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[5]) << 32) |
4197283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[6]) << 24) |
4198283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[7]) << 16) |
4199283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[8]) << 8) |
4200283b4a9bSStephen M. Cameron 			cmd->cmnd[9];
4201283b4a9bSStephen M. Cameron 		block_cnt =
4202283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[10]) << 24) |
4203283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[11]) << 16) |
4204283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[12]) << 8) |
4205283b4a9bSStephen M. Cameron 			cmd->cmnd[13];
4206283b4a9bSStephen M. Cameron 		break;
4207283b4a9bSStephen M. Cameron 	default:
4208283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
4209283b4a9bSStephen M. Cameron 	}
4210283b4a9bSStephen M. Cameron 	last_block = first_block + block_cnt - 1;
4211283b4a9bSStephen M. Cameron 
4212283b4a9bSStephen M. Cameron 	/* check for write to non-RAID-0 */
4213283b4a9bSStephen M. Cameron 	if (is_write && dev->raid_level != 0)
4214283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
4215283b4a9bSStephen M. Cameron 
4216283b4a9bSStephen M. Cameron 	/* check for invalid block or wraparound */
42172b08b3e9SDon Brace 	if (last_block >= le64_to_cpu(map->volume_blk_cnt) ||
42182b08b3e9SDon Brace 		last_block < first_block)
4219283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
4220283b4a9bSStephen M. Cameron 
4221283b4a9bSStephen M. Cameron 	/* calculate stripe information for the request */
42222b08b3e9SDon Brace 	blocks_per_row = le16_to_cpu(map->data_disks_per_row) *
42232b08b3e9SDon Brace 				le16_to_cpu(map->strip_size);
42242b08b3e9SDon Brace 	strip_size = le16_to_cpu(map->strip_size);
4225283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
4226283b4a9bSStephen M. Cameron 	tmpdiv = first_block;
4227283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
4228283b4a9bSStephen M. Cameron 	first_row = tmpdiv;
4229283b4a9bSStephen M. Cameron 	tmpdiv = last_block;
4230283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
4231283b4a9bSStephen M. Cameron 	last_row = tmpdiv;
4232283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
4233283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
4234283b4a9bSStephen M. Cameron 	tmpdiv = first_row_offset;
42352b08b3e9SDon Brace 	(void) do_div(tmpdiv, strip_size);
4236283b4a9bSStephen M. Cameron 	first_column = tmpdiv;
4237283b4a9bSStephen M. Cameron 	tmpdiv = last_row_offset;
42382b08b3e9SDon Brace 	(void) do_div(tmpdiv, strip_size);
4239283b4a9bSStephen M. Cameron 	last_column = tmpdiv;
4240283b4a9bSStephen M. Cameron #else
4241283b4a9bSStephen M. Cameron 	first_row = first_block / blocks_per_row;
4242283b4a9bSStephen M. Cameron 	last_row = last_block / blocks_per_row;
4243283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
4244283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
42452b08b3e9SDon Brace 	first_column = first_row_offset / strip_size;
42462b08b3e9SDon Brace 	last_column = last_row_offset / strip_size;
4247283b4a9bSStephen M. Cameron #endif
4248283b4a9bSStephen M. Cameron 
4249283b4a9bSStephen M. Cameron 	/* if this isn't a single row/column then give to the controller */
4250283b4a9bSStephen M. Cameron 	if ((first_row != last_row) || (first_column != last_column))
4251283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
4252283b4a9bSStephen M. Cameron 
4253283b4a9bSStephen M. Cameron 	/* proceeding with driver mapping */
42542b08b3e9SDon Brace 	total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
42552b08b3e9SDon Brace 				le16_to_cpu(map->metadata_disks_per_row);
4256283b4a9bSStephen M. Cameron 	map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
42572b08b3e9SDon Brace 				le16_to_cpu(map->row_cnt);
42586b80b18fSScott Teel 	map_index = (map_row * total_disks_per_row) + first_column;
42596b80b18fSScott Teel 
42606b80b18fSScott Teel 	switch (dev->raid_level) {
42616b80b18fSScott Teel 	case HPSA_RAID_0:
42626b80b18fSScott Teel 		break; /* nothing special to do */
42636b80b18fSScott Teel 	case HPSA_RAID_1:
42646b80b18fSScott Teel 		/* Handles load balance across RAID 1 members.
42656b80b18fSScott Teel 		 * (2-drive R1 and R10 with even # of drives.)
42666b80b18fSScott Teel 		 * Appropriate for SSDs, not optimal for HDDs
4267283b4a9bSStephen M. Cameron 		 */
42682b08b3e9SDon Brace 		BUG_ON(le16_to_cpu(map->layout_map_count) != 2);
4269283b4a9bSStephen M. Cameron 		if (dev->offload_to_mirror)
42702b08b3e9SDon Brace 			map_index += le16_to_cpu(map->data_disks_per_row);
4271283b4a9bSStephen M. Cameron 		dev->offload_to_mirror = !dev->offload_to_mirror;
42726b80b18fSScott Teel 		break;
42736b80b18fSScott Teel 	case HPSA_RAID_ADM:
42746b80b18fSScott Teel 		/* Handles N-way mirrors  (R1-ADM)
42756b80b18fSScott Teel 		 * and R10 with # of drives divisible by 3.)
42766b80b18fSScott Teel 		 */
42772b08b3e9SDon Brace 		BUG_ON(le16_to_cpu(map->layout_map_count) != 3);
42786b80b18fSScott Teel 
42796b80b18fSScott Teel 		offload_to_mirror = dev->offload_to_mirror;
42806b80b18fSScott Teel 		raid_map_helper(map, offload_to_mirror,
42816b80b18fSScott Teel 				&map_index, &current_group);
42826b80b18fSScott Teel 		/* set mirror group to use next time */
42836b80b18fSScott Teel 		offload_to_mirror =
42842b08b3e9SDon Brace 			(offload_to_mirror >=
42852b08b3e9SDon Brace 			le16_to_cpu(map->layout_map_count) - 1)
42866b80b18fSScott Teel 			? 0 : offload_to_mirror + 1;
42876b80b18fSScott Teel 		dev->offload_to_mirror = offload_to_mirror;
42886b80b18fSScott Teel 		/* Avoid direct use of dev->offload_to_mirror within this
42896b80b18fSScott Teel 		 * function since multiple threads might simultaneously
42906b80b18fSScott Teel 		 * increment it beyond the range of dev->layout_map_count -1.
42916b80b18fSScott Teel 		 */
42926b80b18fSScott Teel 		break;
42936b80b18fSScott Teel 	case HPSA_RAID_5:
42946b80b18fSScott Teel 	case HPSA_RAID_6:
42952b08b3e9SDon Brace 		if (le16_to_cpu(map->layout_map_count) <= 1)
42966b80b18fSScott Teel 			break;
42976b80b18fSScott Teel 
42986b80b18fSScott Teel 		/* Verify first and last block are in same RAID group */
42996b80b18fSScott Teel 		r5or6_blocks_per_row =
43002b08b3e9SDon Brace 			le16_to_cpu(map->strip_size) *
43012b08b3e9SDon Brace 			le16_to_cpu(map->data_disks_per_row);
43026b80b18fSScott Teel 		BUG_ON(r5or6_blocks_per_row == 0);
43032b08b3e9SDon Brace 		stripesize = r5or6_blocks_per_row *
43042b08b3e9SDon Brace 			le16_to_cpu(map->layout_map_count);
43056b80b18fSScott Teel #if BITS_PER_LONG == 32
43066b80b18fSScott Teel 		tmpdiv = first_block;
43076b80b18fSScott Teel 		first_group = do_div(tmpdiv, stripesize);
43086b80b18fSScott Teel 		tmpdiv = first_group;
43096b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
43106b80b18fSScott Teel 		first_group = tmpdiv;
43116b80b18fSScott Teel 		tmpdiv = last_block;
43126b80b18fSScott Teel 		last_group = do_div(tmpdiv, stripesize);
43136b80b18fSScott Teel 		tmpdiv = last_group;
43146b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
43156b80b18fSScott Teel 		last_group = tmpdiv;
43166b80b18fSScott Teel #else
43176b80b18fSScott Teel 		first_group = (first_block % stripesize) / r5or6_blocks_per_row;
43186b80b18fSScott Teel 		last_group = (last_block % stripesize) / r5or6_blocks_per_row;
43196b80b18fSScott Teel #endif
4320000ff7c2SStephen M. Cameron 		if (first_group != last_group)
43216b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
43226b80b18fSScott Teel 
43236b80b18fSScott Teel 		/* Verify request is in a single row of RAID 5/6 */
43246b80b18fSScott Teel #if BITS_PER_LONG == 32
43256b80b18fSScott Teel 		tmpdiv = first_block;
43266b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
43276b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row = tmpdiv;
43286b80b18fSScott Teel 		tmpdiv = last_block;
43296b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
43306b80b18fSScott Teel 		r5or6_last_row = r0_last_row = tmpdiv;
43316b80b18fSScott Teel #else
43326b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row =
43336b80b18fSScott Teel 						first_block / stripesize;
43346b80b18fSScott Teel 		r5or6_last_row = r0_last_row = last_block / stripesize;
43356b80b18fSScott Teel #endif
43366b80b18fSScott Teel 		if (r5or6_first_row != r5or6_last_row)
43376b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
43386b80b18fSScott Teel 
43396b80b18fSScott Teel 
43406b80b18fSScott Teel 		/* Verify request is in a single column */
43416b80b18fSScott Teel #if BITS_PER_LONG == 32
43426b80b18fSScott Teel 		tmpdiv = first_block;
43436b80b18fSScott Teel 		first_row_offset = do_div(tmpdiv, stripesize);
43446b80b18fSScott Teel 		tmpdiv = first_row_offset;
43456b80b18fSScott Teel 		first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
43466b80b18fSScott Teel 		r5or6_first_row_offset = first_row_offset;
43476b80b18fSScott Teel 		tmpdiv = last_block;
43486b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, stripesize);
43496b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
43506b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
43516b80b18fSScott Teel 		tmpdiv = r5or6_first_row_offset;
43526b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
43536b80b18fSScott Teel 		first_column = r5or6_first_column = tmpdiv;
43546b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
43556b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
43566b80b18fSScott Teel 		r5or6_last_column = tmpdiv;
43576b80b18fSScott Teel #else
43586b80b18fSScott Teel 		first_row_offset = r5or6_first_row_offset =
43596b80b18fSScott Teel 			(u32)((first_block % stripesize) %
43606b80b18fSScott Teel 						r5or6_blocks_per_row);
43616b80b18fSScott Teel 
43626b80b18fSScott Teel 		r5or6_last_row_offset =
43636b80b18fSScott Teel 			(u32)((last_block % stripesize) %
43646b80b18fSScott Teel 						r5or6_blocks_per_row);
43656b80b18fSScott Teel 
43666b80b18fSScott Teel 		first_column = r5or6_first_column =
43672b08b3e9SDon Brace 			r5or6_first_row_offset / le16_to_cpu(map->strip_size);
43686b80b18fSScott Teel 		r5or6_last_column =
43692b08b3e9SDon Brace 			r5or6_last_row_offset / le16_to_cpu(map->strip_size);
43706b80b18fSScott Teel #endif
43716b80b18fSScott Teel 		if (r5or6_first_column != r5or6_last_column)
43726b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
43736b80b18fSScott Teel 
43746b80b18fSScott Teel 		/* Request is eligible */
43756b80b18fSScott Teel 		map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
43762b08b3e9SDon Brace 			le16_to_cpu(map->row_cnt);
43776b80b18fSScott Teel 
43786b80b18fSScott Teel 		map_index = (first_group *
43792b08b3e9SDon Brace 			(le16_to_cpu(map->row_cnt) * total_disks_per_row)) +
43806b80b18fSScott Teel 			(map_row * total_disks_per_row) + first_column;
43816b80b18fSScott Teel 		break;
43826b80b18fSScott Teel 	default:
43836b80b18fSScott Teel 		return IO_ACCEL_INELIGIBLE;
4384283b4a9bSStephen M. Cameron 	}
43856b80b18fSScott Teel 
438607543e0cSStephen Cameron 	if (unlikely(map_index >= RAID_MAP_MAX_ENTRIES))
438707543e0cSStephen Cameron 		return IO_ACCEL_INELIGIBLE;
438807543e0cSStephen Cameron 
438903383736SDon Brace 	c->phys_disk = dev->phys_disk[map_index];
439003383736SDon Brace 
4391283b4a9bSStephen M. Cameron 	disk_handle = dd[map_index].ioaccel_handle;
43922b08b3e9SDon Brace 	disk_block = le64_to_cpu(map->disk_starting_blk) +
43932b08b3e9SDon Brace 			first_row * le16_to_cpu(map->strip_size) +
43942b08b3e9SDon Brace 			(first_row_offset - first_column *
43952b08b3e9SDon Brace 			le16_to_cpu(map->strip_size));
4396283b4a9bSStephen M. Cameron 	disk_block_cnt = block_cnt;
4397283b4a9bSStephen M. Cameron 
4398283b4a9bSStephen M. Cameron 	/* handle differing logical/physical block sizes */
4399283b4a9bSStephen M. Cameron 	if (map->phys_blk_shift) {
4400283b4a9bSStephen M. Cameron 		disk_block <<= map->phys_blk_shift;
4401283b4a9bSStephen M. Cameron 		disk_block_cnt <<= map->phys_blk_shift;
4402283b4a9bSStephen M. Cameron 	}
4403283b4a9bSStephen M. Cameron 	BUG_ON(disk_block_cnt > 0xffff);
4404283b4a9bSStephen M. Cameron 
4405283b4a9bSStephen M. Cameron 	/* build the new CDB for the physical disk I/O */
4406283b4a9bSStephen M. Cameron 	if (disk_block > 0xffffffff) {
4407283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_16 : READ_16;
4408283b4a9bSStephen M. Cameron 		cdb[1] = 0;
4409283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 56);
4410283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 48);
4411283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 40);
4412283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block >> 32);
4413283b4a9bSStephen M. Cameron 		cdb[6] = (u8) (disk_block >> 24);
4414283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block >> 16);
4415283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block >> 8);
4416283b4a9bSStephen M. Cameron 		cdb[9] = (u8) (disk_block);
4417283b4a9bSStephen M. Cameron 		cdb[10] = (u8) (disk_block_cnt >> 24);
4418283b4a9bSStephen M. Cameron 		cdb[11] = (u8) (disk_block_cnt >> 16);
4419283b4a9bSStephen M. Cameron 		cdb[12] = (u8) (disk_block_cnt >> 8);
4420283b4a9bSStephen M. Cameron 		cdb[13] = (u8) (disk_block_cnt);
4421283b4a9bSStephen M. Cameron 		cdb[14] = 0;
4422283b4a9bSStephen M. Cameron 		cdb[15] = 0;
4423283b4a9bSStephen M. Cameron 		cdb_len = 16;
4424283b4a9bSStephen M. Cameron 	} else {
4425283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
4426283b4a9bSStephen M. Cameron 		cdb[1] = 0;
4427283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 24);
4428283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 16);
4429283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 8);
4430283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block);
4431283b4a9bSStephen M. Cameron 		cdb[6] = 0;
4432283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block_cnt >> 8);
4433283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block_cnt);
4434283b4a9bSStephen M. Cameron 		cdb[9] = 0;
4435283b4a9bSStephen M. Cameron 		cdb_len = 10;
4436283b4a9bSStephen M. Cameron 	}
4437283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
443803383736SDon Brace 						dev->scsi3addr,
443903383736SDon Brace 						dev->phys_disk[map_index]);
4440283b4a9bSStephen M. Cameron }
4441283b4a9bSStephen M. Cameron 
444225163bd5SWebb Scales /*
444325163bd5SWebb Scales  * Submit commands down the "normal" RAID stack path
444425163bd5SWebb Scales  * All callers to hpsa_ciss_submit must check lockup_detected
444525163bd5SWebb Scales  * beforehand, before (opt.) and after calling cmd_alloc
444625163bd5SWebb Scales  */
4447574f05d3SStephen Cameron static int hpsa_ciss_submit(struct ctlr_info *h,
4448574f05d3SStephen Cameron 	struct CommandList *c, struct scsi_cmnd *cmd,
4449574f05d3SStephen Cameron 	unsigned char scsi3addr[])
4450edd16368SStephen M. Cameron {
4451edd16368SStephen M. Cameron 	cmd->host_scribble = (unsigned char *) c;
4452edd16368SStephen M. Cameron 	c->cmd_type = CMD_SCSI;
4453edd16368SStephen M. Cameron 	c->scsi_cmd = cmd;
4454edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;  /* unused in simple mode */
4455edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
4456f2405db8SDon Brace 	c->Header.tag = cpu_to_le64((c->cmdindex << DIRECT_LOOKUP_SHIFT));
4457edd16368SStephen M. Cameron 
4458edd16368SStephen M. Cameron 	/* Fill in the request block... */
4459edd16368SStephen M. Cameron 
4460edd16368SStephen M. Cameron 	c->Request.Timeout = 0;
4461edd16368SStephen M. Cameron 	BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
4462edd16368SStephen M. Cameron 	c->Request.CDBLen = cmd->cmd_len;
4463edd16368SStephen M. Cameron 	memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
4464edd16368SStephen M. Cameron 	switch (cmd->sc_data_direction) {
4465edd16368SStephen M. Cameron 	case DMA_TO_DEVICE:
4466a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4467a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_WRITE);
4468edd16368SStephen M. Cameron 		break;
4469edd16368SStephen M. Cameron 	case DMA_FROM_DEVICE:
4470a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4471a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_READ);
4472edd16368SStephen M. Cameron 		break;
4473edd16368SStephen M. Cameron 	case DMA_NONE:
4474a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4475a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_NONE);
4476edd16368SStephen M. Cameron 		break;
4477edd16368SStephen M. Cameron 	case DMA_BIDIRECTIONAL:
4478edd16368SStephen M. Cameron 		/* This can happen if a buggy application does a scsi passthru
4479edd16368SStephen M. Cameron 		 * and sets both inlen and outlen to non-zero. ( see
4480edd16368SStephen M. Cameron 		 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
4481edd16368SStephen M. Cameron 		 */
4482edd16368SStephen M. Cameron 
4483a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4484a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_RSVD);
4485edd16368SStephen M. Cameron 		/* This is technically wrong, and hpsa controllers should
4486edd16368SStephen M. Cameron 		 * reject it with CMD_INVALID, which is the most correct
4487edd16368SStephen M. Cameron 		 * response, but non-fibre backends appear to let it
4488edd16368SStephen M. Cameron 		 * slide by, and give the same results as if this field
4489edd16368SStephen M. Cameron 		 * were set correctly.  Either way is acceptable for
4490edd16368SStephen M. Cameron 		 * our purposes here.
4491edd16368SStephen M. Cameron 		 */
4492edd16368SStephen M. Cameron 
4493edd16368SStephen M. Cameron 		break;
4494edd16368SStephen M. Cameron 
4495edd16368SStephen M. Cameron 	default:
4496edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4497edd16368SStephen M. Cameron 			cmd->sc_data_direction);
4498edd16368SStephen M. Cameron 		BUG();
4499edd16368SStephen M. Cameron 		break;
4500edd16368SStephen M. Cameron 	}
4501edd16368SStephen M. Cameron 
450233a2ffceSStephen M. Cameron 	if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
4503edd16368SStephen M. Cameron 		cmd_free(h, c);
4504edd16368SStephen M. Cameron 		return SCSI_MLQUEUE_HOST_BUSY;
4505edd16368SStephen M. Cameron 	}
4506edd16368SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
4507edd16368SStephen M. Cameron 	/* the cmd'll come back via intr handler in complete_scsi_command()  */
4508edd16368SStephen M. Cameron 	return 0;
4509edd16368SStephen M. Cameron }
4510edd16368SStephen M. Cameron 
4511360c73bdSStephen Cameron static void hpsa_cmd_init(struct ctlr_info *h, int index,
4512360c73bdSStephen Cameron 				struct CommandList *c)
4513360c73bdSStephen Cameron {
4514360c73bdSStephen Cameron 	dma_addr_t cmd_dma_handle, err_dma_handle;
4515360c73bdSStephen Cameron 
4516360c73bdSStephen Cameron 	/* Zero out all of commandlist except the last field, refcount */
4517360c73bdSStephen Cameron 	memset(c, 0, offsetof(struct CommandList, refcount));
4518360c73bdSStephen Cameron 	c->Header.tag = cpu_to_le64((u64) (index << DIRECT_LOOKUP_SHIFT));
4519360c73bdSStephen Cameron 	cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
4520360c73bdSStephen Cameron 	c->err_info = h->errinfo_pool + index;
4521360c73bdSStephen Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
4522360c73bdSStephen Cameron 	err_dma_handle = h->errinfo_pool_dhandle
4523360c73bdSStephen Cameron 	    + index * sizeof(*c->err_info);
4524360c73bdSStephen Cameron 	c->cmdindex = index;
4525360c73bdSStephen Cameron 	c->busaddr = (u32) cmd_dma_handle;
4526360c73bdSStephen Cameron 	c->ErrDesc.Addr = cpu_to_le64((u64) err_dma_handle);
4527360c73bdSStephen Cameron 	c->ErrDesc.Len = cpu_to_le32((u32) sizeof(*c->err_info));
4528360c73bdSStephen Cameron 	c->h = h;
4529a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_IDLE;
4530360c73bdSStephen Cameron }
4531360c73bdSStephen Cameron 
4532360c73bdSStephen Cameron static void hpsa_preinitialize_commands(struct ctlr_info *h)
4533360c73bdSStephen Cameron {
4534360c73bdSStephen Cameron 	int i;
4535360c73bdSStephen Cameron 
4536360c73bdSStephen Cameron 	for (i = 0; i < h->nr_cmds; i++) {
4537360c73bdSStephen Cameron 		struct CommandList *c = h->cmd_pool + i;
4538360c73bdSStephen Cameron 
4539360c73bdSStephen Cameron 		hpsa_cmd_init(h, i, c);
4540360c73bdSStephen Cameron 		atomic_set(&c->refcount, 0);
4541360c73bdSStephen Cameron 	}
4542360c73bdSStephen Cameron }
4543360c73bdSStephen Cameron 
4544360c73bdSStephen Cameron static inline void hpsa_cmd_partial_init(struct ctlr_info *h, int index,
4545360c73bdSStephen Cameron 				struct CommandList *c)
4546360c73bdSStephen Cameron {
4547360c73bdSStephen Cameron 	dma_addr_t cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
4548360c73bdSStephen Cameron 
4549360c73bdSStephen Cameron 	memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
4550360c73bdSStephen Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
4551360c73bdSStephen Cameron 	c->busaddr = (u32) cmd_dma_handle;
4552360c73bdSStephen Cameron }
4553360c73bdSStephen Cameron 
4554592a0ad5SWebb Scales static int hpsa_ioaccel_submit(struct ctlr_info *h,
4555592a0ad5SWebb Scales 		struct CommandList *c, struct scsi_cmnd *cmd,
4556592a0ad5SWebb Scales 		unsigned char *scsi3addr)
4557592a0ad5SWebb Scales {
4558592a0ad5SWebb Scales 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4559592a0ad5SWebb Scales 	int rc = IO_ACCEL_INELIGIBLE;
4560592a0ad5SWebb Scales 
4561592a0ad5SWebb Scales 	cmd->host_scribble = (unsigned char *) c;
4562592a0ad5SWebb Scales 
4563592a0ad5SWebb Scales 	if (dev->offload_enabled) {
4564592a0ad5SWebb Scales 		hpsa_cmd_init(h, c->cmdindex, c);
4565592a0ad5SWebb Scales 		c->cmd_type = CMD_SCSI;
4566592a0ad5SWebb Scales 		c->scsi_cmd = cmd;
4567592a0ad5SWebb Scales 		rc = hpsa_scsi_ioaccel_raid_map(h, c);
4568592a0ad5SWebb Scales 		if (rc < 0)     /* scsi_dma_map failed. */
4569592a0ad5SWebb Scales 			rc = SCSI_MLQUEUE_HOST_BUSY;
4570a3144e0bSJoe Handzik 	} else if (dev->hba_ioaccel_enabled) {
4571592a0ad5SWebb Scales 		hpsa_cmd_init(h, c->cmdindex, c);
4572592a0ad5SWebb Scales 		c->cmd_type = CMD_SCSI;
4573592a0ad5SWebb Scales 		c->scsi_cmd = cmd;
4574592a0ad5SWebb Scales 		rc = hpsa_scsi_ioaccel_direct_map(h, c);
4575592a0ad5SWebb Scales 		if (rc < 0)     /* scsi_dma_map failed. */
4576592a0ad5SWebb Scales 			rc = SCSI_MLQUEUE_HOST_BUSY;
4577592a0ad5SWebb Scales 	}
4578592a0ad5SWebb Scales 	return rc;
4579592a0ad5SWebb Scales }
4580592a0ad5SWebb Scales 
4581080ef1ccSDon Brace static void hpsa_command_resubmit_worker(struct work_struct *work)
4582080ef1ccSDon Brace {
4583080ef1ccSDon Brace 	struct scsi_cmnd *cmd;
4584080ef1ccSDon Brace 	struct hpsa_scsi_dev_t *dev;
45858a0ff92cSWebb Scales 	struct CommandList *c = container_of(work, struct CommandList, work);
4586080ef1ccSDon Brace 
4587080ef1ccSDon Brace 	cmd = c->scsi_cmd;
4588080ef1ccSDon Brace 	dev = cmd->device->hostdata;
4589080ef1ccSDon Brace 	if (!dev) {
4590080ef1ccSDon Brace 		cmd->result = DID_NO_CONNECT << 16;
45918a0ff92cSWebb Scales 		return hpsa_cmd_free_and_done(c->h, c, cmd);
4592080ef1ccSDon Brace 	}
4593a58e7e53SWebb Scales 	if (c->abort_pending)
4594a58e7e53SWebb Scales 		return hpsa_cmd_abort_and_free(c->h, c, cmd);
4595592a0ad5SWebb Scales 	if (c->cmd_type == CMD_IOACCEL2) {
4596592a0ad5SWebb Scales 		struct ctlr_info *h = c->h;
4597592a0ad5SWebb Scales 		struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
4598592a0ad5SWebb Scales 		int rc;
4599592a0ad5SWebb Scales 
4600592a0ad5SWebb Scales 		if (c2->error_data.serv_response ==
4601592a0ad5SWebb Scales 				IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL) {
4602592a0ad5SWebb Scales 			rc = hpsa_ioaccel_submit(h, c, cmd, dev->scsi3addr);
4603592a0ad5SWebb Scales 			if (rc == 0)
4604592a0ad5SWebb Scales 				return;
4605592a0ad5SWebb Scales 			if (rc == SCSI_MLQUEUE_HOST_BUSY) {
4606592a0ad5SWebb Scales 				/*
4607592a0ad5SWebb Scales 				 * If we get here, it means dma mapping failed.
4608592a0ad5SWebb Scales 				 * Try again via scsi mid layer, which will
4609592a0ad5SWebb Scales 				 * then get SCSI_MLQUEUE_HOST_BUSY.
4610592a0ad5SWebb Scales 				 */
4611592a0ad5SWebb Scales 				cmd->result = DID_IMM_RETRY << 16;
46128a0ff92cSWebb Scales 				return hpsa_cmd_free_and_done(h, c, cmd);
4613592a0ad5SWebb Scales 			}
4614592a0ad5SWebb Scales 			/* else, fall thru and resubmit down CISS path */
4615592a0ad5SWebb Scales 		}
4616592a0ad5SWebb Scales 	}
4617360c73bdSStephen Cameron 	hpsa_cmd_partial_init(c->h, c->cmdindex, c);
4618080ef1ccSDon Brace 	if (hpsa_ciss_submit(c->h, c, cmd, dev->scsi3addr)) {
4619080ef1ccSDon Brace 		/*
4620080ef1ccSDon Brace 		 * If we get here, it means dma mapping failed. Try
4621080ef1ccSDon Brace 		 * again via scsi mid layer, which will then get
4622080ef1ccSDon Brace 		 * SCSI_MLQUEUE_HOST_BUSY.
4623592a0ad5SWebb Scales 		 *
4624592a0ad5SWebb Scales 		 * hpsa_ciss_submit will have already freed c
4625592a0ad5SWebb Scales 		 * if it encountered a dma mapping failure.
4626080ef1ccSDon Brace 		 */
4627080ef1ccSDon Brace 		cmd->result = DID_IMM_RETRY << 16;
4628080ef1ccSDon Brace 		cmd->scsi_done(cmd);
4629080ef1ccSDon Brace 	}
4630080ef1ccSDon Brace }
4631080ef1ccSDon Brace 
4632574f05d3SStephen Cameron /* Running in struct Scsi_Host->host_lock less mode */
4633574f05d3SStephen Cameron static int hpsa_scsi_queue_command(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
4634574f05d3SStephen Cameron {
4635574f05d3SStephen Cameron 	struct ctlr_info *h;
4636574f05d3SStephen Cameron 	struct hpsa_scsi_dev_t *dev;
4637574f05d3SStephen Cameron 	unsigned char scsi3addr[8];
4638574f05d3SStephen Cameron 	struct CommandList *c;
4639574f05d3SStephen Cameron 	int rc = 0;
4640574f05d3SStephen Cameron 
4641574f05d3SStephen Cameron 	/* Get the ptr to our adapter structure out of cmd->host. */
4642574f05d3SStephen Cameron 	h = sdev_to_hba(cmd->device);
4643574f05d3SStephen Cameron 	dev = cmd->device->hostdata;
4644574f05d3SStephen Cameron 	if (!dev) {
4645574f05d3SStephen Cameron 		cmd->result = DID_NO_CONNECT << 16;
4646574f05d3SStephen Cameron 		cmd->scsi_done(cmd);
4647574f05d3SStephen Cameron 		return 0;
4648574f05d3SStephen Cameron 	}
4649574f05d3SStephen Cameron 	memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
4650574f05d3SStephen Cameron 
4651574f05d3SStephen Cameron 	if (unlikely(lockup_detected(h))) {
465225163bd5SWebb Scales 		cmd->result = DID_NO_CONNECT << 16;
4653574f05d3SStephen Cameron 		cmd->scsi_done(cmd);
4654574f05d3SStephen Cameron 		return 0;
4655574f05d3SStephen Cameron 	}
4656574f05d3SStephen Cameron 	c = cmd_alloc(h);
4657bf43caf3SRobert Elliott 
4658407863cbSStephen Cameron 	if (unlikely(lockup_detected(h))) {
465925163bd5SWebb Scales 		cmd->result = DID_NO_CONNECT << 16;
4660407863cbSStephen Cameron 		cmd_free(h, c);
4661407863cbSStephen Cameron 		cmd->scsi_done(cmd);
4662407863cbSStephen Cameron 		return 0;
4663407863cbSStephen Cameron 	}
4664574f05d3SStephen Cameron 
4665407863cbSStephen Cameron 	/*
4666407863cbSStephen Cameron 	 * Call alternate submit routine for I/O accelerated commands.
4667574f05d3SStephen Cameron 	 * Retries always go down the normal I/O path.
4668574f05d3SStephen Cameron 	 */
4669574f05d3SStephen Cameron 	if (likely(cmd->retries == 0 &&
4670574f05d3SStephen Cameron 		cmd->request->cmd_type == REQ_TYPE_FS &&
4671574f05d3SStephen Cameron 		h->acciopath_status)) {
4672592a0ad5SWebb Scales 		rc = hpsa_ioaccel_submit(h, c, cmd, scsi3addr);
4673574f05d3SStephen Cameron 		if (rc == 0)
4674592a0ad5SWebb Scales 			return 0;
4675592a0ad5SWebb Scales 		if (rc == SCSI_MLQUEUE_HOST_BUSY) {
46768a0ff92cSWebb Scales 			cmd_free(h, c);
4677574f05d3SStephen Cameron 			return SCSI_MLQUEUE_HOST_BUSY;
4678574f05d3SStephen Cameron 		}
4679574f05d3SStephen Cameron 	}
4680574f05d3SStephen Cameron 	return hpsa_ciss_submit(h, c, cmd, scsi3addr);
4681574f05d3SStephen Cameron }
4682574f05d3SStephen Cameron 
46838ebc9248SWebb Scales static void hpsa_scan_complete(struct ctlr_info *h)
46845f389360SStephen M. Cameron {
46855f389360SStephen M. Cameron 	unsigned long flags;
46865f389360SStephen M. Cameron 
46875f389360SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
46885f389360SStephen M. Cameron 	h->scan_finished = 1;
46895f389360SStephen M. Cameron 	wake_up_all(&h->scan_wait_queue);
46905f389360SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
46915f389360SStephen M. Cameron }
46925f389360SStephen M. Cameron 
4693a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *sh)
4694a08a8471SStephen M. Cameron {
4695a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
4696a08a8471SStephen M. Cameron 	unsigned long flags;
4697a08a8471SStephen M. Cameron 
46988ebc9248SWebb Scales 	/*
46998ebc9248SWebb Scales 	 * Don't let rescans be initiated on a controller known to be locked
47008ebc9248SWebb Scales 	 * up.  If the controller locks up *during* a rescan, that thread is
47018ebc9248SWebb Scales 	 * probably hosed, but at least we can prevent new rescan threads from
47028ebc9248SWebb Scales 	 * piling up on a locked up controller.
47038ebc9248SWebb Scales 	 */
47048ebc9248SWebb Scales 	if (unlikely(lockup_detected(h)))
47058ebc9248SWebb Scales 		return hpsa_scan_complete(h);
47065f389360SStephen M. Cameron 
4707a08a8471SStephen M. Cameron 	/* wait until any scan already in progress is finished. */
4708a08a8471SStephen M. Cameron 	while (1) {
4709a08a8471SStephen M. Cameron 		spin_lock_irqsave(&h->scan_lock, flags);
4710a08a8471SStephen M. Cameron 		if (h->scan_finished)
4711a08a8471SStephen M. Cameron 			break;
4712a08a8471SStephen M. Cameron 		spin_unlock_irqrestore(&h->scan_lock, flags);
4713a08a8471SStephen M. Cameron 		wait_event(h->scan_wait_queue, h->scan_finished);
4714a08a8471SStephen M. Cameron 		/* Note: We don't need to worry about a race between this
4715a08a8471SStephen M. Cameron 		 * thread and driver unload because the midlayer will
4716a08a8471SStephen M. Cameron 		 * have incremented the reference count, so unload won't
4717a08a8471SStephen M. Cameron 		 * happen if we're in here.
4718a08a8471SStephen M. Cameron 		 */
4719a08a8471SStephen M. Cameron 	}
4720a08a8471SStephen M. Cameron 	h->scan_finished = 0; /* mark scan as in progress */
4721a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4722a08a8471SStephen M. Cameron 
47238ebc9248SWebb Scales 	if (unlikely(lockup_detected(h)))
47248ebc9248SWebb Scales 		return hpsa_scan_complete(h);
47255f389360SStephen M. Cameron 
4726a08a8471SStephen M. Cameron 	hpsa_update_scsi_devices(h, h->scsi_host->host_no);
4727a08a8471SStephen M. Cameron 
47288ebc9248SWebb Scales 	hpsa_scan_complete(h);
4729a08a8471SStephen M. Cameron }
4730a08a8471SStephen M. Cameron 
47317c0a0229SDon Brace static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth)
47327c0a0229SDon Brace {
473303383736SDon Brace 	struct hpsa_scsi_dev_t *logical_drive = sdev->hostdata;
473403383736SDon Brace 
473503383736SDon Brace 	if (!logical_drive)
473603383736SDon Brace 		return -ENODEV;
47377c0a0229SDon Brace 
47387c0a0229SDon Brace 	if (qdepth < 1)
47397c0a0229SDon Brace 		qdepth = 1;
474003383736SDon Brace 	else if (qdepth > logical_drive->queue_depth)
474103383736SDon Brace 		qdepth = logical_drive->queue_depth;
474203383736SDon Brace 
474303383736SDon Brace 	return scsi_change_queue_depth(sdev, qdepth);
47447c0a0229SDon Brace }
47457c0a0229SDon Brace 
4746a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
4747a08a8471SStephen M. Cameron 	unsigned long elapsed_time)
4748a08a8471SStephen M. Cameron {
4749a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
4750a08a8471SStephen M. Cameron 	unsigned long flags;
4751a08a8471SStephen M. Cameron 	int finished;
4752a08a8471SStephen M. Cameron 
4753a08a8471SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
4754a08a8471SStephen M. Cameron 	finished = h->scan_finished;
4755a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4756a08a8471SStephen M. Cameron 	return finished;
4757a08a8471SStephen M. Cameron }
4758a08a8471SStephen M. Cameron 
4759edd16368SStephen M. Cameron static void hpsa_unregister_scsi(struct ctlr_info *h)
4760edd16368SStephen M. Cameron {
4761edd16368SStephen M. Cameron 	/* we are being forcibly unloaded, and may not refuse. */
4762edd16368SStephen M. Cameron 	scsi_remove_host(h->scsi_host);
4763edd16368SStephen M. Cameron 	scsi_host_put(h->scsi_host);
4764edd16368SStephen M. Cameron 	h->scsi_host = NULL;
4765edd16368SStephen M. Cameron }
4766edd16368SStephen M. Cameron 
4767edd16368SStephen M. Cameron static int hpsa_register_scsi(struct ctlr_info *h)
4768edd16368SStephen M. Cameron {
4769b705690dSStephen M. Cameron 	struct Scsi_Host *sh;
4770b705690dSStephen M. Cameron 	int error;
4771edd16368SStephen M. Cameron 
4772b705690dSStephen M. Cameron 	sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
4773b705690dSStephen M. Cameron 	if (sh == NULL)
4774b705690dSStephen M. Cameron 		goto fail;
4775b705690dSStephen M. Cameron 
4776b705690dSStephen M. Cameron 	sh->io_port = 0;
4777b705690dSStephen M. Cameron 	sh->n_io_port = 0;
4778b705690dSStephen M. Cameron 	sh->this_id = -1;
4779b705690dSStephen M. Cameron 	sh->max_channel = 3;
4780b705690dSStephen M. Cameron 	sh->max_cmd_len = MAX_COMMAND_SIZE;
4781b705690dSStephen M. Cameron 	sh->max_lun = HPSA_MAX_LUN;
4782b705690dSStephen M. Cameron 	sh->max_id = HPSA_MAX_LUN;
478341ce4c35SStephen Cameron 	sh->can_queue = h->nr_cmds - HPSA_NRESERVED_CMDS;
4784d54c5c24SStephen Cameron 	sh->cmd_per_lun = sh->can_queue;
4785b705690dSStephen M. Cameron 	sh->sg_tablesize = h->maxsgentries;
4786b705690dSStephen M. Cameron 	h->scsi_host = sh;
4787b705690dSStephen M. Cameron 	sh->hostdata[0] = (unsigned long) h;
4788b705690dSStephen M. Cameron 	sh->irq = h->intr[h->intr_mode];
4789b705690dSStephen M. Cameron 	sh->unique_id = sh->irq;
4790b705690dSStephen M. Cameron 	error = scsi_add_host(sh, &h->pdev->dev);
4791b705690dSStephen M. Cameron 	if (error)
4792b705690dSStephen M. Cameron 		goto fail_host_put;
4793b705690dSStephen M. Cameron 	scsi_scan_host(sh);
4794b705690dSStephen M. Cameron 	return 0;
4795b705690dSStephen M. Cameron 
4796b705690dSStephen M. Cameron  fail_host_put:
4797b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_add_host"
4798b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
4799b705690dSStephen M. Cameron 	scsi_host_put(sh);
4800b705690dSStephen M. Cameron 	return error;
4801b705690dSStephen M. Cameron  fail:
4802b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_host_alloc"
4803b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
4804b705690dSStephen M. Cameron 	return -ENOMEM;
4805edd16368SStephen M. Cameron }
4806edd16368SStephen M. Cameron 
4807b69324ffSWebb Scales /*
4808b69324ffSWebb Scales  * Send a TEST_UNIT_READY command to the specified LUN using the specified
4809b69324ffSWebb Scales  * reply queue; returns zero if the unit is ready, and non-zero otherwise.
4810b69324ffSWebb Scales  */
4811b69324ffSWebb Scales static int hpsa_send_test_unit_ready(struct ctlr_info *h,
4812b69324ffSWebb Scales 				struct CommandList *c, unsigned char lunaddr[],
4813b69324ffSWebb Scales 				int reply_queue)
4814edd16368SStephen M. Cameron {
48158919358eSTomas Henzl 	int rc;
4816edd16368SStephen M. Cameron 
4817a2dac136SStephen M. Cameron 	/* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
4818a2dac136SStephen M. Cameron 	(void) fill_cmd(c, TEST_UNIT_READY, h,
4819a2dac136SStephen M. Cameron 			NULL, 0, 0, lunaddr, TYPE_CMD);
4820b69324ffSWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
482125163bd5SWebb Scales 	if (rc)
4822b69324ffSWebb Scales 		return rc;
4823edd16368SStephen M. Cameron 	/* no unmap needed here because no data xfer. */
4824edd16368SStephen M. Cameron 
4825b69324ffSWebb Scales 	/* Check if the unit is already ready. */
4826edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus == CMD_SUCCESS)
4827b69324ffSWebb Scales 		return 0;
4828edd16368SStephen M. Cameron 
4829b69324ffSWebb Scales 	/*
4830b69324ffSWebb Scales 	 * The first command sent after reset will receive "unit attention" to
4831b69324ffSWebb Scales 	 * indicate that the LUN has been reset...this is actually what we're
4832b69324ffSWebb Scales 	 * looking for (but, success is good too).
4833b69324ffSWebb Scales 	 */
4834edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
4835edd16368SStephen M. Cameron 		c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
4836edd16368SStephen M. Cameron 			(c->err_info->SenseInfo[2] == NO_SENSE ||
4837edd16368SStephen M. Cameron 			 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
4838b69324ffSWebb Scales 		return 0;
4839b69324ffSWebb Scales 
4840b69324ffSWebb Scales 	return 1;
4841b69324ffSWebb Scales }
4842b69324ffSWebb Scales 
4843b69324ffSWebb Scales /*
4844b69324ffSWebb Scales  * Wait for a TEST_UNIT_READY command to complete, retrying as necessary;
4845b69324ffSWebb Scales  * returns zero when the unit is ready, and non-zero when giving up.
4846b69324ffSWebb Scales  */
4847b69324ffSWebb Scales static int hpsa_wait_for_test_unit_ready(struct ctlr_info *h,
4848b69324ffSWebb Scales 				struct CommandList *c,
4849b69324ffSWebb Scales 				unsigned char lunaddr[], int reply_queue)
4850b69324ffSWebb Scales {
4851b69324ffSWebb Scales 	int rc;
4852b69324ffSWebb Scales 	int count = 0;
4853b69324ffSWebb Scales 	int waittime = 1; /* seconds */
4854b69324ffSWebb Scales 
4855b69324ffSWebb Scales 	/* Send test unit ready until device ready, or give up. */
4856b69324ffSWebb Scales 	for (count = 0; count < HPSA_TUR_RETRY_LIMIT; count++) {
4857b69324ffSWebb Scales 
4858b69324ffSWebb Scales 		/*
4859b69324ffSWebb Scales 		 * Wait for a bit.  do this first, because if we send
4860b69324ffSWebb Scales 		 * the TUR right away, the reset will just abort it.
4861b69324ffSWebb Scales 		 */
4862b69324ffSWebb Scales 		msleep(1000 * waittime);
4863b69324ffSWebb Scales 
4864b69324ffSWebb Scales 		rc = hpsa_send_test_unit_ready(h, c, lunaddr, reply_queue);
4865b69324ffSWebb Scales 		if (!rc)
4866edd16368SStephen M. Cameron 			break;
4867b69324ffSWebb Scales 
4868b69324ffSWebb Scales 		/* Increase wait time with each try, up to a point. */
4869b69324ffSWebb Scales 		if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
4870b69324ffSWebb Scales 			waittime *= 2;
4871b69324ffSWebb Scales 
4872b69324ffSWebb Scales 		dev_warn(&h->pdev->dev,
4873b69324ffSWebb Scales 			 "waiting %d secs for device to become ready.\n",
4874b69324ffSWebb Scales 			 waittime);
4875b69324ffSWebb Scales 	}
4876b69324ffSWebb Scales 
4877b69324ffSWebb Scales 	return rc;
4878b69324ffSWebb Scales }
4879b69324ffSWebb Scales 
4880b69324ffSWebb Scales static int wait_for_device_to_become_ready(struct ctlr_info *h,
4881b69324ffSWebb Scales 					   unsigned char lunaddr[],
4882b69324ffSWebb Scales 					   int reply_queue)
4883b69324ffSWebb Scales {
4884b69324ffSWebb Scales 	int first_queue;
4885b69324ffSWebb Scales 	int last_queue;
4886b69324ffSWebb Scales 	int rq;
4887b69324ffSWebb Scales 	int rc = 0;
4888b69324ffSWebb Scales 	struct CommandList *c;
4889b69324ffSWebb Scales 
4890b69324ffSWebb Scales 	c = cmd_alloc(h);
4891b69324ffSWebb Scales 
4892b69324ffSWebb Scales 	/*
4893b69324ffSWebb Scales 	 * If no specific reply queue was requested, then send the TUR
4894b69324ffSWebb Scales 	 * repeatedly, requesting a reply on each reply queue; otherwise execute
4895b69324ffSWebb Scales 	 * the loop exactly once using only the specified queue.
4896b69324ffSWebb Scales 	 */
4897b69324ffSWebb Scales 	if (reply_queue == DEFAULT_REPLY_QUEUE) {
4898b69324ffSWebb Scales 		first_queue = 0;
4899b69324ffSWebb Scales 		last_queue = h->nreply_queues - 1;
4900b69324ffSWebb Scales 	} else {
4901b69324ffSWebb Scales 		first_queue = reply_queue;
4902b69324ffSWebb Scales 		last_queue = reply_queue;
4903b69324ffSWebb Scales 	}
4904b69324ffSWebb Scales 
4905b69324ffSWebb Scales 	for (rq = first_queue; rq <= last_queue; rq++) {
4906b69324ffSWebb Scales 		rc = hpsa_wait_for_test_unit_ready(h, c, lunaddr, rq);
4907b69324ffSWebb Scales 		if (rc)
4908b69324ffSWebb Scales 			break;
4909edd16368SStephen M. Cameron 	}
4910edd16368SStephen M. Cameron 
4911edd16368SStephen M. Cameron 	if (rc)
4912edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "giving up on device.\n");
4913edd16368SStephen M. Cameron 	else
4914edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "device is ready.\n");
4915edd16368SStephen M. Cameron 
491645fcb86eSStephen Cameron 	cmd_free(h, c);
4917edd16368SStephen M. Cameron 	return rc;
4918edd16368SStephen M. Cameron }
4919edd16368SStephen M. Cameron 
4920edd16368SStephen M. Cameron /* Need at least one of these error handlers to keep ../scsi/hosts.c from
4921edd16368SStephen M. Cameron  * complaining.  Doing a host- or bus-reset can't do anything good here.
4922edd16368SStephen M. Cameron  */
4923edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
4924edd16368SStephen M. Cameron {
4925edd16368SStephen M. Cameron 	int rc;
4926edd16368SStephen M. Cameron 	struct ctlr_info *h;
4927edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
4928edd16368SStephen M. Cameron 
4929edd16368SStephen M. Cameron 	/* find the controller to which the command to be aborted was sent */
4930edd16368SStephen M. Cameron 	h = sdev_to_hba(scsicmd->device);
4931edd16368SStephen M. Cameron 	if (h == NULL) /* paranoia */
4932edd16368SStephen M. Cameron 		return FAILED;
4933e345893bSDon Brace 
4934e345893bSDon Brace 	if (lockup_detected(h))
4935e345893bSDon Brace 		return FAILED;
4936e345893bSDon Brace 
4937edd16368SStephen M. Cameron 	dev = scsicmd->device->hostdata;
4938edd16368SStephen M. Cameron 	if (!dev) {
4939edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
4940edd16368SStephen M. Cameron 			"device lookup failed.\n");
4941edd16368SStephen M. Cameron 		return FAILED;
4942edd16368SStephen M. Cameron 	}
494325163bd5SWebb Scales 
494425163bd5SWebb Scales 	/* if controller locked up, we can guarantee command won't complete */
494525163bd5SWebb Scales 	if (lockup_detected(h)) {
494625163bd5SWebb Scales 		dev_warn(&h->pdev->dev,
494725163bd5SWebb Scales 			"scsi %d:%d:%d:%d RESET FAILED, lockup detected\n",
494825163bd5SWebb Scales 			h->scsi_host->host_no, dev->bus, dev->target,
494925163bd5SWebb Scales 			dev->lun);
495025163bd5SWebb Scales 		return FAILED;
495125163bd5SWebb Scales 	}
495225163bd5SWebb Scales 
495325163bd5SWebb Scales 	/* this reset request might be the result of a lockup; check */
495425163bd5SWebb Scales 	if (detect_controller_lockup(h)) {
495525163bd5SWebb Scales 		dev_warn(&h->pdev->dev,
495625163bd5SWebb Scales 			 "scsi %d:%d:%d:%d RESET FAILED, new lockup detected\n",
495725163bd5SWebb Scales 			 h->scsi_host->host_no, dev->bus, dev->target,
495825163bd5SWebb Scales 			 dev->lun);
495925163bd5SWebb Scales 		return FAILED;
496025163bd5SWebb Scales 	}
496125163bd5SWebb Scales 
496225163bd5SWebb Scales 	hpsa_show_dev_msg(KERN_WARNING, h, dev, "resetting");
496325163bd5SWebb Scales 
4964edd16368SStephen M. Cameron 	/* send a reset to the SCSI LUN which the command was sent to */
496525163bd5SWebb Scales 	rc = hpsa_send_reset(h, dev->scsi3addr, HPSA_RESET_TYPE_LUN,
496625163bd5SWebb Scales 			     DEFAULT_REPLY_QUEUE);
4967b69324ffSWebb Scales 	if (rc == 0)
4968edd16368SStephen M. Cameron 		return SUCCESS;
4969edd16368SStephen M. Cameron 
497025163bd5SWebb Scales 	dev_warn(&h->pdev->dev,
497125163bd5SWebb Scales 		"scsi %d:%d:%d:%d reset failed\n",
497225163bd5SWebb Scales 		h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
4973edd16368SStephen M. Cameron 	return FAILED;
4974edd16368SStephen M. Cameron }
4975edd16368SStephen M. Cameron 
49766cba3f19SStephen M. Cameron static void swizzle_abort_tag(u8 *tag)
49776cba3f19SStephen M. Cameron {
49786cba3f19SStephen M. Cameron 	u8 original_tag[8];
49796cba3f19SStephen M. Cameron 
49806cba3f19SStephen M. Cameron 	memcpy(original_tag, tag, 8);
49816cba3f19SStephen M. Cameron 	tag[0] = original_tag[3];
49826cba3f19SStephen M. Cameron 	tag[1] = original_tag[2];
49836cba3f19SStephen M. Cameron 	tag[2] = original_tag[1];
49846cba3f19SStephen M. Cameron 	tag[3] = original_tag[0];
49856cba3f19SStephen M. Cameron 	tag[4] = original_tag[7];
49866cba3f19SStephen M. Cameron 	tag[5] = original_tag[6];
49876cba3f19SStephen M. Cameron 	tag[6] = original_tag[5];
49886cba3f19SStephen M. Cameron 	tag[7] = original_tag[4];
49896cba3f19SStephen M. Cameron }
49906cba3f19SStephen M. Cameron 
499117eb87d2SScott Teel static void hpsa_get_tag(struct ctlr_info *h,
49922b08b3e9SDon Brace 	struct CommandList *c, __le32 *taglower, __le32 *tagupper)
499317eb87d2SScott Teel {
49942b08b3e9SDon Brace 	u64 tag;
499517eb87d2SScott Teel 	if (c->cmd_type == CMD_IOACCEL1) {
499617eb87d2SScott Teel 		struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
499717eb87d2SScott Teel 			&h->ioaccel_cmd_pool[c->cmdindex];
49982b08b3e9SDon Brace 		tag = le64_to_cpu(cm1->tag);
49992b08b3e9SDon Brace 		*tagupper = cpu_to_le32(tag >> 32);
50002b08b3e9SDon Brace 		*taglower = cpu_to_le32(tag);
500154b6e9e9SScott Teel 		return;
500254b6e9e9SScott Teel 	}
500354b6e9e9SScott Teel 	if (c->cmd_type == CMD_IOACCEL2) {
500454b6e9e9SScott Teel 		struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
500554b6e9e9SScott Teel 			&h->ioaccel2_cmd_pool[c->cmdindex];
5006dd0e19f3SScott Teel 		/* upper tag not used in ioaccel2 mode */
5007dd0e19f3SScott Teel 		memset(tagupper, 0, sizeof(*tagupper));
5008dd0e19f3SScott Teel 		*taglower = cm2->Tag;
500954b6e9e9SScott Teel 		return;
501054b6e9e9SScott Teel 	}
50112b08b3e9SDon Brace 	tag = le64_to_cpu(c->Header.tag);
50122b08b3e9SDon Brace 	*tagupper = cpu_to_le32(tag >> 32);
50132b08b3e9SDon Brace 	*taglower = cpu_to_le32(tag);
501417eb87d2SScott Teel }
501554b6e9e9SScott Teel 
501675167d2cSStephen M. Cameron static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
50179b5c48c2SStephen Cameron 	struct CommandList *abort, int reply_queue)
501875167d2cSStephen M. Cameron {
501975167d2cSStephen M. Cameron 	int rc = IO_OK;
502075167d2cSStephen M. Cameron 	struct CommandList *c;
502175167d2cSStephen M. Cameron 	struct ErrorInfo *ei;
50222b08b3e9SDon Brace 	__le32 tagupper, taglower;
502375167d2cSStephen M. Cameron 
502445fcb86eSStephen Cameron 	c = cmd_alloc(h);
502575167d2cSStephen M. Cameron 
5026a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no buffer to map */
50279b5c48c2SStephen Cameron 	(void) fill_cmd(c, HPSA_ABORT_MSG, h, &abort->Header.tag,
5028a2dac136SStephen M. Cameron 		0, 0, scsi3addr, TYPE_MSG);
50299b5c48c2SStephen Cameron 	if (h->needs_abort_tags_swizzled)
50306cba3f19SStephen M. Cameron 		swizzle_abort_tag(&c->Request.CDB[4]);
503125163bd5SWebb Scales 	(void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
503217eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
503325163bd5SWebb Scales 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd(abort) completed.\n",
503417eb87d2SScott Teel 		__func__, tagupper, taglower);
503575167d2cSStephen M. Cameron 	/* no unmap needed here because no data xfer. */
503675167d2cSStephen M. Cameron 
503775167d2cSStephen M. Cameron 	ei = c->err_info;
503875167d2cSStephen M. Cameron 	switch (ei->CommandStatus) {
503975167d2cSStephen M. Cameron 	case CMD_SUCCESS:
504075167d2cSStephen M. Cameron 		break;
50419437ac43SStephen Cameron 	case CMD_TMF_STATUS:
50429437ac43SStephen Cameron 		rc = hpsa_evaluate_tmf_status(h, c);
50439437ac43SStephen Cameron 		break;
504475167d2cSStephen M. Cameron 	case CMD_UNABORTABLE: /* Very common, don't make noise. */
504575167d2cSStephen M. Cameron 		rc = -1;
504675167d2cSStephen M. Cameron 		break;
504775167d2cSStephen M. Cameron 	default:
504875167d2cSStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
504917eb87d2SScott Teel 			__func__, tagupper, taglower);
5050d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
505175167d2cSStephen M. Cameron 		rc = -1;
505275167d2cSStephen M. Cameron 		break;
505375167d2cSStephen M. Cameron 	}
505445fcb86eSStephen Cameron 	cmd_free(h, c);
5055dd0e19f3SScott Teel 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n",
5056dd0e19f3SScott Teel 		__func__, tagupper, taglower);
505775167d2cSStephen M. Cameron 	return rc;
505875167d2cSStephen M. Cameron }
505975167d2cSStephen M. Cameron 
50608be986ccSStephen Cameron static void setup_ioaccel2_abort_cmd(struct CommandList *c, struct ctlr_info *h,
50618be986ccSStephen Cameron 	struct CommandList *command_to_abort, int reply_queue)
50628be986ccSStephen Cameron {
50638be986ccSStephen Cameron 	struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
50648be986ccSStephen Cameron 	struct hpsa_tmf_struct *ac = (struct hpsa_tmf_struct *) c2;
50658be986ccSStephen Cameron 	struct io_accel2_cmd *c2a =
50668be986ccSStephen Cameron 		&h->ioaccel2_cmd_pool[command_to_abort->cmdindex];
5067a58e7e53SWebb Scales 	struct scsi_cmnd *scmd = command_to_abort->scsi_cmd;
50688be986ccSStephen Cameron 	struct hpsa_scsi_dev_t *dev = scmd->device->hostdata;
50698be986ccSStephen Cameron 
50708be986ccSStephen Cameron 	/*
50718be986ccSStephen Cameron 	 * We're overlaying struct hpsa_tmf_struct on top of something which
50728be986ccSStephen Cameron 	 * was allocated as a struct io_accel2_cmd, so we better be sure it
50738be986ccSStephen Cameron 	 * actually fits, and doesn't overrun the error info space.
50748be986ccSStephen Cameron 	 */
50758be986ccSStephen Cameron 	BUILD_BUG_ON(sizeof(struct hpsa_tmf_struct) >
50768be986ccSStephen Cameron 			sizeof(struct io_accel2_cmd));
50778be986ccSStephen Cameron 	BUG_ON(offsetof(struct io_accel2_cmd, error_data) <
50788be986ccSStephen Cameron 			offsetof(struct hpsa_tmf_struct, error_len) +
50798be986ccSStephen Cameron 				sizeof(ac->error_len));
50808be986ccSStephen Cameron 
50818be986ccSStephen Cameron 	c->cmd_type = IOACCEL2_TMF;
5082a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_BUSY;
5083a58e7e53SWebb Scales 
50848be986ccSStephen Cameron 	/* Adjust the DMA address to point to the accelerated command buffer */
50858be986ccSStephen Cameron 	c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
50868be986ccSStephen Cameron 				(c->cmdindex * sizeof(struct io_accel2_cmd));
50878be986ccSStephen Cameron 	BUG_ON(c->busaddr & 0x0000007F);
50888be986ccSStephen Cameron 
50898be986ccSStephen Cameron 	memset(ac, 0, sizeof(*c2)); /* yes this is correct */
50908be986ccSStephen Cameron 	ac->iu_type = IOACCEL2_IU_TMF_TYPE;
50918be986ccSStephen Cameron 	ac->reply_queue = reply_queue;
50928be986ccSStephen Cameron 	ac->tmf = IOACCEL2_TMF_ABORT;
50938be986ccSStephen Cameron 	ac->it_nexus = cpu_to_le32(dev->ioaccel_handle);
50948be986ccSStephen Cameron 	memset(ac->lun_id, 0, sizeof(ac->lun_id));
50958be986ccSStephen Cameron 	ac->tag = cpu_to_le64(c->cmdindex << DIRECT_LOOKUP_SHIFT);
50968be986ccSStephen Cameron 	ac->abort_tag = cpu_to_le64(le32_to_cpu(c2a->Tag));
50978be986ccSStephen Cameron 	ac->error_ptr = cpu_to_le64(c->busaddr +
50988be986ccSStephen Cameron 			offsetof(struct io_accel2_cmd, error_data));
50998be986ccSStephen Cameron 	ac->error_len = cpu_to_le32(sizeof(c2->error_data));
51008be986ccSStephen Cameron }
51018be986ccSStephen Cameron 
510254b6e9e9SScott Teel /* ioaccel2 path firmware cannot handle abort task requests.
510354b6e9e9SScott Teel  * Change abort requests to physical target reset, and send to the
510454b6e9e9SScott Teel  * address of the physical disk used for the ioaccel 2 command.
510554b6e9e9SScott Teel  * Return 0 on success (IO_OK)
510654b6e9e9SScott Teel  *	 -1 on failure
510754b6e9e9SScott Teel  */
510854b6e9e9SScott Teel 
510954b6e9e9SScott Teel static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
511025163bd5SWebb Scales 	unsigned char *scsi3addr, struct CommandList *abort, int reply_queue)
511154b6e9e9SScott Teel {
511254b6e9e9SScott Teel 	int rc = IO_OK;
511354b6e9e9SScott Teel 	struct scsi_cmnd *scmd; /* scsi command within request being aborted */
511454b6e9e9SScott Teel 	struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
511554b6e9e9SScott Teel 	unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
511654b6e9e9SScott Teel 	unsigned char *psa = &phys_scsi3addr[0];
511754b6e9e9SScott Teel 
511854b6e9e9SScott Teel 	/* Get a pointer to the hpsa logical device. */
51197fa3030cSStephen Cameron 	scmd = abort->scsi_cmd;
512054b6e9e9SScott Teel 	dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
512154b6e9e9SScott Teel 	if (dev == NULL) {
512254b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
512354b6e9e9SScott Teel 			"Cannot abort: no device pointer for command.\n");
512454b6e9e9SScott Teel 			return -1; /* not abortable */
512554b6e9e9SScott Teel 	}
512654b6e9e9SScott Teel 
51272ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
51282ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
51290d96ef5fSWebb Scales 			"scsi %d:%d:%d:%d %s scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
51302ba8bfc8SStephen M. Cameron 			h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
51310d96ef5fSWebb Scales 			"Reset as abort",
51322ba8bfc8SStephen M. Cameron 			scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
51332ba8bfc8SStephen M. Cameron 			scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]);
51342ba8bfc8SStephen M. Cameron 
513554b6e9e9SScott Teel 	if (!dev->offload_enabled) {
513654b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
513754b6e9e9SScott Teel 			"Can't abort: device is not operating in HP SSD Smart Path mode.\n");
513854b6e9e9SScott Teel 		return -1; /* not abortable */
513954b6e9e9SScott Teel 	}
514054b6e9e9SScott Teel 
514154b6e9e9SScott Teel 	/* Incoming scsi3addr is logical addr. We need physical disk addr. */
514254b6e9e9SScott Teel 	if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
514354b6e9e9SScott Teel 		dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
514454b6e9e9SScott Teel 		return -1; /* not abortable */
514554b6e9e9SScott Teel 	}
514654b6e9e9SScott Teel 
514754b6e9e9SScott Teel 	/* send the reset */
51482ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
51492ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
51502ba8bfc8SStephen M. Cameron 			"Reset as abort: Resetting physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
51512ba8bfc8SStephen M. Cameron 			psa[0], psa[1], psa[2], psa[3],
51522ba8bfc8SStephen M. Cameron 			psa[4], psa[5], psa[6], psa[7]);
515325163bd5SWebb Scales 	rc = hpsa_send_reset(h, psa, HPSA_RESET_TYPE_TARGET, reply_queue);
515454b6e9e9SScott Teel 	if (rc != 0) {
515554b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
515654b6e9e9SScott Teel 			"Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
515754b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
515854b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
515954b6e9e9SScott Teel 		return rc; /* failed to reset */
516054b6e9e9SScott Teel 	}
516154b6e9e9SScott Teel 
516254b6e9e9SScott Teel 	/* wait for device to recover */
5163b69324ffSWebb Scales 	if (wait_for_device_to_become_ready(h, psa, reply_queue) != 0) {
516454b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
516554b6e9e9SScott Teel 			"Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
516654b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
516754b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
516854b6e9e9SScott Teel 		return -1;  /* failed to recover */
516954b6e9e9SScott Teel 	}
517054b6e9e9SScott Teel 
517154b6e9e9SScott Teel 	/* device recovered */
517254b6e9e9SScott Teel 	dev_info(&h->pdev->dev,
517354b6e9e9SScott Teel 		"Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
517454b6e9e9SScott Teel 		psa[0], psa[1], psa[2], psa[3],
517554b6e9e9SScott Teel 		psa[4], psa[5], psa[6], psa[7]);
517654b6e9e9SScott Teel 
517754b6e9e9SScott Teel 	return rc; /* success */
517854b6e9e9SScott Teel }
517954b6e9e9SScott Teel 
51808be986ccSStephen Cameron static int hpsa_send_abort_ioaccel2(struct ctlr_info *h,
51818be986ccSStephen Cameron 	struct CommandList *abort, int reply_queue)
51828be986ccSStephen Cameron {
51838be986ccSStephen Cameron 	int rc = IO_OK;
51848be986ccSStephen Cameron 	struct CommandList *c;
51858be986ccSStephen Cameron 	__le32 taglower, tagupper;
51868be986ccSStephen Cameron 	struct hpsa_scsi_dev_t *dev;
51878be986ccSStephen Cameron 	struct io_accel2_cmd *c2;
51888be986ccSStephen Cameron 
51898be986ccSStephen Cameron 	dev = abort->scsi_cmd->device->hostdata;
51908be986ccSStephen Cameron 	if (!dev->offload_enabled && !dev->hba_ioaccel_enabled)
51918be986ccSStephen Cameron 		return -1;
51928be986ccSStephen Cameron 
51938be986ccSStephen Cameron 	c = cmd_alloc(h);
51948be986ccSStephen Cameron 	setup_ioaccel2_abort_cmd(c, h, abort, reply_queue);
51958be986ccSStephen Cameron 	c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
51968be986ccSStephen Cameron 	(void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
51978be986ccSStephen Cameron 	hpsa_get_tag(h, abort, &taglower, &tagupper);
51988be986ccSStephen Cameron 	dev_dbg(&h->pdev->dev,
51998be986ccSStephen Cameron 		"%s: Tag:0x%08x:%08x: do_simple_cmd(ioaccel2 abort) completed.\n",
52008be986ccSStephen Cameron 		__func__, tagupper, taglower);
52018be986ccSStephen Cameron 	/* no unmap needed here because no data xfer. */
52028be986ccSStephen Cameron 
52038be986ccSStephen Cameron 	dev_dbg(&h->pdev->dev,
52048be986ccSStephen Cameron 		"%s: Tag:0x%08x:%08x: abort service response = 0x%02x.\n",
52058be986ccSStephen Cameron 		__func__, tagupper, taglower, c2->error_data.serv_response);
52068be986ccSStephen Cameron 	switch (c2->error_data.serv_response) {
52078be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
52088be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
52098be986ccSStephen Cameron 		rc = 0;
52108be986ccSStephen Cameron 		break;
52118be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
52128be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_FAILURE:
52138be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
52148be986ccSStephen Cameron 		rc = -1;
52158be986ccSStephen Cameron 		break;
52168be986ccSStephen Cameron 	default:
52178be986ccSStephen Cameron 		dev_warn(&h->pdev->dev,
52188be986ccSStephen Cameron 			"%s: Tag:0x%08x:%08x: unknown abort service response 0x%02x\n",
52198be986ccSStephen Cameron 			__func__, tagupper, taglower,
52208be986ccSStephen Cameron 			c2->error_data.serv_response);
52218be986ccSStephen Cameron 		rc = -1;
52228be986ccSStephen Cameron 	}
52238be986ccSStephen Cameron 	cmd_free(h, c);
52248be986ccSStephen Cameron 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n", __func__,
52258be986ccSStephen Cameron 		tagupper, taglower);
52268be986ccSStephen Cameron 	return rc;
52278be986ccSStephen Cameron }
52288be986ccSStephen Cameron 
52296cba3f19SStephen M. Cameron static int hpsa_send_abort_both_ways(struct ctlr_info *h,
523025163bd5SWebb Scales 	unsigned char *scsi3addr, struct CommandList *abort, int reply_queue)
52316cba3f19SStephen M. Cameron {
52328be986ccSStephen Cameron 	/*
52338be986ccSStephen Cameron 	 * ioccelerator mode 2 commands should be aborted via the
523454b6e9e9SScott Teel 	 * accelerated path, since RAID path is unaware of these commands,
52358be986ccSStephen Cameron 	 * but not all underlying firmware can handle abort TMF.
52368be986ccSStephen Cameron 	 * Change abort to physical device reset when abort TMF is unsupported.
523754b6e9e9SScott Teel 	 */
52388be986ccSStephen Cameron 	if (abort->cmd_type == CMD_IOACCEL2) {
52398be986ccSStephen Cameron 		if (HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags)
52408be986ccSStephen Cameron 			return hpsa_send_abort_ioaccel2(h, abort,
52418be986ccSStephen Cameron 						reply_queue);
52428be986ccSStephen Cameron 		else
524325163bd5SWebb Scales 			return hpsa_send_reset_as_abort_ioaccel2(h, scsi3addr,
524425163bd5SWebb Scales 							abort, reply_queue);
52458be986ccSStephen Cameron 	}
52469b5c48c2SStephen Cameron 	return hpsa_send_abort(h, scsi3addr, abort, reply_queue);
524725163bd5SWebb Scales }
524825163bd5SWebb Scales 
524925163bd5SWebb Scales /* Find out which reply queue a command was meant to return on */
525025163bd5SWebb Scales static int hpsa_extract_reply_queue(struct ctlr_info *h,
525125163bd5SWebb Scales 					struct CommandList *c)
525225163bd5SWebb Scales {
525325163bd5SWebb Scales 	if (c->cmd_type == CMD_IOACCEL2)
525425163bd5SWebb Scales 		return h->ioaccel2_cmd_pool[c->cmdindex].reply_queue;
525525163bd5SWebb Scales 	return c->Header.ReplyQueue;
52566cba3f19SStephen M. Cameron }
52576cba3f19SStephen M. Cameron 
52589b5c48c2SStephen Cameron /*
52599b5c48c2SStephen Cameron  * Limit concurrency of abort commands to prevent
52609b5c48c2SStephen Cameron  * over-subscription of commands
52619b5c48c2SStephen Cameron  */
52629b5c48c2SStephen Cameron static inline int wait_for_available_abort_cmd(struct ctlr_info *h)
52639b5c48c2SStephen Cameron {
52649b5c48c2SStephen Cameron #define ABORT_CMD_WAIT_MSECS 5000
52659b5c48c2SStephen Cameron 	return !wait_event_timeout(h->abort_cmd_wait_queue,
52669b5c48c2SStephen Cameron 			atomic_dec_if_positive(&h->abort_cmds_available) >= 0,
52679b5c48c2SStephen Cameron 			msecs_to_jiffies(ABORT_CMD_WAIT_MSECS));
52689b5c48c2SStephen Cameron }
52699b5c48c2SStephen Cameron 
527075167d2cSStephen M. Cameron /* Send an abort for the specified command.
527175167d2cSStephen M. Cameron  *	If the device and controller support it,
527275167d2cSStephen M. Cameron  *		send a task abort request.
527375167d2cSStephen M. Cameron  */
527475167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
527575167d2cSStephen M. Cameron {
527675167d2cSStephen M. Cameron 
5277a58e7e53SWebb Scales 	int rc;
527875167d2cSStephen M. Cameron 	struct ctlr_info *h;
527975167d2cSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
528075167d2cSStephen M. Cameron 	struct CommandList *abort; /* pointer to command to be aborted */
528175167d2cSStephen M. Cameron 	struct scsi_cmnd *as;	/* ptr to scsi cmd inside aborted command. */
528275167d2cSStephen M. Cameron 	char msg[256];		/* For debug messaging. */
528375167d2cSStephen M. Cameron 	int ml = 0;
52842b08b3e9SDon Brace 	__le32 tagupper, taglower;
528525163bd5SWebb Scales 	int refcount, reply_queue;
528625163bd5SWebb Scales 
528725163bd5SWebb Scales 	if (sc == NULL)
528825163bd5SWebb Scales 		return FAILED;
528975167d2cSStephen M. Cameron 
52909b5c48c2SStephen Cameron 	if (sc->device == NULL)
52919b5c48c2SStephen Cameron 		return FAILED;
52929b5c48c2SStephen Cameron 
529375167d2cSStephen M. Cameron 	/* Find the controller of the command to be aborted */
529475167d2cSStephen M. Cameron 	h = sdev_to_hba(sc->device);
52959b5c48c2SStephen Cameron 	if (h == NULL)
529675167d2cSStephen M. Cameron 		return FAILED;
529775167d2cSStephen M. Cameron 
529825163bd5SWebb Scales 	/* Find the device of the command to be aborted */
529925163bd5SWebb Scales 	dev = sc->device->hostdata;
530025163bd5SWebb Scales 	if (!dev) {
530125163bd5SWebb Scales 		dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
530225163bd5SWebb Scales 				msg);
5303e345893bSDon Brace 		return FAILED;
530425163bd5SWebb Scales 	}
530525163bd5SWebb Scales 
530625163bd5SWebb Scales 	/* If controller locked up, we can guarantee command won't complete */
530725163bd5SWebb Scales 	if (lockup_detected(h)) {
530825163bd5SWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev,
530925163bd5SWebb Scales 					"ABORT FAILED, lockup detected");
531025163bd5SWebb Scales 		return FAILED;
531125163bd5SWebb Scales 	}
531225163bd5SWebb Scales 
531325163bd5SWebb Scales 	/* This is a good time to check if controller lockup has occurred */
531425163bd5SWebb Scales 	if (detect_controller_lockup(h)) {
531525163bd5SWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev,
531625163bd5SWebb Scales 					"ABORT FAILED, new lockup detected");
531725163bd5SWebb Scales 		return FAILED;
531825163bd5SWebb Scales 	}
5319e345893bSDon Brace 
532075167d2cSStephen M. Cameron 	/* Check that controller supports some kind of task abort */
532175167d2cSStephen M. Cameron 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
532275167d2cSStephen M. Cameron 		!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
532375167d2cSStephen M. Cameron 		return FAILED;
532475167d2cSStephen M. Cameron 
532575167d2cSStephen M. Cameron 	memset(msg, 0, sizeof(msg));
53264b761557SRobert Elliott 	ml += sprintf(msg+ml, "scsi %d:%d:%d:%llu %s %p",
532775167d2cSStephen M. Cameron 		h->scsi_host->host_no, sc->device->channel,
53280d96ef5fSWebb Scales 		sc->device->id, sc->device->lun,
53294b761557SRobert Elliott 		"Aborting command", sc);
533075167d2cSStephen M. Cameron 
533175167d2cSStephen M. Cameron 	/* Get SCSI command to be aborted */
533275167d2cSStephen M. Cameron 	abort = (struct CommandList *) sc->host_scribble;
533375167d2cSStephen M. Cameron 	if (abort == NULL) {
5334281a7fd0SWebb Scales 		/* This can happen if the command already completed. */
5335281a7fd0SWebb Scales 		return SUCCESS;
5336281a7fd0SWebb Scales 	}
5337281a7fd0SWebb Scales 	refcount = atomic_inc_return(&abort->refcount);
5338281a7fd0SWebb Scales 	if (refcount == 1) { /* Command is done already. */
5339281a7fd0SWebb Scales 		cmd_free(h, abort);
5340281a7fd0SWebb Scales 		return SUCCESS;
534175167d2cSStephen M. Cameron 	}
53429b5c48c2SStephen Cameron 
53439b5c48c2SStephen Cameron 	/* Don't bother trying the abort if we know it won't work. */
53449b5c48c2SStephen Cameron 	if (abort->cmd_type != CMD_IOACCEL2 &&
53459b5c48c2SStephen Cameron 		abort->cmd_type != CMD_IOACCEL1 && !dev->supports_aborts) {
53469b5c48c2SStephen Cameron 		cmd_free(h, abort);
53479b5c48c2SStephen Cameron 		return FAILED;
53489b5c48c2SStephen Cameron 	}
53499b5c48c2SStephen Cameron 
5350a58e7e53SWebb Scales 	/*
5351a58e7e53SWebb Scales 	 * Check that we're aborting the right command.
5352a58e7e53SWebb Scales 	 * It's possible the CommandList already completed and got re-used.
5353a58e7e53SWebb Scales 	 */
5354a58e7e53SWebb Scales 	if (abort->scsi_cmd != sc) {
5355a58e7e53SWebb Scales 		cmd_free(h, abort);
5356a58e7e53SWebb Scales 		return SUCCESS;
5357a58e7e53SWebb Scales 	}
5358a58e7e53SWebb Scales 
5359a58e7e53SWebb Scales 	abort->abort_pending = true;
536017eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
536125163bd5SWebb Scales 	reply_queue = hpsa_extract_reply_queue(h, abort);
536217eb87d2SScott Teel 	ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
53637fa3030cSStephen Cameron 	as  = abort->scsi_cmd;
536475167d2cSStephen M. Cameron 	if (as != NULL)
53654b761557SRobert Elliott 		ml += sprintf(msg+ml,
53664b761557SRobert Elliott 			"CDBLen: %d CDB: 0x%02x%02x... SN: 0x%lx ",
53674b761557SRobert Elliott 			as->cmd_len, as->cmnd[0], as->cmnd[1],
53684b761557SRobert Elliott 			as->serial_number);
53694b761557SRobert Elliott 	dev_warn(&h->pdev->dev, "%s BEING SENT\n", msg);
53700d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_WARNING, h, dev, "Aborting command");
53714b761557SRobert Elliott 
537275167d2cSStephen M. Cameron 	/*
537375167d2cSStephen M. Cameron 	 * Command is in flight, or possibly already completed
537475167d2cSStephen M. Cameron 	 * by the firmware (but not to the scsi mid layer) but we can't
537575167d2cSStephen M. Cameron 	 * distinguish which.  Send the abort down.
537675167d2cSStephen M. Cameron 	 */
53779b5c48c2SStephen Cameron 	if (wait_for_available_abort_cmd(h)) {
53789b5c48c2SStephen Cameron 		dev_warn(&h->pdev->dev,
53794b761557SRobert Elliott 			"%s FAILED, timeout waiting for an abort command to become available.\n",
53804b761557SRobert Elliott 			msg);
53819b5c48c2SStephen Cameron 		cmd_free(h, abort);
53829b5c48c2SStephen Cameron 		return FAILED;
53839b5c48c2SStephen Cameron 	}
538425163bd5SWebb Scales 	rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort, reply_queue);
53859b5c48c2SStephen Cameron 	atomic_inc(&h->abort_cmds_available);
53869b5c48c2SStephen Cameron 	wake_up_all(&h->abort_cmd_wait_queue);
538775167d2cSStephen M. Cameron 	if (rc != 0) {
53884b761557SRobert Elliott 		dev_warn(&h->pdev->dev, "%s SENT, FAILED\n", msg);
53890d96ef5fSWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev,
53900d96ef5fSWebb Scales 				"FAILED to abort command");
5391281a7fd0SWebb Scales 		cmd_free(h, abort);
539275167d2cSStephen M. Cameron 		return FAILED;
539375167d2cSStephen M. Cameron 	}
53944b761557SRobert Elliott 	dev_info(&h->pdev->dev, "%s SENT, SUCCESS\n", msg);
5395a58e7e53SWebb Scales 	wait_event(h->abort_sync_wait_queue,
5396a58e7e53SWebb Scales 		   abort->scsi_cmd != sc || lockup_detected(h));
5397281a7fd0SWebb Scales 	cmd_free(h, abort);
5398a58e7e53SWebb Scales 	return !lockup_detected(h) ? SUCCESS : FAILED;
539975167d2cSStephen M. Cameron }
540075167d2cSStephen M. Cameron 
5401edd16368SStephen M. Cameron /*
5402edd16368SStephen M. Cameron  * For operations that cannot sleep, a command block is allocated at init,
5403edd16368SStephen M. Cameron  * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
5404edd16368SStephen M. Cameron  * which ones are free or in use.  Lock must be held when calling this.
5405edd16368SStephen M. Cameron  * cmd_free() is the complement.
5406bf43caf3SRobert Elliott  * This function never gives up and returns NULL.  If it hangs,
5407bf43caf3SRobert Elliott  * another thread must call cmd_free() to free some tags.
5408edd16368SStephen M. Cameron  */
5409281a7fd0SWebb Scales 
5410edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h)
5411edd16368SStephen M. Cameron {
5412edd16368SStephen M. Cameron 	struct CommandList *c;
5413360c73bdSStephen Cameron 	int refcount, i;
541433811026SRobert Elliott 	unsigned long offset;
5415edd16368SStephen M. Cameron 
541633811026SRobert Elliott 	/*
541733811026SRobert Elliott 	 * There is some *extremely* small but non-zero chance that that
54184c413128SStephen M. Cameron 	 * multiple threads could get in here, and one thread could
54194c413128SStephen M. Cameron 	 * be scanning through the list of bits looking for a free
54204c413128SStephen M. Cameron 	 * one, but the free ones are always behind him, and other
54214c413128SStephen M. Cameron 	 * threads sneak in behind him and eat them before he can
54224c413128SStephen M. Cameron 	 * get to them, so that while there is always a free one, a
54234c413128SStephen M. Cameron 	 * very unlucky thread might be starved anyway, never able to
54244c413128SStephen M. Cameron 	 * beat the other threads.  In reality, this happens so
54254c413128SStephen M. Cameron 	 * infrequently as to be indistinguishable from never.
54264c413128SStephen M. Cameron 	 */
54274c413128SStephen M. Cameron 
542833811026SRobert Elliott 	offset = h->last_allocation; /* benignly racy */
5429281a7fd0SWebb Scales 	for (;;) {
5430281a7fd0SWebb Scales 		i = find_next_zero_bit(h->cmd_pool_bits, h->nr_cmds, offset);
5431281a7fd0SWebb Scales 		if (unlikely(i == h->nr_cmds)) {
5432281a7fd0SWebb Scales 			offset = 0;
5433281a7fd0SWebb Scales 			continue;
5434281a7fd0SWebb Scales 		}
5435edd16368SStephen M. Cameron 		c = h->cmd_pool + i;
5436281a7fd0SWebb Scales 		refcount = atomic_inc_return(&c->refcount);
5437281a7fd0SWebb Scales 		if (unlikely(refcount > 1)) {
5438281a7fd0SWebb Scales 			cmd_free(h, c); /* already in use */
5439281a7fd0SWebb Scales 			offset = (i + 1) % h->nr_cmds;
5440281a7fd0SWebb Scales 			continue;
5441281a7fd0SWebb Scales 		}
5442281a7fd0SWebb Scales 		set_bit(i & (BITS_PER_LONG - 1),
5443281a7fd0SWebb Scales 			h->cmd_pool_bits + (i / BITS_PER_LONG));
5444281a7fd0SWebb Scales 		break; /* it's ours now. */
5445281a7fd0SWebb Scales 	}
544633811026SRobert Elliott 	h->last_allocation = i; /* benignly racy */
5447360c73bdSStephen Cameron 	hpsa_cmd_partial_init(h, i, c);
5448edd16368SStephen M. Cameron 	return c;
5449edd16368SStephen M. Cameron }
5450edd16368SStephen M. Cameron 
5451edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c)
5452edd16368SStephen M. Cameron {
5453281a7fd0SWebb Scales 	if (atomic_dec_and_test(&c->refcount)) {
5454edd16368SStephen M. Cameron 		int i;
5455edd16368SStephen M. Cameron 
5456edd16368SStephen M. Cameron 		i = c - h->cmd_pool;
5457edd16368SStephen M. Cameron 		clear_bit(i & (BITS_PER_LONG - 1),
5458edd16368SStephen M. Cameron 			  h->cmd_pool_bits + (i / BITS_PER_LONG));
5459edd16368SStephen M. Cameron 	}
5460281a7fd0SWebb Scales }
5461edd16368SStephen M. Cameron 
5462edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
5463edd16368SStephen M. Cameron 
546442a91641SDon Brace static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd,
546542a91641SDon Brace 	void __user *arg)
5466edd16368SStephen M. Cameron {
5467edd16368SStephen M. Cameron 	IOCTL32_Command_struct __user *arg32 =
5468edd16368SStephen M. Cameron 	    (IOCTL32_Command_struct __user *) arg;
5469edd16368SStephen M. Cameron 	IOCTL_Command_struct arg64;
5470edd16368SStephen M. Cameron 	IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
5471edd16368SStephen M. Cameron 	int err;
5472edd16368SStephen M. Cameron 	u32 cp;
5473edd16368SStephen M. Cameron 
5474938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
5475edd16368SStephen M. Cameron 	err = 0;
5476edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
5477edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
5478edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
5479edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
5480edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
5481edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
5482edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
5483edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
5484edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
5485edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
5486edd16368SStephen M. Cameron 
5487edd16368SStephen M. Cameron 	if (err)
5488edd16368SStephen M. Cameron 		return -EFAULT;
5489edd16368SStephen M. Cameron 
549042a91641SDon Brace 	err = hpsa_ioctl(dev, CCISS_PASSTHRU, p);
5491edd16368SStephen M. Cameron 	if (err)
5492edd16368SStephen M. Cameron 		return err;
5493edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
5494edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
5495edd16368SStephen M. Cameron 	if (err)
5496edd16368SStephen M. Cameron 		return -EFAULT;
5497edd16368SStephen M. Cameron 	return err;
5498edd16368SStephen M. Cameron }
5499edd16368SStephen M. Cameron 
5500edd16368SStephen M. Cameron static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
550142a91641SDon Brace 	int cmd, void __user *arg)
5502edd16368SStephen M. Cameron {
5503edd16368SStephen M. Cameron 	BIG_IOCTL32_Command_struct __user *arg32 =
5504edd16368SStephen M. Cameron 	    (BIG_IOCTL32_Command_struct __user *) arg;
5505edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct arg64;
5506edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct __user *p =
5507edd16368SStephen M. Cameron 	    compat_alloc_user_space(sizeof(arg64));
5508edd16368SStephen M. Cameron 	int err;
5509edd16368SStephen M. Cameron 	u32 cp;
5510edd16368SStephen M. Cameron 
5511938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
5512edd16368SStephen M. Cameron 	err = 0;
5513edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
5514edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
5515edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
5516edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
5517edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
5518edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
5519edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
5520edd16368SStephen M. Cameron 	err |= get_user(arg64.malloc_size, &arg32->malloc_size);
5521edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
5522edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
5523edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
5524edd16368SStephen M. Cameron 
5525edd16368SStephen M. Cameron 	if (err)
5526edd16368SStephen M. Cameron 		return -EFAULT;
5527edd16368SStephen M. Cameron 
552842a91641SDon Brace 	err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, p);
5529edd16368SStephen M. Cameron 	if (err)
5530edd16368SStephen M. Cameron 		return err;
5531edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
5532edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
5533edd16368SStephen M. Cameron 	if (err)
5534edd16368SStephen M. Cameron 		return -EFAULT;
5535edd16368SStephen M. Cameron 	return err;
5536edd16368SStephen M. Cameron }
553771fe75a7SStephen M. Cameron 
553842a91641SDon Brace static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
553971fe75a7SStephen M. Cameron {
554071fe75a7SStephen M. Cameron 	switch (cmd) {
554171fe75a7SStephen M. Cameron 	case CCISS_GETPCIINFO:
554271fe75a7SStephen M. Cameron 	case CCISS_GETINTINFO:
554371fe75a7SStephen M. Cameron 	case CCISS_SETINTINFO:
554471fe75a7SStephen M. Cameron 	case CCISS_GETNODENAME:
554571fe75a7SStephen M. Cameron 	case CCISS_SETNODENAME:
554671fe75a7SStephen M. Cameron 	case CCISS_GETHEARTBEAT:
554771fe75a7SStephen M. Cameron 	case CCISS_GETBUSTYPES:
554871fe75a7SStephen M. Cameron 	case CCISS_GETFIRMVER:
554971fe75a7SStephen M. Cameron 	case CCISS_GETDRIVVER:
555071fe75a7SStephen M. Cameron 	case CCISS_REVALIDVOLS:
555171fe75a7SStephen M. Cameron 	case CCISS_DEREGDISK:
555271fe75a7SStephen M. Cameron 	case CCISS_REGNEWDISK:
555371fe75a7SStephen M. Cameron 	case CCISS_REGNEWD:
555471fe75a7SStephen M. Cameron 	case CCISS_RESCANDISK:
555571fe75a7SStephen M. Cameron 	case CCISS_GETLUNINFO:
555671fe75a7SStephen M. Cameron 		return hpsa_ioctl(dev, cmd, arg);
555771fe75a7SStephen M. Cameron 
555871fe75a7SStephen M. Cameron 	case CCISS_PASSTHRU32:
555971fe75a7SStephen M. Cameron 		return hpsa_ioctl32_passthru(dev, cmd, arg);
556071fe75a7SStephen M. Cameron 	case CCISS_BIG_PASSTHRU32:
556171fe75a7SStephen M. Cameron 		return hpsa_ioctl32_big_passthru(dev, cmd, arg);
556271fe75a7SStephen M. Cameron 
556371fe75a7SStephen M. Cameron 	default:
556471fe75a7SStephen M. Cameron 		return -ENOIOCTLCMD;
556571fe75a7SStephen M. Cameron 	}
556671fe75a7SStephen M. Cameron }
5567edd16368SStephen M. Cameron #endif
5568edd16368SStephen M. Cameron 
5569edd16368SStephen M. Cameron static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
5570edd16368SStephen M. Cameron {
5571edd16368SStephen M. Cameron 	struct hpsa_pci_info pciinfo;
5572edd16368SStephen M. Cameron 
5573edd16368SStephen M. Cameron 	if (!argp)
5574edd16368SStephen M. Cameron 		return -EINVAL;
5575edd16368SStephen M. Cameron 	pciinfo.domain = pci_domain_nr(h->pdev->bus);
5576edd16368SStephen M. Cameron 	pciinfo.bus = h->pdev->bus->number;
5577edd16368SStephen M. Cameron 	pciinfo.dev_fn = h->pdev->devfn;
5578edd16368SStephen M. Cameron 	pciinfo.board_id = h->board_id;
5579edd16368SStephen M. Cameron 	if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
5580edd16368SStephen M. Cameron 		return -EFAULT;
5581edd16368SStephen M. Cameron 	return 0;
5582edd16368SStephen M. Cameron }
5583edd16368SStephen M. Cameron 
5584edd16368SStephen M. Cameron static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
5585edd16368SStephen M. Cameron {
5586edd16368SStephen M. Cameron 	DriverVer_type DriverVer;
5587edd16368SStephen M. Cameron 	unsigned char vmaj, vmin, vsubmin;
5588edd16368SStephen M. Cameron 	int rc;
5589edd16368SStephen M. Cameron 
5590edd16368SStephen M. Cameron 	rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
5591edd16368SStephen M. Cameron 		&vmaj, &vmin, &vsubmin);
5592edd16368SStephen M. Cameron 	if (rc != 3) {
5593edd16368SStephen M. Cameron 		dev_info(&h->pdev->dev, "driver version string '%s' "
5594edd16368SStephen M. Cameron 			"unrecognized.", HPSA_DRIVER_VERSION);
5595edd16368SStephen M. Cameron 		vmaj = 0;
5596edd16368SStephen M. Cameron 		vmin = 0;
5597edd16368SStephen M. Cameron 		vsubmin = 0;
5598edd16368SStephen M. Cameron 	}
5599edd16368SStephen M. Cameron 	DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
5600edd16368SStephen M. Cameron 	if (!argp)
5601edd16368SStephen M. Cameron 		return -EINVAL;
5602edd16368SStephen M. Cameron 	if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
5603edd16368SStephen M. Cameron 		return -EFAULT;
5604edd16368SStephen M. Cameron 	return 0;
5605edd16368SStephen M. Cameron }
5606edd16368SStephen M. Cameron 
5607edd16368SStephen M. Cameron static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
5608edd16368SStephen M. Cameron {
5609edd16368SStephen M. Cameron 	IOCTL_Command_struct iocommand;
5610edd16368SStephen M. Cameron 	struct CommandList *c;
5611edd16368SStephen M. Cameron 	char *buff = NULL;
561250a0decfSStephen M. Cameron 	u64 temp64;
5613c1f63c8fSStephen M. Cameron 	int rc = 0;
5614edd16368SStephen M. Cameron 
5615edd16368SStephen M. Cameron 	if (!argp)
5616edd16368SStephen M. Cameron 		return -EINVAL;
5617edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
5618edd16368SStephen M. Cameron 		return -EPERM;
5619edd16368SStephen M. Cameron 	if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
5620edd16368SStephen M. Cameron 		return -EFAULT;
5621edd16368SStephen M. Cameron 	if ((iocommand.buf_size < 1) &&
5622edd16368SStephen M. Cameron 	    (iocommand.Request.Type.Direction != XFER_NONE)) {
5623edd16368SStephen M. Cameron 		return -EINVAL;
5624edd16368SStephen M. Cameron 	}
5625edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
5626edd16368SStephen M. Cameron 		buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
5627edd16368SStephen M. Cameron 		if (buff == NULL)
56282dd02d74SRobert Elliott 			return -ENOMEM;
56299233fb10SStephen M. Cameron 		if (iocommand.Request.Type.Direction & XFER_WRITE) {
5630edd16368SStephen M. Cameron 			/* Copy the data into the buffer we created */
5631b03a7771SStephen M. Cameron 			if (copy_from_user(buff, iocommand.buf,
5632b03a7771SStephen M. Cameron 				iocommand.buf_size)) {
5633c1f63c8fSStephen M. Cameron 				rc = -EFAULT;
5634c1f63c8fSStephen M. Cameron 				goto out_kfree;
5635edd16368SStephen M. Cameron 			}
5636b03a7771SStephen M. Cameron 		} else {
5637edd16368SStephen M. Cameron 			memset(buff, 0, iocommand.buf_size);
5638b03a7771SStephen M. Cameron 		}
5639b03a7771SStephen M. Cameron 	}
564045fcb86eSStephen Cameron 	c = cmd_alloc(h);
5641bf43caf3SRobert Elliott 
5642edd16368SStephen M. Cameron 	/* Fill in the command type */
5643edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5644a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_BUSY;
5645edd16368SStephen M. Cameron 	/* Fill in Command Header */
5646edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0; /* unused in simple mode */
5647edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {	/* buffer to fill */
5648edd16368SStephen M. Cameron 		c->Header.SGList = 1;
564950a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(1);
5650edd16368SStephen M. Cameron 	} else	{ /* no buffers to fill */
5651edd16368SStephen M. Cameron 		c->Header.SGList = 0;
565250a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(0);
5653edd16368SStephen M. Cameron 	}
5654edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
5655edd16368SStephen M. Cameron 
5656edd16368SStephen M. Cameron 	/* Fill in Request block */
5657edd16368SStephen M. Cameron 	memcpy(&c->Request, &iocommand.Request,
5658edd16368SStephen M. Cameron 		sizeof(c->Request));
5659edd16368SStephen M. Cameron 
5660edd16368SStephen M. Cameron 	/* Fill in the scatter gather information */
5661edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
566250a0decfSStephen M. Cameron 		temp64 = pci_map_single(h->pdev, buff,
5663edd16368SStephen M. Cameron 			iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
566450a0decfSStephen M. Cameron 		if (dma_mapping_error(&h->pdev->dev, (dma_addr_t) temp64)) {
566550a0decfSStephen M. Cameron 			c->SG[0].Addr = cpu_to_le64(0);
566650a0decfSStephen M. Cameron 			c->SG[0].Len = cpu_to_le32(0);
5667bcc48ffaSStephen M. Cameron 			rc = -ENOMEM;
5668bcc48ffaSStephen M. Cameron 			goto out;
5669bcc48ffaSStephen M. Cameron 		}
567050a0decfSStephen M. Cameron 		c->SG[0].Addr = cpu_to_le64(temp64);
567150a0decfSStephen M. Cameron 		c->SG[0].Len = cpu_to_le32(iocommand.buf_size);
567250a0decfSStephen M. Cameron 		c->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* not chaining */
5673edd16368SStephen M. Cameron 	}
567425163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
5675c2dd32e0SStephen M. Cameron 	if (iocommand.buf_size > 0)
5676edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
5677edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
567825163bd5SWebb Scales 	if (rc) {
567925163bd5SWebb Scales 		rc = -EIO;
568025163bd5SWebb Scales 		goto out;
568125163bd5SWebb Scales 	}
5682edd16368SStephen M. Cameron 
5683edd16368SStephen M. Cameron 	/* Copy the error information out */
5684edd16368SStephen M. Cameron 	memcpy(&iocommand.error_info, c->err_info,
5685edd16368SStephen M. Cameron 		sizeof(iocommand.error_info));
5686edd16368SStephen M. Cameron 	if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
5687c1f63c8fSStephen M. Cameron 		rc = -EFAULT;
5688c1f63c8fSStephen M. Cameron 		goto out;
5689edd16368SStephen M. Cameron 	}
56909233fb10SStephen M. Cameron 	if ((iocommand.Request.Type.Direction & XFER_READ) &&
5691b03a7771SStephen M. Cameron 		iocommand.buf_size > 0) {
5692edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
5693edd16368SStephen M. Cameron 		if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
5694c1f63c8fSStephen M. Cameron 			rc = -EFAULT;
5695c1f63c8fSStephen M. Cameron 			goto out;
5696edd16368SStephen M. Cameron 		}
5697edd16368SStephen M. Cameron 	}
5698c1f63c8fSStephen M. Cameron out:
569945fcb86eSStephen Cameron 	cmd_free(h, c);
5700c1f63c8fSStephen M. Cameron out_kfree:
5701c1f63c8fSStephen M. Cameron 	kfree(buff);
5702c1f63c8fSStephen M. Cameron 	return rc;
5703edd16368SStephen M. Cameron }
5704edd16368SStephen M. Cameron 
5705edd16368SStephen M. Cameron static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
5706edd16368SStephen M. Cameron {
5707edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct *ioc;
5708edd16368SStephen M. Cameron 	struct CommandList *c;
5709edd16368SStephen M. Cameron 	unsigned char **buff = NULL;
5710edd16368SStephen M. Cameron 	int *buff_size = NULL;
571150a0decfSStephen M. Cameron 	u64 temp64;
5712edd16368SStephen M. Cameron 	BYTE sg_used = 0;
5713edd16368SStephen M. Cameron 	int status = 0;
571401a02ffcSStephen M. Cameron 	u32 left;
571501a02ffcSStephen M. Cameron 	u32 sz;
5716edd16368SStephen M. Cameron 	BYTE __user *data_ptr;
5717edd16368SStephen M. Cameron 
5718edd16368SStephen M. Cameron 	if (!argp)
5719edd16368SStephen M. Cameron 		return -EINVAL;
5720edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
5721edd16368SStephen M. Cameron 		return -EPERM;
5722edd16368SStephen M. Cameron 	ioc = (BIG_IOCTL_Command_struct *)
5723edd16368SStephen M. Cameron 	    kmalloc(sizeof(*ioc), GFP_KERNEL);
5724edd16368SStephen M. Cameron 	if (!ioc) {
5725edd16368SStephen M. Cameron 		status = -ENOMEM;
5726edd16368SStephen M. Cameron 		goto cleanup1;
5727edd16368SStephen M. Cameron 	}
5728edd16368SStephen M. Cameron 	if (copy_from_user(ioc, argp, sizeof(*ioc))) {
5729edd16368SStephen M. Cameron 		status = -EFAULT;
5730edd16368SStephen M. Cameron 		goto cleanup1;
5731edd16368SStephen M. Cameron 	}
5732edd16368SStephen M. Cameron 	if ((ioc->buf_size < 1) &&
5733edd16368SStephen M. Cameron 	    (ioc->Request.Type.Direction != XFER_NONE)) {
5734edd16368SStephen M. Cameron 		status = -EINVAL;
5735edd16368SStephen M. Cameron 		goto cleanup1;
5736edd16368SStephen M. Cameron 	}
5737edd16368SStephen M. Cameron 	/* Check kmalloc limits  using all SGs */
5738edd16368SStephen M. Cameron 	if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
5739edd16368SStephen M. Cameron 		status = -EINVAL;
5740edd16368SStephen M. Cameron 		goto cleanup1;
5741edd16368SStephen M. Cameron 	}
5742d66ae08bSStephen M. Cameron 	if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
5743edd16368SStephen M. Cameron 		status = -EINVAL;
5744edd16368SStephen M. Cameron 		goto cleanup1;
5745edd16368SStephen M. Cameron 	}
5746d66ae08bSStephen M. Cameron 	buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
5747edd16368SStephen M. Cameron 	if (!buff) {
5748edd16368SStephen M. Cameron 		status = -ENOMEM;
5749edd16368SStephen M. Cameron 		goto cleanup1;
5750edd16368SStephen M. Cameron 	}
5751d66ae08bSStephen M. Cameron 	buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
5752edd16368SStephen M. Cameron 	if (!buff_size) {
5753edd16368SStephen M. Cameron 		status = -ENOMEM;
5754edd16368SStephen M. Cameron 		goto cleanup1;
5755edd16368SStephen M. Cameron 	}
5756edd16368SStephen M. Cameron 	left = ioc->buf_size;
5757edd16368SStephen M. Cameron 	data_ptr = ioc->buf;
5758edd16368SStephen M. Cameron 	while (left) {
5759edd16368SStephen M. Cameron 		sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
5760edd16368SStephen M. Cameron 		buff_size[sg_used] = sz;
5761edd16368SStephen M. Cameron 		buff[sg_used] = kmalloc(sz, GFP_KERNEL);
5762edd16368SStephen M. Cameron 		if (buff[sg_used] == NULL) {
5763edd16368SStephen M. Cameron 			status = -ENOMEM;
5764edd16368SStephen M. Cameron 			goto cleanup1;
5765edd16368SStephen M. Cameron 		}
57669233fb10SStephen M. Cameron 		if (ioc->Request.Type.Direction & XFER_WRITE) {
5767edd16368SStephen M. Cameron 			if (copy_from_user(buff[sg_used], data_ptr, sz)) {
57680758f4f7SStephen M. Cameron 				status = -EFAULT;
5769edd16368SStephen M. Cameron 				goto cleanup1;
5770edd16368SStephen M. Cameron 			}
5771edd16368SStephen M. Cameron 		} else
5772edd16368SStephen M. Cameron 			memset(buff[sg_used], 0, sz);
5773edd16368SStephen M. Cameron 		left -= sz;
5774edd16368SStephen M. Cameron 		data_ptr += sz;
5775edd16368SStephen M. Cameron 		sg_used++;
5776edd16368SStephen M. Cameron 	}
577745fcb86eSStephen Cameron 	c = cmd_alloc(h);
5778bf43caf3SRobert Elliott 
5779edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5780a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_BUSY;
5781edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
578250a0decfSStephen M. Cameron 	c->Header.SGList = (u8) sg_used;
578350a0decfSStephen M. Cameron 	c->Header.SGTotal = cpu_to_le16(sg_used);
5784edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
5785edd16368SStephen M. Cameron 	memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
5786edd16368SStephen M. Cameron 	if (ioc->buf_size > 0) {
5787edd16368SStephen M. Cameron 		int i;
5788edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
578950a0decfSStephen M. Cameron 			temp64 = pci_map_single(h->pdev, buff[i],
5790edd16368SStephen M. Cameron 				    buff_size[i], PCI_DMA_BIDIRECTIONAL);
579150a0decfSStephen M. Cameron 			if (dma_mapping_error(&h->pdev->dev,
579250a0decfSStephen M. Cameron 							(dma_addr_t) temp64)) {
579350a0decfSStephen M. Cameron 				c->SG[i].Addr = cpu_to_le64(0);
579450a0decfSStephen M. Cameron 				c->SG[i].Len = cpu_to_le32(0);
5795bcc48ffaSStephen M. Cameron 				hpsa_pci_unmap(h->pdev, c, i,
5796bcc48ffaSStephen M. Cameron 					PCI_DMA_BIDIRECTIONAL);
5797bcc48ffaSStephen M. Cameron 				status = -ENOMEM;
5798e2d4a1f6SStephen M. Cameron 				goto cleanup0;
5799bcc48ffaSStephen M. Cameron 			}
580050a0decfSStephen M. Cameron 			c->SG[i].Addr = cpu_to_le64(temp64);
580150a0decfSStephen M. Cameron 			c->SG[i].Len = cpu_to_le32(buff_size[i]);
580250a0decfSStephen M. Cameron 			c->SG[i].Ext = cpu_to_le32(0);
5803edd16368SStephen M. Cameron 		}
580450a0decfSStephen M. Cameron 		c->SG[--i].Ext = cpu_to_le32(HPSA_SG_LAST);
5805edd16368SStephen M. Cameron 	}
580625163bd5SWebb Scales 	status = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
5807b03a7771SStephen M. Cameron 	if (sg_used)
5808edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
5809edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
581025163bd5SWebb Scales 	if (status) {
581125163bd5SWebb Scales 		status = -EIO;
581225163bd5SWebb Scales 		goto cleanup0;
581325163bd5SWebb Scales 	}
581425163bd5SWebb Scales 
5815edd16368SStephen M. Cameron 	/* Copy the error information out */
5816edd16368SStephen M. Cameron 	memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
5817edd16368SStephen M. Cameron 	if (copy_to_user(argp, ioc, sizeof(*ioc))) {
5818edd16368SStephen M. Cameron 		status = -EFAULT;
5819e2d4a1f6SStephen M. Cameron 		goto cleanup0;
5820edd16368SStephen M. Cameron 	}
58219233fb10SStephen M. Cameron 	if ((ioc->Request.Type.Direction & XFER_READ) && ioc->buf_size > 0) {
58222b08b3e9SDon Brace 		int i;
58232b08b3e9SDon Brace 
5824edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
5825edd16368SStephen M. Cameron 		BYTE __user *ptr = ioc->buf;
5826edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
5827edd16368SStephen M. Cameron 			if (copy_to_user(ptr, buff[i], buff_size[i])) {
5828edd16368SStephen M. Cameron 				status = -EFAULT;
5829e2d4a1f6SStephen M. Cameron 				goto cleanup0;
5830edd16368SStephen M. Cameron 			}
5831edd16368SStephen M. Cameron 			ptr += buff_size[i];
5832edd16368SStephen M. Cameron 		}
5833edd16368SStephen M. Cameron 	}
5834edd16368SStephen M. Cameron 	status = 0;
5835e2d4a1f6SStephen M. Cameron cleanup0:
583645fcb86eSStephen Cameron 	cmd_free(h, c);
5837edd16368SStephen M. Cameron cleanup1:
5838edd16368SStephen M. Cameron 	if (buff) {
58392b08b3e9SDon Brace 		int i;
58402b08b3e9SDon Brace 
5841edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++)
5842edd16368SStephen M. Cameron 			kfree(buff[i]);
5843edd16368SStephen M. Cameron 		kfree(buff);
5844edd16368SStephen M. Cameron 	}
5845edd16368SStephen M. Cameron 	kfree(buff_size);
5846edd16368SStephen M. Cameron 	kfree(ioc);
5847edd16368SStephen M. Cameron 	return status;
5848edd16368SStephen M. Cameron }
5849edd16368SStephen M. Cameron 
5850edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
5851edd16368SStephen M. Cameron 	struct CommandList *c)
5852edd16368SStephen M. Cameron {
5853edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
5854edd16368SStephen M. Cameron 			c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
5855edd16368SStephen M. Cameron 		(void) check_for_unit_attention(h, c);
5856edd16368SStephen M. Cameron }
58570390f0c0SStephen M. Cameron 
5858edd16368SStephen M. Cameron /*
5859edd16368SStephen M. Cameron  * ioctl
5860edd16368SStephen M. Cameron  */
586142a91641SDon Brace static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
5862edd16368SStephen M. Cameron {
5863edd16368SStephen M. Cameron 	struct ctlr_info *h;
5864edd16368SStephen M. Cameron 	void __user *argp = (void __user *)arg;
58650390f0c0SStephen M. Cameron 	int rc;
5866edd16368SStephen M. Cameron 
5867edd16368SStephen M. Cameron 	h = sdev_to_hba(dev);
5868edd16368SStephen M. Cameron 
5869edd16368SStephen M. Cameron 	switch (cmd) {
5870edd16368SStephen M. Cameron 	case CCISS_DEREGDISK:
5871edd16368SStephen M. Cameron 	case CCISS_REGNEWDISK:
5872edd16368SStephen M. Cameron 	case CCISS_REGNEWD:
5873a08a8471SStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
5874edd16368SStephen M. Cameron 		return 0;
5875edd16368SStephen M. Cameron 	case CCISS_GETPCIINFO:
5876edd16368SStephen M. Cameron 		return hpsa_getpciinfo_ioctl(h, argp);
5877edd16368SStephen M. Cameron 	case CCISS_GETDRIVVER:
5878edd16368SStephen M. Cameron 		return hpsa_getdrivver_ioctl(h, argp);
5879edd16368SStephen M. Cameron 	case CCISS_PASSTHRU:
588034f0c627SDon Brace 		if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
58810390f0c0SStephen M. Cameron 			return -EAGAIN;
58820390f0c0SStephen M. Cameron 		rc = hpsa_passthru_ioctl(h, argp);
588334f0c627SDon Brace 		atomic_inc(&h->passthru_cmds_avail);
58840390f0c0SStephen M. Cameron 		return rc;
5885edd16368SStephen M. Cameron 	case CCISS_BIG_PASSTHRU:
588634f0c627SDon Brace 		if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
58870390f0c0SStephen M. Cameron 			return -EAGAIN;
58880390f0c0SStephen M. Cameron 		rc = hpsa_big_passthru_ioctl(h, argp);
588934f0c627SDon Brace 		atomic_inc(&h->passthru_cmds_avail);
58900390f0c0SStephen M. Cameron 		return rc;
5891edd16368SStephen M. Cameron 	default:
5892edd16368SStephen M. Cameron 		return -ENOTTY;
5893edd16368SStephen M. Cameron 	}
5894edd16368SStephen M. Cameron }
5895edd16368SStephen M. Cameron 
5896bf43caf3SRobert Elliott static void hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
58976f039790SGreg Kroah-Hartman 				u8 reset_type)
589864670ac8SStephen M. Cameron {
589964670ac8SStephen M. Cameron 	struct CommandList *c;
590064670ac8SStephen M. Cameron 
590164670ac8SStephen M. Cameron 	c = cmd_alloc(h);
5902bf43caf3SRobert Elliott 
5903a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map */
5904a2dac136SStephen M. Cameron 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
590564670ac8SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_MSG);
590664670ac8SStephen M. Cameron 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
590764670ac8SStephen M. Cameron 	c->waiting = NULL;
590864670ac8SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
590964670ac8SStephen M. Cameron 	/* Don't wait for completion, the reset won't complete.  Don't free
591064670ac8SStephen M. Cameron 	 * the command either.  This is the last command we will send before
591164670ac8SStephen M. Cameron 	 * re-initializing everything, so it doesn't matter and won't leak.
591264670ac8SStephen M. Cameron 	 */
5913bf43caf3SRobert Elliott 	return;
591464670ac8SStephen M. Cameron }
591564670ac8SStephen M. Cameron 
5916a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
5917b7bb24ebSStephen M. Cameron 	void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
5918edd16368SStephen M. Cameron 	int cmd_type)
5919edd16368SStephen M. Cameron {
5920edd16368SStephen M. Cameron 	int pci_dir = XFER_NONE;
59219b5c48c2SStephen Cameron 	u64 tag; /* for commands to be aborted */
5922edd16368SStephen M. Cameron 
5923edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5924a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_BUSY;
5925edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
5926edd16368SStephen M. Cameron 	if (buff != NULL && size > 0) {
5927edd16368SStephen M. Cameron 		c->Header.SGList = 1;
592850a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(1);
5929edd16368SStephen M. Cameron 	} else {
5930edd16368SStephen M. Cameron 		c->Header.SGList = 0;
593150a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(0);
5932edd16368SStephen M. Cameron 	}
5933edd16368SStephen M. Cameron 	memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
5934edd16368SStephen M. Cameron 
5935edd16368SStephen M. Cameron 	if (cmd_type == TYPE_CMD) {
5936edd16368SStephen M. Cameron 		switch (cmd) {
5937edd16368SStephen M. Cameron 		case HPSA_INQUIRY:
5938edd16368SStephen M. Cameron 			/* are we trying to read a vital product page */
5939b7bb24ebSStephen M. Cameron 			if (page_code & VPD_PAGE) {
5940edd16368SStephen M. Cameron 				c->Request.CDB[1] = 0x01;
5941b7bb24ebSStephen M. Cameron 				c->Request.CDB[2] = (page_code & 0xff);
5942edd16368SStephen M. Cameron 			}
5943edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
5944a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5945a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5946edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5947edd16368SStephen M. Cameron 			c->Request.CDB[0] = HPSA_INQUIRY;
5948edd16368SStephen M. Cameron 			c->Request.CDB[4] = size & 0xFF;
5949edd16368SStephen M. Cameron 			break;
5950edd16368SStephen M. Cameron 		case HPSA_REPORT_LOG:
5951edd16368SStephen M. Cameron 		case HPSA_REPORT_PHYS:
5952edd16368SStephen M. Cameron 			/* Talking to controller so It's a physical command
5953edd16368SStephen M. Cameron 			   mode = 00 target = 0.  Nothing to write.
5954edd16368SStephen M. Cameron 			 */
5955edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
5956a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5957a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5958edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5959edd16368SStephen M. Cameron 			c->Request.CDB[0] = cmd;
5960edd16368SStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
5961edd16368SStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5962edd16368SStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5963edd16368SStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
5964edd16368SStephen M. Cameron 			break;
5965edd16368SStephen M. Cameron 		case HPSA_CACHE_FLUSH:
5966edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
5967a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5968a505b86fSStephen M. Cameron 					TYPE_ATTR_DIR(cmd_type,
5969a505b86fSStephen M. Cameron 						ATTR_SIMPLE, XFER_WRITE);
5970edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5971edd16368SStephen M. Cameron 			c->Request.CDB[0] = BMIC_WRITE;
5972edd16368SStephen M. Cameron 			c->Request.CDB[6] = BMIC_CACHE_FLUSH;
5973bb158eabSStephen M. Cameron 			c->Request.CDB[7] = (size >> 8) & 0xFF;
5974bb158eabSStephen M. Cameron 			c->Request.CDB[8] = size & 0xFF;
5975edd16368SStephen M. Cameron 			break;
5976edd16368SStephen M. Cameron 		case TEST_UNIT_READY:
5977edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
5978a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5979a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
5980edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5981edd16368SStephen M. Cameron 			break;
5982283b4a9bSStephen M. Cameron 		case HPSA_GET_RAID_MAP:
5983283b4a9bSStephen M. Cameron 			c->Request.CDBLen = 12;
5984a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5985a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5986283b4a9bSStephen M. Cameron 			c->Request.Timeout = 0;
5987283b4a9bSStephen M. Cameron 			c->Request.CDB[0] = HPSA_CISS_READ;
5988283b4a9bSStephen M. Cameron 			c->Request.CDB[1] = cmd;
5989283b4a9bSStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
5990283b4a9bSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5991283b4a9bSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5992283b4a9bSStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
5993283b4a9bSStephen M. Cameron 			break;
5994316b221aSStephen M. Cameron 		case BMIC_SENSE_CONTROLLER_PARAMETERS:
5995316b221aSStephen M. Cameron 			c->Request.CDBLen = 10;
5996a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5997a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5998316b221aSStephen M. Cameron 			c->Request.Timeout = 0;
5999316b221aSStephen M. Cameron 			c->Request.CDB[0] = BMIC_READ;
6000316b221aSStephen M. Cameron 			c->Request.CDB[6] = BMIC_SENSE_CONTROLLER_PARAMETERS;
6001316b221aSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
6002316b221aSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
6003316b221aSStephen M. Cameron 			break;
600403383736SDon Brace 		case BMIC_IDENTIFY_PHYSICAL_DEVICE:
600503383736SDon Brace 			c->Request.CDBLen = 10;
600603383736SDon Brace 			c->Request.type_attr_dir =
600703383736SDon Brace 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
600803383736SDon Brace 			c->Request.Timeout = 0;
600903383736SDon Brace 			c->Request.CDB[0] = BMIC_READ;
601003383736SDon Brace 			c->Request.CDB[6] = BMIC_IDENTIFY_PHYSICAL_DEVICE;
601103383736SDon Brace 			c->Request.CDB[7] = (size >> 16) & 0xFF;
601203383736SDon Brace 			c->Request.CDB[8] = (size >> 8) & 0XFF;
601303383736SDon Brace 			break;
6014edd16368SStephen M. Cameron 		default:
6015edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
6016edd16368SStephen M. Cameron 			BUG();
6017a2dac136SStephen M. Cameron 			return -1;
6018edd16368SStephen M. Cameron 		}
6019edd16368SStephen M. Cameron 	} else if (cmd_type == TYPE_MSG) {
6020edd16368SStephen M. Cameron 		switch (cmd) {
6021edd16368SStephen M. Cameron 
6022edd16368SStephen M. Cameron 		case  HPSA_DEVICE_RESET_MSG:
6023edd16368SStephen M. Cameron 			c->Request.CDBLen = 16;
6024a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6025a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
6026edd16368SStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
602764670ac8SStephen M. Cameron 			memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
602864670ac8SStephen M. Cameron 			c->Request.CDB[0] =  cmd;
602921e89afdSStephen M. Cameron 			c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
6030edd16368SStephen M. Cameron 			/* If bytes 4-7 are zero, it means reset the */
6031edd16368SStephen M. Cameron 			/* LunID device */
6032edd16368SStephen M. Cameron 			c->Request.CDB[4] = 0x00;
6033edd16368SStephen M. Cameron 			c->Request.CDB[5] = 0x00;
6034edd16368SStephen M. Cameron 			c->Request.CDB[6] = 0x00;
6035edd16368SStephen M. Cameron 			c->Request.CDB[7] = 0x00;
6036edd16368SStephen M. Cameron 			break;
603775167d2cSStephen M. Cameron 		case  HPSA_ABORT_MSG:
60389b5c48c2SStephen Cameron 			memcpy(&tag, buff, sizeof(tag));
60392b08b3e9SDon Brace 			dev_dbg(&h->pdev->dev,
60409b5c48c2SStephen Cameron 				"Abort Tag:0x%016llx using rqst Tag:0x%016llx",
60419b5c48c2SStephen Cameron 				tag, c->Header.tag);
604275167d2cSStephen M. Cameron 			c->Request.CDBLen = 16;
6043a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6044a505b86fSStephen M. Cameron 					TYPE_ATTR_DIR(cmd_type,
6045a505b86fSStephen M. Cameron 						ATTR_SIMPLE, XFER_WRITE);
604675167d2cSStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
604775167d2cSStephen M. Cameron 			c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
604875167d2cSStephen M. Cameron 			c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
604975167d2cSStephen M. Cameron 			c->Request.CDB[2] = 0x00; /* reserved */
605075167d2cSStephen M. Cameron 			c->Request.CDB[3] = 0x00; /* reserved */
605175167d2cSStephen M. Cameron 			/* Tag to abort goes in CDB[4]-CDB[11] */
60529b5c48c2SStephen Cameron 			memcpy(&c->Request.CDB[4], &tag, sizeof(tag));
605375167d2cSStephen M. Cameron 			c->Request.CDB[12] = 0x00; /* reserved */
605475167d2cSStephen M. Cameron 			c->Request.CDB[13] = 0x00; /* reserved */
605575167d2cSStephen M. Cameron 			c->Request.CDB[14] = 0x00; /* reserved */
605675167d2cSStephen M. Cameron 			c->Request.CDB[15] = 0x00; /* reserved */
605775167d2cSStephen M. Cameron 		break;
6058edd16368SStephen M. Cameron 		default:
6059edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown message type %d\n",
6060edd16368SStephen M. Cameron 				cmd);
6061edd16368SStephen M. Cameron 			BUG();
6062edd16368SStephen M. Cameron 		}
6063edd16368SStephen M. Cameron 	} else {
6064edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
6065edd16368SStephen M. Cameron 		BUG();
6066edd16368SStephen M. Cameron 	}
6067edd16368SStephen M. Cameron 
6068a505b86fSStephen M. Cameron 	switch (GET_DIR(c->Request.type_attr_dir)) {
6069edd16368SStephen M. Cameron 	case XFER_READ:
6070edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_FROMDEVICE;
6071edd16368SStephen M. Cameron 		break;
6072edd16368SStephen M. Cameron 	case XFER_WRITE:
6073edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_TODEVICE;
6074edd16368SStephen M. Cameron 		break;
6075edd16368SStephen M. Cameron 	case XFER_NONE:
6076edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_NONE;
6077edd16368SStephen M. Cameron 		break;
6078edd16368SStephen M. Cameron 	default:
6079edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_BIDIRECTIONAL;
6080edd16368SStephen M. Cameron 	}
6081a2dac136SStephen M. Cameron 	if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
6082a2dac136SStephen M. Cameron 		return -1;
6083a2dac136SStephen M. Cameron 	return 0;
6084edd16368SStephen M. Cameron }
6085edd16368SStephen M. Cameron 
6086edd16368SStephen M. Cameron /*
6087edd16368SStephen M. Cameron  * Map (physical) PCI mem into (virtual) kernel space
6088edd16368SStephen M. Cameron  */
6089edd16368SStephen M. Cameron static void __iomem *remap_pci_mem(ulong base, ulong size)
6090edd16368SStephen M. Cameron {
6091edd16368SStephen M. Cameron 	ulong page_base = ((ulong) base) & PAGE_MASK;
6092edd16368SStephen M. Cameron 	ulong page_offs = ((ulong) base) - page_base;
6093088ba34cSStephen M. Cameron 	void __iomem *page_remapped = ioremap_nocache(page_base,
6094088ba34cSStephen M. Cameron 		page_offs + size);
6095edd16368SStephen M. Cameron 
6096edd16368SStephen M. Cameron 	return page_remapped ? (page_remapped + page_offs) : NULL;
6097edd16368SStephen M. Cameron }
6098edd16368SStephen M. Cameron 
6099254f796bSMatt Gates static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
6100edd16368SStephen M. Cameron {
6101254f796bSMatt Gates 	return h->access.command_completed(h, q);
6102edd16368SStephen M. Cameron }
6103edd16368SStephen M. Cameron 
6104900c5440SStephen M. Cameron static inline bool interrupt_pending(struct ctlr_info *h)
6105edd16368SStephen M. Cameron {
6106edd16368SStephen M. Cameron 	return h->access.intr_pending(h);
6107edd16368SStephen M. Cameron }
6108edd16368SStephen M. Cameron 
6109edd16368SStephen M. Cameron static inline long interrupt_not_for_us(struct ctlr_info *h)
6110edd16368SStephen M. Cameron {
611110f66018SStephen M. Cameron 	return (h->access.intr_pending(h) == 0) ||
611210f66018SStephen M. Cameron 		(h->interrupts_enabled == 0);
6113edd16368SStephen M. Cameron }
6114edd16368SStephen M. Cameron 
611501a02ffcSStephen M. Cameron static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
611601a02ffcSStephen M. Cameron 	u32 raw_tag)
6117edd16368SStephen M. Cameron {
6118edd16368SStephen M. Cameron 	if (unlikely(tag_index >= h->nr_cmds)) {
6119edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
6120edd16368SStephen M. Cameron 		return 1;
6121edd16368SStephen M. Cameron 	}
6122edd16368SStephen M. Cameron 	return 0;
6123edd16368SStephen M. Cameron }
6124edd16368SStephen M. Cameron 
61255a3d16f5SStephen M. Cameron static inline void finish_cmd(struct CommandList *c)
6126edd16368SStephen M. Cameron {
6127e85c5974SStephen M. Cameron 	dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
6128c349775eSScott Teel 	if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
6129c349775eSScott Teel 			|| c->cmd_type == CMD_IOACCEL2))
61301fb011fbSStephen M. Cameron 		complete_scsi_command(c);
61318be986ccSStephen Cameron 	else if (c->cmd_type == CMD_IOCTL_PEND || c->cmd_type == IOACCEL2_TMF)
6132edd16368SStephen M. Cameron 		complete(c->waiting);
6133a104c99fSStephen M. Cameron }
6134a104c99fSStephen M. Cameron 
6135a9a3a273SStephen M. Cameron 
6136a9a3a273SStephen M. Cameron static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
6137a104c99fSStephen M. Cameron {
6138a9a3a273SStephen M. Cameron #define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
6139a9a3a273SStephen M. Cameron #define HPSA_SIMPLE_ERROR_BITS 0x03
6140960a30e7SStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
6141a9a3a273SStephen M. Cameron 		return tag & ~HPSA_SIMPLE_ERROR_BITS;
6142a9a3a273SStephen M. Cameron 	return tag & ~HPSA_PERF_ERROR_BITS;
6143a104c99fSStephen M. Cameron }
6144a104c99fSStephen M. Cameron 
6145303932fdSDon Brace /* process completion of an indexed ("direct lookup") command */
61461d94f94dSStephen M. Cameron static inline void process_indexed_cmd(struct ctlr_info *h,
6147303932fdSDon Brace 	u32 raw_tag)
6148303932fdSDon Brace {
6149303932fdSDon Brace 	u32 tag_index;
6150303932fdSDon Brace 	struct CommandList *c;
6151303932fdSDon Brace 
6152f2405db8SDon Brace 	tag_index = raw_tag >> DIRECT_LOOKUP_SHIFT;
61531d94f94dSStephen M. Cameron 	if (!bad_tag(h, tag_index, raw_tag)) {
6154303932fdSDon Brace 		c = h->cmd_pool + tag_index;
61555a3d16f5SStephen M. Cameron 		finish_cmd(c);
61561d94f94dSStephen M. Cameron 	}
6157303932fdSDon Brace }
6158303932fdSDon Brace 
615964670ac8SStephen M. Cameron /* Some controllers, like p400, will give us one interrupt
616064670ac8SStephen M. Cameron  * after a soft reset, even if we turned interrupts off.
616164670ac8SStephen M. Cameron  * Only need to check for this in the hpsa_xxx_discard_completions
616264670ac8SStephen M. Cameron  * functions.
616364670ac8SStephen M. Cameron  */
616464670ac8SStephen M. Cameron static int ignore_bogus_interrupt(struct ctlr_info *h)
616564670ac8SStephen M. Cameron {
616664670ac8SStephen M. Cameron 	if (likely(!reset_devices))
616764670ac8SStephen M. Cameron 		return 0;
616864670ac8SStephen M. Cameron 
616964670ac8SStephen M. Cameron 	if (likely(h->interrupts_enabled))
617064670ac8SStephen M. Cameron 		return 0;
617164670ac8SStephen M. Cameron 
617264670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
617364670ac8SStephen M. Cameron 		"(known firmware bug.)  Ignoring.\n");
617464670ac8SStephen M. Cameron 
617564670ac8SStephen M. Cameron 	return 1;
617664670ac8SStephen M. Cameron }
617764670ac8SStephen M. Cameron 
6178254f796bSMatt Gates /*
6179254f796bSMatt Gates  * Convert &h->q[x] (passed to interrupt handlers) back to h.
6180254f796bSMatt Gates  * Relies on (h-q[x] == x) being true for x such that
6181254f796bSMatt Gates  * 0 <= x < MAX_REPLY_QUEUES.
6182254f796bSMatt Gates  */
6183254f796bSMatt Gates static struct ctlr_info *queue_to_hba(u8 *queue)
618464670ac8SStephen M. Cameron {
6185254f796bSMatt Gates 	return container_of((queue - *queue), struct ctlr_info, q[0]);
6186254f796bSMatt Gates }
6187254f796bSMatt Gates 
6188254f796bSMatt Gates static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
6189254f796bSMatt Gates {
6190254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
6191254f796bSMatt Gates 	u8 q = *(u8 *) queue;
619264670ac8SStephen M. Cameron 	u32 raw_tag;
619364670ac8SStephen M. Cameron 
619464670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
619564670ac8SStephen M. Cameron 		return IRQ_NONE;
619664670ac8SStephen M. Cameron 
619764670ac8SStephen M. Cameron 	if (interrupt_not_for_us(h))
619864670ac8SStephen M. Cameron 		return IRQ_NONE;
6199a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
620064670ac8SStephen M. Cameron 	while (interrupt_pending(h)) {
6201254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
620264670ac8SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY)
6203254f796bSMatt Gates 			raw_tag = next_command(h, q);
620464670ac8SStephen M. Cameron 	}
620564670ac8SStephen M. Cameron 	return IRQ_HANDLED;
620664670ac8SStephen M. Cameron }
620764670ac8SStephen M. Cameron 
6208254f796bSMatt Gates static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
620964670ac8SStephen M. Cameron {
6210254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
621164670ac8SStephen M. Cameron 	u32 raw_tag;
6212254f796bSMatt Gates 	u8 q = *(u8 *) queue;
621364670ac8SStephen M. Cameron 
621464670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
621564670ac8SStephen M. Cameron 		return IRQ_NONE;
621664670ac8SStephen M. Cameron 
6217a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
6218254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
621964670ac8SStephen M. Cameron 	while (raw_tag != FIFO_EMPTY)
6220254f796bSMatt Gates 		raw_tag = next_command(h, q);
622164670ac8SStephen M. Cameron 	return IRQ_HANDLED;
622264670ac8SStephen M. Cameron }
622364670ac8SStephen M. Cameron 
6224254f796bSMatt Gates static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
6225edd16368SStephen M. Cameron {
6226254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba((u8 *) queue);
6227303932fdSDon Brace 	u32 raw_tag;
6228254f796bSMatt Gates 	u8 q = *(u8 *) queue;
6229edd16368SStephen M. Cameron 
6230edd16368SStephen M. Cameron 	if (interrupt_not_for_us(h))
6231edd16368SStephen M. Cameron 		return IRQ_NONE;
6232a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
623310f66018SStephen M. Cameron 	while (interrupt_pending(h)) {
6234254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
623510f66018SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY) {
62361d94f94dSStephen M. Cameron 			process_indexed_cmd(h, raw_tag);
6237254f796bSMatt Gates 			raw_tag = next_command(h, q);
623810f66018SStephen M. Cameron 		}
623910f66018SStephen M. Cameron 	}
624010f66018SStephen M. Cameron 	return IRQ_HANDLED;
624110f66018SStephen M. Cameron }
624210f66018SStephen M. Cameron 
6243254f796bSMatt Gates static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
624410f66018SStephen M. Cameron {
6245254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
624610f66018SStephen M. Cameron 	u32 raw_tag;
6247254f796bSMatt Gates 	u8 q = *(u8 *) queue;
624810f66018SStephen M. Cameron 
6249a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
6250254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
6251303932fdSDon Brace 	while (raw_tag != FIFO_EMPTY) {
62521d94f94dSStephen M. Cameron 		process_indexed_cmd(h, raw_tag);
6253254f796bSMatt Gates 		raw_tag = next_command(h, q);
6254edd16368SStephen M. Cameron 	}
6255edd16368SStephen M. Cameron 	return IRQ_HANDLED;
6256edd16368SStephen M. Cameron }
6257edd16368SStephen M. Cameron 
6258a9a3a273SStephen M. Cameron /* Send a message CDB to the firmware. Careful, this only works
6259a9a3a273SStephen M. Cameron  * in simple mode, not performant mode due to the tag lookup.
6260a9a3a273SStephen M. Cameron  * We only ever use this immediately after a controller reset.
6261a9a3a273SStephen M. Cameron  */
62626f039790SGreg Kroah-Hartman static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
6263edd16368SStephen M. Cameron 			unsigned char type)
6264edd16368SStephen M. Cameron {
6265edd16368SStephen M. Cameron 	struct Command {
6266edd16368SStephen M. Cameron 		struct CommandListHeader CommandHeader;
6267edd16368SStephen M. Cameron 		struct RequestBlock Request;
6268edd16368SStephen M. Cameron 		struct ErrDescriptor ErrorDescriptor;
6269edd16368SStephen M. Cameron 	};
6270edd16368SStephen M. Cameron 	struct Command *cmd;
6271edd16368SStephen M. Cameron 	static const size_t cmd_sz = sizeof(*cmd) +
6272edd16368SStephen M. Cameron 					sizeof(cmd->ErrorDescriptor);
6273edd16368SStephen M. Cameron 	dma_addr_t paddr64;
62742b08b3e9SDon Brace 	__le32 paddr32;
62752b08b3e9SDon Brace 	u32 tag;
6276edd16368SStephen M. Cameron 	void __iomem *vaddr;
6277edd16368SStephen M. Cameron 	int i, err;
6278edd16368SStephen M. Cameron 
6279edd16368SStephen M. Cameron 	vaddr = pci_ioremap_bar(pdev, 0);
6280edd16368SStephen M. Cameron 	if (vaddr == NULL)
6281edd16368SStephen M. Cameron 		return -ENOMEM;
6282edd16368SStephen M. Cameron 
6283edd16368SStephen M. Cameron 	/* The Inbound Post Queue only accepts 32-bit physical addresses for the
6284edd16368SStephen M. Cameron 	 * CCISS commands, so they must be allocated from the lower 4GiB of
6285edd16368SStephen M. Cameron 	 * memory.
6286edd16368SStephen M. Cameron 	 */
6287edd16368SStephen M. Cameron 	err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
6288edd16368SStephen M. Cameron 	if (err) {
6289edd16368SStephen M. Cameron 		iounmap(vaddr);
62901eaec8f3SRobert Elliott 		return err;
6291edd16368SStephen M. Cameron 	}
6292edd16368SStephen M. Cameron 
6293edd16368SStephen M. Cameron 	cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
6294edd16368SStephen M. Cameron 	if (cmd == NULL) {
6295edd16368SStephen M. Cameron 		iounmap(vaddr);
6296edd16368SStephen M. Cameron 		return -ENOMEM;
6297edd16368SStephen M. Cameron 	}
6298edd16368SStephen M. Cameron 
6299edd16368SStephen M. Cameron 	/* This must fit, because of the 32-bit consistent DMA mask.  Also,
6300edd16368SStephen M. Cameron 	 * although there's no guarantee, we assume that the address is at
6301edd16368SStephen M. Cameron 	 * least 4-byte aligned (most likely, it's page-aligned).
6302edd16368SStephen M. Cameron 	 */
63032b08b3e9SDon Brace 	paddr32 = cpu_to_le32(paddr64);
6304edd16368SStephen M. Cameron 
6305edd16368SStephen M. Cameron 	cmd->CommandHeader.ReplyQueue = 0;
6306edd16368SStephen M. Cameron 	cmd->CommandHeader.SGList = 0;
630750a0decfSStephen M. Cameron 	cmd->CommandHeader.SGTotal = cpu_to_le16(0);
63082b08b3e9SDon Brace 	cmd->CommandHeader.tag = cpu_to_le64(paddr64);
6309edd16368SStephen M. Cameron 	memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
6310edd16368SStephen M. Cameron 
6311edd16368SStephen M. Cameron 	cmd->Request.CDBLen = 16;
6312a505b86fSStephen M. Cameron 	cmd->Request.type_attr_dir =
6313a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_MSG, ATTR_HEADOFQUEUE, XFER_NONE);
6314edd16368SStephen M. Cameron 	cmd->Request.Timeout = 0; /* Don't time out */
6315edd16368SStephen M. Cameron 	cmd->Request.CDB[0] = opcode;
6316edd16368SStephen M. Cameron 	cmd->Request.CDB[1] = type;
6317edd16368SStephen M. Cameron 	memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
631850a0decfSStephen M. Cameron 	cmd->ErrorDescriptor.Addr =
63192b08b3e9SDon Brace 			cpu_to_le64((le32_to_cpu(paddr32) + sizeof(*cmd)));
632050a0decfSStephen M. Cameron 	cmd->ErrorDescriptor.Len = cpu_to_le32(sizeof(struct ErrorInfo));
6321edd16368SStephen M. Cameron 
63222b08b3e9SDon Brace 	writel(le32_to_cpu(paddr32), vaddr + SA5_REQUEST_PORT_OFFSET);
6323edd16368SStephen M. Cameron 
6324edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
6325edd16368SStephen M. Cameron 		tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
63262b08b3e9SDon Brace 		if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr64)
6327edd16368SStephen M. Cameron 			break;
6328edd16368SStephen M. Cameron 		msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
6329edd16368SStephen M. Cameron 	}
6330edd16368SStephen M. Cameron 
6331edd16368SStephen M. Cameron 	iounmap(vaddr);
6332edd16368SStephen M. Cameron 
6333edd16368SStephen M. Cameron 	/* we leak the DMA buffer here ... no choice since the controller could
6334edd16368SStephen M. Cameron 	 *  still complete the command.
6335edd16368SStephen M. Cameron 	 */
6336edd16368SStephen M. Cameron 	if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
6337edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
6338edd16368SStephen M. Cameron 			opcode, type);
6339edd16368SStephen M. Cameron 		return -ETIMEDOUT;
6340edd16368SStephen M. Cameron 	}
6341edd16368SStephen M. Cameron 
6342edd16368SStephen M. Cameron 	pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
6343edd16368SStephen M. Cameron 
6344edd16368SStephen M. Cameron 	if (tag & HPSA_ERROR_BIT) {
6345edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
6346edd16368SStephen M. Cameron 			opcode, type);
6347edd16368SStephen M. Cameron 		return -EIO;
6348edd16368SStephen M. Cameron 	}
6349edd16368SStephen M. Cameron 
6350edd16368SStephen M. Cameron 	dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
6351edd16368SStephen M. Cameron 		opcode, type);
6352edd16368SStephen M. Cameron 	return 0;
6353edd16368SStephen M. Cameron }
6354edd16368SStephen M. Cameron 
6355edd16368SStephen M. Cameron #define hpsa_noop(p) hpsa_message(p, 3, 0)
6356edd16368SStephen M. Cameron 
63571df8552aSStephen M. Cameron static int hpsa_controller_hard_reset(struct pci_dev *pdev,
635842a91641SDon Brace 	void __iomem *vaddr, u32 use_doorbell)
6359edd16368SStephen M. Cameron {
6360edd16368SStephen M. Cameron 
63611df8552aSStephen M. Cameron 	if (use_doorbell) {
63621df8552aSStephen M. Cameron 		/* For everything after the P600, the PCI power state method
63631df8552aSStephen M. Cameron 		 * of resetting the controller doesn't work, so we have this
63641df8552aSStephen M. Cameron 		 * other way using the doorbell register.
6365edd16368SStephen M. Cameron 		 */
63661df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using doorbell to reset controller\n");
6367cf0b08d0SStephen M. Cameron 		writel(use_doorbell, vaddr + SA5_DOORBELL);
636885009239SStephen M. Cameron 
636900701a96SJustin Lindley 		/* PMC hardware guys tell us we need a 10 second delay after
637085009239SStephen M. Cameron 		 * doorbell reset and before any attempt to talk to the board
637185009239SStephen M. Cameron 		 * at all to ensure that this actually works and doesn't fall
637285009239SStephen M. Cameron 		 * over in some weird corner cases.
637385009239SStephen M. Cameron 		 */
637400701a96SJustin Lindley 		msleep(10000);
63751df8552aSStephen M. Cameron 	} else { /* Try to do it the PCI power state way */
6376edd16368SStephen M. Cameron 
6377edd16368SStephen M. Cameron 		/* Quoting from the Open CISS Specification: "The Power
6378edd16368SStephen M. Cameron 		 * Management Control/Status Register (CSR) controls the power
6379edd16368SStephen M. Cameron 		 * state of the device.  The normal operating state is D0,
6380edd16368SStephen M. Cameron 		 * CSR=00h.  The software off state is D3, CSR=03h.  To reset
63811df8552aSStephen M. Cameron 		 * the controller, place the interface device in D3 then to D0,
63821df8552aSStephen M. Cameron 		 * this causes a secondary PCI reset which will reset the
63831df8552aSStephen M. Cameron 		 * controller." */
6384edd16368SStephen M. Cameron 
63852662cab8SDon Brace 		int rc = 0;
63862662cab8SDon Brace 
63871df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using PCI PM to reset controller\n");
63882662cab8SDon Brace 
6389edd16368SStephen M. Cameron 		/* enter the D3hot power management state */
63902662cab8SDon Brace 		rc = pci_set_power_state(pdev, PCI_D3hot);
63912662cab8SDon Brace 		if (rc)
63922662cab8SDon Brace 			return rc;
6393edd16368SStephen M. Cameron 
6394edd16368SStephen M. Cameron 		msleep(500);
6395edd16368SStephen M. Cameron 
6396edd16368SStephen M. Cameron 		/* enter the D0 power management state */
63972662cab8SDon Brace 		rc = pci_set_power_state(pdev, PCI_D0);
63982662cab8SDon Brace 		if (rc)
63992662cab8SDon Brace 			return rc;
6400c4853efeSMike Miller 
6401c4853efeSMike Miller 		/*
6402c4853efeSMike Miller 		 * The P600 requires a small delay when changing states.
6403c4853efeSMike Miller 		 * Otherwise we may think the board did not reset and we bail.
6404c4853efeSMike Miller 		 * This for kdump only and is particular to the P600.
6405c4853efeSMike Miller 		 */
6406c4853efeSMike Miller 		msleep(500);
64071df8552aSStephen M. Cameron 	}
64081df8552aSStephen M. Cameron 	return 0;
64091df8552aSStephen M. Cameron }
64101df8552aSStephen M. Cameron 
64116f039790SGreg Kroah-Hartman static void init_driver_version(char *driver_version, int len)
6412580ada3cSStephen M. Cameron {
6413580ada3cSStephen M. Cameron 	memset(driver_version, 0, len);
6414f79cfec6SStephen M. Cameron 	strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
6415580ada3cSStephen M. Cameron }
6416580ada3cSStephen M. Cameron 
64176f039790SGreg Kroah-Hartman static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
6418580ada3cSStephen M. Cameron {
6419580ada3cSStephen M. Cameron 	char *driver_version;
6420580ada3cSStephen M. Cameron 	int i, size = sizeof(cfgtable->driver_version);
6421580ada3cSStephen M. Cameron 
6422580ada3cSStephen M. Cameron 	driver_version = kmalloc(size, GFP_KERNEL);
6423580ada3cSStephen M. Cameron 	if (!driver_version)
6424580ada3cSStephen M. Cameron 		return -ENOMEM;
6425580ada3cSStephen M. Cameron 
6426580ada3cSStephen M. Cameron 	init_driver_version(driver_version, size);
6427580ada3cSStephen M. Cameron 	for (i = 0; i < size; i++)
6428580ada3cSStephen M. Cameron 		writeb(driver_version[i], &cfgtable->driver_version[i]);
6429580ada3cSStephen M. Cameron 	kfree(driver_version);
6430580ada3cSStephen M. Cameron 	return 0;
6431580ada3cSStephen M. Cameron }
6432580ada3cSStephen M. Cameron 
64336f039790SGreg Kroah-Hartman static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
64346f039790SGreg Kroah-Hartman 					  unsigned char *driver_ver)
6435580ada3cSStephen M. Cameron {
6436580ada3cSStephen M. Cameron 	int i;
6437580ada3cSStephen M. Cameron 
6438580ada3cSStephen M. Cameron 	for (i = 0; i < sizeof(cfgtable->driver_version); i++)
6439580ada3cSStephen M. Cameron 		driver_ver[i] = readb(&cfgtable->driver_version[i]);
6440580ada3cSStephen M. Cameron }
6441580ada3cSStephen M. Cameron 
64426f039790SGreg Kroah-Hartman static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
6443580ada3cSStephen M. Cameron {
6444580ada3cSStephen M. Cameron 
6445580ada3cSStephen M. Cameron 	char *driver_ver, *old_driver_ver;
6446580ada3cSStephen M. Cameron 	int rc, size = sizeof(cfgtable->driver_version);
6447580ada3cSStephen M. Cameron 
6448580ada3cSStephen M. Cameron 	old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
6449580ada3cSStephen M. Cameron 	if (!old_driver_ver)
6450580ada3cSStephen M. Cameron 		return -ENOMEM;
6451580ada3cSStephen M. Cameron 	driver_ver = old_driver_ver + size;
6452580ada3cSStephen M. Cameron 
6453580ada3cSStephen M. Cameron 	/* After a reset, the 32 bytes of "driver version" in the cfgtable
6454580ada3cSStephen M. Cameron 	 * should have been changed, otherwise we know the reset failed.
6455580ada3cSStephen M. Cameron 	 */
6456580ada3cSStephen M. Cameron 	init_driver_version(old_driver_ver, size);
6457580ada3cSStephen M. Cameron 	read_driver_ver_from_cfgtable(cfgtable, driver_ver);
6458580ada3cSStephen M. Cameron 	rc = !memcmp(driver_ver, old_driver_ver, size);
6459580ada3cSStephen M. Cameron 	kfree(old_driver_ver);
6460580ada3cSStephen M. Cameron 	return rc;
6461580ada3cSStephen M. Cameron }
64621df8552aSStephen M. Cameron /* This does a hard reset of the controller using PCI power management
64631df8552aSStephen M. Cameron  * states or the using the doorbell register.
64641df8552aSStephen M. Cameron  */
64656b6c1cd7STomas Henzl static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev, u32 board_id)
64661df8552aSStephen M. Cameron {
64671df8552aSStephen M. Cameron 	u64 cfg_offset;
64681df8552aSStephen M. Cameron 	u32 cfg_base_addr;
64691df8552aSStephen M. Cameron 	u64 cfg_base_addr_index;
64701df8552aSStephen M. Cameron 	void __iomem *vaddr;
64711df8552aSStephen M. Cameron 	unsigned long paddr;
6472580ada3cSStephen M. Cameron 	u32 misc_fw_support;
6473270d05deSStephen M. Cameron 	int rc;
64741df8552aSStephen M. Cameron 	struct CfgTable __iomem *cfgtable;
6475cf0b08d0SStephen M. Cameron 	u32 use_doorbell;
6476270d05deSStephen M. Cameron 	u16 command_register;
64771df8552aSStephen M. Cameron 
64781df8552aSStephen M. Cameron 	/* For controllers as old as the P600, this is very nearly
64791df8552aSStephen M. Cameron 	 * the same thing as
64801df8552aSStephen M. Cameron 	 *
64811df8552aSStephen M. Cameron 	 * pci_save_state(pci_dev);
64821df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D3hot);
64831df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D0);
64841df8552aSStephen M. Cameron 	 * pci_restore_state(pci_dev);
64851df8552aSStephen M. Cameron 	 *
64861df8552aSStephen M. Cameron 	 * For controllers newer than the P600, the pci power state
64871df8552aSStephen M. Cameron 	 * method of resetting doesn't work so we have another way
64881df8552aSStephen M. Cameron 	 * using the doorbell register.
64891df8552aSStephen M. Cameron 	 */
649018867659SStephen M. Cameron 
649160f923b9SRobert Elliott 	if (!ctlr_is_resettable(board_id)) {
649260f923b9SRobert Elliott 		dev_warn(&pdev->dev, "Controller not resettable\n");
649325c1e56aSStephen M. Cameron 		return -ENODEV;
649425c1e56aSStephen M. Cameron 	}
649546380786SStephen M. Cameron 
649646380786SStephen M. Cameron 	/* if controller is soft- but not hard resettable... */
649746380786SStephen M. Cameron 	if (!ctlr_is_hard_resettable(board_id))
649846380786SStephen M. Cameron 		return -ENOTSUPP; /* try soft reset later. */
649918867659SStephen M. Cameron 
6500270d05deSStephen M. Cameron 	/* Save the PCI command register */
6501270d05deSStephen M. Cameron 	pci_read_config_word(pdev, 4, &command_register);
6502270d05deSStephen M. Cameron 	pci_save_state(pdev);
65031df8552aSStephen M. Cameron 
65041df8552aSStephen M. Cameron 	/* find the first memory BAR, so we can find the cfg table */
65051df8552aSStephen M. Cameron 	rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
65061df8552aSStephen M. Cameron 	if (rc)
65071df8552aSStephen M. Cameron 		return rc;
65081df8552aSStephen M. Cameron 	vaddr = remap_pci_mem(paddr, 0x250);
65091df8552aSStephen M. Cameron 	if (!vaddr)
65101df8552aSStephen M. Cameron 		return -ENOMEM;
65111df8552aSStephen M. Cameron 
65121df8552aSStephen M. Cameron 	/* find cfgtable in order to check if reset via doorbell is supported */
65131df8552aSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
65141df8552aSStephen M. Cameron 					&cfg_base_addr_index, &cfg_offset);
65151df8552aSStephen M. Cameron 	if (rc)
65161df8552aSStephen M. Cameron 		goto unmap_vaddr;
65171df8552aSStephen M. Cameron 	cfgtable = remap_pci_mem(pci_resource_start(pdev,
65181df8552aSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
65191df8552aSStephen M. Cameron 	if (!cfgtable) {
65201df8552aSStephen M. Cameron 		rc = -ENOMEM;
65211df8552aSStephen M. Cameron 		goto unmap_vaddr;
65221df8552aSStephen M. Cameron 	}
6523580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(cfgtable);
6524580ada3cSStephen M. Cameron 	if (rc)
652503741d95STomas Henzl 		goto unmap_cfgtable;
65261df8552aSStephen M. Cameron 
6527cf0b08d0SStephen M. Cameron 	/* If reset via doorbell register is supported, use that.
6528cf0b08d0SStephen M. Cameron 	 * There are two such methods.  Favor the newest method.
6529cf0b08d0SStephen M. Cameron 	 */
65301df8552aSStephen M. Cameron 	misc_fw_support = readl(&cfgtable->misc_fw_support);
6531cf0b08d0SStephen M. Cameron 	use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
6532cf0b08d0SStephen M. Cameron 	if (use_doorbell) {
6533cf0b08d0SStephen M. Cameron 		use_doorbell = DOORBELL_CTLR_RESET2;
6534cf0b08d0SStephen M. Cameron 	} else {
65351df8552aSStephen M. Cameron 		use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
6536cf0b08d0SStephen M. Cameron 		if (use_doorbell) {
6537050f7147SStephen Cameron 			dev_warn(&pdev->dev,
6538050f7147SStephen Cameron 				"Soft reset not supported. Firmware update is required.\n");
653964670ac8SStephen M. Cameron 			rc = -ENOTSUPP; /* try soft reset */
6540cf0b08d0SStephen M. Cameron 			goto unmap_cfgtable;
6541cf0b08d0SStephen M. Cameron 		}
6542cf0b08d0SStephen M. Cameron 	}
65431df8552aSStephen M. Cameron 
65441df8552aSStephen M. Cameron 	rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
65451df8552aSStephen M. Cameron 	if (rc)
65461df8552aSStephen M. Cameron 		goto unmap_cfgtable;
6547edd16368SStephen M. Cameron 
6548270d05deSStephen M. Cameron 	pci_restore_state(pdev);
6549270d05deSStephen M. Cameron 	pci_write_config_word(pdev, 4, command_register);
6550edd16368SStephen M. Cameron 
65511df8552aSStephen M. Cameron 	/* Some devices (notably the HP Smart Array 5i Controller)
65521df8552aSStephen M. Cameron 	   need a little pause here */
65531df8552aSStephen M. Cameron 	msleep(HPSA_POST_RESET_PAUSE_MSECS);
65541df8552aSStephen M. Cameron 
6555fe5389c8SStephen M. Cameron 	rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
6556fe5389c8SStephen M. Cameron 	if (rc) {
6557fe5389c8SStephen M. Cameron 		dev_warn(&pdev->dev,
6558050f7147SStephen Cameron 			"Failed waiting for board to become ready after hard reset\n");
6559fe5389c8SStephen M. Cameron 		goto unmap_cfgtable;
6560fe5389c8SStephen M. Cameron 	}
6561fe5389c8SStephen M. Cameron 
6562580ada3cSStephen M. Cameron 	rc = controller_reset_failed(vaddr);
6563580ada3cSStephen M. Cameron 	if (rc < 0)
6564580ada3cSStephen M. Cameron 		goto unmap_cfgtable;
6565580ada3cSStephen M. Cameron 	if (rc) {
656664670ac8SStephen M. Cameron 		dev_warn(&pdev->dev, "Unable to successfully reset "
656764670ac8SStephen M. Cameron 			"controller. Will try soft reset.\n");
656864670ac8SStephen M. Cameron 		rc = -ENOTSUPP;
6569580ada3cSStephen M. Cameron 	} else {
657064670ac8SStephen M. Cameron 		dev_info(&pdev->dev, "board ready after hard reset.\n");
65711df8552aSStephen M. Cameron 	}
65721df8552aSStephen M. Cameron 
65731df8552aSStephen M. Cameron unmap_cfgtable:
65741df8552aSStephen M. Cameron 	iounmap(cfgtable);
65751df8552aSStephen M. Cameron 
65761df8552aSStephen M. Cameron unmap_vaddr:
65771df8552aSStephen M. Cameron 	iounmap(vaddr);
65781df8552aSStephen M. Cameron 	return rc;
6579edd16368SStephen M. Cameron }
6580edd16368SStephen M. Cameron 
6581edd16368SStephen M. Cameron /*
6582edd16368SStephen M. Cameron  *  We cannot read the structure directly, for portability we must use
6583edd16368SStephen M. Cameron  *   the io functions.
6584edd16368SStephen M. Cameron  *   This is for debug only.
6585edd16368SStephen M. Cameron  */
658642a91641SDon Brace static void print_cfg_table(struct device *dev, struct CfgTable __iomem *tb)
6587edd16368SStephen M. Cameron {
658858f8665cSStephen M. Cameron #ifdef HPSA_DEBUG
6589edd16368SStephen M. Cameron 	int i;
6590edd16368SStephen M. Cameron 	char temp_name[17];
6591edd16368SStephen M. Cameron 
6592edd16368SStephen M. Cameron 	dev_info(dev, "Controller Configuration information\n");
6593edd16368SStephen M. Cameron 	dev_info(dev, "------------------------------------\n");
6594edd16368SStephen M. Cameron 	for (i = 0; i < 4; i++)
6595edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->Signature[i]));
6596edd16368SStephen M. Cameron 	temp_name[4] = '\0';
6597edd16368SStephen M. Cameron 	dev_info(dev, "   Signature = %s\n", temp_name);
6598edd16368SStephen M. Cameron 	dev_info(dev, "   Spec Number = %d\n", readl(&(tb->SpecValence)));
6599edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods supported = 0x%x\n",
6600edd16368SStephen M. Cameron 	       readl(&(tb->TransportSupport)));
6601edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods active = 0x%x\n",
6602edd16368SStephen M. Cameron 	       readl(&(tb->TransportActive)));
6603edd16368SStephen M. Cameron 	dev_info(dev, "   Requested transport Method = 0x%x\n",
6604edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.TransportRequest)));
6605edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Delay = 0x%x\n",
6606edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntDelay)));
6607edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Count = 0x%x\n",
6608edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntCount)));
660969d6e33dSRobert Elliott 	dev_info(dev, "   Max outstanding commands = %d\n",
6610edd16368SStephen M. Cameron 	       readl(&(tb->CmdsOutMax)));
6611edd16368SStephen M. Cameron 	dev_info(dev, "   Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
6612edd16368SStephen M. Cameron 	for (i = 0; i < 16; i++)
6613edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->ServerName[i]));
6614edd16368SStephen M. Cameron 	temp_name[16] = '\0';
6615edd16368SStephen M. Cameron 	dev_info(dev, "   Server Name = %s\n", temp_name);
6616edd16368SStephen M. Cameron 	dev_info(dev, "   Heartbeat Counter = 0x%x\n\n\n",
6617edd16368SStephen M. Cameron 		readl(&(tb->HeartBeat)));
6618edd16368SStephen M. Cameron #endif				/* HPSA_DEBUG */
661958f8665cSStephen M. Cameron }
6620edd16368SStephen M. Cameron 
6621edd16368SStephen M. Cameron static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
6622edd16368SStephen M. Cameron {
6623edd16368SStephen M. Cameron 	int i, offset, mem_type, bar_type;
6624edd16368SStephen M. Cameron 
6625edd16368SStephen M. Cameron 	if (pci_bar_addr == PCI_BASE_ADDRESS_0)	/* looking for BAR zero? */
6626edd16368SStephen M. Cameron 		return 0;
6627edd16368SStephen M. Cameron 	offset = 0;
6628edd16368SStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
6629edd16368SStephen M. Cameron 		bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
6630edd16368SStephen M. Cameron 		if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
6631edd16368SStephen M. Cameron 			offset += 4;
6632edd16368SStephen M. Cameron 		else {
6633edd16368SStephen M. Cameron 			mem_type = pci_resource_flags(pdev, i) &
6634edd16368SStephen M. Cameron 			    PCI_BASE_ADDRESS_MEM_TYPE_MASK;
6635edd16368SStephen M. Cameron 			switch (mem_type) {
6636edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_32:
6637edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_1M:
6638edd16368SStephen M. Cameron 				offset += 4;	/* 32 bit */
6639edd16368SStephen M. Cameron 				break;
6640edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_64:
6641edd16368SStephen M. Cameron 				offset += 8;
6642edd16368SStephen M. Cameron 				break;
6643edd16368SStephen M. Cameron 			default:	/* reserved in PCI 2.2 */
6644edd16368SStephen M. Cameron 				dev_warn(&pdev->dev,
6645edd16368SStephen M. Cameron 				       "base address is invalid\n");
6646edd16368SStephen M. Cameron 				return -1;
6647edd16368SStephen M. Cameron 				break;
6648edd16368SStephen M. Cameron 			}
6649edd16368SStephen M. Cameron 		}
6650edd16368SStephen M. Cameron 		if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
6651edd16368SStephen M. Cameron 			return i + 1;
6652edd16368SStephen M. Cameron 	}
6653edd16368SStephen M. Cameron 	return -1;
6654edd16368SStephen M. Cameron }
6655edd16368SStephen M. Cameron 
6656cc64c817SRobert Elliott static void hpsa_disable_interrupt_mode(struct ctlr_info *h)
6657cc64c817SRobert Elliott {
6658cc64c817SRobert Elliott 	if (h->msix_vector) {
6659cc64c817SRobert Elliott 		if (h->pdev->msix_enabled)
6660cc64c817SRobert Elliott 			pci_disable_msix(h->pdev);
6661105a3dbcSRobert Elliott 		h->msix_vector = 0;
6662cc64c817SRobert Elliott 	} else if (h->msi_vector) {
6663cc64c817SRobert Elliott 		if (h->pdev->msi_enabled)
6664cc64c817SRobert Elliott 			pci_disable_msi(h->pdev);
6665105a3dbcSRobert Elliott 		h->msi_vector = 0;
6666cc64c817SRobert Elliott 	}
6667cc64c817SRobert Elliott }
6668cc64c817SRobert Elliott 
6669edd16368SStephen M. Cameron /* If MSI/MSI-X is supported by the kernel we will try to enable it on
6670050f7147SStephen Cameron  * controllers that are capable. If not, we use legacy INTx mode.
6671edd16368SStephen M. Cameron  */
66726f039790SGreg Kroah-Hartman static void hpsa_interrupt_mode(struct ctlr_info *h)
6673edd16368SStephen M. Cameron {
6674edd16368SStephen M. Cameron #ifdef CONFIG_PCI_MSI
6675254f796bSMatt Gates 	int err, i;
6676254f796bSMatt Gates 	struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
6677254f796bSMatt Gates 
6678254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++) {
6679254f796bSMatt Gates 		hpsa_msix_entries[i].vector = 0;
6680254f796bSMatt Gates 		hpsa_msix_entries[i].entry = i;
6681254f796bSMatt Gates 	}
6682edd16368SStephen M. Cameron 
6683edd16368SStephen M. Cameron 	/* Some boards advertise MSI but don't really support it */
66846b3f4c52SStephen M. Cameron 	if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
66856b3f4c52SStephen M. Cameron 	    (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
6686edd16368SStephen M. Cameron 		goto default_int_mode;
668755c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
6688050f7147SStephen Cameron 		dev_info(&h->pdev->dev, "MSI-X capable controller\n");
6689eee0f03aSHannes Reinecke 		h->msix_vector = MAX_REPLY_QUEUES;
6690f89439bcSStephen M. Cameron 		if (h->msix_vector > num_online_cpus())
6691f89439bcSStephen M. Cameron 			h->msix_vector = num_online_cpus();
669218fce3c4SAlexander Gordeev 		err = pci_enable_msix_range(h->pdev, hpsa_msix_entries,
669318fce3c4SAlexander Gordeev 					    1, h->msix_vector);
669418fce3c4SAlexander Gordeev 		if (err < 0) {
669518fce3c4SAlexander Gordeev 			dev_warn(&h->pdev->dev, "MSI-X init failed %d\n", err);
669618fce3c4SAlexander Gordeev 			h->msix_vector = 0;
669718fce3c4SAlexander Gordeev 			goto single_msi_mode;
669818fce3c4SAlexander Gordeev 		} else if (err < h->msix_vector) {
669955c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
6700edd16368SStephen M. Cameron 			       "available\n", err);
6701eee0f03aSHannes Reinecke 		}
670218fce3c4SAlexander Gordeev 		h->msix_vector = err;
6703eee0f03aSHannes Reinecke 		for (i = 0; i < h->msix_vector; i++)
6704eee0f03aSHannes Reinecke 			h->intr[i] = hpsa_msix_entries[i].vector;
6705eee0f03aSHannes Reinecke 		return;
6706edd16368SStephen M. Cameron 	}
670718fce3c4SAlexander Gordeev single_msi_mode:
670855c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
6709050f7147SStephen Cameron 		dev_info(&h->pdev->dev, "MSI capable controller\n");
671055c06c71SStephen M. Cameron 		if (!pci_enable_msi(h->pdev))
6711edd16368SStephen M. Cameron 			h->msi_vector = 1;
6712edd16368SStephen M. Cameron 		else
671355c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "MSI init failed\n");
6714edd16368SStephen M. Cameron 	}
6715edd16368SStephen M. Cameron default_int_mode:
6716edd16368SStephen M. Cameron #endif				/* CONFIG_PCI_MSI */
6717edd16368SStephen M. Cameron 	/* if we get here we're going to use the default interrupt mode */
6718a9a3a273SStephen M. Cameron 	h->intr[h->intr_mode] = h->pdev->irq;
6719edd16368SStephen M. Cameron }
6720edd16368SStephen M. Cameron 
67216f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
6722e5c880d1SStephen M. Cameron {
6723e5c880d1SStephen M. Cameron 	int i;
6724e5c880d1SStephen M. Cameron 	u32 subsystem_vendor_id, subsystem_device_id;
6725e5c880d1SStephen M. Cameron 
6726e5c880d1SStephen M. Cameron 	subsystem_vendor_id = pdev->subsystem_vendor;
6727e5c880d1SStephen M. Cameron 	subsystem_device_id = pdev->subsystem_device;
6728e5c880d1SStephen M. Cameron 	*board_id = ((subsystem_device_id << 16) & 0xffff0000) |
6729e5c880d1SStephen M. Cameron 		    subsystem_vendor_id;
6730e5c880d1SStephen M. Cameron 
6731e5c880d1SStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(products); i++)
6732e5c880d1SStephen M. Cameron 		if (*board_id == products[i].board_id)
6733e5c880d1SStephen M. Cameron 			return i;
6734e5c880d1SStephen M. Cameron 
67356798cc0aSStephen M. Cameron 	if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
67366798cc0aSStephen M. Cameron 		subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
67376798cc0aSStephen M. Cameron 		!hpsa_allow_any) {
6738e5c880d1SStephen M. Cameron 		dev_warn(&pdev->dev, "unrecognized board ID: "
6739e5c880d1SStephen M. Cameron 			"0x%08x, ignoring.\n", *board_id);
6740e5c880d1SStephen M. Cameron 			return -ENODEV;
6741e5c880d1SStephen M. Cameron 	}
6742e5c880d1SStephen M. Cameron 	return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
6743e5c880d1SStephen M. Cameron }
6744e5c880d1SStephen M. Cameron 
67456f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
67463a7774ceSStephen M. Cameron 				    unsigned long *memory_bar)
67473a7774ceSStephen M. Cameron {
67483a7774ceSStephen M. Cameron 	int i;
67493a7774ceSStephen M. Cameron 
67503a7774ceSStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
675112d2cd47SStephen M. Cameron 		if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
67523a7774ceSStephen M. Cameron 			/* addressing mode bits already removed */
675312d2cd47SStephen M. Cameron 			*memory_bar = pci_resource_start(pdev, i);
675412d2cd47SStephen M. Cameron 			dev_dbg(&pdev->dev, "memory BAR = %lx\n",
67553a7774ceSStephen M. Cameron 				*memory_bar);
67563a7774ceSStephen M. Cameron 			return 0;
67573a7774ceSStephen M. Cameron 		}
675812d2cd47SStephen M. Cameron 	dev_warn(&pdev->dev, "no memory BAR found\n");
67593a7774ceSStephen M. Cameron 	return -ENODEV;
67603a7774ceSStephen M. Cameron }
67613a7774ceSStephen M. Cameron 
67626f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
67636f039790SGreg Kroah-Hartman 				     int wait_for_ready)
67642c4c8c8bSStephen M. Cameron {
6765fe5389c8SStephen M. Cameron 	int i, iterations;
67662c4c8c8bSStephen M. Cameron 	u32 scratchpad;
6767fe5389c8SStephen M. Cameron 	if (wait_for_ready)
6768fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_READY_ITERATIONS;
6769fe5389c8SStephen M. Cameron 	else
6770fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
67712c4c8c8bSStephen M. Cameron 
6772fe5389c8SStephen M. Cameron 	for (i = 0; i < iterations; i++) {
6773fe5389c8SStephen M. Cameron 		scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
6774fe5389c8SStephen M. Cameron 		if (wait_for_ready) {
67752c4c8c8bSStephen M. Cameron 			if (scratchpad == HPSA_FIRMWARE_READY)
67762c4c8c8bSStephen M. Cameron 				return 0;
6777fe5389c8SStephen M. Cameron 		} else {
6778fe5389c8SStephen M. Cameron 			if (scratchpad != HPSA_FIRMWARE_READY)
6779fe5389c8SStephen M. Cameron 				return 0;
6780fe5389c8SStephen M. Cameron 		}
67812c4c8c8bSStephen M. Cameron 		msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
67822c4c8c8bSStephen M. Cameron 	}
6783fe5389c8SStephen M. Cameron 	dev_warn(&pdev->dev, "board not ready, timed out.\n");
67842c4c8c8bSStephen M. Cameron 	return -ENODEV;
67852c4c8c8bSStephen M. Cameron }
67862c4c8c8bSStephen M. Cameron 
67876f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
67886f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
6789a51fd47fSStephen M. Cameron 			       u64 *cfg_offset)
6790a51fd47fSStephen M. Cameron {
6791a51fd47fSStephen M. Cameron 	*cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
6792a51fd47fSStephen M. Cameron 	*cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
6793a51fd47fSStephen M. Cameron 	*cfg_base_addr &= (u32) 0x0000ffff;
6794a51fd47fSStephen M. Cameron 	*cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
6795a51fd47fSStephen M. Cameron 	if (*cfg_base_addr_index == -1) {
6796a51fd47fSStephen M. Cameron 		dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
6797a51fd47fSStephen M. Cameron 		return -ENODEV;
6798a51fd47fSStephen M. Cameron 	}
6799a51fd47fSStephen M. Cameron 	return 0;
6800a51fd47fSStephen M. Cameron }
6801a51fd47fSStephen M. Cameron 
6802195f2c65SRobert Elliott static void hpsa_free_cfgtables(struct ctlr_info *h)
6803195f2c65SRobert Elliott {
6804105a3dbcSRobert Elliott 	if (h->transtable) {
6805195f2c65SRobert Elliott 		iounmap(h->transtable);
6806105a3dbcSRobert Elliott 		h->transtable = NULL;
6807105a3dbcSRobert Elliott 	}
6808105a3dbcSRobert Elliott 	if (h->cfgtable) {
6809195f2c65SRobert Elliott 		iounmap(h->cfgtable);
6810105a3dbcSRobert Elliott 		h->cfgtable = NULL;
6811105a3dbcSRobert Elliott 	}
6812195f2c65SRobert Elliott }
6813195f2c65SRobert Elliott 
6814195f2c65SRobert Elliott /* Find and map CISS config table and transfer table
6815195f2c65SRobert Elliott + * several items must be unmapped (freed) later
6816195f2c65SRobert Elliott + * */
68176f039790SGreg Kroah-Hartman static int hpsa_find_cfgtables(struct ctlr_info *h)
6818edd16368SStephen M. Cameron {
681901a02ffcSStephen M. Cameron 	u64 cfg_offset;
682001a02ffcSStephen M. Cameron 	u32 cfg_base_addr;
682101a02ffcSStephen M. Cameron 	u64 cfg_base_addr_index;
6822303932fdSDon Brace 	u32 trans_offset;
6823a51fd47fSStephen M. Cameron 	int rc;
682477c4495cSStephen M. Cameron 
6825a51fd47fSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
6826a51fd47fSStephen M. Cameron 		&cfg_base_addr_index, &cfg_offset);
6827a51fd47fSStephen M. Cameron 	if (rc)
6828a51fd47fSStephen M. Cameron 		return rc;
682977c4495cSStephen M. Cameron 	h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
6830a51fd47fSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
6831cd3c81c4SRobert Elliott 	if (!h->cfgtable) {
6832cd3c81c4SRobert Elliott 		dev_err(&h->pdev->dev, "Failed mapping cfgtable\n");
683377c4495cSStephen M. Cameron 		return -ENOMEM;
6834cd3c81c4SRobert Elliott 	}
6835580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(h->cfgtable);
6836580ada3cSStephen M. Cameron 	if (rc)
6837580ada3cSStephen M. Cameron 		return rc;
683877c4495cSStephen M. Cameron 	/* Find performant mode table. */
6839a51fd47fSStephen M. Cameron 	trans_offset = readl(&h->cfgtable->TransMethodOffset);
684077c4495cSStephen M. Cameron 	h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
684177c4495cSStephen M. Cameron 				cfg_base_addr_index)+cfg_offset+trans_offset,
684277c4495cSStephen M. Cameron 				sizeof(*h->transtable));
6843195f2c65SRobert Elliott 	if (!h->transtable) {
6844195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "Failed mapping transfer table\n");
6845195f2c65SRobert Elliott 		hpsa_free_cfgtables(h);
684677c4495cSStephen M. Cameron 		return -ENOMEM;
6847195f2c65SRobert Elliott 	}
684877c4495cSStephen M. Cameron 	return 0;
684977c4495cSStephen M. Cameron }
685077c4495cSStephen M. Cameron 
68516f039790SGreg Kroah-Hartman static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
6852cba3d38bSStephen M. Cameron {
685341ce4c35SStephen Cameron #define MIN_MAX_COMMANDS 16
685441ce4c35SStephen Cameron 	BUILD_BUG_ON(MIN_MAX_COMMANDS <= HPSA_NRESERVED_CMDS);
685541ce4c35SStephen Cameron 
685641ce4c35SStephen Cameron 	h->max_commands = readl(&h->cfgtable->MaxPerformantModeCommands);
685772ceeaecSStephen M. Cameron 
685872ceeaecSStephen M. Cameron 	/* Limit commands in memory limited kdump scenario. */
685972ceeaecSStephen M. Cameron 	if (reset_devices && h->max_commands > 32)
686072ceeaecSStephen M. Cameron 		h->max_commands = 32;
686172ceeaecSStephen M. Cameron 
686241ce4c35SStephen Cameron 	if (h->max_commands < MIN_MAX_COMMANDS) {
686341ce4c35SStephen Cameron 		dev_warn(&h->pdev->dev,
686441ce4c35SStephen Cameron 			"Controller reports max supported commands of %d Using %d instead. Ensure that firmware is up to date.\n",
686541ce4c35SStephen Cameron 			h->max_commands,
686641ce4c35SStephen Cameron 			MIN_MAX_COMMANDS);
686741ce4c35SStephen Cameron 		h->max_commands = MIN_MAX_COMMANDS;
6868cba3d38bSStephen M. Cameron 	}
6869cba3d38bSStephen M. Cameron }
6870cba3d38bSStephen M. Cameron 
6871c7ee65b3SWebb Scales /* If the controller reports that the total max sg entries is greater than 512,
6872c7ee65b3SWebb Scales  * then we know that chained SG blocks work.  (Original smart arrays did not
6873c7ee65b3SWebb Scales  * support chained SG blocks and would return zero for max sg entries.)
6874c7ee65b3SWebb Scales  */
6875c7ee65b3SWebb Scales static int hpsa_supports_chained_sg_blocks(struct ctlr_info *h)
6876c7ee65b3SWebb Scales {
6877c7ee65b3SWebb Scales 	return h->maxsgentries > 512;
6878c7ee65b3SWebb Scales }
6879c7ee65b3SWebb Scales 
6880b93d7536SStephen M. Cameron /* Interrogate the hardware for some limits:
6881b93d7536SStephen M. Cameron  * max commands, max SG elements without chaining, and with chaining,
6882b93d7536SStephen M. Cameron  * SG chain block size, etc.
6883b93d7536SStephen M. Cameron  */
68846f039790SGreg Kroah-Hartman static void hpsa_find_board_params(struct ctlr_info *h)
6885b93d7536SStephen M. Cameron {
6886cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
688745fcb86eSStephen Cameron 	h->nr_cmds = h->max_commands;
6888b93d7536SStephen M. Cameron 	h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
6889283b4a9bSStephen M. Cameron 	h->fw_support = readl(&(h->cfgtable->misc_fw_support));
6890c7ee65b3SWebb Scales 	if (hpsa_supports_chained_sg_blocks(h)) {
6891c7ee65b3SWebb Scales 		/* Limit in-command s/g elements to 32 save dma'able memory. */
6892b93d7536SStephen M. Cameron 		h->max_cmd_sg_entries = 32;
68931a63ea6fSWebb Scales 		h->chainsize = h->maxsgentries - h->max_cmd_sg_entries;
6894b93d7536SStephen M. Cameron 		h->maxsgentries--; /* save one for chain pointer */
6895b93d7536SStephen M. Cameron 	} else {
6896c7ee65b3SWebb Scales 		/*
6897c7ee65b3SWebb Scales 		 * Original smart arrays supported at most 31 s/g entries
6898c7ee65b3SWebb Scales 		 * embedded inline in the command (trying to use more
6899c7ee65b3SWebb Scales 		 * would lock up the controller)
6900c7ee65b3SWebb Scales 		 */
6901c7ee65b3SWebb Scales 		h->max_cmd_sg_entries = 31;
69021a63ea6fSWebb Scales 		h->maxsgentries = 31; /* default to traditional values */
6903c7ee65b3SWebb Scales 		h->chainsize = 0;
6904b93d7536SStephen M. Cameron 	}
690575167d2cSStephen M. Cameron 
690675167d2cSStephen M. Cameron 	/* Find out what task management functions are supported and cache */
690775167d2cSStephen M. Cameron 	h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
69080e7a7fceSScott Teel 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
69090e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
69100e7a7fceSScott Teel 	if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
69110e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
69128be986ccSStephen Cameron 	if (!(HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags))
69138be986ccSStephen Cameron 		dev_warn(&h->pdev->dev, "HP SSD Smart Path aborts not supported\n");
6914b93d7536SStephen M. Cameron }
6915b93d7536SStephen M. Cameron 
691676c46e49SStephen M. Cameron static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
691776c46e49SStephen M. Cameron {
69180fc9fd40SAkinobu Mita 	if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
6919050f7147SStephen Cameron 		dev_err(&h->pdev->dev, "not a valid CISS config table\n");
692076c46e49SStephen M. Cameron 		return false;
692176c46e49SStephen M. Cameron 	}
692276c46e49SStephen M. Cameron 	return true;
692376c46e49SStephen M. Cameron }
692476c46e49SStephen M. Cameron 
692597a5e98cSStephen M. Cameron static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
6926f7c39101SStephen M. Cameron {
692797a5e98cSStephen M. Cameron 	u32 driver_support;
6928f7c39101SStephen M. Cameron 
692997a5e98cSStephen M. Cameron 	driver_support = readl(&(h->cfgtable->driver_support));
69300b9e7b74SArnd Bergmann 	/* Need to enable prefetch in the SCSI core for 6400 in x86 */
69310b9e7b74SArnd Bergmann #ifdef CONFIG_X86
693297a5e98cSStephen M. Cameron 	driver_support |= ENABLE_SCSI_PREFETCH;
6933f7c39101SStephen M. Cameron #endif
693428e13446SStephen M. Cameron 	driver_support |= ENABLE_UNIT_ATTN;
693528e13446SStephen M. Cameron 	writel(driver_support, &(h->cfgtable->driver_support));
6936f7c39101SStephen M. Cameron }
6937f7c39101SStephen M. Cameron 
69383d0eab67SStephen M. Cameron /* Disable DMA prefetch for the P600.  Otherwise an ASIC bug may result
69393d0eab67SStephen M. Cameron  * in a prefetch beyond physical memory.
69403d0eab67SStephen M. Cameron  */
69413d0eab67SStephen M. Cameron static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
69423d0eab67SStephen M. Cameron {
69433d0eab67SStephen M. Cameron 	u32 dma_prefetch;
69443d0eab67SStephen M. Cameron 
69453d0eab67SStephen M. Cameron 	if (h->board_id != 0x3225103C)
69463d0eab67SStephen M. Cameron 		return;
69473d0eab67SStephen M. Cameron 	dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
69483d0eab67SStephen M. Cameron 	dma_prefetch |= 0x8000;
69493d0eab67SStephen M. Cameron 	writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
69503d0eab67SStephen M. Cameron }
69513d0eab67SStephen M. Cameron 
6952c706a795SRobert Elliott static int hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
695376438d08SStephen M. Cameron {
695476438d08SStephen M. Cameron 	int i;
695576438d08SStephen M. Cameron 	u32 doorbell_value;
695676438d08SStephen M. Cameron 	unsigned long flags;
695776438d08SStephen M. Cameron 	/* wait until the clear_event_notify bit 6 is cleared by controller. */
6958007e7aa9SRobert Elliott 	for (i = 0; i < MAX_CLEAR_EVENT_WAIT; i++) {
695976438d08SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
696076438d08SStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
696176438d08SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
696276438d08SStephen M. Cameron 		if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
6963c706a795SRobert Elliott 			goto done;
696476438d08SStephen M. Cameron 		/* delay and try again */
6965007e7aa9SRobert Elliott 		msleep(CLEAR_EVENT_WAIT_INTERVAL);
696676438d08SStephen M. Cameron 	}
6967c706a795SRobert Elliott 	return -ENODEV;
6968c706a795SRobert Elliott done:
6969c706a795SRobert Elliott 	return 0;
697076438d08SStephen M. Cameron }
697176438d08SStephen M. Cameron 
6972c706a795SRobert Elliott static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
6973eb6b2ae9SStephen M. Cameron {
6974eb6b2ae9SStephen M. Cameron 	int i;
69756eaf46fdSStephen M. Cameron 	u32 doorbell_value;
69766eaf46fdSStephen M. Cameron 	unsigned long flags;
6977eb6b2ae9SStephen M. Cameron 
6978eb6b2ae9SStephen M. Cameron 	/* under certain very rare conditions, this can take awhile.
6979eb6b2ae9SStephen M. Cameron 	 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
6980eb6b2ae9SStephen M. Cameron 	 * as we enter this code.)
6981eb6b2ae9SStephen M. Cameron 	 */
6982007e7aa9SRobert Elliott 	for (i = 0; i < MAX_MODE_CHANGE_WAIT; i++) {
698325163bd5SWebb Scales 		if (h->remove_in_progress)
698425163bd5SWebb Scales 			goto done;
69856eaf46fdSStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
69866eaf46fdSStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
69876eaf46fdSStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
6988382be668SDan Carpenter 		if (!(doorbell_value & CFGTBL_ChangeReq))
6989c706a795SRobert Elliott 			goto done;
6990eb6b2ae9SStephen M. Cameron 		/* delay and try again */
6991007e7aa9SRobert Elliott 		msleep(MODE_CHANGE_WAIT_INTERVAL);
6992eb6b2ae9SStephen M. Cameron 	}
6993c706a795SRobert Elliott 	return -ENODEV;
6994c706a795SRobert Elliott done:
6995c706a795SRobert Elliott 	return 0;
69963f4336f3SStephen M. Cameron }
69973f4336f3SStephen M. Cameron 
6998c706a795SRobert Elliott /* return -ENODEV or other reason on error, 0 on success */
69996f039790SGreg Kroah-Hartman static int hpsa_enter_simple_mode(struct ctlr_info *h)
70003f4336f3SStephen M. Cameron {
70013f4336f3SStephen M. Cameron 	u32 trans_support;
70023f4336f3SStephen M. Cameron 
70033f4336f3SStephen M. Cameron 	trans_support = readl(&(h->cfgtable->TransportSupport));
70043f4336f3SStephen M. Cameron 	if (!(trans_support & SIMPLE_MODE))
70053f4336f3SStephen M. Cameron 		return -ENOTSUPP;
70063f4336f3SStephen M. Cameron 
70073f4336f3SStephen M. Cameron 	h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
7008283b4a9bSStephen M. Cameron 
70093f4336f3SStephen M. Cameron 	/* Update the field, and then ring the doorbell */
70103f4336f3SStephen M. Cameron 	writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
7011b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
70123f4336f3SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
7013c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h))
7014c706a795SRobert Elliott 		goto error;
7015eb6b2ae9SStephen M. Cameron 	print_cfg_table(&h->pdev->dev, h->cfgtable);
7016283b4a9bSStephen M. Cameron 	if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
7017283b4a9bSStephen M. Cameron 		goto error;
7018960a30e7SStephen M. Cameron 	h->transMethod = CFGTBL_Trans_Simple;
7019eb6b2ae9SStephen M. Cameron 	return 0;
7020283b4a9bSStephen M. Cameron error:
7021050f7147SStephen Cameron 	dev_err(&h->pdev->dev, "failed to enter simple mode\n");
7022283b4a9bSStephen M. Cameron 	return -ENODEV;
7023eb6b2ae9SStephen M. Cameron }
7024eb6b2ae9SStephen M. Cameron 
7025195f2c65SRobert Elliott /* free items allocated or mapped by hpsa_pci_init */
7026195f2c65SRobert Elliott static void hpsa_free_pci_init(struct ctlr_info *h)
7027195f2c65SRobert Elliott {
7028195f2c65SRobert Elliott 	hpsa_free_cfgtables(h);			/* pci_init 4 */
7029195f2c65SRobert Elliott 	iounmap(h->vaddr);			/* pci_init 3 */
7030105a3dbcSRobert Elliott 	h->vaddr = NULL;
7031195f2c65SRobert Elliott 	hpsa_disable_interrupt_mode(h);		/* pci_init 2 */
7032943a7021SRobert Elliott 	/*
7033943a7021SRobert Elliott 	 * call pci_disable_device before pci_release_regions per
7034943a7021SRobert Elliott 	 * Documentation/PCI/pci.txt
7035943a7021SRobert Elliott 	 */
7036195f2c65SRobert Elliott 	pci_disable_device(h->pdev);		/* pci_init 1 */
7037943a7021SRobert Elliott 	pci_release_regions(h->pdev);		/* pci_init 2 */
7038195f2c65SRobert Elliott }
7039195f2c65SRobert Elliott 
7040195f2c65SRobert Elliott /* several items must be freed later */
70416f039790SGreg Kroah-Hartman static int hpsa_pci_init(struct ctlr_info *h)
704277c4495cSStephen M. Cameron {
7043eb6b2ae9SStephen M. Cameron 	int prod_index, err;
7044edd16368SStephen M. Cameron 
7045e5c880d1SStephen M. Cameron 	prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
7046e5c880d1SStephen M. Cameron 	if (prod_index < 0)
704760f923b9SRobert Elliott 		return prod_index;
7048e5c880d1SStephen M. Cameron 	h->product_name = products[prod_index].product_name;
7049e5c880d1SStephen M. Cameron 	h->access = *(products[prod_index].access);
7050e5c880d1SStephen M. Cameron 
70519b5c48c2SStephen Cameron 	h->needs_abort_tags_swizzled =
70529b5c48c2SStephen Cameron 		ctlr_needs_abort_tags_swizzled(h->board_id);
70539b5c48c2SStephen Cameron 
7054e5a44df8SMatthew Garrett 	pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
7055e5a44df8SMatthew Garrett 			       PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
7056e5a44df8SMatthew Garrett 
705755c06c71SStephen M. Cameron 	err = pci_enable_device(h->pdev);
7058edd16368SStephen M. Cameron 	if (err) {
7059195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "failed to enable PCI device\n");
7060943a7021SRobert Elliott 		pci_disable_device(h->pdev);
7061edd16368SStephen M. Cameron 		return err;
7062edd16368SStephen M. Cameron 	}
7063edd16368SStephen M. Cameron 
7064f79cfec6SStephen M. Cameron 	err = pci_request_regions(h->pdev, HPSA);
7065edd16368SStephen M. Cameron 	if (err) {
706655c06c71SStephen M. Cameron 		dev_err(&h->pdev->dev,
7067195f2c65SRobert Elliott 			"failed to obtain PCI resources\n");
7068943a7021SRobert Elliott 		pci_disable_device(h->pdev);
7069943a7021SRobert Elliott 		return err;
7070edd16368SStephen M. Cameron 	}
70714fa604e1SRobert Elliott 
70724fa604e1SRobert Elliott 	pci_set_master(h->pdev);
70734fa604e1SRobert Elliott 
70746b3f4c52SStephen M. Cameron 	hpsa_interrupt_mode(h);
707512d2cd47SStephen M. Cameron 	err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
70763a7774ceSStephen M. Cameron 	if (err)
7077195f2c65SRobert Elliott 		goto clean2;	/* intmode+region, pci */
7078edd16368SStephen M. Cameron 	h->vaddr = remap_pci_mem(h->paddr, 0x250);
7079204892e9SStephen M. Cameron 	if (!h->vaddr) {
7080195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "failed to remap PCI mem\n");
7081204892e9SStephen M. Cameron 		err = -ENOMEM;
7082195f2c65SRobert Elliott 		goto clean2;	/* intmode+region, pci */
7083204892e9SStephen M. Cameron 	}
7084fe5389c8SStephen M. Cameron 	err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
70852c4c8c8bSStephen M. Cameron 	if (err)
7086195f2c65SRobert Elliott 		goto clean3;	/* vaddr, intmode+region, pci */
708777c4495cSStephen M. Cameron 	err = hpsa_find_cfgtables(h);
708877c4495cSStephen M. Cameron 	if (err)
7089195f2c65SRobert Elliott 		goto clean3;	/* vaddr, intmode+region, pci */
7090b93d7536SStephen M. Cameron 	hpsa_find_board_params(h);
7091edd16368SStephen M. Cameron 
709276c46e49SStephen M. Cameron 	if (!hpsa_CISS_signature_present(h)) {
7093edd16368SStephen M. Cameron 		err = -ENODEV;
7094195f2c65SRobert Elliott 		goto clean4;	/* cfgtables, vaddr, intmode+region, pci */
7095edd16368SStephen M. Cameron 	}
709697a5e98cSStephen M. Cameron 	hpsa_set_driver_support_bits(h);
70973d0eab67SStephen M. Cameron 	hpsa_p600_dma_prefetch_quirk(h);
7098eb6b2ae9SStephen M. Cameron 	err = hpsa_enter_simple_mode(h);
7099eb6b2ae9SStephen M. Cameron 	if (err)
7100195f2c65SRobert Elliott 		goto clean4;	/* cfgtables, vaddr, intmode+region, pci */
7101edd16368SStephen M. Cameron 	return 0;
7102edd16368SStephen M. Cameron 
7103195f2c65SRobert Elliott clean4:	/* cfgtables, vaddr, intmode+region, pci */
7104195f2c65SRobert Elliott 	hpsa_free_cfgtables(h);
7105195f2c65SRobert Elliott clean3:	/* vaddr, intmode+region, pci */
7106204892e9SStephen M. Cameron 	iounmap(h->vaddr);
7107105a3dbcSRobert Elliott 	h->vaddr = NULL;
7108195f2c65SRobert Elliott clean2:	/* intmode+region, pci */
7109195f2c65SRobert Elliott 	hpsa_disable_interrupt_mode(h);
7110943a7021SRobert Elliott 	/*
7111943a7021SRobert Elliott 	 * call pci_disable_device before pci_release_regions per
7112943a7021SRobert Elliott 	 * Documentation/PCI/pci.txt
7113943a7021SRobert Elliott 	 */
7114195f2c65SRobert Elliott 	pci_disable_device(h->pdev);
7115943a7021SRobert Elliott 	pci_release_regions(h->pdev);
7116edd16368SStephen M. Cameron 	return err;
7117edd16368SStephen M. Cameron }
7118edd16368SStephen M. Cameron 
71196f039790SGreg Kroah-Hartman static void hpsa_hba_inquiry(struct ctlr_info *h)
7120339b2b14SStephen M. Cameron {
7121339b2b14SStephen M. Cameron 	int rc;
7122339b2b14SStephen M. Cameron 
7123339b2b14SStephen M. Cameron #define HBA_INQUIRY_BYTE_COUNT 64
7124339b2b14SStephen M. Cameron 	h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
7125339b2b14SStephen M. Cameron 	if (!h->hba_inquiry_data)
7126339b2b14SStephen M. Cameron 		return;
7127339b2b14SStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
7128339b2b14SStephen M. Cameron 		h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
7129339b2b14SStephen M. Cameron 	if (rc != 0) {
7130339b2b14SStephen M. Cameron 		kfree(h->hba_inquiry_data);
7131339b2b14SStephen M. Cameron 		h->hba_inquiry_data = NULL;
7132339b2b14SStephen M. Cameron 	}
7133339b2b14SStephen M. Cameron }
7134339b2b14SStephen M. Cameron 
71356b6c1cd7STomas Henzl static int hpsa_init_reset_devices(struct pci_dev *pdev, u32 board_id)
7136edd16368SStephen M. Cameron {
71371df8552aSStephen M. Cameron 	int rc, i;
71383b747298STomas Henzl 	void __iomem *vaddr;
7139edd16368SStephen M. Cameron 
71404c2a8c40SStephen M. Cameron 	if (!reset_devices)
71414c2a8c40SStephen M. Cameron 		return 0;
71424c2a8c40SStephen M. Cameron 
7143132aa220STomas Henzl 	/* kdump kernel is loading, we don't know in which state is
7144132aa220STomas Henzl 	 * the pci interface. The dev->enable_cnt is equal zero
7145132aa220STomas Henzl 	 * so we call enable+disable, wait a while and switch it on.
7146132aa220STomas Henzl 	 */
7147132aa220STomas Henzl 	rc = pci_enable_device(pdev);
7148132aa220STomas Henzl 	if (rc) {
7149132aa220STomas Henzl 		dev_warn(&pdev->dev, "Failed to enable PCI device\n");
7150132aa220STomas Henzl 		return -ENODEV;
7151132aa220STomas Henzl 	}
7152132aa220STomas Henzl 	pci_disable_device(pdev);
7153132aa220STomas Henzl 	msleep(260);			/* a randomly chosen number */
7154132aa220STomas Henzl 	rc = pci_enable_device(pdev);
7155132aa220STomas Henzl 	if (rc) {
7156132aa220STomas Henzl 		dev_warn(&pdev->dev, "failed to enable device.\n");
7157132aa220STomas Henzl 		return -ENODEV;
7158132aa220STomas Henzl 	}
71594fa604e1SRobert Elliott 
7160859c75abSTomas Henzl 	pci_set_master(pdev);
71614fa604e1SRobert Elliott 
71623b747298STomas Henzl 	vaddr = pci_ioremap_bar(pdev, 0);
71633b747298STomas Henzl 	if (vaddr == NULL) {
71643b747298STomas Henzl 		rc = -ENOMEM;
71653b747298STomas Henzl 		goto out_disable;
71663b747298STomas Henzl 	}
71673b747298STomas Henzl 	writel(SA5_INTR_OFF, vaddr + SA5_REPLY_INTR_MASK_OFFSET);
71683b747298STomas Henzl 	iounmap(vaddr);
71693b747298STomas Henzl 
71701df8552aSStephen M. Cameron 	/* Reset the controller with a PCI power-cycle or via doorbell */
71716b6c1cd7STomas Henzl 	rc = hpsa_kdump_hard_reset_controller(pdev, board_id);
7172edd16368SStephen M. Cameron 
71731df8552aSStephen M. Cameron 	/* -ENOTSUPP here means we cannot reset the controller
71741df8552aSStephen M. Cameron 	 * but it's already (and still) up and running in
717518867659SStephen M. Cameron 	 * "performant mode".  Or, it might be 640x, which can't reset
717618867659SStephen M. Cameron 	 * due to concerns about shared bbwc between 6402/6404 pair.
71771df8552aSStephen M. Cameron 	 */
7178adf1b3a3SRobert Elliott 	if (rc)
7179132aa220STomas Henzl 		goto out_disable;
7180edd16368SStephen M. Cameron 
7181edd16368SStephen M. Cameron 	/* Now try to get the controller to respond to a no-op */
71821ba66c9cSRobert Elliott 	dev_info(&pdev->dev, "Waiting for controller to respond to no-op\n");
7183edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
7184edd16368SStephen M. Cameron 		if (hpsa_noop(pdev) == 0)
7185edd16368SStephen M. Cameron 			break;
7186edd16368SStephen M. Cameron 		else
7187edd16368SStephen M. Cameron 			dev_warn(&pdev->dev, "no-op failed%s\n",
7188edd16368SStephen M. Cameron 					(i < 11 ? "; re-trying" : ""));
7189edd16368SStephen M. Cameron 	}
7190132aa220STomas Henzl 
7191132aa220STomas Henzl out_disable:
7192132aa220STomas Henzl 
7193132aa220STomas Henzl 	pci_disable_device(pdev);
7194132aa220STomas Henzl 	return rc;
7195edd16368SStephen M. Cameron }
7196edd16368SStephen M. Cameron 
71971fb7c98aSRobert Elliott static void hpsa_free_cmd_pool(struct ctlr_info *h)
71981fb7c98aSRobert Elliott {
71991fb7c98aSRobert Elliott 	kfree(h->cmd_pool_bits);
7200105a3dbcSRobert Elliott 	h->cmd_pool_bits = NULL;
7201105a3dbcSRobert Elliott 	if (h->cmd_pool) {
72021fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
72031fb7c98aSRobert Elliott 				h->nr_cmds * sizeof(struct CommandList),
72041fb7c98aSRobert Elliott 				h->cmd_pool,
72051fb7c98aSRobert Elliott 				h->cmd_pool_dhandle);
7206105a3dbcSRobert Elliott 		h->cmd_pool = NULL;
7207105a3dbcSRobert Elliott 		h->cmd_pool_dhandle = 0;
7208105a3dbcSRobert Elliott 	}
7209105a3dbcSRobert Elliott 	if (h->errinfo_pool) {
72101fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
72111fb7c98aSRobert Elliott 				h->nr_cmds * sizeof(struct ErrorInfo),
72121fb7c98aSRobert Elliott 				h->errinfo_pool,
72131fb7c98aSRobert Elliott 				h->errinfo_pool_dhandle);
7214105a3dbcSRobert Elliott 		h->errinfo_pool = NULL;
7215105a3dbcSRobert Elliott 		h->errinfo_pool_dhandle = 0;
7216105a3dbcSRobert Elliott 	}
72171fb7c98aSRobert Elliott }
72181fb7c98aSRobert Elliott 
7219d37ffbe4SRobert Elliott static int hpsa_alloc_cmd_pool(struct ctlr_info *h)
72202e9d1b36SStephen M. Cameron {
72212e9d1b36SStephen M. Cameron 	h->cmd_pool_bits = kzalloc(
72222e9d1b36SStephen M. Cameron 		DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
72232e9d1b36SStephen M. Cameron 		sizeof(unsigned long), GFP_KERNEL);
72242e9d1b36SStephen M. Cameron 	h->cmd_pool = pci_alloc_consistent(h->pdev,
72252e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->cmd_pool),
72262e9d1b36SStephen M. Cameron 		    &(h->cmd_pool_dhandle));
72272e9d1b36SStephen M. Cameron 	h->errinfo_pool = pci_alloc_consistent(h->pdev,
72282e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->errinfo_pool),
72292e9d1b36SStephen M. Cameron 		    &(h->errinfo_pool_dhandle));
72302e9d1b36SStephen M. Cameron 	if ((h->cmd_pool_bits == NULL)
72312e9d1b36SStephen M. Cameron 	    || (h->cmd_pool == NULL)
72322e9d1b36SStephen M. Cameron 	    || (h->errinfo_pool == NULL)) {
72332e9d1b36SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory in %s", __func__);
72342c143342SRobert Elliott 		goto clean_up;
72352e9d1b36SStephen M. Cameron 	}
7236360c73bdSStephen Cameron 	hpsa_preinitialize_commands(h);
72372e9d1b36SStephen M. Cameron 	return 0;
72382c143342SRobert Elliott clean_up:
72392c143342SRobert Elliott 	hpsa_free_cmd_pool(h);
72402c143342SRobert Elliott 	return -ENOMEM;
72412e9d1b36SStephen M. Cameron }
72422e9d1b36SStephen M. Cameron 
724341b3cf08SStephen M. Cameron static void hpsa_irq_affinity_hints(struct ctlr_info *h)
724441b3cf08SStephen M. Cameron {
7245ec429952SFabian Frederick 	int i, cpu;
724641b3cf08SStephen M. Cameron 
724741b3cf08SStephen M. Cameron 	cpu = cpumask_first(cpu_online_mask);
724841b3cf08SStephen M. Cameron 	for (i = 0; i < h->msix_vector; i++) {
7249ec429952SFabian Frederick 		irq_set_affinity_hint(h->intr[i], get_cpu_mask(cpu));
725041b3cf08SStephen M. Cameron 		cpu = cpumask_next(cpu, cpu_online_mask);
725141b3cf08SStephen M. Cameron 	}
725241b3cf08SStephen M. Cameron }
725341b3cf08SStephen M. Cameron 
7254ec501a18SRobert Elliott /* clear affinity hints and free MSI-X, MSI, or legacy INTx vectors */
7255ec501a18SRobert Elliott static void hpsa_free_irqs(struct ctlr_info *h)
7256ec501a18SRobert Elliott {
7257ec501a18SRobert Elliott 	int i;
7258ec501a18SRobert Elliott 
7259ec501a18SRobert Elliott 	if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
7260ec501a18SRobert Elliott 		/* Single reply queue, only one irq to free */
7261ec501a18SRobert Elliott 		i = h->intr_mode;
7262ec501a18SRobert Elliott 		irq_set_affinity_hint(h->intr[i], NULL);
7263ec501a18SRobert Elliott 		free_irq(h->intr[i], &h->q[i]);
7264105a3dbcSRobert Elliott 		h->q[i] = 0;
7265ec501a18SRobert Elliott 		return;
7266ec501a18SRobert Elliott 	}
7267ec501a18SRobert Elliott 
7268ec501a18SRobert Elliott 	for (i = 0; i < h->msix_vector; i++) {
7269ec501a18SRobert Elliott 		irq_set_affinity_hint(h->intr[i], NULL);
7270ec501a18SRobert Elliott 		free_irq(h->intr[i], &h->q[i]);
7271105a3dbcSRobert Elliott 		h->q[i] = 0;
7272ec501a18SRobert Elliott 	}
7273a4e17fc1SRobert Elliott 	for (; i < MAX_REPLY_QUEUES; i++)
7274a4e17fc1SRobert Elliott 		h->q[i] = 0;
7275ec501a18SRobert Elliott }
7276ec501a18SRobert Elliott 
72779ee61794SRobert Elliott /* returns 0 on success; cleans up and returns -Enn on error */
72789ee61794SRobert Elliott static int hpsa_request_irqs(struct ctlr_info *h,
72790ae01a32SStephen M. Cameron 	irqreturn_t (*msixhandler)(int, void *),
72800ae01a32SStephen M. Cameron 	irqreturn_t (*intxhandler)(int, void *))
72810ae01a32SStephen M. Cameron {
7282254f796bSMatt Gates 	int rc, i;
72830ae01a32SStephen M. Cameron 
7284254f796bSMatt Gates 	/*
7285254f796bSMatt Gates 	 * initialize h->q[x] = x so that interrupt handlers know which
7286254f796bSMatt Gates 	 * queue to process.
7287254f796bSMatt Gates 	 */
7288254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++)
7289254f796bSMatt Gates 		h->q[i] = (u8) i;
7290254f796bSMatt Gates 
7291eee0f03aSHannes Reinecke 	if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
7292254f796bSMatt Gates 		/* If performant mode and MSI-X, use multiple reply queues */
7293a4e17fc1SRobert Elliott 		for (i = 0; i < h->msix_vector; i++) {
7294254f796bSMatt Gates 			rc = request_irq(h->intr[i], msixhandler,
7295254f796bSMatt Gates 					0, h->devname,
7296254f796bSMatt Gates 					&h->q[i]);
7297a4e17fc1SRobert Elliott 			if (rc) {
7298a4e17fc1SRobert Elliott 				int j;
7299a4e17fc1SRobert Elliott 
7300a4e17fc1SRobert Elliott 				dev_err(&h->pdev->dev,
7301a4e17fc1SRobert Elliott 					"failed to get irq %d for %s\n",
7302a4e17fc1SRobert Elliott 				       h->intr[i], h->devname);
7303a4e17fc1SRobert Elliott 				for (j = 0; j < i; j++) {
7304a4e17fc1SRobert Elliott 					free_irq(h->intr[j], &h->q[j]);
7305a4e17fc1SRobert Elliott 					h->q[j] = 0;
7306a4e17fc1SRobert Elliott 				}
7307a4e17fc1SRobert Elliott 				for (; j < MAX_REPLY_QUEUES; j++)
7308a4e17fc1SRobert Elliott 					h->q[j] = 0;
7309a4e17fc1SRobert Elliott 				return rc;
7310a4e17fc1SRobert Elliott 			}
7311a4e17fc1SRobert Elliott 		}
731241b3cf08SStephen M. Cameron 		hpsa_irq_affinity_hints(h);
7313254f796bSMatt Gates 	} else {
7314254f796bSMatt Gates 		/* Use single reply pool */
7315eee0f03aSHannes Reinecke 		if (h->msix_vector > 0 || h->msi_vector) {
7316254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
7317254f796bSMatt Gates 				msixhandler, 0, h->devname,
7318254f796bSMatt Gates 				&h->q[h->intr_mode]);
7319254f796bSMatt Gates 		} else {
7320254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
7321254f796bSMatt Gates 				intxhandler, IRQF_SHARED, h->devname,
7322254f796bSMatt Gates 				&h->q[h->intr_mode]);
7323254f796bSMatt Gates 		}
7324105a3dbcSRobert Elliott 		irq_set_affinity_hint(h->intr[h->intr_mode], NULL);
7325254f796bSMatt Gates 	}
73260ae01a32SStephen M. Cameron 	if (rc) {
7327195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "failed to get irq %d for %s\n",
73280ae01a32SStephen M. Cameron 		       h->intr[h->intr_mode], h->devname);
7329195f2c65SRobert Elliott 		hpsa_free_irqs(h);
73300ae01a32SStephen M. Cameron 		return -ENODEV;
73310ae01a32SStephen M. Cameron 	}
73320ae01a32SStephen M. Cameron 	return 0;
73330ae01a32SStephen M. Cameron }
73340ae01a32SStephen M. Cameron 
73356f039790SGreg Kroah-Hartman static int hpsa_kdump_soft_reset(struct ctlr_info *h)
733664670ac8SStephen M. Cameron {
7337bf43caf3SRobert Elliott 	hpsa_send_host_reset(h, RAID_CTLR_LUNID, HPSA_RESET_TYPE_CONTROLLER);
733864670ac8SStephen M. Cameron 
733964670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
734064670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) {
734164670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
734264670ac8SStephen M. Cameron 		return -1;
734364670ac8SStephen M. Cameron 	}
734464670ac8SStephen M. Cameron 
734564670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
734664670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) {
734764670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Board failed to become ready "
734864670ac8SStephen M. Cameron 			"after soft reset.\n");
734964670ac8SStephen M. Cameron 		return -1;
735064670ac8SStephen M. Cameron 	}
735164670ac8SStephen M. Cameron 
735264670ac8SStephen M. Cameron 	return 0;
735364670ac8SStephen M. Cameron }
735464670ac8SStephen M. Cameron 
7355072b0518SStephen M. Cameron static void hpsa_free_reply_queues(struct ctlr_info *h)
7356072b0518SStephen M. Cameron {
7357072b0518SStephen M. Cameron 	int i;
7358072b0518SStephen M. Cameron 
7359072b0518SStephen M. Cameron 	for (i = 0; i < h->nreply_queues; i++) {
7360072b0518SStephen M. Cameron 		if (!h->reply_queue[i].head)
7361072b0518SStephen M. Cameron 			continue;
73621fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
73631fb7c98aSRobert Elliott 					h->reply_queue_size,
73641fb7c98aSRobert Elliott 					h->reply_queue[i].head,
73651fb7c98aSRobert Elliott 					h->reply_queue[i].busaddr);
7366072b0518SStephen M. Cameron 		h->reply_queue[i].head = NULL;
7367072b0518SStephen M. Cameron 		h->reply_queue[i].busaddr = 0;
7368072b0518SStephen M. Cameron 	}
7369105a3dbcSRobert Elliott 	h->reply_queue_size = 0;
7370072b0518SStephen M. Cameron }
7371072b0518SStephen M. Cameron 
73720097f0f4SStephen M. Cameron static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
73730097f0f4SStephen M. Cameron {
7374105a3dbcSRobert Elliott 	hpsa_free_performant_mode(h);		/* init_one 7 */
7375105a3dbcSRobert Elliott 	hpsa_free_sg_chain_blocks(h);		/* init_one 6 */
7376105a3dbcSRobert Elliott 	hpsa_free_cmd_pool(h);			/* init_one 5 */
7377105a3dbcSRobert Elliott 	hpsa_free_irqs(h);			/* init_one 4 */
7378105a3dbcSRobert Elliott 	hpsa_free_pci_init(h);			/* init_one 3 */
7379*9ecd953aSRobert Elliott 	free_percpu(h->lockup_detected);	/* init_one 2 */
7380*9ecd953aSRobert Elliott 	h->lockup_detected = NULL;		/* init_one 2 */
7381*9ecd953aSRobert Elliott 	if (h->resubmit_wq) {
7382*9ecd953aSRobert Elliott 		destroy_workqueue(h->resubmit_wq);	/* init_one 1 */
7383*9ecd953aSRobert Elliott 		h->resubmit_wq = NULL;
7384*9ecd953aSRobert Elliott 	}
7385*9ecd953aSRobert Elliott 	if (h->rescan_ctlr_wq) {
7386*9ecd953aSRobert Elliott 		destroy_workqueue(h->rescan_ctlr_wq);
7387*9ecd953aSRobert Elliott 		h->rescan_ctlr_wq = NULL;
7388*9ecd953aSRobert Elliott 	}
7389105a3dbcSRobert Elliott 	kfree(h);				/* init_one 1 */
739064670ac8SStephen M. Cameron }
739164670ac8SStephen M. Cameron 
7392a0c12413SStephen M. Cameron /* Called when controller lockup detected. */
7393f2405db8SDon Brace static void fail_all_outstanding_cmds(struct ctlr_info *h)
7394a0c12413SStephen M. Cameron {
7395281a7fd0SWebb Scales 	int i, refcount;
7396281a7fd0SWebb Scales 	struct CommandList *c;
739725163bd5SWebb Scales 	int failcount = 0;
7398a0c12413SStephen M. Cameron 
7399080ef1ccSDon Brace 	flush_workqueue(h->resubmit_wq); /* ensure all cmds are fully built */
7400f2405db8SDon Brace 	for (i = 0; i < h->nr_cmds; i++) {
7401f2405db8SDon Brace 		c = h->cmd_pool + i;
7402281a7fd0SWebb Scales 		refcount = atomic_inc_return(&c->refcount);
7403281a7fd0SWebb Scales 		if (refcount > 1) {
740425163bd5SWebb Scales 			c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
74055a3d16f5SStephen M. Cameron 			finish_cmd(c);
7406433b5f4dSStephen Cameron 			atomic_dec(&h->commands_outstanding);
740725163bd5SWebb Scales 			failcount++;
7408a0c12413SStephen M. Cameron 		}
7409281a7fd0SWebb Scales 		cmd_free(h, c);
7410281a7fd0SWebb Scales 	}
741125163bd5SWebb Scales 	dev_warn(&h->pdev->dev,
741225163bd5SWebb Scales 		"failed %d commands in fail_all\n", failcount);
7413a0c12413SStephen M. Cameron }
7414a0c12413SStephen M. Cameron 
7415094963daSStephen M. Cameron static void set_lockup_detected_for_all_cpus(struct ctlr_info *h, u32 value)
7416094963daSStephen M. Cameron {
7417c8ed0010SRusty Russell 	int cpu;
7418094963daSStephen M. Cameron 
7419c8ed0010SRusty Russell 	for_each_online_cpu(cpu) {
7420094963daSStephen M. Cameron 		u32 *lockup_detected;
7421094963daSStephen M. Cameron 		lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
7422094963daSStephen M. Cameron 		*lockup_detected = value;
7423094963daSStephen M. Cameron 	}
7424094963daSStephen M. Cameron 	wmb(); /* be sure the per-cpu variables are out to memory */
7425094963daSStephen M. Cameron }
7426094963daSStephen M. Cameron 
7427a0c12413SStephen M. Cameron static void controller_lockup_detected(struct ctlr_info *h)
7428a0c12413SStephen M. Cameron {
7429a0c12413SStephen M. Cameron 	unsigned long flags;
7430094963daSStephen M. Cameron 	u32 lockup_detected;
7431a0c12413SStephen M. Cameron 
7432a0c12413SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
7433a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
7434094963daSStephen M. Cameron 	lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
7435094963daSStephen M. Cameron 	if (!lockup_detected) {
7436094963daSStephen M. Cameron 		/* no heartbeat, but controller gave us a zero. */
7437094963daSStephen M. Cameron 		dev_warn(&h->pdev->dev,
743825163bd5SWebb Scales 			"lockup detected after %d but scratchpad register is zero\n",
743925163bd5SWebb Scales 			h->heartbeat_sample_interval / HZ);
7440094963daSStephen M. Cameron 		lockup_detected = 0xffffffff;
7441094963daSStephen M. Cameron 	}
7442094963daSStephen M. Cameron 	set_lockup_detected_for_all_cpus(h, lockup_detected);
7443a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
744425163bd5SWebb Scales 	dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x after %d\n",
744525163bd5SWebb Scales 			lockup_detected, h->heartbeat_sample_interval / HZ);
7446a0c12413SStephen M. Cameron 	pci_disable_device(h->pdev);
7447f2405db8SDon Brace 	fail_all_outstanding_cmds(h);
7448a0c12413SStephen M. Cameron }
7449a0c12413SStephen M. Cameron 
745025163bd5SWebb Scales static int detect_controller_lockup(struct ctlr_info *h)
7451a0c12413SStephen M. Cameron {
7452a0c12413SStephen M. Cameron 	u64 now;
7453a0c12413SStephen M. Cameron 	u32 heartbeat;
7454a0c12413SStephen M. Cameron 	unsigned long flags;
7455a0c12413SStephen M. Cameron 
7456a0c12413SStephen M. Cameron 	now = get_jiffies_64();
7457a0c12413SStephen M. Cameron 	/* If we've received an interrupt recently, we're ok. */
7458a0c12413SStephen M. Cameron 	if (time_after64(h->last_intr_timestamp +
7459e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
746025163bd5SWebb Scales 		return false;
7461a0c12413SStephen M. Cameron 
7462a0c12413SStephen M. Cameron 	/*
7463a0c12413SStephen M. Cameron 	 * If we've already checked the heartbeat recently, we're ok.
7464a0c12413SStephen M. Cameron 	 * This could happen if someone sends us a signal. We
7465a0c12413SStephen M. Cameron 	 * otherwise don't care about signals in this thread.
7466a0c12413SStephen M. Cameron 	 */
7467a0c12413SStephen M. Cameron 	if (time_after64(h->last_heartbeat_timestamp +
7468e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
746925163bd5SWebb Scales 		return false;
7470a0c12413SStephen M. Cameron 
7471a0c12413SStephen M. Cameron 	/* If heartbeat has not changed since we last looked, we're not ok. */
7472a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
7473a0c12413SStephen M. Cameron 	heartbeat = readl(&h->cfgtable->HeartBeat);
7474a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
7475a0c12413SStephen M. Cameron 	if (h->last_heartbeat == heartbeat) {
7476a0c12413SStephen M. Cameron 		controller_lockup_detected(h);
747725163bd5SWebb Scales 		return true;
7478a0c12413SStephen M. Cameron 	}
7479a0c12413SStephen M. Cameron 
7480a0c12413SStephen M. Cameron 	/* We're ok. */
7481a0c12413SStephen M. Cameron 	h->last_heartbeat = heartbeat;
7482a0c12413SStephen M. Cameron 	h->last_heartbeat_timestamp = now;
748325163bd5SWebb Scales 	return false;
7484a0c12413SStephen M. Cameron }
7485a0c12413SStephen M. Cameron 
74869846590eSStephen M. Cameron static void hpsa_ack_ctlr_events(struct ctlr_info *h)
748776438d08SStephen M. Cameron {
748876438d08SStephen M. Cameron 	int i;
748976438d08SStephen M. Cameron 	char *event_type;
749076438d08SStephen M. Cameron 
7491e4aa3e6aSStephen Cameron 	if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
7492e4aa3e6aSStephen Cameron 		return;
7493e4aa3e6aSStephen Cameron 
749476438d08SStephen M. Cameron 	/* Ask the controller to clear the events we're handling. */
74951f7cee8cSStephen M. Cameron 	if ((h->transMethod & (CFGTBL_Trans_io_accel1
74961f7cee8cSStephen M. Cameron 			| CFGTBL_Trans_io_accel2)) &&
749776438d08SStephen M. Cameron 		(h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
749876438d08SStephen M. Cameron 		 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
749976438d08SStephen M. Cameron 
750076438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
750176438d08SStephen M. Cameron 			event_type = "state change";
750276438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
750376438d08SStephen M. Cameron 			event_type = "configuration change";
750476438d08SStephen M. Cameron 		/* Stop sending new RAID offload reqs via the IO accelerator */
750576438d08SStephen M. Cameron 		scsi_block_requests(h->scsi_host);
750676438d08SStephen M. Cameron 		for (i = 0; i < h->ndevices; i++)
750776438d08SStephen M. Cameron 			h->dev[i]->offload_enabled = 0;
750823100dd9SStephen M. Cameron 		hpsa_drain_accel_commands(h);
750976438d08SStephen M. Cameron 		/* Set 'accelerator path config change' bit */
751076438d08SStephen M. Cameron 		dev_warn(&h->pdev->dev,
751176438d08SStephen M. Cameron 			"Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
751276438d08SStephen M. Cameron 			h->events, event_type);
751376438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
751476438d08SStephen M. Cameron 		/* Set the "clear event notify field update" bit 6 */
751576438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
751676438d08SStephen M. Cameron 		/* Wait until ctlr clears 'clear event notify field', bit 6 */
751776438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
751876438d08SStephen M. Cameron 		scsi_unblock_requests(h->scsi_host);
751976438d08SStephen M. Cameron 	} else {
752076438d08SStephen M. Cameron 		/* Acknowledge controller notification events. */
752176438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
752276438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
752376438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
752476438d08SStephen M. Cameron #if 0
752576438d08SStephen M. Cameron 		writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
752676438d08SStephen M. Cameron 		hpsa_wait_for_mode_change_ack(h);
752776438d08SStephen M. Cameron #endif
752876438d08SStephen M. Cameron 	}
75299846590eSStephen M. Cameron 	return;
753076438d08SStephen M. Cameron }
753176438d08SStephen M. Cameron 
753276438d08SStephen M. Cameron /* Check a register on the controller to see if there are configuration
753376438d08SStephen M. Cameron  * changes (added/changed/removed logical drives, etc.) which mean that
7534e863d68eSScott Teel  * we should rescan the controller for devices.
7535e863d68eSScott Teel  * Also check flag for driver-initiated rescan.
753676438d08SStephen M. Cameron  */
75379846590eSStephen M. Cameron static int hpsa_ctlr_needs_rescan(struct ctlr_info *h)
753876438d08SStephen M. Cameron {
753976438d08SStephen M. Cameron 	if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
75409846590eSStephen M. Cameron 		return 0;
754176438d08SStephen M. Cameron 
754276438d08SStephen M. Cameron 	h->events = readl(&(h->cfgtable->event_notify));
75439846590eSStephen M. Cameron 	return h->events & RESCAN_REQUIRED_EVENT_BITS;
75449846590eSStephen M. Cameron }
754576438d08SStephen M. Cameron 
754676438d08SStephen M. Cameron /*
75479846590eSStephen M. Cameron  * Check if any of the offline devices have become ready
754876438d08SStephen M. Cameron  */
75499846590eSStephen M. Cameron static int hpsa_offline_devices_ready(struct ctlr_info *h)
75509846590eSStephen M. Cameron {
75519846590eSStephen M. Cameron 	unsigned long flags;
75529846590eSStephen M. Cameron 	struct offline_device_entry *d;
75539846590eSStephen M. Cameron 	struct list_head *this, *tmp;
75549846590eSStephen M. Cameron 
75559846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
75569846590eSStephen M. Cameron 	list_for_each_safe(this, tmp, &h->offline_device_list) {
75579846590eSStephen M. Cameron 		d = list_entry(this, struct offline_device_entry,
75589846590eSStephen M. Cameron 				offline_list);
75599846590eSStephen M. Cameron 		spin_unlock_irqrestore(&h->offline_device_lock, flags);
7560d1fea47cSStephen M. Cameron 		if (!hpsa_volume_offline(h, d->scsi3addr)) {
7561d1fea47cSStephen M. Cameron 			spin_lock_irqsave(&h->offline_device_lock, flags);
7562d1fea47cSStephen M. Cameron 			list_del(&d->offline_list);
7563d1fea47cSStephen M. Cameron 			spin_unlock_irqrestore(&h->offline_device_lock, flags);
75649846590eSStephen M. Cameron 			return 1;
7565d1fea47cSStephen M. Cameron 		}
75669846590eSStephen M. Cameron 		spin_lock_irqsave(&h->offline_device_lock, flags);
756776438d08SStephen M. Cameron 	}
75689846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
75699846590eSStephen M. Cameron 	return 0;
75709846590eSStephen M. Cameron }
75719846590eSStephen M. Cameron 
75726636e7f4SDon Brace static void hpsa_rescan_ctlr_worker(struct work_struct *work)
7573a0c12413SStephen M. Cameron {
7574a0c12413SStephen M. Cameron 	unsigned long flags;
75758a98db73SStephen M. Cameron 	struct ctlr_info *h = container_of(to_delayed_work(work),
75766636e7f4SDon Brace 					struct ctlr_info, rescan_ctlr_work);
75776636e7f4SDon Brace 
75786636e7f4SDon Brace 
75796636e7f4SDon Brace 	if (h->remove_in_progress)
75808a98db73SStephen M. Cameron 		return;
75819846590eSStephen M. Cameron 
75829846590eSStephen M. Cameron 	if (hpsa_ctlr_needs_rescan(h) || hpsa_offline_devices_ready(h)) {
75839846590eSStephen M. Cameron 		scsi_host_get(h->scsi_host);
75849846590eSStephen M. Cameron 		hpsa_ack_ctlr_events(h);
75859846590eSStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
75869846590eSStephen M. Cameron 		scsi_host_put(h->scsi_host);
75879846590eSStephen M. Cameron 	}
75886636e7f4SDon Brace 	spin_lock_irqsave(&h->lock, flags);
75896636e7f4SDon Brace 	if (!h->remove_in_progress)
75906636e7f4SDon Brace 		queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
75916636e7f4SDon Brace 				h->heartbeat_sample_interval);
75926636e7f4SDon Brace 	spin_unlock_irqrestore(&h->lock, flags);
75936636e7f4SDon Brace }
75946636e7f4SDon Brace 
75956636e7f4SDon Brace static void hpsa_monitor_ctlr_worker(struct work_struct *work)
75966636e7f4SDon Brace {
75976636e7f4SDon Brace 	unsigned long flags;
75986636e7f4SDon Brace 	struct ctlr_info *h = container_of(to_delayed_work(work),
75996636e7f4SDon Brace 					struct ctlr_info, monitor_ctlr_work);
76006636e7f4SDon Brace 
76016636e7f4SDon Brace 	detect_controller_lockup(h);
76026636e7f4SDon Brace 	if (lockup_detected(h))
76036636e7f4SDon Brace 		return;
76049846590eSStephen M. Cameron 
76058a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
76066636e7f4SDon Brace 	if (!h->remove_in_progress)
76078a98db73SStephen M. Cameron 		schedule_delayed_work(&h->monitor_ctlr_work,
76088a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
76098a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
7610a0c12413SStephen M. Cameron }
7611a0c12413SStephen M. Cameron 
76126636e7f4SDon Brace static struct workqueue_struct *hpsa_create_controller_wq(struct ctlr_info *h,
76136636e7f4SDon Brace 						char *name)
76146636e7f4SDon Brace {
76156636e7f4SDon Brace 	struct workqueue_struct *wq = NULL;
76166636e7f4SDon Brace 
7617397ea9cbSDon Brace 	wq = alloc_ordered_workqueue("%s_%d_hpsa", 0, name, h->ctlr);
76186636e7f4SDon Brace 	if (!wq)
76196636e7f4SDon Brace 		dev_err(&h->pdev->dev, "failed to create %s workqueue\n", name);
76206636e7f4SDon Brace 
76216636e7f4SDon Brace 	return wq;
76226636e7f4SDon Brace }
76236636e7f4SDon Brace 
76246f039790SGreg Kroah-Hartman static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
76254c2a8c40SStephen M. Cameron {
76264c2a8c40SStephen M. Cameron 	int dac, rc;
76274c2a8c40SStephen M. Cameron 	struct ctlr_info *h;
762864670ac8SStephen M. Cameron 	int try_soft_reset = 0;
762964670ac8SStephen M. Cameron 	unsigned long flags;
76306b6c1cd7STomas Henzl 	u32 board_id;
76314c2a8c40SStephen M. Cameron 
76324c2a8c40SStephen M. Cameron 	if (number_of_controllers == 0)
76334c2a8c40SStephen M. Cameron 		printk(KERN_INFO DRIVER_NAME "\n");
76344c2a8c40SStephen M. Cameron 
76356b6c1cd7STomas Henzl 	rc = hpsa_lookup_board_id(pdev, &board_id);
76366b6c1cd7STomas Henzl 	if (rc < 0) {
76376b6c1cd7STomas Henzl 		dev_warn(&pdev->dev, "Board ID not found\n");
76386b6c1cd7STomas Henzl 		return rc;
76396b6c1cd7STomas Henzl 	}
76406b6c1cd7STomas Henzl 
76416b6c1cd7STomas Henzl 	rc = hpsa_init_reset_devices(pdev, board_id);
764264670ac8SStephen M. Cameron 	if (rc) {
764364670ac8SStephen M. Cameron 		if (rc != -ENOTSUPP)
76444c2a8c40SStephen M. Cameron 			return rc;
764564670ac8SStephen M. Cameron 		/* If the reset fails in a particular way (it has no way to do
764664670ac8SStephen M. Cameron 		 * a proper hard reset, so returns -ENOTSUPP) we can try to do
764764670ac8SStephen M. Cameron 		 * a soft reset once we get the controller configured up to the
764864670ac8SStephen M. Cameron 		 * point that it can accept a command.
764964670ac8SStephen M. Cameron 		 */
765064670ac8SStephen M. Cameron 		try_soft_reset = 1;
765164670ac8SStephen M. Cameron 		rc = 0;
765264670ac8SStephen M. Cameron 	}
765364670ac8SStephen M. Cameron 
765464670ac8SStephen M. Cameron reinit_after_soft_reset:
76554c2a8c40SStephen M. Cameron 
7656303932fdSDon Brace 	/* Command structures must be aligned on a 32-byte boundary because
7657303932fdSDon Brace 	 * the 5 lower bits of the address are used by the hardware. and by
7658303932fdSDon Brace 	 * the driver.  See comments in hpsa.h for more info.
7659303932fdSDon Brace 	 */
7660303932fdSDon Brace 	BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
7661edd16368SStephen M. Cameron 	h = kzalloc(sizeof(*h), GFP_KERNEL);
7662105a3dbcSRobert Elliott 	if (!h) {
7663105a3dbcSRobert Elliott 		dev_err(&pdev->dev, "Failed to allocate controller head\n");
7664ecd9aad4SStephen M. Cameron 		return -ENOMEM;
7665105a3dbcSRobert Elliott 	}
7666edd16368SStephen M. Cameron 
766755c06c71SStephen M. Cameron 	h->pdev = pdev;
7668105a3dbcSRobert Elliott 
7669a9a3a273SStephen M. Cameron 	h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
76709846590eSStephen M. Cameron 	INIT_LIST_HEAD(&h->offline_device_list);
76716eaf46fdSStephen M. Cameron 	spin_lock_init(&h->lock);
76729846590eSStephen M. Cameron 	spin_lock_init(&h->offline_device_lock);
76736eaf46fdSStephen M. Cameron 	spin_lock_init(&h->scan_lock);
767434f0c627SDon Brace 	atomic_set(&h->passthru_cmds_avail, HPSA_MAX_CONCURRENT_PASSTHRUS);
76759b5c48c2SStephen Cameron 	atomic_set(&h->abort_cmds_available, HPSA_CMDS_RESERVED_FOR_ABORTS);
7676094963daSStephen M. Cameron 
76776636e7f4SDon Brace 	h->rescan_ctlr_wq = hpsa_create_controller_wq(h, "rescan");
76786636e7f4SDon Brace 	if (!h->rescan_ctlr_wq) {
7679080ef1ccSDon Brace 		rc = -ENOMEM;
7680080ef1ccSDon Brace 		goto clean1;
7681080ef1ccSDon Brace 	}
76826636e7f4SDon Brace 
76836636e7f4SDon Brace 	h->resubmit_wq = hpsa_create_controller_wq(h, "resubmit");
76846636e7f4SDon Brace 	if (!h->resubmit_wq) {
76856636e7f4SDon Brace 		rc = -ENOMEM;
7686105a3dbcSRobert Elliott 		goto clean1;	/* aer/h */
76876636e7f4SDon Brace 	}
76886636e7f4SDon Brace 
7689094963daSStephen M. Cameron 	/* Allocate and clear per-cpu variable lockup_detected */
7690094963daSStephen M. Cameron 	h->lockup_detected = alloc_percpu(u32);
76912a5ac326SStephen M. Cameron 	if (!h->lockup_detected) {
7692105a3dbcSRobert Elliott 		dev_err(&h->pdev->dev, "Failed to allocate lockup detector\n");
76932a5ac326SStephen M. Cameron 		rc = -ENOMEM;
7694105a3dbcSRobert Elliott 		goto clean1;	/* wq/aer/h */
76952a5ac326SStephen M. Cameron 	}
7696094963daSStephen M. Cameron 	set_lockup_detected_for_all_cpus(h, 0);
7697094963daSStephen M. Cameron 
769855c06c71SStephen M. Cameron 	rc = hpsa_pci_init(h);
7699105a3dbcSRobert Elliott 	if (rc)
7700105a3dbcSRobert Elliott 		goto clean2;	/* lockup, wq/aer/h */
7701edd16368SStephen M. Cameron 
7702f79cfec6SStephen M. Cameron 	sprintf(h->devname, HPSA "%d", number_of_controllers);
7703edd16368SStephen M. Cameron 	h->ctlr = number_of_controllers;
7704edd16368SStephen M. Cameron 	number_of_controllers++;
7705edd16368SStephen M. Cameron 
7706edd16368SStephen M. Cameron 	/* configure PCI DMA stuff */
7707ecd9aad4SStephen M. Cameron 	rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
7708ecd9aad4SStephen M. Cameron 	if (rc == 0) {
7709edd16368SStephen M. Cameron 		dac = 1;
7710ecd9aad4SStephen M. Cameron 	} else {
7711ecd9aad4SStephen M. Cameron 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7712ecd9aad4SStephen M. Cameron 		if (rc == 0) {
7713edd16368SStephen M. Cameron 			dac = 0;
7714ecd9aad4SStephen M. Cameron 		} else {
7715edd16368SStephen M. Cameron 			dev_err(&pdev->dev, "no suitable DMA available\n");
7716105a3dbcSRobert Elliott 			goto clean3;	/* pci, lockup, wq/aer/h */
7717edd16368SStephen M. Cameron 		}
7718ecd9aad4SStephen M. Cameron 	}
7719edd16368SStephen M. Cameron 
7720edd16368SStephen M. Cameron 	/* make sure the board interrupts are off */
7721edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
772210f66018SStephen M. Cameron 
7723105a3dbcSRobert Elliott 	rc = hpsa_request_irqs(h, do_hpsa_intr_msi, do_hpsa_intr_intx);
7724105a3dbcSRobert Elliott 	if (rc)
7725105a3dbcSRobert Elliott 		goto clean3;	/* pci, lockup, wq/aer/h */
7726303932fdSDon Brace 	dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
7727303932fdSDon Brace 	       h->devname, pdev->device,
7728a9a3a273SStephen M. Cameron 	       h->intr[h->intr_mode], dac ? "" : " not");
7729d37ffbe4SRobert Elliott 	rc = hpsa_alloc_cmd_pool(h);
77308947fd10SRobert Elliott 	if (rc)
7731105a3dbcSRobert Elliott 		goto clean4;	/* irq, pci, lockup, wq/aer/h */
7732105a3dbcSRobert Elliott 	rc = hpsa_alloc_sg_chain_blocks(h);
7733105a3dbcSRobert Elliott 	if (rc)
7734105a3dbcSRobert Elliott 		goto clean5;	/* cmd, irq, pci, lockup, wq/aer/h */
7735a08a8471SStephen M. Cameron 	init_waitqueue_head(&h->scan_wait_queue);
77369b5c48c2SStephen Cameron 	init_waitqueue_head(&h->abort_cmd_wait_queue);
7737a58e7e53SWebb Scales 	init_waitqueue_head(&h->abort_sync_wait_queue);
7738a08a8471SStephen M. Cameron 	h->scan_finished = 1; /* no scan currently in progress */
7739edd16368SStephen M. Cameron 
7740edd16368SStephen M. Cameron 	pci_set_drvdata(pdev, h);
77419a41338eSStephen M. Cameron 	h->ndevices = 0;
7742316b221aSStephen M. Cameron 	h->hba_mode_enabled = 0;
77439a41338eSStephen M. Cameron 	h->scsi_host = NULL;
77449a41338eSStephen M. Cameron 	spin_lock_init(&h->devlock);
7745105a3dbcSRobert Elliott 	rc = hpsa_put_ctlr_into_performant_mode(h);
7746105a3dbcSRobert Elliott 	if (rc)
7747105a3dbcSRobert Elliott 		goto clean6;	/* sg, cmd, irq, pci, lockup, wq/aer/h */
774864670ac8SStephen M. Cameron 
7749105a3dbcSRobert Elliott 	/*
7750105a3dbcSRobert Elliott 	 * At this point, the controller is ready to take commands.
775164670ac8SStephen M. Cameron 	 * Now, if reset_devices and the hard reset didn't work, try
775264670ac8SStephen M. Cameron 	 * the soft reset and see if that works.
775364670ac8SStephen M. Cameron 	 */
775464670ac8SStephen M. Cameron 	if (try_soft_reset) {
775564670ac8SStephen M. Cameron 
775664670ac8SStephen M. Cameron 		/* This is kind of gross.  We may or may not get a completion
775764670ac8SStephen M. Cameron 		 * from the soft reset command, and if we do, then the value
775864670ac8SStephen M. Cameron 		 * from the fifo may or may not be valid.  So, we wait 10 secs
775964670ac8SStephen M. Cameron 		 * after the reset throwing away any completions we get during
776064670ac8SStephen M. Cameron 		 * that time.  Unregister the interrupt handler and register
776164670ac8SStephen M. Cameron 		 * fake ones to scoop up any residual completions.
776264670ac8SStephen M. Cameron 		 */
776364670ac8SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
776464670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
776564670ac8SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
7766ec501a18SRobert Elliott 		hpsa_free_irqs(h);
77679ee61794SRobert Elliott 		rc = hpsa_request_irqs(h, hpsa_msix_discard_completions,
776864670ac8SStephen M. Cameron 					hpsa_intx_discard_completions);
776964670ac8SStephen M. Cameron 		if (rc) {
77709ee61794SRobert Elliott 			dev_warn(&h->pdev->dev,
77719ee61794SRobert Elliott 				"Failed to request_irq after soft reset.\n");
7772d498757cSRobert Elliott 			/*
7773d498757cSRobert Elliott 			 * clean4 starts with free_irqs, but that was just
7774d498757cSRobert Elliott 			 * done. Then, request_irqs_failed, so there is
7775d498757cSRobert Elliott 			 * nothing to free. So, goto the next label.
7776d498757cSRobert Elliott 			 */
7777d498757cSRobert Elliott 			goto clean3;
777864670ac8SStephen M. Cameron 		}
777964670ac8SStephen M. Cameron 
778064670ac8SStephen M. Cameron 		rc = hpsa_kdump_soft_reset(h);
778164670ac8SStephen M. Cameron 		if (rc)
778264670ac8SStephen M. Cameron 			/* Neither hard nor soft reset worked, we're hosed. */
778364670ac8SStephen M. Cameron 			goto clean4;
778464670ac8SStephen M. Cameron 
778564670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev, "Board READY.\n");
778664670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev,
778764670ac8SStephen M. Cameron 			"Waiting for stale completions to drain.\n");
778864670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_ON);
778964670ac8SStephen M. Cameron 		msleep(10000);
779064670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
779164670ac8SStephen M. Cameron 
779264670ac8SStephen M. Cameron 		rc = controller_reset_failed(h->cfgtable);
779364670ac8SStephen M. Cameron 		if (rc)
779464670ac8SStephen M. Cameron 			dev_info(&h->pdev->dev,
779564670ac8SStephen M. Cameron 				"Soft reset appears to have failed.\n");
779664670ac8SStephen M. Cameron 
779764670ac8SStephen M. Cameron 		/* since the controller's reset, we have to go back and re-init
779864670ac8SStephen M. Cameron 		 * everything.  Easiest to just forget what we've done and do it
779964670ac8SStephen M. Cameron 		 * all over again.
780064670ac8SStephen M. Cameron 		 */
780164670ac8SStephen M. Cameron 		hpsa_undo_allocations_after_kdump_soft_reset(h);
780264670ac8SStephen M. Cameron 		try_soft_reset = 0;
780364670ac8SStephen M. Cameron 		if (rc)
780464670ac8SStephen M. Cameron 			/* don't go to clean4, we already unallocated */
780564670ac8SStephen M. Cameron 			return -ENODEV;
780664670ac8SStephen M. Cameron 
780764670ac8SStephen M. Cameron 		goto reinit_after_soft_reset;
780864670ac8SStephen M. Cameron 	}
7809edd16368SStephen M. Cameron 
7810da0697bdSScott Teel 	/* Enable Accelerated IO path at driver layer */
7811da0697bdSScott Teel 	h->acciopath_status = 1;
7812da0697bdSScott Teel 
7813e863d68eSScott Teel 
7814edd16368SStephen M. Cameron 	/* Turn the interrupts on so we can service requests */
7815edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_ON);
7816edd16368SStephen M. Cameron 
7817339b2b14SStephen M. Cameron 	hpsa_hba_inquiry(h);
78184a4384ceSStephen Cameron 	rc = hpsa_register_scsi(h);	/* hook ourselves into SCSI subsystem */
78194a4384ceSStephen Cameron 	if (rc)
7820105a3dbcSRobert Elliott 		goto clean7;
78218a98db73SStephen M. Cameron 
78228a98db73SStephen M. Cameron 	/* Monitor the controller for firmware lockups */
78238a98db73SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
78248a98db73SStephen M. Cameron 	INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
78258a98db73SStephen M. Cameron 	schedule_delayed_work(&h->monitor_ctlr_work,
78268a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
78276636e7f4SDon Brace 	INIT_DELAYED_WORK(&h->rescan_ctlr_work, hpsa_rescan_ctlr_worker);
78286636e7f4SDon Brace 	queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
78296636e7f4SDon Brace 				h->heartbeat_sample_interval);
783088bf6d62SStephen M. Cameron 	return 0;
7831edd16368SStephen M. Cameron 
7832105a3dbcSRobert Elliott clean7: /* perf, sg, cmd, irq, pci, lockup, wq/aer/h */
7833105a3dbcSRobert Elliott 	kfree(h->hba_inquiry_data);
7834105a3dbcSRobert Elliott 	hpsa_free_performant_mode(h);
7835105a3dbcSRobert Elliott 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
7836105a3dbcSRobert Elliott clean6: /* sg, cmd, irq, pci, lockup, wq/aer/h */
783733a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
7838105a3dbcSRobert Elliott clean5: /* cmd, irq, pci, lockup, wq/aer/h */
78392e9d1b36SStephen M. Cameron 	hpsa_free_cmd_pool(h);
7840105a3dbcSRobert Elliott clean4: /* irq, pci, lockup, wq/aer/h */
7841ec501a18SRobert Elliott 	hpsa_free_irqs(h);
7842105a3dbcSRobert Elliott clean3: /* pci, lockup, wq/aer/h */
7843195f2c65SRobert Elliott 	hpsa_free_pci_init(h);
7844105a3dbcSRobert Elliott clean2: /* lockup, wq/aer/h */
7845105a3dbcSRobert Elliott 	if (h->lockup_detected) {
7846094963daSStephen M. Cameron 		free_percpu(h->lockup_detected);
7847105a3dbcSRobert Elliott 		h->lockup_detected = NULL;
7848105a3dbcSRobert Elliott 	}
7849105a3dbcSRobert Elliott clean1:	/* wq/aer/h */
7850105a3dbcSRobert Elliott 	if (h->resubmit_wq) {
7851105a3dbcSRobert Elliott 		destroy_workqueue(h->resubmit_wq);
7852105a3dbcSRobert Elliott 		h->resubmit_wq = NULL;
7853105a3dbcSRobert Elliott 	}
7854105a3dbcSRobert Elliott 	if (h->rescan_ctlr_wq) {
7855105a3dbcSRobert Elliott 		destroy_workqueue(h->rescan_ctlr_wq);
7856105a3dbcSRobert Elliott 		h->rescan_ctlr_wq = NULL;
7857105a3dbcSRobert Elliott 	}
7858edd16368SStephen M. Cameron 	kfree(h);
7859ecd9aad4SStephen M. Cameron 	return rc;
7860edd16368SStephen M. Cameron }
7861edd16368SStephen M. Cameron 
7862edd16368SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h)
7863edd16368SStephen M. Cameron {
7864edd16368SStephen M. Cameron 	char *flush_buf;
7865edd16368SStephen M. Cameron 	struct CommandList *c;
786625163bd5SWebb Scales 	int rc;
7867702890e3SStephen M. Cameron 
7868094963daSStephen M. Cameron 	if (unlikely(lockup_detected(h)))
7869702890e3SStephen M. Cameron 		return;
7870edd16368SStephen M. Cameron 	flush_buf = kzalloc(4, GFP_KERNEL);
7871edd16368SStephen M. Cameron 	if (!flush_buf)
7872edd16368SStephen M. Cameron 		return;
7873edd16368SStephen M. Cameron 
787445fcb86eSStephen Cameron 	c = cmd_alloc(h);
7875bf43caf3SRobert Elliott 
7876a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
7877a2dac136SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_CMD)) {
7878a2dac136SStephen M. Cameron 		goto out;
7879a2dac136SStephen M. Cameron 	}
788025163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
788125163bd5SWebb Scales 					PCI_DMA_TODEVICE, NO_TIMEOUT);
788225163bd5SWebb Scales 	if (rc)
788325163bd5SWebb Scales 		goto out;
7884edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus != 0)
7885a2dac136SStephen M. Cameron out:
7886edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
7887edd16368SStephen M. Cameron 			"error flushing cache on controller\n");
788845fcb86eSStephen Cameron 	cmd_free(h, c);
7889edd16368SStephen M. Cameron 	kfree(flush_buf);
7890edd16368SStephen M. Cameron }
7891edd16368SStephen M. Cameron 
7892edd16368SStephen M. Cameron static void hpsa_shutdown(struct pci_dev *pdev)
7893edd16368SStephen M. Cameron {
7894edd16368SStephen M. Cameron 	struct ctlr_info *h;
7895edd16368SStephen M. Cameron 
7896edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
7897edd16368SStephen M. Cameron 	/* Turn board interrupts off  and send the flush cache command
7898edd16368SStephen M. Cameron 	 * sendcmd will turn off interrupt, and send the flush...
7899edd16368SStephen M. Cameron 	 * To write all data in the battery backed cache to disks
7900edd16368SStephen M. Cameron 	 */
7901edd16368SStephen M. Cameron 	hpsa_flush_cache(h);
7902edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
7903105a3dbcSRobert Elliott 	hpsa_free_irqs(h);			/* init_one 4 */
7904cc64c817SRobert Elliott 	hpsa_disable_interrupt_mode(h);		/* pci_init 2 */
7905edd16368SStephen M. Cameron }
7906edd16368SStephen M. Cameron 
79076f039790SGreg Kroah-Hartman static void hpsa_free_device_info(struct ctlr_info *h)
790855e14e76SStephen M. Cameron {
790955e14e76SStephen M. Cameron 	int i;
791055e14e76SStephen M. Cameron 
7911105a3dbcSRobert Elliott 	for (i = 0; i < h->ndevices; i++) {
791255e14e76SStephen M. Cameron 		kfree(h->dev[i]);
7913105a3dbcSRobert Elliott 		h->dev[i] = NULL;
7914105a3dbcSRobert Elliott 	}
791555e14e76SStephen M. Cameron }
791655e14e76SStephen M. Cameron 
79176f039790SGreg Kroah-Hartman static void hpsa_remove_one(struct pci_dev *pdev)
7918edd16368SStephen M. Cameron {
7919edd16368SStephen M. Cameron 	struct ctlr_info *h;
79208a98db73SStephen M. Cameron 	unsigned long flags;
7921edd16368SStephen M. Cameron 
7922edd16368SStephen M. Cameron 	if (pci_get_drvdata(pdev) == NULL) {
7923edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "unable to remove device\n");
7924edd16368SStephen M. Cameron 		return;
7925edd16368SStephen M. Cameron 	}
7926edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
79278a98db73SStephen M. Cameron 
79288a98db73SStephen M. Cameron 	/* Get rid of any controller monitoring work items */
79298a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
79308a98db73SStephen M. Cameron 	h->remove_in_progress = 1;
79318a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
79326636e7f4SDon Brace 	cancel_delayed_work_sync(&h->monitor_ctlr_work);
79336636e7f4SDon Brace 	cancel_delayed_work_sync(&h->rescan_ctlr_work);
79346636e7f4SDon Brace 	destroy_workqueue(h->rescan_ctlr_wq);
79356636e7f4SDon Brace 	destroy_workqueue(h->resubmit_wq);
7936cc64c817SRobert Elliott 
7937105a3dbcSRobert Elliott 	/* includes hpsa_free_irqs - init_one 4 */
7938195f2c65SRobert Elliott 	/* includes hpsa_disable_interrupt_mode - pci_init 2 */
7939edd16368SStephen M. Cameron 	hpsa_shutdown(pdev);
7940cc64c817SRobert Elliott 
7941105a3dbcSRobert Elliott 	hpsa_free_device_info(h);		/* scan */
7942105a3dbcSRobert Elliott 
7943105a3dbcSRobert Elliott 	hpsa_unregister_scsi(h);			/* init_one "8" */
7944105a3dbcSRobert Elliott 	kfree(h->hba_inquiry_data);			/* init_one "8" */
7945105a3dbcSRobert Elliott 	h->hba_inquiry_data = NULL;			/* init_one "8" */
7946105a3dbcSRobert Elliott 	hpsa_free_performant_mode(h);			/* init_one 7 */
7947105a3dbcSRobert Elliott 	hpsa_free_sg_chain_blocks(h);			/* init_one 6 */
79481fb7c98aSRobert Elliott 	hpsa_free_cmd_pool(h);				/* init_one 5 */
7949105a3dbcSRobert Elliott 
7950105a3dbcSRobert Elliott 	/* hpsa_free_irqs already called via hpsa_shutdown init_one 4 */
7951195f2c65SRobert Elliott 
7952195f2c65SRobert Elliott 	/* includes hpsa_disable_interrupt_mode - pci_init 2 */
7953105a3dbcSRobert Elliott 	hpsa_free_pci_init(h);				/* init_one 3 */
7954195f2c65SRobert Elliott 
7955105a3dbcSRobert Elliott 	free_percpu(h->lockup_detected);		/* init_one 2 */
7956105a3dbcSRobert Elliott 	h->lockup_detected = NULL;			/* init_one 2 */
7957105a3dbcSRobert Elliott 	/* (void) pci_disable_pcie_error_reporting(pdev); */	/* init_one 1 */
7958105a3dbcSRobert Elliott 	kfree(h);					/* init_one 1 */
7959edd16368SStephen M. Cameron }
7960edd16368SStephen M. Cameron 
7961edd16368SStephen M. Cameron static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
7962edd16368SStephen M. Cameron 	__attribute__((unused)) pm_message_t state)
7963edd16368SStephen M. Cameron {
7964edd16368SStephen M. Cameron 	return -ENOSYS;
7965edd16368SStephen M. Cameron }
7966edd16368SStephen M. Cameron 
7967edd16368SStephen M. Cameron static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
7968edd16368SStephen M. Cameron {
7969edd16368SStephen M. Cameron 	return -ENOSYS;
7970edd16368SStephen M. Cameron }
7971edd16368SStephen M. Cameron 
7972edd16368SStephen M. Cameron static struct pci_driver hpsa_pci_driver = {
7973f79cfec6SStephen M. Cameron 	.name = HPSA,
7974edd16368SStephen M. Cameron 	.probe = hpsa_init_one,
79756f039790SGreg Kroah-Hartman 	.remove = hpsa_remove_one,
7976edd16368SStephen M. Cameron 	.id_table = hpsa_pci_device_id,	/* id_table */
7977edd16368SStephen M. Cameron 	.shutdown = hpsa_shutdown,
7978edd16368SStephen M. Cameron 	.suspend = hpsa_suspend,
7979edd16368SStephen M. Cameron 	.resume = hpsa_resume,
7980edd16368SStephen M. Cameron };
7981edd16368SStephen M. Cameron 
7982303932fdSDon Brace /* Fill in bucket_map[], given nsgs (the max number of
7983303932fdSDon Brace  * scatter gather elements supported) and bucket[],
7984303932fdSDon Brace  * which is an array of 8 integers.  The bucket[] array
7985303932fdSDon Brace  * contains 8 different DMA transfer sizes (in 16
7986303932fdSDon Brace  * byte increments) which the controller uses to fetch
7987303932fdSDon Brace  * commands.  This function fills in bucket_map[], which
7988303932fdSDon Brace  * maps a given number of scatter gather elements to one of
7989303932fdSDon Brace  * the 8 DMA transfer sizes.  The point of it is to allow the
7990303932fdSDon Brace  * controller to only do as much DMA as needed to fetch the
7991303932fdSDon Brace  * command, with the DMA transfer size encoded in the lower
7992303932fdSDon Brace  * bits of the command address.
7993303932fdSDon Brace  */
7994303932fdSDon Brace static void  calc_bucket_map(int bucket[], int num_buckets,
79952b08b3e9SDon Brace 	int nsgs, int min_blocks, u32 *bucket_map)
7996303932fdSDon Brace {
7997303932fdSDon Brace 	int i, j, b, size;
7998303932fdSDon Brace 
7999303932fdSDon Brace 	/* Note, bucket_map must have nsgs+1 entries. */
8000303932fdSDon Brace 	for (i = 0; i <= nsgs; i++) {
8001303932fdSDon Brace 		/* Compute size of a command with i SG entries */
8002e1f7de0cSMatt Gates 		size = i + min_blocks;
8003303932fdSDon Brace 		b = num_buckets; /* Assume the biggest bucket */
8004303932fdSDon Brace 		/* Find the bucket that is just big enough */
8005e1f7de0cSMatt Gates 		for (j = 0; j < num_buckets; j++) {
8006303932fdSDon Brace 			if (bucket[j] >= size) {
8007303932fdSDon Brace 				b = j;
8008303932fdSDon Brace 				break;
8009303932fdSDon Brace 			}
8010303932fdSDon Brace 		}
8011303932fdSDon Brace 		/* for a command with i SG entries, use bucket b. */
8012303932fdSDon Brace 		bucket_map[i] = b;
8013303932fdSDon Brace 	}
8014303932fdSDon Brace }
8015303932fdSDon Brace 
8016105a3dbcSRobert Elliott /*
8017105a3dbcSRobert Elliott  * return -ENODEV on err, 0 on success (or no action)
8018105a3dbcSRobert Elliott  * allocates numerous items that must be freed later
8019105a3dbcSRobert Elliott  */
8020c706a795SRobert Elliott static int hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
8021303932fdSDon Brace {
80226c311b57SStephen M. Cameron 	int i;
80236c311b57SStephen M. Cameron 	unsigned long register_value;
8024e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
8025e1f7de0cSMatt Gates 			(trans_support & CFGTBL_Trans_use_short_tags) |
8026e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix |
8027b9af4937SStephen M. Cameron 			(trans_support & (CFGTBL_Trans_io_accel1 |
8028b9af4937SStephen M. Cameron 				CFGTBL_Trans_io_accel2));
8029e1f7de0cSMatt Gates 	struct access_method access = SA5_performant_access;
8030def342bdSStephen M. Cameron 
8031def342bdSStephen M. Cameron 	/* This is a bit complicated.  There are 8 registers on
8032def342bdSStephen M. Cameron 	 * the controller which we write to to tell it 8 different
8033def342bdSStephen M. Cameron 	 * sizes of commands which there may be.  It's a way of
8034def342bdSStephen M. Cameron 	 * reducing the DMA done to fetch each command.  Encoded into
8035def342bdSStephen M. Cameron 	 * each command's tag are 3 bits which communicate to the controller
8036def342bdSStephen M. Cameron 	 * which of the eight sizes that command fits within.  The size of
8037def342bdSStephen M. Cameron 	 * each command depends on how many scatter gather entries there are.
8038def342bdSStephen M. Cameron 	 * Each SG entry requires 16 bytes.  The eight registers are programmed
8039def342bdSStephen M. Cameron 	 * with the number of 16-byte blocks a command of that size requires.
8040def342bdSStephen M. Cameron 	 * The smallest command possible requires 5 such 16 byte blocks.
8041d66ae08bSStephen M. Cameron 	 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
8042def342bdSStephen M. Cameron 	 * blocks.  Note, this only extends to the SG entries contained
8043def342bdSStephen M. Cameron 	 * within the command block, and does not extend to chained blocks
8044def342bdSStephen M. Cameron 	 * of SG elements.   bft[] contains the eight values we write to
8045def342bdSStephen M. Cameron 	 * the registers.  They are not evenly distributed, but have more
8046def342bdSStephen M. Cameron 	 * sizes for small commands, and fewer sizes for larger commands.
8047def342bdSStephen M. Cameron 	 */
8048d66ae08bSStephen M. Cameron 	int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
8049b9af4937SStephen M. Cameron #define MIN_IOACCEL2_BFT_ENTRY 5
8050b9af4937SStephen M. Cameron #define HPSA_IOACCEL2_HEADER_SZ 4
8051b9af4937SStephen M. Cameron 	int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
8052b9af4937SStephen M. Cameron 			13, 14, 15, 16, 17, 18, 19,
8053b9af4937SStephen M. Cameron 			HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
8054b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
8055b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
8056b9af4937SStephen M. Cameron 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
8057b9af4937SStephen M. Cameron 				 16 * MIN_IOACCEL2_BFT_ENTRY);
8058b9af4937SStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
8059d66ae08bSStephen M. Cameron 	BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
8060303932fdSDon Brace 	/*  5 = 1 s/g entry or 4k
8061303932fdSDon Brace 	 *  6 = 2 s/g entry or 8k
8062303932fdSDon Brace 	 *  8 = 4 s/g entry or 16k
8063303932fdSDon Brace 	 * 10 = 6 s/g entry or 24k
8064303932fdSDon Brace 	 */
8065303932fdSDon Brace 
8066b3a52e79SStephen M. Cameron 	/* If the controller supports either ioaccel method then
8067b3a52e79SStephen M. Cameron 	 * we can also use the RAID stack submit path that does not
8068b3a52e79SStephen M. Cameron 	 * perform the superfluous readl() after each command submission.
8069b3a52e79SStephen M. Cameron 	 */
8070b3a52e79SStephen M. Cameron 	if (trans_support & (CFGTBL_Trans_io_accel1 | CFGTBL_Trans_io_accel2))
8071b3a52e79SStephen M. Cameron 		access = SA5_performant_access_no_read;
8072b3a52e79SStephen M. Cameron 
8073303932fdSDon Brace 	/* Controller spec: zero out this buffer. */
8074072b0518SStephen M. Cameron 	for (i = 0; i < h->nreply_queues; i++)
8075072b0518SStephen M. Cameron 		memset(h->reply_queue[i].head, 0, h->reply_queue_size);
8076303932fdSDon Brace 
8077d66ae08bSStephen M. Cameron 	bft[7] = SG_ENTRIES_IN_CMD + 4;
8078d66ae08bSStephen M. Cameron 	calc_bucket_map(bft, ARRAY_SIZE(bft),
8079e1f7de0cSMatt Gates 				SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
8080303932fdSDon Brace 	for (i = 0; i < 8; i++)
8081303932fdSDon Brace 		writel(bft[i], &h->transtable->BlockFetch[i]);
8082303932fdSDon Brace 
8083303932fdSDon Brace 	/* size of controller ring buffer */
8084303932fdSDon Brace 	writel(h->max_commands, &h->transtable->RepQSize);
8085254f796bSMatt Gates 	writel(h->nreply_queues, &h->transtable->RepQCount);
8086303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrLow32);
8087303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrHigh32);
8088254f796bSMatt Gates 
8089254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
8090254f796bSMatt Gates 		writel(0, &h->transtable->RepQAddr[i].upper);
8091072b0518SStephen M. Cameron 		writel(h->reply_queue[i].busaddr,
8092254f796bSMatt Gates 			&h->transtable->RepQAddr[i].lower);
8093254f796bSMatt Gates 	}
8094254f796bSMatt Gates 
8095b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
8096e1f7de0cSMatt Gates 	writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
8097e1f7de0cSMatt Gates 	/*
8098e1f7de0cSMatt Gates 	 * enable outbound interrupt coalescing in accelerator mode;
8099e1f7de0cSMatt Gates 	 */
8100e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
8101e1f7de0cSMatt Gates 		access = SA5_ioaccel_mode1_access;
8102e1f7de0cSMatt Gates 		writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
8103e1f7de0cSMatt Gates 		writel(4, &h->cfgtable->HostWrite.CoalIntCount);
8104c349775eSScott Teel 	} else {
8105c349775eSScott Teel 		if (trans_support & CFGTBL_Trans_io_accel2) {
8106c349775eSScott Teel 			access = SA5_ioaccel_mode2_access;
8107c349775eSScott Teel 			writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
8108c349775eSScott Teel 			writel(4, &h->cfgtable->HostWrite.CoalIntCount);
8109c349775eSScott Teel 		}
8110e1f7de0cSMatt Gates 	}
8111303932fdSDon Brace 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
8112c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h)) {
8113c706a795SRobert Elliott 		dev_err(&h->pdev->dev,
8114c706a795SRobert Elliott 			"performant mode problem - doorbell timeout\n");
8115c706a795SRobert Elliott 		return -ENODEV;
8116c706a795SRobert Elliott 	}
8117303932fdSDon Brace 	register_value = readl(&(h->cfgtable->TransportActive));
8118303932fdSDon Brace 	if (!(register_value & CFGTBL_Trans_Performant)) {
8119050f7147SStephen Cameron 		dev_err(&h->pdev->dev,
8120050f7147SStephen Cameron 			"performant mode problem - transport not active\n");
8121c706a795SRobert Elliott 		return -ENODEV;
8122303932fdSDon Brace 	}
8123960a30e7SStephen M. Cameron 	/* Change the access methods to the performant access methods */
8124e1f7de0cSMatt Gates 	h->access = access;
8125e1f7de0cSMatt Gates 	h->transMethod = transMethod;
8126e1f7de0cSMatt Gates 
8127b9af4937SStephen M. Cameron 	if (!((trans_support & CFGTBL_Trans_io_accel1) ||
8128b9af4937SStephen M. Cameron 		(trans_support & CFGTBL_Trans_io_accel2)))
8129c706a795SRobert Elliott 		return 0;
8130e1f7de0cSMatt Gates 
8131b9af4937SStephen M. Cameron 	if (trans_support & CFGTBL_Trans_io_accel1) {
8132e1f7de0cSMatt Gates 		/* Set up I/O accelerator mode */
8133e1f7de0cSMatt Gates 		for (i = 0; i < h->nreply_queues; i++) {
8134e1f7de0cSMatt Gates 			writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
8135e1f7de0cSMatt Gates 			h->reply_queue[i].current_entry =
8136e1f7de0cSMatt Gates 				readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
8137e1f7de0cSMatt Gates 		}
8138283b4a9bSStephen M. Cameron 		bft[7] = h->ioaccel_maxsg + 8;
8139283b4a9bSStephen M. Cameron 		calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
8140e1f7de0cSMatt Gates 				h->ioaccel1_blockFetchTable);
8141e1f7de0cSMatt Gates 
8142e1f7de0cSMatt Gates 		/* initialize all reply queue entries to unused */
8143072b0518SStephen M. Cameron 		for (i = 0; i < h->nreply_queues; i++)
8144072b0518SStephen M. Cameron 			memset(h->reply_queue[i].head,
8145072b0518SStephen M. Cameron 				(u8) IOACCEL_MODE1_REPLY_UNUSED,
8146072b0518SStephen M. Cameron 				h->reply_queue_size);
8147e1f7de0cSMatt Gates 
8148e1f7de0cSMatt Gates 		/* set all the constant fields in the accelerator command
8149e1f7de0cSMatt Gates 		 * frames once at init time to save CPU cycles later.
8150e1f7de0cSMatt Gates 		 */
8151e1f7de0cSMatt Gates 		for (i = 0; i < h->nr_cmds; i++) {
8152e1f7de0cSMatt Gates 			struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
8153e1f7de0cSMatt Gates 
8154e1f7de0cSMatt Gates 			cp->function = IOACCEL1_FUNCTION_SCSIIO;
8155e1f7de0cSMatt Gates 			cp->err_info = (u32) (h->errinfo_pool_dhandle +
8156e1f7de0cSMatt Gates 					(i * sizeof(struct ErrorInfo)));
8157e1f7de0cSMatt Gates 			cp->err_info_len = sizeof(struct ErrorInfo);
8158e1f7de0cSMatt Gates 			cp->sgl_offset = IOACCEL1_SGLOFFSET;
81592b08b3e9SDon Brace 			cp->host_context_flags =
81602b08b3e9SDon Brace 				cpu_to_le16(IOACCEL1_HCFLAGS_CISS_FORMAT);
8161e1f7de0cSMatt Gates 			cp->timeout_sec = 0;
8162e1f7de0cSMatt Gates 			cp->ReplyQueue = 0;
816350a0decfSStephen M. Cameron 			cp->tag =
8164f2405db8SDon Brace 				cpu_to_le64((i << DIRECT_LOOKUP_SHIFT));
816550a0decfSStephen M. Cameron 			cp->host_addr =
816650a0decfSStephen M. Cameron 				cpu_to_le64(h->ioaccel_cmd_pool_dhandle +
8167e1f7de0cSMatt Gates 					(i * sizeof(struct io_accel1_cmd)));
8168e1f7de0cSMatt Gates 		}
8169b9af4937SStephen M. Cameron 	} else if (trans_support & CFGTBL_Trans_io_accel2) {
8170b9af4937SStephen M. Cameron 		u64 cfg_offset, cfg_base_addr_index;
8171b9af4937SStephen M. Cameron 		u32 bft2_offset, cfg_base_addr;
8172b9af4937SStephen M. Cameron 		int rc;
8173b9af4937SStephen M. Cameron 
8174b9af4937SStephen M. Cameron 		rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
8175b9af4937SStephen M. Cameron 			&cfg_base_addr_index, &cfg_offset);
8176b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
8177b9af4937SStephen M. Cameron 		bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
8178b9af4937SStephen M. Cameron 		calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
8179b9af4937SStephen M. Cameron 				4, h->ioaccel2_blockFetchTable);
8180b9af4937SStephen M. Cameron 		bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
8181b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct CfgTable,
8182b9af4937SStephen M. Cameron 				io_accel_request_size_offset) != 0xb8);
8183b9af4937SStephen M. Cameron 		h->ioaccel2_bft2_regs =
8184b9af4937SStephen M. Cameron 			remap_pci_mem(pci_resource_start(h->pdev,
8185b9af4937SStephen M. Cameron 					cfg_base_addr_index) +
8186b9af4937SStephen M. Cameron 					cfg_offset + bft2_offset,
8187b9af4937SStephen M. Cameron 					ARRAY_SIZE(bft2) *
8188b9af4937SStephen M. Cameron 					sizeof(*h->ioaccel2_bft2_regs));
8189b9af4937SStephen M. Cameron 		for (i = 0; i < ARRAY_SIZE(bft2); i++)
8190b9af4937SStephen M. Cameron 			writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
8191b9af4937SStephen M. Cameron 	}
8192b9af4937SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
8193c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h)) {
8194c706a795SRobert Elliott 		dev_err(&h->pdev->dev,
8195c706a795SRobert Elliott 			"performant mode problem - enabling ioaccel mode\n");
8196c706a795SRobert Elliott 		return -ENODEV;
8197c706a795SRobert Elliott 	}
8198c706a795SRobert Elliott 	return 0;
8199e1f7de0cSMatt Gates }
8200e1f7de0cSMatt Gates 
82011fb7c98aSRobert Elliott /* Free ioaccel1 mode command blocks and block fetch table */
82021fb7c98aSRobert Elliott static void hpsa_free_ioaccel1_cmd_and_bft(struct ctlr_info *h)
82031fb7c98aSRobert Elliott {
8204105a3dbcSRobert Elliott 	if (h->ioaccel_cmd_pool) {
82051fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
82061fb7c98aSRobert Elliott 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
82071fb7c98aSRobert Elliott 			h->ioaccel_cmd_pool,
82081fb7c98aSRobert Elliott 			h->ioaccel_cmd_pool_dhandle);
8209105a3dbcSRobert Elliott 		h->ioaccel_cmd_pool = NULL;
8210105a3dbcSRobert Elliott 		h->ioaccel_cmd_pool_dhandle = 0;
8211105a3dbcSRobert Elliott 	}
82121fb7c98aSRobert Elliott 	kfree(h->ioaccel1_blockFetchTable);
8213105a3dbcSRobert Elliott 	h->ioaccel1_blockFetchTable = NULL;
82141fb7c98aSRobert Elliott }
82151fb7c98aSRobert Elliott 
8216d37ffbe4SRobert Elliott /* Allocate ioaccel1 mode command blocks and block fetch table */
8217d37ffbe4SRobert Elliott static int hpsa_alloc_ioaccel1_cmd_and_bft(struct ctlr_info *h)
8218e1f7de0cSMatt Gates {
8219283b4a9bSStephen M. Cameron 	h->ioaccel_maxsg =
8220283b4a9bSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
8221283b4a9bSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
8222283b4a9bSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
8223283b4a9bSStephen M. Cameron 
8224e1f7de0cSMatt Gates 	/* Command structures must be aligned on a 128-byte boundary
8225e1f7de0cSMatt Gates 	 * because the 7 lower bits of the address are used by the
8226e1f7de0cSMatt Gates 	 * hardware.
8227e1f7de0cSMatt Gates 	 */
8228e1f7de0cSMatt Gates 	BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
8229e1f7de0cSMatt Gates 			IOACCEL1_COMMANDLIST_ALIGNMENT);
8230e1f7de0cSMatt Gates 	h->ioaccel_cmd_pool =
8231e1f7de0cSMatt Gates 		pci_alloc_consistent(h->pdev,
8232e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
8233e1f7de0cSMatt Gates 			&(h->ioaccel_cmd_pool_dhandle));
8234e1f7de0cSMatt Gates 
8235e1f7de0cSMatt Gates 	h->ioaccel1_blockFetchTable =
8236283b4a9bSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
8237e1f7de0cSMatt Gates 				sizeof(u32)), GFP_KERNEL);
8238e1f7de0cSMatt Gates 
8239e1f7de0cSMatt Gates 	if ((h->ioaccel_cmd_pool == NULL) ||
8240e1f7de0cSMatt Gates 		(h->ioaccel1_blockFetchTable == NULL))
8241e1f7de0cSMatt Gates 		goto clean_up;
8242e1f7de0cSMatt Gates 
8243e1f7de0cSMatt Gates 	memset(h->ioaccel_cmd_pool, 0,
8244e1f7de0cSMatt Gates 		h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
8245e1f7de0cSMatt Gates 	return 0;
8246e1f7de0cSMatt Gates 
8247e1f7de0cSMatt Gates clean_up:
82481fb7c98aSRobert Elliott 	hpsa_free_ioaccel1_cmd_and_bft(h);
82492dd02d74SRobert Elliott 	return -ENOMEM;
82506c311b57SStephen M. Cameron }
82516c311b57SStephen M. Cameron 
82521fb7c98aSRobert Elliott /* Free ioaccel2 mode command blocks and block fetch table */
82531fb7c98aSRobert Elliott static void hpsa_free_ioaccel2_cmd_and_bft(struct ctlr_info *h)
82541fb7c98aSRobert Elliott {
8255d9a729f3SWebb Scales 	hpsa_free_ioaccel2_sg_chain_blocks(h);
8256d9a729f3SWebb Scales 
8257105a3dbcSRobert Elliott 	if (h->ioaccel2_cmd_pool) {
82581fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
82591fb7c98aSRobert Elliott 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
82601fb7c98aSRobert Elliott 			h->ioaccel2_cmd_pool,
82611fb7c98aSRobert Elliott 			h->ioaccel2_cmd_pool_dhandle);
8262105a3dbcSRobert Elliott 		h->ioaccel2_cmd_pool = NULL;
8263105a3dbcSRobert Elliott 		h->ioaccel2_cmd_pool_dhandle = 0;
8264105a3dbcSRobert Elliott 	}
82651fb7c98aSRobert Elliott 	kfree(h->ioaccel2_blockFetchTable);
8266105a3dbcSRobert Elliott 	h->ioaccel2_blockFetchTable = NULL;
82671fb7c98aSRobert Elliott }
82681fb7c98aSRobert Elliott 
8269d37ffbe4SRobert Elliott /* Allocate ioaccel2 mode command blocks and block fetch table */
8270d37ffbe4SRobert Elliott static int hpsa_alloc_ioaccel2_cmd_and_bft(struct ctlr_info *h)
8271aca9012aSStephen M. Cameron {
8272d9a729f3SWebb Scales 	int rc;
8273d9a729f3SWebb Scales 
8274aca9012aSStephen M. Cameron 	/* Allocate ioaccel2 mode command blocks and block fetch table */
8275aca9012aSStephen M. Cameron 
8276aca9012aSStephen M. Cameron 	h->ioaccel_maxsg =
8277aca9012aSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
8278aca9012aSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
8279aca9012aSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
8280aca9012aSStephen M. Cameron 
8281aca9012aSStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
8282aca9012aSStephen M. Cameron 			IOACCEL2_COMMANDLIST_ALIGNMENT);
8283aca9012aSStephen M. Cameron 	h->ioaccel2_cmd_pool =
8284aca9012aSStephen M. Cameron 		pci_alloc_consistent(h->pdev,
8285aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
8286aca9012aSStephen M. Cameron 			&(h->ioaccel2_cmd_pool_dhandle));
8287aca9012aSStephen M. Cameron 
8288aca9012aSStephen M. Cameron 	h->ioaccel2_blockFetchTable =
8289aca9012aSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
8290aca9012aSStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
8291aca9012aSStephen M. Cameron 
8292aca9012aSStephen M. Cameron 	if ((h->ioaccel2_cmd_pool == NULL) ||
8293d9a729f3SWebb Scales 		(h->ioaccel2_blockFetchTable == NULL)) {
8294d9a729f3SWebb Scales 		rc = -ENOMEM;
8295d9a729f3SWebb Scales 		goto clean_up;
8296d9a729f3SWebb Scales 	}
8297d9a729f3SWebb Scales 
8298d9a729f3SWebb Scales 	rc = hpsa_allocate_ioaccel2_sg_chain_blocks(h);
8299d9a729f3SWebb Scales 	if (rc)
8300aca9012aSStephen M. Cameron 		goto clean_up;
8301aca9012aSStephen M. Cameron 
8302aca9012aSStephen M. Cameron 	memset(h->ioaccel2_cmd_pool, 0,
8303aca9012aSStephen M. Cameron 		h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
8304aca9012aSStephen M. Cameron 	return 0;
8305aca9012aSStephen M. Cameron 
8306aca9012aSStephen M. Cameron clean_up:
83071fb7c98aSRobert Elliott 	hpsa_free_ioaccel2_cmd_and_bft(h);
8308d9a729f3SWebb Scales 	return rc;
8309aca9012aSStephen M. Cameron }
8310aca9012aSStephen M. Cameron 
8311105a3dbcSRobert Elliott /* Free items allocated by hpsa_put_ctlr_into_performant_mode */
8312105a3dbcSRobert Elliott static void hpsa_free_performant_mode(struct ctlr_info *h)
8313105a3dbcSRobert Elliott {
8314105a3dbcSRobert Elliott 	kfree(h->blockFetchTable);
8315105a3dbcSRobert Elliott 	h->blockFetchTable = NULL;
8316105a3dbcSRobert Elliott 	hpsa_free_reply_queues(h);
8317105a3dbcSRobert Elliott 	hpsa_free_ioaccel1_cmd_and_bft(h);
8318105a3dbcSRobert Elliott 	hpsa_free_ioaccel2_cmd_and_bft(h);
8319105a3dbcSRobert Elliott }
8320105a3dbcSRobert Elliott 
8321105a3dbcSRobert Elliott /* return -ENODEV on error, 0 on success (or no action)
8322105a3dbcSRobert Elliott  * allocates numerous items that must be freed later
8323105a3dbcSRobert Elliott  */
8324105a3dbcSRobert Elliott static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
83256c311b57SStephen M. Cameron {
83266c311b57SStephen M. Cameron 	u32 trans_support;
8327e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
8328e1f7de0cSMatt Gates 					CFGTBL_Trans_use_short_tags;
8329105a3dbcSRobert Elliott 	int i, rc;
83306c311b57SStephen M. Cameron 
833102ec19c8SStephen M. Cameron 	if (hpsa_simple_mode)
8332105a3dbcSRobert Elliott 		return 0;
833302ec19c8SStephen M. Cameron 
833467c99a72Sscameron@beardog.cce.hp.com 	trans_support = readl(&(h->cfgtable->TransportSupport));
833567c99a72Sscameron@beardog.cce.hp.com 	if (!(trans_support & PERFORMANT_MODE))
8336105a3dbcSRobert Elliott 		return 0;
833767c99a72Sscameron@beardog.cce.hp.com 
8338e1f7de0cSMatt Gates 	/* Check for I/O accelerator mode support */
8339e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
8340e1f7de0cSMatt Gates 		transMethod |= CFGTBL_Trans_io_accel1 |
8341e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix;
8342105a3dbcSRobert Elliott 		rc = hpsa_alloc_ioaccel1_cmd_and_bft(h);
8343105a3dbcSRobert Elliott 		if (rc)
8344105a3dbcSRobert Elliott 			return rc;
8345105a3dbcSRobert Elliott 	} else if (trans_support & CFGTBL_Trans_io_accel2) {
8346aca9012aSStephen M. Cameron 		transMethod |= CFGTBL_Trans_io_accel2 |
8347aca9012aSStephen M. Cameron 				CFGTBL_Trans_enable_directed_msix;
8348105a3dbcSRobert Elliott 		rc = hpsa_alloc_ioaccel2_cmd_and_bft(h);
8349105a3dbcSRobert Elliott 		if (rc)
8350105a3dbcSRobert Elliott 			return rc;
8351e1f7de0cSMatt Gates 	}
8352e1f7de0cSMatt Gates 
8353eee0f03aSHannes Reinecke 	h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
8354cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
83556c311b57SStephen M. Cameron 	/* Performant mode ring buffer and supporting data structures */
8356072b0518SStephen M. Cameron 	h->reply_queue_size = h->max_commands * sizeof(u64);
83576c311b57SStephen M. Cameron 
8358254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
8359072b0518SStephen M. Cameron 		h->reply_queue[i].head = pci_alloc_consistent(h->pdev,
8360072b0518SStephen M. Cameron 						h->reply_queue_size,
8361072b0518SStephen M. Cameron 						&(h->reply_queue[i].busaddr));
8362105a3dbcSRobert Elliott 		if (!h->reply_queue[i].head) {
8363105a3dbcSRobert Elliott 			rc = -ENOMEM;
8364105a3dbcSRobert Elliott 			goto clean1;	/* rq, ioaccel */
8365105a3dbcSRobert Elliott 		}
8366254f796bSMatt Gates 		h->reply_queue[i].size = h->max_commands;
8367254f796bSMatt Gates 		h->reply_queue[i].wraparound = 1;  /* spec: init to 1 */
8368254f796bSMatt Gates 		h->reply_queue[i].current_entry = 0;
8369254f796bSMatt Gates 	}
8370254f796bSMatt Gates 
83716c311b57SStephen M. Cameron 	/* Need a block fetch table for performant mode */
8372d66ae08bSStephen M. Cameron 	h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
83736c311b57SStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
8374105a3dbcSRobert Elliott 	if (!h->blockFetchTable) {
8375105a3dbcSRobert Elliott 		rc = -ENOMEM;
8376105a3dbcSRobert Elliott 		goto clean1;	/* rq, ioaccel */
8377105a3dbcSRobert Elliott 	}
83786c311b57SStephen M. Cameron 
8379105a3dbcSRobert Elliott 	rc = hpsa_enter_performant_mode(h, trans_support);
8380105a3dbcSRobert Elliott 	if (rc)
8381105a3dbcSRobert Elliott 		goto clean2;	/* bft, rq, ioaccel */
8382105a3dbcSRobert Elliott 	return 0;
8383303932fdSDon Brace 
8384105a3dbcSRobert Elliott clean2:	/* bft, rq, ioaccel */
8385303932fdSDon Brace 	kfree(h->blockFetchTable);
8386105a3dbcSRobert Elliott 	h->blockFetchTable = NULL;
8387105a3dbcSRobert Elliott clean1:	/* rq, ioaccel */
8388105a3dbcSRobert Elliott 	hpsa_free_reply_queues(h);
8389105a3dbcSRobert Elliott 	hpsa_free_ioaccel1_cmd_and_bft(h);
8390105a3dbcSRobert Elliott 	hpsa_free_ioaccel2_cmd_and_bft(h);
8391105a3dbcSRobert Elliott 	return rc;
8392303932fdSDon Brace }
8393303932fdSDon Brace 
839423100dd9SStephen M. Cameron static int is_accelerated_cmd(struct CommandList *c)
839576438d08SStephen M. Cameron {
839623100dd9SStephen M. Cameron 	return c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_IOACCEL2;
839723100dd9SStephen M. Cameron }
839823100dd9SStephen M. Cameron 
839923100dd9SStephen M. Cameron static void hpsa_drain_accel_commands(struct ctlr_info *h)
840023100dd9SStephen M. Cameron {
840123100dd9SStephen M. Cameron 	struct CommandList *c = NULL;
8402f2405db8SDon Brace 	int i, accel_cmds_out;
8403281a7fd0SWebb Scales 	int refcount;
840476438d08SStephen M. Cameron 
8405f2405db8SDon Brace 	do { /* wait for all outstanding ioaccel commands to drain out */
840623100dd9SStephen M. Cameron 		accel_cmds_out = 0;
8407f2405db8SDon Brace 		for (i = 0; i < h->nr_cmds; i++) {
8408f2405db8SDon Brace 			c = h->cmd_pool + i;
8409281a7fd0SWebb Scales 			refcount = atomic_inc_return(&c->refcount);
8410281a7fd0SWebb Scales 			if (refcount > 1) /* Command is allocated */
841123100dd9SStephen M. Cameron 				accel_cmds_out += is_accelerated_cmd(c);
8412281a7fd0SWebb Scales 			cmd_free(h, c);
8413f2405db8SDon Brace 		}
841423100dd9SStephen M. Cameron 		if (accel_cmds_out <= 0)
841576438d08SStephen M. Cameron 			break;
841676438d08SStephen M. Cameron 		msleep(100);
841776438d08SStephen M. Cameron 	} while (1);
841876438d08SStephen M. Cameron }
841976438d08SStephen M. Cameron 
8420edd16368SStephen M. Cameron /*
8421edd16368SStephen M. Cameron  *  This is it.  Register the PCI driver information for the cards we control
8422edd16368SStephen M. Cameron  *  the OS will call our registered routines when it finds one of our cards.
8423edd16368SStephen M. Cameron  */
8424edd16368SStephen M. Cameron static int __init hpsa_init(void)
8425edd16368SStephen M. Cameron {
842631468401SMike Miller 	return pci_register_driver(&hpsa_pci_driver);
8427edd16368SStephen M. Cameron }
8428edd16368SStephen M. Cameron 
8429edd16368SStephen M. Cameron static void __exit hpsa_cleanup(void)
8430edd16368SStephen M. Cameron {
8431edd16368SStephen M. Cameron 	pci_unregister_driver(&hpsa_pci_driver);
8432edd16368SStephen M. Cameron }
8433edd16368SStephen M. Cameron 
8434e1f7de0cSMatt Gates static void __attribute__((unused)) verify_offsets(void)
8435e1f7de0cSMatt Gates {
8436e1f7de0cSMatt Gates #define VERIFY_OFFSET(member, offset) \
8437dd0e19f3SScott Teel 	BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
8438dd0e19f3SScott Teel 
8439dd0e19f3SScott Teel 	VERIFY_OFFSET(structure_size, 0);
8440dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_size, 4);
8441dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_cnt, 8);
8442dd0e19f3SScott Teel 	VERIFY_OFFSET(phys_blk_shift, 16);
8443dd0e19f3SScott Teel 	VERIFY_OFFSET(parity_rotation_shift, 17);
8444dd0e19f3SScott Teel 	VERIFY_OFFSET(strip_size, 18);
8445dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_starting_blk, 20);
8446dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_blk_cnt, 28);
8447dd0e19f3SScott Teel 	VERIFY_OFFSET(data_disks_per_row, 36);
8448dd0e19f3SScott Teel 	VERIFY_OFFSET(metadata_disks_per_row, 38);
8449dd0e19f3SScott Teel 	VERIFY_OFFSET(row_cnt, 40);
8450dd0e19f3SScott Teel 	VERIFY_OFFSET(layout_map_count, 42);
8451dd0e19f3SScott Teel 	VERIFY_OFFSET(flags, 44);
8452dd0e19f3SScott Teel 	VERIFY_OFFSET(dekindex, 46);
8453dd0e19f3SScott Teel 	/* VERIFY_OFFSET(reserved, 48 */
8454dd0e19f3SScott Teel 	VERIFY_OFFSET(data, 64);
8455dd0e19f3SScott Teel 
8456dd0e19f3SScott Teel #undef VERIFY_OFFSET
8457dd0e19f3SScott Teel 
8458dd0e19f3SScott Teel #define VERIFY_OFFSET(member, offset) \
8459b66cc250SMike Miller 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
8460b66cc250SMike Miller 
8461b66cc250SMike Miller 	VERIFY_OFFSET(IU_type, 0);
8462b66cc250SMike Miller 	VERIFY_OFFSET(direction, 1);
8463b66cc250SMike Miller 	VERIFY_OFFSET(reply_queue, 2);
8464b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved1, 3);  */
8465b66cc250SMike Miller 	VERIFY_OFFSET(scsi_nexus, 4);
8466b66cc250SMike Miller 	VERIFY_OFFSET(Tag, 8);
8467b66cc250SMike Miller 	VERIFY_OFFSET(cdb, 16);
8468b66cc250SMike Miller 	VERIFY_OFFSET(cciss_lun, 32);
8469b66cc250SMike Miller 	VERIFY_OFFSET(data_len, 40);
8470b66cc250SMike Miller 	VERIFY_OFFSET(cmd_priority_task_attr, 44);
8471b66cc250SMike Miller 	VERIFY_OFFSET(sg_count, 45);
8472b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved3 */
8473b66cc250SMike Miller 	VERIFY_OFFSET(err_ptr, 48);
8474b66cc250SMike Miller 	VERIFY_OFFSET(err_len, 56);
8475b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved4  */
8476b66cc250SMike Miller 	VERIFY_OFFSET(sg, 64);
8477b66cc250SMike Miller 
8478b66cc250SMike Miller #undef VERIFY_OFFSET
8479b66cc250SMike Miller 
8480b66cc250SMike Miller #define VERIFY_OFFSET(member, offset) \
8481e1f7de0cSMatt Gates 	BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
8482e1f7de0cSMatt Gates 
8483e1f7de0cSMatt Gates 	VERIFY_OFFSET(dev_handle, 0x00);
8484e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved1, 0x02);
8485e1f7de0cSMatt Gates 	VERIFY_OFFSET(function, 0x03);
8486e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved2, 0x04);
8487e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info, 0x0C);
8488e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved3, 0x10);
8489e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info_len, 0x12);
8490e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved4, 0x13);
8491e1f7de0cSMatt Gates 	VERIFY_OFFSET(sgl_offset, 0x14);
8492e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved5, 0x15);
8493e1f7de0cSMatt Gates 	VERIFY_OFFSET(transfer_len, 0x1C);
8494e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved6, 0x20);
8495e1f7de0cSMatt Gates 	VERIFY_OFFSET(io_flags, 0x24);
8496e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved7, 0x26);
8497e1f7de0cSMatt Gates 	VERIFY_OFFSET(LUN, 0x34);
8498e1f7de0cSMatt Gates 	VERIFY_OFFSET(control, 0x3C);
8499e1f7de0cSMatt Gates 	VERIFY_OFFSET(CDB, 0x40);
8500e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved8, 0x50);
8501e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_context_flags, 0x60);
8502e1f7de0cSMatt Gates 	VERIFY_OFFSET(timeout_sec, 0x62);
8503e1f7de0cSMatt Gates 	VERIFY_OFFSET(ReplyQueue, 0x64);
8504e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved9, 0x65);
850550a0decfSStephen M. Cameron 	VERIFY_OFFSET(tag, 0x68);
8506e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_addr, 0x70);
8507e1f7de0cSMatt Gates 	VERIFY_OFFSET(CISS_LUN, 0x78);
8508e1f7de0cSMatt Gates 	VERIFY_OFFSET(SG, 0x78 + 8);
8509e1f7de0cSMatt Gates #undef VERIFY_OFFSET
8510e1f7de0cSMatt Gates }
8511e1f7de0cSMatt Gates 
8512edd16368SStephen M. Cameron module_init(hpsa_init);
8513edd16368SStephen M. Cameron module_exit(hpsa_cleanup);
8514