xref: /openbmc/linux/drivers/scsi/hpsa.c (revision 8be986cc57f1f802a8cd8542ac309a0e6ac24a4b)
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 
864*8be986ccSStephen Cameron static void set_ioaccel2_tmf_performant_mode(struct ctlr_info *h,
865*8be986ccSStephen Cameron 						struct CommandList *c,
866*8be986ccSStephen Cameron 						int reply_queue)
867*8be986ccSStephen Cameron {
868*8be986ccSStephen Cameron 	struct hpsa_tmf_struct *cp = (struct hpsa_tmf_struct *)
869*8be986ccSStephen Cameron 		&h->ioaccel2_cmd_pool[c->cmdindex];
870*8be986ccSStephen Cameron 
871*8be986ccSStephen Cameron 	/* Tell the controller to post the reply to the queue for this
872*8be986ccSStephen Cameron 	 * processor.  This seems to give the best I/O throughput.
873*8be986ccSStephen Cameron 	 */
874*8be986ccSStephen Cameron 	if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
875*8be986ccSStephen Cameron 		cp->reply_queue = smp_processor_id() % h->nreply_queues;
876*8be986ccSStephen Cameron 	else
877*8be986ccSStephen Cameron 		cp->reply_queue = reply_queue % h->nreply_queues;
878*8be986ccSStephen Cameron 	/* Set the bits in the address sent down to include:
879*8be986ccSStephen Cameron 	 *  - performant mode bit not used in ioaccel mode 2
880*8be986ccSStephen Cameron 	 *  - pull count (bits 0-3)
881*8be986ccSStephen Cameron 	 *  - command type isn't needed for ioaccel2
882*8be986ccSStephen Cameron 	 */
883*8be986ccSStephen Cameron 	c->busaddr |= h->ioaccel2_blockFetchTable[0];
884*8be986ccSStephen Cameron }
885*8be986ccSStephen 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;
952*8be986ccSStephen Cameron 	case IOACCEL2_TMF:
953*8be986ccSStephen Cameron 		set_ioaccel2_tmf_performant_mode(h, c, reply_queue);
954*8be986ccSStephen Cameron 		writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
955*8be986ccSStephen 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 
1976c349775eSScott Teel static void process_ioaccel2_completion(struct ctlr_info *h,
1977c349775eSScott Teel 		struct CommandList *c, struct scsi_cmnd *cmd,
1978c349775eSScott Teel 		struct hpsa_scsi_dev_t *dev)
1979c349775eSScott Teel {
1980c349775eSScott Teel 	struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
1981c349775eSScott Teel 
1982c349775eSScott Teel 	/* check for good status */
1983c349775eSScott Teel 	if (likely(c2->error_data.serv_response == 0 &&
1984c349775eSScott Teel 			c2->error_data.status == 0)) {
1985c349775eSScott Teel 		cmd_free(h, c);
1986c349775eSScott Teel 		cmd->scsi_done(cmd);
1987c349775eSScott Teel 		return;
1988c349775eSScott Teel 	}
1989c349775eSScott Teel 
1990c349775eSScott Teel 	/* Any RAID offload error results in retry which will use
1991c349775eSScott Teel 	 * the normal I/O path so the controller can handle whatever's
1992c349775eSScott Teel 	 * wrong.
1993c349775eSScott Teel 	 */
1994c349775eSScott Teel 	if (is_logical_dev_addr_mode(dev->scsi3addr) &&
1995c349775eSScott Teel 		c2->error_data.serv_response ==
1996c349775eSScott Teel 			IOACCEL2_SERV_RESPONSE_FAILURE) {
1997080ef1ccSDon Brace 		if (c2->error_data.status ==
1998080ef1ccSDon Brace 			IOACCEL2_STATUS_SR_IOACCEL_DISABLED)
1999c349775eSScott Teel 			dev->offload_enabled = 0;
2000080ef1ccSDon Brace 		goto retry_cmd;
2001080ef1ccSDon Brace 	}
2002080ef1ccSDon Brace 
2003080ef1ccSDon Brace 	if (handle_ioaccel_mode2_error(h, c, cmd, c2))
2004080ef1ccSDon Brace 		goto retry_cmd;
2005080ef1ccSDon Brace 
2006c349775eSScott Teel 	cmd_free(h, c);
2007c349775eSScott Teel 	cmd->scsi_done(cmd);
2008c349775eSScott Teel 	return;
2009080ef1ccSDon Brace 
2010080ef1ccSDon Brace retry_cmd:
2011080ef1ccSDon Brace 	INIT_WORK(&c->work, hpsa_command_resubmit_worker);
2012080ef1ccSDon Brace 	queue_work_on(raw_smp_processor_id(), h->resubmit_wq, &c->work);
2013c349775eSScott Teel }
2014c349775eSScott Teel 
20159437ac43SStephen Cameron /* Returns 0 on success, < 0 otherwise. */
20169437ac43SStephen Cameron static int hpsa_evaluate_tmf_status(struct ctlr_info *h,
20179437ac43SStephen Cameron 					struct CommandList *cp)
20189437ac43SStephen Cameron {
20199437ac43SStephen Cameron 	u8 tmf_status = cp->err_info->ScsiStatus;
20209437ac43SStephen Cameron 
20219437ac43SStephen Cameron 	switch (tmf_status) {
20229437ac43SStephen Cameron 	case CISS_TMF_COMPLETE:
20239437ac43SStephen Cameron 		/*
20249437ac43SStephen Cameron 		 * CISS_TMF_COMPLETE never happens, instead,
20259437ac43SStephen Cameron 		 * ei->CommandStatus == 0 for this case.
20269437ac43SStephen Cameron 		 */
20279437ac43SStephen Cameron 	case CISS_TMF_SUCCESS:
20289437ac43SStephen Cameron 		return 0;
20299437ac43SStephen Cameron 	case CISS_TMF_INVALID_FRAME:
20309437ac43SStephen Cameron 	case CISS_TMF_NOT_SUPPORTED:
20319437ac43SStephen Cameron 	case CISS_TMF_FAILED:
20329437ac43SStephen Cameron 	case CISS_TMF_WRONG_LUN:
20339437ac43SStephen Cameron 	case CISS_TMF_OVERLAPPED_TAG:
20349437ac43SStephen Cameron 		break;
20359437ac43SStephen Cameron 	default:
20369437ac43SStephen Cameron 		dev_warn(&h->pdev->dev, "Unknown TMF status: 0x%02x\n",
20379437ac43SStephen Cameron 				tmf_status);
20389437ac43SStephen Cameron 		break;
20399437ac43SStephen Cameron 	}
20409437ac43SStephen Cameron 	return -tmf_status;
20419437ac43SStephen Cameron }
20429437ac43SStephen Cameron 
20431fb011fbSStephen M. Cameron static void complete_scsi_command(struct CommandList *cp)
2044edd16368SStephen M. Cameron {
2045edd16368SStephen M. Cameron 	struct scsi_cmnd *cmd;
2046edd16368SStephen M. Cameron 	struct ctlr_info *h;
2047edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2048283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
2049d9a729f3SWebb Scales 	struct io_accel2_cmd *c2;
2050edd16368SStephen M. Cameron 
20519437ac43SStephen Cameron 	u8 sense_key;
20529437ac43SStephen Cameron 	u8 asc;      /* additional sense code */
20539437ac43SStephen Cameron 	u8 ascq;     /* additional sense code qualifier */
2054db111e18SStephen M. Cameron 	unsigned long sense_data_size;
2055edd16368SStephen M. Cameron 
2056edd16368SStephen M. Cameron 	ei = cp->err_info;
20577fa3030cSStephen Cameron 	cmd = cp->scsi_cmd;
2058edd16368SStephen M. Cameron 	h = cp->h;
2059283b4a9bSStephen M. Cameron 	dev = cmd->device->hostdata;
2060d9a729f3SWebb Scales 	c2 = &h->ioaccel2_cmd_pool[cp->cmdindex];
2061edd16368SStephen M. Cameron 
2062edd16368SStephen M. Cameron 	scsi_dma_unmap(cmd); /* undo the DMA mappings */
2063e1f7de0cSMatt Gates 	if ((cp->cmd_type == CMD_SCSI) &&
20642b08b3e9SDon Brace 		(le16_to_cpu(cp->Header.SGTotal) > h->max_cmd_sg_entries))
206533a2ffceSStephen M. Cameron 		hpsa_unmap_sg_chain_block(h, cp);
2066edd16368SStephen M. Cameron 
2067d9a729f3SWebb Scales 	if ((cp->cmd_type == CMD_IOACCEL2) &&
2068d9a729f3SWebb Scales 		(c2->sg[0].chain_indicator == IOACCEL2_CHAIN))
2069d9a729f3SWebb Scales 		hpsa_unmap_ioaccel2_sg_chain_block(h, c2);
2070d9a729f3SWebb Scales 
2071edd16368SStephen M. Cameron 	cmd->result = (DID_OK << 16); 		/* host byte */
2072edd16368SStephen M. Cameron 	cmd->result |= (COMMAND_COMPLETE << 8);	/* msg byte */
2073c349775eSScott Teel 
207403383736SDon Brace 	if (cp->cmd_type == CMD_IOACCEL2 || cp->cmd_type == CMD_IOACCEL1)
207503383736SDon Brace 		atomic_dec(&cp->phys_disk->ioaccel_cmds_out);
207603383736SDon Brace 
207725163bd5SWebb Scales 	/*
207825163bd5SWebb Scales 	 * We check for lockup status here as it may be set for
207925163bd5SWebb Scales 	 * CMD_SCSI, CMD_IOACCEL1 and CMD_IOACCEL2 commands by
208025163bd5SWebb Scales 	 * fail_all_oustanding_cmds()
208125163bd5SWebb Scales 	 */
208225163bd5SWebb Scales 	if (unlikely(ei->CommandStatus == CMD_CTLR_LOCKUP)) {
208325163bd5SWebb Scales 		/* DID_NO_CONNECT will prevent a retry */
208425163bd5SWebb Scales 		cmd->result = DID_NO_CONNECT << 16;
208525163bd5SWebb Scales 		cmd_free(h, cp);
208625163bd5SWebb Scales 		cmd->scsi_done(cmd);
208725163bd5SWebb Scales 		return;
208825163bd5SWebb Scales 	}
208925163bd5SWebb Scales 
2090c349775eSScott Teel 	if (cp->cmd_type == CMD_IOACCEL2)
2091c349775eSScott Teel 		return process_ioaccel2_completion(h, cp, cmd, dev);
2092c349775eSScott Teel 
20936aa4c361SRobert Elliott 	scsi_set_resid(cmd, ei->ResidualCnt);
20946aa4c361SRobert Elliott 	if (ei->CommandStatus == 0) {
209503383736SDon Brace 		if (cp->cmd_type == CMD_IOACCEL1)
209603383736SDon Brace 			atomic_dec(&cp->phys_disk->ioaccel_cmds_out);
20976aa4c361SRobert Elliott 		cmd_free(h, cp);
20986aa4c361SRobert Elliott 		cmd->scsi_done(cmd);
20996aa4c361SRobert Elliott 		return;
21006aa4c361SRobert Elliott 	}
21016aa4c361SRobert Elliott 
2102e1f7de0cSMatt Gates 	/* For I/O accelerator commands, copy over some fields to the normal
2103e1f7de0cSMatt Gates 	 * CISS header used below for error handling.
2104e1f7de0cSMatt Gates 	 */
2105e1f7de0cSMatt Gates 	if (cp->cmd_type == CMD_IOACCEL1) {
2106e1f7de0cSMatt Gates 		struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
21072b08b3e9SDon Brace 		cp->Header.SGList = scsi_sg_count(cmd);
21082b08b3e9SDon Brace 		cp->Header.SGTotal = cpu_to_le16(cp->Header.SGList);
21092b08b3e9SDon Brace 		cp->Request.CDBLen = le16_to_cpu(c->io_flags) &
21102b08b3e9SDon Brace 			IOACCEL1_IOFLAGS_CDBLEN_MASK;
211150a0decfSStephen M. Cameron 		cp->Header.tag = c->tag;
2112e1f7de0cSMatt Gates 		memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
2113e1f7de0cSMatt Gates 		memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
2114283b4a9bSStephen M. Cameron 
2115283b4a9bSStephen M. Cameron 		/* Any RAID offload error results in retry which will use
2116283b4a9bSStephen M. Cameron 		 * the normal I/O path so the controller can handle whatever's
2117283b4a9bSStephen M. Cameron 		 * wrong.
2118283b4a9bSStephen M. Cameron 		 */
2119283b4a9bSStephen M. Cameron 		if (is_logical_dev_addr_mode(dev->scsi3addr)) {
2120283b4a9bSStephen M. Cameron 			if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
2121283b4a9bSStephen M. Cameron 				dev->offload_enabled = 0;
2122080ef1ccSDon Brace 			INIT_WORK(&cp->work, hpsa_command_resubmit_worker);
2123080ef1ccSDon Brace 			queue_work_on(raw_smp_processor_id(),
2124080ef1ccSDon Brace 					h->resubmit_wq, &cp->work);
2125283b4a9bSStephen M. Cameron 			return;
2126283b4a9bSStephen M. Cameron 		}
2127e1f7de0cSMatt Gates 	}
2128e1f7de0cSMatt Gates 
2129edd16368SStephen M. Cameron 	/* an error has occurred */
2130edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
2131edd16368SStephen M. Cameron 
2132edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
21339437ac43SStephen Cameron 		cmd->result |= ei->ScsiStatus;
21349437ac43SStephen Cameron 		/* copy the sense data */
21359437ac43SStephen Cameron 		if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
21369437ac43SStephen Cameron 			sense_data_size = SCSI_SENSE_BUFFERSIZE;
21379437ac43SStephen Cameron 		else
21389437ac43SStephen Cameron 			sense_data_size = sizeof(ei->SenseInfo);
21399437ac43SStephen Cameron 		if (ei->SenseLen < sense_data_size)
21409437ac43SStephen Cameron 			sense_data_size = ei->SenseLen;
21419437ac43SStephen Cameron 		memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
21429437ac43SStephen Cameron 		if (ei->ScsiStatus)
21439437ac43SStephen Cameron 			decode_sense_data(ei->SenseInfo, sense_data_size,
21449437ac43SStephen Cameron 				&sense_key, &asc, &ascq);
2145edd16368SStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
21461d3b3609SMatt Gates 			if (sense_key == ABORTED_COMMAND) {
21472e311fbaSStephen M. Cameron 				cmd->result |= DID_SOFT_ERROR << 16;
21481d3b3609SMatt Gates 				break;
21491d3b3609SMatt Gates 			}
2150edd16368SStephen M. Cameron 			break;
2151edd16368SStephen M. Cameron 		}
2152edd16368SStephen M. Cameron 		/* Problem was not a check condition
2153edd16368SStephen M. Cameron 		 * Pass it up to the upper layers...
2154edd16368SStephen M. Cameron 		 */
2155edd16368SStephen M. Cameron 		if (ei->ScsiStatus) {
2156edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
2157edd16368SStephen M. Cameron 				"Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
2158edd16368SStephen M. Cameron 				"Returning result: 0x%x\n",
2159edd16368SStephen M. Cameron 				cp, ei->ScsiStatus,
2160edd16368SStephen M. Cameron 				sense_key, asc, ascq,
2161edd16368SStephen M. Cameron 				cmd->result);
2162edd16368SStephen M. Cameron 		} else {  /* scsi status is zero??? How??? */
2163edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
2164edd16368SStephen M. Cameron 				"Returning no connection.\n", cp),
2165edd16368SStephen M. Cameron 
2166edd16368SStephen M. Cameron 			/* Ordinarily, this case should never happen,
2167edd16368SStephen M. Cameron 			 * but there is a bug in some released firmware
2168edd16368SStephen M. Cameron 			 * revisions that allows it to happen if, for
2169edd16368SStephen M. Cameron 			 * example, a 4100 backplane loses power and
2170edd16368SStephen M. Cameron 			 * the tape drive is in it.  We assume that
2171edd16368SStephen M. Cameron 			 * it's a fatal error of some kind because we
2172edd16368SStephen M. Cameron 			 * can't show that it wasn't. We will make it
2173edd16368SStephen M. Cameron 			 * look like selection timeout since that is
2174edd16368SStephen M. Cameron 			 * the most common reason for this to occur,
2175edd16368SStephen M. Cameron 			 * and it's severe enough.
2176edd16368SStephen M. Cameron 			 */
2177edd16368SStephen M. Cameron 
2178edd16368SStephen M. Cameron 			cmd->result = DID_NO_CONNECT << 16;
2179edd16368SStephen M. Cameron 		}
2180edd16368SStephen M. Cameron 		break;
2181edd16368SStephen M. Cameron 
2182edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
2183edd16368SStephen M. Cameron 		break;
2184edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
2185f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev,
2186f42e81e1SStephen Cameron 			"CDB %16phN data overrun\n", cp->Request.CDB);
2187edd16368SStephen M. Cameron 		break;
2188edd16368SStephen M. Cameron 	case CMD_INVALID: {
2189edd16368SStephen M. Cameron 		/* print_bytes(cp, sizeof(*cp), 1, 0);
2190edd16368SStephen M. Cameron 		print_cmd(cp); */
2191edd16368SStephen M. Cameron 		/* We get CMD_INVALID if you address a non-existent device
2192edd16368SStephen M. Cameron 		 * instead of a selection timeout (no response).  You will
2193edd16368SStephen M. Cameron 		 * see this if you yank out a drive, then try to access it.
2194edd16368SStephen M. Cameron 		 * This is kind of a shame because it means that any other
2195edd16368SStephen M. Cameron 		 * CMD_INVALID (e.g. driver bug) will get interpreted as a
2196edd16368SStephen M. Cameron 		 * missing target. */
2197edd16368SStephen M. Cameron 		cmd->result = DID_NO_CONNECT << 16;
2198edd16368SStephen M. Cameron 	}
2199edd16368SStephen M. Cameron 		break;
2200edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
2201256d0eaaSStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2202f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : protocol error\n",
2203f42e81e1SStephen Cameron 				cp->Request.CDB);
2204edd16368SStephen M. Cameron 		break;
2205edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
2206edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2207f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : hardware error\n",
2208f42e81e1SStephen Cameron 			cp->Request.CDB);
2209edd16368SStephen M. Cameron 		break;
2210edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
2211edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2212f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : connection lost\n",
2213f42e81e1SStephen Cameron 			cp->Request.CDB);
2214edd16368SStephen M. Cameron 		break;
2215edd16368SStephen M. Cameron 	case CMD_ABORTED:
2216edd16368SStephen M. Cameron 		cmd->result = DID_ABORT << 16;
2217f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN was aborted with status 0x%x\n",
2218f42e81e1SStephen Cameron 				cp->Request.CDB, ei->ScsiStatus);
2219edd16368SStephen M. Cameron 		break;
2220edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
2221edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2222f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : abort failed\n",
2223f42e81e1SStephen Cameron 			cp->Request.CDB);
2224edd16368SStephen M. Cameron 		break;
2225edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
2226f6e76055SStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
2227f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : unsolicited abort\n",
2228f42e81e1SStephen Cameron 			cp->Request.CDB);
2229edd16368SStephen M. Cameron 		break;
2230edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
2231edd16368SStephen M. Cameron 		cmd->result = DID_TIME_OUT << 16;
2232f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN timed out\n",
2233f42e81e1SStephen Cameron 			cp->Request.CDB);
2234edd16368SStephen M. Cameron 		break;
22351d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
22361d5e2ed0SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
22371d5e2ed0SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Command unabortable\n");
22381d5e2ed0SStephen M. Cameron 		break;
22399437ac43SStephen Cameron 	case CMD_TMF_STATUS:
22409437ac43SStephen Cameron 		if (hpsa_evaluate_tmf_status(h, cp)) /* TMF failed? */
22419437ac43SStephen Cameron 			cmd->result = DID_ERROR << 16;
22429437ac43SStephen Cameron 		break;
2243283b4a9bSStephen M. Cameron 	case CMD_IOACCEL_DISABLED:
2244283b4a9bSStephen M. Cameron 		/* This only handles the direct pass-through case since RAID
2245283b4a9bSStephen M. Cameron 		 * offload is handled above.  Just attempt a retry.
2246283b4a9bSStephen M. Cameron 		 */
2247283b4a9bSStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16;
2248283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev,
2249283b4a9bSStephen M. Cameron 				"cp %p had HP SSD Smart Path error\n", cp);
2250283b4a9bSStephen M. Cameron 		break;
2251edd16368SStephen M. Cameron 	default:
2252edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2253edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
2254edd16368SStephen M. Cameron 				cp, ei->CommandStatus);
2255edd16368SStephen M. Cameron 	}
2256edd16368SStephen M. Cameron 	cmd_free(h, cp);
22572cc5bfafSTomas Henzl 	cmd->scsi_done(cmd);
2258edd16368SStephen M. Cameron }
2259edd16368SStephen M. Cameron 
2260edd16368SStephen M. Cameron static void hpsa_pci_unmap(struct pci_dev *pdev,
2261edd16368SStephen M. Cameron 	struct CommandList *c, int sg_used, int data_direction)
2262edd16368SStephen M. Cameron {
2263edd16368SStephen M. Cameron 	int i;
2264edd16368SStephen M. Cameron 
226550a0decfSStephen M. Cameron 	for (i = 0; i < sg_used; i++)
226650a0decfSStephen M. Cameron 		pci_unmap_single(pdev, (dma_addr_t) le64_to_cpu(c->SG[i].Addr),
226750a0decfSStephen M. Cameron 				le32_to_cpu(c->SG[i].Len),
2268edd16368SStephen M. Cameron 				data_direction);
2269edd16368SStephen M. Cameron }
2270edd16368SStephen M. Cameron 
2271a2dac136SStephen M. Cameron static int hpsa_map_one(struct pci_dev *pdev,
2272edd16368SStephen M. Cameron 		struct CommandList *cp,
2273edd16368SStephen M. Cameron 		unsigned char *buf,
2274edd16368SStephen M. Cameron 		size_t buflen,
2275edd16368SStephen M. Cameron 		int data_direction)
2276edd16368SStephen M. Cameron {
227701a02ffcSStephen M. Cameron 	u64 addr64;
2278edd16368SStephen M. Cameron 
2279edd16368SStephen M. Cameron 	if (buflen == 0 || data_direction == PCI_DMA_NONE) {
2280edd16368SStephen M. Cameron 		cp->Header.SGList = 0;
228150a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(0);
2282a2dac136SStephen M. Cameron 		return 0;
2283edd16368SStephen M. Cameron 	}
2284edd16368SStephen M. Cameron 
228550a0decfSStephen M. Cameron 	addr64 = pci_map_single(pdev, buf, buflen, data_direction);
2286eceaae18SShuah Khan 	if (dma_mapping_error(&pdev->dev, addr64)) {
2287a2dac136SStephen M. Cameron 		/* Prevent subsequent unmap of something never mapped */
2288eceaae18SShuah Khan 		cp->Header.SGList = 0;
228950a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(0);
2290a2dac136SStephen M. Cameron 		return -1;
2291eceaae18SShuah Khan 	}
229250a0decfSStephen M. Cameron 	cp->SG[0].Addr = cpu_to_le64(addr64);
229350a0decfSStephen M. Cameron 	cp->SG[0].Len = cpu_to_le32(buflen);
229450a0decfSStephen M. Cameron 	cp->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* we are not chaining */
229550a0decfSStephen M. Cameron 	cp->Header.SGList = 1;   /* no. SGs contig in this cmd */
229650a0decfSStephen M. Cameron 	cp->Header.SGTotal = cpu_to_le16(1); /* total sgs in cmd list */
2297a2dac136SStephen M. Cameron 	return 0;
2298edd16368SStephen M. Cameron }
2299edd16368SStephen M. Cameron 
230025163bd5SWebb Scales #define NO_TIMEOUT ((unsigned long) -1)
230125163bd5SWebb Scales #define DEFAULT_TIMEOUT 30000 /* milliseconds */
230225163bd5SWebb Scales static int hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
230325163bd5SWebb Scales 	struct CommandList *c, int reply_queue, unsigned long timeout_msecs)
2304edd16368SStephen M. Cameron {
2305edd16368SStephen M. Cameron 	DECLARE_COMPLETION_ONSTACK(wait);
2306edd16368SStephen M. Cameron 
2307edd16368SStephen M. Cameron 	c->waiting = &wait;
230825163bd5SWebb Scales 	__enqueue_cmd_and_start_io(h, c, reply_queue);
230925163bd5SWebb Scales 	if (timeout_msecs == NO_TIMEOUT) {
231025163bd5SWebb Scales 		/* TODO: get rid of this no-timeout thing */
231125163bd5SWebb Scales 		wait_for_completion_io(&wait);
231225163bd5SWebb Scales 		return IO_OK;
231325163bd5SWebb Scales 	}
231425163bd5SWebb Scales 	if (!wait_for_completion_io_timeout(&wait,
231525163bd5SWebb Scales 					msecs_to_jiffies(timeout_msecs))) {
231625163bd5SWebb Scales 		dev_warn(&h->pdev->dev, "Command timed out.\n");
231725163bd5SWebb Scales 		return -ETIMEDOUT;
231825163bd5SWebb Scales 	}
231925163bd5SWebb Scales 	return IO_OK;
232025163bd5SWebb Scales }
232125163bd5SWebb Scales 
232225163bd5SWebb Scales static int hpsa_scsi_do_simple_cmd(struct ctlr_info *h, struct CommandList *c,
232325163bd5SWebb Scales 				   int reply_queue, unsigned long timeout_msecs)
232425163bd5SWebb Scales {
232525163bd5SWebb Scales 	if (unlikely(lockup_detected(h))) {
232625163bd5SWebb Scales 		c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
232725163bd5SWebb Scales 		return IO_OK;
232825163bd5SWebb Scales 	}
232925163bd5SWebb Scales 	return hpsa_scsi_do_simple_cmd_core(h, c, reply_queue, timeout_msecs);
2330edd16368SStephen M. Cameron }
2331edd16368SStephen M. Cameron 
2332094963daSStephen M. Cameron static u32 lockup_detected(struct ctlr_info *h)
2333094963daSStephen M. Cameron {
2334094963daSStephen M. Cameron 	int cpu;
2335094963daSStephen M. Cameron 	u32 rc, *lockup_detected;
2336094963daSStephen M. Cameron 
2337094963daSStephen M. Cameron 	cpu = get_cpu();
2338094963daSStephen M. Cameron 	lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
2339094963daSStephen M. Cameron 	rc = *lockup_detected;
2340094963daSStephen M. Cameron 	put_cpu();
2341094963daSStephen M. Cameron 	return rc;
2342094963daSStephen M. Cameron }
2343094963daSStephen M. Cameron 
23449c2fc160SStephen M. Cameron #define MAX_DRIVER_CMD_RETRIES 25
234525163bd5SWebb Scales static int hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
234625163bd5SWebb Scales 	struct CommandList *c, int data_direction, unsigned long timeout_msecs)
2347edd16368SStephen M. Cameron {
23489c2fc160SStephen M. Cameron 	int backoff_time = 10, retry_count = 0;
234925163bd5SWebb Scales 	int rc;
2350edd16368SStephen M. Cameron 
2351edd16368SStephen M. Cameron 	do {
23527630abd0SJoe Perches 		memset(c->err_info, 0, sizeof(*c->err_info));
235325163bd5SWebb Scales 		rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
235425163bd5SWebb Scales 						  timeout_msecs);
235525163bd5SWebb Scales 		if (rc)
235625163bd5SWebb Scales 			break;
2357edd16368SStephen M. Cameron 		retry_count++;
23589c2fc160SStephen M. Cameron 		if (retry_count > 3) {
23599c2fc160SStephen M. Cameron 			msleep(backoff_time);
23609c2fc160SStephen M. Cameron 			if (backoff_time < 1000)
23619c2fc160SStephen M. Cameron 				backoff_time *= 2;
23629c2fc160SStephen M. Cameron 		}
2363852af20aSMatt Bondurant 	} while ((check_for_unit_attention(h, c) ||
23649c2fc160SStephen M. Cameron 			check_for_busy(h, c)) &&
23659c2fc160SStephen M. Cameron 			retry_count <= MAX_DRIVER_CMD_RETRIES);
2366edd16368SStephen M. Cameron 	hpsa_pci_unmap(h->pdev, c, 1, data_direction);
236725163bd5SWebb Scales 	if (retry_count > MAX_DRIVER_CMD_RETRIES)
236825163bd5SWebb Scales 		rc = -EIO;
236925163bd5SWebb Scales 	return rc;
2370edd16368SStephen M. Cameron }
2371edd16368SStephen M. Cameron 
2372d1e8beacSStephen M. Cameron static void hpsa_print_cmd(struct ctlr_info *h, char *txt,
2373d1e8beacSStephen M. Cameron 				struct CommandList *c)
2374edd16368SStephen M. Cameron {
2375d1e8beacSStephen M. Cameron 	const u8 *cdb = c->Request.CDB;
2376d1e8beacSStephen M. Cameron 	const u8 *lun = c->Header.LUN.LunAddrBytes;
2377edd16368SStephen M. Cameron 
2378d1e8beacSStephen M. Cameron 	dev_warn(&h->pdev->dev, "%s: LUN:%02x%02x%02x%02x%02x%02x%02x%02x"
2379d1e8beacSStephen M. Cameron 	" CDB:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
2380d1e8beacSStephen M. Cameron 		txt, lun[0], lun[1], lun[2], lun[3],
2381d1e8beacSStephen M. Cameron 		lun[4], lun[5], lun[6], lun[7],
2382d1e8beacSStephen M. Cameron 		cdb[0], cdb[1], cdb[2], cdb[3],
2383d1e8beacSStephen M. Cameron 		cdb[4], cdb[5], cdb[6], cdb[7],
2384d1e8beacSStephen M. Cameron 		cdb[8], cdb[9], cdb[10], cdb[11],
2385d1e8beacSStephen M. Cameron 		cdb[12], cdb[13], cdb[14], cdb[15]);
2386d1e8beacSStephen M. Cameron }
2387d1e8beacSStephen M. Cameron 
2388d1e8beacSStephen M. Cameron static void hpsa_scsi_interpret_error(struct ctlr_info *h,
2389d1e8beacSStephen M. Cameron 			struct CommandList *cp)
2390d1e8beacSStephen M. Cameron {
2391d1e8beacSStephen M. Cameron 	const struct ErrorInfo *ei = cp->err_info;
2392d1e8beacSStephen M. Cameron 	struct device *d = &cp->h->pdev->dev;
23939437ac43SStephen Cameron 	u8 sense_key, asc, ascq;
23949437ac43SStephen Cameron 	int sense_len;
2395d1e8beacSStephen M. Cameron 
2396edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
2397edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
23989437ac43SStephen Cameron 		if (ei->SenseLen > sizeof(ei->SenseInfo))
23999437ac43SStephen Cameron 			sense_len = sizeof(ei->SenseInfo);
24009437ac43SStephen Cameron 		else
24019437ac43SStephen Cameron 			sense_len = ei->SenseLen;
24029437ac43SStephen Cameron 		decode_sense_data(ei->SenseInfo, sense_len,
24039437ac43SStephen Cameron 					&sense_key, &asc, &ascq);
2404d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "SCSI status", cp);
2405d1e8beacSStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION)
24069437ac43SStephen Cameron 			dev_warn(d, "SCSI Status = 02, Sense key = 0x%02x, ASC = 0x%02x, ASCQ = 0x%02x\n",
24079437ac43SStephen Cameron 				sense_key, asc, ascq);
2408d1e8beacSStephen M. Cameron 		else
24099437ac43SStephen Cameron 			dev_warn(d, "SCSI Status = 0x%02x\n", ei->ScsiStatus);
2410edd16368SStephen M. Cameron 		if (ei->ScsiStatus == 0)
2411edd16368SStephen M. Cameron 			dev_warn(d, "SCSI status is abnormally zero.  "
2412edd16368SStephen M. Cameron 			"(probably indicates selection timeout "
2413edd16368SStephen M. Cameron 			"reported incorrectly due to a known "
2414edd16368SStephen M. Cameron 			"firmware bug, circa July, 2001.)\n");
2415edd16368SStephen M. Cameron 		break;
2416edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
2417edd16368SStephen M. Cameron 		break;
2418edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
2419d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "overrun condition", cp);
2420edd16368SStephen M. Cameron 		break;
2421edd16368SStephen M. Cameron 	case CMD_INVALID: {
2422edd16368SStephen M. Cameron 		/* controller unfortunately reports SCSI passthru's
2423edd16368SStephen M. Cameron 		 * to non-existent targets as invalid commands.
2424edd16368SStephen M. Cameron 		 */
2425d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "invalid command", cp);
2426d1e8beacSStephen M. Cameron 		dev_warn(d, "probably means device no longer present\n");
2427edd16368SStephen M. Cameron 		}
2428edd16368SStephen M. Cameron 		break;
2429edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
2430d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "protocol error", cp);
2431edd16368SStephen M. Cameron 		break;
2432edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
2433d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "hardware error", cp);
2434edd16368SStephen M. Cameron 		break;
2435edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
2436d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "connection lost", cp);
2437edd16368SStephen M. Cameron 		break;
2438edd16368SStephen M. Cameron 	case CMD_ABORTED:
2439d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "aborted", cp);
2440edd16368SStephen M. Cameron 		break;
2441edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
2442d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "abort failed", cp);
2443edd16368SStephen M. Cameron 		break;
2444edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
2445d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unsolicited abort", cp);
2446edd16368SStephen M. Cameron 		break;
2447edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
2448d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "timed out", cp);
2449edd16368SStephen M. Cameron 		break;
24501d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
2451d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unabortable", cp);
24521d5e2ed0SStephen M. Cameron 		break;
245325163bd5SWebb Scales 	case CMD_CTLR_LOCKUP:
245425163bd5SWebb Scales 		hpsa_print_cmd(h, "controller lockup detected", cp);
245525163bd5SWebb Scales 		break;
2456edd16368SStephen M. Cameron 	default:
2457d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unknown status", cp);
2458d1e8beacSStephen M. Cameron 		dev_warn(d, "Unknown command status %x\n",
2459edd16368SStephen M. Cameron 				ei->CommandStatus);
2460edd16368SStephen M. Cameron 	}
2461edd16368SStephen M. Cameron }
2462edd16368SStephen M. Cameron 
2463edd16368SStephen M. Cameron static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
2464b7bb24ebSStephen M. Cameron 			u16 page, unsigned char *buf,
2465edd16368SStephen M. Cameron 			unsigned char bufsize)
2466edd16368SStephen M. Cameron {
2467edd16368SStephen M. Cameron 	int rc = IO_OK;
2468edd16368SStephen M. Cameron 	struct CommandList *c;
2469edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2470edd16368SStephen M. Cameron 
247145fcb86eSStephen Cameron 	c = cmd_alloc(h);
2472edd16368SStephen M. Cameron 
2473a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
2474a2dac136SStephen M. Cameron 			page, scsi3addr, TYPE_CMD)) {
2475a2dac136SStephen M. Cameron 		rc = -1;
2476a2dac136SStephen M. Cameron 		goto out;
2477a2dac136SStephen M. Cameron 	}
247825163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
247925163bd5SWebb Scales 					PCI_DMA_FROMDEVICE, NO_TIMEOUT);
248025163bd5SWebb Scales 	if (rc)
248125163bd5SWebb Scales 		goto out;
2482edd16368SStephen M. Cameron 	ei = c->err_info;
2483edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2484d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2485edd16368SStephen M. Cameron 		rc = -1;
2486edd16368SStephen M. Cameron 	}
2487a2dac136SStephen M. Cameron out:
248845fcb86eSStephen Cameron 	cmd_free(h, c);
2489edd16368SStephen M. Cameron 	return rc;
2490edd16368SStephen M. Cameron }
2491edd16368SStephen M. Cameron 
2492316b221aSStephen M. Cameron static int hpsa_bmic_ctrl_mode_sense(struct ctlr_info *h,
2493316b221aSStephen M. Cameron 		unsigned char *scsi3addr, unsigned char page,
2494316b221aSStephen M. Cameron 		struct bmic_controller_parameters *buf, size_t bufsize)
2495316b221aSStephen M. Cameron {
2496316b221aSStephen M. Cameron 	int rc = IO_OK;
2497316b221aSStephen M. Cameron 	struct CommandList *c;
2498316b221aSStephen M. Cameron 	struct ErrorInfo *ei;
2499316b221aSStephen M. Cameron 
250045fcb86eSStephen Cameron 	c = cmd_alloc(h);
2501316b221aSStephen M. Cameron 	if (fill_cmd(c, BMIC_SENSE_CONTROLLER_PARAMETERS, h, buf, bufsize,
2502316b221aSStephen M. Cameron 			page, scsi3addr, TYPE_CMD)) {
2503316b221aSStephen M. Cameron 		rc = -1;
2504316b221aSStephen M. Cameron 		goto out;
2505316b221aSStephen M. Cameron 	}
250625163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
250725163bd5SWebb Scales 			PCI_DMA_FROMDEVICE, NO_TIMEOUT);
250825163bd5SWebb Scales 	if (rc)
250925163bd5SWebb Scales 		goto out;
2510316b221aSStephen M. Cameron 	ei = c->err_info;
2511316b221aSStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2512316b221aSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2513316b221aSStephen M. Cameron 		rc = -1;
2514316b221aSStephen M. Cameron 	}
2515316b221aSStephen M. Cameron out:
251645fcb86eSStephen Cameron 	cmd_free(h, c);
2517316b221aSStephen M. Cameron 	return rc;
2518316b221aSStephen M. Cameron }
2519316b221aSStephen M. Cameron 
2520bf711ac6SScott Teel static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
252125163bd5SWebb Scales 	u8 reset_type, int reply_queue)
2522edd16368SStephen M. Cameron {
2523edd16368SStephen M. Cameron 	int rc = IO_OK;
2524edd16368SStephen M. Cameron 	struct CommandList *c;
2525edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2526edd16368SStephen M. Cameron 
252745fcb86eSStephen Cameron 	c = cmd_alloc(h);
2528edd16368SStephen M. Cameron 
2529edd16368SStephen M. Cameron 
2530a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map. */
2531bf711ac6SScott Teel 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
2532bf711ac6SScott Teel 			scsi3addr, TYPE_MSG);
2533bf711ac6SScott Teel 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to LUN reset */
253425163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
253525163bd5SWebb Scales 	if (rc) {
253625163bd5SWebb Scales 		dev_warn(&h->pdev->dev, "Failed to send reset command\n");
253725163bd5SWebb Scales 		goto out;
253825163bd5SWebb Scales 	}
2539edd16368SStephen M. Cameron 	/* no unmap needed here because no data xfer. */
2540edd16368SStephen M. Cameron 
2541edd16368SStephen M. Cameron 	ei = c->err_info;
2542edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0) {
2543d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2544edd16368SStephen M. Cameron 		rc = -1;
2545edd16368SStephen M. Cameron 	}
254625163bd5SWebb Scales out:
254745fcb86eSStephen Cameron 	cmd_free(h, c);
2548edd16368SStephen M. Cameron 	return rc;
2549edd16368SStephen M. Cameron }
2550edd16368SStephen M. Cameron 
2551edd16368SStephen M. Cameron static void hpsa_get_raid_level(struct ctlr_info *h,
2552edd16368SStephen M. Cameron 	unsigned char *scsi3addr, unsigned char *raid_level)
2553edd16368SStephen M. Cameron {
2554edd16368SStephen M. Cameron 	int rc;
2555edd16368SStephen M. Cameron 	unsigned char *buf;
2556edd16368SStephen M. Cameron 
2557edd16368SStephen M. Cameron 	*raid_level = RAID_UNKNOWN;
2558edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2559edd16368SStephen M. Cameron 	if (!buf)
2560edd16368SStephen M. Cameron 		return;
2561b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0xC1, buf, 64);
2562edd16368SStephen M. Cameron 	if (rc == 0)
2563edd16368SStephen M. Cameron 		*raid_level = buf[8];
2564edd16368SStephen M. Cameron 	if (*raid_level > RAID_UNKNOWN)
2565edd16368SStephen M. Cameron 		*raid_level = RAID_UNKNOWN;
2566edd16368SStephen M. Cameron 	kfree(buf);
2567edd16368SStephen M. Cameron 	return;
2568edd16368SStephen M. Cameron }
2569edd16368SStephen M. Cameron 
2570283b4a9bSStephen M. Cameron #define HPSA_MAP_DEBUG
2571283b4a9bSStephen M. Cameron #ifdef HPSA_MAP_DEBUG
2572283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
2573283b4a9bSStephen M. Cameron 				struct raid_map_data *map_buff)
2574283b4a9bSStephen M. Cameron {
2575283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map_buff->data[0];
2576283b4a9bSStephen M. Cameron 	int map, row, col;
2577283b4a9bSStephen M. Cameron 	u16 map_cnt, row_cnt, disks_per_row;
2578283b4a9bSStephen M. Cameron 
2579283b4a9bSStephen M. Cameron 	if (rc != 0)
2580283b4a9bSStephen M. Cameron 		return;
2581283b4a9bSStephen M. Cameron 
25822ba8bfc8SStephen M. Cameron 	/* Show details only if debugging has been activated. */
25832ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug < 2)
25842ba8bfc8SStephen M. Cameron 		return;
25852ba8bfc8SStephen M. Cameron 
2586283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "structure_size = %u\n",
2587283b4a9bSStephen M. Cameron 				le32_to_cpu(map_buff->structure_size));
2588283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
2589283b4a9bSStephen M. Cameron 			le32_to_cpu(map_buff->volume_blk_size));
2590283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
2591283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->volume_blk_cnt));
2592283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
2593283b4a9bSStephen M. Cameron 			map_buff->phys_blk_shift);
2594283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
2595283b4a9bSStephen M. Cameron 			map_buff->parity_rotation_shift);
2596283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "strip_size = %u\n",
2597283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->strip_size));
2598283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
2599283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_starting_blk));
2600283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
2601283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_blk_cnt));
2602283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
2603283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->data_disks_per_row));
2604283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
2605283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->metadata_disks_per_row));
2606283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "row_cnt = %u\n",
2607283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->row_cnt));
2608283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "layout_map_count = %u\n",
2609283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->layout_map_count));
26102b08b3e9SDon Brace 	dev_info(&h->pdev->dev, "flags = 0x%x\n",
2611dd0e19f3SScott Teel 			le16_to_cpu(map_buff->flags));
26122b08b3e9SDon Brace 	dev_info(&h->pdev->dev, "encrypytion = %s\n",
26132b08b3e9SDon Brace 			le16_to_cpu(map_buff->flags) &
26142b08b3e9SDon Brace 			RAID_MAP_FLAG_ENCRYPT_ON ?  "ON" : "OFF");
2615dd0e19f3SScott Teel 	dev_info(&h->pdev->dev, "dekindex = %u\n",
2616dd0e19f3SScott Teel 			le16_to_cpu(map_buff->dekindex));
2617283b4a9bSStephen M. Cameron 	map_cnt = le16_to_cpu(map_buff->layout_map_count);
2618283b4a9bSStephen M. Cameron 	for (map = 0; map < map_cnt; map++) {
2619283b4a9bSStephen M. Cameron 		dev_info(&h->pdev->dev, "Map%u:\n", map);
2620283b4a9bSStephen M. Cameron 		row_cnt = le16_to_cpu(map_buff->row_cnt);
2621283b4a9bSStephen M. Cameron 		for (row = 0; row < row_cnt; row++) {
2622283b4a9bSStephen M. Cameron 			dev_info(&h->pdev->dev, "  Row%u:\n", row);
2623283b4a9bSStephen M. Cameron 			disks_per_row =
2624283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->data_disks_per_row);
2625283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2626283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2627283b4a9bSStephen M. Cameron 					"    D%02u: h=0x%04x xor=%u,%u\n",
2628283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2629283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2630283b4a9bSStephen M. Cameron 			disks_per_row =
2631283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->metadata_disks_per_row);
2632283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2633283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2634283b4a9bSStephen M. Cameron 					"    M%02u: h=0x%04x xor=%u,%u\n",
2635283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2636283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2637283b4a9bSStephen M. Cameron 		}
2638283b4a9bSStephen M. Cameron 	}
2639283b4a9bSStephen M. Cameron }
2640283b4a9bSStephen M. Cameron #else
2641283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
2642283b4a9bSStephen M. Cameron 			__attribute__((unused)) int rc,
2643283b4a9bSStephen M. Cameron 			__attribute__((unused)) struct raid_map_data *map_buff)
2644283b4a9bSStephen M. Cameron {
2645283b4a9bSStephen M. Cameron }
2646283b4a9bSStephen M. Cameron #endif
2647283b4a9bSStephen M. Cameron 
2648283b4a9bSStephen M. Cameron static int hpsa_get_raid_map(struct ctlr_info *h,
2649283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2650283b4a9bSStephen M. Cameron {
2651283b4a9bSStephen M. Cameron 	int rc = 0;
2652283b4a9bSStephen M. Cameron 	struct CommandList *c;
2653283b4a9bSStephen M. Cameron 	struct ErrorInfo *ei;
2654283b4a9bSStephen M. Cameron 
265545fcb86eSStephen Cameron 	c = cmd_alloc(h);
2656bf43caf3SRobert Elliott 
2657283b4a9bSStephen M. Cameron 	if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
2658283b4a9bSStephen M. Cameron 			sizeof(this_device->raid_map), 0,
2659283b4a9bSStephen M. Cameron 			scsi3addr, TYPE_CMD)) {
26602dd02d74SRobert Elliott 		dev_warn(&h->pdev->dev, "hpsa_get_raid_map fill_cmd failed\n");
26612dd02d74SRobert Elliott 		cmd_free(h, c);
26622dd02d74SRobert Elliott 		return -1;
2663283b4a9bSStephen M. Cameron 	}
266425163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
266525163bd5SWebb Scales 					PCI_DMA_FROMDEVICE, NO_TIMEOUT);
266625163bd5SWebb Scales 	if (rc)
266725163bd5SWebb Scales 		goto out;
2668283b4a9bSStephen M. Cameron 	ei = c->err_info;
2669283b4a9bSStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2670d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
267125163bd5SWebb Scales 		rc = -1;
267225163bd5SWebb Scales 		goto out;
2673283b4a9bSStephen M. Cameron 	}
267445fcb86eSStephen Cameron 	cmd_free(h, c);
2675283b4a9bSStephen M. Cameron 
2676283b4a9bSStephen M. Cameron 	/* @todo in the future, dynamically allocate RAID map memory */
2677283b4a9bSStephen M. Cameron 	if (le32_to_cpu(this_device->raid_map.structure_size) >
2678283b4a9bSStephen M. Cameron 				sizeof(this_device->raid_map)) {
2679283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
2680283b4a9bSStephen M. Cameron 		rc = -1;
2681283b4a9bSStephen M. Cameron 	}
2682283b4a9bSStephen M. Cameron 	hpsa_debug_map_buff(h, rc, &this_device->raid_map);
2683283b4a9bSStephen M. Cameron 	return rc;
268425163bd5SWebb Scales out:
268525163bd5SWebb Scales 	cmd_free(h, c);
268625163bd5SWebb Scales 	return rc;
2687283b4a9bSStephen M. Cameron }
2688283b4a9bSStephen M. Cameron 
268903383736SDon Brace static int hpsa_bmic_id_physical_device(struct ctlr_info *h,
269003383736SDon Brace 		unsigned char scsi3addr[], u16 bmic_device_index,
269103383736SDon Brace 		struct bmic_identify_physical_device *buf, size_t bufsize)
269203383736SDon Brace {
269303383736SDon Brace 	int rc = IO_OK;
269403383736SDon Brace 	struct CommandList *c;
269503383736SDon Brace 	struct ErrorInfo *ei;
269603383736SDon Brace 
269703383736SDon Brace 	c = cmd_alloc(h);
269803383736SDon Brace 	rc = fill_cmd(c, BMIC_IDENTIFY_PHYSICAL_DEVICE, h, buf, bufsize,
269903383736SDon Brace 		0, RAID_CTLR_LUNID, TYPE_CMD);
270003383736SDon Brace 	if (rc)
270103383736SDon Brace 		goto out;
270203383736SDon Brace 
270303383736SDon Brace 	c->Request.CDB[2] = bmic_device_index & 0xff;
270403383736SDon Brace 	c->Request.CDB[9] = (bmic_device_index >> 8) & 0xff;
270503383736SDon Brace 
270625163bd5SWebb Scales 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE,
270725163bd5SWebb Scales 						NO_TIMEOUT);
270803383736SDon Brace 	ei = c->err_info;
270903383736SDon Brace 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
271003383736SDon Brace 		hpsa_scsi_interpret_error(h, c);
271103383736SDon Brace 		rc = -1;
271203383736SDon Brace 	}
271303383736SDon Brace out:
271403383736SDon Brace 	cmd_free(h, c);
271503383736SDon Brace 	return rc;
271603383736SDon Brace }
271703383736SDon Brace 
27181b70150aSStephen M. Cameron static int hpsa_vpd_page_supported(struct ctlr_info *h,
27191b70150aSStephen M. Cameron 	unsigned char scsi3addr[], u8 page)
27201b70150aSStephen M. Cameron {
27211b70150aSStephen M. Cameron 	int rc;
27221b70150aSStephen M. Cameron 	int i;
27231b70150aSStephen M. Cameron 	int pages;
27241b70150aSStephen M. Cameron 	unsigned char *buf, bufsize;
27251b70150aSStephen M. Cameron 
27261b70150aSStephen M. Cameron 	buf = kzalloc(256, GFP_KERNEL);
27271b70150aSStephen M. Cameron 	if (!buf)
27281b70150aSStephen M. Cameron 		return 0;
27291b70150aSStephen M. Cameron 
27301b70150aSStephen M. Cameron 	/* Get the size of the page list first */
27311b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
27321b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
27331b70150aSStephen M. Cameron 				buf, HPSA_VPD_HEADER_SZ);
27341b70150aSStephen M. Cameron 	if (rc != 0)
27351b70150aSStephen M. Cameron 		goto exit_unsupported;
27361b70150aSStephen M. Cameron 	pages = buf[3];
27371b70150aSStephen M. Cameron 	if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
27381b70150aSStephen M. Cameron 		bufsize = pages + HPSA_VPD_HEADER_SZ;
27391b70150aSStephen M. Cameron 	else
27401b70150aSStephen M. Cameron 		bufsize = 255;
27411b70150aSStephen M. Cameron 
27421b70150aSStephen M. Cameron 	/* Get the whole VPD page list */
27431b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
27441b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
27451b70150aSStephen M. Cameron 				buf, bufsize);
27461b70150aSStephen M. Cameron 	if (rc != 0)
27471b70150aSStephen M. Cameron 		goto exit_unsupported;
27481b70150aSStephen M. Cameron 
27491b70150aSStephen M. Cameron 	pages = buf[3];
27501b70150aSStephen M. Cameron 	for (i = 1; i <= pages; i++)
27511b70150aSStephen M. Cameron 		if (buf[3 + i] == page)
27521b70150aSStephen M. Cameron 			goto exit_supported;
27531b70150aSStephen M. Cameron exit_unsupported:
27541b70150aSStephen M. Cameron 	kfree(buf);
27551b70150aSStephen M. Cameron 	return 0;
27561b70150aSStephen M. Cameron exit_supported:
27571b70150aSStephen M. Cameron 	kfree(buf);
27581b70150aSStephen M. Cameron 	return 1;
27591b70150aSStephen M. Cameron }
27601b70150aSStephen M. Cameron 
2761283b4a9bSStephen M. Cameron static void hpsa_get_ioaccel_status(struct ctlr_info *h,
2762283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2763283b4a9bSStephen M. Cameron {
2764283b4a9bSStephen M. Cameron 	int rc;
2765283b4a9bSStephen M. Cameron 	unsigned char *buf;
2766283b4a9bSStephen M. Cameron 	u8 ioaccel_status;
2767283b4a9bSStephen M. Cameron 
2768283b4a9bSStephen M. Cameron 	this_device->offload_config = 0;
2769283b4a9bSStephen M. Cameron 	this_device->offload_enabled = 0;
277041ce4c35SStephen Cameron 	this_device->offload_to_be_enabled = 0;
2771283b4a9bSStephen M. Cameron 
2772283b4a9bSStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2773283b4a9bSStephen M. Cameron 	if (!buf)
2774283b4a9bSStephen M. Cameron 		return;
27751b70150aSStephen M. Cameron 	if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
27761b70150aSStephen M. Cameron 		goto out;
2777283b4a9bSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
2778b7bb24ebSStephen M. Cameron 			VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
2779283b4a9bSStephen M. Cameron 	if (rc != 0)
2780283b4a9bSStephen M. Cameron 		goto out;
2781283b4a9bSStephen M. Cameron 
2782283b4a9bSStephen M. Cameron #define IOACCEL_STATUS_BYTE 4
2783283b4a9bSStephen M. Cameron #define OFFLOAD_CONFIGURED_BIT 0x01
2784283b4a9bSStephen M. Cameron #define OFFLOAD_ENABLED_BIT 0x02
2785283b4a9bSStephen M. Cameron 	ioaccel_status = buf[IOACCEL_STATUS_BYTE];
2786283b4a9bSStephen M. Cameron 	this_device->offload_config =
2787283b4a9bSStephen M. Cameron 		!!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
2788283b4a9bSStephen M. Cameron 	if (this_device->offload_config) {
2789283b4a9bSStephen M. Cameron 		this_device->offload_enabled =
2790283b4a9bSStephen M. Cameron 			!!(ioaccel_status & OFFLOAD_ENABLED_BIT);
2791283b4a9bSStephen M. Cameron 		if (hpsa_get_raid_map(h, scsi3addr, this_device))
2792283b4a9bSStephen M. Cameron 			this_device->offload_enabled = 0;
2793283b4a9bSStephen M. Cameron 	}
279441ce4c35SStephen Cameron 	this_device->offload_to_be_enabled = this_device->offload_enabled;
2795283b4a9bSStephen M. Cameron out:
2796283b4a9bSStephen M. Cameron 	kfree(buf);
2797283b4a9bSStephen M. Cameron 	return;
2798283b4a9bSStephen M. Cameron }
2799283b4a9bSStephen M. Cameron 
2800edd16368SStephen M. Cameron /* Get the device id from inquiry page 0x83 */
2801edd16368SStephen M. Cameron static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
2802edd16368SStephen M. Cameron 	unsigned char *device_id, int buflen)
2803edd16368SStephen M. Cameron {
2804edd16368SStephen M. Cameron 	int rc;
2805edd16368SStephen M. Cameron 	unsigned char *buf;
2806edd16368SStephen M. Cameron 
2807edd16368SStephen M. Cameron 	if (buflen > 16)
2808edd16368SStephen M. Cameron 		buflen = 16;
2809edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2810edd16368SStephen M. Cameron 	if (!buf)
2811a84d794dSStephen M. Cameron 		return -ENOMEM;
2812b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0x83, buf, 64);
2813edd16368SStephen M. Cameron 	if (rc == 0)
2814edd16368SStephen M. Cameron 		memcpy(device_id, &buf[8], buflen);
2815edd16368SStephen M. Cameron 	kfree(buf);
2816edd16368SStephen M. Cameron 	return rc != 0;
2817edd16368SStephen M. Cameron }
2818edd16368SStephen M. Cameron 
2819edd16368SStephen M. Cameron static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
282003383736SDon Brace 		void *buf, int bufsize,
2821edd16368SStephen M. Cameron 		int extended_response)
2822edd16368SStephen M. Cameron {
2823edd16368SStephen M. Cameron 	int rc = IO_OK;
2824edd16368SStephen M. Cameron 	struct CommandList *c;
2825edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
2826edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2827edd16368SStephen M. Cameron 
282845fcb86eSStephen Cameron 	c = cmd_alloc(h);
2829bf43caf3SRobert Elliott 
2830e89c0ae7SStephen M. Cameron 	/* address the controller */
2831e89c0ae7SStephen M. Cameron 	memset(scsi3addr, 0, sizeof(scsi3addr));
2832a2dac136SStephen M. Cameron 	if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
2833a2dac136SStephen M. Cameron 		buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
2834a2dac136SStephen M. Cameron 		rc = -1;
2835a2dac136SStephen M. Cameron 		goto out;
2836a2dac136SStephen M. Cameron 	}
2837edd16368SStephen M. Cameron 	if (extended_response)
2838edd16368SStephen M. Cameron 		c->Request.CDB[1] = extended_response;
283925163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
284025163bd5SWebb Scales 					PCI_DMA_FROMDEVICE, NO_TIMEOUT);
284125163bd5SWebb Scales 	if (rc)
284225163bd5SWebb Scales 		goto out;
2843edd16368SStephen M. Cameron 	ei = c->err_info;
2844edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 &&
2845edd16368SStephen M. Cameron 	    ei->CommandStatus != CMD_DATA_UNDERRUN) {
2846d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2847edd16368SStephen M. Cameron 		rc = -1;
2848283b4a9bSStephen M. Cameron 	} else {
284903383736SDon Brace 		struct ReportLUNdata *rld = buf;
285003383736SDon Brace 
285103383736SDon Brace 		if (rld->extended_response_flag != extended_response) {
2852283b4a9bSStephen M. Cameron 			dev_err(&h->pdev->dev,
2853283b4a9bSStephen M. Cameron 				"report luns requested format %u, got %u\n",
2854283b4a9bSStephen M. Cameron 				extended_response,
285503383736SDon Brace 				rld->extended_response_flag);
2856283b4a9bSStephen M. Cameron 			rc = -1;
2857283b4a9bSStephen M. Cameron 		}
2858edd16368SStephen M. Cameron 	}
2859a2dac136SStephen M. Cameron out:
286045fcb86eSStephen Cameron 	cmd_free(h, c);
2861edd16368SStephen M. Cameron 	return rc;
2862edd16368SStephen M. Cameron }
2863edd16368SStephen M. Cameron 
2864edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
286503383736SDon Brace 		struct ReportExtendedLUNdata *buf, int bufsize)
2866edd16368SStephen M. Cameron {
286703383736SDon Brace 	return hpsa_scsi_do_report_luns(h, 0, buf, bufsize,
286803383736SDon Brace 						HPSA_REPORT_PHYS_EXTENDED);
2869edd16368SStephen M. Cameron }
2870edd16368SStephen M. Cameron 
2871edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
2872edd16368SStephen M. Cameron 		struct ReportLUNdata *buf, int bufsize)
2873edd16368SStephen M. Cameron {
2874edd16368SStephen M. Cameron 	return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
2875edd16368SStephen M. Cameron }
2876edd16368SStephen M. Cameron 
2877edd16368SStephen M. Cameron static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
2878edd16368SStephen M. Cameron 	int bus, int target, int lun)
2879edd16368SStephen M. Cameron {
2880edd16368SStephen M. Cameron 	device->bus = bus;
2881edd16368SStephen M. Cameron 	device->target = target;
2882edd16368SStephen M. Cameron 	device->lun = lun;
2883edd16368SStephen M. Cameron }
2884edd16368SStephen M. Cameron 
28859846590eSStephen M. Cameron /* Use VPD inquiry to get details of volume status */
28869846590eSStephen M. Cameron static int hpsa_get_volume_status(struct ctlr_info *h,
28879846590eSStephen M. Cameron 					unsigned char scsi3addr[])
28889846590eSStephen M. Cameron {
28899846590eSStephen M. Cameron 	int rc;
28909846590eSStephen M. Cameron 	int status;
28919846590eSStephen M. Cameron 	int size;
28929846590eSStephen M. Cameron 	unsigned char *buf;
28939846590eSStephen M. Cameron 
28949846590eSStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
28959846590eSStephen M. Cameron 	if (!buf)
28969846590eSStephen M. Cameron 		return HPSA_VPD_LV_STATUS_UNSUPPORTED;
28979846590eSStephen M. Cameron 
28989846590eSStephen M. Cameron 	/* Does controller have VPD for logical volume status? */
289924a4b078SStephen M. Cameron 	if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_STATUS))
29009846590eSStephen M. Cameron 		goto exit_failed;
29019846590eSStephen M. Cameron 
29029846590eSStephen M. Cameron 	/* Get the size of the VPD return buffer */
29039846590eSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
29049846590eSStephen M. Cameron 					buf, HPSA_VPD_HEADER_SZ);
290524a4b078SStephen M. Cameron 	if (rc != 0)
29069846590eSStephen M. Cameron 		goto exit_failed;
29079846590eSStephen M. Cameron 	size = buf[3];
29089846590eSStephen M. Cameron 
29099846590eSStephen M. Cameron 	/* Now get the whole VPD buffer */
29109846590eSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
29119846590eSStephen M. Cameron 					buf, size + HPSA_VPD_HEADER_SZ);
291224a4b078SStephen M. Cameron 	if (rc != 0)
29139846590eSStephen M. Cameron 		goto exit_failed;
29149846590eSStephen M. Cameron 	status = buf[4]; /* status byte */
29159846590eSStephen M. Cameron 
29169846590eSStephen M. Cameron 	kfree(buf);
29179846590eSStephen M. Cameron 	return status;
29189846590eSStephen M. Cameron exit_failed:
29199846590eSStephen M. Cameron 	kfree(buf);
29209846590eSStephen M. Cameron 	return HPSA_VPD_LV_STATUS_UNSUPPORTED;
29219846590eSStephen M. Cameron }
29229846590eSStephen M. Cameron 
29239846590eSStephen M. Cameron /* Determine offline status of a volume.
29249846590eSStephen M. Cameron  * Return either:
29259846590eSStephen M. Cameron  *  0 (not offline)
292667955ba3SStephen M. Cameron  *  0xff (offline for unknown reasons)
29279846590eSStephen M. Cameron  *  # (integer code indicating one of several NOT READY states
29289846590eSStephen M. Cameron  *     describing why a volume is to be kept offline)
29299846590eSStephen M. Cameron  */
293067955ba3SStephen M. Cameron static int hpsa_volume_offline(struct ctlr_info *h,
29319846590eSStephen M. Cameron 					unsigned char scsi3addr[])
29329846590eSStephen M. Cameron {
29339846590eSStephen M. Cameron 	struct CommandList *c;
29349437ac43SStephen Cameron 	unsigned char *sense;
29359437ac43SStephen Cameron 	u8 sense_key, asc, ascq;
29369437ac43SStephen Cameron 	int sense_len;
293725163bd5SWebb Scales 	int rc, ldstat = 0;
29389846590eSStephen M. Cameron 	u16 cmd_status;
29399846590eSStephen M. Cameron 	u8 scsi_status;
29409846590eSStephen M. Cameron #define ASC_LUN_NOT_READY 0x04
29419846590eSStephen M. Cameron #define ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS 0x04
29429846590eSStephen M. Cameron #define ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ 0x02
29439846590eSStephen M. Cameron 
29449846590eSStephen M. Cameron 	c = cmd_alloc(h);
2945bf43caf3SRobert Elliott 
29469846590eSStephen M. Cameron 	(void) fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, scsi3addr, TYPE_CMD);
294725163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
294825163bd5SWebb Scales 	if (rc) {
294925163bd5SWebb Scales 		cmd_free(h, c);
295025163bd5SWebb Scales 		return 0;
295125163bd5SWebb Scales 	}
29529846590eSStephen M. Cameron 	sense = c->err_info->SenseInfo;
29539437ac43SStephen Cameron 	if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
29549437ac43SStephen Cameron 		sense_len = sizeof(c->err_info->SenseInfo);
29559437ac43SStephen Cameron 	else
29569437ac43SStephen Cameron 		sense_len = c->err_info->SenseLen;
29579437ac43SStephen Cameron 	decode_sense_data(sense, sense_len, &sense_key, &asc, &ascq);
29589846590eSStephen M. Cameron 	cmd_status = c->err_info->CommandStatus;
29599846590eSStephen M. Cameron 	scsi_status = c->err_info->ScsiStatus;
29609846590eSStephen M. Cameron 	cmd_free(h, c);
29619846590eSStephen M. Cameron 	/* Is the volume 'not ready'? */
29629846590eSStephen M. Cameron 	if (cmd_status != CMD_TARGET_STATUS ||
29639846590eSStephen M. Cameron 		scsi_status != SAM_STAT_CHECK_CONDITION ||
29649846590eSStephen M. Cameron 		sense_key != NOT_READY ||
29659846590eSStephen M. Cameron 		asc != ASC_LUN_NOT_READY)  {
29669846590eSStephen M. Cameron 		return 0;
29679846590eSStephen M. Cameron 	}
29689846590eSStephen M. Cameron 
29699846590eSStephen M. Cameron 	/* Determine the reason for not ready state */
29709846590eSStephen M. Cameron 	ldstat = hpsa_get_volume_status(h, scsi3addr);
29719846590eSStephen M. Cameron 
29729846590eSStephen M. Cameron 	/* Keep volume offline in certain cases: */
29739846590eSStephen M. Cameron 	switch (ldstat) {
29749846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ERASE:
29759846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_RPI:
29769846590eSStephen M. Cameron 	case HPSA_LV_PENDING_RPI:
29779846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_NO_KEY:
29789846590eSStephen M. Cameron 	case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
29799846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION:
29809846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
29819846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
29829846590eSStephen M. Cameron 		return ldstat;
29839846590eSStephen M. Cameron 	case HPSA_VPD_LV_STATUS_UNSUPPORTED:
29849846590eSStephen M. Cameron 		/* If VPD status page isn't available,
29859846590eSStephen M. Cameron 		 * use ASC/ASCQ to determine state
29869846590eSStephen M. Cameron 		 */
29879846590eSStephen M. Cameron 		if ((ascq == ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS) ||
29889846590eSStephen M. Cameron 			(ascq == ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ))
29899846590eSStephen M. Cameron 			return ldstat;
29909846590eSStephen M. Cameron 		break;
29919846590eSStephen M. Cameron 	default:
29929846590eSStephen M. Cameron 		break;
29939846590eSStephen M. Cameron 	}
29949846590eSStephen M. Cameron 	return 0;
29959846590eSStephen M. Cameron }
29969846590eSStephen M. Cameron 
29979b5c48c2SStephen Cameron /*
29989b5c48c2SStephen Cameron  * Find out if a logical device supports aborts by simply trying one.
29999b5c48c2SStephen Cameron  * Smart Array may claim not to support aborts on logical drives, but
30009b5c48c2SStephen Cameron  * if a MSA2000 * is connected, the drives on that will be presented
30019b5c48c2SStephen Cameron  * by the Smart Array as logical drives, and aborts may be sent to
30029b5c48c2SStephen Cameron  * those devices successfully.  So the simplest way to find out is
30039b5c48c2SStephen Cameron  * to simply try an abort and see how the device responds.
30049b5c48c2SStephen Cameron  */
30059b5c48c2SStephen Cameron static int hpsa_device_supports_aborts(struct ctlr_info *h,
30069b5c48c2SStephen Cameron 					unsigned char *scsi3addr)
30079b5c48c2SStephen Cameron {
30089b5c48c2SStephen Cameron 	struct CommandList *c;
30099b5c48c2SStephen Cameron 	struct ErrorInfo *ei;
30109b5c48c2SStephen Cameron 	int rc = 0;
30119b5c48c2SStephen Cameron 
30129b5c48c2SStephen Cameron 	u64 tag = (u64) -1; /* bogus tag */
30139b5c48c2SStephen Cameron 
30149b5c48c2SStephen Cameron 	/* Assume that physical devices support aborts */
30159b5c48c2SStephen Cameron 	if (!is_logical_dev_addr_mode(scsi3addr))
30169b5c48c2SStephen Cameron 		return 1;
30179b5c48c2SStephen Cameron 
30189b5c48c2SStephen Cameron 	c = cmd_alloc(h);
3019bf43caf3SRobert Elliott 
30209b5c48c2SStephen Cameron 	(void) fill_cmd(c, HPSA_ABORT_MSG, h, &tag, 0, 0, scsi3addr, TYPE_MSG);
30219b5c48c2SStephen Cameron 	(void) hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
30229b5c48c2SStephen Cameron 	/* no unmap needed here because no data xfer. */
30239b5c48c2SStephen Cameron 	ei = c->err_info;
30249b5c48c2SStephen Cameron 	switch (ei->CommandStatus) {
30259b5c48c2SStephen Cameron 	case CMD_INVALID:
30269b5c48c2SStephen Cameron 		rc = 0;
30279b5c48c2SStephen Cameron 		break;
30289b5c48c2SStephen Cameron 	case CMD_UNABORTABLE:
30299b5c48c2SStephen Cameron 	case CMD_ABORT_FAILED:
30309b5c48c2SStephen Cameron 		rc = 1;
30319b5c48c2SStephen Cameron 		break;
30329437ac43SStephen Cameron 	case CMD_TMF_STATUS:
30339437ac43SStephen Cameron 		rc = hpsa_evaluate_tmf_status(h, c);
30349437ac43SStephen Cameron 		break;
30359b5c48c2SStephen Cameron 	default:
30369b5c48c2SStephen Cameron 		rc = 0;
30379b5c48c2SStephen Cameron 		break;
30389b5c48c2SStephen Cameron 	}
30399b5c48c2SStephen Cameron 	cmd_free(h, c);
30409b5c48c2SStephen Cameron 	return rc;
30419b5c48c2SStephen Cameron }
30429b5c48c2SStephen Cameron 
3043edd16368SStephen M. Cameron static int hpsa_update_device_info(struct ctlr_info *h,
30440b0e1d6cSStephen M. Cameron 	unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
30450b0e1d6cSStephen M. Cameron 	unsigned char *is_OBDR_device)
3046edd16368SStephen M. Cameron {
30470b0e1d6cSStephen M. Cameron 
30480b0e1d6cSStephen M. Cameron #define OBDR_SIG_OFFSET 43
30490b0e1d6cSStephen M. Cameron #define OBDR_TAPE_SIG "$DR-10"
30500b0e1d6cSStephen M. Cameron #define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
30510b0e1d6cSStephen M. Cameron #define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
30520b0e1d6cSStephen M. Cameron 
3053ea6d3bc3SStephen M. Cameron 	unsigned char *inq_buff;
30540b0e1d6cSStephen M. Cameron 	unsigned char *obdr_sig;
3055edd16368SStephen M. Cameron 
3056ea6d3bc3SStephen M. Cameron 	inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
3057edd16368SStephen M. Cameron 	if (!inq_buff)
3058edd16368SStephen M. Cameron 		goto bail_out;
3059edd16368SStephen M. Cameron 
3060edd16368SStephen M. Cameron 	/* Do an inquiry to the device to see what it is. */
3061edd16368SStephen M. Cameron 	if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
3062edd16368SStephen M. Cameron 		(unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
3063edd16368SStephen M. Cameron 		/* Inquiry failed (msg printed already) */
3064edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev,
3065edd16368SStephen M. Cameron 			"hpsa_update_device_info: inquiry failed\n");
3066edd16368SStephen M. Cameron 		goto bail_out;
3067edd16368SStephen M. Cameron 	}
3068edd16368SStephen M. Cameron 
3069edd16368SStephen M. Cameron 	this_device->devtype = (inq_buff[0] & 0x1f);
3070edd16368SStephen M. Cameron 	memcpy(this_device->scsi3addr, scsi3addr, 8);
3071edd16368SStephen M. Cameron 	memcpy(this_device->vendor, &inq_buff[8],
3072edd16368SStephen M. Cameron 		sizeof(this_device->vendor));
3073edd16368SStephen M. Cameron 	memcpy(this_device->model, &inq_buff[16],
3074edd16368SStephen M. Cameron 		sizeof(this_device->model));
3075edd16368SStephen M. Cameron 	memset(this_device->device_id, 0,
3076edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
3077edd16368SStephen M. Cameron 	hpsa_get_device_id(h, scsi3addr, this_device->device_id,
3078edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
3079edd16368SStephen M. Cameron 
3080edd16368SStephen M. Cameron 	if (this_device->devtype == TYPE_DISK &&
3081283b4a9bSStephen M. Cameron 		is_logical_dev_addr_mode(scsi3addr)) {
308267955ba3SStephen M. Cameron 		int volume_offline;
308367955ba3SStephen M. Cameron 
3084edd16368SStephen M. Cameron 		hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
3085283b4a9bSStephen M. Cameron 		if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
3086283b4a9bSStephen M. Cameron 			hpsa_get_ioaccel_status(h, scsi3addr, this_device);
308767955ba3SStephen M. Cameron 		volume_offline = hpsa_volume_offline(h, scsi3addr);
308867955ba3SStephen M. Cameron 		if (volume_offline < 0 || volume_offline > 0xff)
308967955ba3SStephen M. Cameron 			volume_offline = HPSA_VPD_LV_STATUS_UNSUPPORTED;
309067955ba3SStephen M. Cameron 		this_device->volume_offline = volume_offline & 0xff;
3091283b4a9bSStephen M. Cameron 	} else {
3092edd16368SStephen M. Cameron 		this_device->raid_level = RAID_UNKNOWN;
3093283b4a9bSStephen M. Cameron 		this_device->offload_config = 0;
3094283b4a9bSStephen M. Cameron 		this_device->offload_enabled = 0;
309541ce4c35SStephen Cameron 		this_device->offload_to_be_enabled = 0;
3096a3144e0bSJoe Handzik 		this_device->hba_ioaccel_enabled = 0;
30979846590eSStephen M. Cameron 		this_device->volume_offline = 0;
309803383736SDon Brace 		this_device->queue_depth = h->nr_cmds;
3099283b4a9bSStephen M. Cameron 	}
3100edd16368SStephen M. Cameron 
31010b0e1d6cSStephen M. Cameron 	if (is_OBDR_device) {
31020b0e1d6cSStephen M. Cameron 		/* See if this is a One-Button-Disaster-Recovery device
31030b0e1d6cSStephen M. Cameron 		 * by looking for "$DR-10" at offset 43 in inquiry data.
31040b0e1d6cSStephen M. Cameron 		 */
31050b0e1d6cSStephen M. Cameron 		obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
31060b0e1d6cSStephen M. Cameron 		*is_OBDR_device = (this_device->devtype == TYPE_ROM &&
31070b0e1d6cSStephen M. Cameron 					strncmp(obdr_sig, OBDR_TAPE_SIG,
31080b0e1d6cSStephen M. Cameron 						OBDR_SIG_LEN) == 0);
31090b0e1d6cSStephen M. Cameron 	}
3110edd16368SStephen M. Cameron 	kfree(inq_buff);
3111edd16368SStephen M. Cameron 	return 0;
3112edd16368SStephen M. Cameron 
3113edd16368SStephen M. Cameron bail_out:
3114edd16368SStephen M. Cameron 	kfree(inq_buff);
3115edd16368SStephen M. Cameron 	return 1;
3116edd16368SStephen M. Cameron }
3117edd16368SStephen M. Cameron 
31189b5c48c2SStephen Cameron static void hpsa_update_device_supports_aborts(struct ctlr_info *h,
31199b5c48c2SStephen Cameron 			struct hpsa_scsi_dev_t *dev, u8 *scsi3addr)
31209b5c48c2SStephen Cameron {
31219b5c48c2SStephen Cameron 	unsigned long flags;
31229b5c48c2SStephen Cameron 	int rc, entry;
31239b5c48c2SStephen Cameron 	/*
31249b5c48c2SStephen Cameron 	 * See if this device supports aborts.  If we already know
31259b5c48c2SStephen Cameron 	 * the device, we already know if it supports aborts, otherwise
31269b5c48c2SStephen Cameron 	 * we have to find out if it supports aborts by trying one.
31279b5c48c2SStephen Cameron 	 */
31289b5c48c2SStephen Cameron 	spin_lock_irqsave(&h->devlock, flags);
31299b5c48c2SStephen Cameron 	rc = hpsa_scsi_find_entry(dev, h->dev, h->ndevices, &entry);
31309b5c48c2SStephen Cameron 	if ((rc == DEVICE_SAME || rc == DEVICE_UPDATED) &&
31319b5c48c2SStephen Cameron 		entry >= 0 && entry < h->ndevices) {
31329b5c48c2SStephen Cameron 		dev->supports_aborts = h->dev[entry]->supports_aborts;
31339b5c48c2SStephen Cameron 		spin_unlock_irqrestore(&h->devlock, flags);
31349b5c48c2SStephen Cameron 	} else {
31359b5c48c2SStephen Cameron 		spin_unlock_irqrestore(&h->devlock, flags);
31369b5c48c2SStephen Cameron 		dev->supports_aborts =
31379b5c48c2SStephen Cameron 				hpsa_device_supports_aborts(h, scsi3addr);
31389b5c48c2SStephen Cameron 		if (dev->supports_aborts < 0)
31399b5c48c2SStephen Cameron 			dev->supports_aborts = 0;
31409b5c48c2SStephen Cameron 	}
31419b5c48c2SStephen Cameron }
31429b5c48c2SStephen Cameron 
31434f4eb9f1SScott Teel static unsigned char *ext_target_model[] = {
3144edd16368SStephen M. Cameron 	"MSA2012",
3145edd16368SStephen M. Cameron 	"MSA2024",
3146edd16368SStephen M. Cameron 	"MSA2312",
3147edd16368SStephen M. Cameron 	"MSA2324",
3148fda38518SStephen M. Cameron 	"P2000 G3 SAS",
3149e06c8e5cSStephen M. Cameron 	"MSA 2040 SAS",
3150edd16368SStephen M. Cameron 	NULL,
3151edd16368SStephen M. Cameron };
3152edd16368SStephen M. Cameron 
31534f4eb9f1SScott Teel static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
3154edd16368SStephen M. Cameron {
3155edd16368SStephen M. Cameron 	int i;
3156edd16368SStephen M. Cameron 
31574f4eb9f1SScott Teel 	for (i = 0; ext_target_model[i]; i++)
31584f4eb9f1SScott Teel 		if (strncmp(device->model, ext_target_model[i],
31594f4eb9f1SScott Teel 			strlen(ext_target_model[i])) == 0)
3160edd16368SStephen M. Cameron 			return 1;
3161edd16368SStephen M. Cameron 	return 0;
3162edd16368SStephen M. Cameron }
3163edd16368SStephen M. Cameron 
3164edd16368SStephen M. Cameron /* Helper function to assign bus, target, lun mapping of devices.
31654f4eb9f1SScott Teel  * Puts non-external target logical volumes on bus 0, external target logical
3166edd16368SStephen M. Cameron  * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
3167edd16368SStephen M. Cameron  * Logical drive target and lun are assigned at this time, but
3168edd16368SStephen M. Cameron  * physical device lun and target assignment are deferred (assigned
3169edd16368SStephen M. Cameron  * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
3170edd16368SStephen M. Cameron  */
3171edd16368SStephen M. Cameron static void figure_bus_target_lun(struct ctlr_info *h,
31721f310bdeSStephen M. Cameron 	u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
3173edd16368SStephen M. Cameron {
31741f310bdeSStephen M. Cameron 	u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
3175edd16368SStephen M. Cameron 
31761f310bdeSStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes)) {
31771f310bdeSStephen M. Cameron 		/* physical device, target and lun filled in later */
31781f310bdeSStephen M. Cameron 		if (is_hba_lunid(lunaddrbytes))
31791f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
31801f310bdeSStephen M. Cameron 		else
31811f310bdeSStephen M. Cameron 			/* defer target, lun assignment for physical devices */
31821f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 2, -1, -1);
31831f310bdeSStephen M. Cameron 		return;
31841f310bdeSStephen M. Cameron 	}
31851f310bdeSStephen M. Cameron 	/* It's a logical device */
31864f4eb9f1SScott Teel 	if (is_ext_target(h, device)) {
31874f4eb9f1SScott Teel 		/* external target way, put logicals on bus 1
3188339b2b14SStephen M. Cameron 		 * and match target/lun numbers box
31891f310bdeSStephen M. Cameron 		 * reports, other smart array, bus 0, target 0, match lunid
3190339b2b14SStephen M. Cameron 		 */
31911f310bdeSStephen M. Cameron 		hpsa_set_bus_target_lun(device,
31921f310bdeSStephen M. Cameron 			1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
31931f310bdeSStephen M. Cameron 		return;
3194339b2b14SStephen M. Cameron 	}
31951f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
3196edd16368SStephen M. Cameron }
3197edd16368SStephen M. Cameron 
3198edd16368SStephen M. Cameron /*
3199edd16368SStephen M. Cameron  * If there is no lun 0 on a target, linux won't find any devices.
32004f4eb9f1SScott Teel  * For the external targets (arrays), we have to manually detect the enclosure
3201edd16368SStephen M. Cameron  * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
3202edd16368SStephen M. Cameron  * it for some reason.  *tmpdevice is the target we're adding,
3203edd16368SStephen M. Cameron  * this_device is a pointer into the current element of currentsd[]
3204edd16368SStephen M. Cameron  * that we're building up in update_scsi_devices(), below.
3205edd16368SStephen M. Cameron  * lunzerobits is a bitmap that tracks which targets already have a
3206edd16368SStephen M. Cameron  * lun 0 assigned.
3207edd16368SStephen M. Cameron  * Returns 1 if an enclosure was added, 0 if not.
3208edd16368SStephen M. Cameron  */
32094f4eb9f1SScott Teel static int add_ext_target_dev(struct ctlr_info *h,
3210edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *tmpdevice,
321101a02ffcSStephen M. Cameron 	struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
32124f4eb9f1SScott Teel 	unsigned long lunzerobits[], int *n_ext_target_devs)
3213edd16368SStephen M. Cameron {
3214edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
3215edd16368SStephen M. Cameron 
32161f310bdeSStephen M. Cameron 	if (test_bit(tmpdevice->target, lunzerobits))
3217edd16368SStephen M. Cameron 		return 0; /* There is already a lun 0 on this target. */
3218edd16368SStephen M. Cameron 
3219edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes))
3220edd16368SStephen M. Cameron 		return 0; /* It's the logical targets that may lack lun 0. */
3221edd16368SStephen M. Cameron 
32224f4eb9f1SScott Teel 	if (!is_ext_target(h, tmpdevice))
32234f4eb9f1SScott Teel 		return 0; /* Only external target devices have this problem. */
3224edd16368SStephen M. Cameron 
32251f310bdeSStephen M. Cameron 	if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
3226edd16368SStephen M. Cameron 		return 0;
3227edd16368SStephen M. Cameron 
3228c4f8a299SStephen M. Cameron 	memset(scsi3addr, 0, 8);
32291f310bdeSStephen M. Cameron 	scsi3addr[3] = tmpdevice->target;
3230edd16368SStephen M. Cameron 	if (is_hba_lunid(scsi3addr))
3231edd16368SStephen M. Cameron 		return 0; /* Don't add the RAID controller here. */
3232edd16368SStephen M. Cameron 
3233339b2b14SStephen M. Cameron 	if (is_scsi_rev_5(h))
3234339b2b14SStephen M. Cameron 		return 0; /* p1210m doesn't need to do this. */
3235339b2b14SStephen M. Cameron 
32364f4eb9f1SScott Teel 	if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
3237aca4a520SScott Teel 		dev_warn(&h->pdev->dev, "Maximum number of external "
3238aca4a520SScott Teel 			"target devices exceeded.  Check your hardware "
3239edd16368SStephen M. Cameron 			"configuration.");
3240edd16368SStephen M. Cameron 		return 0;
3241edd16368SStephen M. Cameron 	}
3242edd16368SStephen M. Cameron 
32430b0e1d6cSStephen M. Cameron 	if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
3244edd16368SStephen M. Cameron 		return 0;
32454f4eb9f1SScott Teel 	(*n_ext_target_devs)++;
32461f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(this_device,
32471f310bdeSStephen M. Cameron 				tmpdevice->bus, tmpdevice->target, 0);
32489b5c48c2SStephen Cameron 	hpsa_update_device_supports_aborts(h, this_device, scsi3addr);
32491f310bdeSStephen M. Cameron 	set_bit(tmpdevice->target, lunzerobits);
3250edd16368SStephen M. Cameron 	return 1;
3251edd16368SStephen M. Cameron }
3252edd16368SStephen M. Cameron 
3253edd16368SStephen M. Cameron /*
325454b6e9e9SScott Teel  * Get address of physical disk used for an ioaccel2 mode command:
325554b6e9e9SScott Teel  *	1. Extract ioaccel2 handle from the command.
325654b6e9e9SScott Teel  *	2. Find a matching ioaccel2 handle from list of physical disks.
325754b6e9e9SScott Teel  *	3. Return:
325854b6e9e9SScott Teel  *		1 and set scsi3addr to address of matching physical
325954b6e9e9SScott Teel  *		0 if no matching physical disk was found.
326054b6e9e9SScott Teel  */
326154b6e9e9SScott Teel static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
326254b6e9e9SScott Teel 	struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
326354b6e9e9SScott Teel {
326441ce4c35SStephen Cameron 	struct io_accel2_cmd *c2 =
326541ce4c35SStephen Cameron 			&h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
326641ce4c35SStephen Cameron 	unsigned long flags;
326754b6e9e9SScott Teel 	int i;
326854b6e9e9SScott Teel 
326941ce4c35SStephen Cameron 	spin_lock_irqsave(&h->devlock, flags);
327041ce4c35SStephen Cameron 	for (i = 0; i < h->ndevices; i++)
327141ce4c35SStephen Cameron 		if (h->dev[i]->ioaccel_handle == le32_to_cpu(c2->scsi_nexus)) {
327241ce4c35SStephen Cameron 			memcpy(scsi3addr, h->dev[i]->scsi3addr,
327341ce4c35SStephen Cameron 				sizeof(h->dev[i]->scsi3addr));
327441ce4c35SStephen Cameron 			spin_unlock_irqrestore(&h->devlock, flags);
327554b6e9e9SScott Teel 			return 1;
327654b6e9e9SScott Teel 		}
327741ce4c35SStephen Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
327841ce4c35SStephen Cameron 	return 0;
327941ce4c35SStephen Cameron }
328041ce4c35SStephen Cameron 
328154b6e9e9SScott Teel /*
3282edd16368SStephen M. Cameron  * Do CISS_REPORT_PHYS and CISS_REPORT_LOG.  Data is returned in physdev,
3283edd16368SStephen M. Cameron  * logdev.  The number of luns in physdev and logdev are returned in
3284edd16368SStephen M. Cameron  * *nphysicals and *nlogicals, respectively.
3285edd16368SStephen M. Cameron  * Returns 0 on success, -1 otherwise.
3286edd16368SStephen M. Cameron  */
3287edd16368SStephen M. Cameron static int hpsa_gather_lun_info(struct ctlr_info *h,
328803383736SDon Brace 	struct ReportExtendedLUNdata *physdev, u32 *nphysicals,
328901a02ffcSStephen M. Cameron 	struct ReportLUNdata *logdev, u32 *nlogicals)
3290edd16368SStephen M. Cameron {
329103383736SDon Brace 	if (hpsa_scsi_do_report_phys_luns(h, physdev, sizeof(*physdev))) {
3292edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
3293edd16368SStephen M. Cameron 		return -1;
3294edd16368SStephen M. Cameron 	}
329503383736SDon Brace 	*nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 24;
3296edd16368SStephen M. Cameron 	if (*nphysicals > HPSA_MAX_PHYS_LUN) {
329703383736SDon Brace 		dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded. %d LUNs ignored.\n",
329803383736SDon Brace 			HPSA_MAX_PHYS_LUN, *nphysicals - HPSA_MAX_PHYS_LUN);
3299edd16368SStephen M. Cameron 		*nphysicals = HPSA_MAX_PHYS_LUN;
3300edd16368SStephen M. Cameron 	}
330103383736SDon Brace 	if (hpsa_scsi_do_report_log_luns(h, logdev, sizeof(*logdev))) {
3302edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
3303edd16368SStephen M. Cameron 		return -1;
3304edd16368SStephen M. Cameron 	}
33056df1e954SStephen M. Cameron 	*nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
3306edd16368SStephen M. Cameron 	/* Reject Logicals in excess of our max capability. */
3307edd16368SStephen M. Cameron 	if (*nlogicals > HPSA_MAX_LUN) {
3308edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
3309edd16368SStephen M. Cameron 			"maximum logical LUNs (%d) exceeded.  "
3310edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_LUN,
3311edd16368SStephen M. Cameron 			*nlogicals - HPSA_MAX_LUN);
3312edd16368SStephen M. Cameron 			*nlogicals = HPSA_MAX_LUN;
3313edd16368SStephen M. Cameron 	}
3314edd16368SStephen M. Cameron 	if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
3315edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
3316edd16368SStephen M. Cameron 			"maximum logical + physical LUNs (%d) exceeded. "
3317edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
3318edd16368SStephen M. Cameron 			*nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
3319edd16368SStephen M. Cameron 		*nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
3320edd16368SStephen M. Cameron 	}
3321edd16368SStephen M. Cameron 	return 0;
3322edd16368SStephen M. Cameron }
3323edd16368SStephen M. Cameron 
332442a91641SDon Brace static u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position,
332542a91641SDon Brace 	int i, int nphysicals, int nlogicals,
3326a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list,
3327339b2b14SStephen M. Cameron 	struct ReportLUNdata *logdev_list)
3328339b2b14SStephen M. Cameron {
3329339b2b14SStephen M. Cameron 	/* Helper function, figure out where the LUN ID info is coming from
3330339b2b14SStephen M. Cameron 	 * given index i, lists of physical and logical devices, where in
3331339b2b14SStephen M. Cameron 	 * the list the raid controller is supposed to appear (first or last)
3332339b2b14SStephen M. Cameron 	 */
3333339b2b14SStephen M. Cameron 
3334339b2b14SStephen M. Cameron 	int logicals_start = nphysicals + (raid_ctlr_position == 0);
3335339b2b14SStephen M. Cameron 	int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
3336339b2b14SStephen M. Cameron 
3337339b2b14SStephen M. Cameron 	if (i == raid_ctlr_position)
3338339b2b14SStephen M. Cameron 		return RAID_CTLR_LUNID;
3339339b2b14SStephen M. Cameron 
3340339b2b14SStephen M. Cameron 	if (i < logicals_start)
3341d5b5d964SStephen M. Cameron 		return &physdev_list->LUN[i -
3342d5b5d964SStephen M. Cameron 				(raid_ctlr_position == 0)].lunid[0];
3343339b2b14SStephen M. Cameron 
3344339b2b14SStephen M. Cameron 	if (i < last_device)
3345339b2b14SStephen M. Cameron 		return &logdev_list->LUN[i - nphysicals -
3346339b2b14SStephen M. Cameron 			(raid_ctlr_position == 0)][0];
3347339b2b14SStephen M. Cameron 	BUG();
3348339b2b14SStephen M. Cameron 	return NULL;
3349339b2b14SStephen M. Cameron }
3350339b2b14SStephen M. Cameron 
3351316b221aSStephen M. Cameron static int hpsa_hba_mode_enabled(struct ctlr_info *h)
3352316b221aSStephen M. Cameron {
3353316b221aSStephen M. Cameron 	int rc;
33546e8e8088SJoe Handzik 	int hba_mode_enabled;
3355316b221aSStephen M. Cameron 	struct bmic_controller_parameters *ctlr_params;
3356316b221aSStephen M. Cameron 	ctlr_params = kzalloc(sizeof(struct bmic_controller_parameters),
3357316b221aSStephen M. Cameron 		GFP_KERNEL);
3358316b221aSStephen M. Cameron 
3359316b221aSStephen M. Cameron 	if (!ctlr_params)
336096444fbbSJoe Handzik 		return -ENOMEM;
3361316b221aSStephen M. Cameron 	rc = hpsa_bmic_ctrl_mode_sense(h, RAID_CTLR_LUNID, 0, ctlr_params,
3362316b221aSStephen M. Cameron 		sizeof(struct bmic_controller_parameters));
336396444fbbSJoe Handzik 	if (rc) {
3364316b221aSStephen M. Cameron 		kfree(ctlr_params);
336596444fbbSJoe Handzik 		return rc;
3366316b221aSStephen M. Cameron 	}
33676e8e8088SJoe Handzik 
33686e8e8088SJoe Handzik 	hba_mode_enabled =
33696e8e8088SJoe Handzik 		((ctlr_params->nvram_flags & HBA_MODE_ENABLED_FLAG) != 0);
33706e8e8088SJoe Handzik 	kfree(ctlr_params);
33716e8e8088SJoe Handzik 	return hba_mode_enabled;
3372316b221aSStephen M. Cameron }
3373316b221aSStephen M. Cameron 
337403383736SDon Brace /* get physical drive ioaccel handle and queue depth */
337503383736SDon Brace static void hpsa_get_ioaccel_drive_info(struct ctlr_info *h,
337603383736SDon Brace 		struct hpsa_scsi_dev_t *dev,
337703383736SDon Brace 		u8 *lunaddrbytes,
337803383736SDon Brace 		struct bmic_identify_physical_device *id_phys)
337903383736SDon Brace {
338003383736SDon Brace 	int rc;
338103383736SDon Brace 	struct ext_report_lun_entry *rle =
338203383736SDon Brace 		(struct ext_report_lun_entry *) lunaddrbytes;
338303383736SDon Brace 
338403383736SDon Brace 	dev->ioaccel_handle = rle->ioaccel_handle;
3385a3144e0bSJoe Handzik 	if (PHYS_IOACCEL(lunaddrbytes) && dev->ioaccel_handle)
3386a3144e0bSJoe Handzik 		dev->hba_ioaccel_enabled = 1;
338703383736SDon Brace 	memset(id_phys, 0, sizeof(*id_phys));
338803383736SDon Brace 	rc = hpsa_bmic_id_physical_device(h, lunaddrbytes,
338903383736SDon Brace 			GET_BMIC_DRIVE_NUMBER(lunaddrbytes), id_phys,
339003383736SDon Brace 			sizeof(*id_phys));
339103383736SDon Brace 	if (!rc)
339203383736SDon Brace 		/* Reserve space for FW operations */
339303383736SDon Brace #define DRIVE_CMDS_RESERVED_FOR_FW 2
339403383736SDon Brace #define DRIVE_QUEUE_DEPTH 7
339503383736SDon Brace 		dev->queue_depth =
339603383736SDon Brace 			le16_to_cpu(id_phys->current_queue_depth_limit) -
339703383736SDon Brace 				DRIVE_CMDS_RESERVED_FOR_FW;
339803383736SDon Brace 	else
339903383736SDon Brace 		dev->queue_depth = DRIVE_QUEUE_DEPTH; /* conservative */
340003383736SDon Brace 	atomic_set(&dev->ioaccel_cmds_out, 0);
340103383736SDon Brace }
340203383736SDon Brace 
3403edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
3404edd16368SStephen M. Cameron {
3405edd16368SStephen M. Cameron 	/* the idea here is we could get notified
3406edd16368SStephen M. Cameron 	 * that some devices have changed, so we do a report
3407edd16368SStephen M. Cameron 	 * physical luns and report logical luns cmd, and adjust
3408edd16368SStephen M. Cameron 	 * our list of devices accordingly.
3409edd16368SStephen M. Cameron 	 *
3410edd16368SStephen M. Cameron 	 * The scsi3addr's of devices won't change so long as the
3411edd16368SStephen M. Cameron 	 * adapter is not reset.  That means we can rescan and
3412edd16368SStephen M. Cameron 	 * tell which devices we already know about, vs. new
3413edd16368SStephen M. Cameron 	 * devices, vs.  disappearing devices.
3414edd16368SStephen M. Cameron 	 */
3415a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list = NULL;
3416edd16368SStephen M. Cameron 	struct ReportLUNdata *logdev_list = NULL;
341703383736SDon Brace 	struct bmic_identify_physical_device *id_phys = NULL;
341801a02ffcSStephen M. Cameron 	u32 nphysicals = 0;
341901a02ffcSStephen M. Cameron 	u32 nlogicals = 0;
342001a02ffcSStephen M. Cameron 	u32 ndev_allocated = 0;
3421edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
3422edd16368SStephen M. Cameron 	int ncurrent = 0;
34234f4eb9f1SScott Teel 	int i, n_ext_target_devs, ndevs_to_allocate;
3424339b2b14SStephen M. Cameron 	int raid_ctlr_position;
34252bbf5c7fSJoe Handzik 	int rescan_hba_mode;
3426aca4a520SScott Teel 	DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
3427edd16368SStephen M. Cameron 
3428cfe5badcSScott Teel 	currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
342992084715SStephen M. Cameron 	physdev_list = kzalloc(sizeof(*physdev_list), GFP_KERNEL);
343092084715SStephen M. Cameron 	logdev_list = kzalloc(sizeof(*logdev_list), GFP_KERNEL);
3431edd16368SStephen M. Cameron 	tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
343203383736SDon Brace 	id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
3433edd16368SStephen M. Cameron 
343403383736SDon Brace 	if (!currentsd || !physdev_list || !logdev_list ||
343503383736SDon Brace 		!tmpdevice || !id_phys) {
3436edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory\n");
3437edd16368SStephen M. Cameron 		goto out;
3438edd16368SStephen M. Cameron 	}
3439edd16368SStephen M. Cameron 	memset(lunzerobits, 0, sizeof(lunzerobits));
3440edd16368SStephen M. Cameron 
3441316b221aSStephen M. Cameron 	rescan_hba_mode = hpsa_hba_mode_enabled(h);
344296444fbbSJoe Handzik 	if (rescan_hba_mode < 0)
344396444fbbSJoe Handzik 		goto out;
3444316b221aSStephen M. Cameron 
3445316b221aSStephen M. Cameron 	if (!h->hba_mode_enabled && rescan_hba_mode)
3446316b221aSStephen M. Cameron 		dev_warn(&h->pdev->dev, "HBA mode enabled\n");
3447316b221aSStephen M. Cameron 	else if (h->hba_mode_enabled && !rescan_hba_mode)
3448316b221aSStephen M. Cameron 		dev_warn(&h->pdev->dev, "HBA mode disabled\n");
3449316b221aSStephen M. Cameron 
3450316b221aSStephen M. Cameron 	h->hba_mode_enabled = rescan_hba_mode;
3451316b221aSStephen M. Cameron 
345203383736SDon Brace 	if (hpsa_gather_lun_info(h, physdev_list, &nphysicals,
345303383736SDon Brace 			logdev_list, &nlogicals))
3454edd16368SStephen M. Cameron 		goto out;
3455edd16368SStephen M. Cameron 
3456aca4a520SScott Teel 	/* We might see up to the maximum number of logical and physical disks
3457aca4a520SScott Teel 	 * plus external target devices, and a device for the local RAID
3458aca4a520SScott Teel 	 * controller.
3459edd16368SStephen M. Cameron 	 */
3460aca4a520SScott Teel 	ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
3461edd16368SStephen M. Cameron 
3462edd16368SStephen M. Cameron 	/* Allocate the per device structures */
3463edd16368SStephen M. Cameron 	for (i = 0; i < ndevs_to_allocate; i++) {
3464b7ec021fSScott Teel 		if (i >= HPSA_MAX_DEVICES) {
3465b7ec021fSScott Teel 			dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
3466b7ec021fSScott Teel 				"  %d devices ignored.\n", HPSA_MAX_DEVICES,
3467b7ec021fSScott Teel 				ndevs_to_allocate - HPSA_MAX_DEVICES);
3468b7ec021fSScott Teel 			break;
3469b7ec021fSScott Teel 		}
3470b7ec021fSScott Teel 
3471edd16368SStephen M. Cameron 		currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
3472edd16368SStephen M. Cameron 		if (!currentsd[i]) {
3473edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
3474edd16368SStephen M. Cameron 				__FILE__, __LINE__);
3475edd16368SStephen M. Cameron 			goto out;
3476edd16368SStephen M. Cameron 		}
3477edd16368SStephen M. Cameron 		ndev_allocated++;
3478edd16368SStephen M. Cameron 	}
3479edd16368SStephen M. Cameron 
34808645291bSStephen M. Cameron 	if (is_scsi_rev_5(h))
3481339b2b14SStephen M. Cameron 		raid_ctlr_position = 0;
3482339b2b14SStephen M. Cameron 	else
3483339b2b14SStephen M. Cameron 		raid_ctlr_position = nphysicals + nlogicals;
3484339b2b14SStephen M. Cameron 
3485edd16368SStephen M. Cameron 	/* adjust our table of devices */
34864f4eb9f1SScott Teel 	n_ext_target_devs = 0;
3487edd16368SStephen M. Cameron 	for (i = 0; i < nphysicals + nlogicals + 1; i++) {
34880b0e1d6cSStephen M. Cameron 		u8 *lunaddrbytes, is_OBDR = 0;
3489edd16368SStephen M. Cameron 
3490edd16368SStephen M. Cameron 		/* Figure out where the LUN ID info is coming from */
3491339b2b14SStephen M. Cameron 		lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
3492339b2b14SStephen M. Cameron 			i, nphysicals, nlogicals, physdev_list, logdev_list);
349341ce4c35SStephen Cameron 
349441ce4c35SStephen Cameron 		/* skip masked non-disk devices */
349541ce4c35SStephen Cameron 		if (MASKED_DEVICE(lunaddrbytes))
349641ce4c35SStephen Cameron 			if (i < nphysicals + (raid_ctlr_position == 0) &&
349741ce4c35SStephen Cameron 				NON_DISK_PHYS_DEV(lunaddrbytes))
3498edd16368SStephen M. Cameron 				continue;
3499edd16368SStephen M. Cameron 
3500edd16368SStephen M. Cameron 		/* Get device type, vendor, model, device id */
35010b0e1d6cSStephen M. Cameron 		if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
35020b0e1d6cSStephen M. Cameron 							&is_OBDR))
3503edd16368SStephen M. Cameron 			continue; /* skip it if we can't talk to it. */
35041f310bdeSStephen M. Cameron 		figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
35059b5c48c2SStephen Cameron 		hpsa_update_device_supports_aborts(h, tmpdevice, lunaddrbytes);
3506edd16368SStephen M. Cameron 		this_device = currentsd[ncurrent];
3507edd16368SStephen M. Cameron 
3508edd16368SStephen M. Cameron 		/*
35094f4eb9f1SScott Teel 		 * For external target devices, we have to insert a LUN 0 which
3510edd16368SStephen M. Cameron 		 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
3511edd16368SStephen M. Cameron 		 * is nonetheless an enclosure device there.  We have to
3512edd16368SStephen M. Cameron 		 * present that otherwise linux won't find anything if
3513edd16368SStephen M. Cameron 		 * there is no lun 0.
3514edd16368SStephen M. Cameron 		 */
35154f4eb9f1SScott Teel 		if (add_ext_target_dev(h, tmpdevice, this_device,
35161f310bdeSStephen M. Cameron 				lunaddrbytes, lunzerobits,
35174f4eb9f1SScott Teel 				&n_ext_target_devs)) {
3518edd16368SStephen M. Cameron 			ncurrent++;
3519edd16368SStephen M. Cameron 			this_device = currentsd[ncurrent];
3520edd16368SStephen M. Cameron 		}
3521edd16368SStephen M. Cameron 
3522edd16368SStephen M. Cameron 		*this_device = *tmpdevice;
3523edd16368SStephen M. Cameron 
352441ce4c35SStephen Cameron 		/* do not expose masked devices */
352541ce4c35SStephen Cameron 		if (MASKED_DEVICE(lunaddrbytes) &&
352641ce4c35SStephen Cameron 			i < nphysicals + (raid_ctlr_position == 0)) {
352741ce4c35SStephen Cameron 			if (h->hba_mode_enabled)
352841ce4c35SStephen Cameron 				dev_warn(&h->pdev->dev,
352941ce4c35SStephen Cameron 					"Masked physical device detected\n");
353041ce4c35SStephen Cameron 			this_device->expose_state = HPSA_DO_NOT_EXPOSE;
353141ce4c35SStephen Cameron 		} else {
353241ce4c35SStephen Cameron 			this_device->expose_state =
353341ce4c35SStephen Cameron 					HPSA_SG_ATTACH | HPSA_ULD_ATTACH;
353441ce4c35SStephen Cameron 		}
353541ce4c35SStephen Cameron 
3536edd16368SStephen M. Cameron 		switch (this_device->devtype) {
35370b0e1d6cSStephen M. Cameron 		case TYPE_ROM:
3538edd16368SStephen M. Cameron 			/* We don't *really* support actual CD-ROM devices,
3539edd16368SStephen M. Cameron 			 * just "One Button Disaster Recovery" tape drive
3540edd16368SStephen M. Cameron 			 * which temporarily pretends to be a CD-ROM drive.
3541edd16368SStephen M. Cameron 			 * So we check that the device is really an OBDR tape
3542edd16368SStephen M. Cameron 			 * device by checking for "$DR-10" in bytes 43-48 of
3543edd16368SStephen M. Cameron 			 * the inquiry data.
3544edd16368SStephen M. Cameron 			 */
35450b0e1d6cSStephen M. Cameron 			if (is_OBDR)
3546edd16368SStephen M. Cameron 				ncurrent++;
3547edd16368SStephen M. Cameron 			break;
3548edd16368SStephen M. Cameron 		case TYPE_DISK:
3549283b4a9bSStephen M. Cameron 			if (i >= nphysicals) {
3550283b4a9bSStephen M. Cameron 				ncurrent++;
3551edd16368SStephen M. Cameron 				break;
3552283b4a9bSStephen M. Cameron 			}
3553ecf418d1SJoe Handzik 
3554ecf418d1SJoe Handzik 			if (h->hba_mode_enabled)
3555ecf418d1SJoe Handzik 				/* never use raid mapper in HBA mode */
3556ecf418d1SJoe Handzik 				this_device->offload_enabled = 0;
3557ecf418d1SJoe Handzik 			else if (!(h->transMethod & CFGTBL_Trans_io_accel1 ||
3558ecf418d1SJoe Handzik 				h->transMethod & CFGTBL_Trans_io_accel2))
3559316b221aSStephen M. Cameron 				break;
3560ecf418d1SJoe Handzik 
356103383736SDon Brace 			hpsa_get_ioaccel_drive_info(h, this_device,
356203383736SDon Brace 						lunaddrbytes, id_phys);
356303383736SDon Brace 			atomic_set(&this_device->ioaccel_cmds_out, 0);
3564edd16368SStephen M. Cameron 			ncurrent++;
3565edd16368SStephen M. Cameron 			break;
3566edd16368SStephen M. Cameron 		case TYPE_TAPE:
3567edd16368SStephen M. Cameron 		case TYPE_MEDIUM_CHANGER:
3568edd16368SStephen M. Cameron 			ncurrent++;
3569edd16368SStephen M. Cameron 			break;
357041ce4c35SStephen Cameron 		case TYPE_ENCLOSURE:
357141ce4c35SStephen Cameron 			if (h->hba_mode_enabled)
357241ce4c35SStephen Cameron 				ncurrent++;
357341ce4c35SStephen Cameron 			break;
3574edd16368SStephen M. Cameron 		case TYPE_RAID:
3575edd16368SStephen M. Cameron 			/* Only present the Smartarray HBA as a RAID controller.
3576edd16368SStephen M. Cameron 			 * If it's a RAID controller other than the HBA itself
3577edd16368SStephen M. Cameron 			 * (an external RAID controller, MSA500 or similar)
3578edd16368SStephen M. Cameron 			 * don't present it.
3579edd16368SStephen M. Cameron 			 */
3580edd16368SStephen M. Cameron 			if (!is_hba_lunid(lunaddrbytes))
3581edd16368SStephen M. Cameron 				break;
3582edd16368SStephen M. Cameron 			ncurrent++;
3583edd16368SStephen M. Cameron 			break;
3584edd16368SStephen M. Cameron 		default:
3585edd16368SStephen M. Cameron 			break;
3586edd16368SStephen M. Cameron 		}
3587cfe5badcSScott Teel 		if (ncurrent >= HPSA_MAX_DEVICES)
3588edd16368SStephen M. Cameron 			break;
3589edd16368SStephen M. Cameron 	}
3590edd16368SStephen M. Cameron 	adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
3591edd16368SStephen M. Cameron out:
3592edd16368SStephen M. Cameron 	kfree(tmpdevice);
3593edd16368SStephen M. Cameron 	for (i = 0; i < ndev_allocated; i++)
3594edd16368SStephen M. Cameron 		kfree(currentsd[i]);
3595edd16368SStephen M. Cameron 	kfree(currentsd);
3596edd16368SStephen M. Cameron 	kfree(physdev_list);
3597edd16368SStephen M. Cameron 	kfree(logdev_list);
359803383736SDon Brace 	kfree(id_phys);
3599edd16368SStephen M. Cameron }
3600edd16368SStephen M. Cameron 
3601ec5cbf04SWebb Scales static void hpsa_set_sg_descriptor(struct SGDescriptor *desc,
3602ec5cbf04SWebb Scales 				   struct scatterlist *sg)
3603ec5cbf04SWebb Scales {
3604ec5cbf04SWebb Scales 	u64 addr64 = (u64) sg_dma_address(sg);
3605ec5cbf04SWebb Scales 	unsigned int len = sg_dma_len(sg);
3606ec5cbf04SWebb Scales 
3607ec5cbf04SWebb Scales 	desc->Addr = cpu_to_le64(addr64);
3608ec5cbf04SWebb Scales 	desc->Len = cpu_to_le32(len);
3609ec5cbf04SWebb Scales 	desc->Ext = 0;
3610ec5cbf04SWebb Scales }
3611ec5cbf04SWebb Scales 
3612c7ee65b3SWebb Scales /*
3613c7ee65b3SWebb Scales  * hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
3614edd16368SStephen M. Cameron  * dma mapping  and fills in the scatter gather entries of the
3615edd16368SStephen M. Cameron  * hpsa command, cp.
3616edd16368SStephen M. Cameron  */
361733a2ffceSStephen M. Cameron static int hpsa_scatter_gather(struct ctlr_info *h,
3618edd16368SStephen M. Cameron 		struct CommandList *cp,
3619edd16368SStephen M. Cameron 		struct scsi_cmnd *cmd)
3620edd16368SStephen M. Cameron {
3621edd16368SStephen M. Cameron 	struct scatterlist *sg;
362233a2ffceSStephen M. Cameron 	int use_sg, i, sg_index, chained;
362333a2ffceSStephen M. Cameron 	struct SGDescriptor *curr_sg;
3624edd16368SStephen M. Cameron 
362533a2ffceSStephen M. Cameron 	BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
3626edd16368SStephen M. Cameron 
3627edd16368SStephen M. Cameron 	use_sg = scsi_dma_map(cmd);
3628edd16368SStephen M. Cameron 	if (use_sg < 0)
3629edd16368SStephen M. Cameron 		return use_sg;
3630edd16368SStephen M. Cameron 
3631edd16368SStephen M. Cameron 	if (!use_sg)
3632edd16368SStephen M. Cameron 		goto sglist_finished;
3633edd16368SStephen M. Cameron 
363433a2ffceSStephen M. Cameron 	curr_sg = cp->SG;
363533a2ffceSStephen M. Cameron 	chained = 0;
363633a2ffceSStephen M. Cameron 	sg_index = 0;
3637edd16368SStephen M. Cameron 	scsi_for_each_sg(cmd, sg, use_sg, i) {
363833a2ffceSStephen M. Cameron 		if (i == h->max_cmd_sg_entries - 1 &&
363933a2ffceSStephen M. Cameron 			use_sg > h->max_cmd_sg_entries) {
364033a2ffceSStephen M. Cameron 			chained = 1;
364133a2ffceSStephen M. Cameron 			curr_sg = h->cmd_sg_list[cp->cmdindex];
364233a2ffceSStephen M. Cameron 			sg_index = 0;
364333a2ffceSStephen M. Cameron 		}
3644ec5cbf04SWebb Scales 		hpsa_set_sg_descriptor(curr_sg, sg);
364533a2ffceSStephen M. Cameron 		curr_sg++;
364633a2ffceSStephen M. Cameron 	}
3647ec5cbf04SWebb Scales 
3648ec5cbf04SWebb Scales 	/* Back the pointer up to the last entry and mark it as "last". */
364950a0decfSStephen M. Cameron 	(--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
365033a2ffceSStephen M. Cameron 
365133a2ffceSStephen M. Cameron 	if (use_sg + chained > h->maxSG)
365233a2ffceSStephen M. Cameron 		h->maxSG = use_sg + chained;
365333a2ffceSStephen M. Cameron 
365433a2ffceSStephen M. Cameron 	if (chained) {
365533a2ffceSStephen M. Cameron 		cp->Header.SGList = h->max_cmd_sg_entries;
365650a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(use_sg + 1);
3657e2bea6dfSStephen M. Cameron 		if (hpsa_map_sg_chain_block(h, cp)) {
3658e2bea6dfSStephen M. Cameron 			scsi_dma_unmap(cmd);
3659e2bea6dfSStephen M. Cameron 			return -1;
3660e2bea6dfSStephen M. Cameron 		}
366133a2ffceSStephen M. Cameron 		return 0;
3662edd16368SStephen M. Cameron 	}
3663edd16368SStephen M. Cameron 
3664edd16368SStephen M. Cameron sglist_finished:
3665edd16368SStephen M. Cameron 
366601a02ffcSStephen M. Cameron 	cp->Header.SGList = (u8) use_sg;   /* no. SGs contig in this cmd */
3667c7ee65b3SWebb Scales 	cp->Header.SGTotal = cpu_to_le16(use_sg); /* total sgs in cmd list */
3668edd16368SStephen M. Cameron 	return 0;
3669edd16368SStephen M. Cameron }
3670edd16368SStephen M. Cameron 
3671283b4a9bSStephen M. Cameron #define IO_ACCEL_INELIGIBLE (1)
3672283b4a9bSStephen M. Cameron static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
3673283b4a9bSStephen M. Cameron {
3674283b4a9bSStephen M. Cameron 	int is_write = 0;
3675283b4a9bSStephen M. Cameron 	u32 block;
3676283b4a9bSStephen M. Cameron 	u32 block_cnt;
3677283b4a9bSStephen M. Cameron 
3678283b4a9bSStephen M. Cameron 	/* Perform some CDB fixups if needed using 10 byte reads/writes only */
3679283b4a9bSStephen M. Cameron 	switch (cdb[0]) {
3680283b4a9bSStephen M. Cameron 	case WRITE_6:
3681283b4a9bSStephen M. Cameron 	case WRITE_12:
3682283b4a9bSStephen M. Cameron 		is_write = 1;
3683283b4a9bSStephen M. Cameron 	case READ_6:
3684283b4a9bSStephen M. Cameron 	case READ_12:
3685283b4a9bSStephen M. Cameron 		if (*cdb_len == 6) {
3686283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 8) | cdb[3];
3687283b4a9bSStephen M. Cameron 			block_cnt = cdb[4];
3688283b4a9bSStephen M. Cameron 		} else {
3689283b4a9bSStephen M. Cameron 			BUG_ON(*cdb_len != 12);
3690283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 24) |
3691283b4a9bSStephen M. Cameron 				(((u32) cdb[3]) << 16) |
3692283b4a9bSStephen M. Cameron 				(((u32) cdb[4]) << 8) |
3693283b4a9bSStephen M. Cameron 				cdb[5];
3694283b4a9bSStephen M. Cameron 			block_cnt =
3695283b4a9bSStephen M. Cameron 				(((u32) cdb[6]) << 24) |
3696283b4a9bSStephen M. Cameron 				(((u32) cdb[7]) << 16) |
3697283b4a9bSStephen M. Cameron 				(((u32) cdb[8]) << 8) |
3698283b4a9bSStephen M. Cameron 				cdb[9];
3699283b4a9bSStephen M. Cameron 		}
3700283b4a9bSStephen M. Cameron 		if (block_cnt > 0xffff)
3701283b4a9bSStephen M. Cameron 			return IO_ACCEL_INELIGIBLE;
3702283b4a9bSStephen M. Cameron 
3703283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
3704283b4a9bSStephen M. Cameron 		cdb[1] = 0;
3705283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (block >> 24);
3706283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (block >> 16);
3707283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (block >> 8);
3708283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (block);
3709283b4a9bSStephen M. Cameron 		cdb[6] = 0;
3710283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (block_cnt >> 8);
3711283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (block_cnt);
3712283b4a9bSStephen M. Cameron 		cdb[9] = 0;
3713283b4a9bSStephen M. Cameron 		*cdb_len = 10;
3714283b4a9bSStephen M. Cameron 		break;
3715283b4a9bSStephen M. Cameron 	}
3716283b4a9bSStephen M. Cameron 	return 0;
3717283b4a9bSStephen M. Cameron }
3718283b4a9bSStephen M. Cameron 
3719c349775eSScott Teel static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
3720283b4a9bSStephen M. Cameron 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
372103383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
3722e1f7de0cSMatt Gates {
3723e1f7de0cSMatt Gates 	struct scsi_cmnd *cmd = c->scsi_cmd;
3724e1f7de0cSMatt Gates 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
3725e1f7de0cSMatt Gates 	unsigned int len;
3726e1f7de0cSMatt Gates 	unsigned int total_len = 0;
3727e1f7de0cSMatt Gates 	struct scatterlist *sg;
3728e1f7de0cSMatt Gates 	u64 addr64;
3729e1f7de0cSMatt Gates 	int use_sg, i;
3730e1f7de0cSMatt Gates 	struct SGDescriptor *curr_sg;
3731e1f7de0cSMatt Gates 	u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
3732e1f7de0cSMatt Gates 
3733283b4a9bSStephen M. Cameron 	/* TODO: implement chaining support */
373403383736SDon Brace 	if (scsi_sg_count(cmd) > h->ioaccel_maxsg) {
373503383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3736283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
373703383736SDon Brace 	}
3738283b4a9bSStephen M. Cameron 
3739e1f7de0cSMatt Gates 	BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
3740e1f7de0cSMatt Gates 
374103383736SDon Brace 	if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
374203383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3743283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
374403383736SDon Brace 	}
3745283b4a9bSStephen M. Cameron 
3746e1f7de0cSMatt Gates 	c->cmd_type = CMD_IOACCEL1;
3747e1f7de0cSMatt Gates 
3748e1f7de0cSMatt Gates 	/* Adjust the DMA address to point to the accelerated command buffer */
3749e1f7de0cSMatt Gates 	c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
3750e1f7de0cSMatt Gates 				(c->cmdindex * sizeof(*cp));
3751e1f7de0cSMatt Gates 	BUG_ON(c->busaddr & 0x0000007F);
3752e1f7de0cSMatt Gates 
3753e1f7de0cSMatt Gates 	use_sg = scsi_dma_map(cmd);
375403383736SDon Brace 	if (use_sg < 0) {
375503383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3756e1f7de0cSMatt Gates 		return use_sg;
375703383736SDon Brace 	}
3758e1f7de0cSMatt Gates 
3759e1f7de0cSMatt Gates 	if (use_sg) {
3760e1f7de0cSMatt Gates 		curr_sg = cp->SG;
3761e1f7de0cSMatt Gates 		scsi_for_each_sg(cmd, sg, use_sg, i) {
3762e1f7de0cSMatt Gates 			addr64 = (u64) sg_dma_address(sg);
3763e1f7de0cSMatt Gates 			len  = sg_dma_len(sg);
3764e1f7de0cSMatt Gates 			total_len += len;
376550a0decfSStephen M. Cameron 			curr_sg->Addr = cpu_to_le64(addr64);
376650a0decfSStephen M. Cameron 			curr_sg->Len = cpu_to_le32(len);
376750a0decfSStephen M. Cameron 			curr_sg->Ext = cpu_to_le32(0);
3768e1f7de0cSMatt Gates 			curr_sg++;
3769e1f7de0cSMatt Gates 		}
377050a0decfSStephen M. Cameron 		(--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
3771e1f7de0cSMatt Gates 
3772e1f7de0cSMatt Gates 		switch (cmd->sc_data_direction) {
3773e1f7de0cSMatt Gates 		case DMA_TO_DEVICE:
3774e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_OUT;
3775e1f7de0cSMatt Gates 			break;
3776e1f7de0cSMatt Gates 		case DMA_FROM_DEVICE:
3777e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_IN;
3778e1f7de0cSMatt Gates 			break;
3779e1f7de0cSMatt Gates 		case DMA_NONE:
3780e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_NODATAXFER;
3781e1f7de0cSMatt Gates 			break;
3782e1f7de0cSMatt Gates 		default:
3783e1f7de0cSMatt Gates 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3784e1f7de0cSMatt Gates 			cmd->sc_data_direction);
3785e1f7de0cSMatt Gates 			BUG();
3786e1f7de0cSMatt Gates 			break;
3787e1f7de0cSMatt Gates 		}
3788e1f7de0cSMatt Gates 	} else {
3789e1f7de0cSMatt Gates 		control |= IOACCEL1_CONTROL_NODATAXFER;
3790e1f7de0cSMatt Gates 	}
3791e1f7de0cSMatt Gates 
3792c349775eSScott Teel 	c->Header.SGList = use_sg;
3793e1f7de0cSMatt Gates 	/* Fill out the command structure to submit */
37942b08b3e9SDon Brace 	cp->dev_handle = cpu_to_le16(ioaccel_handle & 0xFFFF);
37952b08b3e9SDon Brace 	cp->transfer_len = cpu_to_le32(total_len);
37962b08b3e9SDon Brace 	cp->io_flags = cpu_to_le16(IOACCEL1_IOFLAGS_IO_REQ |
37972b08b3e9SDon Brace 			(cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK));
37982b08b3e9SDon Brace 	cp->control = cpu_to_le32(control);
3799283b4a9bSStephen M. Cameron 	memcpy(cp->CDB, cdb, cdb_len);
3800283b4a9bSStephen M. Cameron 	memcpy(cp->CISS_LUN, scsi3addr, 8);
3801c349775eSScott Teel 	/* Tag was already set at init time. */
3802e1f7de0cSMatt Gates 	enqueue_cmd_and_start_io(h, c);
3803e1f7de0cSMatt Gates 	return 0;
3804e1f7de0cSMatt Gates }
3805edd16368SStephen M. Cameron 
3806283b4a9bSStephen M. Cameron /*
3807283b4a9bSStephen M. Cameron  * Queue a command directly to a device behind the controller using the
3808283b4a9bSStephen M. Cameron  * I/O accelerator path.
3809283b4a9bSStephen M. Cameron  */
3810283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
3811283b4a9bSStephen M. Cameron 	struct CommandList *c)
3812283b4a9bSStephen M. Cameron {
3813283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
3814283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3815283b4a9bSStephen M. Cameron 
381603383736SDon Brace 	c->phys_disk = dev;
381703383736SDon Brace 
3818283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
381903383736SDon Brace 		cmd->cmnd, cmd->cmd_len, dev->scsi3addr, dev);
3820283b4a9bSStephen M. Cameron }
3821283b4a9bSStephen M. Cameron 
3822dd0e19f3SScott Teel /*
3823dd0e19f3SScott Teel  * Set encryption parameters for the ioaccel2 request
3824dd0e19f3SScott Teel  */
3825dd0e19f3SScott Teel static void set_encrypt_ioaccel2(struct ctlr_info *h,
3826dd0e19f3SScott Teel 	struct CommandList *c, struct io_accel2_cmd *cp)
3827dd0e19f3SScott Teel {
3828dd0e19f3SScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
3829dd0e19f3SScott Teel 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3830dd0e19f3SScott Teel 	struct raid_map_data *map = &dev->raid_map;
3831dd0e19f3SScott Teel 	u64 first_block;
3832dd0e19f3SScott Teel 
3833dd0e19f3SScott Teel 	/* Are we doing encryption on this device */
38342b08b3e9SDon Brace 	if (!(le16_to_cpu(map->flags) & RAID_MAP_FLAG_ENCRYPT_ON))
3835dd0e19f3SScott Teel 		return;
3836dd0e19f3SScott Teel 	/* Set the data encryption key index. */
3837dd0e19f3SScott Teel 	cp->dekindex = map->dekindex;
3838dd0e19f3SScott Teel 
3839dd0e19f3SScott Teel 	/* Set the encryption enable flag, encoded into direction field. */
3840dd0e19f3SScott Teel 	cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
3841dd0e19f3SScott Teel 
3842dd0e19f3SScott Teel 	/* Set encryption tweak values based on logical block address
3843dd0e19f3SScott Teel 	 * If block size is 512, tweak value is LBA.
3844dd0e19f3SScott Teel 	 * For other block sizes, tweak is (LBA * block size)/ 512)
3845dd0e19f3SScott Teel 	 */
3846dd0e19f3SScott Teel 	switch (cmd->cmnd[0]) {
3847dd0e19f3SScott Teel 	/* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
3848dd0e19f3SScott Teel 	case WRITE_6:
3849dd0e19f3SScott Teel 	case READ_6:
38502b08b3e9SDon Brace 		first_block = get_unaligned_be16(&cmd->cmnd[2]);
3851dd0e19f3SScott Teel 		break;
3852dd0e19f3SScott Teel 	case WRITE_10:
3853dd0e19f3SScott Teel 	case READ_10:
3854dd0e19f3SScott Teel 	/* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
3855dd0e19f3SScott Teel 	case WRITE_12:
3856dd0e19f3SScott Teel 	case READ_12:
38572b08b3e9SDon Brace 		first_block = get_unaligned_be32(&cmd->cmnd[2]);
3858dd0e19f3SScott Teel 		break;
3859dd0e19f3SScott Teel 	case WRITE_16:
3860dd0e19f3SScott Teel 	case READ_16:
38612b08b3e9SDon Brace 		first_block = get_unaligned_be64(&cmd->cmnd[2]);
3862dd0e19f3SScott Teel 		break;
3863dd0e19f3SScott Teel 	default:
3864dd0e19f3SScott Teel 		dev_err(&h->pdev->dev,
38652b08b3e9SDon Brace 			"ERROR: %s: size (0x%x) not supported for encryption\n",
38662b08b3e9SDon Brace 			__func__, cmd->cmnd[0]);
3867dd0e19f3SScott Teel 		BUG();
3868dd0e19f3SScott Teel 		break;
3869dd0e19f3SScott Teel 	}
38702b08b3e9SDon Brace 
38712b08b3e9SDon Brace 	if (le32_to_cpu(map->volume_blk_size) != 512)
38722b08b3e9SDon Brace 		first_block = first_block *
38732b08b3e9SDon Brace 				le32_to_cpu(map->volume_blk_size)/512;
38742b08b3e9SDon Brace 
38752b08b3e9SDon Brace 	cp->tweak_lower = cpu_to_le32(first_block);
38762b08b3e9SDon Brace 	cp->tweak_upper = cpu_to_le32(first_block >> 32);
3877dd0e19f3SScott Teel }
3878dd0e19f3SScott Teel 
3879c349775eSScott Teel static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
3880c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
388103383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
3882c349775eSScott Teel {
3883c349775eSScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
3884c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
3885c349775eSScott Teel 	struct ioaccel2_sg_element *curr_sg;
3886c349775eSScott Teel 	int use_sg, i;
3887c349775eSScott Teel 	struct scatterlist *sg;
3888c349775eSScott Teel 	u64 addr64;
3889c349775eSScott Teel 	u32 len;
3890c349775eSScott Teel 	u32 total_len = 0;
3891c349775eSScott Teel 
3892d9a729f3SWebb Scales 	BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
3893c349775eSScott Teel 
389403383736SDon Brace 	if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
389503383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3896c349775eSScott Teel 		return IO_ACCEL_INELIGIBLE;
389703383736SDon Brace 	}
389803383736SDon Brace 
3899c349775eSScott Teel 	c->cmd_type = CMD_IOACCEL2;
3900c349775eSScott Teel 	/* Adjust the DMA address to point to the accelerated command buffer */
3901c349775eSScott Teel 	c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
3902c349775eSScott Teel 				(c->cmdindex * sizeof(*cp));
3903c349775eSScott Teel 	BUG_ON(c->busaddr & 0x0000007F);
3904c349775eSScott Teel 
3905c349775eSScott Teel 	memset(cp, 0, sizeof(*cp));
3906c349775eSScott Teel 	cp->IU_type = IOACCEL2_IU_TYPE;
3907c349775eSScott Teel 
3908c349775eSScott Teel 	use_sg = scsi_dma_map(cmd);
390903383736SDon Brace 	if (use_sg < 0) {
391003383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3911c349775eSScott Teel 		return use_sg;
391203383736SDon Brace 	}
3913c349775eSScott Teel 
3914c349775eSScott Teel 	if (use_sg) {
3915c349775eSScott Teel 		curr_sg = cp->sg;
3916d9a729f3SWebb Scales 		if (use_sg > h->ioaccel_maxsg) {
3917d9a729f3SWebb Scales 			addr64 = le64_to_cpu(
3918d9a729f3SWebb Scales 				h->ioaccel2_cmd_sg_list[c->cmdindex]->address);
3919d9a729f3SWebb Scales 			curr_sg->address = cpu_to_le64(addr64);
3920d9a729f3SWebb Scales 			curr_sg->length = 0;
3921d9a729f3SWebb Scales 			curr_sg->reserved[0] = 0;
3922d9a729f3SWebb Scales 			curr_sg->reserved[1] = 0;
3923d9a729f3SWebb Scales 			curr_sg->reserved[2] = 0;
3924d9a729f3SWebb Scales 			curr_sg->chain_indicator = 0x80;
3925d9a729f3SWebb Scales 
3926d9a729f3SWebb Scales 			curr_sg = h->ioaccel2_cmd_sg_list[c->cmdindex];
3927d9a729f3SWebb Scales 		}
3928c349775eSScott Teel 		scsi_for_each_sg(cmd, sg, use_sg, i) {
3929c349775eSScott Teel 			addr64 = (u64) sg_dma_address(sg);
3930c349775eSScott Teel 			len  = sg_dma_len(sg);
3931c349775eSScott Teel 			total_len += len;
3932c349775eSScott Teel 			curr_sg->address = cpu_to_le64(addr64);
3933c349775eSScott Teel 			curr_sg->length = cpu_to_le32(len);
3934c349775eSScott Teel 			curr_sg->reserved[0] = 0;
3935c349775eSScott Teel 			curr_sg->reserved[1] = 0;
3936c349775eSScott Teel 			curr_sg->reserved[2] = 0;
3937c349775eSScott Teel 			curr_sg->chain_indicator = 0;
3938c349775eSScott Teel 			curr_sg++;
3939c349775eSScott Teel 		}
3940c349775eSScott Teel 
3941c349775eSScott Teel 		switch (cmd->sc_data_direction) {
3942c349775eSScott Teel 		case DMA_TO_DEVICE:
3943dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3944dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_OUT;
3945c349775eSScott Teel 			break;
3946c349775eSScott Teel 		case DMA_FROM_DEVICE:
3947dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3948dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_IN;
3949c349775eSScott Teel 			break;
3950c349775eSScott Teel 		case DMA_NONE:
3951dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3952dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_NO_DATA;
3953c349775eSScott Teel 			break;
3954c349775eSScott Teel 		default:
3955c349775eSScott Teel 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3956c349775eSScott Teel 				cmd->sc_data_direction);
3957c349775eSScott Teel 			BUG();
3958c349775eSScott Teel 			break;
3959c349775eSScott Teel 		}
3960c349775eSScott Teel 	} else {
3961dd0e19f3SScott Teel 		cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3962dd0e19f3SScott Teel 		cp->direction |= IOACCEL2_DIR_NO_DATA;
3963c349775eSScott Teel 	}
3964dd0e19f3SScott Teel 
3965dd0e19f3SScott Teel 	/* Set encryption parameters, if necessary */
3966dd0e19f3SScott Teel 	set_encrypt_ioaccel2(h, c, cp);
3967dd0e19f3SScott Teel 
39682b08b3e9SDon Brace 	cp->scsi_nexus = cpu_to_le32(ioaccel_handle);
3969f2405db8SDon Brace 	cp->Tag = cpu_to_le32(c->cmdindex << DIRECT_LOOKUP_SHIFT);
3970c349775eSScott Teel 	memcpy(cp->cdb, cdb, sizeof(cp->cdb));
3971c349775eSScott Teel 
3972c349775eSScott Teel 	cp->data_len = cpu_to_le32(total_len);
3973c349775eSScott Teel 	cp->err_ptr = cpu_to_le64(c->busaddr +
3974c349775eSScott Teel 			offsetof(struct io_accel2_cmd, error_data));
397550a0decfSStephen M. Cameron 	cp->err_len = cpu_to_le32(sizeof(cp->error_data));
3976c349775eSScott Teel 
3977d9a729f3SWebb Scales 	/* fill in sg elements */
3978d9a729f3SWebb Scales 	if (use_sg > h->ioaccel_maxsg) {
3979d9a729f3SWebb Scales 		cp->sg_count = 1;
3980d9a729f3SWebb Scales 		if (hpsa_map_ioaccel2_sg_chain_block(h, cp, c)) {
3981d9a729f3SWebb Scales 			atomic_dec(&phys_disk->ioaccel_cmds_out);
3982d9a729f3SWebb Scales 			scsi_dma_unmap(cmd);
3983d9a729f3SWebb Scales 			return -1;
3984d9a729f3SWebb Scales 		}
3985d9a729f3SWebb Scales 	} else
3986d9a729f3SWebb Scales 		cp->sg_count = (u8) use_sg;
3987d9a729f3SWebb Scales 
3988c349775eSScott Teel 	enqueue_cmd_and_start_io(h, c);
3989c349775eSScott Teel 	return 0;
3990c349775eSScott Teel }
3991c349775eSScott Teel 
3992c349775eSScott Teel /*
3993c349775eSScott Teel  * Queue a command to the correct I/O accelerator path.
3994c349775eSScott Teel  */
3995c349775eSScott Teel static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
3996c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
399703383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
3998c349775eSScott Teel {
399903383736SDon Brace 	/* Try to honor the device's queue depth */
400003383736SDon Brace 	if (atomic_inc_return(&phys_disk->ioaccel_cmds_out) >
400103383736SDon Brace 					phys_disk->queue_depth) {
400203383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
400303383736SDon Brace 		return IO_ACCEL_INELIGIBLE;
400403383736SDon Brace 	}
4005c349775eSScott Teel 	if (h->transMethod & CFGTBL_Trans_io_accel1)
4006c349775eSScott Teel 		return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
400703383736SDon Brace 						cdb, cdb_len, scsi3addr,
400803383736SDon Brace 						phys_disk);
4009c349775eSScott Teel 	else
4010c349775eSScott Teel 		return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
401103383736SDon Brace 						cdb, cdb_len, scsi3addr,
401203383736SDon Brace 						phys_disk);
4013c349775eSScott Teel }
4014c349775eSScott Teel 
40156b80b18fSScott Teel static void raid_map_helper(struct raid_map_data *map,
40166b80b18fSScott Teel 		int offload_to_mirror, u32 *map_index, u32 *current_group)
40176b80b18fSScott Teel {
40186b80b18fSScott Teel 	if (offload_to_mirror == 0)  {
40196b80b18fSScott Teel 		/* use physical disk in the first mirrored group. */
40202b08b3e9SDon Brace 		*map_index %= le16_to_cpu(map->data_disks_per_row);
40216b80b18fSScott Teel 		return;
40226b80b18fSScott Teel 	}
40236b80b18fSScott Teel 	do {
40246b80b18fSScott Teel 		/* determine mirror group that *map_index indicates */
40252b08b3e9SDon Brace 		*current_group = *map_index /
40262b08b3e9SDon Brace 			le16_to_cpu(map->data_disks_per_row);
40276b80b18fSScott Teel 		if (offload_to_mirror == *current_group)
40286b80b18fSScott Teel 			continue;
40292b08b3e9SDon Brace 		if (*current_group < le16_to_cpu(map->layout_map_count) - 1) {
40306b80b18fSScott Teel 			/* select map index from next group */
40312b08b3e9SDon Brace 			*map_index += le16_to_cpu(map->data_disks_per_row);
40326b80b18fSScott Teel 			(*current_group)++;
40336b80b18fSScott Teel 		} else {
40346b80b18fSScott Teel 			/* select map index from first group */
40352b08b3e9SDon Brace 			*map_index %= le16_to_cpu(map->data_disks_per_row);
40366b80b18fSScott Teel 			*current_group = 0;
40376b80b18fSScott Teel 		}
40386b80b18fSScott Teel 	} while (offload_to_mirror != *current_group);
40396b80b18fSScott Teel }
40406b80b18fSScott Teel 
4041283b4a9bSStephen M. Cameron /*
4042283b4a9bSStephen M. Cameron  * Attempt to perform offload RAID mapping for a logical volume I/O.
4043283b4a9bSStephen M. Cameron  */
4044283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
4045283b4a9bSStephen M. Cameron 	struct CommandList *c)
4046283b4a9bSStephen M. Cameron {
4047283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
4048283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4049283b4a9bSStephen M. Cameron 	struct raid_map_data *map = &dev->raid_map;
4050283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map->data[0];
4051283b4a9bSStephen M. Cameron 	int is_write = 0;
4052283b4a9bSStephen M. Cameron 	u32 map_index;
4053283b4a9bSStephen M. Cameron 	u64 first_block, last_block;
4054283b4a9bSStephen M. Cameron 	u32 block_cnt;
4055283b4a9bSStephen M. Cameron 	u32 blocks_per_row;
4056283b4a9bSStephen M. Cameron 	u64 first_row, last_row;
4057283b4a9bSStephen M. Cameron 	u32 first_row_offset, last_row_offset;
4058283b4a9bSStephen M. Cameron 	u32 first_column, last_column;
40596b80b18fSScott Teel 	u64 r0_first_row, r0_last_row;
40606b80b18fSScott Teel 	u32 r5or6_blocks_per_row;
40616b80b18fSScott Teel 	u64 r5or6_first_row, r5or6_last_row;
40626b80b18fSScott Teel 	u32 r5or6_first_row_offset, r5or6_last_row_offset;
40636b80b18fSScott Teel 	u32 r5or6_first_column, r5or6_last_column;
40646b80b18fSScott Teel 	u32 total_disks_per_row;
40656b80b18fSScott Teel 	u32 stripesize;
40666b80b18fSScott Teel 	u32 first_group, last_group, current_group;
4067283b4a9bSStephen M. Cameron 	u32 map_row;
4068283b4a9bSStephen M. Cameron 	u32 disk_handle;
4069283b4a9bSStephen M. Cameron 	u64 disk_block;
4070283b4a9bSStephen M. Cameron 	u32 disk_block_cnt;
4071283b4a9bSStephen M. Cameron 	u8 cdb[16];
4072283b4a9bSStephen M. Cameron 	u8 cdb_len;
40732b08b3e9SDon Brace 	u16 strip_size;
4074283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
4075283b4a9bSStephen M. Cameron 	u64 tmpdiv;
4076283b4a9bSStephen M. Cameron #endif
40776b80b18fSScott Teel 	int offload_to_mirror;
4078283b4a9bSStephen M. Cameron 
4079283b4a9bSStephen M. Cameron 	/* check for valid opcode, get LBA and block count */
4080283b4a9bSStephen M. Cameron 	switch (cmd->cmnd[0]) {
4081283b4a9bSStephen M. Cameron 	case WRITE_6:
4082283b4a9bSStephen M. Cameron 		is_write = 1;
4083283b4a9bSStephen M. Cameron 	case READ_6:
4084283b4a9bSStephen M. Cameron 		first_block =
4085283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 8) |
4086283b4a9bSStephen M. Cameron 			cmd->cmnd[3];
4087283b4a9bSStephen M. Cameron 		block_cnt = cmd->cmnd[4];
40883fa89a04SStephen M. Cameron 		if (block_cnt == 0)
40893fa89a04SStephen M. Cameron 			block_cnt = 256;
4090283b4a9bSStephen M. Cameron 		break;
4091283b4a9bSStephen M. Cameron 	case WRITE_10:
4092283b4a9bSStephen M. Cameron 		is_write = 1;
4093283b4a9bSStephen M. Cameron 	case READ_10:
4094283b4a9bSStephen M. Cameron 		first_block =
4095283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
4096283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
4097283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
4098283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
4099283b4a9bSStephen M. Cameron 		block_cnt =
4100283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 8) |
4101283b4a9bSStephen M. Cameron 			cmd->cmnd[8];
4102283b4a9bSStephen M. Cameron 		break;
4103283b4a9bSStephen M. Cameron 	case WRITE_12:
4104283b4a9bSStephen M. Cameron 		is_write = 1;
4105283b4a9bSStephen M. Cameron 	case READ_12:
4106283b4a9bSStephen M. Cameron 		first_block =
4107283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
4108283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
4109283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
4110283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
4111283b4a9bSStephen M. Cameron 		block_cnt =
4112283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[6]) << 24) |
4113283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 16) |
4114283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[8]) << 8) |
4115283b4a9bSStephen M. Cameron 		cmd->cmnd[9];
4116283b4a9bSStephen M. Cameron 		break;
4117283b4a9bSStephen M. Cameron 	case WRITE_16:
4118283b4a9bSStephen M. Cameron 		is_write = 1;
4119283b4a9bSStephen M. Cameron 	case READ_16:
4120283b4a9bSStephen M. Cameron 		first_block =
4121283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 56) |
4122283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 48) |
4123283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 40) |
4124283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[5]) << 32) |
4125283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[6]) << 24) |
4126283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[7]) << 16) |
4127283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[8]) << 8) |
4128283b4a9bSStephen M. Cameron 			cmd->cmnd[9];
4129283b4a9bSStephen M. Cameron 		block_cnt =
4130283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[10]) << 24) |
4131283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[11]) << 16) |
4132283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[12]) << 8) |
4133283b4a9bSStephen M. Cameron 			cmd->cmnd[13];
4134283b4a9bSStephen M. Cameron 		break;
4135283b4a9bSStephen M. Cameron 	default:
4136283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
4137283b4a9bSStephen M. Cameron 	}
4138283b4a9bSStephen M. Cameron 	last_block = first_block + block_cnt - 1;
4139283b4a9bSStephen M. Cameron 
4140283b4a9bSStephen M. Cameron 	/* check for write to non-RAID-0 */
4141283b4a9bSStephen M. Cameron 	if (is_write && dev->raid_level != 0)
4142283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
4143283b4a9bSStephen M. Cameron 
4144283b4a9bSStephen M. Cameron 	/* check for invalid block or wraparound */
41452b08b3e9SDon Brace 	if (last_block >= le64_to_cpu(map->volume_blk_cnt) ||
41462b08b3e9SDon Brace 		last_block < first_block)
4147283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
4148283b4a9bSStephen M. Cameron 
4149283b4a9bSStephen M. Cameron 	/* calculate stripe information for the request */
41502b08b3e9SDon Brace 	blocks_per_row = le16_to_cpu(map->data_disks_per_row) *
41512b08b3e9SDon Brace 				le16_to_cpu(map->strip_size);
41522b08b3e9SDon Brace 	strip_size = le16_to_cpu(map->strip_size);
4153283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
4154283b4a9bSStephen M. Cameron 	tmpdiv = first_block;
4155283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
4156283b4a9bSStephen M. Cameron 	first_row = tmpdiv;
4157283b4a9bSStephen M. Cameron 	tmpdiv = last_block;
4158283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
4159283b4a9bSStephen M. Cameron 	last_row = tmpdiv;
4160283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
4161283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
4162283b4a9bSStephen M. Cameron 	tmpdiv = first_row_offset;
41632b08b3e9SDon Brace 	(void) do_div(tmpdiv, strip_size);
4164283b4a9bSStephen M. Cameron 	first_column = tmpdiv;
4165283b4a9bSStephen M. Cameron 	tmpdiv = last_row_offset;
41662b08b3e9SDon Brace 	(void) do_div(tmpdiv, strip_size);
4167283b4a9bSStephen M. Cameron 	last_column = tmpdiv;
4168283b4a9bSStephen M. Cameron #else
4169283b4a9bSStephen M. Cameron 	first_row = first_block / blocks_per_row;
4170283b4a9bSStephen M. Cameron 	last_row = last_block / blocks_per_row;
4171283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
4172283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
41732b08b3e9SDon Brace 	first_column = first_row_offset / strip_size;
41742b08b3e9SDon Brace 	last_column = last_row_offset / strip_size;
4175283b4a9bSStephen M. Cameron #endif
4176283b4a9bSStephen M. Cameron 
4177283b4a9bSStephen M. Cameron 	/* if this isn't a single row/column then give to the controller */
4178283b4a9bSStephen M. Cameron 	if ((first_row != last_row) || (first_column != last_column))
4179283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
4180283b4a9bSStephen M. Cameron 
4181283b4a9bSStephen M. Cameron 	/* proceeding with driver mapping */
41822b08b3e9SDon Brace 	total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
41832b08b3e9SDon Brace 				le16_to_cpu(map->metadata_disks_per_row);
4184283b4a9bSStephen M. Cameron 	map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
41852b08b3e9SDon Brace 				le16_to_cpu(map->row_cnt);
41866b80b18fSScott Teel 	map_index = (map_row * total_disks_per_row) + first_column;
41876b80b18fSScott Teel 
41886b80b18fSScott Teel 	switch (dev->raid_level) {
41896b80b18fSScott Teel 	case HPSA_RAID_0:
41906b80b18fSScott Teel 		break; /* nothing special to do */
41916b80b18fSScott Teel 	case HPSA_RAID_1:
41926b80b18fSScott Teel 		/* Handles load balance across RAID 1 members.
41936b80b18fSScott Teel 		 * (2-drive R1 and R10 with even # of drives.)
41946b80b18fSScott Teel 		 * Appropriate for SSDs, not optimal for HDDs
4195283b4a9bSStephen M. Cameron 		 */
41962b08b3e9SDon Brace 		BUG_ON(le16_to_cpu(map->layout_map_count) != 2);
4197283b4a9bSStephen M. Cameron 		if (dev->offload_to_mirror)
41982b08b3e9SDon Brace 			map_index += le16_to_cpu(map->data_disks_per_row);
4199283b4a9bSStephen M. Cameron 		dev->offload_to_mirror = !dev->offload_to_mirror;
42006b80b18fSScott Teel 		break;
42016b80b18fSScott Teel 	case HPSA_RAID_ADM:
42026b80b18fSScott Teel 		/* Handles N-way mirrors  (R1-ADM)
42036b80b18fSScott Teel 		 * and R10 with # of drives divisible by 3.)
42046b80b18fSScott Teel 		 */
42052b08b3e9SDon Brace 		BUG_ON(le16_to_cpu(map->layout_map_count) != 3);
42066b80b18fSScott Teel 
42076b80b18fSScott Teel 		offload_to_mirror = dev->offload_to_mirror;
42086b80b18fSScott Teel 		raid_map_helper(map, offload_to_mirror,
42096b80b18fSScott Teel 				&map_index, &current_group);
42106b80b18fSScott Teel 		/* set mirror group to use next time */
42116b80b18fSScott Teel 		offload_to_mirror =
42122b08b3e9SDon Brace 			(offload_to_mirror >=
42132b08b3e9SDon Brace 			le16_to_cpu(map->layout_map_count) - 1)
42146b80b18fSScott Teel 			? 0 : offload_to_mirror + 1;
42156b80b18fSScott Teel 		dev->offload_to_mirror = offload_to_mirror;
42166b80b18fSScott Teel 		/* Avoid direct use of dev->offload_to_mirror within this
42176b80b18fSScott Teel 		 * function since multiple threads might simultaneously
42186b80b18fSScott Teel 		 * increment it beyond the range of dev->layout_map_count -1.
42196b80b18fSScott Teel 		 */
42206b80b18fSScott Teel 		break;
42216b80b18fSScott Teel 	case HPSA_RAID_5:
42226b80b18fSScott Teel 	case HPSA_RAID_6:
42232b08b3e9SDon Brace 		if (le16_to_cpu(map->layout_map_count) <= 1)
42246b80b18fSScott Teel 			break;
42256b80b18fSScott Teel 
42266b80b18fSScott Teel 		/* Verify first and last block are in same RAID group */
42276b80b18fSScott Teel 		r5or6_blocks_per_row =
42282b08b3e9SDon Brace 			le16_to_cpu(map->strip_size) *
42292b08b3e9SDon Brace 			le16_to_cpu(map->data_disks_per_row);
42306b80b18fSScott Teel 		BUG_ON(r5or6_blocks_per_row == 0);
42312b08b3e9SDon Brace 		stripesize = r5or6_blocks_per_row *
42322b08b3e9SDon Brace 			le16_to_cpu(map->layout_map_count);
42336b80b18fSScott Teel #if BITS_PER_LONG == 32
42346b80b18fSScott Teel 		tmpdiv = first_block;
42356b80b18fSScott Teel 		first_group = do_div(tmpdiv, stripesize);
42366b80b18fSScott Teel 		tmpdiv = first_group;
42376b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
42386b80b18fSScott Teel 		first_group = tmpdiv;
42396b80b18fSScott Teel 		tmpdiv = last_block;
42406b80b18fSScott Teel 		last_group = do_div(tmpdiv, stripesize);
42416b80b18fSScott Teel 		tmpdiv = last_group;
42426b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
42436b80b18fSScott Teel 		last_group = tmpdiv;
42446b80b18fSScott Teel #else
42456b80b18fSScott Teel 		first_group = (first_block % stripesize) / r5or6_blocks_per_row;
42466b80b18fSScott Teel 		last_group = (last_block % stripesize) / r5or6_blocks_per_row;
42476b80b18fSScott Teel #endif
4248000ff7c2SStephen M. Cameron 		if (first_group != last_group)
42496b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
42506b80b18fSScott Teel 
42516b80b18fSScott Teel 		/* Verify request is in a single row of RAID 5/6 */
42526b80b18fSScott Teel #if BITS_PER_LONG == 32
42536b80b18fSScott Teel 		tmpdiv = first_block;
42546b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
42556b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row = tmpdiv;
42566b80b18fSScott Teel 		tmpdiv = last_block;
42576b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
42586b80b18fSScott Teel 		r5or6_last_row = r0_last_row = tmpdiv;
42596b80b18fSScott Teel #else
42606b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row =
42616b80b18fSScott Teel 						first_block / stripesize;
42626b80b18fSScott Teel 		r5or6_last_row = r0_last_row = last_block / stripesize;
42636b80b18fSScott Teel #endif
42646b80b18fSScott Teel 		if (r5or6_first_row != r5or6_last_row)
42656b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
42666b80b18fSScott Teel 
42676b80b18fSScott Teel 
42686b80b18fSScott Teel 		/* Verify request is in a single column */
42696b80b18fSScott Teel #if BITS_PER_LONG == 32
42706b80b18fSScott Teel 		tmpdiv = first_block;
42716b80b18fSScott Teel 		first_row_offset = do_div(tmpdiv, stripesize);
42726b80b18fSScott Teel 		tmpdiv = first_row_offset;
42736b80b18fSScott Teel 		first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
42746b80b18fSScott Teel 		r5or6_first_row_offset = first_row_offset;
42756b80b18fSScott Teel 		tmpdiv = last_block;
42766b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, stripesize);
42776b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
42786b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
42796b80b18fSScott Teel 		tmpdiv = r5or6_first_row_offset;
42806b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
42816b80b18fSScott Teel 		first_column = r5or6_first_column = tmpdiv;
42826b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
42836b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
42846b80b18fSScott Teel 		r5or6_last_column = tmpdiv;
42856b80b18fSScott Teel #else
42866b80b18fSScott Teel 		first_row_offset = r5or6_first_row_offset =
42876b80b18fSScott Teel 			(u32)((first_block % stripesize) %
42886b80b18fSScott Teel 						r5or6_blocks_per_row);
42896b80b18fSScott Teel 
42906b80b18fSScott Teel 		r5or6_last_row_offset =
42916b80b18fSScott Teel 			(u32)((last_block % stripesize) %
42926b80b18fSScott Teel 						r5or6_blocks_per_row);
42936b80b18fSScott Teel 
42946b80b18fSScott Teel 		first_column = r5or6_first_column =
42952b08b3e9SDon Brace 			r5or6_first_row_offset / le16_to_cpu(map->strip_size);
42966b80b18fSScott Teel 		r5or6_last_column =
42972b08b3e9SDon Brace 			r5or6_last_row_offset / le16_to_cpu(map->strip_size);
42986b80b18fSScott Teel #endif
42996b80b18fSScott Teel 		if (r5or6_first_column != r5or6_last_column)
43006b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
43016b80b18fSScott Teel 
43026b80b18fSScott Teel 		/* Request is eligible */
43036b80b18fSScott Teel 		map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
43042b08b3e9SDon Brace 			le16_to_cpu(map->row_cnt);
43056b80b18fSScott Teel 
43066b80b18fSScott Teel 		map_index = (first_group *
43072b08b3e9SDon Brace 			(le16_to_cpu(map->row_cnt) * total_disks_per_row)) +
43086b80b18fSScott Teel 			(map_row * total_disks_per_row) + first_column;
43096b80b18fSScott Teel 		break;
43106b80b18fSScott Teel 	default:
43116b80b18fSScott Teel 		return IO_ACCEL_INELIGIBLE;
4312283b4a9bSStephen M. Cameron 	}
43136b80b18fSScott Teel 
431407543e0cSStephen Cameron 	if (unlikely(map_index >= RAID_MAP_MAX_ENTRIES))
431507543e0cSStephen Cameron 		return IO_ACCEL_INELIGIBLE;
431607543e0cSStephen Cameron 
431703383736SDon Brace 	c->phys_disk = dev->phys_disk[map_index];
431803383736SDon Brace 
4319283b4a9bSStephen M. Cameron 	disk_handle = dd[map_index].ioaccel_handle;
43202b08b3e9SDon Brace 	disk_block = le64_to_cpu(map->disk_starting_blk) +
43212b08b3e9SDon Brace 			first_row * le16_to_cpu(map->strip_size) +
43222b08b3e9SDon Brace 			(first_row_offset - first_column *
43232b08b3e9SDon Brace 			le16_to_cpu(map->strip_size));
4324283b4a9bSStephen M. Cameron 	disk_block_cnt = block_cnt;
4325283b4a9bSStephen M. Cameron 
4326283b4a9bSStephen M. Cameron 	/* handle differing logical/physical block sizes */
4327283b4a9bSStephen M. Cameron 	if (map->phys_blk_shift) {
4328283b4a9bSStephen M. Cameron 		disk_block <<= map->phys_blk_shift;
4329283b4a9bSStephen M. Cameron 		disk_block_cnt <<= map->phys_blk_shift;
4330283b4a9bSStephen M. Cameron 	}
4331283b4a9bSStephen M. Cameron 	BUG_ON(disk_block_cnt > 0xffff);
4332283b4a9bSStephen M. Cameron 
4333283b4a9bSStephen M. Cameron 	/* build the new CDB for the physical disk I/O */
4334283b4a9bSStephen M. Cameron 	if (disk_block > 0xffffffff) {
4335283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_16 : READ_16;
4336283b4a9bSStephen M. Cameron 		cdb[1] = 0;
4337283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 56);
4338283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 48);
4339283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 40);
4340283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block >> 32);
4341283b4a9bSStephen M. Cameron 		cdb[6] = (u8) (disk_block >> 24);
4342283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block >> 16);
4343283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block >> 8);
4344283b4a9bSStephen M. Cameron 		cdb[9] = (u8) (disk_block);
4345283b4a9bSStephen M. Cameron 		cdb[10] = (u8) (disk_block_cnt >> 24);
4346283b4a9bSStephen M. Cameron 		cdb[11] = (u8) (disk_block_cnt >> 16);
4347283b4a9bSStephen M. Cameron 		cdb[12] = (u8) (disk_block_cnt >> 8);
4348283b4a9bSStephen M. Cameron 		cdb[13] = (u8) (disk_block_cnt);
4349283b4a9bSStephen M. Cameron 		cdb[14] = 0;
4350283b4a9bSStephen M. Cameron 		cdb[15] = 0;
4351283b4a9bSStephen M. Cameron 		cdb_len = 16;
4352283b4a9bSStephen M. Cameron 	} else {
4353283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
4354283b4a9bSStephen M. Cameron 		cdb[1] = 0;
4355283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 24);
4356283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 16);
4357283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 8);
4358283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block);
4359283b4a9bSStephen M. Cameron 		cdb[6] = 0;
4360283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block_cnt >> 8);
4361283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block_cnt);
4362283b4a9bSStephen M. Cameron 		cdb[9] = 0;
4363283b4a9bSStephen M. Cameron 		cdb_len = 10;
4364283b4a9bSStephen M. Cameron 	}
4365283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
436603383736SDon Brace 						dev->scsi3addr,
436703383736SDon Brace 						dev->phys_disk[map_index]);
4368283b4a9bSStephen M. Cameron }
4369283b4a9bSStephen M. Cameron 
437025163bd5SWebb Scales /*
437125163bd5SWebb Scales  * Submit commands down the "normal" RAID stack path
437225163bd5SWebb Scales  * All callers to hpsa_ciss_submit must check lockup_detected
437325163bd5SWebb Scales  * beforehand, before (opt.) and after calling cmd_alloc
437425163bd5SWebb Scales  */
4375574f05d3SStephen Cameron static int hpsa_ciss_submit(struct ctlr_info *h,
4376574f05d3SStephen Cameron 	struct CommandList *c, struct scsi_cmnd *cmd,
4377574f05d3SStephen Cameron 	unsigned char scsi3addr[])
4378edd16368SStephen M. Cameron {
4379edd16368SStephen M. Cameron 	cmd->host_scribble = (unsigned char *) c;
4380edd16368SStephen M. Cameron 	c->cmd_type = CMD_SCSI;
4381edd16368SStephen M. Cameron 	c->scsi_cmd = cmd;
4382edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;  /* unused in simple mode */
4383edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
4384f2405db8SDon Brace 	c->Header.tag = cpu_to_le64((c->cmdindex << DIRECT_LOOKUP_SHIFT));
4385edd16368SStephen M. Cameron 
4386edd16368SStephen M. Cameron 	/* Fill in the request block... */
4387edd16368SStephen M. Cameron 
4388edd16368SStephen M. Cameron 	c->Request.Timeout = 0;
4389edd16368SStephen M. Cameron 	BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
4390edd16368SStephen M. Cameron 	c->Request.CDBLen = cmd->cmd_len;
4391edd16368SStephen M. Cameron 	memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
4392edd16368SStephen M. Cameron 	switch (cmd->sc_data_direction) {
4393edd16368SStephen M. Cameron 	case DMA_TO_DEVICE:
4394a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4395a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_WRITE);
4396edd16368SStephen M. Cameron 		break;
4397edd16368SStephen M. Cameron 	case DMA_FROM_DEVICE:
4398a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4399a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_READ);
4400edd16368SStephen M. Cameron 		break;
4401edd16368SStephen M. Cameron 	case DMA_NONE:
4402a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4403a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_NONE);
4404edd16368SStephen M. Cameron 		break;
4405edd16368SStephen M. Cameron 	case DMA_BIDIRECTIONAL:
4406edd16368SStephen M. Cameron 		/* This can happen if a buggy application does a scsi passthru
4407edd16368SStephen M. Cameron 		 * and sets both inlen and outlen to non-zero. ( see
4408edd16368SStephen M. Cameron 		 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
4409edd16368SStephen M. Cameron 		 */
4410edd16368SStephen M. Cameron 
4411a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4412a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_RSVD);
4413edd16368SStephen M. Cameron 		/* This is technically wrong, and hpsa controllers should
4414edd16368SStephen M. Cameron 		 * reject it with CMD_INVALID, which is the most correct
4415edd16368SStephen M. Cameron 		 * response, but non-fibre backends appear to let it
4416edd16368SStephen M. Cameron 		 * slide by, and give the same results as if this field
4417edd16368SStephen M. Cameron 		 * were set correctly.  Either way is acceptable for
4418edd16368SStephen M. Cameron 		 * our purposes here.
4419edd16368SStephen M. Cameron 		 */
4420edd16368SStephen M. Cameron 
4421edd16368SStephen M. Cameron 		break;
4422edd16368SStephen M. Cameron 
4423edd16368SStephen M. Cameron 	default:
4424edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4425edd16368SStephen M. Cameron 			cmd->sc_data_direction);
4426edd16368SStephen M. Cameron 		BUG();
4427edd16368SStephen M. Cameron 		break;
4428edd16368SStephen M. Cameron 	}
4429edd16368SStephen M. Cameron 
443033a2ffceSStephen M. Cameron 	if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
4431edd16368SStephen M. Cameron 		cmd_free(h, c);
4432edd16368SStephen M. Cameron 		return SCSI_MLQUEUE_HOST_BUSY;
4433edd16368SStephen M. Cameron 	}
4434edd16368SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
4435edd16368SStephen M. Cameron 	/* the cmd'll come back via intr handler in complete_scsi_command()  */
4436edd16368SStephen M. Cameron 	return 0;
4437edd16368SStephen M. Cameron }
4438edd16368SStephen M. Cameron 
4439360c73bdSStephen Cameron static void hpsa_cmd_init(struct ctlr_info *h, int index,
4440360c73bdSStephen Cameron 				struct CommandList *c)
4441360c73bdSStephen Cameron {
4442360c73bdSStephen Cameron 	dma_addr_t cmd_dma_handle, err_dma_handle;
4443360c73bdSStephen Cameron 
4444360c73bdSStephen Cameron 	/* Zero out all of commandlist except the last field, refcount */
4445360c73bdSStephen Cameron 	memset(c, 0, offsetof(struct CommandList, refcount));
4446360c73bdSStephen Cameron 	c->Header.tag = cpu_to_le64((u64) (index << DIRECT_LOOKUP_SHIFT));
4447360c73bdSStephen Cameron 	cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
4448360c73bdSStephen Cameron 	c->err_info = h->errinfo_pool + index;
4449360c73bdSStephen Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
4450360c73bdSStephen Cameron 	err_dma_handle = h->errinfo_pool_dhandle
4451360c73bdSStephen Cameron 	    + index * sizeof(*c->err_info);
4452360c73bdSStephen Cameron 	c->cmdindex = index;
4453360c73bdSStephen Cameron 	c->busaddr = (u32) cmd_dma_handle;
4454360c73bdSStephen Cameron 	c->ErrDesc.Addr = cpu_to_le64((u64) err_dma_handle);
4455360c73bdSStephen Cameron 	c->ErrDesc.Len = cpu_to_le32((u32) sizeof(*c->err_info));
4456360c73bdSStephen Cameron 	c->h = h;
4457360c73bdSStephen Cameron }
4458360c73bdSStephen Cameron 
4459360c73bdSStephen Cameron static void hpsa_preinitialize_commands(struct ctlr_info *h)
4460360c73bdSStephen Cameron {
4461360c73bdSStephen Cameron 	int i;
4462360c73bdSStephen Cameron 
4463360c73bdSStephen Cameron 	for (i = 0; i < h->nr_cmds; i++) {
4464360c73bdSStephen Cameron 		struct CommandList *c = h->cmd_pool + i;
4465360c73bdSStephen Cameron 
4466360c73bdSStephen Cameron 		hpsa_cmd_init(h, i, c);
4467360c73bdSStephen Cameron 		atomic_set(&c->refcount, 0);
4468360c73bdSStephen Cameron 	}
4469360c73bdSStephen Cameron }
4470360c73bdSStephen Cameron 
4471360c73bdSStephen Cameron static inline void hpsa_cmd_partial_init(struct ctlr_info *h, int index,
4472360c73bdSStephen Cameron 				struct CommandList *c)
4473360c73bdSStephen Cameron {
4474360c73bdSStephen Cameron 	dma_addr_t cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
4475360c73bdSStephen Cameron 
4476360c73bdSStephen Cameron 	memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
4477360c73bdSStephen Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
4478360c73bdSStephen Cameron 	c->busaddr = (u32) cmd_dma_handle;
4479360c73bdSStephen Cameron }
4480360c73bdSStephen Cameron 
4481592a0ad5SWebb Scales static int hpsa_ioaccel_submit(struct ctlr_info *h,
4482592a0ad5SWebb Scales 		struct CommandList *c, struct scsi_cmnd *cmd,
4483592a0ad5SWebb Scales 		unsigned char *scsi3addr)
4484592a0ad5SWebb Scales {
4485592a0ad5SWebb Scales 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4486592a0ad5SWebb Scales 	int rc = IO_ACCEL_INELIGIBLE;
4487592a0ad5SWebb Scales 
4488592a0ad5SWebb Scales 	cmd->host_scribble = (unsigned char *) c;
4489592a0ad5SWebb Scales 
4490592a0ad5SWebb Scales 	if (dev->offload_enabled) {
4491592a0ad5SWebb Scales 		hpsa_cmd_init(h, c->cmdindex, c);
4492592a0ad5SWebb Scales 		c->cmd_type = CMD_SCSI;
4493592a0ad5SWebb Scales 		c->scsi_cmd = cmd;
4494592a0ad5SWebb Scales 		rc = hpsa_scsi_ioaccel_raid_map(h, c);
4495592a0ad5SWebb Scales 		if (rc < 0)     /* scsi_dma_map failed. */
4496592a0ad5SWebb Scales 			rc = SCSI_MLQUEUE_HOST_BUSY;
4497a3144e0bSJoe Handzik 	} else if (dev->hba_ioaccel_enabled) {
4498592a0ad5SWebb Scales 		hpsa_cmd_init(h, c->cmdindex, c);
4499592a0ad5SWebb Scales 		c->cmd_type = CMD_SCSI;
4500592a0ad5SWebb Scales 		c->scsi_cmd = cmd;
4501592a0ad5SWebb Scales 		rc = hpsa_scsi_ioaccel_direct_map(h, c);
4502592a0ad5SWebb Scales 		if (rc < 0)     /* scsi_dma_map failed. */
4503592a0ad5SWebb Scales 			rc = SCSI_MLQUEUE_HOST_BUSY;
4504592a0ad5SWebb Scales 	}
4505592a0ad5SWebb Scales 	return rc;
4506592a0ad5SWebb Scales }
4507592a0ad5SWebb Scales 
4508080ef1ccSDon Brace static void hpsa_command_resubmit_worker(struct work_struct *work)
4509080ef1ccSDon Brace {
4510080ef1ccSDon Brace 	struct scsi_cmnd *cmd;
4511080ef1ccSDon Brace 	struct hpsa_scsi_dev_t *dev;
4512080ef1ccSDon Brace 	struct CommandList *c =
4513080ef1ccSDon Brace 			container_of(work, struct CommandList, work);
4514080ef1ccSDon Brace 
4515080ef1ccSDon Brace 	cmd = c->scsi_cmd;
4516080ef1ccSDon Brace 	dev = cmd->device->hostdata;
4517080ef1ccSDon Brace 	if (!dev) {
4518080ef1ccSDon Brace 		cmd->result = DID_NO_CONNECT << 16;
4519592a0ad5SWebb Scales 		cmd_free(c->h, c);
4520080ef1ccSDon Brace 		cmd->scsi_done(cmd);
4521080ef1ccSDon Brace 		return;
4522080ef1ccSDon Brace 	}
4523592a0ad5SWebb Scales 	if (c->cmd_type == CMD_IOACCEL2) {
4524592a0ad5SWebb Scales 		struct ctlr_info *h = c->h;
4525592a0ad5SWebb Scales 		struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
4526592a0ad5SWebb Scales 		int rc;
4527592a0ad5SWebb Scales 
4528592a0ad5SWebb Scales 		if (c2->error_data.serv_response ==
4529592a0ad5SWebb Scales 				IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL) {
4530592a0ad5SWebb Scales 			rc = hpsa_ioaccel_submit(h, c, cmd, dev->scsi3addr);
4531592a0ad5SWebb Scales 			if (rc == 0)
4532592a0ad5SWebb Scales 				return;
4533592a0ad5SWebb Scales 			if (rc == SCSI_MLQUEUE_HOST_BUSY) {
4534592a0ad5SWebb Scales 				/*
4535592a0ad5SWebb Scales 				 * If we get here, it means dma mapping failed.
4536592a0ad5SWebb Scales 				 * Try again via scsi mid layer, which will
4537592a0ad5SWebb Scales 				 * then get SCSI_MLQUEUE_HOST_BUSY.
4538592a0ad5SWebb Scales 				 */
4539592a0ad5SWebb Scales 				cmd->result = DID_IMM_RETRY << 16;
4540592a0ad5SWebb Scales 				cmd->scsi_done(cmd);
4541592a0ad5SWebb Scales 				cmd_free(h, c);	/* FIX-ME:  on merge, change
4542592a0ad5SWebb Scales 						 * to cmd_tagged_free() and
4543592a0ad5SWebb Scales 						 * ultimately to
4544592a0ad5SWebb Scales 						 * hpsa_cmd_free_and_done(). */
4545592a0ad5SWebb Scales 				return;
4546592a0ad5SWebb Scales 			}
4547592a0ad5SWebb Scales 			/* else, fall thru and resubmit down CISS path */
4548592a0ad5SWebb Scales 		}
4549592a0ad5SWebb Scales 	}
4550360c73bdSStephen Cameron 	hpsa_cmd_partial_init(c->h, c->cmdindex, c);
4551080ef1ccSDon Brace 	if (hpsa_ciss_submit(c->h, c, cmd, dev->scsi3addr)) {
4552080ef1ccSDon Brace 		/*
4553080ef1ccSDon Brace 		 * If we get here, it means dma mapping failed. Try
4554080ef1ccSDon Brace 		 * again via scsi mid layer, which will then get
4555080ef1ccSDon Brace 		 * SCSI_MLQUEUE_HOST_BUSY.
4556592a0ad5SWebb Scales 		 *
4557592a0ad5SWebb Scales 		 * hpsa_ciss_submit will have already freed c
4558592a0ad5SWebb Scales 		 * if it encountered a dma mapping failure.
4559080ef1ccSDon Brace 		 */
4560080ef1ccSDon Brace 		cmd->result = DID_IMM_RETRY << 16;
4561080ef1ccSDon Brace 		cmd->scsi_done(cmd);
4562080ef1ccSDon Brace 	}
4563080ef1ccSDon Brace }
4564080ef1ccSDon Brace 
4565574f05d3SStephen Cameron /* Running in struct Scsi_Host->host_lock less mode */
4566574f05d3SStephen Cameron static int hpsa_scsi_queue_command(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
4567574f05d3SStephen Cameron {
4568574f05d3SStephen Cameron 	struct ctlr_info *h;
4569574f05d3SStephen Cameron 	struct hpsa_scsi_dev_t *dev;
4570574f05d3SStephen Cameron 	unsigned char scsi3addr[8];
4571574f05d3SStephen Cameron 	struct CommandList *c;
4572574f05d3SStephen Cameron 	int rc = 0;
4573574f05d3SStephen Cameron 
4574574f05d3SStephen Cameron 	/* Get the ptr to our adapter structure out of cmd->host. */
4575574f05d3SStephen Cameron 	h = sdev_to_hba(cmd->device);
4576574f05d3SStephen Cameron 	dev = cmd->device->hostdata;
4577574f05d3SStephen Cameron 	if (!dev) {
4578574f05d3SStephen Cameron 		cmd->result = DID_NO_CONNECT << 16;
4579574f05d3SStephen Cameron 		cmd->scsi_done(cmd);
4580574f05d3SStephen Cameron 		return 0;
4581574f05d3SStephen Cameron 	}
4582574f05d3SStephen Cameron 	memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
4583574f05d3SStephen Cameron 
4584574f05d3SStephen Cameron 	if (unlikely(lockup_detected(h))) {
458525163bd5SWebb Scales 		cmd->result = DID_NO_CONNECT << 16;
4586574f05d3SStephen Cameron 		cmd->scsi_done(cmd);
4587574f05d3SStephen Cameron 		return 0;
4588574f05d3SStephen Cameron 	}
4589574f05d3SStephen Cameron 	c = cmd_alloc(h);
4590bf43caf3SRobert Elliott 
4591407863cbSStephen Cameron 	if (unlikely(lockup_detected(h))) {
459225163bd5SWebb Scales 		cmd->result = DID_NO_CONNECT << 16;
4593407863cbSStephen Cameron 		cmd_free(h, c);
4594407863cbSStephen Cameron 		cmd->scsi_done(cmd);
4595407863cbSStephen Cameron 		return 0;
4596407863cbSStephen Cameron 	}
4597574f05d3SStephen Cameron 
4598407863cbSStephen Cameron 	/*
4599407863cbSStephen Cameron 	 * Call alternate submit routine for I/O accelerated commands.
4600574f05d3SStephen Cameron 	 * Retries always go down the normal I/O path.
4601574f05d3SStephen Cameron 	 */
4602574f05d3SStephen Cameron 	if (likely(cmd->retries == 0 &&
4603574f05d3SStephen Cameron 		cmd->request->cmd_type == REQ_TYPE_FS &&
4604574f05d3SStephen Cameron 		h->acciopath_status)) {
4605592a0ad5SWebb Scales 		rc = hpsa_ioaccel_submit(h, c, cmd, scsi3addr);
4606574f05d3SStephen Cameron 		if (rc == 0)
4607592a0ad5SWebb Scales 			return 0;
4608592a0ad5SWebb Scales 		if (rc == SCSI_MLQUEUE_HOST_BUSY) {
4609592a0ad5SWebb Scales 			cmd_free(h, c);	/* FIX-ME:  on merge, change to
4610592a0ad5SWebb Scales 					 * cmd_tagged_free(), and ultimately
4611592a0ad5SWebb Scales 					 * to hpsa_cmd_resolve_and_free(). */
4612574f05d3SStephen Cameron 			return SCSI_MLQUEUE_HOST_BUSY;
4613574f05d3SStephen Cameron 		}
4614574f05d3SStephen Cameron 	}
4615574f05d3SStephen Cameron 	return hpsa_ciss_submit(h, c, cmd, scsi3addr);
4616574f05d3SStephen Cameron }
4617574f05d3SStephen Cameron 
46188ebc9248SWebb Scales static void hpsa_scan_complete(struct ctlr_info *h)
46195f389360SStephen M. Cameron {
46205f389360SStephen M. Cameron 	unsigned long flags;
46215f389360SStephen M. Cameron 
46225f389360SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
46235f389360SStephen M. Cameron 	h->scan_finished = 1;
46245f389360SStephen M. Cameron 	wake_up_all(&h->scan_wait_queue);
46255f389360SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
46265f389360SStephen M. Cameron }
46275f389360SStephen M. Cameron 
4628a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *sh)
4629a08a8471SStephen M. Cameron {
4630a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
4631a08a8471SStephen M. Cameron 	unsigned long flags;
4632a08a8471SStephen M. Cameron 
46338ebc9248SWebb Scales 	/*
46348ebc9248SWebb Scales 	 * Don't let rescans be initiated on a controller known to be locked
46358ebc9248SWebb Scales 	 * up.  If the controller locks up *during* a rescan, that thread is
46368ebc9248SWebb Scales 	 * probably hosed, but at least we can prevent new rescan threads from
46378ebc9248SWebb Scales 	 * piling up on a locked up controller.
46388ebc9248SWebb Scales 	 */
46398ebc9248SWebb Scales 	if (unlikely(lockup_detected(h)))
46408ebc9248SWebb Scales 		return hpsa_scan_complete(h);
46415f389360SStephen M. Cameron 
4642a08a8471SStephen M. Cameron 	/* wait until any scan already in progress is finished. */
4643a08a8471SStephen M. Cameron 	while (1) {
4644a08a8471SStephen M. Cameron 		spin_lock_irqsave(&h->scan_lock, flags);
4645a08a8471SStephen M. Cameron 		if (h->scan_finished)
4646a08a8471SStephen M. Cameron 			break;
4647a08a8471SStephen M. Cameron 		spin_unlock_irqrestore(&h->scan_lock, flags);
4648a08a8471SStephen M. Cameron 		wait_event(h->scan_wait_queue, h->scan_finished);
4649a08a8471SStephen M. Cameron 		/* Note: We don't need to worry about a race between this
4650a08a8471SStephen M. Cameron 		 * thread and driver unload because the midlayer will
4651a08a8471SStephen M. Cameron 		 * have incremented the reference count, so unload won't
4652a08a8471SStephen M. Cameron 		 * happen if we're in here.
4653a08a8471SStephen M. Cameron 		 */
4654a08a8471SStephen M. Cameron 	}
4655a08a8471SStephen M. Cameron 	h->scan_finished = 0; /* mark scan as in progress */
4656a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4657a08a8471SStephen M. Cameron 
46588ebc9248SWebb Scales 	if (unlikely(lockup_detected(h)))
46598ebc9248SWebb Scales 		return hpsa_scan_complete(h);
46605f389360SStephen M. Cameron 
4661a08a8471SStephen M. Cameron 	hpsa_update_scsi_devices(h, h->scsi_host->host_no);
4662a08a8471SStephen M. Cameron 
46638ebc9248SWebb Scales 	hpsa_scan_complete(h);
4664a08a8471SStephen M. Cameron }
4665a08a8471SStephen M. Cameron 
46667c0a0229SDon Brace static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth)
46677c0a0229SDon Brace {
466803383736SDon Brace 	struct hpsa_scsi_dev_t *logical_drive = sdev->hostdata;
466903383736SDon Brace 
467003383736SDon Brace 	if (!logical_drive)
467103383736SDon Brace 		return -ENODEV;
46727c0a0229SDon Brace 
46737c0a0229SDon Brace 	if (qdepth < 1)
46747c0a0229SDon Brace 		qdepth = 1;
467503383736SDon Brace 	else if (qdepth > logical_drive->queue_depth)
467603383736SDon Brace 		qdepth = logical_drive->queue_depth;
467703383736SDon Brace 
467803383736SDon Brace 	return scsi_change_queue_depth(sdev, qdepth);
46797c0a0229SDon Brace }
46807c0a0229SDon Brace 
4681a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
4682a08a8471SStephen M. Cameron 	unsigned long elapsed_time)
4683a08a8471SStephen M. Cameron {
4684a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
4685a08a8471SStephen M. Cameron 	unsigned long flags;
4686a08a8471SStephen M. Cameron 	int finished;
4687a08a8471SStephen M. Cameron 
4688a08a8471SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
4689a08a8471SStephen M. Cameron 	finished = h->scan_finished;
4690a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4691a08a8471SStephen M. Cameron 	return finished;
4692a08a8471SStephen M. Cameron }
4693a08a8471SStephen M. Cameron 
4694edd16368SStephen M. Cameron static void hpsa_unregister_scsi(struct ctlr_info *h)
4695edd16368SStephen M. Cameron {
4696edd16368SStephen M. Cameron 	/* we are being forcibly unloaded, and may not refuse. */
4697edd16368SStephen M. Cameron 	scsi_remove_host(h->scsi_host);
4698edd16368SStephen M. Cameron 	scsi_host_put(h->scsi_host);
4699edd16368SStephen M. Cameron 	h->scsi_host = NULL;
4700edd16368SStephen M. Cameron }
4701edd16368SStephen M. Cameron 
4702edd16368SStephen M. Cameron static int hpsa_register_scsi(struct ctlr_info *h)
4703edd16368SStephen M. Cameron {
4704b705690dSStephen M. Cameron 	struct Scsi_Host *sh;
4705b705690dSStephen M. Cameron 	int error;
4706edd16368SStephen M. Cameron 
4707b705690dSStephen M. Cameron 	sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
4708b705690dSStephen M. Cameron 	if (sh == NULL)
4709b705690dSStephen M. Cameron 		goto fail;
4710b705690dSStephen M. Cameron 
4711b705690dSStephen M. Cameron 	sh->io_port = 0;
4712b705690dSStephen M. Cameron 	sh->n_io_port = 0;
4713b705690dSStephen M. Cameron 	sh->this_id = -1;
4714b705690dSStephen M. Cameron 	sh->max_channel = 3;
4715b705690dSStephen M. Cameron 	sh->max_cmd_len = MAX_COMMAND_SIZE;
4716b705690dSStephen M. Cameron 	sh->max_lun = HPSA_MAX_LUN;
4717b705690dSStephen M. Cameron 	sh->max_id = HPSA_MAX_LUN;
471841ce4c35SStephen Cameron 	sh->can_queue = h->nr_cmds - HPSA_NRESERVED_CMDS;
4719d54c5c24SStephen Cameron 	sh->cmd_per_lun = sh->can_queue;
4720b705690dSStephen M. Cameron 	sh->sg_tablesize = h->maxsgentries;
4721b705690dSStephen M. Cameron 	h->scsi_host = sh;
4722b705690dSStephen M. Cameron 	sh->hostdata[0] = (unsigned long) h;
4723b705690dSStephen M. Cameron 	sh->irq = h->intr[h->intr_mode];
4724b705690dSStephen M. Cameron 	sh->unique_id = sh->irq;
4725b705690dSStephen M. Cameron 	error = scsi_add_host(sh, &h->pdev->dev);
4726b705690dSStephen M. Cameron 	if (error)
4727b705690dSStephen M. Cameron 		goto fail_host_put;
4728b705690dSStephen M. Cameron 	scsi_scan_host(sh);
4729b705690dSStephen M. Cameron 	return 0;
4730b705690dSStephen M. Cameron 
4731b705690dSStephen M. Cameron  fail_host_put:
4732b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_add_host"
4733b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
4734b705690dSStephen M. Cameron 	scsi_host_put(sh);
4735b705690dSStephen M. Cameron 	return error;
4736b705690dSStephen M. Cameron  fail:
4737b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_host_alloc"
4738b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
4739b705690dSStephen M. Cameron 	return -ENOMEM;
4740edd16368SStephen M. Cameron }
4741edd16368SStephen M. Cameron 
4742edd16368SStephen M. Cameron static int wait_for_device_to_become_ready(struct ctlr_info *h,
4743edd16368SStephen M. Cameron 	unsigned char lunaddr[])
4744edd16368SStephen M. Cameron {
47458919358eSTomas Henzl 	int rc;
4746edd16368SStephen M. Cameron 	int count = 0;
4747edd16368SStephen M. Cameron 	int waittime = 1; /* seconds */
4748edd16368SStephen M. Cameron 	struct CommandList *c;
4749edd16368SStephen M. Cameron 
475045fcb86eSStephen Cameron 	c = cmd_alloc(h);
4751edd16368SStephen M. Cameron 
4752edd16368SStephen M. Cameron 	/* Send test unit ready until device ready, or give up. */
4753edd16368SStephen M. Cameron 	while (count < HPSA_TUR_RETRY_LIMIT) {
4754edd16368SStephen M. Cameron 
4755edd16368SStephen M. Cameron 		/* Wait for a bit.  do this first, because if we send
4756edd16368SStephen M. Cameron 		 * the TUR right away, the reset will just abort it.
4757edd16368SStephen M. Cameron 		 */
4758edd16368SStephen M. Cameron 		msleep(1000 * waittime);
4759edd16368SStephen M. Cameron 		count++;
47608919358eSTomas Henzl 		rc = 0; /* Device ready. */
4761edd16368SStephen M. Cameron 
4762edd16368SStephen M. Cameron 		/* Increase wait time with each try, up to a point. */
4763edd16368SStephen M. Cameron 		if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
4764edd16368SStephen M. Cameron 			waittime = waittime * 2;
4765edd16368SStephen M. Cameron 
4766a2dac136SStephen M. Cameron 		/* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
4767a2dac136SStephen M. Cameron 		(void) fill_cmd(c, TEST_UNIT_READY, h,
4768a2dac136SStephen M. Cameron 				NULL, 0, 0, lunaddr, TYPE_CMD);
476925163bd5SWebb Scales 		rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
477025163bd5SWebb Scales 						NO_TIMEOUT);
477125163bd5SWebb Scales 		if (rc)
477225163bd5SWebb Scales 			goto do_it_again;
4773edd16368SStephen M. Cameron 		/* no unmap needed here because no data xfer. */
4774edd16368SStephen M. Cameron 
4775edd16368SStephen M. Cameron 		if (c->err_info->CommandStatus == CMD_SUCCESS)
4776edd16368SStephen M. Cameron 			break;
4777edd16368SStephen M. Cameron 
4778edd16368SStephen M. Cameron 		if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
4779edd16368SStephen M. Cameron 			c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
4780edd16368SStephen M. Cameron 			(c->err_info->SenseInfo[2] == NO_SENSE ||
4781edd16368SStephen M. Cameron 			c->err_info->SenseInfo[2] == UNIT_ATTENTION))
4782edd16368SStephen M. Cameron 			break;
478325163bd5SWebb Scales do_it_again:
4784edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "waiting %d secs "
4785edd16368SStephen M. Cameron 			"for device to become ready.\n", waittime);
4786edd16368SStephen M. Cameron 		rc = 1; /* device not ready. */
4787edd16368SStephen M. Cameron 	}
4788edd16368SStephen M. Cameron 
4789edd16368SStephen M. Cameron 	if (rc)
4790edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "giving up on device.\n");
4791edd16368SStephen M. Cameron 	else
4792edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "device is ready.\n");
4793edd16368SStephen M. Cameron 
479445fcb86eSStephen Cameron 	cmd_free(h, c);
4795edd16368SStephen M. Cameron 	return rc;
4796edd16368SStephen M. Cameron }
4797edd16368SStephen M. Cameron 
4798edd16368SStephen M. Cameron /* Need at least one of these error handlers to keep ../scsi/hosts.c from
4799edd16368SStephen M. Cameron  * complaining.  Doing a host- or bus-reset can't do anything good here.
4800edd16368SStephen M. Cameron  */
4801edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
4802edd16368SStephen M. Cameron {
4803edd16368SStephen M. Cameron 	int rc;
4804edd16368SStephen M. Cameron 	struct ctlr_info *h;
4805edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
4806edd16368SStephen M. Cameron 
4807edd16368SStephen M. Cameron 	/* find the controller to which the command to be aborted was sent */
4808edd16368SStephen M. Cameron 	h = sdev_to_hba(scsicmd->device);
4809edd16368SStephen M. Cameron 	if (h == NULL) /* paranoia */
4810edd16368SStephen M. Cameron 		return FAILED;
4811e345893bSDon Brace 
4812e345893bSDon Brace 	if (lockup_detected(h))
4813e345893bSDon Brace 		return FAILED;
4814e345893bSDon Brace 
4815edd16368SStephen M. Cameron 	dev = scsicmd->device->hostdata;
4816edd16368SStephen M. Cameron 	if (!dev) {
4817edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
4818edd16368SStephen M. Cameron 			"device lookup failed.\n");
4819edd16368SStephen M. Cameron 		return FAILED;
4820edd16368SStephen M. Cameron 	}
482125163bd5SWebb Scales 
482225163bd5SWebb Scales 	/* if controller locked up, we can guarantee command won't complete */
482325163bd5SWebb Scales 	if (lockup_detected(h)) {
482425163bd5SWebb Scales 		dev_warn(&h->pdev->dev,
482525163bd5SWebb Scales 			"scsi %d:%d:%d:%d RESET FAILED, lockup detected\n",
482625163bd5SWebb Scales 			h->scsi_host->host_no, dev->bus, dev->target,
482725163bd5SWebb Scales 			dev->lun);
482825163bd5SWebb Scales 		return FAILED;
482925163bd5SWebb Scales 	}
483025163bd5SWebb Scales 
483125163bd5SWebb Scales 	/* this reset request might be the result of a lockup; check */
483225163bd5SWebb Scales 	if (detect_controller_lockup(h)) {
483325163bd5SWebb Scales 		dev_warn(&h->pdev->dev,
483425163bd5SWebb Scales 			 "scsi %d:%d:%d:%d RESET FAILED, new lockup detected\n",
483525163bd5SWebb Scales 			 h->scsi_host->host_no, dev->bus, dev->target,
483625163bd5SWebb Scales 			 dev->lun);
483725163bd5SWebb Scales 		return FAILED;
483825163bd5SWebb Scales 	}
483925163bd5SWebb Scales 
484025163bd5SWebb Scales 	hpsa_show_dev_msg(KERN_WARNING, h, dev, "resetting");
484125163bd5SWebb Scales 
4842edd16368SStephen M. Cameron 	/* send a reset to the SCSI LUN which the command was sent to */
484325163bd5SWebb Scales 	rc = hpsa_send_reset(h, dev->scsi3addr, HPSA_RESET_TYPE_LUN,
484425163bd5SWebb Scales 			     DEFAULT_REPLY_QUEUE);
4845edd16368SStephen M. Cameron 	if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
4846edd16368SStephen M. Cameron 		return SUCCESS;
4847edd16368SStephen M. Cameron 
484825163bd5SWebb Scales 	dev_warn(&h->pdev->dev,
484925163bd5SWebb Scales 		"scsi %d:%d:%d:%d reset failed\n",
485025163bd5SWebb Scales 		h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
4851edd16368SStephen M. Cameron 	return FAILED;
4852edd16368SStephen M. Cameron }
4853edd16368SStephen M. Cameron 
48546cba3f19SStephen M. Cameron static void swizzle_abort_tag(u8 *tag)
48556cba3f19SStephen M. Cameron {
48566cba3f19SStephen M. Cameron 	u8 original_tag[8];
48576cba3f19SStephen M. Cameron 
48586cba3f19SStephen M. Cameron 	memcpy(original_tag, tag, 8);
48596cba3f19SStephen M. Cameron 	tag[0] = original_tag[3];
48606cba3f19SStephen M. Cameron 	tag[1] = original_tag[2];
48616cba3f19SStephen M. Cameron 	tag[2] = original_tag[1];
48626cba3f19SStephen M. Cameron 	tag[3] = original_tag[0];
48636cba3f19SStephen M. Cameron 	tag[4] = original_tag[7];
48646cba3f19SStephen M. Cameron 	tag[5] = original_tag[6];
48656cba3f19SStephen M. Cameron 	tag[6] = original_tag[5];
48666cba3f19SStephen M. Cameron 	tag[7] = original_tag[4];
48676cba3f19SStephen M. Cameron }
48686cba3f19SStephen M. Cameron 
486917eb87d2SScott Teel static void hpsa_get_tag(struct ctlr_info *h,
48702b08b3e9SDon Brace 	struct CommandList *c, __le32 *taglower, __le32 *tagupper)
487117eb87d2SScott Teel {
48722b08b3e9SDon Brace 	u64 tag;
487317eb87d2SScott Teel 	if (c->cmd_type == CMD_IOACCEL1) {
487417eb87d2SScott Teel 		struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
487517eb87d2SScott Teel 			&h->ioaccel_cmd_pool[c->cmdindex];
48762b08b3e9SDon Brace 		tag = le64_to_cpu(cm1->tag);
48772b08b3e9SDon Brace 		*tagupper = cpu_to_le32(tag >> 32);
48782b08b3e9SDon Brace 		*taglower = cpu_to_le32(tag);
487954b6e9e9SScott Teel 		return;
488054b6e9e9SScott Teel 	}
488154b6e9e9SScott Teel 	if (c->cmd_type == CMD_IOACCEL2) {
488254b6e9e9SScott Teel 		struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
488354b6e9e9SScott Teel 			&h->ioaccel2_cmd_pool[c->cmdindex];
4884dd0e19f3SScott Teel 		/* upper tag not used in ioaccel2 mode */
4885dd0e19f3SScott Teel 		memset(tagupper, 0, sizeof(*tagupper));
4886dd0e19f3SScott Teel 		*taglower = cm2->Tag;
488754b6e9e9SScott Teel 		return;
488854b6e9e9SScott Teel 	}
48892b08b3e9SDon Brace 	tag = le64_to_cpu(c->Header.tag);
48902b08b3e9SDon Brace 	*tagupper = cpu_to_le32(tag >> 32);
48912b08b3e9SDon Brace 	*taglower = cpu_to_le32(tag);
489217eb87d2SScott Teel }
489354b6e9e9SScott Teel 
489475167d2cSStephen M. Cameron static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
48959b5c48c2SStephen Cameron 	struct CommandList *abort, int reply_queue)
489675167d2cSStephen M. Cameron {
489775167d2cSStephen M. Cameron 	int rc = IO_OK;
489875167d2cSStephen M. Cameron 	struct CommandList *c;
489975167d2cSStephen M. Cameron 	struct ErrorInfo *ei;
49002b08b3e9SDon Brace 	__le32 tagupper, taglower;
490175167d2cSStephen M. Cameron 
490245fcb86eSStephen Cameron 	c = cmd_alloc(h);
490375167d2cSStephen M. Cameron 
4904a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no buffer to map */
49059b5c48c2SStephen Cameron 	(void) fill_cmd(c, HPSA_ABORT_MSG, h, &abort->Header.tag,
4906a2dac136SStephen M. Cameron 		0, 0, scsi3addr, TYPE_MSG);
49079b5c48c2SStephen Cameron 	if (h->needs_abort_tags_swizzled)
49086cba3f19SStephen M. Cameron 		swizzle_abort_tag(&c->Request.CDB[4]);
490925163bd5SWebb Scales 	(void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
491017eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
491125163bd5SWebb Scales 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd(abort) completed.\n",
491217eb87d2SScott Teel 		__func__, tagupper, taglower);
491375167d2cSStephen M. Cameron 	/* no unmap needed here because no data xfer. */
491475167d2cSStephen M. Cameron 
491575167d2cSStephen M. Cameron 	ei = c->err_info;
491675167d2cSStephen M. Cameron 	switch (ei->CommandStatus) {
491775167d2cSStephen M. Cameron 	case CMD_SUCCESS:
491875167d2cSStephen M. Cameron 		break;
49199437ac43SStephen Cameron 	case CMD_TMF_STATUS:
49209437ac43SStephen Cameron 		rc = hpsa_evaluate_tmf_status(h, c);
49219437ac43SStephen Cameron 		break;
492275167d2cSStephen M. Cameron 	case CMD_UNABORTABLE: /* Very common, don't make noise. */
492375167d2cSStephen M. Cameron 		rc = -1;
492475167d2cSStephen M. Cameron 		break;
492575167d2cSStephen M. Cameron 	default:
492675167d2cSStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
492717eb87d2SScott Teel 			__func__, tagupper, taglower);
4928d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
492975167d2cSStephen M. Cameron 		rc = -1;
493075167d2cSStephen M. Cameron 		break;
493175167d2cSStephen M. Cameron 	}
493245fcb86eSStephen Cameron 	cmd_free(h, c);
4933dd0e19f3SScott Teel 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n",
4934dd0e19f3SScott Teel 		__func__, tagupper, taglower);
493575167d2cSStephen M. Cameron 	return rc;
493675167d2cSStephen M. Cameron }
493775167d2cSStephen M. Cameron 
4938*8be986ccSStephen Cameron static void setup_ioaccel2_abort_cmd(struct CommandList *c, struct ctlr_info *h,
4939*8be986ccSStephen Cameron 	struct CommandList *command_to_abort, int reply_queue)
4940*8be986ccSStephen Cameron {
4941*8be986ccSStephen Cameron 	struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
4942*8be986ccSStephen Cameron 	struct hpsa_tmf_struct *ac = (struct hpsa_tmf_struct *) c2;
4943*8be986ccSStephen Cameron 	struct io_accel2_cmd *c2a =
4944*8be986ccSStephen Cameron 		&h->ioaccel2_cmd_pool[command_to_abort->cmdindex];
4945*8be986ccSStephen Cameron 	struct scsi_cmnd *scmd =
4946*8be986ccSStephen Cameron 		(struct scsi_cmnd *) command_to_abort->scsi_cmd;
4947*8be986ccSStephen Cameron 	struct hpsa_scsi_dev_t *dev = scmd->device->hostdata;
4948*8be986ccSStephen Cameron 
4949*8be986ccSStephen Cameron 	/*
4950*8be986ccSStephen Cameron 	 * We're overlaying struct hpsa_tmf_struct on top of something which
4951*8be986ccSStephen Cameron 	 * was allocated as a struct io_accel2_cmd, so we better be sure it
4952*8be986ccSStephen Cameron 	 * actually fits, and doesn't overrun the error info space.
4953*8be986ccSStephen Cameron 	 */
4954*8be986ccSStephen Cameron 	BUILD_BUG_ON(sizeof(struct hpsa_tmf_struct) >
4955*8be986ccSStephen Cameron 			sizeof(struct io_accel2_cmd));
4956*8be986ccSStephen Cameron 	BUG_ON(offsetof(struct io_accel2_cmd, error_data) <
4957*8be986ccSStephen Cameron 			offsetof(struct hpsa_tmf_struct, error_len) +
4958*8be986ccSStephen Cameron 				sizeof(ac->error_len));
4959*8be986ccSStephen Cameron 
4960*8be986ccSStephen Cameron 	c->cmd_type = IOACCEL2_TMF;
4961*8be986ccSStephen Cameron 	/* Adjust the DMA address to point to the accelerated command buffer */
4962*8be986ccSStephen Cameron 	c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
4963*8be986ccSStephen Cameron 				(c->cmdindex * sizeof(struct io_accel2_cmd));
4964*8be986ccSStephen Cameron 	BUG_ON(c->busaddr & 0x0000007F);
4965*8be986ccSStephen Cameron 
4966*8be986ccSStephen Cameron 	memset(ac, 0, sizeof(*c2)); /* yes this is correct */
4967*8be986ccSStephen Cameron 	ac->iu_type = IOACCEL2_IU_TMF_TYPE;
4968*8be986ccSStephen Cameron 	ac->reply_queue = reply_queue;
4969*8be986ccSStephen Cameron 	ac->tmf = IOACCEL2_TMF_ABORT;
4970*8be986ccSStephen Cameron 	ac->it_nexus = cpu_to_le32(dev->ioaccel_handle);
4971*8be986ccSStephen Cameron 	memset(ac->lun_id, 0, sizeof(ac->lun_id));
4972*8be986ccSStephen Cameron 	ac->tag = cpu_to_le64(c->cmdindex << DIRECT_LOOKUP_SHIFT);
4973*8be986ccSStephen Cameron 	ac->abort_tag = cpu_to_le64(le32_to_cpu(c2a->Tag));
4974*8be986ccSStephen Cameron 	ac->error_ptr = cpu_to_le64(c->busaddr +
4975*8be986ccSStephen Cameron 			offsetof(struct io_accel2_cmd, error_data));
4976*8be986ccSStephen Cameron 	ac->error_len = cpu_to_le32(sizeof(c2->error_data));
4977*8be986ccSStephen Cameron }
4978*8be986ccSStephen Cameron 
497954b6e9e9SScott Teel /* ioaccel2 path firmware cannot handle abort task requests.
498054b6e9e9SScott Teel  * Change abort requests to physical target reset, and send to the
498154b6e9e9SScott Teel  * address of the physical disk used for the ioaccel 2 command.
498254b6e9e9SScott Teel  * Return 0 on success (IO_OK)
498354b6e9e9SScott Teel  *	 -1 on failure
498454b6e9e9SScott Teel  */
498554b6e9e9SScott Teel 
498654b6e9e9SScott Teel static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
498725163bd5SWebb Scales 	unsigned char *scsi3addr, struct CommandList *abort, int reply_queue)
498854b6e9e9SScott Teel {
498954b6e9e9SScott Teel 	int rc = IO_OK;
499054b6e9e9SScott Teel 	struct scsi_cmnd *scmd; /* scsi command within request being aborted */
499154b6e9e9SScott Teel 	struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
499254b6e9e9SScott Teel 	unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
499354b6e9e9SScott Teel 	unsigned char *psa = &phys_scsi3addr[0];
499454b6e9e9SScott Teel 
499554b6e9e9SScott Teel 	/* Get a pointer to the hpsa logical device. */
49967fa3030cSStephen Cameron 	scmd = abort->scsi_cmd;
499754b6e9e9SScott Teel 	dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
499854b6e9e9SScott Teel 	if (dev == NULL) {
499954b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
500054b6e9e9SScott Teel 			"Cannot abort: no device pointer for command.\n");
500154b6e9e9SScott Teel 			return -1; /* not abortable */
500254b6e9e9SScott Teel 	}
500354b6e9e9SScott Teel 
50042ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
50052ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
50060d96ef5fSWebb Scales 			"scsi %d:%d:%d:%d %s scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
50072ba8bfc8SStephen M. Cameron 			h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
50080d96ef5fSWebb Scales 			"Reset as abort",
50092ba8bfc8SStephen M. Cameron 			scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
50102ba8bfc8SStephen M. Cameron 			scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]);
50112ba8bfc8SStephen M. Cameron 
501254b6e9e9SScott Teel 	if (!dev->offload_enabled) {
501354b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
501454b6e9e9SScott Teel 			"Can't abort: device is not operating in HP SSD Smart Path mode.\n");
501554b6e9e9SScott Teel 		return -1; /* not abortable */
501654b6e9e9SScott Teel 	}
501754b6e9e9SScott Teel 
501854b6e9e9SScott Teel 	/* Incoming scsi3addr is logical addr. We need physical disk addr. */
501954b6e9e9SScott Teel 	if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
502054b6e9e9SScott Teel 		dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
502154b6e9e9SScott Teel 		return -1; /* not abortable */
502254b6e9e9SScott Teel 	}
502354b6e9e9SScott Teel 
502454b6e9e9SScott Teel 	/* send the reset */
50252ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
50262ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
50272ba8bfc8SStephen M. Cameron 			"Reset as abort: Resetting physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
50282ba8bfc8SStephen M. Cameron 			psa[0], psa[1], psa[2], psa[3],
50292ba8bfc8SStephen M. Cameron 			psa[4], psa[5], psa[6], psa[7]);
503025163bd5SWebb Scales 	rc = hpsa_send_reset(h, psa, HPSA_RESET_TYPE_TARGET, reply_queue);
503154b6e9e9SScott Teel 	if (rc != 0) {
503254b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
503354b6e9e9SScott Teel 			"Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
503454b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
503554b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
503654b6e9e9SScott Teel 		return rc; /* failed to reset */
503754b6e9e9SScott Teel 	}
503854b6e9e9SScott Teel 
503954b6e9e9SScott Teel 	/* wait for device to recover */
504054b6e9e9SScott Teel 	if (wait_for_device_to_become_ready(h, psa) != 0) {
504154b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
504254b6e9e9SScott Teel 			"Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
504354b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
504454b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
504554b6e9e9SScott Teel 		return -1;  /* failed to recover */
504654b6e9e9SScott Teel 	}
504754b6e9e9SScott Teel 
504854b6e9e9SScott Teel 	/* device recovered */
504954b6e9e9SScott Teel 	dev_info(&h->pdev->dev,
505054b6e9e9SScott Teel 		"Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
505154b6e9e9SScott Teel 		psa[0], psa[1], psa[2], psa[3],
505254b6e9e9SScott Teel 		psa[4], psa[5], psa[6], psa[7]);
505354b6e9e9SScott Teel 
505454b6e9e9SScott Teel 	return rc; /* success */
505554b6e9e9SScott Teel }
505654b6e9e9SScott Teel 
5057*8be986ccSStephen Cameron static int hpsa_send_abort_ioaccel2(struct ctlr_info *h,
5058*8be986ccSStephen Cameron 	struct CommandList *abort, int reply_queue)
5059*8be986ccSStephen Cameron {
5060*8be986ccSStephen Cameron 	int rc = IO_OK;
5061*8be986ccSStephen Cameron 	struct CommandList *c;
5062*8be986ccSStephen Cameron 	__le32 taglower, tagupper;
5063*8be986ccSStephen Cameron 	struct hpsa_scsi_dev_t *dev;
5064*8be986ccSStephen Cameron 	struct io_accel2_cmd *c2;
5065*8be986ccSStephen Cameron 
5066*8be986ccSStephen Cameron 	dev = abort->scsi_cmd->device->hostdata;
5067*8be986ccSStephen Cameron 	if (!dev->offload_enabled && !dev->hba_ioaccel_enabled)
5068*8be986ccSStephen Cameron 		return -1;
5069*8be986ccSStephen Cameron 
5070*8be986ccSStephen Cameron 	c = cmd_alloc(h);
5071*8be986ccSStephen Cameron 	setup_ioaccel2_abort_cmd(c, h, abort, reply_queue);
5072*8be986ccSStephen Cameron 	c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
5073*8be986ccSStephen Cameron 	(void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
5074*8be986ccSStephen Cameron 	hpsa_get_tag(h, abort, &taglower, &tagupper);
5075*8be986ccSStephen Cameron 	dev_dbg(&h->pdev->dev,
5076*8be986ccSStephen Cameron 		"%s: Tag:0x%08x:%08x: do_simple_cmd(ioaccel2 abort) completed.\n",
5077*8be986ccSStephen Cameron 		__func__, tagupper, taglower);
5078*8be986ccSStephen Cameron 	/* no unmap needed here because no data xfer. */
5079*8be986ccSStephen Cameron 
5080*8be986ccSStephen Cameron 	dev_dbg(&h->pdev->dev,
5081*8be986ccSStephen Cameron 		"%s: Tag:0x%08x:%08x: abort service response = 0x%02x.\n",
5082*8be986ccSStephen Cameron 		__func__, tagupper, taglower, c2->error_data.serv_response);
5083*8be986ccSStephen Cameron 	switch (c2->error_data.serv_response) {
5084*8be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
5085*8be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
5086*8be986ccSStephen Cameron 		rc = 0;
5087*8be986ccSStephen Cameron 		break;
5088*8be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
5089*8be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_FAILURE:
5090*8be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
5091*8be986ccSStephen Cameron 		rc = -1;
5092*8be986ccSStephen Cameron 		break;
5093*8be986ccSStephen Cameron 	default:
5094*8be986ccSStephen Cameron 		dev_warn(&h->pdev->dev,
5095*8be986ccSStephen Cameron 			"%s: Tag:0x%08x:%08x: unknown abort service response 0x%02x\n",
5096*8be986ccSStephen Cameron 			__func__, tagupper, taglower,
5097*8be986ccSStephen Cameron 			c2->error_data.serv_response);
5098*8be986ccSStephen Cameron 		rc = -1;
5099*8be986ccSStephen Cameron 	}
5100*8be986ccSStephen Cameron 	cmd_free(h, c);
5101*8be986ccSStephen Cameron 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n", __func__,
5102*8be986ccSStephen Cameron 		tagupper, taglower);
5103*8be986ccSStephen Cameron 	return rc;
5104*8be986ccSStephen Cameron }
5105*8be986ccSStephen Cameron 
51066cba3f19SStephen M. Cameron static int hpsa_send_abort_both_ways(struct ctlr_info *h,
510725163bd5SWebb Scales 	unsigned char *scsi3addr, struct CommandList *abort, int reply_queue)
51086cba3f19SStephen M. Cameron {
5109*8be986ccSStephen Cameron 	/*
5110*8be986ccSStephen Cameron 	 * ioccelerator mode 2 commands should be aborted via the
511154b6e9e9SScott Teel 	 * accelerated path, since RAID path is unaware of these commands,
5112*8be986ccSStephen Cameron 	 * but not all underlying firmware can handle abort TMF.
5113*8be986ccSStephen Cameron 	 * Change abort to physical device reset when abort TMF is unsupported.
511454b6e9e9SScott Teel 	 */
5115*8be986ccSStephen Cameron 	if (abort->cmd_type == CMD_IOACCEL2) {
5116*8be986ccSStephen Cameron 		if (HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags)
5117*8be986ccSStephen Cameron 			return hpsa_send_abort_ioaccel2(h, abort,
5118*8be986ccSStephen Cameron 						reply_queue);
5119*8be986ccSStephen Cameron 		else
512025163bd5SWebb Scales 			return hpsa_send_reset_as_abort_ioaccel2(h, scsi3addr,
512125163bd5SWebb Scales 							abort, reply_queue);
5122*8be986ccSStephen Cameron 	}
51239b5c48c2SStephen Cameron 	return hpsa_send_abort(h, scsi3addr, abort, reply_queue);
512425163bd5SWebb Scales }
512525163bd5SWebb Scales 
512625163bd5SWebb Scales /* Find out which reply queue a command was meant to return on */
512725163bd5SWebb Scales static int hpsa_extract_reply_queue(struct ctlr_info *h,
512825163bd5SWebb Scales 					struct CommandList *c)
512925163bd5SWebb Scales {
513025163bd5SWebb Scales 	if (c->cmd_type == CMD_IOACCEL2)
513125163bd5SWebb Scales 		return h->ioaccel2_cmd_pool[c->cmdindex].reply_queue;
513225163bd5SWebb Scales 	return c->Header.ReplyQueue;
51336cba3f19SStephen M. Cameron }
51346cba3f19SStephen M. Cameron 
51359b5c48c2SStephen Cameron /*
51369b5c48c2SStephen Cameron  * Limit concurrency of abort commands to prevent
51379b5c48c2SStephen Cameron  * over-subscription of commands
51389b5c48c2SStephen Cameron  */
51399b5c48c2SStephen Cameron static inline int wait_for_available_abort_cmd(struct ctlr_info *h)
51409b5c48c2SStephen Cameron {
51419b5c48c2SStephen Cameron #define ABORT_CMD_WAIT_MSECS 5000
51429b5c48c2SStephen Cameron 	return !wait_event_timeout(h->abort_cmd_wait_queue,
51439b5c48c2SStephen Cameron 			atomic_dec_if_positive(&h->abort_cmds_available) >= 0,
51449b5c48c2SStephen Cameron 			msecs_to_jiffies(ABORT_CMD_WAIT_MSECS));
51459b5c48c2SStephen Cameron }
51469b5c48c2SStephen Cameron 
514775167d2cSStephen M. Cameron /* Send an abort for the specified command.
514875167d2cSStephen M. Cameron  *	If the device and controller support it,
514975167d2cSStephen M. Cameron  *		send a task abort request.
515075167d2cSStephen M. Cameron  */
515175167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
515275167d2cSStephen M. Cameron {
515375167d2cSStephen M. Cameron 
515475167d2cSStephen M. Cameron 	int i, rc;
515575167d2cSStephen M. Cameron 	struct ctlr_info *h;
515675167d2cSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
515775167d2cSStephen M. Cameron 	struct CommandList *abort; /* pointer to command to be aborted */
515875167d2cSStephen M. Cameron 	struct scsi_cmnd *as;	/* ptr to scsi cmd inside aborted command. */
515975167d2cSStephen M. Cameron 	char msg[256];		/* For debug messaging. */
516075167d2cSStephen M. Cameron 	int ml = 0;
51612b08b3e9SDon Brace 	__le32 tagupper, taglower;
516225163bd5SWebb Scales 	int refcount, reply_queue;
516325163bd5SWebb Scales 
516425163bd5SWebb Scales 	if (sc == NULL)
516525163bd5SWebb Scales 		return FAILED;
516675167d2cSStephen M. Cameron 
51679b5c48c2SStephen Cameron 	if (sc->device == NULL)
51689b5c48c2SStephen Cameron 		return FAILED;
51699b5c48c2SStephen Cameron 
517075167d2cSStephen M. Cameron 	/* Find the controller of the command to be aborted */
517175167d2cSStephen M. Cameron 	h = sdev_to_hba(sc->device);
51729b5c48c2SStephen Cameron 	if (h == NULL)
517375167d2cSStephen M. Cameron 		return FAILED;
517475167d2cSStephen M. Cameron 
517525163bd5SWebb Scales 	/* Find the device of the command to be aborted */
517625163bd5SWebb Scales 	dev = sc->device->hostdata;
517725163bd5SWebb Scales 	if (!dev) {
517825163bd5SWebb Scales 		dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
517925163bd5SWebb Scales 				msg);
5180e345893bSDon Brace 		return FAILED;
518125163bd5SWebb Scales 	}
518225163bd5SWebb Scales 
518325163bd5SWebb Scales 	/* If controller locked up, we can guarantee command won't complete */
518425163bd5SWebb Scales 	if (lockup_detected(h)) {
518525163bd5SWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev,
518625163bd5SWebb Scales 					"ABORT FAILED, lockup detected");
518725163bd5SWebb Scales 		return FAILED;
518825163bd5SWebb Scales 	}
518925163bd5SWebb Scales 
519025163bd5SWebb Scales 	/* This is a good time to check if controller lockup has occurred */
519125163bd5SWebb Scales 	if (detect_controller_lockup(h)) {
519225163bd5SWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev,
519325163bd5SWebb Scales 					"ABORT FAILED, new lockup detected");
519425163bd5SWebb Scales 		return FAILED;
519525163bd5SWebb Scales 	}
5196e345893bSDon Brace 
519775167d2cSStephen M. Cameron 	/* Check that controller supports some kind of task abort */
519875167d2cSStephen M. Cameron 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
519975167d2cSStephen M. Cameron 		!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
520075167d2cSStephen M. Cameron 		return FAILED;
520175167d2cSStephen M. Cameron 
520275167d2cSStephen M. Cameron 	memset(msg, 0, sizeof(msg));
52034b761557SRobert Elliott 	ml += sprintf(msg+ml, "scsi %d:%d:%d:%llu %s %p",
520475167d2cSStephen M. Cameron 		h->scsi_host->host_no, sc->device->channel,
52050d96ef5fSWebb Scales 		sc->device->id, sc->device->lun,
52064b761557SRobert Elliott 		"Aborting command", sc);
520775167d2cSStephen M. Cameron 
520875167d2cSStephen M. Cameron 	/* Get SCSI command to be aborted */
520975167d2cSStephen M. Cameron 	abort = (struct CommandList *) sc->host_scribble;
521075167d2cSStephen M. Cameron 	if (abort == NULL) {
5211281a7fd0SWebb Scales 		/* This can happen if the command already completed. */
5212281a7fd0SWebb Scales 		return SUCCESS;
5213281a7fd0SWebb Scales 	}
5214281a7fd0SWebb Scales 	refcount = atomic_inc_return(&abort->refcount);
5215281a7fd0SWebb Scales 	if (refcount == 1) { /* Command is done already. */
5216281a7fd0SWebb Scales 		cmd_free(h, abort);
5217281a7fd0SWebb Scales 		return SUCCESS;
521875167d2cSStephen M. Cameron 	}
52199b5c48c2SStephen Cameron 
52209b5c48c2SStephen Cameron 	/* Don't bother trying the abort if we know it won't work. */
52219b5c48c2SStephen Cameron 	if (abort->cmd_type != CMD_IOACCEL2 &&
52229b5c48c2SStephen Cameron 		abort->cmd_type != CMD_IOACCEL1 && !dev->supports_aborts) {
52239b5c48c2SStephen Cameron 		cmd_free(h, abort);
52249b5c48c2SStephen Cameron 		return FAILED;
52259b5c48c2SStephen Cameron 	}
52269b5c48c2SStephen Cameron 
522717eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
522825163bd5SWebb Scales 	reply_queue = hpsa_extract_reply_queue(h, abort);
522917eb87d2SScott Teel 	ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
52307fa3030cSStephen Cameron 	as  = abort->scsi_cmd;
523175167d2cSStephen M. Cameron 	if (as != NULL)
52324b761557SRobert Elliott 		ml += sprintf(msg+ml,
52334b761557SRobert Elliott 			"CDBLen: %d CDB: 0x%02x%02x... SN: 0x%lx ",
52344b761557SRobert Elliott 			as->cmd_len, as->cmnd[0], as->cmnd[1],
52354b761557SRobert Elliott 			as->serial_number);
52364b761557SRobert Elliott 	dev_warn(&h->pdev->dev, "%s BEING SENT\n", msg);
52370d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_WARNING, h, dev, "Aborting command");
52384b761557SRobert Elliott 
523975167d2cSStephen M. Cameron 	/*
524075167d2cSStephen M. Cameron 	 * Command is in flight, or possibly already completed
524175167d2cSStephen M. Cameron 	 * by the firmware (but not to the scsi mid layer) but we can't
524275167d2cSStephen M. Cameron 	 * distinguish which.  Send the abort down.
524375167d2cSStephen M. Cameron 	 */
52449b5c48c2SStephen Cameron 	if (wait_for_available_abort_cmd(h)) {
52459b5c48c2SStephen Cameron 		dev_warn(&h->pdev->dev,
52464b761557SRobert Elliott 			"%s FAILED, timeout waiting for an abort command to become available.\n",
52474b761557SRobert Elliott 			msg);
52489b5c48c2SStephen Cameron 		cmd_free(h, abort);
52499b5c48c2SStephen Cameron 		return FAILED;
52509b5c48c2SStephen Cameron 	}
525125163bd5SWebb Scales 	rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort, reply_queue);
52529b5c48c2SStephen Cameron 	atomic_inc(&h->abort_cmds_available);
52539b5c48c2SStephen Cameron 	wake_up_all(&h->abort_cmd_wait_queue);
525475167d2cSStephen M. Cameron 	if (rc != 0) {
52554b761557SRobert Elliott 		dev_warn(&h->pdev->dev, "%s SENT, FAILED\n", msg);
52560d96ef5fSWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev,
52570d96ef5fSWebb Scales 				"FAILED to abort command");
5258281a7fd0SWebb Scales 		cmd_free(h, abort);
525975167d2cSStephen M. Cameron 		return FAILED;
526075167d2cSStephen M. Cameron 	}
52614b761557SRobert Elliott 	dev_info(&h->pdev->dev, "%s SENT, SUCCESS\n", msg);
526275167d2cSStephen M. Cameron 
52634b761557SRobert Elliott 	/*
52644b761557SRobert Elliott 	 * If the abort(s) above completed and actually aborted the
526575167d2cSStephen M. Cameron 	 * command, then the command to be aborted should already be
526675167d2cSStephen M. Cameron 	 * completed.  If not, wait around a bit more to see if they
526775167d2cSStephen M. Cameron 	 * manage to complete normally.
526875167d2cSStephen M. Cameron 	 */
526975167d2cSStephen M. Cameron #define ABORT_COMPLETE_WAIT_SECS 30
527075167d2cSStephen M. Cameron 	for (i = 0; i < ABORT_COMPLETE_WAIT_SECS * 10; i++) {
5271281a7fd0SWebb Scales 		refcount = atomic_read(&abort->refcount);
5272281a7fd0SWebb Scales 		if (refcount < 2) {
5273281a7fd0SWebb Scales 			cmd_free(h, abort);
5274f2405db8SDon Brace 			return SUCCESS;
5275281a7fd0SWebb Scales 		} else {
5276281a7fd0SWebb Scales 			msleep(100);
5277281a7fd0SWebb Scales 		}
527875167d2cSStephen M. Cameron 	}
527975167d2cSStephen M. Cameron 	dev_warn(&h->pdev->dev, "%s FAILED. Aborted command has not completed after %d seconds.\n",
528075167d2cSStephen M. Cameron 		msg, ABORT_COMPLETE_WAIT_SECS);
5281281a7fd0SWebb Scales 	cmd_free(h, abort);
528275167d2cSStephen M. Cameron 	return FAILED;
528375167d2cSStephen M. Cameron }
528475167d2cSStephen M. Cameron 
5285edd16368SStephen M. Cameron /*
5286edd16368SStephen M. Cameron  * For operations that cannot sleep, a command block is allocated at init,
5287edd16368SStephen M. Cameron  * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
5288edd16368SStephen M. Cameron  * which ones are free or in use.  Lock must be held when calling this.
5289edd16368SStephen M. Cameron  * cmd_free() is the complement.
5290bf43caf3SRobert Elliott  * This function never gives up and returns NULL.  If it hangs,
5291bf43caf3SRobert Elliott  * another thread must call cmd_free() to free some tags.
5292edd16368SStephen M. Cameron  */
5293281a7fd0SWebb Scales 
5294edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h)
5295edd16368SStephen M. Cameron {
5296edd16368SStephen M. Cameron 	struct CommandList *c;
5297360c73bdSStephen Cameron 	int refcount, i;
529833811026SRobert Elliott 	unsigned long offset;
5299edd16368SStephen M. Cameron 
530033811026SRobert Elliott 	/*
530133811026SRobert Elliott 	 * There is some *extremely* small but non-zero chance that that
53024c413128SStephen M. Cameron 	 * multiple threads could get in here, and one thread could
53034c413128SStephen M. Cameron 	 * be scanning through the list of bits looking for a free
53044c413128SStephen M. Cameron 	 * one, but the free ones are always behind him, and other
53054c413128SStephen M. Cameron 	 * threads sneak in behind him and eat them before he can
53064c413128SStephen M. Cameron 	 * get to them, so that while there is always a free one, a
53074c413128SStephen M. Cameron 	 * very unlucky thread might be starved anyway, never able to
53084c413128SStephen M. Cameron 	 * beat the other threads.  In reality, this happens so
53094c413128SStephen M. Cameron 	 * infrequently as to be indistinguishable from never.
53104c413128SStephen M. Cameron 	 */
53114c413128SStephen M. Cameron 
531233811026SRobert Elliott 	offset = h->last_allocation; /* benignly racy */
5313281a7fd0SWebb Scales 	for (;;) {
5314281a7fd0SWebb Scales 		i = find_next_zero_bit(h->cmd_pool_bits, h->nr_cmds, offset);
5315281a7fd0SWebb Scales 		if (unlikely(i == h->nr_cmds)) {
5316281a7fd0SWebb Scales 			offset = 0;
5317281a7fd0SWebb Scales 			continue;
5318281a7fd0SWebb Scales 		}
5319edd16368SStephen M. Cameron 		c = h->cmd_pool + i;
5320281a7fd0SWebb Scales 		refcount = atomic_inc_return(&c->refcount);
5321281a7fd0SWebb Scales 		if (unlikely(refcount > 1)) {
5322281a7fd0SWebb Scales 			cmd_free(h, c); /* already in use */
5323281a7fd0SWebb Scales 			offset = (i + 1) % h->nr_cmds;
5324281a7fd0SWebb Scales 			continue;
5325281a7fd0SWebb Scales 		}
5326281a7fd0SWebb Scales 		set_bit(i & (BITS_PER_LONG - 1),
5327281a7fd0SWebb Scales 			h->cmd_pool_bits + (i / BITS_PER_LONG));
5328281a7fd0SWebb Scales 		break; /* it's ours now. */
5329281a7fd0SWebb Scales 	}
533033811026SRobert Elliott 	h->last_allocation = i; /* benignly racy */
5331360c73bdSStephen Cameron 	hpsa_cmd_partial_init(h, i, c);
5332edd16368SStephen M. Cameron 	return c;
5333edd16368SStephen M. Cameron }
5334edd16368SStephen M. Cameron 
5335edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c)
5336edd16368SStephen M. Cameron {
5337281a7fd0SWebb Scales 	if (atomic_dec_and_test(&c->refcount)) {
5338edd16368SStephen M. Cameron 		int i;
5339edd16368SStephen M. Cameron 
5340edd16368SStephen M. Cameron 		i = c - h->cmd_pool;
5341edd16368SStephen M. Cameron 		clear_bit(i & (BITS_PER_LONG - 1),
5342edd16368SStephen M. Cameron 			  h->cmd_pool_bits + (i / BITS_PER_LONG));
5343edd16368SStephen M. Cameron 	}
5344281a7fd0SWebb Scales }
5345edd16368SStephen M. Cameron 
5346edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
5347edd16368SStephen M. Cameron 
534842a91641SDon Brace static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd,
534942a91641SDon Brace 	void __user *arg)
5350edd16368SStephen M. Cameron {
5351edd16368SStephen M. Cameron 	IOCTL32_Command_struct __user *arg32 =
5352edd16368SStephen M. Cameron 	    (IOCTL32_Command_struct __user *) arg;
5353edd16368SStephen M. Cameron 	IOCTL_Command_struct arg64;
5354edd16368SStephen M. Cameron 	IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
5355edd16368SStephen M. Cameron 	int err;
5356edd16368SStephen M. Cameron 	u32 cp;
5357edd16368SStephen M. Cameron 
5358938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
5359edd16368SStephen M. Cameron 	err = 0;
5360edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
5361edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
5362edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
5363edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
5364edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
5365edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
5366edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
5367edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
5368edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
5369edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
5370edd16368SStephen M. Cameron 
5371edd16368SStephen M. Cameron 	if (err)
5372edd16368SStephen M. Cameron 		return -EFAULT;
5373edd16368SStephen M. Cameron 
537442a91641SDon Brace 	err = hpsa_ioctl(dev, CCISS_PASSTHRU, p);
5375edd16368SStephen M. Cameron 	if (err)
5376edd16368SStephen M. Cameron 		return err;
5377edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
5378edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
5379edd16368SStephen M. Cameron 	if (err)
5380edd16368SStephen M. Cameron 		return -EFAULT;
5381edd16368SStephen M. Cameron 	return err;
5382edd16368SStephen M. Cameron }
5383edd16368SStephen M. Cameron 
5384edd16368SStephen M. Cameron static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
538542a91641SDon Brace 	int cmd, void __user *arg)
5386edd16368SStephen M. Cameron {
5387edd16368SStephen M. Cameron 	BIG_IOCTL32_Command_struct __user *arg32 =
5388edd16368SStephen M. Cameron 	    (BIG_IOCTL32_Command_struct __user *) arg;
5389edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct arg64;
5390edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct __user *p =
5391edd16368SStephen M. Cameron 	    compat_alloc_user_space(sizeof(arg64));
5392edd16368SStephen M. Cameron 	int err;
5393edd16368SStephen M. Cameron 	u32 cp;
5394edd16368SStephen M. Cameron 
5395938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
5396edd16368SStephen M. Cameron 	err = 0;
5397edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
5398edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
5399edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
5400edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
5401edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
5402edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
5403edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
5404edd16368SStephen M. Cameron 	err |= get_user(arg64.malloc_size, &arg32->malloc_size);
5405edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
5406edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
5407edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
5408edd16368SStephen M. Cameron 
5409edd16368SStephen M. Cameron 	if (err)
5410edd16368SStephen M. Cameron 		return -EFAULT;
5411edd16368SStephen M. Cameron 
541242a91641SDon Brace 	err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, p);
5413edd16368SStephen M. Cameron 	if (err)
5414edd16368SStephen M. Cameron 		return err;
5415edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
5416edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
5417edd16368SStephen M. Cameron 	if (err)
5418edd16368SStephen M. Cameron 		return -EFAULT;
5419edd16368SStephen M. Cameron 	return err;
5420edd16368SStephen M. Cameron }
542171fe75a7SStephen M. Cameron 
542242a91641SDon Brace static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
542371fe75a7SStephen M. Cameron {
542471fe75a7SStephen M. Cameron 	switch (cmd) {
542571fe75a7SStephen M. Cameron 	case CCISS_GETPCIINFO:
542671fe75a7SStephen M. Cameron 	case CCISS_GETINTINFO:
542771fe75a7SStephen M. Cameron 	case CCISS_SETINTINFO:
542871fe75a7SStephen M. Cameron 	case CCISS_GETNODENAME:
542971fe75a7SStephen M. Cameron 	case CCISS_SETNODENAME:
543071fe75a7SStephen M. Cameron 	case CCISS_GETHEARTBEAT:
543171fe75a7SStephen M. Cameron 	case CCISS_GETBUSTYPES:
543271fe75a7SStephen M. Cameron 	case CCISS_GETFIRMVER:
543371fe75a7SStephen M. Cameron 	case CCISS_GETDRIVVER:
543471fe75a7SStephen M. Cameron 	case CCISS_REVALIDVOLS:
543571fe75a7SStephen M. Cameron 	case CCISS_DEREGDISK:
543671fe75a7SStephen M. Cameron 	case CCISS_REGNEWDISK:
543771fe75a7SStephen M. Cameron 	case CCISS_REGNEWD:
543871fe75a7SStephen M. Cameron 	case CCISS_RESCANDISK:
543971fe75a7SStephen M. Cameron 	case CCISS_GETLUNINFO:
544071fe75a7SStephen M. Cameron 		return hpsa_ioctl(dev, cmd, arg);
544171fe75a7SStephen M. Cameron 
544271fe75a7SStephen M. Cameron 	case CCISS_PASSTHRU32:
544371fe75a7SStephen M. Cameron 		return hpsa_ioctl32_passthru(dev, cmd, arg);
544471fe75a7SStephen M. Cameron 	case CCISS_BIG_PASSTHRU32:
544571fe75a7SStephen M. Cameron 		return hpsa_ioctl32_big_passthru(dev, cmd, arg);
544671fe75a7SStephen M. Cameron 
544771fe75a7SStephen M. Cameron 	default:
544871fe75a7SStephen M. Cameron 		return -ENOIOCTLCMD;
544971fe75a7SStephen M. Cameron 	}
545071fe75a7SStephen M. Cameron }
5451edd16368SStephen M. Cameron #endif
5452edd16368SStephen M. Cameron 
5453edd16368SStephen M. Cameron static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
5454edd16368SStephen M. Cameron {
5455edd16368SStephen M. Cameron 	struct hpsa_pci_info pciinfo;
5456edd16368SStephen M. Cameron 
5457edd16368SStephen M. Cameron 	if (!argp)
5458edd16368SStephen M. Cameron 		return -EINVAL;
5459edd16368SStephen M. Cameron 	pciinfo.domain = pci_domain_nr(h->pdev->bus);
5460edd16368SStephen M. Cameron 	pciinfo.bus = h->pdev->bus->number;
5461edd16368SStephen M. Cameron 	pciinfo.dev_fn = h->pdev->devfn;
5462edd16368SStephen M. Cameron 	pciinfo.board_id = h->board_id;
5463edd16368SStephen M. Cameron 	if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
5464edd16368SStephen M. Cameron 		return -EFAULT;
5465edd16368SStephen M. Cameron 	return 0;
5466edd16368SStephen M. Cameron }
5467edd16368SStephen M. Cameron 
5468edd16368SStephen M. Cameron static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
5469edd16368SStephen M. Cameron {
5470edd16368SStephen M. Cameron 	DriverVer_type DriverVer;
5471edd16368SStephen M. Cameron 	unsigned char vmaj, vmin, vsubmin;
5472edd16368SStephen M. Cameron 	int rc;
5473edd16368SStephen M. Cameron 
5474edd16368SStephen M. Cameron 	rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
5475edd16368SStephen M. Cameron 		&vmaj, &vmin, &vsubmin);
5476edd16368SStephen M. Cameron 	if (rc != 3) {
5477edd16368SStephen M. Cameron 		dev_info(&h->pdev->dev, "driver version string '%s' "
5478edd16368SStephen M. Cameron 			"unrecognized.", HPSA_DRIVER_VERSION);
5479edd16368SStephen M. Cameron 		vmaj = 0;
5480edd16368SStephen M. Cameron 		vmin = 0;
5481edd16368SStephen M. Cameron 		vsubmin = 0;
5482edd16368SStephen M. Cameron 	}
5483edd16368SStephen M. Cameron 	DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
5484edd16368SStephen M. Cameron 	if (!argp)
5485edd16368SStephen M. Cameron 		return -EINVAL;
5486edd16368SStephen M. Cameron 	if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
5487edd16368SStephen M. Cameron 		return -EFAULT;
5488edd16368SStephen M. Cameron 	return 0;
5489edd16368SStephen M. Cameron }
5490edd16368SStephen M. Cameron 
5491edd16368SStephen M. Cameron static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
5492edd16368SStephen M. Cameron {
5493edd16368SStephen M. Cameron 	IOCTL_Command_struct iocommand;
5494edd16368SStephen M. Cameron 	struct CommandList *c;
5495edd16368SStephen M. Cameron 	char *buff = NULL;
549650a0decfSStephen M. Cameron 	u64 temp64;
5497c1f63c8fSStephen M. Cameron 	int rc = 0;
5498edd16368SStephen M. Cameron 
5499edd16368SStephen M. Cameron 	if (!argp)
5500edd16368SStephen M. Cameron 		return -EINVAL;
5501edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
5502edd16368SStephen M. Cameron 		return -EPERM;
5503edd16368SStephen M. Cameron 	if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
5504edd16368SStephen M. Cameron 		return -EFAULT;
5505edd16368SStephen M. Cameron 	if ((iocommand.buf_size < 1) &&
5506edd16368SStephen M. Cameron 	    (iocommand.Request.Type.Direction != XFER_NONE)) {
5507edd16368SStephen M. Cameron 		return -EINVAL;
5508edd16368SStephen M. Cameron 	}
5509edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
5510edd16368SStephen M. Cameron 		buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
5511edd16368SStephen M. Cameron 		if (buff == NULL)
55122dd02d74SRobert Elliott 			return -ENOMEM;
55139233fb10SStephen M. Cameron 		if (iocommand.Request.Type.Direction & XFER_WRITE) {
5514edd16368SStephen M. Cameron 			/* Copy the data into the buffer we created */
5515b03a7771SStephen M. Cameron 			if (copy_from_user(buff, iocommand.buf,
5516b03a7771SStephen M. Cameron 				iocommand.buf_size)) {
5517c1f63c8fSStephen M. Cameron 				rc = -EFAULT;
5518c1f63c8fSStephen M. Cameron 				goto out_kfree;
5519edd16368SStephen M. Cameron 			}
5520b03a7771SStephen M. Cameron 		} else {
5521edd16368SStephen M. Cameron 			memset(buff, 0, iocommand.buf_size);
5522b03a7771SStephen M. Cameron 		}
5523b03a7771SStephen M. Cameron 	}
552445fcb86eSStephen Cameron 	c = cmd_alloc(h);
5525bf43caf3SRobert Elliott 
5526edd16368SStephen M. Cameron 	/* Fill in the command type */
5527edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5528edd16368SStephen M. Cameron 	/* Fill in Command Header */
5529edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0; /* unused in simple mode */
5530edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {	/* buffer to fill */
5531edd16368SStephen M. Cameron 		c->Header.SGList = 1;
553250a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(1);
5533edd16368SStephen M. Cameron 	} else	{ /* no buffers to fill */
5534edd16368SStephen M. Cameron 		c->Header.SGList = 0;
553550a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(0);
5536edd16368SStephen M. Cameron 	}
5537edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
5538edd16368SStephen M. Cameron 
5539edd16368SStephen M. Cameron 	/* Fill in Request block */
5540edd16368SStephen M. Cameron 	memcpy(&c->Request, &iocommand.Request,
5541edd16368SStephen M. Cameron 		sizeof(c->Request));
5542edd16368SStephen M. Cameron 
5543edd16368SStephen M. Cameron 	/* Fill in the scatter gather information */
5544edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
554550a0decfSStephen M. Cameron 		temp64 = pci_map_single(h->pdev, buff,
5546edd16368SStephen M. Cameron 			iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
554750a0decfSStephen M. Cameron 		if (dma_mapping_error(&h->pdev->dev, (dma_addr_t) temp64)) {
554850a0decfSStephen M. Cameron 			c->SG[0].Addr = cpu_to_le64(0);
554950a0decfSStephen M. Cameron 			c->SG[0].Len = cpu_to_le32(0);
5550bcc48ffaSStephen M. Cameron 			rc = -ENOMEM;
5551bcc48ffaSStephen M. Cameron 			goto out;
5552bcc48ffaSStephen M. Cameron 		}
555350a0decfSStephen M. Cameron 		c->SG[0].Addr = cpu_to_le64(temp64);
555450a0decfSStephen M. Cameron 		c->SG[0].Len = cpu_to_le32(iocommand.buf_size);
555550a0decfSStephen M. Cameron 		c->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* not chaining */
5556edd16368SStephen M. Cameron 	}
555725163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
5558c2dd32e0SStephen M. Cameron 	if (iocommand.buf_size > 0)
5559edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
5560edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
556125163bd5SWebb Scales 	if (rc) {
556225163bd5SWebb Scales 		rc = -EIO;
556325163bd5SWebb Scales 		goto out;
556425163bd5SWebb Scales 	}
5565edd16368SStephen M. Cameron 
5566edd16368SStephen M. Cameron 	/* Copy the error information out */
5567edd16368SStephen M. Cameron 	memcpy(&iocommand.error_info, c->err_info,
5568edd16368SStephen M. Cameron 		sizeof(iocommand.error_info));
5569edd16368SStephen M. Cameron 	if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
5570c1f63c8fSStephen M. Cameron 		rc = -EFAULT;
5571c1f63c8fSStephen M. Cameron 		goto out;
5572edd16368SStephen M. Cameron 	}
55739233fb10SStephen M. Cameron 	if ((iocommand.Request.Type.Direction & XFER_READ) &&
5574b03a7771SStephen M. Cameron 		iocommand.buf_size > 0) {
5575edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
5576edd16368SStephen M. Cameron 		if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
5577c1f63c8fSStephen M. Cameron 			rc = -EFAULT;
5578c1f63c8fSStephen M. Cameron 			goto out;
5579edd16368SStephen M. Cameron 		}
5580edd16368SStephen M. Cameron 	}
5581c1f63c8fSStephen M. Cameron out:
558245fcb86eSStephen Cameron 	cmd_free(h, c);
5583c1f63c8fSStephen M. Cameron out_kfree:
5584c1f63c8fSStephen M. Cameron 	kfree(buff);
5585c1f63c8fSStephen M. Cameron 	return rc;
5586edd16368SStephen M. Cameron }
5587edd16368SStephen M. Cameron 
5588edd16368SStephen M. Cameron static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
5589edd16368SStephen M. Cameron {
5590edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct *ioc;
5591edd16368SStephen M. Cameron 	struct CommandList *c;
5592edd16368SStephen M. Cameron 	unsigned char **buff = NULL;
5593edd16368SStephen M. Cameron 	int *buff_size = NULL;
559450a0decfSStephen M. Cameron 	u64 temp64;
5595edd16368SStephen M. Cameron 	BYTE sg_used = 0;
5596edd16368SStephen M. Cameron 	int status = 0;
559701a02ffcSStephen M. Cameron 	u32 left;
559801a02ffcSStephen M. Cameron 	u32 sz;
5599edd16368SStephen M. Cameron 	BYTE __user *data_ptr;
5600edd16368SStephen M. Cameron 
5601edd16368SStephen M. Cameron 	if (!argp)
5602edd16368SStephen M. Cameron 		return -EINVAL;
5603edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
5604edd16368SStephen M. Cameron 		return -EPERM;
5605edd16368SStephen M. Cameron 	ioc = (BIG_IOCTL_Command_struct *)
5606edd16368SStephen M. Cameron 	    kmalloc(sizeof(*ioc), GFP_KERNEL);
5607edd16368SStephen M. Cameron 	if (!ioc) {
5608edd16368SStephen M. Cameron 		status = -ENOMEM;
5609edd16368SStephen M. Cameron 		goto cleanup1;
5610edd16368SStephen M. Cameron 	}
5611edd16368SStephen M. Cameron 	if (copy_from_user(ioc, argp, sizeof(*ioc))) {
5612edd16368SStephen M. Cameron 		status = -EFAULT;
5613edd16368SStephen M. Cameron 		goto cleanup1;
5614edd16368SStephen M. Cameron 	}
5615edd16368SStephen M. Cameron 	if ((ioc->buf_size < 1) &&
5616edd16368SStephen M. Cameron 	    (ioc->Request.Type.Direction != XFER_NONE)) {
5617edd16368SStephen M. Cameron 		status = -EINVAL;
5618edd16368SStephen M. Cameron 		goto cleanup1;
5619edd16368SStephen M. Cameron 	}
5620edd16368SStephen M. Cameron 	/* Check kmalloc limits  using all SGs */
5621edd16368SStephen M. Cameron 	if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
5622edd16368SStephen M. Cameron 		status = -EINVAL;
5623edd16368SStephen M. Cameron 		goto cleanup1;
5624edd16368SStephen M. Cameron 	}
5625d66ae08bSStephen M. Cameron 	if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
5626edd16368SStephen M. Cameron 		status = -EINVAL;
5627edd16368SStephen M. Cameron 		goto cleanup1;
5628edd16368SStephen M. Cameron 	}
5629d66ae08bSStephen M. Cameron 	buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
5630edd16368SStephen M. Cameron 	if (!buff) {
5631edd16368SStephen M. Cameron 		status = -ENOMEM;
5632edd16368SStephen M. Cameron 		goto cleanup1;
5633edd16368SStephen M. Cameron 	}
5634d66ae08bSStephen M. Cameron 	buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
5635edd16368SStephen M. Cameron 	if (!buff_size) {
5636edd16368SStephen M. Cameron 		status = -ENOMEM;
5637edd16368SStephen M. Cameron 		goto cleanup1;
5638edd16368SStephen M. Cameron 	}
5639edd16368SStephen M. Cameron 	left = ioc->buf_size;
5640edd16368SStephen M. Cameron 	data_ptr = ioc->buf;
5641edd16368SStephen M. Cameron 	while (left) {
5642edd16368SStephen M. Cameron 		sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
5643edd16368SStephen M. Cameron 		buff_size[sg_used] = sz;
5644edd16368SStephen M. Cameron 		buff[sg_used] = kmalloc(sz, GFP_KERNEL);
5645edd16368SStephen M. Cameron 		if (buff[sg_used] == NULL) {
5646edd16368SStephen M. Cameron 			status = -ENOMEM;
5647edd16368SStephen M. Cameron 			goto cleanup1;
5648edd16368SStephen M. Cameron 		}
56499233fb10SStephen M. Cameron 		if (ioc->Request.Type.Direction & XFER_WRITE) {
5650edd16368SStephen M. Cameron 			if (copy_from_user(buff[sg_used], data_ptr, sz)) {
56510758f4f7SStephen M. Cameron 				status = -EFAULT;
5652edd16368SStephen M. Cameron 				goto cleanup1;
5653edd16368SStephen M. Cameron 			}
5654edd16368SStephen M. Cameron 		} else
5655edd16368SStephen M. Cameron 			memset(buff[sg_used], 0, sz);
5656edd16368SStephen M. Cameron 		left -= sz;
5657edd16368SStephen M. Cameron 		data_ptr += sz;
5658edd16368SStephen M. Cameron 		sg_used++;
5659edd16368SStephen M. Cameron 	}
566045fcb86eSStephen Cameron 	c = cmd_alloc(h);
5661bf43caf3SRobert Elliott 
5662edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5663edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
566450a0decfSStephen M. Cameron 	c->Header.SGList = (u8) sg_used;
566550a0decfSStephen M. Cameron 	c->Header.SGTotal = cpu_to_le16(sg_used);
5666edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
5667edd16368SStephen M. Cameron 	memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
5668edd16368SStephen M. Cameron 	if (ioc->buf_size > 0) {
5669edd16368SStephen M. Cameron 		int i;
5670edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
567150a0decfSStephen M. Cameron 			temp64 = pci_map_single(h->pdev, buff[i],
5672edd16368SStephen M. Cameron 				    buff_size[i], PCI_DMA_BIDIRECTIONAL);
567350a0decfSStephen M. Cameron 			if (dma_mapping_error(&h->pdev->dev,
567450a0decfSStephen M. Cameron 							(dma_addr_t) temp64)) {
567550a0decfSStephen M. Cameron 				c->SG[i].Addr = cpu_to_le64(0);
567650a0decfSStephen M. Cameron 				c->SG[i].Len = cpu_to_le32(0);
5677bcc48ffaSStephen M. Cameron 				hpsa_pci_unmap(h->pdev, c, i,
5678bcc48ffaSStephen M. Cameron 					PCI_DMA_BIDIRECTIONAL);
5679bcc48ffaSStephen M. Cameron 				status = -ENOMEM;
5680e2d4a1f6SStephen M. Cameron 				goto cleanup0;
5681bcc48ffaSStephen M. Cameron 			}
568250a0decfSStephen M. Cameron 			c->SG[i].Addr = cpu_to_le64(temp64);
568350a0decfSStephen M. Cameron 			c->SG[i].Len = cpu_to_le32(buff_size[i]);
568450a0decfSStephen M. Cameron 			c->SG[i].Ext = cpu_to_le32(0);
5685edd16368SStephen M. Cameron 		}
568650a0decfSStephen M. Cameron 		c->SG[--i].Ext = cpu_to_le32(HPSA_SG_LAST);
5687edd16368SStephen M. Cameron 	}
568825163bd5SWebb Scales 	status = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
5689b03a7771SStephen M. Cameron 	if (sg_used)
5690edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
5691edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
569225163bd5SWebb Scales 	if (status) {
569325163bd5SWebb Scales 		status = -EIO;
569425163bd5SWebb Scales 		goto cleanup0;
569525163bd5SWebb Scales 	}
569625163bd5SWebb Scales 
5697edd16368SStephen M. Cameron 	/* Copy the error information out */
5698edd16368SStephen M. Cameron 	memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
5699edd16368SStephen M. Cameron 	if (copy_to_user(argp, ioc, sizeof(*ioc))) {
5700edd16368SStephen M. Cameron 		status = -EFAULT;
5701e2d4a1f6SStephen M. Cameron 		goto cleanup0;
5702edd16368SStephen M. Cameron 	}
57039233fb10SStephen M. Cameron 	if ((ioc->Request.Type.Direction & XFER_READ) && ioc->buf_size > 0) {
57042b08b3e9SDon Brace 		int i;
57052b08b3e9SDon Brace 
5706edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
5707edd16368SStephen M. Cameron 		BYTE __user *ptr = ioc->buf;
5708edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
5709edd16368SStephen M. Cameron 			if (copy_to_user(ptr, buff[i], buff_size[i])) {
5710edd16368SStephen M. Cameron 				status = -EFAULT;
5711e2d4a1f6SStephen M. Cameron 				goto cleanup0;
5712edd16368SStephen M. Cameron 			}
5713edd16368SStephen M. Cameron 			ptr += buff_size[i];
5714edd16368SStephen M. Cameron 		}
5715edd16368SStephen M. Cameron 	}
5716edd16368SStephen M. Cameron 	status = 0;
5717e2d4a1f6SStephen M. Cameron cleanup0:
571845fcb86eSStephen Cameron 	cmd_free(h, c);
5719edd16368SStephen M. Cameron cleanup1:
5720edd16368SStephen M. Cameron 	if (buff) {
57212b08b3e9SDon Brace 		int i;
57222b08b3e9SDon Brace 
5723edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++)
5724edd16368SStephen M. Cameron 			kfree(buff[i]);
5725edd16368SStephen M. Cameron 		kfree(buff);
5726edd16368SStephen M. Cameron 	}
5727edd16368SStephen M. Cameron 	kfree(buff_size);
5728edd16368SStephen M. Cameron 	kfree(ioc);
5729edd16368SStephen M. Cameron 	return status;
5730edd16368SStephen M. Cameron }
5731edd16368SStephen M. Cameron 
5732edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
5733edd16368SStephen M. Cameron 	struct CommandList *c)
5734edd16368SStephen M. Cameron {
5735edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
5736edd16368SStephen M. Cameron 			c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
5737edd16368SStephen M. Cameron 		(void) check_for_unit_attention(h, c);
5738edd16368SStephen M. Cameron }
57390390f0c0SStephen M. Cameron 
5740edd16368SStephen M. Cameron /*
5741edd16368SStephen M. Cameron  * ioctl
5742edd16368SStephen M. Cameron  */
574342a91641SDon Brace static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
5744edd16368SStephen M. Cameron {
5745edd16368SStephen M. Cameron 	struct ctlr_info *h;
5746edd16368SStephen M. Cameron 	void __user *argp = (void __user *)arg;
57470390f0c0SStephen M. Cameron 	int rc;
5748edd16368SStephen M. Cameron 
5749edd16368SStephen M. Cameron 	h = sdev_to_hba(dev);
5750edd16368SStephen M. Cameron 
5751edd16368SStephen M. Cameron 	switch (cmd) {
5752edd16368SStephen M. Cameron 	case CCISS_DEREGDISK:
5753edd16368SStephen M. Cameron 	case CCISS_REGNEWDISK:
5754edd16368SStephen M. Cameron 	case CCISS_REGNEWD:
5755a08a8471SStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
5756edd16368SStephen M. Cameron 		return 0;
5757edd16368SStephen M. Cameron 	case CCISS_GETPCIINFO:
5758edd16368SStephen M. Cameron 		return hpsa_getpciinfo_ioctl(h, argp);
5759edd16368SStephen M. Cameron 	case CCISS_GETDRIVVER:
5760edd16368SStephen M. Cameron 		return hpsa_getdrivver_ioctl(h, argp);
5761edd16368SStephen M. Cameron 	case CCISS_PASSTHRU:
576234f0c627SDon Brace 		if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
57630390f0c0SStephen M. Cameron 			return -EAGAIN;
57640390f0c0SStephen M. Cameron 		rc = hpsa_passthru_ioctl(h, argp);
576534f0c627SDon Brace 		atomic_inc(&h->passthru_cmds_avail);
57660390f0c0SStephen M. Cameron 		return rc;
5767edd16368SStephen M. Cameron 	case CCISS_BIG_PASSTHRU:
576834f0c627SDon Brace 		if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
57690390f0c0SStephen M. Cameron 			return -EAGAIN;
57700390f0c0SStephen M. Cameron 		rc = hpsa_big_passthru_ioctl(h, argp);
577134f0c627SDon Brace 		atomic_inc(&h->passthru_cmds_avail);
57720390f0c0SStephen M. Cameron 		return rc;
5773edd16368SStephen M. Cameron 	default:
5774edd16368SStephen M. Cameron 		return -ENOTTY;
5775edd16368SStephen M. Cameron 	}
5776edd16368SStephen M. Cameron }
5777edd16368SStephen M. Cameron 
5778bf43caf3SRobert Elliott static void hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
57796f039790SGreg Kroah-Hartman 				u8 reset_type)
578064670ac8SStephen M. Cameron {
578164670ac8SStephen M. Cameron 	struct CommandList *c;
578264670ac8SStephen M. Cameron 
578364670ac8SStephen M. Cameron 	c = cmd_alloc(h);
5784bf43caf3SRobert Elliott 
5785a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map */
5786a2dac136SStephen M. Cameron 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
578764670ac8SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_MSG);
578864670ac8SStephen M. Cameron 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
578964670ac8SStephen M. Cameron 	c->waiting = NULL;
579064670ac8SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
579164670ac8SStephen M. Cameron 	/* Don't wait for completion, the reset won't complete.  Don't free
579264670ac8SStephen M. Cameron 	 * the command either.  This is the last command we will send before
579364670ac8SStephen M. Cameron 	 * re-initializing everything, so it doesn't matter and won't leak.
579464670ac8SStephen M. Cameron 	 */
5795bf43caf3SRobert Elliott 	return;
579664670ac8SStephen M. Cameron }
579764670ac8SStephen M. Cameron 
5798a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
5799b7bb24ebSStephen M. Cameron 	void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
5800edd16368SStephen M. Cameron 	int cmd_type)
5801edd16368SStephen M. Cameron {
5802edd16368SStephen M. Cameron 	int pci_dir = XFER_NONE;
58039b5c48c2SStephen Cameron 	u64 tag; /* for commands to be aborted */
5804edd16368SStephen M. Cameron 
5805edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5806edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
5807edd16368SStephen M. Cameron 	if (buff != NULL && size > 0) {
5808edd16368SStephen M. Cameron 		c->Header.SGList = 1;
580950a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(1);
5810edd16368SStephen M. Cameron 	} else {
5811edd16368SStephen M. Cameron 		c->Header.SGList = 0;
581250a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(0);
5813edd16368SStephen M. Cameron 	}
5814edd16368SStephen M. Cameron 	memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
5815edd16368SStephen M. Cameron 
5816edd16368SStephen M. Cameron 	if (cmd_type == TYPE_CMD) {
5817edd16368SStephen M. Cameron 		switch (cmd) {
5818edd16368SStephen M. Cameron 		case HPSA_INQUIRY:
5819edd16368SStephen M. Cameron 			/* are we trying to read a vital product page */
5820b7bb24ebSStephen M. Cameron 			if (page_code & VPD_PAGE) {
5821edd16368SStephen M. Cameron 				c->Request.CDB[1] = 0x01;
5822b7bb24ebSStephen M. Cameron 				c->Request.CDB[2] = (page_code & 0xff);
5823edd16368SStephen M. Cameron 			}
5824edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
5825a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5826a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5827edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5828edd16368SStephen M. Cameron 			c->Request.CDB[0] = HPSA_INQUIRY;
5829edd16368SStephen M. Cameron 			c->Request.CDB[4] = size & 0xFF;
5830edd16368SStephen M. Cameron 			break;
5831edd16368SStephen M. Cameron 		case HPSA_REPORT_LOG:
5832edd16368SStephen M. Cameron 		case HPSA_REPORT_PHYS:
5833edd16368SStephen M. Cameron 			/* Talking to controller so It's a physical command
5834edd16368SStephen M. Cameron 			   mode = 00 target = 0.  Nothing to write.
5835edd16368SStephen M. Cameron 			 */
5836edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
5837a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5838a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5839edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5840edd16368SStephen M. Cameron 			c->Request.CDB[0] = cmd;
5841edd16368SStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
5842edd16368SStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5843edd16368SStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5844edd16368SStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
5845edd16368SStephen M. Cameron 			break;
5846edd16368SStephen M. Cameron 		case HPSA_CACHE_FLUSH:
5847edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
5848a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5849a505b86fSStephen M. Cameron 					TYPE_ATTR_DIR(cmd_type,
5850a505b86fSStephen M. Cameron 						ATTR_SIMPLE, XFER_WRITE);
5851edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5852edd16368SStephen M. Cameron 			c->Request.CDB[0] = BMIC_WRITE;
5853edd16368SStephen M. Cameron 			c->Request.CDB[6] = BMIC_CACHE_FLUSH;
5854bb158eabSStephen M. Cameron 			c->Request.CDB[7] = (size >> 8) & 0xFF;
5855bb158eabSStephen M. Cameron 			c->Request.CDB[8] = size & 0xFF;
5856edd16368SStephen M. Cameron 			break;
5857edd16368SStephen M. Cameron 		case TEST_UNIT_READY:
5858edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
5859a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5860a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
5861edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5862edd16368SStephen M. Cameron 			break;
5863283b4a9bSStephen M. Cameron 		case HPSA_GET_RAID_MAP:
5864283b4a9bSStephen M. Cameron 			c->Request.CDBLen = 12;
5865a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5866a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5867283b4a9bSStephen M. Cameron 			c->Request.Timeout = 0;
5868283b4a9bSStephen M. Cameron 			c->Request.CDB[0] = HPSA_CISS_READ;
5869283b4a9bSStephen M. Cameron 			c->Request.CDB[1] = cmd;
5870283b4a9bSStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
5871283b4a9bSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5872283b4a9bSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5873283b4a9bSStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
5874283b4a9bSStephen M. Cameron 			break;
5875316b221aSStephen M. Cameron 		case BMIC_SENSE_CONTROLLER_PARAMETERS:
5876316b221aSStephen M. Cameron 			c->Request.CDBLen = 10;
5877a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5878a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5879316b221aSStephen M. Cameron 			c->Request.Timeout = 0;
5880316b221aSStephen M. Cameron 			c->Request.CDB[0] = BMIC_READ;
5881316b221aSStephen M. Cameron 			c->Request.CDB[6] = BMIC_SENSE_CONTROLLER_PARAMETERS;
5882316b221aSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5883316b221aSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5884316b221aSStephen M. Cameron 			break;
588503383736SDon Brace 		case BMIC_IDENTIFY_PHYSICAL_DEVICE:
588603383736SDon Brace 			c->Request.CDBLen = 10;
588703383736SDon Brace 			c->Request.type_attr_dir =
588803383736SDon Brace 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
588903383736SDon Brace 			c->Request.Timeout = 0;
589003383736SDon Brace 			c->Request.CDB[0] = BMIC_READ;
589103383736SDon Brace 			c->Request.CDB[6] = BMIC_IDENTIFY_PHYSICAL_DEVICE;
589203383736SDon Brace 			c->Request.CDB[7] = (size >> 16) & 0xFF;
589303383736SDon Brace 			c->Request.CDB[8] = (size >> 8) & 0XFF;
589403383736SDon Brace 			break;
5895edd16368SStephen M. Cameron 		default:
5896edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
5897edd16368SStephen M. Cameron 			BUG();
5898a2dac136SStephen M. Cameron 			return -1;
5899edd16368SStephen M. Cameron 		}
5900edd16368SStephen M. Cameron 	} else if (cmd_type == TYPE_MSG) {
5901edd16368SStephen M. Cameron 		switch (cmd) {
5902edd16368SStephen M. Cameron 
5903edd16368SStephen M. Cameron 		case  HPSA_DEVICE_RESET_MSG:
5904edd16368SStephen M. Cameron 			c->Request.CDBLen = 16;
5905a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5906a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
5907edd16368SStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
590864670ac8SStephen M. Cameron 			memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
590964670ac8SStephen M. Cameron 			c->Request.CDB[0] =  cmd;
591021e89afdSStephen M. Cameron 			c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
5911edd16368SStephen M. Cameron 			/* If bytes 4-7 are zero, it means reset the */
5912edd16368SStephen M. Cameron 			/* LunID device */
5913edd16368SStephen M. Cameron 			c->Request.CDB[4] = 0x00;
5914edd16368SStephen M. Cameron 			c->Request.CDB[5] = 0x00;
5915edd16368SStephen M. Cameron 			c->Request.CDB[6] = 0x00;
5916edd16368SStephen M. Cameron 			c->Request.CDB[7] = 0x00;
5917edd16368SStephen M. Cameron 			break;
591875167d2cSStephen M. Cameron 		case  HPSA_ABORT_MSG:
59199b5c48c2SStephen Cameron 			memcpy(&tag, buff, sizeof(tag));
59202b08b3e9SDon Brace 			dev_dbg(&h->pdev->dev,
59219b5c48c2SStephen Cameron 				"Abort Tag:0x%016llx using rqst Tag:0x%016llx",
59229b5c48c2SStephen Cameron 				tag, c->Header.tag);
592375167d2cSStephen M. Cameron 			c->Request.CDBLen = 16;
5924a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5925a505b86fSStephen M. Cameron 					TYPE_ATTR_DIR(cmd_type,
5926a505b86fSStephen M. Cameron 						ATTR_SIMPLE, XFER_WRITE);
592775167d2cSStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
592875167d2cSStephen M. Cameron 			c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
592975167d2cSStephen M. Cameron 			c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
593075167d2cSStephen M. Cameron 			c->Request.CDB[2] = 0x00; /* reserved */
593175167d2cSStephen M. Cameron 			c->Request.CDB[3] = 0x00; /* reserved */
593275167d2cSStephen M. Cameron 			/* Tag to abort goes in CDB[4]-CDB[11] */
59339b5c48c2SStephen Cameron 			memcpy(&c->Request.CDB[4], &tag, sizeof(tag));
593475167d2cSStephen M. Cameron 			c->Request.CDB[12] = 0x00; /* reserved */
593575167d2cSStephen M. Cameron 			c->Request.CDB[13] = 0x00; /* reserved */
593675167d2cSStephen M. Cameron 			c->Request.CDB[14] = 0x00; /* reserved */
593775167d2cSStephen M. Cameron 			c->Request.CDB[15] = 0x00; /* reserved */
593875167d2cSStephen M. Cameron 		break;
5939edd16368SStephen M. Cameron 		default:
5940edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown message type %d\n",
5941edd16368SStephen M. Cameron 				cmd);
5942edd16368SStephen M. Cameron 			BUG();
5943edd16368SStephen M. Cameron 		}
5944edd16368SStephen M. Cameron 	} else {
5945edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
5946edd16368SStephen M. Cameron 		BUG();
5947edd16368SStephen M. Cameron 	}
5948edd16368SStephen M. Cameron 
5949a505b86fSStephen M. Cameron 	switch (GET_DIR(c->Request.type_attr_dir)) {
5950edd16368SStephen M. Cameron 	case XFER_READ:
5951edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_FROMDEVICE;
5952edd16368SStephen M. Cameron 		break;
5953edd16368SStephen M. Cameron 	case XFER_WRITE:
5954edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_TODEVICE;
5955edd16368SStephen M. Cameron 		break;
5956edd16368SStephen M. Cameron 	case XFER_NONE:
5957edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_NONE;
5958edd16368SStephen M. Cameron 		break;
5959edd16368SStephen M. Cameron 	default:
5960edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_BIDIRECTIONAL;
5961edd16368SStephen M. Cameron 	}
5962a2dac136SStephen M. Cameron 	if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
5963a2dac136SStephen M. Cameron 		return -1;
5964a2dac136SStephen M. Cameron 	return 0;
5965edd16368SStephen M. Cameron }
5966edd16368SStephen M. Cameron 
5967edd16368SStephen M. Cameron /*
5968edd16368SStephen M. Cameron  * Map (physical) PCI mem into (virtual) kernel space
5969edd16368SStephen M. Cameron  */
5970edd16368SStephen M. Cameron static void __iomem *remap_pci_mem(ulong base, ulong size)
5971edd16368SStephen M. Cameron {
5972edd16368SStephen M. Cameron 	ulong page_base = ((ulong) base) & PAGE_MASK;
5973edd16368SStephen M. Cameron 	ulong page_offs = ((ulong) base) - page_base;
5974088ba34cSStephen M. Cameron 	void __iomem *page_remapped = ioremap_nocache(page_base,
5975088ba34cSStephen M. Cameron 		page_offs + size);
5976edd16368SStephen M. Cameron 
5977edd16368SStephen M. Cameron 	return page_remapped ? (page_remapped + page_offs) : NULL;
5978edd16368SStephen M. Cameron }
5979edd16368SStephen M. Cameron 
5980254f796bSMatt Gates static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
5981edd16368SStephen M. Cameron {
5982254f796bSMatt Gates 	return h->access.command_completed(h, q);
5983edd16368SStephen M. Cameron }
5984edd16368SStephen M. Cameron 
5985900c5440SStephen M. Cameron static inline bool interrupt_pending(struct ctlr_info *h)
5986edd16368SStephen M. Cameron {
5987edd16368SStephen M. Cameron 	return h->access.intr_pending(h);
5988edd16368SStephen M. Cameron }
5989edd16368SStephen M. Cameron 
5990edd16368SStephen M. Cameron static inline long interrupt_not_for_us(struct ctlr_info *h)
5991edd16368SStephen M. Cameron {
599210f66018SStephen M. Cameron 	return (h->access.intr_pending(h) == 0) ||
599310f66018SStephen M. Cameron 		(h->interrupts_enabled == 0);
5994edd16368SStephen M. Cameron }
5995edd16368SStephen M. Cameron 
599601a02ffcSStephen M. Cameron static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
599701a02ffcSStephen M. Cameron 	u32 raw_tag)
5998edd16368SStephen M. Cameron {
5999edd16368SStephen M. Cameron 	if (unlikely(tag_index >= h->nr_cmds)) {
6000edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
6001edd16368SStephen M. Cameron 		return 1;
6002edd16368SStephen M. Cameron 	}
6003edd16368SStephen M. Cameron 	return 0;
6004edd16368SStephen M. Cameron }
6005edd16368SStephen M. Cameron 
60065a3d16f5SStephen M. Cameron static inline void finish_cmd(struct CommandList *c)
6007edd16368SStephen M. Cameron {
6008e85c5974SStephen M. Cameron 	dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
6009c349775eSScott Teel 	if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
6010c349775eSScott Teel 			|| c->cmd_type == CMD_IOACCEL2))
60111fb011fbSStephen M. Cameron 		complete_scsi_command(c);
6012*8be986ccSStephen Cameron 	else if (c->cmd_type == CMD_IOCTL_PEND || c->cmd_type == IOACCEL2_TMF)
6013edd16368SStephen M. Cameron 		complete(c->waiting);
6014a104c99fSStephen M. Cameron }
6015a104c99fSStephen M. Cameron 
6016a9a3a273SStephen M. Cameron 
6017a9a3a273SStephen M. Cameron static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
6018a104c99fSStephen M. Cameron {
6019a9a3a273SStephen M. Cameron #define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
6020a9a3a273SStephen M. Cameron #define HPSA_SIMPLE_ERROR_BITS 0x03
6021960a30e7SStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
6022a9a3a273SStephen M. Cameron 		return tag & ~HPSA_SIMPLE_ERROR_BITS;
6023a9a3a273SStephen M. Cameron 	return tag & ~HPSA_PERF_ERROR_BITS;
6024a104c99fSStephen M. Cameron }
6025a104c99fSStephen M. Cameron 
6026303932fdSDon Brace /* process completion of an indexed ("direct lookup") command */
60271d94f94dSStephen M. Cameron static inline void process_indexed_cmd(struct ctlr_info *h,
6028303932fdSDon Brace 	u32 raw_tag)
6029303932fdSDon Brace {
6030303932fdSDon Brace 	u32 tag_index;
6031303932fdSDon Brace 	struct CommandList *c;
6032303932fdSDon Brace 
6033f2405db8SDon Brace 	tag_index = raw_tag >> DIRECT_LOOKUP_SHIFT;
60341d94f94dSStephen M. Cameron 	if (!bad_tag(h, tag_index, raw_tag)) {
6035303932fdSDon Brace 		c = h->cmd_pool + tag_index;
60365a3d16f5SStephen M. Cameron 		finish_cmd(c);
60371d94f94dSStephen M. Cameron 	}
6038303932fdSDon Brace }
6039303932fdSDon Brace 
604064670ac8SStephen M. Cameron /* Some controllers, like p400, will give us one interrupt
604164670ac8SStephen M. Cameron  * after a soft reset, even if we turned interrupts off.
604264670ac8SStephen M. Cameron  * Only need to check for this in the hpsa_xxx_discard_completions
604364670ac8SStephen M. Cameron  * functions.
604464670ac8SStephen M. Cameron  */
604564670ac8SStephen M. Cameron static int ignore_bogus_interrupt(struct ctlr_info *h)
604664670ac8SStephen M. Cameron {
604764670ac8SStephen M. Cameron 	if (likely(!reset_devices))
604864670ac8SStephen M. Cameron 		return 0;
604964670ac8SStephen M. Cameron 
605064670ac8SStephen M. Cameron 	if (likely(h->interrupts_enabled))
605164670ac8SStephen M. Cameron 		return 0;
605264670ac8SStephen M. Cameron 
605364670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
605464670ac8SStephen M. Cameron 		"(known firmware bug.)  Ignoring.\n");
605564670ac8SStephen M. Cameron 
605664670ac8SStephen M. Cameron 	return 1;
605764670ac8SStephen M. Cameron }
605864670ac8SStephen M. Cameron 
6059254f796bSMatt Gates /*
6060254f796bSMatt Gates  * Convert &h->q[x] (passed to interrupt handlers) back to h.
6061254f796bSMatt Gates  * Relies on (h-q[x] == x) being true for x such that
6062254f796bSMatt Gates  * 0 <= x < MAX_REPLY_QUEUES.
6063254f796bSMatt Gates  */
6064254f796bSMatt Gates static struct ctlr_info *queue_to_hba(u8 *queue)
606564670ac8SStephen M. Cameron {
6066254f796bSMatt Gates 	return container_of((queue - *queue), struct ctlr_info, q[0]);
6067254f796bSMatt Gates }
6068254f796bSMatt Gates 
6069254f796bSMatt Gates static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
6070254f796bSMatt Gates {
6071254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
6072254f796bSMatt Gates 	u8 q = *(u8 *) queue;
607364670ac8SStephen M. Cameron 	u32 raw_tag;
607464670ac8SStephen M. Cameron 
607564670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
607664670ac8SStephen M. Cameron 		return IRQ_NONE;
607764670ac8SStephen M. Cameron 
607864670ac8SStephen M. Cameron 	if (interrupt_not_for_us(h))
607964670ac8SStephen M. Cameron 		return IRQ_NONE;
6080a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
608164670ac8SStephen M. Cameron 	while (interrupt_pending(h)) {
6082254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
608364670ac8SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY)
6084254f796bSMatt Gates 			raw_tag = next_command(h, q);
608564670ac8SStephen M. Cameron 	}
608664670ac8SStephen M. Cameron 	return IRQ_HANDLED;
608764670ac8SStephen M. Cameron }
608864670ac8SStephen M. Cameron 
6089254f796bSMatt Gates static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
609064670ac8SStephen M. Cameron {
6091254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
609264670ac8SStephen M. Cameron 	u32 raw_tag;
6093254f796bSMatt Gates 	u8 q = *(u8 *) queue;
609464670ac8SStephen M. Cameron 
609564670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
609664670ac8SStephen M. Cameron 		return IRQ_NONE;
609764670ac8SStephen M. Cameron 
6098a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
6099254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
610064670ac8SStephen M. Cameron 	while (raw_tag != FIFO_EMPTY)
6101254f796bSMatt Gates 		raw_tag = next_command(h, q);
610264670ac8SStephen M. Cameron 	return IRQ_HANDLED;
610364670ac8SStephen M. Cameron }
610464670ac8SStephen M. Cameron 
6105254f796bSMatt Gates static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
6106edd16368SStephen M. Cameron {
6107254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba((u8 *) queue);
6108303932fdSDon Brace 	u32 raw_tag;
6109254f796bSMatt Gates 	u8 q = *(u8 *) queue;
6110edd16368SStephen M. Cameron 
6111edd16368SStephen M. Cameron 	if (interrupt_not_for_us(h))
6112edd16368SStephen M. Cameron 		return IRQ_NONE;
6113a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
611410f66018SStephen M. Cameron 	while (interrupt_pending(h)) {
6115254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
611610f66018SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY) {
61171d94f94dSStephen M. Cameron 			process_indexed_cmd(h, raw_tag);
6118254f796bSMatt Gates 			raw_tag = next_command(h, q);
611910f66018SStephen M. Cameron 		}
612010f66018SStephen M. Cameron 	}
612110f66018SStephen M. Cameron 	return IRQ_HANDLED;
612210f66018SStephen M. Cameron }
612310f66018SStephen M. Cameron 
6124254f796bSMatt Gates static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
612510f66018SStephen M. Cameron {
6126254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
612710f66018SStephen M. Cameron 	u32 raw_tag;
6128254f796bSMatt Gates 	u8 q = *(u8 *) queue;
612910f66018SStephen M. Cameron 
6130a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
6131254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
6132303932fdSDon Brace 	while (raw_tag != FIFO_EMPTY) {
61331d94f94dSStephen M. Cameron 		process_indexed_cmd(h, raw_tag);
6134254f796bSMatt Gates 		raw_tag = next_command(h, q);
6135edd16368SStephen M. Cameron 	}
6136edd16368SStephen M. Cameron 	return IRQ_HANDLED;
6137edd16368SStephen M. Cameron }
6138edd16368SStephen M. Cameron 
6139a9a3a273SStephen M. Cameron /* Send a message CDB to the firmware. Careful, this only works
6140a9a3a273SStephen M. Cameron  * in simple mode, not performant mode due to the tag lookup.
6141a9a3a273SStephen M. Cameron  * We only ever use this immediately after a controller reset.
6142a9a3a273SStephen M. Cameron  */
61436f039790SGreg Kroah-Hartman static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
6144edd16368SStephen M. Cameron 			unsigned char type)
6145edd16368SStephen M. Cameron {
6146edd16368SStephen M. Cameron 	struct Command {
6147edd16368SStephen M. Cameron 		struct CommandListHeader CommandHeader;
6148edd16368SStephen M. Cameron 		struct RequestBlock Request;
6149edd16368SStephen M. Cameron 		struct ErrDescriptor ErrorDescriptor;
6150edd16368SStephen M. Cameron 	};
6151edd16368SStephen M. Cameron 	struct Command *cmd;
6152edd16368SStephen M. Cameron 	static const size_t cmd_sz = sizeof(*cmd) +
6153edd16368SStephen M. Cameron 					sizeof(cmd->ErrorDescriptor);
6154edd16368SStephen M. Cameron 	dma_addr_t paddr64;
61552b08b3e9SDon Brace 	__le32 paddr32;
61562b08b3e9SDon Brace 	u32 tag;
6157edd16368SStephen M. Cameron 	void __iomem *vaddr;
6158edd16368SStephen M. Cameron 	int i, err;
6159edd16368SStephen M. Cameron 
6160edd16368SStephen M. Cameron 	vaddr = pci_ioremap_bar(pdev, 0);
6161edd16368SStephen M. Cameron 	if (vaddr == NULL)
6162edd16368SStephen M. Cameron 		return -ENOMEM;
6163edd16368SStephen M. Cameron 
6164edd16368SStephen M. Cameron 	/* The Inbound Post Queue only accepts 32-bit physical addresses for the
6165edd16368SStephen M. Cameron 	 * CCISS commands, so they must be allocated from the lower 4GiB of
6166edd16368SStephen M. Cameron 	 * memory.
6167edd16368SStephen M. Cameron 	 */
6168edd16368SStephen M. Cameron 	err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
6169edd16368SStephen M. Cameron 	if (err) {
6170edd16368SStephen M. Cameron 		iounmap(vaddr);
61711eaec8f3SRobert Elliott 		return err;
6172edd16368SStephen M. Cameron 	}
6173edd16368SStephen M. Cameron 
6174edd16368SStephen M. Cameron 	cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
6175edd16368SStephen M. Cameron 	if (cmd == NULL) {
6176edd16368SStephen M. Cameron 		iounmap(vaddr);
6177edd16368SStephen M. Cameron 		return -ENOMEM;
6178edd16368SStephen M. Cameron 	}
6179edd16368SStephen M. Cameron 
6180edd16368SStephen M. Cameron 	/* This must fit, because of the 32-bit consistent DMA mask.  Also,
6181edd16368SStephen M. Cameron 	 * although there's no guarantee, we assume that the address is at
6182edd16368SStephen M. Cameron 	 * least 4-byte aligned (most likely, it's page-aligned).
6183edd16368SStephen M. Cameron 	 */
61842b08b3e9SDon Brace 	paddr32 = cpu_to_le32(paddr64);
6185edd16368SStephen M. Cameron 
6186edd16368SStephen M. Cameron 	cmd->CommandHeader.ReplyQueue = 0;
6187edd16368SStephen M. Cameron 	cmd->CommandHeader.SGList = 0;
618850a0decfSStephen M. Cameron 	cmd->CommandHeader.SGTotal = cpu_to_le16(0);
61892b08b3e9SDon Brace 	cmd->CommandHeader.tag = cpu_to_le64(paddr64);
6190edd16368SStephen M. Cameron 	memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
6191edd16368SStephen M. Cameron 
6192edd16368SStephen M. Cameron 	cmd->Request.CDBLen = 16;
6193a505b86fSStephen M. Cameron 	cmd->Request.type_attr_dir =
6194a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_MSG, ATTR_HEADOFQUEUE, XFER_NONE);
6195edd16368SStephen M. Cameron 	cmd->Request.Timeout = 0; /* Don't time out */
6196edd16368SStephen M. Cameron 	cmd->Request.CDB[0] = opcode;
6197edd16368SStephen M. Cameron 	cmd->Request.CDB[1] = type;
6198edd16368SStephen M. Cameron 	memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
619950a0decfSStephen M. Cameron 	cmd->ErrorDescriptor.Addr =
62002b08b3e9SDon Brace 			cpu_to_le64((le32_to_cpu(paddr32) + sizeof(*cmd)));
620150a0decfSStephen M. Cameron 	cmd->ErrorDescriptor.Len = cpu_to_le32(sizeof(struct ErrorInfo));
6202edd16368SStephen M. Cameron 
62032b08b3e9SDon Brace 	writel(le32_to_cpu(paddr32), vaddr + SA5_REQUEST_PORT_OFFSET);
6204edd16368SStephen M. Cameron 
6205edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
6206edd16368SStephen M. Cameron 		tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
62072b08b3e9SDon Brace 		if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr64)
6208edd16368SStephen M. Cameron 			break;
6209edd16368SStephen M. Cameron 		msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
6210edd16368SStephen M. Cameron 	}
6211edd16368SStephen M. Cameron 
6212edd16368SStephen M. Cameron 	iounmap(vaddr);
6213edd16368SStephen M. Cameron 
6214edd16368SStephen M. Cameron 	/* we leak the DMA buffer here ... no choice since the controller could
6215edd16368SStephen M. Cameron 	 *  still complete the command.
6216edd16368SStephen M. Cameron 	 */
6217edd16368SStephen M. Cameron 	if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
6218edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
6219edd16368SStephen M. Cameron 			opcode, type);
6220edd16368SStephen M. Cameron 		return -ETIMEDOUT;
6221edd16368SStephen M. Cameron 	}
6222edd16368SStephen M. Cameron 
6223edd16368SStephen M. Cameron 	pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
6224edd16368SStephen M. Cameron 
6225edd16368SStephen M. Cameron 	if (tag & HPSA_ERROR_BIT) {
6226edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
6227edd16368SStephen M. Cameron 			opcode, type);
6228edd16368SStephen M. Cameron 		return -EIO;
6229edd16368SStephen M. Cameron 	}
6230edd16368SStephen M. Cameron 
6231edd16368SStephen M. Cameron 	dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
6232edd16368SStephen M. Cameron 		opcode, type);
6233edd16368SStephen M. Cameron 	return 0;
6234edd16368SStephen M. Cameron }
6235edd16368SStephen M. Cameron 
6236edd16368SStephen M. Cameron #define hpsa_noop(p) hpsa_message(p, 3, 0)
6237edd16368SStephen M. Cameron 
62381df8552aSStephen M. Cameron static int hpsa_controller_hard_reset(struct pci_dev *pdev,
623942a91641SDon Brace 	void __iomem *vaddr, u32 use_doorbell)
6240edd16368SStephen M. Cameron {
6241edd16368SStephen M. Cameron 
62421df8552aSStephen M. Cameron 	if (use_doorbell) {
62431df8552aSStephen M. Cameron 		/* For everything after the P600, the PCI power state method
62441df8552aSStephen M. Cameron 		 * of resetting the controller doesn't work, so we have this
62451df8552aSStephen M. Cameron 		 * other way using the doorbell register.
6246edd16368SStephen M. Cameron 		 */
62471df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using doorbell to reset controller\n");
6248cf0b08d0SStephen M. Cameron 		writel(use_doorbell, vaddr + SA5_DOORBELL);
624985009239SStephen M. Cameron 
625000701a96SJustin Lindley 		/* PMC hardware guys tell us we need a 10 second delay after
625185009239SStephen M. Cameron 		 * doorbell reset and before any attempt to talk to the board
625285009239SStephen M. Cameron 		 * at all to ensure that this actually works and doesn't fall
625385009239SStephen M. Cameron 		 * over in some weird corner cases.
625485009239SStephen M. Cameron 		 */
625500701a96SJustin Lindley 		msleep(10000);
62561df8552aSStephen M. Cameron 	} else { /* Try to do it the PCI power state way */
6257edd16368SStephen M. Cameron 
6258edd16368SStephen M. Cameron 		/* Quoting from the Open CISS Specification: "The Power
6259edd16368SStephen M. Cameron 		 * Management Control/Status Register (CSR) controls the power
6260edd16368SStephen M. Cameron 		 * state of the device.  The normal operating state is D0,
6261edd16368SStephen M. Cameron 		 * CSR=00h.  The software off state is D3, CSR=03h.  To reset
62621df8552aSStephen M. Cameron 		 * the controller, place the interface device in D3 then to D0,
62631df8552aSStephen M. Cameron 		 * this causes a secondary PCI reset which will reset the
62641df8552aSStephen M. Cameron 		 * controller." */
6265edd16368SStephen M. Cameron 
62662662cab8SDon Brace 		int rc = 0;
62672662cab8SDon Brace 
62681df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using PCI PM to reset controller\n");
62692662cab8SDon Brace 
6270edd16368SStephen M. Cameron 		/* enter the D3hot power management state */
62712662cab8SDon Brace 		rc = pci_set_power_state(pdev, PCI_D3hot);
62722662cab8SDon Brace 		if (rc)
62732662cab8SDon Brace 			return rc;
6274edd16368SStephen M. Cameron 
6275edd16368SStephen M. Cameron 		msleep(500);
6276edd16368SStephen M. Cameron 
6277edd16368SStephen M. Cameron 		/* enter the D0 power management state */
62782662cab8SDon Brace 		rc = pci_set_power_state(pdev, PCI_D0);
62792662cab8SDon Brace 		if (rc)
62802662cab8SDon Brace 			return rc;
6281c4853efeSMike Miller 
6282c4853efeSMike Miller 		/*
6283c4853efeSMike Miller 		 * The P600 requires a small delay when changing states.
6284c4853efeSMike Miller 		 * Otherwise we may think the board did not reset and we bail.
6285c4853efeSMike Miller 		 * This for kdump only and is particular to the P600.
6286c4853efeSMike Miller 		 */
6287c4853efeSMike Miller 		msleep(500);
62881df8552aSStephen M. Cameron 	}
62891df8552aSStephen M. Cameron 	return 0;
62901df8552aSStephen M. Cameron }
62911df8552aSStephen M. Cameron 
62926f039790SGreg Kroah-Hartman static void init_driver_version(char *driver_version, int len)
6293580ada3cSStephen M. Cameron {
6294580ada3cSStephen M. Cameron 	memset(driver_version, 0, len);
6295f79cfec6SStephen M. Cameron 	strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
6296580ada3cSStephen M. Cameron }
6297580ada3cSStephen M. Cameron 
62986f039790SGreg Kroah-Hartman static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
6299580ada3cSStephen M. Cameron {
6300580ada3cSStephen M. Cameron 	char *driver_version;
6301580ada3cSStephen M. Cameron 	int i, size = sizeof(cfgtable->driver_version);
6302580ada3cSStephen M. Cameron 
6303580ada3cSStephen M. Cameron 	driver_version = kmalloc(size, GFP_KERNEL);
6304580ada3cSStephen M. Cameron 	if (!driver_version)
6305580ada3cSStephen M. Cameron 		return -ENOMEM;
6306580ada3cSStephen M. Cameron 
6307580ada3cSStephen M. Cameron 	init_driver_version(driver_version, size);
6308580ada3cSStephen M. Cameron 	for (i = 0; i < size; i++)
6309580ada3cSStephen M. Cameron 		writeb(driver_version[i], &cfgtable->driver_version[i]);
6310580ada3cSStephen M. Cameron 	kfree(driver_version);
6311580ada3cSStephen M. Cameron 	return 0;
6312580ada3cSStephen M. Cameron }
6313580ada3cSStephen M. Cameron 
63146f039790SGreg Kroah-Hartman static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
63156f039790SGreg Kroah-Hartman 					  unsigned char *driver_ver)
6316580ada3cSStephen M. Cameron {
6317580ada3cSStephen M. Cameron 	int i;
6318580ada3cSStephen M. Cameron 
6319580ada3cSStephen M. Cameron 	for (i = 0; i < sizeof(cfgtable->driver_version); i++)
6320580ada3cSStephen M. Cameron 		driver_ver[i] = readb(&cfgtable->driver_version[i]);
6321580ada3cSStephen M. Cameron }
6322580ada3cSStephen M. Cameron 
63236f039790SGreg Kroah-Hartman static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
6324580ada3cSStephen M. Cameron {
6325580ada3cSStephen M. Cameron 
6326580ada3cSStephen M. Cameron 	char *driver_ver, *old_driver_ver;
6327580ada3cSStephen M. Cameron 	int rc, size = sizeof(cfgtable->driver_version);
6328580ada3cSStephen M. Cameron 
6329580ada3cSStephen M. Cameron 	old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
6330580ada3cSStephen M. Cameron 	if (!old_driver_ver)
6331580ada3cSStephen M. Cameron 		return -ENOMEM;
6332580ada3cSStephen M. Cameron 	driver_ver = old_driver_ver + size;
6333580ada3cSStephen M. Cameron 
6334580ada3cSStephen M. Cameron 	/* After a reset, the 32 bytes of "driver version" in the cfgtable
6335580ada3cSStephen M. Cameron 	 * should have been changed, otherwise we know the reset failed.
6336580ada3cSStephen M. Cameron 	 */
6337580ada3cSStephen M. Cameron 	init_driver_version(old_driver_ver, size);
6338580ada3cSStephen M. Cameron 	read_driver_ver_from_cfgtable(cfgtable, driver_ver);
6339580ada3cSStephen M. Cameron 	rc = !memcmp(driver_ver, old_driver_ver, size);
6340580ada3cSStephen M. Cameron 	kfree(old_driver_ver);
6341580ada3cSStephen M. Cameron 	return rc;
6342580ada3cSStephen M. Cameron }
63431df8552aSStephen M. Cameron /* This does a hard reset of the controller using PCI power management
63441df8552aSStephen M. Cameron  * states or the using the doorbell register.
63451df8552aSStephen M. Cameron  */
63466b6c1cd7STomas Henzl static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev, u32 board_id)
63471df8552aSStephen M. Cameron {
63481df8552aSStephen M. Cameron 	u64 cfg_offset;
63491df8552aSStephen M. Cameron 	u32 cfg_base_addr;
63501df8552aSStephen M. Cameron 	u64 cfg_base_addr_index;
63511df8552aSStephen M. Cameron 	void __iomem *vaddr;
63521df8552aSStephen M. Cameron 	unsigned long paddr;
6353580ada3cSStephen M. Cameron 	u32 misc_fw_support;
6354270d05deSStephen M. Cameron 	int rc;
63551df8552aSStephen M. Cameron 	struct CfgTable __iomem *cfgtable;
6356cf0b08d0SStephen M. Cameron 	u32 use_doorbell;
6357270d05deSStephen M. Cameron 	u16 command_register;
63581df8552aSStephen M. Cameron 
63591df8552aSStephen M. Cameron 	/* For controllers as old as the P600, this is very nearly
63601df8552aSStephen M. Cameron 	 * the same thing as
63611df8552aSStephen M. Cameron 	 *
63621df8552aSStephen M. Cameron 	 * pci_save_state(pci_dev);
63631df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D3hot);
63641df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D0);
63651df8552aSStephen M. Cameron 	 * pci_restore_state(pci_dev);
63661df8552aSStephen M. Cameron 	 *
63671df8552aSStephen M. Cameron 	 * For controllers newer than the P600, the pci power state
63681df8552aSStephen M. Cameron 	 * method of resetting doesn't work so we have another way
63691df8552aSStephen M. Cameron 	 * using the doorbell register.
63701df8552aSStephen M. Cameron 	 */
637118867659SStephen M. Cameron 
637260f923b9SRobert Elliott 	if (!ctlr_is_resettable(board_id)) {
637360f923b9SRobert Elliott 		dev_warn(&pdev->dev, "Controller not resettable\n");
637425c1e56aSStephen M. Cameron 		return -ENODEV;
637525c1e56aSStephen M. Cameron 	}
637646380786SStephen M. Cameron 
637746380786SStephen M. Cameron 	/* if controller is soft- but not hard resettable... */
637846380786SStephen M. Cameron 	if (!ctlr_is_hard_resettable(board_id))
637946380786SStephen M. Cameron 		return -ENOTSUPP; /* try soft reset later. */
638018867659SStephen M. Cameron 
6381270d05deSStephen M. Cameron 	/* Save the PCI command register */
6382270d05deSStephen M. Cameron 	pci_read_config_word(pdev, 4, &command_register);
6383270d05deSStephen M. Cameron 	pci_save_state(pdev);
63841df8552aSStephen M. Cameron 
63851df8552aSStephen M. Cameron 	/* find the first memory BAR, so we can find the cfg table */
63861df8552aSStephen M. Cameron 	rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
63871df8552aSStephen M. Cameron 	if (rc)
63881df8552aSStephen M. Cameron 		return rc;
63891df8552aSStephen M. Cameron 	vaddr = remap_pci_mem(paddr, 0x250);
63901df8552aSStephen M. Cameron 	if (!vaddr)
63911df8552aSStephen M. Cameron 		return -ENOMEM;
63921df8552aSStephen M. Cameron 
63931df8552aSStephen M. Cameron 	/* find cfgtable in order to check if reset via doorbell is supported */
63941df8552aSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
63951df8552aSStephen M. Cameron 					&cfg_base_addr_index, &cfg_offset);
63961df8552aSStephen M. Cameron 	if (rc)
63971df8552aSStephen M. Cameron 		goto unmap_vaddr;
63981df8552aSStephen M. Cameron 	cfgtable = remap_pci_mem(pci_resource_start(pdev,
63991df8552aSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
64001df8552aSStephen M. Cameron 	if (!cfgtable) {
64011df8552aSStephen M. Cameron 		rc = -ENOMEM;
64021df8552aSStephen M. Cameron 		goto unmap_vaddr;
64031df8552aSStephen M. Cameron 	}
6404580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(cfgtable);
6405580ada3cSStephen M. Cameron 	if (rc)
640603741d95STomas Henzl 		goto unmap_cfgtable;
64071df8552aSStephen M. Cameron 
6408cf0b08d0SStephen M. Cameron 	/* If reset via doorbell register is supported, use that.
6409cf0b08d0SStephen M. Cameron 	 * There are two such methods.  Favor the newest method.
6410cf0b08d0SStephen M. Cameron 	 */
64111df8552aSStephen M. Cameron 	misc_fw_support = readl(&cfgtable->misc_fw_support);
6412cf0b08d0SStephen M. Cameron 	use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
6413cf0b08d0SStephen M. Cameron 	if (use_doorbell) {
6414cf0b08d0SStephen M. Cameron 		use_doorbell = DOORBELL_CTLR_RESET2;
6415cf0b08d0SStephen M. Cameron 	} else {
64161df8552aSStephen M. Cameron 		use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
6417cf0b08d0SStephen M. Cameron 		if (use_doorbell) {
6418050f7147SStephen Cameron 			dev_warn(&pdev->dev,
6419050f7147SStephen Cameron 				"Soft reset not supported. Firmware update is required.\n");
642064670ac8SStephen M. Cameron 			rc = -ENOTSUPP; /* try soft reset */
6421cf0b08d0SStephen M. Cameron 			goto unmap_cfgtable;
6422cf0b08d0SStephen M. Cameron 		}
6423cf0b08d0SStephen M. Cameron 	}
64241df8552aSStephen M. Cameron 
64251df8552aSStephen M. Cameron 	rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
64261df8552aSStephen M. Cameron 	if (rc)
64271df8552aSStephen M. Cameron 		goto unmap_cfgtable;
6428edd16368SStephen M. Cameron 
6429270d05deSStephen M. Cameron 	pci_restore_state(pdev);
6430270d05deSStephen M. Cameron 	pci_write_config_word(pdev, 4, command_register);
6431edd16368SStephen M. Cameron 
64321df8552aSStephen M. Cameron 	/* Some devices (notably the HP Smart Array 5i Controller)
64331df8552aSStephen M. Cameron 	   need a little pause here */
64341df8552aSStephen M. Cameron 	msleep(HPSA_POST_RESET_PAUSE_MSECS);
64351df8552aSStephen M. Cameron 
6436fe5389c8SStephen M. Cameron 	rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
6437fe5389c8SStephen M. Cameron 	if (rc) {
6438fe5389c8SStephen M. Cameron 		dev_warn(&pdev->dev,
6439050f7147SStephen Cameron 			"Failed waiting for board to become ready after hard reset\n");
6440fe5389c8SStephen M. Cameron 		goto unmap_cfgtable;
6441fe5389c8SStephen M. Cameron 	}
6442fe5389c8SStephen M. Cameron 
6443580ada3cSStephen M. Cameron 	rc = controller_reset_failed(vaddr);
6444580ada3cSStephen M. Cameron 	if (rc < 0)
6445580ada3cSStephen M. Cameron 		goto unmap_cfgtable;
6446580ada3cSStephen M. Cameron 	if (rc) {
644764670ac8SStephen M. Cameron 		dev_warn(&pdev->dev, "Unable to successfully reset "
644864670ac8SStephen M. Cameron 			"controller. Will try soft reset.\n");
644964670ac8SStephen M. Cameron 		rc = -ENOTSUPP;
6450580ada3cSStephen M. Cameron 	} else {
645164670ac8SStephen M. Cameron 		dev_info(&pdev->dev, "board ready after hard reset.\n");
64521df8552aSStephen M. Cameron 	}
64531df8552aSStephen M. Cameron 
64541df8552aSStephen M. Cameron unmap_cfgtable:
64551df8552aSStephen M. Cameron 	iounmap(cfgtable);
64561df8552aSStephen M. Cameron 
64571df8552aSStephen M. Cameron unmap_vaddr:
64581df8552aSStephen M. Cameron 	iounmap(vaddr);
64591df8552aSStephen M. Cameron 	return rc;
6460edd16368SStephen M. Cameron }
6461edd16368SStephen M. Cameron 
6462edd16368SStephen M. Cameron /*
6463edd16368SStephen M. Cameron  *  We cannot read the structure directly, for portability we must use
6464edd16368SStephen M. Cameron  *   the io functions.
6465edd16368SStephen M. Cameron  *   This is for debug only.
6466edd16368SStephen M. Cameron  */
646742a91641SDon Brace static void print_cfg_table(struct device *dev, struct CfgTable __iomem *tb)
6468edd16368SStephen M. Cameron {
646958f8665cSStephen M. Cameron #ifdef HPSA_DEBUG
6470edd16368SStephen M. Cameron 	int i;
6471edd16368SStephen M. Cameron 	char temp_name[17];
6472edd16368SStephen M. Cameron 
6473edd16368SStephen M. Cameron 	dev_info(dev, "Controller Configuration information\n");
6474edd16368SStephen M. Cameron 	dev_info(dev, "------------------------------------\n");
6475edd16368SStephen M. Cameron 	for (i = 0; i < 4; i++)
6476edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->Signature[i]));
6477edd16368SStephen M. Cameron 	temp_name[4] = '\0';
6478edd16368SStephen M. Cameron 	dev_info(dev, "   Signature = %s\n", temp_name);
6479edd16368SStephen M. Cameron 	dev_info(dev, "   Spec Number = %d\n", readl(&(tb->SpecValence)));
6480edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods supported = 0x%x\n",
6481edd16368SStephen M. Cameron 	       readl(&(tb->TransportSupport)));
6482edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods active = 0x%x\n",
6483edd16368SStephen M. Cameron 	       readl(&(tb->TransportActive)));
6484edd16368SStephen M. Cameron 	dev_info(dev, "   Requested transport Method = 0x%x\n",
6485edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.TransportRequest)));
6486edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Delay = 0x%x\n",
6487edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntDelay)));
6488edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Count = 0x%x\n",
6489edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntCount)));
649069d6e33dSRobert Elliott 	dev_info(dev, "   Max outstanding commands = %d\n",
6491edd16368SStephen M. Cameron 	       readl(&(tb->CmdsOutMax)));
6492edd16368SStephen M. Cameron 	dev_info(dev, "   Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
6493edd16368SStephen M. Cameron 	for (i = 0; i < 16; i++)
6494edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->ServerName[i]));
6495edd16368SStephen M. Cameron 	temp_name[16] = '\0';
6496edd16368SStephen M. Cameron 	dev_info(dev, "   Server Name = %s\n", temp_name);
6497edd16368SStephen M. Cameron 	dev_info(dev, "   Heartbeat Counter = 0x%x\n\n\n",
6498edd16368SStephen M. Cameron 		readl(&(tb->HeartBeat)));
6499edd16368SStephen M. Cameron #endif				/* HPSA_DEBUG */
650058f8665cSStephen M. Cameron }
6501edd16368SStephen M. Cameron 
6502edd16368SStephen M. Cameron static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
6503edd16368SStephen M. Cameron {
6504edd16368SStephen M. Cameron 	int i, offset, mem_type, bar_type;
6505edd16368SStephen M. Cameron 
6506edd16368SStephen M. Cameron 	if (pci_bar_addr == PCI_BASE_ADDRESS_0)	/* looking for BAR zero? */
6507edd16368SStephen M. Cameron 		return 0;
6508edd16368SStephen M. Cameron 	offset = 0;
6509edd16368SStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
6510edd16368SStephen M. Cameron 		bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
6511edd16368SStephen M. Cameron 		if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
6512edd16368SStephen M. Cameron 			offset += 4;
6513edd16368SStephen M. Cameron 		else {
6514edd16368SStephen M. Cameron 			mem_type = pci_resource_flags(pdev, i) &
6515edd16368SStephen M. Cameron 			    PCI_BASE_ADDRESS_MEM_TYPE_MASK;
6516edd16368SStephen M. Cameron 			switch (mem_type) {
6517edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_32:
6518edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_1M:
6519edd16368SStephen M. Cameron 				offset += 4;	/* 32 bit */
6520edd16368SStephen M. Cameron 				break;
6521edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_64:
6522edd16368SStephen M. Cameron 				offset += 8;
6523edd16368SStephen M. Cameron 				break;
6524edd16368SStephen M. Cameron 			default:	/* reserved in PCI 2.2 */
6525edd16368SStephen M. Cameron 				dev_warn(&pdev->dev,
6526edd16368SStephen M. Cameron 				       "base address is invalid\n");
6527edd16368SStephen M. Cameron 				return -1;
6528edd16368SStephen M. Cameron 				break;
6529edd16368SStephen M. Cameron 			}
6530edd16368SStephen M. Cameron 		}
6531edd16368SStephen M. Cameron 		if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
6532edd16368SStephen M. Cameron 			return i + 1;
6533edd16368SStephen M. Cameron 	}
6534edd16368SStephen M. Cameron 	return -1;
6535edd16368SStephen M. Cameron }
6536edd16368SStephen M. Cameron 
6537cc64c817SRobert Elliott static void hpsa_disable_interrupt_mode(struct ctlr_info *h)
6538cc64c817SRobert Elliott {
6539cc64c817SRobert Elliott 	if (h->msix_vector) {
6540cc64c817SRobert Elliott 		if (h->pdev->msix_enabled)
6541cc64c817SRobert Elliott 			pci_disable_msix(h->pdev);
6542105a3dbcSRobert Elliott 		h->msix_vector = 0;
6543cc64c817SRobert Elliott 	} else if (h->msi_vector) {
6544cc64c817SRobert Elliott 		if (h->pdev->msi_enabled)
6545cc64c817SRobert Elliott 			pci_disable_msi(h->pdev);
6546105a3dbcSRobert Elliott 		h->msi_vector = 0;
6547cc64c817SRobert Elliott 	}
6548cc64c817SRobert Elliott }
6549cc64c817SRobert Elliott 
6550edd16368SStephen M. Cameron /* If MSI/MSI-X is supported by the kernel we will try to enable it on
6551050f7147SStephen Cameron  * controllers that are capable. If not, we use legacy INTx mode.
6552edd16368SStephen M. Cameron  */
65536f039790SGreg Kroah-Hartman static void hpsa_interrupt_mode(struct ctlr_info *h)
6554edd16368SStephen M. Cameron {
6555edd16368SStephen M. Cameron #ifdef CONFIG_PCI_MSI
6556254f796bSMatt Gates 	int err, i;
6557254f796bSMatt Gates 	struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
6558254f796bSMatt Gates 
6559254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++) {
6560254f796bSMatt Gates 		hpsa_msix_entries[i].vector = 0;
6561254f796bSMatt Gates 		hpsa_msix_entries[i].entry = i;
6562254f796bSMatt Gates 	}
6563edd16368SStephen M. Cameron 
6564edd16368SStephen M. Cameron 	/* Some boards advertise MSI but don't really support it */
65656b3f4c52SStephen M. Cameron 	if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
65666b3f4c52SStephen M. Cameron 	    (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
6567edd16368SStephen M. Cameron 		goto default_int_mode;
656855c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
6569050f7147SStephen Cameron 		dev_info(&h->pdev->dev, "MSI-X capable controller\n");
6570eee0f03aSHannes Reinecke 		h->msix_vector = MAX_REPLY_QUEUES;
6571f89439bcSStephen M. Cameron 		if (h->msix_vector > num_online_cpus())
6572f89439bcSStephen M. Cameron 			h->msix_vector = num_online_cpus();
657318fce3c4SAlexander Gordeev 		err = pci_enable_msix_range(h->pdev, hpsa_msix_entries,
657418fce3c4SAlexander Gordeev 					    1, h->msix_vector);
657518fce3c4SAlexander Gordeev 		if (err < 0) {
657618fce3c4SAlexander Gordeev 			dev_warn(&h->pdev->dev, "MSI-X init failed %d\n", err);
657718fce3c4SAlexander Gordeev 			h->msix_vector = 0;
657818fce3c4SAlexander Gordeev 			goto single_msi_mode;
657918fce3c4SAlexander Gordeev 		} else if (err < h->msix_vector) {
658055c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
6581edd16368SStephen M. Cameron 			       "available\n", err);
6582eee0f03aSHannes Reinecke 		}
658318fce3c4SAlexander Gordeev 		h->msix_vector = err;
6584eee0f03aSHannes Reinecke 		for (i = 0; i < h->msix_vector; i++)
6585eee0f03aSHannes Reinecke 			h->intr[i] = hpsa_msix_entries[i].vector;
6586eee0f03aSHannes Reinecke 		return;
6587edd16368SStephen M. Cameron 	}
658818fce3c4SAlexander Gordeev single_msi_mode:
658955c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
6590050f7147SStephen Cameron 		dev_info(&h->pdev->dev, "MSI capable controller\n");
659155c06c71SStephen M. Cameron 		if (!pci_enable_msi(h->pdev))
6592edd16368SStephen M. Cameron 			h->msi_vector = 1;
6593edd16368SStephen M. Cameron 		else
659455c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "MSI init failed\n");
6595edd16368SStephen M. Cameron 	}
6596edd16368SStephen M. Cameron default_int_mode:
6597edd16368SStephen M. Cameron #endif				/* CONFIG_PCI_MSI */
6598edd16368SStephen M. Cameron 	/* if we get here we're going to use the default interrupt mode */
6599a9a3a273SStephen M. Cameron 	h->intr[h->intr_mode] = h->pdev->irq;
6600edd16368SStephen M. Cameron }
6601edd16368SStephen M. Cameron 
66026f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
6603e5c880d1SStephen M. Cameron {
6604e5c880d1SStephen M. Cameron 	int i;
6605e5c880d1SStephen M. Cameron 	u32 subsystem_vendor_id, subsystem_device_id;
6606e5c880d1SStephen M. Cameron 
6607e5c880d1SStephen M. Cameron 	subsystem_vendor_id = pdev->subsystem_vendor;
6608e5c880d1SStephen M. Cameron 	subsystem_device_id = pdev->subsystem_device;
6609e5c880d1SStephen M. Cameron 	*board_id = ((subsystem_device_id << 16) & 0xffff0000) |
6610e5c880d1SStephen M. Cameron 		    subsystem_vendor_id;
6611e5c880d1SStephen M. Cameron 
6612e5c880d1SStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(products); i++)
6613e5c880d1SStephen M. Cameron 		if (*board_id == products[i].board_id)
6614e5c880d1SStephen M. Cameron 			return i;
6615e5c880d1SStephen M. Cameron 
66166798cc0aSStephen M. Cameron 	if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
66176798cc0aSStephen M. Cameron 		subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
66186798cc0aSStephen M. Cameron 		!hpsa_allow_any) {
6619e5c880d1SStephen M. Cameron 		dev_warn(&pdev->dev, "unrecognized board ID: "
6620e5c880d1SStephen M. Cameron 			"0x%08x, ignoring.\n", *board_id);
6621e5c880d1SStephen M. Cameron 			return -ENODEV;
6622e5c880d1SStephen M. Cameron 	}
6623e5c880d1SStephen M. Cameron 	return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
6624e5c880d1SStephen M. Cameron }
6625e5c880d1SStephen M. Cameron 
66266f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
66273a7774ceSStephen M. Cameron 				    unsigned long *memory_bar)
66283a7774ceSStephen M. Cameron {
66293a7774ceSStephen M. Cameron 	int i;
66303a7774ceSStephen M. Cameron 
66313a7774ceSStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
663212d2cd47SStephen M. Cameron 		if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
66333a7774ceSStephen M. Cameron 			/* addressing mode bits already removed */
663412d2cd47SStephen M. Cameron 			*memory_bar = pci_resource_start(pdev, i);
663512d2cd47SStephen M. Cameron 			dev_dbg(&pdev->dev, "memory BAR = %lx\n",
66363a7774ceSStephen M. Cameron 				*memory_bar);
66373a7774ceSStephen M. Cameron 			return 0;
66383a7774ceSStephen M. Cameron 		}
663912d2cd47SStephen M. Cameron 	dev_warn(&pdev->dev, "no memory BAR found\n");
66403a7774ceSStephen M. Cameron 	return -ENODEV;
66413a7774ceSStephen M. Cameron }
66423a7774ceSStephen M. Cameron 
66436f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
66446f039790SGreg Kroah-Hartman 				     int wait_for_ready)
66452c4c8c8bSStephen M. Cameron {
6646fe5389c8SStephen M. Cameron 	int i, iterations;
66472c4c8c8bSStephen M. Cameron 	u32 scratchpad;
6648fe5389c8SStephen M. Cameron 	if (wait_for_ready)
6649fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_READY_ITERATIONS;
6650fe5389c8SStephen M. Cameron 	else
6651fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
66522c4c8c8bSStephen M. Cameron 
6653fe5389c8SStephen M. Cameron 	for (i = 0; i < iterations; i++) {
6654fe5389c8SStephen M. Cameron 		scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
6655fe5389c8SStephen M. Cameron 		if (wait_for_ready) {
66562c4c8c8bSStephen M. Cameron 			if (scratchpad == HPSA_FIRMWARE_READY)
66572c4c8c8bSStephen M. Cameron 				return 0;
6658fe5389c8SStephen M. Cameron 		} else {
6659fe5389c8SStephen M. Cameron 			if (scratchpad != HPSA_FIRMWARE_READY)
6660fe5389c8SStephen M. Cameron 				return 0;
6661fe5389c8SStephen M. Cameron 		}
66622c4c8c8bSStephen M. Cameron 		msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
66632c4c8c8bSStephen M. Cameron 	}
6664fe5389c8SStephen M. Cameron 	dev_warn(&pdev->dev, "board not ready, timed out.\n");
66652c4c8c8bSStephen M. Cameron 	return -ENODEV;
66662c4c8c8bSStephen M. Cameron }
66672c4c8c8bSStephen M. Cameron 
66686f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
66696f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
6670a51fd47fSStephen M. Cameron 			       u64 *cfg_offset)
6671a51fd47fSStephen M. Cameron {
6672a51fd47fSStephen M. Cameron 	*cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
6673a51fd47fSStephen M. Cameron 	*cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
6674a51fd47fSStephen M. Cameron 	*cfg_base_addr &= (u32) 0x0000ffff;
6675a51fd47fSStephen M. Cameron 	*cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
6676a51fd47fSStephen M. Cameron 	if (*cfg_base_addr_index == -1) {
6677a51fd47fSStephen M. Cameron 		dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
6678a51fd47fSStephen M. Cameron 		return -ENODEV;
6679a51fd47fSStephen M. Cameron 	}
6680a51fd47fSStephen M. Cameron 	return 0;
6681a51fd47fSStephen M. Cameron }
6682a51fd47fSStephen M. Cameron 
6683195f2c65SRobert Elliott static void hpsa_free_cfgtables(struct ctlr_info *h)
6684195f2c65SRobert Elliott {
6685105a3dbcSRobert Elliott 	if (h->transtable) {
6686195f2c65SRobert Elliott 		iounmap(h->transtable);
6687105a3dbcSRobert Elliott 		h->transtable = NULL;
6688105a3dbcSRobert Elliott 	}
6689105a3dbcSRobert Elliott 	if (h->cfgtable) {
6690195f2c65SRobert Elliott 		iounmap(h->cfgtable);
6691105a3dbcSRobert Elliott 		h->cfgtable = NULL;
6692105a3dbcSRobert Elliott 	}
6693195f2c65SRobert Elliott }
6694195f2c65SRobert Elliott 
6695195f2c65SRobert Elliott /* Find and map CISS config table and transfer table
6696195f2c65SRobert Elliott + * several items must be unmapped (freed) later
6697195f2c65SRobert Elliott + * */
66986f039790SGreg Kroah-Hartman static int hpsa_find_cfgtables(struct ctlr_info *h)
6699edd16368SStephen M. Cameron {
670001a02ffcSStephen M. Cameron 	u64 cfg_offset;
670101a02ffcSStephen M. Cameron 	u32 cfg_base_addr;
670201a02ffcSStephen M. Cameron 	u64 cfg_base_addr_index;
6703303932fdSDon Brace 	u32 trans_offset;
6704a51fd47fSStephen M. Cameron 	int rc;
670577c4495cSStephen M. Cameron 
6706a51fd47fSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
6707a51fd47fSStephen M. Cameron 		&cfg_base_addr_index, &cfg_offset);
6708a51fd47fSStephen M. Cameron 	if (rc)
6709a51fd47fSStephen M. Cameron 		return rc;
671077c4495cSStephen M. Cameron 	h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
6711a51fd47fSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
6712cd3c81c4SRobert Elliott 	if (!h->cfgtable) {
6713cd3c81c4SRobert Elliott 		dev_err(&h->pdev->dev, "Failed mapping cfgtable\n");
671477c4495cSStephen M. Cameron 		return -ENOMEM;
6715cd3c81c4SRobert Elliott 	}
6716580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(h->cfgtable);
6717580ada3cSStephen M. Cameron 	if (rc)
6718580ada3cSStephen M. Cameron 		return rc;
671977c4495cSStephen M. Cameron 	/* Find performant mode table. */
6720a51fd47fSStephen M. Cameron 	trans_offset = readl(&h->cfgtable->TransMethodOffset);
672177c4495cSStephen M. Cameron 	h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
672277c4495cSStephen M. Cameron 				cfg_base_addr_index)+cfg_offset+trans_offset,
672377c4495cSStephen M. Cameron 				sizeof(*h->transtable));
6724195f2c65SRobert Elliott 	if (!h->transtable) {
6725195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "Failed mapping transfer table\n");
6726195f2c65SRobert Elliott 		hpsa_free_cfgtables(h);
672777c4495cSStephen M. Cameron 		return -ENOMEM;
6728195f2c65SRobert Elliott 	}
672977c4495cSStephen M. Cameron 	return 0;
673077c4495cSStephen M. Cameron }
673177c4495cSStephen M. Cameron 
67326f039790SGreg Kroah-Hartman static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
6733cba3d38bSStephen M. Cameron {
673441ce4c35SStephen Cameron #define MIN_MAX_COMMANDS 16
673541ce4c35SStephen Cameron 	BUILD_BUG_ON(MIN_MAX_COMMANDS <= HPSA_NRESERVED_CMDS);
673641ce4c35SStephen Cameron 
673741ce4c35SStephen Cameron 	h->max_commands = readl(&h->cfgtable->MaxPerformantModeCommands);
673872ceeaecSStephen M. Cameron 
673972ceeaecSStephen M. Cameron 	/* Limit commands in memory limited kdump scenario. */
674072ceeaecSStephen M. Cameron 	if (reset_devices && h->max_commands > 32)
674172ceeaecSStephen M. Cameron 		h->max_commands = 32;
674272ceeaecSStephen M. Cameron 
674341ce4c35SStephen Cameron 	if (h->max_commands < MIN_MAX_COMMANDS) {
674441ce4c35SStephen Cameron 		dev_warn(&h->pdev->dev,
674541ce4c35SStephen Cameron 			"Controller reports max supported commands of %d Using %d instead. Ensure that firmware is up to date.\n",
674641ce4c35SStephen Cameron 			h->max_commands,
674741ce4c35SStephen Cameron 			MIN_MAX_COMMANDS);
674841ce4c35SStephen Cameron 		h->max_commands = MIN_MAX_COMMANDS;
6749cba3d38bSStephen M. Cameron 	}
6750cba3d38bSStephen M. Cameron }
6751cba3d38bSStephen M. Cameron 
6752c7ee65b3SWebb Scales /* If the controller reports that the total max sg entries is greater than 512,
6753c7ee65b3SWebb Scales  * then we know that chained SG blocks work.  (Original smart arrays did not
6754c7ee65b3SWebb Scales  * support chained SG blocks and would return zero for max sg entries.)
6755c7ee65b3SWebb Scales  */
6756c7ee65b3SWebb Scales static int hpsa_supports_chained_sg_blocks(struct ctlr_info *h)
6757c7ee65b3SWebb Scales {
6758c7ee65b3SWebb Scales 	return h->maxsgentries > 512;
6759c7ee65b3SWebb Scales }
6760c7ee65b3SWebb Scales 
6761b93d7536SStephen M. Cameron /* Interrogate the hardware for some limits:
6762b93d7536SStephen M. Cameron  * max commands, max SG elements without chaining, and with chaining,
6763b93d7536SStephen M. Cameron  * SG chain block size, etc.
6764b93d7536SStephen M. Cameron  */
67656f039790SGreg Kroah-Hartman static void hpsa_find_board_params(struct ctlr_info *h)
6766b93d7536SStephen M. Cameron {
6767cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
676845fcb86eSStephen Cameron 	h->nr_cmds = h->max_commands;
6769b93d7536SStephen M. Cameron 	h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
6770283b4a9bSStephen M. Cameron 	h->fw_support = readl(&(h->cfgtable->misc_fw_support));
6771c7ee65b3SWebb Scales 	if (hpsa_supports_chained_sg_blocks(h)) {
6772c7ee65b3SWebb Scales 		/* Limit in-command s/g elements to 32 save dma'able memory. */
6773b93d7536SStephen M. Cameron 		h->max_cmd_sg_entries = 32;
67741a63ea6fSWebb Scales 		h->chainsize = h->maxsgentries - h->max_cmd_sg_entries;
6775b93d7536SStephen M. Cameron 		h->maxsgentries--; /* save one for chain pointer */
6776b93d7536SStephen M. Cameron 	} else {
6777c7ee65b3SWebb Scales 		/*
6778c7ee65b3SWebb Scales 		 * Original smart arrays supported at most 31 s/g entries
6779c7ee65b3SWebb Scales 		 * embedded inline in the command (trying to use more
6780c7ee65b3SWebb Scales 		 * would lock up the controller)
6781c7ee65b3SWebb Scales 		 */
6782c7ee65b3SWebb Scales 		h->max_cmd_sg_entries = 31;
67831a63ea6fSWebb Scales 		h->maxsgentries = 31; /* default to traditional values */
6784c7ee65b3SWebb Scales 		h->chainsize = 0;
6785b93d7536SStephen M. Cameron 	}
678675167d2cSStephen M. Cameron 
678775167d2cSStephen M. Cameron 	/* Find out what task management functions are supported and cache */
678875167d2cSStephen M. Cameron 	h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
67890e7a7fceSScott Teel 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
67900e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
67910e7a7fceSScott Teel 	if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
67920e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
6793*8be986ccSStephen Cameron 	if (!(HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags))
6794*8be986ccSStephen Cameron 		dev_warn(&h->pdev->dev, "HP SSD Smart Path aborts not supported\n");
6795b93d7536SStephen M. Cameron }
6796b93d7536SStephen M. Cameron 
679776c46e49SStephen M. Cameron static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
679876c46e49SStephen M. Cameron {
67990fc9fd40SAkinobu Mita 	if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
6800050f7147SStephen Cameron 		dev_err(&h->pdev->dev, "not a valid CISS config table\n");
680176c46e49SStephen M. Cameron 		return false;
680276c46e49SStephen M. Cameron 	}
680376c46e49SStephen M. Cameron 	return true;
680476c46e49SStephen M. Cameron }
680576c46e49SStephen M. Cameron 
680697a5e98cSStephen M. Cameron static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
6807f7c39101SStephen M. Cameron {
680897a5e98cSStephen M. Cameron 	u32 driver_support;
6809f7c39101SStephen M. Cameron 
681097a5e98cSStephen M. Cameron 	driver_support = readl(&(h->cfgtable->driver_support));
68110b9e7b74SArnd Bergmann 	/* Need to enable prefetch in the SCSI core for 6400 in x86 */
68120b9e7b74SArnd Bergmann #ifdef CONFIG_X86
681397a5e98cSStephen M. Cameron 	driver_support |= ENABLE_SCSI_PREFETCH;
6814f7c39101SStephen M. Cameron #endif
681528e13446SStephen M. Cameron 	driver_support |= ENABLE_UNIT_ATTN;
681628e13446SStephen M. Cameron 	writel(driver_support, &(h->cfgtable->driver_support));
6817f7c39101SStephen M. Cameron }
6818f7c39101SStephen M. Cameron 
68193d0eab67SStephen M. Cameron /* Disable DMA prefetch for the P600.  Otherwise an ASIC bug may result
68203d0eab67SStephen M. Cameron  * in a prefetch beyond physical memory.
68213d0eab67SStephen M. Cameron  */
68223d0eab67SStephen M. Cameron static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
68233d0eab67SStephen M. Cameron {
68243d0eab67SStephen M. Cameron 	u32 dma_prefetch;
68253d0eab67SStephen M. Cameron 
68263d0eab67SStephen M. Cameron 	if (h->board_id != 0x3225103C)
68273d0eab67SStephen M. Cameron 		return;
68283d0eab67SStephen M. Cameron 	dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
68293d0eab67SStephen M. Cameron 	dma_prefetch |= 0x8000;
68303d0eab67SStephen M. Cameron 	writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
68313d0eab67SStephen M. Cameron }
68323d0eab67SStephen M. Cameron 
6833c706a795SRobert Elliott static int hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
683476438d08SStephen M. Cameron {
683576438d08SStephen M. Cameron 	int i;
683676438d08SStephen M. Cameron 	u32 doorbell_value;
683776438d08SStephen M. Cameron 	unsigned long flags;
683876438d08SStephen M. Cameron 	/* wait until the clear_event_notify bit 6 is cleared by controller. */
6839007e7aa9SRobert Elliott 	for (i = 0; i < MAX_CLEAR_EVENT_WAIT; i++) {
684076438d08SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
684176438d08SStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
684276438d08SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
684376438d08SStephen M. Cameron 		if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
6844c706a795SRobert Elliott 			goto done;
684576438d08SStephen M. Cameron 		/* delay and try again */
6846007e7aa9SRobert Elliott 		msleep(CLEAR_EVENT_WAIT_INTERVAL);
684776438d08SStephen M. Cameron 	}
6848c706a795SRobert Elliott 	return -ENODEV;
6849c706a795SRobert Elliott done:
6850c706a795SRobert Elliott 	return 0;
685176438d08SStephen M. Cameron }
685276438d08SStephen M. Cameron 
6853c706a795SRobert Elliott static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
6854eb6b2ae9SStephen M. Cameron {
6855eb6b2ae9SStephen M. Cameron 	int i;
68566eaf46fdSStephen M. Cameron 	u32 doorbell_value;
68576eaf46fdSStephen M. Cameron 	unsigned long flags;
6858eb6b2ae9SStephen M. Cameron 
6859eb6b2ae9SStephen M. Cameron 	/* under certain very rare conditions, this can take awhile.
6860eb6b2ae9SStephen M. Cameron 	 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
6861eb6b2ae9SStephen M. Cameron 	 * as we enter this code.)
6862eb6b2ae9SStephen M. Cameron 	 */
6863007e7aa9SRobert Elliott 	for (i = 0; i < MAX_MODE_CHANGE_WAIT; i++) {
686425163bd5SWebb Scales 		if (h->remove_in_progress)
686525163bd5SWebb Scales 			goto done;
68666eaf46fdSStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
68676eaf46fdSStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
68686eaf46fdSStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
6869382be668SDan Carpenter 		if (!(doorbell_value & CFGTBL_ChangeReq))
6870c706a795SRobert Elliott 			goto done;
6871eb6b2ae9SStephen M. Cameron 		/* delay and try again */
6872007e7aa9SRobert Elliott 		msleep(MODE_CHANGE_WAIT_INTERVAL);
6873eb6b2ae9SStephen M. Cameron 	}
6874c706a795SRobert Elliott 	return -ENODEV;
6875c706a795SRobert Elliott done:
6876c706a795SRobert Elliott 	return 0;
68773f4336f3SStephen M. Cameron }
68783f4336f3SStephen M. Cameron 
6879c706a795SRobert Elliott /* return -ENODEV or other reason on error, 0 on success */
68806f039790SGreg Kroah-Hartman static int hpsa_enter_simple_mode(struct ctlr_info *h)
68813f4336f3SStephen M. Cameron {
68823f4336f3SStephen M. Cameron 	u32 trans_support;
68833f4336f3SStephen M. Cameron 
68843f4336f3SStephen M. Cameron 	trans_support = readl(&(h->cfgtable->TransportSupport));
68853f4336f3SStephen M. Cameron 	if (!(trans_support & SIMPLE_MODE))
68863f4336f3SStephen M. Cameron 		return -ENOTSUPP;
68873f4336f3SStephen M. Cameron 
68883f4336f3SStephen M. Cameron 	h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
6889283b4a9bSStephen M. Cameron 
68903f4336f3SStephen M. Cameron 	/* Update the field, and then ring the doorbell */
68913f4336f3SStephen M. Cameron 	writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
6892b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
68933f4336f3SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
6894c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h))
6895c706a795SRobert Elliott 		goto error;
6896eb6b2ae9SStephen M. Cameron 	print_cfg_table(&h->pdev->dev, h->cfgtable);
6897283b4a9bSStephen M. Cameron 	if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
6898283b4a9bSStephen M. Cameron 		goto error;
6899960a30e7SStephen M. Cameron 	h->transMethod = CFGTBL_Trans_Simple;
6900eb6b2ae9SStephen M. Cameron 	return 0;
6901283b4a9bSStephen M. Cameron error:
6902050f7147SStephen Cameron 	dev_err(&h->pdev->dev, "failed to enter simple mode\n");
6903283b4a9bSStephen M. Cameron 	return -ENODEV;
6904eb6b2ae9SStephen M. Cameron }
6905eb6b2ae9SStephen M. Cameron 
6906195f2c65SRobert Elliott /* free items allocated or mapped by hpsa_pci_init */
6907195f2c65SRobert Elliott static void hpsa_free_pci_init(struct ctlr_info *h)
6908195f2c65SRobert Elliott {
6909195f2c65SRobert Elliott 	hpsa_free_cfgtables(h);			/* pci_init 4 */
6910195f2c65SRobert Elliott 	iounmap(h->vaddr);			/* pci_init 3 */
6911105a3dbcSRobert Elliott 	h->vaddr = NULL;
6912195f2c65SRobert Elliott 	hpsa_disable_interrupt_mode(h);		/* pci_init 2 */
6913195f2c65SRobert Elliott 	pci_release_regions(h->pdev);		/* pci_init 2 */
6914195f2c65SRobert Elliott 	pci_disable_device(h->pdev);		/* pci_init 1 */
6915195f2c65SRobert Elliott }
6916195f2c65SRobert Elliott 
6917195f2c65SRobert Elliott /* several items must be freed later */
69186f039790SGreg Kroah-Hartman static int hpsa_pci_init(struct ctlr_info *h)
691977c4495cSStephen M. Cameron {
6920eb6b2ae9SStephen M. Cameron 	int prod_index, err;
6921edd16368SStephen M. Cameron 
6922e5c880d1SStephen M. Cameron 	prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
6923e5c880d1SStephen M. Cameron 	if (prod_index < 0)
692460f923b9SRobert Elliott 		return prod_index;
6925e5c880d1SStephen M. Cameron 	h->product_name = products[prod_index].product_name;
6926e5c880d1SStephen M. Cameron 	h->access = *(products[prod_index].access);
6927e5c880d1SStephen M. Cameron 
69289b5c48c2SStephen Cameron 	h->needs_abort_tags_swizzled =
69299b5c48c2SStephen Cameron 		ctlr_needs_abort_tags_swizzled(h->board_id);
69309b5c48c2SStephen Cameron 
6931e5a44df8SMatthew Garrett 	pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
6932e5a44df8SMatthew Garrett 			       PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
6933e5a44df8SMatthew Garrett 
693455c06c71SStephen M. Cameron 	err = pci_enable_device(h->pdev);
6935edd16368SStephen M. Cameron 	if (err) {
6936195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "failed to enable PCI device\n");
6937edd16368SStephen M. Cameron 		return err;
6938edd16368SStephen M. Cameron 	}
6939edd16368SStephen M. Cameron 
6940f79cfec6SStephen M. Cameron 	err = pci_request_regions(h->pdev, HPSA);
6941edd16368SStephen M. Cameron 	if (err) {
694255c06c71SStephen M. Cameron 		dev_err(&h->pdev->dev,
6943195f2c65SRobert Elliott 			"failed to obtain PCI resources\n");
6944195f2c65SRobert Elliott 		goto clean1;	/* pci */
6945edd16368SStephen M. Cameron 	}
69464fa604e1SRobert Elliott 
69474fa604e1SRobert Elliott 	pci_set_master(h->pdev);
69484fa604e1SRobert Elliott 
69496b3f4c52SStephen M. Cameron 	hpsa_interrupt_mode(h);
695012d2cd47SStephen M. Cameron 	err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
69513a7774ceSStephen M. Cameron 	if (err)
6952195f2c65SRobert Elliott 		goto clean2;	/* intmode+region, pci */
6953edd16368SStephen M. Cameron 	h->vaddr = remap_pci_mem(h->paddr, 0x250);
6954204892e9SStephen M. Cameron 	if (!h->vaddr) {
6955195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "failed to remap PCI mem\n");
6956204892e9SStephen M. Cameron 		err = -ENOMEM;
6957195f2c65SRobert Elliott 		goto clean2;	/* intmode+region, pci */
6958204892e9SStephen M. Cameron 	}
6959fe5389c8SStephen M. Cameron 	err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
69602c4c8c8bSStephen M. Cameron 	if (err)
6961195f2c65SRobert Elliott 		goto clean3;	/* vaddr, intmode+region, pci */
696277c4495cSStephen M. Cameron 	err = hpsa_find_cfgtables(h);
696377c4495cSStephen M. Cameron 	if (err)
6964195f2c65SRobert Elliott 		goto clean3;	/* vaddr, intmode+region, pci */
6965b93d7536SStephen M. Cameron 	hpsa_find_board_params(h);
6966edd16368SStephen M. Cameron 
696776c46e49SStephen M. Cameron 	if (!hpsa_CISS_signature_present(h)) {
6968edd16368SStephen M. Cameron 		err = -ENODEV;
6969195f2c65SRobert Elliott 		goto clean4;	/* cfgtables, vaddr, intmode+region, pci */
6970edd16368SStephen M. Cameron 	}
697197a5e98cSStephen M. Cameron 	hpsa_set_driver_support_bits(h);
69723d0eab67SStephen M. Cameron 	hpsa_p600_dma_prefetch_quirk(h);
6973eb6b2ae9SStephen M. Cameron 	err = hpsa_enter_simple_mode(h);
6974eb6b2ae9SStephen M. Cameron 	if (err)
6975195f2c65SRobert Elliott 		goto clean4;	/* cfgtables, vaddr, intmode+region, pci */
6976edd16368SStephen M. Cameron 	return 0;
6977edd16368SStephen M. Cameron 
6978195f2c65SRobert Elliott clean4:	/* cfgtables, vaddr, intmode+region, pci */
6979195f2c65SRobert Elliott 	hpsa_free_cfgtables(h);
6980195f2c65SRobert Elliott clean3:	/* vaddr, intmode+region, pci */
6981204892e9SStephen M. Cameron 	iounmap(h->vaddr);
6982105a3dbcSRobert Elliott 	h->vaddr = NULL;
6983195f2c65SRobert Elliott clean2:	/* intmode+region, pci */
6984195f2c65SRobert Elliott 	hpsa_disable_interrupt_mode(h);
698555c06c71SStephen M. Cameron 	pci_release_regions(h->pdev);
6986195f2c65SRobert Elliott clean1:	/* pci */
6987195f2c65SRobert Elliott 	pci_disable_device(h->pdev);
6988edd16368SStephen M. Cameron 	return err;
6989edd16368SStephen M. Cameron }
6990edd16368SStephen M. Cameron 
69916f039790SGreg Kroah-Hartman static void hpsa_hba_inquiry(struct ctlr_info *h)
6992339b2b14SStephen M. Cameron {
6993339b2b14SStephen M. Cameron 	int rc;
6994339b2b14SStephen M. Cameron 
6995339b2b14SStephen M. Cameron #define HBA_INQUIRY_BYTE_COUNT 64
6996339b2b14SStephen M. Cameron 	h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
6997339b2b14SStephen M. Cameron 	if (!h->hba_inquiry_data)
6998339b2b14SStephen M. Cameron 		return;
6999339b2b14SStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
7000339b2b14SStephen M. Cameron 		h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
7001339b2b14SStephen M. Cameron 	if (rc != 0) {
7002339b2b14SStephen M. Cameron 		kfree(h->hba_inquiry_data);
7003339b2b14SStephen M. Cameron 		h->hba_inquiry_data = NULL;
7004339b2b14SStephen M. Cameron 	}
7005339b2b14SStephen M. Cameron }
7006339b2b14SStephen M. Cameron 
70076b6c1cd7STomas Henzl static int hpsa_init_reset_devices(struct pci_dev *pdev, u32 board_id)
7008edd16368SStephen M. Cameron {
70091df8552aSStephen M. Cameron 	int rc, i;
70103b747298STomas Henzl 	void __iomem *vaddr;
7011edd16368SStephen M. Cameron 
70124c2a8c40SStephen M. Cameron 	if (!reset_devices)
70134c2a8c40SStephen M. Cameron 		return 0;
70144c2a8c40SStephen M. Cameron 
7015132aa220STomas Henzl 	/* kdump kernel is loading, we don't know in which state is
7016132aa220STomas Henzl 	 * the pci interface. The dev->enable_cnt is equal zero
7017132aa220STomas Henzl 	 * so we call enable+disable, wait a while and switch it on.
7018132aa220STomas Henzl 	 */
7019132aa220STomas Henzl 	rc = pci_enable_device(pdev);
7020132aa220STomas Henzl 	if (rc) {
7021132aa220STomas Henzl 		dev_warn(&pdev->dev, "Failed to enable PCI device\n");
7022132aa220STomas Henzl 		return -ENODEV;
7023132aa220STomas Henzl 	}
7024132aa220STomas Henzl 	pci_disable_device(pdev);
7025132aa220STomas Henzl 	msleep(260);			/* a randomly chosen number */
7026132aa220STomas Henzl 	rc = pci_enable_device(pdev);
7027132aa220STomas Henzl 	if (rc) {
7028132aa220STomas Henzl 		dev_warn(&pdev->dev, "failed to enable device.\n");
7029132aa220STomas Henzl 		return -ENODEV;
7030132aa220STomas Henzl 	}
70314fa604e1SRobert Elliott 
7032859c75abSTomas Henzl 	pci_set_master(pdev);
70334fa604e1SRobert Elliott 
70343b747298STomas Henzl 	vaddr = pci_ioremap_bar(pdev, 0);
70353b747298STomas Henzl 	if (vaddr == NULL) {
70363b747298STomas Henzl 		rc = -ENOMEM;
70373b747298STomas Henzl 		goto out_disable;
70383b747298STomas Henzl 	}
70393b747298STomas Henzl 	writel(SA5_INTR_OFF, vaddr + SA5_REPLY_INTR_MASK_OFFSET);
70403b747298STomas Henzl 	iounmap(vaddr);
70413b747298STomas Henzl 
70421df8552aSStephen M. Cameron 	/* Reset the controller with a PCI power-cycle or via doorbell */
70436b6c1cd7STomas Henzl 	rc = hpsa_kdump_hard_reset_controller(pdev, board_id);
7044edd16368SStephen M. Cameron 
70451df8552aSStephen M. Cameron 	/* -ENOTSUPP here means we cannot reset the controller
70461df8552aSStephen M. Cameron 	 * but it's already (and still) up and running in
704718867659SStephen M. Cameron 	 * "performant mode".  Or, it might be 640x, which can't reset
704818867659SStephen M. Cameron 	 * due to concerns about shared bbwc between 6402/6404 pair.
70491df8552aSStephen M. Cameron 	 */
7050adf1b3a3SRobert Elliott 	if (rc)
7051132aa220STomas Henzl 		goto out_disable;
7052edd16368SStephen M. Cameron 
7053edd16368SStephen M. Cameron 	/* Now try to get the controller to respond to a no-op */
70541ba66c9cSRobert Elliott 	dev_info(&pdev->dev, "Waiting for controller to respond to no-op\n");
7055edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
7056edd16368SStephen M. Cameron 		if (hpsa_noop(pdev) == 0)
7057edd16368SStephen M. Cameron 			break;
7058edd16368SStephen M. Cameron 		else
7059edd16368SStephen M. Cameron 			dev_warn(&pdev->dev, "no-op failed%s\n",
7060edd16368SStephen M. Cameron 					(i < 11 ? "; re-trying" : ""));
7061edd16368SStephen M. Cameron 	}
7062132aa220STomas Henzl 
7063132aa220STomas Henzl out_disable:
7064132aa220STomas Henzl 
7065132aa220STomas Henzl 	pci_disable_device(pdev);
7066132aa220STomas Henzl 	return rc;
7067edd16368SStephen M. Cameron }
7068edd16368SStephen M. Cameron 
70691fb7c98aSRobert Elliott static void hpsa_free_cmd_pool(struct ctlr_info *h)
70701fb7c98aSRobert Elliott {
70711fb7c98aSRobert Elliott 	kfree(h->cmd_pool_bits);
7072105a3dbcSRobert Elliott 	h->cmd_pool_bits = NULL;
7073105a3dbcSRobert Elliott 	if (h->cmd_pool) {
70741fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
70751fb7c98aSRobert Elliott 				h->nr_cmds * sizeof(struct CommandList),
70761fb7c98aSRobert Elliott 				h->cmd_pool,
70771fb7c98aSRobert Elliott 				h->cmd_pool_dhandle);
7078105a3dbcSRobert Elliott 		h->cmd_pool = NULL;
7079105a3dbcSRobert Elliott 		h->cmd_pool_dhandle = 0;
7080105a3dbcSRobert Elliott 	}
7081105a3dbcSRobert Elliott 	if (h->errinfo_pool) {
70821fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
70831fb7c98aSRobert Elliott 				h->nr_cmds * sizeof(struct ErrorInfo),
70841fb7c98aSRobert Elliott 				h->errinfo_pool,
70851fb7c98aSRobert Elliott 				h->errinfo_pool_dhandle);
7086105a3dbcSRobert Elliott 		h->errinfo_pool = NULL;
7087105a3dbcSRobert Elliott 		h->errinfo_pool_dhandle = 0;
7088105a3dbcSRobert Elliott 	}
70891fb7c98aSRobert Elliott }
70901fb7c98aSRobert Elliott 
7091d37ffbe4SRobert Elliott static int hpsa_alloc_cmd_pool(struct ctlr_info *h)
70922e9d1b36SStephen M. Cameron {
70932e9d1b36SStephen M. Cameron 	h->cmd_pool_bits = kzalloc(
70942e9d1b36SStephen M. Cameron 		DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
70952e9d1b36SStephen M. Cameron 		sizeof(unsigned long), GFP_KERNEL);
70962e9d1b36SStephen M. Cameron 	h->cmd_pool = pci_alloc_consistent(h->pdev,
70972e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->cmd_pool),
70982e9d1b36SStephen M. Cameron 		    &(h->cmd_pool_dhandle));
70992e9d1b36SStephen M. Cameron 	h->errinfo_pool = pci_alloc_consistent(h->pdev,
71002e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->errinfo_pool),
71012e9d1b36SStephen M. Cameron 		    &(h->errinfo_pool_dhandle));
71022e9d1b36SStephen M. Cameron 	if ((h->cmd_pool_bits == NULL)
71032e9d1b36SStephen M. Cameron 	    || (h->cmd_pool == NULL)
71042e9d1b36SStephen M. Cameron 	    || (h->errinfo_pool == NULL)) {
71052e9d1b36SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory in %s", __func__);
71062c143342SRobert Elliott 		goto clean_up;
71072e9d1b36SStephen M. Cameron 	}
7108360c73bdSStephen Cameron 	hpsa_preinitialize_commands(h);
71092e9d1b36SStephen M. Cameron 	return 0;
71102c143342SRobert Elliott clean_up:
71112c143342SRobert Elliott 	hpsa_free_cmd_pool(h);
71122c143342SRobert Elliott 	return -ENOMEM;
71132e9d1b36SStephen M. Cameron }
71142e9d1b36SStephen M. Cameron 
711541b3cf08SStephen M. Cameron static void hpsa_irq_affinity_hints(struct ctlr_info *h)
711641b3cf08SStephen M. Cameron {
7117ec429952SFabian Frederick 	int i, cpu;
711841b3cf08SStephen M. Cameron 
711941b3cf08SStephen M. Cameron 	cpu = cpumask_first(cpu_online_mask);
712041b3cf08SStephen M. Cameron 	for (i = 0; i < h->msix_vector; i++) {
7121ec429952SFabian Frederick 		irq_set_affinity_hint(h->intr[i], get_cpu_mask(cpu));
712241b3cf08SStephen M. Cameron 		cpu = cpumask_next(cpu, cpu_online_mask);
712341b3cf08SStephen M. Cameron 	}
712441b3cf08SStephen M. Cameron }
712541b3cf08SStephen M. Cameron 
7126ec501a18SRobert Elliott /* clear affinity hints and free MSI-X, MSI, or legacy INTx vectors */
7127ec501a18SRobert Elliott static void hpsa_free_irqs(struct ctlr_info *h)
7128ec501a18SRobert Elliott {
7129ec501a18SRobert Elliott 	int i;
7130ec501a18SRobert Elliott 
7131ec501a18SRobert Elliott 	if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
7132ec501a18SRobert Elliott 		/* Single reply queue, only one irq to free */
7133ec501a18SRobert Elliott 		i = h->intr_mode;
7134ec501a18SRobert Elliott 		irq_set_affinity_hint(h->intr[i], NULL);
7135ec501a18SRobert Elliott 		free_irq(h->intr[i], &h->q[i]);
7136105a3dbcSRobert Elliott 		h->q[i] = 0;
7137ec501a18SRobert Elliott 		return;
7138ec501a18SRobert Elliott 	}
7139ec501a18SRobert Elliott 
7140ec501a18SRobert Elliott 	for (i = 0; i < h->msix_vector; i++) {
7141ec501a18SRobert Elliott 		irq_set_affinity_hint(h->intr[i], NULL);
7142ec501a18SRobert Elliott 		free_irq(h->intr[i], &h->q[i]);
7143105a3dbcSRobert Elliott 		h->q[i] = 0;
7144ec501a18SRobert Elliott 	}
7145a4e17fc1SRobert Elliott 	for (; i < MAX_REPLY_QUEUES; i++)
7146a4e17fc1SRobert Elliott 		h->q[i] = 0;
7147ec501a18SRobert Elliott }
7148ec501a18SRobert Elliott 
71499ee61794SRobert Elliott /* returns 0 on success; cleans up and returns -Enn on error */
71509ee61794SRobert Elliott static int hpsa_request_irqs(struct ctlr_info *h,
71510ae01a32SStephen M. Cameron 	irqreturn_t (*msixhandler)(int, void *),
71520ae01a32SStephen M. Cameron 	irqreturn_t (*intxhandler)(int, void *))
71530ae01a32SStephen M. Cameron {
7154254f796bSMatt Gates 	int rc, i;
71550ae01a32SStephen M. Cameron 
7156254f796bSMatt Gates 	/*
7157254f796bSMatt Gates 	 * initialize h->q[x] = x so that interrupt handlers know which
7158254f796bSMatt Gates 	 * queue to process.
7159254f796bSMatt Gates 	 */
7160254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++)
7161254f796bSMatt Gates 		h->q[i] = (u8) i;
7162254f796bSMatt Gates 
7163eee0f03aSHannes Reinecke 	if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
7164254f796bSMatt Gates 		/* If performant mode and MSI-X, use multiple reply queues */
7165a4e17fc1SRobert Elliott 		for (i = 0; i < h->msix_vector; i++) {
7166254f796bSMatt Gates 			rc = request_irq(h->intr[i], msixhandler,
7167254f796bSMatt Gates 					0, h->devname,
7168254f796bSMatt Gates 					&h->q[i]);
7169a4e17fc1SRobert Elliott 			if (rc) {
7170a4e17fc1SRobert Elliott 				int j;
7171a4e17fc1SRobert Elliott 
7172a4e17fc1SRobert Elliott 				dev_err(&h->pdev->dev,
7173a4e17fc1SRobert Elliott 					"failed to get irq %d for %s\n",
7174a4e17fc1SRobert Elliott 				       h->intr[i], h->devname);
7175a4e17fc1SRobert Elliott 				for (j = 0; j < i; j++) {
7176a4e17fc1SRobert Elliott 					free_irq(h->intr[j], &h->q[j]);
7177a4e17fc1SRobert Elliott 					h->q[j] = 0;
7178a4e17fc1SRobert Elliott 				}
7179a4e17fc1SRobert Elliott 				for (; j < MAX_REPLY_QUEUES; j++)
7180a4e17fc1SRobert Elliott 					h->q[j] = 0;
7181a4e17fc1SRobert Elliott 				return rc;
7182a4e17fc1SRobert Elliott 			}
7183a4e17fc1SRobert Elliott 		}
718441b3cf08SStephen M. Cameron 		hpsa_irq_affinity_hints(h);
7185254f796bSMatt Gates 	} else {
7186254f796bSMatt Gates 		/* Use single reply pool */
7187eee0f03aSHannes Reinecke 		if (h->msix_vector > 0 || h->msi_vector) {
7188254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
7189254f796bSMatt Gates 				msixhandler, 0, h->devname,
7190254f796bSMatt Gates 				&h->q[h->intr_mode]);
7191254f796bSMatt Gates 		} else {
7192254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
7193254f796bSMatt Gates 				intxhandler, IRQF_SHARED, h->devname,
7194254f796bSMatt Gates 				&h->q[h->intr_mode]);
7195254f796bSMatt Gates 		}
7196105a3dbcSRobert Elliott 		irq_set_affinity_hint(h->intr[h->intr_mode], NULL);
7197254f796bSMatt Gates 	}
71980ae01a32SStephen M. Cameron 	if (rc) {
7199195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "failed to get irq %d for %s\n",
72000ae01a32SStephen M. Cameron 		       h->intr[h->intr_mode], h->devname);
7201195f2c65SRobert Elliott 		hpsa_free_irqs(h);
72020ae01a32SStephen M. Cameron 		return -ENODEV;
72030ae01a32SStephen M. Cameron 	}
72040ae01a32SStephen M. Cameron 	return 0;
72050ae01a32SStephen M. Cameron }
72060ae01a32SStephen M. Cameron 
72076f039790SGreg Kroah-Hartman static int hpsa_kdump_soft_reset(struct ctlr_info *h)
720864670ac8SStephen M. Cameron {
7209bf43caf3SRobert Elliott 	hpsa_send_host_reset(h, RAID_CTLR_LUNID, HPSA_RESET_TYPE_CONTROLLER);
721064670ac8SStephen M. Cameron 
721164670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
721264670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) {
721364670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
721464670ac8SStephen M. Cameron 		return -1;
721564670ac8SStephen M. Cameron 	}
721664670ac8SStephen M. Cameron 
721764670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
721864670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) {
721964670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Board failed to become ready "
722064670ac8SStephen M. Cameron 			"after soft reset.\n");
722164670ac8SStephen M. Cameron 		return -1;
722264670ac8SStephen M. Cameron 	}
722364670ac8SStephen M. Cameron 
722464670ac8SStephen M. Cameron 	return 0;
722564670ac8SStephen M. Cameron }
722664670ac8SStephen M. Cameron 
7227072b0518SStephen M. Cameron static void hpsa_free_reply_queues(struct ctlr_info *h)
7228072b0518SStephen M. Cameron {
7229072b0518SStephen M. Cameron 	int i;
7230072b0518SStephen M. Cameron 
7231072b0518SStephen M. Cameron 	for (i = 0; i < h->nreply_queues; i++) {
7232072b0518SStephen M. Cameron 		if (!h->reply_queue[i].head)
7233072b0518SStephen M. Cameron 			continue;
72341fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
72351fb7c98aSRobert Elliott 					h->reply_queue_size,
72361fb7c98aSRobert Elliott 					h->reply_queue[i].head,
72371fb7c98aSRobert Elliott 					h->reply_queue[i].busaddr);
7238072b0518SStephen M. Cameron 		h->reply_queue[i].head = NULL;
7239072b0518SStephen M. Cameron 		h->reply_queue[i].busaddr = 0;
7240072b0518SStephen M. Cameron 	}
7241105a3dbcSRobert Elliott 	h->reply_queue_size = 0;
7242072b0518SStephen M. Cameron }
7243072b0518SStephen M. Cameron 
72440097f0f4SStephen M. Cameron static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
72450097f0f4SStephen M. Cameron {
7246105a3dbcSRobert Elliott 	hpsa_free_performant_mode(h);		/* init_one 7 */
7247105a3dbcSRobert Elliott 	hpsa_free_sg_chain_blocks(h);		/* init_one 6 */
7248105a3dbcSRobert Elliott 	hpsa_free_cmd_pool(h);			/* init_one 5 */
7249105a3dbcSRobert Elliott 	hpsa_free_irqs(h);			/* init_one 4 */
7250105a3dbcSRobert Elliott 	hpsa_free_pci_init(h);			/* init_one 3 */
7251105a3dbcSRobert Elliott 	kfree(h);				/* init_one 1 */
725264670ac8SStephen M. Cameron }
725364670ac8SStephen M. Cameron 
7254a0c12413SStephen M. Cameron /* Called when controller lockup detected. */
7255f2405db8SDon Brace static void fail_all_outstanding_cmds(struct ctlr_info *h)
7256a0c12413SStephen M. Cameron {
7257281a7fd0SWebb Scales 	int i, refcount;
7258281a7fd0SWebb Scales 	struct CommandList *c;
725925163bd5SWebb Scales 	int failcount = 0;
7260a0c12413SStephen M. Cameron 
7261080ef1ccSDon Brace 	flush_workqueue(h->resubmit_wq); /* ensure all cmds are fully built */
7262f2405db8SDon Brace 	for (i = 0; i < h->nr_cmds; i++) {
7263f2405db8SDon Brace 		c = h->cmd_pool + i;
7264281a7fd0SWebb Scales 		refcount = atomic_inc_return(&c->refcount);
7265281a7fd0SWebb Scales 		if (refcount > 1) {
726625163bd5SWebb Scales 			c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
72675a3d16f5SStephen M. Cameron 			finish_cmd(c);
7268433b5f4dSStephen Cameron 			atomic_dec(&h->commands_outstanding);
726925163bd5SWebb Scales 			failcount++;
7270a0c12413SStephen M. Cameron 		}
7271281a7fd0SWebb Scales 		cmd_free(h, c);
7272281a7fd0SWebb Scales 	}
727325163bd5SWebb Scales 	dev_warn(&h->pdev->dev,
727425163bd5SWebb Scales 		"failed %d commands in fail_all\n", failcount);
7275a0c12413SStephen M. Cameron }
7276a0c12413SStephen M. Cameron 
7277094963daSStephen M. Cameron static void set_lockup_detected_for_all_cpus(struct ctlr_info *h, u32 value)
7278094963daSStephen M. Cameron {
7279c8ed0010SRusty Russell 	int cpu;
7280094963daSStephen M. Cameron 
7281c8ed0010SRusty Russell 	for_each_online_cpu(cpu) {
7282094963daSStephen M. Cameron 		u32 *lockup_detected;
7283094963daSStephen M. Cameron 		lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
7284094963daSStephen M. Cameron 		*lockup_detected = value;
7285094963daSStephen M. Cameron 	}
7286094963daSStephen M. Cameron 	wmb(); /* be sure the per-cpu variables are out to memory */
7287094963daSStephen M. Cameron }
7288094963daSStephen M. Cameron 
7289a0c12413SStephen M. Cameron static void controller_lockup_detected(struct ctlr_info *h)
7290a0c12413SStephen M. Cameron {
7291a0c12413SStephen M. Cameron 	unsigned long flags;
7292094963daSStephen M. Cameron 	u32 lockup_detected;
7293a0c12413SStephen M. Cameron 
7294a0c12413SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
7295a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
7296094963daSStephen M. Cameron 	lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
7297094963daSStephen M. Cameron 	if (!lockup_detected) {
7298094963daSStephen M. Cameron 		/* no heartbeat, but controller gave us a zero. */
7299094963daSStephen M. Cameron 		dev_warn(&h->pdev->dev,
730025163bd5SWebb Scales 			"lockup detected after %d but scratchpad register is zero\n",
730125163bd5SWebb Scales 			h->heartbeat_sample_interval / HZ);
7302094963daSStephen M. Cameron 		lockup_detected = 0xffffffff;
7303094963daSStephen M. Cameron 	}
7304094963daSStephen M. Cameron 	set_lockup_detected_for_all_cpus(h, lockup_detected);
7305a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
730625163bd5SWebb Scales 	dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x after %d\n",
730725163bd5SWebb Scales 			lockup_detected, h->heartbeat_sample_interval / HZ);
7308a0c12413SStephen M. Cameron 	pci_disable_device(h->pdev);
7309f2405db8SDon Brace 	fail_all_outstanding_cmds(h);
7310a0c12413SStephen M. Cameron }
7311a0c12413SStephen M. Cameron 
731225163bd5SWebb Scales static int detect_controller_lockup(struct ctlr_info *h)
7313a0c12413SStephen M. Cameron {
7314a0c12413SStephen M. Cameron 	u64 now;
7315a0c12413SStephen M. Cameron 	u32 heartbeat;
7316a0c12413SStephen M. Cameron 	unsigned long flags;
7317a0c12413SStephen M. Cameron 
7318a0c12413SStephen M. Cameron 	now = get_jiffies_64();
7319a0c12413SStephen M. Cameron 	/* If we've received an interrupt recently, we're ok. */
7320a0c12413SStephen M. Cameron 	if (time_after64(h->last_intr_timestamp +
7321e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
732225163bd5SWebb Scales 		return false;
7323a0c12413SStephen M. Cameron 
7324a0c12413SStephen M. Cameron 	/*
7325a0c12413SStephen M. Cameron 	 * If we've already checked the heartbeat recently, we're ok.
7326a0c12413SStephen M. Cameron 	 * This could happen if someone sends us a signal. We
7327a0c12413SStephen M. Cameron 	 * otherwise don't care about signals in this thread.
7328a0c12413SStephen M. Cameron 	 */
7329a0c12413SStephen M. Cameron 	if (time_after64(h->last_heartbeat_timestamp +
7330e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
733125163bd5SWebb Scales 		return false;
7332a0c12413SStephen M. Cameron 
7333a0c12413SStephen M. Cameron 	/* If heartbeat has not changed since we last looked, we're not ok. */
7334a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
7335a0c12413SStephen M. Cameron 	heartbeat = readl(&h->cfgtable->HeartBeat);
7336a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
7337a0c12413SStephen M. Cameron 	if (h->last_heartbeat == heartbeat) {
7338a0c12413SStephen M. Cameron 		controller_lockup_detected(h);
733925163bd5SWebb Scales 		return true;
7340a0c12413SStephen M. Cameron 	}
7341a0c12413SStephen M. Cameron 
7342a0c12413SStephen M. Cameron 	/* We're ok. */
7343a0c12413SStephen M. Cameron 	h->last_heartbeat = heartbeat;
7344a0c12413SStephen M. Cameron 	h->last_heartbeat_timestamp = now;
734525163bd5SWebb Scales 	return false;
7346a0c12413SStephen M. Cameron }
7347a0c12413SStephen M. Cameron 
73489846590eSStephen M. Cameron static void hpsa_ack_ctlr_events(struct ctlr_info *h)
734976438d08SStephen M. Cameron {
735076438d08SStephen M. Cameron 	int i;
735176438d08SStephen M. Cameron 	char *event_type;
735276438d08SStephen M. Cameron 
7353e4aa3e6aSStephen Cameron 	if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
7354e4aa3e6aSStephen Cameron 		return;
7355e4aa3e6aSStephen Cameron 
735676438d08SStephen M. Cameron 	/* Ask the controller to clear the events we're handling. */
73571f7cee8cSStephen M. Cameron 	if ((h->transMethod & (CFGTBL_Trans_io_accel1
73581f7cee8cSStephen M. Cameron 			| CFGTBL_Trans_io_accel2)) &&
735976438d08SStephen M. Cameron 		(h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
736076438d08SStephen M. Cameron 		 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
736176438d08SStephen M. Cameron 
736276438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
736376438d08SStephen M. Cameron 			event_type = "state change";
736476438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
736576438d08SStephen M. Cameron 			event_type = "configuration change";
736676438d08SStephen M. Cameron 		/* Stop sending new RAID offload reqs via the IO accelerator */
736776438d08SStephen M. Cameron 		scsi_block_requests(h->scsi_host);
736876438d08SStephen M. Cameron 		for (i = 0; i < h->ndevices; i++)
736976438d08SStephen M. Cameron 			h->dev[i]->offload_enabled = 0;
737023100dd9SStephen M. Cameron 		hpsa_drain_accel_commands(h);
737176438d08SStephen M. Cameron 		/* Set 'accelerator path config change' bit */
737276438d08SStephen M. Cameron 		dev_warn(&h->pdev->dev,
737376438d08SStephen M. Cameron 			"Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
737476438d08SStephen M. Cameron 			h->events, event_type);
737576438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
737676438d08SStephen M. Cameron 		/* Set the "clear event notify field update" bit 6 */
737776438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
737876438d08SStephen M. Cameron 		/* Wait until ctlr clears 'clear event notify field', bit 6 */
737976438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
738076438d08SStephen M. Cameron 		scsi_unblock_requests(h->scsi_host);
738176438d08SStephen M. Cameron 	} else {
738276438d08SStephen M. Cameron 		/* Acknowledge controller notification events. */
738376438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
738476438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
738576438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
738676438d08SStephen M. Cameron #if 0
738776438d08SStephen M. Cameron 		writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
738876438d08SStephen M. Cameron 		hpsa_wait_for_mode_change_ack(h);
738976438d08SStephen M. Cameron #endif
739076438d08SStephen M. Cameron 	}
73919846590eSStephen M. Cameron 	return;
739276438d08SStephen M. Cameron }
739376438d08SStephen M. Cameron 
739476438d08SStephen M. Cameron /* Check a register on the controller to see if there are configuration
739576438d08SStephen M. Cameron  * changes (added/changed/removed logical drives, etc.) which mean that
7396e863d68eSScott Teel  * we should rescan the controller for devices.
7397e863d68eSScott Teel  * Also check flag for driver-initiated rescan.
739876438d08SStephen M. Cameron  */
73999846590eSStephen M. Cameron static int hpsa_ctlr_needs_rescan(struct ctlr_info *h)
740076438d08SStephen M. Cameron {
740176438d08SStephen M. Cameron 	if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
74029846590eSStephen M. Cameron 		return 0;
740376438d08SStephen M. Cameron 
740476438d08SStephen M. Cameron 	h->events = readl(&(h->cfgtable->event_notify));
74059846590eSStephen M. Cameron 	return h->events & RESCAN_REQUIRED_EVENT_BITS;
74069846590eSStephen M. Cameron }
740776438d08SStephen M. Cameron 
740876438d08SStephen M. Cameron /*
74099846590eSStephen M. Cameron  * Check if any of the offline devices have become ready
741076438d08SStephen M. Cameron  */
74119846590eSStephen M. Cameron static int hpsa_offline_devices_ready(struct ctlr_info *h)
74129846590eSStephen M. Cameron {
74139846590eSStephen M. Cameron 	unsigned long flags;
74149846590eSStephen M. Cameron 	struct offline_device_entry *d;
74159846590eSStephen M. Cameron 	struct list_head *this, *tmp;
74169846590eSStephen M. Cameron 
74179846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
74189846590eSStephen M. Cameron 	list_for_each_safe(this, tmp, &h->offline_device_list) {
74199846590eSStephen M. Cameron 		d = list_entry(this, struct offline_device_entry,
74209846590eSStephen M. Cameron 				offline_list);
74219846590eSStephen M. Cameron 		spin_unlock_irqrestore(&h->offline_device_lock, flags);
7422d1fea47cSStephen M. Cameron 		if (!hpsa_volume_offline(h, d->scsi3addr)) {
7423d1fea47cSStephen M. Cameron 			spin_lock_irqsave(&h->offline_device_lock, flags);
7424d1fea47cSStephen M. Cameron 			list_del(&d->offline_list);
7425d1fea47cSStephen M. Cameron 			spin_unlock_irqrestore(&h->offline_device_lock, flags);
74269846590eSStephen M. Cameron 			return 1;
7427d1fea47cSStephen M. Cameron 		}
74289846590eSStephen M. Cameron 		spin_lock_irqsave(&h->offline_device_lock, flags);
742976438d08SStephen M. Cameron 	}
74309846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
74319846590eSStephen M. Cameron 	return 0;
74329846590eSStephen M. Cameron }
74339846590eSStephen M. Cameron 
74346636e7f4SDon Brace static void hpsa_rescan_ctlr_worker(struct work_struct *work)
7435a0c12413SStephen M. Cameron {
7436a0c12413SStephen M. Cameron 	unsigned long flags;
74378a98db73SStephen M. Cameron 	struct ctlr_info *h = container_of(to_delayed_work(work),
74386636e7f4SDon Brace 					struct ctlr_info, rescan_ctlr_work);
74396636e7f4SDon Brace 
74406636e7f4SDon Brace 
74416636e7f4SDon Brace 	if (h->remove_in_progress)
74428a98db73SStephen M. Cameron 		return;
74439846590eSStephen M. Cameron 
74449846590eSStephen M. Cameron 	if (hpsa_ctlr_needs_rescan(h) || hpsa_offline_devices_ready(h)) {
74459846590eSStephen M. Cameron 		scsi_host_get(h->scsi_host);
74469846590eSStephen M. Cameron 		hpsa_ack_ctlr_events(h);
74479846590eSStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
74489846590eSStephen M. Cameron 		scsi_host_put(h->scsi_host);
74499846590eSStephen M. Cameron 	}
74506636e7f4SDon Brace 	spin_lock_irqsave(&h->lock, flags);
74516636e7f4SDon Brace 	if (!h->remove_in_progress)
74526636e7f4SDon Brace 		queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
74536636e7f4SDon Brace 				h->heartbeat_sample_interval);
74546636e7f4SDon Brace 	spin_unlock_irqrestore(&h->lock, flags);
74556636e7f4SDon Brace }
74566636e7f4SDon Brace 
74576636e7f4SDon Brace static void hpsa_monitor_ctlr_worker(struct work_struct *work)
74586636e7f4SDon Brace {
74596636e7f4SDon Brace 	unsigned long flags;
74606636e7f4SDon Brace 	struct ctlr_info *h = container_of(to_delayed_work(work),
74616636e7f4SDon Brace 					struct ctlr_info, monitor_ctlr_work);
74626636e7f4SDon Brace 
74636636e7f4SDon Brace 	detect_controller_lockup(h);
74646636e7f4SDon Brace 	if (lockup_detected(h))
74656636e7f4SDon Brace 		return;
74669846590eSStephen M. Cameron 
74678a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
74686636e7f4SDon Brace 	if (!h->remove_in_progress)
74698a98db73SStephen M. Cameron 		schedule_delayed_work(&h->monitor_ctlr_work,
74708a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
74718a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
7472a0c12413SStephen M. Cameron }
7473a0c12413SStephen M. Cameron 
74746636e7f4SDon Brace static struct workqueue_struct *hpsa_create_controller_wq(struct ctlr_info *h,
74756636e7f4SDon Brace 						char *name)
74766636e7f4SDon Brace {
74776636e7f4SDon Brace 	struct workqueue_struct *wq = NULL;
74786636e7f4SDon Brace 
7479397ea9cbSDon Brace 	wq = alloc_ordered_workqueue("%s_%d_hpsa", 0, name, h->ctlr);
74806636e7f4SDon Brace 	if (!wq)
74816636e7f4SDon Brace 		dev_err(&h->pdev->dev, "failed to create %s workqueue\n", name);
74826636e7f4SDon Brace 
74836636e7f4SDon Brace 	return wq;
74846636e7f4SDon Brace }
74856636e7f4SDon Brace 
74866f039790SGreg Kroah-Hartman static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
74874c2a8c40SStephen M. Cameron {
74884c2a8c40SStephen M. Cameron 	int dac, rc;
74894c2a8c40SStephen M. Cameron 	struct ctlr_info *h;
749064670ac8SStephen M. Cameron 	int try_soft_reset = 0;
749164670ac8SStephen M. Cameron 	unsigned long flags;
74926b6c1cd7STomas Henzl 	u32 board_id;
74934c2a8c40SStephen M. Cameron 
74944c2a8c40SStephen M. Cameron 	if (number_of_controllers == 0)
74954c2a8c40SStephen M. Cameron 		printk(KERN_INFO DRIVER_NAME "\n");
74964c2a8c40SStephen M. Cameron 
74976b6c1cd7STomas Henzl 	rc = hpsa_lookup_board_id(pdev, &board_id);
74986b6c1cd7STomas Henzl 	if (rc < 0) {
74996b6c1cd7STomas Henzl 		dev_warn(&pdev->dev, "Board ID not found\n");
75006b6c1cd7STomas Henzl 		return rc;
75016b6c1cd7STomas Henzl 	}
75026b6c1cd7STomas Henzl 
75036b6c1cd7STomas Henzl 	rc = hpsa_init_reset_devices(pdev, board_id);
750464670ac8SStephen M. Cameron 	if (rc) {
750564670ac8SStephen M. Cameron 		if (rc != -ENOTSUPP)
75064c2a8c40SStephen M. Cameron 			return rc;
750764670ac8SStephen M. Cameron 		/* If the reset fails in a particular way (it has no way to do
750864670ac8SStephen M. Cameron 		 * a proper hard reset, so returns -ENOTSUPP) we can try to do
750964670ac8SStephen M. Cameron 		 * a soft reset once we get the controller configured up to the
751064670ac8SStephen M. Cameron 		 * point that it can accept a command.
751164670ac8SStephen M. Cameron 		 */
751264670ac8SStephen M. Cameron 		try_soft_reset = 1;
751364670ac8SStephen M. Cameron 		rc = 0;
751464670ac8SStephen M. Cameron 	}
751564670ac8SStephen M. Cameron 
751664670ac8SStephen M. Cameron reinit_after_soft_reset:
75174c2a8c40SStephen M. Cameron 
7518303932fdSDon Brace 	/* Command structures must be aligned on a 32-byte boundary because
7519303932fdSDon Brace 	 * the 5 lower bits of the address are used by the hardware. and by
7520303932fdSDon Brace 	 * the driver.  See comments in hpsa.h for more info.
7521303932fdSDon Brace 	 */
7522303932fdSDon Brace 	BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
7523edd16368SStephen M. Cameron 	h = kzalloc(sizeof(*h), GFP_KERNEL);
7524105a3dbcSRobert Elliott 	if (!h) {
7525105a3dbcSRobert Elliott 		dev_err(&pdev->dev, "Failed to allocate controller head\n");
7526ecd9aad4SStephen M. Cameron 		return -ENOMEM;
7527105a3dbcSRobert Elliott 	}
7528edd16368SStephen M. Cameron 
752955c06c71SStephen M. Cameron 	h->pdev = pdev;
7530105a3dbcSRobert Elliott 
7531a9a3a273SStephen M. Cameron 	h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
75329846590eSStephen M. Cameron 	INIT_LIST_HEAD(&h->offline_device_list);
75336eaf46fdSStephen M. Cameron 	spin_lock_init(&h->lock);
75349846590eSStephen M. Cameron 	spin_lock_init(&h->offline_device_lock);
75356eaf46fdSStephen M. Cameron 	spin_lock_init(&h->scan_lock);
753634f0c627SDon Brace 	atomic_set(&h->passthru_cmds_avail, HPSA_MAX_CONCURRENT_PASSTHRUS);
75379b5c48c2SStephen Cameron 	atomic_set(&h->abort_cmds_available, HPSA_CMDS_RESERVED_FOR_ABORTS);
7538094963daSStephen M. Cameron 
75396636e7f4SDon Brace 	h->rescan_ctlr_wq = hpsa_create_controller_wq(h, "rescan");
75406636e7f4SDon Brace 	if (!h->rescan_ctlr_wq) {
7541080ef1ccSDon Brace 		rc = -ENOMEM;
7542080ef1ccSDon Brace 		goto clean1;
7543080ef1ccSDon Brace 	}
75446636e7f4SDon Brace 
75456636e7f4SDon Brace 	h->resubmit_wq = hpsa_create_controller_wq(h, "resubmit");
75466636e7f4SDon Brace 	if (!h->resubmit_wq) {
75476636e7f4SDon Brace 		rc = -ENOMEM;
7548105a3dbcSRobert Elliott 		goto clean1;	/* aer/h */
75496636e7f4SDon Brace 	}
75506636e7f4SDon Brace 
7551094963daSStephen M. Cameron 	/* Allocate and clear per-cpu variable lockup_detected */
7552094963daSStephen M. Cameron 	h->lockup_detected = alloc_percpu(u32);
75532a5ac326SStephen M. Cameron 	if (!h->lockup_detected) {
7554105a3dbcSRobert Elliott 		dev_err(&h->pdev->dev, "Failed to allocate lockup detector\n");
75552a5ac326SStephen M. Cameron 		rc = -ENOMEM;
7556105a3dbcSRobert Elliott 		goto clean1;	/* wq/aer/h */
75572a5ac326SStephen M. Cameron 	}
7558094963daSStephen M. Cameron 	set_lockup_detected_for_all_cpus(h, 0);
7559094963daSStephen M. Cameron 
756055c06c71SStephen M. Cameron 	rc = hpsa_pci_init(h);
7561105a3dbcSRobert Elliott 	if (rc)
7562105a3dbcSRobert Elliott 		goto clean2;	/* lockup, wq/aer/h */
7563edd16368SStephen M. Cameron 
7564f79cfec6SStephen M. Cameron 	sprintf(h->devname, HPSA "%d", number_of_controllers);
7565edd16368SStephen M. Cameron 	h->ctlr = number_of_controllers;
7566edd16368SStephen M. Cameron 	number_of_controllers++;
7567edd16368SStephen M. Cameron 
7568edd16368SStephen M. Cameron 	/* configure PCI DMA stuff */
7569ecd9aad4SStephen M. Cameron 	rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
7570ecd9aad4SStephen M. Cameron 	if (rc == 0) {
7571edd16368SStephen M. Cameron 		dac = 1;
7572ecd9aad4SStephen M. Cameron 	} else {
7573ecd9aad4SStephen M. Cameron 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7574ecd9aad4SStephen M. Cameron 		if (rc == 0) {
7575edd16368SStephen M. Cameron 			dac = 0;
7576ecd9aad4SStephen M. Cameron 		} else {
7577edd16368SStephen M. Cameron 			dev_err(&pdev->dev, "no suitable DMA available\n");
7578105a3dbcSRobert Elliott 			goto clean3;	/* pci, lockup, wq/aer/h */
7579edd16368SStephen M. Cameron 		}
7580ecd9aad4SStephen M. Cameron 	}
7581edd16368SStephen M. Cameron 
7582edd16368SStephen M. Cameron 	/* make sure the board interrupts are off */
7583edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
758410f66018SStephen M. Cameron 
7585105a3dbcSRobert Elliott 	rc = hpsa_request_irqs(h, do_hpsa_intr_msi, do_hpsa_intr_intx);
7586105a3dbcSRobert Elliott 	if (rc)
7587105a3dbcSRobert Elliott 		goto clean3;	/* pci, lockup, wq/aer/h */
7588303932fdSDon Brace 	dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
7589303932fdSDon Brace 	       h->devname, pdev->device,
7590a9a3a273SStephen M. Cameron 	       h->intr[h->intr_mode], dac ? "" : " not");
7591d37ffbe4SRobert Elliott 	rc = hpsa_alloc_cmd_pool(h);
75928947fd10SRobert Elliott 	if (rc)
7593105a3dbcSRobert Elliott 		goto clean4;	/* irq, pci, lockup, wq/aer/h */
7594105a3dbcSRobert Elliott 	rc = hpsa_alloc_sg_chain_blocks(h);
7595105a3dbcSRobert Elliott 	if (rc)
7596105a3dbcSRobert Elliott 		goto clean5;	/* cmd, irq, pci, lockup, wq/aer/h */
7597a08a8471SStephen M. Cameron 	init_waitqueue_head(&h->scan_wait_queue);
75989b5c48c2SStephen Cameron 	init_waitqueue_head(&h->abort_cmd_wait_queue);
7599a08a8471SStephen M. Cameron 	h->scan_finished = 1; /* no scan currently in progress */
7600edd16368SStephen M. Cameron 
7601edd16368SStephen M. Cameron 	pci_set_drvdata(pdev, h);
76029a41338eSStephen M. Cameron 	h->ndevices = 0;
7603316b221aSStephen M. Cameron 	h->hba_mode_enabled = 0;
76049a41338eSStephen M. Cameron 	h->scsi_host = NULL;
76059a41338eSStephen M. Cameron 	spin_lock_init(&h->devlock);
7606105a3dbcSRobert Elliott 	rc = hpsa_put_ctlr_into_performant_mode(h);
7607105a3dbcSRobert Elliott 	if (rc)
7608105a3dbcSRobert Elliott 		goto clean6;	/* sg, cmd, irq, pci, lockup, wq/aer/h */
760964670ac8SStephen M. Cameron 
7610105a3dbcSRobert Elliott 	/*
7611105a3dbcSRobert Elliott 	 * At this point, the controller is ready to take commands.
761264670ac8SStephen M. Cameron 	 * Now, if reset_devices and the hard reset didn't work, try
761364670ac8SStephen M. Cameron 	 * the soft reset and see if that works.
761464670ac8SStephen M. Cameron 	 */
761564670ac8SStephen M. Cameron 	if (try_soft_reset) {
761664670ac8SStephen M. Cameron 
761764670ac8SStephen M. Cameron 		/* This is kind of gross.  We may or may not get a completion
761864670ac8SStephen M. Cameron 		 * from the soft reset command, and if we do, then the value
761964670ac8SStephen M. Cameron 		 * from the fifo may or may not be valid.  So, we wait 10 secs
762064670ac8SStephen M. Cameron 		 * after the reset throwing away any completions we get during
762164670ac8SStephen M. Cameron 		 * that time.  Unregister the interrupt handler and register
762264670ac8SStephen M. Cameron 		 * fake ones to scoop up any residual completions.
762364670ac8SStephen M. Cameron 		 */
762464670ac8SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
762564670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
762664670ac8SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
7627ec501a18SRobert Elliott 		hpsa_free_irqs(h);
76289ee61794SRobert Elliott 		rc = hpsa_request_irqs(h, hpsa_msix_discard_completions,
762964670ac8SStephen M. Cameron 					hpsa_intx_discard_completions);
763064670ac8SStephen M. Cameron 		if (rc) {
76319ee61794SRobert Elliott 			dev_warn(&h->pdev->dev,
76329ee61794SRobert Elliott 				"Failed to request_irq after soft reset.\n");
763364670ac8SStephen M. Cameron 			goto clean4;
763464670ac8SStephen M. Cameron 		}
763564670ac8SStephen M. Cameron 
763664670ac8SStephen M. Cameron 		rc = hpsa_kdump_soft_reset(h);
763764670ac8SStephen M. Cameron 		if (rc)
763864670ac8SStephen M. Cameron 			/* Neither hard nor soft reset worked, we're hosed. */
763964670ac8SStephen M. Cameron 			goto clean4;
764064670ac8SStephen M. Cameron 
764164670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev, "Board READY.\n");
764264670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev,
764364670ac8SStephen M. Cameron 			"Waiting for stale completions to drain.\n");
764464670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_ON);
764564670ac8SStephen M. Cameron 		msleep(10000);
764664670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
764764670ac8SStephen M. Cameron 
764864670ac8SStephen M. Cameron 		rc = controller_reset_failed(h->cfgtable);
764964670ac8SStephen M. Cameron 		if (rc)
765064670ac8SStephen M. Cameron 			dev_info(&h->pdev->dev,
765164670ac8SStephen M. Cameron 				"Soft reset appears to have failed.\n");
765264670ac8SStephen M. Cameron 
765364670ac8SStephen M. Cameron 		/* since the controller's reset, we have to go back and re-init
765464670ac8SStephen M. Cameron 		 * everything.  Easiest to just forget what we've done and do it
765564670ac8SStephen M. Cameron 		 * all over again.
765664670ac8SStephen M. Cameron 		 */
765764670ac8SStephen M. Cameron 		hpsa_undo_allocations_after_kdump_soft_reset(h);
765864670ac8SStephen M. Cameron 		try_soft_reset = 0;
765964670ac8SStephen M. Cameron 		if (rc)
766064670ac8SStephen M. Cameron 			/* don't go to clean4, we already unallocated */
766164670ac8SStephen M. Cameron 			return -ENODEV;
766264670ac8SStephen M. Cameron 
766364670ac8SStephen M. Cameron 		goto reinit_after_soft_reset;
766464670ac8SStephen M. Cameron 	}
7665edd16368SStephen M. Cameron 
7666da0697bdSScott Teel 	/* Enable Accelerated IO path at driver layer */
7667da0697bdSScott Teel 	h->acciopath_status = 1;
7668da0697bdSScott Teel 
7669e863d68eSScott Teel 
7670edd16368SStephen M. Cameron 	/* Turn the interrupts on so we can service requests */
7671edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_ON);
7672edd16368SStephen M. Cameron 
7673339b2b14SStephen M. Cameron 	hpsa_hba_inquiry(h);
76744a4384ceSStephen Cameron 	rc = hpsa_register_scsi(h);	/* hook ourselves into SCSI subsystem */
76754a4384ceSStephen Cameron 	if (rc)
7676105a3dbcSRobert Elliott 		goto clean7;
76778a98db73SStephen M. Cameron 
76788a98db73SStephen M. Cameron 	/* Monitor the controller for firmware lockups */
76798a98db73SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
76808a98db73SStephen M. Cameron 	INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
76818a98db73SStephen M. Cameron 	schedule_delayed_work(&h->monitor_ctlr_work,
76828a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
76836636e7f4SDon Brace 	INIT_DELAYED_WORK(&h->rescan_ctlr_work, hpsa_rescan_ctlr_worker);
76846636e7f4SDon Brace 	queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
76856636e7f4SDon Brace 				h->heartbeat_sample_interval);
768688bf6d62SStephen M. Cameron 	return 0;
7687edd16368SStephen M. Cameron 
7688105a3dbcSRobert Elliott clean7: /* perf, sg, cmd, irq, pci, lockup, wq/aer/h */
7689105a3dbcSRobert Elliott 	kfree(h->hba_inquiry_data);
7690105a3dbcSRobert Elliott 	hpsa_free_performant_mode(h);
7691105a3dbcSRobert Elliott 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
7692105a3dbcSRobert Elliott clean6: /* sg, cmd, irq, pci, lockup, wq/aer/h */
769333a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
7694105a3dbcSRobert Elliott clean5: /* cmd, irq, pci, lockup, wq/aer/h */
76952e9d1b36SStephen M. Cameron 	hpsa_free_cmd_pool(h);
7696105a3dbcSRobert Elliott clean4: /* irq, pci, lockup, wq/aer/h */
7697ec501a18SRobert Elliott 	hpsa_free_irqs(h);
7698105a3dbcSRobert Elliott clean3: /* pci, lockup, wq/aer/h */
7699195f2c65SRobert Elliott 	hpsa_free_pci_init(h);
7700105a3dbcSRobert Elliott clean2: /* lockup, wq/aer/h */
7701105a3dbcSRobert Elliott 	if (h->lockup_detected) {
7702094963daSStephen M. Cameron 		free_percpu(h->lockup_detected);
7703105a3dbcSRobert Elliott 		h->lockup_detected = NULL;
7704105a3dbcSRobert Elliott 	}
7705105a3dbcSRobert Elliott clean1:	/* wq/aer/h */
7706105a3dbcSRobert Elliott 	if (h->resubmit_wq) {
7707105a3dbcSRobert Elliott 		destroy_workqueue(h->resubmit_wq);
7708105a3dbcSRobert Elliott 		h->resubmit_wq = NULL;
7709105a3dbcSRobert Elliott 	}
7710105a3dbcSRobert Elliott 	if (h->rescan_ctlr_wq) {
7711105a3dbcSRobert Elliott 		destroy_workqueue(h->rescan_ctlr_wq);
7712105a3dbcSRobert Elliott 		h->rescan_ctlr_wq = NULL;
7713105a3dbcSRobert Elliott 	}
7714edd16368SStephen M. Cameron 	kfree(h);
7715ecd9aad4SStephen M. Cameron 	return rc;
7716edd16368SStephen M. Cameron }
7717edd16368SStephen M. Cameron 
7718edd16368SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h)
7719edd16368SStephen M. Cameron {
7720edd16368SStephen M. Cameron 	char *flush_buf;
7721edd16368SStephen M. Cameron 	struct CommandList *c;
772225163bd5SWebb Scales 	int rc;
7723702890e3SStephen M. Cameron 
7724702890e3SStephen M. Cameron 	/* Don't bother trying to flush the cache if locked up */
772525163bd5SWebb Scales 	/* FIXME not necessary if do_simple_cmd does the check */
7726094963daSStephen M. Cameron 	if (unlikely(lockup_detected(h)))
7727702890e3SStephen M. Cameron 		return;
7728edd16368SStephen M. Cameron 	flush_buf = kzalloc(4, GFP_KERNEL);
7729edd16368SStephen M. Cameron 	if (!flush_buf)
7730edd16368SStephen M. Cameron 		return;
7731edd16368SStephen M. Cameron 
773245fcb86eSStephen Cameron 	c = cmd_alloc(h);
7733bf43caf3SRobert Elliott 
7734a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
7735a2dac136SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_CMD)) {
7736a2dac136SStephen M. Cameron 		goto out;
7737a2dac136SStephen M. Cameron 	}
773825163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
773925163bd5SWebb Scales 					PCI_DMA_TODEVICE, NO_TIMEOUT);
774025163bd5SWebb Scales 	if (rc)
774125163bd5SWebb Scales 		goto out;
7742edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus != 0)
7743a2dac136SStephen M. Cameron out:
7744edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
7745edd16368SStephen M. Cameron 			"error flushing cache on controller\n");
774645fcb86eSStephen Cameron 	cmd_free(h, c);
7747edd16368SStephen M. Cameron 	kfree(flush_buf);
7748edd16368SStephen M. Cameron }
7749edd16368SStephen M. Cameron 
7750edd16368SStephen M. Cameron static void hpsa_shutdown(struct pci_dev *pdev)
7751edd16368SStephen M. Cameron {
7752edd16368SStephen M. Cameron 	struct ctlr_info *h;
7753edd16368SStephen M. Cameron 
7754edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
7755edd16368SStephen M. Cameron 	/* Turn board interrupts off  and send the flush cache command
7756edd16368SStephen M. Cameron 	 * sendcmd will turn off interrupt, and send the flush...
7757edd16368SStephen M. Cameron 	 * To write all data in the battery backed cache to disks
7758edd16368SStephen M. Cameron 	 */
7759edd16368SStephen M. Cameron 	hpsa_flush_cache(h);
7760edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
7761105a3dbcSRobert Elliott 	hpsa_free_irqs(h);			/* init_one 4 */
7762cc64c817SRobert Elliott 	hpsa_disable_interrupt_mode(h);		/* pci_init 2 */
7763edd16368SStephen M. Cameron }
7764edd16368SStephen M. Cameron 
77656f039790SGreg Kroah-Hartman static void hpsa_free_device_info(struct ctlr_info *h)
776655e14e76SStephen M. Cameron {
776755e14e76SStephen M. Cameron 	int i;
776855e14e76SStephen M. Cameron 
7769105a3dbcSRobert Elliott 	for (i = 0; i < h->ndevices; i++) {
777055e14e76SStephen M. Cameron 		kfree(h->dev[i]);
7771105a3dbcSRobert Elliott 		h->dev[i] = NULL;
7772105a3dbcSRobert Elliott 	}
777355e14e76SStephen M. Cameron }
777455e14e76SStephen M. Cameron 
77756f039790SGreg Kroah-Hartman static void hpsa_remove_one(struct pci_dev *pdev)
7776edd16368SStephen M. Cameron {
7777edd16368SStephen M. Cameron 	struct ctlr_info *h;
77788a98db73SStephen M. Cameron 	unsigned long flags;
7779edd16368SStephen M. Cameron 
7780edd16368SStephen M. Cameron 	if (pci_get_drvdata(pdev) == NULL) {
7781edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "unable to remove device\n");
7782edd16368SStephen M. Cameron 		return;
7783edd16368SStephen M. Cameron 	}
7784edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
77858a98db73SStephen M. Cameron 
77868a98db73SStephen M. Cameron 	/* Get rid of any controller monitoring work items */
77878a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
77888a98db73SStephen M. Cameron 	h->remove_in_progress = 1;
77898a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
77906636e7f4SDon Brace 	cancel_delayed_work_sync(&h->monitor_ctlr_work);
77916636e7f4SDon Brace 	cancel_delayed_work_sync(&h->rescan_ctlr_work);
77926636e7f4SDon Brace 	destroy_workqueue(h->rescan_ctlr_wq);
77936636e7f4SDon Brace 	destroy_workqueue(h->resubmit_wq);
7794cc64c817SRobert Elliott 
7795105a3dbcSRobert Elliott 	/* includes hpsa_free_irqs - init_one 4 */
7796195f2c65SRobert Elliott 	/* includes hpsa_disable_interrupt_mode - pci_init 2 */
7797edd16368SStephen M. Cameron 	hpsa_shutdown(pdev);
7798cc64c817SRobert Elliott 
7799105a3dbcSRobert Elliott 	hpsa_free_device_info(h);		/* scan */
7800105a3dbcSRobert Elliott 
7801105a3dbcSRobert Elliott 	hpsa_unregister_scsi(h);			/* init_one "8" */
7802105a3dbcSRobert Elliott 	kfree(h->hba_inquiry_data);			/* init_one "8" */
7803105a3dbcSRobert Elliott 	h->hba_inquiry_data = NULL;			/* init_one "8" */
7804105a3dbcSRobert Elliott 	hpsa_free_performant_mode(h);			/* init_one 7 */
7805105a3dbcSRobert Elliott 	hpsa_free_sg_chain_blocks(h);			/* init_one 6 */
78061fb7c98aSRobert Elliott 	hpsa_free_cmd_pool(h);				/* init_one 5 */
7807105a3dbcSRobert Elliott 
7808105a3dbcSRobert Elliott 	/* hpsa_free_irqs already called via hpsa_shutdown init_one 4 */
7809195f2c65SRobert Elliott 
7810195f2c65SRobert Elliott 	/* includes hpsa_disable_interrupt_mode - pci_init 2 */
7811105a3dbcSRobert Elliott 	hpsa_free_pci_init(h);				/* init_one 3 */
7812195f2c65SRobert Elliott 
7813105a3dbcSRobert Elliott 	free_percpu(h->lockup_detected);		/* init_one 2 */
7814105a3dbcSRobert Elliott 	h->lockup_detected = NULL;			/* init_one 2 */
7815105a3dbcSRobert Elliott 	/* (void) pci_disable_pcie_error_reporting(pdev); */	/* init_one 1 */
7816105a3dbcSRobert Elliott 	kfree(h);					/* init_one 1 */
7817edd16368SStephen M. Cameron }
7818edd16368SStephen M. Cameron 
7819edd16368SStephen M. Cameron static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
7820edd16368SStephen M. Cameron 	__attribute__((unused)) pm_message_t state)
7821edd16368SStephen M. Cameron {
7822edd16368SStephen M. Cameron 	return -ENOSYS;
7823edd16368SStephen M. Cameron }
7824edd16368SStephen M. Cameron 
7825edd16368SStephen M. Cameron static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
7826edd16368SStephen M. Cameron {
7827edd16368SStephen M. Cameron 	return -ENOSYS;
7828edd16368SStephen M. Cameron }
7829edd16368SStephen M. Cameron 
7830edd16368SStephen M. Cameron static struct pci_driver hpsa_pci_driver = {
7831f79cfec6SStephen M. Cameron 	.name = HPSA,
7832edd16368SStephen M. Cameron 	.probe = hpsa_init_one,
78336f039790SGreg Kroah-Hartman 	.remove = hpsa_remove_one,
7834edd16368SStephen M. Cameron 	.id_table = hpsa_pci_device_id,	/* id_table */
7835edd16368SStephen M. Cameron 	.shutdown = hpsa_shutdown,
7836edd16368SStephen M. Cameron 	.suspend = hpsa_suspend,
7837edd16368SStephen M. Cameron 	.resume = hpsa_resume,
7838edd16368SStephen M. Cameron };
7839edd16368SStephen M. Cameron 
7840303932fdSDon Brace /* Fill in bucket_map[], given nsgs (the max number of
7841303932fdSDon Brace  * scatter gather elements supported) and bucket[],
7842303932fdSDon Brace  * which is an array of 8 integers.  The bucket[] array
7843303932fdSDon Brace  * contains 8 different DMA transfer sizes (in 16
7844303932fdSDon Brace  * byte increments) which the controller uses to fetch
7845303932fdSDon Brace  * commands.  This function fills in bucket_map[], which
7846303932fdSDon Brace  * maps a given number of scatter gather elements to one of
7847303932fdSDon Brace  * the 8 DMA transfer sizes.  The point of it is to allow the
7848303932fdSDon Brace  * controller to only do as much DMA as needed to fetch the
7849303932fdSDon Brace  * command, with the DMA transfer size encoded in the lower
7850303932fdSDon Brace  * bits of the command address.
7851303932fdSDon Brace  */
7852303932fdSDon Brace static void  calc_bucket_map(int bucket[], int num_buckets,
78532b08b3e9SDon Brace 	int nsgs, int min_blocks, u32 *bucket_map)
7854303932fdSDon Brace {
7855303932fdSDon Brace 	int i, j, b, size;
7856303932fdSDon Brace 
7857303932fdSDon Brace 	/* Note, bucket_map must have nsgs+1 entries. */
7858303932fdSDon Brace 	for (i = 0; i <= nsgs; i++) {
7859303932fdSDon Brace 		/* Compute size of a command with i SG entries */
7860e1f7de0cSMatt Gates 		size = i + min_blocks;
7861303932fdSDon Brace 		b = num_buckets; /* Assume the biggest bucket */
7862303932fdSDon Brace 		/* Find the bucket that is just big enough */
7863e1f7de0cSMatt Gates 		for (j = 0; j < num_buckets; j++) {
7864303932fdSDon Brace 			if (bucket[j] >= size) {
7865303932fdSDon Brace 				b = j;
7866303932fdSDon Brace 				break;
7867303932fdSDon Brace 			}
7868303932fdSDon Brace 		}
7869303932fdSDon Brace 		/* for a command with i SG entries, use bucket b. */
7870303932fdSDon Brace 		bucket_map[i] = b;
7871303932fdSDon Brace 	}
7872303932fdSDon Brace }
7873303932fdSDon Brace 
7874105a3dbcSRobert Elliott /*
7875105a3dbcSRobert Elliott  * return -ENODEV on err, 0 on success (or no action)
7876105a3dbcSRobert Elliott  * allocates numerous items that must be freed later
7877105a3dbcSRobert Elliott  */
7878c706a795SRobert Elliott static int hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
7879303932fdSDon Brace {
78806c311b57SStephen M. Cameron 	int i;
78816c311b57SStephen M. Cameron 	unsigned long register_value;
7882e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
7883e1f7de0cSMatt Gates 			(trans_support & CFGTBL_Trans_use_short_tags) |
7884e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix |
7885b9af4937SStephen M. Cameron 			(trans_support & (CFGTBL_Trans_io_accel1 |
7886b9af4937SStephen M. Cameron 				CFGTBL_Trans_io_accel2));
7887e1f7de0cSMatt Gates 	struct access_method access = SA5_performant_access;
7888def342bdSStephen M. Cameron 
7889def342bdSStephen M. Cameron 	/* This is a bit complicated.  There are 8 registers on
7890def342bdSStephen M. Cameron 	 * the controller which we write to to tell it 8 different
7891def342bdSStephen M. Cameron 	 * sizes of commands which there may be.  It's a way of
7892def342bdSStephen M. Cameron 	 * reducing the DMA done to fetch each command.  Encoded into
7893def342bdSStephen M. Cameron 	 * each command's tag are 3 bits which communicate to the controller
7894def342bdSStephen M. Cameron 	 * which of the eight sizes that command fits within.  The size of
7895def342bdSStephen M. Cameron 	 * each command depends on how many scatter gather entries there are.
7896def342bdSStephen M. Cameron 	 * Each SG entry requires 16 bytes.  The eight registers are programmed
7897def342bdSStephen M. Cameron 	 * with the number of 16-byte blocks a command of that size requires.
7898def342bdSStephen M. Cameron 	 * The smallest command possible requires 5 such 16 byte blocks.
7899d66ae08bSStephen M. Cameron 	 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
7900def342bdSStephen M. Cameron 	 * blocks.  Note, this only extends to the SG entries contained
7901def342bdSStephen M. Cameron 	 * within the command block, and does not extend to chained blocks
7902def342bdSStephen M. Cameron 	 * of SG elements.   bft[] contains the eight values we write to
7903def342bdSStephen M. Cameron 	 * the registers.  They are not evenly distributed, but have more
7904def342bdSStephen M. Cameron 	 * sizes for small commands, and fewer sizes for larger commands.
7905def342bdSStephen M. Cameron 	 */
7906d66ae08bSStephen M. Cameron 	int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
7907b9af4937SStephen M. Cameron #define MIN_IOACCEL2_BFT_ENTRY 5
7908b9af4937SStephen M. Cameron #define HPSA_IOACCEL2_HEADER_SZ 4
7909b9af4937SStephen M. Cameron 	int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
7910b9af4937SStephen M. Cameron 			13, 14, 15, 16, 17, 18, 19,
7911b9af4937SStephen M. Cameron 			HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
7912b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
7913b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
7914b9af4937SStephen M. Cameron 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
7915b9af4937SStephen M. Cameron 				 16 * MIN_IOACCEL2_BFT_ENTRY);
7916b9af4937SStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
7917d66ae08bSStephen M. Cameron 	BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
7918303932fdSDon Brace 	/*  5 = 1 s/g entry or 4k
7919303932fdSDon Brace 	 *  6 = 2 s/g entry or 8k
7920303932fdSDon Brace 	 *  8 = 4 s/g entry or 16k
7921303932fdSDon Brace 	 * 10 = 6 s/g entry or 24k
7922303932fdSDon Brace 	 */
7923303932fdSDon Brace 
7924b3a52e79SStephen M. Cameron 	/* If the controller supports either ioaccel method then
7925b3a52e79SStephen M. Cameron 	 * we can also use the RAID stack submit path that does not
7926b3a52e79SStephen M. Cameron 	 * perform the superfluous readl() after each command submission.
7927b3a52e79SStephen M. Cameron 	 */
7928b3a52e79SStephen M. Cameron 	if (trans_support & (CFGTBL_Trans_io_accel1 | CFGTBL_Trans_io_accel2))
7929b3a52e79SStephen M. Cameron 		access = SA5_performant_access_no_read;
7930b3a52e79SStephen M. Cameron 
7931303932fdSDon Brace 	/* Controller spec: zero out this buffer. */
7932072b0518SStephen M. Cameron 	for (i = 0; i < h->nreply_queues; i++)
7933072b0518SStephen M. Cameron 		memset(h->reply_queue[i].head, 0, h->reply_queue_size);
7934303932fdSDon Brace 
7935d66ae08bSStephen M. Cameron 	bft[7] = SG_ENTRIES_IN_CMD + 4;
7936d66ae08bSStephen M. Cameron 	calc_bucket_map(bft, ARRAY_SIZE(bft),
7937e1f7de0cSMatt Gates 				SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
7938303932fdSDon Brace 	for (i = 0; i < 8; i++)
7939303932fdSDon Brace 		writel(bft[i], &h->transtable->BlockFetch[i]);
7940303932fdSDon Brace 
7941303932fdSDon Brace 	/* size of controller ring buffer */
7942303932fdSDon Brace 	writel(h->max_commands, &h->transtable->RepQSize);
7943254f796bSMatt Gates 	writel(h->nreply_queues, &h->transtable->RepQCount);
7944303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrLow32);
7945303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrHigh32);
7946254f796bSMatt Gates 
7947254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
7948254f796bSMatt Gates 		writel(0, &h->transtable->RepQAddr[i].upper);
7949072b0518SStephen M. Cameron 		writel(h->reply_queue[i].busaddr,
7950254f796bSMatt Gates 			&h->transtable->RepQAddr[i].lower);
7951254f796bSMatt Gates 	}
7952254f796bSMatt Gates 
7953b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
7954e1f7de0cSMatt Gates 	writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
7955e1f7de0cSMatt Gates 	/*
7956e1f7de0cSMatt Gates 	 * enable outbound interrupt coalescing in accelerator mode;
7957e1f7de0cSMatt Gates 	 */
7958e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
7959e1f7de0cSMatt Gates 		access = SA5_ioaccel_mode1_access;
7960e1f7de0cSMatt Gates 		writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
7961e1f7de0cSMatt Gates 		writel(4, &h->cfgtable->HostWrite.CoalIntCount);
7962c349775eSScott Teel 	} else {
7963c349775eSScott Teel 		if (trans_support & CFGTBL_Trans_io_accel2) {
7964c349775eSScott Teel 			access = SA5_ioaccel_mode2_access;
7965c349775eSScott Teel 			writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
7966c349775eSScott Teel 			writel(4, &h->cfgtable->HostWrite.CoalIntCount);
7967c349775eSScott Teel 		}
7968e1f7de0cSMatt Gates 	}
7969303932fdSDon Brace 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
7970c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h)) {
7971c706a795SRobert Elliott 		dev_err(&h->pdev->dev,
7972c706a795SRobert Elliott 			"performant mode problem - doorbell timeout\n");
7973c706a795SRobert Elliott 		return -ENODEV;
7974c706a795SRobert Elliott 	}
7975303932fdSDon Brace 	register_value = readl(&(h->cfgtable->TransportActive));
7976303932fdSDon Brace 	if (!(register_value & CFGTBL_Trans_Performant)) {
7977050f7147SStephen Cameron 		dev_err(&h->pdev->dev,
7978050f7147SStephen Cameron 			"performant mode problem - transport not active\n");
7979c706a795SRobert Elliott 		return -ENODEV;
7980303932fdSDon Brace 	}
7981960a30e7SStephen M. Cameron 	/* Change the access methods to the performant access methods */
7982e1f7de0cSMatt Gates 	h->access = access;
7983e1f7de0cSMatt Gates 	h->transMethod = transMethod;
7984e1f7de0cSMatt Gates 
7985b9af4937SStephen M. Cameron 	if (!((trans_support & CFGTBL_Trans_io_accel1) ||
7986b9af4937SStephen M. Cameron 		(trans_support & CFGTBL_Trans_io_accel2)))
7987c706a795SRobert Elliott 		return 0;
7988e1f7de0cSMatt Gates 
7989b9af4937SStephen M. Cameron 	if (trans_support & CFGTBL_Trans_io_accel1) {
7990e1f7de0cSMatt Gates 		/* Set up I/O accelerator mode */
7991e1f7de0cSMatt Gates 		for (i = 0; i < h->nreply_queues; i++) {
7992e1f7de0cSMatt Gates 			writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
7993e1f7de0cSMatt Gates 			h->reply_queue[i].current_entry =
7994e1f7de0cSMatt Gates 				readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
7995e1f7de0cSMatt Gates 		}
7996283b4a9bSStephen M. Cameron 		bft[7] = h->ioaccel_maxsg + 8;
7997283b4a9bSStephen M. Cameron 		calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
7998e1f7de0cSMatt Gates 				h->ioaccel1_blockFetchTable);
7999e1f7de0cSMatt Gates 
8000e1f7de0cSMatt Gates 		/* initialize all reply queue entries to unused */
8001072b0518SStephen M. Cameron 		for (i = 0; i < h->nreply_queues; i++)
8002072b0518SStephen M. Cameron 			memset(h->reply_queue[i].head,
8003072b0518SStephen M. Cameron 				(u8) IOACCEL_MODE1_REPLY_UNUSED,
8004072b0518SStephen M. Cameron 				h->reply_queue_size);
8005e1f7de0cSMatt Gates 
8006e1f7de0cSMatt Gates 		/* set all the constant fields in the accelerator command
8007e1f7de0cSMatt Gates 		 * frames once at init time to save CPU cycles later.
8008e1f7de0cSMatt Gates 		 */
8009e1f7de0cSMatt Gates 		for (i = 0; i < h->nr_cmds; i++) {
8010e1f7de0cSMatt Gates 			struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
8011e1f7de0cSMatt Gates 
8012e1f7de0cSMatt Gates 			cp->function = IOACCEL1_FUNCTION_SCSIIO;
8013e1f7de0cSMatt Gates 			cp->err_info = (u32) (h->errinfo_pool_dhandle +
8014e1f7de0cSMatt Gates 					(i * sizeof(struct ErrorInfo)));
8015e1f7de0cSMatt Gates 			cp->err_info_len = sizeof(struct ErrorInfo);
8016e1f7de0cSMatt Gates 			cp->sgl_offset = IOACCEL1_SGLOFFSET;
80172b08b3e9SDon Brace 			cp->host_context_flags =
80182b08b3e9SDon Brace 				cpu_to_le16(IOACCEL1_HCFLAGS_CISS_FORMAT);
8019e1f7de0cSMatt Gates 			cp->timeout_sec = 0;
8020e1f7de0cSMatt Gates 			cp->ReplyQueue = 0;
802150a0decfSStephen M. Cameron 			cp->tag =
8022f2405db8SDon Brace 				cpu_to_le64((i << DIRECT_LOOKUP_SHIFT));
802350a0decfSStephen M. Cameron 			cp->host_addr =
802450a0decfSStephen M. Cameron 				cpu_to_le64(h->ioaccel_cmd_pool_dhandle +
8025e1f7de0cSMatt Gates 					(i * sizeof(struct io_accel1_cmd)));
8026e1f7de0cSMatt Gates 		}
8027b9af4937SStephen M. Cameron 	} else if (trans_support & CFGTBL_Trans_io_accel2) {
8028b9af4937SStephen M. Cameron 		u64 cfg_offset, cfg_base_addr_index;
8029b9af4937SStephen M. Cameron 		u32 bft2_offset, cfg_base_addr;
8030b9af4937SStephen M. Cameron 		int rc;
8031b9af4937SStephen M. Cameron 
8032b9af4937SStephen M. Cameron 		rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
8033b9af4937SStephen M. Cameron 			&cfg_base_addr_index, &cfg_offset);
8034b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
8035b9af4937SStephen M. Cameron 		bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
8036b9af4937SStephen M. Cameron 		calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
8037b9af4937SStephen M. Cameron 				4, h->ioaccel2_blockFetchTable);
8038b9af4937SStephen M. Cameron 		bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
8039b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct CfgTable,
8040b9af4937SStephen M. Cameron 				io_accel_request_size_offset) != 0xb8);
8041b9af4937SStephen M. Cameron 		h->ioaccel2_bft2_regs =
8042b9af4937SStephen M. Cameron 			remap_pci_mem(pci_resource_start(h->pdev,
8043b9af4937SStephen M. Cameron 					cfg_base_addr_index) +
8044b9af4937SStephen M. Cameron 					cfg_offset + bft2_offset,
8045b9af4937SStephen M. Cameron 					ARRAY_SIZE(bft2) *
8046b9af4937SStephen M. Cameron 					sizeof(*h->ioaccel2_bft2_regs));
8047b9af4937SStephen M. Cameron 		for (i = 0; i < ARRAY_SIZE(bft2); i++)
8048b9af4937SStephen M. Cameron 			writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
8049b9af4937SStephen M. Cameron 	}
8050b9af4937SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
8051c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h)) {
8052c706a795SRobert Elliott 		dev_err(&h->pdev->dev,
8053c706a795SRobert Elliott 			"performant mode problem - enabling ioaccel mode\n");
8054c706a795SRobert Elliott 		return -ENODEV;
8055c706a795SRobert Elliott 	}
8056c706a795SRobert Elliott 	return 0;
8057e1f7de0cSMatt Gates }
8058e1f7de0cSMatt Gates 
80591fb7c98aSRobert Elliott /* Free ioaccel1 mode command blocks and block fetch table */
80601fb7c98aSRobert Elliott static void hpsa_free_ioaccel1_cmd_and_bft(struct ctlr_info *h)
80611fb7c98aSRobert Elliott {
8062105a3dbcSRobert Elliott 	if (h->ioaccel_cmd_pool) {
80631fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
80641fb7c98aSRobert Elliott 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
80651fb7c98aSRobert Elliott 			h->ioaccel_cmd_pool,
80661fb7c98aSRobert Elliott 			h->ioaccel_cmd_pool_dhandle);
8067105a3dbcSRobert Elliott 		h->ioaccel_cmd_pool = NULL;
8068105a3dbcSRobert Elliott 		h->ioaccel_cmd_pool_dhandle = 0;
8069105a3dbcSRobert Elliott 	}
80701fb7c98aSRobert Elliott 	kfree(h->ioaccel1_blockFetchTable);
8071105a3dbcSRobert Elliott 	h->ioaccel1_blockFetchTable = NULL;
80721fb7c98aSRobert Elliott }
80731fb7c98aSRobert Elliott 
8074d37ffbe4SRobert Elliott /* Allocate ioaccel1 mode command blocks and block fetch table */
8075d37ffbe4SRobert Elliott static int hpsa_alloc_ioaccel1_cmd_and_bft(struct ctlr_info *h)
8076e1f7de0cSMatt Gates {
8077283b4a9bSStephen M. Cameron 	h->ioaccel_maxsg =
8078283b4a9bSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
8079283b4a9bSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
8080283b4a9bSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
8081283b4a9bSStephen M. Cameron 
8082e1f7de0cSMatt Gates 	/* Command structures must be aligned on a 128-byte boundary
8083e1f7de0cSMatt Gates 	 * because the 7 lower bits of the address are used by the
8084e1f7de0cSMatt Gates 	 * hardware.
8085e1f7de0cSMatt Gates 	 */
8086e1f7de0cSMatt Gates 	BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
8087e1f7de0cSMatt Gates 			IOACCEL1_COMMANDLIST_ALIGNMENT);
8088e1f7de0cSMatt Gates 	h->ioaccel_cmd_pool =
8089e1f7de0cSMatt Gates 		pci_alloc_consistent(h->pdev,
8090e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
8091e1f7de0cSMatt Gates 			&(h->ioaccel_cmd_pool_dhandle));
8092e1f7de0cSMatt Gates 
8093e1f7de0cSMatt Gates 	h->ioaccel1_blockFetchTable =
8094283b4a9bSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
8095e1f7de0cSMatt Gates 				sizeof(u32)), GFP_KERNEL);
8096e1f7de0cSMatt Gates 
8097e1f7de0cSMatt Gates 	if ((h->ioaccel_cmd_pool == NULL) ||
8098e1f7de0cSMatt Gates 		(h->ioaccel1_blockFetchTable == NULL))
8099e1f7de0cSMatt Gates 		goto clean_up;
8100e1f7de0cSMatt Gates 
8101e1f7de0cSMatt Gates 	memset(h->ioaccel_cmd_pool, 0,
8102e1f7de0cSMatt Gates 		h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
8103e1f7de0cSMatt Gates 	return 0;
8104e1f7de0cSMatt Gates 
8105e1f7de0cSMatt Gates clean_up:
81061fb7c98aSRobert Elliott 	hpsa_free_ioaccel1_cmd_and_bft(h);
81072dd02d74SRobert Elliott 	return -ENOMEM;
81086c311b57SStephen M. Cameron }
81096c311b57SStephen M. Cameron 
81101fb7c98aSRobert Elliott /* Free ioaccel2 mode command blocks and block fetch table */
81111fb7c98aSRobert Elliott static void hpsa_free_ioaccel2_cmd_and_bft(struct ctlr_info *h)
81121fb7c98aSRobert Elliott {
8113d9a729f3SWebb Scales 	hpsa_free_ioaccel2_sg_chain_blocks(h);
8114d9a729f3SWebb Scales 
8115105a3dbcSRobert Elliott 	if (h->ioaccel2_cmd_pool) {
81161fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
81171fb7c98aSRobert Elliott 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
81181fb7c98aSRobert Elliott 			h->ioaccel2_cmd_pool,
81191fb7c98aSRobert Elliott 			h->ioaccel2_cmd_pool_dhandle);
8120105a3dbcSRobert Elliott 		h->ioaccel2_cmd_pool = NULL;
8121105a3dbcSRobert Elliott 		h->ioaccel2_cmd_pool_dhandle = 0;
8122105a3dbcSRobert Elliott 	}
81231fb7c98aSRobert Elliott 	kfree(h->ioaccel2_blockFetchTable);
8124105a3dbcSRobert Elliott 	h->ioaccel2_blockFetchTable = NULL;
81251fb7c98aSRobert Elliott }
81261fb7c98aSRobert Elliott 
8127d37ffbe4SRobert Elliott /* Allocate ioaccel2 mode command blocks and block fetch table */
8128d37ffbe4SRobert Elliott static int hpsa_alloc_ioaccel2_cmd_and_bft(struct ctlr_info *h)
8129aca9012aSStephen M. Cameron {
8130d9a729f3SWebb Scales 	int rc;
8131d9a729f3SWebb Scales 
8132aca9012aSStephen M. Cameron 	/* Allocate ioaccel2 mode command blocks and block fetch table */
8133aca9012aSStephen M. Cameron 
8134aca9012aSStephen M. Cameron 	h->ioaccel_maxsg =
8135aca9012aSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
8136aca9012aSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
8137aca9012aSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
8138aca9012aSStephen M. Cameron 
8139aca9012aSStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
8140aca9012aSStephen M. Cameron 			IOACCEL2_COMMANDLIST_ALIGNMENT);
8141aca9012aSStephen M. Cameron 	h->ioaccel2_cmd_pool =
8142aca9012aSStephen M. Cameron 		pci_alloc_consistent(h->pdev,
8143aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
8144aca9012aSStephen M. Cameron 			&(h->ioaccel2_cmd_pool_dhandle));
8145aca9012aSStephen M. Cameron 
8146aca9012aSStephen M. Cameron 	h->ioaccel2_blockFetchTable =
8147aca9012aSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
8148aca9012aSStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
8149aca9012aSStephen M. Cameron 
8150aca9012aSStephen M. Cameron 	if ((h->ioaccel2_cmd_pool == NULL) ||
8151d9a729f3SWebb Scales 		(h->ioaccel2_blockFetchTable == NULL)) {
8152d9a729f3SWebb Scales 		rc = -ENOMEM;
8153d9a729f3SWebb Scales 		goto clean_up;
8154d9a729f3SWebb Scales 	}
8155d9a729f3SWebb Scales 
8156d9a729f3SWebb Scales 	rc = hpsa_allocate_ioaccel2_sg_chain_blocks(h);
8157d9a729f3SWebb Scales 	if (rc)
8158aca9012aSStephen M. Cameron 		goto clean_up;
8159aca9012aSStephen M. Cameron 
8160aca9012aSStephen M. Cameron 	memset(h->ioaccel2_cmd_pool, 0,
8161aca9012aSStephen M. Cameron 		h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
8162aca9012aSStephen M. Cameron 	return 0;
8163aca9012aSStephen M. Cameron 
8164aca9012aSStephen M. Cameron clean_up:
81651fb7c98aSRobert Elliott 	hpsa_free_ioaccel2_cmd_and_bft(h);
8166d9a729f3SWebb Scales 	return rc;
8167aca9012aSStephen M. Cameron }
8168aca9012aSStephen M. Cameron 
8169105a3dbcSRobert Elliott /* Free items allocated by hpsa_put_ctlr_into_performant_mode */
8170105a3dbcSRobert Elliott static void hpsa_free_performant_mode(struct ctlr_info *h)
8171105a3dbcSRobert Elliott {
8172105a3dbcSRobert Elliott 	kfree(h->blockFetchTable);
8173105a3dbcSRobert Elliott 	h->blockFetchTable = NULL;
8174105a3dbcSRobert Elliott 	hpsa_free_reply_queues(h);
8175105a3dbcSRobert Elliott 	hpsa_free_ioaccel1_cmd_and_bft(h);
8176105a3dbcSRobert Elliott 	hpsa_free_ioaccel2_cmd_and_bft(h);
8177105a3dbcSRobert Elliott }
8178105a3dbcSRobert Elliott 
8179105a3dbcSRobert Elliott /* return -ENODEV on error, 0 on success (or no action)
8180105a3dbcSRobert Elliott  * allocates numerous items that must be freed later
8181105a3dbcSRobert Elliott  */
8182105a3dbcSRobert Elliott static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
81836c311b57SStephen M. Cameron {
81846c311b57SStephen M. Cameron 	u32 trans_support;
8185e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
8186e1f7de0cSMatt Gates 					CFGTBL_Trans_use_short_tags;
8187105a3dbcSRobert Elliott 	int i, rc;
81886c311b57SStephen M. Cameron 
818902ec19c8SStephen M. Cameron 	if (hpsa_simple_mode)
8190105a3dbcSRobert Elliott 		return 0;
819102ec19c8SStephen M. Cameron 
819267c99a72Sscameron@beardog.cce.hp.com 	trans_support = readl(&(h->cfgtable->TransportSupport));
819367c99a72Sscameron@beardog.cce.hp.com 	if (!(trans_support & PERFORMANT_MODE))
8194105a3dbcSRobert Elliott 		return 0;
819567c99a72Sscameron@beardog.cce.hp.com 
8196e1f7de0cSMatt Gates 	/* Check for I/O accelerator mode support */
8197e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
8198e1f7de0cSMatt Gates 		transMethod |= CFGTBL_Trans_io_accel1 |
8199e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix;
8200105a3dbcSRobert Elliott 		rc = hpsa_alloc_ioaccel1_cmd_and_bft(h);
8201105a3dbcSRobert Elliott 		if (rc)
8202105a3dbcSRobert Elliott 			return rc;
8203105a3dbcSRobert Elliott 	} else if (trans_support & CFGTBL_Trans_io_accel2) {
8204aca9012aSStephen M. Cameron 		transMethod |= CFGTBL_Trans_io_accel2 |
8205aca9012aSStephen M. Cameron 				CFGTBL_Trans_enable_directed_msix;
8206105a3dbcSRobert Elliott 		rc = hpsa_alloc_ioaccel2_cmd_and_bft(h);
8207105a3dbcSRobert Elliott 		if (rc)
8208105a3dbcSRobert Elliott 			return rc;
8209e1f7de0cSMatt Gates 	}
8210e1f7de0cSMatt Gates 
8211eee0f03aSHannes Reinecke 	h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
8212cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
82136c311b57SStephen M. Cameron 	/* Performant mode ring buffer and supporting data structures */
8214072b0518SStephen M. Cameron 	h->reply_queue_size = h->max_commands * sizeof(u64);
82156c311b57SStephen M. Cameron 
8216254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
8217072b0518SStephen M. Cameron 		h->reply_queue[i].head = pci_alloc_consistent(h->pdev,
8218072b0518SStephen M. Cameron 						h->reply_queue_size,
8219072b0518SStephen M. Cameron 						&(h->reply_queue[i].busaddr));
8220105a3dbcSRobert Elliott 		if (!h->reply_queue[i].head) {
8221105a3dbcSRobert Elliott 			rc = -ENOMEM;
8222105a3dbcSRobert Elliott 			goto clean1;	/* rq, ioaccel */
8223105a3dbcSRobert Elliott 		}
8224254f796bSMatt Gates 		h->reply_queue[i].size = h->max_commands;
8225254f796bSMatt Gates 		h->reply_queue[i].wraparound = 1;  /* spec: init to 1 */
8226254f796bSMatt Gates 		h->reply_queue[i].current_entry = 0;
8227254f796bSMatt Gates 	}
8228254f796bSMatt Gates 
82296c311b57SStephen M. Cameron 	/* Need a block fetch table for performant mode */
8230d66ae08bSStephen M. Cameron 	h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
82316c311b57SStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
8232105a3dbcSRobert Elliott 	if (!h->blockFetchTable) {
8233105a3dbcSRobert Elliott 		rc = -ENOMEM;
8234105a3dbcSRobert Elliott 		goto clean1;	/* rq, ioaccel */
8235105a3dbcSRobert Elliott 	}
82366c311b57SStephen M. Cameron 
8237105a3dbcSRobert Elliott 	rc = hpsa_enter_performant_mode(h, trans_support);
8238105a3dbcSRobert Elliott 	if (rc)
8239105a3dbcSRobert Elliott 		goto clean2;	/* bft, rq, ioaccel */
8240105a3dbcSRobert Elliott 	return 0;
8241303932fdSDon Brace 
8242105a3dbcSRobert Elliott clean2:	/* bft, rq, ioaccel */
8243303932fdSDon Brace 	kfree(h->blockFetchTable);
8244105a3dbcSRobert Elliott 	h->blockFetchTable = NULL;
8245105a3dbcSRobert Elliott clean1:	/* rq, ioaccel */
8246105a3dbcSRobert Elliott 	hpsa_free_reply_queues(h);
8247105a3dbcSRobert Elliott 	hpsa_free_ioaccel1_cmd_and_bft(h);
8248105a3dbcSRobert Elliott 	hpsa_free_ioaccel2_cmd_and_bft(h);
8249105a3dbcSRobert Elliott 	return rc;
8250303932fdSDon Brace }
8251303932fdSDon Brace 
825223100dd9SStephen M. Cameron static int is_accelerated_cmd(struct CommandList *c)
825376438d08SStephen M. Cameron {
825423100dd9SStephen M. Cameron 	return c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_IOACCEL2;
825523100dd9SStephen M. Cameron }
825623100dd9SStephen M. Cameron 
825723100dd9SStephen M. Cameron static void hpsa_drain_accel_commands(struct ctlr_info *h)
825823100dd9SStephen M. Cameron {
825923100dd9SStephen M. Cameron 	struct CommandList *c = NULL;
8260f2405db8SDon Brace 	int i, accel_cmds_out;
8261281a7fd0SWebb Scales 	int refcount;
826276438d08SStephen M. Cameron 
8263f2405db8SDon Brace 	do { /* wait for all outstanding ioaccel commands to drain out */
826423100dd9SStephen M. Cameron 		accel_cmds_out = 0;
8265f2405db8SDon Brace 		for (i = 0; i < h->nr_cmds; i++) {
8266f2405db8SDon Brace 			c = h->cmd_pool + i;
8267281a7fd0SWebb Scales 			refcount = atomic_inc_return(&c->refcount);
8268281a7fd0SWebb Scales 			if (refcount > 1) /* Command is allocated */
826923100dd9SStephen M. Cameron 				accel_cmds_out += is_accelerated_cmd(c);
8270281a7fd0SWebb Scales 			cmd_free(h, c);
8271f2405db8SDon Brace 		}
827223100dd9SStephen M. Cameron 		if (accel_cmds_out <= 0)
827376438d08SStephen M. Cameron 			break;
827476438d08SStephen M. Cameron 		msleep(100);
827576438d08SStephen M. Cameron 	} while (1);
827676438d08SStephen M. Cameron }
827776438d08SStephen M. Cameron 
8278edd16368SStephen M. Cameron /*
8279edd16368SStephen M. Cameron  *  This is it.  Register the PCI driver information for the cards we control
8280edd16368SStephen M. Cameron  *  the OS will call our registered routines when it finds one of our cards.
8281edd16368SStephen M. Cameron  */
8282edd16368SStephen M. Cameron static int __init hpsa_init(void)
8283edd16368SStephen M. Cameron {
828431468401SMike Miller 	return pci_register_driver(&hpsa_pci_driver);
8285edd16368SStephen M. Cameron }
8286edd16368SStephen M. Cameron 
8287edd16368SStephen M. Cameron static void __exit hpsa_cleanup(void)
8288edd16368SStephen M. Cameron {
8289edd16368SStephen M. Cameron 	pci_unregister_driver(&hpsa_pci_driver);
8290edd16368SStephen M. Cameron }
8291edd16368SStephen M. Cameron 
8292e1f7de0cSMatt Gates static void __attribute__((unused)) verify_offsets(void)
8293e1f7de0cSMatt Gates {
8294e1f7de0cSMatt Gates #define VERIFY_OFFSET(member, offset) \
8295dd0e19f3SScott Teel 	BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
8296dd0e19f3SScott Teel 
8297dd0e19f3SScott Teel 	VERIFY_OFFSET(structure_size, 0);
8298dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_size, 4);
8299dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_cnt, 8);
8300dd0e19f3SScott Teel 	VERIFY_OFFSET(phys_blk_shift, 16);
8301dd0e19f3SScott Teel 	VERIFY_OFFSET(parity_rotation_shift, 17);
8302dd0e19f3SScott Teel 	VERIFY_OFFSET(strip_size, 18);
8303dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_starting_blk, 20);
8304dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_blk_cnt, 28);
8305dd0e19f3SScott Teel 	VERIFY_OFFSET(data_disks_per_row, 36);
8306dd0e19f3SScott Teel 	VERIFY_OFFSET(metadata_disks_per_row, 38);
8307dd0e19f3SScott Teel 	VERIFY_OFFSET(row_cnt, 40);
8308dd0e19f3SScott Teel 	VERIFY_OFFSET(layout_map_count, 42);
8309dd0e19f3SScott Teel 	VERIFY_OFFSET(flags, 44);
8310dd0e19f3SScott Teel 	VERIFY_OFFSET(dekindex, 46);
8311dd0e19f3SScott Teel 	/* VERIFY_OFFSET(reserved, 48 */
8312dd0e19f3SScott Teel 	VERIFY_OFFSET(data, 64);
8313dd0e19f3SScott Teel 
8314dd0e19f3SScott Teel #undef VERIFY_OFFSET
8315dd0e19f3SScott Teel 
8316dd0e19f3SScott Teel #define VERIFY_OFFSET(member, offset) \
8317b66cc250SMike Miller 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
8318b66cc250SMike Miller 
8319b66cc250SMike Miller 	VERIFY_OFFSET(IU_type, 0);
8320b66cc250SMike Miller 	VERIFY_OFFSET(direction, 1);
8321b66cc250SMike Miller 	VERIFY_OFFSET(reply_queue, 2);
8322b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved1, 3);  */
8323b66cc250SMike Miller 	VERIFY_OFFSET(scsi_nexus, 4);
8324b66cc250SMike Miller 	VERIFY_OFFSET(Tag, 8);
8325b66cc250SMike Miller 	VERIFY_OFFSET(cdb, 16);
8326b66cc250SMike Miller 	VERIFY_OFFSET(cciss_lun, 32);
8327b66cc250SMike Miller 	VERIFY_OFFSET(data_len, 40);
8328b66cc250SMike Miller 	VERIFY_OFFSET(cmd_priority_task_attr, 44);
8329b66cc250SMike Miller 	VERIFY_OFFSET(sg_count, 45);
8330b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved3 */
8331b66cc250SMike Miller 	VERIFY_OFFSET(err_ptr, 48);
8332b66cc250SMike Miller 	VERIFY_OFFSET(err_len, 56);
8333b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved4  */
8334b66cc250SMike Miller 	VERIFY_OFFSET(sg, 64);
8335b66cc250SMike Miller 
8336b66cc250SMike Miller #undef VERIFY_OFFSET
8337b66cc250SMike Miller 
8338b66cc250SMike Miller #define VERIFY_OFFSET(member, offset) \
8339e1f7de0cSMatt Gates 	BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
8340e1f7de0cSMatt Gates 
8341e1f7de0cSMatt Gates 	VERIFY_OFFSET(dev_handle, 0x00);
8342e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved1, 0x02);
8343e1f7de0cSMatt Gates 	VERIFY_OFFSET(function, 0x03);
8344e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved2, 0x04);
8345e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info, 0x0C);
8346e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved3, 0x10);
8347e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info_len, 0x12);
8348e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved4, 0x13);
8349e1f7de0cSMatt Gates 	VERIFY_OFFSET(sgl_offset, 0x14);
8350e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved5, 0x15);
8351e1f7de0cSMatt Gates 	VERIFY_OFFSET(transfer_len, 0x1C);
8352e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved6, 0x20);
8353e1f7de0cSMatt Gates 	VERIFY_OFFSET(io_flags, 0x24);
8354e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved7, 0x26);
8355e1f7de0cSMatt Gates 	VERIFY_OFFSET(LUN, 0x34);
8356e1f7de0cSMatt Gates 	VERIFY_OFFSET(control, 0x3C);
8357e1f7de0cSMatt Gates 	VERIFY_OFFSET(CDB, 0x40);
8358e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved8, 0x50);
8359e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_context_flags, 0x60);
8360e1f7de0cSMatt Gates 	VERIFY_OFFSET(timeout_sec, 0x62);
8361e1f7de0cSMatt Gates 	VERIFY_OFFSET(ReplyQueue, 0x64);
8362e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved9, 0x65);
836350a0decfSStephen M. Cameron 	VERIFY_OFFSET(tag, 0x68);
8364e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_addr, 0x70);
8365e1f7de0cSMatt Gates 	VERIFY_OFFSET(CISS_LUN, 0x78);
8366e1f7de0cSMatt Gates 	VERIFY_OFFSET(SG, 0x78 + 8);
8367e1f7de0cSMatt Gates #undef VERIFY_OFFSET
8368e1f7de0cSMatt Gates }
8369e1f7de0cSMatt Gates 
8370edd16368SStephen M. Cameron module_init(hpsa_init);
8371edd16368SStephen M. Cameron module_exit(hpsa_cleanup);
8372