xref: /openbmc/linux/drivers/scsi/hpsa.c (revision 8a0ff92cc342e6be0f4db5183b27446796c15d91)
1edd16368SStephen M. Cameron /*
2edd16368SStephen M. Cameron  *    Disk Array driver for HP Smart Array SAS controllers
351c35139SScott Teel  *    Copyright 2000, 2014 Hewlett-Packard Development Company, L.P.
4edd16368SStephen M. Cameron  *
5edd16368SStephen M. Cameron  *    This program is free software; you can redistribute it and/or modify
6edd16368SStephen M. Cameron  *    it under the terms of the GNU General Public License as published by
7edd16368SStephen M. Cameron  *    the Free Software Foundation; version 2 of the License.
8edd16368SStephen M. Cameron  *
9edd16368SStephen M. Cameron  *    This program is distributed in the hope that it will be useful,
10edd16368SStephen M. Cameron  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
11edd16368SStephen M. Cameron  *    MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
12edd16368SStephen M. Cameron  *    NON INFRINGEMENT.  See the GNU General Public License for more details.
13edd16368SStephen M. Cameron  *
14edd16368SStephen M. Cameron  *    You should have received a copy of the GNU General Public License
15edd16368SStephen M. Cameron  *    along with this program; if not, write to the Free Software
16edd16368SStephen M. Cameron  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17edd16368SStephen M. Cameron  *
18edd16368SStephen M. Cameron  *    Questions/Comments/Bugfixes to iss_storagedev@hp.com
19edd16368SStephen M. Cameron  *
20edd16368SStephen M. Cameron  */
21edd16368SStephen M. Cameron 
22edd16368SStephen M. Cameron #include <linux/module.h>
23edd16368SStephen M. Cameron #include <linux/interrupt.h>
24edd16368SStephen M. Cameron #include <linux/types.h>
25edd16368SStephen M. Cameron #include <linux/pci.h>
26e5a44df8SMatthew Garrett #include <linux/pci-aspm.h>
27edd16368SStephen M. Cameron #include <linux/kernel.h>
28edd16368SStephen M. Cameron #include <linux/slab.h>
29edd16368SStephen M. Cameron #include <linux/delay.h>
30edd16368SStephen M. Cameron #include <linux/fs.h>
31edd16368SStephen M. Cameron #include <linux/timer.h>
32edd16368SStephen M. Cameron #include <linux/init.h>
33edd16368SStephen M. Cameron #include <linux/spinlock.h>
34edd16368SStephen M. Cameron #include <linux/compat.h>
35edd16368SStephen M. Cameron #include <linux/blktrace_api.h>
36edd16368SStephen M. Cameron #include <linux/uaccess.h>
37edd16368SStephen M. Cameron #include <linux/io.h>
38edd16368SStephen M. Cameron #include <linux/dma-mapping.h>
39edd16368SStephen M. Cameron #include <linux/completion.h>
40edd16368SStephen M. Cameron #include <linux/moduleparam.h>
41edd16368SStephen M. Cameron #include <scsi/scsi.h>
42edd16368SStephen M. Cameron #include <scsi/scsi_cmnd.h>
43edd16368SStephen M. Cameron #include <scsi/scsi_device.h>
44edd16368SStephen M. Cameron #include <scsi/scsi_host.h>
45667e23d4SStephen M. Cameron #include <scsi/scsi_tcq.h>
469437ac43SStephen Cameron #include <scsi/scsi_eh.h>
47edd16368SStephen M. Cameron #include <linux/cciss_ioctl.h>
48edd16368SStephen M. Cameron #include <linux/string.h>
49edd16368SStephen M. Cameron #include <linux/bitmap.h>
5060063497SArun Sharma #include <linux/atomic.h>
51a0c12413SStephen M. Cameron #include <linux/jiffies.h>
5242a91641SDon Brace #include <linux/percpu-defs.h>
53094963daSStephen M. Cameron #include <linux/percpu.h>
542b08b3e9SDon Brace #include <asm/unaligned.h>
55283b4a9bSStephen M. Cameron #include <asm/div64.h>
56edd16368SStephen M. Cameron #include "hpsa_cmd.h"
57edd16368SStephen M. Cameron #include "hpsa.h"
58edd16368SStephen M. Cameron 
59edd16368SStephen M. Cameron /* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */
609a993302SStephen M. Cameron #define HPSA_DRIVER_VERSION "3.4.4-1"
61edd16368SStephen M. Cameron #define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
62f79cfec6SStephen M. Cameron #define HPSA "hpsa"
63edd16368SStephen M. Cameron 
64007e7aa9SRobert Elliott /* How long to wait for CISS doorbell communication */
65007e7aa9SRobert Elliott #define CLEAR_EVENT_WAIT_INTERVAL 20	/* ms for each msleep() call */
66007e7aa9SRobert Elliott #define MODE_CHANGE_WAIT_INTERVAL 10	/* ms for each msleep() call */
67007e7aa9SRobert Elliott #define MAX_CLEAR_EVENT_WAIT 30000	/* times 20 ms = 600 s */
68007e7aa9SRobert Elliott #define MAX_MODE_CHANGE_WAIT 2000	/* times 10 ms = 20 s */
69edd16368SStephen M. Cameron #define MAX_IOCTL_CONFIG_WAIT 1000
70edd16368SStephen M. Cameron 
71edd16368SStephen M. Cameron /*define how many times we will try a command because of bus resets */
72edd16368SStephen M. Cameron #define MAX_CMD_RETRIES 3
73edd16368SStephen M. Cameron 
74edd16368SStephen M. Cameron /* Embedded module documentation macros - see modules.h */
75edd16368SStephen M. Cameron MODULE_AUTHOR("Hewlett-Packard Company");
76edd16368SStephen M. Cameron MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
77edd16368SStephen M. Cameron 	HPSA_DRIVER_VERSION);
78edd16368SStephen M. Cameron MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
79edd16368SStephen M. Cameron MODULE_VERSION(HPSA_DRIVER_VERSION);
80edd16368SStephen M. Cameron MODULE_LICENSE("GPL");
81edd16368SStephen M. Cameron 
82edd16368SStephen M. Cameron static int hpsa_allow_any;
83edd16368SStephen M. Cameron module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
84edd16368SStephen M. Cameron MODULE_PARM_DESC(hpsa_allow_any,
85edd16368SStephen M. Cameron 		"Allow hpsa driver to access unknown HP Smart Array hardware");
8602ec19c8SStephen M. Cameron static int hpsa_simple_mode;
8702ec19c8SStephen M. Cameron module_param(hpsa_simple_mode, int, S_IRUGO|S_IWUSR);
8802ec19c8SStephen M. Cameron MODULE_PARM_DESC(hpsa_simple_mode,
8902ec19c8SStephen M. Cameron 	"Use 'simple mode' rather than 'performant mode'");
90edd16368SStephen M. Cameron 
91edd16368SStephen M. Cameron /* define the PCI info for the cards we can control */
92edd16368SStephen M. Cameron static const struct pci_device_id hpsa_pci_device_id[] = {
93edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3241},
94edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3243},
95edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3245},
96edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3247},
97edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3249},
98163dbcd8SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x324A},
99163dbcd8SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x324B},
100f8b01eb9SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3233},
1019143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3350},
1029143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3351},
1039143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3352},
1049143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3353},
1059143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3354},
1069143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3355},
1079143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3356},
108fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1921},
109fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1922},
110fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1923},
111fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1924},
112fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1926},
113fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1928},
11497b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1929},
11597b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BD},
11697b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BE},
11797b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BF},
11897b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C0},
11997b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C1},
12097b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C2},
12197b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C3},
12297b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C4},
12397b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C5},
1243b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C6},
12597b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C7},
12697b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C8},
12797b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C9},
1283b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CA},
1293b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CB},
1303b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CC},
1313b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CD},
1323b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CE},
1338e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0076},
1348e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0087},
1358e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x007D},
1368e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0088},
1378e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP, 0x333f, 0x103c, 0x333f},
138edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_ANY_ID,	PCI_ANY_ID, PCI_ANY_ID,
139edd16368SStephen M. Cameron 		PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
140edd16368SStephen M. Cameron 	{0,}
141edd16368SStephen M. Cameron };
142edd16368SStephen M. Cameron 
143edd16368SStephen M. Cameron MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
144edd16368SStephen M. Cameron 
145edd16368SStephen M. Cameron /*  board_id = Subsystem Device ID & Vendor ID
146edd16368SStephen M. Cameron  *  product = Marketing Name for the board
147edd16368SStephen M. Cameron  *  access = Address of the struct of function pointers
148edd16368SStephen M. Cameron  */
149edd16368SStephen M. Cameron static struct board_type products[] = {
150edd16368SStephen M. Cameron 	{0x3241103C, "Smart Array P212", &SA5_access},
151edd16368SStephen M. Cameron 	{0x3243103C, "Smart Array P410", &SA5_access},
152edd16368SStephen M. Cameron 	{0x3245103C, "Smart Array P410i", &SA5_access},
153edd16368SStephen M. Cameron 	{0x3247103C, "Smart Array P411", &SA5_access},
154edd16368SStephen M. Cameron 	{0x3249103C, "Smart Array P812", &SA5_access},
155163dbcd8SMike Miller 	{0x324A103C, "Smart Array P712m", &SA5_access},
156163dbcd8SMike Miller 	{0x324B103C, "Smart Array P711m", &SA5_access},
1577d2cce58SStephen M. Cameron 	{0x3233103C, "HP StorageWorks 1210m", &SA5_access}, /* alias of 333f */
158fe0c9610SMike Miller 	{0x3350103C, "Smart Array P222", &SA5_access},
159fe0c9610SMike Miller 	{0x3351103C, "Smart Array P420", &SA5_access},
160fe0c9610SMike Miller 	{0x3352103C, "Smart Array P421", &SA5_access},
161fe0c9610SMike Miller 	{0x3353103C, "Smart Array P822", &SA5_access},
162fe0c9610SMike Miller 	{0x3354103C, "Smart Array P420i", &SA5_access},
163fe0c9610SMike Miller 	{0x3355103C, "Smart Array P220i", &SA5_access},
164fe0c9610SMike Miller 	{0x3356103C, "Smart Array P721m", &SA5_access},
1651fd6c8e3SMike Miller 	{0x1921103C, "Smart Array P830i", &SA5_access},
1661fd6c8e3SMike Miller 	{0x1922103C, "Smart Array P430", &SA5_access},
1671fd6c8e3SMike Miller 	{0x1923103C, "Smart Array P431", &SA5_access},
1681fd6c8e3SMike Miller 	{0x1924103C, "Smart Array P830", &SA5_access},
1691fd6c8e3SMike Miller 	{0x1926103C, "Smart Array P731m", &SA5_access},
1701fd6c8e3SMike Miller 	{0x1928103C, "Smart Array P230i", &SA5_access},
1711fd6c8e3SMike Miller 	{0x1929103C, "Smart Array P530", &SA5_access},
17227fb8137SDon Brace 	{0x21BD103C, "Smart Array P244br", &SA5_access},
17327fb8137SDon Brace 	{0x21BE103C, "Smart Array P741m", &SA5_access},
17427fb8137SDon Brace 	{0x21BF103C, "Smart HBA H240ar", &SA5_access},
17527fb8137SDon Brace 	{0x21C0103C, "Smart Array P440ar", &SA5_access},
176c8ae0ab1SDon Brace 	{0x21C1103C, "Smart Array P840ar", &SA5_access},
17727fb8137SDon Brace 	{0x21C2103C, "Smart Array P440", &SA5_access},
17827fb8137SDon Brace 	{0x21C3103C, "Smart Array P441", &SA5_access},
17997b9f53dSMike Miller 	{0x21C4103C, "Smart Array", &SA5_access},
18027fb8137SDon Brace 	{0x21C5103C, "Smart Array P841", &SA5_access},
18127fb8137SDon Brace 	{0x21C6103C, "Smart HBA H244br", &SA5_access},
18227fb8137SDon Brace 	{0x21C7103C, "Smart HBA H240", &SA5_access},
18327fb8137SDon Brace 	{0x21C8103C, "Smart HBA H241", &SA5_access},
18497b9f53dSMike Miller 	{0x21C9103C, "Smart Array", &SA5_access},
18527fb8137SDon Brace 	{0x21CA103C, "Smart Array P246br", &SA5_access},
18627fb8137SDon Brace 	{0x21CB103C, "Smart Array P840", &SA5_access},
1873b7a45e5SJoe Handzik 	{0x21CC103C, "Smart Array", &SA5_access},
1883b7a45e5SJoe Handzik 	{0x21CD103C, "Smart Array", &SA5_access},
18927fb8137SDon Brace 	{0x21CE103C, "Smart HBA", &SA5_access},
1908e616a5eSStephen M. Cameron 	{0x00761590, "HP Storage P1224 Array Controller", &SA5_access},
1918e616a5eSStephen M. Cameron 	{0x00871590, "HP Storage P1224e Array Controller", &SA5_access},
1928e616a5eSStephen M. Cameron 	{0x007D1590, "HP Storage P1228 Array Controller", &SA5_access},
1938e616a5eSStephen M. Cameron 	{0x00881590, "HP Storage P1228e Array Controller", &SA5_access},
1948e616a5eSStephen M. Cameron 	{0x333f103c, "HP StorageWorks 1210m Array Controller", &SA5_access},
195edd16368SStephen M. Cameron 	{0xFFFF103C, "Unknown Smart Array", &SA5_access},
196edd16368SStephen M. Cameron };
197edd16368SStephen M. Cameron 
198edd16368SStephen M. Cameron static int number_of_controllers;
199edd16368SStephen M. Cameron 
20010f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id);
20110f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id);
20242a91641SDon Brace static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg);
203edd16368SStephen M. Cameron 
204edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
20542a91641SDon Brace static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd,
20642a91641SDon Brace 	void __user *arg);
207edd16368SStephen M. Cameron #endif
208edd16368SStephen M. Cameron 
209edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c);
210edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h);
211a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
212b7bb24ebSStephen M. Cameron 	void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
213edd16368SStephen M. Cameron 	int cmd_type);
2142c143342SRobert Elliott static void hpsa_free_cmd_pool(struct ctlr_info *h);
215b7bb24ebSStephen M. Cameron #define VPD_PAGE (1 << 8)
216edd16368SStephen M. Cameron 
217f281233dSJeff Garzik static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
218a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *);
219a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
220a08a8471SStephen M. Cameron 	unsigned long elapsed_time);
2217c0a0229SDon Brace static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth);
222edd16368SStephen M. Cameron 
223edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
22475167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *scsicmd);
225edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev);
22641ce4c35SStephen Cameron static int hpsa_slave_configure(struct scsi_device *sdev);
227edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev);
228edd16368SStephen M. Cameron 
229edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
230edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
231edd16368SStephen M. Cameron 	struct CommandList *c);
232edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
233edd16368SStephen M. Cameron 	struct CommandList *c);
234303932fdSDon Brace /* performant mode helper functions */
235303932fdSDon Brace static void calc_bucket_map(int *bucket, int num_buckets,
2362b08b3e9SDon Brace 	int nsgs, int min_blocks, u32 *bucket_map);
237105a3dbcSRobert Elliott static void hpsa_free_performant_mode(struct ctlr_info *h);
238105a3dbcSRobert Elliott static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
239254f796bSMatt Gates static inline u32 next_command(struct ctlr_info *h, u8 q);
2406f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
2416f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
2421df8552aSStephen M. Cameron 			       u64 *cfg_offset);
2436f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
2441df8552aSStephen M. Cameron 				    unsigned long *memory_bar);
2456f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
2466f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
2476f039790SGreg Kroah-Hartman 				     int wait_for_ready);
24875167d2cSStephen M. Cameron static inline void finish_cmd(struct CommandList *c);
249c706a795SRobert Elliott static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h);
250fe5389c8SStephen M. Cameron #define BOARD_NOT_READY 0
251fe5389c8SStephen M. Cameron #define BOARD_READY 1
25223100dd9SStephen M. Cameron static void hpsa_drain_accel_commands(struct ctlr_info *h);
25376438d08SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h);
254c349775eSScott Teel static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
255c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
25603383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk);
257080ef1ccSDon Brace static void hpsa_command_resubmit_worker(struct work_struct *work);
25825163bd5SWebb Scales static u32 lockup_detected(struct ctlr_info *h);
25925163bd5SWebb Scales static int detect_controller_lockup(struct ctlr_info *h);
260edd16368SStephen M. Cameron 
261edd16368SStephen M. Cameron static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
262edd16368SStephen M. Cameron {
263edd16368SStephen M. Cameron 	unsigned long *priv = shost_priv(sdev->host);
264edd16368SStephen M. Cameron 	return (struct ctlr_info *) *priv;
265edd16368SStephen M. Cameron }
266edd16368SStephen M. Cameron 
267a23513e8SStephen M. Cameron static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
268a23513e8SStephen M. Cameron {
269a23513e8SStephen M. Cameron 	unsigned long *priv = shost_priv(sh);
270a23513e8SStephen M. Cameron 	return (struct ctlr_info *) *priv;
271a23513e8SStephen M. Cameron }
272a23513e8SStephen M. Cameron 
2739437ac43SStephen Cameron /* extract sense key, asc, and ascq from sense data.  -1 means invalid. */
2749437ac43SStephen Cameron static void decode_sense_data(const u8 *sense_data, int sense_data_len,
2759437ac43SStephen Cameron 			u8 *sense_key, u8 *asc, u8 *ascq)
2769437ac43SStephen Cameron {
2779437ac43SStephen Cameron 	struct scsi_sense_hdr sshdr;
2789437ac43SStephen Cameron 	bool rc;
2799437ac43SStephen Cameron 
2809437ac43SStephen Cameron 	*sense_key = -1;
2819437ac43SStephen Cameron 	*asc = -1;
2829437ac43SStephen Cameron 	*ascq = -1;
2839437ac43SStephen Cameron 
2849437ac43SStephen Cameron 	if (sense_data_len < 1)
2859437ac43SStephen Cameron 		return;
2869437ac43SStephen Cameron 
2879437ac43SStephen Cameron 	rc = scsi_normalize_sense(sense_data, sense_data_len, &sshdr);
2889437ac43SStephen Cameron 	if (rc) {
2899437ac43SStephen Cameron 		*sense_key = sshdr.sense_key;
2909437ac43SStephen Cameron 		*asc = sshdr.asc;
2919437ac43SStephen Cameron 		*ascq = sshdr.ascq;
2929437ac43SStephen Cameron 	}
2939437ac43SStephen Cameron }
2949437ac43SStephen Cameron 
295edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
296edd16368SStephen M. Cameron 	struct CommandList *c)
297edd16368SStephen M. Cameron {
2989437ac43SStephen Cameron 	u8 sense_key, asc, ascq;
2999437ac43SStephen Cameron 	int sense_len;
3009437ac43SStephen Cameron 
3019437ac43SStephen Cameron 	if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
3029437ac43SStephen Cameron 		sense_len = sizeof(c->err_info->SenseInfo);
3039437ac43SStephen Cameron 	else
3049437ac43SStephen Cameron 		sense_len = c->err_info->SenseLen;
3059437ac43SStephen Cameron 
3069437ac43SStephen Cameron 	decode_sense_data(c->err_info->SenseInfo, sense_len,
3079437ac43SStephen Cameron 				&sense_key, &asc, &ascq);
3089437ac43SStephen Cameron 	if (sense_key != UNIT_ATTENTION || asc == -1)
309edd16368SStephen M. Cameron 		return 0;
310edd16368SStephen M. Cameron 
3119437ac43SStephen Cameron 	switch (asc) {
312edd16368SStephen M. Cameron 	case STATE_CHANGED:
3139437ac43SStephen Cameron 		dev_warn(&h->pdev->dev,
3149437ac43SStephen Cameron 			HPSA "%d: a state change detected, command retried\n",
3159437ac43SStephen Cameron 			h->ctlr);
316edd16368SStephen M. Cameron 		break;
317edd16368SStephen M. Cameron 	case LUN_FAILED:
3187f73695aSStephen M. Cameron 		dev_warn(&h->pdev->dev,
3197f73695aSStephen M. Cameron 			HPSA "%d: LUN failure detected\n", h->ctlr);
320edd16368SStephen M. Cameron 		break;
321edd16368SStephen M. Cameron 	case REPORT_LUNS_CHANGED:
3227f73695aSStephen M. Cameron 		dev_warn(&h->pdev->dev,
3237f73695aSStephen M. Cameron 			HPSA "%d: report LUN data changed\n", h->ctlr);
324edd16368SStephen M. Cameron 	/*
3254f4eb9f1SScott Teel 	 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
3264f4eb9f1SScott Teel 	 * target (array) devices.
327edd16368SStephen M. Cameron 	 */
328edd16368SStephen M. Cameron 		break;
329edd16368SStephen M. Cameron 	case POWER_OR_RESET:
330f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: a power on "
331edd16368SStephen M. Cameron 			"or device reset detected\n", h->ctlr);
332edd16368SStephen M. Cameron 		break;
333edd16368SStephen M. Cameron 	case UNIT_ATTENTION_CLEARED:
334f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: unit attention "
335edd16368SStephen M. Cameron 		    "cleared by another initiator\n", h->ctlr);
336edd16368SStephen M. Cameron 		break;
337edd16368SStephen M. Cameron 	default:
338f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: unknown "
339edd16368SStephen M. Cameron 			"unit attention detected\n", h->ctlr);
340edd16368SStephen M. Cameron 		break;
341edd16368SStephen M. Cameron 	}
342edd16368SStephen M. Cameron 	return 1;
343edd16368SStephen M. Cameron }
344edd16368SStephen M. Cameron 
345852af20aSMatt Bondurant static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
346852af20aSMatt Bondurant {
347852af20aSMatt Bondurant 	if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
348852af20aSMatt Bondurant 		(c->err_info->ScsiStatus != SAM_STAT_BUSY &&
349852af20aSMatt Bondurant 		 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
350852af20aSMatt Bondurant 		return 0;
351852af20aSMatt Bondurant 	dev_warn(&h->pdev->dev, HPSA "device busy");
352852af20aSMatt Bondurant 	return 1;
353852af20aSMatt Bondurant }
354852af20aSMatt Bondurant 
355e985c58fSStephen Cameron static u32 lockup_detected(struct ctlr_info *h);
356e985c58fSStephen Cameron static ssize_t host_show_lockup_detected(struct device *dev,
357e985c58fSStephen Cameron 		struct device_attribute *attr, char *buf)
358e985c58fSStephen Cameron {
359e985c58fSStephen Cameron 	int ld;
360e985c58fSStephen Cameron 	struct ctlr_info *h;
361e985c58fSStephen Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
362e985c58fSStephen Cameron 
363e985c58fSStephen Cameron 	h = shost_to_hba(shost);
364e985c58fSStephen Cameron 	ld = lockup_detected(h);
365e985c58fSStephen Cameron 
366e985c58fSStephen Cameron 	return sprintf(buf, "ld=%d\n", ld);
367e985c58fSStephen Cameron }
368e985c58fSStephen Cameron 
369da0697bdSScott Teel static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
370da0697bdSScott Teel 					 struct device_attribute *attr,
371da0697bdSScott Teel 					 const char *buf, size_t count)
372da0697bdSScott Teel {
373da0697bdSScott Teel 	int status, len;
374da0697bdSScott Teel 	struct ctlr_info *h;
375da0697bdSScott Teel 	struct Scsi_Host *shost = class_to_shost(dev);
376da0697bdSScott Teel 	char tmpbuf[10];
377da0697bdSScott Teel 
378da0697bdSScott Teel 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
379da0697bdSScott Teel 		return -EACCES;
380da0697bdSScott Teel 	len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
381da0697bdSScott Teel 	strncpy(tmpbuf, buf, len);
382da0697bdSScott Teel 	tmpbuf[len] = '\0';
383da0697bdSScott Teel 	if (sscanf(tmpbuf, "%d", &status) != 1)
384da0697bdSScott Teel 		return -EINVAL;
385da0697bdSScott Teel 	h = shost_to_hba(shost);
386da0697bdSScott Teel 	h->acciopath_status = !!status;
387da0697bdSScott Teel 	dev_warn(&h->pdev->dev,
388da0697bdSScott Teel 		"hpsa: HP SSD Smart Path %s via sysfs update.\n",
389da0697bdSScott Teel 		h->acciopath_status ? "enabled" : "disabled");
390da0697bdSScott Teel 	return count;
391da0697bdSScott Teel }
392da0697bdSScott Teel 
3932ba8bfc8SStephen M. Cameron static ssize_t host_store_raid_offload_debug(struct device *dev,
3942ba8bfc8SStephen M. Cameron 					 struct device_attribute *attr,
3952ba8bfc8SStephen M. Cameron 					 const char *buf, size_t count)
3962ba8bfc8SStephen M. Cameron {
3972ba8bfc8SStephen M. Cameron 	int debug_level, len;
3982ba8bfc8SStephen M. Cameron 	struct ctlr_info *h;
3992ba8bfc8SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
4002ba8bfc8SStephen M. Cameron 	char tmpbuf[10];
4012ba8bfc8SStephen M. Cameron 
4022ba8bfc8SStephen M. Cameron 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
4032ba8bfc8SStephen M. Cameron 		return -EACCES;
4042ba8bfc8SStephen M. Cameron 	len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
4052ba8bfc8SStephen M. Cameron 	strncpy(tmpbuf, buf, len);
4062ba8bfc8SStephen M. Cameron 	tmpbuf[len] = '\0';
4072ba8bfc8SStephen M. Cameron 	if (sscanf(tmpbuf, "%d", &debug_level) != 1)
4082ba8bfc8SStephen M. Cameron 		return -EINVAL;
4092ba8bfc8SStephen M. Cameron 	if (debug_level < 0)
4102ba8bfc8SStephen M. Cameron 		debug_level = 0;
4112ba8bfc8SStephen M. Cameron 	h = shost_to_hba(shost);
4122ba8bfc8SStephen M. Cameron 	h->raid_offload_debug = debug_level;
4132ba8bfc8SStephen M. Cameron 	dev_warn(&h->pdev->dev, "hpsa: Set raid_offload_debug level = %d\n",
4142ba8bfc8SStephen M. Cameron 		h->raid_offload_debug);
4152ba8bfc8SStephen M. Cameron 	return count;
4162ba8bfc8SStephen M. Cameron }
4172ba8bfc8SStephen M. Cameron 
418edd16368SStephen M. Cameron static ssize_t host_store_rescan(struct device *dev,
419edd16368SStephen M. Cameron 				 struct device_attribute *attr,
420edd16368SStephen M. Cameron 				 const char *buf, size_t count)
421edd16368SStephen M. Cameron {
422edd16368SStephen M. Cameron 	struct ctlr_info *h;
423edd16368SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
424a23513e8SStephen M. Cameron 	h = shost_to_hba(shost);
42531468401SMike Miller 	hpsa_scan_start(h->scsi_host);
426edd16368SStephen M. Cameron 	return count;
427edd16368SStephen M. Cameron }
428edd16368SStephen M. Cameron 
429d28ce020SStephen M. Cameron static ssize_t host_show_firmware_revision(struct device *dev,
430d28ce020SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
431d28ce020SStephen M. Cameron {
432d28ce020SStephen M. Cameron 	struct ctlr_info *h;
433d28ce020SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
434d28ce020SStephen M. Cameron 	unsigned char *fwrev;
435d28ce020SStephen M. Cameron 
436d28ce020SStephen M. Cameron 	h = shost_to_hba(shost);
437d28ce020SStephen M. Cameron 	if (!h->hba_inquiry_data)
438d28ce020SStephen M. Cameron 		return 0;
439d28ce020SStephen M. Cameron 	fwrev = &h->hba_inquiry_data[32];
440d28ce020SStephen M. Cameron 	return snprintf(buf, 20, "%c%c%c%c\n",
441d28ce020SStephen M. Cameron 		fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
442d28ce020SStephen M. Cameron }
443d28ce020SStephen M. Cameron 
44494a13649SStephen M. Cameron static ssize_t host_show_commands_outstanding(struct device *dev,
44594a13649SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
44694a13649SStephen M. Cameron {
44794a13649SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
44894a13649SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(shost);
44994a13649SStephen M. Cameron 
4500cbf768eSStephen M. Cameron 	return snprintf(buf, 20, "%d\n",
4510cbf768eSStephen M. Cameron 			atomic_read(&h->commands_outstanding));
45294a13649SStephen M. Cameron }
45394a13649SStephen M. Cameron 
454745a7a25SStephen M. Cameron static ssize_t host_show_transport_mode(struct device *dev,
455745a7a25SStephen M. Cameron 	struct device_attribute *attr, char *buf)
456745a7a25SStephen M. Cameron {
457745a7a25SStephen M. Cameron 	struct ctlr_info *h;
458745a7a25SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
459745a7a25SStephen M. Cameron 
460745a7a25SStephen M. Cameron 	h = shost_to_hba(shost);
461745a7a25SStephen M. Cameron 	return snprintf(buf, 20, "%s\n",
462960a30e7SStephen M. Cameron 		h->transMethod & CFGTBL_Trans_Performant ?
463745a7a25SStephen M. Cameron 			"performant" : "simple");
464745a7a25SStephen M. Cameron }
465745a7a25SStephen M. Cameron 
466da0697bdSScott Teel static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
467da0697bdSScott Teel 	struct device_attribute *attr, char *buf)
468da0697bdSScott Teel {
469da0697bdSScott Teel 	struct ctlr_info *h;
470da0697bdSScott Teel 	struct Scsi_Host *shost = class_to_shost(dev);
471da0697bdSScott Teel 
472da0697bdSScott Teel 	h = shost_to_hba(shost);
473da0697bdSScott Teel 	return snprintf(buf, 30, "HP SSD Smart Path %s\n",
474da0697bdSScott Teel 		(h->acciopath_status == 1) ?  "enabled" : "disabled");
475da0697bdSScott Teel }
476da0697bdSScott Teel 
47746380786SStephen M. Cameron /* List of controllers which cannot be hard reset on kexec with reset_devices */
478941b1cdaSStephen M. Cameron static u32 unresettable_controller[] = {
479941b1cdaSStephen M. Cameron 	0x324a103C, /* Smart Array P712m */
480941b1cdaSStephen M. Cameron 	0x324b103C, /* Smart Array P711m */
481941b1cdaSStephen M. Cameron 	0x3223103C, /* Smart Array P800 */
482941b1cdaSStephen M. Cameron 	0x3234103C, /* Smart Array P400 */
483941b1cdaSStephen M. Cameron 	0x3235103C, /* Smart Array P400i */
484941b1cdaSStephen M. Cameron 	0x3211103C, /* Smart Array E200i */
485941b1cdaSStephen M. Cameron 	0x3212103C, /* Smart Array E200 */
486941b1cdaSStephen M. Cameron 	0x3213103C, /* Smart Array E200i */
487941b1cdaSStephen M. Cameron 	0x3214103C, /* Smart Array E200i */
488941b1cdaSStephen M. Cameron 	0x3215103C, /* Smart Array E200i */
489941b1cdaSStephen M. Cameron 	0x3237103C, /* Smart Array E500 */
490941b1cdaSStephen M. Cameron 	0x323D103C, /* Smart Array P700m */
4917af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
492941b1cdaSStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
493941b1cdaSStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
4945a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
4955a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
4965a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
4975a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
4985a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
4995a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
500941b1cdaSStephen M. Cameron };
501941b1cdaSStephen M. Cameron 
50246380786SStephen M. Cameron /* List of controllers which cannot even be soft reset */
50346380786SStephen M. Cameron static u32 soft_unresettable_controller[] = {
5047af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
5055a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
5065a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
5075a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
5085a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
5095a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
5105a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
51146380786SStephen M. Cameron 	/* Exclude 640x boards.  These are two pci devices in one slot
51246380786SStephen M. Cameron 	 * which share a battery backed cache module.  One controls the
51346380786SStephen M. Cameron 	 * cache, the other accesses the cache through the one that controls
51446380786SStephen M. Cameron 	 * it.  If we reset the one controlling the cache, the other will
51546380786SStephen M. Cameron 	 * likely not be happy.  Just forbid resetting this conjoined mess.
51646380786SStephen M. Cameron 	 * The 640x isn't really supported by hpsa anyway.
51746380786SStephen M. Cameron 	 */
51846380786SStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
51946380786SStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
52046380786SStephen M. Cameron };
52146380786SStephen M. Cameron 
5229b5c48c2SStephen Cameron static u32 needs_abort_tags_swizzled[] = {
5239b5c48c2SStephen Cameron 	0x323D103C, /* Smart Array P700m */
5249b5c48c2SStephen Cameron 	0x324a103C, /* Smart Array P712m */
5259b5c48c2SStephen Cameron 	0x324b103C, /* SmartArray P711m */
5269b5c48c2SStephen Cameron };
5279b5c48c2SStephen Cameron 
5289b5c48c2SStephen Cameron static int board_id_in_array(u32 a[], int nelems, u32 board_id)
529941b1cdaSStephen M. Cameron {
530941b1cdaSStephen M. Cameron 	int i;
531941b1cdaSStephen M. Cameron 
5329b5c48c2SStephen Cameron 	for (i = 0; i < nelems; i++)
5339b5c48c2SStephen Cameron 		if (a[i] == board_id)
534941b1cdaSStephen M. Cameron 			return 1;
5359b5c48c2SStephen Cameron 	return 0;
5369b5c48c2SStephen Cameron }
5379b5c48c2SStephen Cameron 
5389b5c48c2SStephen Cameron static int ctlr_is_hard_resettable(u32 board_id)
5399b5c48c2SStephen Cameron {
5409b5c48c2SStephen Cameron 	return !board_id_in_array(unresettable_controller,
5419b5c48c2SStephen Cameron 			ARRAY_SIZE(unresettable_controller), board_id);
542941b1cdaSStephen M. Cameron }
543941b1cdaSStephen M. Cameron 
54446380786SStephen M. Cameron static int ctlr_is_soft_resettable(u32 board_id)
54546380786SStephen M. Cameron {
5469b5c48c2SStephen Cameron 	return !board_id_in_array(soft_unresettable_controller,
5479b5c48c2SStephen Cameron 			ARRAY_SIZE(soft_unresettable_controller), board_id);
54846380786SStephen M. Cameron }
54946380786SStephen M. Cameron 
55046380786SStephen M. Cameron static int ctlr_is_resettable(u32 board_id)
55146380786SStephen M. Cameron {
55246380786SStephen M. Cameron 	return ctlr_is_hard_resettable(board_id) ||
55346380786SStephen M. Cameron 		ctlr_is_soft_resettable(board_id);
55446380786SStephen M. Cameron }
55546380786SStephen M. Cameron 
5569b5c48c2SStephen Cameron static int ctlr_needs_abort_tags_swizzled(u32 board_id)
5579b5c48c2SStephen Cameron {
5589b5c48c2SStephen Cameron 	return board_id_in_array(needs_abort_tags_swizzled,
5599b5c48c2SStephen Cameron 			ARRAY_SIZE(needs_abort_tags_swizzled), board_id);
5609b5c48c2SStephen Cameron }
5619b5c48c2SStephen Cameron 
562941b1cdaSStephen M. Cameron static ssize_t host_show_resettable(struct device *dev,
563941b1cdaSStephen M. Cameron 	struct device_attribute *attr, char *buf)
564941b1cdaSStephen M. Cameron {
565941b1cdaSStephen M. Cameron 	struct ctlr_info *h;
566941b1cdaSStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
567941b1cdaSStephen M. Cameron 
568941b1cdaSStephen M. Cameron 	h = shost_to_hba(shost);
56946380786SStephen M. Cameron 	return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
570941b1cdaSStephen M. Cameron }
571941b1cdaSStephen M. Cameron 
572edd16368SStephen M. Cameron static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
573edd16368SStephen M. Cameron {
574edd16368SStephen M. Cameron 	return (scsi3addr[3] & 0xC0) == 0x40;
575edd16368SStephen M. Cameron }
576edd16368SStephen M. Cameron 
577f2ef0ce7SRobert Elliott static const char * const raid_label[] = { "0", "4", "1(+0)", "5", "5+1", "6",
578f2ef0ce7SRobert Elliott 	"1(+0)ADM", "UNKNOWN"
579edd16368SStephen M. Cameron };
5806b80b18fSScott Teel #define HPSA_RAID_0	0
5816b80b18fSScott Teel #define HPSA_RAID_4	1
5826b80b18fSScott Teel #define HPSA_RAID_1	2	/* also used for RAID 10 */
5836b80b18fSScott Teel #define HPSA_RAID_5	3	/* also used for RAID 50 */
5846b80b18fSScott Teel #define HPSA_RAID_51	4
5856b80b18fSScott Teel #define HPSA_RAID_6	5	/* also used for RAID 60 */
5866b80b18fSScott Teel #define HPSA_RAID_ADM	6	/* also used for RAID 1+0 ADM */
587edd16368SStephen M. Cameron #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
588edd16368SStephen M. Cameron 
589edd16368SStephen M. Cameron static ssize_t raid_level_show(struct device *dev,
590edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
591edd16368SStephen M. Cameron {
592edd16368SStephen M. Cameron 	ssize_t l = 0;
59382a72c0aSStephen M. Cameron 	unsigned char rlevel;
594edd16368SStephen M. Cameron 	struct ctlr_info *h;
595edd16368SStephen M. Cameron 	struct scsi_device *sdev;
596edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
597edd16368SStephen M. Cameron 	unsigned long flags;
598edd16368SStephen M. Cameron 
599edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
600edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
601edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
602edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
603edd16368SStephen M. Cameron 	if (!hdev) {
604edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
605edd16368SStephen M. Cameron 		return -ENODEV;
606edd16368SStephen M. Cameron 	}
607edd16368SStephen M. Cameron 
608edd16368SStephen M. Cameron 	/* Is this even a logical drive? */
609edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
610edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
611edd16368SStephen M. Cameron 		l = snprintf(buf, PAGE_SIZE, "N/A\n");
612edd16368SStephen M. Cameron 		return l;
613edd16368SStephen M. Cameron 	}
614edd16368SStephen M. Cameron 
615edd16368SStephen M. Cameron 	rlevel = hdev->raid_level;
616edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
61782a72c0aSStephen M. Cameron 	if (rlevel > RAID_UNKNOWN)
618edd16368SStephen M. Cameron 		rlevel = RAID_UNKNOWN;
619edd16368SStephen M. Cameron 	l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
620edd16368SStephen M. Cameron 	return l;
621edd16368SStephen M. Cameron }
622edd16368SStephen M. Cameron 
623edd16368SStephen M. Cameron static ssize_t lunid_show(struct device *dev,
624edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
625edd16368SStephen M. Cameron {
626edd16368SStephen M. Cameron 	struct ctlr_info *h;
627edd16368SStephen M. Cameron 	struct scsi_device *sdev;
628edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
629edd16368SStephen M. Cameron 	unsigned long flags;
630edd16368SStephen M. Cameron 	unsigned char lunid[8];
631edd16368SStephen M. Cameron 
632edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
633edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
634edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
635edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
636edd16368SStephen M. Cameron 	if (!hdev) {
637edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
638edd16368SStephen M. Cameron 		return -ENODEV;
639edd16368SStephen M. Cameron 	}
640edd16368SStephen M. Cameron 	memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
641edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
642edd16368SStephen M. Cameron 	return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
643edd16368SStephen M. Cameron 		lunid[0], lunid[1], lunid[2], lunid[3],
644edd16368SStephen M. Cameron 		lunid[4], lunid[5], lunid[6], lunid[7]);
645edd16368SStephen M. Cameron }
646edd16368SStephen M. Cameron 
647edd16368SStephen M. Cameron static ssize_t unique_id_show(struct device *dev,
648edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
649edd16368SStephen M. Cameron {
650edd16368SStephen M. Cameron 	struct ctlr_info *h;
651edd16368SStephen M. Cameron 	struct scsi_device *sdev;
652edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
653edd16368SStephen M. Cameron 	unsigned long flags;
654edd16368SStephen M. Cameron 	unsigned char sn[16];
655edd16368SStephen M. Cameron 
656edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
657edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
658edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
659edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
660edd16368SStephen M. Cameron 	if (!hdev) {
661edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
662edd16368SStephen M. Cameron 		return -ENODEV;
663edd16368SStephen M. Cameron 	}
664edd16368SStephen M. Cameron 	memcpy(sn, hdev->device_id, sizeof(sn));
665edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
666edd16368SStephen M. Cameron 	return snprintf(buf, 16 * 2 + 2,
667edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X"
668edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X\n",
669edd16368SStephen M. Cameron 			sn[0], sn[1], sn[2], sn[3],
670edd16368SStephen M. Cameron 			sn[4], sn[5], sn[6], sn[7],
671edd16368SStephen M. Cameron 			sn[8], sn[9], sn[10], sn[11],
672edd16368SStephen M. Cameron 			sn[12], sn[13], sn[14], sn[15]);
673edd16368SStephen M. Cameron }
674edd16368SStephen M. Cameron 
675c1988684SScott Teel static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
676c1988684SScott Teel 	     struct device_attribute *attr, char *buf)
677c1988684SScott Teel {
678c1988684SScott Teel 	struct ctlr_info *h;
679c1988684SScott Teel 	struct scsi_device *sdev;
680c1988684SScott Teel 	struct hpsa_scsi_dev_t *hdev;
681c1988684SScott Teel 	unsigned long flags;
682c1988684SScott Teel 	int offload_enabled;
683c1988684SScott Teel 
684c1988684SScott Teel 	sdev = to_scsi_device(dev);
685c1988684SScott Teel 	h = sdev_to_hba(sdev);
686c1988684SScott Teel 	spin_lock_irqsave(&h->lock, flags);
687c1988684SScott Teel 	hdev = sdev->hostdata;
688c1988684SScott Teel 	if (!hdev) {
689c1988684SScott Teel 		spin_unlock_irqrestore(&h->lock, flags);
690c1988684SScott Teel 		return -ENODEV;
691c1988684SScott Teel 	}
692c1988684SScott Teel 	offload_enabled = hdev->offload_enabled;
693c1988684SScott Teel 	spin_unlock_irqrestore(&h->lock, flags);
694c1988684SScott Teel 	return snprintf(buf, 20, "%d\n", offload_enabled);
695c1988684SScott Teel }
696c1988684SScott Teel 
6973f5eac3aSStephen M. Cameron static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
6983f5eac3aSStephen M. Cameron static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
6993f5eac3aSStephen M. Cameron static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
7003f5eac3aSStephen M. Cameron static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
701c1988684SScott Teel static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
702c1988684SScott Teel 			host_show_hp_ssd_smart_path_enabled, NULL);
703da0697bdSScott Teel static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
704da0697bdSScott Teel 		host_show_hp_ssd_smart_path_status,
705da0697bdSScott Teel 		host_store_hp_ssd_smart_path_status);
7062ba8bfc8SStephen M. Cameron static DEVICE_ATTR(raid_offload_debug, S_IWUSR, NULL,
7072ba8bfc8SStephen M. Cameron 			host_store_raid_offload_debug);
7083f5eac3aSStephen M. Cameron static DEVICE_ATTR(firmware_revision, S_IRUGO,
7093f5eac3aSStephen M. Cameron 	host_show_firmware_revision, NULL);
7103f5eac3aSStephen M. Cameron static DEVICE_ATTR(commands_outstanding, S_IRUGO,
7113f5eac3aSStephen M. Cameron 	host_show_commands_outstanding, NULL);
7123f5eac3aSStephen M. Cameron static DEVICE_ATTR(transport_mode, S_IRUGO,
7133f5eac3aSStephen M. Cameron 	host_show_transport_mode, NULL);
714941b1cdaSStephen M. Cameron static DEVICE_ATTR(resettable, S_IRUGO,
715941b1cdaSStephen M. Cameron 	host_show_resettable, NULL);
716e985c58fSStephen Cameron static DEVICE_ATTR(lockup_detected, S_IRUGO,
717e985c58fSStephen Cameron 	host_show_lockup_detected, NULL);
7183f5eac3aSStephen M. Cameron 
7193f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_sdev_attrs[] = {
7203f5eac3aSStephen M. Cameron 	&dev_attr_raid_level,
7213f5eac3aSStephen M. Cameron 	&dev_attr_lunid,
7223f5eac3aSStephen M. Cameron 	&dev_attr_unique_id,
723c1988684SScott Teel 	&dev_attr_hp_ssd_smart_path_enabled,
724e985c58fSStephen Cameron 	&dev_attr_lockup_detected,
7253f5eac3aSStephen M. Cameron 	NULL,
7263f5eac3aSStephen M. Cameron };
7273f5eac3aSStephen M. Cameron 
7283f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_shost_attrs[] = {
7293f5eac3aSStephen M. Cameron 	&dev_attr_rescan,
7303f5eac3aSStephen M. Cameron 	&dev_attr_firmware_revision,
7313f5eac3aSStephen M. Cameron 	&dev_attr_commands_outstanding,
7323f5eac3aSStephen M. Cameron 	&dev_attr_transport_mode,
733941b1cdaSStephen M. Cameron 	&dev_attr_resettable,
734da0697bdSScott Teel 	&dev_attr_hp_ssd_smart_path_status,
7352ba8bfc8SStephen M. Cameron 	&dev_attr_raid_offload_debug,
7363f5eac3aSStephen M. Cameron 	NULL,
7373f5eac3aSStephen M. Cameron };
7383f5eac3aSStephen M. Cameron 
73941ce4c35SStephen Cameron #define HPSA_NRESERVED_CMDS	(HPSA_CMDS_RESERVED_FOR_ABORTS + \
74041ce4c35SStephen Cameron 		HPSA_CMDS_RESERVED_FOR_DRIVER + HPSA_MAX_CONCURRENT_PASSTHRUS)
74141ce4c35SStephen Cameron 
7423f5eac3aSStephen M. Cameron static struct scsi_host_template hpsa_driver_template = {
7433f5eac3aSStephen M. Cameron 	.module			= THIS_MODULE,
744f79cfec6SStephen M. Cameron 	.name			= HPSA,
745f79cfec6SStephen M. Cameron 	.proc_name		= HPSA,
7463f5eac3aSStephen M. Cameron 	.queuecommand		= hpsa_scsi_queue_command,
7473f5eac3aSStephen M. Cameron 	.scan_start		= hpsa_scan_start,
7483f5eac3aSStephen M. Cameron 	.scan_finished		= hpsa_scan_finished,
7497c0a0229SDon Brace 	.change_queue_depth	= hpsa_change_queue_depth,
7503f5eac3aSStephen M. Cameron 	.this_id		= -1,
7513f5eac3aSStephen M. Cameron 	.use_clustering		= ENABLE_CLUSTERING,
75275167d2cSStephen M. Cameron 	.eh_abort_handler	= hpsa_eh_abort_handler,
7533f5eac3aSStephen M. Cameron 	.eh_device_reset_handler = hpsa_eh_device_reset_handler,
7543f5eac3aSStephen M. Cameron 	.ioctl			= hpsa_ioctl,
7553f5eac3aSStephen M. Cameron 	.slave_alloc		= hpsa_slave_alloc,
75641ce4c35SStephen Cameron 	.slave_configure	= hpsa_slave_configure,
7573f5eac3aSStephen M. Cameron 	.slave_destroy		= hpsa_slave_destroy,
7583f5eac3aSStephen M. Cameron #ifdef CONFIG_COMPAT
7593f5eac3aSStephen M. Cameron 	.compat_ioctl		= hpsa_compat_ioctl,
7603f5eac3aSStephen M. Cameron #endif
7613f5eac3aSStephen M. Cameron 	.sdev_attrs = hpsa_sdev_attrs,
7623f5eac3aSStephen M. Cameron 	.shost_attrs = hpsa_shost_attrs,
763c0d6a4d1SStephen M. Cameron 	.max_sectors = 8192,
76454b2b50cSMartin K. Petersen 	.no_write_same = 1,
7653f5eac3aSStephen M. Cameron };
7663f5eac3aSStephen M. Cameron 
767254f796bSMatt Gates static inline u32 next_command(struct ctlr_info *h, u8 q)
7683f5eac3aSStephen M. Cameron {
7693f5eac3aSStephen M. Cameron 	u32 a;
770072b0518SStephen M. Cameron 	struct reply_queue_buffer *rq = &h->reply_queue[q];
7713f5eac3aSStephen M. Cameron 
772e1f7de0cSMatt Gates 	if (h->transMethod & CFGTBL_Trans_io_accel1)
773e1f7de0cSMatt Gates 		return h->access.command_completed(h, q);
774e1f7de0cSMatt Gates 
7753f5eac3aSStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
776254f796bSMatt Gates 		return h->access.command_completed(h, q);
7773f5eac3aSStephen M. Cameron 
778254f796bSMatt Gates 	if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
779254f796bSMatt Gates 		a = rq->head[rq->current_entry];
780254f796bSMatt Gates 		rq->current_entry++;
7810cbf768eSStephen M. Cameron 		atomic_dec(&h->commands_outstanding);
7823f5eac3aSStephen M. Cameron 	} else {
7833f5eac3aSStephen M. Cameron 		a = FIFO_EMPTY;
7843f5eac3aSStephen M. Cameron 	}
7853f5eac3aSStephen M. Cameron 	/* Check for wraparound */
786254f796bSMatt Gates 	if (rq->current_entry == h->max_commands) {
787254f796bSMatt Gates 		rq->current_entry = 0;
788254f796bSMatt Gates 		rq->wraparound ^= 1;
7893f5eac3aSStephen M. Cameron 	}
7903f5eac3aSStephen M. Cameron 	return a;
7913f5eac3aSStephen M. Cameron }
7923f5eac3aSStephen M. Cameron 
793c349775eSScott Teel /*
794c349775eSScott Teel  * There are some special bits in the bus address of the
795c349775eSScott Teel  * command that we have to set for the controller to know
796c349775eSScott Teel  * how to process the command:
797c349775eSScott Teel  *
798c349775eSScott Teel  * Normal performant mode:
799c349775eSScott Teel  * bit 0: 1 means performant mode, 0 means simple mode.
800c349775eSScott Teel  * bits 1-3 = block fetch table entry
801c349775eSScott Teel  * bits 4-6 = command type (== 0)
802c349775eSScott Teel  *
803c349775eSScott Teel  * ioaccel1 mode:
804c349775eSScott Teel  * bit 0 = "performant mode" bit.
805c349775eSScott Teel  * bits 1-3 = block fetch table entry
806c349775eSScott Teel  * bits 4-6 = command type (== 110)
807c349775eSScott Teel  * (command type is needed because ioaccel1 mode
808c349775eSScott Teel  * commands are submitted through the same register as normal
809c349775eSScott Teel  * mode commands, so this is how the controller knows whether
810c349775eSScott Teel  * the command is normal mode or ioaccel1 mode.)
811c349775eSScott Teel  *
812c349775eSScott Teel  * ioaccel2 mode:
813c349775eSScott Teel  * bit 0 = "performant mode" bit.
814c349775eSScott Teel  * bits 1-4 = block fetch table entry (note extra bit)
815c349775eSScott Teel  * bits 4-6 = not needed, because ioaccel2 mode has
816c349775eSScott Teel  * a separate special register for submitting commands.
817c349775eSScott Teel  */
818c349775eSScott Teel 
81925163bd5SWebb Scales /*
82025163bd5SWebb Scales  * set_performant_mode: Modify the tag for cciss performant
8213f5eac3aSStephen M. Cameron  * set bit 0 for pull model, bits 3-1 for block fetch
8223f5eac3aSStephen M. Cameron  * register number
8233f5eac3aSStephen M. Cameron  */
82425163bd5SWebb Scales #define DEFAULT_REPLY_QUEUE (-1)
82525163bd5SWebb Scales static void set_performant_mode(struct ctlr_info *h, struct CommandList *c,
82625163bd5SWebb Scales 					int reply_queue)
8273f5eac3aSStephen M. Cameron {
828254f796bSMatt Gates 	if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
8293f5eac3aSStephen M. Cameron 		c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
83025163bd5SWebb Scales 		if (unlikely(!h->msix_vector))
83125163bd5SWebb Scales 			return;
83225163bd5SWebb Scales 		if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
833254f796bSMatt Gates 			c->Header.ReplyQueue =
834804a5cb5SJohn Kacur 				raw_smp_processor_id() % h->nreply_queues;
83525163bd5SWebb Scales 		else
83625163bd5SWebb Scales 			c->Header.ReplyQueue = reply_queue % h->nreply_queues;
837254f796bSMatt Gates 	}
8383f5eac3aSStephen M. Cameron }
8393f5eac3aSStephen M. Cameron 
840c349775eSScott Teel static void set_ioaccel1_performant_mode(struct ctlr_info *h,
84125163bd5SWebb Scales 						struct CommandList *c,
84225163bd5SWebb Scales 						int reply_queue)
843c349775eSScott Teel {
844c349775eSScott Teel 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
845c349775eSScott Teel 
84625163bd5SWebb Scales 	/*
84725163bd5SWebb Scales 	 * Tell the controller to post the reply to the queue for this
848c349775eSScott Teel 	 * processor.  This seems to give the best I/O throughput.
849c349775eSScott Teel 	 */
85025163bd5SWebb Scales 	if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
851c349775eSScott Teel 		cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
85225163bd5SWebb Scales 	else
85325163bd5SWebb Scales 		cp->ReplyQueue = reply_queue % h->nreply_queues;
85425163bd5SWebb Scales 	/*
85525163bd5SWebb Scales 	 * Set the bits in the address sent down to include:
856c349775eSScott Teel 	 *  - performant mode bit (bit 0)
857c349775eSScott Teel 	 *  - pull count (bits 1-3)
858c349775eSScott Teel 	 *  - command type (bits 4-6)
859c349775eSScott Teel 	 */
860c349775eSScott Teel 	c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
861c349775eSScott Teel 					IOACCEL1_BUSADDR_CMDTYPE;
862c349775eSScott Teel }
863c349775eSScott Teel 
8648be986ccSStephen Cameron static void set_ioaccel2_tmf_performant_mode(struct ctlr_info *h,
8658be986ccSStephen Cameron 						struct CommandList *c,
8668be986ccSStephen Cameron 						int reply_queue)
8678be986ccSStephen Cameron {
8688be986ccSStephen Cameron 	struct hpsa_tmf_struct *cp = (struct hpsa_tmf_struct *)
8698be986ccSStephen Cameron 		&h->ioaccel2_cmd_pool[c->cmdindex];
8708be986ccSStephen Cameron 
8718be986ccSStephen Cameron 	/* Tell the controller to post the reply to the queue for this
8728be986ccSStephen Cameron 	 * processor.  This seems to give the best I/O throughput.
8738be986ccSStephen Cameron 	 */
8748be986ccSStephen Cameron 	if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
8758be986ccSStephen Cameron 		cp->reply_queue = smp_processor_id() % h->nreply_queues;
8768be986ccSStephen Cameron 	else
8778be986ccSStephen Cameron 		cp->reply_queue = reply_queue % h->nreply_queues;
8788be986ccSStephen Cameron 	/* Set the bits in the address sent down to include:
8798be986ccSStephen Cameron 	 *  - performant mode bit not used in ioaccel mode 2
8808be986ccSStephen Cameron 	 *  - pull count (bits 0-3)
8818be986ccSStephen Cameron 	 *  - command type isn't needed for ioaccel2
8828be986ccSStephen Cameron 	 */
8838be986ccSStephen Cameron 	c->busaddr |= h->ioaccel2_blockFetchTable[0];
8848be986ccSStephen Cameron }
8858be986ccSStephen Cameron 
886c349775eSScott Teel static void set_ioaccel2_performant_mode(struct ctlr_info *h,
88725163bd5SWebb Scales 						struct CommandList *c,
88825163bd5SWebb Scales 						int reply_queue)
889c349775eSScott Teel {
890c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
891c349775eSScott Teel 
89225163bd5SWebb Scales 	/*
89325163bd5SWebb Scales 	 * Tell the controller to post the reply to the queue for this
894c349775eSScott Teel 	 * processor.  This seems to give the best I/O throughput.
895c349775eSScott Teel 	 */
89625163bd5SWebb Scales 	if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
897c349775eSScott Teel 		cp->reply_queue = smp_processor_id() % h->nreply_queues;
89825163bd5SWebb Scales 	else
89925163bd5SWebb Scales 		cp->reply_queue = reply_queue % h->nreply_queues;
90025163bd5SWebb Scales 	/*
90125163bd5SWebb Scales 	 * Set the bits in the address sent down to include:
902c349775eSScott Teel 	 *  - performant mode bit not used in ioaccel mode 2
903c349775eSScott Teel 	 *  - pull count (bits 0-3)
904c349775eSScott Teel 	 *  - command type isn't needed for ioaccel2
905c349775eSScott Teel 	 */
906c349775eSScott Teel 	c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
907c349775eSScott Teel }
908c349775eSScott Teel 
909e85c5974SStephen M. Cameron static int is_firmware_flash_cmd(u8 *cdb)
910e85c5974SStephen M. Cameron {
911e85c5974SStephen M. Cameron 	return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
912e85c5974SStephen M. Cameron }
913e85c5974SStephen M. Cameron 
914e85c5974SStephen M. Cameron /*
915e85c5974SStephen M. Cameron  * During firmware flash, the heartbeat register may not update as frequently
916e85c5974SStephen M. Cameron  * as it should.  So we dial down lockup detection during firmware flash. and
917e85c5974SStephen M. Cameron  * dial it back up when firmware flash completes.
918e85c5974SStephen M. Cameron  */
919e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
920e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
921e85c5974SStephen M. Cameron static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
922e85c5974SStephen M. Cameron 		struct CommandList *c)
923e85c5974SStephen M. Cameron {
924e85c5974SStephen M. Cameron 	if (!is_firmware_flash_cmd(c->Request.CDB))
925e85c5974SStephen M. Cameron 		return;
926e85c5974SStephen M. Cameron 	atomic_inc(&h->firmware_flash_in_progress);
927e85c5974SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
928e85c5974SStephen M. Cameron }
929e85c5974SStephen M. Cameron 
930e85c5974SStephen M. Cameron static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
931e85c5974SStephen M. Cameron 		struct CommandList *c)
932e85c5974SStephen M. Cameron {
933e85c5974SStephen M. Cameron 	if (is_firmware_flash_cmd(c->Request.CDB) &&
934e85c5974SStephen M. Cameron 		atomic_dec_and_test(&h->firmware_flash_in_progress))
935e85c5974SStephen M. Cameron 		h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
936e85c5974SStephen M. Cameron }
937e85c5974SStephen M. Cameron 
93825163bd5SWebb Scales static void __enqueue_cmd_and_start_io(struct ctlr_info *h,
93925163bd5SWebb Scales 	struct CommandList *c, int reply_queue)
9403f5eac3aSStephen M. Cameron {
941c05e8866SStephen Cameron 	dial_down_lockup_detection_during_fw_flash(h, c);
942c05e8866SStephen Cameron 	atomic_inc(&h->commands_outstanding);
943c349775eSScott Teel 	switch (c->cmd_type) {
944c349775eSScott Teel 	case CMD_IOACCEL1:
94525163bd5SWebb Scales 		set_ioaccel1_performant_mode(h, c, reply_queue);
946c05e8866SStephen Cameron 		writel(c->busaddr, h->vaddr + SA5_REQUEST_PORT_OFFSET);
947c349775eSScott Teel 		break;
948c349775eSScott Teel 	case CMD_IOACCEL2:
94925163bd5SWebb Scales 		set_ioaccel2_performant_mode(h, c, reply_queue);
950c05e8866SStephen Cameron 		writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
951c349775eSScott Teel 		break;
9528be986ccSStephen Cameron 	case IOACCEL2_TMF:
9538be986ccSStephen Cameron 		set_ioaccel2_tmf_performant_mode(h, c, reply_queue);
9548be986ccSStephen Cameron 		writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
9558be986ccSStephen Cameron 		break;
956c349775eSScott Teel 	default:
95725163bd5SWebb Scales 		set_performant_mode(h, c, reply_queue);
958f2405db8SDon Brace 		h->access.submit_command(h, c);
9593f5eac3aSStephen M. Cameron 	}
960c05e8866SStephen Cameron }
9613f5eac3aSStephen M. Cameron 
96225163bd5SWebb Scales static void enqueue_cmd_and_start_io(struct ctlr_info *h,
96325163bd5SWebb Scales 					struct CommandList *c)
96425163bd5SWebb Scales {
96525163bd5SWebb Scales 	__enqueue_cmd_and_start_io(h, c, DEFAULT_REPLY_QUEUE);
96625163bd5SWebb Scales }
96725163bd5SWebb Scales 
9683f5eac3aSStephen M. Cameron static inline int is_hba_lunid(unsigned char scsi3addr[])
9693f5eac3aSStephen M. Cameron {
9703f5eac3aSStephen M. Cameron 	return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
9713f5eac3aSStephen M. Cameron }
9723f5eac3aSStephen M. Cameron 
9733f5eac3aSStephen M. Cameron static inline int is_scsi_rev_5(struct ctlr_info *h)
9743f5eac3aSStephen M. Cameron {
9753f5eac3aSStephen M. Cameron 	if (!h->hba_inquiry_data)
9763f5eac3aSStephen M. Cameron 		return 0;
9773f5eac3aSStephen M. Cameron 	if ((h->hba_inquiry_data[2] & 0x07) == 5)
9783f5eac3aSStephen M. Cameron 		return 1;
9793f5eac3aSStephen M. Cameron 	return 0;
9803f5eac3aSStephen M. Cameron }
9813f5eac3aSStephen M. Cameron 
982edd16368SStephen M. Cameron static int hpsa_find_target_lun(struct ctlr_info *h,
983edd16368SStephen M. Cameron 	unsigned char scsi3addr[], int bus, int *target, int *lun)
984edd16368SStephen M. Cameron {
985edd16368SStephen M. Cameron 	/* finds an unused bus, target, lun for a new physical device
986edd16368SStephen M. Cameron 	 * assumes h->devlock is held
987edd16368SStephen M. Cameron 	 */
988edd16368SStephen M. Cameron 	int i, found = 0;
989cfe5badcSScott Teel 	DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
990edd16368SStephen M. Cameron 
991263d9401SAkinobu Mita 	bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
992edd16368SStephen M. Cameron 
993edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
994edd16368SStephen M. Cameron 		if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
995263d9401SAkinobu Mita 			__set_bit(h->dev[i]->target, lun_taken);
996edd16368SStephen M. Cameron 	}
997edd16368SStephen M. Cameron 
998263d9401SAkinobu Mita 	i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
999263d9401SAkinobu Mita 	if (i < HPSA_MAX_DEVICES) {
1000edd16368SStephen M. Cameron 		/* *bus = 1; */
1001edd16368SStephen M. Cameron 		*target = i;
1002edd16368SStephen M. Cameron 		*lun = 0;
1003edd16368SStephen M. Cameron 		found = 1;
1004edd16368SStephen M. Cameron 	}
1005edd16368SStephen M. Cameron 	return !found;
1006edd16368SStephen M. Cameron }
1007edd16368SStephen M. Cameron 
10080d96ef5fSWebb Scales static inline void hpsa_show_dev_msg(const char *level, struct ctlr_info *h,
10090d96ef5fSWebb Scales 	struct hpsa_scsi_dev_t *dev, char *description)
10100d96ef5fSWebb Scales {
10110d96ef5fSWebb Scales 	dev_printk(level, &h->pdev->dev,
10120d96ef5fSWebb Scales 			"scsi %d:%d:%d:%d: %s %s %.8s %.16s RAID-%s SSDSmartPathCap%c En%c Exp=%d\n",
10130d96ef5fSWebb Scales 			h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
10140d96ef5fSWebb Scales 			description,
10150d96ef5fSWebb Scales 			scsi_device_type(dev->devtype),
10160d96ef5fSWebb Scales 			dev->vendor,
10170d96ef5fSWebb Scales 			dev->model,
10180d96ef5fSWebb Scales 			dev->raid_level > RAID_UNKNOWN ?
10190d96ef5fSWebb Scales 				"RAID-?" : raid_label[dev->raid_level],
10200d96ef5fSWebb Scales 			dev->offload_config ? '+' : '-',
10210d96ef5fSWebb Scales 			dev->offload_enabled ? '+' : '-',
10220d96ef5fSWebb Scales 			dev->expose_state);
10230d96ef5fSWebb Scales }
10240d96ef5fSWebb Scales 
1025edd16368SStephen M. Cameron /* Add an entry into h->dev[] array. */
1026edd16368SStephen M. Cameron static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
1027edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *device,
1028edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *added[], int *nadded)
1029edd16368SStephen M. Cameron {
1030edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
1031edd16368SStephen M. Cameron 	int n = h->ndevices;
1032edd16368SStephen M. Cameron 	int i;
1033edd16368SStephen M. Cameron 	unsigned char addr1[8], addr2[8];
1034edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1035edd16368SStephen M. Cameron 
1036cfe5badcSScott Teel 	if (n >= HPSA_MAX_DEVICES) {
1037edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "too many devices, some will be "
1038edd16368SStephen M. Cameron 			"inaccessible.\n");
1039edd16368SStephen M. Cameron 		return -1;
1040edd16368SStephen M. Cameron 	}
1041edd16368SStephen M. Cameron 
1042edd16368SStephen M. Cameron 	/* physical devices do not have lun or target assigned until now. */
1043edd16368SStephen M. Cameron 	if (device->lun != -1)
1044edd16368SStephen M. Cameron 		/* Logical device, lun is already assigned. */
1045edd16368SStephen M. Cameron 		goto lun_assigned;
1046edd16368SStephen M. Cameron 
1047edd16368SStephen M. Cameron 	/* If this device a non-zero lun of a multi-lun device
1048edd16368SStephen M. Cameron 	 * byte 4 of the 8-byte LUN addr will contain the logical
10492b08b3e9SDon Brace 	 * unit no, zero otherwise.
1050edd16368SStephen M. Cameron 	 */
1051edd16368SStephen M. Cameron 	if (device->scsi3addr[4] == 0) {
1052edd16368SStephen M. Cameron 		/* This is not a non-zero lun of a multi-lun device */
1053edd16368SStephen M. Cameron 		if (hpsa_find_target_lun(h, device->scsi3addr,
1054edd16368SStephen M. Cameron 			device->bus, &device->target, &device->lun) != 0)
1055edd16368SStephen M. Cameron 			return -1;
1056edd16368SStephen M. Cameron 		goto lun_assigned;
1057edd16368SStephen M. Cameron 	}
1058edd16368SStephen M. Cameron 
1059edd16368SStephen M. Cameron 	/* This is a non-zero lun of a multi-lun device.
1060edd16368SStephen M. Cameron 	 * Search through our list and find the device which
1061edd16368SStephen M. Cameron 	 * has the same 8 byte LUN address, excepting byte 4.
1062edd16368SStephen M. Cameron 	 * Assign the same bus and target for this new LUN.
1063edd16368SStephen M. Cameron 	 * Use the logical unit number from the firmware.
1064edd16368SStephen M. Cameron 	 */
1065edd16368SStephen M. Cameron 	memcpy(addr1, device->scsi3addr, 8);
1066edd16368SStephen M. Cameron 	addr1[4] = 0;
1067edd16368SStephen M. Cameron 	for (i = 0; i < n; i++) {
1068edd16368SStephen M. Cameron 		sd = h->dev[i];
1069edd16368SStephen M. Cameron 		memcpy(addr2, sd->scsi3addr, 8);
1070edd16368SStephen M. Cameron 		addr2[4] = 0;
1071edd16368SStephen M. Cameron 		/* differ only in byte 4? */
1072edd16368SStephen M. Cameron 		if (memcmp(addr1, addr2, 8) == 0) {
1073edd16368SStephen M. Cameron 			device->bus = sd->bus;
1074edd16368SStephen M. Cameron 			device->target = sd->target;
1075edd16368SStephen M. Cameron 			device->lun = device->scsi3addr[4];
1076edd16368SStephen M. Cameron 			break;
1077edd16368SStephen M. Cameron 		}
1078edd16368SStephen M. Cameron 	}
1079edd16368SStephen M. Cameron 	if (device->lun == -1) {
1080edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
1081edd16368SStephen M. Cameron 			" suspect firmware bug or unsupported hardware "
1082edd16368SStephen M. Cameron 			"configuration.\n");
1083edd16368SStephen M. Cameron 			return -1;
1084edd16368SStephen M. Cameron 	}
1085edd16368SStephen M. Cameron 
1086edd16368SStephen M. Cameron lun_assigned:
1087edd16368SStephen M. Cameron 
1088edd16368SStephen M. Cameron 	h->dev[n] = device;
1089edd16368SStephen M. Cameron 	h->ndevices++;
1090edd16368SStephen M. Cameron 	added[*nadded] = device;
1091edd16368SStephen M. Cameron 	(*nadded)++;
10920d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_INFO, h, device,
10930d96ef5fSWebb Scales 		device->expose_state & HPSA_SCSI_ADD ? "added" : "masked");
1094a473d86cSRobert Elliott 	device->offload_to_be_enabled = device->offload_enabled;
1095a473d86cSRobert Elliott 	device->offload_enabled = 0;
1096edd16368SStephen M. Cameron 	return 0;
1097edd16368SStephen M. Cameron }
1098edd16368SStephen M. Cameron 
1099bd9244f7SScott Teel /* Update an entry in h->dev[] array. */
1100bd9244f7SScott Teel static void hpsa_scsi_update_entry(struct ctlr_info *h, int hostno,
1101bd9244f7SScott Teel 	int entry, struct hpsa_scsi_dev_t *new_entry)
1102bd9244f7SScott Teel {
1103a473d86cSRobert Elliott 	int offload_enabled;
1104bd9244f7SScott Teel 	/* assumes h->devlock is held */
1105bd9244f7SScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
1106bd9244f7SScott Teel 
1107bd9244f7SScott Teel 	/* Raid level changed. */
1108bd9244f7SScott Teel 	h->dev[entry]->raid_level = new_entry->raid_level;
1109250fb125SStephen M. Cameron 
111003383736SDon Brace 	/* Raid offload parameters changed.  Careful about the ordering. */
111103383736SDon Brace 	if (new_entry->offload_config && new_entry->offload_enabled) {
111203383736SDon Brace 		/*
111303383736SDon Brace 		 * if drive is newly offload_enabled, we want to copy the
111403383736SDon Brace 		 * raid map data first.  If previously offload_enabled and
111503383736SDon Brace 		 * offload_config were set, raid map data had better be
111603383736SDon Brace 		 * the same as it was before.  if raid map data is changed
111703383736SDon Brace 		 * then it had better be the case that
111803383736SDon Brace 		 * h->dev[entry]->offload_enabled is currently 0.
111903383736SDon Brace 		 */
11209fb0de2dSStephen M. Cameron 		h->dev[entry]->raid_map = new_entry->raid_map;
112103383736SDon Brace 		h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
112203383736SDon Brace 	}
1123a3144e0bSJoe Handzik 	if (new_entry->hba_ioaccel_enabled) {
1124a3144e0bSJoe Handzik 		h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
1125a3144e0bSJoe Handzik 		wmb(); /* set ioaccel_handle *before* hba_ioaccel_enabled */
1126a3144e0bSJoe Handzik 	}
1127a3144e0bSJoe Handzik 	h->dev[entry]->hba_ioaccel_enabled = new_entry->hba_ioaccel_enabled;
112803383736SDon Brace 	h->dev[entry]->offload_config = new_entry->offload_config;
112903383736SDon Brace 	h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
113003383736SDon Brace 	h->dev[entry]->queue_depth = new_entry->queue_depth;
1131250fb125SStephen M. Cameron 
113241ce4c35SStephen Cameron 	/*
113341ce4c35SStephen Cameron 	 * We can turn off ioaccel offload now, but need to delay turning
113441ce4c35SStephen Cameron 	 * it on until we can update h->dev[entry]->phys_disk[], but we
113541ce4c35SStephen Cameron 	 * can't do that until all the devices are updated.
113641ce4c35SStephen Cameron 	 */
113741ce4c35SStephen Cameron 	h->dev[entry]->offload_to_be_enabled = new_entry->offload_enabled;
113841ce4c35SStephen Cameron 	if (!new_entry->offload_enabled)
113941ce4c35SStephen Cameron 		h->dev[entry]->offload_enabled = 0;
114041ce4c35SStephen Cameron 
1141a473d86cSRobert Elliott 	offload_enabled = h->dev[entry]->offload_enabled;
1142a473d86cSRobert Elliott 	h->dev[entry]->offload_enabled = h->dev[entry]->offload_to_be_enabled;
11430d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_INFO, h, h->dev[entry], "updated");
1144a473d86cSRobert Elliott 	h->dev[entry]->offload_enabled = offload_enabled;
1145bd9244f7SScott Teel }
1146bd9244f7SScott Teel 
11472a8ccf31SStephen M. Cameron /* Replace an entry from h->dev[] array. */
11482a8ccf31SStephen M. Cameron static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
11492a8ccf31SStephen M. Cameron 	int entry, struct hpsa_scsi_dev_t *new_entry,
11502a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *added[], int *nadded,
11512a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
11522a8ccf31SStephen M. Cameron {
11532a8ccf31SStephen M. Cameron 	/* assumes h->devlock is held */
1154cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
11552a8ccf31SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
11562a8ccf31SStephen M. Cameron 	(*nremoved)++;
115701350d05SStephen M. Cameron 
115801350d05SStephen M. Cameron 	/*
115901350d05SStephen M. Cameron 	 * New physical devices won't have target/lun assigned yet
116001350d05SStephen M. Cameron 	 * so we need to preserve the values in the slot we are replacing.
116101350d05SStephen M. Cameron 	 */
116201350d05SStephen M. Cameron 	if (new_entry->target == -1) {
116301350d05SStephen M. Cameron 		new_entry->target = h->dev[entry]->target;
116401350d05SStephen M. Cameron 		new_entry->lun = h->dev[entry]->lun;
116501350d05SStephen M. Cameron 	}
116601350d05SStephen M. Cameron 
11672a8ccf31SStephen M. Cameron 	h->dev[entry] = new_entry;
11682a8ccf31SStephen M. Cameron 	added[*nadded] = new_entry;
11692a8ccf31SStephen M. Cameron 	(*nadded)++;
11700d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_INFO, h, new_entry, "replaced");
1171a473d86cSRobert Elliott 	new_entry->offload_to_be_enabled = new_entry->offload_enabled;
1172a473d86cSRobert Elliott 	new_entry->offload_enabled = 0;
11732a8ccf31SStephen M. Cameron }
11742a8ccf31SStephen M. Cameron 
1175edd16368SStephen M. Cameron /* Remove an entry from h->dev[] array. */
1176edd16368SStephen M. Cameron static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
1177edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
1178edd16368SStephen M. Cameron {
1179edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
1180edd16368SStephen M. Cameron 	int i;
1181edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1182edd16368SStephen M. Cameron 
1183cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
1184edd16368SStephen M. Cameron 
1185edd16368SStephen M. Cameron 	sd = h->dev[entry];
1186edd16368SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
1187edd16368SStephen M. Cameron 	(*nremoved)++;
1188edd16368SStephen M. Cameron 
1189edd16368SStephen M. Cameron 	for (i = entry; i < h->ndevices-1; i++)
1190edd16368SStephen M. Cameron 		h->dev[i] = h->dev[i+1];
1191edd16368SStephen M. Cameron 	h->ndevices--;
11920d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_INFO, h, sd, "removed");
1193edd16368SStephen M. Cameron }
1194edd16368SStephen M. Cameron 
1195edd16368SStephen M. Cameron #define SCSI3ADDR_EQ(a, b) ( \
1196edd16368SStephen M. Cameron 	(a)[7] == (b)[7] && \
1197edd16368SStephen M. Cameron 	(a)[6] == (b)[6] && \
1198edd16368SStephen M. Cameron 	(a)[5] == (b)[5] && \
1199edd16368SStephen M. Cameron 	(a)[4] == (b)[4] && \
1200edd16368SStephen M. Cameron 	(a)[3] == (b)[3] && \
1201edd16368SStephen M. Cameron 	(a)[2] == (b)[2] && \
1202edd16368SStephen M. Cameron 	(a)[1] == (b)[1] && \
1203edd16368SStephen M. Cameron 	(a)[0] == (b)[0])
1204edd16368SStephen M. Cameron 
1205edd16368SStephen M. Cameron static void fixup_botched_add(struct ctlr_info *h,
1206edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *added)
1207edd16368SStephen M. Cameron {
1208edd16368SStephen M. Cameron 	/* called when scsi_add_device fails in order to re-adjust
1209edd16368SStephen M. Cameron 	 * h->dev[] to match the mid layer's view.
1210edd16368SStephen M. Cameron 	 */
1211edd16368SStephen M. Cameron 	unsigned long flags;
1212edd16368SStephen M. Cameron 	int i, j;
1213edd16368SStephen M. Cameron 
1214edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
1215edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1216edd16368SStephen M. Cameron 		if (h->dev[i] == added) {
1217edd16368SStephen M. Cameron 			for (j = i; j < h->ndevices-1; j++)
1218edd16368SStephen M. Cameron 				h->dev[j] = h->dev[j+1];
1219edd16368SStephen M. Cameron 			h->ndevices--;
1220edd16368SStephen M. Cameron 			break;
1221edd16368SStephen M. Cameron 		}
1222edd16368SStephen M. Cameron 	}
1223edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
1224edd16368SStephen M. Cameron 	kfree(added);
1225edd16368SStephen M. Cameron }
1226edd16368SStephen M. Cameron 
1227edd16368SStephen M. Cameron static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
1228edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev2)
1229edd16368SStephen M. Cameron {
1230edd16368SStephen M. Cameron 	/* we compare everything except lun and target as these
1231edd16368SStephen M. Cameron 	 * are not yet assigned.  Compare parts likely
1232edd16368SStephen M. Cameron 	 * to differ first
1233edd16368SStephen M. Cameron 	 */
1234edd16368SStephen M. Cameron 	if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
1235edd16368SStephen M. Cameron 		sizeof(dev1->scsi3addr)) != 0)
1236edd16368SStephen M. Cameron 		return 0;
1237edd16368SStephen M. Cameron 	if (memcmp(dev1->device_id, dev2->device_id,
1238edd16368SStephen M. Cameron 		sizeof(dev1->device_id)) != 0)
1239edd16368SStephen M. Cameron 		return 0;
1240edd16368SStephen M. Cameron 	if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
1241edd16368SStephen M. Cameron 		return 0;
1242edd16368SStephen M. Cameron 	if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
1243edd16368SStephen M. Cameron 		return 0;
1244edd16368SStephen M. Cameron 	if (dev1->devtype != dev2->devtype)
1245edd16368SStephen M. Cameron 		return 0;
1246edd16368SStephen M. Cameron 	if (dev1->bus != dev2->bus)
1247edd16368SStephen M. Cameron 		return 0;
1248edd16368SStephen M. Cameron 	return 1;
1249edd16368SStephen M. Cameron }
1250edd16368SStephen M. Cameron 
1251bd9244f7SScott Teel static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
1252bd9244f7SScott Teel 	struct hpsa_scsi_dev_t *dev2)
1253bd9244f7SScott Teel {
1254bd9244f7SScott Teel 	/* Device attributes that can change, but don't mean
1255bd9244f7SScott Teel 	 * that the device is a different device, nor that the OS
1256bd9244f7SScott Teel 	 * needs to be told anything about the change.
1257bd9244f7SScott Teel 	 */
1258bd9244f7SScott Teel 	if (dev1->raid_level != dev2->raid_level)
1259bd9244f7SScott Teel 		return 1;
1260250fb125SStephen M. Cameron 	if (dev1->offload_config != dev2->offload_config)
1261250fb125SStephen M. Cameron 		return 1;
1262250fb125SStephen M. Cameron 	if (dev1->offload_enabled != dev2->offload_enabled)
1263250fb125SStephen M. Cameron 		return 1;
126403383736SDon Brace 	if (dev1->queue_depth != dev2->queue_depth)
126503383736SDon Brace 		return 1;
1266bd9244f7SScott Teel 	return 0;
1267bd9244f7SScott Teel }
1268bd9244f7SScott Teel 
1269edd16368SStephen M. Cameron /* Find needle in haystack.  If exact match found, return DEVICE_SAME,
1270edd16368SStephen M. Cameron  * and return needle location in *index.  If scsi3addr matches, but not
1271edd16368SStephen M. Cameron  * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
1272bd9244f7SScott Teel  * location in *index.
1273bd9244f7SScott Teel  * In the case of a minor device attribute change, such as RAID level, just
1274bd9244f7SScott Teel  * return DEVICE_UPDATED, along with the updated device's location in index.
1275bd9244f7SScott Teel  * If needle not found, return DEVICE_NOT_FOUND.
1276edd16368SStephen M. Cameron  */
1277edd16368SStephen M. Cameron static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
1278edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *haystack[], int haystack_size,
1279edd16368SStephen M. Cameron 	int *index)
1280edd16368SStephen M. Cameron {
1281edd16368SStephen M. Cameron 	int i;
1282edd16368SStephen M. Cameron #define DEVICE_NOT_FOUND 0
1283edd16368SStephen M. Cameron #define DEVICE_CHANGED 1
1284edd16368SStephen M. Cameron #define DEVICE_SAME 2
1285bd9244f7SScott Teel #define DEVICE_UPDATED 3
1286edd16368SStephen M. Cameron 	for (i = 0; i < haystack_size; i++) {
128723231048SStephen M. Cameron 		if (haystack[i] == NULL) /* previously removed. */
128823231048SStephen M. Cameron 			continue;
1289edd16368SStephen M. Cameron 		if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
1290edd16368SStephen M. Cameron 			*index = i;
1291bd9244f7SScott Teel 			if (device_is_the_same(needle, haystack[i])) {
1292bd9244f7SScott Teel 				if (device_updated(needle, haystack[i]))
1293bd9244f7SScott Teel 					return DEVICE_UPDATED;
1294edd16368SStephen M. Cameron 				return DEVICE_SAME;
1295bd9244f7SScott Teel 			} else {
12969846590eSStephen M. Cameron 				/* Keep offline devices offline */
12979846590eSStephen M. Cameron 				if (needle->volume_offline)
12989846590eSStephen M. Cameron 					return DEVICE_NOT_FOUND;
1299edd16368SStephen M. Cameron 				return DEVICE_CHANGED;
1300edd16368SStephen M. Cameron 			}
1301edd16368SStephen M. Cameron 		}
1302bd9244f7SScott Teel 	}
1303edd16368SStephen M. Cameron 	*index = -1;
1304edd16368SStephen M. Cameron 	return DEVICE_NOT_FOUND;
1305edd16368SStephen M. Cameron }
1306edd16368SStephen M. Cameron 
13079846590eSStephen M. Cameron static void hpsa_monitor_offline_device(struct ctlr_info *h,
13089846590eSStephen M. Cameron 					unsigned char scsi3addr[])
13099846590eSStephen M. Cameron {
13109846590eSStephen M. Cameron 	struct offline_device_entry *device;
13119846590eSStephen M. Cameron 	unsigned long flags;
13129846590eSStephen M. Cameron 
13139846590eSStephen M. Cameron 	/* Check to see if device is already on the list */
13149846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
13159846590eSStephen M. Cameron 	list_for_each_entry(device, &h->offline_device_list, offline_list) {
13169846590eSStephen M. Cameron 		if (memcmp(device->scsi3addr, scsi3addr,
13179846590eSStephen M. Cameron 			sizeof(device->scsi3addr)) == 0) {
13189846590eSStephen M. Cameron 			spin_unlock_irqrestore(&h->offline_device_lock, flags);
13199846590eSStephen M. Cameron 			return;
13209846590eSStephen M. Cameron 		}
13219846590eSStephen M. Cameron 	}
13229846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
13239846590eSStephen M. Cameron 
13249846590eSStephen M. Cameron 	/* Device is not on the list, add it. */
13259846590eSStephen M. Cameron 	device = kmalloc(sizeof(*device), GFP_KERNEL);
13269846590eSStephen M. Cameron 	if (!device) {
13279846590eSStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in %s\n", __func__);
13289846590eSStephen M. Cameron 		return;
13299846590eSStephen M. Cameron 	}
13309846590eSStephen M. Cameron 	memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
13319846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
13329846590eSStephen M. Cameron 	list_add_tail(&device->offline_list, &h->offline_device_list);
13339846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
13349846590eSStephen M. Cameron }
13359846590eSStephen M. Cameron 
13369846590eSStephen M. Cameron /* Print a message explaining various offline volume states */
13379846590eSStephen M. Cameron static void hpsa_show_volume_status(struct ctlr_info *h,
13389846590eSStephen M. Cameron 	struct hpsa_scsi_dev_t *sd)
13399846590eSStephen M. Cameron {
13409846590eSStephen M. Cameron 	if (sd->volume_offline == HPSA_VPD_LV_STATUS_UNSUPPORTED)
13419846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13429846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume status is not available through vital product data pages.\n",
13439846590eSStephen M. Cameron 			h->scsi_host->host_no,
13449846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13459846590eSStephen M. Cameron 	switch (sd->volume_offline) {
13469846590eSStephen M. Cameron 	case HPSA_LV_OK:
13479846590eSStephen M. Cameron 		break;
13489846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ERASE:
13499846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13509846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing background erase process.\n",
13519846590eSStephen M. Cameron 			h->scsi_host->host_no,
13529846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13539846590eSStephen M. Cameron 		break;
13549846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_RPI:
13559846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13569846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing rapid parity initialization process.\n",
13579846590eSStephen M. Cameron 			h->scsi_host->host_no,
13589846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13599846590eSStephen M. Cameron 		break;
13609846590eSStephen M. Cameron 	case HPSA_LV_PENDING_RPI:
13619846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13629846590eSStephen M. Cameron 				"C%d:B%d:T%d:L%d Volume is queued for rapid parity initialization process.\n",
13639846590eSStephen M. Cameron 				h->scsi_host->host_no,
13649846590eSStephen M. Cameron 				sd->bus, sd->target, sd->lun);
13659846590eSStephen M. Cameron 		break;
13669846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_NO_KEY:
13679846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13689846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because key is not present.\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_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
13739846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13749846590eSStephen 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",
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_UNDERGOING_ENCRYPTION:
13799846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13809846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing encryption 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_UNDERGOING_ENCRYPTION_REKEYING:
13859846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13869846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing encryption re-keying process.\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_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
13919846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13929846590eSStephen 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",
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_PENDING_ENCRYPTION:
13979846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13989846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is pending migration to encrypted state, but process has not started.\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_PENDING_ENCRYPTION_REKEYING:
14039846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
14049846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is encrypted and is pending encryption rekeying.\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 	}
14099846590eSStephen M. Cameron }
14109846590eSStephen M. Cameron 
141103383736SDon Brace /*
141203383736SDon Brace  * Figure the list of physical drive pointers for a logical drive with
141303383736SDon Brace  * raid offload configured.
141403383736SDon Brace  */
141503383736SDon Brace static void hpsa_figure_phys_disk_ptrs(struct ctlr_info *h,
141603383736SDon Brace 				struct hpsa_scsi_dev_t *dev[], int ndevices,
141703383736SDon Brace 				struct hpsa_scsi_dev_t *logical_drive)
141803383736SDon Brace {
141903383736SDon Brace 	struct raid_map_data *map = &logical_drive->raid_map;
142003383736SDon Brace 	struct raid_map_disk_data *dd = &map->data[0];
142103383736SDon Brace 	int i, j;
142203383736SDon Brace 	int total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
142303383736SDon Brace 				le16_to_cpu(map->metadata_disks_per_row);
142403383736SDon Brace 	int nraid_map_entries = le16_to_cpu(map->row_cnt) *
142503383736SDon Brace 				le16_to_cpu(map->layout_map_count) *
142603383736SDon Brace 				total_disks_per_row;
142703383736SDon Brace 	int nphys_disk = le16_to_cpu(map->layout_map_count) *
142803383736SDon Brace 				total_disks_per_row;
142903383736SDon Brace 	int qdepth;
143003383736SDon Brace 
143103383736SDon Brace 	if (nraid_map_entries > RAID_MAP_MAX_ENTRIES)
143203383736SDon Brace 		nraid_map_entries = RAID_MAP_MAX_ENTRIES;
143303383736SDon Brace 
143403383736SDon Brace 	qdepth = 0;
143503383736SDon Brace 	for (i = 0; i < nraid_map_entries; i++) {
143603383736SDon Brace 		logical_drive->phys_disk[i] = NULL;
143703383736SDon Brace 		if (!logical_drive->offload_config)
143803383736SDon Brace 			continue;
143903383736SDon Brace 		for (j = 0; j < ndevices; j++) {
144003383736SDon Brace 			if (dev[j]->devtype != TYPE_DISK)
144103383736SDon Brace 				continue;
144203383736SDon Brace 			if (is_logical_dev_addr_mode(dev[j]->scsi3addr))
144303383736SDon Brace 				continue;
144403383736SDon Brace 			if (dev[j]->ioaccel_handle != dd[i].ioaccel_handle)
144503383736SDon Brace 				continue;
144603383736SDon Brace 
144703383736SDon Brace 			logical_drive->phys_disk[i] = dev[j];
144803383736SDon Brace 			if (i < nphys_disk)
144903383736SDon Brace 				qdepth = min(h->nr_cmds, qdepth +
145003383736SDon Brace 				    logical_drive->phys_disk[i]->queue_depth);
145103383736SDon Brace 			break;
145203383736SDon Brace 		}
145303383736SDon Brace 
145403383736SDon Brace 		/*
145503383736SDon Brace 		 * This can happen if a physical drive is removed and
145603383736SDon Brace 		 * the logical drive is degraded.  In that case, the RAID
145703383736SDon Brace 		 * map data will refer to a physical disk which isn't actually
145803383736SDon Brace 		 * present.  And in that case offload_enabled should already
145903383736SDon Brace 		 * be 0, but we'll turn it off here just in case
146003383736SDon Brace 		 */
146103383736SDon Brace 		if (!logical_drive->phys_disk[i]) {
146203383736SDon Brace 			logical_drive->offload_enabled = 0;
146341ce4c35SStephen Cameron 			logical_drive->offload_to_be_enabled = 0;
146441ce4c35SStephen Cameron 			logical_drive->queue_depth = 8;
146503383736SDon Brace 		}
146603383736SDon Brace 	}
146703383736SDon Brace 	if (nraid_map_entries)
146803383736SDon Brace 		/*
146903383736SDon Brace 		 * This is correct for reads, too high for full stripe writes,
147003383736SDon Brace 		 * way too high for partial stripe writes
147103383736SDon Brace 		 */
147203383736SDon Brace 		logical_drive->queue_depth = qdepth;
147303383736SDon Brace 	else
147403383736SDon Brace 		logical_drive->queue_depth = h->nr_cmds;
147503383736SDon Brace }
147603383736SDon Brace 
147703383736SDon Brace static void hpsa_update_log_drive_phys_drive_ptrs(struct ctlr_info *h,
147803383736SDon Brace 				struct hpsa_scsi_dev_t *dev[], int ndevices)
147903383736SDon Brace {
148003383736SDon Brace 	int i;
148103383736SDon Brace 
148203383736SDon Brace 	for (i = 0; i < ndevices; i++) {
148303383736SDon Brace 		if (dev[i]->devtype != TYPE_DISK)
148403383736SDon Brace 			continue;
148503383736SDon Brace 		if (!is_logical_dev_addr_mode(dev[i]->scsi3addr))
148603383736SDon Brace 			continue;
148741ce4c35SStephen Cameron 
148841ce4c35SStephen Cameron 		/*
148941ce4c35SStephen Cameron 		 * If offload is currently enabled, the RAID map and
149041ce4c35SStephen Cameron 		 * phys_disk[] assignment *better* not be changing
149141ce4c35SStephen Cameron 		 * and since it isn't changing, we do not need to
149241ce4c35SStephen Cameron 		 * update it.
149341ce4c35SStephen Cameron 		 */
149441ce4c35SStephen Cameron 		if (dev[i]->offload_enabled)
149541ce4c35SStephen Cameron 			continue;
149641ce4c35SStephen Cameron 
149703383736SDon Brace 		hpsa_figure_phys_disk_ptrs(h, dev, ndevices, dev[i]);
149803383736SDon Brace 	}
149903383736SDon Brace }
150003383736SDon Brace 
15014967bd3eSStephen M. Cameron static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
1502edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd[], int nsds)
1503edd16368SStephen M. Cameron {
1504edd16368SStephen M. Cameron 	/* sd contains scsi3 addresses and devtypes, and inquiry
1505edd16368SStephen M. Cameron 	 * data.  This function takes what's in sd to be the current
1506edd16368SStephen M. Cameron 	 * reality and updates h->dev[] to reflect that reality.
1507edd16368SStephen M. Cameron 	 */
1508edd16368SStephen M. Cameron 	int i, entry, device_change, changes = 0;
1509edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *csd;
1510edd16368SStephen M. Cameron 	unsigned long flags;
1511edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **added, **removed;
1512edd16368SStephen M. Cameron 	int nadded, nremoved;
1513edd16368SStephen M. Cameron 	struct Scsi_Host *sh = NULL;
1514edd16368SStephen M. Cameron 
1515cfe5badcSScott Teel 	added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
1516cfe5badcSScott Teel 	removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
1517edd16368SStephen M. Cameron 
1518edd16368SStephen M. Cameron 	if (!added || !removed) {
1519edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in "
1520edd16368SStephen M. Cameron 			"adjust_hpsa_scsi_table\n");
1521edd16368SStephen M. Cameron 		goto free_and_out;
1522edd16368SStephen M. Cameron 	}
1523edd16368SStephen M. Cameron 
1524edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1525edd16368SStephen M. Cameron 
1526edd16368SStephen M. Cameron 	/* find any devices in h->dev[] that are not in
1527edd16368SStephen M. Cameron 	 * sd[] and remove them from h->dev[], and for any
1528edd16368SStephen M. Cameron 	 * devices which have changed, remove the old device
1529edd16368SStephen M. Cameron 	 * info and add the new device info.
1530bd9244f7SScott Teel 	 * If minor device attributes change, just update
1531bd9244f7SScott Teel 	 * the existing device structure.
1532edd16368SStephen M. Cameron 	 */
1533edd16368SStephen M. Cameron 	i = 0;
1534edd16368SStephen M. Cameron 	nremoved = 0;
1535edd16368SStephen M. Cameron 	nadded = 0;
1536edd16368SStephen M. Cameron 	while (i < h->ndevices) {
1537edd16368SStephen M. Cameron 		csd = h->dev[i];
1538edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
1539edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
1540edd16368SStephen M. Cameron 			changes++;
1541edd16368SStephen M. Cameron 			hpsa_scsi_remove_entry(h, hostno, i,
1542edd16368SStephen M. Cameron 				removed, &nremoved);
1543edd16368SStephen M. Cameron 			continue; /* remove ^^^, hence i not incremented */
1544edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
1545edd16368SStephen M. Cameron 			changes++;
15462a8ccf31SStephen M. Cameron 			hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
15472a8ccf31SStephen M. Cameron 				added, &nadded, removed, &nremoved);
1548c7f172dcSStephen M. Cameron 			/* Set it to NULL to prevent it from being freed
1549c7f172dcSStephen M. Cameron 			 * at the bottom of hpsa_update_scsi_devices()
1550c7f172dcSStephen M. Cameron 			 */
1551c7f172dcSStephen M. Cameron 			sd[entry] = NULL;
1552bd9244f7SScott Teel 		} else if (device_change == DEVICE_UPDATED) {
1553bd9244f7SScott Teel 			hpsa_scsi_update_entry(h, hostno, i, sd[entry]);
1554edd16368SStephen M. Cameron 		}
1555edd16368SStephen M. Cameron 		i++;
1556edd16368SStephen M. Cameron 	}
1557edd16368SStephen M. Cameron 
1558edd16368SStephen M. Cameron 	/* Now, make sure every device listed in sd[] is also
1559edd16368SStephen M. Cameron 	 * listed in h->dev[], adding them if they aren't found
1560edd16368SStephen M. Cameron 	 */
1561edd16368SStephen M. Cameron 
1562edd16368SStephen M. Cameron 	for (i = 0; i < nsds; i++) {
1563edd16368SStephen M. Cameron 		if (!sd[i]) /* if already added above. */
1564edd16368SStephen M. Cameron 			continue;
15659846590eSStephen M. Cameron 
15669846590eSStephen M. Cameron 		/* Don't add devices which are NOT READY, FORMAT IN PROGRESS
15679846590eSStephen M. Cameron 		 * as the SCSI mid-layer does not handle such devices well.
15689846590eSStephen M. Cameron 		 * It relentlessly loops sending TUR at 3Hz, then READ(10)
15699846590eSStephen M. Cameron 		 * at 160Hz, and prevents the system from coming up.
15709846590eSStephen M. Cameron 		 */
15719846590eSStephen M. Cameron 		if (sd[i]->volume_offline) {
15729846590eSStephen M. Cameron 			hpsa_show_volume_status(h, sd[i]);
15730d96ef5fSWebb Scales 			hpsa_show_dev_msg(KERN_INFO, h, sd[i], "offline");
15749846590eSStephen M. Cameron 			continue;
15759846590eSStephen M. Cameron 		}
15769846590eSStephen M. Cameron 
1577edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1578edd16368SStephen M. Cameron 					h->ndevices, &entry);
1579edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
1580edd16368SStephen M. Cameron 			changes++;
1581edd16368SStephen M. Cameron 			if (hpsa_scsi_add_entry(h, hostno, sd[i],
1582edd16368SStephen M. Cameron 				added, &nadded) != 0)
1583edd16368SStephen M. Cameron 				break;
1584edd16368SStephen M. Cameron 			sd[i] = NULL; /* prevent from being freed later. */
1585edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
1586edd16368SStephen M. Cameron 			/* should never happen... */
1587edd16368SStephen M. Cameron 			changes++;
1588edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev,
1589edd16368SStephen M. Cameron 				"device unexpectedly changed.\n");
1590edd16368SStephen M. Cameron 			/* but if it does happen, we just ignore that device */
1591edd16368SStephen M. Cameron 		}
1592edd16368SStephen M. Cameron 	}
159341ce4c35SStephen Cameron 	hpsa_update_log_drive_phys_drive_ptrs(h, h->dev, h->ndevices);
159441ce4c35SStephen Cameron 
159541ce4c35SStephen Cameron 	/* Now that h->dev[]->phys_disk[] is coherent, we can enable
159641ce4c35SStephen Cameron 	 * any logical drives that need it enabled.
159741ce4c35SStephen Cameron 	 */
159841ce4c35SStephen Cameron 	for (i = 0; i < h->ndevices; i++)
159941ce4c35SStephen Cameron 		h->dev[i]->offload_enabled = h->dev[i]->offload_to_be_enabled;
160041ce4c35SStephen Cameron 
1601edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1602edd16368SStephen M. Cameron 
16039846590eSStephen M. Cameron 	/* Monitor devices which are in one of several NOT READY states to be
16049846590eSStephen M. Cameron 	 * brought online later. This must be done without holding h->devlock,
16059846590eSStephen M. Cameron 	 * so don't touch h->dev[]
16069846590eSStephen M. Cameron 	 */
16079846590eSStephen M. Cameron 	for (i = 0; i < nsds; i++) {
16089846590eSStephen M. Cameron 		if (!sd[i]) /* if already added above. */
16099846590eSStephen M. Cameron 			continue;
16109846590eSStephen M. Cameron 		if (sd[i]->volume_offline)
16119846590eSStephen M. Cameron 			hpsa_monitor_offline_device(h, sd[i]->scsi3addr);
16129846590eSStephen M. Cameron 	}
16139846590eSStephen M. Cameron 
1614edd16368SStephen M. Cameron 	/* Don't notify scsi mid layer of any changes the first time through
1615edd16368SStephen M. Cameron 	 * (or if there are no changes) scsi_scan_host will do it later the
1616edd16368SStephen M. Cameron 	 * first time through.
1617edd16368SStephen M. Cameron 	 */
1618edd16368SStephen M. Cameron 	if (hostno == -1 || !changes)
1619edd16368SStephen M. Cameron 		goto free_and_out;
1620edd16368SStephen M. Cameron 
1621edd16368SStephen M. Cameron 	sh = h->scsi_host;
1622edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any removed devices */
1623edd16368SStephen M. Cameron 	for (i = 0; i < nremoved; i++) {
162441ce4c35SStephen Cameron 		if (removed[i]->expose_state & HPSA_SCSI_ADD) {
1625edd16368SStephen M. Cameron 			struct scsi_device *sdev =
1626edd16368SStephen M. Cameron 				scsi_device_lookup(sh, removed[i]->bus,
1627edd16368SStephen M. Cameron 					removed[i]->target, removed[i]->lun);
1628edd16368SStephen M. Cameron 			if (sdev != NULL) {
1629edd16368SStephen M. Cameron 				scsi_remove_device(sdev);
1630edd16368SStephen M. Cameron 				scsi_device_put(sdev);
1631edd16368SStephen M. Cameron 			} else {
163241ce4c35SStephen Cameron 				/*
163341ce4c35SStephen Cameron 				 * We don't expect to get here.
1634edd16368SStephen M. Cameron 				 * future cmds to this device will get selection
1635edd16368SStephen M. Cameron 				 * timeout as if the device was gone.
1636edd16368SStephen M. Cameron 				 */
16370d96ef5fSWebb Scales 				hpsa_show_dev_msg(KERN_WARNING, h, removed[i],
16380d96ef5fSWebb Scales 					"didn't find device for removal.");
1639edd16368SStephen M. Cameron 			}
164041ce4c35SStephen Cameron 		}
1641edd16368SStephen M. Cameron 		kfree(removed[i]);
1642edd16368SStephen M. Cameron 		removed[i] = NULL;
1643edd16368SStephen M. Cameron 	}
1644edd16368SStephen M. Cameron 
1645edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any added devices */
1646edd16368SStephen M. Cameron 	for (i = 0; i < nadded; i++) {
164741ce4c35SStephen Cameron 		if (!(added[i]->expose_state & HPSA_SCSI_ADD))
164841ce4c35SStephen Cameron 			continue;
1649edd16368SStephen M. Cameron 		if (scsi_add_device(sh, added[i]->bus,
1650edd16368SStephen M. Cameron 			added[i]->target, added[i]->lun) == 0)
1651edd16368SStephen M. Cameron 			continue;
16520d96ef5fSWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, added[i],
16530d96ef5fSWebb Scales 					"addition failed, device not added.");
1654edd16368SStephen M. Cameron 		/* now we have to remove it from h->dev,
1655edd16368SStephen M. Cameron 		 * since it didn't get added to scsi mid layer
1656edd16368SStephen M. Cameron 		 */
1657edd16368SStephen M. Cameron 		fixup_botched_add(h, added[i]);
1658105a3dbcSRobert Elliott 		added[i] = NULL;
1659edd16368SStephen M. Cameron 	}
1660edd16368SStephen M. Cameron 
1661edd16368SStephen M. Cameron free_and_out:
1662edd16368SStephen M. Cameron 	kfree(added);
1663edd16368SStephen M. Cameron 	kfree(removed);
1664edd16368SStephen M. Cameron }
1665edd16368SStephen M. Cameron 
1666edd16368SStephen M. Cameron /*
16679e03aa2fSJoe Perches  * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
1668edd16368SStephen M. Cameron  * Assume's h->devlock is held.
1669edd16368SStephen M. Cameron  */
1670edd16368SStephen M. Cameron static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1671edd16368SStephen M. Cameron 	int bus, int target, int lun)
1672edd16368SStephen M. Cameron {
1673edd16368SStephen M. Cameron 	int i;
1674edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1675edd16368SStephen M. Cameron 
1676edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1677edd16368SStephen M. Cameron 		sd = h->dev[i];
1678edd16368SStephen M. Cameron 		if (sd->bus == bus && sd->target == target && sd->lun == lun)
1679edd16368SStephen M. Cameron 			return sd;
1680edd16368SStephen M. Cameron 	}
1681edd16368SStephen M. Cameron 	return NULL;
1682edd16368SStephen M. Cameron }
1683edd16368SStephen M. Cameron 
1684edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev)
1685edd16368SStephen M. Cameron {
1686edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1687edd16368SStephen M. Cameron 	unsigned long flags;
1688edd16368SStephen M. Cameron 	struct ctlr_info *h;
1689edd16368SStephen M. Cameron 
1690edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
1691edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1692edd16368SStephen M. Cameron 	sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1693edd16368SStephen M. Cameron 		sdev_id(sdev), sdev->lun);
169441ce4c35SStephen Cameron 	if (likely(sd)) {
169503383736SDon Brace 		atomic_set(&sd->ioaccel_cmds_out, 0);
169641ce4c35SStephen Cameron 		sdev->hostdata = (sd->expose_state & HPSA_SCSI_ADD) ? sd : NULL;
169741ce4c35SStephen Cameron 	} else
169841ce4c35SStephen Cameron 		sdev->hostdata = NULL;
1699edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1700edd16368SStephen M. Cameron 	return 0;
1701edd16368SStephen M. Cameron }
1702edd16368SStephen M. Cameron 
170341ce4c35SStephen Cameron /* configure scsi device based on internal per-device structure */
170441ce4c35SStephen Cameron static int hpsa_slave_configure(struct scsi_device *sdev)
170541ce4c35SStephen Cameron {
170641ce4c35SStephen Cameron 	struct hpsa_scsi_dev_t *sd;
170741ce4c35SStephen Cameron 	int queue_depth;
170841ce4c35SStephen Cameron 
170941ce4c35SStephen Cameron 	sd = sdev->hostdata;
171041ce4c35SStephen Cameron 	sdev->no_uld_attach = !sd || !(sd->expose_state & HPSA_ULD_ATTACH);
171141ce4c35SStephen Cameron 
171241ce4c35SStephen Cameron 	if (sd)
171341ce4c35SStephen Cameron 		queue_depth = sd->queue_depth != 0 ?
171441ce4c35SStephen Cameron 			sd->queue_depth : sdev->host->can_queue;
171541ce4c35SStephen Cameron 	else
171641ce4c35SStephen Cameron 		queue_depth = sdev->host->can_queue;
171741ce4c35SStephen Cameron 
171841ce4c35SStephen Cameron 	scsi_change_queue_depth(sdev, queue_depth);
171941ce4c35SStephen Cameron 
172041ce4c35SStephen Cameron 	return 0;
172141ce4c35SStephen Cameron }
172241ce4c35SStephen Cameron 
1723edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev)
1724edd16368SStephen M. Cameron {
1725bcc44255SStephen M. Cameron 	/* nothing to do. */
1726edd16368SStephen M. Cameron }
1727edd16368SStephen M. Cameron 
1728d9a729f3SWebb Scales static void hpsa_free_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
1729d9a729f3SWebb Scales {
1730d9a729f3SWebb Scales 	int i;
1731d9a729f3SWebb Scales 
1732d9a729f3SWebb Scales 	if (!h->ioaccel2_cmd_sg_list)
1733d9a729f3SWebb Scales 		return;
1734d9a729f3SWebb Scales 	for (i = 0; i < h->nr_cmds; i++) {
1735d9a729f3SWebb Scales 		kfree(h->ioaccel2_cmd_sg_list[i]);
1736d9a729f3SWebb Scales 		h->ioaccel2_cmd_sg_list[i] = NULL;
1737d9a729f3SWebb Scales 	}
1738d9a729f3SWebb Scales 	kfree(h->ioaccel2_cmd_sg_list);
1739d9a729f3SWebb Scales 	h->ioaccel2_cmd_sg_list = NULL;
1740d9a729f3SWebb Scales }
1741d9a729f3SWebb Scales 
1742d9a729f3SWebb Scales static int hpsa_allocate_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
1743d9a729f3SWebb Scales {
1744d9a729f3SWebb Scales 	int i;
1745d9a729f3SWebb Scales 
1746d9a729f3SWebb Scales 	if (h->chainsize <= 0)
1747d9a729f3SWebb Scales 		return 0;
1748d9a729f3SWebb Scales 
1749d9a729f3SWebb Scales 	h->ioaccel2_cmd_sg_list =
1750d9a729f3SWebb Scales 		kzalloc(sizeof(*h->ioaccel2_cmd_sg_list) * h->nr_cmds,
1751d9a729f3SWebb Scales 					GFP_KERNEL);
1752d9a729f3SWebb Scales 	if (!h->ioaccel2_cmd_sg_list)
1753d9a729f3SWebb Scales 		return -ENOMEM;
1754d9a729f3SWebb Scales 	for (i = 0; i < h->nr_cmds; i++) {
1755d9a729f3SWebb Scales 		h->ioaccel2_cmd_sg_list[i] =
1756d9a729f3SWebb Scales 			kmalloc(sizeof(*h->ioaccel2_cmd_sg_list[i]) *
1757d9a729f3SWebb Scales 					h->maxsgentries, GFP_KERNEL);
1758d9a729f3SWebb Scales 		if (!h->ioaccel2_cmd_sg_list[i])
1759d9a729f3SWebb Scales 			goto clean;
1760d9a729f3SWebb Scales 	}
1761d9a729f3SWebb Scales 	return 0;
1762d9a729f3SWebb Scales 
1763d9a729f3SWebb Scales clean:
1764d9a729f3SWebb Scales 	hpsa_free_ioaccel2_sg_chain_blocks(h);
1765d9a729f3SWebb Scales 	return -ENOMEM;
1766d9a729f3SWebb Scales }
1767d9a729f3SWebb Scales 
176833a2ffceSStephen M. Cameron static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
176933a2ffceSStephen M. Cameron {
177033a2ffceSStephen M. Cameron 	int i;
177133a2ffceSStephen M. Cameron 
177233a2ffceSStephen M. Cameron 	if (!h->cmd_sg_list)
177333a2ffceSStephen M. Cameron 		return;
177433a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
177533a2ffceSStephen M. Cameron 		kfree(h->cmd_sg_list[i]);
177633a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = NULL;
177733a2ffceSStephen M. Cameron 	}
177833a2ffceSStephen M. Cameron 	kfree(h->cmd_sg_list);
177933a2ffceSStephen M. Cameron 	h->cmd_sg_list = NULL;
178033a2ffceSStephen M. Cameron }
178133a2ffceSStephen M. Cameron 
1782105a3dbcSRobert Elliott static int hpsa_alloc_sg_chain_blocks(struct ctlr_info *h)
178333a2ffceSStephen M. Cameron {
178433a2ffceSStephen M. Cameron 	int i;
178533a2ffceSStephen M. Cameron 
178633a2ffceSStephen M. Cameron 	if (h->chainsize <= 0)
178733a2ffceSStephen M. Cameron 		return 0;
178833a2ffceSStephen M. Cameron 
178933a2ffceSStephen M. Cameron 	h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
179033a2ffceSStephen M. Cameron 				GFP_KERNEL);
17913d4e6af8SRobert Elliott 	if (!h->cmd_sg_list) {
17923d4e6af8SRobert Elliott 		dev_err(&h->pdev->dev, "Failed to allocate SG list\n");
179333a2ffceSStephen M. Cameron 		return -ENOMEM;
17943d4e6af8SRobert Elliott 	}
179533a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
179633a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
179733a2ffceSStephen M. Cameron 						h->chainsize, GFP_KERNEL);
17983d4e6af8SRobert Elliott 		if (!h->cmd_sg_list[i]) {
17993d4e6af8SRobert Elliott 			dev_err(&h->pdev->dev, "Failed to allocate cmd SG\n");
180033a2ffceSStephen M. Cameron 			goto clean;
180133a2ffceSStephen M. Cameron 		}
18023d4e6af8SRobert Elliott 	}
180333a2ffceSStephen M. Cameron 	return 0;
180433a2ffceSStephen M. Cameron 
180533a2ffceSStephen M. Cameron clean:
180633a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
180733a2ffceSStephen M. Cameron 	return -ENOMEM;
180833a2ffceSStephen M. Cameron }
180933a2ffceSStephen M. Cameron 
1810d9a729f3SWebb Scales static int hpsa_map_ioaccel2_sg_chain_block(struct ctlr_info *h,
1811d9a729f3SWebb Scales 	struct io_accel2_cmd *cp, struct CommandList *c)
1812d9a729f3SWebb Scales {
1813d9a729f3SWebb Scales 	struct ioaccel2_sg_element *chain_block;
1814d9a729f3SWebb Scales 	u64 temp64;
1815d9a729f3SWebb Scales 	u32 chain_size;
1816d9a729f3SWebb Scales 
1817d9a729f3SWebb Scales 	chain_block = h->ioaccel2_cmd_sg_list[c->cmdindex];
1818d9a729f3SWebb Scales 	chain_size = le32_to_cpu(cp->data_len);
1819d9a729f3SWebb Scales 	temp64 = pci_map_single(h->pdev, chain_block, chain_size,
1820d9a729f3SWebb Scales 				PCI_DMA_TODEVICE);
1821d9a729f3SWebb Scales 	if (dma_mapping_error(&h->pdev->dev, temp64)) {
1822d9a729f3SWebb Scales 		/* prevent subsequent unmapping */
1823d9a729f3SWebb Scales 		cp->sg->address = 0;
1824d9a729f3SWebb Scales 		return -1;
1825d9a729f3SWebb Scales 	}
1826d9a729f3SWebb Scales 	cp->sg->address = cpu_to_le64(temp64);
1827d9a729f3SWebb Scales 	return 0;
1828d9a729f3SWebb Scales }
1829d9a729f3SWebb Scales 
1830d9a729f3SWebb Scales static void hpsa_unmap_ioaccel2_sg_chain_block(struct ctlr_info *h,
1831d9a729f3SWebb Scales 	struct io_accel2_cmd *cp)
1832d9a729f3SWebb Scales {
1833d9a729f3SWebb Scales 	struct ioaccel2_sg_element *chain_sg;
1834d9a729f3SWebb Scales 	u64 temp64;
1835d9a729f3SWebb Scales 	u32 chain_size;
1836d9a729f3SWebb Scales 
1837d9a729f3SWebb Scales 	chain_sg = cp->sg;
1838d9a729f3SWebb Scales 	temp64 = le64_to_cpu(chain_sg->address);
1839d9a729f3SWebb Scales 	chain_size = le32_to_cpu(cp->data_len);
1840d9a729f3SWebb Scales 	pci_unmap_single(h->pdev, temp64, chain_size, PCI_DMA_TODEVICE);
1841d9a729f3SWebb Scales }
1842d9a729f3SWebb Scales 
1843e2bea6dfSStephen M. Cameron static int hpsa_map_sg_chain_block(struct ctlr_info *h,
184433a2ffceSStephen M. Cameron 	struct CommandList *c)
184533a2ffceSStephen M. Cameron {
184633a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg, *chain_block;
184733a2ffceSStephen M. Cameron 	u64 temp64;
184850a0decfSStephen M. Cameron 	u32 chain_len;
184933a2ffceSStephen M. Cameron 
185033a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
185133a2ffceSStephen M. Cameron 	chain_block = h->cmd_sg_list[c->cmdindex];
185250a0decfSStephen M. Cameron 	chain_sg->Ext = cpu_to_le32(HPSA_SG_CHAIN);
185350a0decfSStephen M. Cameron 	chain_len = sizeof(*chain_sg) *
18542b08b3e9SDon Brace 		(le16_to_cpu(c->Header.SGTotal) - h->max_cmd_sg_entries);
185550a0decfSStephen M. Cameron 	chain_sg->Len = cpu_to_le32(chain_len);
185650a0decfSStephen M. Cameron 	temp64 = pci_map_single(h->pdev, chain_block, chain_len,
185733a2ffceSStephen M. Cameron 				PCI_DMA_TODEVICE);
1858e2bea6dfSStephen M. Cameron 	if (dma_mapping_error(&h->pdev->dev, temp64)) {
1859e2bea6dfSStephen M. Cameron 		/* prevent subsequent unmapping */
186050a0decfSStephen M. Cameron 		chain_sg->Addr = cpu_to_le64(0);
1861e2bea6dfSStephen M. Cameron 		return -1;
1862e2bea6dfSStephen M. Cameron 	}
186350a0decfSStephen M. Cameron 	chain_sg->Addr = cpu_to_le64(temp64);
1864e2bea6dfSStephen M. Cameron 	return 0;
186533a2ffceSStephen M. Cameron }
186633a2ffceSStephen M. Cameron 
186733a2ffceSStephen M. Cameron static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
186833a2ffceSStephen M. Cameron 	struct CommandList *c)
186933a2ffceSStephen M. Cameron {
187033a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg;
187133a2ffceSStephen M. Cameron 
187250a0decfSStephen M. Cameron 	if (le16_to_cpu(c->Header.SGTotal) <= h->max_cmd_sg_entries)
187333a2ffceSStephen M. Cameron 		return;
187433a2ffceSStephen M. Cameron 
187533a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
187650a0decfSStephen M. Cameron 	pci_unmap_single(h->pdev, le64_to_cpu(chain_sg->Addr),
187750a0decfSStephen M. Cameron 			le32_to_cpu(chain_sg->Len), PCI_DMA_TODEVICE);
187833a2ffceSStephen M. Cameron }
187933a2ffceSStephen M. Cameron 
1880a09c1441SScott Teel 
1881a09c1441SScott Teel /* Decode the various types of errors on ioaccel2 path.
1882a09c1441SScott Teel  * Return 1 for any error that should generate a RAID path retry.
1883a09c1441SScott Teel  * Return 0 for errors that don't require a RAID path retry.
1884a09c1441SScott Teel  */
1885a09c1441SScott Teel static int handle_ioaccel_mode2_error(struct ctlr_info *h,
1886c349775eSScott Teel 					struct CommandList *c,
1887c349775eSScott Teel 					struct scsi_cmnd *cmd,
1888c349775eSScott Teel 					struct io_accel2_cmd *c2)
1889c349775eSScott Teel {
1890c349775eSScott Teel 	int data_len;
1891a09c1441SScott Teel 	int retry = 0;
1892c40820d5SJoe Handzik 	u32 ioaccel2_resid = 0;
1893c349775eSScott Teel 
1894c349775eSScott Teel 	switch (c2->error_data.serv_response) {
1895c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_COMPLETE:
1896c349775eSScott Teel 		switch (c2->error_data.status) {
1897c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
1898c349775eSScott Teel 			break;
1899c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
1900ee6b1889SStephen M. Cameron 			cmd->result |= SAM_STAT_CHECK_CONDITION;
1901c349775eSScott Teel 			if (c2->error_data.data_present !=
1902ee6b1889SStephen M. Cameron 					IOACCEL2_SENSE_DATA_PRESENT) {
1903ee6b1889SStephen M. Cameron 				memset(cmd->sense_buffer, 0,
1904ee6b1889SStephen M. Cameron 					SCSI_SENSE_BUFFERSIZE);
1905c349775eSScott Teel 				break;
1906ee6b1889SStephen M. Cameron 			}
1907c349775eSScott Teel 			/* copy the sense data */
1908c349775eSScott Teel 			data_len = c2->error_data.sense_data_len;
1909c349775eSScott Teel 			if (data_len > SCSI_SENSE_BUFFERSIZE)
1910c349775eSScott Teel 				data_len = SCSI_SENSE_BUFFERSIZE;
1911c349775eSScott Teel 			if (data_len > sizeof(c2->error_data.sense_data_buff))
1912c349775eSScott Teel 				data_len =
1913c349775eSScott Teel 					sizeof(c2->error_data.sense_data_buff);
1914c349775eSScott Teel 			memcpy(cmd->sense_buffer,
1915c349775eSScott Teel 				c2->error_data.sense_data_buff, data_len);
1916a09c1441SScott Teel 			retry = 1;
1917c349775eSScott Teel 			break;
1918c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
1919a09c1441SScott Teel 			retry = 1;
1920c349775eSScott Teel 			break;
1921c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
1922a09c1441SScott Teel 			retry = 1;
1923c349775eSScott Teel 			break;
1924c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
19254a8da22bSStephen Cameron 			retry = 1;
1926c349775eSScott Teel 			break;
1927c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
1928a09c1441SScott Teel 			retry = 1;
1929c349775eSScott Teel 			break;
1930c349775eSScott Teel 		default:
1931a09c1441SScott Teel 			retry = 1;
1932c349775eSScott Teel 			break;
1933c349775eSScott Teel 		}
1934c349775eSScott Teel 		break;
1935c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_FAILURE:
1936c40820d5SJoe Handzik 		switch (c2->error_data.status) {
1937c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_IO_ERROR:
1938c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_IO_ABORTED:
1939c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_OVERRUN:
1940c40820d5SJoe Handzik 			retry = 1;
1941c40820d5SJoe Handzik 			break;
1942c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_UNDERRUN:
1943c40820d5SJoe Handzik 			cmd->result = (DID_OK << 16);		/* host byte */
1944c40820d5SJoe Handzik 			cmd->result |= (COMMAND_COMPLETE << 8);	/* msg byte */
1945c40820d5SJoe Handzik 			ioaccel2_resid = get_unaligned_le32(
1946c40820d5SJoe Handzik 						&c2->error_data.resid_cnt[0]);
1947c40820d5SJoe Handzik 			scsi_set_resid(cmd, ioaccel2_resid);
1948c40820d5SJoe Handzik 			break;
1949c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_NO_PATH_TO_DEVICE:
1950c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_INVALID_DEVICE:
1951c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_IOACCEL_DISABLED:
1952c40820d5SJoe Handzik 			/* We will get an event from ctlr to trigger rescan */
1953c40820d5SJoe Handzik 			retry = 1;
1954c40820d5SJoe Handzik 			break;
1955c40820d5SJoe Handzik 		default:
1956c40820d5SJoe Handzik 			retry = 1;
1957c40820d5SJoe Handzik 		}
1958c349775eSScott Teel 		break;
1959c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
1960c349775eSScott Teel 		break;
1961c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
1962c349775eSScott Teel 		break;
1963c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
1964a09c1441SScott Teel 		retry = 1;
1965c349775eSScott Teel 		break;
1966c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
1967c349775eSScott Teel 		break;
1968c349775eSScott Teel 	default:
1969a09c1441SScott Teel 		retry = 1;
1970c349775eSScott Teel 		break;
1971c349775eSScott Teel 	}
1972a09c1441SScott Teel 
1973a09c1441SScott Teel 	return retry;	/* retry on raid path? */
1974c349775eSScott Teel }
1975c349775eSScott Teel 
1976*8a0ff92cSWebb Scales static void hpsa_cmd_free_and_done(struct ctlr_info *h,
1977*8a0ff92cSWebb Scales 		struct CommandList *c, struct scsi_cmnd *cmd)
1978*8a0ff92cSWebb Scales {
1979*8a0ff92cSWebb Scales 	cmd_free(h, c);
1980*8a0ff92cSWebb Scales 	cmd->scsi_done(cmd);
1981*8a0ff92cSWebb Scales }
1982*8a0ff92cSWebb Scales 
1983*8a0ff92cSWebb Scales static void hpsa_retry_cmd(struct ctlr_info *h, struct CommandList *c)
1984*8a0ff92cSWebb Scales {
1985*8a0ff92cSWebb Scales 	INIT_WORK(&c->work, hpsa_command_resubmit_worker);
1986*8a0ff92cSWebb Scales 	queue_work_on(raw_smp_processor_id(), h->resubmit_wq, &c->work);
1987*8a0ff92cSWebb Scales }
1988*8a0ff92cSWebb Scales 
1989c349775eSScott Teel static void process_ioaccel2_completion(struct ctlr_info *h,
1990c349775eSScott Teel 		struct CommandList *c, struct scsi_cmnd *cmd,
1991c349775eSScott Teel 		struct hpsa_scsi_dev_t *dev)
1992c349775eSScott Teel {
1993c349775eSScott Teel 	struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
1994c349775eSScott Teel 
1995c349775eSScott Teel 	/* check for good status */
1996c349775eSScott Teel 	if (likely(c2->error_data.serv_response == 0 &&
1997*8a0ff92cSWebb Scales 			c2->error_data.status == 0))
1998*8a0ff92cSWebb Scales 		return hpsa_cmd_free_and_done(h, c, cmd);
1999c349775eSScott Teel 
2000*8a0ff92cSWebb Scales 	/*
2001*8a0ff92cSWebb Scales 	 * Any RAID offload error results in retry which will use
2002c349775eSScott Teel 	 * the normal I/O path so the controller can handle whatever's
2003c349775eSScott Teel 	 * wrong.
2004c349775eSScott Teel 	 */
2005c349775eSScott Teel 	if (is_logical_dev_addr_mode(dev->scsi3addr) &&
2006c349775eSScott Teel 		c2->error_data.serv_response ==
2007c349775eSScott Teel 			IOACCEL2_SERV_RESPONSE_FAILURE) {
2008080ef1ccSDon Brace 		if (c2->error_data.status ==
2009080ef1ccSDon Brace 			IOACCEL2_STATUS_SR_IOACCEL_DISABLED)
2010c349775eSScott Teel 			dev->offload_enabled = 0;
2011*8a0ff92cSWebb Scales 
2012*8a0ff92cSWebb Scales 		return hpsa_retry_cmd(h, c);
2013080ef1ccSDon Brace 	}
2014080ef1ccSDon Brace 
2015080ef1ccSDon Brace 	if (handle_ioaccel_mode2_error(h, c, cmd, c2))
2016*8a0ff92cSWebb Scales 		return hpsa_retry_cmd(h, c);
2017080ef1ccSDon Brace 
2018*8a0ff92cSWebb Scales 	return hpsa_cmd_free_and_done(h, c, cmd);
2019c349775eSScott Teel }
2020c349775eSScott Teel 
20219437ac43SStephen Cameron /* Returns 0 on success, < 0 otherwise. */
20229437ac43SStephen Cameron static int hpsa_evaluate_tmf_status(struct ctlr_info *h,
20239437ac43SStephen Cameron 					struct CommandList *cp)
20249437ac43SStephen Cameron {
20259437ac43SStephen Cameron 	u8 tmf_status = cp->err_info->ScsiStatus;
20269437ac43SStephen Cameron 
20279437ac43SStephen Cameron 	switch (tmf_status) {
20289437ac43SStephen Cameron 	case CISS_TMF_COMPLETE:
20299437ac43SStephen Cameron 		/*
20309437ac43SStephen Cameron 		 * CISS_TMF_COMPLETE never happens, instead,
20319437ac43SStephen Cameron 		 * ei->CommandStatus == 0 for this case.
20329437ac43SStephen Cameron 		 */
20339437ac43SStephen Cameron 	case CISS_TMF_SUCCESS:
20349437ac43SStephen Cameron 		return 0;
20359437ac43SStephen Cameron 	case CISS_TMF_INVALID_FRAME:
20369437ac43SStephen Cameron 	case CISS_TMF_NOT_SUPPORTED:
20379437ac43SStephen Cameron 	case CISS_TMF_FAILED:
20389437ac43SStephen Cameron 	case CISS_TMF_WRONG_LUN:
20399437ac43SStephen Cameron 	case CISS_TMF_OVERLAPPED_TAG:
20409437ac43SStephen Cameron 		break;
20419437ac43SStephen Cameron 	default:
20429437ac43SStephen Cameron 		dev_warn(&h->pdev->dev, "Unknown TMF status: 0x%02x\n",
20439437ac43SStephen Cameron 				tmf_status);
20449437ac43SStephen Cameron 		break;
20459437ac43SStephen Cameron 	}
20469437ac43SStephen Cameron 	return -tmf_status;
20479437ac43SStephen Cameron }
20489437ac43SStephen Cameron 
20491fb011fbSStephen M. Cameron static void complete_scsi_command(struct CommandList *cp)
2050edd16368SStephen M. Cameron {
2051edd16368SStephen M. Cameron 	struct scsi_cmnd *cmd;
2052edd16368SStephen M. Cameron 	struct ctlr_info *h;
2053edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2054283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
2055d9a729f3SWebb Scales 	struct io_accel2_cmd *c2;
2056edd16368SStephen M. Cameron 
20579437ac43SStephen Cameron 	u8 sense_key;
20589437ac43SStephen Cameron 	u8 asc;      /* additional sense code */
20599437ac43SStephen Cameron 	u8 ascq;     /* additional sense code qualifier */
2060db111e18SStephen M. Cameron 	unsigned long sense_data_size;
2061edd16368SStephen M. Cameron 
2062edd16368SStephen M. Cameron 	ei = cp->err_info;
20637fa3030cSStephen Cameron 	cmd = cp->scsi_cmd;
2064edd16368SStephen M. Cameron 	h = cp->h;
2065283b4a9bSStephen M. Cameron 	dev = cmd->device->hostdata;
2066d9a729f3SWebb Scales 	c2 = &h->ioaccel2_cmd_pool[cp->cmdindex];
2067edd16368SStephen M. Cameron 
2068edd16368SStephen M. Cameron 	scsi_dma_unmap(cmd); /* undo the DMA mappings */
2069e1f7de0cSMatt Gates 	if ((cp->cmd_type == CMD_SCSI) &&
20702b08b3e9SDon Brace 		(le16_to_cpu(cp->Header.SGTotal) > h->max_cmd_sg_entries))
207133a2ffceSStephen M. Cameron 		hpsa_unmap_sg_chain_block(h, cp);
2072edd16368SStephen M. Cameron 
2073d9a729f3SWebb Scales 	if ((cp->cmd_type == CMD_IOACCEL2) &&
2074d9a729f3SWebb Scales 		(c2->sg[0].chain_indicator == IOACCEL2_CHAIN))
2075d9a729f3SWebb Scales 		hpsa_unmap_ioaccel2_sg_chain_block(h, c2);
2076d9a729f3SWebb Scales 
2077edd16368SStephen M. Cameron 	cmd->result = (DID_OK << 16); 		/* host byte */
2078edd16368SStephen M. Cameron 	cmd->result |= (COMMAND_COMPLETE << 8);	/* msg byte */
2079c349775eSScott Teel 
208003383736SDon Brace 	if (cp->cmd_type == CMD_IOACCEL2 || cp->cmd_type == CMD_IOACCEL1)
208103383736SDon Brace 		atomic_dec(&cp->phys_disk->ioaccel_cmds_out);
208203383736SDon Brace 
208325163bd5SWebb Scales 	/*
208425163bd5SWebb Scales 	 * We check for lockup status here as it may be set for
208525163bd5SWebb Scales 	 * CMD_SCSI, CMD_IOACCEL1 and CMD_IOACCEL2 commands by
208625163bd5SWebb Scales 	 * fail_all_oustanding_cmds()
208725163bd5SWebb Scales 	 */
208825163bd5SWebb Scales 	if (unlikely(ei->CommandStatus == CMD_CTLR_LOCKUP)) {
208925163bd5SWebb Scales 		/* DID_NO_CONNECT will prevent a retry */
209025163bd5SWebb Scales 		cmd->result = DID_NO_CONNECT << 16;
2091*8a0ff92cSWebb Scales 		return hpsa_cmd_free_and_done(h, cp, cmd);
209225163bd5SWebb Scales 	}
209325163bd5SWebb Scales 
2094c349775eSScott Teel 	if (cp->cmd_type == CMD_IOACCEL2)
2095c349775eSScott Teel 		return process_ioaccel2_completion(h, cp, cmd, dev);
2096c349775eSScott Teel 
20976aa4c361SRobert Elliott 	scsi_set_resid(cmd, ei->ResidualCnt);
2098*8a0ff92cSWebb Scales 	if (ei->CommandStatus == 0)
2099*8a0ff92cSWebb Scales 		return hpsa_cmd_free_and_done(h, cp, cmd);
21006aa4c361SRobert Elliott 
2101e1f7de0cSMatt Gates 	/* For I/O accelerator commands, copy over some fields to the normal
2102e1f7de0cSMatt Gates 	 * CISS header used below for error handling.
2103e1f7de0cSMatt Gates 	 */
2104e1f7de0cSMatt Gates 	if (cp->cmd_type == CMD_IOACCEL1) {
2105e1f7de0cSMatt Gates 		struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
21062b08b3e9SDon Brace 		cp->Header.SGList = scsi_sg_count(cmd);
21072b08b3e9SDon Brace 		cp->Header.SGTotal = cpu_to_le16(cp->Header.SGList);
21082b08b3e9SDon Brace 		cp->Request.CDBLen = le16_to_cpu(c->io_flags) &
21092b08b3e9SDon Brace 			IOACCEL1_IOFLAGS_CDBLEN_MASK;
211050a0decfSStephen M. Cameron 		cp->Header.tag = c->tag;
2111e1f7de0cSMatt Gates 		memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
2112e1f7de0cSMatt Gates 		memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
2113283b4a9bSStephen M. Cameron 
2114283b4a9bSStephen M. Cameron 		/* Any RAID offload error results in retry which will use
2115283b4a9bSStephen M. Cameron 		 * the normal I/O path so the controller can handle whatever's
2116283b4a9bSStephen M. Cameron 		 * wrong.
2117283b4a9bSStephen M. Cameron 		 */
2118283b4a9bSStephen M. Cameron 		if (is_logical_dev_addr_mode(dev->scsi3addr)) {
2119283b4a9bSStephen M. Cameron 			if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
2120283b4a9bSStephen M. Cameron 				dev->offload_enabled = 0;
2121*8a0ff92cSWebb Scales 			return hpsa_retry_cmd(h, cp);
2122283b4a9bSStephen M. Cameron 		}
2123e1f7de0cSMatt Gates 	}
2124e1f7de0cSMatt Gates 
2125edd16368SStephen M. Cameron 	/* an error has occurred */
2126edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
2127edd16368SStephen M. Cameron 
2128edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
21299437ac43SStephen Cameron 		cmd->result |= ei->ScsiStatus;
21309437ac43SStephen Cameron 		/* copy the sense data */
21319437ac43SStephen Cameron 		if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
21329437ac43SStephen Cameron 			sense_data_size = SCSI_SENSE_BUFFERSIZE;
21339437ac43SStephen Cameron 		else
21349437ac43SStephen Cameron 			sense_data_size = sizeof(ei->SenseInfo);
21359437ac43SStephen Cameron 		if (ei->SenseLen < sense_data_size)
21369437ac43SStephen Cameron 			sense_data_size = ei->SenseLen;
21379437ac43SStephen Cameron 		memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
21389437ac43SStephen Cameron 		if (ei->ScsiStatus)
21399437ac43SStephen Cameron 			decode_sense_data(ei->SenseInfo, sense_data_size,
21409437ac43SStephen Cameron 				&sense_key, &asc, &ascq);
2141edd16368SStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
21421d3b3609SMatt Gates 			if (sense_key == ABORTED_COMMAND) {
21432e311fbaSStephen M. Cameron 				cmd->result |= DID_SOFT_ERROR << 16;
21441d3b3609SMatt Gates 				break;
21451d3b3609SMatt Gates 			}
2146edd16368SStephen M. Cameron 			break;
2147edd16368SStephen M. Cameron 		}
2148edd16368SStephen M. Cameron 		/* Problem was not a check condition
2149edd16368SStephen M. Cameron 		 * Pass it up to the upper layers...
2150edd16368SStephen M. Cameron 		 */
2151edd16368SStephen M. Cameron 		if (ei->ScsiStatus) {
2152edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
2153edd16368SStephen M. Cameron 				"Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
2154edd16368SStephen M. Cameron 				"Returning result: 0x%x\n",
2155edd16368SStephen M. Cameron 				cp, ei->ScsiStatus,
2156edd16368SStephen M. Cameron 				sense_key, asc, ascq,
2157edd16368SStephen M. Cameron 				cmd->result);
2158edd16368SStephen M. Cameron 		} else {  /* scsi status is zero??? How??? */
2159edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
2160edd16368SStephen M. Cameron 				"Returning no connection.\n", cp),
2161edd16368SStephen M. Cameron 
2162edd16368SStephen M. Cameron 			/* Ordinarily, this case should never happen,
2163edd16368SStephen M. Cameron 			 * but there is a bug in some released firmware
2164edd16368SStephen M. Cameron 			 * revisions that allows it to happen if, for
2165edd16368SStephen M. Cameron 			 * example, a 4100 backplane loses power and
2166edd16368SStephen M. Cameron 			 * the tape drive is in it.  We assume that
2167edd16368SStephen M. Cameron 			 * it's a fatal error of some kind because we
2168edd16368SStephen M. Cameron 			 * can't show that it wasn't. We will make it
2169edd16368SStephen M. Cameron 			 * look like selection timeout since that is
2170edd16368SStephen M. Cameron 			 * the most common reason for this to occur,
2171edd16368SStephen M. Cameron 			 * and it's severe enough.
2172edd16368SStephen M. Cameron 			 */
2173edd16368SStephen M. Cameron 
2174edd16368SStephen M. Cameron 			cmd->result = DID_NO_CONNECT << 16;
2175edd16368SStephen M. Cameron 		}
2176edd16368SStephen M. Cameron 		break;
2177edd16368SStephen M. Cameron 
2178edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
2179edd16368SStephen M. Cameron 		break;
2180edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
2181f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev,
2182f42e81e1SStephen Cameron 			"CDB %16phN data overrun\n", cp->Request.CDB);
2183edd16368SStephen M. Cameron 		break;
2184edd16368SStephen M. Cameron 	case CMD_INVALID: {
2185edd16368SStephen M. Cameron 		/* print_bytes(cp, sizeof(*cp), 1, 0);
2186edd16368SStephen M. Cameron 		print_cmd(cp); */
2187edd16368SStephen M. Cameron 		/* We get CMD_INVALID if you address a non-existent device
2188edd16368SStephen M. Cameron 		 * instead of a selection timeout (no response).  You will
2189edd16368SStephen M. Cameron 		 * see this if you yank out a drive, then try to access it.
2190edd16368SStephen M. Cameron 		 * This is kind of a shame because it means that any other
2191edd16368SStephen M. Cameron 		 * CMD_INVALID (e.g. driver bug) will get interpreted as a
2192edd16368SStephen M. Cameron 		 * missing target. */
2193edd16368SStephen M. Cameron 		cmd->result = DID_NO_CONNECT << 16;
2194edd16368SStephen M. Cameron 	}
2195edd16368SStephen M. Cameron 		break;
2196edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
2197256d0eaaSStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2198f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : protocol error\n",
2199f42e81e1SStephen Cameron 				cp->Request.CDB);
2200edd16368SStephen M. Cameron 		break;
2201edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
2202edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2203f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : hardware error\n",
2204f42e81e1SStephen Cameron 			cp->Request.CDB);
2205edd16368SStephen M. Cameron 		break;
2206edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
2207edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2208f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : connection lost\n",
2209f42e81e1SStephen Cameron 			cp->Request.CDB);
2210edd16368SStephen M. Cameron 		break;
2211edd16368SStephen M. Cameron 	case CMD_ABORTED:
2212edd16368SStephen M. Cameron 		cmd->result = DID_ABORT << 16;
2213f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN was aborted with status 0x%x\n",
2214f42e81e1SStephen Cameron 				cp->Request.CDB, ei->ScsiStatus);
2215edd16368SStephen M. Cameron 		break;
2216edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
2217edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2218f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : abort failed\n",
2219f42e81e1SStephen Cameron 			cp->Request.CDB);
2220edd16368SStephen M. Cameron 		break;
2221edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
2222f6e76055SStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
2223f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : unsolicited abort\n",
2224f42e81e1SStephen Cameron 			cp->Request.CDB);
2225edd16368SStephen M. Cameron 		break;
2226edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
2227edd16368SStephen M. Cameron 		cmd->result = DID_TIME_OUT << 16;
2228f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN timed out\n",
2229f42e81e1SStephen Cameron 			cp->Request.CDB);
2230edd16368SStephen M. Cameron 		break;
22311d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
22321d5e2ed0SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
22331d5e2ed0SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Command unabortable\n");
22341d5e2ed0SStephen M. Cameron 		break;
22359437ac43SStephen Cameron 	case CMD_TMF_STATUS:
22369437ac43SStephen Cameron 		if (hpsa_evaluate_tmf_status(h, cp)) /* TMF failed? */
22379437ac43SStephen Cameron 			cmd->result = DID_ERROR << 16;
22389437ac43SStephen Cameron 		break;
2239283b4a9bSStephen M. Cameron 	case CMD_IOACCEL_DISABLED:
2240283b4a9bSStephen M. Cameron 		/* This only handles the direct pass-through case since RAID
2241283b4a9bSStephen M. Cameron 		 * offload is handled above.  Just attempt a retry.
2242283b4a9bSStephen M. Cameron 		 */
2243283b4a9bSStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16;
2244283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev,
2245283b4a9bSStephen M. Cameron 				"cp %p had HP SSD Smart Path error\n", cp);
2246283b4a9bSStephen M. Cameron 		break;
2247edd16368SStephen M. Cameron 	default:
2248edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2249edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
2250edd16368SStephen M. Cameron 				cp, ei->CommandStatus);
2251edd16368SStephen M. Cameron 	}
2252*8a0ff92cSWebb Scales 
2253*8a0ff92cSWebb Scales 	return hpsa_cmd_free_and_done(h, cp, cmd);
2254edd16368SStephen M. Cameron }
2255edd16368SStephen M. Cameron 
2256edd16368SStephen M. Cameron static void hpsa_pci_unmap(struct pci_dev *pdev,
2257edd16368SStephen M. Cameron 	struct CommandList *c, int sg_used, int data_direction)
2258edd16368SStephen M. Cameron {
2259edd16368SStephen M. Cameron 	int i;
2260edd16368SStephen M. Cameron 
226150a0decfSStephen M. Cameron 	for (i = 0; i < sg_used; i++)
226250a0decfSStephen M. Cameron 		pci_unmap_single(pdev, (dma_addr_t) le64_to_cpu(c->SG[i].Addr),
226350a0decfSStephen M. Cameron 				le32_to_cpu(c->SG[i].Len),
2264edd16368SStephen M. Cameron 				data_direction);
2265edd16368SStephen M. Cameron }
2266edd16368SStephen M. Cameron 
2267a2dac136SStephen M. Cameron static int hpsa_map_one(struct pci_dev *pdev,
2268edd16368SStephen M. Cameron 		struct CommandList *cp,
2269edd16368SStephen M. Cameron 		unsigned char *buf,
2270edd16368SStephen M. Cameron 		size_t buflen,
2271edd16368SStephen M. Cameron 		int data_direction)
2272edd16368SStephen M. Cameron {
227301a02ffcSStephen M. Cameron 	u64 addr64;
2274edd16368SStephen M. Cameron 
2275edd16368SStephen M. Cameron 	if (buflen == 0 || data_direction == PCI_DMA_NONE) {
2276edd16368SStephen M. Cameron 		cp->Header.SGList = 0;
227750a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(0);
2278a2dac136SStephen M. Cameron 		return 0;
2279edd16368SStephen M. Cameron 	}
2280edd16368SStephen M. Cameron 
228150a0decfSStephen M. Cameron 	addr64 = pci_map_single(pdev, buf, buflen, data_direction);
2282eceaae18SShuah Khan 	if (dma_mapping_error(&pdev->dev, addr64)) {
2283a2dac136SStephen M. Cameron 		/* Prevent subsequent unmap of something never mapped */
2284eceaae18SShuah Khan 		cp->Header.SGList = 0;
228550a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(0);
2286a2dac136SStephen M. Cameron 		return -1;
2287eceaae18SShuah Khan 	}
228850a0decfSStephen M. Cameron 	cp->SG[0].Addr = cpu_to_le64(addr64);
228950a0decfSStephen M. Cameron 	cp->SG[0].Len = cpu_to_le32(buflen);
229050a0decfSStephen M. Cameron 	cp->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* we are not chaining */
229150a0decfSStephen M. Cameron 	cp->Header.SGList = 1;   /* no. SGs contig in this cmd */
229250a0decfSStephen M. Cameron 	cp->Header.SGTotal = cpu_to_le16(1); /* total sgs in cmd list */
2293a2dac136SStephen M. Cameron 	return 0;
2294edd16368SStephen M. Cameron }
2295edd16368SStephen M. Cameron 
229625163bd5SWebb Scales #define NO_TIMEOUT ((unsigned long) -1)
229725163bd5SWebb Scales #define DEFAULT_TIMEOUT 30000 /* milliseconds */
229825163bd5SWebb Scales static int hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
229925163bd5SWebb Scales 	struct CommandList *c, int reply_queue, unsigned long timeout_msecs)
2300edd16368SStephen M. Cameron {
2301edd16368SStephen M. Cameron 	DECLARE_COMPLETION_ONSTACK(wait);
2302edd16368SStephen M. Cameron 
2303edd16368SStephen M. Cameron 	c->waiting = &wait;
230425163bd5SWebb Scales 	__enqueue_cmd_and_start_io(h, c, reply_queue);
230525163bd5SWebb Scales 	if (timeout_msecs == NO_TIMEOUT) {
230625163bd5SWebb Scales 		/* TODO: get rid of this no-timeout thing */
230725163bd5SWebb Scales 		wait_for_completion_io(&wait);
230825163bd5SWebb Scales 		return IO_OK;
230925163bd5SWebb Scales 	}
231025163bd5SWebb Scales 	if (!wait_for_completion_io_timeout(&wait,
231125163bd5SWebb Scales 					msecs_to_jiffies(timeout_msecs))) {
231225163bd5SWebb Scales 		dev_warn(&h->pdev->dev, "Command timed out.\n");
231325163bd5SWebb Scales 		return -ETIMEDOUT;
231425163bd5SWebb Scales 	}
231525163bd5SWebb Scales 	return IO_OK;
231625163bd5SWebb Scales }
231725163bd5SWebb Scales 
231825163bd5SWebb Scales static int hpsa_scsi_do_simple_cmd(struct ctlr_info *h, struct CommandList *c,
231925163bd5SWebb Scales 				   int reply_queue, unsigned long timeout_msecs)
232025163bd5SWebb Scales {
232125163bd5SWebb Scales 	if (unlikely(lockup_detected(h))) {
232225163bd5SWebb Scales 		c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
232325163bd5SWebb Scales 		return IO_OK;
232425163bd5SWebb Scales 	}
232525163bd5SWebb Scales 	return hpsa_scsi_do_simple_cmd_core(h, c, reply_queue, timeout_msecs);
2326edd16368SStephen M. Cameron }
2327edd16368SStephen M. Cameron 
2328094963daSStephen M. Cameron static u32 lockup_detected(struct ctlr_info *h)
2329094963daSStephen M. Cameron {
2330094963daSStephen M. Cameron 	int cpu;
2331094963daSStephen M. Cameron 	u32 rc, *lockup_detected;
2332094963daSStephen M. Cameron 
2333094963daSStephen M. Cameron 	cpu = get_cpu();
2334094963daSStephen M. Cameron 	lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
2335094963daSStephen M. Cameron 	rc = *lockup_detected;
2336094963daSStephen M. Cameron 	put_cpu();
2337094963daSStephen M. Cameron 	return rc;
2338094963daSStephen M. Cameron }
2339094963daSStephen M. Cameron 
23409c2fc160SStephen M. Cameron #define MAX_DRIVER_CMD_RETRIES 25
234125163bd5SWebb Scales static int hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
234225163bd5SWebb Scales 	struct CommandList *c, int data_direction, unsigned long timeout_msecs)
2343edd16368SStephen M. Cameron {
23449c2fc160SStephen M. Cameron 	int backoff_time = 10, retry_count = 0;
234525163bd5SWebb Scales 	int rc;
2346edd16368SStephen M. Cameron 
2347edd16368SStephen M. Cameron 	do {
23487630abd0SJoe Perches 		memset(c->err_info, 0, sizeof(*c->err_info));
234925163bd5SWebb Scales 		rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
235025163bd5SWebb Scales 						  timeout_msecs);
235125163bd5SWebb Scales 		if (rc)
235225163bd5SWebb Scales 			break;
2353edd16368SStephen M. Cameron 		retry_count++;
23549c2fc160SStephen M. Cameron 		if (retry_count > 3) {
23559c2fc160SStephen M. Cameron 			msleep(backoff_time);
23569c2fc160SStephen M. Cameron 			if (backoff_time < 1000)
23579c2fc160SStephen M. Cameron 				backoff_time *= 2;
23589c2fc160SStephen M. Cameron 		}
2359852af20aSMatt Bondurant 	} while ((check_for_unit_attention(h, c) ||
23609c2fc160SStephen M. Cameron 			check_for_busy(h, c)) &&
23619c2fc160SStephen M. Cameron 			retry_count <= MAX_DRIVER_CMD_RETRIES);
2362edd16368SStephen M. Cameron 	hpsa_pci_unmap(h->pdev, c, 1, data_direction);
236325163bd5SWebb Scales 	if (retry_count > MAX_DRIVER_CMD_RETRIES)
236425163bd5SWebb Scales 		rc = -EIO;
236525163bd5SWebb Scales 	return rc;
2366edd16368SStephen M. Cameron }
2367edd16368SStephen M. Cameron 
2368d1e8beacSStephen M. Cameron static void hpsa_print_cmd(struct ctlr_info *h, char *txt,
2369d1e8beacSStephen M. Cameron 				struct CommandList *c)
2370edd16368SStephen M. Cameron {
2371d1e8beacSStephen M. Cameron 	const u8 *cdb = c->Request.CDB;
2372d1e8beacSStephen M. Cameron 	const u8 *lun = c->Header.LUN.LunAddrBytes;
2373edd16368SStephen M. Cameron 
2374d1e8beacSStephen M. Cameron 	dev_warn(&h->pdev->dev, "%s: LUN:%02x%02x%02x%02x%02x%02x%02x%02x"
2375d1e8beacSStephen M. Cameron 	" CDB:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
2376d1e8beacSStephen M. Cameron 		txt, lun[0], lun[1], lun[2], lun[3],
2377d1e8beacSStephen M. Cameron 		lun[4], lun[5], lun[6], lun[7],
2378d1e8beacSStephen M. Cameron 		cdb[0], cdb[1], cdb[2], cdb[3],
2379d1e8beacSStephen M. Cameron 		cdb[4], cdb[5], cdb[6], cdb[7],
2380d1e8beacSStephen M. Cameron 		cdb[8], cdb[9], cdb[10], cdb[11],
2381d1e8beacSStephen M. Cameron 		cdb[12], cdb[13], cdb[14], cdb[15]);
2382d1e8beacSStephen M. Cameron }
2383d1e8beacSStephen M. Cameron 
2384d1e8beacSStephen M. Cameron static void hpsa_scsi_interpret_error(struct ctlr_info *h,
2385d1e8beacSStephen M. Cameron 			struct CommandList *cp)
2386d1e8beacSStephen M. Cameron {
2387d1e8beacSStephen M. Cameron 	const struct ErrorInfo *ei = cp->err_info;
2388d1e8beacSStephen M. Cameron 	struct device *d = &cp->h->pdev->dev;
23899437ac43SStephen Cameron 	u8 sense_key, asc, ascq;
23909437ac43SStephen Cameron 	int sense_len;
2391d1e8beacSStephen M. Cameron 
2392edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
2393edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
23949437ac43SStephen Cameron 		if (ei->SenseLen > sizeof(ei->SenseInfo))
23959437ac43SStephen Cameron 			sense_len = sizeof(ei->SenseInfo);
23969437ac43SStephen Cameron 		else
23979437ac43SStephen Cameron 			sense_len = ei->SenseLen;
23989437ac43SStephen Cameron 		decode_sense_data(ei->SenseInfo, sense_len,
23999437ac43SStephen Cameron 					&sense_key, &asc, &ascq);
2400d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "SCSI status", cp);
2401d1e8beacSStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION)
24029437ac43SStephen Cameron 			dev_warn(d, "SCSI Status = 02, Sense key = 0x%02x, ASC = 0x%02x, ASCQ = 0x%02x\n",
24039437ac43SStephen Cameron 				sense_key, asc, ascq);
2404d1e8beacSStephen M. Cameron 		else
24059437ac43SStephen Cameron 			dev_warn(d, "SCSI Status = 0x%02x\n", ei->ScsiStatus);
2406edd16368SStephen M. Cameron 		if (ei->ScsiStatus == 0)
2407edd16368SStephen M. Cameron 			dev_warn(d, "SCSI status is abnormally zero.  "
2408edd16368SStephen M. Cameron 			"(probably indicates selection timeout "
2409edd16368SStephen M. Cameron 			"reported incorrectly due to a known "
2410edd16368SStephen M. Cameron 			"firmware bug, circa July, 2001.)\n");
2411edd16368SStephen M. Cameron 		break;
2412edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
2413edd16368SStephen M. Cameron 		break;
2414edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
2415d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "overrun condition", cp);
2416edd16368SStephen M. Cameron 		break;
2417edd16368SStephen M. Cameron 	case CMD_INVALID: {
2418edd16368SStephen M. Cameron 		/* controller unfortunately reports SCSI passthru's
2419edd16368SStephen M. Cameron 		 * to non-existent targets as invalid commands.
2420edd16368SStephen M. Cameron 		 */
2421d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "invalid command", cp);
2422d1e8beacSStephen M. Cameron 		dev_warn(d, "probably means device no longer present\n");
2423edd16368SStephen M. Cameron 		}
2424edd16368SStephen M. Cameron 		break;
2425edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
2426d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "protocol error", cp);
2427edd16368SStephen M. Cameron 		break;
2428edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
2429d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "hardware error", cp);
2430edd16368SStephen M. Cameron 		break;
2431edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
2432d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "connection lost", cp);
2433edd16368SStephen M. Cameron 		break;
2434edd16368SStephen M. Cameron 	case CMD_ABORTED:
2435d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "aborted", cp);
2436edd16368SStephen M. Cameron 		break;
2437edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
2438d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "abort failed", cp);
2439edd16368SStephen M. Cameron 		break;
2440edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
2441d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unsolicited abort", cp);
2442edd16368SStephen M. Cameron 		break;
2443edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
2444d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "timed out", cp);
2445edd16368SStephen M. Cameron 		break;
24461d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
2447d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unabortable", cp);
24481d5e2ed0SStephen M. Cameron 		break;
244925163bd5SWebb Scales 	case CMD_CTLR_LOCKUP:
245025163bd5SWebb Scales 		hpsa_print_cmd(h, "controller lockup detected", cp);
245125163bd5SWebb Scales 		break;
2452edd16368SStephen M. Cameron 	default:
2453d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unknown status", cp);
2454d1e8beacSStephen M. Cameron 		dev_warn(d, "Unknown command status %x\n",
2455edd16368SStephen M. Cameron 				ei->CommandStatus);
2456edd16368SStephen M. Cameron 	}
2457edd16368SStephen M. Cameron }
2458edd16368SStephen M. Cameron 
2459edd16368SStephen M. Cameron static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
2460b7bb24ebSStephen M. Cameron 			u16 page, unsigned char *buf,
2461edd16368SStephen M. Cameron 			unsigned char bufsize)
2462edd16368SStephen M. Cameron {
2463edd16368SStephen M. Cameron 	int rc = IO_OK;
2464edd16368SStephen M. Cameron 	struct CommandList *c;
2465edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2466edd16368SStephen M. Cameron 
246745fcb86eSStephen Cameron 	c = cmd_alloc(h);
2468edd16368SStephen M. Cameron 
2469a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
2470a2dac136SStephen M. Cameron 			page, scsi3addr, TYPE_CMD)) {
2471a2dac136SStephen M. Cameron 		rc = -1;
2472a2dac136SStephen M. Cameron 		goto out;
2473a2dac136SStephen M. Cameron 	}
247425163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
247525163bd5SWebb Scales 					PCI_DMA_FROMDEVICE, NO_TIMEOUT);
247625163bd5SWebb Scales 	if (rc)
247725163bd5SWebb Scales 		goto out;
2478edd16368SStephen M. Cameron 	ei = c->err_info;
2479edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2480d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2481edd16368SStephen M. Cameron 		rc = -1;
2482edd16368SStephen M. Cameron 	}
2483a2dac136SStephen M. Cameron out:
248445fcb86eSStephen Cameron 	cmd_free(h, c);
2485edd16368SStephen M. Cameron 	return rc;
2486edd16368SStephen M. Cameron }
2487edd16368SStephen M. Cameron 
2488316b221aSStephen M. Cameron static int hpsa_bmic_ctrl_mode_sense(struct ctlr_info *h,
2489316b221aSStephen M. Cameron 		unsigned char *scsi3addr, unsigned char page,
2490316b221aSStephen M. Cameron 		struct bmic_controller_parameters *buf, size_t bufsize)
2491316b221aSStephen M. Cameron {
2492316b221aSStephen M. Cameron 	int rc = IO_OK;
2493316b221aSStephen M. Cameron 	struct CommandList *c;
2494316b221aSStephen M. Cameron 	struct ErrorInfo *ei;
2495316b221aSStephen M. Cameron 
249645fcb86eSStephen Cameron 	c = cmd_alloc(h);
2497316b221aSStephen M. Cameron 	if (fill_cmd(c, BMIC_SENSE_CONTROLLER_PARAMETERS, h, buf, bufsize,
2498316b221aSStephen M. Cameron 			page, scsi3addr, TYPE_CMD)) {
2499316b221aSStephen M. Cameron 		rc = -1;
2500316b221aSStephen M. Cameron 		goto out;
2501316b221aSStephen M. Cameron 	}
250225163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
250325163bd5SWebb Scales 			PCI_DMA_FROMDEVICE, NO_TIMEOUT);
250425163bd5SWebb Scales 	if (rc)
250525163bd5SWebb Scales 		goto out;
2506316b221aSStephen M. Cameron 	ei = c->err_info;
2507316b221aSStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2508316b221aSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2509316b221aSStephen M. Cameron 		rc = -1;
2510316b221aSStephen M. Cameron 	}
2511316b221aSStephen M. Cameron out:
251245fcb86eSStephen Cameron 	cmd_free(h, c);
2513316b221aSStephen M. Cameron 	return rc;
2514316b221aSStephen M. Cameron }
2515316b221aSStephen M. Cameron 
2516bf711ac6SScott Teel static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
251725163bd5SWebb Scales 	u8 reset_type, int reply_queue)
2518edd16368SStephen M. Cameron {
2519edd16368SStephen M. Cameron 	int rc = IO_OK;
2520edd16368SStephen M. Cameron 	struct CommandList *c;
2521edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2522edd16368SStephen M. Cameron 
252345fcb86eSStephen Cameron 	c = cmd_alloc(h);
2524edd16368SStephen M. Cameron 
2525edd16368SStephen M. Cameron 
2526a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map. */
2527bf711ac6SScott Teel 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
2528bf711ac6SScott Teel 			scsi3addr, TYPE_MSG);
2529bf711ac6SScott Teel 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to LUN reset */
253025163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
253125163bd5SWebb Scales 	if (rc) {
253225163bd5SWebb Scales 		dev_warn(&h->pdev->dev, "Failed to send reset command\n");
253325163bd5SWebb Scales 		goto out;
253425163bd5SWebb Scales 	}
2535edd16368SStephen M. Cameron 	/* no unmap needed here because no data xfer. */
2536edd16368SStephen M. Cameron 
2537edd16368SStephen M. Cameron 	ei = c->err_info;
2538edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0) {
2539d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2540edd16368SStephen M. Cameron 		rc = -1;
2541edd16368SStephen M. Cameron 	}
254225163bd5SWebb Scales out:
254345fcb86eSStephen Cameron 	cmd_free(h, c);
2544edd16368SStephen M. Cameron 	return rc;
2545edd16368SStephen M. Cameron }
2546edd16368SStephen M. Cameron 
2547edd16368SStephen M. Cameron static void hpsa_get_raid_level(struct ctlr_info *h,
2548edd16368SStephen M. Cameron 	unsigned char *scsi3addr, unsigned char *raid_level)
2549edd16368SStephen M. Cameron {
2550edd16368SStephen M. Cameron 	int rc;
2551edd16368SStephen M. Cameron 	unsigned char *buf;
2552edd16368SStephen M. Cameron 
2553edd16368SStephen M. Cameron 	*raid_level = RAID_UNKNOWN;
2554edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2555edd16368SStephen M. Cameron 	if (!buf)
2556edd16368SStephen M. Cameron 		return;
2557b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0xC1, buf, 64);
2558edd16368SStephen M. Cameron 	if (rc == 0)
2559edd16368SStephen M. Cameron 		*raid_level = buf[8];
2560edd16368SStephen M. Cameron 	if (*raid_level > RAID_UNKNOWN)
2561edd16368SStephen M. Cameron 		*raid_level = RAID_UNKNOWN;
2562edd16368SStephen M. Cameron 	kfree(buf);
2563edd16368SStephen M. Cameron 	return;
2564edd16368SStephen M. Cameron }
2565edd16368SStephen M. Cameron 
2566283b4a9bSStephen M. Cameron #define HPSA_MAP_DEBUG
2567283b4a9bSStephen M. Cameron #ifdef HPSA_MAP_DEBUG
2568283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
2569283b4a9bSStephen M. Cameron 				struct raid_map_data *map_buff)
2570283b4a9bSStephen M. Cameron {
2571283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map_buff->data[0];
2572283b4a9bSStephen M. Cameron 	int map, row, col;
2573283b4a9bSStephen M. Cameron 	u16 map_cnt, row_cnt, disks_per_row;
2574283b4a9bSStephen M. Cameron 
2575283b4a9bSStephen M. Cameron 	if (rc != 0)
2576283b4a9bSStephen M. Cameron 		return;
2577283b4a9bSStephen M. Cameron 
25782ba8bfc8SStephen M. Cameron 	/* Show details only if debugging has been activated. */
25792ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug < 2)
25802ba8bfc8SStephen M. Cameron 		return;
25812ba8bfc8SStephen M. Cameron 
2582283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "structure_size = %u\n",
2583283b4a9bSStephen M. Cameron 				le32_to_cpu(map_buff->structure_size));
2584283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
2585283b4a9bSStephen M. Cameron 			le32_to_cpu(map_buff->volume_blk_size));
2586283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
2587283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->volume_blk_cnt));
2588283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
2589283b4a9bSStephen M. Cameron 			map_buff->phys_blk_shift);
2590283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
2591283b4a9bSStephen M. Cameron 			map_buff->parity_rotation_shift);
2592283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "strip_size = %u\n",
2593283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->strip_size));
2594283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
2595283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_starting_blk));
2596283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
2597283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_blk_cnt));
2598283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
2599283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->data_disks_per_row));
2600283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
2601283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->metadata_disks_per_row));
2602283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "row_cnt = %u\n",
2603283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->row_cnt));
2604283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "layout_map_count = %u\n",
2605283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->layout_map_count));
26062b08b3e9SDon Brace 	dev_info(&h->pdev->dev, "flags = 0x%x\n",
2607dd0e19f3SScott Teel 			le16_to_cpu(map_buff->flags));
26082b08b3e9SDon Brace 	dev_info(&h->pdev->dev, "encrypytion = %s\n",
26092b08b3e9SDon Brace 			le16_to_cpu(map_buff->flags) &
26102b08b3e9SDon Brace 			RAID_MAP_FLAG_ENCRYPT_ON ?  "ON" : "OFF");
2611dd0e19f3SScott Teel 	dev_info(&h->pdev->dev, "dekindex = %u\n",
2612dd0e19f3SScott Teel 			le16_to_cpu(map_buff->dekindex));
2613283b4a9bSStephen M. Cameron 	map_cnt = le16_to_cpu(map_buff->layout_map_count);
2614283b4a9bSStephen M. Cameron 	for (map = 0; map < map_cnt; map++) {
2615283b4a9bSStephen M. Cameron 		dev_info(&h->pdev->dev, "Map%u:\n", map);
2616283b4a9bSStephen M. Cameron 		row_cnt = le16_to_cpu(map_buff->row_cnt);
2617283b4a9bSStephen M. Cameron 		for (row = 0; row < row_cnt; row++) {
2618283b4a9bSStephen M. Cameron 			dev_info(&h->pdev->dev, "  Row%u:\n", row);
2619283b4a9bSStephen M. Cameron 			disks_per_row =
2620283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->data_disks_per_row);
2621283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2622283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2623283b4a9bSStephen M. Cameron 					"    D%02u: h=0x%04x xor=%u,%u\n",
2624283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2625283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2626283b4a9bSStephen M. Cameron 			disks_per_row =
2627283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->metadata_disks_per_row);
2628283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2629283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2630283b4a9bSStephen M. Cameron 					"    M%02u: h=0x%04x xor=%u,%u\n",
2631283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2632283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2633283b4a9bSStephen M. Cameron 		}
2634283b4a9bSStephen M. Cameron 	}
2635283b4a9bSStephen M. Cameron }
2636283b4a9bSStephen M. Cameron #else
2637283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
2638283b4a9bSStephen M. Cameron 			__attribute__((unused)) int rc,
2639283b4a9bSStephen M. Cameron 			__attribute__((unused)) struct raid_map_data *map_buff)
2640283b4a9bSStephen M. Cameron {
2641283b4a9bSStephen M. Cameron }
2642283b4a9bSStephen M. Cameron #endif
2643283b4a9bSStephen M. Cameron 
2644283b4a9bSStephen M. Cameron static int hpsa_get_raid_map(struct ctlr_info *h,
2645283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2646283b4a9bSStephen M. Cameron {
2647283b4a9bSStephen M. Cameron 	int rc = 0;
2648283b4a9bSStephen M. Cameron 	struct CommandList *c;
2649283b4a9bSStephen M. Cameron 	struct ErrorInfo *ei;
2650283b4a9bSStephen M. Cameron 
265145fcb86eSStephen Cameron 	c = cmd_alloc(h);
2652bf43caf3SRobert Elliott 
2653283b4a9bSStephen M. Cameron 	if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
2654283b4a9bSStephen M. Cameron 			sizeof(this_device->raid_map), 0,
2655283b4a9bSStephen M. Cameron 			scsi3addr, TYPE_CMD)) {
26562dd02d74SRobert Elliott 		dev_warn(&h->pdev->dev, "hpsa_get_raid_map fill_cmd failed\n");
26572dd02d74SRobert Elliott 		cmd_free(h, c);
26582dd02d74SRobert Elliott 		return -1;
2659283b4a9bSStephen M. Cameron 	}
266025163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
266125163bd5SWebb Scales 					PCI_DMA_FROMDEVICE, NO_TIMEOUT);
266225163bd5SWebb Scales 	if (rc)
266325163bd5SWebb Scales 		goto out;
2664283b4a9bSStephen M. Cameron 	ei = c->err_info;
2665283b4a9bSStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2666d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
266725163bd5SWebb Scales 		rc = -1;
266825163bd5SWebb Scales 		goto out;
2669283b4a9bSStephen M. Cameron 	}
267045fcb86eSStephen Cameron 	cmd_free(h, c);
2671283b4a9bSStephen M. Cameron 
2672283b4a9bSStephen M. Cameron 	/* @todo in the future, dynamically allocate RAID map memory */
2673283b4a9bSStephen M. Cameron 	if (le32_to_cpu(this_device->raid_map.structure_size) >
2674283b4a9bSStephen M. Cameron 				sizeof(this_device->raid_map)) {
2675283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
2676283b4a9bSStephen M. Cameron 		rc = -1;
2677283b4a9bSStephen M. Cameron 	}
2678283b4a9bSStephen M. Cameron 	hpsa_debug_map_buff(h, rc, &this_device->raid_map);
2679283b4a9bSStephen M. Cameron 	return rc;
268025163bd5SWebb Scales out:
268125163bd5SWebb Scales 	cmd_free(h, c);
268225163bd5SWebb Scales 	return rc;
2683283b4a9bSStephen M. Cameron }
2684283b4a9bSStephen M. Cameron 
268503383736SDon Brace static int hpsa_bmic_id_physical_device(struct ctlr_info *h,
268603383736SDon Brace 		unsigned char scsi3addr[], u16 bmic_device_index,
268703383736SDon Brace 		struct bmic_identify_physical_device *buf, size_t bufsize)
268803383736SDon Brace {
268903383736SDon Brace 	int rc = IO_OK;
269003383736SDon Brace 	struct CommandList *c;
269103383736SDon Brace 	struct ErrorInfo *ei;
269203383736SDon Brace 
269303383736SDon Brace 	c = cmd_alloc(h);
269403383736SDon Brace 	rc = fill_cmd(c, BMIC_IDENTIFY_PHYSICAL_DEVICE, h, buf, bufsize,
269503383736SDon Brace 		0, RAID_CTLR_LUNID, TYPE_CMD);
269603383736SDon Brace 	if (rc)
269703383736SDon Brace 		goto out;
269803383736SDon Brace 
269903383736SDon Brace 	c->Request.CDB[2] = bmic_device_index & 0xff;
270003383736SDon Brace 	c->Request.CDB[9] = (bmic_device_index >> 8) & 0xff;
270103383736SDon Brace 
270225163bd5SWebb Scales 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE,
270325163bd5SWebb Scales 						NO_TIMEOUT);
270403383736SDon Brace 	ei = c->err_info;
270503383736SDon Brace 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
270603383736SDon Brace 		hpsa_scsi_interpret_error(h, c);
270703383736SDon Brace 		rc = -1;
270803383736SDon Brace 	}
270903383736SDon Brace out:
271003383736SDon Brace 	cmd_free(h, c);
271103383736SDon Brace 	return rc;
271203383736SDon Brace }
271303383736SDon Brace 
27141b70150aSStephen M. Cameron static int hpsa_vpd_page_supported(struct ctlr_info *h,
27151b70150aSStephen M. Cameron 	unsigned char scsi3addr[], u8 page)
27161b70150aSStephen M. Cameron {
27171b70150aSStephen M. Cameron 	int rc;
27181b70150aSStephen M. Cameron 	int i;
27191b70150aSStephen M. Cameron 	int pages;
27201b70150aSStephen M. Cameron 	unsigned char *buf, bufsize;
27211b70150aSStephen M. Cameron 
27221b70150aSStephen M. Cameron 	buf = kzalloc(256, GFP_KERNEL);
27231b70150aSStephen M. Cameron 	if (!buf)
27241b70150aSStephen M. Cameron 		return 0;
27251b70150aSStephen M. Cameron 
27261b70150aSStephen M. Cameron 	/* Get the size of the page list first */
27271b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
27281b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
27291b70150aSStephen M. Cameron 				buf, HPSA_VPD_HEADER_SZ);
27301b70150aSStephen M. Cameron 	if (rc != 0)
27311b70150aSStephen M. Cameron 		goto exit_unsupported;
27321b70150aSStephen M. Cameron 	pages = buf[3];
27331b70150aSStephen M. Cameron 	if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
27341b70150aSStephen M. Cameron 		bufsize = pages + HPSA_VPD_HEADER_SZ;
27351b70150aSStephen M. Cameron 	else
27361b70150aSStephen M. Cameron 		bufsize = 255;
27371b70150aSStephen M. Cameron 
27381b70150aSStephen M. Cameron 	/* Get the whole VPD page list */
27391b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
27401b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
27411b70150aSStephen M. Cameron 				buf, bufsize);
27421b70150aSStephen M. Cameron 	if (rc != 0)
27431b70150aSStephen M. Cameron 		goto exit_unsupported;
27441b70150aSStephen M. Cameron 
27451b70150aSStephen M. Cameron 	pages = buf[3];
27461b70150aSStephen M. Cameron 	for (i = 1; i <= pages; i++)
27471b70150aSStephen M. Cameron 		if (buf[3 + i] == page)
27481b70150aSStephen M. Cameron 			goto exit_supported;
27491b70150aSStephen M. Cameron exit_unsupported:
27501b70150aSStephen M. Cameron 	kfree(buf);
27511b70150aSStephen M. Cameron 	return 0;
27521b70150aSStephen M. Cameron exit_supported:
27531b70150aSStephen M. Cameron 	kfree(buf);
27541b70150aSStephen M. Cameron 	return 1;
27551b70150aSStephen M. Cameron }
27561b70150aSStephen M. Cameron 
2757283b4a9bSStephen M. Cameron static void hpsa_get_ioaccel_status(struct ctlr_info *h,
2758283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2759283b4a9bSStephen M. Cameron {
2760283b4a9bSStephen M. Cameron 	int rc;
2761283b4a9bSStephen M. Cameron 	unsigned char *buf;
2762283b4a9bSStephen M. Cameron 	u8 ioaccel_status;
2763283b4a9bSStephen M. Cameron 
2764283b4a9bSStephen M. Cameron 	this_device->offload_config = 0;
2765283b4a9bSStephen M. Cameron 	this_device->offload_enabled = 0;
276641ce4c35SStephen Cameron 	this_device->offload_to_be_enabled = 0;
2767283b4a9bSStephen M. Cameron 
2768283b4a9bSStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2769283b4a9bSStephen M. Cameron 	if (!buf)
2770283b4a9bSStephen M. Cameron 		return;
27711b70150aSStephen M. Cameron 	if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
27721b70150aSStephen M. Cameron 		goto out;
2773283b4a9bSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
2774b7bb24ebSStephen M. Cameron 			VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
2775283b4a9bSStephen M. Cameron 	if (rc != 0)
2776283b4a9bSStephen M. Cameron 		goto out;
2777283b4a9bSStephen M. Cameron 
2778283b4a9bSStephen M. Cameron #define IOACCEL_STATUS_BYTE 4
2779283b4a9bSStephen M. Cameron #define OFFLOAD_CONFIGURED_BIT 0x01
2780283b4a9bSStephen M. Cameron #define OFFLOAD_ENABLED_BIT 0x02
2781283b4a9bSStephen M. Cameron 	ioaccel_status = buf[IOACCEL_STATUS_BYTE];
2782283b4a9bSStephen M. Cameron 	this_device->offload_config =
2783283b4a9bSStephen M. Cameron 		!!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
2784283b4a9bSStephen M. Cameron 	if (this_device->offload_config) {
2785283b4a9bSStephen M. Cameron 		this_device->offload_enabled =
2786283b4a9bSStephen M. Cameron 			!!(ioaccel_status & OFFLOAD_ENABLED_BIT);
2787283b4a9bSStephen M. Cameron 		if (hpsa_get_raid_map(h, scsi3addr, this_device))
2788283b4a9bSStephen M. Cameron 			this_device->offload_enabled = 0;
2789283b4a9bSStephen M. Cameron 	}
279041ce4c35SStephen Cameron 	this_device->offload_to_be_enabled = this_device->offload_enabled;
2791283b4a9bSStephen M. Cameron out:
2792283b4a9bSStephen M. Cameron 	kfree(buf);
2793283b4a9bSStephen M. Cameron 	return;
2794283b4a9bSStephen M. Cameron }
2795283b4a9bSStephen M. Cameron 
2796edd16368SStephen M. Cameron /* Get the device id from inquiry page 0x83 */
2797edd16368SStephen M. Cameron static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
2798edd16368SStephen M. Cameron 	unsigned char *device_id, int buflen)
2799edd16368SStephen M. Cameron {
2800edd16368SStephen M. Cameron 	int rc;
2801edd16368SStephen M. Cameron 	unsigned char *buf;
2802edd16368SStephen M. Cameron 
2803edd16368SStephen M. Cameron 	if (buflen > 16)
2804edd16368SStephen M. Cameron 		buflen = 16;
2805edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2806edd16368SStephen M. Cameron 	if (!buf)
2807a84d794dSStephen M. Cameron 		return -ENOMEM;
2808b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0x83, buf, 64);
2809edd16368SStephen M. Cameron 	if (rc == 0)
2810edd16368SStephen M. Cameron 		memcpy(device_id, &buf[8], buflen);
2811edd16368SStephen M. Cameron 	kfree(buf);
2812edd16368SStephen M. Cameron 	return rc != 0;
2813edd16368SStephen M. Cameron }
2814edd16368SStephen M. Cameron 
2815edd16368SStephen M. Cameron static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
281603383736SDon Brace 		void *buf, int bufsize,
2817edd16368SStephen M. Cameron 		int extended_response)
2818edd16368SStephen M. Cameron {
2819edd16368SStephen M. Cameron 	int rc = IO_OK;
2820edd16368SStephen M. Cameron 	struct CommandList *c;
2821edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
2822edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2823edd16368SStephen M. Cameron 
282445fcb86eSStephen Cameron 	c = cmd_alloc(h);
2825bf43caf3SRobert Elliott 
2826e89c0ae7SStephen M. Cameron 	/* address the controller */
2827e89c0ae7SStephen M. Cameron 	memset(scsi3addr, 0, sizeof(scsi3addr));
2828a2dac136SStephen M. Cameron 	if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
2829a2dac136SStephen M. Cameron 		buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
2830a2dac136SStephen M. Cameron 		rc = -1;
2831a2dac136SStephen M. Cameron 		goto out;
2832a2dac136SStephen M. Cameron 	}
2833edd16368SStephen M. Cameron 	if (extended_response)
2834edd16368SStephen M. Cameron 		c->Request.CDB[1] = extended_response;
283525163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
283625163bd5SWebb Scales 					PCI_DMA_FROMDEVICE, NO_TIMEOUT);
283725163bd5SWebb Scales 	if (rc)
283825163bd5SWebb Scales 		goto out;
2839edd16368SStephen M. Cameron 	ei = c->err_info;
2840edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 &&
2841edd16368SStephen M. Cameron 	    ei->CommandStatus != CMD_DATA_UNDERRUN) {
2842d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2843edd16368SStephen M. Cameron 		rc = -1;
2844283b4a9bSStephen M. Cameron 	} else {
284503383736SDon Brace 		struct ReportLUNdata *rld = buf;
284603383736SDon Brace 
284703383736SDon Brace 		if (rld->extended_response_flag != extended_response) {
2848283b4a9bSStephen M. Cameron 			dev_err(&h->pdev->dev,
2849283b4a9bSStephen M. Cameron 				"report luns requested format %u, got %u\n",
2850283b4a9bSStephen M. Cameron 				extended_response,
285103383736SDon Brace 				rld->extended_response_flag);
2852283b4a9bSStephen M. Cameron 			rc = -1;
2853283b4a9bSStephen M. Cameron 		}
2854edd16368SStephen M. Cameron 	}
2855a2dac136SStephen M. Cameron out:
285645fcb86eSStephen Cameron 	cmd_free(h, c);
2857edd16368SStephen M. Cameron 	return rc;
2858edd16368SStephen M. Cameron }
2859edd16368SStephen M. Cameron 
2860edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
286103383736SDon Brace 		struct ReportExtendedLUNdata *buf, int bufsize)
2862edd16368SStephen M. Cameron {
286303383736SDon Brace 	return hpsa_scsi_do_report_luns(h, 0, buf, bufsize,
286403383736SDon Brace 						HPSA_REPORT_PHYS_EXTENDED);
2865edd16368SStephen M. Cameron }
2866edd16368SStephen M. Cameron 
2867edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
2868edd16368SStephen M. Cameron 		struct ReportLUNdata *buf, int bufsize)
2869edd16368SStephen M. Cameron {
2870edd16368SStephen M. Cameron 	return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
2871edd16368SStephen M. Cameron }
2872edd16368SStephen M. Cameron 
2873edd16368SStephen M. Cameron static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
2874edd16368SStephen M. Cameron 	int bus, int target, int lun)
2875edd16368SStephen M. Cameron {
2876edd16368SStephen M. Cameron 	device->bus = bus;
2877edd16368SStephen M. Cameron 	device->target = target;
2878edd16368SStephen M. Cameron 	device->lun = lun;
2879edd16368SStephen M. Cameron }
2880edd16368SStephen M. Cameron 
28819846590eSStephen M. Cameron /* Use VPD inquiry to get details of volume status */
28829846590eSStephen M. Cameron static int hpsa_get_volume_status(struct ctlr_info *h,
28839846590eSStephen M. Cameron 					unsigned char scsi3addr[])
28849846590eSStephen M. Cameron {
28859846590eSStephen M. Cameron 	int rc;
28869846590eSStephen M. Cameron 	int status;
28879846590eSStephen M. Cameron 	int size;
28889846590eSStephen M. Cameron 	unsigned char *buf;
28899846590eSStephen M. Cameron 
28909846590eSStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
28919846590eSStephen M. Cameron 	if (!buf)
28929846590eSStephen M. Cameron 		return HPSA_VPD_LV_STATUS_UNSUPPORTED;
28939846590eSStephen M. Cameron 
28949846590eSStephen M. Cameron 	/* Does controller have VPD for logical volume status? */
289524a4b078SStephen M. Cameron 	if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_STATUS))
28969846590eSStephen M. Cameron 		goto exit_failed;
28979846590eSStephen M. Cameron 
28989846590eSStephen M. Cameron 	/* Get the size of the VPD return buffer */
28999846590eSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
29009846590eSStephen M. Cameron 					buf, HPSA_VPD_HEADER_SZ);
290124a4b078SStephen M. Cameron 	if (rc != 0)
29029846590eSStephen M. Cameron 		goto exit_failed;
29039846590eSStephen M. Cameron 	size = buf[3];
29049846590eSStephen M. Cameron 
29059846590eSStephen M. Cameron 	/* Now get the whole VPD buffer */
29069846590eSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
29079846590eSStephen M. Cameron 					buf, size + HPSA_VPD_HEADER_SZ);
290824a4b078SStephen M. Cameron 	if (rc != 0)
29099846590eSStephen M. Cameron 		goto exit_failed;
29109846590eSStephen M. Cameron 	status = buf[4]; /* status byte */
29119846590eSStephen M. Cameron 
29129846590eSStephen M. Cameron 	kfree(buf);
29139846590eSStephen M. Cameron 	return status;
29149846590eSStephen M. Cameron exit_failed:
29159846590eSStephen M. Cameron 	kfree(buf);
29169846590eSStephen M. Cameron 	return HPSA_VPD_LV_STATUS_UNSUPPORTED;
29179846590eSStephen M. Cameron }
29189846590eSStephen M. Cameron 
29199846590eSStephen M. Cameron /* Determine offline status of a volume.
29209846590eSStephen M. Cameron  * Return either:
29219846590eSStephen M. Cameron  *  0 (not offline)
292267955ba3SStephen M. Cameron  *  0xff (offline for unknown reasons)
29239846590eSStephen M. Cameron  *  # (integer code indicating one of several NOT READY states
29249846590eSStephen M. Cameron  *     describing why a volume is to be kept offline)
29259846590eSStephen M. Cameron  */
292667955ba3SStephen M. Cameron static int hpsa_volume_offline(struct ctlr_info *h,
29279846590eSStephen M. Cameron 					unsigned char scsi3addr[])
29289846590eSStephen M. Cameron {
29299846590eSStephen M. Cameron 	struct CommandList *c;
29309437ac43SStephen Cameron 	unsigned char *sense;
29319437ac43SStephen Cameron 	u8 sense_key, asc, ascq;
29329437ac43SStephen Cameron 	int sense_len;
293325163bd5SWebb Scales 	int rc, ldstat = 0;
29349846590eSStephen M. Cameron 	u16 cmd_status;
29359846590eSStephen M. Cameron 	u8 scsi_status;
29369846590eSStephen M. Cameron #define ASC_LUN_NOT_READY 0x04
29379846590eSStephen M. Cameron #define ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS 0x04
29389846590eSStephen M. Cameron #define ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ 0x02
29399846590eSStephen M. Cameron 
29409846590eSStephen M. Cameron 	c = cmd_alloc(h);
2941bf43caf3SRobert Elliott 
29429846590eSStephen M. Cameron 	(void) fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, scsi3addr, TYPE_CMD);
294325163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
294425163bd5SWebb Scales 	if (rc) {
294525163bd5SWebb Scales 		cmd_free(h, c);
294625163bd5SWebb Scales 		return 0;
294725163bd5SWebb Scales 	}
29489846590eSStephen M. Cameron 	sense = c->err_info->SenseInfo;
29499437ac43SStephen Cameron 	if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
29509437ac43SStephen Cameron 		sense_len = sizeof(c->err_info->SenseInfo);
29519437ac43SStephen Cameron 	else
29529437ac43SStephen Cameron 		sense_len = c->err_info->SenseLen;
29539437ac43SStephen Cameron 	decode_sense_data(sense, sense_len, &sense_key, &asc, &ascq);
29549846590eSStephen M. Cameron 	cmd_status = c->err_info->CommandStatus;
29559846590eSStephen M. Cameron 	scsi_status = c->err_info->ScsiStatus;
29569846590eSStephen M. Cameron 	cmd_free(h, c);
29579846590eSStephen M. Cameron 	/* Is the volume 'not ready'? */
29589846590eSStephen M. Cameron 	if (cmd_status != CMD_TARGET_STATUS ||
29599846590eSStephen M. Cameron 		scsi_status != SAM_STAT_CHECK_CONDITION ||
29609846590eSStephen M. Cameron 		sense_key != NOT_READY ||
29619846590eSStephen M. Cameron 		asc != ASC_LUN_NOT_READY)  {
29629846590eSStephen M. Cameron 		return 0;
29639846590eSStephen M. Cameron 	}
29649846590eSStephen M. Cameron 
29659846590eSStephen M. Cameron 	/* Determine the reason for not ready state */
29669846590eSStephen M. Cameron 	ldstat = hpsa_get_volume_status(h, scsi3addr);
29679846590eSStephen M. Cameron 
29689846590eSStephen M. Cameron 	/* Keep volume offline in certain cases: */
29699846590eSStephen M. Cameron 	switch (ldstat) {
29709846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ERASE:
29719846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_RPI:
29729846590eSStephen M. Cameron 	case HPSA_LV_PENDING_RPI:
29739846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_NO_KEY:
29749846590eSStephen M. Cameron 	case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
29759846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION:
29769846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
29779846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
29789846590eSStephen M. Cameron 		return ldstat;
29799846590eSStephen M. Cameron 	case HPSA_VPD_LV_STATUS_UNSUPPORTED:
29809846590eSStephen M. Cameron 		/* If VPD status page isn't available,
29819846590eSStephen M. Cameron 		 * use ASC/ASCQ to determine state
29829846590eSStephen M. Cameron 		 */
29839846590eSStephen M. Cameron 		if ((ascq == ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS) ||
29849846590eSStephen M. Cameron 			(ascq == ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ))
29859846590eSStephen M. Cameron 			return ldstat;
29869846590eSStephen M. Cameron 		break;
29879846590eSStephen M. Cameron 	default:
29889846590eSStephen M. Cameron 		break;
29899846590eSStephen M. Cameron 	}
29909846590eSStephen M. Cameron 	return 0;
29919846590eSStephen M. Cameron }
29929846590eSStephen M. Cameron 
29939b5c48c2SStephen Cameron /*
29949b5c48c2SStephen Cameron  * Find out if a logical device supports aborts by simply trying one.
29959b5c48c2SStephen Cameron  * Smart Array may claim not to support aborts on logical drives, but
29969b5c48c2SStephen Cameron  * if a MSA2000 * is connected, the drives on that will be presented
29979b5c48c2SStephen Cameron  * by the Smart Array as logical drives, and aborts may be sent to
29989b5c48c2SStephen Cameron  * those devices successfully.  So the simplest way to find out is
29999b5c48c2SStephen Cameron  * to simply try an abort and see how the device responds.
30009b5c48c2SStephen Cameron  */
30019b5c48c2SStephen Cameron static int hpsa_device_supports_aborts(struct ctlr_info *h,
30029b5c48c2SStephen Cameron 					unsigned char *scsi3addr)
30039b5c48c2SStephen Cameron {
30049b5c48c2SStephen Cameron 	struct CommandList *c;
30059b5c48c2SStephen Cameron 	struct ErrorInfo *ei;
30069b5c48c2SStephen Cameron 	int rc = 0;
30079b5c48c2SStephen Cameron 
30089b5c48c2SStephen Cameron 	u64 tag = (u64) -1; /* bogus tag */
30099b5c48c2SStephen Cameron 
30109b5c48c2SStephen Cameron 	/* Assume that physical devices support aborts */
30119b5c48c2SStephen Cameron 	if (!is_logical_dev_addr_mode(scsi3addr))
30129b5c48c2SStephen Cameron 		return 1;
30139b5c48c2SStephen Cameron 
30149b5c48c2SStephen Cameron 	c = cmd_alloc(h);
3015bf43caf3SRobert Elliott 
30169b5c48c2SStephen Cameron 	(void) fill_cmd(c, HPSA_ABORT_MSG, h, &tag, 0, 0, scsi3addr, TYPE_MSG);
30179b5c48c2SStephen Cameron 	(void) hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
30189b5c48c2SStephen Cameron 	/* no unmap needed here because no data xfer. */
30199b5c48c2SStephen Cameron 	ei = c->err_info;
30209b5c48c2SStephen Cameron 	switch (ei->CommandStatus) {
30219b5c48c2SStephen Cameron 	case CMD_INVALID:
30229b5c48c2SStephen Cameron 		rc = 0;
30239b5c48c2SStephen Cameron 		break;
30249b5c48c2SStephen Cameron 	case CMD_UNABORTABLE:
30259b5c48c2SStephen Cameron 	case CMD_ABORT_FAILED:
30269b5c48c2SStephen Cameron 		rc = 1;
30279b5c48c2SStephen Cameron 		break;
30289437ac43SStephen Cameron 	case CMD_TMF_STATUS:
30299437ac43SStephen Cameron 		rc = hpsa_evaluate_tmf_status(h, c);
30309437ac43SStephen Cameron 		break;
30319b5c48c2SStephen Cameron 	default:
30329b5c48c2SStephen Cameron 		rc = 0;
30339b5c48c2SStephen Cameron 		break;
30349b5c48c2SStephen Cameron 	}
30359b5c48c2SStephen Cameron 	cmd_free(h, c);
30369b5c48c2SStephen Cameron 	return rc;
30379b5c48c2SStephen Cameron }
30389b5c48c2SStephen Cameron 
3039edd16368SStephen M. Cameron static int hpsa_update_device_info(struct ctlr_info *h,
30400b0e1d6cSStephen M. Cameron 	unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
30410b0e1d6cSStephen M. Cameron 	unsigned char *is_OBDR_device)
3042edd16368SStephen M. Cameron {
30430b0e1d6cSStephen M. Cameron 
30440b0e1d6cSStephen M. Cameron #define OBDR_SIG_OFFSET 43
30450b0e1d6cSStephen M. Cameron #define OBDR_TAPE_SIG "$DR-10"
30460b0e1d6cSStephen M. Cameron #define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
30470b0e1d6cSStephen M. Cameron #define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
30480b0e1d6cSStephen M. Cameron 
3049ea6d3bc3SStephen M. Cameron 	unsigned char *inq_buff;
30500b0e1d6cSStephen M. Cameron 	unsigned char *obdr_sig;
3051edd16368SStephen M. Cameron 
3052ea6d3bc3SStephen M. Cameron 	inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
3053edd16368SStephen M. Cameron 	if (!inq_buff)
3054edd16368SStephen M. Cameron 		goto bail_out;
3055edd16368SStephen M. Cameron 
3056edd16368SStephen M. Cameron 	/* Do an inquiry to the device to see what it is. */
3057edd16368SStephen M. Cameron 	if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
3058edd16368SStephen M. Cameron 		(unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
3059edd16368SStephen M. Cameron 		/* Inquiry failed (msg printed already) */
3060edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev,
3061edd16368SStephen M. Cameron 			"hpsa_update_device_info: inquiry failed\n");
3062edd16368SStephen M. Cameron 		goto bail_out;
3063edd16368SStephen M. Cameron 	}
3064edd16368SStephen M. Cameron 
3065edd16368SStephen M. Cameron 	this_device->devtype = (inq_buff[0] & 0x1f);
3066edd16368SStephen M. Cameron 	memcpy(this_device->scsi3addr, scsi3addr, 8);
3067edd16368SStephen M. Cameron 	memcpy(this_device->vendor, &inq_buff[8],
3068edd16368SStephen M. Cameron 		sizeof(this_device->vendor));
3069edd16368SStephen M. Cameron 	memcpy(this_device->model, &inq_buff[16],
3070edd16368SStephen M. Cameron 		sizeof(this_device->model));
3071edd16368SStephen M. Cameron 	memset(this_device->device_id, 0,
3072edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
3073edd16368SStephen M. Cameron 	hpsa_get_device_id(h, scsi3addr, this_device->device_id,
3074edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
3075edd16368SStephen M. Cameron 
3076edd16368SStephen M. Cameron 	if (this_device->devtype == TYPE_DISK &&
3077283b4a9bSStephen M. Cameron 		is_logical_dev_addr_mode(scsi3addr)) {
307867955ba3SStephen M. Cameron 		int volume_offline;
307967955ba3SStephen M. Cameron 
3080edd16368SStephen M. Cameron 		hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
3081283b4a9bSStephen M. Cameron 		if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
3082283b4a9bSStephen M. Cameron 			hpsa_get_ioaccel_status(h, scsi3addr, this_device);
308367955ba3SStephen M. Cameron 		volume_offline = hpsa_volume_offline(h, scsi3addr);
308467955ba3SStephen M. Cameron 		if (volume_offline < 0 || volume_offline > 0xff)
308567955ba3SStephen M. Cameron 			volume_offline = HPSA_VPD_LV_STATUS_UNSUPPORTED;
308667955ba3SStephen M. Cameron 		this_device->volume_offline = volume_offline & 0xff;
3087283b4a9bSStephen M. Cameron 	} else {
3088edd16368SStephen M. Cameron 		this_device->raid_level = RAID_UNKNOWN;
3089283b4a9bSStephen M. Cameron 		this_device->offload_config = 0;
3090283b4a9bSStephen M. Cameron 		this_device->offload_enabled = 0;
309141ce4c35SStephen Cameron 		this_device->offload_to_be_enabled = 0;
3092a3144e0bSJoe Handzik 		this_device->hba_ioaccel_enabled = 0;
30939846590eSStephen M. Cameron 		this_device->volume_offline = 0;
309403383736SDon Brace 		this_device->queue_depth = h->nr_cmds;
3095283b4a9bSStephen M. Cameron 	}
3096edd16368SStephen M. Cameron 
30970b0e1d6cSStephen M. Cameron 	if (is_OBDR_device) {
30980b0e1d6cSStephen M. Cameron 		/* See if this is a One-Button-Disaster-Recovery device
30990b0e1d6cSStephen M. Cameron 		 * by looking for "$DR-10" at offset 43 in inquiry data.
31000b0e1d6cSStephen M. Cameron 		 */
31010b0e1d6cSStephen M. Cameron 		obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
31020b0e1d6cSStephen M. Cameron 		*is_OBDR_device = (this_device->devtype == TYPE_ROM &&
31030b0e1d6cSStephen M. Cameron 					strncmp(obdr_sig, OBDR_TAPE_SIG,
31040b0e1d6cSStephen M. Cameron 						OBDR_SIG_LEN) == 0);
31050b0e1d6cSStephen M. Cameron 	}
3106edd16368SStephen M. Cameron 	kfree(inq_buff);
3107edd16368SStephen M. Cameron 	return 0;
3108edd16368SStephen M. Cameron 
3109edd16368SStephen M. Cameron bail_out:
3110edd16368SStephen M. Cameron 	kfree(inq_buff);
3111edd16368SStephen M. Cameron 	return 1;
3112edd16368SStephen M. Cameron }
3113edd16368SStephen M. Cameron 
31149b5c48c2SStephen Cameron static void hpsa_update_device_supports_aborts(struct ctlr_info *h,
31159b5c48c2SStephen Cameron 			struct hpsa_scsi_dev_t *dev, u8 *scsi3addr)
31169b5c48c2SStephen Cameron {
31179b5c48c2SStephen Cameron 	unsigned long flags;
31189b5c48c2SStephen Cameron 	int rc, entry;
31199b5c48c2SStephen Cameron 	/*
31209b5c48c2SStephen Cameron 	 * See if this device supports aborts.  If we already know
31219b5c48c2SStephen Cameron 	 * the device, we already know if it supports aborts, otherwise
31229b5c48c2SStephen Cameron 	 * we have to find out if it supports aborts by trying one.
31239b5c48c2SStephen Cameron 	 */
31249b5c48c2SStephen Cameron 	spin_lock_irqsave(&h->devlock, flags);
31259b5c48c2SStephen Cameron 	rc = hpsa_scsi_find_entry(dev, h->dev, h->ndevices, &entry);
31269b5c48c2SStephen Cameron 	if ((rc == DEVICE_SAME || rc == DEVICE_UPDATED) &&
31279b5c48c2SStephen Cameron 		entry >= 0 && entry < h->ndevices) {
31289b5c48c2SStephen Cameron 		dev->supports_aborts = h->dev[entry]->supports_aborts;
31299b5c48c2SStephen Cameron 		spin_unlock_irqrestore(&h->devlock, flags);
31309b5c48c2SStephen Cameron 	} else {
31319b5c48c2SStephen Cameron 		spin_unlock_irqrestore(&h->devlock, flags);
31329b5c48c2SStephen Cameron 		dev->supports_aborts =
31339b5c48c2SStephen Cameron 				hpsa_device_supports_aborts(h, scsi3addr);
31349b5c48c2SStephen Cameron 		if (dev->supports_aborts < 0)
31359b5c48c2SStephen Cameron 			dev->supports_aborts = 0;
31369b5c48c2SStephen Cameron 	}
31379b5c48c2SStephen Cameron }
31389b5c48c2SStephen Cameron 
31394f4eb9f1SScott Teel static unsigned char *ext_target_model[] = {
3140edd16368SStephen M. Cameron 	"MSA2012",
3141edd16368SStephen M. Cameron 	"MSA2024",
3142edd16368SStephen M. Cameron 	"MSA2312",
3143edd16368SStephen M. Cameron 	"MSA2324",
3144fda38518SStephen M. Cameron 	"P2000 G3 SAS",
3145e06c8e5cSStephen M. Cameron 	"MSA 2040 SAS",
3146edd16368SStephen M. Cameron 	NULL,
3147edd16368SStephen M. Cameron };
3148edd16368SStephen M. Cameron 
31494f4eb9f1SScott Teel static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
3150edd16368SStephen M. Cameron {
3151edd16368SStephen M. Cameron 	int i;
3152edd16368SStephen M. Cameron 
31534f4eb9f1SScott Teel 	for (i = 0; ext_target_model[i]; i++)
31544f4eb9f1SScott Teel 		if (strncmp(device->model, ext_target_model[i],
31554f4eb9f1SScott Teel 			strlen(ext_target_model[i])) == 0)
3156edd16368SStephen M. Cameron 			return 1;
3157edd16368SStephen M. Cameron 	return 0;
3158edd16368SStephen M. Cameron }
3159edd16368SStephen M. Cameron 
3160edd16368SStephen M. Cameron /* Helper function to assign bus, target, lun mapping of devices.
31614f4eb9f1SScott Teel  * Puts non-external target logical volumes on bus 0, external target logical
3162edd16368SStephen M. Cameron  * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
3163edd16368SStephen M. Cameron  * Logical drive target and lun are assigned at this time, but
3164edd16368SStephen M. Cameron  * physical device lun and target assignment are deferred (assigned
3165edd16368SStephen M. Cameron  * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
3166edd16368SStephen M. Cameron  */
3167edd16368SStephen M. Cameron static void figure_bus_target_lun(struct ctlr_info *h,
31681f310bdeSStephen M. Cameron 	u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
3169edd16368SStephen M. Cameron {
31701f310bdeSStephen M. Cameron 	u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
3171edd16368SStephen M. Cameron 
31721f310bdeSStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes)) {
31731f310bdeSStephen M. Cameron 		/* physical device, target and lun filled in later */
31741f310bdeSStephen M. Cameron 		if (is_hba_lunid(lunaddrbytes))
31751f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
31761f310bdeSStephen M. Cameron 		else
31771f310bdeSStephen M. Cameron 			/* defer target, lun assignment for physical devices */
31781f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 2, -1, -1);
31791f310bdeSStephen M. Cameron 		return;
31801f310bdeSStephen M. Cameron 	}
31811f310bdeSStephen M. Cameron 	/* It's a logical device */
31824f4eb9f1SScott Teel 	if (is_ext_target(h, device)) {
31834f4eb9f1SScott Teel 		/* external target way, put logicals on bus 1
3184339b2b14SStephen M. Cameron 		 * and match target/lun numbers box
31851f310bdeSStephen M. Cameron 		 * reports, other smart array, bus 0, target 0, match lunid
3186339b2b14SStephen M. Cameron 		 */
31871f310bdeSStephen M. Cameron 		hpsa_set_bus_target_lun(device,
31881f310bdeSStephen M. Cameron 			1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
31891f310bdeSStephen M. Cameron 		return;
3190339b2b14SStephen M. Cameron 	}
31911f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
3192edd16368SStephen M. Cameron }
3193edd16368SStephen M. Cameron 
3194edd16368SStephen M. Cameron /*
3195edd16368SStephen M. Cameron  * If there is no lun 0 on a target, linux won't find any devices.
31964f4eb9f1SScott Teel  * For the external targets (arrays), we have to manually detect the enclosure
3197edd16368SStephen M. Cameron  * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
3198edd16368SStephen M. Cameron  * it for some reason.  *tmpdevice is the target we're adding,
3199edd16368SStephen M. Cameron  * this_device is a pointer into the current element of currentsd[]
3200edd16368SStephen M. Cameron  * that we're building up in update_scsi_devices(), below.
3201edd16368SStephen M. Cameron  * lunzerobits is a bitmap that tracks which targets already have a
3202edd16368SStephen M. Cameron  * lun 0 assigned.
3203edd16368SStephen M. Cameron  * Returns 1 if an enclosure was added, 0 if not.
3204edd16368SStephen M. Cameron  */
32054f4eb9f1SScott Teel static int add_ext_target_dev(struct ctlr_info *h,
3206edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *tmpdevice,
320701a02ffcSStephen M. Cameron 	struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
32084f4eb9f1SScott Teel 	unsigned long lunzerobits[], int *n_ext_target_devs)
3209edd16368SStephen M. Cameron {
3210edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
3211edd16368SStephen M. Cameron 
32121f310bdeSStephen M. Cameron 	if (test_bit(tmpdevice->target, lunzerobits))
3213edd16368SStephen M. Cameron 		return 0; /* There is already a lun 0 on this target. */
3214edd16368SStephen M. Cameron 
3215edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes))
3216edd16368SStephen M. Cameron 		return 0; /* It's the logical targets that may lack lun 0. */
3217edd16368SStephen M. Cameron 
32184f4eb9f1SScott Teel 	if (!is_ext_target(h, tmpdevice))
32194f4eb9f1SScott Teel 		return 0; /* Only external target devices have this problem. */
3220edd16368SStephen M. Cameron 
32211f310bdeSStephen M. Cameron 	if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
3222edd16368SStephen M. Cameron 		return 0;
3223edd16368SStephen M. Cameron 
3224c4f8a299SStephen M. Cameron 	memset(scsi3addr, 0, 8);
32251f310bdeSStephen M. Cameron 	scsi3addr[3] = tmpdevice->target;
3226edd16368SStephen M. Cameron 	if (is_hba_lunid(scsi3addr))
3227edd16368SStephen M. Cameron 		return 0; /* Don't add the RAID controller here. */
3228edd16368SStephen M. Cameron 
3229339b2b14SStephen M. Cameron 	if (is_scsi_rev_5(h))
3230339b2b14SStephen M. Cameron 		return 0; /* p1210m doesn't need to do this. */
3231339b2b14SStephen M. Cameron 
32324f4eb9f1SScott Teel 	if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
3233aca4a520SScott Teel 		dev_warn(&h->pdev->dev, "Maximum number of external "
3234aca4a520SScott Teel 			"target devices exceeded.  Check your hardware "
3235edd16368SStephen M. Cameron 			"configuration.");
3236edd16368SStephen M. Cameron 		return 0;
3237edd16368SStephen M. Cameron 	}
3238edd16368SStephen M. Cameron 
32390b0e1d6cSStephen M. Cameron 	if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
3240edd16368SStephen M. Cameron 		return 0;
32414f4eb9f1SScott Teel 	(*n_ext_target_devs)++;
32421f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(this_device,
32431f310bdeSStephen M. Cameron 				tmpdevice->bus, tmpdevice->target, 0);
32449b5c48c2SStephen Cameron 	hpsa_update_device_supports_aborts(h, this_device, scsi3addr);
32451f310bdeSStephen M. Cameron 	set_bit(tmpdevice->target, lunzerobits);
3246edd16368SStephen M. Cameron 	return 1;
3247edd16368SStephen M. Cameron }
3248edd16368SStephen M. Cameron 
3249edd16368SStephen M. Cameron /*
325054b6e9e9SScott Teel  * Get address of physical disk used for an ioaccel2 mode command:
325154b6e9e9SScott Teel  *	1. Extract ioaccel2 handle from the command.
325254b6e9e9SScott Teel  *	2. Find a matching ioaccel2 handle from list of physical disks.
325354b6e9e9SScott Teel  *	3. Return:
325454b6e9e9SScott Teel  *		1 and set scsi3addr to address of matching physical
325554b6e9e9SScott Teel  *		0 if no matching physical disk was found.
325654b6e9e9SScott Teel  */
325754b6e9e9SScott Teel static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
325854b6e9e9SScott Teel 	struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
325954b6e9e9SScott Teel {
326041ce4c35SStephen Cameron 	struct io_accel2_cmd *c2 =
326141ce4c35SStephen Cameron 			&h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
326241ce4c35SStephen Cameron 	unsigned long flags;
326354b6e9e9SScott Teel 	int i;
326454b6e9e9SScott Teel 
326541ce4c35SStephen Cameron 	spin_lock_irqsave(&h->devlock, flags);
326641ce4c35SStephen Cameron 	for (i = 0; i < h->ndevices; i++)
326741ce4c35SStephen Cameron 		if (h->dev[i]->ioaccel_handle == le32_to_cpu(c2->scsi_nexus)) {
326841ce4c35SStephen Cameron 			memcpy(scsi3addr, h->dev[i]->scsi3addr,
326941ce4c35SStephen Cameron 				sizeof(h->dev[i]->scsi3addr));
327041ce4c35SStephen Cameron 			spin_unlock_irqrestore(&h->devlock, flags);
327154b6e9e9SScott Teel 			return 1;
327254b6e9e9SScott Teel 		}
327341ce4c35SStephen Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
327441ce4c35SStephen Cameron 	return 0;
327541ce4c35SStephen Cameron }
327641ce4c35SStephen Cameron 
327754b6e9e9SScott Teel /*
3278edd16368SStephen M. Cameron  * Do CISS_REPORT_PHYS and CISS_REPORT_LOG.  Data is returned in physdev,
3279edd16368SStephen M. Cameron  * logdev.  The number of luns in physdev and logdev are returned in
3280edd16368SStephen M. Cameron  * *nphysicals and *nlogicals, respectively.
3281edd16368SStephen M. Cameron  * Returns 0 on success, -1 otherwise.
3282edd16368SStephen M. Cameron  */
3283edd16368SStephen M. Cameron static int hpsa_gather_lun_info(struct ctlr_info *h,
328403383736SDon Brace 	struct ReportExtendedLUNdata *physdev, u32 *nphysicals,
328501a02ffcSStephen M. Cameron 	struct ReportLUNdata *logdev, u32 *nlogicals)
3286edd16368SStephen M. Cameron {
328703383736SDon Brace 	if (hpsa_scsi_do_report_phys_luns(h, physdev, sizeof(*physdev))) {
3288edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
3289edd16368SStephen M. Cameron 		return -1;
3290edd16368SStephen M. Cameron 	}
329103383736SDon Brace 	*nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 24;
3292edd16368SStephen M. Cameron 	if (*nphysicals > HPSA_MAX_PHYS_LUN) {
329303383736SDon Brace 		dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded. %d LUNs ignored.\n",
329403383736SDon Brace 			HPSA_MAX_PHYS_LUN, *nphysicals - HPSA_MAX_PHYS_LUN);
3295edd16368SStephen M. Cameron 		*nphysicals = HPSA_MAX_PHYS_LUN;
3296edd16368SStephen M. Cameron 	}
329703383736SDon Brace 	if (hpsa_scsi_do_report_log_luns(h, logdev, sizeof(*logdev))) {
3298edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
3299edd16368SStephen M. Cameron 		return -1;
3300edd16368SStephen M. Cameron 	}
33016df1e954SStephen M. Cameron 	*nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
3302edd16368SStephen M. Cameron 	/* Reject Logicals in excess of our max capability. */
3303edd16368SStephen M. Cameron 	if (*nlogicals > HPSA_MAX_LUN) {
3304edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
3305edd16368SStephen M. Cameron 			"maximum logical LUNs (%d) exceeded.  "
3306edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_LUN,
3307edd16368SStephen M. Cameron 			*nlogicals - HPSA_MAX_LUN);
3308edd16368SStephen M. Cameron 			*nlogicals = HPSA_MAX_LUN;
3309edd16368SStephen M. Cameron 	}
3310edd16368SStephen M. Cameron 	if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
3311edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
3312edd16368SStephen M. Cameron 			"maximum logical + physical LUNs (%d) exceeded. "
3313edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
3314edd16368SStephen M. Cameron 			*nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
3315edd16368SStephen M. Cameron 		*nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
3316edd16368SStephen M. Cameron 	}
3317edd16368SStephen M. Cameron 	return 0;
3318edd16368SStephen M. Cameron }
3319edd16368SStephen M. Cameron 
332042a91641SDon Brace static u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position,
332142a91641SDon Brace 	int i, int nphysicals, int nlogicals,
3322a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list,
3323339b2b14SStephen M. Cameron 	struct ReportLUNdata *logdev_list)
3324339b2b14SStephen M. Cameron {
3325339b2b14SStephen M. Cameron 	/* Helper function, figure out where the LUN ID info is coming from
3326339b2b14SStephen M. Cameron 	 * given index i, lists of physical and logical devices, where in
3327339b2b14SStephen M. Cameron 	 * the list the raid controller is supposed to appear (first or last)
3328339b2b14SStephen M. Cameron 	 */
3329339b2b14SStephen M. Cameron 
3330339b2b14SStephen M. Cameron 	int logicals_start = nphysicals + (raid_ctlr_position == 0);
3331339b2b14SStephen M. Cameron 	int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
3332339b2b14SStephen M. Cameron 
3333339b2b14SStephen M. Cameron 	if (i == raid_ctlr_position)
3334339b2b14SStephen M. Cameron 		return RAID_CTLR_LUNID;
3335339b2b14SStephen M. Cameron 
3336339b2b14SStephen M. Cameron 	if (i < logicals_start)
3337d5b5d964SStephen M. Cameron 		return &physdev_list->LUN[i -
3338d5b5d964SStephen M. Cameron 				(raid_ctlr_position == 0)].lunid[0];
3339339b2b14SStephen M. Cameron 
3340339b2b14SStephen M. Cameron 	if (i < last_device)
3341339b2b14SStephen M. Cameron 		return &logdev_list->LUN[i - nphysicals -
3342339b2b14SStephen M. Cameron 			(raid_ctlr_position == 0)][0];
3343339b2b14SStephen M. Cameron 	BUG();
3344339b2b14SStephen M. Cameron 	return NULL;
3345339b2b14SStephen M. Cameron }
3346339b2b14SStephen M. Cameron 
3347316b221aSStephen M. Cameron static int hpsa_hba_mode_enabled(struct ctlr_info *h)
3348316b221aSStephen M. Cameron {
3349316b221aSStephen M. Cameron 	int rc;
33506e8e8088SJoe Handzik 	int hba_mode_enabled;
3351316b221aSStephen M. Cameron 	struct bmic_controller_parameters *ctlr_params;
3352316b221aSStephen M. Cameron 	ctlr_params = kzalloc(sizeof(struct bmic_controller_parameters),
3353316b221aSStephen M. Cameron 		GFP_KERNEL);
3354316b221aSStephen M. Cameron 
3355316b221aSStephen M. Cameron 	if (!ctlr_params)
335696444fbbSJoe Handzik 		return -ENOMEM;
3357316b221aSStephen M. Cameron 	rc = hpsa_bmic_ctrl_mode_sense(h, RAID_CTLR_LUNID, 0, ctlr_params,
3358316b221aSStephen M. Cameron 		sizeof(struct bmic_controller_parameters));
335996444fbbSJoe Handzik 	if (rc) {
3360316b221aSStephen M. Cameron 		kfree(ctlr_params);
336196444fbbSJoe Handzik 		return rc;
3362316b221aSStephen M. Cameron 	}
33636e8e8088SJoe Handzik 
33646e8e8088SJoe Handzik 	hba_mode_enabled =
33656e8e8088SJoe Handzik 		((ctlr_params->nvram_flags & HBA_MODE_ENABLED_FLAG) != 0);
33666e8e8088SJoe Handzik 	kfree(ctlr_params);
33676e8e8088SJoe Handzik 	return hba_mode_enabled;
3368316b221aSStephen M. Cameron }
3369316b221aSStephen M. Cameron 
337003383736SDon Brace /* get physical drive ioaccel handle and queue depth */
337103383736SDon Brace static void hpsa_get_ioaccel_drive_info(struct ctlr_info *h,
337203383736SDon Brace 		struct hpsa_scsi_dev_t *dev,
337303383736SDon Brace 		u8 *lunaddrbytes,
337403383736SDon Brace 		struct bmic_identify_physical_device *id_phys)
337503383736SDon Brace {
337603383736SDon Brace 	int rc;
337703383736SDon Brace 	struct ext_report_lun_entry *rle =
337803383736SDon Brace 		(struct ext_report_lun_entry *) lunaddrbytes;
337903383736SDon Brace 
338003383736SDon Brace 	dev->ioaccel_handle = rle->ioaccel_handle;
3381a3144e0bSJoe Handzik 	if (PHYS_IOACCEL(lunaddrbytes) && dev->ioaccel_handle)
3382a3144e0bSJoe Handzik 		dev->hba_ioaccel_enabled = 1;
338303383736SDon Brace 	memset(id_phys, 0, sizeof(*id_phys));
338403383736SDon Brace 	rc = hpsa_bmic_id_physical_device(h, lunaddrbytes,
338503383736SDon Brace 			GET_BMIC_DRIVE_NUMBER(lunaddrbytes), id_phys,
338603383736SDon Brace 			sizeof(*id_phys));
338703383736SDon Brace 	if (!rc)
338803383736SDon Brace 		/* Reserve space for FW operations */
338903383736SDon Brace #define DRIVE_CMDS_RESERVED_FOR_FW 2
339003383736SDon Brace #define DRIVE_QUEUE_DEPTH 7
339103383736SDon Brace 		dev->queue_depth =
339203383736SDon Brace 			le16_to_cpu(id_phys->current_queue_depth_limit) -
339303383736SDon Brace 				DRIVE_CMDS_RESERVED_FOR_FW;
339403383736SDon Brace 	else
339503383736SDon Brace 		dev->queue_depth = DRIVE_QUEUE_DEPTH; /* conservative */
339603383736SDon Brace 	atomic_set(&dev->ioaccel_cmds_out, 0);
339703383736SDon Brace }
339803383736SDon Brace 
3399edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
3400edd16368SStephen M. Cameron {
3401edd16368SStephen M. Cameron 	/* the idea here is we could get notified
3402edd16368SStephen M. Cameron 	 * that some devices have changed, so we do a report
3403edd16368SStephen M. Cameron 	 * physical luns and report logical luns cmd, and adjust
3404edd16368SStephen M. Cameron 	 * our list of devices accordingly.
3405edd16368SStephen M. Cameron 	 *
3406edd16368SStephen M. Cameron 	 * The scsi3addr's of devices won't change so long as the
3407edd16368SStephen M. Cameron 	 * adapter is not reset.  That means we can rescan and
3408edd16368SStephen M. Cameron 	 * tell which devices we already know about, vs. new
3409edd16368SStephen M. Cameron 	 * devices, vs.  disappearing devices.
3410edd16368SStephen M. Cameron 	 */
3411a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list = NULL;
3412edd16368SStephen M. Cameron 	struct ReportLUNdata *logdev_list = NULL;
341303383736SDon Brace 	struct bmic_identify_physical_device *id_phys = NULL;
341401a02ffcSStephen M. Cameron 	u32 nphysicals = 0;
341501a02ffcSStephen M. Cameron 	u32 nlogicals = 0;
341601a02ffcSStephen M. Cameron 	u32 ndev_allocated = 0;
3417edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
3418edd16368SStephen M. Cameron 	int ncurrent = 0;
34194f4eb9f1SScott Teel 	int i, n_ext_target_devs, ndevs_to_allocate;
3420339b2b14SStephen M. Cameron 	int raid_ctlr_position;
34212bbf5c7fSJoe Handzik 	int rescan_hba_mode;
3422aca4a520SScott Teel 	DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
3423edd16368SStephen M. Cameron 
3424cfe5badcSScott Teel 	currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
342592084715SStephen M. Cameron 	physdev_list = kzalloc(sizeof(*physdev_list), GFP_KERNEL);
342692084715SStephen M. Cameron 	logdev_list = kzalloc(sizeof(*logdev_list), GFP_KERNEL);
3427edd16368SStephen M. Cameron 	tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
342803383736SDon Brace 	id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
3429edd16368SStephen M. Cameron 
343003383736SDon Brace 	if (!currentsd || !physdev_list || !logdev_list ||
343103383736SDon Brace 		!tmpdevice || !id_phys) {
3432edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory\n");
3433edd16368SStephen M. Cameron 		goto out;
3434edd16368SStephen M. Cameron 	}
3435edd16368SStephen M. Cameron 	memset(lunzerobits, 0, sizeof(lunzerobits));
3436edd16368SStephen M. Cameron 
3437316b221aSStephen M. Cameron 	rescan_hba_mode = hpsa_hba_mode_enabled(h);
343896444fbbSJoe Handzik 	if (rescan_hba_mode < 0)
343996444fbbSJoe Handzik 		goto out;
3440316b221aSStephen M. Cameron 
3441316b221aSStephen M. Cameron 	if (!h->hba_mode_enabled && rescan_hba_mode)
3442316b221aSStephen M. Cameron 		dev_warn(&h->pdev->dev, "HBA mode enabled\n");
3443316b221aSStephen M. Cameron 	else if (h->hba_mode_enabled && !rescan_hba_mode)
3444316b221aSStephen M. Cameron 		dev_warn(&h->pdev->dev, "HBA mode disabled\n");
3445316b221aSStephen M. Cameron 
3446316b221aSStephen M. Cameron 	h->hba_mode_enabled = rescan_hba_mode;
3447316b221aSStephen M. Cameron 
344803383736SDon Brace 	if (hpsa_gather_lun_info(h, physdev_list, &nphysicals,
344903383736SDon Brace 			logdev_list, &nlogicals))
3450edd16368SStephen M. Cameron 		goto out;
3451edd16368SStephen M. Cameron 
3452aca4a520SScott Teel 	/* We might see up to the maximum number of logical and physical disks
3453aca4a520SScott Teel 	 * plus external target devices, and a device for the local RAID
3454aca4a520SScott Teel 	 * controller.
3455edd16368SStephen M. Cameron 	 */
3456aca4a520SScott Teel 	ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
3457edd16368SStephen M. Cameron 
3458edd16368SStephen M. Cameron 	/* Allocate the per device structures */
3459edd16368SStephen M. Cameron 	for (i = 0; i < ndevs_to_allocate; i++) {
3460b7ec021fSScott Teel 		if (i >= HPSA_MAX_DEVICES) {
3461b7ec021fSScott Teel 			dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
3462b7ec021fSScott Teel 				"  %d devices ignored.\n", HPSA_MAX_DEVICES,
3463b7ec021fSScott Teel 				ndevs_to_allocate - HPSA_MAX_DEVICES);
3464b7ec021fSScott Teel 			break;
3465b7ec021fSScott Teel 		}
3466b7ec021fSScott Teel 
3467edd16368SStephen M. Cameron 		currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
3468edd16368SStephen M. Cameron 		if (!currentsd[i]) {
3469edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
3470edd16368SStephen M. Cameron 				__FILE__, __LINE__);
3471edd16368SStephen M. Cameron 			goto out;
3472edd16368SStephen M. Cameron 		}
3473edd16368SStephen M. Cameron 		ndev_allocated++;
3474edd16368SStephen M. Cameron 	}
3475edd16368SStephen M. Cameron 
34768645291bSStephen M. Cameron 	if (is_scsi_rev_5(h))
3477339b2b14SStephen M. Cameron 		raid_ctlr_position = 0;
3478339b2b14SStephen M. Cameron 	else
3479339b2b14SStephen M. Cameron 		raid_ctlr_position = nphysicals + nlogicals;
3480339b2b14SStephen M. Cameron 
3481edd16368SStephen M. Cameron 	/* adjust our table of devices */
34824f4eb9f1SScott Teel 	n_ext_target_devs = 0;
3483edd16368SStephen M. Cameron 	for (i = 0; i < nphysicals + nlogicals + 1; i++) {
34840b0e1d6cSStephen M. Cameron 		u8 *lunaddrbytes, is_OBDR = 0;
3485edd16368SStephen M. Cameron 
3486edd16368SStephen M. Cameron 		/* Figure out where the LUN ID info is coming from */
3487339b2b14SStephen M. Cameron 		lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
3488339b2b14SStephen M. Cameron 			i, nphysicals, nlogicals, physdev_list, logdev_list);
348941ce4c35SStephen Cameron 
349041ce4c35SStephen Cameron 		/* skip masked non-disk devices */
349141ce4c35SStephen Cameron 		if (MASKED_DEVICE(lunaddrbytes))
349241ce4c35SStephen Cameron 			if (i < nphysicals + (raid_ctlr_position == 0) &&
349341ce4c35SStephen Cameron 				NON_DISK_PHYS_DEV(lunaddrbytes))
3494edd16368SStephen M. Cameron 				continue;
3495edd16368SStephen M. Cameron 
3496edd16368SStephen M. Cameron 		/* Get device type, vendor, model, device id */
34970b0e1d6cSStephen M. Cameron 		if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
34980b0e1d6cSStephen M. Cameron 							&is_OBDR))
3499edd16368SStephen M. Cameron 			continue; /* skip it if we can't talk to it. */
35001f310bdeSStephen M. Cameron 		figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
35019b5c48c2SStephen Cameron 		hpsa_update_device_supports_aborts(h, tmpdevice, lunaddrbytes);
3502edd16368SStephen M. Cameron 		this_device = currentsd[ncurrent];
3503edd16368SStephen M. Cameron 
3504edd16368SStephen M. Cameron 		/*
35054f4eb9f1SScott Teel 		 * For external target devices, we have to insert a LUN 0 which
3506edd16368SStephen M. Cameron 		 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
3507edd16368SStephen M. Cameron 		 * is nonetheless an enclosure device there.  We have to
3508edd16368SStephen M. Cameron 		 * present that otherwise linux won't find anything if
3509edd16368SStephen M. Cameron 		 * there is no lun 0.
3510edd16368SStephen M. Cameron 		 */
35114f4eb9f1SScott Teel 		if (add_ext_target_dev(h, tmpdevice, this_device,
35121f310bdeSStephen M. Cameron 				lunaddrbytes, lunzerobits,
35134f4eb9f1SScott Teel 				&n_ext_target_devs)) {
3514edd16368SStephen M. Cameron 			ncurrent++;
3515edd16368SStephen M. Cameron 			this_device = currentsd[ncurrent];
3516edd16368SStephen M. Cameron 		}
3517edd16368SStephen M. Cameron 
3518edd16368SStephen M. Cameron 		*this_device = *tmpdevice;
3519edd16368SStephen M. Cameron 
352041ce4c35SStephen Cameron 		/* do not expose masked devices */
352141ce4c35SStephen Cameron 		if (MASKED_DEVICE(lunaddrbytes) &&
352241ce4c35SStephen Cameron 			i < nphysicals + (raid_ctlr_position == 0)) {
352341ce4c35SStephen Cameron 			if (h->hba_mode_enabled)
352441ce4c35SStephen Cameron 				dev_warn(&h->pdev->dev,
352541ce4c35SStephen Cameron 					"Masked physical device detected\n");
352641ce4c35SStephen Cameron 			this_device->expose_state = HPSA_DO_NOT_EXPOSE;
352741ce4c35SStephen Cameron 		} else {
352841ce4c35SStephen Cameron 			this_device->expose_state =
352941ce4c35SStephen Cameron 					HPSA_SG_ATTACH | HPSA_ULD_ATTACH;
353041ce4c35SStephen Cameron 		}
353141ce4c35SStephen Cameron 
3532edd16368SStephen M. Cameron 		switch (this_device->devtype) {
35330b0e1d6cSStephen M. Cameron 		case TYPE_ROM:
3534edd16368SStephen M. Cameron 			/* We don't *really* support actual CD-ROM devices,
3535edd16368SStephen M. Cameron 			 * just "One Button Disaster Recovery" tape drive
3536edd16368SStephen M. Cameron 			 * which temporarily pretends to be a CD-ROM drive.
3537edd16368SStephen M. Cameron 			 * So we check that the device is really an OBDR tape
3538edd16368SStephen M. Cameron 			 * device by checking for "$DR-10" in bytes 43-48 of
3539edd16368SStephen M. Cameron 			 * the inquiry data.
3540edd16368SStephen M. Cameron 			 */
35410b0e1d6cSStephen M. Cameron 			if (is_OBDR)
3542edd16368SStephen M. Cameron 				ncurrent++;
3543edd16368SStephen M. Cameron 			break;
3544edd16368SStephen M. Cameron 		case TYPE_DISK:
3545283b4a9bSStephen M. Cameron 			if (i >= nphysicals) {
3546283b4a9bSStephen M. Cameron 				ncurrent++;
3547edd16368SStephen M. Cameron 				break;
3548283b4a9bSStephen M. Cameron 			}
3549ecf418d1SJoe Handzik 
3550ecf418d1SJoe Handzik 			if (h->hba_mode_enabled)
3551ecf418d1SJoe Handzik 				/* never use raid mapper in HBA mode */
3552ecf418d1SJoe Handzik 				this_device->offload_enabled = 0;
3553ecf418d1SJoe Handzik 			else if (!(h->transMethod & CFGTBL_Trans_io_accel1 ||
3554ecf418d1SJoe Handzik 				h->transMethod & CFGTBL_Trans_io_accel2))
3555316b221aSStephen M. Cameron 				break;
3556ecf418d1SJoe Handzik 
355703383736SDon Brace 			hpsa_get_ioaccel_drive_info(h, this_device,
355803383736SDon Brace 						lunaddrbytes, id_phys);
355903383736SDon Brace 			atomic_set(&this_device->ioaccel_cmds_out, 0);
3560edd16368SStephen M. Cameron 			ncurrent++;
3561edd16368SStephen M. Cameron 			break;
3562edd16368SStephen M. Cameron 		case TYPE_TAPE:
3563edd16368SStephen M. Cameron 		case TYPE_MEDIUM_CHANGER:
3564edd16368SStephen M. Cameron 			ncurrent++;
3565edd16368SStephen M. Cameron 			break;
356641ce4c35SStephen Cameron 		case TYPE_ENCLOSURE:
356741ce4c35SStephen Cameron 			if (h->hba_mode_enabled)
356841ce4c35SStephen Cameron 				ncurrent++;
356941ce4c35SStephen Cameron 			break;
3570edd16368SStephen M. Cameron 		case TYPE_RAID:
3571edd16368SStephen M. Cameron 			/* Only present the Smartarray HBA as a RAID controller.
3572edd16368SStephen M. Cameron 			 * If it's a RAID controller other than the HBA itself
3573edd16368SStephen M. Cameron 			 * (an external RAID controller, MSA500 or similar)
3574edd16368SStephen M. Cameron 			 * don't present it.
3575edd16368SStephen M. Cameron 			 */
3576edd16368SStephen M. Cameron 			if (!is_hba_lunid(lunaddrbytes))
3577edd16368SStephen M. Cameron 				break;
3578edd16368SStephen M. Cameron 			ncurrent++;
3579edd16368SStephen M. Cameron 			break;
3580edd16368SStephen M. Cameron 		default:
3581edd16368SStephen M. Cameron 			break;
3582edd16368SStephen M. Cameron 		}
3583cfe5badcSScott Teel 		if (ncurrent >= HPSA_MAX_DEVICES)
3584edd16368SStephen M. Cameron 			break;
3585edd16368SStephen M. Cameron 	}
3586edd16368SStephen M. Cameron 	adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
3587edd16368SStephen M. Cameron out:
3588edd16368SStephen M. Cameron 	kfree(tmpdevice);
3589edd16368SStephen M. Cameron 	for (i = 0; i < ndev_allocated; i++)
3590edd16368SStephen M. Cameron 		kfree(currentsd[i]);
3591edd16368SStephen M. Cameron 	kfree(currentsd);
3592edd16368SStephen M. Cameron 	kfree(physdev_list);
3593edd16368SStephen M. Cameron 	kfree(logdev_list);
359403383736SDon Brace 	kfree(id_phys);
3595edd16368SStephen M. Cameron }
3596edd16368SStephen M. Cameron 
3597ec5cbf04SWebb Scales static void hpsa_set_sg_descriptor(struct SGDescriptor *desc,
3598ec5cbf04SWebb Scales 				   struct scatterlist *sg)
3599ec5cbf04SWebb Scales {
3600ec5cbf04SWebb Scales 	u64 addr64 = (u64) sg_dma_address(sg);
3601ec5cbf04SWebb Scales 	unsigned int len = sg_dma_len(sg);
3602ec5cbf04SWebb Scales 
3603ec5cbf04SWebb Scales 	desc->Addr = cpu_to_le64(addr64);
3604ec5cbf04SWebb Scales 	desc->Len = cpu_to_le32(len);
3605ec5cbf04SWebb Scales 	desc->Ext = 0;
3606ec5cbf04SWebb Scales }
3607ec5cbf04SWebb Scales 
3608c7ee65b3SWebb Scales /*
3609c7ee65b3SWebb Scales  * hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
3610edd16368SStephen M. Cameron  * dma mapping  and fills in the scatter gather entries of the
3611edd16368SStephen M. Cameron  * hpsa command, cp.
3612edd16368SStephen M. Cameron  */
361333a2ffceSStephen M. Cameron static int hpsa_scatter_gather(struct ctlr_info *h,
3614edd16368SStephen M. Cameron 		struct CommandList *cp,
3615edd16368SStephen M. Cameron 		struct scsi_cmnd *cmd)
3616edd16368SStephen M. Cameron {
3617edd16368SStephen M. Cameron 	struct scatterlist *sg;
361833a2ffceSStephen M. Cameron 	int use_sg, i, sg_index, chained;
361933a2ffceSStephen M. Cameron 	struct SGDescriptor *curr_sg;
3620edd16368SStephen M. Cameron 
362133a2ffceSStephen M. Cameron 	BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
3622edd16368SStephen M. Cameron 
3623edd16368SStephen M. Cameron 	use_sg = scsi_dma_map(cmd);
3624edd16368SStephen M. Cameron 	if (use_sg < 0)
3625edd16368SStephen M. Cameron 		return use_sg;
3626edd16368SStephen M. Cameron 
3627edd16368SStephen M. Cameron 	if (!use_sg)
3628edd16368SStephen M. Cameron 		goto sglist_finished;
3629edd16368SStephen M. Cameron 
363033a2ffceSStephen M. Cameron 	curr_sg = cp->SG;
363133a2ffceSStephen M. Cameron 	chained = 0;
363233a2ffceSStephen M. Cameron 	sg_index = 0;
3633edd16368SStephen M. Cameron 	scsi_for_each_sg(cmd, sg, use_sg, i) {
363433a2ffceSStephen M. Cameron 		if (i == h->max_cmd_sg_entries - 1 &&
363533a2ffceSStephen M. Cameron 			use_sg > h->max_cmd_sg_entries) {
363633a2ffceSStephen M. Cameron 			chained = 1;
363733a2ffceSStephen M. Cameron 			curr_sg = h->cmd_sg_list[cp->cmdindex];
363833a2ffceSStephen M. Cameron 			sg_index = 0;
363933a2ffceSStephen M. Cameron 		}
3640ec5cbf04SWebb Scales 		hpsa_set_sg_descriptor(curr_sg, sg);
364133a2ffceSStephen M. Cameron 		curr_sg++;
364233a2ffceSStephen M. Cameron 	}
3643ec5cbf04SWebb Scales 
3644ec5cbf04SWebb Scales 	/* Back the pointer up to the last entry and mark it as "last". */
364550a0decfSStephen M. Cameron 	(--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
364633a2ffceSStephen M. Cameron 
364733a2ffceSStephen M. Cameron 	if (use_sg + chained > h->maxSG)
364833a2ffceSStephen M. Cameron 		h->maxSG = use_sg + chained;
364933a2ffceSStephen M. Cameron 
365033a2ffceSStephen M. Cameron 	if (chained) {
365133a2ffceSStephen M. Cameron 		cp->Header.SGList = h->max_cmd_sg_entries;
365250a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(use_sg + 1);
3653e2bea6dfSStephen M. Cameron 		if (hpsa_map_sg_chain_block(h, cp)) {
3654e2bea6dfSStephen M. Cameron 			scsi_dma_unmap(cmd);
3655e2bea6dfSStephen M. Cameron 			return -1;
3656e2bea6dfSStephen M. Cameron 		}
365733a2ffceSStephen M. Cameron 		return 0;
3658edd16368SStephen M. Cameron 	}
3659edd16368SStephen M. Cameron 
3660edd16368SStephen M. Cameron sglist_finished:
3661edd16368SStephen M. Cameron 
366201a02ffcSStephen M. Cameron 	cp->Header.SGList = (u8) use_sg;   /* no. SGs contig in this cmd */
3663c7ee65b3SWebb Scales 	cp->Header.SGTotal = cpu_to_le16(use_sg); /* total sgs in cmd list */
3664edd16368SStephen M. Cameron 	return 0;
3665edd16368SStephen M. Cameron }
3666edd16368SStephen M. Cameron 
3667283b4a9bSStephen M. Cameron #define IO_ACCEL_INELIGIBLE (1)
3668283b4a9bSStephen M. Cameron static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
3669283b4a9bSStephen M. Cameron {
3670283b4a9bSStephen M. Cameron 	int is_write = 0;
3671283b4a9bSStephen M. Cameron 	u32 block;
3672283b4a9bSStephen M. Cameron 	u32 block_cnt;
3673283b4a9bSStephen M. Cameron 
3674283b4a9bSStephen M. Cameron 	/* Perform some CDB fixups if needed using 10 byte reads/writes only */
3675283b4a9bSStephen M. Cameron 	switch (cdb[0]) {
3676283b4a9bSStephen M. Cameron 	case WRITE_6:
3677283b4a9bSStephen M. Cameron 	case WRITE_12:
3678283b4a9bSStephen M. Cameron 		is_write = 1;
3679283b4a9bSStephen M. Cameron 	case READ_6:
3680283b4a9bSStephen M. Cameron 	case READ_12:
3681283b4a9bSStephen M. Cameron 		if (*cdb_len == 6) {
3682283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 8) | cdb[3];
3683283b4a9bSStephen M. Cameron 			block_cnt = cdb[4];
3684283b4a9bSStephen M. Cameron 		} else {
3685283b4a9bSStephen M. Cameron 			BUG_ON(*cdb_len != 12);
3686283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 24) |
3687283b4a9bSStephen M. Cameron 				(((u32) cdb[3]) << 16) |
3688283b4a9bSStephen M. Cameron 				(((u32) cdb[4]) << 8) |
3689283b4a9bSStephen M. Cameron 				cdb[5];
3690283b4a9bSStephen M. Cameron 			block_cnt =
3691283b4a9bSStephen M. Cameron 				(((u32) cdb[6]) << 24) |
3692283b4a9bSStephen M. Cameron 				(((u32) cdb[7]) << 16) |
3693283b4a9bSStephen M. Cameron 				(((u32) cdb[8]) << 8) |
3694283b4a9bSStephen M. Cameron 				cdb[9];
3695283b4a9bSStephen M. Cameron 		}
3696283b4a9bSStephen M. Cameron 		if (block_cnt > 0xffff)
3697283b4a9bSStephen M. Cameron 			return IO_ACCEL_INELIGIBLE;
3698283b4a9bSStephen M. Cameron 
3699283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
3700283b4a9bSStephen M. Cameron 		cdb[1] = 0;
3701283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (block >> 24);
3702283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (block >> 16);
3703283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (block >> 8);
3704283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (block);
3705283b4a9bSStephen M. Cameron 		cdb[6] = 0;
3706283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (block_cnt >> 8);
3707283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (block_cnt);
3708283b4a9bSStephen M. Cameron 		cdb[9] = 0;
3709283b4a9bSStephen M. Cameron 		*cdb_len = 10;
3710283b4a9bSStephen M. Cameron 		break;
3711283b4a9bSStephen M. Cameron 	}
3712283b4a9bSStephen M. Cameron 	return 0;
3713283b4a9bSStephen M. Cameron }
3714283b4a9bSStephen M. Cameron 
3715c349775eSScott Teel static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
3716283b4a9bSStephen M. Cameron 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
371703383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
3718e1f7de0cSMatt Gates {
3719e1f7de0cSMatt Gates 	struct scsi_cmnd *cmd = c->scsi_cmd;
3720e1f7de0cSMatt Gates 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
3721e1f7de0cSMatt Gates 	unsigned int len;
3722e1f7de0cSMatt Gates 	unsigned int total_len = 0;
3723e1f7de0cSMatt Gates 	struct scatterlist *sg;
3724e1f7de0cSMatt Gates 	u64 addr64;
3725e1f7de0cSMatt Gates 	int use_sg, i;
3726e1f7de0cSMatt Gates 	struct SGDescriptor *curr_sg;
3727e1f7de0cSMatt Gates 	u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
3728e1f7de0cSMatt Gates 
3729283b4a9bSStephen M. Cameron 	/* TODO: implement chaining support */
373003383736SDon Brace 	if (scsi_sg_count(cmd) > h->ioaccel_maxsg) {
373103383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3732283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
373303383736SDon Brace 	}
3734283b4a9bSStephen M. Cameron 
3735e1f7de0cSMatt Gates 	BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
3736e1f7de0cSMatt Gates 
373703383736SDon Brace 	if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
373803383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3739283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
374003383736SDon Brace 	}
3741283b4a9bSStephen M. Cameron 
3742e1f7de0cSMatt Gates 	c->cmd_type = CMD_IOACCEL1;
3743e1f7de0cSMatt Gates 
3744e1f7de0cSMatt Gates 	/* Adjust the DMA address to point to the accelerated command buffer */
3745e1f7de0cSMatt Gates 	c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
3746e1f7de0cSMatt Gates 				(c->cmdindex * sizeof(*cp));
3747e1f7de0cSMatt Gates 	BUG_ON(c->busaddr & 0x0000007F);
3748e1f7de0cSMatt Gates 
3749e1f7de0cSMatt Gates 	use_sg = scsi_dma_map(cmd);
375003383736SDon Brace 	if (use_sg < 0) {
375103383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3752e1f7de0cSMatt Gates 		return use_sg;
375303383736SDon Brace 	}
3754e1f7de0cSMatt Gates 
3755e1f7de0cSMatt Gates 	if (use_sg) {
3756e1f7de0cSMatt Gates 		curr_sg = cp->SG;
3757e1f7de0cSMatt Gates 		scsi_for_each_sg(cmd, sg, use_sg, i) {
3758e1f7de0cSMatt Gates 			addr64 = (u64) sg_dma_address(sg);
3759e1f7de0cSMatt Gates 			len  = sg_dma_len(sg);
3760e1f7de0cSMatt Gates 			total_len += len;
376150a0decfSStephen M. Cameron 			curr_sg->Addr = cpu_to_le64(addr64);
376250a0decfSStephen M. Cameron 			curr_sg->Len = cpu_to_le32(len);
376350a0decfSStephen M. Cameron 			curr_sg->Ext = cpu_to_le32(0);
3764e1f7de0cSMatt Gates 			curr_sg++;
3765e1f7de0cSMatt Gates 		}
376650a0decfSStephen M. Cameron 		(--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
3767e1f7de0cSMatt Gates 
3768e1f7de0cSMatt Gates 		switch (cmd->sc_data_direction) {
3769e1f7de0cSMatt Gates 		case DMA_TO_DEVICE:
3770e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_OUT;
3771e1f7de0cSMatt Gates 			break;
3772e1f7de0cSMatt Gates 		case DMA_FROM_DEVICE:
3773e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_IN;
3774e1f7de0cSMatt Gates 			break;
3775e1f7de0cSMatt Gates 		case DMA_NONE:
3776e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_NODATAXFER;
3777e1f7de0cSMatt Gates 			break;
3778e1f7de0cSMatt Gates 		default:
3779e1f7de0cSMatt Gates 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3780e1f7de0cSMatt Gates 			cmd->sc_data_direction);
3781e1f7de0cSMatt Gates 			BUG();
3782e1f7de0cSMatt Gates 			break;
3783e1f7de0cSMatt Gates 		}
3784e1f7de0cSMatt Gates 	} else {
3785e1f7de0cSMatt Gates 		control |= IOACCEL1_CONTROL_NODATAXFER;
3786e1f7de0cSMatt Gates 	}
3787e1f7de0cSMatt Gates 
3788c349775eSScott Teel 	c->Header.SGList = use_sg;
3789e1f7de0cSMatt Gates 	/* Fill out the command structure to submit */
37902b08b3e9SDon Brace 	cp->dev_handle = cpu_to_le16(ioaccel_handle & 0xFFFF);
37912b08b3e9SDon Brace 	cp->transfer_len = cpu_to_le32(total_len);
37922b08b3e9SDon Brace 	cp->io_flags = cpu_to_le16(IOACCEL1_IOFLAGS_IO_REQ |
37932b08b3e9SDon Brace 			(cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK));
37942b08b3e9SDon Brace 	cp->control = cpu_to_le32(control);
3795283b4a9bSStephen M. Cameron 	memcpy(cp->CDB, cdb, cdb_len);
3796283b4a9bSStephen M. Cameron 	memcpy(cp->CISS_LUN, scsi3addr, 8);
3797c349775eSScott Teel 	/* Tag was already set at init time. */
3798e1f7de0cSMatt Gates 	enqueue_cmd_and_start_io(h, c);
3799e1f7de0cSMatt Gates 	return 0;
3800e1f7de0cSMatt Gates }
3801edd16368SStephen M. Cameron 
3802283b4a9bSStephen M. Cameron /*
3803283b4a9bSStephen M. Cameron  * Queue a command directly to a device behind the controller using the
3804283b4a9bSStephen M. Cameron  * I/O accelerator path.
3805283b4a9bSStephen M. Cameron  */
3806283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
3807283b4a9bSStephen M. Cameron 	struct CommandList *c)
3808283b4a9bSStephen M. Cameron {
3809283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
3810283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3811283b4a9bSStephen M. Cameron 
381203383736SDon Brace 	c->phys_disk = dev;
381303383736SDon Brace 
3814283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
381503383736SDon Brace 		cmd->cmnd, cmd->cmd_len, dev->scsi3addr, dev);
3816283b4a9bSStephen M. Cameron }
3817283b4a9bSStephen M. Cameron 
3818dd0e19f3SScott Teel /*
3819dd0e19f3SScott Teel  * Set encryption parameters for the ioaccel2 request
3820dd0e19f3SScott Teel  */
3821dd0e19f3SScott Teel static void set_encrypt_ioaccel2(struct ctlr_info *h,
3822dd0e19f3SScott Teel 	struct CommandList *c, struct io_accel2_cmd *cp)
3823dd0e19f3SScott Teel {
3824dd0e19f3SScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
3825dd0e19f3SScott Teel 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3826dd0e19f3SScott Teel 	struct raid_map_data *map = &dev->raid_map;
3827dd0e19f3SScott Teel 	u64 first_block;
3828dd0e19f3SScott Teel 
3829dd0e19f3SScott Teel 	/* Are we doing encryption on this device */
38302b08b3e9SDon Brace 	if (!(le16_to_cpu(map->flags) & RAID_MAP_FLAG_ENCRYPT_ON))
3831dd0e19f3SScott Teel 		return;
3832dd0e19f3SScott Teel 	/* Set the data encryption key index. */
3833dd0e19f3SScott Teel 	cp->dekindex = map->dekindex;
3834dd0e19f3SScott Teel 
3835dd0e19f3SScott Teel 	/* Set the encryption enable flag, encoded into direction field. */
3836dd0e19f3SScott Teel 	cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
3837dd0e19f3SScott Teel 
3838dd0e19f3SScott Teel 	/* Set encryption tweak values based on logical block address
3839dd0e19f3SScott Teel 	 * If block size is 512, tweak value is LBA.
3840dd0e19f3SScott Teel 	 * For other block sizes, tweak is (LBA * block size)/ 512)
3841dd0e19f3SScott Teel 	 */
3842dd0e19f3SScott Teel 	switch (cmd->cmnd[0]) {
3843dd0e19f3SScott Teel 	/* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
3844dd0e19f3SScott Teel 	case WRITE_6:
3845dd0e19f3SScott Teel 	case READ_6:
38462b08b3e9SDon Brace 		first_block = get_unaligned_be16(&cmd->cmnd[2]);
3847dd0e19f3SScott Teel 		break;
3848dd0e19f3SScott Teel 	case WRITE_10:
3849dd0e19f3SScott Teel 	case READ_10:
3850dd0e19f3SScott Teel 	/* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
3851dd0e19f3SScott Teel 	case WRITE_12:
3852dd0e19f3SScott Teel 	case READ_12:
38532b08b3e9SDon Brace 		first_block = get_unaligned_be32(&cmd->cmnd[2]);
3854dd0e19f3SScott Teel 		break;
3855dd0e19f3SScott Teel 	case WRITE_16:
3856dd0e19f3SScott Teel 	case READ_16:
38572b08b3e9SDon Brace 		first_block = get_unaligned_be64(&cmd->cmnd[2]);
3858dd0e19f3SScott Teel 		break;
3859dd0e19f3SScott Teel 	default:
3860dd0e19f3SScott Teel 		dev_err(&h->pdev->dev,
38612b08b3e9SDon Brace 			"ERROR: %s: size (0x%x) not supported for encryption\n",
38622b08b3e9SDon Brace 			__func__, cmd->cmnd[0]);
3863dd0e19f3SScott Teel 		BUG();
3864dd0e19f3SScott Teel 		break;
3865dd0e19f3SScott Teel 	}
38662b08b3e9SDon Brace 
38672b08b3e9SDon Brace 	if (le32_to_cpu(map->volume_blk_size) != 512)
38682b08b3e9SDon Brace 		first_block = first_block *
38692b08b3e9SDon Brace 				le32_to_cpu(map->volume_blk_size)/512;
38702b08b3e9SDon Brace 
38712b08b3e9SDon Brace 	cp->tweak_lower = cpu_to_le32(first_block);
38722b08b3e9SDon Brace 	cp->tweak_upper = cpu_to_le32(first_block >> 32);
3873dd0e19f3SScott Teel }
3874dd0e19f3SScott Teel 
3875c349775eSScott Teel static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
3876c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
387703383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
3878c349775eSScott Teel {
3879c349775eSScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
3880c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
3881c349775eSScott Teel 	struct ioaccel2_sg_element *curr_sg;
3882c349775eSScott Teel 	int use_sg, i;
3883c349775eSScott Teel 	struct scatterlist *sg;
3884c349775eSScott Teel 	u64 addr64;
3885c349775eSScott Teel 	u32 len;
3886c349775eSScott Teel 	u32 total_len = 0;
3887c349775eSScott Teel 
3888d9a729f3SWebb Scales 	BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
3889c349775eSScott Teel 
389003383736SDon Brace 	if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
389103383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3892c349775eSScott Teel 		return IO_ACCEL_INELIGIBLE;
389303383736SDon Brace 	}
389403383736SDon Brace 
3895c349775eSScott Teel 	c->cmd_type = CMD_IOACCEL2;
3896c349775eSScott Teel 	/* Adjust the DMA address to point to the accelerated command buffer */
3897c349775eSScott Teel 	c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
3898c349775eSScott Teel 				(c->cmdindex * sizeof(*cp));
3899c349775eSScott Teel 	BUG_ON(c->busaddr & 0x0000007F);
3900c349775eSScott Teel 
3901c349775eSScott Teel 	memset(cp, 0, sizeof(*cp));
3902c349775eSScott Teel 	cp->IU_type = IOACCEL2_IU_TYPE;
3903c349775eSScott Teel 
3904c349775eSScott Teel 	use_sg = scsi_dma_map(cmd);
390503383736SDon Brace 	if (use_sg < 0) {
390603383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3907c349775eSScott Teel 		return use_sg;
390803383736SDon Brace 	}
3909c349775eSScott Teel 
3910c349775eSScott Teel 	if (use_sg) {
3911c349775eSScott Teel 		curr_sg = cp->sg;
3912d9a729f3SWebb Scales 		if (use_sg > h->ioaccel_maxsg) {
3913d9a729f3SWebb Scales 			addr64 = le64_to_cpu(
3914d9a729f3SWebb Scales 				h->ioaccel2_cmd_sg_list[c->cmdindex]->address);
3915d9a729f3SWebb Scales 			curr_sg->address = cpu_to_le64(addr64);
3916d9a729f3SWebb Scales 			curr_sg->length = 0;
3917d9a729f3SWebb Scales 			curr_sg->reserved[0] = 0;
3918d9a729f3SWebb Scales 			curr_sg->reserved[1] = 0;
3919d9a729f3SWebb Scales 			curr_sg->reserved[2] = 0;
3920d9a729f3SWebb Scales 			curr_sg->chain_indicator = 0x80;
3921d9a729f3SWebb Scales 
3922d9a729f3SWebb Scales 			curr_sg = h->ioaccel2_cmd_sg_list[c->cmdindex];
3923d9a729f3SWebb Scales 		}
3924c349775eSScott Teel 		scsi_for_each_sg(cmd, sg, use_sg, i) {
3925c349775eSScott Teel 			addr64 = (u64) sg_dma_address(sg);
3926c349775eSScott Teel 			len  = sg_dma_len(sg);
3927c349775eSScott Teel 			total_len += len;
3928c349775eSScott Teel 			curr_sg->address = cpu_to_le64(addr64);
3929c349775eSScott Teel 			curr_sg->length = cpu_to_le32(len);
3930c349775eSScott Teel 			curr_sg->reserved[0] = 0;
3931c349775eSScott Teel 			curr_sg->reserved[1] = 0;
3932c349775eSScott Teel 			curr_sg->reserved[2] = 0;
3933c349775eSScott Teel 			curr_sg->chain_indicator = 0;
3934c349775eSScott Teel 			curr_sg++;
3935c349775eSScott Teel 		}
3936c349775eSScott Teel 
3937c349775eSScott Teel 		switch (cmd->sc_data_direction) {
3938c349775eSScott Teel 		case DMA_TO_DEVICE:
3939dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3940dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_OUT;
3941c349775eSScott Teel 			break;
3942c349775eSScott Teel 		case DMA_FROM_DEVICE:
3943dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3944dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_IN;
3945c349775eSScott Teel 			break;
3946c349775eSScott Teel 		case DMA_NONE:
3947dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3948dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_NO_DATA;
3949c349775eSScott Teel 			break;
3950c349775eSScott Teel 		default:
3951c349775eSScott Teel 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3952c349775eSScott Teel 				cmd->sc_data_direction);
3953c349775eSScott Teel 			BUG();
3954c349775eSScott Teel 			break;
3955c349775eSScott Teel 		}
3956c349775eSScott Teel 	} else {
3957dd0e19f3SScott Teel 		cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3958dd0e19f3SScott Teel 		cp->direction |= IOACCEL2_DIR_NO_DATA;
3959c349775eSScott Teel 	}
3960dd0e19f3SScott Teel 
3961dd0e19f3SScott Teel 	/* Set encryption parameters, if necessary */
3962dd0e19f3SScott Teel 	set_encrypt_ioaccel2(h, c, cp);
3963dd0e19f3SScott Teel 
39642b08b3e9SDon Brace 	cp->scsi_nexus = cpu_to_le32(ioaccel_handle);
3965f2405db8SDon Brace 	cp->Tag = cpu_to_le32(c->cmdindex << DIRECT_LOOKUP_SHIFT);
3966c349775eSScott Teel 	memcpy(cp->cdb, cdb, sizeof(cp->cdb));
3967c349775eSScott Teel 
3968c349775eSScott Teel 	cp->data_len = cpu_to_le32(total_len);
3969c349775eSScott Teel 	cp->err_ptr = cpu_to_le64(c->busaddr +
3970c349775eSScott Teel 			offsetof(struct io_accel2_cmd, error_data));
397150a0decfSStephen M. Cameron 	cp->err_len = cpu_to_le32(sizeof(cp->error_data));
3972c349775eSScott Teel 
3973d9a729f3SWebb Scales 	/* fill in sg elements */
3974d9a729f3SWebb Scales 	if (use_sg > h->ioaccel_maxsg) {
3975d9a729f3SWebb Scales 		cp->sg_count = 1;
3976d9a729f3SWebb Scales 		if (hpsa_map_ioaccel2_sg_chain_block(h, cp, c)) {
3977d9a729f3SWebb Scales 			atomic_dec(&phys_disk->ioaccel_cmds_out);
3978d9a729f3SWebb Scales 			scsi_dma_unmap(cmd);
3979d9a729f3SWebb Scales 			return -1;
3980d9a729f3SWebb Scales 		}
3981d9a729f3SWebb Scales 	} else
3982d9a729f3SWebb Scales 		cp->sg_count = (u8) use_sg;
3983d9a729f3SWebb Scales 
3984c349775eSScott Teel 	enqueue_cmd_and_start_io(h, c);
3985c349775eSScott Teel 	return 0;
3986c349775eSScott Teel }
3987c349775eSScott Teel 
3988c349775eSScott Teel /*
3989c349775eSScott Teel  * Queue a command to the correct I/O accelerator path.
3990c349775eSScott Teel  */
3991c349775eSScott Teel static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
3992c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
399303383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
3994c349775eSScott Teel {
399503383736SDon Brace 	/* Try to honor the device's queue depth */
399603383736SDon Brace 	if (atomic_inc_return(&phys_disk->ioaccel_cmds_out) >
399703383736SDon Brace 					phys_disk->queue_depth) {
399803383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
399903383736SDon Brace 		return IO_ACCEL_INELIGIBLE;
400003383736SDon Brace 	}
4001c349775eSScott Teel 	if (h->transMethod & CFGTBL_Trans_io_accel1)
4002c349775eSScott Teel 		return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
400303383736SDon Brace 						cdb, cdb_len, scsi3addr,
400403383736SDon Brace 						phys_disk);
4005c349775eSScott Teel 	else
4006c349775eSScott Teel 		return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
400703383736SDon Brace 						cdb, cdb_len, scsi3addr,
400803383736SDon Brace 						phys_disk);
4009c349775eSScott Teel }
4010c349775eSScott Teel 
40116b80b18fSScott Teel static void raid_map_helper(struct raid_map_data *map,
40126b80b18fSScott Teel 		int offload_to_mirror, u32 *map_index, u32 *current_group)
40136b80b18fSScott Teel {
40146b80b18fSScott Teel 	if (offload_to_mirror == 0)  {
40156b80b18fSScott Teel 		/* use physical disk in the first mirrored group. */
40162b08b3e9SDon Brace 		*map_index %= le16_to_cpu(map->data_disks_per_row);
40176b80b18fSScott Teel 		return;
40186b80b18fSScott Teel 	}
40196b80b18fSScott Teel 	do {
40206b80b18fSScott Teel 		/* determine mirror group that *map_index indicates */
40212b08b3e9SDon Brace 		*current_group = *map_index /
40222b08b3e9SDon Brace 			le16_to_cpu(map->data_disks_per_row);
40236b80b18fSScott Teel 		if (offload_to_mirror == *current_group)
40246b80b18fSScott Teel 			continue;
40252b08b3e9SDon Brace 		if (*current_group < le16_to_cpu(map->layout_map_count) - 1) {
40266b80b18fSScott Teel 			/* select map index from next group */
40272b08b3e9SDon Brace 			*map_index += le16_to_cpu(map->data_disks_per_row);
40286b80b18fSScott Teel 			(*current_group)++;
40296b80b18fSScott Teel 		} else {
40306b80b18fSScott Teel 			/* select map index from first group */
40312b08b3e9SDon Brace 			*map_index %= le16_to_cpu(map->data_disks_per_row);
40326b80b18fSScott Teel 			*current_group = 0;
40336b80b18fSScott Teel 		}
40346b80b18fSScott Teel 	} while (offload_to_mirror != *current_group);
40356b80b18fSScott Teel }
40366b80b18fSScott Teel 
4037283b4a9bSStephen M. Cameron /*
4038283b4a9bSStephen M. Cameron  * Attempt to perform offload RAID mapping for a logical volume I/O.
4039283b4a9bSStephen M. Cameron  */
4040283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
4041283b4a9bSStephen M. Cameron 	struct CommandList *c)
4042283b4a9bSStephen M. Cameron {
4043283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
4044283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4045283b4a9bSStephen M. Cameron 	struct raid_map_data *map = &dev->raid_map;
4046283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map->data[0];
4047283b4a9bSStephen M. Cameron 	int is_write = 0;
4048283b4a9bSStephen M. Cameron 	u32 map_index;
4049283b4a9bSStephen M. Cameron 	u64 first_block, last_block;
4050283b4a9bSStephen M. Cameron 	u32 block_cnt;
4051283b4a9bSStephen M. Cameron 	u32 blocks_per_row;
4052283b4a9bSStephen M. Cameron 	u64 first_row, last_row;
4053283b4a9bSStephen M. Cameron 	u32 first_row_offset, last_row_offset;
4054283b4a9bSStephen M. Cameron 	u32 first_column, last_column;
40556b80b18fSScott Teel 	u64 r0_first_row, r0_last_row;
40566b80b18fSScott Teel 	u32 r5or6_blocks_per_row;
40576b80b18fSScott Teel 	u64 r5or6_first_row, r5or6_last_row;
40586b80b18fSScott Teel 	u32 r5or6_first_row_offset, r5or6_last_row_offset;
40596b80b18fSScott Teel 	u32 r5or6_first_column, r5or6_last_column;
40606b80b18fSScott Teel 	u32 total_disks_per_row;
40616b80b18fSScott Teel 	u32 stripesize;
40626b80b18fSScott Teel 	u32 first_group, last_group, current_group;
4063283b4a9bSStephen M. Cameron 	u32 map_row;
4064283b4a9bSStephen M. Cameron 	u32 disk_handle;
4065283b4a9bSStephen M. Cameron 	u64 disk_block;
4066283b4a9bSStephen M. Cameron 	u32 disk_block_cnt;
4067283b4a9bSStephen M. Cameron 	u8 cdb[16];
4068283b4a9bSStephen M. Cameron 	u8 cdb_len;
40692b08b3e9SDon Brace 	u16 strip_size;
4070283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
4071283b4a9bSStephen M. Cameron 	u64 tmpdiv;
4072283b4a9bSStephen M. Cameron #endif
40736b80b18fSScott Teel 	int offload_to_mirror;
4074283b4a9bSStephen M. Cameron 
4075283b4a9bSStephen M. Cameron 	/* check for valid opcode, get LBA and block count */
4076283b4a9bSStephen M. Cameron 	switch (cmd->cmnd[0]) {
4077283b4a9bSStephen M. Cameron 	case WRITE_6:
4078283b4a9bSStephen M. Cameron 		is_write = 1;
4079283b4a9bSStephen M. Cameron 	case READ_6:
4080283b4a9bSStephen M. Cameron 		first_block =
4081283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 8) |
4082283b4a9bSStephen M. Cameron 			cmd->cmnd[3];
4083283b4a9bSStephen M. Cameron 		block_cnt = cmd->cmnd[4];
40843fa89a04SStephen M. Cameron 		if (block_cnt == 0)
40853fa89a04SStephen M. Cameron 			block_cnt = 256;
4086283b4a9bSStephen M. Cameron 		break;
4087283b4a9bSStephen M. Cameron 	case WRITE_10:
4088283b4a9bSStephen M. Cameron 		is_write = 1;
4089283b4a9bSStephen M. Cameron 	case READ_10:
4090283b4a9bSStephen M. Cameron 		first_block =
4091283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
4092283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
4093283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
4094283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
4095283b4a9bSStephen M. Cameron 		block_cnt =
4096283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 8) |
4097283b4a9bSStephen M. Cameron 			cmd->cmnd[8];
4098283b4a9bSStephen M. Cameron 		break;
4099283b4a9bSStephen M. Cameron 	case WRITE_12:
4100283b4a9bSStephen M. Cameron 		is_write = 1;
4101283b4a9bSStephen M. Cameron 	case READ_12:
4102283b4a9bSStephen M. Cameron 		first_block =
4103283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
4104283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
4105283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
4106283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
4107283b4a9bSStephen M. Cameron 		block_cnt =
4108283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[6]) << 24) |
4109283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 16) |
4110283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[8]) << 8) |
4111283b4a9bSStephen M. Cameron 		cmd->cmnd[9];
4112283b4a9bSStephen M. Cameron 		break;
4113283b4a9bSStephen M. Cameron 	case WRITE_16:
4114283b4a9bSStephen M. Cameron 		is_write = 1;
4115283b4a9bSStephen M. Cameron 	case READ_16:
4116283b4a9bSStephen M. Cameron 		first_block =
4117283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 56) |
4118283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 48) |
4119283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 40) |
4120283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[5]) << 32) |
4121283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[6]) << 24) |
4122283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[7]) << 16) |
4123283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[8]) << 8) |
4124283b4a9bSStephen M. Cameron 			cmd->cmnd[9];
4125283b4a9bSStephen M. Cameron 		block_cnt =
4126283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[10]) << 24) |
4127283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[11]) << 16) |
4128283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[12]) << 8) |
4129283b4a9bSStephen M. Cameron 			cmd->cmnd[13];
4130283b4a9bSStephen M. Cameron 		break;
4131283b4a9bSStephen M. Cameron 	default:
4132283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
4133283b4a9bSStephen M. Cameron 	}
4134283b4a9bSStephen M. Cameron 	last_block = first_block + block_cnt - 1;
4135283b4a9bSStephen M. Cameron 
4136283b4a9bSStephen M. Cameron 	/* check for write to non-RAID-0 */
4137283b4a9bSStephen M. Cameron 	if (is_write && dev->raid_level != 0)
4138283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
4139283b4a9bSStephen M. Cameron 
4140283b4a9bSStephen M. Cameron 	/* check for invalid block or wraparound */
41412b08b3e9SDon Brace 	if (last_block >= le64_to_cpu(map->volume_blk_cnt) ||
41422b08b3e9SDon Brace 		last_block < first_block)
4143283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
4144283b4a9bSStephen M. Cameron 
4145283b4a9bSStephen M. Cameron 	/* calculate stripe information for the request */
41462b08b3e9SDon Brace 	blocks_per_row = le16_to_cpu(map->data_disks_per_row) *
41472b08b3e9SDon Brace 				le16_to_cpu(map->strip_size);
41482b08b3e9SDon Brace 	strip_size = le16_to_cpu(map->strip_size);
4149283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
4150283b4a9bSStephen M. Cameron 	tmpdiv = first_block;
4151283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
4152283b4a9bSStephen M. Cameron 	first_row = tmpdiv;
4153283b4a9bSStephen M. Cameron 	tmpdiv = last_block;
4154283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
4155283b4a9bSStephen M. Cameron 	last_row = tmpdiv;
4156283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
4157283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
4158283b4a9bSStephen M. Cameron 	tmpdiv = first_row_offset;
41592b08b3e9SDon Brace 	(void) do_div(tmpdiv, strip_size);
4160283b4a9bSStephen M. Cameron 	first_column = tmpdiv;
4161283b4a9bSStephen M. Cameron 	tmpdiv = last_row_offset;
41622b08b3e9SDon Brace 	(void) do_div(tmpdiv, strip_size);
4163283b4a9bSStephen M. Cameron 	last_column = tmpdiv;
4164283b4a9bSStephen M. Cameron #else
4165283b4a9bSStephen M. Cameron 	first_row = first_block / blocks_per_row;
4166283b4a9bSStephen M. Cameron 	last_row = last_block / blocks_per_row;
4167283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
4168283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
41692b08b3e9SDon Brace 	first_column = first_row_offset / strip_size;
41702b08b3e9SDon Brace 	last_column = last_row_offset / strip_size;
4171283b4a9bSStephen M. Cameron #endif
4172283b4a9bSStephen M. Cameron 
4173283b4a9bSStephen M. Cameron 	/* if this isn't a single row/column then give to the controller */
4174283b4a9bSStephen M. Cameron 	if ((first_row != last_row) || (first_column != last_column))
4175283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
4176283b4a9bSStephen M. Cameron 
4177283b4a9bSStephen M. Cameron 	/* proceeding with driver mapping */
41782b08b3e9SDon Brace 	total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
41792b08b3e9SDon Brace 				le16_to_cpu(map->metadata_disks_per_row);
4180283b4a9bSStephen M. Cameron 	map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
41812b08b3e9SDon Brace 				le16_to_cpu(map->row_cnt);
41826b80b18fSScott Teel 	map_index = (map_row * total_disks_per_row) + first_column;
41836b80b18fSScott Teel 
41846b80b18fSScott Teel 	switch (dev->raid_level) {
41856b80b18fSScott Teel 	case HPSA_RAID_0:
41866b80b18fSScott Teel 		break; /* nothing special to do */
41876b80b18fSScott Teel 	case HPSA_RAID_1:
41886b80b18fSScott Teel 		/* Handles load balance across RAID 1 members.
41896b80b18fSScott Teel 		 * (2-drive R1 and R10 with even # of drives.)
41906b80b18fSScott Teel 		 * Appropriate for SSDs, not optimal for HDDs
4191283b4a9bSStephen M. Cameron 		 */
41922b08b3e9SDon Brace 		BUG_ON(le16_to_cpu(map->layout_map_count) != 2);
4193283b4a9bSStephen M. Cameron 		if (dev->offload_to_mirror)
41942b08b3e9SDon Brace 			map_index += le16_to_cpu(map->data_disks_per_row);
4195283b4a9bSStephen M. Cameron 		dev->offload_to_mirror = !dev->offload_to_mirror;
41966b80b18fSScott Teel 		break;
41976b80b18fSScott Teel 	case HPSA_RAID_ADM:
41986b80b18fSScott Teel 		/* Handles N-way mirrors  (R1-ADM)
41996b80b18fSScott Teel 		 * and R10 with # of drives divisible by 3.)
42006b80b18fSScott Teel 		 */
42012b08b3e9SDon Brace 		BUG_ON(le16_to_cpu(map->layout_map_count) != 3);
42026b80b18fSScott Teel 
42036b80b18fSScott Teel 		offload_to_mirror = dev->offload_to_mirror;
42046b80b18fSScott Teel 		raid_map_helper(map, offload_to_mirror,
42056b80b18fSScott Teel 				&map_index, &current_group);
42066b80b18fSScott Teel 		/* set mirror group to use next time */
42076b80b18fSScott Teel 		offload_to_mirror =
42082b08b3e9SDon Brace 			(offload_to_mirror >=
42092b08b3e9SDon Brace 			le16_to_cpu(map->layout_map_count) - 1)
42106b80b18fSScott Teel 			? 0 : offload_to_mirror + 1;
42116b80b18fSScott Teel 		dev->offload_to_mirror = offload_to_mirror;
42126b80b18fSScott Teel 		/* Avoid direct use of dev->offload_to_mirror within this
42136b80b18fSScott Teel 		 * function since multiple threads might simultaneously
42146b80b18fSScott Teel 		 * increment it beyond the range of dev->layout_map_count -1.
42156b80b18fSScott Teel 		 */
42166b80b18fSScott Teel 		break;
42176b80b18fSScott Teel 	case HPSA_RAID_5:
42186b80b18fSScott Teel 	case HPSA_RAID_6:
42192b08b3e9SDon Brace 		if (le16_to_cpu(map->layout_map_count) <= 1)
42206b80b18fSScott Teel 			break;
42216b80b18fSScott Teel 
42226b80b18fSScott Teel 		/* Verify first and last block are in same RAID group */
42236b80b18fSScott Teel 		r5or6_blocks_per_row =
42242b08b3e9SDon Brace 			le16_to_cpu(map->strip_size) *
42252b08b3e9SDon Brace 			le16_to_cpu(map->data_disks_per_row);
42266b80b18fSScott Teel 		BUG_ON(r5or6_blocks_per_row == 0);
42272b08b3e9SDon Brace 		stripesize = r5or6_blocks_per_row *
42282b08b3e9SDon Brace 			le16_to_cpu(map->layout_map_count);
42296b80b18fSScott Teel #if BITS_PER_LONG == 32
42306b80b18fSScott Teel 		tmpdiv = first_block;
42316b80b18fSScott Teel 		first_group = do_div(tmpdiv, stripesize);
42326b80b18fSScott Teel 		tmpdiv = first_group;
42336b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
42346b80b18fSScott Teel 		first_group = tmpdiv;
42356b80b18fSScott Teel 		tmpdiv = last_block;
42366b80b18fSScott Teel 		last_group = do_div(tmpdiv, stripesize);
42376b80b18fSScott Teel 		tmpdiv = last_group;
42386b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
42396b80b18fSScott Teel 		last_group = tmpdiv;
42406b80b18fSScott Teel #else
42416b80b18fSScott Teel 		first_group = (first_block % stripesize) / r5or6_blocks_per_row;
42426b80b18fSScott Teel 		last_group = (last_block % stripesize) / r5or6_blocks_per_row;
42436b80b18fSScott Teel #endif
4244000ff7c2SStephen M. Cameron 		if (first_group != last_group)
42456b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
42466b80b18fSScott Teel 
42476b80b18fSScott Teel 		/* Verify request is in a single row of RAID 5/6 */
42486b80b18fSScott Teel #if BITS_PER_LONG == 32
42496b80b18fSScott Teel 		tmpdiv = first_block;
42506b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
42516b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row = tmpdiv;
42526b80b18fSScott Teel 		tmpdiv = last_block;
42536b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
42546b80b18fSScott Teel 		r5or6_last_row = r0_last_row = tmpdiv;
42556b80b18fSScott Teel #else
42566b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row =
42576b80b18fSScott Teel 						first_block / stripesize;
42586b80b18fSScott Teel 		r5or6_last_row = r0_last_row = last_block / stripesize;
42596b80b18fSScott Teel #endif
42606b80b18fSScott Teel 		if (r5or6_first_row != r5or6_last_row)
42616b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
42626b80b18fSScott Teel 
42636b80b18fSScott Teel 
42646b80b18fSScott Teel 		/* Verify request is in a single column */
42656b80b18fSScott Teel #if BITS_PER_LONG == 32
42666b80b18fSScott Teel 		tmpdiv = first_block;
42676b80b18fSScott Teel 		first_row_offset = do_div(tmpdiv, stripesize);
42686b80b18fSScott Teel 		tmpdiv = first_row_offset;
42696b80b18fSScott Teel 		first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
42706b80b18fSScott Teel 		r5or6_first_row_offset = first_row_offset;
42716b80b18fSScott Teel 		tmpdiv = last_block;
42726b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, stripesize);
42736b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
42746b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
42756b80b18fSScott Teel 		tmpdiv = r5or6_first_row_offset;
42766b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
42776b80b18fSScott Teel 		first_column = r5or6_first_column = tmpdiv;
42786b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
42796b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
42806b80b18fSScott Teel 		r5or6_last_column = tmpdiv;
42816b80b18fSScott Teel #else
42826b80b18fSScott Teel 		first_row_offset = r5or6_first_row_offset =
42836b80b18fSScott Teel 			(u32)((first_block % stripesize) %
42846b80b18fSScott Teel 						r5or6_blocks_per_row);
42856b80b18fSScott Teel 
42866b80b18fSScott Teel 		r5or6_last_row_offset =
42876b80b18fSScott Teel 			(u32)((last_block % stripesize) %
42886b80b18fSScott Teel 						r5or6_blocks_per_row);
42896b80b18fSScott Teel 
42906b80b18fSScott Teel 		first_column = r5or6_first_column =
42912b08b3e9SDon Brace 			r5or6_first_row_offset / le16_to_cpu(map->strip_size);
42926b80b18fSScott Teel 		r5or6_last_column =
42932b08b3e9SDon Brace 			r5or6_last_row_offset / le16_to_cpu(map->strip_size);
42946b80b18fSScott Teel #endif
42956b80b18fSScott Teel 		if (r5or6_first_column != r5or6_last_column)
42966b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
42976b80b18fSScott Teel 
42986b80b18fSScott Teel 		/* Request is eligible */
42996b80b18fSScott Teel 		map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
43002b08b3e9SDon Brace 			le16_to_cpu(map->row_cnt);
43016b80b18fSScott Teel 
43026b80b18fSScott Teel 		map_index = (first_group *
43032b08b3e9SDon Brace 			(le16_to_cpu(map->row_cnt) * total_disks_per_row)) +
43046b80b18fSScott Teel 			(map_row * total_disks_per_row) + first_column;
43056b80b18fSScott Teel 		break;
43066b80b18fSScott Teel 	default:
43076b80b18fSScott Teel 		return IO_ACCEL_INELIGIBLE;
4308283b4a9bSStephen M. Cameron 	}
43096b80b18fSScott Teel 
431007543e0cSStephen Cameron 	if (unlikely(map_index >= RAID_MAP_MAX_ENTRIES))
431107543e0cSStephen Cameron 		return IO_ACCEL_INELIGIBLE;
431207543e0cSStephen Cameron 
431303383736SDon Brace 	c->phys_disk = dev->phys_disk[map_index];
431403383736SDon Brace 
4315283b4a9bSStephen M. Cameron 	disk_handle = dd[map_index].ioaccel_handle;
43162b08b3e9SDon Brace 	disk_block = le64_to_cpu(map->disk_starting_blk) +
43172b08b3e9SDon Brace 			first_row * le16_to_cpu(map->strip_size) +
43182b08b3e9SDon Brace 			(first_row_offset - first_column *
43192b08b3e9SDon Brace 			le16_to_cpu(map->strip_size));
4320283b4a9bSStephen M. Cameron 	disk_block_cnt = block_cnt;
4321283b4a9bSStephen M. Cameron 
4322283b4a9bSStephen M. Cameron 	/* handle differing logical/physical block sizes */
4323283b4a9bSStephen M. Cameron 	if (map->phys_blk_shift) {
4324283b4a9bSStephen M. Cameron 		disk_block <<= map->phys_blk_shift;
4325283b4a9bSStephen M. Cameron 		disk_block_cnt <<= map->phys_blk_shift;
4326283b4a9bSStephen M. Cameron 	}
4327283b4a9bSStephen M. Cameron 	BUG_ON(disk_block_cnt > 0xffff);
4328283b4a9bSStephen M. Cameron 
4329283b4a9bSStephen M. Cameron 	/* build the new CDB for the physical disk I/O */
4330283b4a9bSStephen M. Cameron 	if (disk_block > 0xffffffff) {
4331283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_16 : READ_16;
4332283b4a9bSStephen M. Cameron 		cdb[1] = 0;
4333283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 56);
4334283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 48);
4335283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 40);
4336283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block >> 32);
4337283b4a9bSStephen M. Cameron 		cdb[6] = (u8) (disk_block >> 24);
4338283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block >> 16);
4339283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block >> 8);
4340283b4a9bSStephen M. Cameron 		cdb[9] = (u8) (disk_block);
4341283b4a9bSStephen M. Cameron 		cdb[10] = (u8) (disk_block_cnt >> 24);
4342283b4a9bSStephen M. Cameron 		cdb[11] = (u8) (disk_block_cnt >> 16);
4343283b4a9bSStephen M. Cameron 		cdb[12] = (u8) (disk_block_cnt >> 8);
4344283b4a9bSStephen M. Cameron 		cdb[13] = (u8) (disk_block_cnt);
4345283b4a9bSStephen M. Cameron 		cdb[14] = 0;
4346283b4a9bSStephen M. Cameron 		cdb[15] = 0;
4347283b4a9bSStephen M. Cameron 		cdb_len = 16;
4348283b4a9bSStephen M. Cameron 	} else {
4349283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
4350283b4a9bSStephen M. Cameron 		cdb[1] = 0;
4351283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 24);
4352283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 16);
4353283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 8);
4354283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block);
4355283b4a9bSStephen M. Cameron 		cdb[6] = 0;
4356283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block_cnt >> 8);
4357283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block_cnt);
4358283b4a9bSStephen M. Cameron 		cdb[9] = 0;
4359283b4a9bSStephen M. Cameron 		cdb_len = 10;
4360283b4a9bSStephen M. Cameron 	}
4361283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
436203383736SDon Brace 						dev->scsi3addr,
436303383736SDon Brace 						dev->phys_disk[map_index]);
4364283b4a9bSStephen M. Cameron }
4365283b4a9bSStephen M. Cameron 
436625163bd5SWebb Scales /*
436725163bd5SWebb Scales  * Submit commands down the "normal" RAID stack path
436825163bd5SWebb Scales  * All callers to hpsa_ciss_submit must check lockup_detected
436925163bd5SWebb Scales  * beforehand, before (opt.) and after calling cmd_alloc
437025163bd5SWebb Scales  */
4371574f05d3SStephen Cameron static int hpsa_ciss_submit(struct ctlr_info *h,
4372574f05d3SStephen Cameron 	struct CommandList *c, struct scsi_cmnd *cmd,
4373574f05d3SStephen Cameron 	unsigned char scsi3addr[])
4374edd16368SStephen M. Cameron {
4375edd16368SStephen M. Cameron 	cmd->host_scribble = (unsigned char *) c;
4376edd16368SStephen M. Cameron 	c->cmd_type = CMD_SCSI;
4377edd16368SStephen M. Cameron 	c->scsi_cmd = cmd;
4378edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;  /* unused in simple mode */
4379edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
4380f2405db8SDon Brace 	c->Header.tag = cpu_to_le64((c->cmdindex << DIRECT_LOOKUP_SHIFT));
4381edd16368SStephen M. Cameron 
4382edd16368SStephen M. Cameron 	/* Fill in the request block... */
4383edd16368SStephen M. Cameron 
4384edd16368SStephen M. Cameron 	c->Request.Timeout = 0;
4385edd16368SStephen M. Cameron 	BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
4386edd16368SStephen M. Cameron 	c->Request.CDBLen = cmd->cmd_len;
4387edd16368SStephen M. Cameron 	memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
4388edd16368SStephen M. Cameron 	switch (cmd->sc_data_direction) {
4389edd16368SStephen M. Cameron 	case DMA_TO_DEVICE:
4390a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4391a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_WRITE);
4392edd16368SStephen M. Cameron 		break;
4393edd16368SStephen M. Cameron 	case DMA_FROM_DEVICE:
4394a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4395a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_READ);
4396edd16368SStephen M. Cameron 		break;
4397edd16368SStephen M. Cameron 	case DMA_NONE:
4398a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4399a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_NONE);
4400edd16368SStephen M. Cameron 		break;
4401edd16368SStephen M. Cameron 	case DMA_BIDIRECTIONAL:
4402edd16368SStephen M. Cameron 		/* This can happen if a buggy application does a scsi passthru
4403edd16368SStephen M. Cameron 		 * and sets both inlen and outlen to non-zero. ( see
4404edd16368SStephen M. Cameron 		 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
4405edd16368SStephen M. Cameron 		 */
4406edd16368SStephen M. Cameron 
4407a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4408a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_RSVD);
4409edd16368SStephen M. Cameron 		/* This is technically wrong, and hpsa controllers should
4410edd16368SStephen M. Cameron 		 * reject it with CMD_INVALID, which is the most correct
4411edd16368SStephen M. Cameron 		 * response, but non-fibre backends appear to let it
4412edd16368SStephen M. Cameron 		 * slide by, and give the same results as if this field
4413edd16368SStephen M. Cameron 		 * were set correctly.  Either way is acceptable for
4414edd16368SStephen M. Cameron 		 * our purposes here.
4415edd16368SStephen M. Cameron 		 */
4416edd16368SStephen M. Cameron 
4417edd16368SStephen M. Cameron 		break;
4418edd16368SStephen M. Cameron 
4419edd16368SStephen M. Cameron 	default:
4420edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4421edd16368SStephen M. Cameron 			cmd->sc_data_direction);
4422edd16368SStephen M. Cameron 		BUG();
4423edd16368SStephen M. Cameron 		break;
4424edd16368SStephen M. Cameron 	}
4425edd16368SStephen M. Cameron 
442633a2ffceSStephen M. Cameron 	if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
4427edd16368SStephen M. Cameron 		cmd_free(h, c);
4428edd16368SStephen M. Cameron 		return SCSI_MLQUEUE_HOST_BUSY;
4429edd16368SStephen M. Cameron 	}
4430edd16368SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
4431edd16368SStephen M. Cameron 	/* the cmd'll come back via intr handler in complete_scsi_command()  */
4432edd16368SStephen M. Cameron 	return 0;
4433edd16368SStephen M. Cameron }
4434edd16368SStephen M. Cameron 
4435360c73bdSStephen Cameron static void hpsa_cmd_init(struct ctlr_info *h, int index,
4436360c73bdSStephen Cameron 				struct CommandList *c)
4437360c73bdSStephen Cameron {
4438360c73bdSStephen Cameron 	dma_addr_t cmd_dma_handle, err_dma_handle;
4439360c73bdSStephen Cameron 
4440360c73bdSStephen Cameron 	/* Zero out all of commandlist except the last field, refcount */
4441360c73bdSStephen Cameron 	memset(c, 0, offsetof(struct CommandList, refcount));
4442360c73bdSStephen Cameron 	c->Header.tag = cpu_to_le64((u64) (index << DIRECT_LOOKUP_SHIFT));
4443360c73bdSStephen Cameron 	cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
4444360c73bdSStephen Cameron 	c->err_info = h->errinfo_pool + index;
4445360c73bdSStephen Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
4446360c73bdSStephen Cameron 	err_dma_handle = h->errinfo_pool_dhandle
4447360c73bdSStephen Cameron 	    + index * sizeof(*c->err_info);
4448360c73bdSStephen Cameron 	c->cmdindex = index;
4449360c73bdSStephen Cameron 	c->busaddr = (u32) cmd_dma_handle;
4450360c73bdSStephen Cameron 	c->ErrDesc.Addr = cpu_to_le64((u64) err_dma_handle);
4451360c73bdSStephen Cameron 	c->ErrDesc.Len = cpu_to_le32((u32) sizeof(*c->err_info));
4452360c73bdSStephen Cameron 	c->h = h;
4453360c73bdSStephen Cameron }
4454360c73bdSStephen Cameron 
4455360c73bdSStephen Cameron static void hpsa_preinitialize_commands(struct ctlr_info *h)
4456360c73bdSStephen Cameron {
4457360c73bdSStephen Cameron 	int i;
4458360c73bdSStephen Cameron 
4459360c73bdSStephen Cameron 	for (i = 0; i < h->nr_cmds; i++) {
4460360c73bdSStephen Cameron 		struct CommandList *c = h->cmd_pool + i;
4461360c73bdSStephen Cameron 
4462360c73bdSStephen Cameron 		hpsa_cmd_init(h, i, c);
4463360c73bdSStephen Cameron 		atomic_set(&c->refcount, 0);
4464360c73bdSStephen Cameron 	}
4465360c73bdSStephen Cameron }
4466360c73bdSStephen Cameron 
4467360c73bdSStephen Cameron static inline void hpsa_cmd_partial_init(struct ctlr_info *h, int index,
4468360c73bdSStephen Cameron 				struct CommandList *c)
4469360c73bdSStephen Cameron {
4470360c73bdSStephen Cameron 	dma_addr_t cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
4471360c73bdSStephen Cameron 
4472360c73bdSStephen Cameron 	memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
4473360c73bdSStephen Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
4474360c73bdSStephen Cameron 	c->busaddr = (u32) cmd_dma_handle;
4475360c73bdSStephen Cameron }
4476360c73bdSStephen Cameron 
4477592a0ad5SWebb Scales static int hpsa_ioaccel_submit(struct ctlr_info *h,
4478592a0ad5SWebb Scales 		struct CommandList *c, struct scsi_cmnd *cmd,
4479592a0ad5SWebb Scales 		unsigned char *scsi3addr)
4480592a0ad5SWebb Scales {
4481592a0ad5SWebb Scales 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4482592a0ad5SWebb Scales 	int rc = IO_ACCEL_INELIGIBLE;
4483592a0ad5SWebb Scales 
4484592a0ad5SWebb Scales 	cmd->host_scribble = (unsigned char *) c;
4485592a0ad5SWebb Scales 
4486592a0ad5SWebb Scales 	if (dev->offload_enabled) {
4487592a0ad5SWebb Scales 		hpsa_cmd_init(h, c->cmdindex, c);
4488592a0ad5SWebb Scales 		c->cmd_type = CMD_SCSI;
4489592a0ad5SWebb Scales 		c->scsi_cmd = cmd;
4490592a0ad5SWebb Scales 		rc = hpsa_scsi_ioaccel_raid_map(h, c);
4491592a0ad5SWebb Scales 		if (rc < 0)     /* scsi_dma_map failed. */
4492592a0ad5SWebb Scales 			rc = SCSI_MLQUEUE_HOST_BUSY;
4493a3144e0bSJoe Handzik 	} else if (dev->hba_ioaccel_enabled) {
4494592a0ad5SWebb Scales 		hpsa_cmd_init(h, c->cmdindex, c);
4495592a0ad5SWebb Scales 		c->cmd_type = CMD_SCSI;
4496592a0ad5SWebb Scales 		c->scsi_cmd = cmd;
4497592a0ad5SWebb Scales 		rc = hpsa_scsi_ioaccel_direct_map(h, c);
4498592a0ad5SWebb Scales 		if (rc < 0)     /* scsi_dma_map failed. */
4499592a0ad5SWebb Scales 			rc = SCSI_MLQUEUE_HOST_BUSY;
4500592a0ad5SWebb Scales 	}
4501592a0ad5SWebb Scales 	return rc;
4502592a0ad5SWebb Scales }
4503592a0ad5SWebb Scales 
4504080ef1ccSDon Brace static void hpsa_command_resubmit_worker(struct work_struct *work)
4505080ef1ccSDon Brace {
4506080ef1ccSDon Brace 	struct scsi_cmnd *cmd;
4507080ef1ccSDon Brace 	struct hpsa_scsi_dev_t *dev;
4508*8a0ff92cSWebb Scales 	struct CommandList *c = container_of(work, struct CommandList, work);
4509080ef1ccSDon Brace 
4510080ef1ccSDon Brace 	cmd = c->scsi_cmd;
4511080ef1ccSDon Brace 	dev = cmd->device->hostdata;
4512080ef1ccSDon Brace 	if (!dev) {
4513080ef1ccSDon Brace 		cmd->result = DID_NO_CONNECT << 16;
4514*8a0ff92cSWebb Scales 		return hpsa_cmd_free_and_done(c->h, c, cmd);
4515080ef1ccSDon Brace 	}
4516592a0ad5SWebb Scales 	if (c->cmd_type == CMD_IOACCEL2) {
4517592a0ad5SWebb Scales 		struct ctlr_info *h = c->h;
4518592a0ad5SWebb Scales 		struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
4519592a0ad5SWebb Scales 		int rc;
4520592a0ad5SWebb Scales 
4521592a0ad5SWebb Scales 		if (c2->error_data.serv_response ==
4522592a0ad5SWebb Scales 				IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL) {
4523592a0ad5SWebb Scales 			rc = hpsa_ioaccel_submit(h, c, cmd, dev->scsi3addr);
4524592a0ad5SWebb Scales 			if (rc == 0)
4525592a0ad5SWebb Scales 				return;
4526592a0ad5SWebb Scales 			if (rc == SCSI_MLQUEUE_HOST_BUSY) {
4527592a0ad5SWebb Scales 				/*
4528592a0ad5SWebb Scales 				 * If we get here, it means dma mapping failed.
4529592a0ad5SWebb Scales 				 * Try again via scsi mid layer, which will
4530592a0ad5SWebb Scales 				 * then get SCSI_MLQUEUE_HOST_BUSY.
4531592a0ad5SWebb Scales 				 */
4532592a0ad5SWebb Scales 				cmd->result = DID_IMM_RETRY << 16;
4533*8a0ff92cSWebb Scales 				return hpsa_cmd_free_and_done(h, c, cmd);
4534592a0ad5SWebb Scales 			}
4535592a0ad5SWebb Scales 			/* else, fall thru and resubmit down CISS path */
4536592a0ad5SWebb Scales 		}
4537592a0ad5SWebb Scales 	}
4538360c73bdSStephen Cameron 	hpsa_cmd_partial_init(c->h, c->cmdindex, c);
4539080ef1ccSDon Brace 	if (hpsa_ciss_submit(c->h, c, cmd, dev->scsi3addr)) {
4540080ef1ccSDon Brace 		/*
4541080ef1ccSDon Brace 		 * If we get here, it means dma mapping failed. Try
4542080ef1ccSDon Brace 		 * again via scsi mid layer, which will then get
4543080ef1ccSDon Brace 		 * SCSI_MLQUEUE_HOST_BUSY.
4544592a0ad5SWebb Scales 		 *
4545592a0ad5SWebb Scales 		 * hpsa_ciss_submit will have already freed c
4546592a0ad5SWebb Scales 		 * if it encountered a dma mapping failure.
4547080ef1ccSDon Brace 		 */
4548080ef1ccSDon Brace 		cmd->result = DID_IMM_RETRY << 16;
4549080ef1ccSDon Brace 		cmd->scsi_done(cmd);
4550080ef1ccSDon Brace 	}
4551080ef1ccSDon Brace }
4552080ef1ccSDon Brace 
4553574f05d3SStephen Cameron /* Running in struct Scsi_Host->host_lock less mode */
4554574f05d3SStephen Cameron static int hpsa_scsi_queue_command(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
4555574f05d3SStephen Cameron {
4556574f05d3SStephen Cameron 	struct ctlr_info *h;
4557574f05d3SStephen Cameron 	struct hpsa_scsi_dev_t *dev;
4558574f05d3SStephen Cameron 	unsigned char scsi3addr[8];
4559574f05d3SStephen Cameron 	struct CommandList *c;
4560574f05d3SStephen Cameron 	int rc = 0;
4561574f05d3SStephen Cameron 
4562574f05d3SStephen Cameron 	/* Get the ptr to our adapter structure out of cmd->host. */
4563574f05d3SStephen Cameron 	h = sdev_to_hba(cmd->device);
4564574f05d3SStephen Cameron 	dev = cmd->device->hostdata;
4565574f05d3SStephen Cameron 	if (!dev) {
4566574f05d3SStephen Cameron 		cmd->result = DID_NO_CONNECT << 16;
4567574f05d3SStephen Cameron 		cmd->scsi_done(cmd);
4568574f05d3SStephen Cameron 		return 0;
4569574f05d3SStephen Cameron 	}
4570574f05d3SStephen Cameron 	memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
4571574f05d3SStephen Cameron 
4572574f05d3SStephen Cameron 	if (unlikely(lockup_detected(h))) {
457325163bd5SWebb Scales 		cmd->result = DID_NO_CONNECT << 16;
4574574f05d3SStephen Cameron 		cmd->scsi_done(cmd);
4575574f05d3SStephen Cameron 		return 0;
4576574f05d3SStephen Cameron 	}
4577574f05d3SStephen Cameron 	c = cmd_alloc(h);
4578bf43caf3SRobert Elliott 
4579407863cbSStephen Cameron 	if (unlikely(lockup_detected(h))) {
458025163bd5SWebb Scales 		cmd->result = DID_NO_CONNECT << 16;
4581407863cbSStephen Cameron 		cmd_free(h, c);
4582407863cbSStephen Cameron 		cmd->scsi_done(cmd);
4583407863cbSStephen Cameron 		return 0;
4584407863cbSStephen Cameron 	}
4585574f05d3SStephen Cameron 
4586407863cbSStephen Cameron 	/*
4587407863cbSStephen Cameron 	 * Call alternate submit routine for I/O accelerated commands.
4588574f05d3SStephen Cameron 	 * Retries always go down the normal I/O path.
4589574f05d3SStephen Cameron 	 */
4590574f05d3SStephen Cameron 	if (likely(cmd->retries == 0 &&
4591574f05d3SStephen Cameron 		cmd->request->cmd_type == REQ_TYPE_FS &&
4592574f05d3SStephen Cameron 		h->acciopath_status)) {
4593592a0ad5SWebb Scales 		rc = hpsa_ioaccel_submit(h, c, cmd, scsi3addr);
4594574f05d3SStephen Cameron 		if (rc == 0)
4595592a0ad5SWebb Scales 			return 0;
4596592a0ad5SWebb Scales 		if (rc == SCSI_MLQUEUE_HOST_BUSY) {
4597*8a0ff92cSWebb Scales 			cmd_free(h, c);
4598574f05d3SStephen Cameron 			return SCSI_MLQUEUE_HOST_BUSY;
4599574f05d3SStephen Cameron 		}
4600574f05d3SStephen Cameron 	}
4601574f05d3SStephen Cameron 	return hpsa_ciss_submit(h, c, cmd, scsi3addr);
4602574f05d3SStephen Cameron }
4603574f05d3SStephen Cameron 
46048ebc9248SWebb Scales static void hpsa_scan_complete(struct ctlr_info *h)
46055f389360SStephen M. Cameron {
46065f389360SStephen M. Cameron 	unsigned long flags;
46075f389360SStephen M. Cameron 
46085f389360SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
46095f389360SStephen M. Cameron 	h->scan_finished = 1;
46105f389360SStephen M. Cameron 	wake_up_all(&h->scan_wait_queue);
46115f389360SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
46125f389360SStephen M. Cameron }
46135f389360SStephen M. Cameron 
4614a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *sh)
4615a08a8471SStephen M. Cameron {
4616a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
4617a08a8471SStephen M. Cameron 	unsigned long flags;
4618a08a8471SStephen M. Cameron 
46198ebc9248SWebb Scales 	/*
46208ebc9248SWebb Scales 	 * Don't let rescans be initiated on a controller known to be locked
46218ebc9248SWebb Scales 	 * up.  If the controller locks up *during* a rescan, that thread is
46228ebc9248SWebb Scales 	 * probably hosed, but at least we can prevent new rescan threads from
46238ebc9248SWebb Scales 	 * piling up on a locked up controller.
46248ebc9248SWebb Scales 	 */
46258ebc9248SWebb Scales 	if (unlikely(lockup_detected(h)))
46268ebc9248SWebb Scales 		return hpsa_scan_complete(h);
46275f389360SStephen M. Cameron 
4628a08a8471SStephen M. Cameron 	/* wait until any scan already in progress is finished. */
4629a08a8471SStephen M. Cameron 	while (1) {
4630a08a8471SStephen M. Cameron 		spin_lock_irqsave(&h->scan_lock, flags);
4631a08a8471SStephen M. Cameron 		if (h->scan_finished)
4632a08a8471SStephen M. Cameron 			break;
4633a08a8471SStephen M. Cameron 		spin_unlock_irqrestore(&h->scan_lock, flags);
4634a08a8471SStephen M. Cameron 		wait_event(h->scan_wait_queue, h->scan_finished);
4635a08a8471SStephen M. Cameron 		/* Note: We don't need to worry about a race between this
4636a08a8471SStephen M. Cameron 		 * thread and driver unload because the midlayer will
4637a08a8471SStephen M. Cameron 		 * have incremented the reference count, so unload won't
4638a08a8471SStephen M. Cameron 		 * happen if we're in here.
4639a08a8471SStephen M. Cameron 		 */
4640a08a8471SStephen M. Cameron 	}
4641a08a8471SStephen M. Cameron 	h->scan_finished = 0; /* mark scan as in progress */
4642a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4643a08a8471SStephen M. Cameron 
46448ebc9248SWebb Scales 	if (unlikely(lockup_detected(h)))
46458ebc9248SWebb Scales 		return hpsa_scan_complete(h);
46465f389360SStephen M. Cameron 
4647a08a8471SStephen M. Cameron 	hpsa_update_scsi_devices(h, h->scsi_host->host_no);
4648a08a8471SStephen M. Cameron 
46498ebc9248SWebb Scales 	hpsa_scan_complete(h);
4650a08a8471SStephen M. Cameron }
4651a08a8471SStephen M. Cameron 
46527c0a0229SDon Brace static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth)
46537c0a0229SDon Brace {
465403383736SDon Brace 	struct hpsa_scsi_dev_t *logical_drive = sdev->hostdata;
465503383736SDon Brace 
465603383736SDon Brace 	if (!logical_drive)
465703383736SDon Brace 		return -ENODEV;
46587c0a0229SDon Brace 
46597c0a0229SDon Brace 	if (qdepth < 1)
46607c0a0229SDon Brace 		qdepth = 1;
466103383736SDon Brace 	else if (qdepth > logical_drive->queue_depth)
466203383736SDon Brace 		qdepth = logical_drive->queue_depth;
466303383736SDon Brace 
466403383736SDon Brace 	return scsi_change_queue_depth(sdev, qdepth);
46657c0a0229SDon Brace }
46667c0a0229SDon Brace 
4667a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
4668a08a8471SStephen M. Cameron 	unsigned long elapsed_time)
4669a08a8471SStephen M. Cameron {
4670a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
4671a08a8471SStephen M. Cameron 	unsigned long flags;
4672a08a8471SStephen M. Cameron 	int finished;
4673a08a8471SStephen M. Cameron 
4674a08a8471SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
4675a08a8471SStephen M. Cameron 	finished = h->scan_finished;
4676a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4677a08a8471SStephen M. Cameron 	return finished;
4678a08a8471SStephen M. Cameron }
4679a08a8471SStephen M. Cameron 
4680edd16368SStephen M. Cameron static void hpsa_unregister_scsi(struct ctlr_info *h)
4681edd16368SStephen M. Cameron {
4682edd16368SStephen M. Cameron 	/* we are being forcibly unloaded, and may not refuse. */
4683edd16368SStephen M. Cameron 	scsi_remove_host(h->scsi_host);
4684edd16368SStephen M. Cameron 	scsi_host_put(h->scsi_host);
4685edd16368SStephen M. Cameron 	h->scsi_host = NULL;
4686edd16368SStephen M. Cameron }
4687edd16368SStephen M. Cameron 
4688edd16368SStephen M. Cameron static int hpsa_register_scsi(struct ctlr_info *h)
4689edd16368SStephen M. Cameron {
4690b705690dSStephen M. Cameron 	struct Scsi_Host *sh;
4691b705690dSStephen M. Cameron 	int error;
4692edd16368SStephen M. Cameron 
4693b705690dSStephen M. Cameron 	sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
4694b705690dSStephen M. Cameron 	if (sh == NULL)
4695b705690dSStephen M. Cameron 		goto fail;
4696b705690dSStephen M. Cameron 
4697b705690dSStephen M. Cameron 	sh->io_port = 0;
4698b705690dSStephen M. Cameron 	sh->n_io_port = 0;
4699b705690dSStephen M. Cameron 	sh->this_id = -1;
4700b705690dSStephen M. Cameron 	sh->max_channel = 3;
4701b705690dSStephen M. Cameron 	sh->max_cmd_len = MAX_COMMAND_SIZE;
4702b705690dSStephen M. Cameron 	sh->max_lun = HPSA_MAX_LUN;
4703b705690dSStephen M. Cameron 	sh->max_id = HPSA_MAX_LUN;
470441ce4c35SStephen Cameron 	sh->can_queue = h->nr_cmds - HPSA_NRESERVED_CMDS;
4705d54c5c24SStephen Cameron 	sh->cmd_per_lun = sh->can_queue;
4706b705690dSStephen M. Cameron 	sh->sg_tablesize = h->maxsgentries;
4707b705690dSStephen M. Cameron 	h->scsi_host = sh;
4708b705690dSStephen M. Cameron 	sh->hostdata[0] = (unsigned long) h;
4709b705690dSStephen M. Cameron 	sh->irq = h->intr[h->intr_mode];
4710b705690dSStephen M. Cameron 	sh->unique_id = sh->irq;
4711b705690dSStephen M. Cameron 	error = scsi_add_host(sh, &h->pdev->dev);
4712b705690dSStephen M. Cameron 	if (error)
4713b705690dSStephen M. Cameron 		goto fail_host_put;
4714b705690dSStephen M. Cameron 	scsi_scan_host(sh);
4715b705690dSStephen M. Cameron 	return 0;
4716b705690dSStephen M. Cameron 
4717b705690dSStephen M. Cameron  fail_host_put:
4718b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_add_host"
4719b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
4720b705690dSStephen M. Cameron 	scsi_host_put(sh);
4721b705690dSStephen M. Cameron 	return error;
4722b705690dSStephen M. Cameron  fail:
4723b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_host_alloc"
4724b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
4725b705690dSStephen M. Cameron 	return -ENOMEM;
4726edd16368SStephen M. Cameron }
4727edd16368SStephen M. Cameron 
4728edd16368SStephen M. Cameron static int wait_for_device_to_become_ready(struct ctlr_info *h,
4729edd16368SStephen M. Cameron 	unsigned char lunaddr[])
4730edd16368SStephen M. Cameron {
47318919358eSTomas Henzl 	int rc;
4732edd16368SStephen M. Cameron 	int count = 0;
4733edd16368SStephen M. Cameron 	int waittime = 1; /* seconds */
4734edd16368SStephen M. Cameron 	struct CommandList *c;
4735edd16368SStephen M. Cameron 
473645fcb86eSStephen Cameron 	c = cmd_alloc(h);
4737edd16368SStephen M. Cameron 
4738edd16368SStephen M. Cameron 	/* Send test unit ready until device ready, or give up. */
4739edd16368SStephen M. Cameron 	while (count < HPSA_TUR_RETRY_LIMIT) {
4740edd16368SStephen M. Cameron 
4741edd16368SStephen M. Cameron 		/* Wait for a bit.  do this first, because if we send
4742edd16368SStephen M. Cameron 		 * the TUR right away, the reset will just abort it.
4743edd16368SStephen M. Cameron 		 */
4744edd16368SStephen M. Cameron 		msleep(1000 * waittime);
4745edd16368SStephen M. Cameron 		count++;
47468919358eSTomas Henzl 		rc = 0; /* Device ready. */
4747edd16368SStephen M. Cameron 
4748edd16368SStephen M. Cameron 		/* Increase wait time with each try, up to a point. */
4749edd16368SStephen M. Cameron 		if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
4750edd16368SStephen M. Cameron 			waittime = waittime * 2;
4751edd16368SStephen M. Cameron 
4752a2dac136SStephen M. Cameron 		/* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
4753a2dac136SStephen M. Cameron 		(void) fill_cmd(c, TEST_UNIT_READY, h,
4754a2dac136SStephen M. Cameron 				NULL, 0, 0, lunaddr, TYPE_CMD);
475525163bd5SWebb Scales 		rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
475625163bd5SWebb Scales 						NO_TIMEOUT);
475725163bd5SWebb Scales 		if (rc)
475825163bd5SWebb Scales 			goto do_it_again;
4759edd16368SStephen M. Cameron 		/* no unmap needed here because no data xfer. */
4760edd16368SStephen M. Cameron 
4761edd16368SStephen M. Cameron 		if (c->err_info->CommandStatus == CMD_SUCCESS)
4762edd16368SStephen M. Cameron 			break;
4763edd16368SStephen M. Cameron 
4764edd16368SStephen M. Cameron 		if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
4765edd16368SStephen M. Cameron 			c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
4766edd16368SStephen M. Cameron 			(c->err_info->SenseInfo[2] == NO_SENSE ||
4767edd16368SStephen M. Cameron 			c->err_info->SenseInfo[2] == UNIT_ATTENTION))
4768edd16368SStephen M. Cameron 			break;
476925163bd5SWebb Scales do_it_again:
4770edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "waiting %d secs "
4771edd16368SStephen M. Cameron 			"for device to become ready.\n", waittime);
4772edd16368SStephen M. Cameron 		rc = 1; /* device not ready. */
4773edd16368SStephen M. Cameron 	}
4774edd16368SStephen M. Cameron 
4775edd16368SStephen M. Cameron 	if (rc)
4776edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "giving up on device.\n");
4777edd16368SStephen M. Cameron 	else
4778edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "device is ready.\n");
4779edd16368SStephen M. Cameron 
478045fcb86eSStephen Cameron 	cmd_free(h, c);
4781edd16368SStephen M. Cameron 	return rc;
4782edd16368SStephen M. Cameron }
4783edd16368SStephen M. Cameron 
4784edd16368SStephen M. Cameron /* Need at least one of these error handlers to keep ../scsi/hosts.c from
4785edd16368SStephen M. Cameron  * complaining.  Doing a host- or bus-reset can't do anything good here.
4786edd16368SStephen M. Cameron  */
4787edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
4788edd16368SStephen M. Cameron {
4789edd16368SStephen M. Cameron 	int rc;
4790edd16368SStephen M. Cameron 	struct ctlr_info *h;
4791edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
4792edd16368SStephen M. Cameron 
4793edd16368SStephen M. Cameron 	/* find the controller to which the command to be aborted was sent */
4794edd16368SStephen M. Cameron 	h = sdev_to_hba(scsicmd->device);
4795edd16368SStephen M. Cameron 	if (h == NULL) /* paranoia */
4796edd16368SStephen M. Cameron 		return FAILED;
4797e345893bSDon Brace 
4798e345893bSDon Brace 	if (lockup_detected(h))
4799e345893bSDon Brace 		return FAILED;
4800e345893bSDon Brace 
4801edd16368SStephen M. Cameron 	dev = scsicmd->device->hostdata;
4802edd16368SStephen M. Cameron 	if (!dev) {
4803edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
4804edd16368SStephen M. Cameron 			"device lookup failed.\n");
4805edd16368SStephen M. Cameron 		return FAILED;
4806edd16368SStephen M. Cameron 	}
480725163bd5SWebb Scales 
480825163bd5SWebb Scales 	/* if controller locked up, we can guarantee command won't complete */
480925163bd5SWebb Scales 	if (lockup_detected(h)) {
481025163bd5SWebb Scales 		dev_warn(&h->pdev->dev,
481125163bd5SWebb Scales 			"scsi %d:%d:%d:%d RESET FAILED, lockup detected\n",
481225163bd5SWebb Scales 			h->scsi_host->host_no, dev->bus, dev->target,
481325163bd5SWebb Scales 			dev->lun);
481425163bd5SWebb Scales 		return FAILED;
481525163bd5SWebb Scales 	}
481625163bd5SWebb Scales 
481725163bd5SWebb Scales 	/* this reset request might be the result of a lockup; check */
481825163bd5SWebb Scales 	if (detect_controller_lockup(h)) {
481925163bd5SWebb Scales 		dev_warn(&h->pdev->dev,
482025163bd5SWebb Scales 			 "scsi %d:%d:%d:%d RESET FAILED, new lockup detected\n",
482125163bd5SWebb Scales 			 h->scsi_host->host_no, dev->bus, dev->target,
482225163bd5SWebb Scales 			 dev->lun);
482325163bd5SWebb Scales 		return FAILED;
482425163bd5SWebb Scales 	}
482525163bd5SWebb Scales 
482625163bd5SWebb Scales 	hpsa_show_dev_msg(KERN_WARNING, h, dev, "resetting");
482725163bd5SWebb Scales 
4828edd16368SStephen M. Cameron 	/* send a reset to the SCSI LUN which the command was sent to */
482925163bd5SWebb Scales 	rc = hpsa_send_reset(h, dev->scsi3addr, HPSA_RESET_TYPE_LUN,
483025163bd5SWebb Scales 			     DEFAULT_REPLY_QUEUE);
4831edd16368SStephen M. Cameron 	if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
4832edd16368SStephen M. Cameron 		return SUCCESS;
4833edd16368SStephen M. Cameron 
483425163bd5SWebb Scales 	dev_warn(&h->pdev->dev,
483525163bd5SWebb Scales 		"scsi %d:%d:%d:%d reset failed\n",
483625163bd5SWebb Scales 		h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
4837edd16368SStephen M. Cameron 	return FAILED;
4838edd16368SStephen M. Cameron }
4839edd16368SStephen M. Cameron 
48406cba3f19SStephen M. Cameron static void swizzle_abort_tag(u8 *tag)
48416cba3f19SStephen M. Cameron {
48426cba3f19SStephen M. Cameron 	u8 original_tag[8];
48436cba3f19SStephen M. Cameron 
48446cba3f19SStephen M. Cameron 	memcpy(original_tag, tag, 8);
48456cba3f19SStephen M. Cameron 	tag[0] = original_tag[3];
48466cba3f19SStephen M. Cameron 	tag[1] = original_tag[2];
48476cba3f19SStephen M. Cameron 	tag[2] = original_tag[1];
48486cba3f19SStephen M. Cameron 	tag[3] = original_tag[0];
48496cba3f19SStephen M. Cameron 	tag[4] = original_tag[7];
48506cba3f19SStephen M. Cameron 	tag[5] = original_tag[6];
48516cba3f19SStephen M. Cameron 	tag[6] = original_tag[5];
48526cba3f19SStephen M. Cameron 	tag[7] = original_tag[4];
48536cba3f19SStephen M. Cameron }
48546cba3f19SStephen M. Cameron 
485517eb87d2SScott Teel static void hpsa_get_tag(struct ctlr_info *h,
48562b08b3e9SDon Brace 	struct CommandList *c, __le32 *taglower, __le32 *tagupper)
485717eb87d2SScott Teel {
48582b08b3e9SDon Brace 	u64 tag;
485917eb87d2SScott Teel 	if (c->cmd_type == CMD_IOACCEL1) {
486017eb87d2SScott Teel 		struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
486117eb87d2SScott Teel 			&h->ioaccel_cmd_pool[c->cmdindex];
48622b08b3e9SDon Brace 		tag = le64_to_cpu(cm1->tag);
48632b08b3e9SDon Brace 		*tagupper = cpu_to_le32(tag >> 32);
48642b08b3e9SDon Brace 		*taglower = cpu_to_le32(tag);
486554b6e9e9SScott Teel 		return;
486654b6e9e9SScott Teel 	}
486754b6e9e9SScott Teel 	if (c->cmd_type == CMD_IOACCEL2) {
486854b6e9e9SScott Teel 		struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
486954b6e9e9SScott Teel 			&h->ioaccel2_cmd_pool[c->cmdindex];
4870dd0e19f3SScott Teel 		/* upper tag not used in ioaccel2 mode */
4871dd0e19f3SScott Teel 		memset(tagupper, 0, sizeof(*tagupper));
4872dd0e19f3SScott Teel 		*taglower = cm2->Tag;
487354b6e9e9SScott Teel 		return;
487454b6e9e9SScott Teel 	}
48752b08b3e9SDon Brace 	tag = le64_to_cpu(c->Header.tag);
48762b08b3e9SDon Brace 	*tagupper = cpu_to_le32(tag >> 32);
48772b08b3e9SDon Brace 	*taglower = cpu_to_le32(tag);
487817eb87d2SScott Teel }
487954b6e9e9SScott Teel 
488075167d2cSStephen M. Cameron static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
48819b5c48c2SStephen Cameron 	struct CommandList *abort, int reply_queue)
488275167d2cSStephen M. Cameron {
488375167d2cSStephen M. Cameron 	int rc = IO_OK;
488475167d2cSStephen M. Cameron 	struct CommandList *c;
488575167d2cSStephen M. Cameron 	struct ErrorInfo *ei;
48862b08b3e9SDon Brace 	__le32 tagupper, taglower;
488775167d2cSStephen M. Cameron 
488845fcb86eSStephen Cameron 	c = cmd_alloc(h);
488975167d2cSStephen M. Cameron 
4890a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no buffer to map */
48919b5c48c2SStephen Cameron 	(void) fill_cmd(c, HPSA_ABORT_MSG, h, &abort->Header.tag,
4892a2dac136SStephen M. Cameron 		0, 0, scsi3addr, TYPE_MSG);
48939b5c48c2SStephen Cameron 	if (h->needs_abort_tags_swizzled)
48946cba3f19SStephen M. Cameron 		swizzle_abort_tag(&c->Request.CDB[4]);
489525163bd5SWebb Scales 	(void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
489617eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
489725163bd5SWebb Scales 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd(abort) completed.\n",
489817eb87d2SScott Teel 		__func__, tagupper, taglower);
489975167d2cSStephen M. Cameron 	/* no unmap needed here because no data xfer. */
490075167d2cSStephen M. Cameron 
490175167d2cSStephen M. Cameron 	ei = c->err_info;
490275167d2cSStephen M. Cameron 	switch (ei->CommandStatus) {
490375167d2cSStephen M. Cameron 	case CMD_SUCCESS:
490475167d2cSStephen M. Cameron 		break;
49059437ac43SStephen Cameron 	case CMD_TMF_STATUS:
49069437ac43SStephen Cameron 		rc = hpsa_evaluate_tmf_status(h, c);
49079437ac43SStephen Cameron 		break;
490875167d2cSStephen M. Cameron 	case CMD_UNABORTABLE: /* Very common, don't make noise. */
490975167d2cSStephen M. Cameron 		rc = -1;
491075167d2cSStephen M. Cameron 		break;
491175167d2cSStephen M. Cameron 	default:
491275167d2cSStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
491317eb87d2SScott Teel 			__func__, tagupper, taglower);
4914d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
491575167d2cSStephen M. Cameron 		rc = -1;
491675167d2cSStephen M. Cameron 		break;
491775167d2cSStephen M. Cameron 	}
491845fcb86eSStephen Cameron 	cmd_free(h, c);
4919dd0e19f3SScott Teel 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n",
4920dd0e19f3SScott Teel 		__func__, tagupper, taglower);
492175167d2cSStephen M. Cameron 	return rc;
492275167d2cSStephen M. Cameron }
492375167d2cSStephen M. Cameron 
49248be986ccSStephen Cameron static void setup_ioaccel2_abort_cmd(struct CommandList *c, struct ctlr_info *h,
49258be986ccSStephen Cameron 	struct CommandList *command_to_abort, int reply_queue)
49268be986ccSStephen Cameron {
49278be986ccSStephen Cameron 	struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
49288be986ccSStephen Cameron 	struct hpsa_tmf_struct *ac = (struct hpsa_tmf_struct *) c2;
49298be986ccSStephen Cameron 	struct io_accel2_cmd *c2a =
49308be986ccSStephen Cameron 		&h->ioaccel2_cmd_pool[command_to_abort->cmdindex];
49318be986ccSStephen Cameron 	struct scsi_cmnd *scmd =
49328be986ccSStephen Cameron 		(struct scsi_cmnd *) command_to_abort->scsi_cmd;
49338be986ccSStephen Cameron 	struct hpsa_scsi_dev_t *dev = scmd->device->hostdata;
49348be986ccSStephen Cameron 
49358be986ccSStephen Cameron 	/*
49368be986ccSStephen Cameron 	 * We're overlaying struct hpsa_tmf_struct on top of something which
49378be986ccSStephen Cameron 	 * was allocated as a struct io_accel2_cmd, so we better be sure it
49388be986ccSStephen Cameron 	 * actually fits, and doesn't overrun the error info space.
49398be986ccSStephen Cameron 	 */
49408be986ccSStephen Cameron 	BUILD_BUG_ON(sizeof(struct hpsa_tmf_struct) >
49418be986ccSStephen Cameron 			sizeof(struct io_accel2_cmd));
49428be986ccSStephen Cameron 	BUG_ON(offsetof(struct io_accel2_cmd, error_data) <
49438be986ccSStephen Cameron 			offsetof(struct hpsa_tmf_struct, error_len) +
49448be986ccSStephen Cameron 				sizeof(ac->error_len));
49458be986ccSStephen Cameron 
49468be986ccSStephen Cameron 	c->cmd_type = IOACCEL2_TMF;
49478be986ccSStephen Cameron 	/* Adjust the DMA address to point to the accelerated command buffer */
49488be986ccSStephen Cameron 	c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
49498be986ccSStephen Cameron 				(c->cmdindex * sizeof(struct io_accel2_cmd));
49508be986ccSStephen Cameron 	BUG_ON(c->busaddr & 0x0000007F);
49518be986ccSStephen Cameron 
49528be986ccSStephen Cameron 	memset(ac, 0, sizeof(*c2)); /* yes this is correct */
49538be986ccSStephen Cameron 	ac->iu_type = IOACCEL2_IU_TMF_TYPE;
49548be986ccSStephen Cameron 	ac->reply_queue = reply_queue;
49558be986ccSStephen Cameron 	ac->tmf = IOACCEL2_TMF_ABORT;
49568be986ccSStephen Cameron 	ac->it_nexus = cpu_to_le32(dev->ioaccel_handle);
49578be986ccSStephen Cameron 	memset(ac->lun_id, 0, sizeof(ac->lun_id));
49588be986ccSStephen Cameron 	ac->tag = cpu_to_le64(c->cmdindex << DIRECT_LOOKUP_SHIFT);
49598be986ccSStephen Cameron 	ac->abort_tag = cpu_to_le64(le32_to_cpu(c2a->Tag));
49608be986ccSStephen Cameron 	ac->error_ptr = cpu_to_le64(c->busaddr +
49618be986ccSStephen Cameron 			offsetof(struct io_accel2_cmd, error_data));
49628be986ccSStephen Cameron 	ac->error_len = cpu_to_le32(sizeof(c2->error_data));
49638be986ccSStephen Cameron }
49648be986ccSStephen Cameron 
496554b6e9e9SScott Teel /* ioaccel2 path firmware cannot handle abort task requests.
496654b6e9e9SScott Teel  * Change abort requests to physical target reset, and send to the
496754b6e9e9SScott Teel  * address of the physical disk used for the ioaccel 2 command.
496854b6e9e9SScott Teel  * Return 0 on success (IO_OK)
496954b6e9e9SScott Teel  *	 -1 on failure
497054b6e9e9SScott Teel  */
497154b6e9e9SScott Teel 
497254b6e9e9SScott Teel static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
497325163bd5SWebb Scales 	unsigned char *scsi3addr, struct CommandList *abort, int reply_queue)
497454b6e9e9SScott Teel {
497554b6e9e9SScott Teel 	int rc = IO_OK;
497654b6e9e9SScott Teel 	struct scsi_cmnd *scmd; /* scsi command within request being aborted */
497754b6e9e9SScott Teel 	struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
497854b6e9e9SScott Teel 	unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
497954b6e9e9SScott Teel 	unsigned char *psa = &phys_scsi3addr[0];
498054b6e9e9SScott Teel 
498154b6e9e9SScott Teel 	/* Get a pointer to the hpsa logical device. */
49827fa3030cSStephen Cameron 	scmd = abort->scsi_cmd;
498354b6e9e9SScott Teel 	dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
498454b6e9e9SScott Teel 	if (dev == NULL) {
498554b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
498654b6e9e9SScott Teel 			"Cannot abort: no device pointer for command.\n");
498754b6e9e9SScott Teel 			return -1; /* not abortable */
498854b6e9e9SScott Teel 	}
498954b6e9e9SScott Teel 
49902ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
49912ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
49920d96ef5fSWebb Scales 			"scsi %d:%d:%d:%d %s scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
49932ba8bfc8SStephen M. Cameron 			h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
49940d96ef5fSWebb Scales 			"Reset as abort",
49952ba8bfc8SStephen M. Cameron 			scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
49962ba8bfc8SStephen M. Cameron 			scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]);
49972ba8bfc8SStephen M. Cameron 
499854b6e9e9SScott Teel 	if (!dev->offload_enabled) {
499954b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
500054b6e9e9SScott Teel 			"Can't abort: device is not operating in HP SSD Smart Path mode.\n");
500154b6e9e9SScott Teel 		return -1; /* not abortable */
500254b6e9e9SScott Teel 	}
500354b6e9e9SScott Teel 
500454b6e9e9SScott Teel 	/* Incoming scsi3addr is logical addr. We need physical disk addr. */
500554b6e9e9SScott Teel 	if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
500654b6e9e9SScott Teel 		dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
500754b6e9e9SScott Teel 		return -1; /* not abortable */
500854b6e9e9SScott Teel 	}
500954b6e9e9SScott Teel 
501054b6e9e9SScott Teel 	/* send the reset */
50112ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
50122ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
50132ba8bfc8SStephen M. Cameron 			"Reset as abort: Resetting physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
50142ba8bfc8SStephen M. Cameron 			psa[0], psa[1], psa[2], psa[3],
50152ba8bfc8SStephen M. Cameron 			psa[4], psa[5], psa[6], psa[7]);
501625163bd5SWebb Scales 	rc = hpsa_send_reset(h, psa, HPSA_RESET_TYPE_TARGET, reply_queue);
501754b6e9e9SScott Teel 	if (rc != 0) {
501854b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
501954b6e9e9SScott Teel 			"Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
502054b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
502154b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
502254b6e9e9SScott Teel 		return rc; /* failed to reset */
502354b6e9e9SScott Teel 	}
502454b6e9e9SScott Teel 
502554b6e9e9SScott Teel 	/* wait for device to recover */
502654b6e9e9SScott Teel 	if (wait_for_device_to_become_ready(h, psa) != 0) {
502754b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
502854b6e9e9SScott Teel 			"Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
502954b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
503054b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
503154b6e9e9SScott Teel 		return -1;  /* failed to recover */
503254b6e9e9SScott Teel 	}
503354b6e9e9SScott Teel 
503454b6e9e9SScott Teel 	/* device recovered */
503554b6e9e9SScott Teel 	dev_info(&h->pdev->dev,
503654b6e9e9SScott Teel 		"Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
503754b6e9e9SScott Teel 		psa[0], psa[1], psa[2], psa[3],
503854b6e9e9SScott Teel 		psa[4], psa[5], psa[6], psa[7]);
503954b6e9e9SScott Teel 
504054b6e9e9SScott Teel 	return rc; /* success */
504154b6e9e9SScott Teel }
504254b6e9e9SScott Teel 
50438be986ccSStephen Cameron static int hpsa_send_abort_ioaccel2(struct ctlr_info *h,
50448be986ccSStephen Cameron 	struct CommandList *abort, int reply_queue)
50458be986ccSStephen Cameron {
50468be986ccSStephen Cameron 	int rc = IO_OK;
50478be986ccSStephen Cameron 	struct CommandList *c;
50488be986ccSStephen Cameron 	__le32 taglower, tagupper;
50498be986ccSStephen Cameron 	struct hpsa_scsi_dev_t *dev;
50508be986ccSStephen Cameron 	struct io_accel2_cmd *c2;
50518be986ccSStephen Cameron 
50528be986ccSStephen Cameron 	dev = abort->scsi_cmd->device->hostdata;
50538be986ccSStephen Cameron 	if (!dev->offload_enabled && !dev->hba_ioaccel_enabled)
50548be986ccSStephen Cameron 		return -1;
50558be986ccSStephen Cameron 
50568be986ccSStephen Cameron 	c = cmd_alloc(h);
50578be986ccSStephen Cameron 	setup_ioaccel2_abort_cmd(c, h, abort, reply_queue);
50588be986ccSStephen Cameron 	c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
50598be986ccSStephen Cameron 	(void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
50608be986ccSStephen Cameron 	hpsa_get_tag(h, abort, &taglower, &tagupper);
50618be986ccSStephen Cameron 	dev_dbg(&h->pdev->dev,
50628be986ccSStephen Cameron 		"%s: Tag:0x%08x:%08x: do_simple_cmd(ioaccel2 abort) completed.\n",
50638be986ccSStephen Cameron 		__func__, tagupper, taglower);
50648be986ccSStephen Cameron 	/* no unmap needed here because no data xfer. */
50658be986ccSStephen Cameron 
50668be986ccSStephen Cameron 	dev_dbg(&h->pdev->dev,
50678be986ccSStephen Cameron 		"%s: Tag:0x%08x:%08x: abort service response = 0x%02x.\n",
50688be986ccSStephen Cameron 		__func__, tagupper, taglower, c2->error_data.serv_response);
50698be986ccSStephen Cameron 	switch (c2->error_data.serv_response) {
50708be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
50718be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
50728be986ccSStephen Cameron 		rc = 0;
50738be986ccSStephen Cameron 		break;
50748be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
50758be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_FAILURE:
50768be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
50778be986ccSStephen Cameron 		rc = -1;
50788be986ccSStephen Cameron 		break;
50798be986ccSStephen Cameron 	default:
50808be986ccSStephen Cameron 		dev_warn(&h->pdev->dev,
50818be986ccSStephen Cameron 			"%s: Tag:0x%08x:%08x: unknown abort service response 0x%02x\n",
50828be986ccSStephen Cameron 			__func__, tagupper, taglower,
50838be986ccSStephen Cameron 			c2->error_data.serv_response);
50848be986ccSStephen Cameron 		rc = -1;
50858be986ccSStephen Cameron 	}
50868be986ccSStephen Cameron 	cmd_free(h, c);
50878be986ccSStephen Cameron 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n", __func__,
50888be986ccSStephen Cameron 		tagupper, taglower);
50898be986ccSStephen Cameron 	return rc;
50908be986ccSStephen Cameron }
50918be986ccSStephen Cameron 
50926cba3f19SStephen M. Cameron static int hpsa_send_abort_both_ways(struct ctlr_info *h,
509325163bd5SWebb Scales 	unsigned char *scsi3addr, struct CommandList *abort, int reply_queue)
50946cba3f19SStephen M. Cameron {
50958be986ccSStephen Cameron 	/*
50968be986ccSStephen Cameron 	 * ioccelerator mode 2 commands should be aborted via the
509754b6e9e9SScott Teel 	 * accelerated path, since RAID path is unaware of these commands,
50988be986ccSStephen Cameron 	 * but not all underlying firmware can handle abort TMF.
50998be986ccSStephen Cameron 	 * Change abort to physical device reset when abort TMF is unsupported.
510054b6e9e9SScott Teel 	 */
51018be986ccSStephen Cameron 	if (abort->cmd_type == CMD_IOACCEL2) {
51028be986ccSStephen Cameron 		if (HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags)
51038be986ccSStephen Cameron 			return hpsa_send_abort_ioaccel2(h, abort,
51048be986ccSStephen Cameron 						reply_queue);
51058be986ccSStephen Cameron 		else
510625163bd5SWebb Scales 			return hpsa_send_reset_as_abort_ioaccel2(h, scsi3addr,
510725163bd5SWebb Scales 							abort, reply_queue);
51088be986ccSStephen Cameron 	}
51099b5c48c2SStephen Cameron 	return hpsa_send_abort(h, scsi3addr, abort, reply_queue);
511025163bd5SWebb Scales }
511125163bd5SWebb Scales 
511225163bd5SWebb Scales /* Find out which reply queue a command was meant to return on */
511325163bd5SWebb Scales static int hpsa_extract_reply_queue(struct ctlr_info *h,
511425163bd5SWebb Scales 					struct CommandList *c)
511525163bd5SWebb Scales {
511625163bd5SWebb Scales 	if (c->cmd_type == CMD_IOACCEL2)
511725163bd5SWebb Scales 		return h->ioaccel2_cmd_pool[c->cmdindex].reply_queue;
511825163bd5SWebb Scales 	return c->Header.ReplyQueue;
51196cba3f19SStephen M. Cameron }
51206cba3f19SStephen M. Cameron 
51219b5c48c2SStephen Cameron /*
51229b5c48c2SStephen Cameron  * Limit concurrency of abort commands to prevent
51239b5c48c2SStephen Cameron  * over-subscription of commands
51249b5c48c2SStephen Cameron  */
51259b5c48c2SStephen Cameron static inline int wait_for_available_abort_cmd(struct ctlr_info *h)
51269b5c48c2SStephen Cameron {
51279b5c48c2SStephen Cameron #define ABORT_CMD_WAIT_MSECS 5000
51289b5c48c2SStephen Cameron 	return !wait_event_timeout(h->abort_cmd_wait_queue,
51299b5c48c2SStephen Cameron 			atomic_dec_if_positive(&h->abort_cmds_available) >= 0,
51309b5c48c2SStephen Cameron 			msecs_to_jiffies(ABORT_CMD_WAIT_MSECS));
51319b5c48c2SStephen Cameron }
51329b5c48c2SStephen Cameron 
513375167d2cSStephen M. Cameron /* Send an abort for the specified command.
513475167d2cSStephen M. Cameron  *	If the device and controller support it,
513575167d2cSStephen M. Cameron  *		send a task abort request.
513675167d2cSStephen M. Cameron  */
513775167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
513875167d2cSStephen M. Cameron {
513975167d2cSStephen M. Cameron 
514075167d2cSStephen M. Cameron 	int i, rc;
514175167d2cSStephen M. Cameron 	struct ctlr_info *h;
514275167d2cSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
514375167d2cSStephen M. Cameron 	struct CommandList *abort; /* pointer to command to be aborted */
514475167d2cSStephen M. Cameron 	struct scsi_cmnd *as;	/* ptr to scsi cmd inside aborted command. */
514575167d2cSStephen M. Cameron 	char msg[256];		/* For debug messaging. */
514675167d2cSStephen M. Cameron 	int ml = 0;
51472b08b3e9SDon Brace 	__le32 tagupper, taglower;
514825163bd5SWebb Scales 	int refcount, reply_queue;
514925163bd5SWebb Scales 
515025163bd5SWebb Scales 	if (sc == NULL)
515125163bd5SWebb Scales 		return FAILED;
515275167d2cSStephen M. Cameron 
51539b5c48c2SStephen Cameron 	if (sc->device == NULL)
51549b5c48c2SStephen Cameron 		return FAILED;
51559b5c48c2SStephen Cameron 
515675167d2cSStephen M. Cameron 	/* Find the controller of the command to be aborted */
515775167d2cSStephen M. Cameron 	h = sdev_to_hba(sc->device);
51589b5c48c2SStephen Cameron 	if (h == NULL)
515975167d2cSStephen M. Cameron 		return FAILED;
516075167d2cSStephen M. Cameron 
516125163bd5SWebb Scales 	/* Find the device of the command to be aborted */
516225163bd5SWebb Scales 	dev = sc->device->hostdata;
516325163bd5SWebb Scales 	if (!dev) {
516425163bd5SWebb Scales 		dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
516525163bd5SWebb Scales 				msg);
5166e345893bSDon Brace 		return FAILED;
516725163bd5SWebb Scales 	}
516825163bd5SWebb Scales 
516925163bd5SWebb Scales 	/* If controller locked up, we can guarantee command won't complete */
517025163bd5SWebb Scales 	if (lockup_detected(h)) {
517125163bd5SWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev,
517225163bd5SWebb Scales 					"ABORT FAILED, lockup detected");
517325163bd5SWebb Scales 		return FAILED;
517425163bd5SWebb Scales 	}
517525163bd5SWebb Scales 
517625163bd5SWebb Scales 	/* This is a good time to check if controller lockup has occurred */
517725163bd5SWebb Scales 	if (detect_controller_lockup(h)) {
517825163bd5SWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev,
517925163bd5SWebb Scales 					"ABORT FAILED, new lockup detected");
518025163bd5SWebb Scales 		return FAILED;
518125163bd5SWebb Scales 	}
5182e345893bSDon Brace 
518375167d2cSStephen M. Cameron 	/* Check that controller supports some kind of task abort */
518475167d2cSStephen M. Cameron 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
518575167d2cSStephen M. Cameron 		!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
518675167d2cSStephen M. Cameron 		return FAILED;
518775167d2cSStephen M. Cameron 
518875167d2cSStephen M. Cameron 	memset(msg, 0, sizeof(msg));
51894b761557SRobert Elliott 	ml += sprintf(msg+ml, "scsi %d:%d:%d:%llu %s %p",
519075167d2cSStephen M. Cameron 		h->scsi_host->host_no, sc->device->channel,
51910d96ef5fSWebb Scales 		sc->device->id, sc->device->lun,
51924b761557SRobert Elliott 		"Aborting command", sc);
519375167d2cSStephen M. Cameron 
519475167d2cSStephen M. Cameron 	/* Get SCSI command to be aborted */
519575167d2cSStephen M. Cameron 	abort = (struct CommandList *) sc->host_scribble;
519675167d2cSStephen M. Cameron 	if (abort == NULL) {
5197281a7fd0SWebb Scales 		/* This can happen if the command already completed. */
5198281a7fd0SWebb Scales 		return SUCCESS;
5199281a7fd0SWebb Scales 	}
5200281a7fd0SWebb Scales 	refcount = atomic_inc_return(&abort->refcount);
5201281a7fd0SWebb Scales 	if (refcount == 1) { /* Command is done already. */
5202281a7fd0SWebb Scales 		cmd_free(h, abort);
5203281a7fd0SWebb Scales 		return SUCCESS;
520475167d2cSStephen M. Cameron 	}
52059b5c48c2SStephen Cameron 
52069b5c48c2SStephen Cameron 	/* Don't bother trying the abort if we know it won't work. */
52079b5c48c2SStephen Cameron 	if (abort->cmd_type != CMD_IOACCEL2 &&
52089b5c48c2SStephen Cameron 		abort->cmd_type != CMD_IOACCEL1 && !dev->supports_aborts) {
52099b5c48c2SStephen Cameron 		cmd_free(h, abort);
52109b5c48c2SStephen Cameron 		return FAILED;
52119b5c48c2SStephen Cameron 	}
52129b5c48c2SStephen Cameron 
521317eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
521425163bd5SWebb Scales 	reply_queue = hpsa_extract_reply_queue(h, abort);
521517eb87d2SScott Teel 	ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
52167fa3030cSStephen Cameron 	as  = abort->scsi_cmd;
521775167d2cSStephen M. Cameron 	if (as != NULL)
52184b761557SRobert Elliott 		ml += sprintf(msg+ml,
52194b761557SRobert Elliott 			"CDBLen: %d CDB: 0x%02x%02x... SN: 0x%lx ",
52204b761557SRobert Elliott 			as->cmd_len, as->cmnd[0], as->cmnd[1],
52214b761557SRobert Elliott 			as->serial_number);
52224b761557SRobert Elliott 	dev_warn(&h->pdev->dev, "%s BEING SENT\n", msg);
52230d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_WARNING, h, dev, "Aborting command");
52244b761557SRobert Elliott 
522575167d2cSStephen M. Cameron 	/*
522675167d2cSStephen M. Cameron 	 * Command is in flight, or possibly already completed
522775167d2cSStephen M. Cameron 	 * by the firmware (but not to the scsi mid layer) but we can't
522875167d2cSStephen M. Cameron 	 * distinguish which.  Send the abort down.
522975167d2cSStephen M. Cameron 	 */
52309b5c48c2SStephen Cameron 	if (wait_for_available_abort_cmd(h)) {
52319b5c48c2SStephen Cameron 		dev_warn(&h->pdev->dev,
52324b761557SRobert Elliott 			"%s FAILED, timeout waiting for an abort command to become available.\n",
52334b761557SRobert Elliott 			msg);
52349b5c48c2SStephen Cameron 		cmd_free(h, abort);
52359b5c48c2SStephen Cameron 		return FAILED;
52369b5c48c2SStephen Cameron 	}
523725163bd5SWebb Scales 	rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort, reply_queue);
52389b5c48c2SStephen Cameron 	atomic_inc(&h->abort_cmds_available);
52399b5c48c2SStephen Cameron 	wake_up_all(&h->abort_cmd_wait_queue);
524075167d2cSStephen M. Cameron 	if (rc != 0) {
52414b761557SRobert Elliott 		dev_warn(&h->pdev->dev, "%s SENT, FAILED\n", msg);
52420d96ef5fSWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev,
52430d96ef5fSWebb Scales 				"FAILED to abort command");
5244281a7fd0SWebb Scales 		cmd_free(h, abort);
524575167d2cSStephen M. Cameron 		return FAILED;
524675167d2cSStephen M. Cameron 	}
52474b761557SRobert Elliott 	dev_info(&h->pdev->dev, "%s SENT, SUCCESS\n", msg);
524875167d2cSStephen M. Cameron 
52494b761557SRobert Elliott 	/*
52504b761557SRobert Elliott 	 * If the abort(s) above completed and actually aborted the
525175167d2cSStephen M. Cameron 	 * command, then the command to be aborted should already be
525275167d2cSStephen M. Cameron 	 * completed.  If not, wait around a bit more to see if they
525375167d2cSStephen M. Cameron 	 * manage to complete normally.
525475167d2cSStephen M. Cameron 	 */
525575167d2cSStephen M. Cameron #define ABORT_COMPLETE_WAIT_SECS 30
525675167d2cSStephen M. Cameron 	for (i = 0; i < ABORT_COMPLETE_WAIT_SECS * 10; i++) {
5257281a7fd0SWebb Scales 		refcount = atomic_read(&abort->refcount);
5258281a7fd0SWebb Scales 		if (refcount < 2) {
5259281a7fd0SWebb Scales 			cmd_free(h, abort);
5260f2405db8SDon Brace 			return SUCCESS;
5261281a7fd0SWebb Scales 		} else {
5262281a7fd0SWebb Scales 			msleep(100);
5263281a7fd0SWebb Scales 		}
526475167d2cSStephen M. Cameron 	}
526575167d2cSStephen M. Cameron 	dev_warn(&h->pdev->dev, "%s FAILED. Aborted command has not completed after %d seconds.\n",
526675167d2cSStephen M. Cameron 		msg, ABORT_COMPLETE_WAIT_SECS);
5267281a7fd0SWebb Scales 	cmd_free(h, abort);
526875167d2cSStephen M. Cameron 	return FAILED;
526975167d2cSStephen M. Cameron }
527075167d2cSStephen M. Cameron 
5271edd16368SStephen M. Cameron /*
5272edd16368SStephen M. Cameron  * For operations that cannot sleep, a command block is allocated at init,
5273edd16368SStephen M. Cameron  * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
5274edd16368SStephen M. Cameron  * which ones are free or in use.  Lock must be held when calling this.
5275edd16368SStephen M. Cameron  * cmd_free() is the complement.
5276bf43caf3SRobert Elliott  * This function never gives up and returns NULL.  If it hangs,
5277bf43caf3SRobert Elliott  * another thread must call cmd_free() to free some tags.
5278edd16368SStephen M. Cameron  */
5279281a7fd0SWebb Scales 
5280edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h)
5281edd16368SStephen M. Cameron {
5282edd16368SStephen M. Cameron 	struct CommandList *c;
5283360c73bdSStephen Cameron 	int refcount, i;
528433811026SRobert Elliott 	unsigned long offset;
5285edd16368SStephen M. Cameron 
528633811026SRobert Elliott 	/*
528733811026SRobert Elliott 	 * There is some *extremely* small but non-zero chance that that
52884c413128SStephen M. Cameron 	 * multiple threads could get in here, and one thread could
52894c413128SStephen M. Cameron 	 * be scanning through the list of bits looking for a free
52904c413128SStephen M. Cameron 	 * one, but the free ones are always behind him, and other
52914c413128SStephen M. Cameron 	 * threads sneak in behind him and eat them before he can
52924c413128SStephen M. Cameron 	 * get to them, so that while there is always a free one, a
52934c413128SStephen M. Cameron 	 * very unlucky thread might be starved anyway, never able to
52944c413128SStephen M. Cameron 	 * beat the other threads.  In reality, this happens so
52954c413128SStephen M. Cameron 	 * infrequently as to be indistinguishable from never.
52964c413128SStephen M. Cameron 	 */
52974c413128SStephen M. Cameron 
529833811026SRobert Elliott 	offset = h->last_allocation; /* benignly racy */
5299281a7fd0SWebb Scales 	for (;;) {
5300281a7fd0SWebb Scales 		i = find_next_zero_bit(h->cmd_pool_bits, h->nr_cmds, offset);
5301281a7fd0SWebb Scales 		if (unlikely(i == h->nr_cmds)) {
5302281a7fd0SWebb Scales 			offset = 0;
5303281a7fd0SWebb Scales 			continue;
5304281a7fd0SWebb Scales 		}
5305edd16368SStephen M. Cameron 		c = h->cmd_pool + i;
5306281a7fd0SWebb Scales 		refcount = atomic_inc_return(&c->refcount);
5307281a7fd0SWebb Scales 		if (unlikely(refcount > 1)) {
5308281a7fd0SWebb Scales 			cmd_free(h, c); /* already in use */
5309281a7fd0SWebb Scales 			offset = (i + 1) % h->nr_cmds;
5310281a7fd0SWebb Scales 			continue;
5311281a7fd0SWebb Scales 		}
5312281a7fd0SWebb Scales 		set_bit(i & (BITS_PER_LONG - 1),
5313281a7fd0SWebb Scales 			h->cmd_pool_bits + (i / BITS_PER_LONG));
5314281a7fd0SWebb Scales 		break; /* it's ours now. */
5315281a7fd0SWebb Scales 	}
531633811026SRobert Elliott 	h->last_allocation = i; /* benignly racy */
5317360c73bdSStephen Cameron 	hpsa_cmd_partial_init(h, i, c);
5318edd16368SStephen M. Cameron 	return c;
5319edd16368SStephen M. Cameron }
5320edd16368SStephen M. Cameron 
5321edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c)
5322edd16368SStephen M. Cameron {
5323281a7fd0SWebb Scales 	if (atomic_dec_and_test(&c->refcount)) {
5324edd16368SStephen M. Cameron 		int i;
5325edd16368SStephen M. Cameron 
5326edd16368SStephen M. Cameron 		i = c - h->cmd_pool;
5327edd16368SStephen M. Cameron 		clear_bit(i & (BITS_PER_LONG - 1),
5328edd16368SStephen M. Cameron 			  h->cmd_pool_bits + (i / BITS_PER_LONG));
5329edd16368SStephen M. Cameron 	}
5330281a7fd0SWebb Scales }
5331edd16368SStephen M. Cameron 
5332edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
5333edd16368SStephen M. Cameron 
533442a91641SDon Brace static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd,
533542a91641SDon Brace 	void __user *arg)
5336edd16368SStephen M. Cameron {
5337edd16368SStephen M. Cameron 	IOCTL32_Command_struct __user *arg32 =
5338edd16368SStephen M. Cameron 	    (IOCTL32_Command_struct __user *) arg;
5339edd16368SStephen M. Cameron 	IOCTL_Command_struct arg64;
5340edd16368SStephen M. Cameron 	IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
5341edd16368SStephen M. Cameron 	int err;
5342edd16368SStephen M. Cameron 	u32 cp;
5343edd16368SStephen M. Cameron 
5344938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
5345edd16368SStephen M. Cameron 	err = 0;
5346edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
5347edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
5348edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
5349edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
5350edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
5351edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
5352edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
5353edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
5354edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
5355edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
5356edd16368SStephen M. Cameron 
5357edd16368SStephen M. Cameron 	if (err)
5358edd16368SStephen M. Cameron 		return -EFAULT;
5359edd16368SStephen M. Cameron 
536042a91641SDon Brace 	err = hpsa_ioctl(dev, CCISS_PASSTHRU, p);
5361edd16368SStephen M. Cameron 	if (err)
5362edd16368SStephen M. Cameron 		return err;
5363edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
5364edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
5365edd16368SStephen M. Cameron 	if (err)
5366edd16368SStephen M. Cameron 		return -EFAULT;
5367edd16368SStephen M. Cameron 	return err;
5368edd16368SStephen M. Cameron }
5369edd16368SStephen M. Cameron 
5370edd16368SStephen M. Cameron static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
537142a91641SDon Brace 	int cmd, void __user *arg)
5372edd16368SStephen M. Cameron {
5373edd16368SStephen M. Cameron 	BIG_IOCTL32_Command_struct __user *arg32 =
5374edd16368SStephen M. Cameron 	    (BIG_IOCTL32_Command_struct __user *) arg;
5375edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct arg64;
5376edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct __user *p =
5377edd16368SStephen M. Cameron 	    compat_alloc_user_space(sizeof(arg64));
5378edd16368SStephen M. Cameron 	int err;
5379edd16368SStephen M. Cameron 	u32 cp;
5380edd16368SStephen M. Cameron 
5381938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
5382edd16368SStephen M. Cameron 	err = 0;
5383edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
5384edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
5385edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
5386edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
5387edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
5388edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
5389edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
5390edd16368SStephen M. Cameron 	err |= get_user(arg64.malloc_size, &arg32->malloc_size);
5391edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
5392edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
5393edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
5394edd16368SStephen M. Cameron 
5395edd16368SStephen M. Cameron 	if (err)
5396edd16368SStephen M. Cameron 		return -EFAULT;
5397edd16368SStephen M. Cameron 
539842a91641SDon Brace 	err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, p);
5399edd16368SStephen M. Cameron 	if (err)
5400edd16368SStephen M. Cameron 		return err;
5401edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
5402edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
5403edd16368SStephen M. Cameron 	if (err)
5404edd16368SStephen M. Cameron 		return -EFAULT;
5405edd16368SStephen M. Cameron 	return err;
5406edd16368SStephen M. Cameron }
540771fe75a7SStephen M. Cameron 
540842a91641SDon Brace static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
540971fe75a7SStephen M. Cameron {
541071fe75a7SStephen M. Cameron 	switch (cmd) {
541171fe75a7SStephen M. Cameron 	case CCISS_GETPCIINFO:
541271fe75a7SStephen M. Cameron 	case CCISS_GETINTINFO:
541371fe75a7SStephen M. Cameron 	case CCISS_SETINTINFO:
541471fe75a7SStephen M. Cameron 	case CCISS_GETNODENAME:
541571fe75a7SStephen M. Cameron 	case CCISS_SETNODENAME:
541671fe75a7SStephen M. Cameron 	case CCISS_GETHEARTBEAT:
541771fe75a7SStephen M. Cameron 	case CCISS_GETBUSTYPES:
541871fe75a7SStephen M. Cameron 	case CCISS_GETFIRMVER:
541971fe75a7SStephen M. Cameron 	case CCISS_GETDRIVVER:
542071fe75a7SStephen M. Cameron 	case CCISS_REVALIDVOLS:
542171fe75a7SStephen M. Cameron 	case CCISS_DEREGDISK:
542271fe75a7SStephen M. Cameron 	case CCISS_REGNEWDISK:
542371fe75a7SStephen M. Cameron 	case CCISS_REGNEWD:
542471fe75a7SStephen M. Cameron 	case CCISS_RESCANDISK:
542571fe75a7SStephen M. Cameron 	case CCISS_GETLUNINFO:
542671fe75a7SStephen M. Cameron 		return hpsa_ioctl(dev, cmd, arg);
542771fe75a7SStephen M. Cameron 
542871fe75a7SStephen M. Cameron 	case CCISS_PASSTHRU32:
542971fe75a7SStephen M. Cameron 		return hpsa_ioctl32_passthru(dev, cmd, arg);
543071fe75a7SStephen M. Cameron 	case CCISS_BIG_PASSTHRU32:
543171fe75a7SStephen M. Cameron 		return hpsa_ioctl32_big_passthru(dev, cmd, arg);
543271fe75a7SStephen M. Cameron 
543371fe75a7SStephen M. Cameron 	default:
543471fe75a7SStephen M. Cameron 		return -ENOIOCTLCMD;
543571fe75a7SStephen M. Cameron 	}
543671fe75a7SStephen M. Cameron }
5437edd16368SStephen M. Cameron #endif
5438edd16368SStephen M. Cameron 
5439edd16368SStephen M. Cameron static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
5440edd16368SStephen M. Cameron {
5441edd16368SStephen M. Cameron 	struct hpsa_pci_info pciinfo;
5442edd16368SStephen M. Cameron 
5443edd16368SStephen M. Cameron 	if (!argp)
5444edd16368SStephen M. Cameron 		return -EINVAL;
5445edd16368SStephen M. Cameron 	pciinfo.domain = pci_domain_nr(h->pdev->bus);
5446edd16368SStephen M. Cameron 	pciinfo.bus = h->pdev->bus->number;
5447edd16368SStephen M. Cameron 	pciinfo.dev_fn = h->pdev->devfn;
5448edd16368SStephen M. Cameron 	pciinfo.board_id = h->board_id;
5449edd16368SStephen M. Cameron 	if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
5450edd16368SStephen M. Cameron 		return -EFAULT;
5451edd16368SStephen M. Cameron 	return 0;
5452edd16368SStephen M. Cameron }
5453edd16368SStephen M. Cameron 
5454edd16368SStephen M. Cameron static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
5455edd16368SStephen M. Cameron {
5456edd16368SStephen M. Cameron 	DriverVer_type DriverVer;
5457edd16368SStephen M. Cameron 	unsigned char vmaj, vmin, vsubmin;
5458edd16368SStephen M. Cameron 	int rc;
5459edd16368SStephen M. Cameron 
5460edd16368SStephen M. Cameron 	rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
5461edd16368SStephen M. Cameron 		&vmaj, &vmin, &vsubmin);
5462edd16368SStephen M. Cameron 	if (rc != 3) {
5463edd16368SStephen M. Cameron 		dev_info(&h->pdev->dev, "driver version string '%s' "
5464edd16368SStephen M. Cameron 			"unrecognized.", HPSA_DRIVER_VERSION);
5465edd16368SStephen M. Cameron 		vmaj = 0;
5466edd16368SStephen M. Cameron 		vmin = 0;
5467edd16368SStephen M. Cameron 		vsubmin = 0;
5468edd16368SStephen M. Cameron 	}
5469edd16368SStephen M. Cameron 	DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
5470edd16368SStephen M. Cameron 	if (!argp)
5471edd16368SStephen M. Cameron 		return -EINVAL;
5472edd16368SStephen M. Cameron 	if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
5473edd16368SStephen M. Cameron 		return -EFAULT;
5474edd16368SStephen M. Cameron 	return 0;
5475edd16368SStephen M. Cameron }
5476edd16368SStephen M. Cameron 
5477edd16368SStephen M. Cameron static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
5478edd16368SStephen M. Cameron {
5479edd16368SStephen M. Cameron 	IOCTL_Command_struct iocommand;
5480edd16368SStephen M. Cameron 	struct CommandList *c;
5481edd16368SStephen M. Cameron 	char *buff = NULL;
548250a0decfSStephen M. Cameron 	u64 temp64;
5483c1f63c8fSStephen M. Cameron 	int rc = 0;
5484edd16368SStephen M. Cameron 
5485edd16368SStephen M. Cameron 	if (!argp)
5486edd16368SStephen M. Cameron 		return -EINVAL;
5487edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
5488edd16368SStephen M. Cameron 		return -EPERM;
5489edd16368SStephen M. Cameron 	if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
5490edd16368SStephen M. Cameron 		return -EFAULT;
5491edd16368SStephen M. Cameron 	if ((iocommand.buf_size < 1) &&
5492edd16368SStephen M. Cameron 	    (iocommand.Request.Type.Direction != XFER_NONE)) {
5493edd16368SStephen M. Cameron 		return -EINVAL;
5494edd16368SStephen M. Cameron 	}
5495edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
5496edd16368SStephen M. Cameron 		buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
5497edd16368SStephen M. Cameron 		if (buff == NULL)
54982dd02d74SRobert Elliott 			return -ENOMEM;
54999233fb10SStephen M. Cameron 		if (iocommand.Request.Type.Direction & XFER_WRITE) {
5500edd16368SStephen M. Cameron 			/* Copy the data into the buffer we created */
5501b03a7771SStephen M. Cameron 			if (copy_from_user(buff, iocommand.buf,
5502b03a7771SStephen M. Cameron 				iocommand.buf_size)) {
5503c1f63c8fSStephen M. Cameron 				rc = -EFAULT;
5504c1f63c8fSStephen M. Cameron 				goto out_kfree;
5505edd16368SStephen M. Cameron 			}
5506b03a7771SStephen M. Cameron 		} else {
5507edd16368SStephen M. Cameron 			memset(buff, 0, iocommand.buf_size);
5508b03a7771SStephen M. Cameron 		}
5509b03a7771SStephen M. Cameron 	}
551045fcb86eSStephen Cameron 	c = cmd_alloc(h);
5511bf43caf3SRobert Elliott 
5512edd16368SStephen M. Cameron 	/* Fill in the command type */
5513edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5514edd16368SStephen M. Cameron 	/* Fill in Command Header */
5515edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0; /* unused in simple mode */
5516edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {	/* buffer to fill */
5517edd16368SStephen M. Cameron 		c->Header.SGList = 1;
551850a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(1);
5519edd16368SStephen M. Cameron 	} else	{ /* no buffers to fill */
5520edd16368SStephen M. Cameron 		c->Header.SGList = 0;
552150a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(0);
5522edd16368SStephen M. Cameron 	}
5523edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
5524edd16368SStephen M. Cameron 
5525edd16368SStephen M. Cameron 	/* Fill in Request block */
5526edd16368SStephen M. Cameron 	memcpy(&c->Request, &iocommand.Request,
5527edd16368SStephen M. Cameron 		sizeof(c->Request));
5528edd16368SStephen M. Cameron 
5529edd16368SStephen M. Cameron 	/* Fill in the scatter gather information */
5530edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
553150a0decfSStephen M. Cameron 		temp64 = pci_map_single(h->pdev, buff,
5532edd16368SStephen M. Cameron 			iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
553350a0decfSStephen M. Cameron 		if (dma_mapping_error(&h->pdev->dev, (dma_addr_t) temp64)) {
553450a0decfSStephen M. Cameron 			c->SG[0].Addr = cpu_to_le64(0);
553550a0decfSStephen M. Cameron 			c->SG[0].Len = cpu_to_le32(0);
5536bcc48ffaSStephen M. Cameron 			rc = -ENOMEM;
5537bcc48ffaSStephen M. Cameron 			goto out;
5538bcc48ffaSStephen M. Cameron 		}
553950a0decfSStephen M. Cameron 		c->SG[0].Addr = cpu_to_le64(temp64);
554050a0decfSStephen M. Cameron 		c->SG[0].Len = cpu_to_le32(iocommand.buf_size);
554150a0decfSStephen M. Cameron 		c->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* not chaining */
5542edd16368SStephen M. Cameron 	}
554325163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
5544c2dd32e0SStephen M. Cameron 	if (iocommand.buf_size > 0)
5545edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
5546edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
554725163bd5SWebb Scales 	if (rc) {
554825163bd5SWebb Scales 		rc = -EIO;
554925163bd5SWebb Scales 		goto out;
555025163bd5SWebb Scales 	}
5551edd16368SStephen M. Cameron 
5552edd16368SStephen M. Cameron 	/* Copy the error information out */
5553edd16368SStephen M. Cameron 	memcpy(&iocommand.error_info, c->err_info,
5554edd16368SStephen M. Cameron 		sizeof(iocommand.error_info));
5555edd16368SStephen M. Cameron 	if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
5556c1f63c8fSStephen M. Cameron 		rc = -EFAULT;
5557c1f63c8fSStephen M. Cameron 		goto out;
5558edd16368SStephen M. Cameron 	}
55599233fb10SStephen M. Cameron 	if ((iocommand.Request.Type.Direction & XFER_READ) &&
5560b03a7771SStephen M. Cameron 		iocommand.buf_size > 0) {
5561edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
5562edd16368SStephen M. Cameron 		if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
5563c1f63c8fSStephen M. Cameron 			rc = -EFAULT;
5564c1f63c8fSStephen M. Cameron 			goto out;
5565edd16368SStephen M. Cameron 		}
5566edd16368SStephen M. Cameron 	}
5567c1f63c8fSStephen M. Cameron out:
556845fcb86eSStephen Cameron 	cmd_free(h, c);
5569c1f63c8fSStephen M. Cameron out_kfree:
5570c1f63c8fSStephen M. Cameron 	kfree(buff);
5571c1f63c8fSStephen M. Cameron 	return rc;
5572edd16368SStephen M. Cameron }
5573edd16368SStephen M. Cameron 
5574edd16368SStephen M. Cameron static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
5575edd16368SStephen M. Cameron {
5576edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct *ioc;
5577edd16368SStephen M. Cameron 	struct CommandList *c;
5578edd16368SStephen M. Cameron 	unsigned char **buff = NULL;
5579edd16368SStephen M. Cameron 	int *buff_size = NULL;
558050a0decfSStephen M. Cameron 	u64 temp64;
5581edd16368SStephen M. Cameron 	BYTE sg_used = 0;
5582edd16368SStephen M. Cameron 	int status = 0;
558301a02ffcSStephen M. Cameron 	u32 left;
558401a02ffcSStephen M. Cameron 	u32 sz;
5585edd16368SStephen M. Cameron 	BYTE __user *data_ptr;
5586edd16368SStephen M. Cameron 
5587edd16368SStephen M. Cameron 	if (!argp)
5588edd16368SStephen M. Cameron 		return -EINVAL;
5589edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
5590edd16368SStephen M. Cameron 		return -EPERM;
5591edd16368SStephen M. Cameron 	ioc = (BIG_IOCTL_Command_struct *)
5592edd16368SStephen M. Cameron 	    kmalloc(sizeof(*ioc), GFP_KERNEL);
5593edd16368SStephen M. Cameron 	if (!ioc) {
5594edd16368SStephen M. Cameron 		status = -ENOMEM;
5595edd16368SStephen M. Cameron 		goto cleanup1;
5596edd16368SStephen M. Cameron 	}
5597edd16368SStephen M. Cameron 	if (copy_from_user(ioc, argp, sizeof(*ioc))) {
5598edd16368SStephen M. Cameron 		status = -EFAULT;
5599edd16368SStephen M. Cameron 		goto cleanup1;
5600edd16368SStephen M. Cameron 	}
5601edd16368SStephen M. Cameron 	if ((ioc->buf_size < 1) &&
5602edd16368SStephen M. Cameron 	    (ioc->Request.Type.Direction != XFER_NONE)) {
5603edd16368SStephen M. Cameron 		status = -EINVAL;
5604edd16368SStephen M. Cameron 		goto cleanup1;
5605edd16368SStephen M. Cameron 	}
5606edd16368SStephen M. Cameron 	/* Check kmalloc limits  using all SGs */
5607edd16368SStephen M. Cameron 	if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
5608edd16368SStephen M. Cameron 		status = -EINVAL;
5609edd16368SStephen M. Cameron 		goto cleanup1;
5610edd16368SStephen M. Cameron 	}
5611d66ae08bSStephen M. Cameron 	if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
5612edd16368SStephen M. Cameron 		status = -EINVAL;
5613edd16368SStephen M. Cameron 		goto cleanup1;
5614edd16368SStephen M. Cameron 	}
5615d66ae08bSStephen M. Cameron 	buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
5616edd16368SStephen M. Cameron 	if (!buff) {
5617edd16368SStephen M. Cameron 		status = -ENOMEM;
5618edd16368SStephen M. Cameron 		goto cleanup1;
5619edd16368SStephen M. Cameron 	}
5620d66ae08bSStephen M. Cameron 	buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
5621edd16368SStephen M. Cameron 	if (!buff_size) {
5622edd16368SStephen M. Cameron 		status = -ENOMEM;
5623edd16368SStephen M. Cameron 		goto cleanup1;
5624edd16368SStephen M. Cameron 	}
5625edd16368SStephen M. Cameron 	left = ioc->buf_size;
5626edd16368SStephen M. Cameron 	data_ptr = ioc->buf;
5627edd16368SStephen M. Cameron 	while (left) {
5628edd16368SStephen M. Cameron 		sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
5629edd16368SStephen M. Cameron 		buff_size[sg_used] = sz;
5630edd16368SStephen M. Cameron 		buff[sg_used] = kmalloc(sz, GFP_KERNEL);
5631edd16368SStephen M. Cameron 		if (buff[sg_used] == NULL) {
5632edd16368SStephen M. Cameron 			status = -ENOMEM;
5633edd16368SStephen M. Cameron 			goto cleanup1;
5634edd16368SStephen M. Cameron 		}
56359233fb10SStephen M. Cameron 		if (ioc->Request.Type.Direction & XFER_WRITE) {
5636edd16368SStephen M. Cameron 			if (copy_from_user(buff[sg_used], data_ptr, sz)) {
56370758f4f7SStephen M. Cameron 				status = -EFAULT;
5638edd16368SStephen M. Cameron 				goto cleanup1;
5639edd16368SStephen M. Cameron 			}
5640edd16368SStephen M. Cameron 		} else
5641edd16368SStephen M. Cameron 			memset(buff[sg_used], 0, sz);
5642edd16368SStephen M. Cameron 		left -= sz;
5643edd16368SStephen M. Cameron 		data_ptr += sz;
5644edd16368SStephen M. Cameron 		sg_used++;
5645edd16368SStephen M. Cameron 	}
564645fcb86eSStephen Cameron 	c = cmd_alloc(h);
5647bf43caf3SRobert Elliott 
5648edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5649edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
565050a0decfSStephen M. Cameron 	c->Header.SGList = (u8) sg_used;
565150a0decfSStephen M. Cameron 	c->Header.SGTotal = cpu_to_le16(sg_used);
5652edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
5653edd16368SStephen M. Cameron 	memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
5654edd16368SStephen M. Cameron 	if (ioc->buf_size > 0) {
5655edd16368SStephen M. Cameron 		int i;
5656edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
565750a0decfSStephen M. Cameron 			temp64 = pci_map_single(h->pdev, buff[i],
5658edd16368SStephen M. Cameron 				    buff_size[i], PCI_DMA_BIDIRECTIONAL);
565950a0decfSStephen M. Cameron 			if (dma_mapping_error(&h->pdev->dev,
566050a0decfSStephen M. Cameron 							(dma_addr_t) temp64)) {
566150a0decfSStephen M. Cameron 				c->SG[i].Addr = cpu_to_le64(0);
566250a0decfSStephen M. Cameron 				c->SG[i].Len = cpu_to_le32(0);
5663bcc48ffaSStephen M. Cameron 				hpsa_pci_unmap(h->pdev, c, i,
5664bcc48ffaSStephen M. Cameron 					PCI_DMA_BIDIRECTIONAL);
5665bcc48ffaSStephen M. Cameron 				status = -ENOMEM;
5666e2d4a1f6SStephen M. Cameron 				goto cleanup0;
5667bcc48ffaSStephen M. Cameron 			}
566850a0decfSStephen M. Cameron 			c->SG[i].Addr = cpu_to_le64(temp64);
566950a0decfSStephen M. Cameron 			c->SG[i].Len = cpu_to_le32(buff_size[i]);
567050a0decfSStephen M. Cameron 			c->SG[i].Ext = cpu_to_le32(0);
5671edd16368SStephen M. Cameron 		}
567250a0decfSStephen M. Cameron 		c->SG[--i].Ext = cpu_to_le32(HPSA_SG_LAST);
5673edd16368SStephen M. Cameron 	}
567425163bd5SWebb Scales 	status = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
5675b03a7771SStephen M. Cameron 	if (sg_used)
5676edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
5677edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
567825163bd5SWebb Scales 	if (status) {
567925163bd5SWebb Scales 		status = -EIO;
568025163bd5SWebb Scales 		goto cleanup0;
568125163bd5SWebb Scales 	}
568225163bd5SWebb Scales 
5683edd16368SStephen M. Cameron 	/* Copy the error information out */
5684edd16368SStephen M. Cameron 	memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
5685edd16368SStephen M. Cameron 	if (copy_to_user(argp, ioc, sizeof(*ioc))) {
5686edd16368SStephen M. Cameron 		status = -EFAULT;
5687e2d4a1f6SStephen M. Cameron 		goto cleanup0;
5688edd16368SStephen M. Cameron 	}
56899233fb10SStephen M. Cameron 	if ((ioc->Request.Type.Direction & XFER_READ) && ioc->buf_size > 0) {
56902b08b3e9SDon Brace 		int i;
56912b08b3e9SDon Brace 
5692edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
5693edd16368SStephen M. Cameron 		BYTE __user *ptr = ioc->buf;
5694edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
5695edd16368SStephen M. Cameron 			if (copy_to_user(ptr, buff[i], buff_size[i])) {
5696edd16368SStephen M. Cameron 				status = -EFAULT;
5697e2d4a1f6SStephen M. Cameron 				goto cleanup0;
5698edd16368SStephen M. Cameron 			}
5699edd16368SStephen M. Cameron 			ptr += buff_size[i];
5700edd16368SStephen M. Cameron 		}
5701edd16368SStephen M. Cameron 	}
5702edd16368SStephen M. Cameron 	status = 0;
5703e2d4a1f6SStephen M. Cameron cleanup0:
570445fcb86eSStephen Cameron 	cmd_free(h, c);
5705edd16368SStephen M. Cameron cleanup1:
5706edd16368SStephen M. Cameron 	if (buff) {
57072b08b3e9SDon Brace 		int i;
57082b08b3e9SDon Brace 
5709edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++)
5710edd16368SStephen M. Cameron 			kfree(buff[i]);
5711edd16368SStephen M. Cameron 		kfree(buff);
5712edd16368SStephen M. Cameron 	}
5713edd16368SStephen M. Cameron 	kfree(buff_size);
5714edd16368SStephen M. Cameron 	kfree(ioc);
5715edd16368SStephen M. Cameron 	return status;
5716edd16368SStephen M. Cameron }
5717edd16368SStephen M. Cameron 
5718edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
5719edd16368SStephen M. Cameron 	struct CommandList *c)
5720edd16368SStephen M. Cameron {
5721edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
5722edd16368SStephen M. Cameron 			c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
5723edd16368SStephen M. Cameron 		(void) check_for_unit_attention(h, c);
5724edd16368SStephen M. Cameron }
57250390f0c0SStephen M. Cameron 
5726edd16368SStephen M. Cameron /*
5727edd16368SStephen M. Cameron  * ioctl
5728edd16368SStephen M. Cameron  */
572942a91641SDon Brace static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
5730edd16368SStephen M. Cameron {
5731edd16368SStephen M. Cameron 	struct ctlr_info *h;
5732edd16368SStephen M. Cameron 	void __user *argp = (void __user *)arg;
57330390f0c0SStephen M. Cameron 	int rc;
5734edd16368SStephen M. Cameron 
5735edd16368SStephen M. Cameron 	h = sdev_to_hba(dev);
5736edd16368SStephen M. Cameron 
5737edd16368SStephen M. Cameron 	switch (cmd) {
5738edd16368SStephen M. Cameron 	case CCISS_DEREGDISK:
5739edd16368SStephen M. Cameron 	case CCISS_REGNEWDISK:
5740edd16368SStephen M. Cameron 	case CCISS_REGNEWD:
5741a08a8471SStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
5742edd16368SStephen M. Cameron 		return 0;
5743edd16368SStephen M. Cameron 	case CCISS_GETPCIINFO:
5744edd16368SStephen M. Cameron 		return hpsa_getpciinfo_ioctl(h, argp);
5745edd16368SStephen M. Cameron 	case CCISS_GETDRIVVER:
5746edd16368SStephen M. Cameron 		return hpsa_getdrivver_ioctl(h, argp);
5747edd16368SStephen M. Cameron 	case CCISS_PASSTHRU:
574834f0c627SDon Brace 		if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
57490390f0c0SStephen M. Cameron 			return -EAGAIN;
57500390f0c0SStephen M. Cameron 		rc = hpsa_passthru_ioctl(h, argp);
575134f0c627SDon Brace 		atomic_inc(&h->passthru_cmds_avail);
57520390f0c0SStephen M. Cameron 		return rc;
5753edd16368SStephen M. Cameron 	case CCISS_BIG_PASSTHRU:
575434f0c627SDon Brace 		if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
57550390f0c0SStephen M. Cameron 			return -EAGAIN;
57560390f0c0SStephen M. Cameron 		rc = hpsa_big_passthru_ioctl(h, argp);
575734f0c627SDon Brace 		atomic_inc(&h->passthru_cmds_avail);
57580390f0c0SStephen M. Cameron 		return rc;
5759edd16368SStephen M. Cameron 	default:
5760edd16368SStephen M. Cameron 		return -ENOTTY;
5761edd16368SStephen M. Cameron 	}
5762edd16368SStephen M. Cameron }
5763edd16368SStephen M. Cameron 
5764bf43caf3SRobert Elliott static void hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
57656f039790SGreg Kroah-Hartman 				u8 reset_type)
576664670ac8SStephen M. Cameron {
576764670ac8SStephen M. Cameron 	struct CommandList *c;
576864670ac8SStephen M. Cameron 
576964670ac8SStephen M. Cameron 	c = cmd_alloc(h);
5770bf43caf3SRobert Elliott 
5771a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map */
5772a2dac136SStephen M. Cameron 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
577364670ac8SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_MSG);
577464670ac8SStephen M. Cameron 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
577564670ac8SStephen M. Cameron 	c->waiting = NULL;
577664670ac8SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
577764670ac8SStephen M. Cameron 	/* Don't wait for completion, the reset won't complete.  Don't free
577864670ac8SStephen M. Cameron 	 * the command either.  This is the last command we will send before
577964670ac8SStephen M. Cameron 	 * re-initializing everything, so it doesn't matter and won't leak.
578064670ac8SStephen M. Cameron 	 */
5781bf43caf3SRobert Elliott 	return;
578264670ac8SStephen M. Cameron }
578364670ac8SStephen M. Cameron 
5784a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
5785b7bb24ebSStephen M. Cameron 	void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
5786edd16368SStephen M. Cameron 	int cmd_type)
5787edd16368SStephen M. Cameron {
5788edd16368SStephen M. Cameron 	int pci_dir = XFER_NONE;
57899b5c48c2SStephen Cameron 	u64 tag; /* for commands to be aborted */
5790edd16368SStephen M. Cameron 
5791edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5792edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
5793edd16368SStephen M. Cameron 	if (buff != NULL && size > 0) {
5794edd16368SStephen M. Cameron 		c->Header.SGList = 1;
579550a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(1);
5796edd16368SStephen M. Cameron 	} else {
5797edd16368SStephen M. Cameron 		c->Header.SGList = 0;
579850a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(0);
5799edd16368SStephen M. Cameron 	}
5800edd16368SStephen M. Cameron 	memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
5801edd16368SStephen M. Cameron 
5802edd16368SStephen M. Cameron 	if (cmd_type == TYPE_CMD) {
5803edd16368SStephen M. Cameron 		switch (cmd) {
5804edd16368SStephen M. Cameron 		case HPSA_INQUIRY:
5805edd16368SStephen M. Cameron 			/* are we trying to read a vital product page */
5806b7bb24ebSStephen M. Cameron 			if (page_code & VPD_PAGE) {
5807edd16368SStephen M. Cameron 				c->Request.CDB[1] = 0x01;
5808b7bb24ebSStephen M. Cameron 				c->Request.CDB[2] = (page_code & 0xff);
5809edd16368SStephen M. Cameron 			}
5810edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
5811a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5812a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5813edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5814edd16368SStephen M. Cameron 			c->Request.CDB[0] = HPSA_INQUIRY;
5815edd16368SStephen M. Cameron 			c->Request.CDB[4] = size & 0xFF;
5816edd16368SStephen M. Cameron 			break;
5817edd16368SStephen M. Cameron 		case HPSA_REPORT_LOG:
5818edd16368SStephen M. Cameron 		case HPSA_REPORT_PHYS:
5819edd16368SStephen M. Cameron 			/* Talking to controller so It's a physical command
5820edd16368SStephen M. Cameron 			   mode = 00 target = 0.  Nothing to write.
5821edd16368SStephen M. Cameron 			 */
5822edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
5823a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5824a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5825edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5826edd16368SStephen M. Cameron 			c->Request.CDB[0] = cmd;
5827edd16368SStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
5828edd16368SStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5829edd16368SStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5830edd16368SStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
5831edd16368SStephen M. Cameron 			break;
5832edd16368SStephen M. Cameron 		case HPSA_CACHE_FLUSH:
5833edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
5834a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5835a505b86fSStephen M. Cameron 					TYPE_ATTR_DIR(cmd_type,
5836a505b86fSStephen M. Cameron 						ATTR_SIMPLE, XFER_WRITE);
5837edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5838edd16368SStephen M. Cameron 			c->Request.CDB[0] = BMIC_WRITE;
5839edd16368SStephen M. Cameron 			c->Request.CDB[6] = BMIC_CACHE_FLUSH;
5840bb158eabSStephen M. Cameron 			c->Request.CDB[7] = (size >> 8) & 0xFF;
5841bb158eabSStephen M. Cameron 			c->Request.CDB[8] = size & 0xFF;
5842edd16368SStephen M. Cameron 			break;
5843edd16368SStephen M. Cameron 		case TEST_UNIT_READY:
5844edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
5845a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5846a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
5847edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5848edd16368SStephen M. Cameron 			break;
5849283b4a9bSStephen M. Cameron 		case HPSA_GET_RAID_MAP:
5850283b4a9bSStephen M. Cameron 			c->Request.CDBLen = 12;
5851a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5852a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5853283b4a9bSStephen M. Cameron 			c->Request.Timeout = 0;
5854283b4a9bSStephen M. Cameron 			c->Request.CDB[0] = HPSA_CISS_READ;
5855283b4a9bSStephen M. Cameron 			c->Request.CDB[1] = cmd;
5856283b4a9bSStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
5857283b4a9bSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5858283b4a9bSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5859283b4a9bSStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
5860283b4a9bSStephen M. Cameron 			break;
5861316b221aSStephen M. Cameron 		case BMIC_SENSE_CONTROLLER_PARAMETERS:
5862316b221aSStephen M. Cameron 			c->Request.CDBLen = 10;
5863a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5864a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5865316b221aSStephen M. Cameron 			c->Request.Timeout = 0;
5866316b221aSStephen M. Cameron 			c->Request.CDB[0] = BMIC_READ;
5867316b221aSStephen M. Cameron 			c->Request.CDB[6] = BMIC_SENSE_CONTROLLER_PARAMETERS;
5868316b221aSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5869316b221aSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5870316b221aSStephen M. Cameron 			break;
587103383736SDon Brace 		case BMIC_IDENTIFY_PHYSICAL_DEVICE:
587203383736SDon Brace 			c->Request.CDBLen = 10;
587303383736SDon Brace 			c->Request.type_attr_dir =
587403383736SDon Brace 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
587503383736SDon Brace 			c->Request.Timeout = 0;
587603383736SDon Brace 			c->Request.CDB[0] = BMIC_READ;
587703383736SDon Brace 			c->Request.CDB[6] = BMIC_IDENTIFY_PHYSICAL_DEVICE;
587803383736SDon Brace 			c->Request.CDB[7] = (size >> 16) & 0xFF;
587903383736SDon Brace 			c->Request.CDB[8] = (size >> 8) & 0XFF;
588003383736SDon Brace 			break;
5881edd16368SStephen M. Cameron 		default:
5882edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
5883edd16368SStephen M. Cameron 			BUG();
5884a2dac136SStephen M. Cameron 			return -1;
5885edd16368SStephen M. Cameron 		}
5886edd16368SStephen M. Cameron 	} else if (cmd_type == TYPE_MSG) {
5887edd16368SStephen M. Cameron 		switch (cmd) {
5888edd16368SStephen M. Cameron 
5889edd16368SStephen M. Cameron 		case  HPSA_DEVICE_RESET_MSG:
5890edd16368SStephen M. Cameron 			c->Request.CDBLen = 16;
5891a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5892a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
5893edd16368SStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
589464670ac8SStephen M. Cameron 			memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
589564670ac8SStephen M. Cameron 			c->Request.CDB[0] =  cmd;
589621e89afdSStephen M. Cameron 			c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
5897edd16368SStephen M. Cameron 			/* If bytes 4-7 are zero, it means reset the */
5898edd16368SStephen M. Cameron 			/* LunID device */
5899edd16368SStephen M. Cameron 			c->Request.CDB[4] = 0x00;
5900edd16368SStephen M. Cameron 			c->Request.CDB[5] = 0x00;
5901edd16368SStephen M. Cameron 			c->Request.CDB[6] = 0x00;
5902edd16368SStephen M. Cameron 			c->Request.CDB[7] = 0x00;
5903edd16368SStephen M. Cameron 			break;
590475167d2cSStephen M. Cameron 		case  HPSA_ABORT_MSG:
59059b5c48c2SStephen Cameron 			memcpy(&tag, buff, sizeof(tag));
59062b08b3e9SDon Brace 			dev_dbg(&h->pdev->dev,
59079b5c48c2SStephen Cameron 				"Abort Tag:0x%016llx using rqst Tag:0x%016llx",
59089b5c48c2SStephen Cameron 				tag, c->Header.tag);
590975167d2cSStephen M. Cameron 			c->Request.CDBLen = 16;
5910a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5911a505b86fSStephen M. Cameron 					TYPE_ATTR_DIR(cmd_type,
5912a505b86fSStephen M. Cameron 						ATTR_SIMPLE, XFER_WRITE);
591375167d2cSStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
591475167d2cSStephen M. Cameron 			c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
591575167d2cSStephen M. Cameron 			c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
591675167d2cSStephen M. Cameron 			c->Request.CDB[2] = 0x00; /* reserved */
591775167d2cSStephen M. Cameron 			c->Request.CDB[3] = 0x00; /* reserved */
591875167d2cSStephen M. Cameron 			/* Tag to abort goes in CDB[4]-CDB[11] */
59199b5c48c2SStephen Cameron 			memcpy(&c->Request.CDB[4], &tag, sizeof(tag));
592075167d2cSStephen M. Cameron 			c->Request.CDB[12] = 0x00; /* reserved */
592175167d2cSStephen M. Cameron 			c->Request.CDB[13] = 0x00; /* reserved */
592275167d2cSStephen M. Cameron 			c->Request.CDB[14] = 0x00; /* reserved */
592375167d2cSStephen M. Cameron 			c->Request.CDB[15] = 0x00; /* reserved */
592475167d2cSStephen M. Cameron 		break;
5925edd16368SStephen M. Cameron 		default:
5926edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown message type %d\n",
5927edd16368SStephen M. Cameron 				cmd);
5928edd16368SStephen M. Cameron 			BUG();
5929edd16368SStephen M. Cameron 		}
5930edd16368SStephen M. Cameron 	} else {
5931edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
5932edd16368SStephen M. Cameron 		BUG();
5933edd16368SStephen M. Cameron 	}
5934edd16368SStephen M. Cameron 
5935a505b86fSStephen M. Cameron 	switch (GET_DIR(c->Request.type_attr_dir)) {
5936edd16368SStephen M. Cameron 	case XFER_READ:
5937edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_FROMDEVICE;
5938edd16368SStephen M. Cameron 		break;
5939edd16368SStephen M. Cameron 	case XFER_WRITE:
5940edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_TODEVICE;
5941edd16368SStephen M. Cameron 		break;
5942edd16368SStephen M. Cameron 	case XFER_NONE:
5943edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_NONE;
5944edd16368SStephen M. Cameron 		break;
5945edd16368SStephen M. Cameron 	default:
5946edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_BIDIRECTIONAL;
5947edd16368SStephen M. Cameron 	}
5948a2dac136SStephen M. Cameron 	if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
5949a2dac136SStephen M. Cameron 		return -1;
5950a2dac136SStephen M. Cameron 	return 0;
5951edd16368SStephen M. Cameron }
5952edd16368SStephen M. Cameron 
5953edd16368SStephen M. Cameron /*
5954edd16368SStephen M. Cameron  * Map (physical) PCI mem into (virtual) kernel space
5955edd16368SStephen M. Cameron  */
5956edd16368SStephen M. Cameron static void __iomem *remap_pci_mem(ulong base, ulong size)
5957edd16368SStephen M. Cameron {
5958edd16368SStephen M. Cameron 	ulong page_base = ((ulong) base) & PAGE_MASK;
5959edd16368SStephen M. Cameron 	ulong page_offs = ((ulong) base) - page_base;
5960088ba34cSStephen M. Cameron 	void __iomem *page_remapped = ioremap_nocache(page_base,
5961088ba34cSStephen M. Cameron 		page_offs + size);
5962edd16368SStephen M. Cameron 
5963edd16368SStephen M. Cameron 	return page_remapped ? (page_remapped + page_offs) : NULL;
5964edd16368SStephen M. Cameron }
5965edd16368SStephen M. Cameron 
5966254f796bSMatt Gates static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
5967edd16368SStephen M. Cameron {
5968254f796bSMatt Gates 	return h->access.command_completed(h, q);
5969edd16368SStephen M. Cameron }
5970edd16368SStephen M. Cameron 
5971900c5440SStephen M. Cameron static inline bool interrupt_pending(struct ctlr_info *h)
5972edd16368SStephen M. Cameron {
5973edd16368SStephen M. Cameron 	return h->access.intr_pending(h);
5974edd16368SStephen M. Cameron }
5975edd16368SStephen M. Cameron 
5976edd16368SStephen M. Cameron static inline long interrupt_not_for_us(struct ctlr_info *h)
5977edd16368SStephen M. Cameron {
597810f66018SStephen M. Cameron 	return (h->access.intr_pending(h) == 0) ||
597910f66018SStephen M. Cameron 		(h->interrupts_enabled == 0);
5980edd16368SStephen M. Cameron }
5981edd16368SStephen M. Cameron 
598201a02ffcSStephen M. Cameron static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
598301a02ffcSStephen M. Cameron 	u32 raw_tag)
5984edd16368SStephen M. Cameron {
5985edd16368SStephen M. Cameron 	if (unlikely(tag_index >= h->nr_cmds)) {
5986edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
5987edd16368SStephen M. Cameron 		return 1;
5988edd16368SStephen M. Cameron 	}
5989edd16368SStephen M. Cameron 	return 0;
5990edd16368SStephen M. Cameron }
5991edd16368SStephen M. Cameron 
59925a3d16f5SStephen M. Cameron static inline void finish_cmd(struct CommandList *c)
5993edd16368SStephen M. Cameron {
5994e85c5974SStephen M. Cameron 	dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
5995c349775eSScott Teel 	if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
5996c349775eSScott Teel 			|| c->cmd_type == CMD_IOACCEL2))
59971fb011fbSStephen M. Cameron 		complete_scsi_command(c);
59988be986ccSStephen Cameron 	else if (c->cmd_type == CMD_IOCTL_PEND || c->cmd_type == IOACCEL2_TMF)
5999edd16368SStephen M. Cameron 		complete(c->waiting);
6000a104c99fSStephen M. Cameron }
6001a104c99fSStephen M. Cameron 
6002a9a3a273SStephen M. Cameron 
6003a9a3a273SStephen M. Cameron static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
6004a104c99fSStephen M. Cameron {
6005a9a3a273SStephen M. Cameron #define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
6006a9a3a273SStephen M. Cameron #define HPSA_SIMPLE_ERROR_BITS 0x03
6007960a30e7SStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
6008a9a3a273SStephen M. Cameron 		return tag & ~HPSA_SIMPLE_ERROR_BITS;
6009a9a3a273SStephen M. Cameron 	return tag & ~HPSA_PERF_ERROR_BITS;
6010a104c99fSStephen M. Cameron }
6011a104c99fSStephen M. Cameron 
6012303932fdSDon Brace /* process completion of an indexed ("direct lookup") command */
60131d94f94dSStephen M. Cameron static inline void process_indexed_cmd(struct ctlr_info *h,
6014303932fdSDon Brace 	u32 raw_tag)
6015303932fdSDon Brace {
6016303932fdSDon Brace 	u32 tag_index;
6017303932fdSDon Brace 	struct CommandList *c;
6018303932fdSDon Brace 
6019f2405db8SDon Brace 	tag_index = raw_tag >> DIRECT_LOOKUP_SHIFT;
60201d94f94dSStephen M. Cameron 	if (!bad_tag(h, tag_index, raw_tag)) {
6021303932fdSDon Brace 		c = h->cmd_pool + tag_index;
60225a3d16f5SStephen M. Cameron 		finish_cmd(c);
60231d94f94dSStephen M. Cameron 	}
6024303932fdSDon Brace }
6025303932fdSDon Brace 
602664670ac8SStephen M. Cameron /* Some controllers, like p400, will give us one interrupt
602764670ac8SStephen M. Cameron  * after a soft reset, even if we turned interrupts off.
602864670ac8SStephen M. Cameron  * Only need to check for this in the hpsa_xxx_discard_completions
602964670ac8SStephen M. Cameron  * functions.
603064670ac8SStephen M. Cameron  */
603164670ac8SStephen M. Cameron static int ignore_bogus_interrupt(struct ctlr_info *h)
603264670ac8SStephen M. Cameron {
603364670ac8SStephen M. Cameron 	if (likely(!reset_devices))
603464670ac8SStephen M. Cameron 		return 0;
603564670ac8SStephen M. Cameron 
603664670ac8SStephen M. Cameron 	if (likely(h->interrupts_enabled))
603764670ac8SStephen M. Cameron 		return 0;
603864670ac8SStephen M. Cameron 
603964670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
604064670ac8SStephen M. Cameron 		"(known firmware bug.)  Ignoring.\n");
604164670ac8SStephen M. Cameron 
604264670ac8SStephen M. Cameron 	return 1;
604364670ac8SStephen M. Cameron }
604464670ac8SStephen M. Cameron 
6045254f796bSMatt Gates /*
6046254f796bSMatt Gates  * Convert &h->q[x] (passed to interrupt handlers) back to h.
6047254f796bSMatt Gates  * Relies on (h-q[x] == x) being true for x such that
6048254f796bSMatt Gates  * 0 <= x < MAX_REPLY_QUEUES.
6049254f796bSMatt Gates  */
6050254f796bSMatt Gates static struct ctlr_info *queue_to_hba(u8 *queue)
605164670ac8SStephen M. Cameron {
6052254f796bSMatt Gates 	return container_of((queue - *queue), struct ctlr_info, q[0]);
6053254f796bSMatt Gates }
6054254f796bSMatt Gates 
6055254f796bSMatt Gates static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
6056254f796bSMatt Gates {
6057254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
6058254f796bSMatt Gates 	u8 q = *(u8 *) queue;
605964670ac8SStephen M. Cameron 	u32 raw_tag;
606064670ac8SStephen M. Cameron 
606164670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
606264670ac8SStephen M. Cameron 		return IRQ_NONE;
606364670ac8SStephen M. Cameron 
606464670ac8SStephen M. Cameron 	if (interrupt_not_for_us(h))
606564670ac8SStephen M. Cameron 		return IRQ_NONE;
6066a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
606764670ac8SStephen M. Cameron 	while (interrupt_pending(h)) {
6068254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
606964670ac8SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY)
6070254f796bSMatt Gates 			raw_tag = next_command(h, q);
607164670ac8SStephen M. Cameron 	}
607264670ac8SStephen M. Cameron 	return IRQ_HANDLED;
607364670ac8SStephen M. Cameron }
607464670ac8SStephen M. Cameron 
6075254f796bSMatt Gates static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
607664670ac8SStephen M. Cameron {
6077254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
607864670ac8SStephen M. Cameron 	u32 raw_tag;
6079254f796bSMatt Gates 	u8 q = *(u8 *) queue;
608064670ac8SStephen M. Cameron 
608164670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
608264670ac8SStephen M. Cameron 		return IRQ_NONE;
608364670ac8SStephen M. Cameron 
6084a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
6085254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
608664670ac8SStephen M. Cameron 	while (raw_tag != FIFO_EMPTY)
6087254f796bSMatt Gates 		raw_tag = next_command(h, q);
608864670ac8SStephen M. Cameron 	return IRQ_HANDLED;
608964670ac8SStephen M. Cameron }
609064670ac8SStephen M. Cameron 
6091254f796bSMatt Gates static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
6092edd16368SStephen M. Cameron {
6093254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba((u8 *) queue);
6094303932fdSDon Brace 	u32 raw_tag;
6095254f796bSMatt Gates 	u8 q = *(u8 *) queue;
6096edd16368SStephen M. Cameron 
6097edd16368SStephen M. Cameron 	if (interrupt_not_for_us(h))
6098edd16368SStephen M. Cameron 		return IRQ_NONE;
6099a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
610010f66018SStephen M. Cameron 	while (interrupt_pending(h)) {
6101254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
610210f66018SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY) {
61031d94f94dSStephen M. Cameron 			process_indexed_cmd(h, raw_tag);
6104254f796bSMatt Gates 			raw_tag = next_command(h, q);
610510f66018SStephen M. Cameron 		}
610610f66018SStephen M. Cameron 	}
610710f66018SStephen M. Cameron 	return IRQ_HANDLED;
610810f66018SStephen M. Cameron }
610910f66018SStephen M. Cameron 
6110254f796bSMatt Gates static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
611110f66018SStephen M. Cameron {
6112254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
611310f66018SStephen M. Cameron 	u32 raw_tag;
6114254f796bSMatt Gates 	u8 q = *(u8 *) queue;
611510f66018SStephen M. Cameron 
6116a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
6117254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
6118303932fdSDon Brace 	while (raw_tag != FIFO_EMPTY) {
61191d94f94dSStephen M. Cameron 		process_indexed_cmd(h, raw_tag);
6120254f796bSMatt Gates 		raw_tag = next_command(h, q);
6121edd16368SStephen M. Cameron 	}
6122edd16368SStephen M. Cameron 	return IRQ_HANDLED;
6123edd16368SStephen M. Cameron }
6124edd16368SStephen M. Cameron 
6125a9a3a273SStephen M. Cameron /* Send a message CDB to the firmware. Careful, this only works
6126a9a3a273SStephen M. Cameron  * in simple mode, not performant mode due to the tag lookup.
6127a9a3a273SStephen M. Cameron  * We only ever use this immediately after a controller reset.
6128a9a3a273SStephen M. Cameron  */
61296f039790SGreg Kroah-Hartman static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
6130edd16368SStephen M. Cameron 			unsigned char type)
6131edd16368SStephen M. Cameron {
6132edd16368SStephen M. Cameron 	struct Command {
6133edd16368SStephen M. Cameron 		struct CommandListHeader CommandHeader;
6134edd16368SStephen M. Cameron 		struct RequestBlock Request;
6135edd16368SStephen M. Cameron 		struct ErrDescriptor ErrorDescriptor;
6136edd16368SStephen M. Cameron 	};
6137edd16368SStephen M. Cameron 	struct Command *cmd;
6138edd16368SStephen M. Cameron 	static const size_t cmd_sz = sizeof(*cmd) +
6139edd16368SStephen M. Cameron 					sizeof(cmd->ErrorDescriptor);
6140edd16368SStephen M. Cameron 	dma_addr_t paddr64;
61412b08b3e9SDon Brace 	__le32 paddr32;
61422b08b3e9SDon Brace 	u32 tag;
6143edd16368SStephen M. Cameron 	void __iomem *vaddr;
6144edd16368SStephen M. Cameron 	int i, err;
6145edd16368SStephen M. Cameron 
6146edd16368SStephen M. Cameron 	vaddr = pci_ioremap_bar(pdev, 0);
6147edd16368SStephen M. Cameron 	if (vaddr == NULL)
6148edd16368SStephen M. Cameron 		return -ENOMEM;
6149edd16368SStephen M. Cameron 
6150edd16368SStephen M. Cameron 	/* The Inbound Post Queue only accepts 32-bit physical addresses for the
6151edd16368SStephen M. Cameron 	 * CCISS commands, so they must be allocated from the lower 4GiB of
6152edd16368SStephen M. Cameron 	 * memory.
6153edd16368SStephen M. Cameron 	 */
6154edd16368SStephen M. Cameron 	err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
6155edd16368SStephen M. Cameron 	if (err) {
6156edd16368SStephen M. Cameron 		iounmap(vaddr);
61571eaec8f3SRobert Elliott 		return err;
6158edd16368SStephen M. Cameron 	}
6159edd16368SStephen M. Cameron 
6160edd16368SStephen M. Cameron 	cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
6161edd16368SStephen M. Cameron 	if (cmd == NULL) {
6162edd16368SStephen M. Cameron 		iounmap(vaddr);
6163edd16368SStephen M. Cameron 		return -ENOMEM;
6164edd16368SStephen M. Cameron 	}
6165edd16368SStephen M. Cameron 
6166edd16368SStephen M. Cameron 	/* This must fit, because of the 32-bit consistent DMA mask.  Also,
6167edd16368SStephen M. Cameron 	 * although there's no guarantee, we assume that the address is at
6168edd16368SStephen M. Cameron 	 * least 4-byte aligned (most likely, it's page-aligned).
6169edd16368SStephen M. Cameron 	 */
61702b08b3e9SDon Brace 	paddr32 = cpu_to_le32(paddr64);
6171edd16368SStephen M. Cameron 
6172edd16368SStephen M. Cameron 	cmd->CommandHeader.ReplyQueue = 0;
6173edd16368SStephen M. Cameron 	cmd->CommandHeader.SGList = 0;
617450a0decfSStephen M. Cameron 	cmd->CommandHeader.SGTotal = cpu_to_le16(0);
61752b08b3e9SDon Brace 	cmd->CommandHeader.tag = cpu_to_le64(paddr64);
6176edd16368SStephen M. Cameron 	memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
6177edd16368SStephen M. Cameron 
6178edd16368SStephen M. Cameron 	cmd->Request.CDBLen = 16;
6179a505b86fSStephen M. Cameron 	cmd->Request.type_attr_dir =
6180a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_MSG, ATTR_HEADOFQUEUE, XFER_NONE);
6181edd16368SStephen M. Cameron 	cmd->Request.Timeout = 0; /* Don't time out */
6182edd16368SStephen M. Cameron 	cmd->Request.CDB[0] = opcode;
6183edd16368SStephen M. Cameron 	cmd->Request.CDB[1] = type;
6184edd16368SStephen M. Cameron 	memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
618550a0decfSStephen M. Cameron 	cmd->ErrorDescriptor.Addr =
61862b08b3e9SDon Brace 			cpu_to_le64((le32_to_cpu(paddr32) + sizeof(*cmd)));
618750a0decfSStephen M. Cameron 	cmd->ErrorDescriptor.Len = cpu_to_le32(sizeof(struct ErrorInfo));
6188edd16368SStephen M. Cameron 
61892b08b3e9SDon Brace 	writel(le32_to_cpu(paddr32), vaddr + SA5_REQUEST_PORT_OFFSET);
6190edd16368SStephen M. Cameron 
6191edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
6192edd16368SStephen M. Cameron 		tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
61932b08b3e9SDon Brace 		if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr64)
6194edd16368SStephen M. Cameron 			break;
6195edd16368SStephen M. Cameron 		msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
6196edd16368SStephen M. Cameron 	}
6197edd16368SStephen M. Cameron 
6198edd16368SStephen M. Cameron 	iounmap(vaddr);
6199edd16368SStephen M. Cameron 
6200edd16368SStephen M. Cameron 	/* we leak the DMA buffer here ... no choice since the controller could
6201edd16368SStephen M. Cameron 	 *  still complete the command.
6202edd16368SStephen M. Cameron 	 */
6203edd16368SStephen M. Cameron 	if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
6204edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
6205edd16368SStephen M. Cameron 			opcode, type);
6206edd16368SStephen M. Cameron 		return -ETIMEDOUT;
6207edd16368SStephen M. Cameron 	}
6208edd16368SStephen M. Cameron 
6209edd16368SStephen M. Cameron 	pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
6210edd16368SStephen M. Cameron 
6211edd16368SStephen M. Cameron 	if (tag & HPSA_ERROR_BIT) {
6212edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
6213edd16368SStephen M. Cameron 			opcode, type);
6214edd16368SStephen M. Cameron 		return -EIO;
6215edd16368SStephen M. Cameron 	}
6216edd16368SStephen M. Cameron 
6217edd16368SStephen M. Cameron 	dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
6218edd16368SStephen M. Cameron 		opcode, type);
6219edd16368SStephen M. Cameron 	return 0;
6220edd16368SStephen M. Cameron }
6221edd16368SStephen M. Cameron 
6222edd16368SStephen M. Cameron #define hpsa_noop(p) hpsa_message(p, 3, 0)
6223edd16368SStephen M. Cameron 
62241df8552aSStephen M. Cameron static int hpsa_controller_hard_reset(struct pci_dev *pdev,
622542a91641SDon Brace 	void __iomem *vaddr, u32 use_doorbell)
6226edd16368SStephen M. Cameron {
6227edd16368SStephen M. Cameron 
62281df8552aSStephen M. Cameron 	if (use_doorbell) {
62291df8552aSStephen M. Cameron 		/* For everything after the P600, the PCI power state method
62301df8552aSStephen M. Cameron 		 * of resetting the controller doesn't work, so we have this
62311df8552aSStephen M. Cameron 		 * other way using the doorbell register.
6232edd16368SStephen M. Cameron 		 */
62331df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using doorbell to reset controller\n");
6234cf0b08d0SStephen M. Cameron 		writel(use_doorbell, vaddr + SA5_DOORBELL);
623585009239SStephen M. Cameron 
623600701a96SJustin Lindley 		/* PMC hardware guys tell us we need a 10 second delay after
623785009239SStephen M. Cameron 		 * doorbell reset and before any attempt to talk to the board
623885009239SStephen M. Cameron 		 * at all to ensure that this actually works and doesn't fall
623985009239SStephen M. Cameron 		 * over in some weird corner cases.
624085009239SStephen M. Cameron 		 */
624100701a96SJustin Lindley 		msleep(10000);
62421df8552aSStephen M. Cameron 	} else { /* Try to do it the PCI power state way */
6243edd16368SStephen M. Cameron 
6244edd16368SStephen M. Cameron 		/* Quoting from the Open CISS Specification: "The Power
6245edd16368SStephen M. Cameron 		 * Management Control/Status Register (CSR) controls the power
6246edd16368SStephen M. Cameron 		 * state of the device.  The normal operating state is D0,
6247edd16368SStephen M. Cameron 		 * CSR=00h.  The software off state is D3, CSR=03h.  To reset
62481df8552aSStephen M. Cameron 		 * the controller, place the interface device in D3 then to D0,
62491df8552aSStephen M. Cameron 		 * this causes a secondary PCI reset which will reset the
62501df8552aSStephen M. Cameron 		 * controller." */
6251edd16368SStephen M. Cameron 
62522662cab8SDon Brace 		int rc = 0;
62532662cab8SDon Brace 
62541df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using PCI PM to reset controller\n");
62552662cab8SDon Brace 
6256edd16368SStephen M. Cameron 		/* enter the D3hot power management state */
62572662cab8SDon Brace 		rc = pci_set_power_state(pdev, PCI_D3hot);
62582662cab8SDon Brace 		if (rc)
62592662cab8SDon Brace 			return rc;
6260edd16368SStephen M. Cameron 
6261edd16368SStephen M. Cameron 		msleep(500);
6262edd16368SStephen M. Cameron 
6263edd16368SStephen M. Cameron 		/* enter the D0 power management state */
62642662cab8SDon Brace 		rc = pci_set_power_state(pdev, PCI_D0);
62652662cab8SDon Brace 		if (rc)
62662662cab8SDon Brace 			return rc;
6267c4853efeSMike Miller 
6268c4853efeSMike Miller 		/*
6269c4853efeSMike Miller 		 * The P600 requires a small delay when changing states.
6270c4853efeSMike Miller 		 * Otherwise we may think the board did not reset and we bail.
6271c4853efeSMike Miller 		 * This for kdump only and is particular to the P600.
6272c4853efeSMike Miller 		 */
6273c4853efeSMike Miller 		msleep(500);
62741df8552aSStephen M. Cameron 	}
62751df8552aSStephen M. Cameron 	return 0;
62761df8552aSStephen M. Cameron }
62771df8552aSStephen M. Cameron 
62786f039790SGreg Kroah-Hartman static void init_driver_version(char *driver_version, int len)
6279580ada3cSStephen M. Cameron {
6280580ada3cSStephen M. Cameron 	memset(driver_version, 0, len);
6281f79cfec6SStephen M. Cameron 	strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
6282580ada3cSStephen M. Cameron }
6283580ada3cSStephen M. Cameron 
62846f039790SGreg Kroah-Hartman static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
6285580ada3cSStephen M. Cameron {
6286580ada3cSStephen M. Cameron 	char *driver_version;
6287580ada3cSStephen M. Cameron 	int i, size = sizeof(cfgtable->driver_version);
6288580ada3cSStephen M. Cameron 
6289580ada3cSStephen M. Cameron 	driver_version = kmalloc(size, GFP_KERNEL);
6290580ada3cSStephen M. Cameron 	if (!driver_version)
6291580ada3cSStephen M. Cameron 		return -ENOMEM;
6292580ada3cSStephen M. Cameron 
6293580ada3cSStephen M. Cameron 	init_driver_version(driver_version, size);
6294580ada3cSStephen M. Cameron 	for (i = 0; i < size; i++)
6295580ada3cSStephen M. Cameron 		writeb(driver_version[i], &cfgtable->driver_version[i]);
6296580ada3cSStephen M. Cameron 	kfree(driver_version);
6297580ada3cSStephen M. Cameron 	return 0;
6298580ada3cSStephen M. Cameron }
6299580ada3cSStephen M. Cameron 
63006f039790SGreg Kroah-Hartman static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
63016f039790SGreg Kroah-Hartman 					  unsigned char *driver_ver)
6302580ada3cSStephen M. Cameron {
6303580ada3cSStephen M. Cameron 	int i;
6304580ada3cSStephen M. Cameron 
6305580ada3cSStephen M. Cameron 	for (i = 0; i < sizeof(cfgtable->driver_version); i++)
6306580ada3cSStephen M. Cameron 		driver_ver[i] = readb(&cfgtable->driver_version[i]);
6307580ada3cSStephen M. Cameron }
6308580ada3cSStephen M. Cameron 
63096f039790SGreg Kroah-Hartman static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
6310580ada3cSStephen M. Cameron {
6311580ada3cSStephen M. Cameron 
6312580ada3cSStephen M. Cameron 	char *driver_ver, *old_driver_ver;
6313580ada3cSStephen M. Cameron 	int rc, size = sizeof(cfgtable->driver_version);
6314580ada3cSStephen M. Cameron 
6315580ada3cSStephen M. Cameron 	old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
6316580ada3cSStephen M. Cameron 	if (!old_driver_ver)
6317580ada3cSStephen M. Cameron 		return -ENOMEM;
6318580ada3cSStephen M. Cameron 	driver_ver = old_driver_ver + size;
6319580ada3cSStephen M. Cameron 
6320580ada3cSStephen M. Cameron 	/* After a reset, the 32 bytes of "driver version" in the cfgtable
6321580ada3cSStephen M. Cameron 	 * should have been changed, otherwise we know the reset failed.
6322580ada3cSStephen M. Cameron 	 */
6323580ada3cSStephen M. Cameron 	init_driver_version(old_driver_ver, size);
6324580ada3cSStephen M. Cameron 	read_driver_ver_from_cfgtable(cfgtable, driver_ver);
6325580ada3cSStephen M. Cameron 	rc = !memcmp(driver_ver, old_driver_ver, size);
6326580ada3cSStephen M. Cameron 	kfree(old_driver_ver);
6327580ada3cSStephen M. Cameron 	return rc;
6328580ada3cSStephen M. Cameron }
63291df8552aSStephen M. Cameron /* This does a hard reset of the controller using PCI power management
63301df8552aSStephen M. Cameron  * states or the using the doorbell register.
63311df8552aSStephen M. Cameron  */
63326b6c1cd7STomas Henzl static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev, u32 board_id)
63331df8552aSStephen M. Cameron {
63341df8552aSStephen M. Cameron 	u64 cfg_offset;
63351df8552aSStephen M. Cameron 	u32 cfg_base_addr;
63361df8552aSStephen M. Cameron 	u64 cfg_base_addr_index;
63371df8552aSStephen M. Cameron 	void __iomem *vaddr;
63381df8552aSStephen M. Cameron 	unsigned long paddr;
6339580ada3cSStephen M. Cameron 	u32 misc_fw_support;
6340270d05deSStephen M. Cameron 	int rc;
63411df8552aSStephen M. Cameron 	struct CfgTable __iomem *cfgtable;
6342cf0b08d0SStephen M. Cameron 	u32 use_doorbell;
6343270d05deSStephen M. Cameron 	u16 command_register;
63441df8552aSStephen M. Cameron 
63451df8552aSStephen M. Cameron 	/* For controllers as old as the P600, this is very nearly
63461df8552aSStephen M. Cameron 	 * the same thing as
63471df8552aSStephen M. Cameron 	 *
63481df8552aSStephen M. Cameron 	 * pci_save_state(pci_dev);
63491df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D3hot);
63501df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D0);
63511df8552aSStephen M. Cameron 	 * pci_restore_state(pci_dev);
63521df8552aSStephen M. Cameron 	 *
63531df8552aSStephen M. Cameron 	 * For controllers newer than the P600, the pci power state
63541df8552aSStephen M. Cameron 	 * method of resetting doesn't work so we have another way
63551df8552aSStephen M. Cameron 	 * using the doorbell register.
63561df8552aSStephen M. Cameron 	 */
635718867659SStephen M. Cameron 
635860f923b9SRobert Elliott 	if (!ctlr_is_resettable(board_id)) {
635960f923b9SRobert Elliott 		dev_warn(&pdev->dev, "Controller not resettable\n");
636025c1e56aSStephen M. Cameron 		return -ENODEV;
636125c1e56aSStephen M. Cameron 	}
636246380786SStephen M. Cameron 
636346380786SStephen M. Cameron 	/* if controller is soft- but not hard resettable... */
636446380786SStephen M. Cameron 	if (!ctlr_is_hard_resettable(board_id))
636546380786SStephen M. Cameron 		return -ENOTSUPP; /* try soft reset later. */
636618867659SStephen M. Cameron 
6367270d05deSStephen M. Cameron 	/* Save the PCI command register */
6368270d05deSStephen M. Cameron 	pci_read_config_word(pdev, 4, &command_register);
6369270d05deSStephen M. Cameron 	pci_save_state(pdev);
63701df8552aSStephen M. Cameron 
63711df8552aSStephen M. Cameron 	/* find the first memory BAR, so we can find the cfg table */
63721df8552aSStephen M. Cameron 	rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
63731df8552aSStephen M. Cameron 	if (rc)
63741df8552aSStephen M. Cameron 		return rc;
63751df8552aSStephen M. Cameron 	vaddr = remap_pci_mem(paddr, 0x250);
63761df8552aSStephen M. Cameron 	if (!vaddr)
63771df8552aSStephen M. Cameron 		return -ENOMEM;
63781df8552aSStephen M. Cameron 
63791df8552aSStephen M. Cameron 	/* find cfgtable in order to check if reset via doorbell is supported */
63801df8552aSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
63811df8552aSStephen M. Cameron 					&cfg_base_addr_index, &cfg_offset);
63821df8552aSStephen M. Cameron 	if (rc)
63831df8552aSStephen M. Cameron 		goto unmap_vaddr;
63841df8552aSStephen M. Cameron 	cfgtable = remap_pci_mem(pci_resource_start(pdev,
63851df8552aSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
63861df8552aSStephen M. Cameron 	if (!cfgtable) {
63871df8552aSStephen M. Cameron 		rc = -ENOMEM;
63881df8552aSStephen M. Cameron 		goto unmap_vaddr;
63891df8552aSStephen M. Cameron 	}
6390580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(cfgtable);
6391580ada3cSStephen M. Cameron 	if (rc)
639203741d95STomas Henzl 		goto unmap_cfgtable;
63931df8552aSStephen M. Cameron 
6394cf0b08d0SStephen M. Cameron 	/* If reset via doorbell register is supported, use that.
6395cf0b08d0SStephen M. Cameron 	 * There are two such methods.  Favor the newest method.
6396cf0b08d0SStephen M. Cameron 	 */
63971df8552aSStephen M. Cameron 	misc_fw_support = readl(&cfgtable->misc_fw_support);
6398cf0b08d0SStephen M. Cameron 	use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
6399cf0b08d0SStephen M. Cameron 	if (use_doorbell) {
6400cf0b08d0SStephen M. Cameron 		use_doorbell = DOORBELL_CTLR_RESET2;
6401cf0b08d0SStephen M. Cameron 	} else {
64021df8552aSStephen M. Cameron 		use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
6403cf0b08d0SStephen M. Cameron 		if (use_doorbell) {
6404050f7147SStephen Cameron 			dev_warn(&pdev->dev,
6405050f7147SStephen Cameron 				"Soft reset not supported. Firmware update is required.\n");
640664670ac8SStephen M. Cameron 			rc = -ENOTSUPP; /* try soft reset */
6407cf0b08d0SStephen M. Cameron 			goto unmap_cfgtable;
6408cf0b08d0SStephen M. Cameron 		}
6409cf0b08d0SStephen M. Cameron 	}
64101df8552aSStephen M. Cameron 
64111df8552aSStephen M. Cameron 	rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
64121df8552aSStephen M. Cameron 	if (rc)
64131df8552aSStephen M. Cameron 		goto unmap_cfgtable;
6414edd16368SStephen M. Cameron 
6415270d05deSStephen M. Cameron 	pci_restore_state(pdev);
6416270d05deSStephen M. Cameron 	pci_write_config_word(pdev, 4, command_register);
6417edd16368SStephen M. Cameron 
64181df8552aSStephen M. Cameron 	/* Some devices (notably the HP Smart Array 5i Controller)
64191df8552aSStephen M. Cameron 	   need a little pause here */
64201df8552aSStephen M. Cameron 	msleep(HPSA_POST_RESET_PAUSE_MSECS);
64211df8552aSStephen M. Cameron 
6422fe5389c8SStephen M. Cameron 	rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
6423fe5389c8SStephen M. Cameron 	if (rc) {
6424fe5389c8SStephen M. Cameron 		dev_warn(&pdev->dev,
6425050f7147SStephen Cameron 			"Failed waiting for board to become ready after hard reset\n");
6426fe5389c8SStephen M. Cameron 		goto unmap_cfgtable;
6427fe5389c8SStephen M. Cameron 	}
6428fe5389c8SStephen M. Cameron 
6429580ada3cSStephen M. Cameron 	rc = controller_reset_failed(vaddr);
6430580ada3cSStephen M. Cameron 	if (rc < 0)
6431580ada3cSStephen M. Cameron 		goto unmap_cfgtable;
6432580ada3cSStephen M. Cameron 	if (rc) {
643364670ac8SStephen M. Cameron 		dev_warn(&pdev->dev, "Unable to successfully reset "
643464670ac8SStephen M. Cameron 			"controller. Will try soft reset.\n");
643564670ac8SStephen M. Cameron 		rc = -ENOTSUPP;
6436580ada3cSStephen M. Cameron 	} else {
643764670ac8SStephen M. Cameron 		dev_info(&pdev->dev, "board ready after hard reset.\n");
64381df8552aSStephen M. Cameron 	}
64391df8552aSStephen M. Cameron 
64401df8552aSStephen M. Cameron unmap_cfgtable:
64411df8552aSStephen M. Cameron 	iounmap(cfgtable);
64421df8552aSStephen M. Cameron 
64431df8552aSStephen M. Cameron unmap_vaddr:
64441df8552aSStephen M. Cameron 	iounmap(vaddr);
64451df8552aSStephen M. Cameron 	return rc;
6446edd16368SStephen M. Cameron }
6447edd16368SStephen M. Cameron 
6448edd16368SStephen M. Cameron /*
6449edd16368SStephen M. Cameron  *  We cannot read the structure directly, for portability we must use
6450edd16368SStephen M. Cameron  *   the io functions.
6451edd16368SStephen M. Cameron  *   This is for debug only.
6452edd16368SStephen M. Cameron  */
645342a91641SDon Brace static void print_cfg_table(struct device *dev, struct CfgTable __iomem *tb)
6454edd16368SStephen M. Cameron {
645558f8665cSStephen M. Cameron #ifdef HPSA_DEBUG
6456edd16368SStephen M. Cameron 	int i;
6457edd16368SStephen M. Cameron 	char temp_name[17];
6458edd16368SStephen M. Cameron 
6459edd16368SStephen M. Cameron 	dev_info(dev, "Controller Configuration information\n");
6460edd16368SStephen M. Cameron 	dev_info(dev, "------------------------------------\n");
6461edd16368SStephen M. Cameron 	for (i = 0; i < 4; i++)
6462edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->Signature[i]));
6463edd16368SStephen M. Cameron 	temp_name[4] = '\0';
6464edd16368SStephen M. Cameron 	dev_info(dev, "   Signature = %s\n", temp_name);
6465edd16368SStephen M. Cameron 	dev_info(dev, "   Spec Number = %d\n", readl(&(tb->SpecValence)));
6466edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods supported = 0x%x\n",
6467edd16368SStephen M. Cameron 	       readl(&(tb->TransportSupport)));
6468edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods active = 0x%x\n",
6469edd16368SStephen M. Cameron 	       readl(&(tb->TransportActive)));
6470edd16368SStephen M. Cameron 	dev_info(dev, "   Requested transport Method = 0x%x\n",
6471edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.TransportRequest)));
6472edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Delay = 0x%x\n",
6473edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntDelay)));
6474edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Count = 0x%x\n",
6475edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntCount)));
647669d6e33dSRobert Elliott 	dev_info(dev, "   Max outstanding commands = %d\n",
6477edd16368SStephen M. Cameron 	       readl(&(tb->CmdsOutMax)));
6478edd16368SStephen M. Cameron 	dev_info(dev, "   Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
6479edd16368SStephen M. Cameron 	for (i = 0; i < 16; i++)
6480edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->ServerName[i]));
6481edd16368SStephen M. Cameron 	temp_name[16] = '\0';
6482edd16368SStephen M. Cameron 	dev_info(dev, "   Server Name = %s\n", temp_name);
6483edd16368SStephen M. Cameron 	dev_info(dev, "   Heartbeat Counter = 0x%x\n\n\n",
6484edd16368SStephen M. Cameron 		readl(&(tb->HeartBeat)));
6485edd16368SStephen M. Cameron #endif				/* HPSA_DEBUG */
648658f8665cSStephen M. Cameron }
6487edd16368SStephen M. Cameron 
6488edd16368SStephen M. Cameron static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
6489edd16368SStephen M. Cameron {
6490edd16368SStephen M. Cameron 	int i, offset, mem_type, bar_type;
6491edd16368SStephen M. Cameron 
6492edd16368SStephen M. Cameron 	if (pci_bar_addr == PCI_BASE_ADDRESS_0)	/* looking for BAR zero? */
6493edd16368SStephen M. Cameron 		return 0;
6494edd16368SStephen M. Cameron 	offset = 0;
6495edd16368SStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
6496edd16368SStephen M. Cameron 		bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
6497edd16368SStephen M. Cameron 		if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
6498edd16368SStephen M. Cameron 			offset += 4;
6499edd16368SStephen M. Cameron 		else {
6500edd16368SStephen M. Cameron 			mem_type = pci_resource_flags(pdev, i) &
6501edd16368SStephen M. Cameron 			    PCI_BASE_ADDRESS_MEM_TYPE_MASK;
6502edd16368SStephen M. Cameron 			switch (mem_type) {
6503edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_32:
6504edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_1M:
6505edd16368SStephen M. Cameron 				offset += 4;	/* 32 bit */
6506edd16368SStephen M. Cameron 				break;
6507edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_64:
6508edd16368SStephen M. Cameron 				offset += 8;
6509edd16368SStephen M. Cameron 				break;
6510edd16368SStephen M. Cameron 			default:	/* reserved in PCI 2.2 */
6511edd16368SStephen M. Cameron 				dev_warn(&pdev->dev,
6512edd16368SStephen M. Cameron 				       "base address is invalid\n");
6513edd16368SStephen M. Cameron 				return -1;
6514edd16368SStephen M. Cameron 				break;
6515edd16368SStephen M. Cameron 			}
6516edd16368SStephen M. Cameron 		}
6517edd16368SStephen M. Cameron 		if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
6518edd16368SStephen M. Cameron 			return i + 1;
6519edd16368SStephen M. Cameron 	}
6520edd16368SStephen M. Cameron 	return -1;
6521edd16368SStephen M. Cameron }
6522edd16368SStephen M. Cameron 
6523cc64c817SRobert Elliott static void hpsa_disable_interrupt_mode(struct ctlr_info *h)
6524cc64c817SRobert Elliott {
6525cc64c817SRobert Elliott 	if (h->msix_vector) {
6526cc64c817SRobert Elliott 		if (h->pdev->msix_enabled)
6527cc64c817SRobert Elliott 			pci_disable_msix(h->pdev);
6528105a3dbcSRobert Elliott 		h->msix_vector = 0;
6529cc64c817SRobert Elliott 	} else if (h->msi_vector) {
6530cc64c817SRobert Elliott 		if (h->pdev->msi_enabled)
6531cc64c817SRobert Elliott 			pci_disable_msi(h->pdev);
6532105a3dbcSRobert Elliott 		h->msi_vector = 0;
6533cc64c817SRobert Elliott 	}
6534cc64c817SRobert Elliott }
6535cc64c817SRobert Elliott 
6536edd16368SStephen M. Cameron /* If MSI/MSI-X is supported by the kernel we will try to enable it on
6537050f7147SStephen Cameron  * controllers that are capable. If not, we use legacy INTx mode.
6538edd16368SStephen M. Cameron  */
65396f039790SGreg Kroah-Hartman static void hpsa_interrupt_mode(struct ctlr_info *h)
6540edd16368SStephen M. Cameron {
6541edd16368SStephen M. Cameron #ifdef CONFIG_PCI_MSI
6542254f796bSMatt Gates 	int err, i;
6543254f796bSMatt Gates 	struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
6544254f796bSMatt Gates 
6545254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++) {
6546254f796bSMatt Gates 		hpsa_msix_entries[i].vector = 0;
6547254f796bSMatt Gates 		hpsa_msix_entries[i].entry = i;
6548254f796bSMatt Gates 	}
6549edd16368SStephen M. Cameron 
6550edd16368SStephen M. Cameron 	/* Some boards advertise MSI but don't really support it */
65516b3f4c52SStephen M. Cameron 	if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
65526b3f4c52SStephen M. Cameron 	    (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
6553edd16368SStephen M. Cameron 		goto default_int_mode;
655455c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
6555050f7147SStephen Cameron 		dev_info(&h->pdev->dev, "MSI-X capable controller\n");
6556eee0f03aSHannes Reinecke 		h->msix_vector = MAX_REPLY_QUEUES;
6557f89439bcSStephen M. Cameron 		if (h->msix_vector > num_online_cpus())
6558f89439bcSStephen M. Cameron 			h->msix_vector = num_online_cpus();
655918fce3c4SAlexander Gordeev 		err = pci_enable_msix_range(h->pdev, hpsa_msix_entries,
656018fce3c4SAlexander Gordeev 					    1, h->msix_vector);
656118fce3c4SAlexander Gordeev 		if (err < 0) {
656218fce3c4SAlexander Gordeev 			dev_warn(&h->pdev->dev, "MSI-X init failed %d\n", err);
656318fce3c4SAlexander Gordeev 			h->msix_vector = 0;
656418fce3c4SAlexander Gordeev 			goto single_msi_mode;
656518fce3c4SAlexander Gordeev 		} else if (err < h->msix_vector) {
656655c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
6567edd16368SStephen M. Cameron 			       "available\n", err);
6568eee0f03aSHannes Reinecke 		}
656918fce3c4SAlexander Gordeev 		h->msix_vector = err;
6570eee0f03aSHannes Reinecke 		for (i = 0; i < h->msix_vector; i++)
6571eee0f03aSHannes Reinecke 			h->intr[i] = hpsa_msix_entries[i].vector;
6572eee0f03aSHannes Reinecke 		return;
6573edd16368SStephen M. Cameron 	}
657418fce3c4SAlexander Gordeev single_msi_mode:
657555c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
6576050f7147SStephen Cameron 		dev_info(&h->pdev->dev, "MSI capable controller\n");
657755c06c71SStephen M. Cameron 		if (!pci_enable_msi(h->pdev))
6578edd16368SStephen M. Cameron 			h->msi_vector = 1;
6579edd16368SStephen M. Cameron 		else
658055c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "MSI init failed\n");
6581edd16368SStephen M. Cameron 	}
6582edd16368SStephen M. Cameron default_int_mode:
6583edd16368SStephen M. Cameron #endif				/* CONFIG_PCI_MSI */
6584edd16368SStephen M. Cameron 	/* if we get here we're going to use the default interrupt mode */
6585a9a3a273SStephen M. Cameron 	h->intr[h->intr_mode] = h->pdev->irq;
6586edd16368SStephen M. Cameron }
6587edd16368SStephen M. Cameron 
65886f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
6589e5c880d1SStephen M. Cameron {
6590e5c880d1SStephen M. Cameron 	int i;
6591e5c880d1SStephen M. Cameron 	u32 subsystem_vendor_id, subsystem_device_id;
6592e5c880d1SStephen M. Cameron 
6593e5c880d1SStephen M. Cameron 	subsystem_vendor_id = pdev->subsystem_vendor;
6594e5c880d1SStephen M. Cameron 	subsystem_device_id = pdev->subsystem_device;
6595e5c880d1SStephen M. Cameron 	*board_id = ((subsystem_device_id << 16) & 0xffff0000) |
6596e5c880d1SStephen M. Cameron 		    subsystem_vendor_id;
6597e5c880d1SStephen M. Cameron 
6598e5c880d1SStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(products); i++)
6599e5c880d1SStephen M. Cameron 		if (*board_id == products[i].board_id)
6600e5c880d1SStephen M. Cameron 			return i;
6601e5c880d1SStephen M. Cameron 
66026798cc0aSStephen M. Cameron 	if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
66036798cc0aSStephen M. Cameron 		subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
66046798cc0aSStephen M. Cameron 		!hpsa_allow_any) {
6605e5c880d1SStephen M. Cameron 		dev_warn(&pdev->dev, "unrecognized board ID: "
6606e5c880d1SStephen M. Cameron 			"0x%08x, ignoring.\n", *board_id);
6607e5c880d1SStephen M. Cameron 			return -ENODEV;
6608e5c880d1SStephen M. Cameron 	}
6609e5c880d1SStephen M. Cameron 	return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
6610e5c880d1SStephen M. Cameron }
6611e5c880d1SStephen M. Cameron 
66126f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
66133a7774ceSStephen M. Cameron 				    unsigned long *memory_bar)
66143a7774ceSStephen M. Cameron {
66153a7774ceSStephen M. Cameron 	int i;
66163a7774ceSStephen M. Cameron 
66173a7774ceSStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
661812d2cd47SStephen M. Cameron 		if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
66193a7774ceSStephen M. Cameron 			/* addressing mode bits already removed */
662012d2cd47SStephen M. Cameron 			*memory_bar = pci_resource_start(pdev, i);
662112d2cd47SStephen M. Cameron 			dev_dbg(&pdev->dev, "memory BAR = %lx\n",
66223a7774ceSStephen M. Cameron 				*memory_bar);
66233a7774ceSStephen M. Cameron 			return 0;
66243a7774ceSStephen M. Cameron 		}
662512d2cd47SStephen M. Cameron 	dev_warn(&pdev->dev, "no memory BAR found\n");
66263a7774ceSStephen M. Cameron 	return -ENODEV;
66273a7774ceSStephen M. Cameron }
66283a7774ceSStephen M. Cameron 
66296f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
66306f039790SGreg Kroah-Hartman 				     int wait_for_ready)
66312c4c8c8bSStephen M. Cameron {
6632fe5389c8SStephen M. Cameron 	int i, iterations;
66332c4c8c8bSStephen M. Cameron 	u32 scratchpad;
6634fe5389c8SStephen M. Cameron 	if (wait_for_ready)
6635fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_READY_ITERATIONS;
6636fe5389c8SStephen M. Cameron 	else
6637fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
66382c4c8c8bSStephen M. Cameron 
6639fe5389c8SStephen M. Cameron 	for (i = 0; i < iterations; i++) {
6640fe5389c8SStephen M. Cameron 		scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
6641fe5389c8SStephen M. Cameron 		if (wait_for_ready) {
66422c4c8c8bSStephen M. Cameron 			if (scratchpad == HPSA_FIRMWARE_READY)
66432c4c8c8bSStephen M. Cameron 				return 0;
6644fe5389c8SStephen M. Cameron 		} else {
6645fe5389c8SStephen M. Cameron 			if (scratchpad != HPSA_FIRMWARE_READY)
6646fe5389c8SStephen M. Cameron 				return 0;
6647fe5389c8SStephen M. Cameron 		}
66482c4c8c8bSStephen M. Cameron 		msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
66492c4c8c8bSStephen M. Cameron 	}
6650fe5389c8SStephen M. Cameron 	dev_warn(&pdev->dev, "board not ready, timed out.\n");
66512c4c8c8bSStephen M. Cameron 	return -ENODEV;
66522c4c8c8bSStephen M. Cameron }
66532c4c8c8bSStephen M. Cameron 
66546f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
66556f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
6656a51fd47fSStephen M. Cameron 			       u64 *cfg_offset)
6657a51fd47fSStephen M. Cameron {
6658a51fd47fSStephen M. Cameron 	*cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
6659a51fd47fSStephen M. Cameron 	*cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
6660a51fd47fSStephen M. Cameron 	*cfg_base_addr &= (u32) 0x0000ffff;
6661a51fd47fSStephen M. Cameron 	*cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
6662a51fd47fSStephen M. Cameron 	if (*cfg_base_addr_index == -1) {
6663a51fd47fSStephen M. Cameron 		dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
6664a51fd47fSStephen M. Cameron 		return -ENODEV;
6665a51fd47fSStephen M. Cameron 	}
6666a51fd47fSStephen M. Cameron 	return 0;
6667a51fd47fSStephen M. Cameron }
6668a51fd47fSStephen M. Cameron 
6669195f2c65SRobert Elliott static void hpsa_free_cfgtables(struct ctlr_info *h)
6670195f2c65SRobert Elliott {
6671105a3dbcSRobert Elliott 	if (h->transtable) {
6672195f2c65SRobert Elliott 		iounmap(h->transtable);
6673105a3dbcSRobert Elliott 		h->transtable = NULL;
6674105a3dbcSRobert Elliott 	}
6675105a3dbcSRobert Elliott 	if (h->cfgtable) {
6676195f2c65SRobert Elliott 		iounmap(h->cfgtable);
6677105a3dbcSRobert Elliott 		h->cfgtable = NULL;
6678105a3dbcSRobert Elliott 	}
6679195f2c65SRobert Elliott }
6680195f2c65SRobert Elliott 
6681195f2c65SRobert Elliott /* Find and map CISS config table and transfer table
6682195f2c65SRobert Elliott + * several items must be unmapped (freed) later
6683195f2c65SRobert Elliott + * */
66846f039790SGreg Kroah-Hartman static int hpsa_find_cfgtables(struct ctlr_info *h)
6685edd16368SStephen M. Cameron {
668601a02ffcSStephen M. Cameron 	u64 cfg_offset;
668701a02ffcSStephen M. Cameron 	u32 cfg_base_addr;
668801a02ffcSStephen M. Cameron 	u64 cfg_base_addr_index;
6689303932fdSDon Brace 	u32 trans_offset;
6690a51fd47fSStephen M. Cameron 	int rc;
669177c4495cSStephen M. Cameron 
6692a51fd47fSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
6693a51fd47fSStephen M. Cameron 		&cfg_base_addr_index, &cfg_offset);
6694a51fd47fSStephen M. Cameron 	if (rc)
6695a51fd47fSStephen M. Cameron 		return rc;
669677c4495cSStephen M. Cameron 	h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
6697a51fd47fSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
6698cd3c81c4SRobert Elliott 	if (!h->cfgtable) {
6699cd3c81c4SRobert Elliott 		dev_err(&h->pdev->dev, "Failed mapping cfgtable\n");
670077c4495cSStephen M. Cameron 		return -ENOMEM;
6701cd3c81c4SRobert Elliott 	}
6702580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(h->cfgtable);
6703580ada3cSStephen M. Cameron 	if (rc)
6704580ada3cSStephen M. Cameron 		return rc;
670577c4495cSStephen M. Cameron 	/* Find performant mode table. */
6706a51fd47fSStephen M. Cameron 	trans_offset = readl(&h->cfgtable->TransMethodOffset);
670777c4495cSStephen M. Cameron 	h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
670877c4495cSStephen M. Cameron 				cfg_base_addr_index)+cfg_offset+trans_offset,
670977c4495cSStephen M. Cameron 				sizeof(*h->transtable));
6710195f2c65SRobert Elliott 	if (!h->transtable) {
6711195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "Failed mapping transfer table\n");
6712195f2c65SRobert Elliott 		hpsa_free_cfgtables(h);
671377c4495cSStephen M. Cameron 		return -ENOMEM;
6714195f2c65SRobert Elliott 	}
671577c4495cSStephen M. Cameron 	return 0;
671677c4495cSStephen M. Cameron }
671777c4495cSStephen M. Cameron 
67186f039790SGreg Kroah-Hartman static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
6719cba3d38bSStephen M. Cameron {
672041ce4c35SStephen Cameron #define MIN_MAX_COMMANDS 16
672141ce4c35SStephen Cameron 	BUILD_BUG_ON(MIN_MAX_COMMANDS <= HPSA_NRESERVED_CMDS);
672241ce4c35SStephen Cameron 
672341ce4c35SStephen Cameron 	h->max_commands = readl(&h->cfgtable->MaxPerformantModeCommands);
672472ceeaecSStephen M. Cameron 
672572ceeaecSStephen M. Cameron 	/* Limit commands in memory limited kdump scenario. */
672672ceeaecSStephen M. Cameron 	if (reset_devices && h->max_commands > 32)
672772ceeaecSStephen M. Cameron 		h->max_commands = 32;
672872ceeaecSStephen M. Cameron 
672941ce4c35SStephen Cameron 	if (h->max_commands < MIN_MAX_COMMANDS) {
673041ce4c35SStephen Cameron 		dev_warn(&h->pdev->dev,
673141ce4c35SStephen Cameron 			"Controller reports max supported commands of %d Using %d instead. Ensure that firmware is up to date.\n",
673241ce4c35SStephen Cameron 			h->max_commands,
673341ce4c35SStephen Cameron 			MIN_MAX_COMMANDS);
673441ce4c35SStephen Cameron 		h->max_commands = MIN_MAX_COMMANDS;
6735cba3d38bSStephen M. Cameron 	}
6736cba3d38bSStephen M. Cameron }
6737cba3d38bSStephen M. Cameron 
6738c7ee65b3SWebb Scales /* If the controller reports that the total max sg entries is greater than 512,
6739c7ee65b3SWebb Scales  * then we know that chained SG blocks work.  (Original smart arrays did not
6740c7ee65b3SWebb Scales  * support chained SG blocks and would return zero for max sg entries.)
6741c7ee65b3SWebb Scales  */
6742c7ee65b3SWebb Scales static int hpsa_supports_chained_sg_blocks(struct ctlr_info *h)
6743c7ee65b3SWebb Scales {
6744c7ee65b3SWebb Scales 	return h->maxsgentries > 512;
6745c7ee65b3SWebb Scales }
6746c7ee65b3SWebb Scales 
6747b93d7536SStephen M. Cameron /* Interrogate the hardware for some limits:
6748b93d7536SStephen M. Cameron  * max commands, max SG elements without chaining, and with chaining,
6749b93d7536SStephen M. Cameron  * SG chain block size, etc.
6750b93d7536SStephen M. Cameron  */
67516f039790SGreg Kroah-Hartman static void hpsa_find_board_params(struct ctlr_info *h)
6752b93d7536SStephen M. Cameron {
6753cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
675445fcb86eSStephen Cameron 	h->nr_cmds = h->max_commands;
6755b93d7536SStephen M. Cameron 	h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
6756283b4a9bSStephen M. Cameron 	h->fw_support = readl(&(h->cfgtable->misc_fw_support));
6757c7ee65b3SWebb Scales 	if (hpsa_supports_chained_sg_blocks(h)) {
6758c7ee65b3SWebb Scales 		/* Limit in-command s/g elements to 32 save dma'able memory. */
6759b93d7536SStephen M. Cameron 		h->max_cmd_sg_entries = 32;
67601a63ea6fSWebb Scales 		h->chainsize = h->maxsgentries - h->max_cmd_sg_entries;
6761b93d7536SStephen M. Cameron 		h->maxsgentries--; /* save one for chain pointer */
6762b93d7536SStephen M. Cameron 	} else {
6763c7ee65b3SWebb Scales 		/*
6764c7ee65b3SWebb Scales 		 * Original smart arrays supported at most 31 s/g entries
6765c7ee65b3SWebb Scales 		 * embedded inline in the command (trying to use more
6766c7ee65b3SWebb Scales 		 * would lock up the controller)
6767c7ee65b3SWebb Scales 		 */
6768c7ee65b3SWebb Scales 		h->max_cmd_sg_entries = 31;
67691a63ea6fSWebb Scales 		h->maxsgentries = 31; /* default to traditional values */
6770c7ee65b3SWebb Scales 		h->chainsize = 0;
6771b93d7536SStephen M. Cameron 	}
677275167d2cSStephen M. Cameron 
677375167d2cSStephen M. Cameron 	/* Find out what task management functions are supported and cache */
677475167d2cSStephen M. Cameron 	h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
67750e7a7fceSScott Teel 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
67760e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
67770e7a7fceSScott Teel 	if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
67780e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
67798be986ccSStephen Cameron 	if (!(HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags))
67808be986ccSStephen Cameron 		dev_warn(&h->pdev->dev, "HP SSD Smart Path aborts not supported\n");
6781b93d7536SStephen M. Cameron }
6782b93d7536SStephen M. Cameron 
678376c46e49SStephen M. Cameron static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
678476c46e49SStephen M. Cameron {
67850fc9fd40SAkinobu Mita 	if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
6786050f7147SStephen Cameron 		dev_err(&h->pdev->dev, "not a valid CISS config table\n");
678776c46e49SStephen M. Cameron 		return false;
678876c46e49SStephen M. Cameron 	}
678976c46e49SStephen M. Cameron 	return true;
679076c46e49SStephen M. Cameron }
679176c46e49SStephen M. Cameron 
679297a5e98cSStephen M. Cameron static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
6793f7c39101SStephen M. Cameron {
679497a5e98cSStephen M. Cameron 	u32 driver_support;
6795f7c39101SStephen M. Cameron 
679697a5e98cSStephen M. Cameron 	driver_support = readl(&(h->cfgtable->driver_support));
67970b9e7b74SArnd Bergmann 	/* Need to enable prefetch in the SCSI core for 6400 in x86 */
67980b9e7b74SArnd Bergmann #ifdef CONFIG_X86
679997a5e98cSStephen M. Cameron 	driver_support |= ENABLE_SCSI_PREFETCH;
6800f7c39101SStephen M. Cameron #endif
680128e13446SStephen M. Cameron 	driver_support |= ENABLE_UNIT_ATTN;
680228e13446SStephen M. Cameron 	writel(driver_support, &(h->cfgtable->driver_support));
6803f7c39101SStephen M. Cameron }
6804f7c39101SStephen M. Cameron 
68053d0eab67SStephen M. Cameron /* Disable DMA prefetch for the P600.  Otherwise an ASIC bug may result
68063d0eab67SStephen M. Cameron  * in a prefetch beyond physical memory.
68073d0eab67SStephen M. Cameron  */
68083d0eab67SStephen M. Cameron static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
68093d0eab67SStephen M. Cameron {
68103d0eab67SStephen M. Cameron 	u32 dma_prefetch;
68113d0eab67SStephen M. Cameron 
68123d0eab67SStephen M. Cameron 	if (h->board_id != 0x3225103C)
68133d0eab67SStephen M. Cameron 		return;
68143d0eab67SStephen M. Cameron 	dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
68153d0eab67SStephen M. Cameron 	dma_prefetch |= 0x8000;
68163d0eab67SStephen M. Cameron 	writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
68173d0eab67SStephen M. Cameron }
68183d0eab67SStephen M. Cameron 
6819c706a795SRobert Elliott static int hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
682076438d08SStephen M. Cameron {
682176438d08SStephen M. Cameron 	int i;
682276438d08SStephen M. Cameron 	u32 doorbell_value;
682376438d08SStephen M. Cameron 	unsigned long flags;
682476438d08SStephen M. Cameron 	/* wait until the clear_event_notify bit 6 is cleared by controller. */
6825007e7aa9SRobert Elliott 	for (i = 0; i < MAX_CLEAR_EVENT_WAIT; i++) {
682676438d08SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
682776438d08SStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
682876438d08SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
682976438d08SStephen M. Cameron 		if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
6830c706a795SRobert Elliott 			goto done;
683176438d08SStephen M. Cameron 		/* delay and try again */
6832007e7aa9SRobert Elliott 		msleep(CLEAR_EVENT_WAIT_INTERVAL);
683376438d08SStephen M. Cameron 	}
6834c706a795SRobert Elliott 	return -ENODEV;
6835c706a795SRobert Elliott done:
6836c706a795SRobert Elliott 	return 0;
683776438d08SStephen M. Cameron }
683876438d08SStephen M. Cameron 
6839c706a795SRobert Elliott static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
6840eb6b2ae9SStephen M. Cameron {
6841eb6b2ae9SStephen M. Cameron 	int i;
68426eaf46fdSStephen M. Cameron 	u32 doorbell_value;
68436eaf46fdSStephen M. Cameron 	unsigned long flags;
6844eb6b2ae9SStephen M. Cameron 
6845eb6b2ae9SStephen M. Cameron 	/* under certain very rare conditions, this can take awhile.
6846eb6b2ae9SStephen M. Cameron 	 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
6847eb6b2ae9SStephen M. Cameron 	 * as we enter this code.)
6848eb6b2ae9SStephen M. Cameron 	 */
6849007e7aa9SRobert Elliott 	for (i = 0; i < MAX_MODE_CHANGE_WAIT; i++) {
685025163bd5SWebb Scales 		if (h->remove_in_progress)
685125163bd5SWebb Scales 			goto done;
68526eaf46fdSStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
68536eaf46fdSStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
68546eaf46fdSStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
6855382be668SDan Carpenter 		if (!(doorbell_value & CFGTBL_ChangeReq))
6856c706a795SRobert Elliott 			goto done;
6857eb6b2ae9SStephen M. Cameron 		/* delay and try again */
6858007e7aa9SRobert Elliott 		msleep(MODE_CHANGE_WAIT_INTERVAL);
6859eb6b2ae9SStephen M. Cameron 	}
6860c706a795SRobert Elliott 	return -ENODEV;
6861c706a795SRobert Elliott done:
6862c706a795SRobert Elliott 	return 0;
68633f4336f3SStephen M. Cameron }
68643f4336f3SStephen M. Cameron 
6865c706a795SRobert Elliott /* return -ENODEV or other reason on error, 0 on success */
68666f039790SGreg Kroah-Hartman static int hpsa_enter_simple_mode(struct ctlr_info *h)
68673f4336f3SStephen M. Cameron {
68683f4336f3SStephen M. Cameron 	u32 trans_support;
68693f4336f3SStephen M. Cameron 
68703f4336f3SStephen M. Cameron 	trans_support = readl(&(h->cfgtable->TransportSupport));
68713f4336f3SStephen M. Cameron 	if (!(trans_support & SIMPLE_MODE))
68723f4336f3SStephen M. Cameron 		return -ENOTSUPP;
68733f4336f3SStephen M. Cameron 
68743f4336f3SStephen M. Cameron 	h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
6875283b4a9bSStephen M. Cameron 
68763f4336f3SStephen M. Cameron 	/* Update the field, and then ring the doorbell */
68773f4336f3SStephen M. Cameron 	writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
6878b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
68793f4336f3SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
6880c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h))
6881c706a795SRobert Elliott 		goto error;
6882eb6b2ae9SStephen M. Cameron 	print_cfg_table(&h->pdev->dev, h->cfgtable);
6883283b4a9bSStephen M. Cameron 	if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
6884283b4a9bSStephen M. Cameron 		goto error;
6885960a30e7SStephen M. Cameron 	h->transMethod = CFGTBL_Trans_Simple;
6886eb6b2ae9SStephen M. Cameron 	return 0;
6887283b4a9bSStephen M. Cameron error:
6888050f7147SStephen Cameron 	dev_err(&h->pdev->dev, "failed to enter simple mode\n");
6889283b4a9bSStephen M. Cameron 	return -ENODEV;
6890eb6b2ae9SStephen M. Cameron }
6891eb6b2ae9SStephen M. Cameron 
6892195f2c65SRobert Elliott /* free items allocated or mapped by hpsa_pci_init */
6893195f2c65SRobert Elliott static void hpsa_free_pci_init(struct ctlr_info *h)
6894195f2c65SRobert Elliott {
6895195f2c65SRobert Elliott 	hpsa_free_cfgtables(h);			/* pci_init 4 */
6896195f2c65SRobert Elliott 	iounmap(h->vaddr);			/* pci_init 3 */
6897105a3dbcSRobert Elliott 	h->vaddr = NULL;
6898195f2c65SRobert Elliott 	hpsa_disable_interrupt_mode(h);		/* pci_init 2 */
6899195f2c65SRobert Elliott 	pci_release_regions(h->pdev);		/* pci_init 2 */
6900195f2c65SRobert Elliott 	pci_disable_device(h->pdev);		/* pci_init 1 */
6901195f2c65SRobert Elliott }
6902195f2c65SRobert Elliott 
6903195f2c65SRobert Elliott /* several items must be freed later */
69046f039790SGreg Kroah-Hartman static int hpsa_pci_init(struct ctlr_info *h)
690577c4495cSStephen M. Cameron {
6906eb6b2ae9SStephen M. Cameron 	int prod_index, err;
6907edd16368SStephen M. Cameron 
6908e5c880d1SStephen M. Cameron 	prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
6909e5c880d1SStephen M. Cameron 	if (prod_index < 0)
691060f923b9SRobert Elliott 		return prod_index;
6911e5c880d1SStephen M. Cameron 	h->product_name = products[prod_index].product_name;
6912e5c880d1SStephen M. Cameron 	h->access = *(products[prod_index].access);
6913e5c880d1SStephen M. Cameron 
69149b5c48c2SStephen Cameron 	h->needs_abort_tags_swizzled =
69159b5c48c2SStephen Cameron 		ctlr_needs_abort_tags_swizzled(h->board_id);
69169b5c48c2SStephen Cameron 
6917e5a44df8SMatthew Garrett 	pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
6918e5a44df8SMatthew Garrett 			       PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
6919e5a44df8SMatthew Garrett 
692055c06c71SStephen M. Cameron 	err = pci_enable_device(h->pdev);
6921edd16368SStephen M. Cameron 	if (err) {
6922195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "failed to enable PCI device\n");
6923edd16368SStephen M. Cameron 		return err;
6924edd16368SStephen M. Cameron 	}
6925edd16368SStephen M. Cameron 
6926f79cfec6SStephen M. Cameron 	err = pci_request_regions(h->pdev, HPSA);
6927edd16368SStephen M. Cameron 	if (err) {
692855c06c71SStephen M. Cameron 		dev_err(&h->pdev->dev,
6929195f2c65SRobert Elliott 			"failed to obtain PCI resources\n");
6930195f2c65SRobert Elliott 		goto clean1;	/* pci */
6931edd16368SStephen M. Cameron 	}
69324fa604e1SRobert Elliott 
69334fa604e1SRobert Elliott 	pci_set_master(h->pdev);
69344fa604e1SRobert Elliott 
69356b3f4c52SStephen M. Cameron 	hpsa_interrupt_mode(h);
693612d2cd47SStephen M. Cameron 	err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
69373a7774ceSStephen M. Cameron 	if (err)
6938195f2c65SRobert Elliott 		goto clean2;	/* intmode+region, pci */
6939edd16368SStephen M. Cameron 	h->vaddr = remap_pci_mem(h->paddr, 0x250);
6940204892e9SStephen M. Cameron 	if (!h->vaddr) {
6941195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "failed to remap PCI mem\n");
6942204892e9SStephen M. Cameron 		err = -ENOMEM;
6943195f2c65SRobert Elliott 		goto clean2;	/* intmode+region, pci */
6944204892e9SStephen M. Cameron 	}
6945fe5389c8SStephen M. Cameron 	err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
69462c4c8c8bSStephen M. Cameron 	if (err)
6947195f2c65SRobert Elliott 		goto clean3;	/* vaddr, intmode+region, pci */
694877c4495cSStephen M. Cameron 	err = hpsa_find_cfgtables(h);
694977c4495cSStephen M. Cameron 	if (err)
6950195f2c65SRobert Elliott 		goto clean3;	/* vaddr, intmode+region, pci */
6951b93d7536SStephen M. Cameron 	hpsa_find_board_params(h);
6952edd16368SStephen M. Cameron 
695376c46e49SStephen M. Cameron 	if (!hpsa_CISS_signature_present(h)) {
6954edd16368SStephen M. Cameron 		err = -ENODEV;
6955195f2c65SRobert Elliott 		goto clean4;	/* cfgtables, vaddr, intmode+region, pci */
6956edd16368SStephen M. Cameron 	}
695797a5e98cSStephen M. Cameron 	hpsa_set_driver_support_bits(h);
69583d0eab67SStephen M. Cameron 	hpsa_p600_dma_prefetch_quirk(h);
6959eb6b2ae9SStephen M. Cameron 	err = hpsa_enter_simple_mode(h);
6960eb6b2ae9SStephen M. Cameron 	if (err)
6961195f2c65SRobert Elliott 		goto clean4;	/* cfgtables, vaddr, intmode+region, pci */
6962edd16368SStephen M. Cameron 	return 0;
6963edd16368SStephen M. Cameron 
6964195f2c65SRobert Elliott clean4:	/* cfgtables, vaddr, intmode+region, pci */
6965195f2c65SRobert Elliott 	hpsa_free_cfgtables(h);
6966195f2c65SRobert Elliott clean3:	/* vaddr, intmode+region, pci */
6967204892e9SStephen M. Cameron 	iounmap(h->vaddr);
6968105a3dbcSRobert Elliott 	h->vaddr = NULL;
6969195f2c65SRobert Elliott clean2:	/* intmode+region, pci */
6970195f2c65SRobert Elliott 	hpsa_disable_interrupt_mode(h);
697155c06c71SStephen M. Cameron 	pci_release_regions(h->pdev);
6972195f2c65SRobert Elliott clean1:	/* pci */
6973195f2c65SRobert Elliott 	pci_disable_device(h->pdev);
6974edd16368SStephen M. Cameron 	return err;
6975edd16368SStephen M. Cameron }
6976edd16368SStephen M. Cameron 
69776f039790SGreg Kroah-Hartman static void hpsa_hba_inquiry(struct ctlr_info *h)
6978339b2b14SStephen M. Cameron {
6979339b2b14SStephen M. Cameron 	int rc;
6980339b2b14SStephen M. Cameron 
6981339b2b14SStephen M. Cameron #define HBA_INQUIRY_BYTE_COUNT 64
6982339b2b14SStephen M. Cameron 	h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
6983339b2b14SStephen M. Cameron 	if (!h->hba_inquiry_data)
6984339b2b14SStephen M. Cameron 		return;
6985339b2b14SStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
6986339b2b14SStephen M. Cameron 		h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
6987339b2b14SStephen M. Cameron 	if (rc != 0) {
6988339b2b14SStephen M. Cameron 		kfree(h->hba_inquiry_data);
6989339b2b14SStephen M. Cameron 		h->hba_inquiry_data = NULL;
6990339b2b14SStephen M. Cameron 	}
6991339b2b14SStephen M. Cameron }
6992339b2b14SStephen M. Cameron 
69936b6c1cd7STomas Henzl static int hpsa_init_reset_devices(struct pci_dev *pdev, u32 board_id)
6994edd16368SStephen M. Cameron {
69951df8552aSStephen M. Cameron 	int rc, i;
69963b747298STomas Henzl 	void __iomem *vaddr;
6997edd16368SStephen M. Cameron 
69984c2a8c40SStephen M. Cameron 	if (!reset_devices)
69994c2a8c40SStephen M. Cameron 		return 0;
70004c2a8c40SStephen M. Cameron 
7001132aa220STomas Henzl 	/* kdump kernel is loading, we don't know in which state is
7002132aa220STomas Henzl 	 * the pci interface. The dev->enable_cnt is equal zero
7003132aa220STomas Henzl 	 * so we call enable+disable, wait a while and switch it on.
7004132aa220STomas Henzl 	 */
7005132aa220STomas Henzl 	rc = pci_enable_device(pdev);
7006132aa220STomas Henzl 	if (rc) {
7007132aa220STomas Henzl 		dev_warn(&pdev->dev, "Failed to enable PCI device\n");
7008132aa220STomas Henzl 		return -ENODEV;
7009132aa220STomas Henzl 	}
7010132aa220STomas Henzl 	pci_disable_device(pdev);
7011132aa220STomas Henzl 	msleep(260);			/* a randomly chosen number */
7012132aa220STomas Henzl 	rc = pci_enable_device(pdev);
7013132aa220STomas Henzl 	if (rc) {
7014132aa220STomas Henzl 		dev_warn(&pdev->dev, "failed to enable device.\n");
7015132aa220STomas Henzl 		return -ENODEV;
7016132aa220STomas Henzl 	}
70174fa604e1SRobert Elliott 
7018859c75abSTomas Henzl 	pci_set_master(pdev);
70194fa604e1SRobert Elliott 
70203b747298STomas Henzl 	vaddr = pci_ioremap_bar(pdev, 0);
70213b747298STomas Henzl 	if (vaddr == NULL) {
70223b747298STomas Henzl 		rc = -ENOMEM;
70233b747298STomas Henzl 		goto out_disable;
70243b747298STomas Henzl 	}
70253b747298STomas Henzl 	writel(SA5_INTR_OFF, vaddr + SA5_REPLY_INTR_MASK_OFFSET);
70263b747298STomas Henzl 	iounmap(vaddr);
70273b747298STomas Henzl 
70281df8552aSStephen M. Cameron 	/* Reset the controller with a PCI power-cycle or via doorbell */
70296b6c1cd7STomas Henzl 	rc = hpsa_kdump_hard_reset_controller(pdev, board_id);
7030edd16368SStephen M. Cameron 
70311df8552aSStephen M. Cameron 	/* -ENOTSUPP here means we cannot reset the controller
70321df8552aSStephen M. Cameron 	 * but it's already (and still) up and running in
703318867659SStephen M. Cameron 	 * "performant mode".  Or, it might be 640x, which can't reset
703418867659SStephen M. Cameron 	 * due to concerns about shared bbwc between 6402/6404 pair.
70351df8552aSStephen M. Cameron 	 */
7036adf1b3a3SRobert Elliott 	if (rc)
7037132aa220STomas Henzl 		goto out_disable;
7038edd16368SStephen M. Cameron 
7039edd16368SStephen M. Cameron 	/* Now try to get the controller to respond to a no-op */
70401ba66c9cSRobert Elliott 	dev_info(&pdev->dev, "Waiting for controller to respond to no-op\n");
7041edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
7042edd16368SStephen M. Cameron 		if (hpsa_noop(pdev) == 0)
7043edd16368SStephen M. Cameron 			break;
7044edd16368SStephen M. Cameron 		else
7045edd16368SStephen M. Cameron 			dev_warn(&pdev->dev, "no-op failed%s\n",
7046edd16368SStephen M. Cameron 					(i < 11 ? "; re-trying" : ""));
7047edd16368SStephen M. Cameron 	}
7048132aa220STomas Henzl 
7049132aa220STomas Henzl out_disable:
7050132aa220STomas Henzl 
7051132aa220STomas Henzl 	pci_disable_device(pdev);
7052132aa220STomas Henzl 	return rc;
7053edd16368SStephen M. Cameron }
7054edd16368SStephen M. Cameron 
70551fb7c98aSRobert Elliott static void hpsa_free_cmd_pool(struct ctlr_info *h)
70561fb7c98aSRobert Elliott {
70571fb7c98aSRobert Elliott 	kfree(h->cmd_pool_bits);
7058105a3dbcSRobert Elliott 	h->cmd_pool_bits = NULL;
7059105a3dbcSRobert Elliott 	if (h->cmd_pool) {
70601fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
70611fb7c98aSRobert Elliott 				h->nr_cmds * sizeof(struct CommandList),
70621fb7c98aSRobert Elliott 				h->cmd_pool,
70631fb7c98aSRobert Elliott 				h->cmd_pool_dhandle);
7064105a3dbcSRobert Elliott 		h->cmd_pool = NULL;
7065105a3dbcSRobert Elliott 		h->cmd_pool_dhandle = 0;
7066105a3dbcSRobert Elliott 	}
7067105a3dbcSRobert Elliott 	if (h->errinfo_pool) {
70681fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
70691fb7c98aSRobert Elliott 				h->nr_cmds * sizeof(struct ErrorInfo),
70701fb7c98aSRobert Elliott 				h->errinfo_pool,
70711fb7c98aSRobert Elliott 				h->errinfo_pool_dhandle);
7072105a3dbcSRobert Elliott 		h->errinfo_pool = NULL;
7073105a3dbcSRobert Elliott 		h->errinfo_pool_dhandle = 0;
7074105a3dbcSRobert Elliott 	}
70751fb7c98aSRobert Elliott }
70761fb7c98aSRobert Elliott 
7077d37ffbe4SRobert Elliott static int hpsa_alloc_cmd_pool(struct ctlr_info *h)
70782e9d1b36SStephen M. Cameron {
70792e9d1b36SStephen M. Cameron 	h->cmd_pool_bits = kzalloc(
70802e9d1b36SStephen M. Cameron 		DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
70812e9d1b36SStephen M. Cameron 		sizeof(unsigned long), GFP_KERNEL);
70822e9d1b36SStephen M. Cameron 	h->cmd_pool = pci_alloc_consistent(h->pdev,
70832e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->cmd_pool),
70842e9d1b36SStephen M. Cameron 		    &(h->cmd_pool_dhandle));
70852e9d1b36SStephen M. Cameron 	h->errinfo_pool = pci_alloc_consistent(h->pdev,
70862e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->errinfo_pool),
70872e9d1b36SStephen M. Cameron 		    &(h->errinfo_pool_dhandle));
70882e9d1b36SStephen M. Cameron 	if ((h->cmd_pool_bits == NULL)
70892e9d1b36SStephen M. Cameron 	    || (h->cmd_pool == NULL)
70902e9d1b36SStephen M. Cameron 	    || (h->errinfo_pool == NULL)) {
70912e9d1b36SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory in %s", __func__);
70922c143342SRobert Elliott 		goto clean_up;
70932e9d1b36SStephen M. Cameron 	}
7094360c73bdSStephen Cameron 	hpsa_preinitialize_commands(h);
70952e9d1b36SStephen M. Cameron 	return 0;
70962c143342SRobert Elliott clean_up:
70972c143342SRobert Elliott 	hpsa_free_cmd_pool(h);
70982c143342SRobert Elliott 	return -ENOMEM;
70992e9d1b36SStephen M. Cameron }
71002e9d1b36SStephen M. Cameron 
710141b3cf08SStephen M. Cameron static void hpsa_irq_affinity_hints(struct ctlr_info *h)
710241b3cf08SStephen M. Cameron {
7103ec429952SFabian Frederick 	int i, cpu;
710441b3cf08SStephen M. Cameron 
710541b3cf08SStephen M. Cameron 	cpu = cpumask_first(cpu_online_mask);
710641b3cf08SStephen M. Cameron 	for (i = 0; i < h->msix_vector; i++) {
7107ec429952SFabian Frederick 		irq_set_affinity_hint(h->intr[i], get_cpu_mask(cpu));
710841b3cf08SStephen M. Cameron 		cpu = cpumask_next(cpu, cpu_online_mask);
710941b3cf08SStephen M. Cameron 	}
711041b3cf08SStephen M. Cameron }
711141b3cf08SStephen M. Cameron 
7112ec501a18SRobert Elliott /* clear affinity hints and free MSI-X, MSI, or legacy INTx vectors */
7113ec501a18SRobert Elliott static void hpsa_free_irqs(struct ctlr_info *h)
7114ec501a18SRobert Elliott {
7115ec501a18SRobert Elliott 	int i;
7116ec501a18SRobert Elliott 
7117ec501a18SRobert Elliott 	if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
7118ec501a18SRobert Elliott 		/* Single reply queue, only one irq to free */
7119ec501a18SRobert Elliott 		i = h->intr_mode;
7120ec501a18SRobert Elliott 		irq_set_affinity_hint(h->intr[i], NULL);
7121ec501a18SRobert Elliott 		free_irq(h->intr[i], &h->q[i]);
7122105a3dbcSRobert Elliott 		h->q[i] = 0;
7123ec501a18SRobert Elliott 		return;
7124ec501a18SRobert Elliott 	}
7125ec501a18SRobert Elliott 
7126ec501a18SRobert Elliott 	for (i = 0; i < h->msix_vector; i++) {
7127ec501a18SRobert Elliott 		irq_set_affinity_hint(h->intr[i], NULL);
7128ec501a18SRobert Elliott 		free_irq(h->intr[i], &h->q[i]);
7129105a3dbcSRobert Elliott 		h->q[i] = 0;
7130ec501a18SRobert Elliott 	}
7131a4e17fc1SRobert Elliott 	for (; i < MAX_REPLY_QUEUES; i++)
7132a4e17fc1SRobert Elliott 		h->q[i] = 0;
7133ec501a18SRobert Elliott }
7134ec501a18SRobert Elliott 
71359ee61794SRobert Elliott /* returns 0 on success; cleans up and returns -Enn on error */
71369ee61794SRobert Elliott static int hpsa_request_irqs(struct ctlr_info *h,
71370ae01a32SStephen M. Cameron 	irqreturn_t (*msixhandler)(int, void *),
71380ae01a32SStephen M. Cameron 	irqreturn_t (*intxhandler)(int, void *))
71390ae01a32SStephen M. Cameron {
7140254f796bSMatt Gates 	int rc, i;
71410ae01a32SStephen M. Cameron 
7142254f796bSMatt Gates 	/*
7143254f796bSMatt Gates 	 * initialize h->q[x] = x so that interrupt handlers know which
7144254f796bSMatt Gates 	 * queue to process.
7145254f796bSMatt Gates 	 */
7146254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++)
7147254f796bSMatt Gates 		h->q[i] = (u8) i;
7148254f796bSMatt Gates 
7149eee0f03aSHannes Reinecke 	if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
7150254f796bSMatt Gates 		/* If performant mode and MSI-X, use multiple reply queues */
7151a4e17fc1SRobert Elliott 		for (i = 0; i < h->msix_vector; i++) {
7152254f796bSMatt Gates 			rc = request_irq(h->intr[i], msixhandler,
7153254f796bSMatt Gates 					0, h->devname,
7154254f796bSMatt Gates 					&h->q[i]);
7155a4e17fc1SRobert Elliott 			if (rc) {
7156a4e17fc1SRobert Elliott 				int j;
7157a4e17fc1SRobert Elliott 
7158a4e17fc1SRobert Elliott 				dev_err(&h->pdev->dev,
7159a4e17fc1SRobert Elliott 					"failed to get irq %d for %s\n",
7160a4e17fc1SRobert Elliott 				       h->intr[i], h->devname);
7161a4e17fc1SRobert Elliott 				for (j = 0; j < i; j++) {
7162a4e17fc1SRobert Elliott 					free_irq(h->intr[j], &h->q[j]);
7163a4e17fc1SRobert Elliott 					h->q[j] = 0;
7164a4e17fc1SRobert Elliott 				}
7165a4e17fc1SRobert Elliott 				for (; j < MAX_REPLY_QUEUES; j++)
7166a4e17fc1SRobert Elliott 					h->q[j] = 0;
7167a4e17fc1SRobert Elliott 				return rc;
7168a4e17fc1SRobert Elliott 			}
7169a4e17fc1SRobert Elliott 		}
717041b3cf08SStephen M. Cameron 		hpsa_irq_affinity_hints(h);
7171254f796bSMatt Gates 	} else {
7172254f796bSMatt Gates 		/* Use single reply pool */
7173eee0f03aSHannes Reinecke 		if (h->msix_vector > 0 || h->msi_vector) {
7174254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
7175254f796bSMatt Gates 				msixhandler, 0, h->devname,
7176254f796bSMatt Gates 				&h->q[h->intr_mode]);
7177254f796bSMatt Gates 		} else {
7178254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
7179254f796bSMatt Gates 				intxhandler, IRQF_SHARED, h->devname,
7180254f796bSMatt Gates 				&h->q[h->intr_mode]);
7181254f796bSMatt Gates 		}
7182105a3dbcSRobert Elliott 		irq_set_affinity_hint(h->intr[h->intr_mode], NULL);
7183254f796bSMatt Gates 	}
71840ae01a32SStephen M. Cameron 	if (rc) {
7185195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "failed to get irq %d for %s\n",
71860ae01a32SStephen M. Cameron 		       h->intr[h->intr_mode], h->devname);
7187195f2c65SRobert Elliott 		hpsa_free_irqs(h);
71880ae01a32SStephen M. Cameron 		return -ENODEV;
71890ae01a32SStephen M. Cameron 	}
71900ae01a32SStephen M. Cameron 	return 0;
71910ae01a32SStephen M. Cameron }
71920ae01a32SStephen M. Cameron 
71936f039790SGreg Kroah-Hartman static int hpsa_kdump_soft_reset(struct ctlr_info *h)
719464670ac8SStephen M. Cameron {
7195bf43caf3SRobert Elliott 	hpsa_send_host_reset(h, RAID_CTLR_LUNID, HPSA_RESET_TYPE_CONTROLLER);
719664670ac8SStephen M. Cameron 
719764670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
719864670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) {
719964670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
720064670ac8SStephen M. Cameron 		return -1;
720164670ac8SStephen M. Cameron 	}
720264670ac8SStephen M. Cameron 
720364670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
720464670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) {
720564670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Board failed to become ready "
720664670ac8SStephen M. Cameron 			"after soft reset.\n");
720764670ac8SStephen M. Cameron 		return -1;
720864670ac8SStephen M. Cameron 	}
720964670ac8SStephen M. Cameron 
721064670ac8SStephen M. Cameron 	return 0;
721164670ac8SStephen M. Cameron }
721264670ac8SStephen M. Cameron 
7213072b0518SStephen M. Cameron static void hpsa_free_reply_queues(struct ctlr_info *h)
7214072b0518SStephen M. Cameron {
7215072b0518SStephen M. Cameron 	int i;
7216072b0518SStephen M. Cameron 
7217072b0518SStephen M. Cameron 	for (i = 0; i < h->nreply_queues; i++) {
7218072b0518SStephen M. Cameron 		if (!h->reply_queue[i].head)
7219072b0518SStephen M. Cameron 			continue;
72201fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
72211fb7c98aSRobert Elliott 					h->reply_queue_size,
72221fb7c98aSRobert Elliott 					h->reply_queue[i].head,
72231fb7c98aSRobert Elliott 					h->reply_queue[i].busaddr);
7224072b0518SStephen M. Cameron 		h->reply_queue[i].head = NULL;
7225072b0518SStephen M. Cameron 		h->reply_queue[i].busaddr = 0;
7226072b0518SStephen M. Cameron 	}
7227105a3dbcSRobert Elliott 	h->reply_queue_size = 0;
7228072b0518SStephen M. Cameron }
7229072b0518SStephen M. Cameron 
72300097f0f4SStephen M. Cameron static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
72310097f0f4SStephen M. Cameron {
7232105a3dbcSRobert Elliott 	hpsa_free_performant_mode(h);		/* init_one 7 */
7233105a3dbcSRobert Elliott 	hpsa_free_sg_chain_blocks(h);		/* init_one 6 */
7234105a3dbcSRobert Elliott 	hpsa_free_cmd_pool(h);			/* init_one 5 */
7235105a3dbcSRobert Elliott 	hpsa_free_irqs(h);			/* init_one 4 */
7236105a3dbcSRobert Elliott 	hpsa_free_pci_init(h);			/* init_one 3 */
7237105a3dbcSRobert Elliott 	kfree(h);				/* init_one 1 */
723864670ac8SStephen M. Cameron }
723964670ac8SStephen M. Cameron 
7240a0c12413SStephen M. Cameron /* Called when controller lockup detected. */
7241f2405db8SDon Brace static void fail_all_outstanding_cmds(struct ctlr_info *h)
7242a0c12413SStephen M. Cameron {
7243281a7fd0SWebb Scales 	int i, refcount;
7244281a7fd0SWebb Scales 	struct CommandList *c;
724525163bd5SWebb Scales 	int failcount = 0;
7246a0c12413SStephen M. Cameron 
7247080ef1ccSDon Brace 	flush_workqueue(h->resubmit_wq); /* ensure all cmds are fully built */
7248f2405db8SDon Brace 	for (i = 0; i < h->nr_cmds; i++) {
7249f2405db8SDon Brace 		c = h->cmd_pool + i;
7250281a7fd0SWebb Scales 		refcount = atomic_inc_return(&c->refcount);
7251281a7fd0SWebb Scales 		if (refcount > 1) {
725225163bd5SWebb Scales 			c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
72535a3d16f5SStephen M. Cameron 			finish_cmd(c);
7254433b5f4dSStephen Cameron 			atomic_dec(&h->commands_outstanding);
725525163bd5SWebb Scales 			failcount++;
7256a0c12413SStephen M. Cameron 		}
7257281a7fd0SWebb Scales 		cmd_free(h, c);
7258281a7fd0SWebb Scales 	}
725925163bd5SWebb Scales 	dev_warn(&h->pdev->dev,
726025163bd5SWebb Scales 		"failed %d commands in fail_all\n", failcount);
7261a0c12413SStephen M. Cameron }
7262a0c12413SStephen M. Cameron 
7263094963daSStephen M. Cameron static void set_lockup_detected_for_all_cpus(struct ctlr_info *h, u32 value)
7264094963daSStephen M. Cameron {
7265c8ed0010SRusty Russell 	int cpu;
7266094963daSStephen M. Cameron 
7267c8ed0010SRusty Russell 	for_each_online_cpu(cpu) {
7268094963daSStephen M. Cameron 		u32 *lockup_detected;
7269094963daSStephen M. Cameron 		lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
7270094963daSStephen M. Cameron 		*lockup_detected = value;
7271094963daSStephen M. Cameron 	}
7272094963daSStephen M. Cameron 	wmb(); /* be sure the per-cpu variables are out to memory */
7273094963daSStephen M. Cameron }
7274094963daSStephen M. Cameron 
7275a0c12413SStephen M. Cameron static void controller_lockup_detected(struct ctlr_info *h)
7276a0c12413SStephen M. Cameron {
7277a0c12413SStephen M. Cameron 	unsigned long flags;
7278094963daSStephen M. Cameron 	u32 lockup_detected;
7279a0c12413SStephen M. Cameron 
7280a0c12413SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
7281a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
7282094963daSStephen M. Cameron 	lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
7283094963daSStephen M. Cameron 	if (!lockup_detected) {
7284094963daSStephen M. Cameron 		/* no heartbeat, but controller gave us a zero. */
7285094963daSStephen M. Cameron 		dev_warn(&h->pdev->dev,
728625163bd5SWebb Scales 			"lockup detected after %d but scratchpad register is zero\n",
728725163bd5SWebb Scales 			h->heartbeat_sample_interval / HZ);
7288094963daSStephen M. Cameron 		lockup_detected = 0xffffffff;
7289094963daSStephen M. Cameron 	}
7290094963daSStephen M. Cameron 	set_lockup_detected_for_all_cpus(h, lockup_detected);
7291a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
729225163bd5SWebb Scales 	dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x after %d\n",
729325163bd5SWebb Scales 			lockup_detected, h->heartbeat_sample_interval / HZ);
7294a0c12413SStephen M. Cameron 	pci_disable_device(h->pdev);
7295f2405db8SDon Brace 	fail_all_outstanding_cmds(h);
7296a0c12413SStephen M. Cameron }
7297a0c12413SStephen M. Cameron 
729825163bd5SWebb Scales static int detect_controller_lockup(struct ctlr_info *h)
7299a0c12413SStephen M. Cameron {
7300a0c12413SStephen M. Cameron 	u64 now;
7301a0c12413SStephen M. Cameron 	u32 heartbeat;
7302a0c12413SStephen M. Cameron 	unsigned long flags;
7303a0c12413SStephen M. Cameron 
7304a0c12413SStephen M. Cameron 	now = get_jiffies_64();
7305a0c12413SStephen M. Cameron 	/* If we've received an interrupt recently, we're ok. */
7306a0c12413SStephen M. Cameron 	if (time_after64(h->last_intr_timestamp +
7307e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
730825163bd5SWebb Scales 		return false;
7309a0c12413SStephen M. Cameron 
7310a0c12413SStephen M. Cameron 	/*
7311a0c12413SStephen M. Cameron 	 * If we've already checked the heartbeat recently, we're ok.
7312a0c12413SStephen M. Cameron 	 * This could happen if someone sends us a signal. We
7313a0c12413SStephen M. Cameron 	 * otherwise don't care about signals in this thread.
7314a0c12413SStephen M. Cameron 	 */
7315a0c12413SStephen M. Cameron 	if (time_after64(h->last_heartbeat_timestamp +
7316e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
731725163bd5SWebb Scales 		return false;
7318a0c12413SStephen M. Cameron 
7319a0c12413SStephen M. Cameron 	/* If heartbeat has not changed since we last looked, we're not ok. */
7320a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
7321a0c12413SStephen M. Cameron 	heartbeat = readl(&h->cfgtable->HeartBeat);
7322a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
7323a0c12413SStephen M. Cameron 	if (h->last_heartbeat == heartbeat) {
7324a0c12413SStephen M. Cameron 		controller_lockup_detected(h);
732525163bd5SWebb Scales 		return true;
7326a0c12413SStephen M. Cameron 	}
7327a0c12413SStephen M. Cameron 
7328a0c12413SStephen M. Cameron 	/* We're ok. */
7329a0c12413SStephen M. Cameron 	h->last_heartbeat = heartbeat;
7330a0c12413SStephen M. Cameron 	h->last_heartbeat_timestamp = now;
733125163bd5SWebb Scales 	return false;
7332a0c12413SStephen M. Cameron }
7333a0c12413SStephen M. Cameron 
73349846590eSStephen M. Cameron static void hpsa_ack_ctlr_events(struct ctlr_info *h)
733576438d08SStephen M. Cameron {
733676438d08SStephen M. Cameron 	int i;
733776438d08SStephen M. Cameron 	char *event_type;
733876438d08SStephen M. Cameron 
7339e4aa3e6aSStephen Cameron 	if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
7340e4aa3e6aSStephen Cameron 		return;
7341e4aa3e6aSStephen Cameron 
734276438d08SStephen M. Cameron 	/* Ask the controller to clear the events we're handling. */
73431f7cee8cSStephen M. Cameron 	if ((h->transMethod & (CFGTBL_Trans_io_accel1
73441f7cee8cSStephen M. Cameron 			| CFGTBL_Trans_io_accel2)) &&
734576438d08SStephen M. Cameron 		(h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
734676438d08SStephen M. Cameron 		 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
734776438d08SStephen M. Cameron 
734876438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
734976438d08SStephen M. Cameron 			event_type = "state change";
735076438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
735176438d08SStephen M. Cameron 			event_type = "configuration change";
735276438d08SStephen M. Cameron 		/* Stop sending new RAID offload reqs via the IO accelerator */
735376438d08SStephen M. Cameron 		scsi_block_requests(h->scsi_host);
735476438d08SStephen M. Cameron 		for (i = 0; i < h->ndevices; i++)
735576438d08SStephen M. Cameron 			h->dev[i]->offload_enabled = 0;
735623100dd9SStephen M. Cameron 		hpsa_drain_accel_commands(h);
735776438d08SStephen M. Cameron 		/* Set 'accelerator path config change' bit */
735876438d08SStephen M. Cameron 		dev_warn(&h->pdev->dev,
735976438d08SStephen M. Cameron 			"Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
736076438d08SStephen M. Cameron 			h->events, event_type);
736176438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
736276438d08SStephen M. Cameron 		/* Set the "clear event notify field update" bit 6 */
736376438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
736476438d08SStephen M. Cameron 		/* Wait until ctlr clears 'clear event notify field', bit 6 */
736576438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
736676438d08SStephen M. Cameron 		scsi_unblock_requests(h->scsi_host);
736776438d08SStephen M. Cameron 	} else {
736876438d08SStephen M. Cameron 		/* Acknowledge controller notification events. */
736976438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
737076438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
737176438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
737276438d08SStephen M. Cameron #if 0
737376438d08SStephen M. Cameron 		writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
737476438d08SStephen M. Cameron 		hpsa_wait_for_mode_change_ack(h);
737576438d08SStephen M. Cameron #endif
737676438d08SStephen M. Cameron 	}
73779846590eSStephen M. Cameron 	return;
737876438d08SStephen M. Cameron }
737976438d08SStephen M. Cameron 
738076438d08SStephen M. Cameron /* Check a register on the controller to see if there are configuration
738176438d08SStephen M. Cameron  * changes (added/changed/removed logical drives, etc.) which mean that
7382e863d68eSScott Teel  * we should rescan the controller for devices.
7383e863d68eSScott Teel  * Also check flag for driver-initiated rescan.
738476438d08SStephen M. Cameron  */
73859846590eSStephen M. Cameron static int hpsa_ctlr_needs_rescan(struct ctlr_info *h)
738676438d08SStephen M. Cameron {
738776438d08SStephen M. Cameron 	if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
73889846590eSStephen M. Cameron 		return 0;
738976438d08SStephen M. Cameron 
739076438d08SStephen M. Cameron 	h->events = readl(&(h->cfgtable->event_notify));
73919846590eSStephen M. Cameron 	return h->events & RESCAN_REQUIRED_EVENT_BITS;
73929846590eSStephen M. Cameron }
739376438d08SStephen M. Cameron 
739476438d08SStephen M. Cameron /*
73959846590eSStephen M. Cameron  * Check if any of the offline devices have become ready
739676438d08SStephen M. Cameron  */
73979846590eSStephen M. Cameron static int hpsa_offline_devices_ready(struct ctlr_info *h)
73989846590eSStephen M. Cameron {
73999846590eSStephen M. Cameron 	unsigned long flags;
74009846590eSStephen M. Cameron 	struct offline_device_entry *d;
74019846590eSStephen M. Cameron 	struct list_head *this, *tmp;
74029846590eSStephen M. Cameron 
74039846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
74049846590eSStephen M. Cameron 	list_for_each_safe(this, tmp, &h->offline_device_list) {
74059846590eSStephen M. Cameron 		d = list_entry(this, struct offline_device_entry,
74069846590eSStephen M. Cameron 				offline_list);
74079846590eSStephen M. Cameron 		spin_unlock_irqrestore(&h->offline_device_lock, flags);
7408d1fea47cSStephen M. Cameron 		if (!hpsa_volume_offline(h, d->scsi3addr)) {
7409d1fea47cSStephen M. Cameron 			spin_lock_irqsave(&h->offline_device_lock, flags);
7410d1fea47cSStephen M. Cameron 			list_del(&d->offline_list);
7411d1fea47cSStephen M. Cameron 			spin_unlock_irqrestore(&h->offline_device_lock, flags);
74129846590eSStephen M. Cameron 			return 1;
7413d1fea47cSStephen M. Cameron 		}
74149846590eSStephen M. Cameron 		spin_lock_irqsave(&h->offline_device_lock, flags);
741576438d08SStephen M. Cameron 	}
74169846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
74179846590eSStephen M. Cameron 	return 0;
74189846590eSStephen M. Cameron }
74199846590eSStephen M. Cameron 
74206636e7f4SDon Brace static void hpsa_rescan_ctlr_worker(struct work_struct *work)
7421a0c12413SStephen M. Cameron {
7422a0c12413SStephen M. Cameron 	unsigned long flags;
74238a98db73SStephen M. Cameron 	struct ctlr_info *h = container_of(to_delayed_work(work),
74246636e7f4SDon Brace 					struct ctlr_info, rescan_ctlr_work);
74256636e7f4SDon Brace 
74266636e7f4SDon Brace 
74276636e7f4SDon Brace 	if (h->remove_in_progress)
74288a98db73SStephen M. Cameron 		return;
74299846590eSStephen M. Cameron 
74309846590eSStephen M. Cameron 	if (hpsa_ctlr_needs_rescan(h) || hpsa_offline_devices_ready(h)) {
74319846590eSStephen M. Cameron 		scsi_host_get(h->scsi_host);
74329846590eSStephen M. Cameron 		hpsa_ack_ctlr_events(h);
74339846590eSStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
74349846590eSStephen M. Cameron 		scsi_host_put(h->scsi_host);
74359846590eSStephen M. Cameron 	}
74366636e7f4SDon Brace 	spin_lock_irqsave(&h->lock, flags);
74376636e7f4SDon Brace 	if (!h->remove_in_progress)
74386636e7f4SDon Brace 		queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
74396636e7f4SDon Brace 				h->heartbeat_sample_interval);
74406636e7f4SDon Brace 	spin_unlock_irqrestore(&h->lock, flags);
74416636e7f4SDon Brace }
74426636e7f4SDon Brace 
74436636e7f4SDon Brace static void hpsa_monitor_ctlr_worker(struct work_struct *work)
74446636e7f4SDon Brace {
74456636e7f4SDon Brace 	unsigned long flags;
74466636e7f4SDon Brace 	struct ctlr_info *h = container_of(to_delayed_work(work),
74476636e7f4SDon Brace 					struct ctlr_info, monitor_ctlr_work);
74486636e7f4SDon Brace 
74496636e7f4SDon Brace 	detect_controller_lockup(h);
74506636e7f4SDon Brace 	if (lockup_detected(h))
74516636e7f4SDon Brace 		return;
74529846590eSStephen M. Cameron 
74538a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
74546636e7f4SDon Brace 	if (!h->remove_in_progress)
74558a98db73SStephen M. Cameron 		schedule_delayed_work(&h->monitor_ctlr_work,
74568a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
74578a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
7458a0c12413SStephen M. Cameron }
7459a0c12413SStephen M. Cameron 
74606636e7f4SDon Brace static struct workqueue_struct *hpsa_create_controller_wq(struct ctlr_info *h,
74616636e7f4SDon Brace 						char *name)
74626636e7f4SDon Brace {
74636636e7f4SDon Brace 	struct workqueue_struct *wq = NULL;
74646636e7f4SDon Brace 
7465397ea9cbSDon Brace 	wq = alloc_ordered_workqueue("%s_%d_hpsa", 0, name, h->ctlr);
74666636e7f4SDon Brace 	if (!wq)
74676636e7f4SDon Brace 		dev_err(&h->pdev->dev, "failed to create %s workqueue\n", name);
74686636e7f4SDon Brace 
74696636e7f4SDon Brace 	return wq;
74706636e7f4SDon Brace }
74716636e7f4SDon Brace 
74726f039790SGreg Kroah-Hartman static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
74734c2a8c40SStephen M. Cameron {
74744c2a8c40SStephen M. Cameron 	int dac, rc;
74754c2a8c40SStephen M. Cameron 	struct ctlr_info *h;
747664670ac8SStephen M. Cameron 	int try_soft_reset = 0;
747764670ac8SStephen M. Cameron 	unsigned long flags;
74786b6c1cd7STomas Henzl 	u32 board_id;
74794c2a8c40SStephen M. Cameron 
74804c2a8c40SStephen M. Cameron 	if (number_of_controllers == 0)
74814c2a8c40SStephen M. Cameron 		printk(KERN_INFO DRIVER_NAME "\n");
74824c2a8c40SStephen M. Cameron 
74836b6c1cd7STomas Henzl 	rc = hpsa_lookup_board_id(pdev, &board_id);
74846b6c1cd7STomas Henzl 	if (rc < 0) {
74856b6c1cd7STomas Henzl 		dev_warn(&pdev->dev, "Board ID not found\n");
74866b6c1cd7STomas Henzl 		return rc;
74876b6c1cd7STomas Henzl 	}
74886b6c1cd7STomas Henzl 
74896b6c1cd7STomas Henzl 	rc = hpsa_init_reset_devices(pdev, board_id);
749064670ac8SStephen M. Cameron 	if (rc) {
749164670ac8SStephen M. Cameron 		if (rc != -ENOTSUPP)
74924c2a8c40SStephen M. Cameron 			return rc;
749364670ac8SStephen M. Cameron 		/* If the reset fails in a particular way (it has no way to do
749464670ac8SStephen M. Cameron 		 * a proper hard reset, so returns -ENOTSUPP) we can try to do
749564670ac8SStephen M. Cameron 		 * a soft reset once we get the controller configured up to the
749664670ac8SStephen M. Cameron 		 * point that it can accept a command.
749764670ac8SStephen M. Cameron 		 */
749864670ac8SStephen M. Cameron 		try_soft_reset = 1;
749964670ac8SStephen M. Cameron 		rc = 0;
750064670ac8SStephen M. Cameron 	}
750164670ac8SStephen M. Cameron 
750264670ac8SStephen M. Cameron reinit_after_soft_reset:
75034c2a8c40SStephen M. Cameron 
7504303932fdSDon Brace 	/* Command structures must be aligned on a 32-byte boundary because
7505303932fdSDon Brace 	 * the 5 lower bits of the address are used by the hardware. and by
7506303932fdSDon Brace 	 * the driver.  See comments in hpsa.h for more info.
7507303932fdSDon Brace 	 */
7508303932fdSDon Brace 	BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
7509edd16368SStephen M. Cameron 	h = kzalloc(sizeof(*h), GFP_KERNEL);
7510105a3dbcSRobert Elliott 	if (!h) {
7511105a3dbcSRobert Elliott 		dev_err(&pdev->dev, "Failed to allocate controller head\n");
7512ecd9aad4SStephen M. Cameron 		return -ENOMEM;
7513105a3dbcSRobert Elliott 	}
7514edd16368SStephen M. Cameron 
751555c06c71SStephen M. Cameron 	h->pdev = pdev;
7516105a3dbcSRobert Elliott 
7517a9a3a273SStephen M. Cameron 	h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
75189846590eSStephen M. Cameron 	INIT_LIST_HEAD(&h->offline_device_list);
75196eaf46fdSStephen M. Cameron 	spin_lock_init(&h->lock);
75209846590eSStephen M. Cameron 	spin_lock_init(&h->offline_device_lock);
75216eaf46fdSStephen M. Cameron 	spin_lock_init(&h->scan_lock);
752234f0c627SDon Brace 	atomic_set(&h->passthru_cmds_avail, HPSA_MAX_CONCURRENT_PASSTHRUS);
75239b5c48c2SStephen Cameron 	atomic_set(&h->abort_cmds_available, HPSA_CMDS_RESERVED_FOR_ABORTS);
7524094963daSStephen M. Cameron 
75256636e7f4SDon Brace 	h->rescan_ctlr_wq = hpsa_create_controller_wq(h, "rescan");
75266636e7f4SDon Brace 	if (!h->rescan_ctlr_wq) {
7527080ef1ccSDon Brace 		rc = -ENOMEM;
7528080ef1ccSDon Brace 		goto clean1;
7529080ef1ccSDon Brace 	}
75306636e7f4SDon Brace 
75316636e7f4SDon Brace 	h->resubmit_wq = hpsa_create_controller_wq(h, "resubmit");
75326636e7f4SDon Brace 	if (!h->resubmit_wq) {
75336636e7f4SDon Brace 		rc = -ENOMEM;
7534105a3dbcSRobert Elliott 		goto clean1;	/* aer/h */
75356636e7f4SDon Brace 	}
75366636e7f4SDon Brace 
7537094963daSStephen M. Cameron 	/* Allocate and clear per-cpu variable lockup_detected */
7538094963daSStephen M. Cameron 	h->lockup_detected = alloc_percpu(u32);
75392a5ac326SStephen M. Cameron 	if (!h->lockup_detected) {
7540105a3dbcSRobert Elliott 		dev_err(&h->pdev->dev, "Failed to allocate lockup detector\n");
75412a5ac326SStephen M. Cameron 		rc = -ENOMEM;
7542105a3dbcSRobert Elliott 		goto clean1;	/* wq/aer/h */
75432a5ac326SStephen M. Cameron 	}
7544094963daSStephen M. Cameron 	set_lockup_detected_for_all_cpus(h, 0);
7545094963daSStephen M. Cameron 
754655c06c71SStephen M. Cameron 	rc = hpsa_pci_init(h);
7547105a3dbcSRobert Elliott 	if (rc)
7548105a3dbcSRobert Elliott 		goto clean2;	/* lockup, wq/aer/h */
7549edd16368SStephen M. Cameron 
7550f79cfec6SStephen M. Cameron 	sprintf(h->devname, HPSA "%d", number_of_controllers);
7551edd16368SStephen M. Cameron 	h->ctlr = number_of_controllers;
7552edd16368SStephen M. Cameron 	number_of_controllers++;
7553edd16368SStephen M. Cameron 
7554edd16368SStephen M. Cameron 	/* configure PCI DMA stuff */
7555ecd9aad4SStephen M. Cameron 	rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
7556ecd9aad4SStephen M. Cameron 	if (rc == 0) {
7557edd16368SStephen M. Cameron 		dac = 1;
7558ecd9aad4SStephen M. Cameron 	} else {
7559ecd9aad4SStephen M. Cameron 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7560ecd9aad4SStephen M. Cameron 		if (rc == 0) {
7561edd16368SStephen M. Cameron 			dac = 0;
7562ecd9aad4SStephen M. Cameron 		} else {
7563edd16368SStephen M. Cameron 			dev_err(&pdev->dev, "no suitable DMA available\n");
7564105a3dbcSRobert Elliott 			goto clean3;	/* pci, lockup, wq/aer/h */
7565edd16368SStephen M. Cameron 		}
7566ecd9aad4SStephen M. Cameron 	}
7567edd16368SStephen M. Cameron 
7568edd16368SStephen M. Cameron 	/* make sure the board interrupts are off */
7569edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
757010f66018SStephen M. Cameron 
7571105a3dbcSRobert Elliott 	rc = hpsa_request_irqs(h, do_hpsa_intr_msi, do_hpsa_intr_intx);
7572105a3dbcSRobert Elliott 	if (rc)
7573105a3dbcSRobert Elliott 		goto clean3;	/* pci, lockup, wq/aer/h */
7574303932fdSDon Brace 	dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
7575303932fdSDon Brace 	       h->devname, pdev->device,
7576a9a3a273SStephen M. Cameron 	       h->intr[h->intr_mode], dac ? "" : " not");
7577d37ffbe4SRobert Elliott 	rc = hpsa_alloc_cmd_pool(h);
75788947fd10SRobert Elliott 	if (rc)
7579105a3dbcSRobert Elliott 		goto clean4;	/* irq, pci, lockup, wq/aer/h */
7580105a3dbcSRobert Elliott 	rc = hpsa_alloc_sg_chain_blocks(h);
7581105a3dbcSRobert Elliott 	if (rc)
7582105a3dbcSRobert Elliott 		goto clean5;	/* cmd, irq, pci, lockup, wq/aer/h */
7583a08a8471SStephen M. Cameron 	init_waitqueue_head(&h->scan_wait_queue);
75849b5c48c2SStephen Cameron 	init_waitqueue_head(&h->abort_cmd_wait_queue);
7585a08a8471SStephen M. Cameron 	h->scan_finished = 1; /* no scan currently in progress */
7586edd16368SStephen M. Cameron 
7587edd16368SStephen M. Cameron 	pci_set_drvdata(pdev, h);
75889a41338eSStephen M. Cameron 	h->ndevices = 0;
7589316b221aSStephen M. Cameron 	h->hba_mode_enabled = 0;
75909a41338eSStephen M. Cameron 	h->scsi_host = NULL;
75919a41338eSStephen M. Cameron 	spin_lock_init(&h->devlock);
7592105a3dbcSRobert Elliott 	rc = hpsa_put_ctlr_into_performant_mode(h);
7593105a3dbcSRobert Elliott 	if (rc)
7594105a3dbcSRobert Elliott 		goto clean6;	/* sg, cmd, irq, pci, lockup, wq/aer/h */
759564670ac8SStephen M. Cameron 
7596105a3dbcSRobert Elliott 	/*
7597105a3dbcSRobert Elliott 	 * At this point, the controller is ready to take commands.
759864670ac8SStephen M. Cameron 	 * Now, if reset_devices and the hard reset didn't work, try
759964670ac8SStephen M. Cameron 	 * the soft reset and see if that works.
760064670ac8SStephen M. Cameron 	 */
760164670ac8SStephen M. Cameron 	if (try_soft_reset) {
760264670ac8SStephen M. Cameron 
760364670ac8SStephen M. Cameron 		/* This is kind of gross.  We may or may not get a completion
760464670ac8SStephen M. Cameron 		 * from the soft reset command, and if we do, then the value
760564670ac8SStephen M. Cameron 		 * from the fifo may or may not be valid.  So, we wait 10 secs
760664670ac8SStephen M. Cameron 		 * after the reset throwing away any completions we get during
760764670ac8SStephen M. Cameron 		 * that time.  Unregister the interrupt handler and register
760864670ac8SStephen M. Cameron 		 * fake ones to scoop up any residual completions.
760964670ac8SStephen M. Cameron 		 */
761064670ac8SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
761164670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
761264670ac8SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
7613ec501a18SRobert Elliott 		hpsa_free_irqs(h);
76149ee61794SRobert Elliott 		rc = hpsa_request_irqs(h, hpsa_msix_discard_completions,
761564670ac8SStephen M. Cameron 					hpsa_intx_discard_completions);
761664670ac8SStephen M. Cameron 		if (rc) {
76179ee61794SRobert Elliott 			dev_warn(&h->pdev->dev,
76189ee61794SRobert Elliott 				"Failed to request_irq after soft reset.\n");
761964670ac8SStephen M. Cameron 			goto clean4;
762064670ac8SStephen M. Cameron 		}
762164670ac8SStephen M. Cameron 
762264670ac8SStephen M. Cameron 		rc = hpsa_kdump_soft_reset(h);
762364670ac8SStephen M. Cameron 		if (rc)
762464670ac8SStephen M. Cameron 			/* Neither hard nor soft reset worked, we're hosed. */
762564670ac8SStephen M. Cameron 			goto clean4;
762664670ac8SStephen M. Cameron 
762764670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev, "Board READY.\n");
762864670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev,
762964670ac8SStephen M. Cameron 			"Waiting for stale completions to drain.\n");
763064670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_ON);
763164670ac8SStephen M. Cameron 		msleep(10000);
763264670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
763364670ac8SStephen M. Cameron 
763464670ac8SStephen M. Cameron 		rc = controller_reset_failed(h->cfgtable);
763564670ac8SStephen M. Cameron 		if (rc)
763664670ac8SStephen M. Cameron 			dev_info(&h->pdev->dev,
763764670ac8SStephen M. Cameron 				"Soft reset appears to have failed.\n");
763864670ac8SStephen M. Cameron 
763964670ac8SStephen M. Cameron 		/* since the controller's reset, we have to go back and re-init
764064670ac8SStephen M. Cameron 		 * everything.  Easiest to just forget what we've done and do it
764164670ac8SStephen M. Cameron 		 * all over again.
764264670ac8SStephen M. Cameron 		 */
764364670ac8SStephen M. Cameron 		hpsa_undo_allocations_after_kdump_soft_reset(h);
764464670ac8SStephen M. Cameron 		try_soft_reset = 0;
764564670ac8SStephen M. Cameron 		if (rc)
764664670ac8SStephen M. Cameron 			/* don't go to clean4, we already unallocated */
764764670ac8SStephen M. Cameron 			return -ENODEV;
764864670ac8SStephen M. Cameron 
764964670ac8SStephen M. Cameron 		goto reinit_after_soft_reset;
765064670ac8SStephen M. Cameron 	}
7651edd16368SStephen M. Cameron 
7652da0697bdSScott Teel 	/* Enable Accelerated IO path at driver layer */
7653da0697bdSScott Teel 	h->acciopath_status = 1;
7654da0697bdSScott Teel 
7655e863d68eSScott Teel 
7656edd16368SStephen M. Cameron 	/* Turn the interrupts on so we can service requests */
7657edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_ON);
7658edd16368SStephen M. Cameron 
7659339b2b14SStephen M. Cameron 	hpsa_hba_inquiry(h);
76604a4384ceSStephen Cameron 	rc = hpsa_register_scsi(h);	/* hook ourselves into SCSI subsystem */
76614a4384ceSStephen Cameron 	if (rc)
7662105a3dbcSRobert Elliott 		goto clean7;
76638a98db73SStephen M. Cameron 
76648a98db73SStephen M. Cameron 	/* Monitor the controller for firmware lockups */
76658a98db73SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
76668a98db73SStephen M. Cameron 	INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
76678a98db73SStephen M. Cameron 	schedule_delayed_work(&h->monitor_ctlr_work,
76688a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
76696636e7f4SDon Brace 	INIT_DELAYED_WORK(&h->rescan_ctlr_work, hpsa_rescan_ctlr_worker);
76706636e7f4SDon Brace 	queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
76716636e7f4SDon Brace 				h->heartbeat_sample_interval);
767288bf6d62SStephen M. Cameron 	return 0;
7673edd16368SStephen M. Cameron 
7674105a3dbcSRobert Elliott clean7: /* perf, sg, cmd, irq, pci, lockup, wq/aer/h */
7675105a3dbcSRobert Elliott 	kfree(h->hba_inquiry_data);
7676105a3dbcSRobert Elliott 	hpsa_free_performant_mode(h);
7677105a3dbcSRobert Elliott 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
7678105a3dbcSRobert Elliott clean6: /* sg, cmd, irq, pci, lockup, wq/aer/h */
767933a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
7680105a3dbcSRobert Elliott clean5: /* cmd, irq, pci, lockup, wq/aer/h */
76812e9d1b36SStephen M. Cameron 	hpsa_free_cmd_pool(h);
7682105a3dbcSRobert Elliott clean4: /* irq, pci, lockup, wq/aer/h */
7683ec501a18SRobert Elliott 	hpsa_free_irqs(h);
7684105a3dbcSRobert Elliott clean3: /* pci, lockup, wq/aer/h */
7685195f2c65SRobert Elliott 	hpsa_free_pci_init(h);
7686105a3dbcSRobert Elliott clean2: /* lockup, wq/aer/h */
7687105a3dbcSRobert Elliott 	if (h->lockup_detected) {
7688094963daSStephen M. Cameron 		free_percpu(h->lockup_detected);
7689105a3dbcSRobert Elliott 		h->lockup_detected = NULL;
7690105a3dbcSRobert Elliott 	}
7691105a3dbcSRobert Elliott clean1:	/* wq/aer/h */
7692105a3dbcSRobert Elliott 	if (h->resubmit_wq) {
7693105a3dbcSRobert Elliott 		destroy_workqueue(h->resubmit_wq);
7694105a3dbcSRobert Elliott 		h->resubmit_wq = NULL;
7695105a3dbcSRobert Elliott 	}
7696105a3dbcSRobert Elliott 	if (h->rescan_ctlr_wq) {
7697105a3dbcSRobert Elliott 		destroy_workqueue(h->rescan_ctlr_wq);
7698105a3dbcSRobert Elliott 		h->rescan_ctlr_wq = NULL;
7699105a3dbcSRobert Elliott 	}
7700edd16368SStephen M. Cameron 	kfree(h);
7701ecd9aad4SStephen M. Cameron 	return rc;
7702edd16368SStephen M. Cameron }
7703edd16368SStephen M. Cameron 
7704edd16368SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h)
7705edd16368SStephen M. Cameron {
7706edd16368SStephen M. Cameron 	char *flush_buf;
7707edd16368SStephen M. Cameron 	struct CommandList *c;
770825163bd5SWebb Scales 	int rc;
7709702890e3SStephen M. Cameron 
7710094963daSStephen M. Cameron 	if (unlikely(lockup_detected(h)))
7711702890e3SStephen M. Cameron 		return;
7712edd16368SStephen M. Cameron 	flush_buf = kzalloc(4, GFP_KERNEL);
7713edd16368SStephen M. Cameron 	if (!flush_buf)
7714edd16368SStephen M. Cameron 		return;
7715edd16368SStephen M. Cameron 
771645fcb86eSStephen Cameron 	c = cmd_alloc(h);
7717bf43caf3SRobert Elliott 
7718a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
7719a2dac136SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_CMD)) {
7720a2dac136SStephen M. Cameron 		goto out;
7721a2dac136SStephen M. Cameron 	}
772225163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
772325163bd5SWebb Scales 					PCI_DMA_TODEVICE, NO_TIMEOUT);
772425163bd5SWebb Scales 	if (rc)
772525163bd5SWebb Scales 		goto out;
7726edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus != 0)
7727a2dac136SStephen M. Cameron out:
7728edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
7729edd16368SStephen M. Cameron 			"error flushing cache on controller\n");
773045fcb86eSStephen Cameron 	cmd_free(h, c);
7731edd16368SStephen M. Cameron 	kfree(flush_buf);
7732edd16368SStephen M. Cameron }
7733edd16368SStephen M. Cameron 
7734edd16368SStephen M. Cameron static void hpsa_shutdown(struct pci_dev *pdev)
7735edd16368SStephen M. Cameron {
7736edd16368SStephen M. Cameron 	struct ctlr_info *h;
7737edd16368SStephen M. Cameron 
7738edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
7739edd16368SStephen M. Cameron 	/* Turn board interrupts off  and send the flush cache command
7740edd16368SStephen M. Cameron 	 * sendcmd will turn off interrupt, and send the flush...
7741edd16368SStephen M. Cameron 	 * To write all data in the battery backed cache to disks
7742edd16368SStephen M. Cameron 	 */
7743edd16368SStephen M. Cameron 	hpsa_flush_cache(h);
7744edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
7745105a3dbcSRobert Elliott 	hpsa_free_irqs(h);			/* init_one 4 */
7746cc64c817SRobert Elliott 	hpsa_disable_interrupt_mode(h);		/* pci_init 2 */
7747edd16368SStephen M. Cameron }
7748edd16368SStephen M. Cameron 
77496f039790SGreg Kroah-Hartman static void hpsa_free_device_info(struct ctlr_info *h)
775055e14e76SStephen M. Cameron {
775155e14e76SStephen M. Cameron 	int i;
775255e14e76SStephen M. Cameron 
7753105a3dbcSRobert Elliott 	for (i = 0; i < h->ndevices; i++) {
775455e14e76SStephen M. Cameron 		kfree(h->dev[i]);
7755105a3dbcSRobert Elliott 		h->dev[i] = NULL;
7756105a3dbcSRobert Elliott 	}
775755e14e76SStephen M. Cameron }
775855e14e76SStephen M. Cameron 
77596f039790SGreg Kroah-Hartman static void hpsa_remove_one(struct pci_dev *pdev)
7760edd16368SStephen M. Cameron {
7761edd16368SStephen M. Cameron 	struct ctlr_info *h;
77628a98db73SStephen M. Cameron 	unsigned long flags;
7763edd16368SStephen M. Cameron 
7764edd16368SStephen M. Cameron 	if (pci_get_drvdata(pdev) == NULL) {
7765edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "unable to remove device\n");
7766edd16368SStephen M. Cameron 		return;
7767edd16368SStephen M. Cameron 	}
7768edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
77698a98db73SStephen M. Cameron 
77708a98db73SStephen M. Cameron 	/* Get rid of any controller monitoring work items */
77718a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
77728a98db73SStephen M. Cameron 	h->remove_in_progress = 1;
77738a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
77746636e7f4SDon Brace 	cancel_delayed_work_sync(&h->monitor_ctlr_work);
77756636e7f4SDon Brace 	cancel_delayed_work_sync(&h->rescan_ctlr_work);
77766636e7f4SDon Brace 	destroy_workqueue(h->rescan_ctlr_wq);
77776636e7f4SDon Brace 	destroy_workqueue(h->resubmit_wq);
7778cc64c817SRobert Elliott 
7779105a3dbcSRobert Elliott 	/* includes hpsa_free_irqs - init_one 4 */
7780195f2c65SRobert Elliott 	/* includes hpsa_disable_interrupt_mode - pci_init 2 */
7781edd16368SStephen M. Cameron 	hpsa_shutdown(pdev);
7782cc64c817SRobert Elliott 
7783105a3dbcSRobert Elliott 	hpsa_free_device_info(h);		/* scan */
7784105a3dbcSRobert Elliott 
7785105a3dbcSRobert Elliott 	hpsa_unregister_scsi(h);			/* init_one "8" */
7786105a3dbcSRobert Elliott 	kfree(h->hba_inquiry_data);			/* init_one "8" */
7787105a3dbcSRobert Elliott 	h->hba_inquiry_data = NULL;			/* init_one "8" */
7788105a3dbcSRobert Elliott 	hpsa_free_performant_mode(h);			/* init_one 7 */
7789105a3dbcSRobert Elliott 	hpsa_free_sg_chain_blocks(h);			/* init_one 6 */
77901fb7c98aSRobert Elliott 	hpsa_free_cmd_pool(h);				/* init_one 5 */
7791105a3dbcSRobert Elliott 
7792105a3dbcSRobert Elliott 	/* hpsa_free_irqs already called via hpsa_shutdown init_one 4 */
7793195f2c65SRobert Elliott 
7794195f2c65SRobert Elliott 	/* includes hpsa_disable_interrupt_mode - pci_init 2 */
7795105a3dbcSRobert Elliott 	hpsa_free_pci_init(h);				/* init_one 3 */
7796195f2c65SRobert Elliott 
7797105a3dbcSRobert Elliott 	free_percpu(h->lockup_detected);		/* init_one 2 */
7798105a3dbcSRobert Elliott 	h->lockup_detected = NULL;			/* init_one 2 */
7799105a3dbcSRobert Elliott 	/* (void) pci_disable_pcie_error_reporting(pdev); */	/* init_one 1 */
7800105a3dbcSRobert Elliott 	kfree(h);					/* init_one 1 */
7801edd16368SStephen M. Cameron }
7802edd16368SStephen M. Cameron 
7803edd16368SStephen M. Cameron static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
7804edd16368SStephen M. Cameron 	__attribute__((unused)) pm_message_t state)
7805edd16368SStephen M. Cameron {
7806edd16368SStephen M. Cameron 	return -ENOSYS;
7807edd16368SStephen M. Cameron }
7808edd16368SStephen M. Cameron 
7809edd16368SStephen M. Cameron static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
7810edd16368SStephen M. Cameron {
7811edd16368SStephen M. Cameron 	return -ENOSYS;
7812edd16368SStephen M. Cameron }
7813edd16368SStephen M. Cameron 
7814edd16368SStephen M. Cameron static struct pci_driver hpsa_pci_driver = {
7815f79cfec6SStephen M. Cameron 	.name = HPSA,
7816edd16368SStephen M. Cameron 	.probe = hpsa_init_one,
78176f039790SGreg Kroah-Hartman 	.remove = hpsa_remove_one,
7818edd16368SStephen M. Cameron 	.id_table = hpsa_pci_device_id,	/* id_table */
7819edd16368SStephen M. Cameron 	.shutdown = hpsa_shutdown,
7820edd16368SStephen M. Cameron 	.suspend = hpsa_suspend,
7821edd16368SStephen M. Cameron 	.resume = hpsa_resume,
7822edd16368SStephen M. Cameron };
7823edd16368SStephen M. Cameron 
7824303932fdSDon Brace /* Fill in bucket_map[], given nsgs (the max number of
7825303932fdSDon Brace  * scatter gather elements supported) and bucket[],
7826303932fdSDon Brace  * which is an array of 8 integers.  The bucket[] array
7827303932fdSDon Brace  * contains 8 different DMA transfer sizes (in 16
7828303932fdSDon Brace  * byte increments) which the controller uses to fetch
7829303932fdSDon Brace  * commands.  This function fills in bucket_map[], which
7830303932fdSDon Brace  * maps a given number of scatter gather elements to one of
7831303932fdSDon Brace  * the 8 DMA transfer sizes.  The point of it is to allow the
7832303932fdSDon Brace  * controller to only do as much DMA as needed to fetch the
7833303932fdSDon Brace  * command, with the DMA transfer size encoded in the lower
7834303932fdSDon Brace  * bits of the command address.
7835303932fdSDon Brace  */
7836303932fdSDon Brace static void  calc_bucket_map(int bucket[], int num_buckets,
78372b08b3e9SDon Brace 	int nsgs, int min_blocks, u32 *bucket_map)
7838303932fdSDon Brace {
7839303932fdSDon Brace 	int i, j, b, size;
7840303932fdSDon Brace 
7841303932fdSDon Brace 	/* Note, bucket_map must have nsgs+1 entries. */
7842303932fdSDon Brace 	for (i = 0; i <= nsgs; i++) {
7843303932fdSDon Brace 		/* Compute size of a command with i SG entries */
7844e1f7de0cSMatt Gates 		size = i + min_blocks;
7845303932fdSDon Brace 		b = num_buckets; /* Assume the biggest bucket */
7846303932fdSDon Brace 		/* Find the bucket that is just big enough */
7847e1f7de0cSMatt Gates 		for (j = 0; j < num_buckets; j++) {
7848303932fdSDon Brace 			if (bucket[j] >= size) {
7849303932fdSDon Brace 				b = j;
7850303932fdSDon Brace 				break;
7851303932fdSDon Brace 			}
7852303932fdSDon Brace 		}
7853303932fdSDon Brace 		/* for a command with i SG entries, use bucket b. */
7854303932fdSDon Brace 		bucket_map[i] = b;
7855303932fdSDon Brace 	}
7856303932fdSDon Brace }
7857303932fdSDon Brace 
7858105a3dbcSRobert Elliott /*
7859105a3dbcSRobert Elliott  * return -ENODEV on err, 0 on success (or no action)
7860105a3dbcSRobert Elliott  * allocates numerous items that must be freed later
7861105a3dbcSRobert Elliott  */
7862c706a795SRobert Elliott static int hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
7863303932fdSDon Brace {
78646c311b57SStephen M. Cameron 	int i;
78656c311b57SStephen M. Cameron 	unsigned long register_value;
7866e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
7867e1f7de0cSMatt Gates 			(trans_support & CFGTBL_Trans_use_short_tags) |
7868e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix |
7869b9af4937SStephen M. Cameron 			(trans_support & (CFGTBL_Trans_io_accel1 |
7870b9af4937SStephen M. Cameron 				CFGTBL_Trans_io_accel2));
7871e1f7de0cSMatt Gates 	struct access_method access = SA5_performant_access;
7872def342bdSStephen M. Cameron 
7873def342bdSStephen M. Cameron 	/* This is a bit complicated.  There are 8 registers on
7874def342bdSStephen M. Cameron 	 * the controller which we write to to tell it 8 different
7875def342bdSStephen M. Cameron 	 * sizes of commands which there may be.  It's a way of
7876def342bdSStephen M. Cameron 	 * reducing the DMA done to fetch each command.  Encoded into
7877def342bdSStephen M. Cameron 	 * each command's tag are 3 bits which communicate to the controller
7878def342bdSStephen M. Cameron 	 * which of the eight sizes that command fits within.  The size of
7879def342bdSStephen M. Cameron 	 * each command depends on how many scatter gather entries there are.
7880def342bdSStephen M. Cameron 	 * Each SG entry requires 16 bytes.  The eight registers are programmed
7881def342bdSStephen M. Cameron 	 * with the number of 16-byte blocks a command of that size requires.
7882def342bdSStephen M. Cameron 	 * The smallest command possible requires 5 such 16 byte blocks.
7883d66ae08bSStephen M. Cameron 	 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
7884def342bdSStephen M. Cameron 	 * blocks.  Note, this only extends to the SG entries contained
7885def342bdSStephen M. Cameron 	 * within the command block, and does not extend to chained blocks
7886def342bdSStephen M. Cameron 	 * of SG elements.   bft[] contains the eight values we write to
7887def342bdSStephen M. Cameron 	 * the registers.  They are not evenly distributed, but have more
7888def342bdSStephen M. Cameron 	 * sizes for small commands, and fewer sizes for larger commands.
7889def342bdSStephen M. Cameron 	 */
7890d66ae08bSStephen M. Cameron 	int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
7891b9af4937SStephen M. Cameron #define MIN_IOACCEL2_BFT_ENTRY 5
7892b9af4937SStephen M. Cameron #define HPSA_IOACCEL2_HEADER_SZ 4
7893b9af4937SStephen M. Cameron 	int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
7894b9af4937SStephen M. Cameron 			13, 14, 15, 16, 17, 18, 19,
7895b9af4937SStephen M. Cameron 			HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
7896b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
7897b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
7898b9af4937SStephen M. Cameron 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
7899b9af4937SStephen M. Cameron 				 16 * MIN_IOACCEL2_BFT_ENTRY);
7900b9af4937SStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
7901d66ae08bSStephen M. Cameron 	BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
7902303932fdSDon Brace 	/*  5 = 1 s/g entry or 4k
7903303932fdSDon Brace 	 *  6 = 2 s/g entry or 8k
7904303932fdSDon Brace 	 *  8 = 4 s/g entry or 16k
7905303932fdSDon Brace 	 * 10 = 6 s/g entry or 24k
7906303932fdSDon Brace 	 */
7907303932fdSDon Brace 
7908b3a52e79SStephen M. Cameron 	/* If the controller supports either ioaccel method then
7909b3a52e79SStephen M. Cameron 	 * we can also use the RAID stack submit path that does not
7910b3a52e79SStephen M. Cameron 	 * perform the superfluous readl() after each command submission.
7911b3a52e79SStephen M. Cameron 	 */
7912b3a52e79SStephen M. Cameron 	if (trans_support & (CFGTBL_Trans_io_accel1 | CFGTBL_Trans_io_accel2))
7913b3a52e79SStephen M. Cameron 		access = SA5_performant_access_no_read;
7914b3a52e79SStephen M. Cameron 
7915303932fdSDon Brace 	/* Controller spec: zero out this buffer. */
7916072b0518SStephen M. Cameron 	for (i = 0; i < h->nreply_queues; i++)
7917072b0518SStephen M. Cameron 		memset(h->reply_queue[i].head, 0, h->reply_queue_size);
7918303932fdSDon Brace 
7919d66ae08bSStephen M. Cameron 	bft[7] = SG_ENTRIES_IN_CMD + 4;
7920d66ae08bSStephen M. Cameron 	calc_bucket_map(bft, ARRAY_SIZE(bft),
7921e1f7de0cSMatt Gates 				SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
7922303932fdSDon Brace 	for (i = 0; i < 8; i++)
7923303932fdSDon Brace 		writel(bft[i], &h->transtable->BlockFetch[i]);
7924303932fdSDon Brace 
7925303932fdSDon Brace 	/* size of controller ring buffer */
7926303932fdSDon Brace 	writel(h->max_commands, &h->transtable->RepQSize);
7927254f796bSMatt Gates 	writel(h->nreply_queues, &h->transtable->RepQCount);
7928303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrLow32);
7929303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrHigh32);
7930254f796bSMatt Gates 
7931254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
7932254f796bSMatt Gates 		writel(0, &h->transtable->RepQAddr[i].upper);
7933072b0518SStephen M. Cameron 		writel(h->reply_queue[i].busaddr,
7934254f796bSMatt Gates 			&h->transtable->RepQAddr[i].lower);
7935254f796bSMatt Gates 	}
7936254f796bSMatt Gates 
7937b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
7938e1f7de0cSMatt Gates 	writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
7939e1f7de0cSMatt Gates 	/*
7940e1f7de0cSMatt Gates 	 * enable outbound interrupt coalescing in accelerator mode;
7941e1f7de0cSMatt Gates 	 */
7942e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
7943e1f7de0cSMatt Gates 		access = SA5_ioaccel_mode1_access;
7944e1f7de0cSMatt Gates 		writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
7945e1f7de0cSMatt Gates 		writel(4, &h->cfgtable->HostWrite.CoalIntCount);
7946c349775eSScott Teel 	} else {
7947c349775eSScott Teel 		if (trans_support & CFGTBL_Trans_io_accel2) {
7948c349775eSScott Teel 			access = SA5_ioaccel_mode2_access;
7949c349775eSScott Teel 			writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
7950c349775eSScott Teel 			writel(4, &h->cfgtable->HostWrite.CoalIntCount);
7951c349775eSScott Teel 		}
7952e1f7de0cSMatt Gates 	}
7953303932fdSDon Brace 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
7954c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h)) {
7955c706a795SRobert Elliott 		dev_err(&h->pdev->dev,
7956c706a795SRobert Elliott 			"performant mode problem - doorbell timeout\n");
7957c706a795SRobert Elliott 		return -ENODEV;
7958c706a795SRobert Elliott 	}
7959303932fdSDon Brace 	register_value = readl(&(h->cfgtable->TransportActive));
7960303932fdSDon Brace 	if (!(register_value & CFGTBL_Trans_Performant)) {
7961050f7147SStephen Cameron 		dev_err(&h->pdev->dev,
7962050f7147SStephen Cameron 			"performant mode problem - transport not active\n");
7963c706a795SRobert Elliott 		return -ENODEV;
7964303932fdSDon Brace 	}
7965960a30e7SStephen M. Cameron 	/* Change the access methods to the performant access methods */
7966e1f7de0cSMatt Gates 	h->access = access;
7967e1f7de0cSMatt Gates 	h->transMethod = transMethod;
7968e1f7de0cSMatt Gates 
7969b9af4937SStephen M. Cameron 	if (!((trans_support & CFGTBL_Trans_io_accel1) ||
7970b9af4937SStephen M. Cameron 		(trans_support & CFGTBL_Trans_io_accel2)))
7971c706a795SRobert Elliott 		return 0;
7972e1f7de0cSMatt Gates 
7973b9af4937SStephen M. Cameron 	if (trans_support & CFGTBL_Trans_io_accel1) {
7974e1f7de0cSMatt Gates 		/* Set up I/O accelerator mode */
7975e1f7de0cSMatt Gates 		for (i = 0; i < h->nreply_queues; i++) {
7976e1f7de0cSMatt Gates 			writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
7977e1f7de0cSMatt Gates 			h->reply_queue[i].current_entry =
7978e1f7de0cSMatt Gates 				readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
7979e1f7de0cSMatt Gates 		}
7980283b4a9bSStephen M. Cameron 		bft[7] = h->ioaccel_maxsg + 8;
7981283b4a9bSStephen M. Cameron 		calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
7982e1f7de0cSMatt Gates 				h->ioaccel1_blockFetchTable);
7983e1f7de0cSMatt Gates 
7984e1f7de0cSMatt Gates 		/* initialize all reply queue entries to unused */
7985072b0518SStephen M. Cameron 		for (i = 0; i < h->nreply_queues; i++)
7986072b0518SStephen M. Cameron 			memset(h->reply_queue[i].head,
7987072b0518SStephen M. Cameron 				(u8) IOACCEL_MODE1_REPLY_UNUSED,
7988072b0518SStephen M. Cameron 				h->reply_queue_size);
7989e1f7de0cSMatt Gates 
7990e1f7de0cSMatt Gates 		/* set all the constant fields in the accelerator command
7991e1f7de0cSMatt Gates 		 * frames once at init time to save CPU cycles later.
7992e1f7de0cSMatt Gates 		 */
7993e1f7de0cSMatt Gates 		for (i = 0; i < h->nr_cmds; i++) {
7994e1f7de0cSMatt Gates 			struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
7995e1f7de0cSMatt Gates 
7996e1f7de0cSMatt Gates 			cp->function = IOACCEL1_FUNCTION_SCSIIO;
7997e1f7de0cSMatt Gates 			cp->err_info = (u32) (h->errinfo_pool_dhandle +
7998e1f7de0cSMatt Gates 					(i * sizeof(struct ErrorInfo)));
7999e1f7de0cSMatt Gates 			cp->err_info_len = sizeof(struct ErrorInfo);
8000e1f7de0cSMatt Gates 			cp->sgl_offset = IOACCEL1_SGLOFFSET;
80012b08b3e9SDon Brace 			cp->host_context_flags =
80022b08b3e9SDon Brace 				cpu_to_le16(IOACCEL1_HCFLAGS_CISS_FORMAT);
8003e1f7de0cSMatt Gates 			cp->timeout_sec = 0;
8004e1f7de0cSMatt Gates 			cp->ReplyQueue = 0;
800550a0decfSStephen M. Cameron 			cp->tag =
8006f2405db8SDon Brace 				cpu_to_le64((i << DIRECT_LOOKUP_SHIFT));
800750a0decfSStephen M. Cameron 			cp->host_addr =
800850a0decfSStephen M. Cameron 				cpu_to_le64(h->ioaccel_cmd_pool_dhandle +
8009e1f7de0cSMatt Gates 					(i * sizeof(struct io_accel1_cmd)));
8010e1f7de0cSMatt Gates 		}
8011b9af4937SStephen M. Cameron 	} else if (trans_support & CFGTBL_Trans_io_accel2) {
8012b9af4937SStephen M. Cameron 		u64 cfg_offset, cfg_base_addr_index;
8013b9af4937SStephen M. Cameron 		u32 bft2_offset, cfg_base_addr;
8014b9af4937SStephen M. Cameron 		int rc;
8015b9af4937SStephen M. Cameron 
8016b9af4937SStephen M. Cameron 		rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
8017b9af4937SStephen M. Cameron 			&cfg_base_addr_index, &cfg_offset);
8018b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
8019b9af4937SStephen M. Cameron 		bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
8020b9af4937SStephen M. Cameron 		calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
8021b9af4937SStephen M. Cameron 				4, h->ioaccel2_blockFetchTable);
8022b9af4937SStephen M. Cameron 		bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
8023b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct CfgTable,
8024b9af4937SStephen M. Cameron 				io_accel_request_size_offset) != 0xb8);
8025b9af4937SStephen M. Cameron 		h->ioaccel2_bft2_regs =
8026b9af4937SStephen M. Cameron 			remap_pci_mem(pci_resource_start(h->pdev,
8027b9af4937SStephen M. Cameron 					cfg_base_addr_index) +
8028b9af4937SStephen M. Cameron 					cfg_offset + bft2_offset,
8029b9af4937SStephen M. Cameron 					ARRAY_SIZE(bft2) *
8030b9af4937SStephen M. Cameron 					sizeof(*h->ioaccel2_bft2_regs));
8031b9af4937SStephen M. Cameron 		for (i = 0; i < ARRAY_SIZE(bft2); i++)
8032b9af4937SStephen M. Cameron 			writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
8033b9af4937SStephen M. Cameron 	}
8034b9af4937SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
8035c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h)) {
8036c706a795SRobert Elliott 		dev_err(&h->pdev->dev,
8037c706a795SRobert Elliott 			"performant mode problem - enabling ioaccel mode\n");
8038c706a795SRobert Elliott 		return -ENODEV;
8039c706a795SRobert Elliott 	}
8040c706a795SRobert Elliott 	return 0;
8041e1f7de0cSMatt Gates }
8042e1f7de0cSMatt Gates 
80431fb7c98aSRobert Elliott /* Free ioaccel1 mode command blocks and block fetch table */
80441fb7c98aSRobert Elliott static void hpsa_free_ioaccel1_cmd_and_bft(struct ctlr_info *h)
80451fb7c98aSRobert Elliott {
8046105a3dbcSRobert Elliott 	if (h->ioaccel_cmd_pool) {
80471fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
80481fb7c98aSRobert Elliott 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
80491fb7c98aSRobert Elliott 			h->ioaccel_cmd_pool,
80501fb7c98aSRobert Elliott 			h->ioaccel_cmd_pool_dhandle);
8051105a3dbcSRobert Elliott 		h->ioaccel_cmd_pool = NULL;
8052105a3dbcSRobert Elliott 		h->ioaccel_cmd_pool_dhandle = 0;
8053105a3dbcSRobert Elliott 	}
80541fb7c98aSRobert Elliott 	kfree(h->ioaccel1_blockFetchTable);
8055105a3dbcSRobert Elliott 	h->ioaccel1_blockFetchTable = NULL;
80561fb7c98aSRobert Elliott }
80571fb7c98aSRobert Elliott 
8058d37ffbe4SRobert Elliott /* Allocate ioaccel1 mode command blocks and block fetch table */
8059d37ffbe4SRobert Elliott static int hpsa_alloc_ioaccel1_cmd_and_bft(struct ctlr_info *h)
8060e1f7de0cSMatt Gates {
8061283b4a9bSStephen M. Cameron 	h->ioaccel_maxsg =
8062283b4a9bSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
8063283b4a9bSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
8064283b4a9bSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
8065283b4a9bSStephen M. Cameron 
8066e1f7de0cSMatt Gates 	/* Command structures must be aligned on a 128-byte boundary
8067e1f7de0cSMatt Gates 	 * because the 7 lower bits of the address are used by the
8068e1f7de0cSMatt Gates 	 * hardware.
8069e1f7de0cSMatt Gates 	 */
8070e1f7de0cSMatt Gates 	BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
8071e1f7de0cSMatt Gates 			IOACCEL1_COMMANDLIST_ALIGNMENT);
8072e1f7de0cSMatt Gates 	h->ioaccel_cmd_pool =
8073e1f7de0cSMatt Gates 		pci_alloc_consistent(h->pdev,
8074e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
8075e1f7de0cSMatt Gates 			&(h->ioaccel_cmd_pool_dhandle));
8076e1f7de0cSMatt Gates 
8077e1f7de0cSMatt Gates 	h->ioaccel1_blockFetchTable =
8078283b4a9bSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
8079e1f7de0cSMatt Gates 				sizeof(u32)), GFP_KERNEL);
8080e1f7de0cSMatt Gates 
8081e1f7de0cSMatt Gates 	if ((h->ioaccel_cmd_pool == NULL) ||
8082e1f7de0cSMatt Gates 		(h->ioaccel1_blockFetchTable == NULL))
8083e1f7de0cSMatt Gates 		goto clean_up;
8084e1f7de0cSMatt Gates 
8085e1f7de0cSMatt Gates 	memset(h->ioaccel_cmd_pool, 0,
8086e1f7de0cSMatt Gates 		h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
8087e1f7de0cSMatt Gates 	return 0;
8088e1f7de0cSMatt Gates 
8089e1f7de0cSMatt Gates clean_up:
80901fb7c98aSRobert Elliott 	hpsa_free_ioaccel1_cmd_and_bft(h);
80912dd02d74SRobert Elliott 	return -ENOMEM;
80926c311b57SStephen M. Cameron }
80936c311b57SStephen M. Cameron 
80941fb7c98aSRobert Elliott /* Free ioaccel2 mode command blocks and block fetch table */
80951fb7c98aSRobert Elliott static void hpsa_free_ioaccel2_cmd_and_bft(struct ctlr_info *h)
80961fb7c98aSRobert Elliott {
8097d9a729f3SWebb Scales 	hpsa_free_ioaccel2_sg_chain_blocks(h);
8098d9a729f3SWebb Scales 
8099105a3dbcSRobert Elliott 	if (h->ioaccel2_cmd_pool) {
81001fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
81011fb7c98aSRobert Elliott 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
81021fb7c98aSRobert Elliott 			h->ioaccel2_cmd_pool,
81031fb7c98aSRobert Elliott 			h->ioaccel2_cmd_pool_dhandle);
8104105a3dbcSRobert Elliott 		h->ioaccel2_cmd_pool = NULL;
8105105a3dbcSRobert Elliott 		h->ioaccel2_cmd_pool_dhandle = 0;
8106105a3dbcSRobert Elliott 	}
81071fb7c98aSRobert Elliott 	kfree(h->ioaccel2_blockFetchTable);
8108105a3dbcSRobert Elliott 	h->ioaccel2_blockFetchTable = NULL;
81091fb7c98aSRobert Elliott }
81101fb7c98aSRobert Elliott 
8111d37ffbe4SRobert Elliott /* Allocate ioaccel2 mode command blocks and block fetch table */
8112d37ffbe4SRobert Elliott static int hpsa_alloc_ioaccel2_cmd_and_bft(struct ctlr_info *h)
8113aca9012aSStephen M. Cameron {
8114d9a729f3SWebb Scales 	int rc;
8115d9a729f3SWebb Scales 
8116aca9012aSStephen M. Cameron 	/* Allocate ioaccel2 mode command blocks and block fetch table */
8117aca9012aSStephen M. Cameron 
8118aca9012aSStephen M. Cameron 	h->ioaccel_maxsg =
8119aca9012aSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
8120aca9012aSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
8121aca9012aSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
8122aca9012aSStephen M. Cameron 
8123aca9012aSStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
8124aca9012aSStephen M. Cameron 			IOACCEL2_COMMANDLIST_ALIGNMENT);
8125aca9012aSStephen M. Cameron 	h->ioaccel2_cmd_pool =
8126aca9012aSStephen M. Cameron 		pci_alloc_consistent(h->pdev,
8127aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
8128aca9012aSStephen M. Cameron 			&(h->ioaccel2_cmd_pool_dhandle));
8129aca9012aSStephen M. Cameron 
8130aca9012aSStephen M. Cameron 	h->ioaccel2_blockFetchTable =
8131aca9012aSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
8132aca9012aSStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
8133aca9012aSStephen M. Cameron 
8134aca9012aSStephen M. Cameron 	if ((h->ioaccel2_cmd_pool == NULL) ||
8135d9a729f3SWebb Scales 		(h->ioaccel2_blockFetchTable == NULL)) {
8136d9a729f3SWebb Scales 		rc = -ENOMEM;
8137d9a729f3SWebb Scales 		goto clean_up;
8138d9a729f3SWebb Scales 	}
8139d9a729f3SWebb Scales 
8140d9a729f3SWebb Scales 	rc = hpsa_allocate_ioaccel2_sg_chain_blocks(h);
8141d9a729f3SWebb Scales 	if (rc)
8142aca9012aSStephen M. Cameron 		goto clean_up;
8143aca9012aSStephen M. Cameron 
8144aca9012aSStephen M. Cameron 	memset(h->ioaccel2_cmd_pool, 0,
8145aca9012aSStephen M. Cameron 		h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
8146aca9012aSStephen M. Cameron 	return 0;
8147aca9012aSStephen M. Cameron 
8148aca9012aSStephen M. Cameron clean_up:
81491fb7c98aSRobert Elliott 	hpsa_free_ioaccel2_cmd_and_bft(h);
8150d9a729f3SWebb Scales 	return rc;
8151aca9012aSStephen M. Cameron }
8152aca9012aSStephen M. Cameron 
8153105a3dbcSRobert Elliott /* Free items allocated by hpsa_put_ctlr_into_performant_mode */
8154105a3dbcSRobert Elliott static void hpsa_free_performant_mode(struct ctlr_info *h)
8155105a3dbcSRobert Elliott {
8156105a3dbcSRobert Elliott 	kfree(h->blockFetchTable);
8157105a3dbcSRobert Elliott 	h->blockFetchTable = NULL;
8158105a3dbcSRobert Elliott 	hpsa_free_reply_queues(h);
8159105a3dbcSRobert Elliott 	hpsa_free_ioaccel1_cmd_and_bft(h);
8160105a3dbcSRobert Elliott 	hpsa_free_ioaccel2_cmd_and_bft(h);
8161105a3dbcSRobert Elliott }
8162105a3dbcSRobert Elliott 
8163105a3dbcSRobert Elliott /* return -ENODEV on error, 0 on success (or no action)
8164105a3dbcSRobert Elliott  * allocates numerous items that must be freed later
8165105a3dbcSRobert Elliott  */
8166105a3dbcSRobert Elliott static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
81676c311b57SStephen M. Cameron {
81686c311b57SStephen M. Cameron 	u32 trans_support;
8169e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
8170e1f7de0cSMatt Gates 					CFGTBL_Trans_use_short_tags;
8171105a3dbcSRobert Elliott 	int i, rc;
81726c311b57SStephen M. Cameron 
817302ec19c8SStephen M. Cameron 	if (hpsa_simple_mode)
8174105a3dbcSRobert Elliott 		return 0;
817502ec19c8SStephen M. Cameron 
817667c99a72Sscameron@beardog.cce.hp.com 	trans_support = readl(&(h->cfgtable->TransportSupport));
817767c99a72Sscameron@beardog.cce.hp.com 	if (!(trans_support & PERFORMANT_MODE))
8178105a3dbcSRobert Elliott 		return 0;
817967c99a72Sscameron@beardog.cce.hp.com 
8180e1f7de0cSMatt Gates 	/* Check for I/O accelerator mode support */
8181e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
8182e1f7de0cSMatt Gates 		transMethod |= CFGTBL_Trans_io_accel1 |
8183e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix;
8184105a3dbcSRobert Elliott 		rc = hpsa_alloc_ioaccel1_cmd_and_bft(h);
8185105a3dbcSRobert Elliott 		if (rc)
8186105a3dbcSRobert Elliott 			return rc;
8187105a3dbcSRobert Elliott 	} else if (trans_support & CFGTBL_Trans_io_accel2) {
8188aca9012aSStephen M. Cameron 		transMethod |= CFGTBL_Trans_io_accel2 |
8189aca9012aSStephen M. Cameron 				CFGTBL_Trans_enable_directed_msix;
8190105a3dbcSRobert Elliott 		rc = hpsa_alloc_ioaccel2_cmd_and_bft(h);
8191105a3dbcSRobert Elliott 		if (rc)
8192105a3dbcSRobert Elliott 			return rc;
8193e1f7de0cSMatt Gates 	}
8194e1f7de0cSMatt Gates 
8195eee0f03aSHannes Reinecke 	h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
8196cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
81976c311b57SStephen M. Cameron 	/* Performant mode ring buffer and supporting data structures */
8198072b0518SStephen M. Cameron 	h->reply_queue_size = h->max_commands * sizeof(u64);
81996c311b57SStephen M. Cameron 
8200254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
8201072b0518SStephen M. Cameron 		h->reply_queue[i].head = pci_alloc_consistent(h->pdev,
8202072b0518SStephen M. Cameron 						h->reply_queue_size,
8203072b0518SStephen M. Cameron 						&(h->reply_queue[i].busaddr));
8204105a3dbcSRobert Elliott 		if (!h->reply_queue[i].head) {
8205105a3dbcSRobert Elliott 			rc = -ENOMEM;
8206105a3dbcSRobert Elliott 			goto clean1;	/* rq, ioaccel */
8207105a3dbcSRobert Elliott 		}
8208254f796bSMatt Gates 		h->reply_queue[i].size = h->max_commands;
8209254f796bSMatt Gates 		h->reply_queue[i].wraparound = 1;  /* spec: init to 1 */
8210254f796bSMatt Gates 		h->reply_queue[i].current_entry = 0;
8211254f796bSMatt Gates 	}
8212254f796bSMatt Gates 
82136c311b57SStephen M. Cameron 	/* Need a block fetch table for performant mode */
8214d66ae08bSStephen M. Cameron 	h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
82156c311b57SStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
8216105a3dbcSRobert Elliott 	if (!h->blockFetchTable) {
8217105a3dbcSRobert Elliott 		rc = -ENOMEM;
8218105a3dbcSRobert Elliott 		goto clean1;	/* rq, ioaccel */
8219105a3dbcSRobert Elliott 	}
82206c311b57SStephen M. Cameron 
8221105a3dbcSRobert Elliott 	rc = hpsa_enter_performant_mode(h, trans_support);
8222105a3dbcSRobert Elliott 	if (rc)
8223105a3dbcSRobert Elliott 		goto clean2;	/* bft, rq, ioaccel */
8224105a3dbcSRobert Elliott 	return 0;
8225303932fdSDon Brace 
8226105a3dbcSRobert Elliott clean2:	/* bft, rq, ioaccel */
8227303932fdSDon Brace 	kfree(h->blockFetchTable);
8228105a3dbcSRobert Elliott 	h->blockFetchTable = NULL;
8229105a3dbcSRobert Elliott clean1:	/* rq, ioaccel */
8230105a3dbcSRobert Elliott 	hpsa_free_reply_queues(h);
8231105a3dbcSRobert Elliott 	hpsa_free_ioaccel1_cmd_and_bft(h);
8232105a3dbcSRobert Elliott 	hpsa_free_ioaccel2_cmd_and_bft(h);
8233105a3dbcSRobert Elliott 	return rc;
8234303932fdSDon Brace }
8235303932fdSDon Brace 
823623100dd9SStephen M. Cameron static int is_accelerated_cmd(struct CommandList *c)
823776438d08SStephen M. Cameron {
823823100dd9SStephen M. Cameron 	return c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_IOACCEL2;
823923100dd9SStephen M. Cameron }
824023100dd9SStephen M. Cameron 
824123100dd9SStephen M. Cameron static void hpsa_drain_accel_commands(struct ctlr_info *h)
824223100dd9SStephen M. Cameron {
824323100dd9SStephen M. Cameron 	struct CommandList *c = NULL;
8244f2405db8SDon Brace 	int i, accel_cmds_out;
8245281a7fd0SWebb Scales 	int refcount;
824676438d08SStephen M. Cameron 
8247f2405db8SDon Brace 	do { /* wait for all outstanding ioaccel commands to drain out */
824823100dd9SStephen M. Cameron 		accel_cmds_out = 0;
8249f2405db8SDon Brace 		for (i = 0; i < h->nr_cmds; i++) {
8250f2405db8SDon Brace 			c = h->cmd_pool + i;
8251281a7fd0SWebb Scales 			refcount = atomic_inc_return(&c->refcount);
8252281a7fd0SWebb Scales 			if (refcount > 1) /* Command is allocated */
825323100dd9SStephen M. Cameron 				accel_cmds_out += is_accelerated_cmd(c);
8254281a7fd0SWebb Scales 			cmd_free(h, c);
8255f2405db8SDon Brace 		}
825623100dd9SStephen M. Cameron 		if (accel_cmds_out <= 0)
825776438d08SStephen M. Cameron 			break;
825876438d08SStephen M. Cameron 		msleep(100);
825976438d08SStephen M. Cameron 	} while (1);
826076438d08SStephen M. Cameron }
826176438d08SStephen M. Cameron 
8262edd16368SStephen M. Cameron /*
8263edd16368SStephen M. Cameron  *  This is it.  Register the PCI driver information for the cards we control
8264edd16368SStephen M. Cameron  *  the OS will call our registered routines when it finds one of our cards.
8265edd16368SStephen M. Cameron  */
8266edd16368SStephen M. Cameron static int __init hpsa_init(void)
8267edd16368SStephen M. Cameron {
826831468401SMike Miller 	return pci_register_driver(&hpsa_pci_driver);
8269edd16368SStephen M. Cameron }
8270edd16368SStephen M. Cameron 
8271edd16368SStephen M. Cameron static void __exit hpsa_cleanup(void)
8272edd16368SStephen M. Cameron {
8273edd16368SStephen M. Cameron 	pci_unregister_driver(&hpsa_pci_driver);
8274edd16368SStephen M. Cameron }
8275edd16368SStephen M. Cameron 
8276e1f7de0cSMatt Gates static void __attribute__((unused)) verify_offsets(void)
8277e1f7de0cSMatt Gates {
8278e1f7de0cSMatt Gates #define VERIFY_OFFSET(member, offset) \
8279dd0e19f3SScott Teel 	BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
8280dd0e19f3SScott Teel 
8281dd0e19f3SScott Teel 	VERIFY_OFFSET(structure_size, 0);
8282dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_size, 4);
8283dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_cnt, 8);
8284dd0e19f3SScott Teel 	VERIFY_OFFSET(phys_blk_shift, 16);
8285dd0e19f3SScott Teel 	VERIFY_OFFSET(parity_rotation_shift, 17);
8286dd0e19f3SScott Teel 	VERIFY_OFFSET(strip_size, 18);
8287dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_starting_blk, 20);
8288dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_blk_cnt, 28);
8289dd0e19f3SScott Teel 	VERIFY_OFFSET(data_disks_per_row, 36);
8290dd0e19f3SScott Teel 	VERIFY_OFFSET(metadata_disks_per_row, 38);
8291dd0e19f3SScott Teel 	VERIFY_OFFSET(row_cnt, 40);
8292dd0e19f3SScott Teel 	VERIFY_OFFSET(layout_map_count, 42);
8293dd0e19f3SScott Teel 	VERIFY_OFFSET(flags, 44);
8294dd0e19f3SScott Teel 	VERIFY_OFFSET(dekindex, 46);
8295dd0e19f3SScott Teel 	/* VERIFY_OFFSET(reserved, 48 */
8296dd0e19f3SScott Teel 	VERIFY_OFFSET(data, 64);
8297dd0e19f3SScott Teel 
8298dd0e19f3SScott Teel #undef VERIFY_OFFSET
8299dd0e19f3SScott Teel 
8300dd0e19f3SScott Teel #define VERIFY_OFFSET(member, offset) \
8301b66cc250SMike Miller 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
8302b66cc250SMike Miller 
8303b66cc250SMike Miller 	VERIFY_OFFSET(IU_type, 0);
8304b66cc250SMike Miller 	VERIFY_OFFSET(direction, 1);
8305b66cc250SMike Miller 	VERIFY_OFFSET(reply_queue, 2);
8306b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved1, 3);  */
8307b66cc250SMike Miller 	VERIFY_OFFSET(scsi_nexus, 4);
8308b66cc250SMike Miller 	VERIFY_OFFSET(Tag, 8);
8309b66cc250SMike Miller 	VERIFY_OFFSET(cdb, 16);
8310b66cc250SMike Miller 	VERIFY_OFFSET(cciss_lun, 32);
8311b66cc250SMike Miller 	VERIFY_OFFSET(data_len, 40);
8312b66cc250SMike Miller 	VERIFY_OFFSET(cmd_priority_task_attr, 44);
8313b66cc250SMike Miller 	VERIFY_OFFSET(sg_count, 45);
8314b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved3 */
8315b66cc250SMike Miller 	VERIFY_OFFSET(err_ptr, 48);
8316b66cc250SMike Miller 	VERIFY_OFFSET(err_len, 56);
8317b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved4  */
8318b66cc250SMike Miller 	VERIFY_OFFSET(sg, 64);
8319b66cc250SMike Miller 
8320b66cc250SMike Miller #undef VERIFY_OFFSET
8321b66cc250SMike Miller 
8322b66cc250SMike Miller #define VERIFY_OFFSET(member, offset) \
8323e1f7de0cSMatt Gates 	BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
8324e1f7de0cSMatt Gates 
8325e1f7de0cSMatt Gates 	VERIFY_OFFSET(dev_handle, 0x00);
8326e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved1, 0x02);
8327e1f7de0cSMatt Gates 	VERIFY_OFFSET(function, 0x03);
8328e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved2, 0x04);
8329e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info, 0x0C);
8330e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved3, 0x10);
8331e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info_len, 0x12);
8332e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved4, 0x13);
8333e1f7de0cSMatt Gates 	VERIFY_OFFSET(sgl_offset, 0x14);
8334e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved5, 0x15);
8335e1f7de0cSMatt Gates 	VERIFY_OFFSET(transfer_len, 0x1C);
8336e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved6, 0x20);
8337e1f7de0cSMatt Gates 	VERIFY_OFFSET(io_flags, 0x24);
8338e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved7, 0x26);
8339e1f7de0cSMatt Gates 	VERIFY_OFFSET(LUN, 0x34);
8340e1f7de0cSMatt Gates 	VERIFY_OFFSET(control, 0x3C);
8341e1f7de0cSMatt Gates 	VERIFY_OFFSET(CDB, 0x40);
8342e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved8, 0x50);
8343e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_context_flags, 0x60);
8344e1f7de0cSMatt Gates 	VERIFY_OFFSET(timeout_sec, 0x62);
8345e1f7de0cSMatt Gates 	VERIFY_OFFSET(ReplyQueue, 0x64);
8346e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved9, 0x65);
834750a0decfSStephen M. Cameron 	VERIFY_OFFSET(tag, 0x68);
8348e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_addr, 0x70);
8349e1f7de0cSMatt Gates 	VERIFY_OFFSET(CISS_LUN, 0x78);
8350e1f7de0cSMatt Gates 	VERIFY_OFFSET(SG, 0x78 + 8);
8351e1f7de0cSMatt Gates #undef VERIFY_OFFSET
8352e1f7de0cSMatt Gates }
8353e1f7de0cSMatt Gates 
8354edd16368SStephen M. Cameron module_init(hpsa_init);
8355edd16368SStephen M. Cameron module_exit(hpsa_cleanup);
8356