xref: /openbmc/linux/drivers/scsi/hpsa.c (revision 39c53f55a950027fe8e94d56541e21f1a921f7ce)
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>
4773153fe5SWebb Scales #include <scsi/scsi_dbg.h>
48edd16368SStephen M. Cameron #include <linux/cciss_ioctl.h>
49edd16368SStephen M. Cameron #include <linux/string.h>
50edd16368SStephen M. Cameron #include <linux/bitmap.h>
5160063497SArun Sharma #include <linux/atomic.h>
52a0c12413SStephen M. Cameron #include <linux/jiffies.h>
5342a91641SDon Brace #include <linux/percpu-defs.h>
54094963daSStephen M. Cameron #include <linux/percpu.h>
552b08b3e9SDon Brace #include <asm/unaligned.h>
56283b4a9bSStephen M. Cameron #include <asm/div64.h>
57edd16368SStephen M. Cameron #include "hpsa_cmd.h"
58edd16368SStephen M. Cameron #include "hpsa.h"
59edd16368SStephen M. Cameron 
60edd16368SStephen M. Cameron /* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */
619a993302SStephen M. Cameron #define HPSA_DRIVER_VERSION "3.4.4-1"
62edd16368SStephen M. Cameron #define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
63f79cfec6SStephen M. Cameron #define HPSA "hpsa"
64edd16368SStephen M. Cameron 
65007e7aa9SRobert Elliott /* How long to wait for CISS doorbell communication */
66007e7aa9SRobert Elliott #define CLEAR_EVENT_WAIT_INTERVAL 20	/* ms for each msleep() call */
67007e7aa9SRobert Elliott #define MODE_CHANGE_WAIT_INTERVAL 10	/* ms for each msleep() call */
68007e7aa9SRobert Elliott #define MAX_CLEAR_EVENT_WAIT 30000	/* times 20 ms = 600 s */
69007e7aa9SRobert Elliott #define MAX_MODE_CHANGE_WAIT 2000	/* times 10 ms = 20 s */
70edd16368SStephen M. Cameron #define MAX_IOCTL_CONFIG_WAIT 1000
71edd16368SStephen M. Cameron 
72edd16368SStephen M. Cameron /*define how many times we will try a command because of bus resets */
73edd16368SStephen M. Cameron #define MAX_CMD_RETRIES 3
74edd16368SStephen M. Cameron 
75edd16368SStephen M. Cameron /* Embedded module documentation macros - see modules.h */
76edd16368SStephen M. Cameron MODULE_AUTHOR("Hewlett-Packard Company");
77edd16368SStephen M. Cameron MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
78edd16368SStephen M. Cameron 	HPSA_DRIVER_VERSION);
79edd16368SStephen M. Cameron MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
80edd16368SStephen M. Cameron MODULE_VERSION(HPSA_DRIVER_VERSION);
81edd16368SStephen M. Cameron MODULE_LICENSE("GPL");
82edd16368SStephen M. Cameron 
83edd16368SStephen M. Cameron static int hpsa_allow_any;
84edd16368SStephen M. Cameron module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
85edd16368SStephen M. Cameron MODULE_PARM_DESC(hpsa_allow_any,
86edd16368SStephen M. Cameron 		"Allow hpsa driver to access unknown HP Smart Array hardware");
8702ec19c8SStephen M. Cameron static int hpsa_simple_mode;
8802ec19c8SStephen M. Cameron module_param(hpsa_simple_mode, int, S_IRUGO|S_IWUSR);
8902ec19c8SStephen M. Cameron MODULE_PARM_DESC(hpsa_simple_mode,
9002ec19c8SStephen M. Cameron 	"Use 'simple mode' rather than 'performant mode'");
91edd16368SStephen M. Cameron 
92edd16368SStephen M. Cameron /* define the PCI info for the cards we can control */
93edd16368SStephen M. Cameron static const struct pci_device_id hpsa_pci_device_id[] = {
94edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3241},
95edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3243},
96edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3245},
97edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3247},
98edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3249},
99163dbcd8SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x324A},
100163dbcd8SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x324B},
101f8b01eb9SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3233},
1029143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3350},
1039143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3351},
1049143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3352},
1059143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3353},
1069143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3354},
1079143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3355},
1089143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3356},
109fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1921},
110fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1922},
111fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1923},
112fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1924},
113fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1926},
114fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1928},
11597b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1929},
11697b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BD},
11797b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BE},
11897b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BF},
11997b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C0},
12097b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C1},
12197b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C2},
12297b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C3},
12397b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C4},
12497b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C5},
1253b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C6},
12697b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C7},
12797b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C8},
12897b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C9},
1293b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CA},
1303b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CB},
1313b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CC},
1323b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CD},
1333b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CE},
1348e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0076},
1358e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0087},
1368e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x007D},
1378e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0088},
1388e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP, 0x333f, 0x103c, 0x333f},
139edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_ANY_ID,	PCI_ANY_ID, PCI_ANY_ID,
140edd16368SStephen M. Cameron 		PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
141edd16368SStephen M. Cameron 	{0,}
142edd16368SStephen M. Cameron };
143edd16368SStephen M. Cameron 
144edd16368SStephen M. Cameron MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
145edd16368SStephen M. Cameron 
146edd16368SStephen M. Cameron /*  board_id = Subsystem Device ID & Vendor ID
147edd16368SStephen M. Cameron  *  product = Marketing Name for the board
148edd16368SStephen M. Cameron  *  access = Address of the struct of function pointers
149edd16368SStephen M. Cameron  */
150edd16368SStephen M. Cameron static struct board_type products[] = {
151edd16368SStephen M. Cameron 	{0x3241103C, "Smart Array P212", &SA5_access},
152edd16368SStephen M. Cameron 	{0x3243103C, "Smart Array P410", &SA5_access},
153edd16368SStephen M. Cameron 	{0x3245103C, "Smart Array P410i", &SA5_access},
154edd16368SStephen M. Cameron 	{0x3247103C, "Smart Array P411", &SA5_access},
155edd16368SStephen M. Cameron 	{0x3249103C, "Smart Array P812", &SA5_access},
156163dbcd8SMike Miller 	{0x324A103C, "Smart Array P712m", &SA5_access},
157163dbcd8SMike Miller 	{0x324B103C, "Smart Array P711m", &SA5_access},
1587d2cce58SStephen M. Cameron 	{0x3233103C, "HP StorageWorks 1210m", &SA5_access}, /* alias of 333f */
159fe0c9610SMike Miller 	{0x3350103C, "Smart Array P222", &SA5_access},
160fe0c9610SMike Miller 	{0x3351103C, "Smart Array P420", &SA5_access},
161fe0c9610SMike Miller 	{0x3352103C, "Smart Array P421", &SA5_access},
162fe0c9610SMike Miller 	{0x3353103C, "Smart Array P822", &SA5_access},
163fe0c9610SMike Miller 	{0x3354103C, "Smart Array P420i", &SA5_access},
164fe0c9610SMike Miller 	{0x3355103C, "Smart Array P220i", &SA5_access},
165fe0c9610SMike Miller 	{0x3356103C, "Smart Array P721m", &SA5_access},
1661fd6c8e3SMike Miller 	{0x1921103C, "Smart Array P830i", &SA5_access},
1671fd6c8e3SMike Miller 	{0x1922103C, "Smart Array P430", &SA5_access},
1681fd6c8e3SMike Miller 	{0x1923103C, "Smart Array P431", &SA5_access},
1691fd6c8e3SMike Miller 	{0x1924103C, "Smart Array P830", &SA5_access},
1701fd6c8e3SMike Miller 	{0x1926103C, "Smart Array P731m", &SA5_access},
1711fd6c8e3SMike Miller 	{0x1928103C, "Smart Array P230i", &SA5_access},
1721fd6c8e3SMike Miller 	{0x1929103C, "Smart Array P530", &SA5_access},
17327fb8137SDon Brace 	{0x21BD103C, "Smart Array P244br", &SA5_access},
17427fb8137SDon Brace 	{0x21BE103C, "Smart Array P741m", &SA5_access},
17527fb8137SDon Brace 	{0x21BF103C, "Smart HBA H240ar", &SA5_access},
17627fb8137SDon Brace 	{0x21C0103C, "Smart Array P440ar", &SA5_access},
177c8ae0ab1SDon Brace 	{0x21C1103C, "Smart Array P840ar", &SA5_access},
17827fb8137SDon Brace 	{0x21C2103C, "Smart Array P440", &SA5_access},
17927fb8137SDon Brace 	{0x21C3103C, "Smart Array P441", &SA5_access},
18097b9f53dSMike Miller 	{0x21C4103C, "Smart Array", &SA5_access},
18127fb8137SDon Brace 	{0x21C5103C, "Smart Array P841", &SA5_access},
18227fb8137SDon Brace 	{0x21C6103C, "Smart HBA H244br", &SA5_access},
18327fb8137SDon Brace 	{0x21C7103C, "Smart HBA H240", &SA5_access},
18427fb8137SDon Brace 	{0x21C8103C, "Smart HBA H241", &SA5_access},
18597b9f53dSMike Miller 	{0x21C9103C, "Smart Array", &SA5_access},
18627fb8137SDon Brace 	{0x21CA103C, "Smart Array P246br", &SA5_access},
18727fb8137SDon Brace 	{0x21CB103C, "Smart Array P840", &SA5_access},
1883b7a45e5SJoe Handzik 	{0x21CC103C, "Smart Array", &SA5_access},
1893b7a45e5SJoe Handzik 	{0x21CD103C, "Smart Array", &SA5_access},
19027fb8137SDon Brace 	{0x21CE103C, "Smart HBA", &SA5_access},
1918e616a5eSStephen M. Cameron 	{0x00761590, "HP Storage P1224 Array Controller", &SA5_access},
1928e616a5eSStephen M. Cameron 	{0x00871590, "HP Storage P1224e Array Controller", &SA5_access},
1938e616a5eSStephen M. Cameron 	{0x007D1590, "HP Storage P1228 Array Controller", &SA5_access},
1948e616a5eSStephen M. Cameron 	{0x00881590, "HP Storage P1228e Array Controller", &SA5_access},
1958e616a5eSStephen M. Cameron 	{0x333f103c, "HP StorageWorks 1210m Array Controller", &SA5_access},
196edd16368SStephen M. Cameron 	{0xFFFF103C, "Unknown Smart Array", &SA5_access},
197edd16368SStephen M. Cameron };
198edd16368SStephen M. Cameron 
199a58e7e53SWebb Scales #define SCSI_CMD_BUSY ((struct scsi_cmnd *)&hpsa_cmd_busy)
200a58e7e53SWebb Scales static const struct scsi_cmnd hpsa_cmd_busy;
201a58e7e53SWebb Scales #define SCSI_CMD_IDLE ((struct scsi_cmnd *)&hpsa_cmd_idle)
202a58e7e53SWebb Scales static const struct scsi_cmnd hpsa_cmd_idle;
203edd16368SStephen M. Cameron static int number_of_controllers;
204edd16368SStephen M. Cameron 
20510f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id);
20610f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id);
20742a91641SDon Brace static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg);
208edd16368SStephen M. Cameron 
209edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
21042a91641SDon Brace static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd,
21142a91641SDon Brace 	void __user *arg);
212edd16368SStephen M. Cameron #endif
213edd16368SStephen M. Cameron 
214edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c);
215edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h);
21673153fe5SWebb Scales static void cmd_tagged_free(struct ctlr_info *h, struct CommandList *c);
21773153fe5SWebb Scales static struct CommandList *cmd_tagged_alloc(struct ctlr_info *h,
21873153fe5SWebb Scales 					    struct scsi_cmnd *scmd);
219a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
220b7bb24ebSStephen M. Cameron 	void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
221edd16368SStephen M. Cameron 	int cmd_type);
2222c143342SRobert Elliott static void hpsa_free_cmd_pool(struct ctlr_info *h);
223b7bb24ebSStephen M. Cameron #define VPD_PAGE (1 << 8)
224edd16368SStephen M. Cameron 
225f281233dSJeff Garzik static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
226a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *);
227a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
228a08a8471SStephen M. Cameron 	unsigned long elapsed_time);
2297c0a0229SDon Brace static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth);
230edd16368SStephen M. Cameron 
231edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
23275167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *scsicmd);
233edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev);
23441ce4c35SStephen Cameron static int hpsa_slave_configure(struct scsi_device *sdev);
235edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev);
236edd16368SStephen M. Cameron 
237edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
238edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
239edd16368SStephen M. Cameron 	struct CommandList *c);
240edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
241edd16368SStephen M. Cameron 	struct CommandList *c);
242303932fdSDon Brace /* performant mode helper functions */
243303932fdSDon Brace static void calc_bucket_map(int *bucket, int num_buckets,
2442b08b3e9SDon Brace 	int nsgs, int min_blocks, u32 *bucket_map);
245105a3dbcSRobert Elliott static void hpsa_free_performant_mode(struct ctlr_info *h);
246105a3dbcSRobert Elliott static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
247254f796bSMatt Gates static inline u32 next_command(struct ctlr_info *h, u8 q);
2486f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
2496f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
2501df8552aSStephen M. Cameron 			       u64 *cfg_offset);
2516f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
2521df8552aSStephen M. Cameron 				    unsigned long *memory_bar);
2536f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
2546f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
2556f039790SGreg Kroah-Hartman 				     int wait_for_ready);
25675167d2cSStephen M. Cameron static inline void finish_cmd(struct CommandList *c);
257c706a795SRobert Elliott static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h);
258fe5389c8SStephen M. Cameron #define BOARD_NOT_READY 0
259fe5389c8SStephen M. Cameron #define BOARD_READY 1
26023100dd9SStephen M. Cameron static void hpsa_drain_accel_commands(struct ctlr_info *h);
26176438d08SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h);
262c349775eSScott Teel static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
263c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
26403383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk);
265080ef1ccSDon Brace static void hpsa_command_resubmit_worker(struct work_struct *work);
26625163bd5SWebb Scales static u32 lockup_detected(struct ctlr_info *h);
26725163bd5SWebb Scales static int detect_controller_lockup(struct ctlr_info *h);
268edd16368SStephen M. Cameron 
269edd16368SStephen M. Cameron static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
270edd16368SStephen M. Cameron {
271edd16368SStephen M. Cameron 	unsigned long *priv = shost_priv(sdev->host);
272edd16368SStephen M. Cameron 	return (struct ctlr_info *) *priv;
273edd16368SStephen M. Cameron }
274edd16368SStephen M. Cameron 
275a23513e8SStephen M. Cameron static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
276a23513e8SStephen M. Cameron {
277a23513e8SStephen M. Cameron 	unsigned long *priv = shost_priv(sh);
278a23513e8SStephen M. Cameron 	return (struct ctlr_info *) *priv;
279a23513e8SStephen M. Cameron }
280a23513e8SStephen M. Cameron 
281a58e7e53SWebb Scales static inline bool hpsa_is_cmd_idle(struct CommandList *c)
282a58e7e53SWebb Scales {
283a58e7e53SWebb Scales 	return c->scsi_cmd == SCSI_CMD_IDLE;
284a58e7e53SWebb Scales }
285a58e7e53SWebb Scales 
2869437ac43SStephen Cameron /* extract sense key, asc, and ascq from sense data.  -1 means invalid. */
2879437ac43SStephen Cameron static void decode_sense_data(const u8 *sense_data, int sense_data_len,
2889437ac43SStephen Cameron 			u8 *sense_key, u8 *asc, u8 *ascq)
2899437ac43SStephen Cameron {
2909437ac43SStephen Cameron 	struct scsi_sense_hdr sshdr;
2919437ac43SStephen Cameron 	bool rc;
2929437ac43SStephen Cameron 
2939437ac43SStephen Cameron 	*sense_key = -1;
2949437ac43SStephen Cameron 	*asc = -1;
2959437ac43SStephen Cameron 	*ascq = -1;
2969437ac43SStephen Cameron 
2979437ac43SStephen Cameron 	if (sense_data_len < 1)
2989437ac43SStephen Cameron 		return;
2999437ac43SStephen Cameron 
3009437ac43SStephen Cameron 	rc = scsi_normalize_sense(sense_data, sense_data_len, &sshdr);
3019437ac43SStephen Cameron 	if (rc) {
3029437ac43SStephen Cameron 		*sense_key = sshdr.sense_key;
3039437ac43SStephen Cameron 		*asc = sshdr.asc;
3049437ac43SStephen Cameron 		*ascq = sshdr.ascq;
3059437ac43SStephen Cameron 	}
3069437ac43SStephen Cameron }
3079437ac43SStephen Cameron 
308edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
309edd16368SStephen M. Cameron 	struct CommandList *c)
310edd16368SStephen M. Cameron {
3119437ac43SStephen Cameron 	u8 sense_key, asc, ascq;
3129437ac43SStephen Cameron 	int sense_len;
3139437ac43SStephen Cameron 
3149437ac43SStephen Cameron 	if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
3159437ac43SStephen Cameron 		sense_len = sizeof(c->err_info->SenseInfo);
3169437ac43SStephen Cameron 	else
3179437ac43SStephen Cameron 		sense_len = c->err_info->SenseLen;
3189437ac43SStephen Cameron 
3199437ac43SStephen Cameron 	decode_sense_data(c->err_info->SenseInfo, sense_len,
3209437ac43SStephen Cameron 				&sense_key, &asc, &ascq);
3219437ac43SStephen Cameron 	if (sense_key != UNIT_ATTENTION || asc == -1)
322edd16368SStephen M. Cameron 		return 0;
323edd16368SStephen M. Cameron 
3249437ac43SStephen Cameron 	switch (asc) {
325edd16368SStephen M. Cameron 	case STATE_CHANGED:
3269437ac43SStephen Cameron 		dev_warn(&h->pdev->dev,
3272946e82bSRobert Elliott 			"%s: a state change detected, command retried\n",
3282946e82bSRobert Elliott 			h->devname);
329edd16368SStephen M. Cameron 		break;
330edd16368SStephen M. Cameron 	case LUN_FAILED:
3317f73695aSStephen M. Cameron 		dev_warn(&h->pdev->dev,
3322946e82bSRobert Elliott 			"%s: LUN failure detected\n", h->devname);
333edd16368SStephen M. Cameron 		break;
334edd16368SStephen M. Cameron 	case REPORT_LUNS_CHANGED:
3357f73695aSStephen M. Cameron 		dev_warn(&h->pdev->dev,
3362946e82bSRobert Elliott 			"%s: report LUN data changed\n", h->devname);
337edd16368SStephen M. Cameron 	/*
3384f4eb9f1SScott Teel 	 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
3394f4eb9f1SScott Teel 	 * target (array) devices.
340edd16368SStephen M. Cameron 	 */
341edd16368SStephen M. Cameron 		break;
342edd16368SStephen M. Cameron 	case POWER_OR_RESET:
3432946e82bSRobert Elliott 		dev_warn(&h->pdev->dev,
3442946e82bSRobert Elliott 			"%s: a power on or device reset detected\n",
3452946e82bSRobert Elliott 			h->devname);
346edd16368SStephen M. Cameron 		break;
347edd16368SStephen M. Cameron 	case UNIT_ATTENTION_CLEARED:
3482946e82bSRobert Elliott 		dev_warn(&h->pdev->dev,
3492946e82bSRobert Elliott 			"%s: unit attention cleared by another initiator\n",
3502946e82bSRobert Elliott 			h->devname);
351edd16368SStephen M. Cameron 		break;
352edd16368SStephen M. Cameron 	default:
3532946e82bSRobert Elliott 		dev_warn(&h->pdev->dev,
3542946e82bSRobert Elliott 			"%s: unknown unit attention detected\n",
3552946e82bSRobert Elliott 			h->devname);
356edd16368SStephen M. Cameron 		break;
357edd16368SStephen M. Cameron 	}
358edd16368SStephen M. Cameron 	return 1;
359edd16368SStephen M. Cameron }
360edd16368SStephen M. Cameron 
361852af20aSMatt Bondurant static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
362852af20aSMatt Bondurant {
363852af20aSMatt Bondurant 	if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
364852af20aSMatt Bondurant 		(c->err_info->ScsiStatus != SAM_STAT_BUSY &&
365852af20aSMatt Bondurant 		 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
366852af20aSMatt Bondurant 		return 0;
367852af20aSMatt Bondurant 	dev_warn(&h->pdev->dev, HPSA "device busy");
368852af20aSMatt Bondurant 	return 1;
369852af20aSMatt Bondurant }
370852af20aSMatt Bondurant 
371e985c58fSStephen Cameron static u32 lockup_detected(struct ctlr_info *h);
372e985c58fSStephen Cameron static ssize_t host_show_lockup_detected(struct device *dev,
373e985c58fSStephen Cameron 		struct device_attribute *attr, char *buf)
374e985c58fSStephen Cameron {
375e985c58fSStephen Cameron 	int ld;
376e985c58fSStephen Cameron 	struct ctlr_info *h;
377e985c58fSStephen Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
378e985c58fSStephen Cameron 
379e985c58fSStephen Cameron 	h = shost_to_hba(shost);
380e985c58fSStephen Cameron 	ld = lockup_detected(h);
381e985c58fSStephen Cameron 
382e985c58fSStephen Cameron 	return sprintf(buf, "ld=%d\n", ld);
383e985c58fSStephen Cameron }
384e985c58fSStephen Cameron 
385da0697bdSScott Teel static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
386da0697bdSScott Teel 					 struct device_attribute *attr,
387da0697bdSScott Teel 					 const char *buf, size_t count)
388da0697bdSScott Teel {
389da0697bdSScott Teel 	int status, len;
390da0697bdSScott Teel 	struct ctlr_info *h;
391da0697bdSScott Teel 	struct Scsi_Host *shost = class_to_shost(dev);
392da0697bdSScott Teel 	char tmpbuf[10];
393da0697bdSScott Teel 
394da0697bdSScott Teel 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
395da0697bdSScott Teel 		return -EACCES;
396da0697bdSScott Teel 	len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
397da0697bdSScott Teel 	strncpy(tmpbuf, buf, len);
398da0697bdSScott Teel 	tmpbuf[len] = '\0';
399da0697bdSScott Teel 	if (sscanf(tmpbuf, "%d", &status) != 1)
400da0697bdSScott Teel 		return -EINVAL;
401da0697bdSScott Teel 	h = shost_to_hba(shost);
402da0697bdSScott Teel 	h->acciopath_status = !!status;
403da0697bdSScott Teel 	dev_warn(&h->pdev->dev,
404da0697bdSScott Teel 		"hpsa: HP SSD Smart Path %s via sysfs update.\n",
405da0697bdSScott Teel 		h->acciopath_status ? "enabled" : "disabled");
406da0697bdSScott Teel 	return count;
407da0697bdSScott Teel }
408da0697bdSScott Teel 
4092ba8bfc8SStephen M. Cameron static ssize_t host_store_raid_offload_debug(struct device *dev,
4102ba8bfc8SStephen M. Cameron 					 struct device_attribute *attr,
4112ba8bfc8SStephen M. Cameron 					 const char *buf, size_t count)
4122ba8bfc8SStephen M. Cameron {
4132ba8bfc8SStephen M. Cameron 	int debug_level, len;
4142ba8bfc8SStephen M. Cameron 	struct ctlr_info *h;
4152ba8bfc8SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
4162ba8bfc8SStephen M. Cameron 	char tmpbuf[10];
4172ba8bfc8SStephen M. Cameron 
4182ba8bfc8SStephen M. Cameron 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
4192ba8bfc8SStephen M. Cameron 		return -EACCES;
4202ba8bfc8SStephen M. Cameron 	len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
4212ba8bfc8SStephen M. Cameron 	strncpy(tmpbuf, buf, len);
4222ba8bfc8SStephen M. Cameron 	tmpbuf[len] = '\0';
4232ba8bfc8SStephen M. Cameron 	if (sscanf(tmpbuf, "%d", &debug_level) != 1)
4242ba8bfc8SStephen M. Cameron 		return -EINVAL;
4252ba8bfc8SStephen M. Cameron 	if (debug_level < 0)
4262ba8bfc8SStephen M. Cameron 		debug_level = 0;
4272ba8bfc8SStephen M. Cameron 	h = shost_to_hba(shost);
4282ba8bfc8SStephen M. Cameron 	h->raid_offload_debug = debug_level;
4292ba8bfc8SStephen M. Cameron 	dev_warn(&h->pdev->dev, "hpsa: Set raid_offload_debug level = %d\n",
4302ba8bfc8SStephen M. Cameron 		h->raid_offload_debug);
4312ba8bfc8SStephen M. Cameron 	return count;
4322ba8bfc8SStephen M. Cameron }
4332ba8bfc8SStephen M. Cameron 
434edd16368SStephen M. Cameron static ssize_t host_store_rescan(struct device *dev,
435edd16368SStephen M. Cameron 				 struct device_attribute *attr,
436edd16368SStephen M. Cameron 				 const char *buf, size_t count)
437edd16368SStephen M. Cameron {
438edd16368SStephen M. Cameron 	struct ctlr_info *h;
439edd16368SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
440a23513e8SStephen M. Cameron 	h = shost_to_hba(shost);
44131468401SMike Miller 	hpsa_scan_start(h->scsi_host);
442edd16368SStephen M. Cameron 	return count;
443edd16368SStephen M. Cameron }
444edd16368SStephen M. Cameron 
445d28ce020SStephen M. Cameron static ssize_t host_show_firmware_revision(struct device *dev,
446d28ce020SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
447d28ce020SStephen M. Cameron {
448d28ce020SStephen M. Cameron 	struct ctlr_info *h;
449d28ce020SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
450d28ce020SStephen M. Cameron 	unsigned char *fwrev;
451d28ce020SStephen M. Cameron 
452d28ce020SStephen M. Cameron 	h = shost_to_hba(shost);
453d28ce020SStephen M. Cameron 	if (!h->hba_inquiry_data)
454d28ce020SStephen M. Cameron 		return 0;
455d28ce020SStephen M. Cameron 	fwrev = &h->hba_inquiry_data[32];
456d28ce020SStephen M. Cameron 	return snprintf(buf, 20, "%c%c%c%c\n",
457d28ce020SStephen M. Cameron 		fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
458d28ce020SStephen M. Cameron }
459d28ce020SStephen M. Cameron 
46094a13649SStephen M. Cameron static ssize_t host_show_commands_outstanding(struct device *dev,
46194a13649SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
46294a13649SStephen M. Cameron {
46394a13649SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
46494a13649SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(shost);
46594a13649SStephen M. Cameron 
4660cbf768eSStephen M. Cameron 	return snprintf(buf, 20, "%d\n",
4670cbf768eSStephen M. Cameron 			atomic_read(&h->commands_outstanding));
46894a13649SStephen M. Cameron }
46994a13649SStephen M. Cameron 
470745a7a25SStephen M. Cameron static ssize_t host_show_transport_mode(struct device *dev,
471745a7a25SStephen M. Cameron 	struct device_attribute *attr, char *buf)
472745a7a25SStephen M. Cameron {
473745a7a25SStephen M. Cameron 	struct ctlr_info *h;
474745a7a25SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
475745a7a25SStephen M. Cameron 
476745a7a25SStephen M. Cameron 	h = shost_to_hba(shost);
477745a7a25SStephen M. Cameron 	return snprintf(buf, 20, "%s\n",
478960a30e7SStephen M. Cameron 		h->transMethod & CFGTBL_Trans_Performant ?
479745a7a25SStephen M. Cameron 			"performant" : "simple");
480745a7a25SStephen M. Cameron }
481745a7a25SStephen M. Cameron 
482da0697bdSScott Teel static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
483da0697bdSScott Teel 	struct device_attribute *attr, char *buf)
484da0697bdSScott Teel {
485da0697bdSScott Teel 	struct ctlr_info *h;
486da0697bdSScott Teel 	struct Scsi_Host *shost = class_to_shost(dev);
487da0697bdSScott Teel 
488da0697bdSScott Teel 	h = shost_to_hba(shost);
489da0697bdSScott Teel 	return snprintf(buf, 30, "HP SSD Smart Path %s\n",
490da0697bdSScott Teel 		(h->acciopath_status == 1) ?  "enabled" : "disabled");
491da0697bdSScott Teel }
492da0697bdSScott Teel 
49346380786SStephen M. Cameron /* List of controllers which cannot be hard reset on kexec with reset_devices */
494941b1cdaSStephen M. Cameron static u32 unresettable_controller[] = {
495941b1cdaSStephen M. Cameron 	0x324a103C, /* Smart Array P712m */
496941b1cdaSStephen M. Cameron 	0x324b103C, /* Smart Array P711m */
497941b1cdaSStephen M. Cameron 	0x3223103C, /* Smart Array P800 */
498941b1cdaSStephen M. Cameron 	0x3234103C, /* Smart Array P400 */
499941b1cdaSStephen M. Cameron 	0x3235103C, /* Smart Array P400i */
500941b1cdaSStephen M. Cameron 	0x3211103C, /* Smart Array E200i */
501941b1cdaSStephen M. Cameron 	0x3212103C, /* Smart Array E200 */
502941b1cdaSStephen M. Cameron 	0x3213103C, /* Smart Array E200i */
503941b1cdaSStephen M. Cameron 	0x3214103C, /* Smart Array E200i */
504941b1cdaSStephen M. Cameron 	0x3215103C, /* Smart Array E200i */
505941b1cdaSStephen M. Cameron 	0x3237103C, /* Smart Array E500 */
506941b1cdaSStephen M. Cameron 	0x323D103C, /* Smart Array P700m */
5077af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
508941b1cdaSStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
509941b1cdaSStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
5105a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
5115a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
5125a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
5135a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
5145a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
5155a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
516941b1cdaSStephen M. Cameron };
517941b1cdaSStephen M. Cameron 
51846380786SStephen M. Cameron /* List of controllers which cannot even be soft reset */
51946380786SStephen M. Cameron static u32 soft_unresettable_controller[] = {
5207af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
5215a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
5225a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
5235a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
5245a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
5255a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
5265a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
52746380786SStephen M. Cameron 	/* Exclude 640x boards.  These are two pci devices in one slot
52846380786SStephen M. Cameron 	 * which share a battery backed cache module.  One controls the
52946380786SStephen M. Cameron 	 * cache, the other accesses the cache through the one that controls
53046380786SStephen M. Cameron 	 * it.  If we reset the one controlling the cache, the other will
53146380786SStephen M. Cameron 	 * likely not be happy.  Just forbid resetting this conjoined mess.
53246380786SStephen M. Cameron 	 * The 640x isn't really supported by hpsa anyway.
53346380786SStephen M. Cameron 	 */
53446380786SStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
53546380786SStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
53646380786SStephen M. Cameron };
53746380786SStephen M. Cameron 
5389b5c48c2SStephen Cameron static u32 needs_abort_tags_swizzled[] = {
5399b5c48c2SStephen Cameron 	0x323D103C, /* Smart Array P700m */
5409b5c48c2SStephen Cameron 	0x324a103C, /* Smart Array P712m */
5419b5c48c2SStephen Cameron 	0x324b103C, /* SmartArray P711m */
5429b5c48c2SStephen Cameron };
5439b5c48c2SStephen Cameron 
5449b5c48c2SStephen Cameron static int board_id_in_array(u32 a[], int nelems, u32 board_id)
545941b1cdaSStephen M. Cameron {
546941b1cdaSStephen M. Cameron 	int i;
547941b1cdaSStephen M. Cameron 
5489b5c48c2SStephen Cameron 	for (i = 0; i < nelems; i++)
5499b5c48c2SStephen Cameron 		if (a[i] == board_id)
550941b1cdaSStephen M. Cameron 			return 1;
5519b5c48c2SStephen Cameron 	return 0;
5529b5c48c2SStephen Cameron }
5539b5c48c2SStephen Cameron 
5549b5c48c2SStephen Cameron static int ctlr_is_hard_resettable(u32 board_id)
5559b5c48c2SStephen Cameron {
5569b5c48c2SStephen Cameron 	return !board_id_in_array(unresettable_controller,
5579b5c48c2SStephen Cameron 			ARRAY_SIZE(unresettable_controller), board_id);
558941b1cdaSStephen M. Cameron }
559941b1cdaSStephen M. Cameron 
56046380786SStephen M. Cameron static int ctlr_is_soft_resettable(u32 board_id)
56146380786SStephen M. Cameron {
5629b5c48c2SStephen Cameron 	return !board_id_in_array(soft_unresettable_controller,
5639b5c48c2SStephen Cameron 			ARRAY_SIZE(soft_unresettable_controller), board_id);
56446380786SStephen M. Cameron }
56546380786SStephen M. Cameron 
56646380786SStephen M. Cameron static int ctlr_is_resettable(u32 board_id)
56746380786SStephen M. Cameron {
56846380786SStephen M. Cameron 	return ctlr_is_hard_resettable(board_id) ||
56946380786SStephen M. Cameron 		ctlr_is_soft_resettable(board_id);
57046380786SStephen M. Cameron }
57146380786SStephen M. Cameron 
5729b5c48c2SStephen Cameron static int ctlr_needs_abort_tags_swizzled(u32 board_id)
5739b5c48c2SStephen Cameron {
5749b5c48c2SStephen Cameron 	return board_id_in_array(needs_abort_tags_swizzled,
5759b5c48c2SStephen Cameron 			ARRAY_SIZE(needs_abort_tags_swizzled), board_id);
5769b5c48c2SStephen Cameron }
5779b5c48c2SStephen Cameron 
578941b1cdaSStephen M. Cameron static ssize_t host_show_resettable(struct device *dev,
579941b1cdaSStephen M. Cameron 	struct device_attribute *attr, char *buf)
580941b1cdaSStephen M. Cameron {
581941b1cdaSStephen M. Cameron 	struct ctlr_info *h;
582941b1cdaSStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
583941b1cdaSStephen M. Cameron 
584941b1cdaSStephen M. Cameron 	h = shost_to_hba(shost);
58546380786SStephen M. Cameron 	return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
586941b1cdaSStephen M. Cameron }
587941b1cdaSStephen M. Cameron 
588edd16368SStephen M. Cameron static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
589edd16368SStephen M. Cameron {
590edd16368SStephen M. Cameron 	return (scsi3addr[3] & 0xC0) == 0x40;
591edd16368SStephen M. Cameron }
592edd16368SStephen M. Cameron 
593f2ef0ce7SRobert Elliott static const char * const raid_label[] = { "0", "4", "1(+0)", "5", "5+1", "6",
594f2ef0ce7SRobert Elliott 	"1(+0)ADM", "UNKNOWN"
595edd16368SStephen M. Cameron };
5966b80b18fSScott Teel #define HPSA_RAID_0	0
5976b80b18fSScott Teel #define HPSA_RAID_4	1
5986b80b18fSScott Teel #define HPSA_RAID_1	2	/* also used for RAID 10 */
5996b80b18fSScott Teel #define HPSA_RAID_5	3	/* also used for RAID 50 */
6006b80b18fSScott Teel #define HPSA_RAID_51	4
6016b80b18fSScott Teel #define HPSA_RAID_6	5	/* also used for RAID 60 */
6026b80b18fSScott Teel #define HPSA_RAID_ADM	6	/* also used for RAID 1+0 ADM */
603edd16368SStephen M. Cameron #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
604edd16368SStephen M. Cameron 
605edd16368SStephen M. Cameron static ssize_t raid_level_show(struct device *dev,
606edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
607edd16368SStephen M. Cameron {
608edd16368SStephen M. Cameron 	ssize_t l = 0;
60982a72c0aSStephen M. Cameron 	unsigned char rlevel;
610edd16368SStephen M. Cameron 	struct ctlr_info *h;
611edd16368SStephen M. Cameron 	struct scsi_device *sdev;
612edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
613edd16368SStephen M. Cameron 	unsigned long flags;
614edd16368SStephen M. Cameron 
615edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
616edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
617edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
618edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
619edd16368SStephen M. Cameron 	if (!hdev) {
620edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
621edd16368SStephen M. Cameron 		return -ENODEV;
622edd16368SStephen M. Cameron 	}
623edd16368SStephen M. Cameron 
624edd16368SStephen M. Cameron 	/* Is this even a logical drive? */
625edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
626edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
627edd16368SStephen M. Cameron 		l = snprintf(buf, PAGE_SIZE, "N/A\n");
628edd16368SStephen M. Cameron 		return l;
629edd16368SStephen M. Cameron 	}
630edd16368SStephen M. Cameron 
631edd16368SStephen M. Cameron 	rlevel = hdev->raid_level;
632edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
63382a72c0aSStephen M. Cameron 	if (rlevel > RAID_UNKNOWN)
634edd16368SStephen M. Cameron 		rlevel = RAID_UNKNOWN;
635edd16368SStephen M. Cameron 	l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
636edd16368SStephen M. Cameron 	return l;
637edd16368SStephen M. Cameron }
638edd16368SStephen M. Cameron 
639edd16368SStephen M. Cameron static ssize_t lunid_show(struct device *dev,
640edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
641edd16368SStephen M. Cameron {
642edd16368SStephen M. Cameron 	struct ctlr_info *h;
643edd16368SStephen M. Cameron 	struct scsi_device *sdev;
644edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
645edd16368SStephen M. Cameron 	unsigned long flags;
646edd16368SStephen M. Cameron 	unsigned char lunid[8];
647edd16368SStephen M. Cameron 
648edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
649edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
650edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
651edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
652edd16368SStephen M. Cameron 	if (!hdev) {
653edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
654edd16368SStephen M. Cameron 		return -ENODEV;
655edd16368SStephen M. Cameron 	}
656edd16368SStephen M. Cameron 	memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
657edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
658edd16368SStephen M. Cameron 	return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
659edd16368SStephen M. Cameron 		lunid[0], lunid[1], lunid[2], lunid[3],
660edd16368SStephen M. Cameron 		lunid[4], lunid[5], lunid[6], lunid[7]);
661edd16368SStephen M. Cameron }
662edd16368SStephen M. Cameron 
663edd16368SStephen M. Cameron static ssize_t unique_id_show(struct device *dev,
664edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
665edd16368SStephen M. Cameron {
666edd16368SStephen M. Cameron 	struct ctlr_info *h;
667edd16368SStephen M. Cameron 	struct scsi_device *sdev;
668edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
669edd16368SStephen M. Cameron 	unsigned long flags;
670edd16368SStephen M. Cameron 	unsigned char sn[16];
671edd16368SStephen M. Cameron 
672edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
673edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
674edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
675edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
676edd16368SStephen M. Cameron 	if (!hdev) {
677edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
678edd16368SStephen M. Cameron 		return -ENODEV;
679edd16368SStephen M. Cameron 	}
680edd16368SStephen M. Cameron 	memcpy(sn, hdev->device_id, sizeof(sn));
681edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
682edd16368SStephen M. Cameron 	return snprintf(buf, 16 * 2 + 2,
683edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X"
684edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X\n",
685edd16368SStephen M. Cameron 			sn[0], sn[1], sn[2], sn[3],
686edd16368SStephen M. Cameron 			sn[4], sn[5], sn[6], sn[7],
687edd16368SStephen M. Cameron 			sn[8], sn[9], sn[10], sn[11],
688edd16368SStephen M. Cameron 			sn[12], sn[13], sn[14], sn[15]);
689edd16368SStephen M. Cameron }
690edd16368SStephen M. Cameron 
691c1988684SScott Teel static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
692c1988684SScott Teel 	     struct device_attribute *attr, char *buf)
693c1988684SScott Teel {
694c1988684SScott Teel 	struct ctlr_info *h;
695c1988684SScott Teel 	struct scsi_device *sdev;
696c1988684SScott Teel 	struct hpsa_scsi_dev_t *hdev;
697c1988684SScott Teel 	unsigned long flags;
698c1988684SScott Teel 	int offload_enabled;
699c1988684SScott Teel 
700c1988684SScott Teel 	sdev = to_scsi_device(dev);
701c1988684SScott Teel 	h = sdev_to_hba(sdev);
702c1988684SScott Teel 	spin_lock_irqsave(&h->lock, flags);
703c1988684SScott Teel 	hdev = sdev->hostdata;
704c1988684SScott Teel 	if (!hdev) {
705c1988684SScott Teel 		spin_unlock_irqrestore(&h->lock, flags);
706c1988684SScott Teel 		return -ENODEV;
707c1988684SScott Teel 	}
708c1988684SScott Teel 	offload_enabled = hdev->offload_enabled;
709c1988684SScott Teel 	spin_unlock_irqrestore(&h->lock, flags);
710c1988684SScott Teel 	return snprintf(buf, 20, "%d\n", offload_enabled);
711c1988684SScott Teel }
712c1988684SScott Teel 
7133f5eac3aSStephen M. Cameron static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
7143f5eac3aSStephen M. Cameron static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
7153f5eac3aSStephen M. Cameron static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
7163f5eac3aSStephen M. Cameron static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
717c1988684SScott Teel static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
718c1988684SScott Teel 			host_show_hp_ssd_smart_path_enabled, NULL);
719da0697bdSScott Teel static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
720da0697bdSScott Teel 		host_show_hp_ssd_smart_path_status,
721da0697bdSScott Teel 		host_store_hp_ssd_smart_path_status);
7222ba8bfc8SStephen M. Cameron static DEVICE_ATTR(raid_offload_debug, S_IWUSR, NULL,
7232ba8bfc8SStephen M. Cameron 			host_store_raid_offload_debug);
7243f5eac3aSStephen M. Cameron static DEVICE_ATTR(firmware_revision, S_IRUGO,
7253f5eac3aSStephen M. Cameron 	host_show_firmware_revision, NULL);
7263f5eac3aSStephen M. Cameron static DEVICE_ATTR(commands_outstanding, S_IRUGO,
7273f5eac3aSStephen M. Cameron 	host_show_commands_outstanding, NULL);
7283f5eac3aSStephen M. Cameron static DEVICE_ATTR(transport_mode, S_IRUGO,
7293f5eac3aSStephen M. Cameron 	host_show_transport_mode, NULL);
730941b1cdaSStephen M. Cameron static DEVICE_ATTR(resettable, S_IRUGO,
731941b1cdaSStephen M. Cameron 	host_show_resettable, NULL);
732e985c58fSStephen Cameron static DEVICE_ATTR(lockup_detected, S_IRUGO,
733e985c58fSStephen Cameron 	host_show_lockup_detected, NULL);
7343f5eac3aSStephen M. Cameron 
7353f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_sdev_attrs[] = {
7363f5eac3aSStephen M. Cameron 	&dev_attr_raid_level,
7373f5eac3aSStephen M. Cameron 	&dev_attr_lunid,
7383f5eac3aSStephen M. Cameron 	&dev_attr_unique_id,
739c1988684SScott Teel 	&dev_attr_hp_ssd_smart_path_enabled,
740e985c58fSStephen Cameron 	&dev_attr_lockup_detected,
7413f5eac3aSStephen M. Cameron 	NULL,
7423f5eac3aSStephen M. Cameron };
7433f5eac3aSStephen M. Cameron 
7443f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_shost_attrs[] = {
7453f5eac3aSStephen M. Cameron 	&dev_attr_rescan,
7463f5eac3aSStephen M. Cameron 	&dev_attr_firmware_revision,
7473f5eac3aSStephen M. Cameron 	&dev_attr_commands_outstanding,
7483f5eac3aSStephen M. Cameron 	&dev_attr_transport_mode,
749941b1cdaSStephen M. Cameron 	&dev_attr_resettable,
750da0697bdSScott Teel 	&dev_attr_hp_ssd_smart_path_status,
7512ba8bfc8SStephen M. Cameron 	&dev_attr_raid_offload_debug,
7523f5eac3aSStephen M. Cameron 	NULL,
7533f5eac3aSStephen M. Cameron };
7543f5eac3aSStephen M. Cameron 
75541ce4c35SStephen Cameron #define HPSA_NRESERVED_CMDS	(HPSA_CMDS_RESERVED_FOR_ABORTS + \
75641ce4c35SStephen Cameron 		HPSA_CMDS_RESERVED_FOR_DRIVER + HPSA_MAX_CONCURRENT_PASSTHRUS)
75741ce4c35SStephen Cameron 
7583f5eac3aSStephen M. Cameron static struct scsi_host_template hpsa_driver_template = {
7593f5eac3aSStephen M. Cameron 	.module			= THIS_MODULE,
760f79cfec6SStephen M. Cameron 	.name			= HPSA,
761f79cfec6SStephen M. Cameron 	.proc_name		= HPSA,
7623f5eac3aSStephen M. Cameron 	.queuecommand		= hpsa_scsi_queue_command,
7633f5eac3aSStephen M. Cameron 	.scan_start		= hpsa_scan_start,
7643f5eac3aSStephen M. Cameron 	.scan_finished		= hpsa_scan_finished,
7657c0a0229SDon Brace 	.change_queue_depth	= hpsa_change_queue_depth,
7663f5eac3aSStephen M. Cameron 	.this_id		= -1,
7673f5eac3aSStephen M. Cameron 	.use_clustering		= ENABLE_CLUSTERING,
76875167d2cSStephen M. Cameron 	.eh_abort_handler	= hpsa_eh_abort_handler,
7693f5eac3aSStephen M. Cameron 	.eh_device_reset_handler = hpsa_eh_device_reset_handler,
7703f5eac3aSStephen M. Cameron 	.ioctl			= hpsa_ioctl,
7713f5eac3aSStephen M. Cameron 	.slave_alloc		= hpsa_slave_alloc,
77241ce4c35SStephen Cameron 	.slave_configure	= hpsa_slave_configure,
7733f5eac3aSStephen M. Cameron 	.slave_destroy		= hpsa_slave_destroy,
7743f5eac3aSStephen M. Cameron #ifdef CONFIG_COMPAT
7753f5eac3aSStephen M. Cameron 	.compat_ioctl		= hpsa_compat_ioctl,
7763f5eac3aSStephen M. Cameron #endif
7773f5eac3aSStephen M. Cameron 	.sdev_attrs = hpsa_sdev_attrs,
7783f5eac3aSStephen M. Cameron 	.shost_attrs = hpsa_shost_attrs,
779c0d6a4d1SStephen M. Cameron 	.max_sectors = 8192,
78054b2b50cSMartin K. Petersen 	.no_write_same = 1,
7813f5eac3aSStephen M. Cameron };
7823f5eac3aSStephen M. Cameron 
783254f796bSMatt Gates static inline u32 next_command(struct ctlr_info *h, u8 q)
7843f5eac3aSStephen M. Cameron {
7853f5eac3aSStephen M. Cameron 	u32 a;
786072b0518SStephen M. Cameron 	struct reply_queue_buffer *rq = &h->reply_queue[q];
7873f5eac3aSStephen M. Cameron 
788e1f7de0cSMatt Gates 	if (h->transMethod & CFGTBL_Trans_io_accel1)
789e1f7de0cSMatt Gates 		return h->access.command_completed(h, q);
790e1f7de0cSMatt Gates 
7913f5eac3aSStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
792254f796bSMatt Gates 		return h->access.command_completed(h, q);
7933f5eac3aSStephen M. Cameron 
794254f796bSMatt Gates 	if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
795254f796bSMatt Gates 		a = rq->head[rq->current_entry];
796254f796bSMatt Gates 		rq->current_entry++;
7970cbf768eSStephen M. Cameron 		atomic_dec(&h->commands_outstanding);
7983f5eac3aSStephen M. Cameron 	} else {
7993f5eac3aSStephen M. Cameron 		a = FIFO_EMPTY;
8003f5eac3aSStephen M. Cameron 	}
8013f5eac3aSStephen M. Cameron 	/* Check for wraparound */
802254f796bSMatt Gates 	if (rq->current_entry == h->max_commands) {
803254f796bSMatt Gates 		rq->current_entry = 0;
804254f796bSMatt Gates 		rq->wraparound ^= 1;
8053f5eac3aSStephen M. Cameron 	}
8063f5eac3aSStephen M. Cameron 	return a;
8073f5eac3aSStephen M. Cameron }
8083f5eac3aSStephen M. Cameron 
809c349775eSScott Teel /*
810c349775eSScott Teel  * There are some special bits in the bus address of the
811c349775eSScott Teel  * command that we have to set for the controller to know
812c349775eSScott Teel  * how to process the command:
813c349775eSScott Teel  *
814c349775eSScott Teel  * Normal performant mode:
815c349775eSScott Teel  * bit 0: 1 means performant mode, 0 means simple mode.
816c349775eSScott Teel  * bits 1-3 = block fetch table entry
817c349775eSScott Teel  * bits 4-6 = command type (== 0)
818c349775eSScott Teel  *
819c349775eSScott Teel  * ioaccel1 mode:
820c349775eSScott Teel  * bit 0 = "performant mode" bit.
821c349775eSScott Teel  * bits 1-3 = block fetch table entry
822c349775eSScott Teel  * bits 4-6 = command type (== 110)
823c349775eSScott Teel  * (command type is needed because ioaccel1 mode
824c349775eSScott Teel  * commands are submitted through the same register as normal
825c349775eSScott Teel  * mode commands, so this is how the controller knows whether
826c349775eSScott Teel  * the command is normal mode or ioaccel1 mode.)
827c349775eSScott Teel  *
828c349775eSScott Teel  * ioaccel2 mode:
829c349775eSScott Teel  * bit 0 = "performant mode" bit.
830c349775eSScott Teel  * bits 1-4 = block fetch table entry (note extra bit)
831c349775eSScott Teel  * bits 4-6 = not needed, because ioaccel2 mode has
832c349775eSScott Teel  * a separate special register for submitting commands.
833c349775eSScott Teel  */
834c349775eSScott Teel 
83525163bd5SWebb Scales /*
83625163bd5SWebb Scales  * set_performant_mode: Modify the tag for cciss performant
8373f5eac3aSStephen M. Cameron  * set bit 0 for pull model, bits 3-1 for block fetch
8383f5eac3aSStephen M. Cameron  * register number
8393f5eac3aSStephen M. Cameron  */
84025163bd5SWebb Scales #define DEFAULT_REPLY_QUEUE (-1)
84125163bd5SWebb Scales static void set_performant_mode(struct ctlr_info *h, struct CommandList *c,
84225163bd5SWebb Scales 					int reply_queue)
8433f5eac3aSStephen M. Cameron {
844254f796bSMatt Gates 	if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
8453f5eac3aSStephen M. Cameron 		c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
84625163bd5SWebb Scales 		if (unlikely(!h->msix_vector))
84725163bd5SWebb Scales 			return;
84825163bd5SWebb Scales 		if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
849254f796bSMatt Gates 			c->Header.ReplyQueue =
850804a5cb5SJohn Kacur 				raw_smp_processor_id() % h->nreply_queues;
85125163bd5SWebb Scales 		else
85225163bd5SWebb Scales 			c->Header.ReplyQueue = reply_queue % h->nreply_queues;
853254f796bSMatt Gates 	}
8543f5eac3aSStephen M. Cameron }
8553f5eac3aSStephen M. Cameron 
856c349775eSScott Teel static void set_ioaccel1_performant_mode(struct ctlr_info *h,
85725163bd5SWebb Scales 						struct CommandList *c,
85825163bd5SWebb Scales 						int reply_queue)
859c349775eSScott Teel {
860c349775eSScott Teel 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
861c349775eSScott Teel 
86225163bd5SWebb Scales 	/*
86325163bd5SWebb Scales 	 * Tell the controller to post the reply to the queue for this
864c349775eSScott Teel 	 * processor.  This seems to give the best I/O throughput.
865c349775eSScott Teel 	 */
86625163bd5SWebb Scales 	if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
867c349775eSScott Teel 		cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
86825163bd5SWebb Scales 	else
86925163bd5SWebb Scales 		cp->ReplyQueue = reply_queue % h->nreply_queues;
87025163bd5SWebb Scales 	/*
87125163bd5SWebb Scales 	 * Set the bits in the address sent down to include:
872c349775eSScott Teel 	 *  - performant mode bit (bit 0)
873c349775eSScott Teel 	 *  - pull count (bits 1-3)
874c349775eSScott Teel 	 *  - command type (bits 4-6)
875c349775eSScott Teel 	 */
876c349775eSScott Teel 	c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
877c349775eSScott Teel 					IOACCEL1_BUSADDR_CMDTYPE;
878c349775eSScott Teel }
879c349775eSScott Teel 
8808be986ccSStephen Cameron static void set_ioaccel2_tmf_performant_mode(struct ctlr_info *h,
8818be986ccSStephen Cameron 						struct CommandList *c,
8828be986ccSStephen Cameron 						int reply_queue)
8838be986ccSStephen Cameron {
8848be986ccSStephen Cameron 	struct hpsa_tmf_struct *cp = (struct hpsa_tmf_struct *)
8858be986ccSStephen Cameron 		&h->ioaccel2_cmd_pool[c->cmdindex];
8868be986ccSStephen Cameron 
8878be986ccSStephen Cameron 	/* Tell the controller to post the reply to the queue for this
8888be986ccSStephen Cameron 	 * processor.  This seems to give the best I/O throughput.
8898be986ccSStephen Cameron 	 */
8908be986ccSStephen Cameron 	if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
8918be986ccSStephen Cameron 		cp->reply_queue = smp_processor_id() % h->nreply_queues;
8928be986ccSStephen Cameron 	else
8938be986ccSStephen Cameron 		cp->reply_queue = reply_queue % h->nreply_queues;
8948be986ccSStephen Cameron 	/* Set the bits in the address sent down to include:
8958be986ccSStephen Cameron 	 *  - performant mode bit not used in ioaccel mode 2
8968be986ccSStephen Cameron 	 *  - pull count (bits 0-3)
8978be986ccSStephen Cameron 	 *  - command type isn't needed for ioaccel2
8988be986ccSStephen Cameron 	 */
8998be986ccSStephen Cameron 	c->busaddr |= h->ioaccel2_blockFetchTable[0];
9008be986ccSStephen Cameron }
9018be986ccSStephen Cameron 
902c349775eSScott Teel static void set_ioaccel2_performant_mode(struct ctlr_info *h,
90325163bd5SWebb Scales 						struct CommandList *c,
90425163bd5SWebb Scales 						int reply_queue)
905c349775eSScott Teel {
906c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
907c349775eSScott Teel 
90825163bd5SWebb Scales 	/*
90925163bd5SWebb Scales 	 * Tell the controller to post the reply to the queue for this
910c349775eSScott Teel 	 * processor.  This seems to give the best I/O throughput.
911c349775eSScott Teel 	 */
91225163bd5SWebb Scales 	if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
913c349775eSScott Teel 		cp->reply_queue = smp_processor_id() % h->nreply_queues;
91425163bd5SWebb Scales 	else
91525163bd5SWebb Scales 		cp->reply_queue = reply_queue % h->nreply_queues;
91625163bd5SWebb Scales 	/*
91725163bd5SWebb Scales 	 * Set the bits in the address sent down to include:
918c349775eSScott Teel 	 *  - performant mode bit not used in ioaccel mode 2
919c349775eSScott Teel 	 *  - pull count (bits 0-3)
920c349775eSScott Teel 	 *  - command type isn't needed for ioaccel2
921c349775eSScott Teel 	 */
922c349775eSScott Teel 	c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
923c349775eSScott Teel }
924c349775eSScott Teel 
925e85c5974SStephen M. Cameron static int is_firmware_flash_cmd(u8 *cdb)
926e85c5974SStephen M. Cameron {
927e85c5974SStephen M. Cameron 	return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
928e85c5974SStephen M. Cameron }
929e85c5974SStephen M. Cameron 
930e85c5974SStephen M. Cameron /*
931e85c5974SStephen M. Cameron  * During firmware flash, the heartbeat register may not update as frequently
932e85c5974SStephen M. Cameron  * as it should.  So we dial down lockup detection during firmware flash. and
933e85c5974SStephen M. Cameron  * dial it back up when firmware flash completes.
934e85c5974SStephen M. Cameron  */
935e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
936e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
937e85c5974SStephen M. Cameron static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
938e85c5974SStephen M. Cameron 		struct CommandList *c)
939e85c5974SStephen M. Cameron {
940e85c5974SStephen M. Cameron 	if (!is_firmware_flash_cmd(c->Request.CDB))
941e85c5974SStephen M. Cameron 		return;
942e85c5974SStephen M. Cameron 	atomic_inc(&h->firmware_flash_in_progress);
943e85c5974SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
944e85c5974SStephen M. Cameron }
945e85c5974SStephen M. Cameron 
946e85c5974SStephen M. Cameron static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
947e85c5974SStephen M. Cameron 		struct CommandList *c)
948e85c5974SStephen M. Cameron {
949e85c5974SStephen M. Cameron 	if (is_firmware_flash_cmd(c->Request.CDB) &&
950e85c5974SStephen M. Cameron 		atomic_dec_and_test(&h->firmware_flash_in_progress))
951e85c5974SStephen M. Cameron 		h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
952e85c5974SStephen M. Cameron }
953e85c5974SStephen M. Cameron 
95425163bd5SWebb Scales static void __enqueue_cmd_and_start_io(struct ctlr_info *h,
95525163bd5SWebb Scales 	struct CommandList *c, int reply_queue)
9563f5eac3aSStephen M. Cameron {
957c05e8866SStephen Cameron 	dial_down_lockup_detection_during_fw_flash(h, c);
958c05e8866SStephen Cameron 	atomic_inc(&h->commands_outstanding);
959c349775eSScott Teel 	switch (c->cmd_type) {
960c349775eSScott Teel 	case CMD_IOACCEL1:
96125163bd5SWebb Scales 		set_ioaccel1_performant_mode(h, c, reply_queue);
962c05e8866SStephen Cameron 		writel(c->busaddr, h->vaddr + SA5_REQUEST_PORT_OFFSET);
963c349775eSScott Teel 		break;
964c349775eSScott Teel 	case CMD_IOACCEL2:
96525163bd5SWebb Scales 		set_ioaccel2_performant_mode(h, c, reply_queue);
966c05e8866SStephen Cameron 		writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
967c349775eSScott Teel 		break;
9688be986ccSStephen Cameron 	case IOACCEL2_TMF:
9698be986ccSStephen Cameron 		set_ioaccel2_tmf_performant_mode(h, c, reply_queue);
9708be986ccSStephen Cameron 		writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
9718be986ccSStephen Cameron 		break;
972c349775eSScott Teel 	default:
97325163bd5SWebb Scales 		set_performant_mode(h, c, reply_queue);
974f2405db8SDon Brace 		h->access.submit_command(h, c);
9753f5eac3aSStephen M. Cameron 	}
976c05e8866SStephen Cameron }
9773f5eac3aSStephen M. Cameron 
978a58e7e53SWebb Scales static void enqueue_cmd_and_start_io(struct ctlr_info *h, struct CommandList *c)
97925163bd5SWebb Scales {
980a58e7e53SWebb Scales 	if (unlikely(c->abort_pending))
981a58e7e53SWebb Scales 		return finish_cmd(c);
982a58e7e53SWebb Scales 
98325163bd5SWebb Scales 	__enqueue_cmd_and_start_io(h, c, DEFAULT_REPLY_QUEUE);
98425163bd5SWebb Scales }
98525163bd5SWebb Scales 
9863f5eac3aSStephen M. Cameron static inline int is_hba_lunid(unsigned char scsi3addr[])
9873f5eac3aSStephen M. Cameron {
9883f5eac3aSStephen M. Cameron 	return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
9893f5eac3aSStephen M. Cameron }
9903f5eac3aSStephen M. Cameron 
9913f5eac3aSStephen M. Cameron static inline int is_scsi_rev_5(struct ctlr_info *h)
9923f5eac3aSStephen M. Cameron {
9933f5eac3aSStephen M. Cameron 	if (!h->hba_inquiry_data)
9943f5eac3aSStephen M. Cameron 		return 0;
9953f5eac3aSStephen M. Cameron 	if ((h->hba_inquiry_data[2] & 0x07) == 5)
9963f5eac3aSStephen M. Cameron 		return 1;
9973f5eac3aSStephen M. Cameron 	return 0;
9983f5eac3aSStephen M. Cameron }
9993f5eac3aSStephen M. Cameron 
1000edd16368SStephen M. Cameron static int hpsa_find_target_lun(struct ctlr_info *h,
1001edd16368SStephen M. Cameron 	unsigned char scsi3addr[], int bus, int *target, int *lun)
1002edd16368SStephen M. Cameron {
1003edd16368SStephen M. Cameron 	/* finds an unused bus, target, lun for a new physical device
1004edd16368SStephen M. Cameron 	 * assumes h->devlock is held
1005edd16368SStephen M. Cameron 	 */
1006edd16368SStephen M. Cameron 	int i, found = 0;
1007cfe5badcSScott Teel 	DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
1008edd16368SStephen M. Cameron 
1009263d9401SAkinobu Mita 	bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
1010edd16368SStephen M. Cameron 
1011edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1012edd16368SStephen M. Cameron 		if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
1013263d9401SAkinobu Mita 			__set_bit(h->dev[i]->target, lun_taken);
1014edd16368SStephen M. Cameron 	}
1015edd16368SStephen M. Cameron 
1016263d9401SAkinobu Mita 	i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
1017263d9401SAkinobu Mita 	if (i < HPSA_MAX_DEVICES) {
1018edd16368SStephen M. Cameron 		/* *bus = 1; */
1019edd16368SStephen M. Cameron 		*target = i;
1020edd16368SStephen M. Cameron 		*lun = 0;
1021edd16368SStephen M. Cameron 		found = 1;
1022edd16368SStephen M. Cameron 	}
1023edd16368SStephen M. Cameron 	return !found;
1024edd16368SStephen M. Cameron }
1025edd16368SStephen M. Cameron 
10260d96ef5fSWebb Scales static inline void hpsa_show_dev_msg(const char *level, struct ctlr_info *h,
10270d96ef5fSWebb Scales 	struct hpsa_scsi_dev_t *dev, char *description)
10280d96ef5fSWebb Scales {
10290d96ef5fSWebb Scales 	dev_printk(level, &h->pdev->dev,
10300d96ef5fSWebb Scales 			"scsi %d:%d:%d:%d: %s %s %.8s %.16s RAID-%s SSDSmartPathCap%c En%c Exp=%d\n",
10310d96ef5fSWebb Scales 			h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
10320d96ef5fSWebb Scales 			description,
10330d96ef5fSWebb Scales 			scsi_device_type(dev->devtype),
10340d96ef5fSWebb Scales 			dev->vendor,
10350d96ef5fSWebb Scales 			dev->model,
10360d96ef5fSWebb Scales 			dev->raid_level > RAID_UNKNOWN ?
10370d96ef5fSWebb Scales 				"RAID-?" : raid_label[dev->raid_level],
10380d96ef5fSWebb Scales 			dev->offload_config ? '+' : '-',
10390d96ef5fSWebb Scales 			dev->offload_enabled ? '+' : '-',
10400d96ef5fSWebb Scales 			dev->expose_state);
10410d96ef5fSWebb Scales }
10420d96ef5fSWebb Scales 
1043edd16368SStephen M. Cameron /* Add an entry into h->dev[] array. */
1044edd16368SStephen M. Cameron static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
1045edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *device,
1046edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *added[], int *nadded)
1047edd16368SStephen M. Cameron {
1048edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
1049edd16368SStephen M. Cameron 	int n = h->ndevices;
1050edd16368SStephen M. Cameron 	int i;
1051edd16368SStephen M. Cameron 	unsigned char addr1[8], addr2[8];
1052edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1053edd16368SStephen M. Cameron 
1054cfe5badcSScott Teel 	if (n >= HPSA_MAX_DEVICES) {
1055edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "too many devices, some will be "
1056edd16368SStephen M. Cameron 			"inaccessible.\n");
1057edd16368SStephen M. Cameron 		return -1;
1058edd16368SStephen M. Cameron 	}
1059edd16368SStephen M. Cameron 
1060edd16368SStephen M. Cameron 	/* physical devices do not have lun or target assigned until now. */
1061edd16368SStephen M. Cameron 	if (device->lun != -1)
1062edd16368SStephen M. Cameron 		/* Logical device, lun is already assigned. */
1063edd16368SStephen M. Cameron 		goto lun_assigned;
1064edd16368SStephen M. Cameron 
1065edd16368SStephen M. Cameron 	/* If this device a non-zero lun of a multi-lun device
1066edd16368SStephen M. Cameron 	 * byte 4 of the 8-byte LUN addr will contain the logical
10672b08b3e9SDon Brace 	 * unit no, zero otherwise.
1068edd16368SStephen M. Cameron 	 */
1069edd16368SStephen M. Cameron 	if (device->scsi3addr[4] == 0) {
1070edd16368SStephen M. Cameron 		/* This is not a non-zero lun of a multi-lun device */
1071edd16368SStephen M. Cameron 		if (hpsa_find_target_lun(h, device->scsi3addr,
1072edd16368SStephen M. Cameron 			device->bus, &device->target, &device->lun) != 0)
1073edd16368SStephen M. Cameron 			return -1;
1074edd16368SStephen M. Cameron 		goto lun_assigned;
1075edd16368SStephen M. Cameron 	}
1076edd16368SStephen M. Cameron 
1077edd16368SStephen M. Cameron 	/* This is a non-zero lun of a multi-lun device.
1078edd16368SStephen M. Cameron 	 * Search through our list and find the device which
1079edd16368SStephen M. Cameron 	 * has the same 8 byte LUN address, excepting byte 4.
1080edd16368SStephen M. Cameron 	 * Assign the same bus and target for this new LUN.
1081edd16368SStephen M. Cameron 	 * Use the logical unit number from the firmware.
1082edd16368SStephen M. Cameron 	 */
1083edd16368SStephen M. Cameron 	memcpy(addr1, device->scsi3addr, 8);
1084edd16368SStephen M. Cameron 	addr1[4] = 0;
1085edd16368SStephen M. Cameron 	for (i = 0; i < n; i++) {
1086edd16368SStephen M. Cameron 		sd = h->dev[i];
1087edd16368SStephen M. Cameron 		memcpy(addr2, sd->scsi3addr, 8);
1088edd16368SStephen M. Cameron 		addr2[4] = 0;
1089edd16368SStephen M. Cameron 		/* differ only in byte 4? */
1090edd16368SStephen M. Cameron 		if (memcmp(addr1, addr2, 8) == 0) {
1091edd16368SStephen M. Cameron 			device->bus = sd->bus;
1092edd16368SStephen M. Cameron 			device->target = sd->target;
1093edd16368SStephen M. Cameron 			device->lun = device->scsi3addr[4];
1094edd16368SStephen M. Cameron 			break;
1095edd16368SStephen M. Cameron 		}
1096edd16368SStephen M. Cameron 	}
1097edd16368SStephen M. Cameron 	if (device->lun == -1) {
1098edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
1099edd16368SStephen M. Cameron 			" suspect firmware bug or unsupported hardware "
1100edd16368SStephen M. Cameron 			"configuration.\n");
1101edd16368SStephen M. Cameron 			return -1;
1102edd16368SStephen M. Cameron 	}
1103edd16368SStephen M. Cameron 
1104edd16368SStephen M. Cameron lun_assigned:
1105edd16368SStephen M. Cameron 
1106edd16368SStephen M. Cameron 	h->dev[n] = device;
1107edd16368SStephen M. Cameron 	h->ndevices++;
1108edd16368SStephen M. Cameron 	added[*nadded] = device;
1109edd16368SStephen M. Cameron 	(*nadded)++;
11100d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_INFO, h, device,
11110d96ef5fSWebb Scales 		device->expose_state & HPSA_SCSI_ADD ? "added" : "masked");
1112a473d86cSRobert Elliott 	device->offload_to_be_enabled = device->offload_enabled;
1113a473d86cSRobert Elliott 	device->offload_enabled = 0;
1114edd16368SStephen M. Cameron 	return 0;
1115edd16368SStephen M. Cameron }
1116edd16368SStephen M. Cameron 
1117bd9244f7SScott Teel /* Update an entry in h->dev[] array. */
1118bd9244f7SScott Teel static void hpsa_scsi_update_entry(struct ctlr_info *h, int hostno,
1119bd9244f7SScott Teel 	int entry, struct hpsa_scsi_dev_t *new_entry)
1120bd9244f7SScott Teel {
1121a473d86cSRobert Elliott 	int offload_enabled;
1122bd9244f7SScott Teel 	/* assumes h->devlock is held */
1123bd9244f7SScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
1124bd9244f7SScott Teel 
1125bd9244f7SScott Teel 	/* Raid level changed. */
1126bd9244f7SScott Teel 	h->dev[entry]->raid_level = new_entry->raid_level;
1127250fb125SStephen M. Cameron 
112803383736SDon Brace 	/* Raid offload parameters changed.  Careful about the ordering. */
112903383736SDon Brace 	if (new_entry->offload_config && new_entry->offload_enabled) {
113003383736SDon Brace 		/*
113103383736SDon Brace 		 * if drive is newly offload_enabled, we want to copy the
113203383736SDon Brace 		 * raid map data first.  If previously offload_enabled and
113303383736SDon Brace 		 * offload_config were set, raid map data had better be
113403383736SDon Brace 		 * the same as it was before.  if raid map data is changed
113503383736SDon Brace 		 * then it had better be the case that
113603383736SDon Brace 		 * h->dev[entry]->offload_enabled is currently 0.
113703383736SDon Brace 		 */
11389fb0de2dSStephen M. Cameron 		h->dev[entry]->raid_map = new_entry->raid_map;
113903383736SDon Brace 		h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
114003383736SDon Brace 	}
1141a3144e0bSJoe Handzik 	if (new_entry->hba_ioaccel_enabled) {
1142a3144e0bSJoe Handzik 		h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
1143a3144e0bSJoe Handzik 		wmb(); /* set ioaccel_handle *before* hba_ioaccel_enabled */
1144a3144e0bSJoe Handzik 	}
1145a3144e0bSJoe Handzik 	h->dev[entry]->hba_ioaccel_enabled = new_entry->hba_ioaccel_enabled;
114603383736SDon Brace 	h->dev[entry]->offload_config = new_entry->offload_config;
114703383736SDon Brace 	h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
114803383736SDon Brace 	h->dev[entry]->queue_depth = new_entry->queue_depth;
1149250fb125SStephen M. Cameron 
115041ce4c35SStephen Cameron 	/*
115141ce4c35SStephen Cameron 	 * We can turn off ioaccel offload now, but need to delay turning
115241ce4c35SStephen Cameron 	 * it on until we can update h->dev[entry]->phys_disk[], but we
115341ce4c35SStephen Cameron 	 * can't do that until all the devices are updated.
115441ce4c35SStephen Cameron 	 */
115541ce4c35SStephen Cameron 	h->dev[entry]->offload_to_be_enabled = new_entry->offload_enabled;
115641ce4c35SStephen Cameron 	if (!new_entry->offload_enabled)
115741ce4c35SStephen Cameron 		h->dev[entry]->offload_enabled = 0;
115841ce4c35SStephen Cameron 
1159a473d86cSRobert Elliott 	offload_enabled = h->dev[entry]->offload_enabled;
1160a473d86cSRobert Elliott 	h->dev[entry]->offload_enabled = h->dev[entry]->offload_to_be_enabled;
11610d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_INFO, h, h->dev[entry], "updated");
1162a473d86cSRobert Elliott 	h->dev[entry]->offload_enabled = offload_enabled;
1163bd9244f7SScott Teel }
1164bd9244f7SScott Teel 
11652a8ccf31SStephen M. Cameron /* Replace an entry from h->dev[] array. */
11662a8ccf31SStephen M. Cameron static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
11672a8ccf31SStephen M. Cameron 	int entry, struct hpsa_scsi_dev_t *new_entry,
11682a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *added[], int *nadded,
11692a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
11702a8ccf31SStephen M. Cameron {
11712a8ccf31SStephen M. Cameron 	/* assumes h->devlock is held */
1172cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
11732a8ccf31SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
11742a8ccf31SStephen M. Cameron 	(*nremoved)++;
117501350d05SStephen M. Cameron 
117601350d05SStephen M. Cameron 	/*
117701350d05SStephen M. Cameron 	 * New physical devices won't have target/lun assigned yet
117801350d05SStephen M. Cameron 	 * so we need to preserve the values in the slot we are replacing.
117901350d05SStephen M. Cameron 	 */
118001350d05SStephen M. Cameron 	if (new_entry->target == -1) {
118101350d05SStephen M. Cameron 		new_entry->target = h->dev[entry]->target;
118201350d05SStephen M. Cameron 		new_entry->lun = h->dev[entry]->lun;
118301350d05SStephen M. Cameron 	}
118401350d05SStephen M. Cameron 
11852a8ccf31SStephen M. Cameron 	h->dev[entry] = new_entry;
11862a8ccf31SStephen M. Cameron 	added[*nadded] = new_entry;
11872a8ccf31SStephen M. Cameron 	(*nadded)++;
11880d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_INFO, h, new_entry, "replaced");
1189a473d86cSRobert Elliott 	new_entry->offload_to_be_enabled = new_entry->offload_enabled;
1190a473d86cSRobert Elliott 	new_entry->offload_enabled = 0;
11912a8ccf31SStephen M. Cameron }
11922a8ccf31SStephen M. Cameron 
1193edd16368SStephen M. Cameron /* Remove an entry from h->dev[] array. */
1194edd16368SStephen M. Cameron static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
1195edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
1196edd16368SStephen M. Cameron {
1197edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
1198edd16368SStephen M. Cameron 	int i;
1199edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1200edd16368SStephen M. Cameron 
1201cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
1202edd16368SStephen M. Cameron 
1203edd16368SStephen M. Cameron 	sd = h->dev[entry];
1204edd16368SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
1205edd16368SStephen M. Cameron 	(*nremoved)++;
1206edd16368SStephen M. Cameron 
1207edd16368SStephen M. Cameron 	for (i = entry; i < h->ndevices-1; i++)
1208edd16368SStephen M. Cameron 		h->dev[i] = h->dev[i+1];
1209edd16368SStephen M. Cameron 	h->ndevices--;
12100d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_INFO, h, sd, "removed");
1211edd16368SStephen M. Cameron }
1212edd16368SStephen M. Cameron 
1213edd16368SStephen M. Cameron #define SCSI3ADDR_EQ(a, b) ( \
1214edd16368SStephen M. Cameron 	(a)[7] == (b)[7] && \
1215edd16368SStephen M. Cameron 	(a)[6] == (b)[6] && \
1216edd16368SStephen M. Cameron 	(a)[5] == (b)[5] && \
1217edd16368SStephen M. Cameron 	(a)[4] == (b)[4] && \
1218edd16368SStephen M. Cameron 	(a)[3] == (b)[3] && \
1219edd16368SStephen M. Cameron 	(a)[2] == (b)[2] && \
1220edd16368SStephen M. Cameron 	(a)[1] == (b)[1] && \
1221edd16368SStephen M. Cameron 	(a)[0] == (b)[0])
1222edd16368SStephen M. Cameron 
1223edd16368SStephen M. Cameron static void fixup_botched_add(struct ctlr_info *h,
1224edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *added)
1225edd16368SStephen M. Cameron {
1226edd16368SStephen M. Cameron 	/* called when scsi_add_device fails in order to re-adjust
1227edd16368SStephen M. Cameron 	 * h->dev[] to match the mid layer's view.
1228edd16368SStephen M. Cameron 	 */
1229edd16368SStephen M. Cameron 	unsigned long flags;
1230edd16368SStephen M. Cameron 	int i, j;
1231edd16368SStephen M. Cameron 
1232edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
1233edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1234edd16368SStephen M. Cameron 		if (h->dev[i] == added) {
1235edd16368SStephen M. Cameron 			for (j = i; j < h->ndevices-1; j++)
1236edd16368SStephen M. Cameron 				h->dev[j] = h->dev[j+1];
1237edd16368SStephen M. Cameron 			h->ndevices--;
1238edd16368SStephen M. Cameron 			break;
1239edd16368SStephen M. Cameron 		}
1240edd16368SStephen M. Cameron 	}
1241edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
1242edd16368SStephen M. Cameron 	kfree(added);
1243edd16368SStephen M. Cameron }
1244edd16368SStephen M. Cameron 
1245edd16368SStephen M. Cameron static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
1246edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev2)
1247edd16368SStephen M. Cameron {
1248edd16368SStephen M. Cameron 	/* we compare everything except lun and target as these
1249edd16368SStephen M. Cameron 	 * are not yet assigned.  Compare parts likely
1250edd16368SStephen M. Cameron 	 * to differ first
1251edd16368SStephen M. Cameron 	 */
1252edd16368SStephen M. Cameron 	if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
1253edd16368SStephen M. Cameron 		sizeof(dev1->scsi3addr)) != 0)
1254edd16368SStephen M. Cameron 		return 0;
1255edd16368SStephen M. Cameron 	if (memcmp(dev1->device_id, dev2->device_id,
1256edd16368SStephen M. Cameron 		sizeof(dev1->device_id)) != 0)
1257edd16368SStephen M. Cameron 		return 0;
1258edd16368SStephen M. Cameron 	if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
1259edd16368SStephen M. Cameron 		return 0;
1260edd16368SStephen M. Cameron 	if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
1261edd16368SStephen M. Cameron 		return 0;
1262edd16368SStephen M. Cameron 	if (dev1->devtype != dev2->devtype)
1263edd16368SStephen M. Cameron 		return 0;
1264edd16368SStephen M. Cameron 	if (dev1->bus != dev2->bus)
1265edd16368SStephen M. Cameron 		return 0;
1266edd16368SStephen M. Cameron 	return 1;
1267edd16368SStephen M. Cameron }
1268edd16368SStephen M. Cameron 
1269bd9244f7SScott Teel static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
1270bd9244f7SScott Teel 	struct hpsa_scsi_dev_t *dev2)
1271bd9244f7SScott Teel {
1272bd9244f7SScott Teel 	/* Device attributes that can change, but don't mean
1273bd9244f7SScott Teel 	 * that the device is a different device, nor that the OS
1274bd9244f7SScott Teel 	 * needs to be told anything about the change.
1275bd9244f7SScott Teel 	 */
1276bd9244f7SScott Teel 	if (dev1->raid_level != dev2->raid_level)
1277bd9244f7SScott Teel 		return 1;
1278250fb125SStephen M. Cameron 	if (dev1->offload_config != dev2->offload_config)
1279250fb125SStephen M. Cameron 		return 1;
1280250fb125SStephen M. Cameron 	if (dev1->offload_enabled != dev2->offload_enabled)
1281250fb125SStephen M. Cameron 		return 1;
128203383736SDon Brace 	if (dev1->queue_depth != dev2->queue_depth)
128303383736SDon Brace 		return 1;
1284bd9244f7SScott Teel 	return 0;
1285bd9244f7SScott Teel }
1286bd9244f7SScott Teel 
1287edd16368SStephen M. Cameron /* Find needle in haystack.  If exact match found, return DEVICE_SAME,
1288edd16368SStephen M. Cameron  * and return needle location in *index.  If scsi3addr matches, but not
1289edd16368SStephen M. Cameron  * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
1290bd9244f7SScott Teel  * location in *index.
1291bd9244f7SScott Teel  * In the case of a minor device attribute change, such as RAID level, just
1292bd9244f7SScott Teel  * return DEVICE_UPDATED, along with the updated device's location in index.
1293bd9244f7SScott Teel  * If needle not found, return DEVICE_NOT_FOUND.
1294edd16368SStephen M. Cameron  */
1295edd16368SStephen M. Cameron static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
1296edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *haystack[], int haystack_size,
1297edd16368SStephen M. Cameron 	int *index)
1298edd16368SStephen M. Cameron {
1299edd16368SStephen M. Cameron 	int i;
1300edd16368SStephen M. Cameron #define DEVICE_NOT_FOUND 0
1301edd16368SStephen M. Cameron #define DEVICE_CHANGED 1
1302edd16368SStephen M. Cameron #define DEVICE_SAME 2
1303bd9244f7SScott Teel #define DEVICE_UPDATED 3
1304edd16368SStephen M. Cameron 	for (i = 0; i < haystack_size; i++) {
130523231048SStephen M. Cameron 		if (haystack[i] == NULL) /* previously removed. */
130623231048SStephen M. Cameron 			continue;
1307edd16368SStephen M. Cameron 		if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
1308edd16368SStephen M. Cameron 			*index = i;
1309bd9244f7SScott Teel 			if (device_is_the_same(needle, haystack[i])) {
1310bd9244f7SScott Teel 				if (device_updated(needle, haystack[i]))
1311bd9244f7SScott Teel 					return DEVICE_UPDATED;
1312edd16368SStephen M. Cameron 				return DEVICE_SAME;
1313bd9244f7SScott Teel 			} else {
13149846590eSStephen M. Cameron 				/* Keep offline devices offline */
13159846590eSStephen M. Cameron 				if (needle->volume_offline)
13169846590eSStephen M. Cameron 					return DEVICE_NOT_FOUND;
1317edd16368SStephen M. Cameron 				return DEVICE_CHANGED;
1318edd16368SStephen M. Cameron 			}
1319edd16368SStephen M. Cameron 		}
1320bd9244f7SScott Teel 	}
1321edd16368SStephen M. Cameron 	*index = -1;
1322edd16368SStephen M. Cameron 	return DEVICE_NOT_FOUND;
1323edd16368SStephen M. Cameron }
1324edd16368SStephen M. Cameron 
13259846590eSStephen M. Cameron static void hpsa_monitor_offline_device(struct ctlr_info *h,
13269846590eSStephen M. Cameron 					unsigned char scsi3addr[])
13279846590eSStephen M. Cameron {
13289846590eSStephen M. Cameron 	struct offline_device_entry *device;
13299846590eSStephen M. Cameron 	unsigned long flags;
13309846590eSStephen M. Cameron 
13319846590eSStephen M. Cameron 	/* Check to see if device is already on the list */
13329846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
13339846590eSStephen M. Cameron 	list_for_each_entry(device, &h->offline_device_list, offline_list) {
13349846590eSStephen M. Cameron 		if (memcmp(device->scsi3addr, scsi3addr,
13359846590eSStephen M. Cameron 			sizeof(device->scsi3addr)) == 0) {
13369846590eSStephen M. Cameron 			spin_unlock_irqrestore(&h->offline_device_lock, flags);
13379846590eSStephen M. Cameron 			return;
13389846590eSStephen M. Cameron 		}
13399846590eSStephen M. Cameron 	}
13409846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
13419846590eSStephen M. Cameron 
13429846590eSStephen M. Cameron 	/* Device is not on the list, add it. */
13439846590eSStephen M. Cameron 	device = kmalloc(sizeof(*device), GFP_KERNEL);
13449846590eSStephen M. Cameron 	if (!device) {
13459846590eSStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in %s\n", __func__);
13469846590eSStephen M. Cameron 		return;
13479846590eSStephen M. Cameron 	}
13489846590eSStephen M. Cameron 	memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
13499846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
13509846590eSStephen M. Cameron 	list_add_tail(&device->offline_list, &h->offline_device_list);
13519846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
13529846590eSStephen M. Cameron }
13539846590eSStephen M. Cameron 
13549846590eSStephen M. Cameron /* Print a message explaining various offline volume states */
13559846590eSStephen M. Cameron static void hpsa_show_volume_status(struct ctlr_info *h,
13569846590eSStephen M. Cameron 	struct hpsa_scsi_dev_t *sd)
13579846590eSStephen M. Cameron {
13589846590eSStephen M. Cameron 	if (sd->volume_offline == HPSA_VPD_LV_STATUS_UNSUPPORTED)
13599846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13609846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume status is not available through vital product data pages.\n",
13619846590eSStephen M. Cameron 			h->scsi_host->host_no,
13629846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13639846590eSStephen M. Cameron 	switch (sd->volume_offline) {
13649846590eSStephen M. Cameron 	case HPSA_LV_OK:
13659846590eSStephen M. Cameron 		break;
13669846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ERASE:
13679846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13689846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing background erase process.\n",
13699846590eSStephen M. Cameron 			h->scsi_host->host_no,
13709846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13719846590eSStephen M. Cameron 		break;
13729846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_RPI:
13739846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13749846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing rapid parity initialization process.\n",
13759846590eSStephen M. Cameron 			h->scsi_host->host_no,
13769846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13779846590eSStephen M. Cameron 		break;
13789846590eSStephen M. Cameron 	case HPSA_LV_PENDING_RPI:
13799846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13809846590eSStephen M. Cameron 				"C%d:B%d:T%d:L%d Volume is queued for rapid parity initialization process.\n",
13819846590eSStephen M. Cameron 				h->scsi_host->host_no,
13829846590eSStephen M. Cameron 				sd->bus, sd->target, sd->lun);
13839846590eSStephen M. Cameron 		break;
13849846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_NO_KEY:
13859846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13869846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because key is not present.\n",
13879846590eSStephen M. Cameron 			h->scsi_host->host_no,
13889846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13899846590eSStephen M. Cameron 		break;
13909846590eSStephen M. Cameron 	case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
13919846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13929846590eSStephen 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",
13939846590eSStephen M. Cameron 			h->scsi_host->host_no,
13949846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13959846590eSStephen M. Cameron 		break;
13969846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION:
13979846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13989846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing encryption process.\n",
13999846590eSStephen M. Cameron 			h->scsi_host->host_no,
14009846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
14019846590eSStephen M. Cameron 		break;
14029846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
14039846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
14049846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing encryption re-keying process.\n",
14059846590eSStephen M. Cameron 			h->scsi_host->host_no,
14069846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
14079846590eSStephen M. Cameron 		break;
14089846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
14099846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
14109846590eSStephen 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",
14119846590eSStephen M. Cameron 			h->scsi_host->host_no,
14129846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
14139846590eSStephen M. Cameron 		break;
14149846590eSStephen M. Cameron 	case HPSA_LV_PENDING_ENCRYPTION:
14159846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
14169846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is pending migration to encrypted state, but process has not started.\n",
14179846590eSStephen M. Cameron 			h->scsi_host->host_no,
14189846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
14199846590eSStephen M. Cameron 		break;
14209846590eSStephen M. Cameron 	case HPSA_LV_PENDING_ENCRYPTION_REKEYING:
14219846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
14229846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is encrypted and is pending encryption rekeying.\n",
14239846590eSStephen M. Cameron 			h->scsi_host->host_no,
14249846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
14259846590eSStephen M. Cameron 		break;
14269846590eSStephen M. Cameron 	}
14279846590eSStephen M. Cameron }
14289846590eSStephen M. Cameron 
142903383736SDon Brace /*
143003383736SDon Brace  * Figure the list of physical drive pointers for a logical drive with
143103383736SDon Brace  * raid offload configured.
143203383736SDon Brace  */
143303383736SDon Brace static void hpsa_figure_phys_disk_ptrs(struct ctlr_info *h,
143403383736SDon Brace 				struct hpsa_scsi_dev_t *dev[], int ndevices,
143503383736SDon Brace 				struct hpsa_scsi_dev_t *logical_drive)
143603383736SDon Brace {
143703383736SDon Brace 	struct raid_map_data *map = &logical_drive->raid_map;
143803383736SDon Brace 	struct raid_map_disk_data *dd = &map->data[0];
143903383736SDon Brace 	int i, j;
144003383736SDon Brace 	int total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
144103383736SDon Brace 				le16_to_cpu(map->metadata_disks_per_row);
144203383736SDon Brace 	int nraid_map_entries = le16_to_cpu(map->row_cnt) *
144303383736SDon Brace 				le16_to_cpu(map->layout_map_count) *
144403383736SDon Brace 				total_disks_per_row;
144503383736SDon Brace 	int nphys_disk = le16_to_cpu(map->layout_map_count) *
144603383736SDon Brace 				total_disks_per_row;
144703383736SDon Brace 	int qdepth;
144803383736SDon Brace 
144903383736SDon Brace 	if (nraid_map_entries > RAID_MAP_MAX_ENTRIES)
145003383736SDon Brace 		nraid_map_entries = RAID_MAP_MAX_ENTRIES;
145103383736SDon Brace 
145203383736SDon Brace 	qdepth = 0;
145303383736SDon Brace 	for (i = 0; i < nraid_map_entries; i++) {
145403383736SDon Brace 		logical_drive->phys_disk[i] = NULL;
145503383736SDon Brace 		if (!logical_drive->offload_config)
145603383736SDon Brace 			continue;
145703383736SDon Brace 		for (j = 0; j < ndevices; j++) {
145803383736SDon Brace 			if (dev[j]->devtype != TYPE_DISK)
145903383736SDon Brace 				continue;
146003383736SDon Brace 			if (is_logical_dev_addr_mode(dev[j]->scsi3addr))
146103383736SDon Brace 				continue;
146203383736SDon Brace 			if (dev[j]->ioaccel_handle != dd[i].ioaccel_handle)
146303383736SDon Brace 				continue;
146403383736SDon Brace 
146503383736SDon Brace 			logical_drive->phys_disk[i] = dev[j];
146603383736SDon Brace 			if (i < nphys_disk)
146703383736SDon Brace 				qdepth = min(h->nr_cmds, qdepth +
146803383736SDon Brace 				    logical_drive->phys_disk[i]->queue_depth);
146903383736SDon Brace 			break;
147003383736SDon Brace 		}
147103383736SDon Brace 
147203383736SDon Brace 		/*
147303383736SDon Brace 		 * This can happen if a physical drive is removed and
147403383736SDon Brace 		 * the logical drive is degraded.  In that case, the RAID
147503383736SDon Brace 		 * map data will refer to a physical disk which isn't actually
147603383736SDon Brace 		 * present.  And in that case offload_enabled should already
147703383736SDon Brace 		 * be 0, but we'll turn it off here just in case
147803383736SDon Brace 		 */
147903383736SDon Brace 		if (!logical_drive->phys_disk[i]) {
148003383736SDon Brace 			logical_drive->offload_enabled = 0;
148141ce4c35SStephen Cameron 			logical_drive->offload_to_be_enabled = 0;
148241ce4c35SStephen Cameron 			logical_drive->queue_depth = 8;
148303383736SDon Brace 		}
148403383736SDon Brace 	}
148503383736SDon Brace 	if (nraid_map_entries)
148603383736SDon Brace 		/*
148703383736SDon Brace 		 * This is correct for reads, too high for full stripe writes,
148803383736SDon Brace 		 * way too high for partial stripe writes
148903383736SDon Brace 		 */
149003383736SDon Brace 		logical_drive->queue_depth = qdepth;
149103383736SDon Brace 	else
149203383736SDon Brace 		logical_drive->queue_depth = h->nr_cmds;
149303383736SDon Brace }
149403383736SDon Brace 
149503383736SDon Brace static void hpsa_update_log_drive_phys_drive_ptrs(struct ctlr_info *h,
149603383736SDon Brace 				struct hpsa_scsi_dev_t *dev[], int ndevices)
149703383736SDon Brace {
149803383736SDon Brace 	int i;
149903383736SDon Brace 
150003383736SDon Brace 	for (i = 0; i < ndevices; i++) {
150103383736SDon Brace 		if (dev[i]->devtype != TYPE_DISK)
150203383736SDon Brace 			continue;
150303383736SDon Brace 		if (!is_logical_dev_addr_mode(dev[i]->scsi3addr))
150403383736SDon Brace 			continue;
150541ce4c35SStephen Cameron 
150641ce4c35SStephen Cameron 		/*
150741ce4c35SStephen Cameron 		 * If offload is currently enabled, the RAID map and
150841ce4c35SStephen Cameron 		 * phys_disk[] assignment *better* not be changing
150941ce4c35SStephen Cameron 		 * and since it isn't changing, we do not need to
151041ce4c35SStephen Cameron 		 * update it.
151141ce4c35SStephen Cameron 		 */
151241ce4c35SStephen Cameron 		if (dev[i]->offload_enabled)
151341ce4c35SStephen Cameron 			continue;
151441ce4c35SStephen Cameron 
151503383736SDon Brace 		hpsa_figure_phys_disk_ptrs(h, dev, ndevices, dev[i]);
151603383736SDon Brace 	}
151703383736SDon Brace }
151803383736SDon Brace 
15194967bd3eSStephen M. Cameron static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
1520edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd[], int nsds)
1521edd16368SStephen M. Cameron {
1522edd16368SStephen M. Cameron 	/* sd contains scsi3 addresses and devtypes, and inquiry
1523edd16368SStephen M. Cameron 	 * data.  This function takes what's in sd to be the current
1524edd16368SStephen M. Cameron 	 * reality and updates h->dev[] to reflect that reality.
1525edd16368SStephen M. Cameron 	 */
1526edd16368SStephen M. Cameron 	int i, entry, device_change, changes = 0;
1527edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *csd;
1528edd16368SStephen M. Cameron 	unsigned long flags;
1529edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **added, **removed;
1530edd16368SStephen M. Cameron 	int nadded, nremoved;
1531edd16368SStephen M. Cameron 	struct Scsi_Host *sh = NULL;
1532edd16368SStephen M. Cameron 
1533cfe5badcSScott Teel 	added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
1534cfe5badcSScott Teel 	removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
1535edd16368SStephen M. Cameron 
1536edd16368SStephen M. Cameron 	if (!added || !removed) {
1537edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in "
1538edd16368SStephen M. Cameron 			"adjust_hpsa_scsi_table\n");
1539edd16368SStephen M. Cameron 		goto free_and_out;
1540edd16368SStephen M. Cameron 	}
1541edd16368SStephen M. Cameron 
1542edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1543edd16368SStephen M. Cameron 
1544edd16368SStephen M. Cameron 	/* find any devices in h->dev[] that are not in
1545edd16368SStephen M. Cameron 	 * sd[] and remove them from h->dev[], and for any
1546edd16368SStephen M. Cameron 	 * devices which have changed, remove the old device
1547edd16368SStephen M. Cameron 	 * info and add the new device info.
1548bd9244f7SScott Teel 	 * If minor device attributes change, just update
1549bd9244f7SScott Teel 	 * the existing device structure.
1550edd16368SStephen M. Cameron 	 */
1551edd16368SStephen M. Cameron 	i = 0;
1552edd16368SStephen M. Cameron 	nremoved = 0;
1553edd16368SStephen M. Cameron 	nadded = 0;
1554edd16368SStephen M. Cameron 	while (i < h->ndevices) {
1555edd16368SStephen M. Cameron 		csd = h->dev[i];
1556edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
1557edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
1558edd16368SStephen M. Cameron 			changes++;
1559edd16368SStephen M. Cameron 			hpsa_scsi_remove_entry(h, hostno, i,
1560edd16368SStephen M. Cameron 				removed, &nremoved);
1561edd16368SStephen M. Cameron 			continue; /* remove ^^^, hence i not incremented */
1562edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
1563edd16368SStephen M. Cameron 			changes++;
15642a8ccf31SStephen M. Cameron 			hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
15652a8ccf31SStephen M. Cameron 				added, &nadded, removed, &nremoved);
1566c7f172dcSStephen M. Cameron 			/* Set it to NULL to prevent it from being freed
1567c7f172dcSStephen M. Cameron 			 * at the bottom of hpsa_update_scsi_devices()
1568c7f172dcSStephen M. Cameron 			 */
1569c7f172dcSStephen M. Cameron 			sd[entry] = NULL;
1570bd9244f7SScott Teel 		} else if (device_change == DEVICE_UPDATED) {
1571bd9244f7SScott Teel 			hpsa_scsi_update_entry(h, hostno, i, sd[entry]);
1572edd16368SStephen M. Cameron 		}
1573edd16368SStephen M. Cameron 		i++;
1574edd16368SStephen M. Cameron 	}
1575edd16368SStephen M. Cameron 
1576edd16368SStephen M. Cameron 	/* Now, make sure every device listed in sd[] is also
1577edd16368SStephen M. Cameron 	 * listed in h->dev[], adding them if they aren't found
1578edd16368SStephen M. Cameron 	 */
1579edd16368SStephen M. Cameron 
1580edd16368SStephen M. Cameron 	for (i = 0; i < nsds; i++) {
1581edd16368SStephen M. Cameron 		if (!sd[i]) /* if already added above. */
1582edd16368SStephen M. Cameron 			continue;
15839846590eSStephen M. Cameron 
15849846590eSStephen M. Cameron 		/* Don't add devices which are NOT READY, FORMAT IN PROGRESS
15859846590eSStephen M. Cameron 		 * as the SCSI mid-layer does not handle such devices well.
15869846590eSStephen M. Cameron 		 * It relentlessly loops sending TUR at 3Hz, then READ(10)
15879846590eSStephen M. Cameron 		 * at 160Hz, and prevents the system from coming up.
15889846590eSStephen M. Cameron 		 */
15899846590eSStephen M. Cameron 		if (sd[i]->volume_offline) {
15909846590eSStephen M. Cameron 			hpsa_show_volume_status(h, sd[i]);
15910d96ef5fSWebb Scales 			hpsa_show_dev_msg(KERN_INFO, h, sd[i], "offline");
15929846590eSStephen M. Cameron 			continue;
15939846590eSStephen M. Cameron 		}
15949846590eSStephen M. Cameron 
1595edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1596edd16368SStephen M. Cameron 					h->ndevices, &entry);
1597edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
1598edd16368SStephen M. Cameron 			changes++;
1599edd16368SStephen M. Cameron 			if (hpsa_scsi_add_entry(h, hostno, sd[i],
1600edd16368SStephen M. Cameron 				added, &nadded) != 0)
1601edd16368SStephen M. Cameron 				break;
1602edd16368SStephen M. Cameron 			sd[i] = NULL; /* prevent from being freed later. */
1603edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
1604edd16368SStephen M. Cameron 			/* should never happen... */
1605edd16368SStephen M. Cameron 			changes++;
1606edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev,
1607edd16368SStephen M. Cameron 				"device unexpectedly changed.\n");
1608edd16368SStephen M. Cameron 			/* but if it does happen, we just ignore that device */
1609edd16368SStephen M. Cameron 		}
1610edd16368SStephen M. Cameron 	}
161141ce4c35SStephen Cameron 	hpsa_update_log_drive_phys_drive_ptrs(h, h->dev, h->ndevices);
161241ce4c35SStephen Cameron 
161341ce4c35SStephen Cameron 	/* Now that h->dev[]->phys_disk[] is coherent, we can enable
161441ce4c35SStephen Cameron 	 * any logical drives that need it enabled.
161541ce4c35SStephen Cameron 	 */
161641ce4c35SStephen Cameron 	for (i = 0; i < h->ndevices; i++)
161741ce4c35SStephen Cameron 		h->dev[i]->offload_enabled = h->dev[i]->offload_to_be_enabled;
161841ce4c35SStephen Cameron 
1619edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1620edd16368SStephen M. Cameron 
16219846590eSStephen M. Cameron 	/* Monitor devices which are in one of several NOT READY states to be
16229846590eSStephen M. Cameron 	 * brought online later. This must be done without holding h->devlock,
16239846590eSStephen M. Cameron 	 * so don't touch h->dev[]
16249846590eSStephen M. Cameron 	 */
16259846590eSStephen M. Cameron 	for (i = 0; i < nsds; i++) {
16269846590eSStephen M. Cameron 		if (!sd[i]) /* if already added above. */
16279846590eSStephen M. Cameron 			continue;
16289846590eSStephen M. Cameron 		if (sd[i]->volume_offline)
16299846590eSStephen M. Cameron 			hpsa_monitor_offline_device(h, sd[i]->scsi3addr);
16309846590eSStephen M. Cameron 	}
16319846590eSStephen M. Cameron 
1632edd16368SStephen M. Cameron 	/* Don't notify scsi mid layer of any changes the first time through
1633edd16368SStephen M. Cameron 	 * (or if there are no changes) scsi_scan_host will do it later the
1634edd16368SStephen M. Cameron 	 * first time through.
1635edd16368SStephen M. Cameron 	 */
1636edd16368SStephen M. Cameron 	if (hostno == -1 || !changes)
1637edd16368SStephen M. Cameron 		goto free_and_out;
1638edd16368SStephen M. Cameron 
1639edd16368SStephen M. Cameron 	sh = h->scsi_host;
1640edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any removed devices */
1641edd16368SStephen M. Cameron 	for (i = 0; i < nremoved; i++) {
164241ce4c35SStephen Cameron 		if (removed[i]->expose_state & HPSA_SCSI_ADD) {
1643edd16368SStephen M. Cameron 			struct scsi_device *sdev =
1644edd16368SStephen M. Cameron 				scsi_device_lookup(sh, removed[i]->bus,
1645edd16368SStephen M. Cameron 					removed[i]->target, removed[i]->lun);
1646edd16368SStephen M. Cameron 			if (sdev != NULL) {
1647edd16368SStephen M. Cameron 				scsi_remove_device(sdev);
1648edd16368SStephen M. Cameron 				scsi_device_put(sdev);
1649edd16368SStephen M. Cameron 			} else {
165041ce4c35SStephen Cameron 				/*
165141ce4c35SStephen Cameron 				 * We don't expect to get here.
1652edd16368SStephen M. Cameron 				 * future cmds to this device will get selection
1653edd16368SStephen M. Cameron 				 * timeout as if the device was gone.
1654edd16368SStephen M. Cameron 				 */
16550d96ef5fSWebb Scales 				hpsa_show_dev_msg(KERN_WARNING, h, removed[i],
16560d96ef5fSWebb Scales 					"didn't find device for removal.");
1657edd16368SStephen M. Cameron 			}
165841ce4c35SStephen Cameron 		}
1659edd16368SStephen M. Cameron 		kfree(removed[i]);
1660edd16368SStephen M. Cameron 		removed[i] = NULL;
1661edd16368SStephen M. Cameron 	}
1662edd16368SStephen M. Cameron 
1663edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any added devices */
1664edd16368SStephen M. Cameron 	for (i = 0; i < nadded; i++) {
166541ce4c35SStephen Cameron 		if (!(added[i]->expose_state & HPSA_SCSI_ADD))
166641ce4c35SStephen Cameron 			continue;
1667edd16368SStephen M. Cameron 		if (scsi_add_device(sh, added[i]->bus,
1668edd16368SStephen M. Cameron 			added[i]->target, added[i]->lun) == 0)
1669edd16368SStephen M. Cameron 			continue;
16700d96ef5fSWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, added[i],
16710d96ef5fSWebb Scales 					"addition failed, device not added.");
1672edd16368SStephen M. Cameron 		/* now we have to remove it from h->dev,
1673edd16368SStephen M. Cameron 		 * since it didn't get added to scsi mid layer
1674edd16368SStephen M. Cameron 		 */
1675edd16368SStephen M. Cameron 		fixup_botched_add(h, added[i]);
1676105a3dbcSRobert Elliott 		added[i] = NULL;
1677edd16368SStephen M. Cameron 	}
1678edd16368SStephen M. Cameron 
1679edd16368SStephen M. Cameron free_and_out:
1680edd16368SStephen M. Cameron 	kfree(added);
1681edd16368SStephen M. Cameron 	kfree(removed);
1682edd16368SStephen M. Cameron }
1683edd16368SStephen M. Cameron 
1684edd16368SStephen M. Cameron /*
16859e03aa2fSJoe Perches  * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
1686edd16368SStephen M. Cameron  * Assume's h->devlock is held.
1687edd16368SStephen M. Cameron  */
1688edd16368SStephen M. Cameron static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1689edd16368SStephen M. Cameron 	int bus, int target, int lun)
1690edd16368SStephen M. Cameron {
1691edd16368SStephen M. Cameron 	int i;
1692edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1693edd16368SStephen M. Cameron 
1694edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1695edd16368SStephen M. Cameron 		sd = h->dev[i];
1696edd16368SStephen M. Cameron 		if (sd->bus == bus && sd->target == target && sd->lun == lun)
1697edd16368SStephen M. Cameron 			return sd;
1698edd16368SStephen M. Cameron 	}
1699edd16368SStephen M. Cameron 	return NULL;
1700edd16368SStephen M. Cameron }
1701edd16368SStephen M. Cameron 
1702edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev)
1703edd16368SStephen M. Cameron {
1704edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1705edd16368SStephen M. Cameron 	unsigned long flags;
1706edd16368SStephen M. Cameron 	struct ctlr_info *h;
1707edd16368SStephen M. Cameron 
1708edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
1709edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1710edd16368SStephen M. Cameron 	sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1711edd16368SStephen M. Cameron 		sdev_id(sdev), sdev->lun);
171241ce4c35SStephen Cameron 	if (likely(sd)) {
171303383736SDon Brace 		atomic_set(&sd->ioaccel_cmds_out, 0);
171441ce4c35SStephen Cameron 		sdev->hostdata = (sd->expose_state & HPSA_SCSI_ADD) ? sd : NULL;
171541ce4c35SStephen Cameron 	} else
171641ce4c35SStephen Cameron 		sdev->hostdata = NULL;
1717edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1718edd16368SStephen M. Cameron 	return 0;
1719edd16368SStephen M. Cameron }
1720edd16368SStephen M. Cameron 
172141ce4c35SStephen Cameron /* configure scsi device based on internal per-device structure */
172241ce4c35SStephen Cameron static int hpsa_slave_configure(struct scsi_device *sdev)
172341ce4c35SStephen Cameron {
172441ce4c35SStephen Cameron 	struct hpsa_scsi_dev_t *sd;
172541ce4c35SStephen Cameron 	int queue_depth;
172641ce4c35SStephen Cameron 
172741ce4c35SStephen Cameron 	sd = sdev->hostdata;
172841ce4c35SStephen Cameron 	sdev->no_uld_attach = !sd || !(sd->expose_state & HPSA_ULD_ATTACH);
172941ce4c35SStephen Cameron 
173041ce4c35SStephen Cameron 	if (sd)
173141ce4c35SStephen Cameron 		queue_depth = sd->queue_depth != 0 ?
173241ce4c35SStephen Cameron 			sd->queue_depth : sdev->host->can_queue;
173341ce4c35SStephen Cameron 	else
173441ce4c35SStephen Cameron 		queue_depth = sdev->host->can_queue;
173541ce4c35SStephen Cameron 
173641ce4c35SStephen Cameron 	scsi_change_queue_depth(sdev, queue_depth);
173741ce4c35SStephen Cameron 
173841ce4c35SStephen Cameron 	return 0;
173941ce4c35SStephen Cameron }
174041ce4c35SStephen Cameron 
1741edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev)
1742edd16368SStephen M. Cameron {
1743bcc44255SStephen M. Cameron 	/* nothing to do. */
1744edd16368SStephen M. Cameron }
1745edd16368SStephen M. Cameron 
1746d9a729f3SWebb Scales static void hpsa_free_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
1747d9a729f3SWebb Scales {
1748d9a729f3SWebb Scales 	int i;
1749d9a729f3SWebb Scales 
1750d9a729f3SWebb Scales 	if (!h->ioaccel2_cmd_sg_list)
1751d9a729f3SWebb Scales 		return;
1752d9a729f3SWebb Scales 	for (i = 0; i < h->nr_cmds; i++) {
1753d9a729f3SWebb Scales 		kfree(h->ioaccel2_cmd_sg_list[i]);
1754d9a729f3SWebb Scales 		h->ioaccel2_cmd_sg_list[i] = NULL;
1755d9a729f3SWebb Scales 	}
1756d9a729f3SWebb Scales 	kfree(h->ioaccel2_cmd_sg_list);
1757d9a729f3SWebb Scales 	h->ioaccel2_cmd_sg_list = NULL;
1758d9a729f3SWebb Scales }
1759d9a729f3SWebb Scales 
1760d9a729f3SWebb Scales static int hpsa_allocate_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
1761d9a729f3SWebb Scales {
1762d9a729f3SWebb Scales 	int i;
1763d9a729f3SWebb Scales 
1764d9a729f3SWebb Scales 	if (h->chainsize <= 0)
1765d9a729f3SWebb Scales 		return 0;
1766d9a729f3SWebb Scales 
1767d9a729f3SWebb Scales 	h->ioaccel2_cmd_sg_list =
1768d9a729f3SWebb Scales 		kzalloc(sizeof(*h->ioaccel2_cmd_sg_list) * h->nr_cmds,
1769d9a729f3SWebb Scales 					GFP_KERNEL);
1770d9a729f3SWebb Scales 	if (!h->ioaccel2_cmd_sg_list)
1771d9a729f3SWebb Scales 		return -ENOMEM;
1772d9a729f3SWebb Scales 	for (i = 0; i < h->nr_cmds; i++) {
1773d9a729f3SWebb Scales 		h->ioaccel2_cmd_sg_list[i] =
1774d9a729f3SWebb Scales 			kmalloc(sizeof(*h->ioaccel2_cmd_sg_list[i]) *
1775d9a729f3SWebb Scales 					h->maxsgentries, GFP_KERNEL);
1776d9a729f3SWebb Scales 		if (!h->ioaccel2_cmd_sg_list[i])
1777d9a729f3SWebb Scales 			goto clean;
1778d9a729f3SWebb Scales 	}
1779d9a729f3SWebb Scales 	return 0;
1780d9a729f3SWebb Scales 
1781d9a729f3SWebb Scales clean:
1782d9a729f3SWebb Scales 	hpsa_free_ioaccel2_sg_chain_blocks(h);
1783d9a729f3SWebb Scales 	return -ENOMEM;
1784d9a729f3SWebb Scales }
1785d9a729f3SWebb Scales 
178633a2ffceSStephen M. Cameron static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
178733a2ffceSStephen M. Cameron {
178833a2ffceSStephen M. Cameron 	int i;
178933a2ffceSStephen M. Cameron 
179033a2ffceSStephen M. Cameron 	if (!h->cmd_sg_list)
179133a2ffceSStephen M. Cameron 		return;
179233a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
179333a2ffceSStephen M. Cameron 		kfree(h->cmd_sg_list[i]);
179433a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = NULL;
179533a2ffceSStephen M. Cameron 	}
179633a2ffceSStephen M. Cameron 	kfree(h->cmd_sg_list);
179733a2ffceSStephen M. Cameron 	h->cmd_sg_list = NULL;
179833a2ffceSStephen M. Cameron }
179933a2ffceSStephen M. Cameron 
1800105a3dbcSRobert Elliott static int hpsa_alloc_sg_chain_blocks(struct ctlr_info *h)
180133a2ffceSStephen M. Cameron {
180233a2ffceSStephen M. Cameron 	int i;
180333a2ffceSStephen M. Cameron 
180433a2ffceSStephen M. Cameron 	if (h->chainsize <= 0)
180533a2ffceSStephen M. Cameron 		return 0;
180633a2ffceSStephen M. Cameron 
180733a2ffceSStephen M. Cameron 	h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
180833a2ffceSStephen M. Cameron 				GFP_KERNEL);
18093d4e6af8SRobert Elliott 	if (!h->cmd_sg_list) {
18103d4e6af8SRobert Elliott 		dev_err(&h->pdev->dev, "Failed to allocate SG list\n");
181133a2ffceSStephen M. Cameron 		return -ENOMEM;
18123d4e6af8SRobert Elliott 	}
181333a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
181433a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
181533a2ffceSStephen M. Cameron 						h->chainsize, GFP_KERNEL);
18163d4e6af8SRobert Elliott 		if (!h->cmd_sg_list[i]) {
18173d4e6af8SRobert Elliott 			dev_err(&h->pdev->dev, "Failed to allocate cmd SG\n");
181833a2ffceSStephen M. Cameron 			goto clean;
181933a2ffceSStephen M. Cameron 		}
18203d4e6af8SRobert Elliott 	}
182133a2ffceSStephen M. Cameron 	return 0;
182233a2ffceSStephen M. Cameron 
182333a2ffceSStephen M. Cameron clean:
182433a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
182533a2ffceSStephen M. Cameron 	return -ENOMEM;
182633a2ffceSStephen M. Cameron }
182733a2ffceSStephen M. Cameron 
1828d9a729f3SWebb Scales static int hpsa_map_ioaccel2_sg_chain_block(struct ctlr_info *h,
1829d9a729f3SWebb Scales 	struct io_accel2_cmd *cp, struct CommandList *c)
1830d9a729f3SWebb Scales {
1831d9a729f3SWebb Scales 	struct ioaccel2_sg_element *chain_block;
1832d9a729f3SWebb Scales 	u64 temp64;
1833d9a729f3SWebb Scales 	u32 chain_size;
1834d9a729f3SWebb Scales 
1835d9a729f3SWebb Scales 	chain_block = h->ioaccel2_cmd_sg_list[c->cmdindex];
1836d9a729f3SWebb Scales 	chain_size = le32_to_cpu(cp->data_len);
1837d9a729f3SWebb Scales 	temp64 = pci_map_single(h->pdev, chain_block, chain_size,
1838d9a729f3SWebb Scales 				PCI_DMA_TODEVICE);
1839d9a729f3SWebb Scales 	if (dma_mapping_error(&h->pdev->dev, temp64)) {
1840d9a729f3SWebb Scales 		/* prevent subsequent unmapping */
1841d9a729f3SWebb Scales 		cp->sg->address = 0;
1842d9a729f3SWebb Scales 		return -1;
1843d9a729f3SWebb Scales 	}
1844d9a729f3SWebb Scales 	cp->sg->address = cpu_to_le64(temp64);
1845d9a729f3SWebb Scales 	return 0;
1846d9a729f3SWebb Scales }
1847d9a729f3SWebb Scales 
1848d9a729f3SWebb Scales static void hpsa_unmap_ioaccel2_sg_chain_block(struct ctlr_info *h,
1849d9a729f3SWebb Scales 	struct io_accel2_cmd *cp)
1850d9a729f3SWebb Scales {
1851d9a729f3SWebb Scales 	struct ioaccel2_sg_element *chain_sg;
1852d9a729f3SWebb Scales 	u64 temp64;
1853d9a729f3SWebb Scales 	u32 chain_size;
1854d9a729f3SWebb Scales 
1855d9a729f3SWebb Scales 	chain_sg = cp->sg;
1856d9a729f3SWebb Scales 	temp64 = le64_to_cpu(chain_sg->address);
1857d9a729f3SWebb Scales 	chain_size = le32_to_cpu(cp->data_len);
1858d9a729f3SWebb Scales 	pci_unmap_single(h->pdev, temp64, chain_size, PCI_DMA_TODEVICE);
1859d9a729f3SWebb Scales }
1860d9a729f3SWebb Scales 
1861e2bea6dfSStephen M. Cameron static int hpsa_map_sg_chain_block(struct ctlr_info *h,
186233a2ffceSStephen M. Cameron 	struct CommandList *c)
186333a2ffceSStephen M. Cameron {
186433a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg, *chain_block;
186533a2ffceSStephen M. Cameron 	u64 temp64;
186650a0decfSStephen M. Cameron 	u32 chain_len;
186733a2ffceSStephen M. Cameron 
186833a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
186933a2ffceSStephen M. Cameron 	chain_block = h->cmd_sg_list[c->cmdindex];
187050a0decfSStephen M. Cameron 	chain_sg->Ext = cpu_to_le32(HPSA_SG_CHAIN);
187150a0decfSStephen M. Cameron 	chain_len = sizeof(*chain_sg) *
18722b08b3e9SDon Brace 		(le16_to_cpu(c->Header.SGTotal) - h->max_cmd_sg_entries);
187350a0decfSStephen M. Cameron 	chain_sg->Len = cpu_to_le32(chain_len);
187450a0decfSStephen M. Cameron 	temp64 = pci_map_single(h->pdev, chain_block, chain_len,
187533a2ffceSStephen M. Cameron 				PCI_DMA_TODEVICE);
1876e2bea6dfSStephen M. Cameron 	if (dma_mapping_error(&h->pdev->dev, temp64)) {
1877e2bea6dfSStephen M. Cameron 		/* prevent subsequent unmapping */
187850a0decfSStephen M. Cameron 		chain_sg->Addr = cpu_to_le64(0);
1879e2bea6dfSStephen M. Cameron 		return -1;
1880e2bea6dfSStephen M. Cameron 	}
188150a0decfSStephen M. Cameron 	chain_sg->Addr = cpu_to_le64(temp64);
1882e2bea6dfSStephen M. Cameron 	return 0;
188333a2ffceSStephen M. Cameron }
188433a2ffceSStephen M. Cameron 
188533a2ffceSStephen M. Cameron static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
188633a2ffceSStephen M. Cameron 	struct CommandList *c)
188733a2ffceSStephen M. Cameron {
188833a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg;
188933a2ffceSStephen M. Cameron 
189050a0decfSStephen M. Cameron 	if (le16_to_cpu(c->Header.SGTotal) <= h->max_cmd_sg_entries)
189133a2ffceSStephen M. Cameron 		return;
189233a2ffceSStephen M. Cameron 
189333a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
189450a0decfSStephen M. Cameron 	pci_unmap_single(h->pdev, le64_to_cpu(chain_sg->Addr),
189550a0decfSStephen M. Cameron 			le32_to_cpu(chain_sg->Len), PCI_DMA_TODEVICE);
189633a2ffceSStephen M. Cameron }
189733a2ffceSStephen M. Cameron 
1898a09c1441SScott Teel 
1899a09c1441SScott Teel /* Decode the various types of errors on ioaccel2 path.
1900a09c1441SScott Teel  * Return 1 for any error that should generate a RAID path retry.
1901a09c1441SScott Teel  * Return 0 for errors that don't require a RAID path retry.
1902a09c1441SScott Teel  */
1903a09c1441SScott Teel static int handle_ioaccel_mode2_error(struct ctlr_info *h,
1904c349775eSScott Teel 					struct CommandList *c,
1905c349775eSScott Teel 					struct scsi_cmnd *cmd,
1906c349775eSScott Teel 					struct io_accel2_cmd *c2)
1907c349775eSScott Teel {
1908c349775eSScott Teel 	int data_len;
1909a09c1441SScott Teel 	int retry = 0;
1910c40820d5SJoe Handzik 	u32 ioaccel2_resid = 0;
1911c349775eSScott Teel 
1912c349775eSScott Teel 	switch (c2->error_data.serv_response) {
1913c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_COMPLETE:
1914c349775eSScott Teel 		switch (c2->error_data.status) {
1915c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
1916c349775eSScott Teel 			break;
1917c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
1918ee6b1889SStephen M. Cameron 			cmd->result |= SAM_STAT_CHECK_CONDITION;
1919c349775eSScott Teel 			if (c2->error_data.data_present !=
1920ee6b1889SStephen M. Cameron 					IOACCEL2_SENSE_DATA_PRESENT) {
1921ee6b1889SStephen M. Cameron 				memset(cmd->sense_buffer, 0,
1922ee6b1889SStephen M. Cameron 					SCSI_SENSE_BUFFERSIZE);
1923c349775eSScott Teel 				break;
1924ee6b1889SStephen M. Cameron 			}
1925c349775eSScott Teel 			/* copy the sense data */
1926c349775eSScott Teel 			data_len = c2->error_data.sense_data_len;
1927c349775eSScott Teel 			if (data_len > SCSI_SENSE_BUFFERSIZE)
1928c349775eSScott Teel 				data_len = SCSI_SENSE_BUFFERSIZE;
1929c349775eSScott Teel 			if (data_len > sizeof(c2->error_data.sense_data_buff))
1930c349775eSScott Teel 				data_len =
1931c349775eSScott Teel 					sizeof(c2->error_data.sense_data_buff);
1932c349775eSScott Teel 			memcpy(cmd->sense_buffer,
1933c349775eSScott Teel 				c2->error_data.sense_data_buff, data_len);
1934a09c1441SScott Teel 			retry = 1;
1935c349775eSScott Teel 			break;
1936c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
1937a09c1441SScott Teel 			retry = 1;
1938c349775eSScott Teel 			break;
1939c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
1940a09c1441SScott Teel 			retry = 1;
1941c349775eSScott Teel 			break;
1942c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
19434a8da22bSStephen Cameron 			retry = 1;
1944c349775eSScott Teel 			break;
1945c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
1946a09c1441SScott Teel 			retry = 1;
1947c349775eSScott Teel 			break;
1948c349775eSScott Teel 		default:
1949a09c1441SScott Teel 			retry = 1;
1950c349775eSScott Teel 			break;
1951c349775eSScott Teel 		}
1952c349775eSScott Teel 		break;
1953c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_FAILURE:
1954c40820d5SJoe Handzik 		switch (c2->error_data.status) {
1955c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_IO_ERROR:
1956c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_IO_ABORTED:
1957c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_OVERRUN:
1958c40820d5SJoe Handzik 			retry = 1;
1959c40820d5SJoe Handzik 			break;
1960c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_UNDERRUN:
1961c40820d5SJoe Handzik 			cmd->result = (DID_OK << 16);		/* host byte */
1962c40820d5SJoe Handzik 			cmd->result |= (COMMAND_COMPLETE << 8);	/* msg byte */
1963c40820d5SJoe Handzik 			ioaccel2_resid = get_unaligned_le32(
1964c40820d5SJoe Handzik 						&c2->error_data.resid_cnt[0]);
1965c40820d5SJoe Handzik 			scsi_set_resid(cmd, ioaccel2_resid);
1966c40820d5SJoe Handzik 			break;
1967c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_NO_PATH_TO_DEVICE:
1968c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_INVALID_DEVICE:
1969c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_IOACCEL_DISABLED:
1970c40820d5SJoe Handzik 			/* We will get an event from ctlr to trigger rescan */
1971c40820d5SJoe Handzik 			retry = 1;
1972c40820d5SJoe Handzik 			break;
1973c40820d5SJoe Handzik 		default:
1974c40820d5SJoe Handzik 			retry = 1;
1975c40820d5SJoe Handzik 		}
1976c349775eSScott Teel 		break;
1977c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
1978c349775eSScott Teel 		break;
1979c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
1980c349775eSScott Teel 		break;
1981c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
1982a09c1441SScott Teel 		retry = 1;
1983c349775eSScott Teel 		break;
1984c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
1985c349775eSScott Teel 		break;
1986c349775eSScott Teel 	default:
1987a09c1441SScott Teel 		retry = 1;
1988c349775eSScott Teel 		break;
1989c349775eSScott Teel 	}
1990a09c1441SScott Teel 
1991a09c1441SScott Teel 	return retry;	/* retry on raid path? */
1992c349775eSScott Teel }
1993c349775eSScott Teel 
1994a58e7e53SWebb Scales static void hpsa_cmd_resolve_events(struct ctlr_info *h,
1995a58e7e53SWebb Scales 		struct CommandList *c)
1996a58e7e53SWebb Scales {
1997a58e7e53SWebb Scales 	/*
1998a58e7e53SWebb Scales 	 * Prevent the following race in the abort handler:
1999a58e7e53SWebb Scales 	 *
2000a58e7e53SWebb Scales 	 * 1. LLD is requested to abort a SCSI command
2001a58e7e53SWebb Scales 	 * 2. The SCSI command completes
2002a58e7e53SWebb Scales 	 * 3. The struct CommandList associated with step 2 is made available
2003a58e7e53SWebb Scales 	 * 4. New I/O request to LLD to another LUN re-uses struct CommandList
2004a58e7e53SWebb Scales 	 * 5. Abort handler follows scsi_cmnd->host_scribble and
2005a58e7e53SWebb Scales 	 *    finds struct CommandList and tries to aborts it
2006a58e7e53SWebb Scales 	 * Now we have aborted the wrong command.
2007a58e7e53SWebb Scales 	 *
2008a58e7e53SWebb Scales 	 * Clear c->scsi_cmd here so that the abort handler will know this
2009a58e7e53SWebb Scales 	 * command has completed.  Then, check to see if the abort handler is
2010a58e7e53SWebb Scales 	 * waiting for this command, and, if so, wake it.
2011a58e7e53SWebb Scales 	 */
2012a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_IDLE;
2013a58e7e53SWebb Scales 	mb(); /* Ensure c->scsi_cmd is set to SCSI_CMD_IDLE */
2014a58e7e53SWebb Scales 	if (c->abort_pending) {
2015a58e7e53SWebb Scales 		c->abort_pending = false;
2016a58e7e53SWebb Scales 		wake_up_all(&h->abort_sync_wait_queue);
2017a58e7e53SWebb Scales 	}
2018a58e7e53SWebb Scales }
2019a58e7e53SWebb Scales 
202073153fe5SWebb Scales static void hpsa_cmd_resolve_and_free(struct ctlr_info *h,
202173153fe5SWebb Scales 				      struct CommandList *c)
202273153fe5SWebb Scales {
202373153fe5SWebb Scales 	hpsa_cmd_resolve_events(h, c);
202473153fe5SWebb Scales 	cmd_tagged_free(h, c);
202573153fe5SWebb Scales }
202673153fe5SWebb Scales 
20278a0ff92cSWebb Scales static void hpsa_cmd_free_and_done(struct ctlr_info *h,
20288a0ff92cSWebb Scales 		struct CommandList *c, struct scsi_cmnd *cmd)
20298a0ff92cSWebb Scales {
203073153fe5SWebb Scales 	hpsa_cmd_resolve_and_free(h, c);
20318a0ff92cSWebb Scales 	cmd->scsi_done(cmd);
20328a0ff92cSWebb Scales }
20338a0ff92cSWebb Scales 
20348a0ff92cSWebb Scales static void hpsa_retry_cmd(struct ctlr_info *h, struct CommandList *c)
20358a0ff92cSWebb Scales {
20368a0ff92cSWebb Scales 	INIT_WORK(&c->work, hpsa_command_resubmit_worker);
20378a0ff92cSWebb Scales 	queue_work_on(raw_smp_processor_id(), h->resubmit_wq, &c->work);
20388a0ff92cSWebb Scales }
20398a0ff92cSWebb Scales 
2040a58e7e53SWebb Scales static void hpsa_set_scsi_cmd_aborted(struct scsi_cmnd *cmd)
2041a58e7e53SWebb Scales {
2042a58e7e53SWebb Scales 	cmd->result = DID_ABORT << 16;
2043a58e7e53SWebb Scales }
2044a58e7e53SWebb Scales 
2045a58e7e53SWebb Scales static void hpsa_cmd_abort_and_free(struct ctlr_info *h, struct CommandList *c,
2046a58e7e53SWebb Scales 				    struct scsi_cmnd *cmd)
2047a58e7e53SWebb Scales {
2048a58e7e53SWebb Scales 	hpsa_set_scsi_cmd_aborted(cmd);
2049a58e7e53SWebb Scales 	dev_warn(&h->pdev->dev, "CDB %16phN was aborted with status 0x%x\n",
2050a58e7e53SWebb Scales 			 c->Request.CDB, c->err_info->ScsiStatus);
205173153fe5SWebb Scales 	hpsa_cmd_resolve_and_free(h, c);
2052a58e7e53SWebb Scales }
2053a58e7e53SWebb Scales 
2054c349775eSScott Teel static void process_ioaccel2_completion(struct ctlr_info *h,
2055c349775eSScott Teel 		struct CommandList *c, struct scsi_cmnd *cmd,
2056c349775eSScott Teel 		struct hpsa_scsi_dev_t *dev)
2057c349775eSScott Teel {
2058c349775eSScott Teel 	struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
2059c349775eSScott Teel 
2060c349775eSScott Teel 	/* check for good status */
2061c349775eSScott Teel 	if (likely(c2->error_data.serv_response == 0 &&
20628a0ff92cSWebb Scales 			c2->error_data.status == 0))
20638a0ff92cSWebb Scales 		return hpsa_cmd_free_and_done(h, c, cmd);
2064c349775eSScott Teel 
2065a58e7e53SWebb Scales 	/* don't requeue a command which is being aborted */
2066a58e7e53SWebb Scales 	if (unlikely(c->abort_pending))
2067a58e7e53SWebb Scales 		return hpsa_cmd_abort_and_free(h, c, cmd);
2068a58e7e53SWebb Scales 
20698a0ff92cSWebb Scales 	/*
20708a0ff92cSWebb Scales 	 * Any RAID offload error results in retry which will use
2071c349775eSScott Teel 	 * the normal I/O path so the controller can handle whatever's
2072c349775eSScott Teel 	 * wrong.
2073c349775eSScott Teel 	 */
2074c349775eSScott Teel 	if (is_logical_dev_addr_mode(dev->scsi3addr) &&
2075c349775eSScott Teel 		c2->error_data.serv_response ==
2076c349775eSScott Teel 			IOACCEL2_SERV_RESPONSE_FAILURE) {
2077080ef1ccSDon Brace 		if (c2->error_data.status ==
2078080ef1ccSDon Brace 			IOACCEL2_STATUS_SR_IOACCEL_DISABLED)
2079c349775eSScott Teel 			dev->offload_enabled = 0;
20808a0ff92cSWebb Scales 
20818a0ff92cSWebb Scales 		return hpsa_retry_cmd(h, c);
2082080ef1ccSDon Brace 	}
2083080ef1ccSDon Brace 
2084080ef1ccSDon Brace 	if (handle_ioaccel_mode2_error(h, c, cmd, c2))
20858a0ff92cSWebb Scales 		return hpsa_retry_cmd(h, c);
2086080ef1ccSDon Brace 
20878a0ff92cSWebb Scales 	return hpsa_cmd_free_and_done(h, c, cmd);
2088c349775eSScott Teel }
2089c349775eSScott Teel 
20909437ac43SStephen Cameron /* Returns 0 on success, < 0 otherwise. */
20919437ac43SStephen Cameron static int hpsa_evaluate_tmf_status(struct ctlr_info *h,
20929437ac43SStephen Cameron 					struct CommandList *cp)
20939437ac43SStephen Cameron {
20949437ac43SStephen Cameron 	u8 tmf_status = cp->err_info->ScsiStatus;
20959437ac43SStephen Cameron 
20969437ac43SStephen Cameron 	switch (tmf_status) {
20979437ac43SStephen Cameron 	case CISS_TMF_COMPLETE:
20989437ac43SStephen Cameron 		/*
20999437ac43SStephen Cameron 		 * CISS_TMF_COMPLETE never happens, instead,
21009437ac43SStephen Cameron 		 * ei->CommandStatus == 0 for this case.
21019437ac43SStephen Cameron 		 */
21029437ac43SStephen Cameron 	case CISS_TMF_SUCCESS:
21039437ac43SStephen Cameron 		return 0;
21049437ac43SStephen Cameron 	case CISS_TMF_INVALID_FRAME:
21059437ac43SStephen Cameron 	case CISS_TMF_NOT_SUPPORTED:
21069437ac43SStephen Cameron 	case CISS_TMF_FAILED:
21079437ac43SStephen Cameron 	case CISS_TMF_WRONG_LUN:
21089437ac43SStephen Cameron 	case CISS_TMF_OVERLAPPED_TAG:
21099437ac43SStephen Cameron 		break;
21109437ac43SStephen Cameron 	default:
21119437ac43SStephen Cameron 		dev_warn(&h->pdev->dev, "Unknown TMF status: 0x%02x\n",
21129437ac43SStephen Cameron 				tmf_status);
21139437ac43SStephen Cameron 		break;
21149437ac43SStephen Cameron 	}
21159437ac43SStephen Cameron 	return -tmf_status;
21169437ac43SStephen Cameron }
21179437ac43SStephen Cameron 
21181fb011fbSStephen M. Cameron static void complete_scsi_command(struct CommandList *cp)
2119edd16368SStephen M. Cameron {
2120edd16368SStephen M. Cameron 	struct scsi_cmnd *cmd;
2121edd16368SStephen M. Cameron 	struct ctlr_info *h;
2122edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2123283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
2124d9a729f3SWebb Scales 	struct io_accel2_cmd *c2;
2125edd16368SStephen M. Cameron 
21269437ac43SStephen Cameron 	u8 sense_key;
21279437ac43SStephen Cameron 	u8 asc;      /* additional sense code */
21289437ac43SStephen Cameron 	u8 ascq;     /* additional sense code qualifier */
2129db111e18SStephen M. Cameron 	unsigned long sense_data_size;
2130edd16368SStephen M. Cameron 
2131edd16368SStephen M. Cameron 	ei = cp->err_info;
21327fa3030cSStephen Cameron 	cmd = cp->scsi_cmd;
2133edd16368SStephen M. Cameron 	h = cp->h;
2134283b4a9bSStephen M. Cameron 	dev = cmd->device->hostdata;
2135d9a729f3SWebb Scales 	c2 = &h->ioaccel2_cmd_pool[cp->cmdindex];
2136edd16368SStephen M. Cameron 
2137edd16368SStephen M. Cameron 	scsi_dma_unmap(cmd); /* undo the DMA mappings */
2138e1f7de0cSMatt Gates 	if ((cp->cmd_type == CMD_SCSI) &&
21392b08b3e9SDon Brace 		(le16_to_cpu(cp->Header.SGTotal) > h->max_cmd_sg_entries))
214033a2ffceSStephen M. Cameron 		hpsa_unmap_sg_chain_block(h, cp);
2141edd16368SStephen M. Cameron 
2142d9a729f3SWebb Scales 	if ((cp->cmd_type == CMD_IOACCEL2) &&
2143d9a729f3SWebb Scales 		(c2->sg[0].chain_indicator == IOACCEL2_CHAIN))
2144d9a729f3SWebb Scales 		hpsa_unmap_ioaccel2_sg_chain_block(h, c2);
2145d9a729f3SWebb Scales 
2146edd16368SStephen M. Cameron 	cmd->result = (DID_OK << 16); 		/* host byte */
2147edd16368SStephen M. Cameron 	cmd->result |= (COMMAND_COMPLETE << 8);	/* msg byte */
2148c349775eSScott Teel 
214903383736SDon Brace 	if (cp->cmd_type == CMD_IOACCEL2 || cp->cmd_type == CMD_IOACCEL1)
215003383736SDon Brace 		atomic_dec(&cp->phys_disk->ioaccel_cmds_out);
215103383736SDon Brace 
215225163bd5SWebb Scales 	/*
215325163bd5SWebb Scales 	 * We check for lockup status here as it may be set for
215425163bd5SWebb Scales 	 * CMD_SCSI, CMD_IOACCEL1 and CMD_IOACCEL2 commands by
215525163bd5SWebb Scales 	 * fail_all_oustanding_cmds()
215625163bd5SWebb Scales 	 */
215725163bd5SWebb Scales 	if (unlikely(ei->CommandStatus == CMD_CTLR_LOCKUP)) {
215825163bd5SWebb Scales 		/* DID_NO_CONNECT will prevent a retry */
215925163bd5SWebb Scales 		cmd->result = DID_NO_CONNECT << 16;
21608a0ff92cSWebb Scales 		return hpsa_cmd_free_and_done(h, cp, cmd);
216125163bd5SWebb Scales 	}
216225163bd5SWebb Scales 
2163c349775eSScott Teel 	if (cp->cmd_type == CMD_IOACCEL2)
2164c349775eSScott Teel 		return process_ioaccel2_completion(h, cp, cmd, dev);
2165c349775eSScott Teel 
21666aa4c361SRobert Elliott 	scsi_set_resid(cmd, ei->ResidualCnt);
21678a0ff92cSWebb Scales 	if (ei->CommandStatus == 0)
21688a0ff92cSWebb Scales 		return hpsa_cmd_free_and_done(h, cp, cmd);
21696aa4c361SRobert Elliott 
2170e1f7de0cSMatt Gates 	/* For I/O accelerator commands, copy over some fields to the normal
2171e1f7de0cSMatt Gates 	 * CISS header used below for error handling.
2172e1f7de0cSMatt Gates 	 */
2173e1f7de0cSMatt Gates 	if (cp->cmd_type == CMD_IOACCEL1) {
2174e1f7de0cSMatt Gates 		struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
21752b08b3e9SDon Brace 		cp->Header.SGList = scsi_sg_count(cmd);
21762b08b3e9SDon Brace 		cp->Header.SGTotal = cpu_to_le16(cp->Header.SGList);
21772b08b3e9SDon Brace 		cp->Request.CDBLen = le16_to_cpu(c->io_flags) &
21782b08b3e9SDon Brace 			IOACCEL1_IOFLAGS_CDBLEN_MASK;
217950a0decfSStephen M. Cameron 		cp->Header.tag = c->tag;
2180e1f7de0cSMatt Gates 		memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
2181e1f7de0cSMatt Gates 		memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
2182283b4a9bSStephen M. Cameron 
2183283b4a9bSStephen M. Cameron 		/* Any RAID offload error results in retry which will use
2184283b4a9bSStephen M. Cameron 		 * the normal I/O path so the controller can handle whatever's
2185283b4a9bSStephen M. Cameron 		 * wrong.
2186283b4a9bSStephen M. Cameron 		 */
2187283b4a9bSStephen M. Cameron 		if (is_logical_dev_addr_mode(dev->scsi3addr)) {
2188283b4a9bSStephen M. Cameron 			if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
2189283b4a9bSStephen M. Cameron 				dev->offload_enabled = 0;
2190a58e7e53SWebb Scales 			if (!cp->abort_pending)
21918a0ff92cSWebb Scales 				return hpsa_retry_cmd(h, cp);
2192283b4a9bSStephen M. Cameron 		}
2193e1f7de0cSMatt Gates 	}
2194e1f7de0cSMatt Gates 
2195a58e7e53SWebb Scales 	if (cp->abort_pending)
2196a58e7e53SWebb Scales 		ei->CommandStatus = CMD_ABORTED;
2197a58e7e53SWebb Scales 
2198edd16368SStephen M. Cameron 	/* an error has occurred */
2199edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
2200edd16368SStephen M. Cameron 
2201edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
22029437ac43SStephen Cameron 		cmd->result |= ei->ScsiStatus;
22039437ac43SStephen Cameron 		/* copy the sense data */
22049437ac43SStephen Cameron 		if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
22059437ac43SStephen Cameron 			sense_data_size = SCSI_SENSE_BUFFERSIZE;
22069437ac43SStephen Cameron 		else
22079437ac43SStephen Cameron 			sense_data_size = sizeof(ei->SenseInfo);
22089437ac43SStephen Cameron 		if (ei->SenseLen < sense_data_size)
22099437ac43SStephen Cameron 			sense_data_size = ei->SenseLen;
22109437ac43SStephen Cameron 		memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
22119437ac43SStephen Cameron 		if (ei->ScsiStatus)
22129437ac43SStephen Cameron 			decode_sense_data(ei->SenseInfo, sense_data_size,
22139437ac43SStephen Cameron 				&sense_key, &asc, &ascq);
2214edd16368SStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
22151d3b3609SMatt Gates 			if (sense_key == ABORTED_COMMAND) {
22162e311fbaSStephen M. Cameron 				cmd->result |= DID_SOFT_ERROR << 16;
22171d3b3609SMatt Gates 				break;
22181d3b3609SMatt Gates 			}
2219edd16368SStephen M. Cameron 			break;
2220edd16368SStephen M. Cameron 		}
2221edd16368SStephen M. Cameron 		/* Problem was not a check condition
2222edd16368SStephen M. Cameron 		 * Pass it up to the upper layers...
2223edd16368SStephen M. Cameron 		 */
2224edd16368SStephen M. Cameron 		if (ei->ScsiStatus) {
2225edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
2226edd16368SStephen M. Cameron 				"Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
2227edd16368SStephen M. Cameron 				"Returning result: 0x%x\n",
2228edd16368SStephen M. Cameron 				cp, ei->ScsiStatus,
2229edd16368SStephen M. Cameron 				sense_key, asc, ascq,
2230edd16368SStephen M. Cameron 				cmd->result);
2231edd16368SStephen M. Cameron 		} else {  /* scsi status is zero??? How??? */
2232edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
2233edd16368SStephen M. Cameron 				"Returning no connection.\n", cp),
2234edd16368SStephen M. Cameron 
2235edd16368SStephen M. Cameron 			/* Ordinarily, this case should never happen,
2236edd16368SStephen M. Cameron 			 * but there is a bug in some released firmware
2237edd16368SStephen M. Cameron 			 * revisions that allows it to happen if, for
2238edd16368SStephen M. Cameron 			 * example, a 4100 backplane loses power and
2239edd16368SStephen M. Cameron 			 * the tape drive is in it.  We assume that
2240edd16368SStephen M. Cameron 			 * it's a fatal error of some kind because we
2241edd16368SStephen M. Cameron 			 * can't show that it wasn't. We will make it
2242edd16368SStephen M. Cameron 			 * look like selection timeout since that is
2243edd16368SStephen M. Cameron 			 * the most common reason for this to occur,
2244edd16368SStephen M. Cameron 			 * and it's severe enough.
2245edd16368SStephen M. Cameron 			 */
2246edd16368SStephen M. Cameron 
2247edd16368SStephen M. Cameron 			cmd->result = DID_NO_CONNECT << 16;
2248edd16368SStephen M. Cameron 		}
2249edd16368SStephen M. Cameron 		break;
2250edd16368SStephen M. Cameron 
2251edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
2252edd16368SStephen M. Cameron 		break;
2253edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
2254f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev,
2255f42e81e1SStephen Cameron 			"CDB %16phN data overrun\n", cp->Request.CDB);
2256edd16368SStephen M. Cameron 		break;
2257edd16368SStephen M. Cameron 	case CMD_INVALID: {
2258edd16368SStephen M. Cameron 		/* print_bytes(cp, sizeof(*cp), 1, 0);
2259edd16368SStephen M. Cameron 		print_cmd(cp); */
2260edd16368SStephen M. Cameron 		/* We get CMD_INVALID if you address a non-existent device
2261edd16368SStephen M. Cameron 		 * instead of a selection timeout (no response).  You will
2262edd16368SStephen M. Cameron 		 * see this if you yank out a drive, then try to access it.
2263edd16368SStephen M. Cameron 		 * This is kind of a shame because it means that any other
2264edd16368SStephen M. Cameron 		 * CMD_INVALID (e.g. driver bug) will get interpreted as a
2265edd16368SStephen M. Cameron 		 * missing target. */
2266edd16368SStephen M. Cameron 		cmd->result = DID_NO_CONNECT << 16;
2267edd16368SStephen M. Cameron 	}
2268edd16368SStephen M. Cameron 		break;
2269edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
2270256d0eaaSStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2271f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : protocol error\n",
2272f42e81e1SStephen Cameron 				cp->Request.CDB);
2273edd16368SStephen M. Cameron 		break;
2274edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
2275edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2276f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : hardware error\n",
2277f42e81e1SStephen Cameron 			cp->Request.CDB);
2278edd16368SStephen M. Cameron 		break;
2279edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
2280edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2281f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : connection lost\n",
2282f42e81e1SStephen Cameron 			cp->Request.CDB);
2283edd16368SStephen M. Cameron 		break;
2284edd16368SStephen M. Cameron 	case CMD_ABORTED:
2285a58e7e53SWebb Scales 		/* Return now to avoid calling scsi_done(). */
2286a58e7e53SWebb Scales 		return hpsa_cmd_abort_and_free(h, cp, cmd);
2287edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
2288edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2289f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : abort failed\n",
2290f42e81e1SStephen Cameron 			cp->Request.CDB);
2291edd16368SStephen M. Cameron 		break;
2292edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
2293f6e76055SStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
2294f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : unsolicited abort\n",
2295f42e81e1SStephen Cameron 			cp->Request.CDB);
2296edd16368SStephen M. Cameron 		break;
2297edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
2298edd16368SStephen M. Cameron 		cmd->result = DID_TIME_OUT << 16;
2299f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN timed out\n",
2300f42e81e1SStephen Cameron 			cp->Request.CDB);
2301edd16368SStephen M. Cameron 		break;
23021d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
23031d5e2ed0SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
23041d5e2ed0SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Command unabortable\n");
23051d5e2ed0SStephen M. Cameron 		break;
23069437ac43SStephen Cameron 	case CMD_TMF_STATUS:
23079437ac43SStephen Cameron 		if (hpsa_evaluate_tmf_status(h, cp)) /* TMF failed? */
23089437ac43SStephen Cameron 			cmd->result = DID_ERROR << 16;
23099437ac43SStephen Cameron 		break;
2310283b4a9bSStephen M. Cameron 	case CMD_IOACCEL_DISABLED:
2311283b4a9bSStephen M. Cameron 		/* This only handles the direct pass-through case since RAID
2312283b4a9bSStephen M. Cameron 		 * offload is handled above.  Just attempt a retry.
2313283b4a9bSStephen M. Cameron 		 */
2314283b4a9bSStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16;
2315283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev,
2316283b4a9bSStephen M. Cameron 				"cp %p had HP SSD Smart Path error\n", cp);
2317283b4a9bSStephen M. Cameron 		break;
2318edd16368SStephen M. Cameron 	default:
2319edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2320edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
2321edd16368SStephen M. Cameron 				cp, ei->CommandStatus);
2322edd16368SStephen M. Cameron 	}
23238a0ff92cSWebb Scales 
23248a0ff92cSWebb Scales 	return hpsa_cmd_free_and_done(h, cp, cmd);
2325edd16368SStephen M. Cameron }
2326edd16368SStephen M. Cameron 
2327edd16368SStephen M. Cameron static void hpsa_pci_unmap(struct pci_dev *pdev,
2328edd16368SStephen M. Cameron 	struct CommandList *c, int sg_used, int data_direction)
2329edd16368SStephen M. Cameron {
2330edd16368SStephen M. Cameron 	int i;
2331edd16368SStephen M. Cameron 
233250a0decfSStephen M. Cameron 	for (i = 0; i < sg_used; i++)
233350a0decfSStephen M. Cameron 		pci_unmap_single(pdev, (dma_addr_t) le64_to_cpu(c->SG[i].Addr),
233450a0decfSStephen M. Cameron 				le32_to_cpu(c->SG[i].Len),
2335edd16368SStephen M. Cameron 				data_direction);
2336edd16368SStephen M. Cameron }
2337edd16368SStephen M. Cameron 
2338a2dac136SStephen M. Cameron static int hpsa_map_one(struct pci_dev *pdev,
2339edd16368SStephen M. Cameron 		struct CommandList *cp,
2340edd16368SStephen M. Cameron 		unsigned char *buf,
2341edd16368SStephen M. Cameron 		size_t buflen,
2342edd16368SStephen M. Cameron 		int data_direction)
2343edd16368SStephen M. Cameron {
234401a02ffcSStephen M. Cameron 	u64 addr64;
2345edd16368SStephen M. Cameron 
2346edd16368SStephen M. Cameron 	if (buflen == 0 || data_direction == PCI_DMA_NONE) {
2347edd16368SStephen M. Cameron 		cp->Header.SGList = 0;
234850a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(0);
2349a2dac136SStephen M. Cameron 		return 0;
2350edd16368SStephen M. Cameron 	}
2351edd16368SStephen M. Cameron 
235250a0decfSStephen M. Cameron 	addr64 = pci_map_single(pdev, buf, buflen, data_direction);
2353eceaae18SShuah Khan 	if (dma_mapping_error(&pdev->dev, addr64)) {
2354a2dac136SStephen M. Cameron 		/* Prevent subsequent unmap of something never mapped */
2355eceaae18SShuah Khan 		cp->Header.SGList = 0;
235650a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(0);
2357a2dac136SStephen M. Cameron 		return -1;
2358eceaae18SShuah Khan 	}
235950a0decfSStephen M. Cameron 	cp->SG[0].Addr = cpu_to_le64(addr64);
236050a0decfSStephen M. Cameron 	cp->SG[0].Len = cpu_to_le32(buflen);
236150a0decfSStephen M. Cameron 	cp->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* we are not chaining */
236250a0decfSStephen M. Cameron 	cp->Header.SGList = 1;   /* no. SGs contig in this cmd */
236350a0decfSStephen M. Cameron 	cp->Header.SGTotal = cpu_to_le16(1); /* total sgs in cmd list */
2364a2dac136SStephen M. Cameron 	return 0;
2365edd16368SStephen M. Cameron }
2366edd16368SStephen M. Cameron 
236725163bd5SWebb Scales #define NO_TIMEOUT ((unsigned long) -1)
236825163bd5SWebb Scales #define DEFAULT_TIMEOUT 30000 /* milliseconds */
236925163bd5SWebb Scales static int hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
237025163bd5SWebb Scales 	struct CommandList *c, int reply_queue, unsigned long timeout_msecs)
2371edd16368SStephen M. Cameron {
2372edd16368SStephen M. Cameron 	DECLARE_COMPLETION_ONSTACK(wait);
2373edd16368SStephen M. Cameron 
2374edd16368SStephen M. Cameron 	c->waiting = &wait;
237525163bd5SWebb Scales 	__enqueue_cmd_and_start_io(h, c, reply_queue);
237625163bd5SWebb Scales 	if (timeout_msecs == NO_TIMEOUT) {
237725163bd5SWebb Scales 		/* TODO: get rid of this no-timeout thing */
237825163bd5SWebb Scales 		wait_for_completion_io(&wait);
237925163bd5SWebb Scales 		return IO_OK;
238025163bd5SWebb Scales 	}
238125163bd5SWebb Scales 	if (!wait_for_completion_io_timeout(&wait,
238225163bd5SWebb Scales 					msecs_to_jiffies(timeout_msecs))) {
238325163bd5SWebb Scales 		dev_warn(&h->pdev->dev, "Command timed out.\n");
238425163bd5SWebb Scales 		return -ETIMEDOUT;
238525163bd5SWebb Scales 	}
238625163bd5SWebb Scales 	return IO_OK;
238725163bd5SWebb Scales }
238825163bd5SWebb Scales 
238925163bd5SWebb Scales static int hpsa_scsi_do_simple_cmd(struct ctlr_info *h, struct CommandList *c,
239025163bd5SWebb Scales 				   int reply_queue, unsigned long timeout_msecs)
239125163bd5SWebb Scales {
239225163bd5SWebb Scales 	if (unlikely(lockup_detected(h))) {
239325163bd5SWebb Scales 		c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
239425163bd5SWebb Scales 		return IO_OK;
239525163bd5SWebb Scales 	}
239625163bd5SWebb Scales 	return hpsa_scsi_do_simple_cmd_core(h, c, reply_queue, timeout_msecs);
2397edd16368SStephen M. Cameron }
2398edd16368SStephen M. Cameron 
2399094963daSStephen M. Cameron static u32 lockup_detected(struct ctlr_info *h)
2400094963daSStephen M. Cameron {
2401094963daSStephen M. Cameron 	int cpu;
2402094963daSStephen M. Cameron 	u32 rc, *lockup_detected;
2403094963daSStephen M. Cameron 
2404094963daSStephen M. Cameron 	cpu = get_cpu();
2405094963daSStephen M. Cameron 	lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
2406094963daSStephen M. Cameron 	rc = *lockup_detected;
2407094963daSStephen M. Cameron 	put_cpu();
2408094963daSStephen M. Cameron 	return rc;
2409094963daSStephen M. Cameron }
2410094963daSStephen M. Cameron 
24119c2fc160SStephen M. Cameron #define MAX_DRIVER_CMD_RETRIES 25
241225163bd5SWebb Scales static int hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
241325163bd5SWebb Scales 	struct CommandList *c, int data_direction, unsigned long timeout_msecs)
2414edd16368SStephen M. Cameron {
24159c2fc160SStephen M. Cameron 	int backoff_time = 10, retry_count = 0;
241625163bd5SWebb Scales 	int rc;
2417edd16368SStephen M. Cameron 
2418edd16368SStephen M. Cameron 	do {
24197630abd0SJoe Perches 		memset(c->err_info, 0, sizeof(*c->err_info));
242025163bd5SWebb Scales 		rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
242125163bd5SWebb Scales 						  timeout_msecs);
242225163bd5SWebb Scales 		if (rc)
242325163bd5SWebb Scales 			break;
2424edd16368SStephen M. Cameron 		retry_count++;
24259c2fc160SStephen M. Cameron 		if (retry_count > 3) {
24269c2fc160SStephen M. Cameron 			msleep(backoff_time);
24279c2fc160SStephen M. Cameron 			if (backoff_time < 1000)
24289c2fc160SStephen M. Cameron 				backoff_time *= 2;
24299c2fc160SStephen M. Cameron 		}
2430852af20aSMatt Bondurant 	} while ((check_for_unit_attention(h, c) ||
24319c2fc160SStephen M. Cameron 			check_for_busy(h, c)) &&
24329c2fc160SStephen M. Cameron 			retry_count <= MAX_DRIVER_CMD_RETRIES);
2433edd16368SStephen M. Cameron 	hpsa_pci_unmap(h->pdev, c, 1, data_direction);
243425163bd5SWebb Scales 	if (retry_count > MAX_DRIVER_CMD_RETRIES)
243525163bd5SWebb Scales 		rc = -EIO;
243625163bd5SWebb Scales 	return rc;
2437edd16368SStephen M. Cameron }
2438edd16368SStephen M. Cameron 
2439d1e8beacSStephen M. Cameron static void hpsa_print_cmd(struct ctlr_info *h, char *txt,
2440d1e8beacSStephen M. Cameron 				struct CommandList *c)
2441edd16368SStephen M. Cameron {
2442d1e8beacSStephen M. Cameron 	const u8 *cdb = c->Request.CDB;
2443d1e8beacSStephen M. Cameron 	const u8 *lun = c->Header.LUN.LunAddrBytes;
2444edd16368SStephen M. Cameron 
2445d1e8beacSStephen M. Cameron 	dev_warn(&h->pdev->dev, "%s: LUN:%02x%02x%02x%02x%02x%02x%02x%02x"
2446d1e8beacSStephen M. Cameron 	" CDB:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
2447d1e8beacSStephen M. Cameron 		txt, lun[0], lun[1], lun[2], lun[3],
2448d1e8beacSStephen M. Cameron 		lun[4], lun[5], lun[6], lun[7],
2449d1e8beacSStephen M. Cameron 		cdb[0], cdb[1], cdb[2], cdb[3],
2450d1e8beacSStephen M. Cameron 		cdb[4], cdb[5], cdb[6], cdb[7],
2451d1e8beacSStephen M. Cameron 		cdb[8], cdb[9], cdb[10], cdb[11],
2452d1e8beacSStephen M. Cameron 		cdb[12], cdb[13], cdb[14], cdb[15]);
2453d1e8beacSStephen M. Cameron }
2454d1e8beacSStephen M. Cameron 
2455d1e8beacSStephen M. Cameron static void hpsa_scsi_interpret_error(struct ctlr_info *h,
2456d1e8beacSStephen M. Cameron 			struct CommandList *cp)
2457d1e8beacSStephen M. Cameron {
2458d1e8beacSStephen M. Cameron 	const struct ErrorInfo *ei = cp->err_info;
2459d1e8beacSStephen M. Cameron 	struct device *d = &cp->h->pdev->dev;
24609437ac43SStephen Cameron 	u8 sense_key, asc, ascq;
24619437ac43SStephen Cameron 	int sense_len;
2462d1e8beacSStephen M. Cameron 
2463edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
2464edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
24659437ac43SStephen Cameron 		if (ei->SenseLen > sizeof(ei->SenseInfo))
24669437ac43SStephen Cameron 			sense_len = sizeof(ei->SenseInfo);
24679437ac43SStephen Cameron 		else
24689437ac43SStephen Cameron 			sense_len = ei->SenseLen;
24699437ac43SStephen Cameron 		decode_sense_data(ei->SenseInfo, sense_len,
24709437ac43SStephen Cameron 					&sense_key, &asc, &ascq);
2471d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "SCSI status", cp);
2472d1e8beacSStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION)
24739437ac43SStephen Cameron 			dev_warn(d, "SCSI Status = 02, Sense key = 0x%02x, ASC = 0x%02x, ASCQ = 0x%02x\n",
24749437ac43SStephen Cameron 				sense_key, asc, ascq);
2475d1e8beacSStephen M. Cameron 		else
24769437ac43SStephen Cameron 			dev_warn(d, "SCSI Status = 0x%02x\n", ei->ScsiStatus);
2477edd16368SStephen M. Cameron 		if (ei->ScsiStatus == 0)
2478edd16368SStephen M. Cameron 			dev_warn(d, "SCSI status is abnormally zero.  "
2479edd16368SStephen M. Cameron 			"(probably indicates selection timeout "
2480edd16368SStephen M. Cameron 			"reported incorrectly due to a known "
2481edd16368SStephen M. Cameron 			"firmware bug, circa July, 2001.)\n");
2482edd16368SStephen M. Cameron 		break;
2483edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
2484edd16368SStephen M. Cameron 		break;
2485edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
2486d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "overrun condition", cp);
2487edd16368SStephen M. Cameron 		break;
2488edd16368SStephen M. Cameron 	case CMD_INVALID: {
2489edd16368SStephen M. Cameron 		/* controller unfortunately reports SCSI passthru's
2490edd16368SStephen M. Cameron 		 * to non-existent targets as invalid commands.
2491edd16368SStephen M. Cameron 		 */
2492d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "invalid command", cp);
2493d1e8beacSStephen M. Cameron 		dev_warn(d, "probably means device no longer present\n");
2494edd16368SStephen M. Cameron 		}
2495edd16368SStephen M. Cameron 		break;
2496edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
2497d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "protocol error", cp);
2498edd16368SStephen M. Cameron 		break;
2499edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
2500d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "hardware error", cp);
2501edd16368SStephen M. Cameron 		break;
2502edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
2503d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "connection lost", cp);
2504edd16368SStephen M. Cameron 		break;
2505edd16368SStephen M. Cameron 	case CMD_ABORTED:
2506d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "aborted", cp);
2507edd16368SStephen M. Cameron 		break;
2508edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
2509d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "abort failed", cp);
2510edd16368SStephen M. Cameron 		break;
2511edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
2512d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unsolicited abort", cp);
2513edd16368SStephen M. Cameron 		break;
2514edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
2515d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "timed out", cp);
2516edd16368SStephen M. Cameron 		break;
25171d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
2518d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unabortable", cp);
25191d5e2ed0SStephen M. Cameron 		break;
252025163bd5SWebb Scales 	case CMD_CTLR_LOCKUP:
252125163bd5SWebb Scales 		hpsa_print_cmd(h, "controller lockup detected", cp);
252225163bd5SWebb Scales 		break;
2523edd16368SStephen M. Cameron 	default:
2524d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unknown status", cp);
2525d1e8beacSStephen M. Cameron 		dev_warn(d, "Unknown command status %x\n",
2526edd16368SStephen M. Cameron 				ei->CommandStatus);
2527edd16368SStephen M. Cameron 	}
2528edd16368SStephen M. Cameron }
2529edd16368SStephen M. Cameron 
2530edd16368SStephen M. Cameron static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
2531b7bb24ebSStephen M. Cameron 			u16 page, unsigned char *buf,
2532edd16368SStephen M. Cameron 			unsigned char bufsize)
2533edd16368SStephen M. Cameron {
2534edd16368SStephen M. Cameron 	int rc = IO_OK;
2535edd16368SStephen M. Cameron 	struct CommandList *c;
2536edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2537edd16368SStephen M. Cameron 
253845fcb86eSStephen Cameron 	c = cmd_alloc(h);
2539edd16368SStephen M. Cameron 
2540a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
2541a2dac136SStephen M. Cameron 			page, scsi3addr, TYPE_CMD)) {
2542a2dac136SStephen M. Cameron 		rc = -1;
2543a2dac136SStephen M. Cameron 		goto out;
2544a2dac136SStephen M. Cameron 	}
254525163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
254625163bd5SWebb Scales 					PCI_DMA_FROMDEVICE, NO_TIMEOUT);
254725163bd5SWebb Scales 	if (rc)
254825163bd5SWebb Scales 		goto out;
2549edd16368SStephen M. Cameron 	ei = c->err_info;
2550edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2551d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2552edd16368SStephen M. Cameron 		rc = -1;
2553edd16368SStephen M. Cameron 	}
2554a2dac136SStephen M. Cameron out:
255545fcb86eSStephen Cameron 	cmd_free(h, c);
2556edd16368SStephen M. Cameron 	return rc;
2557edd16368SStephen M. Cameron }
2558edd16368SStephen M. Cameron 
2559316b221aSStephen M. Cameron static int hpsa_bmic_ctrl_mode_sense(struct ctlr_info *h,
2560316b221aSStephen M. Cameron 		unsigned char *scsi3addr, unsigned char page,
2561316b221aSStephen M. Cameron 		struct bmic_controller_parameters *buf, size_t bufsize)
2562316b221aSStephen M. Cameron {
2563316b221aSStephen M. Cameron 	int rc = IO_OK;
2564316b221aSStephen M. Cameron 	struct CommandList *c;
2565316b221aSStephen M. Cameron 	struct ErrorInfo *ei;
2566316b221aSStephen M. Cameron 
256745fcb86eSStephen Cameron 	c = cmd_alloc(h);
2568316b221aSStephen M. Cameron 	if (fill_cmd(c, BMIC_SENSE_CONTROLLER_PARAMETERS, h, buf, bufsize,
2569316b221aSStephen M. Cameron 			page, scsi3addr, TYPE_CMD)) {
2570316b221aSStephen M. Cameron 		rc = -1;
2571316b221aSStephen M. Cameron 		goto out;
2572316b221aSStephen M. Cameron 	}
257325163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
257425163bd5SWebb Scales 			PCI_DMA_FROMDEVICE, NO_TIMEOUT);
257525163bd5SWebb Scales 	if (rc)
257625163bd5SWebb Scales 		goto out;
2577316b221aSStephen M. Cameron 	ei = c->err_info;
2578316b221aSStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2579316b221aSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2580316b221aSStephen M. Cameron 		rc = -1;
2581316b221aSStephen M. Cameron 	}
2582316b221aSStephen M. Cameron out:
258345fcb86eSStephen Cameron 	cmd_free(h, c);
2584316b221aSStephen M. Cameron 	return rc;
2585316b221aSStephen M. Cameron }
2586316b221aSStephen M. Cameron 
2587bf711ac6SScott Teel static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
258825163bd5SWebb Scales 	u8 reset_type, int reply_queue)
2589edd16368SStephen M. Cameron {
2590edd16368SStephen M. Cameron 	int rc = IO_OK;
2591edd16368SStephen M. Cameron 	struct CommandList *c;
2592edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2593edd16368SStephen M. Cameron 
259445fcb86eSStephen Cameron 	c = cmd_alloc(h);
2595edd16368SStephen M. Cameron 
2596edd16368SStephen M. Cameron 
2597a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map. */
2598bf711ac6SScott Teel 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
2599bf711ac6SScott Teel 			scsi3addr, TYPE_MSG);
2600bf711ac6SScott Teel 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to LUN reset */
260125163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
260225163bd5SWebb Scales 	if (rc) {
260325163bd5SWebb Scales 		dev_warn(&h->pdev->dev, "Failed to send reset command\n");
260425163bd5SWebb Scales 		goto out;
260525163bd5SWebb Scales 	}
2606edd16368SStephen M. Cameron 	/* no unmap needed here because no data xfer. */
2607edd16368SStephen M. Cameron 
2608edd16368SStephen M. Cameron 	ei = c->err_info;
2609edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0) {
2610d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2611edd16368SStephen M. Cameron 		rc = -1;
2612edd16368SStephen M. Cameron 	}
261325163bd5SWebb Scales out:
261445fcb86eSStephen Cameron 	cmd_free(h, c);
2615edd16368SStephen M. Cameron 	return rc;
2616edd16368SStephen M. Cameron }
2617edd16368SStephen M. Cameron 
2618edd16368SStephen M. Cameron static void hpsa_get_raid_level(struct ctlr_info *h,
2619edd16368SStephen M. Cameron 	unsigned char *scsi3addr, unsigned char *raid_level)
2620edd16368SStephen M. Cameron {
2621edd16368SStephen M. Cameron 	int rc;
2622edd16368SStephen M. Cameron 	unsigned char *buf;
2623edd16368SStephen M. Cameron 
2624edd16368SStephen M. Cameron 	*raid_level = RAID_UNKNOWN;
2625edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2626edd16368SStephen M. Cameron 	if (!buf)
2627edd16368SStephen M. Cameron 		return;
2628b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0xC1, buf, 64);
2629edd16368SStephen M. Cameron 	if (rc == 0)
2630edd16368SStephen M. Cameron 		*raid_level = buf[8];
2631edd16368SStephen M. Cameron 	if (*raid_level > RAID_UNKNOWN)
2632edd16368SStephen M. Cameron 		*raid_level = RAID_UNKNOWN;
2633edd16368SStephen M. Cameron 	kfree(buf);
2634edd16368SStephen M. Cameron 	return;
2635edd16368SStephen M. Cameron }
2636edd16368SStephen M. Cameron 
2637283b4a9bSStephen M. Cameron #define HPSA_MAP_DEBUG
2638283b4a9bSStephen M. Cameron #ifdef HPSA_MAP_DEBUG
2639283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
2640283b4a9bSStephen M. Cameron 				struct raid_map_data *map_buff)
2641283b4a9bSStephen M. Cameron {
2642283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map_buff->data[0];
2643283b4a9bSStephen M. Cameron 	int map, row, col;
2644283b4a9bSStephen M. Cameron 	u16 map_cnt, row_cnt, disks_per_row;
2645283b4a9bSStephen M. Cameron 
2646283b4a9bSStephen M. Cameron 	if (rc != 0)
2647283b4a9bSStephen M. Cameron 		return;
2648283b4a9bSStephen M. Cameron 
26492ba8bfc8SStephen M. Cameron 	/* Show details only if debugging has been activated. */
26502ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug < 2)
26512ba8bfc8SStephen M. Cameron 		return;
26522ba8bfc8SStephen M. Cameron 
2653283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "structure_size = %u\n",
2654283b4a9bSStephen M. Cameron 				le32_to_cpu(map_buff->structure_size));
2655283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
2656283b4a9bSStephen M. Cameron 			le32_to_cpu(map_buff->volume_blk_size));
2657283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
2658283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->volume_blk_cnt));
2659283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
2660283b4a9bSStephen M. Cameron 			map_buff->phys_blk_shift);
2661283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
2662283b4a9bSStephen M. Cameron 			map_buff->parity_rotation_shift);
2663283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "strip_size = %u\n",
2664283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->strip_size));
2665283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
2666283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_starting_blk));
2667283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
2668283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_blk_cnt));
2669283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
2670283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->data_disks_per_row));
2671283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
2672283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->metadata_disks_per_row));
2673283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "row_cnt = %u\n",
2674283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->row_cnt));
2675283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "layout_map_count = %u\n",
2676283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->layout_map_count));
26772b08b3e9SDon Brace 	dev_info(&h->pdev->dev, "flags = 0x%x\n",
2678dd0e19f3SScott Teel 			le16_to_cpu(map_buff->flags));
26792b08b3e9SDon Brace 	dev_info(&h->pdev->dev, "encrypytion = %s\n",
26802b08b3e9SDon Brace 			le16_to_cpu(map_buff->flags) &
26812b08b3e9SDon Brace 			RAID_MAP_FLAG_ENCRYPT_ON ?  "ON" : "OFF");
2682dd0e19f3SScott Teel 	dev_info(&h->pdev->dev, "dekindex = %u\n",
2683dd0e19f3SScott Teel 			le16_to_cpu(map_buff->dekindex));
2684283b4a9bSStephen M. Cameron 	map_cnt = le16_to_cpu(map_buff->layout_map_count);
2685283b4a9bSStephen M. Cameron 	for (map = 0; map < map_cnt; map++) {
2686283b4a9bSStephen M. Cameron 		dev_info(&h->pdev->dev, "Map%u:\n", map);
2687283b4a9bSStephen M. Cameron 		row_cnt = le16_to_cpu(map_buff->row_cnt);
2688283b4a9bSStephen M. Cameron 		for (row = 0; row < row_cnt; row++) {
2689283b4a9bSStephen M. Cameron 			dev_info(&h->pdev->dev, "  Row%u:\n", row);
2690283b4a9bSStephen M. Cameron 			disks_per_row =
2691283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->data_disks_per_row);
2692283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2693283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2694283b4a9bSStephen M. Cameron 					"    D%02u: h=0x%04x xor=%u,%u\n",
2695283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2696283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2697283b4a9bSStephen M. Cameron 			disks_per_row =
2698283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->metadata_disks_per_row);
2699283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2700283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2701283b4a9bSStephen M. Cameron 					"    M%02u: h=0x%04x xor=%u,%u\n",
2702283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2703283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2704283b4a9bSStephen M. Cameron 		}
2705283b4a9bSStephen M. Cameron 	}
2706283b4a9bSStephen M. Cameron }
2707283b4a9bSStephen M. Cameron #else
2708283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
2709283b4a9bSStephen M. Cameron 			__attribute__((unused)) int rc,
2710283b4a9bSStephen M. Cameron 			__attribute__((unused)) struct raid_map_data *map_buff)
2711283b4a9bSStephen M. Cameron {
2712283b4a9bSStephen M. Cameron }
2713283b4a9bSStephen M. Cameron #endif
2714283b4a9bSStephen M. Cameron 
2715283b4a9bSStephen M. Cameron static int hpsa_get_raid_map(struct ctlr_info *h,
2716283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2717283b4a9bSStephen M. Cameron {
2718283b4a9bSStephen M. Cameron 	int rc = 0;
2719283b4a9bSStephen M. Cameron 	struct CommandList *c;
2720283b4a9bSStephen M. Cameron 	struct ErrorInfo *ei;
2721283b4a9bSStephen M. Cameron 
272245fcb86eSStephen Cameron 	c = cmd_alloc(h);
2723bf43caf3SRobert Elliott 
2724283b4a9bSStephen M. Cameron 	if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
2725283b4a9bSStephen M. Cameron 			sizeof(this_device->raid_map), 0,
2726283b4a9bSStephen M. Cameron 			scsi3addr, TYPE_CMD)) {
27272dd02d74SRobert Elliott 		dev_warn(&h->pdev->dev, "hpsa_get_raid_map fill_cmd failed\n");
27282dd02d74SRobert Elliott 		cmd_free(h, c);
27292dd02d74SRobert Elliott 		return -1;
2730283b4a9bSStephen M. Cameron 	}
273125163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
273225163bd5SWebb Scales 					PCI_DMA_FROMDEVICE, NO_TIMEOUT);
273325163bd5SWebb Scales 	if (rc)
273425163bd5SWebb Scales 		goto out;
2735283b4a9bSStephen M. Cameron 	ei = c->err_info;
2736283b4a9bSStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2737d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
273825163bd5SWebb Scales 		rc = -1;
273925163bd5SWebb Scales 		goto out;
2740283b4a9bSStephen M. Cameron 	}
274145fcb86eSStephen Cameron 	cmd_free(h, c);
2742283b4a9bSStephen M. Cameron 
2743283b4a9bSStephen M. Cameron 	/* @todo in the future, dynamically allocate RAID map memory */
2744283b4a9bSStephen M. Cameron 	if (le32_to_cpu(this_device->raid_map.structure_size) >
2745283b4a9bSStephen M. Cameron 				sizeof(this_device->raid_map)) {
2746283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
2747283b4a9bSStephen M. Cameron 		rc = -1;
2748283b4a9bSStephen M. Cameron 	}
2749283b4a9bSStephen M. Cameron 	hpsa_debug_map_buff(h, rc, &this_device->raid_map);
2750283b4a9bSStephen M. Cameron 	return rc;
275125163bd5SWebb Scales out:
275225163bd5SWebb Scales 	cmd_free(h, c);
275325163bd5SWebb Scales 	return rc;
2754283b4a9bSStephen M. Cameron }
2755283b4a9bSStephen M. Cameron 
275603383736SDon Brace static int hpsa_bmic_id_physical_device(struct ctlr_info *h,
275703383736SDon Brace 		unsigned char scsi3addr[], u16 bmic_device_index,
275803383736SDon Brace 		struct bmic_identify_physical_device *buf, size_t bufsize)
275903383736SDon Brace {
276003383736SDon Brace 	int rc = IO_OK;
276103383736SDon Brace 	struct CommandList *c;
276203383736SDon Brace 	struct ErrorInfo *ei;
276303383736SDon Brace 
276403383736SDon Brace 	c = cmd_alloc(h);
276503383736SDon Brace 	rc = fill_cmd(c, BMIC_IDENTIFY_PHYSICAL_DEVICE, h, buf, bufsize,
276603383736SDon Brace 		0, RAID_CTLR_LUNID, TYPE_CMD);
276703383736SDon Brace 	if (rc)
276803383736SDon Brace 		goto out;
276903383736SDon Brace 
277003383736SDon Brace 	c->Request.CDB[2] = bmic_device_index & 0xff;
277103383736SDon Brace 	c->Request.CDB[9] = (bmic_device_index >> 8) & 0xff;
277203383736SDon Brace 
277325163bd5SWebb Scales 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE,
277425163bd5SWebb Scales 						NO_TIMEOUT);
277503383736SDon Brace 	ei = c->err_info;
277603383736SDon Brace 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
277703383736SDon Brace 		hpsa_scsi_interpret_error(h, c);
277803383736SDon Brace 		rc = -1;
277903383736SDon Brace 	}
278003383736SDon Brace out:
278103383736SDon Brace 	cmd_free(h, c);
278203383736SDon Brace 	return rc;
278303383736SDon Brace }
278403383736SDon Brace 
27851b70150aSStephen M. Cameron static int hpsa_vpd_page_supported(struct ctlr_info *h,
27861b70150aSStephen M. Cameron 	unsigned char scsi3addr[], u8 page)
27871b70150aSStephen M. Cameron {
27881b70150aSStephen M. Cameron 	int rc;
27891b70150aSStephen M. Cameron 	int i;
27901b70150aSStephen M. Cameron 	int pages;
27911b70150aSStephen M. Cameron 	unsigned char *buf, bufsize;
27921b70150aSStephen M. Cameron 
27931b70150aSStephen M. Cameron 	buf = kzalloc(256, GFP_KERNEL);
27941b70150aSStephen M. Cameron 	if (!buf)
27951b70150aSStephen M. Cameron 		return 0;
27961b70150aSStephen M. Cameron 
27971b70150aSStephen M. Cameron 	/* Get the size of the page list first */
27981b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
27991b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
28001b70150aSStephen M. Cameron 				buf, HPSA_VPD_HEADER_SZ);
28011b70150aSStephen M. Cameron 	if (rc != 0)
28021b70150aSStephen M. Cameron 		goto exit_unsupported;
28031b70150aSStephen M. Cameron 	pages = buf[3];
28041b70150aSStephen M. Cameron 	if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
28051b70150aSStephen M. Cameron 		bufsize = pages + HPSA_VPD_HEADER_SZ;
28061b70150aSStephen M. Cameron 	else
28071b70150aSStephen M. Cameron 		bufsize = 255;
28081b70150aSStephen M. Cameron 
28091b70150aSStephen M. Cameron 	/* Get the whole VPD page list */
28101b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
28111b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
28121b70150aSStephen M. Cameron 				buf, bufsize);
28131b70150aSStephen M. Cameron 	if (rc != 0)
28141b70150aSStephen M. Cameron 		goto exit_unsupported;
28151b70150aSStephen M. Cameron 
28161b70150aSStephen M. Cameron 	pages = buf[3];
28171b70150aSStephen M. Cameron 	for (i = 1; i <= pages; i++)
28181b70150aSStephen M. Cameron 		if (buf[3 + i] == page)
28191b70150aSStephen M. Cameron 			goto exit_supported;
28201b70150aSStephen M. Cameron exit_unsupported:
28211b70150aSStephen M. Cameron 	kfree(buf);
28221b70150aSStephen M. Cameron 	return 0;
28231b70150aSStephen M. Cameron exit_supported:
28241b70150aSStephen M. Cameron 	kfree(buf);
28251b70150aSStephen M. Cameron 	return 1;
28261b70150aSStephen M. Cameron }
28271b70150aSStephen M. Cameron 
2828283b4a9bSStephen M. Cameron static void hpsa_get_ioaccel_status(struct ctlr_info *h,
2829283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2830283b4a9bSStephen M. Cameron {
2831283b4a9bSStephen M. Cameron 	int rc;
2832283b4a9bSStephen M. Cameron 	unsigned char *buf;
2833283b4a9bSStephen M. Cameron 	u8 ioaccel_status;
2834283b4a9bSStephen M. Cameron 
2835283b4a9bSStephen M. Cameron 	this_device->offload_config = 0;
2836283b4a9bSStephen M. Cameron 	this_device->offload_enabled = 0;
283741ce4c35SStephen Cameron 	this_device->offload_to_be_enabled = 0;
2838283b4a9bSStephen M. Cameron 
2839283b4a9bSStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2840283b4a9bSStephen M. Cameron 	if (!buf)
2841283b4a9bSStephen M. Cameron 		return;
28421b70150aSStephen M. Cameron 	if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
28431b70150aSStephen M. Cameron 		goto out;
2844283b4a9bSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
2845b7bb24ebSStephen M. Cameron 			VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
2846283b4a9bSStephen M. Cameron 	if (rc != 0)
2847283b4a9bSStephen M. Cameron 		goto out;
2848283b4a9bSStephen M. Cameron 
2849283b4a9bSStephen M. Cameron #define IOACCEL_STATUS_BYTE 4
2850283b4a9bSStephen M. Cameron #define OFFLOAD_CONFIGURED_BIT 0x01
2851283b4a9bSStephen M. Cameron #define OFFLOAD_ENABLED_BIT 0x02
2852283b4a9bSStephen M. Cameron 	ioaccel_status = buf[IOACCEL_STATUS_BYTE];
2853283b4a9bSStephen M. Cameron 	this_device->offload_config =
2854283b4a9bSStephen M. Cameron 		!!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
2855283b4a9bSStephen M. Cameron 	if (this_device->offload_config) {
2856283b4a9bSStephen M. Cameron 		this_device->offload_enabled =
2857283b4a9bSStephen M. Cameron 			!!(ioaccel_status & OFFLOAD_ENABLED_BIT);
2858283b4a9bSStephen M. Cameron 		if (hpsa_get_raid_map(h, scsi3addr, this_device))
2859283b4a9bSStephen M. Cameron 			this_device->offload_enabled = 0;
2860283b4a9bSStephen M. Cameron 	}
286141ce4c35SStephen Cameron 	this_device->offload_to_be_enabled = this_device->offload_enabled;
2862283b4a9bSStephen M. Cameron out:
2863283b4a9bSStephen M. Cameron 	kfree(buf);
2864283b4a9bSStephen M. Cameron 	return;
2865283b4a9bSStephen M. Cameron }
2866283b4a9bSStephen M. Cameron 
2867edd16368SStephen M. Cameron /* Get the device id from inquiry page 0x83 */
2868edd16368SStephen M. Cameron static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
2869edd16368SStephen M. Cameron 	unsigned char *device_id, int buflen)
2870edd16368SStephen M. Cameron {
2871edd16368SStephen M. Cameron 	int rc;
2872edd16368SStephen M. Cameron 	unsigned char *buf;
2873edd16368SStephen M. Cameron 
2874edd16368SStephen M. Cameron 	if (buflen > 16)
2875edd16368SStephen M. Cameron 		buflen = 16;
2876edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2877edd16368SStephen M. Cameron 	if (!buf)
2878a84d794dSStephen M. Cameron 		return -ENOMEM;
2879b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0x83, buf, 64);
2880edd16368SStephen M. Cameron 	if (rc == 0)
2881edd16368SStephen M. Cameron 		memcpy(device_id, &buf[8], buflen);
2882edd16368SStephen M. Cameron 	kfree(buf);
2883edd16368SStephen M. Cameron 	return rc != 0;
2884edd16368SStephen M. Cameron }
2885edd16368SStephen M. Cameron 
2886edd16368SStephen M. Cameron static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
288703383736SDon Brace 		void *buf, int bufsize,
2888edd16368SStephen M. Cameron 		int extended_response)
2889edd16368SStephen M. Cameron {
2890edd16368SStephen M. Cameron 	int rc = IO_OK;
2891edd16368SStephen M. Cameron 	struct CommandList *c;
2892edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
2893edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2894edd16368SStephen M. Cameron 
289545fcb86eSStephen Cameron 	c = cmd_alloc(h);
2896bf43caf3SRobert Elliott 
2897e89c0ae7SStephen M. Cameron 	/* address the controller */
2898e89c0ae7SStephen M. Cameron 	memset(scsi3addr, 0, sizeof(scsi3addr));
2899a2dac136SStephen M. Cameron 	if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
2900a2dac136SStephen M. Cameron 		buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
2901a2dac136SStephen M. Cameron 		rc = -1;
2902a2dac136SStephen M. Cameron 		goto out;
2903a2dac136SStephen M. Cameron 	}
2904edd16368SStephen M. Cameron 	if (extended_response)
2905edd16368SStephen M. Cameron 		c->Request.CDB[1] = extended_response;
290625163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
290725163bd5SWebb Scales 					PCI_DMA_FROMDEVICE, NO_TIMEOUT);
290825163bd5SWebb Scales 	if (rc)
290925163bd5SWebb Scales 		goto out;
2910edd16368SStephen M. Cameron 	ei = c->err_info;
2911edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 &&
2912edd16368SStephen M. Cameron 	    ei->CommandStatus != CMD_DATA_UNDERRUN) {
2913d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2914edd16368SStephen M. Cameron 		rc = -1;
2915283b4a9bSStephen M. Cameron 	} else {
291603383736SDon Brace 		struct ReportLUNdata *rld = buf;
291703383736SDon Brace 
291803383736SDon Brace 		if (rld->extended_response_flag != extended_response) {
2919283b4a9bSStephen M. Cameron 			dev_err(&h->pdev->dev,
2920283b4a9bSStephen M. Cameron 				"report luns requested format %u, got %u\n",
2921283b4a9bSStephen M. Cameron 				extended_response,
292203383736SDon Brace 				rld->extended_response_flag);
2923283b4a9bSStephen M. Cameron 			rc = -1;
2924283b4a9bSStephen M. Cameron 		}
2925edd16368SStephen M. Cameron 	}
2926a2dac136SStephen M. Cameron out:
292745fcb86eSStephen Cameron 	cmd_free(h, c);
2928edd16368SStephen M. Cameron 	return rc;
2929edd16368SStephen M. Cameron }
2930edd16368SStephen M. Cameron 
2931edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
293203383736SDon Brace 		struct ReportExtendedLUNdata *buf, int bufsize)
2933edd16368SStephen M. Cameron {
293403383736SDon Brace 	return hpsa_scsi_do_report_luns(h, 0, buf, bufsize,
293503383736SDon Brace 						HPSA_REPORT_PHYS_EXTENDED);
2936edd16368SStephen M. Cameron }
2937edd16368SStephen M. Cameron 
2938edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
2939edd16368SStephen M. Cameron 		struct ReportLUNdata *buf, int bufsize)
2940edd16368SStephen M. Cameron {
2941edd16368SStephen M. Cameron 	return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
2942edd16368SStephen M. Cameron }
2943edd16368SStephen M. Cameron 
2944edd16368SStephen M. Cameron static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
2945edd16368SStephen M. Cameron 	int bus, int target, int lun)
2946edd16368SStephen M. Cameron {
2947edd16368SStephen M. Cameron 	device->bus = bus;
2948edd16368SStephen M. Cameron 	device->target = target;
2949edd16368SStephen M. Cameron 	device->lun = lun;
2950edd16368SStephen M. Cameron }
2951edd16368SStephen M. Cameron 
29529846590eSStephen M. Cameron /* Use VPD inquiry to get details of volume status */
29539846590eSStephen M. Cameron static int hpsa_get_volume_status(struct ctlr_info *h,
29549846590eSStephen M. Cameron 					unsigned char scsi3addr[])
29559846590eSStephen M. Cameron {
29569846590eSStephen M. Cameron 	int rc;
29579846590eSStephen M. Cameron 	int status;
29589846590eSStephen M. Cameron 	int size;
29599846590eSStephen M. Cameron 	unsigned char *buf;
29609846590eSStephen M. Cameron 
29619846590eSStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
29629846590eSStephen M. Cameron 	if (!buf)
29639846590eSStephen M. Cameron 		return HPSA_VPD_LV_STATUS_UNSUPPORTED;
29649846590eSStephen M. Cameron 
29659846590eSStephen M. Cameron 	/* Does controller have VPD for logical volume status? */
296624a4b078SStephen M. Cameron 	if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_STATUS))
29679846590eSStephen M. Cameron 		goto exit_failed;
29689846590eSStephen M. Cameron 
29699846590eSStephen M. Cameron 	/* Get the size of the VPD return buffer */
29709846590eSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
29719846590eSStephen M. Cameron 					buf, HPSA_VPD_HEADER_SZ);
297224a4b078SStephen M. Cameron 	if (rc != 0)
29739846590eSStephen M. Cameron 		goto exit_failed;
29749846590eSStephen M. Cameron 	size = buf[3];
29759846590eSStephen M. Cameron 
29769846590eSStephen M. Cameron 	/* Now get the whole VPD buffer */
29779846590eSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
29789846590eSStephen M. Cameron 					buf, size + HPSA_VPD_HEADER_SZ);
297924a4b078SStephen M. Cameron 	if (rc != 0)
29809846590eSStephen M. Cameron 		goto exit_failed;
29819846590eSStephen M. Cameron 	status = buf[4]; /* status byte */
29829846590eSStephen M. Cameron 
29839846590eSStephen M. Cameron 	kfree(buf);
29849846590eSStephen M. Cameron 	return status;
29859846590eSStephen M. Cameron exit_failed:
29869846590eSStephen M. Cameron 	kfree(buf);
29879846590eSStephen M. Cameron 	return HPSA_VPD_LV_STATUS_UNSUPPORTED;
29889846590eSStephen M. Cameron }
29899846590eSStephen M. Cameron 
29909846590eSStephen M. Cameron /* Determine offline status of a volume.
29919846590eSStephen M. Cameron  * Return either:
29929846590eSStephen M. Cameron  *  0 (not offline)
299367955ba3SStephen M. Cameron  *  0xff (offline for unknown reasons)
29949846590eSStephen M. Cameron  *  # (integer code indicating one of several NOT READY states
29959846590eSStephen M. Cameron  *     describing why a volume is to be kept offline)
29969846590eSStephen M. Cameron  */
299767955ba3SStephen M. Cameron static int hpsa_volume_offline(struct ctlr_info *h,
29989846590eSStephen M. Cameron 					unsigned char scsi3addr[])
29999846590eSStephen M. Cameron {
30009846590eSStephen M. Cameron 	struct CommandList *c;
30019437ac43SStephen Cameron 	unsigned char *sense;
30029437ac43SStephen Cameron 	u8 sense_key, asc, ascq;
30039437ac43SStephen Cameron 	int sense_len;
300425163bd5SWebb Scales 	int rc, ldstat = 0;
30059846590eSStephen M. Cameron 	u16 cmd_status;
30069846590eSStephen M. Cameron 	u8 scsi_status;
30079846590eSStephen M. Cameron #define ASC_LUN_NOT_READY 0x04
30089846590eSStephen M. Cameron #define ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS 0x04
30099846590eSStephen M. Cameron #define ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ 0x02
30109846590eSStephen M. Cameron 
30119846590eSStephen M. Cameron 	c = cmd_alloc(h);
3012bf43caf3SRobert Elliott 
30139846590eSStephen M. Cameron 	(void) fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, scsi3addr, TYPE_CMD);
301425163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
301525163bd5SWebb Scales 	if (rc) {
301625163bd5SWebb Scales 		cmd_free(h, c);
301725163bd5SWebb Scales 		return 0;
301825163bd5SWebb Scales 	}
30199846590eSStephen M. Cameron 	sense = c->err_info->SenseInfo;
30209437ac43SStephen Cameron 	if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
30219437ac43SStephen Cameron 		sense_len = sizeof(c->err_info->SenseInfo);
30229437ac43SStephen Cameron 	else
30239437ac43SStephen Cameron 		sense_len = c->err_info->SenseLen;
30249437ac43SStephen Cameron 	decode_sense_data(sense, sense_len, &sense_key, &asc, &ascq);
30259846590eSStephen M. Cameron 	cmd_status = c->err_info->CommandStatus;
30269846590eSStephen M. Cameron 	scsi_status = c->err_info->ScsiStatus;
30279846590eSStephen M. Cameron 	cmd_free(h, c);
30289846590eSStephen M. Cameron 	/* Is the volume 'not ready'? */
30299846590eSStephen M. Cameron 	if (cmd_status != CMD_TARGET_STATUS ||
30309846590eSStephen M. Cameron 		scsi_status != SAM_STAT_CHECK_CONDITION ||
30319846590eSStephen M. Cameron 		sense_key != NOT_READY ||
30329846590eSStephen M. Cameron 		asc != ASC_LUN_NOT_READY)  {
30339846590eSStephen M. Cameron 		return 0;
30349846590eSStephen M. Cameron 	}
30359846590eSStephen M. Cameron 
30369846590eSStephen M. Cameron 	/* Determine the reason for not ready state */
30379846590eSStephen M. Cameron 	ldstat = hpsa_get_volume_status(h, scsi3addr);
30389846590eSStephen M. Cameron 
30399846590eSStephen M. Cameron 	/* Keep volume offline in certain cases: */
30409846590eSStephen M. Cameron 	switch (ldstat) {
30419846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ERASE:
30429846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_RPI:
30439846590eSStephen M. Cameron 	case HPSA_LV_PENDING_RPI:
30449846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_NO_KEY:
30459846590eSStephen M. Cameron 	case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
30469846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION:
30479846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
30489846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
30499846590eSStephen M. Cameron 		return ldstat;
30509846590eSStephen M. Cameron 	case HPSA_VPD_LV_STATUS_UNSUPPORTED:
30519846590eSStephen M. Cameron 		/* If VPD status page isn't available,
30529846590eSStephen M. Cameron 		 * use ASC/ASCQ to determine state
30539846590eSStephen M. Cameron 		 */
30549846590eSStephen M. Cameron 		if ((ascq == ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS) ||
30559846590eSStephen M. Cameron 			(ascq == ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ))
30569846590eSStephen M. Cameron 			return ldstat;
30579846590eSStephen M. Cameron 		break;
30589846590eSStephen M. Cameron 	default:
30599846590eSStephen M. Cameron 		break;
30609846590eSStephen M. Cameron 	}
30619846590eSStephen M. Cameron 	return 0;
30629846590eSStephen M. Cameron }
30639846590eSStephen M. Cameron 
30649b5c48c2SStephen Cameron /*
30659b5c48c2SStephen Cameron  * Find out if a logical device supports aborts by simply trying one.
30669b5c48c2SStephen Cameron  * Smart Array may claim not to support aborts on logical drives, but
30679b5c48c2SStephen Cameron  * if a MSA2000 * is connected, the drives on that will be presented
30689b5c48c2SStephen Cameron  * by the Smart Array as logical drives, and aborts may be sent to
30699b5c48c2SStephen Cameron  * those devices successfully.  So the simplest way to find out is
30709b5c48c2SStephen Cameron  * to simply try an abort and see how the device responds.
30719b5c48c2SStephen Cameron  */
30729b5c48c2SStephen Cameron static int hpsa_device_supports_aborts(struct ctlr_info *h,
30739b5c48c2SStephen Cameron 					unsigned char *scsi3addr)
30749b5c48c2SStephen Cameron {
30759b5c48c2SStephen Cameron 	struct CommandList *c;
30769b5c48c2SStephen Cameron 	struct ErrorInfo *ei;
30779b5c48c2SStephen Cameron 	int rc = 0;
30789b5c48c2SStephen Cameron 
30799b5c48c2SStephen Cameron 	u64 tag = (u64) -1; /* bogus tag */
30809b5c48c2SStephen Cameron 
30819b5c48c2SStephen Cameron 	/* Assume that physical devices support aborts */
30829b5c48c2SStephen Cameron 	if (!is_logical_dev_addr_mode(scsi3addr))
30839b5c48c2SStephen Cameron 		return 1;
30849b5c48c2SStephen Cameron 
30859b5c48c2SStephen Cameron 	c = cmd_alloc(h);
3086bf43caf3SRobert Elliott 
30879b5c48c2SStephen Cameron 	(void) fill_cmd(c, HPSA_ABORT_MSG, h, &tag, 0, 0, scsi3addr, TYPE_MSG);
30889b5c48c2SStephen Cameron 	(void) hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
30899b5c48c2SStephen Cameron 	/* no unmap needed here because no data xfer. */
30909b5c48c2SStephen Cameron 	ei = c->err_info;
30919b5c48c2SStephen Cameron 	switch (ei->CommandStatus) {
30929b5c48c2SStephen Cameron 	case CMD_INVALID:
30939b5c48c2SStephen Cameron 		rc = 0;
30949b5c48c2SStephen Cameron 		break;
30959b5c48c2SStephen Cameron 	case CMD_UNABORTABLE:
30969b5c48c2SStephen Cameron 	case CMD_ABORT_FAILED:
30979b5c48c2SStephen Cameron 		rc = 1;
30989b5c48c2SStephen Cameron 		break;
30999437ac43SStephen Cameron 	case CMD_TMF_STATUS:
31009437ac43SStephen Cameron 		rc = hpsa_evaluate_tmf_status(h, c);
31019437ac43SStephen Cameron 		break;
31029b5c48c2SStephen Cameron 	default:
31039b5c48c2SStephen Cameron 		rc = 0;
31049b5c48c2SStephen Cameron 		break;
31059b5c48c2SStephen Cameron 	}
31069b5c48c2SStephen Cameron 	cmd_free(h, c);
31079b5c48c2SStephen Cameron 	return rc;
31089b5c48c2SStephen Cameron }
31099b5c48c2SStephen Cameron 
3110edd16368SStephen M. Cameron static int hpsa_update_device_info(struct ctlr_info *h,
31110b0e1d6cSStephen M. Cameron 	unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
31120b0e1d6cSStephen M. Cameron 	unsigned char *is_OBDR_device)
3113edd16368SStephen M. Cameron {
31140b0e1d6cSStephen M. Cameron 
31150b0e1d6cSStephen M. Cameron #define OBDR_SIG_OFFSET 43
31160b0e1d6cSStephen M. Cameron #define OBDR_TAPE_SIG "$DR-10"
31170b0e1d6cSStephen M. Cameron #define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
31180b0e1d6cSStephen M. Cameron #define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
31190b0e1d6cSStephen M. Cameron 
3120ea6d3bc3SStephen M. Cameron 	unsigned char *inq_buff;
31210b0e1d6cSStephen M. Cameron 	unsigned char *obdr_sig;
3122edd16368SStephen M. Cameron 
3123ea6d3bc3SStephen M. Cameron 	inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
3124edd16368SStephen M. Cameron 	if (!inq_buff)
3125edd16368SStephen M. Cameron 		goto bail_out;
3126edd16368SStephen M. Cameron 
3127edd16368SStephen M. Cameron 	/* Do an inquiry to the device to see what it is. */
3128edd16368SStephen M. Cameron 	if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
3129edd16368SStephen M. Cameron 		(unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
3130edd16368SStephen M. Cameron 		/* Inquiry failed (msg printed already) */
3131edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev,
3132edd16368SStephen M. Cameron 			"hpsa_update_device_info: inquiry failed\n");
3133edd16368SStephen M. Cameron 		goto bail_out;
3134edd16368SStephen M. Cameron 	}
3135edd16368SStephen M. Cameron 
3136edd16368SStephen M. Cameron 	this_device->devtype = (inq_buff[0] & 0x1f);
3137edd16368SStephen M. Cameron 	memcpy(this_device->scsi3addr, scsi3addr, 8);
3138edd16368SStephen M. Cameron 	memcpy(this_device->vendor, &inq_buff[8],
3139edd16368SStephen M. Cameron 		sizeof(this_device->vendor));
3140edd16368SStephen M. Cameron 	memcpy(this_device->model, &inq_buff[16],
3141edd16368SStephen M. Cameron 		sizeof(this_device->model));
3142edd16368SStephen M. Cameron 	memset(this_device->device_id, 0,
3143edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
3144edd16368SStephen M. Cameron 	hpsa_get_device_id(h, scsi3addr, this_device->device_id,
3145edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
3146edd16368SStephen M. Cameron 
3147edd16368SStephen M. Cameron 	if (this_device->devtype == TYPE_DISK &&
3148283b4a9bSStephen M. Cameron 		is_logical_dev_addr_mode(scsi3addr)) {
314967955ba3SStephen M. Cameron 		int volume_offline;
315067955ba3SStephen M. Cameron 
3151edd16368SStephen M. Cameron 		hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
3152283b4a9bSStephen M. Cameron 		if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
3153283b4a9bSStephen M. Cameron 			hpsa_get_ioaccel_status(h, scsi3addr, this_device);
315467955ba3SStephen M. Cameron 		volume_offline = hpsa_volume_offline(h, scsi3addr);
315567955ba3SStephen M. Cameron 		if (volume_offline < 0 || volume_offline > 0xff)
315667955ba3SStephen M. Cameron 			volume_offline = HPSA_VPD_LV_STATUS_UNSUPPORTED;
315767955ba3SStephen M. Cameron 		this_device->volume_offline = volume_offline & 0xff;
3158283b4a9bSStephen M. Cameron 	} else {
3159edd16368SStephen M. Cameron 		this_device->raid_level = RAID_UNKNOWN;
3160283b4a9bSStephen M. Cameron 		this_device->offload_config = 0;
3161283b4a9bSStephen M. Cameron 		this_device->offload_enabled = 0;
316241ce4c35SStephen Cameron 		this_device->offload_to_be_enabled = 0;
3163a3144e0bSJoe Handzik 		this_device->hba_ioaccel_enabled = 0;
31649846590eSStephen M. Cameron 		this_device->volume_offline = 0;
316503383736SDon Brace 		this_device->queue_depth = h->nr_cmds;
3166283b4a9bSStephen M. Cameron 	}
3167edd16368SStephen M. Cameron 
31680b0e1d6cSStephen M. Cameron 	if (is_OBDR_device) {
31690b0e1d6cSStephen M. Cameron 		/* See if this is a One-Button-Disaster-Recovery device
31700b0e1d6cSStephen M. Cameron 		 * by looking for "$DR-10" at offset 43 in inquiry data.
31710b0e1d6cSStephen M. Cameron 		 */
31720b0e1d6cSStephen M. Cameron 		obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
31730b0e1d6cSStephen M. Cameron 		*is_OBDR_device = (this_device->devtype == TYPE_ROM &&
31740b0e1d6cSStephen M. Cameron 					strncmp(obdr_sig, OBDR_TAPE_SIG,
31750b0e1d6cSStephen M. Cameron 						OBDR_SIG_LEN) == 0);
31760b0e1d6cSStephen M. Cameron 	}
3177edd16368SStephen M. Cameron 	kfree(inq_buff);
3178edd16368SStephen M. Cameron 	return 0;
3179edd16368SStephen M. Cameron 
3180edd16368SStephen M. Cameron bail_out:
3181edd16368SStephen M. Cameron 	kfree(inq_buff);
3182edd16368SStephen M. Cameron 	return 1;
3183edd16368SStephen M. Cameron }
3184edd16368SStephen M. Cameron 
31859b5c48c2SStephen Cameron static void hpsa_update_device_supports_aborts(struct ctlr_info *h,
31869b5c48c2SStephen Cameron 			struct hpsa_scsi_dev_t *dev, u8 *scsi3addr)
31879b5c48c2SStephen Cameron {
31889b5c48c2SStephen Cameron 	unsigned long flags;
31899b5c48c2SStephen Cameron 	int rc, entry;
31909b5c48c2SStephen Cameron 	/*
31919b5c48c2SStephen Cameron 	 * See if this device supports aborts.  If we already know
31929b5c48c2SStephen Cameron 	 * the device, we already know if it supports aborts, otherwise
31939b5c48c2SStephen Cameron 	 * we have to find out if it supports aborts by trying one.
31949b5c48c2SStephen Cameron 	 */
31959b5c48c2SStephen Cameron 	spin_lock_irqsave(&h->devlock, flags);
31969b5c48c2SStephen Cameron 	rc = hpsa_scsi_find_entry(dev, h->dev, h->ndevices, &entry);
31979b5c48c2SStephen Cameron 	if ((rc == DEVICE_SAME || rc == DEVICE_UPDATED) &&
31989b5c48c2SStephen Cameron 		entry >= 0 && entry < h->ndevices) {
31999b5c48c2SStephen Cameron 		dev->supports_aborts = h->dev[entry]->supports_aborts;
32009b5c48c2SStephen Cameron 		spin_unlock_irqrestore(&h->devlock, flags);
32019b5c48c2SStephen Cameron 	} else {
32029b5c48c2SStephen Cameron 		spin_unlock_irqrestore(&h->devlock, flags);
32039b5c48c2SStephen Cameron 		dev->supports_aborts =
32049b5c48c2SStephen Cameron 				hpsa_device_supports_aborts(h, scsi3addr);
32059b5c48c2SStephen Cameron 		if (dev->supports_aborts < 0)
32069b5c48c2SStephen Cameron 			dev->supports_aborts = 0;
32079b5c48c2SStephen Cameron 	}
32089b5c48c2SStephen Cameron }
32099b5c48c2SStephen Cameron 
32104f4eb9f1SScott Teel static unsigned char *ext_target_model[] = {
3211edd16368SStephen M. Cameron 	"MSA2012",
3212edd16368SStephen M. Cameron 	"MSA2024",
3213edd16368SStephen M. Cameron 	"MSA2312",
3214edd16368SStephen M. Cameron 	"MSA2324",
3215fda38518SStephen M. Cameron 	"P2000 G3 SAS",
3216e06c8e5cSStephen M. Cameron 	"MSA 2040 SAS",
3217edd16368SStephen M. Cameron 	NULL,
3218edd16368SStephen M. Cameron };
3219edd16368SStephen M. Cameron 
32204f4eb9f1SScott Teel static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
3221edd16368SStephen M. Cameron {
3222edd16368SStephen M. Cameron 	int i;
3223edd16368SStephen M. Cameron 
32244f4eb9f1SScott Teel 	for (i = 0; ext_target_model[i]; i++)
32254f4eb9f1SScott Teel 		if (strncmp(device->model, ext_target_model[i],
32264f4eb9f1SScott Teel 			strlen(ext_target_model[i])) == 0)
3227edd16368SStephen M. Cameron 			return 1;
3228edd16368SStephen M. Cameron 	return 0;
3229edd16368SStephen M. Cameron }
3230edd16368SStephen M. Cameron 
3231edd16368SStephen M. Cameron /* Helper function to assign bus, target, lun mapping of devices.
32324f4eb9f1SScott Teel  * Puts non-external target logical volumes on bus 0, external target logical
3233edd16368SStephen M. Cameron  * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
3234edd16368SStephen M. Cameron  * Logical drive target and lun are assigned at this time, but
3235edd16368SStephen M. Cameron  * physical device lun and target assignment are deferred (assigned
3236edd16368SStephen M. Cameron  * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
3237edd16368SStephen M. Cameron  */
3238edd16368SStephen M. Cameron static void figure_bus_target_lun(struct ctlr_info *h,
32391f310bdeSStephen M. Cameron 	u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
3240edd16368SStephen M. Cameron {
32411f310bdeSStephen M. Cameron 	u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
3242edd16368SStephen M. Cameron 
32431f310bdeSStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes)) {
32441f310bdeSStephen M. Cameron 		/* physical device, target and lun filled in later */
32451f310bdeSStephen M. Cameron 		if (is_hba_lunid(lunaddrbytes))
32461f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
32471f310bdeSStephen M. Cameron 		else
32481f310bdeSStephen M. Cameron 			/* defer target, lun assignment for physical devices */
32491f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 2, -1, -1);
32501f310bdeSStephen M. Cameron 		return;
32511f310bdeSStephen M. Cameron 	}
32521f310bdeSStephen M. Cameron 	/* It's a logical device */
32534f4eb9f1SScott Teel 	if (is_ext_target(h, device)) {
32544f4eb9f1SScott Teel 		/* external target way, put logicals on bus 1
3255339b2b14SStephen M. Cameron 		 * and match target/lun numbers box
32561f310bdeSStephen M. Cameron 		 * reports, other smart array, bus 0, target 0, match lunid
3257339b2b14SStephen M. Cameron 		 */
32581f310bdeSStephen M. Cameron 		hpsa_set_bus_target_lun(device,
32591f310bdeSStephen M. Cameron 			1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
32601f310bdeSStephen M. Cameron 		return;
3261339b2b14SStephen M. Cameron 	}
32621f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
3263edd16368SStephen M. Cameron }
3264edd16368SStephen M. Cameron 
3265edd16368SStephen M. Cameron /*
3266edd16368SStephen M. Cameron  * If there is no lun 0 on a target, linux won't find any devices.
32674f4eb9f1SScott Teel  * For the external targets (arrays), we have to manually detect the enclosure
3268edd16368SStephen M. Cameron  * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
3269edd16368SStephen M. Cameron  * it for some reason.  *tmpdevice is the target we're adding,
3270edd16368SStephen M. Cameron  * this_device is a pointer into the current element of currentsd[]
3271edd16368SStephen M. Cameron  * that we're building up in update_scsi_devices(), below.
3272edd16368SStephen M. Cameron  * lunzerobits is a bitmap that tracks which targets already have a
3273edd16368SStephen M. Cameron  * lun 0 assigned.
3274edd16368SStephen M. Cameron  * Returns 1 if an enclosure was added, 0 if not.
3275edd16368SStephen M. Cameron  */
32764f4eb9f1SScott Teel static int add_ext_target_dev(struct ctlr_info *h,
3277edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *tmpdevice,
327801a02ffcSStephen M. Cameron 	struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
32794f4eb9f1SScott Teel 	unsigned long lunzerobits[], int *n_ext_target_devs)
3280edd16368SStephen M. Cameron {
3281edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
3282edd16368SStephen M. Cameron 
32831f310bdeSStephen M. Cameron 	if (test_bit(tmpdevice->target, lunzerobits))
3284edd16368SStephen M. Cameron 		return 0; /* There is already a lun 0 on this target. */
3285edd16368SStephen M. Cameron 
3286edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes))
3287edd16368SStephen M. Cameron 		return 0; /* It's the logical targets that may lack lun 0. */
3288edd16368SStephen M. Cameron 
32894f4eb9f1SScott Teel 	if (!is_ext_target(h, tmpdevice))
32904f4eb9f1SScott Teel 		return 0; /* Only external target devices have this problem. */
3291edd16368SStephen M. Cameron 
32921f310bdeSStephen M. Cameron 	if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
3293edd16368SStephen M. Cameron 		return 0;
3294edd16368SStephen M. Cameron 
3295c4f8a299SStephen M. Cameron 	memset(scsi3addr, 0, 8);
32961f310bdeSStephen M. Cameron 	scsi3addr[3] = tmpdevice->target;
3297edd16368SStephen M. Cameron 	if (is_hba_lunid(scsi3addr))
3298edd16368SStephen M. Cameron 		return 0; /* Don't add the RAID controller here. */
3299edd16368SStephen M. Cameron 
3300339b2b14SStephen M. Cameron 	if (is_scsi_rev_5(h))
3301339b2b14SStephen M. Cameron 		return 0; /* p1210m doesn't need to do this. */
3302339b2b14SStephen M. Cameron 
33034f4eb9f1SScott Teel 	if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
3304aca4a520SScott Teel 		dev_warn(&h->pdev->dev, "Maximum number of external "
3305aca4a520SScott Teel 			"target devices exceeded.  Check your hardware "
3306edd16368SStephen M. Cameron 			"configuration.");
3307edd16368SStephen M. Cameron 		return 0;
3308edd16368SStephen M. Cameron 	}
3309edd16368SStephen M. Cameron 
33100b0e1d6cSStephen M. Cameron 	if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
3311edd16368SStephen M. Cameron 		return 0;
33124f4eb9f1SScott Teel 	(*n_ext_target_devs)++;
33131f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(this_device,
33141f310bdeSStephen M. Cameron 				tmpdevice->bus, tmpdevice->target, 0);
33159b5c48c2SStephen Cameron 	hpsa_update_device_supports_aborts(h, this_device, scsi3addr);
33161f310bdeSStephen M. Cameron 	set_bit(tmpdevice->target, lunzerobits);
3317edd16368SStephen M. Cameron 	return 1;
3318edd16368SStephen M. Cameron }
3319edd16368SStephen M. Cameron 
3320edd16368SStephen M. Cameron /*
332154b6e9e9SScott Teel  * Get address of physical disk used for an ioaccel2 mode command:
332254b6e9e9SScott Teel  *	1. Extract ioaccel2 handle from the command.
332354b6e9e9SScott Teel  *	2. Find a matching ioaccel2 handle from list of physical disks.
332454b6e9e9SScott Teel  *	3. Return:
332554b6e9e9SScott Teel  *		1 and set scsi3addr to address of matching physical
332654b6e9e9SScott Teel  *		0 if no matching physical disk was found.
332754b6e9e9SScott Teel  */
332854b6e9e9SScott Teel static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
332954b6e9e9SScott Teel 	struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
333054b6e9e9SScott Teel {
333141ce4c35SStephen Cameron 	struct io_accel2_cmd *c2 =
333241ce4c35SStephen Cameron 			&h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
333341ce4c35SStephen Cameron 	unsigned long flags;
333454b6e9e9SScott Teel 	int i;
333554b6e9e9SScott Teel 
333641ce4c35SStephen Cameron 	spin_lock_irqsave(&h->devlock, flags);
333741ce4c35SStephen Cameron 	for (i = 0; i < h->ndevices; i++)
333841ce4c35SStephen Cameron 		if (h->dev[i]->ioaccel_handle == le32_to_cpu(c2->scsi_nexus)) {
333941ce4c35SStephen Cameron 			memcpy(scsi3addr, h->dev[i]->scsi3addr,
334041ce4c35SStephen Cameron 				sizeof(h->dev[i]->scsi3addr));
334141ce4c35SStephen Cameron 			spin_unlock_irqrestore(&h->devlock, flags);
334254b6e9e9SScott Teel 			return 1;
334354b6e9e9SScott Teel 		}
334441ce4c35SStephen Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
334541ce4c35SStephen Cameron 	return 0;
334641ce4c35SStephen Cameron }
334741ce4c35SStephen Cameron 
334854b6e9e9SScott Teel /*
3349edd16368SStephen M. Cameron  * Do CISS_REPORT_PHYS and CISS_REPORT_LOG.  Data is returned in physdev,
3350edd16368SStephen M. Cameron  * logdev.  The number of luns in physdev and logdev are returned in
3351edd16368SStephen M. Cameron  * *nphysicals and *nlogicals, respectively.
3352edd16368SStephen M. Cameron  * Returns 0 on success, -1 otherwise.
3353edd16368SStephen M. Cameron  */
3354edd16368SStephen M. Cameron static int hpsa_gather_lun_info(struct ctlr_info *h,
335503383736SDon Brace 	struct ReportExtendedLUNdata *physdev, u32 *nphysicals,
335601a02ffcSStephen M. Cameron 	struct ReportLUNdata *logdev, u32 *nlogicals)
3357edd16368SStephen M. Cameron {
335803383736SDon Brace 	if (hpsa_scsi_do_report_phys_luns(h, physdev, sizeof(*physdev))) {
3359edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
3360edd16368SStephen M. Cameron 		return -1;
3361edd16368SStephen M. Cameron 	}
336203383736SDon Brace 	*nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 24;
3363edd16368SStephen M. Cameron 	if (*nphysicals > HPSA_MAX_PHYS_LUN) {
336403383736SDon Brace 		dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded. %d LUNs ignored.\n",
336503383736SDon Brace 			HPSA_MAX_PHYS_LUN, *nphysicals - HPSA_MAX_PHYS_LUN);
3366edd16368SStephen M. Cameron 		*nphysicals = HPSA_MAX_PHYS_LUN;
3367edd16368SStephen M. Cameron 	}
336803383736SDon Brace 	if (hpsa_scsi_do_report_log_luns(h, logdev, sizeof(*logdev))) {
3369edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
3370edd16368SStephen M. Cameron 		return -1;
3371edd16368SStephen M. Cameron 	}
33726df1e954SStephen M. Cameron 	*nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
3373edd16368SStephen M. Cameron 	/* Reject Logicals in excess of our max capability. */
3374edd16368SStephen M. Cameron 	if (*nlogicals > HPSA_MAX_LUN) {
3375edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
3376edd16368SStephen M. Cameron 			"maximum logical LUNs (%d) exceeded.  "
3377edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_LUN,
3378edd16368SStephen M. Cameron 			*nlogicals - HPSA_MAX_LUN);
3379edd16368SStephen M. Cameron 			*nlogicals = HPSA_MAX_LUN;
3380edd16368SStephen M. Cameron 	}
3381edd16368SStephen M. Cameron 	if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
3382edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
3383edd16368SStephen M. Cameron 			"maximum logical + physical LUNs (%d) exceeded. "
3384edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
3385edd16368SStephen M. Cameron 			*nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
3386edd16368SStephen M. Cameron 		*nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
3387edd16368SStephen M. Cameron 	}
3388edd16368SStephen M. Cameron 	return 0;
3389edd16368SStephen M. Cameron }
3390edd16368SStephen M. Cameron 
339142a91641SDon Brace static u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position,
339242a91641SDon Brace 	int i, int nphysicals, int nlogicals,
3393a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list,
3394339b2b14SStephen M. Cameron 	struct ReportLUNdata *logdev_list)
3395339b2b14SStephen M. Cameron {
3396339b2b14SStephen M. Cameron 	/* Helper function, figure out where the LUN ID info is coming from
3397339b2b14SStephen M. Cameron 	 * given index i, lists of physical and logical devices, where in
3398339b2b14SStephen M. Cameron 	 * the list the raid controller is supposed to appear (first or last)
3399339b2b14SStephen M. Cameron 	 */
3400339b2b14SStephen M. Cameron 
3401339b2b14SStephen M. Cameron 	int logicals_start = nphysicals + (raid_ctlr_position == 0);
3402339b2b14SStephen M. Cameron 	int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
3403339b2b14SStephen M. Cameron 
3404339b2b14SStephen M. Cameron 	if (i == raid_ctlr_position)
3405339b2b14SStephen M. Cameron 		return RAID_CTLR_LUNID;
3406339b2b14SStephen M. Cameron 
3407339b2b14SStephen M. Cameron 	if (i < logicals_start)
3408d5b5d964SStephen M. Cameron 		return &physdev_list->LUN[i -
3409d5b5d964SStephen M. Cameron 				(raid_ctlr_position == 0)].lunid[0];
3410339b2b14SStephen M. Cameron 
3411339b2b14SStephen M. Cameron 	if (i < last_device)
3412339b2b14SStephen M. Cameron 		return &logdev_list->LUN[i - nphysicals -
3413339b2b14SStephen M. Cameron 			(raid_ctlr_position == 0)][0];
3414339b2b14SStephen M. Cameron 	BUG();
3415339b2b14SStephen M. Cameron 	return NULL;
3416339b2b14SStephen M. Cameron }
3417339b2b14SStephen M. Cameron 
3418316b221aSStephen M. Cameron static int hpsa_hba_mode_enabled(struct ctlr_info *h)
3419316b221aSStephen M. Cameron {
3420316b221aSStephen M. Cameron 	int rc;
34216e8e8088SJoe Handzik 	int hba_mode_enabled;
3422316b221aSStephen M. Cameron 	struct bmic_controller_parameters *ctlr_params;
3423316b221aSStephen M. Cameron 	ctlr_params = kzalloc(sizeof(struct bmic_controller_parameters),
3424316b221aSStephen M. Cameron 		GFP_KERNEL);
3425316b221aSStephen M. Cameron 
3426316b221aSStephen M. Cameron 	if (!ctlr_params)
342796444fbbSJoe Handzik 		return -ENOMEM;
3428316b221aSStephen M. Cameron 	rc = hpsa_bmic_ctrl_mode_sense(h, RAID_CTLR_LUNID, 0, ctlr_params,
3429316b221aSStephen M. Cameron 		sizeof(struct bmic_controller_parameters));
343096444fbbSJoe Handzik 	if (rc) {
3431316b221aSStephen M. Cameron 		kfree(ctlr_params);
343296444fbbSJoe Handzik 		return rc;
3433316b221aSStephen M. Cameron 	}
34346e8e8088SJoe Handzik 
34356e8e8088SJoe Handzik 	hba_mode_enabled =
34366e8e8088SJoe Handzik 		((ctlr_params->nvram_flags & HBA_MODE_ENABLED_FLAG) != 0);
34376e8e8088SJoe Handzik 	kfree(ctlr_params);
34386e8e8088SJoe Handzik 	return hba_mode_enabled;
3439316b221aSStephen M. Cameron }
3440316b221aSStephen M. Cameron 
344103383736SDon Brace /* get physical drive ioaccel handle and queue depth */
344203383736SDon Brace static void hpsa_get_ioaccel_drive_info(struct ctlr_info *h,
344303383736SDon Brace 		struct hpsa_scsi_dev_t *dev,
344403383736SDon Brace 		u8 *lunaddrbytes,
344503383736SDon Brace 		struct bmic_identify_physical_device *id_phys)
344603383736SDon Brace {
344703383736SDon Brace 	int rc;
344803383736SDon Brace 	struct ext_report_lun_entry *rle =
344903383736SDon Brace 		(struct ext_report_lun_entry *) lunaddrbytes;
345003383736SDon Brace 
345103383736SDon Brace 	dev->ioaccel_handle = rle->ioaccel_handle;
3452a3144e0bSJoe Handzik 	if (PHYS_IOACCEL(lunaddrbytes) && dev->ioaccel_handle)
3453a3144e0bSJoe Handzik 		dev->hba_ioaccel_enabled = 1;
345403383736SDon Brace 	memset(id_phys, 0, sizeof(*id_phys));
345503383736SDon Brace 	rc = hpsa_bmic_id_physical_device(h, lunaddrbytes,
345603383736SDon Brace 			GET_BMIC_DRIVE_NUMBER(lunaddrbytes), id_phys,
345703383736SDon Brace 			sizeof(*id_phys));
345803383736SDon Brace 	if (!rc)
345903383736SDon Brace 		/* Reserve space for FW operations */
346003383736SDon Brace #define DRIVE_CMDS_RESERVED_FOR_FW 2
346103383736SDon Brace #define DRIVE_QUEUE_DEPTH 7
346203383736SDon Brace 		dev->queue_depth =
346303383736SDon Brace 			le16_to_cpu(id_phys->current_queue_depth_limit) -
346403383736SDon Brace 				DRIVE_CMDS_RESERVED_FOR_FW;
346503383736SDon Brace 	else
346603383736SDon Brace 		dev->queue_depth = DRIVE_QUEUE_DEPTH; /* conservative */
346703383736SDon Brace 	atomic_set(&dev->ioaccel_cmds_out, 0);
346803383736SDon Brace }
346903383736SDon Brace 
3470edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
3471edd16368SStephen M. Cameron {
3472edd16368SStephen M. Cameron 	/* the idea here is we could get notified
3473edd16368SStephen M. Cameron 	 * that some devices have changed, so we do a report
3474edd16368SStephen M. Cameron 	 * physical luns and report logical luns cmd, and adjust
3475edd16368SStephen M. Cameron 	 * our list of devices accordingly.
3476edd16368SStephen M. Cameron 	 *
3477edd16368SStephen M. Cameron 	 * The scsi3addr's of devices won't change so long as the
3478edd16368SStephen M. Cameron 	 * adapter is not reset.  That means we can rescan and
3479edd16368SStephen M. Cameron 	 * tell which devices we already know about, vs. new
3480edd16368SStephen M. Cameron 	 * devices, vs.  disappearing devices.
3481edd16368SStephen M. Cameron 	 */
3482a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list = NULL;
3483edd16368SStephen M. Cameron 	struct ReportLUNdata *logdev_list = NULL;
348403383736SDon Brace 	struct bmic_identify_physical_device *id_phys = NULL;
348501a02ffcSStephen M. Cameron 	u32 nphysicals = 0;
348601a02ffcSStephen M. Cameron 	u32 nlogicals = 0;
348701a02ffcSStephen M. Cameron 	u32 ndev_allocated = 0;
3488edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
3489edd16368SStephen M. Cameron 	int ncurrent = 0;
34904f4eb9f1SScott Teel 	int i, n_ext_target_devs, ndevs_to_allocate;
3491339b2b14SStephen M. Cameron 	int raid_ctlr_position;
34922bbf5c7fSJoe Handzik 	int rescan_hba_mode;
3493aca4a520SScott Teel 	DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
3494edd16368SStephen M. Cameron 
3495cfe5badcSScott Teel 	currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
349692084715SStephen M. Cameron 	physdev_list = kzalloc(sizeof(*physdev_list), GFP_KERNEL);
349792084715SStephen M. Cameron 	logdev_list = kzalloc(sizeof(*logdev_list), GFP_KERNEL);
3498edd16368SStephen M. Cameron 	tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
349903383736SDon Brace 	id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
3500edd16368SStephen M. Cameron 
350103383736SDon Brace 	if (!currentsd || !physdev_list || !logdev_list ||
350203383736SDon Brace 		!tmpdevice || !id_phys) {
3503edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory\n");
3504edd16368SStephen M. Cameron 		goto out;
3505edd16368SStephen M. Cameron 	}
3506edd16368SStephen M. Cameron 	memset(lunzerobits, 0, sizeof(lunzerobits));
3507edd16368SStephen M. Cameron 
3508316b221aSStephen M. Cameron 	rescan_hba_mode = hpsa_hba_mode_enabled(h);
350996444fbbSJoe Handzik 	if (rescan_hba_mode < 0)
351096444fbbSJoe Handzik 		goto out;
3511316b221aSStephen M. Cameron 
3512316b221aSStephen M. Cameron 	if (!h->hba_mode_enabled && rescan_hba_mode)
3513316b221aSStephen M. Cameron 		dev_warn(&h->pdev->dev, "HBA mode enabled\n");
3514316b221aSStephen M. Cameron 	else if (h->hba_mode_enabled && !rescan_hba_mode)
3515316b221aSStephen M. Cameron 		dev_warn(&h->pdev->dev, "HBA mode disabled\n");
3516316b221aSStephen M. Cameron 
3517316b221aSStephen M. Cameron 	h->hba_mode_enabled = rescan_hba_mode;
3518316b221aSStephen M. Cameron 
351903383736SDon Brace 	if (hpsa_gather_lun_info(h, physdev_list, &nphysicals,
352003383736SDon Brace 			logdev_list, &nlogicals))
3521edd16368SStephen M. Cameron 		goto out;
3522edd16368SStephen M. Cameron 
3523aca4a520SScott Teel 	/* We might see up to the maximum number of logical and physical disks
3524aca4a520SScott Teel 	 * plus external target devices, and a device for the local RAID
3525aca4a520SScott Teel 	 * controller.
3526edd16368SStephen M. Cameron 	 */
3527aca4a520SScott Teel 	ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
3528edd16368SStephen M. Cameron 
3529edd16368SStephen M. Cameron 	/* Allocate the per device structures */
3530edd16368SStephen M. Cameron 	for (i = 0; i < ndevs_to_allocate; i++) {
3531b7ec021fSScott Teel 		if (i >= HPSA_MAX_DEVICES) {
3532b7ec021fSScott Teel 			dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
3533b7ec021fSScott Teel 				"  %d devices ignored.\n", HPSA_MAX_DEVICES,
3534b7ec021fSScott Teel 				ndevs_to_allocate - HPSA_MAX_DEVICES);
3535b7ec021fSScott Teel 			break;
3536b7ec021fSScott Teel 		}
3537b7ec021fSScott Teel 
3538edd16368SStephen M. Cameron 		currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
3539edd16368SStephen M. Cameron 		if (!currentsd[i]) {
3540edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
3541edd16368SStephen M. Cameron 				__FILE__, __LINE__);
3542edd16368SStephen M. Cameron 			goto out;
3543edd16368SStephen M. Cameron 		}
3544edd16368SStephen M. Cameron 		ndev_allocated++;
3545edd16368SStephen M. Cameron 	}
3546edd16368SStephen M. Cameron 
35478645291bSStephen M. Cameron 	if (is_scsi_rev_5(h))
3548339b2b14SStephen M. Cameron 		raid_ctlr_position = 0;
3549339b2b14SStephen M. Cameron 	else
3550339b2b14SStephen M. Cameron 		raid_ctlr_position = nphysicals + nlogicals;
3551339b2b14SStephen M. Cameron 
3552edd16368SStephen M. Cameron 	/* adjust our table of devices */
35534f4eb9f1SScott Teel 	n_ext_target_devs = 0;
3554edd16368SStephen M. Cameron 	for (i = 0; i < nphysicals + nlogicals + 1; i++) {
35550b0e1d6cSStephen M. Cameron 		u8 *lunaddrbytes, is_OBDR = 0;
3556edd16368SStephen M. Cameron 
3557edd16368SStephen M. Cameron 		/* Figure out where the LUN ID info is coming from */
3558339b2b14SStephen M. Cameron 		lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
3559339b2b14SStephen M. Cameron 			i, nphysicals, nlogicals, physdev_list, logdev_list);
356041ce4c35SStephen Cameron 
356141ce4c35SStephen Cameron 		/* skip masked non-disk devices */
356241ce4c35SStephen Cameron 		if (MASKED_DEVICE(lunaddrbytes))
356341ce4c35SStephen Cameron 			if (i < nphysicals + (raid_ctlr_position == 0) &&
356441ce4c35SStephen Cameron 				NON_DISK_PHYS_DEV(lunaddrbytes))
3565edd16368SStephen M. Cameron 				continue;
3566edd16368SStephen M. Cameron 
3567edd16368SStephen M. Cameron 		/* Get device type, vendor, model, device id */
35680b0e1d6cSStephen M. Cameron 		if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
35690b0e1d6cSStephen M. Cameron 							&is_OBDR))
3570edd16368SStephen M. Cameron 			continue; /* skip it if we can't talk to it. */
35711f310bdeSStephen M. Cameron 		figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
35729b5c48c2SStephen Cameron 		hpsa_update_device_supports_aborts(h, tmpdevice, lunaddrbytes);
3573edd16368SStephen M. Cameron 		this_device = currentsd[ncurrent];
3574edd16368SStephen M. Cameron 
3575edd16368SStephen M. Cameron 		/*
35764f4eb9f1SScott Teel 		 * For external target devices, we have to insert a LUN 0 which
3577edd16368SStephen M. Cameron 		 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
3578edd16368SStephen M. Cameron 		 * is nonetheless an enclosure device there.  We have to
3579edd16368SStephen M. Cameron 		 * present that otherwise linux won't find anything if
3580edd16368SStephen M. Cameron 		 * there is no lun 0.
3581edd16368SStephen M. Cameron 		 */
35824f4eb9f1SScott Teel 		if (add_ext_target_dev(h, tmpdevice, this_device,
35831f310bdeSStephen M. Cameron 				lunaddrbytes, lunzerobits,
35844f4eb9f1SScott Teel 				&n_ext_target_devs)) {
3585edd16368SStephen M. Cameron 			ncurrent++;
3586edd16368SStephen M. Cameron 			this_device = currentsd[ncurrent];
3587edd16368SStephen M. Cameron 		}
3588edd16368SStephen M. Cameron 
3589edd16368SStephen M. Cameron 		*this_device = *tmpdevice;
3590edd16368SStephen M. Cameron 
359141ce4c35SStephen Cameron 		/* do not expose masked devices */
359241ce4c35SStephen Cameron 		if (MASKED_DEVICE(lunaddrbytes) &&
359341ce4c35SStephen Cameron 			i < nphysicals + (raid_ctlr_position == 0)) {
359441ce4c35SStephen Cameron 			if (h->hba_mode_enabled)
359541ce4c35SStephen Cameron 				dev_warn(&h->pdev->dev,
359641ce4c35SStephen Cameron 					"Masked physical device detected\n");
359741ce4c35SStephen Cameron 			this_device->expose_state = HPSA_DO_NOT_EXPOSE;
359841ce4c35SStephen Cameron 		} else {
359941ce4c35SStephen Cameron 			this_device->expose_state =
360041ce4c35SStephen Cameron 					HPSA_SG_ATTACH | HPSA_ULD_ATTACH;
360141ce4c35SStephen Cameron 		}
360241ce4c35SStephen Cameron 
3603edd16368SStephen M. Cameron 		switch (this_device->devtype) {
36040b0e1d6cSStephen M. Cameron 		case TYPE_ROM:
3605edd16368SStephen M. Cameron 			/* We don't *really* support actual CD-ROM devices,
3606edd16368SStephen M. Cameron 			 * just "One Button Disaster Recovery" tape drive
3607edd16368SStephen M. Cameron 			 * which temporarily pretends to be a CD-ROM drive.
3608edd16368SStephen M. Cameron 			 * So we check that the device is really an OBDR tape
3609edd16368SStephen M. Cameron 			 * device by checking for "$DR-10" in bytes 43-48 of
3610edd16368SStephen M. Cameron 			 * the inquiry data.
3611edd16368SStephen M. Cameron 			 */
36120b0e1d6cSStephen M. Cameron 			if (is_OBDR)
3613edd16368SStephen M. Cameron 				ncurrent++;
3614edd16368SStephen M. Cameron 			break;
3615edd16368SStephen M. Cameron 		case TYPE_DISK:
3616283b4a9bSStephen M. Cameron 			if (i >= nphysicals) {
3617283b4a9bSStephen M. Cameron 				ncurrent++;
3618edd16368SStephen M. Cameron 				break;
3619283b4a9bSStephen M. Cameron 			}
3620ecf418d1SJoe Handzik 
3621ecf418d1SJoe Handzik 			if (h->hba_mode_enabled)
3622ecf418d1SJoe Handzik 				/* never use raid mapper in HBA mode */
3623ecf418d1SJoe Handzik 				this_device->offload_enabled = 0;
3624ecf418d1SJoe Handzik 			else if (!(h->transMethod & CFGTBL_Trans_io_accel1 ||
3625ecf418d1SJoe Handzik 				h->transMethod & CFGTBL_Trans_io_accel2))
3626316b221aSStephen M. Cameron 				break;
3627ecf418d1SJoe Handzik 
362803383736SDon Brace 			hpsa_get_ioaccel_drive_info(h, this_device,
362903383736SDon Brace 						lunaddrbytes, id_phys);
363003383736SDon Brace 			atomic_set(&this_device->ioaccel_cmds_out, 0);
3631edd16368SStephen M. Cameron 			ncurrent++;
3632edd16368SStephen M. Cameron 			break;
3633edd16368SStephen M. Cameron 		case TYPE_TAPE:
3634edd16368SStephen M. Cameron 		case TYPE_MEDIUM_CHANGER:
3635edd16368SStephen M. Cameron 			ncurrent++;
3636edd16368SStephen M. Cameron 			break;
363741ce4c35SStephen Cameron 		case TYPE_ENCLOSURE:
363841ce4c35SStephen Cameron 			if (h->hba_mode_enabled)
363941ce4c35SStephen Cameron 				ncurrent++;
364041ce4c35SStephen Cameron 			break;
3641edd16368SStephen M. Cameron 		case TYPE_RAID:
3642edd16368SStephen M. Cameron 			/* Only present the Smartarray HBA as a RAID controller.
3643edd16368SStephen M. Cameron 			 * If it's a RAID controller other than the HBA itself
3644edd16368SStephen M. Cameron 			 * (an external RAID controller, MSA500 or similar)
3645edd16368SStephen M. Cameron 			 * don't present it.
3646edd16368SStephen M. Cameron 			 */
3647edd16368SStephen M. Cameron 			if (!is_hba_lunid(lunaddrbytes))
3648edd16368SStephen M. Cameron 				break;
3649edd16368SStephen M. Cameron 			ncurrent++;
3650edd16368SStephen M. Cameron 			break;
3651edd16368SStephen M. Cameron 		default:
3652edd16368SStephen M. Cameron 			break;
3653edd16368SStephen M. Cameron 		}
3654cfe5badcSScott Teel 		if (ncurrent >= HPSA_MAX_DEVICES)
3655edd16368SStephen M. Cameron 			break;
3656edd16368SStephen M. Cameron 	}
3657edd16368SStephen M. Cameron 	adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
3658edd16368SStephen M. Cameron out:
3659edd16368SStephen M. Cameron 	kfree(tmpdevice);
3660edd16368SStephen M. Cameron 	for (i = 0; i < ndev_allocated; i++)
3661edd16368SStephen M. Cameron 		kfree(currentsd[i]);
3662edd16368SStephen M. Cameron 	kfree(currentsd);
3663edd16368SStephen M. Cameron 	kfree(physdev_list);
3664edd16368SStephen M. Cameron 	kfree(logdev_list);
366503383736SDon Brace 	kfree(id_phys);
3666edd16368SStephen M. Cameron }
3667edd16368SStephen M. Cameron 
3668ec5cbf04SWebb Scales static void hpsa_set_sg_descriptor(struct SGDescriptor *desc,
3669ec5cbf04SWebb Scales 				   struct scatterlist *sg)
3670ec5cbf04SWebb Scales {
3671ec5cbf04SWebb Scales 	u64 addr64 = (u64) sg_dma_address(sg);
3672ec5cbf04SWebb Scales 	unsigned int len = sg_dma_len(sg);
3673ec5cbf04SWebb Scales 
3674ec5cbf04SWebb Scales 	desc->Addr = cpu_to_le64(addr64);
3675ec5cbf04SWebb Scales 	desc->Len = cpu_to_le32(len);
3676ec5cbf04SWebb Scales 	desc->Ext = 0;
3677ec5cbf04SWebb Scales }
3678ec5cbf04SWebb Scales 
3679c7ee65b3SWebb Scales /*
3680c7ee65b3SWebb Scales  * hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
3681edd16368SStephen M. Cameron  * dma mapping  and fills in the scatter gather entries of the
3682edd16368SStephen M. Cameron  * hpsa command, cp.
3683edd16368SStephen M. Cameron  */
368433a2ffceSStephen M. Cameron static int hpsa_scatter_gather(struct ctlr_info *h,
3685edd16368SStephen M. Cameron 		struct CommandList *cp,
3686edd16368SStephen M. Cameron 		struct scsi_cmnd *cmd)
3687edd16368SStephen M. Cameron {
3688edd16368SStephen M. Cameron 	struct scatterlist *sg;
3689b3a7ba7cSWebb Scales 	int use_sg, i, sg_limit, chained, last_sg;
369033a2ffceSStephen M. Cameron 	struct SGDescriptor *curr_sg;
3691edd16368SStephen M. Cameron 
369233a2ffceSStephen M. Cameron 	BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
3693edd16368SStephen M. Cameron 
3694edd16368SStephen M. Cameron 	use_sg = scsi_dma_map(cmd);
3695edd16368SStephen M. Cameron 	if (use_sg < 0)
3696edd16368SStephen M. Cameron 		return use_sg;
3697edd16368SStephen M. Cameron 
3698edd16368SStephen M. Cameron 	if (!use_sg)
3699edd16368SStephen M. Cameron 		goto sglist_finished;
3700edd16368SStephen M. Cameron 
3701b3a7ba7cSWebb Scales 	/*
3702b3a7ba7cSWebb Scales 	 * If the number of entries is greater than the max for a single list,
3703b3a7ba7cSWebb Scales 	 * then we have a chained list; we will set up all but one entry in the
3704b3a7ba7cSWebb Scales 	 * first list (the last entry is saved for link information);
3705b3a7ba7cSWebb Scales 	 * otherwise, we don't have a chained list and we'll set up at each of
3706b3a7ba7cSWebb Scales 	 * the entries in the one list.
3707b3a7ba7cSWebb Scales 	 */
370833a2ffceSStephen M. Cameron 	curr_sg = cp->SG;
3709b3a7ba7cSWebb Scales 	chained = use_sg > h->max_cmd_sg_entries;
3710b3a7ba7cSWebb Scales 	sg_limit = chained ? h->max_cmd_sg_entries - 1 : use_sg;
3711b3a7ba7cSWebb Scales 	last_sg = scsi_sg_count(cmd) - 1;
3712b3a7ba7cSWebb Scales 	scsi_for_each_sg(cmd, sg, sg_limit, i) {
3713ec5cbf04SWebb Scales 		hpsa_set_sg_descriptor(curr_sg, sg);
371433a2ffceSStephen M. Cameron 		curr_sg++;
371533a2ffceSStephen M. Cameron 	}
3716ec5cbf04SWebb Scales 
3717b3a7ba7cSWebb Scales 	if (chained) {
3718b3a7ba7cSWebb Scales 		/*
3719b3a7ba7cSWebb Scales 		 * Continue with the chained list.  Set curr_sg to the chained
3720b3a7ba7cSWebb Scales 		 * list.  Modify the limit to the total count less the entries
3721b3a7ba7cSWebb Scales 		 * we've already set up.  Resume the scan at the list entry
3722b3a7ba7cSWebb Scales 		 * where the previous loop left off.
3723b3a7ba7cSWebb Scales 		 */
3724b3a7ba7cSWebb Scales 		curr_sg = h->cmd_sg_list[cp->cmdindex];
3725b3a7ba7cSWebb Scales 		sg_limit = use_sg - sg_limit;
3726b3a7ba7cSWebb Scales 		for_each_sg(sg, sg, sg_limit, i) {
3727b3a7ba7cSWebb Scales 			hpsa_set_sg_descriptor(curr_sg, sg);
3728b3a7ba7cSWebb Scales 			curr_sg++;
3729b3a7ba7cSWebb Scales 		}
3730b3a7ba7cSWebb Scales 	}
3731b3a7ba7cSWebb Scales 
3732ec5cbf04SWebb Scales 	/* Back the pointer up to the last entry and mark it as "last". */
3733b3a7ba7cSWebb Scales 	(curr_sg - 1)->Ext = cpu_to_le32(HPSA_SG_LAST);
373433a2ffceSStephen M. Cameron 
373533a2ffceSStephen M. Cameron 	if (use_sg + chained > h->maxSG)
373633a2ffceSStephen M. Cameron 		h->maxSG = use_sg + chained;
373733a2ffceSStephen M. Cameron 
373833a2ffceSStephen M. Cameron 	if (chained) {
373933a2ffceSStephen M. Cameron 		cp->Header.SGList = h->max_cmd_sg_entries;
374050a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(use_sg + 1);
3741e2bea6dfSStephen M. Cameron 		if (hpsa_map_sg_chain_block(h, cp)) {
3742e2bea6dfSStephen M. Cameron 			scsi_dma_unmap(cmd);
3743e2bea6dfSStephen M. Cameron 			return -1;
3744e2bea6dfSStephen M. Cameron 		}
374533a2ffceSStephen M. Cameron 		return 0;
3746edd16368SStephen M. Cameron 	}
3747edd16368SStephen M. Cameron 
3748edd16368SStephen M. Cameron sglist_finished:
3749edd16368SStephen M. Cameron 
375001a02ffcSStephen M. Cameron 	cp->Header.SGList = (u8) use_sg;   /* no. SGs contig in this cmd */
3751c7ee65b3SWebb Scales 	cp->Header.SGTotal = cpu_to_le16(use_sg); /* total sgs in cmd list */
3752edd16368SStephen M. Cameron 	return 0;
3753edd16368SStephen M. Cameron }
3754edd16368SStephen M. Cameron 
3755283b4a9bSStephen M. Cameron #define IO_ACCEL_INELIGIBLE (1)
3756283b4a9bSStephen M. Cameron static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
3757283b4a9bSStephen M. Cameron {
3758283b4a9bSStephen M. Cameron 	int is_write = 0;
3759283b4a9bSStephen M. Cameron 	u32 block;
3760283b4a9bSStephen M. Cameron 	u32 block_cnt;
3761283b4a9bSStephen M. Cameron 
3762283b4a9bSStephen M. Cameron 	/* Perform some CDB fixups if needed using 10 byte reads/writes only */
3763283b4a9bSStephen M. Cameron 	switch (cdb[0]) {
3764283b4a9bSStephen M. Cameron 	case WRITE_6:
3765283b4a9bSStephen M. Cameron 	case WRITE_12:
3766283b4a9bSStephen M. Cameron 		is_write = 1;
3767283b4a9bSStephen M. Cameron 	case READ_6:
3768283b4a9bSStephen M. Cameron 	case READ_12:
3769283b4a9bSStephen M. Cameron 		if (*cdb_len == 6) {
3770283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 8) | cdb[3];
3771283b4a9bSStephen M. Cameron 			block_cnt = cdb[4];
3772283b4a9bSStephen M. Cameron 		} else {
3773283b4a9bSStephen M. Cameron 			BUG_ON(*cdb_len != 12);
3774283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 24) |
3775283b4a9bSStephen M. Cameron 				(((u32) cdb[3]) << 16) |
3776283b4a9bSStephen M. Cameron 				(((u32) cdb[4]) << 8) |
3777283b4a9bSStephen M. Cameron 				cdb[5];
3778283b4a9bSStephen M. Cameron 			block_cnt =
3779283b4a9bSStephen M. Cameron 				(((u32) cdb[6]) << 24) |
3780283b4a9bSStephen M. Cameron 				(((u32) cdb[7]) << 16) |
3781283b4a9bSStephen M. Cameron 				(((u32) cdb[8]) << 8) |
3782283b4a9bSStephen M. Cameron 				cdb[9];
3783283b4a9bSStephen M. Cameron 		}
3784283b4a9bSStephen M. Cameron 		if (block_cnt > 0xffff)
3785283b4a9bSStephen M. Cameron 			return IO_ACCEL_INELIGIBLE;
3786283b4a9bSStephen M. Cameron 
3787283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
3788283b4a9bSStephen M. Cameron 		cdb[1] = 0;
3789283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (block >> 24);
3790283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (block >> 16);
3791283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (block >> 8);
3792283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (block);
3793283b4a9bSStephen M. Cameron 		cdb[6] = 0;
3794283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (block_cnt >> 8);
3795283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (block_cnt);
3796283b4a9bSStephen M. Cameron 		cdb[9] = 0;
3797283b4a9bSStephen M. Cameron 		*cdb_len = 10;
3798283b4a9bSStephen M. Cameron 		break;
3799283b4a9bSStephen M. Cameron 	}
3800283b4a9bSStephen M. Cameron 	return 0;
3801283b4a9bSStephen M. Cameron }
3802283b4a9bSStephen M. Cameron 
3803c349775eSScott Teel static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
3804283b4a9bSStephen M. Cameron 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
380503383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
3806e1f7de0cSMatt Gates {
3807e1f7de0cSMatt Gates 	struct scsi_cmnd *cmd = c->scsi_cmd;
3808e1f7de0cSMatt Gates 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
3809e1f7de0cSMatt Gates 	unsigned int len;
3810e1f7de0cSMatt Gates 	unsigned int total_len = 0;
3811e1f7de0cSMatt Gates 	struct scatterlist *sg;
3812e1f7de0cSMatt Gates 	u64 addr64;
3813e1f7de0cSMatt Gates 	int use_sg, i;
3814e1f7de0cSMatt Gates 	struct SGDescriptor *curr_sg;
3815e1f7de0cSMatt Gates 	u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
3816e1f7de0cSMatt Gates 
3817283b4a9bSStephen M. Cameron 	/* TODO: implement chaining support */
381803383736SDon Brace 	if (scsi_sg_count(cmd) > h->ioaccel_maxsg) {
381903383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3820283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
382103383736SDon Brace 	}
3822283b4a9bSStephen M. Cameron 
3823e1f7de0cSMatt Gates 	BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
3824e1f7de0cSMatt Gates 
382503383736SDon Brace 	if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
382603383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3827283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
382803383736SDon Brace 	}
3829283b4a9bSStephen M. Cameron 
3830e1f7de0cSMatt Gates 	c->cmd_type = CMD_IOACCEL1;
3831e1f7de0cSMatt Gates 
3832e1f7de0cSMatt Gates 	/* Adjust the DMA address to point to the accelerated command buffer */
3833e1f7de0cSMatt Gates 	c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
3834e1f7de0cSMatt Gates 				(c->cmdindex * sizeof(*cp));
3835e1f7de0cSMatt Gates 	BUG_ON(c->busaddr & 0x0000007F);
3836e1f7de0cSMatt Gates 
3837e1f7de0cSMatt Gates 	use_sg = scsi_dma_map(cmd);
383803383736SDon Brace 	if (use_sg < 0) {
383903383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3840e1f7de0cSMatt Gates 		return use_sg;
384103383736SDon Brace 	}
3842e1f7de0cSMatt Gates 
3843e1f7de0cSMatt Gates 	if (use_sg) {
3844e1f7de0cSMatt Gates 		curr_sg = cp->SG;
3845e1f7de0cSMatt Gates 		scsi_for_each_sg(cmd, sg, use_sg, i) {
3846e1f7de0cSMatt Gates 			addr64 = (u64) sg_dma_address(sg);
3847e1f7de0cSMatt Gates 			len  = sg_dma_len(sg);
3848e1f7de0cSMatt Gates 			total_len += len;
384950a0decfSStephen M. Cameron 			curr_sg->Addr = cpu_to_le64(addr64);
385050a0decfSStephen M. Cameron 			curr_sg->Len = cpu_to_le32(len);
385150a0decfSStephen M. Cameron 			curr_sg->Ext = cpu_to_le32(0);
3852e1f7de0cSMatt Gates 			curr_sg++;
3853e1f7de0cSMatt Gates 		}
385450a0decfSStephen M. Cameron 		(--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
3855e1f7de0cSMatt Gates 
3856e1f7de0cSMatt Gates 		switch (cmd->sc_data_direction) {
3857e1f7de0cSMatt Gates 		case DMA_TO_DEVICE:
3858e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_OUT;
3859e1f7de0cSMatt Gates 			break;
3860e1f7de0cSMatt Gates 		case DMA_FROM_DEVICE:
3861e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_IN;
3862e1f7de0cSMatt Gates 			break;
3863e1f7de0cSMatt Gates 		case DMA_NONE:
3864e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_NODATAXFER;
3865e1f7de0cSMatt Gates 			break;
3866e1f7de0cSMatt Gates 		default:
3867e1f7de0cSMatt Gates 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3868e1f7de0cSMatt Gates 			cmd->sc_data_direction);
3869e1f7de0cSMatt Gates 			BUG();
3870e1f7de0cSMatt Gates 			break;
3871e1f7de0cSMatt Gates 		}
3872e1f7de0cSMatt Gates 	} else {
3873e1f7de0cSMatt Gates 		control |= IOACCEL1_CONTROL_NODATAXFER;
3874e1f7de0cSMatt Gates 	}
3875e1f7de0cSMatt Gates 
3876c349775eSScott Teel 	c->Header.SGList = use_sg;
3877e1f7de0cSMatt Gates 	/* Fill out the command structure to submit */
38782b08b3e9SDon Brace 	cp->dev_handle = cpu_to_le16(ioaccel_handle & 0xFFFF);
38792b08b3e9SDon Brace 	cp->transfer_len = cpu_to_le32(total_len);
38802b08b3e9SDon Brace 	cp->io_flags = cpu_to_le16(IOACCEL1_IOFLAGS_IO_REQ |
38812b08b3e9SDon Brace 			(cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK));
38822b08b3e9SDon Brace 	cp->control = cpu_to_le32(control);
3883283b4a9bSStephen M. Cameron 	memcpy(cp->CDB, cdb, cdb_len);
3884283b4a9bSStephen M. Cameron 	memcpy(cp->CISS_LUN, scsi3addr, 8);
3885c349775eSScott Teel 	/* Tag was already set at init time. */
3886e1f7de0cSMatt Gates 	enqueue_cmd_and_start_io(h, c);
3887e1f7de0cSMatt Gates 	return 0;
3888e1f7de0cSMatt Gates }
3889edd16368SStephen M. Cameron 
3890283b4a9bSStephen M. Cameron /*
3891283b4a9bSStephen M. Cameron  * Queue a command directly to a device behind the controller using the
3892283b4a9bSStephen M. Cameron  * I/O accelerator path.
3893283b4a9bSStephen M. Cameron  */
3894283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
3895283b4a9bSStephen M. Cameron 	struct CommandList *c)
3896283b4a9bSStephen M. Cameron {
3897283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
3898283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3899283b4a9bSStephen M. Cameron 
390003383736SDon Brace 	c->phys_disk = dev;
390103383736SDon Brace 
3902283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
390303383736SDon Brace 		cmd->cmnd, cmd->cmd_len, dev->scsi3addr, dev);
3904283b4a9bSStephen M. Cameron }
3905283b4a9bSStephen M. Cameron 
3906dd0e19f3SScott Teel /*
3907dd0e19f3SScott Teel  * Set encryption parameters for the ioaccel2 request
3908dd0e19f3SScott Teel  */
3909dd0e19f3SScott Teel static void set_encrypt_ioaccel2(struct ctlr_info *h,
3910dd0e19f3SScott Teel 	struct CommandList *c, struct io_accel2_cmd *cp)
3911dd0e19f3SScott Teel {
3912dd0e19f3SScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
3913dd0e19f3SScott Teel 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3914dd0e19f3SScott Teel 	struct raid_map_data *map = &dev->raid_map;
3915dd0e19f3SScott Teel 	u64 first_block;
3916dd0e19f3SScott Teel 
3917dd0e19f3SScott Teel 	/* Are we doing encryption on this device */
39182b08b3e9SDon Brace 	if (!(le16_to_cpu(map->flags) & RAID_MAP_FLAG_ENCRYPT_ON))
3919dd0e19f3SScott Teel 		return;
3920dd0e19f3SScott Teel 	/* Set the data encryption key index. */
3921dd0e19f3SScott Teel 	cp->dekindex = map->dekindex;
3922dd0e19f3SScott Teel 
3923dd0e19f3SScott Teel 	/* Set the encryption enable flag, encoded into direction field. */
3924dd0e19f3SScott Teel 	cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
3925dd0e19f3SScott Teel 
3926dd0e19f3SScott Teel 	/* Set encryption tweak values based on logical block address
3927dd0e19f3SScott Teel 	 * If block size is 512, tweak value is LBA.
3928dd0e19f3SScott Teel 	 * For other block sizes, tweak is (LBA * block size)/ 512)
3929dd0e19f3SScott Teel 	 */
3930dd0e19f3SScott Teel 	switch (cmd->cmnd[0]) {
3931dd0e19f3SScott Teel 	/* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
3932dd0e19f3SScott Teel 	case WRITE_6:
3933dd0e19f3SScott Teel 	case READ_6:
39342b08b3e9SDon Brace 		first_block = get_unaligned_be16(&cmd->cmnd[2]);
3935dd0e19f3SScott Teel 		break;
3936dd0e19f3SScott Teel 	case WRITE_10:
3937dd0e19f3SScott Teel 	case READ_10:
3938dd0e19f3SScott Teel 	/* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
3939dd0e19f3SScott Teel 	case WRITE_12:
3940dd0e19f3SScott Teel 	case READ_12:
39412b08b3e9SDon Brace 		first_block = get_unaligned_be32(&cmd->cmnd[2]);
3942dd0e19f3SScott Teel 		break;
3943dd0e19f3SScott Teel 	case WRITE_16:
3944dd0e19f3SScott Teel 	case READ_16:
39452b08b3e9SDon Brace 		first_block = get_unaligned_be64(&cmd->cmnd[2]);
3946dd0e19f3SScott Teel 		break;
3947dd0e19f3SScott Teel 	default:
3948dd0e19f3SScott Teel 		dev_err(&h->pdev->dev,
39492b08b3e9SDon Brace 			"ERROR: %s: size (0x%x) not supported for encryption\n",
39502b08b3e9SDon Brace 			__func__, cmd->cmnd[0]);
3951dd0e19f3SScott Teel 		BUG();
3952dd0e19f3SScott Teel 		break;
3953dd0e19f3SScott Teel 	}
39542b08b3e9SDon Brace 
39552b08b3e9SDon Brace 	if (le32_to_cpu(map->volume_blk_size) != 512)
39562b08b3e9SDon Brace 		first_block = first_block *
39572b08b3e9SDon Brace 				le32_to_cpu(map->volume_blk_size)/512;
39582b08b3e9SDon Brace 
39592b08b3e9SDon Brace 	cp->tweak_lower = cpu_to_le32(first_block);
39602b08b3e9SDon Brace 	cp->tweak_upper = cpu_to_le32(first_block >> 32);
3961dd0e19f3SScott Teel }
3962dd0e19f3SScott Teel 
3963c349775eSScott Teel static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
3964c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
396503383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
3966c349775eSScott Teel {
3967c349775eSScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
3968c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
3969c349775eSScott Teel 	struct ioaccel2_sg_element *curr_sg;
3970c349775eSScott Teel 	int use_sg, i;
3971c349775eSScott Teel 	struct scatterlist *sg;
3972c349775eSScott Teel 	u64 addr64;
3973c349775eSScott Teel 	u32 len;
3974c349775eSScott Teel 	u32 total_len = 0;
3975c349775eSScott Teel 
3976d9a729f3SWebb Scales 	BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
3977c349775eSScott Teel 
397803383736SDon Brace 	if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
397903383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3980c349775eSScott Teel 		return IO_ACCEL_INELIGIBLE;
398103383736SDon Brace 	}
398203383736SDon Brace 
3983c349775eSScott Teel 	c->cmd_type = CMD_IOACCEL2;
3984c349775eSScott Teel 	/* Adjust the DMA address to point to the accelerated command buffer */
3985c349775eSScott Teel 	c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
3986c349775eSScott Teel 				(c->cmdindex * sizeof(*cp));
3987c349775eSScott Teel 	BUG_ON(c->busaddr & 0x0000007F);
3988c349775eSScott Teel 
3989c349775eSScott Teel 	memset(cp, 0, sizeof(*cp));
3990c349775eSScott Teel 	cp->IU_type = IOACCEL2_IU_TYPE;
3991c349775eSScott Teel 
3992c349775eSScott Teel 	use_sg = scsi_dma_map(cmd);
399303383736SDon Brace 	if (use_sg < 0) {
399403383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3995c349775eSScott Teel 		return use_sg;
399603383736SDon Brace 	}
3997c349775eSScott Teel 
3998c349775eSScott Teel 	if (use_sg) {
3999c349775eSScott Teel 		curr_sg = cp->sg;
4000d9a729f3SWebb Scales 		if (use_sg > h->ioaccel_maxsg) {
4001d9a729f3SWebb Scales 			addr64 = le64_to_cpu(
4002d9a729f3SWebb Scales 				h->ioaccel2_cmd_sg_list[c->cmdindex]->address);
4003d9a729f3SWebb Scales 			curr_sg->address = cpu_to_le64(addr64);
4004d9a729f3SWebb Scales 			curr_sg->length = 0;
4005d9a729f3SWebb Scales 			curr_sg->reserved[0] = 0;
4006d9a729f3SWebb Scales 			curr_sg->reserved[1] = 0;
4007d9a729f3SWebb Scales 			curr_sg->reserved[2] = 0;
4008d9a729f3SWebb Scales 			curr_sg->chain_indicator = 0x80;
4009d9a729f3SWebb Scales 
4010d9a729f3SWebb Scales 			curr_sg = h->ioaccel2_cmd_sg_list[c->cmdindex];
4011d9a729f3SWebb Scales 		}
4012c349775eSScott Teel 		scsi_for_each_sg(cmd, sg, use_sg, i) {
4013c349775eSScott Teel 			addr64 = (u64) sg_dma_address(sg);
4014c349775eSScott Teel 			len  = sg_dma_len(sg);
4015c349775eSScott Teel 			total_len += len;
4016c349775eSScott Teel 			curr_sg->address = cpu_to_le64(addr64);
4017c349775eSScott Teel 			curr_sg->length = cpu_to_le32(len);
4018c349775eSScott Teel 			curr_sg->reserved[0] = 0;
4019c349775eSScott Teel 			curr_sg->reserved[1] = 0;
4020c349775eSScott Teel 			curr_sg->reserved[2] = 0;
4021c349775eSScott Teel 			curr_sg->chain_indicator = 0;
4022c349775eSScott Teel 			curr_sg++;
4023c349775eSScott Teel 		}
4024c349775eSScott Teel 
4025c349775eSScott Teel 		switch (cmd->sc_data_direction) {
4026c349775eSScott Teel 		case DMA_TO_DEVICE:
4027dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4028dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_OUT;
4029c349775eSScott Teel 			break;
4030c349775eSScott Teel 		case DMA_FROM_DEVICE:
4031dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4032dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_IN;
4033c349775eSScott Teel 			break;
4034c349775eSScott Teel 		case DMA_NONE:
4035dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4036dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_NO_DATA;
4037c349775eSScott Teel 			break;
4038c349775eSScott Teel 		default:
4039c349775eSScott Teel 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4040c349775eSScott Teel 				cmd->sc_data_direction);
4041c349775eSScott Teel 			BUG();
4042c349775eSScott Teel 			break;
4043c349775eSScott Teel 		}
4044c349775eSScott Teel 	} else {
4045dd0e19f3SScott Teel 		cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4046dd0e19f3SScott Teel 		cp->direction |= IOACCEL2_DIR_NO_DATA;
4047c349775eSScott Teel 	}
4048dd0e19f3SScott Teel 
4049dd0e19f3SScott Teel 	/* Set encryption parameters, if necessary */
4050dd0e19f3SScott Teel 	set_encrypt_ioaccel2(h, c, cp);
4051dd0e19f3SScott Teel 
40522b08b3e9SDon Brace 	cp->scsi_nexus = cpu_to_le32(ioaccel_handle);
4053f2405db8SDon Brace 	cp->Tag = cpu_to_le32(c->cmdindex << DIRECT_LOOKUP_SHIFT);
4054c349775eSScott Teel 	memcpy(cp->cdb, cdb, sizeof(cp->cdb));
4055c349775eSScott Teel 
4056c349775eSScott Teel 	cp->data_len = cpu_to_le32(total_len);
4057c349775eSScott Teel 	cp->err_ptr = cpu_to_le64(c->busaddr +
4058c349775eSScott Teel 			offsetof(struct io_accel2_cmd, error_data));
405950a0decfSStephen M. Cameron 	cp->err_len = cpu_to_le32(sizeof(cp->error_data));
4060c349775eSScott Teel 
4061d9a729f3SWebb Scales 	/* fill in sg elements */
4062d9a729f3SWebb Scales 	if (use_sg > h->ioaccel_maxsg) {
4063d9a729f3SWebb Scales 		cp->sg_count = 1;
4064d9a729f3SWebb Scales 		if (hpsa_map_ioaccel2_sg_chain_block(h, cp, c)) {
4065d9a729f3SWebb Scales 			atomic_dec(&phys_disk->ioaccel_cmds_out);
4066d9a729f3SWebb Scales 			scsi_dma_unmap(cmd);
4067d9a729f3SWebb Scales 			return -1;
4068d9a729f3SWebb Scales 		}
4069d9a729f3SWebb Scales 	} else
4070d9a729f3SWebb Scales 		cp->sg_count = (u8) use_sg;
4071d9a729f3SWebb Scales 
4072c349775eSScott Teel 	enqueue_cmd_and_start_io(h, c);
4073c349775eSScott Teel 	return 0;
4074c349775eSScott Teel }
4075c349775eSScott Teel 
4076c349775eSScott Teel /*
4077c349775eSScott Teel  * Queue a command to the correct I/O accelerator path.
4078c349775eSScott Teel  */
4079c349775eSScott Teel static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
4080c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
408103383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
4082c349775eSScott Teel {
408303383736SDon Brace 	/* Try to honor the device's queue depth */
408403383736SDon Brace 	if (atomic_inc_return(&phys_disk->ioaccel_cmds_out) >
408503383736SDon Brace 					phys_disk->queue_depth) {
408603383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
408703383736SDon Brace 		return IO_ACCEL_INELIGIBLE;
408803383736SDon Brace 	}
4089c349775eSScott Teel 	if (h->transMethod & CFGTBL_Trans_io_accel1)
4090c349775eSScott Teel 		return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
409103383736SDon Brace 						cdb, cdb_len, scsi3addr,
409203383736SDon Brace 						phys_disk);
4093c349775eSScott Teel 	else
4094c349775eSScott Teel 		return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
409503383736SDon Brace 						cdb, cdb_len, scsi3addr,
409603383736SDon Brace 						phys_disk);
4097c349775eSScott Teel }
4098c349775eSScott Teel 
40996b80b18fSScott Teel static void raid_map_helper(struct raid_map_data *map,
41006b80b18fSScott Teel 		int offload_to_mirror, u32 *map_index, u32 *current_group)
41016b80b18fSScott Teel {
41026b80b18fSScott Teel 	if (offload_to_mirror == 0)  {
41036b80b18fSScott Teel 		/* use physical disk in the first mirrored group. */
41042b08b3e9SDon Brace 		*map_index %= le16_to_cpu(map->data_disks_per_row);
41056b80b18fSScott Teel 		return;
41066b80b18fSScott Teel 	}
41076b80b18fSScott Teel 	do {
41086b80b18fSScott Teel 		/* determine mirror group that *map_index indicates */
41092b08b3e9SDon Brace 		*current_group = *map_index /
41102b08b3e9SDon Brace 			le16_to_cpu(map->data_disks_per_row);
41116b80b18fSScott Teel 		if (offload_to_mirror == *current_group)
41126b80b18fSScott Teel 			continue;
41132b08b3e9SDon Brace 		if (*current_group < le16_to_cpu(map->layout_map_count) - 1) {
41146b80b18fSScott Teel 			/* select map index from next group */
41152b08b3e9SDon Brace 			*map_index += le16_to_cpu(map->data_disks_per_row);
41166b80b18fSScott Teel 			(*current_group)++;
41176b80b18fSScott Teel 		} else {
41186b80b18fSScott Teel 			/* select map index from first group */
41192b08b3e9SDon Brace 			*map_index %= le16_to_cpu(map->data_disks_per_row);
41206b80b18fSScott Teel 			*current_group = 0;
41216b80b18fSScott Teel 		}
41226b80b18fSScott Teel 	} while (offload_to_mirror != *current_group);
41236b80b18fSScott Teel }
41246b80b18fSScott Teel 
4125283b4a9bSStephen M. Cameron /*
4126283b4a9bSStephen M. Cameron  * Attempt to perform offload RAID mapping for a logical volume I/O.
4127283b4a9bSStephen M. Cameron  */
4128283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
4129283b4a9bSStephen M. Cameron 	struct CommandList *c)
4130283b4a9bSStephen M. Cameron {
4131283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
4132283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4133283b4a9bSStephen M. Cameron 	struct raid_map_data *map = &dev->raid_map;
4134283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map->data[0];
4135283b4a9bSStephen M. Cameron 	int is_write = 0;
4136283b4a9bSStephen M. Cameron 	u32 map_index;
4137283b4a9bSStephen M. Cameron 	u64 first_block, last_block;
4138283b4a9bSStephen M. Cameron 	u32 block_cnt;
4139283b4a9bSStephen M. Cameron 	u32 blocks_per_row;
4140283b4a9bSStephen M. Cameron 	u64 first_row, last_row;
4141283b4a9bSStephen M. Cameron 	u32 first_row_offset, last_row_offset;
4142283b4a9bSStephen M. Cameron 	u32 first_column, last_column;
41436b80b18fSScott Teel 	u64 r0_first_row, r0_last_row;
41446b80b18fSScott Teel 	u32 r5or6_blocks_per_row;
41456b80b18fSScott Teel 	u64 r5or6_first_row, r5or6_last_row;
41466b80b18fSScott Teel 	u32 r5or6_first_row_offset, r5or6_last_row_offset;
41476b80b18fSScott Teel 	u32 r5or6_first_column, r5or6_last_column;
41486b80b18fSScott Teel 	u32 total_disks_per_row;
41496b80b18fSScott Teel 	u32 stripesize;
41506b80b18fSScott Teel 	u32 first_group, last_group, current_group;
4151283b4a9bSStephen M. Cameron 	u32 map_row;
4152283b4a9bSStephen M. Cameron 	u32 disk_handle;
4153283b4a9bSStephen M. Cameron 	u64 disk_block;
4154283b4a9bSStephen M. Cameron 	u32 disk_block_cnt;
4155283b4a9bSStephen M. Cameron 	u8 cdb[16];
4156283b4a9bSStephen M. Cameron 	u8 cdb_len;
41572b08b3e9SDon Brace 	u16 strip_size;
4158283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
4159283b4a9bSStephen M. Cameron 	u64 tmpdiv;
4160283b4a9bSStephen M. Cameron #endif
41616b80b18fSScott Teel 	int offload_to_mirror;
4162283b4a9bSStephen M. Cameron 
4163283b4a9bSStephen M. Cameron 	/* check for valid opcode, get LBA and block count */
4164283b4a9bSStephen M. Cameron 	switch (cmd->cmnd[0]) {
4165283b4a9bSStephen M. Cameron 	case WRITE_6:
4166283b4a9bSStephen M. Cameron 		is_write = 1;
4167283b4a9bSStephen M. Cameron 	case READ_6:
4168283b4a9bSStephen M. Cameron 		first_block =
4169283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 8) |
4170283b4a9bSStephen M. Cameron 			cmd->cmnd[3];
4171283b4a9bSStephen M. Cameron 		block_cnt = cmd->cmnd[4];
41723fa89a04SStephen M. Cameron 		if (block_cnt == 0)
41733fa89a04SStephen M. Cameron 			block_cnt = 256;
4174283b4a9bSStephen M. Cameron 		break;
4175283b4a9bSStephen M. Cameron 	case WRITE_10:
4176283b4a9bSStephen M. Cameron 		is_write = 1;
4177283b4a9bSStephen M. Cameron 	case READ_10:
4178283b4a9bSStephen M. Cameron 		first_block =
4179283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
4180283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
4181283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
4182283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
4183283b4a9bSStephen M. Cameron 		block_cnt =
4184283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 8) |
4185283b4a9bSStephen M. Cameron 			cmd->cmnd[8];
4186283b4a9bSStephen M. Cameron 		break;
4187283b4a9bSStephen M. Cameron 	case WRITE_12:
4188283b4a9bSStephen M. Cameron 		is_write = 1;
4189283b4a9bSStephen M. Cameron 	case READ_12:
4190283b4a9bSStephen M. Cameron 		first_block =
4191283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
4192283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
4193283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
4194283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
4195283b4a9bSStephen M. Cameron 		block_cnt =
4196283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[6]) << 24) |
4197283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 16) |
4198283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[8]) << 8) |
4199283b4a9bSStephen M. Cameron 		cmd->cmnd[9];
4200283b4a9bSStephen M. Cameron 		break;
4201283b4a9bSStephen M. Cameron 	case WRITE_16:
4202283b4a9bSStephen M. Cameron 		is_write = 1;
4203283b4a9bSStephen M. Cameron 	case READ_16:
4204283b4a9bSStephen M. Cameron 		first_block =
4205283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 56) |
4206283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 48) |
4207283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 40) |
4208283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[5]) << 32) |
4209283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[6]) << 24) |
4210283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[7]) << 16) |
4211283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[8]) << 8) |
4212283b4a9bSStephen M. Cameron 			cmd->cmnd[9];
4213283b4a9bSStephen M. Cameron 		block_cnt =
4214283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[10]) << 24) |
4215283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[11]) << 16) |
4216283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[12]) << 8) |
4217283b4a9bSStephen M. Cameron 			cmd->cmnd[13];
4218283b4a9bSStephen M. Cameron 		break;
4219283b4a9bSStephen M. Cameron 	default:
4220283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
4221283b4a9bSStephen M. Cameron 	}
4222283b4a9bSStephen M. Cameron 	last_block = first_block + block_cnt - 1;
4223283b4a9bSStephen M. Cameron 
4224283b4a9bSStephen M. Cameron 	/* check for write to non-RAID-0 */
4225283b4a9bSStephen M. Cameron 	if (is_write && dev->raid_level != 0)
4226283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
4227283b4a9bSStephen M. Cameron 
4228283b4a9bSStephen M. Cameron 	/* check for invalid block or wraparound */
42292b08b3e9SDon Brace 	if (last_block >= le64_to_cpu(map->volume_blk_cnt) ||
42302b08b3e9SDon Brace 		last_block < first_block)
4231283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
4232283b4a9bSStephen M. Cameron 
4233283b4a9bSStephen M. Cameron 	/* calculate stripe information for the request */
42342b08b3e9SDon Brace 	blocks_per_row = le16_to_cpu(map->data_disks_per_row) *
42352b08b3e9SDon Brace 				le16_to_cpu(map->strip_size);
42362b08b3e9SDon Brace 	strip_size = le16_to_cpu(map->strip_size);
4237283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
4238283b4a9bSStephen M. Cameron 	tmpdiv = first_block;
4239283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
4240283b4a9bSStephen M. Cameron 	first_row = tmpdiv;
4241283b4a9bSStephen M. Cameron 	tmpdiv = last_block;
4242283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
4243283b4a9bSStephen M. Cameron 	last_row = tmpdiv;
4244283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
4245283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
4246283b4a9bSStephen M. Cameron 	tmpdiv = first_row_offset;
42472b08b3e9SDon Brace 	(void) do_div(tmpdiv, strip_size);
4248283b4a9bSStephen M. Cameron 	first_column = tmpdiv;
4249283b4a9bSStephen M. Cameron 	tmpdiv = last_row_offset;
42502b08b3e9SDon Brace 	(void) do_div(tmpdiv, strip_size);
4251283b4a9bSStephen M. Cameron 	last_column = tmpdiv;
4252283b4a9bSStephen M. Cameron #else
4253283b4a9bSStephen M. Cameron 	first_row = first_block / blocks_per_row;
4254283b4a9bSStephen M. Cameron 	last_row = last_block / blocks_per_row;
4255283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
4256283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
42572b08b3e9SDon Brace 	first_column = first_row_offset / strip_size;
42582b08b3e9SDon Brace 	last_column = last_row_offset / strip_size;
4259283b4a9bSStephen M. Cameron #endif
4260283b4a9bSStephen M. Cameron 
4261283b4a9bSStephen M. Cameron 	/* if this isn't a single row/column then give to the controller */
4262283b4a9bSStephen M. Cameron 	if ((first_row != last_row) || (first_column != last_column))
4263283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
4264283b4a9bSStephen M. Cameron 
4265283b4a9bSStephen M. Cameron 	/* proceeding with driver mapping */
42662b08b3e9SDon Brace 	total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
42672b08b3e9SDon Brace 				le16_to_cpu(map->metadata_disks_per_row);
4268283b4a9bSStephen M. Cameron 	map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
42692b08b3e9SDon Brace 				le16_to_cpu(map->row_cnt);
42706b80b18fSScott Teel 	map_index = (map_row * total_disks_per_row) + first_column;
42716b80b18fSScott Teel 
42726b80b18fSScott Teel 	switch (dev->raid_level) {
42736b80b18fSScott Teel 	case HPSA_RAID_0:
42746b80b18fSScott Teel 		break; /* nothing special to do */
42756b80b18fSScott Teel 	case HPSA_RAID_1:
42766b80b18fSScott Teel 		/* Handles load balance across RAID 1 members.
42776b80b18fSScott Teel 		 * (2-drive R1 and R10 with even # of drives.)
42786b80b18fSScott Teel 		 * Appropriate for SSDs, not optimal for HDDs
4279283b4a9bSStephen M. Cameron 		 */
42802b08b3e9SDon Brace 		BUG_ON(le16_to_cpu(map->layout_map_count) != 2);
4281283b4a9bSStephen M. Cameron 		if (dev->offload_to_mirror)
42822b08b3e9SDon Brace 			map_index += le16_to_cpu(map->data_disks_per_row);
4283283b4a9bSStephen M. Cameron 		dev->offload_to_mirror = !dev->offload_to_mirror;
42846b80b18fSScott Teel 		break;
42856b80b18fSScott Teel 	case HPSA_RAID_ADM:
42866b80b18fSScott Teel 		/* Handles N-way mirrors  (R1-ADM)
42876b80b18fSScott Teel 		 * and R10 with # of drives divisible by 3.)
42886b80b18fSScott Teel 		 */
42892b08b3e9SDon Brace 		BUG_ON(le16_to_cpu(map->layout_map_count) != 3);
42906b80b18fSScott Teel 
42916b80b18fSScott Teel 		offload_to_mirror = dev->offload_to_mirror;
42926b80b18fSScott Teel 		raid_map_helper(map, offload_to_mirror,
42936b80b18fSScott Teel 				&map_index, &current_group);
42946b80b18fSScott Teel 		/* set mirror group to use next time */
42956b80b18fSScott Teel 		offload_to_mirror =
42962b08b3e9SDon Brace 			(offload_to_mirror >=
42972b08b3e9SDon Brace 			le16_to_cpu(map->layout_map_count) - 1)
42986b80b18fSScott Teel 			? 0 : offload_to_mirror + 1;
42996b80b18fSScott Teel 		dev->offload_to_mirror = offload_to_mirror;
43006b80b18fSScott Teel 		/* Avoid direct use of dev->offload_to_mirror within this
43016b80b18fSScott Teel 		 * function since multiple threads might simultaneously
43026b80b18fSScott Teel 		 * increment it beyond the range of dev->layout_map_count -1.
43036b80b18fSScott Teel 		 */
43046b80b18fSScott Teel 		break;
43056b80b18fSScott Teel 	case HPSA_RAID_5:
43066b80b18fSScott Teel 	case HPSA_RAID_6:
43072b08b3e9SDon Brace 		if (le16_to_cpu(map->layout_map_count) <= 1)
43086b80b18fSScott Teel 			break;
43096b80b18fSScott Teel 
43106b80b18fSScott Teel 		/* Verify first and last block are in same RAID group */
43116b80b18fSScott Teel 		r5or6_blocks_per_row =
43122b08b3e9SDon Brace 			le16_to_cpu(map->strip_size) *
43132b08b3e9SDon Brace 			le16_to_cpu(map->data_disks_per_row);
43146b80b18fSScott Teel 		BUG_ON(r5or6_blocks_per_row == 0);
43152b08b3e9SDon Brace 		stripesize = r5or6_blocks_per_row *
43162b08b3e9SDon Brace 			le16_to_cpu(map->layout_map_count);
43176b80b18fSScott Teel #if BITS_PER_LONG == 32
43186b80b18fSScott Teel 		tmpdiv = first_block;
43196b80b18fSScott Teel 		first_group = do_div(tmpdiv, stripesize);
43206b80b18fSScott Teel 		tmpdiv = first_group;
43216b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
43226b80b18fSScott Teel 		first_group = tmpdiv;
43236b80b18fSScott Teel 		tmpdiv = last_block;
43246b80b18fSScott Teel 		last_group = do_div(tmpdiv, stripesize);
43256b80b18fSScott Teel 		tmpdiv = last_group;
43266b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
43276b80b18fSScott Teel 		last_group = tmpdiv;
43286b80b18fSScott Teel #else
43296b80b18fSScott Teel 		first_group = (first_block % stripesize) / r5or6_blocks_per_row;
43306b80b18fSScott Teel 		last_group = (last_block % stripesize) / r5or6_blocks_per_row;
43316b80b18fSScott Teel #endif
4332000ff7c2SStephen M. Cameron 		if (first_group != last_group)
43336b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
43346b80b18fSScott Teel 
43356b80b18fSScott Teel 		/* Verify request is in a single row of RAID 5/6 */
43366b80b18fSScott Teel #if BITS_PER_LONG == 32
43376b80b18fSScott Teel 		tmpdiv = first_block;
43386b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
43396b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row = tmpdiv;
43406b80b18fSScott Teel 		tmpdiv = last_block;
43416b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
43426b80b18fSScott Teel 		r5or6_last_row = r0_last_row = tmpdiv;
43436b80b18fSScott Teel #else
43446b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row =
43456b80b18fSScott Teel 						first_block / stripesize;
43466b80b18fSScott Teel 		r5or6_last_row = r0_last_row = last_block / stripesize;
43476b80b18fSScott Teel #endif
43486b80b18fSScott Teel 		if (r5or6_first_row != r5or6_last_row)
43496b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
43506b80b18fSScott Teel 
43516b80b18fSScott Teel 
43526b80b18fSScott Teel 		/* Verify request is in a single column */
43536b80b18fSScott Teel #if BITS_PER_LONG == 32
43546b80b18fSScott Teel 		tmpdiv = first_block;
43556b80b18fSScott Teel 		first_row_offset = do_div(tmpdiv, stripesize);
43566b80b18fSScott Teel 		tmpdiv = first_row_offset;
43576b80b18fSScott Teel 		first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
43586b80b18fSScott Teel 		r5or6_first_row_offset = first_row_offset;
43596b80b18fSScott Teel 		tmpdiv = last_block;
43606b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, stripesize);
43616b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
43626b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
43636b80b18fSScott Teel 		tmpdiv = r5or6_first_row_offset;
43646b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
43656b80b18fSScott Teel 		first_column = r5or6_first_column = tmpdiv;
43666b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
43676b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
43686b80b18fSScott Teel 		r5or6_last_column = tmpdiv;
43696b80b18fSScott Teel #else
43706b80b18fSScott Teel 		first_row_offset = r5or6_first_row_offset =
43716b80b18fSScott Teel 			(u32)((first_block % stripesize) %
43726b80b18fSScott Teel 						r5or6_blocks_per_row);
43736b80b18fSScott Teel 
43746b80b18fSScott Teel 		r5or6_last_row_offset =
43756b80b18fSScott Teel 			(u32)((last_block % stripesize) %
43766b80b18fSScott Teel 						r5or6_blocks_per_row);
43776b80b18fSScott Teel 
43786b80b18fSScott Teel 		first_column = r5or6_first_column =
43792b08b3e9SDon Brace 			r5or6_first_row_offset / le16_to_cpu(map->strip_size);
43806b80b18fSScott Teel 		r5or6_last_column =
43812b08b3e9SDon Brace 			r5or6_last_row_offset / le16_to_cpu(map->strip_size);
43826b80b18fSScott Teel #endif
43836b80b18fSScott Teel 		if (r5or6_first_column != r5or6_last_column)
43846b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
43856b80b18fSScott Teel 
43866b80b18fSScott Teel 		/* Request is eligible */
43876b80b18fSScott Teel 		map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
43882b08b3e9SDon Brace 			le16_to_cpu(map->row_cnt);
43896b80b18fSScott Teel 
43906b80b18fSScott Teel 		map_index = (first_group *
43912b08b3e9SDon Brace 			(le16_to_cpu(map->row_cnt) * total_disks_per_row)) +
43926b80b18fSScott Teel 			(map_row * total_disks_per_row) + first_column;
43936b80b18fSScott Teel 		break;
43946b80b18fSScott Teel 	default:
43956b80b18fSScott Teel 		return IO_ACCEL_INELIGIBLE;
4396283b4a9bSStephen M. Cameron 	}
43976b80b18fSScott Teel 
439807543e0cSStephen Cameron 	if (unlikely(map_index >= RAID_MAP_MAX_ENTRIES))
439907543e0cSStephen Cameron 		return IO_ACCEL_INELIGIBLE;
440007543e0cSStephen Cameron 
440103383736SDon Brace 	c->phys_disk = dev->phys_disk[map_index];
440203383736SDon Brace 
4403283b4a9bSStephen M. Cameron 	disk_handle = dd[map_index].ioaccel_handle;
44042b08b3e9SDon Brace 	disk_block = le64_to_cpu(map->disk_starting_blk) +
44052b08b3e9SDon Brace 			first_row * le16_to_cpu(map->strip_size) +
44062b08b3e9SDon Brace 			(first_row_offset - first_column *
44072b08b3e9SDon Brace 			le16_to_cpu(map->strip_size));
4408283b4a9bSStephen M. Cameron 	disk_block_cnt = block_cnt;
4409283b4a9bSStephen M. Cameron 
4410283b4a9bSStephen M. Cameron 	/* handle differing logical/physical block sizes */
4411283b4a9bSStephen M. Cameron 	if (map->phys_blk_shift) {
4412283b4a9bSStephen M. Cameron 		disk_block <<= map->phys_blk_shift;
4413283b4a9bSStephen M. Cameron 		disk_block_cnt <<= map->phys_blk_shift;
4414283b4a9bSStephen M. Cameron 	}
4415283b4a9bSStephen M. Cameron 	BUG_ON(disk_block_cnt > 0xffff);
4416283b4a9bSStephen M. Cameron 
4417283b4a9bSStephen M. Cameron 	/* build the new CDB for the physical disk I/O */
4418283b4a9bSStephen M. Cameron 	if (disk_block > 0xffffffff) {
4419283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_16 : READ_16;
4420283b4a9bSStephen M. Cameron 		cdb[1] = 0;
4421283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 56);
4422283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 48);
4423283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 40);
4424283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block >> 32);
4425283b4a9bSStephen M. Cameron 		cdb[6] = (u8) (disk_block >> 24);
4426283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block >> 16);
4427283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block >> 8);
4428283b4a9bSStephen M. Cameron 		cdb[9] = (u8) (disk_block);
4429283b4a9bSStephen M. Cameron 		cdb[10] = (u8) (disk_block_cnt >> 24);
4430283b4a9bSStephen M. Cameron 		cdb[11] = (u8) (disk_block_cnt >> 16);
4431283b4a9bSStephen M. Cameron 		cdb[12] = (u8) (disk_block_cnt >> 8);
4432283b4a9bSStephen M. Cameron 		cdb[13] = (u8) (disk_block_cnt);
4433283b4a9bSStephen M. Cameron 		cdb[14] = 0;
4434283b4a9bSStephen M. Cameron 		cdb[15] = 0;
4435283b4a9bSStephen M. Cameron 		cdb_len = 16;
4436283b4a9bSStephen M. Cameron 	} else {
4437283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
4438283b4a9bSStephen M. Cameron 		cdb[1] = 0;
4439283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 24);
4440283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 16);
4441283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 8);
4442283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block);
4443283b4a9bSStephen M. Cameron 		cdb[6] = 0;
4444283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block_cnt >> 8);
4445283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block_cnt);
4446283b4a9bSStephen M. Cameron 		cdb[9] = 0;
4447283b4a9bSStephen M. Cameron 		cdb_len = 10;
4448283b4a9bSStephen M. Cameron 	}
4449283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
445003383736SDon Brace 						dev->scsi3addr,
445103383736SDon Brace 						dev->phys_disk[map_index]);
4452283b4a9bSStephen M. Cameron }
4453283b4a9bSStephen M. Cameron 
445425163bd5SWebb Scales /*
445525163bd5SWebb Scales  * Submit commands down the "normal" RAID stack path
445625163bd5SWebb Scales  * All callers to hpsa_ciss_submit must check lockup_detected
445725163bd5SWebb Scales  * beforehand, before (opt.) and after calling cmd_alloc
445825163bd5SWebb Scales  */
4459574f05d3SStephen Cameron static int hpsa_ciss_submit(struct ctlr_info *h,
4460574f05d3SStephen Cameron 	struct CommandList *c, struct scsi_cmnd *cmd,
4461574f05d3SStephen Cameron 	unsigned char scsi3addr[])
4462edd16368SStephen M. Cameron {
4463edd16368SStephen M. Cameron 	cmd->host_scribble = (unsigned char *) c;
4464edd16368SStephen M. Cameron 	c->cmd_type = CMD_SCSI;
4465edd16368SStephen M. Cameron 	c->scsi_cmd = cmd;
4466edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;  /* unused in simple mode */
4467edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
4468f2405db8SDon Brace 	c->Header.tag = cpu_to_le64((c->cmdindex << DIRECT_LOOKUP_SHIFT));
4469edd16368SStephen M. Cameron 
4470edd16368SStephen M. Cameron 	/* Fill in the request block... */
4471edd16368SStephen M. Cameron 
4472edd16368SStephen M. Cameron 	c->Request.Timeout = 0;
4473edd16368SStephen M. Cameron 	BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
4474edd16368SStephen M. Cameron 	c->Request.CDBLen = cmd->cmd_len;
4475edd16368SStephen M. Cameron 	memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
4476edd16368SStephen M. Cameron 	switch (cmd->sc_data_direction) {
4477edd16368SStephen M. Cameron 	case DMA_TO_DEVICE:
4478a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4479a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_WRITE);
4480edd16368SStephen M. Cameron 		break;
4481edd16368SStephen M. Cameron 	case DMA_FROM_DEVICE:
4482a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4483a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_READ);
4484edd16368SStephen M. Cameron 		break;
4485edd16368SStephen M. Cameron 	case DMA_NONE:
4486a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4487a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_NONE);
4488edd16368SStephen M. Cameron 		break;
4489edd16368SStephen M. Cameron 	case DMA_BIDIRECTIONAL:
4490edd16368SStephen M. Cameron 		/* This can happen if a buggy application does a scsi passthru
4491edd16368SStephen M. Cameron 		 * and sets both inlen and outlen to non-zero. ( see
4492edd16368SStephen M. Cameron 		 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
4493edd16368SStephen M. Cameron 		 */
4494edd16368SStephen M. Cameron 
4495a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4496a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_RSVD);
4497edd16368SStephen M. Cameron 		/* This is technically wrong, and hpsa controllers should
4498edd16368SStephen M. Cameron 		 * reject it with CMD_INVALID, which is the most correct
4499edd16368SStephen M. Cameron 		 * response, but non-fibre backends appear to let it
4500edd16368SStephen M. Cameron 		 * slide by, and give the same results as if this field
4501edd16368SStephen M. Cameron 		 * were set correctly.  Either way is acceptable for
4502edd16368SStephen M. Cameron 		 * our purposes here.
4503edd16368SStephen M. Cameron 		 */
4504edd16368SStephen M. Cameron 
4505edd16368SStephen M. Cameron 		break;
4506edd16368SStephen M. Cameron 
4507edd16368SStephen M. Cameron 	default:
4508edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4509edd16368SStephen M. Cameron 			cmd->sc_data_direction);
4510edd16368SStephen M. Cameron 		BUG();
4511edd16368SStephen M. Cameron 		break;
4512edd16368SStephen M. Cameron 	}
4513edd16368SStephen M. Cameron 
451433a2ffceSStephen M. Cameron 	if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
451573153fe5SWebb Scales 		hpsa_cmd_resolve_and_free(h, c);
4516edd16368SStephen M. Cameron 		return SCSI_MLQUEUE_HOST_BUSY;
4517edd16368SStephen M. Cameron 	}
4518edd16368SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
4519edd16368SStephen M. Cameron 	/* the cmd'll come back via intr handler in complete_scsi_command()  */
4520edd16368SStephen M. Cameron 	return 0;
4521edd16368SStephen M. Cameron }
4522edd16368SStephen M. Cameron 
4523360c73bdSStephen Cameron static void hpsa_cmd_init(struct ctlr_info *h, int index,
4524360c73bdSStephen Cameron 				struct CommandList *c)
4525360c73bdSStephen Cameron {
4526360c73bdSStephen Cameron 	dma_addr_t cmd_dma_handle, err_dma_handle;
4527360c73bdSStephen Cameron 
4528360c73bdSStephen Cameron 	/* Zero out all of commandlist except the last field, refcount */
4529360c73bdSStephen Cameron 	memset(c, 0, offsetof(struct CommandList, refcount));
4530360c73bdSStephen Cameron 	c->Header.tag = cpu_to_le64((u64) (index << DIRECT_LOOKUP_SHIFT));
4531360c73bdSStephen Cameron 	cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
4532360c73bdSStephen Cameron 	c->err_info = h->errinfo_pool + index;
4533360c73bdSStephen Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
4534360c73bdSStephen Cameron 	err_dma_handle = h->errinfo_pool_dhandle
4535360c73bdSStephen Cameron 	    + index * sizeof(*c->err_info);
4536360c73bdSStephen Cameron 	c->cmdindex = index;
4537360c73bdSStephen Cameron 	c->busaddr = (u32) cmd_dma_handle;
4538360c73bdSStephen Cameron 	c->ErrDesc.Addr = cpu_to_le64((u64) err_dma_handle);
4539360c73bdSStephen Cameron 	c->ErrDesc.Len = cpu_to_le32((u32) sizeof(*c->err_info));
4540360c73bdSStephen Cameron 	c->h = h;
4541a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_IDLE;
4542360c73bdSStephen Cameron }
4543360c73bdSStephen Cameron 
4544360c73bdSStephen Cameron static void hpsa_preinitialize_commands(struct ctlr_info *h)
4545360c73bdSStephen Cameron {
4546360c73bdSStephen Cameron 	int i;
4547360c73bdSStephen Cameron 
4548360c73bdSStephen Cameron 	for (i = 0; i < h->nr_cmds; i++) {
4549360c73bdSStephen Cameron 		struct CommandList *c = h->cmd_pool + i;
4550360c73bdSStephen Cameron 
4551360c73bdSStephen Cameron 		hpsa_cmd_init(h, i, c);
4552360c73bdSStephen Cameron 		atomic_set(&c->refcount, 0);
4553360c73bdSStephen Cameron 	}
4554360c73bdSStephen Cameron }
4555360c73bdSStephen Cameron 
4556360c73bdSStephen Cameron static inline void hpsa_cmd_partial_init(struct ctlr_info *h, int index,
4557360c73bdSStephen Cameron 				struct CommandList *c)
4558360c73bdSStephen Cameron {
4559360c73bdSStephen Cameron 	dma_addr_t cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
4560360c73bdSStephen Cameron 
456173153fe5SWebb Scales 	BUG_ON(c->cmdindex != index);
456273153fe5SWebb Scales 
4563360c73bdSStephen Cameron 	memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
4564360c73bdSStephen Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
4565360c73bdSStephen Cameron 	c->busaddr = (u32) cmd_dma_handle;
4566360c73bdSStephen Cameron }
4567360c73bdSStephen Cameron 
4568592a0ad5SWebb Scales static int hpsa_ioaccel_submit(struct ctlr_info *h,
4569592a0ad5SWebb Scales 		struct CommandList *c, struct scsi_cmnd *cmd,
4570592a0ad5SWebb Scales 		unsigned char *scsi3addr)
4571592a0ad5SWebb Scales {
4572592a0ad5SWebb Scales 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4573592a0ad5SWebb Scales 	int rc = IO_ACCEL_INELIGIBLE;
4574592a0ad5SWebb Scales 
4575592a0ad5SWebb Scales 	cmd->host_scribble = (unsigned char *) c;
4576592a0ad5SWebb Scales 
4577592a0ad5SWebb Scales 	if (dev->offload_enabled) {
4578592a0ad5SWebb Scales 		hpsa_cmd_init(h, c->cmdindex, c);
4579592a0ad5SWebb Scales 		c->cmd_type = CMD_SCSI;
4580592a0ad5SWebb Scales 		c->scsi_cmd = cmd;
4581592a0ad5SWebb Scales 		rc = hpsa_scsi_ioaccel_raid_map(h, c);
4582592a0ad5SWebb Scales 		if (rc < 0)     /* scsi_dma_map failed. */
4583592a0ad5SWebb Scales 			rc = SCSI_MLQUEUE_HOST_BUSY;
4584a3144e0bSJoe Handzik 	} else if (dev->hba_ioaccel_enabled) {
4585592a0ad5SWebb Scales 		hpsa_cmd_init(h, c->cmdindex, c);
4586592a0ad5SWebb Scales 		c->cmd_type = CMD_SCSI;
4587592a0ad5SWebb Scales 		c->scsi_cmd = cmd;
4588592a0ad5SWebb Scales 		rc = hpsa_scsi_ioaccel_direct_map(h, c);
4589592a0ad5SWebb Scales 		if (rc < 0)     /* scsi_dma_map failed. */
4590592a0ad5SWebb Scales 			rc = SCSI_MLQUEUE_HOST_BUSY;
4591592a0ad5SWebb Scales 	}
4592592a0ad5SWebb Scales 	return rc;
4593592a0ad5SWebb Scales }
4594592a0ad5SWebb Scales 
4595080ef1ccSDon Brace static void hpsa_command_resubmit_worker(struct work_struct *work)
4596080ef1ccSDon Brace {
4597080ef1ccSDon Brace 	struct scsi_cmnd *cmd;
4598080ef1ccSDon Brace 	struct hpsa_scsi_dev_t *dev;
45998a0ff92cSWebb Scales 	struct CommandList *c = container_of(work, struct CommandList, work);
4600080ef1ccSDon Brace 
4601080ef1ccSDon Brace 	cmd = c->scsi_cmd;
4602080ef1ccSDon Brace 	dev = cmd->device->hostdata;
4603080ef1ccSDon Brace 	if (!dev) {
4604080ef1ccSDon Brace 		cmd->result = DID_NO_CONNECT << 16;
46058a0ff92cSWebb Scales 		return hpsa_cmd_free_and_done(c->h, c, cmd);
4606080ef1ccSDon Brace 	}
4607a58e7e53SWebb Scales 	if (c->abort_pending)
4608a58e7e53SWebb Scales 		return hpsa_cmd_abort_and_free(c->h, c, cmd);
4609592a0ad5SWebb Scales 	if (c->cmd_type == CMD_IOACCEL2) {
4610592a0ad5SWebb Scales 		struct ctlr_info *h = c->h;
4611592a0ad5SWebb Scales 		struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
4612592a0ad5SWebb Scales 		int rc;
4613592a0ad5SWebb Scales 
4614592a0ad5SWebb Scales 		if (c2->error_data.serv_response ==
4615592a0ad5SWebb Scales 				IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL) {
4616592a0ad5SWebb Scales 			rc = hpsa_ioaccel_submit(h, c, cmd, dev->scsi3addr);
4617592a0ad5SWebb Scales 			if (rc == 0)
4618592a0ad5SWebb Scales 				return;
4619592a0ad5SWebb Scales 			if (rc == SCSI_MLQUEUE_HOST_BUSY) {
4620592a0ad5SWebb Scales 				/*
4621592a0ad5SWebb Scales 				 * If we get here, it means dma mapping failed.
4622592a0ad5SWebb Scales 				 * Try again via scsi mid layer, which will
4623592a0ad5SWebb Scales 				 * then get SCSI_MLQUEUE_HOST_BUSY.
4624592a0ad5SWebb Scales 				 */
4625592a0ad5SWebb Scales 				cmd->result = DID_IMM_RETRY << 16;
46268a0ff92cSWebb Scales 				return hpsa_cmd_free_and_done(h, c, cmd);
4627592a0ad5SWebb Scales 			}
4628592a0ad5SWebb Scales 			/* else, fall thru and resubmit down CISS path */
4629592a0ad5SWebb Scales 		}
4630592a0ad5SWebb Scales 	}
4631360c73bdSStephen Cameron 	hpsa_cmd_partial_init(c->h, c->cmdindex, c);
4632080ef1ccSDon Brace 	if (hpsa_ciss_submit(c->h, c, cmd, dev->scsi3addr)) {
4633080ef1ccSDon Brace 		/*
4634080ef1ccSDon Brace 		 * If we get here, it means dma mapping failed. Try
4635080ef1ccSDon Brace 		 * again via scsi mid layer, which will then get
4636080ef1ccSDon Brace 		 * SCSI_MLQUEUE_HOST_BUSY.
4637592a0ad5SWebb Scales 		 *
4638592a0ad5SWebb Scales 		 * hpsa_ciss_submit will have already freed c
4639592a0ad5SWebb Scales 		 * if it encountered a dma mapping failure.
4640080ef1ccSDon Brace 		 */
4641080ef1ccSDon Brace 		cmd->result = DID_IMM_RETRY << 16;
4642080ef1ccSDon Brace 		cmd->scsi_done(cmd);
4643080ef1ccSDon Brace 	}
4644080ef1ccSDon Brace }
4645080ef1ccSDon Brace 
4646574f05d3SStephen Cameron /* Running in struct Scsi_Host->host_lock less mode */
4647574f05d3SStephen Cameron static int hpsa_scsi_queue_command(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
4648574f05d3SStephen Cameron {
4649574f05d3SStephen Cameron 	struct ctlr_info *h;
4650574f05d3SStephen Cameron 	struct hpsa_scsi_dev_t *dev;
4651574f05d3SStephen Cameron 	unsigned char scsi3addr[8];
4652574f05d3SStephen Cameron 	struct CommandList *c;
4653574f05d3SStephen Cameron 	int rc = 0;
4654574f05d3SStephen Cameron 
4655574f05d3SStephen Cameron 	/* Get the ptr to our adapter structure out of cmd->host. */
4656574f05d3SStephen Cameron 	h = sdev_to_hba(cmd->device);
465773153fe5SWebb Scales 
465873153fe5SWebb Scales 	BUG_ON(cmd->request->tag < 0);
465973153fe5SWebb Scales 
4660574f05d3SStephen Cameron 	dev = cmd->device->hostdata;
4661574f05d3SStephen Cameron 	if (!dev) {
4662574f05d3SStephen Cameron 		cmd->result = DID_NO_CONNECT << 16;
4663574f05d3SStephen Cameron 		cmd->scsi_done(cmd);
4664574f05d3SStephen Cameron 		return 0;
4665574f05d3SStephen Cameron 	}
466673153fe5SWebb Scales 
4667574f05d3SStephen Cameron 	memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
4668574f05d3SStephen Cameron 
4669574f05d3SStephen Cameron 	if (unlikely(lockup_detected(h))) {
467025163bd5SWebb Scales 		cmd->result = DID_NO_CONNECT << 16;
4671574f05d3SStephen Cameron 		cmd->scsi_done(cmd);
4672574f05d3SStephen Cameron 		return 0;
4673574f05d3SStephen Cameron 	}
467473153fe5SWebb Scales 	c = cmd_tagged_alloc(h, cmd);
4675574f05d3SStephen Cameron 
4676407863cbSStephen Cameron 	/*
4677407863cbSStephen Cameron 	 * Call alternate submit routine for I/O accelerated commands.
4678574f05d3SStephen Cameron 	 * Retries always go down the normal I/O path.
4679574f05d3SStephen Cameron 	 */
4680574f05d3SStephen Cameron 	if (likely(cmd->retries == 0 &&
4681574f05d3SStephen Cameron 		cmd->request->cmd_type == REQ_TYPE_FS &&
4682574f05d3SStephen Cameron 		h->acciopath_status)) {
4683592a0ad5SWebb Scales 		rc = hpsa_ioaccel_submit(h, c, cmd, scsi3addr);
4684574f05d3SStephen Cameron 		if (rc == 0)
4685592a0ad5SWebb Scales 			return 0;
4686592a0ad5SWebb Scales 		if (rc == SCSI_MLQUEUE_HOST_BUSY) {
468773153fe5SWebb Scales 			hpsa_cmd_resolve_and_free(h, c);
4688574f05d3SStephen Cameron 			return SCSI_MLQUEUE_HOST_BUSY;
4689574f05d3SStephen Cameron 		}
4690574f05d3SStephen Cameron 	}
4691574f05d3SStephen Cameron 	return hpsa_ciss_submit(h, c, cmd, scsi3addr);
4692574f05d3SStephen Cameron }
4693574f05d3SStephen Cameron 
46948ebc9248SWebb Scales static void hpsa_scan_complete(struct ctlr_info *h)
46955f389360SStephen M. Cameron {
46965f389360SStephen M. Cameron 	unsigned long flags;
46975f389360SStephen M. Cameron 
46985f389360SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
46995f389360SStephen M. Cameron 	h->scan_finished = 1;
47005f389360SStephen M. Cameron 	wake_up_all(&h->scan_wait_queue);
47015f389360SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
47025f389360SStephen M. Cameron }
47035f389360SStephen M. Cameron 
4704a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *sh)
4705a08a8471SStephen M. Cameron {
4706a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
4707a08a8471SStephen M. Cameron 	unsigned long flags;
4708a08a8471SStephen M. Cameron 
47098ebc9248SWebb Scales 	/*
47108ebc9248SWebb Scales 	 * Don't let rescans be initiated on a controller known to be locked
47118ebc9248SWebb Scales 	 * up.  If the controller locks up *during* a rescan, that thread is
47128ebc9248SWebb Scales 	 * probably hosed, but at least we can prevent new rescan threads from
47138ebc9248SWebb Scales 	 * piling up on a locked up controller.
47148ebc9248SWebb Scales 	 */
47158ebc9248SWebb Scales 	if (unlikely(lockup_detected(h)))
47168ebc9248SWebb Scales 		return hpsa_scan_complete(h);
47175f389360SStephen M. Cameron 
4718a08a8471SStephen M. Cameron 	/* wait until any scan already in progress is finished. */
4719a08a8471SStephen M. Cameron 	while (1) {
4720a08a8471SStephen M. Cameron 		spin_lock_irqsave(&h->scan_lock, flags);
4721a08a8471SStephen M. Cameron 		if (h->scan_finished)
4722a08a8471SStephen M. Cameron 			break;
4723a08a8471SStephen M. Cameron 		spin_unlock_irqrestore(&h->scan_lock, flags);
4724a08a8471SStephen M. Cameron 		wait_event(h->scan_wait_queue, h->scan_finished);
4725a08a8471SStephen M. Cameron 		/* Note: We don't need to worry about a race between this
4726a08a8471SStephen M. Cameron 		 * thread and driver unload because the midlayer will
4727a08a8471SStephen M. Cameron 		 * have incremented the reference count, so unload won't
4728a08a8471SStephen M. Cameron 		 * happen if we're in here.
4729a08a8471SStephen M. Cameron 		 */
4730a08a8471SStephen M. Cameron 	}
4731a08a8471SStephen M. Cameron 	h->scan_finished = 0; /* mark scan as in progress */
4732a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4733a08a8471SStephen M. Cameron 
47348ebc9248SWebb Scales 	if (unlikely(lockup_detected(h)))
47358ebc9248SWebb Scales 		return hpsa_scan_complete(h);
47365f389360SStephen M. Cameron 
4737a08a8471SStephen M. Cameron 	hpsa_update_scsi_devices(h, h->scsi_host->host_no);
4738a08a8471SStephen M. Cameron 
47398ebc9248SWebb Scales 	hpsa_scan_complete(h);
4740a08a8471SStephen M. Cameron }
4741a08a8471SStephen M. Cameron 
47427c0a0229SDon Brace static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth)
47437c0a0229SDon Brace {
474403383736SDon Brace 	struct hpsa_scsi_dev_t *logical_drive = sdev->hostdata;
474503383736SDon Brace 
474603383736SDon Brace 	if (!logical_drive)
474703383736SDon Brace 		return -ENODEV;
47487c0a0229SDon Brace 
47497c0a0229SDon Brace 	if (qdepth < 1)
47507c0a0229SDon Brace 		qdepth = 1;
475103383736SDon Brace 	else if (qdepth > logical_drive->queue_depth)
475203383736SDon Brace 		qdepth = logical_drive->queue_depth;
475303383736SDon Brace 
475403383736SDon Brace 	return scsi_change_queue_depth(sdev, qdepth);
47557c0a0229SDon Brace }
47567c0a0229SDon Brace 
4757a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
4758a08a8471SStephen M. Cameron 	unsigned long elapsed_time)
4759a08a8471SStephen M. Cameron {
4760a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
4761a08a8471SStephen M. Cameron 	unsigned long flags;
4762a08a8471SStephen M. Cameron 	int finished;
4763a08a8471SStephen M. Cameron 
4764a08a8471SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
4765a08a8471SStephen M. Cameron 	finished = h->scan_finished;
4766a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4767a08a8471SStephen M. Cameron 	return finished;
4768a08a8471SStephen M. Cameron }
4769a08a8471SStephen M. Cameron 
47702946e82bSRobert Elliott static int hpsa_scsi_host_alloc(struct ctlr_info *h)
4771edd16368SStephen M. Cameron {
4772b705690dSStephen M. Cameron 	struct Scsi_Host *sh;
4773b705690dSStephen M. Cameron 	int error;
4774edd16368SStephen M. Cameron 
4775b705690dSStephen M. Cameron 	sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
47762946e82bSRobert Elliott 	if (sh == NULL) {
47772946e82bSRobert Elliott 		dev_err(&h->pdev->dev, "scsi_host_alloc failed\n");
47782946e82bSRobert Elliott 		return -ENOMEM;
47792946e82bSRobert Elliott 	}
4780b705690dSStephen M. Cameron 
4781b705690dSStephen M. Cameron 	sh->io_port = 0;
4782b705690dSStephen M. Cameron 	sh->n_io_port = 0;
4783b705690dSStephen M. Cameron 	sh->this_id = -1;
4784b705690dSStephen M. Cameron 	sh->max_channel = 3;
4785b705690dSStephen M. Cameron 	sh->max_cmd_len = MAX_COMMAND_SIZE;
4786b705690dSStephen M. Cameron 	sh->max_lun = HPSA_MAX_LUN;
4787b705690dSStephen M. Cameron 	sh->max_id = HPSA_MAX_LUN;
478841ce4c35SStephen Cameron 	sh->can_queue = h->nr_cmds - HPSA_NRESERVED_CMDS;
4789d54c5c24SStephen Cameron 	sh->cmd_per_lun = sh->can_queue;
4790b705690dSStephen M. Cameron 	sh->sg_tablesize = h->maxsgentries;
4791b705690dSStephen M. Cameron 	sh->hostdata[0] = (unsigned long) h;
4792b705690dSStephen M. Cameron 	sh->irq = h->intr[h->intr_mode];
4793b705690dSStephen M. Cameron 	sh->unique_id = sh->irq;
479473153fe5SWebb Scales 	error = scsi_init_shared_tag_map(sh, sh->can_queue);
479573153fe5SWebb Scales 	if (error) {
479673153fe5SWebb Scales 		dev_err(&h->pdev->dev,
479773153fe5SWebb Scales 			"%s: scsi_init_shared_tag_map failed for controller %d\n",
479873153fe5SWebb Scales 			__func__, h->ctlr);
4799b705690dSStephen M. Cameron 			scsi_host_put(sh);
4800b705690dSStephen M. Cameron 			return error;
48012946e82bSRobert Elliott 	}
48022946e82bSRobert Elliott 	h->scsi_host = sh;
48032946e82bSRobert Elliott 	return 0;
48042946e82bSRobert Elliott }
48052946e82bSRobert Elliott 
48062946e82bSRobert Elliott static int hpsa_scsi_add_host(struct ctlr_info *h)
48072946e82bSRobert Elliott {
48082946e82bSRobert Elliott 	int rv;
48092946e82bSRobert Elliott 
48102946e82bSRobert Elliott 	rv = scsi_add_host(h->scsi_host, &h->pdev->dev);
48112946e82bSRobert Elliott 	if (rv) {
48122946e82bSRobert Elliott 		dev_err(&h->pdev->dev, "scsi_add_host failed\n");
48132946e82bSRobert Elliott 		return rv;
48142946e82bSRobert Elliott 	}
48152946e82bSRobert Elliott 	scsi_scan_host(h->scsi_host);
48162946e82bSRobert Elliott 	return 0;
4817edd16368SStephen M. Cameron }
4818edd16368SStephen M. Cameron 
4819b69324ffSWebb Scales /*
482073153fe5SWebb Scales  * The block layer has already gone to the trouble of picking out a unique,
482173153fe5SWebb Scales  * small-integer tag for this request.  We use an offset from that value as
482273153fe5SWebb Scales  * an index to select our command block.  (The offset allows us to reserve the
482373153fe5SWebb Scales  * low-numbered entries for our own uses.)
482473153fe5SWebb Scales  */
482573153fe5SWebb Scales static int hpsa_get_cmd_index(struct scsi_cmnd *scmd)
482673153fe5SWebb Scales {
482773153fe5SWebb Scales 	int idx = scmd->request->tag;
482873153fe5SWebb Scales 
482973153fe5SWebb Scales 	if (idx < 0)
483073153fe5SWebb Scales 		return idx;
483173153fe5SWebb Scales 
483273153fe5SWebb Scales 	/* Offset to leave space for internal cmds. */
483373153fe5SWebb Scales 	return idx += HPSA_NRESERVED_CMDS;
483473153fe5SWebb Scales }
483573153fe5SWebb Scales 
483673153fe5SWebb Scales /*
4837b69324ffSWebb Scales  * Send a TEST_UNIT_READY command to the specified LUN using the specified
4838b69324ffSWebb Scales  * reply queue; returns zero if the unit is ready, and non-zero otherwise.
4839b69324ffSWebb Scales  */
4840b69324ffSWebb Scales static int hpsa_send_test_unit_ready(struct ctlr_info *h,
4841b69324ffSWebb Scales 				struct CommandList *c, unsigned char lunaddr[],
4842b69324ffSWebb Scales 				int reply_queue)
4843edd16368SStephen M. Cameron {
48448919358eSTomas Henzl 	int rc;
4845edd16368SStephen M. Cameron 
4846a2dac136SStephen M. Cameron 	/* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
4847a2dac136SStephen M. Cameron 	(void) fill_cmd(c, TEST_UNIT_READY, h,
4848a2dac136SStephen M. Cameron 			NULL, 0, 0, lunaddr, TYPE_CMD);
4849b69324ffSWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
485025163bd5SWebb Scales 	if (rc)
4851b69324ffSWebb Scales 		return rc;
4852edd16368SStephen M. Cameron 	/* no unmap needed here because no data xfer. */
4853edd16368SStephen M. Cameron 
4854b69324ffSWebb Scales 	/* Check if the unit is already ready. */
4855edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus == CMD_SUCCESS)
4856b69324ffSWebb Scales 		return 0;
4857edd16368SStephen M. Cameron 
4858b69324ffSWebb Scales 	/*
4859b69324ffSWebb Scales 	 * The first command sent after reset will receive "unit attention" to
4860b69324ffSWebb Scales 	 * indicate that the LUN has been reset...this is actually what we're
4861b69324ffSWebb Scales 	 * looking for (but, success is good too).
4862b69324ffSWebb Scales 	 */
4863edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
4864edd16368SStephen M. Cameron 		c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
4865edd16368SStephen M. Cameron 			(c->err_info->SenseInfo[2] == NO_SENSE ||
4866edd16368SStephen M. Cameron 			 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
4867b69324ffSWebb Scales 		return 0;
4868b69324ffSWebb Scales 
4869b69324ffSWebb Scales 	return 1;
4870b69324ffSWebb Scales }
4871b69324ffSWebb Scales 
4872b69324ffSWebb Scales /*
4873b69324ffSWebb Scales  * Wait for a TEST_UNIT_READY command to complete, retrying as necessary;
4874b69324ffSWebb Scales  * returns zero when the unit is ready, and non-zero when giving up.
4875b69324ffSWebb Scales  */
4876b69324ffSWebb Scales static int hpsa_wait_for_test_unit_ready(struct ctlr_info *h,
4877b69324ffSWebb Scales 				struct CommandList *c,
4878b69324ffSWebb Scales 				unsigned char lunaddr[], int reply_queue)
4879b69324ffSWebb Scales {
4880b69324ffSWebb Scales 	int rc;
4881b69324ffSWebb Scales 	int count = 0;
4882b69324ffSWebb Scales 	int waittime = 1; /* seconds */
4883b69324ffSWebb Scales 
4884b69324ffSWebb Scales 	/* Send test unit ready until device ready, or give up. */
4885b69324ffSWebb Scales 	for (count = 0; count < HPSA_TUR_RETRY_LIMIT; count++) {
4886b69324ffSWebb Scales 
4887b69324ffSWebb Scales 		/*
4888b69324ffSWebb Scales 		 * Wait for a bit.  do this first, because if we send
4889b69324ffSWebb Scales 		 * the TUR right away, the reset will just abort it.
4890b69324ffSWebb Scales 		 */
4891b69324ffSWebb Scales 		msleep(1000 * waittime);
4892b69324ffSWebb Scales 
4893b69324ffSWebb Scales 		rc = hpsa_send_test_unit_ready(h, c, lunaddr, reply_queue);
4894b69324ffSWebb Scales 		if (!rc)
4895edd16368SStephen M. Cameron 			break;
4896b69324ffSWebb Scales 
4897b69324ffSWebb Scales 		/* Increase wait time with each try, up to a point. */
4898b69324ffSWebb Scales 		if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
4899b69324ffSWebb Scales 			waittime *= 2;
4900b69324ffSWebb Scales 
4901b69324ffSWebb Scales 		dev_warn(&h->pdev->dev,
4902b69324ffSWebb Scales 			 "waiting %d secs for device to become ready.\n",
4903b69324ffSWebb Scales 			 waittime);
4904b69324ffSWebb Scales 	}
4905b69324ffSWebb Scales 
4906b69324ffSWebb Scales 	return rc;
4907b69324ffSWebb Scales }
4908b69324ffSWebb Scales 
4909b69324ffSWebb Scales static int wait_for_device_to_become_ready(struct ctlr_info *h,
4910b69324ffSWebb Scales 					   unsigned char lunaddr[],
4911b69324ffSWebb Scales 					   int reply_queue)
4912b69324ffSWebb Scales {
4913b69324ffSWebb Scales 	int first_queue;
4914b69324ffSWebb Scales 	int last_queue;
4915b69324ffSWebb Scales 	int rq;
4916b69324ffSWebb Scales 	int rc = 0;
4917b69324ffSWebb Scales 	struct CommandList *c;
4918b69324ffSWebb Scales 
4919b69324ffSWebb Scales 	c = cmd_alloc(h);
4920b69324ffSWebb Scales 
4921b69324ffSWebb Scales 	/*
4922b69324ffSWebb Scales 	 * If no specific reply queue was requested, then send the TUR
4923b69324ffSWebb Scales 	 * repeatedly, requesting a reply on each reply queue; otherwise execute
4924b69324ffSWebb Scales 	 * the loop exactly once using only the specified queue.
4925b69324ffSWebb Scales 	 */
4926b69324ffSWebb Scales 	if (reply_queue == DEFAULT_REPLY_QUEUE) {
4927b69324ffSWebb Scales 		first_queue = 0;
4928b69324ffSWebb Scales 		last_queue = h->nreply_queues - 1;
4929b69324ffSWebb Scales 	} else {
4930b69324ffSWebb Scales 		first_queue = reply_queue;
4931b69324ffSWebb Scales 		last_queue = reply_queue;
4932b69324ffSWebb Scales 	}
4933b69324ffSWebb Scales 
4934b69324ffSWebb Scales 	for (rq = first_queue; rq <= last_queue; rq++) {
4935b69324ffSWebb Scales 		rc = hpsa_wait_for_test_unit_ready(h, c, lunaddr, rq);
4936b69324ffSWebb Scales 		if (rc)
4937b69324ffSWebb Scales 			break;
4938edd16368SStephen M. Cameron 	}
4939edd16368SStephen M. Cameron 
4940edd16368SStephen M. Cameron 	if (rc)
4941edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "giving up on device.\n");
4942edd16368SStephen M. Cameron 	else
4943edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "device is ready.\n");
4944edd16368SStephen M. Cameron 
494545fcb86eSStephen Cameron 	cmd_free(h, c);
4946edd16368SStephen M. Cameron 	return rc;
4947edd16368SStephen M. Cameron }
4948edd16368SStephen M. Cameron 
4949edd16368SStephen M. Cameron /* Need at least one of these error handlers to keep ../scsi/hosts.c from
4950edd16368SStephen M. Cameron  * complaining.  Doing a host- or bus-reset can't do anything good here.
4951edd16368SStephen M. Cameron  */
4952edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
4953edd16368SStephen M. Cameron {
4954edd16368SStephen M. Cameron 	int rc;
4955edd16368SStephen M. Cameron 	struct ctlr_info *h;
4956edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
495773153fe5SWebb Scales 	char msg[40];
4958edd16368SStephen M. Cameron 
4959edd16368SStephen M. Cameron 	/* find the controller to which the command to be aborted was sent */
4960edd16368SStephen M. Cameron 	h = sdev_to_hba(scsicmd->device);
4961edd16368SStephen M. Cameron 	if (h == NULL) /* paranoia */
4962edd16368SStephen M. Cameron 		return FAILED;
4963e345893bSDon Brace 
4964e345893bSDon Brace 	if (lockup_detected(h))
4965e345893bSDon Brace 		return FAILED;
4966e345893bSDon Brace 
4967edd16368SStephen M. Cameron 	dev = scsicmd->device->hostdata;
4968edd16368SStephen M. Cameron 	if (!dev) {
4969edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
4970edd16368SStephen M. Cameron 			"device lookup failed.\n");
4971edd16368SStephen M. Cameron 		return FAILED;
4972edd16368SStephen M. Cameron 	}
497325163bd5SWebb Scales 
497425163bd5SWebb Scales 	/* if controller locked up, we can guarantee command won't complete */
497525163bd5SWebb Scales 	if (lockup_detected(h)) {
497673153fe5SWebb Scales 		sprintf(msg, "cmd %d RESET FAILED, lockup detected",
497773153fe5SWebb Scales 				hpsa_get_cmd_index(scsicmd));
497873153fe5SWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
497925163bd5SWebb Scales 		return FAILED;
498025163bd5SWebb Scales 	}
498125163bd5SWebb Scales 
498225163bd5SWebb Scales 	/* this reset request might be the result of a lockup; check */
498325163bd5SWebb Scales 	if (detect_controller_lockup(h)) {
498473153fe5SWebb Scales 		sprintf(msg, "cmd %d RESET FAILED, new lockup detected",
498573153fe5SWebb Scales 				hpsa_get_cmd_index(scsicmd));
498673153fe5SWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
498725163bd5SWebb Scales 		return FAILED;
498825163bd5SWebb Scales 	}
498925163bd5SWebb Scales 
499025163bd5SWebb Scales 	hpsa_show_dev_msg(KERN_WARNING, h, dev, "resetting");
499125163bd5SWebb Scales 
4992edd16368SStephen M. Cameron 	/* send a reset to the SCSI LUN which the command was sent to */
499325163bd5SWebb Scales 	rc = hpsa_send_reset(h, dev->scsi3addr, HPSA_RESET_TYPE_LUN,
499425163bd5SWebb Scales 			     DEFAULT_REPLY_QUEUE);
4995b69324ffSWebb Scales 	if (rc == 0)
4996edd16368SStephen M. Cameron 		return SUCCESS;
4997edd16368SStephen M. Cameron 
499825163bd5SWebb Scales 	dev_warn(&h->pdev->dev,
499925163bd5SWebb Scales 		"scsi %d:%d:%d:%d reset failed\n",
500025163bd5SWebb Scales 		h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
5001edd16368SStephen M. Cameron 	return FAILED;
5002edd16368SStephen M. Cameron }
5003edd16368SStephen M. Cameron 
50046cba3f19SStephen M. Cameron static void swizzle_abort_tag(u8 *tag)
50056cba3f19SStephen M. Cameron {
50066cba3f19SStephen M. Cameron 	u8 original_tag[8];
50076cba3f19SStephen M. Cameron 
50086cba3f19SStephen M. Cameron 	memcpy(original_tag, tag, 8);
50096cba3f19SStephen M. Cameron 	tag[0] = original_tag[3];
50106cba3f19SStephen M. Cameron 	tag[1] = original_tag[2];
50116cba3f19SStephen M. Cameron 	tag[2] = original_tag[1];
50126cba3f19SStephen M. Cameron 	tag[3] = original_tag[0];
50136cba3f19SStephen M. Cameron 	tag[4] = original_tag[7];
50146cba3f19SStephen M. Cameron 	tag[5] = original_tag[6];
50156cba3f19SStephen M. Cameron 	tag[6] = original_tag[5];
50166cba3f19SStephen M. Cameron 	tag[7] = original_tag[4];
50176cba3f19SStephen M. Cameron }
50186cba3f19SStephen M. Cameron 
501917eb87d2SScott Teel static void hpsa_get_tag(struct ctlr_info *h,
50202b08b3e9SDon Brace 	struct CommandList *c, __le32 *taglower, __le32 *tagupper)
502117eb87d2SScott Teel {
50222b08b3e9SDon Brace 	u64 tag;
502317eb87d2SScott Teel 	if (c->cmd_type == CMD_IOACCEL1) {
502417eb87d2SScott Teel 		struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
502517eb87d2SScott Teel 			&h->ioaccel_cmd_pool[c->cmdindex];
50262b08b3e9SDon Brace 		tag = le64_to_cpu(cm1->tag);
50272b08b3e9SDon Brace 		*tagupper = cpu_to_le32(tag >> 32);
50282b08b3e9SDon Brace 		*taglower = cpu_to_le32(tag);
502954b6e9e9SScott Teel 		return;
503054b6e9e9SScott Teel 	}
503154b6e9e9SScott Teel 	if (c->cmd_type == CMD_IOACCEL2) {
503254b6e9e9SScott Teel 		struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
503354b6e9e9SScott Teel 			&h->ioaccel2_cmd_pool[c->cmdindex];
5034dd0e19f3SScott Teel 		/* upper tag not used in ioaccel2 mode */
5035dd0e19f3SScott Teel 		memset(tagupper, 0, sizeof(*tagupper));
5036dd0e19f3SScott Teel 		*taglower = cm2->Tag;
503754b6e9e9SScott Teel 		return;
503854b6e9e9SScott Teel 	}
50392b08b3e9SDon Brace 	tag = le64_to_cpu(c->Header.tag);
50402b08b3e9SDon Brace 	*tagupper = cpu_to_le32(tag >> 32);
50412b08b3e9SDon Brace 	*taglower = cpu_to_le32(tag);
504217eb87d2SScott Teel }
504354b6e9e9SScott Teel 
504475167d2cSStephen M. Cameron static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
50459b5c48c2SStephen Cameron 	struct CommandList *abort, int reply_queue)
504675167d2cSStephen M. Cameron {
504775167d2cSStephen M. Cameron 	int rc = IO_OK;
504875167d2cSStephen M. Cameron 	struct CommandList *c;
504975167d2cSStephen M. Cameron 	struct ErrorInfo *ei;
50502b08b3e9SDon Brace 	__le32 tagupper, taglower;
505175167d2cSStephen M. Cameron 
505245fcb86eSStephen Cameron 	c = cmd_alloc(h);
505375167d2cSStephen M. Cameron 
5054a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no buffer to map */
50559b5c48c2SStephen Cameron 	(void) fill_cmd(c, HPSA_ABORT_MSG, h, &abort->Header.tag,
5056a2dac136SStephen M. Cameron 		0, 0, scsi3addr, TYPE_MSG);
50579b5c48c2SStephen Cameron 	if (h->needs_abort_tags_swizzled)
50586cba3f19SStephen M. Cameron 		swizzle_abort_tag(&c->Request.CDB[4]);
505925163bd5SWebb Scales 	(void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
506017eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
506125163bd5SWebb Scales 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd(abort) completed.\n",
506217eb87d2SScott Teel 		__func__, tagupper, taglower);
506375167d2cSStephen M. Cameron 	/* no unmap needed here because no data xfer. */
506475167d2cSStephen M. Cameron 
506575167d2cSStephen M. Cameron 	ei = c->err_info;
506675167d2cSStephen M. Cameron 	switch (ei->CommandStatus) {
506775167d2cSStephen M. Cameron 	case CMD_SUCCESS:
506875167d2cSStephen M. Cameron 		break;
50699437ac43SStephen Cameron 	case CMD_TMF_STATUS:
50709437ac43SStephen Cameron 		rc = hpsa_evaluate_tmf_status(h, c);
50719437ac43SStephen Cameron 		break;
507275167d2cSStephen M. Cameron 	case CMD_UNABORTABLE: /* Very common, don't make noise. */
507375167d2cSStephen M. Cameron 		rc = -1;
507475167d2cSStephen M. Cameron 		break;
507575167d2cSStephen M. Cameron 	default:
507675167d2cSStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
507717eb87d2SScott Teel 			__func__, tagupper, taglower);
5078d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
507975167d2cSStephen M. Cameron 		rc = -1;
508075167d2cSStephen M. Cameron 		break;
508175167d2cSStephen M. Cameron 	}
508245fcb86eSStephen Cameron 	cmd_free(h, c);
5083dd0e19f3SScott Teel 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n",
5084dd0e19f3SScott Teel 		__func__, tagupper, taglower);
508575167d2cSStephen M. Cameron 	return rc;
508675167d2cSStephen M. Cameron }
508775167d2cSStephen M. Cameron 
50888be986ccSStephen Cameron static void setup_ioaccel2_abort_cmd(struct CommandList *c, struct ctlr_info *h,
50898be986ccSStephen Cameron 	struct CommandList *command_to_abort, int reply_queue)
50908be986ccSStephen Cameron {
50918be986ccSStephen Cameron 	struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
50928be986ccSStephen Cameron 	struct hpsa_tmf_struct *ac = (struct hpsa_tmf_struct *) c2;
50938be986ccSStephen Cameron 	struct io_accel2_cmd *c2a =
50948be986ccSStephen Cameron 		&h->ioaccel2_cmd_pool[command_to_abort->cmdindex];
5095a58e7e53SWebb Scales 	struct scsi_cmnd *scmd = command_to_abort->scsi_cmd;
50968be986ccSStephen Cameron 	struct hpsa_scsi_dev_t *dev = scmd->device->hostdata;
50978be986ccSStephen Cameron 
50988be986ccSStephen Cameron 	/*
50998be986ccSStephen Cameron 	 * We're overlaying struct hpsa_tmf_struct on top of something which
51008be986ccSStephen Cameron 	 * was allocated as a struct io_accel2_cmd, so we better be sure it
51018be986ccSStephen Cameron 	 * actually fits, and doesn't overrun the error info space.
51028be986ccSStephen Cameron 	 */
51038be986ccSStephen Cameron 	BUILD_BUG_ON(sizeof(struct hpsa_tmf_struct) >
51048be986ccSStephen Cameron 			sizeof(struct io_accel2_cmd));
51058be986ccSStephen Cameron 	BUG_ON(offsetof(struct io_accel2_cmd, error_data) <
51068be986ccSStephen Cameron 			offsetof(struct hpsa_tmf_struct, error_len) +
51078be986ccSStephen Cameron 				sizeof(ac->error_len));
51088be986ccSStephen Cameron 
51098be986ccSStephen Cameron 	c->cmd_type = IOACCEL2_TMF;
5110a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_BUSY;
5111a58e7e53SWebb Scales 
51128be986ccSStephen Cameron 	/* Adjust the DMA address to point to the accelerated command buffer */
51138be986ccSStephen Cameron 	c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
51148be986ccSStephen Cameron 				(c->cmdindex * sizeof(struct io_accel2_cmd));
51158be986ccSStephen Cameron 	BUG_ON(c->busaddr & 0x0000007F);
51168be986ccSStephen Cameron 
51178be986ccSStephen Cameron 	memset(ac, 0, sizeof(*c2)); /* yes this is correct */
51188be986ccSStephen Cameron 	ac->iu_type = IOACCEL2_IU_TMF_TYPE;
51198be986ccSStephen Cameron 	ac->reply_queue = reply_queue;
51208be986ccSStephen Cameron 	ac->tmf = IOACCEL2_TMF_ABORT;
51218be986ccSStephen Cameron 	ac->it_nexus = cpu_to_le32(dev->ioaccel_handle);
51228be986ccSStephen Cameron 	memset(ac->lun_id, 0, sizeof(ac->lun_id));
51238be986ccSStephen Cameron 	ac->tag = cpu_to_le64(c->cmdindex << DIRECT_LOOKUP_SHIFT);
51248be986ccSStephen Cameron 	ac->abort_tag = cpu_to_le64(le32_to_cpu(c2a->Tag));
51258be986ccSStephen Cameron 	ac->error_ptr = cpu_to_le64(c->busaddr +
51268be986ccSStephen Cameron 			offsetof(struct io_accel2_cmd, error_data));
51278be986ccSStephen Cameron 	ac->error_len = cpu_to_le32(sizeof(c2->error_data));
51288be986ccSStephen Cameron }
51298be986ccSStephen Cameron 
513054b6e9e9SScott Teel /* ioaccel2 path firmware cannot handle abort task requests.
513154b6e9e9SScott Teel  * Change abort requests to physical target reset, and send to the
513254b6e9e9SScott Teel  * address of the physical disk used for the ioaccel 2 command.
513354b6e9e9SScott Teel  * Return 0 on success (IO_OK)
513454b6e9e9SScott Teel  *	 -1 on failure
513554b6e9e9SScott Teel  */
513654b6e9e9SScott Teel 
513754b6e9e9SScott Teel static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
513825163bd5SWebb Scales 	unsigned char *scsi3addr, struct CommandList *abort, int reply_queue)
513954b6e9e9SScott Teel {
514054b6e9e9SScott Teel 	int rc = IO_OK;
514154b6e9e9SScott Teel 	struct scsi_cmnd *scmd; /* scsi command within request being aborted */
514254b6e9e9SScott Teel 	struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
514354b6e9e9SScott Teel 	unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
514454b6e9e9SScott Teel 	unsigned char *psa = &phys_scsi3addr[0];
514554b6e9e9SScott Teel 
514654b6e9e9SScott Teel 	/* Get a pointer to the hpsa logical device. */
51477fa3030cSStephen Cameron 	scmd = abort->scsi_cmd;
514854b6e9e9SScott Teel 	dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
514954b6e9e9SScott Teel 	if (dev == NULL) {
515054b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
515154b6e9e9SScott Teel 			"Cannot abort: no device pointer for command.\n");
515254b6e9e9SScott Teel 			return -1; /* not abortable */
515354b6e9e9SScott Teel 	}
515454b6e9e9SScott Teel 
51552ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
51562ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
51570d96ef5fSWebb Scales 			"scsi %d:%d:%d:%d %s scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
51582ba8bfc8SStephen M. Cameron 			h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
51590d96ef5fSWebb Scales 			"Reset as abort",
51602ba8bfc8SStephen M. Cameron 			scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
51612ba8bfc8SStephen M. Cameron 			scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]);
51622ba8bfc8SStephen M. Cameron 
516354b6e9e9SScott Teel 	if (!dev->offload_enabled) {
516454b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
516554b6e9e9SScott Teel 			"Can't abort: device is not operating in HP SSD Smart Path mode.\n");
516654b6e9e9SScott Teel 		return -1; /* not abortable */
516754b6e9e9SScott Teel 	}
516854b6e9e9SScott Teel 
516954b6e9e9SScott Teel 	/* Incoming scsi3addr is logical addr. We need physical disk addr. */
517054b6e9e9SScott Teel 	if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
517154b6e9e9SScott Teel 		dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
517254b6e9e9SScott Teel 		return -1; /* not abortable */
517354b6e9e9SScott Teel 	}
517454b6e9e9SScott Teel 
517554b6e9e9SScott Teel 	/* send the reset */
51762ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
51772ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
51782ba8bfc8SStephen M. Cameron 			"Reset as abort: Resetting physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
51792ba8bfc8SStephen M. Cameron 			psa[0], psa[1], psa[2], psa[3],
51802ba8bfc8SStephen M. Cameron 			psa[4], psa[5], psa[6], psa[7]);
518125163bd5SWebb Scales 	rc = hpsa_send_reset(h, psa, HPSA_RESET_TYPE_TARGET, reply_queue);
518254b6e9e9SScott Teel 	if (rc != 0) {
518354b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
518454b6e9e9SScott Teel 			"Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
518554b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
518654b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
518754b6e9e9SScott Teel 		return rc; /* failed to reset */
518854b6e9e9SScott Teel 	}
518954b6e9e9SScott Teel 
519054b6e9e9SScott Teel 	/* wait for device to recover */
5191b69324ffSWebb Scales 	if (wait_for_device_to_become_ready(h, psa, reply_queue) != 0) {
519254b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
519354b6e9e9SScott Teel 			"Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
519454b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
519554b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
519654b6e9e9SScott Teel 		return -1;  /* failed to recover */
519754b6e9e9SScott Teel 	}
519854b6e9e9SScott Teel 
519954b6e9e9SScott Teel 	/* device recovered */
520054b6e9e9SScott Teel 	dev_info(&h->pdev->dev,
520154b6e9e9SScott Teel 		"Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
520254b6e9e9SScott Teel 		psa[0], psa[1], psa[2], psa[3],
520354b6e9e9SScott Teel 		psa[4], psa[5], psa[6], psa[7]);
520454b6e9e9SScott Teel 
520554b6e9e9SScott Teel 	return rc; /* success */
520654b6e9e9SScott Teel }
520754b6e9e9SScott Teel 
52088be986ccSStephen Cameron static int hpsa_send_abort_ioaccel2(struct ctlr_info *h,
52098be986ccSStephen Cameron 	struct CommandList *abort, int reply_queue)
52108be986ccSStephen Cameron {
52118be986ccSStephen Cameron 	int rc = IO_OK;
52128be986ccSStephen Cameron 	struct CommandList *c;
52138be986ccSStephen Cameron 	__le32 taglower, tagupper;
52148be986ccSStephen Cameron 	struct hpsa_scsi_dev_t *dev;
52158be986ccSStephen Cameron 	struct io_accel2_cmd *c2;
52168be986ccSStephen Cameron 
52178be986ccSStephen Cameron 	dev = abort->scsi_cmd->device->hostdata;
52188be986ccSStephen Cameron 	if (!dev->offload_enabled && !dev->hba_ioaccel_enabled)
52198be986ccSStephen Cameron 		return -1;
52208be986ccSStephen Cameron 
52218be986ccSStephen Cameron 	c = cmd_alloc(h);
52228be986ccSStephen Cameron 	setup_ioaccel2_abort_cmd(c, h, abort, reply_queue);
52238be986ccSStephen Cameron 	c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
52248be986ccSStephen Cameron 	(void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
52258be986ccSStephen Cameron 	hpsa_get_tag(h, abort, &taglower, &tagupper);
52268be986ccSStephen Cameron 	dev_dbg(&h->pdev->dev,
52278be986ccSStephen Cameron 		"%s: Tag:0x%08x:%08x: do_simple_cmd(ioaccel2 abort) completed.\n",
52288be986ccSStephen Cameron 		__func__, tagupper, taglower);
52298be986ccSStephen Cameron 	/* no unmap needed here because no data xfer. */
52308be986ccSStephen Cameron 
52318be986ccSStephen Cameron 	dev_dbg(&h->pdev->dev,
52328be986ccSStephen Cameron 		"%s: Tag:0x%08x:%08x: abort service response = 0x%02x.\n",
52338be986ccSStephen Cameron 		__func__, tagupper, taglower, c2->error_data.serv_response);
52348be986ccSStephen Cameron 	switch (c2->error_data.serv_response) {
52358be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
52368be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
52378be986ccSStephen Cameron 		rc = 0;
52388be986ccSStephen Cameron 		break;
52398be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
52408be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_FAILURE:
52418be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
52428be986ccSStephen Cameron 		rc = -1;
52438be986ccSStephen Cameron 		break;
52448be986ccSStephen Cameron 	default:
52458be986ccSStephen Cameron 		dev_warn(&h->pdev->dev,
52468be986ccSStephen Cameron 			"%s: Tag:0x%08x:%08x: unknown abort service response 0x%02x\n",
52478be986ccSStephen Cameron 			__func__, tagupper, taglower,
52488be986ccSStephen Cameron 			c2->error_data.serv_response);
52498be986ccSStephen Cameron 		rc = -1;
52508be986ccSStephen Cameron 	}
52518be986ccSStephen Cameron 	cmd_free(h, c);
52528be986ccSStephen Cameron 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n", __func__,
52538be986ccSStephen Cameron 		tagupper, taglower);
52548be986ccSStephen Cameron 	return rc;
52558be986ccSStephen Cameron }
52568be986ccSStephen Cameron 
52576cba3f19SStephen M. Cameron static int hpsa_send_abort_both_ways(struct ctlr_info *h,
525825163bd5SWebb Scales 	unsigned char *scsi3addr, struct CommandList *abort, int reply_queue)
52596cba3f19SStephen M. Cameron {
52608be986ccSStephen Cameron 	/*
52618be986ccSStephen Cameron 	 * ioccelerator mode 2 commands should be aborted via the
526254b6e9e9SScott Teel 	 * accelerated path, since RAID path is unaware of these commands,
52638be986ccSStephen Cameron 	 * but not all underlying firmware can handle abort TMF.
52648be986ccSStephen Cameron 	 * Change abort to physical device reset when abort TMF is unsupported.
526554b6e9e9SScott Teel 	 */
52668be986ccSStephen Cameron 	if (abort->cmd_type == CMD_IOACCEL2) {
52678be986ccSStephen Cameron 		if (HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags)
52688be986ccSStephen Cameron 			return hpsa_send_abort_ioaccel2(h, abort,
52698be986ccSStephen Cameron 						reply_queue);
52708be986ccSStephen Cameron 		else
527125163bd5SWebb Scales 			return hpsa_send_reset_as_abort_ioaccel2(h, scsi3addr,
527225163bd5SWebb Scales 							abort, reply_queue);
52738be986ccSStephen Cameron 	}
52749b5c48c2SStephen Cameron 	return hpsa_send_abort(h, scsi3addr, abort, reply_queue);
527525163bd5SWebb Scales }
527625163bd5SWebb Scales 
527725163bd5SWebb Scales /* Find out which reply queue a command was meant to return on */
527825163bd5SWebb Scales static int hpsa_extract_reply_queue(struct ctlr_info *h,
527925163bd5SWebb Scales 					struct CommandList *c)
528025163bd5SWebb Scales {
528125163bd5SWebb Scales 	if (c->cmd_type == CMD_IOACCEL2)
528225163bd5SWebb Scales 		return h->ioaccel2_cmd_pool[c->cmdindex].reply_queue;
528325163bd5SWebb Scales 	return c->Header.ReplyQueue;
52846cba3f19SStephen M. Cameron }
52856cba3f19SStephen M. Cameron 
52869b5c48c2SStephen Cameron /*
52879b5c48c2SStephen Cameron  * Limit concurrency of abort commands to prevent
52889b5c48c2SStephen Cameron  * over-subscription of commands
52899b5c48c2SStephen Cameron  */
52909b5c48c2SStephen Cameron static inline int wait_for_available_abort_cmd(struct ctlr_info *h)
52919b5c48c2SStephen Cameron {
52929b5c48c2SStephen Cameron #define ABORT_CMD_WAIT_MSECS 5000
52939b5c48c2SStephen Cameron 	return !wait_event_timeout(h->abort_cmd_wait_queue,
52949b5c48c2SStephen Cameron 			atomic_dec_if_positive(&h->abort_cmds_available) >= 0,
52959b5c48c2SStephen Cameron 			msecs_to_jiffies(ABORT_CMD_WAIT_MSECS));
52969b5c48c2SStephen Cameron }
52979b5c48c2SStephen Cameron 
529875167d2cSStephen M. Cameron /* Send an abort for the specified command.
529975167d2cSStephen M. Cameron  *	If the device and controller support it,
530075167d2cSStephen M. Cameron  *		send a task abort request.
530175167d2cSStephen M. Cameron  */
530275167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
530375167d2cSStephen M. Cameron {
530475167d2cSStephen M. Cameron 
5305a58e7e53SWebb Scales 	int rc;
530675167d2cSStephen M. Cameron 	struct ctlr_info *h;
530775167d2cSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
530875167d2cSStephen M. Cameron 	struct CommandList *abort; /* pointer to command to be aborted */
530975167d2cSStephen M. Cameron 	struct scsi_cmnd *as;	/* ptr to scsi cmd inside aborted command. */
531075167d2cSStephen M. Cameron 	char msg[256];		/* For debug messaging. */
531175167d2cSStephen M. Cameron 	int ml = 0;
53122b08b3e9SDon Brace 	__le32 tagupper, taglower;
531325163bd5SWebb Scales 	int refcount, reply_queue;
531425163bd5SWebb Scales 
531525163bd5SWebb Scales 	if (sc == NULL)
531625163bd5SWebb Scales 		return FAILED;
531775167d2cSStephen M. Cameron 
53189b5c48c2SStephen Cameron 	if (sc->device == NULL)
53199b5c48c2SStephen Cameron 		return FAILED;
53209b5c48c2SStephen Cameron 
532175167d2cSStephen M. Cameron 	/* Find the controller of the command to be aborted */
532275167d2cSStephen M. Cameron 	h = sdev_to_hba(sc->device);
53239b5c48c2SStephen Cameron 	if (h == NULL)
532475167d2cSStephen M. Cameron 		return FAILED;
532575167d2cSStephen M. Cameron 
532625163bd5SWebb Scales 	/* Find the device of the command to be aborted */
532725163bd5SWebb Scales 	dev = sc->device->hostdata;
532825163bd5SWebb Scales 	if (!dev) {
532925163bd5SWebb Scales 		dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
533025163bd5SWebb Scales 				msg);
5331e345893bSDon Brace 		return FAILED;
533225163bd5SWebb Scales 	}
533325163bd5SWebb Scales 
533425163bd5SWebb Scales 	/* If controller locked up, we can guarantee command won't complete */
533525163bd5SWebb Scales 	if (lockup_detected(h)) {
533625163bd5SWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev,
533725163bd5SWebb Scales 					"ABORT FAILED, lockup detected");
533825163bd5SWebb Scales 		return FAILED;
533925163bd5SWebb Scales 	}
534025163bd5SWebb Scales 
534125163bd5SWebb Scales 	/* This is a good time to check if controller lockup has occurred */
534225163bd5SWebb Scales 	if (detect_controller_lockup(h)) {
534325163bd5SWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev,
534425163bd5SWebb Scales 					"ABORT FAILED, new lockup detected");
534525163bd5SWebb Scales 		return FAILED;
534625163bd5SWebb Scales 	}
5347e345893bSDon Brace 
534875167d2cSStephen M. Cameron 	/* Check that controller supports some kind of task abort */
534975167d2cSStephen M. Cameron 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
535075167d2cSStephen M. Cameron 		!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
535175167d2cSStephen M. Cameron 		return FAILED;
535275167d2cSStephen M. Cameron 
535375167d2cSStephen M. Cameron 	memset(msg, 0, sizeof(msg));
53544b761557SRobert Elliott 	ml += sprintf(msg+ml, "scsi %d:%d:%d:%llu %s %p",
535575167d2cSStephen M. Cameron 		h->scsi_host->host_no, sc->device->channel,
53560d96ef5fSWebb Scales 		sc->device->id, sc->device->lun,
53574b761557SRobert Elliott 		"Aborting command", sc);
535875167d2cSStephen M. Cameron 
535975167d2cSStephen M. Cameron 	/* Get SCSI command to be aborted */
536075167d2cSStephen M. Cameron 	abort = (struct CommandList *) sc->host_scribble;
536175167d2cSStephen M. Cameron 	if (abort == NULL) {
5362281a7fd0SWebb Scales 		/* This can happen if the command already completed. */
5363281a7fd0SWebb Scales 		return SUCCESS;
5364281a7fd0SWebb Scales 	}
5365281a7fd0SWebb Scales 	refcount = atomic_inc_return(&abort->refcount);
5366281a7fd0SWebb Scales 	if (refcount == 1) { /* Command is done already. */
5367281a7fd0SWebb Scales 		cmd_free(h, abort);
5368281a7fd0SWebb Scales 		return SUCCESS;
536975167d2cSStephen M. Cameron 	}
53709b5c48c2SStephen Cameron 
53719b5c48c2SStephen Cameron 	/* Don't bother trying the abort if we know it won't work. */
53729b5c48c2SStephen Cameron 	if (abort->cmd_type != CMD_IOACCEL2 &&
53739b5c48c2SStephen Cameron 		abort->cmd_type != CMD_IOACCEL1 && !dev->supports_aborts) {
53749b5c48c2SStephen Cameron 		cmd_free(h, abort);
53759b5c48c2SStephen Cameron 		return FAILED;
53769b5c48c2SStephen Cameron 	}
53779b5c48c2SStephen Cameron 
5378a58e7e53SWebb Scales 	/*
5379a58e7e53SWebb Scales 	 * Check that we're aborting the right command.
5380a58e7e53SWebb Scales 	 * It's possible the CommandList already completed and got re-used.
5381a58e7e53SWebb Scales 	 */
5382a58e7e53SWebb Scales 	if (abort->scsi_cmd != sc) {
5383a58e7e53SWebb Scales 		cmd_free(h, abort);
5384a58e7e53SWebb Scales 		return SUCCESS;
5385a58e7e53SWebb Scales 	}
5386a58e7e53SWebb Scales 
5387a58e7e53SWebb Scales 	abort->abort_pending = true;
538817eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
538925163bd5SWebb Scales 	reply_queue = hpsa_extract_reply_queue(h, abort);
539017eb87d2SScott Teel 	ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
53917fa3030cSStephen Cameron 	as  = abort->scsi_cmd;
539275167d2cSStephen M. Cameron 	if (as != NULL)
53934b761557SRobert Elliott 		ml += sprintf(msg+ml,
53944b761557SRobert Elliott 			"CDBLen: %d CDB: 0x%02x%02x... SN: 0x%lx ",
53954b761557SRobert Elliott 			as->cmd_len, as->cmnd[0], as->cmnd[1],
53964b761557SRobert Elliott 			as->serial_number);
53974b761557SRobert Elliott 	dev_warn(&h->pdev->dev, "%s BEING SENT\n", msg);
53980d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_WARNING, h, dev, "Aborting command");
53994b761557SRobert Elliott 
540075167d2cSStephen M. Cameron 	/*
540175167d2cSStephen M. Cameron 	 * Command is in flight, or possibly already completed
540275167d2cSStephen M. Cameron 	 * by the firmware (but not to the scsi mid layer) but we can't
540375167d2cSStephen M. Cameron 	 * distinguish which.  Send the abort down.
540475167d2cSStephen M. Cameron 	 */
54059b5c48c2SStephen Cameron 	if (wait_for_available_abort_cmd(h)) {
54069b5c48c2SStephen Cameron 		dev_warn(&h->pdev->dev,
54074b761557SRobert Elliott 			"%s FAILED, timeout waiting for an abort command to become available.\n",
54084b761557SRobert Elliott 			msg);
54099b5c48c2SStephen Cameron 		cmd_free(h, abort);
54109b5c48c2SStephen Cameron 		return FAILED;
54119b5c48c2SStephen Cameron 	}
541225163bd5SWebb Scales 	rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort, reply_queue);
54139b5c48c2SStephen Cameron 	atomic_inc(&h->abort_cmds_available);
54149b5c48c2SStephen Cameron 	wake_up_all(&h->abort_cmd_wait_queue);
541575167d2cSStephen M. Cameron 	if (rc != 0) {
54164b761557SRobert Elliott 		dev_warn(&h->pdev->dev, "%s SENT, FAILED\n", msg);
54170d96ef5fSWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev,
54180d96ef5fSWebb Scales 				"FAILED to abort command");
5419281a7fd0SWebb Scales 		cmd_free(h, abort);
542075167d2cSStephen M. Cameron 		return FAILED;
542175167d2cSStephen M. Cameron 	}
54224b761557SRobert Elliott 	dev_info(&h->pdev->dev, "%s SENT, SUCCESS\n", msg);
5423a58e7e53SWebb Scales 	wait_event(h->abort_sync_wait_queue,
5424a58e7e53SWebb Scales 		   abort->scsi_cmd != sc || lockup_detected(h));
5425281a7fd0SWebb Scales 	cmd_free(h, abort);
5426a58e7e53SWebb Scales 	return !lockup_detected(h) ? SUCCESS : FAILED;
542775167d2cSStephen M. Cameron }
542875167d2cSStephen M. Cameron 
5429edd16368SStephen M. Cameron /*
543073153fe5SWebb Scales  * For operations with an associated SCSI command, a command block is allocated
543173153fe5SWebb Scales  * at init, and managed by cmd_tagged_alloc() and cmd_tagged_free() using the
543273153fe5SWebb Scales  * block request tag as an index into a table of entries.  cmd_tagged_free() is
543373153fe5SWebb Scales  * the complement, although cmd_free() may be called instead.
543473153fe5SWebb Scales  */
543573153fe5SWebb Scales static struct CommandList *cmd_tagged_alloc(struct ctlr_info *h,
543673153fe5SWebb Scales 					    struct scsi_cmnd *scmd)
543773153fe5SWebb Scales {
543873153fe5SWebb Scales 	int idx = hpsa_get_cmd_index(scmd);
543973153fe5SWebb Scales 	struct CommandList *c = h->cmd_pool + idx;
544073153fe5SWebb Scales 
544173153fe5SWebb Scales 	if (idx < HPSA_NRESERVED_CMDS || idx >= h->nr_cmds) {
544273153fe5SWebb Scales 		dev_err(&h->pdev->dev, "Bad block tag: %d not in [%d..%d]\n",
544373153fe5SWebb Scales 			idx, HPSA_NRESERVED_CMDS, h->nr_cmds - 1);
544473153fe5SWebb Scales 		/* The index value comes from the block layer, so if it's out of
544573153fe5SWebb Scales 		 * bounds, it's probably not our bug.
544673153fe5SWebb Scales 		 */
544773153fe5SWebb Scales 		BUG();
544873153fe5SWebb Scales 	}
544973153fe5SWebb Scales 
545073153fe5SWebb Scales 	atomic_inc(&c->refcount);
545173153fe5SWebb Scales 	if (unlikely(!hpsa_is_cmd_idle(c))) {
545273153fe5SWebb Scales 		/*
545373153fe5SWebb Scales 		 * We expect that the SCSI layer will hand us a unique tag
545473153fe5SWebb Scales 		 * value.  Thus, there should never be a collision here between
545573153fe5SWebb Scales 		 * two requests...because if the selected command isn't idle
545673153fe5SWebb Scales 		 * then someone is going to be very disappointed.
545773153fe5SWebb Scales 		 */
545873153fe5SWebb Scales 		dev_err(&h->pdev->dev,
545973153fe5SWebb Scales 			"tag collision (tag=%d) in cmd_tagged_alloc().\n",
546073153fe5SWebb Scales 			idx);
546173153fe5SWebb Scales 		if (c->scsi_cmd != NULL)
546273153fe5SWebb Scales 			scsi_print_command(c->scsi_cmd);
546373153fe5SWebb Scales 		scsi_print_command(scmd);
546473153fe5SWebb Scales 	}
546573153fe5SWebb Scales 
546673153fe5SWebb Scales 	hpsa_cmd_partial_init(h, idx, c);
546773153fe5SWebb Scales 	return c;
546873153fe5SWebb Scales }
546973153fe5SWebb Scales 
547073153fe5SWebb Scales static void cmd_tagged_free(struct ctlr_info *h, struct CommandList *c)
547173153fe5SWebb Scales {
547273153fe5SWebb Scales 	/*
547373153fe5SWebb Scales 	 * Release our reference to the block.  We don't need to do anything
547473153fe5SWebb Scales 	 * else to free it, because it is accessed by index.  (There's no point
547573153fe5SWebb Scales 	 * in checking the result of the decrement, since we cannot guarantee
547673153fe5SWebb Scales 	 * that there isn't a concurrent abort which is also accessing it.)
547773153fe5SWebb Scales 	 */
547873153fe5SWebb Scales 	(void)atomic_dec(&c->refcount);
547973153fe5SWebb Scales }
548073153fe5SWebb Scales 
548173153fe5SWebb Scales /*
5482edd16368SStephen M. Cameron  * For operations that cannot sleep, a command block is allocated at init,
5483edd16368SStephen M. Cameron  * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
5484edd16368SStephen M. Cameron  * which ones are free or in use.  Lock must be held when calling this.
5485edd16368SStephen M. Cameron  * cmd_free() is the complement.
5486bf43caf3SRobert Elliott  * This function never gives up and returns NULL.  If it hangs,
5487bf43caf3SRobert Elliott  * another thread must call cmd_free() to free some tags.
5488edd16368SStephen M. Cameron  */
5489281a7fd0SWebb Scales 
5490edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h)
5491edd16368SStephen M. Cameron {
5492edd16368SStephen M. Cameron 	struct CommandList *c;
5493360c73bdSStephen Cameron 	int refcount, i;
549473153fe5SWebb Scales 	int offset = 0;
5495edd16368SStephen M. Cameron 
549633811026SRobert Elliott 	/*
549733811026SRobert Elliott 	 * There is some *extremely* small but non-zero chance that that
54984c413128SStephen M. Cameron 	 * multiple threads could get in here, and one thread could
54994c413128SStephen M. Cameron 	 * be scanning through the list of bits looking for a free
55004c413128SStephen M. Cameron 	 * one, but the free ones are always behind him, and other
55014c413128SStephen M. Cameron 	 * threads sneak in behind him and eat them before he can
55024c413128SStephen M. Cameron 	 * get to them, so that while there is always a free one, a
55034c413128SStephen M. Cameron 	 * very unlucky thread might be starved anyway, never able to
55044c413128SStephen M. Cameron 	 * beat the other threads.  In reality, this happens so
55054c413128SStephen M. Cameron 	 * infrequently as to be indistinguishable from never.
550673153fe5SWebb Scales 	 *
550773153fe5SWebb Scales 	 * Note that we start allocating commands before the SCSI host structure
550873153fe5SWebb Scales 	 * is initialized.  Since the search starts at bit zero, this
550973153fe5SWebb Scales 	 * all works, since we have at least one command structure available;
551073153fe5SWebb Scales 	 * however, it means that the structures with the low indexes have to be
551173153fe5SWebb Scales 	 * reserved for driver-initiated requests, while requests from the block
551273153fe5SWebb Scales 	 * layer will use the higher indexes.
55134c413128SStephen M. Cameron 	 */
55144c413128SStephen M. Cameron 
5515281a7fd0SWebb Scales 	for (;;) {
551673153fe5SWebb Scales 		i = find_next_zero_bit(h->cmd_pool_bits,
551773153fe5SWebb Scales 					HPSA_NRESERVED_CMDS,
551873153fe5SWebb Scales 					offset);
551973153fe5SWebb Scales 		if (unlikely(i >= HPSA_NRESERVED_CMDS)) {
5520281a7fd0SWebb Scales 			offset = 0;
5521281a7fd0SWebb Scales 			continue;
5522281a7fd0SWebb Scales 		}
5523edd16368SStephen M. Cameron 		c = h->cmd_pool + i;
5524281a7fd0SWebb Scales 		refcount = atomic_inc_return(&c->refcount);
5525281a7fd0SWebb Scales 		if (unlikely(refcount > 1)) {
5526281a7fd0SWebb Scales 			cmd_free(h, c); /* already in use */
552773153fe5SWebb Scales 			offset = (i + 1) % HPSA_NRESERVED_CMDS;
5528281a7fd0SWebb Scales 			continue;
5529281a7fd0SWebb Scales 		}
5530281a7fd0SWebb Scales 		set_bit(i & (BITS_PER_LONG - 1),
5531281a7fd0SWebb Scales 			h->cmd_pool_bits + (i / BITS_PER_LONG));
5532281a7fd0SWebb Scales 		break; /* it's ours now. */
5533281a7fd0SWebb Scales 	}
5534360c73bdSStephen Cameron 	hpsa_cmd_partial_init(h, i, c);
5535edd16368SStephen M. Cameron 	return c;
5536edd16368SStephen M. Cameron }
5537edd16368SStephen M. Cameron 
553873153fe5SWebb Scales /*
553973153fe5SWebb Scales  * This is the complementary operation to cmd_alloc().  Note, however, in some
554073153fe5SWebb Scales  * corner cases it may also be used to free blocks allocated by
554173153fe5SWebb Scales  * cmd_tagged_alloc() in which case the ref-count decrement does the trick and
554273153fe5SWebb Scales  * the clear-bit is harmless.
554373153fe5SWebb Scales  */
5544edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c)
5545edd16368SStephen M. Cameron {
5546281a7fd0SWebb Scales 	if (atomic_dec_and_test(&c->refcount)) {
5547edd16368SStephen M. Cameron 		int i;
5548edd16368SStephen M. Cameron 
5549edd16368SStephen M. Cameron 		i = c - h->cmd_pool;
5550edd16368SStephen M. Cameron 		clear_bit(i & (BITS_PER_LONG - 1),
5551edd16368SStephen M. Cameron 			  h->cmd_pool_bits + (i / BITS_PER_LONG));
5552edd16368SStephen M. Cameron 	}
5553281a7fd0SWebb Scales }
5554edd16368SStephen M. Cameron 
5555edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
5556edd16368SStephen M. Cameron 
555742a91641SDon Brace static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd,
555842a91641SDon Brace 	void __user *arg)
5559edd16368SStephen M. Cameron {
5560edd16368SStephen M. Cameron 	IOCTL32_Command_struct __user *arg32 =
5561edd16368SStephen M. Cameron 	    (IOCTL32_Command_struct __user *) arg;
5562edd16368SStephen M. Cameron 	IOCTL_Command_struct arg64;
5563edd16368SStephen M. Cameron 	IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
5564edd16368SStephen M. Cameron 	int err;
5565edd16368SStephen M. Cameron 	u32 cp;
5566edd16368SStephen M. Cameron 
5567938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
5568edd16368SStephen M. Cameron 	err = 0;
5569edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
5570edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
5571edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
5572edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
5573edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
5574edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
5575edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
5576edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
5577edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
5578edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
5579edd16368SStephen M. Cameron 
5580edd16368SStephen M. Cameron 	if (err)
5581edd16368SStephen M. Cameron 		return -EFAULT;
5582edd16368SStephen M. Cameron 
558342a91641SDon Brace 	err = hpsa_ioctl(dev, CCISS_PASSTHRU, p);
5584edd16368SStephen M. Cameron 	if (err)
5585edd16368SStephen M. Cameron 		return err;
5586edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
5587edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
5588edd16368SStephen M. Cameron 	if (err)
5589edd16368SStephen M. Cameron 		return -EFAULT;
5590edd16368SStephen M. Cameron 	return err;
5591edd16368SStephen M. Cameron }
5592edd16368SStephen M. Cameron 
5593edd16368SStephen M. Cameron static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
559442a91641SDon Brace 	int cmd, void __user *arg)
5595edd16368SStephen M. Cameron {
5596edd16368SStephen M. Cameron 	BIG_IOCTL32_Command_struct __user *arg32 =
5597edd16368SStephen M. Cameron 	    (BIG_IOCTL32_Command_struct __user *) arg;
5598edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct arg64;
5599edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct __user *p =
5600edd16368SStephen M. Cameron 	    compat_alloc_user_space(sizeof(arg64));
5601edd16368SStephen M. Cameron 	int err;
5602edd16368SStephen M. Cameron 	u32 cp;
5603edd16368SStephen M. Cameron 
5604938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
5605edd16368SStephen M. Cameron 	err = 0;
5606edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
5607edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
5608edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
5609edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
5610edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
5611edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
5612edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
5613edd16368SStephen M. Cameron 	err |= get_user(arg64.malloc_size, &arg32->malloc_size);
5614edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
5615edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
5616edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
5617edd16368SStephen M. Cameron 
5618edd16368SStephen M. Cameron 	if (err)
5619edd16368SStephen M. Cameron 		return -EFAULT;
5620edd16368SStephen M. Cameron 
562142a91641SDon Brace 	err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, p);
5622edd16368SStephen M. Cameron 	if (err)
5623edd16368SStephen M. Cameron 		return err;
5624edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
5625edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
5626edd16368SStephen M. Cameron 	if (err)
5627edd16368SStephen M. Cameron 		return -EFAULT;
5628edd16368SStephen M. Cameron 	return err;
5629edd16368SStephen M. Cameron }
563071fe75a7SStephen M. Cameron 
563142a91641SDon Brace static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
563271fe75a7SStephen M. Cameron {
563371fe75a7SStephen M. Cameron 	switch (cmd) {
563471fe75a7SStephen M. Cameron 	case CCISS_GETPCIINFO:
563571fe75a7SStephen M. Cameron 	case CCISS_GETINTINFO:
563671fe75a7SStephen M. Cameron 	case CCISS_SETINTINFO:
563771fe75a7SStephen M. Cameron 	case CCISS_GETNODENAME:
563871fe75a7SStephen M. Cameron 	case CCISS_SETNODENAME:
563971fe75a7SStephen M. Cameron 	case CCISS_GETHEARTBEAT:
564071fe75a7SStephen M. Cameron 	case CCISS_GETBUSTYPES:
564171fe75a7SStephen M. Cameron 	case CCISS_GETFIRMVER:
564271fe75a7SStephen M. Cameron 	case CCISS_GETDRIVVER:
564371fe75a7SStephen M. Cameron 	case CCISS_REVALIDVOLS:
564471fe75a7SStephen M. Cameron 	case CCISS_DEREGDISK:
564571fe75a7SStephen M. Cameron 	case CCISS_REGNEWDISK:
564671fe75a7SStephen M. Cameron 	case CCISS_REGNEWD:
564771fe75a7SStephen M. Cameron 	case CCISS_RESCANDISK:
564871fe75a7SStephen M. Cameron 	case CCISS_GETLUNINFO:
564971fe75a7SStephen M. Cameron 		return hpsa_ioctl(dev, cmd, arg);
565071fe75a7SStephen M. Cameron 
565171fe75a7SStephen M. Cameron 	case CCISS_PASSTHRU32:
565271fe75a7SStephen M. Cameron 		return hpsa_ioctl32_passthru(dev, cmd, arg);
565371fe75a7SStephen M. Cameron 	case CCISS_BIG_PASSTHRU32:
565471fe75a7SStephen M. Cameron 		return hpsa_ioctl32_big_passthru(dev, cmd, arg);
565571fe75a7SStephen M. Cameron 
565671fe75a7SStephen M. Cameron 	default:
565771fe75a7SStephen M. Cameron 		return -ENOIOCTLCMD;
565871fe75a7SStephen M. Cameron 	}
565971fe75a7SStephen M. Cameron }
5660edd16368SStephen M. Cameron #endif
5661edd16368SStephen M. Cameron 
5662edd16368SStephen M. Cameron static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
5663edd16368SStephen M. Cameron {
5664edd16368SStephen M. Cameron 	struct hpsa_pci_info pciinfo;
5665edd16368SStephen M. Cameron 
5666edd16368SStephen M. Cameron 	if (!argp)
5667edd16368SStephen M. Cameron 		return -EINVAL;
5668edd16368SStephen M. Cameron 	pciinfo.domain = pci_domain_nr(h->pdev->bus);
5669edd16368SStephen M. Cameron 	pciinfo.bus = h->pdev->bus->number;
5670edd16368SStephen M. Cameron 	pciinfo.dev_fn = h->pdev->devfn;
5671edd16368SStephen M. Cameron 	pciinfo.board_id = h->board_id;
5672edd16368SStephen M. Cameron 	if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
5673edd16368SStephen M. Cameron 		return -EFAULT;
5674edd16368SStephen M. Cameron 	return 0;
5675edd16368SStephen M. Cameron }
5676edd16368SStephen M. Cameron 
5677edd16368SStephen M. Cameron static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
5678edd16368SStephen M. Cameron {
5679edd16368SStephen M. Cameron 	DriverVer_type DriverVer;
5680edd16368SStephen M. Cameron 	unsigned char vmaj, vmin, vsubmin;
5681edd16368SStephen M. Cameron 	int rc;
5682edd16368SStephen M. Cameron 
5683edd16368SStephen M. Cameron 	rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
5684edd16368SStephen M. Cameron 		&vmaj, &vmin, &vsubmin);
5685edd16368SStephen M. Cameron 	if (rc != 3) {
5686edd16368SStephen M. Cameron 		dev_info(&h->pdev->dev, "driver version string '%s' "
5687edd16368SStephen M. Cameron 			"unrecognized.", HPSA_DRIVER_VERSION);
5688edd16368SStephen M. Cameron 		vmaj = 0;
5689edd16368SStephen M. Cameron 		vmin = 0;
5690edd16368SStephen M. Cameron 		vsubmin = 0;
5691edd16368SStephen M. Cameron 	}
5692edd16368SStephen M. Cameron 	DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
5693edd16368SStephen M. Cameron 	if (!argp)
5694edd16368SStephen M. Cameron 		return -EINVAL;
5695edd16368SStephen M. Cameron 	if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
5696edd16368SStephen M. Cameron 		return -EFAULT;
5697edd16368SStephen M. Cameron 	return 0;
5698edd16368SStephen M. Cameron }
5699edd16368SStephen M. Cameron 
5700edd16368SStephen M. Cameron static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
5701edd16368SStephen M. Cameron {
5702edd16368SStephen M. Cameron 	IOCTL_Command_struct iocommand;
5703edd16368SStephen M. Cameron 	struct CommandList *c;
5704edd16368SStephen M. Cameron 	char *buff = NULL;
570550a0decfSStephen M. Cameron 	u64 temp64;
5706c1f63c8fSStephen M. Cameron 	int rc = 0;
5707edd16368SStephen M. Cameron 
5708edd16368SStephen M. Cameron 	if (!argp)
5709edd16368SStephen M. Cameron 		return -EINVAL;
5710edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
5711edd16368SStephen M. Cameron 		return -EPERM;
5712edd16368SStephen M. Cameron 	if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
5713edd16368SStephen M. Cameron 		return -EFAULT;
5714edd16368SStephen M. Cameron 	if ((iocommand.buf_size < 1) &&
5715edd16368SStephen M. Cameron 	    (iocommand.Request.Type.Direction != XFER_NONE)) {
5716edd16368SStephen M. Cameron 		return -EINVAL;
5717edd16368SStephen M. Cameron 	}
5718edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
5719edd16368SStephen M. Cameron 		buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
5720edd16368SStephen M. Cameron 		if (buff == NULL)
57212dd02d74SRobert Elliott 			return -ENOMEM;
57229233fb10SStephen M. Cameron 		if (iocommand.Request.Type.Direction & XFER_WRITE) {
5723edd16368SStephen M. Cameron 			/* Copy the data into the buffer we created */
5724b03a7771SStephen M. Cameron 			if (copy_from_user(buff, iocommand.buf,
5725b03a7771SStephen M. Cameron 				iocommand.buf_size)) {
5726c1f63c8fSStephen M. Cameron 				rc = -EFAULT;
5727c1f63c8fSStephen M. Cameron 				goto out_kfree;
5728edd16368SStephen M. Cameron 			}
5729b03a7771SStephen M. Cameron 		} else {
5730edd16368SStephen M. Cameron 			memset(buff, 0, iocommand.buf_size);
5731b03a7771SStephen M. Cameron 		}
5732b03a7771SStephen M. Cameron 	}
573345fcb86eSStephen Cameron 	c = cmd_alloc(h);
5734bf43caf3SRobert Elliott 
5735edd16368SStephen M. Cameron 	/* Fill in the command type */
5736edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5737a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_BUSY;
5738edd16368SStephen M. Cameron 	/* Fill in Command Header */
5739edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0; /* unused in simple mode */
5740edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {	/* buffer to fill */
5741edd16368SStephen M. Cameron 		c->Header.SGList = 1;
574250a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(1);
5743edd16368SStephen M. Cameron 	} else	{ /* no buffers to fill */
5744edd16368SStephen M. Cameron 		c->Header.SGList = 0;
574550a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(0);
5746edd16368SStephen M. Cameron 	}
5747edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
5748edd16368SStephen M. Cameron 
5749edd16368SStephen M. Cameron 	/* Fill in Request block */
5750edd16368SStephen M. Cameron 	memcpy(&c->Request, &iocommand.Request,
5751edd16368SStephen M. Cameron 		sizeof(c->Request));
5752edd16368SStephen M. Cameron 
5753edd16368SStephen M. Cameron 	/* Fill in the scatter gather information */
5754edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
575550a0decfSStephen M. Cameron 		temp64 = pci_map_single(h->pdev, buff,
5756edd16368SStephen M. Cameron 			iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
575750a0decfSStephen M. Cameron 		if (dma_mapping_error(&h->pdev->dev, (dma_addr_t) temp64)) {
575850a0decfSStephen M. Cameron 			c->SG[0].Addr = cpu_to_le64(0);
575950a0decfSStephen M. Cameron 			c->SG[0].Len = cpu_to_le32(0);
5760bcc48ffaSStephen M. Cameron 			rc = -ENOMEM;
5761bcc48ffaSStephen M. Cameron 			goto out;
5762bcc48ffaSStephen M. Cameron 		}
576350a0decfSStephen M. Cameron 		c->SG[0].Addr = cpu_to_le64(temp64);
576450a0decfSStephen M. Cameron 		c->SG[0].Len = cpu_to_le32(iocommand.buf_size);
576550a0decfSStephen M. Cameron 		c->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* not chaining */
5766edd16368SStephen M. Cameron 	}
576725163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
5768c2dd32e0SStephen M. Cameron 	if (iocommand.buf_size > 0)
5769edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
5770edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
577125163bd5SWebb Scales 	if (rc) {
577225163bd5SWebb Scales 		rc = -EIO;
577325163bd5SWebb Scales 		goto out;
577425163bd5SWebb Scales 	}
5775edd16368SStephen M. Cameron 
5776edd16368SStephen M. Cameron 	/* Copy the error information out */
5777edd16368SStephen M. Cameron 	memcpy(&iocommand.error_info, c->err_info,
5778edd16368SStephen M. Cameron 		sizeof(iocommand.error_info));
5779edd16368SStephen M. Cameron 	if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
5780c1f63c8fSStephen M. Cameron 		rc = -EFAULT;
5781c1f63c8fSStephen M. Cameron 		goto out;
5782edd16368SStephen M. Cameron 	}
57839233fb10SStephen M. Cameron 	if ((iocommand.Request.Type.Direction & XFER_READ) &&
5784b03a7771SStephen M. Cameron 		iocommand.buf_size > 0) {
5785edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
5786edd16368SStephen M. Cameron 		if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
5787c1f63c8fSStephen M. Cameron 			rc = -EFAULT;
5788c1f63c8fSStephen M. Cameron 			goto out;
5789edd16368SStephen M. Cameron 		}
5790edd16368SStephen M. Cameron 	}
5791c1f63c8fSStephen M. Cameron out:
579245fcb86eSStephen Cameron 	cmd_free(h, c);
5793c1f63c8fSStephen M. Cameron out_kfree:
5794c1f63c8fSStephen M. Cameron 	kfree(buff);
5795c1f63c8fSStephen M. Cameron 	return rc;
5796edd16368SStephen M. Cameron }
5797edd16368SStephen M. Cameron 
5798edd16368SStephen M. Cameron static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
5799edd16368SStephen M. Cameron {
5800edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct *ioc;
5801edd16368SStephen M. Cameron 	struct CommandList *c;
5802edd16368SStephen M. Cameron 	unsigned char **buff = NULL;
5803edd16368SStephen M. Cameron 	int *buff_size = NULL;
580450a0decfSStephen M. Cameron 	u64 temp64;
5805edd16368SStephen M. Cameron 	BYTE sg_used = 0;
5806edd16368SStephen M. Cameron 	int status = 0;
580701a02ffcSStephen M. Cameron 	u32 left;
580801a02ffcSStephen M. Cameron 	u32 sz;
5809edd16368SStephen M. Cameron 	BYTE __user *data_ptr;
5810edd16368SStephen M. Cameron 
5811edd16368SStephen M. Cameron 	if (!argp)
5812edd16368SStephen M. Cameron 		return -EINVAL;
5813edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
5814edd16368SStephen M. Cameron 		return -EPERM;
5815edd16368SStephen M. Cameron 	ioc = (BIG_IOCTL_Command_struct *)
5816edd16368SStephen M. Cameron 	    kmalloc(sizeof(*ioc), GFP_KERNEL);
5817edd16368SStephen M. Cameron 	if (!ioc) {
5818edd16368SStephen M. Cameron 		status = -ENOMEM;
5819edd16368SStephen M. Cameron 		goto cleanup1;
5820edd16368SStephen M. Cameron 	}
5821edd16368SStephen M. Cameron 	if (copy_from_user(ioc, argp, sizeof(*ioc))) {
5822edd16368SStephen M. Cameron 		status = -EFAULT;
5823edd16368SStephen M. Cameron 		goto cleanup1;
5824edd16368SStephen M. Cameron 	}
5825edd16368SStephen M. Cameron 	if ((ioc->buf_size < 1) &&
5826edd16368SStephen M. Cameron 	    (ioc->Request.Type.Direction != XFER_NONE)) {
5827edd16368SStephen M. Cameron 		status = -EINVAL;
5828edd16368SStephen M. Cameron 		goto cleanup1;
5829edd16368SStephen M. Cameron 	}
5830edd16368SStephen M. Cameron 	/* Check kmalloc limits  using all SGs */
5831edd16368SStephen M. Cameron 	if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
5832edd16368SStephen M. Cameron 		status = -EINVAL;
5833edd16368SStephen M. Cameron 		goto cleanup1;
5834edd16368SStephen M. Cameron 	}
5835d66ae08bSStephen M. Cameron 	if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
5836edd16368SStephen M. Cameron 		status = -EINVAL;
5837edd16368SStephen M. Cameron 		goto cleanup1;
5838edd16368SStephen M. Cameron 	}
5839d66ae08bSStephen M. Cameron 	buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
5840edd16368SStephen M. Cameron 	if (!buff) {
5841edd16368SStephen M. Cameron 		status = -ENOMEM;
5842edd16368SStephen M. Cameron 		goto cleanup1;
5843edd16368SStephen M. Cameron 	}
5844d66ae08bSStephen M. Cameron 	buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
5845edd16368SStephen M. Cameron 	if (!buff_size) {
5846edd16368SStephen M. Cameron 		status = -ENOMEM;
5847edd16368SStephen M. Cameron 		goto cleanup1;
5848edd16368SStephen M. Cameron 	}
5849edd16368SStephen M. Cameron 	left = ioc->buf_size;
5850edd16368SStephen M. Cameron 	data_ptr = ioc->buf;
5851edd16368SStephen M. Cameron 	while (left) {
5852edd16368SStephen M. Cameron 		sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
5853edd16368SStephen M. Cameron 		buff_size[sg_used] = sz;
5854edd16368SStephen M. Cameron 		buff[sg_used] = kmalloc(sz, GFP_KERNEL);
5855edd16368SStephen M. Cameron 		if (buff[sg_used] == NULL) {
5856edd16368SStephen M. Cameron 			status = -ENOMEM;
5857edd16368SStephen M. Cameron 			goto cleanup1;
5858edd16368SStephen M. Cameron 		}
58599233fb10SStephen M. Cameron 		if (ioc->Request.Type.Direction & XFER_WRITE) {
5860edd16368SStephen M. Cameron 			if (copy_from_user(buff[sg_used], data_ptr, sz)) {
58610758f4f7SStephen M. Cameron 				status = -EFAULT;
5862edd16368SStephen M. Cameron 				goto cleanup1;
5863edd16368SStephen M. Cameron 			}
5864edd16368SStephen M. Cameron 		} else
5865edd16368SStephen M. Cameron 			memset(buff[sg_used], 0, sz);
5866edd16368SStephen M. Cameron 		left -= sz;
5867edd16368SStephen M. Cameron 		data_ptr += sz;
5868edd16368SStephen M. Cameron 		sg_used++;
5869edd16368SStephen M. Cameron 	}
587045fcb86eSStephen Cameron 	c = cmd_alloc(h);
5871bf43caf3SRobert Elliott 
5872edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5873a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_BUSY;
5874edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
587550a0decfSStephen M. Cameron 	c->Header.SGList = (u8) sg_used;
587650a0decfSStephen M. Cameron 	c->Header.SGTotal = cpu_to_le16(sg_used);
5877edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
5878edd16368SStephen M. Cameron 	memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
5879edd16368SStephen M. Cameron 	if (ioc->buf_size > 0) {
5880edd16368SStephen M. Cameron 		int i;
5881edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
588250a0decfSStephen M. Cameron 			temp64 = pci_map_single(h->pdev, buff[i],
5883edd16368SStephen M. Cameron 				    buff_size[i], PCI_DMA_BIDIRECTIONAL);
588450a0decfSStephen M. Cameron 			if (dma_mapping_error(&h->pdev->dev,
588550a0decfSStephen M. Cameron 							(dma_addr_t) temp64)) {
588650a0decfSStephen M. Cameron 				c->SG[i].Addr = cpu_to_le64(0);
588750a0decfSStephen M. Cameron 				c->SG[i].Len = cpu_to_le32(0);
5888bcc48ffaSStephen M. Cameron 				hpsa_pci_unmap(h->pdev, c, i,
5889bcc48ffaSStephen M. Cameron 					PCI_DMA_BIDIRECTIONAL);
5890bcc48ffaSStephen M. Cameron 				status = -ENOMEM;
5891e2d4a1f6SStephen M. Cameron 				goto cleanup0;
5892bcc48ffaSStephen M. Cameron 			}
589350a0decfSStephen M. Cameron 			c->SG[i].Addr = cpu_to_le64(temp64);
589450a0decfSStephen M. Cameron 			c->SG[i].Len = cpu_to_le32(buff_size[i]);
589550a0decfSStephen M. Cameron 			c->SG[i].Ext = cpu_to_le32(0);
5896edd16368SStephen M. Cameron 		}
589750a0decfSStephen M. Cameron 		c->SG[--i].Ext = cpu_to_le32(HPSA_SG_LAST);
5898edd16368SStephen M. Cameron 	}
589925163bd5SWebb Scales 	status = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
5900b03a7771SStephen M. Cameron 	if (sg_used)
5901edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
5902edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
590325163bd5SWebb Scales 	if (status) {
590425163bd5SWebb Scales 		status = -EIO;
590525163bd5SWebb Scales 		goto cleanup0;
590625163bd5SWebb Scales 	}
590725163bd5SWebb Scales 
5908edd16368SStephen M. Cameron 	/* Copy the error information out */
5909edd16368SStephen M. Cameron 	memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
5910edd16368SStephen M. Cameron 	if (copy_to_user(argp, ioc, sizeof(*ioc))) {
5911edd16368SStephen M. Cameron 		status = -EFAULT;
5912e2d4a1f6SStephen M. Cameron 		goto cleanup0;
5913edd16368SStephen M. Cameron 	}
59149233fb10SStephen M. Cameron 	if ((ioc->Request.Type.Direction & XFER_READ) && ioc->buf_size > 0) {
59152b08b3e9SDon Brace 		int i;
59162b08b3e9SDon Brace 
5917edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
5918edd16368SStephen M. Cameron 		BYTE __user *ptr = ioc->buf;
5919edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
5920edd16368SStephen M. Cameron 			if (copy_to_user(ptr, buff[i], buff_size[i])) {
5921edd16368SStephen M. Cameron 				status = -EFAULT;
5922e2d4a1f6SStephen M. Cameron 				goto cleanup0;
5923edd16368SStephen M. Cameron 			}
5924edd16368SStephen M. Cameron 			ptr += buff_size[i];
5925edd16368SStephen M. Cameron 		}
5926edd16368SStephen M. Cameron 	}
5927edd16368SStephen M. Cameron 	status = 0;
5928e2d4a1f6SStephen M. Cameron cleanup0:
592945fcb86eSStephen Cameron 	cmd_free(h, c);
5930edd16368SStephen M. Cameron cleanup1:
5931edd16368SStephen M. Cameron 	if (buff) {
59322b08b3e9SDon Brace 		int i;
59332b08b3e9SDon Brace 
5934edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++)
5935edd16368SStephen M. Cameron 			kfree(buff[i]);
5936edd16368SStephen M. Cameron 		kfree(buff);
5937edd16368SStephen M. Cameron 	}
5938edd16368SStephen M. Cameron 	kfree(buff_size);
5939edd16368SStephen M. Cameron 	kfree(ioc);
5940edd16368SStephen M. Cameron 	return status;
5941edd16368SStephen M. Cameron }
5942edd16368SStephen M. Cameron 
5943edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
5944edd16368SStephen M. Cameron 	struct CommandList *c)
5945edd16368SStephen M. Cameron {
5946edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
5947edd16368SStephen M. Cameron 			c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
5948edd16368SStephen M. Cameron 		(void) check_for_unit_attention(h, c);
5949edd16368SStephen M. Cameron }
59500390f0c0SStephen M. Cameron 
5951edd16368SStephen M. Cameron /*
5952edd16368SStephen M. Cameron  * ioctl
5953edd16368SStephen M. Cameron  */
595442a91641SDon Brace static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
5955edd16368SStephen M. Cameron {
5956edd16368SStephen M. Cameron 	struct ctlr_info *h;
5957edd16368SStephen M. Cameron 	void __user *argp = (void __user *)arg;
59580390f0c0SStephen M. Cameron 	int rc;
5959edd16368SStephen M. Cameron 
5960edd16368SStephen M. Cameron 	h = sdev_to_hba(dev);
5961edd16368SStephen M. Cameron 
5962edd16368SStephen M. Cameron 	switch (cmd) {
5963edd16368SStephen M. Cameron 	case CCISS_DEREGDISK:
5964edd16368SStephen M. Cameron 	case CCISS_REGNEWDISK:
5965edd16368SStephen M. Cameron 	case CCISS_REGNEWD:
5966a08a8471SStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
5967edd16368SStephen M. Cameron 		return 0;
5968edd16368SStephen M. Cameron 	case CCISS_GETPCIINFO:
5969edd16368SStephen M. Cameron 		return hpsa_getpciinfo_ioctl(h, argp);
5970edd16368SStephen M. Cameron 	case CCISS_GETDRIVVER:
5971edd16368SStephen M. Cameron 		return hpsa_getdrivver_ioctl(h, argp);
5972edd16368SStephen M. Cameron 	case CCISS_PASSTHRU:
597334f0c627SDon Brace 		if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
59740390f0c0SStephen M. Cameron 			return -EAGAIN;
59750390f0c0SStephen M. Cameron 		rc = hpsa_passthru_ioctl(h, argp);
597634f0c627SDon Brace 		atomic_inc(&h->passthru_cmds_avail);
59770390f0c0SStephen M. Cameron 		return rc;
5978edd16368SStephen M. Cameron 	case CCISS_BIG_PASSTHRU:
597934f0c627SDon Brace 		if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
59800390f0c0SStephen M. Cameron 			return -EAGAIN;
59810390f0c0SStephen M. Cameron 		rc = hpsa_big_passthru_ioctl(h, argp);
598234f0c627SDon Brace 		atomic_inc(&h->passthru_cmds_avail);
59830390f0c0SStephen M. Cameron 		return rc;
5984edd16368SStephen M. Cameron 	default:
5985edd16368SStephen M. Cameron 		return -ENOTTY;
5986edd16368SStephen M. Cameron 	}
5987edd16368SStephen M. Cameron }
5988edd16368SStephen M. Cameron 
5989bf43caf3SRobert Elliott static void hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
59906f039790SGreg Kroah-Hartman 				u8 reset_type)
599164670ac8SStephen M. Cameron {
599264670ac8SStephen M. Cameron 	struct CommandList *c;
599364670ac8SStephen M. Cameron 
599464670ac8SStephen M. Cameron 	c = cmd_alloc(h);
5995bf43caf3SRobert Elliott 
5996a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map */
5997a2dac136SStephen M. Cameron 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
599864670ac8SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_MSG);
599964670ac8SStephen M. Cameron 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
600064670ac8SStephen M. Cameron 	c->waiting = NULL;
600164670ac8SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
600264670ac8SStephen M. Cameron 	/* Don't wait for completion, the reset won't complete.  Don't free
600364670ac8SStephen M. Cameron 	 * the command either.  This is the last command we will send before
600464670ac8SStephen M. Cameron 	 * re-initializing everything, so it doesn't matter and won't leak.
600564670ac8SStephen M. Cameron 	 */
6006bf43caf3SRobert Elliott 	return;
600764670ac8SStephen M. Cameron }
600864670ac8SStephen M. Cameron 
6009a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
6010b7bb24ebSStephen M. Cameron 	void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
6011edd16368SStephen M. Cameron 	int cmd_type)
6012edd16368SStephen M. Cameron {
6013edd16368SStephen M. Cameron 	int pci_dir = XFER_NONE;
60149b5c48c2SStephen Cameron 	u64 tag; /* for commands to be aborted */
6015edd16368SStephen M. Cameron 
6016edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
6017a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_BUSY;
6018edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
6019edd16368SStephen M. Cameron 	if (buff != NULL && size > 0) {
6020edd16368SStephen M. Cameron 		c->Header.SGList = 1;
602150a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(1);
6022edd16368SStephen M. Cameron 	} else {
6023edd16368SStephen M. Cameron 		c->Header.SGList = 0;
602450a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(0);
6025edd16368SStephen M. Cameron 	}
6026edd16368SStephen M. Cameron 	memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
6027edd16368SStephen M. Cameron 
6028edd16368SStephen M. Cameron 	if (cmd_type == TYPE_CMD) {
6029edd16368SStephen M. Cameron 		switch (cmd) {
6030edd16368SStephen M. Cameron 		case HPSA_INQUIRY:
6031edd16368SStephen M. Cameron 			/* are we trying to read a vital product page */
6032b7bb24ebSStephen M. Cameron 			if (page_code & VPD_PAGE) {
6033edd16368SStephen M. Cameron 				c->Request.CDB[1] = 0x01;
6034b7bb24ebSStephen M. Cameron 				c->Request.CDB[2] = (page_code & 0xff);
6035edd16368SStephen M. Cameron 			}
6036edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
6037a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6038a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6039edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
6040edd16368SStephen M. Cameron 			c->Request.CDB[0] = HPSA_INQUIRY;
6041edd16368SStephen M. Cameron 			c->Request.CDB[4] = size & 0xFF;
6042edd16368SStephen M. Cameron 			break;
6043edd16368SStephen M. Cameron 		case HPSA_REPORT_LOG:
6044edd16368SStephen M. Cameron 		case HPSA_REPORT_PHYS:
6045edd16368SStephen M. Cameron 			/* Talking to controller so It's a physical command
6046edd16368SStephen M. Cameron 			   mode = 00 target = 0.  Nothing to write.
6047edd16368SStephen M. Cameron 			 */
6048edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
6049a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6050a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6051edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
6052edd16368SStephen M. Cameron 			c->Request.CDB[0] = cmd;
6053edd16368SStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
6054edd16368SStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
6055edd16368SStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
6056edd16368SStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
6057edd16368SStephen M. Cameron 			break;
6058edd16368SStephen M. Cameron 		case HPSA_CACHE_FLUSH:
6059edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
6060a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6061a505b86fSStephen M. Cameron 					TYPE_ATTR_DIR(cmd_type,
6062a505b86fSStephen M. Cameron 						ATTR_SIMPLE, XFER_WRITE);
6063edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
6064edd16368SStephen M. Cameron 			c->Request.CDB[0] = BMIC_WRITE;
6065edd16368SStephen M. Cameron 			c->Request.CDB[6] = BMIC_CACHE_FLUSH;
6066bb158eabSStephen M. Cameron 			c->Request.CDB[7] = (size >> 8) & 0xFF;
6067bb158eabSStephen M. Cameron 			c->Request.CDB[8] = size & 0xFF;
6068edd16368SStephen M. Cameron 			break;
6069edd16368SStephen M. Cameron 		case TEST_UNIT_READY:
6070edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
6071a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6072a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
6073edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
6074edd16368SStephen M. Cameron 			break;
6075283b4a9bSStephen M. Cameron 		case HPSA_GET_RAID_MAP:
6076283b4a9bSStephen M. Cameron 			c->Request.CDBLen = 12;
6077a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6078a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6079283b4a9bSStephen M. Cameron 			c->Request.Timeout = 0;
6080283b4a9bSStephen M. Cameron 			c->Request.CDB[0] = HPSA_CISS_READ;
6081283b4a9bSStephen M. Cameron 			c->Request.CDB[1] = cmd;
6082283b4a9bSStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
6083283b4a9bSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
6084283b4a9bSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
6085283b4a9bSStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
6086283b4a9bSStephen M. Cameron 			break;
6087316b221aSStephen M. Cameron 		case BMIC_SENSE_CONTROLLER_PARAMETERS:
6088316b221aSStephen M. Cameron 			c->Request.CDBLen = 10;
6089a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6090a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6091316b221aSStephen M. Cameron 			c->Request.Timeout = 0;
6092316b221aSStephen M. Cameron 			c->Request.CDB[0] = BMIC_READ;
6093316b221aSStephen M. Cameron 			c->Request.CDB[6] = BMIC_SENSE_CONTROLLER_PARAMETERS;
6094316b221aSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
6095316b221aSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
6096316b221aSStephen M. Cameron 			break;
609703383736SDon Brace 		case BMIC_IDENTIFY_PHYSICAL_DEVICE:
609803383736SDon Brace 			c->Request.CDBLen = 10;
609903383736SDon Brace 			c->Request.type_attr_dir =
610003383736SDon Brace 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
610103383736SDon Brace 			c->Request.Timeout = 0;
610203383736SDon Brace 			c->Request.CDB[0] = BMIC_READ;
610303383736SDon Brace 			c->Request.CDB[6] = BMIC_IDENTIFY_PHYSICAL_DEVICE;
610403383736SDon Brace 			c->Request.CDB[7] = (size >> 16) & 0xFF;
610503383736SDon Brace 			c->Request.CDB[8] = (size >> 8) & 0XFF;
610603383736SDon Brace 			break;
6107edd16368SStephen M. Cameron 		default:
6108edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
6109edd16368SStephen M. Cameron 			BUG();
6110a2dac136SStephen M. Cameron 			return -1;
6111edd16368SStephen M. Cameron 		}
6112edd16368SStephen M. Cameron 	} else if (cmd_type == TYPE_MSG) {
6113edd16368SStephen M. Cameron 		switch (cmd) {
6114edd16368SStephen M. Cameron 
6115edd16368SStephen M. Cameron 		case  HPSA_DEVICE_RESET_MSG:
6116edd16368SStephen M. Cameron 			c->Request.CDBLen = 16;
6117a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6118a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
6119edd16368SStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
612064670ac8SStephen M. Cameron 			memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
612164670ac8SStephen M. Cameron 			c->Request.CDB[0] =  cmd;
612221e89afdSStephen M. Cameron 			c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
6123edd16368SStephen M. Cameron 			/* If bytes 4-7 are zero, it means reset the */
6124edd16368SStephen M. Cameron 			/* LunID device */
6125edd16368SStephen M. Cameron 			c->Request.CDB[4] = 0x00;
6126edd16368SStephen M. Cameron 			c->Request.CDB[5] = 0x00;
6127edd16368SStephen M. Cameron 			c->Request.CDB[6] = 0x00;
6128edd16368SStephen M. Cameron 			c->Request.CDB[7] = 0x00;
6129edd16368SStephen M. Cameron 			break;
613075167d2cSStephen M. Cameron 		case  HPSA_ABORT_MSG:
61319b5c48c2SStephen Cameron 			memcpy(&tag, buff, sizeof(tag));
61322b08b3e9SDon Brace 			dev_dbg(&h->pdev->dev,
61339b5c48c2SStephen Cameron 				"Abort Tag:0x%016llx using rqst Tag:0x%016llx",
61349b5c48c2SStephen Cameron 				tag, c->Header.tag);
613575167d2cSStephen M. Cameron 			c->Request.CDBLen = 16;
6136a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6137a505b86fSStephen M. Cameron 					TYPE_ATTR_DIR(cmd_type,
6138a505b86fSStephen M. Cameron 						ATTR_SIMPLE, XFER_WRITE);
613975167d2cSStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
614075167d2cSStephen M. Cameron 			c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
614175167d2cSStephen M. Cameron 			c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
614275167d2cSStephen M. Cameron 			c->Request.CDB[2] = 0x00; /* reserved */
614375167d2cSStephen M. Cameron 			c->Request.CDB[3] = 0x00; /* reserved */
614475167d2cSStephen M. Cameron 			/* Tag to abort goes in CDB[4]-CDB[11] */
61459b5c48c2SStephen Cameron 			memcpy(&c->Request.CDB[4], &tag, sizeof(tag));
614675167d2cSStephen M. Cameron 			c->Request.CDB[12] = 0x00; /* reserved */
614775167d2cSStephen M. Cameron 			c->Request.CDB[13] = 0x00; /* reserved */
614875167d2cSStephen M. Cameron 			c->Request.CDB[14] = 0x00; /* reserved */
614975167d2cSStephen M. Cameron 			c->Request.CDB[15] = 0x00; /* reserved */
615075167d2cSStephen M. Cameron 		break;
6151edd16368SStephen M. Cameron 		default:
6152edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown message type %d\n",
6153edd16368SStephen M. Cameron 				cmd);
6154edd16368SStephen M. Cameron 			BUG();
6155edd16368SStephen M. Cameron 		}
6156edd16368SStephen M. Cameron 	} else {
6157edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
6158edd16368SStephen M. Cameron 		BUG();
6159edd16368SStephen M. Cameron 	}
6160edd16368SStephen M. Cameron 
6161a505b86fSStephen M. Cameron 	switch (GET_DIR(c->Request.type_attr_dir)) {
6162edd16368SStephen M. Cameron 	case XFER_READ:
6163edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_FROMDEVICE;
6164edd16368SStephen M. Cameron 		break;
6165edd16368SStephen M. Cameron 	case XFER_WRITE:
6166edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_TODEVICE;
6167edd16368SStephen M. Cameron 		break;
6168edd16368SStephen M. Cameron 	case XFER_NONE:
6169edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_NONE;
6170edd16368SStephen M. Cameron 		break;
6171edd16368SStephen M. Cameron 	default:
6172edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_BIDIRECTIONAL;
6173edd16368SStephen M. Cameron 	}
6174a2dac136SStephen M. Cameron 	if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
6175a2dac136SStephen M. Cameron 		return -1;
6176a2dac136SStephen M. Cameron 	return 0;
6177edd16368SStephen M. Cameron }
6178edd16368SStephen M. Cameron 
6179edd16368SStephen M. Cameron /*
6180edd16368SStephen M. Cameron  * Map (physical) PCI mem into (virtual) kernel space
6181edd16368SStephen M. Cameron  */
6182edd16368SStephen M. Cameron static void __iomem *remap_pci_mem(ulong base, ulong size)
6183edd16368SStephen M. Cameron {
6184edd16368SStephen M. Cameron 	ulong page_base = ((ulong) base) & PAGE_MASK;
6185edd16368SStephen M. Cameron 	ulong page_offs = ((ulong) base) - page_base;
6186088ba34cSStephen M. Cameron 	void __iomem *page_remapped = ioremap_nocache(page_base,
6187088ba34cSStephen M. Cameron 		page_offs + size);
6188edd16368SStephen M. Cameron 
6189edd16368SStephen M. Cameron 	return page_remapped ? (page_remapped + page_offs) : NULL;
6190edd16368SStephen M. Cameron }
6191edd16368SStephen M. Cameron 
6192254f796bSMatt Gates static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
6193edd16368SStephen M. Cameron {
6194254f796bSMatt Gates 	return h->access.command_completed(h, q);
6195edd16368SStephen M. Cameron }
6196edd16368SStephen M. Cameron 
6197900c5440SStephen M. Cameron static inline bool interrupt_pending(struct ctlr_info *h)
6198edd16368SStephen M. Cameron {
6199edd16368SStephen M. Cameron 	return h->access.intr_pending(h);
6200edd16368SStephen M. Cameron }
6201edd16368SStephen M. Cameron 
6202edd16368SStephen M. Cameron static inline long interrupt_not_for_us(struct ctlr_info *h)
6203edd16368SStephen M. Cameron {
620410f66018SStephen M. Cameron 	return (h->access.intr_pending(h) == 0) ||
620510f66018SStephen M. Cameron 		(h->interrupts_enabled == 0);
6206edd16368SStephen M. Cameron }
6207edd16368SStephen M. Cameron 
620801a02ffcSStephen M. Cameron static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
620901a02ffcSStephen M. Cameron 	u32 raw_tag)
6210edd16368SStephen M. Cameron {
6211edd16368SStephen M. Cameron 	if (unlikely(tag_index >= h->nr_cmds)) {
6212edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
6213edd16368SStephen M. Cameron 		return 1;
6214edd16368SStephen M. Cameron 	}
6215edd16368SStephen M. Cameron 	return 0;
6216edd16368SStephen M. Cameron }
6217edd16368SStephen M. Cameron 
62185a3d16f5SStephen M. Cameron static inline void finish_cmd(struct CommandList *c)
6219edd16368SStephen M. Cameron {
6220e85c5974SStephen M. Cameron 	dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
6221c349775eSScott Teel 	if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
6222c349775eSScott Teel 			|| c->cmd_type == CMD_IOACCEL2))
62231fb011fbSStephen M. Cameron 		complete_scsi_command(c);
62248be986ccSStephen Cameron 	else if (c->cmd_type == CMD_IOCTL_PEND || c->cmd_type == IOACCEL2_TMF)
6225edd16368SStephen M. Cameron 		complete(c->waiting);
6226a104c99fSStephen M. Cameron }
6227a104c99fSStephen M. Cameron 
6228a9a3a273SStephen M. Cameron 
6229a9a3a273SStephen M. Cameron static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
6230a104c99fSStephen M. Cameron {
6231a9a3a273SStephen M. Cameron #define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
6232a9a3a273SStephen M. Cameron #define HPSA_SIMPLE_ERROR_BITS 0x03
6233960a30e7SStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
6234a9a3a273SStephen M. Cameron 		return tag & ~HPSA_SIMPLE_ERROR_BITS;
6235a9a3a273SStephen M. Cameron 	return tag & ~HPSA_PERF_ERROR_BITS;
6236a104c99fSStephen M. Cameron }
6237a104c99fSStephen M. Cameron 
6238303932fdSDon Brace /* process completion of an indexed ("direct lookup") command */
62391d94f94dSStephen M. Cameron static inline void process_indexed_cmd(struct ctlr_info *h,
6240303932fdSDon Brace 	u32 raw_tag)
6241303932fdSDon Brace {
6242303932fdSDon Brace 	u32 tag_index;
6243303932fdSDon Brace 	struct CommandList *c;
6244303932fdSDon Brace 
6245f2405db8SDon Brace 	tag_index = raw_tag >> DIRECT_LOOKUP_SHIFT;
62461d94f94dSStephen M. Cameron 	if (!bad_tag(h, tag_index, raw_tag)) {
6247303932fdSDon Brace 		c = h->cmd_pool + tag_index;
62485a3d16f5SStephen M. Cameron 		finish_cmd(c);
62491d94f94dSStephen M. Cameron 	}
6250303932fdSDon Brace }
6251303932fdSDon Brace 
625264670ac8SStephen M. Cameron /* Some controllers, like p400, will give us one interrupt
625364670ac8SStephen M. Cameron  * after a soft reset, even if we turned interrupts off.
625464670ac8SStephen M. Cameron  * Only need to check for this in the hpsa_xxx_discard_completions
625564670ac8SStephen M. Cameron  * functions.
625664670ac8SStephen M. Cameron  */
625764670ac8SStephen M. Cameron static int ignore_bogus_interrupt(struct ctlr_info *h)
625864670ac8SStephen M. Cameron {
625964670ac8SStephen M. Cameron 	if (likely(!reset_devices))
626064670ac8SStephen M. Cameron 		return 0;
626164670ac8SStephen M. Cameron 
626264670ac8SStephen M. Cameron 	if (likely(h->interrupts_enabled))
626364670ac8SStephen M. Cameron 		return 0;
626464670ac8SStephen M. Cameron 
626564670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
626664670ac8SStephen M. Cameron 		"(known firmware bug.)  Ignoring.\n");
626764670ac8SStephen M. Cameron 
626864670ac8SStephen M. Cameron 	return 1;
626964670ac8SStephen M. Cameron }
627064670ac8SStephen M. Cameron 
6271254f796bSMatt Gates /*
6272254f796bSMatt Gates  * Convert &h->q[x] (passed to interrupt handlers) back to h.
6273254f796bSMatt Gates  * Relies on (h-q[x] == x) being true for x such that
6274254f796bSMatt Gates  * 0 <= x < MAX_REPLY_QUEUES.
6275254f796bSMatt Gates  */
6276254f796bSMatt Gates static struct ctlr_info *queue_to_hba(u8 *queue)
627764670ac8SStephen M. Cameron {
6278254f796bSMatt Gates 	return container_of((queue - *queue), struct ctlr_info, q[0]);
6279254f796bSMatt Gates }
6280254f796bSMatt Gates 
6281254f796bSMatt Gates static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
6282254f796bSMatt Gates {
6283254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
6284254f796bSMatt Gates 	u8 q = *(u8 *) queue;
628564670ac8SStephen M. Cameron 	u32 raw_tag;
628664670ac8SStephen M. Cameron 
628764670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
628864670ac8SStephen M. Cameron 		return IRQ_NONE;
628964670ac8SStephen M. Cameron 
629064670ac8SStephen M. Cameron 	if (interrupt_not_for_us(h))
629164670ac8SStephen M. Cameron 		return IRQ_NONE;
6292a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
629364670ac8SStephen M. Cameron 	while (interrupt_pending(h)) {
6294254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
629564670ac8SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY)
6296254f796bSMatt Gates 			raw_tag = next_command(h, q);
629764670ac8SStephen M. Cameron 	}
629864670ac8SStephen M. Cameron 	return IRQ_HANDLED;
629964670ac8SStephen M. Cameron }
630064670ac8SStephen M. Cameron 
6301254f796bSMatt Gates static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
630264670ac8SStephen M. Cameron {
6303254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
630464670ac8SStephen M. Cameron 	u32 raw_tag;
6305254f796bSMatt Gates 	u8 q = *(u8 *) queue;
630664670ac8SStephen M. Cameron 
630764670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
630864670ac8SStephen M. Cameron 		return IRQ_NONE;
630964670ac8SStephen M. Cameron 
6310a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
6311254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
631264670ac8SStephen M. Cameron 	while (raw_tag != FIFO_EMPTY)
6313254f796bSMatt Gates 		raw_tag = next_command(h, q);
631464670ac8SStephen M. Cameron 	return IRQ_HANDLED;
631564670ac8SStephen M. Cameron }
631664670ac8SStephen M. Cameron 
6317254f796bSMatt Gates static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
6318edd16368SStephen M. Cameron {
6319254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba((u8 *) queue);
6320303932fdSDon Brace 	u32 raw_tag;
6321254f796bSMatt Gates 	u8 q = *(u8 *) queue;
6322edd16368SStephen M. Cameron 
6323edd16368SStephen M. Cameron 	if (interrupt_not_for_us(h))
6324edd16368SStephen M. Cameron 		return IRQ_NONE;
6325a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
632610f66018SStephen M. Cameron 	while (interrupt_pending(h)) {
6327254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
632810f66018SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY) {
63291d94f94dSStephen M. Cameron 			process_indexed_cmd(h, raw_tag);
6330254f796bSMatt Gates 			raw_tag = next_command(h, q);
633110f66018SStephen M. Cameron 		}
633210f66018SStephen M. Cameron 	}
633310f66018SStephen M. Cameron 	return IRQ_HANDLED;
633410f66018SStephen M. Cameron }
633510f66018SStephen M. Cameron 
6336254f796bSMatt Gates static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
633710f66018SStephen M. Cameron {
6338254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
633910f66018SStephen M. Cameron 	u32 raw_tag;
6340254f796bSMatt Gates 	u8 q = *(u8 *) queue;
634110f66018SStephen M. Cameron 
6342a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
6343254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
6344303932fdSDon Brace 	while (raw_tag != FIFO_EMPTY) {
63451d94f94dSStephen M. Cameron 		process_indexed_cmd(h, raw_tag);
6346254f796bSMatt Gates 		raw_tag = next_command(h, q);
6347edd16368SStephen M. Cameron 	}
6348edd16368SStephen M. Cameron 	return IRQ_HANDLED;
6349edd16368SStephen M. Cameron }
6350edd16368SStephen M. Cameron 
6351a9a3a273SStephen M. Cameron /* Send a message CDB to the firmware. Careful, this only works
6352a9a3a273SStephen M. Cameron  * in simple mode, not performant mode due to the tag lookup.
6353a9a3a273SStephen M. Cameron  * We only ever use this immediately after a controller reset.
6354a9a3a273SStephen M. Cameron  */
63556f039790SGreg Kroah-Hartman static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
6356edd16368SStephen M. Cameron 			unsigned char type)
6357edd16368SStephen M. Cameron {
6358edd16368SStephen M. Cameron 	struct Command {
6359edd16368SStephen M. Cameron 		struct CommandListHeader CommandHeader;
6360edd16368SStephen M. Cameron 		struct RequestBlock Request;
6361edd16368SStephen M. Cameron 		struct ErrDescriptor ErrorDescriptor;
6362edd16368SStephen M. Cameron 	};
6363edd16368SStephen M. Cameron 	struct Command *cmd;
6364edd16368SStephen M. Cameron 	static const size_t cmd_sz = sizeof(*cmd) +
6365edd16368SStephen M. Cameron 					sizeof(cmd->ErrorDescriptor);
6366edd16368SStephen M. Cameron 	dma_addr_t paddr64;
63672b08b3e9SDon Brace 	__le32 paddr32;
63682b08b3e9SDon Brace 	u32 tag;
6369edd16368SStephen M. Cameron 	void __iomem *vaddr;
6370edd16368SStephen M. Cameron 	int i, err;
6371edd16368SStephen M. Cameron 
6372edd16368SStephen M. Cameron 	vaddr = pci_ioremap_bar(pdev, 0);
6373edd16368SStephen M. Cameron 	if (vaddr == NULL)
6374edd16368SStephen M. Cameron 		return -ENOMEM;
6375edd16368SStephen M. Cameron 
6376edd16368SStephen M. Cameron 	/* The Inbound Post Queue only accepts 32-bit physical addresses for the
6377edd16368SStephen M. Cameron 	 * CCISS commands, so they must be allocated from the lower 4GiB of
6378edd16368SStephen M. Cameron 	 * memory.
6379edd16368SStephen M. Cameron 	 */
6380edd16368SStephen M. Cameron 	err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
6381edd16368SStephen M. Cameron 	if (err) {
6382edd16368SStephen M. Cameron 		iounmap(vaddr);
63831eaec8f3SRobert Elliott 		return err;
6384edd16368SStephen M. Cameron 	}
6385edd16368SStephen M. Cameron 
6386edd16368SStephen M. Cameron 	cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
6387edd16368SStephen M. Cameron 	if (cmd == NULL) {
6388edd16368SStephen M. Cameron 		iounmap(vaddr);
6389edd16368SStephen M. Cameron 		return -ENOMEM;
6390edd16368SStephen M. Cameron 	}
6391edd16368SStephen M. Cameron 
6392edd16368SStephen M. Cameron 	/* This must fit, because of the 32-bit consistent DMA mask.  Also,
6393edd16368SStephen M. Cameron 	 * although there's no guarantee, we assume that the address is at
6394edd16368SStephen M. Cameron 	 * least 4-byte aligned (most likely, it's page-aligned).
6395edd16368SStephen M. Cameron 	 */
63962b08b3e9SDon Brace 	paddr32 = cpu_to_le32(paddr64);
6397edd16368SStephen M. Cameron 
6398edd16368SStephen M. Cameron 	cmd->CommandHeader.ReplyQueue = 0;
6399edd16368SStephen M. Cameron 	cmd->CommandHeader.SGList = 0;
640050a0decfSStephen M. Cameron 	cmd->CommandHeader.SGTotal = cpu_to_le16(0);
64012b08b3e9SDon Brace 	cmd->CommandHeader.tag = cpu_to_le64(paddr64);
6402edd16368SStephen M. Cameron 	memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
6403edd16368SStephen M. Cameron 
6404edd16368SStephen M. Cameron 	cmd->Request.CDBLen = 16;
6405a505b86fSStephen M. Cameron 	cmd->Request.type_attr_dir =
6406a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_MSG, ATTR_HEADOFQUEUE, XFER_NONE);
6407edd16368SStephen M. Cameron 	cmd->Request.Timeout = 0; /* Don't time out */
6408edd16368SStephen M. Cameron 	cmd->Request.CDB[0] = opcode;
6409edd16368SStephen M. Cameron 	cmd->Request.CDB[1] = type;
6410edd16368SStephen M. Cameron 	memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
641150a0decfSStephen M. Cameron 	cmd->ErrorDescriptor.Addr =
64122b08b3e9SDon Brace 			cpu_to_le64((le32_to_cpu(paddr32) + sizeof(*cmd)));
641350a0decfSStephen M. Cameron 	cmd->ErrorDescriptor.Len = cpu_to_le32(sizeof(struct ErrorInfo));
6414edd16368SStephen M. Cameron 
64152b08b3e9SDon Brace 	writel(le32_to_cpu(paddr32), vaddr + SA5_REQUEST_PORT_OFFSET);
6416edd16368SStephen M. Cameron 
6417edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
6418edd16368SStephen M. Cameron 		tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
64192b08b3e9SDon Brace 		if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr64)
6420edd16368SStephen M. Cameron 			break;
6421edd16368SStephen M. Cameron 		msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
6422edd16368SStephen M. Cameron 	}
6423edd16368SStephen M. Cameron 
6424edd16368SStephen M. Cameron 	iounmap(vaddr);
6425edd16368SStephen M. Cameron 
6426edd16368SStephen M. Cameron 	/* we leak the DMA buffer here ... no choice since the controller could
6427edd16368SStephen M. Cameron 	 *  still complete the command.
6428edd16368SStephen M. Cameron 	 */
6429edd16368SStephen M. Cameron 	if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
6430edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
6431edd16368SStephen M. Cameron 			opcode, type);
6432edd16368SStephen M. Cameron 		return -ETIMEDOUT;
6433edd16368SStephen M. Cameron 	}
6434edd16368SStephen M. Cameron 
6435edd16368SStephen M. Cameron 	pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
6436edd16368SStephen M. Cameron 
6437edd16368SStephen M. Cameron 	if (tag & HPSA_ERROR_BIT) {
6438edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
6439edd16368SStephen M. Cameron 			opcode, type);
6440edd16368SStephen M. Cameron 		return -EIO;
6441edd16368SStephen M. Cameron 	}
6442edd16368SStephen M. Cameron 
6443edd16368SStephen M. Cameron 	dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
6444edd16368SStephen M. Cameron 		opcode, type);
6445edd16368SStephen M. Cameron 	return 0;
6446edd16368SStephen M. Cameron }
6447edd16368SStephen M. Cameron 
6448edd16368SStephen M. Cameron #define hpsa_noop(p) hpsa_message(p, 3, 0)
6449edd16368SStephen M. Cameron 
64501df8552aSStephen M. Cameron static int hpsa_controller_hard_reset(struct pci_dev *pdev,
645142a91641SDon Brace 	void __iomem *vaddr, u32 use_doorbell)
6452edd16368SStephen M. Cameron {
6453edd16368SStephen M. Cameron 
64541df8552aSStephen M. Cameron 	if (use_doorbell) {
64551df8552aSStephen M. Cameron 		/* For everything after the P600, the PCI power state method
64561df8552aSStephen M. Cameron 		 * of resetting the controller doesn't work, so we have this
64571df8552aSStephen M. Cameron 		 * other way using the doorbell register.
6458edd16368SStephen M. Cameron 		 */
64591df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using doorbell to reset controller\n");
6460cf0b08d0SStephen M. Cameron 		writel(use_doorbell, vaddr + SA5_DOORBELL);
646185009239SStephen M. Cameron 
646200701a96SJustin Lindley 		/* PMC hardware guys tell us we need a 10 second delay after
646385009239SStephen M. Cameron 		 * doorbell reset and before any attempt to talk to the board
646485009239SStephen M. Cameron 		 * at all to ensure that this actually works and doesn't fall
646585009239SStephen M. Cameron 		 * over in some weird corner cases.
646685009239SStephen M. Cameron 		 */
646700701a96SJustin Lindley 		msleep(10000);
64681df8552aSStephen M. Cameron 	} else { /* Try to do it the PCI power state way */
6469edd16368SStephen M. Cameron 
6470edd16368SStephen M. Cameron 		/* Quoting from the Open CISS Specification: "The Power
6471edd16368SStephen M. Cameron 		 * Management Control/Status Register (CSR) controls the power
6472edd16368SStephen M. Cameron 		 * state of the device.  The normal operating state is D0,
6473edd16368SStephen M. Cameron 		 * CSR=00h.  The software off state is D3, CSR=03h.  To reset
64741df8552aSStephen M. Cameron 		 * the controller, place the interface device in D3 then to D0,
64751df8552aSStephen M. Cameron 		 * this causes a secondary PCI reset which will reset the
64761df8552aSStephen M. Cameron 		 * controller." */
6477edd16368SStephen M. Cameron 
64782662cab8SDon Brace 		int rc = 0;
64792662cab8SDon Brace 
64801df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using PCI PM to reset controller\n");
64812662cab8SDon Brace 
6482edd16368SStephen M. Cameron 		/* enter the D3hot power management state */
64832662cab8SDon Brace 		rc = pci_set_power_state(pdev, PCI_D3hot);
64842662cab8SDon Brace 		if (rc)
64852662cab8SDon Brace 			return rc;
6486edd16368SStephen M. Cameron 
6487edd16368SStephen M. Cameron 		msleep(500);
6488edd16368SStephen M. Cameron 
6489edd16368SStephen M. Cameron 		/* enter the D0 power management state */
64902662cab8SDon Brace 		rc = pci_set_power_state(pdev, PCI_D0);
64912662cab8SDon Brace 		if (rc)
64922662cab8SDon Brace 			return rc;
6493c4853efeSMike Miller 
6494c4853efeSMike Miller 		/*
6495c4853efeSMike Miller 		 * The P600 requires a small delay when changing states.
6496c4853efeSMike Miller 		 * Otherwise we may think the board did not reset and we bail.
6497c4853efeSMike Miller 		 * This for kdump only and is particular to the P600.
6498c4853efeSMike Miller 		 */
6499c4853efeSMike Miller 		msleep(500);
65001df8552aSStephen M. Cameron 	}
65011df8552aSStephen M. Cameron 	return 0;
65021df8552aSStephen M. Cameron }
65031df8552aSStephen M. Cameron 
65046f039790SGreg Kroah-Hartman static void init_driver_version(char *driver_version, int len)
6505580ada3cSStephen M. Cameron {
6506580ada3cSStephen M. Cameron 	memset(driver_version, 0, len);
6507f79cfec6SStephen M. Cameron 	strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
6508580ada3cSStephen M. Cameron }
6509580ada3cSStephen M. Cameron 
65106f039790SGreg Kroah-Hartman static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
6511580ada3cSStephen M. Cameron {
6512580ada3cSStephen M. Cameron 	char *driver_version;
6513580ada3cSStephen M. Cameron 	int i, size = sizeof(cfgtable->driver_version);
6514580ada3cSStephen M. Cameron 
6515580ada3cSStephen M. Cameron 	driver_version = kmalloc(size, GFP_KERNEL);
6516580ada3cSStephen M. Cameron 	if (!driver_version)
6517580ada3cSStephen M. Cameron 		return -ENOMEM;
6518580ada3cSStephen M. Cameron 
6519580ada3cSStephen M. Cameron 	init_driver_version(driver_version, size);
6520580ada3cSStephen M. Cameron 	for (i = 0; i < size; i++)
6521580ada3cSStephen M. Cameron 		writeb(driver_version[i], &cfgtable->driver_version[i]);
6522580ada3cSStephen M. Cameron 	kfree(driver_version);
6523580ada3cSStephen M. Cameron 	return 0;
6524580ada3cSStephen M. Cameron }
6525580ada3cSStephen M. Cameron 
65266f039790SGreg Kroah-Hartman static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
65276f039790SGreg Kroah-Hartman 					  unsigned char *driver_ver)
6528580ada3cSStephen M. Cameron {
6529580ada3cSStephen M. Cameron 	int i;
6530580ada3cSStephen M. Cameron 
6531580ada3cSStephen M. Cameron 	for (i = 0; i < sizeof(cfgtable->driver_version); i++)
6532580ada3cSStephen M. Cameron 		driver_ver[i] = readb(&cfgtable->driver_version[i]);
6533580ada3cSStephen M. Cameron }
6534580ada3cSStephen M. Cameron 
65356f039790SGreg Kroah-Hartman static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
6536580ada3cSStephen M. Cameron {
6537580ada3cSStephen M. Cameron 
6538580ada3cSStephen M. Cameron 	char *driver_ver, *old_driver_ver;
6539580ada3cSStephen M. Cameron 	int rc, size = sizeof(cfgtable->driver_version);
6540580ada3cSStephen M. Cameron 
6541580ada3cSStephen M. Cameron 	old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
6542580ada3cSStephen M. Cameron 	if (!old_driver_ver)
6543580ada3cSStephen M. Cameron 		return -ENOMEM;
6544580ada3cSStephen M. Cameron 	driver_ver = old_driver_ver + size;
6545580ada3cSStephen M. Cameron 
6546580ada3cSStephen M. Cameron 	/* After a reset, the 32 bytes of "driver version" in the cfgtable
6547580ada3cSStephen M. Cameron 	 * should have been changed, otherwise we know the reset failed.
6548580ada3cSStephen M. Cameron 	 */
6549580ada3cSStephen M. Cameron 	init_driver_version(old_driver_ver, size);
6550580ada3cSStephen M. Cameron 	read_driver_ver_from_cfgtable(cfgtable, driver_ver);
6551580ada3cSStephen M. Cameron 	rc = !memcmp(driver_ver, old_driver_ver, size);
6552580ada3cSStephen M. Cameron 	kfree(old_driver_ver);
6553580ada3cSStephen M. Cameron 	return rc;
6554580ada3cSStephen M. Cameron }
65551df8552aSStephen M. Cameron /* This does a hard reset of the controller using PCI power management
65561df8552aSStephen M. Cameron  * states or the using the doorbell register.
65571df8552aSStephen M. Cameron  */
65586b6c1cd7STomas Henzl static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev, u32 board_id)
65591df8552aSStephen M. Cameron {
65601df8552aSStephen M. Cameron 	u64 cfg_offset;
65611df8552aSStephen M. Cameron 	u32 cfg_base_addr;
65621df8552aSStephen M. Cameron 	u64 cfg_base_addr_index;
65631df8552aSStephen M. Cameron 	void __iomem *vaddr;
65641df8552aSStephen M. Cameron 	unsigned long paddr;
6565580ada3cSStephen M. Cameron 	u32 misc_fw_support;
6566270d05deSStephen M. Cameron 	int rc;
65671df8552aSStephen M. Cameron 	struct CfgTable __iomem *cfgtable;
6568cf0b08d0SStephen M. Cameron 	u32 use_doorbell;
6569270d05deSStephen M. Cameron 	u16 command_register;
65701df8552aSStephen M. Cameron 
65711df8552aSStephen M. Cameron 	/* For controllers as old as the P600, this is very nearly
65721df8552aSStephen M. Cameron 	 * the same thing as
65731df8552aSStephen M. Cameron 	 *
65741df8552aSStephen M. Cameron 	 * pci_save_state(pci_dev);
65751df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D3hot);
65761df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D0);
65771df8552aSStephen M. Cameron 	 * pci_restore_state(pci_dev);
65781df8552aSStephen M. Cameron 	 *
65791df8552aSStephen M. Cameron 	 * For controllers newer than the P600, the pci power state
65801df8552aSStephen M. Cameron 	 * method of resetting doesn't work so we have another way
65811df8552aSStephen M. Cameron 	 * using the doorbell register.
65821df8552aSStephen M. Cameron 	 */
658318867659SStephen M. Cameron 
658460f923b9SRobert Elliott 	if (!ctlr_is_resettable(board_id)) {
658560f923b9SRobert Elliott 		dev_warn(&pdev->dev, "Controller not resettable\n");
658625c1e56aSStephen M. Cameron 		return -ENODEV;
658725c1e56aSStephen M. Cameron 	}
658846380786SStephen M. Cameron 
658946380786SStephen M. Cameron 	/* if controller is soft- but not hard resettable... */
659046380786SStephen M. Cameron 	if (!ctlr_is_hard_resettable(board_id))
659146380786SStephen M. Cameron 		return -ENOTSUPP; /* try soft reset later. */
659218867659SStephen M. Cameron 
6593270d05deSStephen M. Cameron 	/* Save the PCI command register */
6594270d05deSStephen M. Cameron 	pci_read_config_word(pdev, 4, &command_register);
6595270d05deSStephen M. Cameron 	pci_save_state(pdev);
65961df8552aSStephen M. Cameron 
65971df8552aSStephen M. Cameron 	/* find the first memory BAR, so we can find the cfg table */
65981df8552aSStephen M. Cameron 	rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
65991df8552aSStephen M. Cameron 	if (rc)
66001df8552aSStephen M. Cameron 		return rc;
66011df8552aSStephen M. Cameron 	vaddr = remap_pci_mem(paddr, 0x250);
66021df8552aSStephen M. Cameron 	if (!vaddr)
66031df8552aSStephen M. Cameron 		return -ENOMEM;
66041df8552aSStephen M. Cameron 
66051df8552aSStephen M. Cameron 	/* find cfgtable in order to check if reset via doorbell is supported */
66061df8552aSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
66071df8552aSStephen M. Cameron 					&cfg_base_addr_index, &cfg_offset);
66081df8552aSStephen M. Cameron 	if (rc)
66091df8552aSStephen M. Cameron 		goto unmap_vaddr;
66101df8552aSStephen M. Cameron 	cfgtable = remap_pci_mem(pci_resource_start(pdev,
66111df8552aSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
66121df8552aSStephen M. Cameron 	if (!cfgtable) {
66131df8552aSStephen M. Cameron 		rc = -ENOMEM;
66141df8552aSStephen M. Cameron 		goto unmap_vaddr;
66151df8552aSStephen M. Cameron 	}
6616580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(cfgtable);
6617580ada3cSStephen M. Cameron 	if (rc)
661803741d95STomas Henzl 		goto unmap_cfgtable;
66191df8552aSStephen M. Cameron 
6620cf0b08d0SStephen M. Cameron 	/* If reset via doorbell register is supported, use that.
6621cf0b08d0SStephen M. Cameron 	 * There are two such methods.  Favor the newest method.
6622cf0b08d0SStephen M. Cameron 	 */
66231df8552aSStephen M. Cameron 	misc_fw_support = readl(&cfgtable->misc_fw_support);
6624cf0b08d0SStephen M. Cameron 	use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
6625cf0b08d0SStephen M. Cameron 	if (use_doorbell) {
6626cf0b08d0SStephen M. Cameron 		use_doorbell = DOORBELL_CTLR_RESET2;
6627cf0b08d0SStephen M. Cameron 	} else {
66281df8552aSStephen M. Cameron 		use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
6629cf0b08d0SStephen M. Cameron 		if (use_doorbell) {
6630050f7147SStephen Cameron 			dev_warn(&pdev->dev,
6631050f7147SStephen Cameron 				"Soft reset not supported. Firmware update is required.\n");
663264670ac8SStephen M. Cameron 			rc = -ENOTSUPP; /* try soft reset */
6633cf0b08d0SStephen M. Cameron 			goto unmap_cfgtable;
6634cf0b08d0SStephen M. Cameron 		}
6635cf0b08d0SStephen M. Cameron 	}
66361df8552aSStephen M. Cameron 
66371df8552aSStephen M. Cameron 	rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
66381df8552aSStephen M. Cameron 	if (rc)
66391df8552aSStephen M. Cameron 		goto unmap_cfgtable;
6640edd16368SStephen M. Cameron 
6641270d05deSStephen M. Cameron 	pci_restore_state(pdev);
6642270d05deSStephen M. Cameron 	pci_write_config_word(pdev, 4, command_register);
6643edd16368SStephen M. Cameron 
66441df8552aSStephen M. Cameron 	/* Some devices (notably the HP Smart Array 5i Controller)
66451df8552aSStephen M. Cameron 	   need a little pause here */
66461df8552aSStephen M. Cameron 	msleep(HPSA_POST_RESET_PAUSE_MSECS);
66471df8552aSStephen M. Cameron 
6648fe5389c8SStephen M. Cameron 	rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
6649fe5389c8SStephen M. Cameron 	if (rc) {
6650fe5389c8SStephen M. Cameron 		dev_warn(&pdev->dev,
6651050f7147SStephen Cameron 			"Failed waiting for board to become ready after hard reset\n");
6652fe5389c8SStephen M. Cameron 		goto unmap_cfgtable;
6653fe5389c8SStephen M. Cameron 	}
6654fe5389c8SStephen M. Cameron 
6655580ada3cSStephen M. Cameron 	rc = controller_reset_failed(vaddr);
6656580ada3cSStephen M. Cameron 	if (rc < 0)
6657580ada3cSStephen M. Cameron 		goto unmap_cfgtable;
6658580ada3cSStephen M. Cameron 	if (rc) {
665964670ac8SStephen M. Cameron 		dev_warn(&pdev->dev, "Unable to successfully reset "
666064670ac8SStephen M. Cameron 			"controller. Will try soft reset.\n");
666164670ac8SStephen M. Cameron 		rc = -ENOTSUPP;
6662580ada3cSStephen M. Cameron 	} else {
666364670ac8SStephen M. Cameron 		dev_info(&pdev->dev, "board ready after hard reset.\n");
66641df8552aSStephen M. Cameron 	}
66651df8552aSStephen M. Cameron 
66661df8552aSStephen M. Cameron unmap_cfgtable:
66671df8552aSStephen M. Cameron 	iounmap(cfgtable);
66681df8552aSStephen M. Cameron 
66691df8552aSStephen M. Cameron unmap_vaddr:
66701df8552aSStephen M. Cameron 	iounmap(vaddr);
66711df8552aSStephen M. Cameron 	return rc;
6672edd16368SStephen M. Cameron }
6673edd16368SStephen M. Cameron 
6674edd16368SStephen M. Cameron /*
6675edd16368SStephen M. Cameron  *  We cannot read the structure directly, for portability we must use
6676edd16368SStephen M. Cameron  *   the io functions.
6677edd16368SStephen M. Cameron  *   This is for debug only.
6678edd16368SStephen M. Cameron  */
667942a91641SDon Brace static void print_cfg_table(struct device *dev, struct CfgTable __iomem *tb)
6680edd16368SStephen M. Cameron {
668158f8665cSStephen M. Cameron #ifdef HPSA_DEBUG
6682edd16368SStephen M. Cameron 	int i;
6683edd16368SStephen M. Cameron 	char temp_name[17];
6684edd16368SStephen M. Cameron 
6685edd16368SStephen M. Cameron 	dev_info(dev, "Controller Configuration information\n");
6686edd16368SStephen M. Cameron 	dev_info(dev, "------------------------------------\n");
6687edd16368SStephen M. Cameron 	for (i = 0; i < 4; i++)
6688edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->Signature[i]));
6689edd16368SStephen M. Cameron 	temp_name[4] = '\0';
6690edd16368SStephen M. Cameron 	dev_info(dev, "   Signature = %s\n", temp_name);
6691edd16368SStephen M. Cameron 	dev_info(dev, "   Spec Number = %d\n", readl(&(tb->SpecValence)));
6692edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods supported = 0x%x\n",
6693edd16368SStephen M. Cameron 	       readl(&(tb->TransportSupport)));
6694edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods active = 0x%x\n",
6695edd16368SStephen M. Cameron 	       readl(&(tb->TransportActive)));
6696edd16368SStephen M. Cameron 	dev_info(dev, "   Requested transport Method = 0x%x\n",
6697edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.TransportRequest)));
6698edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Delay = 0x%x\n",
6699edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntDelay)));
6700edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Count = 0x%x\n",
6701edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntCount)));
670269d6e33dSRobert Elliott 	dev_info(dev, "   Max outstanding commands = %d\n",
6703edd16368SStephen M. Cameron 	       readl(&(tb->CmdsOutMax)));
6704edd16368SStephen M. Cameron 	dev_info(dev, "   Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
6705edd16368SStephen M. Cameron 	for (i = 0; i < 16; i++)
6706edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->ServerName[i]));
6707edd16368SStephen M. Cameron 	temp_name[16] = '\0';
6708edd16368SStephen M. Cameron 	dev_info(dev, "   Server Name = %s\n", temp_name);
6709edd16368SStephen M. Cameron 	dev_info(dev, "   Heartbeat Counter = 0x%x\n\n\n",
6710edd16368SStephen M. Cameron 		readl(&(tb->HeartBeat)));
6711edd16368SStephen M. Cameron #endif				/* HPSA_DEBUG */
671258f8665cSStephen M. Cameron }
6713edd16368SStephen M. Cameron 
6714edd16368SStephen M. Cameron static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
6715edd16368SStephen M. Cameron {
6716edd16368SStephen M. Cameron 	int i, offset, mem_type, bar_type;
6717edd16368SStephen M. Cameron 
6718edd16368SStephen M. Cameron 	if (pci_bar_addr == PCI_BASE_ADDRESS_0)	/* looking for BAR zero? */
6719edd16368SStephen M. Cameron 		return 0;
6720edd16368SStephen M. Cameron 	offset = 0;
6721edd16368SStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
6722edd16368SStephen M. Cameron 		bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
6723edd16368SStephen M. Cameron 		if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
6724edd16368SStephen M. Cameron 			offset += 4;
6725edd16368SStephen M. Cameron 		else {
6726edd16368SStephen M. Cameron 			mem_type = pci_resource_flags(pdev, i) &
6727edd16368SStephen M. Cameron 			    PCI_BASE_ADDRESS_MEM_TYPE_MASK;
6728edd16368SStephen M. Cameron 			switch (mem_type) {
6729edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_32:
6730edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_1M:
6731edd16368SStephen M. Cameron 				offset += 4;	/* 32 bit */
6732edd16368SStephen M. Cameron 				break;
6733edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_64:
6734edd16368SStephen M. Cameron 				offset += 8;
6735edd16368SStephen M. Cameron 				break;
6736edd16368SStephen M. Cameron 			default:	/* reserved in PCI 2.2 */
6737edd16368SStephen M. Cameron 				dev_warn(&pdev->dev,
6738edd16368SStephen M. Cameron 				       "base address is invalid\n");
6739edd16368SStephen M. Cameron 				return -1;
6740edd16368SStephen M. Cameron 				break;
6741edd16368SStephen M. Cameron 			}
6742edd16368SStephen M. Cameron 		}
6743edd16368SStephen M. Cameron 		if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
6744edd16368SStephen M. Cameron 			return i + 1;
6745edd16368SStephen M. Cameron 	}
6746edd16368SStephen M. Cameron 	return -1;
6747edd16368SStephen M. Cameron }
6748edd16368SStephen M. Cameron 
6749cc64c817SRobert Elliott static void hpsa_disable_interrupt_mode(struct ctlr_info *h)
6750cc64c817SRobert Elliott {
6751cc64c817SRobert Elliott 	if (h->msix_vector) {
6752cc64c817SRobert Elliott 		if (h->pdev->msix_enabled)
6753cc64c817SRobert Elliott 			pci_disable_msix(h->pdev);
6754105a3dbcSRobert Elliott 		h->msix_vector = 0;
6755cc64c817SRobert Elliott 	} else if (h->msi_vector) {
6756cc64c817SRobert Elliott 		if (h->pdev->msi_enabled)
6757cc64c817SRobert Elliott 			pci_disable_msi(h->pdev);
6758105a3dbcSRobert Elliott 		h->msi_vector = 0;
6759cc64c817SRobert Elliott 	}
6760cc64c817SRobert Elliott }
6761cc64c817SRobert Elliott 
6762edd16368SStephen M. Cameron /* If MSI/MSI-X is supported by the kernel we will try to enable it on
6763050f7147SStephen Cameron  * controllers that are capable. If not, we use legacy INTx mode.
6764edd16368SStephen M. Cameron  */
67656f039790SGreg Kroah-Hartman static void hpsa_interrupt_mode(struct ctlr_info *h)
6766edd16368SStephen M. Cameron {
6767edd16368SStephen M. Cameron #ifdef CONFIG_PCI_MSI
6768254f796bSMatt Gates 	int err, i;
6769254f796bSMatt Gates 	struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
6770254f796bSMatt Gates 
6771254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++) {
6772254f796bSMatt Gates 		hpsa_msix_entries[i].vector = 0;
6773254f796bSMatt Gates 		hpsa_msix_entries[i].entry = i;
6774254f796bSMatt Gates 	}
6775edd16368SStephen M. Cameron 
6776edd16368SStephen M. Cameron 	/* Some boards advertise MSI but don't really support it */
67776b3f4c52SStephen M. Cameron 	if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
67786b3f4c52SStephen M. Cameron 	    (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
6779edd16368SStephen M. Cameron 		goto default_int_mode;
678055c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
6781050f7147SStephen Cameron 		dev_info(&h->pdev->dev, "MSI-X capable controller\n");
6782eee0f03aSHannes Reinecke 		h->msix_vector = MAX_REPLY_QUEUES;
6783f89439bcSStephen M. Cameron 		if (h->msix_vector > num_online_cpus())
6784f89439bcSStephen M. Cameron 			h->msix_vector = num_online_cpus();
678518fce3c4SAlexander Gordeev 		err = pci_enable_msix_range(h->pdev, hpsa_msix_entries,
678618fce3c4SAlexander Gordeev 					    1, h->msix_vector);
678718fce3c4SAlexander Gordeev 		if (err < 0) {
678818fce3c4SAlexander Gordeev 			dev_warn(&h->pdev->dev, "MSI-X init failed %d\n", err);
678918fce3c4SAlexander Gordeev 			h->msix_vector = 0;
679018fce3c4SAlexander Gordeev 			goto single_msi_mode;
679118fce3c4SAlexander Gordeev 		} else if (err < h->msix_vector) {
679255c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
6793edd16368SStephen M. Cameron 			       "available\n", err);
6794eee0f03aSHannes Reinecke 		}
679518fce3c4SAlexander Gordeev 		h->msix_vector = err;
6796eee0f03aSHannes Reinecke 		for (i = 0; i < h->msix_vector; i++)
6797eee0f03aSHannes Reinecke 			h->intr[i] = hpsa_msix_entries[i].vector;
6798eee0f03aSHannes Reinecke 		return;
6799edd16368SStephen M. Cameron 	}
680018fce3c4SAlexander Gordeev single_msi_mode:
680155c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
6802050f7147SStephen Cameron 		dev_info(&h->pdev->dev, "MSI capable controller\n");
680355c06c71SStephen M. Cameron 		if (!pci_enable_msi(h->pdev))
6804edd16368SStephen M. Cameron 			h->msi_vector = 1;
6805edd16368SStephen M. Cameron 		else
680655c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "MSI init failed\n");
6807edd16368SStephen M. Cameron 	}
6808edd16368SStephen M. Cameron default_int_mode:
6809edd16368SStephen M. Cameron #endif				/* CONFIG_PCI_MSI */
6810edd16368SStephen M. Cameron 	/* if we get here we're going to use the default interrupt mode */
6811a9a3a273SStephen M. Cameron 	h->intr[h->intr_mode] = h->pdev->irq;
6812edd16368SStephen M. Cameron }
6813edd16368SStephen M. Cameron 
68146f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
6815e5c880d1SStephen M. Cameron {
6816e5c880d1SStephen M. Cameron 	int i;
6817e5c880d1SStephen M. Cameron 	u32 subsystem_vendor_id, subsystem_device_id;
6818e5c880d1SStephen M. Cameron 
6819e5c880d1SStephen M. Cameron 	subsystem_vendor_id = pdev->subsystem_vendor;
6820e5c880d1SStephen M. Cameron 	subsystem_device_id = pdev->subsystem_device;
6821e5c880d1SStephen M. Cameron 	*board_id = ((subsystem_device_id << 16) & 0xffff0000) |
6822e5c880d1SStephen M. Cameron 		    subsystem_vendor_id;
6823e5c880d1SStephen M. Cameron 
6824e5c880d1SStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(products); i++)
6825e5c880d1SStephen M. Cameron 		if (*board_id == products[i].board_id)
6826e5c880d1SStephen M. Cameron 			return i;
6827e5c880d1SStephen M. Cameron 
68286798cc0aSStephen M. Cameron 	if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
68296798cc0aSStephen M. Cameron 		subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
68306798cc0aSStephen M. Cameron 		!hpsa_allow_any) {
6831e5c880d1SStephen M. Cameron 		dev_warn(&pdev->dev, "unrecognized board ID: "
6832e5c880d1SStephen M. Cameron 			"0x%08x, ignoring.\n", *board_id);
6833e5c880d1SStephen M. Cameron 			return -ENODEV;
6834e5c880d1SStephen M. Cameron 	}
6835e5c880d1SStephen M. Cameron 	return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
6836e5c880d1SStephen M. Cameron }
6837e5c880d1SStephen M. Cameron 
68386f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
68393a7774ceSStephen M. Cameron 				    unsigned long *memory_bar)
68403a7774ceSStephen M. Cameron {
68413a7774ceSStephen M. Cameron 	int i;
68423a7774ceSStephen M. Cameron 
68433a7774ceSStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
684412d2cd47SStephen M. Cameron 		if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
68453a7774ceSStephen M. Cameron 			/* addressing mode bits already removed */
684612d2cd47SStephen M. Cameron 			*memory_bar = pci_resource_start(pdev, i);
684712d2cd47SStephen M. Cameron 			dev_dbg(&pdev->dev, "memory BAR = %lx\n",
68483a7774ceSStephen M. Cameron 				*memory_bar);
68493a7774ceSStephen M. Cameron 			return 0;
68503a7774ceSStephen M. Cameron 		}
685112d2cd47SStephen M. Cameron 	dev_warn(&pdev->dev, "no memory BAR found\n");
68523a7774ceSStephen M. Cameron 	return -ENODEV;
68533a7774ceSStephen M. Cameron }
68543a7774ceSStephen M. Cameron 
68556f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
68566f039790SGreg Kroah-Hartman 				     int wait_for_ready)
68572c4c8c8bSStephen M. Cameron {
6858fe5389c8SStephen M. Cameron 	int i, iterations;
68592c4c8c8bSStephen M. Cameron 	u32 scratchpad;
6860fe5389c8SStephen M. Cameron 	if (wait_for_ready)
6861fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_READY_ITERATIONS;
6862fe5389c8SStephen M. Cameron 	else
6863fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
68642c4c8c8bSStephen M. Cameron 
6865fe5389c8SStephen M. Cameron 	for (i = 0; i < iterations; i++) {
6866fe5389c8SStephen M. Cameron 		scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
6867fe5389c8SStephen M. Cameron 		if (wait_for_ready) {
68682c4c8c8bSStephen M. Cameron 			if (scratchpad == HPSA_FIRMWARE_READY)
68692c4c8c8bSStephen M. Cameron 				return 0;
6870fe5389c8SStephen M. Cameron 		} else {
6871fe5389c8SStephen M. Cameron 			if (scratchpad != HPSA_FIRMWARE_READY)
6872fe5389c8SStephen M. Cameron 				return 0;
6873fe5389c8SStephen M. Cameron 		}
68742c4c8c8bSStephen M. Cameron 		msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
68752c4c8c8bSStephen M. Cameron 	}
6876fe5389c8SStephen M. Cameron 	dev_warn(&pdev->dev, "board not ready, timed out.\n");
68772c4c8c8bSStephen M. Cameron 	return -ENODEV;
68782c4c8c8bSStephen M. Cameron }
68792c4c8c8bSStephen M. Cameron 
68806f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
68816f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
6882a51fd47fSStephen M. Cameron 			       u64 *cfg_offset)
6883a51fd47fSStephen M. Cameron {
6884a51fd47fSStephen M. Cameron 	*cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
6885a51fd47fSStephen M. Cameron 	*cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
6886a51fd47fSStephen M. Cameron 	*cfg_base_addr &= (u32) 0x0000ffff;
6887a51fd47fSStephen M. Cameron 	*cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
6888a51fd47fSStephen M. Cameron 	if (*cfg_base_addr_index == -1) {
6889a51fd47fSStephen M. Cameron 		dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
6890a51fd47fSStephen M. Cameron 		return -ENODEV;
6891a51fd47fSStephen M. Cameron 	}
6892a51fd47fSStephen M. Cameron 	return 0;
6893a51fd47fSStephen M. Cameron }
6894a51fd47fSStephen M. Cameron 
6895195f2c65SRobert Elliott static void hpsa_free_cfgtables(struct ctlr_info *h)
6896195f2c65SRobert Elliott {
6897105a3dbcSRobert Elliott 	if (h->transtable) {
6898195f2c65SRobert Elliott 		iounmap(h->transtable);
6899105a3dbcSRobert Elliott 		h->transtable = NULL;
6900105a3dbcSRobert Elliott 	}
6901105a3dbcSRobert Elliott 	if (h->cfgtable) {
6902195f2c65SRobert Elliott 		iounmap(h->cfgtable);
6903105a3dbcSRobert Elliott 		h->cfgtable = NULL;
6904105a3dbcSRobert Elliott 	}
6905195f2c65SRobert Elliott }
6906195f2c65SRobert Elliott 
6907195f2c65SRobert Elliott /* Find and map CISS config table and transfer table
6908195f2c65SRobert Elliott + * several items must be unmapped (freed) later
6909195f2c65SRobert Elliott + * */
69106f039790SGreg Kroah-Hartman static int hpsa_find_cfgtables(struct ctlr_info *h)
6911edd16368SStephen M. Cameron {
691201a02ffcSStephen M. Cameron 	u64 cfg_offset;
691301a02ffcSStephen M. Cameron 	u32 cfg_base_addr;
691401a02ffcSStephen M. Cameron 	u64 cfg_base_addr_index;
6915303932fdSDon Brace 	u32 trans_offset;
6916a51fd47fSStephen M. Cameron 	int rc;
691777c4495cSStephen M. Cameron 
6918a51fd47fSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
6919a51fd47fSStephen M. Cameron 		&cfg_base_addr_index, &cfg_offset);
6920a51fd47fSStephen M. Cameron 	if (rc)
6921a51fd47fSStephen M. Cameron 		return rc;
692277c4495cSStephen M. Cameron 	h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
6923a51fd47fSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
6924cd3c81c4SRobert Elliott 	if (!h->cfgtable) {
6925cd3c81c4SRobert Elliott 		dev_err(&h->pdev->dev, "Failed mapping cfgtable\n");
692677c4495cSStephen M. Cameron 		return -ENOMEM;
6927cd3c81c4SRobert Elliott 	}
6928580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(h->cfgtable);
6929580ada3cSStephen M. Cameron 	if (rc)
6930580ada3cSStephen M. Cameron 		return rc;
693177c4495cSStephen M. Cameron 	/* Find performant mode table. */
6932a51fd47fSStephen M. Cameron 	trans_offset = readl(&h->cfgtable->TransMethodOffset);
693377c4495cSStephen M. Cameron 	h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
693477c4495cSStephen M. Cameron 				cfg_base_addr_index)+cfg_offset+trans_offset,
693577c4495cSStephen M. Cameron 				sizeof(*h->transtable));
6936195f2c65SRobert Elliott 	if (!h->transtable) {
6937195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "Failed mapping transfer table\n");
6938195f2c65SRobert Elliott 		hpsa_free_cfgtables(h);
693977c4495cSStephen M. Cameron 		return -ENOMEM;
6940195f2c65SRobert Elliott 	}
694177c4495cSStephen M. Cameron 	return 0;
694277c4495cSStephen M. Cameron }
694377c4495cSStephen M. Cameron 
69446f039790SGreg Kroah-Hartman static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
6945cba3d38bSStephen M. Cameron {
694641ce4c35SStephen Cameron #define MIN_MAX_COMMANDS 16
694741ce4c35SStephen Cameron 	BUILD_BUG_ON(MIN_MAX_COMMANDS <= HPSA_NRESERVED_CMDS);
694841ce4c35SStephen Cameron 
694941ce4c35SStephen Cameron 	h->max_commands = readl(&h->cfgtable->MaxPerformantModeCommands);
695072ceeaecSStephen M. Cameron 
695172ceeaecSStephen M. Cameron 	/* Limit commands in memory limited kdump scenario. */
695272ceeaecSStephen M. Cameron 	if (reset_devices && h->max_commands > 32)
695372ceeaecSStephen M. Cameron 		h->max_commands = 32;
695472ceeaecSStephen M. Cameron 
695541ce4c35SStephen Cameron 	if (h->max_commands < MIN_MAX_COMMANDS) {
695641ce4c35SStephen Cameron 		dev_warn(&h->pdev->dev,
695741ce4c35SStephen Cameron 			"Controller reports max supported commands of %d Using %d instead. Ensure that firmware is up to date.\n",
695841ce4c35SStephen Cameron 			h->max_commands,
695941ce4c35SStephen Cameron 			MIN_MAX_COMMANDS);
696041ce4c35SStephen Cameron 		h->max_commands = MIN_MAX_COMMANDS;
6961cba3d38bSStephen M. Cameron 	}
6962cba3d38bSStephen M. Cameron }
6963cba3d38bSStephen M. Cameron 
6964c7ee65b3SWebb Scales /* If the controller reports that the total max sg entries is greater than 512,
6965c7ee65b3SWebb Scales  * then we know that chained SG blocks work.  (Original smart arrays did not
6966c7ee65b3SWebb Scales  * support chained SG blocks and would return zero for max sg entries.)
6967c7ee65b3SWebb Scales  */
6968c7ee65b3SWebb Scales static int hpsa_supports_chained_sg_blocks(struct ctlr_info *h)
6969c7ee65b3SWebb Scales {
6970c7ee65b3SWebb Scales 	return h->maxsgentries > 512;
6971c7ee65b3SWebb Scales }
6972c7ee65b3SWebb Scales 
6973b93d7536SStephen M. Cameron /* Interrogate the hardware for some limits:
6974b93d7536SStephen M. Cameron  * max commands, max SG elements without chaining, and with chaining,
6975b93d7536SStephen M. Cameron  * SG chain block size, etc.
6976b93d7536SStephen M. Cameron  */
69776f039790SGreg Kroah-Hartman static void hpsa_find_board_params(struct ctlr_info *h)
6978b93d7536SStephen M. Cameron {
6979cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
698045fcb86eSStephen Cameron 	h->nr_cmds = h->max_commands;
6981b93d7536SStephen M. Cameron 	h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
6982283b4a9bSStephen M. Cameron 	h->fw_support = readl(&(h->cfgtable->misc_fw_support));
6983c7ee65b3SWebb Scales 	if (hpsa_supports_chained_sg_blocks(h)) {
6984c7ee65b3SWebb Scales 		/* Limit in-command s/g elements to 32 save dma'able memory. */
6985b93d7536SStephen M. Cameron 		h->max_cmd_sg_entries = 32;
69861a63ea6fSWebb Scales 		h->chainsize = h->maxsgentries - h->max_cmd_sg_entries;
6987b93d7536SStephen M. Cameron 		h->maxsgentries--; /* save one for chain pointer */
6988b93d7536SStephen M. Cameron 	} else {
6989c7ee65b3SWebb Scales 		/*
6990c7ee65b3SWebb Scales 		 * Original smart arrays supported at most 31 s/g entries
6991c7ee65b3SWebb Scales 		 * embedded inline in the command (trying to use more
6992c7ee65b3SWebb Scales 		 * would lock up the controller)
6993c7ee65b3SWebb Scales 		 */
6994c7ee65b3SWebb Scales 		h->max_cmd_sg_entries = 31;
69951a63ea6fSWebb Scales 		h->maxsgentries = 31; /* default to traditional values */
6996c7ee65b3SWebb Scales 		h->chainsize = 0;
6997b93d7536SStephen M. Cameron 	}
699875167d2cSStephen M. Cameron 
699975167d2cSStephen M. Cameron 	/* Find out what task management functions are supported and cache */
700075167d2cSStephen M. Cameron 	h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
70010e7a7fceSScott Teel 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
70020e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
70030e7a7fceSScott Teel 	if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
70040e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
70058be986ccSStephen Cameron 	if (!(HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags))
70068be986ccSStephen Cameron 		dev_warn(&h->pdev->dev, "HP SSD Smart Path aborts not supported\n");
7007b93d7536SStephen M. Cameron }
7008b93d7536SStephen M. Cameron 
700976c46e49SStephen M. Cameron static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
701076c46e49SStephen M. Cameron {
70110fc9fd40SAkinobu Mita 	if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
7012050f7147SStephen Cameron 		dev_err(&h->pdev->dev, "not a valid CISS config table\n");
701376c46e49SStephen M. Cameron 		return false;
701476c46e49SStephen M. Cameron 	}
701576c46e49SStephen M. Cameron 	return true;
701676c46e49SStephen M. Cameron }
701776c46e49SStephen M. Cameron 
701897a5e98cSStephen M. Cameron static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
7019f7c39101SStephen M. Cameron {
702097a5e98cSStephen M. Cameron 	u32 driver_support;
7021f7c39101SStephen M. Cameron 
702297a5e98cSStephen M. Cameron 	driver_support = readl(&(h->cfgtable->driver_support));
70230b9e7b74SArnd Bergmann 	/* Need to enable prefetch in the SCSI core for 6400 in x86 */
70240b9e7b74SArnd Bergmann #ifdef CONFIG_X86
702597a5e98cSStephen M. Cameron 	driver_support |= ENABLE_SCSI_PREFETCH;
7026f7c39101SStephen M. Cameron #endif
702728e13446SStephen M. Cameron 	driver_support |= ENABLE_UNIT_ATTN;
702828e13446SStephen M. Cameron 	writel(driver_support, &(h->cfgtable->driver_support));
7029f7c39101SStephen M. Cameron }
7030f7c39101SStephen M. Cameron 
70313d0eab67SStephen M. Cameron /* Disable DMA prefetch for the P600.  Otherwise an ASIC bug may result
70323d0eab67SStephen M. Cameron  * in a prefetch beyond physical memory.
70333d0eab67SStephen M. Cameron  */
70343d0eab67SStephen M. Cameron static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
70353d0eab67SStephen M. Cameron {
70363d0eab67SStephen M. Cameron 	u32 dma_prefetch;
70373d0eab67SStephen M. Cameron 
70383d0eab67SStephen M. Cameron 	if (h->board_id != 0x3225103C)
70393d0eab67SStephen M. Cameron 		return;
70403d0eab67SStephen M. Cameron 	dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
70413d0eab67SStephen M. Cameron 	dma_prefetch |= 0x8000;
70423d0eab67SStephen M. Cameron 	writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
70433d0eab67SStephen M. Cameron }
70443d0eab67SStephen M. Cameron 
7045c706a795SRobert Elliott static int hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
704676438d08SStephen M. Cameron {
704776438d08SStephen M. Cameron 	int i;
704876438d08SStephen M. Cameron 	u32 doorbell_value;
704976438d08SStephen M. Cameron 	unsigned long flags;
705076438d08SStephen M. Cameron 	/* wait until the clear_event_notify bit 6 is cleared by controller. */
7051007e7aa9SRobert Elliott 	for (i = 0; i < MAX_CLEAR_EVENT_WAIT; i++) {
705276438d08SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
705376438d08SStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
705476438d08SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
705576438d08SStephen M. Cameron 		if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
7056c706a795SRobert Elliott 			goto done;
705776438d08SStephen M. Cameron 		/* delay and try again */
7058007e7aa9SRobert Elliott 		msleep(CLEAR_EVENT_WAIT_INTERVAL);
705976438d08SStephen M. Cameron 	}
7060c706a795SRobert Elliott 	return -ENODEV;
7061c706a795SRobert Elliott done:
7062c706a795SRobert Elliott 	return 0;
706376438d08SStephen M. Cameron }
706476438d08SStephen M. Cameron 
7065c706a795SRobert Elliott static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
7066eb6b2ae9SStephen M. Cameron {
7067eb6b2ae9SStephen M. Cameron 	int i;
70686eaf46fdSStephen M. Cameron 	u32 doorbell_value;
70696eaf46fdSStephen M. Cameron 	unsigned long flags;
7070eb6b2ae9SStephen M. Cameron 
7071eb6b2ae9SStephen M. Cameron 	/* under certain very rare conditions, this can take awhile.
7072eb6b2ae9SStephen M. Cameron 	 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
7073eb6b2ae9SStephen M. Cameron 	 * as we enter this code.)
7074eb6b2ae9SStephen M. Cameron 	 */
7075007e7aa9SRobert Elliott 	for (i = 0; i < MAX_MODE_CHANGE_WAIT; i++) {
707625163bd5SWebb Scales 		if (h->remove_in_progress)
707725163bd5SWebb Scales 			goto done;
70786eaf46fdSStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
70796eaf46fdSStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
70806eaf46fdSStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
7081382be668SDan Carpenter 		if (!(doorbell_value & CFGTBL_ChangeReq))
7082c706a795SRobert Elliott 			goto done;
7083eb6b2ae9SStephen M. Cameron 		/* delay and try again */
7084007e7aa9SRobert Elliott 		msleep(MODE_CHANGE_WAIT_INTERVAL);
7085eb6b2ae9SStephen M. Cameron 	}
7086c706a795SRobert Elliott 	return -ENODEV;
7087c706a795SRobert Elliott done:
7088c706a795SRobert Elliott 	return 0;
70893f4336f3SStephen M. Cameron }
70903f4336f3SStephen M. Cameron 
7091c706a795SRobert Elliott /* return -ENODEV or other reason on error, 0 on success */
70926f039790SGreg Kroah-Hartman static int hpsa_enter_simple_mode(struct ctlr_info *h)
70933f4336f3SStephen M. Cameron {
70943f4336f3SStephen M. Cameron 	u32 trans_support;
70953f4336f3SStephen M. Cameron 
70963f4336f3SStephen M. Cameron 	trans_support = readl(&(h->cfgtable->TransportSupport));
70973f4336f3SStephen M. Cameron 	if (!(trans_support & SIMPLE_MODE))
70983f4336f3SStephen M. Cameron 		return -ENOTSUPP;
70993f4336f3SStephen M. Cameron 
71003f4336f3SStephen M. Cameron 	h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
7101283b4a9bSStephen M. Cameron 
71023f4336f3SStephen M. Cameron 	/* Update the field, and then ring the doorbell */
71033f4336f3SStephen M. Cameron 	writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
7104b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
71053f4336f3SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
7106c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h))
7107c706a795SRobert Elliott 		goto error;
7108eb6b2ae9SStephen M. Cameron 	print_cfg_table(&h->pdev->dev, h->cfgtable);
7109283b4a9bSStephen M. Cameron 	if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
7110283b4a9bSStephen M. Cameron 		goto error;
7111960a30e7SStephen M. Cameron 	h->transMethod = CFGTBL_Trans_Simple;
7112eb6b2ae9SStephen M. Cameron 	return 0;
7113283b4a9bSStephen M. Cameron error:
7114050f7147SStephen Cameron 	dev_err(&h->pdev->dev, "failed to enter simple mode\n");
7115283b4a9bSStephen M. Cameron 	return -ENODEV;
7116eb6b2ae9SStephen M. Cameron }
7117eb6b2ae9SStephen M. Cameron 
7118195f2c65SRobert Elliott /* free items allocated or mapped by hpsa_pci_init */
7119195f2c65SRobert Elliott static void hpsa_free_pci_init(struct ctlr_info *h)
7120195f2c65SRobert Elliott {
7121195f2c65SRobert Elliott 	hpsa_free_cfgtables(h);			/* pci_init 4 */
7122195f2c65SRobert Elliott 	iounmap(h->vaddr);			/* pci_init 3 */
7123105a3dbcSRobert Elliott 	h->vaddr = NULL;
7124195f2c65SRobert Elliott 	hpsa_disable_interrupt_mode(h);		/* pci_init 2 */
7125943a7021SRobert Elliott 	/*
7126943a7021SRobert Elliott 	 * call pci_disable_device before pci_release_regions per
7127943a7021SRobert Elliott 	 * Documentation/PCI/pci.txt
7128943a7021SRobert Elliott 	 */
7129195f2c65SRobert Elliott 	pci_disable_device(h->pdev);		/* pci_init 1 */
7130943a7021SRobert Elliott 	pci_release_regions(h->pdev);		/* pci_init 2 */
7131195f2c65SRobert Elliott }
7132195f2c65SRobert Elliott 
7133195f2c65SRobert Elliott /* several items must be freed later */
71346f039790SGreg Kroah-Hartman static int hpsa_pci_init(struct ctlr_info *h)
713577c4495cSStephen M. Cameron {
7136eb6b2ae9SStephen M. Cameron 	int prod_index, err;
7137edd16368SStephen M. Cameron 
7138e5c880d1SStephen M. Cameron 	prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
7139e5c880d1SStephen M. Cameron 	if (prod_index < 0)
714060f923b9SRobert Elliott 		return prod_index;
7141e5c880d1SStephen M. Cameron 	h->product_name = products[prod_index].product_name;
7142e5c880d1SStephen M. Cameron 	h->access = *(products[prod_index].access);
7143e5c880d1SStephen M. Cameron 
71449b5c48c2SStephen Cameron 	h->needs_abort_tags_swizzled =
71459b5c48c2SStephen Cameron 		ctlr_needs_abort_tags_swizzled(h->board_id);
71469b5c48c2SStephen Cameron 
7147e5a44df8SMatthew Garrett 	pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
7148e5a44df8SMatthew Garrett 			       PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
7149e5a44df8SMatthew Garrett 
715055c06c71SStephen M. Cameron 	err = pci_enable_device(h->pdev);
7151edd16368SStephen M. Cameron 	if (err) {
7152195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "failed to enable PCI device\n");
7153943a7021SRobert Elliott 		pci_disable_device(h->pdev);
7154edd16368SStephen M. Cameron 		return err;
7155edd16368SStephen M. Cameron 	}
7156edd16368SStephen M. Cameron 
7157f79cfec6SStephen M. Cameron 	err = pci_request_regions(h->pdev, HPSA);
7158edd16368SStephen M. Cameron 	if (err) {
715955c06c71SStephen M. Cameron 		dev_err(&h->pdev->dev,
7160195f2c65SRobert Elliott 			"failed to obtain PCI resources\n");
7161943a7021SRobert Elliott 		pci_disable_device(h->pdev);
7162943a7021SRobert Elliott 		return err;
7163edd16368SStephen M. Cameron 	}
71644fa604e1SRobert Elliott 
71654fa604e1SRobert Elliott 	pci_set_master(h->pdev);
71664fa604e1SRobert Elliott 
71676b3f4c52SStephen M. Cameron 	hpsa_interrupt_mode(h);
716812d2cd47SStephen M. Cameron 	err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
71693a7774ceSStephen M. Cameron 	if (err)
7170195f2c65SRobert Elliott 		goto clean2;	/* intmode+region, pci */
7171edd16368SStephen M. Cameron 	h->vaddr = remap_pci_mem(h->paddr, 0x250);
7172204892e9SStephen M. Cameron 	if (!h->vaddr) {
7173195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "failed to remap PCI mem\n");
7174204892e9SStephen M. Cameron 		err = -ENOMEM;
7175195f2c65SRobert Elliott 		goto clean2;	/* intmode+region, pci */
7176204892e9SStephen M. Cameron 	}
7177fe5389c8SStephen M. Cameron 	err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
71782c4c8c8bSStephen M. Cameron 	if (err)
7179195f2c65SRobert Elliott 		goto clean3;	/* vaddr, intmode+region, pci */
718077c4495cSStephen M. Cameron 	err = hpsa_find_cfgtables(h);
718177c4495cSStephen M. Cameron 	if (err)
7182195f2c65SRobert Elliott 		goto clean3;	/* vaddr, intmode+region, pci */
7183b93d7536SStephen M. Cameron 	hpsa_find_board_params(h);
7184edd16368SStephen M. Cameron 
718576c46e49SStephen M. Cameron 	if (!hpsa_CISS_signature_present(h)) {
7186edd16368SStephen M. Cameron 		err = -ENODEV;
7187195f2c65SRobert Elliott 		goto clean4;	/* cfgtables, vaddr, intmode+region, pci */
7188edd16368SStephen M. Cameron 	}
718997a5e98cSStephen M. Cameron 	hpsa_set_driver_support_bits(h);
71903d0eab67SStephen M. Cameron 	hpsa_p600_dma_prefetch_quirk(h);
7191eb6b2ae9SStephen M. Cameron 	err = hpsa_enter_simple_mode(h);
7192eb6b2ae9SStephen M. Cameron 	if (err)
7193195f2c65SRobert Elliott 		goto clean4;	/* cfgtables, vaddr, intmode+region, pci */
7194edd16368SStephen M. Cameron 	return 0;
7195edd16368SStephen M. Cameron 
7196195f2c65SRobert Elliott clean4:	/* cfgtables, vaddr, intmode+region, pci */
7197195f2c65SRobert Elliott 	hpsa_free_cfgtables(h);
7198195f2c65SRobert Elliott clean3:	/* vaddr, intmode+region, pci */
7199204892e9SStephen M. Cameron 	iounmap(h->vaddr);
7200105a3dbcSRobert Elliott 	h->vaddr = NULL;
7201195f2c65SRobert Elliott clean2:	/* intmode+region, pci */
7202195f2c65SRobert Elliott 	hpsa_disable_interrupt_mode(h);
7203943a7021SRobert Elliott 	/*
7204943a7021SRobert Elliott 	 * call pci_disable_device before pci_release_regions per
7205943a7021SRobert Elliott 	 * Documentation/PCI/pci.txt
7206943a7021SRobert Elliott 	 */
7207195f2c65SRobert Elliott 	pci_disable_device(h->pdev);
7208943a7021SRobert Elliott 	pci_release_regions(h->pdev);
7209edd16368SStephen M. Cameron 	return err;
7210edd16368SStephen M. Cameron }
7211edd16368SStephen M. Cameron 
72126f039790SGreg Kroah-Hartman static void hpsa_hba_inquiry(struct ctlr_info *h)
7213339b2b14SStephen M. Cameron {
7214339b2b14SStephen M. Cameron 	int rc;
7215339b2b14SStephen M. Cameron 
7216339b2b14SStephen M. Cameron #define HBA_INQUIRY_BYTE_COUNT 64
7217339b2b14SStephen M. Cameron 	h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
7218339b2b14SStephen M. Cameron 	if (!h->hba_inquiry_data)
7219339b2b14SStephen M. Cameron 		return;
7220339b2b14SStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
7221339b2b14SStephen M. Cameron 		h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
7222339b2b14SStephen M. Cameron 	if (rc != 0) {
7223339b2b14SStephen M. Cameron 		kfree(h->hba_inquiry_data);
7224339b2b14SStephen M. Cameron 		h->hba_inquiry_data = NULL;
7225339b2b14SStephen M. Cameron 	}
7226339b2b14SStephen M. Cameron }
7227339b2b14SStephen M. Cameron 
72286b6c1cd7STomas Henzl static int hpsa_init_reset_devices(struct pci_dev *pdev, u32 board_id)
7229edd16368SStephen M. Cameron {
72301df8552aSStephen M. Cameron 	int rc, i;
72313b747298STomas Henzl 	void __iomem *vaddr;
7232edd16368SStephen M. Cameron 
72334c2a8c40SStephen M. Cameron 	if (!reset_devices)
72344c2a8c40SStephen M. Cameron 		return 0;
72354c2a8c40SStephen M. Cameron 
7236132aa220STomas Henzl 	/* kdump kernel is loading, we don't know in which state is
7237132aa220STomas Henzl 	 * the pci interface. The dev->enable_cnt is equal zero
7238132aa220STomas Henzl 	 * so we call enable+disable, wait a while and switch it on.
7239132aa220STomas Henzl 	 */
7240132aa220STomas Henzl 	rc = pci_enable_device(pdev);
7241132aa220STomas Henzl 	if (rc) {
7242132aa220STomas Henzl 		dev_warn(&pdev->dev, "Failed to enable PCI device\n");
7243132aa220STomas Henzl 		return -ENODEV;
7244132aa220STomas Henzl 	}
7245132aa220STomas Henzl 	pci_disable_device(pdev);
7246132aa220STomas Henzl 	msleep(260);			/* a randomly chosen number */
7247132aa220STomas Henzl 	rc = pci_enable_device(pdev);
7248132aa220STomas Henzl 	if (rc) {
7249132aa220STomas Henzl 		dev_warn(&pdev->dev, "failed to enable device.\n");
7250132aa220STomas Henzl 		return -ENODEV;
7251132aa220STomas Henzl 	}
72524fa604e1SRobert Elliott 
7253859c75abSTomas Henzl 	pci_set_master(pdev);
72544fa604e1SRobert Elliott 
72553b747298STomas Henzl 	vaddr = pci_ioremap_bar(pdev, 0);
72563b747298STomas Henzl 	if (vaddr == NULL) {
72573b747298STomas Henzl 		rc = -ENOMEM;
72583b747298STomas Henzl 		goto out_disable;
72593b747298STomas Henzl 	}
72603b747298STomas Henzl 	writel(SA5_INTR_OFF, vaddr + SA5_REPLY_INTR_MASK_OFFSET);
72613b747298STomas Henzl 	iounmap(vaddr);
72623b747298STomas Henzl 
72631df8552aSStephen M. Cameron 	/* Reset the controller with a PCI power-cycle or via doorbell */
72646b6c1cd7STomas Henzl 	rc = hpsa_kdump_hard_reset_controller(pdev, board_id);
7265edd16368SStephen M. Cameron 
72661df8552aSStephen M. Cameron 	/* -ENOTSUPP here means we cannot reset the controller
72671df8552aSStephen M. Cameron 	 * but it's already (and still) up and running in
726818867659SStephen M. Cameron 	 * "performant mode".  Or, it might be 640x, which can't reset
726918867659SStephen M. Cameron 	 * due to concerns about shared bbwc between 6402/6404 pair.
72701df8552aSStephen M. Cameron 	 */
7271adf1b3a3SRobert Elliott 	if (rc)
7272132aa220STomas Henzl 		goto out_disable;
7273edd16368SStephen M. Cameron 
7274edd16368SStephen M. Cameron 	/* Now try to get the controller to respond to a no-op */
72751ba66c9cSRobert Elliott 	dev_info(&pdev->dev, "Waiting for controller to respond to no-op\n");
7276edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
7277edd16368SStephen M. Cameron 		if (hpsa_noop(pdev) == 0)
7278edd16368SStephen M. Cameron 			break;
7279edd16368SStephen M. Cameron 		else
7280edd16368SStephen M. Cameron 			dev_warn(&pdev->dev, "no-op failed%s\n",
7281edd16368SStephen M. Cameron 					(i < 11 ? "; re-trying" : ""));
7282edd16368SStephen M. Cameron 	}
7283132aa220STomas Henzl 
7284132aa220STomas Henzl out_disable:
7285132aa220STomas Henzl 
7286132aa220STomas Henzl 	pci_disable_device(pdev);
7287132aa220STomas Henzl 	return rc;
7288edd16368SStephen M. Cameron }
7289edd16368SStephen M. Cameron 
72901fb7c98aSRobert Elliott static void hpsa_free_cmd_pool(struct ctlr_info *h)
72911fb7c98aSRobert Elliott {
72921fb7c98aSRobert Elliott 	kfree(h->cmd_pool_bits);
7293105a3dbcSRobert Elliott 	h->cmd_pool_bits = NULL;
7294105a3dbcSRobert Elliott 	if (h->cmd_pool) {
72951fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
72961fb7c98aSRobert Elliott 				h->nr_cmds * sizeof(struct CommandList),
72971fb7c98aSRobert Elliott 				h->cmd_pool,
72981fb7c98aSRobert Elliott 				h->cmd_pool_dhandle);
7299105a3dbcSRobert Elliott 		h->cmd_pool = NULL;
7300105a3dbcSRobert Elliott 		h->cmd_pool_dhandle = 0;
7301105a3dbcSRobert Elliott 	}
7302105a3dbcSRobert Elliott 	if (h->errinfo_pool) {
73031fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
73041fb7c98aSRobert Elliott 				h->nr_cmds * sizeof(struct ErrorInfo),
73051fb7c98aSRobert Elliott 				h->errinfo_pool,
73061fb7c98aSRobert Elliott 				h->errinfo_pool_dhandle);
7307105a3dbcSRobert Elliott 		h->errinfo_pool = NULL;
7308105a3dbcSRobert Elliott 		h->errinfo_pool_dhandle = 0;
7309105a3dbcSRobert Elliott 	}
73101fb7c98aSRobert Elliott }
73111fb7c98aSRobert Elliott 
7312d37ffbe4SRobert Elliott static int hpsa_alloc_cmd_pool(struct ctlr_info *h)
73132e9d1b36SStephen M. Cameron {
73142e9d1b36SStephen M. Cameron 	h->cmd_pool_bits = kzalloc(
73152e9d1b36SStephen M. Cameron 		DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
73162e9d1b36SStephen M. Cameron 		sizeof(unsigned long), GFP_KERNEL);
73172e9d1b36SStephen M. Cameron 	h->cmd_pool = pci_alloc_consistent(h->pdev,
73182e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->cmd_pool),
73192e9d1b36SStephen M. Cameron 		    &(h->cmd_pool_dhandle));
73202e9d1b36SStephen M. Cameron 	h->errinfo_pool = pci_alloc_consistent(h->pdev,
73212e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->errinfo_pool),
73222e9d1b36SStephen M. Cameron 		    &(h->errinfo_pool_dhandle));
73232e9d1b36SStephen M. Cameron 	if ((h->cmd_pool_bits == NULL)
73242e9d1b36SStephen M. Cameron 	    || (h->cmd_pool == NULL)
73252e9d1b36SStephen M. Cameron 	    || (h->errinfo_pool == NULL)) {
73262e9d1b36SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory in %s", __func__);
73272c143342SRobert Elliott 		goto clean_up;
73282e9d1b36SStephen M. Cameron 	}
7329360c73bdSStephen Cameron 	hpsa_preinitialize_commands(h);
73302e9d1b36SStephen M. Cameron 	return 0;
73312c143342SRobert Elliott clean_up:
73322c143342SRobert Elliott 	hpsa_free_cmd_pool(h);
73332c143342SRobert Elliott 	return -ENOMEM;
73342e9d1b36SStephen M. Cameron }
73352e9d1b36SStephen M. Cameron 
733641b3cf08SStephen M. Cameron static void hpsa_irq_affinity_hints(struct ctlr_info *h)
733741b3cf08SStephen M. Cameron {
7338ec429952SFabian Frederick 	int i, cpu;
733941b3cf08SStephen M. Cameron 
734041b3cf08SStephen M. Cameron 	cpu = cpumask_first(cpu_online_mask);
734141b3cf08SStephen M. Cameron 	for (i = 0; i < h->msix_vector; i++) {
7342ec429952SFabian Frederick 		irq_set_affinity_hint(h->intr[i], get_cpu_mask(cpu));
734341b3cf08SStephen M. Cameron 		cpu = cpumask_next(cpu, cpu_online_mask);
734441b3cf08SStephen M. Cameron 	}
734541b3cf08SStephen M. Cameron }
734641b3cf08SStephen M. Cameron 
7347ec501a18SRobert Elliott /* clear affinity hints and free MSI-X, MSI, or legacy INTx vectors */
7348ec501a18SRobert Elliott static void hpsa_free_irqs(struct ctlr_info *h)
7349ec501a18SRobert Elliott {
7350ec501a18SRobert Elliott 	int i;
7351ec501a18SRobert Elliott 
7352ec501a18SRobert Elliott 	if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
7353ec501a18SRobert Elliott 		/* Single reply queue, only one irq to free */
7354ec501a18SRobert Elliott 		i = h->intr_mode;
7355ec501a18SRobert Elliott 		irq_set_affinity_hint(h->intr[i], NULL);
7356ec501a18SRobert Elliott 		free_irq(h->intr[i], &h->q[i]);
7357105a3dbcSRobert Elliott 		h->q[i] = 0;
7358ec501a18SRobert Elliott 		return;
7359ec501a18SRobert Elliott 	}
7360ec501a18SRobert Elliott 
7361ec501a18SRobert Elliott 	for (i = 0; i < h->msix_vector; i++) {
7362ec501a18SRobert Elliott 		irq_set_affinity_hint(h->intr[i], NULL);
7363ec501a18SRobert Elliott 		free_irq(h->intr[i], &h->q[i]);
7364105a3dbcSRobert Elliott 		h->q[i] = 0;
7365ec501a18SRobert Elliott 	}
7366a4e17fc1SRobert Elliott 	for (; i < MAX_REPLY_QUEUES; i++)
7367a4e17fc1SRobert Elliott 		h->q[i] = 0;
7368ec501a18SRobert Elliott }
7369ec501a18SRobert Elliott 
73709ee61794SRobert Elliott /* returns 0 on success; cleans up and returns -Enn on error */
73719ee61794SRobert Elliott static int hpsa_request_irqs(struct ctlr_info *h,
73720ae01a32SStephen M. Cameron 	irqreturn_t (*msixhandler)(int, void *),
73730ae01a32SStephen M. Cameron 	irqreturn_t (*intxhandler)(int, void *))
73740ae01a32SStephen M. Cameron {
7375254f796bSMatt Gates 	int rc, i;
73760ae01a32SStephen M. Cameron 
7377254f796bSMatt Gates 	/*
7378254f796bSMatt Gates 	 * initialize h->q[x] = x so that interrupt handlers know which
7379254f796bSMatt Gates 	 * queue to process.
7380254f796bSMatt Gates 	 */
7381254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++)
7382254f796bSMatt Gates 		h->q[i] = (u8) i;
7383254f796bSMatt Gates 
7384eee0f03aSHannes Reinecke 	if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
7385254f796bSMatt Gates 		/* If performant mode and MSI-X, use multiple reply queues */
7386a4e17fc1SRobert Elliott 		for (i = 0; i < h->msix_vector; i++) {
73878b47004aSRobert Elliott 			sprintf(h->intrname[i], "%s-msix%d", h->devname, i);
7388254f796bSMatt Gates 			rc = request_irq(h->intr[i], msixhandler,
73898b47004aSRobert Elliott 					0, h->intrname[i],
7390254f796bSMatt Gates 					&h->q[i]);
7391a4e17fc1SRobert Elliott 			if (rc) {
7392a4e17fc1SRobert Elliott 				int j;
7393a4e17fc1SRobert Elliott 
7394a4e17fc1SRobert Elliott 				dev_err(&h->pdev->dev,
7395a4e17fc1SRobert Elliott 					"failed to get irq %d for %s\n",
7396a4e17fc1SRobert Elliott 				       h->intr[i], h->devname);
7397a4e17fc1SRobert Elliott 				for (j = 0; j < i; j++) {
7398a4e17fc1SRobert Elliott 					free_irq(h->intr[j], &h->q[j]);
7399a4e17fc1SRobert Elliott 					h->q[j] = 0;
7400a4e17fc1SRobert Elliott 				}
7401a4e17fc1SRobert Elliott 				for (; j < MAX_REPLY_QUEUES; j++)
7402a4e17fc1SRobert Elliott 					h->q[j] = 0;
7403a4e17fc1SRobert Elliott 				return rc;
7404a4e17fc1SRobert Elliott 			}
7405a4e17fc1SRobert Elliott 		}
740641b3cf08SStephen M. Cameron 		hpsa_irq_affinity_hints(h);
7407254f796bSMatt Gates 	} else {
7408254f796bSMatt Gates 		/* Use single reply pool */
7409eee0f03aSHannes Reinecke 		if (h->msix_vector > 0 || h->msi_vector) {
74108b47004aSRobert Elliott 			if (h->msix_vector)
74118b47004aSRobert Elliott 				sprintf(h->intrname[h->intr_mode],
74128b47004aSRobert Elliott 					"%s-msix", h->devname);
74138b47004aSRobert Elliott 			else
74148b47004aSRobert Elliott 				sprintf(h->intrname[h->intr_mode],
74158b47004aSRobert Elliott 					"%s-msi", h->devname);
7416254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
74178b47004aSRobert Elliott 				msixhandler, 0,
74188b47004aSRobert Elliott 				h->intrname[h->intr_mode],
7419254f796bSMatt Gates 				&h->q[h->intr_mode]);
7420254f796bSMatt Gates 		} else {
74218b47004aSRobert Elliott 			sprintf(h->intrname[h->intr_mode],
74228b47004aSRobert Elliott 				"%s-intx", h->devname);
7423254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
74248b47004aSRobert Elliott 				intxhandler, IRQF_SHARED,
74258b47004aSRobert Elliott 				h->intrname[h->intr_mode],
7426254f796bSMatt Gates 				&h->q[h->intr_mode]);
7427254f796bSMatt Gates 		}
7428105a3dbcSRobert Elliott 		irq_set_affinity_hint(h->intr[h->intr_mode], NULL);
7429254f796bSMatt Gates 	}
74300ae01a32SStephen M. Cameron 	if (rc) {
7431195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "failed to get irq %d for %s\n",
74320ae01a32SStephen M. Cameron 		       h->intr[h->intr_mode], h->devname);
7433195f2c65SRobert Elliott 		hpsa_free_irqs(h);
74340ae01a32SStephen M. Cameron 		return -ENODEV;
74350ae01a32SStephen M. Cameron 	}
74360ae01a32SStephen M. Cameron 	return 0;
74370ae01a32SStephen M. Cameron }
74380ae01a32SStephen M. Cameron 
74396f039790SGreg Kroah-Hartman static int hpsa_kdump_soft_reset(struct ctlr_info *h)
744064670ac8SStephen M. Cameron {
7441*39c53f55SRobert Elliott 	int rc;
7442bf43caf3SRobert Elliott 	hpsa_send_host_reset(h, RAID_CTLR_LUNID, HPSA_RESET_TYPE_CONTROLLER);
744364670ac8SStephen M. Cameron 
744464670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
7445*39c53f55SRobert Elliott 	rc = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY);
7446*39c53f55SRobert Elliott 	if (rc) {
744764670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
7448*39c53f55SRobert Elliott 		return rc;
744964670ac8SStephen M. Cameron 	}
745064670ac8SStephen M. Cameron 
745164670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
7452*39c53f55SRobert Elliott 	rc = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
7453*39c53f55SRobert Elliott 	if (rc) {
745464670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Board failed to become ready "
745564670ac8SStephen M. Cameron 			"after soft reset.\n");
7456*39c53f55SRobert Elliott 		return rc;
745764670ac8SStephen M. Cameron 	}
745864670ac8SStephen M. Cameron 
745964670ac8SStephen M. Cameron 	return 0;
746064670ac8SStephen M. Cameron }
746164670ac8SStephen M. Cameron 
7462072b0518SStephen M. Cameron static void hpsa_free_reply_queues(struct ctlr_info *h)
7463072b0518SStephen M. Cameron {
7464072b0518SStephen M. Cameron 	int i;
7465072b0518SStephen M. Cameron 
7466072b0518SStephen M. Cameron 	for (i = 0; i < h->nreply_queues; i++) {
7467072b0518SStephen M. Cameron 		if (!h->reply_queue[i].head)
7468072b0518SStephen M. Cameron 			continue;
74691fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
74701fb7c98aSRobert Elliott 					h->reply_queue_size,
74711fb7c98aSRobert Elliott 					h->reply_queue[i].head,
74721fb7c98aSRobert Elliott 					h->reply_queue[i].busaddr);
7473072b0518SStephen M. Cameron 		h->reply_queue[i].head = NULL;
7474072b0518SStephen M. Cameron 		h->reply_queue[i].busaddr = 0;
7475072b0518SStephen M. Cameron 	}
7476105a3dbcSRobert Elliott 	h->reply_queue_size = 0;
7477072b0518SStephen M. Cameron }
7478072b0518SStephen M. Cameron 
74790097f0f4SStephen M. Cameron static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
74800097f0f4SStephen M. Cameron {
7481105a3dbcSRobert Elliott 	hpsa_free_performant_mode(h);		/* init_one 7 */
7482105a3dbcSRobert Elliott 	hpsa_free_sg_chain_blocks(h);		/* init_one 6 */
7483105a3dbcSRobert Elliott 	hpsa_free_cmd_pool(h);			/* init_one 5 */
7484105a3dbcSRobert Elliott 	hpsa_free_irqs(h);			/* init_one 4 */
74852946e82bSRobert Elliott 	scsi_host_put(h->scsi_host);		/* init_one 3 */
74862946e82bSRobert Elliott 	h->scsi_host = NULL;			/* init_one 3 */
74872946e82bSRobert Elliott 	hpsa_free_pci_init(h);			/* init_one 2_5 */
74889ecd953aSRobert Elliott 	free_percpu(h->lockup_detected);	/* init_one 2 */
74899ecd953aSRobert Elliott 	h->lockup_detected = NULL;		/* init_one 2 */
74909ecd953aSRobert Elliott 	if (h->resubmit_wq) {
74919ecd953aSRobert Elliott 		destroy_workqueue(h->resubmit_wq);	/* init_one 1 */
74929ecd953aSRobert Elliott 		h->resubmit_wq = NULL;
74939ecd953aSRobert Elliott 	}
74949ecd953aSRobert Elliott 	if (h->rescan_ctlr_wq) {
74959ecd953aSRobert Elliott 		destroy_workqueue(h->rescan_ctlr_wq);
74969ecd953aSRobert Elliott 		h->rescan_ctlr_wq = NULL;
74979ecd953aSRobert Elliott 	}
7498105a3dbcSRobert Elliott 	kfree(h);				/* init_one 1 */
749964670ac8SStephen M. Cameron }
750064670ac8SStephen M. Cameron 
7501a0c12413SStephen M. Cameron /* Called when controller lockup detected. */
7502f2405db8SDon Brace static void fail_all_outstanding_cmds(struct ctlr_info *h)
7503a0c12413SStephen M. Cameron {
7504281a7fd0SWebb Scales 	int i, refcount;
7505281a7fd0SWebb Scales 	struct CommandList *c;
750625163bd5SWebb Scales 	int failcount = 0;
7507a0c12413SStephen M. Cameron 
7508080ef1ccSDon Brace 	flush_workqueue(h->resubmit_wq); /* ensure all cmds are fully built */
7509f2405db8SDon Brace 	for (i = 0; i < h->nr_cmds; i++) {
7510f2405db8SDon Brace 		c = h->cmd_pool + i;
7511281a7fd0SWebb Scales 		refcount = atomic_inc_return(&c->refcount);
7512281a7fd0SWebb Scales 		if (refcount > 1) {
751325163bd5SWebb Scales 			c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
75145a3d16f5SStephen M. Cameron 			finish_cmd(c);
7515433b5f4dSStephen Cameron 			atomic_dec(&h->commands_outstanding);
751625163bd5SWebb Scales 			failcount++;
7517a0c12413SStephen M. Cameron 		}
7518281a7fd0SWebb Scales 		cmd_free(h, c);
7519281a7fd0SWebb Scales 	}
752025163bd5SWebb Scales 	dev_warn(&h->pdev->dev,
752125163bd5SWebb Scales 		"failed %d commands in fail_all\n", failcount);
7522a0c12413SStephen M. Cameron }
7523a0c12413SStephen M. Cameron 
7524094963daSStephen M. Cameron static void set_lockup_detected_for_all_cpus(struct ctlr_info *h, u32 value)
7525094963daSStephen M. Cameron {
7526c8ed0010SRusty Russell 	int cpu;
7527094963daSStephen M. Cameron 
7528c8ed0010SRusty Russell 	for_each_online_cpu(cpu) {
7529094963daSStephen M. Cameron 		u32 *lockup_detected;
7530094963daSStephen M. Cameron 		lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
7531094963daSStephen M. Cameron 		*lockup_detected = value;
7532094963daSStephen M. Cameron 	}
7533094963daSStephen M. Cameron 	wmb(); /* be sure the per-cpu variables are out to memory */
7534094963daSStephen M. Cameron }
7535094963daSStephen M. Cameron 
7536a0c12413SStephen M. Cameron static void controller_lockup_detected(struct ctlr_info *h)
7537a0c12413SStephen M. Cameron {
7538a0c12413SStephen M. Cameron 	unsigned long flags;
7539094963daSStephen M. Cameron 	u32 lockup_detected;
7540a0c12413SStephen M. Cameron 
7541a0c12413SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
7542a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
7543094963daSStephen M. Cameron 	lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
7544094963daSStephen M. Cameron 	if (!lockup_detected) {
7545094963daSStephen M. Cameron 		/* no heartbeat, but controller gave us a zero. */
7546094963daSStephen M. Cameron 		dev_warn(&h->pdev->dev,
754725163bd5SWebb Scales 			"lockup detected after %d but scratchpad register is zero\n",
754825163bd5SWebb Scales 			h->heartbeat_sample_interval / HZ);
7549094963daSStephen M. Cameron 		lockup_detected = 0xffffffff;
7550094963daSStephen M. Cameron 	}
7551094963daSStephen M. Cameron 	set_lockup_detected_for_all_cpus(h, lockup_detected);
7552a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
755325163bd5SWebb Scales 	dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x after %d\n",
755425163bd5SWebb Scales 			lockup_detected, h->heartbeat_sample_interval / HZ);
7555a0c12413SStephen M. Cameron 	pci_disable_device(h->pdev);
7556f2405db8SDon Brace 	fail_all_outstanding_cmds(h);
7557a0c12413SStephen M. Cameron }
7558a0c12413SStephen M. Cameron 
755925163bd5SWebb Scales static int detect_controller_lockup(struct ctlr_info *h)
7560a0c12413SStephen M. Cameron {
7561a0c12413SStephen M. Cameron 	u64 now;
7562a0c12413SStephen M. Cameron 	u32 heartbeat;
7563a0c12413SStephen M. Cameron 	unsigned long flags;
7564a0c12413SStephen M. Cameron 
7565a0c12413SStephen M. Cameron 	now = get_jiffies_64();
7566a0c12413SStephen M. Cameron 	/* If we've received an interrupt recently, we're ok. */
7567a0c12413SStephen M. Cameron 	if (time_after64(h->last_intr_timestamp +
7568e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
756925163bd5SWebb Scales 		return false;
7570a0c12413SStephen M. Cameron 
7571a0c12413SStephen M. Cameron 	/*
7572a0c12413SStephen M. Cameron 	 * If we've already checked the heartbeat recently, we're ok.
7573a0c12413SStephen M. Cameron 	 * This could happen if someone sends us a signal. We
7574a0c12413SStephen M. Cameron 	 * otherwise don't care about signals in this thread.
7575a0c12413SStephen M. Cameron 	 */
7576a0c12413SStephen M. Cameron 	if (time_after64(h->last_heartbeat_timestamp +
7577e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
757825163bd5SWebb Scales 		return false;
7579a0c12413SStephen M. Cameron 
7580a0c12413SStephen M. Cameron 	/* If heartbeat has not changed since we last looked, we're not ok. */
7581a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
7582a0c12413SStephen M. Cameron 	heartbeat = readl(&h->cfgtable->HeartBeat);
7583a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
7584a0c12413SStephen M. Cameron 	if (h->last_heartbeat == heartbeat) {
7585a0c12413SStephen M. Cameron 		controller_lockup_detected(h);
758625163bd5SWebb Scales 		return true;
7587a0c12413SStephen M. Cameron 	}
7588a0c12413SStephen M. Cameron 
7589a0c12413SStephen M. Cameron 	/* We're ok. */
7590a0c12413SStephen M. Cameron 	h->last_heartbeat = heartbeat;
7591a0c12413SStephen M. Cameron 	h->last_heartbeat_timestamp = now;
759225163bd5SWebb Scales 	return false;
7593a0c12413SStephen M. Cameron }
7594a0c12413SStephen M. Cameron 
75959846590eSStephen M. Cameron static void hpsa_ack_ctlr_events(struct ctlr_info *h)
759676438d08SStephen M. Cameron {
759776438d08SStephen M. Cameron 	int i;
759876438d08SStephen M. Cameron 	char *event_type;
759976438d08SStephen M. Cameron 
7600e4aa3e6aSStephen Cameron 	if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
7601e4aa3e6aSStephen Cameron 		return;
7602e4aa3e6aSStephen Cameron 
760376438d08SStephen M. Cameron 	/* Ask the controller to clear the events we're handling. */
76041f7cee8cSStephen M. Cameron 	if ((h->transMethod & (CFGTBL_Trans_io_accel1
76051f7cee8cSStephen M. Cameron 			| CFGTBL_Trans_io_accel2)) &&
760676438d08SStephen M. Cameron 		(h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
760776438d08SStephen M. Cameron 		 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
760876438d08SStephen M. Cameron 
760976438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
761076438d08SStephen M. Cameron 			event_type = "state change";
761176438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
761276438d08SStephen M. Cameron 			event_type = "configuration change";
761376438d08SStephen M. Cameron 		/* Stop sending new RAID offload reqs via the IO accelerator */
761476438d08SStephen M. Cameron 		scsi_block_requests(h->scsi_host);
761576438d08SStephen M. Cameron 		for (i = 0; i < h->ndevices; i++)
761676438d08SStephen M. Cameron 			h->dev[i]->offload_enabled = 0;
761723100dd9SStephen M. Cameron 		hpsa_drain_accel_commands(h);
761876438d08SStephen M. Cameron 		/* Set 'accelerator path config change' bit */
761976438d08SStephen M. Cameron 		dev_warn(&h->pdev->dev,
762076438d08SStephen M. Cameron 			"Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
762176438d08SStephen M. Cameron 			h->events, event_type);
762276438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
762376438d08SStephen M. Cameron 		/* Set the "clear event notify field update" bit 6 */
762476438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
762576438d08SStephen M. Cameron 		/* Wait until ctlr clears 'clear event notify field', bit 6 */
762676438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
762776438d08SStephen M. Cameron 		scsi_unblock_requests(h->scsi_host);
762876438d08SStephen M. Cameron 	} else {
762976438d08SStephen M. Cameron 		/* Acknowledge controller notification events. */
763076438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
763176438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
763276438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
763376438d08SStephen M. Cameron #if 0
763476438d08SStephen M. Cameron 		writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
763576438d08SStephen M. Cameron 		hpsa_wait_for_mode_change_ack(h);
763676438d08SStephen M. Cameron #endif
763776438d08SStephen M. Cameron 	}
76389846590eSStephen M. Cameron 	return;
763976438d08SStephen M. Cameron }
764076438d08SStephen M. Cameron 
764176438d08SStephen M. Cameron /* Check a register on the controller to see if there are configuration
764276438d08SStephen M. Cameron  * changes (added/changed/removed logical drives, etc.) which mean that
7643e863d68eSScott Teel  * we should rescan the controller for devices.
7644e863d68eSScott Teel  * Also check flag for driver-initiated rescan.
764576438d08SStephen M. Cameron  */
76469846590eSStephen M. Cameron static int hpsa_ctlr_needs_rescan(struct ctlr_info *h)
764776438d08SStephen M. Cameron {
764876438d08SStephen M. Cameron 	if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
76499846590eSStephen M. Cameron 		return 0;
765076438d08SStephen M. Cameron 
765176438d08SStephen M. Cameron 	h->events = readl(&(h->cfgtable->event_notify));
76529846590eSStephen M. Cameron 	return h->events & RESCAN_REQUIRED_EVENT_BITS;
76539846590eSStephen M. Cameron }
765476438d08SStephen M. Cameron 
765576438d08SStephen M. Cameron /*
76569846590eSStephen M. Cameron  * Check if any of the offline devices have become ready
765776438d08SStephen M. Cameron  */
76589846590eSStephen M. Cameron static int hpsa_offline_devices_ready(struct ctlr_info *h)
76599846590eSStephen M. Cameron {
76609846590eSStephen M. Cameron 	unsigned long flags;
76619846590eSStephen M. Cameron 	struct offline_device_entry *d;
76629846590eSStephen M. Cameron 	struct list_head *this, *tmp;
76639846590eSStephen M. Cameron 
76649846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
76659846590eSStephen M. Cameron 	list_for_each_safe(this, tmp, &h->offline_device_list) {
76669846590eSStephen M. Cameron 		d = list_entry(this, struct offline_device_entry,
76679846590eSStephen M. Cameron 				offline_list);
76689846590eSStephen M. Cameron 		spin_unlock_irqrestore(&h->offline_device_lock, flags);
7669d1fea47cSStephen M. Cameron 		if (!hpsa_volume_offline(h, d->scsi3addr)) {
7670d1fea47cSStephen M. Cameron 			spin_lock_irqsave(&h->offline_device_lock, flags);
7671d1fea47cSStephen M. Cameron 			list_del(&d->offline_list);
7672d1fea47cSStephen M. Cameron 			spin_unlock_irqrestore(&h->offline_device_lock, flags);
76739846590eSStephen M. Cameron 			return 1;
7674d1fea47cSStephen M. Cameron 		}
76759846590eSStephen M. Cameron 		spin_lock_irqsave(&h->offline_device_lock, flags);
767676438d08SStephen M. Cameron 	}
76779846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
76789846590eSStephen M. Cameron 	return 0;
76799846590eSStephen M. Cameron }
76809846590eSStephen M. Cameron 
76816636e7f4SDon Brace static void hpsa_rescan_ctlr_worker(struct work_struct *work)
7682a0c12413SStephen M. Cameron {
7683a0c12413SStephen M. Cameron 	unsigned long flags;
76848a98db73SStephen M. Cameron 	struct ctlr_info *h = container_of(to_delayed_work(work),
76856636e7f4SDon Brace 					struct ctlr_info, rescan_ctlr_work);
76866636e7f4SDon Brace 
76876636e7f4SDon Brace 
76886636e7f4SDon Brace 	if (h->remove_in_progress)
76898a98db73SStephen M. Cameron 		return;
76909846590eSStephen M. Cameron 
76919846590eSStephen M. Cameron 	if (hpsa_ctlr_needs_rescan(h) || hpsa_offline_devices_ready(h)) {
76929846590eSStephen M. Cameron 		scsi_host_get(h->scsi_host);
76939846590eSStephen M. Cameron 		hpsa_ack_ctlr_events(h);
76949846590eSStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
76959846590eSStephen M. Cameron 		scsi_host_put(h->scsi_host);
76969846590eSStephen M. Cameron 	}
76976636e7f4SDon Brace 	spin_lock_irqsave(&h->lock, flags);
76986636e7f4SDon Brace 	if (!h->remove_in_progress)
76996636e7f4SDon Brace 		queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
77006636e7f4SDon Brace 				h->heartbeat_sample_interval);
77016636e7f4SDon Brace 	spin_unlock_irqrestore(&h->lock, flags);
77026636e7f4SDon Brace }
77036636e7f4SDon Brace 
77046636e7f4SDon Brace static void hpsa_monitor_ctlr_worker(struct work_struct *work)
77056636e7f4SDon Brace {
77066636e7f4SDon Brace 	unsigned long flags;
77076636e7f4SDon Brace 	struct ctlr_info *h = container_of(to_delayed_work(work),
77086636e7f4SDon Brace 					struct ctlr_info, monitor_ctlr_work);
77096636e7f4SDon Brace 
77106636e7f4SDon Brace 	detect_controller_lockup(h);
77116636e7f4SDon Brace 	if (lockup_detected(h))
77126636e7f4SDon Brace 		return;
77139846590eSStephen M. Cameron 
77148a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
77156636e7f4SDon Brace 	if (!h->remove_in_progress)
77168a98db73SStephen M. Cameron 		schedule_delayed_work(&h->monitor_ctlr_work,
77178a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
77188a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
7719a0c12413SStephen M. Cameron }
7720a0c12413SStephen M. Cameron 
77216636e7f4SDon Brace static struct workqueue_struct *hpsa_create_controller_wq(struct ctlr_info *h,
77226636e7f4SDon Brace 						char *name)
77236636e7f4SDon Brace {
77246636e7f4SDon Brace 	struct workqueue_struct *wq = NULL;
77256636e7f4SDon Brace 
7726397ea9cbSDon Brace 	wq = alloc_ordered_workqueue("%s_%d_hpsa", 0, name, h->ctlr);
77276636e7f4SDon Brace 	if (!wq)
77286636e7f4SDon Brace 		dev_err(&h->pdev->dev, "failed to create %s workqueue\n", name);
77296636e7f4SDon Brace 
77306636e7f4SDon Brace 	return wq;
77316636e7f4SDon Brace }
77326636e7f4SDon Brace 
77336f039790SGreg Kroah-Hartman static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
77344c2a8c40SStephen M. Cameron {
77354c2a8c40SStephen M. Cameron 	int dac, rc;
77364c2a8c40SStephen M. Cameron 	struct ctlr_info *h;
773764670ac8SStephen M. Cameron 	int try_soft_reset = 0;
773864670ac8SStephen M. Cameron 	unsigned long flags;
77396b6c1cd7STomas Henzl 	u32 board_id;
77404c2a8c40SStephen M. Cameron 
77414c2a8c40SStephen M. Cameron 	if (number_of_controllers == 0)
77424c2a8c40SStephen M. Cameron 		printk(KERN_INFO DRIVER_NAME "\n");
77434c2a8c40SStephen M. Cameron 
77446b6c1cd7STomas Henzl 	rc = hpsa_lookup_board_id(pdev, &board_id);
77456b6c1cd7STomas Henzl 	if (rc < 0) {
77466b6c1cd7STomas Henzl 		dev_warn(&pdev->dev, "Board ID not found\n");
77476b6c1cd7STomas Henzl 		return rc;
77486b6c1cd7STomas Henzl 	}
77496b6c1cd7STomas Henzl 
77506b6c1cd7STomas Henzl 	rc = hpsa_init_reset_devices(pdev, board_id);
775164670ac8SStephen M. Cameron 	if (rc) {
775264670ac8SStephen M. Cameron 		if (rc != -ENOTSUPP)
77534c2a8c40SStephen M. Cameron 			return rc;
775464670ac8SStephen M. Cameron 		/* If the reset fails in a particular way (it has no way to do
775564670ac8SStephen M. Cameron 		 * a proper hard reset, so returns -ENOTSUPP) we can try to do
775664670ac8SStephen M. Cameron 		 * a soft reset once we get the controller configured up to the
775764670ac8SStephen M. Cameron 		 * point that it can accept a command.
775864670ac8SStephen M. Cameron 		 */
775964670ac8SStephen M. Cameron 		try_soft_reset = 1;
776064670ac8SStephen M. Cameron 		rc = 0;
776164670ac8SStephen M. Cameron 	}
776264670ac8SStephen M. Cameron 
776364670ac8SStephen M. Cameron reinit_after_soft_reset:
77644c2a8c40SStephen M. Cameron 
7765303932fdSDon Brace 	/* Command structures must be aligned on a 32-byte boundary because
7766303932fdSDon Brace 	 * the 5 lower bits of the address are used by the hardware. and by
7767303932fdSDon Brace 	 * the driver.  See comments in hpsa.h for more info.
7768303932fdSDon Brace 	 */
7769303932fdSDon Brace 	BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
7770edd16368SStephen M. Cameron 	h = kzalloc(sizeof(*h), GFP_KERNEL);
7771105a3dbcSRobert Elliott 	if (!h) {
7772105a3dbcSRobert Elliott 		dev_err(&pdev->dev, "Failed to allocate controller head\n");
7773ecd9aad4SStephen M. Cameron 		return -ENOMEM;
7774105a3dbcSRobert Elliott 	}
7775edd16368SStephen M. Cameron 
777655c06c71SStephen M. Cameron 	h->pdev = pdev;
7777105a3dbcSRobert Elliott 
7778a9a3a273SStephen M. Cameron 	h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
77799846590eSStephen M. Cameron 	INIT_LIST_HEAD(&h->offline_device_list);
77806eaf46fdSStephen M. Cameron 	spin_lock_init(&h->lock);
77819846590eSStephen M. Cameron 	spin_lock_init(&h->offline_device_lock);
77826eaf46fdSStephen M. Cameron 	spin_lock_init(&h->scan_lock);
778334f0c627SDon Brace 	atomic_set(&h->passthru_cmds_avail, HPSA_MAX_CONCURRENT_PASSTHRUS);
77849b5c48c2SStephen Cameron 	atomic_set(&h->abort_cmds_available, HPSA_CMDS_RESERVED_FOR_ABORTS);
7785094963daSStephen M. Cameron 
7786094963daSStephen M. Cameron 	/* Allocate and clear per-cpu variable lockup_detected */
7787094963daSStephen M. Cameron 	h->lockup_detected = alloc_percpu(u32);
77882a5ac326SStephen M. Cameron 	if (!h->lockup_detected) {
7789105a3dbcSRobert Elliott 		dev_err(&h->pdev->dev, "Failed to allocate lockup detector\n");
77902a5ac326SStephen M. Cameron 		rc = -ENOMEM;
77912efa5929SRobert Elliott 		goto clean1;	/* aer/h */
77922a5ac326SStephen M. Cameron 	}
7793094963daSStephen M. Cameron 	set_lockup_detected_for_all_cpus(h, 0);
7794094963daSStephen M. Cameron 
779555c06c71SStephen M. Cameron 	rc = hpsa_pci_init(h);
7796105a3dbcSRobert Elliott 	if (rc)
77972946e82bSRobert Elliott 		goto clean2;	/* lu, aer/h */
7798edd16368SStephen M. Cameron 
77992946e82bSRobert Elliott 	/* relies on h-> settings made by hpsa_pci_init, including
78002946e82bSRobert Elliott 	 * interrupt_mode h->intr */
78012946e82bSRobert Elliott 	rc = hpsa_scsi_host_alloc(h);
78022946e82bSRobert Elliott 	if (rc)
78032946e82bSRobert Elliott 		goto clean2_5;	/* pci, lu, aer/h */
78042946e82bSRobert Elliott 
78052946e82bSRobert Elliott 	sprintf(h->devname, HPSA "%d", h->scsi_host->host_no);
7806edd16368SStephen M. Cameron 	h->ctlr = number_of_controllers;
7807edd16368SStephen M. Cameron 	number_of_controllers++;
7808edd16368SStephen M. Cameron 
7809edd16368SStephen M. Cameron 	/* configure PCI DMA stuff */
7810ecd9aad4SStephen M. Cameron 	rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
7811ecd9aad4SStephen M. Cameron 	if (rc == 0) {
7812edd16368SStephen M. Cameron 		dac = 1;
7813ecd9aad4SStephen M. Cameron 	} else {
7814ecd9aad4SStephen M. Cameron 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7815ecd9aad4SStephen M. Cameron 		if (rc == 0) {
7816edd16368SStephen M. Cameron 			dac = 0;
7817ecd9aad4SStephen M. Cameron 		} else {
7818edd16368SStephen M. Cameron 			dev_err(&pdev->dev, "no suitable DMA available\n");
78192946e82bSRobert Elliott 			goto clean3;	/* shost, pci, lu, aer/h */
7820edd16368SStephen M. Cameron 		}
7821ecd9aad4SStephen M. Cameron 	}
7822edd16368SStephen M. Cameron 
7823edd16368SStephen M. Cameron 	/* make sure the board interrupts are off */
7824edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
782510f66018SStephen M. Cameron 
7826105a3dbcSRobert Elliott 	rc = hpsa_request_irqs(h, do_hpsa_intr_msi, do_hpsa_intr_intx);
7827105a3dbcSRobert Elliott 	if (rc)
78282946e82bSRobert Elliott 		goto clean3;	/* shost, pci, lu, aer/h */
7829d37ffbe4SRobert Elliott 	rc = hpsa_alloc_cmd_pool(h);
78308947fd10SRobert Elliott 	if (rc)
78312946e82bSRobert Elliott 		goto clean4;	/* irq, shost, pci, lu, aer/h */
7832105a3dbcSRobert Elliott 	rc = hpsa_alloc_sg_chain_blocks(h);
7833105a3dbcSRobert Elliott 	if (rc)
78342946e82bSRobert Elliott 		goto clean5;	/* cmd, irq, shost, pci, lu, aer/h */
7835a08a8471SStephen M. Cameron 	init_waitqueue_head(&h->scan_wait_queue);
78369b5c48c2SStephen Cameron 	init_waitqueue_head(&h->abort_cmd_wait_queue);
7837a58e7e53SWebb Scales 	init_waitqueue_head(&h->abort_sync_wait_queue);
7838a08a8471SStephen M. Cameron 	h->scan_finished = 1; /* no scan currently in progress */
7839edd16368SStephen M. Cameron 
7840edd16368SStephen M. Cameron 	pci_set_drvdata(pdev, h);
78419a41338eSStephen M. Cameron 	h->ndevices = 0;
7842316b221aSStephen M. Cameron 	h->hba_mode_enabled = 0;
78432946e82bSRobert Elliott 
78449a41338eSStephen M. Cameron 	spin_lock_init(&h->devlock);
7845105a3dbcSRobert Elliott 	rc = hpsa_put_ctlr_into_performant_mode(h);
7846105a3dbcSRobert Elliott 	if (rc)
78472946e82bSRobert Elliott 		goto clean6; /* sg, cmd, irq, shost, pci, lu, aer/h */
78482946e82bSRobert Elliott 
78492946e82bSRobert Elliott 	/* hook into SCSI subsystem */
78502946e82bSRobert Elliott 	rc = hpsa_scsi_add_host(h);
78512946e82bSRobert Elliott 	if (rc)
78522946e82bSRobert Elliott 		goto clean7; /* perf, sg, cmd, irq, shost, pci, lu, aer/h */
78532efa5929SRobert Elliott 
78542efa5929SRobert Elliott 	/* create the resubmit workqueue */
78552efa5929SRobert Elliott 	h->rescan_ctlr_wq = hpsa_create_controller_wq(h, "rescan");
78562efa5929SRobert Elliott 	if (!h->rescan_ctlr_wq) {
78572efa5929SRobert Elliott 		rc = -ENOMEM;
78582efa5929SRobert Elliott 		goto clean7;
78592efa5929SRobert Elliott 	}
78602efa5929SRobert Elliott 
78612efa5929SRobert Elliott 	h->resubmit_wq = hpsa_create_controller_wq(h, "resubmit");
78622efa5929SRobert Elliott 	if (!h->resubmit_wq) {
78632efa5929SRobert Elliott 		rc = -ENOMEM;
78642efa5929SRobert Elliott 		goto clean7;	/* aer/h */
78652efa5929SRobert Elliott 	}
786664670ac8SStephen M. Cameron 
7867105a3dbcSRobert Elliott 	/*
7868105a3dbcSRobert Elliott 	 * At this point, the controller is ready to take commands.
786964670ac8SStephen M. Cameron 	 * Now, if reset_devices and the hard reset didn't work, try
787064670ac8SStephen M. Cameron 	 * the soft reset and see if that works.
787164670ac8SStephen M. Cameron 	 */
787264670ac8SStephen M. Cameron 	if (try_soft_reset) {
787364670ac8SStephen M. Cameron 
787464670ac8SStephen M. Cameron 		/* This is kind of gross.  We may or may not get a completion
787564670ac8SStephen M. Cameron 		 * from the soft reset command, and if we do, then the value
787664670ac8SStephen M. Cameron 		 * from the fifo may or may not be valid.  So, we wait 10 secs
787764670ac8SStephen M. Cameron 		 * after the reset throwing away any completions we get during
787864670ac8SStephen M. Cameron 		 * that time.  Unregister the interrupt handler and register
787964670ac8SStephen M. Cameron 		 * fake ones to scoop up any residual completions.
788064670ac8SStephen M. Cameron 		 */
788164670ac8SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
788264670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
788364670ac8SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
7884ec501a18SRobert Elliott 		hpsa_free_irqs(h);
78859ee61794SRobert Elliott 		rc = hpsa_request_irqs(h, hpsa_msix_discard_completions,
788664670ac8SStephen M. Cameron 					hpsa_intx_discard_completions);
788764670ac8SStephen M. Cameron 		if (rc) {
78889ee61794SRobert Elliott 			dev_warn(&h->pdev->dev,
78899ee61794SRobert Elliott 				"Failed to request_irq after soft reset.\n");
7890d498757cSRobert Elliott 			/*
7891b2ef480cSRobert Elliott 			 * cannot goto clean7 or free_irqs will be called
7892b2ef480cSRobert Elliott 			 * again. Instead, do its work
7893b2ef480cSRobert Elliott 			 */
7894b2ef480cSRobert Elliott 			hpsa_free_performant_mode(h);	/* clean7 */
7895b2ef480cSRobert Elliott 			hpsa_free_sg_chain_blocks(h);	/* clean6 */
7896b2ef480cSRobert Elliott 			hpsa_free_cmd_pool(h);		/* clean5 */
7897b2ef480cSRobert Elliott 			/*
7898b2ef480cSRobert Elliott 			 * skip hpsa_free_irqs(h) clean4 since that
7899b2ef480cSRobert Elliott 			 * was just called before request_irqs failed
7900d498757cSRobert Elliott 			 */
7901d498757cSRobert Elliott 			goto clean3;
790264670ac8SStephen M. Cameron 		}
790364670ac8SStephen M. Cameron 
790464670ac8SStephen M. Cameron 		rc = hpsa_kdump_soft_reset(h);
790564670ac8SStephen M. Cameron 		if (rc)
790664670ac8SStephen M. Cameron 			/* Neither hard nor soft reset worked, we're hosed. */
79072946e82bSRobert Elliott 			goto clean9;
790864670ac8SStephen M. Cameron 
790964670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev, "Board READY.\n");
791064670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev,
791164670ac8SStephen M. Cameron 			"Waiting for stale completions to drain.\n");
791264670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_ON);
791364670ac8SStephen M. Cameron 		msleep(10000);
791464670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
791564670ac8SStephen M. Cameron 
791664670ac8SStephen M. Cameron 		rc = controller_reset_failed(h->cfgtable);
791764670ac8SStephen M. Cameron 		if (rc)
791864670ac8SStephen M. Cameron 			dev_info(&h->pdev->dev,
791964670ac8SStephen M. Cameron 				"Soft reset appears to have failed.\n");
792064670ac8SStephen M. Cameron 
792164670ac8SStephen M. Cameron 		/* since the controller's reset, we have to go back and re-init
792264670ac8SStephen M. Cameron 		 * everything.  Easiest to just forget what we've done and do it
792364670ac8SStephen M. Cameron 		 * all over again.
792464670ac8SStephen M. Cameron 		 */
792564670ac8SStephen M. Cameron 		hpsa_undo_allocations_after_kdump_soft_reset(h);
792664670ac8SStephen M. Cameron 		try_soft_reset = 0;
792764670ac8SStephen M. Cameron 		if (rc)
7928b2ef480cSRobert Elliott 			/* don't goto clean, we already unallocated */
792964670ac8SStephen M. Cameron 			return -ENODEV;
793064670ac8SStephen M. Cameron 
793164670ac8SStephen M. Cameron 		goto reinit_after_soft_reset;
793264670ac8SStephen M. Cameron 	}
7933edd16368SStephen M. Cameron 
7934da0697bdSScott Teel 	/* Enable Accelerated IO path at driver layer */
7935da0697bdSScott Teel 	h->acciopath_status = 1;
7936da0697bdSScott Teel 
7937e863d68eSScott Teel 
7938edd16368SStephen M. Cameron 	/* Turn the interrupts on so we can service requests */
7939edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_ON);
7940edd16368SStephen M. Cameron 
7941339b2b14SStephen M. Cameron 	hpsa_hba_inquiry(h);
79428a98db73SStephen M. Cameron 
79438a98db73SStephen M. Cameron 	/* Monitor the controller for firmware lockups */
79448a98db73SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
79458a98db73SStephen M. Cameron 	INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
79468a98db73SStephen M. Cameron 	schedule_delayed_work(&h->monitor_ctlr_work,
79478a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
79486636e7f4SDon Brace 	INIT_DELAYED_WORK(&h->rescan_ctlr_work, hpsa_rescan_ctlr_worker);
79496636e7f4SDon Brace 	queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
79506636e7f4SDon Brace 				h->heartbeat_sample_interval);
795188bf6d62SStephen M. Cameron 	return 0;
7952edd16368SStephen M. Cameron 
79532946e82bSRobert Elliott clean9: /* wq, sh, perf, sg, cmd, irq, shost, pci, lu, aer/h */
7954105a3dbcSRobert Elliott 	kfree(h->hba_inquiry_data);
79552946e82bSRobert Elliott clean7: /* perf, sg, cmd, irq, shost, pci, lu, aer/h */
7956105a3dbcSRobert Elliott 	hpsa_free_performant_mode(h);
7957105a3dbcSRobert Elliott 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
7958105a3dbcSRobert Elliott clean6: /* sg, cmd, irq, pci, lockup, wq/aer/h */
795933a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
79602946e82bSRobert Elliott clean5: /* cmd, irq, shost, pci, lu, aer/h */
79612e9d1b36SStephen M. Cameron 	hpsa_free_cmd_pool(h);
79622946e82bSRobert Elliott clean4: /* irq, shost, pci, lu, aer/h */
7963ec501a18SRobert Elliott 	hpsa_free_irqs(h);
79642946e82bSRobert Elliott clean3: /* shost, pci, lu, aer/h */
79652946e82bSRobert Elliott 	scsi_host_put(h->scsi_host);
79662946e82bSRobert Elliott 	h->scsi_host = NULL;
79672946e82bSRobert Elliott clean2_5: /* pci, lu, aer/h */
7968195f2c65SRobert Elliott 	hpsa_free_pci_init(h);
79692946e82bSRobert Elliott clean2: /* lu, aer/h */
7970105a3dbcSRobert Elliott 	if (h->lockup_detected) {
7971094963daSStephen M. Cameron 		free_percpu(h->lockup_detected);
7972105a3dbcSRobert Elliott 		h->lockup_detected = NULL;
7973105a3dbcSRobert Elliott 	}
7974105a3dbcSRobert Elliott clean1:	/* wq/aer/h */
7975105a3dbcSRobert Elliott 	if (h->resubmit_wq) {
7976105a3dbcSRobert Elliott 		destroy_workqueue(h->resubmit_wq);
7977105a3dbcSRobert Elliott 		h->resubmit_wq = NULL;
7978105a3dbcSRobert Elliott 	}
7979105a3dbcSRobert Elliott 	if (h->rescan_ctlr_wq) {
7980105a3dbcSRobert Elliott 		destroy_workqueue(h->rescan_ctlr_wq);
7981105a3dbcSRobert Elliott 		h->rescan_ctlr_wq = NULL;
7982105a3dbcSRobert Elliott 	}
7983edd16368SStephen M. Cameron 	kfree(h);
7984ecd9aad4SStephen M. Cameron 	return rc;
7985edd16368SStephen M. Cameron }
7986edd16368SStephen M. Cameron 
7987edd16368SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h)
7988edd16368SStephen M. Cameron {
7989edd16368SStephen M. Cameron 	char *flush_buf;
7990edd16368SStephen M. Cameron 	struct CommandList *c;
799125163bd5SWebb Scales 	int rc;
7992702890e3SStephen M. Cameron 
7993094963daSStephen M. Cameron 	if (unlikely(lockup_detected(h)))
7994702890e3SStephen M. Cameron 		return;
7995edd16368SStephen M. Cameron 	flush_buf = kzalloc(4, GFP_KERNEL);
7996edd16368SStephen M. Cameron 	if (!flush_buf)
7997edd16368SStephen M. Cameron 		return;
7998edd16368SStephen M. Cameron 
799945fcb86eSStephen Cameron 	c = cmd_alloc(h);
8000bf43caf3SRobert Elliott 
8001a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
8002a2dac136SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_CMD)) {
8003a2dac136SStephen M. Cameron 		goto out;
8004a2dac136SStephen M. Cameron 	}
800525163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
800625163bd5SWebb Scales 					PCI_DMA_TODEVICE, NO_TIMEOUT);
800725163bd5SWebb Scales 	if (rc)
800825163bd5SWebb Scales 		goto out;
8009edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus != 0)
8010a2dac136SStephen M. Cameron out:
8011edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
8012edd16368SStephen M. Cameron 			"error flushing cache on controller\n");
801345fcb86eSStephen Cameron 	cmd_free(h, c);
8014edd16368SStephen M. Cameron 	kfree(flush_buf);
8015edd16368SStephen M. Cameron }
8016edd16368SStephen M. Cameron 
8017edd16368SStephen M. Cameron static void hpsa_shutdown(struct pci_dev *pdev)
8018edd16368SStephen M. Cameron {
8019edd16368SStephen M. Cameron 	struct ctlr_info *h;
8020edd16368SStephen M. Cameron 
8021edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
8022edd16368SStephen M. Cameron 	/* Turn board interrupts off  and send the flush cache command
8023edd16368SStephen M. Cameron 	 * sendcmd will turn off interrupt, and send the flush...
8024edd16368SStephen M. Cameron 	 * To write all data in the battery backed cache to disks
8025edd16368SStephen M. Cameron 	 */
8026edd16368SStephen M. Cameron 	hpsa_flush_cache(h);
8027edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
8028105a3dbcSRobert Elliott 	hpsa_free_irqs(h);			/* init_one 4 */
8029cc64c817SRobert Elliott 	hpsa_disable_interrupt_mode(h);		/* pci_init 2 */
8030edd16368SStephen M. Cameron }
8031edd16368SStephen M. Cameron 
80326f039790SGreg Kroah-Hartman static void hpsa_free_device_info(struct ctlr_info *h)
803355e14e76SStephen M. Cameron {
803455e14e76SStephen M. Cameron 	int i;
803555e14e76SStephen M. Cameron 
8036105a3dbcSRobert Elliott 	for (i = 0; i < h->ndevices; i++) {
803755e14e76SStephen M. Cameron 		kfree(h->dev[i]);
8038105a3dbcSRobert Elliott 		h->dev[i] = NULL;
8039105a3dbcSRobert Elliott 	}
804055e14e76SStephen M. Cameron }
804155e14e76SStephen M. Cameron 
80426f039790SGreg Kroah-Hartman static void hpsa_remove_one(struct pci_dev *pdev)
8043edd16368SStephen M. Cameron {
8044edd16368SStephen M. Cameron 	struct ctlr_info *h;
80458a98db73SStephen M. Cameron 	unsigned long flags;
8046edd16368SStephen M. Cameron 
8047edd16368SStephen M. Cameron 	if (pci_get_drvdata(pdev) == NULL) {
8048edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "unable to remove device\n");
8049edd16368SStephen M. Cameron 		return;
8050edd16368SStephen M. Cameron 	}
8051edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
80528a98db73SStephen M. Cameron 
80538a98db73SStephen M. Cameron 	/* Get rid of any controller monitoring work items */
80548a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
80558a98db73SStephen M. Cameron 	h->remove_in_progress = 1;
80568a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
80576636e7f4SDon Brace 	cancel_delayed_work_sync(&h->monitor_ctlr_work);
80586636e7f4SDon Brace 	cancel_delayed_work_sync(&h->rescan_ctlr_work);
80596636e7f4SDon Brace 	destroy_workqueue(h->rescan_ctlr_wq);
80606636e7f4SDon Brace 	destroy_workqueue(h->resubmit_wq);
8061cc64c817SRobert Elliott 
8062105a3dbcSRobert Elliott 	/* includes hpsa_free_irqs - init_one 4 */
8063195f2c65SRobert Elliott 	/* includes hpsa_disable_interrupt_mode - pci_init 2 */
8064edd16368SStephen M. Cameron 	hpsa_shutdown(pdev);
8065cc64c817SRobert Elliott 
8066105a3dbcSRobert Elliott 	hpsa_free_device_info(h);		/* scan */
8067105a3dbcSRobert Elliott 
80682946e82bSRobert Elliott 	kfree(h->hba_inquiry_data);			/* init_one 10 */
80692946e82bSRobert Elliott 	h->hba_inquiry_data = NULL;			/* init_one 10 */
80702946e82bSRobert Elliott 	if (h->scsi_host)
80712946e82bSRobert Elliott 		scsi_remove_host(h->scsi_host);		/* init_one 8 */
80722946e82bSRobert Elliott 	hpsa_free_ioaccel2_sg_chain_blocks(h);
8073105a3dbcSRobert Elliott 	hpsa_free_performant_mode(h);			/* init_one 7 */
8074105a3dbcSRobert Elliott 	hpsa_free_sg_chain_blocks(h);			/* init_one 6 */
80751fb7c98aSRobert Elliott 	hpsa_free_cmd_pool(h);				/* init_one 5 */
8076105a3dbcSRobert Elliott 
8077105a3dbcSRobert Elliott 	/* hpsa_free_irqs already called via hpsa_shutdown init_one 4 */
8078195f2c65SRobert Elliott 
80792946e82bSRobert Elliott 	scsi_host_put(h->scsi_host);			/* init_one 3 */
80802946e82bSRobert Elliott 	h->scsi_host = NULL;				/* init_one 3 */
80812946e82bSRobert Elliott 
8082195f2c65SRobert Elliott 	/* includes hpsa_disable_interrupt_mode - pci_init 2 */
80832946e82bSRobert Elliott 	hpsa_free_pci_init(h);				/* init_one 2.5 */
8084195f2c65SRobert Elliott 
8085105a3dbcSRobert Elliott 	free_percpu(h->lockup_detected);		/* init_one 2 */
8086105a3dbcSRobert Elliott 	h->lockup_detected = NULL;			/* init_one 2 */
8087105a3dbcSRobert Elliott 	/* (void) pci_disable_pcie_error_reporting(pdev); */	/* init_one 1 */
8088105a3dbcSRobert Elliott 	kfree(h);					/* init_one 1 */
8089edd16368SStephen M. Cameron }
8090edd16368SStephen M. Cameron 
8091edd16368SStephen M. Cameron static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
8092edd16368SStephen M. Cameron 	__attribute__((unused)) pm_message_t state)
8093edd16368SStephen M. Cameron {
8094edd16368SStephen M. Cameron 	return -ENOSYS;
8095edd16368SStephen M. Cameron }
8096edd16368SStephen M. Cameron 
8097edd16368SStephen M. Cameron static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
8098edd16368SStephen M. Cameron {
8099edd16368SStephen M. Cameron 	return -ENOSYS;
8100edd16368SStephen M. Cameron }
8101edd16368SStephen M. Cameron 
8102edd16368SStephen M. Cameron static struct pci_driver hpsa_pci_driver = {
8103f79cfec6SStephen M. Cameron 	.name = HPSA,
8104edd16368SStephen M. Cameron 	.probe = hpsa_init_one,
81056f039790SGreg Kroah-Hartman 	.remove = hpsa_remove_one,
8106edd16368SStephen M. Cameron 	.id_table = hpsa_pci_device_id,	/* id_table */
8107edd16368SStephen M. Cameron 	.shutdown = hpsa_shutdown,
8108edd16368SStephen M. Cameron 	.suspend = hpsa_suspend,
8109edd16368SStephen M. Cameron 	.resume = hpsa_resume,
8110edd16368SStephen M. Cameron };
8111edd16368SStephen M. Cameron 
8112303932fdSDon Brace /* Fill in bucket_map[], given nsgs (the max number of
8113303932fdSDon Brace  * scatter gather elements supported) and bucket[],
8114303932fdSDon Brace  * which is an array of 8 integers.  The bucket[] array
8115303932fdSDon Brace  * contains 8 different DMA transfer sizes (in 16
8116303932fdSDon Brace  * byte increments) which the controller uses to fetch
8117303932fdSDon Brace  * commands.  This function fills in bucket_map[], which
8118303932fdSDon Brace  * maps a given number of scatter gather elements to one of
8119303932fdSDon Brace  * the 8 DMA transfer sizes.  The point of it is to allow the
8120303932fdSDon Brace  * controller to only do as much DMA as needed to fetch the
8121303932fdSDon Brace  * command, with the DMA transfer size encoded in the lower
8122303932fdSDon Brace  * bits of the command address.
8123303932fdSDon Brace  */
8124303932fdSDon Brace static void  calc_bucket_map(int bucket[], int num_buckets,
81252b08b3e9SDon Brace 	int nsgs, int min_blocks, u32 *bucket_map)
8126303932fdSDon Brace {
8127303932fdSDon Brace 	int i, j, b, size;
8128303932fdSDon Brace 
8129303932fdSDon Brace 	/* Note, bucket_map must have nsgs+1 entries. */
8130303932fdSDon Brace 	for (i = 0; i <= nsgs; i++) {
8131303932fdSDon Brace 		/* Compute size of a command with i SG entries */
8132e1f7de0cSMatt Gates 		size = i + min_blocks;
8133303932fdSDon Brace 		b = num_buckets; /* Assume the biggest bucket */
8134303932fdSDon Brace 		/* Find the bucket that is just big enough */
8135e1f7de0cSMatt Gates 		for (j = 0; j < num_buckets; j++) {
8136303932fdSDon Brace 			if (bucket[j] >= size) {
8137303932fdSDon Brace 				b = j;
8138303932fdSDon Brace 				break;
8139303932fdSDon Brace 			}
8140303932fdSDon Brace 		}
8141303932fdSDon Brace 		/* for a command with i SG entries, use bucket b. */
8142303932fdSDon Brace 		bucket_map[i] = b;
8143303932fdSDon Brace 	}
8144303932fdSDon Brace }
8145303932fdSDon Brace 
8146105a3dbcSRobert Elliott /*
8147105a3dbcSRobert Elliott  * return -ENODEV on err, 0 on success (or no action)
8148105a3dbcSRobert Elliott  * allocates numerous items that must be freed later
8149105a3dbcSRobert Elliott  */
8150c706a795SRobert Elliott static int hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
8151303932fdSDon Brace {
81526c311b57SStephen M. Cameron 	int i;
81536c311b57SStephen M. Cameron 	unsigned long register_value;
8154e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
8155e1f7de0cSMatt Gates 			(trans_support & CFGTBL_Trans_use_short_tags) |
8156e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix |
8157b9af4937SStephen M. Cameron 			(trans_support & (CFGTBL_Trans_io_accel1 |
8158b9af4937SStephen M. Cameron 				CFGTBL_Trans_io_accel2));
8159e1f7de0cSMatt Gates 	struct access_method access = SA5_performant_access;
8160def342bdSStephen M. Cameron 
8161def342bdSStephen M. Cameron 	/* This is a bit complicated.  There are 8 registers on
8162def342bdSStephen M. Cameron 	 * the controller which we write to to tell it 8 different
8163def342bdSStephen M. Cameron 	 * sizes of commands which there may be.  It's a way of
8164def342bdSStephen M. Cameron 	 * reducing the DMA done to fetch each command.  Encoded into
8165def342bdSStephen M. Cameron 	 * each command's tag are 3 bits which communicate to the controller
8166def342bdSStephen M. Cameron 	 * which of the eight sizes that command fits within.  The size of
8167def342bdSStephen M. Cameron 	 * each command depends on how many scatter gather entries there are.
8168def342bdSStephen M. Cameron 	 * Each SG entry requires 16 bytes.  The eight registers are programmed
8169def342bdSStephen M. Cameron 	 * with the number of 16-byte blocks a command of that size requires.
8170def342bdSStephen M. Cameron 	 * The smallest command possible requires 5 such 16 byte blocks.
8171d66ae08bSStephen M. Cameron 	 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
8172def342bdSStephen M. Cameron 	 * blocks.  Note, this only extends to the SG entries contained
8173def342bdSStephen M. Cameron 	 * within the command block, and does not extend to chained blocks
8174def342bdSStephen M. Cameron 	 * of SG elements.   bft[] contains the eight values we write to
8175def342bdSStephen M. Cameron 	 * the registers.  They are not evenly distributed, but have more
8176def342bdSStephen M. Cameron 	 * sizes for small commands, and fewer sizes for larger commands.
8177def342bdSStephen M. Cameron 	 */
8178d66ae08bSStephen M. Cameron 	int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
8179b9af4937SStephen M. Cameron #define MIN_IOACCEL2_BFT_ENTRY 5
8180b9af4937SStephen M. Cameron #define HPSA_IOACCEL2_HEADER_SZ 4
8181b9af4937SStephen M. Cameron 	int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
8182b9af4937SStephen M. Cameron 			13, 14, 15, 16, 17, 18, 19,
8183b9af4937SStephen M. Cameron 			HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
8184b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
8185b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
8186b9af4937SStephen M. Cameron 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
8187b9af4937SStephen M. Cameron 				 16 * MIN_IOACCEL2_BFT_ENTRY);
8188b9af4937SStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
8189d66ae08bSStephen M. Cameron 	BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
8190303932fdSDon Brace 	/*  5 = 1 s/g entry or 4k
8191303932fdSDon Brace 	 *  6 = 2 s/g entry or 8k
8192303932fdSDon Brace 	 *  8 = 4 s/g entry or 16k
8193303932fdSDon Brace 	 * 10 = 6 s/g entry or 24k
8194303932fdSDon Brace 	 */
8195303932fdSDon Brace 
8196b3a52e79SStephen M. Cameron 	/* If the controller supports either ioaccel method then
8197b3a52e79SStephen M. Cameron 	 * we can also use the RAID stack submit path that does not
8198b3a52e79SStephen M. Cameron 	 * perform the superfluous readl() after each command submission.
8199b3a52e79SStephen M. Cameron 	 */
8200b3a52e79SStephen M. Cameron 	if (trans_support & (CFGTBL_Trans_io_accel1 | CFGTBL_Trans_io_accel2))
8201b3a52e79SStephen M. Cameron 		access = SA5_performant_access_no_read;
8202b3a52e79SStephen M. Cameron 
8203303932fdSDon Brace 	/* Controller spec: zero out this buffer. */
8204072b0518SStephen M. Cameron 	for (i = 0; i < h->nreply_queues; i++)
8205072b0518SStephen M. Cameron 		memset(h->reply_queue[i].head, 0, h->reply_queue_size);
8206303932fdSDon Brace 
8207d66ae08bSStephen M. Cameron 	bft[7] = SG_ENTRIES_IN_CMD + 4;
8208d66ae08bSStephen M. Cameron 	calc_bucket_map(bft, ARRAY_SIZE(bft),
8209e1f7de0cSMatt Gates 				SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
8210303932fdSDon Brace 	for (i = 0; i < 8; i++)
8211303932fdSDon Brace 		writel(bft[i], &h->transtable->BlockFetch[i]);
8212303932fdSDon Brace 
8213303932fdSDon Brace 	/* size of controller ring buffer */
8214303932fdSDon Brace 	writel(h->max_commands, &h->transtable->RepQSize);
8215254f796bSMatt Gates 	writel(h->nreply_queues, &h->transtable->RepQCount);
8216303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrLow32);
8217303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrHigh32);
8218254f796bSMatt Gates 
8219254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
8220254f796bSMatt Gates 		writel(0, &h->transtable->RepQAddr[i].upper);
8221072b0518SStephen M. Cameron 		writel(h->reply_queue[i].busaddr,
8222254f796bSMatt Gates 			&h->transtable->RepQAddr[i].lower);
8223254f796bSMatt Gates 	}
8224254f796bSMatt Gates 
8225b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
8226e1f7de0cSMatt Gates 	writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
8227e1f7de0cSMatt Gates 	/*
8228e1f7de0cSMatt Gates 	 * enable outbound interrupt coalescing in accelerator mode;
8229e1f7de0cSMatt Gates 	 */
8230e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
8231e1f7de0cSMatt Gates 		access = SA5_ioaccel_mode1_access;
8232e1f7de0cSMatt Gates 		writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
8233e1f7de0cSMatt Gates 		writel(4, &h->cfgtable->HostWrite.CoalIntCount);
8234c349775eSScott Teel 	} else {
8235c349775eSScott Teel 		if (trans_support & CFGTBL_Trans_io_accel2) {
8236c349775eSScott Teel 			access = SA5_ioaccel_mode2_access;
8237c349775eSScott Teel 			writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
8238c349775eSScott Teel 			writel(4, &h->cfgtable->HostWrite.CoalIntCount);
8239c349775eSScott Teel 		}
8240e1f7de0cSMatt Gates 	}
8241303932fdSDon Brace 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
8242c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h)) {
8243c706a795SRobert Elliott 		dev_err(&h->pdev->dev,
8244c706a795SRobert Elliott 			"performant mode problem - doorbell timeout\n");
8245c706a795SRobert Elliott 		return -ENODEV;
8246c706a795SRobert Elliott 	}
8247303932fdSDon Brace 	register_value = readl(&(h->cfgtable->TransportActive));
8248303932fdSDon Brace 	if (!(register_value & CFGTBL_Trans_Performant)) {
8249050f7147SStephen Cameron 		dev_err(&h->pdev->dev,
8250050f7147SStephen Cameron 			"performant mode problem - transport not active\n");
8251c706a795SRobert Elliott 		return -ENODEV;
8252303932fdSDon Brace 	}
8253960a30e7SStephen M. Cameron 	/* Change the access methods to the performant access methods */
8254e1f7de0cSMatt Gates 	h->access = access;
8255e1f7de0cSMatt Gates 	h->transMethod = transMethod;
8256e1f7de0cSMatt Gates 
8257b9af4937SStephen M. Cameron 	if (!((trans_support & CFGTBL_Trans_io_accel1) ||
8258b9af4937SStephen M. Cameron 		(trans_support & CFGTBL_Trans_io_accel2)))
8259c706a795SRobert Elliott 		return 0;
8260e1f7de0cSMatt Gates 
8261b9af4937SStephen M. Cameron 	if (trans_support & CFGTBL_Trans_io_accel1) {
8262e1f7de0cSMatt Gates 		/* Set up I/O accelerator mode */
8263e1f7de0cSMatt Gates 		for (i = 0; i < h->nreply_queues; i++) {
8264e1f7de0cSMatt Gates 			writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
8265e1f7de0cSMatt Gates 			h->reply_queue[i].current_entry =
8266e1f7de0cSMatt Gates 				readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
8267e1f7de0cSMatt Gates 		}
8268283b4a9bSStephen M. Cameron 		bft[7] = h->ioaccel_maxsg + 8;
8269283b4a9bSStephen M. Cameron 		calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
8270e1f7de0cSMatt Gates 				h->ioaccel1_blockFetchTable);
8271e1f7de0cSMatt Gates 
8272e1f7de0cSMatt Gates 		/* initialize all reply queue entries to unused */
8273072b0518SStephen M. Cameron 		for (i = 0; i < h->nreply_queues; i++)
8274072b0518SStephen M. Cameron 			memset(h->reply_queue[i].head,
8275072b0518SStephen M. Cameron 				(u8) IOACCEL_MODE1_REPLY_UNUSED,
8276072b0518SStephen M. Cameron 				h->reply_queue_size);
8277e1f7de0cSMatt Gates 
8278e1f7de0cSMatt Gates 		/* set all the constant fields in the accelerator command
8279e1f7de0cSMatt Gates 		 * frames once at init time to save CPU cycles later.
8280e1f7de0cSMatt Gates 		 */
8281e1f7de0cSMatt Gates 		for (i = 0; i < h->nr_cmds; i++) {
8282e1f7de0cSMatt Gates 			struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
8283e1f7de0cSMatt Gates 
8284e1f7de0cSMatt Gates 			cp->function = IOACCEL1_FUNCTION_SCSIIO;
8285e1f7de0cSMatt Gates 			cp->err_info = (u32) (h->errinfo_pool_dhandle +
8286e1f7de0cSMatt Gates 					(i * sizeof(struct ErrorInfo)));
8287e1f7de0cSMatt Gates 			cp->err_info_len = sizeof(struct ErrorInfo);
8288e1f7de0cSMatt Gates 			cp->sgl_offset = IOACCEL1_SGLOFFSET;
82892b08b3e9SDon Brace 			cp->host_context_flags =
82902b08b3e9SDon Brace 				cpu_to_le16(IOACCEL1_HCFLAGS_CISS_FORMAT);
8291e1f7de0cSMatt Gates 			cp->timeout_sec = 0;
8292e1f7de0cSMatt Gates 			cp->ReplyQueue = 0;
829350a0decfSStephen M. Cameron 			cp->tag =
8294f2405db8SDon Brace 				cpu_to_le64((i << DIRECT_LOOKUP_SHIFT));
829550a0decfSStephen M. Cameron 			cp->host_addr =
829650a0decfSStephen M. Cameron 				cpu_to_le64(h->ioaccel_cmd_pool_dhandle +
8297e1f7de0cSMatt Gates 					(i * sizeof(struct io_accel1_cmd)));
8298e1f7de0cSMatt Gates 		}
8299b9af4937SStephen M. Cameron 	} else if (trans_support & CFGTBL_Trans_io_accel2) {
8300b9af4937SStephen M. Cameron 		u64 cfg_offset, cfg_base_addr_index;
8301b9af4937SStephen M. Cameron 		u32 bft2_offset, cfg_base_addr;
8302b9af4937SStephen M. Cameron 		int rc;
8303b9af4937SStephen M. Cameron 
8304b9af4937SStephen M. Cameron 		rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
8305b9af4937SStephen M. Cameron 			&cfg_base_addr_index, &cfg_offset);
8306b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
8307b9af4937SStephen M. Cameron 		bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
8308b9af4937SStephen M. Cameron 		calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
8309b9af4937SStephen M. Cameron 				4, h->ioaccel2_blockFetchTable);
8310b9af4937SStephen M. Cameron 		bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
8311b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct CfgTable,
8312b9af4937SStephen M. Cameron 				io_accel_request_size_offset) != 0xb8);
8313b9af4937SStephen M. Cameron 		h->ioaccel2_bft2_regs =
8314b9af4937SStephen M. Cameron 			remap_pci_mem(pci_resource_start(h->pdev,
8315b9af4937SStephen M. Cameron 					cfg_base_addr_index) +
8316b9af4937SStephen M. Cameron 					cfg_offset + bft2_offset,
8317b9af4937SStephen M. Cameron 					ARRAY_SIZE(bft2) *
8318b9af4937SStephen M. Cameron 					sizeof(*h->ioaccel2_bft2_regs));
8319b9af4937SStephen M. Cameron 		for (i = 0; i < ARRAY_SIZE(bft2); i++)
8320b9af4937SStephen M. Cameron 			writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
8321b9af4937SStephen M. Cameron 	}
8322b9af4937SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
8323c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h)) {
8324c706a795SRobert Elliott 		dev_err(&h->pdev->dev,
8325c706a795SRobert Elliott 			"performant mode problem - enabling ioaccel mode\n");
8326c706a795SRobert Elliott 		return -ENODEV;
8327c706a795SRobert Elliott 	}
8328c706a795SRobert Elliott 	return 0;
8329e1f7de0cSMatt Gates }
8330e1f7de0cSMatt Gates 
83311fb7c98aSRobert Elliott /* Free ioaccel1 mode command blocks and block fetch table */
83321fb7c98aSRobert Elliott static void hpsa_free_ioaccel1_cmd_and_bft(struct ctlr_info *h)
83331fb7c98aSRobert Elliott {
8334105a3dbcSRobert Elliott 	if (h->ioaccel_cmd_pool) {
83351fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
83361fb7c98aSRobert Elliott 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
83371fb7c98aSRobert Elliott 			h->ioaccel_cmd_pool,
83381fb7c98aSRobert Elliott 			h->ioaccel_cmd_pool_dhandle);
8339105a3dbcSRobert Elliott 		h->ioaccel_cmd_pool = NULL;
8340105a3dbcSRobert Elliott 		h->ioaccel_cmd_pool_dhandle = 0;
8341105a3dbcSRobert Elliott 	}
83421fb7c98aSRobert Elliott 	kfree(h->ioaccel1_blockFetchTable);
8343105a3dbcSRobert Elliott 	h->ioaccel1_blockFetchTable = NULL;
83441fb7c98aSRobert Elliott }
83451fb7c98aSRobert Elliott 
8346d37ffbe4SRobert Elliott /* Allocate ioaccel1 mode command blocks and block fetch table */
8347d37ffbe4SRobert Elliott static int hpsa_alloc_ioaccel1_cmd_and_bft(struct ctlr_info *h)
8348e1f7de0cSMatt Gates {
8349283b4a9bSStephen M. Cameron 	h->ioaccel_maxsg =
8350283b4a9bSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
8351283b4a9bSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
8352283b4a9bSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
8353283b4a9bSStephen M. Cameron 
8354e1f7de0cSMatt Gates 	/* Command structures must be aligned on a 128-byte boundary
8355e1f7de0cSMatt Gates 	 * because the 7 lower bits of the address are used by the
8356e1f7de0cSMatt Gates 	 * hardware.
8357e1f7de0cSMatt Gates 	 */
8358e1f7de0cSMatt Gates 	BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
8359e1f7de0cSMatt Gates 			IOACCEL1_COMMANDLIST_ALIGNMENT);
8360e1f7de0cSMatt Gates 	h->ioaccel_cmd_pool =
8361e1f7de0cSMatt Gates 		pci_alloc_consistent(h->pdev,
8362e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
8363e1f7de0cSMatt Gates 			&(h->ioaccel_cmd_pool_dhandle));
8364e1f7de0cSMatt Gates 
8365e1f7de0cSMatt Gates 	h->ioaccel1_blockFetchTable =
8366283b4a9bSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
8367e1f7de0cSMatt Gates 				sizeof(u32)), GFP_KERNEL);
8368e1f7de0cSMatt Gates 
8369e1f7de0cSMatt Gates 	if ((h->ioaccel_cmd_pool == NULL) ||
8370e1f7de0cSMatt Gates 		(h->ioaccel1_blockFetchTable == NULL))
8371e1f7de0cSMatt Gates 		goto clean_up;
8372e1f7de0cSMatt Gates 
8373e1f7de0cSMatt Gates 	memset(h->ioaccel_cmd_pool, 0,
8374e1f7de0cSMatt Gates 		h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
8375e1f7de0cSMatt Gates 	return 0;
8376e1f7de0cSMatt Gates 
8377e1f7de0cSMatt Gates clean_up:
83781fb7c98aSRobert Elliott 	hpsa_free_ioaccel1_cmd_and_bft(h);
83792dd02d74SRobert Elliott 	return -ENOMEM;
83806c311b57SStephen M. Cameron }
83816c311b57SStephen M. Cameron 
83821fb7c98aSRobert Elliott /* Free ioaccel2 mode command blocks and block fetch table */
83831fb7c98aSRobert Elliott static void hpsa_free_ioaccel2_cmd_and_bft(struct ctlr_info *h)
83841fb7c98aSRobert Elliott {
8385d9a729f3SWebb Scales 	hpsa_free_ioaccel2_sg_chain_blocks(h);
8386d9a729f3SWebb Scales 
8387105a3dbcSRobert Elliott 	if (h->ioaccel2_cmd_pool) {
83881fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
83891fb7c98aSRobert Elliott 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
83901fb7c98aSRobert Elliott 			h->ioaccel2_cmd_pool,
83911fb7c98aSRobert Elliott 			h->ioaccel2_cmd_pool_dhandle);
8392105a3dbcSRobert Elliott 		h->ioaccel2_cmd_pool = NULL;
8393105a3dbcSRobert Elliott 		h->ioaccel2_cmd_pool_dhandle = 0;
8394105a3dbcSRobert Elliott 	}
83951fb7c98aSRobert Elliott 	kfree(h->ioaccel2_blockFetchTable);
8396105a3dbcSRobert Elliott 	h->ioaccel2_blockFetchTable = NULL;
83971fb7c98aSRobert Elliott }
83981fb7c98aSRobert Elliott 
8399d37ffbe4SRobert Elliott /* Allocate ioaccel2 mode command blocks and block fetch table */
8400d37ffbe4SRobert Elliott static int hpsa_alloc_ioaccel2_cmd_and_bft(struct ctlr_info *h)
8401aca9012aSStephen M. Cameron {
8402d9a729f3SWebb Scales 	int rc;
8403d9a729f3SWebb Scales 
8404aca9012aSStephen M. Cameron 	/* Allocate ioaccel2 mode command blocks and block fetch table */
8405aca9012aSStephen M. Cameron 
8406aca9012aSStephen M. Cameron 	h->ioaccel_maxsg =
8407aca9012aSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
8408aca9012aSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
8409aca9012aSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
8410aca9012aSStephen M. Cameron 
8411aca9012aSStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
8412aca9012aSStephen M. Cameron 			IOACCEL2_COMMANDLIST_ALIGNMENT);
8413aca9012aSStephen M. Cameron 	h->ioaccel2_cmd_pool =
8414aca9012aSStephen M. Cameron 		pci_alloc_consistent(h->pdev,
8415aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
8416aca9012aSStephen M. Cameron 			&(h->ioaccel2_cmd_pool_dhandle));
8417aca9012aSStephen M. Cameron 
8418aca9012aSStephen M. Cameron 	h->ioaccel2_blockFetchTable =
8419aca9012aSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
8420aca9012aSStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
8421aca9012aSStephen M. Cameron 
8422aca9012aSStephen M. Cameron 	if ((h->ioaccel2_cmd_pool == NULL) ||
8423d9a729f3SWebb Scales 		(h->ioaccel2_blockFetchTable == NULL)) {
8424d9a729f3SWebb Scales 		rc = -ENOMEM;
8425d9a729f3SWebb Scales 		goto clean_up;
8426d9a729f3SWebb Scales 	}
8427d9a729f3SWebb Scales 
8428d9a729f3SWebb Scales 	rc = hpsa_allocate_ioaccel2_sg_chain_blocks(h);
8429d9a729f3SWebb Scales 	if (rc)
8430aca9012aSStephen M. Cameron 		goto clean_up;
8431aca9012aSStephen M. Cameron 
8432aca9012aSStephen M. Cameron 	memset(h->ioaccel2_cmd_pool, 0,
8433aca9012aSStephen M. Cameron 		h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
8434aca9012aSStephen M. Cameron 	return 0;
8435aca9012aSStephen M. Cameron 
8436aca9012aSStephen M. Cameron clean_up:
84371fb7c98aSRobert Elliott 	hpsa_free_ioaccel2_cmd_and_bft(h);
8438d9a729f3SWebb Scales 	return rc;
8439aca9012aSStephen M. Cameron }
8440aca9012aSStephen M. Cameron 
8441105a3dbcSRobert Elliott /* Free items allocated by hpsa_put_ctlr_into_performant_mode */
8442105a3dbcSRobert Elliott static void hpsa_free_performant_mode(struct ctlr_info *h)
8443105a3dbcSRobert Elliott {
8444105a3dbcSRobert Elliott 	kfree(h->blockFetchTable);
8445105a3dbcSRobert Elliott 	h->blockFetchTable = NULL;
8446105a3dbcSRobert Elliott 	hpsa_free_reply_queues(h);
8447105a3dbcSRobert Elliott 	hpsa_free_ioaccel1_cmd_and_bft(h);
8448105a3dbcSRobert Elliott 	hpsa_free_ioaccel2_cmd_and_bft(h);
8449105a3dbcSRobert Elliott }
8450105a3dbcSRobert Elliott 
8451105a3dbcSRobert Elliott /* return -ENODEV on error, 0 on success (or no action)
8452105a3dbcSRobert Elliott  * allocates numerous items that must be freed later
8453105a3dbcSRobert Elliott  */
8454105a3dbcSRobert Elliott static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
84556c311b57SStephen M. Cameron {
84566c311b57SStephen M. Cameron 	u32 trans_support;
8457e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
8458e1f7de0cSMatt Gates 					CFGTBL_Trans_use_short_tags;
8459105a3dbcSRobert Elliott 	int i, rc;
84606c311b57SStephen M. Cameron 
846102ec19c8SStephen M. Cameron 	if (hpsa_simple_mode)
8462105a3dbcSRobert Elliott 		return 0;
846302ec19c8SStephen M. Cameron 
846467c99a72Sscameron@beardog.cce.hp.com 	trans_support = readl(&(h->cfgtable->TransportSupport));
846567c99a72Sscameron@beardog.cce.hp.com 	if (!(trans_support & PERFORMANT_MODE))
8466105a3dbcSRobert Elliott 		return 0;
846767c99a72Sscameron@beardog.cce.hp.com 
8468e1f7de0cSMatt Gates 	/* Check for I/O accelerator mode support */
8469e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
8470e1f7de0cSMatt Gates 		transMethod |= CFGTBL_Trans_io_accel1 |
8471e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix;
8472105a3dbcSRobert Elliott 		rc = hpsa_alloc_ioaccel1_cmd_and_bft(h);
8473105a3dbcSRobert Elliott 		if (rc)
8474105a3dbcSRobert Elliott 			return rc;
8475105a3dbcSRobert Elliott 	} else if (trans_support & CFGTBL_Trans_io_accel2) {
8476aca9012aSStephen M. Cameron 		transMethod |= CFGTBL_Trans_io_accel2 |
8477aca9012aSStephen M. Cameron 				CFGTBL_Trans_enable_directed_msix;
8478105a3dbcSRobert Elliott 		rc = hpsa_alloc_ioaccel2_cmd_and_bft(h);
8479105a3dbcSRobert Elliott 		if (rc)
8480105a3dbcSRobert Elliott 			return rc;
8481e1f7de0cSMatt Gates 	}
8482e1f7de0cSMatt Gates 
8483eee0f03aSHannes Reinecke 	h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
8484cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
84856c311b57SStephen M. Cameron 	/* Performant mode ring buffer and supporting data structures */
8486072b0518SStephen M. Cameron 	h->reply_queue_size = h->max_commands * sizeof(u64);
84876c311b57SStephen M. Cameron 
8488254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
8489072b0518SStephen M. Cameron 		h->reply_queue[i].head = pci_alloc_consistent(h->pdev,
8490072b0518SStephen M. Cameron 						h->reply_queue_size,
8491072b0518SStephen M. Cameron 						&(h->reply_queue[i].busaddr));
8492105a3dbcSRobert Elliott 		if (!h->reply_queue[i].head) {
8493105a3dbcSRobert Elliott 			rc = -ENOMEM;
8494105a3dbcSRobert Elliott 			goto clean1;	/* rq, ioaccel */
8495105a3dbcSRobert Elliott 		}
8496254f796bSMatt Gates 		h->reply_queue[i].size = h->max_commands;
8497254f796bSMatt Gates 		h->reply_queue[i].wraparound = 1;  /* spec: init to 1 */
8498254f796bSMatt Gates 		h->reply_queue[i].current_entry = 0;
8499254f796bSMatt Gates 	}
8500254f796bSMatt Gates 
85016c311b57SStephen M. Cameron 	/* Need a block fetch table for performant mode */
8502d66ae08bSStephen M. Cameron 	h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
85036c311b57SStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
8504105a3dbcSRobert Elliott 	if (!h->blockFetchTable) {
8505105a3dbcSRobert Elliott 		rc = -ENOMEM;
8506105a3dbcSRobert Elliott 		goto clean1;	/* rq, ioaccel */
8507105a3dbcSRobert Elliott 	}
85086c311b57SStephen M. Cameron 
8509105a3dbcSRobert Elliott 	rc = hpsa_enter_performant_mode(h, trans_support);
8510105a3dbcSRobert Elliott 	if (rc)
8511105a3dbcSRobert Elliott 		goto clean2;	/* bft, rq, ioaccel */
8512105a3dbcSRobert Elliott 	return 0;
8513303932fdSDon Brace 
8514105a3dbcSRobert Elliott clean2:	/* bft, rq, ioaccel */
8515303932fdSDon Brace 	kfree(h->blockFetchTable);
8516105a3dbcSRobert Elliott 	h->blockFetchTable = NULL;
8517105a3dbcSRobert Elliott clean1:	/* rq, ioaccel */
8518105a3dbcSRobert Elliott 	hpsa_free_reply_queues(h);
8519105a3dbcSRobert Elliott 	hpsa_free_ioaccel1_cmd_and_bft(h);
8520105a3dbcSRobert Elliott 	hpsa_free_ioaccel2_cmd_and_bft(h);
8521105a3dbcSRobert Elliott 	return rc;
8522303932fdSDon Brace }
8523303932fdSDon Brace 
852423100dd9SStephen M. Cameron static int is_accelerated_cmd(struct CommandList *c)
852576438d08SStephen M. Cameron {
852623100dd9SStephen M. Cameron 	return c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_IOACCEL2;
852723100dd9SStephen M. Cameron }
852823100dd9SStephen M. Cameron 
852923100dd9SStephen M. Cameron static void hpsa_drain_accel_commands(struct ctlr_info *h)
853023100dd9SStephen M. Cameron {
853123100dd9SStephen M. Cameron 	struct CommandList *c = NULL;
8532f2405db8SDon Brace 	int i, accel_cmds_out;
8533281a7fd0SWebb Scales 	int refcount;
853476438d08SStephen M. Cameron 
8535f2405db8SDon Brace 	do { /* wait for all outstanding ioaccel commands to drain out */
853623100dd9SStephen M. Cameron 		accel_cmds_out = 0;
8537f2405db8SDon Brace 		for (i = 0; i < h->nr_cmds; i++) {
8538f2405db8SDon Brace 			c = h->cmd_pool + i;
8539281a7fd0SWebb Scales 			refcount = atomic_inc_return(&c->refcount);
8540281a7fd0SWebb Scales 			if (refcount > 1) /* Command is allocated */
854123100dd9SStephen M. Cameron 				accel_cmds_out += is_accelerated_cmd(c);
8542281a7fd0SWebb Scales 			cmd_free(h, c);
8543f2405db8SDon Brace 		}
854423100dd9SStephen M. Cameron 		if (accel_cmds_out <= 0)
854576438d08SStephen M. Cameron 			break;
854676438d08SStephen M. Cameron 		msleep(100);
854776438d08SStephen M. Cameron 	} while (1);
854876438d08SStephen M. Cameron }
854976438d08SStephen M. Cameron 
8550edd16368SStephen M. Cameron /*
8551edd16368SStephen M. Cameron  *  This is it.  Register the PCI driver information for the cards we control
8552edd16368SStephen M. Cameron  *  the OS will call our registered routines when it finds one of our cards.
8553edd16368SStephen M. Cameron  */
8554edd16368SStephen M. Cameron static int __init hpsa_init(void)
8555edd16368SStephen M. Cameron {
855631468401SMike Miller 	return pci_register_driver(&hpsa_pci_driver);
8557edd16368SStephen M. Cameron }
8558edd16368SStephen M. Cameron 
8559edd16368SStephen M. Cameron static void __exit hpsa_cleanup(void)
8560edd16368SStephen M. Cameron {
8561edd16368SStephen M. Cameron 	pci_unregister_driver(&hpsa_pci_driver);
8562edd16368SStephen M. Cameron }
8563edd16368SStephen M. Cameron 
8564e1f7de0cSMatt Gates static void __attribute__((unused)) verify_offsets(void)
8565e1f7de0cSMatt Gates {
8566e1f7de0cSMatt Gates #define VERIFY_OFFSET(member, offset) \
8567dd0e19f3SScott Teel 	BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
8568dd0e19f3SScott Teel 
8569dd0e19f3SScott Teel 	VERIFY_OFFSET(structure_size, 0);
8570dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_size, 4);
8571dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_cnt, 8);
8572dd0e19f3SScott Teel 	VERIFY_OFFSET(phys_blk_shift, 16);
8573dd0e19f3SScott Teel 	VERIFY_OFFSET(parity_rotation_shift, 17);
8574dd0e19f3SScott Teel 	VERIFY_OFFSET(strip_size, 18);
8575dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_starting_blk, 20);
8576dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_blk_cnt, 28);
8577dd0e19f3SScott Teel 	VERIFY_OFFSET(data_disks_per_row, 36);
8578dd0e19f3SScott Teel 	VERIFY_OFFSET(metadata_disks_per_row, 38);
8579dd0e19f3SScott Teel 	VERIFY_OFFSET(row_cnt, 40);
8580dd0e19f3SScott Teel 	VERIFY_OFFSET(layout_map_count, 42);
8581dd0e19f3SScott Teel 	VERIFY_OFFSET(flags, 44);
8582dd0e19f3SScott Teel 	VERIFY_OFFSET(dekindex, 46);
8583dd0e19f3SScott Teel 	/* VERIFY_OFFSET(reserved, 48 */
8584dd0e19f3SScott Teel 	VERIFY_OFFSET(data, 64);
8585dd0e19f3SScott Teel 
8586dd0e19f3SScott Teel #undef VERIFY_OFFSET
8587dd0e19f3SScott Teel 
8588dd0e19f3SScott Teel #define VERIFY_OFFSET(member, offset) \
8589b66cc250SMike Miller 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
8590b66cc250SMike Miller 
8591b66cc250SMike Miller 	VERIFY_OFFSET(IU_type, 0);
8592b66cc250SMike Miller 	VERIFY_OFFSET(direction, 1);
8593b66cc250SMike Miller 	VERIFY_OFFSET(reply_queue, 2);
8594b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved1, 3);  */
8595b66cc250SMike Miller 	VERIFY_OFFSET(scsi_nexus, 4);
8596b66cc250SMike Miller 	VERIFY_OFFSET(Tag, 8);
8597b66cc250SMike Miller 	VERIFY_OFFSET(cdb, 16);
8598b66cc250SMike Miller 	VERIFY_OFFSET(cciss_lun, 32);
8599b66cc250SMike Miller 	VERIFY_OFFSET(data_len, 40);
8600b66cc250SMike Miller 	VERIFY_OFFSET(cmd_priority_task_attr, 44);
8601b66cc250SMike Miller 	VERIFY_OFFSET(sg_count, 45);
8602b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved3 */
8603b66cc250SMike Miller 	VERIFY_OFFSET(err_ptr, 48);
8604b66cc250SMike Miller 	VERIFY_OFFSET(err_len, 56);
8605b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved4  */
8606b66cc250SMike Miller 	VERIFY_OFFSET(sg, 64);
8607b66cc250SMike Miller 
8608b66cc250SMike Miller #undef VERIFY_OFFSET
8609b66cc250SMike Miller 
8610b66cc250SMike Miller #define VERIFY_OFFSET(member, offset) \
8611e1f7de0cSMatt Gates 	BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
8612e1f7de0cSMatt Gates 
8613e1f7de0cSMatt Gates 	VERIFY_OFFSET(dev_handle, 0x00);
8614e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved1, 0x02);
8615e1f7de0cSMatt Gates 	VERIFY_OFFSET(function, 0x03);
8616e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved2, 0x04);
8617e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info, 0x0C);
8618e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved3, 0x10);
8619e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info_len, 0x12);
8620e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved4, 0x13);
8621e1f7de0cSMatt Gates 	VERIFY_OFFSET(sgl_offset, 0x14);
8622e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved5, 0x15);
8623e1f7de0cSMatt Gates 	VERIFY_OFFSET(transfer_len, 0x1C);
8624e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved6, 0x20);
8625e1f7de0cSMatt Gates 	VERIFY_OFFSET(io_flags, 0x24);
8626e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved7, 0x26);
8627e1f7de0cSMatt Gates 	VERIFY_OFFSET(LUN, 0x34);
8628e1f7de0cSMatt Gates 	VERIFY_OFFSET(control, 0x3C);
8629e1f7de0cSMatt Gates 	VERIFY_OFFSET(CDB, 0x40);
8630e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved8, 0x50);
8631e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_context_flags, 0x60);
8632e1f7de0cSMatt Gates 	VERIFY_OFFSET(timeout_sec, 0x62);
8633e1f7de0cSMatt Gates 	VERIFY_OFFSET(ReplyQueue, 0x64);
8634e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved9, 0x65);
863550a0decfSStephen M. Cameron 	VERIFY_OFFSET(tag, 0x68);
8636e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_addr, 0x70);
8637e1f7de0cSMatt Gates 	VERIFY_OFFSET(CISS_LUN, 0x78);
8638e1f7de0cSMatt Gates 	VERIFY_OFFSET(SG, 0x78 + 8);
8639e1f7de0cSMatt Gates #undef VERIFY_OFFSET
8640e1f7de0cSMatt Gates }
8641e1f7de0cSMatt Gates 
8642edd16368SStephen M. Cameron module_init(hpsa_init);
8643edd16368SStephen M. Cameron module_exit(hpsa_cleanup);
8644