xref: /openbmc/linux/drivers/scsi/hpsa.c (revision ec501a18637d04a7ee78a1754817f57125552fd6)
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 
63edd16368SStephen M. Cameron /* How long to wait (in milliseconds) for board to go into simple mode */
64edd16368SStephen M. Cameron #define MAX_CONFIG_WAIT 30000
65edd16368SStephen M. Cameron #define MAX_IOCTL_CONFIG_WAIT 1000
66edd16368SStephen M. Cameron 
67edd16368SStephen M. Cameron /*define how many times we will try a command because of bus resets */
68edd16368SStephen M. Cameron #define MAX_CMD_RETRIES 3
69edd16368SStephen M. Cameron 
70edd16368SStephen M. Cameron /* Embedded module documentation macros - see modules.h */
71edd16368SStephen M. Cameron MODULE_AUTHOR("Hewlett-Packard Company");
72edd16368SStephen M. Cameron MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
73edd16368SStephen M. Cameron 	HPSA_DRIVER_VERSION);
74edd16368SStephen M. Cameron MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
75edd16368SStephen M. Cameron MODULE_VERSION(HPSA_DRIVER_VERSION);
76edd16368SStephen M. Cameron MODULE_LICENSE("GPL");
77edd16368SStephen M. Cameron 
78edd16368SStephen M. Cameron static int hpsa_allow_any;
79edd16368SStephen M. Cameron module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
80edd16368SStephen M. Cameron MODULE_PARM_DESC(hpsa_allow_any,
81edd16368SStephen M. Cameron 		"Allow hpsa driver to access unknown HP Smart Array hardware");
8202ec19c8SStephen M. Cameron static int hpsa_simple_mode;
8302ec19c8SStephen M. Cameron module_param(hpsa_simple_mode, int, S_IRUGO|S_IWUSR);
8402ec19c8SStephen M. Cameron MODULE_PARM_DESC(hpsa_simple_mode,
8502ec19c8SStephen M. Cameron 	"Use 'simple mode' rather than 'performant mode'");
86edd16368SStephen M. Cameron 
87edd16368SStephen M. Cameron /* define the PCI info for the cards we can control */
88edd16368SStephen M. Cameron static const struct pci_device_id hpsa_pci_device_id[] = {
89edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3241},
90edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3243},
91edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3245},
92edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3247},
93edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3249},
94163dbcd8SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x324A},
95163dbcd8SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x324B},
96f8b01eb9SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3233},
979143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3350},
989143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3351},
999143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3352},
1009143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3353},
1019143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3354},
1029143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3355},
1039143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3356},
104fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1921},
105fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1922},
106fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1923},
107fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1924},
108fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1926},
109fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1928},
11097b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1929},
11197b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BD},
11297b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BE},
11397b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BF},
11497b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C0},
11597b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C1},
11697b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C2},
11797b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C3},
11897b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C4},
11997b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C5},
1203b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C6},
12197b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C7},
12297b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C8},
12397b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C9},
1243b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CA},
1253b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CB},
1263b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CC},
1273b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CD},
1283b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CE},
1298e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0076},
1308e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0087},
1318e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x007D},
1328e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0088},
1338e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP, 0x333f, 0x103c, 0x333f},
134edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_ANY_ID,	PCI_ANY_ID, PCI_ANY_ID,
135edd16368SStephen M. Cameron 		PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
136edd16368SStephen M. Cameron 	{0,}
137edd16368SStephen M. Cameron };
138edd16368SStephen M. Cameron 
139edd16368SStephen M. Cameron MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
140edd16368SStephen M. Cameron 
141edd16368SStephen M. Cameron /*  board_id = Subsystem Device ID & Vendor ID
142edd16368SStephen M. Cameron  *  product = Marketing Name for the board
143edd16368SStephen M. Cameron  *  access = Address of the struct of function pointers
144edd16368SStephen M. Cameron  */
145edd16368SStephen M. Cameron static struct board_type products[] = {
146edd16368SStephen M. Cameron 	{0x3241103C, "Smart Array P212", &SA5_access},
147edd16368SStephen M. Cameron 	{0x3243103C, "Smart Array P410", &SA5_access},
148edd16368SStephen M. Cameron 	{0x3245103C, "Smart Array P410i", &SA5_access},
149edd16368SStephen M. Cameron 	{0x3247103C, "Smart Array P411", &SA5_access},
150edd16368SStephen M. Cameron 	{0x3249103C, "Smart Array P812", &SA5_access},
151163dbcd8SMike Miller 	{0x324A103C, "Smart Array P712m", &SA5_access},
152163dbcd8SMike Miller 	{0x324B103C, "Smart Array P711m", &SA5_access},
1537d2cce58SStephen M. Cameron 	{0x3233103C, "HP StorageWorks 1210m", &SA5_access}, /* alias of 333f */
154fe0c9610SMike Miller 	{0x3350103C, "Smart Array P222", &SA5_access},
155fe0c9610SMike Miller 	{0x3351103C, "Smart Array P420", &SA5_access},
156fe0c9610SMike Miller 	{0x3352103C, "Smart Array P421", &SA5_access},
157fe0c9610SMike Miller 	{0x3353103C, "Smart Array P822", &SA5_access},
158fe0c9610SMike Miller 	{0x3354103C, "Smart Array P420i", &SA5_access},
159fe0c9610SMike Miller 	{0x3355103C, "Smart Array P220i", &SA5_access},
160fe0c9610SMike Miller 	{0x3356103C, "Smart Array P721m", &SA5_access},
1611fd6c8e3SMike Miller 	{0x1921103C, "Smart Array P830i", &SA5_access},
1621fd6c8e3SMike Miller 	{0x1922103C, "Smart Array P430", &SA5_access},
1631fd6c8e3SMike Miller 	{0x1923103C, "Smart Array P431", &SA5_access},
1641fd6c8e3SMike Miller 	{0x1924103C, "Smart Array P830", &SA5_access},
1651fd6c8e3SMike Miller 	{0x1926103C, "Smart Array P731m", &SA5_access},
1661fd6c8e3SMike Miller 	{0x1928103C, "Smart Array P230i", &SA5_access},
1671fd6c8e3SMike Miller 	{0x1929103C, "Smart Array P530", &SA5_access},
16897b9f53dSMike Miller 	{0x21BD103C, "Smart Array", &SA5_access},
16997b9f53dSMike Miller 	{0x21BE103C, "Smart Array", &SA5_access},
17097b9f53dSMike Miller 	{0x21BF103C, "Smart Array", &SA5_access},
17197b9f53dSMike Miller 	{0x21C0103C, "Smart Array", &SA5_access},
17297b9f53dSMike Miller 	{0x21C1103C, "Smart Array", &SA5_access},
17397b9f53dSMike Miller 	{0x21C2103C, "Smart Array", &SA5_access},
17497b9f53dSMike Miller 	{0x21C3103C, "Smart Array", &SA5_access},
17597b9f53dSMike Miller 	{0x21C4103C, "Smart Array", &SA5_access},
17697b9f53dSMike Miller 	{0x21C5103C, "Smart Array", &SA5_access},
1773b7a45e5SJoe Handzik 	{0x21C6103C, "Smart Array", &SA5_access},
17897b9f53dSMike Miller 	{0x21C7103C, "Smart Array", &SA5_access},
17997b9f53dSMike Miller 	{0x21C8103C, "Smart Array", &SA5_access},
18097b9f53dSMike Miller 	{0x21C9103C, "Smart Array", &SA5_access},
1813b7a45e5SJoe Handzik 	{0x21CA103C, "Smart Array", &SA5_access},
1823b7a45e5SJoe Handzik 	{0x21CB103C, "Smart Array", &SA5_access},
1833b7a45e5SJoe Handzik 	{0x21CC103C, "Smart Array", &SA5_access},
1843b7a45e5SJoe Handzik 	{0x21CD103C, "Smart Array", &SA5_access},
1853b7a45e5SJoe Handzik 	{0x21CE103C, "Smart Array", &SA5_access},
1868e616a5eSStephen M. Cameron 	{0x00761590, "HP Storage P1224 Array Controller", &SA5_access},
1878e616a5eSStephen M. Cameron 	{0x00871590, "HP Storage P1224e Array Controller", &SA5_access},
1888e616a5eSStephen M. Cameron 	{0x007D1590, "HP Storage P1228 Array Controller", &SA5_access},
1898e616a5eSStephen M. Cameron 	{0x00881590, "HP Storage P1228e Array Controller", &SA5_access},
1908e616a5eSStephen M. Cameron 	{0x333f103c, "HP StorageWorks 1210m Array Controller", &SA5_access},
191edd16368SStephen M. Cameron 	{0xFFFF103C, "Unknown Smart Array", &SA5_access},
192edd16368SStephen M. Cameron };
193edd16368SStephen M. Cameron 
194edd16368SStephen M. Cameron static int number_of_controllers;
195edd16368SStephen M. Cameron 
19610f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id);
19710f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id);
19842a91641SDon Brace static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg);
1990b57075dSStephen M. Cameron static void lock_and_start_io(struct ctlr_info *h);
2000b57075dSStephen M. Cameron static void start_io(struct ctlr_info *h, unsigned long *flags);
201edd16368SStephen M. Cameron 
202edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
20342a91641SDon Brace static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd,
20442a91641SDon Brace 	void __user *arg);
205edd16368SStephen M. Cameron #endif
206edd16368SStephen M. Cameron 
207edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c);
208edd16368SStephen M. Cameron static void cmd_special_free(struct ctlr_info *h, struct CommandList *c);
209edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h);
210edd16368SStephen M. Cameron static struct CommandList *cmd_special_alloc(struct ctlr_info *h);
211a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
212b7bb24ebSStephen M. Cameron 	void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
213edd16368SStephen M. Cameron 	int cmd_type);
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);
246283b4a9bSStephen M. Cameron static void 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,
253c349775eSScott Teel 	u8 *scsi3addr);
254edd16368SStephen M. Cameron 
255edd16368SStephen M. Cameron static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
256edd16368SStephen M. Cameron {
257edd16368SStephen M. Cameron 	unsigned long *priv = shost_priv(sdev->host);
258edd16368SStephen M. Cameron 	return (struct ctlr_info *) *priv;
259edd16368SStephen M. Cameron }
260edd16368SStephen M. Cameron 
261a23513e8SStephen M. Cameron static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
262a23513e8SStephen M. Cameron {
263a23513e8SStephen M. Cameron 	unsigned long *priv = shost_priv(sh);
264a23513e8SStephen M. Cameron 	return (struct ctlr_info *) *priv;
265a23513e8SStephen M. Cameron }
266a23513e8SStephen M. Cameron 
267edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
268edd16368SStephen M. Cameron 	struct CommandList *c)
269edd16368SStephen M. Cameron {
270edd16368SStephen M. Cameron 	if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
271edd16368SStephen M. Cameron 		return 0;
272edd16368SStephen M. Cameron 
273edd16368SStephen M. Cameron 	switch (c->err_info->SenseInfo[12]) {
274edd16368SStephen M. Cameron 	case STATE_CHANGED:
275f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: a state change "
276edd16368SStephen M. Cameron 			"detected, command retried\n", h->ctlr);
277edd16368SStephen M. Cameron 		break;
278edd16368SStephen M. Cameron 	case LUN_FAILED:
2797f73695aSStephen M. Cameron 		dev_warn(&h->pdev->dev,
2807f73695aSStephen M. Cameron 			HPSA "%d: LUN failure detected\n", h->ctlr);
281edd16368SStephen M. Cameron 		break;
282edd16368SStephen M. Cameron 	case REPORT_LUNS_CHANGED:
2837f73695aSStephen M. Cameron 		dev_warn(&h->pdev->dev,
2847f73695aSStephen M. Cameron 			HPSA "%d: report LUN data changed\n", h->ctlr);
285edd16368SStephen M. Cameron 	/*
2864f4eb9f1SScott Teel 	 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
2874f4eb9f1SScott Teel 	 * target (array) devices.
288edd16368SStephen M. Cameron 	 */
289edd16368SStephen M. Cameron 		break;
290edd16368SStephen M. Cameron 	case POWER_OR_RESET:
291f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: a power on "
292edd16368SStephen M. Cameron 			"or device reset detected\n", h->ctlr);
293edd16368SStephen M. Cameron 		break;
294edd16368SStephen M. Cameron 	case UNIT_ATTENTION_CLEARED:
295f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: unit attention "
296edd16368SStephen M. Cameron 		    "cleared by another initiator\n", h->ctlr);
297edd16368SStephen M. Cameron 		break;
298edd16368SStephen M. Cameron 	default:
299f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: unknown "
300edd16368SStephen M. Cameron 			"unit attention detected\n", h->ctlr);
301edd16368SStephen M. Cameron 		break;
302edd16368SStephen M. Cameron 	}
303edd16368SStephen M. Cameron 	return 1;
304edd16368SStephen M. Cameron }
305edd16368SStephen M. Cameron 
306852af20aSMatt Bondurant static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
307852af20aSMatt Bondurant {
308852af20aSMatt Bondurant 	if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
309852af20aSMatt Bondurant 		(c->err_info->ScsiStatus != SAM_STAT_BUSY &&
310852af20aSMatt Bondurant 		 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
311852af20aSMatt Bondurant 		return 0;
312852af20aSMatt Bondurant 	dev_warn(&h->pdev->dev, HPSA "device busy");
313852af20aSMatt Bondurant 	return 1;
314852af20aSMatt Bondurant }
315852af20aSMatt Bondurant 
316da0697bdSScott Teel static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
317da0697bdSScott Teel 					 struct device_attribute *attr,
318da0697bdSScott Teel 					 const char *buf, size_t count)
319da0697bdSScott Teel {
320da0697bdSScott Teel 	int status, len;
321da0697bdSScott Teel 	struct ctlr_info *h;
322da0697bdSScott Teel 	struct Scsi_Host *shost = class_to_shost(dev);
323da0697bdSScott Teel 	char tmpbuf[10];
324da0697bdSScott Teel 
325da0697bdSScott Teel 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
326da0697bdSScott Teel 		return -EACCES;
327da0697bdSScott Teel 	len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
328da0697bdSScott Teel 	strncpy(tmpbuf, buf, len);
329da0697bdSScott Teel 	tmpbuf[len] = '\0';
330da0697bdSScott Teel 	if (sscanf(tmpbuf, "%d", &status) != 1)
331da0697bdSScott Teel 		return -EINVAL;
332da0697bdSScott Teel 	h = shost_to_hba(shost);
333da0697bdSScott Teel 	h->acciopath_status = !!status;
334da0697bdSScott Teel 	dev_warn(&h->pdev->dev,
335da0697bdSScott Teel 		"hpsa: HP SSD Smart Path %s via sysfs update.\n",
336da0697bdSScott Teel 		h->acciopath_status ? "enabled" : "disabled");
337da0697bdSScott Teel 	return count;
338da0697bdSScott Teel }
339da0697bdSScott Teel 
3402ba8bfc8SStephen M. Cameron static ssize_t host_store_raid_offload_debug(struct device *dev,
3412ba8bfc8SStephen M. Cameron 					 struct device_attribute *attr,
3422ba8bfc8SStephen M. Cameron 					 const char *buf, size_t count)
3432ba8bfc8SStephen M. Cameron {
3442ba8bfc8SStephen M. Cameron 	int debug_level, len;
3452ba8bfc8SStephen M. Cameron 	struct ctlr_info *h;
3462ba8bfc8SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
3472ba8bfc8SStephen M. Cameron 	char tmpbuf[10];
3482ba8bfc8SStephen M. Cameron 
3492ba8bfc8SStephen M. Cameron 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
3502ba8bfc8SStephen M. Cameron 		return -EACCES;
3512ba8bfc8SStephen M. Cameron 	len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
3522ba8bfc8SStephen M. Cameron 	strncpy(tmpbuf, buf, len);
3532ba8bfc8SStephen M. Cameron 	tmpbuf[len] = '\0';
3542ba8bfc8SStephen M. Cameron 	if (sscanf(tmpbuf, "%d", &debug_level) != 1)
3552ba8bfc8SStephen M. Cameron 		return -EINVAL;
3562ba8bfc8SStephen M. Cameron 	if (debug_level < 0)
3572ba8bfc8SStephen M. Cameron 		debug_level = 0;
3582ba8bfc8SStephen M. Cameron 	h = shost_to_hba(shost);
3592ba8bfc8SStephen M. Cameron 	h->raid_offload_debug = debug_level;
3602ba8bfc8SStephen M. Cameron 	dev_warn(&h->pdev->dev, "hpsa: Set raid_offload_debug level = %d\n",
3612ba8bfc8SStephen M. Cameron 		h->raid_offload_debug);
3622ba8bfc8SStephen M. Cameron 	return count;
3632ba8bfc8SStephen M. Cameron }
3642ba8bfc8SStephen M. Cameron 
365edd16368SStephen M. Cameron static ssize_t host_store_rescan(struct device *dev,
366edd16368SStephen M. Cameron 				 struct device_attribute *attr,
367edd16368SStephen M. Cameron 				 const char *buf, size_t count)
368edd16368SStephen M. Cameron {
369edd16368SStephen M. Cameron 	struct ctlr_info *h;
370edd16368SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
371a23513e8SStephen M. Cameron 	h = shost_to_hba(shost);
37231468401SMike Miller 	hpsa_scan_start(h->scsi_host);
373edd16368SStephen M. Cameron 	return count;
374edd16368SStephen M. Cameron }
375edd16368SStephen M. Cameron 
376d28ce020SStephen M. Cameron static ssize_t host_show_firmware_revision(struct device *dev,
377d28ce020SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
378d28ce020SStephen M. Cameron {
379d28ce020SStephen M. Cameron 	struct ctlr_info *h;
380d28ce020SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
381d28ce020SStephen M. Cameron 	unsigned char *fwrev;
382d28ce020SStephen M. Cameron 
383d28ce020SStephen M. Cameron 	h = shost_to_hba(shost);
384d28ce020SStephen M. Cameron 	if (!h->hba_inquiry_data)
385d28ce020SStephen M. Cameron 		return 0;
386d28ce020SStephen M. Cameron 	fwrev = &h->hba_inquiry_data[32];
387d28ce020SStephen M. Cameron 	return snprintf(buf, 20, "%c%c%c%c\n",
388d28ce020SStephen M. Cameron 		fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
389d28ce020SStephen M. Cameron }
390d28ce020SStephen M. Cameron 
39194a13649SStephen M. Cameron static ssize_t host_show_commands_outstanding(struct device *dev,
39294a13649SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
39394a13649SStephen M. Cameron {
39494a13649SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
39594a13649SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(shost);
39694a13649SStephen M. Cameron 
3970cbf768eSStephen M. Cameron 	return snprintf(buf, 20, "%d\n",
3980cbf768eSStephen M. Cameron 			atomic_read(&h->commands_outstanding));
39994a13649SStephen M. Cameron }
40094a13649SStephen M. Cameron 
401745a7a25SStephen M. Cameron static ssize_t host_show_transport_mode(struct device *dev,
402745a7a25SStephen M. Cameron 	struct device_attribute *attr, char *buf)
403745a7a25SStephen M. Cameron {
404745a7a25SStephen M. Cameron 	struct ctlr_info *h;
405745a7a25SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
406745a7a25SStephen M. Cameron 
407745a7a25SStephen M. Cameron 	h = shost_to_hba(shost);
408745a7a25SStephen M. Cameron 	return snprintf(buf, 20, "%s\n",
409960a30e7SStephen M. Cameron 		h->transMethod & CFGTBL_Trans_Performant ?
410745a7a25SStephen M. Cameron 			"performant" : "simple");
411745a7a25SStephen M. Cameron }
412745a7a25SStephen M. Cameron 
413da0697bdSScott Teel static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
414da0697bdSScott Teel 	struct device_attribute *attr, char *buf)
415da0697bdSScott Teel {
416da0697bdSScott Teel 	struct ctlr_info *h;
417da0697bdSScott Teel 	struct Scsi_Host *shost = class_to_shost(dev);
418da0697bdSScott Teel 
419da0697bdSScott Teel 	h = shost_to_hba(shost);
420da0697bdSScott Teel 	return snprintf(buf, 30, "HP SSD Smart Path %s\n",
421da0697bdSScott Teel 		(h->acciopath_status == 1) ?  "enabled" : "disabled");
422da0697bdSScott Teel }
423da0697bdSScott Teel 
42446380786SStephen M. Cameron /* List of controllers which cannot be hard reset on kexec with reset_devices */
425941b1cdaSStephen M. Cameron static u32 unresettable_controller[] = {
426941b1cdaSStephen M. Cameron 	0x324a103C, /* Smart Array P712m */
427941b1cdaSStephen M. Cameron 	0x324b103C, /* SmartArray P711m */
428941b1cdaSStephen M. Cameron 	0x3223103C, /* Smart Array P800 */
429941b1cdaSStephen M. Cameron 	0x3234103C, /* Smart Array P400 */
430941b1cdaSStephen M. Cameron 	0x3235103C, /* Smart Array P400i */
431941b1cdaSStephen M. Cameron 	0x3211103C, /* Smart Array E200i */
432941b1cdaSStephen M. Cameron 	0x3212103C, /* Smart Array E200 */
433941b1cdaSStephen M. Cameron 	0x3213103C, /* Smart Array E200i */
434941b1cdaSStephen M. Cameron 	0x3214103C, /* Smart Array E200i */
435941b1cdaSStephen M. Cameron 	0x3215103C, /* Smart Array E200i */
436941b1cdaSStephen M. Cameron 	0x3237103C, /* Smart Array E500 */
437941b1cdaSStephen M. Cameron 	0x323D103C, /* Smart Array P700m */
4387af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
439941b1cdaSStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
440941b1cdaSStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
4415a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
4425a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
4435a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
4445a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
4455a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
4465a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
447941b1cdaSStephen M. Cameron };
448941b1cdaSStephen M. Cameron 
44946380786SStephen M. Cameron /* List of controllers which cannot even be soft reset */
45046380786SStephen M. Cameron static u32 soft_unresettable_controller[] = {
4517af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
4525a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
4535a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
4545a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
4555a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
4565a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
4575a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
45846380786SStephen M. Cameron 	/* Exclude 640x boards.  These are two pci devices in one slot
45946380786SStephen M. Cameron 	 * which share a battery backed cache module.  One controls the
46046380786SStephen M. Cameron 	 * cache, the other accesses the cache through the one that controls
46146380786SStephen M. Cameron 	 * it.  If we reset the one controlling the cache, the other will
46246380786SStephen M. Cameron 	 * likely not be happy.  Just forbid resetting this conjoined mess.
46346380786SStephen M. Cameron 	 * The 640x isn't really supported by hpsa anyway.
46446380786SStephen M. Cameron 	 */
46546380786SStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
46646380786SStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
46746380786SStephen M. Cameron };
46846380786SStephen M. Cameron 
46946380786SStephen M. Cameron static int ctlr_is_hard_resettable(u32 board_id)
470941b1cdaSStephen M. Cameron {
471941b1cdaSStephen M. Cameron 	int i;
472941b1cdaSStephen M. Cameron 
473941b1cdaSStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(unresettable_controller); i++)
47446380786SStephen M. Cameron 		if (unresettable_controller[i] == board_id)
475941b1cdaSStephen M. Cameron 			return 0;
476941b1cdaSStephen M. Cameron 	return 1;
477941b1cdaSStephen M. Cameron }
478941b1cdaSStephen M. Cameron 
47946380786SStephen M. Cameron static int ctlr_is_soft_resettable(u32 board_id)
48046380786SStephen M. Cameron {
48146380786SStephen M. Cameron 	int i;
48246380786SStephen M. Cameron 
48346380786SStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(soft_unresettable_controller); i++)
48446380786SStephen M. Cameron 		if (soft_unresettable_controller[i] == board_id)
48546380786SStephen M. Cameron 			return 0;
48646380786SStephen M. Cameron 	return 1;
48746380786SStephen M. Cameron }
48846380786SStephen M. Cameron 
48946380786SStephen M. Cameron static int ctlr_is_resettable(u32 board_id)
49046380786SStephen M. Cameron {
49146380786SStephen M. Cameron 	return ctlr_is_hard_resettable(board_id) ||
49246380786SStephen M. Cameron 		ctlr_is_soft_resettable(board_id);
49346380786SStephen M. Cameron }
49446380786SStephen M. Cameron 
495941b1cdaSStephen M. Cameron static ssize_t host_show_resettable(struct device *dev,
496941b1cdaSStephen M. Cameron 	struct device_attribute *attr, char *buf)
497941b1cdaSStephen M. Cameron {
498941b1cdaSStephen M. Cameron 	struct ctlr_info *h;
499941b1cdaSStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
500941b1cdaSStephen M. Cameron 
501941b1cdaSStephen M. Cameron 	h = shost_to_hba(shost);
50246380786SStephen M. Cameron 	return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
503941b1cdaSStephen M. Cameron }
504941b1cdaSStephen M. Cameron 
505edd16368SStephen M. Cameron static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
506edd16368SStephen M. Cameron {
507edd16368SStephen M. Cameron 	return (scsi3addr[3] & 0xC0) == 0x40;
508edd16368SStephen M. Cameron }
509edd16368SStephen M. Cameron 
510f2ef0ce7SRobert Elliott static const char * const raid_label[] = { "0", "4", "1(+0)", "5", "5+1", "6",
511f2ef0ce7SRobert Elliott 	"1(+0)ADM", "UNKNOWN"
512edd16368SStephen M. Cameron };
5136b80b18fSScott Teel #define HPSA_RAID_0	0
5146b80b18fSScott Teel #define HPSA_RAID_4	1
5156b80b18fSScott Teel #define HPSA_RAID_1	2	/* also used for RAID 10 */
5166b80b18fSScott Teel #define HPSA_RAID_5	3	/* also used for RAID 50 */
5176b80b18fSScott Teel #define HPSA_RAID_51	4
5186b80b18fSScott Teel #define HPSA_RAID_6	5	/* also used for RAID 60 */
5196b80b18fSScott Teel #define HPSA_RAID_ADM	6	/* also used for RAID 1+0 ADM */
520edd16368SStephen M. Cameron #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
521edd16368SStephen M. Cameron 
522edd16368SStephen M. Cameron static ssize_t raid_level_show(struct device *dev,
523edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
524edd16368SStephen M. Cameron {
525edd16368SStephen M. Cameron 	ssize_t l = 0;
52682a72c0aSStephen M. Cameron 	unsigned char rlevel;
527edd16368SStephen M. Cameron 	struct ctlr_info *h;
528edd16368SStephen M. Cameron 	struct scsi_device *sdev;
529edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
530edd16368SStephen M. Cameron 	unsigned long flags;
531edd16368SStephen M. Cameron 
532edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
533edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
534edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
535edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
536edd16368SStephen M. Cameron 	if (!hdev) {
537edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
538edd16368SStephen M. Cameron 		return -ENODEV;
539edd16368SStephen M. Cameron 	}
540edd16368SStephen M. Cameron 
541edd16368SStephen M. Cameron 	/* Is this even a logical drive? */
542edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
543edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
544edd16368SStephen M. Cameron 		l = snprintf(buf, PAGE_SIZE, "N/A\n");
545edd16368SStephen M. Cameron 		return l;
546edd16368SStephen M. Cameron 	}
547edd16368SStephen M. Cameron 
548edd16368SStephen M. Cameron 	rlevel = hdev->raid_level;
549edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
55082a72c0aSStephen M. Cameron 	if (rlevel > RAID_UNKNOWN)
551edd16368SStephen M. Cameron 		rlevel = RAID_UNKNOWN;
552edd16368SStephen M. Cameron 	l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
553edd16368SStephen M. Cameron 	return l;
554edd16368SStephen M. Cameron }
555edd16368SStephen M. Cameron 
556edd16368SStephen M. Cameron static ssize_t lunid_show(struct device *dev,
557edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
558edd16368SStephen M. Cameron {
559edd16368SStephen M. Cameron 	struct ctlr_info *h;
560edd16368SStephen M. Cameron 	struct scsi_device *sdev;
561edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
562edd16368SStephen M. Cameron 	unsigned long flags;
563edd16368SStephen M. Cameron 	unsigned char lunid[8];
564edd16368SStephen M. Cameron 
565edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
566edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
567edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
568edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
569edd16368SStephen M. Cameron 	if (!hdev) {
570edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
571edd16368SStephen M. Cameron 		return -ENODEV;
572edd16368SStephen M. Cameron 	}
573edd16368SStephen M. Cameron 	memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
574edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
575edd16368SStephen M. Cameron 	return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
576edd16368SStephen M. Cameron 		lunid[0], lunid[1], lunid[2], lunid[3],
577edd16368SStephen M. Cameron 		lunid[4], lunid[5], lunid[6], lunid[7]);
578edd16368SStephen M. Cameron }
579edd16368SStephen M. Cameron 
580edd16368SStephen M. Cameron static ssize_t unique_id_show(struct device *dev,
581edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
582edd16368SStephen M. Cameron {
583edd16368SStephen M. Cameron 	struct ctlr_info *h;
584edd16368SStephen M. Cameron 	struct scsi_device *sdev;
585edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
586edd16368SStephen M. Cameron 	unsigned long flags;
587edd16368SStephen M. Cameron 	unsigned char sn[16];
588edd16368SStephen M. Cameron 
589edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
590edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
591edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
592edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
593edd16368SStephen M. Cameron 	if (!hdev) {
594edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
595edd16368SStephen M. Cameron 		return -ENODEV;
596edd16368SStephen M. Cameron 	}
597edd16368SStephen M. Cameron 	memcpy(sn, hdev->device_id, sizeof(sn));
598edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
599edd16368SStephen M. Cameron 	return snprintf(buf, 16 * 2 + 2,
600edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X"
601edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X\n",
602edd16368SStephen M. Cameron 			sn[0], sn[1], sn[2], sn[3],
603edd16368SStephen M. Cameron 			sn[4], sn[5], sn[6], sn[7],
604edd16368SStephen M. Cameron 			sn[8], sn[9], sn[10], sn[11],
605edd16368SStephen M. Cameron 			sn[12], sn[13], sn[14], sn[15]);
606edd16368SStephen M. Cameron }
607edd16368SStephen M. Cameron 
608c1988684SScott Teel static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
609c1988684SScott Teel 	     struct device_attribute *attr, char *buf)
610c1988684SScott Teel {
611c1988684SScott Teel 	struct ctlr_info *h;
612c1988684SScott Teel 	struct scsi_device *sdev;
613c1988684SScott Teel 	struct hpsa_scsi_dev_t *hdev;
614c1988684SScott Teel 	unsigned long flags;
615c1988684SScott Teel 	int offload_enabled;
616c1988684SScott Teel 
617c1988684SScott Teel 	sdev = to_scsi_device(dev);
618c1988684SScott Teel 	h = sdev_to_hba(sdev);
619c1988684SScott Teel 	spin_lock_irqsave(&h->lock, flags);
620c1988684SScott Teel 	hdev = sdev->hostdata;
621c1988684SScott Teel 	if (!hdev) {
622c1988684SScott Teel 		spin_unlock_irqrestore(&h->lock, flags);
623c1988684SScott Teel 		return -ENODEV;
624c1988684SScott Teel 	}
625c1988684SScott Teel 	offload_enabled = hdev->offload_enabled;
626c1988684SScott Teel 	spin_unlock_irqrestore(&h->lock, flags);
627c1988684SScott Teel 	return snprintf(buf, 20, "%d\n", offload_enabled);
628c1988684SScott Teel }
629c1988684SScott Teel 
6303f5eac3aSStephen M. Cameron static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
6313f5eac3aSStephen M. Cameron static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
6323f5eac3aSStephen M. Cameron static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
6333f5eac3aSStephen M. Cameron static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
634c1988684SScott Teel static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
635c1988684SScott Teel 			host_show_hp_ssd_smart_path_enabled, NULL);
636da0697bdSScott Teel static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
637da0697bdSScott Teel 		host_show_hp_ssd_smart_path_status,
638da0697bdSScott Teel 		host_store_hp_ssd_smart_path_status);
6392ba8bfc8SStephen M. Cameron static DEVICE_ATTR(raid_offload_debug, S_IWUSR, NULL,
6402ba8bfc8SStephen M. Cameron 			host_store_raid_offload_debug);
6413f5eac3aSStephen M. Cameron static DEVICE_ATTR(firmware_revision, S_IRUGO,
6423f5eac3aSStephen M. Cameron 	host_show_firmware_revision, NULL);
6433f5eac3aSStephen M. Cameron static DEVICE_ATTR(commands_outstanding, S_IRUGO,
6443f5eac3aSStephen M. Cameron 	host_show_commands_outstanding, NULL);
6453f5eac3aSStephen M. Cameron static DEVICE_ATTR(transport_mode, S_IRUGO,
6463f5eac3aSStephen M. Cameron 	host_show_transport_mode, NULL);
647941b1cdaSStephen M. Cameron static DEVICE_ATTR(resettable, S_IRUGO,
648941b1cdaSStephen M. Cameron 	host_show_resettable, NULL);
6493f5eac3aSStephen M. Cameron 
6503f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_sdev_attrs[] = {
6513f5eac3aSStephen M. Cameron 	&dev_attr_raid_level,
6523f5eac3aSStephen M. Cameron 	&dev_attr_lunid,
6533f5eac3aSStephen M. Cameron 	&dev_attr_unique_id,
654c1988684SScott Teel 	&dev_attr_hp_ssd_smart_path_enabled,
6553f5eac3aSStephen M. Cameron 	NULL,
6563f5eac3aSStephen M. Cameron };
6573f5eac3aSStephen M. Cameron 
6583f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_shost_attrs[] = {
6593f5eac3aSStephen M. Cameron 	&dev_attr_rescan,
6603f5eac3aSStephen M. Cameron 	&dev_attr_firmware_revision,
6613f5eac3aSStephen M. Cameron 	&dev_attr_commands_outstanding,
6623f5eac3aSStephen M. Cameron 	&dev_attr_transport_mode,
663941b1cdaSStephen M. Cameron 	&dev_attr_resettable,
664da0697bdSScott Teel 	&dev_attr_hp_ssd_smart_path_status,
6652ba8bfc8SStephen M. Cameron 	&dev_attr_raid_offload_debug,
6663f5eac3aSStephen M. Cameron 	NULL,
6673f5eac3aSStephen M. Cameron };
6683f5eac3aSStephen M. Cameron 
6693f5eac3aSStephen M. Cameron static struct scsi_host_template hpsa_driver_template = {
6703f5eac3aSStephen M. Cameron 	.module			= THIS_MODULE,
671f79cfec6SStephen M. Cameron 	.name			= HPSA,
672f79cfec6SStephen M. Cameron 	.proc_name		= HPSA,
6733f5eac3aSStephen M. Cameron 	.queuecommand		= hpsa_scsi_queue_command,
6743f5eac3aSStephen M. Cameron 	.scan_start		= hpsa_scan_start,
6753f5eac3aSStephen M. Cameron 	.scan_finished		= hpsa_scan_finished,
6767c0a0229SDon Brace 	.change_queue_depth	= hpsa_change_queue_depth,
6773f5eac3aSStephen M. Cameron 	.this_id		= -1,
6783f5eac3aSStephen M. Cameron 	.use_clustering		= ENABLE_CLUSTERING,
67975167d2cSStephen M. Cameron 	.eh_abort_handler	= hpsa_eh_abort_handler,
6803f5eac3aSStephen M. Cameron 	.eh_device_reset_handler = hpsa_eh_device_reset_handler,
6813f5eac3aSStephen M. Cameron 	.ioctl			= hpsa_ioctl,
6823f5eac3aSStephen M. Cameron 	.slave_alloc		= hpsa_slave_alloc,
6833f5eac3aSStephen M. Cameron 	.slave_destroy		= hpsa_slave_destroy,
6843f5eac3aSStephen M. Cameron #ifdef CONFIG_COMPAT
6853f5eac3aSStephen M. Cameron 	.compat_ioctl		= hpsa_compat_ioctl,
6863f5eac3aSStephen M. Cameron #endif
6873f5eac3aSStephen M. Cameron 	.sdev_attrs = hpsa_sdev_attrs,
6883f5eac3aSStephen M. Cameron 	.shost_attrs = hpsa_shost_attrs,
689c0d6a4d1SStephen M. Cameron 	.max_sectors = 8192,
69054b2b50cSMartin K. Petersen 	.no_write_same = 1,
6913f5eac3aSStephen M. Cameron };
6923f5eac3aSStephen M. Cameron 
6933f5eac3aSStephen M. Cameron 
6943f5eac3aSStephen M. Cameron /* Enqueuing and dequeuing functions for cmdlists. */
6953f5eac3aSStephen M. Cameron static inline void addQ(struct list_head *list, struct CommandList *c)
6963f5eac3aSStephen M. Cameron {
6973f5eac3aSStephen M. Cameron 	list_add_tail(&c->list, list);
6983f5eac3aSStephen M. Cameron }
6993f5eac3aSStephen M. Cameron 
700254f796bSMatt Gates static inline u32 next_command(struct ctlr_info *h, u8 q)
7013f5eac3aSStephen M. Cameron {
7023f5eac3aSStephen M. Cameron 	u32 a;
703072b0518SStephen M. Cameron 	struct reply_queue_buffer *rq = &h->reply_queue[q];
7043f5eac3aSStephen M. Cameron 
705e1f7de0cSMatt Gates 	if (h->transMethod & CFGTBL_Trans_io_accel1)
706e1f7de0cSMatt Gates 		return h->access.command_completed(h, q);
707e1f7de0cSMatt Gates 
7083f5eac3aSStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
709254f796bSMatt Gates 		return h->access.command_completed(h, q);
7103f5eac3aSStephen M. Cameron 
711254f796bSMatt Gates 	if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
712254f796bSMatt Gates 		a = rq->head[rq->current_entry];
713254f796bSMatt Gates 		rq->current_entry++;
7140cbf768eSStephen M. Cameron 		atomic_dec(&h->commands_outstanding);
7153f5eac3aSStephen M. Cameron 	} else {
7163f5eac3aSStephen M. Cameron 		a = FIFO_EMPTY;
7173f5eac3aSStephen M. Cameron 	}
7183f5eac3aSStephen M. Cameron 	/* Check for wraparound */
719254f796bSMatt Gates 	if (rq->current_entry == h->max_commands) {
720254f796bSMatt Gates 		rq->current_entry = 0;
721254f796bSMatt Gates 		rq->wraparound ^= 1;
7223f5eac3aSStephen M. Cameron 	}
7233f5eac3aSStephen M. Cameron 	return a;
7243f5eac3aSStephen M. Cameron }
7253f5eac3aSStephen M. Cameron 
726c349775eSScott Teel /*
727c349775eSScott Teel  * There are some special bits in the bus address of the
728c349775eSScott Teel  * command that we have to set for the controller to know
729c349775eSScott Teel  * how to process the command:
730c349775eSScott Teel  *
731c349775eSScott Teel  * Normal performant mode:
732c349775eSScott Teel  * bit 0: 1 means performant mode, 0 means simple mode.
733c349775eSScott Teel  * bits 1-3 = block fetch table entry
734c349775eSScott Teel  * bits 4-6 = command type (== 0)
735c349775eSScott Teel  *
736c349775eSScott Teel  * ioaccel1 mode:
737c349775eSScott Teel  * bit 0 = "performant mode" bit.
738c349775eSScott Teel  * bits 1-3 = block fetch table entry
739c349775eSScott Teel  * bits 4-6 = command type (== 110)
740c349775eSScott Teel  * (command type is needed because ioaccel1 mode
741c349775eSScott Teel  * commands are submitted through the same register as normal
742c349775eSScott Teel  * mode commands, so this is how the controller knows whether
743c349775eSScott Teel  * the command is normal mode or ioaccel1 mode.)
744c349775eSScott Teel  *
745c349775eSScott Teel  * ioaccel2 mode:
746c349775eSScott Teel  * bit 0 = "performant mode" bit.
747c349775eSScott Teel  * bits 1-4 = block fetch table entry (note extra bit)
748c349775eSScott Teel  * bits 4-6 = not needed, because ioaccel2 mode has
749c349775eSScott Teel  * a separate special register for submitting commands.
750c349775eSScott Teel  */
751c349775eSScott Teel 
7523f5eac3aSStephen M. Cameron /* set_performant_mode: Modify the tag for cciss performant
7533f5eac3aSStephen M. Cameron  * set bit 0 for pull model, bits 3-1 for block fetch
7543f5eac3aSStephen M. Cameron  * register number
7553f5eac3aSStephen M. Cameron  */
7563f5eac3aSStephen M. Cameron static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
7573f5eac3aSStephen M. Cameron {
758254f796bSMatt Gates 	if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
7593f5eac3aSStephen M. Cameron 		c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
760eee0f03aSHannes Reinecke 		if (likely(h->msix_vector > 0))
761254f796bSMatt Gates 			c->Header.ReplyQueue =
762804a5cb5SJohn Kacur 				raw_smp_processor_id() % h->nreply_queues;
763254f796bSMatt Gates 	}
7643f5eac3aSStephen M. Cameron }
7653f5eac3aSStephen M. Cameron 
766c349775eSScott Teel static void set_ioaccel1_performant_mode(struct ctlr_info *h,
767c349775eSScott Teel 						struct CommandList *c)
768c349775eSScott Teel {
769c349775eSScott Teel 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
770c349775eSScott Teel 
771c349775eSScott Teel 	/* Tell the controller to post the reply to the queue for this
772c349775eSScott Teel 	 * processor.  This seems to give the best I/O throughput.
773c349775eSScott Teel 	 */
774c349775eSScott Teel 	cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
775c349775eSScott Teel 	/* Set the bits in the address sent down to include:
776c349775eSScott Teel 	 *  - performant mode bit (bit 0)
777c349775eSScott Teel 	 *  - pull count (bits 1-3)
778c349775eSScott Teel 	 *  - command type (bits 4-6)
779c349775eSScott Teel 	 */
780c349775eSScott Teel 	c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
781c349775eSScott Teel 					IOACCEL1_BUSADDR_CMDTYPE;
782c349775eSScott Teel }
783c349775eSScott Teel 
784c349775eSScott Teel static void set_ioaccel2_performant_mode(struct ctlr_info *h,
785c349775eSScott Teel 						struct CommandList *c)
786c349775eSScott Teel {
787c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
788c349775eSScott Teel 
789c349775eSScott Teel 	/* Tell the controller to post the reply to the queue for this
790c349775eSScott Teel 	 * processor.  This seems to give the best I/O throughput.
791c349775eSScott Teel 	 */
792c349775eSScott Teel 	cp->reply_queue = smp_processor_id() % h->nreply_queues;
793c349775eSScott Teel 	/* Set the bits in the address sent down to include:
794c349775eSScott Teel 	 *  - performant mode bit not used in ioaccel mode 2
795c349775eSScott Teel 	 *  - pull count (bits 0-3)
796c349775eSScott Teel 	 *  - command type isn't needed for ioaccel2
797c349775eSScott Teel 	 */
798c349775eSScott Teel 	c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
799c349775eSScott Teel }
800c349775eSScott Teel 
801e85c5974SStephen M. Cameron static int is_firmware_flash_cmd(u8 *cdb)
802e85c5974SStephen M. Cameron {
803e85c5974SStephen M. Cameron 	return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
804e85c5974SStephen M. Cameron }
805e85c5974SStephen M. Cameron 
806e85c5974SStephen M. Cameron /*
807e85c5974SStephen M. Cameron  * During firmware flash, the heartbeat register may not update as frequently
808e85c5974SStephen M. Cameron  * as it should.  So we dial down lockup detection during firmware flash. and
809e85c5974SStephen M. Cameron  * dial it back up when firmware flash completes.
810e85c5974SStephen M. Cameron  */
811e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
812e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
813e85c5974SStephen M. Cameron static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
814e85c5974SStephen M. Cameron 		struct CommandList *c)
815e85c5974SStephen M. Cameron {
816e85c5974SStephen M. Cameron 	if (!is_firmware_flash_cmd(c->Request.CDB))
817e85c5974SStephen M. Cameron 		return;
818e85c5974SStephen M. Cameron 	atomic_inc(&h->firmware_flash_in_progress);
819e85c5974SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
820e85c5974SStephen M. Cameron }
821e85c5974SStephen M. Cameron 
822e85c5974SStephen M. Cameron static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
823e85c5974SStephen M. Cameron 		struct CommandList *c)
824e85c5974SStephen M. Cameron {
825e85c5974SStephen M. Cameron 	if (is_firmware_flash_cmd(c->Request.CDB) &&
826e85c5974SStephen M. Cameron 		atomic_dec_and_test(&h->firmware_flash_in_progress))
827e85c5974SStephen M. Cameron 		h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
828e85c5974SStephen M. Cameron }
829e85c5974SStephen M. Cameron 
8303f5eac3aSStephen M. Cameron static void enqueue_cmd_and_start_io(struct ctlr_info *h,
8313f5eac3aSStephen M. Cameron 	struct CommandList *c)
8323f5eac3aSStephen M. Cameron {
8333f5eac3aSStephen M. Cameron 	unsigned long flags;
8343f5eac3aSStephen M. Cameron 
835c349775eSScott Teel 	switch (c->cmd_type) {
836c349775eSScott Teel 	case CMD_IOACCEL1:
837c349775eSScott Teel 		set_ioaccel1_performant_mode(h, c);
838c349775eSScott Teel 		break;
839c349775eSScott Teel 	case CMD_IOACCEL2:
840c349775eSScott Teel 		set_ioaccel2_performant_mode(h, c);
841c349775eSScott Teel 		break;
842c349775eSScott Teel 	default:
8433f5eac3aSStephen M. Cameron 		set_performant_mode(h, c);
844c349775eSScott Teel 	}
845e85c5974SStephen M. Cameron 	dial_down_lockup_detection_during_fw_flash(h, c);
8463f5eac3aSStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
8473f5eac3aSStephen M. Cameron 	addQ(&h->reqQ, c);
8483f5eac3aSStephen M. Cameron 	h->Qdepth++;
8490b57075dSStephen M. Cameron 	start_io(h, &flags);
8503f5eac3aSStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
8513f5eac3aSStephen M. Cameron }
8523f5eac3aSStephen M. Cameron 
8533f5eac3aSStephen M. Cameron static inline void removeQ(struct CommandList *c)
8543f5eac3aSStephen M. Cameron {
8553f5eac3aSStephen M. Cameron 	if (WARN_ON(list_empty(&c->list)))
8563f5eac3aSStephen M. Cameron 		return;
8573f5eac3aSStephen M. Cameron 	list_del_init(&c->list);
8583f5eac3aSStephen M. Cameron }
8593f5eac3aSStephen M. Cameron 
8603f5eac3aSStephen M. Cameron static inline int is_hba_lunid(unsigned char scsi3addr[])
8613f5eac3aSStephen M. Cameron {
8623f5eac3aSStephen M. Cameron 	return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
8633f5eac3aSStephen M. Cameron }
8643f5eac3aSStephen M. Cameron 
8653f5eac3aSStephen M. Cameron static inline int is_scsi_rev_5(struct ctlr_info *h)
8663f5eac3aSStephen M. Cameron {
8673f5eac3aSStephen M. Cameron 	if (!h->hba_inquiry_data)
8683f5eac3aSStephen M. Cameron 		return 0;
8693f5eac3aSStephen M. Cameron 	if ((h->hba_inquiry_data[2] & 0x07) == 5)
8703f5eac3aSStephen M. Cameron 		return 1;
8713f5eac3aSStephen M. Cameron 	return 0;
8723f5eac3aSStephen M. Cameron }
8733f5eac3aSStephen M. Cameron 
874edd16368SStephen M. Cameron static int hpsa_find_target_lun(struct ctlr_info *h,
875edd16368SStephen M. Cameron 	unsigned char scsi3addr[], int bus, int *target, int *lun)
876edd16368SStephen M. Cameron {
877edd16368SStephen M. Cameron 	/* finds an unused bus, target, lun for a new physical device
878edd16368SStephen M. Cameron 	 * assumes h->devlock is held
879edd16368SStephen M. Cameron 	 */
880edd16368SStephen M. Cameron 	int i, found = 0;
881cfe5badcSScott Teel 	DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
882edd16368SStephen M. Cameron 
883263d9401SAkinobu Mita 	bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
884edd16368SStephen M. Cameron 
885edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
886edd16368SStephen M. Cameron 		if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
887263d9401SAkinobu Mita 			__set_bit(h->dev[i]->target, lun_taken);
888edd16368SStephen M. Cameron 	}
889edd16368SStephen M. Cameron 
890263d9401SAkinobu Mita 	i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
891263d9401SAkinobu Mita 	if (i < HPSA_MAX_DEVICES) {
892edd16368SStephen M. Cameron 		/* *bus = 1; */
893edd16368SStephen M. Cameron 		*target = i;
894edd16368SStephen M. Cameron 		*lun = 0;
895edd16368SStephen M. Cameron 		found = 1;
896edd16368SStephen M. Cameron 	}
897edd16368SStephen M. Cameron 	return !found;
898edd16368SStephen M. Cameron }
899edd16368SStephen M. Cameron 
900edd16368SStephen M. Cameron /* Add an entry into h->dev[] array. */
901edd16368SStephen M. Cameron static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
902edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *device,
903edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *added[], int *nadded)
904edd16368SStephen M. Cameron {
905edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
906edd16368SStephen M. Cameron 	int n = h->ndevices;
907edd16368SStephen M. Cameron 	int i;
908edd16368SStephen M. Cameron 	unsigned char addr1[8], addr2[8];
909edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
910edd16368SStephen M. Cameron 
911cfe5badcSScott Teel 	if (n >= HPSA_MAX_DEVICES) {
912edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "too many devices, some will be "
913edd16368SStephen M. Cameron 			"inaccessible.\n");
914edd16368SStephen M. Cameron 		return -1;
915edd16368SStephen M. Cameron 	}
916edd16368SStephen M. Cameron 
917edd16368SStephen M. Cameron 	/* physical devices do not have lun or target assigned until now. */
918edd16368SStephen M. Cameron 	if (device->lun != -1)
919edd16368SStephen M. Cameron 		/* Logical device, lun is already assigned. */
920edd16368SStephen M. Cameron 		goto lun_assigned;
921edd16368SStephen M. Cameron 
922edd16368SStephen M. Cameron 	/* If this device a non-zero lun of a multi-lun device
923edd16368SStephen M. Cameron 	 * byte 4 of the 8-byte LUN addr will contain the logical
9242b08b3e9SDon Brace 	 * unit no, zero otherwise.
925edd16368SStephen M. Cameron 	 */
926edd16368SStephen M. Cameron 	if (device->scsi3addr[4] == 0) {
927edd16368SStephen M. Cameron 		/* This is not a non-zero lun of a multi-lun device */
928edd16368SStephen M. Cameron 		if (hpsa_find_target_lun(h, device->scsi3addr,
929edd16368SStephen M. Cameron 			device->bus, &device->target, &device->lun) != 0)
930edd16368SStephen M. Cameron 			return -1;
931edd16368SStephen M. Cameron 		goto lun_assigned;
932edd16368SStephen M. Cameron 	}
933edd16368SStephen M. Cameron 
934edd16368SStephen M. Cameron 	/* This is a non-zero lun of a multi-lun device.
935edd16368SStephen M. Cameron 	 * Search through our list and find the device which
936edd16368SStephen M. Cameron 	 * has the same 8 byte LUN address, excepting byte 4.
937edd16368SStephen M. Cameron 	 * Assign the same bus and target for this new LUN.
938edd16368SStephen M. Cameron 	 * Use the logical unit number from the firmware.
939edd16368SStephen M. Cameron 	 */
940edd16368SStephen M. Cameron 	memcpy(addr1, device->scsi3addr, 8);
941edd16368SStephen M. Cameron 	addr1[4] = 0;
942edd16368SStephen M. Cameron 	for (i = 0; i < n; i++) {
943edd16368SStephen M. Cameron 		sd = h->dev[i];
944edd16368SStephen M. Cameron 		memcpy(addr2, sd->scsi3addr, 8);
945edd16368SStephen M. Cameron 		addr2[4] = 0;
946edd16368SStephen M. Cameron 		/* differ only in byte 4? */
947edd16368SStephen M. Cameron 		if (memcmp(addr1, addr2, 8) == 0) {
948edd16368SStephen M. Cameron 			device->bus = sd->bus;
949edd16368SStephen M. Cameron 			device->target = sd->target;
950edd16368SStephen M. Cameron 			device->lun = device->scsi3addr[4];
951edd16368SStephen M. Cameron 			break;
952edd16368SStephen M. Cameron 		}
953edd16368SStephen M. Cameron 	}
954edd16368SStephen M. Cameron 	if (device->lun == -1) {
955edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
956edd16368SStephen M. Cameron 			" suspect firmware bug or unsupported hardware "
957edd16368SStephen M. Cameron 			"configuration.\n");
958edd16368SStephen M. Cameron 			return -1;
959edd16368SStephen M. Cameron 	}
960edd16368SStephen M. Cameron 
961edd16368SStephen M. Cameron lun_assigned:
962edd16368SStephen M. Cameron 
963edd16368SStephen M. Cameron 	h->dev[n] = device;
964edd16368SStephen M. Cameron 	h->ndevices++;
965edd16368SStephen M. Cameron 	added[*nadded] = device;
966edd16368SStephen M. Cameron 	(*nadded)++;
967edd16368SStephen M. Cameron 
968edd16368SStephen M. Cameron 	/* initially, (before registering with scsi layer) we don't
969edd16368SStephen M. Cameron 	 * know our hostno and we don't want to print anything first
970edd16368SStephen M. Cameron 	 * time anyway (the scsi layer's inquiries will show that info)
971edd16368SStephen M. Cameron 	 */
972edd16368SStephen M. Cameron 	/* if (hostno != -1) */
973edd16368SStephen M. Cameron 		dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
974edd16368SStephen M. Cameron 			scsi_device_type(device->devtype), hostno,
975edd16368SStephen M. Cameron 			device->bus, device->target, device->lun);
976edd16368SStephen M. Cameron 	return 0;
977edd16368SStephen M. Cameron }
978edd16368SStephen M. Cameron 
979bd9244f7SScott Teel /* Update an entry in h->dev[] array. */
980bd9244f7SScott Teel static void hpsa_scsi_update_entry(struct ctlr_info *h, int hostno,
981bd9244f7SScott Teel 	int entry, struct hpsa_scsi_dev_t *new_entry)
982bd9244f7SScott Teel {
983bd9244f7SScott Teel 	/* assumes h->devlock is held */
984bd9244f7SScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
985bd9244f7SScott Teel 
986bd9244f7SScott Teel 	/* Raid level changed. */
987bd9244f7SScott Teel 	h->dev[entry]->raid_level = new_entry->raid_level;
988250fb125SStephen M. Cameron 
989250fb125SStephen M. Cameron 	/* Raid offload parameters changed. */
990250fb125SStephen M. Cameron 	h->dev[entry]->offload_config = new_entry->offload_config;
991250fb125SStephen M. Cameron 	h->dev[entry]->offload_enabled = new_entry->offload_enabled;
9929fb0de2dSStephen M. Cameron 	h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
9939fb0de2dSStephen M. Cameron 	h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
9949fb0de2dSStephen M. Cameron 	h->dev[entry]->raid_map = new_entry->raid_map;
995250fb125SStephen M. Cameron 
996bd9244f7SScott Teel 	dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d updated.\n",
997bd9244f7SScott Teel 		scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
998bd9244f7SScott Teel 		new_entry->target, new_entry->lun);
999bd9244f7SScott Teel }
1000bd9244f7SScott Teel 
10012a8ccf31SStephen M. Cameron /* Replace an entry from h->dev[] array. */
10022a8ccf31SStephen M. Cameron static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
10032a8ccf31SStephen M. Cameron 	int entry, struct hpsa_scsi_dev_t *new_entry,
10042a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *added[], int *nadded,
10052a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
10062a8ccf31SStephen M. Cameron {
10072a8ccf31SStephen M. Cameron 	/* assumes h->devlock is held */
1008cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
10092a8ccf31SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
10102a8ccf31SStephen M. Cameron 	(*nremoved)++;
101101350d05SStephen M. Cameron 
101201350d05SStephen M. Cameron 	/*
101301350d05SStephen M. Cameron 	 * New physical devices won't have target/lun assigned yet
101401350d05SStephen M. Cameron 	 * so we need to preserve the values in the slot we are replacing.
101501350d05SStephen M. Cameron 	 */
101601350d05SStephen M. Cameron 	if (new_entry->target == -1) {
101701350d05SStephen M. Cameron 		new_entry->target = h->dev[entry]->target;
101801350d05SStephen M. Cameron 		new_entry->lun = h->dev[entry]->lun;
101901350d05SStephen M. Cameron 	}
102001350d05SStephen M. Cameron 
10212a8ccf31SStephen M. Cameron 	h->dev[entry] = new_entry;
10222a8ccf31SStephen M. Cameron 	added[*nadded] = new_entry;
10232a8ccf31SStephen M. Cameron 	(*nadded)++;
10242a8ccf31SStephen M. Cameron 	dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n",
10252a8ccf31SStephen M. Cameron 		scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
10262a8ccf31SStephen M. Cameron 			new_entry->target, new_entry->lun);
10272a8ccf31SStephen M. Cameron }
10282a8ccf31SStephen M. Cameron 
1029edd16368SStephen M. Cameron /* Remove an entry from h->dev[] array. */
1030edd16368SStephen M. Cameron static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
1031edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
1032edd16368SStephen M. Cameron {
1033edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
1034edd16368SStephen M. Cameron 	int i;
1035edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1036edd16368SStephen M. Cameron 
1037cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
1038edd16368SStephen M. Cameron 
1039edd16368SStephen M. Cameron 	sd = h->dev[entry];
1040edd16368SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
1041edd16368SStephen M. Cameron 	(*nremoved)++;
1042edd16368SStephen M. Cameron 
1043edd16368SStephen M. Cameron 	for (i = entry; i < h->ndevices-1; i++)
1044edd16368SStephen M. Cameron 		h->dev[i] = h->dev[i+1];
1045edd16368SStephen M. Cameron 	h->ndevices--;
1046edd16368SStephen M. Cameron 	dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
1047edd16368SStephen M. Cameron 		scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
1048edd16368SStephen M. Cameron 		sd->lun);
1049edd16368SStephen M. Cameron }
1050edd16368SStephen M. Cameron 
1051edd16368SStephen M. Cameron #define SCSI3ADDR_EQ(a, b) ( \
1052edd16368SStephen M. Cameron 	(a)[7] == (b)[7] && \
1053edd16368SStephen M. Cameron 	(a)[6] == (b)[6] && \
1054edd16368SStephen M. Cameron 	(a)[5] == (b)[5] && \
1055edd16368SStephen M. Cameron 	(a)[4] == (b)[4] && \
1056edd16368SStephen M. Cameron 	(a)[3] == (b)[3] && \
1057edd16368SStephen M. Cameron 	(a)[2] == (b)[2] && \
1058edd16368SStephen M. Cameron 	(a)[1] == (b)[1] && \
1059edd16368SStephen M. Cameron 	(a)[0] == (b)[0])
1060edd16368SStephen M. Cameron 
1061edd16368SStephen M. Cameron static void fixup_botched_add(struct ctlr_info *h,
1062edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *added)
1063edd16368SStephen M. Cameron {
1064edd16368SStephen M. Cameron 	/* called when scsi_add_device fails in order to re-adjust
1065edd16368SStephen M. Cameron 	 * h->dev[] to match the mid layer's view.
1066edd16368SStephen M. Cameron 	 */
1067edd16368SStephen M. Cameron 	unsigned long flags;
1068edd16368SStephen M. Cameron 	int i, j;
1069edd16368SStephen M. Cameron 
1070edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
1071edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1072edd16368SStephen M. Cameron 		if (h->dev[i] == added) {
1073edd16368SStephen M. Cameron 			for (j = i; j < h->ndevices-1; j++)
1074edd16368SStephen M. Cameron 				h->dev[j] = h->dev[j+1];
1075edd16368SStephen M. Cameron 			h->ndevices--;
1076edd16368SStephen M. Cameron 			break;
1077edd16368SStephen M. Cameron 		}
1078edd16368SStephen M. Cameron 	}
1079edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
1080edd16368SStephen M. Cameron 	kfree(added);
1081edd16368SStephen M. Cameron }
1082edd16368SStephen M. Cameron 
1083edd16368SStephen M. Cameron static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
1084edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev2)
1085edd16368SStephen M. Cameron {
1086edd16368SStephen M. Cameron 	/* we compare everything except lun and target as these
1087edd16368SStephen M. Cameron 	 * are not yet assigned.  Compare parts likely
1088edd16368SStephen M. Cameron 	 * to differ first
1089edd16368SStephen M. Cameron 	 */
1090edd16368SStephen M. Cameron 	if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
1091edd16368SStephen M. Cameron 		sizeof(dev1->scsi3addr)) != 0)
1092edd16368SStephen M. Cameron 		return 0;
1093edd16368SStephen M. Cameron 	if (memcmp(dev1->device_id, dev2->device_id,
1094edd16368SStephen M. Cameron 		sizeof(dev1->device_id)) != 0)
1095edd16368SStephen M. Cameron 		return 0;
1096edd16368SStephen M. Cameron 	if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
1097edd16368SStephen M. Cameron 		return 0;
1098edd16368SStephen M. Cameron 	if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
1099edd16368SStephen M. Cameron 		return 0;
1100edd16368SStephen M. Cameron 	if (dev1->devtype != dev2->devtype)
1101edd16368SStephen M. Cameron 		return 0;
1102edd16368SStephen M. Cameron 	if (dev1->bus != dev2->bus)
1103edd16368SStephen M. Cameron 		return 0;
1104edd16368SStephen M. Cameron 	return 1;
1105edd16368SStephen M. Cameron }
1106edd16368SStephen M. Cameron 
1107bd9244f7SScott Teel static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
1108bd9244f7SScott Teel 	struct hpsa_scsi_dev_t *dev2)
1109bd9244f7SScott Teel {
1110bd9244f7SScott Teel 	/* Device attributes that can change, but don't mean
1111bd9244f7SScott Teel 	 * that the device is a different device, nor that the OS
1112bd9244f7SScott Teel 	 * needs to be told anything about the change.
1113bd9244f7SScott Teel 	 */
1114bd9244f7SScott Teel 	if (dev1->raid_level != dev2->raid_level)
1115bd9244f7SScott Teel 		return 1;
1116250fb125SStephen M. Cameron 	if (dev1->offload_config != dev2->offload_config)
1117250fb125SStephen M. Cameron 		return 1;
1118250fb125SStephen M. Cameron 	if (dev1->offload_enabled != dev2->offload_enabled)
1119250fb125SStephen M. Cameron 		return 1;
1120bd9244f7SScott Teel 	return 0;
1121bd9244f7SScott Teel }
1122bd9244f7SScott Teel 
1123edd16368SStephen M. Cameron /* Find needle in haystack.  If exact match found, return DEVICE_SAME,
1124edd16368SStephen M. Cameron  * and return needle location in *index.  If scsi3addr matches, but not
1125edd16368SStephen M. Cameron  * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
1126bd9244f7SScott Teel  * location in *index.
1127bd9244f7SScott Teel  * In the case of a minor device attribute change, such as RAID level, just
1128bd9244f7SScott Teel  * return DEVICE_UPDATED, along with the updated device's location in index.
1129bd9244f7SScott Teel  * If needle not found, return DEVICE_NOT_FOUND.
1130edd16368SStephen M. Cameron  */
1131edd16368SStephen M. Cameron static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
1132edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *haystack[], int haystack_size,
1133edd16368SStephen M. Cameron 	int *index)
1134edd16368SStephen M. Cameron {
1135edd16368SStephen M. Cameron 	int i;
1136edd16368SStephen M. Cameron #define DEVICE_NOT_FOUND 0
1137edd16368SStephen M. Cameron #define DEVICE_CHANGED 1
1138edd16368SStephen M. Cameron #define DEVICE_SAME 2
1139bd9244f7SScott Teel #define DEVICE_UPDATED 3
1140edd16368SStephen M. Cameron 	for (i = 0; i < haystack_size; i++) {
114123231048SStephen M. Cameron 		if (haystack[i] == NULL) /* previously removed. */
114223231048SStephen M. Cameron 			continue;
1143edd16368SStephen M. Cameron 		if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
1144edd16368SStephen M. Cameron 			*index = i;
1145bd9244f7SScott Teel 			if (device_is_the_same(needle, haystack[i])) {
1146bd9244f7SScott Teel 				if (device_updated(needle, haystack[i]))
1147bd9244f7SScott Teel 					return DEVICE_UPDATED;
1148edd16368SStephen M. Cameron 				return DEVICE_SAME;
1149bd9244f7SScott Teel 			} else {
11509846590eSStephen M. Cameron 				/* Keep offline devices offline */
11519846590eSStephen M. Cameron 				if (needle->volume_offline)
11529846590eSStephen M. Cameron 					return DEVICE_NOT_FOUND;
1153edd16368SStephen M. Cameron 				return DEVICE_CHANGED;
1154edd16368SStephen M. Cameron 			}
1155edd16368SStephen M. Cameron 		}
1156bd9244f7SScott Teel 	}
1157edd16368SStephen M. Cameron 	*index = -1;
1158edd16368SStephen M. Cameron 	return DEVICE_NOT_FOUND;
1159edd16368SStephen M. Cameron }
1160edd16368SStephen M. Cameron 
11619846590eSStephen M. Cameron static void hpsa_monitor_offline_device(struct ctlr_info *h,
11629846590eSStephen M. Cameron 					unsigned char scsi3addr[])
11639846590eSStephen M. Cameron {
11649846590eSStephen M. Cameron 	struct offline_device_entry *device;
11659846590eSStephen M. Cameron 	unsigned long flags;
11669846590eSStephen M. Cameron 
11679846590eSStephen M. Cameron 	/* Check to see if device is already on the list */
11689846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
11699846590eSStephen M. Cameron 	list_for_each_entry(device, &h->offline_device_list, offline_list) {
11709846590eSStephen M. Cameron 		if (memcmp(device->scsi3addr, scsi3addr,
11719846590eSStephen M. Cameron 			sizeof(device->scsi3addr)) == 0) {
11729846590eSStephen M. Cameron 			spin_unlock_irqrestore(&h->offline_device_lock, flags);
11739846590eSStephen M. Cameron 			return;
11749846590eSStephen M. Cameron 		}
11759846590eSStephen M. Cameron 	}
11769846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
11779846590eSStephen M. Cameron 
11789846590eSStephen M. Cameron 	/* Device is not on the list, add it. */
11799846590eSStephen M. Cameron 	device = kmalloc(sizeof(*device), GFP_KERNEL);
11809846590eSStephen M. Cameron 	if (!device) {
11819846590eSStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in %s\n", __func__);
11829846590eSStephen M. Cameron 		return;
11839846590eSStephen M. Cameron 	}
11849846590eSStephen M. Cameron 	memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
11859846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
11869846590eSStephen M. Cameron 	list_add_tail(&device->offline_list, &h->offline_device_list);
11879846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
11889846590eSStephen M. Cameron }
11899846590eSStephen M. Cameron 
11909846590eSStephen M. Cameron /* Print a message explaining various offline volume states */
11919846590eSStephen M. Cameron static void hpsa_show_volume_status(struct ctlr_info *h,
11929846590eSStephen M. Cameron 	struct hpsa_scsi_dev_t *sd)
11939846590eSStephen M. Cameron {
11949846590eSStephen M. Cameron 	if (sd->volume_offline == HPSA_VPD_LV_STATUS_UNSUPPORTED)
11959846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
11969846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume status is not available through vital product data pages.\n",
11979846590eSStephen M. Cameron 			h->scsi_host->host_no,
11989846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
11999846590eSStephen M. Cameron 	switch (sd->volume_offline) {
12009846590eSStephen M. Cameron 	case HPSA_LV_OK:
12019846590eSStephen M. Cameron 		break;
12029846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ERASE:
12039846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12049846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing background erase process.\n",
12059846590eSStephen M. Cameron 			h->scsi_host->host_no,
12069846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12079846590eSStephen M. Cameron 		break;
12089846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_RPI:
12099846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12109846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing rapid parity initialization process.\n",
12119846590eSStephen M. Cameron 			h->scsi_host->host_no,
12129846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12139846590eSStephen M. Cameron 		break;
12149846590eSStephen M. Cameron 	case HPSA_LV_PENDING_RPI:
12159846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12169846590eSStephen M. Cameron 				"C%d:B%d:T%d:L%d Volume is queued for rapid parity initialization process.\n",
12179846590eSStephen M. Cameron 				h->scsi_host->host_no,
12189846590eSStephen M. Cameron 				sd->bus, sd->target, sd->lun);
12199846590eSStephen M. Cameron 		break;
12209846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_NO_KEY:
12219846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12229846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because key is not present.\n",
12239846590eSStephen M. Cameron 			h->scsi_host->host_no,
12249846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12259846590eSStephen M. Cameron 		break;
12269846590eSStephen M. Cameron 	case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
12279846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12289846590eSStephen 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",
12299846590eSStephen M. Cameron 			h->scsi_host->host_no,
12309846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12319846590eSStephen M. Cameron 		break;
12329846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION:
12339846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12349846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing encryption process.\n",
12359846590eSStephen M. Cameron 			h->scsi_host->host_no,
12369846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12379846590eSStephen M. Cameron 		break;
12389846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
12399846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12409846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing encryption re-keying process.\n",
12419846590eSStephen M. Cameron 			h->scsi_host->host_no,
12429846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12439846590eSStephen M. Cameron 		break;
12449846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
12459846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12469846590eSStephen 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",
12479846590eSStephen M. Cameron 			h->scsi_host->host_no,
12489846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12499846590eSStephen M. Cameron 		break;
12509846590eSStephen M. Cameron 	case HPSA_LV_PENDING_ENCRYPTION:
12519846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12529846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is pending migration to encrypted state, but process has not started.\n",
12539846590eSStephen M. Cameron 			h->scsi_host->host_no,
12549846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12559846590eSStephen M. Cameron 		break;
12569846590eSStephen M. Cameron 	case HPSA_LV_PENDING_ENCRYPTION_REKEYING:
12579846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12589846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is encrypted and is pending encryption rekeying.\n",
12599846590eSStephen M. Cameron 			h->scsi_host->host_no,
12609846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12619846590eSStephen M. Cameron 		break;
12629846590eSStephen M. Cameron 	}
12639846590eSStephen M. Cameron }
12649846590eSStephen M. Cameron 
12654967bd3eSStephen M. Cameron static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
1266edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd[], int nsds)
1267edd16368SStephen M. Cameron {
1268edd16368SStephen M. Cameron 	/* sd contains scsi3 addresses and devtypes, and inquiry
1269edd16368SStephen M. Cameron 	 * data.  This function takes what's in sd to be the current
1270edd16368SStephen M. Cameron 	 * reality and updates h->dev[] to reflect that reality.
1271edd16368SStephen M. Cameron 	 */
1272edd16368SStephen M. Cameron 	int i, entry, device_change, changes = 0;
1273edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *csd;
1274edd16368SStephen M. Cameron 	unsigned long flags;
1275edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **added, **removed;
1276edd16368SStephen M. Cameron 	int nadded, nremoved;
1277edd16368SStephen M. Cameron 	struct Scsi_Host *sh = NULL;
1278edd16368SStephen M. Cameron 
1279cfe5badcSScott Teel 	added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
1280cfe5badcSScott Teel 	removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
1281edd16368SStephen M. Cameron 
1282edd16368SStephen M. Cameron 	if (!added || !removed) {
1283edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in "
1284edd16368SStephen M. Cameron 			"adjust_hpsa_scsi_table\n");
1285edd16368SStephen M. Cameron 		goto free_and_out;
1286edd16368SStephen M. Cameron 	}
1287edd16368SStephen M. Cameron 
1288edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1289edd16368SStephen M. Cameron 
1290edd16368SStephen M. Cameron 	/* find any devices in h->dev[] that are not in
1291edd16368SStephen M. Cameron 	 * sd[] and remove them from h->dev[], and for any
1292edd16368SStephen M. Cameron 	 * devices which have changed, remove the old device
1293edd16368SStephen M. Cameron 	 * info and add the new device info.
1294bd9244f7SScott Teel 	 * If minor device attributes change, just update
1295bd9244f7SScott Teel 	 * the existing device structure.
1296edd16368SStephen M. Cameron 	 */
1297edd16368SStephen M. Cameron 	i = 0;
1298edd16368SStephen M. Cameron 	nremoved = 0;
1299edd16368SStephen M. Cameron 	nadded = 0;
1300edd16368SStephen M. Cameron 	while (i < h->ndevices) {
1301edd16368SStephen M. Cameron 		csd = h->dev[i];
1302edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
1303edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
1304edd16368SStephen M. Cameron 			changes++;
1305edd16368SStephen M. Cameron 			hpsa_scsi_remove_entry(h, hostno, i,
1306edd16368SStephen M. Cameron 				removed, &nremoved);
1307edd16368SStephen M. Cameron 			continue; /* remove ^^^, hence i not incremented */
1308edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
1309edd16368SStephen M. Cameron 			changes++;
13102a8ccf31SStephen M. Cameron 			hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
13112a8ccf31SStephen M. Cameron 				added, &nadded, removed, &nremoved);
1312c7f172dcSStephen M. Cameron 			/* Set it to NULL to prevent it from being freed
1313c7f172dcSStephen M. Cameron 			 * at the bottom of hpsa_update_scsi_devices()
1314c7f172dcSStephen M. Cameron 			 */
1315c7f172dcSStephen M. Cameron 			sd[entry] = NULL;
1316bd9244f7SScott Teel 		} else if (device_change == DEVICE_UPDATED) {
1317bd9244f7SScott Teel 			hpsa_scsi_update_entry(h, hostno, i, sd[entry]);
1318edd16368SStephen M. Cameron 		}
1319edd16368SStephen M. Cameron 		i++;
1320edd16368SStephen M. Cameron 	}
1321edd16368SStephen M. Cameron 
1322edd16368SStephen M. Cameron 	/* Now, make sure every device listed in sd[] is also
1323edd16368SStephen M. Cameron 	 * listed in h->dev[], adding them if they aren't found
1324edd16368SStephen M. Cameron 	 */
1325edd16368SStephen M. Cameron 
1326edd16368SStephen M. Cameron 	for (i = 0; i < nsds; i++) {
1327edd16368SStephen M. Cameron 		if (!sd[i]) /* if already added above. */
1328edd16368SStephen M. Cameron 			continue;
13299846590eSStephen M. Cameron 
13309846590eSStephen M. Cameron 		/* Don't add devices which are NOT READY, FORMAT IN PROGRESS
13319846590eSStephen M. Cameron 		 * as the SCSI mid-layer does not handle such devices well.
13329846590eSStephen M. Cameron 		 * It relentlessly loops sending TUR at 3Hz, then READ(10)
13339846590eSStephen M. Cameron 		 * at 160Hz, and prevents the system from coming up.
13349846590eSStephen M. Cameron 		 */
13359846590eSStephen M. Cameron 		if (sd[i]->volume_offline) {
13369846590eSStephen M. Cameron 			hpsa_show_volume_status(h, sd[i]);
13379846590eSStephen M. Cameron 			dev_info(&h->pdev->dev, "c%db%dt%dl%d: temporarily offline\n",
13389846590eSStephen M. Cameron 				h->scsi_host->host_no,
13399846590eSStephen M. Cameron 				sd[i]->bus, sd[i]->target, sd[i]->lun);
13409846590eSStephen M. Cameron 			continue;
13419846590eSStephen M. Cameron 		}
13429846590eSStephen M. Cameron 
1343edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1344edd16368SStephen M. Cameron 					h->ndevices, &entry);
1345edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
1346edd16368SStephen M. Cameron 			changes++;
1347edd16368SStephen M. Cameron 			if (hpsa_scsi_add_entry(h, hostno, sd[i],
1348edd16368SStephen M. Cameron 				added, &nadded) != 0)
1349edd16368SStephen M. Cameron 				break;
1350edd16368SStephen M. Cameron 			sd[i] = NULL; /* prevent from being freed later. */
1351edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
1352edd16368SStephen M. Cameron 			/* should never happen... */
1353edd16368SStephen M. Cameron 			changes++;
1354edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev,
1355edd16368SStephen M. Cameron 				"device unexpectedly changed.\n");
1356edd16368SStephen M. Cameron 			/* but if it does happen, we just ignore that device */
1357edd16368SStephen M. Cameron 		}
1358edd16368SStephen M. Cameron 	}
1359edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1360edd16368SStephen M. Cameron 
13619846590eSStephen M. Cameron 	/* Monitor devices which are in one of several NOT READY states to be
13629846590eSStephen M. Cameron 	 * brought online later. This must be done without holding h->devlock,
13639846590eSStephen M. Cameron 	 * so don't touch h->dev[]
13649846590eSStephen M. Cameron 	 */
13659846590eSStephen M. Cameron 	for (i = 0; i < nsds; i++) {
13669846590eSStephen M. Cameron 		if (!sd[i]) /* if already added above. */
13679846590eSStephen M. Cameron 			continue;
13689846590eSStephen M. Cameron 		if (sd[i]->volume_offline)
13699846590eSStephen M. Cameron 			hpsa_monitor_offline_device(h, sd[i]->scsi3addr);
13709846590eSStephen M. Cameron 	}
13719846590eSStephen M. Cameron 
1372edd16368SStephen M. Cameron 	/* Don't notify scsi mid layer of any changes the first time through
1373edd16368SStephen M. Cameron 	 * (or if there are no changes) scsi_scan_host will do it later the
1374edd16368SStephen M. Cameron 	 * first time through.
1375edd16368SStephen M. Cameron 	 */
1376edd16368SStephen M. Cameron 	if (hostno == -1 || !changes)
1377edd16368SStephen M. Cameron 		goto free_and_out;
1378edd16368SStephen M. Cameron 
1379edd16368SStephen M. Cameron 	sh = h->scsi_host;
1380edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any removed devices */
1381edd16368SStephen M. Cameron 	for (i = 0; i < nremoved; i++) {
1382edd16368SStephen M. Cameron 		struct scsi_device *sdev =
1383edd16368SStephen M. Cameron 			scsi_device_lookup(sh, removed[i]->bus,
1384edd16368SStephen M. Cameron 				removed[i]->target, removed[i]->lun);
1385edd16368SStephen M. Cameron 		if (sdev != NULL) {
1386edd16368SStephen M. Cameron 			scsi_remove_device(sdev);
1387edd16368SStephen M. Cameron 			scsi_device_put(sdev);
1388edd16368SStephen M. Cameron 		} else {
1389edd16368SStephen M. Cameron 			/* We don't expect to get here.
1390edd16368SStephen M. Cameron 			 * future cmds to this device will get selection
1391edd16368SStephen M. Cameron 			 * timeout as if the device was gone.
1392edd16368SStephen M. Cameron 			 */
1393edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
1394edd16368SStephen M. Cameron 				" for removal.", hostno, removed[i]->bus,
1395edd16368SStephen M. Cameron 				removed[i]->target, removed[i]->lun);
1396edd16368SStephen M. Cameron 		}
1397edd16368SStephen M. Cameron 		kfree(removed[i]);
1398edd16368SStephen M. Cameron 		removed[i] = NULL;
1399edd16368SStephen M. Cameron 	}
1400edd16368SStephen M. Cameron 
1401edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any added devices */
1402edd16368SStephen M. Cameron 	for (i = 0; i < nadded; i++) {
1403edd16368SStephen M. Cameron 		if (scsi_add_device(sh, added[i]->bus,
1404edd16368SStephen M. Cameron 			added[i]->target, added[i]->lun) == 0)
1405edd16368SStephen M. Cameron 			continue;
1406edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
1407edd16368SStephen M. Cameron 			"device not added.\n", hostno, added[i]->bus,
1408edd16368SStephen M. Cameron 			added[i]->target, added[i]->lun);
1409edd16368SStephen M. Cameron 		/* now we have to remove it from h->dev,
1410edd16368SStephen M. Cameron 		 * since it didn't get added to scsi mid layer
1411edd16368SStephen M. Cameron 		 */
1412edd16368SStephen M. Cameron 		fixup_botched_add(h, added[i]);
1413edd16368SStephen M. Cameron 	}
1414edd16368SStephen M. Cameron 
1415edd16368SStephen M. Cameron free_and_out:
1416edd16368SStephen M. Cameron 	kfree(added);
1417edd16368SStephen M. Cameron 	kfree(removed);
1418edd16368SStephen M. Cameron }
1419edd16368SStephen M. Cameron 
1420edd16368SStephen M. Cameron /*
14219e03aa2fSJoe Perches  * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
1422edd16368SStephen M. Cameron  * Assume's h->devlock is held.
1423edd16368SStephen M. Cameron  */
1424edd16368SStephen M. Cameron static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1425edd16368SStephen M. Cameron 	int bus, int target, int lun)
1426edd16368SStephen M. Cameron {
1427edd16368SStephen M. Cameron 	int i;
1428edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1429edd16368SStephen M. Cameron 
1430edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1431edd16368SStephen M. Cameron 		sd = h->dev[i];
1432edd16368SStephen M. Cameron 		if (sd->bus == bus && sd->target == target && sd->lun == lun)
1433edd16368SStephen M. Cameron 			return sd;
1434edd16368SStephen M. Cameron 	}
1435edd16368SStephen M. Cameron 	return NULL;
1436edd16368SStephen M. Cameron }
1437edd16368SStephen M. Cameron 
1438edd16368SStephen M. Cameron /* link sdev->hostdata to our per-device structure. */
1439edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev)
1440edd16368SStephen M. Cameron {
1441edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1442edd16368SStephen M. Cameron 	unsigned long flags;
1443edd16368SStephen M. Cameron 	struct ctlr_info *h;
1444edd16368SStephen M. Cameron 
1445edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
1446edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1447edd16368SStephen M. Cameron 	sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1448edd16368SStephen M. Cameron 		sdev_id(sdev), sdev->lun);
1449edd16368SStephen M. Cameron 	if (sd != NULL)
1450edd16368SStephen M. Cameron 		sdev->hostdata = sd;
1451edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1452edd16368SStephen M. Cameron 	return 0;
1453edd16368SStephen M. Cameron }
1454edd16368SStephen M. Cameron 
1455edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev)
1456edd16368SStephen M. Cameron {
1457bcc44255SStephen M. Cameron 	/* nothing to do. */
1458edd16368SStephen M. Cameron }
1459edd16368SStephen M. Cameron 
146033a2ffceSStephen M. Cameron static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
146133a2ffceSStephen M. Cameron {
146233a2ffceSStephen M. Cameron 	int i;
146333a2ffceSStephen M. Cameron 
146433a2ffceSStephen M. Cameron 	if (!h->cmd_sg_list)
146533a2ffceSStephen M. Cameron 		return;
146633a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
146733a2ffceSStephen M. Cameron 		kfree(h->cmd_sg_list[i]);
146833a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = NULL;
146933a2ffceSStephen M. Cameron 	}
147033a2ffceSStephen M. Cameron 	kfree(h->cmd_sg_list);
147133a2ffceSStephen M. Cameron 	h->cmd_sg_list = NULL;
147233a2ffceSStephen M. Cameron }
147333a2ffceSStephen M. Cameron 
147433a2ffceSStephen M. Cameron static int hpsa_allocate_sg_chain_blocks(struct ctlr_info *h)
147533a2ffceSStephen M. Cameron {
147633a2ffceSStephen M. Cameron 	int i;
147733a2ffceSStephen M. Cameron 
147833a2ffceSStephen M. Cameron 	if (h->chainsize <= 0)
147933a2ffceSStephen M. Cameron 		return 0;
148033a2ffceSStephen M. Cameron 
148133a2ffceSStephen M. Cameron 	h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
148233a2ffceSStephen M. Cameron 				GFP_KERNEL);
148333a2ffceSStephen M. Cameron 	if (!h->cmd_sg_list)
148433a2ffceSStephen M. Cameron 		return -ENOMEM;
148533a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
148633a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
148733a2ffceSStephen M. Cameron 						h->chainsize, GFP_KERNEL);
148833a2ffceSStephen M. Cameron 		if (!h->cmd_sg_list[i])
148933a2ffceSStephen M. Cameron 			goto clean;
149033a2ffceSStephen M. Cameron 	}
149133a2ffceSStephen M. Cameron 	return 0;
149233a2ffceSStephen M. Cameron 
149333a2ffceSStephen M. Cameron clean:
149433a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
149533a2ffceSStephen M. Cameron 	return -ENOMEM;
149633a2ffceSStephen M. Cameron }
149733a2ffceSStephen M. Cameron 
1498e2bea6dfSStephen M. Cameron static int hpsa_map_sg_chain_block(struct ctlr_info *h,
149933a2ffceSStephen M. Cameron 	struct CommandList *c)
150033a2ffceSStephen M. Cameron {
150133a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg, *chain_block;
150233a2ffceSStephen M. Cameron 	u64 temp64;
150350a0decfSStephen M. Cameron 	u32 chain_len;
150433a2ffceSStephen M. Cameron 
150533a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
150633a2ffceSStephen M. Cameron 	chain_block = h->cmd_sg_list[c->cmdindex];
150750a0decfSStephen M. Cameron 	chain_sg->Ext = cpu_to_le32(HPSA_SG_CHAIN);
150850a0decfSStephen M. Cameron 	chain_len = sizeof(*chain_sg) *
15092b08b3e9SDon Brace 		(le16_to_cpu(c->Header.SGTotal) - h->max_cmd_sg_entries);
151050a0decfSStephen M. Cameron 	chain_sg->Len = cpu_to_le32(chain_len);
151150a0decfSStephen M. Cameron 	temp64 = pci_map_single(h->pdev, chain_block, chain_len,
151233a2ffceSStephen M. Cameron 				PCI_DMA_TODEVICE);
1513e2bea6dfSStephen M. Cameron 	if (dma_mapping_error(&h->pdev->dev, temp64)) {
1514e2bea6dfSStephen M. Cameron 		/* prevent subsequent unmapping */
151550a0decfSStephen M. Cameron 		chain_sg->Addr = cpu_to_le64(0);
1516e2bea6dfSStephen M. Cameron 		return -1;
1517e2bea6dfSStephen M. Cameron 	}
151850a0decfSStephen M. Cameron 	chain_sg->Addr = cpu_to_le64(temp64);
1519e2bea6dfSStephen M. Cameron 	return 0;
152033a2ffceSStephen M. Cameron }
152133a2ffceSStephen M. Cameron 
152233a2ffceSStephen M. Cameron static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
152333a2ffceSStephen M. Cameron 	struct CommandList *c)
152433a2ffceSStephen M. Cameron {
152533a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg;
152633a2ffceSStephen M. Cameron 
152750a0decfSStephen M. Cameron 	if (le16_to_cpu(c->Header.SGTotal) <= h->max_cmd_sg_entries)
152833a2ffceSStephen M. Cameron 		return;
152933a2ffceSStephen M. Cameron 
153033a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
153150a0decfSStephen M. Cameron 	pci_unmap_single(h->pdev, le64_to_cpu(chain_sg->Addr),
153250a0decfSStephen M. Cameron 			le32_to_cpu(chain_sg->Len), PCI_DMA_TODEVICE);
153333a2ffceSStephen M. Cameron }
153433a2ffceSStephen M. Cameron 
1535a09c1441SScott Teel 
1536a09c1441SScott Teel /* Decode the various types of errors on ioaccel2 path.
1537a09c1441SScott Teel  * Return 1 for any error that should generate a RAID path retry.
1538a09c1441SScott Teel  * Return 0 for errors that don't require a RAID path retry.
1539a09c1441SScott Teel  */
1540a09c1441SScott Teel static int handle_ioaccel_mode2_error(struct ctlr_info *h,
1541c349775eSScott Teel 					struct CommandList *c,
1542c349775eSScott Teel 					struct scsi_cmnd *cmd,
1543c349775eSScott Teel 					struct io_accel2_cmd *c2)
1544c349775eSScott Teel {
1545c349775eSScott Teel 	int data_len;
1546a09c1441SScott Teel 	int retry = 0;
1547c349775eSScott Teel 
1548c349775eSScott Teel 	switch (c2->error_data.serv_response) {
1549c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_COMPLETE:
1550c349775eSScott Teel 		switch (c2->error_data.status) {
1551c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
1552c349775eSScott Teel 			break;
1553c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
1554c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1555c349775eSScott Teel 				"%s: task complete with check condition.\n",
1556c349775eSScott Teel 				"HP SSD Smart Path");
1557ee6b1889SStephen M. Cameron 			cmd->result |= SAM_STAT_CHECK_CONDITION;
1558c349775eSScott Teel 			if (c2->error_data.data_present !=
1559ee6b1889SStephen M. Cameron 					IOACCEL2_SENSE_DATA_PRESENT) {
1560ee6b1889SStephen M. Cameron 				memset(cmd->sense_buffer, 0,
1561ee6b1889SStephen M. Cameron 					SCSI_SENSE_BUFFERSIZE);
1562c349775eSScott Teel 				break;
1563ee6b1889SStephen M. Cameron 			}
1564c349775eSScott Teel 			/* copy the sense data */
1565c349775eSScott Teel 			data_len = c2->error_data.sense_data_len;
1566c349775eSScott Teel 			if (data_len > SCSI_SENSE_BUFFERSIZE)
1567c349775eSScott Teel 				data_len = SCSI_SENSE_BUFFERSIZE;
1568c349775eSScott Teel 			if (data_len > sizeof(c2->error_data.sense_data_buff))
1569c349775eSScott Teel 				data_len =
1570c349775eSScott Teel 					sizeof(c2->error_data.sense_data_buff);
1571c349775eSScott Teel 			memcpy(cmd->sense_buffer,
1572c349775eSScott Teel 				c2->error_data.sense_data_buff, data_len);
1573a09c1441SScott Teel 			retry = 1;
1574c349775eSScott Teel 			break;
1575c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
1576c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1577c349775eSScott Teel 				"%s: task complete with BUSY status.\n",
1578c349775eSScott Teel 				"HP SSD Smart Path");
1579a09c1441SScott Teel 			retry = 1;
1580c349775eSScott Teel 			break;
1581c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
1582c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1583c349775eSScott Teel 				"%s: task complete with reservation conflict.\n",
1584c349775eSScott Teel 				"HP SSD Smart Path");
1585a09c1441SScott Teel 			retry = 1;
1586c349775eSScott Teel 			break;
1587c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
1588c349775eSScott Teel 			/* Make scsi midlayer do unlimited retries */
1589c349775eSScott Teel 			cmd->result = DID_IMM_RETRY << 16;
1590c349775eSScott Teel 			break;
1591c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
1592c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1593c349775eSScott Teel 				"%s: task complete with aborted status.\n",
1594c349775eSScott Teel 				"HP SSD Smart Path");
1595a09c1441SScott Teel 			retry = 1;
1596c349775eSScott Teel 			break;
1597c349775eSScott Teel 		default:
1598c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1599c349775eSScott Teel 				"%s: task complete with unrecognized status: 0x%02x\n",
1600c349775eSScott Teel 				"HP SSD Smart Path", c2->error_data.status);
1601a09c1441SScott Teel 			retry = 1;
1602c349775eSScott Teel 			break;
1603c349775eSScott Teel 		}
1604c349775eSScott Teel 		break;
1605c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_FAILURE:
1606c349775eSScott Teel 		/* don't expect to get here. */
1607c349775eSScott Teel 		dev_warn(&h->pdev->dev,
1608c349775eSScott Teel 			"unexpected delivery or target failure, status = 0x%02x\n",
1609c349775eSScott Teel 			c2->error_data.status);
1610a09c1441SScott Teel 		retry = 1;
1611c349775eSScott Teel 		break;
1612c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
1613c349775eSScott Teel 		break;
1614c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
1615c349775eSScott Teel 		break;
1616c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
1617c349775eSScott Teel 		dev_warn(&h->pdev->dev, "task management function rejected.\n");
1618a09c1441SScott Teel 		retry = 1;
1619c349775eSScott Teel 		break;
1620c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
1621c349775eSScott Teel 		dev_warn(&h->pdev->dev, "task management function invalid LUN\n");
1622c349775eSScott Teel 		break;
1623c349775eSScott Teel 	default:
1624c349775eSScott Teel 		dev_warn(&h->pdev->dev,
1625c349775eSScott Teel 			"%s: Unrecognized server response: 0x%02x\n",
1626a09c1441SScott Teel 			"HP SSD Smart Path",
1627a09c1441SScott Teel 			c2->error_data.serv_response);
1628a09c1441SScott Teel 		retry = 1;
1629c349775eSScott Teel 		break;
1630c349775eSScott Teel 	}
1631a09c1441SScott Teel 
1632a09c1441SScott Teel 	return retry;	/* retry on raid path? */
1633c349775eSScott Teel }
1634c349775eSScott Teel 
1635c349775eSScott Teel static void process_ioaccel2_completion(struct ctlr_info *h,
1636c349775eSScott Teel 		struct CommandList *c, struct scsi_cmnd *cmd,
1637c349775eSScott Teel 		struct hpsa_scsi_dev_t *dev)
1638c349775eSScott Teel {
1639c349775eSScott Teel 	struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
1640a09c1441SScott Teel 	int raid_retry = 0;
1641c349775eSScott Teel 
1642c349775eSScott Teel 	/* check for good status */
1643c349775eSScott Teel 	if (likely(c2->error_data.serv_response == 0 &&
1644c349775eSScott Teel 			c2->error_data.status == 0)) {
1645c349775eSScott Teel 		cmd_free(h, c);
1646c349775eSScott Teel 		cmd->scsi_done(cmd);
1647c349775eSScott Teel 		return;
1648c349775eSScott Teel 	}
1649c349775eSScott Teel 
1650c349775eSScott Teel 	/* Any RAID offload error results in retry which will use
1651c349775eSScott Teel 	 * the normal I/O path so the controller can handle whatever's
1652c349775eSScott Teel 	 * wrong.
1653c349775eSScott Teel 	 */
1654c349775eSScott Teel 	if (is_logical_dev_addr_mode(dev->scsi3addr) &&
1655c349775eSScott Teel 		c2->error_data.serv_response ==
1656c349775eSScott Teel 			IOACCEL2_SERV_RESPONSE_FAILURE) {
1657c349775eSScott Teel 		dev->offload_enabled = 0;
1658e863d68eSScott Teel 		h->drv_req_rescan = 1;	/* schedule controller for a rescan */
1659c349775eSScott Teel 		cmd->result = DID_SOFT_ERROR << 16;
1660c349775eSScott Teel 		cmd_free(h, c);
1661c349775eSScott Teel 		cmd->scsi_done(cmd);
1662c349775eSScott Teel 		return;
1663c349775eSScott Teel 	}
1664a09c1441SScott Teel 	raid_retry = handle_ioaccel_mode2_error(h, c, cmd, c2);
1665a09c1441SScott Teel 	/* If error found, disable Smart Path, schedule a rescan,
1666a09c1441SScott Teel 	 * and force a retry on the standard path.
1667a09c1441SScott Teel 	 */
1668a09c1441SScott Teel 	if (raid_retry) {
1669a09c1441SScott Teel 		dev_warn(&h->pdev->dev, "%s: Retrying on standard path.\n",
1670a09c1441SScott Teel 			"HP SSD Smart Path");
1671a09c1441SScott Teel 		dev->offload_enabled = 0; /* Disable Smart Path */
1672a09c1441SScott Teel 		h->drv_req_rescan = 1;	  /* schedule controller rescan */
1673a09c1441SScott Teel 		cmd->result = DID_SOFT_ERROR << 16;
1674a09c1441SScott Teel 	}
1675c349775eSScott Teel 	cmd_free(h, c);
1676c349775eSScott Teel 	cmd->scsi_done(cmd);
1677c349775eSScott Teel }
1678c349775eSScott Teel 
16791fb011fbSStephen M. Cameron static void complete_scsi_command(struct CommandList *cp)
1680edd16368SStephen M. Cameron {
1681edd16368SStephen M. Cameron 	struct scsi_cmnd *cmd;
1682edd16368SStephen M. Cameron 	struct ctlr_info *h;
1683edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
1684283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
1685edd16368SStephen M. Cameron 
1686edd16368SStephen M. Cameron 	unsigned char sense_key;
1687edd16368SStephen M. Cameron 	unsigned char asc;      /* additional sense code */
1688edd16368SStephen M. Cameron 	unsigned char ascq;     /* additional sense code qualifier */
1689db111e18SStephen M. Cameron 	unsigned long sense_data_size;
1690edd16368SStephen M. Cameron 
1691edd16368SStephen M. Cameron 	ei = cp->err_info;
1692edd16368SStephen M. Cameron 	cmd = (struct scsi_cmnd *) cp->scsi_cmd;
1693edd16368SStephen M. Cameron 	h = cp->h;
1694283b4a9bSStephen M. Cameron 	dev = cmd->device->hostdata;
1695edd16368SStephen M. Cameron 
1696edd16368SStephen M. Cameron 	scsi_dma_unmap(cmd); /* undo the DMA mappings */
1697e1f7de0cSMatt Gates 	if ((cp->cmd_type == CMD_SCSI) &&
16982b08b3e9SDon Brace 		(le16_to_cpu(cp->Header.SGTotal) > h->max_cmd_sg_entries))
169933a2ffceSStephen M. Cameron 		hpsa_unmap_sg_chain_block(h, cp);
1700edd16368SStephen M. Cameron 
1701edd16368SStephen M. Cameron 	cmd->result = (DID_OK << 16); 		/* host byte */
1702edd16368SStephen M. Cameron 	cmd->result |= (COMMAND_COMPLETE << 8);	/* msg byte */
1703c349775eSScott Teel 
1704c349775eSScott Teel 	if (cp->cmd_type == CMD_IOACCEL2)
1705c349775eSScott Teel 		return process_ioaccel2_completion(h, cp, cmd, dev);
1706c349775eSScott Teel 
17075512672fSStephen M. Cameron 	cmd->result |= ei->ScsiStatus;
1708edd16368SStephen M. Cameron 
17096aa4c361SRobert Elliott 	scsi_set_resid(cmd, ei->ResidualCnt);
17106aa4c361SRobert Elliott 	if (ei->CommandStatus == 0) {
17116aa4c361SRobert Elliott 		cmd_free(h, cp);
17126aa4c361SRobert Elliott 		cmd->scsi_done(cmd);
17136aa4c361SRobert Elliott 		return;
17146aa4c361SRobert Elliott 	}
17156aa4c361SRobert Elliott 
17166aa4c361SRobert Elliott 	/* copy the sense data */
1717db111e18SStephen M. Cameron 	if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
1718db111e18SStephen M. Cameron 		sense_data_size = SCSI_SENSE_BUFFERSIZE;
1719db111e18SStephen M. Cameron 	else
1720db111e18SStephen M. Cameron 		sense_data_size = sizeof(ei->SenseInfo);
1721db111e18SStephen M. Cameron 	if (ei->SenseLen < sense_data_size)
1722db111e18SStephen M. Cameron 		sense_data_size = ei->SenseLen;
1723db111e18SStephen M. Cameron 
1724db111e18SStephen M. Cameron 	memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
1725edd16368SStephen M. Cameron 
1726e1f7de0cSMatt Gates 	/* For I/O accelerator commands, copy over some fields to the normal
1727e1f7de0cSMatt Gates 	 * CISS header used below for error handling.
1728e1f7de0cSMatt Gates 	 */
1729e1f7de0cSMatt Gates 	if (cp->cmd_type == CMD_IOACCEL1) {
1730e1f7de0cSMatt Gates 		struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
17312b08b3e9SDon Brace 		cp->Header.SGList = scsi_sg_count(cmd);
17322b08b3e9SDon Brace 		cp->Header.SGTotal = cpu_to_le16(cp->Header.SGList);
17332b08b3e9SDon Brace 		cp->Request.CDBLen = le16_to_cpu(c->io_flags) &
17342b08b3e9SDon Brace 			IOACCEL1_IOFLAGS_CDBLEN_MASK;
173550a0decfSStephen M. Cameron 		cp->Header.tag = c->tag;
1736e1f7de0cSMatt Gates 		memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
1737e1f7de0cSMatt Gates 		memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
1738283b4a9bSStephen M. Cameron 
1739283b4a9bSStephen M. Cameron 		/* Any RAID offload error results in retry which will use
1740283b4a9bSStephen M. Cameron 		 * the normal I/O path so the controller can handle whatever's
1741283b4a9bSStephen M. Cameron 		 * wrong.
1742283b4a9bSStephen M. Cameron 		 */
1743283b4a9bSStephen M. Cameron 		if (is_logical_dev_addr_mode(dev->scsi3addr)) {
1744283b4a9bSStephen M. Cameron 			if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
1745283b4a9bSStephen M. Cameron 				dev->offload_enabled = 0;
1746283b4a9bSStephen M. Cameron 			cmd->result = DID_SOFT_ERROR << 16;
1747283b4a9bSStephen M. Cameron 			cmd_free(h, cp);
1748283b4a9bSStephen M. Cameron 			cmd->scsi_done(cmd);
1749283b4a9bSStephen M. Cameron 			return;
1750283b4a9bSStephen M. Cameron 		}
1751e1f7de0cSMatt Gates 	}
1752e1f7de0cSMatt Gates 
1753edd16368SStephen M. Cameron 	/* an error has occurred */
1754edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
1755edd16368SStephen M. Cameron 
1756edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
1757edd16368SStephen M. Cameron 		if (ei->ScsiStatus) {
1758edd16368SStephen M. Cameron 			/* Get sense key */
1759edd16368SStephen M. Cameron 			sense_key = 0xf & ei->SenseInfo[2];
1760edd16368SStephen M. Cameron 			/* Get additional sense code */
1761edd16368SStephen M. Cameron 			asc = ei->SenseInfo[12];
1762edd16368SStephen M. Cameron 			/* Get addition sense code qualifier */
1763edd16368SStephen M. Cameron 			ascq = ei->SenseInfo[13];
1764edd16368SStephen M. Cameron 		}
1765edd16368SStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
17661d3b3609SMatt Gates 			if (sense_key == ABORTED_COMMAND) {
17672e311fbaSStephen M. Cameron 				cmd->result |= DID_SOFT_ERROR << 16;
17681d3b3609SMatt Gates 				break;
17691d3b3609SMatt Gates 			}
1770edd16368SStephen M. Cameron 			break;
1771edd16368SStephen M. Cameron 		}
1772edd16368SStephen M. Cameron 		/* Problem was not a check condition
1773edd16368SStephen M. Cameron 		 * Pass it up to the upper layers...
1774edd16368SStephen M. Cameron 		 */
1775edd16368SStephen M. Cameron 		if (ei->ScsiStatus) {
1776edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
1777edd16368SStephen M. Cameron 				"Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1778edd16368SStephen M. Cameron 				"Returning result: 0x%x\n",
1779edd16368SStephen M. Cameron 				cp, ei->ScsiStatus,
1780edd16368SStephen M. Cameron 				sense_key, asc, ascq,
1781edd16368SStephen M. Cameron 				cmd->result);
1782edd16368SStephen M. Cameron 		} else {  /* scsi status is zero??? How??? */
1783edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
1784edd16368SStephen M. Cameron 				"Returning no connection.\n", cp),
1785edd16368SStephen M. Cameron 
1786edd16368SStephen M. Cameron 			/* Ordinarily, this case should never happen,
1787edd16368SStephen M. Cameron 			 * but there is a bug in some released firmware
1788edd16368SStephen M. Cameron 			 * revisions that allows it to happen if, for
1789edd16368SStephen M. Cameron 			 * example, a 4100 backplane loses power and
1790edd16368SStephen M. Cameron 			 * the tape drive is in it.  We assume that
1791edd16368SStephen M. Cameron 			 * it's a fatal error of some kind because we
1792edd16368SStephen M. Cameron 			 * can't show that it wasn't. We will make it
1793edd16368SStephen M. Cameron 			 * look like selection timeout since that is
1794edd16368SStephen M. Cameron 			 * the most common reason for this to occur,
1795edd16368SStephen M. Cameron 			 * and it's severe enough.
1796edd16368SStephen M. Cameron 			 */
1797edd16368SStephen M. Cameron 
1798edd16368SStephen M. Cameron 			cmd->result = DID_NO_CONNECT << 16;
1799edd16368SStephen M. Cameron 		}
1800edd16368SStephen M. Cameron 		break;
1801edd16368SStephen M. Cameron 
1802edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1803edd16368SStephen M. Cameron 		break;
1804edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
1805edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p has"
1806edd16368SStephen M. Cameron 			" completed with data overrun "
1807edd16368SStephen M. Cameron 			"reported\n", cp);
1808edd16368SStephen M. Cameron 		break;
1809edd16368SStephen M. Cameron 	case CMD_INVALID: {
1810edd16368SStephen M. Cameron 		/* print_bytes(cp, sizeof(*cp), 1, 0);
1811edd16368SStephen M. Cameron 		print_cmd(cp); */
1812edd16368SStephen M. Cameron 		/* We get CMD_INVALID if you address a non-existent device
1813edd16368SStephen M. Cameron 		 * instead of a selection timeout (no response).  You will
1814edd16368SStephen M. Cameron 		 * see this if you yank out a drive, then try to access it.
1815edd16368SStephen M. Cameron 		 * This is kind of a shame because it means that any other
1816edd16368SStephen M. Cameron 		 * CMD_INVALID (e.g. driver bug) will get interpreted as a
1817edd16368SStephen M. Cameron 		 * missing target. */
1818edd16368SStephen M. Cameron 		cmd->result = DID_NO_CONNECT << 16;
1819edd16368SStephen M. Cameron 	}
1820edd16368SStephen M. Cameron 		break;
1821edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
1822256d0eaaSStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1823edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p has "
1824edd16368SStephen M. Cameron 			"protocol error\n", cp);
1825edd16368SStephen M. Cameron 		break;
1826edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
1827edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1828edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p had  hardware error\n", cp);
1829edd16368SStephen M. Cameron 		break;
1830edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
1831edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1832edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
1833edd16368SStephen M. Cameron 		break;
1834edd16368SStephen M. Cameron 	case CMD_ABORTED:
1835edd16368SStephen M. Cameron 		cmd->result = DID_ABORT << 16;
1836edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
1837edd16368SStephen M. Cameron 				cp, ei->ScsiStatus);
1838edd16368SStephen M. Cameron 		break;
1839edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
1840edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1841edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
1842edd16368SStephen M. Cameron 		break;
1843edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
1844f6e76055SStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
1845f6e76055SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p aborted due to an unsolicited "
1846edd16368SStephen M. Cameron 			"abort\n", cp);
1847edd16368SStephen M. Cameron 		break;
1848edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
1849edd16368SStephen M. Cameron 		cmd->result = DID_TIME_OUT << 16;
1850edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
1851edd16368SStephen M. Cameron 		break;
18521d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
18531d5e2ed0SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
18541d5e2ed0SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Command unabortable\n");
18551d5e2ed0SStephen M. Cameron 		break;
1856283b4a9bSStephen M. Cameron 	case CMD_IOACCEL_DISABLED:
1857283b4a9bSStephen M. Cameron 		/* This only handles the direct pass-through case since RAID
1858283b4a9bSStephen M. Cameron 		 * offload is handled above.  Just attempt a retry.
1859283b4a9bSStephen M. Cameron 		 */
1860283b4a9bSStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16;
1861283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev,
1862283b4a9bSStephen M. Cameron 				"cp %p had HP SSD Smart Path error\n", cp);
1863283b4a9bSStephen M. Cameron 		break;
1864edd16368SStephen M. Cameron 	default:
1865edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1866edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
1867edd16368SStephen M. Cameron 				cp, ei->CommandStatus);
1868edd16368SStephen M. Cameron 	}
1869edd16368SStephen M. Cameron 	cmd_free(h, cp);
18702cc5bfafSTomas Henzl 	cmd->scsi_done(cmd);
1871edd16368SStephen M. Cameron }
1872edd16368SStephen M. Cameron 
1873edd16368SStephen M. Cameron static void hpsa_pci_unmap(struct pci_dev *pdev,
1874edd16368SStephen M. Cameron 	struct CommandList *c, int sg_used, int data_direction)
1875edd16368SStephen M. Cameron {
1876edd16368SStephen M. Cameron 	int i;
1877edd16368SStephen M. Cameron 
187850a0decfSStephen M. Cameron 	for (i = 0; i < sg_used; i++)
187950a0decfSStephen M. Cameron 		pci_unmap_single(pdev, (dma_addr_t) le64_to_cpu(c->SG[i].Addr),
188050a0decfSStephen M. Cameron 				le32_to_cpu(c->SG[i].Len),
1881edd16368SStephen M. Cameron 				data_direction);
1882edd16368SStephen M. Cameron }
1883edd16368SStephen M. Cameron 
1884a2dac136SStephen M. Cameron static int hpsa_map_one(struct pci_dev *pdev,
1885edd16368SStephen M. Cameron 		struct CommandList *cp,
1886edd16368SStephen M. Cameron 		unsigned char *buf,
1887edd16368SStephen M. Cameron 		size_t buflen,
1888edd16368SStephen M. Cameron 		int data_direction)
1889edd16368SStephen M. Cameron {
189001a02ffcSStephen M. Cameron 	u64 addr64;
1891edd16368SStephen M. Cameron 
1892edd16368SStephen M. Cameron 	if (buflen == 0 || data_direction == PCI_DMA_NONE) {
1893edd16368SStephen M. Cameron 		cp->Header.SGList = 0;
189450a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(0);
1895a2dac136SStephen M. Cameron 		return 0;
1896edd16368SStephen M. Cameron 	}
1897edd16368SStephen M. Cameron 
189850a0decfSStephen M. Cameron 	addr64 = pci_map_single(pdev, buf, buflen, data_direction);
1899eceaae18SShuah Khan 	if (dma_mapping_error(&pdev->dev, addr64)) {
1900a2dac136SStephen M. Cameron 		/* Prevent subsequent unmap of something never mapped */
1901eceaae18SShuah Khan 		cp->Header.SGList = 0;
190250a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(0);
1903a2dac136SStephen M. Cameron 		return -1;
1904eceaae18SShuah Khan 	}
190550a0decfSStephen M. Cameron 	cp->SG[0].Addr = cpu_to_le64(addr64);
190650a0decfSStephen M. Cameron 	cp->SG[0].Len = cpu_to_le32(buflen);
190750a0decfSStephen M. Cameron 	cp->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* we are not chaining */
190850a0decfSStephen M. Cameron 	cp->Header.SGList = 1;   /* no. SGs contig in this cmd */
190950a0decfSStephen M. Cameron 	cp->Header.SGTotal = cpu_to_le16(1); /* total sgs in cmd list */
1910a2dac136SStephen M. Cameron 	return 0;
1911edd16368SStephen M. Cameron }
1912edd16368SStephen M. Cameron 
1913edd16368SStephen M. Cameron static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
1914edd16368SStephen M. Cameron 	struct CommandList *c)
1915edd16368SStephen M. Cameron {
1916edd16368SStephen M. Cameron 	DECLARE_COMPLETION_ONSTACK(wait);
1917edd16368SStephen M. Cameron 
1918edd16368SStephen M. Cameron 	c->waiting = &wait;
1919edd16368SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
1920edd16368SStephen M. Cameron 	wait_for_completion(&wait);
1921edd16368SStephen M. Cameron }
1922edd16368SStephen M. Cameron 
1923094963daSStephen M. Cameron static u32 lockup_detected(struct ctlr_info *h)
1924094963daSStephen M. Cameron {
1925094963daSStephen M. Cameron 	int cpu;
1926094963daSStephen M. Cameron 	u32 rc, *lockup_detected;
1927094963daSStephen M. Cameron 
1928094963daSStephen M. Cameron 	cpu = get_cpu();
1929094963daSStephen M. Cameron 	lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
1930094963daSStephen M. Cameron 	rc = *lockup_detected;
1931094963daSStephen M. Cameron 	put_cpu();
1932094963daSStephen M. Cameron 	return rc;
1933094963daSStephen M. Cameron }
1934094963daSStephen M. Cameron 
1935a0c12413SStephen M. Cameron static void hpsa_scsi_do_simple_cmd_core_if_no_lockup(struct ctlr_info *h,
1936a0c12413SStephen M. Cameron 	struct CommandList *c)
1937a0c12413SStephen M. Cameron {
1938a0c12413SStephen M. Cameron 	/* If controller lockup detected, fake a hardware error. */
1939094963daSStephen M. Cameron 	if (unlikely(lockup_detected(h)))
1940a0c12413SStephen M. Cameron 		c->err_info->CommandStatus = CMD_HARDWARE_ERR;
1941094963daSStephen M. Cameron 	else
1942a0c12413SStephen M. Cameron 		hpsa_scsi_do_simple_cmd_core(h, c);
1943a0c12413SStephen M. Cameron }
1944a0c12413SStephen M. Cameron 
19459c2fc160SStephen M. Cameron #define MAX_DRIVER_CMD_RETRIES 25
1946edd16368SStephen M. Cameron static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
1947edd16368SStephen M. Cameron 	struct CommandList *c, int data_direction)
1948edd16368SStephen M. Cameron {
19499c2fc160SStephen M. Cameron 	int backoff_time = 10, retry_count = 0;
1950edd16368SStephen M. Cameron 
1951edd16368SStephen M. Cameron 	do {
19527630abd0SJoe Perches 		memset(c->err_info, 0, sizeof(*c->err_info));
1953edd16368SStephen M. Cameron 		hpsa_scsi_do_simple_cmd_core(h, c);
1954edd16368SStephen M. Cameron 		retry_count++;
19559c2fc160SStephen M. Cameron 		if (retry_count > 3) {
19569c2fc160SStephen M. Cameron 			msleep(backoff_time);
19579c2fc160SStephen M. Cameron 			if (backoff_time < 1000)
19589c2fc160SStephen M. Cameron 				backoff_time *= 2;
19599c2fc160SStephen M. Cameron 		}
1960852af20aSMatt Bondurant 	} while ((check_for_unit_attention(h, c) ||
19619c2fc160SStephen M. Cameron 			check_for_busy(h, c)) &&
19629c2fc160SStephen M. Cameron 			retry_count <= MAX_DRIVER_CMD_RETRIES);
1963edd16368SStephen M. Cameron 	hpsa_pci_unmap(h->pdev, c, 1, data_direction);
1964edd16368SStephen M. Cameron }
1965edd16368SStephen M. Cameron 
1966d1e8beacSStephen M. Cameron static void hpsa_print_cmd(struct ctlr_info *h, char *txt,
1967d1e8beacSStephen M. Cameron 				struct CommandList *c)
1968edd16368SStephen M. Cameron {
1969d1e8beacSStephen M. Cameron 	const u8 *cdb = c->Request.CDB;
1970d1e8beacSStephen M. Cameron 	const u8 *lun = c->Header.LUN.LunAddrBytes;
1971edd16368SStephen M. Cameron 
1972d1e8beacSStephen M. Cameron 	dev_warn(&h->pdev->dev, "%s: LUN:%02x%02x%02x%02x%02x%02x%02x%02x"
1973d1e8beacSStephen M. Cameron 	" CDB:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
1974d1e8beacSStephen M. Cameron 		txt, lun[0], lun[1], lun[2], lun[3],
1975d1e8beacSStephen M. Cameron 		lun[4], lun[5], lun[6], lun[7],
1976d1e8beacSStephen M. Cameron 		cdb[0], cdb[1], cdb[2], cdb[3],
1977d1e8beacSStephen M. Cameron 		cdb[4], cdb[5], cdb[6], cdb[7],
1978d1e8beacSStephen M. Cameron 		cdb[8], cdb[9], cdb[10], cdb[11],
1979d1e8beacSStephen M. Cameron 		cdb[12], cdb[13], cdb[14], cdb[15]);
1980d1e8beacSStephen M. Cameron }
1981d1e8beacSStephen M. Cameron 
1982d1e8beacSStephen M. Cameron static void hpsa_scsi_interpret_error(struct ctlr_info *h,
1983d1e8beacSStephen M. Cameron 			struct CommandList *cp)
1984d1e8beacSStephen M. Cameron {
1985d1e8beacSStephen M. Cameron 	const struct ErrorInfo *ei = cp->err_info;
1986d1e8beacSStephen M. Cameron 	struct device *d = &cp->h->pdev->dev;
1987d1e8beacSStephen M. Cameron 	const u8 *sd = ei->SenseInfo;
1988d1e8beacSStephen M. Cameron 
1989edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
1990edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
1991d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "SCSI status", cp);
1992d1e8beacSStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION)
1993d1e8beacSStephen M. Cameron 			dev_warn(d, "SCSI Status = 02, Sense key = %02x, ASC = %02x, ASCQ = %02x\n",
1994d1e8beacSStephen M. Cameron 				sd[2] & 0x0f, sd[12], sd[13]);
1995d1e8beacSStephen M. Cameron 		else
1996d1e8beacSStephen M. Cameron 			dev_warn(d, "SCSI Status = %02x\n", ei->ScsiStatus);
1997edd16368SStephen M. Cameron 		if (ei->ScsiStatus == 0)
1998edd16368SStephen M. Cameron 			dev_warn(d, "SCSI status is abnormally zero.  "
1999edd16368SStephen M. Cameron 			"(probably indicates selection timeout "
2000edd16368SStephen M. Cameron 			"reported incorrectly due to a known "
2001edd16368SStephen M. Cameron 			"firmware bug, circa July, 2001.)\n");
2002edd16368SStephen M. Cameron 		break;
2003edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
2004edd16368SStephen M. Cameron 		break;
2005edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
2006d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "overrun condition", cp);
2007edd16368SStephen M. Cameron 		break;
2008edd16368SStephen M. Cameron 	case CMD_INVALID: {
2009edd16368SStephen M. Cameron 		/* controller unfortunately reports SCSI passthru's
2010edd16368SStephen M. Cameron 		 * to non-existent targets as invalid commands.
2011edd16368SStephen M. Cameron 		 */
2012d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "invalid command", cp);
2013d1e8beacSStephen M. Cameron 		dev_warn(d, "probably means device no longer present\n");
2014edd16368SStephen M. Cameron 		}
2015edd16368SStephen M. Cameron 		break;
2016edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
2017d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "protocol error", cp);
2018edd16368SStephen M. Cameron 		break;
2019edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
2020d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "hardware error", cp);
2021edd16368SStephen M. Cameron 		break;
2022edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
2023d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "connection lost", cp);
2024edd16368SStephen M. Cameron 		break;
2025edd16368SStephen M. Cameron 	case CMD_ABORTED:
2026d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "aborted", cp);
2027edd16368SStephen M. Cameron 		break;
2028edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
2029d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "abort failed", cp);
2030edd16368SStephen M. Cameron 		break;
2031edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
2032d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unsolicited abort", cp);
2033edd16368SStephen M. Cameron 		break;
2034edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
2035d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "timed out", cp);
2036edd16368SStephen M. Cameron 		break;
20371d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
2038d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unabortable", cp);
20391d5e2ed0SStephen M. Cameron 		break;
2040edd16368SStephen M. Cameron 	default:
2041d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unknown status", cp);
2042d1e8beacSStephen M. Cameron 		dev_warn(d, "Unknown command status %x\n",
2043edd16368SStephen M. Cameron 				ei->CommandStatus);
2044edd16368SStephen M. Cameron 	}
2045edd16368SStephen M. Cameron }
2046edd16368SStephen M. Cameron 
2047edd16368SStephen M. Cameron static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
2048b7bb24ebSStephen M. Cameron 			u16 page, unsigned char *buf,
2049edd16368SStephen M. Cameron 			unsigned char bufsize)
2050edd16368SStephen M. Cameron {
2051edd16368SStephen M. Cameron 	int rc = IO_OK;
2052edd16368SStephen M. Cameron 	struct CommandList *c;
2053edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2054edd16368SStephen M. Cameron 
2055edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
2056edd16368SStephen M. Cameron 
2057edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
2058edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2059ecd9aad4SStephen M. Cameron 		return -ENOMEM;
2060edd16368SStephen M. Cameron 	}
2061edd16368SStephen M. Cameron 
2062a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
2063a2dac136SStephen M. Cameron 			page, scsi3addr, TYPE_CMD)) {
2064a2dac136SStephen M. Cameron 		rc = -1;
2065a2dac136SStephen M. Cameron 		goto out;
2066a2dac136SStephen M. Cameron 	}
2067edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2068edd16368SStephen M. Cameron 	ei = c->err_info;
2069edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2070d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2071edd16368SStephen M. Cameron 		rc = -1;
2072edd16368SStephen M. Cameron 	}
2073a2dac136SStephen M. Cameron out:
2074edd16368SStephen M. Cameron 	cmd_special_free(h, c);
2075edd16368SStephen M. Cameron 	return rc;
2076edd16368SStephen M. Cameron }
2077edd16368SStephen M. Cameron 
2078316b221aSStephen M. Cameron static int hpsa_bmic_ctrl_mode_sense(struct ctlr_info *h,
2079316b221aSStephen M. Cameron 		unsigned char *scsi3addr, unsigned char page,
2080316b221aSStephen M. Cameron 		struct bmic_controller_parameters *buf, size_t bufsize)
2081316b221aSStephen M. Cameron {
2082316b221aSStephen M. Cameron 	int rc = IO_OK;
2083316b221aSStephen M. Cameron 	struct CommandList *c;
2084316b221aSStephen M. Cameron 	struct ErrorInfo *ei;
2085316b221aSStephen M. Cameron 
2086316b221aSStephen M. Cameron 	c = cmd_special_alloc(h);
2087316b221aSStephen M. Cameron 
2088316b221aSStephen M. Cameron 	if (c == NULL) {			/* trouble... */
2089316b221aSStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2090316b221aSStephen M. Cameron 		return -ENOMEM;
2091316b221aSStephen M. Cameron 	}
2092316b221aSStephen M. Cameron 
2093316b221aSStephen M. Cameron 	if (fill_cmd(c, BMIC_SENSE_CONTROLLER_PARAMETERS, h, buf, bufsize,
2094316b221aSStephen M. Cameron 			page, scsi3addr, TYPE_CMD)) {
2095316b221aSStephen M. Cameron 		rc = -1;
2096316b221aSStephen M. Cameron 		goto out;
2097316b221aSStephen M. Cameron 	}
2098316b221aSStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2099316b221aSStephen M. Cameron 	ei = c->err_info;
2100316b221aSStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2101316b221aSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2102316b221aSStephen M. Cameron 		rc = -1;
2103316b221aSStephen M. Cameron 	}
2104316b221aSStephen M. Cameron out:
2105316b221aSStephen M. Cameron 	cmd_special_free(h, c);
2106316b221aSStephen M. Cameron 	return rc;
2107316b221aSStephen M. Cameron 	}
2108316b221aSStephen M. Cameron 
2109bf711ac6SScott Teel static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
2110bf711ac6SScott Teel 	u8 reset_type)
2111edd16368SStephen M. Cameron {
2112edd16368SStephen M. Cameron 	int rc = IO_OK;
2113edd16368SStephen M. Cameron 	struct CommandList *c;
2114edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2115edd16368SStephen M. Cameron 
2116edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
2117edd16368SStephen M. Cameron 
2118edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
2119edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2120e9ea04a6SStephen M. Cameron 		return -ENOMEM;
2121edd16368SStephen M. Cameron 	}
2122edd16368SStephen M. Cameron 
2123a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map. */
2124bf711ac6SScott Teel 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
2125bf711ac6SScott Teel 			scsi3addr, TYPE_MSG);
2126bf711ac6SScott Teel 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to LUN reset */
2127edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core(h, c);
2128edd16368SStephen M. Cameron 	/* no unmap needed here because no data xfer. */
2129edd16368SStephen M. Cameron 
2130edd16368SStephen M. Cameron 	ei = c->err_info;
2131edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0) {
2132d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2133edd16368SStephen M. Cameron 		rc = -1;
2134edd16368SStephen M. Cameron 	}
2135edd16368SStephen M. Cameron 	cmd_special_free(h, c);
2136edd16368SStephen M. Cameron 	return rc;
2137edd16368SStephen M. Cameron }
2138edd16368SStephen M. Cameron 
2139edd16368SStephen M. Cameron static void hpsa_get_raid_level(struct ctlr_info *h,
2140edd16368SStephen M. Cameron 	unsigned char *scsi3addr, unsigned char *raid_level)
2141edd16368SStephen M. Cameron {
2142edd16368SStephen M. Cameron 	int rc;
2143edd16368SStephen M. Cameron 	unsigned char *buf;
2144edd16368SStephen M. Cameron 
2145edd16368SStephen M. Cameron 	*raid_level = RAID_UNKNOWN;
2146edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2147edd16368SStephen M. Cameron 	if (!buf)
2148edd16368SStephen M. Cameron 		return;
2149b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0xC1, buf, 64);
2150edd16368SStephen M. Cameron 	if (rc == 0)
2151edd16368SStephen M. Cameron 		*raid_level = buf[8];
2152edd16368SStephen M. Cameron 	if (*raid_level > RAID_UNKNOWN)
2153edd16368SStephen M. Cameron 		*raid_level = RAID_UNKNOWN;
2154edd16368SStephen M. Cameron 	kfree(buf);
2155edd16368SStephen M. Cameron 	return;
2156edd16368SStephen M. Cameron }
2157edd16368SStephen M. Cameron 
2158283b4a9bSStephen M. Cameron #define HPSA_MAP_DEBUG
2159283b4a9bSStephen M. Cameron #ifdef HPSA_MAP_DEBUG
2160283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
2161283b4a9bSStephen M. Cameron 				struct raid_map_data *map_buff)
2162283b4a9bSStephen M. Cameron {
2163283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map_buff->data[0];
2164283b4a9bSStephen M. Cameron 	int map, row, col;
2165283b4a9bSStephen M. Cameron 	u16 map_cnt, row_cnt, disks_per_row;
2166283b4a9bSStephen M. Cameron 
2167283b4a9bSStephen M. Cameron 	if (rc != 0)
2168283b4a9bSStephen M. Cameron 		return;
2169283b4a9bSStephen M. Cameron 
21702ba8bfc8SStephen M. Cameron 	/* Show details only if debugging has been activated. */
21712ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug < 2)
21722ba8bfc8SStephen M. Cameron 		return;
21732ba8bfc8SStephen M. Cameron 
2174283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "structure_size = %u\n",
2175283b4a9bSStephen M. Cameron 				le32_to_cpu(map_buff->structure_size));
2176283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
2177283b4a9bSStephen M. Cameron 			le32_to_cpu(map_buff->volume_blk_size));
2178283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
2179283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->volume_blk_cnt));
2180283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
2181283b4a9bSStephen M. Cameron 			map_buff->phys_blk_shift);
2182283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
2183283b4a9bSStephen M. Cameron 			map_buff->parity_rotation_shift);
2184283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "strip_size = %u\n",
2185283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->strip_size));
2186283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
2187283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_starting_blk));
2188283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
2189283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_blk_cnt));
2190283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
2191283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->data_disks_per_row));
2192283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
2193283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->metadata_disks_per_row));
2194283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "row_cnt = %u\n",
2195283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->row_cnt));
2196283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "layout_map_count = %u\n",
2197283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->layout_map_count));
21982b08b3e9SDon Brace 	dev_info(&h->pdev->dev, "flags = 0x%x\n",
2199dd0e19f3SScott Teel 			le16_to_cpu(map_buff->flags));
22002b08b3e9SDon Brace 	dev_info(&h->pdev->dev, "encrypytion = %s\n",
22012b08b3e9SDon Brace 			le16_to_cpu(map_buff->flags) &
22022b08b3e9SDon Brace 			RAID_MAP_FLAG_ENCRYPT_ON ?  "ON" : "OFF");
2203dd0e19f3SScott Teel 	dev_info(&h->pdev->dev, "dekindex = %u\n",
2204dd0e19f3SScott Teel 			le16_to_cpu(map_buff->dekindex));
2205283b4a9bSStephen M. Cameron 	map_cnt = le16_to_cpu(map_buff->layout_map_count);
2206283b4a9bSStephen M. Cameron 	for (map = 0; map < map_cnt; map++) {
2207283b4a9bSStephen M. Cameron 		dev_info(&h->pdev->dev, "Map%u:\n", map);
2208283b4a9bSStephen M. Cameron 		row_cnt = le16_to_cpu(map_buff->row_cnt);
2209283b4a9bSStephen M. Cameron 		for (row = 0; row < row_cnt; row++) {
2210283b4a9bSStephen M. Cameron 			dev_info(&h->pdev->dev, "  Row%u:\n", row);
2211283b4a9bSStephen M. Cameron 			disks_per_row =
2212283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->data_disks_per_row);
2213283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2214283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2215283b4a9bSStephen M. Cameron 					"    D%02u: h=0x%04x xor=%u,%u\n",
2216283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2217283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2218283b4a9bSStephen M. Cameron 			disks_per_row =
2219283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->metadata_disks_per_row);
2220283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2221283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2222283b4a9bSStephen M. Cameron 					"    M%02u: h=0x%04x xor=%u,%u\n",
2223283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2224283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2225283b4a9bSStephen M. Cameron 		}
2226283b4a9bSStephen M. Cameron 	}
2227283b4a9bSStephen M. Cameron }
2228283b4a9bSStephen M. Cameron #else
2229283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
2230283b4a9bSStephen M. Cameron 			__attribute__((unused)) int rc,
2231283b4a9bSStephen M. Cameron 			__attribute__((unused)) struct raid_map_data *map_buff)
2232283b4a9bSStephen M. Cameron {
2233283b4a9bSStephen M. Cameron }
2234283b4a9bSStephen M. Cameron #endif
2235283b4a9bSStephen M. Cameron 
2236283b4a9bSStephen M. Cameron static int hpsa_get_raid_map(struct ctlr_info *h,
2237283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2238283b4a9bSStephen M. Cameron {
2239283b4a9bSStephen M. Cameron 	int rc = 0;
2240283b4a9bSStephen M. Cameron 	struct CommandList *c;
2241283b4a9bSStephen M. Cameron 	struct ErrorInfo *ei;
2242283b4a9bSStephen M. Cameron 
2243283b4a9bSStephen M. Cameron 	c = cmd_special_alloc(h);
2244283b4a9bSStephen M. Cameron 	if (c == NULL) {
2245283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2246283b4a9bSStephen M. Cameron 		return -ENOMEM;
2247283b4a9bSStephen M. Cameron 	}
2248283b4a9bSStephen M. Cameron 	if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
2249283b4a9bSStephen M. Cameron 			sizeof(this_device->raid_map), 0,
2250283b4a9bSStephen M. Cameron 			scsi3addr, TYPE_CMD)) {
2251283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "Out of memory in hpsa_get_raid_map()\n");
2252283b4a9bSStephen M. Cameron 		cmd_special_free(h, c);
2253283b4a9bSStephen M. Cameron 		return -ENOMEM;
2254283b4a9bSStephen M. Cameron 	}
2255283b4a9bSStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2256283b4a9bSStephen M. Cameron 	ei = c->err_info;
2257283b4a9bSStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2258d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2259283b4a9bSStephen M. Cameron 		cmd_special_free(h, c);
2260283b4a9bSStephen M. Cameron 		return -1;
2261283b4a9bSStephen M. Cameron 	}
2262283b4a9bSStephen M. Cameron 	cmd_special_free(h, c);
2263283b4a9bSStephen M. Cameron 
2264283b4a9bSStephen M. Cameron 	/* @todo in the future, dynamically allocate RAID map memory */
2265283b4a9bSStephen M. Cameron 	if (le32_to_cpu(this_device->raid_map.structure_size) >
2266283b4a9bSStephen M. Cameron 				sizeof(this_device->raid_map)) {
2267283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
2268283b4a9bSStephen M. Cameron 		rc = -1;
2269283b4a9bSStephen M. Cameron 	}
2270283b4a9bSStephen M. Cameron 	hpsa_debug_map_buff(h, rc, &this_device->raid_map);
2271283b4a9bSStephen M. Cameron 	return rc;
2272283b4a9bSStephen M. Cameron }
2273283b4a9bSStephen M. Cameron 
22741b70150aSStephen M. Cameron static int hpsa_vpd_page_supported(struct ctlr_info *h,
22751b70150aSStephen M. Cameron 	unsigned char scsi3addr[], u8 page)
22761b70150aSStephen M. Cameron {
22771b70150aSStephen M. Cameron 	int rc;
22781b70150aSStephen M. Cameron 	int i;
22791b70150aSStephen M. Cameron 	int pages;
22801b70150aSStephen M. Cameron 	unsigned char *buf, bufsize;
22811b70150aSStephen M. Cameron 
22821b70150aSStephen M. Cameron 	buf = kzalloc(256, GFP_KERNEL);
22831b70150aSStephen M. Cameron 	if (!buf)
22841b70150aSStephen M. Cameron 		return 0;
22851b70150aSStephen M. Cameron 
22861b70150aSStephen M. Cameron 	/* Get the size of the page list first */
22871b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
22881b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
22891b70150aSStephen M. Cameron 				buf, HPSA_VPD_HEADER_SZ);
22901b70150aSStephen M. Cameron 	if (rc != 0)
22911b70150aSStephen M. Cameron 		goto exit_unsupported;
22921b70150aSStephen M. Cameron 	pages = buf[3];
22931b70150aSStephen M. Cameron 	if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
22941b70150aSStephen M. Cameron 		bufsize = pages + HPSA_VPD_HEADER_SZ;
22951b70150aSStephen M. Cameron 	else
22961b70150aSStephen M. Cameron 		bufsize = 255;
22971b70150aSStephen M. Cameron 
22981b70150aSStephen M. Cameron 	/* Get the whole VPD page list */
22991b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
23001b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
23011b70150aSStephen M. Cameron 				buf, bufsize);
23021b70150aSStephen M. Cameron 	if (rc != 0)
23031b70150aSStephen M. Cameron 		goto exit_unsupported;
23041b70150aSStephen M. Cameron 
23051b70150aSStephen M. Cameron 	pages = buf[3];
23061b70150aSStephen M. Cameron 	for (i = 1; i <= pages; i++)
23071b70150aSStephen M. Cameron 		if (buf[3 + i] == page)
23081b70150aSStephen M. Cameron 			goto exit_supported;
23091b70150aSStephen M. Cameron exit_unsupported:
23101b70150aSStephen M. Cameron 	kfree(buf);
23111b70150aSStephen M. Cameron 	return 0;
23121b70150aSStephen M. Cameron exit_supported:
23131b70150aSStephen M. Cameron 	kfree(buf);
23141b70150aSStephen M. Cameron 	return 1;
23151b70150aSStephen M. Cameron }
23161b70150aSStephen M. Cameron 
2317283b4a9bSStephen M. Cameron static void hpsa_get_ioaccel_status(struct ctlr_info *h,
2318283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2319283b4a9bSStephen M. Cameron {
2320283b4a9bSStephen M. Cameron 	int rc;
2321283b4a9bSStephen M. Cameron 	unsigned char *buf;
2322283b4a9bSStephen M. Cameron 	u8 ioaccel_status;
2323283b4a9bSStephen M. Cameron 
2324283b4a9bSStephen M. Cameron 	this_device->offload_config = 0;
2325283b4a9bSStephen M. Cameron 	this_device->offload_enabled = 0;
2326283b4a9bSStephen M. Cameron 
2327283b4a9bSStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2328283b4a9bSStephen M. Cameron 	if (!buf)
2329283b4a9bSStephen M. Cameron 		return;
23301b70150aSStephen M. Cameron 	if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
23311b70150aSStephen M. Cameron 		goto out;
2332283b4a9bSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
2333b7bb24ebSStephen M. Cameron 			VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
2334283b4a9bSStephen M. Cameron 	if (rc != 0)
2335283b4a9bSStephen M. Cameron 		goto out;
2336283b4a9bSStephen M. Cameron 
2337283b4a9bSStephen M. Cameron #define IOACCEL_STATUS_BYTE 4
2338283b4a9bSStephen M. Cameron #define OFFLOAD_CONFIGURED_BIT 0x01
2339283b4a9bSStephen M. Cameron #define OFFLOAD_ENABLED_BIT 0x02
2340283b4a9bSStephen M. Cameron 	ioaccel_status = buf[IOACCEL_STATUS_BYTE];
2341283b4a9bSStephen M. Cameron 	this_device->offload_config =
2342283b4a9bSStephen M. Cameron 		!!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
2343283b4a9bSStephen M. Cameron 	if (this_device->offload_config) {
2344283b4a9bSStephen M. Cameron 		this_device->offload_enabled =
2345283b4a9bSStephen M. Cameron 			!!(ioaccel_status & OFFLOAD_ENABLED_BIT);
2346283b4a9bSStephen M. Cameron 		if (hpsa_get_raid_map(h, scsi3addr, this_device))
2347283b4a9bSStephen M. Cameron 			this_device->offload_enabled = 0;
2348283b4a9bSStephen M. Cameron 	}
2349283b4a9bSStephen M. Cameron out:
2350283b4a9bSStephen M. Cameron 	kfree(buf);
2351283b4a9bSStephen M. Cameron 	return;
2352283b4a9bSStephen M. Cameron }
2353283b4a9bSStephen M. Cameron 
2354edd16368SStephen M. Cameron /* Get the device id from inquiry page 0x83 */
2355edd16368SStephen M. Cameron static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
2356edd16368SStephen M. Cameron 	unsigned char *device_id, int buflen)
2357edd16368SStephen M. Cameron {
2358edd16368SStephen M. Cameron 	int rc;
2359edd16368SStephen M. Cameron 	unsigned char *buf;
2360edd16368SStephen M. Cameron 
2361edd16368SStephen M. Cameron 	if (buflen > 16)
2362edd16368SStephen M. Cameron 		buflen = 16;
2363edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2364edd16368SStephen M. Cameron 	if (!buf)
2365a84d794dSStephen M. Cameron 		return -ENOMEM;
2366b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0x83, buf, 64);
2367edd16368SStephen M. Cameron 	if (rc == 0)
2368edd16368SStephen M. Cameron 		memcpy(device_id, &buf[8], buflen);
2369edd16368SStephen M. Cameron 	kfree(buf);
2370edd16368SStephen M. Cameron 	return rc != 0;
2371edd16368SStephen M. Cameron }
2372edd16368SStephen M. Cameron 
2373edd16368SStephen M. Cameron static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
2374edd16368SStephen M. Cameron 		struct ReportLUNdata *buf, int bufsize,
2375edd16368SStephen M. Cameron 		int extended_response)
2376edd16368SStephen M. Cameron {
2377edd16368SStephen M. Cameron 	int rc = IO_OK;
2378edd16368SStephen M. Cameron 	struct CommandList *c;
2379edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
2380edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2381edd16368SStephen M. Cameron 
2382edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
2383edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
2384edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2385edd16368SStephen M. Cameron 		return -1;
2386edd16368SStephen M. Cameron 	}
2387e89c0ae7SStephen M. Cameron 	/* address the controller */
2388e89c0ae7SStephen M. Cameron 	memset(scsi3addr, 0, sizeof(scsi3addr));
2389a2dac136SStephen M. Cameron 	if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
2390a2dac136SStephen M. Cameron 		buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
2391a2dac136SStephen M. Cameron 		rc = -1;
2392a2dac136SStephen M. Cameron 		goto out;
2393a2dac136SStephen M. Cameron 	}
2394edd16368SStephen M. Cameron 	if (extended_response)
2395edd16368SStephen M. Cameron 		c->Request.CDB[1] = extended_response;
2396edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2397edd16368SStephen M. Cameron 	ei = c->err_info;
2398edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 &&
2399edd16368SStephen M. Cameron 	    ei->CommandStatus != CMD_DATA_UNDERRUN) {
2400d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2401edd16368SStephen M. Cameron 		rc = -1;
2402283b4a9bSStephen M. Cameron 	} else {
2403283b4a9bSStephen M. Cameron 		if (buf->extended_response_flag != extended_response) {
2404283b4a9bSStephen M. Cameron 			dev_err(&h->pdev->dev,
2405283b4a9bSStephen M. Cameron 				"report luns requested format %u, got %u\n",
2406283b4a9bSStephen M. Cameron 				extended_response,
2407283b4a9bSStephen M. Cameron 				buf->extended_response_flag);
2408283b4a9bSStephen M. Cameron 			rc = -1;
2409283b4a9bSStephen M. Cameron 		}
2410edd16368SStephen M. Cameron 	}
2411a2dac136SStephen M. Cameron out:
2412edd16368SStephen M. Cameron 	cmd_special_free(h, c);
2413edd16368SStephen M. Cameron 	return rc;
2414edd16368SStephen M. Cameron }
2415edd16368SStephen M. Cameron 
2416edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
2417edd16368SStephen M. Cameron 		struct ReportLUNdata *buf,
2418edd16368SStephen M. Cameron 		int bufsize, int extended_response)
2419edd16368SStephen M. Cameron {
2420edd16368SStephen M. Cameron 	return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
2421edd16368SStephen M. Cameron }
2422edd16368SStephen M. Cameron 
2423edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
2424edd16368SStephen M. Cameron 		struct ReportLUNdata *buf, int bufsize)
2425edd16368SStephen M. Cameron {
2426edd16368SStephen M. Cameron 	return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
2427edd16368SStephen M. Cameron }
2428edd16368SStephen M. Cameron 
2429edd16368SStephen M. Cameron static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
2430edd16368SStephen M. Cameron 	int bus, int target, int lun)
2431edd16368SStephen M. Cameron {
2432edd16368SStephen M. Cameron 	device->bus = bus;
2433edd16368SStephen M. Cameron 	device->target = target;
2434edd16368SStephen M. Cameron 	device->lun = lun;
2435edd16368SStephen M. Cameron }
2436edd16368SStephen M. Cameron 
24379846590eSStephen M. Cameron /* Use VPD inquiry to get details of volume status */
24389846590eSStephen M. Cameron static int hpsa_get_volume_status(struct ctlr_info *h,
24399846590eSStephen M. Cameron 					unsigned char scsi3addr[])
24409846590eSStephen M. Cameron {
24419846590eSStephen M. Cameron 	int rc;
24429846590eSStephen M. Cameron 	int status;
24439846590eSStephen M. Cameron 	int size;
24449846590eSStephen M. Cameron 	unsigned char *buf;
24459846590eSStephen M. Cameron 
24469846590eSStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
24479846590eSStephen M. Cameron 	if (!buf)
24489846590eSStephen M. Cameron 		return HPSA_VPD_LV_STATUS_UNSUPPORTED;
24499846590eSStephen M. Cameron 
24509846590eSStephen M. Cameron 	/* Does controller have VPD for logical volume status? */
245124a4b078SStephen M. Cameron 	if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_STATUS))
24529846590eSStephen M. Cameron 		goto exit_failed;
24539846590eSStephen M. Cameron 
24549846590eSStephen M. Cameron 	/* Get the size of the VPD return buffer */
24559846590eSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
24569846590eSStephen M. Cameron 					buf, HPSA_VPD_HEADER_SZ);
245724a4b078SStephen M. Cameron 	if (rc != 0)
24589846590eSStephen M. Cameron 		goto exit_failed;
24599846590eSStephen M. Cameron 	size = buf[3];
24609846590eSStephen M. Cameron 
24619846590eSStephen M. Cameron 	/* Now get the whole VPD buffer */
24629846590eSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
24639846590eSStephen M. Cameron 					buf, size + HPSA_VPD_HEADER_SZ);
246424a4b078SStephen M. Cameron 	if (rc != 0)
24659846590eSStephen M. Cameron 		goto exit_failed;
24669846590eSStephen M. Cameron 	status = buf[4]; /* status byte */
24679846590eSStephen M. Cameron 
24689846590eSStephen M. Cameron 	kfree(buf);
24699846590eSStephen M. Cameron 	return status;
24709846590eSStephen M. Cameron exit_failed:
24719846590eSStephen M. Cameron 	kfree(buf);
24729846590eSStephen M. Cameron 	return HPSA_VPD_LV_STATUS_UNSUPPORTED;
24739846590eSStephen M. Cameron }
24749846590eSStephen M. Cameron 
24759846590eSStephen M. Cameron /* Determine offline status of a volume.
24769846590eSStephen M. Cameron  * Return either:
24779846590eSStephen M. Cameron  *  0 (not offline)
247867955ba3SStephen M. Cameron  *  0xff (offline for unknown reasons)
24799846590eSStephen M. Cameron  *  # (integer code indicating one of several NOT READY states
24809846590eSStephen M. Cameron  *     describing why a volume is to be kept offline)
24819846590eSStephen M. Cameron  */
248267955ba3SStephen M. Cameron static int hpsa_volume_offline(struct ctlr_info *h,
24839846590eSStephen M. Cameron 					unsigned char scsi3addr[])
24849846590eSStephen M. Cameron {
24859846590eSStephen M. Cameron 	struct CommandList *c;
24869846590eSStephen M. Cameron 	unsigned char *sense, sense_key, asc, ascq;
24879846590eSStephen M. Cameron 	int ldstat = 0;
24889846590eSStephen M. Cameron 	u16 cmd_status;
24899846590eSStephen M. Cameron 	u8 scsi_status;
24909846590eSStephen M. Cameron #define ASC_LUN_NOT_READY 0x04
24919846590eSStephen M. Cameron #define ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS 0x04
24929846590eSStephen M. Cameron #define ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ 0x02
24939846590eSStephen M. Cameron 
24949846590eSStephen M. Cameron 	c = cmd_alloc(h);
24959846590eSStephen M. Cameron 	if (!c)
24969846590eSStephen M. Cameron 		return 0;
24979846590eSStephen M. Cameron 	(void) fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, scsi3addr, TYPE_CMD);
24989846590eSStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core(h, c);
24999846590eSStephen M. Cameron 	sense = c->err_info->SenseInfo;
25009846590eSStephen M. Cameron 	sense_key = sense[2];
25019846590eSStephen M. Cameron 	asc = sense[12];
25029846590eSStephen M. Cameron 	ascq = sense[13];
25039846590eSStephen M. Cameron 	cmd_status = c->err_info->CommandStatus;
25049846590eSStephen M. Cameron 	scsi_status = c->err_info->ScsiStatus;
25059846590eSStephen M. Cameron 	cmd_free(h, c);
25069846590eSStephen M. Cameron 	/* Is the volume 'not ready'? */
25079846590eSStephen M. Cameron 	if (cmd_status != CMD_TARGET_STATUS ||
25089846590eSStephen M. Cameron 		scsi_status != SAM_STAT_CHECK_CONDITION ||
25099846590eSStephen M. Cameron 		sense_key != NOT_READY ||
25109846590eSStephen M. Cameron 		asc != ASC_LUN_NOT_READY)  {
25119846590eSStephen M. Cameron 		return 0;
25129846590eSStephen M. Cameron 	}
25139846590eSStephen M. Cameron 
25149846590eSStephen M. Cameron 	/* Determine the reason for not ready state */
25159846590eSStephen M. Cameron 	ldstat = hpsa_get_volume_status(h, scsi3addr);
25169846590eSStephen M. Cameron 
25179846590eSStephen M. Cameron 	/* Keep volume offline in certain cases: */
25189846590eSStephen M. Cameron 	switch (ldstat) {
25199846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ERASE:
25209846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_RPI:
25219846590eSStephen M. Cameron 	case HPSA_LV_PENDING_RPI:
25229846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_NO_KEY:
25239846590eSStephen M. Cameron 	case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
25249846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION:
25259846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
25269846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
25279846590eSStephen M. Cameron 		return ldstat;
25289846590eSStephen M. Cameron 	case HPSA_VPD_LV_STATUS_UNSUPPORTED:
25299846590eSStephen M. Cameron 		/* If VPD status page isn't available,
25309846590eSStephen M. Cameron 		 * use ASC/ASCQ to determine state
25319846590eSStephen M. Cameron 		 */
25329846590eSStephen M. Cameron 		if ((ascq == ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS) ||
25339846590eSStephen M. Cameron 			(ascq == ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ))
25349846590eSStephen M. Cameron 			return ldstat;
25359846590eSStephen M. Cameron 		break;
25369846590eSStephen M. Cameron 	default:
25379846590eSStephen M. Cameron 		break;
25389846590eSStephen M. Cameron 	}
25399846590eSStephen M. Cameron 	return 0;
25409846590eSStephen M. Cameron }
25419846590eSStephen M. Cameron 
2542edd16368SStephen M. Cameron static int hpsa_update_device_info(struct ctlr_info *h,
25430b0e1d6cSStephen M. Cameron 	unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
25440b0e1d6cSStephen M. Cameron 	unsigned char *is_OBDR_device)
2545edd16368SStephen M. Cameron {
25460b0e1d6cSStephen M. Cameron 
25470b0e1d6cSStephen M. Cameron #define OBDR_SIG_OFFSET 43
25480b0e1d6cSStephen M. Cameron #define OBDR_TAPE_SIG "$DR-10"
25490b0e1d6cSStephen M. Cameron #define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
25500b0e1d6cSStephen M. Cameron #define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
25510b0e1d6cSStephen M. Cameron 
2552ea6d3bc3SStephen M. Cameron 	unsigned char *inq_buff;
25530b0e1d6cSStephen M. Cameron 	unsigned char *obdr_sig;
2554edd16368SStephen M. Cameron 
2555ea6d3bc3SStephen M. Cameron 	inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
2556edd16368SStephen M. Cameron 	if (!inq_buff)
2557edd16368SStephen M. Cameron 		goto bail_out;
2558edd16368SStephen M. Cameron 
2559edd16368SStephen M. Cameron 	/* Do an inquiry to the device to see what it is. */
2560edd16368SStephen M. Cameron 	if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
2561edd16368SStephen M. Cameron 		(unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
2562edd16368SStephen M. Cameron 		/* Inquiry failed (msg printed already) */
2563edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev,
2564edd16368SStephen M. Cameron 			"hpsa_update_device_info: inquiry failed\n");
2565edd16368SStephen M. Cameron 		goto bail_out;
2566edd16368SStephen M. Cameron 	}
2567edd16368SStephen M. Cameron 
2568edd16368SStephen M. Cameron 	this_device->devtype = (inq_buff[0] & 0x1f);
2569edd16368SStephen M. Cameron 	memcpy(this_device->scsi3addr, scsi3addr, 8);
2570edd16368SStephen M. Cameron 	memcpy(this_device->vendor, &inq_buff[8],
2571edd16368SStephen M. Cameron 		sizeof(this_device->vendor));
2572edd16368SStephen M. Cameron 	memcpy(this_device->model, &inq_buff[16],
2573edd16368SStephen M. Cameron 		sizeof(this_device->model));
2574edd16368SStephen M. Cameron 	memset(this_device->device_id, 0,
2575edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
2576edd16368SStephen M. Cameron 	hpsa_get_device_id(h, scsi3addr, this_device->device_id,
2577edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
2578edd16368SStephen M. Cameron 
2579edd16368SStephen M. Cameron 	if (this_device->devtype == TYPE_DISK &&
2580283b4a9bSStephen M. Cameron 		is_logical_dev_addr_mode(scsi3addr)) {
258167955ba3SStephen M. Cameron 		int volume_offline;
258267955ba3SStephen M. Cameron 
2583edd16368SStephen M. Cameron 		hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
2584283b4a9bSStephen M. Cameron 		if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
2585283b4a9bSStephen M. Cameron 			hpsa_get_ioaccel_status(h, scsi3addr, this_device);
258667955ba3SStephen M. Cameron 		volume_offline = hpsa_volume_offline(h, scsi3addr);
258767955ba3SStephen M. Cameron 		if (volume_offline < 0 || volume_offline > 0xff)
258867955ba3SStephen M. Cameron 			volume_offline = HPSA_VPD_LV_STATUS_UNSUPPORTED;
258967955ba3SStephen M. Cameron 		this_device->volume_offline = volume_offline & 0xff;
2590283b4a9bSStephen M. Cameron 	} else {
2591edd16368SStephen M. Cameron 		this_device->raid_level = RAID_UNKNOWN;
2592283b4a9bSStephen M. Cameron 		this_device->offload_config = 0;
2593283b4a9bSStephen M. Cameron 		this_device->offload_enabled = 0;
25949846590eSStephen M. Cameron 		this_device->volume_offline = 0;
2595283b4a9bSStephen M. Cameron 	}
2596edd16368SStephen M. Cameron 
25970b0e1d6cSStephen M. Cameron 	if (is_OBDR_device) {
25980b0e1d6cSStephen M. Cameron 		/* See if this is a One-Button-Disaster-Recovery device
25990b0e1d6cSStephen M. Cameron 		 * by looking for "$DR-10" at offset 43 in inquiry data.
26000b0e1d6cSStephen M. Cameron 		 */
26010b0e1d6cSStephen M. Cameron 		obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
26020b0e1d6cSStephen M. Cameron 		*is_OBDR_device = (this_device->devtype == TYPE_ROM &&
26030b0e1d6cSStephen M. Cameron 					strncmp(obdr_sig, OBDR_TAPE_SIG,
26040b0e1d6cSStephen M. Cameron 						OBDR_SIG_LEN) == 0);
26050b0e1d6cSStephen M. Cameron 	}
26060b0e1d6cSStephen M. Cameron 
2607edd16368SStephen M. Cameron 	kfree(inq_buff);
2608edd16368SStephen M. Cameron 	return 0;
2609edd16368SStephen M. Cameron 
2610edd16368SStephen M. Cameron bail_out:
2611edd16368SStephen M. Cameron 	kfree(inq_buff);
2612edd16368SStephen M. Cameron 	return 1;
2613edd16368SStephen M. Cameron }
2614edd16368SStephen M. Cameron 
26154f4eb9f1SScott Teel static unsigned char *ext_target_model[] = {
2616edd16368SStephen M. Cameron 	"MSA2012",
2617edd16368SStephen M. Cameron 	"MSA2024",
2618edd16368SStephen M. Cameron 	"MSA2312",
2619edd16368SStephen M. Cameron 	"MSA2324",
2620fda38518SStephen M. Cameron 	"P2000 G3 SAS",
2621e06c8e5cSStephen M. Cameron 	"MSA 2040 SAS",
2622edd16368SStephen M. Cameron 	NULL,
2623edd16368SStephen M. Cameron };
2624edd16368SStephen M. Cameron 
26254f4eb9f1SScott Teel static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
2626edd16368SStephen M. Cameron {
2627edd16368SStephen M. Cameron 	int i;
2628edd16368SStephen M. Cameron 
26294f4eb9f1SScott Teel 	for (i = 0; ext_target_model[i]; i++)
26304f4eb9f1SScott Teel 		if (strncmp(device->model, ext_target_model[i],
26314f4eb9f1SScott Teel 			strlen(ext_target_model[i])) == 0)
2632edd16368SStephen M. Cameron 			return 1;
2633edd16368SStephen M. Cameron 	return 0;
2634edd16368SStephen M. Cameron }
2635edd16368SStephen M. Cameron 
2636edd16368SStephen M. Cameron /* Helper function to assign bus, target, lun mapping of devices.
26374f4eb9f1SScott Teel  * Puts non-external target logical volumes on bus 0, external target logical
2638edd16368SStephen M. Cameron  * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
2639edd16368SStephen M. Cameron  * Logical drive target and lun are assigned at this time, but
2640edd16368SStephen M. Cameron  * physical device lun and target assignment are deferred (assigned
2641edd16368SStephen M. Cameron  * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
2642edd16368SStephen M. Cameron  */
2643edd16368SStephen M. Cameron static void figure_bus_target_lun(struct ctlr_info *h,
26441f310bdeSStephen M. Cameron 	u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
2645edd16368SStephen M. Cameron {
26461f310bdeSStephen M. Cameron 	u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
2647edd16368SStephen M. Cameron 
26481f310bdeSStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes)) {
26491f310bdeSStephen M. Cameron 		/* physical device, target and lun filled in later */
26501f310bdeSStephen M. Cameron 		if (is_hba_lunid(lunaddrbytes))
26511f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
26521f310bdeSStephen M. Cameron 		else
26531f310bdeSStephen M. Cameron 			/* defer target, lun assignment for physical devices */
26541f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 2, -1, -1);
26551f310bdeSStephen M. Cameron 		return;
26561f310bdeSStephen M. Cameron 	}
26571f310bdeSStephen M. Cameron 	/* It's a logical device */
26584f4eb9f1SScott Teel 	if (is_ext_target(h, device)) {
26594f4eb9f1SScott Teel 		/* external target way, put logicals on bus 1
2660339b2b14SStephen M. Cameron 		 * and match target/lun numbers box
26611f310bdeSStephen M. Cameron 		 * reports, other smart array, bus 0, target 0, match lunid
2662339b2b14SStephen M. Cameron 		 */
26631f310bdeSStephen M. Cameron 		hpsa_set_bus_target_lun(device,
26641f310bdeSStephen M. Cameron 			1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
26651f310bdeSStephen M. Cameron 		return;
2666339b2b14SStephen M. Cameron 	}
26671f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
2668edd16368SStephen M. Cameron }
2669edd16368SStephen M. Cameron 
2670edd16368SStephen M. Cameron /*
2671edd16368SStephen M. Cameron  * If there is no lun 0 on a target, linux won't find any devices.
26724f4eb9f1SScott Teel  * For the external targets (arrays), we have to manually detect the enclosure
2673edd16368SStephen M. Cameron  * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
2674edd16368SStephen M. Cameron  * it for some reason.  *tmpdevice is the target we're adding,
2675edd16368SStephen M. Cameron  * this_device is a pointer into the current element of currentsd[]
2676edd16368SStephen M. Cameron  * that we're building up in update_scsi_devices(), below.
2677edd16368SStephen M. Cameron  * lunzerobits is a bitmap that tracks which targets already have a
2678edd16368SStephen M. Cameron  * lun 0 assigned.
2679edd16368SStephen M. Cameron  * Returns 1 if an enclosure was added, 0 if not.
2680edd16368SStephen M. Cameron  */
26814f4eb9f1SScott Teel static int add_ext_target_dev(struct ctlr_info *h,
2682edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *tmpdevice,
268301a02ffcSStephen M. Cameron 	struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
26844f4eb9f1SScott Teel 	unsigned long lunzerobits[], int *n_ext_target_devs)
2685edd16368SStephen M. Cameron {
2686edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
2687edd16368SStephen M. Cameron 
26881f310bdeSStephen M. Cameron 	if (test_bit(tmpdevice->target, lunzerobits))
2689edd16368SStephen M. Cameron 		return 0; /* There is already a lun 0 on this target. */
2690edd16368SStephen M. Cameron 
2691edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes))
2692edd16368SStephen M. Cameron 		return 0; /* It's the logical targets that may lack lun 0. */
2693edd16368SStephen M. Cameron 
26944f4eb9f1SScott Teel 	if (!is_ext_target(h, tmpdevice))
26954f4eb9f1SScott Teel 		return 0; /* Only external target devices have this problem. */
2696edd16368SStephen M. Cameron 
26971f310bdeSStephen M. Cameron 	if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
2698edd16368SStephen M. Cameron 		return 0;
2699edd16368SStephen M. Cameron 
2700c4f8a299SStephen M. Cameron 	memset(scsi3addr, 0, 8);
27011f310bdeSStephen M. Cameron 	scsi3addr[3] = tmpdevice->target;
2702edd16368SStephen M. Cameron 	if (is_hba_lunid(scsi3addr))
2703edd16368SStephen M. Cameron 		return 0; /* Don't add the RAID controller here. */
2704edd16368SStephen M. Cameron 
2705339b2b14SStephen M. Cameron 	if (is_scsi_rev_5(h))
2706339b2b14SStephen M. Cameron 		return 0; /* p1210m doesn't need to do this. */
2707339b2b14SStephen M. Cameron 
27084f4eb9f1SScott Teel 	if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
2709aca4a520SScott Teel 		dev_warn(&h->pdev->dev, "Maximum number of external "
2710aca4a520SScott Teel 			"target devices exceeded.  Check your hardware "
2711edd16368SStephen M. Cameron 			"configuration.");
2712edd16368SStephen M. Cameron 		return 0;
2713edd16368SStephen M. Cameron 	}
2714edd16368SStephen M. Cameron 
27150b0e1d6cSStephen M. Cameron 	if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
2716edd16368SStephen M. Cameron 		return 0;
27174f4eb9f1SScott Teel 	(*n_ext_target_devs)++;
27181f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(this_device,
27191f310bdeSStephen M. Cameron 				tmpdevice->bus, tmpdevice->target, 0);
27201f310bdeSStephen M. Cameron 	set_bit(tmpdevice->target, lunzerobits);
2721edd16368SStephen M. Cameron 	return 1;
2722edd16368SStephen M. Cameron }
2723edd16368SStephen M. Cameron 
2724edd16368SStephen M. Cameron /*
272554b6e9e9SScott Teel  * Get address of physical disk used for an ioaccel2 mode command:
272654b6e9e9SScott Teel  *	1. Extract ioaccel2 handle from the command.
272754b6e9e9SScott Teel  *	2. Find a matching ioaccel2 handle from list of physical disks.
272854b6e9e9SScott Teel  *	3. Return:
272954b6e9e9SScott Teel  *		1 and set scsi3addr to address of matching physical
273054b6e9e9SScott Teel  *		0 if no matching physical disk was found.
273154b6e9e9SScott Teel  */
273254b6e9e9SScott Teel static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
273354b6e9e9SScott Teel 	struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
273454b6e9e9SScott Teel {
273554b6e9e9SScott Teel 	struct ReportExtendedLUNdata *physicals = NULL;
273654b6e9e9SScott Teel 	int responsesize = 24;	/* size of physical extended response */
273754b6e9e9SScott Teel 	int extended = 2;	/* flag forces reporting 'other dev info'. */
273854b6e9e9SScott Teel 	int reportsize = sizeof(*physicals) + HPSA_MAX_PHYS_LUN * responsesize;
273954b6e9e9SScott Teel 	u32 nphysicals = 0;	/* number of reported physical devs */
274054b6e9e9SScott Teel 	int found = 0;		/* found match (1) or not (0) */
274154b6e9e9SScott Teel 	u32 find;		/* handle we need to match */
274254b6e9e9SScott Teel 	int i;
274354b6e9e9SScott Teel 	struct scsi_cmnd *scmd;	/* scsi command within request being aborted */
274454b6e9e9SScott Teel 	struct hpsa_scsi_dev_t *d; /* device of request being aborted */
274554b6e9e9SScott Teel 	struct io_accel2_cmd *c2a; /* ioaccel2 command to abort */
27462b08b3e9SDon Brace 	__le32 it_nexus;	/* 4 byte device handle for the ioaccel2 cmd */
27472b08b3e9SDon Brace 	__le32 scsi_nexus;	/* 4 byte device handle for the ioaccel2 cmd */
274854b6e9e9SScott Teel 
274954b6e9e9SScott Teel 	if (ioaccel2_cmd_to_abort->cmd_type != CMD_IOACCEL2)
275054b6e9e9SScott Teel 		return 0; /* no match */
275154b6e9e9SScott Teel 
275254b6e9e9SScott Teel 	/* point to the ioaccel2 device handle */
275354b6e9e9SScott Teel 	c2a = &h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
275454b6e9e9SScott Teel 	if (c2a == NULL)
275554b6e9e9SScott Teel 		return 0; /* no match */
275654b6e9e9SScott Teel 
275754b6e9e9SScott Teel 	scmd = (struct scsi_cmnd *) ioaccel2_cmd_to_abort->scsi_cmd;
275854b6e9e9SScott Teel 	if (scmd == NULL)
275954b6e9e9SScott Teel 		return 0; /* no match */
276054b6e9e9SScott Teel 
276154b6e9e9SScott Teel 	d = scmd->device->hostdata;
276254b6e9e9SScott Teel 	if (d == NULL)
276354b6e9e9SScott Teel 		return 0; /* no match */
276454b6e9e9SScott Teel 
276550a0decfSStephen M. Cameron 	it_nexus = cpu_to_le32(d->ioaccel_handle);
27662b08b3e9SDon Brace 	scsi_nexus = c2a->scsi_nexus;
27672b08b3e9SDon Brace 	find = le32_to_cpu(c2a->scsi_nexus);
276854b6e9e9SScott Teel 
27692ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
27702ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
27712ba8bfc8SStephen 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",
27722ba8bfc8SStephen M. Cameron 			__func__, scsi_nexus,
27732ba8bfc8SStephen M. Cameron 			d->device_id[0], d->device_id[1], d->device_id[2],
27742ba8bfc8SStephen M. Cameron 			d->device_id[3], d->device_id[4], d->device_id[5],
27752ba8bfc8SStephen M. Cameron 			d->device_id[6], d->device_id[7], d->device_id[8],
27762ba8bfc8SStephen M. Cameron 			d->device_id[9], d->device_id[10], d->device_id[11],
27772ba8bfc8SStephen M. Cameron 			d->device_id[12], d->device_id[13], d->device_id[14],
27782ba8bfc8SStephen M. Cameron 			d->device_id[15]);
27792ba8bfc8SStephen M. Cameron 
278054b6e9e9SScott Teel 	/* Get the list of physical devices */
278154b6e9e9SScott Teel 	physicals = kzalloc(reportsize, GFP_KERNEL);
27823b51a7a3SJoe Handzik 	if (physicals == NULL)
27833b51a7a3SJoe Handzik 		return 0;
278454b6e9e9SScott Teel 	if (hpsa_scsi_do_report_phys_luns(h, (struct ReportLUNdata *) physicals,
278554b6e9e9SScott Teel 		reportsize, extended)) {
278654b6e9e9SScott Teel 		dev_err(&h->pdev->dev,
278754b6e9e9SScott Teel 			"Can't lookup %s device handle: report physical LUNs failed.\n",
278854b6e9e9SScott Teel 			"HP SSD Smart Path");
278954b6e9e9SScott Teel 		kfree(physicals);
279054b6e9e9SScott Teel 		return 0;
279154b6e9e9SScott Teel 	}
279254b6e9e9SScott Teel 	nphysicals = be32_to_cpu(*((__be32 *)physicals->LUNListLength)) /
279354b6e9e9SScott Teel 							responsesize;
279454b6e9e9SScott Teel 
279554b6e9e9SScott Teel 	/* find ioaccel2 handle in list of physicals: */
279654b6e9e9SScott Teel 	for (i = 0; i < nphysicals; i++) {
2797d5b5d964SStephen M. Cameron 		struct ext_report_lun_entry *entry = &physicals->LUN[i];
2798d5b5d964SStephen M. Cameron 
279954b6e9e9SScott Teel 		/* handle is in bytes 28-31 of each lun */
2800d5b5d964SStephen M. Cameron 		if (entry->ioaccel_handle != find)
280154b6e9e9SScott Teel 			continue; /* didn't match */
280254b6e9e9SScott Teel 		found = 1;
2803d5b5d964SStephen M. Cameron 		memcpy(scsi3addr, entry->lunid, 8);
28042ba8bfc8SStephen M. Cameron 		if (h->raid_offload_debug > 0)
28052ba8bfc8SStephen M. Cameron 			dev_info(&h->pdev->dev,
2806d5b5d964SStephen M. Cameron 				"%s: Searched h=0x%08x, Found h=0x%08x, scsiaddr 0x%8phN\n",
28072ba8bfc8SStephen M. Cameron 				__func__, find,
2808d5b5d964SStephen M. Cameron 				entry->ioaccel_handle, scsi3addr);
280954b6e9e9SScott Teel 		break; /* found it */
281054b6e9e9SScott Teel 	}
281154b6e9e9SScott Teel 
281254b6e9e9SScott Teel 	kfree(physicals);
281354b6e9e9SScott Teel 	if (found)
281454b6e9e9SScott Teel 		return 1;
281554b6e9e9SScott Teel 	else
281654b6e9e9SScott Teel 		return 0;
281754b6e9e9SScott Teel 
281854b6e9e9SScott Teel }
281954b6e9e9SScott Teel /*
2820edd16368SStephen M. Cameron  * Do CISS_REPORT_PHYS and CISS_REPORT_LOG.  Data is returned in physdev,
2821edd16368SStephen M. Cameron  * logdev.  The number of luns in physdev and logdev are returned in
2822edd16368SStephen M. Cameron  * *nphysicals and *nlogicals, respectively.
2823edd16368SStephen M. Cameron  * Returns 0 on success, -1 otherwise.
2824edd16368SStephen M. Cameron  */
2825edd16368SStephen M. Cameron static int hpsa_gather_lun_info(struct ctlr_info *h,
282692084715SStephen M. Cameron 	int reportphyslunsize, int reportloglunsize,
2827283b4a9bSStephen M. Cameron 	struct ReportLUNdata *physdev, u32 *nphysicals, int *physical_mode,
282801a02ffcSStephen M. Cameron 	struct ReportLUNdata *logdev, u32 *nlogicals)
2829edd16368SStephen M. Cameron {
2830283b4a9bSStephen M. Cameron 	int physical_entry_size = 8;
2831283b4a9bSStephen M. Cameron 
2832283b4a9bSStephen M. Cameron 	*physical_mode = 0;
2833283b4a9bSStephen M. Cameron 
2834283b4a9bSStephen M. Cameron 	/* For I/O accelerator mode we need to read physical device handles */
2835317d4adfSMike MIller 	if (h->transMethod & CFGTBL_Trans_io_accel1 ||
2836317d4adfSMike MIller 		h->transMethod & CFGTBL_Trans_io_accel2) {
2837283b4a9bSStephen M. Cameron 		*physical_mode = HPSA_REPORT_PHYS_EXTENDED;
2838283b4a9bSStephen M. Cameron 		physical_entry_size = 24;
2839283b4a9bSStephen M. Cameron 	}
284092084715SStephen M. Cameron 	if (hpsa_scsi_do_report_phys_luns(h, physdev, reportphyslunsize,
2841283b4a9bSStephen M. Cameron 							*physical_mode)) {
2842edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
2843edd16368SStephen M. Cameron 		return -1;
2844edd16368SStephen M. Cameron 	}
2845283b4a9bSStephen M. Cameron 	*nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) /
2846283b4a9bSStephen M. Cameron 							physical_entry_size;
2847edd16368SStephen M. Cameron 	if (*nphysicals > HPSA_MAX_PHYS_LUN) {
2848edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
2849edd16368SStephen M. Cameron 			"  %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2850edd16368SStephen M. Cameron 			*nphysicals - HPSA_MAX_PHYS_LUN);
2851edd16368SStephen M. Cameron 		*nphysicals = HPSA_MAX_PHYS_LUN;
2852edd16368SStephen M. Cameron 	}
285392084715SStephen M. Cameron 	if (hpsa_scsi_do_report_log_luns(h, logdev, reportloglunsize)) {
2854edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
2855edd16368SStephen M. Cameron 		return -1;
2856edd16368SStephen M. Cameron 	}
28576df1e954SStephen M. Cameron 	*nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
2858edd16368SStephen M. Cameron 	/* Reject Logicals in excess of our max capability. */
2859edd16368SStephen M. Cameron 	if (*nlogicals > HPSA_MAX_LUN) {
2860edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
2861edd16368SStephen M. Cameron 			"maximum logical LUNs (%d) exceeded.  "
2862edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_LUN,
2863edd16368SStephen M. Cameron 			*nlogicals - HPSA_MAX_LUN);
2864edd16368SStephen M. Cameron 			*nlogicals = HPSA_MAX_LUN;
2865edd16368SStephen M. Cameron 	}
2866edd16368SStephen M. Cameron 	if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
2867edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
2868edd16368SStephen M. Cameron 			"maximum logical + physical LUNs (%d) exceeded. "
2869edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2870edd16368SStephen M. Cameron 			*nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
2871edd16368SStephen M. Cameron 		*nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
2872edd16368SStephen M. Cameron 	}
2873edd16368SStephen M. Cameron 	return 0;
2874edd16368SStephen M. Cameron }
2875edd16368SStephen M. Cameron 
287642a91641SDon Brace static u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position,
287742a91641SDon Brace 	int i, int nphysicals, int nlogicals,
2878a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list,
2879339b2b14SStephen M. Cameron 	struct ReportLUNdata *logdev_list)
2880339b2b14SStephen M. Cameron {
2881339b2b14SStephen M. Cameron 	/* Helper function, figure out where the LUN ID info is coming from
2882339b2b14SStephen M. Cameron 	 * given index i, lists of physical and logical devices, where in
2883339b2b14SStephen M. Cameron 	 * the list the raid controller is supposed to appear (first or last)
2884339b2b14SStephen M. Cameron 	 */
2885339b2b14SStephen M. Cameron 
2886339b2b14SStephen M. Cameron 	int logicals_start = nphysicals + (raid_ctlr_position == 0);
2887339b2b14SStephen M. Cameron 	int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
2888339b2b14SStephen M. Cameron 
2889339b2b14SStephen M. Cameron 	if (i == raid_ctlr_position)
2890339b2b14SStephen M. Cameron 		return RAID_CTLR_LUNID;
2891339b2b14SStephen M. Cameron 
2892339b2b14SStephen M. Cameron 	if (i < logicals_start)
2893d5b5d964SStephen M. Cameron 		return &physdev_list->LUN[i -
2894d5b5d964SStephen M. Cameron 				(raid_ctlr_position == 0)].lunid[0];
2895339b2b14SStephen M. Cameron 
2896339b2b14SStephen M. Cameron 	if (i < last_device)
2897339b2b14SStephen M. Cameron 		return &logdev_list->LUN[i - nphysicals -
2898339b2b14SStephen M. Cameron 			(raid_ctlr_position == 0)][0];
2899339b2b14SStephen M. Cameron 	BUG();
2900339b2b14SStephen M. Cameron 	return NULL;
2901339b2b14SStephen M. Cameron }
2902339b2b14SStephen M. Cameron 
2903316b221aSStephen M. Cameron static int hpsa_hba_mode_enabled(struct ctlr_info *h)
2904316b221aSStephen M. Cameron {
2905316b221aSStephen M. Cameron 	int rc;
29066e8e8088SJoe Handzik 	int hba_mode_enabled;
2907316b221aSStephen M. Cameron 	struct bmic_controller_parameters *ctlr_params;
2908316b221aSStephen M. Cameron 	ctlr_params = kzalloc(sizeof(struct bmic_controller_parameters),
2909316b221aSStephen M. Cameron 		GFP_KERNEL);
2910316b221aSStephen M. Cameron 
2911316b221aSStephen M. Cameron 	if (!ctlr_params)
291296444fbbSJoe Handzik 		return -ENOMEM;
2913316b221aSStephen M. Cameron 	rc = hpsa_bmic_ctrl_mode_sense(h, RAID_CTLR_LUNID, 0, ctlr_params,
2914316b221aSStephen M. Cameron 		sizeof(struct bmic_controller_parameters));
291596444fbbSJoe Handzik 	if (rc) {
2916316b221aSStephen M. Cameron 		kfree(ctlr_params);
291796444fbbSJoe Handzik 		return rc;
2918316b221aSStephen M. Cameron 	}
29196e8e8088SJoe Handzik 
29206e8e8088SJoe Handzik 	hba_mode_enabled =
29216e8e8088SJoe Handzik 		((ctlr_params->nvram_flags & HBA_MODE_ENABLED_FLAG) != 0);
29226e8e8088SJoe Handzik 	kfree(ctlr_params);
29236e8e8088SJoe Handzik 	return hba_mode_enabled;
2924316b221aSStephen M. Cameron }
2925316b221aSStephen M. Cameron 
2926edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
2927edd16368SStephen M. Cameron {
2928edd16368SStephen M. Cameron 	/* the idea here is we could get notified
2929edd16368SStephen M. Cameron 	 * that some devices have changed, so we do a report
2930edd16368SStephen M. Cameron 	 * physical luns and report logical luns cmd, and adjust
2931edd16368SStephen M. Cameron 	 * our list of devices accordingly.
2932edd16368SStephen M. Cameron 	 *
2933edd16368SStephen M. Cameron 	 * The scsi3addr's of devices won't change so long as the
2934edd16368SStephen M. Cameron 	 * adapter is not reset.  That means we can rescan and
2935edd16368SStephen M. Cameron 	 * tell which devices we already know about, vs. new
2936edd16368SStephen M. Cameron 	 * devices, vs.  disappearing devices.
2937edd16368SStephen M. Cameron 	 */
2938a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list = NULL;
2939edd16368SStephen M. Cameron 	struct ReportLUNdata *logdev_list = NULL;
294001a02ffcSStephen M. Cameron 	u32 nphysicals = 0;
294101a02ffcSStephen M. Cameron 	u32 nlogicals = 0;
2942283b4a9bSStephen M. Cameron 	int physical_mode = 0;
294301a02ffcSStephen M. Cameron 	u32 ndev_allocated = 0;
2944edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
2945edd16368SStephen M. Cameron 	int ncurrent = 0;
29464f4eb9f1SScott Teel 	int i, n_ext_target_devs, ndevs_to_allocate;
2947339b2b14SStephen M. Cameron 	int raid_ctlr_position;
29482bbf5c7fSJoe Handzik 	int rescan_hba_mode;
2949aca4a520SScott Teel 	DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
2950edd16368SStephen M. Cameron 
2951cfe5badcSScott Teel 	currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
295292084715SStephen M. Cameron 	physdev_list = kzalloc(sizeof(*physdev_list), GFP_KERNEL);
295392084715SStephen M. Cameron 	logdev_list = kzalloc(sizeof(*logdev_list), GFP_KERNEL);
2954edd16368SStephen M. Cameron 	tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
2955edd16368SStephen M. Cameron 
29560b0e1d6cSStephen M. Cameron 	if (!currentsd || !physdev_list || !logdev_list || !tmpdevice) {
2957edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory\n");
2958edd16368SStephen M. Cameron 		goto out;
2959edd16368SStephen M. Cameron 	}
2960edd16368SStephen M. Cameron 	memset(lunzerobits, 0, sizeof(lunzerobits));
2961edd16368SStephen M. Cameron 
2962316b221aSStephen M. Cameron 	rescan_hba_mode = hpsa_hba_mode_enabled(h);
296396444fbbSJoe Handzik 	if (rescan_hba_mode < 0)
296496444fbbSJoe Handzik 		goto out;
2965316b221aSStephen M. Cameron 
2966316b221aSStephen M. Cameron 	if (!h->hba_mode_enabled && rescan_hba_mode)
2967316b221aSStephen M. Cameron 		dev_warn(&h->pdev->dev, "HBA mode enabled\n");
2968316b221aSStephen M. Cameron 	else if (h->hba_mode_enabled && !rescan_hba_mode)
2969316b221aSStephen M. Cameron 		dev_warn(&h->pdev->dev, "HBA mode disabled\n");
2970316b221aSStephen M. Cameron 
2971316b221aSStephen M. Cameron 	h->hba_mode_enabled = rescan_hba_mode;
2972316b221aSStephen M. Cameron 
297392084715SStephen M. Cameron 	if (hpsa_gather_lun_info(h,
297492084715SStephen M. Cameron 			sizeof(*physdev_list), sizeof(*logdev_list),
2975a93aa1feSMatt Gates 			(struct ReportLUNdata *) physdev_list, &nphysicals,
2976283b4a9bSStephen M. Cameron 			&physical_mode, logdev_list, &nlogicals))
2977edd16368SStephen M. Cameron 		goto out;
2978edd16368SStephen M. Cameron 
2979aca4a520SScott Teel 	/* We might see up to the maximum number of logical and physical disks
2980aca4a520SScott Teel 	 * plus external target devices, and a device for the local RAID
2981aca4a520SScott Teel 	 * controller.
2982edd16368SStephen M. Cameron 	 */
2983aca4a520SScott Teel 	ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
2984edd16368SStephen M. Cameron 
2985edd16368SStephen M. Cameron 	/* Allocate the per device structures */
2986edd16368SStephen M. Cameron 	for (i = 0; i < ndevs_to_allocate; i++) {
2987b7ec021fSScott Teel 		if (i >= HPSA_MAX_DEVICES) {
2988b7ec021fSScott Teel 			dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
2989b7ec021fSScott Teel 				"  %d devices ignored.\n", HPSA_MAX_DEVICES,
2990b7ec021fSScott Teel 				ndevs_to_allocate - HPSA_MAX_DEVICES);
2991b7ec021fSScott Teel 			break;
2992b7ec021fSScott Teel 		}
2993b7ec021fSScott Teel 
2994edd16368SStephen M. Cameron 		currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
2995edd16368SStephen M. Cameron 		if (!currentsd[i]) {
2996edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
2997edd16368SStephen M. Cameron 				__FILE__, __LINE__);
2998edd16368SStephen M. Cameron 			goto out;
2999edd16368SStephen M. Cameron 		}
3000edd16368SStephen M. Cameron 		ndev_allocated++;
3001edd16368SStephen M. Cameron 	}
3002edd16368SStephen M. Cameron 
30038645291bSStephen M. Cameron 	if (is_scsi_rev_5(h))
3004339b2b14SStephen M. Cameron 		raid_ctlr_position = 0;
3005339b2b14SStephen M. Cameron 	else
3006339b2b14SStephen M. Cameron 		raid_ctlr_position = nphysicals + nlogicals;
3007339b2b14SStephen M. Cameron 
3008edd16368SStephen M. Cameron 	/* adjust our table of devices */
30094f4eb9f1SScott Teel 	n_ext_target_devs = 0;
3010edd16368SStephen M. Cameron 	for (i = 0; i < nphysicals + nlogicals + 1; i++) {
30110b0e1d6cSStephen M. Cameron 		u8 *lunaddrbytes, is_OBDR = 0;
3012edd16368SStephen M. Cameron 
3013edd16368SStephen M. Cameron 		/* Figure out where the LUN ID info is coming from */
3014339b2b14SStephen M. Cameron 		lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
3015339b2b14SStephen M. Cameron 			i, nphysicals, nlogicals, physdev_list, logdev_list);
3016edd16368SStephen M. Cameron 		/* skip masked physical devices. */
3017339b2b14SStephen M. Cameron 		if (lunaddrbytes[3] & 0xC0 &&
3018339b2b14SStephen M. Cameron 			i < nphysicals + (raid_ctlr_position == 0))
3019edd16368SStephen M. Cameron 			continue;
3020edd16368SStephen M. Cameron 
3021edd16368SStephen M. Cameron 		/* Get device type, vendor, model, device id */
30220b0e1d6cSStephen M. Cameron 		if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
30230b0e1d6cSStephen M. Cameron 							&is_OBDR))
3024edd16368SStephen M. Cameron 			continue; /* skip it if we can't talk to it. */
30251f310bdeSStephen M. Cameron 		figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
3026edd16368SStephen M. Cameron 		this_device = currentsd[ncurrent];
3027edd16368SStephen M. Cameron 
3028edd16368SStephen M. Cameron 		/*
30294f4eb9f1SScott Teel 		 * For external target devices, we have to insert a LUN 0 which
3030edd16368SStephen M. Cameron 		 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
3031edd16368SStephen M. Cameron 		 * is nonetheless an enclosure device there.  We have to
3032edd16368SStephen M. Cameron 		 * present that otherwise linux won't find anything if
3033edd16368SStephen M. Cameron 		 * there is no lun 0.
3034edd16368SStephen M. Cameron 		 */
30354f4eb9f1SScott Teel 		if (add_ext_target_dev(h, tmpdevice, this_device,
30361f310bdeSStephen M. Cameron 				lunaddrbytes, lunzerobits,
30374f4eb9f1SScott Teel 				&n_ext_target_devs)) {
3038edd16368SStephen M. Cameron 			ncurrent++;
3039edd16368SStephen M. Cameron 			this_device = currentsd[ncurrent];
3040edd16368SStephen M. Cameron 		}
3041edd16368SStephen M. Cameron 
3042edd16368SStephen M. Cameron 		*this_device = *tmpdevice;
3043edd16368SStephen M. Cameron 
3044edd16368SStephen M. Cameron 		switch (this_device->devtype) {
30450b0e1d6cSStephen M. Cameron 		case TYPE_ROM:
3046edd16368SStephen M. Cameron 			/* We don't *really* support actual CD-ROM devices,
3047edd16368SStephen M. Cameron 			 * just "One Button Disaster Recovery" tape drive
3048edd16368SStephen M. Cameron 			 * which temporarily pretends to be a CD-ROM drive.
3049edd16368SStephen M. Cameron 			 * So we check that the device is really an OBDR tape
3050edd16368SStephen M. Cameron 			 * device by checking for "$DR-10" in bytes 43-48 of
3051edd16368SStephen M. Cameron 			 * the inquiry data.
3052edd16368SStephen M. Cameron 			 */
30530b0e1d6cSStephen M. Cameron 			if (is_OBDR)
3054edd16368SStephen M. Cameron 				ncurrent++;
3055edd16368SStephen M. Cameron 			break;
3056edd16368SStephen M. Cameron 		case TYPE_DISK:
3057316b221aSStephen M. Cameron 			if (h->hba_mode_enabled) {
3058316b221aSStephen M. Cameron 				/* never use raid mapper in HBA mode */
3059316b221aSStephen M. Cameron 				this_device->offload_enabled = 0;
3060316b221aSStephen M. Cameron 				ncurrent++;
3061316b221aSStephen M. Cameron 				break;
3062316b221aSStephen M. Cameron 			} else if (h->acciopath_status) {
3063283b4a9bSStephen M. Cameron 				if (i >= nphysicals) {
3064283b4a9bSStephen M. Cameron 					ncurrent++;
3065edd16368SStephen M. Cameron 					break;
3066283b4a9bSStephen M. Cameron 				}
3067316b221aSStephen M. Cameron 			} else {
3068316b221aSStephen M. Cameron 				if (i < nphysicals)
3069316b221aSStephen M. Cameron 					break;
3070316b221aSStephen M. Cameron 				ncurrent++;
3071316b221aSStephen M. Cameron 				break;
3072316b221aSStephen M. Cameron 			}
3073283b4a9bSStephen M. Cameron 			if (physical_mode == HPSA_REPORT_PHYS_EXTENDED) {
3074e1f7de0cSMatt Gates 				memcpy(&this_device->ioaccel_handle,
3075e1f7de0cSMatt Gates 					&lunaddrbytes[20],
3076e1f7de0cSMatt Gates 					sizeof(this_device->ioaccel_handle));
3077edd16368SStephen M. Cameron 				ncurrent++;
3078283b4a9bSStephen M. Cameron 			}
3079edd16368SStephen M. Cameron 			break;
3080edd16368SStephen M. Cameron 		case TYPE_TAPE:
3081edd16368SStephen M. Cameron 		case TYPE_MEDIUM_CHANGER:
3082edd16368SStephen M. Cameron 			ncurrent++;
3083edd16368SStephen M. Cameron 			break;
3084edd16368SStephen M. Cameron 		case TYPE_RAID:
3085edd16368SStephen M. Cameron 			/* Only present the Smartarray HBA as a RAID controller.
3086edd16368SStephen M. Cameron 			 * If it's a RAID controller other than the HBA itself
3087edd16368SStephen M. Cameron 			 * (an external RAID controller, MSA500 or similar)
3088edd16368SStephen M. Cameron 			 * don't present it.
3089edd16368SStephen M. Cameron 			 */
3090edd16368SStephen M. Cameron 			if (!is_hba_lunid(lunaddrbytes))
3091edd16368SStephen M. Cameron 				break;
3092edd16368SStephen M. Cameron 			ncurrent++;
3093edd16368SStephen M. Cameron 			break;
3094edd16368SStephen M. Cameron 		default:
3095edd16368SStephen M. Cameron 			break;
3096edd16368SStephen M. Cameron 		}
3097cfe5badcSScott Teel 		if (ncurrent >= HPSA_MAX_DEVICES)
3098edd16368SStephen M. Cameron 			break;
3099edd16368SStephen M. Cameron 	}
3100edd16368SStephen M. Cameron 	adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
3101edd16368SStephen M. Cameron out:
3102edd16368SStephen M. Cameron 	kfree(tmpdevice);
3103edd16368SStephen M. Cameron 	for (i = 0; i < ndev_allocated; i++)
3104edd16368SStephen M. Cameron 		kfree(currentsd[i]);
3105edd16368SStephen M. Cameron 	kfree(currentsd);
3106edd16368SStephen M. Cameron 	kfree(physdev_list);
3107edd16368SStephen M. Cameron 	kfree(logdev_list);
3108edd16368SStephen M. Cameron }
3109edd16368SStephen M. Cameron 
3110edd16368SStephen M. Cameron /* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
3111edd16368SStephen M. Cameron  * dma mapping  and fills in the scatter gather entries of the
3112edd16368SStephen M. Cameron  * hpsa command, cp.
3113edd16368SStephen M. Cameron  */
311433a2ffceSStephen M. Cameron static int hpsa_scatter_gather(struct ctlr_info *h,
3115edd16368SStephen M. Cameron 		struct CommandList *cp,
3116edd16368SStephen M. Cameron 		struct scsi_cmnd *cmd)
3117edd16368SStephen M. Cameron {
3118edd16368SStephen M. Cameron 	unsigned int len;
3119edd16368SStephen M. Cameron 	struct scatterlist *sg;
312001a02ffcSStephen M. Cameron 	u64 addr64;
312133a2ffceSStephen M. Cameron 	int use_sg, i, sg_index, chained;
312233a2ffceSStephen M. Cameron 	struct SGDescriptor *curr_sg;
3123edd16368SStephen M. Cameron 
312433a2ffceSStephen M. Cameron 	BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
3125edd16368SStephen M. Cameron 
3126edd16368SStephen M. Cameron 	use_sg = scsi_dma_map(cmd);
3127edd16368SStephen M. Cameron 	if (use_sg < 0)
3128edd16368SStephen M. Cameron 		return use_sg;
3129edd16368SStephen M. Cameron 
3130edd16368SStephen M. Cameron 	if (!use_sg)
3131edd16368SStephen M. Cameron 		goto sglist_finished;
3132edd16368SStephen M. Cameron 
313333a2ffceSStephen M. Cameron 	curr_sg = cp->SG;
313433a2ffceSStephen M. Cameron 	chained = 0;
313533a2ffceSStephen M. Cameron 	sg_index = 0;
3136edd16368SStephen M. Cameron 	scsi_for_each_sg(cmd, sg, use_sg, i) {
313733a2ffceSStephen M. Cameron 		if (i == h->max_cmd_sg_entries - 1 &&
313833a2ffceSStephen M. Cameron 			use_sg > h->max_cmd_sg_entries) {
313933a2ffceSStephen M. Cameron 			chained = 1;
314033a2ffceSStephen M. Cameron 			curr_sg = h->cmd_sg_list[cp->cmdindex];
314133a2ffceSStephen M. Cameron 			sg_index = 0;
314233a2ffceSStephen M. Cameron 		}
314301a02ffcSStephen M. Cameron 		addr64 = (u64) sg_dma_address(sg);
3144edd16368SStephen M. Cameron 		len  = sg_dma_len(sg);
314550a0decfSStephen M. Cameron 		curr_sg->Addr = cpu_to_le64(addr64);
314650a0decfSStephen M. Cameron 		curr_sg->Len = cpu_to_le32(len);
314750a0decfSStephen M. Cameron 		curr_sg->Ext = cpu_to_le32(0);
314833a2ffceSStephen M. Cameron 		curr_sg++;
314933a2ffceSStephen M. Cameron 	}
315050a0decfSStephen M. Cameron 	(--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
315133a2ffceSStephen M. Cameron 
315233a2ffceSStephen M. Cameron 	if (use_sg + chained > h->maxSG)
315333a2ffceSStephen M. Cameron 		h->maxSG = use_sg + chained;
315433a2ffceSStephen M. Cameron 
315533a2ffceSStephen M. Cameron 	if (chained) {
315633a2ffceSStephen M. Cameron 		cp->Header.SGList = h->max_cmd_sg_entries;
315750a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(use_sg + 1);
3158e2bea6dfSStephen M. Cameron 		if (hpsa_map_sg_chain_block(h, cp)) {
3159e2bea6dfSStephen M. Cameron 			scsi_dma_unmap(cmd);
3160e2bea6dfSStephen M. Cameron 			return -1;
3161e2bea6dfSStephen M. Cameron 		}
316233a2ffceSStephen M. Cameron 		return 0;
3163edd16368SStephen M. Cameron 	}
3164edd16368SStephen M. Cameron 
3165edd16368SStephen M. Cameron sglist_finished:
3166edd16368SStephen M. Cameron 
316701a02ffcSStephen M. Cameron 	cp->Header.SGList = (u8) use_sg;   /* no. SGs contig in this cmd */
316850a0decfSStephen M. Cameron 	cp->Header.SGTotal = cpu_to_le16(use_sg); /* total sgs in this cmd list */
3169edd16368SStephen M. Cameron 	return 0;
3170edd16368SStephen M. Cameron }
3171edd16368SStephen M. Cameron 
3172283b4a9bSStephen M. Cameron #define IO_ACCEL_INELIGIBLE (1)
3173283b4a9bSStephen M. Cameron static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
3174283b4a9bSStephen M. Cameron {
3175283b4a9bSStephen M. Cameron 	int is_write = 0;
3176283b4a9bSStephen M. Cameron 	u32 block;
3177283b4a9bSStephen M. Cameron 	u32 block_cnt;
3178283b4a9bSStephen M. Cameron 
3179283b4a9bSStephen M. Cameron 	/* Perform some CDB fixups if needed using 10 byte reads/writes only */
3180283b4a9bSStephen M. Cameron 	switch (cdb[0]) {
3181283b4a9bSStephen M. Cameron 	case WRITE_6:
3182283b4a9bSStephen M. Cameron 	case WRITE_12:
3183283b4a9bSStephen M. Cameron 		is_write = 1;
3184283b4a9bSStephen M. Cameron 	case READ_6:
3185283b4a9bSStephen M. Cameron 	case READ_12:
3186283b4a9bSStephen M. Cameron 		if (*cdb_len == 6) {
3187283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 8) | cdb[3];
3188283b4a9bSStephen M. Cameron 			block_cnt = cdb[4];
3189283b4a9bSStephen M. Cameron 		} else {
3190283b4a9bSStephen M. Cameron 			BUG_ON(*cdb_len != 12);
3191283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 24) |
3192283b4a9bSStephen M. Cameron 				(((u32) cdb[3]) << 16) |
3193283b4a9bSStephen M. Cameron 				(((u32) cdb[4]) << 8) |
3194283b4a9bSStephen M. Cameron 				cdb[5];
3195283b4a9bSStephen M. Cameron 			block_cnt =
3196283b4a9bSStephen M. Cameron 				(((u32) cdb[6]) << 24) |
3197283b4a9bSStephen M. Cameron 				(((u32) cdb[7]) << 16) |
3198283b4a9bSStephen M. Cameron 				(((u32) cdb[8]) << 8) |
3199283b4a9bSStephen M. Cameron 				cdb[9];
3200283b4a9bSStephen M. Cameron 		}
3201283b4a9bSStephen M. Cameron 		if (block_cnt > 0xffff)
3202283b4a9bSStephen M. Cameron 			return IO_ACCEL_INELIGIBLE;
3203283b4a9bSStephen M. Cameron 
3204283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
3205283b4a9bSStephen M. Cameron 		cdb[1] = 0;
3206283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (block >> 24);
3207283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (block >> 16);
3208283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (block >> 8);
3209283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (block);
3210283b4a9bSStephen M. Cameron 		cdb[6] = 0;
3211283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (block_cnt >> 8);
3212283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (block_cnt);
3213283b4a9bSStephen M. Cameron 		cdb[9] = 0;
3214283b4a9bSStephen M. Cameron 		*cdb_len = 10;
3215283b4a9bSStephen M. Cameron 		break;
3216283b4a9bSStephen M. Cameron 	}
3217283b4a9bSStephen M. Cameron 	return 0;
3218283b4a9bSStephen M. Cameron }
3219283b4a9bSStephen M. Cameron 
3220c349775eSScott Teel static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
3221283b4a9bSStephen M. Cameron 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
3222283b4a9bSStephen M. Cameron 	u8 *scsi3addr)
3223e1f7de0cSMatt Gates {
3224e1f7de0cSMatt Gates 	struct scsi_cmnd *cmd = c->scsi_cmd;
3225e1f7de0cSMatt Gates 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
3226e1f7de0cSMatt Gates 	unsigned int len;
3227e1f7de0cSMatt Gates 	unsigned int total_len = 0;
3228e1f7de0cSMatt Gates 	struct scatterlist *sg;
3229e1f7de0cSMatt Gates 	u64 addr64;
3230e1f7de0cSMatt Gates 	int use_sg, i;
3231e1f7de0cSMatt Gates 	struct SGDescriptor *curr_sg;
3232e1f7de0cSMatt Gates 	u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
3233e1f7de0cSMatt Gates 
3234283b4a9bSStephen M. Cameron 	/* TODO: implement chaining support */
3235283b4a9bSStephen M. Cameron 	if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
3236283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3237283b4a9bSStephen M. Cameron 
3238e1f7de0cSMatt Gates 	BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
3239e1f7de0cSMatt Gates 
3240283b4a9bSStephen M. Cameron 	if (fixup_ioaccel_cdb(cdb, &cdb_len))
3241283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3242283b4a9bSStephen M. Cameron 
3243e1f7de0cSMatt Gates 	c->cmd_type = CMD_IOACCEL1;
3244e1f7de0cSMatt Gates 
3245e1f7de0cSMatt Gates 	/* Adjust the DMA address to point to the accelerated command buffer */
3246e1f7de0cSMatt Gates 	c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
3247e1f7de0cSMatt Gates 				(c->cmdindex * sizeof(*cp));
3248e1f7de0cSMatt Gates 	BUG_ON(c->busaddr & 0x0000007F);
3249e1f7de0cSMatt Gates 
3250e1f7de0cSMatt Gates 	use_sg = scsi_dma_map(cmd);
3251e1f7de0cSMatt Gates 	if (use_sg < 0)
3252e1f7de0cSMatt Gates 		return use_sg;
3253e1f7de0cSMatt Gates 
3254e1f7de0cSMatt Gates 	if (use_sg) {
3255e1f7de0cSMatt Gates 		curr_sg = cp->SG;
3256e1f7de0cSMatt Gates 		scsi_for_each_sg(cmd, sg, use_sg, i) {
3257e1f7de0cSMatt Gates 			addr64 = (u64) sg_dma_address(sg);
3258e1f7de0cSMatt Gates 			len  = sg_dma_len(sg);
3259e1f7de0cSMatt Gates 			total_len += len;
326050a0decfSStephen M. Cameron 			curr_sg->Addr = cpu_to_le64(addr64);
326150a0decfSStephen M. Cameron 			curr_sg->Len = cpu_to_le32(len);
326250a0decfSStephen M. Cameron 			curr_sg->Ext = cpu_to_le32(0);
3263e1f7de0cSMatt Gates 			curr_sg++;
3264e1f7de0cSMatt Gates 		}
326550a0decfSStephen M. Cameron 		(--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
3266e1f7de0cSMatt Gates 
3267e1f7de0cSMatt Gates 		switch (cmd->sc_data_direction) {
3268e1f7de0cSMatt Gates 		case DMA_TO_DEVICE:
3269e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_OUT;
3270e1f7de0cSMatt Gates 			break;
3271e1f7de0cSMatt Gates 		case DMA_FROM_DEVICE:
3272e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_IN;
3273e1f7de0cSMatt Gates 			break;
3274e1f7de0cSMatt Gates 		case DMA_NONE:
3275e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_NODATAXFER;
3276e1f7de0cSMatt Gates 			break;
3277e1f7de0cSMatt Gates 		default:
3278e1f7de0cSMatt Gates 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3279e1f7de0cSMatt Gates 			cmd->sc_data_direction);
3280e1f7de0cSMatt Gates 			BUG();
3281e1f7de0cSMatt Gates 			break;
3282e1f7de0cSMatt Gates 		}
3283e1f7de0cSMatt Gates 	} else {
3284e1f7de0cSMatt Gates 		control |= IOACCEL1_CONTROL_NODATAXFER;
3285e1f7de0cSMatt Gates 	}
3286e1f7de0cSMatt Gates 
3287c349775eSScott Teel 	c->Header.SGList = use_sg;
3288e1f7de0cSMatt Gates 	/* Fill out the command structure to submit */
32892b08b3e9SDon Brace 	cp->dev_handle = cpu_to_le16(ioaccel_handle & 0xFFFF);
32902b08b3e9SDon Brace 	cp->transfer_len = cpu_to_le32(total_len);
32912b08b3e9SDon Brace 	cp->io_flags = cpu_to_le16(IOACCEL1_IOFLAGS_IO_REQ |
32922b08b3e9SDon Brace 			(cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK));
32932b08b3e9SDon Brace 	cp->control = cpu_to_le32(control);
3294283b4a9bSStephen M. Cameron 	memcpy(cp->CDB, cdb, cdb_len);
3295283b4a9bSStephen M. Cameron 	memcpy(cp->CISS_LUN, scsi3addr, 8);
3296c349775eSScott Teel 	/* Tag was already set at init time. */
3297e1f7de0cSMatt Gates 	enqueue_cmd_and_start_io(h, c);
3298e1f7de0cSMatt Gates 	return 0;
3299e1f7de0cSMatt Gates }
3300edd16368SStephen M. Cameron 
3301283b4a9bSStephen M. Cameron /*
3302283b4a9bSStephen M. Cameron  * Queue a command directly to a device behind the controller using the
3303283b4a9bSStephen M. Cameron  * I/O accelerator path.
3304283b4a9bSStephen M. Cameron  */
3305283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
3306283b4a9bSStephen M. Cameron 	struct CommandList *c)
3307283b4a9bSStephen M. Cameron {
3308283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
3309283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3310283b4a9bSStephen M. Cameron 
3311283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
3312283b4a9bSStephen M. Cameron 		cmd->cmnd, cmd->cmd_len, dev->scsi3addr);
3313283b4a9bSStephen M. Cameron }
3314283b4a9bSStephen M. Cameron 
3315dd0e19f3SScott Teel /*
3316dd0e19f3SScott Teel  * Set encryption parameters for the ioaccel2 request
3317dd0e19f3SScott Teel  */
3318dd0e19f3SScott Teel static void set_encrypt_ioaccel2(struct ctlr_info *h,
3319dd0e19f3SScott Teel 	struct CommandList *c, struct io_accel2_cmd *cp)
3320dd0e19f3SScott Teel {
3321dd0e19f3SScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
3322dd0e19f3SScott Teel 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3323dd0e19f3SScott Teel 	struct raid_map_data *map = &dev->raid_map;
3324dd0e19f3SScott Teel 	u64 first_block;
3325dd0e19f3SScott Teel 
3326dd0e19f3SScott Teel 	BUG_ON(!(dev->offload_config && dev->offload_enabled));
3327dd0e19f3SScott Teel 
3328dd0e19f3SScott Teel 	/* Are we doing encryption on this device */
33292b08b3e9SDon Brace 	if (!(le16_to_cpu(map->flags) & RAID_MAP_FLAG_ENCRYPT_ON))
3330dd0e19f3SScott Teel 		return;
3331dd0e19f3SScott Teel 	/* Set the data encryption key index. */
3332dd0e19f3SScott Teel 	cp->dekindex = map->dekindex;
3333dd0e19f3SScott Teel 
3334dd0e19f3SScott Teel 	/* Set the encryption enable flag, encoded into direction field. */
3335dd0e19f3SScott Teel 	cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
3336dd0e19f3SScott Teel 
3337dd0e19f3SScott Teel 	/* Set encryption tweak values based on logical block address
3338dd0e19f3SScott Teel 	 * If block size is 512, tweak value is LBA.
3339dd0e19f3SScott Teel 	 * For other block sizes, tweak is (LBA * block size)/ 512)
3340dd0e19f3SScott Teel 	 */
3341dd0e19f3SScott Teel 	switch (cmd->cmnd[0]) {
3342dd0e19f3SScott Teel 	/* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
3343dd0e19f3SScott Teel 	case WRITE_6:
3344dd0e19f3SScott Teel 	case READ_6:
33452b08b3e9SDon Brace 		first_block = get_unaligned_be16(&cmd->cmnd[2]);
3346dd0e19f3SScott Teel 		break;
3347dd0e19f3SScott Teel 	case WRITE_10:
3348dd0e19f3SScott Teel 	case READ_10:
3349dd0e19f3SScott Teel 	/* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
3350dd0e19f3SScott Teel 	case WRITE_12:
3351dd0e19f3SScott Teel 	case READ_12:
33522b08b3e9SDon Brace 		first_block = get_unaligned_be32(&cmd->cmnd[2]);
3353dd0e19f3SScott Teel 		break;
3354dd0e19f3SScott Teel 	case WRITE_16:
3355dd0e19f3SScott Teel 	case READ_16:
33562b08b3e9SDon Brace 		first_block = get_unaligned_be64(&cmd->cmnd[2]);
3357dd0e19f3SScott Teel 		break;
3358dd0e19f3SScott Teel 	default:
3359dd0e19f3SScott Teel 		dev_err(&h->pdev->dev,
33602b08b3e9SDon Brace 			"ERROR: %s: size (0x%x) not supported for encryption\n",
33612b08b3e9SDon Brace 			__func__, cmd->cmnd[0]);
3362dd0e19f3SScott Teel 		BUG();
3363dd0e19f3SScott Teel 		break;
3364dd0e19f3SScott Teel 	}
33652b08b3e9SDon Brace 
33662b08b3e9SDon Brace 	if (le32_to_cpu(map->volume_blk_size) != 512)
33672b08b3e9SDon Brace 		first_block = first_block *
33682b08b3e9SDon Brace 				le32_to_cpu(map->volume_blk_size)/512;
33692b08b3e9SDon Brace 
33702b08b3e9SDon Brace 	cp->tweak_lower = cpu_to_le32(first_block);
33712b08b3e9SDon Brace 	cp->tweak_upper = cpu_to_le32(first_block >> 32);
3372dd0e19f3SScott Teel }
3373dd0e19f3SScott Teel 
3374c349775eSScott Teel static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
3375c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
3376c349775eSScott Teel 	u8 *scsi3addr)
3377c349775eSScott Teel {
3378c349775eSScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
3379c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
3380c349775eSScott Teel 	struct ioaccel2_sg_element *curr_sg;
3381c349775eSScott Teel 	int use_sg, i;
3382c349775eSScott Teel 	struct scatterlist *sg;
3383c349775eSScott Teel 	u64 addr64;
3384c349775eSScott Teel 	u32 len;
3385c349775eSScott Teel 	u32 total_len = 0;
3386c349775eSScott Teel 
3387c349775eSScott Teel 	if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
3388c349775eSScott Teel 		return IO_ACCEL_INELIGIBLE;
3389c349775eSScott Teel 
3390c349775eSScott Teel 	if (fixup_ioaccel_cdb(cdb, &cdb_len))
3391c349775eSScott Teel 		return IO_ACCEL_INELIGIBLE;
3392c349775eSScott Teel 	c->cmd_type = CMD_IOACCEL2;
3393c349775eSScott Teel 	/* Adjust the DMA address to point to the accelerated command buffer */
3394c349775eSScott Teel 	c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
3395c349775eSScott Teel 				(c->cmdindex * sizeof(*cp));
3396c349775eSScott Teel 	BUG_ON(c->busaddr & 0x0000007F);
3397c349775eSScott Teel 
3398c349775eSScott Teel 	memset(cp, 0, sizeof(*cp));
3399c349775eSScott Teel 	cp->IU_type = IOACCEL2_IU_TYPE;
3400c349775eSScott Teel 
3401c349775eSScott Teel 	use_sg = scsi_dma_map(cmd);
3402c349775eSScott Teel 	if (use_sg < 0)
3403c349775eSScott Teel 		return use_sg;
3404c349775eSScott Teel 
3405c349775eSScott Teel 	if (use_sg) {
3406c349775eSScott Teel 		BUG_ON(use_sg > IOACCEL2_MAXSGENTRIES);
3407c349775eSScott Teel 		curr_sg = cp->sg;
3408c349775eSScott Teel 		scsi_for_each_sg(cmd, sg, use_sg, i) {
3409c349775eSScott Teel 			addr64 = (u64) sg_dma_address(sg);
3410c349775eSScott Teel 			len  = sg_dma_len(sg);
3411c349775eSScott Teel 			total_len += len;
3412c349775eSScott Teel 			curr_sg->address = cpu_to_le64(addr64);
3413c349775eSScott Teel 			curr_sg->length = cpu_to_le32(len);
3414c349775eSScott Teel 			curr_sg->reserved[0] = 0;
3415c349775eSScott Teel 			curr_sg->reserved[1] = 0;
3416c349775eSScott Teel 			curr_sg->reserved[2] = 0;
3417c349775eSScott Teel 			curr_sg->chain_indicator = 0;
3418c349775eSScott Teel 			curr_sg++;
3419c349775eSScott Teel 		}
3420c349775eSScott Teel 
3421c349775eSScott Teel 		switch (cmd->sc_data_direction) {
3422c349775eSScott Teel 		case DMA_TO_DEVICE:
3423dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3424dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_OUT;
3425c349775eSScott Teel 			break;
3426c349775eSScott Teel 		case DMA_FROM_DEVICE:
3427dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3428dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_IN;
3429c349775eSScott Teel 			break;
3430c349775eSScott Teel 		case DMA_NONE:
3431dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3432dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_NO_DATA;
3433c349775eSScott Teel 			break;
3434c349775eSScott Teel 		default:
3435c349775eSScott Teel 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3436c349775eSScott Teel 				cmd->sc_data_direction);
3437c349775eSScott Teel 			BUG();
3438c349775eSScott Teel 			break;
3439c349775eSScott Teel 		}
3440c349775eSScott Teel 	} else {
3441dd0e19f3SScott Teel 		cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3442dd0e19f3SScott Teel 		cp->direction |= IOACCEL2_DIR_NO_DATA;
3443c349775eSScott Teel 	}
3444dd0e19f3SScott Teel 
3445dd0e19f3SScott Teel 	/* Set encryption parameters, if necessary */
3446dd0e19f3SScott Teel 	set_encrypt_ioaccel2(h, c, cp);
3447dd0e19f3SScott Teel 
34482b08b3e9SDon Brace 	cp->scsi_nexus = cpu_to_le32(ioaccel_handle);
34492b08b3e9SDon Brace 	cp->Tag = cpu_to_le32(c->cmdindex << DIRECT_LOOKUP_SHIFT |
34502b08b3e9SDon Brace 				DIRECT_LOOKUP_BIT);
3451c349775eSScott Teel 	memcpy(cp->cdb, cdb, sizeof(cp->cdb));
3452c349775eSScott Teel 
3453c349775eSScott Teel 	/* fill in sg elements */
3454c349775eSScott Teel 	cp->sg_count = (u8) use_sg;
3455c349775eSScott Teel 
3456c349775eSScott Teel 	cp->data_len = cpu_to_le32(total_len);
3457c349775eSScott Teel 	cp->err_ptr = cpu_to_le64(c->busaddr +
3458c349775eSScott Teel 			offsetof(struct io_accel2_cmd, error_data));
345950a0decfSStephen M. Cameron 	cp->err_len = cpu_to_le32(sizeof(cp->error_data));
3460c349775eSScott Teel 
3461c349775eSScott Teel 	enqueue_cmd_and_start_io(h, c);
3462c349775eSScott Teel 	return 0;
3463c349775eSScott Teel }
3464c349775eSScott Teel 
3465c349775eSScott Teel /*
3466c349775eSScott Teel  * Queue a command to the correct I/O accelerator path.
3467c349775eSScott Teel  */
3468c349775eSScott Teel static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
3469c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
3470c349775eSScott Teel 	u8 *scsi3addr)
3471c349775eSScott Teel {
3472c349775eSScott Teel 	if (h->transMethod & CFGTBL_Trans_io_accel1)
3473c349775eSScott Teel 		return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
3474c349775eSScott Teel 						cdb, cdb_len, scsi3addr);
3475c349775eSScott Teel 	else
3476c349775eSScott Teel 		return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
3477c349775eSScott Teel 						cdb, cdb_len, scsi3addr);
3478c349775eSScott Teel }
3479c349775eSScott Teel 
34806b80b18fSScott Teel static void raid_map_helper(struct raid_map_data *map,
34816b80b18fSScott Teel 		int offload_to_mirror, u32 *map_index, u32 *current_group)
34826b80b18fSScott Teel {
34836b80b18fSScott Teel 	if (offload_to_mirror == 0)  {
34846b80b18fSScott Teel 		/* use physical disk in the first mirrored group. */
34852b08b3e9SDon Brace 		*map_index %= le16_to_cpu(map->data_disks_per_row);
34866b80b18fSScott Teel 		return;
34876b80b18fSScott Teel 	}
34886b80b18fSScott Teel 	do {
34896b80b18fSScott Teel 		/* determine mirror group that *map_index indicates */
34902b08b3e9SDon Brace 		*current_group = *map_index /
34912b08b3e9SDon Brace 			le16_to_cpu(map->data_disks_per_row);
34926b80b18fSScott Teel 		if (offload_to_mirror == *current_group)
34936b80b18fSScott Teel 			continue;
34942b08b3e9SDon Brace 		if (*current_group < le16_to_cpu(map->layout_map_count) - 1) {
34956b80b18fSScott Teel 			/* select map index from next group */
34962b08b3e9SDon Brace 			*map_index += le16_to_cpu(map->data_disks_per_row);
34976b80b18fSScott Teel 			(*current_group)++;
34986b80b18fSScott Teel 		} else {
34996b80b18fSScott Teel 			/* select map index from first group */
35002b08b3e9SDon Brace 			*map_index %= le16_to_cpu(map->data_disks_per_row);
35016b80b18fSScott Teel 			*current_group = 0;
35026b80b18fSScott Teel 		}
35036b80b18fSScott Teel 	} while (offload_to_mirror != *current_group);
35046b80b18fSScott Teel }
35056b80b18fSScott Teel 
3506283b4a9bSStephen M. Cameron /*
3507283b4a9bSStephen M. Cameron  * Attempt to perform offload RAID mapping for a logical volume I/O.
3508283b4a9bSStephen M. Cameron  */
3509283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
3510283b4a9bSStephen M. Cameron 	struct CommandList *c)
3511283b4a9bSStephen M. Cameron {
3512283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
3513283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3514283b4a9bSStephen M. Cameron 	struct raid_map_data *map = &dev->raid_map;
3515283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map->data[0];
3516283b4a9bSStephen M. Cameron 	int is_write = 0;
3517283b4a9bSStephen M. Cameron 	u32 map_index;
3518283b4a9bSStephen M. Cameron 	u64 first_block, last_block;
3519283b4a9bSStephen M. Cameron 	u32 block_cnt;
3520283b4a9bSStephen M. Cameron 	u32 blocks_per_row;
3521283b4a9bSStephen M. Cameron 	u64 first_row, last_row;
3522283b4a9bSStephen M. Cameron 	u32 first_row_offset, last_row_offset;
3523283b4a9bSStephen M. Cameron 	u32 first_column, last_column;
35246b80b18fSScott Teel 	u64 r0_first_row, r0_last_row;
35256b80b18fSScott Teel 	u32 r5or6_blocks_per_row;
35266b80b18fSScott Teel 	u64 r5or6_first_row, r5or6_last_row;
35276b80b18fSScott Teel 	u32 r5or6_first_row_offset, r5or6_last_row_offset;
35286b80b18fSScott Teel 	u32 r5or6_first_column, r5or6_last_column;
35296b80b18fSScott Teel 	u32 total_disks_per_row;
35306b80b18fSScott Teel 	u32 stripesize;
35316b80b18fSScott Teel 	u32 first_group, last_group, current_group;
3532283b4a9bSStephen M. Cameron 	u32 map_row;
3533283b4a9bSStephen M. Cameron 	u32 disk_handle;
3534283b4a9bSStephen M. Cameron 	u64 disk_block;
3535283b4a9bSStephen M. Cameron 	u32 disk_block_cnt;
3536283b4a9bSStephen M. Cameron 	u8 cdb[16];
3537283b4a9bSStephen M. Cameron 	u8 cdb_len;
35382b08b3e9SDon Brace 	u16 strip_size;
3539283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
3540283b4a9bSStephen M. Cameron 	u64 tmpdiv;
3541283b4a9bSStephen M. Cameron #endif
35426b80b18fSScott Teel 	int offload_to_mirror;
3543283b4a9bSStephen M. Cameron 
3544283b4a9bSStephen M. Cameron 	BUG_ON(!(dev->offload_config && dev->offload_enabled));
3545283b4a9bSStephen M. Cameron 
3546283b4a9bSStephen M. Cameron 	/* check for valid opcode, get LBA and block count */
3547283b4a9bSStephen M. Cameron 	switch (cmd->cmnd[0]) {
3548283b4a9bSStephen M. Cameron 	case WRITE_6:
3549283b4a9bSStephen M. Cameron 		is_write = 1;
3550283b4a9bSStephen M. Cameron 	case READ_6:
3551283b4a9bSStephen M. Cameron 		first_block =
3552283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 8) |
3553283b4a9bSStephen M. Cameron 			cmd->cmnd[3];
3554283b4a9bSStephen M. Cameron 		block_cnt = cmd->cmnd[4];
35553fa89a04SStephen M. Cameron 		if (block_cnt == 0)
35563fa89a04SStephen M. Cameron 			block_cnt = 256;
3557283b4a9bSStephen M. Cameron 		break;
3558283b4a9bSStephen M. Cameron 	case WRITE_10:
3559283b4a9bSStephen M. Cameron 		is_write = 1;
3560283b4a9bSStephen M. Cameron 	case READ_10:
3561283b4a9bSStephen M. Cameron 		first_block =
3562283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
3563283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
3564283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
3565283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
3566283b4a9bSStephen M. Cameron 		block_cnt =
3567283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 8) |
3568283b4a9bSStephen M. Cameron 			cmd->cmnd[8];
3569283b4a9bSStephen M. Cameron 		break;
3570283b4a9bSStephen M. Cameron 	case WRITE_12:
3571283b4a9bSStephen M. Cameron 		is_write = 1;
3572283b4a9bSStephen M. Cameron 	case READ_12:
3573283b4a9bSStephen M. Cameron 		first_block =
3574283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
3575283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
3576283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
3577283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
3578283b4a9bSStephen M. Cameron 		block_cnt =
3579283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[6]) << 24) |
3580283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 16) |
3581283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[8]) << 8) |
3582283b4a9bSStephen M. Cameron 		cmd->cmnd[9];
3583283b4a9bSStephen M. Cameron 		break;
3584283b4a9bSStephen M. Cameron 	case WRITE_16:
3585283b4a9bSStephen M. Cameron 		is_write = 1;
3586283b4a9bSStephen M. Cameron 	case READ_16:
3587283b4a9bSStephen M. Cameron 		first_block =
3588283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 56) |
3589283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 48) |
3590283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 40) |
3591283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[5]) << 32) |
3592283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[6]) << 24) |
3593283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[7]) << 16) |
3594283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[8]) << 8) |
3595283b4a9bSStephen M. Cameron 			cmd->cmnd[9];
3596283b4a9bSStephen M. Cameron 		block_cnt =
3597283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[10]) << 24) |
3598283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[11]) << 16) |
3599283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[12]) << 8) |
3600283b4a9bSStephen M. Cameron 			cmd->cmnd[13];
3601283b4a9bSStephen M. Cameron 		break;
3602283b4a9bSStephen M. Cameron 	default:
3603283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
3604283b4a9bSStephen M. Cameron 	}
3605283b4a9bSStephen M. Cameron 	last_block = first_block + block_cnt - 1;
3606283b4a9bSStephen M. Cameron 
3607283b4a9bSStephen M. Cameron 	/* check for write to non-RAID-0 */
3608283b4a9bSStephen M. Cameron 	if (is_write && dev->raid_level != 0)
3609283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3610283b4a9bSStephen M. Cameron 
3611283b4a9bSStephen M. Cameron 	/* check for invalid block or wraparound */
36122b08b3e9SDon Brace 	if (last_block >= le64_to_cpu(map->volume_blk_cnt) ||
36132b08b3e9SDon Brace 		last_block < first_block)
3614283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3615283b4a9bSStephen M. Cameron 
3616283b4a9bSStephen M. Cameron 	/* calculate stripe information for the request */
36172b08b3e9SDon Brace 	blocks_per_row = le16_to_cpu(map->data_disks_per_row) *
36182b08b3e9SDon Brace 				le16_to_cpu(map->strip_size);
36192b08b3e9SDon Brace 	strip_size = le16_to_cpu(map->strip_size);
3620283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
3621283b4a9bSStephen M. Cameron 	tmpdiv = first_block;
3622283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
3623283b4a9bSStephen M. Cameron 	first_row = tmpdiv;
3624283b4a9bSStephen M. Cameron 	tmpdiv = last_block;
3625283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
3626283b4a9bSStephen M. Cameron 	last_row = tmpdiv;
3627283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
3628283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
3629283b4a9bSStephen M. Cameron 	tmpdiv = first_row_offset;
36302b08b3e9SDon Brace 	(void) do_div(tmpdiv, strip_size);
3631283b4a9bSStephen M. Cameron 	first_column = tmpdiv;
3632283b4a9bSStephen M. Cameron 	tmpdiv = last_row_offset;
36332b08b3e9SDon Brace 	(void) do_div(tmpdiv, strip_size);
3634283b4a9bSStephen M. Cameron 	last_column = tmpdiv;
3635283b4a9bSStephen M. Cameron #else
3636283b4a9bSStephen M. Cameron 	first_row = first_block / blocks_per_row;
3637283b4a9bSStephen M. Cameron 	last_row = last_block / blocks_per_row;
3638283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
3639283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
36402b08b3e9SDon Brace 	first_column = first_row_offset / strip_size;
36412b08b3e9SDon Brace 	last_column = last_row_offset / strip_size;
3642283b4a9bSStephen M. Cameron #endif
3643283b4a9bSStephen M. Cameron 
3644283b4a9bSStephen M. Cameron 	/* if this isn't a single row/column then give to the controller */
3645283b4a9bSStephen M. Cameron 	if ((first_row != last_row) || (first_column != last_column))
3646283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3647283b4a9bSStephen M. Cameron 
3648283b4a9bSStephen M. Cameron 	/* proceeding with driver mapping */
36492b08b3e9SDon Brace 	total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
36502b08b3e9SDon Brace 				le16_to_cpu(map->metadata_disks_per_row);
3651283b4a9bSStephen M. Cameron 	map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
36522b08b3e9SDon Brace 				le16_to_cpu(map->row_cnt);
36536b80b18fSScott Teel 	map_index = (map_row * total_disks_per_row) + first_column;
36546b80b18fSScott Teel 
36556b80b18fSScott Teel 	switch (dev->raid_level) {
36566b80b18fSScott Teel 	case HPSA_RAID_0:
36576b80b18fSScott Teel 		break; /* nothing special to do */
36586b80b18fSScott Teel 	case HPSA_RAID_1:
36596b80b18fSScott Teel 		/* Handles load balance across RAID 1 members.
36606b80b18fSScott Teel 		 * (2-drive R1 and R10 with even # of drives.)
36616b80b18fSScott Teel 		 * Appropriate for SSDs, not optimal for HDDs
3662283b4a9bSStephen M. Cameron 		 */
36632b08b3e9SDon Brace 		BUG_ON(le16_to_cpu(map->layout_map_count) != 2);
3664283b4a9bSStephen M. Cameron 		if (dev->offload_to_mirror)
36652b08b3e9SDon Brace 			map_index += le16_to_cpu(map->data_disks_per_row);
3666283b4a9bSStephen M. Cameron 		dev->offload_to_mirror = !dev->offload_to_mirror;
36676b80b18fSScott Teel 		break;
36686b80b18fSScott Teel 	case HPSA_RAID_ADM:
36696b80b18fSScott Teel 		/* Handles N-way mirrors  (R1-ADM)
36706b80b18fSScott Teel 		 * and R10 with # of drives divisible by 3.)
36716b80b18fSScott Teel 		 */
36722b08b3e9SDon Brace 		BUG_ON(le16_to_cpu(map->layout_map_count) != 3);
36736b80b18fSScott Teel 
36746b80b18fSScott Teel 		offload_to_mirror = dev->offload_to_mirror;
36756b80b18fSScott Teel 		raid_map_helper(map, offload_to_mirror,
36766b80b18fSScott Teel 				&map_index, &current_group);
36776b80b18fSScott Teel 		/* set mirror group to use next time */
36786b80b18fSScott Teel 		offload_to_mirror =
36792b08b3e9SDon Brace 			(offload_to_mirror >=
36802b08b3e9SDon Brace 			le16_to_cpu(map->layout_map_count) - 1)
36816b80b18fSScott Teel 			? 0 : offload_to_mirror + 1;
36826b80b18fSScott Teel 		dev->offload_to_mirror = offload_to_mirror;
36836b80b18fSScott Teel 		/* Avoid direct use of dev->offload_to_mirror within this
36846b80b18fSScott Teel 		 * function since multiple threads might simultaneously
36856b80b18fSScott Teel 		 * increment it beyond the range of dev->layout_map_count -1.
36866b80b18fSScott Teel 		 */
36876b80b18fSScott Teel 		break;
36886b80b18fSScott Teel 	case HPSA_RAID_5:
36896b80b18fSScott Teel 	case HPSA_RAID_6:
36902b08b3e9SDon Brace 		if (le16_to_cpu(map->layout_map_count) <= 1)
36916b80b18fSScott Teel 			break;
36926b80b18fSScott Teel 
36936b80b18fSScott Teel 		/* Verify first and last block are in same RAID group */
36946b80b18fSScott Teel 		r5or6_blocks_per_row =
36952b08b3e9SDon Brace 			le16_to_cpu(map->strip_size) *
36962b08b3e9SDon Brace 			le16_to_cpu(map->data_disks_per_row);
36976b80b18fSScott Teel 		BUG_ON(r5or6_blocks_per_row == 0);
36982b08b3e9SDon Brace 		stripesize = r5or6_blocks_per_row *
36992b08b3e9SDon Brace 			le16_to_cpu(map->layout_map_count);
37006b80b18fSScott Teel #if BITS_PER_LONG == 32
37016b80b18fSScott Teel 		tmpdiv = first_block;
37026b80b18fSScott Teel 		first_group = do_div(tmpdiv, stripesize);
37036b80b18fSScott Teel 		tmpdiv = first_group;
37046b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
37056b80b18fSScott Teel 		first_group = tmpdiv;
37066b80b18fSScott Teel 		tmpdiv = last_block;
37076b80b18fSScott Teel 		last_group = do_div(tmpdiv, stripesize);
37086b80b18fSScott Teel 		tmpdiv = last_group;
37096b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
37106b80b18fSScott Teel 		last_group = tmpdiv;
37116b80b18fSScott Teel #else
37126b80b18fSScott Teel 		first_group = (first_block % stripesize) / r5or6_blocks_per_row;
37136b80b18fSScott Teel 		last_group = (last_block % stripesize) / r5or6_blocks_per_row;
37146b80b18fSScott Teel #endif
3715000ff7c2SStephen M. Cameron 		if (first_group != last_group)
37166b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
37176b80b18fSScott Teel 
37186b80b18fSScott Teel 		/* Verify request is in a single row of RAID 5/6 */
37196b80b18fSScott Teel #if BITS_PER_LONG == 32
37206b80b18fSScott Teel 		tmpdiv = first_block;
37216b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
37226b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row = tmpdiv;
37236b80b18fSScott Teel 		tmpdiv = last_block;
37246b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
37256b80b18fSScott Teel 		r5or6_last_row = r0_last_row = tmpdiv;
37266b80b18fSScott Teel #else
37276b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row =
37286b80b18fSScott Teel 						first_block / stripesize;
37296b80b18fSScott Teel 		r5or6_last_row = r0_last_row = last_block / stripesize;
37306b80b18fSScott Teel #endif
37316b80b18fSScott Teel 		if (r5or6_first_row != r5or6_last_row)
37326b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
37336b80b18fSScott Teel 
37346b80b18fSScott Teel 
37356b80b18fSScott Teel 		/* Verify request is in a single column */
37366b80b18fSScott Teel #if BITS_PER_LONG == 32
37376b80b18fSScott Teel 		tmpdiv = first_block;
37386b80b18fSScott Teel 		first_row_offset = do_div(tmpdiv, stripesize);
37396b80b18fSScott Teel 		tmpdiv = first_row_offset;
37406b80b18fSScott Teel 		first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
37416b80b18fSScott Teel 		r5or6_first_row_offset = first_row_offset;
37426b80b18fSScott Teel 		tmpdiv = last_block;
37436b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, stripesize);
37446b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
37456b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
37466b80b18fSScott Teel 		tmpdiv = r5or6_first_row_offset;
37476b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
37486b80b18fSScott Teel 		first_column = r5or6_first_column = tmpdiv;
37496b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
37506b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
37516b80b18fSScott Teel 		r5or6_last_column = tmpdiv;
37526b80b18fSScott Teel #else
37536b80b18fSScott Teel 		first_row_offset = r5or6_first_row_offset =
37546b80b18fSScott Teel 			(u32)((first_block % stripesize) %
37556b80b18fSScott Teel 						r5or6_blocks_per_row);
37566b80b18fSScott Teel 
37576b80b18fSScott Teel 		r5or6_last_row_offset =
37586b80b18fSScott Teel 			(u32)((last_block % stripesize) %
37596b80b18fSScott Teel 						r5or6_blocks_per_row);
37606b80b18fSScott Teel 
37616b80b18fSScott Teel 		first_column = r5or6_first_column =
37622b08b3e9SDon Brace 			r5or6_first_row_offset / le16_to_cpu(map->strip_size);
37636b80b18fSScott Teel 		r5or6_last_column =
37642b08b3e9SDon Brace 			r5or6_last_row_offset / le16_to_cpu(map->strip_size);
37656b80b18fSScott Teel #endif
37666b80b18fSScott Teel 		if (r5or6_first_column != r5or6_last_column)
37676b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
37686b80b18fSScott Teel 
37696b80b18fSScott Teel 		/* Request is eligible */
37706b80b18fSScott Teel 		map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
37712b08b3e9SDon Brace 			le16_to_cpu(map->row_cnt);
37726b80b18fSScott Teel 
37736b80b18fSScott Teel 		map_index = (first_group *
37742b08b3e9SDon Brace 			(le16_to_cpu(map->row_cnt) * total_disks_per_row)) +
37756b80b18fSScott Teel 			(map_row * total_disks_per_row) + first_column;
37766b80b18fSScott Teel 		break;
37776b80b18fSScott Teel 	default:
37786b80b18fSScott Teel 		return IO_ACCEL_INELIGIBLE;
3779283b4a9bSStephen M. Cameron 	}
37806b80b18fSScott Teel 
3781283b4a9bSStephen M. Cameron 	disk_handle = dd[map_index].ioaccel_handle;
37822b08b3e9SDon Brace 	disk_block = le64_to_cpu(map->disk_starting_blk) +
37832b08b3e9SDon Brace 			first_row * le16_to_cpu(map->strip_size) +
37842b08b3e9SDon Brace 			(first_row_offset - first_column *
37852b08b3e9SDon Brace 			le16_to_cpu(map->strip_size));
3786283b4a9bSStephen M. Cameron 	disk_block_cnt = block_cnt;
3787283b4a9bSStephen M. Cameron 
3788283b4a9bSStephen M. Cameron 	/* handle differing logical/physical block sizes */
3789283b4a9bSStephen M. Cameron 	if (map->phys_blk_shift) {
3790283b4a9bSStephen M. Cameron 		disk_block <<= map->phys_blk_shift;
3791283b4a9bSStephen M. Cameron 		disk_block_cnt <<= map->phys_blk_shift;
3792283b4a9bSStephen M. Cameron 	}
3793283b4a9bSStephen M. Cameron 	BUG_ON(disk_block_cnt > 0xffff);
3794283b4a9bSStephen M. Cameron 
3795283b4a9bSStephen M. Cameron 	/* build the new CDB for the physical disk I/O */
3796283b4a9bSStephen M. Cameron 	if (disk_block > 0xffffffff) {
3797283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_16 : READ_16;
3798283b4a9bSStephen M. Cameron 		cdb[1] = 0;
3799283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 56);
3800283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 48);
3801283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 40);
3802283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block >> 32);
3803283b4a9bSStephen M. Cameron 		cdb[6] = (u8) (disk_block >> 24);
3804283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block >> 16);
3805283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block >> 8);
3806283b4a9bSStephen M. Cameron 		cdb[9] = (u8) (disk_block);
3807283b4a9bSStephen M. Cameron 		cdb[10] = (u8) (disk_block_cnt >> 24);
3808283b4a9bSStephen M. Cameron 		cdb[11] = (u8) (disk_block_cnt >> 16);
3809283b4a9bSStephen M. Cameron 		cdb[12] = (u8) (disk_block_cnt >> 8);
3810283b4a9bSStephen M. Cameron 		cdb[13] = (u8) (disk_block_cnt);
3811283b4a9bSStephen M. Cameron 		cdb[14] = 0;
3812283b4a9bSStephen M. Cameron 		cdb[15] = 0;
3813283b4a9bSStephen M. Cameron 		cdb_len = 16;
3814283b4a9bSStephen M. Cameron 	} else {
3815283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
3816283b4a9bSStephen M. Cameron 		cdb[1] = 0;
3817283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 24);
3818283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 16);
3819283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 8);
3820283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block);
3821283b4a9bSStephen M. Cameron 		cdb[6] = 0;
3822283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block_cnt >> 8);
3823283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block_cnt);
3824283b4a9bSStephen M. Cameron 		cdb[9] = 0;
3825283b4a9bSStephen M. Cameron 		cdb_len = 10;
3826283b4a9bSStephen M. Cameron 	}
3827283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
3828283b4a9bSStephen M. Cameron 						dev->scsi3addr);
3829283b4a9bSStephen M. Cameron }
3830283b4a9bSStephen M. Cameron 
3831763aadbfSNicholas Bellinger /*
3832763aadbfSNicholas Bellinger  * Running in struct Scsi_Host->host_lock less mode using LLD internal
3833763aadbfSNicholas Bellinger  * struct ctlr_info *h->lock w/ spin_lock_irqsave() protection.
3834763aadbfSNicholas Bellinger  */
3835763aadbfSNicholas Bellinger static int hpsa_scsi_queue_command(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
3836edd16368SStephen M. Cameron {
3837edd16368SStephen M. Cameron 	struct ctlr_info *h;
3838edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
3839edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
3840edd16368SStephen M. Cameron 	struct CommandList *c;
3841283b4a9bSStephen M. Cameron 	int rc = 0;
3842edd16368SStephen M. Cameron 
3843edd16368SStephen M. Cameron 	/* Get the ptr to our adapter structure out of cmd->host. */
3844edd16368SStephen M. Cameron 	h = sdev_to_hba(cmd->device);
3845edd16368SStephen M. Cameron 	dev = cmd->device->hostdata;
3846edd16368SStephen M. Cameron 	if (!dev) {
3847edd16368SStephen M. Cameron 		cmd->result = DID_NO_CONNECT << 16;
3848763aadbfSNicholas Bellinger 		cmd->scsi_done(cmd);
3849edd16368SStephen M. Cameron 		return 0;
3850edd16368SStephen M. Cameron 	}
3851edd16368SStephen M. Cameron 	memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
3852edd16368SStephen M. Cameron 
3853094963daSStephen M. Cameron 	if (unlikely(lockup_detected(h))) {
3854a0c12413SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
3855763aadbfSNicholas Bellinger 		cmd->scsi_done(cmd);
3856a0c12413SStephen M. Cameron 		return 0;
3857a0c12413SStephen M. Cameron 	}
3858e16a33adSMatt Gates 	c = cmd_alloc(h);
3859edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
3860edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
3861edd16368SStephen M. Cameron 		return SCSI_MLQUEUE_HOST_BUSY;
3862edd16368SStephen M. Cameron 	}
3863edd16368SStephen M. Cameron 
3864edd16368SStephen M. Cameron 	/* Fill in the command list header */
3865edd16368SStephen M. Cameron 	/* save c in case we have to abort it  */
3866edd16368SStephen M. Cameron 	cmd->host_scribble = (unsigned char *) c;
3867edd16368SStephen M. Cameron 
3868edd16368SStephen M. Cameron 	c->cmd_type = CMD_SCSI;
3869edd16368SStephen M. Cameron 	c->scsi_cmd = cmd;
3870e1f7de0cSMatt Gates 
3871283b4a9bSStephen M. Cameron 	/* Call alternate submit routine for I/O accelerated commands.
3872283b4a9bSStephen M. Cameron 	 * Retries always go down the normal I/O path.
3873283b4a9bSStephen M. Cameron 	 */
3874283b4a9bSStephen M. Cameron 	if (likely(cmd->retries == 0 &&
3875da0697bdSScott Teel 		cmd->request->cmd_type == REQ_TYPE_FS &&
3876da0697bdSScott Teel 		h->acciopath_status)) {
3877283b4a9bSStephen M. Cameron 		if (dev->offload_enabled) {
3878283b4a9bSStephen M. Cameron 			rc = hpsa_scsi_ioaccel_raid_map(h, c);
3879283b4a9bSStephen M. Cameron 			if (rc == 0)
3880283b4a9bSStephen M. Cameron 				return 0; /* Sent on ioaccel path */
3881283b4a9bSStephen M. Cameron 			if (rc < 0) {   /* scsi_dma_map failed. */
3882283b4a9bSStephen M. Cameron 				cmd_free(h, c);
3883283b4a9bSStephen M. Cameron 				return SCSI_MLQUEUE_HOST_BUSY;
3884283b4a9bSStephen M. Cameron 			}
3885283b4a9bSStephen M. Cameron 		} else if (dev->ioaccel_handle) {
3886283b4a9bSStephen M. Cameron 			rc = hpsa_scsi_ioaccel_direct_map(h, c);
3887283b4a9bSStephen M. Cameron 			if (rc == 0)
3888283b4a9bSStephen M. Cameron 				return 0; /* Sent on direct map path */
3889283b4a9bSStephen M. Cameron 			if (rc < 0) {   /* scsi_dma_map failed. */
3890283b4a9bSStephen M. Cameron 				cmd_free(h, c);
3891283b4a9bSStephen M. Cameron 				return SCSI_MLQUEUE_HOST_BUSY;
3892283b4a9bSStephen M. Cameron 			}
3893283b4a9bSStephen M. Cameron 		}
3894283b4a9bSStephen M. Cameron 	}
3895e1f7de0cSMatt Gates 
3896edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;  /* unused in simple mode */
3897edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
389850a0decfSStephen M. Cameron 	c->Header.tag = cpu_to_le64((c->cmdindex << DIRECT_LOOKUP_SHIFT) |
389950a0decfSStephen M. Cameron 					DIRECT_LOOKUP_BIT);
3900edd16368SStephen M. Cameron 
3901edd16368SStephen M. Cameron 	/* Fill in the request block... */
3902edd16368SStephen M. Cameron 
3903edd16368SStephen M. Cameron 	c->Request.Timeout = 0;
3904edd16368SStephen M. Cameron 	memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
3905edd16368SStephen M. Cameron 	BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
3906edd16368SStephen M. Cameron 	c->Request.CDBLen = cmd->cmd_len;
3907edd16368SStephen M. Cameron 	memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
3908edd16368SStephen M. Cameron 	switch (cmd->sc_data_direction) {
3909edd16368SStephen M. Cameron 	case DMA_TO_DEVICE:
3910a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
3911a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_WRITE);
3912edd16368SStephen M. Cameron 		break;
3913edd16368SStephen M. Cameron 	case DMA_FROM_DEVICE:
3914a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
3915a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_READ);
3916edd16368SStephen M. Cameron 		break;
3917edd16368SStephen M. Cameron 	case DMA_NONE:
3918a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
3919a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_NONE);
3920edd16368SStephen M. Cameron 		break;
3921edd16368SStephen M. Cameron 	case DMA_BIDIRECTIONAL:
3922edd16368SStephen M. Cameron 		/* This can happen if a buggy application does a scsi passthru
3923edd16368SStephen M. Cameron 		 * and sets both inlen and outlen to non-zero. ( see
3924edd16368SStephen M. Cameron 		 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
3925edd16368SStephen M. Cameron 		 */
3926edd16368SStephen M. Cameron 
3927a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
3928a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_RSVD);
3929edd16368SStephen M. Cameron 		/* This is technically wrong, and hpsa controllers should
3930edd16368SStephen M. Cameron 		 * reject it with CMD_INVALID, which is the most correct
3931edd16368SStephen M. Cameron 		 * response, but non-fibre backends appear to let it
3932edd16368SStephen M. Cameron 		 * slide by, and give the same results as if this field
3933edd16368SStephen M. Cameron 		 * were set correctly.  Either way is acceptable for
3934edd16368SStephen M. Cameron 		 * our purposes here.
3935edd16368SStephen M. Cameron 		 */
3936edd16368SStephen M. Cameron 
3937edd16368SStephen M. Cameron 		break;
3938edd16368SStephen M. Cameron 
3939edd16368SStephen M. Cameron 	default:
3940edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3941edd16368SStephen M. Cameron 			cmd->sc_data_direction);
3942edd16368SStephen M. Cameron 		BUG();
3943edd16368SStephen M. Cameron 		break;
3944edd16368SStephen M. Cameron 	}
3945edd16368SStephen M. Cameron 
394633a2ffceSStephen M. Cameron 	if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
3947edd16368SStephen M. Cameron 		cmd_free(h, c);
3948edd16368SStephen M. Cameron 		return SCSI_MLQUEUE_HOST_BUSY;
3949edd16368SStephen M. Cameron 	}
3950edd16368SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
3951edd16368SStephen M. Cameron 	/* the cmd'll come back via intr handler in complete_scsi_command()  */
3952edd16368SStephen M. Cameron 	return 0;
3953edd16368SStephen M. Cameron }
3954edd16368SStephen M. Cameron 
39555f389360SStephen M. Cameron static int do_not_scan_if_controller_locked_up(struct ctlr_info *h)
39565f389360SStephen M. Cameron {
39575f389360SStephen M. Cameron 	unsigned long flags;
39585f389360SStephen M. Cameron 
39595f389360SStephen M. Cameron 	/*
39605f389360SStephen M. Cameron 	 * Don't let rescans be initiated on a controller known
39615f389360SStephen M. Cameron 	 * to be locked up.  If the controller locks up *during*
39625f389360SStephen M. Cameron 	 * a rescan, that thread is probably hosed, but at least
39635f389360SStephen M. Cameron 	 * we can prevent new rescan threads from piling up on a
39645f389360SStephen M. Cameron 	 * locked up controller.
39655f389360SStephen M. Cameron 	 */
3966094963daSStephen M. Cameron 	if (unlikely(lockup_detected(h))) {
39675f389360SStephen M. Cameron 		spin_lock_irqsave(&h->scan_lock, flags);
39685f389360SStephen M. Cameron 		h->scan_finished = 1;
39695f389360SStephen M. Cameron 		wake_up_all(&h->scan_wait_queue);
39705f389360SStephen M. Cameron 		spin_unlock_irqrestore(&h->scan_lock, flags);
39715f389360SStephen M. Cameron 		return 1;
39725f389360SStephen M. Cameron 	}
39735f389360SStephen M. Cameron 	return 0;
39745f389360SStephen M. Cameron }
39755f389360SStephen M. Cameron 
3976a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *sh)
3977a08a8471SStephen M. Cameron {
3978a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
3979a08a8471SStephen M. Cameron 	unsigned long flags;
3980a08a8471SStephen M. Cameron 
39815f389360SStephen M. Cameron 	if (do_not_scan_if_controller_locked_up(h))
39825f389360SStephen M. Cameron 		return;
39835f389360SStephen M. Cameron 
3984a08a8471SStephen M. Cameron 	/* wait until any scan already in progress is finished. */
3985a08a8471SStephen M. Cameron 	while (1) {
3986a08a8471SStephen M. Cameron 		spin_lock_irqsave(&h->scan_lock, flags);
3987a08a8471SStephen M. Cameron 		if (h->scan_finished)
3988a08a8471SStephen M. Cameron 			break;
3989a08a8471SStephen M. Cameron 		spin_unlock_irqrestore(&h->scan_lock, flags);
3990a08a8471SStephen M. Cameron 		wait_event(h->scan_wait_queue, h->scan_finished);
3991a08a8471SStephen M. Cameron 		/* Note: We don't need to worry about a race between this
3992a08a8471SStephen M. Cameron 		 * thread and driver unload because the midlayer will
3993a08a8471SStephen M. Cameron 		 * have incremented the reference count, so unload won't
3994a08a8471SStephen M. Cameron 		 * happen if we're in here.
3995a08a8471SStephen M. Cameron 		 */
3996a08a8471SStephen M. Cameron 	}
3997a08a8471SStephen M. Cameron 	h->scan_finished = 0; /* mark scan as in progress */
3998a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
3999a08a8471SStephen M. Cameron 
40005f389360SStephen M. Cameron 	if (do_not_scan_if_controller_locked_up(h))
40015f389360SStephen M. Cameron 		return;
40025f389360SStephen M. Cameron 
4003a08a8471SStephen M. Cameron 	hpsa_update_scsi_devices(h, h->scsi_host->host_no);
4004a08a8471SStephen M. Cameron 
4005a08a8471SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
4006a08a8471SStephen M. Cameron 	h->scan_finished = 1; /* mark scan as finished. */
4007a08a8471SStephen M. Cameron 	wake_up_all(&h->scan_wait_queue);
4008a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4009a08a8471SStephen M. Cameron }
4010a08a8471SStephen M. Cameron 
40117c0a0229SDon Brace static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth)
40127c0a0229SDon Brace {
40137c0a0229SDon Brace 	struct ctlr_info *h = sdev_to_hba(sdev);
40147c0a0229SDon Brace 
40157c0a0229SDon Brace 	if (qdepth < 1)
40167c0a0229SDon Brace 		qdepth = 1;
40177c0a0229SDon Brace 	else
40187c0a0229SDon Brace 		if (qdepth > h->nr_cmds)
40197c0a0229SDon Brace 			qdepth = h->nr_cmds;
40207c0a0229SDon Brace 	scsi_change_queue_depth(sdev, qdepth);
40217c0a0229SDon Brace 	return sdev->queue_depth;
40227c0a0229SDon Brace }
40237c0a0229SDon Brace 
4024a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
4025a08a8471SStephen M. Cameron 	unsigned long elapsed_time)
4026a08a8471SStephen M. Cameron {
4027a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
4028a08a8471SStephen M. Cameron 	unsigned long flags;
4029a08a8471SStephen M. Cameron 	int finished;
4030a08a8471SStephen M. Cameron 
4031a08a8471SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
4032a08a8471SStephen M. Cameron 	finished = h->scan_finished;
4033a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4034a08a8471SStephen M. Cameron 	return finished;
4035a08a8471SStephen M. Cameron }
4036a08a8471SStephen M. Cameron 
4037edd16368SStephen M. Cameron static void hpsa_unregister_scsi(struct ctlr_info *h)
4038edd16368SStephen M. Cameron {
4039edd16368SStephen M. Cameron 	/* we are being forcibly unloaded, and may not refuse. */
4040edd16368SStephen M. Cameron 	scsi_remove_host(h->scsi_host);
4041edd16368SStephen M. Cameron 	scsi_host_put(h->scsi_host);
4042edd16368SStephen M. Cameron 	h->scsi_host = NULL;
4043edd16368SStephen M. Cameron }
4044edd16368SStephen M. Cameron 
4045edd16368SStephen M. Cameron static int hpsa_register_scsi(struct ctlr_info *h)
4046edd16368SStephen M. Cameron {
4047b705690dSStephen M. Cameron 	struct Scsi_Host *sh;
4048b705690dSStephen M. Cameron 	int error;
4049edd16368SStephen M. Cameron 
4050b705690dSStephen M. Cameron 	sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
4051b705690dSStephen M. Cameron 	if (sh == NULL)
4052b705690dSStephen M. Cameron 		goto fail;
4053b705690dSStephen M. Cameron 
4054b705690dSStephen M. Cameron 	sh->io_port = 0;
4055b705690dSStephen M. Cameron 	sh->n_io_port = 0;
4056b705690dSStephen M. Cameron 	sh->this_id = -1;
4057b705690dSStephen M. Cameron 	sh->max_channel = 3;
4058b705690dSStephen M. Cameron 	sh->max_cmd_len = MAX_COMMAND_SIZE;
4059b705690dSStephen M. Cameron 	sh->max_lun = HPSA_MAX_LUN;
4060b705690dSStephen M. Cameron 	sh->max_id = HPSA_MAX_LUN;
4061b705690dSStephen M. Cameron 	sh->can_queue = h->nr_cmds;
4062316b221aSStephen M. Cameron 	if (h->hba_mode_enabled)
4063316b221aSStephen M. Cameron 		sh->cmd_per_lun = 7;
4064316b221aSStephen M. Cameron 	else
4065b705690dSStephen M. Cameron 		sh->cmd_per_lun = h->nr_cmds;
4066b705690dSStephen M. Cameron 	sh->sg_tablesize = h->maxsgentries;
4067b705690dSStephen M. Cameron 	h->scsi_host = sh;
4068b705690dSStephen M. Cameron 	sh->hostdata[0] = (unsigned long) h;
4069b705690dSStephen M. Cameron 	sh->irq = h->intr[h->intr_mode];
4070b705690dSStephen M. Cameron 	sh->unique_id = sh->irq;
4071b705690dSStephen M. Cameron 	error = scsi_add_host(sh, &h->pdev->dev);
4072b705690dSStephen M. Cameron 	if (error)
4073b705690dSStephen M. Cameron 		goto fail_host_put;
4074b705690dSStephen M. Cameron 	scsi_scan_host(sh);
4075b705690dSStephen M. Cameron 	return 0;
4076b705690dSStephen M. Cameron 
4077b705690dSStephen M. Cameron  fail_host_put:
4078b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_add_host"
4079b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
4080b705690dSStephen M. Cameron 	scsi_host_put(sh);
4081b705690dSStephen M. Cameron 	return error;
4082b705690dSStephen M. Cameron  fail:
4083b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_host_alloc"
4084b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
4085b705690dSStephen M. Cameron 	return -ENOMEM;
4086edd16368SStephen M. Cameron }
4087edd16368SStephen M. Cameron 
4088edd16368SStephen M. Cameron static int wait_for_device_to_become_ready(struct ctlr_info *h,
4089edd16368SStephen M. Cameron 	unsigned char lunaddr[])
4090edd16368SStephen M. Cameron {
40918919358eSTomas Henzl 	int rc;
4092edd16368SStephen M. Cameron 	int count = 0;
4093edd16368SStephen M. Cameron 	int waittime = 1; /* seconds */
4094edd16368SStephen M. Cameron 	struct CommandList *c;
4095edd16368SStephen M. Cameron 
4096edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
4097edd16368SStephen M. Cameron 	if (!c) {
4098edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in "
4099edd16368SStephen M. Cameron 			"wait_for_device_to_become_ready.\n");
4100edd16368SStephen M. Cameron 		return IO_ERROR;
4101edd16368SStephen M. Cameron 	}
4102edd16368SStephen M. Cameron 
4103edd16368SStephen M. Cameron 	/* Send test unit ready until device ready, or give up. */
4104edd16368SStephen M. Cameron 	while (count < HPSA_TUR_RETRY_LIMIT) {
4105edd16368SStephen M. Cameron 
4106edd16368SStephen M. Cameron 		/* Wait for a bit.  do this first, because if we send
4107edd16368SStephen M. Cameron 		 * the TUR right away, the reset will just abort it.
4108edd16368SStephen M. Cameron 		 */
4109edd16368SStephen M. Cameron 		msleep(1000 * waittime);
4110edd16368SStephen M. Cameron 		count++;
41118919358eSTomas Henzl 		rc = 0; /* Device ready. */
4112edd16368SStephen M. Cameron 
4113edd16368SStephen M. Cameron 		/* Increase wait time with each try, up to a point. */
4114edd16368SStephen M. Cameron 		if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
4115edd16368SStephen M. Cameron 			waittime = waittime * 2;
4116edd16368SStephen M. Cameron 
4117a2dac136SStephen M. Cameron 		/* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
4118a2dac136SStephen M. Cameron 		(void) fill_cmd(c, TEST_UNIT_READY, h,
4119a2dac136SStephen M. Cameron 				NULL, 0, 0, lunaddr, TYPE_CMD);
4120edd16368SStephen M. Cameron 		hpsa_scsi_do_simple_cmd_core(h, c);
4121edd16368SStephen M. Cameron 		/* no unmap needed here because no data xfer. */
4122edd16368SStephen M. Cameron 
4123edd16368SStephen M. Cameron 		if (c->err_info->CommandStatus == CMD_SUCCESS)
4124edd16368SStephen M. Cameron 			break;
4125edd16368SStephen M. Cameron 
4126edd16368SStephen M. Cameron 		if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
4127edd16368SStephen M. Cameron 			c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
4128edd16368SStephen M. Cameron 			(c->err_info->SenseInfo[2] == NO_SENSE ||
4129edd16368SStephen M. Cameron 			c->err_info->SenseInfo[2] == UNIT_ATTENTION))
4130edd16368SStephen M. Cameron 			break;
4131edd16368SStephen M. Cameron 
4132edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "waiting %d secs "
4133edd16368SStephen M. Cameron 			"for device to become ready.\n", waittime);
4134edd16368SStephen M. Cameron 		rc = 1; /* device not ready. */
4135edd16368SStephen M. Cameron 	}
4136edd16368SStephen M. Cameron 
4137edd16368SStephen M. Cameron 	if (rc)
4138edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "giving up on device.\n");
4139edd16368SStephen M. Cameron 	else
4140edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "device is ready.\n");
4141edd16368SStephen M. Cameron 
4142edd16368SStephen M. Cameron 	cmd_special_free(h, c);
4143edd16368SStephen M. Cameron 	return rc;
4144edd16368SStephen M. Cameron }
4145edd16368SStephen M. Cameron 
4146edd16368SStephen M. Cameron /* Need at least one of these error handlers to keep ../scsi/hosts.c from
4147edd16368SStephen M. Cameron  * complaining.  Doing a host- or bus-reset can't do anything good here.
4148edd16368SStephen M. Cameron  */
4149edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
4150edd16368SStephen M. Cameron {
4151edd16368SStephen M. Cameron 	int rc;
4152edd16368SStephen M. Cameron 	struct ctlr_info *h;
4153edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
4154edd16368SStephen M. Cameron 
4155edd16368SStephen M. Cameron 	/* find the controller to which the command to be aborted was sent */
4156edd16368SStephen M. Cameron 	h = sdev_to_hba(scsicmd->device);
4157edd16368SStephen M. Cameron 	if (h == NULL) /* paranoia */
4158edd16368SStephen M. Cameron 		return FAILED;
4159edd16368SStephen M. Cameron 	dev = scsicmd->device->hostdata;
4160edd16368SStephen M. Cameron 	if (!dev) {
4161edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
4162edd16368SStephen M. Cameron 			"device lookup failed.\n");
4163edd16368SStephen M. Cameron 		return FAILED;
4164edd16368SStephen M. Cameron 	}
4165d416b0c7SStephen M. Cameron 	dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
4166d416b0c7SStephen M. Cameron 		h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
4167edd16368SStephen M. Cameron 	/* send a reset to the SCSI LUN which the command was sent to */
4168bf711ac6SScott Teel 	rc = hpsa_send_reset(h, dev->scsi3addr, HPSA_RESET_TYPE_LUN);
4169edd16368SStephen M. Cameron 	if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
4170edd16368SStephen M. Cameron 		return SUCCESS;
4171edd16368SStephen M. Cameron 
4172edd16368SStephen M. Cameron 	dev_warn(&h->pdev->dev, "resetting device failed.\n");
4173edd16368SStephen M. Cameron 	return FAILED;
4174edd16368SStephen M. Cameron }
4175edd16368SStephen M. Cameron 
41766cba3f19SStephen M. Cameron static void swizzle_abort_tag(u8 *tag)
41776cba3f19SStephen M. Cameron {
41786cba3f19SStephen M. Cameron 	u8 original_tag[8];
41796cba3f19SStephen M. Cameron 
41806cba3f19SStephen M. Cameron 	memcpy(original_tag, tag, 8);
41816cba3f19SStephen M. Cameron 	tag[0] = original_tag[3];
41826cba3f19SStephen M. Cameron 	tag[1] = original_tag[2];
41836cba3f19SStephen M. Cameron 	tag[2] = original_tag[1];
41846cba3f19SStephen M. Cameron 	tag[3] = original_tag[0];
41856cba3f19SStephen M. Cameron 	tag[4] = original_tag[7];
41866cba3f19SStephen M. Cameron 	tag[5] = original_tag[6];
41876cba3f19SStephen M. Cameron 	tag[6] = original_tag[5];
41886cba3f19SStephen M. Cameron 	tag[7] = original_tag[4];
41896cba3f19SStephen M. Cameron }
41906cba3f19SStephen M. Cameron 
419117eb87d2SScott Teel static void hpsa_get_tag(struct ctlr_info *h,
41922b08b3e9SDon Brace 	struct CommandList *c, __le32 *taglower, __le32 *tagupper)
419317eb87d2SScott Teel {
41942b08b3e9SDon Brace 	u64 tag;
419517eb87d2SScott Teel 	if (c->cmd_type == CMD_IOACCEL1) {
419617eb87d2SScott Teel 		struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
419717eb87d2SScott Teel 			&h->ioaccel_cmd_pool[c->cmdindex];
41982b08b3e9SDon Brace 		tag = le64_to_cpu(cm1->tag);
41992b08b3e9SDon Brace 		*tagupper = cpu_to_le32(tag >> 32);
42002b08b3e9SDon Brace 		*taglower = cpu_to_le32(tag);
420154b6e9e9SScott Teel 		return;
420254b6e9e9SScott Teel 	}
420354b6e9e9SScott Teel 	if (c->cmd_type == CMD_IOACCEL2) {
420454b6e9e9SScott Teel 		struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
420554b6e9e9SScott Teel 			&h->ioaccel2_cmd_pool[c->cmdindex];
4206dd0e19f3SScott Teel 		/* upper tag not used in ioaccel2 mode */
4207dd0e19f3SScott Teel 		memset(tagupper, 0, sizeof(*tagupper));
4208dd0e19f3SScott Teel 		*taglower = cm2->Tag;
420954b6e9e9SScott Teel 		return;
421054b6e9e9SScott Teel 	}
42112b08b3e9SDon Brace 	tag = le64_to_cpu(c->Header.tag);
42122b08b3e9SDon Brace 	*tagupper = cpu_to_le32(tag >> 32);
42132b08b3e9SDon Brace 	*taglower = cpu_to_le32(tag);
421417eb87d2SScott Teel }
421554b6e9e9SScott Teel 
421675167d2cSStephen M. Cameron static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
42176cba3f19SStephen M. Cameron 	struct CommandList *abort, int swizzle)
421875167d2cSStephen M. Cameron {
421975167d2cSStephen M. Cameron 	int rc = IO_OK;
422075167d2cSStephen M. Cameron 	struct CommandList *c;
422175167d2cSStephen M. Cameron 	struct ErrorInfo *ei;
42222b08b3e9SDon Brace 	__le32 tagupper, taglower;
422375167d2cSStephen M. Cameron 
422475167d2cSStephen M. Cameron 	c = cmd_special_alloc(h);
422575167d2cSStephen M. Cameron 	if (c == NULL) {	/* trouble... */
422675167d2cSStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
422775167d2cSStephen M. Cameron 		return -ENOMEM;
422875167d2cSStephen M. Cameron 	}
422975167d2cSStephen M. Cameron 
4230a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no buffer to map */
4231a2dac136SStephen M. Cameron 	(void) fill_cmd(c, HPSA_ABORT_MSG, h, abort,
4232a2dac136SStephen M. Cameron 		0, 0, scsi3addr, TYPE_MSG);
42336cba3f19SStephen M. Cameron 	if (swizzle)
42346cba3f19SStephen M. Cameron 		swizzle_abort_tag(&c->Request.CDB[4]);
423575167d2cSStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core(h, c);
423617eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
423775167d2cSStephen M. Cameron 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd_core completed.\n",
423817eb87d2SScott Teel 		__func__, tagupper, taglower);
423975167d2cSStephen M. Cameron 	/* no unmap needed here because no data xfer. */
424075167d2cSStephen M. Cameron 
424175167d2cSStephen M. Cameron 	ei = c->err_info;
424275167d2cSStephen M. Cameron 	switch (ei->CommandStatus) {
424375167d2cSStephen M. Cameron 	case CMD_SUCCESS:
424475167d2cSStephen M. Cameron 		break;
424575167d2cSStephen M. Cameron 	case CMD_UNABORTABLE: /* Very common, don't make noise. */
424675167d2cSStephen M. Cameron 		rc = -1;
424775167d2cSStephen M. Cameron 		break;
424875167d2cSStephen M. Cameron 	default:
424975167d2cSStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
425017eb87d2SScott Teel 			__func__, tagupper, taglower);
4251d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
425275167d2cSStephen M. Cameron 		rc = -1;
425375167d2cSStephen M. Cameron 		break;
425475167d2cSStephen M. Cameron 	}
425575167d2cSStephen M. Cameron 	cmd_special_free(h, c);
4256dd0e19f3SScott Teel 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n",
4257dd0e19f3SScott Teel 		__func__, tagupper, taglower);
425875167d2cSStephen M. Cameron 	return rc;
425975167d2cSStephen M. Cameron }
426075167d2cSStephen M. Cameron 
426175167d2cSStephen M. Cameron /*
426275167d2cSStephen M. Cameron  * hpsa_find_cmd_in_queue
426375167d2cSStephen M. Cameron  *
426475167d2cSStephen M. Cameron  * Used to determine whether a command (find) is still present
426575167d2cSStephen M. Cameron  * in queue_head.   Optionally excludes the last element of queue_head.
426675167d2cSStephen M. Cameron  *
426775167d2cSStephen M. Cameron  * This is used to avoid unnecessary aborts.  Commands in h->reqQ have
426875167d2cSStephen M. Cameron  * not yet been submitted, and so can be aborted by the driver without
426975167d2cSStephen M. Cameron  * sending an abort to the hardware.
427075167d2cSStephen M. Cameron  *
427175167d2cSStephen M. Cameron  * Returns pointer to command if found in queue, NULL otherwise.
427275167d2cSStephen M. Cameron  */
427375167d2cSStephen M. Cameron static struct CommandList *hpsa_find_cmd_in_queue(struct ctlr_info *h,
427475167d2cSStephen M. Cameron 			struct scsi_cmnd *find, struct list_head *queue_head)
427575167d2cSStephen M. Cameron {
427675167d2cSStephen M. Cameron 	unsigned long flags;
427775167d2cSStephen M. Cameron 	struct CommandList *c = NULL;	/* ptr into cmpQ */
427875167d2cSStephen M. Cameron 
427975167d2cSStephen M. Cameron 	if (!find)
428042a91641SDon Brace 		return NULL;
428175167d2cSStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
428275167d2cSStephen M. Cameron 	list_for_each_entry(c, queue_head, list) {
428375167d2cSStephen M. Cameron 		if (c->scsi_cmd == NULL) /* e.g.: passthru ioctl */
428475167d2cSStephen M. Cameron 			continue;
428575167d2cSStephen M. Cameron 		if (c->scsi_cmd == find) {
428675167d2cSStephen M. Cameron 			spin_unlock_irqrestore(&h->lock, flags);
428775167d2cSStephen M. Cameron 			return c;
428875167d2cSStephen M. Cameron 		}
428975167d2cSStephen M. Cameron 	}
429075167d2cSStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
429175167d2cSStephen M. Cameron 	return NULL;
429275167d2cSStephen M. Cameron }
429375167d2cSStephen M. Cameron 
42946cba3f19SStephen M. Cameron static struct CommandList *hpsa_find_cmd_in_queue_by_tag(struct ctlr_info *h,
42956cba3f19SStephen M. Cameron 					u8 *tag, struct list_head *queue_head)
42966cba3f19SStephen M. Cameron {
42976cba3f19SStephen M. Cameron 	unsigned long flags;
42986cba3f19SStephen M. Cameron 	struct CommandList *c;
42996cba3f19SStephen M. Cameron 
43006cba3f19SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
43016cba3f19SStephen M. Cameron 	list_for_each_entry(c, queue_head, list) {
430250a0decfSStephen M. Cameron 		if (memcmp(&c->Header.tag, tag, 8) != 0)
43036cba3f19SStephen M. Cameron 			continue;
43046cba3f19SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
43056cba3f19SStephen M. Cameron 		return c;
43066cba3f19SStephen M. Cameron 	}
43076cba3f19SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
43086cba3f19SStephen M. Cameron 	return NULL;
43096cba3f19SStephen M. Cameron }
43106cba3f19SStephen M. Cameron 
431154b6e9e9SScott Teel /* ioaccel2 path firmware cannot handle abort task requests.
431254b6e9e9SScott Teel  * Change abort requests to physical target reset, and send to the
431354b6e9e9SScott Teel  * address of the physical disk used for the ioaccel 2 command.
431454b6e9e9SScott Teel  * Return 0 on success (IO_OK)
431554b6e9e9SScott Teel  *	 -1 on failure
431654b6e9e9SScott Teel  */
431754b6e9e9SScott Teel 
431854b6e9e9SScott Teel static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
431954b6e9e9SScott Teel 	unsigned char *scsi3addr, struct CommandList *abort)
432054b6e9e9SScott Teel {
432154b6e9e9SScott Teel 	int rc = IO_OK;
432254b6e9e9SScott Teel 	struct scsi_cmnd *scmd; /* scsi command within request being aborted */
432354b6e9e9SScott Teel 	struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
432454b6e9e9SScott Teel 	unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
432554b6e9e9SScott Teel 	unsigned char *psa = &phys_scsi3addr[0];
432654b6e9e9SScott Teel 
432754b6e9e9SScott Teel 	/* Get a pointer to the hpsa logical device. */
432854b6e9e9SScott Teel 	scmd = (struct scsi_cmnd *) abort->scsi_cmd;
432954b6e9e9SScott Teel 	dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
433054b6e9e9SScott Teel 	if (dev == NULL) {
433154b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
433254b6e9e9SScott Teel 			"Cannot abort: no device pointer for command.\n");
433354b6e9e9SScott Teel 			return -1; /* not abortable */
433454b6e9e9SScott Teel 	}
433554b6e9e9SScott Teel 
43362ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
43372ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
43382ba8bfc8SStephen 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",
43392ba8bfc8SStephen M. Cameron 			h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
43402ba8bfc8SStephen M. Cameron 			scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
43412ba8bfc8SStephen M. Cameron 			scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]);
43422ba8bfc8SStephen M. Cameron 
434354b6e9e9SScott Teel 	if (!dev->offload_enabled) {
434454b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
434554b6e9e9SScott Teel 			"Can't abort: device is not operating in HP SSD Smart Path mode.\n");
434654b6e9e9SScott Teel 		return -1; /* not abortable */
434754b6e9e9SScott Teel 	}
434854b6e9e9SScott Teel 
434954b6e9e9SScott Teel 	/* Incoming scsi3addr is logical addr. We need physical disk addr. */
435054b6e9e9SScott Teel 	if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
435154b6e9e9SScott Teel 		dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
435254b6e9e9SScott Teel 		return -1; /* not abortable */
435354b6e9e9SScott Teel 	}
435454b6e9e9SScott Teel 
435554b6e9e9SScott Teel 	/* send the reset */
43562ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
43572ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
43582ba8bfc8SStephen M. Cameron 			"Reset as abort: Resetting physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
43592ba8bfc8SStephen M. Cameron 			psa[0], psa[1], psa[2], psa[3],
43602ba8bfc8SStephen M. Cameron 			psa[4], psa[5], psa[6], psa[7]);
436154b6e9e9SScott Teel 	rc = hpsa_send_reset(h, psa, HPSA_RESET_TYPE_TARGET);
436254b6e9e9SScott Teel 	if (rc != 0) {
436354b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
436454b6e9e9SScott Teel 			"Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
436554b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
436654b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
436754b6e9e9SScott Teel 		return rc; /* failed to reset */
436854b6e9e9SScott Teel 	}
436954b6e9e9SScott Teel 
437054b6e9e9SScott Teel 	/* wait for device to recover */
437154b6e9e9SScott Teel 	if (wait_for_device_to_become_ready(h, psa) != 0) {
437254b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
437354b6e9e9SScott Teel 			"Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
437454b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
437554b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
437654b6e9e9SScott Teel 		return -1;  /* failed to recover */
437754b6e9e9SScott Teel 	}
437854b6e9e9SScott Teel 
437954b6e9e9SScott Teel 	/* device recovered */
438054b6e9e9SScott Teel 	dev_info(&h->pdev->dev,
438154b6e9e9SScott Teel 		"Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
438254b6e9e9SScott Teel 		psa[0], psa[1], psa[2], psa[3],
438354b6e9e9SScott Teel 		psa[4], psa[5], psa[6], psa[7]);
438454b6e9e9SScott Teel 
438554b6e9e9SScott Teel 	return rc; /* success */
438654b6e9e9SScott Teel }
438754b6e9e9SScott Teel 
43886cba3f19SStephen M. Cameron /* Some Smart Arrays need the abort tag swizzled, and some don't.  It's hard to
43896cba3f19SStephen M. Cameron  * tell which kind we're dealing with, so we send the abort both ways.  There
43906cba3f19SStephen M. Cameron  * shouldn't be any collisions between swizzled and unswizzled tags due to the
43916cba3f19SStephen M. Cameron  * way we construct our tags but we check anyway in case the assumptions which
43926cba3f19SStephen M. Cameron  * make this true someday become false.
43936cba3f19SStephen M. Cameron  */
43946cba3f19SStephen M. Cameron static int hpsa_send_abort_both_ways(struct ctlr_info *h,
43956cba3f19SStephen M. Cameron 	unsigned char *scsi3addr, struct CommandList *abort)
43966cba3f19SStephen M. Cameron {
43976cba3f19SStephen M. Cameron 	u8 swizzled_tag[8];
43986cba3f19SStephen M. Cameron 	struct CommandList *c;
43996cba3f19SStephen M. Cameron 	int rc = 0, rc2 = 0;
44006cba3f19SStephen M. Cameron 
440154b6e9e9SScott Teel 	/* ioccelerator mode 2 commands should be aborted via the
440254b6e9e9SScott Teel 	 * accelerated path, since RAID path is unaware of these commands,
440354b6e9e9SScott Teel 	 * but underlying firmware can't handle abort TMF.
440454b6e9e9SScott Teel 	 * Change abort to physical device reset.
440554b6e9e9SScott Teel 	 */
440654b6e9e9SScott Teel 	if (abort->cmd_type == CMD_IOACCEL2)
440754b6e9e9SScott Teel 		return hpsa_send_reset_as_abort_ioaccel2(h, scsi3addr, abort);
440854b6e9e9SScott Teel 
44096cba3f19SStephen M. Cameron 	/* we do not expect to find the swizzled tag in our queue, but
44106cba3f19SStephen M. Cameron 	 * check anyway just to be sure the assumptions which make this
44116cba3f19SStephen M. Cameron 	 * the case haven't become wrong.
44126cba3f19SStephen M. Cameron 	 */
44136cba3f19SStephen M. Cameron 	memcpy(swizzled_tag, &abort->Request.CDB[4], 8);
44146cba3f19SStephen M. Cameron 	swizzle_abort_tag(swizzled_tag);
44156cba3f19SStephen M. Cameron 	c = hpsa_find_cmd_in_queue_by_tag(h, swizzled_tag, &h->cmpQ);
44166cba3f19SStephen M. Cameron 	if (c != NULL) {
44176cba3f19SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Unexpectedly found byte-swapped tag in completion queue.\n");
44186cba3f19SStephen M. Cameron 		return hpsa_send_abort(h, scsi3addr, abort, 0);
44196cba3f19SStephen M. Cameron 	}
44206cba3f19SStephen M. Cameron 	rc = hpsa_send_abort(h, scsi3addr, abort, 0);
44216cba3f19SStephen M. Cameron 
44226cba3f19SStephen M. Cameron 	/* if the command is still in our queue, we can't conclude that it was
44236cba3f19SStephen M. Cameron 	 * aborted (it might have just completed normally) but in any case
44246cba3f19SStephen M. Cameron 	 * we don't need to try to abort it another way.
44256cba3f19SStephen M. Cameron 	 */
44266cba3f19SStephen M. Cameron 	c = hpsa_find_cmd_in_queue(h, abort->scsi_cmd, &h->cmpQ);
44276cba3f19SStephen M. Cameron 	if (c)
44286cba3f19SStephen M. Cameron 		rc2 = hpsa_send_abort(h, scsi3addr, abort, 1);
44296cba3f19SStephen M. Cameron 	return rc && rc2;
44306cba3f19SStephen M. Cameron }
44316cba3f19SStephen M. Cameron 
443275167d2cSStephen M. Cameron /* Send an abort for the specified command.
443375167d2cSStephen M. Cameron  *	If the device and controller support it,
443475167d2cSStephen M. Cameron  *		send a task abort request.
443575167d2cSStephen M. Cameron  */
443675167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
443775167d2cSStephen M. Cameron {
443875167d2cSStephen M. Cameron 
443975167d2cSStephen M. Cameron 	int i, rc;
444075167d2cSStephen M. Cameron 	struct ctlr_info *h;
444175167d2cSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
444275167d2cSStephen M. Cameron 	struct CommandList *abort; /* pointer to command to be aborted */
444375167d2cSStephen M. Cameron 	struct CommandList *found;
444475167d2cSStephen M. Cameron 	struct scsi_cmnd *as;	/* ptr to scsi cmd inside aborted command. */
444575167d2cSStephen M. Cameron 	char msg[256];		/* For debug messaging. */
444675167d2cSStephen M. Cameron 	int ml = 0;
44472b08b3e9SDon Brace 	__le32 tagupper, taglower;
444875167d2cSStephen M. Cameron 
444975167d2cSStephen M. Cameron 	/* Find the controller of the command to be aborted */
445075167d2cSStephen M. Cameron 	h = sdev_to_hba(sc->device);
445175167d2cSStephen M. Cameron 	if (WARN(h == NULL,
445275167d2cSStephen M. Cameron 			"ABORT REQUEST FAILED, Controller lookup failed.\n"))
445375167d2cSStephen M. Cameron 		return FAILED;
445475167d2cSStephen M. Cameron 
445575167d2cSStephen M. Cameron 	/* Check that controller supports some kind of task abort */
445675167d2cSStephen M. Cameron 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
445775167d2cSStephen M. Cameron 		!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
445875167d2cSStephen M. Cameron 		return FAILED;
445975167d2cSStephen M. Cameron 
446075167d2cSStephen M. Cameron 	memset(msg, 0, sizeof(msg));
44619cb78c16SHannes Reinecke 	ml += sprintf(msg+ml, "ABORT REQUEST on C%d:B%d:T%d:L%llu ",
446275167d2cSStephen M. Cameron 		h->scsi_host->host_no, sc->device->channel,
446375167d2cSStephen M. Cameron 		sc->device->id, sc->device->lun);
446475167d2cSStephen M. Cameron 
446575167d2cSStephen M. Cameron 	/* Find the device of the command to be aborted */
446675167d2cSStephen M. Cameron 	dev = sc->device->hostdata;
446775167d2cSStephen M. Cameron 	if (!dev) {
446875167d2cSStephen M. Cameron 		dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
446975167d2cSStephen M. Cameron 				msg);
447075167d2cSStephen M. Cameron 		return FAILED;
447175167d2cSStephen M. Cameron 	}
447275167d2cSStephen M. Cameron 
447375167d2cSStephen M. Cameron 	/* Get SCSI command to be aborted */
447475167d2cSStephen M. Cameron 	abort = (struct CommandList *) sc->host_scribble;
447575167d2cSStephen M. Cameron 	if (abort == NULL) {
447675167d2cSStephen M. Cameron 		dev_err(&h->pdev->dev, "%s FAILED, Command to abort is NULL.\n",
447775167d2cSStephen M. Cameron 				msg);
447875167d2cSStephen M. Cameron 		return FAILED;
447975167d2cSStephen M. Cameron 	}
448017eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
448117eb87d2SScott Teel 	ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
448275167d2cSStephen M. Cameron 	as  = (struct scsi_cmnd *) abort->scsi_cmd;
448375167d2cSStephen M. Cameron 	if (as != NULL)
448475167d2cSStephen M. Cameron 		ml += sprintf(msg+ml, "Command:0x%x SN:0x%lx ",
448575167d2cSStephen M. Cameron 			as->cmnd[0], as->serial_number);
448675167d2cSStephen M. Cameron 	dev_dbg(&h->pdev->dev, "%s\n", msg);
448775167d2cSStephen M. Cameron 	dev_warn(&h->pdev->dev, "Abort request on C%d:B%d:T%d:L%d\n",
448875167d2cSStephen M. Cameron 		h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
448975167d2cSStephen M. Cameron 
449075167d2cSStephen M. Cameron 	/* Search reqQ to See if command is queued but not submitted,
449175167d2cSStephen M. Cameron 	 * if so, complete the command with aborted status and remove
449275167d2cSStephen M. Cameron 	 * it from the reqQ.
449375167d2cSStephen M. Cameron 	 */
449475167d2cSStephen M. Cameron 	found = hpsa_find_cmd_in_queue(h, sc, &h->reqQ);
449575167d2cSStephen M. Cameron 	if (found) {
449675167d2cSStephen M. Cameron 		found->err_info->CommandStatus = CMD_ABORTED;
449775167d2cSStephen M. Cameron 		finish_cmd(found);
449875167d2cSStephen M. Cameron 		dev_info(&h->pdev->dev, "%s Request SUCCEEDED (driver queue).\n",
449975167d2cSStephen M. Cameron 				msg);
450075167d2cSStephen M. Cameron 		return SUCCESS;
450175167d2cSStephen M. Cameron 	}
450275167d2cSStephen M. Cameron 
450375167d2cSStephen M. Cameron 	/* not in reqQ, if also not in cmpQ, must have already completed */
450475167d2cSStephen M. Cameron 	found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
450575167d2cSStephen M. Cameron 	if (!found)  {
4506d6ebd0f7SStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s Request SUCCEEDED (not known to driver).\n",
450775167d2cSStephen M. Cameron 				msg);
450875167d2cSStephen M. Cameron 		return SUCCESS;
450975167d2cSStephen M. Cameron 	}
451075167d2cSStephen M. Cameron 
451175167d2cSStephen M. Cameron 	/*
451275167d2cSStephen M. Cameron 	 * Command is in flight, or possibly already completed
451375167d2cSStephen M. Cameron 	 * by the firmware (but not to the scsi mid layer) but we can't
451475167d2cSStephen M. Cameron 	 * distinguish which.  Send the abort down.
451575167d2cSStephen M. Cameron 	 */
45166cba3f19SStephen M. Cameron 	rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort);
451775167d2cSStephen M. Cameron 	if (rc != 0) {
451875167d2cSStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s Request FAILED.\n", msg);
451975167d2cSStephen M. Cameron 		dev_warn(&h->pdev->dev, "FAILED abort on device C%d:B%d:T%d:L%d\n",
452075167d2cSStephen M. Cameron 			h->scsi_host->host_no,
452175167d2cSStephen M. Cameron 			dev->bus, dev->target, dev->lun);
452275167d2cSStephen M. Cameron 		return FAILED;
452375167d2cSStephen M. Cameron 	}
452475167d2cSStephen M. Cameron 	dev_info(&h->pdev->dev, "%s REQUEST SUCCEEDED.\n", msg);
452575167d2cSStephen M. Cameron 
452675167d2cSStephen M. Cameron 	/* If the abort(s) above completed and actually aborted the
452775167d2cSStephen M. Cameron 	 * command, then the command to be aborted should already be
452875167d2cSStephen M. Cameron 	 * completed.  If not, wait around a bit more to see if they
452975167d2cSStephen M. Cameron 	 * manage to complete normally.
453075167d2cSStephen M. Cameron 	 */
453175167d2cSStephen M. Cameron #define ABORT_COMPLETE_WAIT_SECS 30
453275167d2cSStephen M. Cameron 	for (i = 0; i < ABORT_COMPLETE_WAIT_SECS * 10; i++) {
453375167d2cSStephen M. Cameron 		found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
453475167d2cSStephen M. Cameron 		if (!found)
453575167d2cSStephen M. Cameron 			return SUCCESS;
453675167d2cSStephen M. Cameron 		msleep(100);
453775167d2cSStephen M. Cameron 	}
453875167d2cSStephen M. Cameron 	dev_warn(&h->pdev->dev, "%s FAILED. Aborted command has not completed after %d seconds.\n",
453975167d2cSStephen M. Cameron 		msg, ABORT_COMPLETE_WAIT_SECS);
454075167d2cSStephen M. Cameron 	return FAILED;
454175167d2cSStephen M. Cameron }
454275167d2cSStephen M. Cameron 
454375167d2cSStephen M. Cameron 
4544edd16368SStephen M. Cameron /*
4545edd16368SStephen M. Cameron  * For operations that cannot sleep, a command block is allocated at init,
4546edd16368SStephen M. Cameron  * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
4547edd16368SStephen M. Cameron  * which ones are free or in use.  Lock must be held when calling this.
4548edd16368SStephen M. Cameron  * cmd_free() is the complement.
4549edd16368SStephen M. Cameron  */
4550edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h)
4551edd16368SStephen M. Cameron {
4552edd16368SStephen M. Cameron 	struct CommandList *c;
4553edd16368SStephen M. Cameron 	int i;
4554edd16368SStephen M. Cameron 	union u64bit temp64;
4555edd16368SStephen M. Cameron 	dma_addr_t cmd_dma_handle, err_dma_handle;
45564c413128SStephen M. Cameron 	int loopcount;
4557edd16368SStephen M. Cameron 
45584c413128SStephen M. Cameron 	/* There is some *extremely* small but non-zero chance that that
45594c413128SStephen M. Cameron 	 * multiple threads could get in here, and one thread could
45604c413128SStephen M. Cameron 	 * be scanning through the list of bits looking for a free
45614c413128SStephen M. Cameron 	 * one, but the free ones are always behind him, and other
45624c413128SStephen M. Cameron 	 * threads sneak in behind him and eat them before he can
45634c413128SStephen M. Cameron 	 * get to them, so that while there is always a free one, a
45644c413128SStephen M. Cameron 	 * very unlucky thread might be starved anyway, never able to
45654c413128SStephen M. Cameron 	 * beat the other threads.  In reality, this happens so
45664c413128SStephen M. Cameron 	 * infrequently as to be indistinguishable from never.
45674c413128SStephen M. Cameron 	 */
45684c413128SStephen M. Cameron 
45694c413128SStephen M. Cameron 	loopcount = 0;
4570edd16368SStephen M. Cameron 	do {
4571edd16368SStephen M. Cameron 		i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
45724c413128SStephen M. Cameron 		if (i == h->nr_cmds)
45734c413128SStephen M. Cameron 			i = 0;
45744c413128SStephen M. Cameron 		loopcount++;
45754c413128SStephen M. Cameron 	} while (test_and_set_bit(i & (BITS_PER_LONG - 1),
45764c413128SStephen M. Cameron 		  h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0 &&
45774c413128SStephen M. Cameron 		loopcount < 10);
45784c413128SStephen M. Cameron 
45794c413128SStephen M. Cameron 	/* Thread got starved?  We do not expect this to ever happen. */
45804c413128SStephen M. Cameron 	if (loopcount >= 10)
4581edd16368SStephen M. Cameron 		return NULL;
4582e16a33adSMatt Gates 
4583edd16368SStephen M. Cameron 	c = h->cmd_pool + i;
4584edd16368SStephen M. Cameron 	memset(c, 0, sizeof(*c));
4585edd16368SStephen M. Cameron 	cmd_dma_handle = h->cmd_pool_dhandle
4586edd16368SStephen M. Cameron 	    + i * sizeof(*c);
4587edd16368SStephen M. Cameron 	c->err_info = h->errinfo_pool + i;
4588edd16368SStephen M. Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
4589edd16368SStephen M. Cameron 	err_dma_handle = h->errinfo_pool_dhandle
4590edd16368SStephen M. Cameron 	    + i * sizeof(*c->err_info);
4591edd16368SStephen M. Cameron 
4592edd16368SStephen M. Cameron 	c->cmdindex = i;
4593edd16368SStephen M. Cameron 
45949e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&c->list);
459501a02ffcSStephen M. Cameron 	c->busaddr = (u32) cmd_dma_handle;
459601a02ffcSStephen M. Cameron 	temp64.val = (u64) err_dma_handle;
459750a0decfSStephen M. Cameron 	c->ErrDesc.Addr = cpu_to_le64(err_dma_handle);
459850a0decfSStephen M. Cameron 	c->ErrDesc.Len = cpu_to_le32(sizeof(*c->err_info));
4599edd16368SStephen M. Cameron 
4600edd16368SStephen M. Cameron 	c->h = h;
4601edd16368SStephen M. Cameron 	return c;
4602edd16368SStephen M. Cameron }
4603edd16368SStephen M. Cameron 
4604edd16368SStephen M. Cameron /* For operations that can wait for kmalloc to possibly sleep,
4605edd16368SStephen M. Cameron  * this routine can be called. Lock need not be held to call
4606edd16368SStephen M. Cameron  * cmd_special_alloc. cmd_special_free() is the complement.
4607edd16368SStephen M. Cameron  */
4608edd16368SStephen M. Cameron static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
4609edd16368SStephen M. Cameron {
4610edd16368SStephen M. Cameron 	struct CommandList *c;
4611edd16368SStephen M. Cameron 	dma_addr_t cmd_dma_handle, err_dma_handle;
4612edd16368SStephen M. Cameron 
46137c845eb5SJoe Perches 	c = pci_zalloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
4614edd16368SStephen M. Cameron 	if (c == NULL)
4615edd16368SStephen M. Cameron 		return NULL;
4616edd16368SStephen M. Cameron 
4617e1f7de0cSMatt Gates 	c->cmd_type = CMD_SCSI;
4618edd16368SStephen M. Cameron 	c->cmdindex = -1;
4619edd16368SStephen M. Cameron 
46207c845eb5SJoe Perches 	c->err_info = pci_zalloc_consistent(h->pdev, sizeof(*c->err_info),
4621edd16368SStephen M. Cameron 					    &err_dma_handle);
4622edd16368SStephen M. Cameron 
4623edd16368SStephen M. Cameron 	if (c->err_info == NULL) {
4624edd16368SStephen M. Cameron 		pci_free_consistent(h->pdev,
4625edd16368SStephen M. Cameron 			sizeof(*c), c, cmd_dma_handle);
4626edd16368SStephen M. Cameron 		return NULL;
4627edd16368SStephen M. Cameron 	}
4628edd16368SStephen M. Cameron 
46299e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&c->list);
463001a02ffcSStephen M. Cameron 	c->busaddr = (u32) cmd_dma_handle;
463150a0decfSStephen M. Cameron 	c->ErrDesc.Addr = cpu_to_le64(err_dma_handle);
463250a0decfSStephen M. Cameron 	c->ErrDesc.Len = cpu_to_le32(sizeof(*c->err_info));
4633edd16368SStephen M. Cameron 
4634edd16368SStephen M. Cameron 	c->h = h;
4635edd16368SStephen M. Cameron 	return c;
4636edd16368SStephen M. Cameron }
4637edd16368SStephen M. Cameron 
4638edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c)
4639edd16368SStephen M. Cameron {
4640edd16368SStephen M. Cameron 	int i;
4641edd16368SStephen M. Cameron 
4642edd16368SStephen M. Cameron 	i = c - h->cmd_pool;
4643edd16368SStephen M. Cameron 	clear_bit(i & (BITS_PER_LONG - 1),
4644edd16368SStephen M. Cameron 		  h->cmd_pool_bits + (i / BITS_PER_LONG));
4645edd16368SStephen M. Cameron }
4646edd16368SStephen M. Cameron 
4647edd16368SStephen M. Cameron static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
4648edd16368SStephen M. Cameron {
4649edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev, sizeof(*c->err_info),
465050a0decfSStephen M. Cameron 			    c->err_info,
465150a0decfSStephen M. Cameron 			    (dma_addr_t) le64_to_cpu(c->ErrDesc.Addr));
4652edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev, sizeof(*c),
4653d896f3f3SStephen M. Cameron 			    c, (dma_addr_t) (c->busaddr & DIRECT_LOOKUP_MASK));
4654edd16368SStephen M. Cameron }
4655edd16368SStephen M. Cameron 
4656edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
4657edd16368SStephen M. Cameron 
465842a91641SDon Brace static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd,
465942a91641SDon Brace 	void __user *arg)
4660edd16368SStephen M. Cameron {
4661edd16368SStephen M. Cameron 	IOCTL32_Command_struct __user *arg32 =
4662edd16368SStephen M. Cameron 	    (IOCTL32_Command_struct __user *) arg;
4663edd16368SStephen M. Cameron 	IOCTL_Command_struct arg64;
4664edd16368SStephen M. Cameron 	IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
4665edd16368SStephen M. Cameron 	int err;
4666edd16368SStephen M. Cameron 	u32 cp;
4667edd16368SStephen M. Cameron 
4668938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
4669edd16368SStephen M. Cameron 	err = 0;
4670edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
4671edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
4672edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
4673edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
4674edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
4675edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
4676edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
4677edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
4678edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
4679edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
4680edd16368SStephen M. Cameron 
4681edd16368SStephen M. Cameron 	if (err)
4682edd16368SStephen M. Cameron 		return -EFAULT;
4683edd16368SStephen M. Cameron 
468442a91641SDon Brace 	err = hpsa_ioctl(dev, CCISS_PASSTHRU, p);
4685edd16368SStephen M. Cameron 	if (err)
4686edd16368SStephen M. Cameron 		return err;
4687edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
4688edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
4689edd16368SStephen M. Cameron 	if (err)
4690edd16368SStephen M. Cameron 		return -EFAULT;
4691edd16368SStephen M. Cameron 	return err;
4692edd16368SStephen M. Cameron }
4693edd16368SStephen M. Cameron 
4694edd16368SStephen M. Cameron static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
469542a91641SDon Brace 	int cmd, void __user *arg)
4696edd16368SStephen M. Cameron {
4697edd16368SStephen M. Cameron 	BIG_IOCTL32_Command_struct __user *arg32 =
4698edd16368SStephen M. Cameron 	    (BIG_IOCTL32_Command_struct __user *) arg;
4699edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct arg64;
4700edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct __user *p =
4701edd16368SStephen M. Cameron 	    compat_alloc_user_space(sizeof(arg64));
4702edd16368SStephen M. Cameron 	int err;
4703edd16368SStephen M. Cameron 	u32 cp;
4704edd16368SStephen M. Cameron 
4705938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
4706edd16368SStephen M. Cameron 	err = 0;
4707edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
4708edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
4709edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
4710edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
4711edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
4712edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
4713edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
4714edd16368SStephen M. Cameron 	err |= get_user(arg64.malloc_size, &arg32->malloc_size);
4715edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
4716edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
4717edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
4718edd16368SStephen M. Cameron 
4719edd16368SStephen M. Cameron 	if (err)
4720edd16368SStephen M. Cameron 		return -EFAULT;
4721edd16368SStephen M. Cameron 
472242a91641SDon Brace 	err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, p);
4723edd16368SStephen M. Cameron 	if (err)
4724edd16368SStephen M. Cameron 		return err;
4725edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
4726edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
4727edd16368SStephen M. Cameron 	if (err)
4728edd16368SStephen M. Cameron 		return -EFAULT;
4729edd16368SStephen M. Cameron 	return err;
4730edd16368SStephen M. Cameron }
473171fe75a7SStephen M. Cameron 
473242a91641SDon Brace static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
473371fe75a7SStephen M. Cameron {
473471fe75a7SStephen M. Cameron 	switch (cmd) {
473571fe75a7SStephen M. Cameron 	case CCISS_GETPCIINFO:
473671fe75a7SStephen M. Cameron 	case CCISS_GETINTINFO:
473771fe75a7SStephen M. Cameron 	case CCISS_SETINTINFO:
473871fe75a7SStephen M. Cameron 	case CCISS_GETNODENAME:
473971fe75a7SStephen M. Cameron 	case CCISS_SETNODENAME:
474071fe75a7SStephen M. Cameron 	case CCISS_GETHEARTBEAT:
474171fe75a7SStephen M. Cameron 	case CCISS_GETBUSTYPES:
474271fe75a7SStephen M. Cameron 	case CCISS_GETFIRMVER:
474371fe75a7SStephen M. Cameron 	case CCISS_GETDRIVVER:
474471fe75a7SStephen M. Cameron 	case CCISS_REVALIDVOLS:
474571fe75a7SStephen M. Cameron 	case CCISS_DEREGDISK:
474671fe75a7SStephen M. Cameron 	case CCISS_REGNEWDISK:
474771fe75a7SStephen M. Cameron 	case CCISS_REGNEWD:
474871fe75a7SStephen M. Cameron 	case CCISS_RESCANDISK:
474971fe75a7SStephen M. Cameron 	case CCISS_GETLUNINFO:
475071fe75a7SStephen M. Cameron 		return hpsa_ioctl(dev, cmd, arg);
475171fe75a7SStephen M. Cameron 
475271fe75a7SStephen M. Cameron 	case CCISS_PASSTHRU32:
475371fe75a7SStephen M. Cameron 		return hpsa_ioctl32_passthru(dev, cmd, arg);
475471fe75a7SStephen M. Cameron 	case CCISS_BIG_PASSTHRU32:
475571fe75a7SStephen M. Cameron 		return hpsa_ioctl32_big_passthru(dev, cmd, arg);
475671fe75a7SStephen M. Cameron 
475771fe75a7SStephen M. Cameron 	default:
475871fe75a7SStephen M. Cameron 		return -ENOIOCTLCMD;
475971fe75a7SStephen M. Cameron 	}
476071fe75a7SStephen M. Cameron }
4761edd16368SStephen M. Cameron #endif
4762edd16368SStephen M. Cameron 
4763edd16368SStephen M. Cameron static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
4764edd16368SStephen M. Cameron {
4765edd16368SStephen M. Cameron 	struct hpsa_pci_info pciinfo;
4766edd16368SStephen M. Cameron 
4767edd16368SStephen M. Cameron 	if (!argp)
4768edd16368SStephen M. Cameron 		return -EINVAL;
4769edd16368SStephen M. Cameron 	pciinfo.domain = pci_domain_nr(h->pdev->bus);
4770edd16368SStephen M. Cameron 	pciinfo.bus = h->pdev->bus->number;
4771edd16368SStephen M. Cameron 	pciinfo.dev_fn = h->pdev->devfn;
4772edd16368SStephen M. Cameron 	pciinfo.board_id = h->board_id;
4773edd16368SStephen M. Cameron 	if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
4774edd16368SStephen M. Cameron 		return -EFAULT;
4775edd16368SStephen M. Cameron 	return 0;
4776edd16368SStephen M. Cameron }
4777edd16368SStephen M. Cameron 
4778edd16368SStephen M. Cameron static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
4779edd16368SStephen M. Cameron {
4780edd16368SStephen M. Cameron 	DriverVer_type DriverVer;
4781edd16368SStephen M. Cameron 	unsigned char vmaj, vmin, vsubmin;
4782edd16368SStephen M. Cameron 	int rc;
4783edd16368SStephen M. Cameron 
4784edd16368SStephen M. Cameron 	rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
4785edd16368SStephen M. Cameron 		&vmaj, &vmin, &vsubmin);
4786edd16368SStephen M. Cameron 	if (rc != 3) {
4787edd16368SStephen M. Cameron 		dev_info(&h->pdev->dev, "driver version string '%s' "
4788edd16368SStephen M. Cameron 			"unrecognized.", HPSA_DRIVER_VERSION);
4789edd16368SStephen M. Cameron 		vmaj = 0;
4790edd16368SStephen M. Cameron 		vmin = 0;
4791edd16368SStephen M. Cameron 		vsubmin = 0;
4792edd16368SStephen M. Cameron 	}
4793edd16368SStephen M. Cameron 	DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
4794edd16368SStephen M. Cameron 	if (!argp)
4795edd16368SStephen M. Cameron 		return -EINVAL;
4796edd16368SStephen M. Cameron 	if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
4797edd16368SStephen M. Cameron 		return -EFAULT;
4798edd16368SStephen M. Cameron 	return 0;
4799edd16368SStephen M. Cameron }
4800edd16368SStephen M. Cameron 
4801edd16368SStephen M. Cameron static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
4802edd16368SStephen M. Cameron {
4803edd16368SStephen M. Cameron 	IOCTL_Command_struct iocommand;
4804edd16368SStephen M. Cameron 	struct CommandList *c;
4805edd16368SStephen M. Cameron 	char *buff = NULL;
480650a0decfSStephen M. Cameron 	u64 temp64;
4807c1f63c8fSStephen M. Cameron 	int rc = 0;
4808edd16368SStephen M. Cameron 
4809edd16368SStephen M. Cameron 	if (!argp)
4810edd16368SStephen M. Cameron 		return -EINVAL;
4811edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
4812edd16368SStephen M. Cameron 		return -EPERM;
4813edd16368SStephen M. Cameron 	if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
4814edd16368SStephen M. Cameron 		return -EFAULT;
4815edd16368SStephen M. Cameron 	if ((iocommand.buf_size < 1) &&
4816edd16368SStephen M. Cameron 	    (iocommand.Request.Type.Direction != XFER_NONE)) {
4817edd16368SStephen M. Cameron 		return -EINVAL;
4818edd16368SStephen M. Cameron 	}
4819edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
4820edd16368SStephen M. Cameron 		buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
4821edd16368SStephen M. Cameron 		if (buff == NULL)
4822edd16368SStephen M. Cameron 			return -EFAULT;
48239233fb10SStephen M. Cameron 		if (iocommand.Request.Type.Direction & XFER_WRITE) {
4824edd16368SStephen M. Cameron 			/* Copy the data into the buffer we created */
4825b03a7771SStephen M. Cameron 			if (copy_from_user(buff, iocommand.buf,
4826b03a7771SStephen M. Cameron 				iocommand.buf_size)) {
4827c1f63c8fSStephen M. Cameron 				rc = -EFAULT;
4828c1f63c8fSStephen M. Cameron 				goto out_kfree;
4829edd16368SStephen M. Cameron 			}
4830b03a7771SStephen M. Cameron 		} else {
4831edd16368SStephen M. Cameron 			memset(buff, 0, iocommand.buf_size);
4832b03a7771SStephen M. Cameron 		}
4833b03a7771SStephen M. Cameron 	}
4834edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
4835edd16368SStephen M. Cameron 	if (c == NULL) {
4836c1f63c8fSStephen M. Cameron 		rc = -ENOMEM;
4837c1f63c8fSStephen M. Cameron 		goto out_kfree;
4838edd16368SStephen M. Cameron 	}
4839edd16368SStephen M. Cameron 	/* Fill in the command type */
4840edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
4841edd16368SStephen M. Cameron 	/* Fill in Command Header */
4842edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0; /* unused in simple mode */
4843edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {	/* buffer to fill */
4844edd16368SStephen M. Cameron 		c->Header.SGList = 1;
484550a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(1);
4846edd16368SStephen M. Cameron 	} else	{ /* no buffers to fill */
4847edd16368SStephen M. Cameron 		c->Header.SGList = 0;
484850a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(0);
4849edd16368SStephen M. Cameron 	}
4850edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
4851edd16368SStephen M. Cameron 	/* use the kernel address the cmd block for tag */
48522b08b3e9SDon Brace 	c->Header.tag = cpu_to_le64(c->busaddr);
4853edd16368SStephen M. Cameron 
4854edd16368SStephen M. Cameron 	/* Fill in Request block */
4855edd16368SStephen M. Cameron 	memcpy(&c->Request, &iocommand.Request,
4856edd16368SStephen M. Cameron 		sizeof(c->Request));
4857edd16368SStephen M. Cameron 
4858edd16368SStephen M. Cameron 	/* Fill in the scatter gather information */
4859edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
486050a0decfSStephen M. Cameron 		temp64 = pci_map_single(h->pdev, buff,
4861edd16368SStephen M. Cameron 			iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
486250a0decfSStephen M. Cameron 		if (dma_mapping_error(&h->pdev->dev, (dma_addr_t) temp64)) {
486350a0decfSStephen M. Cameron 			c->SG[0].Addr = cpu_to_le64(0);
486450a0decfSStephen M. Cameron 			c->SG[0].Len = cpu_to_le32(0);
4865bcc48ffaSStephen M. Cameron 			rc = -ENOMEM;
4866bcc48ffaSStephen M. Cameron 			goto out;
4867bcc48ffaSStephen M. Cameron 		}
486850a0decfSStephen M. Cameron 		c->SG[0].Addr = cpu_to_le64(temp64);
486950a0decfSStephen M. Cameron 		c->SG[0].Len = cpu_to_le32(iocommand.buf_size);
487050a0decfSStephen M. Cameron 		c->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* not chaining */
4871edd16368SStephen M. Cameron 	}
4872a0c12413SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
4873c2dd32e0SStephen M. Cameron 	if (iocommand.buf_size > 0)
4874edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
4875edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
4876edd16368SStephen M. Cameron 
4877edd16368SStephen M. Cameron 	/* Copy the error information out */
4878edd16368SStephen M. Cameron 	memcpy(&iocommand.error_info, c->err_info,
4879edd16368SStephen M. Cameron 		sizeof(iocommand.error_info));
4880edd16368SStephen M. Cameron 	if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
4881c1f63c8fSStephen M. Cameron 		rc = -EFAULT;
4882c1f63c8fSStephen M. Cameron 		goto out;
4883edd16368SStephen M. Cameron 	}
48849233fb10SStephen M. Cameron 	if ((iocommand.Request.Type.Direction & XFER_READ) &&
4885b03a7771SStephen M. Cameron 		iocommand.buf_size > 0) {
4886edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
4887edd16368SStephen M. Cameron 		if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
4888c1f63c8fSStephen M. Cameron 			rc = -EFAULT;
4889c1f63c8fSStephen M. Cameron 			goto out;
4890edd16368SStephen M. Cameron 		}
4891edd16368SStephen M. Cameron 	}
4892c1f63c8fSStephen M. Cameron out:
4893edd16368SStephen M. Cameron 	cmd_special_free(h, c);
4894c1f63c8fSStephen M. Cameron out_kfree:
4895c1f63c8fSStephen M. Cameron 	kfree(buff);
4896c1f63c8fSStephen M. Cameron 	return rc;
4897edd16368SStephen M. Cameron }
4898edd16368SStephen M. Cameron 
4899edd16368SStephen M. Cameron static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
4900edd16368SStephen M. Cameron {
4901edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct *ioc;
4902edd16368SStephen M. Cameron 	struct CommandList *c;
4903edd16368SStephen M. Cameron 	unsigned char **buff = NULL;
4904edd16368SStephen M. Cameron 	int *buff_size = NULL;
490550a0decfSStephen M. Cameron 	u64 temp64;
4906edd16368SStephen M. Cameron 	BYTE sg_used = 0;
4907edd16368SStephen M. Cameron 	int status = 0;
490801a02ffcSStephen M. Cameron 	u32 left;
490901a02ffcSStephen M. Cameron 	u32 sz;
4910edd16368SStephen M. Cameron 	BYTE __user *data_ptr;
4911edd16368SStephen M. Cameron 
4912edd16368SStephen M. Cameron 	if (!argp)
4913edd16368SStephen M. Cameron 		return -EINVAL;
4914edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
4915edd16368SStephen M. Cameron 		return -EPERM;
4916edd16368SStephen M. Cameron 	ioc = (BIG_IOCTL_Command_struct *)
4917edd16368SStephen M. Cameron 	    kmalloc(sizeof(*ioc), GFP_KERNEL);
4918edd16368SStephen M. Cameron 	if (!ioc) {
4919edd16368SStephen M. Cameron 		status = -ENOMEM;
4920edd16368SStephen M. Cameron 		goto cleanup1;
4921edd16368SStephen M. Cameron 	}
4922edd16368SStephen M. Cameron 	if (copy_from_user(ioc, argp, sizeof(*ioc))) {
4923edd16368SStephen M. Cameron 		status = -EFAULT;
4924edd16368SStephen M. Cameron 		goto cleanup1;
4925edd16368SStephen M. Cameron 	}
4926edd16368SStephen M. Cameron 	if ((ioc->buf_size < 1) &&
4927edd16368SStephen M. Cameron 	    (ioc->Request.Type.Direction != XFER_NONE)) {
4928edd16368SStephen M. Cameron 		status = -EINVAL;
4929edd16368SStephen M. Cameron 		goto cleanup1;
4930edd16368SStephen M. Cameron 	}
4931edd16368SStephen M. Cameron 	/* Check kmalloc limits  using all SGs */
4932edd16368SStephen M. Cameron 	if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
4933edd16368SStephen M. Cameron 		status = -EINVAL;
4934edd16368SStephen M. Cameron 		goto cleanup1;
4935edd16368SStephen M. Cameron 	}
4936d66ae08bSStephen M. Cameron 	if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
4937edd16368SStephen M. Cameron 		status = -EINVAL;
4938edd16368SStephen M. Cameron 		goto cleanup1;
4939edd16368SStephen M. Cameron 	}
4940d66ae08bSStephen M. Cameron 	buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
4941edd16368SStephen M. Cameron 	if (!buff) {
4942edd16368SStephen M. Cameron 		status = -ENOMEM;
4943edd16368SStephen M. Cameron 		goto cleanup1;
4944edd16368SStephen M. Cameron 	}
4945d66ae08bSStephen M. Cameron 	buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
4946edd16368SStephen M. Cameron 	if (!buff_size) {
4947edd16368SStephen M. Cameron 		status = -ENOMEM;
4948edd16368SStephen M. Cameron 		goto cleanup1;
4949edd16368SStephen M. Cameron 	}
4950edd16368SStephen M. Cameron 	left = ioc->buf_size;
4951edd16368SStephen M. Cameron 	data_ptr = ioc->buf;
4952edd16368SStephen M. Cameron 	while (left) {
4953edd16368SStephen M. Cameron 		sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
4954edd16368SStephen M. Cameron 		buff_size[sg_used] = sz;
4955edd16368SStephen M. Cameron 		buff[sg_used] = kmalloc(sz, GFP_KERNEL);
4956edd16368SStephen M. Cameron 		if (buff[sg_used] == NULL) {
4957edd16368SStephen M. Cameron 			status = -ENOMEM;
4958edd16368SStephen M. Cameron 			goto cleanup1;
4959edd16368SStephen M. Cameron 		}
49609233fb10SStephen M. Cameron 		if (ioc->Request.Type.Direction & XFER_WRITE) {
4961edd16368SStephen M. Cameron 			if (copy_from_user(buff[sg_used], data_ptr, sz)) {
49620758f4f7SStephen M. Cameron 				status = -EFAULT;
4963edd16368SStephen M. Cameron 				goto cleanup1;
4964edd16368SStephen M. Cameron 			}
4965edd16368SStephen M. Cameron 		} else
4966edd16368SStephen M. Cameron 			memset(buff[sg_used], 0, sz);
4967edd16368SStephen M. Cameron 		left -= sz;
4968edd16368SStephen M. Cameron 		data_ptr += sz;
4969edd16368SStephen M. Cameron 		sg_used++;
4970edd16368SStephen M. Cameron 	}
4971edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
4972edd16368SStephen M. Cameron 	if (c == NULL) {
4973edd16368SStephen M. Cameron 		status = -ENOMEM;
4974edd16368SStephen M. Cameron 		goto cleanup1;
4975edd16368SStephen M. Cameron 	}
4976edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
4977edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
497850a0decfSStephen M. Cameron 	c->Header.SGList = (u8) sg_used;
497950a0decfSStephen M. Cameron 	c->Header.SGTotal = cpu_to_le16(sg_used);
4980edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
49812b08b3e9SDon Brace 	c->Header.tag = cpu_to_le64(c->busaddr);
4982edd16368SStephen M. Cameron 	memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
4983edd16368SStephen M. Cameron 	if (ioc->buf_size > 0) {
4984edd16368SStephen M. Cameron 		int i;
4985edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
498650a0decfSStephen M. Cameron 			temp64 = pci_map_single(h->pdev, buff[i],
4987edd16368SStephen M. Cameron 				    buff_size[i], PCI_DMA_BIDIRECTIONAL);
498850a0decfSStephen M. Cameron 			if (dma_mapping_error(&h->pdev->dev,
498950a0decfSStephen M. Cameron 							(dma_addr_t) temp64)) {
499050a0decfSStephen M. Cameron 				c->SG[i].Addr = cpu_to_le64(0);
499150a0decfSStephen M. Cameron 				c->SG[i].Len = cpu_to_le32(0);
4992bcc48ffaSStephen M. Cameron 				hpsa_pci_unmap(h->pdev, c, i,
4993bcc48ffaSStephen M. Cameron 					PCI_DMA_BIDIRECTIONAL);
4994bcc48ffaSStephen M. Cameron 				status = -ENOMEM;
4995e2d4a1f6SStephen M. Cameron 				goto cleanup0;
4996bcc48ffaSStephen M. Cameron 			}
499750a0decfSStephen M. Cameron 			c->SG[i].Addr = cpu_to_le64(temp64);
499850a0decfSStephen M. Cameron 			c->SG[i].Len = cpu_to_le32(buff_size[i]);
499950a0decfSStephen M. Cameron 			c->SG[i].Ext = cpu_to_le32(0);
5000edd16368SStephen M. Cameron 		}
500150a0decfSStephen M. Cameron 		c->SG[--i].Ext = cpu_to_le32(HPSA_SG_LAST);
5002edd16368SStephen M. Cameron 	}
5003a0c12413SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
5004b03a7771SStephen M. Cameron 	if (sg_used)
5005edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
5006edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
5007edd16368SStephen M. Cameron 	/* Copy the error information out */
5008edd16368SStephen M. Cameron 	memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
5009edd16368SStephen M. Cameron 	if (copy_to_user(argp, ioc, sizeof(*ioc))) {
5010edd16368SStephen M. Cameron 		status = -EFAULT;
5011e2d4a1f6SStephen M. Cameron 		goto cleanup0;
5012edd16368SStephen M. Cameron 	}
50139233fb10SStephen M. Cameron 	if ((ioc->Request.Type.Direction & XFER_READ) && ioc->buf_size > 0) {
50142b08b3e9SDon Brace 		int i;
50152b08b3e9SDon Brace 
5016edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
5017edd16368SStephen M. Cameron 		BYTE __user *ptr = ioc->buf;
5018edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
5019edd16368SStephen M. Cameron 			if (copy_to_user(ptr, buff[i], buff_size[i])) {
5020edd16368SStephen M. Cameron 				status = -EFAULT;
5021e2d4a1f6SStephen M. Cameron 				goto cleanup0;
5022edd16368SStephen M. Cameron 			}
5023edd16368SStephen M. Cameron 			ptr += buff_size[i];
5024edd16368SStephen M. Cameron 		}
5025edd16368SStephen M. Cameron 	}
5026edd16368SStephen M. Cameron 	status = 0;
5027e2d4a1f6SStephen M. Cameron cleanup0:
5028e2d4a1f6SStephen M. Cameron 	cmd_special_free(h, c);
5029edd16368SStephen M. Cameron cleanup1:
5030edd16368SStephen M. Cameron 	if (buff) {
50312b08b3e9SDon Brace 		int i;
50322b08b3e9SDon Brace 
5033edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++)
5034edd16368SStephen M. Cameron 			kfree(buff[i]);
5035edd16368SStephen M. Cameron 		kfree(buff);
5036edd16368SStephen M. Cameron 	}
5037edd16368SStephen M. Cameron 	kfree(buff_size);
5038edd16368SStephen M. Cameron 	kfree(ioc);
5039edd16368SStephen M. Cameron 	return status;
5040edd16368SStephen M. Cameron }
5041edd16368SStephen M. Cameron 
5042edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
5043edd16368SStephen M. Cameron 	struct CommandList *c)
5044edd16368SStephen M. Cameron {
5045edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
5046edd16368SStephen M. Cameron 			c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
5047edd16368SStephen M. Cameron 		(void) check_for_unit_attention(h, c);
5048edd16368SStephen M. Cameron }
50490390f0c0SStephen M. Cameron 
50500390f0c0SStephen M. Cameron static int increment_passthru_count(struct ctlr_info *h)
50510390f0c0SStephen M. Cameron {
50520390f0c0SStephen M. Cameron 	unsigned long flags;
50530390f0c0SStephen M. Cameron 
50540390f0c0SStephen M. Cameron 	spin_lock_irqsave(&h->passthru_count_lock, flags);
50550390f0c0SStephen M. Cameron 	if (h->passthru_count >= HPSA_MAX_CONCURRENT_PASSTHRUS) {
50560390f0c0SStephen M. Cameron 		spin_unlock_irqrestore(&h->passthru_count_lock, flags);
50570390f0c0SStephen M. Cameron 		return -1;
50580390f0c0SStephen M. Cameron 	}
50590390f0c0SStephen M. Cameron 	h->passthru_count++;
50600390f0c0SStephen M. Cameron 	spin_unlock_irqrestore(&h->passthru_count_lock, flags);
50610390f0c0SStephen M. Cameron 	return 0;
50620390f0c0SStephen M. Cameron }
50630390f0c0SStephen M. Cameron 
50640390f0c0SStephen M. Cameron static void decrement_passthru_count(struct ctlr_info *h)
50650390f0c0SStephen M. Cameron {
50660390f0c0SStephen M. Cameron 	unsigned long flags;
50670390f0c0SStephen M. Cameron 
50680390f0c0SStephen M. Cameron 	spin_lock_irqsave(&h->passthru_count_lock, flags);
50690390f0c0SStephen M. Cameron 	if (h->passthru_count <= 0) {
50700390f0c0SStephen M. Cameron 		spin_unlock_irqrestore(&h->passthru_count_lock, flags);
50710390f0c0SStephen M. Cameron 		/* not expecting to get here. */
50720390f0c0SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Bug detected, passthru_count seems to be incorrect.\n");
50730390f0c0SStephen M. Cameron 		return;
50740390f0c0SStephen M. Cameron 	}
50750390f0c0SStephen M. Cameron 	h->passthru_count--;
50760390f0c0SStephen M. Cameron 	spin_unlock_irqrestore(&h->passthru_count_lock, flags);
50770390f0c0SStephen M. Cameron }
50780390f0c0SStephen M. Cameron 
5079edd16368SStephen M. Cameron /*
5080edd16368SStephen M. Cameron  * ioctl
5081edd16368SStephen M. Cameron  */
508242a91641SDon Brace static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
5083edd16368SStephen M. Cameron {
5084edd16368SStephen M. Cameron 	struct ctlr_info *h;
5085edd16368SStephen M. Cameron 	void __user *argp = (void __user *)arg;
50860390f0c0SStephen M. Cameron 	int rc;
5087edd16368SStephen M. Cameron 
5088edd16368SStephen M. Cameron 	h = sdev_to_hba(dev);
5089edd16368SStephen M. Cameron 
5090edd16368SStephen M. Cameron 	switch (cmd) {
5091edd16368SStephen M. Cameron 	case CCISS_DEREGDISK:
5092edd16368SStephen M. Cameron 	case CCISS_REGNEWDISK:
5093edd16368SStephen M. Cameron 	case CCISS_REGNEWD:
5094a08a8471SStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
5095edd16368SStephen M. Cameron 		return 0;
5096edd16368SStephen M. Cameron 	case CCISS_GETPCIINFO:
5097edd16368SStephen M. Cameron 		return hpsa_getpciinfo_ioctl(h, argp);
5098edd16368SStephen M. Cameron 	case CCISS_GETDRIVVER:
5099edd16368SStephen M. Cameron 		return hpsa_getdrivver_ioctl(h, argp);
5100edd16368SStephen M. Cameron 	case CCISS_PASSTHRU:
51010390f0c0SStephen M. Cameron 		if (increment_passthru_count(h))
51020390f0c0SStephen M. Cameron 			return -EAGAIN;
51030390f0c0SStephen M. Cameron 		rc = hpsa_passthru_ioctl(h, argp);
51040390f0c0SStephen M. Cameron 		decrement_passthru_count(h);
51050390f0c0SStephen M. Cameron 		return rc;
5106edd16368SStephen M. Cameron 	case CCISS_BIG_PASSTHRU:
51070390f0c0SStephen M. Cameron 		if (increment_passthru_count(h))
51080390f0c0SStephen M. Cameron 			return -EAGAIN;
51090390f0c0SStephen M. Cameron 		rc = hpsa_big_passthru_ioctl(h, argp);
51100390f0c0SStephen M. Cameron 		decrement_passthru_count(h);
51110390f0c0SStephen M. Cameron 		return rc;
5112edd16368SStephen M. Cameron 	default:
5113edd16368SStephen M. Cameron 		return -ENOTTY;
5114edd16368SStephen M. Cameron 	}
5115edd16368SStephen M. Cameron }
5116edd16368SStephen M. Cameron 
51176f039790SGreg Kroah-Hartman static int hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
51186f039790SGreg Kroah-Hartman 				u8 reset_type)
511964670ac8SStephen M. Cameron {
512064670ac8SStephen M. Cameron 	struct CommandList *c;
512164670ac8SStephen M. Cameron 
512264670ac8SStephen M. Cameron 	c = cmd_alloc(h);
512364670ac8SStephen M. Cameron 	if (!c)
512464670ac8SStephen M. Cameron 		return -ENOMEM;
5125a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map */
5126a2dac136SStephen M. Cameron 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
512764670ac8SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_MSG);
512864670ac8SStephen M. Cameron 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
512964670ac8SStephen M. Cameron 	c->waiting = NULL;
513064670ac8SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
513164670ac8SStephen M. Cameron 	/* Don't wait for completion, the reset won't complete.  Don't free
513264670ac8SStephen M. Cameron 	 * the command either.  This is the last command we will send before
513364670ac8SStephen M. Cameron 	 * re-initializing everything, so it doesn't matter and won't leak.
513464670ac8SStephen M. Cameron 	 */
513564670ac8SStephen M. Cameron 	return 0;
513664670ac8SStephen M. Cameron }
513764670ac8SStephen M. Cameron 
5138a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
5139b7bb24ebSStephen M. Cameron 	void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
5140edd16368SStephen M. Cameron 	int cmd_type)
5141edd16368SStephen M. Cameron {
5142edd16368SStephen M. Cameron 	int pci_dir = XFER_NONE;
514375167d2cSStephen M. Cameron 	struct CommandList *a; /* for commands to be aborted */
5144edd16368SStephen M. Cameron 
5145edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5146edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
5147edd16368SStephen M. Cameron 	if (buff != NULL && size > 0) {
5148edd16368SStephen M. Cameron 		c->Header.SGList = 1;
514950a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(1);
5150edd16368SStephen M. Cameron 	} else {
5151edd16368SStephen M. Cameron 		c->Header.SGList = 0;
515250a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(0);
5153edd16368SStephen M. Cameron 	}
51542b08b3e9SDon Brace 	c->Header.tag = cpu_to_le64(c->busaddr);
5155edd16368SStephen M. Cameron 	memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
5156edd16368SStephen M. Cameron 
5157edd16368SStephen M. Cameron 	if (cmd_type == TYPE_CMD) {
5158edd16368SStephen M. Cameron 		switch (cmd) {
5159edd16368SStephen M. Cameron 		case HPSA_INQUIRY:
5160edd16368SStephen M. Cameron 			/* are we trying to read a vital product page */
5161b7bb24ebSStephen M. Cameron 			if (page_code & VPD_PAGE) {
5162edd16368SStephen M. Cameron 				c->Request.CDB[1] = 0x01;
5163b7bb24ebSStephen M. Cameron 				c->Request.CDB[2] = (page_code & 0xff);
5164edd16368SStephen M. Cameron 			}
5165edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
5166a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5167a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5168edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5169edd16368SStephen M. Cameron 			c->Request.CDB[0] = HPSA_INQUIRY;
5170edd16368SStephen M. Cameron 			c->Request.CDB[4] = size & 0xFF;
5171edd16368SStephen M. Cameron 			break;
5172edd16368SStephen M. Cameron 		case HPSA_REPORT_LOG:
5173edd16368SStephen M. Cameron 		case HPSA_REPORT_PHYS:
5174edd16368SStephen M. Cameron 			/* Talking to controller so It's a physical command
5175edd16368SStephen M. Cameron 			   mode = 00 target = 0.  Nothing to write.
5176edd16368SStephen M. Cameron 			 */
5177edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
5178a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5179a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5180edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5181edd16368SStephen M. Cameron 			c->Request.CDB[0] = cmd;
5182edd16368SStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
5183edd16368SStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5184edd16368SStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5185edd16368SStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
5186edd16368SStephen M. Cameron 			break;
5187edd16368SStephen M. Cameron 		case HPSA_CACHE_FLUSH:
5188edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
5189a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5190a505b86fSStephen M. Cameron 					TYPE_ATTR_DIR(cmd_type,
5191a505b86fSStephen M. Cameron 						ATTR_SIMPLE, XFER_WRITE);
5192edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5193edd16368SStephen M. Cameron 			c->Request.CDB[0] = BMIC_WRITE;
5194edd16368SStephen M. Cameron 			c->Request.CDB[6] = BMIC_CACHE_FLUSH;
5195bb158eabSStephen M. Cameron 			c->Request.CDB[7] = (size >> 8) & 0xFF;
5196bb158eabSStephen M. Cameron 			c->Request.CDB[8] = size & 0xFF;
5197edd16368SStephen M. Cameron 			break;
5198edd16368SStephen M. Cameron 		case TEST_UNIT_READY:
5199edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
5200a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5201a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
5202edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5203edd16368SStephen M. Cameron 			break;
5204283b4a9bSStephen M. Cameron 		case HPSA_GET_RAID_MAP:
5205283b4a9bSStephen M. Cameron 			c->Request.CDBLen = 12;
5206a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5207a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5208283b4a9bSStephen M. Cameron 			c->Request.Timeout = 0;
5209283b4a9bSStephen M. Cameron 			c->Request.CDB[0] = HPSA_CISS_READ;
5210283b4a9bSStephen M. Cameron 			c->Request.CDB[1] = cmd;
5211283b4a9bSStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
5212283b4a9bSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5213283b4a9bSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5214283b4a9bSStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
5215283b4a9bSStephen M. Cameron 			break;
5216316b221aSStephen M. Cameron 		case BMIC_SENSE_CONTROLLER_PARAMETERS:
5217316b221aSStephen M. Cameron 			c->Request.CDBLen = 10;
5218a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5219a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5220316b221aSStephen M. Cameron 			c->Request.Timeout = 0;
5221316b221aSStephen M. Cameron 			c->Request.CDB[0] = BMIC_READ;
5222316b221aSStephen M. Cameron 			c->Request.CDB[6] = BMIC_SENSE_CONTROLLER_PARAMETERS;
5223316b221aSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5224316b221aSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5225316b221aSStephen M. Cameron 			break;
5226edd16368SStephen M. Cameron 		default:
5227edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
5228edd16368SStephen M. Cameron 			BUG();
5229a2dac136SStephen M. Cameron 			return -1;
5230edd16368SStephen M. Cameron 		}
5231edd16368SStephen M. Cameron 	} else if (cmd_type == TYPE_MSG) {
5232edd16368SStephen M. Cameron 		switch (cmd) {
5233edd16368SStephen M. Cameron 
5234edd16368SStephen M. Cameron 		case  HPSA_DEVICE_RESET_MSG:
5235edd16368SStephen M. Cameron 			c->Request.CDBLen = 16;
5236a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5237a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
5238edd16368SStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
523964670ac8SStephen M. Cameron 			memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
524064670ac8SStephen M. Cameron 			c->Request.CDB[0] =  cmd;
524121e89afdSStephen M. Cameron 			c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
5242edd16368SStephen M. Cameron 			/* If bytes 4-7 are zero, it means reset the */
5243edd16368SStephen M. Cameron 			/* LunID device */
5244edd16368SStephen M. Cameron 			c->Request.CDB[4] = 0x00;
5245edd16368SStephen M. Cameron 			c->Request.CDB[5] = 0x00;
5246edd16368SStephen M. Cameron 			c->Request.CDB[6] = 0x00;
5247edd16368SStephen M. Cameron 			c->Request.CDB[7] = 0x00;
5248edd16368SStephen M. Cameron 			break;
524975167d2cSStephen M. Cameron 		case  HPSA_ABORT_MSG:
525075167d2cSStephen M. Cameron 			a = buff;       /* point to command to be aborted */
52512b08b3e9SDon Brace 			dev_dbg(&h->pdev->dev,
52522b08b3e9SDon Brace 				"Abort Tag:0x%016llx request Tag:0x%016llx",
525350a0decfSStephen M. Cameron 				a->Header.tag, c->Header.tag);
525475167d2cSStephen M. Cameron 			c->Request.CDBLen = 16;
5255a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5256a505b86fSStephen M. Cameron 					TYPE_ATTR_DIR(cmd_type,
5257a505b86fSStephen M. Cameron 						ATTR_SIMPLE, XFER_WRITE);
525875167d2cSStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
525975167d2cSStephen M. Cameron 			c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
526075167d2cSStephen M. Cameron 			c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
526175167d2cSStephen M. Cameron 			c->Request.CDB[2] = 0x00; /* reserved */
526275167d2cSStephen M. Cameron 			c->Request.CDB[3] = 0x00; /* reserved */
526375167d2cSStephen M. Cameron 			/* Tag to abort goes in CDB[4]-CDB[11] */
52642b08b3e9SDon Brace 			memcpy(&c->Request.CDB[4], &a->Header.tag,
52652b08b3e9SDon Brace 				sizeof(a->Header.tag));
526675167d2cSStephen M. Cameron 			c->Request.CDB[12] = 0x00; /* reserved */
526775167d2cSStephen M. Cameron 			c->Request.CDB[13] = 0x00; /* reserved */
526875167d2cSStephen M. Cameron 			c->Request.CDB[14] = 0x00; /* reserved */
526975167d2cSStephen M. Cameron 			c->Request.CDB[15] = 0x00; /* reserved */
527075167d2cSStephen M. Cameron 		break;
5271edd16368SStephen M. Cameron 		default:
5272edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown message type %d\n",
5273edd16368SStephen M. Cameron 				cmd);
5274edd16368SStephen M. Cameron 			BUG();
5275edd16368SStephen M. Cameron 		}
5276edd16368SStephen M. Cameron 	} else {
5277edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
5278edd16368SStephen M. Cameron 		BUG();
5279edd16368SStephen M. Cameron 	}
5280edd16368SStephen M. Cameron 
5281a505b86fSStephen M. Cameron 	switch (GET_DIR(c->Request.type_attr_dir)) {
5282edd16368SStephen M. Cameron 	case XFER_READ:
5283edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_FROMDEVICE;
5284edd16368SStephen M. Cameron 		break;
5285edd16368SStephen M. Cameron 	case XFER_WRITE:
5286edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_TODEVICE;
5287edd16368SStephen M. Cameron 		break;
5288edd16368SStephen M. Cameron 	case XFER_NONE:
5289edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_NONE;
5290edd16368SStephen M. Cameron 		break;
5291edd16368SStephen M. Cameron 	default:
5292edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_BIDIRECTIONAL;
5293edd16368SStephen M. Cameron 	}
5294a2dac136SStephen M. Cameron 	if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
5295a2dac136SStephen M. Cameron 		return -1;
5296a2dac136SStephen M. Cameron 	return 0;
5297edd16368SStephen M. Cameron }
5298edd16368SStephen M. Cameron 
5299edd16368SStephen M. Cameron /*
5300edd16368SStephen M. Cameron  * Map (physical) PCI mem into (virtual) kernel space
5301edd16368SStephen M. Cameron  */
5302edd16368SStephen M. Cameron static void __iomem *remap_pci_mem(ulong base, ulong size)
5303edd16368SStephen M. Cameron {
5304edd16368SStephen M. Cameron 	ulong page_base = ((ulong) base) & PAGE_MASK;
5305edd16368SStephen M. Cameron 	ulong page_offs = ((ulong) base) - page_base;
5306088ba34cSStephen M. Cameron 	void __iomem *page_remapped = ioremap_nocache(page_base,
5307088ba34cSStephen M. Cameron 		page_offs + size);
5308edd16368SStephen M. Cameron 
5309edd16368SStephen M. Cameron 	return page_remapped ? (page_remapped + page_offs) : NULL;
5310edd16368SStephen M. Cameron }
5311edd16368SStephen M. Cameron 
5312edd16368SStephen M. Cameron /* Takes cmds off the submission queue and sends them to the hardware,
5313edd16368SStephen M. Cameron  * then puts them on the queue of cmds waiting for completion.
53140b57075dSStephen M. Cameron  * Assumes h->lock is held
5315edd16368SStephen M. Cameron  */
53160b57075dSStephen M. Cameron static void start_io(struct ctlr_info *h, unsigned long *flags)
5317edd16368SStephen M. Cameron {
5318edd16368SStephen M. Cameron 	struct CommandList *c;
5319edd16368SStephen M. Cameron 
53209e0fc764SStephen M. Cameron 	while (!list_empty(&h->reqQ)) {
53219e0fc764SStephen M. Cameron 		c = list_entry(h->reqQ.next, struct CommandList, list);
5322edd16368SStephen M. Cameron 		/* can't do anything if fifo is full */
5323edd16368SStephen M. Cameron 		if ((h->access.fifo_full(h))) {
5324396883e2SStephen M. Cameron 			h->fifo_recently_full = 1;
5325edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "fifo full\n");
5326edd16368SStephen M. Cameron 			break;
5327edd16368SStephen M. Cameron 		}
5328396883e2SStephen M. Cameron 		h->fifo_recently_full = 0;
5329edd16368SStephen M. Cameron 
5330edd16368SStephen M. Cameron 		/* Get the first entry from the Request Q */
5331edd16368SStephen M. Cameron 		removeQ(c);
5332edd16368SStephen M. Cameron 		h->Qdepth--;
5333edd16368SStephen M. Cameron 
5334edd16368SStephen M. Cameron 		/* Put job onto the completed Q */
5335edd16368SStephen M. Cameron 		addQ(&h->cmpQ, c);
53360cbf768eSStephen M. Cameron 		atomic_inc(&h->commands_outstanding);
53370b57075dSStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, *flags);
53380cbf768eSStephen M. Cameron 		/* Tell the controller execute command */
5339e16a33adSMatt Gates 		h->access.submit_command(h, c);
53400b57075dSStephen M. Cameron 		spin_lock_irqsave(&h->lock, *flags);
5341edd16368SStephen M. Cameron 	}
53420b57075dSStephen M. Cameron }
53430b57075dSStephen M. Cameron 
53440b57075dSStephen M. Cameron static void lock_and_start_io(struct ctlr_info *h)
53450b57075dSStephen M. Cameron {
53460b57075dSStephen M. Cameron 	unsigned long flags;
53470b57075dSStephen M. Cameron 
53480b57075dSStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
53490b57075dSStephen M. Cameron 	start_io(h, &flags);
5350e16a33adSMatt Gates 	spin_unlock_irqrestore(&h->lock, flags);
5351edd16368SStephen M. Cameron }
5352edd16368SStephen M. Cameron 
5353254f796bSMatt Gates static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
5354edd16368SStephen M. Cameron {
5355254f796bSMatt Gates 	return h->access.command_completed(h, q);
5356edd16368SStephen M. Cameron }
5357edd16368SStephen M. Cameron 
5358900c5440SStephen M. Cameron static inline bool interrupt_pending(struct ctlr_info *h)
5359edd16368SStephen M. Cameron {
5360edd16368SStephen M. Cameron 	return h->access.intr_pending(h);
5361edd16368SStephen M. Cameron }
5362edd16368SStephen M. Cameron 
5363edd16368SStephen M. Cameron static inline long interrupt_not_for_us(struct ctlr_info *h)
5364edd16368SStephen M. Cameron {
536510f66018SStephen M. Cameron 	return (h->access.intr_pending(h) == 0) ||
536610f66018SStephen M. Cameron 		(h->interrupts_enabled == 0);
5367edd16368SStephen M. Cameron }
5368edd16368SStephen M. Cameron 
536901a02ffcSStephen M. Cameron static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
537001a02ffcSStephen M. Cameron 	u32 raw_tag)
5371edd16368SStephen M. Cameron {
5372edd16368SStephen M. Cameron 	if (unlikely(tag_index >= h->nr_cmds)) {
5373edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
5374edd16368SStephen M. Cameron 		return 1;
5375edd16368SStephen M. Cameron 	}
5376edd16368SStephen M. Cameron 	return 0;
5377edd16368SStephen M. Cameron }
5378edd16368SStephen M. Cameron 
53795a3d16f5SStephen M. Cameron static inline void finish_cmd(struct CommandList *c)
5380edd16368SStephen M. Cameron {
5381e16a33adSMatt Gates 	unsigned long flags;
5382396883e2SStephen M. Cameron 	int io_may_be_stalled = 0;
5383396883e2SStephen M. Cameron 	struct ctlr_info *h = c->h;
53840cbf768eSStephen M. Cameron 	int count;
5385e16a33adSMatt Gates 
5386396883e2SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
5387edd16368SStephen M. Cameron 	removeQ(c);
5388396883e2SStephen M. Cameron 
5389396883e2SStephen M. Cameron 	/*
5390396883e2SStephen M. Cameron 	 * Check for possibly stalled i/o.
5391396883e2SStephen M. Cameron 	 *
5392396883e2SStephen M. Cameron 	 * If a fifo_full condition is encountered, requests will back up
5393396883e2SStephen M. Cameron 	 * in h->reqQ.  This queue is only emptied out by start_io which is
5394396883e2SStephen M. Cameron 	 * only called when a new i/o request comes in.  If no i/o's are
5395396883e2SStephen M. Cameron 	 * forthcoming, the i/o's in h->reqQ can get stuck.  So we call
5396396883e2SStephen M. Cameron 	 * start_io from here if we detect such a danger.
5397396883e2SStephen M. Cameron 	 *
5398396883e2SStephen M. Cameron 	 * Normally, we shouldn't hit this case, but pounding on the
5399396883e2SStephen M. Cameron 	 * CCISS_PASSTHRU ioctl can provoke it.  Only call start_io if
5400396883e2SStephen M. Cameron 	 * commands_outstanding is low.  We want to avoid calling
5401396883e2SStephen M. Cameron 	 * start_io from in here as much as possible, and esp. don't
5402396883e2SStephen M. Cameron 	 * want to get in a cycle where we call start_io every time
5403396883e2SStephen M. Cameron 	 * through here.
5404396883e2SStephen M. Cameron 	 */
54050cbf768eSStephen M. Cameron 	count = atomic_read(&h->commands_outstanding);
5406396883e2SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
54070cbf768eSStephen M. Cameron 	if (unlikely(h->fifo_recently_full) && count < 5)
54080cbf768eSStephen M. Cameron 		io_may_be_stalled = 1;
5409396883e2SStephen M. Cameron 
5410e85c5974SStephen M. Cameron 	dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
5411c349775eSScott Teel 	if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
5412c349775eSScott Teel 			|| c->cmd_type == CMD_IOACCEL2))
54131fb011fbSStephen M. Cameron 		complete_scsi_command(c);
5414edd16368SStephen M. Cameron 	else if (c->cmd_type == CMD_IOCTL_PEND)
5415edd16368SStephen M. Cameron 		complete(c->waiting);
5416396883e2SStephen M. Cameron 	if (unlikely(io_may_be_stalled))
54170b57075dSStephen M. Cameron 		lock_and_start_io(h);
5418edd16368SStephen M. Cameron }
5419edd16368SStephen M. Cameron 
5420a104c99fSStephen M. Cameron static inline u32 hpsa_tag_contains_index(u32 tag)
5421a104c99fSStephen M. Cameron {
5422a104c99fSStephen M. Cameron 	return tag & DIRECT_LOOKUP_BIT;
5423a104c99fSStephen M. Cameron }
5424a104c99fSStephen M. Cameron 
5425a104c99fSStephen M. Cameron static inline u32 hpsa_tag_to_index(u32 tag)
5426a104c99fSStephen M. Cameron {
5427a104c99fSStephen M. Cameron 	return tag >> DIRECT_LOOKUP_SHIFT;
5428a104c99fSStephen M. Cameron }
5429a104c99fSStephen M. Cameron 
5430a9a3a273SStephen M. Cameron 
5431a9a3a273SStephen M. Cameron static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
5432a104c99fSStephen M. Cameron {
5433a9a3a273SStephen M. Cameron #define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
5434a9a3a273SStephen M. Cameron #define HPSA_SIMPLE_ERROR_BITS 0x03
5435960a30e7SStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
5436a9a3a273SStephen M. Cameron 		return tag & ~HPSA_SIMPLE_ERROR_BITS;
5437a9a3a273SStephen M. Cameron 	return tag & ~HPSA_PERF_ERROR_BITS;
5438a104c99fSStephen M. Cameron }
5439a104c99fSStephen M. Cameron 
5440303932fdSDon Brace /* process completion of an indexed ("direct lookup") command */
54411d94f94dSStephen M. Cameron static inline void process_indexed_cmd(struct ctlr_info *h,
5442303932fdSDon Brace 	u32 raw_tag)
5443303932fdSDon Brace {
5444303932fdSDon Brace 	u32 tag_index;
5445303932fdSDon Brace 	struct CommandList *c;
5446303932fdSDon Brace 
5447303932fdSDon Brace 	tag_index = hpsa_tag_to_index(raw_tag);
54481d94f94dSStephen M. Cameron 	if (!bad_tag(h, tag_index, raw_tag)) {
5449303932fdSDon Brace 		c = h->cmd_pool + tag_index;
54505a3d16f5SStephen M. Cameron 		finish_cmd(c);
54511d94f94dSStephen M. Cameron 	}
5452303932fdSDon Brace }
5453303932fdSDon Brace 
5454303932fdSDon Brace /* process completion of a non-indexed command */
54551d94f94dSStephen M. Cameron static inline void process_nonindexed_cmd(struct ctlr_info *h,
5456303932fdSDon Brace 	u32 raw_tag)
5457303932fdSDon Brace {
5458303932fdSDon Brace 	u32 tag;
5459303932fdSDon Brace 	struct CommandList *c = NULL;
5460e16a33adSMatt Gates 	unsigned long flags;
5461303932fdSDon Brace 
5462a9a3a273SStephen M. Cameron 	tag = hpsa_tag_discard_error_bits(h, raw_tag);
5463e16a33adSMatt Gates 	spin_lock_irqsave(&h->lock, flags);
54649e0fc764SStephen M. Cameron 	list_for_each_entry(c, &h->cmpQ, list) {
5465303932fdSDon Brace 		if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
5466e16a33adSMatt Gates 			spin_unlock_irqrestore(&h->lock, flags);
54675a3d16f5SStephen M. Cameron 			finish_cmd(c);
54681d94f94dSStephen M. Cameron 			return;
5469303932fdSDon Brace 		}
5470303932fdSDon Brace 	}
5471e16a33adSMatt Gates 	spin_unlock_irqrestore(&h->lock, flags);
5472303932fdSDon Brace 	bad_tag(h, h->nr_cmds + 1, raw_tag);
5473303932fdSDon Brace }
5474303932fdSDon Brace 
547564670ac8SStephen M. Cameron /* Some controllers, like p400, will give us one interrupt
547664670ac8SStephen M. Cameron  * after a soft reset, even if we turned interrupts off.
547764670ac8SStephen M. Cameron  * Only need to check for this in the hpsa_xxx_discard_completions
547864670ac8SStephen M. Cameron  * functions.
547964670ac8SStephen M. Cameron  */
548064670ac8SStephen M. Cameron static int ignore_bogus_interrupt(struct ctlr_info *h)
548164670ac8SStephen M. Cameron {
548264670ac8SStephen M. Cameron 	if (likely(!reset_devices))
548364670ac8SStephen M. Cameron 		return 0;
548464670ac8SStephen M. Cameron 
548564670ac8SStephen M. Cameron 	if (likely(h->interrupts_enabled))
548664670ac8SStephen M. Cameron 		return 0;
548764670ac8SStephen M. Cameron 
548864670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
548964670ac8SStephen M. Cameron 		"(known firmware bug.)  Ignoring.\n");
549064670ac8SStephen M. Cameron 
549164670ac8SStephen M. Cameron 	return 1;
549264670ac8SStephen M. Cameron }
549364670ac8SStephen M. Cameron 
5494254f796bSMatt Gates /*
5495254f796bSMatt Gates  * Convert &h->q[x] (passed to interrupt handlers) back to h.
5496254f796bSMatt Gates  * Relies on (h-q[x] == x) being true for x such that
5497254f796bSMatt Gates  * 0 <= x < MAX_REPLY_QUEUES.
5498254f796bSMatt Gates  */
5499254f796bSMatt Gates static struct ctlr_info *queue_to_hba(u8 *queue)
550064670ac8SStephen M. Cameron {
5501254f796bSMatt Gates 	return container_of((queue - *queue), struct ctlr_info, q[0]);
5502254f796bSMatt Gates }
5503254f796bSMatt Gates 
5504254f796bSMatt Gates static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
5505254f796bSMatt Gates {
5506254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
5507254f796bSMatt Gates 	u8 q = *(u8 *) queue;
550864670ac8SStephen M. Cameron 	u32 raw_tag;
550964670ac8SStephen M. Cameron 
551064670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
551164670ac8SStephen M. Cameron 		return IRQ_NONE;
551264670ac8SStephen M. Cameron 
551364670ac8SStephen M. Cameron 	if (interrupt_not_for_us(h))
551464670ac8SStephen M. Cameron 		return IRQ_NONE;
5515a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
551664670ac8SStephen M. Cameron 	while (interrupt_pending(h)) {
5517254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
551864670ac8SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY)
5519254f796bSMatt Gates 			raw_tag = next_command(h, q);
552064670ac8SStephen M. Cameron 	}
552164670ac8SStephen M. Cameron 	return IRQ_HANDLED;
552264670ac8SStephen M. Cameron }
552364670ac8SStephen M. Cameron 
5524254f796bSMatt Gates static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
552564670ac8SStephen M. Cameron {
5526254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
552764670ac8SStephen M. Cameron 	u32 raw_tag;
5528254f796bSMatt Gates 	u8 q = *(u8 *) queue;
552964670ac8SStephen M. Cameron 
553064670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
553164670ac8SStephen M. Cameron 		return IRQ_NONE;
553264670ac8SStephen M. Cameron 
5533a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
5534254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
553564670ac8SStephen M. Cameron 	while (raw_tag != FIFO_EMPTY)
5536254f796bSMatt Gates 		raw_tag = next_command(h, q);
553764670ac8SStephen M. Cameron 	return IRQ_HANDLED;
553864670ac8SStephen M. Cameron }
553964670ac8SStephen M. Cameron 
5540254f796bSMatt Gates static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
5541edd16368SStephen M. Cameron {
5542254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba((u8 *) queue);
5543303932fdSDon Brace 	u32 raw_tag;
5544254f796bSMatt Gates 	u8 q = *(u8 *) queue;
5545edd16368SStephen M. Cameron 
5546edd16368SStephen M. Cameron 	if (interrupt_not_for_us(h))
5547edd16368SStephen M. Cameron 		return IRQ_NONE;
5548a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
554910f66018SStephen M. Cameron 	while (interrupt_pending(h)) {
5550254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
555110f66018SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY) {
55521d94f94dSStephen M. Cameron 			if (likely(hpsa_tag_contains_index(raw_tag)))
55531d94f94dSStephen M. Cameron 				process_indexed_cmd(h, raw_tag);
555410f66018SStephen M. Cameron 			else
55551d94f94dSStephen M. Cameron 				process_nonindexed_cmd(h, raw_tag);
5556254f796bSMatt Gates 			raw_tag = next_command(h, q);
555710f66018SStephen M. Cameron 		}
555810f66018SStephen M. Cameron 	}
555910f66018SStephen M. Cameron 	return IRQ_HANDLED;
556010f66018SStephen M. Cameron }
556110f66018SStephen M. Cameron 
5562254f796bSMatt Gates static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
556310f66018SStephen M. Cameron {
5564254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
556510f66018SStephen M. Cameron 	u32 raw_tag;
5566254f796bSMatt Gates 	u8 q = *(u8 *) queue;
556710f66018SStephen M. Cameron 
5568a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
5569254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
5570303932fdSDon Brace 	while (raw_tag != FIFO_EMPTY) {
55711d94f94dSStephen M. Cameron 		if (likely(hpsa_tag_contains_index(raw_tag)))
55721d94f94dSStephen M. Cameron 			process_indexed_cmd(h, raw_tag);
5573303932fdSDon Brace 		else
55741d94f94dSStephen M. Cameron 			process_nonindexed_cmd(h, raw_tag);
5575254f796bSMatt Gates 		raw_tag = next_command(h, q);
5576edd16368SStephen M. Cameron 	}
5577edd16368SStephen M. Cameron 	return IRQ_HANDLED;
5578edd16368SStephen M. Cameron }
5579edd16368SStephen M. Cameron 
5580a9a3a273SStephen M. Cameron /* Send a message CDB to the firmware. Careful, this only works
5581a9a3a273SStephen M. Cameron  * in simple mode, not performant mode due to the tag lookup.
5582a9a3a273SStephen M. Cameron  * We only ever use this immediately after a controller reset.
5583a9a3a273SStephen M. Cameron  */
55846f039790SGreg Kroah-Hartman static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
5585edd16368SStephen M. Cameron 			unsigned char type)
5586edd16368SStephen M. Cameron {
5587edd16368SStephen M. Cameron 	struct Command {
5588edd16368SStephen M. Cameron 		struct CommandListHeader CommandHeader;
5589edd16368SStephen M. Cameron 		struct RequestBlock Request;
5590edd16368SStephen M. Cameron 		struct ErrDescriptor ErrorDescriptor;
5591edd16368SStephen M. Cameron 	};
5592edd16368SStephen M. Cameron 	struct Command *cmd;
5593edd16368SStephen M. Cameron 	static const size_t cmd_sz = sizeof(*cmd) +
5594edd16368SStephen M. Cameron 					sizeof(cmd->ErrorDescriptor);
5595edd16368SStephen M. Cameron 	dma_addr_t paddr64;
55962b08b3e9SDon Brace 	__le32 paddr32;
55972b08b3e9SDon Brace 	u32 tag;
5598edd16368SStephen M. Cameron 	void __iomem *vaddr;
5599edd16368SStephen M. Cameron 	int i, err;
5600edd16368SStephen M. Cameron 
5601edd16368SStephen M. Cameron 	vaddr = pci_ioremap_bar(pdev, 0);
5602edd16368SStephen M. Cameron 	if (vaddr == NULL)
5603edd16368SStephen M. Cameron 		return -ENOMEM;
5604edd16368SStephen M. Cameron 
5605edd16368SStephen M. Cameron 	/* The Inbound Post Queue only accepts 32-bit physical addresses for the
5606edd16368SStephen M. Cameron 	 * CCISS commands, so they must be allocated from the lower 4GiB of
5607edd16368SStephen M. Cameron 	 * memory.
5608edd16368SStephen M. Cameron 	 */
5609edd16368SStephen M. Cameron 	err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
5610edd16368SStephen M. Cameron 	if (err) {
5611edd16368SStephen M. Cameron 		iounmap(vaddr);
5612edd16368SStephen M. Cameron 		return -ENOMEM;
5613edd16368SStephen M. Cameron 	}
5614edd16368SStephen M. Cameron 
5615edd16368SStephen M. Cameron 	cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
5616edd16368SStephen M. Cameron 	if (cmd == NULL) {
5617edd16368SStephen M. Cameron 		iounmap(vaddr);
5618edd16368SStephen M. Cameron 		return -ENOMEM;
5619edd16368SStephen M. Cameron 	}
5620edd16368SStephen M. Cameron 
5621edd16368SStephen M. Cameron 	/* This must fit, because of the 32-bit consistent DMA mask.  Also,
5622edd16368SStephen M. Cameron 	 * although there's no guarantee, we assume that the address is at
5623edd16368SStephen M. Cameron 	 * least 4-byte aligned (most likely, it's page-aligned).
5624edd16368SStephen M. Cameron 	 */
56252b08b3e9SDon Brace 	paddr32 = cpu_to_le32(paddr64);
5626edd16368SStephen M. Cameron 
5627edd16368SStephen M. Cameron 	cmd->CommandHeader.ReplyQueue = 0;
5628edd16368SStephen M. Cameron 	cmd->CommandHeader.SGList = 0;
562950a0decfSStephen M. Cameron 	cmd->CommandHeader.SGTotal = cpu_to_le16(0);
56302b08b3e9SDon Brace 	cmd->CommandHeader.tag = cpu_to_le64(paddr64);
5631edd16368SStephen M. Cameron 	memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
5632edd16368SStephen M. Cameron 
5633edd16368SStephen M. Cameron 	cmd->Request.CDBLen = 16;
5634a505b86fSStephen M. Cameron 	cmd->Request.type_attr_dir =
5635a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_MSG, ATTR_HEADOFQUEUE, XFER_NONE);
5636edd16368SStephen M. Cameron 	cmd->Request.Timeout = 0; /* Don't time out */
5637edd16368SStephen M. Cameron 	cmd->Request.CDB[0] = opcode;
5638edd16368SStephen M. Cameron 	cmd->Request.CDB[1] = type;
5639edd16368SStephen M. Cameron 	memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
564050a0decfSStephen M. Cameron 	cmd->ErrorDescriptor.Addr =
56412b08b3e9SDon Brace 			cpu_to_le64((le32_to_cpu(paddr32) + sizeof(*cmd)));
564250a0decfSStephen M. Cameron 	cmd->ErrorDescriptor.Len = cpu_to_le32(sizeof(struct ErrorInfo));
5643edd16368SStephen M. Cameron 
56442b08b3e9SDon Brace 	writel(le32_to_cpu(paddr32), vaddr + SA5_REQUEST_PORT_OFFSET);
5645edd16368SStephen M. Cameron 
5646edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
5647edd16368SStephen M. Cameron 		tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
56482b08b3e9SDon Brace 		if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr64)
5649edd16368SStephen M. Cameron 			break;
5650edd16368SStephen M. Cameron 		msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
5651edd16368SStephen M. Cameron 	}
5652edd16368SStephen M. Cameron 
5653edd16368SStephen M. Cameron 	iounmap(vaddr);
5654edd16368SStephen M. Cameron 
5655edd16368SStephen M. Cameron 	/* we leak the DMA buffer here ... no choice since the controller could
5656edd16368SStephen M. Cameron 	 *  still complete the command.
5657edd16368SStephen M. Cameron 	 */
5658edd16368SStephen M. Cameron 	if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
5659edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
5660edd16368SStephen M. Cameron 			opcode, type);
5661edd16368SStephen M. Cameron 		return -ETIMEDOUT;
5662edd16368SStephen M. Cameron 	}
5663edd16368SStephen M. Cameron 
5664edd16368SStephen M. Cameron 	pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
5665edd16368SStephen M. Cameron 
5666edd16368SStephen M. Cameron 	if (tag & HPSA_ERROR_BIT) {
5667edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
5668edd16368SStephen M. Cameron 			opcode, type);
5669edd16368SStephen M. Cameron 		return -EIO;
5670edd16368SStephen M. Cameron 	}
5671edd16368SStephen M. Cameron 
5672edd16368SStephen M. Cameron 	dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
5673edd16368SStephen M. Cameron 		opcode, type);
5674edd16368SStephen M. Cameron 	return 0;
5675edd16368SStephen M. Cameron }
5676edd16368SStephen M. Cameron 
5677edd16368SStephen M. Cameron #define hpsa_noop(p) hpsa_message(p, 3, 0)
5678edd16368SStephen M. Cameron 
56791df8552aSStephen M. Cameron static int hpsa_controller_hard_reset(struct pci_dev *pdev,
568042a91641SDon Brace 	void __iomem *vaddr, u32 use_doorbell)
5681edd16368SStephen M. Cameron {
5682edd16368SStephen M. Cameron 
56831df8552aSStephen M. Cameron 	if (use_doorbell) {
56841df8552aSStephen M. Cameron 		/* For everything after the P600, the PCI power state method
56851df8552aSStephen M. Cameron 		 * of resetting the controller doesn't work, so we have this
56861df8552aSStephen M. Cameron 		 * other way using the doorbell register.
5687edd16368SStephen M. Cameron 		 */
56881df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using doorbell to reset controller\n");
5689cf0b08d0SStephen M. Cameron 		writel(use_doorbell, vaddr + SA5_DOORBELL);
569085009239SStephen M. Cameron 
569100701a96SJustin Lindley 		/* PMC hardware guys tell us we need a 10 second delay after
569285009239SStephen M. Cameron 		 * doorbell reset and before any attempt to talk to the board
569385009239SStephen M. Cameron 		 * at all to ensure that this actually works and doesn't fall
569485009239SStephen M. Cameron 		 * over in some weird corner cases.
569585009239SStephen M. Cameron 		 */
569600701a96SJustin Lindley 		msleep(10000);
56971df8552aSStephen M. Cameron 	} else { /* Try to do it the PCI power state way */
5698edd16368SStephen M. Cameron 
5699edd16368SStephen M. Cameron 		/* Quoting from the Open CISS Specification: "The Power
5700edd16368SStephen M. Cameron 		 * Management Control/Status Register (CSR) controls the power
5701edd16368SStephen M. Cameron 		 * state of the device.  The normal operating state is D0,
5702edd16368SStephen M. Cameron 		 * CSR=00h.  The software off state is D3, CSR=03h.  To reset
57031df8552aSStephen M. Cameron 		 * the controller, place the interface device in D3 then to D0,
57041df8552aSStephen M. Cameron 		 * this causes a secondary PCI reset which will reset the
57051df8552aSStephen M. Cameron 		 * controller." */
5706edd16368SStephen M. Cameron 
57072662cab8SDon Brace 		int rc = 0;
57082662cab8SDon Brace 
57091df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using PCI PM to reset controller\n");
57102662cab8SDon Brace 
5711edd16368SStephen M. Cameron 		/* enter the D3hot power management state */
57122662cab8SDon Brace 		rc = pci_set_power_state(pdev, PCI_D3hot);
57132662cab8SDon Brace 		if (rc)
57142662cab8SDon Brace 			return rc;
5715edd16368SStephen M. Cameron 
5716edd16368SStephen M. Cameron 		msleep(500);
5717edd16368SStephen M. Cameron 
5718edd16368SStephen M. Cameron 		/* enter the D0 power management state */
57192662cab8SDon Brace 		rc = pci_set_power_state(pdev, PCI_D0);
57202662cab8SDon Brace 		if (rc)
57212662cab8SDon Brace 			return rc;
5722c4853efeSMike Miller 
5723c4853efeSMike Miller 		/*
5724c4853efeSMike Miller 		 * The P600 requires a small delay when changing states.
5725c4853efeSMike Miller 		 * Otherwise we may think the board did not reset and we bail.
5726c4853efeSMike Miller 		 * This for kdump only and is particular to the P600.
5727c4853efeSMike Miller 		 */
5728c4853efeSMike Miller 		msleep(500);
57291df8552aSStephen M. Cameron 	}
57301df8552aSStephen M. Cameron 	return 0;
57311df8552aSStephen M. Cameron }
57321df8552aSStephen M. Cameron 
57336f039790SGreg Kroah-Hartman static void init_driver_version(char *driver_version, int len)
5734580ada3cSStephen M. Cameron {
5735580ada3cSStephen M. Cameron 	memset(driver_version, 0, len);
5736f79cfec6SStephen M. Cameron 	strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
5737580ada3cSStephen M. Cameron }
5738580ada3cSStephen M. Cameron 
57396f039790SGreg Kroah-Hartman static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
5740580ada3cSStephen M. Cameron {
5741580ada3cSStephen M. Cameron 	char *driver_version;
5742580ada3cSStephen M. Cameron 	int i, size = sizeof(cfgtable->driver_version);
5743580ada3cSStephen M. Cameron 
5744580ada3cSStephen M. Cameron 	driver_version = kmalloc(size, GFP_KERNEL);
5745580ada3cSStephen M. Cameron 	if (!driver_version)
5746580ada3cSStephen M. Cameron 		return -ENOMEM;
5747580ada3cSStephen M. Cameron 
5748580ada3cSStephen M. Cameron 	init_driver_version(driver_version, size);
5749580ada3cSStephen M. Cameron 	for (i = 0; i < size; i++)
5750580ada3cSStephen M. Cameron 		writeb(driver_version[i], &cfgtable->driver_version[i]);
5751580ada3cSStephen M. Cameron 	kfree(driver_version);
5752580ada3cSStephen M. Cameron 	return 0;
5753580ada3cSStephen M. Cameron }
5754580ada3cSStephen M. Cameron 
57556f039790SGreg Kroah-Hartman static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
57566f039790SGreg Kroah-Hartman 					  unsigned char *driver_ver)
5757580ada3cSStephen M. Cameron {
5758580ada3cSStephen M. Cameron 	int i;
5759580ada3cSStephen M. Cameron 
5760580ada3cSStephen M. Cameron 	for (i = 0; i < sizeof(cfgtable->driver_version); i++)
5761580ada3cSStephen M. Cameron 		driver_ver[i] = readb(&cfgtable->driver_version[i]);
5762580ada3cSStephen M. Cameron }
5763580ada3cSStephen M. Cameron 
57646f039790SGreg Kroah-Hartman static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
5765580ada3cSStephen M. Cameron {
5766580ada3cSStephen M. Cameron 
5767580ada3cSStephen M. Cameron 	char *driver_ver, *old_driver_ver;
5768580ada3cSStephen M. Cameron 	int rc, size = sizeof(cfgtable->driver_version);
5769580ada3cSStephen M. Cameron 
5770580ada3cSStephen M. Cameron 	old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
5771580ada3cSStephen M. Cameron 	if (!old_driver_ver)
5772580ada3cSStephen M. Cameron 		return -ENOMEM;
5773580ada3cSStephen M. Cameron 	driver_ver = old_driver_ver + size;
5774580ada3cSStephen M. Cameron 
5775580ada3cSStephen M. Cameron 	/* After a reset, the 32 bytes of "driver version" in the cfgtable
5776580ada3cSStephen M. Cameron 	 * should have been changed, otherwise we know the reset failed.
5777580ada3cSStephen M. Cameron 	 */
5778580ada3cSStephen M. Cameron 	init_driver_version(old_driver_ver, size);
5779580ada3cSStephen M. Cameron 	read_driver_ver_from_cfgtable(cfgtable, driver_ver);
5780580ada3cSStephen M. Cameron 	rc = !memcmp(driver_ver, old_driver_ver, size);
5781580ada3cSStephen M. Cameron 	kfree(old_driver_ver);
5782580ada3cSStephen M. Cameron 	return rc;
5783580ada3cSStephen M. Cameron }
57841df8552aSStephen M. Cameron /* This does a hard reset of the controller using PCI power management
57851df8552aSStephen M. Cameron  * states or the using the doorbell register.
57861df8552aSStephen M. Cameron  */
57876f039790SGreg Kroah-Hartman static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev)
57881df8552aSStephen M. Cameron {
57891df8552aSStephen M. Cameron 	u64 cfg_offset;
57901df8552aSStephen M. Cameron 	u32 cfg_base_addr;
57911df8552aSStephen M. Cameron 	u64 cfg_base_addr_index;
57921df8552aSStephen M. Cameron 	void __iomem *vaddr;
57931df8552aSStephen M. Cameron 	unsigned long paddr;
5794580ada3cSStephen M. Cameron 	u32 misc_fw_support;
5795270d05deSStephen M. Cameron 	int rc;
57961df8552aSStephen M. Cameron 	struct CfgTable __iomem *cfgtable;
5797cf0b08d0SStephen M. Cameron 	u32 use_doorbell;
579818867659SStephen M. Cameron 	u32 board_id;
5799270d05deSStephen M. Cameron 	u16 command_register;
58001df8552aSStephen M. Cameron 
58011df8552aSStephen M. Cameron 	/* For controllers as old as the P600, this is very nearly
58021df8552aSStephen M. Cameron 	 * the same thing as
58031df8552aSStephen M. Cameron 	 *
58041df8552aSStephen M. Cameron 	 * pci_save_state(pci_dev);
58051df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D3hot);
58061df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D0);
58071df8552aSStephen M. Cameron 	 * pci_restore_state(pci_dev);
58081df8552aSStephen M. Cameron 	 *
58091df8552aSStephen M. Cameron 	 * For controllers newer than the P600, the pci power state
58101df8552aSStephen M. Cameron 	 * method of resetting doesn't work so we have another way
58111df8552aSStephen M. Cameron 	 * using the doorbell register.
58121df8552aSStephen M. Cameron 	 */
581318867659SStephen M. Cameron 
581425c1e56aSStephen M. Cameron 	rc = hpsa_lookup_board_id(pdev, &board_id);
581546380786SStephen M. Cameron 	if (rc < 0 || !ctlr_is_resettable(board_id)) {
581625c1e56aSStephen M. Cameron 		dev_warn(&pdev->dev, "Not resetting device.\n");
581725c1e56aSStephen M. Cameron 		return -ENODEV;
581825c1e56aSStephen M. Cameron 	}
581946380786SStephen M. Cameron 
582046380786SStephen M. Cameron 	/* if controller is soft- but not hard resettable... */
582146380786SStephen M. Cameron 	if (!ctlr_is_hard_resettable(board_id))
582246380786SStephen M. Cameron 		return -ENOTSUPP; /* try soft reset later. */
582318867659SStephen M. Cameron 
5824270d05deSStephen M. Cameron 	/* Save the PCI command register */
5825270d05deSStephen M. Cameron 	pci_read_config_word(pdev, 4, &command_register);
5826270d05deSStephen M. Cameron 	pci_save_state(pdev);
58271df8552aSStephen M. Cameron 
58281df8552aSStephen M. Cameron 	/* find the first memory BAR, so we can find the cfg table */
58291df8552aSStephen M. Cameron 	rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
58301df8552aSStephen M. Cameron 	if (rc)
58311df8552aSStephen M. Cameron 		return rc;
58321df8552aSStephen M. Cameron 	vaddr = remap_pci_mem(paddr, 0x250);
58331df8552aSStephen M. Cameron 	if (!vaddr)
58341df8552aSStephen M. Cameron 		return -ENOMEM;
58351df8552aSStephen M. Cameron 
58361df8552aSStephen M. Cameron 	/* find cfgtable in order to check if reset via doorbell is supported */
58371df8552aSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
58381df8552aSStephen M. Cameron 					&cfg_base_addr_index, &cfg_offset);
58391df8552aSStephen M. Cameron 	if (rc)
58401df8552aSStephen M. Cameron 		goto unmap_vaddr;
58411df8552aSStephen M. Cameron 	cfgtable = remap_pci_mem(pci_resource_start(pdev,
58421df8552aSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
58431df8552aSStephen M. Cameron 	if (!cfgtable) {
58441df8552aSStephen M. Cameron 		rc = -ENOMEM;
58451df8552aSStephen M. Cameron 		goto unmap_vaddr;
58461df8552aSStephen M. Cameron 	}
5847580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(cfgtable);
5848580ada3cSStephen M. Cameron 	if (rc)
584903741d95STomas Henzl 		goto unmap_cfgtable;
58501df8552aSStephen M. Cameron 
5851cf0b08d0SStephen M. Cameron 	/* If reset via doorbell register is supported, use that.
5852cf0b08d0SStephen M. Cameron 	 * There are two such methods.  Favor the newest method.
5853cf0b08d0SStephen M. Cameron 	 */
58541df8552aSStephen M. Cameron 	misc_fw_support = readl(&cfgtable->misc_fw_support);
5855cf0b08d0SStephen M. Cameron 	use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
5856cf0b08d0SStephen M. Cameron 	if (use_doorbell) {
5857cf0b08d0SStephen M. Cameron 		use_doorbell = DOORBELL_CTLR_RESET2;
5858cf0b08d0SStephen M. Cameron 	} else {
58591df8552aSStephen M. Cameron 		use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
5860cf0b08d0SStephen M. Cameron 		if (use_doorbell) {
5861fba63097SMike Miller 			dev_warn(&pdev->dev, "Soft reset not supported. "
5862fba63097SMike Miller 				"Firmware update is required.\n");
586364670ac8SStephen M. Cameron 			rc = -ENOTSUPP; /* try soft reset */
5864cf0b08d0SStephen M. Cameron 			goto unmap_cfgtable;
5865cf0b08d0SStephen M. Cameron 		}
5866cf0b08d0SStephen M. Cameron 	}
58671df8552aSStephen M. Cameron 
58681df8552aSStephen M. Cameron 	rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
58691df8552aSStephen M. Cameron 	if (rc)
58701df8552aSStephen M. Cameron 		goto unmap_cfgtable;
5871edd16368SStephen M. Cameron 
5872270d05deSStephen M. Cameron 	pci_restore_state(pdev);
5873270d05deSStephen M. Cameron 	pci_write_config_word(pdev, 4, command_register);
5874edd16368SStephen M. Cameron 
58751df8552aSStephen M. Cameron 	/* Some devices (notably the HP Smart Array 5i Controller)
58761df8552aSStephen M. Cameron 	   need a little pause here */
58771df8552aSStephen M. Cameron 	msleep(HPSA_POST_RESET_PAUSE_MSECS);
58781df8552aSStephen M. Cameron 
5879fe5389c8SStephen M. Cameron 	rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
5880fe5389c8SStephen M. Cameron 	if (rc) {
5881fe5389c8SStephen M. Cameron 		dev_warn(&pdev->dev,
588264670ac8SStephen M. Cameron 			"failed waiting for board to become ready "
588364670ac8SStephen M. Cameron 			"after hard reset\n");
5884fe5389c8SStephen M. Cameron 		goto unmap_cfgtable;
5885fe5389c8SStephen M. Cameron 	}
5886fe5389c8SStephen M. Cameron 
5887580ada3cSStephen M. Cameron 	rc = controller_reset_failed(vaddr);
5888580ada3cSStephen M. Cameron 	if (rc < 0)
5889580ada3cSStephen M. Cameron 		goto unmap_cfgtable;
5890580ada3cSStephen M. Cameron 	if (rc) {
589164670ac8SStephen M. Cameron 		dev_warn(&pdev->dev, "Unable to successfully reset "
589264670ac8SStephen M. Cameron 			"controller. Will try soft reset.\n");
589364670ac8SStephen M. Cameron 		rc = -ENOTSUPP;
5894580ada3cSStephen M. Cameron 	} else {
589564670ac8SStephen M. Cameron 		dev_info(&pdev->dev, "board ready after hard reset.\n");
58961df8552aSStephen M. Cameron 	}
58971df8552aSStephen M. Cameron 
58981df8552aSStephen M. Cameron unmap_cfgtable:
58991df8552aSStephen M. Cameron 	iounmap(cfgtable);
59001df8552aSStephen M. Cameron 
59011df8552aSStephen M. Cameron unmap_vaddr:
59021df8552aSStephen M. Cameron 	iounmap(vaddr);
59031df8552aSStephen M. Cameron 	return rc;
5904edd16368SStephen M. Cameron }
5905edd16368SStephen M. Cameron 
5906edd16368SStephen M. Cameron /*
5907edd16368SStephen M. Cameron  *  We cannot read the structure directly, for portability we must use
5908edd16368SStephen M. Cameron  *   the io functions.
5909edd16368SStephen M. Cameron  *   This is for debug only.
5910edd16368SStephen M. Cameron  */
591142a91641SDon Brace static void print_cfg_table(struct device *dev, struct CfgTable __iomem *tb)
5912edd16368SStephen M. Cameron {
591358f8665cSStephen M. Cameron #ifdef HPSA_DEBUG
5914edd16368SStephen M. Cameron 	int i;
5915edd16368SStephen M. Cameron 	char temp_name[17];
5916edd16368SStephen M. Cameron 
5917edd16368SStephen M. Cameron 	dev_info(dev, "Controller Configuration information\n");
5918edd16368SStephen M. Cameron 	dev_info(dev, "------------------------------------\n");
5919edd16368SStephen M. Cameron 	for (i = 0; i < 4; i++)
5920edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->Signature[i]));
5921edd16368SStephen M. Cameron 	temp_name[4] = '\0';
5922edd16368SStephen M. Cameron 	dev_info(dev, "   Signature = %s\n", temp_name);
5923edd16368SStephen M. Cameron 	dev_info(dev, "   Spec Number = %d\n", readl(&(tb->SpecValence)));
5924edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods supported = 0x%x\n",
5925edd16368SStephen M. Cameron 	       readl(&(tb->TransportSupport)));
5926edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods active = 0x%x\n",
5927edd16368SStephen M. Cameron 	       readl(&(tb->TransportActive)));
5928edd16368SStephen M. Cameron 	dev_info(dev, "   Requested transport Method = 0x%x\n",
5929edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.TransportRequest)));
5930edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Delay = 0x%x\n",
5931edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntDelay)));
5932edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Count = 0x%x\n",
5933edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntCount)));
5934edd16368SStephen M. Cameron 	dev_info(dev, "   Max outstanding commands = 0x%d\n",
5935edd16368SStephen M. Cameron 	       readl(&(tb->CmdsOutMax)));
5936edd16368SStephen M. Cameron 	dev_info(dev, "   Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
5937edd16368SStephen M. Cameron 	for (i = 0; i < 16; i++)
5938edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->ServerName[i]));
5939edd16368SStephen M. Cameron 	temp_name[16] = '\0';
5940edd16368SStephen M. Cameron 	dev_info(dev, "   Server Name = %s\n", temp_name);
5941edd16368SStephen M. Cameron 	dev_info(dev, "   Heartbeat Counter = 0x%x\n\n\n",
5942edd16368SStephen M. Cameron 		readl(&(tb->HeartBeat)));
5943edd16368SStephen M. Cameron #endif				/* HPSA_DEBUG */
594458f8665cSStephen M. Cameron }
5945edd16368SStephen M. Cameron 
5946edd16368SStephen M. Cameron static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
5947edd16368SStephen M. Cameron {
5948edd16368SStephen M. Cameron 	int i, offset, mem_type, bar_type;
5949edd16368SStephen M. Cameron 
5950edd16368SStephen M. Cameron 	if (pci_bar_addr == PCI_BASE_ADDRESS_0)	/* looking for BAR zero? */
5951edd16368SStephen M. Cameron 		return 0;
5952edd16368SStephen M. Cameron 	offset = 0;
5953edd16368SStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
5954edd16368SStephen M. Cameron 		bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
5955edd16368SStephen M. Cameron 		if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
5956edd16368SStephen M. Cameron 			offset += 4;
5957edd16368SStephen M. Cameron 		else {
5958edd16368SStephen M. Cameron 			mem_type = pci_resource_flags(pdev, i) &
5959edd16368SStephen M. Cameron 			    PCI_BASE_ADDRESS_MEM_TYPE_MASK;
5960edd16368SStephen M. Cameron 			switch (mem_type) {
5961edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_32:
5962edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_1M:
5963edd16368SStephen M. Cameron 				offset += 4;	/* 32 bit */
5964edd16368SStephen M. Cameron 				break;
5965edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_64:
5966edd16368SStephen M. Cameron 				offset += 8;
5967edd16368SStephen M. Cameron 				break;
5968edd16368SStephen M. Cameron 			default:	/* reserved in PCI 2.2 */
5969edd16368SStephen M. Cameron 				dev_warn(&pdev->dev,
5970edd16368SStephen M. Cameron 				       "base address is invalid\n");
5971edd16368SStephen M. Cameron 				return -1;
5972edd16368SStephen M. Cameron 				break;
5973edd16368SStephen M. Cameron 			}
5974edd16368SStephen M. Cameron 		}
5975edd16368SStephen M. Cameron 		if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
5976edd16368SStephen M. Cameron 			return i + 1;
5977edd16368SStephen M. Cameron 	}
5978edd16368SStephen M. Cameron 	return -1;
5979edd16368SStephen M. Cameron }
5980edd16368SStephen M. Cameron 
5981edd16368SStephen M. Cameron /* If MSI/MSI-X is supported by the kernel we will try to enable it on
5982edd16368SStephen M. Cameron  * controllers that are capable. If not, we use IO-APIC mode.
5983edd16368SStephen M. Cameron  */
5984edd16368SStephen M. Cameron 
59856f039790SGreg Kroah-Hartman static void hpsa_interrupt_mode(struct ctlr_info *h)
5986edd16368SStephen M. Cameron {
5987edd16368SStephen M. Cameron #ifdef CONFIG_PCI_MSI
5988254f796bSMatt Gates 	int err, i;
5989254f796bSMatt Gates 	struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
5990254f796bSMatt Gates 
5991254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++) {
5992254f796bSMatt Gates 		hpsa_msix_entries[i].vector = 0;
5993254f796bSMatt Gates 		hpsa_msix_entries[i].entry = i;
5994254f796bSMatt Gates 	}
5995edd16368SStephen M. Cameron 
5996edd16368SStephen M. Cameron 	/* Some boards advertise MSI but don't really support it */
59976b3f4c52SStephen M. Cameron 	if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
59986b3f4c52SStephen M. Cameron 	    (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
5999edd16368SStephen M. Cameron 		goto default_int_mode;
600055c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
600155c06c71SStephen M. Cameron 		dev_info(&h->pdev->dev, "MSIX\n");
6002eee0f03aSHannes Reinecke 		h->msix_vector = MAX_REPLY_QUEUES;
6003f89439bcSStephen M. Cameron 		if (h->msix_vector > num_online_cpus())
6004f89439bcSStephen M. Cameron 			h->msix_vector = num_online_cpus();
600518fce3c4SAlexander Gordeev 		err = pci_enable_msix_range(h->pdev, hpsa_msix_entries,
600618fce3c4SAlexander Gordeev 					    1, h->msix_vector);
600718fce3c4SAlexander Gordeev 		if (err < 0) {
600818fce3c4SAlexander Gordeev 			dev_warn(&h->pdev->dev, "MSI-X init failed %d\n", err);
600918fce3c4SAlexander Gordeev 			h->msix_vector = 0;
601018fce3c4SAlexander Gordeev 			goto single_msi_mode;
601118fce3c4SAlexander Gordeev 		} else if (err < h->msix_vector) {
601255c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
6013edd16368SStephen M. Cameron 			       "available\n", err);
6014eee0f03aSHannes Reinecke 		}
601518fce3c4SAlexander Gordeev 		h->msix_vector = err;
6016eee0f03aSHannes Reinecke 		for (i = 0; i < h->msix_vector; i++)
6017eee0f03aSHannes Reinecke 			h->intr[i] = hpsa_msix_entries[i].vector;
6018eee0f03aSHannes Reinecke 		return;
6019edd16368SStephen M. Cameron 	}
602018fce3c4SAlexander Gordeev single_msi_mode:
602155c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
602255c06c71SStephen M. Cameron 		dev_info(&h->pdev->dev, "MSI\n");
602355c06c71SStephen M. Cameron 		if (!pci_enable_msi(h->pdev))
6024edd16368SStephen M. Cameron 			h->msi_vector = 1;
6025edd16368SStephen M. Cameron 		else
602655c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "MSI init failed\n");
6027edd16368SStephen M. Cameron 	}
6028edd16368SStephen M. Cameron default_int_mode:
6029edd16368SStephen M. Cameron #endif				/* CONFIG_PCI_MSI */
6030edd16368SStephen M. Cameron 	/* if we get here we're going to use the default interrupt mode */
6031a9a3a273SStephen M. Cameron 	h->intr[h->intr_mode] = h->pdev->irq;
6032edd16368SStephen M. Cameron }
6033edd16368SStephen M. Cameron 
60346f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
6035e5c880d1SStephen M. Cameron {
6036e5c880d1SStephen M. Cameron 	int i;
6037e5c880d1SStephen M. Cameron 	u32 subsystem_vendor_id, subsystem_device_id;
6038e5c880d1SStephen M. Cameron 
6039e5c880d1SStephen M. Cameron 	subsystem_vendor_id = pdev->subsystem_vendor;
6040e5c880d1SStephen M. Cameron 	subsystem_device_id = pdev->subsystem_device;
6041e5c880d1SStephen M. Cameron 	*board_id = ((subsystem_device_id << 16) & 0xffff0000) |
6042e5c880d1SStephen M. Cameron 		    subsystem_vendor_id;
6043e5c880d1SStephen M. Cameron 
6044e5c880d1SStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(products); i++)
6045e5c880d1SStephen M. Cameron 		if (*board_id == products[i].board_id)
6046e5c880d1SStephen M. Cameron 			return i;
6047e5c880d1SStephen M. Cameron 
60486798cc0aSStephen M. Cameron 	if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
60496798cc0aSStephen M. Cameron 		subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
60506798cc0aSStephen M. Cameron 		!hpsa_allow_any) {
6051e5c880d1SStephen M. Cameron 		dev_warn(&pdev->dev, "unrecognized board ID: "
6052e5c880d1SStephen M. Cameron 			"0x%08x, ignoring.\n", *board_id);
6053e5c880d1SStephen M. Cameron 			return -ENODEV;
6054e5c880d1SStephen M. Cameron 	}
6055e5c880d1SStephen M. Cameron 	return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
6056e5c880d1SStephen M. Cameron }
6057e5c880d1SStephen M. Cameron 
60586f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
60593a7774ceSStephen M. Cameron 				    unsigned long *memory_bar)
60603a7774ceSStephen M. Cameron {
60613a7774ceSStephen M. Cameron 	int i;
60623a7774ceSStephen M. Cameron 
60633a7774ceSStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
606412d2cd47SStephen M. Cameron 		if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
60653a7774ceSStephen M. Cameron 			/* addressing mode bits already removed */
606612d2cd47SStephen M. Cameron 			*memory_bar = pci_resource_start(pdev, i);
606712d2cd47SStephen M. Cameron 			dev_dbg(&pdev->dev, "memory BAR = %lx\n",
60683a7774ceSStephen M. Cameron 				*memory_bar);
60693a7774ceSStephen M. Cameron 			return 0;
60703a7774ceSStephen M. Cameron 		}
607112d2cd47SStephen M. Cameron 	dev_warn(&pdev->dev, "no memory BAR found\n");
60723a7774ceSStephen M. Cameron 	return -ENODEV;
60733a7774ceSStephen M. Cameron }
60743a7774ceSStephen M. Cameron 
60756f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
60766f039790SGreg Kroah-Hartman 				     int wait_for_ready)
60772c4c8c8bSStephen M. Cameron {
6078fe5389c8SStephen M. Cameron 	int i, iterations;
60792c4c8c8bSStephen M. Cameron 	u32 scratchpad;
6080fe5389c8SStephen M. Cameron 	if (wait_for_ready)
6081fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_READY_ITERATIONS;
6082fe5389c8SStephen M. Cameron 	else
6083fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
60842c4c8c8bSStephen M. Cameron 
6085fe5389c8SStephen M. Cameron 	for (i = 0; i < iterations; i++) {
6086fe5389c8SStephen M. Cameron 		scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
6087fe5389c8SStephen M. Cameron 		if (wait_for_ready) {
60882c4c8c8bSStephen M. Cameron 			if (scratchpad == HPSA_FIRMWARE_READY)
60892c4c8c8bSStephen M. Cameron 				return 0;
6090fe5389c8SStephen M. Cameron 		} else {
6091fe5389c8SStephen M. Cameron 			if (scratchpad != HPSA_FIRMWARE_READY)
6092fe5389c8SStephen M. Cameron 				return 0;
6093fe5389c8SStephen M. Cameron 		}
60942c4c8c8bSStephen M. Cameron 		msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
60952c4c8c8bSStephen M. Cameron 	}
6096fe5389c8SStephen M. Cameron 	dev_warn(&pdev->dev, "board not ready, timed out.\n");
60972c4c8c8bSStephen M. Cameron 	return -ENODEV;
60982c4c8c8bSStephen M. Cameron }
60992c4c8c8bSStephen M. Cameron 
61006f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
61016f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
6102a51fd47fSStephen M. Cameron 			       u64 *cfg_offset)
6103a51fd47fSStephen M. Cameron {
6104a51fd47fSStephen M. Cameron 	*cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
6105a51fd47fSStephen M. Cameron 	*cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
6106a51fd47fSStephen M. Cameron 	*cfg_base_addr &= (u32) 0x0000ffff;
6107a51fd47fSStephen M. Cameron 	*cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
6108a51fd47fSStephen M. Cameron 	if (*cfg_base_addr_index == -1) {
6109a51fd47fSStephen M. Cameron 		dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
6110a51fd47fSStephen M. Cameron 		return -ENODEV;
6111a51fd47fSStephen M. Cameron 	}
6112a51fd47fSStephen M. Cameron 	return 0;
6113a51fd47fSStephen M. Cameron }
6114a51fd47fSStephen M. Cameron 
61156f039790SGreg Kroah-Hartman static int hpsa_find_cfgtables(struct ctlr_info *h)
6116edd16368SStephen M. Cameron {
611701a02ffcSStephen M. Cameron 	u64 cfg_offset;
611801a02ffcSStephen M. Cameron 	u32 cfg_base_addr;
611901a02ffcSStephen M. Cameron 	u64 cfg_base_addr_index;
6120303932fdSDon Brace 	u32 trans_offset;
6121a51fd47fSStephen M. Cameron 	int rc;
612277c4495cSStephen M. Cameron 
6123a51fd47fSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
6124a51fd47fSStephen M. Cameron 		&cfg_base_addr_index, &cfg_offset);
6125a51fd47fSStephen M. Cameron 	if (rc)
6126a51fd47fSStephen M. Cameron 		return rc;
612777c4495cSStephen M. Cameron 	h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
6128a51fd47fSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
612977c4495cSStephen M. Cameron 	if (!h->cfgtable)
613077c4495cSStephen M. Cameron 		return -ENOMEM;
6131580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(h->cfgtable);
6132580ada3cSStephen M. Cameron 	if (rc)
6133580ada3cSStephen M. Cameron 		return rc;
613477c4495cSStephen M. Cameron 	/* Find performant mode table. */
6135a51fd47fSStephen M. Cameron 	trans_offset = readl(&h->cfgtable->TransMethodOffset);
613677c4495cSStephen M. Cameron 	h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
613777c4495cSStephen M. Cameron 				cfg_base_addr_index)+cfg_offset+trans_offset,
613877c4495cSStephen M. Cameron 				sizeof(*h->transtable));
613977c4495cSStephen M. Cameron 	if (!h->transtable)
614077c4495cSStephen M. Cameron 		return -ENOMEM;
614177c4495cSStephen M. Cameron 	return 0;
614277c4495cSStephen M. Cameron }
614377c4495cSStephen M. Cameron 
61446f039790SGreg Kroah-Hartman static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
6145cba3d38bSStephen M. Cameron {
6146cba3d38bSStephen M. Cameron 	h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
614772ceeaecSStephen M. Cameron 
614872ceeaecSStephen M. Cameron 	/* Limit commands in memory limited kdump scenario. */
614972ceeaecSStephen M. Cameron 	if (reset_devices && h->max_commands > 32)
615072ceeaecSStephen M. Cameron 		h->max_commands = 32;
615172ceeaecSStephen M. Cameron 
6152cba3d38bSStephen M. Cameron 	if (h->max_commands < 16) {
6153cba3d38bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "Controller reports "
6154cba3d38bSStephen M. Cameron 			"max supported commands of %d, an obvious lie. "
6155cba3d38bSStephen M. Cameron 			"Using 16.  Ensure that firmware is up to date.\n",
6156cba3d38bSStephen M. Cameron 			h->max_commands);
6157cba3d38bSStephen M. Cameron 		h->max_commands = 16;
6158cba3d38bSStephen M. Cameron 	}
6159cba3d38bSStephen M. Cameron }
6160cba3d38bSStephen M. Cameron 
6161b93d7536SStephen M. Cameron /* Interrogate the hardware for some limits:
6162b93d7536SStephen M. Cameron  * max commands, max SG elements without chaining, and with chaining,
6163b93d7536SStephen M. Cameron  * SG chain block size, etc.
6164b93d7536SStephen M. Cameron  */
61656f039790SGreg Kroah-Hartman static void hpsa_find_board_params(struct ctlr_info *h)
6166b93d7536SStephen M. Cameron {
6167cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
6168b93d7536SStephen M. Cameron 	h->nr_cmds = h->max_commands - 4; /* Allow room for some ioctls */
6169b93d7536SStephen M. Cameron 	h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
6170283b4a9bSStephen M. Cameron 	h->fw_support = readl(&(h->cfgtable->misc_fw_support));
6171b93d7536SStephen M. Cameron 	/*
6172b93d7536SStephen M. Cameron 	 * Limit in-command s/g elements to 32 save dma'able memory.
6173b93d7536SStephen M. Cameron 	 * Howvever spec says if 0, use 31
6174b93d7536SStephen M. Cameron 	 */
6175b93d7536SStephen M. Cameron 	h->max_cmd_sg_entries = 31;
6176b93d7536SStephen M. Cameron 	if (h->maxsgentries > 512) {
6177b93d7536SStephen M. Cameron 		h->max_cmd_sg_entries = 32;
61781a63ea6fSWebb Scales 		h->chainsize = h->maxsgentries - h->max_cmd_sg_entries;
6179b93d7536SStephen M. Cameron 		h->maxsgentries--; /* save one for chain pointer */
6180b93d7536SStephen M. Cameron 	} else {
6181b93d7536SStephen M. Cameron 		h->chainsize = 0;
61821a63ea6fSWebb Scales 		h->maxsgentries = 31; /* default to traditional values */
6183b93d7536SStephen M. Cameron 	}
618475167d2cSStephen M. Cameron 
618575167d2cSStephen M. Cameron 	/* Find out what task management functions are supported and cache */
618675167d2cSStephen M. Cameron 	h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
61870e7a7fceSScott Teel 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
61880e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
61890e7a7fceSScott Teel 	if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
61900e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
6191b93d7536SStephen M. Cameron }
6192b93d7536SStephen M. Cameron 
619376c46e49SStephen M. Cameron static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
619476c46e49SStephen M. Cameron {
61950fc9fd40SAkinobu Mita 	if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
619676c46e49SStephen M. Cameron 		dev_warn(&h->pdev->dev, "not a valid CISS config table\n");
619776c46e49SStephen M. Cameron 		return false;
619876c46e49SStephen M. Cameron 	}
619976c46e49SStephen M. Cameron 	return true;
620076c46e49SStephen M. Cameron }
620176c46e49SStephen M. Cameron 
620297a5e98cSStephen M. Cameron static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
6203f7c39101SStephen M. Cameron {
620497a5e98cSStephen M. Cameron 	u32 driver_support;
6205f7c39101SStephen M. Cameron 
620697a5e98cSStephen M. Cameron 	driver_support = readl(&(h->cfgtable->driver_support));
62070b9e7b74SArnd Bergmann 	/* Need to enable prefetch in the SCSI core for 6400 in x86 */
62080b9e7b74SArnd Bergmann #ifdef CONFIG_X86
620997a5e98cSStephen M. Cameron 	driver_support |= ENABLE_SCSI_PREFETCH;
6210f7c39101SStephen M. Cameron #endif
621128e13446SStephen M. Cameron 	driver_support |= ENABLE_UNIT_ATTN;
621228e13446SStephen M. Cameron 	writel(driver_support, &(h->cfgtable->driver_support));
6213f7c39101SStephen M. Cameron }
6214f7c39101SStephen M. Cameron 
62153d0eab67SStephen M. Cameron /* Disable DMA prefetch for the P600.  Otherwise an ASIC bug may result
62163d0eab67SStephen M. Cameron  * in a prefetch beyond physical memory.
62173d0eab67SStephen M. Cameron  */
62183d0eab67SStephen M. Cameron static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
62193d0eab67SStephen M. Cameron {
62203d0eab67SStephen M. Cameron 	u32 dma_prefetch;
62213d0eab67SStephen M. Cameron 
62223d0eab67SStephen M. Cameron 	if (h->board_id != 0x3225103C)
62233d0eab67SStephen M. Cameron 		return;
62243d0eab67SStephen M. Cameron 	dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
62253d0eab67SStephen M. Cameron 	dma_prefetch |= 0x8000;
62263d0eab67SStephen M. Cameron 	writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
62273d0eab67SStephen M. Cameron }
62283d0eab67SStephen M. Cameron 
622976438d08SStephen M. Cameron static void hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
623076438d08SStephen M. Cameron {
623176438d08SStephen M. Cameron 	int i;
623276438d08SStephen M. Cameron 	u32 doorbell_value;
623376438d08SStephen M. Cameron 	unsigned long flags;
623476438d08SStephen M. Cameron 	/* wait until the clear_event_notify bit 6 is cleared by controller. */
623576438d08SStephen M. Cameron 	for (i = 0; i < MAX_CONFIG_WAIT; i++) {
623676438d08SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
623776438d08SStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
623876438d08SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
623976438d08SStephen M. Cameron 		if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
624076438d08SStephen M. Cameron 			break;
624176438d08SStephen M. Cameron 		/* delay and try again */
624276438d08SStephen M. Cameron 		msleep(20);
624376438d08SStephen M. Cameron 	}
624476438d08SStephen M. Cameron }
624576438d08SStephen M. Cameron 
62466f039790SGreg Kroah-Hartman static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
6247eb6b2ae9SStephen M. Cameron {
6248eb6b2ae9SStephen M. Cameron 	int i;
62496eaf46fdSStephen M. Cameron 	u32 doorbell_value;
62506eaf46fdSStephen M. Cameron 	unsigned long flags;
6251eb6b2ae9SStephen M. Cameron 
6252eb6b2ae9SStephen M. Cameron 	/* under certain very rare conditions, this can take awhile.
6253eb6b2ae9SStephen M. Cameron 	 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
6254eb6b2ae9SStephen M. Cameron 	 * as we enter this code.)
6255eb6b2ae9SStephen M. Cameron 	 */
6256eb6b2ae9SStephen M. Cameron 	for (i = 0; i < MAX_CONFIG_WAIT; i++) {
62576eaf46fdSStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
62586eaf46fdSStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
62596eaf46fdSStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
6260382be668SDan Carpenter 		if (!(doorbell_value & CFGTBL_ChangeReq))
6261eb6b2ae9SStephen M. Cameron 			break;
6262eb6b2ae9SStephen M. Cameron 		/* delay and try again */
626360d3f5b0SStephen M. Cameron 		usleep_range(10000, 20000);
6264eb6b2ae9SStephen M. Cameron 	}
62653f4336f3SStephen M. Cameron }
62663f4336f3SStephen M. Cameron 
62676f039790SGreg Kroah-Hartman static int hpsa_enter_simple_mode(struct ctlr_info *h)
62683f4336f3SStephen M. Cameron {
62693f4336f3SStephen M. Cameron 	u32 trans_support;
62703f4336f3SStephen M. Cameron 
62713f4336f3SStephen M. Cameron 	trans_support = readl(&(h->cfgtable->TransportSupport));
62723f4336f3SStephen M. Cameron 	if (!(trans_support & SIMPLE_MODE))
62733f4336f3SStephen M. Cameron 		return -ENOTSUPP;
62743f4336f3SStephen M. Cameron 
62753f4336f3SStephen M. Cameron 	h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
6276283b4a9bSStephen M. Cameron 
62773f4336f3SStephen M. Cameron 	/* Update the field, and then ring the doorbell */
62783f4336f3SStephen M. Cameron 	writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
6279b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
62803f4336f3SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
62813f4336f3SStephen M. Cameron 	hpsa_wait_for_mode_change_ack(h);
6282eb6b2ae9SStephen M. Cameron 	print_cfg_table(&h->pdev->dev, h->cfgtable);
6283283b4a9bSStephen M. Cameron 	if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
6284283b4a9bSStephen M. Cameron 		goto error;
6285960a30e7SStephen M. Cameron 	h->transMethod = CFGTBL_Trans_Simple;
6286eb6b2ae9SStephen M. Cameron 	return 0;
6287283b4a9bSStephen M. Cameron error:
6288283b4a9bSStephen M. Cameron 	dev_warn(&h->pdev->dev, "unable to get board into simple mode\n");
6289283b4a9bSStephen M. Cameron 	return -ENODEV;
6290eb6b2ae9SStephen M. Cameron }
6291eb6b2ae9SStephen M. Cameron 
62926f039790SGreg Kroah-Hartman static int hpsa_pci_init(struct ctlr_info *h)
629377c4495cSStephen M. Cameron {
6294eb6b2ae9SStephen M. Cameron 	int prod_index, err;
6295edd16368SStephen M. Cameron 
6296e5c880d1SStephen M. Cameron 	prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
6297e5c880d1SStephen M. Cameron 	if (prod_index < 0)
6298edd16368SStephen M. Cameron 		return -ENODEV;
6299e5c880d1SStephen M. Cameron 	h->product_name = products[prod_index].product_name;
6300e5c880d1SStephen M. Cameron 	h->access = *(products[prod_index].access);
6301e5c880d1SStephen M. Cameron 
6302e5a44df8SMatthew Garrett 	pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
6303e5a44df8SMatthew Garrett 			       PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
6304e5a44df8SMatthew Garrett 
630555c06c71SStephen M. Cameron 	err = pci_enable_device(h->pdev);
6306edd16368SStephen M. Cameron 	if (err) {
630755c06c71SStephen M. Cameron 		dev_warn(&h->pdev->dev, "unable to enable PCI device\n");
6308edd16368SStephen M. Cameron 		return err;
6309edd16368SStephen M. Cameron 	}
6310edd16368SStephen M. Cameron 
6311f79cfec6SStephen M. Cameron 	err = pci_request_regions(h->pdev, HPSA);
6312edd16368SStephen M. Cameron 	if (err) {
631355c06c71SStephen M. Cameron 		dev_err(&h->pdev->dev,
631455c06c71SStephen M. Cameron 			"cannot obtain PCI resources, aborting\n");
6315edd16368SStephen M. Cameron 		return err;
6316edd16368SStephen M. Cameron 	}
63174fa604e1SRobert Elliott 
63184fa604e1SRobert Elliott 	pci_set_master(h->pdev);
63194fa604e1SRobert Elliott 
63206b3f4c52SStephen M. Cameron 	hpsa_interrupt_mode(h);
632112d2cd47SStephen M. Cameron 	err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
63223a7774ceSStephen M. Cameron 	if (err)
6323edd16368SStephen M. Cameron 		goto err_out_free_res;
6324edd16368SStephen M. Cameron 	h->vaddr = remap_pci_mem(h->paddr, 0x250);
6325204892e9SStephen M. Cameron 	if (!h->vaddr) {
6326204892e9SStephen M. Cameron 		err = -ENOMEM;
6327204892e9SStephen M. Cameron 		goto err_out_free_res;
6328204892e9SStephen M. Cameron 	}
6329fe5389c8SStephen M. Cameron 	err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
63302c4c8c8bSStephen M. Cameron 	if (err)
6331edd16368SStephen M. Cameron 		goto err_out_free_res;
633277c4495cSStephen M. Cameron 	err = hpsa_find_cfgtables(h);
633377c4495cSStephen M. Cameron 	if (err)
6334edd16368SStephen M. Cameron 		goto err_out_free_res;
6335b93d7536SStephen M. Cameron 	hpsa_find_board_params(h);
6336edd16368SStephen M. Cameron 
633776c46e49SStephen M. Cameron 	if (!hpsa_CISS_signature_present(h)) {
6338edd16368SStephen M. Cameron 		err = -ENODEV;
6339edd16368SStephen M. Cameron 		goto err_out_free_res;
6340edd16368SStephen M. Cameron 	}
634197a5e98cSStephen M. Cameron 	hpsa_set_driver_support_bits(h);
63423d0eab67SStephen M. Cameron 	hpsa_p600_dma_prefetch_quirk(h);
6343eb6b2ae9SStephen M. Cameron 	err = hpsa_enter_simple_mode(h);
6344eb6b2ae9SStephen M. Cameron 	if (err)
6345edd16368SStephen M. Cameron 		goto err_out_free_res;
6346edd16368SStephen M. Cameron 	return 0;
6347edd16368SStephen M. Cameron 
6348edd16368SStephen M. Cameron err_out_free_res:
6349204892e9SStephen M. Cameron 	if (h->transtable)
6350204892e9SStephen M. Cameron 		iounmap(h->transtable);
6351204892e9SStephen M. Cameron 	if (h->cfgtable)
6352204892e9SStephen M. Cameron 		iounmap(h->cfgtable);
6353204892e9SStephen M. Cameron 	if (h->vaddr)
6354204892e9SStephen M. Cameron 		iounmap(h->vaddr);
6355f0bd0b68SStephen M. Cameron 	pci_disable_device(h->pdev);
635655c06c71SStephen M. Cameron 	pci_release_regions(h->pdev);
6357edd16368SStephen M. Cameron 	return err;
6358edd16368SStephen M. Cameron }
6359edd16368SStephen M. Cameron 
63606f039790SGreg Kroah-Hartman static void hpsa_hba_inquiry(struct ctlr_info *h)
6361339b2b14SStephen M. Cameron {
6362339b2b14SStephen M. Cameron 	int rc;
6363339b2b14SStephen M. Cameron 
6364339b2b14SStephen M. Cameron #define HBA_INQUIRY_BYTE_COUNT 64
6365339b2b14SStephen M. Cameron 	h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
6366339b2b14SStephen M. Cameron 	if (!h->hba_inquiry_data)
6367339b2b14SStephen M. Cameron 		return;
6368339b2b14SStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
6369339b2b14SStephen M. Cameron 		h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
6370339b2b14SStephen M. Cameron 	if (rc != 0) {
6371339b2b14SStephen M. Cameron 		kfree(h->hba_inquiry_data);
6372339b2b14SStephen M. Cameron 		h->hba_inquiry_data = NULL;
6373339b2b14SStephen M. Cameron 	}
6374339b2b14SStephen M. Cameron }
6375339b2b14SStephen M. Cameron 
63766f039790SGreg Kroah-Hartman static int hpsa_init_reset_devices(struct pci_dev *pdev)
6377edd16368SStephen M. Cameron {
63781df8552aSStephen M. Cameron 	int rc, i;
63793b747298STomas Henzl 	void __iomem *vaddr;
6380edd16368SStephen M. Cameron 
63814c2a8c40SStephen M. Cameron 	if (!reset_devices)
63824c2a8c40SStephen M. Cameron 		return 0;
63834c2a8c40SStephen M. Cameron 
6384132aa220STomas Henzl 	/* kdump kernel is loading, we don't know in which state is
6385132aa220STomas Henzl 	 * the pci interface. The dev->enable_cnt is equal zero
6386132aa220STomas Henzl 	 * so we call enable+disable, wait a while and switch it on.
6387132aa220STomas Henzl 	 */
6388132aa220STomas Henzl 	rc = pci_enable_device(pdev);
6389132aa220STomas Henzl 	if (rc) {
6390132aa220STomas Henzl 		dev_warn(&pdev->dev, "Failed to enable PCI device\n");
6391132aa220STomas Henzl 		return -ENODEV;
6392132aa220STomas Henzl 	}
6393132aa220STomas Henzl 	pci_disable_device(pdev);
6394132aa220STomas Henzl 	msleep(260);			/* a randomly chosen number */
6395132aa220STomas Henzl 	rc = pci_enable_device(pdev);
6396132aa220STomas Henzl 	if (rc) {
6397132aa220STomas Henzl 		dev_warn(&pdev->dev, "failed to enable device.\n");
6398132aa220STomas Henzl 		return -ENODEV;
6399132aa220STomas Henzl 	}
64004fa604e1SRobert Elliott 
6401859c75abSTomas Henzl 	pci_set_master(pdev);
64024fa604e1SRobert Elliott 
64033b747298STomas Henzl 	vaddr = pci_ioremap_bar(pdev, 0);
64043b747298STomas Henzl 	if (vaddr == NULL) {
64053b747298STomas Henzl 		rc = -ENOMEM;
64063b747298STomas Henzl 		goto out_disable;
64073b747298STomas Henzl 	}
64083b747298STomas Henzl 	writel(SA5_INTR_OFF, vaddr + SA5_REPLY_INTR_MASK_OFFSET);
64093b747298STomas Henzl 	iounmap(vaddr);
64103b747298STomas Henzl 
64111df8552aSStephen M. Cameron 	/* Reset the controller with a PCI power-cycle or via doorbell */
64121df8552aSStephen M. Cameron 	rc = hpsa_kdump_hard_reset_controller(pdev);
6413edd16368SStephen M. Cameron 
64141df8552aSStephen M. Cameron 	/* -ENOTSUPP here means we cannot reset the controller
64151df8552aSStephen M. Cameron 	 * but it's already (and still) up and running in
641618867659SStephen M. Cameron 	 * "performant mode".  Or, it might be 640x, which can't reset
641718867659SStephen M. Cameron 	 * due to concerns about shared bbwc between 6402/6404 pair.
64181df8552aSStephen M. Cameron 	 */
6419132aa220STomas Henzl 	if (rc) {
6420132aa220STomas Henzl 		if (rc != -ENOTSUPP) /* just try to do the kdump anyhow. */
6421132aa220STomas Henzl 			rc = -ENODEV;
6422132aa220STomas Henzl 		goto out_disable;
6423132aa220STomas Henzl 	}
6424edd16368SStephen M. Cameron 
6425edd16368SStephen M. Cameron 	/* Now try to get the controller to respond to a no-op */
64262b870cb3SStephen M. Cameron 	dev_warn(&pdev->dev, "Waiting for controller to respond to no-op\n");
6427edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
6428edd16368SStephen M. Cameron 		if (hpsa_noop(pdev) == 0)
6429edd16368SStephen M. Cameron 			break;
6430edd16368SStephen M. Cameron 		else
6431edd16368SStephen M. Cameron 			dev_warn(&pdev->dev, "no-op failed%s\n",
6432edd16368SStephen M. Cameron 					(i < 11 ? "; re-trying" : ""));
6433edd16368SStephen M. Cameron 	}
6434132aa220STomas Henzl 
6435132aa220STomas Henzl out_disable:
6436132aa220STomas Henzl 
6437132aa220STomas Henzl 	pci_disable_device(pdev);
6438132aa220STomas Henzl 	return rc;
6439edd16368SStephen M. Cameron }
6440edd16368SStephen M. Cameron 
64416f039790SGreg Kroah-Hartman static int hpsa_allocate_cmd_pool(struct ctlr_info *h)
64422e9d1b36SStephen M. Cameron {
64432e9d1b36SStephen M. Cameron 	h->cmd_pool_bits = kzalloc(
64442e9d1b36SStephen M. Cameron 		DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
64452e9d1b36SStephen M. Cameron 		sizeof(unsigned long), GFP_KERNEL);
64462e9d1b36SStephen M. Cameron 	h->cmd_pool = pci_alloc_consistent(h->pdev,
64472e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->cmd_pool),
64482e9d1b36SStephen M. Cameron 		    &(h->cmd_pool_dhandle));
64492e9d1b36SStephen M. Cameron 	h->errinfo_pool = pci_alloc_consistent(h->pdev,
64502e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->errinfo_pool),
64512e9d1b36SStephen M. Cameron 		    &(h->errinfo_pool_dhandle));
64522e9d1b36SStephen M. Cameron 	if ((h->cmd_pool_bits == NULL)
64532e9d1b36SStephen M. Cameron 	    || (h->cmd_pool == NULL)
64542e9d1b36SStephen M. Cameron 	    || (h->errinfo_pool == NULL)) {
64552e9d1b36SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory in %s", __func__);
64562e9d1b36SStephen M. Cameron 		return -ENOMEM;
64572e9d1b36SStephen M. Cameron 	}
64582e9d1b36SStephen M. Cameron 	return 0;
64592e9d1b36SStephen M. Cameron }
64602e9d1b36SStephen M. Cameron 
64612e9d1b36SStephen M. Cameron static void hpsa_free_cmd_pool(struct ctlr_info *h)
64622e9d1b36SStephen M. Cameron {
64632e9d1b36SStephen M. Cameron 	kfree(h->cmd_pool_bits);
64642e9d1b36SStephen M. Cameron 	if (h->cmd_pool)
64652e9d1b36SStephen M. Cameron 		pci_free_consistent(h->pdev,
64662e9d1b36SStephen M. Cameron 			    h->nr_cmds * sizeof(struct CommandList),
64672e9d1b36SStephen M. Cameron 			    h->cmd_pool, h->cmd_pool_dhandle);
6468aca9012aSStephen M. Cameron 	if (h->ioaccel2_cmd_pool)
6469aca9012aSStephen M. Cameron 		pci_free_consistent(h->pdev,
6470aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
6471aca9012aSStephen M. Cameron 			h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
64722e9d1b36SStephen M. Cameron 	if (h->errinfo_pool)
64732e9d1b36SStephen M. Cameron 		pci_free_consistent(h->pdev,
64742e9d1b36SStephen M. Cameron 			    h->nr_cmds * sizeof(struct ErrorInfo),
64752e9d1b36SStephen M. Cameron 			    h->errinfo_pool,
64762e9d1b36SStephen M. Cameron 			    h->errinfo_pool_dhandle);
6477e1f7de0cSMatt Gates 	if (h->ioaccel_cmd_pool)
6478e1f7de0cSMatt Gates 		pci_free_consistent(h->pdev,
6479e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(struct io_accel1_cmd),
6480e1f7de0cSMatt Gates 			h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
64812e9d1b36SStephen M. Cameron }
64822e9d1b36SStephen M. Cameron 
648341b3cf08SStephen M. Cameron static void hpsa_irq_affinity_hints(struct ctlr_info *h)
648441b3cf08SStephen M. Cameron {
648541b3cf08SStephen M. Cameron 	int i, cpu, rc;
648641b3cf08SStephen M. Cameron 
648741b3cf08SStephen M. Cameron 	cpu = cpumask_first(cpu_online_mask);
648841b3cf08SStephen M. Cameron 	for (i = 0; i < h->msix_vector; i++) {
648941b3cf08SStephen M. Cameron 		rc = irq_set_affinity_hint(h->intr[i], get_cpu_mask(cpu));
649041b3cf08SStephen M. Cameron 		cpu = cpumask_next(cpu, cpu_online_mask);
649141b3cf08SStephen M. Cameron 	}
649241b3cf08SStephen M. Cameron }
649341b3cf08SStephen M. Cameron 
6494*ec501a18SRobert Elliott /* clear affinity hints and free MSI-X, MSI, or legacy INTx vectors */
6495*ec501a18SRobert Elliott static void hpsa_free_irqs(struct ctlr_info *h)
6496*ec501a18SRobert Elliott {
6497*ec501a18SRobert Elliott 	int i;
6498*ec501a18SRobert Elliott 
6499*ec501a18SRobert Elliott 	if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
6500*ec501a18SRobert Elliott 		/* Single reply queue, only one irq to free */
6501*ec501a18SRobert Elliott 		i = h->intr_mode;
6502*ec501a18SRobert Elliott 		irq_set_affinity_hint(h->intr[i], NULL);
6503*ec501a18SRobert Elliott 		free_irq(h->intr[i], &h->q[i]);
6504*ec501a18SRobert Elliott 		return;
6505*ec501a18SRobert Elliott 	}
6506*ec501a18SRobert Elliott 
6507*ec501a18SRobert Elliott 	for (i = 0; i < h->msix_vector; i++) {
6508*ec501a18SRobert Elliott 		irq_set_affinity_hint(h->intr[i], NULL);
6509*ec501a18SRobert Elliott 		free_irq(h->intr[i], &h->q[i]);
6510*ec501a18SRobert Elliott 	}
6511*ec501a18SRobert Elliott }
6512*ec501a18SRobert Elliott 
65130ae01a32SStephen M. Cameron static int hpsa_request_irq(struct ctlr_info *h,
65140ae01a32SStephen M. Cameron 	irqreturn_t (*msixhandler)(int, void *),
65150ae01a32SStephen M. Cameron 	irqreturn_t (*intxhandler)(int, void *))
65160ae01a32SStephen M. Cameron {
6517254f796bSMatt Gates 	int rc, i;
65180ae01a32SStephen M. Cameron 
6519254f796bSMatt Gates 	/*
6520254f796bSMatt Gates 	 * initialize h->q[x] = x so that interrupt handlers know which
6521254f796bSMatt Gates 	 * queue to process.
6522254f796bSMatt Gates 	 */
6523254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++)
6524254f796bSMatt Gates 		h->q[i] = (u8) i;
6525254f796bSMatt Gates 
6526eee0f03aSHannes Reinecke 	if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
6527254f796bSMatt Gates 		/* If performant mode and MSI-X, use multiple reply queues */
6528eee0f03aSHannes Reinecke 		for (i = 0; i < h->msix_vector; i++)
6529254f796bSMatt Gates 			rc = request_irq(h->intr[i], msixhandler,
6530254f796bSMatt Gates 					0, h->devname,
6531254f796bSMatt Gates 					&h->q[i]);
653241b3cf08SStephen M. Cameron 		hpsa_irq_affinity_hints(h);
6533254f796bSMatt Gates 	} else {
6534254f796bSMatt Gates 		/* Use single reply pool */
6535eee0f03aSHannes Reinecke 		if (h->msix_vector > 0 || h->msi_vector) {
6536254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
6537254f796bSMatt Gates 				msixhandler, 0, h->devname,
6538254f796bSMatt Gates 				&h->q[h->intr_mode]);
6539254f796bSMatt Gates 		} else {
6540254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
6541254f796bSMatt Gates 				intxhandler, IRQF_SHARED, h->devname,
6542254f796bSMatt Gates 				&h->q[h->intr_mode]);
6543254f796bSMatt Gates 		}
6544254f796bSMatt Gates 	}
65450ae01a32SStephen M. Cameron 	if (rc) {
65460ae01a32SStephen M. Cameron 		dev_err(&h->pdev->dev, "unable to get irq %d for %s\n",
65470ae01a32SStephen M. Cameron 		       h->intr[h->intr_mode], h->devname);
65480ae01a32SStephen M. Cameron 		return -ENODEV;
65490ae01a32SStephen M. Cameron 	}
65500ae01a32SStephen M. Cameron 	return 0;
65510ae01a32SStephen M. Cameron }
65520ae01a32SStephen M. Cameron 
65536f039790SGreg Kroah-Hartman static int hpsa_kdump_soft_reset(struct ctlr_info *h)
655464670ac8SStephen M. Cameron {
655564670ac8SStephen M. Cameron 	if (hpsa_send_host_reset(h, RAID_CTLR_LUNID,
655664670ac8SStephen M. Cameron 		HPSA_RESET_TYPE_CONTROLLER)) {
655764670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Resetting array controller failed.\n");
655864670ac8SStephen M. Cameron 		return -EIO;
655964670ac8SStephen M. Cameron 	}
656064670ac8SStephen M. Cameron 
656164670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
656264670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) {
656364670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
656464670ac8SStephen M. Cameron 		return -1;
656564670ac8SStephen M. Cameron 	}
656664670ac8SStephen M. Cameron 
656764670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
656864670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) {
656964670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Board failed to become ready "
657064670ac8SStephen M. Cameron 			"after soft reset.\n");
657164670ac8SStephen M. Cameron 		return -1;
657264670ac8SStephen M. Cameron 	}
657364670ac8SStephen M. Cameron 
657464670ac8SStephen M. Cameron 	return 0;
657564670ac8SStephen M. Cameron }
657664670ac8SStephen M. Cameron 
65770097f0f4SStephen M. Cameron static void hpsa_free_irqs_and_disable_msix(struct ctlr_info *h)
657864670ac8SStephen M. Cameron {
6579*ec501a18SRobert Elliott 	hpsa_free_irqs(h);
658064670ac8SStephen M. Cameron #ifdef CONFIG_PCI_MSI
65810097f0f4SStephen M. Cameron 	if (h->msix_vector) {
65820097f0f4SStephen M. Cameron 		if (h->pdev->msix_enabled)
658364670ac8SStephen M. Cameron 			pci_disable_msix(h->pdev);
65840097f0f4SStephen M. Cameron 	} else if (h->msi_vector) {
65850097f0f4SStephen M. Cameron 		if (h->pdev->msi_enabled)
658664670ac8SStephen M. Cameron 			pci_disable_msi(h->pdev);
65870097f0f4SStephen M. Cameron 	}
658864670ac8SStephen M. Cameron #endif /* CONFIG_PCI_MSI */
65890097f0f4SStephen M. Cameron }
65900097f0f4SStephen M. Cameron 
6591072b0518SStephen M. Cameron static void hpsa_free_reply_queues(struct ctlr_info *h)
6592072b0518SStephen M. Cameron {
6593072b0518SStephen M. Cameron 	int i;
6594072b0518SStephen M. Cameron 
6595072b0518SStephen M. Cameron 	for (i = 0; i < h->nreply_queues; i++) {
6596072b0518SStephen M. Cameron 		if (!h->reply_queue[i].head)
6597072b0518SStephen M. Cameron 			continue;
6598072b0518SStephen M. Cameron 		pci_free_consistent(h->pdev, h->reply_queue_size,
6599072b0518SStephen M. Cameron 			h->reply_queue[i].head, h->reply_queue[i].busaddr);
6600072b0518SStephen M. Cameron 		h->reply_queue[i].head = NULL;
6601072b0518SStephen M. Cameron 		h->reply_queue[i].busaddr = 0;
6602072b0518SStephen M. Cameron 	}
6603072b0518SStephen M. Cameron }
6604072b0518SStephen M. Cameron 
66050097f0f4SStephen M. Cameron static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
66060097f0f4SStephen M. Cameron {
66070097f0f4SStephen M. Cameron 	hpsa_free_irqs_and_disable_msix(h);
660864670ac8SStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
660964670ac8SStephen M. Cameron 	hpsa_free_cmd_pool(h);
6610e1f7de0cSMatt Gates 	kfree(h->ioaccel1_blockFetchTable);
661164670ac8SStephen M. Cameron 	kfree(h->blockFetchTable);
6612072b0518SStephen M. Cameron 	hpsa_free_reply_queues(h);
661364670ac8SStephen M. Cameron 	if (h->vaddr)
661464670ac8SStephen M. Cameron 		iounmap(h->vaddr);
661564670ac8SStephen M. Cameron 	if (h->transtable)
661664670ac8SStephen M. Cameron 		iounmap(h->transtable);
661764670ac8SStephen M. Cameron 	if (h->cfgtable)
661864670ac8SStephen M. Cameron 		iounmap(h->cfgtable);
6619132aa220STomas Henzl 	pci_disable_device(h->pdev);
662064670ac8SStephen M. Cameron 	pci_release_regions(h->pdev);
662164670ac8SStephen M. Cameron 	kfree(h);
662264670ac8SStephen M. Cameron }
662364670ac8SStephen M. Cameron 
6624a0c12413SStephen M. Cameron /* Called when controller lockup detected. */
6625a0c12413SStephen M. Cameron static void fail_all_cmds_on_list(struct ctlr_info *h, struct list_head *list)
6626a0c12413SStephen M. Cameron {
6627a0c12413SStephen M. Cameron 	struct CommandList *c = NULL;
6628a0c12413SStephen M. Cameron 
6629a0c12413SStephen M. Cameron 	assert_spin_locked(&h->lock);
6630a0c12413SStephen M. Cameron 	/* Mark all outstanding commands as failed and complete them. */
6631a0c12413SStephen M. Cameron 	while (!list_empty(list)) {
6632a0c12413SStephen M. Cameron 		c = list_entry(list->next, struct CommandList, list);
6633a0c12413SStephen M. Cameron 		c->err_info->CommandStatus = CMD_HARDWARE_ERR;
66345a3d16f5SStephen M. Cameron 		finish_cmd(c);
6635a0c12413SStephen M. Cameron 	}
6636a0c12413SStephen M. Cameron }
6637a0c12413SStephen M. Cameron 
6638094963daSStephen M. Cameron static void set_lockup_detected_for_all_cpus(struct ctlr_info *h, u32 value)
6639094963daSStephen M. Cameron {
6640094963daSStephen M. Cameron 	int i, cpu;
6641094963daSStephen M. Cameron 
6642094963daSStephen M. Cameron 	cpu = cpumask_first(cpu_online_mask);
6643094963daSStephen M. Cameron 	for (i = 0; i < num_online_cpus(); i++) {
6644094963daSStephen M. Cameron 		u32 *lockup_detected;
6645094963daSStephen M. Cameron 		lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
6646094963daSStephen M. Cameron 		*lockup_detected = value;
6647094963daSStephen M. Cameron 		cpu = cpumask_next(cpu, cpu_online_mask);
6648094963daSStephen M. Cameron 	}
6649094963daSStephen M. Cameron 	wmb(); /* be sure the per-cpu variables are out to memory */
6650094963daSStephen M. Cameron }
6651094963daSStephen M. Cameron 
6652a0c12413SStephen M. Cameron static void controller_lockup_detected(struct ctlr_info *h)
6653a0c12413SStephen M. Cameron {
6654a0c12413SStephen M. Cameron 	unsigned long flags;
6655094963daSStephen M. Cameron 	u32 lockup_detected;
6656a0c12413SStephen M. Cameron 
6657a0c12413SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
6658a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
6659094963daSStephen M. Cameron 	lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
6660094963daSStephen M. Cameron 	if (!lockup_detected) {
6661094963daSStephen M. Cameron 		/* no heartbeat, but controller gave us a zero. */
6662094963daSStephen M. Cameron 		dev_warn(&h->pdev->dev,
6663094963daSStephen M. Cameron 			"lockup detected but scratchpad register is zero\n");
6664094963daSStephen M. Cameron 		lockup_detected = 0xffffffff;
6665094963daSStephen M. Cameron 	}
6666094963daSStephen M. Cameron 	set_lockup_detected_for_all_cpus(h, lockup_detected);
6667a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
6668a0c12413SStephen M. Cameron 	dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x\n",
6669094963daSStephen M. Cameron 			lockup_detected);
6670a0c12413SStephen M. Cameron 	pci_disable_device(h->pdev);
6671a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
6672a0c12413SStephen M. Cameron 	fail_all_cmds_on_list(h, &h->cmpQ);
6673a0c12413SStephen M. Cameron 	fail_all_cmds_on_list(h, &h->reqQ);
6674a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
6675a0c12413SStephen M. Cameron }
6676a0c12413SStephen M. Cameron 
6677a0c12413SStephen M. Cameron static void detect_controller_lockup(struct ctlr_info *h)
6678a0c12413SStephen M. Cameron {
6679a0c12413SStephen M. Cameron 	u64 now;
6680a0c12413SStephen M. Cameron 	u32 heartbeat;
6681a0c12413SStephen M. Cameron 	unsigned long flags;
6682a0c12413SStephen M. Cameron 
6683a0c12413SStephen M. Cameron 	now = get_jiffies_64();
6684a0c12413SStephen M. Cameron 	/* If we've received an interrupt recently, we're ok. */
6685a0c12413SStephen M. Cameron 	if (time_after64(h->last_intr_timestamp +
6686e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
6687a0c12413SStephen M. Cameron 		return;
6688a0c12413SStephen M. Cameron 
6689a0c12413SStephen M. Cameron 	/*
6690a0c12413SStephen M. Cameron 	 * If we've already checked the heartbeat recently, we're ok.
6691a0c12413SStephen M. Cameron 	 * This could happen if someone sends us a signal. We
6692a0c12413SStephen M. Cameron 	 * otherwise don't care about signals in this thread.
6693a0c12413SStephen M. Cameron 	 */
6694a0c12413SStephen M. Cameron 	if (time_after64(h->last_heartbeat_timestamp +
6695e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
6696a0c12413SStephen M. Cameron 		return;
6697a0c12413SStephen M. Cameron 
6698a0c12413SStephen M. Cameron 	/* If heartbeat has not changed since we last looked, we're not ok. */
6699a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
6700a0c12413SStephen M. Cameron 	heartbeat = readl(&h->cfgtable->HeartBeat);
6701a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
6702a0c12413SStephen M. Cameron 	if (h->last_heartbeat == heartbeat) {
6703a0c12413SStephen M. Cameron 		controller_lockup_detected(h);
6704a0c12413SStephen M. Cameron 		return;
6705a0c12413SStephen M. Cameron 	}
6706a0c12413SStephen M. Cameron 
6707a0c12413SStephen M. Cameron 	/* We're ok. */
6708a0c12413SStephen M. Cameron 	h->last_heartbeat = heartbeat;
6709a0c12413SStephen M. Cameron 	h->last_heartbeat_timestamp = now;
6710a0c12413SStephen M. Cameron }
6711a0c12413SStephen M. Cameron 
67129846590eSStephen M. Cameron static void hpsa_ack_ctlr_events(struct ctlr_info *h)
671376438d08SStephen M. Cameron {
671476438d08SStephen M. Cameron 	int i;
671576438d08SStephen M. Cameron 	char *event_type;
671676438d08SStephen M. Cameron 
6717e863d68eSScott Teel 	/* Clear the driver-requested rescan flag */
6718e863d68eSScott Teel 	h->drv_req_rescan = 0;
6719e863d68eSScott Teel 
672076438d08SStephen M. Cameron 	/* Ask the controller to clear the events we're handling. */
67211f7cee8cSStephen M. Cameron 	if ((h->transMethod & (CFGTBL_Trans_io_accel1
67221f7cee8cSStephen M. Cameron 			| CFGTBL_Trans_io_accel2)) &&
672376438d08SStephen M. Cameron 		(h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
672476438d08SStephen M. Cameron 		 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
672576438d08SStephen M. Cameron 
672676438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
672776438d08SStephen M. Cameron 			event_type = "state change";
672876438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
672976438d08SStephen M. Cameron 			event_type = "configuration change";
673076438d08SStephen M. Cameron 		/* Stop sending new RAID offload reqs via the IO accelerator */
673176438d08SStephen M. Cameron 		scsi_block_requests(h->scsi_host);
673276438d08SStephen M. Cameron 		for (i = 0; i < h->ndevices; i++)
673376438d08SStephen M. Cameron 			h->dev[i]->offload_enabled = 0;
673423100dd9SStephen M. Cameron 		hpsa_drain_accel_commands(h);
673576438d08SStephen M. Cameron 		/* Set 'accelerator path config change' bit */
673676438d08SStephen M. Cameron 		dev_warn(&h->pdev->dev,
673776438d08SStephen M. Cameron 			"Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
673876438d08SStephen M. Cameron 			h->events, event_type);
673976438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
674076438d08SStephen M. Cameron 		/* Set the "clear event notify field update" bit 6 */
674176438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
674276438d08SStephen M. Cameron 		/* Wait until ctlr clears 'clear event notify field', bit 6 */
674376438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
674476438d08SStephen M. Cameron 		scsi_unblock_requests(h->scsi_host);
674576438d08SStephen M. Cameron 	} else {
674676438d08SStephen M. Cameron 		/* Acknowledge controller notification events. */
674776438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
674876438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
674976438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
675076438d08SStephen M. Cameron #if 0
675176438d08SStephen M. Cameron 		writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
675276438d08SStephen M. Cameron 		hpsa_wait_for_mode_change_ack(h);
675376438d08SStephen M. Cameron #endif
675476438d08SStephen M. Cameron 	}
67559846590eSStephen M. Cameron 	return;
675676438d08SStephen M. Cameron }
675776438d08SStephen M. Cameron 
675876438d08SStephen M. Cameron /* Check a register on the controller to see if there are configuration
675976438d08SStephen M. Cameron  * changes (added/changed/removed logical drives, etc.) which mean that
6760e863d68eSScott Teel  * we should rescan the controller for devices.
6761e863d68eSScott Teel  * Also check flag for driver-initiated rescan.
676276438d08SStephen M. Cameron  */
67639846590eSStephen M. Cameron static int hpsa_ctlr_needs_rescan(struct ctlr_info *h)
676476438d08SStephen M. Cameron {
67659846590eSStephen M. Cameron 	if (h->drv_req_rescan)
67669846590eSStephen M. Cameron 		return 1;
67679846590eSStephen M. Cameron 
676876438d08SStephen M. Cameron 	if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
67699846590eSStephen M. Cameron 		return 0;
677076438d08SStephen M. Cameron 
677176438d08SStephen M. Cameron 	h->events = readl(&(h->cfgtable->event_notify));
67729846590eSStephen M. Cameron 	return h->events & RESCAN_REQUIRED_EVENT_BITS;
67739846590eSStephen M. Cameron }
677476438d08SStephen M. Cameron 
677576438d08SStephen M. Cameron /*
67769846590eSStephen M. Cameron  * Check if any of the offline devices have become ready
677776438d08SStephen M. Cameron  */
67789846590eSStephen M. Cameron static int hpsa_offline_devices_ready(struct ctlr_info *h)
67799846590eSStephen M. Cameron {
67809846590eSStephen M. Cameron 	unsigned long flags;
67819846590eSStephen M. Cameron 	struct offline_device_entry *d;
67829846590eSStephen M. Cameron 	struct list_head *this, *tmp;
67839846590eSStephen M. Cameron 
67849846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
67859846590eSStephen M. Cameron 	list_for_each_safe(this, tmp, &h->offline_device_list) {
67869846590eSStephen M. Cameron 		d = list_entry(this, struct offline_device_entry,
67879846590eSStephen M. Cameron 				offline_list);
67889846590eSStephen M. Cameron 		spin_unlock_irqrestore(&h->offline_device_lock, flags);
6789d1fea47cSStephen M. Cameron 		if (!hpsa_volume_offline(h, d->scsi3addr)) {
6790d1fea47cSStephen M. Cameron 			spin_lock_irqsave(&h->offline_device_lock, flags);
6791d1fea47cSStephen M. Cameron 			list_del(&d->offline_list);
6792d1fea47cSStephen M. Cameron 			spin_unlock_irqrestore(&h->offline_device_lock, flags);
67939846590eSStephen M. Cameron 			return 1;
6794d1fea47cSStephen M. Cameron 		}
67959846590eSStephen M. Cameron 		spin_lock_irqsave(&h->offline_device_lock, flags);
679676438d08SStephen M. Cameron 	}
67979846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
67989846590eSStephen M. Cameron 	return 0;
67999846590eSStephen M. Cameron }
68009846590eSStephen M. Cameron 
680176438d08SStephen M. Cameron 
68028a98db73SStephen M. Cameron static void hpsa_monitor_ctlr_worker(struct work_struct *work)
6803a0c12413SStephen M. Cameron {
6804a0c12413SStephen M. Cameron 	unsigned long flags;
68058a98db73SStephen M. Cameron 	struct ctlr_info *h = container_of(to_delayed_work(work),
68068a98db73SStephen M. Cameron 					struct ctlr_info, monitor_ctlr_work);
6807a0c12413SStephen M. Cameron 	detect_controller_lockup(h);
6808094963daSStephen M. Cameron 	if (lockup_detected(h))
68098a98db73SStephen M. Cameron 		return;
68109846590eSStephen M. Cameron 
68119846590eSStephen M. Cameron 	if (hpsa_ctlr_needs_rescan(h) || hpsa_offline_devices_ready(h)) {
68129846590eSStephen M. Cameron 		scsi_host_get(h->scsi_host);
68139846590eSStephen M. Cameron 		h->drv_req_rescan = 0;
68149846590eSStephen M. Cameron 		hpsa_ack_ctlr_events(h);
68159846590eSStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
68169846590eSStephen M. Cameron 		scsi_host_put(h->scsi_host);
68179846590eSStephen M. Cameron 	}
68189846590eSStephen M. Cameron 
68198a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
68208a98db73SStephen M. Cameron 	if (h->remove_in_progress) {
68218a98db73SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
6822a0c12413SStephen M. Cameron 		return;
6823a0c12413SStephen M. Cameron 	}
68248a98db73SStephen M. Cameron 	schedule_delayed_work(&h->monitor_ctlr_work,
68258a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
68268a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
6827a0c12413SStephen M. Cameron }
6828a0c12413SStephen M. Cameron 
68296f039790SGreg Kroah-Hartman static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
68304c2a8c40SStephen M. Cameron {
68314c2a8c40SStephen M. Cameron 	int dac, rc;
68324c2a8c40SStephen M. Cameron 	struct ctlr_info *h;
683364670ac8SStephen M. Cameron 	int try_soft_reset = 0;
683464670ac8SStephen M. Cameron 	unsigned long flags;
68354c2a8c40SStephen M. Cameron 
68364c2a8c40SStephen M. Cameron 	if (number_of_controllers == 0)
68374c2a8c40SStephen M. Cameron 		printk(KERN_INFO DRIVER_NAME "\n");
68384c2a8c40SStephen M. Cameron 
68394c2a8c40SStephen M. Cameron 	rc = hpsa_init_reset_devices(pdev);
684064670ac8SStephen M. Cameron 	if (rc) {
684164670ac8SStephen M. Cameron 		if (rc != -ENOTSUPP)
68424c2a8c40SStephen M. Cameron 			return rc;
684364670ac8SStephen M. Cameron 		/* If the reset fails in a particular way (it has no way to do
684464670ac8SStephen M. Cameron 		 * a proper hard reset, so returns -ENOTSUPP) we can try to do
684564670ac8SStephen M. Cameron 		 * a soft reset once we get the controller configured up to the
684664670ac8SStephen M. Cameron 		 * point that it can accept a command.
684764670ac8SStephen M. Cameron 		 */
684864670ac8SStephen M. Cameron 		try_soft_reset = 1;
684964670ac8SStephen M. Cameron 		rc = 0;
685064670ac8SStephen M. Cameron 	}
685164670ac8SStephen M. Cameron 
685264670ac8SStephen M. Cameron reinit_after_soft_reset:
68534c2a8c40SStephen M. Cameron 
6854303932fdSDon Brace 	/* Command structures must be aligned on a 32-byte boundary because
6855303932fdSDon Brace 	 * the 5 lower bits of the address are used by the hardware. and by
6856303932fdSDon Brace 	 * the driver.  See comments in hpsa.h for more info.
6857303932fdSDon Brace 	 */
6858303932fdSDon Brace 	BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
6859edd16368SStephen M. Cameron 	h = kzalloc(sizeof(*h), GFP_KERNEL);
6860edd16368SStephen M. Cameron 	if (!h)
6861ecd9aad4SStephen M. Cameron 		return -ENOMEM;
6862edd16368SStephen M. Cameron 
686355c06c71SStephen M. Cameron 	h->pdev = pdev;
6864a9a3a273SStephen M. Cameron 	h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
68659e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&h->cmpQ);
68669e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&h->reqQ);
68679846590eSStephen M. Cameron 	INIT_LIST_HEAD(&h->offline_device_list);
68686eaf46fdSStephen M. Cameron 	spin_lock_init(&h->lock);
68699846590eSStephen M. Cameron 	spin_lock_init(&h->offline_device_lock);
68706eaf46fdSStephen M. Cameron 	spin_lock_init(&h->scan_lock);
68710390f0c0SStephen M. Cameron 	spin_lock_init(&h->passthru_count_lock);
6872094963daSStephen M. Cameron 
6873094963daSStephen M. Cameron 	/* Allocate and clear per-cpu variable lockup_detected */
6874094963daSStephen M. Cameron 	h->lockup_detected = alloc_percpu(u32);
68752a5ac326SStephen M. Cameron 	if (!h->lockup_detected) {
68762a5ac326SStephen M. Cameron 		rc = -ENOMEM;
6877094963daSStephen M. Cameron 		goto clean1;
68782a5ac326SStephen M. Cameron 	}
6879094963daSStephen M. Cameron 	set_lockup_detected_for_all_cpus(h, 0);
6880094963daSStephen M. Cameron 
688155c06c71SStephen M. Cameron 	rc = hpsa_pci_init(h);
6882ecd9aad4SStephen M. Cameron 	if (rc != 0)
6883edd16368SStephen M. Cameron 		goto clean1;
6884edd16368SStephen M. Cameron 
6885f79cfec6SStephen M. Cameron 	sprintf(h->devname, HPSA "%d", number_of_controllers);
6886edd16368SStephen M. Cameron 	h->ctlr = number_of_controllers;
6887edd16368SStephen M. Cameron 	number_of_controllers++;
6888edd16368SStephen M. Cameron 
6889edd16368SStephen M. Cameron 	/* configure PCI DMA stuff */
6890ecd9aad4SStephen M. Cameron 	rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
6891ecd9aad4SStephen M. Cameron 	if (rc == 0) {
6892edd16368SStephen M. Cameron 		dac = 1;
6893ecd9aad4SStephen M. Cameron 	} else {
6894ecd9aad4SStephen M. Cameron 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6895ecd9aad4SStephen M. Cameron 		if (rc == 0) {
6896edd16368SStephen M. Cameron 			dac = 0;
6897ecd9aad4SStephen M. Cameron 		} else {
6898edd16368SStephen M. Cameron 			dev_err(&pdev->dev, "no suitable DMA available\n");
6899edd16368SStephen M. Cameron 			goto clean1;
6900edd16368SStephen M. Cameron 		}
6901ecd9aad4SStephen M. Cameron 	}
6902edd16368SStephen M. Cameron 
6903edd16368SStephen M. Cameron 	/* make sure the board interrupts are off */
6904edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
690510f66018SStephen M. Cameron 
69060ae01a32SStephen M. Cameron 	if (hpsa_request_irq(h, do_hpsa_intr_msi, do_hpsa_intr_intx))
6907edd16368SStephen M. Cameron 		goto clean2;
6908303932fdSDon Brace 	dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
6909303932fdSDon Brace 	       h->devname, pdev->device,
6910a9a3a273SStephen M. Cameron 	       h->intr[h->intr_mode], dac ? "" : " not");
69112e9d1b36SStephen M. Cameron 	if (hpsa_allocate_cmd_pool(h))
6912edd16368SStephen M. Cameron 		goto clean4;
691333a2ffceSStephen M. Cameron 	if (hpsa_allocate_sg_chain_blocks(h))
691433a2ffceSStephen M. Cameron 		goto clean4;
6915a08a8471SStephen M. Cameron 	init_waitqueue_head(&h->scan_wait_queue);
6916a08a8471SStephen M. Cameron 	h->scan_finished = 1; /* no scan currently in progress */
6917edd16368SStephen M. Cameron 
6918edd16368SStephen M. Cameron 	pci_set_drvdata(pdev, h);
69199a41338eSStephen M. Cameron 	h->ndevices = 0;
6920316b221aSStephen M. Cameron 	h->hba_mode_enabled = 0;
69219a41338eSStephen M. Cameron 	h->scsi_host = NULL;
69229a41338eSStephen M. Cameron 	spin_lock_init(&h->devlock);
692364670ac8SStephen M. Cameron 	hpsa_put_ctlr_into_performant_mode(h);
692464670ac8SStephen M. Cameron 
692564670ac8SStephen M. Cameron 	/* At this point, the controller is ready to take commands.
692664670ac8SStephen M. Cameron 	 * Now, if reset_devices and the hard reset didn't work, try
692764670ac8SStephen M. Cameron 	 * the soft reset and see if that works.
692864670ac8SStephen M. Cameron 	 */
692964670ac8SStephen M. Cameron 	if (try_soft_reset) {
693064670ac8SStephen M. Cameron 
693164670ac8SStephen M. Cameron 		/* This is kind of gross.  We may or may not get a completion
693264670ac8SStephen M. Cameron 		 * from the soft reset command, and if we do, then the value
693364670ac8SStephen M. Cameron 		 * from the fifo may or may not be valid.  So, we wait 10 secs
693464670ac8SStephen M. Cameron 		 * after the reset throwing away any completions we get during
693564670ac8SStephen M. Cameron 		 * that time.  Unregister the interrupt handler and register
693664670ac8SStephen M. Cameron 		 * fake ones to scoop up any residual completions.
693764670ac8SStephen M. Cameron 		 */
693864670ac8SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
693964670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
694064670ac8SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
6941*ec501a18SRobert Elliott 		hpsa_free_irqs(h);
694264670ac8SStephen M. Cameron 		rc = hpsa_request_irq(h, hpsa_msix_discard_completions,
694364670ac8SStephen M. Cameron 					hpsa_intx_discard_completions);
694464670ac8SStephen M. Cameron 		if (rc) {
694564670ac8SStephen M. Cameron 			dev_warn(&h->pdev->dev, "Failed to request_irq after "
694664670ac8SStephen M. Cameron 				"soft reset.\n");
694764670ac8SStephen M. Cameron 			goto clean4;
694864670ac8SStephen M. Cameron 		}
694964670ac8SStephen M. Cameron 
695064670ac8SStephen M. Cameron 		rc = hpsa_kdump_soft_reset(h);
695164670ac8SStephen M. Cameron 		if (rc)
695264670ac8SStephen M. Cameron 			/* Neither hard nor soft reset worked, we're hosed. */
695364670ac8SStephen M. Cameron 			goto clean4;
695464670ac8SStephen M. Cameron 
695564670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev, "Board READY.\n");
695664670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev,
695764670ac8SStephen M. Cameron 			"Waiting for stale completions to drain.\n");
695864670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_ON);
695964670ac8SStephen M. Cameron 		msleep(10000);
696064670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
696164670ac8SStephen M. Cameron 
696264670ac8SStephen M. Cameron 		rc = controller_reset_failed(h->cfgtable);
696364670ac8SStephen M. Cameron 		if (rc)
696464670ac8SStephen M. Cameron 			dev_info(&h->pdev->dev,
696564670ac8SStephen M. Cameron 				"Soft reset appears to have failed.\n");
696664670ac8SStephen M. Cameron 
696764670ac8SStephen M. Cameron 		/* since the controller's reset, we have to go back and re-init
696864670ac8SStephen M. Cameron 		 * everything.  Easiest to just forget what we've done and do it
696964670ac8SStephen M. Cameron 		 * all over again.
697064670ac8SStephen M. Cameron 		 */
697164670ac8SStephen M. Cameron 		hpsa_undo_allocations_after_kdump_soft_reset(h);
697264670ac8SStephen M. Cameron 		try_soft_reset = 0;
697364670ac8SStephen M. Cameron 		if (rc)
697464670ac8SStephen M. Cameron 			/* don't go to clean4, we already unallocated */
697564670ac8SStephen M. Cameron 			return -ENODEV;
697664670ac8SStephen M. Cameron 
697764670ac8SStephen M. Cameron 		goto reinit_after_soft_reset;
697864670ac8SStephen M. Cameron 	}
6979edd16368SStephen M. Cameron 
6980da0697bdSScott Teel 		/* Enable Accelerated IO path at driver layer */
6981da0697bdSScott Teel 		h->acciopath_status = 1;
6982da0697bdSScott Teel 
6983e863d68eSScott Teel 	h->drv_req_rescan = 0;
6984e863d68eSScott Teel 
6985edd16368SStephen M. Cameron 	/* Turn the interrupts on so we can service requests */
6986edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_ON);
6987edd16368SStephen M. Cameron 
6988339b2b14SStephen M. Cameron 	hpsa_hba_inquiry(h);
6989edd16368SStephen M. Cameron 	hpsa_register_scsi(h);	/* hook ourselves into SCSI subsystem */
69908a98db73SStephen M. Cameron 
69918a98db73SStephen M. Cameron 	/* Monitor the controller for firmware lockups */
69928a98db73SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
69938a98db73SStephen M. Cameron 	INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
69948a98db73SStephen M. Cameron 	schedule_delayed_work(&h->monitor_ctlr_work,
69958a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
699688bf6d62SStephen M. Cameron 	return 0;
6997edd16368SStephen M. Cameron 
6998edd16368SStephen M. Cameron clean4:
699933a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
70002e9d1b36SStephen M. Cameron 	hpsa_free_cmd_pool(h);
7001*ec501a18SRobert Elliott 	hpsa_free_irqs(h);
7002edd16368SStephen M. Cameron clean2:
7003edd16368SStephen M. Cameron clean1:
7004094963daSStephen M. Cameron 	if (h->lockup_detected)
7005094963daSStephen M. Cameron 		free_percpu(h->lockup_detected);
7006edd16368SStephen M. Cameron 	kfree(h);
7007ecd9aad4SStephen M. Cameron 	return rc;
7008edd16368SStephen M. Cameron }
7009edd16368SStephen M. Cameron 
7010edd16368SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h)
7011edd16368SStephen M. Cameron {
7012edd16368SStephen M. Cameron 	char *flush_buf;
7013edd16368SStephen M. Cameron 	struct CommandList *c;
7014702890e3SStephen M. Cameron 
7015702890e3SStephen M. Cameron 	/* Don't bother trying to flush the cache if locked up */
7016094963daSStephen M. Cameron 	if (unlikely(lockup_detected(h)))
7017702890e3SStephen M. Cameron 		return;
7018edd16368SStephen M. Cameron 	flush_buf = kzalloc(4, GFP_KERNEL);
7019edd16368SStephen M. Cameron 	if (!flush_buf)
7020edd16368SStephen M. Cameron 		return;
7021edd16368SStephen M. Cameron 
7022edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
7023edd16368SStephen M. Cameron 	if (!c) {
7024edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
7025edd16368SStephen M. Cameron 		goto out_of_memory;
7026edd16368SStephen M. Cameron 	}
7027a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
7028a2dac136SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_CMD)) {
7029a2dac136SStephen M. Cameron 		goto out;
7030a2dac136SStephen M. Cameron 	}
7031edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
7032edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus != 0)
7033a2dac136SStephen M. Cameron out:
7034edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
7035edd16368SStephen M. Cameron 			"error flushing cache on controller\n");
7036edd16368SStephen M. Cameron 	cmd_special_free(h, c);
7037edd16368SStephen M. Cameron out_of_memory:
7038edd16368SStephen M. Cameron 	kfree(flush_buf);
7039edd16368SStephen M. Cameron }
7040edd16368SStephen M. Cameron 
7041edd16368SStephen M. Cameron static void hpsa_shutdown(struct pci_dev *pdev)
7042edd16368SStephen M. Cameron {
7043edd16368SStephen M. Cameron 	struct ctlr_info *h;
7044edd16368SStephen M. Cameron 
7045edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
7046edd16368SStephen M. Cameron 	/* Turn board interrupts off  and send the flush cache command
7047edd16368SStephen M. Cameron 	 * sendcmd will turn off interrupt, and send the flush...
7048edd16368SStephen M. Cameron 	 * To write all data in the battery backed cache to disks
7049edd16368SStephen M. Cameron 	 */
7050edd16368SStephen M. Cameron 	hpsa_flush_cache(h);
7051edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
70520097f0f4SStephen M. Cameron 	hpsa_free_irqs_and_disable_msix(h);
7053edd16368SStephen M. Cameron }
7054edd16368SStephen M. Cameron 
70556f039790SGreg Kroah-Hartman static void hpsa_free_device_info(struct ctlr_info *h)
705655e14e76SStephen M. Cameron {
705755e14e76SStephen M. Cameron 	int i;
705855e14e76SStephen M. Cameron 
705955e14e76SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++)
706055e14e76SStephen M. Cameron 		kfree(h->dev[i]);
706155e14e76SStephen M. Cameron }
706255e14e76SStephen M. Cameron 
70636f039790SGreg Kroah-Hartman static void hpsa_remove_one(struct pci_dev *pdev)
7064edd16368SStephen M. Cameron {
7065edd16368SStephen M. Cameron 	struct ctlr_info *h;
70668a98db73SStephen M. Cameron 	unsigned long flags;
7067edd16368SStephen M. Cameron 
7068edd16368SStephen M. Cameron 	if (pci_get_drvdata(pdev) == NULL) {
7069edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "unable to remove device\n");
7070edd16368SStephen M. Cameron 		return;
7071edd16368SStephen M. Cameron 	}
7072edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
70738a98db73SStephen M. Cameron 
70748a98db73SStephen M. Cameron 	/* Get rid of any controller monitoring work items */
70758a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
70768a98db73SStephen M. Cameron 	h->remove_in_progress = 1;
70778a98db73SStephen M. Cameron 	cancel_delayed_work(&h->monitor_ctlr_work);
70788a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
70798a98db73SStephen M. Cameron 
7080edd16368SStephen M. Cameron 	hpsa_unregister_scsi(h);	/* unhook from SCSI subsystem */
7081edd16368SStephen M. Cameron 	hpsa_shutdown(pdev);
7082edd16368SStephen M. Cameron 	iounmap(h->vaddr);
7083204892e9SStephen M. Cameron 	iounmap(h->transtable);
7084204892e9SStephen M. Cameron 	iounmap(h->cfgtable);
708555e14e76SStephen M. Cameron 	hpsa_free_device_info(h);
708633a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
7087edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev,
7088edd16368SStephen M. Cameron 		h->nr_cmds * sizeof(struct CommandList),
7089edd16368SStephen M. Cameron 		h->cmd_pool, h->cmd_pool_dhandle);
7090edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev,
7091edd16368SStephen M. Cameron 		h->nr_cmds * sizeof(struct ErrorInfo),
7092edd16368SStephen M. Cameron 		h->errinfo_pool, h->errinfo_pool_dhandle);
7093072b0518SStephen M. Cameron 	hpsa_free_reply_queues(h);
7094edd16368SStephen M. Cameron 	kfree(h->cmd_pool_bits);
7095303932fdSDon Brace 	kfree(h->blockFetchTable);
7096e1f7de0cSMatt Gates 	kfree(h->ioaccel1_blockFetchTable);
7097aca9012aSStephen M. Cameron 	kfree(h->ioaccel2_blockFetchTable);
7098339b2b14SStephen M. Cameron 	kfree(h->hba_inquiry_data);
7099f0bd0b68SStephen M. Cameron 	pci_disable_device(pdev);
7100edd16368SStephen M. Cameron 	pci_release_regions(pdev);
7101094963daSStephen M. Cameron 	free_percpu(h->lockup_detected);
7102edd16368SStephen M. Cameron 	kfree(h);
7103edd16368SStephen M. Cameron }
7104edd16368SStephen M. Cameron 
7105edd16368SStephen M. Cameron static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
7106edd16368SStephen M. Cameron 	__attribute__((unused)) pm_message_t state)
7107edd16368SStephen M. Cameron {
7108edd16368SStephen M. Cameron 	return -ENOSYS;
7109edd16368SStephen M. Cameron }
7110edd16368SStephen M. Cameron 
7111edd16368SStephen M. Cameron static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
7112edd16368SStephen M. Cameron {
7113edd16368SStephen M. Cameron 	return -ENOSYS;
7114edd16368SStephen M. Cameron }
7115edd16368SStephen M. Cameron 
7116edd16368SStephen M. Cameron static struct pci_driver hpsa_pci_driver = {
7117f79cfec6SStephen M. Cameron 	.name = HPSA,
7118edd16368SStephen M. Cameron 	.probe = hpsa_init_one,
71196f039790SGreg Kroah-Hartman 	.remove = hpsa_remove_one,
7120edd16368SStephen M. Cameron 	.id_table = hpsa_pci_device_id,	/* id_table */
7121edd16368SStephen M. Cameron 	.shutdown = hpsa_shutdown,
7122edd16368SStephen M. Cameron 	.suspend = hpsa_suspend,
7123edd16368SStephen M. Cameron 	.resume = hpsa_resume,
7124edd16368SStephen M. Cameron };
7125edd16368SStephen M. Cameron 
7126303932fdSDon Brace /* Fill in bucket_map[], given nsgs (the max number of
7127303932fdSDon Brace  * scatter gather elements supported) and bucket[],
7128303932fdSDon Brace  * which is an array of 8 integers.  The bucket[] array
7129303932fdSDon Brace  * contains 8 different DMA transfer sizes (in 16
7130303932fdSDon Brace  * byte increments) which the controller uses to fetch
7131303932fdSDon Brace  * commands.  This function fills in bucket_map[], which
7132303932fdSDon Brace  * maps a given number of scatter gather elements to one of
7133303932fdSDon Brace  * the 8 DMA transfer sizes.  The point of it is to allow the
7134303932fdSDon Brace  * controller to only do as much DMA as needed to fetch the
7135303932fdSDon Brace  * command, with the DMA transfer size encoded in the lower
7136303932fdSDon Brace  * bits of the command address.
7137303932fdSDon Brace  */
7138303932fdSDon Brace static void  calc_bucket_map(int bucket[], int num_buckets,
71392b08b3e9SDon Brace 	int nsgs, int min_blocks, u32 *bucket_map)
7140303932fdSDon Brace {
7141303932fdSDon Brace 	int i, j, b, size;
7142303932fdSDon Brace 
7143303932fdSDon Brace 	/* Note, bucket_map must have nsgs+1 entries. */
7144303932fdSDon Brace 	for (i = 0; i <= nsgs; i++) {
7145303932fdSDon Brace 		/* Compute size of a command with i SG entries */
7146e1f7de0cSMatt Gates 		size = i + min_blocks;
7147303932fdSDon Brace 		b = num_buckets; /* Assume the biggest bucket */
7148303932fdSDon Brace 		/* Find the bucket that is just big enough */
7149e1f7de0cSMatt Gates 		for (j = 0; j < num_buckets; j++) {
7150303932fdSDon Brace 			if (bucket[j] >= size) {
7151303932fdSDon Brace 				b = j;
7152303932fdSDon Brace 				break;
7153303932fdSDon Brace 			}
7154303932fdSDon Brace 		}
7155303932fdSDon Brace 		/* for a command with i SG entries, use bucket b. */
7156303932fdSDon Brace 		bucket_map[i] = b;
7157303932fdSDon Brace 	}
7158303932fdSDon Brace }
7159303932fdSDon Brace 
7160e1f7de0cSMatt Gates static void hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
7161303932fdSDon Brace {
71626c311b57SStephen M. Cameron 	int i;
71636c311b57SStephen M. Cameron 	unsigned long register_value;
7164e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
7165e1f7de0cSMatt Gates 			(trans_support & CFGTBL_Trans_use_short_tags) |
7166e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix |
7167b9af4937SStephen M. Cameron 			(trans_support & (CFGTBL_Trans_io_accel1 |
7168b9af4937SStephen M. Cameron 				CFGTBL_Trans_io_accel2));
7169e1f7de0cSMatt Gates 	struct access_method access = SA5_performant_access;
7170def342bdSStephen M. Cameron 
7171def342bdSStephen M. Cameron 	/* This is a bit complicated.  There are 8 registers on
7172def342bdSStephen M. Cameron 	 * the controller which we write to to tell it 8 different
7173def342bdSStephen M. Cameron 	 * sizes of commands which there may be.  It's a way of
7174def342bdSStephen M. Cameron 	 * reducing the DMA done to fetch each command.  Encoded into
7175def342bdSStephen M. Cameron 	 * each command's tag are 3 bits which communicate to the controller
7176def342bdSStephen M. Cameron 	 * which of the eight sizes that command fits within.  The size of
7177def342bdSStephen M. Cameron 	 * each command depends on how many scatter gather entries there are.
7178def342bdSStephen M. Cameron 	 * Each SG entry requires 16 bytes.  The eight registers are programmed
7179def342bdSStephen M. Cameron 	 * with the number of 16-byte blocks a command of that size requires.
7180def342bdSStephen M. Cameron 	 * The smallest command possible requires 5 such 16 byte blocks.
7181d66ae08bSStephen M. Cameron 	 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
7182def342bdSStephen M. Cameron 	 * blocks.  Note, this only extends to the SG entries contained
7183def342bdSStephen M. Cameron 	 * within the command block, and does not extend to chained blocks
7184def342bdSStephen M. Cameron 	 * of SG elements.   bft[] contains the eight values we write to
7185def342bdSStephen M. Cameron 	 * the registers.  They are not evenly distributed, but have more
7186def342bdSStephen M. Cameron 	 * sizes for small commands, and fewer sizes for larger commands.
7187def342bdSStephen M. Cameron 	 */
7188d66ae08bSStephen M. Cameron 	int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
7189b9af4937SStephen M. Cameron #define MIN_IOACCEL2_BFT_ENTRY 5
7190b9af4937SStephen M. Cameron #define HPSA_IOACCEL2_HEADER_SZ 4
7191b9af4937SStephen M. Cameron 	int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
7192b9af4937SStephen M. Cameron 			13, 14, 15, 16, 17, 18, 19,
7193b9af4937SStephen M. Cameron 			HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
7194b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
7195b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
7196b9af4937SStephen M. Cameron 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
7197b9af4937SStephen M. Cameron 				 16 * MIN_IOACCEL2_BFT_ENTRY);
7198b9af4937SStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
7199d66ae08bSStephen M. Cameron 	BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
7200303932fdSDon Brace 	/*  5 = 1 s/g entry or 4k
7201303932fdSDon Brace 	 *  6 = 2 s/g entry or 8k
7202303932fdSDon Brace 	 *  8 = 4 s/g entry or 16k
7203303932fdSDon Brace 	 * 10 = 6 s/g entry or 24k
7204303932fdSDon Brace 	 */
7205303932fdSDon Brace 
7206b3a52e79SStephen M. Cameron 	/* If the controller supports either ioaccel method then
7207b3a52e79SStephen M. Cameron 	 * we can also use the RAID stack submit path that does not
7208b3a52e79SStephen M. Cameron 	 * perform the superfluous readl() after each command submission.
7209b3a52e79SStephen M. Cameron 	 */
7210b3a52e79SStephen M. Cameron 	if (trans_support & (CFGTBL_Trans_io_accel1 | CFGTBL_Trans_io_accel2))
7211b3a52e79SStephen M. Cameron 		access = SA5_performant_access_no_read;
7212b3a52e79SStephen M. Cameron 
7213303932fdSDon Brace 	/* Controller spec: zero out this buffer. */
7214072b0518SStephen M. Cameron 	for (i = 0; i < h->nreply_queues; i++)
7215072b0518SStephen M. Cameron 		memset(h->reply_queue[i].head, 0, h->reply_queue_size);
7216303932fdSDon Brace 
7217d66ae08bSStephen M. Cameron 	bft[7] = SG_ENTRIES_IN_CMD + 4;
7218d66ae08bSStephen M. Cameron 	calc_bucket_map(bft, ARRAY_SIZE(bft),
7219e1f7de0cSMatt Gates 				SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
7220303932fdSDon Brace 	for (i = 0; i < 8; i++)
7221303932fdSDon Brace 		writel(bft[i], &h->transtable->BlockFetch[i]);
7222303932fdSDon Brace 
7223303932fdSDon Brace 	/* size of controller ring buffer */
7224303932fdSDon Brace 	writel(h->max_commands, &h->transtable->RepQSize);
7225254f796bSMatt Gates 	writel(h->nreply_queues, &h->transtable->RepQCount);
7226303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrLow32);
7227303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrHigh32);
7228254f796bSMatt Gates 
7229254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
7230254f796bSMatt Gates 		writel(0, &h->transtable->RepQAddr[i].upper);
7231072b0518SStephen M. Cameron 		writel(h->reply_queue[i].busaddr,
7232254f796bSMatt Gates 			&h->transtable->RepQAddr[i].lower);
7233254f796bSMatt Gates 	}
7234254f796bSMatt Gates 
7235b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
7236e1f7de0cSMatt Gates 	writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
7237e1f7de0cSMatt Gates 	/*
7238e1f7de0cSMatt Gates 	 * enable outbound interrupt coalescing in accelerator mode;
7239e1f7de0cSMatt Gates 	 */
7240e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
7241e1f7de0cSMatt Gates 		access = SA5_ioaccel_mode1_access;
7242e1f7de0cSMatt Gates 		writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
7243e1f7de0cSMatt Gates 		writel(4, &h->cfgtable->HostWrite.CoalIntCount);
7244c349775eSScott Teel 	} else {
7245c349775eSScott Teel 		if (trans_support & CFGTBL_Trans_io_accel2) {
7246c349775eSScott Teel 			access = SA5_ioaccel_mode2_access;
7247c349775eSScott Teel 			writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
7248c349775eSScott Teel 			writel(4, &h->cfgtable->HostWrite.CoalIntCount);
7249c349775eSScott Teel 		}
7250e1f7de0cSMatt Gates 	}
7251303932fdSDon Brace 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
72523f4336f3SStephen M. Cameron 	hpsa_wait_for_mode_change_ack(h);
7253303932fdSDon Brace 	register_value = readl(&(h->cfgtable->TransportActive));
7254303932fdSDon Brace 	if (!(register_value & CFGTBL_Trans_Performant)) {
7255303932fdSDon Brace 		dev_warn(&h->pdev->dev, "unable to get board into"
7256303932fdSDon Brace 					" performant mode\n");
7257303932fdSDon Brace 		return;
7258303932fdSDon Brace 	}
7259960a30e7SStephen M. Cameron 	/* Change the access methods to the performant access methods */
7260e1f7de0cSMatt Gates 	h->access = access;
7261e1f7de0cSMatt Gates 	h->transMethod = transMethod;
7262e1f7de0cSMatt Gates 
7263b9af4937SStephen M. Cameron 	if (!((trans_support & CFGTBL_Trans_io_accel1) ||
7264b9af4937SStephen M. Cameron 		(trans_support & CFGTBL_Trans_io_accel2)))
7265e1f7de0cSMatt Gates 		return;
7266e1f7de0cSMatt Gates 
7267b9af4937SStephen M. Cameron 	if (trans_support & CFGTBL_Trans_io_accel1) {
7268e1f7de0cSMatt Gates 		/* Set up I/O accelerator mode */
7269e1f7de0cSMatt Gates 		for (i = 0; i < h->nreply_queues; i++) {
7270e1f7de0cSMatt Gates 			writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
7271e1f7de0cSMatt Gates 			h->reply_queue[i].current_entry =
7272e1f7de0cSMatt Gates 				readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
7273e1f7de0cSMatt Gates 		}
7274283b4a9bSStephen M. Cameron 		bft[7] = h->ioaccel_maxsg + 8;
7275283b4a9bSStephen M. Cameron 		calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
7276e1f7de0cSMatt Gates 				h->ioaccel1_blockFetchTable);
7277e1f7de0cSMatt Gates 
7278e1f7de0cSMatt Gates 		/* initialize all reply queue entries to unused */
7279072b0518SStephen M. Cameron 		for (i = 0; i < h->nreply_queues; i++)
7280072b0518SStephen M. Cameron 			memset(h->reply_queue[i].head,
7281072b0518SStephen M. Cameron 				(u8) IOACCEL_MODE1_REPLY_UNUSED,
7282072b0518SStephen M. Cameron 				h->reply_queue_size);
7283e1f7de0cSMatt Gates 
7284e1f7de0cSMatt Gates 		/* set all the constant fields in the accelerator command
7285e1f7de0cSMatt Gates 		 * frames once at init time to save CPU cycles later.
7286e1f7de0cSMatt Gates 		 */
7287e1f7de0cSMatt Gates 		for (i = 0; i < h->nr_cmds; i++) {
7288e1f7de0cSMatt Gates 			struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
7289e1f7de0cSMatt Gates 
7290e1f7de0cSMatt Gates 			cp->function = IOACCEL1_FUNCTION_SCSIIO;
7291e1f7de0cSMatt Gates 			cp->err_info = (u32) (h->errinfo_pool_dhandle +
7292e1f7de0cSMatt Gates 					(i * sizeof(struct ErrorInfo)));
7293e1f7de0cSMatt Gates 			cp->err_info_len = sizeof(struct ErrorInfo);
7294e1f7de0cSMatt Gates 			cp->sgl_offset = IOACCEL1_SGLOFFSET;
72952b08b3e9SDon Brace 			cp->host_context_flags =
72962b08b3e9SDon Brace 				cpu_to_le16(IOACCEL1_HCFLAGS_CISS_FORMAT);
7297e1f7de0cSMatt Gates 			cp->timeout_sec = 0;
7298e1f7de0cSMatt Gates 			cp->ReplyQueue = 0;
729950a0decfSStephen M. Cameron 			cp->tag =
730050a0decfSStephen M. Cameron 				cpu_to_le64((i << DIRECT_LOOKUP_SHIFT) |
730150a0decfSStephen M. Cameron 						DIRECT_LOOKUP_BIT);
730250a0decfSStephen M. Cameron 			cp->host_addr =
730350a0decfSStephen M. Cameron 				cpu_to_le64(h->ioaccel_cmd_pool_dhandle +
7304e1f7de0cSMatt Gates 					(i * sizeof(struct io_accel1_cmd)));
7305e1f7de0cSMatt Gates 		}
7306b9af4937SStephen M. Cameron 	} else if (trans_support & CFGTBL_Trans_io_accel2) {
7307b9af4937SStephen M. Cameron 		u64 cfg_offset, cfg_base_addr_index;
7308b9af4937SStephen M. Cameron 		u32 bft2_offset, cfg_base_addr;
7309b9af4937SStephen M. Cameron 		int rc;
7310b9af4937SStephen M. Cameron 
7311b9af4937SStephen M. Cameron 		rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
7312b9af4937SStephen M. Cameron 			&cfg_base_addr_index, &cfg_offset);
7313b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
7314b9af4937SStephen M. Cameron 		bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
7315b9af4937SStephen M. Cameron 		calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
7316b9af4937SStephen M. Cameron 				4, h->ioaccel2_blockFetchTable);
7317b9af4937SStephen M. Cameron 		bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
7318b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct CfgTable,
7319b9af4937SStephen M. Cameron 				io_accel_request_size_offset) != 0xb8);
7320b9af4937SStephen M. Cameron 		h->ioaccel2_bft2_regs =
7321b9af4937SStephen M. Cameron 			remap_pci_mem(pci_resource_start(h->pdev,
7322b9af4937SStephen M. Cameron 					cfg_base_addr_index) +
7323b9af4937SStephen M. Cameron 					cfg_offset + bft2_offset,
7324b9af4937SStephen M. Cameron 					ARRAY_SIZE(bft2) *
7325b9af4937SStephen M. Cameron 					sizeof(*h->ioaccel2_bft2_regs));
7326b9af4937SStephen M. Cameron 		for (i = 0; i < ARRAY_SIZE(bft2); i++)
7327b9af4937SStephen M. Cameron 			writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
7328b9af4937SStephen M. Cameron 	}
7329b9af4937SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
7330b9af4937SStephen M. Cameron 	hpsa_wait_for_mode_change_ack(h);
7331e1f7de0cSMatt Gates }
7332e1f7de0cSMatt Gates 
7333e1f7de0cSMatt Gates static int hpsa_alloc_ioaccel_cmd_and_bft(struct ctlr_info *h)
7334e1f7de0cSMatt Gates {
7335283b4a9bSStephen M. Cameron 	h->ioaccel_maxsg =
7336283b4a9bSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
7337283b4a9bSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
7338283b4a9bSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
7339283b4a9bSStephen M. Cameron 
7340e1f7de0cSMatt Gates 	/* Command structures must be aligned on a 128-byte boundary
7341e1f7de0cSMatt Gates 	 * because the 7 lower bits of the address are used by the
7342e1f7de0cSMatt Gates 	 * hardware.
7343e1f7de0cSMatt Gates 	 */
7344e1f7de0cSMatt Gates 	BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
7345e1f7de0cSMatt Gates 			IOACCEL1_COMMANDLIST_ALIGNMENT);
7346e1f7de0cSMatt Gates 	h->ioaccel_cmd_pool =
7347e1f7de0cSMatt Gates 		pci_alloc_consistent(h->pdev,
7348e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
7349e1f7de0cSMatt Gates 			&(h->ioaccel_cmd_pool_dhandle));
7350e1f7de0cSMatt Gates 
7351e1f7de0cSMatt Gates 	h->ioaccel1_blockFetchTable =
7352283b4a9bSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
7353e1f7de0cSMatt Gates 				sizeof(u32)), GFP_KERNEL);
7354e1f7de0cSMatt Gates 
7355e1f7de0cSMatt Gates 	if ((h->ioaccel_cmd_pool == NULL) ||
7356e1f7de0cSMatt Gates 		(h->ioaccel1_blockFetchTable == NULL))
7357e1f7de0cSMatt Gates 		goto clean_up;
7358e1f7de0cSMatt Gates 
7359e1f7de0cSMatt Gates 	memset(h->ioaccel_cmd_pool, 0,
7360e1f7de0cSMatt Gates 		h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
7361e1f7de0cSMatt Gates 	return 0;
7362e1f7de0cSMatt Gates 
7363e1f7de0cSMatt Gates clean_up:
7364e1f7de0cSMatt Gates 	if (h->ioaccel_cmd_pool)
7365e1f7de0cSMatt Gates 		pci_free_consistent(h->pdev,
7366e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
7367e1f7de0cSMatt Gates 			h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
7368e1f7de0cSMatt Gates 	kfree(h->ioaccel1_blockFetchTable);
7369e1f7de0cSMatt Gates 	return 1;
73706c311b57SStephen M. Cameron }
73716c311b57SStephen M. Cameron 
7372aca9012aSStephen M. Cameron static int ioaccel2_alloc_cmds_and_bft(struct ctlr_info *h)
7373aca9012aSStephen M. Cameron {
7374aca9012aSStephen M. Cameron 	/* Allocate ioaccel2 mode command blocks and block fetch table */
7375aca9012aSStephen M. Cameron 
7376aca9012aSStephen M. Cameron 	h->ioaccel_maxsg =
7377aca9012aSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
7378aca9012aSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
7379aca9012aSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
7380aca9012aSStephen M. Cameron 
7381aca9012aSStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
7382aca9012aSStephen M. Cameron 			IOACCEL2_COMMANDLIST_ALIGNMENT);
7383aca9012aSStephen M. Cameron 	h->ioaccel2_cmd_pool =
7384aca9012aSStephen M. Cameron 		pci_alloc_consistent(h->pdev,
7385aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
7386aca9012aSStephen M. Cameron 			&(h->ioaccel2_cmd_pool_dhandle));
7387aca9012aSStephen M. Cameron 
7388aca9012aSStephen M. Cameron 	h->ioaccel2_blockFetchTable =
7389aca9012aSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
7390aca9012aSStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
7391aca9012aSStephen M. Cameron 
7392aca9012aSStephen M. Cameron 	if ((h->ioaccel2_cmd_pool == NULL) ||
7393aca9012aSStephen M. Cameron 		(h->ioaccel2_blockFetchTable == NULL))
7394aca9012aSStephen M. Cameron 		goto clean_up;
7395aca9012aSStephen M. Cameron 
7396aca9012aSStephen M. Cameron 	memset(h->ioaccel2_cmd_pool, 0,
7397aca9012aSStephen M. Cameron 		h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
7398aca9012aSStephen M. Cameron 	return 0;
7399aca9012aSStephen M. Cameron 
7400aca9012aSStephen M. Cameron clean_up:
7401aca9012aSStephen M. Cameron 	if (h->ioaccel2_cmd_pool)
7402aca9012aSStephen M. Cameron 		pci_free_consistent(h->pdev,
7403aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
7404aca9012aSStephen M. Cameron 			h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
7405aca9012aSStephen M. Cameron 	kfree(h->ioaccel2_blockFetchTable);
7406aca9012aSStephen M. Cameron 	return 1;
7407aca9012aSStephen M. Cameron }
7408aca9012aSStephen M. Cameron 
74096f039790SGreg Kroah-Hartman static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
74106c311b57SStephen M. Cameron {
74116c311b57SStephen M. Cameron 	u32 trans_support;
7412e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
7413e1f7de0cSMatt Gates 					CFGTBL_Trans_use_short_tags;
7414254f796bSMatt Gates 	int i;
74156c311b57SStephen M. Cameron 
741602ec19c8SStephen M. Cameron 	if (hpsa_simple_mode)
741702ec19c8SStephen M. Cameron 		return;
741802ec19c8SStephen M. Cameron 
741967c99a72Sscameron@beardog.cce.hp.com 	trans_support = readl(&(h->cfgtable->TransportSupport));
742067c99a72Sscameron@beardog.cce.hp.com 	if (!(trans_support & PERFORMANT_MODE))
742167c99a72Sscameron@beardog.cce.hp.com 		return;
742267c99a72Sscameron@beardog.cce.hp.com 
7423e1f7de0cSMatt Gates 	/* Check for I/O accelerator mode support */
7424e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
7425e1f7de0cSMatt Gates 		transMethod |= CFGTBL_Trans_io_accel1 |
7426e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix;
7427e1f7de0cSMatt Gates 		if (hpsa_alloc_ioaccel_cmd_and_bft(h))
7428e1f7de0cSMatt Gates 			goto clean_up;
7429aca9012aSStephen M. Cameron 	} else {
7430aca9012aSStephen M. Cameron 		if (trans_support & CFGTBL_Trans_io_accel2) {
7431aca9012aSStephen M. Cameron 				transMethod |= CFGTBL_Trans_io_accel2 |
7432aca9012aSStephen M. Cameron 				CFGTBL_Trans_enable_directed_msix;
7433aca9012aSStephen M. Cameron 		if (ioaccel2_alloc_cmds_and_bft(h))
7434aca9012aSStephen M. Cameron 			goto clean_up;
7435aca9012aSStephen M. Cameron 		}
7436e1f7de0cSMatt Gates 	}
7437e1f7de0cSMatt Gates 
7438eee0f03aSHannes Reinecke 	h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
7439cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
74406c311b57SStephen M. Cameron 	/* Performant mode ring buffer and supporting data structures */
7441072b0518SStephen M. Cameron 	h->reply_queue_size = h->max_commands * sizeof(u64);
74426c311b57SStephen M. Cameron 
7443254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
7444072b0518SStephen M. Cameron 		h->reply_queue[i].head = pci_alloc_consistent(h->pdev,
7445072b0518SStephen M. Cameron 						h->reply_queue_size,
7446072b0518SStephen M. Cameron 						&(h->reply_queue[i].busaddr));
7447072b0518SStephen M. Cameron 		if (!h->reply_queue[i].head)
7448072b0518SStephen M. Cameron 			goto clean_up;
7449254f796bSMatt Gates 		h->reply_queue[i].size = h->max_commands;
7450254f796bSMatt Gates 		h->reply_queue[i].wraparound = 1;  /* spec: init to 1 */
7451254f796bSMatt Gates 		h->reply_queue[i].current_entry = 0;
7452254f796bSMatt Gates 	}
7453254f796bSMatt Gates 
74546c311b57SStephen M. Cameron 	/* Need a block fetch table for performant mode */
7455d66ae08bSStephen M. Cameron 	h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
74566c311b57SStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
7457072b0518SStephen M. Cameron 	if (!h->blockFetchTable)
74586c311b57SStephen M. Cameron 		goto clean_up;
74596c311b57SStephen M. Cameron 
7460e1f7de0cSMatt Gates 	hpsa_enter_performant_mode(h, trans_support);
7461303932fdSDon Brace 	return;
7462303932fdSDon Brace 
7463303932fdSDon Brace clean_up:
7464072b0518SStephen M. Cameron 	hpsa_free_reply_queues(h);
7465303932fdSDon Brace 	kfree(h->blockFetchTable);
7466303932fdSDon Brace }
7467303932fdSDon Brace 
746823100dd9SStephen M. Cameron static int is_accelerated_cmd(struct CommandList *c)
746976438d08SStephen M. Cameron {
747023100dd9SStephen M. Cameron 	return c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_IOACCEL2;
747123100dd9SStephen M. Cameron }
747223100dd9SStephen M. Cameron 
747323100dd9SStephen M. Cameron static void hpsa_drain_accel_commands(struct ctlr_info *h)
747423100dd9SStephen M. Cameron {
747523100dd9SStephen M. Cameron 	struct CommandList *c = NULL;
747676438d08SStephen M. Cameron 	unsigned long flags;
747723100dd9SStephen M. Cameron 	int accel_cmds_out;
747876438d08SStephen M. Cameron 
747976438d08SStephen M. Cameron 	do { /* wait for all outstanding commands to drain out */
748023100dd9SStephen M. Cameron 		accel_cmds_out = 0;
748176438d08SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
748223100dd9SStephen M. Cameron 		list_for_each_entry(c, &h->cmpQ, list)
748323100dd9SStephen M. Cameron 			accel_cmds_out += is_accelerated_cmd(c);
748423100dd9SStephen M. Cameron 		list_for_each_entry(c, &h->reqQ, list)
748523100dd9SStephen M. Cameron 			accel_cmds_out += is_accelerated_cmd(c);
748676438d08SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
748723100dd9SStephen M. Cameron 		if (accel_cmds_out <= 0)
748876438d08SStephen M. Cameron 			break;
748976438d08SStephen M. Cameron 		msleep(100);
749076438d08SStephen M. Cameron 	} while (1);
749176438d08SStephen M. Cameron }
749276438d08SStephen M. Cameron 
7493edd16368SStephen M. Cameron /*
7494edd16368SStephen M. Cameron  *  This is it.  Register the PCI driver information for the cards we control
7495edd16368SStephen M. Cameron  *  the OS will call our registered routines when it finds one of our cards.
7496edd16368SStephen M. Cameron  */
7497edd16368SStephen M. Cameron static int __init hpsa_init(void)
7498edd16368SStephen M. Cameron {
749931468401SMike Miller 	return pci_register_driver(&hpsa_pci_driver);
7500edd16368SStephen M. Cameron }
7501edd16368SStephen M. Cameron 
7502edd16368SStephen M. Cameron static void __exit hpsa_cleanup(void)
7503edd16368SStephen M. Cameron {
7504edd16368SStephen M. Cameron 	pci_unregister_driver(&hpsa_pci_driver);
7505edd16368SStephen M. Cameron }
7506edd16368SStephen M. Cameron 
7507e1f7de0cSMatt Gates static void __attribute__((unused)) verify_offsets(void)
7508e1f7de0cSMatt Gates {
7509e1f7de0cSMatt Gates #define VERIFY_OFFSET(member, offset) \
7510dd0e19f3SScott Teel 	BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
7511dd0e19f3SScott Teel 
7512dd0e19f3SScott Teel 	VERIFY_OFFSET(structure_size, 0);
7513dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_size, 4);
7514dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_cnt, 8);
7515dd0e19f3SScott Teel 	VERIFY_OFFSET(phys_blk_shift, 16);
7516dd0e19f3SScott Teel 	VERIFY_OFFSET(parity_rotation_shift, 17);
7517dd0e19f3SScott Teel 	VERIFY_OFFSET(strip_size, 18);
7518dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_starting_blk, 20);
7519dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_blk_cnt, 28);
7520dd0e19f3SScott Teel 	VERIFY_OFFSET(data_disks_per_row, 36);
7521dd0e19f3SScott Teel 	VERIFY_OFFSET(metadata_disks_per_row, 38);
7522dd0e19f3SScott Teel 	VERIFY_OFFSET(row_cnt, 40);
7523dd0e19f3SScott Teel 	VERIFY_OFFSET(layout_map_count, 42);
7524dd0e19f3SScott Teel 	VERIFY_OFFSET(flags, 44);
7525dd0e19f3SScott Teel 	VERIFY_OFFSET(dekindex, 46);
7526dd0e19f3SScott Teel 	/* VERIFY_OFFSET(reserved, 48 */
7527dd0e19f3SScott Teel 	VERIFY_OFFSET(data, 64);
7528dd0e19f3SScott Teel 
7529dd0e19f3SScott Teel #undef VERIFY_OFFSET
7530dd0e19f3SScott Teel 
7531dd0e19f3SScott Teel #define VERIFY_OFFSET(member, offset) \
7532b66cc250SMike Miller 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
7533b66cc250SMike Miller 
7534b66cc250SMike Miller 	VERIFY_OFFSET(IU_type, 0);
7535b66cc250SMike Miller 	VERIFY_OFFSET(direction, 1);
7536b66cc250SMike Miller 	VERIFY_OFFSET(reply_queue, 2);
7537b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved1, 3);  */
7538b66cc250SMike Miller 	VERIFY_OFFSET(scsi_nexus, 4);
7539b66cc250SMike Miller 	VERIFY_OFFSET(Tag, 8);
7540b66cc250SMike Miller 	VERIFY_OFFSET(cdb, 16);
7541b66cc250SMike Miller 	VERIFY_OFFSET(cciss_lun, 32);
7542b66cc250SMike Miller 	VERIFY_OFFSET(data_len, 40);
7543b66cc250SMike Miller 	VERIFY_OFFSET(cmd_priority_task_attr, 44);
7544b66cc250SMike Miller 	VERIFY_OFFSET(sg_count, 45);
7545b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved3 */
7546b66cc250SMike Miller 	VERIFY_OFFSET(err_ptr, 48);
7547b66cc250SMike Miller 	VERIFY_OFFSET(err_len, 56);
7548b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved4  */
7549b66cc250SMike Miller 	VERIFY_OFFSET(sg, 64);
7550b66cc250SMike Miller 
7551b66cc250SMike Miller #undef VERIFY_OFFSET
7552b66cc250SMike Miller 
7553b66cc250SMike Miller #define VERIFY_OFFSET(member, offset) \
7554e1f7de0cSMatt Gates 	BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
7555e1f7de0cSMatt Gates 
7556e1f7de0cSMatt Gates 	VERIFY_OFFSET(dev_handle, 0x00);
7557e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved1, 0x02);
7558e1f7de0cSMatt Gates 	VERIFY_OFFSET(function, 0x03);
7559e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved2, 0x04);
7560e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info, 0x0C);
7561e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved3, 0x10);
7562e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info_len, 0x12);
7563e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved4, 0x13);
7564e1f7de0cSMatt Gates 	VERIFY_OFFSET(sgl_offset, 0x14);
7565e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved5, 0x15);
7566e1f7de0cSMatt Gates 	VERIFY_OFFSET(transfer_len, 0x1C);
7567e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved6, 0x20);
7568e1f7de0cSMatt Gates 	VERIFY_OFFSET(io_flags, 0x24);
7569e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved7, 0x26);
7570e1f7de0cSMatt Gates 	VERIFY_OFFSET(LUN, 0x34);
7571e1f7de0cSMatt Gates 	VERIFY_OFFSET(control, 0x3C);
7572e1f7de0cSMatt Gates 	VERIFY_OFFSET(CDB, 0x40);
7573e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved8, 0x50);
7574e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_context_flags, 0x60);
7575e1f7de0cSMatt Gates 	VERIFY_OFFSET(timeout_sec, 0x62);
7576e1f7de0cSMatt Gates 	VERIFY_OFFSET(ReplyQueue, 0x64);
7577e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved9, 0x65);
757850a0decfSStephen M. Cameron 	VERIFY_OFFSET(tag, 0x68);
7579e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_addr, 0x70);
7580e1f7de0cSMatt Gates 	VERIFY_OFFSET(CISS_LUN, 0x78);
7581e1f7de0cSMatt Gates 	VERIFY_OFFSET(SG, 0x78 + 8);
7582e1f7de0cSMatt Gates #undef VERIFY_OFFSET
7583e1f7de0cSMatt Gates }
7584e1f7de0cSMatt Gates 
7585edd16368SStephen M. Cameron module_init(hpsa_init);
7586edd16368SStephen M. Cameron module_exit(hpsa_cleanup);
7587