xref: /openbmc/linux/drivers/scsi/hpsa.c (revision 6636e7f455b33b957c5ee016daa6de46148026ab)
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>
46edd16368SStephen M. Cameron #include <linux/cciss_ioctl.h>
47edd16368SStephen M. Cameron #include <linux/string.h>
48edd16368SStephen M. Cameron #include <linux/bitmap.h>
4960063497SArun Sharma #include <linux/atomic.h>
50a0c12413SStephen M. Cameron #include <linux/jiffies.h>
5142a91641SDon Brace #include <linux/percpu-defs.h>
52094963daSStephen M. Cameron #include <linux/percpu.h>
532b08b3e9SDon Brace #include <asm/unaligned.h>
54283b4a9bSStephen M. Cameron #include <asm/div64.h>
55edd16368SStephen M. Cameron #include "hpsa_cmd.h"
56edd16368SStephen M. Cameron #include "hpsa.h"
57edd16368SStephen M. Cameron 
58edd16368SStephen M. Cameron /* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */
599a993302SStephen M. Cameron #define HPSA_DRIVER_VERSION "3.4.4-1"
60edd16368SStephen M. Cameron #define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
61f79cfec6SStephen M. Cameron #define HPSA "hpsa"
62edd16368SStephen M. Cameron 
63007e7aa9SRobert Elliott /* How long to wait for CISS doorbell communication */
64007e7aa9SRobert Elliott #define CLEAR_EVENT_WAIT_INTERVAL 20	/* ms for each msleep() call */
65007e7aa9SRobert Elliott #define MODE_CHANGE_WAIT_INTERVAL 10	/* ms for each msleep() call */
66007e7aa9SRobert Elliott #define MAX_CLEAR_EVENT_WAIT 30000	/* times 20 ms = 600 s */
67007e7aa9SRobert Elliott #define MAX_MODE_CHANGE_WAIT 2000	/* times 10 ms = 20 s */
68edd16368SStephen M. Cameron #define MAX_IOCTL_CONFIG_WAIT 1000
69edd16368SStephen M. Cameron 
70edd16368SStephen M. Cameron /*define how many times we will try a command because of bus resets */
71edd16368SStephen M. Cameron #define MAX_CMD_RETRIES 3
72edd16368SStephen M. Cameron 
73edd16368SStephen M. Cameron /* Embedded module documentation macros - see modules.h */
74edd16368SStephen M. Cameron MODULE_AUTHOR("Hewlett-Packard Company");
75edd16368SStephen M. Cameron MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
76edd16368SStephen M. Cameron 	HPSA_DRIVER_VERSION);
77edd16368SStephen M. Cameron MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
78edd16368SStephen M. Cameron MODULE_VERSION(HPSA_DRIVER_VERSION);
79edd16368SStephen M. Cameron MODULE_LICENSE("GPL");
80edd16368SStephen M. Cameron 
81edd16368SStephen M. Cameron static int hpsa_allow_any;
82edd16368SStephen M. Cameron module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
83edd16368SStephen M. Cameron MODULE_PARM_DESC(hpsa_allow_any,
84edd16368SStephen M. Cameron 		"Allow hpsa driver to access unknown HP Smart Array hardware");
8502ec19c8SStephen M. Cameron static int hpsa_simple_mode;
8602ec19c8SStephen M. Cameron module_param(hpsa_simple_mode, int, S_IRUGO|S_IWUSR);
8702ec19c8SStephen M. Cameron MODULE_PARM_DESC(hpsa_simple_mode,
8802ec19c8SStephen M. Cameron 	"Use 'simple mode' rather than 'performant mode'");
89edd16368SStephen M. Cameron 
90edd16368SStephen M. Cameron /* define the PCI info for the cards we can control */
91edd16368SStephen M. Cameron static const struct pci_device_id hpsa_pci_device_id[] = {
92edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3241},
93edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3243},
94edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3245},
95edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3247},
96edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3249},
97163dbcd8SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x324A},
98163dbcd8SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x324B},
99f8b01eb9SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3233},
1009143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3350},
1019143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3351},
1029143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3352},
1039143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3353},
1049143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3354},
1059143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3355},
1069143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3356},
107fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1921},
108fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1922},
109fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1923},
110fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1924},
111fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1926},
112fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1928},
11397b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1929},
11497b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BD},
11597b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BE},
11697b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BF},
11797b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C0},
11897b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C1},
11997b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C2},
12097b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C3},
12197b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C4},
12297b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C5},
1233b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C6},
12497b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C7},
12597b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C8},
12697b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C9},
1273b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CA},
1283b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CB},
1293b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CC},
1303b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CD},
1313b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CE},
1328e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0076},
1338e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0087},
1348e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x007D},
1358e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0088},
1368e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP, 0x333f, 0x103c, 0x333f},
137edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_ANY_ID,	PCI_ANY_ID, PCI_ANY_ID,
138edd16368SStephen M. Cameron 		PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
139edd16368SStephen M. Cameron 	{0,}
140edd16368SStephen M. Cameron };
141edd16368SStephen M. Cameron 
142edd16368SStephen M. Cameron MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
143edd16368SStephen M. Cameron 
144edd16368SStephen M. Cameron /*  board_id = Subsystem Device ID & Vendor ID
145edd16368SStephen M. Cameron  *  product = Marketing Name for the board
146edd16368SStephen M. Cameron  *  access = Address of the struct of function pointers
147edd16368SStephen M. Cameron  */
148edd16368SStephen M. Cameron static struct board_type products[] = {
149edd16368SStephen M. Cameron 	{0x3241103C, "Smart Array P212", &SA5_access},
150edd16368SStephen M. Cameron 	{0x3243103C, "Smart Array P410", &SA5_access},
151edd16368SStephen M. Cameron 	{0x3245103C, "Smart Array P410i", &SA5_access},
152edd16368SStephen M. Cameron 	{0x3247103C, "Smart Array P411", &SA5_access},
153edd16368SStephen M. Cameron 	{0x3249103C, "Smart Array P812", &SA5_access},
154163dbcd8SMike Miller 	{0x324A103C, "Smart Array P712m", &SA5_access},
155163dbcd8SMike Miller 	{0x324B103C, "Smart Array P711m", &SA5_access},
1567d2cce58SStephen M. Cameron 	{0x3233103C, "HP StorageWorks 1210m", &SA5_access}, /* alias of 333f */
157fe0c9610SMike Miller 	{0x3350103C, "Smart Array P222", &SA5_access},
158fe0c9610SMike Miller 	{0x3351103C, "Smart Array P420", &SA5_access},
159fe0c9610SMike Miller 	{0x3352103C, "Smart Array P421", &SA5_access},
160fe0c9610SMike Miller 	{0x3353103C, "Smart Array P822", &SA5_access},
161fe0c9610SMike Miller 	{0x3354103C, "Smart Array P420i", &SA5_access},
162fe0c9610SMike Miller 	{0x3355103C, "Smart Array P220i", &SA5_access},
163fe0c9610SMike Miller 	{0x3356103C, "Smart Array P721m", &SA5_access},
1641fd6c8e3SMike Miller 	{0x1921103C, "Smart Array P830i", &SA5_access},
1651fd6c8e3SMike Miller 	{0x1922103C, "Smart Array P430", &SA5_access},
1661fd6c8e3SMike Miller 	{0x1923103C, "Smart Array P431", &SA5_access},
1671fd6c8e3SMike Miller 	{0x1924103C, "Smart Array P830", &SA5_access},
1681fd6c8e3SMike Miller 	{0x1926103C, "Smart Array P731m", &SA5_access},
1691fd6c8e3SMike Miller 	{0x1928103C, "Smart Array P230i", &SA5_access},
1701fd6c8e3SMike Miller 	{0x1929103C, "Smart Array P530", &SA5_access},
17127fb8137SDon Brace 	{0x21BD103C, "Smart Array P244br", &SA5_access},
17227fb8137SDon Brace 	{0x21BE103C, "Smart Array P741m", &SA5_access},
17327fb8137SDon Brace 	{0x21BF103C, "Smart HBA H240ar", &SA5_access},
17427fb8137SDon Brace 	{0x21C0103C, "Smart Array P440ar", &SA5_access},
175c8ae0ab1SDon Brace 	{0x21C1103C, "Smart Array P840ar", &SA5_access},
17627fb8137SDon Brace 	{0x21C2103C, "Smart Array P440", &SA5_access},
17727fb8137SDon Brace 	{0x21C3103C, "Smart Array P441", &SA5_access},
17897b9f53dSMike Miller 	{0x21C4103C, "Smart Array", &SA5_access},
17927fb8137SDon Brace 	{0x21C5103C, "Smart Array P841", &SA5_access},
18027fb8137SDon Brace 	{0x21C6103C, "Smart HBA H244br", &SA5_access},
18127fb8137SDon Brace 	{0x21C7103C, "Smart HBA H240", &SA5_access},
18227fb8137SDon Brace 	{0x21C8103C, "Smart HBA H241", &SA5_access},
18397b9f53dSMike Miller 	{0x21C9103C, "Smart Array", &SA5_access},
18427fb8137SDon Brace 	{0x21CA103C, "Smart Array P246br", &SA5_access},
18527fb8137SDon Brace 	{0x21CB103C, "Smart Array P840", &SA5_access},
1863b7a45e5SJoe Handzik 	{0x21CC103C, "Smart Array", &SA5_access},
1873b7a45e5SJoe Handzik 	{0x21CD103C, "Smart Array", &SA5_access},
18827fb8137SDon Brace 	{0x21CE103C, "Smart HBA", &SA5_access},
1898e616a5eSStephen M. Cameron 	{0x00761590, "HP Storage P1224 Array Controller", &SA5_access},
1908e616a5eSStephen M. Cameron 	{0x00871590, "HP Storage P1224e Array Controller", &SA5_access},
1918e616a5eSStephen M. Cameron 	{0x007D1590, "HP Storage P1228 Array Controller", &SA5_access},
1928e616a5eSStephen M. Cameron 	{0x00881590, "HP Storage P1228e Array Controller", &SA5_access},
1938e616a5eSStephen M. Cameron 	{0x333f103c, "HP StorageWorks 1210m Array Controller", &SA5_access},
194edd16368SStephen M. Cameron 	{0xFFFF103C, "Unknown Smart Array", &SA5_access},
195edd16368SStephen M. Cameron };
196edd16368SStephen M. Cameron 
197edd16368SStephen M. Cameron static int number_of_controllers;
198edd16368SStephen M. Cameron 
19910f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id);
20010f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id);
20142a91641SDon Brace static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg);
202edd16368SStephen M. Cameron 
203edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
20442a91641SDon Brace static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd,
20542a91641SDon Brace 	void __user *arg);
206edd16368SStephen M. Cameron #endif
207edd16368SStephen M. Cameron 
208edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c);
209edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h);
210a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
211b7bb24ebSStephen M. Cameron 	void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
212edd16368SStephen M. Cameron 	int cmd_type);
2132c143342SRobert Elliott static void hpsa_free_cmd_pool(struct ctlr_info *h);
214b7bb24ebSStephen M. Cameron #define VPD_PAGE (1 << 8)
215edd16368SStephen M. Cameron 
216f281233dSJeff Garzik static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
217a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *);
218a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
219a08a8471SStephen M. Cameron 	unsigned long elapsed_time);
2207c0a0229SDon Brace static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth);
221edd16368SStephen M. Cameron 
222edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
22375167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *scsicmd);
224edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev);
225edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev);
226edd16368SStephen M. Cameron 
227edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
228edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
229edd16368SStephen M. Cameron 	struct CommandList *c);
230edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
231edd16368SStephen M. Cameron 	struct CommandList *c);
232303932fdSDon Brace /* performant mode helper functions */
233303932fdSDon Brace static void calc_bucket_map(int *bucket, int num_buckets,
2342b08b3e9SDon Brace 	int nsgs, int min_blocks, u32 *bucket_map);
2356f039790SGreg Kroah-Hartman static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
236254f796bSMatt Gates static inline u32 next_command(struct ctlr_info *h, u8 q);
2376f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
2386f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
2391df8552aSStephen M. Cameron 			       u64 *cfg_offset);
2406f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
2411df8552aSStephen M. Cameron 				    unsigned long *memory_bar);
2426f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
2436f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
2446f039790SGreg Kroah-Hartman 				     int wait_for_ready);
24575167d2cSStephen M. Cameron static inline void finish_cmd(struct CommandList *c);
246c706a795SRobert Elliott static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h);
247fe5389c8SStephen M. Cameron #define BOARD_NOT_READY 0
248fe5389c8SStephen M. Cameron #define BOARD_READY 1
24923100dd9SStephen M. Cameron static void hpsa_drain_accel_commands(struct ctlr_info *h);
25076438d08SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h);
251c349775eSScott Teel static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
252c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
25303383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk);
254080ef1ccSDon Brace static void hpsa_command_resubmit_worker(struct work_struct *work);
255edd16368SStephen M. Cameron 
256edd16368SStephen M. Cameron static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
257edd16368SStephen M. Cameron {
258edd16368SStephen M. Cameron 	unsigned long *priv = shost_priv(sdev->host);
259edd16368SStephen M. Cameron 	return (struct ctlr_info *) *priv;
260edd16368SStephen M. Cameron }
261edd16368SStephen M. Cameron 
262a23513e8SStephen M. Cameron static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
263a23513e8SStephen M. Cameron {
264a23513e8SStephen M. Cameron 	unsigned long *priv = shost_priv(sh);
265a23513e8SStephen M. Cameron 	return (struct ctlr_info *) *priv;
266a23513e8SStephen M. Cameron }
267a23513e8SStephen M. Cameron 
268edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
269edd16368SStephen M. Cameron 	struct CommandList *c)
270edd16368SStephen M. Cameron {
271edd16368SStephen M. Cameron 	if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
272edd16368SStephen M. Cameron 		return 0;
273edd16368SStephen M. Cameron 
274edd16368SStephen M. Cameron 	switch (c->err_info->SenseInfo[12]) {
275edd16368SStephen M. Cameron 	case STATE_CHANGED:
276f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: a state change "
277edd16368SStephen M. Cameron 			"detected, command retried\n", h->ctlr);
278edd16368SStephen M. Cameron 		break;
279edd16368SStephen M. Cameron 	case LUN_FAILED:
2807f73695aSStephen M. Cameron 		dev_warn(&h->pdev->dev,
2817f73695aSStephen M. Cameron 			HPSA "%d: LUN failure detected\n", h->ctlr);
282edd16368SStephen M. Cameron 		break;
283edd16368SStephen M. Cameron 	case REPORT_LUNS_CHANGED:
2847f73695aSStephen M. Cameron 		dev_warn(&h->pdev->dev,
2857f73695aSStephen M. Cameron 			HPSA "%d: report LUN data changed\n", h->ctlr);
286edd16368SStephen M. Cameron 	/*
2874f4eb9f1SScott Teel 	 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
2884f4eb9f1SScott Teel 	 * target (array) devices.
289edd16368SStephen M. Cameron 	 */
290edd16368SStephen M. Cameron 		break;
291edd16368SStephen M. Cameron 	case POWER_OR_RESET:
292f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: a power on "
293edd16368SStephen M. Cameron 			"or device reset detected\n", h->ctlr);
294edd16368SStephen M. Cameron 		break;
295edd16368SStephen M. Cameron 	case UNIT_ATTENTION_CLEARED:
296f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: unit attention "
297edd16368SStephen M. Cameron 		    "cleared by another initiator\n", h->ctlr);
298edd16368SStephen M. Cameron 		break;
299edd16368SStephen M. Cameron 	default:
300f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: unknown "
301edd16368SStephen M. Cameron 			"unit attention detected\n", h->ctlr);
302edd16368SStephen M. Cameron 		break;
303edd16368SStephen M. Cameron 	}
304edd16368SStephen M. Cameron 	return 1;
305edd16368SStephen M. Cameron }
306edd16368SStephen M. Cameron 
307852af20aSMatt Bondurant static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
308852af20aSMatt Bondurant {
309852af20aSMatt Bondurant 	if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
310852af20aSMatt Bondurant 		(c->err_info->ScsiStatus != SAM_STAT_BUSY &&
311852af20aSMatt Bondurant 		 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
312852af20aSMatt Bondurant 		return 0;
313852af20aSMatt Bondurant 	dev_warn(&h->pdev->dev, HPSA "device busy");
314852af20aSMatt Bondurant 	return 1;
315852af20aSMatt Bondurant }
316852af20aSMatt Bondurant 
317da0697bdSScott Teel static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
318da0697bdSScott Teel 					 struct device_attribute *attr,
319da0697bdSScott Teel 					 const char *buf, size_t count)
320da0697bdSScott Teel {
321da0697bdSScott Teel 	int status, len;
322da0697bdSScott Teel 	struct ctlr_info *h;
323da0697bdSScott Teel 	struct Scsi_Host *shost = class_to_shost(dev);
324da0697bdSScott Teel 	char tmpbuf[10];
325da0697bdSScott Teel 
326da0697bdSScott Teel 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
327da0697bdSScott Teel 		return -EACCES;
328da0697bdSScott Teel 	len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
329da0697bdSScott Teel 	strncpy(tmpbuf, buf, len);
330da0697bdSScott Teel 	tmpbuf[len] = '\0';
331da0697bdSScott Teel 	if (sscanf(tmpbuf, "%d", &status) != 1)
332da0697bdSScott Teel 		return -EINVAL;
333da0697bdSScott Teel 	h = shost_to_hba(shost);
334da0697bdSScott Teel 	h->acciopath_status = !!status;
335da0697bdSScott Teel 	dev_warn(&h->pdev->dev,
336da0697bdSScott Teel 		"hpsa: HP SSD Smart Path %s via sysfs update.\n",
337da0697bdSScott Teel 		h->acciopath_status ? "enabled" : "disabled");
338da0697bdSScott Teel 	return count;
339da0697bdSScott Teel }
340da0697bdSScott Teel 
3412ba8bfc8SStephen M. Cameron static ssize_t host_store_raid_offload_debug(struct device *dev,
3422ba8bfc8SStephen M. Cameron 					 struct device_attribute *attr,
3432ba8bfc8SStephen M. Cameron 					 const char *buf, size_t count)
3442ba8bfc8SStephen M. Cameron {
3452ba8bfc8SStephen M. Cameron 	int debug_level, len;
3462ba8bfc8SStephen M. Cameron 	struct ctlr_info *h;
3472ba8bfc8SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
3482ba8bfc8SStephen M. Cameron 	char tmpbuf[10];
3492ba8bfc8SStephen M. Cameron 
3502ba8bfc8SStephen M. Cameron 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
3512ba8bfc8SStephen M. Cameron 		return -EACCES;
3522ba8bfc8SStephen M. Cameron 	len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
3532ba8bfc8SStephen M. Cameron 	strncpy(tmpbuf, buf, len);
3542ba8bfc8SStephen M. Cameron 	tmpbuf[len] = '\0';
3552ba8bfc8SStephen M. Cameron 	if (sscanf(tmpbuf, "%d", &debug_level) != 1)
3562ba8bfc8SStephen M. Cameron 		return -EINVAL;
3572ba8bfc8SStephen M. Cameron 	if (debug_level < 0)
3582ba8bfc8SStephen M. Cameron 		debug_level = 0;
3592ba8bfc8SStephen M. Cameron 	h = shost_to_hba(shost);
3602ba8bfc8SStephen M. Cameron 	h->raid_offload_debug = debug_level;
3612ba8bfc8SStephen M. Cameron 	dev_warn(&h->pdev->dev, "hpsa: Set raid_offload_debug level = %d\n",
3622ba8bfc8SStephen M. Cameron 		h->raid_offload_debug);
3632ba8bfc8SStephen M. Cameron 	return count;
3642ba8bfc8SStephen M. Cameron }
3652ba8bfc8SStephen M. Cameron 
366edd16368SStephen M. Cameron static ssize_t host_store_rescan(struct device *dev,
367edd16368SStephen M. Cameron 				 struct device_attribute *attr,
368edd16368SStephen M. Cameron 				 const char *buf, size_t count)
369edd16368SStephen M. Cameron {
370edd16368SStephen M. Cameron 	struct ctlr_info *h;
371edd16368SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
372a23513e8SStephen M. Cameron 	h = shost_to_hba(shost);
37331468401SMike Miller 	hpsa_scan_start(h->scsi_host);
374edd16368SStephen M. Cameron 	return count;
375edd16368SStephen M. Cameron }
376edd16368SStephen M. Cameron 
377d28ce020SStephen M. Cameron static ssize_t host_show_firmware_revision(struct device *dev,
378d28ce020SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
379d28ce020SStephen M. Cameron {
380d28ce020SStephen M. Cameron 	struct ctlr_info *h;
381d28ce020SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
382d28ce020SStephen M. Cameron 	unsigned char *fwrev;
383d28ce020SStephen M. Cameron 
384d28ce020SStephen M. Cameron 	h = shost_to_hba(shost);
385d28ce020SStephen M. Cameron 	if (!h->hba_inquiry_data)
386d28ce020SStephen M. Cameron 		return 0;
387d28ce020SStephen M. Cameron 	fwrev = &h->hba_inquiry_data[32];
388d28ce020SStephen M. Cameron 	return snprintf(buf, 20, "%c%c%c%c\n",
389d28ce020SStephen M. Cameron 		fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
390d28ce020SStephen M. Cameron }
391d28ce020SStephen M. Cameron 
39294a13649SStephen M. Cameron static ssize_t host_show_commands_outstanding(struct device *dev,
39394a13649SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
39494a13649SStephen M. Cameron {
39594a13649SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
39694a13649SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(shost);
39794a13649SStephen M. Cameron 
3980cbf768eSStephen M. Cameron 	return snprintf(buf, 20, "%d\n",
3990cbf768eSStephen M. Cameron 			atomic_read(&h->commands_outstanding));
40094a13649SStephen M. Cameron }
40194a13649SStephen M. Cameron 
402745a7a25SStephen M. Cameron static ssize_t host_show_transport_mode(struct device *dev,
403745a7a25SStephen M. Cameron 	struct device_attribute *attr, char *buf)
404745a7a25SStephen M. Cameron {
405745a7a25SStephen M. Cameron 	struct ctlr_info *h;
406745a7a25SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
407745a7a25SStephen M. Cameron 
408745a7a25SStephen M. Cameron 	h = shost_to_hba(shost);
409745a7a25SStephen M. Cameron 	return snprintf(buf, 20, "%s\n",
410960a30e7SStephen M. Cameron 		h->transMethod & CFGTBL_Trans_Performant ?
411745a7a25SStephen M. Cameron 			"performant" : "simple");
412745a7a25SStephen M. Cameron }
413745a7a25SStephen M. Cameron 
414da0697bdSScott Teel static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
415da0697bdSScott Teel 	struct device_attribute *attr, char *buf)
416da0697bdSScott Teel {
417da0697bdSScott Teel 	struct ctlr_info *h;
418da0697bdSScott Teel 	struct Scsi_Host *shost = class_to_shost(dev);
419da0697bdSScott Teel 
420da0697bdSScott Teel 	h = shost_to_hba(shost);
421da0697bdSScott Teel 	return snprintf(buf, 30, "HP SSD Smart Path %s\n",
422da0697bdSScott Teel 		(h->acciopath_status == 1) ?  "enabled" : "disabled");
423da0697bdSScott Teel }
424da0697bdSScott Teel 
42546380786SStephen M. Cameron /* List of controllers which cannot be hard reset on kexec with reset_devices */
426941b1cdaSStephen M. Cameron static u32 unresettable_controller[] = {
427941b1cdaSStephen M. Cameron 	0x324a103C, /* Smart Array P712m */
428941b1cdaSStephen M. Cameron 	0x324b103C, /* SmartArray P711m */
429941b1cdaSStephen M. Cameron 	0x3223103C, /* Smart Array P800 */
430941b1cdaSStephen M. Cameron 	0x3234103C, /* Smart Array P400 */
431941b1cdaSStephen M. Cameron 	0x3235103C, /* Smart Array P400i */
432941b1cdaSStephen M. Cameron 	0x3211103C, /* Smart Array E200i */
433941b1cdaSStephen M. Cameron 	0x3212103C, /* Smart Array E200 */
434941b1cdaSStephen M. Cameron 	0x3213103C, /* Smart Array E200i */
435941b1cdaSStephen M. Cameron 	0x3214103C, /* Smart Array E200i */
436941b1cdaSStephen M. Cameron 	0x3215103C, /* Smart Array E200i */
437941b1cdaSStephen M. Cameron 	0x3237103C, /* Smart Array E500 */
438941b1cdaSStephen M. Cameron 	0x323D103C, /* Smart Array P700m */
4397af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
440941b1cdaSStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
441941b1cdaSStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
4425a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
4435a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
4445a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
4455a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
4465a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
4475a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
448941b1cdaSStephen M. Cameron };
449941b1cdaSStephen M. Cameron 
45046380786SStephen M. Cameron /* List of controllers which cannot even be soft reset */
45146380786SStephen M. Cameron static u32 soft_unresettable_controller[] = {
4527af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
4535a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
4545a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
4555a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
4565a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
4575a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
4585a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
45946380786SStephen M. Cameron 	/* Exclude 640x boards.  These are two pci devices in one slot
46046380786SStephen M. Cameron 	 * which share a battery backed cache module.  One controls the
46146380786SStephen M. Cameron 	 * cache, the other accesses the cache through the one that controls
46246380786SStephen M. Cameron 	 * it.  If we reset the one controlling the cache, the other will
46346380786SStephen M. Cameron 	 * likely not be happy.  Just forbid resetting this conjoined mess.
46446380786SStephen M. Cameron 	 * The 640x isn't really supported by hpsa anyway.
46546380786SStephen M. Cameron 	 */
46646380786SStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
46746380786SStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
46846380786SStephen M. Cameron };
46946380786SStephen M. Cameron 
47046380786SStephen M. Cameron static int ctlr_is_hard_resettable(u32 board_id)
471941b1cdaSStephen M. Cameron {
472941b1cdaSStephen M. Cameron 	int i;
473941b1cdaSStephen M. Cameron 
474941b1cdaSStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(unresettable_controller); i++)
47546380786SStephen M. Cameron 		if (unresettable_controller[i] == board_id)
476941b1cdaSStephen M. Cameron 			return 0;
477941b1cdaSStephen M. Cameron 	return 1;
478941b1cdaSStephen M. Cameron }
479941b1cdaSStephen M. Cameron 
48046380786SStephen M. Cameron static int ctlr_is_soft_resettable(u32 board_id)
48146380786SStephen M. Cameron {
48246380786SStephen M. Cameron 	int i;
48346380786SStephen M. Cameron 
48446380786SStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(soft_unresettable_controller); i++)
48546380786SStephen M. Cameron 		if (soft_unresettable_controller[i] == board_id)
48646380786SStephen M. Cameron 			return 0;
48746380786SStephen M. Cameron 	return 1;
48846380786SStephen M. Cameron }
48946380786SStephen M. Cameron 
49046380786SStephen M. Cameron static int ctlr_is_resettable(u32 board_id)
49146380786SStephen M. Cameron {
49246380786SStephen M. Cameron 	return ctlr_is_hard_resettable(board_id) ||
49346380786SStephen M. Cameron 		ctlr_is_soft_resettable(board_id);
49446380786SStephen M. Cameron }
49546380786SStephen M. Cameron 
496941b1cdaSStephen M. Cameron static ssize_t host_show_resettable(struct device *dev,
497941b1cdaSStephen M. Cameron 	struct device_attribute *attr, char *buf)
498941b1cdaSStephen M. Cameron {
499941b1cdaSStephen M. Cameron 	struct ctlr_info *h;
500941b1cdaSStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
501941b1cdaSStephen M. Cameron 
502941b1cdaSStephen M. Cameron 	h = shost_to_hba(shost);
50346380786SStephen M. Cameron 	return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
504941b1cdaSStephen M. Cameron }
505941b1cdaSStephen M. Cameron 
506edd16368SStephen M. Cameron static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
507edd16368SStephen M. Cameron {
508edd16368SStephen M. Cameron 	return (scsi3addr[3] & 0xC0) == 0x40;
509edd16368SStephen M. Cameron }
510edd16368SStephen M. Cameron 
511f2ef0ce7SRobert Elliott static const char * const raid_label[] = { "0", "4", "1(+0)", "5", "5+1", "6",
512f2ef0ce7SRobert Elliott 	"1(+0)ADM", "UNKNOWN"
513edd16368SStephen M. Cameron };
5146b80b18fSScott Teel #define HPSA_RAID_0	0
5156b80b18fSScott Teel #define HPSA_RAID_4	1
5166b80b18fSScott Teel #define HPSA_RAID_1	2	/* also used for RAID 10 */
5176b80b18fSScott Teel #define HPSA_RAID_5	3	/* also used for RAID 50 */
5186b80b18fSScott Teel #define HPSA_RAID_51	4
5196b80b18fSScott Teel #define HPSA_RAID_6	5	/* also used for RAID 60 */
5206b80b18fSScott Teel #define HPSA_RAID_ADM	6	/* also used for RAID 1+0 ADM */
521edd16368SStephen M. Cameron #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
522edd16368SStephen M. Cameron 
523edd16368SStephen M. Cameron static ssize_t raid_level_show(struct device *dev,
524edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
525edd16368SStephen M. Cameron {
526edd16368SStephen M. Cameron 	ssize_t l = 0;
52782a72c0aSStephen M. Cameron 	unsigned char rlevel;
528edd16368SStephen M. Cameron 	struct ctlr_info *h;
529edd16368SStephen M. Cameron 	struct scsi_device *sdev;
530edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
531edd16368SStephen M. Cameron 	unsigned long flags;
532edd16368SStephen M. Cameron 
533edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
534edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
535edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
536edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
537edd16368SStephen M. Cameron 	if (!hdev) {
538edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
539edd16368SStephen M. Cameron 		return -ENODEV;
540edd16368SStephen M. Cameron 	}
541edd16368SStephen M. Cameron 
542edd16368SStephen M. Cameron 	/* Is this even a logical drive? */
543edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
544edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
545edd16368SStephen M. Cameron 		l = snprintf(buf, PAGE_SIZE, "N/A\n");
546edd16368SStephen M. Cameron 		return l;
547edd16368SStephen M. Cameron 	}
548edd16368SStephen M. Cameron 
549edd16368SStephen M. Cameron 	rlevel = hdev->raid_level;
550edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
55182a72c0aSStephen M. Cameron 	if (rlevel > RAID_UNKNOWN)
552edd16368SStephen M. Cameron 		rlevel = RAID_UNKNOWN;
553edd16368SStephen M. Cameron 	l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
554edd16368SStephen M. Cameron 	return l;
555edd16368SStephen M. Cameron }
556edd16368SStephen M. Cameron 
557edd16368SStephen M. Cameron static ssize_t lunid_show(struct device *dev,
558edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
559edd16368SStephen M. Cameron {
560edd16368SStephen M. Cameron 	struct ctlr_info *h;
561edd16368SStephen M. Cameron 	struct scsi_device *sdev;
562edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
563edd16368SStephen M. Cameron 	unsigned long flags;
564edd16368SStephen M. Cameron 	unsigned char lunid[8];
565edd16368SStephen M. Cameron 
566edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
567edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
568edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
569edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
570edd16368SStephen M. Cameron 	if (!hdev) {
571edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
572edd16368SStephen M. Cameron 		return -ENODEV;
573edd16368SStephen M. Cameron 	}
574edd16368SStephen M. Cameron 	memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
575edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
576edd16368SStephen M. Cameron 	return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
577edd16368SStephen M. Cameron 		lunid[0], lunid[1], lunid[2], lunid[3],
578edd16368SStephen M. Cameron 		lunid[4], lunid[5], lunid[6], lunid[7]);
579edd16368SStephen M. Cameron }
580edd16368SStephen M. Cameron 
581edd16368SStephen M. Cameron static ssize_t unique_id_show(struct device *dev,
582edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
583edd16368SStephen M. Cameron {
584edd16368SStephen M. Cameron 	struct ctlr_info *h;
585edd16368SStephen M. Cameron 	struct scsi_device *sdev;
586edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
587edd16368SStephen M. Cameron 	unsigned long flags;
588edd16368SStephen M. Cameron 	unsigned char sn[16];
589edd16368SStephen M. Cameron 
590edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
591edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
592edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
593edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
594edd16368SStephen M. Cameron 	if (!hdev) {
595edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
596edd16368SStephen M. Cameron 		return -ENODEV;
597edd16368SStephen M. Cameron 	}
598edd16368SStephen M. Cameron 	memcpy(sn, hdev->device_id, sizeof(sn));
599edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
600edd16368SStephen M. Cameron 	return snprintf(buf, 16 * 2 + 2,
601edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X"
602edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X\n",
603edd16368SStephen M. Cameron 			sn[0], sn[1], sn[2], sn[3],
604edd16368SStephen M. Cameron 			sn[4], sn[5], sn[6], sn[7],
605edd16368SStephen M. Cameron 			sn[8], sn[9], sn[10], sn[11],
606edd16368SStephen M. Cameron 			sn[12], sn[13], sn[14], sn[15]);
607edd16368SStephen M. Cameron }
608edd16368SStephen M. Cameron 
609c1988684SScott Teel static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
610c1988684SScott Teel 	     struct device_attribute *attr, char *buf)
611c1988684SScott Teel {
612c1988684SScott Teel 	struct ctlr_info *h;
613c1988684SScott Teel 	struct scsi_device *sdev;
614c1988684SScott Teel 	struct hpsa_scsi_dev_t *hdev;
615c1988684SScott Teel 	unsigned long flags;
616c1988684SScott Teel 	int offload_enabled;
617c1988684SScott Teel 
618c1988684SScott Teel 	sdev = to_scsi_device(dev);
619c1988684SScott Teel 	h = sdev_to_hba(sdev);
620c1988684SScott Teel 	spin_lock_irqsave(&h->lock, flags);
621c1988684SScott Teel 	hdev = sdev->hostdata;
622c1988684SScott Teel 	if (!hdev) {
623c1988684SScott Teel 		spin_unlock_irqrestore(&h->lock, flags);
624c1988684SScott Teel 		return -ENODEV;
625c1988684SScott Teel 	}
626c1988684SScott Teel 	offload_enabled = hdev->offload_enabled;
627c1988684SScott Teel 	spin_unlock_irqrestore(&h->lock, flags);
628c1988684SScott Teel 	return snprintf(buf, 20, "%d\n", offload_enabled);
629c1988684SScott Teel }
630c1988684SScott Teel 
6313f5eac3aSStephen M. Cameron static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
6323f5eac3aSStephen M. Cameron static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
6333f5eac3aSStephen M. Cameron static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
6343f5eac3aSStephen M. Cameron static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
635c1988684SScott Teel static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
636c1988684SScott Teel 			host_show_hp_ssd_smart_path_enabled, NULL);
637da0697bdSScott Teel static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
638da0697bdSScott Teel 		host_show_hp_ssd_smart_path_status,
639da0697bdSScott Teel 		host_store_hp_ssd_smart_path_status);
6402ba8bfc8SStephen M. Cameron static DEVICE_ATTR(raid_offload_debug, S_IWUSR, NULL,
6412ba8bfc8SStephen M. Cameron 			host_store_raid_offload_debug);
6423f5eac3aSStephen M. Cameron static DEVICE_ATTR(firmware_revision, S_IRUGO,
6433f5eac3aSStephen M. Cameron 	host_show_firmware_revision, NULL);
6443f5eac3aSStephen M. Cameron static DEVICE_ATTR(commands_outstanding, S_IRUGO,
6453f5eac3aSStephen M. Cameron 	host_show_commands_outstanding, NULL);
6463f5eac3aSStephen M. Cameron static DEVICE_ATTR(transport_mode, S_IRUGO,
6473f5eac3aSStephen M. Cameron 	host_show_transport_mode, NULL);
648941b1cdaSStephen M. Cameron static DEVICE_ATTR(resettable, S_IRUGO,
649941b1cdaSStephen M. Cameron 	host_show_resettable, NULL);
6503f5eac3aSStephen M. Cameron 
6513f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_sdev_attrs[] = {
6523f5eac3aSStephen M. Cameron 	&dev_attr_raid_level,
6533f5eac3aSStephen M. Cameron 	&dev_attr_lunid,
6543f5eac3aSStephen M. Cameron 	&dev_attr_unique_id,
655c1988684SScott Teel 	&dev_attr_hp_ssd_smart_path_enabled,
6563f5eac3aSStephen M. Cameron 	NULL,
6573f5eac3aSStephen M. Cameron };
6583f5eac3aSStephen M. Cameron 
6593f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_shost_attrs[] = {
6603f5eac3aSStephen M. Cameron 	&dev_attr_rescan,
6613f5eac3aSStephen M. Cameron 	&dev_attr_firmware_revision,
6623f5eac3aSStephen M. Cameron 	&dev_attr_commands_outstanding,
6633f5eac3aSStephen M. Cameron 	&dev_attr_transport_mode,
664941b1cdaSStephen M. Cameron 	&dev_attr_resettable,
665da0697bdSScott Teel 	&dev_attr_hp_ssd_smart_path_status,
6662ba8bfc8SStephen M. Cameron 	&dev_attr_raid_offload_debug,
6673f5eac3aSStephen M. Cameron 	NULL,
6683f5eac3aSStephen M. Cameron };
6693f5eac3aSStephen M. Cameron 
6703f5eac3aSStephen M. Cameron static struct scsi_host_template hpsa_driver_template = {
6713f5eac3aSStephen M. Cameron 	.module			= THIS_MODULE,
672f79cfec6SStephen M. Cameron 	.name			= HPSA,
673f79cfec6SStephen M. Cameron 	.proc_name		= HPSA,
6743f5eac3aSStephen M. Cameron 	.queuecommand		= hpsa_scsi_queue_command,
6753f5eac3aSStephen M. Cameron 	.scan_start		= hpsa_scan_start,
6763f5eac3aSStephen M. Cameron 	.scan_finished		= hpsa_scan_finished,
6777c0a0229SDon Brace 	.change_queue_depth	= hpsa_change_queue_depth,
6783f5eac3aSStephen M. Cameron 	.this_id		= -1,
6793f5eac3aSStephen M. Cameron 	.use_clustering		= ENABLE_CLUSTERING,
68075167d2cSStephen M. Cameron 	.eh_abort_handler	= hpsa_eh_abort_handler,
6813f5eac3aSStephen M. Cameron 	.eh_device_reset_handler = hpsa_eh_device_reset_handler,
6823f5eac3aSStephen M. Cameron 	.ioctl			= hpsa_ioctl,
6833f5eac3aSStephen M. Cameron 	.slave_alloc		= hpsa_slave_alloc,
6843f5eac3aSStephen M. Cameron 	.slave_destroy		= hpsa_slave_destroy,
6853f5eac3aSStephen M. Cameron #ifdef CONFIG_COMPAT
6863f5eac3aSStephen M. Cameron 	.compat_ioctl		= hpsa_compat_ioctl,
6873f5eac3aSStephen M. Cameron #endif
6883f5eac3aSStephen M. Cameron 	.sdev_attrs = hpsa_sdev_attrs,
6893f5eac3aSStephen M. Cameron 	.shost_attrs = hpsa_shost_attrs,
690c0d6a4d1SStephen M. Cameron 	.max_sectors = 8192,
69154b2b50cSMartin K. Petersen 	.no_write_same = 1,
6923f5eac3aSStephen M. Cameron };
6933f5eac3aSStephen M. Cameron 
694254f796bSMatt Gates static inline u32 next_command(struct ctlr_info *h, u8 q)
6953f5eac3aSStephen M. Cameron {
6963f5eac3aSStephen M. Cameron 	u32 a;
697072b0518SStephen M. Cameron 	struct reply_queue_buffer *rq = &h->reply_queue[q];
6983f5eac3aSStephen M. Cameron 
699e1f7de0cSMatt Gates 	if (h->transMethod & CFGTBL_Trans_io_accel1)
700e1f7de0cSMatt Gates 		return h->access.command_completed(h, q);
701e1f7de0cSMatt Gates 
7023f5eac3aSStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
703254f796bSMatt Gates 		return h->access.command_completed(h, q);
7043f5eac3aSStephen M. Cameron 
705254f796bSMatt Gates 	if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
706254f796bSMatt Gates 		a = rq->head[rq->current_entry];
707254f796bSMatt Gates 		rq->current_entry++;
7080cbf768eSStephen M. Cameron 		atomic_dec(&h->commands_outstanding);
7093f5eac3aSStephen M. Cameron 	} else {
7103f5eac3aSStephen M. Cameron 		a = FIFO_EMPTY;
7113f5eac3aSStephen M. Cameron 	}
7123f5eac3aSStephen M. Cameron 	/* Check for wraparound */
713254f796bSMatt Gates 	if (rq->current_entry == h->max_commands) {
714254f796bSMatt Gates 		rq->current_entry = 0;
715254f796bSMatt Gates 		rq->wraparound ^= 1;
7163f5eac3aSStephen M. Cameron 	}
7173f5eac3aSStephen M. Cameron 	return a;
7183f5eac3aSStephen M. Cameron }
7193f5eac3aSStephen M. Cameron 
720c349775eSScott Teel /*
721c349775eSScott Teel  * There are some special bits in the bus address of the
722c349775eSScott Teel  * command that we have to set for the controller to know
723c349775eSScott Teel  * how to process the command:
724c349775eSScott Teel  *
725c349775eSScott Teel  * Normal performant mode:
726c349775eSScott Teel  * bit 0: 1 means performant mode, 0 means simple mode.
727c349775eSScott Teel  * bits 1-3 = block fetch table entry
728c349775eSScott Teel  * bits 4-6 = command type (== 0)
729c349775eSScott Teel  *
730c349775eSScott Teel  * ioaccel1 mode:
731c349775eSScott Teel  * bit 0 = "performant mode" bit.
732c349775eSScott Teel  * bits 1-3 = block fetch table entry
733c349775eSScott Teel  * bits 4-6 = command type (== 110)
734c349775eSScott Teel  * (command type is needed because ioaccel1 mode
735c349775eSScott Teel  * commands are submitted through the same register as normal
736c349775eSScott Teel  * mode commands, so this is how the controller knows whether
737c349775eSScott Teel  * the command is normal mode or ioaccel1 mode.)
738c349775eSScott Teel  *
739c349775eSScott Teel  * ioaccel2 mode:
740c349775eSScott Teel  * bit 0 = "performant mode" bit.
741c349775eSScott Teel  * bits 1-4 = block fetch table entry (note extra bit)
742c349775eSScott Teel  * bits 4-6 = not needed, because ioaccel2 mode has
743c349775eSScott Teel  * a separate special register for submitting commands.
744c349775eSScott Teel  */
745c349775eSScott Teel 
7463f5eac3aSStephen M. Cameron /* set_performant_mode: Modify the tag for cciss performant
7473f5eac3aSStephen M. Cameron  * set bit 0 for pull model, bits 3-1 for block fetch
7483f5eac3aSStephen M. Cameron  * register number
7493f5eac3aSStephen M. Cameron  */
7503f5eac3aSStephen M. Cameron static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
7513f5eac3aSStephen M. Cameron {
752254f796bSMatt Gates 	if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
7533f5eac3aSStephen M. Cameron 		c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
754eee0f03aSHannes Reinecke 		if (likely(h->msix_vector > 0))
755254f796bSMatt Gates 			c->Header.ReplyQueue =
756804a5cb5SJohn Kacur 				raw_smp_processor_id() % h->nreply_queues;
757254f796bSMatt Gates 	}
7583f5eac3aSStephen M. Cameron }
7593f5eac3aSStephen M. Cameron 
760c349775eSScott Teel static void set_ioaccel1_performant_mode(struct ctlr_info *h,
761c349775eSScott Teel 						struct CommandList *c)
762c349775eSScott Teel {
763c349775eSScott Teel 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
764c349775eSScott Teel 
765c349775eSScott Teel 	/* Tell the controller to post the reply to the queue for this
766c349775eSScott Teel 	 * processor.  This seems to give the best I/O throughput.
767c349775eSScott Teel 	 */
768c349775eSScott Teel 	cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
769c349775eSScott Teel 	/* Set the bits in the address sent down to include:
770c349775eSScott Teel 	 *  - performant mode bit (bit 0)
771c349775eSScott Teel 	 *  - pull count (bits 1-3)
772c349775eSScott Teel 	 *  - command type (bits 4-6)
773c349775eSScott Teel 	 */
774c349775eSScott Teel 	c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
775c349775eSScott Teel 					IOACCEL1_BUSADDR_CMDTYPE;
776c349775eSScott Teel }
777c349775eSScott Teel 
778c349775eSScott Teel static void set_ioaccel2_performant_mode(struct ctlr_info *h,
779c349775eSScott Teel 						struct CommandList *c)
780c349775eSScott Teel {
781c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
782c349775eSScott Teel 
783c349775eSScott Teel 	/* Tell the controller to post the reply to the queue for this
784c349775eSScott Teel 	 * processor.  This seems to give the best I/O throughput.
785c349775eSScott Teel 	 */
786c349775eSScott Teel 	cp->reply_queue = smp_processor_id() % h->nreply_queues;
787c349775eSScott Teel 	/* Set the bits in the address sent down to include:
788c349775eSScott Teel 	 *  - performant mode bit not used in ioaccel mode 2
789c349775eSScott Teel 	 *  - pull count (bits 0-3)
790c349775eSScott Teel 	 *  - command type isn't needed for ioaccel2
791c349775eSScott Teel 	 */
792c349775eSScott Teel 	c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
793c349775eSScott Teel }
794c349775eSScott Teel 
795e85c5974SStephen M. Cameron static int is_firmware_flash_cmd(u8 *cdb)
796e85c5974SStephen M. Cameron {
797e85c5974SStephen M. Cameron 	return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
798e85c5974SStephen M. Cameron }
799e85c5974SStephen M. Cameron 
800e85c5974SStephen M. Cameron /*
801e85c5974SStephen M. Cameron  * During firmware flash, the heartbeat register may not update as frequently
802e85c5974SStephen M. Cameron  * as it should.  So we dial down lockup detection during firmware flash. and
803e85c5974SStephen M. Cameron  * dial it back up when firmware flash completes.
804e85c5974SStephen M. Cameron  */
805e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
806e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
807e85c5974SStephen M. Cameron static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
808e85c5974SStephen M. Cameron 		struct CommandList *c)
809e85c5974SStephen M. Cameron {
810e85c5974SStephen M. Cameron 	if (!is_firmware_flash_cmd(c->Request.CDB))
811e85c5974SStephen M. Cameron 		return;
812e85c5974SStephen M. Cameron 	atomic_inc(&h->firmware_flash_in_progress);
813e85c5974SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
814e85c5974SStephen M. Cameron }
815e85c5974SStephen M. Cameron 
816e85c5974SStephen M. Cameron static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
817e85c5974SStephen M. Cameron 		struct CommandList *c)
818e85c5974SStephen M. Cameron {
819e85c5974SStephen M. Cameron 	if (is_firmware_flash_cmd(c->Request.CDB) &&
820e85c5974SStephen M. Cameron 		atomic_dec_and_test(&h->firmware_flash_in_progress))
821e85c5974SStephen M. Cameron 		h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
822e85c5974SStephen M. Cameron }
823e85c5974SStephen M. Cameron 
8243f5eac3aSStephen M. Cameron static void enqueue_cmd_and_start_io(struct ctlr_info *h,
8253f5eac3aSStephen M. Cameron 	struct CommandList *c)
8263f5eac3aSStephen M. Cameron {
827c05e8866SStephen Cameron 	dial_down_lockup_detection_during_fw_flash(h, c);
828c05e8866SStephen Cameron 	atomic_inc(&h->commands_outstanding);
829c349775eSScott Teel 	switch (c->cmd_type) {
830c349775eSScott Teel 	case CMD_IOACCEL1:
831c349775eSScott Teel 		set_ioaccel1_performant_mode(h, c);
832c05e8866SStephen Cameron 		writel(c->busaddr, h->vaddr + SA5_REQUEST_PORT_OFFSET);
833c349775eSScott Teel 		break;
834c349775eSScott Teel 	case CMD_IOACCEL2:
835c349775eSScott Teel 		set_ioaccel2_performant_mode(h, c);
836c05e8866SStephen Cameron 		writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
837c349775eSScott Teel 		break;
838c349775eSScott Teel 	default:
8393f5eac3aSStephen M. Cameron 		set_performant_mode(h, c);
840f2405db8SDon Brace 		h->access.submit_command(h, c);
8413f5eac3aSStephen M. Cameron 	}
842c05e8866SStephen Cameron }
8433f5eac3aSStephen M. Cameron 
8443f5eac3aSStephen M. Cameron static inline int is_hba_lunid(unsigned char scsi3addr[])
8453f5eac3aSStephen M. Cameron {
8463f5eac3aSStephen M. Cameron 	return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
8473f5eac3aSStephen M. Cameron }
8483f5eac3aSStephen M. Cameron 
8493f5eac3aSStephen M. Cameron static inline int is_scsi_rev_5(struct ctlr_info *h)
8503f5eac3aSStephen M. Cameron {
8513f5eac3aSStephen M. Cameron 	if (!h->hba_inquiry_data)
8523f5eac3aSStephen M. Cameron 		return 0;
8533f5eac3aSStephen M. Cameron 	if ((h->hba_inquiry_data[2] & 0x07) == 5)
8543f5eac3aSStephen M. Cameron 		return 1;
8553f5eac3aSStephen M. Cameron 	return 0;
8563f5eac3aSStephen M. Cameron }
8573f5eac3aSStephen M. Cameron 
858edd16368SStephen M. Cameron static int hpsa_find_target_lun(struct ctlr_info *h,
859edd16368SStephen M. Cameron 	unsigned char scsi3addr[], int bus, int *target, int *lun)
860edd16368SStephen M. Cameron {
861edd16368SStephen M. Cameron 	/* finds an unused bus, target, lun for a new physical device
862edd16368SStephen M. Cameron 	 * assumes h->devlock is held
863edd16368SStephen M. Cameron 	 */
864edd16368SStephen M. Cameron 	int i, found = 0;
865cfe5badcSScott Teel 	DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
866edd16368SStephen M. Cameron 
867263d9401SAkinobu Mita 	bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
868edd16368SStephen M. Cameron 
869edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
870edd16368SStephen M. Cameron 		if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
871263d9401SAkinobu Mita 			__set_bit(h->dev[i]->target, lun_taken);
872edd16368SStephen M. Cameron 	}
873edd16368SStephen M. Cameron 
874263d9401SAkinobu Mita 	i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
875263d9401SAkinobu Mita 	if (i < HPSA_MAX_DEVICES) {
876edd16368SStephen M. Cameron 		/* *bus = 1; */
877edd16368SStephen M. Cameron 		*target = i;
878edd16368SStephen M. Cameron 		*lun = 0;
879edd16368SStephen M. Cameron 		found = 1;
880edd16368SStephen M. Cameron 	}
881edd16368SStephen M. Cameron 	return !found;
882edd16368SStephen M. Cameron }
883edd16368SStephen M. Cameron 
884edd16368SStephen M. Cameron /* Add an entry into h->dev[] array. */
885edd16368SStephen M. Cameron static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
886edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *device,
887edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *added[], int *nadded)
888edd16368SStephen M. Cameron {
889edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
890edd16368SStephen M. Cameron 	int n = h->ndevices;
891edd16368SStephen M. Cameron 	int i;
892edd16368SStephen M. Cameron 	unsigned char addr1[8], addr2[8];
893edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
894edd16368SStephen M. Cameron 
895cfe5badcSScott Teel 	if (n >= HPSA_MAX_DEVICES) {
896edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "too many devices, some will be "
897edd16368SStephen M. Cameron 			"inaccessible.\n");
898edd16368SStephen M. Cameron 		return -1;
899edd16368SStephen M. Cameron 	}
900edd16368SStephen M. Cameron 
901edd16368SStephen M. Cameron 	/* physical devices do not have lun or target assigned until now. */
902edd16368SStephen M. Cameron 	if (device->lun != -1)
903edd16368SStephen M. Cameron 		/* Logical device, lun is already assigned. */
904edd16368SStephen M. Cameron 		goto lun_assigned;
905edd16368SStephen M. Cameron 
906edd16368SStephen M. Cameron 	/* If this device a non-zero lun of a multi-lun device
907edd16368SStephen M. Cameron 	 * byte 4 of the 8-byte LUN addr will contain the logical
9082b08b3e9SDon Brace 	 * unit no, zero otherwise.
909edd16368SStephen M. Cameron 	 */
910edd16368SStephen M. Cameron 	if (device->scsi3addr[4] == 0) {
911edd16368SStephen M. Cameron 		/* This is not a non-zero lun of a multi-lun device */
912edd16368SStephen M. Cameron 		if (hpsa_find_target_lun(h, device->scsi3addr,
913edd16368SStephen M. Cameron 			device->bus, &device->target, &device->lun) != 0)
914edd16368SStephen M. Cameron 			return -1;
915edd16368SStephen M. Cameron 		goto lun_assigned;
916edd16368SStephen M. Cameron 	}
917edd16368SStephen M. Cameron 
918edd16368SStephen M. Cameron 	/* This is a non-zero lun of a multi-lun device.
919edd16368SStephen M. Cameron 	 * Search through our list and find the device which
920edd16368SStephen M. Cameron 	 * has the same 8 byte LUN address, excepting byte 4.
921edd16368SStephen M. Cameron 	 * Assign the same bus and target for this new LUN.
922edd16368SStephen M. Cameron 	 * Use the logical unit number from the firmware.
923edd16368SStephen M. Cameron 	 */
924edd16368SStephen M. Cameron 	memcpy(addr1, device->scsi3addr, 8);
925edd16368SStephen M. Cameron 	addr1[4] = 0;
926edd16368SStephen M. Cameron 	for (i = 0; i < n; i++) {
927edd16368SStephen M. Cameron 		sd = h->dev[i];
928edd16368SStephen M. Cameron 		memcpy(addr2, sd->scsi3addr, 8);
929edd16368SStephen M. Cameron 		addr2[4] = 0;
930edd16368SStephen M. Cameron 		/* differ only in byte 4? */
931edd16368SStephen M. Cameron 		if (memcmp(addr1, addr2, 8) == 0) {
932edd16368SStephen M. Cameron 			device->bus = sd->bus;
933edd16368SStephen M. Cameron 			device->target = sd->target;
934edd16368SStephen M. Cameron 			device->lun = device->scsi3addr[4];
935edd16368SStephen M. Cameron 			break;
936edd16368SStephen M. Cameron 		}
937edd16368SStephen M. Cameron 	}
938edd16368SStephen M. Cameron 	if (device->lun == -1) {
939edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
940edd16368SStephen M. Cameron 			" suspect firmware bug or unsupported hardware "
941edd16368SStephen M. Cameron 			"configuration.\n");
942edd16368SStephen M. Cameron 			return -1;
943edd16368SStephen M. Cameron 	}
944edd16368SStephen M. Cameron 
945edd16368SStephen M. Cameron lun_assigned:
946edd16368SStephen M. Cameron 
947edd16368SStephen M. Cameron 	h->dev[n] = device;
948edd16368SStephen M. Cameron 	h->ndevices++;
949edd16368SStephen M. Cameron 	added[*nadded] = device;
950edd16368SStephen M. Cameron 	(*nadded)++;
951edd16368SStephen M. Cameron 
952edd16368SStephen M. Cameron 	/* initially, (before registering with scsi layer) we don't
953edd16368SStephen M. Cameron 	 * know our hostno and we don't want to print anything first
954edd16368SStephen M. Cameron 	 * time anyway (the scsi layer's inquiries will show that info)
955edd16368SStephen M. Cameron 	 */
956edd16368SStephen M. Cameron 	/* if (hostno != -1) */
957edd16368SStephen M. Cameron 		dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
958edd16368SStephen M. Cameron 			scsi_device_type(device->devtype), hostno,
959edd16368SStephen M. Cameron 			device->bus, device->target, device->lun);
960edd16368SStephen M. Cameron 	return 0;
961edd16368SStephen M. Cameron }
962edd16368SStephen M. Cameron 
963bd9244f7SScott Teel /* Update an entry in h->dev[] array. */
964bd9244f7SScott Teel static void hpsa_scsi_update_entry(struct ctlr_info *h, int hostno,
965bd9244f7SScott Teel 	int entry, struct hpsa_scsi_dev_t *new_entry)
966bd9244f7SScott Teel {
967bd9244f7SScott Teel 	/* assumes h->devlock is held */
968bd9244f7SScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
969bd9244f7SScott Teel 
970bd9244f7SScott Teel 	/* Raid level changed. */
971bd9244f7SScott Teel 	h->dev[entry]->raid_level = new_entry->raid_level;
972250fb125SStephen M. Cameron 
97303383736SDon Brace 	/* Raid offload parameters changed.  Careful about the ordering. */
97403383736SDon Brace 	if (new_entry->offload_config && new_entry->offload_enabled) {
97503383736SDon Brace 		/*
97603383736SDon Brace 		 * if drive is newly offload_enabled, we want to copy the
97703383736SDon Brace 		 * raid map data first.  If previously offload_enabled and
97803383736SDon Brace 		 * offload_config were set, raid map data had better be
97903383736SDon Brace 		 * the same as it was before.  if raid map data is changed
98003383736SDon Brace 		 * then it had better be the case that
98103383736SDon Brace 		 * h->dev[entry]->offload_enabled is currently 0.
98203383736SDon Brace 		 */
9839fb0de2dSStephen M. Cameron 		h->dev[entry]->raid_map = new_entry->raid_map;
98403383736SDon Brace 		h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
98503383736SDon Brace 		wmb(); /* ensure raid map updated prior to ->offload_enabled */
98603383736SDon Brace 	}
98703383736SDon Brace 	h->dev[entry]->offload_config = new_entry->offload_config;
98803383736SDon Brace 	h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
98903383736SDon Brace 	h->dev[entry]->offload_enabled = new_entry->offload_enabled;
99003383736SDon Brace 	h->dev[entry]->queue_depth = new_entry->queue_depth;
991250fb125SStephen M. Cameron 
992bd9244f7SScott Teel 	dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d updated.\n",
993bd9244f7SScott Teel 		scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
994bd9244f7SScott Teel 		new_entry->target, new_entry->lun);
995bd9244f7SScott Teel }
996bd9244f7SScott Teel 
9972a8ccf31SStephen M. Cameron /* Replace an entry from h->dev[] array. */
9982a8ccf31SStephen M. Cameron static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
9992a8ccf31SStephen M. Cameron 	int entry, struct hpsa_scsi_dev_t *new_entry,
10002a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *added[], int *nadded,
10012a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
10022a8ccf31SStephen M. Cameron {
10032a8ccf31SStephen M. Cameron 	/* assumes h->devlock is held */
1004cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
10052a8ccf31SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
10062a8ccf31SStephen M. Cameron 	(*nremoved)++;
100701350d05SStephen M. Cameron 
100801350d05SStephen M. Cameron 	/*
100901350d05SStephen M. Cameron 	 * New physical devices won't have target/lun assigned yet
101001350d05SStephen M. Cameron 	 * so we need to preserve the values in the slot we are replacing.
101101350d05SStephen M. Cameron 	 */
101201350d05SStephen M. Cameron 	if (new_entry->target == -1) {
101301350d05SStephen M. Cameron 		new_entry->target = h->dev[entry]->target;
101401350d05SStephen M. Cameron 		new_entry->lun = h->dev[entry]->lun;
101501350d05SStephen M. Cameron 	}
101601350d05SStephen M. Cameron 
10172a8ccf31SStephen M. Cameron 	h->dev[entry] = new_entry;
10182a8ccf31SStephen M. Cameron 	added[*nadded] = new_entry;
10192a8ccf31SStephen M. Cameron 	(*nadded)++;
10202a8ccf31SStephen M. Cameron 	dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n",
10212a8ccf31SStephen M. Cameron 		scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
10222a8ccf31SStephen M. Cameron 			new_entry->target, new_entry->lun);
10232a8ccf31SStephen M. Cameron }
10242a8ccf31SStephen M. Cameron 
1025edd16368SStephen M. Cameron /* Remove an entry from h->dev[] array. */
1026edd16368SStephen M. Cameron static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
1027edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
1028edd16368SStephen M. Cameron {
1029edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
1030edd16368SStephen M. Cameron 	int i;
1031edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1032edd16368SStephen M. Cameron 
1033cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
1034edd16368SStephen M. Cameron 
1035edd16368SStephen M. Cameron 	sd = h->dev[entry];
1036edd16368SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
1037edd16368SStephen M. Cameron 	(*nremoved)++;
1038edd16368SStephen M. Cameron 
1039edd16368SStephen M. Cameron 	for (i = entry; i < h->ndevices-1; i++)
1040edd16368SStephen M. Cameron 		h->dev[i] = h->dev[i+1];
1041edd16368SStephen M. Cameron 	h->ndevices--;
1042edd16368SStephen M. Cameron 	dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
1043edd16368SStephen M. Cameron 		scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
1044edd16368SStephen M. Cameron 		sd->lun);
1045edd16368SStephen M. Cameron }
1046edd16368SStephen M. Cameron 
1047edd16368SStephen M. Cameron #define SCSI3ADDR_EQ(a, b) ( \
1048edd16368SStephen M. Cameron 	(a)[7] == (b)[7] && \
1049edd16368SStephen M. Cameron 	(a)[6] == (b)[6] && \
1050edd16368SStephen M. Cameron 	(a)[5] == (b)[5] && \
1051edd16368SStephen M. Cameron 	(a)[4] == (b)[4] && \
1052edd16368SStephen M. Cameron 	(a)[3] == (b)[3] && \
1053edd16368SStephen M. Cameron 	(a)[2] == (b)[2] && \
1054edd16368SStephen M. Cameron 	(a)[1] == (b)[1] && \
1055edd16368SStephen M. Cameron 	(a)[0] == (b)[0])
1056edd16368SStephen M. Cameron 
1057edd16368SStephen M. Cameron static void fixup_botched_add(struct ctlr_info *h,
1058edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *added)
1059edd16368SStephen M. Cameron {
1060edd16368SStephen M. Cameron 	/* called when scsi_add_device fails in order to re-adjust
1061edd16368SStephen M. Cameron 	 * h->dev[] to match the mid layer's view.
1062edd16368SStephen M. Cameron 	 */
1063edd16368SStephen M. Cameron 	unsigned long flags;
1064edd16368SStephen M. Cameron 	int i, j;
1065edd16368SStephen M. Cameron 
1066edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
1067edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1068edd16368SStephen M. Cameron 		if (h->dev[i] == added) {
1069edd16368SStephen M. Cameron 			for (j = i; j < h->ndevices-1; j++)
1070edd16368SStephen M. Cameron 				h->dev[j] = h->dev[j+1];
1071edd16368SStephen M. Cameron 			h->ndevices--;
1072edd16368SStephen M. Cameron 			break;
1073edd16368SStephen M. Cameron 		}
1074edd16368SStephen M. Cameron 	}
1075edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
1076edd16368SStephen M. Cameron 	kfree(added);
1077edd16368SStephen M. Cameron }
1078edd16368SStephen M. Cameron 
1079edd16368SStephen M. Cameron static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
1080edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev2)
1081edd16368SStephen M. Cameron {
1082edd16368SStephen M. Cameron 	/* we compare everything except lun and target as these
1083edd16368SStephen M. Cameron 	 * are not yet assigned.  Compare parts likely
1084edd16368SStephen M. Cameron 	 * to differ first
1085edd16368SStephen M. Cameron 	 */
1086edd16368SStephen M. Cameron 	if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
1087edd16368SStephen M. Cameron 		sizeof(dev1->scsi3addr)) != 0)
1088edd16368SStephen M. Cameron 		return 0;
1089edd16368SStephen M. Cameron 	if (memcmp(dev1->device_id, dev2->device_id,
1090edd16368SStephen M. Cameron 		sizeof(dev1->device_id)) != 0)
1091edd16368SStephen M. Cameron 		return 0;
1092edd16368SStephen M. Cameron 	if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
1093edd16368SStephen M. Cameron 		return 0;
1094edd16368SStephen M. Cameron 	if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
1095edd16368SStephen M. Cameron 		return 0;
1096edd16368SStephen M. Cameron 	if (dev1->devtype != dev2->devtype)
1097edd16368SStephen M. Cameron 		return 0;
1098edd16368SStephen M. Cameron 	if (dev1->bus != dev2->bus)
1099edd16368SStephen M. Cameron 		return 0;
1100edd16368SStephen M. Cameron 	return 1;
1101edd16368SStephen M. Cameron }
1102edd16368SStephen M. Cameron 
1103bd9244f7SScott Teel static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
1104bd9244f7SScott Teel 	struct hpsa_scsi_dev_t *dev2)
1105bd9244f7SScott Teel {
1106bd9244f7SScott Teel 	/* Device attributes that can change, but don't mean
1107bd9244f7SScott Teel 	 * that the device is a different device, nor that the OS
1108bd9244f7SScott Teel 	 * needs to be told anything about the change.
1109bd9244f7SScott Teel 	 */
1110bd9244f7SScott Teel 	if (dev1->raid_level != dev2->raid_level)
1111bd9244f7SScott Teel 		return 1;
1112250fb125SStephen M. Cameron 	if (dev1->offload_config != dev2->offload_config)
1113250fb125SStephen M. Cameron 		return 1;
1114250fb125SStephen M. Cameron 	if (dev1->offload_enabled != dev2->offload_enabled)
1115250fb125SStephen M. Cameron 		return 1;
111603383736SDon Brace 	if (dev1->queue_depth != dev2->queue_depth)
111703383736SDon Brace 		return 1;
1118bd9244f7SScott Teel 	return 0;
1119bd9244f7SScott Teel }
1120bd9244f7SScott Teel 
1121edd16368SStephen M. Cameron /* Find needle in haystack.  If exact match found, return DEVICE_SAME,
1122edd16368SStephen M. Cameron  * and return needle location in *index.  If scsi3addr matches, but not
1123edd16368SStephen M. Cameron  * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
1124bd9244f7SScott Teel  * location in *index.
1125bd9244f7SScott Teel  * In the case of a minor device attribute change, such as RAID level, just
1126bd9244f7SScott Teel  * return DEVICE_UPDATED, along with the updated device's location in index.
1127bd9244f7SScott Teel  * If needle not found, return DEVICE_NOT_FOUND.
1128edd16368SStephen M. Cameron  */
1129edd16368SStephen M. Cameron static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
1130edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *haystack[], int haystack_size,
1131edd16368SStephen M. Cameron 	int *index)
1132edd16368SStephen M. Cameron {
1133edd16368SStephen M. Cameron 	int i;
1134edd16368SStephen M. Cameron #define DEVICE_NOT_FOUND 0
1135edd16368SStephen M. Cameron #define DEVICE_CHANGED 1
1136edd16368SStephen M. Cameron #define DEVICE_SAME 2
1137bd9244f7SScott Teel #define DEVICE_UPDATED 3
1138edd16368SStephen M. Cameron 	for (i = 0; i < haystack_size; i++) {
113923231048SStephen M. Cameron 		if (haystack[i] == NULL) /* previously removed. */
114023231048SStephen M. Cameron 			continue;
1141edd16368SStephen M. Cameron 		if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
1142edd16368SStephen M. Cameron 			*index = i;
1143bd9244f7SScott Teel 			if (device_is_the_same(needle, haystack[i])) {
1144bd9244f7SScott Teel 				if (device_updated(needle, haystack[i]))
1145bd9244f7SScott Teel 					return DEVICE_UPDATED;
1146edd16368SStephen M. Cameron 				return DEVICE_SAME;
1147bd9244f7SScott Teel 			} else {
11489846590eSStephen M. Cameron 				/* Keep offline devices offline */
11499846590eSStephen M. Cameron 				if (needle->volume_offline)
11509846590eSStephen M. Cameron 					return DEVICE_NOT_FOUND;
1151edd16368SStephen M. Cameron 				return DEVICE_CHANGED;
1152edd16368SStephen M. Cameron 			}
1153edd16368SStephen M. Cameron 		}
1154bd9244f7SScott Teel 	}
1155edd16368SStephen M. Cameron 	*index = -1;
1156edd16368SStephen M. Cameron 	return DEVICE_NOT_FOUND;
1157edd16368SStephen M. Cameron }
1158edd16368SStephen M. Cameron 
11599846590eSStephen M. Cameron static void hpsa_monitor_offline_device(struct ctlr_info *h,
11609846590eSStephen M. Cameron 					unsigned char scsi3addr[])
11619846590eSStephen M. Cameron {
11629846590eSStephen M. Cameron 	struct offline_device_entry *device;
11639846590eSStephen M. Cameron 	unsigned long flags;
11649846590eSStephen M. Cameron 
11659846590eSStephen M. Cameron 	/* Check to see if device is already on the list */
11669846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
11679846590eSStephen M. Cameron 	list_for_each_entry(device, &h->offline_device_list, offline_list) {
11689846590eSStephen M. Cameron 		if (memcmp(device->scsi3addr, scsi3addr,
11699846590eSStephen M. Cameron 			sizeof(device->scsi3addr)) == 0) {
11709846590eSStephen M. Cameron 			spin_unlock_irqrestore(&h->offline_device_lock, flags);
11719846590eSStephen M. Cameron 			return;
11729846590eSStephen M. Cameron 		}
11739846590eSStephen M. Cameron 	}
11749846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
11759846590eSStephen M. Cameron 
11769846590eSStephen M. Cameron 	/* Device is not on the list, add it. */
11779846590eSStephen M. Cameron 	device = kmalloc(sizeof(*device), GFP_KERNEL);
11789846590eSStephen M. Cameron 	if (!device) {
11799846590eSStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in %s\n", __func__);
11809846590eSStephen M. Cameron 		return;
11819846590eSStephen M. Cameron 	}
11829846590eSStephen M. Cameron 	memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
11839846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
11849846590eSStephen M. Cameron 	list_add_tail(&device->offline_list, &h->offline_device_list);
11859846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
11869846590eSStephen M. Cameron }
11879846590eSStephen M. Cameron 
11889846590eSStephen M. Cameron /* Print a message explaining various offline volume states */
11899846590eSStephen M. Cameron static void hpsa_show_volume_status(struct ctlr_info *h,
11909846590eSStephen M. Cameron 	struct hpsa_scsi_dev_t *sd)
11919846590eSStephen M. Cameron {
11929846590eSStephen M. Cameron 	if (sd->volume_offline == HPSA_VPD_LV_STATUS_UNSUPPORTED)
11939846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
11949846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume status is not available through vital product data pages.\n",
11959846590eSStephen M. Cameron 			h->scsi_host->host_no,
11969846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
11979846590eSStephen M. Cameron 	switch (sd->volume_offline) {
11989846590eSStephen M. Cameron 	case HPSA_LV_OK:
11999846590eSStephen M. Cameron 		break;
12009846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ERASE:
12019846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12029846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing background erase process.\n",
12039846590eSStephen M. Cameron 			h->scsi_host->host_no,
12049846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12059846590eSStephen M. Cameron 		break;
12069846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_RPI:
12079846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12089846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing rapid parity initialization process.\n",
12099846590eSStephen M. Cameron 			h->scsi_host->host_no,
12109846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12119846590eSStephen M. Cameron 		break;
12129846590eSStephen M. Cameron 	case HPSA_LV_PENDING_RPI:
12139846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12149846590eSStephen M. Cameron 				"C%d:B%d:T%d:L%d Volume is queued for rapid parity initialization process.\n",
12159846590eSStephen M. Cameron 				h->scsi_host->host_no,
12169846590eSStephen M. Cameron 				sd->bus, sd->target, sd->lun);
12179846590eSStephen M. Cameron 		break;
12189846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_NO_KEY:
12199846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12209846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because key is not present.\n",
12219846590eSStephen M. Cameron 			h->scsi_host->host_no,
12229846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12239846590eSStephen M. Cameron 		break;
12249846590eSStephen M. Cameron 	case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
12259846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12269846590eSStephen 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",
12279846590eSStephen M. Cameron 			h->scsi_host->host_no,
12289846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12299846590eSStephen M. Cameron 		break;
12309846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION:
12319846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12329846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing encryption process.\n",
12339846590eSStephen M. Cameron 			h->scsi_host->host_no,
12349846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12359846590eSStephen M. Cameron 		break;
12369846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
12379846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12389846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing encryption re-keying process.\n",
12399846590eSStephen M. Cameron 			h->scsi_host->host_no,
12409846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12419846590eSStephen M. Cameron 		break;
12429846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
12439846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12449846590eSStephen 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",
12459846590eSStephen M. Cameron 			h->scsi_host->host_no,
12469846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12479846590eSStephen M. Cameron 		break;
12489846590eSStephen M. Cameron 	case HPSA_LV_PENDING_ENCRYPTION:
12499846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12509846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is pending migration to encrypted state, but process has not started.\n",
12519846590eSStephen M. Cameron 			h->scsi_host->host_no,
12529846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12539846590eSStephen M. Cameron 		break;
12549846590eSStephen M. Cameron 	case HPSA_LV_PENDING_ENCRYPTION_REKEYING:
12559846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12569846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is encrypted and is pending encryption rekeying.\n",
12579846590eSStephen M. Cameron 			h->scsi_host->host_no,
12589846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12599846590eSStephen M. Cameron 		break;
12609846590eSStephen M. Cameron 	}
12619846590eSStephen M. Cameron }
12629846590eSStephen M. Cameron 
126303383736SDon Brace /*
126403383736SDon Brace  * Figure the list of physical drive pointers for a logical drive with
126503383736SDon Brace  * raid offload configured.
126603383736SDon Brace  */
126703383736SDon Brace static void hpsa_figure_phys_disk_ptrs(struct ctlr_info *h,
126803383736SDon Brace 				struct hpsa_scsi_dev_t *dev[], int ndevices,
126903383736SDon Brace 				struct hpsa_scsi_dev_t *logical_drive)
127003383736SDon Brace {
127103383736SDon Brace 	struct raid_map_data *map = &logical_drive->raid_map;
127203383736SDon Brace 	struct raid_map_disk_data *dd = &map->data[0];
127303383736SDon Brace 	int i, j;
127403383736SDon Brace 	int total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
127503383736SDon Brace 				le16_to_cpu(map->metadata_disks_per_row);
127603383736SDon Brace 	int nraid_map_entries = le16_to_cpu(map->row_cnt) *
127703383736SDon Brace 				le16_to_cpu(map->layout_map_count) *
127803383736SDon Brace 				total_disks_per_row;
127903383736SDon Brace 	int nphys_disk = le16_to_cpu(map->layout_map_count) *
128003383736SDon Brace 				total_disks_per_row;
128103383736SDon Brace 	int qdepth;
128203383736SDon Brace 
128303383736SDon Brace 	if (nraid_map_entries > RAID_MAP_MAX_ENTRIES)
128403383736SDon Brace 		nraid_map_entries = RAID_MAP_MAX_ENTRIES;
128503383736SDon Brace 
128603383736SDon Brace 	qdepth = 0;
128703383736SDon Brace 	for (i = 0; i < nraid_map_entries; i++) {
128803383736SDon Brace 		logical_drive->phys_disk[i] = NULL;
128903383736SDon Brace 		if (!logical_drive->offload_config)
129003383736SDon Brace 			continue;
129103383736SDon Brace 		for (j = 0; j < ndevices; j++) {
129203383736SDon Brace 			if (dev[j]->devtype != TYPE_DISK)
129303383736SDon Brace 				continue;
129403383736SDon Brace 			if (is_logical_dev_addr_mode(dev[j]->scsi3addr))
129503383736SDon Brace 				continue;
129603383736SDon Brace 			if (dev[j]->ioaccel_handle != dd[i].ioaccel_handle)
129703383736SDon Brace 				continue;
129803383736SDon Brace 
129903383736SDon Brace 			logical_drive->phys_disk[i] = dev[j];
130003383736SDon Brace 			if (i < nphys_disk)
130103383736SDon Brace 				qdepth = min(h->nr_cmds, qdepth +
130203383736SDon Brace 				    logical_drive->phys_disk[i]->queue_depth);
130303383736SDon Brace 			break;
130403383736SDon Brace 		}
130503383736SDon Brace 
130603383736SDon Brace 		/*
130703383736SDon Brace 		 * This can happen if a physical drive is removed and
130803383736SDon Brace 		 * the logical drive is degraded.  In that case, the RAID
130903383736SDon Brace 		 * map data will refer to a physical disk which isn't actually
131003383736SDon Brace 		 * present.  And in that case offload_enabled should already
131103383736SDon Brace 		 * be 0, but we'll turn it off here just in case
131203383736SDon Brace 		 */
131303383736SDon Brace 		if (!logical_drive->phys_disk[i]) {
131403383736SDon Brace 			logical_drive->offload_enabled = 0;
131503383736SDon Brace 			logical_drive->queue_depth = h->nr_cmds;
131603383736SDon Brace 		}
131703383736SDon Brace 	}
131803383736SDon Brace 	if (nraid_map_entries)
131903383736SDon Brace 		/*
132003383736SDon Brace 		 * This is correct for reads, too high for full stripe writes,
132103383736SDon Brace 		 * way too high for partial stripe writes
132203383736SDon Brace 		 */
132303383736SDon Brace 		logical_drive->queue_depth = qdepth;
132403383736SDon Brace 	else
132503383736SDon Brace 		logical_drive->queue_depth = h->nr_cmds;
132603383736SDon Brace }
132703383736SDon Brace 
132803383736SDon Brace static void hpsa_update_log_drive_phys_drive_ptrs(struct ctlr_info *h,
132903383736SDon Brace 				struct hpsa_scsi_dev_t *dev[], int ndevices)
133003383736SDon Brace {
133103383736SDon Brace 	int i;
133203383736SDon Brace 
133303383736SDon Brace 	for (i = 0; i < ndevices; i++) {
133403383736SDon Brace 		if (dev[i]->devtype != TYPE_DISK)
133503383736SDon Brace 			continue;
133603383736SDon Brace 		if (!is_logical_dev_addr_mode(dev[i]->scsi3addr))
133703383736SDon Brace 			continue;
133803383736SDon Brace 		hpsa_figure_phys_disk_ptrs(h, dev, ndevices, dev[i]);
133903383736SDon Brace 	}
134003383736SDon Brace }
134103383736SDon Brace 
13424967bd3eSStephen M. Cameron static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
1343edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd[], int nsds)
1344edd16368SStephen M. Cameron {
1345edd16368SStephen M. Cameron 	/* sd contains scsi3 addresses and devtypes, and inquiry
1346edd16368SStephen M. Cameron 	 * data.  This function takes what's in sd to be the current
1347edd16368SStephen M. Cameron 	 * reality and updates h->dev[] to reflect that reality.
1348edd16368SStephen M. Cameron 	 */
1349edd16368SStephen M. Cameron 	int i, entry, device_change, changes = 0;
1350edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *csd;
1351edd16368SStephen M. Cameron 	unsigned long flags;
1352edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **added, **removed;
1353edd16368SStephen M. Cameron 	int nadded, nremoved;
1354edd16368SStephen M. Cameron 	struct Scsi_Host *sh = NULL;
1355edd16368SStephen M. Cameron 
1356cfe5badcSScott Teel 	added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
1357cfe5badcSScott Teel 	removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
1358edd16368SStephen M. Cameron 
1359edd16368SStephen M. Cameron 	if (!added || !removed) {
1360edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in "
1361edd16368SStephen M. Cameron 			"adjust_hpsa_scsi_table\n");
1362edd16368SStephen M. Cameron 		goto free_and_out;
1363edd16368SStephen M. Cameron 	}
1364edd16368SStephen M. Cameron 
1365edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1366edd16368SStephen M. Cameron 
1367edd16368SStephen M. Cameron 	/* find any devices in h->dev[] that are not in
1368edd16368SStephen M. Cameron 	 * sd[] and remove them from h->dev[], and for any
1369edd16368SStephen M. Cameron 	 * devices which have changed, remove the old device
1370edd16368SStephen M. Cameron 	 * info and add the new device info.
1371bd9244f7SScott Teel 	 * If minor device attributes change, just update
1372bd9244f7SScott Teel 	 * the existing device structure.
1373edd16368SStephen M. Cameron 	 */
1374edd16368SStephen M. Cameron 	i = 0;
1375edd16368SStephen M. Cameron 	nremoved = 0;
1376edd16368SStephen M. Cameron 	nadded = 0;
1377edd16368SStephen M. Cameron 	while (i < h->ndevices) {
1378edd16368SStephen M. Cameron 		csd = h->dev[i];
1379edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
1380edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
1381edd16368SStephen M. Cameron 			changes++;
1382edd16368SStephen M. Cameron 			hpsa_scsi_remove_entry(h, hostno, i,
1383edd16368SStephen M. Cameron 				removed, &nremoved);
1384edd16368SStephen M. Cameron 			continue; /* remove ^^^, hence i not incremented */
1385edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
1386edd16368SStephen M. Cameron 			changes++;
13872a8ccf31SStephen M. Cameron 			hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
13882a8ccf31SStephen M. Cameron 				added, &nadded, removed, &nremoved);
1389c7f172dcSStephen M. Cameron 			/* Set it to NULL to prevent it from being freed
1390c7f172dcSStephen M. Cameron 			 * at the bottom of hpsa_update_scsi_devices()
1391c7f172dcSStephen M. Cameron 			 */
1392c7f172dcSStephen M. Cameron 			sd[entry] = NULL;
1393bd9244f7SScott Teel 		} else if (device_change == DEVICE_UPDATED) {
1394bd9244f7SScott Teel 			hpsa_scsi_update_entry(h, hostno, i, sd[entry]);
1395edd16368SStephen M. Cameron 		}
1396edd16368SStephen M. Cameron 		i++;
1397edd16368SStephen M. Cameron 	}
1398edd16368SStephen M. Cameron 
1399edd16368SStephen M. Cameron 	/* Now, make sure every device listed in sd[] is also
1400edd16368SStephen M. Cameron 	 * listed in h->dev[], adding them if they aren't found
1401edd16368SStephen M. Cameron 	 */
1402edd16368SStephen M. Cameron 
1403edd16368SStephen M. Cameron 	for (i = 0; i < nsds; i++) {
1404edd16368SStephen M. Cameron 		if (!sd[i]) /* if already added above. */
1405edd16368SStephen M. Cameron 			continue;
14069846590eSStephen M. Cameron 
14079846590eSStephen M. Cameron 		/* Don't add devices which are NOT READY, FORMAT IN PROGRESS
14089846590eSStephen M. Cameron 		 * as the SCSI mid-layer does not handle such devices well.
14099846590eSStephen M. Cameron 		 * It relentlessly loops sending TUR at 3Hz, then READ(10)
14109846590eSStephen M. Cameron 		 * at 160Hz, and prevents the system from coming up.
14119846590eSStephen M. Cameron 		 */
14129846590eSStephen M. Cameron 		if (sd[i]->volume_offline) {
14139846590eSStephen M. Cameron 			hpsa_show_volume_status(h, sd[i]);
14149846590eSStephen M. Cameron 			dev_info(&h->pdev->dev, "c%db%dt%dl%d: temporarily offline\n",
14159846590eSStephen M. Cameron 				h->scsi_host->host_no,
14169846590eSStephen M. Cameron 				sd[i]->bus, sd[i]->target, sd[i]->lun);
14179846590eSStephen M. Cameron 			continue;
14189846590eSStephen M. Cameron 		}
14199846590eSStephen M. Cameron 
1420edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1421edd16368SStephen M. Cameron 					h->ndevices, &entry);
1422edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
1423edd16368SStephen M. Cameron 			changes++;
1424edd16368SStephen M. Cameron 			if (hpsa_scsi_add_entry(h, hostno, sd[i],
1425edd16368SStephen M. Cameron 				added, &nadded) != 0)
1426edd16368SStephen M. Cameron 				break;
1427edd16368SStephen M. Cameron 			sd[i] = NULL; /* prevent from being freed later. */
1428edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
1429edd16368SStephen M. Cameron 			/* should never happen... */
1430edd16368SStephen M. Cameron 			changes++;
1431edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev,
1432edd16368SStephen M. Cameron 				"device unexpectedly changed.\n");
1433edd16368SStephen M. Cameron 			/* but if it does happen, we just ignore that device */
1434edd16368SStephen M. Cameron 		}
1435edd16368SStephen M. Cameron 	}
1436edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1437edd16368SStephen M. Cameron 
14389846590eSStephen M. Cameron 	/* Monitor devices which are in one of several NOT READY states to be
14399846590eSStephen M. Cameron 	 * brought online later. This must be done without holding h->devlock,
14409846590eSStephen M. Cameron 	 * so don't touch h->dev[]
14419846590eSStephen M. Cameron 	 */
14429846590eSStephen M. Cameron 	for (i = 0; i < nsds; i++) {
14439846590eSStephen M. Cameron 		if (!sd[i]) /* if already added above. */
14449846590eSStephen M. Cameron 			continue;
14459846590eSStephen M. Cameron 		if (sd[i]->volume_offline)
14469846590eSStephen M. Cameron 			hpsa_monitor_offline_device(h, sd[i]->scsi3addr);
14479846590eSStephen M. Cameron 	}
14489846590eSStephen M. Cameron 
1449edd16368SStephen M. Cameron 	/* Don't notify scsi mid layer of any changes the first time through
1450edd16368SStephen M. Cameron 	 * (or if there are no changes) scsi_scan_host will do it later the
1451edd16368SStephen M. Cameron 	 * first time through.
1452edd16368SStephen M. Cameron 	 */
1453edd16368SStephen M. Cameron 	if (hostno == -1 || !changes)
1454edd16368SStephen M. Cameron 		goto free_and_out;
1455edd16368SStephen M. Cameron 
1456edd16368SStephen M. Cameron 	sh = h->scsi_host;
1457edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any removed devices */
1458edd16368SStephen M. Cameron 	for (i = 0; i < nremoved; i++) {
1459edd16368SStephen M. Cameron 		struct scsi_device *sdev =
1460edd16368SStephen M. Cameron 			scsi_device_lookup(sh, removed[i]->bus,
1461edd16368SStephen M. Cameron 				removed[i]->target, removed[i]->lun);
1462edd16368SStephen M. Cameron 		if (sdev != NULL) {
1463edd16368SStephen M. Cameron 			scsi_remove_device(sdev);
1464edd16368SStephen M. Cameron 			scsi_device_put(sdev);
1465edd16368SStephen M. Cameron 		} else {
1466edd16368SStephen M. Cameron 			/* We don't expect to get here.
1467edd16368SStephen M. Cameron 			 * future cmds to this device will get selection
1468edd16368SStephen M. Cameron 			 * timeout as if the device was gone.
1469edd16368SStephen M. Cameron 			 */
1470edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
1471edd16368SStephen M. Cameron 				" for removal.", hostno, removed[i]->bus,
1472edd16368SStephen M. Cameron 				removed[i]->target, removed[i]->lun);
1473edd16368SStephen M. Cameron 		}
1474edd16368SStephen M. Cameron 		kfree(removed[i]);
1475edd16368SStephen M. Cameron 		removed[i] = NULL;
1476edd16368SStephen M. Cameron 	}
1477edd16368SStephen M. Cameron 
1478edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any added devices */
1479edd16368SStephen M. Cameron 	for (i = 0; i < nadded; i++) {
1480edd16368SStephen M. Cameron 		if (scsi_add_device(sh, added[i]->bus,
1481edd16368SStephen M. Cameron 			added[i]->target, added[i]->lun) == 0)
1482edd16368SStephen M. Cameron 			continue;
1483edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
1484edd16368SStephen M. Cameron 			"device not added.\n", hostno, added[i]->bus,
1485edd16368SStephen M. Cameron 			added[i]->target, added[i]->lun);
1486edd16368SStephen M. Cameron 		/* now we have to remove it from h->dev,
1487edd16368SStephen M. Cameron 		 * since it didn't get added to scsi mid layer
1488edd16368SStephen M. Cameron 		 */
1489edd16368SStephen M. Cameron 		fixup_botched_add(h, added[i]);
1490edd16368SStephen M. Cameron 	}
1491edd16368SStephen M. Cameron 
1492edd16368SStephen M. Cameron free_and_out:
1493edd16368SStephen M. Cameron 	kfree(added);
1494edd16368SStephen M. Cameron 	kfree(removed);
1495edd16368SStephen M. Cameron }
1496edd16368SStephen M. Cameron 
1497edd16368SStephen M. Cameron /*
14989e03aa2fSJoe Perches  * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
1499edd16368SStephen M. Cameron  * Assume's h->devlock is held.
1500edd16368SStephen M. Cameron  */
1501edd16368SStephen M. Cameron static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1502edd16368SStephen M. Cameron 	int bus, int target, int lun)
1503edd16368SStephen M. Cameron {
1504edd16368SStephen M. Cameron 	int i;
1505edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1506edd16368SStephen M. Cameron 
1507edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1508edd16368SStephen M. Cameron 		sd = h->dev[i];
1509edd16368SStephen M. Cameron 		if (sd->bus == bus && sd->target == target && sd->lun == lun)
1510edd16368SStephen M. Cameron 			return sd;
1511edd16368SStephen M. Cameron 	}
1512edd16368SStephen M. Cameron 	return NULL;
1513edd16368SStephen M. Cameron }
1514edd16368SStephen M. Cameron 
1515edd16368SStephen M. Cameron /* link sdev->hostdata to our per-device structure. */
1516edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev)
1517edd16368SStephen M. Cameron {
1518edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1519edd16368SStephen M. Cameron 	unsigned long flags;
1520edd16368SStephen M. Cameron 	struct ctlr_info *h;
1521edd16368SStephen M. Cameron 
1522edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
1523edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1524edd16368SStephen M. Cameron 	sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1525edd16368SStephen M. Cameron 		sdev_id(sdev), sdev->lun);
152603383736SDon Brace 	if (sd != NULL) {
1527edd16368SStephen M. Cameron 		sdev->hostdata = sd;
152803383736SDon Brace 		if (sd->queue_depth)
152903383736SDon Brace 			scsi_change_queue_depth(sdev, sd->queue_depth);
153003383736SDon Brace 		atomic_set(&sd->ioaccel_cmds_out, 0);
153103383736SDon Brace 	}
1532edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1533edd16368SStephen M. Cameron 	return 0;
1534edd16368SStephen M. Cameron }
1535edd16368SStephen M. Cameron 
1536edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev)
1537edd16368SStephen M. Cameron {
1538bcc44255SStephen M. Cameron 	/* nothing to do. */
1539edd16368SStephen M. Cameron }
1540edd16368SStephen M. Cameron 
154133a2ffceSStephen M. Cameron static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
154233a2ffceSStephen M. Cameron {
154333a2ffceSStephen M. Cameron 	int i;
154433a2ffceSStephen M. Cameron 
154533a2ffceSStephen M. Cameron 	if (!h->cmd_sg_list)
154633a2ffceSStephen M. Cameron 		return;
154733a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
154833a2ffceSStephen M. Cameron 		kfree(h->cmd_sg_list[i]);
154933a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = NULL;
155033a2ffceSStephen M. Cameron 	}
155133a2ffceSStephen M. Cameron 	kfree(h->cmd_sg_list);
155233a2ffceSStephen M. Cameron 	h->cmd_sg_list = NULL;
155333a2ffceSStephen M. Cameron }
155433a2ffceSStephen M. Cameron 
155533a2ffceSStephen M. Cameron static int hpsa_allocate_sg_chain_blocks(struct ctlr_info *h)
155633a2ffceSStephen M. Cameron {
155733a2ffceSStephen M. Cameron 	int i;
155833a2ffceSStephen M. Cameron 
155933a2ffceSStephen M. Cameron 	if (h->chainsize <= 0)
156033a2ffceSStephen M. Cameron 		return 0;
156133a2ffceSStephen M. Cameron 
156233a2ffceSStephen M. Cameron 	h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
156333a2ffceSStephen M. Cameron 				GFP_KERNEL);
15643d4e6af8SRobert Elliott 	if (!h->cmd_sg_list) {
15653d4e6af8SRobert Elliott 		dev_err(&h->pdev->dev, "Failed to allocate SG list\n");
156633a2ffceSStephen M. Cameron 		return -ENOMEM;
15673d4e6af8SRobert Elliott 	}
156833a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
156933a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
157033a2ffceSStephen M. Cameron 						h->chainsize, GFP_KERNEL);
15713d4e6af8SRobert Elliott 		if (!h->cmd_sg_list[i]) {
15723d4e6af8SRobert Elliott 			dev_err(&h->pdev->dev, "Failed to allocate cmd SG\n");
157333a2ffceSStephen M. Cameron 			goto clean;
157433a2ffceSStephen M. Cameron 		}
15753d4e6af8SRobert Elliott 	}
157633a2ffceSStephen M. Cameron 	return 0;
157733a2ffceSStephen M. Cameron 
157833a2ffceSStephen M. Cameron clean:
157933a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
158033a2ffceSStephen M. Cameron 	return -ENOMEM;
158133a2ffceSStephen M. Cameron }
158233a2ffceSStephen M. Cameron 
1583e2bea6dfSStephen M. Cameron static int hpsa_map_sg_chain_block(struct ctlr_info *h,
158433a2ffceSStephen M. Cameron 	struct CommandList *c)
158533a2ffceSStephen M. Cameron {
158633a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg, *chain_block;
158733a2ffceSStephen M. Cameron 	u64 temp64;
158850a0decfSStephen M. Cameron 	u32 chain_len;
158933a2ffceSStephen M. Cameron 
159033a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
159133a2ffceSStephen M. Cameron 	chain_block = h->cmd_sg_list[c->cmdindex];
159250a0decfSStephen M. Cameron 	chain_sg->Ext = cpu_to_le32(HPSA_SG_CHAIN);
159350a0decfSStephen M. Cameron 	chain_len = sizeof(*chain_sg) *
15942b08b3e9SDon Brace 		(le16_to_cpu(c->Header.SGTotal) - h->max_cmd_sg_entries);
159550a0decfSStephen M. Cameron 	chain_sg->Len = cpu_to_le32(chain_len);
159650a0decfSStephen M. Cameron 	temp64 = pci_map_single(h->pdev, chain_block, chain_len,
159733a2ffceSStephen M. Cameron 				PCI_DMA_TODEVICE);
1598e2bea6dfSStephen M. Cameron 	if (dma_mapping_error(&h->pdev->dev, temp64)) {
1599e2bea6dfSStephen M. Cameron 		/* prevent subsequent unmapping */
160050a0decfSStephen M. Cameron 		chain_sg->Addr = cpu_to_le64(0);
1601e2bea6dfSStephen M. Cameron 		return -1;
1602e2bea6dfSStephen M. Cameron 	}
160350a0decfSStephen M. Cameron 	chain_sg->Addr = cpu_to_le64(temp64);
1604e2bea6dfSStephen M. Cameron 	return 0;
160533a2ffceSStephen M. Cameron }
160633a2ffceSStephen M. Cameron 
160733a2ffceSStephen M. Cameron static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
160833a2ffceSStephen M. Cameron 	struct CommandList *c)
160933a2ffceSStephen M. Cameron {
161033a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg;
161133a2ffceSStephen M. Cameron 
161250a0decfSStephen M. Cameron 	if (le16_to_cpu(c->Header.SGTotal) <= h->max_cmd_sg_entries)
161333a2ffceSStephen M. Cameron 		return;
161433a2ffceSStephen M. Cameron 
161533a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
161650a0decfSStephen M. Cameron 	pci_unmap_single(h->pdev, le64_to_cpu(chain_sg->Addr),
161750a0decfSStephen M. Cameron 			le32_to_cpu(chain_sg->Len), PCI_DMA_TODEVICE);
161833a2ffceSStephen M. Cameron }
161933a2ffceSStephen M. Cameron 
1620a09c1441SScott Teel 
1621a09c1441SScott Teel /* Decode the various types of errors on ioaccel2 path.
1622a09c1441SScott Teel  * Return 1 for any error that should generate a RAID path retry.
1623a09c1441SScott Teel  * Return 0 for errors that don't require a RAID path retry.
1624a09c1441SScott Teel  */
1625a09c1441SScott Teel static int handle_ioaccel_mode2_error(struct ctlr_info *h,
1626c349775eSScott Teel 					struct CommandList *c,
1627c349775eSScott Teel 					struct scsi_cmnd *cmd,
1628c349775eSScott Teel 					struct io_accel2_cmd *c2)
1629c349775eSScott Teel {
1630c349775eSScott Teel 	int data_len;
1631a09c1441SScott Teel 	int retry = 0;
1632c349775eSScott Teel 
1633c349775eSScott Teel 	switch (c2->error_data.serv_response) {
1634c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_COMPLETE:
1635c349775eSScott Teel 		switch (c2->error_data.status) {
1636c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
1637c349775eSScott Teel 			break;
1638c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
1639c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1640c349775eSScott Teel 				"%s: task complete with check condition.\n",
1641c349775eSScott Teel 				"HP SSD Smart Path");
1642ee6b1889SStephen M. Cameron 			cmd->result |= SAM_STAT_CHECK_CONDITION;
1643c349775eSScott Teel 			if (c2->error_data.data_present !=
1644ee6b1889SStephen M. Cameron 					IOACCEL2_SENSE_DATA_PRESENT) {
1645ee6b1889SStephen M. Cameron 				memset(cmd->sense_buffer, 0,
1646ee6b1889SStephen M. Cameron 					SCSI_SENSE_BUFFERSIZE);
1647c349775eSScott Teel 				break;
1648ee6b1889SStephen M. Cameron 			}
1649c349775eSScott Teel 			/* copy the sense data */
1650c349775eSScott Teel 			data_len = c2->error_data.sense_data_len;
1651c349775eSScott Teel 			if (data_len > SCSI_SENSE_BUFFERSIZE)
1652c349775eSScott Teel 				data_len = SCSI_SENSE_BUFFERSIZE;
1653c349775eSScott Teel 			if (data_len > sizeof(c2->error_data.sense_data_buff))
1654c349775eSScott Teel 				data_len =
1655c349775eSScott Teel 					sizeof(c2->error_data.sense_data_buff);
1656c349775eSScott Teel 			memcpy(cmd->sense_buffer,
1657c349775eSScott Teel 				c2->error_data.sense_data_buff, data_len);
1658a09c1441SScott Teel 			retry = 1;
1659c349775eSScott Teel 			break;
1660c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
1661c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1662c349775eSScott Teel 				"%s: task complete with BUSY status.\n",
1663c349775eSScott Teel 				"HP SSD Smart Path");
1664a09c1441SScott Teel 			retry = 1;
1665c349775eSScott Teel 			break;
1666c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
1667c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1668c349775eSScott Teel 				"%s: task complete with reservation conflict.\n",
1669c349775eSScott Teel 				"HP SSD Smart Path");
1670a09c1441SScott Teel 			retry = 1;
1671c349775eSScott Teel 			break;
1672c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
1673c349775eSScott Teel 			/* Make scsi midlayer do unlimited retries */
1674c349775eSScott Teel 			cmd->result = DID_IMM_RETRY << 16;
1675c349775eSScott Teel 			break;
1676c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
1677c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1678c349775eSScott Teel 				"%s: task complete with aborted status.\n",
1679c349775eSScott Teel 				"HP SSD Smart Path");
1680a09c1441SScott Teel 			retry = 1;
1681c349775eSScott Teel 			break;
1682c349775eSScott Teel 		default:
1683c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1684c349775eSScott Teel 				"%s: task complete with unrecognized status: 0x%02x\n",
1685c349775eSScott Teel 				"HP SSD Smart Path", c2->error_data.status);
1686a09c1441SScott Teel 			retry = 1;
1687c349775eSScott Teel 			break;
1688c349775eSScott Teel 		}
1689c349775eSScott Teel 		break;
1690c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_FAILURE:
1691c349775eSScott Teel 		/* don't expect to get here. */
1692c349775eSScott Teel 		dev_warn(&h->pdev->dev,
1693c349775eSScott Teel 			"unexpected delivery or target failure, status = 0x%02x\n",
1694c349775eSScott Teel 			c2->error_data.status);
1695a09c1441SScott Teel 		retry = 1;
1696c349775eSScott Teel 		break;
1697c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
1698c349775eSScott Teel 		break;
1699c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
1700c349775eSScott Teel 		break;
1701c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
1702c349775eSScott Teel 		dev_warn(&h->pdev->dev, "task management function rejected.\n");
1703a09c1441SScott Teel 		retry = 1;
1704c349775eSScott Teel 		break;
1705c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
1706c349775eSScott Teel 		dev_warn(&h->pdev->dev, "task management function invalid LUN\n");
1707c349775eSScott Teel 		break;
1708c349775eSScott Teel 	default:
1709c349775eSScott Teel 		dev_warn(&h->pdev->dev,
1710c349775eSScott Teel 			"%s: Unrecognized server response: 0x%02x\n",
1711a09c1441SScott Teel 			"HP SSD Smart Path",
1712a09c1441SScott Teel 			c2->error_data.serv_response);
1713a09c1441SScott Teel 		retry = 1;
1714c349775eSScott Teel 		break;
1715c349775eSScott Teel 	}
1716a09c1441SScott Teel 
1717a09c1441SScott Teel 	return retry;	/* retry on raid path? */
1718c349775eSScott Teel }
1719c349775eSScott Teel 
1720c349775eSScott Teel static void process_ioaccel2_completion(struct ctlr_info *h,
1721c349775eSScott Teel 		struct CommandList *c, struct scsi_cmnd *cmd,
1722c349775eSScott Teel 		struct hpsa_scsi_dev_t *dev)
1723c349775eSScott Teel {
1724c349775eSScott Teel 	struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
1725c349775eSScott Teel 
1726c349775eSScott Teel 	/* check for good status */
1727c349775eSScott Teel 	if (likely(c2->error_data.serv_response == 0 &&
1728c349775eSScott Teel 			c2->error_data.status == 0)) {
1729c349775eSScott Teel 		cmd_free(h, c);
1730c349775eSScott Teel 		cmd->scsi_done(cmd);
1731c349775eSScott Teel 		return;
1732c349775eSScott Teel 	}
1733c349775eSScott Teel 
1734c349775eSScott Teel 	/* Any RAID offload error results in retry which will use
1735c349775eSScott Teel 	 * the normal I/O path so the controller can handle whatever's
1736c349775eSScott Teel 	 * wrong.
1737c349775eSScott Teel 	 */
1738c349775eSScott Teel 	if (is_logical_dev_addr_mode(dev->scsi3addr) &&
1739c349775eSScott Teel 		c2->error_data.serv_response ==
1740c349775eSScott Teel 			IOACCEL2_SERV_RESPONSE_FAILURE) {
1741080ef1ccSDon Brace 		if (c2->error_data.status ==
1742080ef1ccSDon Brace 			IOACCEL2_STATUS_SR_IOACCEL_DISABLED)
1743c349775eSScott Teel 			dev->offload_enabled = 0;
1744080ef1ccSDon Brace 		goto retry_cmd;
1745080ef1ccSDon Brace 	}
1746080ef1ccSDon Brace 
1747080ef1ccSDon Brace 	if (handle_ioaccel_mode2_error(h, c, cmd, c2))
1748080ef1ccSDon Brace 		goto retry_cmd;
1749080ef1ccSDon Brace 
1750c349775eSScott Teel 	cmd_free(h, c);
1751c349775eSScott Teel 	cmd->scsi_done(cmd);
1752c349775eSScott Teel 	return;
1753080ef1ccSDon Brace 
1754080ef1ccSDon Brace retry_cmd:
1755080ef1ccSDon Brace 	INIT_WORK(&c->work, hpsa_command_resubmit_worker);
1756080ef1ccSDon Brace 	queue_work_on(raw_smp_processor_id(), h->resubmit_wq, &c->work);
1757c349775eSScott Teel }
1758c349775eSScott Teel 
17591fb011fbSStephen M. Cameron static void complete_scsi_command(struct CommandList *cp)
1760edd16368SStephen M. Cameron {
1761edd16368SStephen M. Cameron 	struct scsi_cmnd *cmd;
1762edd16368SStephen M. Cameron 	struct ctlr_info *h;
1763edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
1764283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
1765edd16368SStephen M. Cameron 
1766edd16368SStephen M. Cameron 	unsigned char sense_key;
1767edd16368SStephen M. Cameron 	unsigned char asc;      /* additional sense code */
1768edd16368SStephen M. Cameron 	unsigned char ascq;     /* additional sense code qualifier */
1769db111e18SStephen M. Cameron 	unsigned long sense_data_size;
1770edd16368SStephen M. Cameron 
1771edd16368SStephen M. Cameron 	ei = cp->err_info;
17727fa3030cSStephen Cameron 	cmd = cp->scsi_cmd;
1773edd16368SStephen M. Cameron 	h = cp->h;
1774283b4a9bSStephen M. Cameron 	dev = cmd->device->hostdata;
1775edd16368SStephen M. Cameron 
1776edd16368SStephen M. Cameron 	scsi_dma_unmap(cmd); /* undo the DMA mappings */
1777e1f7de0cSMatt Gates 	if ((cp->cmd_type == CMD_SCSI) &&
17782b08b3e9SDon Brace 		(le16_to_cpu(cp->Header.SGTotal) > h->max_cmd_sg_entries))
177933a2ffceSStephen M. Cameron 		hpsa_unmap_sg_chain_block(h, cp);
1780edd16368SStephen M. Cameron 
1781edd16368SStephen M. Cameron 	cmd->result = (DID_OK << 16); 		/* host byte */
1782edd16368SStephen M. Cameron 	cmd->result |= (COMMAND_COMPLETE << 8);	/* msg byte */
1783c349775eSScott Teel 
178403383736SDon Brace 	if (cp->cmd_type == CMD_IOACCEL2 || cp->cmd_type == CMD_IOACCEL1)
178503383736SDon Brace 		atomic_dec(&cp->phys_disk->ioaccel_cmds_out);
178603383736SDon Brace 
1787c349775eSScott Teel 	if (cp->cmd_type == CMD_IOACCEL2)
1788c349775eSScott Teel 		return process_ioaccel2_completion(h, cp, cmd, dev);
1789c349775eSScott Teel 
17905512672fSStephen M. Cameron 	cmd->result |= ei->ScsiStatus;
1791edd16368SStephen M. Cameron 
17926aa4c361SRobert Elliott 	scsi_set_resid(cmd, ei->ResidualCnt);
17936aa4c361SRobert Elliott 	if (ei->CommandStatus == 0) {
179403383736SDon Brace 		if (cp->cmd_type == CMD_IOACCEL1)
179503383736SDon Brace 			atomic_dec(&cp->phys_disk->ioaccel_cmds_out);
17966aa4c361SRobert Elliott 		cmd_free(h, cp);
17976aa4c361SRobert Elliott 		cmd->scsi_done(cmd);
17986aa4c361SRobert Elliott 		return;
17996aa4c361SRobert Elliott 	}
18006aa4c361SRobert Elliott 
18016aa4c361SRobert Elliott 	/* copy the sense data */
1802db111e18SStephen M. Cameron 	if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
1803db111e18SStephen M. Cameron 		sense_data_size = SCSI_SENSE_BUFFERSIZE;
1804db111e18SStephen M. Cameron 	else
1805db111e18SStephen M. Cameron 		sense_data_size = sizeof(ei->SenseInfo);
1806db111e18SStephen M. Cameron 	if (ei->SenseLen < sense_data_size)
1807db111e18SStephen M. Cameron 		sense_data_size = ei->SenseLen;
1808db111e18SStephen M. Cameron 
1809db111e18SStephen M. Cameron 	memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
1810edd16368SStephen M. Cameron 
1811e1f7de0cSMatt Gates 	/* For I/O accelerator commands, copy over some fields to the normal
1812e1f7de0cSMatt Gates 	 * CISS header used below for error handling.
1813e1f7de0cSMatt Gates 	 */
1814e1f7de0cSMatt Gates 	if (cp->cmd_type == CMD_IOACCEL1) {
1815e1f7de0cSMatt Gates 		struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
18162b08b3e9SDon Brace 		cp->Header.SGList = scsi_sg_count(cmd);
18172b08b3e9SDon Brace 		cp->Header.SGTotal = cpu_to_le16(cp->Header.SGList);
18182b08b3e9SDon Brace 		cp->Request.CDBLen = le16_to_cpu(c->io_flags) &
18192b08b3e9SDon Brace 			IOACCEL1_IOFLAGS_CDBLEN_MASK;
182050a0decfSStephen M. Cameron 		cp->Header.tag = c->tag;
1821e1f7de0cSMatt Gates 		memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
1822e1f7de0cSMatt Gates 		memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
1823283b4a9bSStephen M. Cameron 
1824283b4a9bSStephen M. Cameron 		/* Any RAID offload error results in retry which will use
1825283b4a9bSStephen M. Cameron 		 * the normal I/O path so the controller can handle whatever's
1826283b4a9bSStephen M. Cameron 		 * wrong.
1827283b4a9bSStephen M. Cameron 		 */
1828283b4a9bSStephen M. Cameron 		if (is_logical_dev_addr_mode(dev->scsi3addr)) {
1829283b4a9bSStephen M. Cameron 			if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
1830283b4a9bSStephen M. Cameron 				dev->offload_enabled = 0;
1831080ef1ccSDon Brace 			INIT_WORK(&cp->work, hpsa_command_resubmit_worker);
1832080ef1ccSDon Brace 			queue_work_on(raw_smp_processor_id(),
1833080ef1ccSDon Brace 					h->resubmit_wq, &cp->work);
1834283b4a9bSStephen M. Cameron 			return;
1835283b4a9bSStephen M. Cameron 		}
1836e1f7de0cSMatt Gates 	}
1837e1f7de0cSMatt Gates 
1838edd16368SStephen M. Cameron 	/* an error has occurred */
1839edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
1840edd16368SStephen M. Cameron 
1841edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
1842edd16368SStephen M. Cameron 		if (ei->ScsiStatus) {
1843edd16368SStephen M. Cameron 			/* Get sense key */
1844edd16368SStephen M. Cameron 			sense_key = 0xf & ei->SenseInfo[2];
1845edd16368SStephen M. Cameron 			/* Get additional sense code */
1846edd16368SStephen M. Cameron 			asc = ei->SenseInfo[12];
1847edd16368SStephen M. Cameron 			/* Get addition sense code qualifier */
1848edd16368SStephen M. Cameron 			ascq = ei->SenseInfo[13];
1849edd16368SStephen M. Cameron 		}
1850edd16368SStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
18511d3b3609SMatt Gates 			if (sense_key == ABORTED_COMMAND) {
18522e311fbaSStephen M. Cameron 				cmd->result |= DID_SOFT_ERROR << 16;
18531d3b3609SMatt Gates 				break;
18541d3b3609SMatt Gates 			}
1855edd16368SStephen M. Cameron 			break;
1856edd16368SStephen M. Cameron 		}
1857edd16368SStephen M. Cameron 		/* Problem was not a check condition
1858edd16368SStephen M. Cameron 		 * Pass it up to the upper layers...
1859edd16368SStephen M. Cameron 		 */
1860edd16368SStephen M. Cameron 		if (ei->ScsiStatus) {
1861edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
1862edd16368SStephen M. Cameron 				"Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1863edd16368SStephen M. Cameron 				"Returning result: 0x%x\n",
1864edd16368SStephen M. Cameron 				cp, ei->ScsiStatus,
1865edd16368SStephen M. Cameron 				sense_key, asc, ascq,
1866edd16368SStephen M. Cameron 				cmd->result);
1867edd16368SStephen M. Cameron 		} else {  /* scsi status is zero??? How??? */
1868edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
1869edd16368SStephen M. Cameron 				"Returning no connection.\n", cp),
1870edd16368SStephen M. Cameron 
1871edd16368SStephen M. Cameron 			/* Ordinarily, this case should never happen,
1872edd16368SStephen M. Cameron 			 * but there is a bug in some released firmware
1873edd16368SStephen M. Cameron 			 * revisions that allows it to happen if, for
1874edd16368SStephen M. Cameron 			 * example, a 4100 backplane loses power and
1875edd16368SStephen M. Cameron 			 * the tape drive is in it.  We assume that
1876edd16368SStephen M. Cameron 			 * it's a fatal error of some kind because we
1877edd16368SStephen M. Cameron 			 * can't show that it wasn't. We will make it
1878edd16368SStephen M. Cameron 			 * look like selection timeout since that is
1879edd16368SStephen M. Cameron 			 * the most common reason for this to occur,
1880edd16368SStephen M. Cameron 			 * and it's severe enough.
1881edd16368SStephen M. Cameron 			 */
1882edd16368SStephen M. Cameron 
1883edd16368SStephen M. Cameron 			cmd->result = DID_NO_CONNECT << 16;
1884edd16368SStephen M. Cameron 		}
1885edd16368SStephen M. Cameron 		break;
1886edd16368SStephen M. Cameron 
1887edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1888edd16368SStephen M. Cameron 		break;
1889edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
1890f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev,
1891f42e81e1SStephen Cameron 			"CDB %16phN data overrun\n", cp->Request.CDB);
1892edd16368SStephen M. Cameron 		break;
1893edd16368SStephen M. Cameron 	case CMD_INVALID: {
1894edd16368SStephen M. Cameron 		/* print_bytes(cp, sizeof(*cp), 1, 0);
1895edd16368SStephen M. Cameron 		print_cmd(cp); */
1896edd16368SStephen M. Cameron 		/* We get CMD_INVALID if you address a non-existent device
1897edd16368SStephen M. Cameron 		 * instead of a selection timeout (no response).  You will
1898edd16368SStephen M. Cameron 		 * see this if you yank out a drive, then try to access it.
1899edd16368SStephen M. Cameron 		 * This is kind of a shame because it means that any other
1900edd16368SStephen M. Cameron 		 * CMD_INVALID (e.g. driver bug) will get interpreted as a
1901edd16368SStephen M. Cameron 		 * missing target. */
1902edd16368SStephen M. Cameron 		cmd->result = DID_NO_CONNECT << 16;
1903edd16368SStephen M. Cameron 	}
1904edd16368SStephen M. Cameron 		break;
1905edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
1906256d0eaaSStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1907f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : protocol error\n",
1908f42e81e1SStephen Cameron 				cp->Request.CDB);
1909edd16368SStephen M. Cameron 		break;
1910edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
1911edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1912f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : hardware error\n",
1913f42e81e1SStephen Cameron 			cp->Request.CDB);
1914edd16368SStephen M. Cameron 		break;
1915edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
1916edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1917f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : connection lost\n",
1918f42e81e1SStephen Cameron 			cp->Request.CDB);
1919edd16368SStephen M. Cameron 		break;
1920edd16368SStephen M. Cameron 	case CMD_ABORTED:
1921edd16368SStephen M. Cameron 		cmd->result = DID_ABORT << 16;
1922f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN was aborted with status 0x%x\n",
1923f42e81e1SStephen Cameron 				cp->Request.CDB, ei->ScsiStatus);
1924edd16368SStephen M. Cameron 		break;
1925edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
1926edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1927f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : abort failed\n",
1928f42e81e1SStephen Cameron 			cp->Request.CDB);
1929edd16368SStephen M. Cameron 		break;
1930edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
1931f6e76055SStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
1932f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : unsolicited abort\n",
1933f42e81e1SStephen Cameron 			cp->Request.CDB);
1934edd16368SStephen M. Cameron 		break;
1935edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
1936edd16368SStephen M. Cameron 		cmd->result = DID_TIME_OUT << 16;
1937f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN timed out\n",
1938f42e81e1SStephen Cameron 			cp->Request.CDB);
1939edd16368SStephen M. Cameron 		break;
19401d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
19411d5e2ed0SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
19421d5e2ed0SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Command unabortable\n");
19431d5e2ed0SStephen M. Cameron 		break;
1944283b4a9bSStephen M. Cameron 	case CMD_IOACCEL_DISABLED:
1945283b4a9bSStephen M. Cameron 		/* This only handles the direct pass-through case since RAID
1946283b4a9bSStephen M. Cameron 		 * offload is handled above.  Just attempt a retry.
1947283b4a9bSStephen M. Cameron 		 */
1948283b4a9bSStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16;
1949283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev,
1950283b4a9bSStephen M. Cameron 				"cp %p had HP SSD Smart Path error\n", cp);
1951283b4a9bSStephen M. Cameron 		break;
1952edd16368SStephen M. Cameron 	default:
1953edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1954edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
1955edd16368SStephen M. Cameron 				cp, ei->CommandStatus);
1956edd16368SStephen M. Cameron 	}
1957edd16368SStephen M. Cameron 	cmd_free(h, cp);
19582cc5bfafSTomas Henzl 	cmd->scsi_done(cmd);
1959edd16368SStephen M. Cameron }
1960edd16368SStephen M. Cameron 
1961edd16368SStephen M. Cameron static void hpsa_pci_unmap(struct pci_dev *pdev,
1962edd16368SStephen M. Cameron 	struct CommandList *c, int sg_used, int data_direction)
1963edd16368SStephen M. Cameron {
1964edd16368SStephen M. Cameron 	int i;
1965edd16368SStephen M. Cameron 
196650a0decfSStephen M. Cameron 	for (i = 0; i < sg_used; i++)
196750a0decfSStephen M. Cameron 		pci_unmap_single(pdev, (dma_addr_t) le64_to_cpu(c->SG[i].Addr),
196850a0decfSStephen M. Cameron 				le32_to_cpu(c->SG[i].Len),
1969edd16368SStephen M. Cameron 				data_direction);
1970edd16368SStephen M. Cameron }
1971edd16368SStephen M. Cameron 
1972a2dac136SStephen M. Cameron static int hpsa_map_one(struct pci_dev *pdev,
1973edd16368SStephen M. Cameron 		struct CommandList *cp,
1974edd16368SStephen M. Cameron 		unsigned char *buf,
1975edd16368SStephen M. Cameron 		size_t buflen,
1976edd16368SStephen M. Cameron 		int data_direction)
1977edd16368SStephen M. Cameron {
197801a02ffcSStephen M. Cameron 	u64 addr64;
1979edd16368SStephen M. Cameron 
1980edd16368SStephen M. Cameron 	if (buflen == 0 || data_direction == PCI_DMA_NONE) {
1981edd16368SStephen M. Cameron 		cp->Header.SGList = 0;
198250a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(0);
1983a2dac136SStephen M. Cameron 		return 0;
1984edd16368SStephen M. Cameron 	}
1985edd16368SStephen M. Cameron 
198650a0decfSStephen M. Cameron 	addr64 = pci_map_single(pdev, buf, buflen, data_direction);
1987eceaae18SShuah Khan 	if (dma_mapping_error(&pdev->dev, addr64)) {
1988a2dac136SStephen M. Cameron 		/* Prevent subsequent unmap of something never mapped */
1989eceaae18SShuah Khan 		cp->Header.SGList = 0;
199050a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(0);
1991a2dac136SStephen M. Cameron 		return -1;
1992eceaae18SShuah Khan 	}
199350a0decfSStephen M. Cameron 	cp->SG[0].Addr = cpu_to_le64(addr64);
199450a0decfSStephen M. Cameron 	cp->SG[0].Len = cpu_to_le32(buflen);
199550a0decfSStephen M. Cameron 	cp->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* we are not chaining */
199650a0decfSStephen M. Cameron 	cp->Header.SGList = 1;   /* no. SGs contig in this cmd */
199750a0decfSStephen M. Cameron 	cp->Header.SGTotal = cpu_to_le16(1); /* total sgs in cmd list */
1998a2dac136SStephen M. Cameron 	return 0;
1999edd16368SStephen M. Cameron }
2000edd16368SStephen M. Cameron 
2001edd16368SStephen M. Cameron static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
2002edd16368SStephen M. Cameron 	struct CommandList *c)
2003edd16368SStephen M. Cameron {
2004edd16368SStephen M. Cameron 	DECLARE_COMPLETION_ONSTACK(wait);
2005edd16368SStephen M. Cameron 
2006edd16368SStephen M. Cameron 	c->waiting = &wait;
2007edd16368SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
2008edd16368SStephen M. Cameron 	wait_for_completion(&wait);
2009edd16368SStephen M. Cameron }
2010edd16368SStephen M. Cameron 
2011094963daSStephen M. Cameron static u32 lockup_detected(struct ctlr_info *h)
2012094963daSStephen M. Cameron {
2013094963daSStephen M. Cameron 	int cpu;
2014094963daSStephen M. Cameron 	u32 rc, *lockup_detected;
2015094963daSStephen M. Cameron 
2016094963daSStephen M. Cameron 	cpu = get_cpu();
2017094963daSStephen M. Cameron 	lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
2018094963daSStephen M. Cameron 	rc = *lockup_detected;
2019094963daSStephen M. Cameron 	put_cpu();
2020094963daSStephen M. Cameron 	return rc;
2021094963daSStephen M. Cameron }
2022094963daSStephen M. Cameron 
2023a0c12413SStephen M. Cameron static void hpsa_scsi_do_simple_cmd_core_if_no_lockup(struct ctlr_info *h,
2024a0c12413SStephen M. Cameron 	struct CommandList *c)
2025a0c12413SStephen M. Cameron {
2026a0c12413SStephen M. Cameron 	/* If controller lockup detected, fake a hardware error. */
2027094963daSStephen M. Cameron 	if (unlikely(lockup_detected(h)))
2028a0c12413SStephen M. Cameron 		c->err_info->CommandStatus = CMD_HARDWARE_ERR;
2029094963daSStephen M. Cameron 	else
2030a0c12413SStephen M. Cameron 		hpsa_scsi_do_simple_cmd_core(h, c);
2031a0c12413SStephen M. Cameron }
2032a0c12413SStephen M. Cameron 
20339c2fc160SStephen M. Cameron #define MAX_DRIVER_CMD_RETRIES 25
2034edd16368SStephen M. Cameron static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
2035edd16368SStephen M. Cameron 	struct CommandList *c, int data_direction)
2036edd16368SStephen M. Cameron {
20379c2fc160SStephen M. Cameron 	int backoff_time = 10, retry_count = 0;
2038edd16368SStephen M. Cameron 
2039edd16368SStephen M. Cameron 	do {
20407630abd0SJoe Perches 		memset(c->err_info, 0, sizeof(*c->err_info));
2041edd16368SStephen M. Cameron 		hpsa_scsi_do_simple_cmd_core(h, c);
2042edd16368SStephen M. Cameron 		retry_count++;
20439c2fc160SStephen M. Cameron 		if (retry_count > 3) {
20449c2fc160SStephen M. Cameron 			msleep(backoff_time);
20459c2fc160SStephen M. Cameron 			if (backoff_time < 1000)
20469c2fc160SStephen M. Cameron 				backoff_time *= 2;
20479c2fc160SStephen M. Cameron 		}
2048852af20aSMatt Bondurant 	} while ((check_for_unit_attention(h, c) ||
20499c2fc160SStephen M. Cameron 			check_for_busy(h, c)) &&
20509c2fc160SStephen M. Cameron 			retry_count <= MAX_DRIVER_CMD_RETRIES);
2051edd16368SStephen M. Cameron 	hpsa_pci_unmap(h->pdev, c, 1, data_direction);
2052edd16368SStephen M. Cameron }
2053edd16368SStephen M. Cameron 
2054d1e8beacSStephen M. Cameron static void hpsa_print_cmd(struct ctlr_info *h, char *txt,
2055d1e8beacSStephen M. Cameron 				struct CommandList *c)
2056edd16368SStephen M. Cameron {
2057d1e8beacSStephen M. Cameron 	const u8 *cdb = c->Request.CDB;
2058d1e8beacSStephen M. Cameron 	const u8 *lun = c->Header.LUN.LunAddrBytes;
2059edd16368SStephen M. Cameron 
2060d1e8beacSStephen M. Cameron 	dev_warn(&h->pdev->dev, "%s: LUN:%02x%02x%02x%02x%02x%02x%02x%02x"
2061d1e8beacSStephen M. Cameron 	" CDB:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
2062d1e8beacSStephen M. Cameron 		txt, lun[0], lun[1], lun[2], lun[3],
2063d1e8beacSStephen M. Cameron 		lun[4], lun[5], lun[6], lun[7],
2064d1e8beacSStephen M. Cameron 		cdb[0], cdb[1], cdb[2], cdb[3],
2065d1e8beacSStephen M. Cameron 		cdb[4], cdb[5], cdb[6], cdb[7],
2066d1e8beacSStephen M. Cameron 		cdb[8], cdb[9], cdb[10], cdb[11],
2067d1e8beacSStephen M. Cameron 		cdb[12], cdb[13], cdb[14], cdb[15]);
2068d1e8beacSStephen M. Cameron }
2069d1e8beacSStephen M. Cameron 
2070d1e8beacSStephen M. Cameron static void hpsa_scsi_interpret_error(struct ctlr_info *h,
2071d1e8beacSStephen M. Cameron 			struct CommandList *cp)
2072d1e8beacSStephen M. Cameron {
2073d1e8beacSStephen M. Cameron 	const struct ErrorInfo *ei = cp->err_info;
2074d1e8beacSStephen M. Cameron 	struct device *d = &cp->h->pdev->dev;
2075d1e8beacSStephen M. Cameron 	const u8 *sd = ei->SenseInfo;
2076d1e8beacSStephen M. Cameron 
2077edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
2078edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
2079d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "SCSI status", cp);
2080d1e8beacSStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION)
2081d1e8beacSStephen M. Cameron 			dev_warn(d, "SCSI Status = 02, Sense key = %02x, ASC = %02x, ASCQ = %02x\n",
2082d1e8beacSStephen M. Cameron 				sd[2] & 0x0f, sd[12], sd[13]);
2083d1e8beacSStephen M. Cameron 		else
2084d1e8beacSStephen M. Cameron 			dev_warn(d, "SCSI Status = %02x\n", ei->ScsiStatus);
2085edd16368SStephen M. Cameron 		if (ei->ScsiStatus == 0)
2086edd16368SStephen M. Cameron 			dev_warn(d, "SCSI status is abnormally zero.  "
2087edd16368SStephen M. Cameron 			"(probably indicates selection timeout "
2088edd16368SStephen M. Cameron 			"reported incorrectly due to a known "
2089edd16368SStephen M. Cameron 			"firmware bug, circa July, 2001.)\n");
2090edd16368SStephen M. Cameron 		break;
2091edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
2092edd16368SStephen M. Cameron 		break;
2093edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
2094d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "overrun condition", cp);
2095edd16368SStephen M. Cameron 		break;
2096edd16368SStephen M. Cameron 	case CMD_INVALID: {
2097edd16368SStephen M. Cameron 		/* controller unfortunately reports SCSI passthru's
2098edd16368SStephen M. Cameron 		 * to non-existent targets as invalid commands.
2099edd16368SStephen M. Cameron 		 */
2100d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "invalid command", cp);
2101d1e8beacSStephen M. Cameron 		dev_warn(d, "probably means device no longer present\n");
2102edd16368SStephen M. Cameron 		}
2103edd16368SStephen M. Cameron 		break;
2104edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
2105d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "protocol error", cp);
2106edd16368SStephen M. Cameron 		break;
2107edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
2108d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "hardware error", cp);
2109edd16368SStephen M. Cameron 		break;
2110edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
2111d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "connection lost", cp);
2112edd16368SStephen M. Cameron 		break;
2113edd16368SStephen M. Cameron 	case CMD_ABORTED:
2114d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "aborted", cp);
2115edd16368SStephen M. Cameron 		break;
2116edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
2117d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "abort failed", cp);
2118edd16368SStephen M. Cameron 		break;
2119edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
2120d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unsolicited abort", cp);
2121edd16368SStephen M. Cameron 		break;
2122edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
2123d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "timed out", cp);
2124edd16368SStephen M. Cameron 		break;
21251d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
2126d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unabortable", cp);
21271d5e2ed0SStephen M. Cameron 		break;
2128edd16368SStephen M. Cameron 	default:
2129d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unknown status", cp);
2130d1e8beacSStephen M. Cameron 		dev_warn(d, "Unknown command status %x\n",
2131edd16368SStephen M. Cameron 				ei->CommandStatus);
2132edd16368SStephen M. Cameron 	}
2133edd16368SStephen M. Cameron }
2134edd16368SStephen M. Cameron 
2135edd16368SStephen M. Cameron static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
2136b7bb24ebSStephen M. Cameron 			u16 page, unsigned char *buf,
2137edd16368SStephen M. Cameron 			unsigned char bufsize)
2138edd16368SStephen M. Cameron {
2139edd16368SStephen M. Cameron 	int rc = IO_OK;
2140edd16368SStephen M. Cameron 	struct CommandList *c;
2141edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2142edd16368SStephen M. Cameron 
214345fcb86eSStephen Cameron 	c = cmd_alloc(h);
2144edd16368SStephen M. Cameron 
2145574f05d3SStephen Cameron 	if (c == NULL) {
214645fcb86eSStephen Cameron 		dev_warn(&h->pdev->dev, "cmd_alloc returned NULL!\n");
2147ecd9aad4SStephen M. Cameron 		return -ENOMEM;
2148edd16368SStephen M. Cameron 	}
2149edd16368SStephen M. Cameron 
2150a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
2151a2dac136SStephen M. Cameron 			page, scsi3addr, TYPE_CMD)) {
2152a2dac136SStephen M. Cameron 		rc = -1;
2153a2dac136SStephen M. Cameron 		goto out;
2154a2dac136SStephen M. Cameron 	}
2155edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2156edd16368SStephen M. Cameron 	ei = c->err_info;
2157edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2158d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2159edd16368SStephen M. Cameron 		rc = -1;
2160edd16368SStephen M. Cameron 	}
2161a2dac136SStephen M. Cameron out:
216245fcb86eSStephen Cameron 	cmd_free(h, c);
2163edd16368SStephen M. Cameron 	return rc;
2164edd16368SStephen M. Cameron }
2165edd16368SStephen M. Cameron 
2166316b221aSStephen M. Cameron static int hpsa_bmic_ctrl_mode_sense(struct ctlr_info *h,
2167316b221aSStephen M. Cameron 		unsigned char *scsi3addr, unsigned char page,
2168316b221aSStephen M. Cameron 		struct bmic_controller_parameters *buf, size_t bufsize)
2169316b221aSStephen M. Cameron {
2170316b221aSStephen M. Cameron 	int rc = IO_OK;
2171316b221aSStephen M. Cameron 	struct CommandList *c;
2172316b221aSStephen M. Cameron 	struct ErrorInfo *ei;
2173316b221aSStephen M. Cameron 
217445fcb86eSStephen Cameron 	c = cmd_alloc(h);
2175316b221aSStephen M. Cameron 	if (c == NULL) {			/* trouble... */
217645fcb86eSStephen Cameron 		dev_warn(&h->pdev->dev, "cmd_alloc returned NULL!\n");
2177316b221aSStephen M. Cameron 		return -ENOMEM;
2178316b221aSStephen M. Cameron 	}
2179316b221aSStephen M. Cameron 
2180316b221aSStephen M. Cameron 	if (fill_cmd(c, BMIC_SENSE_CONTROLLER_PARAMETERS, h, buf, bufsize,
2181316b221aSStephen M. Cameron 			page, scsi3addr, TYPE_CMD)) {
2182316b221aSStephen M. Cameron 		rc = -1;
2183316b221aSStephen M. Cameron 		goto out;
2184316b221aSStephen M. Cameron 	}
2185316b221aSStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2186316b221aSStephen M. Cameron 	ei = c->err_info;
2187316b221aSStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2188316b221aSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2189316b221aSStephen M. Cameron 		rc = -1;
2190316b221aSStephen M. Cameron 	}
2191316b221aSStephen M. Cameron out:
219245fcb86eSStephen Cameron 	cmd_free(h, c);
2193316b221aSStephen M. Cameron 	return rc;
2194316b221aSStephen M. Cameron 	}
2195316b221aSStephen M. Cameron 
2196bf711ac6SScott Teel static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
2197bf711ac6SScott Teel 	u8 reset_type)
2198edd16368SStephen M. Cameron {
2199edd16368SStephen M. Cameron 	int rc = IO_OK;
2200edd16368SStephen M. Cameron 	struct CommandList *c;
2201edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2202edd16368SStephen M. Cameron 
220345fcb86eSStephen Cameron 	c = cmd_alloc(h);
2204edd16368SStephen M. Cameron 
2205edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
220645fcb86eSStephen Cameron 		dev_warn(&h->pdev->dev, "cmd_alloc returned NULL!\n");
2207e9ea04a6SStephen M. Cameron 		return -ENOMEM;
2208edd16368SStephen M. Cameron 	}
2209edd16368SStephen M. Cameron 
2210a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map. */
2211bf711ac6SScott Teel 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
2212bf711ac6SScott Teel 			scsi3addr, TYPE_MSG);
2213bf711ac6SScott Teel 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to LUN reset */
2214edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core(h, c);
2215edd16368SStephen M. Cameron 	/* no unmap needed here because no data xfer. */
2216edd16368SStephen M. Cameron 
2217edd16368SStephen M. Cameron 	ei = c->err_info;
2218edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0) {
2219d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2220edd16368SStephen M. Cameron 		rc = -1;
2221edd16368SStephen M. Cameron 	}
222245fcb86eSStephen Cameron 	cmd_free(h, c);
2223edd16368SStephen M. Cameron 	return rc;
2224edd16368SStephen M. Cameron }
2225edd16368SStephen M. Cameron 
2226edd16368SStephen M. Cameron static void hpsa_get_raid_level(struct ctlr_info *h,
2227edd16368SStephen M. Cameron 	unsigned char *scsi3addr, unsigned char *raid_level)
2228edd16368SStephen M. Cameron {
2229edd16368SStephen M. Cameron 	int rc;
2230edd16368SStephen M. Cameron 	unsigned char *buf;
2231edd16368SStephen M. Cameron 
2232edd16368SStephen M. Cameron 	*raid_level = RAID_UNKNOWN;
2233edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2234edd16368SStephen M. Cameron 	if (!buf)
2235edd16368SStephen M. Cameron 		return;
2236b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0xC1, buf, 64);
2237edd16368SStephen M. Cameron 	if (rc == 0)
2238edd16368SStephen M. Cameron 		*raid_level = buf[8];
2239edd16368SStephen M. Cameron 	if (*raid_level > RAID_UNKNOWN)
2240edd16368SStephen M. Cameron 		*raid_level = RAID_UNKNOWN;
2241edd16368SStephen M. Cameron 	kfree(buf);
2242edd16368SStephen M. Cameron 	return;
2243edd16368SStephen M. Cameron }
2244edd16368SStephen M. Cameron 
2245283b4a9bSStephen M. Cameron #define HPSA_MAP_DEBUG
2246283b4a9bSStephen M. Cameron #ifdef HPSA_MAP_DEBUG
2247283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
2248283b4a9bSStephen M. Cameron 				struct raid_map_data *map_buff)
2249283b4a9bSStephen M. Cameron {
2250283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map_buff->data[0];
2251283b4a9bSStephen M. Cameron 	int map, row, col;
2252283b4a9bSStephen M. Cameron 	u16 map_cnt, row_cnt, disks_per_row;
2253283b4a9bSStephen M. Cameron 
2254283b4a9bSStephen M. Cameron 	if (rc != 0)
2255283b4a9bSStephen M. Cameron 		return;
2256283b4a9bSStephen M. Cameron 
22572ba8bfc8SStephen M. Cameron 	/* Show details only if debugging has been activated. */
22582ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug < 2)
22592ba8bfc8SStephen M. Cameron 		return;
22602ba8bfc8SStephen M. Cameron 
2261283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "structure_size = %u\n",
2262283b4a9bSStephen M. Cameron 				le32_to_cpu(map_buff->structure_size));
2263283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
2264283b4a9bSStephen M. Cameron 			le32_to_cpu(map_buff->volume_blk_size));
2265283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
2266283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->volume_blk_cnt));
2267283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
2268283b4a9bSStephen M. Cameron 			map_buff->phys_blk_shift);
2269283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
2270283b4a9bSStephen M. Cameron 			map_buff->parity_rotation_shift);
2271283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "strip_size = %u\n",
2272283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->strip_size));
2273283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
2274283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_starting_blk));
2275283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
2276283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_blk_cnt));
2277283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
2278283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->data_disks_per_row));
2279283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
2280283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->metadata_disks_per_row));
2281283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "row_cnt = %u\n",
2282283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->row_cnt));
2283283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "layout_map_count = %u\n",
2284283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->layout_map_count));
22852b08b3e9SDon Brace 	dev_info(&h->pdev->dev, "flags = 0x%x\n",
2286dd0e19f3SScott Teel 			le16_to_cpu(map_buff->flags));
22872b08b3e9SDon Brace 	dev_info(&h->pdev->dev, "encrypytion = %s\n",
22882b08b3e9SDon Brace 			le16_to_cpu(map_buff->flags) &
22892b08b3e9SDon Brace 			RAID_MAP_FLAG_ENCRYPT_ON ?  "ON" : "OFF");
2290dd0e19f3SScott Teel 	dev_info(&h->pdev->dev, "dekindex = %u\n",
2291dd0e19f3SScott Teel 			le16_to_cpu(map_buff->dekindex));
2292283b4a9bSStephen M. Cameron 	map_cnt = le16_to_cpu(map_buff->layout_map_count);
2293283b4a9bSStephen M. Cameron 	for (map = 0; map < map_cnt; map++) {
2294283b4a9bSStephen M. Cameron 		dev_info(&h->pdev->dev, "Map%u:\n", map);
2295283b4a9bSStephen M. Cameron 		row_cnt = le16_to_cpu(map_buff->row_cnt);
2296283b4a9bSStephen M. Cameron 		for (row = 0; row < row_cnt; row++) {
2297283b4a9bSStephen M. Cameron 			dev_info(&h->pdev->dev, "  Row%u:\n", row);
2298283b4a9bSStephen M. Cameron 			disks_per_row =
2299283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->data_disks_per_row);
2300283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2301283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2302283b4a9bSStephen M. Cameron 					"    D%02u: h=0x%04x xor=%u,%u\n",
2303283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2304283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2305283b4a9bSStephen M. Cameron 			disks_per_row =
2306283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->metadata_disks_per_row);
2307283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2308283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2309283b4a9bSStephen M. Cameron 					"    M%02u: h=0x%04x xor=%u,%u\n",
2310283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2311283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2312283b4a9bSStephen M. Cameron 		}
2313283b4a9bSStephen M. Cameron 	}
2314283b4a9bSStephen M. Cameron }
2315283b4a9bSStephen M. Cameron #else
2316283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
2317283b4a9bSStephen M. Cameron 			__attribute__((unused)) int rc,
2318283b4a9bSStephen M. Cameron 			__attribute__((unused)) struct raid_map_data *map_buff)
2319283b4a9bSStephen M. Cameron {
2320283b4a9bSStephen M. Cameron }
2321283b4a9bSStephen M. Cameron #endif
2322283b4a9bSStephen M. Cameron 
2323283b4a9bSStephen M. Cameron static int hpsa_get_raid_map(struct ctlr_info *h,
2324283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2325283b4a9bSStephen M. Cameron {
2326283b4a9bSStephen M. Cameron 	int rc = 0;
2327283b4a9bSStephen M. Cameron 	struct CommandList *c;
2328283b4a9bSStephen M. Cameron 	struct ErrorInfo *ei;
2329283b4a9bSStephen M. Cameron 
233045fcb86eSStephen Cameron 	c = cmd_alloc(h);
2331283b4a9bSStephen M. Cameron 	if (c == NULL) {
233245fcb86eSStephen Cameron 		dev_warn(&h->pdev->dev, "cmd_alloc returned NULL!\n");
2333283b4a9bSStephen M. Cameron 		return -ENOMEM;
2334283b4a9bSStephen M. Cameron 	}
2335283b4a9bSStephen M. Cameron 	if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
2336283b4a9bSStephen M. Cameron 			sizeof(this_device->raid_map), 0,
2337283b4a9bSStephen M. Cameron 			scsi3addr, TYPE_CMD)) {
2338283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "Out of memory in hpsa_get_raid_map()\n");
233945fcb86eSStephen Cameron 		cmd_free(h, c);
2340283b4a9bSStephen M. Cameron 		return -ENOMEM;
2341283b4a9bSStephen M. Cameron 	}
2342283b4a9bSStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2343283b4a9bSStephen M. Cameron 	ei = c->err_info;
2344283b4a9bSStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2345d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
234645fcb86eSStephen Cameron 		cmd_free(h, c);
2347283b4a9bSStephen M. Cameron 		return -1;
2348283b4a9bSStephen M. Cameron 	}
234945fcb86eSStephen Cameron 	cmd_free(h, c);
2350283b4a9bSStephen M. Cameron 
2351283b4a9bSStephen M. Cameron 	/* @todo in the future, dynamically allocate RAID map memory */
2352283b4a9bSStephen M. Cameron 	if (le32_to_cpu(this_device->raid_map.structure_size) >
2353283b4a9bSStephen M. Cameron 				sizeof(this_device->raid_map)) {
2354283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
2355283b4a9bSStephen M. Cameron 		rc = -1;
2356283b4a9bSStephen M. Cameron 	}
2357283b4a9bSStephen M. Cameron 	hpsa_debug_map_buff(h, rc, &this_device->raid_map);
2358283b4a9bSStephen M. Cameron 	return rc;
2359283b4a9bSStephen M. Cameron }
2360283b4a9bSStephen M. Cameron 
236103383736SDon Brace static int hpsa_bmic_id_physical_device(struct ctlr_info *h,
236203383736SDon Brace 		unsigned char scsi3addr[], u16 bmic_device_index,
236303383736SDon Brace 		struct bmic_identify_physical_device *buf, size_t bufsize)
236403383736SDon Brace {
236503383736SDon Brace 	int rc = IO_OK;
236603383736SDon Brace 	struct CommandList *c;
236703383736SDon Brace 	struct ErrorInfo *ei;
236803383736SDon Brace 
236903383736SDon Brace 	c = cmd_alloc(h);
237003383736SDon Brace 	rc = fill_cmd(c, BMIC_IDENTIFY_PHYSICAL_DEVICE, h, buf, bufsize,
237103383736SDon Brace 		0, RAID_CTLR_LUNID, TYPE_CMD);
237203383736SDon Brace 	if (rc)
237303383736SDon Brace 		goto out;
237403383736SDon Brace 
237503383736SDon Brace 	c->Request.CDB[2] = bmic_device_index & 0xff;
237603383736SDon Brace 	c->Request.CDB[9] = (bmic_device_index >> 8) & 0xff;
237703383736SDon Brace 
237803383736SDon Brace 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
237903383736SDon Brace 	ei = c->err_info;
238003383736SDon Brace 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
238103383736SDon Brace 		hpsa_scsi_interpret_error(h, c);
238203383736SDon Brace 		rc = -1;
238303383736SDon Brace 	}
238403383736SDon Brace out:
238503383736SDon Brace 	cmd_free(h, c);
238603383736SDon Brace 	return rc;
238703383736SDon Brace }
238803383736SDon Brace 
23891b70150aSStephen M. Cameron static int hpsa_vpd_page_supported(struct ctlr_info *h,
23901b70150aSStephen M. Cameron 	unsigned char scsi3addr[], u8 page)
23911b70150aSStephen M. Cameron {
23921b70150aSStephen M. Cameron 	int rc;
23931b70150aSStephen M. Cameron 	int i;
23941b70150aSStephen M. Cameron 	int pages;
23951b70150aSStephen M. Cameron 	unsigned char *buf, bufsize;
23961b70150aSStephen M. Cameron 
23971b70150aSStephen M. Cameron 	buf = kzalloc(256, GFP_KERNEL);
23981b70150aSStephen M. Cameron 	if (!buf)
23991b70150aSStephen M. Cameron 		return 0;
24001b70150aSStephen M. Cameron 
24011b70150aSStephen M. Cameron 	/* Get the size of the page list first */
24021b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
24031b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
24041b70150aSStephen M. Cameron 				buf, HPSA_VPD_HEADER_SZ);
24051b70150aSStephen M. Cameron 	if (rc != 0)
24061b70150aSStephen M. Cameron 		goto exit_unsupported;
24071b70150aSStephen M. Cameron 	pages = buf[3];
24081b70150aSStephen M. Cameron 	if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
24091b70150aSStephen M. Cameron 		bufsize = pages + HPSA_VPD_HEADER_SZ;
24101b70150aSStephen M. Cameron 	else
24111b70150aSStephen M. Cameron 		bufsize = 255;
24121b70150aSStephen M. Cameron 
24131b70150aSStephen M. Cameron 	/* Get the whole VPD page list */
24141b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
24151b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
24161b70150aSStephen M. Cameron 				buf, bufsize);
24171b70150aSStephen M. Cameron 	if (rc != 0)
24181b70150aSStephen M. Cameron 		goto exit_unsupported;
24191b70150aSStephen M. Cameron 
24201b70150aSStephen M. Cameron 	pages = buf[3];
24211b70150aSStephen M. Cameron 	for (i = 1; i <= pages; i++)
24221b70150aSStephen M. Cameron 		if (buf[3 + i] == page)
24231b70150aSStephen M. Cameron 			goto exit_supported;
24241b70150aSStephen M. Cameron exit_unsupported:
24251b70150aSStephen M. Cameron 	kfree(buf);
24261b70150aSStephen M. Cameron 	return 0;
24271b70150aSStephen M. Cameron exit_supported:
24281b70150aSStephen M. Cameron 	kfree(buf);
24291b70150aSStephen M. Cameron 	return 1;
24301b70150aSStephen M. Cameron }
24311b70150aSStephen M. Cameron 
2432283b4a9bSStephen M. Cameron static void hpsa_get_ioaccel_status(struct ctlr_info *h,
2433283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2434283b4a9bSStephen M. Cameron {
2435283b4a9bSStephen M. Cameron 	int rc;
2436283b4a9bSStephen M. Cameron 	unsigned char *buf;
2437283b4a9bSStephen M. Cameron 	u8 ioaccel_status;
2438283b4a9bSStephen M. Cameron 
2439283b4a9bSStephen M. Cameron 	this_device->offload_config = 0;
2440283b4a9bSStephen M. Cameron 	this_device->offload_enabled = 0;
2441283b4a9bSStephen M. Cameron 
2442283b4a9bSStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2443283b4a9bSStephen M. Cameron 	if (!buf)
2444283b4a9bSStephen M. Cameron 		return;
24451b70150aSStephen M. Cameron 	if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
24461b70150aSStephen M. Cameron 		goto out;
2447283b4a9bSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
2448b7bb24ebSStephen M. Cameron 			VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
2449283b4a9bSStephen M. Cameron 	if (rc != 0)
2450283b4a9bSStephen M. Cameron 		goto out;
2451283b4a9bSStephen M. Cameron 
2452283b4a9bSStephen M. Cameron #define IOACCEL_STATUS_BYTE 4
2453283b4a9bSStephen M. Cameron #define OFFLOAD_CONFIGURED_BIT 0x01
2454283b4a9bSStephen M. Cameron #define OFFLOAD_ENABLED_BIT 0x02
2455283b4a9bSStephen M. Cameron 	ioaccel_status = buf[IOACCEL_STATUS_BYTE];
2456283b4a9bSStephen M. Cameron 	this_device->offload_config =
2457283b4a9bSStephen M. Cameron 		!!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
2458283b4a9bSStephen M. Cameron 	if (this_device->offload_config) {
2459283b4a9bSStephen M. Cameron 		this_device->offload_enabled =
2460283b4a9bSStephen M. Cameron 			!!(ioaccel_status & OFFLOAD_ENABLED_BIT);
2461283b4a9bSStephen M. Cameron 		if (hpsa_get_raid_map(h, scsi3addr, this_device))
2462283b4a9bSStephen M. Cameron 			this_device->offload_enabled = 0;
2463283b4a9bSStephen M. Cameron 	}
2464283b4a9bSStephen M. Cameron out:
2465283b4a9bSStephen M. Cameron 	kfree(buf);
2466283b4a9bSStephen M. Cameron 	return;
2467283b4a9bSStephen M. Cameron }
2468283b4a9bSStephen M. Cameron 
2469edd16368SStephen M. Cameron /* Get the device id from inquiry page 0x83 */
2470edd16368SStephen M. Cameron static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
2471edd16368SStephen M. Cameron 	unsigned char *device_id, int buflen)
2472edd16368SStephen M. Cameron {
2473edd16368SStephen M. Cameron 	int rc;
2474edd16368SStephen M. Cameron 	unsigned char *buf;
2475edd16368SStephen M. Cameron 
2476edd16368SStephen M. Cameron 	if (buflen > 16)
2477edd16368SStephen M. Cameron 		buflen = 16;
2478edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2479edd16368SStephen M. Cameron 	if (!buf)
2480a84d794dSStephen M. Cameron 		return -ENOMEM;
2481b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0x83, buf, 64);
2482edd16368SStephen M. Cameron 	if (rc == 0)
2483edd16368SStephen M. Cameron 		memcpy(device_id, &buf[8], buflen);
2484edd16368SStephen M. Cameron 	kfree(buf);
2485edd16368SStephen M. Cameron 	return rc != 0;
2486edd16368SStephen M. Cameron }
2487edd16368SStephen M. Cameron 
2488edd16368SStephen M. Cameron static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
248903383736SDon Brace 		void *buf, int bufsize,
2490edd16368SStephen M. Cameron 		int extended_response)
2491edd16368SStephen M. Cameron {
2492edd16368SStephen M. Cameron 	int rc = IO_OK;
2493edd16368SStephen M. Cameron 	struct CommandList *c;
2494edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
2495edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2496edd16368SStephen M. Cameron 
249745fcb86eSStephen Cameron 	c = cmd_alloc(h);
2498edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
249945fcb86eSStephen Cameron 		dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
2500edd16368SStephen M. Cameron 		return -1;
2501edd16368SStephen M. Cameron 	}
2502e89c0ae7SStephen M. Cameron 	/* address the controller */
2503e89c0ae7SStephen M. Cameron 	memset(scsi3addr, 0, sizeof(scsi3addr));
2504a2dac136SStephen M. Cameron 	if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
2505a2dac136SStephen M. Cameron 		buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
2506a2dac136SStephen M. Cameron 		rc = -1;
2507a2dac136SStephen M. Cameron 		goto out;
2508a2dac136SStephen M. Cameron 	}
2509edd16368SStephen M. Cameron 	if (extended_response)
2510edd16368SStephen M. Cameron 		c->Request.CDB[1] = extended_response;
2511edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2512edd16368SStephen M. Cameron 	ei = c->err_info;
2513edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 &&
2514edd16368SStephen M. Cameron 	    ei->CommandStatus != CMD_DATA_UNDERRUN) {
2515d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2516edd16368SStephen M. Cameron 		rc = -1;
2517283b4a9bSStephen M. Cameron 	} else {
251803383736SDon Brace 		struct ReportLUNdata *rld = buf;
251903383736SDon Brace 
252003383736SDon Brace 		if (rld->extended_response_flag != extended_response) {
2521283b4a9bSStephen M. Cameron 			dev_err(&h->pdev->dev,
2522283b4a9bSStephen M. Cameron 				"report luns requested format %u, got %u\n",
2523283b4a9bSStephen M. Cameron 				extended_response,
252403383736SDon Brace 				rld->extended_response_flag);
2525283b4a9bSStephen M. Cameron 			rc = -1;
2526283b4a9bSStephen M. Cameron 		}
2527edd16368SStephen M. Cameron 	}
2528a2dac136SStephen M. Cameron out:
252945fcb86eSStephen Cameron 	cmd_free(h, c);
2530edd16368SStephen M. Cameron 	return rc;
2531edd16368SStephen M. Cameron }
2532edd16368SStephen M. Cameron 
2533edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
253403383736SDon Brace 		struct ReportExtendedLUNdata *buf, int bufsize)
2535edd16368SStephen M. Cameron {
253603383736SDon Brace 	return hpsa_scsi_do_report_luns(h, 0, buf, bufsize,
253703383736SDon Brace 						HPSA_REPORT_PHYS_EXTENDED);
2538edd16368SStephen M. Cameron }
2539edd16368SStephen M. Cameron 
2540edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
2541edd16368SStephen M. Cameron 		struct ReportLUNdata *buf, int bufsize)
2542edd16368SStephen M. Cameron {
2543edd16368SStephen M. Cameron 	return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
2544edd16368SStephen M. Cameron }
2545edd16368SStephen M. Cameron 
2546edd16368SStephen M. Cameron static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
2547edd16368SStephen M. Cameron 	int bus, int target, int lun)
2548edd16368SStephen M. Cameron {
2549edd16368SStephen M. Cameron 	device->bus = bus;
2550edd16368SStephen M. Cameron 	device->target = target;
2551edd16368SStephen M. Cameron 	device->lun = lun;
2552edd16368SStephen M. Cameron }
2553edd16368SStephen M. Cameron 
25549846590eSStephen M. Cameron /* Use VPD inquiry to get details of volume status */
25559846590eSStephen M. Cameron static int hpsa_get_volume_status(struct ctlr_info *h,
25569846590eSStephen M. Cameron 					unsigned char scsi3addr[])
25579846590eSStephen M. Cameron {
25589846590eSStephen M. Cameron 	int rc;
25599846590eSStephen M. Cameron 	int status;
25609846590eSStephen M. Cameron 	int size;
25619846590eSStephen M. Cameron 	unsigned char *buf;
25629846590eSStephen M. Cameron 
25639846590eSStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
25649846590eSStephen M. Cameron 	if (!buf)
25659846590eSStephen M. Cameron 		return HPSA_VPD_LV_STATUS_UNSUPPORTED;
25669846590eSStephen M. Cameron 
25679846590eSStephen M. Cameron 	/* Does controller have VPD for logical volume status? */
256824a4b078SStephen M. Cameron 	if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_STATUS))
25699846590eSStephen M. Cameron 		goto exit_failed;
25709846590eSStephen M. Cameron 
25719846590eSStephen M. Cameron 	/* Get the size of the VPD return buffer */
25729846590eSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
25739846590eSStephen M. Cameron 					buf, HPSA_VPD_HEADER_SZ);
257424a4b078SStephen M. Cameron 	if (rc != 0)
25759846590eSStephen M. Cameron 		goto exit_failed;
25769846590eSStephen M. Cameron 	size = buf[3];
25779846590eSStephen M. Cameron 
25789846590eSStephen M. Cameron 	/* Now get the whole VPD buffer */
25799846590eSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
25809846590eSStephen M. Cameron 					buf, size + HPSA_VPD_HEADER_SZ);
258124a4b078SStephen M. Cameron 	if (rc != 0)
25829846590eSStephen M. Cameron 		goto exit_failed;
25839846590eSStephen M. Cameron 	status = buf[4]; /* status byte */
25849846590eSStephen M. Cameron 
25859846590eSStephen M. Cameron 	kfree(buf);
25869846590eSStephen M. Cameron 	return status;
25879846590eSStephen M. Cameron exit_failed:
25889846590eSStephen M. Cameron 	kfree(buf);
25899846590eSStephen M. Cameron 	return HPSA_VPD_LV_STATUS_UNSUPPORTED;
25909846590eSStephen M. Cameron }
25919846590eSStephen M. Cameron 
25929846590eSStephen M. Cameron /* Determine offline status of a volume.
25939846590eSStephen M. Cameron  * Return either:
25949846590eSStephen M. Cameron  *  0 (not offline)
259567955ba3SStephen M. Cameron  *  0xff (offline for unknown reasons)
25969846590eSStephen M. Cameron  *  # (integer code indicating one of several NOT READY states
25979846590eSStephen M. Cameron  *     describing why a volume is to be kept offline)
25989846590eSStephen M. Cameron  */
259967955ba3SStephen M. Cameron static int hpsa_volume_offline(struct ctlr_info *h,
26009846590eSStephen M. Cameron 					unsigned char scsi3addr[])
26019846590eSStephen M. Cameron {
26029846590eSStephen M. Cameron 	struct CommandList *c;
26039846590eSStephen M. Cameron 	unsigned char *sense, sense_key, asc, ascq;
26049846590eSStephen M. Cameron 	int ldstat = 0;
26059846590eSStephen M. Cameron 	u16 cmd_status;
26069846590eSStephen M. Cameron 	u8 scsi_status;
26079846590eSStephen M. Cameron #define ASC_LUN_NOT_READY 0x04
26089846590eSStephen M. Cameron #define ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS 0x04
26099846590eSStephen M. Cameron #define ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ 0x02
26109846590eSStephen M. Cameron 
26119846590eSStephen M. Cameron 	c = cmd_alloc(h);
26129846590eSStephen M. Cameron 	if (!c)
26139846590eSStephen M. Cameron 		return 0;
26149846590eSStephen M. Cameron 	(void) fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, scsi3addr, TYPE_CMD);
26159846590eSStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core(h, c);
26169846590eSStephen M. Cameron 	sense = c->err_info->SenseInfo;
26179846590eSStephen M. Cameron 	sense_key = sense[2];
26189846590eSStephen M. Cameron 	asc = sense[12];
26199846590eSStephen M. Cameron 	ascq = sense[13];
26209846590eSStephen M. Cameron 	cmd_status = c->err_info->CommandStatus;
26219846590eSStephen M. Cameron 	scsi_status = c->err_info->ScsiStatus;
26229846590eSStephen M. Cameron 	cmd_free(h, c);
26239846590eSStephen M. Cameron 	/* Is the volume 'not ready'? */
26249846590eSStephen M. Cameron 	if (cmd_status != CMD_TARGET_STATUS ||
26259846590eSStephen M. Cameron 		scsi_status != SAM_STAT_CHECK_CONDITION ||
26269846590eSStephen M. Cameron 		sense_key != NOT_READY ||
26279846590eSStephen M. Cameron 		asc != ASC_LUN_NOT_READY)  {
26289846590eSStephen M. Cameron 		return 0;
26299846590eSStephen M. Cameron 	}
26309846590eSStephen M. Cameron 
26319846590eSStephen M. Cameron 	/* Determine the reason for not ready state */
26329846590eSStephen M. Cameron 	ldstat = hpsa_get_volume_status(h, scsi3addr);
26339846590eSStephen M. Cameron 
26349846590eSStephen M. Cameron 	/* Keep volume offline in certain cases: */
26359846590eSStephen M. Cameron 	switch (ldstat) {
26369846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ERASE:
26379846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_RPI:
26389846590eSStephen M. Cameron 	case HPSA_LV_PENDING_RPI:
26399846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_NO_KEY:
26409846590eSStephen M. Cameron 	case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
26419846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION:
26429846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
26439846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
26449846590eSStephen M. Cameron 		return ldstat;
26459846590eSStephen M. Cameron 	case HPSA_VPD_LV_STATUS_UNSUPPORTED:
26469846590eSStephen M. Cameron 		/* If VPD status page isn't available,
26479846590eSStephen M. Cameron 		 * use ASC/ASCQ to determine state
26489846590eSStephen M. Cameron 		 */
26499846590eSStephen M. Cameron 		if ((ascq == ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS) ||
26509846590eSStephen M. Cameron 			(ascq == ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ))
26519846590eSStephen M. Cameron 			return ldstat;
26529846590eSStephen M. Cameron 		break;
26539846590eSStephen M. Cameron 	default:
26549846590eSStephen M. Cameron 		break;
26559846590eSStephen M. Cameron 	}
26569846590eSStephen M. Cameron 	return 0;
26579846590eSStephen M. Cameron }
26589846590eSStephen M. Cameron 
2659edd16368SStephen M. Cameron static int hpsa_update_device_info(struct ctlr_info *h,
26600b0e1d6cSStephen M. Cameron 	unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
26610b0e1d6cSStephen M. Cameron 	unsigned char *is_OBDR_device)
2662edd16368SStephen M. Cameron {
26630b0e1d6cSStephen M. Cameron 
26640b0e1d6cSStephen M. Cameron #define OBDR_SIG_OFFSET 43
26650b0e1d6cSStephen M. Cameron #define OBDR_TAPE_SIG "$DR-10"
26660b0e1d6cSStephen M. Cameron #define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
26670b0e1d6cSStephen M. Cameron #define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
26680b0e1d6cSStephen M. Cameron 
2669ea6d3bc3SStephen M. Cameron 	unsigned char *inq_buff;
26700b0e1d6cSStephen M. Cameron 	unsigned char *obdr_sig;
2671edd16368SStephen M. Cameron 
2672ea6d3bc3SStephen M. Cameron 	inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
2673edd16368SStephen M. Cameron 	if (!inq_buff)
2674edd16368SStephen M. Cameron 		goto bail_out;
2675edd16368SStephen M. Cameron 
2676edd16368SStephen M. Cameron 	/* Do an inquiry to the device to see what it is. */
2677edd16368SStephen M. Cameron 	if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
2678edd16368SStephen M. Cameron 		(unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
2679edd16368SStephen M. Cameron 		/* Inquiry failed (msg printed already) */
2680edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev,
2681edd16368SStephen M. Cameron 			"hpsa_update_device_info: inquiry failed\n");
2682edd16368SStephen M. Cameron 		goto bail_out;
2683edd16368SStephen M. Cameron 	}
2684edd16368SStephen M. Cameron 
2685edd16368SStephen M. Cameron 	this_device->devtype = (inq_buff[0] & 0x1f);
2686edd16368SStephen M. Cameron 	memcpy(this_device->scsi3addr, scsi3addr, 8);
2687edd16368SStephen M. Cameron 	memcpy(this_device->vendor, &inq_buff[8],
2688edd16368SStephen M. Cameron 		sizeof(this_device->vendor));
2689edd16368SStephen M. Cameron 	memcpy(this_device->model, &inq_buff[16],
2690edd16368SStephen M. Cameron 		sizeof(this_device->model));
2691edd16368SStephen M. Cameron 	memset(this_device->device_id, 0,
2692edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
2693edd16368SStephen M. Cameron 	hpsa_get_device_id(h, scsi3addr, this_device->device_id,
2694edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
2695edd16368SStephen M. Cameron 
2696edd16368SStephen M. Cameron 	if (this_device->devtype == TYPE_DISK &&
2697283b4a9bSStephen M. Cameron 		is_logical_dev_addr_mode(scsi3addr)) {
269867955ba3SStephen M. Cameron 		int volume_offline;
269967955ba3SStephen M. Cameron 
2700edd16368SStephen M. Cameron 		hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
2701283b4a9bSStephen M. Cameron 		if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
2702283b4a9bSStephen M. Cameron 			hpsa_get_ioaccel_status(h, scsi3addr, this_device);
270367955ba3SStephen M. Cameron 		volume_offline = hpsa_volume_offline(h, scsi3addr);
270467955ba3SStephen M. Cameron 		if (volume_offline < 0 || volume_offline > 0xff)
270567955ba3SStephen M. Cameron 			volume_offline = HPSA_VPD_LV_STATUS_UNSUPPORTED;
270667955ba3SStephen M. Cameron 		this_device->volume_offline = volume_offline & 0xff;
2707283b4a9bSStephen M. Cameron 	} else {
2708edd16368SStephen M. Cameron 		this_device->raid_level = RAID_UNKNOWN;
2709283b4a9bSStephen M. Cameron 		this_device->offload_config = 0;
2710283b4a9bSStephen M. Cameron 		this_device->offload_enabled = 0;
27119846590eSStephen M. Cameron 		this_device->volume_offline = 0;
271203383736SDon Brace 		this_device->queue_depth = h->nr_cmds;
2713283b4a9bSStephen M. Cameron 	}
2714edd16368SStephen M. Cameron 
27150b0e1d6cSStephen M. Cameron 	if (is_OBDR_device) {
27160b0e1d6cSStephen M. Cameron 		/* See if this is a One-Button-Disaster-Recovery device
27170b0e1d6cSStephen M. Cameron 		 * by looking for "$DR-10" at offset 43 in inquiry data.
27180b0e1d6cSStephen M. Cameron 		 */
27190b0e1d6cSStephen M. Cameron 		obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
27200b0e1d6cSStephen M. Cameron 		*is_OBDR_device = (this_device->devtype == TYPE_ROM &&
27210b0e1d6cSStephen M. Cameron 					strncmp(obdr_sig, OBDR_TAPE_SIG,
27220b0e1d6cSStephen M. Cameron 						OBDR_SIG_LEN) == 0);
27230b0e1d6cSStephen M. Cameron 	}
27240b0e1d6cSStephen M. Cameron 
2725edd16368SStephen M. Cameron 	kfree(inq_buff);
2726edd16368SStephen M. Cameron 	return 0;
2727edd16368SStephen M. Cameron 
2728edd16368SStephen M. Cameron bail_out:
2729edd16368SStephen M. Cameron 	kfree(inq_buff);
2730edd16368SStephen M. Cameron 	return 1;
2731edd16368SStephen M. Cameron }
2732edd16368SStephen M. Cameron 
27334f4eb9f1SScott Teel static unsigned char *ext_target_model[] = {
2734edd16368SStephen M. Cameron 	"MSA2012",
2735edd16368SStephen M. Cameron 	"MSA2024",
2736edd16368SStephen M. Cameron 	"MSA2312",
2737edd16368SStephen M. Cameron 	"MSA2324",
2738fda38518SStephen M. Cameron 	"P2000 G3 SAS",
2739e06c8e5cSStephen M. Cameron 	"MSA 2040 SAS",
2740edd16368SStephen M. Cameron 	NULL,
2741edd16368SStephen M. Cameron };
2742edd16368SStephen M. Cameron 
27434f4eb9f1SScott Teel static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
2744edd16368SStephen M. Cameron {
2745edd16368SStephen M. Cameron 	int i;
2746edd16368SStephen M. Cameron 
27474f4eb9f1SScott Teel 	for (i = 0; ext_target_model[i]; i++)
27484f4eb9f1SScott Teel 		if (strncmp(device->model, ext_target_model[i],
27494f4eb9f1SScott Teel 			strlen(ext_target_model[i])) == 0)
2750edd16368SStephen M. Cameron 			return 1;
2751edd16368SStephen M. Cameron 	return 0;
2752edd16368SStephen M. Cameron }
2753edd16368SStephen M. Cameron 
2754edd16368SStephen M. Cameron /* Helper function to assign bus, target, lun mapping of devices.
27554f4eb9f1SScott Teel  * Puts non-external target logical volumes on bus 0, external target logical
2756edd16368SStephen M. Cameron  * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
2757edd16368SStephen M. Cameron  * Logical drive target and lun are assigned at this time, but
2758edd16368SStephen M. Cameron  * physical device lun and target assignment are deferred (assigned
2759edd16368SStephen M. Cameron  * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
2760edd16368SStephen M. Cameron  */
2761edd16368SStephen M. Cameron static void figure_bus_target_lun(struct ctlr_info *h,
27621f310bdeSStephen M. Cameron 	u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
2763edd16368SStephen M. Cameron {
27641f310bdeSStephen M. Cameron 	u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
2765edd16368SStephen M. Cameron 
27661f310bdeSStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes)) {
27671f310bdeSStephen M. Cameron 		/* physical device, target and lun filled in later */
27681f310bdeSStephen M. Cameron 		if (is_hba_lunid(lunaddrbytes))
27691f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
27701f310bdeSStephen M. Cameron 		else
27711f310bdeSStephen M. Cameron 			/* defer target, lun assignment for physical devices */
27721f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 2, -1, -1);
27731f310bdeSStephen M. Cameron 		return;
27741f310bdeSStephen M. Cameron 	}
27751f310bdeSStephen M. Cameron 	/* It's a logical device */
27764f4eb9f1SScott Teel 	if (is_ext_target(h, device)) {
27774f4eb9f1SScott Teel 		/* external target way, put logicals on bus 1
2778339b2b14SStephen M. Cameron 		 * and match target/lun numbers box
27791f310bdeSStephen M. Cameron 		 * reports, other smart array, bus 0, target 0, match lunid
2780339b2b14SStephen M. Cameron 		 */
27811f310bdeSStephen M. Cameron 		hpsa_set_bus_target_lun(device,
27821f310bdeSStephen M. Cameron 			1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
27831f310bdeSStephen M. Cameron 		return;
2784339b2b14SStephen M. Cameron 	}
27851f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
2786edd16368SStephen M. Cameron }
2787edd16368SStephen M. Cameron 
2788edd16368SStephen M. Cameron /*
2789edd16368SStephen M. Cameron  * If there is no lun 0 on a target, linux won't find any devices.
27904f4eb9f1SScott Teel  * For the external targets (arrays), we have to manually detect the enclosure
2791edd16368SStephen M. Cameron  * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
2792edd16368SStephen M. Cameron  * it for some reason.  *tmpdevice is the target we're adding,
2793edd16368SStephen M. Cameron  * this_device is a pointer into the current element of currentsd[]
2794edd16368SStephen M. Cameron  * that we're building up in update_scsi_devices(), below.
2795edd16368SStephen M. Cameron  * lunzerobits is a bitmap that tracks which targets already have a
2796edd16368SStephen M. Cameron  * lun 0 assigned.
2797edd16368SStephen M. Cameron  * Returns 1 if an enclosure was added, 0 if not.
2798edd16368SStephen M. Cameron  */
27994f4eb9f1SScott Teel static int add_ext_target_dev(struct ctlr_info *h,
2800edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *tmpdevice,
280101a02ffcSStephen M. Cameron 	struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
28024f4eb9f1SScott Teel 	unsigned long lunzerobits[], int *n_ext_target_devs)
2803edd16368SStephen M. Cameron {
2804edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
2805edd16368SStephen M. Cameron 
28061f310bdeSStephen M. Cameron 	if (test_bit(tmpdevice->target, lunzerobits))
2807edd16368SStephen M. Cameron 		return 0; /* There is already a lun 0 on this target. */
2808edd16368SStephen M. Cameron 
2809edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes))
2810edd16368SStephen M. Cameron 		return 0; /* It's the logical targets that may lack lun 0. */
2811edd16368SStephen M. Cameron 
28124f4eb9f1SScott Teel 	if (!is_ext_target(h, tmpdevice))
28134f4eb9f1SScott Teel 		return 0; /* Only external target devices have this problem. */
2814edd16368SStephen M. Cameron 
28151f310bdeSStephen M. Cameron 	if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
2816edd16368SStephen M. Cameron 		return 0;
2817edd16368SStephen M. Cameron 
2818c4f8a299SStephen M. Cameron 	memset(scsi3addr, 0, 8);
28191f310bdeSStephen M. Cameron 	scsi3addr[3] = tmpdevice->target;
2820edd16368SStephen M. Cameron 	if (is_hba_lunid(scsi3addr))
2821edd16368SStephen M. Cameron 		return 0; /* Don't add the RAID controller here. */
2822edd16368SStephen M. Cameron 
2823339b2b14SStephen M. Cameron 	if (is_scsi_rev_5(h))
2824339b2b14SStephen M. Cameron 		return 0; /* p1210m doesn't need to do this. */
2825339b2b14SStephen M. Cameron 
28264f4eb9f1SScott Teel 	if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
2827aca4a520SScott Teel 		dev_warn(&h->pdev->dev, "Maximum number of external "
2828aca4a520SScott Teel 			"target devices exceeded.  Check your hardware "
2829edd16368SStephen M. Cameron 			"configuration.");
2830edd16368SStephen M. Cameron 		return 0;
2831edd16368SStephen M. Cameron 	}
2832edd16368SStephen M. Cameron 
28330b0e1d6cSStephen M. Cameron 	if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
2834edd16368SStephen M. Cameron 		return 0;
28354f4eb9f1SScott Teel 	(*n_ext_target_devs)++;
28361f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(this_device,
28371f310bdeSStephen M. Cameron 				tmpdevice->bus, tmpdevice->target, 0);
28381f310bdeSStephen M. Cameron 	set_bit(tmpdevice->target, lunzerobits);
2839edd16368SStephen M. Cameron 	return 1;
2840edd16368SStephen M. Cameron }
2841edd16368SStephen M. Cameron 
2842edd16368SStephen M. Cameron /*
284354b6e9e9SScott Teel  * Get address of physical disk used for an ioaccel2 mode command:
284454b6e9e9SScott Teel  *	1. Extract ioaccel2 handle from the command.
284554b6e9e9SScott Teel  *	2. Find a matching ioaccel2 handle from list of physical disks.
284654b6e9e9SScott Teel  *	3. Return:
284754b6e9e9SScott Teel  *		1 and set scsi3addr to address of matching physical
284854b6e9e9SScott Teel  *		0 if no matching physical disk was found.
284954b6e9e9SScott Teel  */
285054b6e9e9SScott Teel static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
285154b6e9e9SScott Teel 	struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
285254b6e9e9SScott Teel {
285354b6e9e9SScott Teel 	struct ReportExtendedLUNdata *physicals = NULL;
285454b6e9e9SScott Teel 	int responsesize = 24;	/* size of physical extended response */
285554b6e9e9SScott Teel 	int reportsize = sizeof(*physicals) + HPSA_MAX_PHYS_LUN * responsesize;
285654b6e9e9SScott Teel 	u32 nphysicals = 0;	/* number of reported physical devs */
285754b6e9e9SScott Teel 	int found = 0;		/* found match (1) or not (0) */
285854b6e9e9SScott Teel 	u32 find;		/* handle we need to match */
285954b6e9e9SScott Teel 	int i;
286054b6e9e9SScott Teel 	struct scsi_cmnd *scmd;	/* scsi command within request being aborted */
286154b6e9e9SScott Teel 	struct hpsa_scsi_dev_t *d; /* device of request being aborted */
286254b6e9e9SScott Teel 	struct io_accel2_cmd *c2a; /* ioaccel2 command to abort */
28632b08b3e9SDon Brace 	__le32 it_nexus;	/* 4 byte device handle for the ioaccel2 cmd */
28642b08b3e9SDon Brace 	__le32 scsi_nexus;	/* 4 byte device handle for the ioaccel2 cmd */
286554b6e9e9SScott Teel 
286654b6e9e9SScott Teel 	if (ioaccel2_cmd_to_abort->cmd_type != CMD_IOACCEL2)
286754b6e9e9SScott Teel 		return 0; /* no match */
286854b6e9e9SScott Teel 
286954b6e9e9SScott Teel 	/* point to the ioaccel2 device handle */
287054b6e9e9SScott Teel 	c2a = &h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
287154b6e9e9SScott Teel 	if (c2a == NULL)
287254b6e9e9SScott Teel 		return 0; /* no match */
287354b6e9e9SScott Teel 
287454b6e9e9SScott Teel 	scmd = (struct scsi_cmnd *) ioaccel2_cmd_to_abort->scsi_cmd;
287554b6e9e9SScott Teel 	if (scmd == NULL)
287654b6e9e9SScott Teel 		return 0; /* no match */
287754b6e9e9SScott Teel 
287854b6e9e9SScott Teel 	d = scmd->device->hostdata;
287954b6e9e9SScott Teel 	if (d == NULL)
288054b6e9e9SScott Teel 		return 0; /* no match */
288154b6e9e9SScott Teel 
288250a0decfSStephen M. Cameron 	it_nexus = cpu_to_le32(d->ioaccel_handle);
28832b08b3e9SDon Brace 	scsi_nexus = c2a->scsi_nexus;
28842b08b3e9SDon Brace 	find = le32_to_cpu(c2a->scsi_nexus);
288554b6e9e9SScott Teel 
28862ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
28872ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
28882ba8bfc8SStephen M. Cameron 			"%s: scsi_nexus:0x%08x device id: 0x%02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
28892ba8bfc8SStephen M. Cameron 			__func__, scsi_nexus,
28902ba8bfc8SStephen M. Cameron 			d->device_id[0], d->device_id[1], d->device_id[2],
28912ba8bfc8SStephen M. Cameron 			d->device_id[3], d->device_id[4], d->device_id[5],
28922ba8bfc8SStephen M. Cameron 			d->device_id[6], d->device_id[7], d->device_id[8],
28932ba8bfc8SStephen M. Cameron 			d->device_id[9], d->device_id[10], d->device_id[11],
28942ba8bfc8SStephen M. Cameron 			d->device_id[12], d->device_id[13], d->device_id[14],
28952ba8bfc8SStephen M. Cameron 			d->device_id[15]);
28962ba8bfc8SStephen M. Cameron 
289754b6e9e9SScott Teel 	/* Get the list of physical devices */
289854b6e9e9SScott Teel 	physicals = kzalloc(reportsize, GFP_KERNEL);
28993b51a7a3SJoe Handzik 	if (physicals == NULL)
29003b51a7a3SJoe Handzik 		return 0;
290103383736SDon Brace 	if (hpsa_scsi_do_report_phys_luns(h, physicals, reportsize)) {
290254b6e9e9SScott Teel 		dev_err(&h->pdev->dev,
290354b6e9e9SScott Teel 			"Can't lookup %s device handle: report physical LUNs failed.\n",
290454b6e9e9SScott Teel 			"HP SSD Smart Path");
290554b6e9e9SScott Teel 		kfree(physicals);
290654b6e9e9SScott Teel 		return 0;
290754b6e9e9SScott Teel 	}
290854b6e9e9SScott Teel 	nphysicals = be32_to_cpu(*((__be32 *)physicals->LUNListLength)) /
290954b6e9e9SScott Teel 							responsesize;
291054b6e9e9SScott Teel 
291154b6e9e9SScott Teel 	/* find ioaccel2 handle in list of physicals: */
291254b6e9e9SScott Teel 	for (i = 0; i < nphysicals; i++) {
2913d5b5d964SStephen M. Cameron 		struct ext_report_lun_entry *entry = &physicals->LUN[i];
2914d5b5d964SStephen M. Cameron 
291554b6e9e9SScott Teel 		/* handle is in bytes 28-31 of each lun */
2916d5b5d964SStephen M. Cameron 		if (entry->ioaccel_handle != find)
291754b6e9e9SScott Teel 			continue; /* didn't match */
291854b6e9e9SScott Teel 		found = 1;
2919d5b5d964SStephen M. Cameron 		memcpy(scsi3addr, entry->lunid, 8);
29202ba8bfc8SStephen M. Cameron 		if (h->raid_offload_debug > 0)
29212ba8bfc8SStephen M. Cameron 			dev_info(&h->pdev->dev,
2922d5b5d964SStephen M. Cameron 				"%s: Searched h=0x%08x, Found h=0x%08x, scsiaddr 0x%8phN\n",
29232ba8bfc8SStephen M. Cameron 				__func__, find,
2924d5b5d964SStephen M. Cameron 				entry->ioaccel_handle, scsi3addr);
292554b6e9e9SScott Teel 		break; /* found it */
292654b6e9e9SScott Teel 	}
292754b6e9e9SScott Teel 
292854b6e9e9SScott Teel 	kfree(physicals);
292954b6e9e9SScott Teel 	if (found)
293054b6e9e9SScott Teel 		return 1;
293154b6e9e9SScott Teel 	else
293254b6e9e9SScott Teel 		return 0;
293354b6e9e9SScott Teel 
293454b6e9e9SScott Teel }
293554b6e9e9SScott Teel /*
2936edd16368SStephen M. Cameron  * Do CISS_REPORT_PHYS and CISS_REPORT_LOG.  Data is returned in physdev,
2937edd16368SStephen M. Cameron  * logdev.  The number of luns in physdev and logdev are returned in
2938edd16368SStephen M. Cameron  * *nphysicals and *nlogicals, respectively.
2939edd16368SStephen M. Cameron  * Returns 0 on success, -1 otherwise.
2940edd16368SStephen M. Cameron  */
2941edd16368SStephen M. Cameron static int hpsa_gather_lun_info(struct ctlr_info *h,
294203383736SDon Brace 	struct ReportExtendedLUNdata *physdev, u32 *nphysicals,
294301a02ffcSStephen M. Cameron 	struct ReportLUNdata *logdev, u32 *nlogicals)
2944edd16368SStephen M. Cameron {
294503383736SDon Brace 	if (hpsa_scsi_do_report_phys_luns(h, physdev, sizeof(*physdev))) {
2946edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
2947edd16368SStephen M. Cameron 		return -1;
2948edd16368SStephen M. Cameron 	}
294903383736SDon Brace 	*nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 24;
2950edd16368SStephen M. Cameron 	if (*nphysicals > HPSA_MAX_PHYS_LUN) {
295103383736SDon Brace 		dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded. %d LUNs ignored.\n",
295203383736SDon Brace 			HPSA_MAX_PHYS_LUN, *nphysicals - HPSA_MAX_PHYS_LUN);
2953edd16368SStephen M. Cameron 		*nphysicals = HPSA_MAX_PHYS_LUN;
2954edd16368SStephen M. Cameron 	}
295503383736SDon Brace 	if (hpsa_scsi_do_report_log_luns(h, logdev, sizeof(*logdev))) {
2956edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
2957edd16368SStephen M. Cameron 		return -1;
2958edd16368SStephen M. Cameron 	}
29596df1e954SStephen M. Cameron 	*nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
2960edd16368SStephen M. Cameron 	/* Reject Logicals in excess of our max capability. */
2961edd16368SStephen M. Cameron 	if (*nlogicals > HPSA_MAX_LUN) {
2962edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
2963edd16368SStephen M. Cameron 			"maximum logical LUNs (%d) exceeded.  "
2964edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_LUN,
2965edd16368SStephen M. Cameron 			*nlogicals - HPSA_MAX_LUN);
2966edd16368SStephen M. Cameron 			*nlogicals = HPSA_MAX_LUN;
2967edd16368SStephen M. Cameron 	}
2968edd16368SStephen M. Cameron 	if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
2969edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
2970edd16368SStephen M. Cameron 			"maximum logical + physical LUNs (%d) exceeded. "
2971edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2972edd16368SStephen M. Cameron 			*nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
2973edd16368SStephen M. Cameron 		*nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
2974edd16368SStephen M. Cameron 	}
2975edd16368SStephen M. Cameron 	return 0;
2976edd16368SStephen M. Cameron }
2977edd16368SStephen M. Cameron 
297842a91641SDon Brace static u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position,
297942a91641SDon Brace 	int i, int nphysicals, int nlogicals,
2980a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list,
2981339b2b14SStephen M. Cameron 	struct ReportLUNdata *logdev_list)
2982339b2b14SStephen M. Cameron {
2983339b2b14SStephen M. Cameron 	/* Helper function, figure out where the LUN ID info is coming from
2984339b2b14SStephen M. Cameron 	 * given index i, lists of physical and logical devices, where in
2985339b2b14SStephen M. Cameron 	 * the list the raid controller is supposed to appear (first or last)
2986339b2b14SStephen M. Cameron 	 */
2987339b2b14SStephen M. Cameron 
2988339b2b14SStephen M. Cameron 	int logicals_start = nphysicals + (raid_ctlr_position == 0);
2989339b2b14SStephen M. Cameron 	int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
2990339b2b14SStephen M. Cameron 
2991339b2b14SStephen M. Cameron 	if (i == raid_ctlr_position)
2992339b2b14SStephen M. Cameron 		return RAID_CTLR_LUNID;
2993339b2b14SStephen M. Cameron 
2994339b2b14SStephen M. Cameron 	if (i < logicals_start)
2995d5b5d964SStephen M. Cameron 		return &physdev_list->LUN[i -
2996d5b5d964SStephen M. Cameron 				(raid_ctlr_position == 0)].lunid[0];
2997339b2b14SStephen M. Cameron 
2998339b2b14SStephen M. Cameron 	if (i < last_device)
2999339b2b14SStephen M. Cameron 		return &logdev_list->LUN[i - nphysicals -
3000339b2b14SStephen M. Cameron 			(raid_ctlr_position == 0)][0];
3001339b2b14SStephen M. Cameron 	BUG();
3002339b2b14SStephen M. Cameron 	return NULL;
3003339b2b14SStephen M. Cameron }
3004339b2b14SStephen M. Cameron 
3005316b221aSStephen M. Cameron static int hpsa_hba_mode_enabled(struct ctlr_info *h)
3006316b221aSStephen M. Cameron {
3007316b221aSStephen M. Cameron 	int rc;
30086e8e8088SJoe Handzik 	int hba_mode_enabled;
3009316b221aSStephen M. Cameron 	struct bmic_controller_parameters *ctlr_params;
3010316b221aSStephen M. Cameron 	ctlr_params = kzalloc(sizeof(struct bmic_controller_parameters),
3011316b221aSStephen M. Cameron 		GFP_KERNEL);
3012316b221aSStephen M. Cameron 
3013316b221aSStephen M. Cameron 	if (!ctlr_params)
301496444fbbSJoe Handzik 		return -ENOMEM;
3015316b221aSStephen M. Cameron 	rc = hpsa_bmic_ctrl_mode_sense(h, RAID_CTLR_LUNID, 0, ctlr_params,
3016316b221aSStephen M. Cameron 		sizeof(struct bmic_controller_parameters));
301796444fbbSJoe Handzik 	if (rc) {
3018316b221aSStephen M. Cameron 		kfree(ctlr_params);
301996444fbbSJoe Handzik 		return rc;
3020316b221aSStephen M. Cameron 	}
30216e8e8088SJoe Handzik 
30226e8e8088SJoe Handzik 	hba_mode_enabled =
30236e8e8088SJoe Handzik 		((ctlr_params->nvram_flags & HBA_MODE_ENABLED_FLAG) != 0);
30246e8e8088SJoe Handzik 	kfree(ctlr_params);
30256e8e8088SJoe Handzik 	return hba_mode_enabled;
3026316b221aSStephen M. Cameron }
3027316b221aSStephen M. Cameron 
302803383736SDon Brace /* get physical drive ioaccel handle and queue depth */
302903383736SDon Brace static void hpsa_get_ioaccel_drive_info(struct ctlr_info *h,
303003383736SDon Brace 		struct hpsa_scsi_dev_t *dev,
303103383736SDon Brace 		u8 *lunaddrbytes,
303203383736SDon Brace 		struct bmic_identify_physical_device *id_phys)
303303383736SDon Brace {
303403383736SDon Brace 	int rc;
303503383736SDon Brace 	struct ext_report_lun_entry *rle =
303603383736SDon Brace 		(struct ext_report_lun_entry *) lunaddrbytes;
303703383736SDon Brace 
303803383736SDon Brace 	dev->ioaccel_handle = rle->ioaccel_handle;
303903383736SDon Brace 	memset(id_phys, 0, sizeof(*id_phys));
304003383736SDon Brace 	rc = hpsa_bmic_id_physical_device(h, lunaddrbytes,
304103383736SDon Brace 			GET_BMIC_DRIVE_NUMBER(lunaddrbytes), id_phys,
304203383736SDon Brace 			sizeof(*id_phys));
304303383736SDon Brace 	if (!rc)
304403383736SDon Brace 		/* Reserve space for FW operations */
304503383736SDon Brace #define DRIVE_CMDS_RESERVED_FOR_FW 2
304603383736SDon Brace #define DRIVE_QUEUE_DEPTH 7
304703383736SDon Brace 		dev->queue_depth =
304803383736SDon Brace 			le16_to_cpu(id_phys->current_queue_depth_limit) -
304903383736SDon Brace 				DRIVE_CMDS_RESERVED_FOR_FW;
305003383736SDon Brace 	else
305103383736SDon Brace 		dev->queue_depth = DRIVE_QUEUE_DEPTH; /* conservative */
305203383736SDon Brace 	atomic_set(&dev->ioaccel_cmds_out, 0);
305303383736SDon Brace }
305403383736SDon Brace 
3055edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
3056edd16368SStephen M. Cameron {
3057edd16368SStephen M. Cameron 	/* the idea here is we could get notified
3058edd16368SStephen M. Cameron 	 * that some devices have changed, so we do a report
3059edd16368SStephen M. Cameron 	 * physical luns and report logical luns cmd, and adjust
3060edd16368SStephen M. Cameron 	 * our list of devices accordingly.
3061edd16368SStephen M. Cameron 	 *
3062edd16368SStephen M. Cameron 	 * The scsi3addr's of devices won't change so long as the
3063edd16368SStephen M. Cameron 	 * adapter is not reset.  That means we can rescan and
3064edd16368SStephen M. Cameron 	 * tell which devices we already know about, vs. new
3065edd16368SStephen M. Cameron 	 * devices, vs.  disappearing devices.
3066edd16368SStephen M. Cameron 	 */
3067a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list = NULL;
3068edd16368SStephen M. Cameron 	struct ReportLUNdata *logdev_list = NULL;
306903383736SDon Brace 	struct bmic_identify_physical_device *id_phys = NULL;
307001a02ffcSStephen M. Cameron 	u32 nphysicals = 0;
307101a02ffcSStephen M. Cameron 	u32 nlogicals = 0;
307201a02ffcSStephen M. Cameron 	u32 ndev_allocated = 0;
3073edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
3074edd16368SStephen M. Cameron 	int ncurrent = 0;
30754f4eb9f1SScott Teel 	int i, n_ext_target_devs, ndevs_to_allocate;
3076339b2b14SStephen M. Cameron 	int raid_ctlr_position;
30772bbf5c7fSJoe Handzik 	int rescan_hba_mode;
3078aca4a520SScott Teel 	DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
3079edd16368SStephen M. Cameron 
3080cfe5badcSScott Teel 	currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
308192084715SStephen M. Cameron 	physdev_list = kzalloc(sizeof(*physdev_list), GFP_KERNEL);
308292084715SStephen M. Cameron 	logdev_list = kzalloc(sizeof(*logdev_list), GFP_KERNEL);
3083edd16368SStephen M. Cameron 	tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
308403383736SDon Brace 	id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
3085edd16368SStephen M. Cameron 
308603383736SDon Brace 	if (!currentsd || !physdev_list || !logdev_list ||
308703383736SDon Brace 		!tmpdevice || !id_phys) {
3088edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory\n");
3089edd16368SStephen M. Cameron 		goto out;
3090edd16368SStephen M. Cameron 	}
3091edd16368SStephen M. Cameron 	memset(lunzerobits, 0, sizeof(lunzerobits));
3092edd16368SStephen M. Cameron 
3093316b221aSStephen M. Cameron 	rescan_hba_mode = hpsa_hba_mode_enabled(h);
309496444fbbSJoe Handzik 	if (rescan_hba_mode < 0)
309596444fbbSJoe Handzik 		goto out;
3096316b221aSStephen M. Cameron 
3097316b221aSStephen M. Cameron 	if (!h->hba_mode_enabled && rescan_hba_mode)
3098316b221aSStephen M. Cameron 		dev_warn(&h->pdev->dev, "HBA mode enabled\n");
3099316b221aSStephen M. Cameron 	else if (h->hba_mode_enabled && !rescan_hba_mode)
3100316b221aSStephen M. Cameron 		dev_warn(&h->pdev->dev, "HBA mode disabled\n");
3101316b221aSStephen M. Cameron 
3102316b221aSStephen M. Cameron 	h->hba_mode_enabled = rescan_hba_mode;
3103316b221aSStephen M. Cameron 
310403383736SDon Brace 	if (hpsa_gather_lun_info(h, physdev_list, &nphysicals,
310503383736SDon Brace 			logdev_list, &nlogicals))
3106edd16368SStephen M. Cameron 		goto out;
3107edd16368SStephen M. Cameron 
3108aca4a520SScott Teel 	/* We might see up to the maximum number of logical and physical disks
3109aca4a520SScott Teel 	 * plus external target devices, and a device for the local RAID
3110aca4a520SScott Teel 	 * controller.
3111edd16368SStephen M. Cameron 	 */
3112aca4a520SScott Teel 	ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
3113edd16368SStephen M. Cameron 
3114edd16368SStephen M. Cameron 	/* Allocate the per device structures */
3115edd16368SStephen M. Cameron 	for (i = 0; i < ndevs_to_allocate; i++) {
3116b7ec021fSScott Teel 		if (i >= HPSA_MAX_DEVICES) {
3117b7ec021fSScott Teel 			dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
3118b7ec021fSScott Teel 				"  %d devices ignored.\n", HPSA_MAX_DEVICES,
3119b7ec021fSScott Teel 				ndevs_to_allocate - HPSA_MAX_DEVICES);
3120b7ec021fSScott Teel 			break;
3121b7ec021fSScott Teel 		}
3122b7ec021fSScott Teel 
3123edd16368SStephen M. Cameron 		currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
3124edd16368SStephen M. Cameron 		if (!currentsd[i]) {
3125edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
3126edd16368SStephen M. Cameron 				__FILE__, __LINE__);
3127edd16368SStephen M. Cameron 			goto out;
3128edd16368SStephen M. Cameron 		}
3129edd16368SStephen M. Cameron 		ndev_allocated++;
3130edd16368SStephen M. Cameron 	}
3131edd16368SStephen M. Cameron 
31328645291bSStephen M. Cameron 	if (is_scsi_rev_5(h))
3133339b2b14SStephen M. Cameron 		raid_ctlr_position = 0;
3134339b2b14SStephen M. Cameron 	else
3135339b2b14SStephen M. Cameron 		raid_ctlr_position = nphysicals + nlogicals;
3136339b2b14SStephen M. Cameron 
3137edd16368SStephen M. Cameron 	/* adjust our table of devices */
31384f4eb9f1SScott Teel 	n_ext_target_devs = 0;
3139edd16368SStephen M. Cameron 	for (i = 0; i < nphysicals + nlogicals + 1; i++) {
31400b0e1d6cSStephen M. Cameron 		u8 *lunaddrbytes, is_OBDR = 0;
3141edd16368SStephen M. Cameron 
3142edd16368SStephen M. Cameron 		/* Figure out where the LUN ID info is coming from */
3143339b2b14SStephen M. Cameron 		lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
3144339b2b14SStephen M. Cameron 			i, nphysicals, nlogicals, physdev_list, logdev_list);
3145edd16368SStephen M. Cameron 		/* skip masked physical devices. */
3146339b2b14SStephen M. Cameron 		if (lunaddrbytes[3] & 0xC0 &&
3147339b2b14SStephen M. Cameron 			i < nphysicals + (raid_ctlr_position == 0))
3148edd16368SStephen M. Cameron 			continue;
3149edd16368SStephen M. Cameron 
3150edd16368SStephen M. Cameron 		/* Get device type, vendor, model, device id */
31510b0e1d6cSStephen M. Cameron 		if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
31520b0e1d6cSStephen M. Cameron 							&is_OBDR))
3153edd16368SStephen M. Cameron 			continue; /* skip it if we can't talk to it. */
31541f310bdeSStephen M. Cameron 		figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
3155edd16368SStephen M. Cameron 		this_device = currentsd[ncurrent];
3156edd16368SStephen M. Cameron 
3157edd16368SStephen M. Cameron 		/*
31584f4eb9f1SScott Teel 		 * For external target devices, we have to insert a LUN 0 which
3159edd16368SStephen M. Cameron 		 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
3160edd16368SStephen M. Cameron 		 * is nonetheless an enclosure device there.  We have to
3161edd16368SStephen M. Cameron 		 * present that otherwise linux won't find anything if
3162edd16368SStephen M. Cameron 		 * there is no lun 0.
3163edd16368SStephen M. Cameron 		 */
31644f4eb9f1SScott Teel 		if (add_ext_target_dev(h, tmpdevice, this_device,
31651f310bdeSStephen M. Cameron 				lunaddrbytes, lunzerobits,
31664f4eb9f1SScott Teel 				&n_ext_target_devs)) {
3167edd16368SStephen M. Cameron 			ncurrent++;
3168edd16368SStephen M. Cameron 			this_device = currentsd[ncurrent];
3169edd16368SStephen M. Cameron 		}
3170edd16368SStephen M. Cameron 
3171edd16368SStephen M. Cameron 		*this_device = *tmpdevice;
3172edd16368SStephen M. Cameron 
3173edd16368SStephen M. Cameron 		switch (this_device->devtype) {
31740b0e1d6cSStephen M. Cameron 		case TYPE_ROM:
3175edd16368SStephen M. Cameron 			/* We don't *really* support actual CD-ROM devices,
3176edd16368SStephen M. Cameron 			 * just "One Button Disaster Recovery" tape drive
3177edd16368SStephen M. Cameron 			 * which temporarily pretends to be a CD-ROM drive.
3178edd16368SStephen M. Cameron 			 * So we check that the device is really an OBDR tape
3179edd16368SStephen M. Cameron 			 * device by checking for "$DR-10" in bytes 43-48 of
3180edd16368SStephen M. Cameron 			 * the inquiry data.
3181edd16368SStephen M. Cameron 			 */
31820b0e1d6cSStephen M. Cameron 			if (is_OBDR)
3183edd16368SStephen M. Cameron 				ncurrent++;
3184edd16368SStephen M. Cameron 			break;
3185edd16368SStephen M. Cameron 		case TYPE_DISK:
3186316b221aSStephen M. Cameron 			if (h->hba_mode_enabled) {
3187316b221aSStephen M. Cameron 				/* never use raid mapper in HBA mode */
3188316b221aSStephen M. Cameron 				this_device->offload_enabled = 0;
3189316b221aSStephen M. Cameron 				ncurrent++;
3190316b221aSStephen M. Cameron 				break;
3191316b221aSStephen M. Cameron 			} else if (h->acciopath_status) {
3192283b4a9bSStephen M. Cameron 				if (i >= nphysicals) {
3193283b4a9bSStephen M. Cameron 					ncurrent++;
3194edd16368SStephen M. Cameron 					break;
3195283b4a9bSStephen M. Cameron 				}
3196316b221aSStephen M. Cameron 			} else {
3197316b221aSStephen M. Cameron 				if (i < nphysicals)
3198316b221aSStephen M. Cameron 					break;
3199316b221aSStephen M. Cameron 				ncurrent++;
3200316b221aSStephen M. Cameron 				break;
3201316b221aSStephen M. Cameron 			}
320203383736SDon Brace 			if (h->transMethod & CFGTBL_Trans_io_accel1 ||
320303383736SDon Brace 				h->transMethod & CFGTBL_Trans_io_accel2) {
320403383736SDon Brace 				hpsa_get_ioaccel_drive_info(h, this_device,
320503383736SDon Brace 							lunaddrbytes, id_phys);
320603383736SDon Brace 				atomic_set(&this_device->ioaccel_cmds_out, 0);
3207edd16368SStephen M. Cameron 				ncurrent++;
3208283b4a9bSStephen M. Cameron 			}
3209edd16368SStephen M. Cameron 			break;
3210edd16368SStephen M. Cameron 		case TYPE_TAPE:
3211edd16368SStephen M. Cameron 		case TYPE_MEDIUM_CHANGER:
3212edd16368SStephen M. Cameron 			ncurrent++;
3213edd16368SStephen M. Cameron 			break;
3214edd16368SStephen M. Cameron 		case TYPE_RAID:
3215edd16368SStephen M. Cameron 			/* Only present the Smartarray HBA as a RAID controller.
3216edd16368SStephen M. Cameron 			 * If it's a RAID controller other than the HBA itself
3217edd16368SStephen M. Cameron 			 * (an external RAID controller, MSA500 or similar)
3218edd16368SStephen M. Cameron 			 * don't present it.
3219edd16368SStephen M. Cameron 			 */
3220edd16368SStephen M. Cameron 			if (!is_hba_lunid(lunaddrbytes))
3221edd16368SStephen M. Cameron 				break;
3222edd16368SStephen M. Cameron 			ncurrent++;
3223edd16368SStephen M. Cameron 			break;
3224edd16368SStephen M. Cameron 		default:
3225edd16368SStephen M. Cameron 			break;
3226edd16368SStephen M. Cameron 		}
3227cfe5badcSScott Teel 		if (ncurrent >= HPSA_MAX_DEVICES)
3228edd16368SStephen M. Cameron 			break;
3229edd16368SStephen M. Cameron 	}
323003383736SDon Brace 	hpsa_update_log_drive_phys_drive_ptrs(h, currentsd, ncurrent);
3231edd16368SStephen M. Cameron 	adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
3232edd16368SStephen M. Cameron out:
3233edd16368SStephen M. Cameron 	kfree(tmpdevice);
3234edd16368SStephen M. Cameron 	for (i = 0; i < ndev_allocated; i++)
3235edd16368SStephen M. Cameron 		kfree(currentsd[i]);
3236edd16368SStephen M. Cameron 	kfree(currentsd);
3237edd16368SStephen M. Cameron 	kfree(physdev_list);
3238edd16368SStephen M. Cameron 	kfree(logdev_list);
323903383736SDon Brace 	kfree(id_phys);
3240edd16368SStephen M. Cameron }
3241edd16368SStephen M. Cameron 
3242ec5cbf04SWebb Scales static void hpsa_set_sg_descriptor(struct SGDescriptor *desc,
3243ec5cbf04SWebb Scales 				   struct scatterlist *sg)
3244ec5cbf04SWebb Scales {
3245ec5cbf04SWebb Scales 	u64 addr64 = (u64) sg_dma_address(sg);
3246ec5cbf04SWebb Scales 	unsigned int len = sg_dma_len(sg);
3247ec5cbf04SWebb Scales 
3248ec5cbf04SWebb Scales 	desc->Addr = cpu_to_le64(addr64);
3249ec5cbf04SWebb Scales 	desc->Len = cpu_to_le32(len);
3250ec5cbf04SWebb Scales 	desc->Ext = 0;
3251ec5cbf04SWebb Scales }
3252ec5cbf04SWebb Scales 
3253c7ee65b3SWebb Scales /*
3254c7ee65b3SWebb Scales  * hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
3255edd16368SStephen M. Cameron  * dma mapping  and fills in the scatter gather entries of the
3256edd16368SStephen M. Cameron  * hpsa command, cp.
3257edd16368SStephen M. Cameron  */
325833a2ffceSStephen M. Cameron static int hpsa_scatter_gather(struct ctlr_info *h,
3259edd16368SStephen M. Cameron 		struct CommandList *cp,
3260edd16368SStephen M. Cameron 		struct scsi_cmnd *cmd)
3261edd16368SStephen M. Cameron {
3262edd16368SStephen M. Cameron 	struct scatterlist *sg;
326333a2ffceSStephen M. Cameron 	int use_sg, i, sg_index, chained;
326433a2ffceSStephen M. Cameron 	struct SGDescriptor *curr_sg;
3265edd16368SStephen M. Cameron 
326633a2ffceSStephen M. Cameron 	BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
3267edd16368SStephen M. Cameron 
3268edd16368SStephen M. Cameron 	use_sg = scsi_dma_map(cmd);
3269edd16368SStephen M. Cameron 	if (use_sg < 0)
3270edd16368SStephen M. Cameron 		return use_sg;
3271edd16368SStephen M. Cameron 
3272edd16368SStephen M. Cameron 	if (!use_sg)
3273edd16368SStephen M. Cameron 		goto sglist_finished;
3274edd16368SStephen M. Cameron 
327533a2ffceSStephen M. Cameron 	curr_sg = cp->SG;
327633a2ffceSStephen M. Cameron 	chained = 0;
327733a2ffceSStephen M. Cameron 	sg_index = 0;
3278edd16368SStephen M. Cameron 	scsi_for_each_sg(cmd, sg, use_sg, i) {
327933a2ffceSStephen M. Cameron 		if (i == h->max_cmd_sg_entries - 1 &&
328033a2ffceSStephen M. Cameron 			use_sg > h->max_cmd_sg_entries) {
328133a2ffceSStephen M. Cameron 			chained = 1;
328233a2ffceSStephen M. Cameron 			curr_sg = h->cmd_sg_list[cp->cmdindex];
328333a2ffceSStephen M. Cameron 			sg_index = 0;
328433a2ffceSStephen M. Cameron 		}
3285ec5cbf04SWebb Scales 		hpsa_set_sg_descriptor(curr_sg, sg);
328633a2ffceSStephen M. Cameron 		curr_sg++;
328733a2ffceSStephen M. Cameron 	}
3288ec5cbf04SWebb Scales 
3289ec5cbf04SWebb Scales 	/* Back the pointer up to the last entry and mark it as "last". */
329050a0decfSStephen M. Cameron 	(--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
329133a2ffceSStephen M. Cameron 
329233a2ffceSStephen M. Cameron 	if (use_sg + chained > h->maxSG)
329333a2ffceSStephen M. Cameron 		h->maxSG = use_sg + chained;
329433a2ffceSStephen M. Cameron 
329533a2ffceSStephen M. Cameron 	if (chained) {
329633a2ffceSStephen M. Cameron 		cp->Header.SGList = h->max_cmd_sg_entries;
329750a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(use_sg + 1);
3298e2bea6dfSStephen M. Cameron 		if (hpsa_map_sg_chain_block(h, cp)) {
3299e2bea6dfSStephen M. Cameron 			scsi_dma_unmap(cmd);
3300e2bea6dfSStephen M. Cameron 			return -1;
3301e2bea6dfSStephen M. Cameron 		}
330233a2ffceSStephen M. Cameron 		return 0;
3303edd16368SStephen M. Cameron 	}
3304edd16368SStephen M. Cameron 
3305edd16368SStephen M. Cameron sglist_finished:
3306edd16368SStephen M. Cameron 
330701a02ffcSStephen M. Cameron 	cp->Header.SGList = (u8) use_sg;   /* no. SGs contig in this cmd */
3308c7ee65b3SWebb Scales 	cp->Header.SGTotal = cpu_to_le16(use_sg); /* total sgs in cmd list */
3309edd16368SStephen M. Cameron 	return 0;
3310edd16368SStephen M. Cameron }
3311edd16368SStephen M. Cameron 
3312283b4a9bSStephen M. Cameron #define IO_ACCEL_INELIGIBLE (1)
3313283b4a9bSStephen M. Cameron static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
3314283b4a9bSStephen M. Cameron {
3315283b4a9bSStephen M. Cameron 	int is_write = 0;
3316283b4a9bSStephen M. Cameron 	u32 block;
3317283b4a9bSStephen M. Cameron 	u32 block_cnt;
3318283b4a9bSStephen M. Cameron 
3319283b4a9bSStephen M. Cameron 	/* Perform some CDB fixups if needed using 10 byte reads/writes only */
3320283b4a9bSStephen M. Cameron 	switch (cdb[0]) {
3321283b4a9bSStephen M. Cameron 	case WRITE_6:
3322283b4a9bSStephen M. Cameron 	case WRITE_12:
3323283b4a9bSStephen M. Cameron 		is_write = 1;
3324283b4a9bSStephen M. Cameron 	case READ_6:
3325283b4a9bSStephen M. Cameron 	case READ_12:
3326283b4a9bSStephen M. Cameron 		if (*cdb_len == 6) {
3327283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 8) | cdb[3];
3328283b4a9bSStephen M. Cameron 			block_cnt = cdb[4];
3329283b4a9bSStephen M. Cameron 		} else {
3330283b4a9bSStephen M. Cameron 			BUG_ON(*cdb_len != 12);
3331283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 24) |
3332283b4a9bSStephen M. Cameron 				(((u32) cdb[3]) << 16) |
3333283b4a9bSStephen M. Cameron 				(((u32) cdb[4]) << 8) |
3334283b4a9bSStephen M. Cameron 				cdb[5];
3335283b4a9bSStephen M. Cameron 			block_cnt =
3336283b4a9bSStephen M. Cameron 				(((u32) cdb[6]) << 24) |
3337283b4a9bSStephen M. Cameron 				(((u32) cdb[7]) << 16) |
3338283b4a9bSStephen M. Cameron 				(((u32) cdb[8]) << 8) |
3339283b4a9bSStephen M. Cameron 				cdb[9];
3340283b4a9bSStephen M. Cameron 		}
3341283b4a9bSStephen M. Cameron 		if (block_cnt > 0xffff)
3342283b4a9bSStephen M. Cameron 			return IO_ACCEL_INELIGIBLE;
3343283b4a9bSStephen M. Cameron 
3344283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
3345283b4a9bSStephen M. Cameron 		cdb[1] = 0;
3346283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (block >> 24);
3347283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (block >> 16);
3348283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (block >> 8);
3349283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (block);
3350283b4a9bSStephen M. Cameron 		cdb[6] = 0;
3351283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (block_cnt >> 8);
3352283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (block_cnt);
3353283b4a9bSStephen M. Cameron 		cdb[9] = 0;
3354283b4a9bSStephen M. Cameron 		*cdb_len = 10;
3355283b4a9bSStephen M. Cameron 		break;
3356283b4a9bSStephen M. Cameron 	}
3357283b4a9bSStephen M. Cameron 	return 0;
3358283b4a9bSStephen M. Cameron }
3359283b4a9bSStephen M. Cameron 
3360c349775eSScott Teel static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
3361283b4a9bSStephen M. Cameron 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
336203383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
3363e1f7de0cSMatt Gates {
3364e1f7de0cSMatt Gates 	struct scsi_cmnd *cmd = c->scsi_cmd;
3365e1f7de0cSMatt Gates 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
3366e1f7de0cSMatt Gates 	unsigned int len;
3367e1f7de0cSMatt Gates 	unsigned int total_len = 0;
3368e1f7de0cSMatt Gates 	struct scatterlist *sg;
3369e1f7de0cSMatt Gates 	u64 addr64;
3370e1f7de0cSMatt Gates 	int use_sg, i;
3371e1f7de0cSMatt Gates 	struct SGDescriptor *curr_sg;
3372e1f7de0cSMatt Gates 	u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
3373e1f7de0cSMatt Gates 
3374283b4a9bSStephen M. Cameron 	/* TODO: implement chaining support */
337503383736SDon Brace 	if (scsi_sg_count(cmd) > h->ioaccel_maxsg) {
337603383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3377283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
337803383736SDon Brace 	}
3379283b4a9bSStephen M. Cameron 
3380e1f7de0cSMatt Gates 	BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
3381e1f7de0cSMatt Gates 
338203383736SDon Brace 	if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
338303383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3384283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
338503383736SDon Brace 	}
3386283b4a9bSStephen M. Cameron 
3387e1f7de0cSMatt Gates 	c->cmd_type = CMD_IOACCEL1;
3388e1f7de0cSMatt Gates 
3389e1f7de0cSMatt Gates 	/* Adjust the DMA address to point to the accelerated command buffer */
3390e1f7de0cSMatt Gates 	c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
3391e1f7de0cSMatt Gates 				(c->cmdindex * sizeof(*cp));
3392e1f7de0cSMatt Gates 	BUG_ON(c->busaddr & 0x0000007F);
3393e1f7de0cSMatt Gates 
3394e1f7de0cSMatt Gates 	use_sg = scsi_dma_map(cmd);
339503383736SDon Brace 	if (use_sg < 0) {
339603383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3397e1f7de0cSMatt Gates 		return use_sg;
339803383736SDon Brace 	}
3399e1f7de0cSMatt Gates 
3400e1f7de0cSMatt Gates 	if (use_sg) {
3401e1f7de0cSMatt Gates 		curr_sg = cp->SG;
3402e1f7de0cSMatt Gates 		scsi_for_each_sg(cmd, sg, use_sg, i) {
3403e1f7de0cSMatt Gates 			addr64 = (u64) sg_dma_address(sg);
3404e1f7de0cSMatt Gates 			len  = sg_dma_len(sg);
3405e1f7de0cSMatt Gates 			total_len += len;
340650a0decfSStephen M. Cameron 			curr_sg->Addr = cpu_to_le64(addr64);
340750a0decfSStephen M. Cameron 			curr_sg->Len = cpu_to_le32(len);
340850a0decfSStephen M. Cameron 			curr_sg->Ext = cpu_to_le32(0);
3409e1f7de0cSMatt Gates 			curr_sg++;
3410e1f7de0cSMatt Gates 		}
341150a0decfSStephen M. Cameron 		(--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
3412e1f7de0cSMatt Gates 
3413e1f7de0cSMatt Gates 		switch (cmd->sc_data_direction) {
3414e1f7de0cSMatt Gates 		case DMA_TO_DEVICE:
3415e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_OUT;
3416e1f7de0cSMatt Gates 			break;
3417e1f7de0cSMatt Gates 		case DMA_FROM_DEVICE:
3418e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_IN;
3419e1f7de0cSMatt Gates 			break;
3420e1f7de0cSMatt Gates 		case DMA_NONE:
3421e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_NODATAXFER;
3422e1f7de0cSMatt Gates 			break;
3423e1f7de0cSMatt Gates 		default:
3424e1f7de0cSMatt Gates 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3425e1f7de0cSMatt Gates 			cmd->sc_data_direction);
3426e1f7de0cSMatt Gates 			BUG();
3427e1f7de0cSMatt Gates 			break;
3428e1f7de0cSMatt Gates 		}
3429e1f7de0cSMatt Gates 	} else {
3430e1f7de0cSMatt Gates 		control |= IOACCEL1_CONTROL_NODATAXFER;
3431e1f7de0cSMatt Gates 	}
3432e1f7de0cSMatt Gates 
3433c349775eSScott Teel 	c->Header.SGList = use_sg;
3434e1f7de0cSMatt Gates 	/* Fill out the command structure to submit */
34352b08b3e9SDon Brace 	cp->dev_handle = cpu_to_le16(ioaccel_handle & 0xFFFF);
34362b08b3e9SDon Brace 	cp->transfer_len = cpu_to_le32(total_len);
34372b08b3e9SDon Brace 	cp->io_flags = cpu_to_le16(IOACCEL1_IOFLAGS_IO_REQ |
34382b08b3e9SDon Brace 			(cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK));
34392b08b3e9SDon Brace 	cp->control = cpu_to_le32(control);
3440283b4a9bSStephen M. Cameron 	memcpy(cp->CDB, cdb, cdb_len);
3441283b4a9bSStephen M. Cameron 	memcpy(cp->CISS_LUN, scsi3addr, 8);
3442c349775eSScott Teel 	/* Tag was already set at init time. */
3443e1f7de0cSMatt Gates 	enqueue_cmd_and_start_io(h, c);
3444e1f7de0cSMatt Gates 	return 0;
3445e1f7de0cSMatt Gates }
3446edd16368SStephen M. Cameron 
3447283b4a9bSStephen M. Cameron /*
3448283b4a9bSStephen M. Cameron  * Queue a command directly to a device behind the controller using the
3449283b4a9bSStephen M. Cameron  * I/O accelerator path.
3450283b4a9bSStephen M. Cameron  */
3451283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
3452283b4a9bSStephen M. Cameron 	struct CommandList *c)
3453283b4a9bSStephen M. Cameron {
3454283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
3455283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3456283b4a9bSStephen M. Cameron 
345703383736SDon Brace 	c->phys_disk = dev;
345803383736SDon Brace 
3459283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
346003383736SDon Brace 		cmd->cmnd, cmd->cmd_len, dev->scsi3addr, dev);
3461283b4a9bSStephen M. Cameron }
3462283b4a9bSStephen M. Cameron 
3463dd0e19f3SScott Teel /*
3464dd0e19f3SScott Teel  * Set encryption parameters for the ioaccel2 request
3465dd0e19f3SScott Teel  */
3466dd0e19f3SScott Teel static void set_encrypt_ioaccel2(struct ctlr_info *h,
3467dd0e19f3SScott Teel 	struct CommandList *c, struct io_accel2_cmd *cp)
3468dd0e19f3SScott Teel {
3469dd0e19f3SScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
3470dd0e19f3SScott Teel 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3471dd0e19f3SScott Teel 	struct raid_map_data *map = &dev->raid_map;
3472dd0e19f3SScott Teel 	u64 first_block;
3473dd0e19f3SScott Teel 
3474dd0e19f3SScott Teel 	/* Are we doing encryption on this device */
34752b08b3e9SDon Brace 	if (!(le16_to_cpu(map->flags) & RAID_MAP_FLAG_ENCRYPT_ON))
3476dd0e19f3SScott Teel 		return;
3477dd0e19f3SScott Teel 	/* Set the data encryption key index. */
3478dd0e19f3SScott Teel 	cp->dekindex = map->dekindex;
3479dd0e19f3SScott Teel 
3480dd0e19f3SScott Teel 	/* Set the encryption enable flag, encoded into direction field. */
3481dd0e19f3SScott Teel 	cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
3482dd0e19f3SScott Teel 
3483dd0e19f3SScott Teel 	/* Set encryption tweak values based on logical block address
3484dd0e19f3SScott Teel 	 * If block size is 512, tweak value is LBA.
3485dd0e19f3SScott Teel 	 * For other block sizes, tweak is (LBA * block size)/ 512)
3486dd0e19f3SScott Teel 	 */
3487dd0e19f3SScott Teel 	switch (cmd->cmnd[0]) {
3488dd0e19f3SScott Teel 	/* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
3489dd0e19f3SScott Teel 	case WRITE_6:
3490dd0e19f3SScott Teel 	case READ_6:
34912b08b3e9SDon Brace 		first_block = get_unaligned_be16(&cmd->cmnd[2]);
3492dd0e19f3SScott Teel 		break;
3493dd0e19f3SScott Teel 	case WRITE_10:
3494dd0e19f3SScott Teel 	case READ_10:
3495dd0e19f3SScott Teel 	/* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
3496dd0e19f3SScott Teel 	case WRITE_12:
3497dd0e19f3SScott Teel 	case READ_12:
34982b08b3e9SDon Brace 		first_block = get_unaligned_be32(&cmd->cmnd[2]);
3499dd0e19f3SScott Teel 		break;
3500dd0e19f3SScott Teel 	case WRITE_16:
3501dd0e19f3SScott Teel 	case READ_16:
35022b08b3e9SDon Brace 		first_block = get_unaligned_be64(&cmd->cmnd[2]);
3503dd0e19f3SScott Teel 		break;
3504dd0e19f3SScott Teel 	default:
3505dd0e19f3SScott Teel 		dev_err(&h->pdev->dev,
35062b08b3e9SDon Brace 			"ERROR: %s: size (0x%x) not supported for encryption\n",
35072b08b3e9SDon Brace 			__func__, cmd->cmnd[0]);
3508dd0e19f3SScott Teel 		BUG();
3509dd0e19f3SScott Teel 		break;
3510dd0e19f3SScott Teel 	}
35112b08b3e9SDon Brace 
35122b08b3e9SDon Brace 	if (le32_to_cpu(map->volume_blk_size) != 512)
35132b08b3e9SDon Brace 		first_block = first_block *
35142b08b3e9SDon Brace 				le32_to_cpu(map->volume_blk_size)/512;
35152b08b3e9SDon Brace 
35162b08b3e9SDon Brace 	cp->tweak_lower = cpu_to_le32(first_block);
35172b08b3e9SDon Brace 	cp->tweak_upper = cpu_to_le32(first_block >> 32);
3518dd0e19f3SScott Teel }
3519dd0e19f3SScott Teel 
3520c349775eSScott Teel static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
3521c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
352203383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
3523c349775eSScott Teel {
3524c349775eSScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
3525c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
3526c349775eSScott Teel 	struct ioaccel2_sg_element *curr_sg;
3527c349775eSScott Teel 	int use_sg, i;
3528c349775eSScott Teel 	struct scatterlist *sg;
3529c349775eSScott Teel 	u64 addr64;
3530c349775eSScott Teel 	u32 len;
3531c349775eSScott Teel 	u32 total_len = 0;
3532c349775eSScott Teel 
353303383736SDon Brace 	if (scsi_sg_count(cmd) > h->ioaccel_maxsg) {
353403383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3535c349775eSScott Teel 		return IO_ACCEL_INELIGIBLE;
353603383736SDon Brace 	}
3537c349775eSScott Teel 
353803383736SDon Brace 	if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
353903383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3540c349775eSScott Teel 		return IO_ACCEL_INELIGIBLE;
354103383736SDon Brace 	}
354203383736SDon Brace 
3543c349775eSScott Teel 	c->cmd_type = CMD_IOACCEL2;
3544c349775eSScott Teel 	/* Adjust the DMA address to point to the accelerated command buffer */
3545c349775eSScott Teel 	c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
3546c349775eSScott Teel 				(c->cmdindex * sizeof(*cp));
3547c349775eSScott Teel 	BUG_ON(c->busaddr & 0x0000007F);
3548c349775eSScott Teel 
3549c349775eSScott Teel 	memset(cp, 0, sizeof(*cp));
3550c349775eSScott Teel 	cp->IU_type = IOACCEL2_IU_TYPE;
3551c349775eSScott Teel 
3552c349775eSScott Teel 	use_sg = scsi_dma_map(cmd);
355303383736SDon Brace 	if (use_sg < 0) {
355403383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
3555c349775eSScott Teel 		return use_sg;
355603383736SDon Brace 	}
3557c349775eSScott Teel 
3558c349775eSScott Teel 	if (use_sg) {
3559c349775eSScott Teel 		BUG_ON(use_sg > IOACCEL2_MAXSGENTRIES);
3560c349775eSScott Teel 		curr_sg = cp->sg;
3561c349775eSScott Teel 		scsi_for_each_sg(cmd, sg, use_sg, i) {
3562c349775eSScott Teel 			addr64 = (u64) sg_dma_address(sg);
3563c349775eSScott Teel 			len  = sg_dma_len(sg);
3564c349775eSScott Teel 			total_len += len;
3565c349775eSScott Teel 			curr_sg->address = cpu_to_le64(addr64);
3566c349775eSScott Teel 			curr_sg->length = cpu_to_le32(len);
3567c349775eSScott Teel 			curr_sg->reserved[0] = 0;
3568c349775eSScott Teel 			curr_sg->reserved[1] = 0;
3569c349775eSScott Teel 			curr_sg->reserved[2] = 0;
3570c349775eSScott Teel 			curr_sg->chain_indicator = 0;
3571c349775eSScott Teel 			curr_sg++;
3572c349775eSScott Teel 		}
3573c349775eSScott Teel 
3574c349775eSScott Teel 		switch (cmd->sc_data_direction) {
3575c349775eSScott Teel 		case DMA_TO_DEVICE:
3576dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3577dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_OUT;
3578c349775eSScott Teel 			break;
3579c349775eSScott Teel 		case DMA_FROM_DEVICE:
3580dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3581dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_IN;
3582c349775eSScott Teel 			break;
3583c349775eSScott Teel 		case DMA_NONE:
3584dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3585dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_NO_DATA;
3586c349775eSScott Teel 			break;
3587c349775eSScott Teel 		default:
3588c349775eSScott Teel 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3589c349775eSScott Teel 				cmd->sc_data_direction);
3590c349775eSScott Teel 			BUG();
3591c349775eSScott Teel 			break;
3592c349775eSScott Teel 		}
3593c349775eSScott Teel 	} else {
3594dd0e19f3SScott Teel 		cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3595dd0e19f3SScott Teel 		cp->direction |= IOACCEL2_DIR_NO_DATA;
3596c349775eSScott Teel 	}
3597dd0e19f3SScott Teel 
3598dd0e19f3SScott Teel 	/* Set encryption parameters, if necessary */
3599dd0e19f3SScott Teel 	set_encrypt_ioaccel2(h, c, cp);
3600dd0e19f3SScott Teel 
36012b08b3e9SDon Brace 	cp->scsi_nexus = cpu_to_le32(ioaccel_handle);
3602f2405db8SDon Brace 	cp->Tag = cpu_to_le32(c->cmdindex << DIRECT_LOOKUP_SHIFT);
3603c349775eSScott Teel 	memcpy(cp->cdb, cdb, sizeof(cp->cdb));
3604c349775eSScott Teel 
3605c349775eSScott Teel 	/* fill in sg elements */
3606c349775eSScott Teel 	cp->sg_count = (u8) use_sg;
3607c349775eSScott Teel 
3608c349775eSScott Teel 	cp->data_len = cpu_to_le32(total_len);
3609c349775eSScott Teel 	cp->err_ptr = cpu_to_le64(c->busaddr +
3610c349775eSScott Teel 			offsetof(struct io_accel2_cmd, error_data));
361150a0decfSStephen M. Cameron 	cp->err_len = cpu_to_le32(sizeof(cp->error_data));
3612c349775eSScott Teel 
3613c349775eSScott Teel 	enqueue_cmd_and_start_io(h, c);
3614c349775eSScott Teel 	return 0;
3615c349775eSScott Teel }
3616c349775eSScott Teel 
3617c349775eSScott Teel /*
3618c349775eSScott Teel  * Queue a command to the correct I/O accelerator path.
3619c349775eSScott Teel  */
3620c349775eSScott Teel static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
3621c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
362203383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
3623c349775eSScott Teel {
362403383736SDon Brace 	/* Try to honor the device's queue depth */
362503383736SDon Brace 	if (atomic_inc_return(&phys_disk->ioaccel_cmds_out) >
362603383736SDon Brace 					phys_disk->queue_depth) {
362703383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
362803383736SDon Brace 		return IO_ACCEL_INELIGIBLE;
362903383736SDon Brace 	}
3630c349775eSScott Teel 	if (h->transMethod & CFGTBL_Trans_io_accel1)
3631c349775eSScott Teel 		return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
363203383736SDon Brace 						cdb, cdb_len, scsi3addr,
363303383736SDon Brace 						phys_disk);
3634c349775eSScott Teel 	else
3635c349775eSScott Teel 		return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
363603383736SDon Brace 						cdb, cdb_len, scsi3addr,
363703383736SDon Brace 						phys_disk);
3638c349775eSScott Teel }
3639c349775eSScott Teel 
36406b80b18fSScott Teel static void raid_map_helper(struct raid_map_data *map,
36416b80b18fSScott Teel 		int offload_to_mirror, u32 *map_index, u32 *current_group)
36426b80b18fSScott Teel {
36436b80b18fSScott Teel 	if (offload_to_mirror == 0)  {
36446b80b18fSScott Teel 		/* use physical disk in the first mirrored group. */
36452b08b3e9SDon Brace 		*map_index %= le16_to_cpu(map->data_disks_per_row);
36466b80b18fSScott Teel 		return;
36476b80b18fSScott Teel 	}
36486b80b18fSScott Teel 	do {
36496b80b18fSScott Teel 		/* determine mirror group that *map_index indicates */
36502b08b3e9SDon Brace 		*current_group = *map_index /
36512b08b3e9SDon Brace 			le16_to_cpu(map->data_disks_per_row);
36526b80b18fSScott Teel 		if (offload_to_mirror == *current_group)
36536b80b18fSScott Teel 			continue;
36542b08b3e9SDon Brace 		if (*current_group < le16_to_cpu(map->layout_map_count) - 1) {
36556b80b18fSScott Teel 			/* select map index from next group */
36562b08b3e9SDon Brace 			*map_index += le16_to_cpu(map->data_disks_per_row);
36576b80b18fSScott Teel 			(*current_group)++;
36586b80b18fSScott Teel 		} else {
36596b80b18fSScott Teel 			/* select map index from first group */
36602b08b3e9SDon Brace 			*map_index %= le16_to_cpu(map->data_disks_per_row);
36616b80b18fSScott Teel 			*current_group = 0;
36626b80b18fSScott Teel 		}
36636b80b18fSScott Teel 	} while (offload_to_mirror != *current_group);
36646b80b18fSScott Teel }
36656b80b18fSScott Teel 
3666283b4a9bSStephen M. Cameron /*
3667283b4a9bSStephen M. Cameron  * Attempt to perform offload RAID mapping for a logical volume I/O.
3668283b4a9bSStephen M. Cameron  */
3669283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
3670283b4a9bSStephen M. Cameron 	struct CommandList *c)
3671283b4a9bSStephen M. Cameron {
3672283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
3673283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3674283b4a9bSStephen M. Cameron 	struct raid_map_data *map = &dev->raid_map;
3675283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map->data[0];
3676283b4a9bSStephen M. Cameron 	int is_write = 0;
3677283b4a9bSStephen M. Cameron 	u32 map_index;
3678283b4a9bSStephen M. Cameron 	u64 first_block, last_block;
3679283b4a9bSStephen M. Cameron 	u32 block_cnt;
3680283b4a9bSStephen M. Cameron 	u32 blocks_per_row;
3681283b4a9bSStephen M. Cameron 	u64 first_row, last_row;
3682283b4a9bSStephen M. Cameron 	u32 first_row_offset, last_row_offset;
3683283b4a9bSStephen M. Cameron 	u32 first_column, last_column;
36846b80b18fSScott Teel 	u64 r0_first_row, r0_last_row;
36856b80b18fSScott Teel 	u32 r5or6_blocks_per_row;
36866b80b18fSScott Teel 	u64 r5or6_first_row, r5or6_last_row;
36876b80b18fSScott Teel 	u32 r5or6_first_row_offset, r5or6_last_row_offset;
36886b80b18fSScott Teel 	u32 r5or6_first_column, r5or6_last_column;
36896b80b18fSScott Teel 	u32 total_disks_per_row;
36906b80b18fSScott Teel 	u32 stripesize;
36916b80b18fSScott Teel 	u32 first_group, last_group, current_group;
3692283b4a9bSStephen M. Cameron 	u32 map_row;
3693283b4a9bSStephen M. Cameron 	u32 disk_handle;
3694283b4a9bSStephen M. Cameron 	u64 disk_block;
3695283b4a9bSStephen M. Cameron 	u32 disk_block_cnt;
3696283b4a9bSStephen M. Cameron 	u8 cdb[16];
3697283b4a9bSStephen M. Cameron 	u8 cdb_len;
36982b08b3e9SDon Brace 	u16 strip_size;
3699283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
3700283b4a9bSStephen M. Cameron 	u64 tmpdiv;
3701283b4a9bSStephen M. Cameron #endif
37026b80b18fSScott Teel 	int offload_to_mirror;
3703283b4a9bSStephen M. Cameron 
3704283b4a9bSStephen M. Cameron 	/* check for valid opcode, get LBA and block count */
3705283b4a9bSStephen M. Cameron 	switch (cmd->cmnd[0]) {
3706283b4a9bSStephen M. Cameron 	case WRITE_6:
3707283b4a9bSStephen M. Cameron 		is_write = 1;
3708283b4a9bSStephen M. Cameron 	case READ_6:
3709283b4a9bSStephen M. Cameron 		first_block =
3710283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 8) |
3711283b4a9bSStephen M. Cameron 			cmd->cmnd[3];
3712283b4a9bSStephen M. Cameron 		block_cnt = cmd->cmnd[4];
37133fa89a04SStephen M. Cameron 		if (block_cnt == 0)
37143fa89a04SStephen M. Cameron 			block_cnt = 256;
3715283b4a9bSStephen M. Cameron 		break;
3716283b4a9bSStephen M. Cameron 	case WRITE_10:
3717283b4a9bSStephen M. Cameron 		is_write = 1;
3718283b4a9bSStephen M. Cameron 	case READ_10:
3719283b4a9bSStephen M. Cameron 		first_block =
3720283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
3721283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
3722283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
3723283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
3724283b4a9bSStephen M. Cameron 		block_cnt =
3725283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 8) |
3726283b4a9bSStephen M. Cameron 			cmd->cmnd[8];
3727283b4a9bSStephen M. Cameron 		break;
3728283b4a9bSStephen M. Cameron 	case WRITE_12:
3729283b4a9bSStephen M. Cameron 		is_write = 1;
3730283b4a9bSStephen M. Cameron 	case READ_12:
3731283b4a9bSStephen M. Cameron 		first_block =
3732283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
3733283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
3734283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
3735283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
3736283b4a9bSStephen M. Cameron 		block_cnt =
3737283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[6]) << 24) |
3738283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 16) |
3739283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[8]) << 8) |
3740283b4a9bSStephen M. Cameron 		cmd->cmnd[9];
3741283b4a9bSStephen M. Cameron 		break;
3742283b4a9bSStephen M. Cameron 	case WRITE_16:
3743283b4a9bSStephen M. Cameron 		is_write = 1;
3744283b4a9bSStephen M. Cameron 	case READ_16:
3745283b4a9bSStephen M. Cameron 		first_block =
3746283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 56) |
3747283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 48) |
3748283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 40) |
3749283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[5]) << 32) |
3750283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[6]) << 24) |
3751283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[7]) << 16) |
3752283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[8]) << 8) |
3753283b4a9bSStephen M. Cameron 			cmd->cmnd[9];
3754283b4a9bSStephen M. Cameron 		block_cnt =
3755283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[10]) << 24) |
3756283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[11]) << 16) |
3757283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[12]) << 8) |
3758283b4a9bSStephen M. Cameron 			cmd->cmnd[13];
3759283b4a9bSStephen M. Cameron 		break;
3760283b4a9bSStephen M. Cameron 	default:
3761283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
3762283b4a9bSStephen M. Cameron 	}
3763283b4a9bSStephen M. Cameron 	last_block = first_block + block_cnt - 1;
3764283b4a9bSStephen M. Cameron 
3765283b4a9bSStephen M. Cameron 	/* check for write to non-RAID-0 */
3766283b4a9bSStephen M. Cameron 	if (is_write && dev->raid_level != 0)
3767283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3768283b4a9bSStephen M. Cameron 
3769283b4a9bSStephen M. Cameron 	/* check for invalid block or wraparound */
37702b08b3e9SDon Brace 	if (last_block >= le64_to_cpu(map->volume_blk_cnt) ||
37712b08b3e9SDon Brace 		last_block < first_block)
3772283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3773283b4a9bSStephen M. Cameron 
3774283b4a9bSStephen M. Cameron 	/* calculate stripe information for the request */
37752b08b3e9SDon Brace 	blocks_per_row = le16_to_cpu(map->data_disks_per_row) *
37762b08b3e9SDon Brace 				le16_to_cpu(map->strip_size);
37772b08b3e9SDon Brace 	strip_size = le16_to_cpu(map->strip_size);
3778283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
3779283b4a9bSStephen M. Cameron 	tmpdiv = first_block;
3780283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
3781283b4a9bSStephen M. Cameron 	first_row = tmpdiv;
3782283b4a9bSStephen M. Cameron 	tmpdiv = last_block;
3783283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
3784283b4a9bSStephen M. Cameron 	last_row = tmpdiv;
3785283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
3786283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
3787283b4a9bSStephen M. Cameron 	tmpdiv = first_row_offset;
37882b08b3e9SDon Brace 	(void) do_div(tmpdiv, strip_size);
3789283b4a9bSStephen M. Cameron 	first_column = tmpdiv;
3790283b4a9bSStephen M. Cameron 	tmpdiv = last_row_offset;
37912b08b3e9SDon Brace 	(void) do_div(tmpdiv, strip_size);
3792283b4a9bSStephen M. Cameron 	last_column = tmpdiv;
3793283b4a9bSStephen M. Cameron #else
3794283b4a9bSStephen M. Cameron 	first_row = first_block / blocks_per_row;
3795283b4a9bSStephen M. Cameron 	last_row = last_block / blocks_per_row;
3796283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
3797283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
37982b08b3e9SDon Brace 	first_column = first_row_offset / strip_size;
37992b08b3e9SDon Brace 	last_column = last_row_offset / strip_size;
3800283b4a9bSStephen M. Cameron #endif
3801283b4a9bSStephen M. Cameron 
3802283b4a9bSStephen M. Cameron 	/* if this isn't a single row/column then give to the controller */
3803283b4a9bSStephen M. Cameron 	if ((first_row != last_row) || (first_column != last_column))
3804283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3805283b4a9bSStephen M. Cameron 
3806283b4a9bSStephen M. Cameron 	/* proceeding with driver mapping */
38072b08b3e9SDon Brace 	total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
38082b08b3e9SDon Brace 				le16_to_cpu(map->metadata_disks_per_row);
3809283b4a9bSStephen M. Cameron 	map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
38102b08b3e9SDon Brace 				le16_to_cpu(map->row_cnt);
38116b80b18fSScott Teel 	map_index = (map_row * total_disks_per_row) + first_column;
38126b80b18fSScott Teel 
38136b80b18fSScott Teel 	switch (dev->raid_level) {
38146b80b18fSScott Teel 	case HPSA_RAID_0:
38156b80b18fSScott Teel 		break; /* nothing special to do */
38166b80b18fSScott Teel 	case HPSA_RAID_1:
38176b80b18fSScott Teel 		/* Handles load balance across RAID 1 members.
38186b80b18fSScott Teel 		 * (2-drive R1 and R10 with even # of drives.)
38196b80b18fSScott Teel 		 * Appropriate for SSDs, not optimal for HDDs
3820283b4a9bSStephen M. Cameron 		 */
38212b08b3e9SDon Brace 		BUG_ON(le16_to_cpu(map->layout_map_count) != 2);
3822283b4a9bSStephen M. Cameron 		if (dev->offload_to_mirror)
38232b08b3e9SDon Brace 			map_index += le16_to_cpu(map->data_disks_per_row);
3824283b4a9bSStephen M. Cameron 		dev->offload_to_mirror = !dev->offload_to_mirror;
38256b80b18fSScott Teel 		break;
38266b80b18fSScott Teel 	case HPSA_RAID_ADM:
38276b80b18fSScott Teel 		/* Handles N-way mirrors  (R1-ADM)
38286b80b18fSScott Teel 		 * and R10 with # of drives divisible by 3.)
38296b80b18fSScott Teel 		 */
38302b08b3e9SDon Brace 		BUG_ON(le16_to_cpu(map->layout_map_count) != 3);
38316b80b18fSScott Teel 
38326b80b18fSScott Teel 		offload_to_mirror = dev->offload_to_mirror;
38336b80b18fSScott Teel 		raid_map_helper(map, offload_to_mirror,
38346b80b18fSScott Teel 				&map_index, &current_group);
38356b80b18fSScott Teel 		/* set mirror group to use next time */
38366b80b18fSScott Teel 		offload_to_mirror =
38372b08b3e9SDon Brace 			(offload_to_mirror >=
38382b08b3e9SDon Brace 			le16_to_cpu(map->layout_map_count) - 1)
38396b80b18fSScott Teel 			? 0 : offload_to_mirror + 1;
38406b80b18fSScott Teel 		dev->offload_to_mirror = offload_to_mirror;
38416b80b18fSScott Teel 		/* Avoid direct use of dev->offload_to_mirror within this
38426b80b18fSScott Teel 		 * function since multiple threads might simultaneously
38436b80b18fSScott Teel 		 * increment it beyond the range of dev->layout_map_count -1.
38446b80b18fSScott Teel 		 */
38456b80b18fSScott Teel 		break;
38466b80b18fSScott Teel 	case HPSA_RAID_5:
38476b80b18fSScott Teel 	case HPSA_RAID_6:
38482b08b3e9SDon Brace 		if (le16_to_cpu(map->layout_map_count) <= 1)
38496b80b18fSScott Teel 			break;
38506b80b18fSScott Teel 
38516b80b18fSScott Teel 		/* Verify first and last block are in same RAID group */
38526b80b18fSScott Teel 		r5or6_blocks_per_row =
38532b08b3e9SDon Brace 			le16_to_cpu(map->strip_size) *
38542b08b3e9SDon Brace 			le16_to_cpu(map->data_disks_per_row);
38556b80b18fSScott Teel 		BUG_ON(r5or6_blocks_per_row == 0);
38562b08b3e9SDon Brace 		stripesize = r5or6_blocks_per_row *
38572b08b3e9SDon Brace 			le16_to_cpu(map->layout_map_count);
38586b80b18fSScott Teel #if BITS_PER_LONG == 32
38596b80b18fSScott Teel 		tmpdiv = first_block;
38606b80b18fSScott Teel 		first_group = do_div(tmpdiv, stripesize);
38616b80b18fSScott Teel 		tmpdiv = first_group;
38626b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
38636b80b18fSScott Teel 		first_group = tmpdiv;
38646b80b18fSScott Teel 		tmpdiv = last_block;
38656b80b18fSScott Teel 		last_group = do_div(tmpdiv, stripesize);
38666b80b18fSScott Teel 		tmpdiv = last_group;
38676b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
38686b80b18fSScott Teel 		last_group = tmpdiv;
38696b80b18fSScott Teel #else
38706b80b18fSScott Teel 		first_group = (first_block % stripesize) / r5or6_blocks_per_row;
38716b80b18fSScott Teel 		last_group = (last_block % stripesize) / r5or6_blocks_per_row;
38726b80b18fSScott Teel #endif
3873000ff7c2SStephen M. Cameron 		if (first_group != last_group)
38746b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
38756b80b18fSScott Teel 
38766b80b18fSScott Teel 		/* Verify request is in a single row of RAID 5/6 */
38776b80b18fSScott Teel #if BITS_PER_LONG == 32
38786b80b18fSScott Teel 		tmpdiv = first_block;
38796b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
38806b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row = tmpdiv;
38816b80b18fSScott Teel 		tmpdiv = last_block;
38826b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
38836b80b18fSScott Teel 		r5or6_last_row = r0_last_row = tmpdiv;
38846b80b18fSScott Teel #else
38856b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row =
38866b80b18fSScott Teel 						first_block / stripesize;
38876b80b18fSScott Teel 		r5or6_last_row = r0_last_row = last_block / stripesize;
38886b80b18fSScott Teel #endif
38896b80b18fSScott Teel 		if (r5or6_first_row != r5or6_last_row)
38906b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
38916b80b18fSScott Teel 
38926b80b18fSScott Teel 
38936b80b18fSScott Teel 		/* Verify request is in a single column */
38946b80b18fSScott Teel #if BITS_PER_LONG == 32
38956b80b18fSScott Teel 		tmpdiv = first_block;
38966b80b18fSScott Teel 		first_row_offset = do_div(tmpdiv, stripesize);
38976b80b18fSScott Teel 		tmpdiv = first_row_offset;
38986b80b18fSScott Teel 		first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
38996b80b18fSScott Teel 		r5or6_first_row_offset = first_row_offset;
39006b80b18fSScott Teel 		tmpdiv = last_block;
39016b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, stripesize);
39026b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
39036b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
39046b80b18fSScott Teel 		tmpdiv = r5or6_first_row_offset;
39056b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
39066b80b18fSScott Teel 		first_column = r5or6_first_column = tmpdiv;
39076b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
39086b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
39096b80b18fSScott Teel 		r5or6_last_column = tmpdiv;
39106b80b18fSScott Teel #else
39116b80b18fSScott Teel 		first_row_offset = r5or6_first_row_offset =
39126b80b18fSScott Teel 			(u32)((first_block % stripesize) %
39136b80b18fSScott Teel 						r5or6_blocks_per_row);
39146b80b18fSScott Teel 
39156b80b18fSScott Teel 		r5or6_last_row_offset =
39166b80b18fSScott Teel 			(u32)((last_block % stripesize) %
39176b80b18fSScott Teel 						r5or6_blocks_per_row);
39186b80b18fSScott Teel 
39196b80b18fSScott Teel 		first_column = r5or6_first_column =
39202b08b3e9SDon Brace 			r5or6_first_row_offset / le16_to_cpu(map->strip_size);
39216b80b18fSScott Teel 		r5or6_last_column =
39222b08b3e9SDon Brace 			r5or6_last_row_offset / le16_to_cpu(map->strip_size);
39236b80b18fSScott Teel #endif
39246b80b18fSScott Teel 		if (r5or6_first_column != r5or6_last_column)
39256b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
39266b80b18fSScott Teel 
39276b80b18fSScott Teel 		/* Request is eligible */
39286b80b18fSScott Teel 		map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
39292b08b3e9SDon Brace 			le16_to_cpu(map->row_cnt);
39306b80b18fSScott Teel 
39316b80b18fSScott Teel 		map_index = (first_group *
39322b08b3e9SDon Brace 			(le16_to_cpu(map->row_cnt) * total_disks_per_row)) +
39336b80b18fSScott Teel 			(map_row * total_disks_per_row) + first_column;
39346b80b18fSScott Teel 		break;
39356b80b18fSScott Teel 	default:
39366b80b18fSScott Teel 		return IO_ACCEL_INELIGIBLE;
3937283b4a9bSStephen M. Cameron 	}
39386b80b18fSScott Teel 
393907543e0cSStephen Cameron 	if (unlikely(map_index >= RAID_MAP_MAX_ENTRIES))
394007543e0cSStephen Cameron 		return IO_ACCEL_INELIGIBLE;
394107543e0cSStephen Cameron 
394203383736SDon Brace 	c->phys_disk = dev->phys_disk[map_index];
394303383736SDon Brace 
3944283b4a9bSStephen M. Cameron 	disk_handle = dd[map_index].ioaccel_handle;
39452b08b3e9SDon Brace 	disk_block = le64_to_cpu(map->disk_starting_blk) +
39462b08b3e9SDon Brace 			first_row * le16_to_cpu(map->strip_size) +
39472b08b3e9SDon Brace 			(first_row_offset - first_column *
39482b08b3e9SDon Brace 			le16_to_cpu(map->strip_size));
3949283b4a9bSStephen M. Cameron 	disk_block_cnt = block_cnt;
3950283b4a9bSStephen M. Cameron 
3951283b4a9bSStephen M. Cameron 	/* handle differing logical/physical block sizes */
3952283b4a9bSStephen M. Cameron 	if (map->phys_blk_shift) {
3953283b4a9bSStephen M. Cameron 		disk_block <<= map->phys_blk_shift;
3954283b4a9bSStephen M. Cameron 		disk_block_cnt <<= map->phys_blk_shift;
3955283b4a9bSStephen M. Cameron 	}
3956283b4a9bSStephen M. Cameron 	BUG_ON(disk_block_cnt > 0xffff);
3957283b4a9bSStephen M. Cameron 
3958283b4a9bSStephen M. Cameron 	/* build the new CDB for the physical disk I/O */
3959283b4a9bSStephen M. Cameron 	if (disk_block > 0xffffffff) {
3960283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_16 : READ_16;
3961283b4a9bSStephen M. Cameron 		cdb[1] = 0;
3962283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 56);
3963283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 48);
3964283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 40);
3965283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block >> 32);
3966283b4a9bSStephen M. Cameron 		cdb[6] = (u8) (disk_block >> 24);
3967283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block >> 16);
3968283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block >> 8);
3969283b4a9bSStephen M. Cameron 		cdb[9] = (u8) (disk_block);
3970283b4a9bSStephen M. Cameron 		cdb[10] = (u8) (disk_block_cnt >> 24);
3971283b4a9bSStephen M. Cameron 		cdb[11] = (u8) (disk_block_cnt >> 16);
3972283b4a9bSStephen M. Cameron 		cdb[12] = (u8) (disk_block_cnt >> 8);
3973283b4a9bSStephen M. Cameron 		cdb[13] = (u8) (disk_block_cnt);
3974283b4a9bSStephen M. Cameron 		cdb[14] = 0;
3975283b4a9bSStephen M. Cameron 		cdb[15] = 0;
3976283b4a9bSStephen M. Cameron 		cdb_len = 16;
3977283b4a9bSStephen M. Cameron 	} else {
3978283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
3979283b4a9bSStephen M. Cameron 		cdb[1] = 0;
3980283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 24);
3981283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 16);
3982283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 8);
3983283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block);
3984283b4a9bSStephen M. Cameron 		cdb[6] = 0;
3985283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block_cnt >> 8);
3986283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block_cnt);
3987283b4a9bSStephen M. Cameron 		cdb[9] = 0;
3988283b4a9bSStephen M. Cameron 		cdb_len = 10;
3989283b4a9bSStephen M. Cameron 	}
3990283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
399103383736SDon Brace 						dev->scsi3addr,
399203383736SDon Brace 						dev->phys_disk[map_index]);
3993283b4a9bSStephen M. Cameron }
3994283b4a9bSStephen M. Cameron 
3995574f05d3SStephen Cameron /* Submit commands down the "normal" RAID stack path */
3996574f05d3SStephen Cameron static int hpsa_ciss_submit(struct ctlr_info *h,
3997574f05d3SStephen Cameron 	struct CommandList *c, struct scsi_cmnd *cmd,
3998574f05d3SStephen Cameron 	unsigned char scsi3addr[])
3999edd16368SStephen M. Cameron {
4000edd16368SStephen M. Cameron 	cmd->host_scribble = (unsigned char *) c;
4001edd16368SStephen M. Cameron 	c->cmd_type = CMD_SCSI;
4002edd16368SStephen M. Cameron 	c->scsi_cmd = cmd;
4003edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;  /* unused in simple mode */
4004edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
4005f2405db8SDon Brace 	c->Header.tag = cpu_to_le64((c->cmdindex << DIRECT_LOOKUP_SHIFT));
4006edd16368SStephen M. Cameron 
4007edd16368SStephen M. Cameron 	/* Fill in the request block... */
4008edd16368SStephen M. Cameron 
4009edd16368SStephen M. Cameron 	c->Request.Timeout = 0;
4010edd16368SStephen M. Cameron 	memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
4011edd16368SStephen M. Cameron 	BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
4012edd16368SStephen M. Cameron 	c->Request.CDBLen = cmd->cmd_len;
4013edd16368SStephen M. Cameron 	memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
4014edd16368SStephen M. Cameron 	switch (cmd->sc_data_direction) {
4015edd16368SStephen M. Cameron 	case DMA_TO_DEVICE:
4016a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4017a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_WRITE);
4018edd16368SStephen M. Cameron 		break;
4019edd16368SStephen M. Cameron 	case DMA_FROM_DEVICE:
4020a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4021a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_READ);
4022edd16368SStephen M. Cameron 		break;
4023edd16368SStephen M. Cameron 	case DMA_NONE:
4024a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4025a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_NONE);
4026edd16368SStephen M. Cameron 		break;
4027edd16368SStephen M. Cameron 	case DMA_BIDIRECTIONAL:
4028edd16368SStephen M. Cameron 		/* This can happen if a buggy application does a scsi passthru
4029edd16368SStephen M. Cameron 		 * and sets both inlen and outlen to non-zero. ( see
4030edd16368SStephen M. Cameron 		 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
4031edd16368SStephen M. Cameron 		 */
4032edd16368SStephen M. Cameron 
4033a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4034a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_RSVD);
4035edd16368SStephen M. Cameron 		/* This is technically wrong, and hpsa controllers should
4036edd16368SStephen M. Cameron 		 * reject it with CMD_INVALID, which is the most correct
4037edd16368SStephen M. Cameron 		 * response, but non-fibre backends appear to let it
4038edd16368SStephen M. Cameron 		 * slide by, and give the same results as if this field
4039edd16368SStephen M. Cameron 		 * were set correctly.  Either way is acceptable for
4040edd16368SStephen M. Cameron 		 * our purposes here.
4041edd16368SStephen M. Cameron 		 */
4042edd16368SStephen M. Cameron 
4043edd16368SStephen M. Cameron 		break;
4044edd16368SStephen M. Cameron 
4045edd16368SStephen M. Cameron 	default:
4046edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4047edd16368SStephen M. Cameron 			cmd->sc_data_direction);
4048edd16368SStephen M. Cameron 		BUG();
4049edd16368SStephen M. Cameron 		break;
4050edd16368SStephen M. Cameron 	}
4051edd16368SStephen M. Cameron 
405233a2ffceSStephen M. Cameron 	if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
4053edd16368SStephen M. Cameron 		cmd_free(h, c);
4054edd16368SStephen M. Cameron 		return SCSI_MLQUEUE_HOST_BUSY;
4055edd16368SStephen M. Cameron 	}
4056edd16368SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
4057edd16368SStephen M. Cameron 	/* the cmd'll come back via intr handler in complete_scsi_command()  */
4058edd16368SStephen M. Cameron 	return 0;
4059edd16368SStephen M. Cameron }
4060edd16368SStephen M. Cameron 
4061080ef1ccSDon Brace static void hpsa_command_resubmit_worker(struct work_struct *work)
4062080ef1ccSDon Brace {
4063080ef1ccSDon Brace 	struct scsi_cmnd *cmd;
4064080ef1ccSDon Brace 	struct hpsa_scsi_dev_t *dev;
4065080ef1ccSDon Brace 	struct CommandList *c =
4066080ef1ccSDon Brace 			container_of(work, struct CommandList, work);
4067080ef1ccSDon Brace 
4068080ef1ccSDon Brace 	cmd = c->scsi_cmd;
4069080ef1ccSDon Brace 	dev = cmd->device->hostdata;
4070080ef1ccSDon Brace 	if (!dev) {
4071080ef1ccSDon Brace 		cmd->result = DID_NO_CONNECT << 16;
4072080ef1ccSDon Brace 		cmd->scsi_done(cmd);
4073080ef1ccSDon Brace 		return;
4074080ef1ccSDon Brace 	}
4075080ef1ccSDon Brace 	if (hpsa_ciss_submit(c->h, c, cmd, dev->scsi3addr)) {
4076080ef1ccSDon Brace 		/*
4077080ef1ccSDon Brace 		 * If we get here, it means dma mapping failed. Try
4078080ef1ccSDon Brace 		 * again via scsi mid layer, which will then get
4079080ef1ccSDon Brace 		 * SCSI_MLQUEUE_HOST_BUSY.
4080080ef1ccSDon Brace 		 */
4081080ef1ccSDon Brace 		cmd->result = DID_IMM_RETRY << 16;
4082080ef1ccSDon Brace 		cmd->scsi_done(cmd);
4083080ef1ccSDon Brace 	}
4084080ef1ccSDon Brace }
4085080ef1ccSDon Brace 
4086574f05d3SStephen Cameron /* Running in struct Scsi_Host->host_lock less mode */
4087574f05d3SStephen Cameron static int hpsa_scsi_queue_command(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
4088574f05d3SStephen Cameron {
4089574f05d3SStephen Cameron 	struct ctlr_info *h;
4090574f05d3SStephen Cameron 	struct hpsa_scsi_dev_t *dev;
4091574f05d3SStephen Cameron 	unsigned char scsi3addr[8];
4092574f05d3SStephen Cameron 	struct CommandList *c;
4093574f05d3SStephen Cameron 	int rc = 0;
4094574f05d3SStephen Cameron 
4095574f05d3SStephen Cameron 	/* Get the ptr to our adapter structure out of cmd->host. */
4096574f05d3SStephen Cameron 	h = sdev_to_hba(cmd->device);
4097574f05d3SStephen Cameron 	dev = cmd->device->hostdata;
4098574f05d3SStephen Cameron 	if (!dev) {
4099574f05d3SStephen Cameron 		cmd->result = DID_NO_CONNECT << 16;
4100574f05d3SStephen Cameron 		cmd->scsi_done(cmd);
4101574f05d3SStephen Cameron 		return 0;
4102574f05d3SStephen Cameron 	}
4103574f05d3SStephen Cameron 	memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
4104574f05d3SStephen Cameron 
4105574f05d3SStephen Cameron 	if (unlikely(lockup_detected(h))) {
4106574f05d3SStephen Cameron 		cmd->result = DID_ERROR << 16;
4107574f05d3SStephen Cameron 		cmd->scsi_done(cmd);
4108574f05d3SStephen Cameron 		return 0;
4109574f05d3SStephen Cameron 	}
4110574f05d3SStephen Cameron 	c = cmd_alloc(h);
4111574f05d3SStephen Cameron 	if (c == NULL) {			/* trouble... */
4112574f05d3SStephen Cameron 		dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
4113574f05d3SStephen Cameron 		return SCSI_MLQUEUE_HOST_BUSY;
4114574f05d3SStephen Cameron 	}
4115407863cbSStephen Cameron 	if (unlikely(lockup_detected(h))) {
4116407863cbSStephen Cameron 		cmd->result = DID_ERROR << 16;
4117407863cbSStephen Cameron 		cmd_free(h, c);
4118407863cbSStephen Cameron 		cmd->scsi_done(cmd);
4119407863cbSStephen Cameron 		return 0;
4120407863cbSStephen Cameron 	}
4121574f05d3SStephen Cameron 
4122407863cbSStephen Cameron 	/*
4123407863cbSStephen Cameron 	 * Call alternate submit routine for I/O accelerated commands.
4124574f05d3SStephen Cameron 	 * Retries always go down the normal I/O path.
4125574f05d3SStephen Cameron 	 */
4126574f05d3SStephen Cameron 	if (likely(cmd->retries == 0 &&
4127574f05d3SStephen Cameron 		cmd->request->cmd_type == REQ_TYPE_FS &&
4128574f05d3SStephen Cameron 		h->acciopath_status)) {
4129574f05d3SStephen Cameron 
4130574f05d3SStephen Cameron 		cmd->host_scribble = (unsigned char *) c;
4131574f05d3SStephen Cameron 		c->cmd_type = CMD_SCSI;
4132574f05d3SStephen Cameron 		c->scsi_cmd = cmd;
4133574f05d3SStephen Cameron 
4134574f05d3SStephen Cameron 		if (dev->offload_enabled) {
4135574f05d3SStephen Cameron 			rc = hpsa_scsi_ioaccel_raid_map(h, c);
4136574f05d3SStephen Cameron 			if (rc == 0)
4137574f05d3SStephen Cameron 				return 0; /* Sent on ioaccel path */
4138574f05d3SStephen Cameron 			if (rc < 0) {   /* scsi_dma_map failed. */
4139574f05d3SStephen Cameron 				cmd_free(h, c);
4140574f05d3SStephen Cameron 				return SCSI_MLQUEUE_HOST_BUSY;
4141574f05d3SStephen Cameron 			}
4142574f05d3SStephen Cameron 		} else if (dev->ioaccel_handle) {
4143574f05d3SStephen Cameron 			rc = hpsa_scsi_ioaccel_direct_map(h, c);
4144574f05d3SStephen Cameron 			if (rc == 0)
4145574f05d3SStephen Cameron 				return 0; /* Sent on direct map path */
4146574f05d3SStephen Cameron 			if (rc < 0) {   /* scsi_dma_map failed. */
4147574f05d3SStephen Cameron 				cmd_free(h, c);
4148574f05d3SStephen Cameron 				return SCSI_MLQUEUE_HOST_BUSY;
4149574f05d3SStephen Cameron 			}
4150574f05d3SStephen Cameron 		}
4151574f05d3SStephen Cameron 	}
4152574f05d3SStephen Cameron 	return hpsa_ciss_submit(h, c, cmd, scsi3addr);
4153574f05d3SStephen Cameron }
4154574f05d3SStephen Cameron 
41558ebc9248SWebb Scales static void hpsa_scan_complete(struct ctlr_info *h)
41565f389360SStephen M. Cameron {
41575f389360SStephen M. Cameron 	unsigned long flags;
41585f389360SStephen M. Cameron 
41595f389360SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
41605f389360SStephen M. Cameron 	h->scan_finished = 1;
41615f389360SStephen M. Cameron 	wake_up_all(&h->scan_wait_queue);
41625f389360SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
41635f389360SStephen M. Cameron }
41645f389360SStephen M. Cameron 
4165a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *sh)
4166a08a8471SStephen M. Cameron {
4167a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
4168a08a8471SStephen M. Cameron 	unsigned long flags;
4169a08a8471SStephen M. Cameron 
41708ebc9248SWebb Scales 	/*
41718ebc9248SWebb Scales 	 * Don't let rescans be initiated on a controller known to be locked
41728ebc9248SWebb Scales 	 * up.  If the controller locks up *during* a rescan, that thread is
41738ebc9248SWebb Scales 	 * probably hosed, but at least we can prevent new rescan threads from
41748ebc9248SWebb Scales 	 * piling up on a locked up controller.
41758ebc9248SWebb Scales 	 */
41768ebc9248SWebb Scales 	if (unlikely(lockup_detected(h)))
41778ebc9248SWebb Scales 		return hpsa_scan_complete(h);
41785f389360SStephen M. Cameron 
4179a08a8471SStephen M. Cameron 	/* wait until any scan already in progress is finished. */
4180a08a8471SStephen M. Cameron 	while (1) {
4181a08a8471SStephen M. Cameron 		spin_lock_irqsave(&h->scan_lock, flags);
4182a08a8471SStephen M. Cameron 		if (h->scan_finished)
4183a08a8471SStephen M. Cameron 			break;
4184a08a8471SStephen M. Cameron 		spin_unlock_irqrestore(&h->scan_lock, flags);
4185a08a8471SStephen M. Cameron 		wait_event(h->scan_wait_queue, h->scan_finished);
4186a08a8471SStephen M. Cameron 		/* Note: We don't need to worry about a race between this
4187a08a8471SStephen M. Cameron 		 * thread and driver unload because the midlayer will
4188a08a8471SStephen M. Cameron 		 * have incremented the reference count, so unload won't
4189a08a8471SStephen M. Cameron 		 * happen if we're in here.
4190a08a8471SStephen M. Cameron 		 */
4191a08a8471SStephen M. Cameron 	}
4192a08a8471SStephen M. Cameron 	h->scan_finished = 0; /* mark scan as in progress */
4193a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4194a08a8471SStephen M. Cameron 
41958ebc9248SWebb Scales 	if (unlikely(lockup_detected(h)))
41968ebc9248SWebb Scales 		return hpsa_scan_complete(h);
41975f389360SStephen M. Cameron 
4198a08a8471SStephen M. Cameron 	hpsa_update_scsi_devices(h, h->scsi_host->host_no);
4199a08a8471SStephen M. Cameron 
42008ebc9248SWebb Scales 	hpsa_scan_complete(h);
4201a08a8471SStephen M. Cameron }
4202a08a8471SStephen M. Cameron 
42037c0a0229SDon Brace static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth)
42047c0a0229SDon Brace {
420503383736SDon Brace 	struct hpsa_scsi_dev_t *logical_drive = sdev->hostdata;
420603383736SDon Brace 
420703383736SDon Brace 	if (!logical_drive)
420803383736SDon Brace 		return -ENODEV;
42097c0a0229SDon Brace 
42107c0a0229SDon Brace 	if (qdepth < 1)
42117c0a0229SDon Brace 		qdepth = 1;
421203383736SDon Brace 	else if (qdepth > logical_drive->queue_depth)
421303383736SDon Brace 		qdepth = logical_drive->queue_depth;
421403383736SDon Brace 
421503383736SDon Brace 	return scsi_change_queue_depth(sdev, qdepth);
42167c0a0229SDon Brace }
42177c0a0229SDon Brace 
4218a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
4219a08a8471SStephen M. Cameron 	unsigned long elapsed_time)
4220a08a8471SStephen M. Cameron {
4221a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
4222a08a8471SStephen M. Cameron 	unsigned long flags;
4223a08a8471SStephen M. Cameron 	int finished;
4224a08a8471SStephen M. Cameron 
4225a08a8471SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
4226a08a8471SStephen M. Cameron 	finished = h->scan_finished;
4227a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4228a08a8471SStephen M. Cameron 	return finished;
4229a08a8471SStephen M. Cameron }
4230a08a8471SStephen M. Cameron 
4231edd16368SStephen M. Cameron static void hpsa_unregister_scsi(struct ctlr_info *h)
4232edd16368SStephen M. Cameron {
4233edd16368SStephen M. Cameron 	/* we are being forcibly unloaded, and may not refuse. */
4234edd16368SStephen M. Cameron 	scsi_remove_host(h->scsi_host);
4235edd16368SStephen M. Cameron 	scsi_host_put(h->scsi_host);
4236edd16368SStephen M. Cameron 	h->scsi_host = NULL;
4237edd16368SStephen M. Cameron }
4238edd16368SStephen M. Cameron 
4239edd16368SStephen M. Cameron static int hpsa_register_scsi(struct ctlr_info *h)
4240edd16368SStephen M. Cameron {
4241b705690dSStephen M. Cameron 	struct Scsi_Host *sh;
4242b705690dSStephen M. Cameron 	int error;
4243edd16368SStephen M. Cameron 
4244b705690dSStephen M. Cameron 	sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
4245b705690dSStephen M. Cameron 	if (sh == NULL)
4246b705690dSStephen M. Cameron 		goto fail;
4247b705690dSStephen M. Cameron 
4248b705690dSStephen M. Cameron 	sh->io_port = 0;
4249b705690dSStephen M. Cameron 	sh->n_io_port = 0;
4250b705690dSStephen M. Cameron 	sh->this_id = -1;
4251b705690dSStephen M. Cameron 	sh->max_channel = 3;
4252b705690dSStephen M. Cameron 	sh->max_cmd_len = MAX_COMMAND_SIZE;
4253b705690dSStephen M. Cameron 	sh->max_lun = HPSA_MAX_LUN;
4254b705690dSStephen M. Cameron 	sh->max_id = HPSA_MAX_LUN;
4255d54c5c24SStephen Cameron 	sh->can_queue = h->nr_cmds -
4256d54c5c24SStephen Cameron 			HPSA_CMDS_RESERVED_FOR_ABORTS -
4257d54c5c24SStephen Cameron 			HPSA_CMDS_RESERVED_FOR_DRIVER -
4258d54c5c24SStephen Cameron 			HPSA_MAX_CONCURRENT_PASSTHRUS;
4259d54c5c24SStephen Cameron 	sh->cmd_per_lun = sh->can_queue;
4260b705690dSStephen M. Cameron 	sh->sg_tablesize = h->maxsgentries;
4261b705690dSStephen M. Cameron 	h->scsi_host = sh;
4262b705690dSStephen M. Cameron 	sh->hostdata[0] = (unsigned long) h;
4263b705690dSStephen M. Cameron 	sh->irq = h->intr[h->intr_mode];
4264b705690dSStephen M. Cameron 	sh->unique_id = sh->irq;
4265b705690dSStephen M. Cameron 	error = scsi_add_host(sh, &h->pdev->dev);
4266b705690dSStephen M. Cameron 	if (error)
4267b705690dSStephen M. Cameron 		goto fail_host_put;
4268b705690dSStephen M. Cameron 	scsi_scan_host(sh);
4269b705690dSStephen M. Cameron 	return 0;
4270b705690dSStephen M. Cameron 
4271b705690dSStephen M. Cameron  fail_host_put:
4272b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_add_host"
4273b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
4274b705690dSStephen M. Cameron 	scsi_host_put(sh);
4275b705690dSStephen M. Cameron 	return error;
4276b705690dSStephen M. Cameron  fail:
4277b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_host_alloc"
4278b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
4279b705690dSStephen M. Cameron 	return -ENOMEM;
4280edd16368SStephen M. Cameron }
4281edd16368SStephen M. Cameron 
4282edd16368SStephen M. Cameron static int wait_for_device_to_become_ready(struct ctlr_info *h,
4283edd16368SStephen M. Cameron 	unsigned char lunaddr[])
4284edd16368SStephen M. Cameron {
42858919358eSTomas Henzl 	int rc;
4286edd16368SStephen M. Cameron 	int count = 0;
4287edd16368SStephen M. Cameron 	int waittime = 1; /* seconds */
4288edd16368SStephen M. Cameron 	struct CommandList *c;
4289edd16368SStephen M. Cameron 
429045fcb86eSStephen Cameron 	c = cmd_alloc(h);
4291edd16368SStephen M. Cameron 	if (!c) {
4292edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in "
4293edd16368SStephen M. Cameron 			"wait_for_device_to_become_ready.\n");
4294edd16368SStephen M. Cameron 		return IO_ERROR;
4295edd16368SStephen M. Cameron 	}
4296edd16368SStephen M. Cameron 
4297edd16368SStephen M. Cameron 	/* Send test unit ready until device ready, or give up. */
4298edd16368SStephen M. Cameron 	while (count < HPSA_TUR_RETRY_LIMIT) {
4299edd16368SStephen M. Cameron 
4300edd16368SStephen M. Cameron 		/* Wait for a bit.  do this first, because if we send
4301edd16368SStephen M. Cameron 		 * the TUR right away, the reset will just abort it.
4302edd16368SStephen M. Cameron 		 */
4303edd16368SStephen M. Cameron 		msleep(1000 * waittime);
4304edd16368SStephen M. Cameron 		count++;
43058919358eSTomas Henzl 		rc = 0; /* Device ready. */
4306edd16368SStephen M. Cameron 
4307edd16368SStephen M. Cameron 		/* Increase wait time with each try, up to a point. */
4308edd16368SStephen M. Cameron 		if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
4309edd16368SStephen M. Cameron 			waittime = waittime * 2;
4310edd16368SStephen M. Cameron 
4311a2dac136SStephen M. Cameron 		/* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
4312a2dac136SStephen M. Cameron 		(void) fill_cmd(c, TEST_UNIT_READY, h,
4313a2dac136SStephen M. Cameron 				NULL, 0, 0, lunaddr, TYPE_CMD);
4314edd16368SStephen M. Cameron 		hpsa_scsi_do_simple_cmd_core(h, c);
4315edd16368SStephen M. Cameron 		/* no unmap needed here because no data xfer. */
4316edd16368SStephen M. Cameron 
4317edd16368SStephen M. Cameron 		if (c->err_info->CommandStatus == CMD_SUCCESS)
4318edd16368SStephen M. Cameron 			break;
4319edd16368SStephen M. Cameron 
4320edd16368SStephen M. Cameron 		if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
4321edd16368SStephen M. Cameron 			c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
4322edd16368SStephen M. Cameron 			(c->err_info->SenseInfo[2] == NO_SENSE ||
4323edd16368SStephen M. Cameron 			c->err_info->SenseInfo[2] == UNIT_ATTENTION))
4324edd16368SStephen M. Cameron 			break;
4325edd16368SStephen M. Cameron 
4326edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "waiting %d secs "
4327edd16368SStephen M. Cameron 			"for device to become ready.\n", waittime);
4328edd16368SStephen M. Cameron 		rc = 1; /* device not ready. */
4329edd16368SStephen M. Cameron 	}
4330edd16368SStephen M. Cameron 
4331edd16368SStephen M. Cameron 	if (rc)
4332edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "giving up on device.\n");
4333edd16368SStephen M. Cameron 	else
4334edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "device is ready.\n");
4335edd16368SStephen M. Cameron 
433645fcb86eSStephen Cameron 	cmd_free(h, c);
4337edd16368SStephen M. Cameron 	return rc;
4338edd16368SStephen M. Cameron }
4339edd16368SStephen M. Cameron 
4340edd16368SStephen M. Cameron /* Need at least one of these error handlers to keep ../scsi/hosts.c from
4341edd16368SStephen M. Cameron  * complaining.  Doing a host- or bus-reset can't do anything good here.
4342edd16368SStephen M. Cameron  */
4343edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
4344edd16368SStephen M. Cameron {
4345edd16368SStephen M. Cameron 	int rc;
4346edd16368SStephen M. Cameron 	struct ctlr_info *h;
4347edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
4348edd16368SStephen M. Cameron 
4349edd16368SStephen M. Cameron 	/* find the controller to which the command to be aborted was sent */
4350edd16368SStephen M. Cameron 	h = sdev_to_hba(scsicmd->device);
4351edd16368SStephen M. Cameron 	if (h == NULL) /* paranoia */
4352edd16368SStephen M. Cameron 		return FAILED;
4353e345893bSDon Brace 
4354e345893bSDon Brace 	if (lockup_detected(h))
4355e345893bSDon Brace 		return FAILED;
4356e345893bSDon Brace 
4357edd16368SStephen M. Cameron 	dev = scsicmd->device->hostdata;
4358edd16368SStephen M. Cameron 	if (!dev) {
4359edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
4360edd16368SStephen M. Cameron 			"device lookup failed.\n");
4361edd16368SStephen M. Cameron 		return FAILED;
4362edd16368SStephen M. Cameron 	}
4363d416b0c7SStephen M. Cameron 	dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
4364d416b0c7SStephen M. Cameron 		h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
4365edd16368SStephen M. Cameron 	/* send a reset to the SCSI LUN which the command was sent to */
4366bf711ac6SScott Teel 	rc = hpsa_send_reset(h, dev->scsi3addr, HPSA_RESET_TYPE_LUN);
4367edd16368SStephen M. Cameron 	if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
4368edd16368SStephen M. Cameron 		return SUCCESS;
4369edd16368SStephen M. Cameron 
4370edd16368SStephen M. Cameron 	dev_warn(&h->pdev->dev, "resetting device failed.\n");
4371edd16368SStephen M. Cameron 	return FAILED;
4372edd16368SStephen M. Cameron }
4373edd16368SStephen M. Cameron 
43746cba3f19SStephen M. Cameron static void swizzle_abort_tag(u8 *tag)
43756cba3f19SStephen M. Cameron {
43766cba3f19SStephen M. Cameron 	u8 original_tag[8];
43776cba3f19SStephen M. Cameron 
43786cba3f19SStephen M. Cameron 	memcpy(original_tag, tag, 8);
43796cba3f19SStephen M. Cameron 	tag[0] = original_tag[3];
43806cba3f19SStephen M. Cameron 	tag[1] = original_tag[2];
43816cba3f19SStephen M. Cameron 	tag[2] = original_tag[1];
43826cba3f19SStephen M. Cameron 	tag[3] = original_tag[0];
43836cba3f19SStephen M. Cameron 	tag[4] = original_tag[7];
43846cba3f19SStephen M. Cameron 	tag[5] = original_tag[6];
43856cba3f19SStephen M. Cameron 	tag[6] = original_tag[5];
43866cba3f19SStephen M. Cameron 	tag[7] = original_tag[4];
43876cba3f19SStephen M. Cameron }
43886cba3f19SStephen M. Cameron 
438917eb87d2SScott Teel static void hpsa_get_tag(struct ctlr_info *h,
43902b08b3e9SDon Brace 	struct CommandList *c, __le32 *taglower, __le32 *tagupper)
439117eb87d2SScott Teel {
43922b08b3e9SDon Brace 	u64 tag;
439317eb87d2SScott Teel 	if (c->cmd_type == CMD_IOACCEL1) {
439417eb87d2SScott Teel 		struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
439517eb87d2SScott Teel 			&h->ioaccel_cmd_pool[c->cmdindex];
43962b08b3e9SDon Brace 		tag = le64_to_cpu(cm1->tag);
43972b08b3e9SDon Brace 		*tagupper = cpu_to_le32(tag >> 32);
43982b08b3e9SDon Brace 		*taglower = cpu_to_le32(tag);
439954b6e9e9SScott Teel 		return;
440054b6e9e9SScott Teel 	}
440154b6e9e9SScott Teel 	if (c->cmd_type == CMD_IOACCEL2) {
440254b6e9e9SScott Teel 		struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
440354b6e9e9SScott Teel 			&h->ioaccel2_cmd_pool[c->cmdindex];
4404dd0e19f3SScott Teel 		/* upper tag not used in ioaccel2 mode */
4405dd0e19f3SScott Teel 		memset(tagupper, 0, sizeof(*tagupper));
4406dd0e19f3SScott Teel 		*taglower = cm2->Tag;
440754b6e9e9SScott Teel 		return;
440854b6e9e9SScott Teel 	}
44092b08b3e9SDon Brace 	tag = le64_to_cpu(c->Header.tag);
44102b08b3e9SDon Brace 	*tagupper = cpu_to_le32(tag >> 32);
44112b08b3e9SDon Brace 	*taglower = cpu_to_le32(tag);
441217eb87d2SScott Teel }
441354b6e9e9SScott Teel 
441475167d2cSStephen M. Cameron static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
44156cba3f19SStephen M. Cameron 	struct CommandList *abort, int swizzle)
441675167d2cSStephen M. Cameron {
441775167d2cSStephen M. Cameron 	int rc = IO_OK;
441875167d2cSStephen M. Cameron 	struct CommandList *c;
441975167d2cSStephen M. Cameron 	struct ErrorInfo *ei;
44202b08b3e9SDon Brace 	__le32 tagupper, taglower;
442175167d2cSStephen M. Cameron 
442245fcb86eSStephen Cameron 	c = cmd_alloc(h);
442375167d2cSStephen M. Cameron 	if (c == NULL) {	/* trouble... */
442445fcb86eSStephen Cameron 		dev_warn(&h->pdev->dev, "cmd_alloc returned NULL!\n");
442575167d2cSStephen M. Cameron 		return -ENOMEM;
442675167d2cSStephen M. Cameron 	}
442775167d2cSStephen M. Cameron 
4428a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no buffer to map */
4429a2dac136SStephen M. Cameron 	(void) fill_cmd(c, HPSA_ABORT_MSG, h, abort,
4430a2dac136SStephen M. Cameron 		0, 0, scsi3addr, TYPE_MSG);
44316cba3f19SStephen M. Cameron 	if (swizzle)
44326cba3f19SStephen M. Cameron 		swizzle_abort_tag(&c->Request.CDB[4]);
443375167d2cSStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core(h, c);
443417eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
443575167d2cSStephen M. Cameron 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd_core completed.\n",
443617eb87d2SScott Teel 		__func__, tagupper, taglower);
443775167d2cSStephen M. Cameron 	/* no unmap needed here because no data xfer. */
443875167d2cSStephen M. Cameron 
443975167d2cSStephen M. Cameron 	ei = c->err_info;
444075167d2cSStephen M. Cameron 	switch (ei->CommandStatus) {
444175167d2cSStephen M. Cameron 	case CMD_SUCCESS:
444275167d2cSStephen M. Cameron 		break;
444375167d2cSStephen M. Cameron 	case CMD_UNABORTABLE: /* Very common, don't make noise. */
444475167d2cSStephen M. Cameron 		rc = -1;
444575167d2cSStephen M. Cameron 		break;
444675167d2cSStephen M. Cameron 	default:
444775167d2cSStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
444817eb87d2SScott Teel 			__func__, tagupper, taglower);
4449d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
445075167d2cSStephen M. Cameron 		rc = -1;
445175167d2cSStephen M. Cameron 		break;
445275167d2cSStephen M. Cameron 	}
445345fcb86eSStephen Cameron 	cmd_free(h, c);
4454dd0e19f3SScott Teel 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n",
4455dd0e19f3SScott Teel 		__func__, tagupper, taglower);
445675167d2cSStephen M. Cameron 	return rc;
445775167d2cSStephen M. Cameron }
445875167d2cSStephen M. Cameron 
445954b6e9e9SScott Teel /* ioaccel2 path firmware cannot handle abort task requests.
446054b6e9e9SScott Teel  * Change abort requests to physical target reset, and send to the
446154b6e9e9SScott Teel  * address of the physical disk used for the ioaccel 2 command.
446254b6e9e9SScott Teel  * Return 0 on success (IO_OK)
446354b6e9e9SScott Teel  *	 -1 on failure
446454b6e9e9SScott Teel  */
446554b6e9e9SScott Teel 
446654b6e9e9SScott Teel static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
446754b6e9e9SScott Teel 	unsigned char *scsi3addr, struct CommandList *abort)
446854b6e9e9SScott Teel {
446954b6e9e9SScott Teel 	int rc = IO_OK;
447054b6e9e9SScott Teel 	struct scsi_cmnd *scmd; /* scsi command within request being aborted */
447154b6e9e9SScott Teel 	struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
447254b6e9e9SScott Teel 	unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
447354b6e9e9SScott Teel 	unsigned char *psa = &phys_scsi3addr[0];
447454b6e9e9SScott Teel 
447554b6e9e9SScott Teel 	/* Get a pointer to the hpsa logical device. */
44767fa3030cSStephen Cameron 	scmd = abort->scsi_cmd;
447754b6e9e9SScott Teel 	dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
447854b6e9e9SScott Teel 	if (dev == NULL) {
447954b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
448054b6e9e9SScott Teel 			"Cannot abort: no device pointer for command.\n");
448154b6e9e9SScott Teel 			return -1; /* not abortable */
448254b6e9e9SScott Teel 	}
448354b6e9e9SScott Teel 
44842ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
44852ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
44862ba8bfc8SStephen M. Cameron 			"Reset as abort: Abort requested on C%d:B%d:T%d:L%d scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
44872ba8bfc8SStephen M. Cameron 			h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
44882ba8bfc8SStephen M. Cameron 			scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
44892ba8bfc8SStephen M. Cameron 			scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]);
44902ba8bfc8SStephen M. Cameron 
449154b6e9e9SScott Teel 	if (!dev->offload_enabled) {
449254b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
449354b6e9e9SScott Teel 			"Can't abort: device is not operating in HP SSD Smart Path mode.\n");
449454b6e9e9SScott Teel 		return -1; /* not abortable */
449554b6e9e9SScott Teel 	}
449654b6e9e9SScott Teel 
449754b6e9e9SScott Teel 	/* Incoming scsi3addr is logical addr. We need physical disk addr. */
449854b6e9e9SScott Teel 	if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
449954b6e9e9SScott Teel 		dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
450054b6e9e9SScott Teel 		return -1; /* not abortable */
450154b6e9e9SScott Teel 	}
450254b6e9e9SScott Teel 
450354b6e9e9SScott Teel 	/* send the reset */
45042ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
45052ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
45062ba8bfc8SStephen M. Cameron 			"Reset as abort: Resetting physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
45072ba8bfc8SStephen M. Cameron 			psa[0], psa[1], psa[2], psa[3],
45082ba8bfc8SStephen M. Cameron 			psa[4], psa[5], psa[6], psa[7]);
450954b6e9e9SScott Teel 	rc = hpsa_send_reset(h, psa, HPSA_RESET_TYPE_TARGET);
451054b6e9e9SScott Teel 	if (rc != 0) {
451154b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
451254b6e9e9SScott Teel 			"Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
451354b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
451454b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
451554b6e9e9SScott Teel 		return rc; /* failed to reset */
451654b6e9e9SScott Teel 	}
451754b6e9e9SScott Teel 
451854b6e9e9SScott Teel 	/* wait for device to recover */
451954b6e9e9SScott Teel 	if (wait_for_device_to_become_ready(h, psa) != 0) {
452054b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
452154b6e9e9SScott Teel 			"Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
452254b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
452354b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
452454b6e9e9SScott Teel 		return -1;  /* failed to recover */
452554b6e9e9SScott Teel 	}
452654b6e9e9SScott Teel 
452754b6e9e9SScott Teel 	/* device recovered */
452854b6e9e9SScott Teel 	dev_info(&h->pdev->dev,
452954b6e9e9SScott Teel 		"Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
453054b6e9e9SScott Teel 		psa[0], psa[1], psa[2], psa[3],
453154b6e9e9SScott Teel 		psa[4], psa[5], psa[6], psa[7]);
453254b6e9e9SScott Teel 
453354b6e9e9SScott Teel 	return rc; /* success */
453454b6e9e9SScott Teel }
453554b6e9e9SScott Teel 
45366cba3f19SStephen M. Cameron /* Some Smart Arrays need the abort tag swizzled, and some don't.  It's hard to
45376cba3f19SStephen M. Cameron  * tell which kind we're dealing with, so we send the abort both ways.  There
45386cba3f19SStephen M. Cameron  * shouldn't be any collisions between swizzled and unswizzled tags due to the
45396cba3f19SStephen M. Cameron  * way we construct our tags but we check anyway in case the assumptions which
45406cba3f19SStephen M. Cameron  * make this true someday become false.
45416cba3f19SStephen M. Cameron  */
45426cba3f19SStephen M. Cameron static int hpsa_send_abort_both_ways(struct ctlr_info *h,
45436cba3f19SStephen M. Cameron 	unsigned char *scsi3addr, struct CommandList *abort)
45446cba3f19SStephen M. Cameron {
454554b6e9e9SScott Teel 	/* ioccelerator mode 2 commands should be aborted via the
454654b6e9e9SScott Teel 	 * accelerated path, since RAID path is unaware of these commands,
454754b6e9e9SScott Teel 	 * but underlying firmware can't handle abort TMF.
454854b6e9e9SScott Teel 	 * Change abort to physical device reset.
454954b6e9e9SScott Teel 	 */
455054b6e9e9SScott Teel 	if (abort->cmd_type == CMD_IOACCEL2)
455154b6e9e9SScott Teel 		return hpsa_send_reset_as_abort_ioaccel2(h, scsi3addr, abort);
455254b6e9e9SScott Teel 
4553f2405db8SDon Brace 	return hpsa_send_abort(h, scsi3addr, abort, 0) &&
4554f2405db8SDon Brace 			hpsa_send_abort(h, scsi3addr, abort, 1);
45556cba3f19SStephen M. Cameron }
45566cba3f19SStephen M. Cameron 
455775167d2cSStephen M. Cameron /* Send an abort for the specified command.
455875167d2cSStephen M. Cameron  *	If the device and controller support it,
455975167d2cSStephen M. Cameron  *		send a task abort request.
456075167d2cSStephen M. Cameron  */
456175167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
456275167d2cSStephen M. Cameron {
456375167d2cSStephen M. Cameron 
456475167d2cSStephen M. Cameron 	int i, rc;
456575167d2cSStephen M. Cameron 	struct ctlr_info *h;
456675167d2cSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
456775167d2cSStephen M. Cameron 	struct CommandList *abort; /* pointer to command to be aborted */
456875167d2cSStephen M. Cameron 	struct scsi_cmnd *as;	/* ptr to scsi cmd inside aborted command. */
456975167d2cSStephen M. Cameron 	char msg[256];		/* For debug messaging. */
457075167d2cSStephen M. Cameron 	int ml = 0;
45712b08b3e9SDon Brace 	__le32 tagupper, taglower;
4572281a7fd0SWebb Scales 	int refcount;
457375167d2cSStephen M. Cameron 
457475167d2cSStephen M. Cameron 	/* Find the controller of the command to be aborted */
457575167d2cSStephen M. Cameron 	h = sdev_to_hba(sc->device);
457675167d2cSStephen M. Cameron 	if (WARN(h == NULL,
457775167d2cSStephen M. Cameron 			"ABORT REQUEST FAILED, Controller lookup failed.\n"))
457875167d2cSStephen M. Cameron 		return FAILED;
457975167d2cSStephen M. Cameron 
4580e345893bSDon Brace 	if (lockup_detected(h))
4581e345893bSDon Brace 		return FAILED;
4582e345893bSDon Brace 
458375167d2cSStephen M. Cameron 	/* Check that controller supports some kind of task abort */
458475167d2cSStephen M. Cameron 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
458575167d2cSStephen M. Cameron 		!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
458675167d2cSStephen M. Cameron 		return FAILED;
458775167d2cSStephen M. Cameron 
458875167d2cSStephen M. Cameron 	memset(msg, 0, sizeof(msg));
45899cb78c16SHannes Reinecke 	ml += sprintf(msg+ml, "ABORT REQUEST on C%d:B%d:T%d:L%llu ",
459075167d2cSStephen M. Cameron 		h->scsi_host->host_no, sc->device->channel,
459175167d2cSStephen M. Cameron 		sc->device->id, sc->device->lun);
459275167d2cSStephen M. Cameron 
459375167d2cSStephen M. Cameron 	/* Find the device of the command to be aborted */
459475167d2cSStephen M. Cameron 	dev = sc->device->hostdata;
459575167d2cSStephen M. Cameron 	if (!dev) {
459675167d2cSStephen M. Cameron 		dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
459775167d2cSStephen M. Cameron 				msg);
459875167d2cSStephen M. Cameron 		return FAILED;
459975167d2cSStephen M. Cameron 	}
460075167d2cSStephen M. Cameron 
460175167d2cSStephen M. Cameron 	/* Get SCSI command to be aborted */
460275167d2cSStephen M. Cameron 	abort = (struct CommandList *) sc->host_scribble;
460375167d2cSStephen M. Cameron 	if (abort == NULL) {
4604281a7fd0SWebb Scales 		/* This can happen if the command already completed. */
4605281a7fd0SWebb Scales 		return SUCCESS;
4606281a7fd0SWebb Scales 	}
4607281a7fd0SWebb Scales 	refcount = atomic_inc_return(&abort->refcount);
4608281a7fd0SWebb Scales 	if (refcount == 1) { /* Command is done already. */
4609281a7fd0SWebb Scales 		cmd_free(h, abort);
4610281a7fd0SWebb Scales 		return SUCCESS;
461175167d2cSStephen M. Cameron 	}
461217eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
461317eb87d2SScott Teel 	ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
46147fa3030cSStephen Cameron 	as  = abort->scsi_cmd;
461575167d2cSStephen M. Cameron 	if (as != NULL)
461675167d2cSStephen M. Cameron 		ml += sprintf(msg+ml, "Command:0x%x SN:0x%lx ",
461775167d2cSStephen M. Cameron 			as->cmnd[0], as->serial_number);
461875167d2cSStephen M. Cameron 	dev_dbg(&h->pdev->dev, "%s\n", msg);
461975167d2cSStephen M. Cameron 	dev_warn(&h->pdev->dev, "Abort request on C%d:B%d:T%d:L%d\n",
462075167d2cSStephen M. Cameron 		h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
462175167d2cSStephen M. Cameron 	/*
462275167d2cSStephen M. Cameron 	 * Command is in flight, or possibly already completed
462375167d2cSStephen M. Cameron 	 * by the firmware (but not to the scsi mid layer) but we can't
462475167d2cSStephen M. Cameron 	 * distinguish which.  Send the abort down.
462575167d2cSStephen M. Cameron 	 */
46266cba3f19SStephen M. Cameron 	rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort);
462775167d2cSStephen M. Cameron 	if (rc != 0) {
462875167d2cSStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s Request FAILED.\n", msg);
462975167d2cSStephen M. Cameron 		dev_warn(&h->pdev->dev, "FAILED abort on device C%d:B%d:T%d:L%d\n",
463075167d2cSStephen M. Cameron 			h->scsi_host->host_no,
463175167d2cSStephen M. Cameron 			dev->bus, dev->target, dev->lun);
4632281a7fd0SWebb Scales 		cmd_free(h, abort);
463375167d2cSStephen M. Cameron 		return FAILED;
463475167d2cSStephen M. Cameron 	}
463575167d2cSStephen M. Cameron 	dev_info(&h->pdev->dev, "%s REQUEST SUCCEEDED.\n", msg);
463675167d2cSStephen M. Cameron 
463775167d2cSStephen M. Cameron 	/* If the abort(s) above completed and actually aborted the
463875167d2cSStephen M. Cameron 	 * command, then the command to be aborted should already be
463975167d2cSStephen M. Cameron 	 * completed.  If not, wait around a bit more to see if they
464075167d2cSStephen M. Cameron 	 * manage to complete normally.
464175167d2cSStephen M. Cameron 	 */
464275167d2cSStephen M. Cameron #define ABORT_COMPLETE_WAIT_SECS 30
464375167d2cSStephen M. Cameron 	for (i = 0; i < ABORT_COMPLETE_WAIT_SECS * 10; i++) {
4644281a7fd0SWebb Scales 		refcount = atomic_read(&abort->refcount);
4645281a7fd0SWebb Scales 		if (refcount < 2) {
4646281a7fd0SWebb Scales 			cmd_free(h, abort);
4647f2405db8SDon Brace 			return SUCCESS;
4648281a7fd0SWebb Scales 		} else {
4649281a7fd0SWebb Scales 			msleep(100);
4650281a7fd0SWebb Scales 		}
465175167d2cSStephen M. Cameron 	}
465275167d2cSStephen M. Cameron 	dev_warn(&h->pdev->dev, "%s FAILED. Aborted command has not completed after %d seconds.\n",
465375167d2cSStephen M. Cameron 		msg, ABORT_COMPLETE_WAIT_SECS);
4654281a7fd0SWebb Scales 	cmd_free(h, abort);
465575167d2cSStephen M. Cameron 	return FAILED;
465675167d2cSStephen M. Cameron }
465775167d2cSStephen M. Cameron 
4658edd16368SStephen M. Cameron /*
4659edd16368SStephen M. Cameron  * For operations that cannot sleep, a command block is allocated at init,
4660edd16368SStephen M. Cameron  * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
4661edd16368SStephen M. Cameron  * which ones are free or in use.  Lock must be held when calling this.
4662edd16368SStephen M. Cameron  * cmd_free() is the complement.
4663edd16368SStephen M. Cameron  */
4664281a7fd0SWebb Scales 
4665edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h)
4666edd16368SStephen M. Cameron {
4667edd16368SStephen M. Cameron 	struct CommandList *c;
4668edd16368SStephen M. Cameron 	int i;
4669edd16368SStephen M. Cameron 	union u64bit temp64;
4670edd16368SStephen M. Cameron 	dma_addr_t cmd_dma_handle, err_dma_handle;
4671281a7fd0SWebb Scales 	int refcount;
467233811026SRobert Elliott 	unsigned long offset;
4673edd16368SStephen M. Cameron 
467433811026SRobert Elliott 	/*
467533811026SRobert Elliott 	 * There is some *extremely* small but non-zero chance that that
46764c413128SStephen M. Cameron 	 * multiple threads could get in here, and one thread could
46774c413128SStephen M. Cameron 	 * be scanning through the list of bits looking for a free
46784c413128SStephen M. Cameron 	 * one, but the free ones are always behind him, and other
46794c413128SStephen M. Cameron 	 * threads sneak in behind him and eat them before he can
46804c413128SStephen M. Cameron 	 * get to them, so that while there is always a free one, a
46814c413128SStephen M. Cameron 	 * very unlucky thread might be starved anyway, never able to
46824c413128SStephen M. Cameron 	 * beat the other threads.  In reality, this happens so
46834c413128SStephen M. Cameron 	 * infrequently as to be indistinguishable from never.
46844c413128SStephen M. Cameron 	 */
46854c413128SStephen M. Cameron 
468633811026SRobert Elliott 	offset = h->last_allocation; /* benignly racy */
4687281a7fd0SWebb Scales 	for (;;) {
4688281a7fd0SWebb Scales 		i = find_next_zero_bit(h->cmd_pool_bits, h->nr_cmds, offset);
4689281a7fd0SWebb Scales 		if (unlikely(i == h->nr_cmds)) {
4690281a7fd0SWebb Scales 			offset = 0;
4691281a7fd0SWebb Scales 			continue;
4692281a7fd0SWebb Scales 		}
4693edd16368SStephen M. Cameron 		c = h->cmd_pool + i;
4694281a7fd0SWebb Scales 		refcount = atomic_inc_return(&c->refcount);
4695281a7fd0SWebb Scales 		if (unlikely(refcount > 1)) {
4696281a7fd0SWebb Scales 			cmd_free(h, c); /* already in use */
4697281a7fd0SWebb Scales 			offset = (i + 1) % h->nr_cmds;
4698281a7fd0SWebb Scales 			continue;
4699281a7fd0SWebb Scales 		}
4700281a7fd0SWebb Scales 		set_bit(i & (BITS_PER_LONG - 1),
4701281a7fd0SWebb Scales 			h->cmd_pool_bits + (i / BITS_PER_LONG));
4702281a7fd0SWebb Scales 		break; /* it's ours now. */
4703281a7fd0SWebb Scales 	}
470433811026SRobert Elliott 	h->last_allocation = i; /* benignly racy */
4705281a7fd0SWebb Scales 
4706281a7fd0SWebb Scales 	/* Zero out all of commandlist except the last field, refcount */
4707281a7fd0SWebb Scales 	memset(c, 0, offsetof(struct CommandList, refcount));
4708281a7fd0SWebb Scales 	c->Header.tag = cpu_to_le64((u64) (i << DIRECT_LOOKUP_SHIFT));
4709f2405db8SDon Brace 	cmd_dma_handle = h->cmd_pool_dhandle + i * sizeof(*c);
4710edd16368SStephen M. Cameron 	c->err_info = h->errinfo_pool + i;
4711edd16368SStephen M. Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
4712edd16368SStephen M. Cameron 	err_dma_handle = h->errinfo_pool_dhandle
4713edd16368SStephen M. Cameron 	    + i * sizeof(*c->err_info);
4714edd16368SStephen M. Cameron 
4715edd16368SStephen M. Cameron 	c->cmdindex = i;
4716edd16368SStephen M. Cameron 
471701a02ffcSStephen M. Cameron 	c->busaddr = (u32) cmd_dma_handle;
471801a02ffcSStephen M. Cameron 	temp64.val = (u64) err_dma_handle;
4719281a7fd0SWebb Scales 	c->ErrDesc.Addr = cpu_to_le64((u64) err_dma_handle);
4720281a7fd0SWebb Scales 	c->ErrDesc.Len = cpu_to_le32((u32) sizeof(*c->err_info));
4721edd16368SStephen M. Cameron 
4722edd16368SStephen M. Cameron 	c->h = h;
4723edd16368SStephen M. Cameron 	return c;
4724edd16368SStephen M. Cameron }
4725edd16368SStephen M. Cameron 
4726edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c)
4727edd16368SStephen M. Cameron {
4728281a7fd0SWebb Scales 	if (atomic_dec_and_test(&c->refcount)) {
4729edd16368SStephen M. Cameron 		int i;
4730edd16368SStephen M. Cameron 
4731edd16368SStephen M. Cameron 		i = c - h->cmd_pool;
4732edd16368SStephen M. Cameron 		clear_bit(i & (BITS_PER_LONG - 1),
4733edd16368SStephen M. Cameron 			  h->cmd_pool_bits + (i / BITS_PER_LONG));
4734edd16368SStephen M. Cameron 	}
4735281a7fd0SWebb Scales }
4736edd16368SStephen M. Cameron 
4737edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
4738edd16368SStephen M. Cameron 
473942a91641SDon Brace static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd,
474042a91641SDon Brace 	void __user *arg)
4741edd16368SStephen M. Cameron {
4742edd16368SStephen M. Cameron 	IOCTL32_Command_struct __user *arg32 =
4743edd16368SStephen M. Cameron 	    (IOCTL32_Command_struct __user *) arg;
4744edd16368SStephen M. Cameron 	IOCTL_Command_struct arg64;
4745edd16368SStephen M. Cameron 	IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
4746edd16368SStephen M. Cameron 	int err;
4747edd16368SStephen M. Cameron 	u32 cp;
4748edd16368SStephen M. Cameron 
4749938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
4750edd16368SStephen M. Cameron 	err = 0;
4751edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
4752edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
4753edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
4754edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
4755edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
4756edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
4757edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
4758edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
4759edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
4760edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
4761edd16368SStephen M. Cameron 
4762edd16368SStephen M. Cameron 	if (err)
4763edd16368SStephen M. Cameron 		return -EFAULT;
4764edd16368SStephen M. Cameron 
476542a91641SDon Brace 	err = hpsa_ioctl(dev, CCISS_PASSTHRU, p);
4766edd16368SStephen M. Cameron 	if (err)
4767edd16368SStephen M. Cameron 		return err;
4768edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
4769edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
4770edd16368SStephen M. Cameron 	if (err)
4771edd16368SStephen M. Cameron 		return -EFAULT;
4772edd16368SStephen M. Cameron 	return err;
4773edd16368SStephen M. Cameron }
4774edd16368SStephen M. Cameron 
4775edd16368SStephen M. Cameron static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
477642a91641SDon Brace 	int cmd, void __user *arg)
4777edd16368SStephen M. Cameron {
4778edd16368SStephen M. Cameron 	BIG_IOCTL32_Command_struct __user *arg32 =
4779edd16368SStephen M. Cameron 	    (BIG_IOCTL32_Command_struct __user *) arg;
4780edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct arg64;
4781edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct __user *p =
4782edd16368SStephen M. Cameron 	    compat_alloc_user_space(sizeof(arg64));
4783edd16368SStephen M. Cameron 	int err;
4784edd16368SStephen M. Cameron 	u32 cp;
4785edd16368SStephen M. Cameron 
4786938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
4787edd16368SStephen M. Cameron 	err = 0;
4788edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
4789edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
4790edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
4791edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
4792edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
4793edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
4794edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
4795edd16368SStephen M. Cameron 	err |= get_user(arg64.malloc_size, &arg32->malloc_size);
4796edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
4797edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
4798edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
4799edd16368SStephen M. Cameron 
4800edd16368SStephen M. Cameron 	if (err)
4801edd16368SStephen M. Cameron 		return -EFAULT;
4802edd16368SStephen M. Cameron 
480342a91641SDon Brace 	err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, p);
4804edd16368SStephen M. Cameron 	if (err)
4805edd16368SStephen M. Cameron 		return err;
4806edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
4807edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
4808edd16368SStephen M. Cameron 	if (err)
4809edd16368SStephen M. Cameron 		return -EFAULT;
4810edd16368SStephen M. Cameron 	return err;
4811edd16368SStephen M. Cameron }
481271fe75a7SStephen M. Cameron 
481342a91641SDon Brace static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
481471fe75a7SStephen M. Cameron {
481571fe75a7SStephen M. Cameron 	switch (cmd) {
481671fe75a7SStephen M. Cameron 	case CCISS_GETPCIINFO:
481771fe75a7SStephen M. Cameron 	case CCISS_GETINTINFO:
481871fe75a7SStephen M. Cameron 	case CCISS_SETINTINFO:
481971fe75a7SStephen M. Cameron 	case CCISS_GETNODENAME:
482071fe75a7SStephen M. Cameron 	case CCISS_SETNODENAME:
482171fe75a7SStephen M. Cameron 	case CCISS_GETHEARTBEAT:
482271fe75a7SStephen M. Cameron 	case CCISS_GETBUSTYPES:
482371fe75a7SStephen M. Cameron 	case CCISS_GETFIRMVER:
482471fe75a7SStephen M. Cameron 	case CCISS_GETDRIVVER:
482571fe75a7SStephen M. Cameron 	case CCISS_REVALIDVOLS:
482671fe75a7SStephen M. Cameron 	case CCISS_DEREGDISK:
482771fe75a7SStephen M. Cameron 	case CCISS_REGNEWDISK:
482871fe75a7SStephen M. Cameron 	case CCISS_REGNEWD:
482971fe75a7SStephen M. Cameron 	case CCISS_RESCANDISK:
483071fe75a7SStephen M. Cameron 	case CCISS_GETLUNINFO:
483171fe75a7SStephen M. Cameron 		return hpsa_ioctl(dev, cmd, arg);
483271fe75a7SStephen M. Cameron 
483371fe75a7SStephen M. Cameron 	case CCISS_PASSTHRU32:
483471fe75a7SStephen M. Cameron 		return hpsa_ioctl32_passthru(dev, cmd, arg);
483571fe75a7SStephen M. Cameron 	case CCISS_BIG_PASSTHRU32:
483671fe75a7SStephen M. Cameron 		return hpsa_ioctl32_big_passthru(dev, cmd, arg);
483771fe75a7SStephen M. Cameron 
483871fe75a7SStephen M. Cameron 	default:
483971fe75a7SStephen M. Cameron 		return -ENOIOCTLCMD;
484071fe75a7SStephen M. Cameron 	}
484171fe75a7SStephen M. Cameron }
4842edd16368SStephen M. Cameron #endif
4843edd16368SStephen M. Cameron 
4844edd16368SStephen M. Cameron static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
4845edd16368SStephen M. Cameron {
4846edd16368SStephen M. Cameron 	struct hpsa_pci_info pciinfo;
4847edd16368SStephen M. Cameron 
4848edd16368SStephen M. Cameron 	if (!argp)
4849edd16368SStephen M. Cameron 		return -EINVAL;
4850edd16368SStephen M. Cameron 	pciinfo.domain = pci_domain_nr(h->pdev->bus);
4851edd16368SStephen M. Cameron 	pciinfo.bus = h->pdev->bus->number;
4852edd16368SStephen M. Cameron 	pciinfo.dev_fn = h->pdev->devfn;
4853edd16368SStephen M. Cameron 	pciinfo.board_id = h->board_id;
4854edd16368SStephen M. Cameron 	if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
4855edd16368SStephen M. Cameron 		return -EFAULT;
4856edd16368SStephen M. Cameron 	return 0;
4857edd16368SStephen M. Cameron }
4858edd16368SStephen M. Cameron 
4859edd16368SStephen M. Cameron static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
4860edd16368SStephen M. Cameron {
4861edd16368SStephen M. Cameron 	DriverVer_type DriverVer;
4862edd16368SStephen M. Cameron 	unsigned char vmaj, vmin, vsubmin;
4863edd16368SStephen M. Cameron 	int rc;
4864edd16368SStephen M. Cameron 
4865edd16368SStephen M. Cameron 	rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
4866edd16368SStephen M. Cameron 		&vmaj, &vmin, &vsubmin);
4867edd16368SStephen M. Cameron 	if (rc != 3) {
4868edd16368SStephen M. Cameron 		dev_info(&h->pdev->dev, "driver version string '%s' "
4869edd16368SStephen M. Cameron 			"unrecognized.", HPSA_DRIVER_VERSION);
4870edd16368SStephen M. Cameron 		vmaj = 0;
4871edd16368SStephen M. Cameron 		vmin = 0;
4872edd16368SStephen M. Cameron 		vsubmin = 0;
4873edd16368SStephen M. Cameron 	}
4874edd16368SStephen M. Cameron 	DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
4875edd16368SStephen M. Cameron 	if (!argp)
4876edd16368SStephen M. Cameron 		return -EINVAL;
4877edd16368SStephen M. Cameron 	if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
4878edd16368SStephen M. Cameron 		return -EFAULT;
4879edd16368SStephen M. Cameron 	return 0;
4880edd16368SStephen M. Cameron }
4881edd16368SStephen M. Cameron 
4882edd16368SStephen M. Cameron static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
4883edd16368SStephen M. Cameron {
4884edd16368SStephen M. Cameron 	IOCTL_Command_struct iocommand;
4885edd16368SStephen M. Cameron 	struct CommandList *c;
4886edd16368SStephen M. Cameron 	char *buff = NULL;
488750a0decfSStephen M. Cameron 	u64 temp64;
4888c1f63c8fSStephen M. Cameron 	int rc = 0;
4889edd16368SStephen M. Cameron 
4890edd16368SStephen M. Cameron 	if (!argp)
4891edd16368SStephen M. Cameron 		return -EINVAL;
4892edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
4893edd16368SStephen M. Cameron 		return -EPERM;
4894edd16368SStephen M. Cameron 	if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
4895edd16368SStephen M. Cameron 		return -EFAULT;
4896edd16368SStephen M. Cameron 	if ((iocommand.buf_size < 1) &&
4897edd16368SStephen M. Cameron 	    (iocommand.Request.Type.Direction != XFER_NONE)) {
4898edd16368SStephen M. Cameron 		return -EINVAL;
4899edd16368SStephen M. Cameron 	}
4900edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
4901edd16368SStephen M. Cameron 		buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
4902edd16368SStephen M. Cameron 		if (buff == NULL)
4903edd16368SStephen M. Cameron 			return -EFAULT;
49049233fb10SStephen M. Cameron 		if (iocommand.Request.Type.Direction & XFER_WRITE) {
4905edd16368SStephen M. Cameron 			/* Copy the data into the buffer we created */
4906b03a7771SStephen M. Cameron 			if (copy_from_user(buff, iocommand.buf,
4907b03a7771SStephen M. Cameron 				iocommand.buf_size)) {
4908c1f63c8fSStephen M. Cameron 				rc = -EFAULT;
4909c1f63c8fSStephen M. Cameron 				goto out_kfree;
4910edd16368SStephen M. Cameron 			}
4911b03a7771SStephen M. Cameron 		} else {
4912edd16368SStephen M. Cameron 			memset(buff, 0, iocommand.buf_size);
4913b03a7771SStephen M. Cameron 		}
4914b03a7771SStephen M. Cameron 	}
491545fcb86eSStephen Cameron 	c = cmd_alloc(h);
4916edd16368SStephen M. Cameron 	if (c == NULL) {
4917c1f63c8fSStephen M. Cameron 		rc = -ENOMEM;
4918c1f63c8fSStephen M. Cameron 		goto out_kfree;
4919edd16368SStephen M. Cameron 	}
4920edd16368SStephen M. Cameron 	/* Fill in the command type */
4921edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
4922edd16368SStephen M. Cameron 	/* Fill in Command Header */
4923edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0; /* unused in simple mode */
4924edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {	/* buffer to fill */
4925edd16368SStephen M. Cameron 		c->Header.SGList = 1;
492650a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(1);
4927edd16368SStephen M. Cameron 	} else	{ /* no buffers to fill */
4928edd16368SStephen M. Cameron 		c->Header.SGList = 0;
492950a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(0);
4930edd16368SStephen M. Cameron 	}
4931edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
4932edd16368SStephen M. Cameron 
4933edd16368SStephen M. Cameron 	/* Fill in Request block */
4934edd16368SStephen M. Cameron 	memcpy(&c->Request, &iocommand.Request,
4935edd16368SStephen M. Cameron 		sizeof(c->Request));
4936edd16368SStephen M. Cameron 
4937edd16368SStephen M. Cameron 	/* Fill in the scatter gather information */
4938edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
493950a0decfSStephen M. Cameron 		temp64 = pci_map_single(h->pdev, buff,
4940edd16368SStephen M. Cameron 			iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
494150a0decfSStephen M. Cameron 		if (dma_mapping_error(&h->pdev->dev, (dma_addr_t) temp64)) {
494250a0decfSStephen M. Cameron 			c->SG[0].Addr = cpu_to_le64(0);
494350a0decfSStephen M. Cameron 			c->SG[0].Len = cpu_to_le32(0);
4944bcc48ffaSStephen M. Cameron 			rc = -ENOMEM;
4945bcc48ffaSStephen M. Cameron 			goto out;
4946bcc48ffaSStephen M. Cameron 		}
494750a0decfSStephen M. Cameron 		c->SG[0].Addr = cpu_to_le64(temp64);
494850a0decfSStephen M. Cameron 		c->SG[0].Len = cpu_to_le32(iocommand.buf_size);
494950a0decfSStephen M. Cameron 		c->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* not chaining */
4950edd16368SStephen M. Cameron 	}
4951a0c12413SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
4952c2dd32e0SStephen M. Cameron 	if (iocommand.buf_size > 0)
4953edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
4954edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
4955edd16368SStephen M. Cameron 
4956edd16368SStephen M. Cameron 	/* Copy the error information out */
4957edd16368SStephen M. Cameron 	memcpy(&iocommand.error_info, c->err_info,
4958edd16368SStephen M. Cameron 		sizeof(iocommand.error_info));
4959edd16368SStephen M. Cameron 	if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
4960c1f63c8fSStephen M. Cameron 		rc = -EFAULT;
4961c1f63c8fSStephen M. Cameron 		goto out;
4962edd16368SStephen M. Cameron 	}
49639233fb10SStephen M. Cameron 	if ((iocommand.Request.Type.Direction & XFER_READ) &&
4964b03a7771SStephen M. Cameron 		iocommand.buf_size > 0) {
4965edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
4966edd16368SStephen M. Cameron 		if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
4967c1f63c8fSStephen M. Cameron 			rc = -EFAULT;
4968c1f63c8fSStephen M. Cameron 			goto out;
4969edd16368SStephen M. Cameron 		}
4970edd16368SStephen M. Cameron 	}
4971c1f63c8fSStephen M. Cameron out:
497245fcb86eSStephen Cameron 	cmd_free(h, c);
4973c1f63c8fSStephen M. Cameron out_kfree:
4974c1f63c8fSStephen M. Cameron 	kfree(buff);
4975c1f63c8fSStephen M. Cameron 	return rc;
4976edd16368SStephen M. Cameron }
4977edd16368SStephen M. Cameron 
4978edd16368SStephen M. Cameron static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
4979edd16368SStephen M. Cameron {
4980edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct *ioc;
4981edd16368SStephen M. Cameron 	struct CommandList *c;
4982edd16368SStephen M. Cameron 	unsigned char **buff = NULL;
4983edd16368SStephen M. Cameron 	int *buff_size = NULL;
498450a0decfSStephen M. Cameron 	u64 temp64;
4985edd16368SStephen M. Cameron 	BYTE sg_used = 0;
4986edd16368SStephen M. Cameron 	int status = 0;
498701a02ffcSStephen M. Cameron 	u32 left;
498801a02ffcSStephen M. Cameron 	u32 sz;
4989edd16368SStephen M. Cameron 	BYTE __user *data_ptr;
4990edd16368SStephen M. Cameron 
4991edd16368SStephen M. Cameron 	if (!argp)
4992edd16368SStephen M. Cameron 		return -EINVAL;
4993edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
4994edd16368SStephen M. Cameron 		return -EPERM;
4995edd16368SStephen M. Cameron 	ioc = (BIG_IOCTL_Command_struct *)
4996edd16368SStephen M. Cameron 	    kmalloc(sizeof(*ioc), GFP_KERNEL);
4997edd16368SStephen M. Cameron 	if (!ioc) {
4998edd16368SStephen M. Cameron 		status = -ENOMEM;
4999edd16368SStephen M. Cameron 		goto cleanup1;
5000edd16368SStephen M. Cameron 	}
5001edd16368SStephen M. Cameron 	if (copy_from_user(ioc, argp, sizeof(*ioc))) {
5002edd16368SStephen M. Cameron 		status = -EFAULT;
5003edd16368SStephen M. Cameron 		goto cleanup1;
5004edd16368SStephen M. Cameron 	}
5005edd16368SStephen M. Cameron 	if ((ioc->buf_size < 1) &&
5006edd16368SStephen M. Cameron 	    (ioc->Request.Type.Direction != XFER_NONE)) {
5007edd16368SStephen M. Cameron 		status = -EINVAL;
5008edd16368SStephen M. Cameron 		goto cleanup1;
5009edd16368SStephen M. Cameron 	}
5010edd16368SStephen M. Cameron 	/* Check kmalloc limits  using all SGs */
5011edd16368SStephen M. Cameron 	if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
5012edd16368SStephen M. Cameron 		status = -EINVAL;
5013edd16368SStephen M. Cameron 		goto cleanup1;
5014edd16368SStephen M. Cameron 	}
5015d66ae08bSStephen M. Cameron 	if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
5016edd16368SStephen M. Cameron 		status = -EINVAL;
5017edd16368SStephen M. Cameron 		goto cleanup1;
5018edd16368SStephen M. Cameron 	}
5019d66ae08bSStephen M. Cameron 	buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
5020edd16368SStephen M. Cameron 	if (!buff) {
5021edd16368SStephen M. Cameron 		status = -ENOMEM;
5022edd16368SStephen M. Cameron 		goto cleanup1;
5023edd16368SStephen M. Cameron 	}
5024d66ae08bSStephen M. Cameron 	buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
5025edd16368SStephen M. Cameron 	if (!buff_size) {
5026edd16368SStephen M. Cameron 		status = -ENOMEM;
5027edd16368SStephen M. Cameron 		goto cleanup1;
5028edd16368SStephen M. Cameron 	}
5029edd16368SStephen M. Cameron 	left = ioc->buf_size;
5030edd16368SStephen M. Cameron 	data_ptr = ioc->buf;
5031edd16368SStephen M. Cameron 	while (left) {
5032edd16368SStephen M. Cameron 		sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
5033edd16368SStephen M. Cameron 		buff_size[sg_used] = sz;
5034edd16368SStephen M. Cameron 		buff[sg_used] = kmalloc(sz, GFP_KERNEL);
5035edd16368SStephen M. Cameron 		if (buff[sg_used] == NULL) {
5036edd16368SStephen M. Cameron 			status = -ENOMEM;
5037edd16368SStephen M. Cameron 			goto cleanup1;
5038edd16368SStephen M. Cameron 		}
50399233fb10SStephen M. Cameron 		if (ioc->Request.Type.Direction & XFER_WRITE) {
5040edd16368SStephen M. Cameron 			if (copy_from_user(buff[sg_used], data_ptr, sz)) {
50410758f4f7SStephen M. Cameron 				status = -EFAULT;
5042edd16368SStephen M. Cameron 				goto cleanup1;
5043edd16368SStephen M. Cameron 			}
5044edd16368SStephen M. Cameron 		} else
5045edd16368SStephen M. Cameron 			memset(buff[sg_used], 0, sz);
5046edd16368SStephen M. Cameron 		left -= sz;
5047edd16368SStephen M. Cameron 		data_ptr += sz;
5048edd16368SStephen M. Cameron 		sg_used++;
5049edd16368SStephen M. Cameron 	}
505045fcb86eSStephen Cameron 	c = cmd_alloc(h);
5051edd16368SStephen M. Cameron 	if (c == NULL) {
5052edd16368SStephen M. Cameron 		status = -ENOMEM;
5053edd16368SStephen M. Cameron 		goto cleanup1;
5054edd16368SStephen M. Cameron 	}
5055edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5056edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
505750a0decfSStephen M. Cameron 	c->Header.SGList = (u8) sg_used;
505850a0decfSStephen M. Cameron 	c->Header.SGTotal = cpu_to_le16(sg_used);
5059edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
5060edd16368SStephen M. Cameron 	memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
5061edd16368SStephen M. Cameron 	if (ioc->buf_size > 0) {
5062edd16368SStephen M. Cameron 		int i;
5063edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
506450a0decfSStephen M. Cameron 			temp64 = pci_map_single(h->pdev, buff[i],
5065edd16368SStephen M. Cameron 				    buff_size[i], PCI_DMA_BIDIRECTIONAL);
506650a0decfSStephen M. Cameron 			if (dma_mapping_error(&h->pdev->dev,
506750a0decfSStephen M. Cameron 							(dma_addr_t) temp64)) {
506850a0decfSStephen M. Cameron 				c->SG[i].Addr = cpu_to_le64(0);
506950a0decfSStephen M. Cameron 				c->SG[i].Len = cpu_to_le32(0);
5070bcc48ffaSStephen M. Cameron 				hpsa_pci_unmap(h->pdev, c, i,
5071bcc48ffaSStephen M. Cameron 					PCI_DMA_BIDIRECTIONAL);
5072bcc48ffaSStephen M. Cameron 				status = -ENOMEM;
5073e2d4a1f6SStephen M. Cameron 				goto cleanup0;
5074bcc48ffaSStephen M. Cameron 			}
507550a0decfSStephen M. Cameron 			c->SG[i].Addr = cpu_to_le64(temp64);
507650a0decfSStephen M. Cameron 			c->SG[i].Len = cpu_to_le32(buff_size[i]);
507750a0decfSStephen M. Cameron 			c->SG[i].Ext = cpu_to_le32(0);
5078edd16368SStephen M. Cameron 		}
507950a0decfSStephen M. Cameron 		c->SG[--i].Ext = cpu_to_le32(HPSA_SG_LAST);
5080edd16368SStephen M. Cameron 	}
5081a0c12413SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
5082b03a7771SStephen M. Cameron 	if (sg_used)
5083edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
5084edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
5085edd16368SStephen M. Cameron 	/* Copy the error information out */
5086edd16368SStephen M. Cameron 	memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
5087edd16368SStephen M. Cameron 	if (copy_to_user(argp, ioc, sizeof(*ioc))) {
5088edd16368SStephen M. Cameron 		status = -EFAULT;
5089e2d4a1f6SStephen M. Cameron 		goto cleanup0;
5090edd16368SStephen M. Cameron 	}
50919233fb10SStephen M. Cameron 	if ((ioc->Request.Type.Direction & XFER_READ) && ioc->buf_size > 0) {
50922b08b3e9SDon Brace 		int i;
50932b08b3e9SDon Brace 
5094edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
5095edd16368SStephen M. Cameron 		BYTE __user *ptr = ioc->buf;
5096edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
5097edd16368SStephen M. Cameron 			if (copy_to_user(ptr, buff[i], buff_size[i])) {
5098edd16368SStephen M. Cameron 				status = -EFAULT;
5099e2d4a1f6SStephen M. Cameron 				goto cleanup0;
5100edd16368SStephen M. Cameron 			}
5101edd16368SStephen M. Cameron 			ptr += buff_size[i];
5102edd16368SStephen M. Cameron 		}
5103edd16368SStephen M. Cameron 	}
5104edd16368SStephen M. Cameron 	status = 0;
5105e2d4a1f6SStephen M. Cameron cleanup0:
510645fcb86eSStephen Cameron 	cmd_free(h, c);
5107edd16368SStephen M. Cameron cleanup1:
5108edd16368SStephen M. Cameron 	if (buff) {
51092b08b3e9SDon Brace 		int i;
51102b08b3e9SDon Brace 
5111edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++)
5112edd16368SStephen M. Cameron 			kfree(buff[i]);
5113edd16368SStephen M. Cameron 		kfree(buff);
5114edd16368SStephen M. Cameron 	}
5115edd16368SStephen M. Cameron 	kfree(buff_size);
5116edd16368SStephen M. Cameron 	kfree(ioc);
5117edd16368SStephen M. Cameron 	return status;
5118edd16368SStephen M. Cameron }
5119edd16368SStephen M. Cameron 
5120edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
5121edd16368SStephen M. Cameron 	struct CommandList *c)
5122edd16368SStephen M. Cameron {
5123edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
5124edd16368SStephen M. Cameron 			c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
5125edd16368SStephen M. Cameron 		(void) check_for_unit_attention(h, c);
5126edd16368SStephen M. Cameron }
51270390f0c0SStephen M. Cameron 
5128edd16368SStephen M. Cameron /*
5129edd16368SStephen M. Cameron  * ioctl
5130edd16368SStephen M. Cameron  */
513142a91641SDon Brace static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
5132edd16368SStephen M. Cameron {
5133edd16368SStephen M. Cameron 	struct ctlr_info *h;
5134edd16368SStephen M. Cameron 	void __user *argp = (void __user *)arg;
51350390f0c0SStephen M. Cameron 	int rc;
5136edd16368SStephen M. Cameron 
5137edd16368SStephen M. Cameron 	h = sdev_to_hba(dev);
5138edd16368SStephen M. Cameron 
5139edd16368SStephen M. Cameron 	switch (cmd) {
5140edd16368SStephen M. Cameron 	case CCISS_DEREGDISK:
5141edd16368SStephen M. Cameron 	case CCISS_REGNEWDISK:
5142edd16368SStephen M. Cameron 	case CCISS_REGNEWD:
5143a08a8471SStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
5144edd16368SStephen M. Cameron 		return 0;
5145edd16368SStephen M. Cameron 	case CCISS_GETPCIINFO:
5146edd16368SStephen M. Cameron 		return hpsa_getpciinfo_ioctl(h, argp);
5147edd16368SStephen M. Cameron 	case CCISS_GETDRIVVER:
5148edd16368SStephen M. Cameron 		return hpsa_getdrivver_ioctl(h, argp);
5149edd16368SStephen M. Cameron 	case CCISS_PASSTHRU:
515034f0c627SDon Brace 		if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
51510390f0c0SStephen M. Cameron 			return -EAGAIN;
51520390f0c0SStephen M. Cameron 		rc = hpsa_passthru_ioctl(h, argp);
515334f0c627SDon Brace 		atomic_inc(&h->passthru_cmds_avail);
51540390f0c0SStephen M. Cameron 		return rc;
5155edd16368SStephen M. Cameron 	case CCISS_BIG_PASSTHRU:
515634f0c627SDon Brace 		if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
51570390f0c0SStephen M. Cameron 			return -EAGAIN;
51580390f0c0SStephen M. Cameron 		rc = hpsa_big_passthru_ioctl(h, argp);
515934f0c627SDon Brace 		atomic_inc(&h->passthru_cmds_avail);
51600390f0c0SStephen M. Cameron 		return rc;
5161edd16368SStephen M. Cameron 	default:
5162edd16368SStephen M. Cameron 		return -ENOTTY;
5163edd16368SStephen M. Cameron 	}
5164edd16368SStephen M. Cameron }
5165edd16368SStephen M. Cameron 
51666f039790SGreg Kroah-Hartman static int hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
51676f039790SGreg Kroah-Hartman 				u8 reset_type)
516864670ac8SStephen M. Cameron {
516964670ac8SStephen M. Cameron 	struct CommandList *c;
517064670ac8SStephen M. Cameron 
517164670ac8SStephen M. Cameron 	c = cmd_alloc(h);
517264670ac8SStephen M. Cameron 	if (!c)
517364670ac8SStephen M. Cameron 		return -ENOMEM;
5174a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map */
5175a2dac136SStephen M. Cameron 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
517664670ac8SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_MSG);
517764670ac8SStephen M. Cameron 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
517864670ac8SStephen M. Cameron 	c->waiting = NULL;
517964670ac8SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
518064670ac8SStephen M. Cameron 	/* Don't wait for completion, the reset won't complete.  Don't free
518164670ac8SStephen M. Cameron 	 * the command either.  This is the last command we will send before
518264670ac8SStephen M. Cameron 	 * re-initializing everything, so it doesn't matter and won't leak.
518364670ac8SStephen M. Cameron 	 */
518464670ac8SStephen M. Cameron 	return 0;
518564670ac8SStephen M. Cameron }
518664670ac8SStephen M. Cameron 
5187a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
5188b7bb24ebSStephen M. Cameron 	void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
5189edd16368SStephen M. Cameron 	int cmd_type)
5190edd16368SStephen M. Cameron {
5191edd16368SStephen M. Cameron 	int pci_dir = XFER_NONE;
519275167d2cSStephen M. Cameron 	struct CommandList *a; /* for commands to be aborted */
5193edd16368SStephen M. Cameron 
5194edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5195edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
5196edd16368SStephen M. Cameron 	if (buff != NULL && size > 0) {
5197edd16368SStephen M. Cameron 		c->Header.SGList = 1;
519850a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(1);
5199edd16368SStephen M. Cameron 	} else {
5200edd16368SStephen M. Cameron 		c->Header.SGList = 0;
520150a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(0);
5202edd16368SStephen M. Cameron 	}
5203edd16368SStephen M. Cameron 	memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
5204edd16368SStephen M. Cameron 
5205edd16368SStephen M. Cameron 	if (cmd_type == TYPE_CMD) {
5206edd16368SStephen M. Cameron 		switch (cmd) {
5207edd16368SStephen M. Cameron 		case HPSA_INQUIRY:
5208edd16368SStephen M. Cameron 			/* are we trying to read a vital product page */
5209b7bb24ebSStephen M. Cameron 			if (page_code & VPD_PAGE) {
5210edd16368SStephen M. Cameron 				c->Request.CDB[1] = 0x01;
5211b7bb24ebSStephen M. Cameron 				c->Request.CDB[2] = (page_code & 0xff);
5212edd16368SStephen M. Cameron 			}
5213edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
5214a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5215a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5216edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5217edd16368SStephen M. Cameron 			c->Request.CDB[0] = HPSA_INQUIRY;
5218edd16368SStephen M. Cameron 			c->Request.CDB[4] = size & 0xFF;
5219edd16368SStephen M. Cameron 			break;
5220edd16368SStephen M. Cameron 		case HPSA_REPORT_LOG:
5221edd16368SStephen M. Cameron 		case HPSA_REPORT_PHYS:
5222edd16368SStephen M. Cameron 			/* Talking to controller so It's a physical command
5223edd16368SStephen M. Cameron 			   mode = 00 target = 0.  Nothing to write.
5224edd16368SStephen M. Cameron 			 */
5225edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
5226a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5227a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5228edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5229edd16368SStephen M. Cameron 			c->Request.CDB[0] = cmd;
5230edd16368SStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
5231edd16368SStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5232edd16368SStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5233edd16368SStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
5234edd16368SStephen M. Cameron 			break;
5235edd16368SStephen M. Cameron 		case HPSA_CACHE_FLUSH:
5236edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
5237a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5238a505b86fSStephen M. Cameron 					TYPE_ATTR_DIR(cmd_type,
5239a505b86fSStephen M. Cameron 						ATTR_SIMPLE, XFER_WRITE);
5240edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5241edd16368SStephen M. Cameron 			c->Request.CDB[0] = BMIC_WRITE;
5242edd16368SStephen M. Cameron 			c->Request.CDB[6] = BMIC_CACHE_FLUSH;
5243bb158eabSStephen M. Cameron 			c->Request.CDB[7] = (size >> 8) & 0xFF;
5244bb158eabSStephen M. Cameron 			c->Request.CDB[8] = size & 0xFF;
5245edd16368SStephen M. Cameron 			break;
5246edd16368SStephen M. Cameron 		case TEST_UNIT_READY:
5247edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
5248a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5249a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
5250edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5251edd16368SStephen M. Cameron 			break;
5252283b4a9bSStephen M. Cameron 		case HPSA_GET_RAID_MAP:
5253283b4a9bSStephen M. Cameron 			c->Request.CDBLen = 12;
5254a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5255a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5256283b4a9bSStephen M. Cameron 			c->Request.Timeout = 0;
5257283b4a9bSStephen M. Cameron 			c->Request.CDB[0] = HPSA_CISS_READ;
5258283b4a9bSStephen M. Cameron 			c->Request.CDB[1] = cmd;
5259283b4a9bSStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
5260283b4a9bSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5261283b4a9bSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5262283b4a9bSStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
5263283b4a9bSStephen M. Cameron 			break;
5264316b221aSStephen M. Cameron 		case BMIC_SENSE_CONTROLLER_PARAMETERS:
5265316b221aSStephen M. Cameron 			c->Request.CDBLen = 10;
5266a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5267a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5268316b221aSStephen M. Cameron 			c->Request.Timeout = 0;
5269316b221aSStephen M. Cameron 			c->Request.CDB[0] = BMIC_READ;
5270316b221aSStephen M. Cameron 			c->Request.CDB[6] = BMIC_SENSE_CONTROLLER_PARAMETERS;
5271316b221aSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5272316b221aSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5273316b221aSStephen M. Cameron 			break;
527403383736SDon Brace 		case BMIC_IDENTIFY_PHYSICAL_DEVICE:
527503383736SDon Brace 			c->Request.CDBLen = 10;
527603383736SDon Brace 			c->Request.type_attr_dir =
527703383736SDon Brace 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
527803383736SDon Brace 			c->Request.Timeout = 0;
527903383736SDon Brace 			c->Request.CDB[0] = BMIC_READ;
528003383736SDon Brace 			c->Request.CDB[6] = BMIC_IDENTIFY_PHYSICAL_DEVICE;
528103383736SDon Brace 			c->Request.CDB[7] = (size >> 16) & 0xFF;
528203383736SDon Brace 			c->Request.CDB[8] = (size >> 8) & 0XFF;
528303383736SDon Brace 			break;
5284edd16368SStephen M. Cameron 		default:
5285edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
5286edd16368SStephen M. Cameron 			BUG();
5287a2dac136SStephen M. Cameron 			return -1;
5288edd16368SStephen M. Cameron 		}
5289edd16368SStephen M. Cameron 	} else if (cmd_type == TYPE_MSG) {
5290edd16368SStephen M. Cameron 		switch (cmd) {
5291edd16368SStephen M. Cameron 
5292edd16368SStephen M. Cameron 		case  HPSA_DEVICE_RESET_MSG:
5293edd16368SStephen M. Cameron 			c->Request.CDBLen = 16;
5294a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5295a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
5296edd16368SStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
529764670ac8SStephen M. Cameron 			memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
529864670ac8SStephen M. Cameron 			c->Request.CDB[0] =  cmd;
529921e89afdSStephen M. Cameron 			c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
5300edd16368SStephen M. Cameron 			/* If bytes 4-7 are zero, it means reset the */
5301edd16368SStephen M. Cameron 			/* LunID device */
5302edd16368SStephen M. Cameron 			c->Request.CDB[4] = 0x00;
5303edd16368SStephen M. Cameron 			c->Request.CDB[5] = 0x00;
5304edd16368SStephen M. Cameron 			c->Request.CDB[6] = 0x00;
5305edd16368SStephen M. Cameron 			c->Request.CDB[7] = 0x00;
5306edd16368SStephen M. Cameron 			break;
530775167d2cSStephen M. Cameron 		case  HPSA_ABORT_MSG:
530875167d2cSStephen M. Cameron 			a = buff;       /* point to command to be aborted */
53092b08b3e9SDon Brace 			dev_dbg(&h->pdev->dev,
53102b08b3e9SDon Brace 				"Abort Tag:0x%016llx request Tag:0x%016llx",
531150a0decfSStephen M. Cameron 				a->Header.tag, c->Header.tag);
531275167d2cSStephen M. Cameron 			c->Request.CDBLen = 16;
5313a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5314a505b86fSStephen M. Cameron 					TYPE_ATTR_DIR(cmd_type,
5315a505b86fSStephen M. Cameron 						ATTR_SIMPLE, XFER_WRITE);
531675167d2cSStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
531775167d2cSStephen M. Cameron 			c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
531875167d2cSStephen M. Cameron 			c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
531975167d2cSStephen M. Cameron 			c->Request.CDB[2] = 0x00; /* reserved */
532075167d2cSStephen M. Cameron 			c->Request.CDB[3] = 0x00; /* reserved */
532175167d2cSStephen M. Cameron 			/* Tag to abort goes in CDB[4]-CDB[11] */
53222b08b3e9SDon Brace 			memcpy(&c->Request.CDB[4], &a->Header.tag,
53232b08b3e9SDon Brace 				sizeof(a->Header.tag));
532475167d2cSStephen M. Cameron 			c->Request.CDB[12] = 0x00; /* reserved */
532575167d2cSStephen M. Cameron 			c->Request.CDB[13] = 0x00; /* reserved */
532675167d2cSStephen M. Cameron 			c->Request.CDB[14] = 0x00; /* reserved */
532775167d2cSStephen M. Cameron 			c->Request.CDB[15] = 0x00; /* reserved */
532875167d2cSStephen M. Cameron 		break;
5329edd16368SStephen M. Cameron 		default:
5330edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown message type %d\n",
5331edd16368SStephen M. Cameron 				cmd);
5332edd16368SStephen M. Cameron 			BUG();
5333edd16368SStephen M. Cameron 		}
5334edd16368SStephen M. Cameron 	} else {
5335edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
5336edd16368SStephen M. Cameron 		BUG();
5337edd16368SStephen M. Cameron 	}
5338edd16368SStephen M. Cameron 
5339a505b86fSStephen M. Cameron 	switch (GET_DIR(c->Request.type_attr_dir)) {
5340edd16368SStephen M. Cameron 	case XFER_READ:
5341edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_FROMDEVICE;
5342edd16368SStephen M. Cameron 		break;
5343edd16368SStephen M. Cameron 	case XFER_WRITE:
5344edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_TODEVICE;
5345edd16368SStephen M. Cameron 		break;
5346edd16368SStephen M. Cameron 	case XFER_NONE:
5347edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_NONE;
5348edd16368SStephen M. Cameron 		break;
5349edd16368SStephen M. Cameron 	default:
5350edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_BIDIRECTIONAL;
5351edd16368SStephen M. Cameron 	}
5352a2dac136SStephen M. Cameron 	if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
5353a2dac136SStephen M. Cameron 		return -1;
5354a2dac136SStephen M. Cameron 	return 0;
5355edd16368SStephen M. Cameron }
5356edd16368SStephen M. Cameron 
5357edd16368SStephen M. Cameron /*
5358edd16368SStephen M. Cameron  * Map (physical) PCI mem into (virtual) kernel space
5359edd16368SStephen M. Cameron  */
5360edd16368SStephen M. Cameron static void __iomem *remap_pci_mem(ulong base, ulong size)
5361edd16368SStephen M. Cameron {
5362edd16368SStephen M. Cameron 	ulong page_base = ((ulong) base) & PAGE_MASK;
5363edd16368SStephen M. Cameron 	ulong page_offs = ((ulong) base) - page_base;
5364088ba34cSStephen M. Cameron 	void __iomem *page_remapped = ioremap_nocache(page_base,
5365088ba34cSStephen M. Cameron 		page_offs + size);
5366edd16368SStephen M. Cameron 
5367edd16368SStephen M. Cameron 	return page_remapped ? (page_remapped + page_offs) : NULL;
5368edd16368SStephen M. Cameron }
5369edd16368SStephen M. Cameron 
5370254f796bSMatt Gates static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
5371edd16368SStephen M. Cameron {
5372254f796bSMatt Gates 	return h->access.command_completed(h, q);
5373edd16368SStephen M. Cameron }
5374edd16368SStephen M. Cameron 
5375900c5440SStephen M. Cameron static inline bool interrupt_pending(struct ctlr_info *h)
5376edd16368SStephen M. Cameron {
5377edd16368SStephen M. Cameron 	return h->access.intr_pending(h);
5378edd16368SStephen M. Cameron }
5379edd16368SStephen M. Cameron 
5380edd16368SStephen M. Cameron static inline long interrupt_not_for_us(struct ctlr_info *h)
5381edd16368SStephen M. Cameron {
538210f66018SStephen M. Cameron 	return (h->access.intr_pending(h) == 0) ||
538310f66018SStephen M. Cameron 		(h->interrupts_enabled == 0);
5384edd16368SStephen M. Cameron }
5385edd16368SStephen M. Cameron 
538601a02ffcSStephen M. Cameron static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
538701a02ffcSStephen M. Cameron 	u32 raw_tag)
5388edd16368SStephen M. Cameron {
5389edd16368SStephen M. Cameron 	if (unlikely(tag_index >= h->nr_cmds)) {
5390edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
5391edd16368SStephen M. Cameron 		return 1;
5392edd16368SStephen M. Cameron 	}
5393edd16368SStephen M. Cameron 	return 0;
5394edd16368SStephen M. Cameron }
5395edd16368SStephen M. Cameron 
53965a3d16f5SStephen M. Cameron static inline void finish_cmd(struct CommandList *c)
5397edd16368SStephen M. Cameron {
5398e85c5974SStephen M. Cameron 	dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
5399c349775eSScott Teel 	if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
5400c349775eSScott Teel 			|| c->cmd_type == CMD_IOACCEL2))
54011fb011fbSStephen M. Cameron 		complete_scsi_command(c);
5402edd16368SStephen M. Cameron 	else if (c->cmd_type == CMD_IOCTL_PEND)
5403edd16368SStephen M. Cameron 		complete(c->waiting);
5404a104c99fSStephen M. Cameron }
5405a104c99fSStephen M. Cameron 
5406a9a3a273SStephen M. Cameron 
5407a9a3a273SStephen M. Cameron static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
5408a104c99fSStephen M. Cameron {
5409a9a3a273SStephen M. Cameron #define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
5410a9a3a273SStephen M. Cameron #define HPSA_SIMPLE_ERROR_BITS 0x03
5411960a30e7SStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
5412a9a3a273SStephen M. Cameron 		return tag & ~HPSA_SIMPLE_ERROR_BITS;
5413a9a3a273SStephen M. Cameron 	return tag & ~HPSA_PERF_ERROR_BITS;
5414a104c99fSStephen M. Cameron }
5415a104c99fSStephen M. Cameron 
5416303932fdSDon Brace /* process completion of an indexed ("direct lookup") command */
54171d94f94dSStephen M. Cameron static inline void process_indexed_cmd(struct ctlr_info *h,
5418303932fdSDon Brace 	u32 raw_tag)
5419303932fdSDon Brace {
5420303932fdSDon Brace 	u32 tag_index;
5421303932fdSDon Brace 	struct CommandList *c;
5422303932fdSDon Brace 
5423f2405db8SDon Brace 	tag_index = raw_tag >> DIRECT_LOOKUP_SHIFT;
54241d94f94dSStephen M. Cameron 	if (!bad_tag(h, tag_index, raw_tag)) {
5425303932fdSDon Brace 		c = h->cmd_pool + tag_index;
54265a3d16f5SStephen M. Cameron 		finish_cmd(c);
54271d94f94dSStephen M. Cameron 	}
5428303932fdSDon Brace }
5429303932fdSDon Brace 
543064670ac8SStephen M. Cameron /* Some controllers, like p400, will give us one interrupt
543164670ac8SStephen M. Cameron  * after a soft reset, even if we turned interrupts off.
543264670ac8SStephen M. Cameron  * Only need to check for this in the hpsa_xxx_discard_completions
543364670ac8SStephen M. Cameron  * functions.
543464670ac8SStephen M. Cameron  */
543564670ac8SStephen M. Cameron static int ignore_bogus_interrupt(struct ctlr_info *h)
543664670ac8SStephen M. Cameron {
543764670ac8SStephen M. Cameron 	if (likely(!reset_devices))
543864670ac8SStephen M. Cameron 		return 0;
543964670ac8SStephen M. Cameron 
544064670ac8SStephen M. Cameron 	if (likely(h->interrupts_enabled))
544164670ac8SStephen M. Cameron 		return 0;
544264670ac8SStephen M. Cameron 
544364670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
544464670ac8SStephen M. Cameron 		"(known firmware bug.)  Ignoring.\n");
544564670ac8SStephen M. Cameron 
544664670ac8SStephen M. Cameron 	return 1;
544764670ac8SStephen M. Cameron }
544864670ac8SStephen M. Cameron 
5449254f796bSMatt Gates /*
5450254f796bSMatt Gates  * Convert &h->q[x] (passed to interrupt handlers) back to h.
5451254f796bSMatt Gates  * Relies on (h-q[x] == x) being true for x such that
5452254f796bSMatt Gates  * 0 <= x < MAX_REPLY_QUEUES.
5453254f796bSMatt Gates  */
5454254f796bSMatt Gates static struct ctlr_info *queue_to_hba(u8 *queue)
545564670ac8SStephen M. Cameron {
5456254f796bSMatt Gates 	return container_of((queue - *queue), struct ctlr_info, q[0]);
5457254f796bSMatt Gates }
5458254f796bSMatt Gates 
5459254f796bSMatt Gates static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
5460254f796bSMatt Gates {
5461254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
5462254f796bSMatt Gates 	u8 q = *(u8 *) queue;
546364670ac8SStephen M. Cameron 	u32 raw_tag;
546464670ac8SStephen M. Cameron 
546564670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
546664670ac8SStephen M. Cameron 		return IRQ_NONE;
546764670ac8SStephen M. Cameron 
546864670ac8SStephen M. Cameron 	if (interrupt_not_for_us(h))
546964670ac8SStephen M. Cameron 		return IRQ_NONE;
5470a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
547164670ac8SStephen M. Cameron 	while (interrupt_pending(h)) {
5472254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
547364670ac8SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY)
5474254f796bSMatt Gates 			raw_tag = next_command(h, q);
547564670ac8SStephen M. Cameron 	}
547664670ac8SStephen M. Cameron 	return IRQ_HANDLED;
547764670ac8SStephen M. Cameron }
547864670ac8SStephen M. Cameron 
5479254f796bSMatt Gates static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
548064670ac8SStephen M. Cameron {
5481254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
548264670ac8SStephen M. Cameron 	u32 raw_tag;
5483254f796bSMatt Gates 	u8 q = *(u8 *) queue;
548464670ac8SStephen M. Cameron 
548564670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
548664670ac8SStephen M. Cameron 		return IRQ_NONE;
548764670ac8SStephen M. Cameron 
5488a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
5489254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
549064670ac8SStephen M. Cameron 	while (raw_tag != FIFO_EMPTY)
5491254f796bSMatt Gates 		raw_tag = next_command(h, q);
549264670ac8SStephen M. Cameron 	return IRQ_HANDLED;
549364670ac8SStephen M. Cameron }
549464670ac8SStephen M. Cameron 
5495254f796bSMatt Gates static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
5496edd16368SStephen M. Cameron {
5497254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba((u8 *) queue);
5498303932fdSDon Brace 	u32 raw_tag;
5499254f796bSMatt Gates 	u8 q = *(u8 *) queue;
5500edd16368SStephen M. Cameron 
5501edd16368SStephen M. Cameron 	if (interrupt_not_for_us(h))
5502edd16368SStephen M. Cameron 		return IRQ_NONE;
5503a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
550410f66018SStephen M. Cameron 	while (interrupt_pending(h)) {
5505254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
550610f66018SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY) {
55071d94f94dSStephen M. Cameron 			process_indexed_cmd(h, raw_tag);
5508254f796bSMatt Gates 			raw_tag = next_command(h, q);
550910f66018SStephen M. Cameron 		}
551010f66018SStephen M. Cameron 	}
551110f66018SStephen M. Cameron 	return IRQ_HANDLED;
551210f66018SStephen M. Cameron }
551310f66018SStephen M. Cameron 
5514254f796bSMatt Gates static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
551510f66018SStephen M. Cameron {
5516254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
551710f66018SStephen M. Cameron 	u32 raw_tag;
5518254f796bSMatt Gates 	u8 q = *(u8 *) queue;
551910f66018SStephen M. Cameron 
5520a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
5521254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
5522303932fdSDon Brace 	while (raw_tag != FIFO_EMPTY) {
55231d94f94dSStephen M. Cameron 		process_indexed_cmd(h, raw_tag);
5524254f796bSMatt Gates 		raw_tag = next_command(h, q);
5525edd16368SStephen M. Cameron 	}
5526edd16368SStephen M. Cameron 	return IRQ_HANDLED;
5527edd16368SStephen M. Cameron }
5528edd16368SStephen M. Cameron 
5529a9a3a273SStephen M. Cameron /* Send a message CDB to the firmware. Careful, this only works
5530a9a3a273SStephen M. Cameron  * in simple mode, not performant mode due to the tag lookup.
5531a9a3a273SStephen M. Cameron  * We only ever use this immediately after a controller reset.
5532a9a3a273SStephen M. Cameron  */
55336f039790SGreg Kroah-Hartman static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
5534edd16368SStephen M. Cameron 			unsigned char type)
5535edd16368SStephen M. Cameron {
5536edd16368SStephen M. Cameron 	struct Command {
5537edd16368SStephen M. Cameron 		struct CommandListHeader CommandHeader;
5538edd16368SStephen M. Cameron 		struct RequestBlock Request;
5539edd16368SStephen M. Cameron 		struct ErrDescriptor ErrorDescriptor;
5540edd16368SStephen M. Cameron 	};
5541edd16368SStephen M. Cameron 	struct Command *cmd;
5542edd16368SStephen M. Cameron 	static const size_t cmd_sz = sizeof(*cmd) +
5543edd16368SStephen M. Cameron 					sizeof(cmd->ErrorDescriptor);
5544edd16368SStephen M. Cameron 	dma_addr_t paddr64;
55452b08b3e9SDon Brace 	__le32 paddr32;
55462b08b3e9SDon Brace 	u32 tag;
5547edd16368SStephen M. Cameron 	void __iomem *vaddr;
5548edd16368SStephen M. Cameron 	int i, err;
5549edd16368SStephen M. Cameron 
5550edd16368SStephen M. Cameron 	vaddr = pci_ioremap_bar(pdev, 0);
5551edd16368SStephen M. Cameron 	if (vaddr == NULL)
5552edd16368SStephen M. Cameron 		return -ENOMEM;
5553edd16368SStephen M. Cameron 
5554edd16368SStephen M. Cameron 	/* The Inbound Post Queue only accepts 32-bit physical addresses for the
5555edd16368SStephen M. Cameron 	 * CCISS commands, so they must be allocated from the lower 4GiB of
5556edd16368SStephen M. Cameron 	 * memory.
5557edd16368SStephen M. Cameron 	 */
5558edd16368SStephen M. Cameron 	err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
5559edd16368SStephen M. Cameron 	if (err) {
5560edd16368SStephen M. Cameron 		iounmap(vaddr);
55611eaec8f3SRobert Elliott 		return err;
5562edd16368SStephen M. Cameron 	}
5563edd16368SStephen M. Cameron 
5564edd16368SStephen M. Cameron 	cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
5565edd16368SStephen M. Cameron 	if (cmd == NULL) {
5566edd16368SStephen M. Cameron 		iounmap(vaddr);
5567edd16368SStephen M. Cameron 		return -ENOMEM;
5568edd16368SStephen M. Cameron 	}
5569edd16368SStephen M. Cameron 
5570edd16368SStephen M. Cameron 	/* This must fit, because of the 32-bit consistent DMA mask.  Also,
5571edd16368SStephen M. Cameron 	 * although there's no guarantee, we assume that the address is at
5572edd16368SStephen M. Cameron 	 * least 4-byte aligned (most likely, it's page-aligned).
5573edd16368SStephen M. Cameron 	 */
55742b08b3e9SDon Brace 	paddr32 = cpu_to_le32(paddr64);
5575edd16368SStephen M. Cameron 
5576edd16368SStephen M. Cameron 	cmd->CommandHeader.ReplyQueue = 0;
5577edd16368SStephen M. Cameron 	cmd->CommandHeader.SGList = 0;
557850a0decfSStephen M. Cameron 	cmd->CommandHeader.SGTotal = cpu_to_le16(0);
55792b08b3e9SDon Brace 	cmd->CommandHeader.tag = cpu_to_le64(paddr64);
5580edd16368SStephen M. Cameron 	memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
5581edd16368SStephen M. Cameron 
5582edd16368SStephen M. Cameron 	cmd->Request.CDBLen = 16;
5583a505b86fSStephen M. Cameron 	cmd->Request.type_attr_dir =
5584a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_MSG, ATTR_HEADOFQUEUE, XFER_NONE);
5585edd16368SStephen M. Cameron 	cmd->Request.Timeout = 0; /* Don't time out */
5586edd16368SStephen M. Cameron 	cmd->Request.CDB[0] = opcode;
5587edd16368SStephen M. Cameron 	cmd->Request.CDB[1] = type;
5588edd16368SStephen M. Cameron 	memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
558950a0decfSStephen M. Cameron 	cmd->ErrorDescriptor.Addr =
55902b08b3e9SDon Brace 			cpu_to_le64((le32_to_cpu(paddr32) + sizeof(*cmd)));
559150a0decfSStephen M. Cameron 	cmd->ErrorDescriptor.Len = cpu_to_le32(sizeof(struct ErrorInfo));
5592edd16368SStephen M. Cameron 
55932b08b3e9SDon Brace 	writel(le32_to_cpu(paddr32), vaddr + SA5_REQUEST_PORT_OFFSET);
5594edd16368SStephen M. Cameron 
5595edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
5596edd16368SStephen M. Cameron 		tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
55972b08b3e9SDon Brace 		if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr64)
5598edd16368SStephen M. Cameron 			break;
5599edd16368SStephen M. Cameron 		msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
5600edd16368SStephen M. Cameron 	}
5601edd16368SStephen M. Cameron 
5602edd16368SStephen M. Cameron 	iounmap(vaddr);
5603edd16368SStephen M. Cameron 
5604edd16368SStephen M. Cameron 	/* we leak the DMA buffer here ... no choice since the controller could
5605edd16368SStephen M. Cameron 	 *  still complete the command.
5606edd16368SStephen M. Cameron 	 */
5607edd16368SStephen M. Cameron 	if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
5608edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
5609edd16368SStephen M. Cameron 			opcode, type);
5610edd16368SStephen M. Cameron 		return -ETIMEDOUT;
5611edd16368SStephen M. Cameron 	}
5612edd16368SStephen M. Cameron 
5613edd16368SStephen M. Cameron 	pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
5614edd16368SStephen M. Cameron 
5615edd16368SStephen M. Cameron 	if (tag & HPSA_ERROR_BIT) {
5616edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
5617edd16368SStephen M. Cameron 			opcode, type);
5618edd16368SStephen M. Cameron 		return -EIO;
5619edd16368SStephen M. Cameron 	}
5620edd16368SStephen M. Cameron 
5621edd16368SStephen M. Cameron 	dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
5622edd16368SStephen M. Cameron 		opcode, type);
5623edd16368SStephen M. Cameron 	return 0;
5624edd16368SStephen M. Cameron }
5625edd16368SStephen M. Cameron 
5626edd16368SStephen M. Cameron #define hpsa_noop(p) hpsa_message(p, 3, 0)
5627edd16368SStephen M. Cameron 
56281df8552aSStephen M. Cameron static int hpsa_controller_hard_reset(struct pci_dev *pdev,
562942a91641SDon Brace 	void __iomem *vaddr, u32 use_doorbell)
5630edd16368SStephen M. Cameron {
5631edd16368SStephen M. Cameron 
56321df8552aSStephen M. Cameron 	if (use_doorbell) {
56331df8552aSStephen M. Cameron 		/* For everything after the P600, the PCI power state method
56341df8552aSStephen M. Cameron 		 * of resetting the controller doesn't work, so we have this
56351df8552aSStephen M. Cameron 		 * other way using the doorbell register.
5636edd16368SStephen M. Cameron 		 */
56371df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using doorbell to reset controller\n");
5638cf0b08d0SStephen M. Cameron 		writel(use_doorbell, vaddr + SA5_DOORBELL);
563985009239SStephen M. Cameron 
564000701a96SJustin Lindley 		/* PMC hardware guys tell us we need a 10 second delay after
564185009239SStephen M. Cameron 		 * doorbell reset and before any attempt to talk to the board
564285009239SStephen M. Cameron 		 * at all to ensure that this actually works and doesn't fall
564385009239SStephen M. Cameron 		 * over in some weird corner cases.
564485009239SStephen M. Cameron 		 */
564500701a96SJustin Lindley 		msleep(10000);
56461df8552aSStephen M. Cameron 	} else { /* Try to do it the PCI power state way */
5647edd16368SStephen M. Cameron 
5648edd16368SStephen M. Cameron 		/* Quoting from the Open CISS Specification: "The Power
5649edd16368SStephen M. Cameron 		 * Management Control/Status Register (CSR) controls the power
5650edd16368SStephen M. Cameron 		 * state of the device.  The normal operating state is D0,
5651edd16368SStephen M. Cameron 		 * CSR=00h.  The software off state is D3, CSR=03h.  To reset
56521df8552aSStephen M. Cameron 		 * the controller, place the interface device in D3 then to D0,
56531df8552aSStephen M. Cameron 		 * this causes a secondary PCI reset which will reset the
56541df8552aSStephen M. Cameron 		 * controller." */
5655edd16368SStephen M. Cameron 
56562662cab8SDon Brace 		int rc = 0;
56572662cab8SDon Brace 
56581df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using PCI PM to reset controller\n");
56592662cab8SDon Brace 
5660edd16368SStephen M. Cameron 		/* enter the D3hot power management state */
56612662cab8SDon Brace 		rc = pci_set_power_state(pdev, PCI_D3hot);
56622662cab8SDon Brace 		if (rc)
56632662cab8SDon Brace 			return rc;
5664edd16368SStephen M. Cameron 
5665edd16368SStephen M. Cameron 		msleep(500);
5666edd16368SStephen M. Cameron 
5667edd16368SStephen M. Cameron 		/* enter the D0 power management state */
56682662cab8SDon Brace 		rc = pci_set_power_state(pdev, PCI_D0);
56692662cab8SDon Brace 		if (rc)
56702662cab8SDon Brace 			return rc;
5671c4853efeSMike Miller 
5672c4853efeSMike Miller 		/*
5673c4853efeSMike Miller 		 * The P600 requires a small delay when changing states.
5674c4853efeSMike Miller 		 * Otherwise we may think the board did not reset and we bail.
5675c4853efeSMike Miller 		 * This for kdump only and is particular to the P600.
5676c4853efeSMike Miller 		 */
5677c4853efeSMike Miller 		msleep(500);
56781df8552aSStephen M. Cameron 	}
56791df8552aSStephen M. Cameron 	return 0;
56801df8552aSStephen M. Cameron }
56811df8552aSStephen M. Cameron 
56826f039790SGreg Kroah-Hartman static void init_driver_version(char *driver_version, int len)
5683580ada3cSStephen M. Cameron {
5684580ada3cSStephen M. Cameron 	memset(driver_version, 0, len);
5685f79cfec6SStephen M. Cameron 	strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
5686580ada3cSStephen M. Cameron }
5687580ada3cSStephen M. Cameron 
56886f039790SGreg Kroah-Hartman static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
5689580ada3cSStephen M. Cameron {
5690580ada3cSStephen M. Cameron 	char *driver_version;
5691580ada3cSStephen M. Cameron 	int i, size = sizeof(cfgtable->driver_version);
5692580ada3cSStephen M. Cameron 
5693580ada3cSStephen M. Cameron 	driver_version = kmalloc(size, GFP_KERNEL);
5694580ada3cSStephen M. Cameron 	if (!driver_version)
5695580ada3cSStephen M. Cameron 		return -ENOMEM;
5696580ada3cSStephen M. Cameron 
5697580ada3cSStephen M. Cameron 	init_driver_version(driver_version, size);
5698580ada3cSStephen M. Cameron 	for (i = 0; i < size; i++)
5699580ada3cSStephen M. Cameron 		writeb(driver_version[i], &cfgtable->driver_version[i]);
5700580ada3cSStephen M. Cameron 	kfree(driver_version);
5701580ada3cSStephen M. Cameron 	return 0;
5702580ada3cSStephen M. Cameron }
5703580ada3cSStephen M. Cameron 
57046f039790SGreg Kroah-Hartman static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
57056f039790SGreg Kroah-Hartman 					  unsigned char *driver_ver)
5706580ada3cSStephen M. Cameron {
5707580ada3cSStephen M. Cameron 	int i;
5708580ada3cSStephen M. Cameron 
5709580ada3cSStephen M. Cameron 	for (i = 0; i < sizeof(cfgtable->driver_version); i++)
5710580ada3cSStephen M. Cameron 		driver_ver[i] = readb(&cfgtable->driver_version[i]);
5711580ada3cSStephen M. Cameron }
5712580ada3cSStephen M. Cameron 
57136f039790SGreg Kroah-Hartman static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
5714580ada3cSStephen M. Cameron {
5715580ada3cSStephen M. Cameron 
5716580ada3cSStephen M. Cameron 	char *driver_ver, *old_driver_ver;
5717580ada3cSStephen M. Cameron 	int rc, size = sizeof(cfgtable->driver_version);
5718580ada3cSStephen M. Cameron 
5719580ada3cSStephen M. Cameron 	old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
5720580ada3cSStephen M. Cameron 	if (!old_driver_ver)
5721580ada3cSStephen M. Cameron 		return -ENOMEM;
5722580ada3cSStephen M. Cameron 	driver_ver = old_driver_ver + size;
5723580ada3cSStephen M. Cameron 
5724580ada3cSStephen M. Cameron 	/* After a reset, the 32 bytes of "driver version" in the cfgtable
5725580ada3cSStephen M. Cameron 	 * should have been changed, otherwise we know the reset failed.
5726580ada3cSStephen M. Cameron 	 */
5727580ada3cSStephen M. Cameron 	init_driver_version(old_driver_ver, size);
5728580ada3cSStephen M. Cameron 	read_driver_ver_from_cfgtable(cfgtable, driver_ver);
5729580ada3cSStephen M. Cameron 	rc = !memcmp(driver_ver, old_driver_ver, size);
5730580ada3cSStephen M. Cameron 	kfree(old_driver_ver);
5731580ada3cSStephen M. Cameron 	return rc;
5732580ada3cSStephen M. Cameron }
57331df8552aSStephen M. Cameron /* This does a hard reset of the controller using PCI power management
57341df8552aSStephen M. Cameron  * states or the using the doorbell register.
57351df8552aSStephen M. Cameron  */
57366f039790SGreg Kroah-Hartman static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev)
57371df8552aSStephen M. Cameron {
57381df8552aSStephen M. Cameron 	u64 cfg_offset;
57391df8552aSStephen M. Cameron 	u32 cfg_base_addr;
57401df8552aSStephen M. Cameron 	u64 cfg_base_addr_index;
57411df8552aSStephen M. Cameron 	void __iomem *vaddr;
57421df8552aSStephen M. Cameron 	unsigned long paddr;
5743580ada3cSStephen M. Cameron 	u32 misc_fw_support;
5744270d05deSStephen M. Cameron 	int rc;
57451df8552aSStephen M. Cameron 	struct CfgTable __iomem *cfgtable;
5746cf0b08d0SStephen M. Cameron 	u32 use_doorbell;
574718867659SStephen M. Cameron 	u32 board_id;
5748270d05deSStephen M. Cameron 	u16 command_register;
57491df8552aSStephen M. Cameron 
57501df8552aSStephen M. Cameron 	/* For controllers as old as the P600, this is very nearly
57511df8552aSStephen M. Cameron 	 * the same thing as
57521df8552aSStephen M. Cameron 	 *
57531df8552aSStephen M. Cameron 	 * pci_save_state(pci_dev);
57541df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D3hot);
57551df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D0);
57561df8552aSStephen M. Cameron 	 * pci_restore_state(pci_dev);
57571df8552aSStephen M. Cameron 	 *
57581df8552aSStephen M. Cameron 	 * For controllers newer than the P600, the pci power state
57591df8552aSStephen M. Cameron 	 * method of resetting doesn't work so we have another way
57601df8552aSStephen M. Cameron 	 * using the doorbell register.
57611df8552aSStephen M. Cameron 	 */
576218867659SStephen M. Cameron 
576325c1e56aSStephen M. Cameron 	rc = hpsa_lookup_board_id(pdev, &board_id);
576460f923b9SRobert Elliott 	if (rc < 0) {
576560f923b9SRobert Elliott 		dev_warn(&pdev->dev, "Board ID not found\n");
576660f923b9SRobert Elliott 		return rc;
576760f923b9SRobert Elliott 	}
576860f923b9SRobert Elliott 	if (!ctlr_is_resettable(board_id)) {
576960f923b9SRobert Elliott 		dev_warn(&pdev->dev, "Controller not resettable\n");
577025c1e56aSStephen M. Cameron 		return -ENODEV;
577125c1e56aSStephen M. Cameron 	}
577246380786SStephen M. Cameron 
577346380786SStephen M. Cameron 	/* if controller is soft- but not hard resettable... */
577446380786SStephen M. Cameron 	if (!ctlr_is_hard_resettable(board_id))
577546380786SStephen M. Cameron 		return -ENOTSUPP; /* try soft reset later. */
577618867659SStephen M. Cameron 
5777270d05deSStephen M. Cameron 	/* Save the PCI command register */
5778270d05deSStephen M. Cameron 	pci_read_config_word(pdev, 4, &command_register);
5779270d05deSStephen M. Cameron 	pci_save_state(pdev);
57801df8552aSStephen M. Cameron 
57811df8552aSStephen M. Cameron 	/* find the first memory BAR, so we can find the cfg table */
57821df8552aSStephen M. Cameron 	rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
57831df8552aSStephen M. Cameron 	if (rc)
57841df8552aSStephen M. Cameron 		return rc;
57851df8552aSStephen M. Cameron 	vaddr = remap_pci_mem(paddr, 0x250);
57861df8552aSStephen M. Cameron 	if (!vaddr)
57871df8552aSStephen M. Cameron 		return -ENOMEM;
57881df8552aSStephen M. Cameron 
57891df8552aSStephen M. Cameron 	/* find cfgtable in order to check if reset via doorbell is supported */
57901df8552aSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
57911df8552aSStephen M. Cameron 					&cfg_base_addr_index, &cfg_offset);
57921df8552aSStephen M. Cameron 	if (rc)
57931df8552aSStephen M. Cameron 		goto unmap_vaddr;
57941df8552aSStephen M. Cameron 	cfgtable = remap_pci_mem(pci_resource_start(pdev,
57951df8552aSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
57961df8552aSStephen M. Cameron 	if (!cfgtable) {
57971df8552aSStephen M. Cameron 		rc = -ENOMEM;
57981df8552aSStephen M. Cameron 		goto unmap_vaddr;
57991df8552aSStephen M. Cameron 	}
5800580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(cfgtable);
5801580ada3cSStephen M. Cameron 	if (rc)
580203741d95STomas Henzl 		goto unmap_cfgtable;
58031df8552aSStephen M. Cameron 
5804cf0b08d0SStephen M. Cameron 	/* If reset via doorbell register is supported, use that.
5805cf0b08d0SStephen M. Cameron 	 * There are two such methods.  Favor the newest method.
5806cf0b08d0SStephen M. Cameron 	 */
58071df8552aSStephen M. Cameron 	misc_fw_support = readl(&cfgtable->misc_fw_support);
5808cf0b08d0SStephen M. Cameron 	use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
5809cf0b08d0SStephen M. Cameron 	if (use_doorbell) {
5810cf0b08d0SStephen M. Cameron 		use_doorbell = DOORBELL_CTLR_RESET2;
5811cf0b08d0SStephen M. Cameron 	} else {
58121df8552aSStephen M. Cameron 		use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
5813cf0b08d0SStephen M. Cameron 		if (use_doorbell) {
5814050f7147SStephen Cameron 			dev_warn(&pdev->dev,
5815050f7147SStephen Cameron 				"Soft reset not supported. Firmware update is required.\n");
581664670ac8SStephen M. Cameron 			rc = -ENOTSUPP; /* try soft reset */
5817cf0b08d0SStephen M. Cameron 			goto unmap_cfgtable;
5818cf0b08d0SStephen M. Cameron 		}
5819cf0b08d0SStephen M. Cameron 	}
58201df8552aSStephen M. Cameron 
58211df8552aSStephen M. Cameron 	rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
58221df8552aSStephen M. Cameron 	if (rc)
58231df8552aSStephen M. Cameron 		goto unmap_cfgtable;
5824edd16368SStephen M. Cameron 
5825270d05deSStephen M. Cameron 	pci_restore_state(pdev);
5826270d05deSStephen M. Cameron 	pci_write_config_word(pdev, 4, command_register);
5827edd16368SStephen M. Cameron 
58281df8552aSStephen M. Cameron 	/* Some devices (notably the HP Smart Array 5i Controller)
58291df8552aSStephen M. Cameron 	   need a little pause here */
58301df8552aSStephen M. Cameron 	msleep(HPSA_POST_RESET_PAUSE_MSECS);
58311df8552aSStephen M. Cameron 
5832fe5389c8SStephen M. Cameron 	rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
5833fe5389c8SStephen M. Cameron 	if (rc) {
5834fe5389c8SStephen M. Cameron 		dev_warn(&pdev->dev,
5835050f7147SStephen Cameron 			"Failed waiting for board to become ready after hard reset\n");
5836fe5389c8SStephen M. Cameron 		goto unmap_cfgtable;
5837fe5389c8SStephen M. Cameron 	}
5838fe5389c8SStephen M. Cameron 
5839580ada3cSStephen M. Cameron 	rc = controller_reset_failed(vaddr);
5840580ada3cSStephen M. Cameron 	if (rc < 0)
5841580ada3cSStephen M. Cameron 		goto unmap_cfgtable;
5842580ada3cSStephen M. Cameron 	if (rc) {
584364670ac8SStephen M. Cameron 		dev_warn(&pdev->dev, "Unable to successfully reset "
584464670ac8SStephen M. Cameron 			"controller. Will try soft reset.\n");
584564670ac8SStephen M. Cameron 		rc = -ENOTSUPP;
5846580ada3cSStephen M. Cameron 	} else {
584764670ac8SStephen M. Cameron 		dev_info(&pdev->dev, "board ready after hard reset.\n");
58481df8552aSStephen M. Cameron 	}
58491df8552aSStephen M. Cameron 
58501df8552aSStephen M. Cameron unmap_cfgtable:
58511df8552aSStephen M. Cameron 	iounmap(cfgtable);
58521df8552aSStephen M. Cameron 
58531df8552aSStephen M. Cameron unmap_vaddr:
58541df8552aSStephen M. Cameron 	iounmap(vaddr);
58551df8552aSStephen M. Cameron 	return rc;
5856edd16368SStephen M. Cameron }
5857edd16368SStephen M. Cameron 
5858edd16368SStephen M. Cameron /*
5859edd16368SStephen M. Cameron  *  We cannot read the structure directly, for portability we must use
5860edd16368SStephen M. Cameron  *   the io functions.
5861edd16368SStephen M. Cameron  *   This is for debug only.
5862edd16368SStephen M. Cameron  */
586342a91641SDon Brace static void print_cfg_table(struct device *dev, struct CfgTable __iomem *tb)
5864edd16368SStephen M. Cameron {
586558f8665cSStephen M. Cameron #ifdef HPSA_DEBUG
5866edd16368SStephen M. Cameron 	int i;
5867edd16368SStephen M. Cameron 	char temp_name[17];
5868edd16368SStephen M. Cameron 
5869edd16368SStephen M. Cameron 	dev_info(dev, "Controller Configuration information\n");
5870edd16368SStephen M. Cameron 	dev_info(dev, "------------------------------------\n");
5871edd16368SStephen M. Cameron 	for (i = 0; i < 4; i++)
5872edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->Signature[i]));
5873edd16368SStephen M. Cameron 	temp_name[4] = '\0';
5874edd16368SStephen M. Cameron 	dev_info(dev, "   Signature = %s\n", temp_name);
5875edd16368SStephen M. Cameron 	dev_info(dev, "   Spec Number = %d\n", readl(&(tb->SpecValence)));
5876edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods supported = 0x%x\n",
5877edd16368SStephen M. Cameron 	       readl(&(tb->TransportSupport)));
5878edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods active = 0x%x\n",
5879edd16368SStephen M. Cameron 	       readl(&(tb->TransportActive)));
5880edd16368SStephen M. Cameron 	dev_info(dev, "   Requested transport Method = 0x%x\n",
5881edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.TransportRequest)));
5882edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Delay = 0x%x\n",
5883edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntDelay)));
5884edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Count = 0x%x\n",
5885edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntCount)));
588669d6e33dSRobert Elliott 	dev_info(dev, "   Max outstanding commands = %d\n",
5887edd16368SStephen M. Cameron 	       readl(&(tb->CmdsOutMax)));
5888edd16368SStephen M. Cameron 	dev_info(dev, "   Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
5889edd16368SStephen M. Cameron 	for (i = 0; i < 16; i++)
5890edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->ServerName[i]));
5891edd16368SStephen M. Cameron 	temp_name[16] = '\0';
5892edd16368SStephen M. Cameron 	dev_info(dev, "   Server Name = %s\n", temp_name);
5893edd16368SStephen M. Cameron 	dev_info(dev, "   Heartbeat Counter = 0x%x\n\n\n",
5894edd16368SStephen M. Cameron 		readl(&(tb->HeartBeat)));
5895edd16368SStephen M. Cameron #endif				/* HPSA_DEBUG */
589658f8665cSStephen M. Cameron }
5897edd16368SStephen M. Cameron 
5898edd16368SStephen M. Cameron static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
5899edd16368SStephen M. Cameron {
5900edd16368SStephen M. Cameron 	int i, offset, mem_type, bar_type;
5901edd16368SStephen M. Cameron 
5902edd16368SStephen M. Cameron 	if (pci_bar_addr == PCI_BASE_ADDRESS_0)	/* looking for BAR zero? */
5903edd16368SStephen M. Cameron 		return 0;
5904edd16368SStephen M. Cameron 	offset = 0;
5905edd16368SStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
5906edd16368SStephen M. Cameron 		bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
5907edd16368SStephen M. Cameron 		if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
5908edd16368SStephen M. Cameron 			offset += 4;
5909edd16368SStephen M. Cameron 		else {
5910edd16368SStephen M. Cameron 			mem_type = pci_resource_flags(pdev, i) &
5911edd16368SStephen M. Cameron 			    PCI_BASE_ADDRESS_MEM_TYPE_MASK;
5912edd16368SStephen M. Cameron 			switch (mem_type) {
5913edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_32:
5914edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_1M:
5915edd16368SStephen M. Cameron 				offset += 4;	/* 32 bit */
5916edd16368SStephen M. Cameron 				break;
5917edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_64:
5918edd16368SStephen M. Cameron 				offset += 8;
5919edd16368SStephen M. Cameron 				break;
5920edd16368SStephen M. Cameron 			default:	/* reserved in PCI 2.2 */
5921edd16368SStephen M. Cameron 				dev_warn(&pdev->dev,
5922edd16368SStephen M. Cameron 				       "base address is invalid\n");
5923edd16368SStephen M. Cameron 				return -1;
5924edd16368SStephen M. Cameron 				break;
5925edd16368SStephen M. Cameron 			}
5926edd16368SStephen M. Cameron 		}
5927edd16368SStephen M. Cameron 		if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
5928edd16368SStephen M. Cameron 			return i + 1;
5929edd16368SStephen M. Cameron 	}
5930edd16368SStephen M. Cameron 	return -1;
5931edd16368SStephen M. Cameron }
5932edd16368SStephen M. Cameron 
5933edd16368SStephen M. Cameron /* If MSI/MSI-X is supported by the kernel we will try to enable it on
5934050f7147SStephen Cameron  * controllers that are capable. If not, we use legacy INTx mode.
5935edd16368SStephen M. Cameron  */
5936edd16368SStephen M. Cameron 
59376f039790SGreg Kroah-Hartman static void hpsa_interrupt_mode(struct ctlr_info *h)
5938edd16368SStephen M. Cameron {
5939edd16368SStephen M. Cameron #ifdef CONFIG_PCI_MSI
5940254f796bSMatt Gates 	int err, i;
5941254f796bSMatt Gates 	struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
5942254f796bSMatt Gates 
5943254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++) {
5944254f796bSMatt Gates 		hpsa_msix_entries[i].vector = 0;
5945254f796bSMatt Gates 		hpsa_msix_entries[i].entry = i;
5946254f796bSMatt Gates 	}
5947edd16368SStephen M. Cameron 
5948edd16368SStephen M. Cameron 	/* Some boards advertise MSI but don't really support it */
59496b3f4c52SStephen M. Cameron 	if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
59506b3f4c52SStephen M. Cameron 	    (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
5951edd16368SStephen M. Cameron 		goto default_int_mode;
595255c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
5953050f7147SStephen Cameron 		dev_info(&h->pdev->dev, "MSI-X capable controller\n");
5954eee0f03aSHannes Reinecke 		h->msix_vector = MAX_REPLY_QUEUES;
5955f89439bcSStephen M. Cameron 		if (h->msix_vector > num_online_cpus())
5956f89439bcSStephen M. Cameron 			h->msix_vector = num_online_cpus();
595718fce3c4SAlexander Gordeev 		err = pci_enable_msix_range(h->pdev, hpsa_msix_entries,
595818fce3c4SAlexander Gordeev 					    1, h->msix_vector);
595918fce3c4SAlexander Gordeev 		if (err < 0) {
596018fce3c4SAlexander Gordeev 			dev_warn(&h->pdev->dev, "MSI-X init failed %d\n", err);
596118fce3c4SAlexander Gordeev 			h->msix_vector = 0;
596218fce3c4SAlexander Gordeev 			goto single_msi_mode;
596318fce3c4SAlexander Gordeev 		} else if (err < h->msix_vector) {
596455c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
5965edd16368SStephen M. Cameron 			       "available\n", err);
5966eee0f03aSHannes Reinecke 		}
596718fce3c4SAlexander Gordeev 		h->msix_vector = err;
5968eee0f03aSHannes Reinecke 		for (i = 0; i < h->msix_vector; i++)
5969eee0f03aSHannes Reinecke 			h->intr[i] = hpsa_msix_entries[i].vector;
5970eee0f03aSHannes Reinecke 		return;
5971edd16368SStephen M. Cameron 	}
597218fce3c4SAlexander Gordeev single_msi_mode:
597355c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
5974050f7147SStephen Cameron 		dev_info(&h->pdev->dev, "MSI capable controller\n");
597555c06c71SStephen M. Cameron 		if (!pci_enable_msi(h->pdev))
5976edd16368SStephen M. Cameron 			h->msi_vector = 1;
5977edd16368SStephen M. Cameron 		else
597855c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "MSI init failed\n");
5979edd16368SStephen M. Cameron 	}
5980edd16368SStephen M. Cameron default_int_mode:
5981edd16368SStephen M. Cameron #endif				/* CONFIG_PCI_MSI */
5982edd16368SStephen M. Cameron 	/* if we get here we're going to use the default interrupt mode */
5983a9a3a273SStephen M. Cameron 	h->intr[h->intr_mode] = h->pdev->irq;
5984edd16368SStephen M. Cameron }
5985edd16368SStephen M. Cameron 
59866f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
5987e5c880d1SStephen M. Cameron {
5988e5c880d1SStephen M. Cameron 	int i;
5989e5c880d1SStephen M. Cameron 	u32 subsystem_vendor_id, subsystem_device_id;
5990e5c880d1SStephen M. Cameron 
5991e5c880d1SStephen M. Cameron 	subsystem_vendor_id = pdev->subsystem_vendor;
5992e5c880d1SStephen M. Cameron 	subsystem_device_id = pdev->subsystem_device;
5993e5c880d1SStephen M. Cameron 	*board_id = ((subsystem_device_id << 16) & 0xffff0000) |
5994e5c880d1SStephen M. Cameron 		    subsystem_vendor_id;
5995e5c880d1SStephen M. Cameron 
5996e5c880d1SStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(products); i++)
5997e5c880d1SStephen M. Cameron 		if (*board_id == products[i].board_id)
5998e5c880d1SStephen M. Cameron 			return i;
5999e5c880d1SStephen M. Cameron 
60006798cc0aSStephen M. Cameron 	if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
60016798cc0aSStephen M. Cameron 		subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
60026798cc0aSStephen M. Cameron 		!hpsa_allow_any) {
6003e5c880d1SStephen M. Cameron 		dev_warn(&pdev->dev, "unrecognized board ID: "
6004e5c880d1SStephen M. Cameron 			"0x%08x, ignoring.\n", *board_id);
6005e5c880d1SStephen M. Cameron 			return -ENODEV;
6006e5c880d1SStephen M. Cameron 	}
6007e5c880d1SStephen M. Cameron 	return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
6008e5c880d1SStephen M. Cameron }
6009e5c880d1SStephen M. Cameron 
60106f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
60113a7774ceSStephen M. Cameron 				    unsigned long *memory_bar)
60123a7774ceSStephen M. Cameron {
60133a7774ceSStephen M. Cameron 	int i;
60143a7774ceSStephen M. Cameron 
60153a7774ceSStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
601612d2cd47SStephen M. Cameron 		if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
60173a7774ceSStephen M. Cameron 			/* addressing mode bits already removed */
601812d2cd47SStephen M. Cameron 			*memory_bar = pci_resource_start(pdev, i);
601912d2cd47SStephen M. Cameron 			dev_dbg(&pdev->dev, "memory BAR = %lx\n",
60203a7774ceSStephen M. Cameron 				*memory_bar);
60213a7774ceSStephen M. Cameron 			return 0;
60223a7774ceSStephen M. Cameron 		}
602312d2cd47SStephen M. Cameron 	dev_warn(&pdev->dev, "no memory BAR found\n");
60243a7774ceSStephen M. Cameron 	return -ENODEV;
60253a7774ceSStephen M. Cameron }
60263a7774ceSStephen M. Cameron 
60276f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
60286f039790SGreg Kroah-Hartman 				     int wait_for_ready)
60292c4c8c8bSStephen M. Cameron {
6030fe5389c8SStephen M. Cameron 	int i, iterations;
60312c4c8c8bSStephen M. Cameron 	u32 scratchpad;
6032fe5389c8SStephen M. Cameron 	if (wait_for_ready)
6033fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_READY_ITERATIONS;
6034fe5389c8SStephen M. Cameron 	else
6035fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
60362c4c8c8bSStephen M. Cameron 
6037fe5389c8SStephen M. Cameron 	for (i = 0; i < iterations; i++) {
6038fe5389c8SStephen M. Cameron 		scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
6039fe5389c8SStephen M. Cameron 		if (wait_for_ready) {
60402c4c8c8bSStephen M. Cameron 			if (scratchpad == HPSA_FIRMWARE_READY)
60412c4c8c8bSStephen M. Cameron 				return 0;
6042fe5389c8SStephen M. Cameron 		} else {
6043fe5389c8SStephen M. Cameron 			if (scratchpad != HPSA_FIRMWARE_READY)
6044fe5389c8SStephen M. Cameron 				return 0;
6045fe5389c8SStephen M. Cameron 		}
60462c4c8c8bSStephen M. Cameron 		msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
60472c4c8c8bSStephen M. Cameron 	}
6048fe5389c8SStephen M. Cameron 	dev_warn(&pdev->dev, "board not ready, timed out.\n");
60492c4c8c8bSStephen M. Cameron 	return -ENODEV;
60502c4c8c8bSStephen M. Cameron }
60512c4c8c8bSStephen M. Cameron 
60526f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
60536f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
6054a51fd47fSStephen M. Cameron 			       u64 *cfg_offset)
6055a51fd47fSStephen M. Cameron {
6056a51fd47fSStephen M. Cameron 	*cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
6057a51fd47fSStephen M. Cameron 	*cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
6058a51fd47fSStephen M. Cameron 	*cfg_base_addr &= (u32) 0x0000ffff;
6059a51fd47fSStephen M. Cameron 	*cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
6060a51fd47fSStephen M. Cameron 	if (*cfg_base_addr_index == -1) {
6061a51fd47fSStephen M. Cameron 		dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
6062a51fd47fSStephen M. Cameron 		return -ENODEV;
6063a51fd47fSStephen M. Cameron 	}
6064a51fd47fSStephen M. Cameron 	return 0;
6065a51fd47fSStephen M. Cameron }
6066a51fd47fSStephen M. Cameron 
60676f039790SGreg Kroah-Hartman static int hpsa_find_cfgtables(struct ctlr_info *h)
6068edd16368SStephen M. Cameron {
606901a02ffcSStephen M. Cameron 	u64 cfg_offset;
607001a02ffcSStephen M. Cameron 	u32 cfg_base_addr;
607101a02ffcSStephen M. Cameron 	u64 cfg_base_addr_index;
6072303932fdSDon Brace 	u32 trans_offset;
6073a51fd47fSStephen M. Cameron 	int rc;
607477c4495cSStephen M. Cameron 
6075a51fd47fSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
6076a51fd47fSStephen M. Cameron 		&cfg_base_addr_index, &cfg_offset);
6077a51fd47fSStephen M. Cameron 	if (rc)
6078a51fd47fSStephen M. Cameron 		return rc;
607977c4495cSStephen M. Cameron 	h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
6080a51fd47fSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
6081cd3c81c4SRobert Elliott 	if (!h->cfgtable) {
6082cd3c81c4SRobert Elliott 		dev_err(&h->pdev->dev, "Failed mapping cfgtable\n");
608377c4495cSStephen M. Cameron 		return -ENOMEM;
6084cd3c81c4SRobert Elliott 	}
6085580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(h->cfgtable);
6086580ada3cSStephen M. Cameron 	if (rc)
6087580ada3cSStephen M. Cameron 		return rc;
608877c4495cSStephen M. Cameron 	/* Find performant mode table. */
6089a51fd47fSStephen M. Cameron 	trans_offset = readl(&h->cfgtable->TransMethodOffset);
609077c4495cSStephen M. Cameron 	h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
609177c4495cSStephen M. Cameron 				cfg_base_addr_index)+cfg_offset+trans_offset,
609277c4495cSStephen M. Cameron 				sizeof(*h->transtable));
609377c4495cSStephen M. Cameron 	if (!h->transtable)
609477c4495cSStephen M. Cameron 		return -ENOMEM;
609577c4495cSStephen M. Cameron 	return 0;
609677c4495cSStephen M. Cameron }
609777c4495cSStephen M. Cameron 
60986f039790SGreg Kroah-Hartman static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
6099cba3d38bSStephen M. Cameron {
6100cba3d38bSStephen M. Cameron 	h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
610172ceeaecSStephen M. Cameron 
610272ceeaecSStephen M. Cameron 	/* Limit commands in memory limited kdump scenario. */
610372ceeaecSStephen M. Cameron 	if (reset_devices && h->max_commands > 32)
610472ceeaecSStephen M. Cameron 		h->max_commands = 32;
610572ceeaecSStephen M. Cameron 
6106cba3d38bSStephen M. Cameron 	if (h->max_commands < 16) {
6107cba3d38bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "Controller reports "
6108cba3d38bSStephen M. Cameron 			"max supported commands of %d, an obvious lie. "
6109cba3d38bSStephen M. Cameron 			"Using 16.  Ensure that firmware is up to date.\n",
6110cba3d38bSStephen M. Cameron 			h->max_commands);
6111cba3d38bSStephen M. Cameron 		h->max_commands = 16;
6112cba3d38bSStephen M. Cameron 	}
6113cba3d38bSStephen M. Cameron }
6114cba3d38bSStephen M. Cameron 
6115c7ee65b3SWebb Scales /* If the controller reports that the total max sg entries is greater than 512,
6116c7ee65b3SWebb Scales  * then we know that chained SG blocks work.  (Original smart arrays did not
6117c7ee65b3SWebb Scales  * support chained SG blocks and would return zero for max sg entries.)
6118c7ee65b3SWebb Scales  */
6119c7ee65b3SWebb Scales static int hpsa_supports_chained_sg_blocks(struct ctlr_info *h)
6120c7ee65b3SWebb Scales {
6121c7ee65b3SWebb Scales 	return h->maxsgentries > 512;
6122c7ee65b3SWebb Scales }
6123c7ee65b3SWebb Scales 
6124b93d7536SStephen M. Cameron /* Interrogate the hardware for some limits:
6125b93d7536SStephen M. Cameron  * max commands, max SG elements without chaining, and with chaining,
6126b93d7536SStephen M. Cameron  * SG chain block size, etc.
6127b93d7536SStephen M. Cameron  */
61286f039790SGreg Kroah-Hartman static void hpsa_find_board_params(struct ctlr_info *h)
6129b93d7536SStephen M. Cameron {
6130cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
613145fcb86eSStephen Cameron 	h->nr_cmds = h->max_commands;
6132b93d7536SStephen M. Cameron 	h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
6133283b4a9bSStephen M. Cameron 	h->fw_support = readl(&(h->cfgtable->misc_fw_support));
6134c7ee65b3SWebb Scales 	if (hpsa_supports_chained_sg_blocks(h)) {
6135c7ee65b3SWebb Scales 		/* Limit in-command s/g elements to 32 save dma'able memory. */
6136b93d7536SStephen M. Cameron 		h->max_cmd_sg_entries = 32;
61371a63ea6fSWebb Scales 		h->chainsize = h->maxsgentries - h->max_cmd_sg_entries;
6138b93d7536SStephen M. Cameron 		h->maxsgentries--; /* save one for chain pointer */
6139b93d7536SStephen M. Cameron 	} else {
6140c7ee65b3SWebb Scales 		/*
6141c7ee65b3SWebb Scales 		 * Original smart arrays supported at most 31 s/g entries
6142c7ee65b3SWebb Scales 		 * embedded inline in the command (trying to use more
6143c7ee65b3SWebb Scales 		 * would lock up the controller)
6144c7ee65b3SWebb Scales 		 */
6145c7ee65b3SWebb Scales 		h->max_cmd_sg_entries = 31;
61461a63ea6fSWebb Scales 		h->maxsgentries = 31; /* default to traditional values */
6147c7ee65b3SWebb Scales 		h->chainsize = 0;
6148b93d7536SStephen M. Cameron 	}
614975167d2cSStephen M. Cameron 
615075167d2cSStephen M. Cameron 	/* Find out what task management functions are supported and cache */
615175167d2cSStephen M. Cameron 	h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
61520e7a7fceSScott Teel 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
61530e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
61540e7a7fceSScott Teel 	if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
61550e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
6156b93d7536SStephen M. Cameron }
6157b93d7536SStephen M. Cameron 
615876c46e49SStephen M. Cameron static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
615976c46e49SStephen M. Cameron {
61600fc9fd40SAkinobu Mita 	if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
6161050f7147SStephen Cameron 		dev_err(&h->pdev->dev, "not a valid CISS config table\n");
616276c46e49SStephen M. Cameron 		return false;
616376c46e49SStephen M. Cameron 	}
616476c46e49SStephen M. Cameron 	return true;
616576c46e49SStephen M. Cameron }
616676c46e49SStephen M. Cameron 
616797a5e98cSStephen M. Cameron static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
6168f7c39101SStephen M. Cameron {
616997a5e98cSStephen M. Cameron 	u32 driver_support;
6170f7c39101SStephen M. Cameron 
617197a5e98cSStephen M. Cameron 	driver_support = readl(&(h->cfgtable->driver_support));
61720b9e7b74SArnd Bergmann 	/* Need to enable prefetch in the SCSI core for 6400 in x86 */
61730b9e7b74SArnd Bergmann #ifdef CONFIG_X86
617497a5e98cSStephen M. Cameron 	driver_support |= ENABLE_SCSI_PREFETCH;
6175f7c39101SStephen M. Cameron #endif
617628e13446SStephen M. Cameron 	driver_support |= ENABLE_UNIT_ATTN;
617728e13446SStephen M. Cameron 	writel(driver_support, &(h->cfgtable->driver_support));
6178f7c39101SStephen M. Cameron }
6179f7c39101SStephen M. Cameron 
61803d0eab67SStephen M. Cameron /* Disable DMA prefetch for the P600.  Otherwise an ASIC bug may result
61813d0eab67SStephen M. Cameron  * in a prefetch beyond physical memory.
61823d0eab67SStephen M. Cameron  */
61833d0eab67SStephen M. Cameron static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
61843d0eab67SStephen M. Cameron {
61853d0eab67SStephen M. Cameron 	u32 dma_prefetch;
61863d0eab67SStephen M. Cameron 
61873d0eab67SStephen M. Cameron 	if (h->board_id != 0x3225103C)
61883d0eab67SStephen M. Cameron 		return;
61893d0eab67SStephen M. Cameron 	dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
61903d0eab67SStephen M. Cameron 	dma_prefetch |= 0x8000;
61913d0eab67SStephen M. Cameron 	writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
61923d0eab67SStephen M. Cameron }
61933d0eab67SStephen M. Cameron 
6194c706a795SRobert Elliott static int hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
619576438d08SStephen M. Cameron {
619676438d08SStephen M. Cameron 	int i;
619776438d08SStephen M. Cameron 	u32 doorbell_value;
619876438d08SStephen M. Cameron 	unsigned long flags;
619976438d08SStephen M. Cameron 	/* wait until the clear_event_notify bit 6 is cleared by controller. */
6200007e7aa9SRobert Elliott 	for (i = 0; i < MAX_CLEAR_EVENT_WAIT; i++) {
620176438d08SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
620276438d08SStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
620376438d08SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
620476438d08SStephen M. Cameron 		if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
6205c706a795SRobert Elliott 			goto done;
620676438d08SStephen M. Cameron 		/* delay and try again */
6207007e7aa9SRobert Elliott 		msleep(CLEAR_EVENT_WAIT_INTERVAL);
620876438d08SStephen M. Cameron 	}
6209c706a795SRobert Elliott 	return -ENODEV;
6210c706a795SRobert Elliott done:
6211c706a795SRobert Elliott 	return 0;
621276438d08SStephen M. Cameron }
621376438d08SStephen M. Cameron 
6214c706a795SRobert Elliott static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
6215eb6b2ae9SStephen M. Cameron {
6216eb6b2ae9SStephen M. Cameron 	int i;
62176eaf46fdSStephen M. Cameron 	u32 doorbell_value;
62186eaf46fdSStephen M. Cameron 	unsigned long flags;
6219eb6b2ae9SStephen M. Cameron 
6220eb6b2ae9SStephen M. Cameron 	/* under certain very rare conditions, this can take awhile.
6221eb6b2ae9SStephen M. Cameron 	 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
6222eb6b2ae9SStephen M. Cameron 	 * as we enter this code.)
6223eb6b2ae9SStephen M. Cameron 	 */
6224007e7aa9SRobert Elliott 	for (i = 0; i < MAX_MODE_CHANGE_WAIT; i++) {
62256eaf46fdSStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
62266eaf46fdSStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
62276eaf46fdSStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
6228382be668SDan Carpenter 		if (!(doorbell_value & CFGTBL_ChangeReq))
6229c706a795SRobert Elliott 			goto done;
6230eb6b2ae9SStephen M. Cameron 		/* delay and try again */
6231007e7aa9SRobert Elliott 		msleep(MODE_CHANGE_WAIT_INTERVAL);
6232eb6b2ae9SStephen M. Cameron 	}
6233c706a795SRobert Elliott 	return -ENODEV;
6234c706a795SRobert Elliott done:
6235c706a795SRobert Elliott 	return 0;
62363f4336f3SStephen M. Cameron }
62373f4336f3SStephen M. Cameron 
6238c706a795SRobert Elliott /* return -ENODEV or other reason on error, 0 on success */
62396f039790SGreg Kroah-Hartman static int hpsa_enter_simple_mode(struct ctlr_info *h)
62403f4336f3SStephen M. Cameron {
62413f4336f3SStephen M. Cameron 	u32 trans_support;
62423f4336f3SStephen M. Cameron 
62433f4336f3SStephen M. Cameron 	trans_support = readl(&(h->cfgtable->TransportSupport));
62443f4336f3SStephen M. Cameron 	if (!(trans_support & SIMPLE_MODE))
62453f4336f3SStephen M. Cameron 		return -ENOTSUPP;
62463f4336f3SStephen M. Cameron 
62473f4336f3SStephen M. Cameron 	h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
6248283b4a9bSStephen M. Cameron 
62493f4336f3SStephen M. Cameron 	/* Update the field, and then ring the doorbell */
62503f4336f3SStephen M. Cameron 	writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
6251b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
62523f4336f3SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
6253c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h))
6254c706a795SRobert Elliott 		goto error;
6255eb6b2ae9SStephen M. Cameron 	print_cfg_table(&h->pdev->dev, h->cfgtable);
6256283b4a9bSStephen M. Cameron 	if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
6257283b4a9bSStephen M. Cameron 		goto error;
6258960a30e7SStephen M. Cameron 	h->transMethod = CFGTBL_Trans_Simple;
6259eb6b2ae9SStephen M. Cameron 	return 0;
6260283b4a9bSStephen M. Cameron error:
6261050f7147SStephen Cameron 	dev_err(&h->pdev->dev, "failed to enter simple mode\n");
6262283b4a9bSStephen M. Cameron 	return -ENODEV;
6263eb6b2ae9SStephen M. Cameron }
6264eb6b2ae9SStephen M. Cameron 
62656f039790SGreg Kroah-Hartman static int hpsa_pci_init(struct ctlr_info *h)
626677c4495cSStephen M. Cameron {
6267eb6b2ae9SStephen M. Cameron 	int prod_index, err;
6268edd16368SStephen M. Cameron 
6269e5c880d1SStephen M. Cameron 	prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
6270e5c880d1SStephen M. Cameron 	if (prod_index < 0)
627160f923b9SRobert Elliott 		return prod_index;
6272e5c880d1SStephen M. Cameron 	h->product_name = products[prod_index].product_name;
6273e5c880d1SStephen M. Cameron 	h->access = *(products[prod_index].access);
6274e5c880d1SStephen M. Cameron 
6275e5a44df8SMatthew Garrett 	pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
6276e5a44df8SMatthew Garrett 			       PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
6277e5a44df8SMatthew Garrett 
627855c06c71SStephen M. Cameron 	err = pci_enable_device(h->pdev);
6279edd16368SStephen M. Cameron 	if (err) {
628055c06c71SStephen M. Cameron 		dev_warn(&h->pdev->dev, "unable to enable PCI device\n");
6281edd16368SStephen M. Cameron 		return err;
6282edd16368SStephen M. Cameron 	}
6283edd16368SStephen M. Cameron 
6284f79cfec6SStephen M. Cameron 	err = pci_request_regions(h->pdev, HPSA);
6285edd16368SStephen M. Cameron 	if (err) {
628655c06c71SStephen M. Cameron 		dev_err(&h->pdev->dev,
628755c06c71SStephen M. Cameron 			"cannot obtain PCI resources, aborting\n");
6288edd16368SStephen M. Cameron 		return err;
6289edd16368SStephen M. Cameron 	}
62904fa604e1SRobert Elliott 
62914fa604e1SRobert Elliott 	pci_set_master(h->pdev);
62924fa604e1SRobert Elliott 
62936b3f4c52SStephen M. Cameron 	hpsa_interrupt_mode(h);
629412d2cd47SStephen M. Cameron 	err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
62953a7774ceSStephen M. Cameron 	if (err)
6296edd16368SStephen M. Cameron 		goto err_out_free_res;
6297edd16368SStephen M. Cameron 	h->vaddr = remap_pci_mem(h->paddr, 0x250);
6298204892e9SStephen M. Cameron 	if (!h->vaddr) {
6299204892e9SStephen M. Cameron 		err = -ENOMEM;
6300204892e9SStephen M. Cameron 		goto err_out_free_res;
6301204892e9SStephen M. Cameron 	}
6302fe5389c8SStephen M. Cameron 	err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
63032c4c8c8bSStephen M. Cameron 	if (err)
6304edd16368SStephen M. Cameron 		goto err_out_free_res;
630577c4495cSStephen M. Cameron 	err = hpsa_find_cfgtables(h);
630677c4495cSStephen M. Cameron 	if (err)
6307edd16368SStephen M. Cameron 		goto err_out_free_res;
6308b93d7536SStephen M. Cameron 	hpsa_find_board_params(h);
6309edd16368SStephen M. Cameron 
631076c46e49SStephen M. Cameron 	if (!hpsa_CISS_signature_present(h)) {
6311edd16368SStephen M. Cameron 		err = -ENODEV;
6312edd16368SStephen M. Cameron 		goto err_out_free_res;
6313edd16368SStephen M. Cameron 	}
631497a5e98cSStephen M. Cameron 	hpsa_set_driver_support_bits(h);
63153d0eab67SStephen M. Cameron 	hpsa_p600_dma_prefetch_quirk(h);
6316eb6b2ae9SStephen M. Cameron 	err = hpsa_enter_simple_mode(h);
6317eb6b2ae9SStephen M. Cameron 	if (err)
6318edd16368SStephen M. Cameron 		goto err_out_free_res;
6319edd16368SStephen M. Cameron 	return 0;
6320edd16368SStephen M. Cameron 
6321edd16368SStephen M. Cameron err_out_free_res:
6322204892e9SStephen M. Cameron 	if (h->transtable)
6323204892e9SStephen M. Cameron 		iounmap(h->transtable);
6324204892e9SStephen M. Cameron 	if (h->cfgtable)
6325204892e9SStephen M. Cameron 		iounmap(h->cfgtable);
6326204892e9SStephen M. Cameron 	if (h->vaddr)
6327204892e9SStephen M. Cameron 		iounmap(h->vaddr);
6328f0bd0b68SStephen M. Cameron 	pci_disable_device(h->pdev);
632955c06c71SStephen M. Cameron 	pci_release_regions(h->pdev);
6330edd16368SStephen M. Cameron 	return err;
6331edd16368SStephen M. Cameron }
6332edd16368SStephen M. Cameron 
63336f039790SGreg Kroah-Hartman static void hpsa_hba_inquiry(struct ctlr_info *h)
6334339b2b14SStephen M. Cameron {
6335339b2b14SStephen M. Cameron 	int rc;
6336339b2b14SStephen M. Cameron 
6337339b2b14SStephen M. Cameron #define HBA_INQUIRY_BYTE_COUNT 64
6338339b2b14SStephen M. Cameron 	h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
6339339b2b14SStephen M. Cameron 	if (!h->hba_inquiry_data)
6340339b2b14SStephen M. Cameron 		return;
6341339b2b14SStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
6342339b2b14SStephen M. Cameron 		h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
6343339b2b14SStephen M. Cameron 	if (rc != 0) {
6344339b2b14SStephen M. Cameron 		kfree(h->hba_inquiry_data);
6345339b2b14SStephen M. Cameron 		h->hba_inquiry_data = NULL;
6346339b2b14SStephen M. Cameron 	}
6347339b2b14SStephen M. Cameron }
6348339b2b14SStephen M. Cameron 
63496f039790SGreg Kroah-Hartman static int hpsa_init_reset_devices(struct pci_dev *pdev)
6350edd16368SStephen M. Cameron {
63511df8552aSStephen M. Cameron 	int rc, i;
63523b747298STomas Henzl 	void __iomem *vaddr;
6353edd16368SStephen M. Cameron 
63544c2a8c40SStephen M. Cameron 	if (!reset_devices)
63554c2a8c40SStephen M. Cameron 		return 0;
63564c2a8c40SStephen M. Cameron 
6357132aa220STomas Henzl 	/* kdump kernel is loading, we don't know in which state is
6358132aa220STomas Henzl 	 * the pci interface. The dev->enable_cnt is equal zero
6359132aa220STomas Henzl 	 * so we call enable+disable, wait a while and switch it on.
6360132aa220STomas Henzl 	 */
6361132aa220STomas Henzl 	rc = pci_enable_device(pdev);
6362132aa220STomas Henzl 	if (rc) {
6363132aa220STomas Henzl 		dev_warn(&pdev->dev, "Failed to enable PCI device\n");
6364132aa220STomas Henzl 		return -ENODEV;
6365132aa220STomas Henzl 	}
6366132aa220STomas Henzl 	pci_disable_device(pdev);
6367132aa220STomas Henzl 	msleep(260);			/* a randomly chosen number */
6368132aa220STomas Henzl 	rc = pci_enable_device(pdev);
6369132aa220STomas Henzl 	if (rc) {
6370132aa220STomas Henzl 		dev_warn(&pdev->dev, "failed to enable device.\n");
6371132aa220STomas Henzl 		return -ENODEV;
6372132aa220STomas Henzl 	}
63734fa604e1SRobert Elliott 
6374859c75abSTomas Henzl 	pci_set_master(pdev);
63754fa604e1SRobert Elliott 
63763b747298STomas Henzl 	vaddr = pci_ioremap_bar(pdev, 0);
63773b747298STomas Henzl 	if (vaddr == NULL) {
63783b747298STomas Henzl 		rc = -ENOMEM;
63793b747298STomas Henzl 		goto out_disable;
63803b747298STomas Henzl 	}
63813b747298STomas Henzl 	writel(SA5_INTR_OFF, vaddr + SA5_REPLY_INTR_MASK_OFFSET);
63823b747298STomas Henzl 	iounmap(vaddr);
63833b747298STomas Henzl 
63841df8552aSStephen M. Cameron 	/* Reset the controller with a PCI power-cycle or via doorbell */
63851df8552aSStephen M. Cameron 	rc = hpsa_kdump_hard_reset_controller(pdev);
6386edd16368SStephen M. Cameron 
63871df8552aSStephen M. Cameron 	/* -ENOTSUPP here means we cannot reset the controller
63881df8552aSStephen M. Cameron 	 * but it's already (and still) up and running in
638918867659SStephen M. Cameron 	 * "performant mode".  Or, it might be 640x, which can't reset
639018867659SStephen M. Cameron 	 * due to concerns about shared bbwc between 6402/6404 pair.
63911df8552aSStephen M. Cameron 	 */
6392adf1b3a3SRobert Elliott 	if (rc)
6393132aa220STomas Henzl 		goto out_disable;
6394edd16368SStephen M. Cameron 
6395edd16368SStephen M. Cameron 	/* Now try to get the controller to respond to a no-op */
63961ba66c9cSRobert Elliott 	dev_info(&pdev->dev, "Waiting for controller to respond to no-op\n");
6397edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
6398edd16368SStephen M. Cameron 		if (hpsa_noop(pdev) == 0)
6399edd16368SStephen M. Cameron 			break;
6400edd16368SStephen M. Cameron 		else
6401edd16368SStephen M. Cameron 			dev_warn(&pdev->dev, "no-op failed%s\n",
6402edd16368SStephen M. Cameron 					(i < 11 ? "; re-trying" : ""));
6403edd16368SStephen M. Cameron 	}
6404132aa220STomas Henzl 
6405132aa220STomas Henzl out_disable:
6406132aa220STomas Henzl 
6407132aa220STomas Henzl 	pci_disable_device(pdev);
6408132aa220STomas Henzl 	return rc;
6409edd16368SStephen M. Cameron }
6410edd16368SStephen M. Cameron 
64116f039790SGreg Kroah-Hartman static int hpsa_allocate_cmd_pool(struct ctlr_info *h)
64122e9d1b36SStephen M. Cameron {
64132e9d1b36SStephen M. Cameron 	h->cmd_pool_bits = kzalloc(
64142e9d1b36SStephen M. Cameron 		DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
64152e9d1b36SStephen M. Cameron 		sizeof(unsigned long), GFP_KERNEL);
64162e9d1b36SStephen M. Cameron 	h->cmd_pool = pci_alloc_consistent(h->pdev,
64172e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->cmd_pool),
64182e9d1b36SStephen M. Cameron 		    &(h->cmd_pool_dhandle));
64192e9d1b36SStephen M. Cameron 	h->errinfo_pool = pci_alloc_consistent(h->pdev,
64202e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->errinfo_pool),
64212e9d1b36SStephen M. Cameron 		    &(h->errinfo_pool_dhandle));
64222e9d1b36SStephen M. Cameron 	if ((h->cmd_pool_bits == NULL)
64232e9d1b36SStephen M. Cameron 	    || (h->cmd_pool == NULL)
64242e9d1b36SStephen M. Cameron 	    || (h->errinfo_pool == NULL)) {
64252e9d1b36SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory in %s", __func__);
64262c143342SRobert Elliott 		goto clean_up;
64272e9d1b36SStephen M. Cameron 	}
64282e9d1b36SStephen M. Cameron 	return 0;
64292c143342SRobert Elliott clean_up:
64302c143342SRobert Elliott 	hpsa_free_cmd_pool(h);
64312c143342SRobert Elliott 	return -ENOMEM;
64322e9d1b36SStephen M. Cameron }
64332e9d1b36SStephen M. Cameron 
64342e9d1b36SStephen M. Cameron static void hpsa_free_cmd_pool(struct ctlr_info *h)
64352e9d1b36SStephen M. Cameron {
64362e9d1b36SStephen M. Cameron 	kfree(h->cmd_pool_bits);
64372e9d1b36SStephen M. Cameron 	if (h->cmd_pool)
64382e9d1b36SStephen M. Cameron 		pci_free_consistent(h->pdev,
64392e9d1b36SStephen M. Cameron 			    h->nr_cmds * sizeof(struct CommandList),
64402e9d1b36SStephen M. Cameron 			    h->cmd_pool, h->cmd_pool_dhandle);
6441aca9012aSStephen M. Cameron 	if (h->ioaccel2_cmd_pool)
6442aca9012aSStephen M. Cameron 		pci_free_consistent(h->pdev,
6443aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
6444aca9012aSStephen M. Cameron 			h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
64452e9d1b36SStephen M. Cameron 	if (h->errinfo_pool)
64462e9d1b36SStephen M. Cameron 		pci_free_consistent(h->pdev,
64472e9d1b36SStephen M. Cameron 			    h->nr_cmds * sizeof(struct ErrorInfo),
64482e9d1b36SStephen M. Cameron 			    h->errinfo_pool,
64492e9d1b36SStephen M. Cameron 			    h->errinfo_pool_dhandle);
6450e1f7de0cSMatt Gates 	if (h->ioaccel_cmd_pool)
6451e1f7de0cSMatt Gates 		pci_free_consistent(h->pdev,
6452e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(struct io_accel1_cmd),
6453e1f7de0cSMatt Gates 			h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
64542e9d1b36SStephen M. Cameron }
64552e9d1b36SStephen M. Cameron 
645641b3cf08SStephen M. Cameron static void hpsa_irq_affinity_hints(struct ctlr_info *h)
645741b3cf08SStephen M. Cameron {
6458ec429952SFabian Frederick 	int i, cpu;
645941b3cf08SStephen M. Cameron 
646041b3cf08SStephen M. Cameron 	cpu = cpumask_first(cpu_online_mask);
646141b3cf08SStephen M. Cameron 	for (i = 0; i < h->msix_vector; i++) {
6462ec429952SFabian Frederick 		irq_set_affinity_hint(h->intr[i], get_cpu_mask(cpu));
646341b3cf08SStephen M. Cameron 		cpu = cpumask_next(cpu, cpu_online_mask);
646441b3cf08SStephen M. Cameron 	}
646541b3cf08SStephen M. Cameron }
646641b3cf08SStephen M. Cameron 
6467ec501a18SRobert Elliott /* clear affinity hints and free MSI-X, MSI, or legacy INTx vectors */
6468ec501a18SRobert Elliott static void hpsa_free_irqs(struct ctlr_info *h)
6469ec501a18SRobert Elliott {
6470ec501a18SRobert Elliott 	int i;
6471ec501a18SRobert Elliott 
6472ec501a18SRobert Elliott 	if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
6473ec501a18SRobert Elliott 		/* Single reply queue, only one irq to free */
6474ec501a18SRobert Elliott 		i = h->intr_mode;
6475ec501a18SRobert Elliott 		irq_set_affinity_hint(h->intr[i], NULL);
6476ec501a18SRobert Elliott 		free_irq(h->intr[i], &h->q[i]);
6477ec501a18SRobert Elliott 		return;
6478ec501a18SRobert Elliott 	}
6479ec501a18SRobert Elliott 
6480ec501a18SRobert Elliott 	for (i = 0; i < h->msix_vector; i++) {
6481ec501a18SRobert Elliott 		irq_set_affinity_hint(h->intr[i], NULL);
6482ec501a18SRobert Elliott 		free_irq(h->intr[i], &h->q[i]);
6483ec501a18SRobert Elliott 	}
6484a4e17fc1SRobert Elliott 	for (; i < MAX_REPLY_QUEUES; i++)
6485a4e17fc1SRobert Elliott 		h->q[i] = 0;
6486ec501a18SRobert Elliott }
6487ec501a18SRobert Elliott 
64889ee61794SRobert Elliott /* returns 0 on success; cleans up and returns -Enn on error */
64899ee61794SRobert Elliott static int hpsa_request_irqs(struct ctlr_info *h,
64900ae01a32SStephen M. Cameron 	irqreturn_t (*msixhandler)(int, void *),
64910ae01a32SStephen M. Cameron 	irqreturn_t (*intxhandler)(int, void *))
64920ae01a32SStephen M. Cameron {
6493254f796bSMatt Gates 	int rc, i;
64940ae01a32SStephen M. Cameron 
6495254f796bSMatt Gates 	/*
6496254f796bSMatt Gates 	 * initialize h->q[x] = x so that interrupt handlers know which
6497254f796bSMatt Gates 	 * queue to process.
6498254f796bSMatt Gates 	 */
6499254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++)
6500254f796bSMatt Gates 		h->q[i] = (u8) i;
6501254f796bSMatt Gates 
6502eee0f03aSHannes Reinecke 	if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
6503254f796bSMatt Gates 		/* If performant mode and MSI-X, use multiple reply queues */
6504a4e17fc1SRobert Elliott 		for (i = 0; i < h->msix_vector; i++) {
6505254f796bSMatt Gates 			rc = request_irq(h->intr[i], msixhandler,
6506254f796bSMatt Gates 					0, h->devname,
6507254f796bSMatt Gates 					&h->q[i]);
6508a4e17fc1SRobert Elliott 			if (rc) {
6509a4e17fc1SRobert Elliott 				int j;
6510a4e17fc1SRobert Elliott 
6511a4e17fc1SRobert Elliott 				dev_err(&h->pdev->dev,
6512a4e17fc1SRobert Elliott 					"failed to get irq %d for %s\n",
6513a4e17fc1SRobert Elliott 				       h->intr[i], h->devname);
6514a4e17fc1SRobert Elliott 				for (j = 0; j < i; j++) {
6515a4e17fc1SRobert Elliott 					free_irq(h->intr[j], &h->q[j]);
6516a4e17fc1SRobert Elliott 					h->q[j] = 0;
6517a4e17fc1SRobert Elliott 				}
6518a4e17fc1SRobert Elliott 				for (; j < MAX_REPLY_QUEUES; j++)
6519a4e17fc1SRobert Elliott 					h->q[j] = 0;
6520a4e17fc1SRobert Elliott 				return rc;
6521a4e17fc1SRobert Elliott 			}
6522a4e17fc1SRobert Elliott 		}
652341b3cf08SStephen M. Cameron 		hpsa_irq_affinity_hints(h);
6524254f796bSMatt Gates 	} else {
6525254f796bSMatt Gates 		/* Use single reply pool */
6526eee0f03aSHannes Reinecke 		if (h->msix_vector > 0 || h->msi_vector) {
6527254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
6528254f796bSMatt Gates 				msixhandler, 0, h->devname,
6529254f796bSMatt Gates 				&h->q[h->intr_mode]);
6530254f796bSMatt Gates 		} else {
6531254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
6532254f796bSMatt Gates 				intxhandler, IRQF_SHARED, h->devname,
6533254f796bSMatt Gates 				&h->q[h->intr_mode]);
6534254f796bSMatt Gates 		}
6535254f796bSMatt Gates 	}
65360ae01a32SStephen M. Cameron 	if (rc) {
65370ae01a32SStephen M. Cameron 		dev_err(&h->pdev->dev, "unable to get irq %d for %s\n",
65380ae01a32SStephen M. Cameron 		       h->intr[h->intr_mode], h->devname);
65390ae01a32SStephen M. Cameron 		return -ENODEV;
65400ae01a32SStephen M. Cameron 	}
65410ae01a32SStephen M. Cameron 	return 0;
65420ae01a32SStephen M. Cameron }
65430ae01a32SStephen M. Cameron 
65446f039790SGreg Kroah-Hartman static int hpsa_kdump_soft_reset(struct ctlr_info *h)
654564670ac8SStephen M. Cameron {
654664670ac8SStephen M. Cameron 	if (hpsa_send_host_reset(h, RAID_CTLR_LUNID,
654764670ac8SStephen M. Cameron 		HPSA_RESET_TYPE_CONTROLLER)) {
654864670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Resetting array controller failed.\n");
654964670ac8SStephen M. Cameron 		return -EIO;
655064670ac8SStephen M. Cameron 	}
655164670ac8SStephen M. Cameron 
655264670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
655364670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) {
655464670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
655564670ac8SStephen M. Cameron 		return -1;
655664670ac8SStephen M. Cameron 	}
655764670ac8SStephen M. Cameron 
655864670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
655964670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) {
656064670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Board failed to become ready "
656164670ac8SStephen M. Cameron 			"after soft reset.\n");
656264670ac8SStephen M. Cameron 		return -1;
656364670ac8SStephen M. Cameron 	}
656464670ac8SStephen M. Cameron 
656564670ac8SStephen M. Cameron 	return 0;
656664670ac8SStephen M. Cameron }
656764670ac8SStephen M. Cameron 
65680097f0f4SStephen M. Cameron static void hpsa_free_irqs_and_disable_msix(struct ctlr_info *h)
656964670ac8SStephen M. Cameron {
6570ec501a18SRobert Elliott 	hpsa_free_irqs(h);
657164670ac8SStephen M. Cameron #ifdef CONFIG_PCI_MSI
65720097f0f4SStephen M. Cameron 	if (h->msix_vector) {
65730097f0f4SStephen M. Cameron 		if (h->pdev->msix_enabled)
657464670ac8SStephen M. Cameron 			pci_disable_msix(h->pdev);
65750097f0f4SStephen M. Cameron 	} else if (h->msi_vector) {
65760097f0f4SStephen M. Cameron 		if (h->pdev->msi_enabled)
657764670ac8SStephen M. Cameron 			pci_disable_msi(h->pdev);
65780097f0f4SStephen M. Cameron 	}
657964670ac8SStephen M. Cameron #endif /* CONFIG_PCI_MSI */
65800097f0f4SStephen M. Cameron }
65810097f0f4SStephen M. Cameron 
6582072b0518SStephen M. Cameron static void hpsa_free_reply_queues(struct ctlr_info *h)
6583072b0518SStephen M. Cameron {
6584072b0518SStephen M. Cameron 	int i;
6585072b0518SStephen M. Cameron 
6586072b0518SStephen M. Cameron 	for (i = 0; i < h->nreply_queues; i++) {
6587072b0518SStephen M. Cameron 		if (!h->reply_queue[i].head)
6588072b0518SStephen M. Cameron 			continue;
6589072b0518SStephen M. Cameron 		pci_free_consistent(h->pdev, h->reply_queue_size,
6590072b0518SStephen M. Cameron 			h->reply_queue[i].head, h->reply_queue[i].busaddr);
6591072b0518SStephen M. Cameron 		h->reply_queue[i].head = NULL;
6592072b0518SStephen M. Cameron 		h->reply_queue[i].busaddr = 0;
6593072b0518SStephen M. Cameron 	}
6594072b0518SStephen M. Cameron }
6595072b0518SStephen M. Cameron 
65960097f0f4SStephen M. Cameron static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
65970097f0f4SStephen M. Cameron {
65980097f0f4SStephen M. Cameron 	hpsa_free_irqs_and_disable_msix(h);
659964670ac8SStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
660064670ac8SStephen M. Cameron 	hpsa_free_cmd_pool(h);
6601e1f7de0cSMatt Gates 	kfree(h->ioaccel1_blockFetchTable);
660264670ac8SStephen M. Cameron 	kfree(h->blockFetchTable);
6603072b0518SStephen M. Cameron 	hpsa_free_reply_queues(h);
660464670ac8SStephen M. Cameron 	if (h->vaddr)
660564670ac8SStephen M. Cameron 		iounmap(h->vaddr);
660664670ac8SStephen M. Cameron 	if (h->transtable)
660764670ac8SStephen M. Cameron 		iounmap(h->transtable);
660864670ac8SStephen M. Cameron 	if (h->cfgtable)
660964670ac8SStephen M. Cameron 		iounmap(h->cfgtable);
6610132aa220STomas Henzl 	pci_disable_device(h->pdev);
661164670ac8SStephen M. Cameron 	pci_release_regions(h->pdev);
661264670ac8SStephen M. Cameron 	kfree(h);
661364670ac8SStephen M. Cameron }
661464670ac8SStephen M. Cameron 
6615a0c12413SStephen M. Cameron /* Called when controller lockup detected. */
6616f2405db8SDon Brace static void fail_all_outstanding_cmds(struct ctlr_info *h)
6617a0c12413SStephen M. Cameron {
6618281a7fd0SWebb Scales 	int i, refcount;
6619281a7fd0SWebb Scales 	struct CommandList *c;
6620a0c12413SStephen M. Cameron 
6621080ef1ccSDon Brace 	flush_workqueue(h->resubmit_wq); /* ensure all cmds are fully built */
6622f2405db8SDon Brace 	for (i = 0; i < h->nr_cmds; i++) {
6623f2405db8SDon Brace 		c = h->cmd_pool + i;
6624281a7fd0SWebb Scales 		refcount = atomic_inc_return(&c->refcount);
6625281a7fd0SWebb Scales 		if (refcount > 1) {
6626a0c12413SStephen M. Cameron 			c->err_info->CommandStatus = CMD_HARDWARE_ERR;
66275a3d16f5SStephen M. Cameron 			finish_cmd(c);
6628a0c12413SStephen M. Cameron 		}
6629281a7fd0SWebb Scales 		cmd_free(h, c);
6630281a7fd0SWebb Scales 	}
6631a0c12413SStephen M. Cameron }
6632a0c12413SStephen M. Cameron 
6633094963daSStephen M. Cameron static void set_lockup_detected_for_all_cpus(struct ctlr_info *h, u32 value)
6634094963daSStephen M. Cameron {
6635094963daSStephen M. Cameron 	int i, cpu;
6636094963daSStephen M. Cameron 
6637094963daSStephen M. Cameron 	cpu = cpumask_first(cpu_online_mask);
6638094963daSStephen M. Cameron 	for (i = 0; i < num_online_cpus(); i++) {
6639094963daSStephen M. Cameron 		u32 *lockup_detected;
6640094963daSStephen M. Cameron 		lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
6641094963daSStephen M. Cameron 		*lockup_detected = value;
6642094963daSStephen M. Cameron 		cpu = cpumask_next(cpu, cpu_online_mask);
6643094963daSStephen M. Cameron 	}
6644094963daSStephen M. Cameron 	wmb(); /* be sure the per-cpu variables are out to memory */
6645094963daSStephen M. Cameron }
6646094963daSStephen M. Cameron 
6647a0c12413SStephen M. Cameron static void controller_lockup_detected(struct ctlr_info *h)
6648a0c12413SStephen M. Cameron {
6649a0c12413SStephen M. Cameron 	unsigned long flags;
6650094963daSStephen M. Cameron 	u32 lockup_detected;
6651a0c12413SStephen M. Cameron 
6652a0c12413SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
6653a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
6654094963daSStephen M. Cameron 	lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
6655094963daSStephen M. Cameron 	if (!lockup_detected) {
6656094963daSStephen M. Cameron 		/* no heartbeat, but controller gave us a zero. */
6657094963daSStephen M. Cameron 		dev_warn(&h->pdev->dev,
6658094963daSStephen M. Cameron 			"lockup detected but scratchpad register is zero\n");
6659094963daSStephen M. Cameron 		lockup_detected = 0xffffffff;
6660094963daSStephen M. Cameron 	}
6661094963daSStephen M. Cameron 	set_lockup_detected_for_all_cpus(h, lockup_detected);
6662a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
6663a0c12413SStephen M. Cameron 	dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x\n",
6664094963daSStephen M. Cameron 			lockup_detected);
6665a0c12413SStephen M. Cameron 	pci_disable_device(h->pdev);
6666f2405db8SDon Brace 	fail_all_outstanding_cmds(h);
6667a0c12413SStephen M. Cameron }
6668a0c12413SStephen M. Cameron 
6669a0c12413SStephen M. Cameron static void detect_controller_lockup(struct ctlr_info *h)
6670a0c12413SStephen M. Cameron {
6671a0c12413SStephen M. Cameron 	u64 now;
6672a0c12413SStephen M. Cameron 	u32 heartbeat;
6673a0c12413SStephen M. Cameron 	unsigned long flags;
6674a0c12413SStephen M. Cameron 
6675a0c12413SStephen M. Cameron 	now = get_jiffies_64();
6676a0c12413SStephen M. Cameron 	/* If we've received an interrupt recently, we're ok. */
6677a0c12413SStephen M. Cameron 	if (time_after64(h->last_intr_timestamp +
6678e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
6679a0c12413SStephen M. Cameron 		return;
6680a0c12413SStephen M. Cameron 
6681a0c12413SStephen M. Cameron 	/*
6682a0c12413SStephen M. Cameron 	 * If we've already checked the heartbeat recently, we're ok.
6683a0c12413SStephen M. Cameron 	 * This could happen if someone sends us a signal. We
6684a0c12413SStephen M. Cameron 	 * otherwise don't care about signals in this thread.
6685a0c12413SStephen M. Cameron 	 */
6686a0c12413SStephen M. Cameron 	if (time_after64(h->last_heartbeat_timestamp +
6687e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
6688a0c12413SStephen M. Cameron 		return;
6689a0c12413SStephen M. Cameron 
6690a0c12413SStephen M. Cameron 	/* If heartbeat has not changed since we last looked, we're not ok. */
6691a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
6692a0c12413SStephen M. Cameron 	heartbeat = readl(&h->cfgtable->HeartBeat);
6693a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
6694a0c12413SStephen M. Cameron 	if (h->last_heartbeat == heartbeat) {
6695a0c12413SStephen M. Cameron 		controller_lockup_detected(h);
6696a0c12413SStephen M. Cameron 		return;
6697a0c12413SStephen M. Cameron 	}
6698a0c12413SStephen M. Cameron 
6699a0c12413SStephen M. Cameron 	/* We're ok. */
6700a0c12413SStephen M. Cameron 	h->last_heartbeat = heartbeat;
6701a0c12413SStephen M. Cameron 	h->last_heartbeat_timestamp = now;
6702a0c12413SStephen M. Cameron }
6703a0c12413SStephen M. Cameron 
67049846590eSStephen M. Cameron static void hpsa_ack_ctlr_events(struct ctlr_info *h)
670576438d08SStephen M. Cameron {
670676438d08SStephen M. Cameron 	int i;
670776438d08SStephen M. Cameron 	char *event_type;
670876438d08SStephen M. Cameron 
6709e4aa3e6aSStephen Cameron 	if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
6710e4aa3e6aSStephen Cameron 		return;
6711e4aa3e6aSStephen Cameron 
671276438d08SStephen M. Cameron 	/* Ask the controller to clear the events we're handling. */
67131f7cee8cSStephen M. Cameron 	if ((h->transMethod & (CFGTBL_Trans_io_accel1
67141f7cee8cSStephen M. Cameron 			| CFGTBL_Trans_io_accel2)) &&
671576438d08SStephen M. Cameron 		(h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
671676438d08SStephen M. Cameron 		 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
671776438d08SStephen M. Cameron 
671876438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
671976438d08SStephen M. Cameron 			event_type = "state change";
672076438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
672176438d08SStephen M. Cameron 			event_type = "configuration change";
672276438d08SStephen M. Cameron 		/* Stop sending new RAID offload reqs via the IO accelerator */
672376438d08SStephen M. Cameron 		scsi_block_requests(h->scsi_host);
672476438d08SStephen M. Cameron 		for (i = 0; i < h->ndevices; i++)
672576438d08SStephen M. Cameron 			h->dev[i]->offload_enabled = 0;
672623100dd9SStephen M. Cameron 		hpsa_drain_accel_commands(h);
672776438d08SStephen M. Cameron 		/* Set 'accelerator path config change' bit */
672876438d08SStephen M. Cameron 		dev_warn(&h->pdev->dev,
672976438d08SStephen M. Cameron 			"Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
673076438d08SStephen M. Cameron 			h->events, event_type);
673176438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
673276438d08SStephen M. Cameron 		/* Set the "clear event notify field update" bit 6 */
673376438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
673476438d08SStephen M. Cameron 		/* Wait until ctlr clears 'clear event notify field', bit 6 */
673576438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
673676438d08SStephen M. Cameron 		scsi_unblock_requests(h->scsi_host);
673776438d08SStephen M. Cameron 	} else {
673876438d08SStephen M. Cameron 		/* Acknowledge controller notification events. */
673976438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
674076438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
674176438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
674276438d08SStephen M. Cameron #if 0
674376438d08SStephen M. Cameron 		writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
674476438d08SStephen M. Cameron 		hpsa_wait_for_mode_change_ack(h);
674576438d08SStephen M. Cameron #endif
674676438d08SStephen M. Cameron 	}
67479846590eSStephen M. Cameron 	return;
674876438d08SStephen M. Cameron }
674976438d08SStephen M. Cameron 
675076438d08SStephen M. Cameron /* Check a register on the controller to see if there are configuration
675176438d08SStephen M. Cameron  * changes (added/changed/removed logical drives, etc.) which mean that
6752e863d68eSScott Teel  * we should rescan the controller for devices.
6753e863d68eSScott Teel  * Also check flag for driver-initiated rescan.
675476438d08SStephen M. Cameron  */
67559846590eSStephen M. Cameron static int hpsa_ctlr_needs_rescan(struct ctlr_info *h)
675676438d08SStephen M. Cameron {
675776438d08SStephen M. Cameron 	if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
67589846590eSStephen M. Cameron 		return 0;
675976438d08SStephen M. Cameron 
676076438d08SStephen M. Cameron 	h->events = readl(&(h->cfgtable->event_notify));
67619846590eSStephen M. Cameron 	return h->events & RESCAN_REQUIRED_EVENT_BITS;
67629846590eSStephen M. Cameron }
676376438d08SStephen M. Cameron 
676476438d08SStephen M. Cameron /*
67659846590eSStephen M. Cameron  * Check if any of the offline devices have become ready
676676438d08SStephen M. Cameron  */
67679846590eSStephen M. Cameron static int hpsa_offline_devices_ready(struct ctlr_info *h)
67689846590eSStephen M. Cameron {
67699846590eSStephen M. Cameron 	unsigned long flags;
67709846590eSStephen M. Cameron 	struct offline_device_entry *d;
67719846590eSStephen M. Cameron 	struct list_head *this, *tmp;
67729846590eSStephen M. Cameron 
67739846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
67749846590eSStephen M. Cameron 	list_for_each_safe(this, tmp, &h->offline_device_list) {
67759846590eSStephen M. Cameron 		d = list_entry(this, struct offline_device_entry,
67769846590eSStephen M. Cameron 				offline_list);
67779846590eSStephen M. Cameron 		spin_unlock_irqrestore(&h->offline_device_lock, flags);
6778d1fea47cSStephen M. Cameron 		if (!hpsa_volume_offline(h, d->scsi3addr)) {
6779d1fea47cSStephen M. Cameron 			spin_lock_irqsave(&h->offline_device_lock, flags);
6780d1fea47cSStephen M. Cameron 			list_del(&d->offline_list);
6781d1fea47cSStephen M. Cameron 			spin_unlock_irqrestore(&h->offline_device_lock, flags);
67829846590eSStephen M. Cameron 			return 1;
6783d1fea47cSStephen M. Cameron 		}
67849846590eSStephen M. Cameron 		spin_lock_irqsave(&h->offline_device_lock, flags);
678576438d08SStephen M. Cameron 	}
67869846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
67879846590eSStephen M. Cameron 	return 0;
67889846590eSStephen M. Cameron }
67899846590eSStephen M. Cameron 
6790*6636e7f4SDon Brace static void hpsa_rescan_ctlr_worker(struct work_struct *work)
6791a0c12413SStephen M. Cameron {
6792a0c12413SStephen M. Cameron 	unsigned long flags;
67938a98db73SStephen M. Cameron 	struct ctlr_info *h = container_of(to_delayed_work(work),
6794*6636e7f4SDon Brace 					struct ctlr_info, rescan_ctlr_work);
6795*6636e7f4SDon Brace 
6796*6636e7f4SDon Brace 
6797*6636e7f4SDon Brace 	if (h->remove_in_progress)
67988a98db73SStephen M. Cameron 		return;
67999846590eSStephen M. Cameron 
68009846590eSStephen M. Cameron 	if (hpsa_ctlr_needs_rescan(h) || hpsa_offline_devices_ready(h)) {
68019846590eSStephen M. Cameron 		scsi_host_get(h->scsi_host);
68029846590eSStephen M. Cameron 		hpsa_ack_ctlr_events(h);
68039846590eSStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
68049846590eSStephen M. Cameron 		scsi_host_put(h->scsi_host);
68059846590eSStephen M. Cameron 	}
6806*6636e7f4SDon Brace 	spin_lock_irqsave(&h->lock, flags);
6807*6636e7f4SDon Brace 	if (!h->remove_in_progress)
6808*6636e7f4SDon Brace 		queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
6809*6636e7f4SDon Brace 				h->heartbeat_sample_interval);
6810*6636e7f4SDon Brace 	spin_unlock_irqrestore(&h->lock, flags);
6811*6636e7f4SDon Brace }
6812*6636e7f4SDon Brace 
6813*6636e7f4SDon Brace static void hpsa_monitor_ctlr_worker(struct work_struct *work)
6814*6636e7f4SDon Brace {
6815*6636e7f4SDon Brace 	unsigned long flags;
6816*6636e7f4SDon Brace 	struct ctlr_info *h = container_of(to_delayed_work(work),
6817*6636e7f4SDon Brace 					struct ctlr_info, monitor_ctlr_work);
6818*6636e7f4SDon Brace 
6819*6636e7f4SDon Brace 	detect_controller_lockup(h);
6820*6636e7f4SDon Brace 	if (lockup_detected(h))
6821*6636e7f4SDon Brace 		return;
68229846590eSStephen M. Cameron 
68238a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
6824*6636e7f4SDon Brace 	if (!h->remove_in_progress)
68258a98db73SStephen M. Cameron 		schedule_delayed_work(&h->monitor_ctlr_work,
68268a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
68278a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
6828a0c12413SStephen M. Cameron }
6829a0c12413SStephen M. Cameron 
6830*6636e7f4SDon Brace static struct workqueue_struct *hpsa_create_controller_wq(struct ctlr_info *h,
6831*6636e7f4SDon Brace 						char *name)
6832*6636e7f4SDon Brace {
6833*6636e7f4SDon Brace 	struct workqueue_struct *wq = NULL;
6834*6636e7f4SDon Brace 	char wq_name[20];
6835*6636e7f4SDon Brace 
6836*6636e7f4SDon Brace 	snprintf(wq_name, sizeof(wq_name), "%s_%d_hpsa", name, h->ctlr);
6837*6636e7f4SDon Brace 	wq = alloc_ordered_workqueue(wq_name, 0);
6838*6636e7f4SDon Brace 	if (!wq)
6839*6636e7f4SDon Brace 		dev_err(&h->pdev->dev, "failed to create %s workqueue\n", name);
6840*6636e7f4SDon Brace 
6841*6636e7f4SDon Brace 	return wq;
6842*6636e7f4SDon Brace }
6843*6636e7f4SDon Brace 
68446f039790SGreg Kroah-Hartman static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
68454c2a8c40SStephen M. Cameron {
68464c2a8c40SStephen M. Cameron 	int dac, rc;
68474c2a8c40SStephen M. Cameron 	struct ctlr_info *h;
684864670ac8SStephen M. Cameron 	int try_soft_reset = 0;
684964670ac8SStephen M. Cameron 	unsigned long flags;
68504c2a8c40SStephen M. Cameron 
68514c2a8c40SStephen M. Cameron 	if (number_of_controllers == 0)
68524c2a8c40SStephen M. Cameron 		printk(KERN_INFO DRIVER_NAME "\n");
68534c2a8c40SStephen M. Cameron 
68544c2a8c40SStephen M. Cameron 	rc = hpsa_init_reset_devices(pdev);
685564670ac8SStephen M. Cameron 	if (rc) {
685664670ac8SStephen M. Cameron 		if (rc != -ENOTSUPP)
68574c2a8c40SStephen M. Cameron 			return rc;
685864670ac8SStephen M. Cameron 		/* If the reset fails in a particular way (it has no way to do
685964670ac8SStephen M. Cameron 		 * a proper hard reset, so returns -ENOTSUPP) we can try to do
686064670ac8SStephen M. Cameron 		 * a soft reset once we get the controller configured up to the
686164670ac8SStephen M. Cameron 		 * point that it can accept a command.
686264670ac8SStephen M. Cameron 		 */
686364670ac8SStephen M. Cameron 		try_soft_reset = 1;
686464670ac8SStephen M. Cameron 		rc = 0;
686564670ac8SStephen M. Cameron 	}
686664670ac8SStephen M. Cameron 
686764670ac8SStephen M. Cameron reinit_after_soft_reset:
68684c2a8c40SStephen M. Cameron 
6869303932fdSDon Brace 	/* Command structures must be aligned on a 32-byte boundary because
6870303932fdSDon Brace 	 * the 5 lower bits of the address are used by the hardware. and by
6871303932fdSDon Brace 	 * the driver.  See comments in hpsa.h for more info.
6872303932fdSDon Brace 	 */
6873303932fdSDon Brace 	BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
6874edd16368SStephen M. Cameron 	h = kzalloc(sizeof(*h), GFP_KERNEL);
6875edd16368SStephen M. Cameron 	if (!h)
6876ecd9aad4SStephen M. Cameron 		return -ENOMEM;
6877edd16368SStephen M. Cameron 
687855c06c71SStephen M. Cameron 	h->pdev = pdev;
6879a9a3a273SStephen M. Cameron 	h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
68809846590eSStephen M. Cameron 	INIT_LIST_HEAD(&h->offline_device_list);
68816eaf46fdSStephen M. Cameron 	spin_lock_init(&h->lock);
68829846590eSStephen M. Cameron 	spin_lock_init(&h->offline_device_lock);
68836eaf46fdSStephen M. Cameron 	spin_lock_init(&h->scan_lock);
688434f0c627SDon Brace 	atomic_set(&h->passthru_cmds_avail, HPSA_MAX_CONCURRENT_PASSTHRUS);
6885094963daSStephen M. Cameron 
6886*6636e7f4SDon Brace 	h->rescan_ctlr_wq = hpsa_create_controller_wq(h, "rescan");
6887*6636e7f4SDon Brace 	if (!h->rescan_ctlr_wq) {
6888080ef1ccSDon Brace 		rc = -ENOMEM;
6889080ef1ccSDon Brace 		goto clean1;
6890080ef1ccSDon Brace 	}
6891*6636e7f4SDon Brace 
6892*6636e7f4SDon Brace 	h->resubmit_wq = hpsa_create_controller_wq(h, "resubmit");
6893*6636e7f4SDon Brace 	if (!h->resubmit_wq) {
6894*6636e7f4SDon Brace 		rc = -ENOMEM;
6895*6636e7f4SDon Brace 		goto clean1;
6896*6636e7f4SDon Brace 	}
6897*6636e7f4SDon Brace 
6898094963daSStephen M. Cameron 	/* Allocate and clear per-cpu variable lockup_detected */
6899094963daSStephen M. Cameron 	h->lockup_detected = alloc_percpu(u32);
69002a5ac326SStephen M. Cameron 	if (!h->lockup_detected) {
69012a5ac326SStephen M. Cameron 		rc = -ENOMEM;
6902094963daSStephen M. Cameron 		goto clean1;
69032a5ac326SStephen M. Cameron 	}
6904094963daSStephen M. Cameron 	set_lockup_detected_for_all_cpus(h, 0);
6905094963daSStephen M. Cameron 
690655c06c71SStephen M. Cameron 	rc = hpsa_pci_init(h);
6907ecd9aad4SStephen M. Cameron 	if (rc != 0)
6908edd16368SStephen M. Cameron 		goto clean1;
6909edd16368SStephen M. Cameron 
6910f79cfec6SStephen M. Cameron 	sprintf(h->devname, HPSA "%d", number_of_controllers);
6911edd16368SStephen M. Cameron 	h->ctlr = number_of_controllers;
6912edd16368SStephen M. Cameron 	number_of_controllers++;
6913edd16368SStephen M. Cameron 
6914edd16368SStephen M. Cameron 	/* configure PCI DMA stuff */
6915ecd9aad4SStephen M. Cameron 	rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
6916ecd9aad4SStephen M. Cameron 	if (rc == 0) {
6917edd16368SStephen M. Cameron 		dac = 1;
6918ecd9aad4SStephen M. Cameron 	} else {
6919ecd9aad4SStephen M. Cameron 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6920ecd9aad4SStephen M. Cameron 		if (rc == 0) {
6921edd16368SStephen M. Cameron 			dac = 0;
6922ecd9aad4SStephen M. Cameron 		} else {
6923edd16368SStephen M. Cameron 			dev_err(&pdev->dev, "no suitable DMA available\n");
6924edd16368SStephen M. Cameron 			goto clean1;
6925edd16368SStephen M. Cameron 		}
6926ecd9aad4SStephen M. Cameron 	}
6927edd16368SStephen M. Cameron 
6928edd16368SStephen M. Cameron 	/* make sure the board interrupts are off */
6929edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
693010f66018SStephen M. Cameron 
69319ee61794SRobert Elliott 	if (hpsa_request_irqs(h, do_hpsa_intr_msi, do_hpsa_intr_intx))
6932edd16368SStephen M. Cameron 		goto clean2;
6933303932fdSDon Brace 	dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
6934303932fdSDon Brace 	       h->devname, pdev->device,
6935a9a3a273SStephen M. Cameron 	       h->intr[h->intr_mode], dac ? "" : " not");
69368947fd10SRobert Elliott 	rc = hpsa_allocate_cmd_pool(h);
69378947fd10SRobert Elliott 	if (rc)
69388947fd10SRobert Elliott 		goto clean2_and_free_irqs;
693933a2ffceSStephen M. Cameron 	if (hpsa_allocate_sg_chain_blocks(h))
694033a2ffceSStephen M. Cameron 		goto clean4;
6941a08a8471SStephen M. Cameron 	init_waitqueue_head(&h->scan_wait_queue);
6942a08a8471SStephen M. Cameron 	h->scan_finished = 1; /* no scan currently in progress */
6943edd16368SStephen M. Cameron 
6944edd16368SStephen M. Cameron 	pci_set_drvdata(pdev, h);
69459a41338eSStephen M. Cameron 	h->ndevices = 0;
6946316b221aSStephen M. Cameron 	h->hba_mode_enabled = 0;
69479a41338eSStephen M. Cameron 	h->scsi_host = NULL;
69489a41338eSStephen M. Cameron 	spin_lock_init(&h->devlock);
694964670ac8SStephen M. Cameron 	hpsa_put_ctlr_into_performant_mode(h);
695064670ac8SStephen M. Cameron 
695164670ac8SStephen M. Cameron 	/* At this point, the controller is ready to take commands.
695264670ac8SStephen M. Cameron 	 * Now, if reset_devices and the hard reset didn't work, try
695364670ac8SStephen M. Cameron 	 * the soft reset and see if that works.
695464670ac8SStephen M. Cameron 	 */
695564670ac8SStephen M. Cameron 	if (try_soft_reset) {
695664670ac8SStephen M. Cameron 
695764670ac8SStephen M. Cameron 		/* This is kind of gross.  We may or may not get a completion
695864670ac8SStephen M. Cameron 		 * from the soft reset command, and if we do, then the value
695964670ac8SStephen M. Cameron 		 * from the fifo may or may not be valid.  So, we wait 10 secs
696064670ac8SStephen M. Cameron 		 * after the reset throwing away any completions we get during
696164670ac8SStephen M. Cameron 		 * that time.  Unregister the interrupt handler and register
696264670ac8SStephen M. Cameron 		 * fake ones to scoop up any residual completions.
696364670ac8SStephen M. Cameron 		 */
696464670ac8SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
696564670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
696664670ac8SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
6967ec501a18SRobert Elliott 		hpsa_free_irqs(h);
69689ee61794SRobert Elliott 		rc = hpsa_request_irqs(h, hpsa_msix_discard_completions,
696964670ac8SStephen M. Cameron 					hpsa_intx_discard_completions);
697064670ac8SStephen M. Cameron 		if (rc) {
69719ee61794SRobert Elliott 			dev_warn(&h->pdev->dev,
69729ee61794SRobert Elliott 				"Failed to request_irq after soft reset.\n");
697364670ac8SStephen M. Cameron 			goto clean4;
697464670ac8SStephen M. Cameron 		}
697564670ac8SStephen M. Cameron 
697664670ac8SStephen M. Cameron 		rc = hpsa_kdump_soft_reset(h);
697764670ac8SStephen M. Cameron 		if (rc)
697864670ac8SStephen M. Cameron 			/* Neither hard nor soft reset worked, we're hosed. */
697964670ac8SStephen M. Cameron 			goto clean4;
698064670ac8SStephen M. Cameron 
698164670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev, "Board READY.\n");
698264670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev,
698364670ac8SStephen M. Cameron 			"Waiting for stale completions to drain.\n");
698464670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_ON);
698564670ac8SStephen M. Cameron 		msleep(10000);
698664670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
698764670ac8SStephen M. Cameron 
698864670ac8SStephen M. Cameron 		rc = controller_reset_failed(h->cfgtable);
698964670ac8SStephen M. Cameron 		if (rc)
699064670ac8SStephen M. Cameron 			dev_info(&h->pdev->dev,
699164670ac8SStephen M. Cameron 				"Soft reset appears to have failed.\n");
699264670ac8SStephen M. Cameron 
699364670ac8SStephen M. Cameron 		/* since the controller's reset, we have to go back and re-init
699464670ac8SStephen M. Cameron 		 * everything.  Easiest to just forget what we've done and do it
699564670ac8SStephen M. Cameron 		 * all over again.
699664670ac8SStephen M. Cameron 		 */
699764670ac8SStephen M. Cameron 		hpsa_undo_allocations_after_kdump_soft_reset(h);
699864670ac8SStephen M. Cameron 		try_soft_reset = 0;
699964670ac8SStephen M. Cameron 		if (rc)
700064670ac8SStephen M. Cameron 			/* don't go to clean4, we already unallocated */
700164670ac8SStephen M. Cameron 			return -ENODEV;
700264670ac8SStephen M. Cameron 
700364670ac8SStephen M. Cameron 		goto reinit_after_soft_reset;
700464670ac8SStephen M. Cameron 	}
7005edd16368SStephen M. Cameron 
7006da0697bdSScott Teel 		/* Enable Accelerated IO path at driver layer */
7007da0697bdSScott Teel 		h->acciopath_status = 1;
7008da0697bdSScott Teel 
7009e863d68eSScott Teel 
7010edd16368SStephen M. Cameron 	/* Turn the interrupts on so we can service requests */
7011edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_ON);
7012edd16368SStephen M. Cameron 
7013339b2b14SStephen M. Cameron 	hpsa_hba_inquiry(h);
7014edd16368SStephen M. Cameron 	hpsa_register_scsi(h);	/* hook ourselves into SCSI subsystem */
70158a98db73SStephen M. Cameron 
70168a98db73SStephen M. Cameron 	/* Monitor the controller for firmware lockups */
70178a98db73SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
70188a98db73SStephen M. Cameron 	INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
70198a98db73SStephen M. Cameron 	schedule_delayed_work(&h->monitor_ctlr_work,
70208a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
7021*6636e7f4SDon Brace 	INIT_DELAYED_WORK(&h->rescan_ctlr_work, hpsa_rescan_ctlr_worker);
7022*6636e7f4SDon Brace 	queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
7023*6636e7f4SDon Brace 				h->heartbeat_sample_interval);
702488bf6d62SStephen M. Cameron 	return 0;
7025edd16368SStephen M. Cameron 
7026edd16368SStephen M. Cameron clean4:
702733a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
70282e9d1b36SStephen M. Cameron 	hpsa_free_cmd_pool(h);
70298947fd10SRobert Elliott clean2_and_free_irqs:
7030ec501a18SRobert Elliott 	hpsa_free_irqs(h);
7031edd16368SStephen M. Cameron clean2:
7032edd16368SStephen M. Cameron clean1:
7033080ef1ccSDon Brace 	if (h->resubmit_wq)
7034080ef1ccSDon Brace 		destroy_workqueue(h->resubmit_wq);
7035*6636e7f4SDon Brace 	if (h->rescan_ctlr_wq)
7036*6636e7f4SDon Brace 		destroy_workqueue(h->rescan_ctlr_wq);
7037094963daSStephen M. Cameron 	if (h->lockup_detected)
7038094963daSStephen M. Cameron 		free_percpu(h->lockup_detected);
7039edd16368SStephen M. Cameron 	kfree(h);
7040ecd9aad4SStephen M. Cameron 	return rc;
7041edd16368SStephen M. Cameron }
7042edd16368SStephen M. Cameron 
7043edd16368SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h)
7044edd16368SStephen M. Cameron {
7045edd16368SStephen M. Cameron 	char *flush_buf;
7046edd16368SStephen M. Cameron 	struct CommandList *c;
7047702890e3SStephen M. Cameron 
7048702890e3SStephen M. Cameron 	/* Don't bother trying to flush the cache if locked up */
7049094963daSStephen M. Cameron 	if (unlikely(lockup_detected(h)))
7050702890e3SStephen M. Cameron 		return;
7051edd16368SStephen M. Cameron 	flush_buf = kzalloc(4, GFP_KERNEL);
7052edd16368SStephen M. Cameron 	if (!flush_buf)
7053edd16368SStephen M. Cameron 		return;
7054edd16368SStephen M. Cameron 
705545fcb86eSStephen Cameron 	c = cmd_alloc(h);
7056edd16368SStephen M. Cameron 	if (!c) {
705745fcb86eSStephen Cameron 		dev_warn(&h->pdev->dev, "cmd_alloc returned NULL!\n");
7058edd16368SStephen M. Cameron 		goto out_of_memory;
7059edd16368SStephen M. Cameron 	}
7060a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
7061a2dac136SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_CMD)) {
7062a2dac136SStephen M. Cameron 		goto out;
7063a2dac136SStephen M. Cameron 	}
7064edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
7065edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus != 0)
7066a2dac136SStephen M. Cameron out:
7067edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
7068edd16368SStephen M. Cameron 			"error flushing cache on controller\n");
706945fcb86eSStephen Cameron 	cmd_free(h, c);
7070edd16368SStephen M. Cameron out_of_memory:
7071edd16368SStephen M. Cameron 	kfree(flush_buf);
7072edd16368SStephen M. Cameron }
7073edd16368SStephen M. Cameron 
7074edd16368SStephen M. Cameron static void hpsa_shutdown(struct pci_dev *pdev)
7075edd16368SStephen M. Cameron {
7076edd16368SStephen M. Cameron 	struct ctlr_info *h;
7077edd16368SStephen M. Cameron 
7078edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
7079edd16368SStephen M. Cameron 	/* Turn board interrupts off  and send the flush cache command
7080edd16368SStephen M. Cameron 	 * sendcmd will turn off interrupt, and send the flush...
7081edd16368SStephen M. Cameron 	 * To write all data in the battery backed cache to disks
7082edd16368SStephen M. Cameron 	 */
7083edd16368SStephen M. Cameron 	hpsa_flush_cache(h);
7084edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
70850097f0f4SStephen M. Cameron 	hpsa_free_irqs_and_disable_msix(h);
7086edd16368SStephen M. Cameron }
7087edd16368SStephen M. Cameron 
70886f039790SGreg Kroah-Hartman static void hpsa_free_device_info(struct ctlr_info *h)
708955e14e76SStephen M. Cameron {
709055e14e76SStephen M. Cameron 	int i;
709155e14e76SStephen M. Cameron 
709255e14e76SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++)
709355e14e76SStephen M. Cameron 		kfree(h->dev[i]);
709455e14e76SStephen M. Cameron }
709555e14e76SStephen M. Cameron 
70966f039790SGreg Kroah-Hartman static void hpsa_remove_one(struct pci_dev *pdev)
7097edd16368SStephen M. Cameron {
7098edd16368SStephen M. Cameron 	struct ctlr_info *h;
70998a98db73SStephen M. Cameron 	unsigned long flags;
7100edd16368SStephen M. Cameron 
7101edd16368SStephen M. Cameron 	if (pci_get_drvdata(pdev) == NULL) {
7102edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "unable to remove device\n");
7103edd16368SStephen M. Cameron 		return;
7104edd16368SStephen M. Cameron 	}
7105edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
71068a98db73SStephen M. Cameron 
71078a98db73SStephen M. Cameron 	/* Get rid of any controller monitoring work items */
71088a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
71098a98db73SStephen M. Cameron 	h->remove_in_progress = 1;
71108a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
7111*6636e7f4SDon Brace 	cancel_delayed_work_sync(&h->monitor_ctlr_work);
7112*6636e7f4SDon Brace 	cancel_delayed_work_sync(&h->rescan_ctlr_work);
7113*6636e7f4SDon Brace 	destroy_workqueue(h->rescan_ctlr_wq);
7114*6636e7f4SDon Brace 	destroy_workqueue(h->resubmit_wq);
7115edd16368SStephen M. Cameron 	hpsa_unregister_scsi(h);	/* unhook from SCSI subsystem */
7116edd16368SStephen M. Cameron 	hpsa_shutdown(pdev);
7117edd16368SStephen M. Cameron 	iounmap(h->vaddr);
7118204892e9SStephen M. Cameron 	iounmap(h->transtable);
7119204892e9SStephen M. Cameron 	iounmap(h->cfgtable);
712055e14e76SStephen M. Cameron 	hpsa_free_device_info(h);
712133a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
7122edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev,
7123edd16368SStephen M. Cameron 		h->nr_cmds * sizeof(struct CommandList),
7124edd16368SStephen M. Cameron 		h->cmd_pool, h->cmd_pool_dhandle);
7125edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev,
7126edd16368SStephen M. Cameron 		h->nr_cmds * sizeof(struct ErrorInfo),
7127edd16368SStephen M. Cameron 		h->errinfo_pool, h->errinfo_pool_dhandle);
7128072b0518SStephen M. Cameron 	hpsa_free_reply_queues(h);
7129edd16368SStephen M. Cameron 	kfree(h->cmd_pool_bits);
7130303932fdSDon Brace 	kfree(h->blockFetchTable);
7131e1f7de0cSMatt Gates 	kfree(h->ioaccel1_blockFetchTable);
7132aca9012aSStephen M. Cameron 	kfree(h->ioaccel2_blockFetchTable);
7133339b2b14SStephen M. Cameron 	kfree(h->hba_inquiry_data);
7134f0bd0b68SStephen M. Cameron 	pci_disable_device(pdev);
7135edd16368SStephen M. Cameron 	pci_release_regions(pdev);
7136094963daSStephen M. Cameron 	free_percpu(h->lockup_detected);
7137edd16368SStephen M. Cameron 	kfree(h);
7138edd16368SStephen M. Cameron }
7139edd16368SStephen M. Cameron 
7140edd16368SStephen M. Cameron static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
7141edd16368SStephen M. Cameron 	__attribute__((unused)) pm_message_t state)
7142edd16368SStephen M. Cameron {
7143edd16368SStephen M. Cameron 	return -ENOSYS;
7144edd16368SStephen M. Cameron }
7145edd16368SStephen M. Cameron 
7146edd16368SStephen M. Cameron static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
7147edd16368SStephen M. Cameron {
7148edd16368SStephen M. Cameron 	return -ENOSYS;
7149edd16368SStephen M. Cameron }
7150edd16368SStephen M. Cameron 
7151edd16368SStephen M. Cameron static struct pci_driver hpsa_pci_driver = {
7152f79cfec6SStephen M. Cameron 	.name = HPSA,
7153edd16368SStephen M. Cameron 	.probe = hpsa_init_one,
71546f039790SGreg Kroah-Hartman 	.remove = hpsa_remove_one,
7155edd16368SStephen M. Cameron 	.id_table = hpsa_pci_device_id,	/* id_table */
7156edd16368SStephen M. Cameron 	.shutdown = hpsa_shutdown,
7157edd16368SStephen M. Cameron 	.suspend = hpsa_suspend,
7158edd16368SStephen M. Cameron 	.resume = hpsa_resume,
7159edd16368SStephen M. Cameron };
7160edd16368SStephen M. Cameron 
7161303932fdSDon Brace /* Fill in bucket_map[], given nsgs (the max number of
7162303932fdSDon Brace  * scatter gather elements supported) and bucket[],
7163303932fdSDon Brace  * which is an array of 8 integers.  The bucket[] array
7164303932fdSDon Brace  * contains 8 different DMA transfer sizes (in 16
7165303932fdSDon Brace  * byte increments) which the controller uses to fetch
7166303932fdSDon Brace  * commands.  This function fills in bucket_map[], which
7167303932fdSDon Brace  * maps a given number of scatter gather elements to one of
7168303932fdSDon Brace  * the 8 DMA transfer sizes.  The point of it is to allow the
7169303932fdSDon Brace  * controller to only do as much DMA as needed to fetch the
7170303932fdSDon Brace  * command, with the DMA transfer size encoded in the lower
7171303932fdSDon Brace  * bits of the command address.
7172303932fdSDon Brace  */
7173303932fdSDon Brace static void  calc_bucket_map(int bucket[], int num_buckets,
71742b08b3e9SDon Brace 	int nsgs, int min_blocks, u32 *bucket_map)
7175303932fdSDon Brace {
7176303932fdSDon Brace 	int i, j, b, size;
7177303932fdSDon Brace 
7178303932fdSDon Brace 	/* Note, bucket_map must have nsgs+1 entries. */
7179303932fdSDon Brace 	for (i = 0; i <= nsgs; i++) {
7180303932fdSDon Brace 		/* Compute size of a command with i SG entries */
7181e1f7de0cSMatt Gates 		size = i + min_blocks;
7182303932fdSDon Brace 		b = num_buckets; /* Assume the biggest bucket */
7183303932fdSDon Brace 		/* Find the bucket that is just big enough */
7184e1f7de0cSMatt Gates 		for (j = 0; j < num_buckets; j++) {
7185303932fdSDon Brace 			if (bucket[j] >= size) {
7186303932fdSDon Brace 				b = j;
7187303932fdSDon Brace 				break;
7188303932fdSDon Brace 			}
7189303932fdSDon Brace 		}
7190303932fdSDon Brace 		/* for a command with i SG entries, use bucket b. */
7191303932fdSDon Brace 		bucket_map[i] = b;
7192303932fdSDon Brace 	}
7193303932fdSDon Brace }
7194303932fdSDon Brace 
7195c706a795SRobert Elliott /* return -ENODEV or other reason on error, 0 on success */
7196c706a795SRobert Elliott static int hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
7197303932fdSDon Brace {
71986c311b57SStephen M. Cameron 	int i;
71996c311b57SStephen M. Cameron 	unsigned long register_value;
7200e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
7201e1f7de0cSMatt Gates 			(trans_support & CFGTBL_Trans_use_short_tags) |
7202e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix |
7203b9af4937SStephen M. Cameron 			(trans_support & (CFGTBL_Trans_io_accel1 |
7204b9af4937SStephen M. Cameron 				CFGTBL_Trans_io_accel2));
7205e1f7de0cSMatt Gates 	struct access_method access = SA5_performant_access;
7206def342bdSStephen M. Cameron 
7207def342bdSStephen M. Cameron 	/* This is a bit complicated.  There are 8 registers on
7208def342bdSStephen M. Cameron 	 * the controller which we write to to tell it 8 different
7209def342bdSStephen M. Cameron 	 * sizes of commands which there may be.  It's a way of
7210def342bdSStephen M. Cameron 	 * reducing the DMA done to fetch each command.  Encoded into
7211def342bdSStephen M. Cameron 	 * each command's tag are 3 bits which communicate to the controller
7212def342bdSStephen M. Cameron 	 * which of the eight sizes that command fits within.  The size of
7213def342bdSStephen M. Cameron 	 * each command depends on how many scatter gather entries there are.
7214def342bdSStephen M. Cameron 	 * Each SG entry requires 16 bytes.  The eight registers are programmed
7215def342bdSStephen M. Cameron 	 * with the number of 16-byte blocks a command of that size requires.
7216def342bdSStephen M. Cameron 	 * The smallest command possible requires 5 such 16 byte blocks.
7217d66ae08bSStephen M. Cameron 	 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
7218def342bdSStephen M. Cameron 	 * blocks.  Note, this only extends to the SG entries contained
7219def342bdSStephen M. Cameron 	 * within the command block, and does not extend to chained blocks
7220def342bdSStephen M. Cameron 	 * of SG elements.   bft[] contains the eight values we write to
7221def342bdSStephen M. Cameron 	 * the registers.  They are not evenly distributed, but have more
7222def342bdSStephen M. Cameron 	 * sizes for small commands, and fewer sizes for larger commands.
7223def342bdSStephen M. Cameron 	 */
7224d66ae08bSStephen M. Cameron 	int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
7225b9af4937SStephen M. Cameron #define MIN_IOACCEL2_BFT_ENTRY 5
7226b9af4937SStephen M. Cameron #define HPSA_IOACCEL2_HEADER_SZ 4
7227b9af4937SStephen M. Cameron 	int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
7228b9af4937SStephen M. Cameron 			13, 14, 15, 16, 17, 18, 19,
7229b9af4937SStephen M. Cameron 			HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
7230b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
7231b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
7232b9af4937SStephen M. Cameron 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
7233b9af4937SStephen M. Cameron 				 16 * MIN_IOACCEL2_BFT_ENTRY);
7234b9af4937SStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
7235d66ae08bSStephen M. Cameron 	BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
7236303932fdSDon Brace 	/*  5 = 1 s/g entry or 4k
7237303932fdSDon Brace 	 *  6 = 2 s/g entry or 8k
7238303932fdSDon Brace 	 *  8 = 4 s/g entry or 16k
7239303932fdSDon Brace 	 * 10 = 6 s/g entry or 24k
7240303932fdSDon Brace 	 */
7241303932fdSDon Brace 
7242b3a52e79SStephen M. Cameron 	/* If the controller supports either ioaccel method then
7243b3a52e79SStephen M. Cameron 	 * we can also use the RAID stack submit path that does not
7244b3a52e79SStephen M. Cameron 	 * perform the superfluous readl() after each command submission.
7245b3a52e79SStephen M. Cameron 	 */
7246b3a52e79SStephen M. Cameron 	if (trans_support & (CFGTBL_Trans_io_accel1 | CFGTBL_Trans_io_accel2))
7247b3a52e79SStephen M. Cameron 		access = SA5_performant_access_no_read;
7248b3a52e79SStephen M. Cameron 
7249303932fdSDon Brace 	/* Controller spec: zero out this buffer. */
7250072b0518SStephen M. Cameron 	for (i = 0; i < h->nreply_queues; i++)
7251072b0518SStephen M. Cameron 		memset(h->reply_queue[i].head, 0, h->reply_queue_size);
7252303932fdSDon Brace 
7253d66ae08bSStephen M. Cameron 	bft[7] = SG_ENTRIES_IN_CMD + 4;
7254d66ae08bSStephen M. Cameron 	calc_bucket_map(bft, ARRAY_SIZE(bft),
7255e1f7de0cSMatt Gates 				SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
7256303932fdSDon Brace 	for (i = 0; i < 8; i++)
7257303932fdSDon Brace 		writel(bft[i], &h->transtable->BlockFetch[i]);
7258303932fdSDon Brace 
7259303932fdSDon Brace 	/* size of controller ring buffer */
7260303932fdSDon Brace 	writel(h->max_commands, &h->transtable->RepQSize);
7261254f796bSMatt Gates 	writel(h->nreply_queues, &h->transtable->RepQCount);
7262303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrLow32);
7263303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrHigh32);
7264254f796bSMatt Gates 
7265254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
7266254f796bSMatt Gates 		writel(0, &h->transtable->RepQAddr[i].upper);
7267072b0518SStephen M. Cameron 		writel(h->reply_queue[i].busaddr,
7268254f796bSMatt Gates 			&h->transtable->RepQAddr[i].lower);
7269254f796bSMatt Gates 	}
7270254f796bSMatt Gates 
7271b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
7272e1f7de0cSMatt Gates 	writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
7273e1f7de0cSMatt Gates 	/*
7274e1f7de0cSMatt Gates 	 * enable outbound interrupt coalescing in accelerator mode;
7275e1f7de0cSMatt Gates 	 */
7276e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
7277e1f7de0cSMatt Gates 		access = SA5_ioaccel_mode1_access;
7278e1f7de0cSMatt Gates 		writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
7279e1f7de0cSMatt Gates 		writel(4, &h->cfgtable->HostWrite.CoalIntCount);
7280c349775eSScott Teel 	} else {
7281c349775eSScott Teel 		if (trans_support & CFGTBL_Trans_io_accel2) {
7282c349775eSScott Teel 			access = SA5_ioaccel_mode2_access;
7283c349775eSScott Teel 			writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
7284c349775eSScott Teel 			writel(4, &h->cfgtable->HostWrite.CoalIntCount);
7285c349775eSScott Teel 		}
7286e1f7de0cSMatt Gates 	}
7287303932fdSDon Brace 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
7288c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h)) {
7289c706a795SRobert Elliott 		dev_err(&h->pdev->dev,
7290c706a795SRobert Elliott 			"performant mode problem - doorbell timeout\n");
7291c706a795SRobert Elliott 		return -ENODEV;
7292c706a795SRobert Elliott 	}
7293303932fdSDon Brace 	register_value = readl(&(h->cfgtable->TransportActive));
7294303932fdSDon Brace 	if (!(register_value & CFGTBL_Trans_Performant)) {
7295050f7147SStephen Cameron 		dev_err(&h->pdev->dev,
7296050f7147SStephen Cameron 			"performant mode problem - transport not active\n");
7297c706a795SRobert Elliott 		return -ENODEV;
7298303932fdSDon Brace 	}
7299960a30e7SStephen M. Cameron 	/* Change the access methods to the performant access methods */
7300e1f7de0cSMatt Gates 	h->access = access;
7301e1f7de0cSMatt Gates 	h->transMethod = transMethod;
7302e1f7de0cSMatt Gates 
7303b9af4937SStephen M. Cameron 	if (!((trans_support & CFGTBL_Trans_io_accel1) ||
7304b9af4937SStephen M. Cameron 		(trans_support & CFGTBL_Trans_io_accel2)))
7305c706a795SRobert Elliott 		return 0;
7306e1f7de0cSMatt Gates 
7307b9af4937SStephen M. Cameron 	if (trans_support & CFGTBL_Trans_io_accel1) {
7308e1f7de0cSMatt Gates 		/* Set up I/O accelerator mode */
7309e1f7de0cSMatt Gates 		for (i = 0; i < h->nreply_queues; i++) {
7310e1f7de0cSMatt Gates 			writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
7311e1f7de0cSMatt Gates 			h->reply_queue[i].current_entry =
7312e1f7de0cSMatt Gates 				readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
7313e1f7de0cSMatt Gates 		}
7314283b4a9bSStephen M. Cameron 		bft[7] = h->ioaccel_maxsg + 8;
7315283b4a9bSStephen M. Cameron 		calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
7316e1f7de0cSMatt Gates 				h->ioaccel1_blockFetchTable);
7317e1f7de0cSMatt Gates 
7318e1f7de0cSMatt Gates 		/* initialize all reply queue entries to unused */
7319072b0518SStephen M. Cameron 		for (i = 0; i < h->nreply_queues; i++)
7320072b0518SStephen M. Cameron 			memset(h->reply_queue[i].head,
7321072b0518SStephen M. Cameron 				(u8) IOACCEL_MODE1_REPLY_UNUSED,
7322072b0518SStephen M. Cameron 				h->reply_queue_size);
7323e1f7de0cSMatt Gates 
7324e1f7de0cSMatt Gates 		/* set all the constant fields in the accelerator command
7325e1f7de0cSMatt Gates 		 * frames once at init time to save CPU cycles later.
7326e1f7de0cSMatt Gates 		 */
7327e1f7de0cSMatt Gates 		for (i = 0; i < h->nr_cmds; i++) {
7328e1f7de0cSMatt Gates 			struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
7329e1f7de0cSMatt Gates 
7330e1f7de0cSMatt Gates 			cp->function = IOACCEL1_FUNCTION_SCSIIO;
7331e1f7de0cSMatt Gates 			cp->err_info = (u32) (h->errinfo_pool_dhandle +
7332e1f7de0cSMatt Gates 					(i * sizeof(struct ErrorInfo)));
7333e1f7de0cSMatt Gates 			cp->err_info_len = sizeof(struct ErrorInfo);
7334e1f7de0cSMatt Gates 			cp->sgl_offset = IOACCEL1_SGLOFFSET;
73352b08b3e9SDon Brace 			cp->host_context_flags =
73362b08b3e9SDon Brace 				cpu_to_le16(IOACCEL1_HCFLAGS_CISS_FORMAT);
7337e1f7de0cSMatt Gates 			cp->timeout_sec = 0;
7338e1f7de0cSMatt Gates 			cp->ReplyQueue = 0;
733950a0decfSStephen M. Cameron 			cp->tag =
7340f2405db8SDon Brace 				cpu_to_le64((i << DIRECT_LOOKUP_SHIFT));
734150a0decfSStephen M. Cameron 			cp->host_addr =
734250a0decfSStephen M. Cameron 				cpu_to_le64(h->ioaccel_cmd_pool_dhandle +
7343e1f7de0cSMatt Gates 					(i * sizeof(struct io_accel1_cmd)));
7344e1f7de0cSMatt Gates 		}
7345b9af4937SStephen M. Cameron 	} else if (trans_support & CFGTBL_Trans_io_accel2) {
7346b9af4937SStephen M. Cameron 		u64 cfg_offset, cfg_base_addr_index;
7347b9af4937SStephen M. Cameron 		u32 bft2_offset, cfg_base_addr;
7348b9af4937SStephen M. Cameron 		int rc;
7349b9af4937SStephen M. Cameron 
7350b9af4937SStephen M. Cameron 		rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
7351b9af4937SStephen M. Cameron 			&cfg_base_addr_index, &cfg_offset);
7352b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
7353b9af4937SStephen M. Cameron 		bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
7354b9af4937SStephen M. Cameron 		calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
7355b9af4937SStephen M. Cameron 				4, h->ioaccel2_blockFetchTable);
7356b9af4937SStephen M. Cameron 		bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
7357b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct CfgTable,
7358b9af4937SStephen M. Cameron 				io_accel_request_size_offset) != 0xb8);
7359b9af4937SStephen M. Cameron 		h->ioaccel2_bft2_regs =
7360b9af4937SStephen M. Cameron 			remap_pci_mem(pci_resource_start(h->pdev,
7361b9af4937SStephen M. Cameron 					cfg_base_addr_index) +
7362b9af4937SStephen M. Cameron 					cfg_offset + bft2_offset,
7363b9af4937SStephen M. Cameron 					ARRAY_SIZE(bft2) *
7364b9af4937SStephen M. Cameron 					sizeof(*h->ioaccel2_bft2_regs));
7365b9af4937SStephen M. Cameron 		for (i = 0; i < ARRAY_SIZE(bft2); i++)
7366b9af4937SStephen M. Cameron 			writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
7367b9af4937SStephen M. Cameron 	}
7368b9af4937SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
7369c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h)) {
7370c706a795SRobert Elliott 		dev_err(&h->pdev->dev,
7371c706a795SRobert Elliott 			"performant mode problem - enabling ioaccel mode\n");
7372c706a795SRobert Elliott 		return -ENODEV;
7373c706a795SRobert Elliott 	}
7374c706a795SRobert Elliott 	return 0;
7375e1f7de0cSMatt Gates }
7376e1f7de0cSMatt Gates 
7377e1f7de0cSMatt Gates static int hpsa_alloc_ioaccel_cmd_and_bft(struct ctlr_info *h)
7378e1f7de0cSMatt Gates {
7379283b4a9bSStephen M. Cameron 	h->ioaccel_maxsg =
7380283b4a9bSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
7381283b4a9bSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
7382283b4a9bSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
7383283b4a9bSStephen M. Cameron 
7384e1f7de0cSMatt Gates 	/* Command structures must be aligned on a 128-byte boundary
7385e1f7de0cSMatt Gates 	 * because the 7 lower bits of the address are used by the
7386e1f7de0cSMatt Gates 	 * hardware.
7387e1f7de0cSMatt Gates 	 */
7388e1f7de0cSMatt Gates 	BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
7389e1f7de0cSMatt Gates 			IOACCEL1_COMMANDLIST_ALIGNMENT);
7390e1f7de0cSMatt Gates 	h->ioaccel_cmd_pool =
7391e1f7de0cSMatt Gates 		pci_alloc_consistent(h->pdev,
7392e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
7393e1f7de0cSMatt Gates 			&(h->ioaccel_cmd_pool_dhandle));
7394e1f7de0cSMatt Gates 
7395e1f7de0cSMatt Gates 	h->ioaccel1_blockFetchTable =
7396283b4a9bSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
7397e1f7de0cSMatt Gates 				sizeof(u32)), GFP_KERNEL);
7398e1f7de0cSMatt Gates 
7399e1f7de0cSMatt Gates 	if ((h->ioaccel_cmd_pool == NULL) ||
7400e1f7de0cSMatt Gates 		(h->ioaccel1_blockFetchTable == NULL))
7401e1f7de0cSMatt Gates 		goto clean_up;
7402e1f7de0cSMatt Gates 
7403e1f7de0cSMatt Gates 	memset(h->ioaccel_cmd_pool, 0,
7404e1f7de0cSMatt Gates 		h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
7405e1f7de0cSMatt Gates 	return 0;
7406e1f7de0cSMatt Gates 
7407e1f7de0cSMatt Gates clean_up:
7408e1f7de0cSMatt Gates 	if (h->ioaccel_cmd_pool)
7409e1f7de0cSMatt Gates 		pci_free_consistent(h->pdev,
7410e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
7411e1f7de0cSMatt Gates 			h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
7412e1f7de0cSMatt Gates 	kfree(h->ioaccel1_blockFetchTable);
7413e1f7de0cSMatt Gates 	return 1;
74146c311b57SStephen M. Cameron }
74156c311b57SStephen M. Cameron 
7416aca9012aSStephen M. Cameron static int ioaccel2_alloc_cmds_and_bft(struct ctlr_info *h)
7417aca9012aSStephen M. Cameron {
7418aca9012aSStephen M. Cameron 	/* Allocate ioaccel2 mode command blocks and block fetch table */
7419aca9012aSStephen M. Cameron 
7420aca9012aSStephen M. Cameron 	h->ioaccel_maxsg =
7421aca9012aSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
7422aca9012aSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
7423aca9012aSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
7424aca9012aSStephen M. Cameron 
7425aca9012aSStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
7426aca9012aSStephen M. Cameron 			IOACCEL2_COMMANDLIST_ALIGNMENT);
7427aca9012aSStephen M. Cameron 	h->ioaccel2_cmd_pool =
7428aca9012aSStephen M. Cameron 		pci_alloc_consistent(h->pdev,
7429aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
7430aca9012aSStephen M. Cameron 			&(h->ioaccel2_cmd_pool_dhandle));
7431aca9012aSStephen M. Cameron 
7432aca9012aSStephen M. Cameron 	h->ioaccel2_blockFetchTable =
7433aca9012aSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
7434aca9012aSStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
7435aca9012aSStephen M. Cameron 
7436aca9012aSStephen M. Cameron 	if ((h->ioaccel2_cmd_pool == NULL) ||
7437aca9012aSStephen M. Cameron 		(h->ioaccel2_blockFetchTable == NULL))
7438aca9012aSStephen M. Cameron 		goto clean_up;
7439aca9012aSStephen M. Cameron 
7440aca9012aSStephen M. Cameron 	memset(h->ioaccel2_cmd_pool, 0,
7441aca9012aSStephen M. Cameron 		h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
7442aca9012aSStephen M. Cameron 	return 0;
7443aca9012aSStephen M. Cameron 
7444aca9012aSStephen M. Cameron clean_up:
7445aca9012aSStephen M. Cameron 	if (h->ioaccel2_cmd_pool)
7446aca9012aSStephen M. Cameron 		pci_free_consistent(h->pdev,
7447aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
7448aca9012aSStephen M. Cameron 			h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
7449aca9012aSStephen M. Cameron 	kfree(h->ioaccel2_blockFetchTable);
7450aca9012aSStephen M. Cameron 	return 1;
7451aca9012aSStephen M. Cameron }
7452aca9012aSStephen M. Cameron 
74536f039790SGreg Kroah-Hartman static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
74546c311b57SStephen M. Cameron {
74556c311b57SStephen M. Cameron 	u32 trans_support;
7456e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
7457e1f7de0cSMatt Gates 					CFGTBL_Trans_use_short_tags;
7458254f796bSMatt Gates 	int i;
74596c311b57SStephen M. Cameron 
746002ec19c8SStephen M. Cameron 	if (hpsa_simple_mode)
746102ec19c8SStephen M. Cameron 		return;
746202ec19c8SStephen M. Cameron 
746367c99a72Sscameron@beardog.cce.hp.com 	trans_support = readl(&(h->cfgtable->TransportSupport));
746467c99a72Sscameron@beardog.cce.hp.com 	if (!(trans_support & PERFORMANT_MODE))
746567c99a72Sscameron@beardog.cce.hp.com 		return;
746667c99a72Sscameron@beardog.cce.hp.com 
7467e1f7de0cSMatt Gates 	/* Check for I/O accelerator mode support */
7468e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
7469e1f7de0cSMatt Gates 		transMethod |= CFGTBL_Trans_io_accel1 |
7470e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix;
7471e1f7de0cSMatt Gates 		if (hpsa_alloc_ioaccel_cmd_and_bft(h))
7472e1f7de0cSMatt Gates 			goto clean_up;
7473aca9012aSStephen M. Cameron 	} else {
7474aca9012aSStephen M. Cameron 		if (trans_support & CFGTBL_Trans_io_accel2) {
7475aca9012aSStephen M. Cameron 				transMethod |= CFGTBL_Trans_io_accel2 |
7476aca9012aSStephen M. Cameron 				CFGTBL_Trans_enable_directed_msix;
7477aca9012aSStephen M. Cameron 		if (ioaccel2_alloc_cmds_and_bft(h))
7478aca9012aSStephen M. Cameron 			goto clean_up;
7479aca9012aSStephen M. Cameron 		}
7480e1f7de0cSMatt Gates 	}
7481e1f7de0cSMatt Gates 
7482eee0f03aSHannes Reinecke 	h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
7483cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
74846c311b57SStephen M. Cameron 	/* Performant mode ring buffer and supporting data structures */
7485072b0518SStephen M. Cameron 	h->reply_queue_size = h->max_commands * sizeof(u64);
74866c311b57SStephen M. Cameron 
7487254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
7488072b0518SStephen M. Cameron 		h->reply_queue[i].head = pci_alloc_consistent(h->pdev,
7489072b0518SStephen M. Cameron 						h->reply_queue_size,
7490072b0518SStephen M. Cameron 						&(h->reply_queue[i].busaddr));
7491072b0518SStephen M. Cameron 		if (!h->reply_queue[i].head)
7492072b0518SStephen M. Cameron 			goto clean_up;
7493254f796bSMatt Gates 		h->reply_queue[i].size = h->max_commands;
7494254f796bSMatt Gates 		h->reply_queue[i].wraparound = 1;  /* spec: init to 1 */
7495254f796bSMatt Gates 		h->reply_queue[i].current_entry = 0;
7496254f796bSMatt Gates 	}
7497254f796bSMatt Gates 
74986c311b57SStephen M. Cameron 	/* Need a block fetch table for performant mode */
7499d66ae08bSStephen M. Cameron 	h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
75006c311b57SStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
7501072b0518SStephen M. Cameron 	if (!h->blockFetchTable)
75026c311b57SStephen M. Cameron 		goto clean_up;
75036c311b57SStephen M. Cameron 
7504e1f7de0cSMatt Gates 	hpsa_enter_performant_mode(h, trans_support);
7505303932fdSDon Brace 	return;
7506303932fdSDon Brace 
7507303932fdSDon Brace clean_up:
7508072b0518SStephen M. Cameron 	hpsa_free_reply_queues(h);
7509303932fdSDon Brace 	kfree(h->blockFetchTable);
7510303932fdSDon Brace }
7511303932fdSDon Brace 
751223100dd9SStephen M. Cameron static int is_accelerated_cmd(struct CommandList *c)
751376438d08SStephen M. Cameron {
751423100dd9SStephen M. Cameron 	return c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_IOACCEL2;
751523100dd9SStephen M. Cameron }
751623100dd9SStephen M. Cameron 
751723100dd9SStephen M. Cameron static void hpsa_drain_accel_commands(struct ctlr_info *h)
751823100dd9SStephen M. Cameron {
751923100dd9SStephen M. Cameron 	struct CommandList *c = NULL;
7520f2405db8SDon Brace 	int i, accel_cmds_out;
7521281a7fd0SWebb Scales 	int refcount;
752276438d08SStephen M. Cameron 
7523f2405db8SDon Brace 	do { /* wait for all outstanding ioaccel commands to drain out */
752423100dd9SStephen M. Cameron 		accel_cmds_out = 0;
7525f2405db8SDon Brace 		for (i = 0; i < h->nr_cmds; i++) {
7526f2405db8SDon Brace 			c = h->cmd_pool + i;
7527281a7fd0SWebb Scales 			refcount = atomic_inc_return(&c->refcount);
7528281a7fd0SWebb Scales 			if (refcount > 1) /* Command is allocated */
752923100dd9SStephen M. Cameron 				accel_cmds_out += is_accelerated_cmd(c);
7530281a7fd0SWebb Scales 			cmd_free(h, c);
7531f2405db8SDon Brace 		}
753223100dd9SStephen M. Cameron 		if (accel_cmds_out <= 0)
753376438d08SStephen M. Cameron 			break;
753476438d08SStephen M. Cameron 		msleep(100);
753576438d08SStephen M. Cameron 	} while (1);
753676438d08SStephen M. Cameron }
753776438d08SStephen M. Cameron 
7538edd16368SStephen M. Cameron /*
7539edd16368SStephen M. Cameron  *  This is it.  Register the PCI driver information for the cards we control
7540edd16368SStephen M. Cameron  *  the OS will call our registered routines when it finds one of our cards.
7541edd16368SStephen M. Cameron  */
7542edd16368SStephen M. Cameron static int __init hpsa_init(void)
7543edd16368SStephen M. Cameron {
754431468401SMike Miller 	return pci_register_driver(&hpsa_pci_driver);
7545edd16368SStephen M. Cameron }
7546edd16368SStephen M. Cameron 
7547edd16368SStephen M. Cameron static void __exit hpsa_cleanup(void)
7548edd16368SStephen M. Cameron {
7549edd16368SStephen M. Cameron 	pci_unregister_driver(&hpsa_pci_driver);
7550edd16368SStephen M. Cameron }
7551edd16368SStephen M. Cameron 
7552e1f7de0cSMatt Gates static void __attribute__((unused)) verify_offsets(void)
7553e1f7de0cSMatt Gates {
7554e1f7de0cSMatt Gates #define VERIFY_OFFSET(member, offset) \
7555dd0e19f3SScott Teel 	BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
7556dd0e19f3SScott Teel 
7557dd0e19f3SScott Teel 	VERIFY_OFFSET(structure_size, 0);
7558dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_size, 4);
7559dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_cnt, 8);
7560dd0e19f3SScott Teel 	VERIFY_OFFSET(phys_blk_shift, 16);
7561dd0e19f3SScott Teel 	VERIFY_OFFSET(parity_rotation_shift, 17);
7562dd0e19f3SScott Teel 	VERIFY_OFFSET(strip_size, 18);
7563dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_starting_blk, 20);
7564dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_blk_cnt, 28);
7565dd0e19f3SScott Teel 	VERIFY_OFFSET(data_disks_per_row, 36);
7566dd0e19f3SScott Teel 	VERIFY_OFFSET(metadata_disks_per_row, 38);
7567dd0e19f3SScott Teel 	VERIFY_OFFSET(row_cnt, 40);
7568dd0e19f3SScott Teel 	VERIFY_OFFSET(layout_map_count, 42);
7569dd0e19f3SScott Teel 	VERIFY_OFFSET(flags, 44);
7570dd0e19f3SScott Teel 	VERIFY_OFFSET(dekindex, 46);
7571dd0e19f3SScott Teel 	/* VERIFY_OFFSET(reserved, 48 */
7572dd0e19f3SScott Teel 	VERIFY_OFFSET(data, 64);
7573dd0e19f3SScott Teel 
7574dd0e19f3SScott Teel #undef VERIFY_OFFSET
7575dd0e19f3SScott Teel 
7576dd0e19f3SScott Teel #define VERIFY_OFFSET(member, offset) \
7577b66cc250SMike Miller 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
7578b66cc250SMike Miller 
7579b66cc250SMike Miller 	VERIFY_OFFSET(IU_type, 0);
7580b66cc250SMike Miller 	VERIFY_OFFSET(direction, 1);
7581b66cc250SMike Miller 	VERIFY_OFFSET(reply_queue, 2);
7582b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved1, 3);  */
7583b66cc250SMike Miller 	VERIFY_OFFSET(scsi_nexus, 4);
7584b66cc250SMike Miller 	VERIFY_OFFSET(Tag, 8);
7585b66cc250SMike Miller 	VERIFY_OFFSET(cdb, 16);
7586b66cc250SMike Miller 	VERIFY_OFFSET(cciss_lun, 32);
7587b66cc250SMike Miller 	VERIFY_OFFSET(data_len, 40);
7588b66cc250SMike Miller 	VERIFY_OFFSET(cmd_priority_task_attr, 44);
7589b66cc250SMike Miller 	VERIFY_OFFSET(sg_count, 45);
7590b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved3 */
7591b66cc250SMike Miller 	VERIFY_OFFSET(err_ptr, 48);
7592b66cc250SMike Miller 	VERIFY_OFFSET(err_len, 56);
7593b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved4  */
7594b66cc250SMike Miller 	VERIFY_OFFSET(sg, 64);
7595b66cc250SMike Miller 
7596b66cc250SMike Miller #undef VERIFY_OFFSET
7597b66cc250SMike Miller 
7598b66cc250SMike Miller #define VERIFY_OFFSET(member, offset) \
7599e1f7de0cSMatt Gates 	BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
7600e1f7de0cSMatt Gates 
7601e1f7de0cSMatt Gates 	VERIFY_OFFSET(dev_handle, 0x00);
7602e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved1, 0x02);
7603e1f7de0cSMatt Gates 	VERIFY_OFFSET(function, 0x03);
7604e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved2, 0x04);
7605e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info, 0x0C);
7606e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved3, 0x10);
7607e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info_len, 0x12);
7608e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved4, 0x13);
7609e1f7de0cSMatt Gates 	VERIFY_OFFSET(sgl_offset, 0x14);
7610e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved5, 0x15);
7611e1f7de0cSMatt Gates 	VERIFY_OFFSET(transfer_len, 0x1C);
7612e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved6, 0x20);
7613e1f7de0cSMatt Gates 	VERIFY_OFFSET(io_flags, 0x24);
7614e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved7, 0x26);
7615e1f7de0cSMatt Gates 	VERIFY_OFFSET(LUN, 0x34);
7616e1f7de0cSMatt Gates 	VERIFY_OFFSET(control, 0x3C);
7617e1f7de0cSMatt Gates 	VERIFY_OFFSET(CDB, 0x40);
7618e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved8, 0x50);
7619e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_context_flags, 0x60);
7620e1f7de0cSMatt Gates 	VERIFY_OFFSET(timeout_sec, 0x62);
7621e1f7de0cSMatt Gates 	VERIFY_OFFSET(ReplyQueue, 0x64);
7622e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved9, 0x65);
762350a0decfSStephen M. Cameron 	VERIFY_OFFSET(tag, 0x68);
7624e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_addr, 0x70);
7625e1f7de0cSMatt Gates 	VERIFY_OFFSET(CISS_LUN, 0x78);
7626e1f7de0cSMatt Gates 	VERIFY_OFFSET(SG, 0x78 + 8);
7627e1f7de0cSMatt Gates #undef VERIFY_OFFSET
7628e1f7de0cSMatt Gates }
7629e1f7de0cSMatt Gates 
7630edd16368SStephen M. Cameron module_init(hpsa_init);
7631edd16368SStephen M. Cameron module_exit(hpsa_cleanup);
7632