xref: /openbmc/linux/drivers/scsi/hpsa.c (revision a58e7e53b410c8ed05f0b1b0f37411c76b8e253f)
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 
198*a58e7e53SWebb Scales #define SCSI_CMD_BUSY ((struct scsi_cmnd *)&hpsa_cmd_busy)
199*a58e7e53SWebb Scales static const struct scsi_cmnd hpsa_cmd_busy;
200*a58e7e53SWebb Scales #define SCSI_CMD_IDLE ((struct scsi_cmnd *)&hpsa_cmd_idle)
201*a58e7e53SWebb 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 
277*a58e7e53SWebb Scales static inline bool hpsa_is_cmd_idle(struct CommandList *c)
278*a58e7e53SWebb Scales {
279*a58e7e53SWebb Scales 	return c->scsi_cmd == SCSI_CMD_IDLE;
280*a58e7e53SWebb Scales }
281*a58e7e53SWebb 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 
971*a58e7e53SWebb Scales static void enqueue_cmd_and_start_io(struct ctlr_info *h, struct CommandList *c)
97225163bd5SWebb Scales {
973*a58e7e53SWebb Scales 	if (unlikely(c->abort_pending))
974*a58e7e53SWebb Scales 		return finish_cmd(c);
975*a58e7e53SWebb 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 
1987*a58e7e53SWebb Scales static void hpsa_cmd_resolve_events(struct ctlr_info *h,
1988*a58e7e53SWebb Scales 		struct CommandList *c)
1989*a58e7e53SWebb Scales {
1990*a58e7e53SWebb Scales 	/*
1991*a58e7e53SWebb Scales 	 * Prevent the following race in the abort handler:
1992*a58e7e53SWebb Scales 	 *
1993*a58e7e53SWebb Scales 	 * 1. LLD is requested to abort a SCSI command
1994*a58e7e53SWebb Scales 	 * 2. The SCSI command completes
1995*a58e7e53SWebb Scales 	 * 3. The struct CommandList associated with step 2 is made available
1996*a58e7e53SWebb Scales 	 * 4. New I/O request to LLD to another LUN re-uses struct CommandList
1997*a58e7e53SWebb Scales 	 * 5. Abort handler follows scsi_cmnd->host_scribble and
1998*a58e7e53SWebb Scales 	 *    finds struct CommandList and tries to aborts it
1999*a58e7e53SWebb Scales 	 * Now we have aborted the wrong command.
2000*a58e7e53SWebb Scales 	 *
2001*a58e7e53SWebb Scales 	 * Clear c->scsi_cmd here so that the abort handler will know this
2002*a58e7e53SWebb Scales 	 * command has completed.  Then, check to see if the abort handler is
2003*a58e7e53SWebb Scales 	 * waiting for this command, and, if so, wake it.
2004*a58e7e53SWebb Scales 	 */
2005*a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_IDLE;
2006*a58e7e53SWebb Scales 	mb(); /* Ensure c->scsi_cmd is set to SCSI_CMD_IDLE */
2007*a58e7e53SWebb Scales 	if (c->abort_pending) {
2008*a58e7e53SWebb Scales 		c->abort_pending = false;
2009*a58e7e53SWebb Scales 		wake_up_all(&h->abort_sync_wait_queue);
2010*a58e7e53SWebb Scales 	}
2011*a58e7e53SWebb Scales }
2012*a58e7e53SWebb 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 {
2016*a58e7e53SWebb 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 
2027*a58e7e53SWebb Scales static void hpsa_set_scsi_cmd_aborted(struct scsi_cmnd *cmd)
2028*a58e7e53SWebb Scales {
2029*a58e7e53SWebb Scales 	cmd->result = DID_ABORT << 16;
2030*a58e7e53SWebb Scales }
2031*a58e7e53SWebb Scales 
2032*a58e7e53SWebb Scales static void hpsa_cmd_abort_and_free(struct ctlr_info *h, struct CommandList *c,
2033*a58e7e53SWebb Scales 				    struct scsi_cmnd *cmd)
2034*a58e7e53SWebb Scales {
2035*a58e7e53SWebb Scales 	hpsa_set_scsi_cmd_aborted(cmd);
2036*a58e7e53SWebb Scales 	dev_warn(&h->pdev->dev, "CDB %16phN was aborted with status 0x%x\n",
2037*a58e7e53SWebb Scales 			 c->Request.CDB, c->err_info->ScsiStatus);
2038*a58e7e53SWebb Scales 	hpsa_cmd_resolve_events(h, c);
2039*a58e7e53SWebb Scales 	cmd_free(h, c);		/* FIX-ME:  change to cmd_tagged_free(h, c) */
2040*a58e7e53SWebb Scales }
2041*a58e7e53SWebb 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 
2053*a58e7e53SWebb Scales 	/* don't requeue a command which is being aborted */
2054*a58e7e53SWebb Scales 	if (unlikely(c->abort_pending))
2055*a58e7e53SWebb Scales 		return hpsa_cmd_abort_and_free(h, c, cmd);
2056*a58e7e53SWebb 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;
2178*a58e7e53SWebb Scales 			if (!cp->abort_pending)
21798a0ff92cSWebb Scales 				return hpsa_retry_cmd(h, cp);
2180283b4a9bSStephen M. Cameron 		}
2181e1f7de0cSMatt Gates 	}
2182e1f7de0cSMatt Gates 
2183*a58e7e53SWebb Scales 	if (cp->abort_pending)
2184*a58e7e53SWebb Scales 		ei->CommandStatus = CMD_ABORTED;
2185*a58e7e53SWebb 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:
2273*a58e7e53SWebb Scales 		/* Return now to avoid calling scsi_done(). */
2274*a58e7e53SWebb 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;
367733a2ffceSStephen M. Cameron 	int use_sg, i, sg_index, chained;
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 
368933a2ffceSStephen M. Cameron 	curr_sg = cp->SG;
369033a2ffceSStephen M. Cameron 	chained = 0;
369133a2ffceSStephen M. Cameron 	sg_index = 0;
3692edd16368SStephen M. Cameron 	scsi_for_each_sg(cmd, sg, use_sg, i) {
369333a2ffceSStephen M. Cameron 		if (i == h->max_cmd_sg_entries - 1 &&
369433a2ffceSStephen M. Cameron 			use_sg > h->max_cmd_sg_entries) {
369533a2ffceSStephen M. Cameron 			chained = 1;
369633a2ffceSStephen M. Cameron 			curr_sg = h->cmd_sg_list[cp->cmdindex];
369733a2ffceSStephen M. Cameron 			sg_index = 0;
369833a2ffceSStephen M. Cameron 		}
3699ec5cbf04SWebb Scales 		hpsa_set_sg_descriptor(curr_sg, sg);
370033a2ffceSStephen M. Cameron 		curr_sg++;
370133a2ffceSStephen M. Cameron 	}
3702ec5cbf04SWebb Scales 
3703ec5cbf04SWebb Scales 	/* Back the pointer up to the last entry and mark it as "last". */
370450a0decfSStephen M. Cameron 	(--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
370533a2ffceSStephen M. Cameron 
370633a2ffceSStephen M. Cameron 	if (use_sg + chained > h->maxSG)
370733a2ffceSStephen M. Cameron 		h->maxSG = use_sg + chained;
370833a2ffceSStephen M. Cameron 
370933a2ffceSStephen M. Cameron 	if (chained) {
371033a2ffceSStephen M. Cameron 		cp->Header.SGList = h->max_cmd_sg_entries;
371150a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(use_sg + 1);
3712e2bea6dfSStephen M. Cameron 		if (hpsa_map_sg_chain_block(h, cp)) {
3713e2bea6dfSStephen M. Cameron 			scsi_dma_unmap(cmd);
3714e2bea6dfSStephen M. Cameron 			return -1;
3715e2bea6dfSStephen M. Cameron 		}
371633a2ffceSStephen M. Cameron 		return 0;
3717edd16368SStephen M. Cameron 	}
3718edd16368SStephen M. Cameron 
3719edd16368SStephen M. Cameron sglist_finished:
3720edd16368SStephen M. Cameron 
372101a02ffcSStephen M. Cameron 	cp->Header.SGList = (u8) use_sg;   /* no. SGs contig in this cmd */
3722c7ee65b3SWebb Scales 	cp->Header.SGTotal = cpu_to_le16(use_sg); /* total sgs in cmd list */
3723edd16368SStephen M. Cameron 	return 0;
3724edd16368SStephen M. Cameron }
3725edd16368SStephen M. Cameron 
3726283b4a9bSStephen M. Cameron #define IO_ACCEL_INELIGIBLE (1)
3727283b4a9bSStephen M. Cameron static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
3728283b4a9bSStephen M. Cameron {
3729283b4a9bSStephen M. Cameron 	int is_write = 0;
3730283b4a9bSStephen M. Cameron 	u32 block;
3731283b4a9bSStephen M. Cameron 	u32 block_cnt;
3732283b4a9bSStephen M. Cameron 
3733283b4a9bSStephen M. Cameron 	/* Perform some CDB fixups if needed using 10 byte reads/writes only */
3734283b4a9bSStephen M. Cameron 	switch (cdb[0]) {
3735283b4a9bSStephen M. Cameron 	case WRITE_6:
3736283b4a9bSStephen M. Cameron 	case WRITE_12:
3737283b4a9bSStephen M. Cameron 		is_write = 1;
3738283b4a9bSStephen M. Cameron 	case READ_6:
3739283b4a9bSStephen M. Cameron 	case READ_12:
3740283b4a9bSStephen M. Cameron 		if (*cdb_len == 6) {
3741283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 8) | cdb[3];
3742283b4a9bSStephen M. Cameron 			block_cnt = cdb[4];
3743283b4a9bSStephen M. Cameron 		} else {
3744283b4a9bSStephen M. Cameron 			BUG_ON(*cdb_len != 12);
3745283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 24) |
3746283b4a9bSStephen M. Cameron 				(((u32) cdb[3]) << 16) |
3747283b4a9bSStephen M. Cameron 				(((u32) cdb[4]) << 8) |
3748283b4a9bSStephen M. Cameron 				cdb[5];
3749283b4a9bSStephen M. Cameron 			block_cnt =
3750283b4a9bSStephen M. Cameron 				(((u32) cdb[6]) << 24) |
3751283b4a9bSStephen M. Cameron 				(((u32) cdb[7]) << 16) |
3752283b4a9bSStephen M. Cameron 				(((u32) cdb[8]) << 8) |
3753283b4a9bSStephen M. Cameron 				cdb[9];
3754283b4a9bSStephen M. Cameron 		}
3755283b4a9bSStephen M. Cameron 		if (block_cnt > 0xffff)
3756283b4a9bSStephen M. Cameron 			return IO_ACCEL_INELIGIBLE;
3757283b4a9bSStephen M. Cameron 
3758283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
3759283b4a9bSStephen M. Cameron 		cdb[1] = 0;
3760283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (block >> 24);
3761283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (block >> 16);
3762283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (block >> 8);
3763283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (block);
3764283b4a9bSStephen M. Cameron 		cdb[6] = 0;
3765283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (block_cnt >> 8);
3766283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (block_cnt);
3767283b4a9bSStephen M. Cameron 		cdb[9] = 0;
3768283b4a9bSStephen M. Cameron 		*cdb_len = 10;
3769283b4a9bSStephen M. Cameron 		break;
3770283b4a9bSStephen M. Cameron 	}
3771283b4a9bSStephen M. Cameron 	return 0;
3772283b4a9bSStephen M. Cameron }
3773283b4a9bSStephen M. Cameron 
3774c349775eSScott Teel static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
3775283b4a9bSStephen M. Cameron 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
377603383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
3777e1f7de0cSMatt Gates {
3778e1f7de0cSMatt Gates 	struct scsi_cmnd *cmd = c->scsi_cmd;
3779e1f7de0cSMatt Gates 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
3780e1f7de0cSMatt Gates 	unsigned int len;
3781e1f7de0cSMatt Gates 	unsigned int total_len = 0;
3782e1f7de0cSMatt Gates 	struct scatterlist *sg;
3783e1f7de0cSMatt Gates 	u64 addr64;
3784e1f7de0cSMatt Gates 	int use_sg, i;
3785e1f7de0cSMatt Gates 	struct SGDescriptor *curr_sg;
3786e1f7de0cSMatt Gates 	u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
3787e1f7de0cSMatt Gates 
3788283b4a9bSStephen M. Cameron 	/* TODO: implement chaining support */
378903383736SDon Brace 	if (scsi_sg_count(cmd) > h->ioaccel_maxsg) {
379003383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3791283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
379203383736SDon Brace 	}
3793283b4a9bSStephen M. Cameron 
3794e1f7de0cSMatt Gates 	BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
3795e1f7de0cSMatt Gates 
379603383736SDon Brace 	if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
379703383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3798283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
379903383736SDon Brace 	}
3800283b4a9bSStephen M. Cameron 
3801e1f7de0cSMatt Gates 	c->cmd_type = CMD_IOACCEL1;
3802e1f7de0cSMatt Gates 
3803e1f7de0cSMatt Gates 	/* Adjust the DMA address to point to the accelerated command buffer */
3804e1f7de0cSMatt Gates 	c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
3805e1f7de0cSMatt Gates 				(c->cmdindex * sizeof(*cp));
3806e1f7de0cSMatt Gates 	BUG_ON(c->busaddr & 0x0000007F);
3807e1f7de0cSMatt Gates 
3808e1f7de0cSMatt Gates 	use_sg = scsi_dma_map(cmd);
380903383736SDon Brace 	if (use_sg < 0) {
381003383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3811e1f7de0cSMatt Gates 		return use_sg;
381203383736SDon Brace 	}
3813e1f7de0cSMatt Gates 
3814e1f7de0cSMatt Gates 	if (use_sg) {
3815e1f7de0cSMatt Gates 		curr_sg = cp->SG;
3816e1f7de0cSMatt Gates 		scsi_for_each_sg(cmd, sg, use_sg, i) {
3817e1f7de0cSMatt Gates 			addr64 = (u64) sg_dma_address(sg);
3818e1f7de0cSMatt Gates 			len  = sg_dma_len(sg);
3819e1f7de0cSMatt Gates 			total_len += len;
382050a0decfSStephen M. Cameron 			curr_sg->Addr = cpu_to_le64(addr64);
382150a0decfSStephen M. Cameron 			curr_sg->Len = cpu_to_le32(len);
382250a0decfSStephen M. Cameron 			curr_sg->Ext = cpu_to_le32(0);
3823e1f7de0cSMatt Gates 			curr_sg++;
3824e1f7de0cSMatt Gates 		}
382550a0decfSStephen M. Cameron 		(--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
3826e1f7de0cSMatt Gates 
3827e1f7de0cSMatt Gates 		switch (cmd->sc_data_direction) {
3828e1f7de0cSMatt Gates 		case DMA_TO_DEVICE:
3829e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_OUT;
3830e1f7de0cSMatt Gates 			break;
3831e1f7de0cSMatt Gates 		case DMA_FROM_DEVICE:
3832e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_IN;
3833e1f7de0cSMatt Gates 			break;
3834e1f7de0cSMatt Gates 		case DMA_NONE:
3835e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_NODATAXFER;
3836e1f7de0cSMatt Gates 			break;
3837e1f7de0cSMatt Gates 		default:
3838e1f7de0cSMatt Gates 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3839e1f7de0cSMatt Gates 			cmd->sc_data_direction);
3840e1f7de0cSMatt Gates 			BUG();
3841e1f7de0cSMatt Gates 			break;
3842e1f7de0cSMatt Gates 		}
3843e1f7de0cSMatt Gates 	} else {
3844e1f7de0cSMatt Gates 		control |= IOACCEL1_CONTROL_NODATAXFER;
3845e1f7de0cSMatt Gates 	}
3846e1f7de0cSMatt Gates 
3847c349775eSScott Teel 	c->Header.SGList = use_sg;
3848e1f7de0cSMatt Gates 	/* Fill out the command structure to submit */
38492b08b3e9SDon Brace 	cp->dev_handle = cpu_to_le16(ioaccel_handle & 0xFFFF);
38502b08b3e9SDon Brace 	cp->transfer_len = cpu_to_le32(total_len);
38512b08b3e9SDon Brace 	cp->io_flags = cpu_to_le16(IOACCEL1_IOFLAGS_IO_REQ |
38522b08b3e9SDon Brace 			(cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK));
38532b08b3e9SDon Brace 	cp->control = cpu_to_le32(control);
3854283b4a9bSStephen M. Cameron 	memcpy(cp->CDB, cdb, cdb_len);
3855283b4a9bSStephen M. Cameron 	memcpy(cp->CISS_LUN, scsi3addr, 8);
3856c349775eSScott Teel 	/* Tag was already set at init time. */
3857e1f7de0cSMatt Gates 	enqueue_cmd_and_start_io(h, c);
3858e1f7de0cSMatt Gates 	return 0;
3859e1f7de0cSMatt Gates }
3860edd16368SStephen M. Cameron 
3861283b4a9bSStephen M. Cameron /*
3862283b4a9bSStephen M. Cameron  * Queue a command directly to a device behind the controller using the
3863283b4a9bSStephen M. Cameron  * I/O accelerator path.
3864283b4a9bSStephen M. Cameron  */
3865283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
3866283b4a9bSStephen M. Cameron 	struct CommandList *c)
3867283b4a9bSStephen M. Cameron {
3868283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
3869283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3870283b4a9bSStephen M. Cameron 
387103383736SDon Brace 	c->phys_disk = dev;
387203383736SDon Brace 
3873283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
387403383736SDon Brace 		cmd->cmnd, cmd->cmd_len, dev->scsi3addr, dev);
3875283b4a9bSStephen M. Cameron }
3876283b4a9bSStephen M. Cameron 
3877dd0e19f3SScott Teel /*
3878dd0e19f3SScott Teel  * Set encryption parameters for the ioaccel2 request
3879dd0e19f3SScott Teel  */
3880dd0e19f3SScott Teel static void set_encrypt_ioaccel2(struct ctlr_info *h,
3881dd0e19f3SScott Teel 	struct CommandList *c, struct io_accel2_cmd *cp)
3882dd0e19f3SScott Teel {
3883dd0e19f3SScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
3884dd0e19f3SScott Teel 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3885dd0e19f3SScott Teel 	struct raid_map_data *map = &dev->raid_map;
3886dd0e19f3SScott Teel 	u64 first_block;
3887dd0e19f3SScott Teel 
3888dd0e19f3SScott Teel 	/* Are we doing encryption on this device */
38892b08b3e9SDon Brace 	if (!(le16_to_cpu(map->flags) & RAID_MAP_FLAG_ENCRYPT_ON))
3890dd0e19f3SScott Teel 		return;
3891dd0e19f3SScott Teel 	/* Set the data encryption key index. */
3892dd0e19f3SScott Teel 	cp->dekindex = map->dekindex;
3893dd0e19f3SScott Teel 
3894dd0e19f3SScott Teel 	/* Set the encryption enable flag, encoded into direction field. */
3895dd0e19f3SScott Teel 	cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
3896dd0e19f3SScott Teel 
3897dd0e19f3SScott Teel 	/* Set encryption tweak values based on logical block address
3898dd0e19f3SScott Teel 	 * If block size is 512, tweak value is LBA.
3899dd0e19f3SScott Teel 	 * For other block sizes, tweak is (LBA * block size)/ 512)
3900dd0e19f3SScott Teel 	 */
3901dd0e19f3SScott Teel 	switch (cmd->cmnd[0]) {
3902dd0e19f3SScott Teel 	/* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
3903dd0e19f3SScott Teel 	case WRITE_6:
3904dd0e19f3SScott Teel 	case READ_6:
39052b08b3e9SDon Brace 		first_block = get_unaligned_be16(&cmd->cmnd[2]);
3906dd0e19f3SScott Teel 		break;
3907dd0e19f3SScott Teel 	case WRITE_10:
3908dd0e19f3SScott Teel 	case READ_10:
3909dd0e19f3SScott Teel 	/* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
3910dd0e19f3SScott Teel 	case WRITE_12:
3911dd0e19f3SScott Teel 	case READ_12:
39122b08b3e9SDon Brace 		first_block = get_unaligned_be32(&cmd->cmnd[2]);
3913dd0e19f3SScott Teel 		break;
3914dd0e19f3SScott Teel 	case WRITE_16:
3915dd0e19f3SScott Teel 	case READ_16:
39162b08b3e9SDon Brace 		first_block = get_unaligned_be64(&cmd->cmnd[2]);
3917dd0e19f3SScott Teel 		break;
3918dd0e19f3SScott Teel 	default:
3919dd0e19f3SScott Teel 		dev_err(&h->pdev->dev,
39202b08b3e9SDon Brace 			"ERROR: %s: size (0x%x) not supported for encryption\n",
39212b08b3e9SDon Brace 			__func__, cmd->cmnd[0]);
3922dd0e19f3SScott Teel 		BUG();
3923dd0e19f3SScott Teel 		break;
3924dd0e19f3SScott Teel 	}
39252b08b3e9SDon Brace 
39262b08b3e9SDon Brace 	if (le32_to_cpu(map->volume_blk_size) != 512)
39272b08b3e9SDon Brace 		first_block = first_block *
39282b08b3e9SDon Brace 				le32_to_cpu(map->volume_blk_size)/512;
39292b08b3e9SDon Brace 
39302b08b3e9SDon Brace 	cp->tweak_lower = cpu_to_le32(first_block);
39312b08b3e9SDon Brace 	cp->tweak_upper = cpu_to_le32(first_block >> 32);
3932dd0e19f3SScott Teel }
3933dd0e19f3SScott Teel 
3934c349775eSScott Teel static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
3935c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
393603383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
3937c349775eSScott Teel {
3938c349775eSScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
3939c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
3940c349775eSScott Teel 	struct ioaccel2_sg_element *curr_sg;
3941c349775eSScott Teel 	int use_sg, i;
3942c349775eSScott Teel 	struct scatterlist *sg;
3943c349775eSScott Teel 	u64 addr64;
3944c349775eSScott Teel 	u32 len;
3945c349775eSScott Teel 	u32 total_len = 0;
3946c349775eSScott Teel 
3947d9a729f3SWebb Scales 	BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
3948c349775eSScott Teel 
394903383736SDon Brace 	if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
395003383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3951c349775eSScott Teel 		return IO_ACCEL_INELIGIBLE;
395203383736SDon Brace 	}
395303383736SDon Brace 
3954c349775eSScott Teel 	c->cmd_type = CMD_IOACCEL2;
3955c349775eSScott Teel 	/* Adjust the DMA address to point to the accelerated command buffer */
3956c349775eSScott Teel 	c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
3957c349775eSScott Teel 				(c->cmdindex * sizeof(*cp));
3958c349775eSScott Teel 	BUG_ON(c->busaddr & 0x0000007F);
3959c349775eSScott Teel 
3960c349775eSScott Teel 	memset(cp, 0, sizeof(*cp));
3961c349775eSScott Teel 	cp->IU_type = IOACCEL2_IU_TYPE;
3962c349775eSScott Teel 
3963c349775eSScott Teel 	use_sg = scsi_dma_map(cmd);
396403383736SDon Brace 	if (use_sg < 0) {
396503383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3966c349775eSScott Teel 		return use_sg;
396703383736SDon Brace 	}
3968c349775eSScott Teel 
3969c349775eSScott Teel 	if (use_sg) {
3970c349775eSScott Teel 		curr_sg = cp->sg;
3971d9a729f3SWebb Scales 		if (use_sg > h->ioaccel_maxsg) {
3972d9a729f3SWebb Scales 			addr64 = le64_to_cpu(
3973d9a729f3SWebb Scales 				h->ioaccel2_cmd_sg_list[c->cmdindex]->address);
3974d9a729f3SWebb Scales 			curr_sg->address = cpu_to_le64(addr64);
3975d9a729f3SWebb Scales 			curr_sg->length = 0;
3976d9a729f3SWebb Scales 			curr_sg->reserved[0] = 0;
3977d9a729f3SWebb Scales 			curr_sg->reserved[1] = 0;
3978d9a729f3SWebb Scales 			curr_sg->reserved[2] = 0;
3979d9a729f3SWebb Scales 			curr_sg->chain_indicator = 0x80;
3980d9a729f3SWebb Scales 
3981d9a729f3SWebb Scales 			curr_sg = h->ioaccel2_cmd_sg_list[c->cmdindex];
3982d9a729f3SWebb Scales 		}
3983c349775eSScott Teel 		scsi_for_each_sg(cmd, sg, use_sg, i) {
3984c349775eSScott Teel 			addr64 = (u64) sg_dma_address(sg);
3985c349775eSScott Teel 			len  = sg_dma_len(sg);
3986c349775eSScott Teel 			total_len += len;
3987c349775eSScott Teel 			curr_sg->address = cpu_to_le64(addr64);
3988c349775eSScott Teel 			curr_sg->length = cpu_to_le32(len);
3989c349775eSScott Teel 			curr_sg->reserved[0] = 0;
3990c349775eSScott Teel 			curr_sg->reserved[1] = 0;
3991c349775eSScott Teel 			curr_sg->reserved[2] = 0;
3992c349775eSScott Teel 			curr_sg->chain_indicator = 0;
3993c349775eSScott Teel 			curr_sg++;
3994c349775eSScott Teel 		}
3995c349775eSScott Teel 
3996c349775eSScott Teel 		switch (cmd->sc_data_direction) {
3997c349775eSScott Teel 		case DMA_TO_DEVICE:
3998dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3999dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_OUT;
4000c349775eSScott Teel 			break;
4001c349775eSScott Teel 		case DMA_FROM_DEVICE:
4002dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4003dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_IN;
4004c349775eSScott Teel 			break;
4005c349775eSScott Teel 		case DMA_NONE:
4006dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4007dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_NO_DATA;
4008c349775eSScott Teel 			break;
4009c349775eSScott Teel 		default:
4010c349775eSScott Teel 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4011c349775eSScott Teel 				cmd->sc_data_direction);
4012c349775eSScott Teel 			BUG();
4013c349775eSScott Teel 			break;
4014c349775eSScott Teel 		}
4015c349775eSScott Teel 	} else {
4016dd0e19f3SScott Teel 		cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4017dd0e19f3SScott Teel 		cp->direction |= IOACCEL2_DIR_NO_DATA;
4018c349775eSScott Teel 	}
4019dd0e19f3SScott Teel 
4020dd0e19f3SScott Teel 	/* Set encryption parameters, if necessary */
4021dd0e19f3SScott Teel 	set_encrypt_ioaccel2(h, c, cp);
4022dd0e19f3SScott Teel 
40232b08b3e9SDon Brace 	cp->scsi_nexus = cpu_to_le32(ioaccel_handle);
4024f2405db8SDon Brace 	cp->Tag = cpu_to_le32(c->cmdindex << DIRECT_LOOKUP_SHIFT);
4025c349775eSScott Teel 	memcpy(cp->cdb, cdb, sizeof(cp->cdb));
4026c349775eSScott Teel 
4027c349775eSScott Teel 	cp->data_len = cpu_to_le32(total_len);
4028c349775eSScott Teel 	cp->err_ptr = cpu_to_le64(c->busaddr +
4029c349775eSScott Teel 			offsetof(struct io_accel2_cmd, error_data));
403050a0decfSStephen M. Cameron 	cp->err_len = cpu_to_le32(sizeof(cp->error_data));
4031c349775eSScott Teel 
4032d9a729f3SWebb Scales 	/* fill in sg elements */
4033d9a729f3SWebb Scales 	if (use_sg > h->ioaccel_maxsg) {
4034d9a729f3SWebb Scales 		cp->sg_count = 1;
4035d9a729f3SWebb Scales 		if (hpsa_map_ioaccel2_sg_chain_block(h, cp, c)) {
4036d9a729f3SWebb Scales 			atomic_dec(&phys_disk->ioaccel_cmds_out);
4037d9a729f3SWebb Scales 			scsi_dma_unmap(cmd);
4038d9a729f3SWebb Scales 			return -1;
4039d9a729f3SWebb Scales 		}
4040d9a729f3SWebb Scales 	} else
4041d9a729f3SWebb Scales 		cp->sg_count = (u8) use_sg;
4042d9a729f3SWebb Scales 
4043c349775eSScott Teel 	enqueue_cmd_and_start_io(h, c);
4044c349775eSScott Teel 	return 0;
4045c349775eSScott Teel }
4046c349775eSScott Teel 
4047c349775eSScott Teel /*
4048c349775eSScott Teel  * Queue a command to the correct I/O accelerator path.
4049c349775eSScott Teel  */
4050c349775eSScott Teel static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
4051c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
405203383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
4053c349775eSScott Teel {
405403383736SDon Brace 	/* Try to honor the device's queue depth */
405503383736SDon Brace 	if (atomic_inc_return(&phys_disk->ioaccel_cmds_out) >
405603383736SDon Brace 					phys_disk->queue_depth) {
405703383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
405803383736SDon Brace 		return IO_ACCEL_INELIGIBLE;
405903383736SDon Brace 	}
4060c349775eSScott Teel 	if (h->transMethod & CFGTBL_Trans_io_accel1)
4061c349775eSScott Teel 		return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
406203383736SDon Brace 						cdb, cdb_len, scsi3addr,
406303383736SDon Brace 						phys_disk);
4064c349775eSScott Teel 	else
4065c349775eSScott Teel 		return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
406603383736SDon Brace 						cdb, cdb_len, scsi3addr,
406703383736SDon Brace 						phys_disk);
4068c349775eSScott Teel }
4069c349775eSScott Teel 
40706b80b18fSScott Teel static void raid_map_helper(struct raid_map_data *map,
40716b80b18fSScott Teel 		int offload_to_mirror, u32 *map_index, u32 *current_group)
40726b80b18fSScott Teel {
40736b80b18fSScott Teel 	if (offload_to_mirror == 0)  {
40746b80b18fSScott Teel 		/* use physical disk in the first mirrored group. */
40752b08b3e9SDon Brace 		*map_index %= le16_to_cpu(map->data_disks_per_row);
40766b80b18fSScott Teel 		return;
40776b80b18fSScott Teel 	}
40786b80b18fSScott Teel 	do {
40796b80b18fSScott Teel 		/* determine mirror group that *map_index indicates */
40802b08b3e9SDon Brace 		*current_group = *map_index /
40812b08b3e9SDon Brace 			le16_to_cpu(map->data_disks_per_row);
40826b80b18fSScott Teel 		if (offload_to_mirror == *current_group)
40836b80b18fSScott Teel 			continue;
40842b08b3e9SDon Brace 		if (*current_group < le16_to_cpu(map->layout_map_count) - 1) {
40856b80b18fSScott Teel 			/* select map index from next group */
40862b08b3e9SDon Brace 			*map_index += le16_to_cpu(map->data_disks_per_row);
40876b80b18fSScott Teel 			(*current_group)++;
40886b80b18fSScott Teel 		} else {
40896b80b18fSScott Teel 			/* select map index from first group */
40902b08b3e9SDon Brace 			*map_index %= le16_to_cpu(map->data_disks_per_row);
40916b80b18fSScott Teel 			*current_group = 0;
40926b80b18fSScott Teel 		}
40936b80b18fSScott Teel 	} while (offload_to_mirror != *current_group);
40946b80b18fSScott Teel }
40956b80b18fSScott Teel 
4096283b4a9bSStephen M. Cameron /*
4097283b4a9bSStephen M. Cameron  * Attempt to perform offload RAID mapping for a logical volume I/O.
4098283b4a9bSStephen M. Cameron  */
4099283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
4100283b4a9bSStephen M. Cameron 	struct CommandList *c)
4101283b4a9bSStephen M. Cameron {
4102283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
4103283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4104283b4a9bSStephen M. Cameron 	struct raid_map_data *map = &dev->raid_map;
4105283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map->data[0];
4106283b4a9bSStephen M. Cameron 	int is_write = 0;
4107283b4a9bSStephen M. Cameron 	u32 map_index;
4108283b4a9bSStephen M. Cameron 	u64 first_block, last_block;
4109283b4a9bSStephen M. Cameron 	u32 block_cnt;
4110283b4a9bSStephen M. Cameron 	u32 blocks_per_row;
4111283b4a9bSStephen M. Cameron 	u64 first_row, last_row;
4112283b4a9bSStephen M. Cameron 	u32 first_row_offset, last_row_offset;
4113283b4a9bSStephen M. Cameron 	u32 first_column, last_column;
41146b80b18fSScott Teel 	u64 r0_first_row, r0_last_row;
41156b80b18fSScott Teel 	u32 r5or6_blocks_per_row;
41166b80b18fSScott Teel 	u64 r5or6_first_row, r5or6_last_row;
41176b80b18fSScott Teel 	u32 r5or6_first_row_offset, r5or6_last_row_offset;
41186b80b18fSScott Teel 	u32 r5or6_first_column, r5or6_last_column;
41196b80b18fSScott Teel 	u32 total_disks_per_row;
41206b80b18fSScott Teel 	u32 stripesize;
41216b80b18fSScott Teel 	u32 first_group, last_group, current_group;
4122283b4a9bSStephen M. Cameron 	u32 map_row;
4123283b4a9bSStephen M. Cameron 	u32 disk_handle;
4124283b4a9bSStephen M. Cameron 	u64 disk_block;
4125283b4a9bSStephen M. Cameron 	u32 disk_block_cnt;
4126283b4a9bSStephen M. Cameron 	u8 cdb[16];
4127283b4a9bSStephen M. Cameron 	u8 cdb_len;
41282b08b3e9SDon Brace 	u16 strip_size;
4129283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
4130283b4a9bSStephen M. Cameron 	u64 tmpdiv;
4131283b4a9bSStephen M. Cameron #endif
41326b80b18fSScott Teel 	int offload_to_mirror;
4133283b4a9bSStephen M. Cameron 
4134283b4a9bSStephen M. Cameron 	/* check for valid opcode, get LBA and block count */
4135283b4a9bSStephen M. Cameron 	switch (cmd->cmnd[0]) {
4136283b4a9bSStephen M. Cameron 	case WRITE_6:
4137283b4a9bSStephen M. Cameron 		is_write = 1;
4138283b4a9bSStephen M. Cameron 	case READ_6:
4139283b4a9bSStephen M. Cameron 		first_block =
4140283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 8) |
4141283b4a9bSStephen M. Cameron 			cmd->cmnd[3];
4142283b4a9bSStephen M. Cameron 		block_cnt = cmd->cmnd[4];
41433fa89a04SStephen M. Cameron 		if (block_cnt == 0)
41443fa89a04SStephen M. Cameron 			block_cnt = 256;
4145283b4a9bSStephen M. Cameron 		break;
4146283b4a9bSStephen M. Cameron 	case WRITE_10:
4147283b4a9bSStephen M. Cameron 		is_write = 1;
4148283b4a9bSStephen M. Cameron 	case READ_10:
4149283b4a9bSStephen M. Cameron 		first_block =
4150283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
4151283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
4152283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
4153283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
4154283b4a9bSStephen M. Cameron 		block_cnt =
4155283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 8) |
4156283b4a9bSStephen M. Cameron 			cmd->cmnd[8];
4157283b4a9bSStephen M. Cameron 		break;
4158283b4a9bSStephen M. Cameron 	case WRITE_12:
4159283b4a9bSStephen M. Cameron 		is_write = 1;
4160283b4a9bSStephen M. Cameron 	case READ_12:
4161283b4a9bSStephen M. Cameron 		first_block =
4162283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
4163283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
4164283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
4165283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
4166283b4a9bSStephen M. Cameron 		block_cnt =
4167283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[6]) << 24) |
4168283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 16) |
4169283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[8]) << 8) |
4170283b4a9bSStephen M. Cameron 		cmd->cmnd[9];
4171283b4a9bSStephen M. Cameron 		break;
4172283b4a9bSStephen M. Cameron 	case WRITE_16:
4173283b4a9bSStephen M. Cameron 		is_write = 1;
4174283b4a9bSStephen M. Cameron 	case READ_16:
4175283b4a9bSStephen M. Cameron 		first_block =
4176283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 56) |
4177283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 48) |
4178283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 40) |
4179283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[5]) << 32) |
4180283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[6]) << 24) |
4181283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[7]) << 16) |
4182283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[8]) << 8) |
4183283b4a9bSStephen M. Cameron 			cmd->cmnd[9];
4184283b4a9bSStephen M. Cameron 		block_cnt =
4185283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[10]) << 24) |
4186283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[11]) << 16) |
4187283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[12]) << 8) |
4188283b4a9bSStephen M. Cameron 			cmd->cmnd[13];
4189283b4a9bSStephen M. Cameron 		break;
4190283b4a9bSStephen M. Cameron 	default:
4191283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
4192283b4a9bSStephen M. Cameron 	}
4193283b4a9bSStephen M. Cameron 	last_block = first_block + block_cnt - 1;
4194283b4a9bSStephen M. Cameron 
4195283b4a9bSStephen M. Cameron 	/* check for write to non-RAID-0 */
4196283b4a9bSStephen M. Cameron 	if (is_write && dev->raid_level != 0)
4197283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
4198283b4a9bSStephen M. Cameron 
4199283b4a9bSStephen M. Cameron 	/* check for invalid block or wraparound */
42002b08b3e9SDon Brace 	if (last_block >= le64_to_cpu(map->volume_blk_cnt) ||
42012b08b3e9SDon Brace 		last_block < first_block)
4202283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
4203283b4a9bSStephen M. Cameron 
4204283b4a9bSStephen M. Cameron 	/* calculate stripe information for the request */
42052b08b3e9SDon Brace 	blocks_per_row = le16_to_cpu(map->data_disks_per_row) *
42062b08b3e9SDon Brace 				le16_to_cpu(map->strip_size);
42072b08b3e9SDon Brace 	strip_size = le16_to_cpu(map->strip_size);
4208283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
4209283b4a9bSStephen M. Cameron 	tmpdiv = first_block;
4210283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
4211283b4a9bSStephen M. Cameron 	first_row = tmpdiv;
4212283b4a9bSStephen M. Cameron 	tmpdiv = last_block;
4213283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
4214283b4a9bSStephen M. Cameron 	last_row = tmpdiv;
4215283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
4216283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
4217283b4a9bSStephen M. Cameron 	tmpdiv = first_row_offset;
42182b08b3e9SDon Brace 	(void) do_div(tmpdiv, strip_size);
4219283b4a9bSStephen M. Cameron 	first_column = tmpdiv;
4220283b4a9bSStephen M. Cameron 	tmpdiv = last_row_offset;
42212b08b3e9SDon Brace 	(void) do_div(tmpdiv, strip_size);
4222283b4a9bSStephen M. Cameron 	last_column = tmpdiv;
4223283b4a9bSStephen M. Cameron #else
4224283b4a9bSStephen M. Cameron 	first_row = first_block / blocks_per_row;
4225283b4a9bSStephen M. Cameron 	last_row = last_block / blocks_per_row;
4226283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
4227283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
42282b08b3e9SDon Brace 	first_column = first_row_offset / strip_size;
42292b08b3e9SDon Brace 	last_column = last_row_offset / strip_size;
4230283b4a9bSStephen M. Cameron #endif
4231283b4a9bSStephen M. Cameron 
4232283b4a9bSStephen M. Cameron 	/* if this isn't a single row/column then give to the controller */
4233283b4a9bSStephen M. Cameron 	if ((first_row != last_row) || (first_column != last_column))
4234283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
4235283b4a9bSStephen M. Cameron 
4236283b4a9bSStephen M. Cameron 	/* proceeding with driver mapping */
42372b08b3e9SDon Brace 	total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
42382b08b3e9SDon Brace 				le16_to_cpu(map->metadata_disks_per_row);
4239283b4a9bSStephen M. Cameron 	map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
42402b08b3e9SDon Brace 				le16_to_cpu(map->row_cnt);
42416b80b18fSScott Teel 	map_index = (map_row * total_disks_per_row) + first_column;
42426b80b18fSScott Teel 
42436b80b18fSScott Teel 	switch (dev->raid_level) {
42446b80b18fSScott Teel 	case HPSA_RAID_0:
42456b80b18fSScott Teel 		break; /* nothing special to do */
42466b80b18fSScott Teel 	case HPSA_RAID_1:
42476b80b18fSScott Teel 		/* Handles load balance across RAID 1 members.
42486b80b18fSScott Teel 		 * (2-drive R1 and R10 with even # of drives.)
42496b80b18fSScott Teel 		 * Appropriate for SSDs, not optimal for HDDs
4250283b4a9bSStephen M. Cameron 		 */
42512b08b3e9SDon Brace 		BUG_ON(le16_to_cpu(map->layout_map_count) != 2);
4252283b4a9bSStephen M. Cameron 		if (dev->offload_to_mirror)
42532b08b3e9SDon Brace 			map_index += le16_to_cpu(map->data_disks_per_row);
4254283b4a9bSStephen M. Cameron 		dev->offload_to_mirror = !dev->offload_to_mirror;
42556b80b18fSScott Teel 		break;
42566b80b18fSScott Teel 	case HPSA_RAID_ADM:
42576b80b18fSScott Teel 		/* Handles N-way mirrors  (R1-ADM)
42586b80b18fSScott Teel 		 * and R10 with # of drives divisible by 3.)
42596b80b18fSScott Teel 		 */
42602b08b3e9SDon Brace 		BUG_ON(le16_to_cpu(map->layout_map_count) != 3);
42616b80b18fSScott Teel 
42626b80b18fSScott Teel 		offload_to_mirror = dev->offload_to_mirror;
42636b80b18fSScott Teel 		raid_map_helper(map, offload_to_mirror,
42646b80b18fSScott Teel 				&map_index, &current_group);
42656b80b18fSScott Teel 		/* set mirror group to use next time */
42666b80b18fSScott Teel 		offload_to_mirror =
42672b08b3e9SDon Brace 			(offload_to_mirror >=
42682b08b3e9SDon Brace 			le16_to_cpu(map->layout_map_count) - 1)
42696b80b18fSScott Teel 			? 0 : offload_to_mirror + 1;
42706b80b18fSScott Teel 		dev->offload_to_mirror = offload_to_mirror;
42716b80b18fSScott Teel 		/* Avoid direct use of dev->offload_to_mirror within this
42726b80b18fSScott Teel 		 * function since multiple threads might simultaneously
42736b80b18fSScott Teel 		 * increment it beyond the range of dev->layout_map_count -1.
42746b80b18fSScott Teel 		 */
42756b80b18fSScott Teel 		break;
42766b80b18fSScott Teel 	case HPSA_RAID_5:
42776b80b18fSScott Teel 	case HPSA_RAID_6:
42782b08b3e9SDon Brace 		if (le16_to_cpu(map->layout_map_count) <= 1)
42796b80b18fSScott Teel 			break;
42806b80b18fSScott Teel 
42816b80b18fSScott Teel 		/* Verify first and last block are in same RAID group */
42826b80b18fSScott Teel 		r5or6_blocks_per_row =
42832b08b3e9SDon Brace 			le16_to_cpu(map->strip_size) *
42842b08b3e9SDon Brace 			le16_to_cpu(map->data_disks_per_row);
42856b80b18fSScott Teel 		BUG_ON(r5or6_blocks_per_row == 0);
42862b08b3e9SDon Brace 		stripesize = r5or6_blocks_per_row *
42872b08b3e9SDon Brace 			le16_to_cpu(map->layout_map_count);
42886b80b18fSScott Teel #if BITS_PER_LONG == 32
42896b80b18fSScott Teel 		tmpdiv = first_block;
42906b80b18fSScott Teel 		first_group = do_div(tmpdiv, stripesize);
42916b80b18fSScott Teel 		tmpdiv = first_group;
42926b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
42936b80b18fSScott Teel 		first_group = tmpdiv;
42946b80b18fSScott Teel 		tmpdiv = last_block;
42956b80b18fSScott Teel 		last_group = do_div(tmpdiv, stripesize);
42966b80b18fSScott Teel 		tmpdiv = last_group;
42976b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
42986b80b18fSScott Teel 		last_group = tmpdiv;
42996b80b18fSScott Teel #else
43006b80b18fSScott Teel 		first_group = (first_block % stripesize) / r5or6_blocks_per_row;
43016b80b18fSScott Teel 		last_group = (last_block % stripesize) / r5or6_blocks_per_row;
43026b80b18fSScott Teel #endif
4303000ff7c2SStephen M. Cameron 		if (first_group != last_group)
43046b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
43056b80b18fSScott Teel 
43066b80b18fSScott Teel 		/* Verify request is in a single row of RAID 5/6 */
43076b80b18fSScott Teel #if BITS_PER_LONG == 32
43086b80b18fSScott Teel 		tmpdiv = first_block;
43096b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
43106b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row = tmpdiv;
43116b80b18fSScott Teel 		tmpdiv = last_block;
43126b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
43136b80b18fSScott Teel 		r5or6_last_row = r0_last_row = tmpdiv;
43146b80b18fSScott Teel #else
43156b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row =
43166b80b18fSScott Teel 						first_block / stripesize;
43176b80b18fSScott Teel 		r5or6_last_row = r0_last_row = last_block / stripesize;
43186b80b18fSScott Teel #endif
43196b80b18fSScott Teel 		if (r5or6_first_row != r5or6_last_row)
43206b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
43216b80b18fSScott Teel 
43226b80b18fSScott Teel 
43236b80b18fSScott Teel 		/* Verify request is in a single column */
43246b80b18fSScott Teel #if BITS_PER_LONG == 32
43256b80b18fSScott Teel 		tmpdiv = first_block;
43266b80b18fSScott Teel 		first_row_offset = do_div(tmpdiv, stripesize);
43276b80b18fSScott Teel 		tmpdiv = first_row_offset;
43286b80b18fSScott Teel 		first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
43296b80b18fSScott Teel 		r5or6_first_row_offset = first_row_offset;
43306b80b18fSScott Teel 		tmpdiv = last_block;
43316b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, stripesize);
43326b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
43336b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
43346b80b18fSScott Teel 		tmpdiv = r5or6_first_row_offset;
43356b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
43366b80b18fSScott Teel 		first_column = r5or6_first_column = tmpdiv;
43376b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
43386b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
43396b80b18fSScott Teel 		r5or6_last_column = tmpdiv;
43406b80b18fSScott Teel #else
43416b80b18fSScott Teel 		first_row_offset = r5or6_first_row_offset =
43426b80b18fSScott Teel 			(u32)((first_block % stripesize) %
43436b80b18fSScott Teel 						r5or6_blocks_per_row);
43446b80b18fSScott Teel 
43456b80b18fSScott Teel 		r5or6_last_row_offset =
43466b80b18fSScott Teel 			(u32)((last_block % stripesize) %
43476b80b18fSScott Teel 						r5or6_blocks_per_row);
43486b80b18fSScott Teel 
43496b80b18fSScott Teel 		first_column = r5or6_first_column =
43502b08b3e9SDon Brace 			r5or6_first_row_offset / le16_to_cpu(map->strip_size);
43516b80b18fSScott Teel 		r5or6_last_column =
43522b08b3e9SDon Brace 			r5or6_last_row_offset / le16_to_cpu(map->strip_size);
43536b80b18fSScott Teel #endif
43546b80b18fSScott Teel 		if (r5or6_first_column != r5or6_last_column)
43556b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
43566b80b18fSScott Teel 
43576b80b18fSScott Teel 		/* Request is eligible */
43586b80b18fSScott Teel 		map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
43592b08b3e9SDon Brace 			le16_to_cpu(map->row_cnt);
43606b80b18fSScott Teel 
43616b80b18fSScott Teel 		map_index = (first_group *
43622b08b3e9SDon Brace 			(le16_to_cpu(map->row_cnt) * total_disks_per_row)) +
43636b80b18fSScott Teel 			(map_row * total_disks_per_row) + first_column;
43646b80b18fSScott Teel 		break;
43656b80b18fSScott Teel 	default:
43666b80b18fSScott Teel 		return IO_ACCEL_INELIGIBLE;
4367283b4a9bSStephen M. Cameron 	}
43686b80b18fSScott Teel 
436907543e0cSStephen Cameron 	if (unlikely(map_index >= RAID_MAP_MAX_ENTRIES))
437007543e0cSStephen Cameron 		return IO_ACCEL_INELIGIBLE;
437107543e0cSStephen Cameron 
437203383736SDon Brace 	c->phys_disk = dev->phys_disk[map_index];
437303383736SDon Brace 
4374283b4a9bSStephen M. Cameron 	disk_handle = dd[map_index].ioaccel_handle;
43752b08b3e9SDon Brace 	disk_block = le64_to_cpu(map->disk_starting_blk) +
43762b08b3e9SDon Brace 			first_row * le16_to_cpu(map->strip_size) +
43772b08b3e9SDon Brace 			(first_row_offset - first_column *
43782b08b3e9SDon Brace 			le16_to_cpu(map->strip_size));
4379283b4a9bSStephen M. Cameron 	disk_block_cnt = block_cnt;
4380283b4a9bSStephen M. Cameron 
4381283b4a9bSStephen M. Cameron 	/* handle differing logical/physical block sizes */
4382283b4a9bSStephen M. Cameron 	if (map->phys_blk_shift) {
4383283b4a9bSStephen M. Cameron 		disk_block <<= map->phys_blk_shift;
4384283b4a9bSStephen M. Cameron 		disk_block_cnt <<= map->phys_blk_shift;
4385283b4a9bSStephen M. Cameron 	}
4386283b4a9bSStephen M. Cameron 	BUG_ON(disk_block_cnt > 0xffff);
4387283b4a9bSStephen M. Cameron 
4388283b4a9bSStephen M. Cameron 	/* build the new CDB for the physical disk I/O */
4389283b4a9bSStephen M. Cameron 	if (disk_block > 0xffffffff) {
4390283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_16 : READ_16;
4391283b4a9bSStephen M. Cameron 		cdb[1] = 0;
4392283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 56);
4393283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 48);
4394283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 40);
4395283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block >> 32);
4396283b4a9bSStephen M. Cameron 		cdb[6] = (u8) (disk_block >> 24);
4397283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block >> 16);
4398283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block >> 8);
4399283b4a9bSStephen M. Cameron 		cdb[9] = (u8) (disk_block);
4400283b4a9bSStephen M. Cameron 		cdb[10] = (u8) (disk_block_cnt >> 24);
4401283b4a9bSStephen M. Cameron 		cdb[11] = (u8) (disk_block_cnt >> 16);
4402283b4a9bSStephen M. Cameron 		cdb[12] = (u8) (disk_block_cnt >> 8);
4403283b4a9bSStephen M. Cameron 		cdb[13] = (u8) (disk_block_cnt);
4404283b4a9bSStephen M. Cameron 		cdb[14] = 0;
4405283b4a9bSStephen M. Cameron 		cdb[15] = 0;
4406283b4a9bSStephen M. Cameron 		cdb_len = 16;
4407283b4a9bSStephen M. Cameron 	} else {
4408283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
4409283b4a9bSStephen M. Cameron 		cdb[1] = 0;
4410283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 24);
4411283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 16);
4412283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 8);
4413283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block);
4414283b4a9bSStephen M. Cameron 		cdb[6] = 0;
4415283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block_cnt >> 8);
4416283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block_cnt);
4417283b4a9bSStephen M. Cameron 		cdb[9] = 0;
4418283b4a9bSStephen M. Cameron 		cdb_len = 10;
4419283b4a9bSStephen M. Cameron 	}
4420283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
442103383736SDon Brace 						dev->scsi3addr,
442203383736SDon Brace 						dev->phys_disk[map_index]);
4423283b4a9bSStephen M. Cameron }
4424283b4a9bSStephen M. Cameron 
442525163bd5SWebb Scales /*
442625163bd5SWebb Scales  * Submit commands down the "normal" RAID stack path
442725163bd5SWebb Scales  * All callers to hpsa_ciss_submit must check lockup_detected
442825163bd5SWebb Scales  * beforehand, before (opt.) and after calling cmd_alloc
442925163bd5SWebb Scales  */
4430574f05d3SStephen Cameron static int hpsa_ciss_submit(struct ctlr_info *h,
4431574f05d3SStephen Cameron 	struct CommandList *c, struct scsi_cmnd *cmd,
4432574f05d3SStephen Cameron 	unsigned char scsi3addr[])
4433edd16368SStephen M. Cameron {
4434edd16368SStephen M. Cameron 	cmd->host_scribble = (unsigned char *) c;
4435edd16368SStephen M. Cameron 	c->cmd_type = CMD_SCSI;
4436edd16368SStephen M. Cameron 	c->scsi_cmd = cmd;
4437edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;  /* unused in simple mode */
4438edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
4439f2405db8SDon Brace 	c->Header.tag = cpu_to_le64((c->cmdindex << DIRECT_LOOKUP_SHIFT));
4440edd16368SStephen M. Cameron 
4441edd16368SStephen M. Cameron 	/* Fill in the request block... */
4442edd16368SStephen M. Cameron 
4443edd16368SStephen M. Cameron 	c->Request.Timeout = 0;
4444edd16368SStephen M. Cameron 	BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
4445edd16368SStephen M. Cameron 	c->Request.CDBLen = cmd->cmd_len;
4446edd16368SStephen M. Cameron 	memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
4447edd16368SStephen M. Cameron 	switch (cmd->sc_data_direction) {
4448edd16368SStephen M. Cameron 	case DMA_TO_DEVICE:
4449a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4450a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_WRITE);
4451edd16368SStephen M. Cameron 		break;
4452edd16368SStephen M. Cameron 	case DMA_FROM_DEVICE:
4453a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4454a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_READ);
4455edd16368SStephen M. Cameron 		break;
4456edd16368SStephen M. Cameron 	case DMA_NONE:
4457a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4458a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_NONE);
4459edd16368SStephen M. Cameron 		break;
4460edd16368SStephen M. Cameron 	case DMA_BIDIRECTIONAL:
4461edd16368SStephen M. Cameron 		/* This can happen if a buggy application does a scsi passthru
4462edd16368SStephen M. Cameron 		 * and sets both inlen and outlen to non-zero. ( see
4463edd16368SStephen M. Cameron 		 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
4464edd16368SStephen M. Cameron 		 */
4465edd16368SStephen M. Cameron 
4466a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4467a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_RSVD);
4468edd16368SStephen M. Cameron 		/* This is technically wrong, and hpsa controllers should
4469edd16368SStephen M. Cameron 		 * reject it with CMD_INVALID, which is the most correct
4470edd16368SStephen M. Cameron 		 * response, but non-fibre backends appear to let it
4471edd16368SStephen M. Cameron 		 * slide by, and give the same results as if this field
4472edd16368SStephen M. Cameron 		 * were set correctly.  Either way is acceptable for
4473edd16368SStephen M. Cameron 		 * our purposes here.
4474edd16368SStephen M. Cameron 		 */
4475edd16368SStephen M. Cameron 
4476edd16368SStephen M. Cameron 		break;
4477edd16368SStephen M. Cameron 
4478edd16368SStephen M. Cameron 	default:
4479edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4480edd16368SStephen M. Cameron 			cmd->sc_data_direction);
4481edd16368SStephen M. Cameron 		BUG();
4482edd16368SStephen M. Cameron 		break;
4483edd16368SStephen M. Cameron 	}
4484edd16368SStephen M. Cameron 
448533a2ffceSStephen M. Cameron 	if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
4486edd16368SStephen M. Cameron 		cmd_free(h, c);
4487edd16368SStephen M. Cameron 		return SCSI_MLQUEUE_HOST_BUSY;
4488edd16368SStephen M. Cameron 	}
4489edd16368SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
4490edd16368SStephen M. Cameron 	/* the cmd'll come back via intr handler in complete_scsi_command()  */
4491edd16368SStephen M. Cameron 	return 0;
4492edd16368SStephen M. Cameron }
4493edd16368SStephen M. Cameron 
4494360c73bdSStephen Cameron static void hpsa_cmd_init(struct ctlr_info *h, int index,
4495360c73bdSStephen Cameron 				struct CommandList *c)
4496360c73bdSStephen Cameron {
4497360c73bdSStephen Cameron 	dma_addr_t cmd_dma_handle, err_dma_handle;
4498360c73bdSStephen Cameron 
4499360c73bdSStephen Cameron 	/* Zero out all of commandlist except the last field, refcount */
4500360c73bdSStephen Cameron 	memset(c, 0, offsetof(struct CommandList, refcount));
4501360c73bdSStephen Cameron 	c->Header.tag = cpu_to_le64((u64) (index << DIRECT_LOOKUP_SHIFT));
4502360c73bdSStephen Cameron 	cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
4503360c73bdSStephen Cameron 	c->err_info = h->errinfo_pool + index;
4504360c73bdSStephen Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
4505360c73bdSStephen Cameron 	err_dma_handle = h->errinfo_pool_dhandle
4506360c73bdSStephen Cameron 	    + index * sizeof(*c->err_info);
4507360c73bdSStephen Cameron 	c->cmdindex = index;
4508360c73bdSStephen Cameron 	c->busaddr = (u32) cmd_dma_handle;
4509360c73bdSStephen Cameron 	c->ErrDesc.Addr = cpu_to_le64((u64) err_dma_handle);
4510360c73bdSStephen Cameron 	c->ErrDesc.Len = cpu_to_le32((u32) sizeof(*c->err_info));
4511360c73bdSStephen Cameron 	c->h = h;
4512*a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_IDLE;
4513360c73bdSStephen Cameron }
4514360c73bdSStephen Cameron 
4515360c73bdSStephen Cameron static void hpsa_preinitialize_commands(struct ctlr_info *h)
4516360c73bdSStephen Cameron {
4517360c73bdSStephen Cameron 	int i;
4518360c73bdSStephen Cameron 
4519360c73bdSStephen Cameron 	for (i = 0; i < h->nr_cmds; i++) {
4520360c73bdSStephen Cameron 		struct CommandList *c = h->cmd_pool + i;
4521360c73bdSStephen Cameron 
4522360c73bdSStephen Cameron 		hpsa_cmd_init(h, i, c);
4523360c73bdSStephen Cameron 		atomic_set(&c->refcount, 0);
4524360c73bdSStephen Cameron 	}
4525360c73bdSStephen Cameron }
4526360c73bdSStephen Cameron 
4527360c73bdSStephen Cameron static inline void hpsa_cmd_partial_init(struct ctlr_info *h, int index,
4528360c73bdSStephen Cameron 				struct CommandList *c)
4529360c73bdSStephen Cameron {
4530360c73bdSStephen Cameron 	dma_addr_t cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
4531360c73bdSStephen Cameron 
4532360c73bdSStephen Cameron 	memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
4533360c73bdSStephen Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
4534360c73bdSStephen Cameron 	c->busaddr = (u32) cmd_dma_handle;
4535360c73bdSStephen Cameron }
4536360c73bdSStephen Cameron 
4537592a0ad5SWebb Scales static int hpsa_ioaccel_submit(struct ctlr_info *h,
4538592a0ad5SWebb Scales 		struct CommandList *c, struct scsi_cmnd *cmd,
4539592a0ad5SWebb Scales 		unsigned char *scsi3addr)
4540592a0ad5SWebb Scales {
4541592a0ad5SWebb Scales 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4542592a0ad5SWebb Scales 	int rc = IO_ACCEL_INELIGIBLE;
4543592a0ad5SWebb Scales 
4544592a0ad5SWebb Scales 	cmd->host_scribble = (unsigned char *) c;
4545592a0ad5SWebb Scales 
4546592a0ad5SWebb Scales 	if (dev->offload_enabled) {
4547592a0ad5SWebb Scales 		hpsa_cmd_init(h, c->cmdindex, c);
4548592a0ad5SWebb Scales 		c->cmd_type = CMD_SCSI;
4549592a0ad5SWebb Scales 		c->scsi_cmd = cmd;
4550592a0ad5SWebb Scales 		rc = hpsa_scsi_ioaccel_raid_map(h, c);
4551592a0ad5SWebb Scales 		if (rc < 0)     /* scsi_dma_map failed. */
4552592a0ad5SWebb Scales 			rc = SCSI_MLQUEUE_HOST_BUSY;
4553a3144e0bSJoe Handzik 	} else if (dev->hba_ioaccel_enabled) {
4554592a0ad5SWebb Scales 		hpsa_cmd_init(h, c->cmdindex, c);
4555592a0ad5SWebb Scales 		c->cmd_type = CMD_SCSI;
4556592a0ad5SWebb Scales 		c->scsi_cmd = cmd;
4557592a0ad5SWebb Scales 		rc = hpsa_scsi_ioaccel_direct_map(h, c);
4558592a0ad5SWebb Scales 		if (rc < 0)     /* scsi_dma_map failed. */
4559592a0ad5SWebb Scales 			rc = SCSI_MLQUEUE_HOST_BUSY;
4560592a0ad5SWebb Scales 	}
4561592a0ad5SWebb Scales 	return rc;
4562592a0ad5SWebb Scales }
4563592a0ad5SWebb Scales 
4564080ef1ccSDon Brace static void hpsa_command_resubmit_worker(struct work_struct *work)
4565080ef1ccSDon Brace {
4566080ef1ccSDon Brace 	struct scsi_cmnd *cmd;
4567080ef1ccSDon Brace 	struct hpsa_scsi_dev_t *dev;
45688a0ff92cSWebb Scales 	struct CommandList *c = container_of(work, struct CommandList, work);
4569080ef1ccSDon Brace 
4570080ef1ccSDon Brace 	cmd = c->scsi_cmd;
4571080ef1ccSDon Brace 	dev = cmd->device->hostdata;
4572080ef1ccSDon Brace 	if (!dev) {
4573080ef1ccSDon Brace 		cmd->result = DID_NO_CONNECT << 16;
45748a0ff92cSWebb Scales 		return hpsa_cmd_free_and_done(c->h, c, cmd);
4575080ef1ccSDon Brace 	}
4576*a58e7e53SWebb Scales 	if (c->abort_pending)
4577*a58e7e53SWebb Scales 		return hpsa_cmd_abort_and_free(c->h, c, cmd);
4578592a0ad5SWebb Scales 	if (c->cmd_type == CMD_IOACCEL2) {
4579592a0ad5SWebb Scales 		struct ctlr_info *h = c->h;
4580592a0ad5SWebb Scales 		struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
4581592a0ad5SWebb Scales 		int rc;
4582592a0ad5SWebb Scales 
4583592a0ad5SWebb Scales 		if (c2->error_data.serv_response ==
4584592a0ad5SWebb Scales 				IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL) {
4585592a0ad5SWebb Scales 			rc = hpsa_ioaccel_submit(h, c, cmd, dev->scsi3addr);
4586592a0ad5SWebb Scales 			if (rc == 0)
4587592a0ad5SWebb Scales 				return;
4588592a0ad5SWebb Scales 			if (rc == SCSI_MLQUEUE_HOST_BUSY) {
4589592a0ad5SWebb Scales 				/*
4590592a0ad5SWebb Scales 				 * If we get here, it means dma mapping failed.
4591592a0ad5SWebb Scales 				 * Try again via scsi mid layer, which will
4592592a0ad5SWebb Scales 				 * then get SCSI_MLQUEUE_HOST_BUSY.
4593592a0ad5SWebb Scales 				 */
4594592a0ad5SWebb Scales 				cmd->result = DID_IMM_RETRY << 16;
45958a0ff92cSWebb Scales 				return hpsa_cmd_free_and_done(h, c, cmd);
4596592a0ad5SWebb Scales 			}
4597592a0ad5SWebb Scales 			/* else, fall thru and resubmit down CISS path */
4598592a0ad5SWebb Scales 		}
4599592a0ad5SWebb Scales 	}
4600360c73bdSStephen Cameron 	hpsa_cmd_partial_init(c->h, c->cmdindex, c);
4601080ef1ccSDon Brace 	if (hpsa_ciss_submit(c->h, c, cmd, dev->scsi3addr)) {
4602080ef1ccSDon Brace 		/*
4603080ef1ccSDon Brace 		 * If we get here, it means dma mapping failed. Try
4604080ef1ccSDon Brace 		 * again via scsi mid layer, which will then get
4605080ef1ccSDon Brace 		 * SCSI_MLQUEUE_HOST_BUSY.
4606592a0ad5SWebb Scales 		 *
4607592a0ad5SWebb Scales 		 * hpsa_ciss_submit will have already freed c
4608592a0ad5SWebb Scales 		 * if it encountered a dma mapping failure.
4609080ef1ccSDon Brace 		 */
4610080ef1ccSDon Brace 		cmd->result = DID_IMM_RETRY << 16;
4611080ef1ccSDon Brace 		cmd->scsi_done(cmd);
4612080ef1ccSDon Brace 	}
4613080ef1ccSDon Brace }
4614080ef1ccSDon Brace 
4615574f05d3SStephen Cameron /* Running in struct Scsi_Host->host_lock less mode */
4616574f05d3SStephen Cameron static int hpsa_scsi_queue_command(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
4617574f05d3SStephen Cameron {
4618574f05d3SStephen Cameron 	struct ctlr_info *h;
4619574f05d3SStephen Cameron 	struct hpsa_scsi_dev_t *dev;
4620574f05d3SStephen Cameron 	unsigned char scsi3addr[8];
4621574f05d3SStephen Cameron 	struct CommandList *c;
4622574f05d3SStephen Cameron 	int rc = 0;
4623574f05d3SStephen Cameron 
4624574f05d3SStephen Cameron 	/* Get the ptr to our adapter structure out of cmd->host. */
4625574f05d3SStephen Cameron 	h = sdev_to_hba(cmd->device);
4626574f05d3SStephen Cameron 	dev = cmd->device->hostdata;
4627574f05d3SStephen Cameron 	if (!dev) {
4628574f05d3SStephen Cameron 		cmd->result = DID_NO_CONNECT << 16;
4629574f05d3SStephen Cameron 		cmd->scsi_done(cmd);
4630574f05d3SStephen Cameron 		return 0;
4631574f05d3SStephen Cameron 	}
4632574f05d3SStephen Cameron 	memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
4633574f05d3SStephen Cameron 
4634574f05d3SStephen Cameron 	if (unlikely(lockup_detected(h))) {
463525163bd5SWebb Scales 		cmd->result = DID_NO_CONNECT << 16;
4636574f05d3SStephen Cameron 		cmd->scsi_done(cmd);
4637574f05d3SStephen Cameron 		return 0;
4638574f05d3SStephen Cameron 	}
4639574f05d3SStephen Cameron 	c = cmd_alloc(h);
4640bf43caf3SRobert Elliott 
4641407863cbSStephen Cameron 	if (unlikely(lockup_detected(h))) {
464225163bd5SWebb Scales 		cmd->result = DID_NO_CONNECT << 16;
4643407863cbSStephen Cameron 		cmd_free(h, c);
4644407863cbSStephen Cameron 		cmd->scsi_done(cmd);
4645407863cbSStephen Cameron 		return 0;
4646407863cbSStephen Cameron 	}
4647574f05d3SStephen Cameron 
4648407863cbSStephen Cameron 	/*
4649407863cbSStephen Cameron 	 * Call alternate submit routine for I/O accelerated commands.
4650574f05d3SStephen Cameron 	 * Retries always go down the normal I/O path.
4651574f05d3SStephen Cameron 	 */
4652574f05d3SStephen Cameron 	if (likely(cmd->retries == 0 &&
4653574f05d3SStephen Cameron 		cmd->request->cmd_type == REQ_TYPE_FS &&
4654574f05d3SStephen Cameron 		h->acciopath_status)) {
4655592a0ad5SWebb Scales 		rc = hpsa_ioaccel_submit(h, c, cmd, scsi3addr);
4656574f05d3SStephen Cameron 		if (rc == 0)
4657592a0ad5SWebb Scales 			return 0;
4658592a0ad5SWebb Scales 		if (rc == SCSI_MLQUEUE_HOST_BUSY) {
46598a0ff92cSWebb Scales 			cmd_free(h, c);
4660574f05d3SStephen Cameron 			return SCSI_MLQUEUE_HOST_BUSY;
4661574f05d3SStephen Cameron 		}
4662574f05d3SStephen Cameron 	}
4663574f05d3SStephen Cameron 	return hpsa_ciss_submit(h, c, cmd, scsi3addr);
4664574f05d3SStephen Cameron }
4665574f05d3SStephen Cameron 
46668ebc9248SWebb Scales static void hpsa_scan_complete(struct ctlr_info *h)
46675f389360SStephen M. Cameron {
46685f389360SStephen M. Cameron 	unsigned long flags;
46695f389360SStephen M. Cameron 
46705f389360SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
46715f389360SStephen M. Cameron 	h->scan_finished = 1;
46725f389360SStephen M. Cameron 	wake_up_all(&h->scan_wait_queue);
46735f389360SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
46745f389360SStephen M. Cameron }
46755f389360SStephen M. Cameron 
4676a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *sh)
4677a08a8471SStephen M. Cameron {
4678a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
4679a08a8471SStephen M. Cameron 	unsigned long flags;
4680a08a8471SStephen M. Cameron 
46818ebc9248SWebb Scales 	/*
46828ebc9248SWebb Scales 	 * Don't let rescans be initiated on a controller known to be locked
46838ebc9248SWebb Scales 	 * up.  If the controller locks up *during* a rescan, that thread is
46848ebc9248SWebb Scales 	 * probably hosed, but at least we can prevent new rescan threads from
46858ebc9248SWebb Scales 	 * piling up on a locked up controller.
46868ebc9248SWebb Scales 	 */
46878ebc9248SWebb Scales 	if (unlikely(lockup_detected(h)))
46888ebc9248SWebb Scales 		return hpsa_scan_complete(h);
46895f389360SStephen M. Cameron 
4690a08a8471SStephen M. Cameron 	/* wait until any scan already in progress is finished. */
4691a08a8471SStephen M. Cameron 	while (1) {
4692a08a8471SStephen M. Cameron 		spin_lock_irqsave(&h->scan_lock, flags);
4693a08a8471SStephen M. Cameron 		if (h->scan_finished)
4694a08a8471SStephen M. Cameron 			break;
4695a08a8471SStephen M. Cameron 		spin_unlock_irqrestore(&h->scan_lock, flags);
4696a08a8471SStephen M. Cameron 		wait_event(h->scan_wait_queue, h->scan_finished);
4697a08a8471SStephen M. Cameron 		/* Note: We don't need to worry about a race between this
4698a08a8471SStephen M. Cameron 		 * thread and driver unload because the midlayer will
4699a08a8471SStephen M. Cameron 		 * have incremented the reference count, so unload won't
4700a08a8471SStephen M. Cameron 		 * happen if we're in here.
4701a08a8471SStephen M. Cameron 		 */
4702a08a8471SStephen M. Cameron 	}
4703a08a8471SStephen M. Cameron 	h->scan_finished = 0; /* mark scan as in progress */
4704a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4705a08a8471SStephen M. Cameron 
47068ebc9248SWebb Scales 	if (unlikely(lockup_detected(h)))
47078ebc9248SWebb Scales 		return hpsa_scan_complete(h);
47085f389360SStephen M. Cameron 
4709a08a8471SStephen M. Cameron 	hpsa_update_scsi_devices(h, h->scsi_host->host_no);
4710a08a8471SStephen M. Cameron 
47118ebc9248SWebb Scales 	hpsa_scan_complete(h);
4712a08a8471SStephen M. Cameron }
4713a08a8471SStephen M. Cameron 
47147c0a0229SDon Brace static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth)
47157c0a0229SDon Brace {
471603383736SDon Brace 	struct hpsa_scsi_dev_t *logical_drive = sdev->hostdata;
471703383736SDon Brace 
471803383736SDon Brace 	if (!logical_drive)
471903383736SDon Brace 		return -ENODEV;
47207c0a0229SDon Brace 
47217c0a0229SDon Brace 	if (qdepth < 1)
47227c0a0229SDon Brace 		qdepth = 1;
472303383736SDon Brace 	else if (qdepth > logical_drive->queue_depth)
472403383736SDon Brace 		qdepth = logical_drive->queue_depth;
472503383736SDon Brace 
472603383736SDon Brace 	return scsi_change_queue_depth(sdev, qdepth);
47277c0a0229SDon Brace }
47287c0a0229SDon Brace 
4729a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
4730a08a8471SStephen M. Cameron 	unsigned long elapsed_time)
4731a08a8471SStephen M. Cameron {
4732a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
4733a08a8471SStephen M. Cameron 	unsigned long flags;
4734a08a8471SStephen M. Cameron 	int finished;
4735a08a8471SStephen M. Cameron 
4736a08a8471SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
4737a08a8471SStephen M. Cameron 	finished = h->scan_finished;
4738a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4739a08a8471SStephen M. Cameron 	return finished;
4740a08a8471SStephen M. Cameron }
4741a08a8471SStephen M. Cameron 
4742edd16368SStephen M. Cameron static void hpsa_unregister_scsi(struct ctlr_info *h)
4743edd16368SStephen M. Cameron {
4744edd16368SStephen M. Cameron 	/* we are being forcibly unloaded, and may not refuse. */
4745edd16368SStephen M. Cameron 	scsi_remove_host(h->scsi_host);
4746edd16368SStephen M. Cameron 	scsi_host_put(h->scsi_host);
4747edd16368SStephen M. Cameron 	h->scsi_host = NULL;
4748edd16368SStephen M. Cameron }
4749edd16368SStephen M. Cameron 
4750edd16368SStephen M. Cameron static int hpsa_register_scsi(struct ctlr_info *h)
4751edd16368SStephen M. Cameron {
4752b705690dSStephen M. Cameron 	struct Scsi_Host *sh;
4753b705690dSStephen M. Cameron 	int error;
4754edd16368SStephen M. Cameron 
4755b705690dSStephen M. Cameron 	sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
4756b705690dSStephen M. Cameron 	if (sh == NULL)
4757b705690dSStephen M. Cameron 		goto fail;
4758b705690dSStephen M. Cameron 
4759b705690dSStephen M. Cameron 	sh->io_port = 0;
4760b705690dSStephen M. Cameron 	sh->n_io_port = 0;
4761b705690dSStephen M. Cameron 	sh->this_id = -1;
4762b705690dSStephen M. Cameron 	sh->max_channel = 3;
4763b705690dSStephen M. Cameron 	sh->max_cmd_len = MAX_COMMAND_SIZE;
4764b705690dSStephen M. Cameron 	sh->max_lun = HPSA_MAX_LUN;
4765b705690dSStephen M. Cameron 	sh->max_id = HPSA_MAX_LUN;
476641ce4c35SStephen Cameron 	sh->can_queue = h->nr_cmds - HPSA_NRESERVED_CMDS;
4767d54c5c24SStephen Cameron 	sh->cmd_per_lun = sh->can_queue;
4768b705690dSStephen M. Cameron 	sh->sg_tablesize = h->maxsgentries;
4769b705690dSStephen M. Cameron 	h->scsi_host = sh;
4770b705690dSStephen M. Cameron 	sh->hostdata[0] = (unsigned long) h;
4771b705690dSStephen M. Cameron 	sh->irq = h->intr[h->intr_mode];
4772b705690dSStephen M. Cameron 	sh->unique_id = sh->irq;
4773b705690dSStephen M. Cameron 	error = scsi_add_host(sh, &h->pdev->dev);
4774b705690dSStephen M. Cameron 	if (error)
4775b705690dSStephen M. Cameron 		goto fail_host_put;
4776b705690dSStephen M. Cameron 	scsi_scan_host(sh);
4777b705690dSStephen M. Cameron 	return 0;
4778b705690dSStephen M. Cameron 
4779b705690dSStephen M. Cameron  fail_host_put:
4780b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_add_host"
4781b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
4782b705690dSStephen M. Cameron 	scsi_host_put(sh);
4783b705690dSStephen M. Cameron 	return error;
4784b705690dSStephen M. Cameron  fail:
4785b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_host_alloc"
4786b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
4787b705690dSStephen M. Cameron 	return -ENOMEM;
4788edd16368SStephen M. Cameron }
4789edd16368SStephen M. Cameron 
4790edd16368SStephen M. Cameron static int wait_for_device_to_become_ready(struct ctlr_info *h,
4791edd16368SStephen M. Cameron 	unsigned char lunaddr[])
4792edd16368SStephen M. Cameron {
47938919358eSTomas Henzl 	int rc;
4794edd16368SStephen M. Cameron 	int count = 0;
4795edd16368SStephen M. Cameron 	int waittime = 1; /* seconds */
4796edd16368SStephen M. Cameron 	struct CommandList *c;
4797edd16368SStephen M. Cameron 
479845fcb86eSStephen Cameron 	c = cmd_alloc(h);
4799edd16368SStephen M. Cameron 
4800edd16368SStephen M. Cameron 	/* Send test unit ready until device ready, or give up. */
4801edd16368SStephen M. Cameron 	while (count < HPSA_TUR_RETRY_LIMIT) {
4802edd16368SStephen M. Cameron 
4803edd16368SStephen M. Cameron 		/* Wait for a bit.  do this first, because if we send
4804edd16368SStephen M. Cameron 		 * the TUR right away, the reset will just abort it.
4805edd16368SStephen M. Cameron 		 */
4806edd16368SStephen M. Cameron 		msleep(1000 * waittime);
4807edd16368SStephen M. Cameron 		count++;
48088919358eSTomas Henzl 		rc = 0; /* Device ready. */
4809edd16368SStephen M. Cameron 
4810edd16368SStephen M. Cameron 		/* Increase wait time with each try, up to a point. */
4811edd16368SStephen M. Cameron 		if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
4812edd16368SStephen M. Cameron 			waittime = waittime * 2;
4813edd16368SStephen M. Cameron 
4814a2dac136SStephen M. Cameron 		/* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
4815a2dac136SStephen M. Cameron 		(void) fill_cmd(c, TEST_UNIT_READY, h,
4816a2dac136SStephen M. Cameron 				NULL, 0, 0, lunaddr, TYPE_CMD);
481725163bd5SWebb Scales 		rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
481825163bd5SWebb Scales 						NO_TIMEOUT);
481925163bd5SWebb Scales 		if (rc)
482025163bd5SWebb Scales 			goto do_it_again;
4821edd16368SStephen M. Cameron 		/* no unmap needed here because no data xfer. */
4822edd16368SStephen M. Cameron 
4823edd16368SStephen M. Cameron 		if (c->err_info->CommandStatus == CMD_SUCCESS)
4824edd16368SStephen M. Cameron 			break;
4825edd16368SStephen M. Cameron 
4826edd16368SStephen M. Cameron 		if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
4827edd16368SStephen M. Cameron 			c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
4828edd16368SStephen M. Cameron 			(c->err_info->SenseInfo[2] == NO_SENSE ||
4829edd16368SStephen M. Cameron 			c->err_info->SenseInfo[2] == UNIT_ATTENTION))
4830edd16368SStephen M. Cameron 			break;
483125163bd5SWebb Scales do_it_again:
4832edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "waiting %d secs "
4833edd16368SStephen M. Cameron 			"for device to become ready.\n", waittime);
4834edd16368SStephen M. Cameron 		rc = 1; /* device not ready. */
4835edd16368SStephen M. Cameron 	}
4836edd16368SStephen M. Cameron 
4837edd16368SStephen M. Cameron 	if (rc)
4838edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "giving up on device.\n");
4839edd16368SStephen M. Cameron 	else
4840edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "device is ready.\n");
4841edd16368SStephen M. Cameron 
484245fcb86eSStephen Cameron 	cmd_free(h, c);
4843edd16368SStephen M. Cameron 	return rc;
4844edd16368SStephen M. Cameron }
4845edd16368SStephen M. Cameron 
4846edd16368SStephen M. Cameron /* Need at least one of these error handlers to keep ../scsi/hosts.c from
4847edd16368SStephen M. Cameron  * complaining.  Doing a host- or bus-reset can't do anything good here.
4848edd16368SStephen M. Cameron  */
4849edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
4850edd16368SStephen M. Cameron {
4851edd16368SStephen M. Cameron 	int rc;
4852edd16368SStephen M. Cameron 	struct ctlr_info *h;
4853edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
4854edd16368SStephen M. Cameron 
4855edd16368SStephen M. Cameron 	/* find the controller to which the command to be aborted was sent */
4856edd16368SStephen M. Cameron 	h = sdev_to_hba(scsicmd->device);
4857edd16368SStephen M. Cameron 	if (h == NULL) /* paranoia */
4858edd16368SStephen M. Cameron 		return FAILED;
4859e345893bSDon Brace 
4860e345893bSDon Brace 	if (lockup_detected(h))
4861e345893bSDon Brace 		return FAILED;
4862e345893bSDon Brace 
4863edd16368SStephen M. Cameron 	dev = scsicmd->device->hostdata;
4864edd16368SStephen M. Cameron 	if (!dev) {
4865edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
4866edd16368SStephen M. Cameron 			"device lookup failed.\n");
4867edd16368SStephen M. Cameron 		return FAILED;
4868edd16368SStephen M. Cameron 	}
486925163bd5SWebb Scales 
487025163bd5SWebb Scales 	/* if controller locked up, we can guarantee command won't complete */
487125163bd5SWebb Scales 	if (lockup_detected(h)) {
487225163bd5SWebb Scales 		dev_warn(&h->pdev->dev,
487325163bd5SWebb Scales 			"scsi %d:%d:%d:%d RESET FAILED, lockup detected\n",
487425163bd5SWebb Scales 			h->scsi_host->host_no, dev->bus, dev->target,
487525163bd5SWebb Scales 			dev->lun);
487625163bd5SWebb Scales 		return FAILED;
487725163bd5SWebb Scales 	}
487825163bd5SWebb Scales 
487925163bd5SWebb Scales 	/* this reset request might be the result of a lockup; check */
488025163bd5SWebb Scales 	if (detect_controller_lockup(h)) {
488125163bd5SWebb Scales 		dev_warn(&h->pdev->dev,
488225163bd5SWebb Scales 			 "scsi %d:%d:%d:%d RESET FAILED, new lockup detected\n",
488325163bd5SWebb Scales 			 h->scsi_host->host_no, dev->bus, dev->target,
488425163bd5SWebb Scales 			 dev->lun);
488525163bd5SWebb Scales 		return FAILED;
488625163bd5SWebb Scales 	}
488725163bd5SWebb Scales 
488825163bd5SWebb Scales 	hpsa_show_dev_msg(KERN_WARNING, h, dev, "resetting");
488925163bd5SWebb Scales 
4890edd16368SStephen M. Cameron 	/* send a reset to the SCSI LUN which the command was sent to */
489125163bd5SWebb Scales 	rc = hpsa_send_reset(h, dev->scsi3addr, HPSA_RESET_TYPE_LUN,
489225163bd5SWebb Scales 			     DEFAULT_REPLY_QUEUE);
4893edd16368SStephen M. Cameron 	if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
4894edd16368SStephen M. Cameron 		return SUCCESS;
4895edd16368SStephen M. Cameron 
489625163bd5SWebb Scales 	dev_warn(&h->pdev->dev,
489725163bd5SWebb Scales 		"scsi %d:%d:%d:%d reset failed\n",
489825163bd5SWebb Scales 		h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
4899edd16368SStephen M. Cameron 	return FAILED;
4900edd16368SStephen M. Cameron }
4901edd16368SStephen M. Cameron 
49026cba3f19SStephen M. Cameron static void swizzle_abort_tag(u8 *tag)
49036cba3f19SStephen M. Cameron {
49046cba3f19SStephen M. Cameron 	u8 original_tag[8];
49056cba3f19SStephen M. Cameron 
49066cba3f19SStephen M. Cameron 	memcpy(original_tag, tag, 8);
49076cba3f19SStephen M. Cameron 	tag[0] = original_tag[3];
49086cba3f19SStephen M. Cameron 	tag[1] = original_tag[2];
49096cba3f19SStephen M. Cameron 	tag[2] = original_tag[1];
49106cba3f19SStephen M. Cameron 	tag[3] = original_tag[0];
49116cba3f19SStephen M. Cameron 	tag[4] = original_tag[7];
49126cba3f19SStephen M. Cameron 	tag[5] = original_tag[6];
49136cba3f19SStephen M. Cameron 	tag[6] = original_tag[5];
49146cba3f19SStephen M. Cameron 	tag[7] = original_tag[4];
49156cba3f19SStephen M. Cameron }
49166cba3f19SStephen M. Cameron 
491717eb87d2SScott Teel static void hpsa_get_tag(struct ctlr_info *h,
49182b08b3e9SDon Brace 	struct CommandList *c, __le32 *taglower, __le32 *tagupper)
491917eb87d2SScott Teel {
49202b08b3e9SDon Brace 	u64 tag;
492117eb87d2SScott Teel 	if (c->cmd_type == CMD_IOACCEL1) {
492217eb87d2SScott Teel 		struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
492317eb87d2SScott Teel 			&h->ioaccel_cmd_pool[c->cmdindex];
49242b08b3e9SDon Brace 		tag = le64_to_cpu(cm1->tag);
49252b08b3e9SDon Brace 		*tagupper = cpu_to_le32(tag >> 32);
49262b08b3e9SDon Brace 		*taglower = cpu_to_le32(tag);
492754b6e9e9SScott Teel 		return;
492854b6e9e9SScott Teel 	}
492954b6e9e9SScott Teel 	if (c->cmd_type == CMD_IOACCEL2) {
493054b6e9e9SScott Teel 		struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
493154b6e9e9SScott Teel 			&h->ioaccel2_cmd_pool[c->cmdindex];
4932dd0e19f3SScott Teel 		/* upper tag not used in ioaccel2 mode */
4933dd0e19f3SScott Teel 		memset(tagupper, 0, sizeof(*tagupper));
4934dd0e19f3SScott Teel 		*taglower = cm2->Tag;
493554b6e9e9SScott Teel 		return;
493654b6e9e9SScott Teel 	}
49372b08b3e9SDon Brace 	tag = le64_to_cpu(c->Header.tag);
49382b08b3e9SDon Brace 	*tagupper = cpu_to_le32(tag >> 32);
49392b08b3e9SDon Brace 	*taglower = cpu_to_le32(tag);
494017eb87d2SScott Teel }
494154b6e9e9SScott Teel 
494275167d2cSStephen M. Cameron static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
49439b5c48c2SStephen Cameron 	struct CommandList *abort, int reply_queue)
494475167d2cSStephen M. Cameron {
494575167d2cSStephen M. Cameron 	int rc = IO_OK;
494675167d2cSStephen M. Cameron 	struct CommandList *c;
494775167d2cSStephen M. Cameron 	struct ErrorInfo *ei;
49482b08b3e9SDon Brace 	__le32 tagupper, taglower;
494975167d2cSStephen M. Cameron 
495045fcb86eSStephen Cameron 	c = cmd_alloc(h);
495175167d2cSStephen M. Cameron 
4952a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no buffer to map */
49539b5c48c2SStephen Cameron 	(void) fill_cmd(c, HPSA_ABORT_MSG, h, &abort->Header.tag,
4954a2dac136SStephen M. Cameron 		0, 0, scsi3addr, TYPE_MSG);
49559b5c48c2SStephen Cameron 	if (h->needs_abort_tags_swizzled)
49566cba3f19SStephen M. Cameron 		swizzle_abort_tag(&c->Request.CDB[4]);
495725163bd5SWebb Scales 	(void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
495817eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
495925163bd5SWebb Scales 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd(abort) completed.\n",
496017eb87d2SScott Teel 		__func__, tagupper, taglower);
496175167d2cSStephen M. Cameron 	/* no unmap needed here because no data xfer. */
496275167d2cSStephen M. Cameron 
496375167d2cSStephen M. Cameron 	ei = c->err_info;
496475167d2cSStephen M. Cameron 	switch (ei->CommandStatus) {
496575167d2cSStephen M. Cameron 	case CMD_SUCCESS:
496675167d2cSStephen M. Cameron 		break;
49679437ac43SStephen Cameron 	case CMD_TMF_STATUS:
49689437ac43SStephen Cameron 		rc = hpsa_evaluate_tmf_status(h, c);
49699437ac43SStephen Cameron 		break;
497075167d2cSStephen M. Cameron 	case CMD_UNABORTABLE: /* Very common, don't make noise. */
497175167d2cSStephen M. Cameron 		rc = -1;
497275167d2cSStephen M. Cameron 		break;
497375167d2cSStephen M. Cameron 	default:
497475167d2cSStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
497517eb87d2SScott Teel 			__func__, tagupper, taglower);
4976d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
497775167d2cSStephen M. Cameron 		rc = -1;
497875167d2cSStephen M. Cameron 		break;
497975167d2cSStephen M. Cameron 	}
498045fcb86eSStephen Cameron 	cmd_free(h, c);
4981dd0e19f3SScott Teel 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n",
4982dd0e19f3SScott Teel 		__func__, tagupper, taglower);
498375167d2cSStephen M. Cameron 	return rc;
498475167d2cSStephen M. Cameron }
498575167d2cSStephen M. Cameron 
49868be986ccSStephen Cameron static void setup_ioaccel2_abort_cmd(struct CommandList *c, struct ctlr_info *h,
49878be986ccSStephen Cameron 	struct CommandList *command_to_abort, int reply_queue)
49888be986ccSStephen Cameron {
49898be986ccSStephen Cameron 	struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
49908be986ccSStephen Cameron 	struct hpsa_tmf_struct *ac = (struct hpsa_tmf_struct *) c2;
49918be986ccSStephen Cameron 	struct io_accel2_cmd *c2a =
49928be986ccSStephen Cameron 		&h->ioaccel2_cmd_pool[command_to_abort->cmdindex];
4993*a58e7e53SWebb Scales 	struct scsi_cmnd *scmd = command_to_abort->scsi_cmd;
49948be986ccSStephen Cameron 	struct hpsa_scsi_dev_t *dev = scmd->device->hostdata;
49958be986ccSStephen Cameron 
49968be986ccSStephen Cameron 	/*
49978be986ccSStephen Cameron 	 * We're overlaying struct hpsa_tmf_struct on top of something which
49988be986ccSStephen Cameron 	 * was allocated as a struct io_accel2_cmd, so we better be sure it
49998be986ccSStephen Cameron 	 * actually fits, and doesn't overrun the error info space.
50008be986ccSStephen Cameron 	 */
50018be986ccSStephen Cameron 	BUILD_BUG_ON(sizeof(struct hpsa_tmf_struct) >
50028be986ccSStephen Cameron 			sizeof(struct io_accel2_cmd));
50038be986ccSStephen Cameron 	BUG_ON(offsetof(struct io_accel2_cmd, error_data) <
50048be986ccSStephen Cameron 			offsetof(struct hpsa_tmf_struct, error_len) +
50058be986ccSStephen Cameron 				sizeof(ac->error_len));
50068be986ccSStephen Cameron 
50078be986ccSStephen Cameron 	c->cmd_type = IOACCEL2_TMF;
5008*a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_BUSY;
5009*a58e7e53SWebb Scales 
50108be986ccSStephen Cameron 	/* Adjust the DMA address to point to the accelerated command buffer */
50118be986ccSStephen Cameron 	c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
50128be986ccSStephen Cameron 				(c->cmdindex * sizeof(struct io_accel2_cmd));
50138be986ccSStephen Cameron 	BUG_ON(c->busaddr & 0x0000007F);
50148be986ccSStephen Cameron 
50158be986ccSStephen Cameron 	memset(ac, 0, sizeof(*c2)); /* yes this is correct */
50168be986ccSStephen Cameron 	ac->iu_type = IOACCEL2_IU_TMF_TYPE;
50178be986ccSStephen Cameron 	ac->reply_queue = reply_queue;
50188be986ccSStephen Cameron 	ac->tmf = IOACCEL2_TMF_ABORT;
50198be986ccSStephen Cameron 	ac->it_nexus = cpu_to_le32(dev->ioaccel_handle);
50208be986ccSStephen Cameron 	memset(ac->lun_id, 0, sizeof(ac->lun_id));
50218be986ccSStephen Cameron 	ac->tag = cpu_to_le64(c->cmdindex << DIRECT_LOOKUP_SHIFT);
50228be986ccSStephen Cameron 	ac->abort_tag = cpu_to_le64(le32_to_cpu(c2a->Tag));
50238be986ccSStephen Cameron 	ac->error_ptr = cpu_to_le64(c->busaddr +
50248be986ccSStephen Cameron 			offsetof(struct io_accel2_cmd, error_data));
50258be986ccSStephen Cameron 	ac->error_len = cpu_to_le32(sizeof(c2->error_data));
50268be986ccSStephen Cameron }
50278be986ccSStephen Cameron 
502854b6e9e9SScott Teel /* ioaccel2 path firmware cannot handle abort task requests.
502954b6e9e9SScott Teel  * Change abort requests to physical target reset, and send to the
503054b6e9e9SScott Teel  * address of the physical disk used for the ioaccel 2 command.
503154b6e9e9SScott Teel  * Return 0 on success (IO_OK)
503254b6e9e9SScott Teel  *	 -1 on failure
503354b6e9e9SScott Teel  */
503454b6e9e9SScott Teel 
503554b6e9e9SScott Teel static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
503625163bd5SWebb Scales 	unsigned char *scsi3addr, struct CommandList *abort, int reply_queue)
503754b6e9e9SScott Teel {
503854b6e9e9SScott Teel 	int rc = IO_OK;
503954b6e9e9SScott Teel 	struct scsi_cmnd *scmd; /* scsi command within request being aborted */
504054b6e9e9SScott Teel 	struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
504154b6e9e9SScott Teel 	unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
504254b6e9e9SScott Teel 	unsigned char *psa = &phys_scsi3addr[0];
504354b6e9e9SScott Teel 
504454b6e9e9SScott Teel 	/* Get a pointer to the hpsa logical device. */
50457fa3030cSStephen Cameron 	scmd = abort->scsi_cmd;
504654b6e9e9SScott Teel 	dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
504754b6e9e9SScott Teel 	if (dev == NULL) {
504854b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
504954b6e9e9SScott Teel 			"Cannot abort: no device pointer for command.\n");
505054b6e9e9SScott Teel 			return -1; /* not abortable */
505154b6e9e9SScott Teel 	}
505254b6e9e9SScott Teel 
50532ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
50542ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
50550d96ef5fSWebb Scales 			"scsi %d:%d:%d:%d %s scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
50562ba8bfc8SStephen M. Cameron 			h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
50570d96ef5fSWebb Scales 			"Reset as abort",
50582ba8bfc8SStephen M. Cameron 			scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
50592ba8bfc8SStephen M. Cameron 			scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]);
50602ba8bfc8SStephen M. Cameron 
506154b6e9e9SScott Teel 	if (!dev->offload_enabled) {
506254b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
506354b6e9e9SScott Teel 			"Can't abort: device is not operating in HP SSD Smart Path mode.\n");
506454b6e9e9SScott Teel 		return -1; /* not abortable */
506554b6e9e9SScott Teel 	}
506654b6e9e9SScott Teel 
506754b6e9e9SScott Teel 	/* Incoming scsi3addr is logical addr. We need physical disk addr. */
506854b6e9e9SScott Teel 	if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
506954b6e9e9SScott Teel 		dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
507054b6e9e9SScott Teel 		return -1; /* not abortable */
507154b6e9e9SScott Teel 	}
507254b6e9e9SScott Teel 
507354b6e9e9SScott Teel 	/* send the reset */
50742ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
50752ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
50762ba8bfc8SStephen M. Cameron 			"Reset as abort: Resetting physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
50772ba8bfc8SStephen M. Cameron 			psa[0], psa[1], psa[2], psa[3],
50782ba8bfc8SStephen M. Cameron 			psa[4], psa[5], psa[6], psa[7]);
507925163bd5SWebb Scales 	rc = hpsa_send_reset(h, psa, HPSA_RESET_TYPE_TARGET, reply_queue);
508054b6e9e9SScott Teel 	if (rc != 0) {
508154b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
508254b6e9e9SScott Teel 			"Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
508354b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
508454b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
508554b6e9e9SScott Teel 		return rc; /* failed to reset */
508654b6e9e9SScott Teel 	}
508754b6e9e9SScott Teel 
508854b6e9e9SScott Teel 	/* wait for device to recover */
508954b6e9e9SScott Teel 	if (wait_for_device_to_become_ready(h, psa) != 0) {
509054b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
509154b6e9e9SScott Teel 			"Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
509254b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
509354b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
509454b6e9e9SScott Teel 		return -1;  /* failed to recover */
509554b6e9e9SScott Teel 	}
509654b6e9e9SScott Teel 
509754b6e9e9SScott Teel 	/* device recovered */
509854b6e9e9SScott Teel 	dev_info(&h->pdev->dev,
509954b6e9e9SScott Teel 		"Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
510054b6e9e9SScott Teel 		psa[0], psa[1], psa[2], psa[3],
510154b6e9e9SScott Teel 		psa[4], psa[5], psa[6], psa[7]);
510254b6e9e9SScott Teel 
510354b6e9e9SScott Teel 	return rc; /* success */
510454b6e9e9SScott Teel }
510554b6e9e9SScott Teel 
51068be986ccSStephen Cameron static int hpsa_send_abort_ioaccel2(struct ctlr_info *h,
51078be986ccSStephen Cameron 	struct CommandList *abort, int reply_queue)
51088be986ccSStephen Cameron {
51098be986ccSStephen Cameron 	int rc = IO_OK;
51108be986ccSStephen Cameron 	struct CommandList *c;
51118be986ccSStephen Cameron 	__le32 taglower, tagupper;
51128be986ccSStephen Cameron 	struct hpsa_scsi_dev_t *dev;
51138be986ccSStephen Cameron 	struct io_accel2_cmd *c2;
51148be986ccSStephen Cameron 
51158be986ccSStephen Cameron 	dev = abort->scsi_cmd->device->hostdata;
51168be986ccSStephen Cameron 	if (!dev->offload_enabled && !dev->hba_ioaccel_enabled)
51178be986ccSStephen Cameron 		return -1;
51188be986ccSStephen Cameron 
51198be986ccSStephen Cameron 	c = cmd_alloc(h);
51208be986ccSStephen Cameron 	setup_ioaccel2_abort_cmd(c, h, abort, reply_queue);
51218be986ccSStephen Cameron 	c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
51228be986ccSStephen Cameron 	(void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
51238be986ccSStephen Cameron 	hpsa_get_tag(h, abort, &taglower, &tagupper);
51248be986ccSStephen Cameron 	dev_dbg(&h->pdev->dev,
51258be986ccSStephen Cameron 		"%s: Tag:0x%08x:%08x: do_simple_cmd(ioaccel2 abort) completed.\n",
51268be986ccSStephen Cameron 		__func__, tagupper, taglower);
51278be986ccSStephen Cameron 	/* no unmap needed here because no data xfer. */
51288be986ccSStephen Cameron 
51298be986ccSStephen Cameron 	dev_dbg(&h->pdev->dev,
51308be986ccSStephen Cameron 		"%s: Tag:0x%08x:%08x: abort service response = 0x%02x.\n",
51318be986ccSStephen Cameron 		__func__, tagupper, taglower, c2->error_data.serv_response);
51328be986ccSStephen Cameron 	switch (c2->error_data.serv_response) {
51338be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
51348be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
51358be986ccSStephen Cameron 		rc = 0;
51368be986ccSStephen Cameron 		break;
51378be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
51388be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_FAILURE:
51398be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
51408be986ccSStephen Cameron 		rc = -1;
51418be986ccSStephen Cameron 		break;
51428be986ccSStephen Cameron 	default:
51438be986ccSStephen Cameron 		dev_warn(&h->pdev->dev,
51448be986ccSStephen Cameron 			"%s: Tag:0x%08x:%08x: unknown abort service response 0x%02x\n",
51458be986ccSStephen Cameron 			__func__, tagupper, taglower,
51468be986ccSStephen Cameron 			c2->error_data.serv_response);
51478be986ccSStephen Cameron 		rc = -1;
51488be986ccSStephen Cameron 	}
51498be986ccSStephen Cameron 	cmd_free(h, c);
51508be986ccSStephen Cameron 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n", __func__,
51518be986ccSStephen Cameron 		tagupper, taglower);
51528be986ccSStephen Cameron 	return rc;
51538be986ccSStephen Cameron }
51548be986ccSStephen Cameron 
51556cba3f19SStephen M. Cameron static int hpsa_send_abort_both_ways(struct ctlr_info *h,
515625163bd5SWebb Scales 	unsigned char *scsi3addr, struct CommandList *abort, int reply_queue)
51576cba3f19SStephen M. Cameron {
51588be986ccSStephen Cameron 	/*
51598be986ccSStephen Cameron 	 * ioccelerator mode 2 commands should be aborted via the
516054b6e9e9SScott Teel 	 * accelerated path, since RAID path is unaware of these commands,
51618be986ccSStephen Cameron 	 * but not all underlying firmware can handle abort TMF.
51628be986ccSStephen Cameron 	 * Change abort to physical device reset when abort TMF is unsupported.
516354b6e9e9SScott Teel 	 */
51648be986ccSStephen Cameron 	if (abort->cmd_type == CMD_IOACCEL2) {
51658be986ccSStephen Cameron 		if (HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags)
51668be986ccSStephen Cameron 			return hpsa_send_abort_ioaccel2(h, abort,
51678be986ccSStephen Cameron 						reply_queue);
51688be986ccSStephen Cameron 		else
516925163bd5SWebb Scales 			return hpsa_send_reset_as_abort_ioaccel2(h, scsi3addr,
517025163bd5SWebb Scales 							abort, reply_queue);
51718be986ccSStephen Cameron 	}
51729b5c48c2SStephen Cameron 	return hpsa_send_abort(h, scsi3addr, abort, reply_queue);
517325163bd5SWebb Scales }
517425163bd5SWebb Scales 
517525163bd5SWebb Scales /* Find out which reply queue a command was meant to return on */
517625163bd5SWebb Scales static int hpsa_extract_reply_queue(struct ctlr_info *h,
517725163bd5SWebb Scales 					struct CommandList *c)
517825163bd5SWebb Scales {
517925163bd5SWebb Scales 	if (c->cmd_type == CMD_IOACCEL2)
518025163bd5SWebb Scales 		return h->ioaccel2_cmd_pool[c->cmdindex].reply_queue;
518125163bd5SWebb Scales 	return c->Header.ReplyQueue;
51826cba3f19SStephen M. Cameron }
51836cba3f19SStephen M. Cameron 
51849b5c48c2SStephen Cameron /*
51859b5c48c2SStephen Cameron  * Limit concurrency of abort commands to prevent
51869b5c48c2SStephen Cameron  * over-subscription of commands
51879b5c48c2SStephen Cameron  */
51889b5c48c2SStephen Cameron static inline int wait_for_available_abort_cmd(struct ctlr_info *h)
51899b5c48c2SStephen Cameron {
51909b5c48c2SStephen Cameron #define ABORT_CMD_WAIT_MSECS 5000
51919b5c48c2SStephen Cameron 	return !wait_event_timeout(h->abort_cmd_wait_queue,
51929b5c48c2SStephen Cameron 			atomic_dec_if_positive(&h->abort_cmds_available) >= 0,
51939b5c48c2SStephen Cameron 			msecs_to_jiffies(ABORT_CMD_WAIT_MSECS));
51949b5c48c2SStephen Cameron }
51959b5c48c2SStephen Cameron 
519675167d2cSStephen M. Cameron /* Send an abort for the specified command.
519775167d2cSStephen M. Cameron  *	If the device and controller support it,
519875167d2cSStephen M. Cameron  *		send a task abort request.
519975167d2cSStephen M. Cameron  */
520075167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
520175167d2cSStephen M. Cameron {
520275167d2cSStephen M. Cameron 
5203*a58e7e53SWebb Scales 	int rc;
520475167d2cSStephen M. Cameron 	struct ctlr_info *h;
520575167d2cSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
520675167d2cSStephen M. Cameron 	struct CommandList *abort; /* pointer to command to be aborted */
520775167d2cSStephen M. Cameron 	struct scsi_cmnd *as;	/* ptr to scsi cmd inside aborted command. */
520875167d2cSStephen M. Cameron 	char msg[256];		/* For debug messaging. */
520975167d2cSStephen M. Cameron 	int ml = 0;
52102b08b3e9SDon Brace 	__le32 tagupper, taglower;
521125163bd5SWebb Scales 	int refcount, reply_queue;
521225163bd5SWebb Scales 
521325163bd5SWebb Scales 	if (sc == NULL)
521425163bd5SWebb Scales 		return FAILED;
521575167d2cSStephen M. Cameron 
52169b5c48c2SStephen Cameron 	if (sc->device == NULL)
52179b5c48c2SStephen Cameron 		return FAILED;
52189b5c48c2SStephen Cameron 
521975167d2cSStephen M. Cameron 	/* Find the controller of the command to be aborted */
522075167d2cSStephen M. Cameron 	h = sdev_to_hba(sc->device);
52219b5c48c2SStephen Cameron 	if (h == NULL)
522275167d2cSStephen M. Cameron 		return FAILED;
522375167d2cSStephen M. Cameron 
522425163bd5SWebb Scales 	/* Find the device of the command to be aborted */
522525163bd5SWebb Scales 	dev = sc->device->hostdata;
522625163bd5SWebb Scales 	if (!dev) {
522725163bd5SWebb Scales 		dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
522825163bd5SWebb Scales 				msg);
5229e345893bSDon Brace 		return FAILED;
523025163bd5SWebb Scales 	}
523125163bd5SWebb Scales 
523225163bd5SWebb Scales 	/* If controller locked up, we can guarantee command won't complete */
523325163bd5SWebb Scales 	if (lockup_detected(h)) {
523425163bd5SWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev,
523525163bd5SWebb Scales 					"ABORT FAILED, lockup detected");
523625163bd5SWebb Scales 		return FAILED;
523725163bd5SWebb Scales 	}
523825163bd5SWebb Scales 
523925163bd5SWebb Scales 	/* This is a good time to check if controller lockup has occurred */
524025163bd5SWebb Scales 	if (detect_controller_lockup(h)) {
524125163bd5SWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev,
524225163bd5SWebb Scales 					"ABORT FAILED, new lockup detected");
524325163bd5SWebb Scales 		return FAILED;
524425163bd5SWebb Scales 	}
5245e345893bSDon Brace 
524675167d2cSStephen M. Cameron 	/* Check that controller supports some kind of task abort */
524775167d2cSStephen M. Cameron 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
524875167d2cSStephen M. Cameron 		!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
524975167d2cSStephen M. Cameron 		return FAILED;
525075167d2cSStephen M. Cameron 
525175167d2cSStephen M. Cameron 	memset(msg, 0, sizeof(msg));
52524b761557SRobert Elliott 	ml += sprintf(msg+ml, "scsi %d:%d:%d:%llu %s %p",
525375167d2cSStephen M. Cameron 		h->scsi_host->host_no, sc->device->channel,
52540d96ef5fSWebb Scales 		sc->device->id, sc->device->lun,
52554b761557SRobert Elliott 		"Aborting command", sc);
525675167d2cSStephen M. Cameron 
525775167d2cSStephen M. Cameron 	/* Get SCSI command to be aborted */
525875167d2cSStephen M. Cameron 	abort = (struct CommandList *) sc->host_scribble;
525975167d2cSStephen M. Cameron 	if (abort == NULL) {
5260281a7fd0SWebb Scales 		/* This can happen if the command already completed. */
5261281a7fd0SWebb Scales 		return SUCCESS;
5262281a7fd0SWebb Scales 	}
5263281a7fd0SWebb Scales 	refcount = atomic_inc_return(&abort->refcount);
5264281a7fd0SWebb Scales 	if (refcount == 1) { /* Command is done already. */
5265281a7fd0SWebb Scales 		cmd_free(h, abort);
5266281a7fd0SWebb Scales 		return SUCCESS;
526775167d2cSStephen M. Cameron 	}
52689b5c48c2SStephen Cameron 
52699b5c48c2SStephen Cameron 	/* Don't bother trying the abort if we know it won't work. */
52709b5c48c2SStephen Cameron 	if (abort->cmd_type != CMD_IOACCEL2 &&
52719b5c48c2SStephen Cameron 		abort->cmd_type != CMD_IOACCEL1 && !dev->supports_aborts) {
52729b5c48c2SStephen Cameron 		cmd_free(h, abort);
52739b5c48c2SStephen Cameron 		return FAILED;
52749b5c48c2SStephen Cameron 	}
52759b5c48c2SStephen Cameron 
5276*a58e7e53SWebb Scales 	/*
5277*a58e7e53SWebb Scales 	 * Check that we're aborting the right command.
5278*a58e7e53SWebb Scales 	 * It's possible the CommandList already completed and got re-used.
5279*a58e7e53SWebb Scales 	 */
5280*a58e7e53SWebb Scales 	if (abort->scsi_cmd != sc) {
5281*a58e7e53SWebb Scales 		cmd_free(h, abort);
5282*a58e7e53SWebb Scales 		return SUCCESS;
5283*a58e7e53SWebb Scales 	}
5284*a58e7e53SWebb Scales 
5285*a58e7e53SWebb Scales 	abort->abort_pending = true;
528617eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
528725163bd5SWebb Scales 	reply_queue = hpsa_extract_reply_queue(h, abort);
528817eb87d2SScott Teel 	ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
52897fa3030cSStephen Cameron 	as  = abort->scsi_cmd;
529075167d2cSStephen M. Cameron 	if (as != NULL)
52914b761557SRobert Elliott 		ml += sprintf(msg+ml,
52924b761557SRobert Elliott 			"CDBLen: %d CDB: 0x%02x%02x... SN: 0x%lx ",
52934b761557SRobert Elliott 			as->cmd_len, as->cmnd[0], as->cmnd[1],
52944b761557SRobert Elliott 			as->serial_number);
52954b761557SRobert Elliott 	dev_warn(&h->pdev->dev, "%s BEING SENT\n", msg);
52960d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_WARNING, h, dev, "Aborting command");
52974b761557SRobert Elliott 
529875167d2cSStephen M. Cameron 	/*
529975167d2cSStephen M. Cameron 	 * Command is in flight, or possibly already completed
530075167d2cSStephen M. Cameron 	 * by the firmware (but not to the scsi mid layer) but we can't
530175167d2cSStephen M. Cameron 	 * distinguish which.  Send the abort down.
530275167d2cSStephen M. Cameron 	 */
53039b5c48c2SStephen Cameron 	if (wait_for_available_abort_cmd(h)) {
53049b5c48c2SStephen Cameron 		dev_warn(&h->pdev->dev,
53054b761557SRobert Elliott 			"%s FAILED, timeout waiting for an abort command to become available.\n",
53064b761557SRobert Elliott 			msg);
53079b5c48c2SStephen Cameron 		cmd_free(h, abort);
53089b5c48c2SStephen Cameron 		return FAILED;
53099b5c48c2SStephen Cameron 	}
531025163bd5SWebb Scales 	rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort, reply_queue);
53119b5c48c2SStephen Cameron 	atomic_inc(&h->abort_cmds_available);
53129b5c48c2SStephen Cameron 	wake_up_all(&h->abort_cmd_wait_queue);
531375167d2cSStephen M. Cameron 	if (rc != 0) {
53144b761557SRobert Elliott 		dev_warn(&h->pdev->dev, "%s SENT, FAILED\n", msg);
53150d96ef5fSWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev,
53160d96ef5fSWebb Scales 				"FAILED to abort command");
5317281a7fd0SWebb Scales 		cmd_free(h, abort);
531875167d2cSStephen M. Cameron 		return FAILED;
531975167d2cSStephen M. Cameron 	}
53204b761557SRobert Elliott 	dev_info(&h->pdev->dev, "%s SENT, SUCCESS\n", msg);
5321*a58e7e53SWebb Scales 	wait_event(h->abort_sync_wait_queue,
5322*a58e7e53SWebb Scales 		   abort->scsi_cmd != sc || lockup_detected(h));
5323281a7fd0SWebb Scales 	cmd_free(h, abort);
5324*a58e7e53SWebb Scales 	return !lockup_detected(h) ? SUCCESS : FAILED;
532575167d2cSStephen M. Cameron }
532675167d2cSStephen M. Cameron 
5327edd16368SStephen M. Cameron /*
5328edd16368SStephen M. Cameron  * For operations that cannot sleep, a command block is allocated at init,
5329edd16368SStephen M. Cameron  * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
5330edd16368SStephen M. Cameron  * which ones are free or in use.  Lock must be held when calling this.
5331edd16368SStephen M. Cameron  * cmd_free() is the complement.
5332bf43caf3SRobert Elliott  * This function never gives up and returns NULL.  If it hangs,
5333bf43caf3SRobert Elliott  * another thread must call cmd_free() to free some tags.
5334edd16368SStephen M. Cameron  */
5335281a7fd0SWebb Scales 
5336edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h)
5337edd16368SStephen M. Cameron {
5338edd16368SStephen M. Cameron 	struct CommandList *c;
5339360c73bdSStephen Cameron 	int refcount, i;
534033811026SRobert Elliott 	unsigned long offset;
5341edd16368SStephen M. Cameron 
534233811026SRobert Elliott 	/*
534333811026SRobert Elliott 	 * There is some *extremely* small but non-zero chance that that
53444c413128SStephen M. Cameron 	 * multiple threads could get in here, and one thread could
53454c413128SStephen M. Cameron 	 * be scanning through the list of bits looking for a free
53464c413128SStephen M. Cameron 	 * one, but the free ones are always behind him, and other
53474c413128SStephen M. Cameron 	 * threads sneak in behind him and eat them before he can
53484c413128SStephen M. Cameron 	 * get to them, so that while there is always a free one, a
53494c413128SStephen M. Cameron 	 * very unlucky thread might be starved anyway, never able to
53504c413128SStephen M. Cameron 	 * beat the other threads.  In reality, this happens so
53514c413128SStephen M. Cameron 	 * infrequently as to be indistinguishable from never.
53524c413128SStephen M. Cameron 	 */
53534c413128SStephen M. Cameron 
535433811026SRobert Elliott 	offset = h->last_allocation; /* benignly racy */
5355281a7fd0SWebb Scales 	for (;;) {
5356281a7fd0SWebb Scales 		i = find_next_zero_bit(h->cmd_pool_bits, h->nr_cmds, offset);
5357281a7fd0SWebb Scales 		if (unlikely(i == h->nr_cmds)) {
5358281a7fd0SWebb Scales 			offset = 0;
5359281a7fd0SWebb Scales 			continue;
5360281a7fd0SWebb Scales 		}
5361edd16368SStephen M. Cameron 		c = h->cmd_pool + i;
5362281a7fd0SWebb Scales 		refcount = atomic_inc_return(&c->refcount);
5363281a7fd0SWebb Scales 		if (unlikely(refcount > 1)) {
5364281a7fd0SWebb Scales 			cmd_free(h, c); /* already in use */
5365281a7fd0SWebb Scales 			offset = (i + 1) % h->nr_cmds;
5366281a7fd0SWebb Scales 			continue;
5367281a7fd0SWebb Scales 		}
5368281a7fd0SWebb Scales 		set_bit(i & (BITS_PER_LONG - 1),
5369281a7fd0SWebb Scales 			h->cmd_pool_bits + (i / BITS_PER_LONG));
5370281a7fd0SWebb Scales 		break; /* it's ours now. */
5371281a7fd0SWebb Scales 	}
537233811026SRobert Elliott 	h->last_allocation = i; /* benignly racy */
5373360c73bdSStephen Cameron 	hpsa_cmd_partial_init(h, i, c);
5374edd16368SStephen M. Cameron 	return c;
5375edd16368SStephen M. Cameron }
5376edd16368SStephen M. Cameron 
5377edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c)
5378edd16368SStephen M. Cameron {
5379281a7fd0SWebb Scales 	if (atomic_dec_and_test(&c->refcount)) {
5380edd16368SStephen M. Cameron 		int i;
5381edd16368SStephen M. Cameron 
5382edd16368SStephen M. Cameron 		i = c - h->cmd_pool;
5383edd16368SStephen M. Cameron 		clear_bit(i & (BITS_PER_LONG - 1),
5384edd16368SStephen M. Cameron 			  h->cmd_pool_bits + (i / BITS_PER_LONG));
5385edd16368SStephen M. Cameron 	}
5386281a7fd0SWebb Scales }
5387edd16368SStephen M. Cameron 
5388edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
5389edd16368SStephen M. Cameron 
539042a91641SDon Brace static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd,
539142a91641SDon Brace 	void __user *arg)
5392edd16368SStephen M. Cameron {
5393edd16368SStephen M. Cameron 	IOCTL32_Command_struct __user *arg32 =
5394edd16368SStephen M. Cameron 	    (IOCTL32_Command_struct __user *) arg;
5395edd16368SStephen M. Cameron 	IOCTL_Command_struct arg64;
5396edd16368SStephen M. Cameron 	IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
5397edd16368SStephen M. Cameron 	int err;
5398edd16368SStephen M. Cameron 	u32 cp;
5399edd16368SStephen M. Cameron 
5400938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
5401edd16368SStephen M. Cameron 	err = 0;
5402edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
5403edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
5404edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
5405edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
5406edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
5407edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
5408edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
5409edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
5410edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
5411edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
5412edd16368SStephen M. Cameron 
5413edd16368SStephen M. Cameron 	if (err)
5414edd16368SStephen M. Cameron 		return -EFAULT;
5415edd16368SStephen M. Cameron 
541642a91641SDon Brace 	err = hpsa_ioctl(dev, CCISS_PASSTHRU, p);
5417edd16368SStephen M. Cameron 	if (err)
5418edd16368SStephen M. Cameron 		return err;
5419edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
5420edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
5421edd16368SStephen M. Cameron 	if (err)
5422edd16368SStephen M. Cameron 		return -EFAULT;
5423edd16368SStephen M. Cameron 	return err;
5424edd16368SStephen M. Cameron }
5425edd16368SStephen M. Cameron 
5426edd16368SStephen M. Cameron static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
542742a91641SDon Brace 	int cmd, void __user *arg)
5428edd16368SStephen M. Cameron {
5429edd16368SStephen M. Cameron 	BIG_IOCTL32_Command_struct __user *arg32 =
5430edd16368SStephen M. Cameron 	    (BIG_IOCTL32_Command_struct __user *) arg;
5431edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct arg64;
5432edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct __user *p =
5433edd16368SStephen M. Cameron 	    compat_alloc_user_space(sizeof(arg64));
5434edd16368SStephen M. Cameron 	int err;
5435edd16368SStephen M. Cameron 	u32 cp;
5436edd16368SStephen M. Cameron 
5437938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
5438edd16368SStephen M. Cameron 	err = 0;
5439edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
5440edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
5441edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
5442edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
5443edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
5444edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
5445edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
5446edd16368SStephen M. Cameron 	err |= get_user(arg64.malloc_size, &arg32->malloc_size);
5447edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
5448edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
5449edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
5450edd16368SStephen M. Cameron 
5451edd16368SStephen M. Cameron 	if (err)
5452edd16368SStephen M. Cameron 		return -EFAULT;
5453edd16368SStephen M. Cameron 
545442a91641SDon Brace 	err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, p);
5455edd16368SStephen M. Cameron 	if (err)
5456edd16368SStephen M. Cameron 		return err;
5457edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
5458edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
5459edd16368SStephen M. Cameron 	if (err)
5460edd16368SStephen M. Cameron 		return -EFAULT;
5461edd16368SStephen M. Cameron 	return err;
5462edd16368SStephen M. Cameron }
546371fe75a7SStephen M. Cameron 
546442a91641SDon Brace static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
546571fe75a7SStephen M. Cameron {
546671fe75a7SStephen M. Cameron 	switch (cmd) {
546771fe75a7SStephen M. Cameron 	case CCISS_GETPCIINFO:
546871fe75a7SStephen M. Cameron 	case CCISS_GETINTINFO:
546971fe75a7SStephen M. Cameron 	case CCISS_SETINTINFO:
547071fe75a7SStephen M. Cameron 	case CCISS_GETNODENAME:
547171fe75a7SStephen M. Cameron 	case CCISS_SETNODENAME:
547271fe75a7SStephen M. Cameron 	case CCISS_GETHEARTBEAT:
547371fe75a7SStephen M. Cameron 	case CCISS_GETBUSTYPES:
547471fe75a7SStephen M. Cameron 	case CCISS_GETFIRMVER:
547571fe75a7SStephen M. Cameron 	case CCISS_GETDRIVVER:
547671fe75a7SStephen M. Cameron 	case CCISS_REVALIDVOLS:
547771fe75a7SStephen M. Cameron 	case CCISS_DEREGDISK:
547871fe75a7SStephen M. Cameron 	case CCISS_REGNEWDISK:
547971fe75a7SStephen M. Cameron 	case CCISS_REGNEWD:
548071fe75a7SStephen M. Cameron 	case CCISS_RESCANDISK:
548171fe75a7SStephen M. Cameron 	case CCISS_GETLUNINFO:
548271fe75a7SStephen M. Cameron 		return hpsa_ioctl(dev, cmd, arg);
548371fe75a7SStephen M. Cameron 
548471fe75a7SStephen M. Cameron 	case CCISS_PASSTHRU32:
548571fe75a7SStephen M. Cameron 		return hpsa_ioctl32_passthru(dev, cmd, arg);
548671fe75a7SStephen M. Cameron 	case CCISS_BIG_PASSTHRU32:
548771fe75a7SStephen M. Cameron 		return hpsa_ioctl32_big_passthru(dev, cmd, arg);
548871fe75a7SStephen M. Cameron 
548971fe75a7SStephen M. Cameron 	default:
549071fe75a7SStephen M. Cameron 		return -ENOIOCTLCMD;
549171fe75a7SStephen M. Cameron 	}
549271fe75a7SStephen M. Cameron }
5493edd16368SStephen M. Cameron #endif
5494edd16368SStephen M. Cameron 
5495edd16368SStephen M. Cameron static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
5496edd16368SStephen M. Cameron {
5497edd16368SStephen M. Cameron 	struct hpsa_pci_info pciinfo;
5498edd16368SStephen M. Cameron 
5499edd16368SStephen M. Cameron 	if (!argp)
5500edd16368SStephen M. Cameron 		return -EINVAL;
5501edd16368SStephen M. Cameron 	pciinfo.domain = pci_domain_nr(h->pdev->bus);
5502edd16368SStephen M. Cameron 	pciinfo.bus = h->pdev->bus->number;
5503edd16368SStephen M. Cameron 	pciinfo.dev_fn = h->pdev->devfn;
5504edd16368SStephen M. Cameron 	pciinfo.board_id = h->board_id;
5505edd16368SStephen M. Cameron 	if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
5506edd16368SStephen M. Cameron 		return -EFAULT;
5507edd16368SStephen M. Cameron 	return 0;
5508edd16368SStephen M. Cameron }
5509edd16368SStephen M. Cameron 
5510edd16368SStephen M. Cameron static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
5511edd16368SStephen M. Cameron {
5512edd16368SStephen M. Cameron 	DriverVer_type DriverVer;
5513edd16368SStephen M. Cameron 	unsigned char vmaj, vmin, vsubmin;
5514edd16368SStephen M. Cameron 	int rc;
5515edd16368SStephen M. Cameron 
5516edd16368SStephen M. Cameron 	rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
5517edd16368SStephen M. Cameron 		&vmaj, &vmin, &vsubmin);
5518edd16368SStephen M. Cameron 	if (rc != 3) {
5519edd16368SStephen M. Cameron 		dev_info(&h->pdev->dev, "driver version string '%s' "
5520edd16368SStephen M. Cameron 			"unrecognized.", HPSA_DRIVER_VERSION);
5521edd16368SStephen M. Cameron 		vmaj = 0;
5522edd16368SStephen M. Cameron 		vmin = 0;
5523edd16368SStephen M. Cameron 		vsubmin = 0;
5524edd16368SStephen M. Cameron 	}
5525edd16368SStephen M. Cameron 	DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
5526edd16368SStephen M. Cameron 	if (!argp)
5527edd16368SStephen M. Cameron 		return -EINVAL;
5528edd16368SStephen M. Cameron 	if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
5529edd16368SStephen M. Cameron 		return -EFAULT;
5530edd16368SStephen M. Cameron 	return 0;
5531edd16368SStephen M. Cameron }
5532edd16368SStephen M. Cameron 
5533edd16368SStephen M. Cameron static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
5534edd16368SStephen M. Cameron {
5535edd16368SStephen M. Cameron 	IOCTL_Command_struct iocommand;
5536edd16368SStephen M. Cameron 	struct CommandList *c;
5537edd16368SStephen M. Cameron 	char *buff = NULL;
553850a0decfSStephen M. Cameron 	u64 temp64;
5539c1f63c8fSStephen M. Cameron 	int rc = 0;
5540edd16368SStephen M. Cameron 
5541edd16368SStephen M. Cameron 	if (!argp)
5542edd16368SStephen M. Cameron 		return -EINVAL;
5543edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
5544edd16368SStephen M. Cameron 		return -EPERM;
5545edd16368SStephen M. Cameron 	if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
5546edd16368SStephen M. Cameron 		return -EFAULT;
5547edd16368SStephen M. Cameron 	if ((iocommand.buf_size < 1) &&
5548edd16368SStephen M. Cameron 	    (iocommand.Request.Type.Direction != XFER_NONE)) {
5549edd16368SStephen M. Cameron 		return -EINVAL;
5550edd16368SStephen M. Cameron 	}
5551edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
5552edd16368SStephen M. Cameron 		buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
5553edd16368SStephen M. Cameron 		if (buff == NULL)
55542dd02d74SRobert Elliott 			return -ENOMEM;
55559233fb10SStephen M. Cameron 		if (iocommand.Request.Type.Direction & XFER_WRITE) {
5556edd16368SStephen M. Cameron 			/* Copy the data into the buffer we created */
5557b03a7771SStephen M. Cameron 			if (copy_from_user(buff, iocommand.buf,
5558b03a7771SStephen M. Cameron 				iocommand.buf_size)) {
5559c1f63c8fSStephen M. Cameron 				rc = -EFAULT;
5560c1f63c8fSStephen M. Cameron 				goto out_kfree;
5561edd16368SStephen M. Cameron 			}
5562b03a7771SStephen M. Cameron 		} else {
5563edd16368SStephen M. Cameron 			memset(buff, 0, iocommand.buf_size);
5564b03a7771SStephen M. Cameron 		}
5565b03a7771SStephen M. Cameron 	}
556645fcb86eSStephen Cameron 	c = cmd_alloc(h);
5567bf43caf3SRobert Elliott 
5568edd16368SStephen M. Cameron 	/* Fill in the command type */
5569edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5570*a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_BUSY;
5571edd16368SStephen M. Cameron 	/* Fill in Command Header */
5572edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0; /* unused in simple mode */
5573edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {	/* buffer to fill */
5574edd16368SStephen M. Cameron 		c->Header.SGList = 1;
557550a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(1);
5576edd16368SStephen M. Cameron 	} else	{ /* no buffers to fill */
5577edd16368SStephen M. Cameron 		c->Header.SGList = 0;
557850a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(0);
5579edd16368SStephen M. Cameron 	}
5580edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
5581edd16368SStephen M. Cameron 
5582edd16368SStephen M. Cameron 	/* Fill in Request block */
5583edd16368SStephen M. Cameron 	memcpy(&c->Request, &iocommand.Request,
5584edd16368SStephen M. Cameron 		sizeof(c->Request));
5585edd16368SStephen M. Cameron 
5586edd16368SStephen M. Cameron 	/* Fill in the scatter gather information */
5587edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
558850a0decfSStephen M. Cameron 		temp64 = pci_map_single(h->pdev, buff,
5589edd16368SStephen M. Cameron 			iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
559050a0decfSStephen M. Cameron 		if (dma_mapping_error(&h->pdev->dev, (dma_addr_t) temp64)) {
559150a0decfSStephen M. Cameron 			c->SG[0].Addr = cpu_to_le64(0);
559250a0decfSStephen M. Cameron 			c->SG[0].Len = cpu_to_le32(0);
5593bcc48ffaSStephen M. Cameron 			rc = -ENOMEM;
5594bcc48ffaSStephen M. Cameron 			goto out;
5595bcc48ffaSStephen M. Cameron 		}
559650a0decfSStephen M. Cameron 		c->SG[0].Addr = cpu_to_le64(temp64);
559750a0decfSStephen M. Cameron 		c->SG[0].Len = cpu_to_le32(iocommand.buf_size);
559850a0decfSStephen M. Cameron 		c->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* not chaining */
5599edd16368SStephen M. Cameron 	}
560025163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
5601c2dd32e0SStephen M. Cameron 	if (iocommand.buf_size > 0)
5602edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
5603edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
560425163bd5SWebb Scales 	if (rc) {
560525163bd5SWebb Scales 		rc = -EIO;
560625163bd5SWebb Scales 		goto out;
560725163bd5SWebb Scales 	}
5608edd16368SStephen M. Cameron 
5609edd16368SStephen M. Cameron 	/* Copy the error information out */
5610edd16368SStephen M. Cameron 	memcpy(&iocommand.error_info, c->err_info,
5611edd16368SStephen M. Cameron 		sizeof(iocommand.error_info));
5612edd16368SStephen M. Cameron 	if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
5613c1f63c8fSStephen M. Cameron 		rc = -EFAULT;
5614c1f63c8fSStephen M. Cameron 		goto out;
5615edd16368SStephen M. Cameron 	}
56169233fb10SStephen M. Cameron 	if ((iocommand.Request.Type.Direction & XFER_READ) &&
5617b03a7771SStephen M. Cameron 		iocommand.buf_size > 0) {
5618edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
5619edd16368SStephen M. Cameron 		if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
5620c1f63c8fSStephen M. Cameron 			rc = -EFAULT;
5621c1f63c8fSStephen M. Cameron 			goto out;
5622edd16368SStephen M. Cameron 		}
5623edd16368SStephen M. Cameron 	}
5624c1f63c8fSStephen M. Cameron out:
562545fcb86eSStephen Cameron 	cmd_free(h, c);
5626c1f63c8fSStephen M. Cameron out_kfree:
5627c1f63c8fSStephen M. Cameron 	kfree(buff);
5628c1f63c8fSStephen M. Cameron 	return rc;
5629edd16368SStephen M. Cameron }
5630edd16368SStephen M. Cameron 
5631edd16368SStephen M. Cameron static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
5632edd16368SStephen M. Cameron {
5633edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct *ioc;
5634edd16368SStephen M. Cameron 	struct CommandList *c;
5635edd16368SStephen M. Cameron 	unsigned char **buff = NULL;
5636edd16368SStephen M. Cameron 	int *buff_size = NULL;
563750a0decfSStephen M. Cameron 	u64 temp64;
5638edd16368SStephen M. Cameron 	BYTE sg_used = 0;
5639edd16368SStephen M. Cameron 	int status = 0;
564001a02ffcSStephen M. Cameron 	u32 left;
564101a02ffcSStephen M. Cameron 	u32 sz;
5642edd16368SStephen M. Cameron 	BYTE __user *data_ptr;
5643edd16368SStephen M. Cameron 
5644edd16368SStephen M. Cameron 	if (!argp)
5645edd16368SStephen M. Cameron 		return -EINVAL;
5646edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
5647edd16368SStephen M. Cameron 		return -EPERM;
5648edd16368SStephen M. Cameron 	ioc = (BIG_IOCTL_Command_struct *)
5649edd16368SStephen M. Cameron 	    kmalloc(sizeof(*ioc), GFP_KERNEL);
5650edd16368SStephen M. Cameron 	if (!ioc) {
5651edd16368SStephen M. Cameron 		status = -ENOMEM;
5652edd16368SStephen M. Cameron 		goto cleanup1;
5653edd16368SStephen M. Cameron 	}
5654edd16368SStephen M. Cameron 	if (copy_from_user(ioc, argp, sizeof(*ioc))) {
5655edd16368SStephen M. Cameron 		status = -EFAULT;
5656edd16368SStephen M. Cameron 		goto cleanup1;
5657edd16368SStephen M. Cameron 	}
5658edd16368SStephen M. Cameron 	if ((ioc->buf_size < 1) &&
5659edd16368SStephen M. Cameron 	    (ioc->Request.Type.Direction != XFER_NONE)) {
5660edd16368SStephen M. Cameron 		status = -EINVAL;
5661edd16368SStephen M. Cameron 		goto cleanup1;
5662edd16368SStephen M. Cameron 	}
5663edd16368SStephen M. Cameron 	/* Check kmalloc limits  using all SGs */
5664edd16368SStephen M. Cameron 	if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
5665edd16368SStephen M. Cameron 		status = -EINVAL;
5666edd16368SStephen M. Cameron 		goto cleanup1;
5667edd16368SStephen M. Cameron 	}
5668d66ae08bSStephen M. Cameron 	if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
5669edd16368SStephen M. Cameron 		status = -EINVAL;
5670edd16368SStephen M. Cameron 		goto cleanup1;
5671edd16368SStephen M. Cameron 	}
5672d66ae08bSStephen M. Cameron 	buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
5673edd16368SStephen M. Cameron 	if (!buff) {
5674edd16368SStephen M. Cameron 		status = -ENOMEM;
5675edd16368SStephen M. Cameron 		goto cleanup1;
5676edd16368SStephen M. Cameron 	}
5677d66ae08bSStephen M. Cameron 	buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
5678edd16368SStephen M. Cameron 	if (!buff_size) {
5679edd16368SStephen M. Cameron 		status = -ENOMEM;
5680edd16368SStephen M. Cameron 		goto cleanup1;
5681edd16368SStephen M. Cameron 	}
5682edd16368SStephen M. Cameron 	left = ioc->buf_size;
5683edd16368SStephen M. Cameron 	data_ptr = ioc->buf;
5684edd16368SStephen M. Cameron 	while (left) {
5685edd16368SStephen M. Cameron 		sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
5686edd16368SStephen M. Cameron 		buff_size[sg_used] = sz;
5687edd16368SStephen M. Cameron 		buff[sg_used] = kmalloc(sz, GFP_KERNEL);
5688edd16368SStephen M. Cameron 		if (buff[sg_used] == NULL) {
5689edd16368SStephen M. Cameron 			status = -ENOMEM;
5690edd16368SStephen M. Cameron 			goto cleanup1;
5691edd16368SStephen M. Cameron 		}
56929233fb10SStephen M. Cameron 		if (ioc->Request.Type.Direction & XFER_WRITE) {
5693edd16368SStephen M. Cameron 			if (copy_from_user(buff[sg_used], data_ptr, sz)) {
56940758f4f7SStephen M. Cameron 				status = -EFAULT;
5695edd16368SStephen M. Cameron 				goto cleanup1;
5696edd16368SStephen M. Cameron 			}
5697edd16368SStephen M. Cameron 		} else
5698edd16368SStephen M. Cameron 			memset(buff[sg_used], 0, sz);
5699edd16368SStephen M. Cameron 		left -= sz;
5700edd16368SStephen M. Cameron 		data_ptr += sz;
5701edd16368SStephen M. Cameron 		sg_used++;
5702edd16368SStephen M. Cameron 	}
570345fcb86eSStephen Cameron 	c = cmd_alloc(h);
5704bf43caf3SRobert Elliott 
5705edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5706*a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_BUSY;
5707edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
570850a0decfSStephen M. Cameron 	c->Header.SGList = (u8) sg_used;
570950a0decfSStephen M. Cameron 	c->Header.SGTotal = cpu_to_le16(sg_used);
5710edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
5711edd16368SStephen M. Cameron 	memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
5712edd16368SStephen M. Cameron 	if (ioc->buf_size > 0) {
5713edd16368SStephen M. Cameron 		int i;
5714edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
571550a0decfSStephen M. Cameron 			temp64 = pci_map_single(h->pdev, buff[i],
5716edd16368SStephen M. Cameron 				    buff_size[i], PCI_DMA_BIDIRECTIONAL);
571750a0decfSStephen M. Cameron 			if (dma_mapping_error(&h->pdev->dev,
571850a0decfSStephen M. Cameron 							(dma_addr_t) temp64)) {
571950a0decfSStephen M. Cameron 				c->SG[i].Addr = cpu_to_le64(0);
572050a0decfSStephen M. Cameron 				c->SG[i].Len = cpu_to_le32(0);
5721bcc48ffaSStephen M. Cameron 				hpsa_pci_unmap(h->pdev, c, i,
5722bcc48ffaSStephen M. Cameron 					PCI_DMA_BIDIRECTIONAL);
5723bcc48ffaSStephen M. Cameron 				status = -ENOMEM;
5724e2d4a1f6SStephen M. Cameron 				goto cleanup0;
5725bcc48ffaSStephen M. Cameron 			}
572650a0decfSStephen M. Cameron 			c->SG[i].Addr = cpu_to_le64(temp64);
572750a0decfSStephen M. Cameron 			c->SG[i].Len = cpu_to_le32(buff_size[i]);
572850a0decfSStephen M. Cameron 			c->SG[i].Ext = cpu_to_le32(0);
5729edd16368SStephen M. Cameron 		}
573050a0decfSStephen M. Cameron 		c->SG[--i].Ext = cpu_to_le32(HPSA_SG_LAST);
5731edd16368SStephen M. Cameron 	}
573225163bd5SWebb Scales 	status = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
5733b03a7771SStephen M. Cameron 	if (sg_used)
5734edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
5735edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
573625163bd5SWebb Scales 	if (status) {
573725163bd5SWebb Scales 		status = -EIO;
573825163bd5SWebb Scales 		goto cleanup0;
573925163bd5SWebb Scales 	}
574025163bd5SWebb Scales 
5741edd16368SStephen M. Cameron 	/* Copy the error information out */
5742edd16368SStephen M. Cameron 	memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
5743edd16368SStephen M. Cameron 	if (copy_to_user(argp, ioc, sizeof(*ioc))) {
5744edd16368SStephen M. Cameron 		status = -EFAULT;
5745e2d4a1f6SStephen M. Cameron 		goto cleanup0;
5746edd16368SStephen M. Cameron 	}
57479233fb10SStephen M. Cameron 	if ((ioc->Request.Type.Direction & XFER_READ) && ioc->buf_size > 0) {
57482b08b3e9SDon Brace 		int i;
57492b08b3e9SDon Brace 
5750edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
5751edd16368SStephen M. Cameron 		BYTE __user *ptr = ioc->buf;
5752edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
5753edd16368SStephen M. Cameron 			if (copy_to_user(ptr, buff[i], buff_size[i])) {
5754edd16368SStephen M. Cameron 				status = -EFAULT;
5755e2d4a1f6SStephen M. Cameron 				goto cleanup0;
5756edd16368SStephen M. Cameron 			}
5757edd16368SStephen M. Cameron 			ptr += buff_size[i];
5758edd16368SStephen M. Cameron 		}
5759edd16368SStephen M. Cameron 	}
5760edd16368SStephen M. Cameron 	status = 0;
5761e2d4a1f6SStephen M. Cameron cleanup0:
576245fcb86eSStephen Cameron 	cmd_free(h, c);
5763edd16368SStephen M. Cameron cleanup1:
5764edd16368SStephen M. Cameron 	if (buff) {
57652b08b3e9SDon Brace 		int i;
57662b08b3e9SDon Brace 
5767edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++)
5768edd16368SStephen M. Cameron 			kfree(buff[i]);
5769edd16368SStephen M. Cameron 		kfree(buff);
5770edd16368SStephen M. Cameron 	}
5771edd16368SStephen M. Cameron 	kfree(buff_size);
5772edd16368SStephen M. Cameron 	kfree(ioc);
5773edd16368SStephen M. Cameron 	return status;
5774edd16368SStephen M. Cameron }
5775edd16368SStephen M. Cameron 
5776edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
5777edd16368SStephen M. Cameron 	struct CommandList *c)
5778edd16368SStephen M. Cameron {
5779edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
5780edd16368SStephen M. Cameron 			c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
5781edd16368SStephen M. Cameron 		(void) check_for_unit_attention(h, c);
5782edd16368SStephen M. Cameron }
57830390f0c0SStephen M. Cameron 
5784edd16368SStephen M. Cameron /*
5785edd16368SStephen M. Cameron  * ioctl
5786edd16368SStephen M. Cameron  */
578742a91641SDon Brace static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
5788edd16368SStephen M. Cameron {
5789edd16368SStephen M. Cameron 	struct ctlr_info *h;
5790edd16368SStephen M. Cameron 	void __user *argp = (void __user *)arg;
57910390f0c0SStephen M. Cameron 	int rc;
5792edd16368SStephen M. Cameron 
5793edd16368SStephen M. Cameron 	h = sdev_to_hba(dev);
5794edd16368SStephen M. Cameron 
5795edd16368SStephen M. Cameron 	switch (cmd) {
5796edd16368SStephen M. Cameron 	case CCISS_DEREGDISK:
5797edd16368SStephen M. Cameron 	case CCISS_REGNEWDISK:
5798edd16368SStephen M. Cameron 	case CCISS_REGNEWD:
5799a08a8471SStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
5800edd16368SStephen M. Cameron 		return 0;
5801edd16368SStephen M. Cameron 	case CCISS_GETPCIINFO:
5802edd16368SStephen M. Cameron 		return hpsa_getpciinfo_ioctl(h, argp);
5803edd16368SStephen M. Cameron 	case CCISS_GETDRIVVER:
5804edd16368SStephen M. Cameron 		return hpsa_getdrivver_ioctl(h, argp);
5805edd16368SStephen M. Cameron 	case CCISS_PASSTHRU:
580634f0c627SDon Brace 		if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
58070390f0c0SStephen M. Cameron 			return -EAGAIN;
58080390f0c0SStephen M. Cameron 		rc = hpsa_passthru_ioctl(h, argp);
580934f0c627SDon Brace 		atomic_inc(&h->passthru_cmds_avail);
58100390f0c0SStephen M. Cameron 		return rc;
5811edd16368SStephen M. Cameron 	case CCISS_BIG_PASSTHRU:
581234f0c627SDon Brace 		if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
58130390f0c0SStephen M. Cameron 			return -EAGAIN;
58140390f0c0SStephen M. Cameron 		rc = hpsa_big_passthru_ioctl(h, argp);
581534f0c627SDon Brace 		atomic_inc(&h->passthru_cmds_avail);
58160390f0c0SStephen M. Cameron 		return rc;
5817edd16368SStephen M. Cameron 	default:
5818edd16368SStephen M. Cameron 		return -ENOTTY;
5819edd16368SStephen M. Cameron 	}
5820edd16368SStephen M. Cameron }
5821edd16368SStephen M. Cameron 
5822bf43caf3SRobert Elliott static void hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
58236f039790SGreg Kroah-Hartman 				u8 reset_type)
582464670ac8SStephen M. Cameron {
582564670ac8SStephen M. Cameron 	struct CommandList *c;
582664670ac8SStephen M. Cameron 
582764670ac8SStephen M. Cameron 	c = cmd_alloc(h);
5828bf43caf3SRobert Elliott 
5829a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map */
5830a2dac136SStephen M. Cameron 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
583164670ac8SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_MSG);
583264670ac8SStephen M. Cameron 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
583364670ac8SStephen M. Cameron 	c->waiting = NULL;
583464670ac8SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
583564670ac8SStephen M. Cameron 	/* Don't wait for completion, the reset won't complete.  Don't free
583664670ac8SStephen M. Cameron 	 * the command either.  This is the last command we will send before
583764670ac8SStephen M. Cameron 	 * re-initializing everything, so it doesn't matter and won't leak.
583864670ac8SStephen M. Cameron 	 */
5839bf43caf3SRobert Elliott 	return;
584064670ac8SStephen M. Cameron }
584164670ac8SStephen M. Cameron 
5842a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
5843b7bb24ebSStephen M. Cameron 	void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
5844edd16368SStephen M. Cameron 	int cmd_type)
5845edd16368SStephen M. Cameron {
5846edd16368SStephen M. Cameron 	int pci_dir = XFER_NONE;
58479b5c48c2SStephen Cameron 	u64 tag; /* for commands to be aborted */
5848edd16368SStephen M. Cameron 
5849edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5850*a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_BUSY;
5851edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
5852edd16368SStephen M. Cameron 	if (buff != NULL && size > 0) {
5853edd16368SStephen M. Cameron 		c->Header.SGList = 1;
585450a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(1);
5855edd16368SStephen M. Cameron 	} else {
5856edd16368SStephen M. Cameron 		c->Header.SGList = 0;
585750a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(0);
5858edd16368SStephen M. Cameron 	}
5859edd16368SStephen M. Cameron 	memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
5860edd16368SStephen M. Cameron 
5861edd16368SStephen M. Cameron 	if (cmd_type == TYPE_CMD) {
5862edd16368SStephen M. Cameron 		switch (cmd) {
5863edd16368SStephen M. Cameron 		case HPSA_INQUIRY:
5864edd16368SStephen M. Cameron 			/* are we trying to read a vital product page */
5865b7bb24ebSStephen M. Cameron 			if (page_code & VPD_PAGE) {
5866edd16368SStephen M. Cameron 				c->Request.CDB[1] = 0x01;
5867b7bb24ebSStephen M. Cameron 				c->Request.CDB[2] = (page_code & 0xff);
5868edd16368SStephen M. Cameron 			}
5869edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
5870a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5871a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5872edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5873edd16368SStephen M. Cameron 			c->Request.CDB[0] = HPSA_INQUIRY;
5874edd16368SStephen M. Cameron 			c->Request.CDB[4] = size & 0xFF;
5875edd16368SStephen M. Cameron 			break;
5876edd16368SStephen M. Cameron 		case HPSA_REPORT_LOG:
5877edd16368SStephen M. Cameron 		case HPSA_REPORT_PHYS:
5878edd16368SStephen M. Cameron 			/* Talking to controller so It's a physical command
5879edd16368SStephen M. Cameron 			   mode = 00 target = 0.  Nothing to write.
5880edd16368SStephen M. Cameron 			 */
5881edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
5882a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5883a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5884edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5885edd16368SStephen M. Cameron 			c->Request.CDB[0] = cmd;
5886edd16368SStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
5887edd16368SStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5888edd16368SStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5889edd16368SStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
5890edd16368SStephen M. Cameron 			break;
5891edd16368SStephen M. Cameron 		case HPSA_CACHE_FLUSH:
5892edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
5893a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5894a505b86fSStephen M. Cameron 					TYPE_ATTR_DIR(cmd_type,
5895a505b86fSStephen M. Cameron 						ATTR_SIMPLE, XFER_WRITE);
5896edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5897edd16368SStephen M. Cameron 			c->Request.CDB[0] = BMIC_WRITE;
5898edd16368SStephen M. Cameron 			c->Request.CDB[6] = BMIC_CACHE_FLUSH;
5899bb158eabSStephen M. Cameron 			c->Request.CDB[7] = (size >> 8) & 0xFF;
5900bb158eabSStephen M. Cameron 			c->Request.CDB[8] = size & 0xFF;
5901edd16368SStephen M. Cameron 			break;
5902edd16368SStephen M. Cameron 		case TEST_UNIT_READY:
5903edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
5904a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5905a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
5906edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5907edd16368SStephen M. Cameron 			break;
5908283b4a9bSStephen M. Cameron 		case HPSA_GET_RAID_MAP:
5909283b4a9bSStephen M. Cameron 			c->Request.CDBLen = 12;
5910a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5911a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5912283b4a9bSStephen M. Cameron 			c->Request.Timeout = 0;
5913283b4a9bSStephen M. Cameron 			c->Request.CDB[0] = HPSA_CISS_READ;
5914283b4a9bSStephen M. Cameron 			c->Request.CDB[1] = cmd;
5915283b4a9bSStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
5916283b4a9bSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5917283b4a9bSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5918283b4a9bSStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
5919283b4a9bSStephen M. Cameron 			break;
5920316b221aSStephen M. Cameron 		case BMIC_SENSE_CONTROLLER_PARAMETERS:
5921316b221aSStephen M. Cameron 			c->Request.CDBLen = 10;
5922a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5923a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5924316b221aSStephen M. Cameron 			c->Request.Timeout = 0;
5925316b221aSStephen M. Cameron 			c->Request.CDB[0] = BMIC_READ;
5926316b221aSStephen M. Cameron 			c->Request.CDB[6] = BMIC_SENSE_CONTROLLER_PARAMETERS;
5927316b221aSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5928316b221aSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5929316b221aSStephen M. Cameron 			break;
593003383736SDon Brace 		case BMIC_IDENTIFY_PHYSICAL_DEVICE:
593103383736SDon Brace 			c->Request.CDBLen = 10;
593203383736SDon Brace 			c->Request.type_attr_dir =
593303383736SDon Brace 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
593403383736SDon Brace 			c->Request.Timeout = 0;
593503383736SDon Brace 			c->Request.CDB[0] = BMIC_READ;
593603383736SDon Brace 			c->Request.CDB[6] = BMIC_IDENTIFY_PHYSICAL_DEVICE;
593703383736SDon Brace 			c->Request.CDB[7] = (size >> 16) & 0xFF;
593803383736SDon Brace 			c->Request.CDB[8] = (size >> 8) & 0XFF;
593903383736SDon Brace 			break;
5940edd16368SStephen M. Cameron 		default:
5941edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
5942edd16368SStephen M. Cameron 			BUG();
5943a2dac136SStephen M. Cameron 			return -1;
5944edd16368SStephen M. Cameron 		}
5945edd16368SStephen M. Cameron 	} else if (cmd_type == TYPE_MSG) {
5946edd16368SStephen M. Cameron 		switch (cmd) {
5947edd16368SStephen M. Cameron 
5948edd16368SStephen M. Cameron 		case  HPSA_DEVICE_RESET_MSG:
5949edd16368SStephen M. Cameron 			c->Request.CDBLen = 16;
5950a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5951a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
5952edd16368SStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
595364670ac8SStephen M. Cameron 			memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
595464670ac8SStephen M. Cameron 			c->Request.CDB[0] =  cmd;
595521e89afdSStephen M. Cameron 			c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
5956edd16368SStephen M. Cameron 			/* If bytes 4-7 are zero, it means reset the */
5957edd16368SStephen M. Cameron 			/* LunID device */
5958edd16368SStephen M. Cameron 			c->Request.CDB[4] = 0x00;
5959edd16368SStephen M. Cameron 			c->Request.CDB[5] = 0x00;
5960edd16368SStephen M. Cameron 			c->Request.CDB[6] = 0x00;
5961edd16368SStephen M. Cameron 			c->Request.CDB[7] = 0x00;
5962edd16368SStephen M. Cameron 			break;
596375167d2cSStephen M. Cameron 		case  HPSA_ABORT_MSG:
59649b5c48c2SStephen Cameron 			memcpy(&tag, buff, sizeof(tag));
59652b08b3e9SDon Brace 			dev_dbg(&h->pdev->dev,
59669b5c48c2SStephen Cameron 				"Abort Tag:0x%016llx using rqst Tag:0x%016llx",
59679b5c48c2SStephen Cameron 				tag, c->Header.tag);
596875167d2cSStephen M. Cameron 			c->Request.CDBLen = 16;
5969a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5970a505b86fSStephen M. Cameron 					TYPE_ATTR_DIR(cmd_type,
5971a505b86fSStephen M. Cameron 						ATTR_SIMPLE, XFER_WRITE);
597275167d2cSStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
597375167d2cSStephen M. Cameron 			c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
597475167d2cSStephen M. Cameron 			c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
597575167d2cSStephen M. Cameron 			c->Request.CDB[2] = 0x00; /* reserved */
597675167d2cSStephen M. Cameron 			c->Request.CDB[3] = 0x00; /* reserved */
597775167d2cSStephen M. Cameron 			/* Tag to abort goes in CDB[4]-CDB[11] */
59789b5c48c2SStephen Cameron 			memcpy(&c->Request.CDB[4], &tag, sizeof(tag));
597975167d2cSStephen M. Cameron 			c->Request.CDB[12] = 0x00; /* reserved */
598075167d2cSStephen M. Cameron 			c->Request.CDB[13] = 0x00; /* reserved */
598175167d2cSStephen M. Cameron 			c->Request.CDB[14] = 0x00; /* reserved */
598275167d2cSStephen M. Cameron 			c->Request.CDB[15] = 0x00; /* reserved */
598375167d2cSStephen M. Cameron 		break;
5984edd16368SStephen M. Cameron 		default:
5985edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown message type %d\n",
5986edd16368SStephen M. Cameron 				cmd);
5987edd16368SStephen M. Cameron 			BUG();
5988edd16368SStephen M. Cameron 		}
5989edd16368SStephen M. Cameron 	} else {
5990edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
5991edd16368SStephen M. Cameron 		BUG();
5992edd16368SStephen M. Cameron 	}
5993edd16368SStephen M. Cameron 
5994a505b86fSStephen M. Cameron 	switch (GET_DIR(c->Request.type_attr_dir)) {
5995edd16368SStephen M. Cameron 	case XFER_READ:
5996edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_FROMDEVICE;
5997edd16368SStephen M. Cameron 		break;
5998edd16368SStephen M. Cameron 	case XFER_WRITE:
5999edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_TODEVICE;
6000edd16368SStephen M. Cameron 		break;
6001edd16368SStephen M. Cameron 	case XFER_NONE:
6002edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_NONE;
6003edd16368SStephen M. Cameron 		break;
6004edd16368SStephen M. Cameron 	default:
6005edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_BIDIRECTIONAL;
6006edd16368SStephen M. Cameron 	}
6007a2dac136SStephen M. Cameron 	if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
6008a2dac136SStephen M. Cameron 		return -1;
6009a2dac136SStephen M. Cameron 	return 0;
6010edd16368SStephen M. Cameron }
6011edd16368SStephen M. Cameron 
6012edd16368SStephen M. Cameron /*
6013edd16368SStephen M. Cameron  * Map (physical) PCI mem into (virtual) kernel space
6014edd16368SStephen M. Cameron  */
6015edd16368SStephen M. Cameron static void __iomem *remap_pci_mem(ulong base, ulong size)
6016edd16368SStephen M. Cameron {
6017edd16368SStephen M. Cameron 	ulong page_base = ((ulong) base) & PAGE_MASK;
6018edd16368SStephen M. Cameron 	ulong page_offs = ((ulong) base) - page_base;
6019088ba34cSStephen M. Cameron 	void __iomem *page_remapped = ioremap_nocache(page_base,
6020088ba34cSStephen M. Cameron 		page_offs + size);
6021edd16368SStephen M. Cameron 
6022edd16368SStephen M. Cameron 	return page_remapped ? (page_remapped + page_offs) : NULL;
6023edd16368SStephen M. Cameron }
6024edd16368SStephen M. Cameron 
6025254f796bSMatt Gates static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
6026edd16368SStephen M. Cameron {
6027254f796bSMatt Gates 	return h->access.command_completed(h, q);
6028edd16368SStephen M. Cameron }
6029edd16368SStephen M. Cameron 
6030900c5440SStephen M. Cameron static inline bool interrupt_pending(struct ctlr_info *h)
6031edd16368SStephen M. Cameron {
6032edd16368SStephen M. Cameron 	return h->access.intr_pending(h);
6033edd16368SStephen M. Cameron }
6034edd16368SStephen M. Cameron 
6035edd16368SStephen M. Cameron static inline long interrupt_not_for_us(struct ctlr_info *h)
6036edd16368SStephen M. Cameron {
603710f66018SStephen M. Cameron 	return (h->access.intr_pending(h) == 0) ||
603810f66018SStephen M. Cameron 		(h->interrupts_enabled == 0);
6039edd16368SStephen M. Cameron }
6040edd16368SStephen M. Cameron 
604101a02ffcSStephen M. Cameron static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
604201a02ffcSStephen M. Cameron 	u32 raw_tag)
6043edd16368SStephen M. Cameron {
6044edd16368SStephen M. Cameron 	if (unlikely(tag_index >= h->nr_cmds)) {
6045edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
6046edd16368SStephen M. Cameron 		return 1;
6047edd16368SStephen M. Cameron 	}
6048edd16368SStephen M. Cameron 	return 0;
6049edd16368SStephen M. Cameron }
6050edd16368SStephen M. Cameron 
60515a3d16f5SStephen M. Cameron static inline void finish_cmd(struct CommandList *c)
6052edd16368SStephen M. Cameron {
6053e85c5974SStephen M. Cameron 	dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
6054c349775eSScott Teel 	if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
6055c349775eSScott Teel 			|| c->cmd_type == CMD_IOACCEL2))
60561fb011fbSStephen M. Cameron 		complete_scsi_command(c);
60578be986ccSStephen Cameron 	else if (c->cmd_type == CMD_IOCTL_PEND || c->cmd_type == IOACCEL2_TMF)
6058edd16368SStephen M. Cameron 		complete(c->waiting);
6059a104c99fSStephen M. Cameron }
6060a104c99fSStephen M. Cameron 
6061a9a3a273SStephen M. Cameron 
6062a9a3a273SStephen M. Cameron static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
6063a104c99fSStephen M. Cameron {
6064a9a3a273SStephen M. Cameron #define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
6065a9a3a273SStephen M. Cameron #define HPSA_SIMPLE_ERROR_BITS 0x03
6066960a30e7SStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
6067a9a3a273SStephen M. Cameron 		return tag & ~HPSA_SIMPLE_ERROR_BITS;
6068a9a3a273SStephen M. Cameron 	return tag & ~HPSA_PERF_ERROR_BITS;
6069a104c99fSStephen M. Cameron }
6070a104c99fSStephen M. Cameron 
6071303932fdSDon Brace /* process completion of an indexed ("direct lookup") command */
60721d94f94dSStephen M. Cameron static inline void process_indexed_cmd(struct ctlr_info *h,
6073303932fdSDon Brace 	u32 raw_tag)
6074303932fdSDon Brace {
6075303932fdSDon Brace 	u32 tag_index;
6076303932fdSDon Brace 	struct CommandList *c;
6077303932fdSDon Brace 
6078f2405db8SDon Brace 	tag_index = raw_tag >> DIRECT_LOOKUP_SHIFT;
60791d94f94dSStephen M. Cameron 	if (!bad_tag(h, tag_index, raw_tag)) {
6080303932fdSDon Brace 		c = h->cmd_pool + tag_index;
60815a3d16f5SStephen M. Cameron 		finish_cmd(c);
60821d94f94dSStephen M. Cameron 	}
6083303932fdSDon Brace }
6084303932fdSDon Brace 
608564670ac8SStephen M. Cameron /* Some controllers, like p400, will give us one interrupt
608664670ac8SStephen M. Cameron  * after a soft reset, even if we turned interrupts off.
608764670ac8SStephen M. Cameron  * Only need to check for this in the hpsa_xxx_discard_completions
608864670ac8SStephen M. Cameron  * functions.
608964670ac8SStephen M. Cameron  */
609064670ac8SStephen M. Cameron static int ignore_bogus_interrupt(struct ctlr_info *h)
609164670ac8SStephen M. Cameron {
609264670ac8SStephen M. Cameron 	if (likely(!reset_devices))
609364670ac8SStephen M. Cameron 		return 0;
609464670ac8SStephen M. Cameron 
609564670ac8SStephen M. Cameron 	if (likely(h->interrupts_enabled))
609664670ac8SStephen M. Cameron 		return 0;
609764670ac8SStephen M. Cameron 
609864670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
609964670ac8SStephen M. Cameron 		"(known firmware bug.)  Ignoring.\n");
610064670ac8SStephen M. Cameron 
610164670ac8SStephen M. Cameron 	return 1;
610264670ac8SStephen M. Cameron }
610364670ac8SStephen M. Cameron 
6104254f796bSMatt Gates /*
6105254f796bSMatt Gates  * Convert &h->q[x] (passed to interrupt handlers) back to h.
6106254f796bSMatt Gates  * Relies on (h-q[x] == x) being true for x such that
6107254f796bSMatt Gates  * 0 <= x < MAX_REPLY_QUEUES.
6108254f796bSMatt Gates  */
6109254f796bSMatt Gates static struct ctlr_info *queue_to_hba(u8 *queue)
611064670ac8SStephen M. Cameron {
6111254f796bSMatt Gates 	return container_of((queue - *queue), struct ctlr_info, q[0]);
6112254f796bSMatt Gates }
6113254f796bSMatt Gates 
6114254f796bSMatt Gates static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
6115254f796bSMatt Gates {
6116254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
6117254f796bSMatt Gates 	u8 q = *(u8 *) queue;
611864670ac8SStephen M. Cameron 	u32 raw_tag;
611964670ac8SStephen M. Cameron 
612064670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
612164670ac8SStephen M. Cameron 		return IRQ_NONE;
612264670ac8SStephen M. Cameron 
612364670ac8SStephen M. Cameron 	if (interrupt_not_for_us(h))
612464670ac8SStephen M. Cameron 		return IRQ_NONE;
6125a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
612664670ac8SStephen M. Cameron 	while (interrupt_pending(h)) {
6127254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
612864670ac8SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY)
6129254f796bSMatt Gates 			raw_tag = next_command(h, q);
613064670ac8SStephen M. Cameron 	}
613164670ac8SStephen M. Cameron 	return IRQ_HANDLED;
613264670ac8SStephen M. Cameron }
613364670ac8SStephen M. Cameron 
6134254f796bSMatt Gates static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
613564670ac8SStephen M. Cameron {
6136254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
613764670ac8SStephen M. Cameron 	u32 raw_tag;
6138254f796bSMatt Gates 	u8 q = *(u8 *) queue;
613964670ac8SStephen M. Cameron 
614064670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
614164670ac8SStephen M. Cameron 		return IRQ_NONE;
614264670ac8SStephen M. Cameron 
6143a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
6144254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
614564670ac8SStephen M. Cameron 	while (raw_tag != FIFO_EMPTY)
6146254f796bSMatt Gates 		raw_tag = next_command(h, q);
614764670ac8SStephen M. Cameron 	return IRQ_HANDLED;
614864670ac8SStephen M. Cameron }
614964670ac8SStephen M. Cameron 
6150254f796bSMatt Gates static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
6151edd16368SStephen M. Cameron {
6152254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba((u8 *) queue);
6153303932fdSDon Brace 	u32 raw_tag;
6154254f796bSMatt Gates 	u8 q = *(u8 *) queue;
6155edd16368SStephen M. Cameron 
6156edd16368SStephen M. Cameron 	if (interrupt_not_for_us(h))
6157edd16368SStephen M. Cameron 		return IRQ_NONE;
6158a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
615910f66018SStephen M. Cameron 	while (interrupt_pending(h)) {
6160254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
616110f66018SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY) {
61621d94f94dSStephen M. Cameron 			process_indexed_cmd(h, raw_tag);
6163254f796bSMatt Gates 			raw_tag = next_command(h, q);
616410f66018SStephen M. Cameron 		}
616510f66018SStephen M. Cameron 	}
616610f66018SStephen M. Cameron 	return IRQ_HANDLED;
616710f66018SStephen M. Cameron }
616810f66018SStephen M. Cameron 
6169254f796bSMatt Gates static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
617010f66018SStephen M. Cameron {
6171254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
617210f66018SStephen M. Cameron 	u32 raw_tag;
6173254f796bSMatt Gates 	u8 q = *(u8 *) queue;
617410f66018SStephen M. Cameron 
6175a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
6176254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
6177303932fdSDon Brace 	while (raw_tag != FIFO_EMPTY) {
61781d94f94dSStephen M. Cameron 		process_indexed_cmd(h, raw_tag);
6179254f796bSMatt Gates 		raw_tag = next_command(h, q);
6180edd16368SStephen M. Cameron 	}
6181edd16368SStephen M. Cameron 	return IRQ_HANDLED;
6182edd16368SStephen M. Cameron }
6183edd16368SStephen M. Cameron 
6184a9a3a273SStephen M. Cameron /* Send a message CDB to the firmware. Careful, this only works
6185a9a3a273SStephen M. Cameron  * in simple mode, not performant mode due to the tag lookup.
6186a9a3a273SStephen M. Cameron  * We only ever use this immediately after a controller reset.
6187a9a3a273SStephen M. Cameron  */
61886f039790SGreg Kroah-Hartman static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
6189edd16368SStephen M. Cameron 			unsigned char type)
6190edd16368SStephen M. Cameron {
6191edd16368SStephen M. Cameron 	struct Command {
6192edd16368SStephen M. Cameron 		struct CommandListHeader CommandHeader;
6193edd16368SStephen M. Cameron 		struct RequestBlock Request;
6194edd16368SStephen M. Cameron 		struct ErrDescriptor ErrorDescriptor;
6195edd16368SStephen M. Cameron 	};
6196edd16368SStephen M. Cameron 	struct Command *cmd;
6197edd16368SStephen M. Cameron 	static const size_t cmd_sz = sizeof(*cmd) +
6198edd16368SStephen M. Cameron 					sizeof(cmd->ErrorDescriptor);
6199edd16368SStephen M. Cameron 	dma_addr_t paddr64;
62002b08b3e9SDon Brace 	__le32 paddr32;
62012b08b3e9SDon Brace 	u32 tag;
6202edd16368SStephen M. Cameron 	void __iomem *vaddr;
6203edd16368SStephen M. Cameron 	int i, err;
6204edd16368SStephen M. Cameron 
6205edd16368SStephen M. Cameron 	vaddr = pci_ioremap_bar(pdev, 0);
6206edd16368SStephen M. Cameron 	if (vaddr == NULL)
6207edd16368SStephen M. Cameron 		return -ENOMEM;
6208edd16368SStephen M. Cameron 
6209edd16368SStephen M. Cameron 	/* The Inbound Post Queue only accepts 32-bit physical addresses for the
6210edd16368SStephen M. Cameron 	 * CCISS commands, so they must be allocated from the lower 4GiB of
6211edd16368SStephen M. Cameron 	 * memory.
6212edd16368SStephen M. Cameron 	 */
6213edd16368SStephen M. Cameron 	err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
6214edd16368SStephen M. Cameron 	if (err) {
6215edd16368SStephen M. Cameron 		iounmap(vaddr);
62161eaec8f3SRobert Elliott 		return err;
6217edd16368SStephen M. Cameron 	}
6218edd16368SStephen M. Cameron 
6219edd16368SStephen M. Cameron 	cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
6220edd16368SStephen M. Cameron 	if (cmd == NULL) {
6221edd16368SStephen M. Cameron 		iounmap(vaddr);
6222edd16368SStephen M. Cameron 		return -ENOMEM;
6223edd16368SStephen M. Cameron 	}
6224edd16368SStephen M. Cameron 
6225edd16368SStephen M. Cameron 	/* This must fit, because of the 32-bit consistent DMA mask.  Also,
6226edd16368SStephen M. Cameron 	 * although there's no guarantee, we assume that the address is at
6227edd16368SStephen M. Cameron 	 * least 4-byte aligned (most likely, it's page-aligned).
6228edd16368SStephen M. Cameron 	 */
62292b08b3e9SDon Brace 	paddr32 = cpu_to_le32(paddr64);
6230edd16368SStephen M. Cameron 
6231edd16368SStephen M. Cameron 	cmd->CommandHeader.ReplyQueue = 0;
6232edd16368SStephen M. Cameron 	cmd->CommandHeader.SGList = 0;
623350a0decfSStephen M. Cameron 	cmd->CommandHeader.SGTotal = cpu_to_le16(0);
62342b08b3e9SDon Brace 	cmd->CommandHeader.tag = cpu_to_le64(paddr64);
6235edd16368SStephen M. Cameron 	memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
6236edd16368SStephen M. Cameron 
6237edd16368SStephen M. Cameron 	cmd->Request.CDBLen = 16;
6238a505b86fSStephen M. Cameron 	cmd->Request.type_attr_dir =
6239a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_MSG, ATTR_HEADOFQUEUE, XFER_NONE);
6240edd16368SStephen M. Cameron 	cmd->Request.Timeout = 0; /* Don't time out */
6241edd16368SStephen M. Cameron 	cmd->Request.CDB[0] = opcode;
6242edd16368SStephen M. Cameron 	cmd->Request.CDB[1] = type;
6243edd16368SStephen M. Cameron 	memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
624450a0decfSStephen M. Cameron 	cmd->ErrorDescriptor.Addr =
62452b08b3e9SDon Brace 			cpu_to_le64((le32_to_cpu(paddr32) + sizeof(*cmd)));
624650a0decfSStephen M. Cameron 	cmd->ErrorDescriptor.Len = cpu_to_le32(sizeof(struct ErrorInfo));
6247edd16368SStephen M. Cameron 
62482b08b3e9SDon Brace 	writel(le32_to_cpu(paddr32), vaddr + SA5_REQUEST_PORT_OFFSET);
6249edd16368SStephen M. Cameron 
6250edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
6251edd16368SStephen M. Cameron 		tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
62522b08b3e9SDon Brace 		if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr64)
6253edd16368SStephen M. Cameron 			break;
6254edd16368SStephen M. Cameron 		msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
6255edd16368SStephen M. Cameron 	}
6256edd16368SStephen M. Cameron 
6257edd16368SStephen M. Cameron 	iounmap(vaddr);
6258edd16368SStephen M. Cameron 
6259edd16368SStephen M. Cameron 	/* we leak the DMA buffer here ... no choice since the controller could
6260edd16368SStephen M. Cameron 	 *  still complete the command.
6261edd16368SStephen M. Cameron 	 */
6262edd16368SStephen M. Cameron 	if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
6263edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
6264edd16368SStephen M. Cameron 			opcode, type);
6265edd16368SStephen M. Cameron 		return -ETIMEDOUT;
6266edd16368SStephen M. Cameron 	}
6267edd16368SStephen M. Cameron 
6268edd16368SStephen M. Cameron 	pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
6269edd16368SStephen M. Cameron 
6270edd16368SStephen M. Cameron 	if (tag & HPSA_ERROR_BIT) {
6271edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
6272edd16368SStephen M. Cameron 			opcode, type);
6273edd16368SStephen M. Cameron 		return -EIO;
6274edd16368SStephen M. Cameron 	}
6275edd16368SStephen M. Cameron 
6276edd16368SStephen M. Cameron 	dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
6277edd16368SStephen M. Cameron 		opcode, type);
6278edd16368SStephen M. Cameron 	return 0;
6279edd16368SStephen M. Cameron }
6280edd16368SStephen M. Cameron 
6281edd16368SStephen M. Cameron #define hpsa_noop(p) hpsa_message(p, 3, 0)
6282edd16368SStephen M. Cameron 
62831df8552aSStephen M. Cameron static int hpsa_controller_hard_reset(struct pci_dev *pdev,
628442a91641SDon Brace 	void __iomem *vaddr, u32 use_doorbell)
6285edd16368SStephen M. Cameron {
6286edd16368SStephen M. Cameron 
62871df8552aSStephen M. Cameron 	if (use_doorbell) {
62881df8552aSStephen M. Cameron 		/* For everything after the P600, the PCI power state method
62891df8552aSStephen M. Cameron 		 * of resetting the controller doesn't work, so we have this
62901df8552aSStephen M. Cameron 		 * other way using the doorbell register.
6291edd16368SStephen M. Cameron 		 */
62921df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using doorbell to reset controller\n");
6293cf0b08d0SStephen M. Cameron 		writel(use_doorbell, vaddr + SA5_DOORBELL);
629485009239SStephen M. Cameron 
629500701a96SJustin Lindley 		/* PMC hardware guys tell us we need a 10 second delay after
629685009239SStephen M. Cameron 		 * doorbell reset and before any attempt to talk to the board
629785009239SStephen M. Cameron 		 * at all to ensure that this actually works and doesn't fall
629885009239SStephen M. Cameron 		 * over in some weird corner cases.
629985009239SStephen M. Cameron 		 */
630000701a96SJustin Lindley 		msleep(10000);
63011df8552aSStephen M. Cameron 	} else { /* Try to do it the PCI power state way */
6302edd16368SStephen M. Cameron 
6303edd16368SStephen M. Cameron 		/* Quoting from the Open CISS Specification: "The Power
6304edd16368SStephen M. Cameron 		 * Management Control/Status Register (CSR) controls the power
6305edd16368SStephen M. Cameron 		 * state of the device.  The normal operating state is D0,
6306edd16368SStephen M. Cameron 		 * CSR=00h.  The software off state is D3, CSR=03h.  To reset
63071df8552aSStephen M. Cameron 		 * the controller, place the interface device in D3 then to D0,
63081df8552aSStephen M. Cameron 		 * this causes a secondary PCI reset which will reset the
63091df8552aSStephen M. Cameron 		 * controller." */
6310edd16368SStephen M. Cameron 
63112662cab8SDon Brace 		int rc = 0;
63122662cab8SDon Brace 
63131df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using PCI PM to reset controller\n");
63142662cab8SDon Brace 
6315edd16368SStephen M. Cameron 		/* enter the D3hot power management state */
63162662cab8SDon Brace 		rc = pci_set_power_state(pdev, PCI_D3hot);
63172662cab8SDon Brace 		if (rc)
63182662cab8SDon Brace 			return rc;
6319edd16368SStephen M. Cameron 
6320edd16368SStephen M. Cameron 		msleep(500);
6321edd16368SStephen M. Cameron 
6322edd16368SStephen M. Cameron 		/* enter the D0 power management state */
63232662cab8SDon Brace 		rc = pci_set_power_state(pdev, PCI_D0);
63242662cab8SDon Brace 		if (rc)
63252662cab8SDon Brace 			return rc;
6326c4853efeSMike Miller 
6327c4853efeSMike Miller 		/*
6328c4853efeSMike Miller 		 * The P600 requires a small delay when changing states.
6329c4853efeSMike Miller 		 * Otherwise we may think the board did not reset and we bail.
6330c4853efeSMike Miller 		 * This for kdump only and is particular to the P600.
6331c4853efeSMike Miller 		 */
6332c4853efeSMike Miller 		msleep(500);
63331df8552aSStephen M. Cameron 	}
63341df8552aSStephen M. Cameron 	return 0;
63351df8552aSStephen M. Cameron }
63361df8552aSStephen M. Cameron 
63376f039790SGreg Kroah-Hartman static void init_driver_version(char *driver_version, int len)
6338580ada3cSStephen M. Cameron {
6339580ada3cSStephen M. Cameron 	memset(driver_version, 0, len);
6340f79cfec6SStephen M. Cameron 	strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
6341580ada3cSStephen M. Cameron }
6342580ada3cSStephen M. Cameron 
63436f039790SGreg Kroah-Hartman static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
6344580ada3cSStephen M. Cameron {
6345580ada3cSStephen M. Cameron 	char *driver_version;
6346580ada3cSStephen M. Cameron 	int i, size = sizeof(cfgtable->driver_version);
6347580ada3cSStephen M. Cameron 
6348580ada3cSStephen M. Cameron 	driver_version = kmalloc(size, GFP_KERNEL);
6349580ada3cSStephen M. Cameron 	if (!driver_version)
6350580ada3cSStephen M. Cameron 		return -ENOMEM;
6351580ada3cSStephen M. Cameron 
6352580ada3cSStephen M. Cameron 	init_driver_version(driver_version, size);
6353580ada3cSStephen M. Cameron 	for (i = 0; i < size; i++)
6354580ada3cSStephen M. Cameron 		writeb(driver_version[i], &cfgtable->driver_version[i]);
6355580ada3cSStephen M. Cameron 	kfree(driver_version);
6356580ada3cSStephen M. Cameron 	return 0;
6357580ada3cSStephen M. Cameron }
6358580ada3cSStephen M. Cameron 
63596f039790SGreg Kroah-Hartman static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
63606f039790SGreg Kroah-Hartman 					  unsigned char *driver_ver)
6361580ada3cSStephen M. Cameron {
6362580ada3cSStephen M. Cameron 	int i;
6363580ada3cSStephen M. Cameron 
6364580ada3cSStephen M. Cameron 	for (i = 0; i < sizeof(cfgtable->driver_version); i++)
6365580ada3cSStephen M. Cameron 		driver_ver[i] = readb(&cfgtable->driver_version[i]);
6366580ada3cSStephen M. Cameron }
6367580ada3cSStephen M. Cameron 
63686f039790SGreg Kroah-Hartman static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
6369580ada3cSStephen M. Cameron {
6370580ada3cSStephen M. Cameron 
6371580ada3cSStephen M. Cameron 	char *driver_ver, *old_driver_ver;
6372580ada3cSStephen M. Cameron 	int rc, size = sizeof(cfgtable->driver_version);
6373580ada3cSStephen M. Cameron 
6374580ada3cSStephen M. Cameron 	old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
6375580ada3cSStephen M. Cameron 	if (!old_driver_ver)
6376580ada3cSStephen M. Cameron 		return -ENOMEM;
6377580ada3cSStephen M. Cameron 	driver_ver = old_driver_ver + size;
6378580ada3cSStephen M. Cameron 
6379580ada3cSStephen M. Cameron 	/* After a reset, the 32 bytes of "driver version" in the cfgtable
6380580ada3cSStephen M. Cameron 	 * should have been changed, otherwise we know the reset failed.
6381580ada3cSStephen M. Cameron 	 */
6382580ada3cSStephen M. Cameron 	init_driver_version(old_driver_ver, size);
6383580ada3cSStephen M. Cameron 	read_driver_ver_from_cfgtable(cfgtable, driver_ver);
6384580ada3cSStephen M. Cameron 	rc = !memcmp(driver_ver, old_driver_ver, size);
6385580ada3cSStephen M. Cameron 	kfree(old_driver_ver);
6386580ada3cSStephen M. Cameron 	return rc;
6387580ada3cSStephen M. Cameron }
63881df8552aSStephen M. Cameron /* This does a hard reset of the controller using PCI power management
63891df8552aSStephen M. Cameron  * states or the using the doorbell register.
63901df8552aSStephen M. Cameron  */
63916b6c1cd7STomas Henzl static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev, u32 board_id)
63921df8552aSStephen M. Cameron {
63931df8552aSStephen M. Cameron 	u64 cfg_offset;
63941df8552aSStephen M. Cameron 	u32 cfg_base_addr;
63951df8552aSStephen M. Cameron 	u64 cfg_base_addr_index;
63961df8552aSStephen M. Cameron 	void __iomem *vaddr;
63971df8552aSStephen M. Cameron 	unsigned long paddr;
6398580ada3cSStephen M. Cameron 	u32 misc_fw_support;
6399270d05deSStephen M. Cameron 	int rc;
64001df8552aSStephen M. Cameron 	struct CfgTable __iomem *cfgtable;
6401cf0b08d0SStephen M. Cameron 	u32 use_doorbell;
6402270d05deSStephen M. Cameron 	u16 command_register;
64031df8552aSStephen M. Cameron 
64041df8552aSStephen M. Cameron 	/* For controllers as old as the P600, this is very nearly
64051df8552aSStephen M. Cameron 	 * the same thing as
64061df8552aSStephen M. Cameron 	 *
64071df8552aSStephen M. Cameron 	 * pci_save_state(pci_dev);
64081df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D3hot);
64091df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D0);
64101df8552aSStephen M. Cameron 	 * pci_restore_state(pci_dev);
64111df8552aSStephen M. Cameron 	 *
64121df8552aSStephen M. Cameron 	 * For controllers newer than the P600, the pci power state
64131df8552aSStephen M. Cameron 	 * method of resetting doesn't work so we have another way
64141df8552aSStephen M. Cameron 	 * using the doorbell register.
64151df8552aSStephen M. Cameron 	 */
641618867659SStephen M. Cameron 
641760f923b9SRobert Elliott 	if (!ctlr_is_resettable(board_id)) {
641860f923b9SRobert Elliott 		dev_warn(&pdev->dev, "Controller not resettable\n");
641925c1e56aSStephen M. Cameron 		return -ENODEV;
642025c1e56aSStephen M. Cameron 	}
642146380786SStephen M. Cameron 
642246380786SStephen M. Cameron 	/* if controller is soft- but not hard resettable... */
642346380786SStephen M. Cameron 	if (!ctlr_is_hard_resettable(board_id))
642446380786SStephen M. Cameron 		return -ENOTSUPP; /* try soft reset later. */
642518867659SStephen M. Cameron 
6426270d05deSStephen M. Cameron 	/* Save the PCI command register */
6427270d05deSStephen M. Cameron 	pci_read_config_word(pdev, 4, &command_register);
6428270d05deSStephen M. Cameron 	pci_save_state(pdev);
64291df8552aSStephen M. Cameron 
64301df8552aSStephen M. Cameron 	/* find the first memory BAR, so we can find the cfg table */
64311df8552aSStephen M. Cameron 	rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
64321df8552aSStephen M. Cameron 	if (rc)
64331df8552aSStephen M. Cameron 		return rc;
64341df8552aSStephen M. Cameron 	vaddr = remap_pci_mem(paddr, 0x250);
64351df8552aSStephen M. Cameron 	if (!vaddr)
64361df8552aSStephen M. Cameron 		return -ENOMEM;
64371df8552aSStephen M. Cameron 
64381df8552aSStephen M. Cameron 	/* find cfgtable in order to check if reset via doorbell is supported */
64391df8552aSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
64401df8552aSStephen M. Cameron 					&cfg_base_addr_index, &cfg_offset);
64411df8552aSStephen M. Cameron 	if (rc)
64421df8552aSStephen M. Cameron 		goto unmap_vaddr;
64431df8552aSStephen M. Cameron 	cfgtable = remap_pci_mem(pci_resource_start(pdev,
64441df8552aSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
64451df8552aSStephen M. Cameron 	if (!cfgtable) {
64461df8552aSStephen M. Cameron 		rc = -ENOMEM;
64471df8552aSStephen M. Cameron 		goto unmap_vaddr;
64481df8552aSStephen M. Cameron 	}
6449580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(cfgtable);
6450580ada3cSStephen M. Cameron 	if (rc)
645103741d95STomas Henzl 		goto unmap_cfgtable;
64521df8552aSStephen M. Cameron 
6453cf0b08d0SStephen M. Cameron 	/* If reset via doorbell register is supported, use that.
6454cf0b08d0SStephen M. Cameron 	 * There are two such methods.  Favor the newest method.
6455cf0b08d0SStephen M. Cameron 	 */
64561df8552aSStephen M. Cameron 	misc_fw_support = readl(&cfgtable->misc_fw_support);
6457cf0b08d0SStephen M. Cameron 	use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
6458cf0b08d0SStephen M. Cameron 	if (use_doorbell) {
6459cf0b08d0SStephen M. Cameron 		use_doorbell = DOORBELL_CTLR_RESET2;
6460cf0b08d0SStephen M. Cameron 	} else {
64611df8552aSStephen M. Cameron 		use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
6462cf0b08d0SStephen M. Cameron 		if (use_doorbell) {
6463050f7147SStephen Cameron 			dev_warn(&pdev->dev,
6464050f7147SStephen Cameron 				"Soft reset not supported. Firmware update is required.\n");
646564670ac8SStephen M. Cameron 			rc = -ENOTSUPP; /* try soft reset */
6466cf0b08d0SStephen M. Cameron 			goto unmap_cfgtable;
6467cf0b08d0SStephen M. Cameron 		}
6468cf0b08d0SStephen M. Cameron 	}
64691df8552aSStephen M. Cameron 
64701df8552aSStephen M. Cameron 	rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
64711df8552aSStephen M. Cameron 	if (rc)
64721df8552aSStephen M. Cameron 		goto unmap_cfgtable;
6473edd16368SStephen M. Cameron 
6474270d05deSStephen M. Cameron 	pci_restore_state(pdev);
6475270d05deSStephen M. Cameron 	pci_write_config_word(pdev, 4, command_register);
6476edd16368SStephen M. Cameron 
64771df8552aSStephen M. Cameron 	/* Some devices (notably the HP Smart Array 5i Controller)
64781df8552aSStephen M. Cameron 	   need a little pause here */
64791df8552aSStephen M. Cameron 	msleep(HPSA_POST_RESET_PAUSE_MSECS);
64801df8552aSStephen M. Cameron 
6481fe5389c8SStephen M. Cameron 	rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
6482fe5389c8SStephen M. Cameron 	if (rc) {
6483fe5389c8SStephen M. Cameron 		dev_warn(&pdev->dev,
6484050f7147SStephen Cameron 			"Failed waiting for board to become ready after hard reset\n");
6485fe5389c8SStephen M. Cameron 		goto unmap_cfgtable;
6486fe5389c8SStephen M. Cameron 	}
6487fe5389c8SStephen M. Cameron 
6488580ada3cSStephen M. Cameron 	rc = controller_reset_failed(vaddr);
6489580ada3cSStephen M. Cameron 	if (rc < 0)
6490580ada3cSStephen M. Cameron 		goto unmap_cfgtable;
6491580ada3cSStephen M. Cameron 	if (rc) {
649264670ac8SStephen M. Cameron 		dev_warn(&pdev->dev, "Unable to successfully reset "
649364670ac8SStephen M. Cameron 			"controller. Will try soft reset.\n");
649464670ac8SStephen M. Cameron 		rc = -ENOTSUPP;
6495580ada3cSStephen M. Cameron 	} else {
649664670ac8SStephen M. Cameron 		dev_info(&pdev->dev, "board ready after hard reset.\n");
64971df8552aSStephen M. Cameron 	}
64981df8552aSStephen M. Cameron 
64991df8552aSStephen M. Cameron unmap_cfgtable:
65001df8552aSStephen M. Cameron 	iounmap(cfgtable);
65011df8552aSStephen M. Cameron 
65021df8552aSStephen M. Cameron unmap_vaddr:
65031df8552aSStephen M. Cameron 	iounmap(vaddr);
65041df8552aSStephen M. Cameron 	return rc;
6505edd16368SStephen M. Cameron }
6506edd16368SStephen M. Cameron 
6507edd16368SStephen M. Cameron /*
6508edd16368SStephen M. Cameron  *  We cannot read the structure directly, for portability we must use
6509edd16368SStephen M. Cameron  *   the io functions.
6510edd16368SStephen M. Cameron  *   This is for debug only.
6511edd16368SStephen M. Cameron  */
651242a91641SDon Brace static void print_cfg_table(struct device *dev, struct CfgTable __iomem *tb)
6513edd16368SStephen M. Cameron {
651458f8665cSStephen M. Cameron #ifdef HPSA_DEBUG
6515edd16368SStephen M. Cameron 	int i;
6516edd16368SStephen M. Cameron 	char temp_name[17];
6517edd16368SStephen M. Cameron 
6518edd16368SStephen M. Cameron 	dev_info(dev, "Controller Configuration information\n");
6519edd16368SStephen M. Cameron 	dev_info(dev, "------------------------------------\n");
6520edd16368SStephen M. Cameron 	for (i = 0; i < 4; i++)
6521edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->Signature[i]));
6522edd16368SStephen M. Cameron 	temp_name[4] = '\0';
6523edd16368SStephen M. Cameron 	dev_info(dev, "   Signature = %s\n", temp_name);
6524edd16368SStephen M. Cameron 	dev_info(dev, "   Spec Number = %d\n", readl(&(tb->SpecValence)));
6525edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods supported = 0x%x\n",
6526edd16368SStephen M. Cameron 	       readl(&(tb->TransportSupport)));
6527edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods active = 0x%x\n",
6528edd16368SStephen M. Cameron 	       readl(&(tb->TransportActive)));
6529edd16368SStephen M. Cameron 	dev_info(dev, "   Requested transport Method = 0x%x\n",
6530edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.TransportRequest)));
6531edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Delay = 0x%x\n",
6532edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntDelay)));
6533edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Count = 0x%x\n",
6534edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntCount)));
653569d6e33dSRobert Elliott 	dev_info(dev, "   Max outstanding commands = %d\n",
6536edd16368SStephen M. Cameron 	       readl(&(tb->CmdsOutMax)));
6537edd16368SStephen M. Cameron 	dev_info(dev, "   Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
6538edd16368SStephen M. Cameron 	for (i = 0; i < 16; i++)
6539edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->ServerName[i]));
6540edd16368SStephen M. Cameron 	temp_name[16] = '\0';
6541edd16368SStephen M. Cameron 	dev_info(dev, "   Server Name = %s\n", temp_name);
6542edd16368SStephen M. Cameron 	dev_info(dev, "   Heartbeat Counter = 0x%x\n\n\n",
6543edd16368SStephen M. Cameron 		readl(&(tb->HeartBeat)));
6544edd16368SStephen M. Cameron #endif				/* HPSA_DEBUG */
654558f8665cSStephen M. Cameron }
6546edd16368SStephen M. Cameron 
6547edd16368SStephen M. Cameron static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
6548edd16368SStephen M. Cameron {
6549edd16368SStephen M. Cameron 	int i, offset, mem_type, bar_type;
6550edd16368SStephen M. Cameron 
6551edd16368SStephen M. Cameron 	if (pci_bar_addr == PCI_BASE_ADDRESS_0)	/* looking for BAR zero? */
6552edd16368SStephen M. Cameron 		return 0;
6553edd16368SStephen M. Cameron 	offset = 0;
6554edd16368SStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
6555edd16368SStephen M. Cameron 		bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
6556edd16368SStephen M. Cameron 		if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
6557edd16368SStephen M. Cameron 			offset += 4;
6558edd16368SStephen M. Cameron 		else {
6559edd16368SStephen M. Cameron 			mem_type = pci_resource_flags(pdev, i) &
6560edd16368SStephen M. Cameron 			    PCI_BASE_ADDRESS_MEM_TYPE_MASK;
6561edd16368SStephen M. Cameron 			switch (mem_type) {
6562edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_32:
6563edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_1M:
6564edd16368SStephen M. Cameron 				offset += 4;	/* 32 bit */
6565edd16368SStephen M. Cameron 				break;
6566edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_64:
6567edd16368SStephen M. Cameron 				offset += 8;
6568edd16368SStephen M. Cameron 				break;
6569edd16368SStephen M. Cameron 			default:	/* reserved in PCI 2.2 */
6570edd16368SStephen M. Cameron 				dev_warn(&pdev->dev,
6571edd16368SStephen M. Cameron 				       "base address is invalid\n");
6572edd16368SStephen M. Cameron 				return -1;
6573edd16368SStephen M. Cameron 				break;
6574edd16368SStephen M. Cameron 			}
6575edd16368SStephen M. Cameron 		}
6576edd16368SStephen M. Cameron 		if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
6577edd16368SStephen M. Cameron 			return i + 1;
6578edd16368SStephen M. Cameron 	}
6579edd16368SStephen M. Cameron 	return -1;
6580edd16368SStephen M. Cameron }
6581edd16368SStephen M. Cameron 
6582cc64c817SRobert Elliott static void hpsa_disable_interrupt_mode(struct ctlr_info *h)
6583cc64c817SRobert Elliott {
6584cc64c817SRobert Elliott 	if (h->msix_vector) {
6585cc64c817SRobert Elliott 		if (h->pdev->msix_enabled)
6586cc64c817SRobert Elliott 			pci_disable_msix(h->pdev);
6587105a3dbcSRobert Elliott 		h->msix_vector = 0;
6588cc64c817SRobert Elliott 	} else if (h->msi_vector) {
6589cc64c817SRobert Elliott 		if (h->pdev->msi_enabled)
6590cc64c817SRobert Elliott 			pci_disable_msi(h->pdev);
6591105a3dbcSRobert Elliott 		h->msi_vector = 0;
6592cc64c817SRobert Elliott 	}
6593cc64c817SRobert Elliott }
6594cc64c817SRobert Elliott 
6595edd16368SStephen M. Cameron /* If MSI/MSI-X is supported by the kernel we will try to enable it on
6596050f7147SStephen Cameron  * controllers that are capable. If not, we use legacy INTx mode.
6597edd16368SStephen M. Cameron  */
65986f039790SGreg Kroah-Hartman static void hpsa_interrupt_mode(struct ctlr_info *h)
6599edd16368SStephen M. Cameron {
6600edd16368SStephen M. Cameron #ifdef CONFIG_PCI_MSI
6601254f796bSMatt Gates 	int err, i;
6602254f796bSMatt Gates 	struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
6603254f796bSMatt Gates 
6604254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++) {
6605254f796bSMatt Gates 		hpsa_msix_entries[i].vector = 0;
6606254f796bSMatt Gates 		hpsa_msix_entries[i].entry = i;
6607254f796bSMatt Gates 	}
6608edd16368SStephen M. Cameron 
6609edd16368SStephen M. Cameron 	/* Some boards advertise MSI but don't really support it */
66106b3f4c52SStephen M. Cameron 	if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
66116b3f4c52SStephen M. Cameron 	    (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
6612edd16368SStephen M. Cameron 		goto default_int_mode;
661355c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
6614050f7147SStephen Cameron 		dev_info(&h->pdev->dev, "MSI-X capable controller\n");
6615eee0f03aSHannes Reinecke 		h->msix_vector = MAX_REPLY_QUEUES;
6616f89439bcSStephen M. Cameron 		if (h->msix_vector > num_online_cpus())
6617f89439bcSStephen M. Cameron 			h->msix_vector = num_online_cpus();
661818fce3c4SAlexander Gordeev 		err = pci_enable_msix_range(h->pdev, hpsa_msix_entries,
661918fce3c4SAlexander Gordeev 					    1, h->msix_vector);
662018fce3c4SAlexander Gordeev 		if (err < 0) {
662118fce3c4SAlexander Gordeev 			dev_warn(&h->pdev->dev, "MSI-X init failed %d\n", err);
662218fce3c4SAlexander Gordeev 			h->msix_vector = 0;
662318fce3c4SAlexander Gordeev 			goto single_msi_mode;
662418fce3c4SAlexander Gordeev 		} else if (err < h->msix_vector) {
662555c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
6626edd16368SStephen M. Cameron 			       "available\n", err);
6627eee0f03aSHannes Reinecke 		}
662818fce3c4SAlexander Gordeev 		h->msix_vector = err;
6629eee0f03aSHannes Reinecke 		for (i = 0; i < h->msix_vector; i++)
6630eee0f03aSHannes Reinecke 			h->intr[i] = hpsa_msix_entries[i].vector;
6631eee0f03aSHannes Reinecke 		return;
6632edd16368SStephen M. Cameron 	}
663318fce3c4SAlexander Gordeev single_msi_mode:
663455c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
6635050f7147SStephen Cameron 		dev_info(&h->pdev->dev, "MSI capable controller\n");
663655c06c71SStephen M. Cameron 		if (!pci_enable_msi(h->pdev))
6637edd16368SStephen M. Cameron 			h->msi_vector = 1;
6638edd16368SStephen M. Cameron 		else
663955c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "MSI init failed\n");
6640edd16368SStephen M. Cameron 	}
6641edd16368SStephen M. Cameron default_int_mode:
6642edd16368SStephen M. Cameron #endif				/* CONFIG_PCI_MSI */
6643edd16368SStephen M. Cameron 	/* if we get here we're going to use the default interrupt mode */
6644a9a3a273SStephen M. Cameron 	h->intr[h->intr_mode] = h->pdev->irq;
6645edd16368SStephen M. Cameron }
6646edd16368SStephen M. Cameron 
66476f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
6648e5c880d1SStephen M. Cameron {
6649e5c880d1SStephen M. Cameron 	int i;
6650e5c880d1SStephen M. Cameron 	u32 subsystem_vendor_id, subsystem_device_id;
6651e5c880d1SStephen M. Cameron 
6652e5c880d1SStephen M. Cameron 	subsystem_vendor_id = pdev->subsystem_vendor;
6653e5c880d1SStephen M. Cameron 	subsystem_device_id = pdev->subsystem_device;
6654e5c880d1SStephen M. Cameron 	*board_id = ((subsystem_device_id << 16) & 0xffff0000) |
6655e5c880d1SStephen M. Cameron 		    subsystem_vendor_id;
6656e5c880d1SStephen M. Cameron 
6657e5c880d1SStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(products); i++)
6658e5c880d1SStephen M. Cameron 		if (*board_id == products[i].board_id)
6659e5c880d1SStephen M. Cameron 			return i;
6660e5c880d1SStephen M. Cameron 
66616798cc0aSStephen M. Cameron 	if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
66626798cc0aSStephen M. Cameron 		subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
66636798cc0aSStephen M. Cameron 		!hpsa_allow_any) {
6664e5c880d1SStephen M. Cameron 		dev_warn(&pdev->dev, "unrecognized board ID: "
6665e5c880d1SStephen M. Cameron 			"0x%08x, ignoring.\n", *board_id);
6666e5c880d1SStephen M. Cameron 			return -ENODEV;
6667e5c880d1SStephen M. Cameron 	}
6668e5c880d1SStephen M. Cameron 	return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
6669e5c880d1SStephen M. Cameron }
6670e5c880d1SStephen M. Cameron 
66716f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
66723a7774ceSStephen M. Cameron 				    unsigned long *memory_bar)
66733a7774ceSStephen M. Cameron {
66743a7774ceSStephen M. Cameron 	int i;
66753a7774ceSStephen M. Cameron 
66763a7774ceSStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
667712d2cd47SStephen M. Cameron 		if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
66783a7774ceSStephen M. Cameron 			/* addressing mode bits already removed */
667912d2cd47SStephen M. Cameron 			*memory_bar = pci_resource_start(pdev, i);
668012d2cd47SStephen M. Cameron 			dev_dbg(&pdev->dev, "memory BAR = %lx\n",
66813a7774ceSStephen M. Cameron 				*memory_bar);
66823a7774ceSStephen M. Cameron 			return 0;
66833a7774ceSStephen M. Cameron 		}
668412d2cd47SStephen M. Cameron 	dev_warn(&pdev->dev, "no memory BAR found\n");
66853a7774ceSStephen M. Cameron 	return -ENODEV;
66863a7774ceSStephen M. Cameron }
66873a7774ceSStephen M. Cameron 
66886f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
66896f039790SGreg Kroah-Hartman 				     int wait_for_ready)
66902c4c8c8bSStephen M. Cameron {
6691fe5389c8SStephen M. Cameron 	int i, iterations;
66922c4c8c8bSStephen M. Cameron 	u32 scratchpad;
6693fe5389c8SStephen M. Cameron 	if (wait_for_ready)
6694fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_READY_ITERATIONS;
6695fe5389c8SStephen M. Cameron 	else
6696fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
66972c4c8c8bSStephen M. Cameron 
6698fe5389c8SStephen M. Cameron 	for (i = 0; i < iterations; i++) {
6699fe5389c8SStephen M. Cameron 		scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
6700fe5389c8SStephen M. Cameron 		if (wait_for_ready) {
67012c4c8c8bSStephen M. Cameron 			if (scratchpad == HPSA_FIRMWARE_READY)
67022c4c8c8bSStephen M. Cameron 				return 0;
6703fe5389c8SStephen M. Cameron 		} else {
6704fe5389c8SStephen M. Cameron 			if (scratchpad != HPSA_FIRMWARE_READY)
6705fe5389c8SStephen M. Cameron 				return 0;
6706fe5389c8SStephen M. Cameron 		}
67072c4c8c8bSStephen M. Cameron 		msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
67082c4c8c8bSStephen M. Cameron 	}
6709fe5389c8SStephen M. Cameron 	dev_warn(&pdev->dev, "board not ready, timed out.\n");
67102c4c8c8bSStephen M. Cameron 	return -ENODEV;
67112c4c8c8bSStephen M. Cameron }
67122c4c8c8bSStephen M. Cameron 
67136f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
67146f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
6715a51fd47fSStephen M. Cameron 			       u64 *cfg_offset)
6716a51fd47fSStephen M. Cameron {
6717a51fd47fSStephen M. Cameron 	*cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
6718a51fd47fSStephen M. Cameron 	*cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
6719a51fd47fSStephen M. Cameron 	*cfg_base_addr &= (u32) 0x0000ffff;
6720a51fd47fSStephen M. Cameron 	*cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
6721a51fd47fSStephen M. Cameron 	if (*cfg_base_addr_index == -1) {
6722a51fd47fSStephen M. Cameron 		dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
6723a51fd47fSStephen M. Cameron 		return -ENODEV;
6724a51fd47fSStephen M. Cameron 	}
6725a51fd47fSStephen M. Cameron 	return 0;
6726a51fd47fSStephen M. Cameron }
6727a51fd47fSStephen M. Cameron 
6728195f2c65SRobert Elliott static void hpsa_free_cfgtables(struct ctlr_info *h)
6729195f2c65SRobert Elliott {
6730105a3dbcSRobert Elliott 	if (h->transtable) {
6731195f2c65SRobert Elliott 		iounmap(h->transtable);
6732105a3dbcSRobert Elliott 		h->transtable = NULL;
6733105a3dbcSRobert Elliott 	}
6734105a3dbcSRobert Elliott 	if (h->cfgtable) {
6735195f2c65SRobert Elliott 		iounmap(h->cfgtable);
6736105a3dbcSRobert Elliott 		h->cfgtable = NULL;
6737105a3dbcSRobert Elliott 	}
6738195f2c65SRobert Elliott }
6739195f2c65SRobert Elliott 
6740195f2c65SRobert Elliott /* Find and map CISS config table and transfer table
6741195f2c65SRobert Elliott + * several items must be unmapped (freed) later
6742195f2c65SRobert Elliott + * */
67436f039790SGreg Kroah-Hartman static int hpsa_find_cfgtables(struct ctlr_info *h)
6744edd16368SStephen M. Cameron {
674501a02ffcSStephen M. Cameron 	u64 cfg_offset;
674601a02ffcSStephen M. Cameron 	u32 cfg_base_addr;
674701a02ffcSStephen M. Cameron 	u64 cfg_base_addr_index;
6748303932fdSDon Brace 	u32 trans_offset;
6749a51fd47fSStephen M. Cameron 	int rc;
675077c4495cSStephen M. Cameron 
6751a51fd47fSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
6752a51fd47fSStephen M. Cameron 		&cfg_base_addr_index, &cfg_offset);
6753a51fd47fSStephen M. Cameron 	if (rc)
6754a51fd47fSStephen M. Cameron 		return rc;
675577c4495cSStephen M. Cameron 	h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
6756a51fd47fSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
6757cd3c81c4SRobert Elliott 	if (!h->cfgtable) {
6758cd3c81c4SRobert Elliott 		dev_err(&h->pdev->dev, "Failed mapping cfgtable\n");
675977c4495cSStephen M. Cameron 		return -ENOMEM;
6760cd3c81c4SRobert Elliott 	}
6761580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(h->cfgtable);
6762580ada3cSStephen M. Cameron 	if (rc)
6763580ada3cSStephen M. Cameron 		return rc;
676477c4495cSStephen M. Cameron 	/* Find performant mode table. */
6765a51fd47fSStephen M. Cameron 	trans_offset = readl(&h->cfgtable->TransMethodOffset);
676677c4495cSStephen M. Cameron 	h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
676777c4495cSStephen M. Cameron 				cfg_base_addr_index)+cfg_offset+trans_offset,
676877c4495cSStephen M. Cameron 				sizeof(*h->transtable));
6769195f2c65SRobert Elliott 	if (!h->transtable) {
6770195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "Failed mapping transfer table\n");
6771195f2c65SRobert Elliott 		hpsa_free_cfgtables(h);
677277c4495cSStephen M. Cameron 		return -ENOMEM;
6773195f2c65SRobert Elliott 	}
677477c4495cSStephen M. Cameron 	return 0;
677577c4495cSStephen M. Cameron }
677677c4495cSStephen M. Cameron 
67776f039790SGreg Kroah-Hartman static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
6778cba3d38bSStephen M. Cameron {
677941ce4c35SStephen Cameron #define MIN_MAX_COMMANDS 16
678041ce4c35SStephen Cameron 	BUILD_BUG_ON(MIN_MAX_COMMANDS <= HPSA_NRESERVED_CMDS);
678141ce4c35SStephen Cameron 
678241ce4c35SStephen Cameron 	h->max_commands = readl(&h->cfgtable->MaxPerformantModeCommands);
678372ceeaecSStephen M. Cameron 
678472ceeaecSStephen M. Cameron 	/* Limit commands in memory limited kdump scenario. */
678572ceeaecSStephen M. Cameron 	if (reset_devices && h->max_commands > 32)
678672ceeaecSStephen M. Cameron 		h->max_commands = 32;
678772ceeaecSStephen M. Cameron 
678841ce4c35SStephen Cameron 	if (h->max_commands < MIN_MAX_COMMANDS) {
678941ce4c35SStephen Cameron 		dev_warn(&h->pdev->dev,
679041ce4c35SStephen Cameron 			"Controller reports max supported commands of %d Using %d instead. Ensure that firmware is up to date.\n",
679141ce4c35SStephen Cameron 			h->max_commands,
679241ce4c35SStephen Cameron 			MIN_MAX_COMMANDS);
679341ce4c35SStephen Cameron 		h->max_commands = MIN_MAX_COMMANDS;
6794cba3d38bSStephen M. Cameron 	}
6795cba3d38bSStephen M. Cameron }
6796cba3d38bSStephen M. Cameron 
6797c7ee65b3SWebb Scales /* If the controller reports that the total max sg entries is greater than 512,
6798c7ee65b3SWebb Scales  * then we know that chained SG blocks work.  (Original smart arrays did not
6799c7ee65b3SWebb Scales  * support chained SG blocks and would return zero for max sg entries.)
6800c7ee65b3SWebb Scales  */
6801c7ee65b3SWebb Scales static int hpsa_supports_chained_sg_blocks(struct ctlr_info *h)
6802c7ee65b3SWebb Scales {
6803c7ee65b3SWebb Scales 	return h->maxsgentries > 512;
6804c7ee65b3SWebb Scales }
6805c7ee65b3SWebb Scales 
6806b93d7536SStephen M. Cameron /* Interrogate the hardware for some limits:
6807b93d7536SStephen M. Cameron  * max commands, max SG elements without chaining, and with chaining,
6808b93d7536SStephen M. Cameron  * SG chain block size, etc.
6809b93d7536SStephen M. Cameron  */
68106f039790SGreg Kroah-Hartman static void hpsa_find_board_params(struct ctlr_info *h)
6811b93d7536SStephen M. Cameron {
6812cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
681345fcb86eSStephen Cameron 	h->nr_cmds = h->max_commands;
6814b93d7536SStephen M. Cameron 	h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
6815283b4a9bSStephen M. Cameron 	h->fw_support = readl(&(h->cfgtable->misc_fw_support));
6816c7ee65b3SWebb Scales 	if (hpsa_supports_chained_sg_blocks(h)) {
6817c7ee65b3SWebb Scales 		/* Limit in-command s/g elements to 32 save dma'able memory. */
6818b93d7536SStephen M. Cameron 		h->max_cmd_sg_entries = 32;
68191a63ea6fSWebb Scales 		h->chainsize = h->maxsgentries - h->max_cmd_sg_entries;
6820b93d7536SStephen M. Cameron 		h->maxsgentries--; /* save one for chain pointer */
6821b93d7536SStephen M. Cameron 	} else {
6822c7ee65b3SWebb Scales 		/*
6823c7ee65b3SWebb Scales 		 * Original smart arrays supported at most 31 s/g entries
6824c7ee65b3SWebb Scales 		 * embedded inline in the command (trying to use more
6825c7ee65b3SWebb Scales 		 * would lock up the controller)
6826c7ee65b3SWebb Scales 		 */
6827c7ee65b3SWebb Scales 		h->max_cmd_sg_entries = 31;
68281a63ea6fSWebb Scales 		h->maxsgentries = 31; /* default to traditional values */
6829c7ee65b3SWebb Scales 		h->chainsize = 0;
6830b93d7536SStephen M. Cameron 	}
683175167d2cSStephen M. Cameron 
683275167d2cSStephen M. Cameron 	/* Find out what task management functions are supported and cache */
683375167d2cSStephen M. Cameron 	h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
68340e7a7fceSScott Teel 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
68350e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
68360e7a7fceSScott Teel 	if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
68370e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
68388be986ccSStephen Cameron 	if (!(HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags))
68398be986ccSStephen Cameron 		dev_warn(&h->pdev->dev, "HP SSD Smart Path aborts not supported\n");
6840b93d7536SStephen M. Cameron }
6841b93d7536SStephen M. Cameron 
684276c46e49SStephen M. Cameron static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
684376c46e49SStephen M. Cameron {
68440fc9fd40SAkinobu Mita 	if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
6845050f7147SStephen Cameron 		dev_err(&h->pdev->dev, "not a valid CISS config table\n");
684676c46e49SStephen M. Cameron 		return false;
684776c46e49SStephen M. Cameron 	}
684876c46e49SStephen M. Cameron 	return true;
684976c46e49SStephen M. Cameron }
685076c46e49SStephen M. Cameron 
685197a5e98cSStephen M. Cameron static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
6852f7c39101SStephen M. Cameron {
685397a5e98cSStephen M. Cameron 	u32 driver_support;
6854f7c39101SStephen M. Cameron 
685597a5e98cSStephen M. Cameron 	driver_support = readl(&(h->cfgtable->driver_support));
68560b9e7b74SArnd Bergmann 	/* Need to enable prefetch in the SCSI core for 6400 in x86 */
68570b9e7b74SArnd Bergmann #ifdef CONFIG_X86
685897a5e98cSStephen M. Cameron 	driver_support |= ENABLE_SCSI_PREFETCH;
6859f7c39101SStephen M. Cameron #endif
686028e13446SStephen M. Cameron 	driver_support |= ENABLE_UNIT_ATTN;
686128e13446SStephen M. Cameron 	writel(driver_support, &(h->cfgtable->driver_support));
6862f7c39101SStephen M. Cameron }
6863f7c39101SStephen M. Cameron 
68643d0eab67SStephen M. Cameron /* Disable DMA prefetch for the P600.  Otherwise an ASIC bug may result
68653d0eab67SStephen M. Cameron  * in a prefetch beyond physical memory.
68663d0eab67SStephen M. Cameron  */
68673d0eab67SStephen M. Cameron static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
68683d0eab67SStephen M. Cameron {
68693d0eab67SStephen M. Cameron 	u32 dma_prefetch;
68703d0eab67SStephen M. Cameron 
68713d0eab67SStephen M. Cameron 	if (h->board_id != 0x3225103C)
68723d0eab67SStephen M. Cameron 		return;
68733d0eab67SStephen M. Cameron 	dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
68743d0eab67SStephen M. Cameron 	dma_prefetch |= 0x8000;
68753d0eab67SStephen M. Cameron 	writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
68763d0eab67SStephen M. Cameron }
68773d0eab67SStephen M. Cameron 
6878c706a795SRobert Elliott static int hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
687976438d08SStephen M. Cameron {
688076438d08SStephen M. Cameron 	int i;
688176438d08SStephen M. Cameron 	u32 doorbell_value;
688276438d08SStephen M. Cameron 	unsigned long flags;
688376438d08SStephen M. Cameron 	/* wait until the clear_event_notify bit 6 is cleared by controller. */
6884007e7aa9SRobert Elliott 	for (i = 0; i < MAX_CLEAR_EVENT_WAIT; i++) {
688576438d08SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
688676438d08SStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
688776438d08SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
688876438d08SStephen M. Cameron 		if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
6889c706a795SRobert Elliott 			goto done;
689076438d08SStephen M. Cameron 		/* delay and try again */
6891007e7aa9SRobert Elliott 		msleep(CLEAR_EVENT_WAIT_INTERVAL);
689276438d08SStephen M. Cameron 	}
6893c706a795SRobert Elliott 	return -ENODEV;
6894c706a795SRobert Elliott done:
6895c706a795SRobert Elliott 	return 0;
689676438d08SStephen M. Cameron }
689776438d08SStephen M. Cameron 
6898c706a795SRobert Elliott static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
6899eb6b2ae9SStephen M. Cameron {
6900eb6b2ae9SStephen M. Cameron 	int i;
69016eaf46fdSStephen M. Cameron 	u32 doorbell_value;
69026eaf46fdSStephen M. Cameron 	unsigned long flags;
6903eb6b2ae9SStephen M. Cameron 
6904eb6b2ae9SStephen M. Cameron 	/* under certain very rare conditions, this can take awhile.
6905eb6b2ae9SStephen M. Cameron 	 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
6906eb6b2ae9SStephen M. Cameron 	 * as we enter this code.)
6907eb6b2ae9SStephen M. Cameron 	 */
6908007e7aa9SRobert Elliott 	for (i = 0; i < MAX_MODE_CHANGE_WAIT; i++) {
690925163bd5SWebb Scales 		if (h->remove_in_progress)
691025163bd5SWebb Scales 			goto done;
69116eaf46fdSStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
69126eaf46fdSStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
69136eaf46fdSStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
6914382be668SDan Carpenter 		if (!(doorbell_value & CFGTBL_ChangeReq))
6915c706a795SRobert Elliott 			goto done;
6916eb6b2ae9SStephen M. Cameron 		/* delay and try again */
6917007e7aa9SRobert Elliott 		msleep(MODE_CHANGE_WAIT_INTERVAL);
6918eb6b2ae9SStephen M. Cameron 	}
6919c706a795SRobert Elliott 	return -ENODEV;
6920c706a795SRobert Elliott done:
6921c706a795SRobert Elliott 	return 0;
69223f4336f3SStephen M. Cameron }
69233f4336f3SStephen M. Cameron 
6924c706a795SRobert Elliott /* return -ENODEV or other reason on error, 0 on success */
69256f039790SGreg Kroah-Hartman static int hpsa_enter_simple_mode(struct ctlr_info *h)
69263f4336f3SStephen M. Cameron {
69273f4336f3SStephen M. Cameron 	u32 trans_support;
69283f4336f3SStephen M. Cameron 
69293f4336f3SStephen M. Cameron 	trans_support = readl(&(h->cfgtable->TransportSupport));
69303f4336f3SStephen M. Cameron 	if (!(trans_support & SIMPLE_MODE))
69313f4336f3SStephen M. Cameron 		return -ENOTSUPP;
69323f4336f3SStephen M. Cameron 
69333f4336f3SStephen M. Cameron 	h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
6934283b4a9bSStephen M. Cameron 
69353f4336f3SStephen M. Cameron 	/* Update the field, and then ring the doorbell */
69363f4336f3SStephen M. Cameron 	writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
6937b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
69383f4336f3SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
6939c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h))
6940c706a795SRobert Elliott 		goto error;
6941eb6b2ae9SStephen M. Cameron 	print_cfg_table(&h->pdev->dev, h->cfgtable);
6942283b4a9bSStephen M. Cameron 	if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
6943283b4a9bSStephen M. Cameron 		goto error;
6944960a30e7SStephen M. Cameron 	h->transMethod = CFGTBL_Trans_Simple;
6945eb6b2ae9SStephen M. Cameron 	return 0;
6946283b4a9bSStephen M. Cameron error:
6947050f7147SStephen Cameron 	dev_err(&h->pdev->dev, "failed to enter simple mode\n");
6948283b4a9bSStephen M. Cameron 	return -ENODEV;
6949eb6b2ae9SStephen M. Cameron }
6950eb6b2ae9SStephen M. Cameron 
6951195f2c65SRobert Elliott /* free items allocated or mapped by hpsa_pci_init */
6952195f2c65SRobert Elliott static void hpsa_free_pci_init(struct ctlr_info *h)
6953195f2c65SRobert Elliott {
6954195f2c65SRobert Elliott 	hpsa_free_cfgtables(h);			/* pci_init 4 */
6955195f2c65SRobert Elliott 	iounmap(h->vaddr);			/* pci_init 3 */
6956105a3dbcSRobert Elliott 	h->vaddr = NULL;
6957195f2c65SRobert Elliott 	hpsa_disable_interrupt_mode(h);		/* pci_init 2 */
6958195f2c65SRobert Elliott 	pci_release_regions(h->pdev);		/* pci_init 2 */
6959195f2c65SRobert Elliott 	pci_disable_device(h->pdev);		/* pci_init 1 */
6960195f2c65SRobert Elliott }
6961195f2c65SRobert Elliott 
6962195f2c65SRobert Elliott /* several items must be freed later */
69636f039790SGreg Kroah-Hartman static int hpsa_pci_init(struct ctlr_info *h)
696477c4495cSStephen M. Cameron {
6965eb6b2ae9SStephen M. Cameron 	int prod_index, err;
6966edd16368SStephen M. Cameron 
6967e5c880d1SStephen M. Cameron 	prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
6968e5c880d1SStephen M. Cameron 	if (prod_index < 0)
696960f923b9SRobert Elliott 		return prod_index;
6970e5c880d1SStephen M. Cameron 	h->product_name = products[prod_index].product_name;
6971e5c880d1SStephen M. Cameron 	h->access = *(products[prod_index].access);
6972e5c880d1SStephen M. Cameron 
69739b5c48c2SStephen Cameron 	h->needs_abort_tags_swizzled =
69749b5c48c2SStephen Cameron 		ctlr_needs_abort_tags_swizzled(h->board_id);
69759b5c48c2SStephen Cameron 
6976e5a44df8SMatthew Garrett 	pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
6977e5a44df8SMatthew Garrett 			       PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
6978e5a44df8SMatthew Garrett 
697955c06c71SStephen M. Cameron 	err = pci_enable_device(h->pdev);
6980edd16368SStephen M. Cameron 	if (err) {
6981195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "failed to enable PCI device\n");
6982edd16368SStephen M. Cameron 		return err;
6983edd16368SStephen M. Cameron 	}
6984edd16368SStephen M. Cameron 
6985f79cfec6SStephen M. Cameron 	err = pci_request_regions(h->pdev, HPSA);
6986edd16368SStephen M. Cameron 	if (err) {
698755c06c71SStephen M. Cameron 		dev_err(&h->pdev->dev,
6988195f2c65SRobert Elliott 			"failed to obtain PCI resources\n");
6989195f2c65SRobert Elliott 		goto clean1;	/* pci */
6990edd16368SStephen M. Cameron 	}
69914fa604e1SRobert Elliott 
69924fa604e1SRobert Elliott 	pci_set_master(h->pdev);
69934fa604e1SRobert Elliott 
69946b3f4c52SStephen M. Cameron 	hpsa_interrupt_mode(h);
699512d2cd47SStephen M. Cameron 	err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
69963a7774ceSStephen M. Cameron 	if (err)
6997195f2c65SRobert Elliott 		goto clean2;	/* intmode+region, pci */
6998edd16368SStephen M. Cameron 	h->vaddr = remap_pci_mem(h->paddr, 0x250);
6999204892e9SStephen M. Cameron 	if (!h->vaddr) {
7000195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "failed to remap PCI mem\n");
7001204892e9SStephen M. Cameron 		err = -ENOMEM;
7002195f2c65SRobert Elliott 		goto clean2;	/* intmode+region, pci */
7003204892e9SStephen M. Cameron 	}
7004fe5389c8SStephen M. Cameron 	err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
70052c4c8c8bSStephen M. Cameron 	if (err)
7006195f2c65SRobert Elliott 		goto clean3;	/* vaddr, intmode+region, pci */
700777c4495cSStephen M. Cameron 	err = hpsa_find_cfgtables(h);
700877c4495cSStephen M. Cameron 	if (err)
7009195f2c65SRobert Elliott 		goto clean3;	/* vaddr, intmode+region, pci */
7010b93d7536SStephen M. Cameron 	hpsa_find_board_params(h);
7011edd16368SStephen M. Cameron 
701276c46e49SStephen M. Cameron 	if (!hpsa_CISS_signature_present(h)) {
7013edd16368SStephen M. Cameron 		err = -ENODEV;
7014195f2c65SRobert Elliott 		goto clean4;	/* cfgtables, vaddr, intmode+region, pci */
7015edd16368SStephen M. Cameron 	}
701697a5e98cSStephen M. Cameron 	hpsa_set_driver_support_bits(h);
70173d0eab67SStephen M. Cameron 	hpsa_p600_dma_prefetch_quirk(h);
7018eb6b2ae9SStephen M. Cameron 	err = hpsa_enter_simple_mode(h);
7019eb6b2ae9SStephen M. Cameron 	if (err)
7020195f2c65SRobert Elliott 		goto clean4;	/* cfgtables, vaddr, intmode+region, pci */
7021edd16368SStephen M. Cameron 	return 0;
7022edd16368SStephen M. Cameron 
7023195f2c65SRobert Elliott clean4:	/* cfgtables, vaddr, intmode+region, pci */
7024195f2c65SRobert Elliott 	hpsa_free_cfgtables(h);
7025195f2c65SRobert Elliott clean3:	/* vaddr, intmode+region, pci */
7026204892e9SStephen M. Cameron 	iounmap(h->vaddr);
7027105a3dbcSRobert Elliott 	h->vaddr = NULL;
7028195f2c65SRobert Elliott clean2:	/* intmode+region, pci */
7029195f2c65SRobert Elliott 	hpsa_disable_interrupt_mode(h);
703055c06c71SStephen M. Cameron 	pci_release_regions(h->pdev);
7031195f2c65SRobert Elliott clean1:	/* pci */
7032195f2c65SRobert Elliott 	pci_disable_device(h->pdev);
7033edd16368SStephen M. Cameron 	return err;
7034edd16368SStephen M. Cameron }
7035edd16368SStephen M. Cameron 
70366f039790SGreg Kroah-Hartman static void hpsa_hba_inquiry(struct ctlr_info *h)
7037339b2b14SStephen M. Cameron {
7038339b2b14SStephen M. Cameron 	int rc;
7039339b2b14SStephen M. Cameron 
7040339b2b14SStephen M. Cameron #define HBA_INQUIRY_BYTE_COUNT 64
7041339b2b14SStephen M. Cameron 	h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
7042339b2b14SStephen M. Cameron 	if (!h->hba_inquiry_data)
7043339b2b14SStephen M. Cameron 		return;
7044339b2b14SStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
7045339b2b14SStephen M. Cameron 		h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
7046339b2b14SStephen M. Cameron 	if (rc != 0) {
7047339b2b14SStephen M. Cameron 		kfree(h->hba_inquiry_data);
7048339b2b14SStephen M. Cameron 		h->hba_inquiry_data = NULL;
7049339b2b14SStephen M. Cameron 	}
7050339b2b14SStephen M. Cameron }
7051339b2b14SStephen M. Cameron 
70526b6c1cd7STomas Henzl static int hpsa_init_reset_devices(struct pci_dev *pdev, u32 board_id)
7053edd16368SStephen M. Cameron {
70541df8552aSStephen M. Cameron 	int rc, i;
70553b747298STomas Henzl 	void __iomem *vaddr;
7056edd16368SStephen M. Cameron 
70574c2a8c40SStephen M. Cameron 	if (!reset_devices)
70584c2a8c40SStephen M. Cameron 		return 0;
70594c2a8c40SStephen M. Cameron 
7060132aa220STomas Henzl 	/* kdump kernel is loading, we don't know in which state is
7061132aa220STomas Henzl 	 * the pci interface. The dev->enable_cnt is equal zero
7062132aa220STomas Henzl 	 * so we call enable+disable, wait a while and switch it on.
7063132aa220STomas Henzl 	 */
7064132aa220STomas Henzl 	rc = pci_enable_device(pdev);
7065132aa220STomas Henzl 	if (rc) {
7066132aa220STomas Henzl 		dev_warn(&pdev->dev, "Failed to enable PCI device\n");
7067132aa220STomas Henzl 		return -ENODEV;
7068132aa220STomas Henzl 	}
7069132aa220STomas Henzl 	pci_disable_device(pdev);
7070132aa220STomas Henzl 	msleep(260);			/* a randomly chosen number */
7071132aa220STomas Henzl 	rc = pci_enable_device(pdev);
7072132aa220STomas Henzl 	if (rc) {
7073132aa220STomas Henzl 		dev_warn(&pdev->dev, "failed to enable device.\n");
7074132aa220STomas Henzl 		return -ENODEV;
7075132aa220STomas Henzl 	}
70764fa604e1SRobert Elliott 
7077859c75abSTomas Henzl 	pci_set_master(pdev);
70784fa604e1SRobert Elliott 
70793b747298STomas Henzl 	vaddr = pci_ioremap_bar(pdev, 0);
70803b747298STomas Henzl 	if (vaddr == NULL) {
70813b747298STomas Henzl 		rc = -ENOMEM;
70823b747298STomas Henzl 		goto out_disable;
70833b747298STomas Henzl 	}
70843b747298STomas Henzl 	writel(SA5_INTR_OFF, vaddr + SA5_REPLY_INTR_MASK_OFFSET);
70853b747298STomas Henzl 	iounmap(vaddr);
70863b747298STomas Henzl 
70871df8552aSStephen M. Cameron 	/* Reset the controller with a PCI power-cycle or via doorbell */
70886b6c1cd7STomas Henzl 	rc = hpsa_kdump_hard_reset_controller(pdev, board_id);
7089edd16368SStephen M. Cameron 
70901df8552aSStephen M. Cameron 	/* -ENOTSUPP here means we cannot reset the controller
70911df8552aSStephen M. Cameron 	 * but it's already (and still) up and running in
709218867659SStephen M. Cameron 	 * "performant mode".  Or, it might be 640x, which can't reset
709318867659SStephen M. Cameron 	 * due to concerns about shared bbwc between 6402/6404 pair.
70941df8552aSStephen M. Cameron 	 */
7095adf1b3a3SRobert Elliott 	if (rc)
7096132aa220STomas Henzl 		goto out_disable;
7097edd16368SStephen M. Cameron 
7098edd16368SStephen M. Cameron 	/* Now try to get the controller to respond to a no-op */
70991ba66c9cSRobert Elliott 	dev_info(&pdev->dev, "Waiting for controller to respond to no-op\n");
7100edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
7101edd16368SStephen M. Cameron 		if (hpsa_noop(pdev) == 0)
7102edd16368SStephen M. Cameron 			break;
7103edd16368SStephen M. Cameron 		else
7104edd16368SStephen M. Cameron 			dev_warn(&pdev->dev, "no-op failed%s\n",
7105edd16368SStephen M. Cameron 					(i < 11 ? "; re-trying" : ""));
7106edd16368SStephen M. Cameron 	}
7107132aa220STomas Henzl 
7108132aa220STomas Henzl out_disable:
7109132aa220STomas Henzl 
7110132aa220STomas Henzl 	pci_disable_device(pdev);
7111132aa220STomas Henzl 	return rc;
7112edd16368SStephen M. Cameron }
7113edd16368SStephen M. Cameron 
71141fb7c98aSRobert Elliott static void hpsa_free_cmd_pool(struct ctlr_info *h)
71151fb7c98aSRobert Elliott {
71161fb7c98aSRobert Elliott 	kfree(h->cmd_pool_bits);
7117105a3dbcSRobert Elliott 	h->cmd_pool_bits = NULL;
7118105a3dbcSRobert Elliott 	if (h->cmd_pool) {
71191fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
71201fb7c98aSRobert Elliott 				h->nr_cmds * sizeof(struct CommandList),
71211fb7c98aSRobert Elliott 				h->cmd_pool,
71221fb7c98aSRobert Elliott 				h->cmd_pool_dhandle);
7123105a3dbcSRobert Elliott 		h->cmd_pool = NULL;
7124105a3dbcSRobert Elliott 		h->cmd_pool_dhandle = 0;
7125105a3dbcSRobert Elliott 	}
7126105a3dbcSRobert Elliott 	if (h->errinfo_pool) {
71271fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
71281fb7c98aSRobert Elliott 				h->nr_cmds * sizeof(struct ErrorInfo),
71291fb7c98aSRobert Elliott 				h->errinfo_pool,
71301fb7c98aSRobert Elliott 				h->errinfo_pool_dhandle);
7131105a3dbcSRobert Elliott 		h->errinfo_pool = NULL;
7132105a3dbcSRobert Elliott 		h->errinfo_pool_dhandle = 0;
7133105a3dbcSRobert Elliott 	}
71341fb7c98aSRobert Elliott }
71351fb7c98aSRobert Elliott 
7136d37ffbe4SRobert Elliott static int hpsa_alloc_cmd_pool(struct ctlr_info *h)
71372e9d1b36SStephen M. Cameron {
71382e9d1b36SStephen M. Cameron 	h->cmd_pool_bits = kzalloc(
71392e9d1b36SStephen M. Cameron 		DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
71402e9d1b36SStephen M. Cameron 		sizeof(unsigned long), GFP_KERNEL);
71412e9d1b36SStephen M. Cameron 	h->cmd_pool = pci_alloc_consistent(h->pdev,
71422e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->cmd_pool),
71432e9d1b36SStephen M. Cameron 		    &(h->cmd_pool_dhandle));
71442e9d1b36SStephen M. Cameron 	h->errinfo_pool = pci_alloc_consistent(h->pdev,
71452e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->errinfo_pool),
71462e9d1b36SStephen M. Cameron 		    &(h->errinfo_pool_dhandle));
71472e9d1b36SStephen M. Cameron 	if ((h->cmd_pool_bits == NULL)
71482e9d1b36SStephen M. Cameron 	    || (h->cmd_pool == NULL)
71492e9d1b36SStephen M. Cameron 	    || (h->errinfo_pool == NULL)) {
71502e9d1b36SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory in %s", __func__);
71512c143342SRobert Elliott 		goto clean_up;
71522e9d1b36SStephen M. Cameron 	}
7153360c73bdSStephen Cameron 	hpsa_preinitialize_commands(h);
71542e9d1b36SStephen M. Cameron 	return 0;
71552c143342SRobert Elliott clean_up:
71562c143342SRobert Elliott 	hpsa_free_cmd_pool(h);
71572c143342SRobert Elliott 	return -ENOMEM;
71582e9d1b36SStephen M. Cameron }
71592e9d1b36SStephen M. Cameron 
716041b3cf08SStephen M. Cameron static void hpsa_irq_affinity_hints(struct ctlr_info *h)
716141b3cf08SStephen M. Cameron {
7162ec429952SFabian Frederick 	int i, cpu;
716341b3cf08SStephen M. Cameron 
716441b3cf08SStephen M. Cameron 	cpu = cpumask_first(cpu_online_mask);
716541b3cf08SStephen M. Cameron 	for (i = 0; i < h->msix_vector; i++) {
7166ec429952SFabian Frederick 		irq_set_affinity_hint(h->intr[i], get_cpu_mask(cpu));
716741b3cf08SStephen M. Cameron 		cpu = cpumask_next(cpu, cpu_online_mask);
716841b3cf08SStephen M. Cameron 	}
716941b3cf08SStephen M. Cameron }
717041b3cf08SStephen M. Cameron 
7171ec501a18SRobert Elliott /* clear affinity hints and free MSI-X, MSI, or legacy INTx vectors */
7172ec501a18SRobert Elliott static void hpsa_free_irqs(struct ctlr_info *h)
7173ec501a18SRobert Elliott {
7174ec501a18SRobert Elliott 	int i;
7175ec501a18SRobert Elliott 
7176ec501a18SRobert Elliott 	if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
7177ec501a18SRobert Elliott 		/* Single reply queue, only one irq to free */
7178ec501a18SRobert Elliott 		i = h->intr_mode;
7179ec501a18SRobert Elliott 		irq_set_affinity_hint(h->intr[i], NULL);
7180ec501a18SRobert Elliott 		free_irq(h->intr[i], &h->q[i]);
7181105a3dbcSRobert Elliott 		h->q[i] = 0;
7182ec501a18SRobert Elliott 		return;
7183ec501a18SRobert Elliott 	}
7184ec501a18SRobert Elliott 
7185ec501a18SRobert Elliott 	for (i = 0; i < h->msix_vector; i++) {
7186ec501a18SRobert Elliott 		irq_set_affinity_hint(h->intr[i], NULL);
7187ec501a18SRobert Elliott 		free_irq(h->intr[i], &h->q[i]);
7188105a3dbcSRobert Elliott 		h->q[i] = 0;
7189ec501a18SRobert Elliott 	}
7190a4e17fc1SRobert Elliott 	for (; i < MAX_REPLY_QUEUES; i++)
7191a4e17fc1SRobert Elliott 		h->q[i] = 0;
7192ec501a18SRobert Elliott }
7193ec501a18SRobert Elliott 
71949ee61794SRobert Elliott /* returns 0 on success; cleans up and returns -Enn on error */
71959ee61794SRobert Elliott static int hpsa_request_irqs(struct ctlr_info *h,
71960ae01a32SStephen M. Cameron 	irqreturn_t (*msixhandler)(int, void *),
71970ae01a32SStephen M. Cameron 	irqreturn_t (*intxhandler)(int, void *))
71980ae01a32SStephen M. Cameron {
7199254f796bSMatt Gates 	int rc, i;
72000ae01a32SStephen M. Cameron 
7201254f796bSMatt Gates 	/*
7202254f796bSMatt Gates 	 * initialize h->q[x] = x so that interrupt handlers know which
7203254f796bSMatt Gates 	 * queue to process.
7204254f796bSMatt Gates 	 */
7205254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++)
7206254f796bSMatt Gates 		h->q[i] = (u8) i;
7207254f796bSMatt Gates 
7208eee0f03aSHannes Reinecke 	if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
7209254f796bSMatt Gates 		/* If performant mode and MSI-X, use multiple reply queues */
7210a4e17fc1SRobert Elliott 		for (i = 0; i < h->msix_vector; i++) {
7211254f796bSMatt Gates 			rc = request_irq(h->intr[i], msixhandler,
7212254f796bSMatt Gates 					0, h->devname,
7213254f796bSMatt Gates 					&h->q[i]);
7214a4e17fc1SRobert Elliott 			if (rc) {
7215a4e17fc1SRobert Elliott 				int j;
7216a4e17fc1SRobert Elliott 
7217a4e17fc1SRobert Elliott 				dev_err(&h->pdev->dev,
7218a4e17fc1SRobert Elliott 					"failed to get irq %d for %s\n",
7219a4e17fc1SRobert Elliott 				       h->intr[i], h->devname);
7220a4e17fc1SRobert Elliott 				for (j = 0; j < i; j++) {
7221a4e17fc1SRobert Elliott 					free_irq(h->intr[j], &h->q[j]);
7222a4e17fc1SRobert Elliott 					h->q[j] = 0;
7223a4e17fc1SRobert Elliott 				}
7224a4e17fc1SRobert Elliott 				for (; j < MAX_REPLY_QUEUES; j++)
7225a4e17fc1SRobert Elliott 					h->q[j] = 0;
7226a4e17fc1SRobert Elliott 				return rc;
7227a4e17fc1SRobert Elliott 			}
7228a4e17fc1SRobert Elliott 		}
722941b3cf08SStephen M. Cameron 		hpsa_irq_affinity_hints(h);
7230254f796bSMatt Gates 	} else {
7231254f796bSMatt Gates 		/* Use single reply pool */
7232eee0f03aSHannes Reinecke 		if (h->msix_vector > 0 || h->msi_vector) {
7233254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
7234254f796bSMatt Gates 				msixhandler, 0, h->devname,
7235254f796bSMatt Gates 				&h->q[h->intr_mode]);
7236254f796bSMatt Gates 		} else {
7237254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
7238254f796bSMatt Gates 				intxhandler, IRQF_SHARED, h->devname,
7239254f796bSMatt Gates 				&h->q[h->intr_mode]);
7240254f796bSMatt Gates 		}
7241105a3dbcSRobert Elliott 		irq_set_affinity_hint(h->intr[h->intr_mode], NULL);
7242254f796bSMatt Gates 	}
72430ae01a32SStephen M. Cameron 	if (rc) {
7244195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "failed to get irq %d for %s\n",
72450ae01a32SStephen M. Cameron 		       h->intr[h->intr_mode], h->devname);
7246195f2c65SRobert Elliott 		hpsa_free_irqs(h);
72470ae01a32SStephen M. Cameron 		return -ENODEV;
72480ae01a32SStephen M. Cameron 	}
72490ae01a32SStephen M. Cameron 	return 0;
72500ae01a32SStephen M. Cameron }
72510ae01a32SStephen M. Cameron 
72526f039790SGreg Kroah-Hartman static int hpsa_kdump_soft_reset(struct ctlr_info *h)
725364670ac8SStephen M. Cameron {
7254bf43caf3SRobert Elliott 	hpsa_send_host_reset(h, RAID_CTLR_LUNID, HPSA_RESET_TYPE_CONTROLLER);
725564670ac8SStephen M. Cameron 
725664670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
725764670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) {
725864670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
725964670ac8SStephen M. Cameron 		return -1;
726064670ac8SStephen M. Cameron 	}
726164670ac8SStephen M. Cameron 
726264670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
726364670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) {
726464670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Board failed to become ready "
726564670ac8SStephen M. Cameron 			"after soft reset.\n");
726664670ac8SStephen M. Cameron 		return -1;
726764670ac8SStephen M. Cameron 	}
726864670ac8SStephen M. Cameron 
726964670ac8SStephen M. Cameron 	return 0;
727064670ac8SStephen M. Cameron }
727164670ac8SStephen M. Cameron 
7272072b0518SStephen M. Cameron static void hpsa_free_reply_queues(struct ctlr_info *h)
7273072b0518SStephen M. Cameron {
7274072b0518SStephen M. Cameron 	int i;
7275072b0518SStephen M. Cameron 
7276072b0518SStephen M. Cameron 	for (i = 0; i < h->nreply_queues; i++) {
7277072b0518SStephen M. Cameron 		if (!h->reply_queue[i].head)
7278072b0518SStephen M. Cameron 			continue;
72791fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
72801fb7c98aSRobert Elliott 					h->reply_queue_size,
72811fb7c98aSRobert Elliott 					h->reply_queue[i].head,
72821fb7c98aSRobert Elliott 					h->reply_queue[i].busaddr);
7283072b0518SStephen M. Cameron 		h->reply_queue[i].head = NULL;
7284072b0518SStephen M. Cameron 		h->reply_queue[i].busaddr = 0;
7285072b0518SStephen M. Cameron 	}
7286105a3dbcSRobert Elliott 	h->reply_queue_size = 0;
7287072b0518SStephen M. Cameron }
7288072b0518SStephen M. Cameron 
72890097f0f4SStephen M. Cameron static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
72900097f0f4SStephen M. Cameron {
7291105a3dbcSRobert Elliott 	hpsa_free_performant_mode(h);		/* init_one 7 */
7292105a3dbcSRobert Elliott 	hpsa_free_sg_chain_blocks(h);		/* init_one 6 */
7293105a3dbcSRobert Elliott 	hpsa_free_cmd_pool(h);			/* init_one 5 */
7294105a3dbcSRobert Elliott 	hpsa_free_irqs(h);			/* init_one 4 */
7295105a3dbcSRobert Elliott 	hpsa_free_pci_init(h);			/* init_one 3 */
7296105a3dbcSRobert Elliott 	kfree(h);				/* init_one 1 */
729764670ac8SStephen M. Cameron }
729864670ac8SStephen M. Cameron 
7299a0c12413SStephen M. Cameron /* Called when controller lockup detected. */
7300f2405db8SDon Brace static void fail_all_outstanding_cmds(struct ctlr_info *h)
7301a0c12413SStephen M. Cameron {
7302281a7fd0SWebb Scales 	int i, refcount;
7303281a7fd0SWebb Scales 	struct CommandList *c;
730425163bd5SWebb Scales 	int failcount = 0;
7305a0c12413SStephen M. Cameron 
7306080ef1ccSDon Brace 	flush_workqueue(h->resubmit_wq); /* ensure all cmds are fully built */
7307f2405db8SDon Brace 	for (i = 0; i < h->nr_cmds; i++) {
7308f2405db8SDon Brace 		c = h->cmd_pool + i;
7309281a7fd0SWebb Scales 		refcount = atomic_inc_return(&c->refcount);
7310281a7fd0SWebb Scales 		if (refcount > 1) {
731125163bd5SWebb Scales 			c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
73125a3d16f5SStephen M. Cameron 			finish_cmd(c);
7313433b5f4dSStephen Cameron 			atomic_dec(&h->commands_outstanding);
731425163bd5SWebb Scales 			failcount++;
7315a0c12413SStephen M. Cameron 		}
7316281a7fd0SWebb Scales 		cmd_free(h, c);
7317281a7fd0SWebb Scales 	}
731825163bd5SWebb Scales 	dev_warn(&h->pdev->dev,
731925163bd5SWebb Scales 		"failed %d commands in fail_all\n", failcount);
7320a0c12413SStephen M. Cameron }
7321a0c12413SStephen M. Cameron 
7322094963daSStephen M. Cameron static void set_lockup_detected_for_all_cpus(struct ctlr_info *h, u32 value)
7323094963daSStephen M. Cameron {
7324c8ed0010SRusty Russell 	int cpu;
7325094963daSStephen M. Cameron 
7326c8ed0010SRusty Russell 	for_each_online_cpu(cpu) {
7327094963daSStephen M. Cameron 		u32 *lockup_detected;
7328094963daSStephen M. Cameron 		lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
7329094963daSStephen M. Cameron 		*lockup_detected = value;
7330094963daSStephen M. Cameron 	}
7331094963daSStephen M. Cameron 	wmb(); /* be sure the per-cpu variables are out to memory */
7332094963daSStephen M. Cameron }
7333094963daSStephen M. Cameron 
7334a0c12413SStephen M. Cameron static void controller_lockup_detected(struct ctlr_info *h)
7335a0c12413SStephen M. Cameron {
7336a0c12413SStephen M. Cameron 	unsigned long flags;
7337094963daSStephen M. Cameron 	u32 lockup_detected;
7338a0c12413SStephen M. Cameron 
7339a0c12413SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
7340a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
7341094963daSStephen M. Cameron 	lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
7342094963daSStephen M. Cameron 	if (!lockup_detected) {
7343094963daSStephen M. Cameron 		/* no heartbeat, but controller gave us a zero. */
7344094963daSStephen M. Cameron 		dev_warn(&h->pdev->dev,
734525163bd5SWebb Scales 			"lockup detected after %d but scratchpad register is zero\n",
734625163bd5SWebb Scales 			h->heartbeat_sample_interval / HZ);
7347094963daSStephen M. Cameron 		lockup_detected = 0xffffffff;
7348094963daSStephen M. Cameron 	}
7349094963daSStephen M. Cameron 	set_lockup_detected_for_all_cpus(h, lockup_detected);
7350a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
735125163bd5SWebb Scales 	dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x after %d\n",
735225163bd5SWebb Scales 			lockup_detected, h->heartbeat_sample_interval / HZ);
7353a0c12413SStephen M. Cameron 	pci_disable_device(h->pdev);
7354f2405db8SDon Brace 	fail_all_outstanding_cmds(h);
7355a0c12413SStephen M. Cameron }
7356a0c12413SStephen M. Cameron 
735725163bd5SWebb Scales static int detect_controller_lockup(struct ctlr_info *h)
7358a0c12413SStephen M. Cameron {
7359a0c12413SStephen M. Cameron 	u64 now;
7360a0c12413SStephen M. Cameron 	u32 heartbeat;
7361a0c12413SStephen M. Cameron 	unsigned long flags;
7362a0c12413SStephen M. Cameron 
7363a0c12413SStephen M. Cameron 	now = get_jiffies_64();
7364a0c12413SStephen M. Cameron 	/* If we've received an interrupt recently, we're ok. */
7365a0c12413SStephen M. Cameron 	if (time_after64(h->last_intr_timestamp +
7366e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
736725163bd5SWebb Scales 		return false;
7368a0c12413SStephen M. Cameron 
7369a0c12413SStephen M. Cameron 	/*
7370a0c12413SStephen M. Cameron 	 * If we've already checked the heartbeat recently, we're ok.
7371a0c12413SStephen M. Cameron 	 * This could happen if someone sends us a signal. We
7372a0c12413SStephen M. Cameron 	 * otherwise don't care about signals in this thread.
7373a0c12413SStephen M. Cameron 	 */
7374a0c12413SStephen M. Cameron 	if (time_after64(h->last_heartbeat_timestamp +
7375e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
737625163bd5SWebb Scales 		return false;
7377a0c12413SStephen M. Cameron 
7378a0c12413SStephen M. Cameron 	/* If heartbeat has not changed since we last looked, we're not ok. */
7379a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
7380a0c12413SStephen M. Cameron 	heartbeat = readl(&h->cfgtable->HeartBeat);
7381a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
7382a0c12413SStephen M. Cameron 	if (h->last_heartbeat == heartbeat) {
7383a0c12413SStephen M. Cameron 		controller_lockup_detected(h);
738425163bd5SWebb Scales 		return true;
7385a0c12413SStephen M. Cameron 	}
7386a0c12413SStephen M. Cameron 
7387a0c12413SStephen M. Cameron 	/* We're ok. */
7388a0c12413SStephen M. Cameron 	h->last_heartbeat = heartbeat;
7389a0c12413SStephen M. Cameron 	h->last_heartbeat_timestamp = now;
739025163bd5SWebb Scales 	return false;
7391a0c12413SStephen M. Cameron }
7392a0c12413SStephen M. Cameron 
73939846590eSStephen M. Cameron static void hpsa_ack_ctlr_events(struct ctlr_info *h)
739476438d08SStephen M. Cameron {
739576438d08SStephen M. Cameron 	int i;
739676438d08SStephen M. Cameron 	char *event_type;
739776438d08SStephen M. Cameron 
7398e4aa3e6aSStephen Cameron 	if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
7399e4aa3e6aSStephen Cameron 		return;
7400e4aa3e6aSStephen Cameron 
740176438d08SStephen M. Cameron 	/* Ask the controller to clear the events we're handling. */
74021f7cee8cSStephen M. Cameron 	if ((h->transMethod & (CFGTBL_Trans_io_accel1
74031f7cee8cSStephen M. Cameron 			| CFGTBL_Trans_io_accel2)) &&
740476438d08SStephen M. Cameron 		(h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
740576438d08SStephen M. Cameron 		 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
740676438d08SStephen M. Cameron 
740776438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
740876438d08SStephen M. Cameron 			event_type = "state change";
740976438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
741076438d08SStephen M. Cameron 			event_type = "configuration change";
741176438d08SStephen M. Cameron 		/* Stop sending new RAID offload reqs via the IO accelerator */
741276438d08SStephen M. Cameron 		scsi_block_requests(h->scsi_host);
741376438d08SStephen M. Cameron 		for (i = 0; i < h->ndevices; i++)
741476438d08SStephen M. Cameron 			h->dev[i]->offload_enabled = 0;
741523100dd9SStephen M. Cameron 		hpsa_drain_accel_commands(h);
741676438d08SStephen M. Cameron 		/* Set 'accelerator path config change' bit */
741776438d08SStephen M. Cameron 		dev_warn(&h->pdev->dev,
741876438d08SStephen M. Cameron 			"Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
741976438d08SStephen M. Cameron 			h->events, event_type);
742076438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
742176438d08SStephen M. Cameron 		/* Set the "clear event notify field update" bit 6 */
742276438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
742376438d08SStephen M. Cameron 		/* Wait until ctlr clears 'clear event notify field', bit 6 */
742476438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
742576438d08SStephen M. Cameron 		scsi_unblock_requests(h->scsi_host);
742676438d08SStephen M. Cameron 	} else {
742776438d08SStephen M. Cameron 		/* Acknowledge controller notification events. */
742876438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
742976438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
743076438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
743176438d08SStephen M. Cameron #if 0
743276438d08SStephen M. Cameron 		writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
743376438d08SStephen M. Cameron 		hpsa_wait_for_mode_change_ack(h);
743476438d08SStephen M. Cameron #endif
743576438d08SStephen M. Cameron 	}
74369846590eSStephen M. Cameron 	return;
743776438d08SStephen M. Cameron }
743876438d08SStephen M. Cameron 
743976438d08SStephen M. Cameron /* Check a register on the controller to see if there are configuration
744076438d08SStephen M. Cameron  * changes (added/changed/removed logical drives, etc.) which mean that
7441e863d68eSScott Teel  * we should rescan the controller for devices.
7442e863d68eSScott Teel  * Also check flag for driver-initiated rescan.
744376438d08SStephen M. Cameron  */
74449846590eSStephen M. Cameron static int hpsa_ctlr_needs_rescan(struct ctlr_info *h)
744576438d08SStephen M. Cameron {
744676438d08SStephen M. Cameron 	if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
74479846590eSStephen M. Cameron 		return 0;
744876438d08SStephen M. Cameron 
744976438d08SStephen M. Cameron 	h->events = readl(&(h->cfgtable->event_notify));
74509846590eSStephen M. Cameron 	return h->events & RESCAN_REQUIRED_EVENT_BITS;
74519846590eSStephen M. Cameron }
745276438d08SStephen M. Cameron 
745376438d08SStephen M. Cameron /*
74549846590eSStephen M. Cameron  * Check if any of the offline devices have become ready
745576438d08SStephen M. Cameron  */
74569846590eSStephen M. Cameron static int hpsa_offline_devices_ready(struct ctlr_info *h)
74579846590eSStephen M. Cameron {
74589846590eSStephen M. Cameron 	unsigned long flags;
74599846590eSStephen M. Cameron 	struct offline_device_entry *d;
74609846590eSStephen M. Cameron 	struct list_head *this, *tmp;
74619846590eSStephen M. Cameron 
74629846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
74639846590eSStephen M. Cameron 	list_for_each_safe(this, tmp, &h->offline_device_list) {
74649846590eSStephen M. Cameron 		d = list_entry(this, struct offline_device_entry,
74659846590eSStephen M. Cameron 				offline_list);
74669846590eSStephen M. Cameron 		spin_unlock_irqrestore(&h->offline_device_lock, flags);
7467d1fea47cSStephen M. Cameron 		if (!hpsa_volume_offline(h, d->scsi3addr)) {
7468d1fea47cSStephen M. Cameron 			spin_lock_irqsave(&h->offline_device_lock, flags);
7469d1fea47cSStephen M. Cameron 			list_del(&d->offline_list);
7470d1fea47cSStephen M. Cameron 			spin_unlock_irqrestore(&h->offline_device_lock, flags);
74719846590eSStephen M. Cameron 			return 1;
7472d1fea47cSStephen M. Cameron 		}
74739846590eSStephen M. Cameron 		spin_lock_irqsave(&h->offline_device_lock, flags);
747476438d08SStephen M. Cameron 	}
74759846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
74769846590eSStephen M. Cameron 	return 0;
74779846590eSStephen M. Cameron }
74789846590eSStephen M. Cameron 
74796636e7f4SDon Brace static void hpsa_rescan_ctlr_worker(struct work_struct *work)
7480a0c12413SStephen M. Cameron {
7481a0c12413SStephen M. Cameron 	unsigned long flags;
74828a98db73SStephen M. Cameron 	struct ctlr_info *h = container_of(to_delayed_work(work),
74836636e7f4SDon Brace 					struct ctlr_info, rescan_ctlr_work);
74846636e7f4SDon Brace 
74856636e7f4SDon Brace 
74866636e7f4SDon Brace 	if (h->remove_in_progress)
74878a98db73SStephen M. Cameron 		return;
74889846590eSStephen M. Cameron 
74899846590eSStephen M. Cameron 	if (hpsa_ctlr_needs_rescan(h) || hpsa_offline_devices_ready(h)) {
74909846590eSStephen M. Cameron 		scsi_host_get(h->scsi_host);
74919846590eSStephen M. Cameron 		hpsa_ack_ctlr_events(h);
74929846590eSStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
74939846590eSStephen M. Cameron 		scsi_host_put(h->scsi_host);
74949846590eSStephen M. Cameron 	}
74956636e7f4SDon Brace 	spin_lock_irqsave(&h->lock, flags);
74966636e7f4SDon Brace 	if (!h->remove_in_progress)
74976636e7f4SDon Brace 		queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
74986636e7f4SDon Brace 				h->heartbeat_sample_interval);
74996636e7f4SDon Brace 	spin_unlock_irqrestore(&h->lock, flags);
75006636e7f4SDon Brace }
75016636e7f4SDon Brace 
75026636e7f4SDon Brace static void hpsa_monitor_ctlr_worker(struct work_struct *work)
75036636e7f4SDon Brace {
75046636e7f4SDon Brace 	unsigned long flags;
75056636e7f4SDon Brace 	struct ctlr_info *h = container_of(to_delayed_work(work),
75066636e7f4SDon Brace 					struct ctlr_info, monitor_ctlr_work);
75076636e7f4SDon Brace 
75086636e7f4SDon Brace 	detect_controller_lockup(h);
75096636e7f4SDon Brace 	if (lockup_detected(h))
75106636e7f4SDon Brace 		return;
75119846590eSStephen M. Cameron 
75128a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
75136636e7f4SDon Brace 	if (!h->remove_in_progress)
75148a98db73SStephen M. Cameron 		schedule_delayed_work(&h->monitor_ctlr_work,
75158a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
75168a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
7517a0c12413SStephen M. Cameron }
7518a0c12413SStephen M. Cameron 
75196636e7f4SDon Brace static struct workqueue_struct *hpsa_create_controller_wq(struct ctlr_info *h,
75206636e7f4SDon Brace 						char *name)
75216636e7f4SDon Brace {
75226636e7f4SDon Brace 	struct workqueue_struct *wq = NULL;
75236636e7f4SDon Brace 
7524397ea9cbSDon Brace 	wq = alloc_ordered_workqueue("%s_%d_hpsa", 0, name, h->ctlr);
75256636e7f4SDon Brace 	if (!wq)
75266636e7f4SDon Brace 		dev_err(&h->pdev->dev, "failed to create %s workqueue\n", name);
75276636e7f4SDon Brace 
75286636e7f4SDon Brace 	return wq;
75296636e7f4SDon Brace }
75306636e7f4SDon Brace 
75316f039790SGreg Kroah-Hartman static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
75324c2a8c40SStephen M. Cameron {
75334c2a8c40SStephen M. Cameron 	int dac, rc;
75344c2a8c40SStephen M. Cameron 	struct ctlr_info *h;
753564670ac8SStephen M. Cameron 	int try_soft_reset = 0;
753664670ac8SStephen M. Cameron 	unsigned long flags;
75376b6c1cd7STomas Henzl 	u32 board_id;
75384c2a8c40SStephen M. Cameron 
75394c2a8c40SStephen M. Cameron 	if (number_of_controllers == 0)
75404c2a8c40SStephen M. Cameron 		printk(KERN_INFO DRIVER_NAME "\n");
75414c2a8c40SStephen M. Cameron 
75426b6c1cd7STomas Henzl 	rc = hpsa_lookup_board_id(pdev, &board_id);
75436b6c1cd7STomas Henzl 	if (rc < 0) {
75446b6c1cd7STomas Henzl 		dev_warn(&pdev->dev, "Board ID not found\n");
75456b6c1cd7STomas Henzl 		return rc;
75466b6c1cd7STomas Henzl 	}
75476b6c1cd7STomas Henzl 
75486b6c1cd7STomas Henzl 	rc = hpsa_init_reset_devices(pdev, board_id);
754964670ac8SStephen M. Cameron 	if (rc) {
755064670ac8SStephen M. Cameron 		if (rc != -ENOTSUPP)
75514c2a8c40SStephen M. Cameron 			return rc;
755264670ac8SStephen M. Cameron 		/* If the reset fails in a particular way (it has no way to do
755364670ac8SStephen M. Cameron 		 * a proper hard reset, so returns -ENOTSUPP) we can try to do
755464670ac8SStephen M. Cameron 		 * a soft reset once we get the controller configured up to the
755564670ac8SStephen M. Cameron 		 * point that it can accept a command.
755664670ac8SStephen M. Cameron 		 */
755764670ac8SStephen M. Cameron 		try_soft_reset = 1;
755864670ac8SStephen M. Cameron 		rc = 0;
755964670ac8SStephen M. Cameron 	}
756064670ac8SStephen M. Cameron 
756164670ac8SStephen M. Cameron reinit_after_soft_reset:
75624c2a8c40SStephen M. Cameron 
7563303932fdSDon Brace 	/* Command structures must be aligned on a 32-byte boundary because
7564303932fdSDon Brace 	 * the 5 lower bits of the address are used by the hardware. and by
7565303932fdSDon Brace 	 * the driver.  See comments in hpsa.h for more info.
7566303932fdSDon Brace 	 */
7567303932fdSDon Brace 	BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
7568edd16368SStephen M. Cameron 	h = kzalloc(sizeof(*h), GFP_KERNEL);
7569105a3dbcSRobert Elliott 	if (!h) {
7570105a3dbcSRobert Elliott 		dev_err(&pdev->dev, "Failed to allocate controller head\n");
7571ecd9aad4SStephen M. Cameron 		return -ENOMEM;
7572105a3dbcSRobert Elliott 	}
7573edd16368SStephen M. Cameron 
757455c06c71SStephen M. Cameron 	h->pdev = pdev;
7575105a3dbcSRobert Elliott 
7576a9a3a273SStephen M. Cameron 	h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
75779846590eSStephen M. Cameron 	INIT_LIST_HEAD(&h->offline_device_list);
75786eaf46fdSStephen M. Cameron 	spin_lock_init(&h->lock);
75799846590eSStephen M. Cameron 	spin_lock_init(&h->offline_device_lock);
75806eaf46fdSStephen M. Cameron 	spin_lock_init(&h->scan_lock);
758134f0c627SDon Brace 	atomic_set(&h->passthru_cmds_avail, HPSA_MAX_CONCURRENT_PASSTHRUS);
75829b5c48c2SStephen Cameron 	atomic_set(&h->abort_cmds_available, HPSA_CMDS_RESERVED_FOR_ABORTS);
7583094963daSStephen M. Cameron 
75846636e7f4SDon Brace 	h->rescan_ctlr_wq = hpsa_create_controller_wq(h, "rescan");
75856636e7f4SDon Brace 	if (!h->rescan_ctlr_wq) {
7586080ef1ccSDon Brace 		rc = -ENOMEM;
7587080ef1ccSDon Brace 		goto clean1;
7588080ef1ccSDon Brace 	}
75896636e7f4SDon Brace 
75906636e7f4SDon Brace 	h->resubmit_wq = hpsa_create_controller_wq(h, "resubmit");
75916636e7f4SDon Brace 	if (!h->resubmit_wq) {
75926636e7f4SDon Brace 		rc = -ENOMEM;
7593105a3dbcSRobert Elliott 		goto clean1;	/* aer/h */
75946636e7f4SDon Brace 	}
75956636e7f4SDon Brace 
7596094963daSStephen M. Cameron 	/* Allocate and clear per-cpu variable lockup_detected */
7597094963daSStephen M. Cameron 	h->lockup_detected = alloc_percpu(u32);
75982a5ac326SStephen M. Cameron 	if (!h->lockup_detected) {
7599105a3dbcSRobert Elliott 		dev_err(&h->pdev->dev, "Failed to allocate lockup detector\n");
76002a5ac326SStephen M. Cameron 		rc = -ENOMEM;
7601105a3dbcSRobert Elliott 		goto clean1;	/* wq/aer/h */
76022a5ac326SStephen M. Cameron 	}
7603094963daSStephen M. Cameron 	set_lockup_detected_for_all_cpus(h, 0);
7604094963daSStephen M. Cameron 
760555c06c71SStephen M. Cameron 	rc = hpsa_pci_init(h);
7606105a3dbcSRobert Elliott 	if (rc)
7607105a3dbcSRobert Elliott 		goto clean2;	/* lockup, wq/aer/h */
7608edd16368SStephen M. Cameron 
7609f79cfec6SStephen M. Cameron 	sprintf(h->devname, HPSA "%d", number_of_controllers);
7610edd16368SStephen M. Cameron 	h->ctlr = number_of_controllers;
7611edd16368SStephen M. Cameron 	number_of_controllers++;
7612edd16368SStephen M. Cameron 
7613edd16368SStephen M. Cameron 	/* configure PCI DMA stuff */
7614ecd9aad4SStephen M. Cameron 	rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
7615ecd9aad4SStephen M. Cameron 	if (rc == 0) {
7616edd16368SStephen M. Cameron 		dac = 1;
7617ecd9aad4SStephen M. Cameron 	} else {
7618ecd9aad4SStephen M. Cameron 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7619ecd9aad4SStephen M. Cameron 		if (rc == 0) {
7620edd16368SStephen M. Cameron 			dac = 0;
7621ecd9aad4SStephen M. Cameron 		} else {
7622edd16368SStephen M. Cameron 			dev_err(&pdev->dev, "no suitable DMA available\n");
7623105a3dbcSRobert Elliott 			goto clean3;	/* pci, lockup, wq/aer/h */
7624edd16368SStephen M. Cameron 		}
7625ecd9aad4SStephen M. Cameron 	}
7626edd16368SStephen M. Cameron 
7627edd16368SStephen M. Cameron 	/* make sure the board interrupts are off */
7628edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
762910f66018SStephen M. Cameron 
7630105a3dbcSRobert Elliott 	rc = hpsa_request_irqs(h, do_hpsa_intr_msi, do_hpsa_intr_intx);
7631105a3dbcSRobert Elliott 	if (rc)
7632105a3dbcSRobert Elliott 		goto clean3;	/* pci, lockup, wq/aer/h */
7633303932fdSDon Brace 	dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
7634303932fdSDon Brace 	       h->devname, pdev->device,
7635a9a3a273SStephen M. Cameron 	       h->intr[h->intr_mode], dac ? "" : " not");
7636d37ffbe4SRobert Elliott 	rc = hpsa_alloc_cmd_pool(h);
76378947fd10SRobert Elliott 	if (rc)
7638105a3dbcSRobert Elliott 		goto clean4;	/* irq, pci, lockup, wq/aer/h */
7639105a3dbcSRobert Elliott 	rc = hpsa_alloc_sg_chain_blocks(h);
7640105a3dbcSRobert Elliott 	if (rc)
7641105a3dbcSRobert Elliott 		goto clean5;	/* cmd, irq, pci, lockup, wq/aer/h */
7642a08a8471SStephen M. Cameron 	init_waitqueue_head(&h->scan_wait_queue);
76439b5c48c2SStephen Cameron 	init_waitqueue_head(&h->abort_cmd_wait_queue);
7644*a58e7e53SWebb Scales 	init_waitqueue_head(&h->abort_sync_wait_queue);
7645a08a8471SStephen M. Cameron 	h->scan_finished = 1; /* no scan currently in progress */
7646edd16368SStephen M. Cameron 
7647edd16368SStephen M. Cameron 	pci_set_drvdata(pdev, h);
76489a41338eSStephen M. Cameron 	h->ndevices = 0;
7649316b221aSStephen M. Cameron 	h->hba_mode_enabled = 0;
76509a41338eSStephen M. Cameron 	h->scsi_host = NULL;
76519a41338eSStephen M. Cameron 	spin_lock_init(&h->devlock);
7652105a3dbcSRobert Elliott 	rc = hpsa_put_ctlr_into_performant_mode(h);
7653105a3dbcSRobert Elliott 	if (rc)
7654105a3dbcSRobert Elliott 		goto clean6;	/* sg, cmd, irq, pci, lockup, wq/aer/h */
765564670ac8SStephen M. Cameron 
7656105a3dbcSRobert Elliott 	/*
7657105a3dbcSRobert Elliott 	 * At this point, the controller is ready to take commands.
765864670ac8SStephen M. Cameron 	 * Now, if reset_devices and the hard reset didn't work, try
765964670ac8SStephen M. Cameron 	 * the soft reset and see if that works.
766064670ac8SStephen M. Cameron 	 */
766164670ac8SStephen M. Cameron 	if (try_soft_reset) {
766264670ac8SStephen M. Cameron 
766364670ac8SStephen M. Cameron 		/* This is kind of gross.  We may or may not get a completion
766464670ac8SStephen M. Cameron 		 * from the soft reset command, and if we do, then the value
766564670ac8SStephen M. Cameron 		 * from the fifo may or may not be valid.  So, we wait 10 secs
766664670ac8SStephen M. Cameron 		 * after the reset throwing away any completions we get during
766764670ac8SStephen M. Cameron 		 * that time.  Unregister the interrupt handler and register
766864670ac8SStephen M. Cameron 		 * fake ones to scoop up any residual completions.
766964670ac8SStephen M. Cameron 		 */
767064670ac8SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
767164670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
767264670ac8SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
7673ec501a18SRobert Elliott 		hpsa_free_irqs(h);
76749ee61794SRobert Elliott 		rc = hpsa_request_irqs(h, hpsa_msix_discard_completions,
767564670ac8SStephen M. Cameron 					hpsa_intx_discard_completions);
767664670ac8SStephen M. Cameron 		if (rc) {
76779ee61794SRobert Elliott 			dev_warn(&h->pdev->dev,
76789ee61794SRobert Elliott 				"Failed to request_irq after soft reset.\n");
767964670ac8SStephen M. Cameron 			goto clean4;
768064670ac8SStephen M. Cameron 		}
768164670ac8SStephen M. Cameron 
768264670ac8SStephen M. Cameron 		rc = hpsa_kdump_soft_reset(h);
768364670ac8SStephen M. Cameron 		if (rc)
768464670ac8SStephen M. Cameron 			/* Neither hard nor soft reset worked, we're hosed. */
768564670ac8SStephen M. Cameron 			goto clean4;
768664670ac8SStephen M. Cameron 
768764670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev, "Board READY.\n");
768864670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev,
768964670ac8SStephen M. Cameron 			"Waiting for stale completions to drain.\n");
769064670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_ON);
769164670ac8SStephen M. Cameron 		msleep(10000);
769264670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
769364670ac8SStephen M. Cameron 
769464670ac8SStephen M. Cameron 		rc = controller_reset_failed(h->cfgtable);
769564670ac8SStephen M. Cameron 		if (rc)
769664670ac8SStephen M. Cameron 			dev_info(&h->pdev->dev,
769764670ac8SStephen M. Cameron 				"Soft reset appears to have failed.\n");
769864670ac8SStephen M. Cameron 
769964670ac8SStephen M. Cameron 		/* since the controller's reset, we have to go back and re-init
770064670ac8SStephen M. Cameron 		 * everything.  Easiest to just forget what we've done and do it
770164670ac8SStephen M. Cameron 		 * all over again.
770264670ac8SStephen M. Cameron 		 */
770364670ac8SStephen M. Cameron 		hpsa_undo_allocations_after_kdump_soft_reset(h);
770464670ac8SStephen M. Cameron 		try_soft_reset = 0;
770564670ac8SStephen M. Cameron 		if (rc)
770664670ac8SStephen M. Cameron 			/* don't go to clean4, we already unallocated */
770764670ac8SStephen M. Cameron 			return -ENODEV;
770864670ac8SStephen M. Cameron 
770964670ac8SStephen M. Cameron 		goto reinit_after_soft_reset;
771064670ac8SStephen M. Cameron 	}
7711edd16368SStephen M. Cameron 
7712da0697bdSScott Teel 	/* Enable Accelerated IO path at driver layer */
7713da0697bdSScott Teel 	h->acciopath_status = 1;
7714da0697bdSScott Teel 
7715e863d68eSScott Teel 
7716edd16368SStephen M. Cameron 	/* Turn the interrupts on so we can service requests */
7717edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_ON);
7718edd16368SStephen M. Cameron 
7719339b2b14SStephen M. Cameron 	hpsa_hba_inquiry(h);
77204a4384ceSStephen Cameron 	rc = hpsa_register_scsi(h);	/* hook ourselves into SCSI subsystem */
77214a4384ceSStephen Cameron 	if (rc)
7722105a3dbcSRobert Elliott 		goto clean7;
77238a98db73SStephen M. Cameron 
77248a98db73SStephen M. Cameron 	/* Monitor the controller for firmware lockups */
77258a98db73SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
77268a98db73SStephen M. Cameron 	INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
77278a98db73SStephen M. Cameron 	schedule_delayed_work(&h->monitor_ctlr_work,
77288a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
77296636e7f4SDon Brace 	INIT_DELAYED_WORK(&h->rescan_ctlr_work, hpsa_rescan_ctlr_worker);
77306636e7f4SDon Brace 	queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
77316636e7f4SDon Brace 				h->heartbeat_sample_interval);
773288bf6d62SStephen M. Cameron 	return 0;
7733edd16368SStephen M. Cameron 
7734105a3dbcSRobert Elliott clean7: /* perf, sg, cmd, irq, pci, lockup, wq/aer/h */
7735105a3dbcSRobert Elliott 	kfree(h->hba_inquiry_data);
7736105a3dbcSRobert Elliott 	hpsa_free_performant_mode(h);
7737105a3dbcSRobert Elliott 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
7738105a3dbcSRobert Elliott clean6: /* sg, cmd, irq, pci, lockup, wq/aer/h */
773933a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
7740105a3dbcSRobert Elliott clean5: /* cmd, irq, pci, lockup, wq/aer/h */
77412e9d1b36SStephen M. Cameron 	hpsa_free_cmd_pool(h);
7742105a3dbcSRobert Elliott clean4: /* irq, pci, lockup, wq/aer/h */
7743ec501a18SRobert Elliott 	hpsa_free_irqs(h);
7744105a3dbcSRobert Elliott clean3: /* pci, lockup, wq/aer/h */
7745195f2c65SRobert Elliott 	hpsa_free_pci_init(h);
7746105a3dbcSRobert Elliott clean2: /* lockup, wq/aer/h */
7747105a3dbcSRobert Elliott 	if (h->lockup_detected) {
7748094963daSStephen M. Cameron 		free_percpu(h->lockup_detected);
7749105a3dbcSRobert Elliott 		h->lockup_detected = NULL;
7750105a3dbcSRobert Elliott 	}
7751105a3dbcSRobert Elliott clean1:	/* wq/aer/h */
7752105a3dbcSRobert Elliott 	if (h->resubmit_wq) {
7753105a3dbcSRobert Elliott 		destroy_workqueue(h->resubmit_wq);
7754105a3dbcSRobert Elliott 		h->resubmit_wq = NULL;
7755105a3dbcSRobert Elliott 	}
7756105a3dbcSRobert Elliott 	if (h->rescan_ctlr_wq) {
7757105a3dbcSRobert Elliott 		destroy_workqueue(h->rescan_ctlr_wq);
7758105a3dbcSRobert Elliott 		h->rescan_ctlr_wq = NULL;
7759105a3dbcSRobert Elliott 	}
7760edd16368SStephen M. Cameron 	kfree(h);
7761ecd9aad4SStephen M. Cameron 	return rc;
7762edd16368SStephen M. Cameron }
7763edd16368SStephen M. Cameron 
7764edd16368SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h)
7765edd16368SStephen M. Cameron {
7766edd16368SStephen M. Cameron 	char *flush_buf;
7767edd16368SStephen M. Cameron 	struct CommandList *c;
776825163bd5SWebb Scales 	int rc;
7769702890e3SStephen M. Cameron 
7770094963daSStephen M. Cameron 	if (unlikely(lockup_detected(h)))
7771702890e3SStephen M. Cameron 		return;
7772edd16368SStephen M. Cameron 	flush_buf = kzalloc(4, GFP_KERNEL);
7773edd16368SStephen M. Cameron 	if (!flush_buf)
7774edd16368SStephen M. Cameron 		return;
7775edd16368SStephen M. Cameron 
777645fcb86eSStephen Cameron 	c = cmd_alloc(h);
7777bf43caf3SRobert Elliott 
7778a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
7779a2dac136SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_CMD)) {
7780a2dac136SStephen M. Cameron 		goto out;
7781a2dac136SStephen M. Cameron 	}
778225163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
778325163bd5SWebb Scales 					PCI_DMA_TODEVICE, NO_TIMEOUT);
778425163bd5SWebb Scales 	if (rc)
778525163bd5SWebb Scales 		goto out;
7786edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus != 0)
7787a2dac136SStephen M. Cameron out:
7788edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
7789edd16368SStephen M. Cameron 			"error flushing cache on controller\n");
779045fcb86eSStephen Cameron 	cmd_free(h, c);
7791edd16368SStephen M. Cameron 	kfree(flush_buf);
7792edd16368SStephen M. Cameron }
7793edd16368SStephen M. Cameron 
7794edd16368SStephen M. Cameron static void hpsa_shutdown(struct pci_dev *pdev)
7795edd16368SStephen M. Cameron {
7796edd16368SStephen M. Cameron 	struct ctlr_info *h;
7797edd16368SStephen M. Cameron 
7798edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
7799edd16368SStephen M. Cameron 	/* Turn board interrupts off  and send the flush cache command
7800edd16368SStephen M. Cameron 	 * sendcmd will turn off interrupt, and send the flush...
7801edd16368SStephen M. Cameron 	 * To write all data in the battery backed cache to disks
7802edd16368SStephen M. Cameron 	 */
7803edd16368SStephen M. Cameron 	hpsa_flush_cache(h);
7804edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
7805105a3dbcSRobert Elliott 	hpsa_free_irqs(h);			/* init_one 4 */
7806cc64c817SRobert Elliott 	hpsa_disable_interrupt_mode(h);		/* pci_init 2 */
7807edd16368SStephen M. Cameron }
7808edd16368SStephen M. Cameron 
78096f039790SGreg Kroah-Hartman static void hpsa_free_device_info(struct ctlr_info *h)
781055e14e76SStephen M. Cameron {
781155e14e76SStephen M. Cameron 	int i;
781255e14e76SStephen M. Cameron 
7813105a3dbcSRobert Elliott 	for (i = 0; i < h->ndevices; i++) {
781455e14e76SStephen M. Cameron 		kfree(h->dev[i]);
7815105a3dbcSRobert Elliott 		h->dev[i] = NULL;
7816105a3dbcSRobert Elliott 	}
781755e14e76SStephen M. Cameron }
781855e14e76SStephen M. Cameron 
78196f039790SGreg Kroah-Hartman static void hpsa_remove_one(struct pci_dev *pdev)
7820edd16368SStephen M. Cameron {
7821edd16368SStephen M. Cameron 	struct ctlr_info *h;
78228a98db73SStephen M. Cameron 	unsigned long flags;
7823edd16368SStephen M. Cameron 
7824edd16368SStephen M. Cameron 	if (pci_get_drvdata(pdev) == NULL) {
7825edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "unable to remove device\n");
7826edd16368SStephen M. Cameron 		return;
7827edd16368SStephen M. Cameron 	}
7828edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
78298a98db73SStephen M. Cameron 
78308a98db73SStephen M. Cameron 	/* Get rid of any controller monitoring work items */
78318a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
78328a98db73SStephen M. Cameron 	h->remove_in_progress = 1;
78338a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
78346636e7f4SDon Brace 	cancel_delayed_work_sync(&h->monitor_ctlr_work);
78356636e7f4SDon Brace 	cancel_delayed_work_sync(&h->rescan_ctlr_work);
78366636e7f4SDon Brace 	destroy_workqueue(h->rescan_ctlr_wq);
78376636e7f4SDon Brace 	destroy_workqueue(h->resubmit_wq);
7838cc64c817SRobert Elliott 
7839105a3dbcSRobert Elliott 	/* includes hpsa_free_irqs - init_one 4 */
7840195f2c65SRobert Elliott 	/* includes hpsa_disable_interrupt_mode - pci_init 2 */
7841edd16368SStephen M. Cameron 	hpsa_shutdown(pdev);
7842cc64c817SRobert Elliott 
7843105a3dbcSRobert Elliott 	hpsa_free_device_info(h);		/* scan */
7844105a3dbcSRobert Elliott 
7845105a3dbcSRobert Elliott 	hpsa_unregister_scsi(h);			/* init_one "8" */
7846105a3dbcSRobert Elliott 	kfree(h->hba_inquiry_data);			/* init_one "8" */
7847105a3dbcSRobert Elliott 	h->hba_inquiry_data = NULL;			/* init_one "8" */
7848105a3dbcSRobert Elliott 	hpsa_free_performant_mode(h);			/* init_one 7 */
7849105a3dbcSRobert Elliott 	hpsa_free_sg_chain_blocks(h);			/* init_one 6 */
78501fb7c98aSRobert Elliott 	hpsa_free_cmd_pool(h);				/* init_one 5 */
7851105a3dbcSRobert Elliott 
7852105a3dbcSRobert Elliott 	/* hpsa_free_irqs already called via hpsa_shutdown init_one 4 */
7853195f2c65SRobert Elliott 
7854195f2c65SRobert Elliott 	/* includes hpsa_disable_interrupt_mode - pci_init 2 */
7855105a3dbcSRobert Elliott 	hpsa_free_pci_init(h);				/* init_one 3 */
7856195f2c65SRobert Elliott 
7857105a3dbcSRobert Elliott 	free_percpu(h->lockup_detected);		/* init_one 2 */
7858105a3dbcSRobert Elliott 	h->lockup_detected = NULL;			/* init_one 2 */
7859105a3dbcSRobert Elliott 	/* (void) pci_disable_pcie_error_reporting(pdev); */	/* init_one 1 */
7860105a3dbcSRobert Elliott 	kfree(h);					/* init_one 1 */
7861edd16368SStephen M. Cameron }
7862edd16368SStephen M. Cameron 
7863edd16368SStephen M. Cameron static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
7864edd16368SStephen M. Cameron 	__attribute__((unused)) pm_message_t state)
7865edd16368SStephen M. Cameron {
7866edd16368SStephen M. Cameron 	return -ENOSYS;
7867edd16368SStephen M. Cameron }
7868edd16368SStephen M. Cameron 
7869edd16368SStephen M. Cameron static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
7870edd16368SStephen M. Cameron {
7871edd16368SStephen M. Cameron 	return -ENOSYS;
7872edd16368SStephen M. Cameron }
7873edd16368SStephen M. Cameron 
7874edd16368SStephen M. Cameron static struct pci_driver hpsa_pci_driver = {
7875f79cfec6SStephen M. Cameron 	.name = HPSA,
7876edd16368SStephen M. Cameron 	.probe = hpsa_init_one,
78776f039790SGreg Kroah-Hartman 	.remove = hpsa_remove_one,
7878edd16368SStephen M. Cameron 	.id_table = hpsa_pci_device_id,	/* id_table */
7879edd16368SStephen M. Cameron 	.shutdown = hpsa_shutdown,
7880edd16368SStephen M. Cameron 	.suspend = hpsa_suspend,
7881edd16368SStephen M. Cameron 	.resume = hpsa_resume,
7882edd16368SStephen M. Cameron };
7883edd16368SStephen M. Cameron 
7884303932fdSDon Brace /* Fill in bucket_map[], given nsgs (the max number of
7885303932fdSDon Brace  * scatter gather elements supported) and bucket[],
7886303932fdSDon Brace  * which is an array of 8 integers.  The bucket[] array
7887303932fdSDon Brace  * contains 8 different DMA transfer sizes (in 16
7888303932fdSDon Brace  * byte increments) which the controller uses to fetch
7889303932fdSDon Brace  * commands.  This function fills in bucket_map[], which
7890303932fdSDon Brace  * maps a given number of scatter gather elements to one of
7891303932fdSDon Brace  * the 8 DMA transfer sizes.  The point of it is to allow the
7892303932fdSDon Brace  * controller to only do as much DMA as needed to fetch the
7893303932fdSDon Brace  * command, with the DMA transfer size encoded in the lower
7894303932fdSDon Brace  * bits of the command address.
7895303932fdSDon Brace  */
7896303932fdSDon Brace static void  calc_bucket_map(int bucket[], int num_buckets,
78972b08b3e9SDon Brace 	int nsgs, int min_blocks, u32 *bucket_map)
7898303932fdSDon Brace {
7899303932fdSDon Brace 	int i, j, b, size;
7900303932fdSDon Brace 
7901303932fdSDon Brace 	/* Note, bucket_map must have nsgs+1 entries. */
7902303932fdSDon Brace 	for (i = 0; i <= nsgs; i++) {
7903303932fdSDon Brace 		/* Compute size of a command with i SG entries */
7904e1f7de0cSMatt Gates 		size = i + min_blocks;
7905303932fdSDon Brace 		b = num_buckets; /* Assume the biggest bucket */
7906303932fdSDon Brace 		/* Find the bucket that is just big enough */
7907e1f7de0cSMatt Gates 		for (j = 0; j < num_buckets; j++) {
7908303932fdSDon Brace 			if (bucket[j] >= size) {
7909303932fdSDon Brace 				b = j;
7910303932fdSDon Brace 				break;
7911303932fdSDon Brace 			}
7912303932fdSDon Brace 		}
7913303932fdSDon Brace 		/* for a command with i SG entries, use bucket b. */
7914303932fdSDon Brace 		bucket_map[i] = b;
7915303932fdSDon Brace 	}
7916303932fdSDon Brace }
7917303932fdSDon Brace 
7918105a3dbcSRobert Elliott /*
7919105a3dbcSRobert Elliott  * return -ENODEV on err, 0 on success (or no action)
7920105a3dbcSRobert Elliott  * allocates numerous items that must be freed later
7921105a3dbcSRobert Elliott  */
7922c706a795SRobert Elliott static int hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
7923303932fdSDon Brace {
79246c311b57SStephen M. Cameron 	int i;
79256c311b57SStephen M. Cameron 	unsigned long register_value;
7926e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
7927e1f7de0cSMatt Gates 			(trans_support & CFGTBL_Trans_use_short_tags) |
7928e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix |
7929b9af4937SStephen M. Cameron 			(trans_support & (CFGTBL_Trans_io_accel1 |
7930b9af4937SStephen M. Cameron 				CFGTBL_Trans_io_accel2));
7931e1f7de0cSMatt Gates 	struct access_method access = SA5_performant_access;
7932def342bdSStephen M. Cameron 
7933def342bdSStephen M. Cameron 	/* This is a bit complicated.  There are 8 registers on
7934def342bdSStephen M. Cameron 	 * the controller which we write to to tell it 8 different
7935def342bdSStephen M. Cameron 	 * sizes of commands which there may be.  It's a way of
7936def342bdSStephen M. Cameron 	 * reducing the DMA done to fetch each command.  Encoded into
7937def342bdSStephen M. Cameron 	 * each command's tag are 3 bits which communicate to the controller
7938def342bdSStephen M. Cameron 	 * which of the eight sizes that command fits within.  The size of
7939def342bdSStephen M. Cameron 	 * each command depends on how many scatter gather entries there are.
7940def342bdSStephen M. Cameron 	 * Each SG entry requires 16 bytes.  The eight registers are programmed
7941def342bdSStephen M. Cameron 	 * with the number of 16-byte blocks a command of that size requires.
7942def342bdSStephen M. Cameron 	 * The smallest command possible requires 5 such 16 byte blocks.
7943d66ae08bSStephen M. Cameron 	 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
7944def342bdSStephen M. Cameron 	 * blocks.  Note, this only extends to the SG entries contained
7945def342bdSStephen M. Cameron 	 * within the command block, and does not extend to chained blocks
7946def342bdSStephen M. Cameron 	 * of SG elements.   bft[] contains the eight values we write to
7947def342bdSStephen M. Cameron 	 * the registers.  They are not evenly distributed, but have more
7948def342bdSStephen M. Cameron 	 * sizes for small commands, and fewer sizes for larger commands.
7949def342bdSStephen M. Cameron 	 */
7950d66ae08bSStephen M. Cameron 	int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
7951b9af4937SStephen M. Cameron #define MIN_IOACCEL2_BFT_ENTRY 5
7952b9af4937SStephen M. Cameron #define HPSA_IOACCEL2_HEADER_SZ 4
7953b9af4937SStephen M. Cameron 	int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
7954b9af4937SStephen M. Cameron 			13, 14, 15, 16, 17, 18, 19,
7955b9af4937SStephen M. Cameron 			HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
7956b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
7957b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
7958b9af4937SStephen M. Cameron 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
7959b9af4937SStephen M. Cameron 				 16 * MIN_IOACCEL2_BFT_ENTRY);
7960b9af4937SStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
7961d66ae08bSStephen M. Cameron 	BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
7962303932fdSDon Brace 	/*  5 = 1 s/g entry or 4k
7963303932fdSDon Brace 	 *  6 = 2 s/g entry or 8k
7964303932fdSDon Brace 	 *  8 = 4 s/g entry or 16k
7965303932fdSDon Brace 	 * 10 = 6 s/g entry or 24k
7966303932fdSDon Brace 	 */
7967303932fdSDon Brace 
7968b3a52e79SStephen M. Cameron 	/* If the controller supports either ioaccel method then
7969b3a52e79SStephen M. Cameron 	 * we can also use the RAID stack submit path that does not
7970b3a52e79SStephen M. Cameron 	 * perform the superfluous readl() after each command submission.
7971b3a52e79SStephen M. Cameron 	 */
7972b3a52e79SStephen M. Cameron 	if (trans_support & (CFGTBL_Trans_io_accel1 | CFGTBL_Trans_io_accel2))
7973b3a52e79SStephen M. Cameron 		access = SA5_performant_access_no_read;
7974b3a52e79SStephen M. Cameron 
7975303932fdSDon Brace 	/* Controller spec: zero out this buffer. */
7976072b0518SStephen M. Cameron 	for (i = 0; i < h->nreply_queues; i++)
7977072b0518SStephen M. Cameron 		memset(h->reply_queue[i].head, 0, h->reply_queue_size);
7978303932fdSDon Brace 
7979d66ae08bSStephen M. Cameron 	bft[7] = SG_ENTRIES_IN_CMD + 4;
7980d66ae08bSStephen M. Cameron 	calc_bucket_map(bft, ARRAY_SIZE(bft),
7981e1f7de0cSMatt Gates 				SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
7982303932fdSDon Brace 	for (i = 0; i < 8; i++)
7983303932fdSDon Brace 		writel(bft[i], &h->transtable->BlockFetch[i]);
7984303932fdSDon Brace 
7985303932fdSDon Brace 	/* size of controller ring buffer */
7986303932fdSDon Brace 	writel(h->max_commands, &h->transtable->RepQSize);
7987254f796bSMatt Gates 	writel(h->nreply_queues, &h->transtable->RepQCount);
7988303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrLow32);
7989303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrHigh32);
7990254f796bSMatt Gates 
7991254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
7992254f796bSMatt Gates 		writel(0, &h->transtable->RepQAddr[i].upper);
7993072b0518SStephen M. Cameron 		writel(h->reply_queue[i].busaddr,
7994254f796bSMatt Gates 			&h->transtable->RepQAddr[i].lower);
7995254f796bSMatt Gates 	}
7996254f796bSMatt Gates 
7997b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
7998e1f7de0cSMatt Gates 	writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
7999e1f7de0cSMatt Gates 	/*
8000e1f7de0cSMatt Gates 	 * enable outbound interrupt coalescing in accelerator mode;
8001e1f7de0cSMatt Gates 	 */
8002e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
8003e1f7de0cSMatt Gates 		access = SA5_ioaccel_mode1_access;
8004e1f7de0cSMatt Gates 		writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
8005e1f7de0cSMatt Gates 		writel(4, &h->cfgtable->HostWrite.CoalIntCount);
8006c349775eSScott Teel 	} else {
8007c349775eSScott Teel 		if (trans_support & CFGTBL_Trans_io_accel2) {
8008c349775eSScott Teel 			access = SA5_ioaccel_mode2_access;
8009c349775eSScott Teel 			writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
8010c349775eSScott Teel 			writel(4, &h->cfgtable->HostWrite.CoalIntCount);
8011c349775eSScott Teel 		}
8012e1f7de0cSMatt Gates 	}
8013303932fdSDon Brace 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
8014c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h)) {
8015c706a795SRobert Elliott 		dev_err(&h->pdev->dev,
8016c706a795SRobert Elliott 			"performant mode problem - doorbell timeout\n");
8017c706a795SRobert Elliott 		return -ENODEV;
8018c706a795SRobert Elliott 	}
8019303932fdSDon Brace 	register_value = readl(&(h->cfgtable->TransportActive));
8020303932fdSDon Brace 	if (!(register_value & CFGTBL_Trans_Performant)) {
8021050f7147SStephen Cameron 		dev_err(&h->pdev->dev,
8022050f7147SStephen Cameron 			"performant mode problem - transport not active\n");
8023c706a795SRobert Elliott 		return -ENODEV;
8024303932fdSDon Brace 	}
8025960a30e7SStephen M. Cameron 	/* Change the access methods to the performant access methods */
8026e1f7de0cSMatt Gates 	h->access = access;
8027e1f7de0cSMatt Gates 	h->transMethod = transMethod;
8028e1f7de0cSMatt Gates 
8029b9af4937SStephen M. Cameron 	if (!((trans_support & CFGTBL_Trans_io_accel1) ||
8030b9af4937SStephen M. Cameron 		(trans_support & CFGTBL_Trans_io_accel2)))
8031c706a795SRobert Elliott 		return 0;
8032e1f7de0cSMatt Gates 
8033b9af4937SStephen M. Cameron 	if (trans_support & CFGTBL_Trans_io_accel1) {
8034e1f7de0cSMatt Gates 		/* Set up I/O accelerator mode */
8035e1f7de0cSMatt Gates 		for (i = 0; i < h->nreply_queues; i++) {
8036e1f7de0cSMatt Gates 			writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
8037e1f7de0cSMatt Gates 			h->reply_queue[i].current_entry =
8038e1f7de0cSMatt Gates 				readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
8039e1f7de0cSMatt Gates 		}
8040283b4a9bSStephen M. Cameron 		bft[7] = h->ioaccel_maxsg + 8;
8041283b4a9bSStephen M. Cameron 		calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
8042e1f7de0cSMatt Gates 				h->ioaccel1_blockFetchTable);
8043e1f7de0cSMatt Gates 
8044e1f7de0cSMatt Gates 		/* initialize all reply queue entries to unused */
8045072b0518SStephen M. Cameron 		for (i = 0; i < h->nreply_queues; i++)
8046072b0518SStephen M. Cameron 			memset(h->reply_queue[i].head,
8047072b0518SStephen M. Cameron 				(u8) IOACCEL_MODE1_REPLY_UNUSED,
8048072b0518SStephen M. Cameron 				h->reply_queue_size);
8049e1f7de0cSMatt Gates 
8050e1f7de0cSMatt Gates 		/* set all the constant fields in the accelerator command
8051e1f7de0cSMatt Gates 		 * frames once at init time to save CPU cycles later.
8052e1f7de0cSMatt Gates 		 */
8053e1f7de0cSMatt Gates 		for (i = 0; i < h->nr_cmds; i++) {
8054e1f7de0cSMatt Gates 			struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
8055e1f7de0cSMatt Gates 
8056e1f7de0cSMatt Gates 			cp->function = IOACCEL1_FUNCTION_SCSIIO;
8057e1f7de0cSMatt Gates 			cp->err_info = (u32) (h->errinfo_pool_dhandle +
8058e1f7de0cSMatt Gates 					(i * sizeof(struct ErrorInfo)));
8059e1f7de0cSMatt Gates 			cp->err_info_len = sizeof(struct ErrorInfo);
8060e1f7de0cSMatt Gates 			cp->sgl_offset = IOACCEL1_SGLOFFSET;
80612b08b3e9SDon Brace 			cp->host_context_flags =
80622b08b3e9SDon Brace 				cpu_to_le16(IOACCEL1_HCFLAGS_CISS_FORMAT);
8063e1f7de0cSMatt Gates 			cp->timeout_sec = 0;
8064e1f7de0cSMatt Gates 			cp->ReplyQueue = 0;
806550a0decfSStephen M. Cameron 			cp->tag =
8066f2405db8SDon Brace 				cpu_to_le64((i << DIRECT_LOOKUP_SHIFT));
806750a0decfSStephen M. Cameron 			cp->host_addr =
806850a0decfSStephen M. Cameron 				cpu_to_le64(h->ioaccel_cmd_pool_dhandle +
8069e1f7de0cSMatt Gates 					(i * sizeof(struct io_accel1_cmd)));
8070e1f7de0cSMatt Gates 		}
8071b9af4937SStephen M. Cameron 	} else if (trans_support & CFGTBL_Trans_io_accel2) {
8072b9af4937SStephen M. Cameron 		u64 cfg_offset, cfg_base_addr_index;
8073b9af4937SStephen M. Cameron 		u32 bft2_offset, cfg_base_addr;
8074b9af4937SStephen M. Cameron 		int rc;
8075b9af4937SStephen M. Cameron 
8076b9af4937SStephen M. Cameron 		rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
8077b9af4937SStephen M. Cameron 			&cfg_base_addr_index, &cfg_offset);
8078b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
8079b9af4937SStephen M. Cameron 		bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
8080b9af4937SStephen M. Cameron 		calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
8081b9af4937SStephen M. Cameron 				4, h->ioaccel2_blockFetchTable);
8082b9af4937SStephen M. Cameron 		bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
8083b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct CfgTable,
8084b9af4937SStephen M. Cameron 				io_accel_request_size_offset) != 0xb8);
8085b9af4937SStephen M. Cameron 		h->ioaccel2_bft2_regs =
8086b9af4937SStephen M. Cameron 			remap_pci_mem(pci_resource_start(h->pdev,
8087b9af4937SStephen M. Cameron 					cfg_base_addr_index) +
8088b9af4937SStephen M. Cameron 					cfg_offset + bft2_offset,
8089b9af4937SStephen M. Cameron 					ARRAY_SIZE(bft2) *
8090b9af4937SStephen M. Cameron 					sizeof(*h->ioaccel2_bft2_regs));
8091b9af4937SStephen M. Cameron 		for (i = 0; i < ARRAY_SIZE(bft2); i++)
8092b9af4937SStephen M. Cameron 			writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
8093b9af4937SStephen M. Cameron 	}
8094b9af4937SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
8095c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h)) {
8096c706a795SRobert Elliott 		dev_err(&h->pdev->dev,
8097c706a795SRobert Elliott 			"performant mode problem - enabling ioaccel mode\n");
8098c706a795SRobert Elliott 		return -ENODEV;
8099c706a795SRobert Elliott 	}
8100c706a795SRobert Elliott 	return 0;
8101e1f7de0cSMatt Gates }
8102e1f7de0cSMatt Gates 
81031fb7c98aSRobert Elliott /* Free ioaccel1 mode command blocks and block fetch table */
81041fb7c98aSRobert Elliott static void hpsa_free_ioaccel1_cmd_and_bft(struct ctlr_info *h)
81051fb7c98aSRobert Elliott {
8106105a3dbcSRobert Elliott 	if (h->ioaccel_cmd_pool) {
81071fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
81081fb7c98aSRobert Elliott 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
81091fb7c98aSRobert Elliott 			h->ioaccel_cmd_pool,
81101fb7c98aSRobert Elliott 			h->ioaccel_cmd_pool_dhandle);
8111105a3dbcSRobert Elliott 		h->ioaccel_cmd_pool = NULL;
8112105a3dbcSRobert Elliott 		h->ioaccel_cmd_pool_dhandle = 0;
8113105a3dbcSRobert Elliott 	}
81141fb7c98aSRobert Elliott 	kfree(h->ioaccel1_blockFetchTable);
8115105a3dbcSRobert Elliott 	h->ioaccel1_blockFetchTable = NULL;
81161fb7c98aSRobert Elliott }
81171fb7c98aSRobert Elliott 
8118d37ffbe4SRobert Elliott /* Allocate ioaccel1 mode command blocks and block fetch table */
8119d37ffbe4SRobert Elliott static int hpsa_alloc_ioaccel1_cmd_and_bft(struct ctlr_info *h)
8120e1f7de0cSMatt Gates {
8121283b4a9bSStephen M. Cameron 	h->ioaccel_maxsg =
8122283b4a9bSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
8123283b4a9bSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
8124283b4a9bSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
8125283b4a9bSStephen M. Cameron 
8126e1f7de0cSMatt Gates 	/* Command structures must be aligned on a 128-byte boundary
8127e1f7de0cSMatt Gates 	 * because the 7 lower bits of the address are used by the
8128e1f7de0cSMatt Gates 	 * hardware.
8129e1f7de0cSMatt Gates 	 */
8130e1f7de0cSMatt Gates 	BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
8131e1f7de0cSMatt Gates 			IOACCEL1_COMMANDLIST_ALIGNMENT);
8132e1f7de0cSMatt Gates 	h->ioaccel_cmd_pool =
8133e1f7de0cSMatt Gates 		pci_alloc_consistent(h->pdev,
8134e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
8135e1f7de0cSMatt Gates 			&(h->ioaccel_cmd_pool_dhandle));
8136e1f7de0cSMatt Gates 
8137e1f7de0cSMatt Gates 	h->ioaccel1_blockFetchTable =
8138283b4a9bSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
8139e1f7de0cSMatt Gates 				sizeof(u32)), GFP_KERNEL);
8140e1f7de0cSMatt Gates 
8141e1f7de0cSMatt Gates 	if ((h->ioaccel_cmd_pool == NULL) ||
8142e1f7de0cSMatt Gates 		(h->ioaccel1_blockFetchTable == NULL))
8143e1f7de0cSMatt Gates 		goto clean_up;
8144e1f7de0cSMatt Gates 
8145e1f7de0cSMatt Gates 	memset(h->ioaccel_cmd_pool, 0,
8146e1f7de0cSMatt Gates 		h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
8147e1f7de0cSMatt Gates 	return 0;
8148e1f7de0cSMatt Gates 
8149e1f7de0cSMatt Gates clean_up:
81501fb7c98aSRobert Elliott 	hpsa_free_ioaccel1_cmd_and_bft(h);
81512dd02d74SRobert Elliott 	return -ENOMEM;
81526c311b57SStephen M. Cameron }
81536c311b57SStephen M. Cameron 
81541fb7c98aSRobert Elliott /* Free ioaccel2 mode command blocks and block fetch table */
81551fb7c98aSRobert Elliott static void hpsa_free_ioaccel2_cmd_and_bft(struct ctlr_info *h)
81561fb7c98aSRobert Elliott {
8157d9a729f3SWebb Scales 	hpsa_free_ioaccel2_sg_chain_blocks(h);
8158d9a729f3SWebb Scales 
8159105a3dbcSRobert Elliott 	if (h->ioaccel2_cmd_pool) {
81601fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
81611fb7c98aSRobert Elliott 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
81621fb7c98aSRobert Elliott 			h->ioaccel2_cmd_pool,
81631fb7c98aSRobert Elliott 			h->ioaccel2_cmd_pool_dhandle);
8164105a3dbcSRobert Elliott 		h->ioaccel2_cmd_pool = NULL;
8165105a3dbcSRobert Elliott 		h->ioaccel2_cmd_pool_dhandle = 0;
8166105a3dbcSRobert Elliott 	}
81671fb7c98aSRobert Elliott 	kfree(h->ioaccel2_blockFetchTable);
8168105a3dbcSRobert Elliott 	h->ioaccel2_blockFetchTable = NULL;
81691fb7c98aSRobert Elliott }
81701fb7c98aSRobert Elliott 
8171d37ffbe4SRobert Elliott /* Allocate ioaccel2 mode command blocks and block fetch table */
8172d37ffbe4SRobert Elliott static int hpsa_alloc_ioaccel2_cmd_and_bft(struct ctlr_info *h)
8173aca9012aSStephen M. Cameron {
8174d9a729f3SWebb Scales 	int rc;
8175d9a729f3SWebb Scales 
8176aca9012aSStephen M. Cameron 	/* Allocate ioaccel2 mode command blocks and block fetch table */
8177aca9012aSStephen M. Cameron 
8178aca9012aSStephen M. Cameron 	h->ioaccel_maxsg =
8179aca9012aSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
8180aca9012aSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
8181aca9012aSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
8182aca9012aSStephen M. Cameron 
8183aca9012aSStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
8184aca9012aSStephen M. Cameron 			IOACCEL2_COMMANDLIST_ALIGNMENT);
8185aca9012aSStephen M. Cameron 	h->ioaccel2_cmd_pool =
8186aca9012aSStephen M. Cameron 		pci_alloc_consistent(h->pdev,
8187aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
8188aca9012aSStephen M. Cameron 			&(h->ioaccel2_cmd_pool_dhandle));
8189aca9012aSStephen M. Cameron 
8190aca9012aSStephen M. Cameron 	h->ioaccel2_blockFetchTable =
8191aca9012aSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
8192aca9012aSStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
8193aca9012aSStephen M. Cameron 
8194aca9012aSStephen M. Cameron 	if ((h->ioaccel2_cmd_pool == NULL) ||
8195d9a729f3SWebb Scales 		(h->ioaccel2_blockFetchTable == NULL)) {
8196d9a729f3SWebb Scales 		rc = -ENOMEM;
8197d9a729f3SWebb Scales 		goto clean_up;
8198d9a729f3SWebb Scales 	}
8199d9a729f3SWebb Scales 
8200d9a729f3SWebb Scales 	rc = hpsa_allocate_ioaccel2_sg_chain_blocks(h);
8201d9a729f3SWebb Scales 	if (rc)
8202aca9012aSStephen M. Cameron 		goto clean_up;
8203aca9012aSStephen M. Cameron 
8204aca9012aSStephen M. Cameron 	memset(h->ioaccel2_cmd_pool, 0,
8205aca9012aSStephen M. Cameron 		h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
8206aca9012aSStephen M. Cameron 	return 0;
8207aca9012aSStephen M. Cameron 
8208aca9012aSStephen M. Cameron clean_up:
82091fb7c98aSRobert Elliott 	hpsa_free_ioaccel2_cmd_and_bft(h);
8210d9a729f3SWebb Scales 	return rc;
8211aca9012aSStephen M. Cameron }
8212aca9012aSStephen M. Cameron 
8213105a3dbcSRobert Elliott /* Free items allocated by hpsa_put_ctlr_into_performant_mode */
8214105a3dbcSRobert Elliott static void hpsa_free_performant_mode(struct ctlr_info *h)
8215105a3dbcSRobert Elliott {
8216105a3dbcSRobert Elliott 	kfree(h->blockFetchTable);
8217105a3dbcSRobert Elliott 	h->blockFetchTable = NULL;
8218105a3dbcSRobert Elliott 	hpsa_free_reply_queues(h);
8219105a3dbcSRobert Elliott 	hpsa_free_ioaccel1_cmd_and_bft(h);
8220105a3dbcSRobert Elliott 	hpsa_free_ioaccel2_cmd_and_bft(h);
8221105a3dbcSRobert Elliott }
8222105a3dbcSRobert Elliott 
8223105a3dbcSRobert Elliott /* return -ENODEV on error, 0 on success (or no action)
8224105a3dbcSRobert Elliott  * allocates numerous items that must be freed later
8225105a3dbcSRobert Elliott  */
8226105a3dbcSRobert Elliott static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
82276c311b57SStephen M. Cameron {
82286c311b57SStephen M. Cameron 	u32 trans_support;
8229e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
8230e1f7de0cSMatt Gates 					CFGTBL_Trans_use_short_tags;
8231105a3dbcSRobert Elliott 	int i, rc;
82326c311b57SStephen M. Cameron 
823302ec19c8SStephen M. Cameron 	if (hpsa_simple_mode)
8234105a3dbcSRobert Elliott 		return 0;
823502ec19c8SStephen M. Cameron 
823667c99a72Sscameron@beardog.cce.hp.com 	trans_support = readl(&(h->cfgtable->TransportSupport));
823767c99a72Sscameron@beardog.cce.hp.com 	if (!(trans_support & PERFORMANT_MODE))
8238105a3dbcSRobert Elliott 		return 0;
823967c99a72Sscameron@beardog.cce.hp.com 
8240e1f7de0cSMatt Gates 	/* Check for I/O accelerator mode support */
8241e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
8242e1f7de0cSMatt Gates 		transMethod |= CFGTBL_Trans_io_accel1 |
8243e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix;
8244105a3dbcSRobert Elliott 		rc = hpsa_alloc_ioaccel1_cmd_and_bft(h);
8245105a3dbcSRobert Elliott 		if (rc)
8246105a3dbcSRobert Elliott 			return rc;
8247105a3dbcSRobert Elliott 	} else if (trans_support & CFGTBL_Trans_io_accel2) {
8248aca9012aSStephen M. Cameron 		transMethod |= CFGTBL_Trans_io_accel2 |
8249aca9012aSStephen M. Cameron 				CFGTBL_Trans_enable_directed_msix;
8250105a3dbcSRobert Elliott 		rc = hpsa_alloc_ioaccel2_cmd_and_bft(h);
8251105a3dbcSRobert Elliott 		if (rc)
8252105a3dbcSRobert Elliott 			return rc;
8253e1f7de0cSMatt Gates 	}
8254e1f7de0cSMatt Gates 
8255eee0f03aSHannes Reinecke 	h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
8256cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
82576c311b57SStephen M. Cameron 	/* Performant mode ring buffer and supporting data structures */
8258072b0518SStephen M. Cameron 	h->reply_queue_size = h->max_commands * sizeof(u64);
82596c311b57SStephen M. Cameron 
8260254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
8261072b0518SStephen M. Cameron 		h->reply_queue[i].head = pci_alloc_consistent(h->pdev,
8262072b0518SStephen M. Cameron 						h->reply_queue_size,
8263072b0518SStephen M. Cameron 						&(h->reply_queue[i].busaddr));
8264105a3dbcSRobert Elliott 		if (!h->reply_queue[i].head) {
8265105a3dbcSRobert Elliott 			rc = -ENOMEM;
8266105a3dbcSRobert Elliott 			goto clean1;	/* rq, ioaccel */
8267105a3dbcSRobert Elliott 		}
8268254f796bSMatt Gates 		h->reply_queue[i].size = h->max_commands;
8269254f796bSMatt Gates 		h->reply_queue[i].wraparound = 1;  /* spec: init to 1 */
8270254f796bSMatt Gates 		h->reply_queue[i].current_entry = 0;
8271254f796bSMatt Gates 	}
8272254f796bSMatt Gates 
82736c311b57SStephen M. Cameron 	/* Need a block fetch table for performant mode */
8274d66ae08bSStephen M. Cameron 	h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
82756c311b57SStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
8276105a3dbcSRobert Elliott 	if (!h->blockFetchTable) {
8277105a3dbcSRobert Elliott 		rc = -ENOMEM;
8278105a3dbcSRobert Elliott 		goto clean1;	/* rq, ioaccel */
8279105a3dbcSRobert Elliott 	}
82806c311b57SStephen M. Cameron 
8281105a3dbcSRobert Elliott 	rc = hpsa_enter_performant_mode(h, trans_support);
8282105a3dbcSRobert Elliott 	if (rc)
8283105a3dbcSRobert Elliott 		goto clean2;	/* bft, rq, ioaccel */
8284105a3dbcSRobert Elliott 	return 0;
8285303932fdSDon Brace 
8286105a3dbcSRobert Elliott clean2:	/* bft, rq, ioaccel */
8287303932fdSDon Brace 	kfree(h->blockFetchTable);
8288105a3dbcSRobert Elliott 	h->blockFetchTable = NULL;
8289105a3dbcSRobert Elliott clean1:	/* rq, ioaccel */
8290105a3dbcSRobert Elliott 	hpsa_free_reply_queues(h);
8291105a3dbcSRobert Elliott 	hpsa_free_ioaccel1_cmd_and_bft(h);
8292105a3dbcSRobert Elliott 	hpsa_free_ioaccel2_cmd_and_bft(h);
8293105a3dbcSRobert Elliott 	return rc;
8294303932fdSDon Brace }
8295303932fdSDon Brace 
829623100dd9SStephen M. Cameron static int is_accelerated_cmd(struct CommandList *c)
829776438d08SStephen M. Cameron {
829823100dd9SStephen M. Cameron 	return c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_IOACCEL2;
829923100dd9SStephen M. Cameron }
830023100dd9SStephen M. Cameron 
830123100dd9SStephen M. Cameron static void hpsa_drain_accel_commands(struct ctlr_info *h)
830223100dd9SStephen M. Cameron {
830323100dd9SStephen M. Cameron 	struct CommandList *c = NULL;
8304f2405db8SDon Brace 	int i, accel_cmds_out;
8305281a7fd0SWebb Scales 	int refcount;
830676438d08SStephen M. Cameron 
8307f2405db8SDon Brace 	do { /* wait for all outstanding ioaccel commands to drain out */
830823100dd9SStephen M. Cameron 		accel_cmds_out = 0;
8309f2405db8SDon Brace 		for (i = 0; i < h->nr_cmds; i++) {
8310f2405db8SDon Brace 			c = h->cmd_pool + i;
8311281a7fd0SWebb Scales 			refcount = atomic_inc_return(&c->refcount);
8312281a7fd0SWebb Scales 			if (refcount > 1) /* Command is allocated */
831323100dd9SStephen M. Cameron 				accel_cmds_out += is_accelerated_cmd(c);
8314281a7fd0SWebb Scales 			cmd_free(h, c);
8315f2405db8SDon Brace 		}
831623100dd9SStephen M. Cameron 		if (accel_cmds_out <= 0)
831776438d08SStephen M. Cameron 			break;
831876438d08SStephen M. Cameron 		msleep(100);
831976438d08SStephen M. Cameron 	} while (1);
832076438d08SStephen M. Cameron }
832176438d08SStephen M. Cameron 
8322edd16368SStephen M. Cameron /*
8323edd16368SStephen M. Cameron  *  This is it.  Register the PCI driver information for the cards we control
8324edd16368SStephen M. Cameron  *  the OS will call our registered routines when it finds one of our cards.
8325edd16368SStephen M. Cameron  */
8326edd16368SStephen M. Cameron static int __init hpsa_init(void)
8327edd16368SStephen M. Cameron {
832831468401SMike Miller 	return pci_register_driver(&hpsa_pci_driver);
8329edd16368SStephen M. Cameron }
8330edd16368SStephen M. Cameron 
8331edd16368SStephen M. Cameron static void __exit hpsa_cleanup(void)
8332edd16368SStephen M. Cameron {
8333edd16368SStephen M. Cameron 	pci_unregister_driver(&hpsa_pci_driver);
8334edd16368SStephen M. Cameron }
8335edd16368SStephen M. Cameron 
8336e1f7de0cSMatt Gates static void __attribute__((unused)) verify_offsets(void)
8337e1f7de0cSMatt Gates {
8338e1f7de0cSMatt Gates #define VERIFY_OFFSET(member, offset) \
8339dd0e19f3SScott Teel 	BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
8340dd0e19f3SScott Teel 
8341dd0e19f3SScott Teel 	VERIFY_OFFSET(structure_size, 0);
8342dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_size, 4);
8343dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_cnt, 8);
8344dd0e19f3SScott Teel 	VERIFY_OFFSET(phys_blk_shift, 16);
8345dd0e19f3SScott Teel 	VERIFY_OFFSET(parity_rotation_shift, 17);
8346dd0e19f3SScott Teel 	VERIFY_OFFSET(strip_size, 18);
8347dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_starting_blk, 20);
8348dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_blk_cnt, 28);
8349dd0e19f3SScott Teel 	VERIFY_OFFSET(data_disks_per_row, 36);
8350dd0e19f3SScott Teel 	VERIFY_OFFSET(metadata_disks_per_row, 38);
8351dd0e19f3SScott Teel 	VERIFY_OFFSET(row_cnt, 40);
8352dd0e19f3SScott Teel 	VERIFY_OFFSET(layout_map_count, 42);
8353dd0e19f3SScott Teel 	VERIFY_OFFSET(flags, 44);
8354dd0e19f3SScott Teel 	VERIFY_OFFSET(dekindex, 46);
8355dd0e19f3SScott Teel 	/* VERIFY_OFFSET(reserved, 48 */
8356dd0e19f3SScott Teel 	VERIFY_OFFSET(data, 64);
8357dd0e19f3SScott Teel 
8358dd0e19f3SScott Teel #undef VERIFY_OFFSET
8359dd0e19f3SScott Teel 
8360dd0e19f3SScott Teel #define VERIFY_OFFSET(member, offset) \
8361b66cc250SMike Miller 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
8362b66cc250SMike Miller 
8363b66cc250SMike Miller 	VERIFY_OFFSET(IU_type, 0);
8364b66cc250SMike Miller 	VERIFY_OFFSET(direction, 1);
8365b66cc250SMike Miller 	VERIFY_OFFSET(reply_queue, 2);
8366b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved1, 3);  */
8367b66cc250SMike Miller 	VERIFY_OFFSET(scsi_nexus, 4);
8368b66cc250SMike Miller 	VERIFY_OFFSET(Tag, 8);
8369b66cc250SMike Miller 	VERIFY_OFFSET(cdb, 16);
8370b66cc250SMike Miller 	VERIFY_OFFSET(cciss_lun, 32);
8371b66cc250SMike Miller 	VERIFY_OFFSET(data_len, 40);
8372b66cc250SMike Miller 	VERIFY_OFFSET(cmd_priority_task_attr, 44);
8373b66cc250SMike Miller 	VERIFY_OFFSET(sg_count, 45);
8374b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved3 */
8375b66cc250SMike Miller 	VERIFY_OFFSET(err_ptr, 48);
8376b66cc250SMike Miller 	VERIFY_OFFSET(err_len, 56);
8377b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved4  */
8378b66cc250SMike Miller 	VERIFY_OFFSET(sg, 64);
8379b66cc250SMike Miller 
8380b66cc250SMike Miller #undef VERIFY_OFFSET
8381b66cc250SMike Miller 
8382b66cc250SMike Miller #define VERIFY_OFFSET(member, offset) \
8383e1f7de0cSMatt Gates 	BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
8384e1f7de0cSMatt Gates 
8385e1f7de0cSMatt Gates 	VERIFY_OFFSET(dev_handle, 0x00);
8386e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved1, 0x02);
8387e1f7de0cSMatt Gates 	VERIFY_OFFSET(function, 0x03);
8388e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved2, 0x04);
8389e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info, 0x0C);
8390e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved3, 0x10);
8391e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info_len, 0x12);
8392e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved4, 0x13);
8393e1f7de0cSMatt Gates 	VERIFY_OFFSET(sgl_offset, 0x14);
8394e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved5, 0x15);
8395e1f7de0cSMatt Gates 	VERIFY_OFFSET(transfer_len, 0x1C);
8396e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved6, 0x20);
8397e1f7de0cSMatt Gates 	VERIFY_OFFSET(io_flags, 0x24);
8398e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved7, 0x26);
8399e1f7de0cSMatt Gates 	VERIFY_OFFSET(LUN, 0x34);
8400e1f7de0cSMatt Gates 	VERIFY_OFFSET(control, 0x3C);
8401e1f7de0cSMatt Gates 	VERIFY_OFFSET(CDB, 0x40);
8402e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved8, 0x50);
8403e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_context_flags, 0x60);
8404e1f7de0cSMatt Gates 	VERIFY_OFFSET(timeout_sec, 0x62);
8405e1f7de0cSMatt Gates 	VERIFY_OFFSET(ReplyQueue, 0x64);
8406e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved9, 0x65);
840750a0decfSStephen M. Cameron 	VERIFY_OFFSET(tag, 0x68);
8408e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_addr, 0x70);
8409e1f7de0cSMatt Gates 	VERIFY_OFFSET(CISS_LUN, 0x78);
8410e1f7de0cSMatt Gates 	VERIFY_OFFSET(SG, 0x78 + 8);
8411e1f7de0cSMatt Gates #undef VERIFY_OFFSET
8412e1f7de0cSMatt Gates }
8413e1f7de0cSMatt Gates 
8414edd16368SStephen M. Cameron module_init(hpsa_init);
8415edd16368SStephen M. Cameron module_exit(hpsa_cleanup);
8416