xref: /openbmc/linux/drivers/scsi/hpsa.c (revision bf43caf316926fced73cfddad9f1b3f4251717e5)
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);
2376f039790SGreg Kroah-Hartman static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
2381fb7c98aSRobert Elliott static void hpsa_free_ioaccel1_cmd_and_bft(struct ctlr_info *h);
2391fb7c98aSRobert Elliott static void hpsa_free_ioaccel2_cmd_and_bft(struct ctlr_info *h);
240254f796bSMatt Gates static inline u32 next_command(struct ctlr_info *h, u8 q);
2416f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
2426f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
2431df8552aSStephen M. Cameron 			       u64 *cfg_offset);
2446f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
2451df8552aSStephen M. Cameron 				    unsigned long *memory_bar);
2466f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
2476f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
2486f039790SGreg Kroah-Hartman 				     int wait_for_ready);
24975167d2cSStephen M. Cameron static inline void finish_cmd(struct CommandList *c);
250c706a795SRobert Elliott static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h);
251fe5389c8SStephen M. Cameron #define BOARD_NOT_READY 0
252fe5389c8SStephen M. Cameron #define BOARD_READY 1
25323100dd9SStephen M. Cameron static void hpsa_drain_accel_commands(struct ctlr_info *h);
25476438d08SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h);
255c349775eSScott Teel static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
256c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
25703383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk);
258080ef1ccSDon Brace static void hpsa_command_resubmit_worker(struct work_struct *work);
25925163bd5SWebb Scales static u32 lockup_detected(struct ctlr_info *h);
26025163bd5SWebb Scales static int detect_controller_lockup(struct ctlr_info *h);
261edd16368SStephen M. Cameron 
262edd16368SStephen M. Cameron static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
263edd16368SStephen M. Cameron {
264edd16368SStephen M. Cameron 	unsigned long *priv = shost_priv(sdev->host);
265edd16368SStephen M. Cameron 	return (struct ctlr_info *) *priv;
266edd16368SStephen M. Cameron }
267edd16368SStephen M. Cameron 
268a23513e8SStephen M. Cameron static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
269a23513e8SStephen M. Cameron {
270a23513e8SStephen M. Cameron 	unsigned long *priv = shost_priv(sh);
271a23513e8SStephen M. Cameron 	return (struct ctlr_info *) *priv;
272a23513e8SStephen M. Cameron }
273a23513e8SStephen M. Cameron 
2749437ac43SStephen Cameron /* extract sense key, asc, and ascq from sense data.  -1 means invalid. */
2759437ac43SStephen Cameron static void decode_sense_data(const u8 *sense_data, int sense_data_len,
2769437ac43SStephen Cameron 			u8 *sense_key, u8 *asc, u8 *ascq)
2779437ac43SStephen Cameron {
2789437ac43SStephen Cameron 	struct scsi_sense_hdr sshdr;
2799437ac43SStephen Cameron 	bool rc;
2809437ac43SStephen Cameron 
2819437ac43SStephen Cameron 	*sense_key = -1;
2829437ac43SStephen Cameron 	*asc = -1;
2839437ac43SStephen Cameron 	*ascq = -1;
2849437ac43SStephen Cameron 
2859437ac43SStephen Cameron 	if (sense_data_len < 1)
2869437ac43SStephen Cameron 		return;
2879437ac43SStephen Cameron 
2889437ac43SStephen Cameron 	rc = scsi_normalize_sense(sense_data, sense_data_len, &sshdr);
2899437ac43SStephen Cameron 	if (rc) {
2909437ac43SStephen Cameron 		*sense_key = sshdr.sense_key;
2919437ac43SStephen Cameron 		*asc = sshdr.asc;
2929437ac43SStephen Cameron 		*ascq = sshdr.ascq;
2939437ac43SStephen Cameron 	}
2949437ac43SStephen Cameron }
2959437ac43SStephen Cameron 
296edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
297edd16368SStephen M. Cameron 	struct CommandList *c)
298edd16368SStephen M. Cameron {
2999437ac43SStephen Cameron 	u8 sense_key, asc, ascq;
3009437ac43SStephen Cameron 	int sense_len;
3019437ac43SStephen Cameron 
3029437ac43SStephen Cameron 	if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
3039437ac43SStephen Cameron 		sense_len = sizeof(c->err_info->SenseInfo);
3049437ac43SStephen Cameron 	else
3059437ac43SStephen Cameron 		sense_len = c->err_info->SenseLen;
3069437ac43SStephen Cameron 
3079437ac43SStephen Cameron 	decode_sense_data(c->err_info->SenseInfo, sense_len,
3089437ac43SStephen Cameron 				&sense_key, &asc, &ascq);
3099437ac43SStephen Cameron 	if (sense_key != UNIT_ATTENTION || asc == -1)
310edd16368SStephen M. Cameron 		return 0;
311edd16368SStephen M. Cameron 
3129437ac43SStephen Cameron 	switch (asc) {
313edd16368SStephen M. Cameron 	case STATE_CHANGED:
3149437ac43SStephen Cameron 		dev_warn(&h->pdev->dev,
3159437ac43SStephen Cameron 			HPSA "%d: a state change detected, command retried\n",
3169437ac43SStephen Cameron 			h->ctlr);
317edd16368SStephen M. Cameron 		break;
318edd16368SStephen M. Cameron 	case LUN_FAILED:
3197f73695aSStephen M. Cameron 		dev_warn(&h->pdev->dev,
3207f73695aSStephen M. Cameron 			HPSA "%d: LUN failure detected\n", h->ctlr);
321edd16368SStephen M. Cameron 		break;
322edd16368SStephen M. Cameron 	case REPORT_LUNS_CHANGED:
3237f73695aSStephen M. Cameron 		dev_warn(&h->pdev->dev,
3247f73695aSStephen M. Cameron 			HPSA "%d: report LUN data changed\n", h->ctlr);
325edd16368SStephen M. Cameron 	/*
3264f4eb9f1SScott Teel 	 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
3274f4eb9f1SScott Teel 	 * target (array) devices.
328edd16368SStephen M. Cameron 	 */
329edd16368SStephen M. Cameron 		break;
330edd16368SStephen M. Cameron 	case POWER_OR_RESET:
331f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: a power on "
332edd16368SStephen M. Cameron 			"or device reset detected\n", h->ctlr);
333edd16368SStephen M. Cameron 		break;
334edd16368SStephen M. Cameron 	case UNIT_ATTENTION_CLEARED:
335f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: unit attention "
336edd16368SStephen M. Cameron 		    "cleared by another initiator\n", h->ctlr);
337edd16368SStephen M. Cameron 		break;
338edd16368SStephen M. Cameron 	default:
339f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: unknown "
340edd16368SStephen M. Cameron 			"unit attention detected\n", h->ctlr);
341edd16368SStephen M. Cameron 		break;
342edd16368SStephen M. Cameron 	}
343edd16368SStephen M. Cameron 	return 1;
344edd16368SStephen M. Cameron }
345edd16368SStephen M. Cameron 
346852af20aSMatt Bondurant static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
347852af20aSMatt Bondurant {
348852af20aSMatt Bondurant 	if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
349852af20aSMatt Bondurant 		(c->err_info->ScsiStatus != SAM_STAT_BUSY &&
350852af20aSMatt Bondurant 		 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
351852af20aSMatt Bondurant 		return 0;
352852af20aSMatt Bondurant 	dev_warn(&h->pdev->dev, HPSA "device busy");
353852af20aSMatt Bondurant 	return 1;
354852af20aSMatt Bondurant }
355852af20aSMatt Bondurant 
356e985c58fSStephen Cameron static u32 lockup_detected(struct ctlr_info *h);
357e985c58fSStephen Cameron static ssize_t host_show_lockup_detected(struct device *dev,
358e985c58fSStephen Cameron 		struct device_attribute *attr, char *buf)
359e985c58fSStephen Cameron {
360e985c58fSStephen Cameron 	int ld;
361e985c58fSStephen Cameron 	struct ctlr_info *h;
362e985c58fSStephen Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
363e985c58fSStephen Cameron 
364e985c58fSStephen Cameron 	h = shost_to_hba(shost);
365e985c58fSStephen Cameron 	ld = lockup_detected(h);
366e985c58fSStephen Cameron 
367e985c58fSStephen Cameron 	return sprintf(buf, "ld=%d\n", ld);
368e985c58fSStephen Cameron }
369e985c58fSStephen Cameron 
370da0697bdSScott Teel static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
371da0697bdSScott Teel 					 struct device_attribute *attr,
372da0697bdSScott Teel 					 const char *buf, size_t count)
373da0697bdSScott Teel {
374da0697bdSScott Teel 	int status, len;
375da0697bdSScott Teel 	struct ctlr_info *h;
376da0697bdSScott Teel 	struct Scsi_Host *shost = class_to_shost(dev);
377da0697bdSScott Teel 	char tmpbuf[10];
378da0697bdSScott Teel 
379da0697bdSScott Teel 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
380da0697bdSScott Teel 		return -EACCES;
381da0697bdSScott Teel 	len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
382da0697bdSScott Teel 	strncpy(tmpbuf, buf, len);
383da0697bdSScott Teel 	tmpbuf[len] = '\0';
384da0697bdSScott Teel 	if (sscanf(tmpbuf, "%d", &status) != 1)
385da0697bdSScott Teel 		return -EINVAL;
386da0697bdSScott Teel 	h = shost_to_hba(shost);
387da0697bdSScott Teel 	h->acciopath_status = !!status;
388da0697bdSScott Teel 	dev_warn(&h->pdev->dev,
389da0697bdSScott Teel 		"hpsa: HP SSD Smart Path %s via sysfs update.\n",
390da0697bdSScott Teel 		h->acciopath_status ? "enabled" : "disabled");
391da0697bdSScott Teel 	return count;
392da0697bdSScott Teel }
393da0697bdSScott Teel 
3942ba8bfc8SStephen M. Cameron static ssize_t host_store_raid_offload_debug(struct device *dev,
3952ba8bfc8SStephen M. Cameron 					 struct device_attribute *attr,
3962ba8bfc8SStephen M. Cameron 					 const char *buf, size_t count)
3972ba8bfc8SStephen M. Cameron {
3982ba8bfc8SStephen M. Cameron 	int debug_level, len;
3992ba8bfc8SStephen M. Cameron 	struct ctlr_info *h;
4002ba8bfc8SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
4012ba8bfc8SStephen M. Cameron 	char tmpbuf[10];
4022ba8bfc8SStephen M. Cameron 
4032ba8bfc8SStephen M. Cameron 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
4042ba8bfc8SStephen M. Cameron 		return -EACCES;
4052ba8bfc8SStephen M. Cameron 	len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
4062ba8bfc8SStephen M. Cameron 	strncpy(tmpbuf, buf, len);
4072ba8bfc8SStephen M. Cameron 	tmpbuf[len] = '\0';
4082ba8bfc8SStephen M. Cameron 	if (sscanf(tmpbuf, "%d", &debug_level) != 1)
4092ba8bfc8SStephen M. Cameron 		return -EINVAL;
4102ba8bfc8SStephen M. Cameron 	if (debug_level < 0)
4112ba8bfc8SStephen M. Cameron 		debug_level = 0;
4122ba8bfc8SStephen M. Cameron 	h = shost_to_hba(shost);
4132ba8bfc8SStephen M. Cameron 	h->raid_offload_debug = debug_level;
4142ba8bfc8SStephen M. Cameron 	dev_warn(&h->pdev->dev, "hpsa: Set raid_offload_debug level = %d\n",
4152ba8bfc8SStephen M. Cameron 		h->raid_offload_debug);
4162ba8bfc8SStephen M. Cameron 	return count;
4172ba8bfc8SStephen M. Cameron }
4182ba8bfc8SStephen M. Cameron 
419edd16368SStephen M. Cameron static ssize_t host_store_rescan(struct device *dev,
420edd16368SStephen M. Cameron 				 struct device_attribute *attr,
421edd16368SStephen M. Cameron 				 const char *buf, size_t count)
422edd16368SStephen M. Cameron {
423edd16368SStephen M. Cameron 	struct ctlr_info *h;
424edd16368SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
425a23513e8SStephen M. Cameron 	h = shost_to_hba(shost);
42631468401SMike Miller 	hpsa_scan_start(h->scsi_host);
427edd16368SStephen M. Cameron 	return count;
428edd16368SStephen M. Cameron }
429edd16368SStephen M. Cameron 
430d28ce020SStephen M. Cameron static ssize_t host_show_firmware_revision(struct device *dev,
431d28ce020SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
432d28ce020SStephen M. Cameron {
433d28ce020SStephen M. Cameron 	struct ctlr_info *h;
434d28ce020SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
435d28ce020SStephen M. Cameron 	unsigned char *fwrev;
436d28ce020SStephen M. Cameron 
437d28ce020SStephen M. Cameron 	h = shost_to_hba(shost);
438d28ce020SStephen M. Cameron 	if (!h->hba_inquiry_data)
439d28ce020SStephen M. Cameron 		return 0;
440d28ce020SStephen M. Cameron 	fwrev = &h->hba_inquiry_data[32];
441d28ce020SStephen M. Cameron 	return snprintf(buf, 20, "%c%c%c%c\n",
442d28ce020SStephen M. Cameron 		fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
443d28ce020SStephen M. Cameron }
444d28ce020SStephen M. Cameron 
44594a13649SStephen M. Cameron static ssize_t host_show_commands_outstanding(struct device *dev,
44694a13649SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
44794a13649SStephen M. Cameron {
44894a13649SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
44994a13649SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(shost);
45094a13649SStephen M. Cameron 
4510cbf768eSStephen M. Cameron 	return snprintf(buf, 20, "%d\n",
4520cbf768eSStephen M. Cameron 			atomic_read(&h->commands_outstanding));
45394a13649SStephen M. Cameron }
45494a13649SStephen M. Cameron 
455745a7a25SStephen M. Cameron static ssize_t host_show_transport_mode(struct device *dev,
456745a7a25SStephen M. Cameron 	struct device_attribute *attr, char *buf)
457745a7a25SStephen M. Cameron {
458745a7a25SStephen M. Cameron 	struct ctlr_info *h;
459745a7a25SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
460745a7a25SStephen M. Cameron 
461745a7a25SStephen M. Cameron 	h = shost_to_hba(shost);
462745a7a25SStephen M. Cameron 	return snprintf(buf, 20, "%s\n",
463960a30e7SStephen M. Cameron 		h->transMethod & CFGTBL_Trans_Performant ?
464745a7a25SStephen M. Cameron 			"performant" : "simple");
465745a7a25SStephen M. Cameron }
466745a7a25SStephen M. Cameron 
467da0697bdSScott Teel static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
468da0697bdSScott Teel 	struct device_attribute *attr, char *buf)
469da0697bdSScott Teel {
470da0697bdSScott Teel 	struct ctlr_info *h;
471da0697bdSScott Teel 	struct Scsi_Host *shost = class_to_shost(dev);
472da0697bdSScott Teel 
473da0697bdSScott Teel 	h = shost_to_hba(shost);
474da0697bdSScott Teel 	return snprintf(buf, 30, "HP SSD Smart Path %s\n",
475da0697bdSScott Teel 		(h->acciopath_status == 1) ?  "enabled" : "disabled");
476da0697bdSScott Teel }
477da0697bdSScott Teel 
47846380786SStephen M. Cameron /* List of controllers which cannot be hard reset on kexec with reset_devices */
479941b1cdaSStephen M. Cameron static u32 unresettable_controller[] = {
480941b1cdaSStephen M. Cameron 	0x324a103C, /* Smart Array P712m */
481941b1cdaSStephen M. Cameron 	0x324b103C, /* Smart Array P711m */
482941b1cdaSStephen M. Cameron 	0x3223103C, /* Smart Array P800 */
483941b1cdaSStephen M. Cameron 	0x3234103C, /* Smart Array P400 */
484941b1cdaSStephen M. Cameron 	0x3235103C, /* Smart Array P400i */
485941b1cdaSStephen M. Cameron 	0x3211103C, /* Smart Array E200i */
486941b1cdaSStephen M. Cameron 	0x3212103C, /* Smart Array E200 */
487941b1cdaSStephen M. Cameron 	0x3213103C, /* Smart Array E200i */
488941b1cdaSStephen M. Cameron 	0x3214103C, /* Smart Array E200i */
489941b1cdaSStephen M. Cameron 	0x3215103C, /* Smart Array E200i */
490941b1cdaSStephen M. Cameron 	0x3237103C, /* Smart Array E500 */
491941b1cdaSStephen M. Cameron 	0x323D103C, /* Smart Array P700m */
4927af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
493941b1cdaSStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
494941b1cdaSStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
4955a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
4965a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
4975a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
4985a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
4995a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
5005a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
501941b1cdaSStephen M. Cameron };
502941b1cdaSStephen M. Cameron 
50346380786SStephen M. Cameron /* List of controllers which cannot even be soft reset */
50446380786SStephen M. Cameron static u32 soft_unresettable_controller[] = {
5057af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
5065a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
5075a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
5085a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
5095a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
5105a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
5115a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
51246380786SStephen M. Cameron 	/* Exclude 640x boards.  These are two pci devices in one slot
51346380786SStephen M. Cameron 	 * which share a battery backed cache module.  One controls the
51446380786SStephen M. Cameron 	 * cache, the other accesses the cache through the one that controls
51546380786SStephen M. Cameron 	 * it.  If we reset the one controlling the cache, the other will
51646380786SStephen M. Cameron 	 * likely not be happy.  Just forbid resetting this conjoined mess.
51746380786SStephen M. Cameron 	 * The 640x isn't really supported by hpsa anyway.
51846380786SStephen M. Cameron 	 */
51946380786SStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
52046380786SStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
52146380786SStephen M. Cameron };
52246380786SStephen M. Cameron 
5239b5c48c2SStephen Cameron static u32 needs_abort_tags_swizzled[] = {
5249b5c48c2SStephen Cameron 	0x323D103C, /* Smart Array P700m */
5259b5c48c2SStephen Cameron 	0x324a103C, /* Smart Array P712m */
5269b5c48c2SStephen Cameron 	0x324b103C, /* SmartArray P711m */
5279b5c48c2SStephen Cameron };
5289b5c48c2SStephen Cameron 
5299b5c48c2SStephen Cameron static int board_id_in_array(u32 a[], int nelems, u32 board_id)
530941b1cdaSStephen M. Cameron {
531941b1cdaSStephen M. Cameron 	int i;
532941b1cdaSStephen M. Cameron 
5339b5c48c2SStephen Cameron 	for (i = 0; i < nelems; i++)
5349b5c48c2SStephen Cameron 		if (a[i] == board_id)
535941b1cdaSStephen M. Cameron 			return 1;
5369b5c48c2SStephen Cameron 	return 0;
5379b5c48c2SStephen Cameron }
5389b5c48c2SStephen Cameron 
5399b5c48c2SStephen Cameron static int ctlr_is_hard_resettable(u32 board_id)
5409b5c48c2SStephen Cameron {
5419b5c48c2SStephen Cameron 	return !board_id_in_array(unresettable_controller,
5429b5c48c2SStephen Cameron 			ARRAY_SIZE(unresettable_controller), board_id);
543941b1cdaSStephen M. Cameron }
544941b1cdaSStephen M. Cameron 
54546380786SStephen M. Cameron static int ctlr_is_soft_resettable(u32 board_id)
54646380786SStephen M. Cameron {
5479b5c48c2SStephen Cameron 	return !board_id_in_array(soft_unresettable_controller,
5489b5c48c2SStephen Cameron 			ARRAY_SIZE(soft_unresettable_controller), board_id);
54946380786SStephen M. Cameron }
55046380786SStephen M. Cameron 
55146380786SStephen M. Cameron static int ctlr_is_resettable(u32 board_id)
55246380786SStephen M. Cameron {
55346380786SStephen M. Cameron 	return ctlr_is_hard_resettable(board_id) ||
55446380786SStephen M. Cameron 		ctlr_is_soft_resettable(board_id);
55546380786SStephen M. Cameron }
55646380786SStephen M. Cameron 
5579b5c48c2SStephen Cameron static int ctlr_needs_abort_tags_swizzled(u32 board_id)
5589b5c48c2SStephen Cameron {
5599b5c48c2SStephen Cameron 	return board_id_in_array(needs_abort_tags_swizzled,
5609b5c48c2SStephen Cameron 			ARRAY_SIZE(needs_abort_tags_swizzled), board_id);
5619b5c48c2SStephen Cameron }
5629b5c48c2SStephen Cameron 
563941b1cdaSStephen M. Cameron static ssize_t host_show_resettable(struct device *dev,
564941b1cdaSStephen M. Cameron 	struct device_attribute *attr, char *buf)
565941b1cdaSStephen M. Cameron {
566941b1cdaSStephen M. Cameron 	struct ctlr_info *h;
567941b1cdaSStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
568941b1cdaSStephen M. Cameron 
569941b1cdaSStephen M. Cameron 	h = shost_to_hba(shost);
57046380786SStephen M. Cameron 	return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
571941b1cdaSStephen M. Cameron }
572941b1cdaSStephen M. Cameron 
573edd16368SStephen M. Cameron static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
574edd16368SStephen M. Cameron {
575edd16368SStephen M. Cameron 	return (scsi3addr[3] & 0xC0) == 0x40;
576edd16368SStephen M. Cameron }
577edd16368SStephen M. Cameron 
578f2ef0ce7SRobert Elliott static const char * const raid_label[] = { "0", "4", "1(+0)", "5", "5+1", "6",
579f2ef0ce7SRobert Elliott 	"1(+0)ADM", "UNKNOWN"
580edd16368SStephen M. Cameron };
5816b80b18fSScott Teel #define HPSA_RAID_0	0
5826b80b18fSScott Teel #define HPSA_RAID_4	1
5836b80b18fSScott Teel #define HPSA_RAID_1	2	/* also used for RAID 10 */
5846b80b18fSScott Teel #define HPSA_RAID_5	3	/* also used for RAID 50 */
5856b80b18fSScott Teel #define HPSA_RAID_51	4
5866b80b18fSScott Teel #define HPSA_RAID_6	5	/* also used for RAID 60 */
5876b80b18fSScott Teel #define HPSA_RAID_ADM	6	/* also used for RAID 1+0 ADM */
588edd16368SStephen M. Cameron #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
589edd16368SStephen M. Cameron 
590edd16368SStephen M. Cameron static ssize_t raid_level_show(struct device *dev,
591edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
592edd16368SStephen M. Cameron {
593edd16368SStephen M. Cameron 	ssize_t l = 0;
59482a72c0aSStephen M. Cameron 	unsigned char rlevel;
595edd16368SStephen M. Cameron 	struct ctlr_info *h;
596edd16368SStephen M. Cameron 	struct scsi_device *sdev;
597edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
598edd16368SStephen M. Cameron 	unsigned long flags;
599edd16368SStephen M. Cameron 
600edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
601edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
602edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
603edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
604edd16368SStephen M. Cameron 	if (!hdev) {
605edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
606edd16368SStephen M. Cameron 		return -ENODEV;
607edd16368SStephen M. Cameron 	}
608edd16368SStephen M. Cameron 
609edd16368SStephen M. Cameron 	/* Is this even a logical drive? */
610edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
611edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
612edd16368SStephen M. Cameron 		l = snprintf(buf, PAGE_SIZE, "N/A\n");
613edd16368SStephen M. Cameron 		return l;
614edd16368SStephen M. Cameron 	}
615edd16368SStephen M. Cameron 
616edd16368SStephen M. Cameron 	rlevel = hdev->raid_level;
617edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
61882a72c0aSStephen M. Cameron 	if (rlevel > RAID_UNKNOWN)
619edd16368SStephen M. Cameron 		rlevel = RAID_UNKNOWN;
620edd16368SStephen M. Cameron 	l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
621edd16368SStephen M. Cameron 	return l;
622edd16368SStephen M. Cameron }
623edd16368SStephen M. Cameron 
624edd16368SStephen M. Cameron static ssize_t lunid_show(struct device *dev,
625edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
626edd16368SStephen M. Cameron {
627edd16368SStephen M. Cameron 	struct ctlr_info *h;
628edd16368SStephen M. Cameron 	struct scsi_device *sdev;
629edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
630edd16368SStephen M. Cameron 	unsigned long flags;
631edd16368SStephen M. Cameron 	unsigned char lunid[8];
632edd16368SStephen M. Cameron 
633edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
634edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
635edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
636edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
637edd16368SStephen M. Cameron 	if (!hdev) {
638edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
639edd16368SStephen M. Cameron 		return -ENODEV;
640edd16368SStephen M. Cameron 	}
641edd16368SStephen M. Cameron 	memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
642edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
643edd16368SStephen M. Cameron 	return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
644edd16368SStephen M. Cameron 		lunid[0], lunid[1], lunid[2], lunid[3],
645edd16368SStephen M. Cameron 		lunid[4], lunid[5], lunid[6], lunid[7]);
646edd16368SStephen M. Cameron }
647edd16368SStephen M. Cameron 
648edd16368SStephen M. Cameron static ssize_t unique_id_show(struct device *dev,
649edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
650edd16368SStephen M. Cameron {
651edd16368SStephen M. Cameron 	struct ctlr_info *h;
652edd16368SStephen M. Cameron 	struct scsi_device *sdev;
653edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
654edd16368SStephen M. Cameron 	unsigned long flags;
655edd16368SStephen M. Cameron 	unsigned char sn[16];
656edd16368SStephen M. Cameron 
657edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
658edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
659edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
660edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
661edd16368SStephen M. Cameron 	if (!hdev) {
662edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
663edd16368SStephen M. Cameron 		return -ENODEV;
664edd16368SStephen M. Cameron 	}
665edd16368SStephen M. Cameron 	memcpy(sn, hdev->device_id, sizeof(sn));
666edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
667edd16368SStephen M. Cameron 	return snprintf(buf, 16 * 2 + 2,
668edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X"
669edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X\n",
670edd16368SStephen M. Cameron 			sn[0], sn[1], sn[2], sn[3],
671edd16368SStephen M. Cameron 			sn[4], sn[5], sn[6], sn[7],
672edd16368SStephen M. Cameron 			sn[8], sn[9], sn[10], sn[11],
673edd16368SStephen M. Cameron 			sn[12], sn[13], sn[14], sn[15]);
674edd16368SStephen M. Cameron }
675edd16368SStephen M. Cameron 
676c1988684SScott Teel static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
677c1988684SScott Teel 	     struct device_attribute *attr, char *buf)
678c1988684SScott Teel {
679c1988684SScott Teel 	struct ctlr_info *h;
680c1988684SScott Teel 	struct scsi_device *sdev;
681c1988684SScott Teel 	struct hpsa_scsi_dev_t *hdev;
682c1988684SScott Teel 	unsigned long flags;
683c1988684SScott Teel 	int offload_enabled;
684c1988684SScott Teel 
685c1988684SScott Teel 	sdev = to_scsi_device(dev);
686c1988684SScott Teel 	h = sdev_to_hba(sdev);
687c1988684SScott Teel 	spin_lock_irqsave(&h->lock, flags);
688c1988684SScott Teel 	hdev = sdev->hostdata;
689c1988684SScott Teel 	if (!hdev) {
690c1988684SScott Teel 		spin_unlock_irqrestore(&h->lock, flags);
691c1988684SScott Teel 		return -ENODEV;
692c1988684SScott Teel 	}
693c1988684SScott Teel 	offload_enabled = hdev->offload_enabled;
694c1988684SScott Teel 	spin_unlock_irqrestore(&h->lock, flags);
695c1988684SScott Teel 	return snprintf(buf, 20, "%d\n", offload_enabled);
696c1988684SScott Teel }
697c1988684SScott Teel 
6983f5eac3aSStephen M. Cameron static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
6993f5eac3aSStephen M. Cameron static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
7003f5eac3aSStephen M. Cameron static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
7013f5eac3aSStephen M. Cameron static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
702c1988684SScott Teel static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
703c1988684SScott Teel 			host_show_hp_ssd_smart_path_enabled, NULL);
704da0697bdSScott Teel static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
705da0697bdSScott Teel 		host_show_hp_ssd_smart_path_status,
706da0697bdSScott Teel 		host_store_hp_ssd_smart_path_status);
7072ba8bfc8SStephen M. Cameron static DEVICE_ATTR(raid_offload_debug, S_IWUSR, NULL,
7082ba8bfc8SStephen M. Cameron 			host_store_raid_offload_debug);
7093f5eac3aSStephen M. Cameron static DEVICE_ATTR(firmware_revision, S_IRUGO,
7103f5eac3aSStephen M. Cameron 	host_show_firmware_revision, NULL);
7113f5eac3aSStephen M. Cameron static DEVICE_ATTR(commands_outstanding, S_IRUGO,
7123f5eac3aSStephen M. Cameron 	host_show_commands_outstanding, NULL);
7133f5eac3aSStephen M. Cameron static DEVICE_ATTR(transport_mode, S_IRUGO,
7143f5eac3aSStephen M. Cameron 	host_show_transport_mode, NULL);
715941b1cdaSStephen M. Cameron static DEVICE_ATTR(resettable, S_IRUGO,
716941b1cdaSStephen M. Cameron 	host_show_resettable, NULL);
717e985c58fSStephen Cameron static DEVICE_ATTR(lockup_detected, S_IRUGO,
718e985c58fSStephen Cameron 	host_show_lockup_detected, NULL);
7193f5eac3aSStephen M. Cameron 
7203f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_sdev_attrs[] = {
7213f5eac3aSStephen M. Cameron 	&dev_attr_raid_level,
7223f5eac3aSStephen M. Cameron 	&dev_attr_lunid,
7233f5eac3aSStephen M. Cameron 	&dev_attr_unique_id,
724c1988684SScott Teel 	&dev_attr_hp_ssd_smart_path_enabled,
725e985c58fSStephen Cameron 	&dev_attr_lockup_detected,
7263f5eac3aSStephen M. Cameron 	NULL,
7273f5eac3aSStephen M. Cameron };
7283f5eac3aSStephen M. Cameron 
7293f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_shost_attrs[] = {
7303f5eac3aSStephen M. Cameron 	&dev_attr_rescan,
7313f5eac3aSStephen M. Cameron 	&dev_attr_firmware_revision,
7323f5eac3aSStephen M. Cameron 	&dev_attr_commands_outstanding,
7333f5eac3aSStephen M. Cameron 	&dev_attr_transport_mode,
734941b1cdaSStephen M. Cameron 	&dev_attr_resettable,
735da0697bdSScott Teel 	&dev_attr_hp_ssd_smart_path_status,
7362ba8bfc8SStephen M. Cameron 	&dev_attr_raid_offload_debug,
7373f5eac3aSStephen M. Cameron 	NULL,
7383f5eac3aSStephen M. Cameron };
7393f5eac3aSStephen M. Cameron 
74041ce4c35SStephen Cameron #define HPSA_NRESERVED_CMDS	(HPSA_CMDS_RESERVED_FOR_ABORTS + \
74141ce4c35SStephen Cameron 		HPSA_CMDS_RESERVED_FOR_DRIVER + HPSA_MAX_CONCURRENT_PASSTHRUS)
74241ce4c35SStephen Cameron 
7433f5eac3aSStephen M. Cameron static struct scsi_host_template hpsa_driver_template = {
7443f5eac3aSStephen M. Cameron 	.module			= THIS_MODULE,
745f79cfec6SStephen M. Cameron 	.name			= HPSA,
746f79cfec6SStephen M. Cameron 	.proc_name		= HPSA,
7473f5eac3aSStephen M. Cameron 	.queuecommand		= hpsa_scsi_queue_command,
7483f5eac3aSStephen M. Cameron 	.scan_start		= hpsa_scan_start,
7493f5eac3aSStephen M. Cameron 	.scan_finished		= hpsa_scan_finished,
7507c0a0229SDon Brace 	.change_queue_depth	= hpsa_change_queue_depth,
7513f5eac3aSStephen M. Cameron 	.this_id		= -1,
7523f5eac3aSStephen M. Cameron 	.use_clustering		= ENABLE_CLUSTERING,
75375167d2cSStephen M. Cameron 	.eh_abort_handler	= hpsa_eh_abort_handler,
7543f5eac3aSStephen M. Cameron 	.eh_device_reset_handler = hpsa_eh_device_reset_handler,
7553f5eac3aSStephen M. Cameron 	.ioctl			= hpsa_ioctl,
7563f5eac3aSStephen M. Cameron 	.slave_alloc		= hpsa_slave_alloc,
75741ce4c35SStephen Cameron 	.slave_configure	= hpsa_slave_configure,
7583f5eac3aSStephen M. Cameron 	.slave_destroy		= hpsa_slave_destroy,
7593f5eac3aSStephen M. Cameron #ifdef CONFIG_COMPAT
7603f5eac3aSStephen M. Cameron 	.compat_ioctl		= hpsa_compat_ioctl,
7613f5eac3aSStephen M. Cameron #endif
7623f5eac3aSStephen M. Cameron 	.sdev_attrs = hpsa_sdev_attrs,
7633f5eac3aSStephen M. Cameron 	.shost_attrs = hpsa_shost_attrs,
764c0d6a4d1SStephen M. Cameron 	.max_sectors = 8192,
76554b2b50cSMartin K. Petersen 	.no_write_same = 1,
7663f5eac3aSStephen M. Cameron };
7673f5eac3aSStephen M. Cameron 
768254f796bSMatt Gates static inline u32 next_command(struct ctlr_info *h, u8 q)
7693f5eac3aSStephen M. Cameron {
7703f5eac3aSStephen M. Cameron 	u32 a;
771072b0518SStephen M. Cameron 	struct reply_queue_buffer *rq = &h->reply_queue[q];
7723f5eac3aSStephen M. Cameron 
773e1f7de0cSMatt Gates 	if (h->transMethod & CFGTBL_Trans_io_accel1)
774e1f7de0cSMatt Gates 		return h->access.command_completed(h, q);
775e1f7de0cSMatt Gates 
7763f5eac3aSStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
777254f796bSMatt Gates 		return h->access.command_completed(h, q);
7783f5eac3aSStephen M. Cameron 
779254f796bSMatt Gates 	if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
780254f796bSMatt Gates 		a = rq->head[rq->current_entry];
781254f796bSMatt Gates 		rq->current_entry++;
7820cbf768eSStephen M. Cameron 		atomic_dec(&h->commands_outstanding);
7833f5eac3aSStephen M. Cameron 	} else {
7843f5eac3aSStephen M. Cameron 		a = FIFO_EMPTY;
7853f5eac3aSStephen M. Cameron 	}
7863f5eac3aSStephen M. Cameron 	/* Check for wraparound */
787254f796bSMatt Gates 	if (rq->current_entry == h->max_commands) {
788254f796bSMatt Gates 		rq->current_entry = 0;
789254f796bSMatt Gates 		rq->wraparound ^= 1;
7903f5eac3aSStephen M. Cameron 	}
7913f5eac3aSStephen M. Cameron 	return a;
7923f5eac3aSStephen M. Cameron }
7933f5eac3aSStephen M. Cameron 
794c349775eSScott Teel /*
795c349775eSScott Teel  * There are some special bits in the bus address of the
796c349775eSScott Teel  * command that we have to set for the controller to know
797c349775eSScott Teel  * how to process the command:
798c349775eSScott Teel  *
799c349775eSScott Teel  * Normal performant mode:
800c349775eSScott Teel  * bit 0: 1 means performant mode, 0 means simple mode.
801c349775eSScott Teel  * bits 1-3 = block fetch table entry
802c349775eSScott Teel  * bits 4-6 = command type (== 0)
803c349775eSScott Teel  *
804c349775eSScott Teel  * ioaccel1 mode:
805c349775eSScott Teel  * bit 0 = "performant mode" bit.
806c349775eSScott Teel  * bits 1-3 = block fetch table entry
807c349775eSScott Teel  * bits 4-6 = command type (== 110)
808c349775eSScott Teel  * (command type is needed because ioaccel1 mode
809c349775eSScott Teel  * commands are submitted through the same register as normal
810c349775eSScott Teel  * mode commands, so this is how the controller knows whether
811c349775eSScott Teel  * the command is normal mode or ioaccel1 mode.)
812c349775eSScott Teel  *
813c349775eSScott Teel  * ioaccel2 mode:
814c349775eSScott Teel  * bit 0 = "performant mode" bit.
815c349775eSScott Teel  * bits 1-4 = block fetch table entry (note extra bit)
816c349775eSScott Teel  * bits 4-6 = not needed, because ioaccel2 mode has
817c349775eSScott Teel  * a separate special register for submitting commands.
818c349775eSScott Teel  */
819c349775eSScott Teel 
82025163bd5SWebb Scales /*
82125163bd5SWebb Scales  * set_performant_mode: Modify the tag for cciss performant
8223f5eac3aSStephen M. Cameron  * set bit 0 for pull model, bits 3-1 for block fetch
8233f5eac3aSStephen M. Cameron  * register number
8243f5eac3aSStephen M. Cameron  */
82525163bd5SWebb Scales #define DEFAULT_REPLY_QUEUE (-1)
82625163bd5SWebb Scales static void set_performant_mode(struct ctlr_info *h, struct CommandList *c,
82725163bd5SWebb Scales 					int reply_queue)
8283f5eac3aSStephen M. Cameron {
829254f796bSMatt Gates 	if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
8303f5eac3aSStephen M. Cameron 		c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
83125163bd5SWebb Scales 		if (unlikely(!h->msix_vector))
83225163bd5SWebb Scales 			return;
83325163bd5SWebb Scales 		if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
834254f796bSMatt Gates 			c->Header.ReplyQueue =
835804a5cb5SJohn Kacur 				raw_smp_processor_id() % h->nreply_queues;
83625163bd5SWebb Scales 		else
83725163bd5SWebb Scales 			c->Header.ReplyQueue = reply_queue % h->nreply_queues;
838254f796bSMatt Gates 	}
8393f5eac3aSStephen M. Cameron }
8403f5eac3aSStephen M. Cameron 
841c349775eSScott Teel static void set_ioaccel1_performant_mode(struct ctlr_info *h,
84225163bd5SWebb Scales 						struct CommandList *c,
84325163bd5SWebb Scales 						int reply_queue)
844c349775eSScott Teel {
845c349775eSScott Teel 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
846c349775eSScott Teel 
84725163bd5SWebb Scales 	/*
84825163bd5SWebb Scales 	 * Tell the controller to post the reply to the queue for this
849c349775eSScott Teel 	 * processor.  This seems to give the best I/O throughput.
850c349775eSScott Teel 	 */
85125163bd5SWebb Scales 	if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
852c349775eSScott Teel 		cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
85325163bd5SWebb Scales 	else
85425163bd5SWebb Scales 		cp->ReplyQueue = reply_queue % h->nreply_queues;
85525163bd5SWebb Scales 	/*
85625163bd5SWebb Scales 	 * Set the bits in the address sent down to include:
857c349775eSScott Teel 	 *  - performant mode bit (bit 0)
858c349775eSScott Teel 	 *  - pull count (bits 1-3)
859c349775eSScott Teel 	 *  - command type (bits 4-6)
860c349775eSScott Teel 	 */
861c349775eSScott Teel 	c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
862c349775eSScott Teel 					IOACCEL1_BUSADDR_CMDTYPE;
863c349775eSScott Teel }
864c349775eSScott Teel 
865c349775eSScott Teel static void set_ioaccel2_performant_mode(struct ctlr_info *h,
86625163bd5SWebb Scales 						struct CommandList *c,
86725163bd5SWebb Scales 						int reply_queue)
868c349775eSScott Teel {
869c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
870c349775eSScott Teel 
87125163bd5SWebb Scales 	/*
87225163bd5SWebb Scales 	 * Tell the controller to post the reply to the queue for this
873c349775eSScott Teel 	 * processor.  This seems to give the best I/O throughput.
874c349775eSScott Teel 	 */
87525163bd5SWebb Scales 	if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
876c349775eSScott Teel 		cp->reply_queue = smp_processor_id() % h->nreply_queues;
87725163bd5SWebb Scales 	else
87825163bd5SWebb Scales 		cp->reply_queue = reply_queue % h->nreply_queues;
87925163bd5SWebb Scales 	/*
88025163bd5SWebb Scales 	 * Set the bits in the address sent down to include:
881c349775eSScott Teel 	 *  - performant mode bit not used in ioaccel mode 2
882c349775eSScott Teel 	 *  - pull count (bits 0-3)
883c349775eSScott Teel 	 *  - command type isn't needed for ioaccel2
884c349775eSScott Teel 	 */
885c349775eSScott Teel 	c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
886c349775eSScott Teel }
887c349775eSScott Teel 
888e85c5974SStephen M. Cameron static int is_firmware_flash_cmd(u8 *cdb)
889e85c5974SStephen M. Cameron {
890e85c5974SStephen M. Cameron 	return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
891e85c5974SStephen M. Cameron }
892e85c5974SStephen M. Cameron 
893e85c5974SStephen M. Cameron /*
894e85c5974SStephen M. Cameron  * During firmware flash, the heartbeat register may not update as frequently
895e85c5974SStephen M. Cameron  * as it should.  So we dial down lockup detection during firmware flash. and
896e85c5974SStephen M. Cameron  * dial it back up when firmware flash completes.
897e85c5974SStephen M. Cameron  */
898e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
899e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
900e85c5974SStephen M. Cameron static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
901e85c5974SStephen M. Cameron 		struct CommandList *c)
902e85c5974SStephen M. Cameron {
903e85c5974SStephen M. Cameron 	if (!is_firmware_flash_cmd(c->Request.CDB))
904e85c5974SStephen M. Cameron 		return;
905e85c5974SStephen M. Cameron 	atomic_inc(&h->firmware_flash_in_progress);
906e85c5974SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
907e85c5974SStephen M. Cameron }
908e85c5974SStephen M. Cameron 
909e85c5974SStephen M. Cameron static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
910e85c5974SStephen M. Cameron 		struct CommandList *c)
911e85c5974SStephen M. Cameron {
912e85c5974SStephen M. Cameron 	if (is_firmware_flash_cmd(c->Request.CDB) &&
913e85c5974SStephen M. Cameron 		atomic_dec_and_test(&h->firmware_flash_in_progress))
914e85c5974SStephen M. Cameron 		h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
915e85c5974SStephen M. Cameron }
916e85c5974SStephen M. Cameron 
91725163bd5SWebb Scales static void __enqueue_cmd_and_start_io(struct ctlr_info *h,
91825163bd5SWebb Scales 	struct CommandList *c, int reply_queue)
9193f5eac3aSStephen M. Cameron {
920c05e8866SStephen Cameron 	dial_down_lockup_detection_during_fw_flash(h, c);
921c05e8866SStephen Cameron 	atomic_inc(&h->commands_outstanding);
922c349775eSScott Teel 	switch (c->cmd_type) {
923c349775eSScott Teel 	case CMD_IOACCEL1:
92425163bd5SWebb Scales 		set_ioaccel1_performant_mode(h, c, reply_queue);
925c05e8866SStephen Cameron 		writel(c->busaddr, h->vaddr + SA5_REQUEST_PORT_OFFSET);
926c349775eSScott Teel 		break;
927c349775eSScott Teel 	case CMD_IOACCEL2:
92825163bd5SWebb Scales 		set_ioaccel2_performant_mode(h, c, reply_queue);
929c05e8866SStephen Cameron 		writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
930c349775eSScott Teel 		break;
931c349775eSScott Teel 	default:
93225163bd5SWebb Scales 		set_performant_mode(h, c, reply_queue);
933f2405db8SDon Brace 		h->access.submit_command(h, c);
9343f5eac3aSStephen M. Cameron 	}
935c05e8866SStephen Cameron }
9363f5eac3aSStephen M. Cameron 
93725163bd5SWebb Scales static void enqueue_cmd_and_start_io(struct ctlr_info *h,
93825163bd5SWebb Scales 					struct CommandList *c)
93925163bd5SWebb Scales {
94025163bd5SWebb Scales 	__enqueue_cmd_and_start_io(h, c, DEFAULT_REPLY_QUEUE);
94125163bd5SWebb Scales }
94225163bd5SWebb Scales 
9433f5eac3aSStephen M. Cameron static inline int is_hba_lunid(unsigned char scsi3addr[])
9443f5eac3aSStephen M. Cameron {
9453f5eac3aSStephen M. Cameron 	return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
9463f5eac3aSStephen M. Cameron }
9473f5eac3aSStephen M. Cameron 
9483f5eac3aSStephen M. Cameron static inline int is_scsi_rev_5(struct ctlr_info *h)
9493f5eac3aSStephen M. Cameron {
9503f5eac3aSStephen M. Cameron 	if (!h->hba_inquiry_data)
9513f5eac3aSStephen M. Cameron 		return 0;
9523f5eac3aSStephen M. Cameron 	if ((h->hba_inquiry_data[2] & 0x07) == 5)
9533f5eac3aSStephen M. Cameron 		return 1;
9543f5eac3aSStephen M. Cameron 	return 0;
9553f5eac3aSStephen M. Cameron }
9563f5eac3aSStephen M. Cameron 
957edd16368SStephen M. Cameron static int hpsa_find_target_lun(struct ctlr_info *h,
958edd16368SStephen M. Cameron 	unsigned char scsi3addr[], int bus, int *target, int *lun)
959edd16368SStephen M. Cameron {
960edd16368SStephen M. Cameron 	/* finds an unused bus, target, lun for a new physical device
961edd16368SStephen M. Cameron 	 * assumes h->devlock is held
962edd16368SStephen M. Cameron 	 */
963edd16368SStephen M. Cameron 	int i, found = 0;
964cfe5badcSScott Teel 	DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
965edd16368SStephen M. Cameron 
966263d9401SAkinobu Mita 	bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
967edd16368SStephen M. Cameron 
968edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
969edd16368SStephen M. Cameron 		if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
970263d9401SAkinobu Mita 			__set_bit(h->dev[i]->target, lun_taken);
971edd16368SStephen M. Cameron 	}
972edd16368SStephen M. Cameron 
973263d9401SAkinobu Mita 	i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
974263d9401SAkinobu Mita 	if (i < HPSA_MAX_DEVICES) {
975edd16368SStephen M. Cameron 		/* *bus = 1; */
976edd16368SStephen M. Cameron 		*target = i;
977edd16368SStephen M. Cameron 		*lun = 0;
978edd16368SStephen M. Cameron 		found = 1;
979edd16368SStephen M. Cameron 	}
980edd16368SStephen M. Cameron 	return !found;
981edd16368SStephen M. Cameron }
982edd16368SStephen M. Cameron 
9830d96ef5fSWebb Scales static inline void hpsa_show_dev_msg(const char *level, struct ctlr_info *h,
9840d96ef5fSWebb Scales 	struct hpsa_scsi_dev_t *dev, char *description)
9850d96ef5fSWebb Scales {
9860d96ef5fSWebb Scales 	dev_printk(level, &h->pdev->dev,
9870d96ef5fSWebb Scales 			"scsi %d:%d:%d:%d: %s %s %.8s %.16s RAID-%s SSDSmartPathCap%c En%c Exp=%d\n",
9880d96ef5fSWebb Scales 			h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
9890d96ef5fSWebb Scales 			description,
9900d96ef5fSWebb Scales 			scsi_device_type(dev->devtype),
9910d96ef5fSWebb Scales 			dev->vendor,
9920d96ef5fSWebb Scales 			dev->model,
9930d96ef5fSWebb Scales 			dev->raid_level > RAID_UNKNOWN ?
9940d96ef5fSWebb Scales 				"RAID-?" : raid_label[dev->raid_level],
9950d96ef5fSWebb Scales 			dev->offload_config ? '+' : '-',
9960d96ef5fSWebb Scales 			dev->offload_enabled ? '+' : '-',
9970d96ef5fSWebb Scales 			dev->expose_state);
9980d96ef5fSWebb Scales }
9990d96ef5fSWebb Scales 
1000edd16368SStephen M. Cameron /* Add an entry into h->dev[] array. */
1001edd16368SStephen M. Cameron static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
1002edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *device,
1003edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *added[], int *nadded)
1004edd16368SStephen M. Cameron {
1005edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
1006edd16368SStephen M. Cameron 	int n = h->ndevices;
1007edd16368SStephen M. Cameron 	int i;
1008edd16368SStephen M. Cameron 	unsigned char addr1[8], addr2[8];
1009edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1010edd16368SStephen M. Cameron 
1011cfe5badcSScott Teel 	if (n >= HPSA_MAX_DEVICES) {
1012edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "too many devices, some will be "
1013edd16368SStephen M. Cameron 			"inaccessible.\n");
1014edd16368SStephen M. Cameron 		return -1;
1015edd16368SStephen M. Cameron 	}
1016edd16368SStephen M. Cameron 
1017edd16368SStephen M. Cameron 	/* physical devices do not have lun or target assigned until now. */
1018edd16368SStephen M. Cameron 	if (device->lun != -1)
1019edd16368SStephen M. Cameron 		/* Logical device, lun is already assigned. */
1020edd16368SStephen M. Cameron 		goto lun_assigned;
1021edd16368SStephen M. Cameron 
1022edd16368SStephen M. Cameron 	/* If this device a non-zero lun of a multi-lun device
1023edd16368SStephen M. Cameron 	 * byte 4 of the 8-byte LUN addr will contain the logical
10242b08b3e9SDon Brace 	 * unit no, zero otherwise.
1025edd16368SStephen M. Cameron 	 */
1026edd16368SStephen M. Cameron 	if (device->scsi3addr[4] == 0) {
1027edd16368SStephen M. Cameron 		/* This is not a non-zero lun of a multi-lun device */
1028edd16368SStephen M. Cameron 		if (hpsa_find_target_lun(h, device->scsi3addr,
1029edd16368SStephen M. Cameron 			device->bus, &device->target, &device->lun) != 0)
1030edd16368SStephen M. Cameron 			return -1;
1031edd16368SStephen M. Cameron 		goto lun_assigned;
1032edd16368SStephen M. Cameron 	}
1033edd16368SStephen M. Cameron 
1034edd16368SStephen M. Cameron 	/* This is a non-zero lun of a multi-lun device.
1035edd16368SStephen M. Cameron 	 * Search through our list and find the device which
1036edd16368SStephen M. Cameron 	 * has the same 8 byte LUN address, excepting byte 4.
1037edd16368SStephen M. Cameron 	 * Assign the same bus and target for this new LUN.
1038edd16368SStephen M. Cameron 	 * Use the logical unit number from the firmware.
1039edd16368SStephen M. Cameron 	 */
1040edd16368SStephen M. Cameron 	memcpy(addr1, device->scsi3addr, 8);
1041edd16368SStephen M. Cameron 	addr1[4] = 0;
1042edd16368SStephen M. Cameron 	for (i = 0; i < n; i++) {
1043edd16368SStephen M. Cameron 		sd = h->dev[i];
1044edd16368SStephen M. Cameron 		memcpy(addr2, sd->scsi3addr, 8);
1045edd16368SStephen M. Cameron 		addr2[4] = 0;
1046edd16368SStephen M. Cameron 		/* differ only in byte 4? */
1047edd16368SStephen M. Cameron 		if (memcmp(addr1, addr2, 8) == 0) {
1048edd16368SStephen M. Cameron 			device->bus = sd->bus;
1049edd16368SStephen M. Cameron 			device->target = sd->target;
1050edd16368SStephen M. Cameron 			device->lun = device->scsi3addr[4];
1051edd16368SStephen M. Cameron 			break;
1052edd16368SStephen M. Cameron 		}
1053edd16368SStephen M. Cameron 	}
1054edd16368SStephen M. Cameron 	if (device->lun == -1) {
1055edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
1056edd16368SStephen M. Cameron 			" suspect firmware bug or unsupported hardware "
1057edd16368SStephen M. Cameron 			"configuration.\n");
1058edd16368SStephen M. Cameron 			return -1;
1059edd16368SStephen M. Cameron 	}
1060edd16368SStephen M. Cameron 
1061edd16368SStephen M. Cameron lun_assigned:
1062edd16368SStephen M. Cameron 
1063edd16368SStephen M. Cameron 	h->dev[n] = device;
1064edd16368SStephen M. Cameron 	h->ndevices++;
1065edd16368SStephen M. Cameron 	added[*nadded] = device;
1066edd16368SStephen M. Cameron 	(*nadded)++;
10670d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_INFO, h, device,
10680d96ef5fSWebb Scales 		device->expose_state & HPSA_SCSI_ADD ? "added" : "masked");
1069a473d86cSRobert Elliott 	device->offload_to_be_enabled = device->offload_enabled;
1070a473d86cSRobert Elliott 	device->offload_enabled = 0;
1071edd16368SStephen M. Cameron 	return 0;
1072edd16368SStephen M. Cameron }
1073edd16368SStephen M. Cameron 
1074bd9244f7SScott Teel /* Update an entry in h->dev[] array. */
1075bd9244f7SScott Teel static void hpsa_scsi_update_entry(struct ctlr_info *h, int hostno,
1076bd9244f7SScott Teel 	int entry, struct hpsa_scsi_dev_t *new_entry)
1077bd9244f7SScott Teel {
1078a473d86cSRobert Elliott 	int offload_enabled;
1079bd9244f7SScott Teel 	/* assumes h->devlock is held */
1080bd9244f7SScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
1081bd9244f7SScott Teel 
1082bd9244f7SScott Teel 	/* Raid level changed. */
1083bd9244f7SScott Teel 	h->dev[entry]->raid_level = new_entry->raid_level;
1084250fb125SStephen M. Cameron 
108503383736SDon Brace 	/* Raid offload parameters changed.  Careful about the ordering. */
108603383736SDon Brace 	if (new_entry->offload_config && new_entry->offload_enabled) {
108703383736SDon Brace 		/*
108803383736SDon Brace 		 * if drive is newly offload_enabled, we want to copy the
108903383736SDon Brace 		 * raid map data first.  If previously offload_enabled and
109003383736SDon Brace 		 * offload_config were set, raid map data had better be
109103383736SDon Brace 		 * the same as it was before.  if raid map data is changed
109203383736SDon Brace 		 * then it had better be the case that
109303383736SDon Brace 		 * h->dev[entry]->offload_enabled is currently 0.
109403383736SDon Brace 		 */
10959fb0de2dSStephen M. Cameron 		h->dev[entry]->raid_map = new_entry->raid_map;
109603383736SDon Brace 		h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
109703383736SDon Brace 	}
1098a3144e0bSJoe Handzik 	if (new_entry->hba_ioaccel_enabled) {
1099a3144e0bSJoe Handzik 		h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
1100a3144e0bSJoe Handzik 		wmb(); /* set ioaccel_handle *before* hba_ioaccel_enabled */
1101a3144e0bSJoe Handzik 	}
1102a3144e0bSJoe Handzik 	h->dev[entry]->hba_ioaccel_enabled = new_entry->hba_ioaccel_enabled;
110303383736SDon Brace 	h->dev[entry]->offload_config = new_entry->offload_config;
110403383736SDon Brace 	h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
110503383736SDon Brace 	h->dev[entry]->queue_depth = new_entry->queue_depth;
1106250fb125SStephen M. Cameron 
110741ce4c35SStephen Cameron 	/*
110841ce4c35SStephen Cameron 	 * We can turn off ioaccel offload now, but need to delay turning
110941ce4c35SStephen Cameron 	 * it on until we can update h->dev[entry]->phys_disk[], but we
111041ce4c35SStephen Cameron 	 * can't do that until all the devices are updated.
111141ce4c35SStephen Cameron 	 */
111241ce4c35SStephen Cameron 	h->dev[entry]->offload_to_be_enabled = new_entry->offload_enabled;
111341ce4c35SStephen Cameron 	if (!new_entry->offload_enabled)
111441ce4c35SStephen Cameron 		h->dev[entry]->offload_enabled = 0;
111541ce4c35SStephen Cameron 
1116a473d86cSRobert Elliott 	offload_enabled = h->dev[entry]->offload_enabled;
1117a473d86cSRobert Elliott 	h->dev[entry]->offload_enabled = h->dev[entry]->offload_to_be_enabled;
11180d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_INFO, h, h->dev[entry], "updated");
1119a473d86cSRobert Elliott 	h->dev[entry]->offload_enabled = offload_enabled;
1120bd9244f7SScott Teel }
1121bd9244f7SScott Teel 
11222a8ccf31SStephen M. Cameron /* Replace an entry from h->dev[] array. */
11232a8ccf31SStephen M. Cameron static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
11242a8ccf31SStephen M. Cameron 	int entry, struct hpsa_scsi_dev_t *new_entry,
11252a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *added[], int *nadded,
11262a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
11272a8ccf31SStephen M. Cameron {
11282a8ccf31SStephen M. Cameron 	/* assumes h->devlock is held */
1129cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
11302a8ccf31SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
11312a8ccf31SStephen M. Cameron 	(*nremoved)++;
113201350d05SStephen M. Cameron 
113301350d05SStephen M. Cameron 	/*
113401350d05SStephen M. Cameron 	 * New physical devices won't have target/lun assigned yet
113501350d05SStephen M. Cameron 	 * so we need to preserve the values in the slot we are replacing.
113601350d05SStephen M. Cameron 	 */
113701350d05SStephen M. Cameron 	if (new_entry->target == -1) {
113801350d05SStephen M. Cameron 		new_entry->target = h->dev[entry]->target;
113901350d05SStephen M. Cameron 		new_entry->lun = h->dev[entry]->lun;
114001350d05SStephen M. Cameron 	}
114101350d05SStephen M. Cameron 
11422a8ccf31SStephen M. Cameron 	h->dev[entry] = new_entry;
11432a8ccf31SStephen M. Cameron 	added[*nadded] = new_entry;
11442a8ccf31SStephen M. Cameron 	(*nadded)++;
11450d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_INFO, h, new_entry, "replaced");
1146a473d86cSRobert Elliott 	new_entry->offload_to_be_enabled = new_entry->offload_enabled;
1147a473d86cSRobert Elliott 	new_entry->offload_enabled = 0;
11482a8ccf31SStephen M. Cameron }
11492a8ccf31SStephen M. Cameron 
1150edd16368SStephen M. Cameron /* Remove an entry from h->dev[] array. */
1151edd16368SStephen M. Cameron static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
1152edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
1153edd16368SStephen M. Cameron {
1154edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
1155edd16368SStephen M. Cameron 	int i;
1156edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1157edd16368SStephen M. Cameron 
1158cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
1159edd16368SStephen M. Cameron 
1160edd16368SStephen M. Cameron 	sd = h->dev[entry];
1161edd16368SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
1162edd16368SStephen M. Cameron 	(*nremoved)++;
1163edd16368SStephen M. Cameron 
1164edd16368SStephen M. Cameron 	for (i = entry; i < h->ndevices-1; i++)
1165edd16368SStephen M. Cameron 		h->dev[i] = h->dev[i+1];
1166edd16368SStephen M. Cameron 	h->ndevices--;
11670d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_INFO, h, sd, "removed");
1168edd16368SStephen M. Cameron }
1169edd16368SStephen M. Cameron 
1170edd16368SStephen M. Cameron #define SCSI3ADDR_EQ(a, b) ( \
1171edd16368SStephen M. Cameron 	(a)[7] == (b)[7] && \
1172edd16368SStephen M. Cameron 	(a)[6] == (b)[6] && \
1173edd16368SStephen M. Cameron 	(a)[5] == (b)[5] && \
1174edd16368SStephen M. Cameron 	(a)[4] == (b)[4] && \
1175edd16368SStephen M. Cameron 	(a)[3] == (b)[3] && \
1176edd16368SStephen M. Cameron 	(a)[2] == (b)[2] && \
1177edd16368SStephen M. Cameron 	(a)[1] == (b)[1] && \
1178edd16368SStephen M. Cameron 	(a)[0] == (b)[0])
1179edd16368SStephen M. Cameron 
1180edd16368SStephen M. Cameron static void fixup_botched_add(struct ctlr_info *h,
1181edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *added)
1182edd16368SStephen M. Cameron {
1183edd16368SStephen M. Cameron 	/* called when scsi_add_device fails in order to re-adjust
1184edd16368SStephen M. Cameron 	 * h->dev[] to match the mid layer's view.
1185edd16368SStephen M. Cameron 	 */
1186edd16368SStephen M. Cameron 	unsigned long flags;
1187edd16368SStephen M. Cameron 	int i, j;
1188edd16368SStephen M. Cameron 
1189edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
1190edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1191edd16368SStephen M. Cameron 		if (h->dev[i] == added) {
1192edd16368SStephen M. Cameron 			for (j = i; j < h->ndevices-1; j++)
1193edd16368SStephen M. Cameron 				h->dev[j] = h->dev[j+1];
1194edd16368SStephen M. Cameron 			h->ndevices--;
1195edd16368SStephen M. Cameron 			break;
1196edd16368SStephen M. Cameron 		}
1197edd16368SStephen M. Cameron 	}
1198edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
1199edd16368SStephen M. Cameron 	kfree(added);
1200edd16368SStephen M. Cameron }
1201edd16368SStephen M. Cameron 
1202edd16368SStephen M. Cameron static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
1203edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev2)
1204edd16368SStephen M. Cameron {
1205edd16368SStephen M. Cameron 	/* we compare everything except lun and target as these
1206edd16368SStephen M. Cameron 	 * are not yet assigned.  Compare parts likely
1207edd16368SStephen M. Cameron 	 * to differ first
1208edd16368SStephen M. Cameron 	 */
1209edd16368SStephen M. Cameron 	if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
1210edd16368SStephen M. Cameron 		sizeof(dev1->scsi3addr)) != 0)
1211edd16368SStephen M. Cameron 		return 0;
1212edd16368SStephen M. Cameron 	if (memcmp(dev1->device_id, dev2->device_id,
1213edd16368SStephen M. Cameron 		sizeof(dev1->device_id)) != 0)
1214edd16368SStephen M. Cameron 		return 0;
1215edd16368SStephen M. Cameron 	if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
1216edd16368SStephen M. Cameron 		return 0;
1217edd16368SStephen M. Cameron 	if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
1218edd16368SStephen M. Cameron 		return 0;
1219edd16368SStephen M. Cameron 	if (dev1->devtype != dev2->devtype)
1220edd16368SStephen M. Cameron 		return 0;
1221edd16368SStephen M. Cameron 	if (dev1->bus != dev2->bus)
1222edd16368SStephen M. Cameron 		return 0;
1223edd16368SStephen M. Cameron 	return 1;
1224edd16368SStephen M. Cameron }
1225edd16368SStephen M. Cameron 
1226bd9244f7SScott Teel static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
1227bd9244f7SScott Teel 	struct hpsa_scsi_dev_t *dev2)
1228bd9244f7SScott Teel {
1229bd9244f7SScott Teel 	/* Device attributes that can change, but don't mean
1230bd9244f7SScott Teel 	 * that the device is a different device, nor that the OS
1231bd9244f7SScott Teel 	 * needs to be told anything about the change.
1232bd9244f7SScott Teel 	 */
1233bd9244f7SScott Teel 	if (dev1->raid_level != dev2->raid_level)
1234bd9244f7SScott Teel 		return 1;
1235250fb125SStephen M. Cameron 	if (dev1->offload_config != dev2->offload_config)
1236250fb125SStephen M. Cameron 		return 1;
1237250fb125SStephen M. Cameron 	if (dev1->offload_enabled != dev2->offload_enabled)
1238250fb125SStephen M. Cameron 		return 1;
123903383736SDon Brace 	if (dev1->queue_depth != dev2->queue_depth)
124003383736SDon Brace 		return 1;
1241bd9244f7SScott Teel 	return 0;
1242bd9244f7SScott Teel }
1243bd9244f7SScott Teel 
1244edd16368SStephen M. Cameron /* Find needle in haystack.  If exact match found, return DEVICE_SAME,
1245edd16368SStephen M. Cameron  * and return needle location in *index.  If scsi3addr matches, but not
1246edd16368SStephen M. Cameron  * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
1247bd9244f7SScott Teel  * location in *index.
1248bd9244f7SScott Teel  * In the case of a minor device attribute change, such as RAID level, just
1249bd9244f7SScott Teel  * return DEVICE_UPDATED, along with the updated device's location in index.
1250bd9244f7SScott Teel  * If needle not found, return DEVICE_NOT_FOUND.
1251edd16368SStephen M. Cameron  */
1252edd16368SStephen M. Cameron static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
1253edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *haystack[], int haystack_size,
1254edd16368SStephen M. Cameron 	int *index)
1255edd16368SStephen M. Cameron {
1256edd16368SStephen M. Cameron 	int i;
1257edd16368SStephen M. Cameron #define DEVICE_NOT_FOUND 0
1258edd16368SStephen M. Cameron #define DEVICE_CHANGED 1
1259edd16368SStephen M. Cameron #define DEVICE_SAME 2
1260bd9244f7SScott Teel #define DEVICE_UPDATED 3
1261edd16368SStephen M. Cameron 	for (i = 0; i < haystack_size; i++) {
126223231048SStephen M. Cameron 		if (haystack[i] == NULL) /* previously removed. */
126323231048SStephen M. Cameron 			continue;
1264edd16368SStephen M. Cameron 		if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
1265edd16368SStephen M. Cameron 			*index = i;
1266bd9244f7SScott Teel 			if (device_is_the_same(needle, haystack[i])) {
1267bd9244f7SScott Teel 				if (device_updated(needle, haystack[i]))
1268bd9244f7SScott Teel 					return DEVICE_UPDATED;
1269edd16368SStephen M. Cameron 				return DEVICE_SAME;
1270bd9244f7SScott Teel 			} else {
12719846590eSStephen M. Cameron 				/* Keep offline devices offline */
12729846590eSStephen M. Cameron 				if (needle->volume_offline)
12739846590eSStephen M. Cameron 					return DEVICE_NOT_FOUND;
1274edd16368SStephen M. Cameron 				return DEVICE_CHANGED;
1275edd16368SStephen M. Cameron 			}
1276edd16368SStephen M. Cameron 		}
1277bd9244f7SScott Teel 	}
1278edd16368SStephen M. Cameron 	*index = -1;
1279edd16368SStephen M. Cameron 	return DEVICE_NOT_FOUND;
1280edd16368SStephen M. Cameron }
1281edd16368SStephen M. Cameron 
12829846590eSStephen M. Cameron static void hpsa_monitor_offline_device(struct ctlr_info *h,
12839846590eSStephen M. Cameron 					unsigned char scsi3addr[])
12849846590eSStephen M. Cameron {
12859846590eSStephen M. Cameron 	struct offline_device_entry *device;
12869846590eSStephen M. Cameron 	unsigned long flags;
12879846590eSStephen M. Cameron 
12889846590eSStephen M. Cameron 	/* Check to see if device is already on the list */
12899846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
12909846590eSStephen M. Cameron 	list_for_each_entry(device, &h->offline_device_list, offline_list) {
12919846590eSStephen M. Cameron 		if (memcmp(device->scsi3addr, scsi3addr,
12929846590eSStephen M. Cameron 			sizeof(device->scsi3addr)) == 0) {
12939846590eSStephen M. Cameron 			spin_unlock_irqrestore(&h->offline_device_lock, flags);
12949846590eSStephen M. Cameron 			return;
12959846590eSStephen M. Cameron 		}
12969846590eSStephen M. Cameron 	}
12979846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
12989846590eSStephen M. Cameron 
12999846590eSStephen M. Cameron 	/* Device is not on the list, add it. */
13009846590eSStephen M. Cameron 	device = kmalloc(sizeof(*device), GFP_KERNEL);
13019846590eSStephen M. Cameron 	if (!device) {
13029846590eSStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in %s\n", __func__);
13039846590eSStephen M. Cameron 		return;
13049846590eSStephen M. Cameron 	}
13059846590eSStephen M. Cameron 	memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
13069846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
13079846590eSStephen M. Cameron 	list_add_tail(&device->offline_list, &h->offline_device_list);
13089846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
13099846590eSStephen M. Cameron }
13109846590eSStephen M. Cameron 
13119846590eSStephen M. Cameron /* Print a message explaining various offline volume states */
13129846590eSStephen M. Cameron static void hpsa_show_volume_status(struct ctlr_info *h,
13139846590eSStephen M. Cameron 	struct hpsa_scsi_dev_t *sd)
13149846590eSStephen M. Cameron {
13159846590eSStephen M. Cameron 	if (sd->volume_offline == HPSA_VPD_LV_STATUS_UNSUPPORTED)
13169846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13179846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume status is not available through vital product data pages.\n",
13189846590eSStephen M. Cameron 			h->scsi_host->host_no,
13199846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13209846590eSStephen M. Cameron 	switch (sd->volume_offline) {
13219846590eSStephen M. Cameron 	case HPSA_LV_OK:
13229846590eSStephen M. Cameron 		break;
13239846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ERASE:
13249846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13259846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing background erase process.\n",
13269846590eSStephen M. Cameron 			h->scsi_host->host_no,
13279846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13289846590eSStephen M. Cameron 		break;
13299846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_RPI:
13309846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13319846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing rapid parity initialization process.\n",
13329846590eSStephen M. Cameron 			h->scsi_host->host_no,
13339846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13349846590eSStephen M. Cameron 		break;
13359846590eSStephen M. Cameron 	case HPSA_LV_PENDING_RPI:
13369846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13379846590eSStephen M. Cameron 				"C%d:B%d:T%d:L%d Volume is queued for rapid parity initialization process.\n",
13389846590eSStephen M. Cameron 				h->scsi_host->host_no,
13399846590eSStephen M. Cameron 				sd->bus, sd->target, sd->lun);
13409846590eSStephen M. Cameron 		break;
13419846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_NO_KEY:
13429846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13439846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because key is not present.\n",
13449846590eSStephen M. Cameron 			h->scsi_host->host_no,
13459846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13469846590eSStephen M. Cameron 		break;
13479846590eSStephen M. Cameron 	case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
13489846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13499846590eSStephen 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",
13509846590eSStephen M. Cameron 			h->scsi_host->host_no,
13519846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13529846590eSStephen M. Cameron 		break;
13539846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION:
13549846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13559846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing encryption process.\n",
13569846590eSStephen M. Cameron 			h->scsi_host->host_no,
13579846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13589846590eSStephen M. Cameron 		break;
13599846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
13609846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13619846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing encryption re-keying process.\n",
13629846590eSStephen M. Cameron 			h->scsi_host->host_no,
13639846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13649846590eSStephen M. Cameron 		break;
13659846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
13669846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13679846590eSStephen 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",
13689846590eSStephen M. Cameron 			h->scsi_host->host_no,
13699846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13709846590eSStephen M. Cameron 		break;
13719846590eSStephen M. Cameron 	case HPSA_LV_PENDING_ENCRYPTION:
13729846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13739846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is pending migration to encrypted state, but process has not started.\n",
13749846590eSStephen M. Cameron 			h->scsi_host->host_no,
13759846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13769846590eSStephen M. Cameron 		break;
13779846590eSStephen M. Cameron 	case HPSA_LV_PENDING_ENCRYPTION_REKEYING:
13789846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
13799846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is encrypted and is pending encryption rekeying.\n",
13809846590eSStephen M. Cameron 			h->scsi_host->host_no,
13819846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
13829846590eSStephen M. Cameron 		break;
13839846590eSStephen M. Cameron 	}
13849846590eSStephen M. Cameron }
13859846590eSStephen M. Cameron 
138603383736SDon Brace /*
138703383736SDon Brace  * Figure the list of physical drive pointers for a logical drive with
138803383736SDon Brace  * raid offload configured.
138903383736SDon Brace  */
139003383736SDon Brace static void hpsa_figure_phys_disk_ptrs(struct ctlr_info *h,
139103383736SDon Brace 				struct hpsa_scsi_dev_t *dev[], int ndevices,
139203383736SDon Brace 				struct hpsa_scsi_dev_t *logical_drive)
139303383736SDon Brace {
139403383736SDon Brace 	struct raid_map_data *map = &logical_drive->raid_map;
139503383736SDon Brace 	struct raid_map_disk_data *dd = &map->data[0];
139603383736SDon Brace 	int i, j;
139703383736SDon Brace 	int total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
139803383736SDon Brace 				le16_to_cpu(map->metadata_disks_per_row);
139903383736SDon Brace 	int nraid_map_entries = le16_to_cpu(map->row_cnt) *
140003383736SDon Brace 				le16_to_cpu(map->layout_map_count) *
140103383736SDon Brace 				total_disks_per_row;
140203383736SDon Brace 	int nphys_disk = le16_to_cpu(map->layout_map_count) *
140303383736SDon Brace 				total_disks_per_row;
140403383736SDon Brace 	int qdepth;
140503383736SDon Brace 
140603383736SDon Brace 	if (nraid_map_entries > RAID_MAP_MAX_ENTRIES)
140703383736SDon Brace 		nraid_map_entries = RAID_MAP_MAX_ENTRIES;
140803383736SDon Brace 
140903383736SDon Brace 	qdepth = 0;
141003383736SDon Brace 	for (i = 0; i < nraid_map_entries; i++) {
141103383736SDon Brace 		logical_drive->phys_disk[i] = NULL;
141203383736SDon Brace 		if (!logical_drive->offload_config)
141303383736SDon Brace 			continue;
141403383736SDon Brace 		for (j = 0; j < ndevices; j++) {
141503383736SDon Brace 			if (dev[j]->devtype != TYPE_DISK)
141603383736SDon Brace 				continue;
141703383736SDon Brace 			if (is_logical_dev_addr_mode(dev[j]->scsi3addr))
141803383736SDon Brace 				continue;
141903383736SDon Brace 			if (dev[j]->ioaccel_handle != dd[i].ioaccel_handle)
142003383736SDon Brace 				continue;
142103383736SDon Brace 
142203383736SDon Brace 			logical_drive->phys_disk[i] = dev[j];
142303383736SDon Brace 			if (i < nphys_disk)
142403383736SDon Brace 				qdepth = min(h->nr_cmds, qdepth +
142503383736SDon Brace 				    logical_drive->phys_disk[i]->queue_depth);
142603383736SDon Brace 			break;
142703383736SDon Brace 		}
142803383736SDon Brace 
142903383736SDon Brace 		/*
143003383736SDon Brace 		 * This can happen if a physical drive is removed and
143103383736SDon Brace 		 * the logical drive is degraded.  In that case, the RAID
143203383736SDon Brace 		 * map data will refer to a physical disk which isn't actually
143303383736SDon Brace 		 * present.  And in that case offload_enabled should already
143403383736SDon Brace 		 * be 0, but we'll turn it off here just in case
143503383736SDon Brace 		 */
143603383736SDon Brace 		if (!logical_drive->phys_disk[i]) {
143703383736SDon Brace 			logical_drive->offload_enabled = 0;
143841ce4c35SStephen Cameron 			logical_drive->offload_to_be_enabled = 0;
143941ce4c35SStephen Cameron 			logical_drive->queue_depth = 8;
144003383736SDon Brace 		}
144103383736SDon Brace 	}
144203383736SDon Brace 	if (nraid_map_entries)
144303383736SDon Brace 		/*
144403383736SDon Brace 		 * This is correct for reads, too high for full stripe writes,
144503383736SDon Brace 		 * way too high for partial stripe writes
144603383736SDon Brace 		 */
144703383736SDon Brace 		logical_drive->queue_depth = qdepth;
144803383736SDon Brace 	else
144903383736SDon Brace 		logical_drive->queue_depth = h->nr_cmds;
145003383736SDon Brace }
145103383736SDon Brace 
145203383736SDon Brace static void hpsa_update_log_drive_phys_drive_ptrs(struct ctlr_info *h,
145303383736SDon Brace 				struct hpsa_scsi_dev_t *dev[], int ndevices)
145403383736SDon Brace {
145503383736SDon Brace 	int i;
145603383736SDon Brace 
145703383736SDon Brace 	for (i = 0; i < ndevices; i++) {
145803383736SDon Brace 		if (dev[i]->devtype != TYPE_DISK)
145903383736SDon Brace 			continue;
146003383736SDon Brace 		if (!is_logical_dev_addr_mode(dev[i]->scsi3addr))
146103383736SDon Brace 			continue;
146241ce4c35SStephen Cameron 
146341ce4c35SStephen Cameron 		/*
146441ce4c35SStephen Cameron 		 * If offload is currently enabled, the RAID map and
146541ce4c35SStephen Cameron 		 * phys_disk[] assignment *better* not be changing
146641ce4c35SStephen Cameron 		 * and since it isn't changing, we do not need to
146741ce4c35SStephen Cameron 		 * update it.
146841ce4c35SStephen Cameron 		 */
146941ce4c35SStephen Cameron 		if (dev[i]->offload_enabled)
147041ce4c35SStephen Cameron 			continue;
147141ce4c35SStephen Cameron 
147203383736SDon Brace 		hpsa_figure_phys_disk_ptrs(h, dev, ndevices, dev[i]);
147303383736SDon Brace 	}
147403383736SDon Brace }
147503383736SDon Brace 
14764967bd3eSStephen M. Cameron static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
1477edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd[], int nsds)
1478edd16368SStephen M. Cameron {
1479edd16368SStephen M. Cameron 	/* sd contains scsi3 addresses and devtypes, and inquiry
1480edd16368SStephen M. Cameron 	 * data.  This function takes what's in sd to be the current
1481edd16368SStephen M. Cameron 	 * reality and updates h->dev[] to reflect that reality.
1482edd16368SStephen M. Cameron 	 */
1483edd16368SStephen M. Cameron 	int i, entry, device_change, changes = 0;
1484edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *csd;
1485edd16368SStephen M. Cameron 	unsigned long flags;
1486edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **added, **removed;
1487edd16368SStephen M. Cameron 	int nadded, nremoved;
1488edd16368SStephen M. Cameron 	struct Scsi_Host *sh = NULL;
1489edd16368SStephen M. Cameron 
1490cfe5badcSScott Teel 	added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
1491cfe5badcSScott Teel 	removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
1492edd16368SStephen M. Cameron 
1493edd16368SStephen M. Cameron 	if (!added || !removed) {
1494edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in "
1495edd16368SStephen M. Cameron 			"adjust_hpsa_scsi_table\n");
1496edd16368SStephen M. Cameron 		goto free_and_out;
1497edd16368SStephen M. Cameron 	}
1498edd16368SStephen M. Cameron 
1499edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1500edd16368SStephen M. Cameron 
1501edd16368SStephen M. Cameron 	/* find any devices in h->dev[] that are not in
1502edd16368SStephen M. Cameron 	 * sd[] and remove them from h->dev[], and for any
1503edd16368SStephen M. Cameron 	 * devices which have changed, remove the old device
1504edd16368SStephen M. Cameron 	 * info and add the new device info.
1505bd9244f7SScott Teel 	 * If minor device attributes change, just update
1506bd9244f7SScott Teel 	 * the existing device structure.
1507edd16368SStephen M. Cameron 	 */
1508edd16368SStephen M. Cameron 	i = 0;
1509edd16368SStephen M. Cameron 	nremoved = 0;
1510edd16368SStephen M. Cameron 	nadded = 0;
1511edd16368SStephen M. Cameron 	while (i < h->ndevices) {
1512edd16368SStephen M. Cameron 		csd = h->dev[i];
1513edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
1514edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
1515edd16368SStephen M. Cameron 			changes++;
1516edd16368SStephen M. Cameron 			hpsa_scsi_remove_entry(h, hostno, i,
1517edd16368SStephen M. Cameron 				removed, &nremoved);
1518edd16368SStephen M. Cameron 			continue; /* remove ^^^, hence i not incremented */
1519edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
1520edd16368SStephen M. Cameron 			changes++;
15212a8ccf31SStephen M. Cameron 			hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
15222a8ccf31SStephen M. Cameron 				added, &nadded, removed, &nremoved);
1523c7f172dcSStephen M. Cameron 			/* Set it to NULL to prevent it from being freed
1524c7f172dcSStephen M. Cameron 			 * at the bottom of hpsa_update_scsi_devices()
1525c7f172dcSStephen M. Cameron 			 */
1526c7f172dcSStephen M. Cameron 			sd[entry] = NULL;
1527bd9244f7SScott Teel 		} else if (device_change == DEVICE_UPDATED) {
1528bd9244f7SScott Teel 			hpsa_scsi_update_entry(h, hostno, i, sd[entry]);
1529edd16368SStephen M. Cameron 		}
1530edd16368SStephen M. Cameron 		i++;
1531edd16368SStephen M. Cameron 	}
1532edd16368SStephen M. Cameron 
1533edd16368SStephen M. Cameron 	/* Now, make sure every device listed in sd[] is also
1534edd16368SStephen M. Cameron 	 * listed in h->dev[], adding them if they aren't found
1535edd16368SStephen M. Cameron 	 */
1536edd16368SStephen M. Cameron 
1537edd16368SStephen M. Cameron 	for (i = 0; i < nsds; i++) {
1538edd16368SStephen M. Cameron 		if (!sd[i]) /* if already added above. */
1539edd16368SStephen M. Cameron 			continue;
15409846590eSStephen M. Cameron 
15419846590eSStephen M. Cameron 		/* Don't add devices which are NOT READY, FORMAT IN PROGRESS
15429846590eSStephen M. Cameron 		 * as the SCSI mid-layer does not handle such devices well.
15439846590eSStephen M. Cameron 		 * It relentlessly loops sending TUR at 3Hz, then READ(10)
15449846590eSStephen M. Cameron 		 * at 160Hz, and prevents the system from coming up.
15459846590eSStephen M. Cameron 		 */
15469846590eSStephen M. Cameron 		if (sd[i]->volume_offline) {
15479846590eSStephen M. Cameron 			hpsa_show_volume_status(h, sd[i]);
15480d96ef5fSWebb Scales 			hpsa_show_dev_msg(KERN_INFO, h, sd[i], "offline");
15499846590eSStephen M. Cameron 			continue;
15509846590eSStephen M. Cameron 		}
15519846590eSStephen M. Cameron 
1552edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1553edd16368SStephen M. Cameron 					h->ndevices, &entry);
1554edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
1555edd16368SStephen M. Cameron 			changes++;
1556edd16368SStephen M. Cameron 			if (hpsa_scsi_add_entry(h, hostno, sd[i],
1557edd16368SStephen M. Cameron 				added, &nadded) != 0)
1558edd16368SStephen M. Cameron 				break;
1559edd16368SStephen M. Cameron 			sd[i] = NULL; /* prevent from being freed later. */
1560edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
1561edd16368SStephen M. Cameron 			/* should never happen... */
1562edd16368SStephen M. Cameron 			changes++;
1563edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev,
1564edd16368SStephen M. Cameron 				"device unexpectedly changed.\n");
1565edd16368SStephen M. Cameron 			/* but if it does happen, we just ignore that device */
1566edd16368SStephen M. Cameron 		}
1567edd16368SStephen M. Cameron 	}
156841ce4c35SStephen Cameron 	hpsa_update_log_drive_phys_drive_ptrs(h, h->dev, h->ndevices);
156941ce4c35SStephen Cameron 
157041ce4c35SStephen Cameron 	/* Now that h->dev[]->phys_disk[] is coherent, we can enable
157141ce4c35SStephen Cameron 	 * any logical drives that need it enabled.
157241ce4c35SStephen Cameron 	 */
157341ce4c35SStephen Cameron 	for (i = 0; i < h->ndevices; i++)
157441ce4c35SStephen Cameron 		h->dev[i]->offload_enabled = h->dev[i]->offload_to_be_enabled;
157541ce4c35SStephen Cameron 
1576edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1577edd16368SStephen M. Cameron 
15789846590eSStephen M. Cameron 	/* Monitor devices which are in one of several NOT READY states to be
15799846590eSStephen M. Cameron 	 * brought online later. This must be done without holding h->devlock,
15809846590eSStephen M. Cameron 	 * so don't touch h->dev[]
15819846590eSStephen M. Cameron 	 */
15829846590eSStephen M. Cameron 	for (i = 0; i < nsds; i++) {
15839846590eSStephen M. Cameron 		if (!sd[i]) /* if already added above. */
15849846590eSStephen M. Cameron 			continue;
15859846590eSStephen M. Cameron 		if (sd[i]->volume_offline)
15869846590eSStephen M. Cameron 			hpsa_monitor_offline_device(h, sd[i]->scsi3addr);
15879846590eSStephen M. Cameron 	}
15889846590eSStephen M. Cameron 
1589edd16368SStephen M. Cameron 	/* Don't notify scsi mid layer of any changes the first time through
1590edd16368SStephen M. Cameron 	 * (or if there are no changes) scsi_scan_host will do it later the
1591edd16368SStephen M. Cameron 	 * first time through.
1592edd16368SStephen M. Cameron 	 */
1593edd16368SStephen M. Cameron 	if (hostno == -1 || !changes)
1594edd16368SStephen M. Cameron 		goto free_and_out;
1595edd16368SStephen M. Cameron 
1596edd16368SStephen M. Cameron 	sh = h->scsi_host;
1597edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any removed devices */
1598edd16368SStephen M. Cameron 	for (i = 0; i < nremoved; i++) {
159941ce4c35SStephen Cameron 		if (removed[i]->expose_state & HPSA_SCSI_ADD) {
1600edd16368SStephen M. Cameron 			struct scsi_device *sdev =
1601edd16368SStephen M. Cameron 				scsi_device_lookup(sh, removed[i]->bus,
1602edd16368SStephen M. Cameron 					removed[i]->target, removed[i]->lun);
1603edd16368SStephen M. Cameron 			if (sdev != NULL) {
1604edd16368SStephen M. Cameron 				scsi_remove_device(sdev);
1605edd16368SStephen M. Cameron 				scsi_device_put(sdev);
1606edd16368SStephen M. Cameron 			} else {
160741ce4c35SStephen Cameron 				/*
160841ce4c35SStephen Cameron 				 * We don't expect to get here.
1609edd16368SStephen M. Cameron 				 * future cmds to this device will get selection
1610edd16368SStephen M. Cameron 				 * timeout as if the device was gone.
1611edd16368SStephen M. Cameron 				 */
16120d96ef5fSWebb Scales 				hpsa_show_dev_msg(KERN_WARNING, h, removed[i],
16130d96ef5fSWebb Scales 					"didn't find device for removal.");
1614edd16368SStephen M. Cameron 			}
161541ce4c35SStephen Cameron 		}
1616edd16368SStephen M. Cameron 		kfree(removed[i]);
1617edd16368SStephen M. Cameron 		removed[i] = NULL;
1618edd16368SStephen M. Cameron 	}
1619edd16368SStephen M. Cameron 
1620edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any added devices */
1621edd16368SStephen M. Cameron 	for (i = 0; i < nadded; i++) {
162241ce4c35SStephen Cameron 		if (!(added[i]->expose_state & HPSA_SCSI_ADD))
162341ce4c35SStephen Cameron 			continue;
1624edd16368SStephen M. Cameron 		if (scsi_add_device(sh, added[i]->bus,
1625edd16368SStephen M. Cameron 			added[i]->target, added[i]->lun) == 0)
1626edd16368SStephen M. Cameron 			continue;
16270d96ef5fSWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, added[i],
16280d96ef5fSWebb Scales 					"addition failed, device not added.");
1629edd16368SStephen M. Cameron 		/* now we have to remove it from h->dev,
1630edd16368SStephen M. Cameron 		 * since it didn't get added to scsi mid layer
1631edd16368SStephen M. Cameron 		 */
1632edd16368SStephen M. Cameron 		fixup_botched_add(h, added[i]);
1633edd16368SStephen M. Cameron 	}
1634edd16368SStephen M. Cameron 
1635edd16368SStephen M. Cameron free_and_out:
1636edd16368SStephen M. Cameron 	kfree(added);
1637edd16368SStephen M. Cameron 	kfree(removed);
1638edd16368SStephen M. Cameron }
1639edd16368SStephen M. Cameron 
1640edd16368SStephen M. Cameron /*
16419e03aa2fSJoe Perches  * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
1642edd16368SStephen M. Cameron  * Assume's h->devlock is held.
1643edd16368SStephen M. Cameron  */
1644edd16368SStephen M. Cameron static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1645edd16368SStephen M. Cameron 	int bus, int target, int lun)
1646edd16368SStephen M. Cameron {
1647edd16368SStephen M. Cameron 	int i;
1648edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1649edd16368SStephen M. Cameron 
1650edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1651edd16368SStephen M. Cameron 		sd = h->dev[i];
1652edd16368SStephen M. Cameron 		if (sd->bus == bus && sd->target == target && sd->lun == lun)
1653edd16368SStephen M. Cameron 			return sd;
1654edd16368SStephen M. Cameron 	}
1655edd16368SStephen M. Cameron 	return NULL;
1656edd16368SStephen M. Cameron }
1657edd16368SStephen M. Cameron 
1658edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev)
1659edd16368SStephen M. Cameron {
1660edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1661edd16368SStephen M. Cameron 	unsigned long flags;
1662edd16368SStephen M. Cameron 	struct ctlr_info *h;
1663edd16368SStephen M. Cameron 
1664edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
1665edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1666edd16368SStephen M. Cameron 	sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1667edd16368SStephen M. Cameron 		sdev_id(sdev), sdev->lun);
166841ce4c35SStephen Cameron 	if (likely(sd)) {
166903383736SDon Brace 		atomic_set(&sd->ioaccel_cmds_out, 0);
167041ce4c35SStephen Cameron 		sdev->hostdata = (sd->expose_state & HPSA_SCSI_ADD) ? sd : NULL;
167141ce4c35SStephen Cameron 	} else
167241ce4c35SStephen Cameron 		sdev->hostdata = NULL;
1673edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1674edd16368SStephen M. Cameron 	return 0;
1675edd16368SStephen M. Cameron }
1676edd16368SStephen M. Cameron 
167741ce4c35SStephen Cameron /* configure scsi device based on internal per-device structure */
167841ce4c35SStephen Cameron static int hpsa_slave_configure(struct scsi_device *sdev)
167941ce4c35SStephen Cameron {
168041ce4c35SStephen Cameron 	struct hpsa_scsi_dev_t *sd;
168141ce4c35SStephen Cameron 	int queue_depth;
168241ce4c35SStephen Cameron 
168341ce4c35SStephen Cameron 	sd = sdev->hostdata;
168441ce4c35SStephen Cameron 	sdev->no_uld_attach = !sd || !(sd->expose_state & HPSA_ULD_ATTACH);
168541ce4c35SStephen Cameron 
168641ce4c35SStephen Cameron 	if (sd)
168741ce4c35SStephen Cameron 		queue_depth = sd->queue_depth != 0 ?
168841ce4c35SStephen Cameron 			sd->queue_depth : sdev->host->can_queue;
168941ce4c35SStephen Cameron 	else
169041ce4c35SStephen Cameron 		queue_depth = sdev->host->can_queue;
169141ce4c35SStephen Cameron 
169241ce4c35SStephen Cameron 	scsi_change_queue_depth(sdev, queue_depth);
169341ce4c35SStephen Cameron 
169441ce4c35SStephen Cameron 	return 0;
169541ce4c35SStephen Cameron }
169641ce4c35SStephen Cameron 
1697edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev)
1698edd16368SStephen M. Cameron {
1699bcc44255SStephen M. Cameron 	/* nothing to do. */
1700edd16368SStephen M. Cameron }
1701edd16368SStephen M. Cameron 
1702d9a729f3SWebb Scales static void hpsa_free_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
1703d9a729f3SWebb Scales {
1704d9a729f3SWebb Scales 	int i;
1705d9a729f3SWebb Scales 
1706d9a729f3SWebb Scales 	if (!h->ioaccel2_cmd_sg_list)
1707d9a729f3SWebb Scales 		return;
1708d9a729f3SWebb Scales 	for (i = 0; i < h->nr_cmds; i++) {
1709d9a729f3SWebb Scales 		kfree(h->ioaccel2_cmd_sg_list[i]);
1710d9a729f3SWebb Scales 		h->ioaccel2_cmd_sg_list[i] = NULL;
1711d9a729f3SWebb Scales 	}
1712d9a729f3SWebb Scales 	kfree(h->ioaccel2_cmd_sg_list);
1713d9a729f3SWebb Scales 	h->ioaccel2_cmd_sg_list = NULL;
1714d9a729f3SWebb Scales }
1715d9a729f3SWebb Scales 
1716d9a729f3SWebb Scales static int hpsa_allocate_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
1717d9a729f3SWebb Scales {
1718d9a729f3SWebb Scales 	int i;
1719d9a729f3SWebb Scales 
1720d9a729f3SWebb Scales 	if (h->chainsize <= 0)
1721d9a729f3SWebb Scales 		return 0;
1722d9a729f3SWebb Scales 
1723d9a729f3SWebb Scales 	h->ioaccel2_cmd_sg_list =
1724d9a729f3SWebb Scales 		kzalloc(sizeof(*h->ioaccel2_cmd_sg_list) * h->nr_cmds,
1725d9a729f3SWebb Scales 					GFP_KERNEL);
1726d9a729f3SWebb Scales 	if (!h->ioaccel2_cmd_sg_list)
1727d9a729f3SWebb Scales 		return -ENOMEM;
1728d9a729f3SWebb Scales 	for (i = 0; i < h->nr_cmds; i++) {
1729d9a729f3SWebb Scales 		h->ioaccel2_cmd_sg_list[i] =
1730d9a729f3SWebb Scales 			kmalloc(sizeof(*h->ioaccel2_cmd_sg_list[i]) *
1731d9a729f3SWebb Scales 					h->maxsgentries, GFP_KERNEL);
1732d9a729f3SWebb Scales 		if (!h->ioaccel2_cmd_sg_list[i])
1733d9a729f3SWebb Scales 			goto clean;
1734d9a729f3SWebb Scales 	}
1735d9a729f3SWebb Scales 	return 0;
1736d9a729f3SWebb Scales 
1737d9a729f3SWebb Scales clean:
1738d9a729f3SWebb Scales 	hpsa_free_ioaccel2_sg_chain_blocks(h);
1739d9a729f3SWebb Scales 	return -ENOMEM;
1740d9a729f3SWebb Scales }
1741d9a729f3SWebb Scales 
174233a2ffceSStephen M. Cameron static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
174333a2ffceSStephen M. Cameron {
174433a2ffceSStephen M. Cameron 	int i;
174533a2ffceSStephen M. Cameron 
174633a2ffceSStephen M. Cameron 	if (!h->cmd_sg_list)
174733a2ffceSStephen M. Cameron 		return;
174833a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
174933a2ffceSStephen M. Cameron 		kfree(h->cmd_sg_list[i]);
175033a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = NULL;
175133a2ffceSStephen M. Cameron 	}
175233a2ffceSStephen M. Cameron 	kfree(h->cmd_sg_list);
175333a2ffceSStephen M. Cameron 	h->cmd_sg_list = NULL;
175433a2ffceSStephen M. Cameron }
175533a2ffceSStephen M. Cameron 
175633a2ffceSStephen M. Cameron static int hpsa_allocate_sg_chain_blocks(struct ctlr_info *h)
175733a2ffceSStephen M. Cameron {
175833a2ffceSStephen M. Cameron 	int i;
175933a2ffceSStephen M. Cameron 
176033a2ffceSStephen M. Cameron 	if (h->chainsize <= 0)
176133a2ffceSStephen M. Cameron 		return 0;
176233a2ffceSStephen M. Cameron 
176333a2ffceSStephen M. Cameron 	h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
176433a2ffceSStephen M. Cameron 				GFP_KERNEL);
17653d4e6af8SRobert Elliott 	if (!h->cmd_sg_list) {
17663d4e6af8SRobert Elliott 		dev_err(&h->pdev->dev, "Failed to allocate SG list\n");
176733a2ffceSStephen M. Cameron 		return -ENOMEM;
17683d4e6af8SRobert Elliott 	}
176933a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
177033a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
177133a2ffceSStephen M. Cameron 						h->chainsize, GFP_KERNEL);
17723d4e6af8SRobert Elliott 		if (!h->cmd_sg_list[i]) {
17733d4e6af8SRobert Elliott 			dev_err(&h->pdev->dev, "Failed to allocate cmd SG\n");
177433a2ffceSStephen M. Cameron 			goto clean;
177533a2ffceSStephen M. Cameron 		}
17763d4e6af8SRobert Elliott 	}
177733a2ffceSStephen M. Cameron 	return 0;
177833a2ffceSStephen M. Cameron 
177933a2ffceSStephen M. Cameron clean:
178033a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
178133a2ffceSStephen M. Cameron 	return -ENOMEM;
178233a2ffceSStephen M. Cameron }
178333a2ffceSStephen M. Cameron 
1784d9a729f3SWebb Scales static int hpsa_map_ioaccel2_sg_chain_block(struct ctlr_info *h,
1785d9a729f3SWebb Scales 	struct io_accel2_cmd *cp, struct CommandList *c)
1786d9a729f3SWebb Scales {
1787d9a729f3SWebb Scales 	struct ioaccel2_sg_element *chain_block;
1788d9a729f3SWebb Scales 	u64 temp64;
1789d9a729f3SWebb Scales 	u32 chain_size;
1790d9a729f3SWebb Scales 
1791d9a729f3SWebb Scales 	chain_block = h->ioaccel2_cmd_sg_list[c->cmdindex];
1792d9a729f3SWebb Scales 	chain_size = le32_to_cpu(cp->data_len);
1793d9a729f3SWebb Scales 	temp64 = pci_map_single(h->pdev, chain_block, chain_size,
1794d9a729f3SWebb Scales 				PCI_DMA_TODEVICE);
1795d9a729f3SWebb Scales 	if (dma_mapping_error(&h->pdev->dev, temp64)) {
1796d9a729f3SWebb Scales 		/* prevent subsequent unmapping */
1797d9a729f3SWebb Scales 		cp->sg->address = 0;
1798d9a729f3SWebb Scales 		return -1;
1799d9a729f3SWebb Scales 	}
1800d9a729f3SWebb Scales 	cp->sg->address = cpu_to_le64(temp64);
1801d9a729f3SWebb Scales 	return 0;
1802d9a729f3SWebb Scales }
1803d9a729f3SWebb Scales 
1804d9a729f3SWebb Scales static void hpsa_unmap_ioaccel2_sg_chain_block(struct ctlr_info *h,
1805d9a729f3SWebb Scales 	struct io_accel2_cmd *cp)
1806d9a729f3SWebb Scales {
1807d9a729f3SWebb Scales 	struct ioaccel2_sg_element *chain_sg;
1808d9a729f3SWebb Scales 	u64 temp64;
1809d9a729f3SWebb Scales 	u32 chain_size;
1810d9a729f3SWebb Scales 
1811d9a729f3SWebb Scales 	chain_sg = cp->sg;
1812d9a729f3SWebb Scales 	temp64 = le64_to_cpu(chain_sg->address);
1813d9a729f3SWebb Scales 	chain_size = le32_to_cpu(cp->data_len);
1814d9a729f3SWebb Scales 	pci_unmap_single(h->pdev, temp64, chain_size, PCI_DMA_TODEVICE);
1815d9a729f3SWebb Scales }
1816d9a729f3SWebb Scales 
1817e2bea6dfSStephen M. Cameron static int hpsa_map_sg_chain_block(struct ctlr_info *h,
181833a2ffceSStephen M. Cameron 	struct CommandList *c)
181933a2ffceSStephen M. Cameron {
182033a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg, *chain_block;
182133a2ffceSStephen M. Cameron 	u64 temp64;
182250a0decfSStephen M. Cameron 	u32 chain_len;
182333a2ffceSStephen M. Cameron 
182433a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
182533a2ffceSStephen M. Cameron 	chain_block = h->cmd_sg_list[c->cmdindex];
182650a0decfSStephen M. Cameron 	chain_sg->Ext = cpu_to_le32(HPSA_SG_CHAIN);
182750a0decfSStephen M. Cameron 	chain_len = sizeof(*chain_sg) *
18282b08b3e9SDon Brace 		(le16_to_cpu(c->Header.SGTotal) - h->max_cmd_sg_entries);
182950a0decfSStephen M. Cameron 	chain_sg->Len = cpu_to_le32(chain_len);
183050a0decfSStephen M. Cameron 	temp64 = pci_map_single(h->pdev, chain_block, chain_len,
183133a2ffceSStephen M. Cameron 				PCI_DMA_TODEVICE);
1832e2bea6dfSStephen M. Cameron 	if (dma_mapping_error(&h->pdev->dev, temp64)) {
1833e2bea6dfSStephen M. Cameron 		/* prevent subsequent unmapping */
183450a0decfSStephen M. Cameron 		chain_sg->Addr = cpu_to_le64(0);
1835e2bea6dfSStephen M. Cameron 		return -1;
1836e2bea6dfSStephen M. Cameron 	}
183750a0decfSStephen M. Cameron 	chain_sg->Addr = cpu_to_le64(temp64);
1838e2bea6dfSStephen M. Cameron 	return 0;
183933a2ffceSStephen M. Cameron }
184033a2ffceSStephen M. Cameron 
184133a2ffceSStephen M. Cameron static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
184233a2ffceSStephen M. Cameron 	struct CommandList *c)
184333a2ffceSStephen M. Cameron {
184433a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg;
184533a2ffceSStephen M. Cameron 
184650a0decfSStephen M. Cameron 	if (le16_to_cpu(c->Header.SGTotal) <= h->max_cmd_sg_entries)
184733a2ffceSStephen M. Cameron 		return;
184833a2ffceSStephen M. Cameron 
184933a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
185050a0decfSStephen M. Cameron 	pci_unmap_single(h->pdev, le64_to_cpu(chain_sg->Addr),
185150a0decfSStephen M. Cameron 			le32_to_cpu(chain_sg->Len), PCI_DMA_TODEVICE);
185233a2ffceSStephen M. Cameron }
185333a2ffceSStephen M. Cameron 
1854a09c1441SScott Teel 
1855a09c1441SScott Teel /* Decode the various types of errors on ioaccel2 path.
1856a09c1441SScott Teel  * Return 1 for any error that should generate a RAID path retry.
1857a09c1441SScott Teel  * Return 0 for errors that don't require a RAID path retry.
1858a09c1441SScott Teel  */
1859a09c1441SScott Teel static int handle_ioaccel_mode2_error(struct ctlr_info *h,
1860c349775eSScott Teel 					struct CommandList *c,
1861c349775eSScott Teel 					struct scsi_cmnd *cmd,
1862c349775eSScott Teel 					struct io_accel2_cmd *c2)
1863c349775eSScott Teel {
1864c349775eSScott Teel 	int data_len;
1865a09c1441SScott Teel 	int retry = 0;
1866c40820d5SJoe Handzik 	u32 ioaccel2_resid = 0;
1867c349775eSScott Teel 
1868c349775eSScott Teel 	switch (c2->error_data.serv_response) {
1869c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_COMPLETE:
1870c349775eSScott Teel 		switch (c2->error_data.status) {
1871c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
1872c349775eSScott Teel 			break;
1873c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
1874c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1875c349775eSScott Teel 				"%s: task complete with check condition.\n",
1876c349775eSScott Teel 				"HP SSD Smart Path");
1877ee6b1889SStephen M. Cameron 			cmd->result |= SAM_STAT_CHECK_CONDITION;
1878c349775eSScott Teel 			if (c2->error_data.data_present !=
1879ee6b1889SStephen M. Cameron 					IOACCEL2_SENSE_DATA_PRESENT) {
1880ee6b1889SStephen M. Cameron 				memset(cmd->sense_buffer, 0,
1881ee6b1889SStephen M. Cameron 					SCSI_SENSE_BUFFERSIZE);
1882c349775eSScott Teel 				break;
1883ee6b1889SStephen M. Cameron 			}
1884c349775eSScott Teel 			/* copy the sense data */
1885c349775eSScott Teel 			data_len = c2->error_data.sense_data_len;
1886c349775eSScott Teel 			if (data_len > SCSI_SENSE_BUFFERSIZE)
1887c349775eSScott Teel 				data_len = SCSI_SENSE_BUFFERSIZE;
1888c349775eSScott Teel 			if (data_len > sizeof(c2->error_data.sense_data_buff))
1889c349775eSScott Teel 				data_len =
1890c349775eSScott Teel 					sizeof(c2->error_data.sense_data_buff);
1891c349775eSScott Teel 			memcpy(cmd->sense_buffer,
1892c349775eSScott Teel 				c2->error_data.sense_data_buff, data_len);
1893a09c1441SScott Teel 			retry = 1;
1894c349775eSScott Teel 			break;
1895c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
1896c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1897c349775eSScott Teel 				"%s: task complete with BUSY status.\n",
1898c349775eSScott Teel 				"HP SSD Smart Path");
1899a09c1441SScott Teel 			retry = 1;
1900c349775eSScott Teel 			break;
1901c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
1902c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1903c349775eSScott Teel 				"%s: task complete with reservation conflict.\n",
1904c349775eSScott Teel 				"HP SSD Smart Path");
1905a09c1441SScott Teel 			retry = 1;
1906c349775eSScott Teel 			break;
1907c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
19084a8da22bSStephen Cameron 			retry = 1;
1909c349775eSScott Teel 			break;
1910c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
1911c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1912c349775eSScott Teel 				"%s: task complete with aborted status.\n",
1913c349775eSScott Teel 				"HP SSD Smart Path");
1914a09c1441SScott Teel 			retry = 1;
1915c349775eSScott Teel 			break;
1916c349775eSScott Teel 		default:
1917c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1918c349775eSScott Teel 				"%s: task complete with unrecognized status: 0x%02x\n",
1919c349775eSScott Teel 				"HP SSD Smart Path", c2->error_data.status);
1920a09c1441SScott Teel 			retry = 1;
1921c349775eSScott Teel 			break;
1922c349775eSScott Teel 		}
1923c349775eSScott Teel 		break;
1924c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_FAILURE:
1925c40820d5SJoe Handzik 		switch (c2->error_data.status) {
1926c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_IO_ERROR:
1927c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_IO_ABORTED:
1928c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_OVERRUN:
1929c40820d5SJoe Handzik 			retry = 1;
1930c40820d5SJoe Handzik 			break;
1931c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_UNDERRUN:
1932c40820d5SJoe Handzik 			cmd->result = (DID_OK << 16);		/* host byte */
1933c40820d5SJoe Handzik 			cmd->result |= (COMMAND_COMPLETE << 8);	/* msg byte */
1934c40820d5SJoe Handzik 			ioaccel2_resid = get_unaligned_le32(
1935c40820d5SJoe Handzik 						&c2->error_data.resid_cnt[0]);
1936c40820d5SJoe Handzik 			scsi_set_resid(cmd, ioaccel2_resid);
1937c40820d5SJoe Handzik 			break;
1938c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_NO_PATH_TO_DEVICE:
1939c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_INVALID_DEVICE:
1940c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_IOACCEL_DISABLED:
1941c40820d5SJoe Handzik 			/* We will get an event from ctlr to trigger rescan */
1942c40820d5SJoe Handzik 			retry = 1;
1943c40820d5SJoe Handzik 			break;
1944c40820d5SJoe Handzik 		default:
1945c40820d5SJoe Handzik 			retry = 1;
1946c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1947c349775eSScott Teel 				"unexpected delivery or target failure, status = 0x%02x\n",
1948c349775eSScott Teel 				c2->error_data.status);
1949c40820d5SJoe Handzik 		}
1950c349775eSScott Teel 		break;
1951c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
1952c349775eSScott Teel 		break;
1953c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
1954c349775eSScott Teel 		break;
1955c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
1956c349775eSScott Teel 		dev_warn(&h->pdev->dev, "task management function rejected.\n");
1957a09c1441SScott Teel 		retry = 1;
1958c349775eSScott Teel 		break;
1959c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
1960c349775eSScott Teel 		dev_warn(&h->pdev->dev, "task management function invalid LUN\n");
1961c349775eSScott Teel 		break;
1962c349775eSScott Teel 	default:
1963c349775eSScott Teel 		dev_warn(&h->pdev->dev,
1964c349775eSScott Teel 			"%s: Unrecognized server response: 0x%02x\n",
1965a09c1441SScott Teel 			"HP SSD Smart Path",
1966a09c1441SScott Teel 			c2->error_data.serv_response);
1967a09c1441SScott Teel 		retry = 1;
1968c349775eSScott Teel 		break;
1969c349775eSScott Teel 	}
1970a09c1441SScott Teel 
1971a09c1441SScott Teel 	return retry;	/* retry on raid path? */
1972c349775eSScott Teel }
1973c349775eSScott Teel 
1974c349775eSScott Teel static void process_ioaccel2_completion(struct ctlr_info *h,
1975c349775eSScott Teel 		struct CommandList *c, struct scsi_cmnd *cmd,
1976c349775eSScott Teel 		struct hpsa_scsi_dev_t *dev)
1977c349775eSScott Teel {
1978c349775eSScott Teel 	struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
1979c349775eSScott Teel 
1980c349775eSScott Teel 	/* check for good status */
1981c349775eSScott Teel 	if (likely(c2->error_data.serv_response == 0 &&
1982c349775eSScott Teel 			c2->error_data.status == 0)) {
1983c349775eSScott Teel 		cmd_free(h, c);
1984c349775eSScott Teel 		cmd->scsi_done(cmd);
1985c349775eSScott Teel 		return;
1986c349775eSScott Teel 	}
1987c349775eSScott Teel 
1988c349775eSScott Teel 	/* Any RAID offload error results in retry which will use
1989c349775eSScott Teel 	 * the normal I/O path so the controller can handle whatever's
1990c349775eSScott Teel 	 * wrong.
1991c349775eSScott Teel 	 */
1992c349775eSScott Teel 	if (is_logical_dev_addr_mode(dev->scsi3addr) &&
1993c349775eSScott Teel 		c2->error_data.serv_response ==
1994c349775eSScott Teel 			IOACCEL2_SERV_RESPONSE_FAILURE) {
1995080ef1ccSDon Brace 		if (c2->error_data.status ==
1996080ef1ccSDon Brace 			IOACCEL2_STATUS_SR_IOACCEL_DISABLED)
1997c349775eSScott Teel 			dev->offload_enabled = 0;
1998080ef1ccSDon Brace 		goto retry_cmd;
1999080ef1ccSDon Brace 	}
2000080ef1ccSDon Brace 
2001080ef1ccSDon Brace 	if (handle_ioaccel_mode2_error(h, c, cmd, c2))
2002080ef1ccSDon Brace 		goto retry_cmd;
2003080ef1ccSDon Brace 
2004c349775eSScott Teel 	cmd_free(h, c);
2005c349775eSScott Teel 	cmd->scsi_done(cmd);
2006c349775eSScott Teel 	return;
2007080ef1ccSDon Brace 
2008080ef1ccSDon Brace retry_cmd:
2009080ef1ccSDon Brace 	INIT_WORK(&c->work, hpsa_command_resubmit_worker);
2010080ef1ccSDon Brace 	queue_work_on(raw_smp_processor_id(), h->resubmit_wq, &c->work);
2011c349775eSScott Teel }
2012c349775eSScott Teel 
20139437ac43SStephen Cameron /* Returns 0 on success, < 0 otherwise. */
20149437ac43SStephen Cameron static int hpsa_evaluate_tmf_status(struct ctlr_info *h,
20159437ac43SStephen Cameron 					struct CommandList *cp)
20169437ac43SStephen Cameron {
20179437ac43SStephen Cameron 	u8 tmf_status = cp->err_info->ScsiStatus;
20189437ac43SStephen Cameron 
20199437ac43SStephen Cameron 	switch (tmf_status) {
20209437ac43SStephen Cameron 	case CISS_TMF_COMPLETE:
20219437ac43SStephen Cameron 		/*
20229437ac43SStephen Cameron 		 * CISS_TMF_COMPLETE never happens, instead,
20239437ac43SStephen Cameron 		 * ei->CommandStatus == 0 for this case.
20249437ac43SStephen Cameron 		 */
20259437ac43SStephen Cameron 	case CISS_TMF_SUCCESS:
20269437ac43SStephen Cameron 		return 0;
20279437ac43SStephen Cameron 	case CISS_TMF_INVALID_FRAME:
20289437ac43SStephen Cameron 	case CISS_TMF_NOT_SUPPORTED:
20299437ac43SStephen Cameron 	case CISS_TMF_FAILED:
20309437ac43SStephen Cameron 	case CISS_TMF_WRONG_LUN:
20319437ac43SStephen Cameron 	case CISS_TMF_OVERLAPPED_TAG:
20329437ac43SStephen Cameron 		break;
20339437ac43SStephen Cameron 	default:
20349437ac43SStephen Cameron 		dev_warn(&h->pdev->dev, "Unknown TMF status: 0x%02x\n",
20359437ac43SStephen Cameron 				tmf_status);
20369437ac43SStephen Cameron 		break;
20379437ac43SStephen Cameron 	}
20389437ac43SStephen Cameron 	return -tmf_status;
20399437ac43SStephen Cameron }
20409437ac43SStephen Cameron 
20411fb011fbSStephen M. Cameron static void complete_scsi_command(struct CommandList *cp)
2042edd16368SStephen M. Cameron {
2043edd16368SStephen M. Cameron 	struct scsi_cmnd *cmd;
2044edd16368SStephen M. Cameron 	struct ctlr_info *h;
2045edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2046283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
2047d9a729f3SWebb Scales 	struct io_accel2_cmd *c2;
2048edd16368SStephen M. Cameron 
20499437ac43SStephen Cameron 	u8 sense_key;
20509437ac43SStephen Cameron 	u8 asc;      /* additional sense code */
20519437ac43SStephen Cameron 	u8 ascq;     /* additional sense code qualifier */
2052db111e18SStephen M. Cameron 	unsigned long sense_data_size;
2053edd16368SStephen M. Cameron 
2054edd16368SStephen M. Cameron 	ei = cp->err_info;
20557fa3030cSStephen Cameron 	cmd = cp->scsi_cmd;
2056edd16368SStephen M. Cameron 	h = cp->h;
2057283b4a9bSStephen M. Cameron 	dev = cmd->device->hostdata;
2058d9a729f3SWebb Scales 	c2 = &h->ioaccel2_cmd_pool[cp->cmdindex];
2059edd16368SStephen M. Cameron 
2060edd16368SStephen M. Cameron 	scsi_dma_unmap(cmd); /* undo the DMA mappings */
2061e1f7de0cSMatt Gates 	if ((cp->cmd_type == CMD_SCSI) &&
20622b08b3e9SDon Brace 		(le16_to_cpu(cp->Header.SGTotal) > h->max_cmd_sg_entries))
206333a2ffceSStephen M. Cameron 		hpsa_unmap_sg_chain_block(h, cp);
2064edd16368SStephen M. Cameron 
2065d9a729f3SWebb Scales 	if ((cp->cmd_type == CMD_IOACCEL2) &&
2066d9a729f3SWebb Scales 		(c2->sg[0].chain_indicator == IOACCEL2_CHAIN))
2067d9a729f3SWebb Scales 		hpsa_unmap_ioaccel2_sg_chain_block(h, c2);
2068d9a729f3SWebb Scales 
2069edd16368SStephen M. Cameron 	cmd->result = (DID_OK << 16); 		/* host byte */
2070edd16368SStephen M. Cameron 	cmd->result |= (COMMAND_COMPLETE << 8);	/* msg byte */
2071c349775eSScott Teel 
207203383736SDon Brace 	if (cp->cmd_type == CMD_IOACCEL2 || cp->cmd_type == CMD_IOACCEL1)
207303383736SDon Brace 		atomic_dec(&cp->phys_disk->ioaccel_cmds_out);
207403383736SDon Brace 
207525163bd5SWebb Scales 	/*
207625163bd5SWebb Scales 	 * We check for lockup status here as it may be set for
207725163bd5SWebb Scales 	 * CMD_SCSI, CMD_IOACCEL1 and CMD_IOACCEL2 commands by
207825163bd5SWebb Scales 	 * fail_all_oustanding_cmds()
207925163bd5SWebb Scales 	 */
208025163bd5SWebb Scales 	if (unlikely(ei->CommandStatus == CMD_CTLR_LOCKUP)) {
208125163bd5SWebb Scales 		/* DID_NO_CONNECT will prevent a retry */
208225163bd5SWebb Scales 		cmd->result = DID_NO_CONNECT << 16;
208325163bd5SWebb Scales 		cmd_free(h, cp);
208425163bd5SWebb Scales 		cmd->scsi_done(cmd);
208525163bd5SWebb Scales 		return;
208625163bd5SWebb Scales 	}
208725163bd5SWebb Scales 
2088c349775eSScott Teel 	if (cp->cmd_type == CMD_IOACCEL2)
2089c349775eSScott Teel 		return process_ioaccel2_completion(h, cp, cmd, dev);
2090c349775eSScott Teel 
20916aa4c361SRobert Elliott 	scsi_set_resid(cmd, ei->ResidualCnt);
20926aa4c361SRobert Elliott 	if (ei->CommandStatus == 0) {
209303383736SDon Brace 		if (cp->cmd_type == CMD_IOACCEL1)
209403383736SDon Brace 			atomic_dec(&cp->phys_disk->ioaccel_cmds_out);
20956aa4c361SRobert Elliott 		cmd_free(h, cp);
20966aa4c361SRobert Elliott 		cmd->scsi_done(cmd);
20976aa4c361SRobert Elliott 		return;
20986aa4c361SRobert Elliott 	}
20996aa4c361SRobert Elliott 
2100e1f7de0cSMatt Gates 	/* For I/O accelerator commands, copy over some fields to the normal
2101e1f7de0cSMatt Gates 	 * CISS header used below for error handling.
2102e1f7de0cSMatt Gates 	 */
2103e1f7de0cSMatt Gates 	if (cp->cmd_type == CMD_IOACCEL1) {
2104e1f7de0cSMatt Gates 		struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
21052b08b3e9SDon Brace 		cp->Header.SGList = scsi_sg_count(cmd);
21062b08b3e9SDon Brace 		cp->Header.SGTotal = cpu_to_le16(cp->Header.SGList);
21072b08b3e9SDon Brace 		cp->Request.CDBLen = le16_to_cpu(c->io_flags) &
21082b08b3e9SDon Brace 			IOACCEL1_IOFLAGS_CDBLEN_MASK;
210950a0decfSStephen M. Cameron 		cp->Header.tag = c->tag;
2110e1f7de0cSMatt Gates 		memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
2111e1f7de0cSMatt Gates 		memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
2112283b4a9bSStephen M. Cameron 
2113283b4a9bSStephen M. Cameron 		/* Any RAID offload error results in retry which will use
2114283b4a9bSStephen M. Cameron 		 * the normal I/O path so the controller can handle whatever's
2115283b4a9bSStephen M. Cameron 		 * wrong.
2116283b4a9bSStephen M. Cameron 		 */
2117283b4a9bSStephen M. Cameron 		if (is_logical_dev_addr_mode(dev->scsi3addr)) {
2118283b4a9bSStephen M. Cameron 			if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
2119283b4a9bSStephen M. Cameron 				dev->offload_enabled = 0;
2120080ef1ccSDon Brace 			INIT_WORK(&cp->work, hpsa_command_resubmit_worker);
2121080ef1ccSDon Brace 			queue_work_on(raw_smp_processor_id(),
2122080ef1ccSDon Brace 					h->resubmit_wq, &cp->work);
2123283b4a9bSStephen M. Cameron 			return;
2124283b4a9bSStephen M. Cameron 		}
2125e1f7de0cSMatt Gates 	}
2126e1f7de0cSMatt Gates 
2127edd16368SStephen M. Cameron 	/* an error has occurred */
2128edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
2129edd16368SStephen M. Cameron 
2130edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
21319437ac43SStephen Cameron 		cmd->result |= ei->ScsiStatus;
21329437ac43SStephen Cameron 		/* copy the sense data */
21339437ac43SStephen Cameron 		if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
21349437ac43SStephen Cameron 			sense_data_size = SCSI_SENSE_BUFFERSIZE;
21359437ac43SStephen Cameron 		else
21369437ac43SStephen Cameron 			sense_data_size = sizeof(ei->SenseInfo);
21379437ac43SStephen Cameron 		if (ei->SenseLen < sense_data_size)
21389437ac43SStephen Cameron 			sense_data_size = ei->SenseLen;
21399437ac43SStephen Cameron 		memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
21409437ac43SStephen Cameron 		if (ei->ScsiStatus)
21419437ac43SStephen Cameron 			decode_sense_data(ei->SenseInfo, sense_data_size,
21429437ac43SStephen Cameron 				&sense_key, &asc, &ascq);
2143edd16368SStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
21441d3b3609SMatt Gates 			if (sense_key == ABORTED_COMMAND) {
21452e311fbaSStephen M. Cameron 				cmd->result |= DID_SOFT_ERROR << 16;
21461d3b3609SMatt Gates 				break;
21471d3b3609SMatt Gates 			}
2148edd16368SStephen M. Cameron 			break;
2149edd16368SStephen M. Cameron 		}
2150edd16368SStephen M. Cameron 		/* Problem was not a check condition
2151edd16368SStephen M. Cameron 		 * Pass it up to the upper layers...
2152edd16368SStephen M. Cameron 		 */
2153edd16368SStephen M. Cameron 		if (ei->ScsiStatus) {
2154edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
2155edd16368SStephen M. Cameron 				"Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
2156edd16368SStephen M. Cameron 				"Returning result: 0x%x\n",
2157edd16368SStephen M. Cameron 				cp, ei->ScsiStatus,
2158edd16368SStephen M. Cameron 				sense_key, asc, ascq,
2159edd16368SStephen M. Cameron 				cmd->result);
2160edd16368SStephen M. Cameron 		} else {  /* scsi status is zero??? How??? */
2161edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
2162edd16368SStephen M. Cameron 				"Returning no connection.\n", cp),
2163edd16368SStephen M. Cameron 
2164edd16368SStephen M. Cameron 			/* Ordinarily, this case should never happen,
2165edd16368SStephen M. Cameron 			 * but there is a bug in some released firmware
2166edd16368SStephen M. Cameron 			 * revisions that allows it to happen if, for
2167edd16368SStephen M. Cameron 			 * example, a 4100 backplane loses power and
2168edd16368SStephen M. Cameron 			 * the tape drive is in it.  We assume that
2169edd16368SStephen M. Cameron 			 * it's a fatal error of some kind because we
2170edd16368SStephen M. Cameron 			 * can't show that it wasn't. We will make it
2171edd16368SStephen M. Cameron 			 * look like selection timeout since that is
2172edd16368SStephen M. Cameron 			 * the most common reason for this to occur,
2173edd16368SStephen M. Cameron 			 * and it's severe enough.
2174edd16368SStephen M. Cameron 			 */
2175edd16368SStephen M. Cameron 
2176edd16368SStephen M. Cameron 			cmd->result = DID_NO_CONNECT << 16;
2177edd16368SStephen M. Cameron 		}
2178edd16368SStephen M. Cameron 		break;
2179edd16368SStephen M. Cameron 
2180edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
2181edd16368SStephen M. Cameron 		break;
2182edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
2183f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev,
2184f42e81e1SStephen Cameron 			"CDB %16phN data overrun\n", cp->Request.CDB);
2185edd16368SStephen M. Cameron 		break;
2186edd16368SStephen M. Cameron 	case CMD_INVALID: {
2187edd16368SStephen M. Cameron 		/* print_bytes(cp, sizeof(*cp), 1, 0);
2188edd16368SStephen M. Cameron 		print_cmd(cp); */
2189edd16368SStephen M. Cameron 		/* We get CMD_INVALID if you address a non-existent device
2190edd16368SStephen M. Cameron 		 * instead of a selection timeout (no response).  You will
2191edd16368SStephen M. Cameron 		 * see this if you yank out a drive, then try to access it.
2192edd16368SStephen M. Cameron 		 * This is kind of a shame because it means that any other
2193edd16368SStephen M. Cameron 		 * CMD_INVALID (e.g. driver bug) will get interpreted as a
2194edd16368SStephen M. Cameron 		 * missing target. */
2195edd16368SStephen M. Cameron 		cmd->result = DID_NO_CONNECT << 16;
2196edd16368SStephen M. Cameron 	}
2197edd16368SStephen M. Cameron 		break;
2198edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
2199256d0eaaSStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2200f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : protocol error\n",
2201f42e81e1SStephen Cameron 				cp->Request.CDB);
2202edd16368SStephen M. Cameron 		break;
2203edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
2204edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2205f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : hardware error\n",
2206f42e81e1SStephen Cameron 			cp->Request.CDB);
2207edd16368SStephen M. Cameron 		break;
2208edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
2209edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2210f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : connection lost\n",
2211f42e81e1SStephen Cameron 			cp->Request.CDB);
2212edd16368SStephen M. Cameron 		break;
2213edd16368SStephen M. Cameron 	case CMD_ABORTED:
2214edd16368SStephen M. Cameron 		cmd->result = DID_ABORT << 16;
2215f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN was aborted with status 0x%x\n",
2216f42e81e1SStephen Cameron 				cp->Request.CDB, ei->ScsiStatus);
2217edd16368SStephen M. Cameron 		break;
2218edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
2219edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2220f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : abort failed\n",
2221f42e81e1SStephen Cameron 			cp->Request.CDB);
2222edd16368SStephen M. Cameron 		break;
2223edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
2224f6e76055SStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
2225f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : unsolicited abort\n",
2226f42e81e1SStephen Cameron 			cp->Request.CDB);
2227edd16368SStephen M. Cameron 		break;
2228edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
2229edd16368SStephen M. Cameron 		cmd->result = DID_TIME_OUT << 16;
2230f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN timed out\n",
2231f42e81e1SStephen Cameron 			cp->Request.CDB);
2232edd16368SStephen M. Cameron 		break;
22331d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
22341d5e2ed0SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
22351d5e2ed0SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Command unabortable\n");
22361d5e2ed0SStephen M. Cameron 		break;
22379437ac43SStephen Cameron 	case CMD_TMF_STATUS:
22389437ac43SStephen Cameron 		if (hpsa_evaluate_tmf_status(h, cp)) /* TMF failed? */
22399437ac43SStephen Cameron 			cmd->result = DID_ERROR << 16;
22409437ac43SStephen Cameron 		break;
2241283b4a9bSStephen M. Cameron 	case CMD_IOACCEL_DISABLED:
2242283b4a9bSStephen M. Cameron 		/* This only handles the direct pass-through case since RAID
2243283b4a9bSStephen M. Cameron 		 * offload is handled above.  Just attempt a retry.
2244283b4a9bSStephen M. Cameron 		 */
2245283b4a9bSStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16;
2246283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev,
2247283b4a9bSStephen M. Cameron 				"cp %p had HP SSD Smart Path error\n", cp);
2248283b4a9bSStephen M. Cameron 		break;
2249edd16368SStephen M. Cameron 	default:
2250edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2251edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
2252edd16368SStephen M. Cameron 				cp, ei->CommandStatus);
2253edd16368SStephen M. Cameron 	}
2254edd16368SStephen M. Cameron 	cmd_free(h, cp);
22552cc5bfafSTomas Henzl 	cmd->scsi_done(cmd);
2256edd16368SStephen M. Cameron }
2257edd16368SStephen M. Cameron 
2258edd16368SStephen M. Cameron static void hpsa_pci_unmap(struct pci_dev *pdev,
2259edd16368SStephen M. Cameron 	struct CommandList *c, int sg_used, int data_direction)
2260edd16368SStephen M. Cameron {
2261edd16368SStephen M. Cameron 	int i;
2262edd16368SStephen M. Cameron 
226350a0decfSStephen M. Cameron 	for (i = 0; i < sg_used; i++)
226450a0decfSStephen M. Cameron 		pci_unmap_single(pdev, (dma_addr_t) le64_to_cpu(c->SG[i].Addr),
226550a0decfSStephen M. Cameron 				le32_to_cpu(c->SG[i].Len),
2266edd16368SStephen M. Cameron 				data_direction);
2267edd16368SStephen M. Cameron }
2268edd16368SStephen M. Cameron 
2269a2dac136SStephen M. Cameron static int hpsa_map_one(struct pci_dev *pdev,
2270edd16368SStephen M. Cameron 		struct CommandList *cp,
2271edd16368SStephen M. Cameron 		unsigned char *buf,
2272edd16368SStephen M. Cameron 		size_t buflen,
2273edd16368SStephen M. Cameron 		int data_direction)
2274edd16368SStephen M. Cameron {
227501a02ffcSStephen M. Cameron 	u64 addr64;
2276edd16368SStephen M. Cameron 
2277edd16368SStephen M. Cameron 	if (buflen == 0 || data_direction == PCI_DMA_NONE) {
2278edd16368SStephen M. Cameron 		cp->Header.SGList = 0;
227950a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(0);
2280a2dac136SStephen M. Cameron 		return 0;
2281edd16368SStephen M. Cameron 	}
2282edd16368SStephen M. Cameron 
228350a0decfSStephen M. Cameron 	addr64 = pci_map_single(pdev, buf, buflen, data_direction);
2284eceaae18SShuah Khan 	if (dma_mapping_error(&pdev->dev, addr64)) {
2285a2dac136SStephen M. Cameron 		/* Prevent subsequent unmap of something never mapped */
2286eceaae18SShuah Khan 		cp->Header.SGList = 0;
228750a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(0);
2288a2dac136SStephen M. Cameron 		return -1;
2289eceaae18SShuah Khan 	}
229050a0decfSStephen M. Cameron 	cp->SG[0].Addr = cpu_to_le64(addr64);
229150a0decfSStephen M. Cameron 	cp->SG[0].Len = cpu_to_le32(buflen);
229250a0decfSStephen M. Cameron 	cp->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* we are not chaining */
229350a0decfSStephen M. Cameron 	cp->Header.SGList = 1;   /* no. SGs contig in this cmd */
229450a0decfSStephen M. Cameron 	cp->Header.SGTotal = cpu_to_le16(1); /* total sgs in cmd list */
2295a2dac136SStephen M. Cameron 	return 0;
2296edd16368SStephen M. Cameron }
2297edd16368SStephen M. Cameron 
229825163bd5SWebb Scales #define NO_TIMEOUT ((unsigned long) -1)
229925163bd5SWebb Scales #define DEFAULT_TIMEOUT 30000 /* milliseconds */
230025163bd5SWebb Scales static int hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
230125163bd5SWebb Scales 	struct CommandList *c, int reply_queue, unsigned long timeout_msecs)
2302edd16368SStephen M. Cameron {
2303edd16368SStephen M. Cameron 	DECLARE_COMPLETION_ONSTACK(wait);
2304edd16368SStephen M. Cameron 
2305edd16368SStephen M. Cameron 	c->waiting = &wait;
230625163bd5SWebb Scales 	__enqueue_cmd_and_start_io(h, c, reply_queue);
230725163bd5SWebb Scales 	if (timeout_msecs == NO_TIMEOUT) {
230825163bd5SWebb Scales 		/* TODO: get rid of this no-timeout thing */
230925163bd5SWebb Scales 		wait_for_completion_io(&wait);
231025163bd5SWebb Scales 		return IO_OK;
231125163bd5SWebb Scales 	}
231225163bd5SWebb Scales 	if (!wait_for_completion_io_timeout(&wait,
231325163bd5SWebb Scales 					msecs_to_jiffies(timeout_msecs))) {
231425163bd5SWebb Scales 		dev_warn(&h->pdev->dev, "Command timed out.\n");
231525163bd5SWebb Scales 		return -ETIMEDOUT;
231625163bd5SWebb Scales 	}
231725163bd5SWebb Scales 	return IO_OK;
231825163bd5SWebb Scales }
231925163bd5SWebb Scales 
232025163bd5SWebb Scales static int hpsa_scsi_do_simple_cmd(struct ctlr_info *h, struct CommandList *c,
232125163bd5SWebb Scales 				   int reply_queue, unsigned long timeout_msecs)
232225163bd5SWebb Scales {
232325163bd5SWebb Scales 	if (unlikely(lockup_detected(h))) {
232425163bd5SWebb Scales 		c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
232525163bd5SWebb Scales 		return IO_OK;
232625163bd5SWebb Scales 	}
232725163bd5SWebb Scales 	return hpsa_scsi_do_simple_cmd_core(h, c, reply_queue, timeout_msecs);
2328edd16368SStephen M. Cameron }
2329edd16368SStephen M. Cameron 
2330094963daSStephen M. Cameron static u32 lockup_detected(struct ctlr_info *h)
2331094963daSStephen M. Cameron {
2332094963daSStephen M. Cameron 	int cpu;
2333094963daSStephen M. Cameron 	u32 rc, *lockup_detected;
2334094963daSStephen M. Cameron 
2335094963daSStephen M. Cameron 	cpu = get_cpu();
2336094963daSStephen M. Cameron 	lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
2337094963daSStephen M. Cameron 	rc = *lockup_detected;
2338094963daSStephen M. Cameron 	put_cpu();
2339094963daSStephen M. Cameron 	return rc;
2340094963daSStephen M. Cameron }
2341094963daSStephen M. Cameron 
23429c2fc160SStephen M. Cameron #define MAX_DRIVER_CMD_RETRIES 25
234325163bd5SWebb Scales static int hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
234425163bd5SWebb Scales 	struct CommandList *c, int data_direction, unsigned long timeout_msecs)
2345edd16368SStephen M. Cameron {
23469c2fc160SStephen M. Cameron 	int backoff_time = 10, retry_count = 0;
234725163bd5SWebb Scales 	int rc;
2348edd16368SStephen M. Cameron 
2349edd16368SStephen M. Cameron 	do {
23507630abd0SJoe Perches 		memset(c->err_info, 0, sizeof(*c->err_info));
235125163bd5SWebb Scales 		rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
235225163bd5SWebb Scales 						  timeout_msecs);
235325163bd5SWebb Scales 		if (rc)
235425163bd5SWebb Scales 			break;
2355edd16368SStephen M. Cameron 		retry_count++;
23569c2fc160SStephen M. Cameron 		if (retry_count > 3) {
23579c2fc160SStephen M. Cameron 			msleep(backoff_time);
23589c2fc160SStephen M. Cameron 			if (backoff_time < 1000)
23599c2fc160SStephen M. Cameron 				backoff_time *= 2;
23609c2fc160SStephen M. Cameron 		}
2361852af20aSMatt Bondurant 	} while ((check_for_unit_attention(h, c) ||
23629c2fc160SStephen M. Cameron 			check_for_busy(h, c)) &&
23639c2fc160SStephen M. Cameron 			retry_count <= MAX_DRIVER_CMD_RETRIES);
2364edd16368SStephen M. Cameron 	hpsa_pci_unmap(h->pdev, c, 1, data_direction);
236525163bd5SWebb Scales 	if (retry_count > MAX_DRIVER_CMD_RETRIES)
236625163bd5SWebb Scales 		rc = -EIO;
236725163bd5SWebb Scales 	return rc;
2368edd16368SStephen M. Cameron }
2369edd16368SStephen M. Cameron 
2370d1e8beacSStephen M. Cameron static void hpsa_print_cmd(struct ctlr_info *h, char *txt,
2371d1e8beacSStephen M. Cameron 				struct CommandList *c)
2372edd16368SStephen M. Cameron {
2373d1e8beacSStephen M. Cameron 	const u8 *cdb = c->Request.CDB;
2374d1e8beacSStephen M. Cameron 	const u8 *lun = c->Header.LUN.LunAddrBytes;
2375edd16368SStephen M. Cameron 
2376d1e8beacSStephen M. Cameron 	dev_warn(&h->pdev->dev, "%s: LUN:%02x%02x%02x%02x%02x%02x%02x%02x"
2377d1e8beacSStephen M. Cameron 	" CDB:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
2378d1e8beacSStephen M. Cameron 		txt, lun[0], lun[1], lun[2], lun[3],
2379d1e8beacSStephen M. Cameron 		lun[4], lun[5], lun[6], lun[7],
2380d1e8beacSStephen M. Cameron 		cdb[0], cdb[1], cdb[2], cdb[3],
2381d1e8beacSStephen M. Cameron 		cdb[4], cdb[5], cdb[6], cdb[7],
2382d1e8beacSStephen M. Cameron 		cdb[8], cdb[9], cdb[10], cdb[11],
2383d1e8beacSStephen M. Cameron 		cdb[12], cdb[13], cdb[14], cdb[15]);
2384d1e8beacSStephen M. Cameron }
2385d1e8beacSStephen M. Cameron 
2386d1e8beacSStephen M. Cameron static void hpsa_scsi_interpret_error(struct ctlr_info *h,
2387d1e8beacSStephen M. Cameron 			struct CommandList *cp)
2388d1e8beacSStephen M. Cameron {
2389d1e8beacSStephen M. Cameron 	const struct ErrorInfo *ei = cp->err_info;
2390d1e8beacSStephen M. Cameron 	struct device *d = &cp->h->pdev->dev;
23919437ac43SStephen Cameron 	u8 sense_key, asc, ascq;
23929437ac43SStephen Cameron 	int sense_len;
2393d1e8beacSStephen M. Cameron 
2394edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
2395edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
23969437ac43SStephen Cameron 		if (ei->SenseLen > sizeof(ei->SenseInfo))
23979437ac43SStephen Cameron 			sense_len = sizeof(ei->SenseInfo);
23989437ac43SStephen Cameron 		else
23999437ac43SStephen Cameron 			sense_len = ei->SenseLen;
24009437ac43SStephen Cameron 		decode_sense_data(ei->SenseInfo, sense_len,
24019437ac43SStephen Cameron 					&sense_key, &asc, &ascq);
2402d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "SCSI status", cp);
2403d1e8beacSStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION)
24049437ac43SStephen Cameron 			dev_warn(d, "SCSI Status = 02, Sense key = 0x%02x, ASC = 0x%02x, ASCQ = 0x%02x\n",
24059437ac43SStephen Cameron 				sense_key, asc, ascq);
2406d1e8beacSStephen M. Cameron 		else
24079437ac43SStephen Cameron 			dev_warn(d, "SCSI Status = 0x%02x\n", ei->ScsiStatus);
2408edd16368SStephen M. Cameron 		if (ei->ScsiStatus == 0)
2409edd16368SStephen M. Cameron 			dev_warn(d, "SCSI status is abnormally zero.  "
2410edd16368SStephen M. Cameron 			"(probably indicates selection timeout "
2411edd16368SStephen M. Cameron 			"reported incorrectly due to a known "
2412edd16368SStephen M. Cameron 			"firmware bug, circa July, 2001.)\n");
2413edd16368SStephen M. Cameron 		break;
2414edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
2415edd16368SStephen M. Cameron 		break;
2416edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
2417d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "overrun condition", cp);
2418edd16368SStephen M. Cameron 		break;
2419edd16368SStephen M. Cameron 	case CMD_INVALID: {
2420edd16368SStephen M. Cameron 		/* controller unfortunately reports SCSI passthru's
2421edd16368SStephen M. Cameron 		 * to non-existent targets as invalid commands.
2422edd16368SStephen M. Cameron 		 */
2423d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "invalid command", cp);
2424d1e8beacSStephen M. Cameron 		dev_warn(d, "probably means device no longer present\n");
2425edd16368SStephen M. Cameron 		}
2426edd16368SStephen M. Cameron 		break;
2427edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
2428d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "protocol error", cp);
2429edd16368SStephen M. Cameron 		break;
2430edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
2431d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "hardware error", cp);
2432edd16368SStephen M. Cameron 		break;
2433edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
2434d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "connection lost", cp);
2435edd16368SStephen M. Cameron 		break;
2436edd16368SStephen M. Cameron 	case CMD_ABORTED:
2437d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "aborted", cp);
2438edd16368SStephen M. Cameron 		break;
2439edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
2440d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "abort failed", cp);
2441edd16368SStephen M. Cameron 		break;
2442edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
2443d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unsolicited abort", cp);
2444edd16368SStephen M. Cameron 		break;
2445edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
2446d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "timed out", cp);
2447edd16368SStephen M. Cameron 		break;
24481d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
2449d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unabortable", cp);
24501d5e2ed0SStephen M. Cameron 		break;
245125163bd5SWebb Scales 	case CMD_CTLR_LOCKUP:
245225163bd5SWebb Scales 		hpsa_print_cmd(h, "controller lockup detected", cp);
245325163bd5SWebb Scales 		break;
2454edd16368SStephen M. Cameron 	default:
2455d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unknown status", cp);
2456d1e8beacSStephen M. Cameron 		dev_warn(d, "Unknown command status %x\n",
2457edd16368SStephen M. Cameron 				ei->CommandStatus);
2458edd16368SStephen M. Cameron 	}
2459edd16368SStephen M. Cameron }
2460edd16368SStephen M. Cameron 
2461edd16368SStephen M. Cameron static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
2462b7bb24ebSStephen M. Cameron 			u16 page, unsigned char *buf,
2463edd16368SStephen M. Cameron 			unsigned char bufsize)
2464edd16368SStephen M. Cameron {
2465edd16368SStephen M. Cameron 	int rc = IO_OK;
2466edd16368SStephen M. Cameron 	struct CommandList *c;
2467edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2468edd16368SStephen M. Cameron 
246945fcb86eSStephen Cameron 	c = cmd_alloc(h);
2470edd16368SStephen M. Cameron 
2471a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
2472a2dac136SStephen M. Cameron 			page, scsi3addr, TYPE_CMD)) {
2473a2dac136SStephen M. Cameron 		rc = -1;
2474a2dac136SStephen M. Cameron 		goto out;
2475a2dac136SStephen M. Cameron 	}
247625163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
247725163bd5SWebb Scales 					PCI_DMA_FROMDEVICE, NO_TIMEOUT);
247825163bd5SWebb Scales 	if (rc)
247925163bd5SWebb Scales 		goto out;
2480edd16368SStephen M. Cameron 	ei = c->err_info;
2481edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2482d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2483edd16368SStephen M. Cameron 		rc = -1;
2484edd16368SStephen M. Cameron 	}
2485a2dac136SStephen M. Cameron out:
248645fcb86eSStephen Cameron 	cmd_free(h, c);
2487edd16368SStephen M. Cameron 	return rc;
2488edd16368SStephen M. Cameron }
2489edd16368SStephen M. Cameron 
2490316b221aSStephen M. Cameron static int hpsa_bmic_ctrl_mode_sense(struct ctlr_info *h,
2491316b221aSStephen M. Cameron 		unsigned char *scsi3addr, unsigned char page,
2492316b221aSStephen M. Cameron 		struct bmic_controller_parameters *buf, size_t bufsize)
2493316b221aSStephen M. Cameron {
2494316b221aSStephen M. Cameron 	int rc = IO_OK;
2495316b221aSStephen M. Cameron 	struct CommandList *c;
2496316b221aSStephen M. Cameron 	struct ErrorInfo *ei;
2497316b221aSStephen M. Cameron 
249845fcb86eSStephen Cameron 	c = cmd_alloc(h);
2499316b221aSStephen M. Cameron 	if (fill_cmd(c, BMIC_SENSE_CONTROLLER_PARAMETERS, h, buf, bufsize,
2500316b221aSStephen M. Cameron 			page, scsi3addr, TYPE_CMD)) {
2501316b221aSStephen M. Cameron 		rc = -1;
2502316b221aSStephen M. Cameron 		goto out;
2503316b221aSStephen M. Cameron 	}
250425163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
250525163bd5SWebb Scales 			PCI_DMA_FROMDEVICE, NO_TIMEOUT);
250625163bd5SWebb Scales 	if (rc)
250725163bd5SWebb Scales 		goto out;
2508316b221aSStephen M. Cameron 	ei = c->err_info;
2509316b221aSStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2510316b221aSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2511316b221aSStephen M. Cameron 		rc = -1;
2512316b221aSStephen M. Cameron 	}
2513316b221aSStephen M. Cameron out:
251445fcb86eSStephen Cameron 	cmd_free(h, c);
2515316b221aSStephen M. Cameron 	return rc;
2516316b221aSStephen M. Cameron }
2517316b221aSStephen M. Cameron 
2518bf711ac6SScott Teel static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
251925163bd5SWebb Scales 	u8 reset_type, int reply_queue)
2520edd16368SStephen M. Cameron {
2521edd16368SStephen M. Cameron 	int rc = IO_OK;
2522edd16368SStephen M. Cameron 	struct CommandList *c;
2523edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2524edd16368SStephen M. Cameron 
252545fcb86eSStephen Cameron 	c = cmd_alloc(h);
2526edd16368SStephen M. Cameron 
2527edd16368SStephen M. Cameron 
2528a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map. */
2529bf711ac6SScott Teel 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
2530bf711ac6SScott Teel 			scsi3addr, TYPE_MSG);
2531bf711ac6SScott Teel 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to LUN reset */
253225163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
253325163bd5SWebb Scales 	if (rc) {
253425163bd5SWebb Scales 		dev_warn(&h->pdev->dev, "Failed to send reset command\n");
253525163bd5SWebb Scales 		goto out;
253625163bd5SWebb Scales 	}
2537edd16368SStephen M. Cameron 	/* no unmap needed here because no data xfer. */
2538edd16368SStephen M. Cameron 
2539edd16368SStephen M. Cameron 	ei = c->err_info;
2540edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0) {
2541d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2542edd16368SStephen M. Cameron 		rc = -1;
2543edd16368SStephen M. Cameron 	}
254425163bd5SWebb Scales out:
254545fcb86eSStephen Cameron 	cmd_free(h, c);
2546edd16368SStephen M. Cameron 	return rc;
2547edd16368SStephen M. Cameron }
2548edd16368SStephen M. Cameron 
2549edd16368SStephen M. Cameron static void hpsa_get_raid_level(struct ctlr_info *h,
2550edd16368SStephen M. Cameron 	unsigned char *scsi3addr, unsigned char *raid_level)
2551edd16368SStephen M. Cameron {
2552edd16368SStephen M. Cameron 	int rc;
2553edd16368SStephen M. Cameron 	unsigned char *buf;
2554edd16368SStephen M. Cameron 
2555edd16368SStephen M. Cameron 	*raid_level = RAID_UNKNOWN;
2556edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2557edd16368SStephen M. Cameron 	if (!buf)
2558edd16368SStephen M. Cameron 		return;
2559b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0xC1, buf, 64);
2560edd16368SStephen M. Cameron 	if (rc == 0)
2561edd16368SStephen M. Cameron 		*raid_level = buf[8];
2562edd16368SStephen M. Cameron 	if (*raid_level > RAID_UNKNOWN)
2563edd16368SStephen M. Cameron 		*raid_level = RAID_UNKNOWN;
2564edd16368SStephen M. Cameron 	kfree(buf);
2565edd16368SStephen M. Cameron 	return;
2566edd16368SStephen M. Cameron }
2567edd16368SStephen M. Cameron 
2568283b4a9bSStephen M. Cameron #define HPSA_MAP_DEBUG
2569283b4a9bSStephen M. Cameron #ifdef HPSA_MAP_DEBUG
2570283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
2571283b4a9bSStephen M. Cameron 				struct raid_map_data *map_buff)
2572283b4a9bSStephen M. Cameron {
2573283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map_buff->data[0];
2574283b4a9bSStephen M. Cameron 	int map, row, col;
2575283b4a9bSStephen M. Cameron 	u16 map_cnt, row_cnt, disks_per_row;
2576283b4a9bSStephen M. Cameron 
2577283b4a9bSStephen M. Cameron 	if (rc != 0)
2578283b4a9bSStephen M. Cameron 		return;
2579283b4a9bSStephen M. Cameron 
25802ba8bfc8SStephen M. Cameron 	/* Show details only if debugging has been activated. */
25812ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug < 2)
25822ba8bfc8SStephen M. Cameron 		return;
25832ba8bfc8SStephen M. Cameron 
2584283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "structure_size = %u\n",
2585283b4a9bSStephen M. Cameron 				le32_to_cpu(map_buff->structure_size));
2586283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
2587283b4a9bSStephen M. Cameron 			le32_to_cpu(map_buff->volume_blk_size));
2588283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
2589283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->volume_blk_cnt));
2590283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
2591283b4a9bSStephen M. Cameron 			map_buff->phys_blk_shift);
2592283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
2593283b4a9bSStephen M. Cameron 			map_buff->parity_rotation_shift);
2594283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "strip_size = %u\n",
2595283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->strip_size));
2596283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
2597283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_starting_blk));
2598283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
2599283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_blk_cnt));
2600283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
2601283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->data_disks_per_row));
2602283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
2603283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->metadata_disks_per_row));
2604283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "row_cnt = %u\n",
2605283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->row_cnt));
2606283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "layout_map_count = %u\n",
2607283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->layout_map_count));
26082b08b3e9SDon Brace 	dev_info(&h->pdev->dev, "flags = 0x%x\n",
2609dd0e19f3SScott Teel 			le16_to_cpu(map_buff->flags));
26102b08b3e9SDon Brace 	dev_info(&h->pdev->dev, "encrypytion = %s\n",
26112b08b3e9SDon Brace 			le16_to_cpu(map_buff->flags) &
26122b08b3e9SDon Brace 			RAID_MAP_FLAG_ENCRYPT_ON ?  "ON" : "OFF");
2613dd0e19f3SScott Teel 	dev_info(&h->pdev->dev, "dekindex = %u\n",
2614dd0e19f3SScott Teel 			le16_to_cpu(map_buff->dekindex));
2615283b4a9bSStephen M. Cameron 	map_cnt = le16_to_cpu(map_buff->layout_map_count);
2616283b4a9bSStephen M. Cameron 	for (map = 0; map < map_cnt; map++) {
2617283b4a9bSStephen M. Cameron 		dev_info(&h->pdev->dev, "Map%u:\n", map);
2618283b4a9bSStephen M. Cameron 		row_cnt = le16_to_cpu(map_buff->row_cnt);
2619283b4a9bSStephen M. Cameron 		for (row = 0; row < row_cnt; row++) {
2620283b4a9bSStephen M. Cameron 			dev_info(&h->pdev->dev, "  Row%u:\n", row);
2621283b4a9bSStephen M. Cameron 			disks_per_row =
2622283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->data_disks_per_row);
2623283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2624283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2625283b4a9bSStephen M. Cameron 					"    D%02u: h=0x%04x xor=%u,%u\n",
2626283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2627283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2628283b4a9bSStephen M. Cameron 			disks_per_row =
2629283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->metadata_disks_per_row);
2630283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2631283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2632283b4a9bSStephen M. Cameron 					"    M%02u: h=0x%04x xor=%u,%u\n",
2633283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2634283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2635283b4a9bSStephen M. Cameron 		}
2636283b4a9bSStephen M. Cameron 	}
2637283b4a9bSStephen M. Cameron }
2638283b4a9bSStephen M. Cameron #else
2639283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
2640283b4a9bSStephen M. Cameron 			__attribute__((unused)) int rc,
2641283b4a9bSStephen M. Cameron 			__attribute__((unused)) struct raid_map_data *map_buff)
2642283b4a9bSStephen M. Cameron {
2643283b4a9bSStephen M. Cameron }
2644283b4a9bSStephen M. Cameron #endif
2645283b4a9bSStephen M. Cameron 
2646283b4a9bSStephen M. Cameron static int hpsa_get_raid_map(struct ctlr_info *h,
2647283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2648283b4a9bSStephen M. Cameron {
2649283b4a9bSStephen M. Cameron 	int rc = 0;
2650283b4a9bSStephen M. Cameron 	struct CommandList *c;
2651283b4a9bSStephen M. Cameron 	struct ErrorInfo *ei;
2652283b4a9bSStephen M. Cameron 
265345fcb86eSStephen Cameron 	c = cmd_alloc(h);
2654*bf43caf3SRobert Elliott 
2655283b4a9bSStephen M. Cameron 	if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
2656283b4a9bSStephen M. Cameron 			sizeof(this_device->raid_map), 0,
2657283b4a9bSStephen M. Cameron 			scsi3addr, TYPE_CMD)) {
2658283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "Out of memory in hpsa_get_raid_map()\n");
265925163bd5SWebb Scales 		rc = -ENOMEM;
266025163bd5SWebb Scales 		goto out;
2661283b4a9bSStephen M. Cameron 	}
266225163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
266325163bd5SWebb Scales 					PCI_DMA_FROMDEVICE, NO_TIMEOUT);
266425163bd5SWebb Scales 	if (rc)
266525163bd5SWebb Scales 		goto out;
2666283b4a9bSStephen M. Cameron 	ei = c->err_info;
2667283b4a9bSStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2668d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
266925163bd5SWebb Scales 		rc = -1;
267025163bd5SWebb Scales 		goto out;
2671283b4a9bSStephen M. Cameron 	}
267245fcb86eSStephen Cameron 	cmd_free(h, c);
2673283b4a9bSStephen M. Cameron 
2674283b4a9bSStephen M. Cameron 	/* @todo in the future, dynamically allocate RAID map memory */
2675283b4a9bSStephen M. Cameron 	if (le32_to_cpu(this_device->raid_map.structure_size) >
2676283b4a9bSStephen M. Cameron 				sizeof(this_device->raid_map)) {
2677283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
2678283b4a9bSStephen M. Cameron 		rc = -1;
2679283b4a9bSStephen M. Cameron 	}
2680283b4a9bSStephen M. Cameron 	hpsa_debug_map_buff(h, rc, &this_device->raid_map);
2681283b4a9bSStephen M. Cameron 	return rc;
268225163bd5SWebb Scales out:
268325163bd5SWebb Scales 	cmd_free(h, c);
268425163bd5SWebb Scales 	return rc;
2685283b4a9bSStephen M. Cameron }
2686283b4a9bSStephen M. Cameron 
268703383736SDon Brace static int hpsa_bmic_id_physical_device(struct ctlr_info *h,
268803383736SDon Brace 		unsigned char scsi3addr[], u16 bmic_device_index,
268903383736SDon Brace 		struct bmic_identify_physical_device *buf, size_t bufsize)
269003383736SDon Brace {
269103383736SDon Brace 	int rc = IO_OK;
269203383736SDon Brace 	struct CommandList *c;
269303383736SDon Brace 	struct ErrorInfo *ei;
269403383736SDon Brace 
269503383736SDon Brace 	c = cmd_alloc(h);
269603383736SDon Brace 	rc = fill_cmd(c, BMIC_IDENTIFY_PHYSICAL_DEVICE, h, buf, bufsize,
269703383736SDon Brace 		0, RAID_CTLR_LUNID, TYPE_CMD);
269803383736SDon Brace 	if (rc)
269903383736SDon Brace 		goto out;
270003383736SDon Brace 
270103383736SDon Brace 	c->Request.CDB[2] = bmic_device_index & 0xff;
270203383736SDon Brace 	c->Request.CDB[9] = (bmic_device_index >> 8) & 0xff;
270303383736SDon Brace 
270425163bd5SWebb Scales 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE,
270525163bd5SWebb Scales 						NO_TIMEOUT);
270603383736SDon Brace 	ei = c->err_info;
270703383736SDon Brace 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
270803383736SDon Brace 		hpsa_scsi_interpret_error(h, c);
270903383736SDon Brace 		rc = -1;
271003383736SDon Brace 	}
271103383736SDon Brace out:
271203383736SDon Brace 	cmd_free(h, c);
271303383736SDon Brace 	return rc;
271403383736SDon Brace }
271503383736SDon Brace 
27161b70150aSStephen M. Cameron static int hpsa_vpd_page_supported(struct ctlr_info *h,
27171b70150aSStephen M. Cameron 	unsigned char scsi3addr[], u8 page)
27181b70150aSStephen M. Cameron {
27191b70150aSStephen M. Cameron 	int rc;
27201b70150aSStephen M. Cameron 	int i;
27211b70150aSStephen M. Cameron 	int pages;
27221b70150aSStephen M. Cameron 	unsigned char *buf, bufsize;
27231b70150aSStephen M. Cameron 
27241b70150aSStephen M. Cameron 	buf = kzalloc(256, GFP_KERNEL);
27251b70150aSStephen M. Cameron 	if (!buf)
27261b70150aSStephen M. Cameron 		return 0;
27271b70150aSStephen M. Cameron 
27281b70150aSStephen M. Cameron 	/* Get the size of the page list first */
27291b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
27301b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
27311b70150aSStephen M. Cameron 				buf, HPSA_VPD_HEADER_SZ);
27321b70150aSStephen M. Cameron 	if (rc != 0)
27331b70150aSStephen M. Cameron 		goto exit_unsupported;
27341b70150aSStephen M. Cameron 	pages = buf[3];
27351b70150aSStephen M. Cameron 	if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
27361b70150aSStephen M. Cameron 		bufsize = pages + HPSA_VPD_HEADER_SZ;
27371b70150aSStephen M. Cameron 	else
27381b70150aSStephen M. Cameron 		bufsize = 255;
27391b70150aSStephen M. Cameron 
27401b70150aSStephen M. Cameron 	/* Get the whole VPD page list */
27411b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
27421b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
27431b70150aSStephen M. Cameron 				buf, bufsize);
27441b70150aSStephen M. Cameron 	if (rc != 0)
27451b70150aSStephen M. Cameron 		goto exit_unsupported;
27461b70150aSStephen M. Cameron 
27471b70150aSStephen M. Cameron 	pages = buf[3];
27481b70150aSStephen M. Cameron 	for (i = 1; i <= pages; i++)
27491b70150aSStephen M. Cameron 		if (buf[3 + i] == page)
27501b70150aSStephen M. Cameron 			goto exit_supported;
27511b70150aSStephen M. Cameron exit_unsupported:
27521b70150aSStephen M. Cameron 	kfree(buf);
27531b70150aSStephen M. Cameron 	return 0;
27541b70150aSStephen M. Cameron exit_supported:
27551b70150aSStephen M. Cameron 	kfree(buf);
27561b70150aSStephen M. Cameron 	return 1;
27571b70150aSStephen M. Cameron }
27581b70150aSStephen M. Cameron 
2759283b4a9bSStephen M. Cameron static void hpsa_get_ioaccel_status(struct ctlr_info *h,
2760283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2761283b4a9bSStephen M. Cameron {
2762283b4a9bSStephen M. Cameron 	int rc;
2763283b4a9bSStephen M. Cameron 	unsigned char *buf;
2764283b4a9bSStephen M. Cameron 	u8 ioaccel_status;
2765283b4a9bSStephen M. Cameron 
2766283b4a9bSStephen M. Cameron 	this_device->offload_config = 0;
2767283b4a9bSStephen M. Cameron 	this_device->offload_enabled = 0;
276841ce4c35SStephen Cameron 	this_device->offload_to_be_enabled = 0;
2769283b4a9bSStephen M. Cameron 
2770283b4a9bSStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2771283b4a9bSStephen M. Cameron 	if (!buf)
2772283b4a9bSStephen M. Cameron 		return;
27731b70150aSStephen M. Cameron 	if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
27741b70150aSStephen M. Cameron 		goto out;
2775283b4a9bSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
2776b7bb24ebSStephen M. Cameron 			VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
2777283b4a9bSStephen M. Cameron 	if (rc != 0)
2778283b4a9bSStephen M. Cameron 		goto out;
2779283b4a9bSStephen M. Cameron 
2780283b4a9bSStephen M. Cameron #define IOACCEL_STATUS_BYTE 4
2781283b4a9bSStephen M. Cameron #define OFFLOAD_CONFIGURED_BIT 0x01
2782283b4a9bSStephen M. Cameron #define OFFLOAD_ENABLED_BIT 0x02
2783283b4a9bSStephen M. Cameron 	ioaccel_status = buf[IOACCEL_STATUS_BYTE];
2784283b4a9bSStephen M. Cameron 	this_device->offload_config =
2785283b4a9bSStephen M. Cameron 		!!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
2786283b4a9bSStephen M. Cameron 	if (this_device->offload_config) {
2787283b4a9bSStephen M. Cameron 		this_device->offload_enabled =
2788283b4a9bSStephen M. Cameron 			!!(ioaccel_status & OFFLOAD_ENABLED_BIT);
2789283b4a9bSStephen M. Cameron 		if (hpsa_get_raid_map(h, scsi3addr, this_device))
2790283b4a9bSStephen M. Cameron 			this_device->offload_enabled = 0;
2791283b4a9bSStephen M. Cameron 	}
279241ce4c35SStephen Cameron 	this_device->offload_to_be_enabled = this_device->offload_enabled;
2793283b4a9bSStephen M. Cameron out:
2794283b4a9bSStephen M. Cameron 	kfree(buf);
2795283b4a9bSStephen M. Cameron 	return;
2796283b4a9bSStephen M. Cameron }
2797283b4a9bSStephen M. Cameron 
2798edd16368SStephen M. Cameron /* Get the device id from inquiry page 0x83 */
2799edd16368SStephen M. Cameron static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
2800edd16368SStephen M. Cameron 	unsigned char *device_id, int buflen)
2801edd16368SStephen M. Cameron {
2802edd16368SStephen M. Cameron 	int rc;
2803edd16368SStephen M. Cameron 	unsigned char *buf;
2804edd16368SStephen M. Cameron 
2805edd16368SStephen M. Cameron 	if (buflen > 16)
2806edd16368SStephen M. Cameron 		buflen = 16;
2807edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2808edd16368SStephen M. Cameron 	if (!buf)
2809a84d794dSStephen M. Cameron 		return -ENOMEM;
2810b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0x83, buf, 64);
2811edd16368SStephen M. Cameron 	if (rc == 0)
2812edd16368SStephen M. Cameron 		memcpy(device_id, &buf[8], buflen);
2813edd16368SStephen M. Cameron 	kfree(buf);
2814edd16368SStephen M. Cameron 	return rc != 0;
2815edd16368SStephen M. Cameron }
2816edd16368SStephen M. Cameron 
2817edd16368SStephen M. Cameron static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
281803383736SDon Brace 		void *buf, int bufsize,
2819edd16368SStephen M. Cameron 		int extended_response)
2820edd16368SStephen M. Cameron {
2821edd16368SStephen M. Cameron 	int rc = IO_OK;
2822edd16368SStephen M. Cameron 	struct CommandList *c;
2823edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
2824edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2825edd16368SStephen M. Cameron 
282645fcb86eSStephen Cameron 	c = cmd_alloc(h);
2827*bf43caf3SRobert Elliott 
2828e89c0ae7SStephen M. Cameron 	/* address the controller */
2829e89c0ae7SStephen M. Cameron 	memset(scsi3addr, 0, sizeof(scsi3addr));
2830a2dac136SStephen M. Cameron 	if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
2831a2dac136SStephen M. Cameron 		buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
2832a2dac136SStephen M. Cameron 		rc = -1;
2833a2dac136SStephen M. Cameron 		goto out;
2834a2dac136SStephen M. Cameron 	}
2835edd16368SStephen M. Cameron 	if (extended_response)
2836edd16368SStephen M. Cameron 		c->Request.CDB[1] = extended_response;
283725163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
283825163bd5SWebb Scales 					PCI_DMA_FROMDEVICE, NO_TIMEOUT);
283925163bd5SWebb Scales 	if (rc)
284025163bd5SWebb Scales 		goto out;
2841edd16368SStephen M. Cameron 	ei = c->err_info;
2842edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 &&
2843edd16368SStephen M. Cameron 	    ei->CommandStatus != CMD_DATA_UNDERRUN) {
2844d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2845edd16368SStephen M. Cameron 		rc = -1;
2846283b4a9bSStephen M. Cameron 	} else {
284703383736SDon Brace 		struct ReportLUNdata *rld = buf;
284803383736SDon Brace 
284903383736SDon Brace 		if (rld->extended_response_flag != extended_response) {
2850283b4a9bSStephen M. Cameron 			dev_err(&h->pdev->dev,
2851283b4a9bSStephen M. Cameron 				"report luns requested format %u, got %u\n",
2852283b4a9bSStephen M. Cameron 				extended_response,
285303383736SDon Brace 				rld->extended_response_flag);
2854283b4a9bSStephen M. Cameron 			rc = -1;
2855283b4a9bSStephen M. Cameron 		}
2856edd16368SStephen M. Cameron 	}
2857a2dac136SStephen M. Cameron out:
285845fcb86eSStephen Cameron 	cmd_free(h, c);
2859edd16368SStephen M. Cameron 	return rc;
2860edd16368SStephen M. Cameron }
2861edd16368SStephen M. Cameron 
2862edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
286303383736SDon Brace 		struct ReportExtendedLUNdata *buf, int bufsize)
2864edd16368SStephen M. Cameron {
286503383736SDon Brace 	return hpsa_scsi_do_report_luns(h, 0, buf, bufsize,
286603383736SDon Brace 						HPSA_REPORT_PHYS_EXTENDED);
2867edd16368SStephen M. Cameron }
2868edd16368SStephen M. Cameron 
2869edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
2870edd16368SStephen M. Cameron 		struct ReportLUNdata *buf, int bufsize)
2871edd16368SStephen M. Cameron {
2872edd16368SStephen M. Cameron 	return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
2873edd16368SStephen M. Cameron }
2874edd16368SStephen M. Cameron 
2875edd16368SStephen M. Cameron static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
2876edd16368SStephen M. Cameron 	int bus, int target, int lun)
2877edd16368SStephen M. Cameron {
2878edd16368SStephen M. Cameron 	device->bus = bus;
2879edd16368SStephen M. Cameron 	device->target = target;
2880edd16368SStephen M. Cameron 	device->lun = lun;
2881edd16368SStephen M. Cameron }
2882edd16368SStephen M. Cameron 
28839846590eSStephen M. Cameron /* Use VPD inquiry to get details of volume status */
28849846590eSStephen M. Cameron static int hpsa_get_volume_status(struct ctlr_info *h,
28859846590eSStephen M. Cameron 					unsigned char scsi3addr[])
28869846590eSStephen M. Cameron {
28879846590eSStephen M. Cameron 	int rc;
28889846590eSStephen M. Cameron 	int status;
28899846590eSStephen M. Cameron 	int size;
28909846590eSStephen M. Cameron 	unsigned char *buf;
28919846590eSStephen M. Cameron 
28929846590eSStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
28939846590eSStephen M. Cameron 	if (!buf)
28949846590eSStephen M. Cameron 		return HPSA_VPD_LV_STATUS_UNSUPPORTED;
28959846590eSStephen M. Cameron 
28969846590eSStephen M. Cameron 	/* Does controller have VPD for logical volume status? */
289724a4b078SStephen M. Cameron 	if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_STATUS))
28989846590eSStephen M. Cameron 		goto exit_failed;
28999846590eSStephen M. Cameron 
29009846590eSStephen M. Cameron 	/* Get the size of the VPD return buffer */
29019846590eSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
29029846590eSStephen M. Cameron 					buf, HPSA_VPD_HEADER_SZ);
290324a4b078SStephen M. Cameron 	if (rc != 0)
29049846590eSStephen M. Cameron 		goto exit_failed;
29059846590eSStephen M. Cameron 	size = buf[3];
29069846590eSStephen M. Cameron 
29079846590eSStephen M. Cameron 	/* Now get the whole VPD buffer */
29089846590eSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
29099846590eSStephen M. Cameron 					buf, size + HPSA_VPD_HEADER_SZ);
291024a4b078SStephen M. Cameron 	if (rc != 0)
29119846590eSStephen M. Cameron 		goto exit_failed;
29129846590eSStephen M. Cameron 	status = buf[4]; /* status byte */
29139846590eSStephen M. Cameron 
29149846590eSStephen M. Cameron 	kfree(buf);
29159846590eSStephen M. Cameron 	return status;
29169846590eSStephen M. Cameron exit_failed:
29179846590eSStephen M. Cameron 	kfree(buf);
29189846590eSStephen M. Cameron 	return HPSA_VPD_LV_STATUS_UNSUPPORTED;
29199846590eSStephen M. Cameron }
29209846590eSStephen M. Cameron 
29219846590eSStephen M. Cameron /* Determine offline status of a volume.
29229846590eSStephen M. Cameron  * Return either:
29239846590eSStephen M. Cameron  *  0 (not offline)
292467955ba3SStephen M. Cameron  *  0xff (offline for unknown reasons)
29259846590eSStephen M. Cameron  *  # (integer code indicating one of several NOT READY states
29269846590eSStephen M. Cameron  *     describing why a volume is to be kept offline)
29279846590eSStephen M. Cameron  */
292867955ba3SStephen M. Cameron static int hpsa_volume_offline(struct ctlr_info *h,
29299846590eSStephen M. Cameron 					unsigned char scsi3addr[])
29309846590eSStephen M. Cameron {
29319846590eSStephen M. Cameron 	struct CommandList *c;
29329437ac43SStephen Cameron 	unsigned char *sense;
29339437ac43SStephen Cameron 	u8 sense_key, asc, ascq;
29349437ac43SStephen Cameron 	int sense_len;
293525163bd5SWebb Scales 	int rc, ldstat = 0;
29369846590eSStephen M. Cameron 	u16 cmd_status;
29379846590eSStephen M. Cameron 	u8 scsi_status;
29389846590eSStephen M. Cameron #define ASC_LUN_NOT_READY 0x04
29399846590eSStephen M. Cameron #define ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS 0x04
29409846590eSStephen M. Cameron #define ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ 0x02
29419846590eSStephen M. Cameron 
29429846590eSStephen M. Cameron 	c = cmd_alloc(h);
2943*bf43caf3SRobert Elliott 
29449846590eSStephen M. Cameron 	(void) fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, scsi3addr, TYPE_CMD);
294525163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
294625163bd5SWebb Scales 	if (rc) {
294725163bd5SWebb Scales 		cmd_free(h, c);
294825163bd5SWebb Scales 		return 0;
294925163bd5SWebb Scales 	}
29509846590eSStephen M. Cameron 	sense = c->err_info->SenseInfo;
29519437ac43SStephen Cameron 	if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
29529437ac43SStephen Cameron 		sense_len = sizeof(c->err_info->SenseInfo);
29539437ac43SStephen Cameron 	else
29549437ac43SStephen Cameron 		sense_len = c->err_info->SenseLen;
29559437ac43SStephen Cameron 	decode_sense_data(sense, sense_len, &sense_key, &asc, &ascq);
29569846590eSStephen M. Cameron 	cmd_status = c->err_info->CommandStatus;
29579846590eSStephen M. Cameron 	scsi_status = c->err_info->ScsiStatus;
29589846590eSStephen M. Cameron 	cmd_free(h, c);
29599846590eSStephen M. Cameron 	/* Is the volume 'not ready'? */
29609846590eSStephen M. Cameron 	if (cmd_status != CMD_TARGET_STATUS ||
29619846590eSStephen M. Cameron 		scsi_status != SAM_STAT_CHECK_CONDITION ||
29629846590eSStephen M. Cameron 		sense_key != NOT_READY ||
29639846590eSStephen M. Cameron 		asc != ASC_LUN_NOT_READY)  {
29649846590eSStephen M. Cameron 		return 0;
29659846590eSStephen M. Cameron 	}
29669846590eSStephen M. Cameron 
29679846590eSStephen M. Cameron 	/* Determine the reason for not ready state */
29689846590eSStephen M. Cameron 	ldstat = hpsa_get_volume_status(h, scsi3addr);
29699846590eSStephen M. Cameron 
29709846590eSStephen M. Cameron 	/* Keep volume offline in certain cases: */
29719846590eSStephen M. Cameron 	switch (ldstat) {
29729846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ERASE:
29739846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_RPI:
29749846590eSStephen M. Cameron 	case HPSA_LV_PENDING_RPI:
29759846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_NO_KEY:
29769846590eSStephen M. Cameron 	case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
29779846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION:
29789846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
29799846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
29809846590eSStephen M. Cameron 		return ldstat;
29819846590eSStephen M. Cameron 	case HPSA_VPD_LV_STATUS_UNSUPPORTED:
29829846590eSStephen M. Cameron 		/* If VPD status page isn't available,
29839846590eSStephen M. Cameron 		 * use ASC/ASCQ to determine state
29849846590eSStephen M. Cameron 		 */
29859846590eSStephen M. Cameron 		if ((ascq == ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS) ||
29869846590eSStephen M. Cameron 			(ascq == ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ))
29879846590eSStephen M. Cameron 			return ldstat;
29889846590eSStephen M. Cameron 		break;
29899846590eSStephen M. Cameron 	default:
29909846590eSStephen M. Cameron 		break;
29919846590eSStephen M. Cameron 	}
29929846590eSStephen M. Cameron 	return 0;
29939846590eSStephen M. Cameron }
29949846590eSStephen M. Cameron 
29959b5c48c2SStephen Cameron /*
29969b5c48c2SStephen Cameron  * Find out if a logical device supports aborts by simply trying one.
29979b5c48c2SStephen Cameron  * Smart Array may claim not to support aborts on logical drives, but
29989b5c48c2SStephen Cameron  * if a MSA2000 * is connected, the drives on that will be presented
29999b5c48c2SStephen Cameron  * by the Smart Array as logical drives, and aborts may be sent to
30009b5c48c2SStephen Cameron  * those devices successfully.  So the simplest way to find out is
30019b5c48c2SStephen Cameron  * to simply try an abort and see how the device responds.
30029b5c48c2SStephen Cameron  */
30039b5c48c2SStephen Cameron static int hpsa_device_supports_aborts(struct ctlr_info *h,
30049b5c48c2SStephen Cameron 					unsigned char *scsi3addr)
30059b5c48c2SStephen Cameron {
30069b5c48c2SStephen Cameron 	struct CommandList *c;
30079b5c48c2SStephen Cameron 	struct ErrorInfo *ei;
30089b5c48c2SStephen Cameron 	int rc = 0;
30099b5c48c2SStephen Cameron 
30109b5c48c2SStephen Cameron 	u64 tag = (u64) -1; /* bogus tag */
30119b5c48c2SStephen Cameron 
30129b5c48c2SStephen Cameron 	/* Assume that physical devices support aborts */
30139b5c48c2SStephen Cameron 	if (!is_logical_dev_addr_mode(scsi3addr))
30149b5c48c2SStephen Cameron 		return 1;
30159b5c48c2SStephen Cameron 
30169b5c48c2SStephen Cameron 	c = cmd_alloc(h);
3017*bf43caf3SRobert Elliott 
30189b5c48c2SStephen Cameron 	(void) fill_cmd(c, HPSA_ABORT_MSG, h, &tag, 0, 0, scsi3addr, TYPE_MSG);
30199b5c48c2SStephen Cameron 	(void) hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
30209b5c48c2SStephen Cameron 	/* no unmap needed here because no data xfer. */
30219b5c48c2SStephen Cameron 	ei = c->err_info;
30229b5c48c2SStephen Cameron 	switch (ei->CommandStatus) {
30239b5c48c2SStephen Cameron 	case CMD_INVALID:
30249b5c48c2SStephen Cameron 		rc = 0;
30259b5c48c2SStephen Cameron 		break;
30269b5c48c2SStephen Cameron 	case CMD_UNABORTABLE:
30279b5c48c2SStephen Cameron 	case CMD_ABORT_FAILED:
30289b5c48c2SStephen Cameron 		rc = 1;
30299b5c48c2SStephen Cameron 		break;
30309437ac43SStephen Cameron 	case CMD_TMF_STATUS:
30319437ac43SStephen Cameron 		rc = hpsa_evaluate_tmf_status(h, c);
30329437ac43SStephen Cameron 		break;
30339b5c48c2SStephen Cameron 	default:
30349b5c48c2SStephen Cameron 		rc = 0;
30359b5c48c2SStephen Cameron 		break;
30369b5c48c2SStephen Cameron 	}
30379b5c48c2SStephen Cameron 	cmd_free(h, c);
30389b5c48c2SStephen Cameron 	return rc;
30399b5c48c2SStephen Cameron }
30409b5c48c2SStephen Cameron 
3041edd16368SStephen M. Cameron static int hpsa_update_device_info(struct ctlr_info *h,
30420b0e1d6cSStephen M. Cameron 	unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
30430b0e1d6cSStephen M. Cameron 	unsigned char *is_OBDR_device)
3044edd16368SStephen M. Cameron {
30450b0e1d6cSStephen M. Cameron 
30460b0e1d6cSStephen M. Cameron #define OBDR_SIG_OFFSET 43
30470b0e1d6cSStephen M. Cameron #define OBDR_TAPE_SIG "$DR-10"
30480b0e1d6cSStephen M. Cameron #define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
30490b0e1d6cSStephen M. Cameron #define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
30500b0e1d6cSStephen M. Cameron 
3051ea6d3bc3SStephen M. Cameron 	unsigned char *inq_buff;
30520b0e1d6cSStephen M. Cameron 	unsigned char *obdr_sig;
3053edd16368SStephen M. Cameron 
3054ea6d3bc3SStephen M. Cameron 	inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
3055edd16368SStephen M. Cameron 	if (!inq_buff)
3056edd16368SStephen M. Cameron 		goto bail_out;
3057edd16368SStephen M. Cameron 
3058edd16368SStephen M. Cameron 	/* Do an inquiry to the device to see what it is. */
3059edd16368SStephen M. Cameron 	if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
3060edd16368SStephen M. Cameron 		(unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
3061edd16368SStephen M. Cameron 		/* Inquiry failed (msg printed already) */
3062edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev,
3063edd16368SStephen M. Cameron 			"hpsa_update_device_info: inquiry failed\n");
3064edd16368SStephen M. Cameron 		goto bail_out;
3065edd16368SStephen M. Cameron 	}
3066edd16368SStephen M. Cameron 
3067edd16368SStephen M. Cameron 	this_device->devtype = (inq_buff[0] & 0x1f);
3068edd16368SStephen M. Cameron 	memcpy(this_device->scsi3addr, scsi3addr, 8);
3069edd16368SStephen M. Cameron 	memcpy(this_device->vendor, &inq_buff[8],
3070edd16368SStephen M. Cameron 		sizeof(this_device->vendor));
3071edd16368SStephen M. Cameron 	memcpy(this_device->model, &inq_buff[16],
3072edd16368SStephen M. Cameron 		sizeof(this_device->model));
3073edd16368SStephen M. Cameron 	memset(this_device->device_id, 0,
3074edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
3075edd16368SStephen M. Cameron 	hpsa_get_device_id(h, scsi3addr, this_device->device_id,
3076edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
3077edd16368SStephen M. Cameron 
3078edd16368SStephen M. Cameron 	if (this_device->devtype == TYPE_DISK &&
3079283b4a9bSStephen M. Cameron 		is_logical_dev_addr_mode(scsi3addr)) {
308067955ba3SStephen M. Cameron 		int volume_offline;
308167955ba3SStephen M. Cameron 
3082edd16368SStephen M. Cameron 		hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
3083283b4a9bSStephen M. Cameron 		if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
3084283b4a9bSStephen M. Cameron 			hpsa_get_ioaccel_status(h, scsi3addr, this_device);
308567955ba3SStephen M. Cameron 		volume_offline = hpsa_volume_offline(h, scsi3addr);
308667955ba3SStephen M. Cameron 		if (volume_offline < 0 || volume_offline > 0xff)
308767955ba3SStephen M. Cameron 			volume_offline = HPSA_VPD_LV_STATUS_UNSUPPORTED;
308867955ba3SStephen M. Cameron 		this_device->volume_offline = volume_offline & 0xff;
3089283b4a9bSStephen M. Cameron 	} else {
3090edd16368SStephen M. Cameron 		this_device->raid_level = RAID_UNKNOWN;
3091283b4a9bSStephen M. Cameron 		this_device->offload_config = 0;
3092283b4a9bSStephen M. Cameron 		this_device->offload_enabled = 0;
309341ce4c35SStephen Cameron 		this_device->offload_to_be_enabled = 0;
3094a3144e0bSJoe Handzik 		this_device->hba_ioaccel_enabled = 0;
30959846590eSStephen M. Cameron 		this_device->volume_offline = 0;
309603383736SDon Brace 		this_device->queue_depth = h->nr_cmds;
3097283b4a9bSStephen M. Cameron 	}
3098edd16368SStephen M. Cameron 
30990b0e1d6cSStephen M. Cameron 	if (is_OBDR_device) {
31000b0e1d6cSStephen M. Cameron 		/* See if this is a One-Button-Disaster-Recovery device
31010b0e1d6cSStephen M. Cameron 		 * by looking for "$DR-10" at offset 43 in inquiry data.
31020b0e1d6cSStephen M. Cameron 		 */
31030b0e1d6cSStephen M. Cameron 		obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
31040b0e1d6cSStephen M. Cameron 		*is_OBDR_device = (this_device->devtype == TYPE_ROM &&
31050b0e1d6cSStephen M. Cameron 					strncmp(obdr_sig, OBDR_TAPE_SIG,
31060b0e1d6cSStephen M. Cameron 						OBDR_SIG_LEN) == 0);
31070b0e1d6cSStephen M. Cameron 	}
3108edd16368SStephen M. Cameron 	kfree(inq_buff);
3109edd16368SStephen M. Cameron 	return 0;
3110edd16368SStephen M. Cameron 
3111edd16368SStephen M. Cameron bail_out:
3112edd16368SStephen M. Cameron 	kfree(inq_buff);
3113edd16368SStephen M. Cameron 	return 1;
3114edd16368SStephen M. Cameron }
3115edd16368SStephen M. Cameron 
31169b5c48c2SStephen Cameron static void hpsa_update_device_supports_aborts(struct ctlr_info *h,
31179b5c48c2SStephen Cameron 			struct hpsa_scsi_dev_t *dev, u8 *scsi3addr)
31189b5c48c2SStephen Cameron {
31199b5c48c2SStephen Cameron 	unsigned long flags;
31209b5c48c2SStephen Cameron 	int rc, entry;
31219b5c48c2SStephen Cameron 	/*
31229b5c48c2SStephen Cameron 	 * See if this device supports aborts.  If we already know
31239b5c48c2SStephen Cameron 	 * the device, we already know if it supports aborts, otherwise
31249b5c48c2SStephen Cameron 	 * we have to find out if it supports aborts by trying one.
31259b5c48c2SStephen Cameron 	 */
31269b5c48c2SStephen Cameron 	spin_lock_irqsave(&h->devlock, flags);
31279b5c48c2SStephen Cameron 	rc = hpsa_scsi_find_entry(dev, h->dev, h->ndevices, &entry);
31289b5c48c2SStephen Cameron 	if ((rc == DEVICE_SAME || rc == DEVICE_UPDATED) &&
31299b5c48c2SStephen Cameron 		entry >= 0 && entry < h->ndevices) {
31309b5c48c2SStephen Cameron 		dev->supports_aborts = h->dev[entry]->supports_aborts;
31319b5c48c2SStephen Cameron 		spin_unlock_irqrestore(&h->devlock, flags);
31329b5c48c2SStephen Cameron 	} else {
31339b5c48c2SStephen Cameron 		spin_unlock_irqrestore(&h->devlock, flags);
31349b5c48c2SStephen Cameron 		dev->supports_aborts =
31359b5c48c2SStephen Cameron 				hpsa_device_supports_aborts(h, scsi3addr);
31369b5c48c2SStephen Cameron 		if (dev->supports_aborts < 0)
31379b5c48c2SStephen Cameron 			dev->supports_aborts = 0;
31389b5c48c2SStephen Cameron 	}
31399b5c48c2SStephen Cameron }
31409b5c48c2SStephen Cameron 
31414f4eb9f1SScott Teel static unsigned char *ext_target_model[] = {
3142edd16368SStephen M. Cameron 	"MSA2012",
3143edd16368SStephen M. Cameron 	"MSA2024",
3144edd16368SStephen M. Cameron 	"MSA2312",
3145edd16368SStephen M. Cameron 	"MSA2324",
3146fda38518SStephen M. Cameron 	"P2000 G3 SAS",
3147e06c8e5cSStephen M. Cameron 	"MSA 2040 SAS",
3148edd16368SStephen M. Cameron 	NULL,
3149edd16368SStephen M. Cameron };
3150edd16368SStephen M. Cameron 
31514f4eb9f1SScott Teel static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
3152edd16368SStephen M. Cameron {
3153edd16368SStephen M. Cameron 	int i;
3154edd16368SStephen M. Cameron 
31554f4eb9f1SScott Teel 	for (i = 0; ext_target_model[i]; i++)
31564f4eb9f1SScott Teel 		if (strncmp(device->model, ext_target_model[i],
31574f4eb9f1SScott Teel 			strlen(ext_target_model[i])) == 0)
3158edd16368SStephen M. Cameron 			return 1;
3159edd16368SStephen M. Cameron 	return 0;
3160edd16368SStephen M. Cameron }
3161edd16368SStephen M. Cameron 
3162edd16368SStephen M. Cameron /* Helper function to assign bus, target, lun mapping of devices.
31634f4eb9f1SScott Teel  * Puts non-external target logical volumes on bus 0, external target logical
3164edd16368SStephen M. Cameron  * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
3165edd16368SStephen M. Cameron  * Logical drive target and lun are assigned at this time, but
3166edd16368SStephen M. Cameron  * physical device lun and target assignment are deferred (assigned
3167edd16368SStephen M. Cameron  * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
3168edd16368SStephen M. Cameron  */
3169edd16368SStephen M. Cameron static void figure_bus_target_lun(struct ctlr_info *h,
31701f310bdeSStephen M. Cameron 	u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
3171edd16368SStephen M. Cameron {
31721f310bdeSStephen M. Cameron 	u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
3173edd16368SStephen M. Cameron 
31741f310bdeSStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes)) {
31751f310bdeSStephen M. Cameron 		/* physical device, target and lun filled in later */
31761f310bdeSStephen M. Cameron 		if (is_hba_lunid(lunaddrbytes))
31771f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
31781f310bdeSStephen M. Cameron 		else
31791f310bdeSStephen M. Cameron 			/* defer target, lun assignment for physical devices */
31801f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 2, -1, -1);
31811f310bdeSStephen M. Cameron 		return;
31821f310bdeSStephen M. Cameron 	}
31831f310bdeSStephen M. Cameron 	/* It's a logical device */
31844f4eb9f1SScott Teel 	if (is_ext_target(h, device)) {
31854f4eb9f1SScott Teel 		/* external target way, put logicals on bus 1
3186339b2b14SStephen M. Cameron 		 * and match target/lun numbers box
31871f310bdeSStephen M. Cameron 		 * reports, other smart array, bus 0, target 0, match lunid
3188339b2b14SStephen M. Cameron 		 */
31891f310bdeSStephen M. Cameron 		hpsa_set_bus_target_lun(device,
31901f310bdeSStephen M. Cameron 			1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
31911f310bdeSStephen M. Cameron 		return;
3192339b2b14SStephen M. Cameron 	}
31931f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
3194edd16368SStephen M. Cameron }
3195edd16368SStephen M. Cameron 
3196edd16368SStephen M. Cameron /*
3197edd16368SStephen M. Cameron  * If there is no lun 0 on a target, linux won't find any devices.
31984f4eb9f1SScott Teel  * For the external targets (arrays), we have to manually detect the enclosure
3199edd16368SStephen M. Cameron  * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
3200edd16368SStephen M. Cameron  * it for some reason.  *tmpdevice is the target we're adding,
3201edd16368SStephen M. Cameron  * this_device is a pointer into the current element of currentsd[]
3202edd16368SStephen M. Cameron  * that we're building up in update_scsi_devices(), below.
3203edd16368SStephen M. Cameron  * lunzerobits is a bitmap that tracks which targets already have a
3204edd16368SStephen M. Cameron  * lun 0 assigned.
3205edd16368SStephen M. Cameron  * Returns 1 if an enclosure was added, 0 if not.
3206edd16368SStephen M. Cameron  */
32074f4eb9f1SScott Teel static int add_ext_target_dev(struct ctlr_info *h,
3208edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *tmpdevice,
320901a02ffcSStephen M. Cameron 	struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
32104f4eb9f1SScott Teel 	unsigned long lunzerobits[], int *n_ext_target_devs)
3211edd16368SStephen M. Cameron {
3212edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
3213edd16368SStephen M. Cameron 
32141f310bdeSStephen M. Cameron 	if (test_bit(tmpdevice->target, lunzerobits))
3215edd16368SStephen M. Cameron 		return 0; /* There is already a lun 0 on this target. */
3216edd16368SStephen M. Cameron 
3217edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes))
3218edd16368SStephen M. Cameron 		return 0; /* It's the logical targets that may lack lun 0. */
3219edd16368SStephen M. Cameron 
32204f4eb9f1SScott Teel 	if (!is_ext_target(h, tmpdevice))
32214f4eb9f1SScott Teel 		return 0; /* Only external target devices have this problem. */
3222edd16368SStephen M. Cameron 
32231f310bdeSStephen M. Cameron 	if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
3224edd16368SStephen M. Cameron 		return 0;
3225edd16368SStephen M. Cameron 
3226c4f8a299SStephen M. Cameron 	memset(scsi3addr, 0, 8);
32271f310bdeSStephen M. Cameron 	scsi3addr[3] = tmpdevice->target;
3228edd16368SStephen M. Cameron 	if (is_hba_lunid(scsi3addr))
3229edd16368SStephen M. Cameron 		return 0; /* Don't add the RAID controller here. */
3230edd16368SStephen M. Cameron 
3231339b2b14SStephen M. Cameron 	if (is_scsi_rev_5(h))
3232339b2b14SStephen M. Cameron 		return 0; /* p1210m doesn't need to do this. */
3233339b2b14SStephen M. Cameron 
32344f4eb9f1SScott Teel 	if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
3235aca4a520SScott Teel 		dev_warn(&h->pdev->dev, "Maximum number of external "
3236aca4a520SScott Teel 			"target devices exceeded.  Check your hardware "
3237edd16368SStephen M. Cameron 			"configuration.");
3238edd16368SStephen M. Cameron 		return 0;
3239edd16368SStephen M. Cameron 	}
3240edd16368SStephen M. Cameron 
32410b0e1d6cSStephen M. Cameron 	if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
3242edd16368SStephen M. Cameron 		return 0;
32434f4eb9f1SScott Teel 	(*n_ext_target_devs)++;
32441f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(this_device,
32451f310bdeSStephen M. Cameron 				tmpdevice->bus, tmpdevice->target, 0);
32469b5c48c2SStephen Cameron 	hpsa_update_device_supports_aborts(h, this_device, scsi3addr);
32471f310bdeSStephen M. Cameron 	set_bit(tmpdevice->target, lunzerobits);
3248edd16368SStephen M. Cameron 	return 1;
3249edd16368SStephen M. Cameron }
3250edd16368SStephen M. Cameron 
3251edd16368SStephen M. Cameron /*
325254b6e9e9SScott Teel  * Get address of physical disk used for an ioaccel2 mode command:
325354b6e9e9SScott Teel  *	1. Extract ioaccel2 handle from the command.
325454b6e9e9SScott Teel  *	2. Find a matching ioaccel2 handle from list of physical disks.
325554b6e9e9SScott Teel  *	3. Return:
325654b6e9e9SScott Teel  *		1 and set scsi3addr to address of matching physical
325754b6e9e9SScott Teel  *		0 if no matching physical disk was found.
325854b6e9e9SScott Teel  */
325954b6e9e9SScott Teel static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
326054b6e9e9SScott Teel 	struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
326154b6e9e9SScott Teel {
326241ce4c35SStephen Cameron 	struct io_accel2_cmd *c2 =
326341ce4c35SStephen Cameron 			&h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
326441ce4c35SStephen Cameron 	unsigned long flags;
326554b6e9e9SScott Teel 	int i;
326654b6e9e9SScott Teel 
326741ce4c35SStephen Cameron 	spin_lock_irqsave(&h->devlock, flags);
326841ce4c35SStephen Cameron 	for (i = 0; i < h->ndevices; i++)
326941ce4c35SStephen Cameron 		if (h->dev[i]->ioaccel_handle == le32_to_cpu(c2->scsi_nexus)) {
327041ce4c35SStephen Cameron 			memcpy(scsi3addr, h->dev[i]->scsi3addr,
327141ce4c35SStephen Cameron 				sizeof(h->dev[i]->scsi3addr));
327241ce4c35SStephen Cameron 			spin_unlock_irqrestore(&h->devlock, flags);
327354b6e9e9SScott Teel 			return 1;
327454b6e9e9SScott Teel 		}
327541ce4c35SStephen Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
327641ce4c35SStephen Cameron 	return 0;
327741ce4c35SStephen Cameron }
327841ce4c35SStephen Cameron 
327954b6e9e9SScott Teel /*
3280edd16368SStephen M. Cameron  * Do CISS_REPORT_PHYS and CISS_REPORT_LOG.  Data is returned in physdev,
3281edd16368SStephen M. Cameron  * logdev.  The number of luns in physdev and logdev are returned in
3282edd16368SStephen M. Cameron  * *nphysicals and *nlogicals, respectively.
3283edd16368SStephen M. Cameron  * Returns 0 on success, -1 otherwise.
3284edd16368SStephen M. Cameron  */
3285edd16368SStephen M. Cameron static int hpsa_gather_lun_info(struct ctlr_info *h,
328603383736SDon Brace 	struct ReportExtendedLUNdata *physdev, u32 *nphysicals,
328701a02ffcSStephen M. Cameron 	struct ReportLUNdata *logdev, u32 *nlogicals)
3288edd16368SStephen M. Cameron {
328903383736SDon Brace 	if (hpsa_scsi_do_report_phys_luns(h, physdev, sizeof(*physdev))) {
3290edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
3291edd16368SStephen M. Cameron 		return -1;
3292edd16368SStephen M. Cameron 	}
329303383736SDon Brace 	*nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 24;
3294edd16368SStephen M. Cameron 	if (*nphysicals > HPSA_MAX_PHYS_LUN) {
329503383736SDon Brace 		dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded. %d LUNs ignored.\n",
329603383736SDon Brace 			HPSA_MAX_PHYS_LUN, *nphysicals - HPSA_MAX_PHYS_LUN);
3297edd16368SStephen M. Cameron 		*nphysicals = HPSA_MAX_PHYS_LUN;
3298edd16368SStephen M. Cameron 	}
329903383736SDon Brace 	if (hpsa_scsi_do_report_log_luns(h, logdev, sizeof(*logdev))) {
3300edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
3301edd16368SStephen M. Cameron 		return -1;
3302edd16368SStephen M. Cameron 	}
33036df1e954SStephen M. Cameron 	*nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
3304edd16368SStephen M. Cameron 	/* Reject Logicals in excess of our max capability. */
3305edd16368SStephen M. Cameron 	if (*nlogicals > HPSA_MAX_LUN) {
3306edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
3307edd16368SStephen M. Cameron 			"maximum logical LUNs (%d) exceeded.  "
3308edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_LUN,
3309edd16368SStephen M. Cameron 			*nlogicals - HPSA_MAX_LUN);
3310edd16368SStephen M. Cameron 			*nlogicals = HPSA_MAX_LUN;
3311edd16368SStephen M. Cameron 	}
3312edd16368SStephen M. Cameron 	if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
3313edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
3314edd16368SStephen M. Cameron 			"maximum logical + physical LUNs (%d) exceeded. "
3315edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
3316edd16368SStephen M. Cameron 			*nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
3317edd16368SStephen M. Cameron 		*nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
3318edd16368SStephen M. Cameron 	}
3319edd16368SStephen M. Cameron 	return 0;
3320edd16368SStephen M. Cameron }
3321edd16368SStephen M. Cameron 
332242a91641SDon Brace static u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position,
332342a91641SDon Brace 	int i, int nphysicals, int nlogicals,
3324a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list,
3325339b2b14SStephen M. Cameron 	struct ReportLUNdata *logdev_list)
3326339b2b14SStephen M. Cameron {
3327339b2b14SStephen M. Cameron 	/* Helper function, figure out where the LUN ID info is coming from
3328339b2b14SStephen M. Cameron 	 * given index i, lists of physical and logical devices, where in
3329339b2b14SStephen M. Cameron 	 * the list the raid controller is supposed to appear (first or last)
3330339b2b14SStephen M. Cameron 	 */
3331339b2b14SStephen M. Cameron 
3332339b2b14SStephen M. Cameron 	int logicals_start = nphysicals + (raid_ctlr_position == 0);
3333339b2b14SStephen M. Cameron 	int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
3334339b2b14SStephen M. Cameron 
3335339b2b14SStephen M. Cameron 	if (i == raid_ctlr_position)
3336339b2b14SStephen M. Cameron 		return RAID_CTLR_LUNID;
3337339b2b14SStephen M. Cameron 
3338339b2b14SStephen M. Cameron 	if (i < logicals_start)
3339d5b5d964SStephen M. Cameron 		return &physdev_list->LUN[i -
3340d5b5d964SStephen M. Cameron 				(raid_ctlr_position == 0)].lunid[0];
3341339b2b14SStephen M. Cameron 
3342339b2b14SStephen M. Cameron 	if (i < last_device)
3343339b2b14SStephen M. Cameron 		return &logdev_list->LUN[i - nphysicals -
3344339b2b14SStephen M. Cameron 			(raid_ctlr_position == 0)][0];
3345339b2b14SStephen M. Cameron 	BUG();
3346339b2b14SStephen M. Cameron 	return NULL;
3347339b2b14SStephen M. Cameron }
3348339b2b14SStephen M. Cameron 
3349316b221aSStephen M. Cameron static int hpsa_hba_mode_enabled(struct ctlr_info *h)
3350316b221aSStephen M. Cameron {
3351316b221aSStephen M. Cameron 	int rc;
33526e8e8088SJoe Handzik 	int hba_mode_enabled;
3353316b221aSStephen M. Cameron 	struct bmic_controller_parameters *ctlr_params;
3354316b221aSStephen M. Cameron 	ctlr_params = kzalloc(sizeof(struct bmic_controller_parameters),
3355316b221aSStephen M. Cameron 		GFP_KERNEL);
3356316b221aSStephen M. Cameron 
3357316b221aSStephen M. Cameron 	if (!ctlr_params)
335896444fbbSJoe Handzik 		return -ENOMEM;
3359316b221aSStephen M. Cameron 	rc = hpsa_bmic_ctrl_mode_sense(h, RAID_CTLR_LUNID, 0, ctlr_params,
3360316b221aSStephen M. Cameron 		sizeof(struct bmic_controller_parameters));
336196444fbbSJoe Handzik 	if (rc) {
3362316b221aSStephen M. Cameron 		kfree(ctlr_params);
336396444fbbSJoe Handzik 		return rc;
3364316b221aSStephen M. Cameron 	}
33656e8e8088SJoe Handzik 
33666e8e8088SJoe Handzik 	hba_mode_enabled =
33676e8e8088SJoe Handzik 		((ctlr_params->nvram_flags & HBA_MODE_ENABLED_FLAG) != 0);
33686e8e8088SJoe Handzik 	kfree(ctlr_params);
33696e8e8088SJoe Handzik 	return hba_mode_enabled;
3370316b221aSStephen M. Cameron }
3371316b221aSStephen M. Cameron 
337203383736SDon Brace /* get physical drive ioaccel handle and queue depth */
337303383736SDon Brace static void hpsa_get_ioaccel_drive_info(struct ctlr_info *h,
337403383736SDon Brace 		struct hpsa_scsi_dev_t *dev,
337503383736SDon Brace 		u8 *lunaddrbytes,
337603383736SDon Brace 		struct bmic_identify_physical_device *id_phys)
337703383736SDon Brace {
337803383736SDon Brace 	int rc;
337903383736SDon Brace 	struct ext_report_lun_entry *rle =
338003383736SDon Brace 		(struct ext_report_lun_entry *) lunaddrbytes;
338103383736SDon Brace 
338203383736SDon Brace 	dev->ioaccel_handle = rle->ioaccel_handle;
3383a3144e0bSJoe Handzik 	if (PHYS_IOACCEL(lunaddrbytes) && dev->ioaccel_handle)
3384a3144e0bSJoe Handzik 		dev->hba_ioaccel_enabled = 1;
338503383736SDon Brace 	memset(id_phys, 0, sizeof(*id_phys));
338603383736SDon Brace 	rc = hpsa_bmic_id_physical_device(h, lunaddrbytes,
338703383736SDon Brace 			GET_BMIC_DRIVE_NUMBER(lunaddrbytes), id_phys,
338803383736SDon Brace 			sizeof(*id_phys));
338903383736SDon Brace 	if (!rc)
339003383736SDon Brace 		/* Reserve space for FW operations */
339103383736SDon Brace #define DRIVE_CMDS_RESERVED_FOR_FW 2
339203383736SDon Brace #define DRIVE_QUEUE_DEPTH 7
339303383736SDon Brace 		dev->queue_depth =
339403383736SDon Brace 			le16_to_cpu(id_phys->current_queue_depth_limit) -
339503383736SDon Brace 				DRIVE_CMDS_RESERVED_FOR_FW;
339603383736SDon Brace 	else
339703383736SDon Brace 		dev->queue_depth = DRIVE_QUEUE_DEPTH; /* conservative */
339803383736SDon Brace 	atomic_set(&dev->ioaccel_cmds_out, 0);
339903383736SDon Brace }
340003383736SDon Brace 
3401edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
3402edd16368SStephen M. Cameron {
3403edd16368SStephen M. Cameron 	/* the idea here is we could get notified
3404edd16368SStephen M. Cameron 	 * that some devices have changed, so we do a report
3405edd16368SStephen M. Cameron 	 * physical luns and report logical luns cmd, and adjust
3406edd16368SStephen M. Cameron 	 * our list of devices accordingly.
3407edd16368SStephen M. Cameron 	 *
3408edd16368SStephen M. Cameron 	 * The scsi3addr's of devices won't change so long as the
3409edd16368SStephen M. Cameron 	 * adapter is not reset.  That means we can rescan and
3410edd16368SStephen M. Cameron 	 * tell which devices we already know about, vs. new
3411edd16368SStephen M. Cameron 	 * devices, vs.  disappearing devices.
3412edd16368SStephen M. Cameron 	 */
3413a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list = NULL;
3414edd16368SStephen M. Cameron 	struct ReportLUNdata *logdev_list = NULL;
341503383736SDon Brace 	struct bmic_identify_physical_device *id_phys = NULL;
341601a02ffcSStephen M. Cameron 	u32 nphysicals = 0;
341701a02ffcSStephen M. Cameron 	u32 nlogicals = 0;
341801a02ffcSStephen M. Cameron 	u32 ndev_allocated = 0;
3419edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
3420edd16368SStephen M. Cameron 	int ncurrent = 0;
34214f4eb9f1SScott Teel 	int i, n_ext_target_devs, ndevs_to_allocate;
3422339b2b14SStephen M. Cameron 	int raid_ctlr_position;
34232bbf5c7fSJoe Handzik 	int rescan_hba_mode;
3424aca4a520SScott Teel 	DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
3425edd16368SStephen M. Cameron 
3426cfe5badcSScott Teel 	currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
342792084715SStephen M. Cameron 	physdev_list = kzalloc(sizeof(*physdev_list), GFP_KERNEL);
342892084715SStephen M. Cameron 	logdev_list = kzalloc(sizeof(*logdev_list), GFP_KERNEL);
3429edd16368SStephen M. Cameron 	tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
343003383736SDon Brace 	id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
3431edd16368SStephen M. Cameron 
343203383736SDon Brace 	if (!currentsd || !physdev_list || !logdev_list ||
343303383736SDon Brace 		!tmpdevice || !id_phys) {
3434edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory\n");
3435edd16368SStephen M. Cameron 		goto out;
3436edd16368SStephen M. Cameron 	}
3437edd16368SStephen M. Cameron 	memset(lunzerobits, 0, sizeof(lunzerobits));
3438edd16368SStephen M. Cameron 
3439316b221aSStephen M. Cameron 	rescan_hba_mode = hpsa_hba_mode_enabled(h);
344096444fbbSJoe Handzik 	if (rescan_hba_mode < 0)
344196444fbbSJoe Handzik 		goto out;
3442316b221aSStephen M. Cameron 
3443316b221aSStephen M. Cameron 	if (!h->hba_mode_enabled && rescan_hba_mode)
3444316b221aSStephen M. Cameron 		dev_warn(&h->pdev->dev, "HBA mode enabled\n");
3445316b221aSStephen M. Cameron 	else if (h->hba_mode_enabled && !rescan_hba_mode)
3446316b221aSStephen M. Cameron 		dev_warn(&h->pdev->dev, "HBA mode disabled\n");
3447316b221aSStephen M. Cameron 
3448316b221aSStephen M. Cameron 	h->hba_mode_enabled = rescan_hba_mode;
3449316b221aSStephen M. Cameron 
345003383736SDon Brace 	if (hpsa_gather_lun_info(h, physdev_list, &nphysicals,
345103383736SDon Brace 			logdev_list, &nlogicals))
3452edd16368SStephen M. Cameron 		goto out;
3453edd16368SStephen M. Cameron 
3454aca4a520SScott Teel 	/* We might see up to the maximum number of logical and physical disks
3455aca4a520SScott Teel 	 * plus external target devices, and a device for the local RAID
3456aca4a520SScott Teel 	 * controller.
3457edd16368SStephen M. Cameron 	 */
3458aca4a520SScott Teel 	ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
3459edd16368SStephen M. Cameron 
3460edd16368SStephen M. Cameron 	/* Allocate the per device structures */
3461edd16368SStephen M. Cameron 	for (i = 0; i < ndevs_to_allocate; i++) {
3462b7ec021fSScott Teel 		if (i >= HPSA_MAX_DEVICES) {
3463b7ec021fSScott Teel 			dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
3464b7ec021fSScott Teel 				"  %d devices ignored.\n", HPSA_MAX_DEVICES,
3465b7ec021fSScott Teel 				ndevs_to_allocate - HPSA_MAX_DEVICES);
3466b7ec021fSScott Teel 			break;
3467b7ec021fSScott Teel 		}
3468b7ec021fSScott Teel 
3469edd16368SStephen M. Cameron 		currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
3470edd16368SStephen M. Cameron 		if (!currentsd[i]) {
3471edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
3472edd16368SStephen M. Cameron 				__FILE__, __LINE__);
3473edd16368SStephen M. Cameron 			goto out;
3474edd16368SStephen M. Cameron 		}
3475edd16368SStephen M. Cameron 		ndev_allocated++;
3476edd16368SStephen M. Cameron 	}
3477edd16368SStephen M. Cameron 
34788645291bSStephen M. Cameron 	if (is_scsi_rev_5(h))
3479339b2b14SStephen M. Cameron 		raid_ctlr_position = 0;
3480339b2b14SStephen M. Cameron 	else
3481339b2b14SStephen M. Cameron 		raid_ctlr_position = nphysicals + nlogicals;
3482339b2b14SStephen M. Cameron 
3483edd16368SStephen M. Cameron 	/* adjust our table of devices */
34844f4eb9f1SScott Teel 	n_ext_target_devs = 0;
3485edd16368SStephen M. Cameron 	for (i = 0; i < nphysicals + nlogicals + 1; i++) {
34860b0e1d6cSStephen M. Cameron 		u8 *lunaddrbytes, is_OBDR = 0;
3487edd16368SStephen M. Cameron 
3488edd16368SStephen M. Cameron 		/* Figure out where the LUN ID info is coming from */
3489339b2b14SStephen M. Cameron 		lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
3490339b2b14SStephen M. Cameron 			i, nphysicals, nlogicals, physdev_list, logdev_list);
349141ce4c35SStephen Cameron 
349241ce4c35SStephen Cameron 		/* skip masked non-disk devices */
349341ce4c35SStephen Cameron 		if (MASKED_DEVICE(lunaddrbytes))
349441ce4c35SStephen Cameron 			if (i < nphysicals + (raid_ctlr_position == 0) &&
349541ce4c35SStephen Cameron 				NON_DISK_PHYS_DEV(lunaddrbytes))
3496edd16368SStephen M. Cameron 				continue;
3497edd16368SStephen M. Cameron 
3498edd16368SStephen M. Cameron 		/* Get device type, vendor, model, device id */
34990b0e1d6cSStephen M. Cameron 		if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
35000b0e1d6cSStephen M. Cameron 							&is_OBDR))
3501edd16368SStephen M. Cameron 			continue; /* skip it if we can't talk to it. */
35021f310bdeSStephen M. Cameron 		figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
35039b5c48c2SStephen Cameron 		hpsa_update_device_supports_aborts(h, tmpdevice, lunaddrbytes);
3504edd16368SStephen M. Cameron 		this_device = currentsd[ncurrent];
3505edd16368SStephen M. Cameron 
3506edd16368SStephen M. Cameron 		/*
35074f4eb9f1SScott Teel 		 * For external target devices, we have to insert a LUN 0 which
3508edd16368SStephen M. Cameron 		 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
3509edd16368SStephen M. Cameron 		 * is nonetheless an enclosure device there.  We have to
3510edd16368SStephen M. Cameron 		 * present that otherwise linux won't find anything if
3511edd16368SStephen M. Cameron 		 * there is no lun 0.
3512edd16368SStephen M. Cameron 		 */
35134f4eb9f1SScott Teel 		if (add_ext_target_dev(h, tmpdevice, this_device,
35141f310bdeSStephen M. Cameron 				lunaddrbytes, lunzerobits,
35154f4eb9f1SScott Teel 				&n_ext_target_devs)) {
3516edd16368SStephen M. Cameron 			ncurrent++;
3517edd16368SStephen M. Cameron 			this_device = currentsd[ncurrent];
3518edd16368SStephen M. Cameron 		}
3519edd16368SStephen M. Cameron 
3520edd16368SStephen M. Cameron 		*this_device = *tmpdevice;
3521edd16368SStephen M. Cameron 
352241ce4c35SStephen Cameron 		/* do not expose masked devices */
352341ce4c35SStephen Cameron 		if (MASKED_DEVICE(lunaddrbytes) &&
352441ce4c35SStephen Cameron 			i < nphysicals + (raid_ctlr_position == 0)) {
352541ce4c35SStephen Cameron 			if (h->hba_mode_enabled)
352641ce4c35SStephen Cameron 				dev_warn(&h->pdev->dev,
352741ce4c35SStephen Cameron 					"Masked physical device detected\n");
352841ce4c35SStephen Cameron 			this_device->expose_state = HPSA_DO_NOT_EXPOSE;
352941ce4c35SStephen Cameron 		} else {
353041ce4c35SStephen Cameron 			this_device->expose_state =
353141ce4c35SStephen Cameron 					HPSA_SG_ATTACH | HPSA_ULD_ATTACH;
353241ce4c35SStephen Cameron 		}
353341ce4c35SStephen Cameron 
3534edd16368SStephen M. Cameron 		switch (this_device->devtype) {
35350b0e1d6cSStephen M. Cameron 		case TYPE_ROM:
3536edd16368SStephen M. Cameron 			/* We don't *really* support actual CD-ROM devices,
3537edd16368SStephen M. Cameron 			 * just "One Button Disaster Recovery" tape drive
3538edd16368SStephen M. Cameron 			 * which temporarily pretends to be a CD-ROM drive.
3539edd16368SStephen M. Cameron 			 * So we check that the device is really an OBDR tape
3540edd16368SStephen M. Cameron 			 * device by checking for "$DR-10" in bytes 43-48 of
3541edd16368SStephen M. Cameron 			 * the inquiry data.
3542edd16368SStephen M. Cameron 			 */
35430b0e1d6cSStephen M. Cameron 			if (is_OBDR)
3544edd16368SStephen M. Cameron 				ncurrent++;
3545edd16368SStephen M. Cameron 			break;
3546edd16368SStephen M. Cameron 		case TYPE_DISK:
3547283b4a9bSStephen M. Cameron 			if (i >= nphysicals) {
3548283b4a9bSStephen M. Cameron 				ncurrent++;
3549edd16368SStephen M. Cameron 				break;
3550283b4a9bSStephen M. Cameron 			}
3551ecf418d1SJoe Handzik 
3552ecf418d1SJoe Handzik 			if (h->hba_mode_enabled)
3553ecf418d1SJoe Handzik 				/* never use raid mapper in HBA mode */
3554ecf418d1SJoe Handzik 				this_device->offload_enabled = 0;
3555ecf418d1SJoe Handzik 			else if (!(h->transMethod & CFGTBL_Trans_io_accel1 ||
3556ecf418d1SJoe Handzik 				h->transMethod & CFGTBL_Trans_io_accel2))
3557316b221aSStephen M. Cameron 				break;
3558ecf418d1SJoe Handzik 
355903383736SDon Brace 			hpsa_get_ioaccel_drive_info(h, this_device,
356003383736SDon Brace 						lunaddrbytes, id_phys);
356103383736SDon Brace 			atomic_set(&this_device->ioaccel_cmds_out, 0);
3562edd16368SStephen M. Cameron 			ncurrent++;
3563edd16368SStephen M. Cameron 			break;
3564edd16368SStephen M. Cameron 		case TYPE_TAPE:
3565edd16368SStephen M. Cameron 		case TYPE_MEDIUM_CHANGER:
3566edd16368SStephen M. Cameron 			ncurrent++;
3567edd16368SStephen M. Cameron 			break;
356841ce4c35SStephen Cameron 		case TYPE_ENCLOSURE:
356941ce4c35SStephen Cameron 			if (h->hba_mode_enabled)
357041ce4c35SStephen Cameron 				ncurrent++;
357141ce4c35SStephen Cameron 			break;
3572edd16368SStephen M. Cameron 		case TYPE_RAID:
3573edd16368SStephen M. Cameron 			/* Only present the Smartarray HBA as a RAID controller.
3574edd16368SStephen M. Cameron 			 * If it's a RAID controller other than the HBA itself
3575edd16368SStephen M. Cameron 			 * (an external RAID controller, MSA500 or similar)
3576edd16368SStephen M. Cameron 			 * don't present it.
3577edd16368SStephen M. Cameron 			 */
3578edd16368SStephen M. Cameron 			if (!is_hba_lunid(lunaddrbytes))
3579edd16368SStephen M. Cameron 				break;
3580edd16368SStephen M. Cameron 			ncurrent++;
3581edd16368SStephen M. Cameron 			break;
3582edd16368SStephen M. Cameron 		default:
3583edd16368SStephen M. Cameron 			break;
3584edd16368SStephen M. Cameron 		}
3585cfe5badcSScott Teel 		if (ncurrent >= HPSA_MAX_DEVICES)
3586edd16368SStephen M. Cameron 			break;
3587edd16368SStephen M. Cameron 	}
3588edd16368SStephen M. Cameron 	adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
3589edd16368SStephen M. Cameron out:
3590edd16368SStephen M. Cameron 	kfree(tmpdevice);
3591edd16368SStephen M. Cameron 	for (i = 0; i < ndev_allocated; i++)
3592edd16368SStephen M. Cameron 		kfree(currentsd[i]);
3593edd16368SStephen M. Cameron 	kfree(currentsd);
3594edd16368SStephen M. Cameron 	kfree(physdev_list);
3595edd16368SStephen M. Cameron 	kfree(logdev_list);
359603383736SDon Brace 	kfree(id_phys);
3597edd16368SStephen M. Cameron }
3598edd16368SStephen M. Cameron 
3599ec5cbf04SWebb Scales static void hpsa_set_sg_descriptor(struct SGDescriptor *desc,
3600ec5cbf04SWebb Scales 				   struct scatterlist *sg)
3601ec5cbf04SWebb Scales {
3602ec5cbf04SWebb Scales 	u64 addr64 = (u64) sg_dma_address(sg);
3603ec5cbf04SWebb Scales 	unsigned int len = sg_dma_len(sg);
3604ec5cbf04SWebb Scales 
3605ec5cbf04SWebb Scales 	desc->Addr = cpu_to_le64(addr64);
3606ec5cbf04SWebb Scales 	desc->Len = cpu_to_le32(len);
3607ec5cbf04SWebb Scales 	desc->Ext = 0;
3608ec5cbf04SWebb Scales }
3609ec5cbf04SWebb Scales 
3610c7ee65b3SWebb Scales /*
3611c7ee65b3SWebb Scales  * hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
3612edd16368SStephen M. Cameron  * dma mapping  and fills in the scatter gather entries of the
3613edd16368SStephen M. Cameron  * hpsa command, cp.
3614edd16368SStephen M. Cameron  */
361533a2ffceSStephen M. Cameron static int hpsa_scatter_gather(struct ctlr_info *h,
3616edd16368SStephen M. Cameron 		struct CommandList *cp,
3617edd16368SStephen M. Cameron 		struct scsi_cmnd *cmd)
3618edd16368SStephen M. Cameron {
3619edd16368SStephen M. Cameron 	struct scatterlist *sg;
362033a2ffceSStephen M. Cameron 	int use_sg, i, sg_index, chained;
362133a2ffceSStephen M. Cameron 	struct SGDescriptor *curr_sg;
3622edd16368SStephen M. Cameron 
362333a2ffceSStephen M. Cameron 	BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
3624edd16368SStephen M. Cameron 
3625edd16368SStephen M. Cameron 	use_sg = scsi_dma_map(cmd);
3626edd16368SStephen M. Cameron 	if (use_sg < 0)
3627edd16368SStephen M. Cameron 		return use_sg;
3628edd16368SStephen M. Cameron 
3629edd16368SStephen M. Cameron 	if (!use_sg)
3630edd16368SStephen M. Cameron 		goto sglist_finished;
3631edd16368SStephen M. Cameron 
363233a2ffceSStephen M. Cameron 	curr_sg = cp->SG;
363333a2ffceSStephen M. Cameron 	chained = 0;
363433a2ffceSStephen M. Cameron 	sg_index = 0;
3635edd16368SStephen M. Cameron 	scsi_for_each_sg(cmd, sg, use_sg, i) {
363633a2ffceSStephen M. Cameron 		if (i == h->max_cmd_sg_entries - 1 &&
363733a2ffceSStephen M. Cameron 			use_sg > h->max_cmd_sg_entries) {
363833a2ffceSStephen M. Cameron 			chained = 1;
363933a2ffceSStephen M. Cameron 			curr_sg = h->cmd_sg_list[cp->cmdindex];
364033a2ffceSStephen M. Cameron 			sg_index = 0;
364133a2ffceSStephen M. Cameron 		}
3642ec5cbf04SWebb Scales 		hpsa_set_sg_descriptor(curr_sg, sg);
364333a2ffceSStephen M. Cameron 		curr_sg++;
364433a2ffceSStephen M. Cameron 	}
3645ec5cbf04SWebb Scales 
3646ec5cbf04SWebb Scales 	/* Back the pointer up to the last entry and mark it as "last". */
364750a0decfSStephen M. Cameron 	(--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
364833a2ffceSStephen M. Cameron 
364933a2ffceSStephen M. Cameron 	if (use_sg + chained > h->maxSG)
365033a2ffceSStephen M. Cameron 		h->maxSG = use_sg + chained;
365133a2ffceSStephen M. Cameron 
365233a2ffceSStephen M. Cameron 	if (chained) {
365333a2ffceSStephen M. Cameron 		cp->Header.SGList = h->max_cmd_sg_entries;
365450a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(use_sg + 1);
3655e2bea6dfSStephen M. Cameron 		if (hpsa_map_sg_chain_block(h, cp)) {
3656e2bea6dfSStephen M. Cameron 			scsi_dma_unmap(cmd);
3657e2bea6dfSStephen M. Cameron 			return -1;
3658e2bea6dfSStephen M. Cameron 		}
365933a2ffceSStephen M. Cameron 		return 0;
3660edd16368SStephen M. Cameron 	}
3661edd16368SStephen M. Cameron 
3662edd16368SStephen M. Cameron sglist_finished:
3663edd16368SStephen M. Cameron 
366401a02ffcSStephen M. Cameron 	cp->Header.SGList = (u8) use_sg;   /* no. SGs contig in this cmd */
3665c7ee65b3SWebb Scales 	cp->Header.SGTotal = cpu_to_le16(use_sg); /* total sgs in cmd list */
3666edd16368SStephen M. Cameron 	return 0;
3667edd16368SStephen M. Cameron }
3668edd16368SStephen M. Cameron 
3669283b4a9bSStephen M. Cameron #define IO_ACCEL_INELIGIBLE (1)
3670283b4a9bSStephen M. Cameron static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
3671283b4a9bSStephen M. Cameron {
3672283b4a9bSStephen M. Cameron 	int is_write = 0;
3673283b4a9bSStephen M. Cameron 	u32 block;
3674283b4a9bSStephen M. Cameron 	u32 block_cnt;
3675283b4a9bSStephen M. Cameron 
3676283b4a9bSStephen M. Cameron 	/* Perform some CDB fixups if needed using 10 byte reads/writes only */
3677283b4a9bSStephen M. Cameron 	switch (cdb[0]) {
3678283b4a9bSStephen M. Cameron 	case WRITE_6:
3679283b4a9bSStephen M. Cameron 	case WRITE_12:
3680283b4a9bSStephen M. Cameron 		is_write = 1;
3681283b4a9bSStephen M. Cameron 	case READ_6:
3682283b4a9bSStephen M. Cameron 	case READ_12:
3683283b4a9bSStephen M. Cameron 		if (*cdb_len == 6) {
3684283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 8) | cdb[3];
3685283b4a9bSStephen M. Cameron 			block_cnt = cdb[4];
3686283b4a9bSStephen M. Cameron 		} else {
3687283b4a9bSStephen M. Cameron 			BUG_ON(*cdb_len != 12);
3688283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 24) |
3689283b4a9bSStephen M. Cameron 				(((u32) cdb[3]) << 16) |
3690283b4a9bSStephen M. Cameron 				(((u32) cdb[4]) << 8) |
3691283b4a9bSStephen M. Cameron 				cdb[5];
3692283b4a9bSStephen M. Cameron 			block_cnt =
3693283b4a9bSStephen M. Cameron 				(((u32) cdb[6]) << 24) |
3694283b4a9bSStephen M. Cameron 				(((u32) cdb[7]) << 16) |
3695283b4a9bSStephen M. Cameron 				(((u32) cdb[8]) << 8) |
3696283b4a9bSStephen M. Cameron 				cdb[9];
3697283b4a9bSStephen M. Cameron 		}
3698283b4a9bSStephen M. Cameron 		if (block_cnt > 0xffff)
3699283b4a9bSStephen M. Cameron 			return IO_ACCEL_INELIGIBLE;
3700283b4a9bSStephen M. Cameron 
3701283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
3702283b4a9bSStephen M. Cameron 		cdb[1] = 0;
3703283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (block >> 24);
3704283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (block >> 16);
3705283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (block >> 8);
3706283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (block);
3707283b4a9bSStephen M. Cameron 		cdb[6] = 0;
3708283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (block_cnt >> 8);
3709283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (block_cnt);
3710283b4a9bSStephen M. Cameron 		cdb[9] = 0;
3711283b4a9bSStephen M. Cameron 		*cdb_len = 10;
3712283b4a9bSStephen M. Cameron 		break;
3713283b4a9bSStephen M. Cameron 	}
3714283b4a9bSStephen M. Cameron 	return 0;
3715283b4a9bSStephen M. Cameron }
3716283b4a9bSStephen M. Cameron 
3717c349775eSScott Teel static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
3718283b4a9bSStephen M. Cameron 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
371903383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
3720e1f7de0cSMatt Gates {
3721e1f7de0cSMatt Gates 	struct scsi_cmnd *cmd = c->scsi_cmd;
3722e1f7de0cSMatt Gates 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
3723e1f7de0cSMatt Gates 	unsigned int len;
3724e1f7de0cSMatt Gates 	unsigned int total_len = 0;
3725e1f7de0cSMatt Gates 	struct scatterlist *sg;
3726e1f7de0cSMatt Gates 	u64 addr64;
3727e1f7de0cSMatt Gates 	int use_sg, i;
3728e1f7de0cSMatt Gates 	struct SGDescriptor *curr_sg;
3729e1f7de0cSMatt Gates 	u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
3730e1f7de0cSMatt Gates 
3731283b4a9bSStephen M. Cameron 	/* TODO: implement chaining support */
373203383736SDon Brace 	if (scsi_sg_count(cmd) > h->ioaccel_maxsg) {
373303383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3734283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
373503383736SDon Brace 	}
3736283b4a9bSStephen M. Cameron 
3737e1f7de0cSMatt Gates 	BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
3738e1f7de0cSMatt Gates 
373903383736SDon Brace 	if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
374003383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3741283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
374203383736SDon Brace 	}
3743283b4a9bSStephen M. Cameron 
3744e1f7de0cSMatt Gates 	c->cmd_type = CMD_IOACCEL1;
3745e1f7de0cSMatt Gates 
3746e1f7de0cSMatt Gates 	/* Adjust the DMA address to point to the accelerated command buffer */
3747e1f7de0cSMatt Gates 	c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
3748e1f7de0cSMatt Gates 				(c->cmdindex * sizeof(*cp));
3749e1f7de0cSMatt Gates 	BUG_ON(c->busaddr & 0x0000007F);
3750e1f7de0cSMatt Gates 
3751e1f7de0cSMatt Gates 	use_sg = scsi_dma_map(cmd);
375203383736SDon Brace 	if (use_sg < 0) {
375303383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3754e1f7de0cSMatt Gates 		return use_sg;
375503383736SDon Brace 	}
3756e1f7de0cSMatt Gates 
3757e1f7de0cSMatt Gates 	if (use_sg) {
3758e1f7de0cSMatt Gates 		curr_sg = cp->SG;
3759e1f7de0cSMatt Gates 		scsi_for_each_sg(cmd, sg, use_sg, i) {
3760e1f7de0cSMatt Gates 			addr64 = (u64) sg_dma_address(sg);
3761e1f7de0cSMatt Gates 			len  = sg_dma_len(sg);
3762e1f7de0cSMatt Gates 			total_len += len;
376350a0decfSStephen M. Cameron 			curr_sg->Addr = cpu_to_le64(addr64);
376450a0decfSStephen M. Cameron 			curr_sg->Len = cpu_to_le32(len);
376550a0decfSStephen M. Cameron 			curr_sg->Ext = cpu_to_le32(0);
3766e1f7de0cSMatt Gates 			curr_sg++;
3767e1f7de0cSMatt Gates 		}
376850a0decfSStephen M. Cameron 		(--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
3769e1f7de0cSMatt Gates 
3770e1f7de0cSMatt Gates 		switch (cmd->sc_data_direction) {
3771e1f7de0cSMatt Gates 		case DMA_TO_DEVICE:
3772e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_OUT;
3773e1f7de0cSMatt Gates 			break;
3774e1f7de0cSMatt Gates 		case DMA_FROM_DEVICE:
3775e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_IN;
3776e1f7de0cSMatt Gates 			break;
3777e1f7de0cSMatt Gates 		case DMA_NONE:
3778e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_NODATAXFER;
3779e1f7de0cSMatt Gates 			break;
3780e1f7de0cSMatt Gates 		default:
3781e1f7de0cSMatt Gates 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3782e1f7de0cSMatt Gates 			cmd->sc_data_direction);
3783e1f7de0cSMatt Gates 			BUG();
3784e1f7de0cSMatt Gates 			break;
3785e1f7de0cSMatt Gates 		}
3786e1f7de0cSMatt Gates 	} else {
3787e1f7de0cSMatt Gates 		control |= IOACCEL1_CONTROL_NODATAXFER;
3788e1f7de0cSMatt Gates 	}
3789e1f7de0cSMatt Gates 
3790c349775eSScott Teel 	c->Header.SGList = use_sg;
3791e1f7de0cSMatt Gates 	/* Fill out the command structure to submit */
37922b08b3e9SDon Brace 	cp->dev_handle = cpu_to_le16(ioaccel_handle & 0xFFFF);
37932b08b3e9SDon Brace 	cp->transfer_len = cpu_to_le32(total_len);
37942b08b3e9SDon Brace 	cp->io_flags = cpu_to_le16(IOACCEL1_IOFLAGS_IO_REQ |
37952b08b3e9SDon Brace 			(cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK));
37962b08b3e9SDon Brace 	cp->control = cpu_to_le32(control);
3797283b4a9bSStephen M. Cameron 	memcpy(cp->CDB, cdb, cdb_len);
3798283b4a9bSStephen M. Cameron 	memcpy(cp->CISS_LUN, scsi3addr, 8);
3799c349775eSScott Teel 	/* Tag was already set at init time. */
3800e1f7de0cSMatt Gates 	enqueue_cmd_and_start_io(h, c);
3801e1f7de0cSMatt Gates 	return 0;
3802e1f7de0cSMatt Gates }
3803edd16368SStephen M. Cameron 
3804283b4a9bSStephen M. Cameron /*
3805283b4a9bSStephen M. Cameron  * Queue a command directly to a device behind the controller using the
3806283b4a9bSStephen M. Cameron  * I/O accelerator path.
3807283b4a9bSStephen M. Cameron  */
3808283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
3809283b4a9bSStephen M. Cameron 	struct CommandList *c)
3810283b4a9bSStephen M. Cameron {
3811283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
3812283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3813283b4a9bSStephen M. Cameron 
381403383736SDon Brace 	c->phys_disk = dev;
381503383736SDon Brace 
3816283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
381703383736SDon Brace 		cmd->cmnd, cmd->cmd_len, dev->scsi3addr, dev);
3818283b4a9bSStephen M. Cameron }
3819283b4a9bSStephen M. Cameron 
3820dd0e19f3SScott Teel /*
3821dd0e19f3SScott Teel  * Set encryption parameters for the ioaccel2 request
3822dd0e19f3SScott Teel  */
3823dd0e19f3SScott Teel static void set_encrypt_ioaccel2(struct ctlr_info *h,
3824dd0e19f3SScott Teel 	struct CommandList *c, struct io_accel2_cmd *cp)
3825dd0e19f3SScott Teel {
3826dd0e19f3SScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
3827dd0e19f3SScott Teel 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3828dd0e19f3SScott Teel 	struct raid_map_data *map = &dev->raid_map;
3829dd0e19f3SScott Teel 	u64 first_block;
3830dd0e19f3SScott Teel 
3831dd0e19f3SScott Teel 	/* Are we doing encryption on this device */
38322b08b3e9SDon Brace 	if (!(le16_to_cpu(map->flags) & RAID_MAP_FLAG_ENCRYPT_ON))
3833dd0e19f3SScott Teel 		return;
3834dd0e19f3SScott Teel 	/* Set the data encryption key index. */
3835dd0e19f3SScott Teel 	cp->dekindex = map->dekindex;
3836dd0e19f3SScott Teel 
3837dd0e19f3SScott Teel 	/* Set the encryption enable flag, encoded into direction field. */
3838dd0e19f3SScott Teel 	cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
3839dd0e19f3SScott Teel 
3840dd0e19f3SScott Teel 	/* Set encryption tweak values based on logical block address
3841dd0e19f3SScott Teel 	 * If block size is 512, tweak value is LBA.
3842dd0e19f3SScott Teel 	 * For other block sizes, tweak is (LBA * block size)/ 512)
3843dd0e19f3SScott Teel 	 */
3844dd0e19f3SScott Teel 	switch (cmd->cmnd[0]) {
3845dd0e19f3SScott Teel 	/* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
3846dd0e19f3SScott Teel 	case WRITE_6:
3847dd0e19f3SScott Teel 	case READ_6:
38482b08b3e9SDon Brace 		first_block = get_unaligned_be16(&cmd->cmnd[2]);
3849dd0e19f3SScott Teel 		break;
3850dd0e19f3SScott Teel 	case WRITE_10:
3851dd0e19f3SScott Teel 	case READ_10:
3852dd0e19f3SScott Teel 	/* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
3853dd0e19f3SScott Teel 	case WRITE_12:
3854dd0e19f3SScott Teel 	case READ_12:
38552b08b3e9SDon Brace 		first_block = get_unaligned_be32(&cmd->cmnd[2]);
3856dd0e19f3SScott Teel 		break;
3857dd0e19f3SScott Teel 	case WRITE_16:
3858dd0e19f3SScott Teel 	case READ_16:
38592b08b3e9SDon Brace 		first_block = get_unaligned_be64(&cmd->cmnd[2]);
3860dd0e19f3SScott Teel 		break;
3861dd0e19f3SScott Teel 	default:
3862dd0e19f3SScott Teel 		dev_err(&h->pdev->dev,
38632b08b3e9SDon Brace 			"ERROR: %s: size (0x%x) not supported for encryption\n",
38642b08b3e9SDon Brace 			__func__, cmd->cmnd[0]);
3865dd0e19f3SScott Teel 		BUG();
3866dd0e19f3SScott Teel 		break;
3867dd0e19f3SScott Teel 	}
38682b08b3e9SDon Brace 
38692b08b3e9SDon Brace 	if (le32_to_cpu(map->volume_blk_size) != 512)
38702b08b3e9SDon Brace 		first_block = first_block *
38712b08b3e9SDon Brace 				le32_to_cpu(map->volume_blk_size)/512;
38722b08b3e9SDon Brace 
38732b08b3e9SDon Brace 	cp->tweak_lower = cpu_to_le32(first_block);
38742b08b3e9SDon Brace 	cp->tweak_upper = cpu_to_le32(first_block >> 32);
3875dd0e19f3SScott Teel }
3876dd0e19f3SScott Teel 
3877c349775eSScott Teel static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
3878c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
387903383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
3880c349775eSScott Teel {
3881c349775eSScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
3882c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
3883c349775eSScott Teel 	struct ioaccel2_sg_element *curr_sg;
3884c349775eSScott Teel 	int use_sg, i;
3885c349775eSScott Teel 	struct scatterlist *sg;
3886c349775eSScott Teel 	u64 addr64;
3887c349775eSScott Teel 	u32 len;
3888c349775eSScott Teel 	u32 total_len = 0;
3889c349775eSScott Teel 
3890d9a729f3SWebb Scales 	BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
3891c349775eSScott Teel 
389203383736SDon Brace 	if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
389303383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3894c349775eSScott Teel 		return IO_ACCEL_INELIGIBLE;
389503383736SDon Brace 	}
389603383736SDon Brace 
3897c349775eSScott Teel 	c->cmd_type = CMD_IOACCEL2;
3898c349775eSScott Teel 	/* Adjust the DMA address to point to the accelerated command buffer */
3899c349775eSScott Teel 	c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
3900c349775eSScott Teel 				(c->cmdindex * sizeof(*cp));
3901c349775eSScott Teel 	BUG_ON(c->busaddr & 0x0000007F);
3902c349775eSScott Teel 
3903c349775eSScott Teel 	memset(cp, 0, sizeof(*cp));
3904c349775eSScott Teel 	cp->IU_type = IOACCEL2_IU_TYPE;
3905c349775eSScott Teel 
3906c349775eSScott Teel 	use_sg = scsi_dma_map(cmd);
390703383736SDon Brace 	if (use_sg < 0) {
390803383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3909c349775eSScott Teel 		return use_sg;
391003383736SDon Brace 	}
3911c349775eSScott Teel 
3912c349775eSScott Teel 	if (use_sg) {
3913c349775eSScott Teel 		curr_sg = cp->sg;
3914d9a729f3SWebb Scales 		if (use_sg > h->ioaccel_maxsg) {
3915d9a729f3SWebb Scales 			addr64 = le64_to_cpu(
3916d9a729f3SWebb Scales 				h->ioaccel2_cmd_sg_list[c->cmdindex]->address);
3917d9a729f3SWebb Scales 			curr_sg->address = cpu_to_le64(addr64);
3918d9a729f3SWebb Scales 			curr_sg->length = 0;
3919d9a729f3SWebb Scales 			curr_sg->reserved[0] = 0;
3920d9a729f3SWebb Scales 			curr_sg->reserved[1] = 0;
3921d9a729f3SWebb Scales 			curr_sg->reserved[2] = 0;
3922d9a729f3SWebb Scales 			curr_sg->chain_indicator = 0x80;
3923d9a729f3SWebb Scales 
3924d9a729f3SWebb Scales 			curr_sg = h->ioaccel2_cmd_sg_list[c->cmdindex];
3925d9a729f3SWebb Scales 		}
3926c349775eSScott Teel 		scsi_for_each_sg(cmd, sg, use_sg, i) {
3927c349775eSScott Teel 			addr64 = (u64) sg_dma_address(sg);
3928c349775eSScott Teel 			len  = sg_dma_len(sg);
3929c349775eSScott Teel 			total_len += len;
3930c349775eSScott Teel 			curr_sg->address = cpu_to_le64(addr64);
3931c349775eSScott Teel 			curr_sg->length = cpu_to_le32(len);
3932c349775eSScott Teel 			curr_sg->reserved[0] = 0;
3933c349775eSScott Teel 			curr_sg->reserved[1] = 0;
3934c349775eSScott Teel 			curr_sg->reserved[2] = 0;
3935c349775eSScott Teel 			curr_sg->chain_indicator = 0;
3936c349775eSScott Teel 			curr_sg++;
3937c349775eSScott Teel 		}
3938c349775eSScott Teel 
3939c349775eSScott Teel 		switch (cmd->sc_data_direction) {
3940c349775eSScott Teel 		case DMA_TO_DEVICE:
3941dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3942dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_OUT;
3943c349775eSScott Teel 			break;
3944c349775eSScott Teel 		case DMA_FROM_DEVICE:
3945dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3946dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_IN;
3947c349775eSScott Teel 			break;
3948c349775eSScott Teel 		case DMA_NONE:
3949dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3950dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_NO_DATA;
3951c349775eSScott Teel 			break;
3952c349775eSScott Teel 		default:
3953c349775eSScott Teel 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3954c349775eSScott Teel 				cmd->sc_data_direction);
3955c349775eSScott Teel 			BUG();
3956c349775eSScott Teel 			break;
3957c349775eSScott Teel 		}
3958c349775eSScott Teel 	} else {
3959dd0e19f3SScott Teel 		cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3960dd0e19f3SScott Teel 		cp->direction |= IOACCEL2_DIR_NO_DATA;
3961c349775eSScott Teel 	}
3962dd0e19f3SScott Teel 
3963dd0e19f3SScott Teel 	/* Set encryption parameters, if necessary */
3964dd0e19f3SScott Teel 	set_encrypt_ioaccel2(h, c, cp);
3965dd0e19f3SScott Teel 
39662b08b3e9SDon Brace 	cp->scsi_nexus = cpu_to_le32(ioaccel_handle);
3967f2405db8SDon Brace 	cp->Tag = cpu_to_le32(c->cmdindex << DIRECT_LOOKUP_SHIFT);
3968c349775eSScott Teel 	memcpy(cp->cdb, cdb, sizeof(cp->cdb));
3969c349775eSScott Teel 
3970c349775eSScott Teel 	cp->data_len = cpu_to_le32(total_len);
3971c349775eSScott Teel 	cp->err_ptr = cpu_to_le64(c->busaddr +
3972c349775eSScott Teel 			offsetof(struct io_accel2_cmd, error_data));
397350a0decfSStephen M. Cameron 	cp->err_len = cpu_to_le32(sizeof(cp->error_data));
3974c349775eSScott Teel 
3975d9a729f3SWebb Scales 	/* fill in sg elements */
3976d9a729f3SWebb Scales 	if (use_sg > h->ioaccel_maxsg) {
3977d9a729f3SWebb Scales 		cp->sg_count = 1;
3978d9a729f3SWebb Scales 		if (hpsa_map_ioaccel2_sg_chain_block(h, cp, c)) {
3979d9a729f3SWebb Scales 			atomic_dec(&phys_disk->ioaccel_cmds_out);
3980d9a729f3SWebb Scales 			scsi_dma_unmap(cmd);
3981d9a729f3SWebb Scales 			return -1;
3982d9a729f3SWebb Scales 		}
3983d9a729f3SWebb Scales 	} else
3984d9a729f3SWebb Scales 		cp->sg_count = (u8) use_sg;
3985d9a729f3SWebb Scales 
3986c349775eSScott Teel 	enqueue_cmd_and_start_io(h, c);
3987c349775eSScott Teel 	return 0;
3988c349775eSScott Teel }
3989c349775eSScott Teel 
3990c349775eSScott Teel /*
3991c349775eSScott Teel  * Queue a command to the correct I/O accelerator path.
3992c349775eSScott Teel  */
3993c349775eSScott Teel static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
3994c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
399503383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
3996c349775eSScott Teel {
399703383736SDon Brace 	/* Try to honor the device's queue depth */
399803383736SDon Brace 	if (atomic_inc_return(&phys_disk->ioaccel_cmds_out) >
399903383736SDon Brace 					phys_disk->queue_depth) {
400003383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
400103383736SDon Brace 		return IO_ACCEL_INELIGIBLE;
400203383736SDon Brace 	}
4003c349775eSScott Teel 	if (h->transMethod & CFGTBL_Trans_io_accel1)
4004c349775eSScott Teel 		return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
400503383736SDon Brace 						cdb, cdb_len, scsi3addr,
400603383736SDon Brace 						phys_disk);
4007c349775eSScott Teel 	else
4008c349775eSScott Teel 		return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
400903383736SDon Brace 						cdb, cdb_len, scsi3addr,
401003383736SDon Brace 						phys_disk);
4011c349775eSScott Teel }
4012c349775eSScott Teel 
40136b80b18fSScott Teel static void raid_map_helper(struct raid_map_data *map,
40146b80b18fSScott Teel 		int offload_to_mirror, u32 *map_index, u32 *current_group)
40156b80b18fSScott Teel {
40166b80b18fSScott Teel 	if (offload_to_mirror == 0)  {
40176b80b18fSScott Teel 		/* use physical disk in the first mirrored group. */
40182b08b3e9SDon Brace 		*map_index %= le16_to_cpu(map->data_disks_per_row);
40196b80b18fSScott Teel 		return;
40206b80b18fSScott Teel 	}
40216b80b18fSScott Teel 	do {
40226b80b18fSScott Teel 		/* determine mirror group that *map_index indicates */
40232b08b3e9SDon Brace 		*current_group = *map_index /
40242b08b3e9SDon Brace 			le16_to_cpu(map->data_disks_per_row);
40256b80b18fSScott Teel 		if (offload_to_mirror == *current_group)
40266b80b18fSScott Teel 			continue;
40272b08b3e9SDon Brace 		if (*current_group < le16_to_cpu(map->layout_map_count) - 1) {
40286b80b18fSScott Teel 			/* select map index from next group */
40292b08b3e9SDon Brace 			*map_index += le16_to_cpu(map->data_disks_per_row);
40306b80b18fSScott Teel 			(*current_group)++;
40316b80b18fSScott Teel 		} else {
40326b80b18fSScott Teel 			/* select map index from first group */
40332b08b3e9SDon Brace 			*map_index %= le16_to_cpu(map->data_disks_per_row);
40346b80b18fSScott Teel 			*current_group = 0;
40356b80b18fSScott Teel 		}
40366b80b18fSScott Teel 	} while (offload_to_mirror != *current_group);
40376b80b18fSScott Teel }
40386b80b18fSScott Teel 
4039283b4a9bSStephen M. Cameron /*
4040283b4a9bSStephen M. Cameron  * Attempt to perform offload RAID mapping for a logical volume I/O.
4041283b4a9bSStephen M. Cameron  */
4042283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
4043283b4a9bSStephen M. Cameron 	struct CommandList *c)
4044283b4a9bSStephen M. Cameron {
4045283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
4046283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4047283b4a9bSStephen M. Cameron 	struct raid_map_data *map = &dev->raid_map;
4048283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map->data[0];
4049283b4a9bSStephen M. Cameron 	int is_write = 0;
4050283b4a9bSStephen M. Cameron 	u32 map_index;
4051283b4a9bSStephen M. Cameron 	u64 first_block, last_block;
4052283b4a9bSStephen M. Cameron 	u32 block_cnt;
4053283b4a9bSStephen M. Cameron 	u32 blocks_per_row;
4054283b4a9bSStephen M. Cameron 	u64 first_row, last_row;
4055283b4a9bSStephen M. Cameron 	u32 first_row_offset, last_row_offset;
4056283b4a9bSStephen M. Cameron 	u32 first_column, last_column;
40576b80b18fSScott Teel 	u64 r0_first_row, r0_last_row;
40586b80b18fSScott Teel 	u32 r5or6_blocks_per_row;
40596b80b18fSScott Teel 	u64 r5or6_first_row, r5or6_last_row;
40606b80b18fSScott Teel 	u32 r5or6_first_row_offset, r5or6_last_row_offset;
40616b80b18fSScott Teel 	u32 r5or6_first_column, r5or6_last_column;
40626b80b18fSScott Teel 	u32 total_disks_per_row;
40636b80b18fSScott Teel 	u32 stripesize;
40646b80b18fSScott Teel 	u32 first_group, last_group, current_group;
4065283b4a9bSStephen M. Cameron 	u32 map_row;
4066283b4a9bSStephen M. Cameron 	u32 disk_handle;
4067283b4a9bSStephen M. Cameron 	u64 disk_block;
4068283b4a9bSStephen M. Cameron 	u32 disk_block_cnt;
4069283b4a9bSStephen M. Cameron 	u8 cdb[16];
4070283b4a9bSStephen M. Cameron 	u8 cdb_len;
40712b08b3e9SDon Brace 	u16 strip_size;
4072283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
4073283b4a9bSStephen M. Cameron 	u64 tmpdiv;
4074283b4a9bSStephen M. Cameron #endif
40756b80b18fSScott Teel 	int offload_to_mirror;
4076283b4a9bSStephen M. Cameron 
4077283b4a9bSStephen M. Cameron 	/* check for valid opcode, get LBA and block count */
4078283b4a9bSStephen M. Cameron 	switch (cmd->cmnd[0]) {
4079283b4a9bSStephen M. Cameron 	case WRITE_6:
4080283b4a9bSStephen M. Cameron 		is_write = 1;
4081283b4a9bSStephen M. Cameron 	case READ_6:
4082283b4a9bSStephen M. Cameron 		first_block =
4083283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 8) |
4084283b4a9bSStephen M. Cameron 			cmd->cmnd[3];
4085283b4a9bSStephen M. Cameron 		block_cnt = cmd->cmnd[4];
40863fa89a04SStephen M. Cameron 		if (block_cnt == 0)
40873fa89a04SStephen M. Cameron 			block_cnt = 256;
4088283b4a9bSStephen M. Cameron 		break;
4089283b4a9bSStephen M. Cameron 	case WRITE_10:
4090283b4a9bSStephen M. Cameron 		is_write = 1;
4091283b4a9bSStephen M. Cameron 	case READ_10:
4092283b4a9bSStephen M. Cameron 		first_block =
4093283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
4094283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
4095283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
4096283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
4097283b4a9bSStephen M. Cameron 		block_cnt =
4098283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 8) |
4099283b4a9bSStephen M. Cameron 			cmd->cmnd[8];
4100283b4a9bSStephen M. Cameron 		break;
4101283b4a9bSStephen M. Cameron 	case WRITE_12:
4102283b4a9bSStephen M. Cameron 		is_write = 1;
4103283b4a9bSStephen M. Cameron 	case READ_12:
4104283b4a9bSStephen M. Cameron 		first_block =
4105283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
4106283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
4107283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
4108283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
4109283b4a9bSStephen M. Cameron 		block_cnt =
4110283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[6]) << 24) |
4111283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 16) |
4112283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[8]) << 8) |
4113283b4a9bSStephen M. Cameron 		cmd->cmnd[9];
4114283b4a9bSStephen M. Cameron 		break;
4115283b4a9bSStephen M. Cameron 	case WRITE_16:
4116283b4a9bSStephen M. Cameron 		is_write = 1;
4117283b4a9bSStephen M. Cameron 	case READ_16:
4118283b4a9bSStephen M. Cameron 		first_block =
4119283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 56) |
4120283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 48) |
4121283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 40) |
4122283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[5]) << 32) |
4123283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[6]) << 24) |
4124283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[7]) << 16) |
4125283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[8]) << 8) |
4126283b4a9bSStephen M. Cameron 			cmd->cmnd[9];
4127283b4a9bSStephen M. Cameron 		block_cnt =
4128283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[10]) << 24) |
4129283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[11]) << 16) |
4130283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[12]) << 8) |
4131283b4a9bSStephen M. Cameron 			cmd->cmnd[13];
4132283b4a9bSStephen M. Cameron 		break;
4133283b4a9bSStephen M. Cameron 	default:
4134283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
4135283b4a9bSStephen M. Cameron 	}
4136283b4a9bSStephen M. Cameron 	last_block = first_block + block_cnt - 1;
4137283b4a9bSStephen M. Cameron 
4138283b4a9bSStephen M. Cameron 	/* check for write to non-RAID-0 */
4139283b4a9bSStephen M. Cameron 	if (is_write && dev->raid_level != 0)
4140283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
4141283b4a9bSStephen M. Cameron 
4142283b4a9bSStephen M. Cameron 	/* check for invalid block or wraparound */
41432b08b3e9SDon Brace 	if (last_block >= le64_to_cpu(map->volume_blk_cnt) ||
41442b08b3e9SDon Brace 		last_block < first_block)
4145283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
4146283b4a9bSStephen M. Cameron 
4147283b4a9bSStephen M. Cameron 	/* calculate stripe information for the request */
41482b08b3e9SDon Brace 	blocks_per_row = le16_to_cpu(map->data_disks_per_row) *
41492b08b3e9SDon Brace 				le16_to_cpu(map->strip_size);
41502b08b3e9SDon Brace 	strip_size = le16_to_cpu(map->strip_size);
4151283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
4152283b4a9bSStephen M. Cameron 	tmpdiv = first_block;
4153283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
4154283b4a9bSStephen M. Cameron 	first_row = tmpdiv;
4155283b4a9bSStephen M. Cameron 	tmpdiv = last_block;
4156283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
4157283b4a9bSStephen M. Cameron 	last_row = tmpdiv;
4158283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
4159283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
4160283b4a9bSStephen M. Cameron 	tmpdiv = first_row_offset;
41612b08b3e9SDon Brace 	(void) do_div(tmpdiv, strip_size);
4162283b4a9bSStephen M. Cameron 	first_column = tmpdiv;
4163283b4a9bSStephen M. Cameron 	tmpdiv = last_row_offset;
41642b08b3e9SDon Brace 	(void) do_div(tmpdiv, strip_size);
4165283b4a9bSStephen M. Cameron 	last_column = tmpdiv;
4166283b4a9bSStephen M. Cameron #else
4167283b4a9bSStephen M. Cameron 	first_row = first_block / blocks_per_row;
4168283b4a9bSStephen M. Cameron 	last_row = last_block / blocks_per_row;
4169283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
4170283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
41712b08b3e9SDon Brace 	first_column = first_row_offset / strip_size;
41722b08b3e9SDon Brace 	last_column = last_row_offset / strip_size;
4173283b4a9bSStephen M. Cameron #endif
4174283b4a9bSStephen M. Cameron 
4175283b4a9bSStephen M. Cameron 	/* if this isn't a single row/column then give to the controller */
4176283b4a9bSStephen M. Cameron 	if ((first_row != last_row) || (first_column != last_column))
4177283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
4178283b4a9bSStephen M. Cameron 
4179283b4a9bSStephen M. Cameron 	/* proceeding with driver mapping */
41802b08b3e9SDon Brace 	total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
41812b08b3e9SDon Brace 				le16_to_cpu(map->metadata_disks_per_row);
4182283b4a9bSStephen M. Cameron 	map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
41832b08b3e9SDon Brace 				le16_to_cpu(map->row_cnt);
41846b80b18fSScott Teel 	map_index = (map_row * total_disks_per_row) + first_column;
41856b80b18fSScott Teel 
41866b80b18fSScott Teel 	switch (dev->raid_level) {
41876b80b18fSScott Teel 	case HPSA_RAID_0:
41886b80b18fSScott Teel 		break; /* nothing special to do */
41896b80b18fSScott Teel 	case HPSA_RAID_1:
41906b80b18fSScott Teel 		/* Handles load balance across RAID 1 members.
41916b80b18fSScott Teel 		 * (2-drive R1 and R10 with even # of drives.)
41926b80b18fSScott Teel 		 * Appropriate for SSDs, not optimal for HDDs
4193283b4a9bSStephen M. Cameron 		 */
41942b08b3e9SDon Brace 		BUG_ON(le16_to_cpu(map->layout_map_count) != 2);
4195283b4a9bSStephen M. Cameron 		if (dev->offload_to_mirror)
41962b08b3e9SDon Brace 			map_index += le16_to_cpu(map->data_disks_per_row);
4197283b4a9bSStephen M. Cameron 		dev->offload_to_mirror = !dev->offload_to_mirror;
41986b80b18fSScott Teel 		break;
41996b80b18fSScott Teel 	case HPSA_RAID_ADM:
42006b80b18fSScott Teel 		/* Handles N-way mirrors  (R1-ADM)
42016b80b18fSScott Teel 		 * and R10 with # of drives divisible by 3.)
42026b80b18fSScott Teel 		 */
42032b08b3e9SDon Brace 		BUG_ON(le16_to_cpu(map->layout_map_count) != 3);
42046b80b18fSScott Teel 
42056b80b18fSScott Teel 		offload_to_mirror = dev->offload_to_mirror;
42066b80b18fSScott Teel 		raid_map_helper(map, offload_to_mirror,
42076b80b18fSScott Teel 				&map_index, &current_group);
42086b80b18fSScott Teel 		/* set mirror group to use next time */
42096b80b18fSScott Teel 		offload_to_mirror =
42102b08b3e9SDon Brace 			(offload_to_mirror >=
42112b08b3e9SDon Brace 			le16_to_cpu(map->layout_map_count) - 1)
42126b80b18fSScott Teel 			? 0 : offload_to_mirror + 1;
42136b80b18fSScott Teel 		dev->offload_to_mirror = offload_to_mirror;
42146b80b18fSScott Teel 		/* Avoid direct use of dev->offload_to_mirror within this
42156b80b18fSScott Teel 		 * function since multiple threads might simultaneously
42166b80b18fSScott Teel 		 * increment it beyond the range of dev->layout_map_count -1.
42176b80b18fSScott Teel 		 */
42186b80b18fSScott Teel 		break;
42196b80b18fSScott Teel 	case HPSA_RAID_5:
42206b80b18fSScott Teel 	case HPSA_RAID_6:
42212b08b3e9SDon Brace 		if (le16_to_cpu(map->layout_map_count) <= 1)
42226b80b18fSScott Teel 			break;
42236b80b18fSScott Teel 
42246b80b18fSScott Teel 		/* Verify first and last block are in same RAID group */
42256b80b18fSScott Teel 		r5or6_blocks_per_row =
42262b08b3e9SDon Brace 			le16_to_cpu(map->strip_size) *
42272b08b3e9SDon Brace 			le16_to_cpu(map->data_disks_per_row);
42286b80b18fSScott Teel 		BUG_ON(r5or6_blocks_per_row == 0);
42292b08b3e9SDon Brace 		stripesize = r5or6_blocks_per_row *
42302b08b3e9SDon Brace 			le16_to_cpu(map->layout_map_count);
42316b80b18fSScott Teel #if BITS_PER_LONG == 32
42326b80b18fSScott Teel 		tmpdiv = first_block;
42336b80b18fSScott Teel 		first_group = do_div(tmpdiv, stripesize);
42346b80b18fSScott Teel 		tmpdiv = first_group;
42356b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
42366b80b18fSScott Teel 		first_group = tmpdiv;
42376b80b18fSScott Teel 		tmpdiv = last_block;
42386b80b18fSScott Teel 		last_group = do_div(tmpdiv, stripesize);
42396b80b18fSScott Teel 		tmpdiv = last_group;
42406b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
42416b80b18fSScott Teel 		last_group = tmpdiv;
42426b80b18fSScott Teel #else
42436b80b18fSScott Teel 		first_group = (first_block % stripesize) / r5or6_blocks_per_row;
42446b80b18fSScott Teel 		last_group = (last_block % stripesize) / r5or6_blocks_per_row;
42456b80b18fSScott Teel #endif
4246000ff7c2SStephen M. Cameron 		if (first_group != last_group)
42476b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
42486b80b18fSScott Teel 
42496b80b18fSScott Teel 		/* Verify request is in a single row of RAID 5/6 */
42506b80b18fSScott Teel #if BITS_PER_LONG == 32
42516b80b18fSScott Teel 		tmpdiv = first_block;
42526b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
42536b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row = tmpdiv;
42546b80b18fSScott Teel 		tmpdiv = last_block;
42556b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
42566b80b18fSScott Teel 		r5or6_last_row = r0_last_row = tmpdiv;
42576b80b18fSScott Teel #else
42586b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row =
42596b80b18fSScott Teel 						first_block / stripesize;
42606b80b18fSScott Teel 		r5or6_last_row = r0_last_row = last_block / stripesize;
42616b80b18fSScott Teel #endif
42626b80b18fSScott Teel 		if (r5or6_first_row != r5or6_last_row)
42636b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
42646b80b18fSScott Teel 
42656b80b18fSScott Teel 
42666b80b18fSScott Teel 		/* Verify request is in a single column */
42676b80b18fSScott Teel #if BITS_PER_LONG == 32
42686b80b18fSScott Teel 		tmpdiv = first_block;
42696b80b18fSScott Teel 		first_row_offset = do_div(tmpdiv, stripesize);
42706b80b18fSScott Teel 		tmpdiv = first_row_offset;
42716b80b18fSScott Teel 		first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
42726b80b18fSScott Teel 		r5or6_first_row_offset = first_row_offset;
42736b80b18fSScott Teel 		tmpdiv = last_block;
42746b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, stripesize);
42756b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
42766b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
42776b80b18fSScott Teel 		tmpdiv = r5or6_first_row_offset;
42786b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
42796b80b18fSScott Teel 		first_column = r5or6_first_column = tmpdiv;
42806b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
42816b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
42826b80b18fSScott Teel 		r5or6_last_column = tmpdiv;
42836b80b18fSScott Teel #else
42846b80b18fSScott Teel 		first_row_offset = r5or6_first_row_offset =
42856b80b18fSScott Teel 			(u32)((first_block % stripesize) %
42866b80b18fSScott Teel 						r5or6_blocks_per_row);
42876b80b18fSScott Teel 
42886b80b18fSScott Teel 		r5or6_last_row_offset =
42896b80b18fSScott Teel 			(u32)((last_block % stripesize) %
42906b80b18fSScott Teel 						r5or6_blocks_per_row);
42916b80b18fSScott Teel 
42926b80b18fSScott Teel 		first_column = r5or6_first_column =
42932b08b3e9SDon Brace 			r5or6_first_row_offset / le16_to_cpu(map->strip_size);
42946b80b18fSScott Teel 		r5or6_last_column =
42952b08b3e9SDon Brace 			r5or6_last_row_offset / le16_to_cpu(map->strip_size);
42966b80b18fSScott Teel #endif
42976b80b18fSScott Teel 		if (r5or6_first_column != r5or6_last_column)
42986b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
42996b80b18fSScott Teel 
43006b80b18fSScott Teel 		/* Request is eligible */
43016b80b18fSScott Teel 		map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
43022b08b3e9SDon Brace 			le16_to_cpu(map->row_cnt);
43036b80b18fSScott Teel 
43046b80b18fSScott Teel 		map_index = (first_group *
43052b08b3e9SDon Brace 			(le16_to_cpu(map->row_cnt) * total_disks_per_row)) +
43066b80b18fSScott Teel 			(map_row * total_disks_per_row) + first_column;
43076b80b18fSScott Teel 		break;
43086b80b18fSScott Teel 	default:
43096b80b18fSScott Teel 		return IO_ACCEL_INELIGIBLE;
4310283b4a9bSStephen M. Cameron 	}
43116b80b18fSScott Teel 
431207543e0cSStephen Cameron 	if (unlikely(map_index >= RAID_MAP_MAX_ENTRIES))
431307543e0cSStephen Cameron 		return IO_ACCEL_INELIGIBLE;
431407543e0cSStephen Cameron 
431503383736SDon Brace 	c->phys_disk = dev->phys_disk[map_index];
431603383736SDon Brace 
4317283b4a9bSStephen M. Cameron 	disk_handle = dd[map_index].ioaccel_handle;
43182b08b3e9SDon Brace 	disk_block = le64_to_cpu(map->disk_starting_blk) +
43192b08b3e9SDon Brace 			first_row * le16_to_cpu(map->strip_size) +
43202b08b3e9SDon Brace 			(first_row_offset - first_column *
43212b08b3e9SDon Brace 			le16_to_cpu(map->strip_size));
4322283b4a9bSStephen M. Cameron 	disk_block_cnt = block_cnt;
4323283b4a9bSStephen M. Cameron 
4324283b4a9bSStephen M. Cameron 	/* handle differing logical/physical block sizes */
4325283b4a9bSStephen M. Cameron 	if (map->phys_blk_shift) {
4326283b4a9bSStephen M. Cameron 		disk_block <<= map->phys_blk_shift;
4327283b4a9bSStephen M. Cameron 		disk_block_cnt <<= map->phys_blk_shift;
4328283b4a9bSStephen M. Cameron 	}
4329283b4a9bSStephen M. Cameron 	BUG_ON(disk_block_cnt > 0xffff);
4330283b4a9bSStephen M. Cameron 
4331283b4a9bSStephen M. Cameron 	/* build the new CDB for the physical disk I/O */
4332283b4a9bSStephen M. Cameron 	if (disk_block > 0xffffffff) {
4333283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_16 : READ_16;
4334283b4a9bSStephen M. Cameron 		cdb[1] = 0;
4335283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 56);
4336283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 48);
4337283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 40);
4338283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block >> 32);
4339283b4a9bSStephen M. Cameron 		cdb[6] = (u8) (disk_block >> 24);
4340283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block >> 16);
4341283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block >> 8);
4342283b4a9bSStephen M. Cameron 		cdb[9] = (u8) (disk_block);
4343283b4a9bSStephen M. Cameron 		cdb[10] = (u8) (disk_block_cnt >> 24);
4344283b4a9bSStephen M. Cameron 		cdb[11] = (u8) (disk_block_cnt >> 16);
4345283b4a9bSStephen M. Cameron 		cdb[12] = (u8) (disk_block_cnt >> 8);
4346283b4a9bSStephen M. Cameron 		cdb[13] = (u8) (disk_block_cnt);
4347283b4a9bSStephen M. Cameron 		cdb[14] = 0;
4348283b4a9bSStephen M. Cameron 		cdb[15] = 0;
4349283b4a9bSStephen M. Cameron 		cdb_len = 16;
4350283b4a9bSStephen M. Cameron 	} else {
4351283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
4352283b4a9bSStephen M. Cameron 		cdb[1] = 0;
4353283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 24);
4354283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 16);
4355283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 8);
4356283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block);
4357283b4a9bSStephen M. Cameron 		cdb[6] = 0;
4358283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block_cnt >> 8);
4359283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block_cnt);
4360283b4a9bSStephen M. Cameron 		cdb[9] = 0;
4361283b4a9bSStephen M. Cameron 		cdb_len = 10;
4362283b4a9bSStephen M. Cameron 	}
4363283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
436403383736SDon Brace 						dev->scsi3addr,
436503383736SDon Brace 						dev->phys_disk[map_index]);
4366283b4a9bSStephen M. Cameron }
4367283b4a9bSStephen M. Cameron 
436825163bd5SWebb Scales /*
436925163bd5SWebb Scales  * Submit commands down the "normal" RAID stack path
437025163bd5SWebb Scales  * All callers to hpsa_ciss_submit must check lockup_detected
437125163bd5SWebb Scales  * beforehand, before (opt.) and after calling cmd_alloc
437225163bd5SWebb Scales  */
4373574f05d3SStephen Cameron static int hpsa_ciss_submit(struct ctlr_info *h,
4374574f05d3SStephen Cameron 	struct CommandList *c, struct scsi_cmnd *cmd,
4375574f05d3SStephen Cameron 	unsigned char scsi3addr[])
4376edd16368SStephen M. Cameron {
4377edd16368SStephen M. Cameron 	cmd->host_scribble = (unsigned char *) c;
4378edd16368SStephen M. Cameron 	c->cmd_type = CMD_SCSI;
4379edd16368SStephen M. Cameron 	c->scsi_cmd = cmd;
4380edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;  /* unused in simple mode */
4381edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
4382f2405db8SDon Brace 	c->Header.tag = cpu_to_le64((c->cmdindex << DIRECT_LOOKUP_SHIFT));
4383edd16368SStephen M. Cameron 
4384edd16368SStephen M. Cameron 	/* Fill in the request block... */
4385edd16368SStephen M. Cameron 
4386edd16368SStephen M. Cameron 	c->Request.Timeout = 0;
4387edd16368SStephen M. Cameron 	BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
4388edd16368SStephen M. Cameron 	c->Request.CDBLen = cmd->cmd_len;
4389edd16368SStephen M. Cameron 	memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
4390edd16368SStephen M. Cameron 	switch (cmd->sc_data_direction) {
4391edd16368SStephen M. Cameron 	case DMA_TO_DEVICE:
4392a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4393a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_WRITE);
4394edd16368SStephen M. Cameron 		break;
4395edd16368SStephen M. Cameron 	case DMA_FROM_DEVICE:
4396a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4397a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_READ);
4398edd16368SStephen M. Cameron 		break;
4399edd16368SStephen M. Cameron 	case DMA_NONE:
4400a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4401a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_NONE);
4402edd16368SStephen M. Cameron 		break;
4403edd16368SStephen M. Cameron 	case DMA_BIDIRECTIONAL:
4404edd16368SStephen M. Cameron 		/* This can happen if a buggy application does a scsi passthru
4405edd16368SStephen M. Cameron 		 * and sets both inlen and outlen to non-zero. ( see
4406edd16368SStephen M. Cameron 		 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
4407edd16368SStephen M. Cameron 		 */
4408edd16368SStephen M. Cameron 
4409a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4410a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_RSVD);
4411edd16368SStephen M. Cameron 		/* This is technically wrong, and hpsa controllers should
4412edd16368SStephen M. Cameron 		 * reject it with CMD_INVALID, which is the most correct
4413edd16368SStephen M. Cameron 		 * response, but non-fibre backends appear to let it
4414edd16368SStephen M. Cameron 		 * slide by, and give the same results as if this field
4415edd16368SStephen M. Cameron 		 * were set correctly.  Either way is acceptable for
4416edd16368SStephen M. Cameron 		 * our purposes here.
4417edd16368SStephen M. Cameron 		 */
4418edd16368SStephen M. Cameron 
4419edd16368SStephen M. Cameron 		break;
4420edd16368SStephen M. Cameron 
4421edd16368SStephen M. Cameron 	default:
4422edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4423edd16368SStephen M. Cameron 			cmd->sc_data_direction);
4424edd16368SStephen M. Cameron 		BUG();
4425edd16368SStephen M. Cameron 		break;
4426edd16368SStephen M. Cameron 	}
4427edd16368SStephen M. Cameron 
442833a2ffceSStephen M. Cameron 	if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
4429edd16368SStephen M. Cameron 		cmd_free(h, c);
4430edd16368SStephen M. Cameron 		return SCSI_MLQUEUE_HOST_BUSY;
4431edd16368SStephen M. Cameron 	}
4432edd16368SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
4433edd16368SStephen M. Cameron 	/* the cmd'll come back via intr handler in complete_scsi_command()  */
4434edd16368SStephen M. Cameron 	return 0;
4435edd16368SStephen M. Cameron }
4436edd16368SStephen M. Cameron 
4437360c73bdSStephen Cameron static void hpsa_cmd_init(struct ctlr_info *h, int index,
4438360c73bdSStephen Cameron 				struct CommandList *c)
4439360c73bdSStephen Cameron {
4440360c73bdSStephen Cameron 	dma_addr_t cmd_dma_handle, err_dma_handle;
4441360c73bdSStephen Cameron 
4442360c73bdSStephen Cameron 	/* Zero out all of commandlist except the last field, refcount */
4443360c73bdSStephen Cameron 	memset(c, 0, offsetof(struct CommandList, refcount));
4444360c73bdSStephen Cameron 	c->Header.tag = cpu_to_le64((u64) (index << DIRECT_LOOKUP_SHIFT));
4445360c73bdSStephen Cameron 	cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
4446360c73bdSStephen Cameron 	c->err_info = h->errinfo_pool + index;
4447360c73bdSStephen Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
4448360c73bdSStephen Cameron 	err_dma_handle = h->errinfo_pool_dhandle
4449360c73bdSStephen Cameron 	    + index * sizeof(*c->err_info);
4450360c73bdSStephen Cameron 	c->cmdindex = index;
4451360c73bdSStephen Cameron 	c->busaddr = (u32) cmd_dma_handle;
4452360c73bdSStephen Cameron 	c->ErrDesc.Addr = cpu_to_le64((u64) err_dma_handle);
4453360c73bdSStephen Cameron 	c->ErrDesc.Len = cpu_to_le32((u32) sizeof(*c->err_info));
4454360c73bdSStephen Cameron 	c->h = h;
4455360c73bdSStephen Cameron }
4456360c73bdSStephen Cameron 
4457360c73bdSStephen Cameron static void hpsa_preinitialize_commands(struct ctlr_info *h)
4458360c73bdSStephen Cameron {
4459360c73bdSStephen Cameron 	int i;
4460360c73bdSStephen Cameron 
4461360c73bdSStephen Cameron 	for (i = 0; i < h->nr_cmds; i++) {
4462360c73bdSStephen Cameron 		struct CommandList *c = h->cmd_pool + i;
4463360c73bdSStephen Cameron 
4464360c73bdSStephen Cameron 		hpsa_cmd_init(h, i, c);
4465360c73bdSStephen Cameron 		atomic_set(&c->refcount, 0);
4466360c73bdSStephen Cameron 	}
4467360c73bdSStephen Cameron }
4468360c73bdSStephen Cameron 
4469360c73bdSStephen Cameron static inline void hpsa_cmd_partial_init(struct ctlr_info *h, int index,
4470360c73bdSStephen Cameron 				struct CommandList *c)
4471360c73bdSStephen Cameron {
4472360c73bdSStephen Cameron 	dma_addr_t cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
4473360c73bdSStephen Cameron 
4474360c73bdSStephen Cameron 	memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
4475360c73bdSStephen Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
4476360c73bdSStephen Cameron 	c->busaddr = (u32) cmd_dma_handle;
4477360c73bdSStephen Cameron }
4478360c73bdSStephen Cameron 
4479592a0ad5SWebb Scales static int hpsa_ioaccel_submit(struct ctlr_info *h,
4480592a0ad5SWebb Scales 		struct CommandList *c, struct scsi_cmnd *cmd,
4481592a0ad5SWebb Scales 		unsigned char *scsi3addr)
4482592a0ad5SWebb Scales {
4483592a0ad5SWebb Scales 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4484592a0ad5SWebb Scales 	int rc = IO_ACCEL_INELIGIBLE;
4485592a0ad5SWebb Scales 
4486592a0ad5SWebb Scales 	cmd->host_scribble = (unsigned char *) c;
4487592a0ad5SWebb Scales 
4488592a0ad5SWebb Scales 	if (dev->offload_enabled) {
4489592a0ad5SWebb Scales 		hpsa_cmd_init(h, c->cmdindex, c);
4490592a0ad5SWebb Scales 		c->cmd_type = CMD_SCSI;
4491592a0ad5SWebb Scales 		c->scsi_cmd = cmd;
4492592a0ad5SWebb Scales 		rc = hpsa_scsi_ioaccel_raid_map(h, c);
4493592a0ad5SWebb Scales 		if (rc < 0)     /* scsi_dma_map failed. */
4494592a0ad5SWebb Scales 			rc = SCSI_MLQUEUE_HOST_BUSY;
4495a3144e0bSJoe Handzik 	} else if (dev->hba_ioaccel_enabled) {
4496592a0ad5SWebb Scales 		hpsa_cmd_init(h, c->cmdindex, c);
4497592a0ad5SWebb Scales 		c->cmd_type = CMD_SCSI;
4498592a0ad5SWebb Scales 		c->scsi_cmd = cmd;
4499592a0ad5SWebb Scales 		rc = hpsa_scsi_ioaccel_direct_map(h, c);
4500592a0ad5SWebb Scales 		if (rc < 0)     /* scsi_dma_map failed. */
4501592a0ad5SWebb Scales 			rc = SCSI_MLQUEUE_HOST_BUSY;
4502592a0ad5SWebb Scales 	}
4503592a0ad5SWebb Scales 	return rc;
4504592a0ad5SWebb Scales }
4505592a0ad5SWebb Scales 
4506080ef1ccSDon Brace static void hpsa_command_resubmit_worker(struct work_struct *work)
4507080ef1ccSDon Brace {
4508080ef1ccSDon Brace 	struct scsi_cmnd *cmd;
4509080ef1ccSDon Brace 	struct hpsa_scsi_dev_t *dev;
4510080ef1ccSDon Brace 	struct CommandList *c =
4511080ef1ccSDon Brace 			container_of(work, struct CommandList, work);
4512080ef1ccSDon Brace 
4513080ef1ccSDon Brace 	cmd = c->scsi_cmd;
4514080ef1ccSDon Brace 	dev = cmd->device->hostdata;
4515080ef1ccSDon Brace 	if (!dev) {
4516080ef1ccSDon Brace 		cmd->result = DID_NO_CONNECT << 16;
4517592a0ad5SWebb Scales 		cmd_free(c->h, c);
4518080ef1ccSDon Brace 		cmd->scsi_done(cmd);
4519080ef1ccSDon Brace 		return;
4520080ef1ccSDon Brace 	}
4521592a0ad5SWebb Scales 	if (c->cmd_type == CMD_IOACCEL2) {
4522592a0ad5SWebb Scales 		struct ctlr_info *h = c->h;
4523592a0ad5SWebb Scales 		struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
4524592a0ad5SWebb Scales 		int rc;
4525592a0ad5SWebb Scales 
4526592a0ad5SWebb Scales 		if (c2->error_data.serv_response ==
4527592a0ad5SWebb Scales 				IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL) {
4528592a0ad5SWebb Scales 			rc = hpsa_ioaccel_submit(h, c, cmd, dev->scsi3addr);
4529592a0ad5SWebb Scales 			if (rc == 0)
4530592a0ad5SWebb Scales 				return;
4531592a0ad5SWebb Scales 			if (rc == SCSI_MLQUEUE_HOST_BUSY) {
4532592a0ad5SWebb Scales 				/*
4533592a0ad5SWebb Scales 				 * If we get here, it means dma mapping failed.
4534592a0ad5SWebb Scales 				 * Try again via scsi mid layer, which will
4535592a0ad5SWebb Scales 				 * then get SCSI_MLQUEUE_HOST_BUSY.
4536592a0ad5SWebb Scales 				 */
4537592a0ad5SWebb Scales 				cmd->result = DID_IMM_RETRY << 16;
4538592a0ad5SWebb Scales 				cmd->scsi_done(cmd);
4539592a0ad5SWebb Scales 				cmd_free(h, c);	/* FIX-ME:  on merge, change
4540592a0ad5SWebb Scales 						 * to cmd_tagged_free() and
4541592a0ad5SWebb Scales 						 * ultimately to
4542592a0ad5SWebb Scales 						 * hpsa_cmd_free_and_done(). */
4543592a0ad5SWebb Scales 				return;
4544592a0ad5SWebb Scales 			}
4545592a0ad5SWebb Scales 			/* else, fall thru and resubmit down CISS path */
4546592a0ad5SWebb Scales 		}
4547592a0ad5SWebb Scales 	}
4548360c73bdSStephen Cameron 	hpsa_cmd_partial_init(c->h, c->cmdindex, c);
4549080ef1ccSDon Brace 	if (hpsa_ciss_submit(c->h, c, cmd, dev->scsi3addr)) {
4550080ef1ccSDon Brace 		/*
4551080ef1ccSDon Brace 		 * If we get here, it means dma mapping failed. Try
4552080ef1ccSDon Brace 		 * again via scsi mid layer, which will then get
4553080ef1ccSDon Brace 		 * SCSI_MLQUEUE_HOST_BUSY.
4554592a0ad5SWebb Scales 		 *
4555592a0ad5SWebb Scales 		 * hpsa_ciss_submit will have already freed c
4556592a0ad5SWebb Scales 		 * if it encountered a dma mapping failure.
4557080ef1ccSDon Brace 		 */
4558080ef1ccSDon Brace 		cmd->result = DID_IMM_RETRY << 16;
4559080ef1ccSDon Brace 		cmd->scsi_done(cmd);
4560080ef1ccSDon Brace 	}
4561080ef1ccSDon Brace }
4562080ef1ccSDon Brace 
4563574f05d3SStephen Cameron /* Running in struct Scsi_Host->host_lock less mode */
4564574f05d3SStephen Cameron static int hpsa_scsi_queue_command(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
4565574f05d3SStephen Cameron {
4566574f05d3SStephen Cameron 	struct ctlr_info *h;
4567574f05d3SStephen Cameron 	struct hpsa_scsi_dev_t *dev;
4568574f05d3SStephen Cameron 	unsigned char scsi3addr[8];
4569574f05d3SStephen Cameron 	struct CommandList *c;
4570574f05d3SStephen Cameron 	int rc = 0;
4571574f05d3SStephen Cameron 
4572574f05d3SStephen Cameron 	/* Get the ptr to our adapter structure out of cmd->host. */
4573574f05d3SStephen Cameron 	h = sdev_to_hba(cmd->device);
4574574f05d3SStephen Cameron 	dev = cmd->device->hostdata;
4575574f05d3SStephen Cameron 	if (!dev) {
4576574f05d3SStephen Cameron 		cmd->result = DID_NO_CONNECT << 16;
4577574f05d3SStephen Cameron 		cmd->scsi_done(cmd);
4578574f05d3SStephen Cameron 		return 0;
4579574f05d3SStephen Cameron 	}
4580574f05d3SStephen Cameron 	memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
4581574f05d3SStephen Cameron 
4582574f05d3SStephen Cameron 	if (unlikely(lockup_detected(h))) {
458325163bd5SWebb Scales 		cmd->result = DID_NO_CONNECT << 16;
4584574f05d3SStephen Cameron 		cmd->scsi_done(cmd);
4585574f05d3SStephen Cameron 		return 0;
4586574f05d3SStephen Cameron 	}
4587574f05d3SStephen Cameron 	c = cmd_alloc(h);
4588*bf43caf3SRobert Elliott 
4589407863cbSStephen Cameron 	if (unlikely(lockup_detected(h))) {
459025163bd5SWebb Scales 		cmd->result = DID_NO_CONNECT << 16;
4591407863cbSStephen Cameron 		cmd_free(h, c);
4592407863cbSStephen Cameron 		cmd->scsi_done(cmd);
4593407863cbSStephen Cameron 		return 0;
4594407863cbSStephen Cameron 	}
4595574f05d3SStephen Cameron 
4596407863cbSStephen Cameron 	/*
4597407863cbSStephen Cameron 	 * Call alternate submit routine for I/O accelerated commands.
4598574f05d3SStephen Cameron 	 * Retries always go down the normal I/O path.
4599574f05d3SStephen Cameron 	 */
4600574f05d3SStephen Cameron 	if (likely(cmd->retries == 0 &&
4601574f05d3SStephen Cameron 		cmd->request->cmd_type == REQ_TYPE_FS &&
4602574f05d3SStephen Cameron 		h->acciopath_status)) {
4603592a0ad5SWebb Scales 		rc = hpsa_ioaccel_submit(h, c, cmd, scsi3addr);
4604574f05d3SStephen Cameron 		if (rc == 0)
4605592a0ad5SWebb Scales 			return 0;
4606592a0ad5SWebb Scales 		if (rc == SCSI_MLQUEUE_HOST_BUSY) {
4607592a0ad5SWebb Scales 			cmd_free(h, c);	/* FIX-ME:  on merge, change to
4608592a0ad5SWebb Scales 					 * cmd_tagged_free(), and ultimately
4609592a0ad5SWebb Scales 					 * to hpsa_cmd_resolve_and_free(). */
4610574f05d3SStephen Cameron 			return SCSI_MLQUEUE_HOST_BUSY;
4611574f05d3SStephen Cameron 		}
4612574f05d3SStephen Cameron 	}
4613574f05d3SStephen Cameron 	return hpsa_ciss_submit(h, c, cmd, scsi3addr);
4614574f05d3SStephen Cameron }
4615574f05d3SStephen Cameron 
46168ebc9248SWebb Scales static void hpsa_scan_complete(struct ctlr_info *h)
46175f389360SStephen M. Cameron {
46185f389360SStephen M. Cameron 	unsigned long flags;
46195f389360SStephen M. Cameron 
46205f389360SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
46215f389360SStephen M. Cameron 	h->scan_finished = 1;
46225f389360SStephen M. Cameron 	wake_up_all(&h->scan_wait_queue);
46235f389360SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
46245f389360SStephen M. Cameron }
46255f389360SStephen M. Cameron 
4626a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *sh)
4627a08a8471SStephen M. Cameron {
4628a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
4629a08a8471SStephen M. Cameron 	unsigned long flags;
4630a08a8471SStephen M. Cameron 
46318ebc9248SWebb Scales 	/*
46328ebc9248SWebb Scales 	 * Don't let rescans be initiated on a controller known to be locked
46338ebc9248SWebb Scales 	 * up.  If the controller locks up *during* a rescan, that thread is
46348ebc9248SWebb Scales 	 * probably hosed, but at least we can prevent new rescan threads from
46358ebc9248SWebb Scales 	 * piling up on a locked up controller.
46368ebc9248SWebb Scales 	 */
46378ebc9248SWebb Scales 	if (unlikely(lockup_detected(h)))
46388ebc9248SWebb Scales 		return hpsa_scan_complete(h);
46395f389360SStephen M. Cameron 
4640a08a8471SStephen M. Cameron 	/* wait until any scan already in progress is finished. */
4641a08a8471SStephen M. Cameron 	while (1) {
4642a08a8471SStephen M. Cameron 		spin_lock_irqsave(&h->scan_lock, flags);
4643a08a8471SStephen M. Cameron 		if (h->scan_finished)
4644a08a8471SStephen M. Cameron 			break;
4645a08a8471SStephen M. Cameron 		spin_unlock_irqrestore(&h->scan_lock, flags);
4646a08a8471SStephen M. Cameron 		wait_event(h->scan_wait_queue, h->scan_finished);
4647a08a8471SStephen M. Cameron 		/* Note: We don't need to worry about a race between this
4648a08a8471SStephen M. Cameron 		 * thread and driver unload because the midlayer will
4649a08a8471SStephen M. Cameron 		 * have incremented the reference count, so unload won't
4650a08a8471SStephen M. Cameron 		 * happen if we're in here.
4651a08a8471SStephen M. Cameron 		 */
4652a08a8471SStephen M. Cameron 	}
4653a08a8471SStephen M. Cameron 	h->scan_finished = 0; /* mark scan as in progress */
4654a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4655a08a8471SStephen M. Cameron 
46568ebc9248SWebb Scales 	if (unlikely(lockup_detected(h)))
46578ebc9248SWebb Scales 		return hpsa_scan_complete(h);
46585f389360SStephen M. Cameron 
4659a08a8471SStephen M. Cameron 	hpsa_update_scsi_devices(h, h->scsi_host->host_no);
4660a08a8471SStephen M. Cameron 
46618ebc9248SWebb Scales 	hpsa_scan_complete(h);
4662a08a8471SStephen M. Cameron }
4663a08a8471SStephen M. Cameron 
46647c0a0229SDon Brace static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth)
46657c0a0229SDon Brace {
466603383736SDon Brace 	struct hpsa_scsi_dev_t *logical_drive = sdev->hostdata;
466703383736SDon Brace 
466803383736SDon Brace 	if (!logical_drive)
466903383736SDon Brace 		return -ENODEV;
46707c0a0229SDon Brace 
46717c0a0229SDon Brace 	if (qdepth < 1)
46727c0a0229SDon Brace 		qdepth = 1;
467303383736SDon Brace 	else if (qdepth > logical_drive->queue_depth)
467403383736SDon Brace 		qdepth = logical_drive->queue_depth;
467503383736SDon Brace 
467603383736SDon Brace 	return scsi_change_queue_depth(sdev, qdepth);
46777c0a0229SDon Brace }
46787c0a0229SDon Brace 
4679a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
4680a08a8471SStephen M. Cameron 	unsigned long elapsed_time)
4681a08a8471SStephen M. Cameron {
4682a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
4683a08a8471SStephen M. Cameron 	unsigned long flags;
4684a08a8471SStephen M. Cameron 	int finished;
4685a08a8471SStephen M. Cameron 
4686a08a8471SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
4687a08a8471SStephen M. Cameron 	finished = h->scan_finished;
4688a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4689a08a8471SStephen M. Cameron 	return finished;
4690a08a8471SStephen M. Cameron }
4691a08a8471SStephen M. Cameron 
4692edd16368SStephen M. Cameron static void hpsa_unregister_scsi(struct ctlr_info *h)
4693edd16368SStephen M. Cameron {
4694edd16368SStephen M. Cameron 	/* we are being forcibly unloaded, and may not refuse. */
4695edd16368SStephen M. Cameron 	scsi_remove_host(h->scsi_host);
4696edd16368SStephen M. Cameron 	scsi_host_put(h->scsi_host);
4697edd16368SStephen M. Cameron 	h->scsi_host = NULL;
4698edd16368SStephen M. Cameron }
4699edd16368SStephen M. Cameron 
4700edd16368SStephen M. Cameron static int hpsa_register_scsi(struct ctlr_info *h)
4701edd16368SStephen M. Cameron {
4702b705690dSStephen M. Cameron 	struct Scsi_Host *sh;
4703b705690dSStephen M. Cameron 	int error;
4704edd16368SStephen M. Cameron 
4705b705690dSStephen M. Cameron 	sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
4706b705690dSStephen M. Cameron 	if (sh == NULL)
4707b705690dSStephen M. Cameron 		goto fail;
4708b705690dSStephen M. Cameron 
4709b705690dSStephen M. Cameron 	sh->io_port = 0;
4710b705690dSStephen M. Cameron 	sh->n_io_port = 0;
4711b705690dSStephen M. Cameron 	sh->this_id = -1;
4712b705690dSStephen M. Cameron 	sh->max_channel = 3;
4713b705690dSStephen M. Cameron 	sh->max_cmd_len = MAX_COMMAND_SIZE;
4714b705690dSStephen M. Cameron 	sh->max_lun = HPSA_MAX_LUN;
4715b705690dSStephen M. Cameron 	sh->max_id = HPSA_MAX_LUN;
471641ce4c35SStephen Cameron 	sh->can_queue = h->nr_cmds - HPSA_NRESERVED_CMDS;
4717d54c5c24SStephen Cameron 	sh->cmd_per_lun = sh->can_queue;
4718b705690dSStephen M. Cameron 	sh->sg_tablesize = h->maxsgentries;
4719b705690dSStephen M. Cameron 	h->scsi_host = sh;
4720b705690dSStephen M. Cameron 	sh->hostdata[0] = (unsigned long) h;
4721b705690dSStephen M. Cameron 	sh->irq = h->intr[h->intr_mode];
4722b705690dSStephen M. Cameron 	sh->unique_id = sh->irq;
4723b705690dSStephen M. Cameron 	error = scsi_add_host(sh, &h->pdev->dev);
4724b705690dSStephen M. Cameron 	if (error)
4725b705690dSStephen M. Cameron 		goto fail_host_put;
4726b705690dSStephen M. Cameron 	scsi_scan_host(sh);
4727b705690dSStephen M. Cameron 	return 0;
4728b705690dSStephen M. Cameron 
4729b705690dSStephen M. Cameron  fail_host_put:
4730b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_add_host"
4731b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
4732b705690dSStephen M. Cameron 	scsi_host_put(sh);
4733b705690dSStephen M. Cameron 	return error;
4734b705690dSStephen M. Cameron  fail:
4735b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_host_alloc"
4736b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
4737b705690dSStephen M. Cameron 	return -ENOMEM;
4738edd16368SStephen M. Cameron }
4739edd16368SStephen M. Cameron 
4740edd16368SStephen M. Cameron static int wait_for_device_to_become_ready(struct ctlr_info *h,
4741edd16368SStephen M. Cameron 	unsigned char lunaddr[])
4742edd16368SStephen M. Cameron {
47438919358eSTomas Henzl 	int rc;
4744edd16368SStephen M. Cameron 	int count = 0;
4745edd16368SStephen M. Cameron 	int waittime = 1; /* seconds */
4746edd16368SStephen M. Cameron 	struct CommandList *c;
4747edd16368SStephen M. Cameron 
474845fcb86eSStephen Cameron 	c = cmd_alloc(h);
4749edd16368SStephen M. Cameron 
4750edd16368SStephen M. Cameron 	/* Send test unit ready until device ready, or give up. */
4751edd16368SStephen M. Cameron 	while (count < HPSA_TUR_RETRY_LIMIT) {
4752edd16368SStephen M. Cameron 
4753edd16368SStephen M. Cameron 		/* Wait for a bit.  do this first, because if we send
4754edd16368SStephen M. Cameron 		 * the TUR right away, the reset will just abort it.
4755edd16368SStephen M. Cameron 		 */
4756edd16368SStephen M. Cameron 		msleep(1000 * waittime);
4757edd16368SStephen M. Cameron 		count++;
47588919358eSTomas Henzl 		rc = 0; /* Device ready. */
4759edd16368SStephen M. Cameron 
4760edd16368SStephen M. Cameron 		/* Increase wait time with each try, up to a point. */
4761edd16368SStephen M. Cameron 		if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
4762edd16368SStephen M. Cameron 			waittime = waittime * 2;
4763edd16368SStephen M. Cameron 
4764a2dac136SStephen M. Cameron 		/* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
4765a2dac136SStephen M. Cameron 		(void) fill_cmd(c, TEST_UNIT_READY, h,
4766a2dac136SStephen M. Cameron 				NULL, 0, 0, lunaddr, TYPE_CMD);
476725163bd5SWebb Scales 		rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
476825163bd5SWebb Scales 						NO_TIMEOUT);
476925163bd5SWebb Scales 		if (rc)
477025163bd5SWebb Scales 			goto do_it_again;
4771edd16368SStephen M. Cameron 		/* no unmap needed here because no data xfer. */
4772edd16368SStephen M. Cameron 
4773edd16368SStephen M. Cameron 		if (c->err_info->CommandStatus == CMD_SUCCESS)
4774edd16368SStephen M. Cameron 			break;
4775edd16368SStephen M. Cameron 
4776edd16368SStephen M. Cameron 		if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
4777edd16368SStephen M. Cameron 			c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
4778edd16368SStephen M. Cameron 			(c->err_info->SenseInfo[2] == NO_SENSE ||
4779edd16368SStephen M. Cameron 			c->err_info->SenseInfo[2] == UNIT_ATTENTION))
4780edd16368SStephen M. Cameron 			break;
478125163bd5SWebb Scales do_it_again:
4782edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "waiting %d secs "
4783edd16368SStephen M. Cameron 			"for device to become ready.\n", waittime);
4784edd16368SStephen M. Cameron 		rc = 1; /* device not ready. */
4785edd16368SStephen M. Cameron 	}
4786edd16368SStephen M. Cameron 
4787edd16368SStephen M. Cameron 	if (rc)
4788edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "giving up on device.\n");
4789edd16368SStephen M. Cameron 	else
4790edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "device is ready.\n");
4791edd16368SStephen M. Cameron 
479245fcb86eSStephen Cameron 	cmd_free(h, c);
4793edd16368SStephen M. Cameron 	return rc;
4794edd16368SStephen M. Cameron }
4795edd16368SStephen M. Cameron 
4796edd16368SStephen M. Cameron /* Need at least one of these error handlers to keep ../scsi/hosts.c from
4797edd16368SStephen M. Cameron  * complaining.  Doing a host- or bus-reset can't do anything good here.
4798edd16368SStephen M. Cameron  */
4799edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
4800edd16368SStephen M. Cameron {
4801edd16368SStephen M. Cameron 	int rc;
4802edd16368SStephen M. Cameron 	struct ctlr_info *h;
4803edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
4804edd16368SStephen M. Cameron 
4805edd16368SStephen M. Cameron 	/* find the controller to which the command to be aborted was sent */
4806edd16368SStephen M. Cameron 	h = sdev_to_hba(scsicmd->device);
4807edd16368SStephen M. Cameron 	if (h == NULL) /* paranoia */
4808edd16368SStephen M. Cameron 		return FAILED;
4809e345893bSDon Brace 
4810e345893bSDon Brace 	if (lockup_detected(h))
4811e345893bSDon Brace 		return FAILED;
4812e345893bSDon Brace 
4813edd16368SStephen M. Cameron 	dev = scsicmd->device->hostdata;
4814edd16368SStephen M. Cameron 	if (!dev) {
4815edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
4816edd16368SStephen M. Cameron 			"device lookup failed.\n");
4817edd16368SStephen M. Cameron 		return FAILED;
4818edd16368SStephen M. Cameron 	}
481925163bd5SWebb Scales 
482025163bd5SWebb Scales 	/* if controller locked up, we can guarantee command won't complete */
482125163bd5SWebb Scales 	if (lockup_detected(h)) {
482225163bd5SWebb Scales 		dev_warn(&h->pdev->dev,
482325163bd5SWebb Scales 			"scsi %d:%d:%d:%d RESET FAILED, lockup detected\n",
482425163bd5SWebb Scales 			h->scsi_host->host_no, dev->bus, dev->target,
482525163bd5SWebb Scales 			dev->lun);
482625163bd5SWebb Scales 		return FAILED;
482725163bd5SWebb Scales 	}
482825163bd5SWebb Scales 
482925163bd5SWebb Scales 	/* this reset request might be the result of a lockup; check */
483025163bd5SWebb Scales 	if (detect_controller_lockup(h)) {
483125163bd5SWebb Scales 		dev_warn(&h->pdev->dev,
483225163bd5SWebb Scales 			 "scsi %d:%d:%d:%d RESET FAILED, new lockup detected\n",
483325163bd5SWebb Scales 			 h->scsi_host->host_no, dev->bus, dev->target,
483425163bd5SWebb Scales 			 dev->lun);
483525163bd5SWebb Scales 		return FAILED;
483625163bd5SWebb Scales 	}
483725163bd5SWebb Scales 
483825163bd5SWebb Scales 	hpsa_show_dev_msg(KERN_WARNING, h, dev, "resetting");
483925163bd5SWebb Scales 
4840edd16368SStephen M. Cameron 	/* send a reset to the SCSI LUN which the command was sent to */
484125163bd5SWebb Scales 	rc = hpsa_send_reset(h, dev->scsi3addr, HPSA_RESET_TYPE_LUN,
484225163bd5SWebb Scales 			     DEFAULT_REPLY_QUEUE);
4843edd16368SStephen M. Cameron 	if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
4844edd16368SStephen M. Cameron 		return SUCCESS;
4845edd16368SStephen M. Cameron 
484625163bd5SWebb Scales 	dev_warn(&h->pdev->dev,
484725163bd5SWebb Scales 		"scsi %d:%d:%d:%d reset failed\n",
484825163bd5SWebb Scales 		h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
4849edd16368SStephen M. Cameron 	return FAILED;
4850edd16368SStephen M. Cameron }
4851edd16368SStephen M. Cameron 
48526cba3f19SStephen M. Cameron static void swizzle_abort_tag(u8 *tag)
48536cba3f19SStephen M. Cameron {
48546cba3f19SStephen M. Cameron 	u8 original_tag[8];
48556cba3f19SStephen M. Cameron 
48566cba3f19SStephen M. Cameron 	memcpy(original_tag, tag, 8);
48576cba3f19SStephen M. Cameron 	tag[0] = original_tag[3];
48586cba3f19SStephen M. Cameron 	tag[1] = original_tag[2];
48596cba3f19SStephen M. Cameron 	tag[2] = original_tag[1];
48606cba3f19SStephen M. Cameron 	tag[3] = original_tag[0];
48616cba3f19SStephen M. Cameron 	tag[4] = original_tag[7];
48626cba3f19SStephen M. Cameron 	tag[5] = original_tag[6];
48636cba3f19SStephen M. Cameron 	tag[6] = original_tag[5];
48646cba3f19SStephen M. Cameron 	tag[7] = original_tag[4];
48656cba3f19SStephen M. Cameron }
48666cba3f19SStephen M. Cameron 
486717eb87d2SScott Teel static void hpsa_get_tag(struct ctlr_info *h,
48682b08b3e9SDon Brace 	struct CommandList *c, __le32 *taglower, __le32 *tagupper)
486917eb87d2SScott Teel {
48702b08b3e9SDon Brace 	u64 tag;
487117eb87d2SScott Teel 	if (c->cmd_type == CMD_IOACCEL1) {
487217eb87d2SScott Teel 		struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
487317eb87d2SScott Teel 			&h->ioaccel_cmd_pool[c->cmdindex];
48742b08b3e9SDon Brace 		tag = le64_to_cpu(cm1->tag);
48752b08b3e9SDon Brace 		*tagupper = cpu_to_le32(tag >> 32);
48762b08b3e9SDon Brace 		*taglower = cpu_to_le32(tag);
487754b6e9e9SScott Teel 		return;
487854b6e9e9SScott Teel 	}
487954b6e9e9SScott Teel 	if (c->cmd_type == CMD_IOACCEL2) {
488054b6e9e9SScott Teel 		struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
488154b6e9e9SScott Teel 			&h->ioaccel2_cmd_pool[c->cmdindex];
4882dd0e19f3SScott Teel 		/* upper tag not used in ioaccel2 mode */
4883dd0e19f3SScott Teel 		memset(tagupper, 0, sizeof(*tagupper));
4884dd0e19f3SScott Teel 		*taglower = cm2->Tag;
488554b6e9e9SScott Teel 		return;
488654b6e9e9SScott Teel 	}
48872b08b3e9SDon Brace 	tag = le64_to_cpu(c->Header.tag);
48882b08b3e9SDon Brace 	*tagupper = cpu_to_le32(tag >> 32);
48892b08b3e9SDon Brace 	*taglower = cpu_to_le32(tag);
489017eb87d2SScott Teel }
489154b6e9e9SScott Teel 
489275167d2cSStephen M. Cameron static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
48939b5c48c2SStephen Cameron 	struct CommandList *abort, int reply_queue)
489475167d2cSStephen M. Cameron {
489575167d2cSStephen M. Cameron 	int rc = IO_OK;
489675167d2cSStephen M. Cameron 	struct CommandList *c;
489775167d2cSStephen M. Cameron 	struct ErrorInfo *ei;
48982b08b3e9SDon Brace 	__le32 tagupper, taglower;
489975167d2cSStephen M. Cameron 
490045fcb86eSStephen Cameron 	c = cmd_alloc(h);
490175167d2cSStephen M. Cameron 
4902a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no buffer to map */
49039b5c48c2SStephen Cameron 	(void) fill_cmd(c, HPSA_ABORT_MSG, h, &abort->Header.tag,
4904a2dac136SStephen M. Cameron 		0, 0, scsi3addr, TYPE_MSG);
49059b5c48c2SStephen Cameron 	if (h->needs_abort_tags_swizzled)
49066cba3f19SStephen M. Cameron 		swizzle_abort_tag(&c->Request.CDB[4]);
490725163bd5SWebb Scales 	(void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
490817eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
490925163bd5SWebb Scales 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd(abort) completed.\n",
491017eb87d2SScott Teel 		__func__, tagupper, taglower);
491175167d2cSStephen M. Cameron 	/* no unmap needed here because no data xfer. */
491275167d2cSStephen M. Cameron 
491375167d2cSStephen M. Cameron 	ei = c->err_info;
491475167d2cSStephen M. Cameron 	switch (ei->CommandStatus) {
491575167d2cSStephen M. Cameron 	case CMD_SUCCESS:
491675167d2cSStephen M. Cameron 		break;
49179437ac43SStephen Cameron 	case CMD_TMF_STATUS:
49189437ac43SStephen Cameron 		rc = hpsa_evaluate_tmf_status(h, c);
49199437ac43SStephen Cameron 		break;
492075167d2cSStephen M. Cameron 	case CMD_UNABORTABLE: /* Very common, don't make noise. */
492175167d2cSStephen M. Cameron 		rc = -1;
492275167d2cSStephen M. Cameron 		break;
492375167d2cSStephen M. Cameron 	default:
492475167d2cSStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
492517eb87d2SScott Teel 			__func__, tagupper, taglower);
4926d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
492775167d2cSStephen M. Cameron 		rc = -1;
492875167d2cSStephen M. Cameron 		break;
492975167d2cSStephen M. Cameron 	}
493045fcb86eSStephen Cameron 	cmd_free(h, c);
4931dd0e19f3SScott Teel 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n",
4932dd0e19f3SScott Teel 		__func__, tagupper, taglower);
493375167d2cSStephen M. Cameron 	return rc;
493475167d2cSStephen M. Cameron }
493575167d2cSStephen M. Cameron 
493654b6e9e9SScott Teel /* ioaccel2 path firmware cannot handle abort task requests.
493754b6e9e9SScott Teel  * Change abort requests to physical target reset, and send to the
493854b6e9e9SScott Teel  * address of the physical disk used for the ioaccel 2 command.
493954b6e9e9SScott Teel  * Return 0 on success (IO_OK)
494054b6e9e9SScott Teel  *	 -1 on failure
494154b6e9e9SScott Teel  */
494254b6e9e9SScott Teel 
494354b6e9e9SScott Teel static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
494425163bd5SWebb Scales 	unsigned char *scsi3addr, struct CommandList *abort, int reply_queue)
494554b6e9e9SScott Teel {
494654b6e9e9SScott Teel 	int rc = IO_OK;
494754b6e9e9SScott Teel 	struct scsi_cmnd *scmd; /* scsi command within request being aborted */
494854b6e9e9SScott Teel 	struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
494954b6e9e9SScott Teel 	unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
495054b6e9e9SScott Teel 	unsigned char *psa = &phys_scsi3addr[0];
495154b6e9e9SScott Teel 
495254b6e9e9SScott Teel 	/* Get a pointer to the hpsa logical device. */
49537fa3030cSStephen Cameron 	scmd = abort->scsi_cmd;
495454b6e9e9SScott Teel 	dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
495554b6e9e9SScott Teel 	if (dev == NULL) {
495654b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
495754b6e9e9SScott Teel 			"Cannot abort: no device pointer for command.\n");
495854b6e9e9SScott Teel 			return -1; /* not abortable */
495954b6e9e9SScott Teel 	}
496054b6e9e9SScott Teel 
49612ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
49622ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
49630d96ef5fSWebb Scales 			"scsi %d:%d:%d:%d %s scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
49642ba8bfc8SStephen M. Cameron 			h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
49650d96ef5fSWebb Scales 			"Reset as abort",
49662ba8bfc8SStephen M. Cameron 			scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
49672ba8bfc8SStephen M. Cameron 			scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]);
49682ba8bfc8SStephen M. Cameron 
496954b6e9e9SScott Teel 	if (!dev->offload_enabled) {
497054b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
497154b6e9e9SScott Teel 			"Can't abort: device is not operating in HP SSD Smart Path mode.\n");
497254b6e9e9SScott Teel 		return -1; /* not abortable */
497354b6e9e9SScott Teel 	}
497454b6e9e9SScott Teel 
497554b6e9e9SScott Teel 	/* Incoming scsi3addr is logical addr. We need physical disk addr. */
497654b6e9e9SScott Teel 	if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
497754b6e9e9SScott Teel 		dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
497854b6e9e9SScott Teel 		return -1; /* not abortable */
497954b6e9e9SScott Teel 	}
498054b6e9e9SScott Teel 
498154b6e9e9SScott Teel 	/* send the reset */
49822ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
49832ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
49842ba8bfc8SStephen M. Cameron 			"Reset as abort: Resetting physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
49852ba8bfc8SStephen M. Cameron 			psa[0], psa[1], psa[2], psa[3],
49862ba8bfc8SStephen M. Cameron 			psa[4], psa[5], psa[6], psa[7]);
498725163bd5SWebb Scales 	rc = hpsa_send_reset(h, psa, HPSA_RESET_TYPE_TARGET, reply_queue);
498854b6e9e9SScott Teel 	if (rc != 0) {
498954b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
499054b6e9e9SScott Teel 			"Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
499154b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
499254b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
499354b6e9e9SScott Teel 		return rc; /* failed to reset */
499454b6e9e9SScott Teel 	}
499554b6e9e9SScott Teel 
499654b6e9e9SScott Teel 	/* wait for device to recover */
499754b6e9e9SScott Teel 	if (wait_for_device_to_become_ready(h, psa) != 0) {
499854b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
499954b6e9e9SScott Teel 			"Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
500054b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
500154b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
500254b6e9e9SScott Teel 		return -1;  /* failed to recover */
500354b6e9e9SScott Teel 	}
500454b6e9e9SScott Teel 
500554b6e9e9SScott Teel 	/* device recovered */
500654b6e9e9SScott Teel 	dev_info(&h->pdev->dev,
500754b6e9e9SScott Teel 		"Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
500854b6e9e9SScott Teel 		psa[0], psa[1], psa[2], psa[3],
500954b6e9e9SScott Teel 		psa[4], psa[5], psa[6], psa[7]);
501054b6e9e9SScott Teel 
501154b6e9e9SScott Teel 	return rc; /* success */
501254b6e9e9SScott Teel }
501354b6e9e9SScott Teel 
50146cba3f19SStephen M. Cameron static int hpsa_send_abort_both_ways(struct ctlr_info *h,
501525163bd5SWebb Scales 	unsigned char *scsi3addr, struct CommandList *abort, int reply_queue)
50166cba3f19SStephen M. Cameron {
501754b6e9e9SScott Teel 	/* ioccelerator mode 2 commands should be aborted via the
501854b6e9e9SScott Teel 	 * accelerated path, since RAID path is unaware of these commands,
501954b6e9e9SScott Teel 	 * but underlying firmware can't handle abort TMF.
502054b6e9e9SScott Teel 	 * Change abort to physical device reset.
502154b6e9e9SScott Teel 	 */
502254b6e9e9SScott Teel 	if (abort->cmd_type == CMD_IOACCEL2)
502325163bd5SWebb Scales 		return hpsa_send_reset_as_abort_ioaccel2(h, scsi3addr,
502425163bd5SWebb Scales 							abort, reply_queue);
50259b5c48c2SStephen Cameron 	return hpsa_send_abort(h, scsi3addr, abort, reply_queue);
502625163bd5SWebb Scales }
502725163bd5SWebb Scales 
502825163bd5SWebb Scales /* Find out which reply queue a command was meant to return on */
502925163bd5SWebb Scales static int hpsa_extract_reply_queue(struct ctlr_info *h,
503025163bd5SWebb Scales 					struct CommandList *c)
503125163bd5SWebb Scales {
503225163bd5SWebb Scales 	if (c->cmd_type == CMD_IOACCEL2)
503325163bd5SWebb Scales 		return h->ioaccel2_cmd_pool[c->cmdindex].reply_queue;
503425163bd5SWebb Scales 	return c->Header.ReplyQueue;
50356cba3f19SStephen M. Cameron }
50366cba3f19SStephen M. Cameron 
50379b5c48c2SStephen Cameron /*
50389b5c48c2SStephen Cameron  * Limit concurrency of abort commands to prevent
50399b5c48c2SStephen Cameron  * over-subscription of commands
50409b5c48c2SStephen Cameron  */
50419b5c48c2SStephen Cameron static inline int wait_for_available_abort_cmd(struct ctlr_info *h)
50429b5c48c2SStephen Cameron {
50439b5c48c2SStephen Cameron #define ABORT_CMD_WAIT_MSECS 5000
50449b5c48c2SStephen Cameron 	return !wait_event_timeout(h->abort_cmd_wait_queue,
50459b5c48c2SStephen Cameron 			atomic_dec_if_positive(&h->abort_cmds_available) >= 0,
50469b5c48c2SStephen Cameron 			msecs_to_jiffies(ABORT_CMD_WAIT_MSECS));
50479b5c48c2SStephen Cameron }
50489b5c48c2SStephen Cameron 
504975167d2cSStephen M. Cameron /* Send an abort for the specified command.
505075167d2cSStephen M. Cameron  *	If the device and controller support it,
505175167d2cSStephen M. Cameron  *		send a task abort request.
505275167d2cSStephen M. Cameron  */
505375167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
505475167d2cSStephen M. Cameron {
505575167d2cSStephen M. Cameron 
505675167d2cSStephen M. Cameron 	int i, rc;
505775167d2cSStephen M. Cameron 	struct ctlr_info *h;
505875167d2cSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
505975167d2cSStephen M. Cameron 	struct CommandList *abort; /* pointer to command to be aborted */
506075167d2cSStephen M. Cameron 	struct scsi_cmnd *as;	/* ptr to scsi cmd inside aborted command. */
506175167d2cSStephen M. Cameron 	char msg[256];		/* For debug messaging. */
506275167d2cSStephen M. Cameron 	int ml = 0;
50632b08b3e9SDon Brace 	__le32 tagupper, taglower;
506425163bd5SWebb Scales 	int refcount, reply_queue;
506525163bd5SWebb Scales 
506625163bd5SWebb Scales 	if (sc == NULL)
506725163bd5SWebb Scales 		return FAILED;
506875167d2cSStephen M. Cameron 
50699b5c48c2SStephen Cameron 	if (sc->device == NULL)
50709b5c48c2SStephen Cameron 		return FAILED;
50719b5c48c2SStephen Cameron 
507275167d2cSStephen M. Cameron 	/* Find the controller of the command to be aborted */
507375167d2cSStephen M. Cameron 	h = sdev_to_hba(sc->device);
50749b5c48c2SStephen Cameron 	if (h == NULL)
507575167d2cSStephen M. Cameron 		return FAILED;
507675167d2cSStephen M. Cameron 
507725163bd5SWebb Scales 	/* Find the device of the command to be aborted */
507825163bd5SWebb Scales 	dev = sc->device->hostdata;
507925163bd5SWebb Scales 	if (!dev) {
508025163bd5SWebb Scales 		dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
508125163bd5SWebb Scales 				msg);
5082e345893bSDon Brace 		return FAILED;
508325163bd5SWebb Scales 	}
508425163bd5SWebb Scales 
508525163bd5SWebb Scales 	/* If controller locked up, we can guarantee command won't complete */
508625163bd5SWebb Scales 	if (lockup_detected(h)) {
508725163bd5SWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev,
508825163bd5SWebb Scales 					"ABORT FAILED, lockup detected");
508925163bd5SWebb Scales 		return FAILED;
509025163bd5SWebb Scales 	}
509125163bd5SWebb Scales 
509225163bd5SWebb Scales 	/* This is a good time to check if controller lockup has occurred */
509325163bd5SWebb Scales 	if (detect_controller_lockup(h)) {
509425163bd5SWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev,
509525163bd5SWebb Scales 					"ABORT FAILED, new lockup detected");
509625163bd5SWebb Scales 		return FAILED;
509725163bd5SWebb Scales 	}
5098e345893bSDon Brace 
509975167d2cSStephen M. Cameron 	/* Check that controller supports some kind of task abort */
510075167d2cSStephen M. Cameron 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
510175167d2cSStephen M. Cameron 		!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
510275167d2cSStephen M. Cameron 		return FAILED;
510375167d2cSStephen M. Cameron 
510475167d2cSStephen M. Cameron 	memset(msg, 0, sizeof(msg));
51050d96ef5fSWebb Scales 	ml += sprintf(msg+ml, "scsi %d:%d:%d:%llu %s",
510675167d2cSStephen M. Cameron 		h->scsi_host->host_no, sc->device->channel,
51070d96ef5fSWebb Scales 		sc->device->id, sc->device->lun,
51080d96ef5fSWebb Scales 		"Aborting command");
510975167d2cSStephen M. Cameron 
511075167d2cSStephen M. Cameron 	/* Get SCSI command to be aborted */
511175167d2cSStephen M. Cameron 	abort = (struct CommandList *) sc->host_scribble;
511275167d2cSStephen M. Cameron 	if (abort == NULL) {
5113281a7fd0SWebb Scales 		/* This can happen if the command already completed. */
5114281a7fd0SWebb Scales 		return SUCCESS;
5115281a7fd0SWebb Scales 	}
5116281a7fd0SWebb Scales 	refcount = atomic_inc_return(&abort->refcount);
5117281a7fd0SWebb Scales 	if (refcount == 1) { /* Command is done already. */
5118281a7fd0SWebb Scales 		cmd_free(h, abort);
5119281a7fd0SWebb Scales 		return SUCCESS;
512075167d2cSStephen M. Cameron 	}
51219b5c48c2SStephen Cameron 
51229b5c48c2SStephen Cameron 	/* Don't bother trying the abort if we know it won't work. */
51239b5c48c2SStephen Cameron 	if (abort->cmd_type != CMD_IOACCEL2 &&
51249b5c48c2SStephen Cameron 		abort->cmd_type != CMD_IOACCEL1 && !dev->supports_aborts) {
51259b5c48c2SStephen Cameron 		cmd_free(h, abort);
51269b5c48c2SStephen Cameron 		return FAILED;
51279b5c48c2SStephen Cameron 	}
51289b5c48c2SStephen Cameron 
512917eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
513025163bd5SWebb Scales 	reply_queue = hpsa_extract_reply_queue(h, abort);
513117eb87d2SScott Teel 	ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
51327fa3030cSStephen Cameron 	as  = abort->scsi_cmd;
513375167d2cSStephen M. Cameron 	if (as != NULL)
513475167d2cSStephen M. Cameron 		ml += sprintf(msg+ml, "Command:0x%x SN:0x%lx ",
513575167d2cSStephen M. Cameron 			as->cmnd[0], as->serial_number);
513675167d2cSStephen M. Cameron 	dev_dbg(&h->pdev->dev, "%s\n", msg);
51370d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_WARNING, h, dev, "Aborting command");
513875167d2cSStephen M. Cameron 	/*
513975167d2cSStephen M. Cameron 	 * Command is in flight, or possibly already completed
514075167d2cSStephen M. Cameron 	 * by the firmware (but not to the scsi mid layer) but we can't
514175167d2cSStephen M. Cameron 	 * distinguish which.  Send the abort down.
514275167d2cSStephen M. Cameron 	 */
51439b5c48c2SStephen Cameron 	if (wait_for_available_abort_cmd(h)) {
51449b5c48c2SStephen Cameron 		dev_warn(&h->pdev->dev,
51459b5c48c2SStephen Cameron 			"Timed out waiting for an abort command to become available.\n");
51469b5c48c2SStephen Cameron 		cmd_free(h, abort);
51479b5c48c2SStephen Cameron 		return FAILED;
51489b5c48c2SStephen Cameron 	}
514925163bd5SWebb Scales 	rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort, reply_queue);
51509b5c48c2SStephen Cameron 	atomic_inc(&h->abort_cmds_available);
51519b5c48c2SStephen Cameron 	wake_up_all(&h->abort_cmd_wait_queue);
515275167d2cSStephen M. Cameron 	if (rc != 0) {
51530d96ef5fSWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev,
51540d96ef5fSWebb Scales 					"FAILED to abort command");
5155281a7fd0SWebb Scales 		cmd_free(h, abort);
515675167d2cSStephen M. Cameron 		return FAILED;
515775167d2cSStephen M. Cameron 	}
515875167d2cSStephen M. Cameron 	dev_info(&h->pdev->dev, "%s REQUEST SUCCEEDED.\n", msg);
515975167d2cSStephen M. Cameron 
516075167d2cSStephen M. Cameron 	/* If the abort(s) above completed and actually aborted the
516175167d2cSStephen M. Cameron 	 * command, then the command to be aborted should already be
516275167d2cSStephen M. Cameron 	 * completed.  If not, wait around a bit more to see if they
516375167d2cSStephen M. Cameron 	 * manage to complete normally.
516475167d2cSStephen M. Cameron 	 */
516575167d2cSStephen M. Cameron #define ABORT_COMPLETE_WAIT_SECS 30
516675167d2cSStephen M. Cameron 	for (i = 0; i < ABORT_COMPLETE_WAIT_SECS * 10; i++) {
5167281a7fd0SWebb Scales 		refcount = atomic_read(&abort->refcount);
5168281a7fd0SWebb Scales 		if (refcount < 2) {
5169281a7fd0SWebb Scales 			cmd_free(h, abort);
5170f2405db8SDon Brace 			return SUCCESS;
5171281a7fd0SWebb Scales 		} else {
5172281a7fd0SWebb Scales 			msleep(100);
5173281a7fd0SWebb Scales 		}
517475167d2cSStephen M. Cameron 	}
517575167d2cSStephen M. Cameron 	dev_warn(&h->pdev->dev, "%s FAILED. Aborted command has not completed after %d seconds.\n",
517675167d2cSStephen M. Cameron 		msg, ABORT_COMPLETE_WAIT_SECS);
5177281a7fd0SWebb Scales 	cmd_free(h, abort);
517875167d2cSStephen M. Cameron 	return FAILED;
517975167d2cSStephen M. Cameron }
518075167d2cSStephen M. Cameron 
5181edd16368SStephen M. Cameron /*
5182edd16368SStephen M. Cameron  * For operations that cannot sleep, a command block is allocated at init,
5183edd16368SStephen M. Cameron  * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
5184edd16368SStephen M. Cameron  * which ones are free or in use.  Lock must be held when calling this.
5185edd16368SStephen M. Cameron  * cmd_free() is the complement.
5186*bf43caf3SRobert Elliott  * This function never gives up and returns NULL.  If it hangs,
5187*bf43caf3SRobert Elliott  * another thread must call cmd_free() to free some tags.
5188edd16368SStephen M. Cameron  */
5189281a7fd0SWebb Scales 
5190edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h)
5191edd16368SStephen M. Cameron {
5192edd16368SStephen M. Cameron 	struct CommandList *c;
5193360c73bdSStephen Cameron 	int refcount, i;
519433811026SRobert Elliott 	unsigned long offset;
5195edd16368SStephen M. Cameron 
519633811026SRobert Elliott 	/*
519733811026SRobert Elliott 	 * There is some *extremely* small but non-zero chance that that
51984c413128SStephen M. Cameron 	 * multiple threads could get in here, and one thread could
51994c413128SStephen M. Cameron 	 * be scanning through the list of bits looking for a free
52004c413128SStephen M. Cameron 	 * one, but the free ones are always behind him, and other
52014c413128SStephen M. Cameron 	 * threads sneak in behind him and eat them before he can
52024c413128SStephen M. Cameron 	 * get to them, so that while there is always a free one, a
52034c413128SStephen M. Cameron 	 * very unlucky thread might be starved anyway, never able to
52044c413128SStephen M. Cameron 	 * beat the other threads.  In reality, this happens so
52054c413128SStephen M. Cameron 	 * infrequently as to be indistinguishable from never.
52064c413128SStephen M. Cameron 	 */
52074c413128SStephen M. Cameron 
520833811026SRobert Elliott 	offset = h->last_allocation; /* benignly racy */
5209281a7fd0SWebb Scales 	for (;;) {
5210281a7fd0SWebb Scales 		i = find_next_zero_bit(h->cmd_pool_bits, h->nr_cmds, offset);
5211281a7fd0SWebb Scales 		if (unlikely(i == h->nr_cmds)) {
5212281a7fd0SWebb Scales 			offset = 0;
5213281a7fd0SWebb Scales 			continue;
5214281a7fd0SWebb Scales 		}
5215edd16368SStephen M. Cameron 		c = h->cmd_pool + i;
5216281a7fd0SWebb Scales 		refcount = atomic_inc_return(&c->refcount);
5217281a7fd0SWebb Scales 		if (unlikely(refcount > 1)) {
5218281a7fd0SWebb Scales 			cmd_free(h, c); /* already in use */
5219281a7fd0SWebb Scales 			offset = (i + 1) % h->nr_cmds;
5220281a7fd0SWebb Scales 			continue;
5221281a7fd0SWebb Scales 		}
5222281a7fd0SWebb Scales 		set_bit(i & (BITS_PER_LONG - 1),
5223281a7fd0SWebb Scales 			h->cmd_pool_bits + (i / BITS_PER_LONG));
5224281a7fd0SWebb Scales 		break; /* it's ours now. */
5225281a7fd0SWebb Scales 	}
522633811026SRobert Elliott 	h->last_allocation = i; /* benignly racy */
5227360c73bdSStephen Cameron 	hpsa_cmd_partial_init(h, i, c);
5228edd16368SStephen M. Cameron 	return c;
5229edd16368SStephen M. Cameron }
5230edd16368SStephen M. Cameron 
5231edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c)
5232edd16368SStephen M. Cameron {
5233281a7fd0SWebb Scales 	if (atomic_dec_and_test(&c->refcount)) {
5234edd16368SStephen M. Cameron 		int i;
5235edd16368SStephen M. Cameron 
5236edd16368SStephen M. Cameron 		i = c - h->cmd_pool;
5237edd16368SStephen M. Cameron 		clear_bit(i & (BITS_PER_LONG - 1),
5238edd16368SStephen M. Cameron 			  h->cmd_pool_bits + (i / BITS_PER_LONG));
5239edd16368SStephen M. Cameron 	}
5240281a7fd0SWebb Scales }
5241edd16368SStephen M. Cameron 
5242edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
5243edd16368SStephen M. Cameron 
524442a91641SDon Brace static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd,
524542a91641SDon Brace 	void __user *arg)
5246edd16368SStephen M. Cameron {
5247edd16368SStephen M. Cameron 	IOCTL32_Command_struct __user *arg32 =
5248edd16368SStephen M. Cameron 	    (IOCTL32_Command_struct __user *) arg;
5249edd16368SStephen M. Cameron 	IOCTL_Command_struct arg64;
5250edd16368SStephen M. Cameron 	IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
5251edd16368SStephen M. Cameron 	int err;
5252edd16368SStephen M. Cameron 	u32 cp;
5253edd16368SStephen M. Cameron 
5254938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
5255edd16368SStephen M. Cameron 	err = 0;
5256edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
5257edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
5258edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
5259edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
5260edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
5261edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
5262edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
5263edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
5264edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
5265edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
5266edd16368SStephen M. Cameron 
5267edd16368SStephen M. Cameron 	if (err)
5268edd16368SStephen M. Cameron 		return -EFAULT;
5269edd16368SStephen M. Cameron 
527042a91641SDon Brace 	err = hpsa_ioctl(dev, CCISS_PASSTHRU, p);
5271edd16368SStephen M. Cameron 	if (err)
5272edd16368SStephen M. Cameron 		return err;
5273edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
5274edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
5275edd16368SStephen M. Cameron 	if (err)
5276edd16368SStephen M. Cameron 		return -EFAULT;
5277edd16368SStephen M. Cameron 	return err;
5278edd16368SStephen M. Cameron }
5279edd16368SStephen M. Cameron 
5280edd16368SStephen M. Cameron static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
528142a91641SDon Brace 	int cmd, void __user *arg)
5282edd16368SStephen M. Cameron {
5283edd16368SStephen M. Cameron 	BIG_IOCTL32_Command_struct __user *arg32 =
5284edd16368SStephen M. Cameron 	    (BIG_IOCTL32_Command_struct __user *) arg;
5285edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct arg64;
5286edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct __user *p =
5287edd16368SStephen M. Cameron 	    compat_alloc_user_space(sizeof(arg64));
5288edd16368SStephen M. Cameron 	int err;
5289edd16368SStephen M. Cameron 	u32 cp;
5290edd16368SStephen M. Cameron 
5291938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
5292edd16368SStephen M. Cameron 	err = 0;
5293edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
5294edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
5295edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
5296edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
5297edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
5298edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
5299edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
5300edd16368SStephen M. Cameron 	err |= get_user(arg64.malloc_size, &arg32->malloc_size);
5301edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
5302edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
5303edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
5304edd16368SStephen M. Cameron 
5305edd16368SStephen M. Cameron 	if (err)
5306edd16368SStephen M. Cameron 		return -EFAULT;
5307edd16368SStephen M. Cameron 
530842a91641SDon Brace 	err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, p);
5309edd16368SStephen M. Cameron 	if (err)
5310edd16368SStephen M. Cameron 		return err;
5311edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
5312edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
5313edd16368SStephen M. Cameron 	if (err)
5314edd16368SStephen M. Cameron 		return -EFAULT;
5315edd16368SStephen M. Cameron 	return err;
5316edd16368SStephen M. Cameron }
531771fe75a7SStephen M. Cameron 
531842a91641SDon Brace static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
531971fe75a7SStephen M. Cameron {
532071fe75a7SStephen M. Cameron 	switch (cmd) {
532171fe75a7SStephen M. Cameron 	case CCISS_GETPCIINFO:
532271fe75a7SStephen M. Cameron 	case CCISS_GETINTINFO:
532371fe75a7SStephen M. Cameron 	case CCISS_SETINTINFO:
532471fe75a7SStephen M. Cameron 	case CCISS_GETNODENAME:
532571fe75a7SStephen M. Cameron 	case CCISS_SETNODENAME:
532671fe75a7SStephen M. Cameron 	case CCISS_GETHEARTBEAT:
532771fe75a7SStephen M. Cameron 	case CCISS_GETBUSTYPES:
532871fe75a7SStephen M. Cameron 	case CCISS_GETFIRMVER:
532971fe75a7SStephen M. Cameron 	case CCISS_GETDRIVVER:
533071fe75a7SStephen M. Cameron 	case CCISS_REVALIDVOLS:
533171fe75a7SStephen M. Cameron 	case CCISS_DEREGDISK:
533271fe75a7SStephen M. Cameron 	case CCISS_REGNEWDISK:
533371fe75a7SStephen M. Cameron 	case CCISS_REGNEWD:
533471fe75a7SStephen M. Cameron 	case CCISS_RESCANDISK:
533571fe75a7SStephen M. Cameron 	case CCISS_GETLUNINFO:
533671fe75a7SStephen M. Cameron 		return hpsa_ioctl(dev, cmd, arg);
533771fe75a7SStephen M. Cameron 
533871fe75a7SStephen M. Cameron 	case CCISS_PASSTHRU32:
533971fe75a7SStephen M. Cameron 		return hpsa_ioctl32_passthru(dev, cmd, arg);
534071fe75a7SStephen M. Cameron 	case CCISS_BIG_PASSTHRU32:
534171fe75a7SStephen M. Cameron 		return hpsa_ioctl32_big_passthru(dev, cmd, arg);
534271fe75a7SStephen M. Cameron 
534371fe75a7SStephen M. Cameron 	default:
534471fe75a7SStephen M. Cameron 		return -ENOIOCTLCMD;
534571fe75a7SStephen M. Cameron 	}
534671fe75a7SStephen M. Cameron }
5347edd16368SStephen M. Cameron #endif
5348edd16368SStephen M. Cameron 
5349edd16368SStephen M. Cameron static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
5350edd16368SStephen M. Cameron {
5351edd16368SStephen M. Cameron 	struct hpsa_pci_info pciinfo;
5352edd16368SStephen M. Cameron 
5353edd16368SStephen M. Cameron 	if (!argp)
5354edd16368SStephen M. Cameron 		return -EINVAL;
5355edd16368SStephen M. Cameron 	pciinfo.domain = pci_domain_nr(h->pdev->bus);
5356edd16368SStephen M. Cameron 	pciinfo.bus = h->pdev->bus->number;
5357edd16368SStephen M. Cameron 	pciinfo.dev_fn = h->pdev->devfn;
5358edd16368SStephen M. Cameron 	pciinfo.board_id = h->board_id;
5359edd16368SStephen M. Cameron 	if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
5360edd16368SStephen M. Cameron 		return -EFAULT;
5361edd16368SStephen M. Cameron 	return 0;
5362edd16368SStephen M. Cameron }
5363edd16368SStephen M. Cameron 
5364edd16368SStephen M. Cameron static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
5365edd16368SStephen M. Cameron {
5366edd16368SStephen M. Cameron 	DriverVer_type DriverVer;
5367edd16368SStephen M. Cameron 	unsigned char vmaj, vmin, vsubmin;
5368edd16368SStephen M. Cameron 	int rc;
5369edd16368SStephen M. Cameron 
5370edd16368SStephen M. Cameron 	rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
5371edd16368SStephen M. Cameron 		&vmaj, &vmin, &vsubmin);
5372edd16368SStephen M. Cameron 	if (rc != 3) {
5373edd16368SStephen M. Cameron 		dev_info(&h->pdev->dev, "driver version string '%s' "
5374edd16368SStephen M. Cameron 			"unrecognized.", HPSA_DRIVER_VERSION);
5375edd16368SStephen M. Cameron 		vmaj = 0;
5376edd16368SStephen M. Cameron 		vmin = 0;
5377edd16368SStephen M. Cameron 		vsubmin = 0;
5378edd16368SStephen M. Cameron 	}
5379edd16368SStephen M. Cameron 	DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
5380edd16368SStephen M. Cameron 	if (!argp)
5381edd16368SStephen M. Cameron 		return -EINVAL;
5382edd16368SStephen M. Cameron 	if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
5383edd16368SStephen M. Cameron 		return -EFAULT;
5384edd16368SStephen M. Cameron 	return 0;
5385edd16368SStephen M. Cameron }
5386edd16368SStephen M. Cameron 
5387edd16368SStephen M. Cameron static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
5388edd16368SStephen M. Cameron {
5389edd16368SStephen M. Cameron 	IOCTL_Command_struct iocommand;
5390edd16368SStephen M. Cameron 	struct CommandList *c;
5391edd16368SStephen M. Cameron 	char *buff = NULL;
539250a0decfSStephen M. Cameron 	u64 temp64;
5393c1f63c8fSStephen M. Cameron 	int rc = 0;
5394edd16368SStephen M. Cameron 
5395edd16368SStephen M. Cameron 	if (!argp)
5396edd16368SStephen M. Cameron 		return -EINVAL;
5397edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
5398edd16368SStephen M. Cameron 		return -EPERM;
5399edd16368SStephen M. Cameron 	if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
5400edd16368SStephen M. Cameron 		return -EFAULT;
5401edd16368SStephen M. Cameron 	if ((iocommand.buf_size < 1) &&
5402edd16368SStephen M. Cameron 	    (iocommand.Request.Type.Direction != XFER_NONE)) {
5403edd16368SStephen M. Cameron 		return -EINVAL;
5404edd16368SStephen M. Cameron 	}
5405edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
5406edd16368SStephen M. Cameron 		buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
5407edd16368SStephen M. Cameron 		if (buff == NULL)
5408edd16368SStephen M. Cameron 			return -EFAULT;
54099233fb10SStephen M. Cameron 		if (iocommand.Request.Type.Direction & XFER_WRITE) {
5410edd16368SStephen M. Cameron 			/* Copy the data into the buffer we created */
5411b03a7771SStephen M. Cameron 			if (copy_from_user(buff, iocommand.buf,
5412b03a7771SStephen M. Cameron 				iocommand.buf_size)) {
5413c1f63c8fSStephen M. Cameron 				rc = -EFAULT;
5414c1f63c8fSStephen M. Cameron 				goto out_kfree;
5415edd16368SStephen M. Cameron 			}
5416b03a7771SStephen M. Cameron 		} else {
5417edd16368SStephen M. Cameron 			memset(buff, 0, iocommand.buf_size);
5418b03a7771SStephen M. Cameron 		}
5419b03a7771SStephen M. Cameron 	}
542045fcb86eSStephen Cameron 	c = cmd_alloc(h);
5421*bf43caf3SRobert Elliott 
5422edd16368SStephen M. Cameron 	/* Fill in the command type */
5423edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5424edd16368SStephen M. Cameron 	/* Fill in Command Header */
5425edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0; /* unused in simple mode */
5426edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {	/* buffer to fill */
5427edd16368SStephen M. Cameron 		c->Header.SGList = 1;
542850a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(1);
5429edd16368SStephen M. Cameron 	} else	{ /* no buffers to fill */
5430edd16368SStephen M. Cameron 		c->Header.SGList = 0;
543150a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(0);
5432edd16368SStephen M. Cameron 	}
5433edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
5434edd16368SStephen M. Cameron 
5435edd16368SStephen M. Cameron 	/* Fill in Request block */
5436edd16368SStephen M. Cameron 	memcpy(&c->Request, &iocommand.Request,
5437edd16368SStephen M. Cameron 		sizeof(c->Request));
5438edd16368SStephen M. Cameron 
5439edd16368SStephen M. Cameron 	/* Fill in the scatter gather information */
5440edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
544150a0decfSStephen M. Cameron 		temp64 = pci_map_single(h->pdev, buff,
5442edd16368SStephen M. Cameron 			iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
544350a0decfSStephen M. Cameron 		if (dma_mapping_error(&h->pdev->dev, (dma_addr_t) temp64)) {
544450a0decfSStephen M. Cameron 			c->SG[0].Addr = cpu_to_le64(0);
544550a0decfSStephen M. Cameron 			c->SG[0].Len = cpu_to_le32(0);
5446bcc48ffaSStephen M. Cameron 			rc = -ENOMEM;
5447bcc48ffaSStephen M. Cameron 			goto out;
5448bcc48ffaSStephen M. Cameron 		}
544950a0decfSStephen M. Cameron 		c->SG[0].Addr = cpu_to_le64(temp64);
545050a0decfSStephen M. Cameron 		c->SG[0].Len = cpu_to_le32(iocommand.buf_size);
545150a0decfSStephen M. Cameron 		c->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* not chaining */
5452edd16368SStephen M. Cameron 	}
545325163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
5454c2dd32e0SStephen M. Cameron 	if (iocommand.buf_size > 0)
5455edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
5456edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
545725163bd5SWebb Scales 	if (rc) {
545825163bd5SWebb Scales 		rc = -EIO;
545925163bd5SWebb Scales 		goto out;
546025163bd5SWebb Scales 	}
5461edd16368SStephen M. Cameron 
5462edd16368SStephen M. Cameron 	/* Copy the error information out */
5463edd16368SStephen M. Cameron 	memcpy(&iocommand.error_info, c->err_info,
5464edd16368SStephen M. Cameron 		sizeof(iocommand.error_info));
5465edd16368SStephen M. Cameron 	if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
5466c1f63c8fSStephen M. Cameron 		rc = -EFAULT;
5467c1f63c8fSStephen M. Cameron 		goto out;
5468edd16368SStephen M. Cameron 	}
54699233fb10SStephen M. Cameron 	if ((iocommand.Request.Type.Direction & XFER_READ) &&
5470b03a7771SStephen M. Cameron 		iocommand.buf_size > 0) {
5471edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
5472edd16368SStephen M. Cameron 		if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
5473c1f63c8fSStephen M. Cameron 			rc = -EFAULT;
5474c1f63c8fSStephen M. Cameron 			goto out;
5475edd16368SStephen M. Cameron 		}
5476edd16368SStephen M. Cameron 	}
5477c1f63c8fSStephen M. Cameron out:
547845fcb86eSStephen Cameron 	cmd_free(h, c);
5479c1f63c8fSStephen M. Cameron out_kfree:
5480c1f63c8fSStephen M. Cameron 	kfree(buff);
5481c1f63c8fSStephen M. Cameron 	return rc;
5482edd16368SStephen M. Cameron }
5483edd16368SStephen M. Cameron 
5484edd16368SStephen M. Cameron static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
5485edd16368SStephen M. Cameron {
5486edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct *ioc;
5487edd16368SStephen M. Cameron 	struct CommandList *c;
5488edd16368SStephen M. Cameron 	unsigned char **buff = NULL;
5489edd16368SStephen M. Cameron 	int *buff_size = NULL;
549050a0decfSStephen M. Cameron 	u64 temp64;
5491edd16368SStephen M. Cameron 	BYTE sg_used = 0;
5492edd16368SStephen M. Cameron 	int status = 0;
549301a02ffcSStephen M. Cameron 	u32 left;
549401a02ffcSStephen M. Cameron 	u32 sz;
5495edd16368SStephen M. Cameron 	BYTE __user *data_ptr;
5496edd16368SStephen M. Cameron 
5497edd16368SStephen M. Cameron 	if (!argp)
5498edd16368SStephen M. Cameron 		return -EINVAL;
5499edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
5500edd16368SStephen M. Cameron 		return -EPERM;
5501edd16368SStephen M. Cameron 	ioc = (BIG_IOCTL_Command_struct *)
5502edd16368SStephen M. Cameron 	    kmalloc(sizeof(*ioc), GFP_KERNEL);
5503edd16368SStephen M. Cameron 	if (!ioc) {
5504edd16368SStephen M. Cameron 		status = -ENOMEM;
5505edd16368SStephen M. Cameron 		goto cleanup1;
5506edd16368SStephen M. Cameron 	}
5507edd16368SStephen M. Cameron 	if (copy_from_user(ioc, argp, sizeof(*ioc))) {
5508edd16368SStephen M. Cameron 		status = -EFAULT;
5509edd16368SStephen M. Cameron 		goto cleanup1;
5510edd16368SStephen M. Cameron 	}
5511edd16368SStephen M. Cameron 	if ((ioc->buf_size < 1) &&
5512edd16368SStephen M. Cameron 	    (ioc->Request.Type.Direction != XFER_NONE)) {
5513edd16368SStephen M. Cameron 		status = -EINVAL;
5514edd16368SStephen M. Cameron 		goto cleanup1;
5515edd16368SStephen M. Cameron 	}
5516edd16368SStephen M. Cameron 	/* Check kmalloc limits  using all SGs */
5517edd16368SStephen M. Cameron 	if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
5518edd16368SStephen M. Cameron 		status = -EINVAL;
5519edd16368SStephen M. Cameron 		goto cleanup1;
5520edd16368SStephen M. Cameron 	}
5521d66ae08bSStephen M. Cameron 	if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
5522edd16368SStephen M. Cameron 		status = -EINVAL;
5523edd16368SStephen M. Cameron 		goto cleanup1;
5524edd16368SStephen M. Cameron 	}
5525d66ae08bSStephen M. Cameron 	buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
5526edd16368SStephen M. Cameron 	if (!buff) {
5527edd16368SStephen M. Cameron 		status = -ENOMEM;
5528edd16368SStephen M. Cameron 		goto cleanup1;
5529edd16368SStephen M. Cameron 	}
5530d66ae08bSStephen M. Cameron 	buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
5531edd16368SStephen M. Cameron 	if (!buff_size) {
5532edd16368SStephen M. Cameron 		status = -ENOMEM;
5533edd16368SStephen M. Cameron 		goto cleanup1;
5534edd16368SStephen M. Cameron 	}
5535edd16368SStephen M. Cameron 	left = ioc->buf_size;
5536edd16368SStephen M. Cameron 	data_ptr = ioc->buf;
5537edd16368SStephen M. Cameron 	while (left) {
5538edd16368SStephen M. Cameron 		sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
5539edd16368SStephen M. Cameron 		buff_size[sg_used] = sz;
5540edd16368SStephen M. Cameron 		buff[sg_used] = kmalloc(sz, GFP_KERNEL);
5541edd16368SStephen M. Cameron 		if (buff[sg_used] == NULL) {
5542edd16368SStephen M. Cameron 			status = -ENOMEM;
5543edd16368SStephen M. Cameron 			goto cleanup1;
5544edd16368SStephen M. Cameron 		}
55459233fb10SStephen M. Cameron 		if (ioc->Request.Type.Direction & XFER_WRITE) {
5546edd16368SStephen M. Cameron 			if (copy_from_user(buff[sg_used], data_ptr, sz)) {
55470758f4f7SStephen M. Cameron 				status = -EFAULT;
5548edd16368SStephen M. Cameron 				goto cleanup1;
5549edd16368SStephen M. Cameron 			}
5550edd16368SStephen M. Cameron 		} else
5551edd16368SStephen M. Cameron 			memset(buff[sg_used], 0, sz);
5552edd16368SStephen M. Cameron 		left -= sz;
5553edd16368SStephen M. Cameron 		data_ptr += sz;
5554edd16368SStephen M. Cameron 		sg_used++;
5555edd16368SStephen M. Cameron 	}
555645fcb86eSStephen Cameron 	c = cmd_alloc(h);
5557*bf43caf3SRobert Elliott 
5558edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5559edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
556050a0decfSStephen M. Cameron 	c->Header.SGList = (u8) sg_used;
556150a0decfSStephen M. Cameron 	c->Header.SGTotal = cpu_to_le16(sg_used);
5562edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
5563edd16368SStephen M. Cameron 	memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
5564edd16368SStephen M. Cameron 	if (ioc->buf_size > 0) {
5565edd16368SStephen M. Cameron 		int i;
5566edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
556750a0decfSStephen M. Cameron 			temp64 = pci_map_single(h->pdev, buff[i],
5568edd16368SStephen M. Cameron 				    buff_size[i], PCI_DMA_BIDIRECTIONAL);
556950a0decfSStephen M. Cameron 			if (dma_mapping_error(&h->pdev->dev,
557050a0decfSStephen M. Cameron 							(dma_addr_t) temp64)) {
557150a0decfSStephen M. Cameron 				c->SG[i].Addr = cpu_to_le64(0);
557250a0decfSStephen M. Cameron 				c->SG[i].Len = cpu_to_le32(0);
5573bcc48ffaSStephen M. Cameron 				hpsa_pci_unmap(h->pdev, c, i,
5574bcc48ffaSStephen M. Cameron 					PCI_DMA_BIDIRECTIONAL);
5575bcc48ffaSStephen M. Cameron 				status = -ENOMEM;
5576e2d4a1f6SStephen M. Cameron 				goto cleanup0;
5577bcc48ffaSStephen M. Cameron 			}
557850a0decfSStephen M. Cameron 			c->SG[i].Addr = cpu_to_le64(temp64);
557950a0decfSStephen M. Cameron 			c->SG[i].Len = cpu_to_le32(buff_size[i]);
558050a0decfSStephen M. Cameron 			c->SG[i].Ext = cpu_to_le32(0);
5581edd16368SStephen M. Cameron 		}
558250a0decfSStephen M. Cameron 		c->SG[--i].Ext = cpu_to_le32(HPSA_SG_LAST);
5583edd16368SStephen M. Cameron 	}
558425163bd5SWebb Scales 	status = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
5585b03a7771SStephen M. Cameron 	if (sg_used)
5586edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
5587edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
558825163bd5SWebb Scales 	if (status) {
558925163bd5SWebb Scales 		status = -EIO;
559025163bd5SWebb Scales 		goto cleanup0;
559125163bd5SWebb Scales 	}
559225163bd5SWebb Scales 
5593edd16368SStephen M. Cameron 	/* Copy the error information out */
5594edd16368SStephen M. Cameron 	memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
5595edd16368SStephen M. Cameron 	if (copy_to_user(argp, ioc, sizeof(*ioc))) {
5596edd16368SStephen M. Cameron 		status = -EFAULT;
5597e2d4a1f6SStephen M. Cameron 		goto cleanup0;
5598edd16368SStephen M. Cameron 	}
55999233fb10SStephen M. Cameron 	if ((ioc->Request.Type.Direction & XFER_READ) && ioc->buf_size > 0) {
56002b08b3e9SDon Brace 		int i;
56012b08b3e9SDon Brace 
5602edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
5603edd16368SStephen M. Cameron 		BYTE __user *ptr = ioc->buf;
5604edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
5605edd16368SStephen M. Cameron 			if (copy_to_user(ptr, buff[i], buff_size[i])) {
5606edd16368SStephen M. Cameron 				status = -EFAULT;
5607e2d4a1f6SStephen M. Cameron 				goto cleanup0;
5608edd16368SStephen M. Cameron 			}
5609edd16368SStephen M. Cameron 			ptr += buff_size[i];
5610edd16368SStephen M. Cameron 		}
5611edd16368SStephen M. Cameron 	}
5612edd16368SStephen M. Cameron 	status = 0;
5613e2d4a1f6SStephen M. Cameron cleanup0:
561445fcb86eSStephen Cameron 	cmd_free(h, c);
5615edd16368SStephen M. Cameron cleanup1:
5616edd16368SStephen M. Cameron 	if (buff) {
56172b08b3e9SDon Brace 		int i;
56182b08b3e9SDon Brace 
5619edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++)
5620edd16368SStephen M. Cameron 			kfree(buff[i]);
5621edd16368SStephen M. Cameron 		kfree(buff);
5622edd16368SStephen M. Cameron 	}
5623edd16368SStephen M. Cameron 	kfree(buff_size);
5624edd16368SStephen M. Cameron 	kfree(ioc);
5625edd16368SStephen M. Cameron 	return status;
5626edd16368SStephen M. Cameron }
5627edd16368SStephen M. Cameron 
5628edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
5629edd16368SStephen M. Cameron 	struct CommandList *c)
5630edd16368SStephen M. Cameron {
5631edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
5632edd16368SStephen M. Cameron 			c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
5633edd16368SStephen M. Cameron 		(void) check_for_unit_attention(h, c);
5634edd16368SStephen M. Cameron }
56350390f0c0SStephen M. Cameron 
5636edd16368SStephen M. Cameron /*
5637edd16368SStephen M. Cameron  * ioctl
5638edd16368SStephen M. Cameron  */
563942a91641SDon Brace static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
5640edd16368SStephen M. Cameron {
5641edd16368SStephen M. Cameron 	struct ctlr_info *h;
5642edd16368SStephen M. Cameron 	void __user *argp = (void __user *)arg;
56430390f0c0SStephen M. Cameron 	int rc;
5644edd16368SStephen M. Cameron 
5645edd16368SStephen M. Cameron 	h = sdev_to_hba(dev);
5646edd16368SStephen M. Cameron 
5647edd16368SStephen M. Cameron 	switch (cmd) {
5648edd16368SStephen M. Cameron 	case CCISS_DEREGDISK:
5649edd16368SStephen M. Cameron 	case CCISS_REGNEWDISK:
5650edd16368SStephen M. Cameron 	case CCISS_REGNEWD:
5651a08a8471SStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
5652edd16368SStephen M. Cameron 		return 0;
5653edd16368SStephen M. Cameron 	case CCISS_GETPCIINFO:
5654edd16368SStephen M. Cameron 		return hpsa_getpciinfo_ioctl(h, argp);
5655edd16368SStephen M. Cameron 	case CCISS_GETDRIVVER:
5656edd16368SStephen M. Cameron 		return hpsa_getdrivver_ioctl(h, argp);
5657edd16368SStephen M. Cameron 	case CCISS_PASSTHRU:
565834f0c627SDon Brace 		if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
56590390f0c0SStephen M. Cameron 			return -EAGAIN;
56600390f0c0SStephen M. Cameron 		rc = hpsa_passthru_ioctl(h, argp);
566134f0c627SDon Brace 		atomic_inc(&h->passthru_cmds_avail);
56620390f0c0SStephen M. Cameron 		return rc;
5663edd16368SStephen M. Cameron 	case CCISS_BIG_PASSTHRU:
566434f0c627SDon Brace 		if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
56650390f0c0SStephen M. Cameron 			return -EAGAIN;
56660390f0c0SStephen M. Cameron 		rc = hpsa_big_passthru_ioctl(h, argp);
566734f0c627SDon Brace 		atomic_inc(&h->passthru_cmds_avail);
56680390f0c0SStephen M. Cameron 		return rc;
5669edd16368SStephen M. Cameron 	default:
5670edd16368SStephen M. Cameron 		return -ENOTTY;
5671edd16368SStephen M. Cameron 	}
5672edd16368SStephen M. Cameron }
5673edd16368SStephen M. Cameron 
5674*bf43caf3SRobert Elliott static void hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
56756f039790SGreg Kroah-Hartman 				u8 reset_type)
567664670ac8SStephen M. Cameron {
567764670ac8SStephen M. Cameron 	struct CommandList *c;
567864670ac8SStephen M. Cameron 
567964670ac8SStephen M. Cameron 	c = cmd_alloc(h);
5680*bf43caf3SRobert Elliott 
5681a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map */
5682a2dac136SStephen M. Cameron 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
568364670ac8SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_MSG);
568464670ac8SStephen M. Cameron 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
568564670ac8SStephen M. Cameron 	c->waiting = NULL;
568664670ac8SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
568764670ac8SStephen M. Cameron 	/* Don't wait for completion, the reset won't complete.  Don't free
568864670ac8SStephen M. Cameron 	 * the command either.  This is the last command we will send before
568964670ac8SStephen M. Cameron 	 * re-initializing everything, so it doesn't matter and won't leak.
569064670ac8SStephen M. Cameron 	 */
5691*bf43caf3SRobert Elliott 	return;
569264670ac8SStephen M. Cameron }
569364670ac8SStephen M. Cameron 
5694a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
5695b7bb24ebSStephen M. Cameron 	void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
5696edd16368SStephen M. Cameron 	int cmd_type)
5697edd16368SStephen M. Cameron {
5698edd16368SStephen M. Cameron 	int pci_dir = XFER_NONE;
56999b5c48c2SStephen Cameron 	u64 tag; /* for commands to be aborted */
5700edd16368SStephen M. Cameron 
5701edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5702edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
5703edd16368SStephen M. Cameron 	if (buff != NULL && size > 0) {
5704edd16368SStephen M. Cameron 		c->Header.SGList = 1;
570550a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(1);
5706edd16368SStephen M. Cameron 	} else {
5707edd16368SStephen M. Cameron 		c->Header.SGList = 0;
570850a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(0);
5709edd16368SStephen M. Cameron 	}
5710edd16368SStephen M. Cameron 	memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
5711edd16368SStephen M. Cameron 
5712edd16368SStephen M. Cameron 	if (cmd_type == TYPE_CMD) {
5713edd16368SStephen M. Cameron 		switch (cmd) {
5714edd16368SStephen M. Cameron 		case HPSA_INQUIRY:
5715edd16368SStephen M. Cameron 			/* are we trying to read a vital product page */
5716b7bb24ebSStephen M. Cameron 			if (page_code & VPD_PAGE) {
5717edd16368SStephen M. Cameron 				c->Request.CDB[1] = 0x01;
5718b7bb24ebSStephen M. Cameron 				c->Request.CDB[2] = (page_code & 0xff);
5719edd16368SStephen M. Cameron 			}
5720edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
5721a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5722a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5723edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5724edd16368SStephen M. Cameron 			c->Request.CDB[0] = HPSA_INQUIRY;
5725edd16368SStephen M. Cameron 			c->Request.CDB[4] = size & 0xFF;
5726edd16368SStephen M. Cameron 			break;
5727edd16368SStephen M. Cameron 		case HPSA_REPORT_LOG:
5728edd16368SStephen M. Cameron 		case HPSA_REPORT_PHYS:
5729edd16368SStephen M. Cameron 			/* Talking to controller so It's a physical command
5730edd16368SStephen M. Cameron 			   mode = 00 target = 0.  Nothing to write.
5731edd16368SStephen M. Cameron 			 */
5732edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
5733a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5734a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5735edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5736edd16368SStephen M. Cameron 			c->Request.CDB[0] = cmd;
5737edd16368SStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
5738edd16368SStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5739edd16368SStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5740edd16368SStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
5741edd16368SStephen M. Cameron 			break;
5742edd16368SStephen M. Cameron 		case HPSA_CACHE_FLUSH:
5743edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
5744a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5745a505b86fSStephen M. Cameron 					TYPE_ATTR_DIR(cmd_type,
5746a505b86fSStephen M. Cameron 						ATTR_SIMPLE, XFER_WRITE);
5747edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5748edd16368SStephen M. Cameron 			c->Request.CDB[0] = BMIC_WRITE;
5749edd16368SStephen M. Cameron 			c->Request.CDB[6] = BMIC_CACHE_FLUSH;
5750bb158eabSStephen M. Cameron 			c->Request.CDB[7] = (size >> 8) & 0xFF;
5751bb158eabSStephen M. Cameron 			c->Request.CDB[8] = size & 0xFF;
5752edd16368SStephen M. Cameron 			break;
5753edd16368SStephen M. Cameron 		case TEST_UNIT_READY:
5754edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
5755a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5756a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
5757edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5758edd16368SStephen M. Cameron 			break;
5759283b4a9bSStephen M. Cameron 		case HPSA_GET_RAID_MAP:
5760283b4a9bSStephen M. Cameron 			c->Request.CDBLen = 12;
5761a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5762a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5763283b4a9bSStephen M. Cameron 			c->Request.Timeout = 0;
5764283b4a9bSStephen M. Cameron 			c->Request.CDB[0] = HPSA_CISS_READ;
5765283b4a9bSStephen M. Cameron 			c->Request.CDB[1] = cmd;
5766283b4a9bSStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
5767283b4a9bSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5768283b4a9bSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5769283b4a9bSStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
5770283b4a9bSStephen M. Cameron 			break;
5771316b221aSStephen M. Cameron 		case BMIC_SENSE_CONTROLLER_PARAMETERS:
5772316b221aSStephen M. Cameron 			c->Request.CDBLen = 10;
5773a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5774a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5775316b221aSStephen M. Cameron 			c->Request.Timeout = 0;
5776316b221aSStephen M. Cameron 			c->Request.CDB[0] = BMIC_READ;
5777316b221aSStephen M. Cameron 			c->Request.CDB[6] = BMIC_SENSE_CONTROLLER_PARAMETERS;
5778316b221aSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5779316b221aSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5780316b221aSStephen M. Cameron 			break;
578103383736SDon Brace 		case BMIC_IDENTIFY_PHYSICAL_DEVICE:
578203383736SDon Brace 			c->Request.CDBLen = 10;
578303383736SDon Brace 			c->Request.type_attr_dir =
578403383736SDon Brace 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
578503383736SDon Brace 			c->Request.Timeout = 0;
578603383736SDon Brace 			c->Request.CDB[0] = BMIC_READ;
578703383736SDon Brace 			c->Request.CDB[6] = BMIC_IDENTIFY_PHYSICAL_DEVICE;
578803383736SDon Brace 			c->Request.CDB[7] = (size >> 16) & 0xFF;
578903383736SDon Brace 			c->Request.CDB[8] = (size >> 8) & 0XFF;
579003383736SDon Brace 			break;
5791edd16368SStephen M. Cameron 		default:
5792edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
5793edd16368SStephen M. Cameron 			BUG();
5794a2dac136SStephen M. Cameron 			return -1;
5795edd16368SStephen M. Cameron 		}
5796edd16368SStephen M. Cameron 	} else if (cmd_type == TYPE_MSG) {
5797edd16368SStephen M. Cameron 		switch (cmd) {
5798edd16368SStephen M. Cameron 
5799edd16368SStephen M. Cameron 		case  HPSA_DEVICE_RESET_MSG:
5800edd16368SStephen M. Cameron 			c->Request.CDBLen = 16;
5801a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5802a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
5803edd16368SStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
580464670ac8SStephen M. Cameron 			memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
580564670ac8SStephen M. Cameron 			c->Request.CDB[0] =  cmd;
580621e89afdSStephen M. Cameron 			c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
5807edd16368SStephen M. Cameron 			/* If bytes 4-7 are zero, it means reset the */
5808edd16368SStephen M. Cameron 			/* LunID device */
5809edd16368SStephen M. Cameron 			c->Request.CDB[4] = 0x00;
5810edd16368SStephen M. Cameron 			c->Request.CDB[5] = 0x00;
5811edd16368SStephen M. Cameron 			c->Request.CDB[6] = 0x00;
5812edd16368SStephen M. Cameron 			c->Request.CDB[7] = 0x00;
5813edd16368SStephen M. Cameron 			break;
581475167d2cSStephen M. Cameron 		case  HPSA_ABORT_MSG:
58159b5c48c2SStephen Cameron 			memcpy(&tag, buff, sizeof(tag));
58162b08b3e9SDon Brace 			dev_dbg(&h->pdev->dev,
58179b5c48c2SStephen Cameron 				"Abort Tag:0x%016llx using rqst Tag:0x%016llx",
58189b5c48c2SStephen Cameron 				tag, c->Header.tag);
581975167d2cSStephen M. Cameron 			c->Request.CDBLen = 16;
5820a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5821a505b86fSStephen M. Cameron 					TYPE_ATTR_DIR(cmd_type,
5822a505b86fSStephen M. Cameron 						ATTR_SIMPLE, XFER_WRITE);
582375167d2cSStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
582475167d2cSStephen M. Cameron 			c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
582575167d2cSStephen M. Cameron 			c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
582675167d2cSStephen M. Cameron 			c->Request.CDB[2] = 0x00; /* reserved */
582775167d2cSStephen M. Cameron 			c->Request.CDB[3] = 0x00; /* reserved */
582875167d2cSStephen M. Cameron 			/* Tag to abort goes in CDB[4]-CDB[11] */
58299b5c48c2SStephen Cameron 			memcpy(&c->Request.CDB[4], &tag, sizeof(tag));
583075167d2cSStephen M. Cameron 			c->Request.CDB[12] = 0x00; /* reserved */
583175167d2cSStephen M. Cameron 			c->Request.CDB[13] = 0x00; /* reserved */
583275167d2cSStephen M. Cameron 			c->Request.CDB[14] = 0x00; /* reserved */
583375167d2cSStephen M. Cameron 			c->Request.CDB[15] = 0x00; /* reserved */
583475167d2cSStephen M. Cameron 		break;
5835edd16368SStephen M. Cameron 		default:
5836edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown message type %d\n",
5837edd16368SStephen M. Cameron 				cmd);
5838edd16368SStephen M. Cameron 			BUG();
5839edd16368SStephen M. Cameron 		}
5840edd16368SStephen M. Cameron 	} else {
5841edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
5842edd16368SStephen M. Cameron 		BUG();
5843edd16368SStephen M. Cameron 	}
5844edd16368SStephen M. Cameron 
5845a505b86fSStephen M. Cameron 	switch (GET_DIR(c->Request.type_attr_dir)) {
5846edd16368SStephen M. Cameron 	case XFER_READ:
5847edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_FROMDEVICE;
5848edd16368SStephen M. Cameron 		break;
5849edd16368SStephen M. Cameron 	case XFER_WRITE:
5850edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_TODEVICE;
5851edd16368SStephen M. Cameron 		break;
5852edd16368SStephen M. Cameron 	case XFER_NONE:
5853edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_NONE;
5854edd16368SStephen M. Cameron 		break;
5855edd16368SStephen M. Cameron 	default:
5856edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_BIDIRECTIONAL;
5857edd16368SStephen M. Cameron 	}
5858a2dac136SStephen M. Cameron 	if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
5859a2dac136SStephen M. Cameron 		return -1;
5860a2dac136SStephen M. Cameron 	return 0;
5861edd16368SStephen M. Cameron }
5862edd16368SStephen M. Cameron 
5863edd16368SStephen M. Cameron /*
5864edd16368SStephen M. Cameron  * Map (physical) PCI mem into (virtual) kernel space
5865edd16368SStephen M. Cameron  */
5866edd16368SStephen M. Cameron static void __iomem *remap_pci_mem(ulong base, ulong size)
5867edd16368SStephen M. Cameron {
5868edd16368SStephen M. Cameron 	ulong page_base = ((ulong) base) & PAGE_MASK;
5869edd16368SStephen M. Cameron 	ulong page_offs = ((ulong) base) - page_base;
5870088ba34cSStephen M. Cameron 	void __iomem *page_remapped = ioremap_nocache(page_base,
5871088ba34cSStephen M. Cameron 		page_offs + size);
5872edd16368SStephen M. Cameron 
5873edd16368SStephen M. Cameron 	return page_remapped ? (page_remapped + page_offs) : NULL;
5874edd16368SStephen M. Cameron }
5875edd16368SStephen M. Cameron 
5876254f796bSMatt Gates static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
5877edd16368SStephen M. Cameron {
5878254f796bSMatt Gates 	return h->access.command_completed(h, q);
5879edd16368SStephen M. Cameron }
5880edd16368SStephen M. Cameron 
5881900c5440SStephen M. Cameron static inline bool interrupt_pending(struct ctlr_info *h)
5882edd16368SStephen M. Cameron {
5883edd16368SStephen M. Cameron 	return h->access.intr_pending(h);
5884edd16368SStephen M. Cameron }
5885edd16368SStephen M. Cameron 
5886edd16368SStephen M. Cameron static inline long interrupt_not_for_us(struct ctlr_info *h)
5887edd16368SStephen M. Cameron {
588810f66018SStephen M. Cameron 	return (h->access.intr_pending(h) == 0) ||
588910f66018SStephen M. Cameron 		(h->interrupts_enabled == 0);
5890edd16368SStephen M. Cameron }
5891edd16368SStephen M. Cameron 
589201a02ffcSStephen M. Cameron static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
589301a02ffcSStephen M. Cameron 	u32 raw_tag)
5894edd16368SStephen M. Cameron {
5895edd16368SStephen M. Cameron 	if (unlikely(tag_index >= h->nr_cmds)) {
5896edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
5897edd16368SStephen M. Cameron 		return 1;
5898edd16368SStephen M. Cameron 	}
5899edd16368SStephen M. Cameron 	return 0;
5900edd16368SStephen M. Cameron }
5901edd16368SStephen M. Cameron 
59025a3d16f5SStephen M. Cameron static inline void finish_cmd(struct CommandList *c)
5903edd16368SStephen M. Cameron {
5904e85c5974SStephen M. Cameron 	dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
5905c349775eSScott Teel 	if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
5906c349775eSScott Teel 			|| c->cmd_type == CMD_IOACCEL2))
59071fb011fbSStephen M. Cameron 		complete_scsi_command(c);
5908edd16368SStephen M. Cameron 	else if (c->cmd_type == CMD_IOCTL_PEND)
5909edd16368SStephen M. Cameron 		complete(c->waiting);
5910a104c99fSStephen M. Cameron }
5911a104c99fSStephen M. Cameron 
5912a9a3a273SStephen M. Cameron 
5913a9a3a273SStephen M. Cameron static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
5914a104c99fSStephen M. Cameron {
5915a9a3a273SStephen M. Cameron #define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
5916a9a3a273SStephen M. Cameron #define HPSA_SIMPLE_ERROR_BITS 0x03
5917960a30e7SStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
5918a9a3a273SStephen M. Cameron 		return tag & ~HPSA_SIMPLE_ERROR_BITS;
5919a9a3a273SStephen M. Cameron 	return tag & ~HPSA_PERF_ERROR_BITS;
5920a104c99fSStephen M. Cameron }
5921a104c99fSStephen M. Cameron 
5922303932fdSDon Brace /* process completion of an indexed ("direct lookup") command */
59231d94f94dSStephen M. Cameron static inline void process_indexed_cmd(struct ctlr_info *h,
5924303932fdSDon Brace 	u32 raw_tag)
5925303932fdSDon Brace {
5926303932fdSDon Brace 	u32 tag_index;
5927303932fdSDon Brace 	struct CommandList *c;
5928303932fdSDon Brace 
5929f2405db8SDon Brace 	tag_index = raw_tag >> DIRECT_LOOKUP_SHIFT;
59301d94f94dSStephen M. Cameron 	if (!bad_tag(h, tag_index, raw_tag)) {
5931303932fdSDon Brace 		c = h->cmd_pool + tag_index;
59325a3d16f5SStephen M. Cameron 		finish_cmd(c);
59331d94f94dSStephen M. Cameron 	}
5934303932fdSDon Brace }
5935303932fdSDon Brace 
593664670ac8SStephen M. Cameron /* Some controllers, like p400, will give us one interrupt
593764670ac8SStephen M. Cameron  * after a soft reset, even if we turned interrupts off.
593864670ac8SStephen M. Cameron  * Only need to check for this in the hpsa_xxx_discard_completions
593964670ac8SStephen M. Cameron  * functions.
594064670ac8SStephen M. Cameron  */
594164670ac8SStephen M. Cameron static int ignore_bogus_interrupt(struct ctlr_info *h)
594264670ac8SStephen M. Cameron {
594364670ac8SStephen M. Cameron 	if (likely(!reset_devices))
594464670ac8SStephen M. Cameron 		return 0;
594564670ac8SStephen M. Cameron 
594664670ac8SStephen M. Cameron 	if (likely(h->interrupts_enabled))
594764670ac8SStephen M. Cameron 		return 0;
594864670ac8SStephen M. Cameron 
594964670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
595064670ac8SStephen M. Cameron 		"(known firmware bug.)  Ignoring.\n");
595164670ac8SStephen M. Cameron 
595264670ac8SStephen M. Cameron 	return 1;
595364670ac8SStephen M. Cameron }
595464670ac8SStephen M. Cameron 
5955254f796bSMatt Gates /*
5956254f796bSMatt Gates  * Convert &h->q[x] (passed to interrupt handlers) back to h.
5957254f796bSMatt Gates  * Relies on (h-q[x] == x) being true for x such that
5958254f796bSMatt Gates  * 0 <= x < MAX_REPLY_QUEUES.
5959254f796bSMatt Gates  */
5960254f796bSMatt Gates static struct ctlr_info *queue_to_hba(u8 *queue)
596164670ac8SStephen M. Cameron {
5962254f796bSMatt Gates 	return container_of((queue - *queue), struct ctlr_info, q[0]);
5963254f796bSMatt Gates }
5964254f796bSMatt Gates 
5965254f796bSMatt Gates static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
5966254f796bSMatt Gates {
5967254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
5968254f796bSMatt Gates 	u8 q = *(u8 *) queue;
596964670ac8SStephen M. Cameron 	u32 raw_tag;
597064670ac8SStephen M. Cameron 
597164670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
597264670ac8SStephen M. Cameron 		return IRQ_NONE;
597364670ac8SStephen M. Cameron 
597464670ac8SStephen M. Cameron 	if (interrupt_not_for_us(h))
597564670ac8SStephen M. Cameron 		return IRQ_NONE;
5976a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
597764670ac8SStephen M. Cameron 	while (interrupt_pending(h)) {
5978254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
597964670ac8SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY)
5980254f796bSMatt Gates 			raw_tag = next_command(h, q);
598164670ac8SStephen M. Cameron 	}
598264670ac8SStephen M. Cameron 	return IRQ_HANDLED;
598364670ac8SStephen M. Cameron }
598464670ac8SStephen M. Cameron 
5985254f796bSMatt Gates static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
598664670ac8SStephen M. Cameron {
5987254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
598864670ac8SStephen M. Cameron 	u32 raw_tag;
5989254f796bSMatt Gates 	u8 q = *(u8 *) queue;
599064670ac8SStephen M. Cameron 
599164670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
599264670ac8SStephen M. Cameron 		return IRQ_NONE;
599364670ac8SStephen M. Cameron 
5994a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
5995254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
599664670ac8SStephen M. Cameron 	while (raw_tag != FIFO_EMPTY)
5997254f796bSMatt Gates 		raw_tag = next_command(h, q);
599864670ac8SStephen M. Cameron 	return IRQ_HANDLED;
599964670ac8SStephen M. Cameron }
600064670ac8SStephen M. Cameron 
6001254f796bSMatt Gates static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
6002edd16368SStephen M. Cameron {
6003254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba((u8 *) queue);
6004303932fdSDon Brace 	u32 raw_tag;
6005254f796bSMatt Gates 	u8 q = *(u8 *) queue;
6006edd16368SStephen M. Cameron 
6007edd16368SStephen M. Cameron 	if (interrupt_not_for_us(h))
6008edd16368SStephen M. Cameron 		return IRQ_NONE;
6009a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
601010f66018SStephen M. Cameron 	while (interrupt_pending(h)) {
6011254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
601210f66018SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY) {
60131d94f94dSStephen M. Cameron 			process_indexed_cmd(h, raw_tag);
6014254f796bSMatt Gates 			raw_tag = next_command(h, q);
601510f66018SStephen M. Cameron 		}
601610f66018SStephen M. Cameron 	}
601710f66018SStephen M. Cameron 	return IRQ_HANDLED;
601810f66018SStephen M. Cameron }
601910f66018SStephen M. Cameron 
6020254f796bSMatt Gates static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
602110f66018SStephen M. Cameron {
6022254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
602310f66018SStephen M. Cameron 	u32 raw_tag;
6024254f796bSMatt Gates 	u8 q = *(u8 *) queue;
602510f66018SStephen M. Cameron 
6026a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
6027254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
6028303932fdSDon Brace 	while (raw_tag != FIFO_EMPTY) {
60291d94f94dSStephen M. Cameron 		process_indexed_cmd(h, raw_tag);
6030254f796bSMatt Gates 		raw_tag = next_command(h, q);
6031edd16368SStephen M. Cameron 	}
6032edd16368SStephen M. Cameron 	return IRQ_HANDLED;
6033edd16368SStephen M. Cameron }
6034edd16368SStephen M. Cameron 
6035a9a3a273SStephen M. Cameron /* Send a message CDB to the firmware. Careful, this only works
6036a9a3a273SStephen M. Cameron  * in simple mode, not performant mode due to the tag lookup.
6037a9a3a273SStephen M. Cameron  * We only ever use this immediately after a controller reset.
6038a9a3a273SStephen M. Cameron  */
60396f039790SGreg Kroah-Hartman static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
6040edd16368SStephen M. Cameron 			unsigned char type)
6041edd16368SStephen M. Cameron {
6042edd16368SStephen M. Cameron 	struct Command {
6043edd16368SStephen M. Cameron 		struct CommandListHeader CommandHeader;
6044edd16368SStephen M. Cameron 		struct RequestBlock Request;
6045edd16368SStephen M. Cameron 		struct ErrDescriptor ErrorDescriptor;
6046edd16368SStephen M. Cameron 	};
6047edd16368SStephen M. Cameron 	struct Command *cmd;
6048edd16368SStephen M. Cameron 	static const size_t cmd_sz = sizeof(*cmd) +
6049edd16368SStephen M. Cameron 					sizeof(cmd->ErrorDescriptor);
6050edd16368SStephen M. Cameron 	dma_addr_t paddr64;
60512b08b3e9SDon Brace 	__le32 paddr32;
60522b08b3e9SDon Brace 	u32 tag;
6053edd16368SStephen M. Cameron 	void __iomem *vaddr;
6054edd16368SStephen M. Cameron 	int i, err;
6055edd16368SStephen M. Cameron 
6056edd16368SStephen M. Cameron 	vaddr = pci_ioremap_bar(pdev, 0);
6057edd16368SStephen M. Cameron 	if (vaddr == NULL)
6058edd16368SStephen M. Cameron 		return -ENOMEM;
6059edd16368SStephen M. Cameron 
6060edd16368SStephen M. Cameron 	/* The Inbound Post Queue only accepts 32-bit physical addresses for the
6061edd16368SStephen M. Cameron 	 * CCISS commands, so they must be allocated from the lower 4GiB of
6062edd16368SStephen M. Cameron 	 * memory.
6063edd16368SStephen M. Cameron 	 */
6064edd16368SStephen M. Cameron 	err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
6065edd16368SStephen M. Cameron 	if (err) {
6066edd16368SStephen M. Cameron 		iounmap(vaddr);
60671eaec8f3SRobert Elliott 		return err;
6068edd16368SStephen M. Cameron 	}
6069edd16368SStephen M. Cameron 
6070edd16368SStephen M. Cameron 	cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
6071edd16368SStephen M. Cameron 	if (cmd == NULL) {
6072edd16368SStephen M. Cameron 		iounmap(vaddr);
6073edd16368SStephen M. Cameron 		return -ENOMEM;
6074edd16368SStephen M. Cameron 	}
6075edd16368SStephen M. Cameron 
6076edd16368SStephen M. Cameron 	/* This must fit, because of the 32-bit consistent DMA mask.  Also,
6077edd16368SStephen M. Cameron 	 * although there's no guarantee, we assume that the address is at
6078edd16368SStephen M. Cameron 	 * least 4-byte aligned (most likely, it's page-aligned).
6079edd16368SStephen M. Cameron 	 */
60802b08b3e9SDon Brace 	paddr32 = cpu_to_le32(paddr64);
6081edd16368SStephen M. Cameron 
6082edd16368SStephen M. Cameron 	cmd->CommandHeader.ReplyQueue = 0;
6083edd16368SStephen M. Cameron 	cmd->CommandHeader.SGList = 0;
608450a0decfSStephen M. Cameron 	cmd->CommandHeader.SGTotal = cpu_to_le16(0);
60852b08b3e9SDon Brace 	cmd->CommandHeader.tag = cpu_to_le64(paddr64);
6086edd16368SStephen M. Cameron 	memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
6087edd16368SStephen M. Cameron 
6088edd16368SStephen M. Cameron 	cmd->Request.CDBLen = 16;
6089a505b86fSStephen M. Cameron 	cmd->Request.type_attr_dir =
6090a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_MSG, ATTR_HEADOFQUEUE, XFER_NONE);
6091edd16368SStephen M. Cameron 	cmd->Request.Timeout = 0; /* Don't time out */
6092edd16368SStephen M. Cameron 	cmd->Request.CDB[0] = opcode;
6093edd16368SStephen M. Cameron 	cmd->Request.CDB[1] = type;
6094edd16368SStephen M. Cameron 	memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
609550a0decfSStephen M. Cameron 	cmd->ErrorDescriptor.Addr =
60962b08b3e9SDon Brace 			cpu_to_le64((le32_to_cpu(paddr32) + sizeof(*cmd)));
609750a0decfSStephen M. Cameron 	cmd->ErrorDescriptor.Len = cpu_to_le32(sizeof(struct ErrorInfo));
6098edd16368SStephen M. Cameron 
60992b08b3e9SDon Brace 	writel(le32_to_cpu(paddr32), vaddr + SA5_REQUEST_PORT_OFFSET);
6100edd16368SStephen M. Cameron 
6101edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
6102edd16368SStephen M. Cameron 		tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
61032b08b3e9SDon Brace 		if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr64)
6104edd16368SStephen M. Cameron 			break;
6105edd16368SStephen M. Cameron 		msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
6106edd16368SStephen M. Cameron 	}
6107edd16368SStephen M. Cameron 
6108edd16368SStephen M. Cameron 	iounmap(vaddr);
6109edd16368SStephen M. Cameron 
6110edd16368SStephen M. Cameron 	/* we leak the DMA buffer here ... no choice since the controller could
6111edd16368SStephen M. Cameron 	 *  still complete the command.
6112edd16368SStephen M. Cameron 	 */
6113edd16368SStephen M. Cameron 	if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
6114edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
6115edd16368SStephen M. Cameron 			opcode, type);
6116edd16368SStephen M. Cameron 		return -ETIMEDOUT;
6117edd16368SStephen M. Cameron 	}
6118edd16368SStephen M. Cameron 
6119edd16368SStephen M. Cameron 	pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
6120edd16368SStephen M. Cameron 
6121edd16368SStephen M. Cameron 	if (tag & HPSA_ERROR_BIT) {
6122edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
6123edd16368SStephen M. Cameron 			opcode, type);
6124edd16368SStephen M. Cameron 		return -EIO;
6125edd16368SStephen M. Cameron 	}
6126edd16368SStephen M. Cameron 
6127edd16368SStephen M. Cameron 	dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
6128edd16368SStephen M. Cameron 		opcode, type);
6129edd16368SStephen M. Cameron 	return 0;
6130edd16368SStephen M. Cameron }
6131edd16368SStephen M. Cameron 
6132edd16368SStephen M. Cameron #define hpsa_noop(p) hpsa_message(p, 3, 0)
6133edd16368SStephen M. Cameron 
61341df8552aSStephen M. Cameron static int hpsa_controller_hard_reset(struct pci_dev *pdev,
613542a91641SDon Brace 	void __iomem *vaddr, u32 use_doorbell)
6136edd16368SStephen M. Cameron {
6137edd16368SStephen M. Cameron 
61381df8552aSStephen M. Cameron 	if (use_doorbell) {
61391df8552aSStephen M. Cameron 		/* For everything after the P600, the PCI power state method
61401df8552aSStephen M. Cameron 		 * of resetting the controller doesn't work, so we have this
61411df8552aSStephen M. Cameron 		 * other way using the doorbell register.
6142edd16368SStephen M. Cameron 		 */
61431df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using doorbell to reset controller\n");
6144cf0b08d0SStephen M. Cameron 		writel(use_doorbell, vaddr + SA5_DOORBELL);
614585009239SStephen M. Cameron 
614600701a96SJustin Lindley 		/* PMC hardware guys tell us we need a 10 second delay after
614785009239SStephen M. Cameron 		 * doorbell reset and before any attempt to talk to the board
614885009239SStephen M. Cameron 		 * at all to ensure that this actually works and doesn't fall
614985009239SStephen M. Cameron 		 * over in some weird corner cases.
615085009239SStephen M. Cameron 		 */
615100701a96SJustin Lindley 		msleep(10000);
61521df8552aSStephen M. Cameron 	} else { /* Try to do it the PCI power state way */
6153edd16368SStephen M. Cameron 
6154edd16368SStephen M. Cameron 		/* Quoting from the Open CISS Specification: "The Power
6155edd16368SStephen M. Cameron 		 * Management Control/Status Register (CSR) controls the power
6156edd16368SStephen M. Cameron 		 * state of the device.  The normal operating state is D0,
6157edd16368SStephen M. Cameron 		 * CSR=00h.  The software off state is D3, CSR=03h.  To reset
61581df8552aSStephen M. Cameron 		 * the controller, place the interface device in D3 then to D0,
61591df8552aSStephen M. Cameron 		 * this causes a secondary PCI reset which will reset the
61601df8552aSStephen M. Cameron 		 * controller." */
6161edd16368SStephen M. Cameron 
61622662cab8SDon Brace 		int rc = 0;
61632662cab8SDon Brace 
61641df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using PCI PM to reset controller\n");
61652662cab8SDon Brace 
6166edd16368SStephen M. Cameron 		/* enter the D3hot power management state */
61672662cab8SDon Brace 		rc = pci_set_power_state(pdev, PCI_D3hot);
61682662cab8SDon Brace 		if (rc)
61692662cab8SDon Brace 			return rc;
6170edd16368SStephen M. Cameron 
6171edd16368SStephen M. Cameron 		msleep(500);
6172edd16368SStephen M. Cameron 
6173edd16368SStephen M. Cameron 		/* enter the D0 power management state */
61742662cab8SDon Brace 		rc = pci_set_power_state(pdev, PCI_D0);
61752662cab8SDon Brace 		if (rc)
61762662cab8SDon Brace 			return rc;
6177c4853efeSMike Miller 
6178c4853efeSMike Miller 		/*
6179c4853efeSMike Miller 		 * The P600 requires a small delay when changing states.
6180c4853efeSMike Miller 		 * Otherwise we may think the board did not reset and we bail.
6181c4853efeSMike Miller 		 * This for kdump only and is particular to the P600.
6182c4853efeSMike Miller 		 */
6183c4853efeSMike Miller 		msleep(500);
61841df8552aSStephen M. Cameron 	}
61851df8552aSStephen M. Cameron 	return 0;
61861df8552aSStephen M. Cameron }
61871df8552aSStephen M. Cameron 
61886f039790SGreg Kroah-Hartman static void init_driver_version(char *driver_version, int len)
6189580ada3cSStephen M. Cameron {
6190580ada3cSStephen M. Cameron 	memset(driver_version, 0, len);
6191f79cfec6SStephen M. Cameron 	strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
6192580ada3cSStephen M. Cameron }
6193580ada3cSStephen M. Cameron 
61946f039790SGreg Kroah-Hartman static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
6195580ada3cSStephen M. Cameron {
6196580ada3cSStephen M. Cameron 	char *driver_version;
6197580ada3cSStephen M. Cameron 	int i, size = sizeof(cfgtable->driver_version);
6198580ada3cSStephen M. Cameron 
6199580ada3cSStephen M. Cameron 	driver_version = kmalloc(size, GFP_KERNEL);
6200580ada3cSStephen M. Cameron 	if (!driver_version)
6201580ada3cSStephen M. Cameron 		return -ENOMEM;
6202580ada3cSStephen M. Cameron 
6203580ada3cSStephen M. Cameron 	init_driver_version(driver_version, size);
6204580ada3cSStephen M. Cameron 	for (i = 0; i < size; i++)
6205580ada3cSStephen M. Cameron 		writeb(driver_version[i], &cfgtable->driver_version[i]);
6206580ada3cSStephen M. Cameron 	kfree(driver_version);
6207580ada3cSStephen M. Cameron 	return 0;
6208580ada3cSStephen M. Cameron }
6209580ada3cSStephen M. Cameron 
62106f039790SGreg Kroah-Hartman static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
62116f039790SGreg Kroah-Hartman 					  unsigned char *driver_ver)
6212580ada3cSStephen M. Cameron {
6213580ada3cSStephen M. Cameron 	int i;
6214580ada3cSStephen M. Cameron 
6215580ada3cSStephen M. Cameron 	for (i = 0; i < sizeof(cfgtable->driver_version); i++)
6216580ada3cSStephen M. Cameron 		driver_ver[i] = readb(&cfgtable->driver_version[i]);
6217580ada3cSStephen M. Cameron }
6218580ada3cSStephen M. Cameron 
62196f039790SGreg Kroah-Hartman static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
6220580ada3cSStephen M. Cameron {
6221580ada3cSStephen M. Cameron 
6222580ada3cSStephen M. Cameron 	char *driver_ver, *old_driver_ver;
6223580ada3cSStephen M. Cameron 	int rc, size = sizeof(cfgtable->driver_version);
6224580ada3cSStephen M. Cameron 
6225580ada3cSStephen M. Cameron 	old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
6226580ada3cSStephen M. Cameron 	if (!old_driver_ver)
6227580ada3cSStephen M. Cameron 		return -ENOMEM;
6228580ada3cSStephen M. Cameron 	driver_ver = old_driver_ver + size;
6229580ada3cSStephen M. Cameron 
6230580ada3cSStephen M. Cameron 	/* After a reset, the 32 bytes of "driver version" in the cfgtable
6231580ada3cSStephen M. Cameron 	 * should have been changed, otherwise we know the reset failed.
6232580ada3cSStephen M. Cameron 	 */
6233580ada3cSStephen M. Cameron 	init_driver_version(old_driver_ver, size);
6234580ada3cSStephen M. Cameron 	read_driver_ver_from_cfgtable(cfgtable, driver_ver);
6235580ada3cSStephen M. Cameron 	rc = !memcmp(driver_ver, old_driver_ver, size);
6236580ada3cSStephen M. Cameron 	kfree(old_driver_ver);
6237580ada3cSStephen M. Cameron 	return rc;
6238580ada3cSStephen M. Cameron }
62391df8552aSStephen M. Cameron /* This does a hard reset of the controller using PCI power management
62401df8552aSStephen M. Cameron  * states or the using the doorbell register.
62411df8552aSStephen M. Cameron  */
62426b6c1cd7STomas Henzl static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev, u32 board_id)
62431df8552aSStephen M. Cameron {
62441df8552aSStephen M. Cameron 	u64 cfg_offset;
62451df8552aSStephen M. Cameron 	u32 cfg_base_addr;
62461df8552aSStephen M. Cameron 	u64 cfg_base_addr_index;
62471df8552aSStephen M. Cameron 	void __iomem *vaddr;
62481df8552aSStephen M. Cameron 	unsigned long paddr;
6249580ada3cSStephen M. Cameron 	u32 misc_fw_support;
6250270d05deSStephen M. Cameron 	int rc;
62511df8552aSStephen M. Cameron 	struct CfgTable __iomem *cfgtable;
6252cf0b08d0SStephen M. Cameron 	u32 use_doorbell;
6253270d05deSStephen M. Cameron 	u16 command_register;
62541df8552aSStephen M. Cameron 
62551df8552aSStephen M. Cameron 	/* For controllers as old as the P600, this is very nearly
62561df8552aSStephen M. Cameron 	 * the same thing as
62571df8552aSStephen M. Cameron 	 *
62581df8552aSStephen M. Cameron 	 * pci_save_state(pci_dev);
62591df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D3hot);
62601df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D0);
62611df8552aSStephen M. Cameron 	 * pci_restore_state(pci_dev);
62621df8552aSStephen M. Cameron 	 *
62631df8552aSStephen M. Cameron 	 * For controllers newer than the P600, the pci power state
62641df8552aSStephen M. Cameron 	 * method of resetting doesn't work so we have another way
62651df8552aSStephen M. Cameron 	 * using the doorbell register.
62661df8552aSStephen M. Cameron 	 */
626718867659SStephen M. Cameron 
626860f923b9SRobert Elliott 	if (!ctlr_is_resettable(board_id)) {
626960f923b9SRobert Elliott 		dev_warn(&pdev->dev, "Controller not resettable\n");
627025c1e56aSStephen M. Cameron 		return -ENODEV;
627125c1e56aSStephen M. Cameron 	}
627246380786SStephen M. Cameron 
627346380786SStephen M. Cameron 	/* if controller is soft- but not hard resettable... */
627446380786SStephen M. Cameron 	if (!ctlr_is_hard_resettable(board_id))
627546380786SStephen M. Cameron 		return -ENOTSUPP; /* try soft reset later. */
627618867659SStephen M. Cameron 
6277270d05deSStephen M. Cameron 	/* Save the PCI command register */
6278270d05deSStephen M. Cameron 	pci_read_config_word(pdev, 4, &command_register);
6279270d05deSStephen M. Cameron 	pci_save_state(pdev);
62801df8552aSStephen M. Cameron 
62811df8552aSStephen M. Cameron 	/* find the first memory BAR, so we can find the cfg table */
62821df8552aSStephen M. Cameron 	rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
62831df8552aSStephen M. Cameron 	if (rc)
62841df8552aSStephen M. Cameron 		return rc;
62851df8552aSStephen M. Cameron 	vaddr = remap_pci_mem(paddr, 0x250);
62861df8552aSStephen M. Cameron 	if (!vaddr)
62871df8552aSStephen M. Cameron 		return -ENOMEM;
62881df8552aSStephen M. Cameron 
62891df8552aSStephen M. Cameron 	/* find cfgtable in order to check if reset via doorbell is supported */
62901df8552aSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
62911df8552aSStephen M. Cameron 					&cfg_base_addr_index, &cfg_offset);
62921df8552aSStephen M. Cameron 	if (rc)
62931df8552aSStephen M. Cameron 		goto unmap_vaddr;
62941df8552aSStephen M. Cameron 	cfgtable = remap_pci_mem(pci_resource_start(pdev,
62951df8552aSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
62961df8552aSStephen M. Cameron 	if (!cfgtable) {
62971df8552aSStephen M. Cameron 		rc = -ENOMEM;
62981df8552aSStephen M. Cameron 		goto unmap_vaddr;
62991df8552aSStephen M. Cameron 	}
6300580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(cfgtable);
6301580ada3cSStephen M. Cameron 	if (rc)
630203741d95STomas Henzl 		goto unmap_cfgtable;
63031df8552aSStephen M. Cameron 
6304cf0b08d0SStephen M. Cameron 	/* If reset via doorbell register is supported, use that.
6305cf0b08d0SStephen M. Cameron 	 * There are two such methods.  Favor the newest method.
6306cf0b08d0SStephen M. Cameron 	 */
63071df8552aSStephen M. Cameron 	misc_fw_support = readl(&cfgtable->misc_fw_support);
6308cf0b08d0SStephen M. Cameron 	use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
6309cf0b08d0SStephen M. Cameron 	if (use_doorbell) {
6310cf0b08d0SStephen M. Cameron 		use_doorbell = DOORBELL_CTLR_RESET2;
6311cf0b08d0SStephen M. Cameron 	} else {
63121df8552aSStephen M. Cameron 		use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
6313cf0b08d0SStephen M. Cameron 		if (use_doorbell) {
6314050f7147SStephen Cameron 			dev_warn(&pdev->dev,
6315050f7147SStephen Cameron 				"Soft reset not supported. Firmware update is required.\n");
631664670ac8SStephen M. Cameron 			rc = -ENOTSUPP; /* try soft reset */
6317cf0b08d0SStephen M. Cameron 			goto unmap_cfgtable;
6318cf0b08d0SStephen M. Cameron 		}
6319cf0b08d0SStephen M. Cameron 	}
63201df8552aSStephen M. Cameron 
63211df8552aSStephen M. Cameron 	rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
63221df8552aSStephen M. Cameron 	if (rc)
63231df8552aSStephen M. Cameron 		goto unmap_cfgtable;
6324edd16368SStephen M. Cameron 
6325270d05deSStephen M. Cameron 	pci_restore_state(pdev);
6326270d05deSStephen M. Cameron 	pci_write_config_word(pdev, 4, command_register);
6327edd16368SStephen M. Cameron 
63281df8552aSStephen M. Cameron 	/* Some devices (notably the HP Smart Array 5i Controller)
63291df8552aSStephen M. Cameron 	   need a little pause here */
63301df8552aSStephen M. Cameron 	msleep(HPSA_POST_RESET_PAUSE_MSECS);
63311df8552aSStephen M. Cameron 
6332fe5389c8SStephen M. Cameron 	rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
6333fe5389c8SStephen M. Cameron 	if (rc) {
6334fe5389c8SStephen M. Cameron 		dev_warn(&pdev->dev,
6335050f7147SStephen Cameron 			"Failed waiting for board to become ready after hard reset\n");
6336fe5389c8SStephen M. Cameron 		goto unmap_cfgtable;
6337fe5389c8SStephen M. Cameron 	}
6338fe5389c8SStephen M. Cameron 
6339580ada3cSStephen M. Cameron 	rc = controller_reset_failed(vaddr);
6340580ada3cSStephen M. Cameron 	if (rc < 0)
6341580ada3cSStephen M. Cameron 		goto unmap_cfgtable;
6342580ada3cSStephen M. Cameron 	if (rc) {
634364670ac8SStephen M. Cameron 		dev_warn(&pdev->dev, "Unable to successfully reset "
634464670ac8SStephen M. Cameron 			"controller. Will try soft reset.\n");
634564670ac8SStephen M. Cameron 		rc = -ENOTSUPP;
6346580ada3cSStephen M. Cameron 	} else {
634764670ac8SStephen M. Cameron 		dev_info(&pdev->dev, "board ready after hard reset.\n");
63481df8552aSStephen M. Cameron 	}
63491df8552aSStephen M. Cameron 
63501df8552aSStephen M. Cameron unmap_cfgtable:
63511df8552aSStephen M. Cameron 	iounmap(cfgtable);
63521df8552aSStephen M. Cameron 
63531df8552aSStephen M. Cameron unmap_vaddr:
63541df8552aSStephen M. Cameron 	iounmap(vaddr);
63551df8552aSStephen M. Cameron 	return rc;
6356edd16368SStephen M. Cameron }
6357edd16368SStephen M. Cameron 
6358edd16368SStephen M. Cameron /*
6359edd16368SStephen M. Cameron  *  We cannot read the structure directly, for portability we must use
6360edd16368SStephen M. Cameron  *   the io functions.
6361edd16368SStephen M. Cameron  *   This is for debug only.
6362edd16368SStephen M. Cameron  */
636342a91641SDon Brace static void print_cfg_table(struct device *dev, struct CfgTable __iomem *tb)
6364edd16368SStephen M. Cameron {
636558f8665cSStephen M. Cameron #ifdef HPSA_DEBUG
6366edd16368SStephen M. Cameron 	int i;
6367edd16368SStephen M. Cameron 	char temp_name[17];
6368edd16368SStephen M. Cameron 
6369edd16368SStephen M. Cameron 	dev_info(dev, "Controller Configuration information\n");
6370edd16368SStephen M. Cameron 	dev_info(dev, "------------------------------------\n");
6371edd16368SStephen M. Cameron 	for (i = 0; i < 4; i++)
6372edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->Signature[i]));
6373edd16368SStephen M. Cameron 	temp_name[4] = '\0';
6374edd16368SStephen M. Cameron 	dev_info(dev, "   Signature = %s\n", temp_name);
6375edd16368SStephen M. Cameron 	dev_info(dev, "   Spec Number = %d\n", readl(&(tb->SpecValence)));
6376edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods supported = 0x%x\n",
6377edd16368SStephen M. Cameron 	       readl(&(tb->TransportSupport)));
6378edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods active = 0x%x\n",
6379edd16368SStephen M. Cameron 	       readl(&(tb->TransportActive)));
6380edd16368SStephen M. Cameron 	dev_info(dev, "   Requested transport Method = 0x%x\n",
6381edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.TransportRequest)));
6382edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Delay = 0x%x\n",
6383edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntDelay)));
6384edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Count = 0x%x\n",
6385edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntCount)));
638669d6e33dSRobert Elliott 	dev_info(dev, "   Max outstanding commands = %d\n",
6387edd16368SStephen M. Cameron 	       readl(&(tb->CmdsOutMax)));
6388edd16368SStephen M. Cameron 	dev_info(dev, "   Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
6389edd16368SStephen M. Cameron 	for (i = 0; i < 16; i++)
6390edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->ServerName[i]));
6391edd16368SStephen M. Cameron 	temp_name[16] = '\0';
6392edd16368SStephen M. Cameron 	dev_info(dev, "   Server Name = %s\n", temp_name);
6393edd16368SStephen M. Cameron 	dev_info(dev, "   Heartbeat Counter = 0x%x\n\n\n",
6394edd16368SStephen M. Cameron 		readl(&(tb->HeartBeat)));
6395edd16368SStephen M. Cameron #endif				/* HPSA_DEBUG */
639658f8665cSStephen M. Cameron }
6397edd16368SStephen M. Cameron 
6398edd16368SStephen M. Cameron static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
6399edd16368SStephen M. Cameron {
6400edd16368SStephen M. Cameron 	int i, offset, mem_type, bar_type;
6401edd16368SStephen M. Cameron 
6402edd16368SStephen M. Cameron 	if (pci_bar_addr == PCI_BASE_ADDRESS_0)	/* looking for BAR zero? */
6403edd16368SStephen M. Cameron 		return 0;
6404edd16368SStephen M. Cameron 	offset = 0;
6405edd16368SStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
6406edd16368SStephen M. Cameron 		bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
6407edd16368SStephen M. Cameron 		if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
6408edd16368SStephen M. Cameron 			offset += 4;
6409edd16368SStephen M. Cameron 		else {
6410edd16368SStephen M. Cameron 			mem_type = pci_resource_flags(pdev, i) &
6411edd16368SStephen M. Cameron 			    PCI_BASE_ADDRESS_MEM_TYPE_MASK;
6412edd16368SStephen M. Cameron 			switch (mem_type) {
6413edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_32:
6414edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_1M:
6415edd16368SStephen M. Cameron 				offset += 4;	/* 32 bit */
6416edd16368SStephen M. Cameron 				break;
6417edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_64:
6418edd16368SStephen M. Cameron 				offset += 8;
6419edd16368SStephen M. Cameron 				break;
6420edd16368SStephen M. Cameron 			default:	/* reserved in PCI 2.2 */
6421edd16368SStephen M. Cameron 				dev_warn(&pdev->dev,
6422edd16368SStephen M. Cameron 				       "base address is invalid\n");
6423edd16368SStephen M. Cameron 				return -1;
6424edd16368SStephen M. Cameron 				break;
6425edd16368SStephen M. Cameron 			}
6426edd16368SStephen M. Cameron 		}
6427edd16368SStephen M. Cameron 		if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
6428edd16368SStephen M. Cameron 			return i + 1;
6429edd16368SStephen M. Cameron 	}
6430edd16368SStephen M. Cameron 	return -1;
6431edd16368SStephen M. Cameron }
6432edd16368SStephen M. Cameron 
6433cc64c817SRobert Elliott static void hpsa_disable_interrupt_mode(struct ctlr_info *h)
6434cc64c817SRobert Elliott {
6435cc64c817SRobert Elliott 	if (h->msix_vector) {
6436cc64c817SRobert Elliott 		if (h->pdev->msix_enabled)
6437cc64c817SRobert Elliott 			pci_disable_msix(h->pdev);
6438cc64c817SRobert Elliott 	} else if (h->msi_vector) {
6439cc64c817SRobert Elliott 		if (h->pdev->msi_enabled)
6440cc64c817SRobert Elliott 			pci_disable_msi(h->pdev);
6441cc64c817SRobert Elliott 	}
6442cc64c817SRobert Elliott }
6443cc64c817SRobert Elliott 
6444edd16368SStephen M. Cameron /* If MSI/MSI-X is supported by the kernel we will try to enable it on
6445050f7147SStephen Cameron  * controllers that are capable. If not, we use legacy INTx mode.
6446edd16368SStephen M. Cameron  */
64476f039790SGreg Kroah-Hartman static void hpsa_interrupt_mode(struct ctlr_info *h)
6448edd16368SStephen M. Cameron {
6449edd16368SStephen M. Cameron #ifdef CONFIG_PCI_MSI
6450254f796bSMatt Gates 	int err, i;
6451254f796bSMatt Gates 	struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
6452254f796bSMatt Gates 
6453254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++) {
6454254f796bSMatt Gates 		hpsa_msix_entries[i].vector = 0;
6455254f796bSMatt Gates 		hpsa_msix_entries[i].entry = i;
6456254f796bSMatt Gates 	}
6457edd16368SStephen M. Cameron 
6458edd16368SStephen M. Cameron 	/* Some boards advertise MSI but don't really support it */
64596b3f4c52SStephen M. Cameron 	if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
64606b3f4c52SStephen M. Cameron 	    (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
6461edd16368SStephen M. Cameron 		goto default_int_mode;
646255c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
6463050f7147SStephen Cameron 		dev_info(&h->pdev->dev, "MSI-X capable controller\n");
6464eee0f03aSHannes Reinecke 		h->msix_vector = MAX_REPLY_QUEUES;
6465f89439bcSStephen M. Cameron 		if (h->msix_vector > num_online_cpus())
6466f89439bcSStephen M. Cameron 			h->msix_vector = num_online_cpus();
646718fce3c4SAlexander Gordeev 		err = pci_enable_msix_range(h->pdev, hpsa_msix_entries,
646818fce3c4SAlexander Gordeev 					    1, h->msix_vector);
646918fce3c4SAlexander Gordeev 		if (err < 0) {
647018fce3c4SAlexander Gordeev 			dev_warn(&h->pdev->dev, "MSI-X init failed %d\n", err);
647118fce3c4SAlexander Gordeev 			h->msix_vector = 0;
647218fce3c4SAlexander Gordeev 			goto single_msi_mode;
647318fce3c4SAlexander Gordeev 		} else if (err < h->msix_vector) {
647455c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
6475edd16368SStephen M. Cameron 			       "available\n", err);
6476eee0f03aSHannes Reinecke 		}
647718fce3c4SAlexander Gordeev 		h->msix_vector = err;
6478eee0f03aSHannes Reinecke 		for (i = 0; i < h->msix_vector; i++)
6479eee0f03aSHannes Reinecke 			h->intr[i] = hpsa_msix_entries[i].vector;
6480eee0f03aSHannes Reinecke 		return;
6481edd16368SStephen M. Cameron 	}
648218fce3c4SAlexander Gordeev single_msi_mode:
648355c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
6484050f7147SStephen Cameron 		dev_info(&h->pdev->dev, "MSI capable controller\n");
648555c06c71SStephen M. Cameron 		if (!pci_enable_msi(h->pdev))
6486edd16368SStephen M. Cameron 			h->msi_vector = 1;
6487edd16368SStephen M. Cameron 		else
648855c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "MSI init failed\n");
6489edd16368SStephen M. Cameron 	}
6490edd16368SStephen M. Cameron default_int_mode:
6491edd16368SStephen M. Cameron #endif				/* CONFIG_PCI_MSI */
6492edd16368SStephen M. Cameron 	/* if we get here we're going to use the default interrupt mode */
6493a9a3a273SStephen M. Cameron 	h->intr[h->intr_mode] = h->pdev->irq;
6494edd16368SStephen M. Cameron }
6495edd16368SStephen M. Cameron 
64966f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
6497e5c880d1SStephen M. Cameron {
6498e5c880d1SStephen M. Cameron 	int i;
6499e5c880d1SStephen M. Cameron 	u32 subsystem_vendor_id, subsystem_device_id;
6500e5c880d1SStephen M. Cameron 
6501e5c880d1SStephen M. Cameron 	subsystem_vendor_id = pdev->subsystem_vendor;
6502e5c880d1SStephen M. Cameron 	subsystem_device_id = pdev->subsystem_device;
6503e5c880d1SStephen M. Cameron 	*board_id = ((subsystem_device_id << 16) & 0xffff0000) |
6504e5c880d1SStephen M. Cameron 		    subsystem_vendor_id;
6505e5c880d1SStephen M. Cameron 
6506e5c880d1SStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(products); i++)
6507e5c880d1SStephen M. Cameron 		if (*board_id == products[i].board_id)
6508e5c880d1SStephen M. Cameron 			return i;
6509e5c880d1SStephen M. Cameron 
65106798cc0aSStephen M. Cameron 	if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
65116798cc0aSStephen M. Cameron 		subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
65126798cc0aSStephen M. Cameron 		!hpsa_allow_any) {
6513e5c880d1SStephen M. Cameron 		dev_warn(&pdev->dev, "unrecognized board ID: "
6514e5c880d1SStephen M. Cameron 			"0x%08x, ignoring.\n", *board_id);
6515e5c880d1SStephen M. Cameron 			return -ENODEV;
6516e5c880d1SStephen M. Cameron 	}
6517e5c880d1SStephen M. Cameron 	return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
6518e5c880d1SStephen M. Cameron }
6519e5c880d1SStephen M. Cameron 
65206f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
65213a7774ceSStephen M. Cameron 				    unsigned long *memory_bar)
65223a7774ceSStephen M. Cameron {
65233a7774ceSStephen M. Cameron 	int i;
65243a7774ceSStephen M. Cameron 
65253a7774ceSStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
652612d2cd47SStephen M. Cameron 		if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
65273a7774ceSStephen M. Cameron 			/* addressing mode bits already removed */
652812d2cd47SStephen M. Cameron 			*memory_bar = pci_resource_start(pdev, i);
652912d2cd47SStephen M. Cameron 			dev_dbg(&pdev->dev, "memory BAR = %lx\n",
65303a7774ceSStephen M. Cameron 				*memory_bar);
65313a7774ceSStephen M. Cameron 			return 0;
65323a7774ceSStephen M. Cameron 		}
653312d2cd47SStephen M. Cameron 	dev_warn(&pdev->dev, "no memory BAR found\n");
65343a7774ceSStephen M. Cameron 	return -ENODEV;
65353a7774ceSStephen M. Cameron }
65363a7774ceSStephen M. Cameron 
65376f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
65386f039790SGreg Kroah-Hartman 				     int wait_for_ready)
65392c4c8c8bSStephen M. Cameron {
6540fe5389c8SStephen M. Cameron 	int i, iterations;
65412c4c8c8bSStephen M. Cameron 	u32 scratchpad;
6542fe5389c8SStephen M. Cameron 	if (wait_for_ready)
6543fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_READY_ITERATIONS;
6544fe5389c8SStephen M. Cameron 	else
6545fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
65462c4c8c8bSStephen M. Cameron 
6547fe5389c8SStephen M. Cameron 	for (i = 0; i < iterations; i++) {
6548fe5389c8SStephen M. Cameron 		scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
6549fe5389c8SStephen M. Cameron 		if (wait_for_ready) {
65502c4c8c8bSStephen M. Cameron 			if (scratchpad == HPSA_FIRMWARE_READY)
65512c4c8c8bSStephen M. Cameron 				return 0;
6552fe5389c8SStephen M. Cameron 		} else {
6553fe5389c8SStephen M. Cameron 			if (scratchpad != HPSA_FIRMWARE_READY)
6554fe5389c8SStephen M. Cameron 				return 0;
6555fe5389c8SStephen M. Cameron 		}
65562c4c8c8bSStephen M. Cameron 		msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
65572c4c8c8bSStephen M. Cameron 	}
6558fe5389c8SStephen M. Cameron 	dev_warn(&pdev->dev, "board not ready, timed out.\n");
65592c4c8c8bSStephen M. Cameron 	return -ENODEV;
65602c4c8c8bSStephen M. Cameron }
65612c4c8c8bSStephen M. Cameron 
65626f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
65636f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
6564a51fd47fSStephen M. Cameron 			       u64 *cfg_offset)
6565a51fd47fSStephen M. Cameron {
6566a51fd47fSStephen M. Cameron 	*cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
6567a51fd47fSStephen M. Cameron 	*cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
6568a51fd47fSStephen M. Cameron 	*cfg_base_addr &= (u32) 0x0000ffff;
6569a51fd47fSStephen M. Cameron 	*cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
6570a51fd47fSStephen M. Cameron 	if (*cfg_base_addr_index == -1) {
6571a51fd47fSStephen M. Cameron 		dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
6572a51fd47fSStephen M. Cameron 		return -ENODEV;
6573a51fd47fSStephen M. Cameron 	}
6574a51fd47fSStephen M. Cameron 	return 0;
6575a51fd47fSStephen M. Cameron }
6576a51fd47fSStephen M. Cameron 
6577195f2c65SRobert Elliott static void hpsa_free_cfgtables(struct ctlr_info *h)
6578195f2c65SRobert Elliott {
6579195f2c65SRobert Elliott 	if (h->transtable)
6580195f2c65SRobert Elliott 		iounmap(h->transtable);
6581195f2c65SRobert Elliott 	if (h->cfgtable)
6582195f2c65SRobert Elliott 		iounmap(h->cfgtable);
6583195f2c65SRobert Elliott }
6584195f2c65SRobert Elliott 
6585195f2c65SRobert Elliott /* Find and map CISS config table and transfer table
6586195f2c65SRobert Elliott + * several items must be unmapped (freed) later
6587195f2c65SRobert Elliott + * */
65886f039790SGreg Kroah-Hartman static int hpsa_find_cfgtables(struct ctlr_info *h)
6589edd16368SStephen M. Cameron {
659001a02ffcSStephen M. Cameron 	u64 cfg_offset;
659101a02ffcSStephen M. Cameron 	u32 cfg_base_addr;
659201a02ffcSStephen M. Cameron 	u64 cfg_base_addr_index;
6593303932fdSDon Brace 	u32 trans_offset;
6594a51fd47fSStephen M. Cameron 	int rc;
659577c4495cSStephen M. Cameron 
6596a51fd47fSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
6597a51fd47fSStephen M. Cameron 		&cfg_base_addr_index, &cfg_offset);
6598a51fd47fSStephen M. Cameron 	if (rc)
6599a51fd47fSStephen M. Cameron 		return rc;
660077c4495cSStephen M. Cameron 	h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
6601a51fd47fSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
6602cd3c81c4SRobert Elliott 	if (!h->cfgtable) {
6603cd3c81c4SRobert Elliott 		dev_err(&h->pdev->dev, "Failed mapping cfgtable\n");
660477c4495cSStephen M. Cameron 		return -ENOMEM;
6605cd3c81c4SRobert Elliott 	}
6606580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(h->cfgtable);
6607580ada3cSStephen M. Cameron 	if (rc)
6608580ada3cSStephen M. Cameron 		return rc;
660977c4495cSStephen M. Cameron 	/* Find performant mode table. */
6610a51fd47fSStephen M. Cameron 	trans_offset = readl(&h->cfgtable->TransMethodOffset);
661177c4495cSStephen M. Cameron 	h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
661277c4495cSStephen M. Cameron 				cfg_base_addr_index)+cfg_offset+trans_offset,
661377c4495cSStephen M. Cameron 				sizeof(*h->transtable));
6614195f2c65SRobert Elliott 	if (!h->transtable) {
6615195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "Failed mapping transfer table\n");
6616195f2c65SRobert Elliott 		hpsa_free_cfgtables(h);
661777c4495cSStephen M. Cameron 		return -ENOMEM;
6618195f2c65SRobert Elliott 	}
661977c4495cSStephen M. Cameron 	return 0;
662077c4495cSStephen M. Cameron }
662177c4495cSStephen M. Cameron 
66226f039790SGreg Kroah-Hartman static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
6623cba3d38bSStephen M. Cameron {
662441ce4c35SStephen Cameron #define MIN_MAX_COMMANDS 16
662541ce4c35SStephen Cameron 	BUILD_BUG_ON(MIN_MAX_COMMANDS <= HPSA_NRESERVED_CMDS);
662641ce4c35SStephen Cameron 
662741ce4c35SStephen Cameron 	h->max_commands = readl(&h->cfgtable->MaxPerformantModeCommands);
662872ceeaecSStephen M. Cameron 
662972ceeaecSStephen M. Cameron 	/* Limit commands in memory limited kdump scenario. */
663072ceeaecSStephen M. Cameron 	if (reset_devices && h->max_commands > 32)
663172ceeaecSStephen M. Cameron 		h->max_commands = 32;
663272ceeaecSStephen M. Cameron 
663341ce4c35SStephen Cameron 	if (h->max_commands < MIN_MAX_COMMANDS) {
663441ce4c35SStephen Cameron 		dev_warn(&h->pdev->dev,
663541ce4c35SStephen Cameron 			"Controller reports max supported commands of %d Using %d instead. Ensure that firmware is up to date.\n",
663641ce4c35SStephen Cameron 			h->max_commands,
663741ce4c35SStephen Cameron 			MIN_MAX_COMMANDS);
663841ce4c35SStephen Cameron 		h->max_commands = MIN_MAX_COMMANDS;
6639cba3d38bSStephen M. Cameron 	}
6640cba3d38bSStephen M. Cameron }
6641cba3d38bSStephen M. Cameron 
6642c7ee65b3SWebb Scales /* If the controller reports that the total max sg entries is greater than 512,
6643c7ee65b3SWebb Scales  * then we know that chained SG blocks work.  (Original smart arrays did not
6644c7ee65b3SWebb Scales  * support chained SG blocks and would return zero for max sg entries.)
6645c7ee65b3SWebb Scales  */
6646c7ee65b3SWebb Scales static int hpsa_supports_chained_sg_blocks(struct ctlr_info *h)
6647c7ee65b3SWebb Scales {
6648c7ee65b3SWebb Scales 	return h->maxsgentries > 512;
6649c7ee65b3SWebb Scales }
6650c7ee65b3SWebb Scales 
6651b93d7536SStephen M. Cameron /* Interrogate the hardware for some limits:
6652b93d7536SStephen M. Cameron  * max commands, max SG elements without chaining, and with chaining,
6653b93d7536SStephen M. Cameron  * SG chain block size, etc.
6654b93d7536SStephen M. Cameron  */
66556f039790SGreg Kroah-Hartman static void hpsa_find_board_params(struct ctlr_info *h)
6656b93d7536SStephen M. Cameron {
6657cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
665845fcb86eSStephen Cameron 	h->nr_cmds = h->max_commands;
6659b93d7536SStephen M. Cameron 	h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
6660283b4a9bSStephen M. Cameron 	h->fw_support = readl(&(h->cfgtable->misc_fw_support));
6661c7ee65b3SWebb Scales 	if (hpsa_supports_chained_sg_blocks(h)) {
6662c7ee65b3SWebb Scales 		/* Limit in-command s/g elements to 32 save dma'able memory. */
6663b93d7536SStephen M. Cameron 		h->max_cmd_sg_entries = 32;
66641a63ea6fSWebb Scales 		h->chainsize = h->maxsgentries - h->max_cmd_sg_entries;
6665b93d7536SStephen M. Cameron 		h->maxsgentries--; /* save one for chain pointer */
6666b93d7536SStephen M. Cameron 	} else {
6667c7ee65b3SWebb Scales 		/*
6668c7ee65b3SWebb Scales 		 * Original smart arrays supported at most 31 s/g entries
6669c7ee65b3SWebb Scales 		 * embedded inline in the command (trying to use more
6670c7ee65b3SWebb Scales 		 * would lock up the controller)
6671c7ee65b3SWebb Scales 		 */
6672c7ee65b3SWebb Scales 		h->max_cmd_sg_entries = 31;
66731a63ea6fSWebb Scales 		h->maxsgentries = 31; /* default to traditional values */
6674c7ee65b3SWebb Scales 		h->chainsize = 0;
6675b93d7536SStephen M. Cameron 	}
667675167d2cSStephen M. Cameron 
667775167d2cSStephen M. Cameron 	/* Find out what task management functions are supported and cache */
667875167d2cSStephen M. Cameron 	h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
66790e7a7fceSScott Teel 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
66800e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
66810e7a7fceSScott Teel 	if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
66820e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
6683b93d7536SStephen M. Cameron }
6684b93d7536SStephen M. Cameron 
668576c46e49SStephen M. Cameron static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
668676c46e49SStephen M. Cameron {
66870fc9fd40SAkinobu Mita 	if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
6688050f7147SStephen Cameron 		dev_err(&h->pdev->dev, "not a valid CISS config table\n");
668976c46e49SStephen M. Cameron 		return false;
669076c46e49SStephen M. Cameron 	}
669176c46e49SStephen M. Cameron 	return true;
669276c46e49SStephen M. Cameron }
669376c46e49SStephen M. Cameron 
669497a5e98cSStephen M. Cameron static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
6695f7c39101SStephen M. Cameron {
669697a5e98cSStephen M. Cameron 	u32 driver_support;
6697f7c39101SStephen M. Cameron 
669897a5e98cSStephen M. Cameron 	driver_support = readl(&(h->cfgtable->driver_support));
66990b9e7b74SArnd Bergmann 	/* Need to enable prefetch in the SCSI core for 6400 in x86 */
67000b9e7b74SArnd Bergmann #ifdef CONFIG_X86
670197a5e98cSStephen M. Cameron 	driver_support |= ENABLE_SCSI_PREFETCH;
6702f7c39101SStephen M. Cameron #endif
670328e13446SStephen M. Cameron 	driver_support |= ENABLE_UNIT_ATTN;
670428e13446SStephen M. Cameron 	writel(driver_support, &(h->cfgtable->driver_support));
6705f7c39101SStephen M. Cameron }
6706f7c39101SStephen M. Cameron 
67073d0eab67SStephen M. Cameron /* Disable DMA prefetch for the P600.  Otherwise an ASIC bug may result
67083d0eab67SStephen M. Cameron  * in a prefetch beyond physical memory.
67093d0eab67SStephen M. Cameron  */
67103d0eab67SStephen M. Cameron static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
67113d0eab67SStephen M. Cameron {
67123d0eab67SStephen M. Cameron 	u32 dma_prefetch;
67133d0eab67SStephen M. Cameron 
67143d0eab67SStephen M. Cameron 	if (h->board_id != 0x3225103C)
67153d0eab67SStephen M. Cameron 		return;
67163d0eab67SStephen M. Cameron 	dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
67173d0eab67SStephen M. Cameron 	dma_prefetch |= 0x8000;
67183d0eab67SStephen M. Cameron 	writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
67193d0eab67SStephen M. Cameron }
67203d0eab67SStephen M. Cameron 
6721c706a795SRobert Elliott static int hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
672276438d08SStephen M. Cameron {
672376438d08SStephen M. Cameron 	int i;
672476438d08SStephen M. Cameron 	u32 doorbell_value;
672576438d08SStephen M. Cameron 	unsigned long flags;
672676438d08SStephen M. Cameron 	/* wait until the clear_event_notify bit 6 is cleared by controller. */
6727007e7aa9SRobert Elliott 	for (i = 0; i < MAX_CLEAR_EVENT_WAIT; i++) {
672876438d08SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
672976438d08SStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
673076438d08SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
673176438d08SStephen M. Cameron 		if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
6732c706a795SRobert Elliott 			goto done;
673376438d08SStephen M. Cameron 		/* delay and try again */
6734007e7aa9SRobert Elliott 		msleep(CLEAR_EVENT_WAIT_INTERVAL);
673576438d08SStephen M. Cameron 	}
6736c706a795SRobert Elliott 	return -ENODEV;
6737c706a795SRobert Elliott done:
6738c706a795SRobert Elliott 	return 0;
673976438d08SStephen M. Cameron }
674076438d08SStephen M. Cameron 
6741c706a795SRobert Elliott static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
6742eb6b2ae9SStephen M. Cameron {
6743eb6b2ae9SStephen M. Cameron 	int i;
67446eaf46fdSStephen M. Cameron 	u32 doorbell_value;
67456eaf46fdSStephen M. Cameron 	unsigned long flags;
6746eb6b2ae9SStephen M. Cameron 
6747eb6b2ae9SStephen M. Cameron 	/* under certain very rare conditions, this can take awhile.
6748eb6b2ae9SStephen M. Cameron 	 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
6749eb6b2ae9SStephen M. Cameron 	 * as we enter this code.)
6750eb6b2ae9SStephen M. Cameron 	 */
6751007e7aa9SRobert Elliott 	for (i = 0; i < MAX_MODE_CHANGE_WAIT; i++) {
675225163bd5SWebb Scales 		if (h->remove_in_progress)
675325163bd5SWebb Scales 			goto done;
67546eaf46fdSStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
67556eaf46fdSStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
67566eaf46fdSStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
6757382be668SDan Carpenter 		if (!(doorbell_value & CFGTBL_ChangeReq))
6758c706a795SRobert Elliott 			goto done;
6759eb6b2ae9SStephen M. Cameron 		/* delay and try again */
6760007e7aa9SRobert Elliott 		msleep(MODE_CHANGE_WAIT_INTERVAL);
6761eb6b2ae9SStephen M. Cameron 	}
6762c706a795SRobert Elliott 	return -ENODEV;
6763c706a795SRobert Elliott done:
6764c706a795SRobert Elliott 	return 0;
67653f4336f3SStephen M. Cameron }
67663f4336f3SStephen M. Cameron 
6767c706a795SRobert Elliott /* return -ENODEV or other reason on error, 0 on success */
67686f039790SGreg Kroah-Hartman static int hpsa_enter_simple_mode(struct ctlr_info *h)
67693f4336f3SStephen M. Cameron {
67703f4336f3SStephen M. Cameron 	u32 trans_support;
67713f4336f3SStephen M. Cameron 
67723f4336f3SStephen M. Cameron 	trans_support = readl(&(h->cfgtable->TransportSupport));
67733f4336f3SStephen M. Cameron 	if (!(trans_support & SIMPLE_MODE))
67743f4336f3SStephen M. Cameron 		return -ENOTSUPP;
67753f4336f3SStephen M. Cameron 
67763f4336f3SStephen M. Cameron 	h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
6777283b4a9bSStephen M. Cameron 
67783f4336f3SStephen M. Cameron 	/* Update the field, and then ring the doorbell */
67793f4336f3SStephen M. Cameron 	writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
6780b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
67813f4336f3SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
6782c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h))
6783c706a795SRobert Elliott 		goto error;
6784eb6b2ae9SStephen M. Cameron 	print_cfg_table(&h->pdev->dev, h->cfgtable);
6785283b4a9bSStephen M. Cameron 	if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
6786283b4a9bSStephen M. Cameron 		goto error;
6787960a30e7SStephen M. Cameron 	h->transMethod = CFGTBL_Trans_Simple;
6788eb6b2ae9SStephen M. Cameron 	return 0;
6789283b4a9bSStephen M. Cameron error:
6790050f7147SStephen Cameron 	dev_err(&h->pdev->dev, "failed to enter simple mode\n");
6791283b4a9bSStephen M. Cameron 	return -ENODEV;
6792eb6b2ae9SStephen M. Cameron }
6793eb6b2ae9SStephen M. Cameron 
6794195f2c65SRobert Elliott /* free items allocated or mapped by hpsa_pci_init */
6795195f2c65SRobert Elliott static void hpsa_free_pci_init(struct ctlr_info *h)
6796195f2c65SRobert Elliott {
6797195f2c65SRobert Elliott 	hpsa_free_cfgtables(h);			/* pci_init 4 */
6798195f2c65SRobert Elliott 	iounmap(h->vaddr);			/* pci_init 3 */
6799195f2c65SRobert Elliott 	hpsa_disable_interrupt_mode(h);		/* pci_init 2 */
6800195f2c65SRobert Elliott 	pci_release_regions(h->pdev);		/* pci_init 2 */
6801195f2c65SRobert Elliott 	pci_disable_device(h->pdev);		/* pci_init 1 */
6802195f2c65SRobert Elliott }
6803195f2c65SRobert Elliott 
6804195f2c65SRobert Elliott /* several items must be freed later */
68056f039790SGreg Kroah-Hartman static int hpsa_pci_init(struct ctlr_info *h)
680677c4495cSStephen M. Cameron {
6807eb6b2ae9SStephen M. Cameron 	int prod_index, err;
6808edd16368SStephen M. Cameron 
6809e5c880d1SStephen M. Cameron 	prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
6810e5c880d1SStephen M. Cameron 	if (prod_index < 0)
681160f923b9SRobert Elliott 		return prod_index;
6812e5c880d1SStephen M. Cameron 	h->product_name = products[prod_index].product_name;
6813e5c880d1SStephen M. Cameron 	h->access = *(products[prod_index].access);
6814e5c880d1SStephen M. Cameron 
68159b5c48c2SStephen Cameron 	h->needs_abort_tags_swizzled =
68169b5c48c2SStephen Cameron 		ctlr_needs_abort_tags_swizzled(h->board_id);
68179b5c48c2SStephen Cameron 
6818e5a44df8SMatthew Garrett 	pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
6819e5a44df8SMatthew Garrett 			       PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
6820e5a44df8SMatthew Garrett 
682155c06c71SStephen M. Cameron 	err = pci_enable_device(h->pdev);
6822edd16368SStephen M. Cameron 	if (err) {
6823195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "failed to enable PCI device\n");
6824edd16368SStephen M. Cameron 		return err;
6825edd16368SStephen M. Cameron 	}
6826edd16368SStephen M. Cameron 
6827f79cfec6SStephen M. Cameron 	err = pci_request_regions(h->pdev, HPSA);
6828edd16368SStephen M. Cameron 	if (err) {
682955c06c71SStephen M. Cameron 		dev_err(&h->pdev->dev,
6830195f2c65SRobert Elliott 			"failed to obtain PCI resources\n");
6831195f2c65SRobert Elliott 		goto clean1;	/* pci */
6832edd16368SStephen M. Cameron 	}
68334fa604e1SRobert Elliott 
68344fa604e1SRobert Elliott 	pci_set_master(h->pdev);
68354fa604e1SRobert Elliott 
68366b3f4c52SStephen M. Cameron 	hpsa_interrupt_mode(h);
683712d2cd47SStephen M. Cameron 	err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
68383a7774ceSStephen M. Cameron 	if (err)
6839195f2c65SRobert Elliott 		goto clean2;	/* intmode+region, pci */
6840edd16368SStephen M. Cameron 	h->vaddr = remap_pci_mem(h->paddr, 0x250);
6841204892e9SStephen M. Cameron 	if (!h->vaddr) {
6842195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "failed to remap PCI mem\n");
6843204892e9SStephen M. Cameron 		err = -ENOMEM;
6844195f2c65SRobert Elliott 		goto clean2;	/* intmode+region, pci */
6845204892e9SStephen M. Cameron 	}
6846fe5389c8SStephen M. Cameron 	err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
68472c4c8c8bSStephen M. Cameron 	if (err)
6848195f2c65SRobert Elliott 		goto clean3;	/* vaddr, intmode+region, pci */
684977c4495cSStephen M. Cameron 	err = hpsa_find_cfgtables(h);
685077c4495cSStephen M. Cameron 	if (err)
6851195f2c65SRobert Elliott 		goto clean3;	/* vaddr, intmode+region, pci */
6852b93d7536SStephen M. Cameron 	hpsa_find_board_params(h);
6853edd16368SStephen M. Cameron 
685476c46e49SStephen M. Cameron 	if (!hpsa_CISS_signature_present(h)) {
6855edd16368SStephen M. Cameron 		err = -ENODEV;
6856195f2c65SRobert Elliott 		goto clean4;	/* cfgtables, vaddr, intmode+region, pci */
6857edd16368SStephen M. Cameron 	}
685897a5e98cSStephen M. Cameron 	hpsa_set_driver_support_bits(h);
68593d0eab67SStephen M. Cameron 	hpsa_p600_dma_prefetch_quirk(h);
6860eb6b2ae9SStephen M. Cameron 	err = hpsa_enter_simple_mode(h);
6861eb6b2ae9SStephen M. Cameron 	if (err)
6862195f2c65SRobert Elliott 		goto clean4;	/* cfgtables, vaddr, intmode+region, pci */
6863edd16368SStephen M. Cameron 	return 0;
6864edd16368SStephen M. Cameron 
6865195f2c65SRobert Elliott clean4:	/* cfgtables, vaddr, intmode+region, pci */
6866195f2c65SRobert Elliott 	hpsa_free_cfgtables(h);
6867195f2c65SRobert Elliott clean3:	/* vaddr, intmode+region, pci */
6868204892e9SStephen M. Cameron 	iounmap(h->vaddr);
6869195f2c65SRobert Elliott clean2:	/* intmode+region, pci */
6870195f2c65SRobert Elliott 	hpsa_disable_interrupt_mode(h);
687155c06c71SStephen M. Cameron 	pci_release_regions(h->pdev);
6872195f2c65SRobert Elliott clean1:	/* pci */
6873195f2c65SRobert Elliott 	pci_disable_device(h->pdev);
6874edd16368SStephen M. Cameron 	return err;
6875edd16368SStephen M. Cameron }
6876edd16368SStephen M. Cameron 
68776f039790SGreg Kroah-Hartman static void hpsa_hba_inquiry(struct ctlr_info *h)
6878339b2b14SStephen M. Cameron {
6879339b2b14SStephen M. Cameron 	int rc;
6880339b2b14SStephen M. Cameron 
6881339b2b14SStephen M. Cameron #define HBA_INQUIRY_BYTE_COUNT 64
6882339b2b14SStephen M. Cameron 	h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
6883339b2b14SStephen M. Cameron 	if (!h->hba_inquiry_data)
6884339b2b14SStephen M. Cameron 		return;
6885339b2b14SStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
6886339b2b14SStephen M. Cameron 		h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
6887339b2b14SStephen M. Cameron 	if (rc != 0) {
6888339b2b14SStephen M. Cameron 		kfree(h->hba_inquiry_data);
6889339b2b14SStephen M. Cameron 		h->hba_inquiry_data = NULL;
6890339b2b14SStephen M. Cameron 	}
6891339b2b14SStephen M. Cameron }
6892339b2b14SStephen M. Cameron 
68936b6c1cd7STomas Henzl static int hpsa_init_reset_devices(struct pci_dev *pdev, u32 board_id)
6894edd16368SStephen M. Cameron {
68951df8552aSStephen M. Cameron 	int rc, i;
68963b747298STomas Henzl 	void __iomem *vaddr;
6897edd16368SStephen M. Cameron 
68984c2a8c40SStephen M. Cameron 	if (!reset_devices)
68994c2a8c40SStephen M. Cameron 		return 0;
69004c2a8c40SStephen M. Cameron 
6901132aa220STomas Henzl 	/* kdump kernel is loading, we don't know in which state is
6902132aa220STomas Henzl 	 * the pci interface. The dev->enable_cnt is equal zero
6903132aa220STomas Henzl 	 * so we call enable+disable, wait a while and switch it on.
6904132aa220STomas Henzl 	 */
6905132aa220STomas Henzl 	rc = pci_enable_device(pdev);
6906132aa220STomas Henzl 	if (rc) {
6907132aa220STomas Henzl 		dev_warn(&pdev->dev, "Failed to enable PCI device\n");
6908132aa220STomas Henzl 		return -ENODEV;
6909132aa220STomas Henzl 	}
6910132aa220STomas Henzl 	pci_disable_device(pdev);
6911132aa220STomas Henzl 	msleep(260);			/* a randomly chosen number */
6912132aa220STomas Henzl 	rc = pci_enable_device(pdev);
6913132aa220STomas Henzl 	if (rc) {
6914132aa220STomas Henzl 		dev_warn(&pdev->dev, "failed to enable device.\n");
6915132aa220STomas Henzl 		return -ENODEV;
6916132aa220STomas Henzl 	}
69174fa604e1SRobert Elliott 
6918859c75abSTomas Henzl 	pci_set_master(pdev);
69194fa604e1SRobert Elliott 
69203b747298STomas Henzl 	vaddr = pci_ioremap_bar(pdev, 0);
69213b747298STomas Henzl 	if (vaddr == NULL) {
69223b747298STomas Henzl 		rc = -ENOMEM;
69233b747298STomas Henzl 		goto out_disable;
69243b747298STomas Henzl 	}
69253b747298STomas Henzl 	writel(SA5_INTR_OFF, vaddr + SA5_REPLY_INTR_MASK_OFFSET);
69263b747298STomas Henzl 	iounmap(vaddr);
69273b747298STomas Henzl 
69281df8552aSStephen M. Cameron 	/* Reset the controller with a PCI power-cycle or via doorbell */
69296b6c1cd7STomas Henzl 	rc = hpsa_kdump_hard_reset_controller(pdev, board_id);
6930edd16368SStephen M. Cameron 
69311df8552aSStephen M. Cameron 	/* -ENOTSUPP here means we cannot reset the controller
69321df8552aSStephen M. Cameron 	 * but it's already (and still) up and running in
693318867659SStephen M. Cameron 	 * "performant mode".  Or, it might be 640x, which can't reset
693418867659SStephen M. Cameron 	 * due to concerns about shared bbwc between 6402/6404 pair.
69351df8552aSStephen M. Cameron 	 */
6936adf1b3a3SRobert Elliott 	if (rc)
6937132aa220STomas Henzl 		goto out_disable;
6938edd16368SStephen M. Cameron 
6939edd16368SStephen M. Cameron 	/* Now try to get the controller to respond to a no-op */
69401ba66c9cSRobert Elliott 	dev_info(&pdev->dev, "Waiting for controller to respond to no-op\n");
6941edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
6942edd16368SStephen M. Cameron 		if (hpsa_noop(pdev) == 0)
6943edd16368SStephen M. Cameron 			break;
6944edd16368SStephen M. Cameron 		else
6945edd16368SStephen M. Cameron 			dev_warn(&pdev->dev, "no-op failed%s\n",
6946edd16368SStephen M. Cameron 					(i < 11 ? "; re-trying" : ""));
6947edd16368SStephen M. Cameron 	}
6948132aa220STomas Henzl 
6949132aa220STomas Henzl out_disable:
6950132aa220STomas Henzl 
6951132aa220STomas Henzl 	pci_disable_device(pdev);
6952132aa220STomas Henzl 	return rc;
6953edd16368SStephen M. Cameron }
6954edd16368SStephen M. Cameron 
69551fb7c98aSRobert Elliott static void hpsa_free_cmd_pool(struct ctlr_info *h)
69561fb7c98aSRobert Elliott {
69571fb7c98aSRobert Elliott 	kfree(h->cmd_pool_bits);
69581fb7c98aSRobert Elliott 	if (h->cmd_pool)
69591fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
69601fb7c98aSRobert Elliott 				h->nr_cmds * sizeof(struct CommandList),
69611fb7c98aSRobert Elliott 				h->cmd_pool,
69621fb7c98aSRobert Elliott 				h->cmd_pool_dhandle);
69631fb7c98aSRobert Elliott 	if (h->errinfo_pool)
69641fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
69651fb7c98aSRobert Elliott 				h->nr_cmds * sizeof(struct ErrorInfo),
69661fb7c98aSRobert Elliott 				h->errinfo_pool,
69671fb7c98aSRobert Elliott 				h->errinfo_pool_dhandle);
69681fb7c98aSRobert Elliott }
69691fb7c98aSRobert Elliott 
6970d37ffbe4SRobert Elliott static int hpsa_alloc_cmd_pool(struct ctlr_info *h)
69712e9d1b36SStephen M. Cameron {
69722e9d1b36SStephen M. Cameron 	h->cmd_pool_bits = kzalloc(
69732e9d1b36SStephen M. Cameron 		DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
69742e9d1b36SStephen M. Cameron 		sizeof(unsigned long), GFP_KERNEL);
69752e9d1b36SStephen M. Cameron 	h->cmd_pool = pci_alloc_consistent(h->pdev,
69762e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->cmd_pool),
69772e9d1b36SStephen M. Cameron 		    &(h->cmd_pool_dhandle));
69782e9d1b36SStephen M. Cameron 	h->errinfo_pool = pci_alloc_consistent(h->pdev,
69792e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->errinfo_pool),
69802e9d1b36SStephen M. Cameron 		    &(h->errinfo_pool_dhandle));
69812e9d1b36SStephen M. Cameron 	if ((h->cmd_pool_bits == NULL)
69822e9d1b36SStephen M. Cameron 	    || (h->cmd_pool == NULL)
69832e9d1b36SStephen M. Cameron 	    || (h->errinfo_pool == NULL)) {
69842e9d1b36SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory in %s", __func__);
69852c143342SRobert Elliott 		goto clean_up;
69862e9d1b36SStephen M. Cameron 	}
6987360c73bdSStephen Cameron 	hpsa_preinitialize_commands(h);
69882e9d1b36SStephen M. Cameron 	return 0;
69892c143342SRobert Elliott clean_up:
69902c143342SRobert Elliott 	hpsa_free_cmd_pool(h);
69912c143342SRobert Elliott 	return -ENOMEM;
69922e9d1b36SStephen M. Cameron }
69932e9d1b36SStephen M. Cameron 
699441b3cf08SStephen M. Cameron static void hpsa_irq_affinity_hints(struct ctlr_info *h)
699541b3cf08SStephen M. Cameron {
6996ec429952SFabian Frederick 	int i, cpu;
699741b3cf08SStephen M. Cameron 
699841b3cf08SStephen M. Cameron 	cpu = cpumask_first(cpu_online_mask);
699941b3cf08SStephen M. Cameron 	for (i = 0; i < h->msix_vector; i++) {
7000ec429952SFabian Frederick 		irq_set_affinity_hint(h->intr[i], get_cpu_mask(cpu));
700141b3cf08SStephen M. Cameron 		cpu = cpumask_next(cpu, cpu_online_mask);
700241b3cf08SStephen M. Cameron 	}
700341b3cf08SStephen M. Cameron }
700441b3cf08SStephen M. Cameron 
7005ec501a18SRobert Elliott /* clear affinity hints and free MSI-X, MSI, or legacy INTx vectors */
7006ec501a18SRobert Elliott static void hpsa_free_irqs(struct ctlr_info *h)
7007ec501a18SRobert Elliott {
7008ec501a18SRobert Elliott 	int i;
7009ec501a18SRobert Elliott 
7010ec501a18SRobert Elliott 	if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
7011ec501a18SRobert Elliott 		/* Single reply queue, only one irq to free */
7012ec501a18SRobert Elliott 		i = h->intr_mode;
7013ec501a18SRobert Elliott 		irq_set_affinity_hint(h->intr[i], NULL);
7014ec501a18SRobert Elliott 		free_irq(h->intr[i], &h->q[i]);
7015ec501a18SRobert Elliott 		return;
7016ec501a18SRobert Elliott 	}
7017ec501a18SRobert Elliott 
7018ec501a18SRobert Elliott 	for (i = 0; i < h->msix_vector; i++) {
7019ec501a18SRobert Elliott 		irq_set_affinity_hint(h->intr[i], NULL);
7020ec501a18SRobert Elliott 		free_irq(h->intr[i], &h->q[i]);
7021ec501a18SRobert Elliott 	}
7022a4e17fc1SRobert Elliott 	for (; i < MAX_REPLY_QUEUES; i++)
7023a4e17fc1SRobert Elliott 		h->q[i] = 0;
7024ec501a18SRobert Elliott }
7025ec501a18SRobert Elliott 
70269ee61794SRobert Elliott /* returns 0 on success; cleans up and returns -Enn on error */
70279ee61794SRobert Elliott static int hpsa_request_irqs(struct ctlr_info *h,
70280ae01a32SStephen M. Cameron 	irqreturn_t (*msixhandler)(int, void *),
70290ae01a32SStephen M. Cameron 	irqreturn_t (*intxhandler)(int, void *))
70300ae01a32SStephen M. Cameron {
7031254f796bSMatt Gates 	int rc, i;
70320ae01a32SStephen M. Cameron 
7033254f796bSMatt Gates 	/*
7034254f796bSMatt Gates 	 * initialize h->q[x] = x so that interrupt handlers know which
7035254f796bSMatt Gates 	 * queue to process.
7036254f796bSMatt Gates 	 */
7037254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++)
7038254f796bSMatt Gates 		h->q[i] = (u8) i;
7039254f796bSMatt Gates 
7040eee0f03aSHannes Reinecke 	if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
7041254f796bSMatt Gates 		/* If performant mode and MSI-X, use multiple reply queues */
7042a4e17fc1SRobert Elliott 		for (i = 0; i < h->msix_vector; i++) {
7043254f796bSMatt Gates 			rc = request_irq(h->intr[i], msixhandler,
7044254f796bSMatt Gates 					0, h->devname,
7045254f796bSMatt Gates 					&h->q[i]);
7046a4e17fc1SRobert Elliott 			if (rc) {
7047a4e17fc1SRobert Elliott 				int j;
7048a4e17fc1SRobert Elliott 
7049a4e17fc1SRobert Elliott 				dev_err(&h->pdev->dev,
7050a4e17fc1SRobert Elliott 					"failed to get irq %d for %s\n",
7051a4e17fc1SRobert Elliott 				       h->intr[i], h->devname);
7052a4e17fc1SRobert Elliott 				for (j = 0; j < i; j++) {
7053a4e17fc1SRobert Elliott 					free_irq(h->intr[j], &h->q[j]);
7054a4e17fc1SRobert Elliott 					h->q[j] = 0;
7055a4e17fc1SRobert Elliott 				}
7056a4e17fc1SRobert Elliott 				for (; j < MAX_REPLY_QUEUES; j++)
7057a4e17fc1SRobert Elliott 					h->q[j] = 0;
7058a4e17fc1SRobert Elliott 				return rc;
7059a4e17fc1SRobert Elliott 			}
7060a4e17fc1SRobert Elliott 		}
706141b3cf08SStephen M. Cameron 		hpsa_irq_affinity_hints(h);
7062254f796bSMatt Gates 	} else {
7063254f796bSMatt Gates 		/* Use single reply pool */
7064eee0f03aSHannes Reinecke 		if (h->msix_vector > 0 || h->msi_vector) {
7065254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
7066254f796bSMatt Gates 				msixhandler, 0, h->devname,
7067254f796bSMatt Gates 				&h->q[h->intr_mode]);
7068254f796bSMatt Gates 		} else {
7069254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
7070254f796bSMatt Gates 				intxhandler, IRQF_SHARED, h->devname,
7071254f796bSMatt Gates 				&h->q[h->intr_mode]);
7072254f796bSMatt Gates 		}
7073254f796bSMatt Gates 	}
70740ae01a32SStephen M. Cameron 	if (rc) {
7075195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "failed to get irq %d for %s\n",
70760ae01a32SStephen M. Cameron 		       h->intr[h->intr_mode], h->devname);
7077195f2c65SRobert Elliott 		hpsa_free_irqs(h);
70780ae01a32SStephen M. Cameron 		return -ENODEV;
70790ae01a32SStephen M. Cameron 	}
70800ae01a32SStephen M. Cameron 	return 0;
70810ae01a32SStephen M. Cameron }
70820ae01a32SStephen M. Cameron 
70836f039790SGreg Kroah-Hartman static int hpsa_kdump_soft_reset(struct ctlr_info *h)
708464670ac8SStephen M. Cameron {
7085*bf43caf3SRobert Elliott 	hpsa_send_host_reset(h, RAID_CTLR_LUNID, HPSA_RESET_TYPE_CONTROLLER);
708664670ac8SStephen M. Cameron 
708764670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
708864670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) {
708964670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
709064670ac8SStephen M. Cameron 		return -1;
709164670ac8SStephen M. Cameron 	}
709264670ac8SStephen M. Cameron 
709364670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
709464670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) {
709564670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Board failed to become ready "
709664670ac8SStephen M. Cameron 			"after soft reset.\n");
709764670ac8SStephen M. Cameron 		return -1;
709864670ac8SStephen M. Cameron 	}
709964670ac8SStephen M. Cameron 
710064670ac8SStephen M. Cameron 	return 0;
710164670ac8SStephen M. Cameron }
710264670ac8SStephen M. Cameron 
7103072b0518SStephen M. Cameron static void hpsa_free_reply_queues(struct ctlr_info *h)
7104072b0518SStephen M. Cameron {
7105072b0518SStephen M. Cameron 	int i;
7106072b0518SStephen M. Cameron 
7107072b0518SStephen M. Cameron 	for (i = 0; i < h->nreply_queues; i++) {
7108072b0518SStephen M. Cameron 		if (!h->reply_queue[i].head)
7109072b0518SStephen M. Cameron 			continue;
71101fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
71111fb7c98aSRobert Elliott 					h->reply_queue_size,
71121fb7c98aSRobert Elliott 					h->reply_queue[i].head,
71131fb7c98aSRobert Elliott 					h->reply_queue[i].busaddr);
7114072b0518SStephen M. Cameron 		h->reply_queue[i].head = NULL;
7115072b0518SStephen M. Cameron 		h->reply_queue[i].busaddr = 0;
7116072b0518SStephen M. Cameron 	}
7117072b0518SStephen M. Cameron }
7118072b0518SStephen M. Cameron 
71190097f0f4SStephen M. Cameron static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
71200097f0f4SStephen M. Cameron {
7121cc64c817SRobert Elliott 	hpsa_free_irqs(h);
712264670ac8SStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
712364670ac8SStephen M. Cameron 	hpsa_free_cmd_pool(h);
71241fb7c98aSRobert Elliott 	kfree(h->blockFetchTable);		/* perf 2 */
71251fb7c98aSRobert Elliott 	hpsa_free_reply_queues(h);		/* perf 1 */
71261fb7c98aSRobert Elliott 	hpsa_free_ioaccel1_cmd_and_bft(h);	/* perf 1 */
71271fb7c98aSRobert Elliott 	hpsa_free_ioaccel2_cmd_and_bft(h);	/* perf 1 */
7128195f2c65SRobert Elliott 	hpsa_free_cfgtables(h);			/* pci_init 4 */
7129195f2c65SRobert Elliott 	iounmap(h->vaddr);			/* pci_init 3 */
7130195f2c65SRobert Elliott 	hpsa_disable_interrupt_mode(h);		/* pci_init 2 */
7131132aa220STomas Henzl 	pci_disable_device(h->pdev);
7132195f2c65SRobert Elliott 	pci_release_regions(h->pdev);		/* pci_init 2 */
713364670ac8SStephen M. Cameron 	kfree(h);
713464670ac8SStephen M. Cameron }
713564670ac8SStephen M. Cameron 
7136a0c12413SStephen M. Cameron /* Called when controller lockup detected. */
7137f2405db8SDon Brace static void fail_all_outstanding_cmds(struct ctlr_info *h)
7138a0c12413SStephen M. Cameron {
7139281a7fd0SWebb Scales 	int i, refcount;
7140281a7fd0SWebb Scales 	struct CommandList *c;
714125163bd5SWebb Scales 	int failcount = 0;
7142a0c12413SStephen M. Cameron 
7143080ef1ccSDon Brace 	flush_workqueue(h->resubmit_wq); /* ensure all cmds are fully built */
7144f2405db8SDon Brace 	for (i = 0; i < h->nr_cmds; i++) {
7145f2405db8SDon Brace 		c = h->cmd_pool + i;
7146281a7fd0SWebb Scales 		refcount = atomic_inc_return(&c->refcount);
7147281a7fd0SWebb Scales 		if (refcount > 1) {
714825163bd5SWebb Scales 			c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
71495a3d16f5SStephen M. Cameron 			finish_cmd(c);
7150433b5f4dSStephen Cameron 			atomic_dec(&h->commands_outstanding);
715125163bd5SWebb Scales 			failcount++;
7152a0c12413SStephen M. Cameron 		}
7153281a7fd0SWebb Scales 		cmd_free(h, c);
7154281a7fd0SWebb Scales 	}
715525163bd5SWebb Scales 	dev_warn(&h->pdev->dev,
715625163bd5SWebb Scales 		"failed %d commands in fail_all\n", failcount);
7157a0c12413SStephen M. Cameron }
7158a0c12413SStephen M. Cameron 
7159094963daSStephen M. Cameron static void set_lockup_detected_for_all_cpus(struct ctlr_info *h, u32 value)
7160094963daSStephen M. Cameron {
7161c8ed0010SRusty Russell 	int cpu;
7162094963daSStephen M. Cameron 
7163c8ed0010SRusty Russell 	for_each_online_cpu(cpu) {
7164094963daSStephen M. Cameron 		u32 *lockup_detected;
7165094963daSStephen M. Cameron 		lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
7166094963daSStephen M. Cameron 		*lockup_detected = value;
7167094963daSStephen M. Cameron 	}
7168094963daSStephen M. Cameron 	wmb(); /* be sure the per-cpu variables are out to memory */
7169094963daSStephen M. Cameron }
7170094963daSStephen M. Cameron 
7171a0c12413SStephen M. Cameron static void controller_lockup_detected(struct ctlr_info *h)
7172a0c12413SStephen M. Cameron {
7173a0c12413SStephen M. Cameron 	unsigned long flags;
7174094963daSStephen M. Cameron 	u32 lockup_detected;
7175a0c12413SStephen M. Cameron 
7176a0c12413SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
7177a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
7178094963daSStephen M. Cameron 	lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
7179094963daSStephen M. Cameron 	if (!lockup_detected) {
7180094963daSStephen M. Cameron 		/* no heartbeat, but controller gave us a zero. */
7181094963daSStephen M. Cameron 		dev_warn(&h->pdev->dev,
718225163bd5SWebb Scales 			"lockup detected after %d but scratchpad register is zero\n",
718325163bd5SWebb Scales 			h->heartbeat_sample_interval / HZ);
7184094963daSStephen M. Cameron 		lockup_detected = 0xffffffff;
7185094963daSStephen M. Cameron 	}
7186094963daSStephen M. Cameron 	set_lockup_detected_for_all_cpus(h, lockup_detected);
7187a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
718825163bd5SWebb Scales 	dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x after %d\n",
718925163bd5SWebb Scales 			lockup_detected, h->heartbeat_sample_interval / HZ);
7190a0c12413SStephen M. Cameron 	pci_disable_device(h->pdev);
7191f2405db8SDon Brace 	fail_all_outstanding_cmds(h);
7192a0c12413SStephen M. Cameron }
7193a0c12413SStephen M. Cameron 
719425163bd5SWebb Scales static int detect_controller_lockup(struct ctlr_info *h)
7195a0c12413SStephen M. Cameron {
7196a0c12413SStephen M. Cameron 	u64 now;
7197a0c12413SStephen M. Cameron 	u32 heartbeat;
7198a0c12413SStephen M. Cameron 	unsigned long flags;
7199a0c12413SStephen M. Cameron 
7200a0c12413SStephen M. Cameron 	now = get_jiffies_64();
7201a0c12413SStephen M. Cameron 	/* If we've received an interrupt recently, we're ok. */
7202a0c12413SStephen M. Cameron 	if (time_after64(h->last_intr_timestamp +
7203e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
720425163bd5SWebb Scales 		return false;
7205a0c12413SStephen M. Cameron 
7206a0c12413SStephen M. Cameron 	/*
7207a0c12413SStephen M. Cameron 	 * If we've already checked the heartbeat recently, we're ok.
7208a0c12413SStephen M. Cameron 	 * This could happen if someone sends us a signal. We
7209a0c12413SStephen M. Cameron 	 * otherwise don't care about signals in this thread.
7210a0c12413SStephen M. Cameron 	 */
7211a0c12413SStephen M. Cameron 	if (time_after64(h->last_heartbeat_timestamp +
7212e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
721325163bd5SWebb Scales 		return false;
7214a0c12413SStephen M. Cameron 
7215a0c12413SStephen M. Cameron 	/* If heartbeat has not changed since we last looked, we're not ok. */
7216a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
7217a0c12413SStephen M. Cameron 	heartbeat = readl(&h->cfgtable->HeartBeat);
7218a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
7219a0c12413SStephen M. Cameron 	if (h->last_heartbeat == heartbeat) {
7220a0c12413SStephen M. Cameron 		controller_lockup_detected(h);
722125163bd5SWebb Scales 		return true;
7222a0c12413SStephen M. Cameron 	}
7223a0c12413SStephen M. Cameron 
7224a0c12413SStephen M. Cameron 	/* We're ok. */
7225a0c12413SStephen M. Cameron 	h->last_heartbeat = heartbeat;
7226a0c12413SStephen M. Cameron 	h->last_heartbeat_timestamp = now;
722725163bd5SWebb Scales 	return false;
7228a0c12413SStephen M. Cameron }
7229a0c12413SStephen M. Cameron 
72309846590eSStephen M. Cameron static void hpsa_ack_ctlr_events(struct ctlr_info *h)
723176438d08SStephen M. Cameron {
723276438d08SStephen M. Cameron 	int i;
723376438d08SStephen M. Cameron 	char *event_type;
723476438d08SStephen M. Cameron 
7235e4aa3e6aSStephen Cameron 	if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
7236e4aa3e6aSStephen Cameron 		return;
7237e4aa3e6aSStephen Cameron 
723876438d08SStephen M. Cameron 	/* Ask the controller to clear the events we're handling. */
72391f7cee8cSStephen M. Cameron 	if ((h->transMethod & (CFGTBL_Trans_io_accel1
72401f7cee8cSStephen M. Cameron 			| CFGTBL_Trans_io_accel2)) &&
724176438d08SStephen M. Cameron 		(h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
724276438d08SStephen M. Cameron 		 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
724376438d08SStephen M. Cameron 
724476438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
724576438d08SStephen M. Cameron 			event_type = "state change";
724676438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
724776438d08SStephen M. Cameron 			event_type = "configuration change";
724876438d08SStephen M. Cameron 		/* Stop sending new RAID offload reqs via the IO accelerator */
724976438d08SStephen M. Cameron 		scsi_block_requests(h->scsi_host);
725076438d08SStephen M. Cameron 		for (i = 0; i < h->ndevices; i++)
725176438d08SStephen M. Cameron 			h->dev[i]->offload_enabled = 0;
725223100dd9SStephen M. Cameron 		hpsa_drain_accel_commands(h);
725376438d08SStephen M. Cameron 		/* Set 'accelerator path config change' bit */
725476438d08SStephen M. Cameron 		dev_warn(&h->pdev->dev,
725576438d08SStephen M. Cameron 			"Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
725676438d08SStephen M. Cameron 			h->events, event_type);
725776438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
725876438d08SStephen M. Cameron 		/* Set the "clear event notify field update" bit 6 */
725976438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
726076438d08SStephen M. Cameron 		/* Wait until ctlr clears 'clear event notify field', bit 6 */
726176438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
726276438d08SStephen M. Cameron 		scsi_unblock_requests(h->scsi_host);
726376438d08SStephen M. Cameron 	} else {
726476438d08SStephen M. Cameron 		/* Acknowledge controller notification events. */
726576438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
726676438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
726776438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
726876438d08SStephen M. Cameron #if 0
726976438d08SStephen M. Cameron 		writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
727076438d08SStephen M. Cameron 		hpsa_wait_for_mode_change_ack(h);
727176438d08SStephen M. Cameron #endif
727276438d08SStephen M. Cameron 	}
72739846590eSStephen M. Cameron 	return;
727476438d08SStephen M. Cameron }
727576438d08SStephen M. Cameron 
727676438d08SStephen M. Cameron /* Check a register on the controller to see if there are configuration
727776438d08SStephen M. Cameron  * changes (added/changed/removed logical drives, etc.) which mean that
7278e863d68eSScott Teel  * we should rescan the controller for devices.
7279e863d68eSScott Teel  * Also check flag for driver-initiated rescan.
728076438d08SStephen M. Cameron  */
72819846590eSStephen M. Cameron static int hpsa_ctlr_needs_rescan(struct ctlr_info *h)
728276438d08SStephen M. Cameron {
728376438d08SStephen M. Cameron 	if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
72849846590eSStephen M. Cameron 		return 0;
728576438d08SStephen M. Cameron 
728676438d08SStephen M. Cameron 	h->events = readl(&(h->cfgtable->event_notify));
72879846590eSStephen M. Cameron 	return h->events & RESCAN_REQUIRED_EVENT_BITS;
72889846590eSStephen M. Cameron }
728976438d08SStephen M. Cameron 
729076438d08SStephen M. Cameron /*
72919846590eSStephen M. Cameron  * Check if any of the offline devices have become ready
729276438d08SStephen M. Cameron  */
72939846590eSStephen M. Cameron static int hpsa_offline_devices_ready(struct ctlr_info *h)
72949846590eSStephen M. Cameron {
72959846590eSStephen M. Cameron 	unsigned long flags;
72969846590eSStephen M. Cameron 	struct offline_device_entry *d;
72979846590eSStephen M. Cameron 	struct list_head *this, *tmp;
72989846590eSStephen M. Cameron 
72999846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
73009846590eSStephen M. Cameron 	list_for_each_safe(this, tmp, &h->offline_device_list) {
73019846590eSStephen M. Cameron 		d = list_entry(this, struct offline_device_entry,
73029846590eSStephen M. Cameron 				offline_list);
73039846590eSStephen M. Cameron 		spin_unlock_irqrestore(&h->offline_device_lock, flags);
7304d1fea47cSStephen M. Cameron 		if (!hpsa_volume_offline(h, d->scsi3addr)) {
7305d1fea47cSStephen M. Cameron 			spin_lock_irqsave(&h->offline_device_lock, flags);
7306d1fea47cSStephen M. Cameron 			list_del(&d->offline_list);
7307d1fea47cSStephen M. Cameron 			spin_unlock_irqrestore(&h->offline_device_lock, flags);
73089846590eSStephen M. Cameron 			return 1;
7309d1fea47cSStephen M. Cameron 		}
73109846590eSStephen M. Cameron 		spin_lock_irqsave(&h->offline_device_lock, flags);
731176438d08SStephen M. Cameron 	}
73129846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
73139846590eSStephen M. Cameron 	return 0;
73149846590eSStephen M. Cameron }
73159846590eSStephen M. Cameron 
73166636e7f4SDon Brace static void hpsa_rescan_ctlr_worker(struct work_struct *work)
7317a0c12413SStephen M. Cameron {
7318a0c12413SStephen M. Cameron 	unsigned long flags;
73198a98db73SStephen M. Cameron 	struct ctlr_info *h = container_of(to_delayed_work(work),
73206636e7f4SDon Brace 					struct ctlr_info, rescan_ctlr_work);
73216636e7f4SDon Brace 
73226636e7f4SDon Brace 
73236636e7f4SDon Brace 	if (h->remove_in_progress)
73248a98db73SStephen M. Cameron 		return;
73259846590eSStephen M. Cameron 
73269846590eSStephen M. Cameron 	if (hpsa_ctlr_needs_rescan(h) || hpsa_offline_devices_ready(h)) {
73279846590eSStephen M. Cameron 		scsi_host_get(h->scsi_host);
73289846590eSStephen M. Cameron 		hpsa_ack_ctlr_events(h);
73299846590eSStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
73309846590eSStephen M. Cameron 		scsi_host_put(h->scsi_host);
73319846590eSStephen M. Cameron 	}
73326636e7f4SDon Brace 	spin_lock_irqsave(&h->lock, flags);
73336636e7f4SDon Brace 	if (!h->remove_in_progress)
73346636e7f4SDon Brace 		queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
73356636e7f4SDon Brace 				h->heartbeat_sample_interval);
73366636e7f4SDon Brace 	spin_unlock_irqrestore(&h->lock, flags);
73376636e7f4SDon Brace }
73386636e7f4SDon Brace 
73396636e7f4SDon Brace static void hpsa_monitor_ctlr_worker(struct work_struct *work)
73406636e7f4SDon Brace {
73416636e7f4SDon Brace 	unsigned long flags;
73426636e7f4SDon Brace 	struct ctlr_info *h = container_of(to_delayed_work(work),
73436636e7f4SDon Brace 					struct ctlr_info, monitor_ctlr_work);
73446636e7f4SDon Brace 
73456636e7f4SDon Brace 	detect_controller_lockup(h);
73466636e7f4SDon Brace 	if (lockup_detected(h))
73476636e7f4SDon Brace 		return;
73489846590eSStephen M. Cameron 
73498a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
73506636e7f4SDon Brace 	if (!h->remove_in_progress)
73518a98db73SStephen M. Cameron 		schedule_delayed_work(&h->monitor_ctlr_work,
73528a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
73538a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
7354a0c12413SStephen M. Cameron }
7355a0c12413SStephen M. Cameron 
73566636e7f4SDon Brace static struct workqueue_struct *hpsa_create_controller_wq(struct ctlr_info *h,
73576636e7f4SDon Brace 						char *name)
73586636e7f4SDon Brace {
73596636e7f4SDon Brace 	struct workqueue_struct *wq = NULL;
73606636e7f4SDon Brace 
7361397ea9cbSDon Brace 	wq = alloc_ordered_workqueue("%s_%d_hpsa", 0, name, h->ctlr);
73626636e7f4SDon Brace 	if (!wq)
73636636e7f4SDon Brace 		dev_err(&h->pdev->dev, "failed to create %s workqueue\n", name);
73646636e7f4SDon Brace 
73656636e7f4SDon Brace 	return wq;
73666636e7f4SDon Brace }
73676636e7f4SDon Brace 
73686f039790SGreg Kroah-Hartman static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
73694c2a8c40SStephen M. Cameron {
73704c2a8c40SStephen M. Cameron 	int dac, rc;
73714c2a8c40SStephen M. Cameron 	struct ctlr_info *h;
737264670ac8SStephen M. Cameron 	int try_soft_reset = 0;
737364670ac8SStephen M. Cameron 	unsigned long flags;
73746b6c1cd7STomas Henzl 	u32 board_id;
73754c2a8c40SStephen M. Cameron 
73764c2a8c40SStephen M. Cameron 	if (number_of_controllers == 0)
73774c2a8c40SStephen M. Cameron 		printk(KERN_INFO DRIVER_NAME "\n");
73784c2a8c40SStephen M. Cameron 
73796b6c1cd7STomas Henzl 	rc = hpsa_lookup_board_id(pdev, &board_id);
73806b6c1cd7STomas Henzl 	if (rc < 0) {
73816b6c1cd7STomas Henzl 		dev_warn(&pdev->dev, "Board ID not found\n");
73826b6c1cd7STomas Henzl 		return rc;
73836b6c1cd7STomas Henzl 	}
73846b6c1cd7STomas Henzl 
73856b6c1cd7STomas Henzl 	rc = hpsa_init_reset_devices(pdev, board_id);
738664670ac8SStephen M. Cameron 	if (rc) {
738764670ac8SStephen M. Cameron 		if (rc != -ENOTSUPP)
73884c2a8c40SStephen M. Cameron 			return rc;
738964670ac8SStephen M. Cameron 		/* If the reset fails in a particular way (it has no way to do
739064670ac8SStephen M. Cameron 		 * a proper hard reset, so returns -ENOTSUPP) we can try to do
739164670ac8SStephen M. Cameron 		 * a soft reset once we get the controller configured up to the
739264670ac8SStephen M. Cameron 		 * point that it can accept a command.
739364670ac8SStephen M. Cameron 		 */
739464670ac8SStephen M. Cameron 		try_soft_reset = 1;
739564670ac8SStephen M. Cameron 		rc = 0;
739664670ac8SStephen M. Cameron 	}
739764670ac8SStephen M. Cameron 
739864670ac8SStephen M. Cameron reinit_after_soft_reset:
73994c2a8c40SStephen M. Cameron 
7400303932fdSDon Brace 	/* Command structures must be aligned on a 32-byte boundary because
7401303932fdSDon Brace 	 * the 5 lower bits of the address are used by the hardware. and by
7402303932fdSDon Brace 	 * the driver.  See comments in hpsa.h for more info.
7403303932fdSDon Brace 	 */
7404303932fdSDon Brace 	BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
7405edd16368SStephen M. Cameron 	h = kzalloc(sizeof(*h), GFP_KERNEL);
7406edd16368SStephen M. Cameron 	if (!h)
7407ecd9aad4SStephen M. Cameron 		return -ENOMEM;
7408edd16368SStephen M. Cameron 
740955c06c71SStephen M. Cameron 	h->pdev = pdev;
7410a9a3a273SStephen M. Cameron 	h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
74119846590eSStephen M. Cameron 	INIT_LIST_HEAD(&h->offline_device_list);
74126eaf46fdSStephen M. Cameron 	spin_lock_init(&h->lock);
74139846590eSStephen M. Cameron 	spin_lock_init(&h->offline_device_lock);
74146eaf46fdSStephen M. Cameron 	spin_lock_init(&h->scan_lock);
741534f0c627SDon Brace 	atomic_set(&h->passthru_cmds_avail, HPSA_MAX_CONCURRENT_PASSTHRUS);
74169b5c48c2SStephen Cameron 	atomic_set(&h->abort_cmds_available, HPSA_CMDS_RESERVED_FOR_ABORTS);
7417094963daSStephen M. Cameron 
74186636e7f4SDon Brace 	h->rescan_ctlr_wq = hpsa_create_controller_wq(h, "rescan");
74196636e7f4SDon Brace 	if (!h->rescan_ctlr_wq) {
7420080ef1ccSDon Brace 		rc = -ENOMEM;
7421080ef1ccSDon Brace 		goto clean1;
7422080ef1ccSDon Brace 	}
74236636e7f4SDon Brace 
74246636e7f4SDon Brace 	h->resubmit_wq = hpsa_create_controller_wq(h, "resubmit");
74256636e7f4SDon Brace 	if (!h->resubmit_wq) {
74266636e7f4SDon Brace 		rc = -ENOMEM;
74276636e7f4SDon Brace 		goto clean1;
74286636e7f4SDon Brace 	}
74296636e7f4SDon Brace 
7430094963daSStephen M. Cameron 	/* Allocate and clear per-cpu variable lockup_detected */
7431094963daSStephen M. Cameron 	h->lockup_detected = alloc_percpu(u32);
74322a5ac326SStephen M. Cameron 	if (!h->lockup_detected) {
74332a5ac326SStephen M. Cameron 		rc = -ENOMEM;
7434094963daSStephen M. Cameron 		goto clean1;
74352a5ac326SStephen M. Cameron 	}
7436094963daSStephen M. Cameron 	set_lockup_detected_for_all_cpus(h, 0);
7437094963daSStephen M. Cameron 
743855c06c71SStephen M. Cameron 	rc = hpsa_pci_init(h);
7439ecd9aad4SStephen M. Cameron 	if (rc != 0)
7440edd16368SStephen M. Cameron 		goto clean1;
7441edd16368SStephen M. Cameron 
7442f79cfec6SStephen M. Cameron 	sprintf(h->devname, HPSA "%d", number_of_controllers);
7443edd16368SStephen M. Cameron 	h->ctlr = number_of_controllers;
7444edd16368SStephen M. Cameron 	number_of_controllers++;
7445edd16368SStephen M. Cameron 
7446edd16368SStephen M. Cameron 	/* configure PCI DMA stuff */
7447ecd9aad4SStephen M. Cameron 	rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
7448ecd9aad4SStephen M. Cameron 	if (rc == 0) {
7449edd16368SStephen M. Cameron 		dac = 1;
7450ecd9aad4SStephen M. Cameron 	} else {
7451ecd9aad4SStephen M. Cameron 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7452ecd9aad4SStephen M. Cameron 		if (rc == 0) {
7453edd16368SStephen M. Cameron 			dac = 0;
7454ecd9aad4SStephen M. Cameron 		} else {
7455edd16368SStephen M. Cameron 			dev_err(&pdev->dev, "no suitable DMA available\n");
7456195f2c65SRobert Elliott 			goto clean2;
7457edd16368SStephen M. Cameron 		}
7458ecd9aad4SStephen M. Cameron 	}
7459edd16368SStephen M. Cameron 
7460edd16368SStephen M. Cameron 	/* make sure the board interrupts are off */
7461edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
746210f66018SStephen M. Cameron 
74639ee61794SRobert Elliott 	if (hpsa_request_irqs(h, do_hpsa_intr_msi, do_hpsa_intr_intx))
7464edd16368SStephen M. Cameron 		goto clean2;
7465303932fdSDon Brace 	dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
7466303932fdSDon Brace 	       h->devname, pdev->device,
7467a9a3a273SStephen M. Cameron 	       h->intr[h->intr_mode], dac ? "" : " not");
7468d37ffbe4SRobert Elliott 	rc = hpsa_alloc_cmd_pool(h);
74698947fd10SRobert Elliott 	if (rc)
74708947fd10SRobert Elliott 		goto clean2_and_free_irqs;
747133a2ffceSStephen M. Cameron 	if (hpsa_allocate_sg_chain_blocks(h))
747233a2ffceSStephen M. Cameron 		goto clean4;
7473a08a8471SStephen M. Cameron 	init_waitqueue_head(&h->scan_wait_queue);
74749b5c48c2SStephen Cameron 	init_waitqueue_head(&h->abort_cmd_wait_queue);
7475a08a8471SStephen M. Cameron 	h->scan_finished = 1; /* no scan currently in progress */
7476edd16368SStephen M. Cameron 
7477edd16368SStephen M. Cameron 	pci_set_drvdata(pdev, h);
74789a41338eSStephen M. Cameron 	h->ndevices = 0;
7479316b221aSStephen M. Cameron 	h->hba_mode_enabled = 0;
74809a41338eSStephen M. Cameron 	h->scsi_host = NULL;
74819a41338eSStephen M. Cameron 	spin_lock_init(&h->devlock);
748264670ac8SStephen M. Cameron 	hpsa_put_ctlr_into_performant_mode(h);
748364670ac8SStephen M. Cameron 
748464670ac8SStephen M. Cameron 	/* At this point, the controller is ready to take commands.
748564670ac8SStephen M. Cameron 	 * Now, if reset_devices and the hard reset didn't work, try
748664670ac8SStephen M. Cameron 	 * the soft reset and see if that works.
748764670ac8SStephen M. Cameron 	 */
748864670ac8SStephen M. Cameron 	if (try_soft_reset) {
748964670ac8SStephen M. Cameron 
749064670ac8SStephen M. Cameron 		/* This is kind of gross.  We may or may not get a completion
749164670ac8SStephen M. Cameron 		 * from the soft reset command, and if we do, then the value
749264670ac8SStephen M. Cameron 		 * from the fifo may or may not be valid.  So, we wait 10 secs
749364670ac8SStephen M. Cameron 		 * after the reset throwing away any completions we get during
749464670ac8SStephen M. Cameron 		 * that time.  Unregister the interrupt handler and register
749564670ac8SStephen M. Cameron 		 * fake ones to scoop up any residual completions.
749664670ac8SStephen M. Cameron 		 */
749764670ac8SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
749864670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
749964670ac8SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
7500ec501a18SRobert Elliott 		hpsa_free_irqs(h);
75019ee61794SRobert Elliott 		rc = hpsa_request_irqs(h, hpsa_msix_discard_completions,
750264670ac8SStephen M. Cameron 					hpsa_intx_discard_completions);
750364670ac8SStephen M. Cameron 		if (rc) {
75049ee61794SRobert Elliott 			dev_warn(&h->pdev->dev,
75059ee61794SRobert Elliott 				"Failed to request_irq after soft reset.\n");
750664670ac8SStephen M. Cameron 			goto clean4;
750764670ac8SStephen M. Cameron 		}
750864670ac8SStephen M. Cameron 
750964670ac8SStephen M. Cameron 		rc = hpsa_kdump_soft_reset(h);
751064670ac8SStephen M. Cameron 		if (rc)
751164670ac8SStephen M. Cameron 			/* Neither hard nor soft reset worked, we're hosed. */
751264670ac8SStephen M. Cameron 			goto clean4;
751364670ac8SStephen M. Cameron 
751464670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev, "Board READY.\n");
751564670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev,
751664670ac8SStephen M. Cameron 			"Waiting for stale completions to drain.\n");
751764670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_ON);
751864670ac8SStephen M. Cameron 		msleep(10000);
751964670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
752064670ac8SStephen M. Cameron 
752164670ac8SStephen M. Cameron 		rc = controller_reset_failed(h->cfgtable);
752264670ac8SStephen M. Cameron 		if (rc)
752364670ac8SStephen M. Cameron 			dev_info(&h->pdev->dev,
752464670ac8SStephen M. Cameron 				"Soft reset appears to have failed.\n");
752564670ac8SStephen M. Cameron 
752664670ac8SStephen M. Cameron 		/* since the controller's reset, we have to go back and re-init
752764670ac8SStephen M. Cameron 		 * everything.  Easiest to just forget what we've done and do it
752864670ac8SStephen M. Cameron 		 * all over again.
752964670ac8SStephen M. Cameron 		 */
753064670ac8SStephen M. Cameron 		hpsa_undo_allocations_after_kdump_soft_reset(h);
753164670ac8SStephen M. Cameron 		try_soft_reset = 0;
753264670ac8SStephen M. Cameron 		if (rc)
753364670ac8SStephen M. Cameron 			/* don't go to clean4, we already unallocated */
753464670ac8SStephen M. Cameron 			return -ENODEV;
753564670ac8SStephen M. Cameron 
753664670ac8SStephen M. Cameron 		goto reinit_after_soft_reset;
753764670ac8SStephen M. Cameron 	}
7538edd16368SStephen M. Cameron 
7539da0697bdSScott Teel 		/* Enable Accelerated IO path at driver layer */
7540da0697bdSScott Teel 		h->acciopath_status = 1;
7541da0697bdSScott Teel 
7542e863d68eSScott Teel 
7543edd16368SStephen M. Cameron 	/* Turn the interrupts on so we can service requests */
7544edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_ON);
7545edd16368SStephen M. Cameron 
7546339b2b14SStephen M. Cameron 	hpsa_hba_inquiry(h);
75474a4384ceSStephen Cameron 	rc = hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */
75484a4384ceSStephen Cameron 	if (rc)
75494a4384ceSStephen Cameron 		goto clean4;
75508a98db73SStephen M. Cameron 
75518a98db73SStephen M. Cameron 	/* Monitor the controller for firmware lockups */
75528a98db73SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
75538a98db73SStephen M. Cameron 	INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
75548a98db73SStephen M. Cameron 	schedule_delayed_work(&h->monitor_ctlr_work,
75558a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
75566636e7f4SDon Brace 	INIT_DELAYED_WORK(&h->rescan_ctlr_work, hpsa_rescan_ctlr_worker);
75576636e7f4SDon Brace 	queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
75586636e7f4SDon Brace 				h->heartbeat_sample_interval);
755988bf6d62SStephen M. Cameron 	return 0;
7560edd16368SStephen M. Cameron 
7561edd16368SStephen M. Cameron clean4:
756233a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
75632e9d1b36SStephen M. Cameron 	hpsa_free_cmd_pool(h);
75641fb7c98aSRobert Elliott 	hpsa_free_ioaccel1_cmd_and_bft(h);
75651fb7c98aSRobert Elliott 	hpsa_free_ioaccel2_cmd_and_bft(h);
75668947fd10SRobert Elliott clean2_and_free_irqs:
7567ec501a18SRobert Elliott 	hpsa_free_irqs(h);
7568edd16368SStephen M. Cameron clean2:
7569195f2c65SRobert Elliott 	hpsa_free_pci_init(h);
7570edd16368SStephen M. Cameron clean1:
7571080ef1ccSDon Brace 	if (h->resubmit_wq)
7572080ef1ccSDon Brace 		destroy_workqueue(h->resubmit_wq);
75736636e7f4SDon Brace 	if (h->rescan_ctlr_wq)
75746636e7f4SDon Brace 		destroy_workqueue(h->rescan_ctlr_wq);
7575094963daSStephen M. Cameron 	if (h->lockup_detected)
7576094963daSStephen M. Cameron 		free_percpu(h->lockup_detected);
7577edd16368SStephen M. Cameron 	kfree(h);
7578ecd9aad4SStephen M. Cameron 	return rc;
7579edd16368SStephen M. Cameron }
7580edd16368SStephen M. Cameron 
7581edd16368SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h)
7582edd16368SStephen M. Cameron {
7583edd16368SStephen M. Cameron 	char *flush_buf;
7584edd16368SStephen M. Cameron 	struct CommandList *c;
758525163bd5SWebb Scales 	int rc;
7586702890e3SStephen M. Cameron 
7587702890e3SStephen M. Cameron 	/* Don't bother trying to flush the cache if locked up */
758825163bd5SWebb Scales 	/* FIXME not necessary if do_simple_cmd does the check */
7589094963daSStephen M. Cameron 	if (unlikely(lockup_detected(h)))
7590702890e3SStephen M. Cameron 		return;
7591edd16368SStephen M. Cameron 	flush_buf = kzalloc(4, GFP_KERNEL);
7592edd16368SStephen M. Cameron 	if (!flush_buf)
7593edd16368SStephen M. Cameron 		return;
7594edd16368SStephen M. Cameron 
759545fcb86eSStephen Cameron 	c = cmd_alloc(h);
7596*bf43caf3SRobert Elliott 
7597a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
7598a2dac136SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_CMD)) {
7599a2dac136SStephen M. Cameron 		goto out;
7600a2dac136SStephen M. Cameron 	}
760125163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
760225163bd5SWebb Scales 					PCI_DMA_TODEVICE, NO_TIMEOUT);
760325163bd5SWebb Scales 	if (rc)
760425163bd5SWebb Scales 		goto out;
7605edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus != 0)
7606a2dac136SStephen M. Cameron out:
7607edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
7608edd16368SStephen M. Cameron 			"error flushing cache on controller\n");
760945fcb86eSStephen Cameron 	cmd_free(h, c);
7610edd16368SStephen M. Cameron 	kfree(flush_buf);
7611edd16368SStephen M. Cameron }
7612edd16368SStephen M. Cameron 
7613edd16368SStephen M. Cameron static void hpsa_shutdown(struct pci_dev *pdev)
7614edd16368SStephen M. Cameron {
7615edd16368SStephen M. Cameron 	struct ctlr_info *h;
7616edd16368SStephen M. Cameron 
7617edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
7618edd16368SStephen M. Cameron 	/* Turn board interrupts off  and send the flush cache command
7619edd16368SStephen M. Cameron 	 * sendcmd will turn off interrupt, and send the flush...
7620edd16368SStephen M. Cameron 	 * To write all data in the battery backed cache to disks
7621edd16368SStephen M. Cameron 	 */
7622edd16368SStephen M. Cameron 	hpsa_flush_cache(h);
7623edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
7624cc64c817SRobert Elliott 	hpsa_free_irqs(h);
7625cc64c817SRobert Elliott 	hpsa_disable_interrupt_mode(h);		/* pci_init 2 */
7626edd16368SStephen M. Cameron }
7627edd16368SStephen M. Cameron 
76286f039790SGreg Kroah-Hartman static void hpsa_free_device_info(struct ctlr_info *h)
762955e14e76SStephen M. Cameron {
763055e14e76SStephen M. Cameron 	int i;
763155e14e76SStephen M. Cameron 
763255e14e76SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++)
763355e14e76SStephen M. Cameron 		kfree(h->dev[i]);
763455e14e76SStephen M. Cameron }
763555e14e76SStephen M. Cameron 
76366f039790SGreg Kroah-Hartman static void hpsa_remove_one(struct pci_dev *pdev)
7637edd16368SStephen M. Cameron {
7638edd16368SStephen M. Cameron 	struct ctlr_info *h;
76398a98db73SStephen M. Cameron 	unsigned long flags;
7640edd16368SStephen M. Cameron 
7641edd16368SStephen M. Cameron 	if (pci_get_drvdata(pdev) == NULL) {
7642edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "unable to remove device\n");
7643edd16368SStephen M. Cameron 		return;
7644edd16368SStephen M. Cameron 	}
7645edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
76468a98db73SStephen M. Cameron 
76478a98db73SStephen M. Cameron 	/* Get rid of any controller monitoring work items */
76488a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
76498a98db73SStephen M. Cameron 	h->remove_in_progress = 1;
76508a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
76516636e7f4SDon Brace 	cancel_delayed_work_sync(&h->monitor_ctlr_work);
76526636e7f4SDon Brace 	cancel_delayed_work_sync(&h->rescan_ctlr_work);
76536636e7f4SDon Brace 	destroy_workqueue(h->rescan_ctlr_wq);
76546636e7f4SDon Brace 	destroy_workqueue(h->resubmit_wq);
7655edd16368SStephen M. Cameron 	hpsa_unregister_scsi(h);	/* unhook from SCSI subsystem */
7656cc64c817SRobert Elliott 
7657195f2c65SRobert Elliott 	/* includes hpsa_free_irqs */
7658195f2c65SRobert Elliott 	/* includes hpsa_disable_interrupt_mode - pci_init 2 */
7659edd16368SStephen M. Cameron 	hpsa_shutdown(pdev);
7660cc64c817SRobert Elliott 
766155e14e76SStephen M. Cameron 	hpsa_free_device_info(h);
766233a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
76631fb7c98aSRobert Elliott 	kfree(h->blockFetchTable);		/* perf 2 */
76641fb7c98aSRobert Elliott 	hpsa_free_reply_queues(h);		/* perf 1 */
76651fb7c98aSRobert Elliott 	hpsa_free_ioaccel1_cmd_and_bft(h);	/* perf 1 */
76661fb7c98aSRobert Elliott 	hpsa_free_ioaccel2_cmd_and_bft(h);	/* perf 1 */
76671fb7c98aSRobert Elliott 	hpsa_free_cmd_pool(h);			/* init_one 5 */
7668339b2b14SStephen M. Cameron 	kfree(h->hba_inquiry_data);
7669195f2c65SRobert Elliott 
7670195f2c65SRobert Elliott 	/* includes hpsa_disable_interrupt_mode - pci_init 2 */
7671195f2c65SRobert Elliott 	hpsa_free_pci_init(h);
7672195f2c65SRobert Elliott 
7673094963daSStephen M. Cameron 	free_percpu(h->lockup_detected);
7674edd16368SStephen M. Cameron 	kfree(h);
7675edd16368SStephen M. Cameron }
7676edd16368SStephen M. Cameron 
7677edd16368SStephen M. Cameron static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
7678edd16368SStephen M. Cameron 	__attribute__((unused)) pm_message_t state)
7679edd16368SStephen M. Cameron {
7680edd16368SStephen M. Cameron 	return -ENOSYS;
7681edd16368SStephen M. Cameron }
7682edd16368SStephen M. Cameron 
7683edd16368SStephen M. Cameron static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
7684edd16368SStephen M. Cameron {
7685edd16368SStephen M. Cameron 	return -ENOSYS;
7686edd16368SStephen M. Cameron }
7687edd16368SStephen M. Cameron 
7688edd16368SStephen M. Cameron static struct pci_driver hpsa_pci_driver = {
7689f79cfec6SStephen M. Cameron 	.name = HPSA,
7690edd16368SStephen M. Cameron 	.probe = hpsa_init_one,
76916f039790SGreg Kroah-Hartman 	.remove = hpsa_remove_one,
7692edd16368SStephen M. Cameron 	.id_table = hpsa_pci_device_id,	/* id_table */
7693edd16368SStephen M. Cameron 	.shutdown = hpsa_shutdown,
7694edd16368SStephen M. Cameron 	.suspend = hpsa_suspend,
7695edd16368SStephen M. Cameron 	.resume = hpsa_resume,
7696edd16368SStephen M. Cameron };
7697edd16368SStephen M. Cameron 
7698303932fdSDon Brace /* Fill in bucket_map[], given nsgs (the max number of
7699303932fdSDon Brace  * scatter gather elements supported) and bucket[],
7700303932fdSDon Brace  * which is an array of 8 integers.  The bucket[] array
7701303932fdSDon Brace  * contains 8 different DMA transfer sizes (in 16
7702303932fdSDon Brace  * byte increments) which the controller uses to fetch
7703303932fdSDon Brace  * commands.  This function fills in bucket_map[], which
7704303932fdSDon Brace  * maps a given number of scatter gather elements to one of
7705303932fdSDon Brace  * the 8 DMA transfer sizes.  The point of it is to allow the
7706303932fdSDon Brace  * controller to only do as much DMA as needed to fetch the
7707303932fdSDon Brace  * command, with the DMA transfer size encoded in the lower
7708303932fdSDon Brace  * bits of the command address.
7709303932fdSDon Brace  */
7710303932fdSDon Brace static void  calc_bucket_map(int bucket[], int num_buckets,
77112b08b3e9SDon Brace 	int nsgs, int min_blocks, u32 *bucket_map)
7712303932fdSDon Brace {
7713303932fdSDon Brace 	int i, j, b, size;
7714303932fdSDon Brace 
7715303932fdSDon Brace 	/* Note, bucket_map must have nsgs+1 entries. */
7716303932fdSDon Brace 	for (i = 0; i <= nsgs; i++) {
7717303932fdSDon Brace 		/* Compute size of a command with i SG entries */
7718e1f7de0cSMatt Gates 		size = i + min_blocks;
7719303932fdSDon Brace 		b = num_buckets; /* Assume the biggest bucket */
7720303932fdSDon Brace 		/* Find the bucket that is just big enough */
7721e1f7de0cSMatt Gates 		for (j = 0; j < num_buckets; j++) {
7722303932fdSDon Brace 			if (bucket[j] >= size) {
7723303932fdSDon Brace 				b = j;
7724303932fdSDon Brace 				break;
7725303932fdSDon Brace 			}
7726303932fdSDon Brace 		}
7727303932fdSDon Brace 		/* for a command with i SG entries, use bucket b. */
7728303932fdSDon Brace 		bucket_map[i] = b;
7729303932fdSDon Brace 	}
7730303932fdSDon Brace }
7731303932fdSDon Brace 
7732c706a795SRobert Elliott /* return -ENODEV or other reason on error, 0 on success */
7733c706a795SRobert Elliott static int hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
7734303932fdSDon Brace {
77356c311b57SStephen M. Cameron 	int i;
77366c311b57SStephen M. Cameron 	unsigned long register_value;
7737e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
7738e1f7de0cSMatt Gates 			(trans_support & CFGTBL_Trans_use_short_tags) |
7739e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix |
7740b9af4937SStephen M. Cameron 			(trans_support & (CFGTBL_Trans_io_accel1 |
7741b9af4937SStephen M. Cameron 				CFGTBL_Trans_io_accel2));
7742e1f7de0cSMatt Gates 	struct access_method access = SA5_performant_access;
7743def342bdSStephen M. Cameron 
7744def342bdSStephen M. Cameron 	/* This is a bit complicated.  There are 8 registers on
7745def342bdSStephen M. Cameron 	 * the controller which we write to to tell it 8 different
7746def342bdSStephen M. Cameron 	 * sizes of commands which there may be.  It's a way of
7747def342bdSStephen M. Cameron 	 * reducing the DMA done to fetch each command.  Encoded into
7748def342bdSStephen M. Cameron 	 * each command's tag are 3 bits which communicate to the controller
7749def342bdSStephen M. Cameron 	 * which of the eight sizes that command fits within.  The size of
7750def342bdSStephen M. Cameron 	 * each command depends on how many scatter gather entries there are.
7751def342bdSStephen M. Cameron 	 * Each SG entry requires 16 bytes.  The eight registers are programmed
7752def342bdSStephen M. Cameron 	 * with the number of 16-byte blocks a command of that size requires.
7753def342bdSStephen M. Cameron 	 * The smallest command possible requires 5 such 16 byte blocks.
7754d66ae08bSStephen M. Cameron 	 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
7755def342bdSStephen M. Cameron 	 * blocks.  Note, this only extends to the SG entries contained
7756def342bdSStephen M. Cameron 	 * within the command block, and does not extend to chained blocks
7757def342bdSStephen M. Cameron 	 * of SG elements.   bft[] contains the eight values we write to
7758def342bdSStephen M. Cameron 	 * the registers.  They are not evenly distributed, but have more
7759def342bdSStephen M. Cameron 	 * sizes for small commands, and fewer sizes for larger commands.
7760def342bdSStephen M. Cameron 	 */
7761d66ae08bSStephen M. Cameron 	int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
7762b9af4937SStephen M. Cameron #define MIN_IOACCEL2_BFT_ENTRY 5
7763b9af4937SStephen M. Cameron #define HPSA_IOACCEL2_HEADER_SZ 4
7764b9af4937SStephen M. Cameron 	int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
7765b9af4937SStephen M. Cameron 			13, 14, 15, 16, 17, 18, 19,
7766b9af4937SStephen M. Cameron 			HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
7767b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
7768b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
7769b9af4937SStephen M. Cameron 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
7770b9af4937SStephen M. Cameron 				 16 * MIN_IOACCEL2_BFT_ENTRY);
7771b9af4937SStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
7772d66ae08bSStephen M. Cameron 	BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
7773303932fdSDon Brace 	/*  5 = 1 s/g entry or 4k
7774303932fdSDon Brace 	 *  6 = 2 s/g entry or 8k
7775303932fdSDon Brace 	 *  8 = 4 s/g entry or 16k
7776303932fdSDon Brace 	 * 10 = 6 s/g entry or 24k
7777303932fdSDon Brace 	 */
7778303932fdSDon Brace 
7779b3a52e79SStephen M. Cameron 	/* If the controller supports either ioaccel method then
7780b3a52e79SStephen M. Cameron 	 * we can also use the RAID stack submit path that does not
7781b3a52e79SStephen M. Cameron 	 * perform the superfluous readl() after each command submission.
7782b3a52e79SStephen M. Cameron 	 */
7783b3a52e79SStephen M. Cameron 	if (trans_support & (CFGTBL_Trans_io_accel1 | CFGTBL_Trans_io_accel2))
7784b3a52e79SStephen M. Cameron 		access = SA5_performant_access_no_read;
7785b3a52e79SStephen M. Cameron 
7786303932fdSDon Brace 	/* Controller spec: zero out this buffer. */
7787072b0518SStephen M. Cameron 	for (i = 0; i < h->nreply_queues; i++)
7788072b0518SStephen M. Cameron 		memset(h->reply_queue[i].head, 0, h->reply_queue_size);
7789303932fdSDon Brace 
7790d66ae08bSStephen M. Cameron 	bft[7] = SG_ENTRIES_IN_CMD + 4;
7791d66ae08bSStephen M. Cameron 	calc_bucket_map(bft, ARRAY_SIZE(bft),
7792e1f7de0cSMatt Gates 				SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
7793303932fdSDon Brace 	for (i = 0; i < 8; i++)
7794303932fdSDon Brace 		writel(bft[i], &h->transtable->BlockFetch[i]);
7795303932fdSDon Brace 
7796303932fdSDon Brace 	/* size of controller ring buffer */
7797303932fdSDon Brace 	writel(h->max_commands, &h->transtable->RepQSize);
7798254f796bSMatt Gates 	writel(h->nreply_queues, &h->transtable->RepQCount);
7799303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrLow32);
7800303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrHigh32);
7801254f796bSMatt Gates 
7802254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
7803254f796bSMatt Gates 		writel(0, &h->transtable->RepQAddr[i].upper);
7804072b0518SStephen M. Cameron 		writel(h->reply_queue[i].busaddr,
7805254f796bSMatt Gates 			&h->transtable->RepQAddr[i].lower);
7806254f796bSMatt Gates 	}
7807254f796bSMatt Gates 
7808b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
7809e1f7de0cSMatt Gates 	writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
7810e1f7de0cSMatt Gates 	/*
7811e1f7de0cSMatt Gates 	 * enable outbound interrupt coalescing in accelerator mode;
7812e1f7de0cSMatt Gates 	 */
7813e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
7814e1f7de0cSMatt Gates 		access = SA5_ioaccel_mode1_access;
7815e1f7de0cSMatt Gates 		writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
7816e1f7de0cSMatt Gates 		writel(4, &h->cfgtable->HostWrite.CoalIntCount);
7817c349775eSScott Teel 	} else {
7818c349775eSScott Teel 		if (trans_support & CFGTBL_Trans_io_accel2) {
7819c349775eSScott Teel 			access = SA5_ioaccel_mode2_access;
7820c349775eSScott Teel 			writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
7821c349775eSScott Teel 			writel(4, &h->cfgtable->HostWrite.CoalIntCount);
7822c349775eSScott Teel 		}
7823e1f7de0cSMatt Gates 	}
7824303932fdSDon Brace 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
7825c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h)) {
7826c706a795SRobert Elliott 		dev_err(&h->pdev->dev,
7827c706a795SRobert Elliott 			"performant mode problem - doorbell timeout\n");
7828c706a795SRobert Elliott 		return -ENODEV;
7829c706a795SRobert Elliott 	}
7830303932fdSDon Brace 	register_value = readl(&(h->cfgtable->TransportActive));
7831303932fdSDon Brace 	if (!(register_value & CFGTBL_Trans_Performant)) {
7832050f7147SStephen Cameron 		dev_err(&h->pdev->dev,
7833050f7147SStephen Cameron 			"performant mode problem - transport not active\n");
7834c706a795SRobert Elliott 		return -ENODEV;
7835303932fdSDon Brace 	}
7836960a30e7SStephen M. Cameron 	/* Change the access methods to the performant access methods */
7837e1f7de0cSMatt Gates 	h->access = access;
7838e1f7de0cSMatt Gates 	h->transMethod = transMethod;
7839e1f7de0cSMatt Gates 
7840b9af4937SStephen M. Cameron 	if (!((trans_support & CFGTBL_Trans_io_accel1) ||
7841b9af4937SStephen M. Cameron 		(trans_support & CFGTBL_Trans_io_accel2)))
7842c706a795SRobert Elliott 		return 0;
7843e1f7de0cSMatt Gates 
7844b9af4937SStephen M. Cameron 	if (trans_support & CFGTBL_Trans_io_accel1) {
7845e1f7de0cSMatt Gates 		/* Set up I/O accelerator mode */
7846e1f7de0cSMatt Gates 		for (i = 0; i < h->nreply_queues; i++) {
7847e1f7de0cSMatt Gates 			writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
7848e1f7de0cSMatt Gates 			h->reply_queue[i].current_entry =
7849e1f7de0cSMatt Gates 				readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
7850e1f7de0cSMatt Gates 		}
7851283b4a9bSStephen M. Cameron 		bft[7] = h->ioaccel_maxsg + 8;
7852283b4a9bSStephen M. Cameron 		calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
7853e1f7de0cSMatt Gates 				h->ioaccel1_blockFetchTable);
7854e1f7de0cSMatt Gates 
7855e1f7de0cSMatt Gates 		/* initialize all reply queue entries to unused */
7856072b0518SStephen M. Cameron 		for (i = 0; i < h->nreply_queues; i++)
7857072b0518SStephen M. Cameron 			memset(h->reply_queue[i].head,
7858072b0518SStephen M. Cameron 				(u8) IOACCEL_MODE1_REPLY_UNUSED,
7859072b0518SStephen M. Cameron 				h->reply_queue_size);
7860e1f7de0cSMatt Gates 
7861e1f7de0cSMatt Gates 		/* set all the constant fields in the accelerator command
7862e1f7de0cSMatt Gates 		 * frames once at init time to save CPU cycles later.
7863e1f7de0cSMatt Gates 		 */
7864e1f7de0cSMatt Gates 		for (i = 0; i < h->nr_cmds; i++) {
7865e1f7de0cSMatt Gates 			struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
7866e1f7de0cSMatt Gates 
7867e1f7de0cSMatt Gates 			cp->function = IOACCEL1_FUNCTION_SCSIIO;
7868e1f7de0cSMatt Gates 			cp->err_info = (u32) (h->errinfo_pool_dhandle +
7869e1f7de0cSMatt Gates 					(i * sizeof(struct ErrorInfo)));
7870e1f7de0cSMatt Gates 			cp->err_info_len = sizeof(struct ErrorInfo);
7871e1f7de0cSMatt Gates 			cp->sgl_offset = IOACCEL1_SGLOFFSET;
78722b08b3e9SDon Brace 			cp->host_context_flags =
78732b08b3e9SDon Brace 				cpu_to_le16(IOACCEL1_HCFLAGS_CISS_FORMAT);
7874e1f7de0cSMatt Gates 			cp->timeout_sec = 0;
7875e1f7de0cSMatt Gates 			cp->ReplyQueue = 0;
787650a0decfSStephen M. Cameron 			cp->tag =
7877f2405db8SDon Brace 				cpu_to_le64((i << DIRECT_LOOKUP_SHIFT));
787850a0decfSStephen M. Cameron 			cp->host_addr =
787950a0decfSStephen M. Cameron 				cpu_to_le64(h->ioaccel_cmd_pool_dhandle +
7880e1f7de0cSMatt Gates 					(i * sizeof(struct io_accel1_cmd)));
7881e1f7de0cSMatt Gates 		}
7882b9af4937SStephen M. Cameron 	} else if (trans_support & CFGTBL_Trans_io_accel2) {
7883b9af4937SStephen M. Cameron 		u64 cfg_offset, cfg_base_addr_index;
7884b9af4937SStephen M. Cameron 		u32 bft2_offset, cfg_base_addr;
7885b9af4937SStephen M. Cameron 		int rc;
7886b9af4937SStephen M. Cameron 
7887b9af4937SStephen M. Cameron 		rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
7888b9af4937SStephen M. Cameron 			&cfg_base_addr_index, &cfg_offset);
7889b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
7890b9af4937SStephen M. Cameron 		bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
7891b9af4937SStephen M. Cameron 		calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
7892b9af4937SStephen M. Cameron 				4, h->ioaccel2_blockFetchTable);
7893b9af4937SStephen M. Cameron 		bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
7894b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct CfgTable,
7895b9af4937SStephen M. Cameron 				io_accel_request_size_offset) != 0xb8);
7896b9af4937SStephen M. Cameron 		h->ioaccel2_bft2_regs =
7897b9af4937SStephen M. Cameron 			remap_pci_mem(pci_resource_start(h->pdev,
7898b9af4937SStephen M. Cameron 					cfg_base_addr_index) +
7899b9af4937SStephen M. Cameron 					cfg_offset + bft2_offset,
7900b9af4937SStephen M. Cameron 					ARRAY_SIZE(bft2) *
7901b9af4937SStephen M. Cameron 					sizeof(*h->ioaccel2_bft2_regs));
7902b9af4937SStephen M. Cameron 		for (i = 0; i < ARRAY_SIZE(bft2); i++)
7903b9af4937SStephen M. Cameron 			writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
7904b9af4937SStephen M. Cameron 	}
7905b9af4937SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
7906c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h)) {
7907c706a795SRobert Elliott 		dev_err(&h->pdev->dev,
7908c706a795SRobert Elliott 			"performant mode problem - enabling ioaccel mode\n");
7909c706a795SRobert Elliott 		return -ENODEV;
7910c706a795SRobert Elliott 	}
7911c706a795SRobert Elliott 	return 0;
7912e1f7de0cSMatt Gates }
7913e1f7de0cSMatt Gates 
79141fb7c98aSRobert Elliott /* Free ioaccel1 mode command blocks and block fetch table */
79151fb7c98aSRobert Elliott static void hpsa_free_ioaccel1_cmd_and_bft(struct ctlr_info *h)
79161fb7c98aSRobert Elliott {
79171fb7c98aSRobert Elliott 	if (h->ioaccel_cmd_pool)
79181fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
79191fb7c98aSRobert Elliott 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
79201fb7c98aSRobert Elliott 			h->ioaccel_cmd_pool,
79211fb7c98aSRobert Elliott 			h->ioaccel_cmd_pool_dhandle);
79221fb7c98aSRobert Elliott 	kfree(h->ioaccel1_blockFetchTable);
79231fb7c98aSRobert Elliott }
79241fb7c98aSRobert Elliott 
7925d37ffbe4SRobert Elliott /* Allocate ioaccel1 mode command blocks and block fetch table */
7926d37ffbe4SRobert Elliott static int hpsa_alloc_ioaccel1_cmd_and_bft(struct ctlr_info *h)
7927e1f7de0cSMatt Gates {
7928283b4a9bSStephen M. Cameron 	h->ioaccel_maxsg =
7929283b4a9bSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
7930283b4a9bSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
7931283b4a9bSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
7932283b4a9bSStephen M. Cameron 
7933e1f7de0cSMatt Gates 	/* Command structures must be aligned on a 128-byte boundary
7934e1f7de0cSMatt Gates 	 * because the 7 lower bits of the address are used by the
7935e1f7de0cSMatt Gates 	 * hardware.
7936e1f7de0cSMatt Gates 	 */
7937e1f7de0cSMatt Gates 	BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
7938e1f7de0cSMatt Gates 			IOACCEL1_COMMANDLIST_ALIGNMENT);
7939e1f7de0cSMatt Gates 	h->ioaccel_cmd_pool =
7940e1f7de0cSMatt Gates 		pci_alloc_consistent(h->pdev,
7941e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
7942e1f7de0cSMatt Gates 			&(h->ioaccel_cmd_pool_dhandle));
7943e1f7de0cSMatt Gates 
7944e1f7de0cSMatt Gates 	h->ioaccel1_blockFetchTable =
7945283b4a9bSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
7946e1f7de0cSMatt Gates 				sizeof(u32)), GFP_KERNEL);
7947e1f7de0cSMatt Gates 
7948e1f7de0cSMatt Gates 	if ((h->ioaccel_cmd_pool == NULL) ||
7949e1f7de0cSMatt Gates 		(h->ioaccel1_blockFetchTable == NULL))
7950e1f7de0cSMatt Gates 		goto clean_up;
7951e1f7de0cSMatt Gates 
7952e1f7de0cSMatt Gates 	memset(h->ioaccel_cmd_pool, 0,
7953e1f7de0cSMatt Gates 		h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
7954e1f7de0cSMatt Gates 	return 0;
7955e1f7de0cSMatt Gates 
7956e1f7de0cSMatt Gates clean_up:
79571fb7c98aSRobert Elliott 	hpsa_free_ioaccel1_cmd_and_bft(h);
7958e1f7de0cSMatt Gates 	return 1;
79596c311b57SStephen M. Cameron }
79606c311b57SStephen M. Cameron 
79611fb7c98aSRobert Elliott /* Free ioaccel2 mode command blocks and block fetch table */
79621fb7c98aSRobert Elliott static void hpsa_free_ioaccel2_cmd_and_bft(struct ctlr_info *h)
79631fb7c98aSRobert Elliott {
7964d9a729f3SWebb Scales 	hpsa_free_ioaccel2_sg_chain_blocks(h);
7965d9a729f3SWebb Scales 
79661fb7c98aSRobert Elliott 	if (h->ioaccel2_cmd_pool)
79671fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
79681fb7c98aSRobert Elliott 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
79691fb7c98aSRobert Elliott 			h->ioaccel2_cmd_pool,
79701fb7c98aSRobert Elliott 			h->ioaccel2_cmd_pool_dhandle);
79711fb7c98aSRobert Elliott 	kfree(h->ioaccel2_blockFetchTable);
79721fb7c98aSRobert Elliott }
79731fb7c98aSRobert Elliott 
7974d37ffbe4SRobert Elliott /* Allocate ioaccel2 mode command blocks and block fetch table */
7975d37ffbe4SRobert Elliott static int hpsa_alloc_ioaccel2_cmd_and_bft(struct ctlr_info *h)
7976aca9012aSStephen M. Cameron {
7977d9a729f3SWebb Scales 	int rc;
7978d9a729f3SWebb Scales 
7979aca9012aSStephen M. Cameron 	/* Allocate ioaccel2 mode command blocks and block fetch table */
7980aca9012aSStephen M. Cameron 
7981aca9012aSStephen M. Cameron 	h->ioaccel_maxsg =
7982aca9012aSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
7983aca9012aSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
7984aca9012aSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
7985aca9012aSStephen M. Cameron 
7986aca9012aSStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
7987aca9012aSStephen M. Cameron 			IOACCEL2_COMMANDLIST_ALIGNMENT);
7988aca9012aSStephen M. Cameron 	h->ioaccel2_cmd_pool =
7989aca9012aSStephen M. Cameron 		pci_alloc_consistent(h->pdev,
7990aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
7991aca9012aSStephen M. Cameron 			&(h->ioaccel2_cmd_pool_dhandle));
7992aca9012aSStephen M. Cameron 
7993aca9012aSStephen M. Cameron 	h->ioaccel2_blockFetchTable =
7994aca9012aSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
7995aca9012aSStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
7996aca9012aSStephen M. Cameron 
7997aca9012aSStephen M. Cameron 	if ((h->ioaccel2_cmd_pool == NULL) ||
7998d9a729f3SWebb Scales 		(h->ioaccel2_blockFetchTable == NULL)) {
7999d9a729f3SWebb Scales 		rc = -ENOMEM;
8000d9a729f3SWebb Scales 		goto clean_up;
8001d9a729f3SWebb Scales 	}
8002d9a729f3SWebb Scales 
8003d9a729f3SWebb Scales 	rc = hpsa_allocate_ioaccel2_sg_chain_blocks(h);
8004d9a729f3SWebb Scales 	if (rc)
8005aca9012aSStephen M. Cameron 		goto clean_up;
8006aca9012aSStephen M. Cameron 
8007aca9012aSStephen M. Cameron 	memset(h->ioaccel2_cmd_pool, 0,
8008aca9012aSStephen M. Cameron 		h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
8009aca9012aSStephen M. Cameron 	return 0;
8010aca9012aSStephen M. Cameron 
8011aca9012aSStephen M. Cameron clean_up:
80121fb7c98aSRobert Elliott 	hpsa_free_ioaccel2_cmd_and_bft(h);
8013d9a729f3SWebb Scales 	return rc;
8014aca9012aSStephen M. Cameron }
8015aca9012aSStephen M. Cameron 
80166f039790SGreg Kroah-Hartman static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
80176c311b57SStephen M. Cameron {
80186c311b57SStephen M. Cameron 	u32 trans_support;
8019e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
8020e1f7de0cSMatt Gates 					CFGTBL_Trans_use_short_tags;
8021254f796bSMatt Gates 	int i;
80226c311b57SStephen M. Cameron 
802302ec19c8SStephen M. Cameron 	if (hpsa_simple_mode)
802402ec19c8SStephen M. Cameron 		return;
802502ec19c8SStephen M. Cameron 
802667c99a72Sscameron@beardog.cce.hp.com 	trans_support = readl(&(h->cfgtable->TransportSupport));
802767c99a72Sscameron@beardog.cce.hp.com 	if (!(trans_support & PERFORMANT_MODE))
802867c99a72Sscameron@beardog.cce.hp.com 		return;
802967c99a72Sscameron@beardog.cce.hp.com 
8030e1f7de0cSMatt Gates 	/* Check for I/O accelerator mode support */
8031e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
8032e1f7de0cSMatt Gates 		transMethod |= CFGTBL_Trans_io_accel1 |
8033e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix;
8034d37ffbe4SRobert Elliott 		if (hpsa_alloc_ioaccel1_cmd_and_bft(h))
8035e1f7de0cSMatt Gates 			goto clean_up;
8036aca9012aSStephen M. Cameron 	} else {
8037aca9012aSStephen M. Cameron 		if (trans_support & CFGTBL_Trans_io_accel2) {
8038aca9012aSStephen M. Cameron 				transMethod |= CFGTBL_Trans_io_accel2 |
8039aca9012aSStephen M. Cameron 				CFGTBL_Trans_enable_directed_msix;
8040d37ffbe4SRobert Elliott 		if (hpsa_alloc_ioaccel2_cmd_and_bft(h))
8041aca9012aSStephen M. Cameron 			goto clean_up;
8042aca9012aSStephen M. Cameron 		}
8043e1f7de0cSMatt Gates 	}
8044e1f7de0cSMatt Gates 
8045eee0f03aSHannes Reinecke 	h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
8046cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
80476c311b57SStephen M. Cameron 	/* Performant mode ring buffer and supporting data structures */
8048072b0518SStephen M. Cameron 	h->reply_queue_size = h->max_commands * sizeof(u64);
80496c311b57SStephen M. Cameron 
8050254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
8051072b0518SStephen M. Cameron 		h->reply_queue[i].head = pci_alloc_consistent(h->pdev,
8052072b0518SStephen M. Cameron 						h->reply_queue_size,
8053072b0518SStephen M. Cameron 						&(h->reply_queue[i].busaddr));
8054072b0518SStephen M. Cameron 		if (!h->reply_queue[i].head)
8055072b0518SStephen M. Cameron 			goto clean_up;
8056254f796bSMatt Gates 		h->reply_queue[i].size = h->max_commands;
8057254f796bSMatt Gates 		h->reply_queue[i].wraparound = 1;  /* spec: init to 1 */
8058254f796bSMatt Gates 		h->reply_queue[i].current_entry = 0;
8059254f796bSMatt Gates 	}
8060254f796bSMatt Gates 
80616c311b57SStephen M. Cameron 	/* Need a block fetch table for performant mode */
8062d66ae08bSStephen M. Cameron 	h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
80636c311b57SStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
8064072b0518SStephen M. Cameron 	if (!h->blockFetchTable)
80656c311b57SStephen M. Cameron 		goto clean_up;
80666c311b57SStephen M. Cameron 
8067e1f7de0cSMatt Gates 	hpsa_enter_performant_mode(h, trans_support);
8068303932fdSDon Brace 	return;
8069303932fdSDon Brace 
8070303932fdSDon Brace clean_up:
8071072b0518SStephen M. Cameron 	hpsa_free_reply_queues(h);
8072303932fdSDon Brace 	kfree(h->blockFetchTable);
8073303932fdSDon Brace }
8074303932fdSDon Brace 
807523100dd9SStephen M. Cameron static int is_accelerated_cmd(struct CommandList *c)
807676438d08SStephen M. Cameron {
807723100dd9SStephen M. Cameron 	return c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_IOACCEL2;
807823100dd9SStephen M. Cameron }
807923100dd9SStephen M. Cameron 
808023100dd9SStephen M. Cameron static void hpsa_drain_accel_commands(struct ctlr_info *h)
808123100dd9SStephen M. Cameron {
808223100dd9SStephen M. Cameron 	struct CommandList *c = NULL;
8083f2405db8SDon Brace 	int i, accel_cmds_out;
8084281a7fd0SWebb Scales 	int refcount;
808576438d08SStephen M. Cameron 
8086f2405db8SDon Brace 	do { /* wait for all outstanding ioaccel commands to drain out */
808723100dd9SStephen M. Cameron 		accel_cmds_out = 0;
8088f2405db8SDon Brace 		for (i = 0; i < h->nr_cmds; i++) {
8089f2405db8SDon Brace 			c = h->cmd_pool + i;
8090281a7fd0SWebb Scales 			refcount = atomic_inc_return(&c->refcount);
8091281a7fd0SWebb Scales 			if (refcount > 1) /* Command is allocated */
809223100dd9SStephen M. Cameron 				accel_cmds_out += is_accelerated_cmd(c);
8093281a7fd0SWebb Scales 			cmd_free(h, c);
8094f2405db8SDon Brace 		}
809523100dd9SStephen M. Cameron 		if (accel_cmds_out <= 0)
809676438d08SStephen M. Cameron 			break;
809776438d08SStephen M. Cameron 		msleep(100);
809876438d08SStephen M. Cameron 	} while (1);
809976438d08SStephen M. Cameron }
810076438d08SStephen M. Cameron 
8101edd16368SStephen M. Cameron /*
8102edd16368SStephen M. Cameron  *  This is it.  Register the PCI driver information for the cards we control
8103edd16368SStephen M. Cameron  *  the OS will call our registered routines when it finds one of our cards.
8104edd16368SStephen M. Cameron  */
8105edd16368SStephen M. Cameron static int __init hpsa_init(void)
8106edd16368SStephen M. Cameron {
810731468401SMike Miller 	return pci_register_driver(&hpsa_pci_driver);
8108edd16368SStephen M. Cameron }
8109edd16368SStephen M. Cameron 
8110edd16368SStephen M. Cameron static void __exit hpsa_cleanup(void)
8111edd16368SStephen M. Cameron {
8112edd16368SStephen M. Cameron 	pci_unregister_driver(&hpsa_pci_driver);
8113edd16368SStephen M. Cameron }
8114edd16368SStephen M. Cameron 
8115e1f7de0cSMatt Gates static void __attribute__((unused)) verify_offsets(void)
8116e1f7de0cSMatt Gates {
8117e1f7de0cSMatt Gates #define VERIFY_OFFSET(member, offset) \
8118dd0e19f3SScott Teel 	BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
8119dd0e19f3SScott Teel 
8120dd0e19f3SScott Teel 	VERIFY_OFFSET(structure_size, 0);
8121dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_size, 4);
8122dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_cnt, 8);
8123dd0e19f3SScott Teel 	VERIFY_OFFSET(phys_blk_shift, 16);
8124dd0e19f3SScott Teel 	VERIFY_OFFSET(parity_rotation_shift, 17);
8125dd0e19f3SScott Teel 	VERIFY_OFFSET(strip_size, 18);
8126dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_starting_blk, 20);
8127dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_blk_cnt, 28);
8128dd0e19f3SScott Teel 	VERIFY_OFFSET(data_disks_per_row, 36);
8129dd0e19f3SScott Teel 	VERIFY_OFFSET(metadata_disks_per_row, 38);
8130dd0e19f3SScott Teel 	VERIFY_OFFSET(row_cnt, 40);
8131dd0e19f3SScott Teel 	VERIFY_OFFSET(layout_map_count, 42);
8132dd0e19f3SScott Teel 	VERIFY_OFFSET(flags, 44);
8133dd0e19f3SScott Teel 	VERIFY_OFFSET(dekindex, 46);
8134dd0e19f3SScott Teel 	/* VERIFY_OFFSET(reserved, 48 */
8135dd0e19f3SScott Teel 	VERIFY_OFFSET(data, 64);
8136dd0e19f3SScott Teel 
8137dd0e19f3SScott Teel #undef VERIFY_OFFSET
8138dd0e19f3SScott Teel 
8139dd0e19f3SScott Teel #define VERIFY_OFFSET(member, offset) \
8140b66cc250SMike Miller 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
8141b66cc250SMike Miller 
8142b66cc250SMike Miller 	VERIFY_OFFSET(IU_type, 0);
8143b66cc250SMike Miller 	VERIFY_OFFSET(direction, 1);
8144b66cc250SMike Miller 	VERIFY_OFFSET(reply_queue, 2);
8145b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved1, 3);  */
8146b66cc250SMike Miller 	VERIFY_OFFSET(scsi_nexus, 4);
8147b66cc250SMike Miller 	VERIFY_OFFSET(Tag, 8);
8148b66cc250SMike Miller 	VERIFY_OFFSET(cdb, 16);
8149b66cc250SMike Miller 	VERIFY_OFFSET(cciss_lun, 32);
8150b66cc250SMike Miller 	VERIFY_OFFSET(data_len, 40);
8151b66cc250SMike Miller 	VERIFY_OFFSET(cmd_priority_task_attr, 44);
8152b66cc250SMike Miller 	VERIFY_OFFSET(sg_count, 45);
8153b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved3 */
8154b66cc250SMike Miller 	VERIFY_OFFSET(err_ptr, 48);
8155b66cc250SMike Miller 	VERIFY_OFFSET(err_len, 56);
8156b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved4  */
8157b66cc250SMike Miller 	VERIFY_OFFSET(sg, 64);
8158b66cc250SMike Miller 
8159b66cc250SMike Miller #undef VERIFY_OFFSET
8160b66cc250SMike Miller 
8161b66cc250SMike Miller #define VERIFY_OFFSET(member, offset) \
8162e1f7de0cSMatt Gates 	BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
8163e1f7de0cSMatt Gates 
8164e1f7de0cSMatt Gates 	VERIFY_OFFSET(dev_handle, 0x00);
8165e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved1, 0x02);
8166e1f7de0cSMatt Gates 	VERIFY_OFFSET(function, 0x03);
8167e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved2, 0x04);
8168e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info, 0x0C);
8169e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved3, 0x10);
8170e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info_len, 0x12);
8171e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved4, 0x13);
8172e1f7de0cSMatt Gates 	VERIFY_OFFSET(sgl_offset, 0x14);
8173e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved5, 0x15);
8174e1f7de0cSMatt Gates 	VERIFY_OFFSET(transfer_len, 0x1C);
8175e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved6, 0x20);
8176e1f7de0cSMatt Gates 	VERIFY_OFFSET(io_flags, 0x24);
8177e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved7, 0x26);
8178e1f7de0cSMatt Gates 	VERIFY_OFFSET(LUN, 0x34);
8179e1f7de0cSMatt Gates 	VERIFY_OFFSET(control, 0x3C);
8180e1f7de0cSMatt Gates 	VERIFY_OFFSET(CDB, 0x40);
8181e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved8, 0x50);
8182e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_context_flags, 0x60);
8183e1f7de0cSMatt Gates 	VERIFY_OFFSET(timeout_sec, 0x62);
8184e1f7de0cSMatt Gates 	VERIFY_OFFSET(ReplyQueue, 0x64);
8185e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved9, 0x65);
818650a0decfSStephen M. Cameron 	VERIFY_OFFSET(tag, 0x68);
8187e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_addr, 0x70);
8188e1f7de0cSMatt Gates 	VERIFY_OFFSET(CISS_LUN, 0x78);
8189e1f7de0cSMatt Gates 	VERIFY_OFFSET(SG, 0x78 + 8);
8190e1f7de0cSMatt Gates #undef VERIFY_OFFSET
8191e1f7de0cSMatt Gates }
8192e1f7de0cSMatt Gates 
8193edd16368SStephen M. Cameron module_init(hpsa_init);
8194edd16368SStephen M. Cameron module_exit(hpsa_cleanup);
8195