xref: /openbmc/linux/drivers/scsi/hpsa.c (revision 45fcb86e4663122590d243a466eb21ae45890f03)
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 struct CommandList *cmd_alloc(struct ctlr_info *h);
209a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
210b7bb24ebSStephen M. Cameron 	void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
211edd16368SStephen M. Cameron 	int cmd_type);
2122c143342SRobert Elliott static void hpsa_free_cmd_pool(struct ctlr_info *h);
213b7bb24ebSStephen M. Cameron #define VPD_PAGE (1 << 8)
214edd16368SStephen M. Cameron 
215f281233dSJeff Garzik static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
216a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *);
217a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
218a08a8471SStephen M. Cameron 	unsigned long elapsed_time);
2197c0a0229SDon Brace static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth);
220edd16368SStephen M. Cameron 
221edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
22275167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *scsicmd);
223edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev);
224edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev);
225edd16368SStephen M. Cameron 
226edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
227edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
228edd16368SStephen M. Cameron 	struct CommandList *c);
229edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
230edd16368SStephen M. Cameron 	struct CommandList *c);
231303932fdSDon Brace /* performant mode helper functions */
232303932fdSDon Brace static void calc_bucket_map(int *bucket, int num_buckets,
2332b08b3e9SDon Brace 	int nsgs, int min_blocks, u32 *bucket_map);
2346f039790SGreg Kroah-Hartman static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
235254f796bSMatt Gates static inline u32 next_command(struct ctlr_info *h, u8 q);
2366f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
2376f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
2381df8552aSStephen M. Cameron 			       u64 *cfg_offset);
2396f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
2401df8552aSStephen M. Cameron 				    unsigned long *memory_bar);
2416f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
2426f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
2436f039790SGreg Kroah-Hartman 				     int wait_for_ready);
24475167d2cSStephen M. Cameron static inline void finish_cmd(struct CommandList *c);
245283b4a9bSStephen M. Cameron static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h);
246fe5389c8SStephen M. Cameron #define BOARD_NOT_READY 0
247fe5389c8SStephen M. Cameron #define BOARD_READY 1
24823100dd9SStephen M. Cameron static void hpsa_drain_accel_commands(struct ctlr_info *h);
24976438d08SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h);
250c349775eSScott Teel static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
251c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
252c349775eSScott Teel 	u8 *scsi3addr);
253edd16368SStephen M. Cameron 
254edd16368SStephen M. Cameron static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
255edd16368SStephen M. Cameron {
256edd16368SStephen M. Cameron 	unsigned long *priv = shost_priv(sdev->host);
257edd16368SStephen M. Cameron 	return (struct ctlr_info *) *priv;
258edd16368SStephen M. Cameron }
259edd16368SStephen M. Cameron 
260a23513e8SStephen M. Cameron static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
261a23513e8SStephen M. Cameron {
262a23513e8SStephen M. Cameron 	unsigned long *priv = shost_priv(sh);
263a23513e8SStephen M. Cameron 	return (struct ctlr_info *) *priv;
264a23513e8SStephen M. Cameron }
265a23513e8SStephen M. Cameron 
266edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
267edd16368SStephen M. Cameron 	struct CommandList *c)
268edd16368SStephen M. Cameron {
269edd16368SStephen M. Cameron 	if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
270edd16368SStephen M. Cameron 		return 0;
271edd16368SStephen M. Cameron 
272edd16368SStephen M. Cameron 	switch (c->err_info->SenseInfo[12]) {
273edd16368SStephen M. Cameron 	case STATE_CHANGED:
274f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: a state change "
275edd16368SStephen M. Cameron 			"detected, command retried\n", h->ctlr);
276edd16368SStephen M. Cameron 		break;
277edd16368SStephen M. Cameron 	case LUN_FAILED:
2787f73695aSStephen M. Cameron 		dev_warn(&h->pdev->dev,
2797f73695aSStephen M. Cameron 			HPSA "%d: LUN failure detected\n", h->ctlr);
280edd16368SStephen M. Cameron 		break;
281edd16368SStephen M. Cameron 	case REPORT_LUNS_CHANGED:
2827f73695aSStephen M. Cameron 		dev_warn(&h->pdev->dev,
2837f73695aSStephen M. Cameron 			HPSA "%d: report LUN data changed\n", h->ctlr);
284edd16368SStephen M. Cameron 	/*
2854f4eb9f1SScott Teel 	 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
2864f4eb9f1SScott Teel 	 * target (array) devices.
287edd16368SStephen M. Cameron 	 */
288edd16368SStephen M. Cameron 		break;
289edd16368SStephen M. Cameron 	case POWER_OR_RESET:
290f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: a power on "
291edd16368SStephen M. Cameron 			"or device reset detected\n", h->ctlr);
292edd16368SStephen M. Cameron 		break;
293edd16368SStephen M. Cameron 	case UNIT_ATTENTION_CLEARED:
294f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: unit attention "
295edd16368SStephen M. Cameron 		    "cleared by another initiator\n", h->ctlr);
296edd16368SStephen M. Cameron 		break;
297edd16368SStephen M. Cameron 	default:
298f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: unknown "
299edd16368SStephen M. Cameron 			"unit attention detected\n", h->ctlr);
300edd16368SStephen M. Cameron 		break;
301edd16368SStephen M. Cameron 	}
302edd16368SStephen M. Cameron 	return 1;
303edd16368SStephen M. Cameron }
304edd16368SStephen M. Cameron 
305852af20aSMatt Bondurant static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
306852af20aSMatt Bondurant {
307852af20aSMatt Bondurant 	if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
308852af20aSMatt Bondurant 		(c->err_info->ScsiStatus != SAM_STAT_BUSY &&
309852af20aSMatt Bondurant 		 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
310852af20aSMatt Bondurant 		return 0;
311852af20aSMatt Bondurant 	dev_warn(&h->pdev->dev, HPSA "device busy");
312852af20aSMatt Bondurant 	return 1;
313852af20aSMatt Bondurant }
314852af20aSMatt Bondurant 
315da0697bdSScott Teel static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
316da0697bdSScott Teel 					 struct device_attribute *attr,
317da0697bdSScott Teel 					 const char *buf, size_t count)
318da0697bdSScott Teel {
319da0697bdSScott Teel 	int status, len;
320da0697bdSScott Teel 	struct ctlr_info *h;
321da0697bdSScott Teel 	struct Scsi_Host *shost = class_to_shost(dev);
322da0697bdSScott Teel 	char tmpbuf[10];
323da0697bdSScott Teel 
324da0697bdSScott Teel 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
325da0697bdSScott Teel 		return -EACCES;
326da0697bdSScott Teel 	len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
327da0697bdSScott Teel 	strncpy(tmpbuf, buf, len);
328da0697bdSScott Teel 	tmpbuf[len] = '\0';
329da0697bdSScott Teel 	if (sscanf(tmpbuf, "%d", &status) != 1)
330da0697bdSScott Teel 		return -EINVAL;
331da0697bdSScott Teel 	h = shost_to_hba(shost);
332da0697bdSScott Teel 	h->acciopath_status = !!status;
333da0697bdSScott Teel 	dev_warn(&h->pdev->dev,
334da0697bdSScott Teel 		"hpsa: HP SSD Smart Path %s via sysfs update.\n",
335da0697bdSScott Teel 		h->acciopath_status ? "enabled" : "disabled");
336da0697bdSScott Teel 	return count;
337da0697bdSScott Teel }
338da0697bdSScott Teel 
3392ba8bfc8SStephen M. Cameron static ssize_t host_store_raid_offload_debug(struct device *dev,
3402ba8bfc8SStephen M. Cameron 					 struct device_attribute *attr,
3412ba8bfc8SStephen M. Cameron 					 const char *buf, size_t count)
3422ba8bfc8SStephen M. Cameron {
3432ba8bfc8SStephen M. Cameron 	int debug_level, len;
3442ba8bfc8SStephen M. Cameron 	struct ctlr_info *h;
3452ba8bfc8SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
3462ba8bfc8SStephen M. Cameron 	char tmpbuf[10];
3472ba8bfc8SStephen M. Cameron 
3482ba8bfc8SStephen M. Cameron 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
3492ba8bfc8SStephen M. Cameron 		return -EACCES;
3502ba8bfc8SStephen M. Cameron 	len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
3512ba8bfc8SStephen M. Cameron 	strncpy(tmpbuf, buf, len);
3522ba8bfc8SStephen M. Cameron 	tmpbuf[len] = '\0';
3532ba8bfc8SStephen M. Cameron 	if (sscanf(tmpbuf, "%d", &debug_level) != 1)
3542ba8bfc8SStephen M. Cameron 		return -EINVAL;
3552ba8bfc8SStephen M. Cameron 	if (debug_level < 0)
3562ba8bfc8SStephen M. Cameron 		debug_level = 0;
3572ba8bfc8SStephen M. Cameron 	h = shost_to_hba(shost);
3582ba8bfc8SStephen M. Cameron 	h->raid_offload_debug = debug_level;
3592ba8bfc8SStephen M. Cameron 	dev_warn(&h->pdev->dev, "hpsa: Set raid_offload_debug level = %d\n",
3602ba8bfc8SStephen M. Cameron 		h->raid_offload_debug);
3612ba8bfc8SStephen M. Cameron 	return count;
3622ba8bfc8SStephen M. Cameron }
3632ba8bfc8SStephen M. Cameron 
364edd16368SStephen M. Cameron static ssize_t host_store_rescan(struct device *dev,
365edd16368SStephen M. Cameron 				 struct device_attribute *attr,
366edd16368SStephen M. Cameron 				 const char *buf, size_t count)
367edd16368SStephen M. Cameron {
368edd16368SStephen M. Cameron 	struct ctlr_info *h;
369edd16368SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
370a23513e8SStephen M. Cameron 	h = shost_to_hba(shost);
37131468401SMike Miller 	hpsa_scan_start(h->scsi_host);
372edd16368SStephen M. Cameron 	return count;
373edd16368SStephen M. Cameron }
374edd16368SStephen M. Cameron 
375d28ce020SStephen M. Cameron static ssize_t host_show_firmware_revision(struct device *dev,
376d28ce020SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
377d28ce020SStephen M. Cameron {
378d28ce020SStephen M. Cameron 	struct ctlr_info *h;
379d28ce020SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
380d28ce020SStephen M. Cameron 	unsigned char *fwrev;
381d28ce020SStephen M. Cameron 
382d28ce020SStephen M. Cameron 	h = shost_to_hba(shost);
383d28ce020SStephen M. Cameron 	if (!h->hba_inquiry_data)
384d28ce020SStephen M. Cameron 		return 0;
385d28ce020SStephen M. Cameron 	fwrev = &h->hba_inquiry_data[32];
386d28ce020SStephen M. Cameron 	return snprintf(buf, 20, "%c%c%c%c\n",
387d28ce020SStephen M. Cameron 		fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
388d28ce020SStephen M. Cameron }
389d28ce020SStephen M. Cameron 
39094a13649SStephen M. Cameron static ssize_t host_show_commands_outstanding(struct device *dev,
39194a13649SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
39294a13649SStephen M. Cameron {
39394a13649SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
39494a13649SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(shost);
39594a13649SStephen M. Cameron 
3960cbf768eSStephen M. Cameron 	return snprintf(buf, 20, "%d\n",
3970cbf768eSStephen M. Cameron 			atomic_read(&h->commands_outstanding));
39894a13649SStephen M. Cameron }
39994a13649SStephen M. Cameron 
400745a7a25SStephen M. Cameron static ssize_t host_show_transport_mode(struct device *dev,
401745a7a25SStephen M. Cameron 	struct device_attribute *attr, char *buf)
402745a7a25SStephen M. Cameron {
403745a7a25SStephen M. Cameron 	struct ctlr_info *h;
404745a7a25SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
405745a7a25SStephen M. Cameron 
406745a7a25SStephen M. Cameron 	h = shost_to_hba(shost);
407745a7a25SStephen M. Cameron 	return snprintf(buf, 20, "%s\n",
408960a30e7SStephen M. Cameron 		h->transMethod & CFGTBL_Trans_Performant ?
409745a7a25SStephen M. Cameron 			"performant" : "simple");
410745a7a25SStephen M. Cameron }
411745a7a25SStephen M. Cameron 
412da0697bdSScott Teel static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
413da0697bdSScott Teel 	struct device_attribute *attr, char *buf)
414da0697bdSScott Teel {
415da0697bdSScott Teel 	struct ctlr_info *h;
416da0697bdSScott Teel 	struct Scsi_Host *shost = class_to_shost(dev);
417da0697bdSScott Teel 
418da0697bdSScott Teel 	h = shost_to_hba(shost);
419da0697bdSScott Teel 	return snprintf(buf, 30, "HP SSD Smart Path %s\n",
420da0697bdSScott Teel 		(h->acciopath_status == 1) ?  "enabled" : "disabled");
421da0697bdSScott Teel }
422da0697bdSScott Teel 
42346380786SStephen M. Cameron /* List of controllers which cannot be hard reset on kexec with reset_devices */
424941b1cdaSStephen M. Cameron static u32 unresettable_controller[] = {
425941b1cdaSStephen M. Cameron 	0x324a103C, /* Smart Array P712m */
426941b1cdaSStephen M. Cameron 	0x324b103C, /* SmartArray P711m */
427941b1cdaSStephen M. Cameron 	0x3223103C, /* Smart Array P800 */
428941b1cdaSStephen M. Cameron 	0x3234103C, /* Smart Array P400 */
429941b1cdaSStephen M. Cameron 	0x3235103C, /* Smart Array P400i */
430941b1cdaSStephen M. Cameron 	0x3211103C, /* Smart Array E200i */
431941b1cdaSStephen M. Cameron 	0x3212103C, /* Smart Array E200 */
432941b1cdaSStephen M. Cameron 	0x3213103C, /* Smart Array E200i */
433941b1cdaSStephen M. Cameron 	0x3214103C, /* Smart Array E200i */
434941b1cdaSStephen M. Cameron 	0x3215103C, /* Smart Array E200i */
435941b1cdaSStephen M. Cameron 	0x3237103C, /* Smart Array E500 */
436941b1cdaSStephen M. Cameron 	0x323D103C, /* Smart Array P700m */
4377af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
438941b1cdaSStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
439941b1cdaSStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
4405a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
4415a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
4425a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
4435a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
4445a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
4455a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
446941b1cdaSStephen M. Cameron };
447941b1cdaSStephen M. Cameron 
44846380786SStephen M. Cameron /* List of controllers which cannot even be soft reset */
44946380786SStephen M. Cameron static u32 soft_unresettable_controller[] = {
4507af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
4515a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
4525a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
4535a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
4545a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
4555a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
4565a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
45746380786SStephen M. Cameron 	/* Exclude 640x boards.  These are two pci devices in one slot
45846380786SStephen M. Cameron 	 * which share a battery backed cache module.  One controls the
45946380786SStephen M. Cameron 	 * cache, the other accesses the cache through the one that controls
46046380786SStephen M. Cameron 	 * it.  If we reset the one controlling the cache, the other will
46146380786SStephen M. Cameron 	 * likely not be happy.  Just forbid resetting this conjoined mess.
46246380786SStephen M. Cameron 	 * The 640x isn't really supported by hpsa anyway.
46346380786SStephen M. Cameron 	 */
46446380786SStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
46546380786SStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
46646380786SStephen M. Cameron };
46746380786SStephen M. Cameron 
46846380786SStephen M. Cameron static int ctlr_is_hard_resettable(u32 board_id)
469941b1cdaSStephen M. Cameron {
470941b1cdaSStephen M. Cameron 	int i;
471941b1cdaSStephen M. Cameron 
472941b1cdaSStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(unresettable_controller); i++)
47346380786SStephen M. Cameron 		if (unresettable_controller[i] == board_id)
474941b1cdaSStephen M. Cameron 			return 0;
475941b1cdaSStephen M. Cameron 	return 1;
476941b1cdaSStephen M. Cameron }
477941b1cdaSStephen M. Cameron 
47846380786SStephen M. Cameron static int ctlr_is_soft_resettable(u32 board_id)
47946380786SStephen M. Cameron {
48046380786SStephen M. Cameron 	int i;
48146380786SStephen M. Cameron 
48246380786SStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(soft_unresettable_controller); i++)
48346380786SStephen M. Cameron 		if (soft_unresettable_controller[i] == board_id)
48446380786SStephen M. Cameron 			return 0;
48546380786SStephen M. Cameron 	return 1;
48646380786SStephen M. Cameron }
48746380786SStephen M. Cameron 
48846380786SStephen M. Cameron static int ctlr_is_resettable(u32 board_id)
48946380786SStephen M. Cameron {
49046380786SStephen M. Cameron 	return ctlr_is_hard_resettable(board_id) ||
49146380786SStephen M. Cameron 		ctlr_is_soft_resettable(board_id);
49246380786SStephen M. Cameron }
49346380786SStephen M. Cameron 
494941b1cdaSStephen M. Cameron static ssize_t host_show_resettable(struct device *dev,
495941b1cdaSStephen M. Cameron 	struct device_attribute *attr, char *buf)
496941b1cdaSStephen M. Cameron {
497941b1cdaSStephen M. Cameron 	struct ctlr_info *h;
498941b1cdaSStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
499941b1cdaSStephen M. Cameron 
500941b1cdaSStephen M. Cameron 	h = shost_to_hba(shost);
50146380786SStephen M. Cameron 	return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
502941b1cdaSStephen M. Cameron }
503941b1cdaSStephen M. Cameron 
504edd16368SStephen M. Cameron static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
505edd16368SStephen M. Cameron {
506edd16368SStephen M. Cameron 	return (scsi3addr[3] & 0xC0) == 0x40;
507edd16368SStephen M. Cameron }
508edd16368SStephen M. Cameron 
509f2ef0ce7SRobert Elliott static const char * const raid_label[] = { "0", "4", "1(+0)", "5", "5+1", "6",
510f2ef0ce7SRobert Elliott 	"1(+0)ADM", "UNKNOWN"
511edd16368SStephen M. Cameron };
5126b80b18fSScott Teel #define HPSA_RAID_0	0
5136b80b18fSScott Teel #define HPSA_RAID_4	1
5146b80b18fSScott Teel #define HPSA_RAID_1	2	/* also used for RAID 10 */
5156b80b18fSScott Teel #define HPSA_RAID_5	3	/* also used for RAID 50 */
5166b80b18fSScott Teel #define HPSA_RAID_51	4
5176b80b18fSScott Teel #define HPSA_RAID_6	5	/* also used for RAID 60 */
5186b80b18fSScott Teel #define HPSA_RAID_ADM	6	/* also used for RAID 1+0 ADM */
519edd16368SStephen M. Cameron #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
520edd16368SStephen M. Cameron 
521edd16368SStephen M. Cameron static ssize_t raid_level_show(struct device *dev,
522edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
523edd16368SStephen M. Cameron {
524edd16368SStephen M. Cameron 	ssize_t l = 0;
52582a72c0aSStephen M. Cameron 	unsigned char rlevel;
526edd16368SStephen M. Cameron 	struct ctlr_info *h;
527edd16368SStephen M. Cameron 	struct scsi_device *sdev;
528edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
529edd16368SStephen M. Cameron 	unsigned long flags;
530edd16368SStephen M. Cameron 
531edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
532edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
533edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
534edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
535edd16368SStephen M. Cameron 	if (!hdev) {
536edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
537edd16368SStephen M. Cameron 		return -ENODEV;
538edd16368SStephen M. Cameron 	}
539edd16368SStephen M. Cameron 
540edd16368SStephen M. Cameron 	/* Is this even a logical drive? */
541edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
542edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
543edd16368SStephen M. Cameron 		l = snprintf(buf, PAGE_SIZE, "N/A\n");
544edd16368SStephen M. Cameron 		return l;
545edd16368SStephen M. Cameron 	}
546edd16368SStephen M. Cameron 
547edd16368SStephen M. Cameron 	rlevel = hdev->raid_level;
548edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
54982a72c0aSStephen M. Cameron 	if (rlevel > RAID_UNKNOWN)
550edd16368SStephen M. Cameron 		rlevel = RAID_UNKNOWN;
551edd16368SStephen M. Cameron 	l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
552edd16368SStephen M. Cameron 	return l;
553edd16368SStephen M. Cameron }
554edd16368SStephen M. Cameron 
555edd16368SStephen M. Cameron static ssize_t lunid_show(struct device *dev,
556edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
557edd16368SStephen M. Cameron {
558edd16368SStephen M. Cameron 	struct ctlr_info *h;
559edd16368SStephen M. Cameron 	struct scsi_device *sdev;
560edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
561edd16368SStephen M. Cameron 	unsigned long flags;
562edd16368SStephen M. Cameron 	unsigned char lunid[8];
563edd16368SStephen M. Cameron 
564edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
565edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
566edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
567edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
568edd16368SStephen M. Cameron 	if (!hdev) {
569edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
570edd16368SStephen M. Cameron 		return -ENODEV;
571edd16368SStephen M. Cameron 	}
572edd16368SStephen M. Cameron 	memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
573edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
574edd16368SStephen M. Cameron 	return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
575edd16368SStephen M. Cameron 		lunid[0], lunid[1], lunid[2], lunid[3],
576edd16368SStephen M. Cameron 		lunid[4], lunid[5], lunid[6], lunid[7]);
577edd16368SStephen M. Cameron }
578edd16368SStephen M. Cameron 
579edd16368SStephen M. Cameron static ssize_t unique_id_show(struct device *dev,
580edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
581edd16368SStephen M. Cameron {
582edd16368SStephen M. Cameron 	struct ctlr_info *h;
583edd16368SStephen M. Cameron 	struct scsi_device *sdev;
584edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
585edd16368SStephen M. Cameron 	unsigned long flags;
586edd16368SStephen M. Cameron 	unsigned char sn[16];
587edd16368SStephen M. Cameron 
588edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
589edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
590edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
591edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
592edd16368SStephen M. Cameron 	if (!hdev) {
593edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
594edd16368SStephen M. Cameron 		return -ENODEV;
595edd16368SStephen M. Cameron 	}
596edd16368SStephen M. Cameron 	memcpy(sn, hdev->device_id, sizeof(sn));
597edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
598edd16368SStephen M. Cameron 	return snprintf(buf, 16 * 2 + 2,
599edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X"
600edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X\n",
601edd16368SStephen M. Cameron 			sn[0], sn[1], sn[2], sn[3],
602edd16368SStephen M. Cameron 			sn[4], sn[5], sn[6], sn[7],
603edd16368SStephen M. Cameron 			sn[8], sn[9], sn[10], sn[11],
604edd16368SStephen M. Cameron 			sn[12], sn[13], sn[14], sn[15]);
605edd16368SStephen M. Cameron }
606edd16368SStephen M. Cameron 
607c1988684SScott Teel static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
608c1988684SScott Teel 	     struct device_attribute *attr, char *buf)
609c1988684SScott Teel {
610c1988684SScott Teel 	struct ctlr_info *h;
611c1988684SScott Teel 	struct scsi_device *sdev;
612c1988684SScott Teel 	struct hpsa_scsi_dev_t *hdev;
613c1988684SScott Teel 	unsigned long flags;
614c1988684SScott Teel 	int offload_enabled;
615c1988684SScott Teel 
616c1988684SScott Teel 	sdev = to_scsi_device(dev);
617c1988684SScott Teel 	h = sdev_to_hba(sdev);
618c1988684SScott Teel 	spin_lock_irqsave(&h->lock, flags);
619c1988684SScott Teel 	hdev = sdev->hostdata;
620c1988684SScott Teel 	if (!hdev) {
621c1988684SScott Teel 		spin_unlock_irqrestore(&h->lock, flags);
622c1988684SScott Teel 		return -ENODEV;
623c1988684SScott Teel 	}
624c1988684SScott Teel 	offload_enabled = hdev->offload_enabled;
625c1988684SScott Teel 	spin_unlock_irqrestore(&h->lock, flags);
626c1988684SScott Teel 	return snprintf(buf, 20, "%d\n", offload_enabled);
627c1988684SScott Teel }
628c1988684SScott Teel 
6293f5eac3aSStephen M. Cameron static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
6303f5eac3aSStephen M. Cameron static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
6313f5eac3aSStephen M. Cameron static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
6323f5eac3aSStephen M. Cameron static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
633c1988684SScott Teel static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
634c1988684SScott Teel 			host_show_hp_ssd_smart_path_enabled, NULL);
635da0697bdSScott Teel static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
636da0697bdSScott Teel 		host_show_hp_ssd_smart_path_status,
637da0697bdSScott Teel 		host_store_hp_ssd_smart_path_status);
6382ba8bfc8SStephen M. Cameron static DEVICE_ATTR(raid_offload_debug, S_IWUSR, NULL,
6392ba8bfc8SStephen M. Cameron 			host_store_raid_offload_debug);
6403f5eac3aSStephen M. Cameron static DEVICE_ATTR(firmware_revision, S_IRUGO,
6413f5eac3aSStephen M. Cameron 	host_show_firmware_revision, NULL);
6423f5eac3aSStephen M. Cameron static DEVICE_ATTR(commands_outstanding, S_IRUGO,
6433f5eac3aSStephen M. Cameron 	host_show_commands_outstanding, NULL);
6443f5eac3aSStephen M. Cameron static DEVICE_ATTR(transport_mode, S_IRUGO,
6453f5eac3aSStephen M. Cameron 	host_show_transport_mode, NULL);
646941b1cdaSStephen M. Cameron static DEVICE_ATTR(resettable, S_IRUGO,
647941b1cdaSStephen M. Cameron 	host_show_resettable, NULL);
6483f5eac3aSStephen M. Cameron 
6493f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_sdev_attrs[] = {
6503f5eac3aSStephen M. Cameron 	&dev_attr_raid_level,
6513f5eac3aSStephen M. Cameron 	&dev_attr_lunid,
6523f5eac3aSStephen M. Cameron 	&dev_attr_unique_id,
653c1988684SScott Teel 	&dev_attr_hp_ssd_smart_path_enabled,
6543f5eac3aSStephen M. Cameron 	NULL,
6553f5eac3aSStephen M. Cameron };
6563f5eac3aSStephen M. Cameron 
6573f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_shost_attrs[] = {
6583f5eac3aSStephen M. Cameron 	&dev_attr_rescan,
6593f5eac3aSStephen M. Cameron 	&dev_attr_firmware_revision,
6603f5eac3aSStephen M. Cameron 	&dev_attr_commands_outstanding,
6613f5eac3aSStephen M. Cameron 	&dev_attr_transport_mode,
662941b1cdaSStephen M. Cameron 	&dev_attr_resettable,
663da0697bdSScott Teel 	&dev_attr_hp_ssd_smart_path_status,
6642ba8bfc8SStephen M. Cameron 	&dev_attr_raid_offload_debug,
6653f5eac3aSStephen M. Cameron 	NULL,
6663f5eac3aSStephen M. Cameron };
6673f5eac3aSStephen M. Cameron 
6683f5eac3aSStephen M. Cameron static struct scsi_host_template hpsa_driver_template = {
6693f5eac3aSStephen M. Cameron 	.module			= THIS_MODULE,
670f79cfec6SStephen M. Cameron 	.name			= HPSA,
671f79cfec6SStephen M. Cameron 	.proc_name		= HPSA,
6723f5eac3aSStephen M. Cameron 	.queuecommand		= hpsa_scsi_queue_command,
6733f5eac3aSStephen M. Cameron 	.scan_start		= hpsa_scan_start,
6743f5eac3aSStephen M. Cameron 	.scan_finished		= hpsa_scan_finished,
6757c0a0229SDon Brace 	.change_queue_depth	= hpsa_change_queue_depth,
6763f5eac3aSStephen M. Cameron 	.this_id		= -1,
6773f5eac3aSStephen M. Cameron 	.use_clustering		= ENABLE_CLUSTERING,
67875167d2cSStephen M. Cameron 	.eh_abort_handler	= hpsa_eh_abort_handler,
6793f5eac3aSStephen M. Cameron 	.eh_device_reset_handler = hpsa_eh_device_reset_handler,
6803f5eac3aSStephen M. Cameron 	.ioctl			= hpsa_ioctl,
6813f5eac3aSStephen M. Cameron 	.slave_alloc		= hpsa_slave_alloc,
6823f5eac3aSStephen M. Cameron 	.slave_destroy		= hpsa_slave_destroy,
6833f5eac3aSStephen M. Cameron #ifdef CONFIG_COMPAT
6843f5eac3aSStephen M. Cameron 	.compat_ioctl		= hpsa_compat_ioctl,
6853f5eac3aSStephen M. Cameron #endif
6863f5eac3aSStephen M. Cameron 	.sdev_attrs = hpsa_sdev_attrs,
6873f5eac3aSStephen M. Cameron 	.shost_attrs = hpsa_shost_attrs,
688c0d6a4d1SStephen M. Cameron 	.max_sectors = 8192,
68954b2b50cSMartin K. Petersen 	.no_write_same = 1,
6903f5eac3aSStephen M. Cameron };
6913f5eac3aSStephen M. Cameron 
6923f5eac3aSStephen M. Cameron 
6933f5eac3aSStephen M. Cameron /* Enqueuing and dequeuing functions for cmdlists. */
6943f5eac3aSStephen M. Cameron static inline void addQ(struct list_head *list, struct CommandList *c)
6953f5eac3aSStephen M. Cameron {
6963f5eac3aSStephen M. Cameron 	list_add_tail(&c->list, list);
6973f5eac3aSStephen M. Cameron }
6983f5eac3aSStephen M. Cameron 
699254f796bSMatt Gates static inline u32 next_command(struct ctlr_info *h, u8 q)
7003f5eac3aSStephen M. Cameron {
7013f5eac3aSStephen M. Cameron 	u32 a;
702072b0518SStephen M. Cameron 	struct reply_queue_buffer *rq = &h->reply_queue[q];
7033f5eac3aSStephen M. Cameron 
704e1f7de0cSMatt Gates 	if (h->transMethod & CFGTBL_Trans_io_accel1)
705e1f7de0cSMatt Gates 		return h->access.command_completed(h, q);
706e1f7de0cSMatt Gates 
7073f5eac3aSStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
708254f796bSMatt Gates 		return h->access.command_completed(h, q);
7093f5eac3aSStephen M. Cameron 
710254f796bSMatt Gates 	if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
711254f796bSMatt Gates 		a = rq->head[rq->current_entry];
712254f796bSMatt Gates 		rq->current_entry++;
7130cbf768eSStephen M. Cameron 		atomic_dec(&h->commands_outstanding);
7143f5eac3aSStephen M. Cameron 	} else {
7153f5eac3aSStephen M. Cameron 		a = FIFO_EMPTY;
7163f5eac3aSStephen M. Cameron 	}
7173f5eac3aSStephen M. Cameron 	/* Check for wraparound */
718254f796bSMatt Gates 	if (rq->current_entry == h->max_commands) {
719254f796bSMatt Gates 		rq->current_entry = 0;
720254f796bSMatt Gates 		rq->wraparound ^= 1;
7213f5eac3aSStephen M. Cameron 	}
7223f5eac3aSStephen M. Cameron 	return a;
7233f5eac3aSStephen M. Cameron }
7243f5eac3aSStephen M. Cameron 
725c349775eSScott Teel /*
726c349775eSScott Teel  * There are some special bits in the bus address of the
727c349775eSScott Teel  * command that we have to set for the controller to know
728c349775eSScott Teel  * how to process the command:
729c349775eSScott Teel  *
730c349775eSScott Teel  * Normal performant mode:
731c349775eSScott Teel  * bit 0: 1 means performant mode, 0 means simple mode.
732c349775eSScott Teel  * bits 1-3 = block fetch table entry
733c349775eSScott Teel  * bits 4-6 = command type (== 0)
734c349775eSScott Teel  *
735c349775eSScott Teel  * ioaccel1 mode:
736c349775eSScott Teel  * bit 0 = "performant mode" bit.
737c349775eSScott Teel  * bits 1-3 = block fetch table entry
738c349775eSScott Teel  * bits 4-6 = command type (== 110)
739c349775eSScott Teel  * (command type is needed because ioaccel1 mode
740c349775eSScott Teel  * commands are submitted through the same register as normal
741c349775eSScott Teel  * mode commands, so this is how the controller knows whether
742c349775eSScott Teel  * the command is normal mode or ioaccel1 mode.)
743c349775eSScott Teel  *
744c349775eSScott Teel  * ioaccel2 mode:
745c349775eSScott Teel  * bit 0 = "performant mode" bit.
746c349775eSScott Teel  * bits 1-4 = block fetch table entry (note extra bit)
747c349775eSScott Teel  * bits 4-6 = not needed, because ioaccel2 mode has
748c349775eSScott Teel  * a separate special register for submitting commands.
749c349775eSScott Teel  */
750c349775eSScott Teel 
7513f5eac3aSStephen M. Cameron /* set_performant_mode: Modify the tag for cciss performant
7523f5eac3aSStephen M. Cameron  * set bit 0 for pull model, bits 3-1 for block fetch
7533f5eac3aSStephen M. Cameron  * register number
7543f5eac3aSStephen M. Cameron  */
7553f5eac3aSStephen M. Cameron static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
7563f5eac3aSStephen M. Cameron {
757254f796bSMatt Gates 	if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
7583f5eac3aSStephen M. Cameron 		c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
759eee0f03aSHannes Reinecke 		if (likely(h->msix_vector > 0))
760254f796bSMatt Gates 			c->Header.ReplyQueue =
761804a5cb5SJohn Kacur 				raw_smp_processor_id() % h->nreply_queues;
762254f796bSMatt Gates 	}
7633f5eac3aSStephen M. Cameron }
7643f5eac3aSStephen M. Cameron 
765c349775eSScott Teel static void set_ioaccel1_performant_mode(struct ctlr_info *h,
766c349775eSScott Teel 						struct CommandList *c)
767c349775eSScott Teel {
768c349775eSScott Teel 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
769c349775eSScott Teel 
770c349775eSScott Teel 	/* Tell the controller to post the reply to the queue for this
771c349775eSScott Teel 	 * processor.  This seems to give the best I/O throughput.
772c349775eSScott Teel 	 */
773c349775eSScott Teel 	cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
774c349775eSScott Teel 	/* Set the bits in the address sent down to include:
775c349775eSScott Teel 	 *  - performant mode bit (bit 0)
776c349775eSScott Teel 	 *  - pull count (bits 1-3)
777c349775eSScott Teel 	 *  - command type (bits 4-6)
778c349775eSScott Teel 	 */
779c349775eSScott Teel 	c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
780c349775eSScott Teel 					IOACCEL1_BUSADDR_CMDTYPE;
781c349775eSScott Teel }
782c349775eSScott Teel 
783c349775eSScott Teel static void set_ioaccel2_performant_mode(struct ctlr_info *h,
784c349775eSScott Teel 						struct CommandList *c)
785c349775eSScott Teel {
786c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
787c349775eSScott Teel 
788c349775eSScott Teel 	/* Tell the controller to post the reply to the queue for this
789c349775eSScott Teel 	 * processor.  This seems to give the best I/O throughput.
790c349775eSScott Teel 	 */
791c349775eSScott Teel 	cp->reply_queue = smp_processor_id() % h->nreply_queues;
792c349775eSScott Teel 	/* Set the bits in the address sent down to include:
793c349775eSScott Teel 	 *  - performant mode bit not used in ioaccel mode 2
794c349775eSScott Teel 	 *  - pull count (bits 0-3)
795c349775eSScott Teel 	 *  - command type isn't needed for ioaccel2
796c349775eSScott Teel 	 */
797c349775eSScott Teel 	c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
798c349775eSScott Teel }
799c349775eSScott Teel 
800e85c5974SStephen M. Cameron static int is_firmware_flash_cmd(u8 *cdb)
801e85c5974SStephen M. Cameron {
802e85c5974SStephen M. Cameron 	return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
803e85c5974SStephen M. Cameron }
804e85c5974SStephen M. Cameron 
805e85c5974SStephen M. Cameron /*
806e85c5974SStephen M. Cameron  * During firmware flash, the heartbeat register may not update as frequently
807e85c5974SStephen M. Cameron  * as it should.  So we dial down lockup detection during firmware flash. and
808e85c5974SStephen M. Cameron  * dial it back up when firmware flash completes.
809e85c5974SStephen M. Cameron  */
810e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
811e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
812e85c5974SStephen M. Cameron static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
813e85c5974SStephen M. Cameron 		struct CommandList *c)
814e85c5974SStephen M. Cameron {
815e85c5974SStephen M. Cameron 	if (!is_firmware_flash_cmd(c->Request.CDB))
816e85c5974SStephen M. Cameron 		return;
817e85c5974SStephen M. Cameron 	atomic_inc(&h->firmware_flash_in_progress);
818e85c5974SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
819e85c5974SStephen M. Cameron }
820e85c5974SStephen M. Cameron 
821e85c5974SStephen M. Cameron static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
822e85c5974SStephen M. Cameron 		struct CommandList *c)
823e85c5974SStephen M. Cameron {
824e85c5974SStephen M. Cameron 	if (is_firmware_flash_cmd(c->Request.CDB) &&
825e85c5974SStephen M. Cameron 		atomic_dec_and_test(&h->firmware_flash_in_progress))
826e85c5974SStephen M. Cameron 		h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
827e85c5974SStephen M. Cameron }
828e85c5974SStephen M. Cameron 
8293f5eac3aSStephen M. Cameron static void enqueue_cmd_and_start_io(struct ctlr_info *h,
8303f5eac3aSStephen M. Cameron 	struct CommandList *c)
8313f5eac3aSStephen M. Cameron {
8323f5eac3aSStephen M. Cameron 	unsigned long flags;
8333f5eac3aSStephen M. Cameron 
834c349775eSScott Teel 	switch (c->cmd_type) {
835c349775eSScott Teel 	case CMD_IOACCEL1:
836c349775eSScott Teel 		set_ioaccel1_performant_mode(h, c);
837c349775eSScott Teel 		break;
838c349775eSScott Teel 	case CMD_IOACCEL2:
839c349775eSScott Teel 		set_ioaccel2_performant_mode(h, c);
840c349775eSScott Teel 		break;
841c349775eSScott Teel 	default:
8423f5eac3aSStephen M. Cameron 		set_performant_mode(h, c);
843c349775eSScott Teel 	}
844e85c5974SStephen M. Cameron 	dial_down_lockup_detection_during_fw_flash(h, c);
8453f5eac3aSStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
8463f5eac3aSStephen M. Cameron 	addQ(&h->reqQ, c);
8473f5eac3aSStephen M. Cameron 	h->Qdepth++;
8480b57075dSStephen M. Cameron 	start_io(h, &flags);
8493f5eac3aSStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
8503f5eac3aSStephen M. Cameron }
8513f5eac3aSStephen M. Cameron 
8523f5eac3aSStephen M. Cameron static inline void removeQ(struct CommandList *c)
8533f5eac3aSStephen M. Cameron {
8543f5eac3aSStephen M. Cameron 	if (WARN_ON(list_empty(&c->list)))
8553f5eac3aSStephen M. Cameron 		return;
8563f5eac3aSStephen M. Cameron 	list_del_init(&c->list);
8573f5eac3aSStephen M. Cameron }
8583f5eac3aSStephen M. Cameron 
8593f5eac3aSStephen M. Cameron static inline int is_hba_lunid(unsigned char scsi3addr[])
8603f5eac3aSStephen M. Cameron {
8613f5eac3aSStephen M. Cameron 	return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
8623f5eac3aSStephen M. Cameron }
8633f5eac3aSStephen M. Cameron 
8643f5eac3aSStephen M. Cameron static inline int is_scsi_rev_5(struct ctlr_info *h)
8653f5eac3aSStephen M. Cameron {
8663f5eac3aSStephen M. Cameron 	if (!h->hba_inquiry_data)
8673f5eac3aSStephen M. Cameron 		return 0;
8683f5eac3aSStephen M. Cameron 	if ((h->hba_inquiry_data[2] & 0x07) == 5)
8693f5eac3aSStephen M. Cameron 		return 1;
8703f5eac3aSStephen M. Cameron 	return 0;
8713f5eac3aSStephen M. Cameron }
8723f5eac3aSStephen M. Cameron 
873edd16368SStephen M. Cameron static int hpsa_find_target_lun(struct ctlr_info *h,
874edd16368SStephen M. Cameron 	unsigned char scsi3addr[], int bus, int *target, int *lun)
875edd16368SStephen M. Cameron {
876edd16368SStephen M. Cameron 	/* finds an unused bus, target, lun for a new physical device
877edd16368SStephen M. Cameron 	 * assumes h->devlock is held
878edd16368SStephen M. Cameron 	 */
879edd16368SStephen M. Cameron 	int i, found = 0;
880cfe5badcSScott Teel 	DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
881edd16368SStephen M. Cameron 
882263d9401SAkinobu Mita 	bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
883edd16368SStephen M. Cameron 
884edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
885edd16368SStephen M. Cameron 		if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
886263d9401SAkinobu Mita 			__set_bit(h->dev[i]->target, lun_taken);
887edd16368SStephen M. Cameron 	}
888edd16368SStephen M. Cameron 
889263d9401SAkinobu Mita 	i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
890263d9401SAkinobu Mita 	if (i < HPSA_MAX_DEVICES) {
891edd16368SStephen M. Cameron 		/* *bus = 1; */
892edd16368SStephen M. Cameron 		*target = i;
893edd16368SStephen M. Cameron 		*lun = 0;
894edd16368SStephen M. Cameron 		found = 1;
895edd16368SStephen M. Cameron 	}
896edd16368SStephen M. Cameron 	return !found;
897edd16368SStephen M. Cameron }
898edd16368SStephen M. Cameron 
899edd16368SStephen M. Cameron /* Add an entry into h->dev[] array. */
900edd16368SStephen M. Cameron static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
901edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *device,
902edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *added[], int *nadded)
903edd16368SStephen M. Cameron {
904edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
905edd16368SStephen M. Cameron 	int n = h->ndevices;
906edd16368SStephen M. Cameron 	int i;
907edd16368SStephen M. Cameron 	unsigned char addr1[8], addr2[8];
908edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
909edd16368SStephen M. Cameron 
910cfe5badcSScott Teel 	if (n >= HPSA_MAX_DEVICES) {
911edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "too many devices, some will be "
912edd16368SStephen M. Cameron 			"inaccessible.\n");
913edd16368SStephen M. Cameron 		return -1;
914edd16368SStephen M. Cameron 	}
915edd16368SStephen M. Cameron 
916edd16368SStephen M. Cameron 	/* physical devices do not have lun or target assigned until now. */
917edd16368SStephen M. Cameron 	if (device->lun != -1)
918edd16368SStephen M. Cameron 		/* Logical device, lun is already assigned. */
919edd16368SStephen M. Cameron 		goto lun_assigned;
920edd16368SStephen M. Cameron 
921edd16368SStephen M. Cameron 	/* If this device a non-zero lun of a multi-lun device
922edd16368SStephen M. Cameron 	 * byte 4 of the 8-byte LUN addr will contain the logical
9232b08b3e9SDon Brace 	 * unit no, zero otherwise.
924edd16368SStephen M. Cameron 	 */
925edd16368SStephen M. Cameron 	if (device->scsi3addr[4] == 0) {
926edd16368SStephen M. Cameron 		/* This is not a non-zero lun of a multi-lun device */
927edd16368SStephen M. Cameron 		if (hpsa_find_target_lun(h, device->scsi3addr,
928edd16368SStephen M. Cameron 			device->bus, &device->target, &device->lun) != 0)
929edd16368SStephen M. Cameron 			return -1;
930edd16368SStephen M. Cameron 		goto lun_assigned;
931edd16368SStephen M. Cameron 	}
932edd16368SStephen M. Cameron 
933edd16368SStephen M. Cameron 	/* This is a non-zero lun of a multi-lun device.
934edd16368SStephen M. Cameron 	 * Search through our list and find the device which
935edd16368SStephen M. Cameron 	 * has the same 8 byte LUN address, excepting byte 4.
936edd16368SStephen M. Cameron 	 * Assign the same bus and target for this new LUN.
937edd16368SStephen M. Cameron 	 * Use the logical unit number from the firmware.
938edd16368SStephen M. Cameron 	 */
939edd16368SStephen M. Cameron 	memcpy(addr1, device->scsi3addr, 8);
940edd16368SStephen M. Cameron 	addr1[4] = 0;
941edd16368SStephen M. Cameron 	for (i = 0; i < n; i++) {
942edd16368SStephen M. Cameron 		sd = h->dev[i];
943edd16368SStephen M. Cameron 		memcpy(addr2, sd->scsi3addr, 8);
944edd16368SStephen M. Cameron 		addr2[4] = 0;
945edd16368SStephen M. Cameron 		/* differ only in byte 4? */
946edd16368SStephen M. Cameron 		if (memcmp(addr1, addr2, 8) == 0) {
947edd16368SStephen M. Cameron 			device->bus = sd->bus;
948edd16368SStephen M. Cameron 			device->target = sd->target;
949edd16368SStephen M. Cameron 			device->lun = device->scsi3addr[4];
950edd16368SStephen M. Cameron 			break;
951edd16368SStephen M. Cameron 		}
952edd16368SStephen M. Cameron 	}
953edd16368SStephen M. Cameron 	if (device->lun == -1) {
954edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
955edd16368SStephen M. Cameron 			" suspect firmware bug or unsupported hardware "
956edd16368SStephen M. Cameron 			"configuration.\n");
957edd16368SStephen M. Cameron 			return -1;
958edd16368SStephen M. Cameron 	}
959edd16368SStephen M. Cameron 
960edd16368SStephen M. Cameron lun_assigned:
961edd16368SStephen M. Cameron 
962edd16368SStephen M. Cameron 	h->dev[n] = device;
963edd16368SStephen M. Cameron 	h->ndevices++;
964edd16368SStephen M. Cameron 	added[*nadded] = device;
965edd16368SStephen M. Cameron 	(*nadded)++;
966edd16368SStephen M. Cameron 
967edd16368SStephen M. Cameron 	/* initially, (before registering with scsi layer) we don't
968edd16368SStephen M. Cameron 	 * know our hostno and we don't want to print anything first
969edd16368SStephen M. Cameron 	 * time anyway (the scsi layer's inquiries will show that info)
970edd16368SStephen M. Cameron 	 */
971edd16368SStephen M. Cameron 	/* if (hostno != -1) */
972edd16368SStephen M. Cameron 		dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
973edd16368SStephen M. Cameron 			scsi_device_type(device->devtype), hostno,
974edd16368SStephen M. Cameron 			device->bus, device->target, device->lun);
975edd16368SStephen M. Cameron 	return 0;
976edd16368SStephen M. Cameron }
977edd16368SStephen M. Cameron 
978bd9244f7SScott Teel /* Update an entry in h->dev[] array. */
979bd9244f7SScott Teel static void hpsa_scsi_update_entry(struct ctlr_info *h, int hostno,
980bd9244f7SScott Teel 	int entry, struct hpsa_scsi_dev_t *new_entry)
981bd9244f7SScott Teel {
982bd9244f7SScott Teel 	/* assumes h->devlock is held */
983bd9244f7SScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
984bd9244f7SScott Teel 
985bd9244f7SScott Teel 	/* Raid level changed. */
986bd9244f7SScott Teel 	h->dev[entry]->raid_level = new_entry->raid_level;
987250fb125SStephen M. Cameron 
988250fb125SStephen M. Cameron 	/* Raid offload parameters changed. */
989250fb125SStephen M. Cameron 	h->dev[entry]->offload_config = new_entry->offload_config;
990250fb125SStephen M. Cameron 	h->dev[entry]->offload_enabled = new_entry->offload_enabled;
9919fb0de2dSStephen M. Cameron 	h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
9929fb0de2dSStephen M. Cameron 	h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
9939fb0de2dSStephen M. Cameron 	h->dev[entry]->raid_map = new_entry->raid_map;
994250fb125SStephen M. Cameron 
995bd9244f7SScott Teel 	dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d updated.\n",
996bd9244f7SScott Teel 		scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
997bd9244f7SScott Teel 		new_entry->target, new_entry->lun);
998bd9244f7SScott Teel }
999bd9244f7SScott Teel 
10002a8ccf31SStephen M. Cameron /* Replace an entry from h->dev[] array. */
10012a8ccf31SStephen M. Cameron static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
10022a8ccf31SStephen M. Cameron 	int entry, struct hpsa_scsi_dev_t *new_entry,
10032a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *added[], int *nadded,
10042a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
10052a8ccf31SStephen M. Cameron {
10062a8ccf31SStephen M. Cameron 	/* assumes h->devlock is held */
1007cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
10082a8ccf31SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
10092a8ccf31SStephen M. Cameron 	(*nremoved)++;
101001350d05SStephen M. Cameron 
101101350d05SStephen M. Cameron 	/*
101201350d05SStephen M. Cameron 	 * New physical devices won't have target/lun assigned yet
101301350d05SStephen M. Cameron 	 * so we need to preserve the values in the slot we are replacing.
101401350d05SStephen M. Cameron 	 */
101501350d05SStephen M. Cameron 	if (new_entry->target == -1) {
101601350d05SStephen M. Cameron 		new_entry->target = h->dev[entry]->target;
101701350d05SStephen M. Cameron 		new_entry->lun = h->dev[entry]->lun;
101801350d05SStephen M. Cameron 	}
101901350d05SStephen M. Cameron 
10202a8ccf31SStephen M. Cameron 	h->dev[entry] = new_entry;
10212a8ccf31SStephen M. Cameron 	added[*nadded] = new_entry;
10222a8ccf31SStephen M. Cameron 	(*nadded)++;
10232a8ccf31SStephen M. Cameron 	dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n",
10242a8ccf31SStephen M. Cameron 		scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
10252a8ccf31SStephen M. Cameron 			new_entry->target, new_entry->lun);
10262a8ccf31SStephen M. Cameron }
10272a8ccf31SStephen M. Cameron 
1028edd16368SStephen M. Cameron /* Remove an entry from h->dev[] array. */
1029edd16368SStephen M. Cameron static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
1030edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
1031edd16368SStephen M. Cameron {
1032edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
1033edd16368SStephen M. Cameron 	int i;
1034edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1035edd16368SStephen M. Cameron 
1036cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
1037edd16368SStephen M. Cameron 
1038edd16368SStephen M. Cameron 	sd = h->dev[entry];
1039edd16368SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
1040edd16368SStephen M. Cameron 	(*nremoved)++;
1041edd16368SStephen M. Cameron 
1042edd16368SStephen M. Cameron 	for (i = entry; i < h->ndevices-1; i++)
1043edd16368SStephen M. Cameron 		h->dev[i] = h->dev[i+1];
1044edd16368SStephen M. Cameron 	h->ndevices--;
1045edd16368SStephen M. Cameron 	dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
1046edd16368SStephen M. Cameron 		scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
1047edd16368SStephen M. Cameron 		sd->lun);
1048edd16368SStephen M. Cameron }
1049edd16368SStephen M. Cameron 
1050edd16368SStephen M. Cameron #define SCSI3ADDR_EQ(a, b) ( \
1051edd16368SStephen M. Cameron 	(a)[7] == (b)[7] && \
1052edd16368SStephen M. Cameron 	(a)[6] == (b)[6] && \
1053edd16368SStephen M. Cameron 	(a)[5] == (b)[5] && \
1054edd16368SStephen M. Cameron 	(a)[4] == (b)[4] && \
1055edd16368SStephen M. Cameron 	(a)[3] == (b)[3] && \
1056edd16368SStephen M. Cameron 	(a)[2] == (b)[2] && \
1057edd16368SStephen M. Cameron 	(a)[1] == (b)[1] && \
1058edd16368SStephen M. Cameron 	(a)[0] == (b)[0])
1059edd16368SStephen M. Cameron 
1060edd16368SStephen M. Cameron static void fixup_botched_add(struct ctlr_info *h,
1061edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *added)
1062edd16368SStephen M. Cameron {
1063edd16368SStephen M. Cameron 	/* called when scsi_add_device fails in order to re-adjust
1064edd16368SStephen M. Cameron 	 * h->dev[] to match the mid layer's view.
1065edd16368SStephen M. Cameron 	 */
1066edd16368SStephen M. Cameron 	unsigned long flags;
1067edd16368SStephen M. Cameron 	int i, j;
1068edd16368SStephen M. Cameron 
1069edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
1070edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1071edd16368SStephen M. Cameron 		if (h->dev[i] == added) {
1072edd16368SStephen M. Cameron 			for (j = i; j < h->ndevices-1; j++)
1073edd16368SStephen M. Cameron 				h->dev[j] = h->dev[j+1];
1074edd16368SStephen M. Cameron 			h->ndevices--;
1075edd16368SStephen M. Cameron 			break;
1076edd16368SStephen M. Cameron 		}
1077edd16368SStephen M. Cameron 	}
1078edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
1079edd16368SStephen M. Cameron 	kfree(added);
1080edd16368SStephen M. Cameron }
1081edd16368SStephen M. Cameron 
1082edd16368SStephen M. Cameron static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
1083edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev2)
1084edd16368SStephen M. Cameron {
1085edd16368SStephen M. Cameron 	/* we compare everything except lun and target as these
1086edd16368SStephen M. Cameron 	 * are not yet assigned.  Compare parts likely
1087edd16368SStephen M. Cameron 	 * to differ first
1088edd16368SStephen M. Cameron 	 */
1089edd16368SStephen M. Cameron 	if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
1090edd16368SStephen M. Cameron 		sizeof(dev1->scsi3addr)) != 0)
1091edd16368SStephen M. Cameron 		return 0;
1092edd16368SStephen M. Cameron 	if (memcmp(dev1->device_id, dev2->device_id,
1093edd16368SStephen M. Cameron 		sizeof(dev1->device_id)) != 0)
1094edd16368SStephen M. Cameron 		return 0;
1095edd16368SStephen M. Cameron 	if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
1096edd16368SStephen M. Cameron 		return 0;
1097edd16368SStephen M. Cameron 	if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
1098edd16368SStephen M. Cameron 		return 0;
1099edd16368SStephen M. Cameron 	if (dev1->devtype != dev2->devtype)
1100edd16368SStephen M. Cameron 		return 0;
1101edd16368SStephen M. Cameron 	if (dev1->bus != dev2->bus)
1102edd16368SStephen M. Cameron 		return 0;
1103edd16368SStephen M. Cameron 	return 1;
1104edd16368SStephen M. Cameron }
1105edd16368SStephen M. Cameron 
1106bd9244f7SScott Teel static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
1107bd9244f7SScott Teel 	struct hpsa_scsi_dev_t *dev2)
1108bd9244f7SScott Teel {
1109bd9244f7SScott Teel 	/* Device attributes that can change, but don't mean
1110bd9244f7SScott Teel 	 * that the device is a different device, nor that the OS
1111bd9244f7SScott Teel 	 * needs to be told anything about the change.
1112bd9244f7SScott Teel 	 */
1113bd9244f7SScott Teel 	if (dev1->raid_level != dev2->raid_level)
1114bd9244f7SScott Teel 		return 1;
1115250fb125SStephen M. Cameron 	if (dev1->offload_config != dev2->offload_config)
1116250fb125SStephen M. Cameron 		return 1;
1117250fb125SStephen M. Cameron 	if (dev1->offload_enabled != dev2->offload_enabled)
1118250fb125SStephen M. Cameron 		return 1;
1119bd9244f7SScott Teel 	return 0;
1120bd9244f7SScott Teel }
1121bd9244f7SScott Teel 
1122edd16368SStephen M. Cameron /* Find needle in haystack.  If exact match found, return DEVICE_SAME,
1123edd16368SStephen M. Cameron  * and return needle location in *index.  If scsi3addr matches, but not
1124edd16368SStephen M. Cameron  * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
1125bd9244f7SScott Teel  * location in *index.
1126bd9244f7SScott Teel  * In the case of a minor device attribute change, such as RAID level, just
1127bd9244f7SScott Teel  * return DEVICE_UPDATED, along with the updated device's location in index.
1128bd9244f7SScott Teel  * If needle not found, return DEVICE_NOT_FOUND.
1129edd16368SStephen M. Cameron  */
1130edd16368SStephen M. Cameron static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
1131edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *haystack[], int haystack_size,
1132edd16368SStephen M. Cameron 	int *index)
1133edd16368SStephen M. Cameron {
1134edd16368SStephen M. Cameron 	int i;
1135edd16368SStephen M. Cameron #define DEVICE_NOT_FOUND 0
1136edd16368SStephen M. Cameron #define DEVICE_CHANGED 1
1137edd16368SStephen M. Cameron #define DEVICE_SAME 2
1138bd9244f7SScott Teel #define DEVICE_UPDATED 3
1139edd16368SStephen M. Cameron 	for (i = 0; i < haystack_size; i++) {
114023231048SStephen M. Cameron 		if (haystack[i] == NULL) /* previously removed. */
114123231048SStephen M. Cameron 			continue;
1142edd16368SStephen M. Cameron 		if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
1143edd16368SStephen M. Cameron 			*index = i;
1144bd9244f7SScott Teel 			if (device_is_the_same(needle, haystack[i])) {
1145bd9244f7SScott Teel 				if (device_updated(needle, haystack[i]))
1146bd9244f7SScott Teel 					return DEVICE_UPDATED;
1147edd16368SStephen M. Cameron 				return DEVICE_SAME;
1148bd9244f7SScott Teel 			} else {
11499846590eSStephen M. Cameron 				/* Keep offline devices offline */
11509846590eSStephen M. Cameron 				if (needle->volume_offline)
11519846590eSStephen M. Cameron 					return DEVICE_NOT_FOUND;
1152edd16368SStephen M. Cameron 				return DEVICE_CHANGED;
1153edd16368SStephen M. Cameron 			}
1154edd16368SStephen M. Cameron 		}
1155bd9244f7SScott Teel 	}
1156edd16368SStephen M. Cameron 	*index = -1;
1157edd16368SStephen M. Cameron 	return DEVICE_NOT_FOUND;
1158edd16368SStephen M. Cameron }
1159edd16368SStephen M. Cameron 
11609846590eSStephen M. Cameron static void hpsa_monitor_offline_device(struct ctlr_info *h,
11619846590eSStephen M. Cameron 					unsigned char scsi3addr[])
11629846590eSStephen M. Cameron {
11639846590eSStephen M. Cameron 	struct offline_device_entry *device;
11649846590eSStephen M. Cameron 	unsigned long flags;
11659846590eSStephen M. Cameron 
11669846590eSStephen M. Cameron 	/* Check to see if device is already on the list */
11679846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
11689846590eSStephen M. Cameron 	list_for_each_entry(device, &h->offline_device_list, offline_list) {
11699846590eSStephen M. Cameron 		if (memcmp(device->scsi3addr, scsi3addr,
11709846590eSStephen M. Cameron 			sizeof(device->scsi3addr)) == 0) {
11719846590eSStephen M. Cameron 			spin_unlock_irqrestore(&h->offline_device_lock, flags);
11729846590eSStephen M. Cameron 			return;
11739846590eSStephen M. Cameron 		}
11749846590eSStephen M. Cameron 	}
11759846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
11769846590eSStephen M. Cameron 
11779846590eSStephen M. Cameron 	/* Device is not on the list, add it. */
11789846590eSStephen M. Cameron 	device = kmalloc(sizeof(*device), GFP_KERNEL);
11799846590eSStephen M. Cameron 	if (!device) {
11809846590eSStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in %s\n", __func__);
11819846590eSStephen M. Cameron 		return;
11829846590eSStephen M. Cameron 	}
11839846590eSStephen M. Cameron 	memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
11849846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
11859846590eSStephen M. Cameron 	list_add_tail(&device->offline_list, &h->offline_device_list);
11869846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
11879846590eSStephen M. Cameron }
11889846590eSStephen M. Cameron 
11899846590eSStephen M. Cameron /* Print a message explaining various offline volume states */
11909846590eSStephen M. Cameron static void hpsa_show_volume_status(struct ctlr_info *h,
11919846590eSStephen M. Cameron 	struct hpsa_scsi_dev_t *sd)
11929846590eSStephen M. Cameron {
11939846590eSStephen M. Cameron 	if (sd->volume_offline == HPSA_VPD_LV_STATUS_UNSUPPORTED)
11949846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
11959846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume status is not available through vital product data pages.\n",
11969846590eSStephen M. Cameron 			h->scsi_host->host_no,
11979846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
11989846590eSStephen M. Cameron 	switch (sd->volume_offline) {
11999846590eSStephen M. Cameron 	case HPSA_LV_OK:
12009846590eSStephen M. Cameron 		break;
12019846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ERASE:
12029846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12039846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing background erase process.\n",
12049846590eSStephen M. Cameron 			h->scsi_host->host_no,
12059846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12069846590eSStephen M. Cameron 		break;
12079846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_RPI:
12089846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12099846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing rapid parity initialization process.\n",
12109846590eSStephen M. Cameron 			h->scsi_host->host_no,
12119846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12129846590eSStephen M. Cameron 		break;
12139846590eSStephen M. Cameron 	case HPSA_LV_PENDING_RPI:
12149846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12159846590eSStephen M. Cameron 				"C%d:B%d:T%d:L%d Volume is queued for rapid parity initialization process.\n",
12169846590eSStephen M. Cameron 				h->scsi_host->host_no,
12179846590eSStephen M. Cameron 				sd->bus, sd->target, sd->lun);
12189846590eSStephen M. Cameron 		break;
12199846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_NO_KEY:
12209846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12219846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because key is not present.\n",
12229846590eSStephen M. Cameron 			h->scsi_host->host_no,
12239846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12249846590eSStephen M. Cameron 		break;
12259846590eSStephen M. Cameron 	case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
12269846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12279846590eSStephen 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",
12289846590eSStephen M. Cameron 			h->scsi_host->host_no,
12299846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12309846590eSStephen M. Cameron 		break;
12319846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION:
12329846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12339846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing encryption process.\n",
12349846590eSStephen M. Cameron 			h->scsi_host->host_no,
12359846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12369846590eSStephen M. Cameron 		break;
12379846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
12389846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12399846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing encryption re-keying process.\n",
12409846590eSStephen M. Cameron 			h->scsi_host->host_no,
12419846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12429846590eSStephen M. Cameron 		break;
12439846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
12449846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12459846590eSStephen 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",
12469846590eSStephen M. Cameron 			h->scsi_host->host_no,
12479846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12489846590eSStephen M. Cameron 		break;
12499846590eSStephen M. Cameron 	case HPSA_LV_PENDING_ENCRYPTION:
12509846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12519846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is pending migration to encrypted state, but process has not started.\n",
12529846590eSStephen M. Cameron 			h->scsi_host->host_no,
12539846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12549846590eSStephen M. Cameron 		break;
12559846590eSStephen M. Cameron 	case HPSA_LV_PENDING_ENCRYPTION_REKEYING:
12569846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12579846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is encrypted and is pending encryption rekeying.\n",
12589846590eSStephen M. Cameron 			h->scsi_host->host_no,
12599846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12609846590eSStephen M. Cameron 		break;
12619846590eSStephen M. Cameron 	}
12629846590eSStephen M. Cameron }
12639846590eSStephen M. Cameron 
12644967bd3eSStephen M. Cameron static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
1265edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd[], int nsds)
1266edd16368SStephen M. Cameron {
1267edd16368SStephen M. Cameron 	/* sd contains scsi3 addresses and devtypes, and inquiry
1268edd16368SStephen M. Cameron 	 * data.  This function takes what's in sd to be the current
1269edd16368SStephen M. Cameron 	 * reality and updates h->dev[] to reflect that reality.
1270edd16368SStephen M. Cameron 	 */
1271edd16368SStephen M. Cameron 	int i, entry, device_change, changes = 0;
1272edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *csd;
1273edd16368SStephen M. Cameron 	unsigned long flags;
1274edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **added, **removed;
1275edd16368SStephen M. Cameron 	int nadded, nremoved;
1276edd16368SStephen M. Cameron 	struct Scsi_Host *sh = NULL;
1277edd16368SStephen M. Cameron 
1278cfe5badcSScott Teel 	added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
1279cfe5badcSScott Teel 	removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
1280edd16368SStephen M. Cameron 
1281edd16368SStephen M. Cameron 	if (!added || !removed) {
1282edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in "
1283edd16368SStephen M. Cameron 			"adjust_hpsa_scsi_table\n");
1284edd16368SStephen M. Cameron 		goto free_and_out;
1285edd16368SStephen M. Cameron 	}
1286edd16368SStephen M. Cameron 
1287edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1288edd16368SStephen M. Cameron 
1289edd16368SStephen M. Cameron 	/* find any devices in h->dev[] that are not in
1290edd16368SStephen M. Cameron 	 * sd[] and remove them from h->dev[], and for any
1291edd16368SStephen M. Cameron 	 * devices which have changed, remove the old device
1292edd16368SStephen M. Cameron 	 * info and add the new device info.
1293bd9244f7SScott Teel 	 * If minor device attributes change, just update
1294bd9244f7SScott Teel 	 * the existing device structure.
1295edd16368SStephen M. Cameron 	 */
1296edd16368SStephen M. Cameron 	i = 0;
1297edd16368SStephen M. Cameron 	nremoved = 0;
1298edd16368SStephen M. Cameron 	nadded = 0;
1299edd16368SStephen M. Cameron 	while (i < h->ndevices) {
1300edd16368SStephen M. Cameron 		csd = h->dev[i];
1301edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
1302edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
1303edd16368SStephen M. Cameron 			changes++;
1304edd16368SStephen M. Cameron 			hpsa_scsi_remove_entry(h, hostno, i,
1305edd16368SStephen M. Cameron 				removed, &nremoved);
1306edd16368SStephen M. Cameron 			continue; /* remove ^^^, hence i not incremented */
1307edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
1308edd16368SStephen M. Cameron 			changes++;
13092a8ccf31SStephen M. Cameron 			hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
13102a8ccf31SStephen M. Cameron 				added, &nadded, removed, &nremoved);
1311c7f172dcSStephen M. Cameron 			/* Set it to NULL to prevent it from being freed
1312c7f172dcSStephen M. Cameron 			 * at the bottom of hpsa_update_scsi_devices()
1313c7f172dcSStephen M. Cameron 			 */
1314c7f172dcSStephen M. Cameron 			sd[entry] = NULL;
1315bd9244f7SScott Teel 		} else if (device_change == DEVICE_UPDATED) {
1316bd9244f7SScott Teel 			hpsa_scsi_update_entry(h, hostno, i, sd[entry]);
1317edd16368SStephen M. Cameron 		}
1318edd16368SStephen M. Cameron 		i++;
1319edd16368SStephen M. Cameron 	}
1320edd16368SStephen M. Cameron 
1321edd16368SStephen M. Cameron 	/* Now, make sure every device listed in sd[] is also
1322edd16368SStephen M. Cameron 	 * listed in h->dev[], adding them if they aren't found
1323edd16368SStephen M. Cameron 	 */
1324edd16368SStephen M. Cameron 
1325edd16368SStephen M. Cameron 	for (i = 0; i < nsds; i++) {
1326edd16368SStephen M. Cameron 		if (!sd[i]) /* if already added above. */
1327edd16368SStephen M. Cameron 			continue;
13289846590eSStephen M. Cameron 
13299846590eSStephen M. Cameron 		/* Don't add devices which are NOT READY, FORMAT IN PROGRESS
13309846590eSStephen M. Cameron 		 * as the SCSI mid-layer does not handle such devices well.
13319846590eSStephen M. Cameron 		 * It relentlessly loops sending TUR at 3Hz, then READ(10)
13329846590eSStephen M. Cameron 		 * at 160Hz, and prevents the system from coming up.
13339846590eSStephen M. Cameron 		 */
13349846590eSStephen M. Cameron 		if (sd[i]->volume_offline) {
13359846590eSStephen M. Cameron 			hpsa_show_volume_status(h, sd[i]);
13369846590eSStephen M. Cameron 			dev_info(&h->pdev->dev, "c%db%dt%dl%d: temporarily offline\n",
13379846590eSStephen M. Cameron 				h->scsi_host->host_no,
13389846590eSStephen M. Cameron 				sd[i]->bus, sd[i]->target, sd[i]->lun);
13399846590eSStephen M. Cameron 			continue;
13409846590eSStephen M. Cameron 		}
13419846590eSStephen M. Cameron 
1342edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1343edd16368SStephen M. Cameron 					h->ndevices, &entry);
1344edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
1345edd16368SStephen M. Cameron 			changes++;
1346edd16368SStephen M. Cameron 			if (hpsa_scsi_add_entry(h, hostno, sd[i],
1347edd16368SStephen M. Cameron 				added, &nadded) != 0)
1348edd16368SStephen M. Cameron 				break;
1349edd16368SStephen M. Cameron 			sd[i] = NULL; /* prevent from being freed later. */
1350edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
1351edd16368SStephen M. Cameron 			/* should never happen... */
1352edd16368SStephen M. Cameron 			changes++;
1353edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev,
1354edd16368SStephen M. Cameron 				"device unexpectedly changed.\n");
1355edd16368SStephen M. Cameron 			/* but if it does happen, we just ignore that device */
1356edd16368SStephen M. Cameron 		}
1357edd16368SStephen M. Cameron 	}
1358edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1359edd16368SStephen M. Cameron 
13609846590eSStephen M. Cameron 	/* Monitor devices which are in one of several NOT READY states to be
13619846590eSStephen M. Cameron 	 * brought online later. This must be done without holding h->devlock,
13629846590eSStephen M. Cameron 	 * so don't touch h->dev[]
13639846590eSStephen M. Cameron 	 */
13649846590eSStephen M. Cameron 	for (i = 0; i < nsds; i++) {
13659846590eSStephen M. Cameron 		if (!sd[i]) /* if already added above. */
13669846590eSStephen M. Cameron 			continue;
13679846590eSStephen M. Cameron 		if (sd[i]->volume_offline)
13689846590eSStephen M. Cameron 			hpsa_monitor_offline_device(h, sd[i]->scsi3addr);
13699846590eSStephen M. Cameron 	}
13709846590eSStephen M. Cameron 
1371edd16368SStephen M. Cameron 	/* Don't notify scsi mid layer of any changes the first time through
1372edd16368SStephen M. Cameron 	 * (or if there are no changes) scsi_scan_host will do it later the
1373edd16368SStephen M. Cameron 	 * first time through.
1374edd16368SStephen M. Cameron 	 */
1375edd16368SStephen M. Cameron 	if (hostno == -1 || !changes)
1376edd16368SStephen M. Cameron 		goto free_and_out;
1377edd16368SStephen M. Cameron 
1378edd16368SStephen M. Cameron 	sh = h->scsi_host;
1379edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any removed devices */
1380edd16368SStephen M. Cameron 	for (i = 0; i < nremoved; i++) {
1381edd16368SStephen M. Cameron 		struct scsi_device *sdev =
1382edd16368SStephen M. Cameron 			scsi_device_lookup(sh, removed[i]->bus,
1383edd16368SStephen M. Cameron 				removed[i]->target, removed[i]->lun);
1384edd16368SStephen M. Cameron 		if (sdev != NULL) {
1385edd16368SStephen M. Cameron 			scsi_remove_device(sdev);
1386edd16368SStephen M. Cameron 			scsi_device_put(sdev);
1387edd16368SStephen M. Cameron 		} else {
1388edd16368SStephen M. Cameron 			/* We don't expect to get here.
1389edd16368SStephen M. Cameron 			 * future cmds to this device will get selection
1390edd16368SStephen M. Cameron 			 * timeout as if the device was gone.
1391edd16368SStephen M. Cameron 			 */
1392edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
1393edd16368SStephen M. Cameron 				" for removal.", hostno, removed[i]->bus,
1394edd16368SStephen M. Cameron 				removed[i]->target, removed[i]->lun);
1395edd16368SStephen M. Cameron 		}
1396edd16368SStephen M. Cameron 		kfree(removed[i]);
1397edd16368SStephen M. Cameron 		removed[i] = NULL;
1398edd16368SStephen M. Cameron 	}
1399edd16368SStephen M. Cameron 
1400edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any added devices */
1401edd16368SStephen M. Cameron 	for (i = 0; i < nadded; i++) {
1402edd16368SStephen M. Cameron 		if (scsi_add_device(sh, added[i]->bus,
1403edd16368SStephen M. Cameron 			added[i]->target, added[i]->lun) == 0)
1404edd16368SStephen M. Cameron 			continue;
1405edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
1406edd16368SStephen M. Cameron 			"device not added.\n", hostno, added[i]->bus,
1407edd16368SStephen M. Cameron 			added[i]->target, added[i]->lun);
1408edd16368SStephen M. Cameron 		/* now we have to remove it from h->dev,
1409edd16368SStephen M. Cameron 		 * since it didn't get added to scsi mid layer
1410edd16368SStephen M. Cameron 		 */
1411edd16368SStephen M. Cameron 		fixup_botched_add(h, added[i]);
1412edd16368SStephen M. Cameron 	}
1413edd16368SStephen M. Cameron 
1414edd16368SStephen M. Cameron free_and_out:
1415edd16368SStephen M. Cameron 	kfree(added);
1416edd16368SStephen M. Cameron 	kfree(removed);
1417edd16368SStephen M. Cameron }
1418edd16368SStephen M. Cameron 
1419edd16368SStephen M. Cameron /*
14209e03aa2fSJoe Perches  * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
1421edd16368SStephen M. Cameron  * Assume's h->devlock is held.
1422edd16368SStephen M. Cameron  */
1423edd16368SStephen M. Cameron static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1424edd16368SStephen M. Cameron 	int bus, int target, int lun)
1425edd16368SStephen M. Cameron {
1426edd16368SStephen M. Cameron 	int i;
1427edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1428edd16368SStephen M. Cameron 
1429edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1430edd16368SStephen M. Cameron 		sd = h->dev[i];
1431edd16368SStephen M. Cameron 		if (sd->bus == bus && sd->target == target && sd->lun == lun)
1432edd16368SStephen M. Cameron 			return sd;
1433edd16368SStephen M. Cameron 	}
1434edd16368SStephen M. Cameron 	return NULL;
1435edd16368SStephen M. Cameron }
1436edd16368SStephen M. Cameron 
1437edd16368SStephen M. Cameron /* link sdev->hostdata to our per-device structure. */
1438edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev)
1439edd16368SStephen M. Cameron {
1440edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1441edd16368SStephen M. Cameron 	unsigned long flags;
1442edd16368SStephen M. Cameron 	struct ctlr_info *h;
1443edd16368SStephen M. Cameron 
1444edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
1445edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1446edd16368SStephen M. Cameron 	sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1447edd16368SStephen M. Cameron 		sdev_id(sdev), sdev->lun);
1448edd16368SStephen M. Cameron 	if (sd != NULL)
1449edd16368SStephen M. Cameron 		sdev->hostdata = sd;
1450edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1451edd16368SStephen M. Cameron 	return 0;
1452edd16368SStephen M. Cameron }
1453edd16368SStephen M. Cameron 
1454edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev)
1455edd16368SStephen M. Cameron {
1456bcc44255SStephen M. Cameron 	/* nothing to do. */
1457edd16368SStephen M. Cameron }
1458edd16368SStephen M. Cameron 
145933a2ffceSStephen M. Cameron static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
146033a2ffceSStephen M. Cameron {
146133a2ffceSStephen M. Cameron 	int i;
146233a2ffceSStephen M. Cameron 
146333a2ffceSStephen M. Cameron 	if (!h->cmd_sg_list)
146433a2ffceSStephen M. Cameron 		return;
146533a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
146633a2ffceSStephen M. Cameron 		kfree(h->cmd_sg_list[i]);
146733a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = NULL;
146833a2ffceSStephen M. Cameron 	}
146933a2ffceSStephen M. Cameron 	kfree(h->cmd_sg_list);
147033a2ffceSStephen M. Cameron 	h->cmd_sg_list = NULL;
147133a2ffceSStephen M. Cameron }
147233a2ffceSStephen M. Cameron 
147333a2ffceSStephen M. Cameron static int hpsa_allocate_sg_chain_blocks(struct ctlr_info *h)
147433a2ffceSStephen M. Cameron {
147533a2ffceSStephen M. Cameron 	int i;
147633a2ffceSStephen M. Cameron 
147733a2ffceSStephen M. Cameron 	if (h->chainsize <= 0)
147833a2ffceSStephen M. Cameron 		return 0;
147933a2ffceSStephen M. Cameron 
148033a2ffceSStephen M. Cameron 	h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
148133a2ffceSStephen M. Cameron 				GFP_KERNEL);
14823d4e6af8SRobert Elliott 	if (!h->cmd_sg_list) {
14833d4e6af8SRobert Elliott 		dev_err(&h->pdev->dev, "Failed to allocate SG list\n");
148433a2ffceSStephen M. Cameron 		return -ENOMEM;
14853d4e6af8SRobert Elliott 	}
148633a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
148733a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
148833a2ffceSStephen M. Cameron 						h->chainsize, GFP_KERNEL);
14893d4e6af8SRobert Elliott 		if (!h->cmd_sg_list[i]) {
14903d4e6af8SRobert Elliott 			dev_err(&h->pdev->dev, "Failed to allocate cmd SG\n");
149133a2ffceSStephen M. Cameron 			goto clean;
149233a2ffceSStephen M. Cameron 		}
14933d4e6af8SRobert Elliott 	}
149433a2ffceSStephen M. Cameron 	return 0;
149533a2ffceSStephen M. Cameron 
149633a2ffceSStephen M. Cameron clean:
149733a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
149833a2ffceSStephen M. Cameron 	return -ENOMEM;
149933a2ffceSStephen M. Cameron }
150033a2ffceSStephen M. Cameron 
1501e2bea6dfSStephen M. Cameron static int hpsa_map_sg_chain_block(struct ctlr_info *h,
150233a2ffceSStephen M. Cameron 	struct CommandList *c)
150333a2ffceSStephen M. Cameron {
150433a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg, *chain_block;
150533a2ffceSStephen M. Cameron 	u64 temp64;
150650a0decfSStephen M. Cameron 	u32 chain_len;
150733a2ffceSStephen M. Cameron 
150833a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
150933a2ffceSStephen M. Cameron 	chain_block = h->cmd_sg_list[c->cmdindex];
151050a0decfSStephen M. Cameron 	chain_sg->Ext = cpu_to_le32(HPSA_SG_CHAIN);
151150a0decfSStephen M. Cameron 	chain_len = sizeof(*chain_sg) *
15122b08b3e9SDon Brace 		(le16_to_cpu(c->Header.SGTotal) - h->max_cmd_sg_entries);
151350a0decfSStephen M. Cameron 	chain_sg->Len = cpu_to_le32(chain_len);
151450a0decfSStephen M. Cameron 	temp64 = pci_map_single(h->pdev, chain_block, chain_len,
151533a2ffceSStephen M. Cameron 				PCI_DMA_TODEVICE);
1516e2bea6dfSStephen M. Cameron 	if (dma_mapping_error(&h->pdev->dev, temp64)) {
1517e2bea6dfSStephen M. Cameron 		/* prevent subsequent unmapping */
151850a0decfSStephen M. Cameron 		chain_sg->Addr = cpu_to_le64(0);
1519e2bea6dfSStephen M. Cameron 		return -1;
1520e2bea6dfSStephen M. Cameron 	}
152150a0decfSStephen M. Cameron 	chain_sg->Addr = cpu_to_le64(temp64);
1522e2bea6dfSStephen M. Cameron 	return 0;
152333a2ffceSStephen M. Cameron }
152433a2ffceSStephen M. Cameron 
152533a2ffceSStephen M. Cameron static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
152633a2ffceSStephen M. Cameron 	struct CommandList *c)
152733a2ffceSStephen M. Cameron {
152833a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg;
152933a2ffceSStephen M. Cameron 
153050a0decfSStephen M. Cameron 	if (le16_to_cpu(c->Header.SGTotal) <= h->max_cmd_sg_entries)
153133a2ffceSStephen M. Cameron 		return;
153233a2ffceSStephen M. Cameron 
153333a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
153450a0decfSStephen M. Cameron 	pci_unmap_single(h->pdev, le64_to_cpu(chain_sg->Addr),
153550a0decfSStephen M. Cameron 			le32_to_cpu(chain_sg->Len), PCI_DMA_TODEVICE);
153633a2ffceSStephen M. Cameron }
153733a2ffceSStephen M. Cameron 
1538a09c1441SScott Teel 
1539a09c1441SScott Teel /* Decode the various types of errors on ioaccel2 path.
1540a09c1441SScott Teel  * Return 1 for any error that should generate a RAID path retry.
1541a09c1441SScott Teel  * Return 0 for errors that don't require a RAID path retry.
1542a09c1441SScott Teel  */
1543a09c1441SScott Teel static int handle_ioaccel_mode2_error(struct ctlr_info *h,
1544c349775eSScott Teel 					struct CommandList *c,
1545c349775eSScott Teel 					struct scsi_cmnd *cmd,
1546c349775eSScott Teel 					struct io_accel2_cmd *c2)
1547c349775eSScott Teel {
1548c349775eSScott Teel 	int data_len;
1549a09c1441SScott Teel 	int retry = 0;
1550c349775eSScott Teel 
1551c349775eSScott Teel 	switch (c2->error_data.serv_response) {
1552c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_COMPLETE:
1553c349775eSScott Teel 		switch (c2->error_data.status) {
1554c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
1555c349775eSScott Teel 			break;
1556c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
1557c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1558c349775eSScott Teel 				"%s: task complete with check condition.\n",
1559c349775eSScott Teel 				"HP SSD Smart Path");
1560ee6b1889SStephen M. Cameron 			cmd->result |= SAM_STAT_CHECK_CONDITION;
1561c349775eSScott Teel 			if (c2->error_data.data_present !=
1562ee6b1889SStephen M. Cameron 					IOACCEL2_SENSE_DATA_PRESENT) {
1563ee6b1889SStephen M. Cameron 				memset(cmd->sense_buffer, 0,
1564ee6b1889SStephen M. Cameron 					SCSI_SENSE_BUFFERSIZE);
1565c349775eSScott Teel 				break;
1566ee6b1889SStephen M. Cameron 			}
1567c349775eSScott Teel 			/* copy the sense data */
1568c349775eSScott Teel 			data_len = c2->error_data.sense_data_len;
1569c349775eSScott Teel 			if (data_len > SCSI_SENSE_BUFFERSIZE)
1570c349775eSScott Teel 				data_len = SCSI_SENSE_BUFFERSIZE;
1571c349775eSScott Teel 			if (data_len > sizeof(c2->error_data.sense_data_buff))
1572c349775eSScott Teel 				data_len =
1573c349775eSScott Teel 					sizeof(c2->error_data.sense_data_buff);
1574c349775eSScott Teel 			memcpy(cmd->sense_buffer,
1575c349775eSScott Teel 				c2->error_data.sense_data_buff, data_len);
1576a09c1441SScott Teel 			retry = 1;
1577c349775eSScott Teel 			break;
1578c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
1579c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1580c349775eSScott Teel 				"%s: task complete with BUSY status.\n",
1581c349775eSScott Teel 				"HP SSD Smart Path");
1582a09c1441SScott Teel 			retry = 1;
1583c349775eSScott Teel 			break;
1584c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
1585c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1586c349775eSScott Teel 				"%s: task complete with reservation conflict.\n",
1587c349775eSScott Teel 				"HP SSD Smart Path");
1588a09c1441SScott Teel 			retry = 1;
1589c349775eSScott Teel 			break;
1590c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
1591c349775eSScott Teel 			/* Make scsi midlayer do unlimited retries */
1592c349775eSScott Teel 			cmd->result = DID_IMM_RETRY << 16;
1593c349775eSScott Teel 			break;
1594c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
1595c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1596c349775eSScott Teel 				"%s: task complete with aborted status.\n",
1597c349775eSScott Teel 				"HP SSD Smart Path");
1598a09c1441SScott Teel 			retry = 1;
1599c349775eSScott Teel 			break;
1600c349775eSScott Teel 		default:
1601c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1602c349775eSScott Teel 				"%s: task complete with unrecognized status: 0x%02x\n",
1603c349775eSScott Teel 				"HP SSD Smart Path", c2->error_data.status);
1604a09c1441SScott Teel 			retry = 1;
1605c349775eSScott Teel 			break;
1606c349775eSScott Teel 		}
1607c349775eSScott Teel 		break;
1608c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_FAILURE:
1609c349775eSScott Teel 		/* don't expect to get here. */
1610c349775eSScott Teel 		dev_warn(&h->pdev->dev,
1611c349775eSScott Teel 			"unexpected delivery or target failure, status = 0x%02x\n",
1612c349775eSScott Teel 			c2->error_data.status);
1613a09c1441SScott Teel 		retry = 1;
1614c349775eSScott Teel 		break;
1615c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
1616c349775eSScott Teel 		break;
1617c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
1618c349775eSScott Teel 		break;
1619c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
1620c349775eSScott Teel 		dev_warn(&h->pdev->dev, "task management function rejected.\n");
1621a09c1441SScott Teel 		retry = 1;
1622c349775eSScott Teel 		break;
1623c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
1624c349775eSScott Teel 		dev_warn(&h->pdev->dev, "task management function invalid LUN\n");
1625c349775eSScott Teel 		break;
1626c349775eSScott Teel 	default:
1627c349775eSScott Teel 		dev_warn(&h->pdev->dev,
1628c349775eSScott Teel 			"%s: Unrecognized server response: 0x%02x\n",
1629a09c1441SScott Teel 			"HP SSD Smart Path",
1630a09c1441SScott Teel 			c2->error_data.serv_response);
1631a09c1441SScott Teel 		retry = 1;
1632c349775eSScott Teel 		break;
1633c349775eSScott Teel 	}
1634a09c1441SScott Teel 
1635a09c1441SScott Teel 	return retry;	/* retry on raid path? */
1636c349775eSScott Teel }
1637c349775eSScott Teel 
1638c349775eSScott Teel static void process_ioaccel2_completion(struct ctlr_info *h,
1639c349775eSScott Teel 		struct CommandList *c, struct scsi_cmnd *cmd,
1640c349775eSScott Teel 		struct hpsa_scsi_dev_t *dev)
1641c349775eSScott Teel {
1642c349775eSScott Teel 	struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
1643a09c1441SScott Teel 	int raid_retry = 0;
1644c349775eSScott Teel 
1645c349775eSScott Teel 	/* check for good status */
1646c349775eSScott Teel 	if (likely(c2->error_data.serv_response == 0 &&
1647c349775eSScott Teel 			c2->error_data.status == 0)) {
1648c349775eSScott Teel 		cmd_free(h, c);
1649c349775eSScott Teel 		cmd->scsi_done(cmd);
1650c349775eSScott Teel 		return;
1651c349775eSScott Teel 	}
1652c349775eSScott Teel 
1653c349775eSScott Teel 	/* Any RAID offload error results in retry which will use
1654c349775eSScott Teel 	 * the normal I/O path so the controller can handle whatever's
1655c349775eSScott Teel 	 * wrong.
1656c349775eSScott Teel 	 */
1657c349775eSScott Teel 	if (is_logical_dev_addr_mode(dev->scsi3addr) &&
1658c349775eSScott Teel 		c2->error_data.serv_response ==
1659c349775eSScott Teel 			IOACCEL2_SERV_RESPONSE_FAILURE) {
1660c349775eSScott Teel 		dev->offload_enabled = 0;
1661e863d68eSScott Teel 		h->drv_req_rescan = 1;	/* schedule controller for a rescan */
1662c349775eSScott Teel 		cmd->result = DID_SOFT_ERROR << 16;
1663c349775eSScott Teel 		cmd_free(h, c);
1664c349775eSScott Teel 		cmd->scsi_done(cmd);
1665c349775eSScott Teel 		return;
1666c349775eSScott Teel 	}
1667a09c1441SScott Teel 	raid_retry = handle_ioaccel_mode2_error(h, c, cmd, c2);
1668a09c1441SScott Teel 	/* If error found, disable Smart Path, schedule a rescan,
1669a09c1441SScott Teel 	 * and force a retry on the standard path.
1670a09c1441SScott Teel 	 */
1671a09c1441SScott Teel 	if (raid_retry) {
1672a09c1441SScott Teel 		dev_warn(&h->pdev->dev, "%s: Retrying on standard path.\n",
1673a09c1441SScott Teel 			"HP SSD Smart Path");
1674a09c1441SScott Teel 		dev->offload_enabled = 0; /* Disable Smart Path */
1675a09c1441SScott Teel 		h->drv_req_rescan = 1;	  /* schedule controller rescan */
1676a09c1441SScott Teel 		cmd->result = DID_SOFT_ERROR << 16;
1677a09c1441SScott Teel 	}
1678c349775eSScott Teel 	cmd_free(h, c);
1679c349775eSScott Teel 	cmd->scsi_done(cmd);
1680c349775eSScott Teel }
1681c349775eSScott Teel 
16821fb011fbSStephen M. Cameron static void complete_scsi_command(struct CommandList *cp)
1683edd16368SStephen M. Cameron {
1684edd16368SStephen M. Cameron 	struct scsi_cmnd *cmd;
1685edd16368SStephen M. Cameron 	struct ctlr_info *h;
1686edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
1687283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
1688edd16368SStephen M. Cameron 
1689edd16368SStephen M. Cameron 	unsigned char sense_key;
1690edd16368SStephen M. Cameron 	unsigned char asc;      /* additional sense code */
1691edd16368SStephen M. Cameron 	unsigned char ascq;     /* additional sense code qualifier */
1692db111e18SStephen M. Cameron 	unsigned long sense_data_size;
1693edd16368SStephen M. Cameron 
1694edd16368SStephen M. Cameron 	ei = cp->err_info;
1695edd16368SStephen M. Cameron 	cmd = (struct scsi_cmnd *) cp->scsi_cmd;
1696edd16368SStephen M. Cameron 	h = cp->h;
1697283b4a9bSStephen M. Cameron 	dev = cmd->device->hostdata;
1698edd16368SStephen M. Cameron 
1699edd16368SStephen M. Cameron 	scsi_dma_unmap(cmd); /* undo the DMA mappings */
1700e1f7de0cSMatt Gates 	if ((cp->cmd_type == CMD_SCSI) &&
17012b08b3e9SDon Brace 		(le16_to_cpu(cp->Header.SGTotal) > h->max_cmd_sg_entries))
170233a2ffceSStephen M. Cameron 		hpsa_unmap_sg_chain_block(h, cp);
1703edd16368SStephen M. Cameron 
1704edd16368SStephen M. Cameron 	cmd->result = (DID_OK << 16); 		/* host byte */
1705edd16368SStephen M. Cameron 	cmd->result |= (COMMAND_COMPLETE << 8);	/* msg byte */
1706c349775eSScott Teel 
1707c349775eSScott Teel 	if (cp->cmd_type == CMD_IOACCEL2)
1708c349775eSScott Teel 		return process_ioaccel2_completion(h, cp, cmd, dev);
1709c349775eSScott Teel 
17105512672fSStephen M. Cameron 	cmd->result |= ei->ScsiStatus;
1711edd16368SStephen M. Cameron 
17126aa4c361SRobert Elliott 	scsi_set_resid(cmd, ei->ResidualCnt);
17136aa4c361SRobert Elliott 	if (ei->CommandStatus == 0) {
17146aa4c361SRobert Elliott 		cmd_free(h, cp);
17156aa4c361SRobert Elliott 		cmd->scsi_done(cmd);
17166aa4c361SRobert Elliott 		return;
17176aa4c361SRobert Elliott 	}
17186aa4c361SRobert Elliott 
17196aa4c361SRobert Elliott 	/* copy the sense data */
1720db111e18SStephen M. Cameron 	if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
1721db111e18SStephen M. Cameron 		sense_data_size = SCSI_SENSE_BUFFERSIZE;
1722db111e18SStephen M. Cameron 	else
1723db111e18SStephen M. Cameron 		sense_data_size = sizeof(ei->SenseInfo);
1724db111e18SStephen M. Cameron 	if (ei->SenseLen < sense_data_size)
1725db111e18SStephen M. Cameron 		sense_data_size = ei->SenseLen;
1726db111e18SStephen M. Cameron 
1727db111e18SStephen M. Cameron 	memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
1728edd16368SStephen M. Cameron 
1729e1f7de0cSMatt Gates 	/* For I/O accelerator commands, copy over some fields to the normal
1730e1f7de0cSMatt Gates 	 * CISS header used below for error handling.
1731e1f7de0cSMatt Gates 	 */
1732e1f7de0cSMatt Gates 	if (cp->cmd_type == CMD_IOACCEL1) {
1733e1f7de0cSMatt Gates 		struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
17342b08b3e9SDon Brace 		cp->Header.SGList = scsi_sg_count(cmd);
17352b08b3e9SDon Brace 		cp->Header.SGTotal = cpu_to_le16(cp->Header.SGList);
17362b08b3e9SDon Brace 		cp->Request.CDBLen = le16_to_cpu(c->io_flags) &
17372b08b3e9SDon Brace 			IOACCEL1_IOFLAGS_CDBLEN_MASK;
173850a0decfSStephen M. Cameron 		cp->Header.tag = c->tag;
1739e1f7de0cSMatt Gates 		memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
1740e1f7de0cSMatt Gates 		memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
1741283b4a9bSStephen M. Cameron 
1742283b4a9bSStephen M. Cameron 		/* Any RAID offload error results in retry which will use
1743283b4a9bSStephen M. Cameron 		 * the normal I/O path so the controller can handle whatever's
1744283b4a9bSStephen M. Cameron 		 * wrong.
1745283b4a9bSStephen M. Cameron 		 */
1746283b4a9bSStephen M. Cameron 		if (is_logical_dev_addr_mode(dev->scsi3addr)) {
1747283b4a9bSStephen M. Cameron 			if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
1748283b4a9bSStephen M. Cameron 				dev->offload_enabled = 0;
1749283b4a9bSStephen M. Cameron 			cmd->result = DID_SOFT_ERROR << 16;
1750283b4a9bSStephen M. Cameron 			cmd_free(h, cp);
1751283b4a9bSStephen M. Cameron 			cmd->scsi_done(cmd);
1752283b4a9bSStephen M. Cameron 			return;
1753283b4a9bSStephen M. Cameron 		}
1754e1f7de0cSMatt Gates 	}
1755e1f7de0cSMatt Gates 
1756edd16368SStephen M. Cameron 	/* an error has occurred */
1757edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
1758edd16368SStephen M. Cameron 
1759edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
1760edd16368SStephen M. Cameron 		if (ei->ScsiStatus) {
1761edd16368SStephen M. Cameron 			/* Get sense key */
1762edd16368SStephen M. Cameron 			sense_key = 0xf & ei->SenseInfo[2];
1763edd16368SStephen M. Cameron 			/* Get additional sense code */
1764edd16368SStephen M. Cameron 			asc = ei->SenseInfo[12];
1765edd16368SStephen M. Cameron 			/* Get addition sense code qualifier */
1766edd16368SStephen M. Cameron 			ascq = ei->SenseInfo[13];
1767edd16368SStephen M. Cameron 		}
1768edd16368SStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
17691d3b3609SMatt Gates 			if (sense_key == ABORTED_COMMAND) {
17702e311fbaSStephen M. Cameron 				cmd->result |= DID_SOFT_ERROR << 16;
17711d3b3609SMatt Gates 				break;
17721d3b3609SMatt Gates 			}
1773edd16368SStephen M. Cameron 			break;
1774edd16368SStephen M. Cameron 		}
1775edd16368SStephen M. Cameron 		/* Problem was not a check condition
1776edd16368SStephen M. Cameron 		 * Pass it up to the upper layers...
1777edd16368SStephen M. Cameron 		 */
1778edd16368SStephen M. Cameron 		if (ei->ScsiStatus) {
1779edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
1780edd16368SStephen M. Cameron 				"Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1781edd16368SStephen M. Cameron 				"Returning result: 0x%x\n",
1782edd16368SStephen M. Cameron 				cp, ei->ScsiStatus,
1783edd16368SStephen M. Cameron 				sense_key, asc, ascq,
1784edd16368SStephen M. Cameron 				cmd->result);
1785edd16368SStephen M. Cameron 		} else {  /* scsi status is zero??? How??? */
1786edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
1787edd16368SStephen M. Cameron 				"Returning no connection.\n", cp),
1788edd16368SStephen M. Cameron 
1789edd16368SStephen M. Cameron 			/* Ordinarily, this case should never happen,
1790edd16368SStephen M. Cameron 			 * but there is a bug in some released firmware
1791edd16368SStephen M. Cameron 			 * revisions that allows it to happen if, for
1792edd16368SStephen M. Cameron 			 * example, a 4100 backplane loses power and
1793edd16368SStephen M. Cameron 			 * the tape drive is in it.  We assume that
1794edd16368SStephen M. Cameron 			 * it's a fatal error of some kind because we
1795edd16368SStephen M. Cameron 			 * can't show that it wasn't. We will make it
1796edd16368SStephen M. Cameron 			 * look like selection timeout since that is
1797edd16368SStephen M. Cameron 			 * the most common reason for this to occur,
1798edd16368SStephen M. Cameron 			 * and it's severe enough.
1799edd16368SStephen M. Cameron 			 */
1800edd16368SStephen M. Cameron 
1801edd16368SStephen M. Cameron 			cmd->result = DID_NO_CONNECT << 16;
1802edd16368SStephen M. Cameron 		}
1803edd16368SStephen M. Cameron 		break;
1804edd16368SStephen M. Cameron 
1805edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1806edd16368SStephen M. Cameron 		break;
1807edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
1808edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p has"
1809edd16368SStephen M. Cameron 			" completed with data overrun "
1810edd16368SStephen M. Cameron 			"reported\n", cp);
1811edd16368SStephen M. Cameron 		break;
1812edd16368SStephen M. Cameron 	case CMD_INVALID: {
1813edd16368SStephen M. Cameron 		/* print_bytes(cp, sizeof(*cp), 1, 0);
1814edd16368SStephen M. Cameron 		print_cmd(cp); */
1815edd16368SStephen M. Cameron 		/* We get CMD_INVALID if you address a non-existent device
1816edd16368SStephen M. Cameron 		 * instead of a selection timeout (no response).  You will
1817edd16368SStephen M. Cameron 		 * see this if you yank out a drive, then try to access it.
1818edd16368SStephen M. Cameron 		 * This is kind of a shame because it means that any other
1819edd16368SStephen M. Cameron 		 * CMD_INVALID (e.g. driver bug) will get interpreted as a
1820edd16368SStephen M. Cameron 		 * missing target. */
1821edd16368SStephen M. Cameron 		cmd->result = DID_NO_CONNECT << 16;
1822edd16368SStephen M. Cameron 	}
1823edd16368SStephen M. Cameron 		break;
1824edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
1825256d0eaaSStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1826edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p has "
1827edd16368SStephen M. Cameron 			"protocol error\n", cp);
1828edd16368SStephen M. Cameron 		break;
1829edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
1830edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1831edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p had  hardware error\n", cp);
1832edd16368SStephen M. Cameron 		break;
1833edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
1834edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1835edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
1836edd16368SStephen M. Cameron 		break;
1837edd16368SStephen M. Cameron 	case CMD_ABORTED:
1838edd16368SStephen M. Cameron 		cmd->result = DID_ABORT << 16;
1839edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
1840edd16368SStephen M. Cameron 				cp, ei->ScsiStatus);
1841edd16368SStephen M. Cameron 		break;
1842edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
1843edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1844edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
1845edd16368SStephen M. Cameron 		break;
1846edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
1847f6e76055SStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
1848f6e76055SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p aborted due to an unsolicited "
1849edd16368SStephen M. Cameron 			"abort\n", cp);
1850edd16368SStephen M. Cameron 		break;
1851edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
1852edd16368SStephen M. Cameron 		cmd->result = DID_TIME_OUT << 16;
1853edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
1854edd16368SStephen M. Cameron 		break;
18551d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
18561d5e2ed0SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
18571d5e2ed0SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Command unabortable\n");
18581d5e2ed0SStephen M. Cameron 		break;
1859283b4a9bSStephen M. Cameron 	case CMD_IOACCEL_DISABLED:
1860283b4a9bSStephen M. Cameron 		/* This only handles the direct pass-through case since RAID
1861283b4a9bSStephen M. Cameron 		 * offload is handled above.  Just attempt a retry.
1862283b4a9bSStephen M. Cameron 		 */
1863283b4a9bSStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16;
1864283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev,
1865283b4a9bSStephen M. Cameron 				"cp %p had HP SSD Smart Path error\n", cp);
1866283b4a9bSStephen M. Cameron 		break;
1867edd16368SStephen M. Cameron 	default:
1868edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1869edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
1870edd16368SStephen M. Cameron 				cp, ei->CommandStatus);
1871edd16368SStephen M. Cameron 	}
1872edd16368SStephen M. Cameron 	cmd_free(h, cp);
18732cc5bfafSTomas Henzl 	cmd->scsi_done(cmd);
1874edd16368SStephen M. Cameron }
1875edd16368SStephen M. Cameron 
1876edd16368SStephen M. Cameron static void hpsa_pci_unmap(struct pci_dev *pdev,
1877edd16368SStephen M. Cameron 	struct CommandList *c, int sg_used, int data_direction)
1878edd16368SStephen M. Cameron {
1879edd16368SStephen M. Cameron 	int i;
1880edd16368SStephen M. Cameron 
188150a0decfSStephen M. Cameron 	for (i = 0; i < sg_used; i++)
188250a0decfSStephen M. Cameron 		pci_unmap_single(pdev, (dma_addr_t) le64_to_cpu(c->SG[i].Addr),
188350a0decfSStephen M. Cameron 				le32_to_cpu(c->SG[i].Len),
1884edd16368SStephen M. Cameron 				data_direction);
1885edd16368SStephen M. Cameron }
1886edd16368SStephen M. Cameron 
1887a2dac136SStephen M. Cameron static int hpsa_map_one(struct pci_dev *pdev,
1888edd16368SStephen M. Cameron 		struct CommandList *cp,
1889edd16368SStephen M. Cameron 		unsigned char *buf,
1890edd16368SStephen M. Cameron 		size_t buflen,
1891edd16368SStephen M. Cameron 		int data_direction)
1892edd16368SStephen M. Cameron {
189301a02ffcSStephen M. Cameron 	u64 addr64;
1894edd16368SStephen M. Cameron 
1895edd16368SStephen M. Cameron 	if (buflen == 0 || data_direction == PCI_DMA_NONE) {
1896edd16368SStephen M. Cameron 		cp->Header.SGList = 0;
189750a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(0);
1898a2dac136SStephen M. Cameron 		return 0;
1899edd16368SStephen M. Cameron 	}
1900edd16368SStephen M. Cameron 
190150a0decfSStephen M. Cameron 	addr64 = pci_map_single(pdev, buf, buflen, data_direction);
1902eceaae18SShuah Khan 	if (dma_mapping_error(&pdev->dev, addr64)) {
1903a2dac136SStephen M. Cameron 		/* Prevent subsequent unmap of something never mapped */
1904eceaae18SShuah Khan 		cp->Header.SGList = 0;
190550a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(0);
1906a2dac136SStephen M. Cameron 		return -1;
1907eceaae18SShuah Khan 	}
190850a0decfSStephen M. Cameron 	cp->SG[0].Addr = cpu_to_le64(addr64);
190950a0decfSStephen M. Cameron 	cp->SG[0].Len = cpu_to_le32(buflen);
191050a0decfSStephen M. Cameron 	cp->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* we are not chaining */
191150a0decfSStephen M. Cameron 	cp->Header.SGList = 1;   /* no. SGs contig in this cmd */
191250a0decfSStephen M. Cameron 	cp->Header.SGTotal = cpu_to_le16(1); /* total sgs in cmd list */
1913a2dac136SStephen M. Cameron 	return 0;
1914edd16368SStephen M. Cameron }
1915edd16368SStephen M. Cameron 
1916edd16368SStephen M. Cameron static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
1917edd16368SStephen M. Cameron 	struct CommandList *c)
1918edd16368SStephen M. Cameron {
1919edd16368SStephen M. Cameron 	DECLARE_COMPLETION_ONSTACK(wait);
1920edd16368SStephen M. Cameron 
1921edd16368SStephen M. Cameron 	c->waiting = &wait;
1922edd16368SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
1923edd16368SStephen M. Cameron 	wait_for_completion(&wait);
1924edd16368SStephen M. Cameron }
1925edd16368SStephen M. Cameron 
1926094963daSStephen M. Cameron static u32 lockup_detected(struct ctlr_info *h)
1927094963daSStephen M. Cameron {
1928094963daSStephen M. Cameron 	int cpu;
1929094963daSStephen M. Cameron 	u32 rc, *lockup_detected;
1930094963daSStephen M. Cameron 
1931094963daSStephen M. Cameron 	cpu = get_cpu();
1932094963daSStephen M. Cameron 	lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
1933094963daSStephen M. Cameron 	rc = *lockup_detected;
1934094963daSStephen M. Cameron 	put_cpu();
1935094963daSStephen M. Cameron 	return rc;
1936094963daSStephen M. Cameron }
1937094963daSStephen M. Cameron 
1938a0c12413SStephen M. Cameron static void hpsa_scsi_do_simple_cmd_core_if_no_lockup(struct ctlr_info *h,
1939a0c12413SStephen M. Cameron 	struct CommandList *c)
1940a0c12413SStephen M. Cameron {
1941a0c12413SStephen M. Cameron 	/* If controller lockup detected, fake a hardware error. */
1942094963daSStephen M. Cameron 	if (unlikely(lockup_detected(h)))
1943a0c12413SStephen M. Cameron 		c->err_info->CommandStatus = CMD_HARDWARE_ERR;
1944094963daSStephen M. Cameron 	else
1945a0c12413SStephen M. Cameron 		hpsa_scsi_do_simple_cmd_core(h, c);
1946a0c12413SStephen M. Cameron }
1947a0c12413SStephen M. Cameron 
19489c2fc160SStephen M. Cameron #define MAX_DRIVER_CMD_RETRIES 25
1949edd16368SStephen M. Cameron static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
1950edd16368SStephen M. Cameron 	struct CommandList *c, int data_direction)
1951edd16368SStephen M. Cameron {
19529c2fc160SStephen M. Cameron 	int backoff_time = 10, retry_count = 0;
1953edd16368SStephen M. Cameron 
1954edd16368SStephen M. Cameron 	do {
19557630abd0SJoe Perches 		memset(c->err_info, 0, sizeof(*c->err_info));
1956edd16368SStephen M. Cameron 		hpsa_scsi_do_simple_cmd_core(h, c);
1957edd16368SStephen M. Cameron 		retry_count++;
19589c2fc160SStephen M. Cameron 		if (retry_count > 3) {
19599c2fc160SStephen M. Cameron 			msleep(backoff_time);
19609c2fc160SStephen M. Cameron 			if (backoff_time < 1000)
19619c2fc160SStephen M. Cameron 				backoff_time *= 2;
19629c2fc160SStephen M. Cameron 		}
1963852af20aSMatt Bondurant 	} while ((check_for_unit_attention(h, c) ||
19649c2fc160SStephen M. Cameron 			check_for_busy(h, c)) &&
19659c2fc160SStephen M. Cameron 			retry_count <= MAX_DRIVER_CMD_RETRIES);
1966edd16368SStephen M. Cameron 	hpsa_pci_unmap(h->pdev, c, 1, data_direction);
1967edd16368SStephen M. Cameron }
1968edd16368SStephen M. Cameron 
1969d1e8beacSStephen M. Cameron static void hpsa_print_cmd(struct ctlr_info *h, char *txt,
1970d1e8beacSStephen M. Cameron 				struct CommandList *c)
1971edd16368SStephen M. Cameron {
1972d1e8beacSStephen M. Cameron 	const u8 *cdb = c->Request.CDB;
1973d1e8beacSStephen M. Cameron 	const u8 *lun = c->Header.LUN.LunAddrBytes;
1974edd16368SStephen M. Cameron 
1975d1e8beacSStephen M. Cameron 	dev_warn(&h->pdev->dev, "%s: LUN:%02x%02x%02x%02x%02x%02x%02x%02x"
1976d1e8beacSStephen M. Cameron 	" CDB:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
1977d1e8beacSStephen M. Cameron 		txt, lun[0], lun[1], lun[2], lun[3],
1978d1e8beacSStephen M. Cameron 		lun[4], lun[5], lun[6], lun[7],
1979d1e8beacSStephen M. Cameron 		cdb[0], cdb[1], cdb[2], cdb[3],
1980d1e8beacSStephen M. Cameron 		cdb[4], cdb[5], cdb[6], cdb[7],
1981d1e8beacSStephen M. Cameron 		cdb[8], cdb[9], cdb[10], cdb[11],
1982d1e8beacSStephen M. Cameron 		cdb[12], cdb[13], cdb[14], cdb[15]);
1983d1e8beacSStephen M. Cameron }
1984d1e8beacSStephen M. Cameron 
1985d1e8beacSStephen M. Cameron static void hpsa_scsi_interpret_error(struct ctlr_info *h,
1986d1e8beacSStephen M. Cameron 			struct CommandList *cp)
1987d1e8beacSStephen M. Cameron {
1988d1e8beacSStephen M. Cameron 	const struct ErrorInfo *ei = cp->err_info;
1989d1e8beacSStephen M. Cameron 	struct device *d = &cp->h->pdev->dev;
1990d1e8beacSStephen M. Cameron 	const u8 *sd = ei->SenseInfo;
1991d1e8beacSStephen M. Cameron 
1992edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
1993edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
1994d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "SCSI status", cp);
1995d1e8beacSStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION)
1996d1e8beacSStephen M. Cameron 			dev_warn(d, "SCSI Status = 02, Sense key = %02x, ASC = %02x, ASCQ = %02x\n",
1997d1e8beacSStephen M. Cameron 				sd[2] & 0x0f, sd[12], sd[13]);
1998d1e8beacSStephen M. Cameron 		else
1999d1e8beacSStephen M. Cameron 			dev_warn(d, "SCSI Status = %02x\n", ei->ScsiStatus);
2000edd16368SStephen M. Cameron 		if (ei->ScsiStatus == 0)
2001edd16368SStephen M. Cameron 			dev_warn(d, "SCSI status is abnormally zero.  "
2002edd16368SStephen M. Cameron 			"(probably indicates selection timeout "
2003edd16368SStephen M. Cameron 			"reported incorrectly due to a known "
2004edd16368SStephen M. Cameron 			"firmware bug, circa July, 2001.)\n");
2005edd16368SStephen M. Cameron 		break;
2006edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
2007edd16368SStephen M. Cameron 		break;
2008edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
2009d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "overrun condition", cp);
2010edd16368SStephen M. Cameron 		break;
2011edd16368SStephen M. Cameron 	case CMD_INVALID: {
2012edd16368SStephen M. Cameron 		/* controller unfortunately reports SCSI passthru's
2013edd16368SStephen M. Cameron 		 * to non-existent targets as invalid commands.
2014edd16368SStephen M. Cameron 		 */
2015d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "invalid command", cp);
2016d1e8beacSStephen M. Cameron 		dev_warn(d, "probably means device no longer present\n");
2017edd16368SStephen M. Cameron 		}
2018edd16368SStephen M. Cameron 		break;
2019edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
2020d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "protocol error", cp);
2021edd16368SStephen M. Cameron 		break;
2022edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
2023d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "hardware error", cp);
2024edd16368SStephen M. Cameron 		break;
2025edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
2026d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "connection lost", cp);
2027edd16368SStephen M. Cameron 		break;
2028edd16368SStephen M. Cameron 	case CMD_ABORTED:
2029d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "aborted", cp);
2030edd16368SStephen M. Cameron 		break;
2031edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
2032d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "abort failed", cp);
2033edd16368SStephen M. Cameron 		break;
2034edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
2035d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unsolicited abort", cp);
2036edd16368SStephen M. Cameron 		break;
2037edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
2038d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "timed out", cp);
2039edd16368SStephen M. Cameron 		break;
20401d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
2041d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unabortable", cp);
20421d5e2ed0SStephen M. Cameron 		break;
2043edd16368SStephen M. Cameron 	default:
2044d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unknown status", cp);
2045d1e8beacSStephen M. Cameron 		dev_warn(d, "Unknown command status %x\n",
2046edd16368SStephen M. Cameron 				ei->CommandStatus);
2047edd16368SStephen M. Cameron 	}
2048edd16368SStephen M. Cameron }
2049edd16368SStephen M. Cameron 
2050edd16368SStephen M. Cameron static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
2051b7bb24ebSStephen M. Cameron 			u16 page, unsigned char *buf,
2052edd16368SStephen M. Cameron 			unsigned char bufsize)
2053edd16368SStephen M. Cameron {
2054edd16368SStephen M. Cameron 	int rc = IO_OK;
2055edd16368SStephen M. Cameron 	struct CommandList *c;
2056edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2057edd16368SStephen M. Cameron 
2058*45fcb86eSStephen Cameron 	c = cmd_alloc(h);
2059edd16368SStephen M. Cameron 
2060edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
2061*45fcb86eSStephen Cameron 		dev_warn(&h->pdev->dev, "cmd_alloc returned NULL!\n");
2062ecd9aad4SStephen M. Cameron 		return -ENOMEM;
2063edd16368SStephen M. Cameron 	}
2064edd16368SStephen M. Cameron 
2065a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
2066a2dac136SStephen M. Cameron 			page, scsi3addr, TYPE_CMD)) {
2067a2dac136SStephen M. Cameron 		rc = -1;
2068a2dac136SStephen M. Cameron 		goto out;
2069a2dac136SStephen M. Cameron 	}
2070edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2071edd16368SStephen M. Cameron 	ei = c->err_info;
2072edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2073d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2074edd16368SStephen M. Cameron 		rc = -1;
2075edd16368SStephen M. Cameron 	}
2076a2dac136SStephen M. Cameron out:
2077*45fcb86eSStephen Cameron 	cmd_free(h, c);
2078edd16368SStephen M. Cameron 	return rc;
2079edd16368SStephen M. Cameron }
2080edd16368SStephen M. Cameron 
2081316b221aSStephen M. Cameron static int hpsa_bmic_ctrl_mode_sense(struct ctlr_info *h,
2082316b221aSStephen M. Cameron 		unsigned char *scsi3addr, unsigned char page,
2083316b221aSStephen M. Cameron 		struct bmic_controller_parameters *buf, size_t bufsize)
2084316b221aSStephen M. Cameron {
2085316b221aSStephen M. Cameron 	int rc = IO_OK;
2086316b221aSStephen M. Cameron 	struct CommandList *c;
2087316b221aSStephen M. Cameron 	struct ErrorInfo *ei;
2088316b221aSStephen M. Cameron 
2089*45fcb86eSStephen Cameron 	c = cmd_alloc(h);
2090316b221aSStephen M. Cameron 	if (c == NULL) {			/* trouble... */
2091*45fcb86eSStephen Cameron 		dev_warn(&h->pdev->dev, "cmd_alloc returned NULL!\n");
2092316b221aSStephen M. Cameron 		return -ENOMEM;
2093316b221aSStephen M. Cameron 	}
2094316b221aSStephen M. Cameron 
2095316b221aSStephen M. Cameron 	if (fill_cmd(c, BMIC_SENSE_CONTROLLER_PARAMETERS, h, buf, bufsize,
2096316b221aSStephen M. Cameron 			page, scsi3addr, TYPE_CMD)) {
2097316b221aSStephen M. Cameron 		rc = -1;
2098316b221aSStephen M. Cameron 		goto out;
2099316b221aSStephen M. Cameron 	}
2100316b221aSStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2101316b221aSStephen M. Cameron 	ei = c->err_info;
2102316b221aSStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2103316b221aSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2104316b221aSStephen M. Cameron 		rc = -1;
2105316b221aSStephen M. Cameron 	}
2106316b221aSStephen M. Cameron out:
2107*45fcb86eSStephen Cameron 	cmd_free(h, c);
2108316b221aSStephen M. Cameron 	return rc;
2109316b221aSStephen M. Cameron 	}
2110316b221aSStephen M. Cameron 
2111bf711ac6SScott Teel static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
2112bf711ac6SScott Teel 	u8 reset_type)
2113edd16368SStephen M. Cameron {
2114edd16368SStephen M. Cameron 	int rc = IO_OK;
2115edd16368SStephen M. Cameron 	struct CommandList *c;
2116edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2117edd16368SStephen M. Cameron 
2118*45fcb86eSStephen Cameron 	c = cmd_alloc(h);
2119edd16368SStephen M. Cameron 
2120edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
2121*45fcb86eSStephen Cameron 		dev_warn(&h->pdev->dev, "cmd_alloc returned NULL!\n");
2122e9ea04a6SStephen M. Cameron 		return -ENOMEM;
2123edd16368SStephen M. Cameron 	}
2124edd16368SStephen M. Cameron 
2125a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map. */
2126bf711ac6SScott Teel 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
2127bf711ac6SScott Teel 			scsi3addr, TYPE_MSG);
2128bf711ac6SScott Teel 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to LUN reset */
2129edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core(h, c);
2130edd16368SStephen M. Cameron 	/* no unmap needed here because no data xfer. */
2131edd16368SStephen M. Cameron 
2132edd16368SStephen M. Cameron 	ei = c->err_info;
2133edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0) {
2134d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2135edd16368SStephen M. Cameron 		rc = -1;
2136edd16368SStephen M. Cameron 	}
2137*45fcb86eSStephen Cameron 	cmd_free(h, c);
2138edd16368SStephen M. Cameron 	return rc;
2139edd16368SStephen M. Cameron }
2140edd16368SStephen M. Cameron 
2141edd16368SStephen M. Cameron static void hpsa_get_raid_level(struct ctlr_info *h,
2142edd16368SStephen M. Cameron 	unsigned char *scsi3addr, unsigned char *raid_level)
2143edd16368SStephen M. Cameron {
2144edd16368SStephen M. Cameron 	int rc;
2145edd16368SStephen M. Cameron 	unsigned char *buf;
2146edd16368SStephen M. Cameron 
2147edd16368SStephen M. Cameron 	*raid_level = RAID_UNKNOWN;
2148edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2149edd16368SStephen M. Cameron 	if (!buf)
2150edd16368SStephen M. Cameron 		return;
2151b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0xC1, buf, 64);
2152edd16368SStephen M. Cameron 	if (rc == 0)
2153edd16368SStephen M. Cameron 		*raid_level = buf[8];
2154edd16368SStephen M. Cameron 	if (*raid_level > RAID_UNKNOWN)
2155edd16368SStephen M. Cameron 		*raid_level = RAID_UNKNOWN;
2156edd16368SStephen M. Cameron 	kfree(buf);
2157edd16368SStephen M. Cameron 	return;
2158edd16368SStephen M. Cameron }
2159edd16368SStephen M. Cameron 
2160283b4a9bSStephen M. Cameron #define HPSA_MAP_DEBUG
2161283b4a9bSStephen M. Cameron #ifdef HPSA_MAP_DEBUG
2162283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
2163283b4a9bSStephen M. Cameron 				struct raid_map_data *map_buff)
2164283b4a9bSStephen M. Cameron {
2165283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map_buff->data[0];
2166283b4a9bSStephen M. Cameron 	int map, row, col;
2167283b4a9bSStephen M. Cameron 	u16 map_cnt, row_cnt, disks_per_row;
2168283b4a9bSStephen M. Cameron 
2169283b4a9bSStephen M. Cameron 	if (rc != 0)
2170283b4a9bSStephen M. Cameron 		return;
2171283b4a9bSStephen M. Cameron 
21722ba8bfc8SStephen M. Cameron 	/* Show details only if debugging has been activated. */
21732ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug < 2)
21742ba8bfc8SStephen M. Cameron 		return;
21752ba8bfc8SStephen M. Cameron 
2176283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "structure_size = %u\n",
2177283b4a9bSStephen M. Cameron 				le32_to_cpu(map_buff->structure_size));
2178283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
2179283b4a9bSStephen M. Cameron 			le32_to_cpu(map_buff->volume_blk_size));
2180283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
2181283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->volume_blk_cnt));
2182283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
2183283b4a9bSStephen M. Cameron 			map_buff->phys_blk_shift);
2184283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
2185283b4a9bSStephen M. Cameron 			map_buff->parity_rotation_shift);
2186283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "strip_size = %u\n",
2187283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->strip_size));
2188283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
2189283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_starting_blk));
2190283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
2191283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_blk_cnt));
2192283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
2193283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->data_disks_per_row));
2194283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
2195283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->metadata_disks_per_row));
2196283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "row_cnt = %u\n",
2197283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->row_cnt));
2198283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "layout_map_count = %u\n",
2199283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->layout_map_count));
22002b08b3e9SDon Brace 	dev_info(&h->pdev->dev, "flags = 0x%x\n",
2201dd0e19f3SScott Teel 			le16_to_cpu(map_buff->flags));
22022b08b3e9SDon Brace 	dev_info(&h->pdev->dev, "encrypytion = %s\n",
22032b08b3e9SDon Brace 			le16_to_cpu(map_buff->flags) &
22042b08b3e9SDon Brace 			RAID_MAP_FLAG_ENCRYPT_ON ?  "ON" : "OFF");
2205dd0e19f3SScott Teel 	dev_info(&h->pdev->dev, "dekindex = %u\n",
2206dd0e19f3SScott Teel 			le16_to_cpu(map_buff->dekindex));
2207283b4a9bSStephen M. Cameron 	map_cnt = le16_to_cpu(map_buff->layout_map_count);
2208283b4a9bSStephen M. Cameron 	for (map = 0; map < map_cnt; map++) {
2209283b4a9bSStephen M. Cameron 		dev_info(&h->pdev->dev, "Map%u:\n", map);
2210283b4a9bSStephen M. Cameron 		row_cnt = le16_to_cpu(map_buff->row_cnt);
2211283b4a9bSStephen M. Cameron 		for (row = 0; row < row_cnt; row++) {
2212283b4a9bSStephen M. Cameron 			dev_info(&h->pdev->dev, "  Row%u:\n", row);
2213283b4a9bSStephen M. Cameron 			disks_per_row =
2214283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->data_disks_per_row);
2215283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2216283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2217283b4a9bSStephen M. Cameron 					"    D%02u: h=0x%04x xor=%u,%u\n",
2218283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2219283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2220283b4a9bSStephen M. Cameron 			disks_per_row =
2221283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->metadata_disks_per_row);
2222283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2223283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2224283b4a9bSStephen M. Cameron 					"    M%02u: h=0x%04x xor=%u,%u\n",
2225283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2226283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2227283b4a9bSStephen M. Cameron 		}
2228283b4a9bSStephen M. Cameron 	}
2229283b4a9bSStephen M. Cameron }
2230283b4a9bSStephen M. Cameron #else
2231283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
2232283b4a9bSStephen M. Cameron 			__attribute__((unused)) int rc,
2233283b4a9bSStephen M. Cameron 			__attribute__((unused)) struct raid_map_data *map_buff)
2234283b4a9bSStephen M. Cameron {
2235283b4a9bSStephen M. Cameron }
2236283b4a9bSStephen M. Cameron #endif
2237283b4a9bSStephen M. Cameron 
2238283b4a9bSStephen M. Cameron static int hpsa_get_raid_map(struct ctlr_info *h,
2239283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2240283b4a9bSStephen M. Cameron {
2241283b4a9bSStephen M. Cameron 	int rc = 0;
2242283b4a9bSStephen M. Cameron 	struct CommandList *c;
2243283b4a9bSStephen M. Cameron 	struct ErrorInfo *ei;
2244283b4a9bSStephen M. Cameron 
2245*45fcb86eSStephen Cameron 	c = cmd_alloc(h);
2246283b4a9bSStephen M. Cameron 	if (c == NULL) {
2247*45fcb86eSStephen Cameron 		dev_warn(&h->pdev->dev, "cmd_alloc returned NULL!\n");
2248283b4a9bSStephen M. Cameron 		return -ENOMEM;
2249283b4a9bSStephen M. Cameron 	}
2250283b4a9bSStephen M. Cameron 	if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
2251283b4a9bSStephen M. Cameron 			sizeof(this_device->raid_map), 0,
2252283b4a9bSStephen M. Cameron 			scsi3addr, TYPE_CMD)) {
2253283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "Out of memory in hpsa_get_raid_map()\n");
2254*45fcb86eSStephen Cameron 		cmd_free(h, c);
2255283b4a9bSStephen M. Cameron 		return -ENOMEM;
2256283b4a9bSStephen M. Cameron 	}
2257283b4a9bSStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2258283b4a9bSStephen M. Cameron 	ei = c->err_info;
2259283b4a9bSStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2260d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2261*45fcb86eSStephen Cameron 		cmd_free(h, c);
2262283b4a9bSStephen M. Cameron 		return -1;
2263283b4a9bSStephen M. Cameron 	}
2264*45fcb86eSStephen Cameron 	cmd_free(h, c);
2265283b4a9bSStephen M. Cameron 
2266283b4a9bSStephen M. Cameron 	/* @todo in the future, dynamically allocate RAID map memory */
2267283b4a9bSStephen M. Cameron 	if (le32_to_cpu(this_device->raid_map.structure_size) >
2268283b4a9bSStephen M. Cameron 				sizeof(this_device->raid_map)) {
2269283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
2270283b4a9bSStephen M. Cameron 		rc = -1;
2271283b4a9bSStephen M. Cameron 	}
2272283b4a9bSStephen M. Cameron 	hpsa_debug_map_buff(h, rc, &this_device->raid_map);
2273283b4a9bSStephen M. Cameron 	return rc;
2274283b4a9bSStephen M. Cameron }
2275283b4a9bSStephen M. Cameron 
22761b70150aSStephen M. Cameron static int hpsa_vpd_page_supported(struct ctlr_info *h,
22771b70150aSStephen M. Cameron 	unsigned char scsi3addr[], u8 page)
22781b70150aSStephen M. Cameron {
22791b70150aSStephen M. Cameron 	int rc;
22801b70150aSStephen M. Cameron 	int i;
22811b70150aSStephen M. Cameron 	int pages;
22821b70150aSStephen M. Cameron 	unsigned char *buf, bufsize;
22831b70150aSStephen M. Cameron 
22841b70150aSStephen M. Cameron 	buf = kzalloc(256, GFP_KERNEL);
22851b70150aSStephen M. Cameron 	if (!buf)
22861b70150aSStephen M. Cameron 		return 0;
22871b70150aSStephen M. Cameron 
22881b70150aSStephen M. Cameron 	/* Get the size of the page list first */
22891b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
22901b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
22911b70150aSStephen M. Cameron 				buf, HPSA_VPD_HEADER_SZ);
22921b70150aSStephen M. Cameron 	if (rc != 0)
22931b70150aSStephen M. Cameron 		goto exit_unsupported;
22941b70150aSStephen M. Cameron 	pages = buf[3];
22951b70150aSStephen M. Cameron 	if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
22961b70150aSStephen M. Cameron 		bufsize = pages + HPSA_VPD_HEADER_SZ;
22971b70150aSStephen M. Cameron 	else
22981b70150aSStephen M. Cameron 		bufsize = 255;
22991b70150aSStephen M. Cameron 
23001b70150aSStephen M. Cameron 	/* Get the whole VPD page list */
23011b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
23021b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
23031b70150aSStephen M. Cameron 				buf, bufsize);
23041b70150aSStephen M. Cameron 	if (rc != 0)
23051b70150aSStephen M. Cameron 		goto exit_unsupported;
23061b70150aSStephen M. Cameron 
23071b70150aSStephen M. Cameron 	pages = buf[3];
23081b70150aSStephen M. Cameron 	for (i = 1; i <= pages; i++)
23091b70150aSStephen M. Cameron 		if (buf[3 + i] == page)
23101b70150aSStephen M. Cameron 			goto exit_supported;
23111b70150aSStephen M. Cameron exit_unsupported:
23121b70150aSStephen M. Cameron 	kfree(buf);
23131b70150aSStephen M. Cameron 	return 0;
23141b70150aSStephen M. Cameron exit_supported:
23151b70150aSStephen M. Cameron 	kfree(buf);
23161b70150aSStephen M. Cameron 	return 1;
23171b70150aSStephen M. Cameron }
23181b70150aSStephen M. Cameron 
2319283b4a9bSStephen M. Cameron static void hpsa_get_ioaccel_status(struct ctlr_info *h,
2320283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2321283b4a9bSStephen M. Cameron {
2322283b4a9bSStephen M. Cameron 	int rc;
2323283b4a9bSStephen M. Cameron 	unsigned char *buf;
2324283b4a9bSStephen M. Cameron 	u8 ioaccel_status;
2325283b4a9bSStephen M. Cameron 
2326283b4a9bSStephen M. Cameron 	this_device->offload_config = 0;
2327283b4a9bSStephen M. Cameron 	this_device->offload_enabled = 0;
2328283b4a9bSStephen M. Cameron 
2329283b4a9bSStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2330283b4a9bSStephen M. Cameron 	if (!buf)
2331283b4a9bSStephen M. Cameron 		return;
23321b70150aSStephen M. Cameron 	if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
23331b70150aSStephen M. Cameron 		goto out;
2334283b4a9bSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
2335b7bb24ebSStephen M. Cameron 			VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
2336283b4a9bSStephen M. Cameron 	if (rc != 0)
2337283b4a9bSStephen M. Cameron 		goto out;
2338283b4a9bSStephen M. Cameron 
2339283b4a9bSStephen M. Cameron #define IOACCEL_STATUS_BYTE 4
2340283b4a9bSStephen M. Cameron #define OFFLOAD_CONFIGURED_BIT 0x01
2341283b4a9bSStephen M. Cameron #define OFFLOAD_ENABLED_BIT 0x02
2342283b4a9bSStephen M. Cameron 	ioaccel_status = buf[IOACCEL_STATUS_BYTE];
2343283b4a9bSStephen M. Cameron 	this_device->offload_config =
2344283b4a9bSStephen M. Cameron 		!!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
2345283b4a9bSStephen M. Cameron 	if (this_device->offload_config) {
2346283b4a9bSStephen M. Cameron 		this_device->offload_enabled =
2347283b4a9bSStephen M. Cameron 			!!(ioaccel_status & OFFLOAD_ENABLED_BIT);
2348283b4a9bSStephen M. Cameron 		if (hpsa_get_raid_map(h, scsi3addr, this_device))
2349283b4a9bSStephen M. Cameron 			this_device->offload_enabled = 0;
2350283b4a9bSStephen M. Cameron 	}
2351283b4a9bSStephen M. Cameron out:
2352283b4a9bSStephen M. Cameron 	kfree(buf);
2353283b4a9bSStephen M. Cameron 	return;
2354283b4a9bSStephen M. Cameron }
2355283b4a9bSStephen M. Cameron 
2356edd16368SStephen M. Cameron /* Get the device id from inquiry page 0x83 */
2357edd16368SStephen M. Cameron static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
2358edd16368SStephen M. Cameron 	unsigned char *device_id, int buflen)
2359edd16368SStephen M. Cameron {
2360edd16368SStephen M. Cameron 	int rc;
2361edd16368SStephen M. Cameron 	unsigned char *buf;
2362edd16368SStephen M. Cameron 
2363edd16368SStephen M. Cameron 	if (buflen > 16)
2364edd16368SStephen M. Cameron 		buflen = 16;
2365edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2366edd16368SStephen M. Cameron 	if (!buf)
2367a84d794dSStephen M. Cameron 		return -ENOMEM;
2368b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0x83, buf, 64);
2369edd16368SStephen M. Cameron 	if (rc == 0)
2370edd16368SStephen M. Cameron 		memcpy(device_id, &buf[8], buflen);
2371edd16368SStephen M. Cameron 	kfree(buf);
2372edd16368SStephen M. Cameron 	return rc != 0;
2373edd16368SStephen M. Cameron }
2374edd16368SStephen M. Cameron 
2375edd16368SStephen M. Cameron static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
2376edd16368SStephen M. Cameron 		struct ReportLUNdata *buf, int bufsize,
2377edd16368SStephen M. Cameron 		int extended_response)
2378edd16368SStephen M. Cameron {
2379edd16368SStephen M. Cameron 	int rc = IO_OK;
2380edd16368SStephen M. Cameron 	struct CommandList *c;
2381edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
2382edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2383edd16368SStephen M. Cameron 
2384*45fcb86eSStephen Cameron 	c = cmd_alloc(h);
2385edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
2386*45fcb86eSStephen Cameron 		dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
2387edd16368SStephen M. Cameron 		return -1;
2388edd16368SStephen M. Cameron 	}
2389e89c0ae7SStephen M. Cameron 	/* address the controller */
2390e89c0ae7SStephen M. Cameron 	memset(scsi3addr, 0, sizeof(scsi3addr));
2391a2dac136SStephen M. Cameron 	if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
2392a2dac136SStephen M. Cameron 		buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
2393a2dac136SStephen M. Cameron 		rc = -1;
2394a2dac136SStephen M. Cameron 		goto out;
2395a2dac136SStephen M. Cameron 	}
2396edd16368SStephen M. Cameron 	if (extended_response)
2397edd16368SStephen M. Cameron 		c->Request.CDB[1] = extended_response;
2398edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2399edd16368SStephen M. Cameron 	ei = c->err_info;
2400edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 &&
2401edd16368SStephen M. Cameron 	    ei->CommandStatus != CMD_DATA_UNDERRUN) {
2402d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2403edd16368SStephen M. Cameron 		rc = -1;
2404283b4a9bSStephen M. Cameron 	} else {
2405283b4a9bSStephen M. Cameron 		if (buf->extended_response_flag != extended_response) {
2406283b4a9bSStephen M. Cameron 			dev_err(&h->pdev->dev,
2407283b4a9bSStephen M. Cameron 				"report luns requested format %u, got %u\n",
2408283b4a9bSStephen M. Cameron 				extended_response,
2409283b4a9bSStephen M. Cameron 				buf->extended_response_flag);
2410283b4a9bSStephen M. Cameron 			rc = -1;
2411283b4a9bSStephen M. Cameron 		}
2412edd16368SStephen M. Cameron 	}
2413a2dac136SStephen M. Cameron out:
2414*45fcb86eSStephen Cameron 	cmd_free(h, c);
2415edd16368SStephen M. Cameron 	return rc;
2416edd16368SStephen M. Cameron }
2417edd16368SStephen M. Cameron 
2418edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
2419edd16368SStephen M. Cameron 		struct ReportLUNdata *buf,
2420edd16368SStephen M. Cameron 		int bufsize, int extended_response)
2421edd16368SStephen M. Cameron {
2422edd16368SStephen M. Cameron 	return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
2423edd16368SStephen M. Cameron }
2424edd16368SStephen M. Cameron 
2425edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
2426edd16368SStephen M. Cameron 		struct ReportLUNdata *buf, int bufsize)
2427edd16368SStephen M. Cameron {
2428edd16368SStephen M. Cameron 	return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
2429edd16368SStephen M. Cameron }
2430edd16368SStephen M. Cameron 
2431edd16368SStephen M. Cameron static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
2432edd16368SStephen M. Cameron 	int bus, int target, int lun)
2433edd16368SStephen M. Cameron {
2434edd16368SStephen M. Cameron 	device->bus = bus;
2435edd16368SStephen M. Cameron 	device->target = target;
2436edd16368SStephen M. Cameron 	device->lun = lun;
2437edd16368SStephen M. Cameron }
2438edd16368SStephen M. Cameron 
24399846590eSStephen M. Cameron /* Use VPD inquiry to get details of volume status */
24409846590eSStephen M. Cameron static int hpsa_get_volume_status(struct ctlr_info *h,
24419846590eSStephen M. Cameron 					unsigned char scsi3addr[])
24429846590eSStephen M. Cameron {
24439846590eSStephen M. Cameron 	int rc;
24449846590eSStephen M. Cameron 	int status;
24459846590eSStephen M. Cameron 	int size;
24469846590eSStephen M. Cameron 	unsigned char *buf;
24479846590eSStephen M. Cameron 
24489846590eSStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
24499846590eSStephen M. Cameron 	if (!buf)
24509846590eSStephen M. Cameron 		return HPSA_VPD_LV_STATUS_UNSUPPORTED;
24519846590eSStephen M. Cameron 
24529846590eSStephen M. Cameron 	/* Does controller have VPD for logical volume status? */
245324a4b078SStephen M. Cameron 	if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_STATUS))
24549846590eSStephen M. Cameron 		goto exit_failed;
24559846590eSStephen M. Cameron 
24569846590eSStephen M. Cameron 	/* Get the size of the VPD return buffer */
24579846590eSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
24589846590eSStephen M. Cameron 					buf, HPSA_VPD_HEADER_SZ);
245924a4b078SStephen M. Cameron 	if (rc != 0)
24609846590eSStephen M. Cameron 		goto exit_failed;
24619846590eSStephen M. Cameron 	size = buf[3];
24629846590eSStephen M. Cameron 
24639846590eSStephen M. Cameron 	/* Now get the whole VPD buffer */
24649846590eSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
24659846590eSStephen M. Cameron 					buf, size + HPSA_VPD_HEADER_SZ);
246624a4b078SStephen M. Cameron 	if (rc != 0)
24679846590eSStephen M. Cameron 		goto exit_failed;
24689846590eSStephen M. Cameron 	status = buf[4]; /* status byte */
24699846590eSStephen M. Cameron 
24709846590eSStephen M. Cameron 	kfree(buf);
24719846590eSStephen M. Cameron 	return status;
24729846590eSStephen M. Cameron exit_failed:
24739846590eSStephen M. Cameron 	kfree(buf);
24749846590eSStephen M. Cameron 	return HPSA_VPD_LV_STATUS_UNSUPPORTED;
24759846590eSStephen M. Cameron }
24769846590eSStephen M. Cameron 
24779846590eSStephen M. Cameron /* Determine offline status of a volume.
24789846590eSStephen M. Cameron  * Return either:
24799846590eSStephen M. Cameron  *  0 (not offline)
248067955ba3SStephen M. Cameron  *  0xff (offline for unknown reasons)
24819846590eSStephen M. Cameron  *  # (integer code indicating one of several NOT READY states
24829846590eSStephen M. Cameron  *     describing why a volume is to be kept offline)
24839846590eSStephen M. Cameron  */
248467955ba3SStephen M. Cameron static int hpsa_volume_offline(struct ctlr_info *h,
24859846590eSStephen M. Cameron 					unsigned char scsi3addr[])
24869846590eSStephen M. Cameron {
24879846590eSStephen M. Cameron 	struct CommandList *c;
24889846590eSStephen M. Cameron 	unsigned char *sense, sense_key, asc, ascq;
24899846590eSStephen M. Cameron 	int ldstat = 0;
24909846590eSStephen M. Cameron 	u16 cmd_status;
24919846590eSStephen M. Cameron 	u8 scsi_status;
24929846590eSStephen M. Cameron #define ASC_LUN_NOT_READY 0x04
24939846590eSStephen M. Cameron #define ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS 0x04
24949846590eSStephen M. Cameron #define ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ 0x02
24959846590eSStephen M. Cameron 
24969846590eSStephen M. Cameron 	c = cmd_alloc(h);
24979846590eSStephen M. Cameron 	if (!c)
24989846590eSStephen M. Cameron 		return 0;
24999846590eSStephen M. Cameron 	(void) fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, scsi3addr, TYPE_CMD);
25009846590eSStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core(h, c);
25019846590eSStephen M. Cameron 	sense = c->err_info->SenseInfo;
25029846590eSStephen M. Cameron 	sense_key = sense[2];
25039846590eSStephen M. Cameron 	asc = sense[12];
25049846590eSStephen M. Cameron 	ascq = sense[13];
25059846590eSStephen M. Cameron 	cmd_status = c->err_info->CommandStatus;
25069846590eSStephen M. Cameron 	scsi_status = c->err_info->ScsiStatus;
25079846590eSStephen M. Cameron 	cmd_free(h, c);
25089846590eSStephen M. Cameron 	/* Is the volume 'not ready'? */
25099846590eSStephen M. Cameron 	if (cmd_status != CMD_TARGET_STATUS ||
25109846590eSStephen M. Cameron 		scsi_status != SAM_STAT_CHECK_CONDITION ||
25119846590eSStephen M. Cameron 		sense_key != NOT_READY ||
25129846590eSStephen M. Cameron 		asc != ASC_LUN_NOT_READY)  {
25139846590eSStephen M. Cameron 		return 0;
25149846590eSStephen M. Cameron 	}
25159846590eSStephen M. Cameron 
25169846590eSStephen M. Cameron 	/* Determine the reason for not ready state */
25179846590eSStephen M. Cameron 	ldstat = hpsa_get_volume_status(h, scsi3addr);
25189846590eSStephen M. Cameron 
25199846590eSStephen M. Cameron 	/* Keep volume offline in certain cases: */
25209846590eSStephen M. Cameron 	switch (ldstat) {
25219846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ERASE:
25229846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_RPI:
25239846590eSStephen M. Cameron 	case HPSA_LV_PENDING_RPI:
25249846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_NO_KEY:
25259846590eSStephen M. Cameron 	case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
25269846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION:
25279846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
25289846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
25299846590eSStephen M. Cameron 		return ldstat;
25309846590eSStephen M. Cameron 	case HPSA_VPD_LV_STATUS_UNSUPPORTED:
25319846590eSStephen M. Cameron 		/* If VPD status page isn't available,
25329846590eSStephen M. Cameron 		 * use ASC/ASCQ to determine state
25339846590eSStephen M. Cameron 		 */
25349846590eSStephen M. Cameron 		if ((ascq == ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS) ||
25359846590eSStephen M. Cameron 			(ascq == ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ))
25369846590eSStephen M. Cameron 			return ldstat;
25379846590eSStephen M. Cameron 		break;
25389846590eSStephen M. Cameron 	default:
25399846590eSStephen M. Cameron 		break;
25409846590eSStephen M. Cameron 	}
25419846590eSStephen M. Cameron 	return 0;
25429846590eSStephen M. Cameron }
25439846590eSStephen M. Cameron 
2544edd16368SStephen M. Cameron static int hpsa_update_device_info(struct ctlr_info *h,
25450b0e1d6cSStephen M. Cameron 	unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
25460b0e1d6cSStephen M. Cameron 	unsigned char *is_OBDR_device)
2547edd16368SStephen M. Cameron {
25480b0e1d6cSStephen M. Cameron 
25490b0e1d6cSStephen M. Cameron #define OBDR_SIG_OFFSET 43
25500b0e1d6cSStephen M. Cameron #define OBDR_TAPE_SIG "$DR-10"
25510b0e1d6cSStephen M. Cameron #define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
25520b0e1d6cSStephen M. Cameron #define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
25530b0e1d6cSStephen M. Cameron 
2554ea6d3bc3SStephen M. Cameron 	unsigned char *inq_buff;
25550b0e1d6cSStephen M. Cameron 	unsigned char *obdr_sig;
2556edd16368SStephen M. Cameron 
2557ea6d3bc3SStephen M. Cameron 	inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
2558edd16368SStephen M. Cameron 	if (!inq_buff)
2559edd16368SStephen M. Cameron 		goto bail_out;
2560edd16368SStephen M. Cameron 
2561edd16368SStephen M. Cameron 	/* Do an inquiry to the device to see what it is. */
2562edd16368SStephen M. Cameron 	if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
2563edd16368SStephen M. Cameron 		(unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
2564edd16368SStephen M. Cameron 		/* Inquiry failed (msg printed already) */
2565edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev,
2566edd16368SStephen M. Cameron 			"hpsa_update_device_info: inquiry failed\n");
2567edd16368SStephen M. Cameron 		goto bail_out;
2568edd16368SStephen M. Cameron 	}
2569edd16368SStephen M. Cameron 
2570edd16368SStephen M. Cameron 	this_device->devtype = (inq_buff[0] & 0x1f);
2571edd16368SStephen M. Cameron 	memcpy(this_device->scsi3addr, scsi3addr, 8);
2572edd16368SStephen M. Cameron 	memcpy(this_device->vendor, &inq_buff[8],
2573edd16368SStephen M. Cameron 		sizeof(this_device->vendor));
2574edd16368SStephen M. Cameron 	memcpy(this_device->model, &inq_buff[16],
2575edd16368SStephen M. Cameron 		sizeof(this_device->model));
2576edd16368SStephen M. Cameron 	memset(this_device->device_id, 0,
2577edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
2578edd16368SStephen M. Cameron 	hpsa_get_device_id(h, scsi3addr, this_device->device_id,
2579edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
2580edd16368SStephen M. Cameron 
2581edd16368SStephen M. Cameron 	if (this_device->devtype == TYPE_DISK &&
2582283b4a9bSStephen M. Cameron 		is_logical_dev_addr_mode(scsi3addr)) {
258367955ba3SStephen M. Cameron 		int volume_offline;
258467955ba3SStephen M. Cameron 
2585edd16368SStephen M. Cameron 		hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
2586283b4a9bSStephen M. Cameron 		if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
2587283b4a9bSStephen M. Cameron 			hpsa_get_ioaccel_status(h, scsi3addr, this_device);
258867955ba3SStephen M. Cameron 		volume_offline = hpsa_volume_offline(h, scsi3addr);
258967955ba3SStephen M. Cameron 		if (volume_offline < 0 || volume_offline > 0xff)
259067955ba3SStephen M. Cameron 			volume_offline = HPSA_VPD_LV_STATUS_UNSUPPORTED;
259167955ba3SStephen M. Cameron 		this_device->volume_offline = volume_offline & 0xff;
2592283b4a9bSStephen M. Cameron 	} else {
2593edd16368SStephen M. Cameron 		this_device->raid_level = RAID_UNKNOWN;
2594283b4a9bSStephen M. Cameron 		this_device->offload_config = 0;
2595283b4a9bSStephen M. Cameron 		this_device->offload_enabled = 0;
25969846590eSStephen M. Cameron 		this_device->volume_offline = 0;
2597283b4a9bSStephen M. Cameron 	}
2598edd16368SStephen M. Cameron 
25990b0e1d6cSStephen M. Cameron 	if (is_OBDR_device) {
26000b0e1d6cSStephen M. Cameron 		/* See if this is a One-Button-Disaster-Recovery device
26010b0e1d6cSStephen M. Cameron 		 * by looking for "$DR-10" at offset 43 in inquiry data.
26020b0e1d6cSStephen M. Cameron 		 */
26030b0e1d6cSStephen M. Cameron 		obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
26040b0e1d6cSStephen M. Cameron 		*is_OBDR_device = (this_device->devtype == TYPE_ROM &&
26050b0e1d6cSStephen M. Cameron 					strncmp(obdr_sig, OBDR_TAPE_SIG,
26060b0e1d6cSStephen M. Cameron 						OBDR_SIG_LEN) == 0);
26070b0e1d6cSStephen M. Cameron 	}
26080b0e1d6cSStephen M. Cameron 
2609edd16368SStephen M. Cameron 	kfree(inq_buff);
2610edd16368SStephen M. Cameron 	return 0;
2611edd16368SStephen M. Cameron 
2612edd16368SStephen M. Cameron bail_out:
2613edd16368SStephen M. Cameron 	kfree(inq_buff);
2614edd16368SStephen M. Cameron 	return 1;
2615edd16368SStephen M. Cameron }
2616edd16368SStephen M. Cameron 
26174f4eb9f1SScott Teel static unsigned char *ext_target_model[] = {
2618edd16368SStephen M. Cameron 	"MSA2012",
2619edd16368SStephen M. Cameron 	"MSA2024",
2620edd16368SStephen M. Cameron 	"MSA2312",
2621edd16368SStephen M. Cameron 	"MSA2324",
2622fda38518SStephen M. Cameron 	"P2000 G3 SAS",
2623e06c8e5cSStephen M. Cameron 	"MSA 2040 SAS",
2624edd16368SStephen M. Cameron 	NULL,
2625edd16368SStephen M. Cameron };
2626edd16368SStephen M. Cameron 
26274f4eb9f1SScott Teel static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
2628edd16368SStephen M. Cameron {
2629edd16368SStephen M. Cameron 	int i;
2630edd16368SStephen M. Cameron 
26314f4eb9f1SScott Teel 	for (i = 0; ext_target_model[i]; i++)
26324f4eb9f1SScott Teel 		if (strncmp(device->model, ext_target_model[i],
26334f4eb9f1SScott Teel 			strlen(ext_target_model[i])) == 0)
2634edd16368SStephen M. Cameron 			return 1;
2635edd16368SStephen M. Cameron 	return 0;
2636edd16368SStephen M. Cameron }
2637edd16368SStephen M. Cameron 
2638edd16368SStephen M. Cameron /* Helper function to assign bus, target, lun mapping of devices.
26394f4eb9f1SScott Teel  * Puts non-external target logical volumes on bus 0, external target logical
2640edd16368SStephen M. Cameron  * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
2641edd16368SStephen M. Cameron  * Logical drive target and lun are assigned at this time, but
2642edd16368SStephen M. Cameron  * physical device lun and target assignment are deferred (assigned
2643edd16368SStephen M. Cameron  * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
2644edd16368SStephen M. Cameron  */
2645edd16368SStephen M. Cameron static void figure_bus_target_lun(struct ctlr_info *h,
26461f310bdeSStephen M. Cameron 	u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
2647edd16368SStephen M. Cameron {
26481f310bdeSStephen M. Cameron 	u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
2649edd16368SStephen M. Cameron 
26501f310bdeSStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes)) {
26511f310bdeSStephen M. Cameron 		/* physical device, target and lun filled in later */
26521f310bdeSStephen M. Cameron 		if (is_hba_lunid(lunaddrbytes))
26531f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
26541f310bdeSStephen M. Cameron 		else
26551f310bdeSStephen M. Cameron 			/* defer target, lun assignment for physical devices */
26561f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 2, -1, -1);
26571f310bdeSStephen M. Cameron 		return;
26581f310bdeSStephen M. Cameron 	}
26591f310bdeSStephen M. Cameron 	/* It's a logical device */
26604f4eb9f1SScott Teel 	if (is_ext_target(h, device)) {
26614f4eb9f1SScott Teel 		/* external target way, put logicals on bus 1
2662339b2b14SStephen M. Cameron 		 * and match target/lun numbers box
26631f310bdeSStephen M. Cameron 		 * reports, other smart array, bus 0, target 0, match lunid
2664339b2b14SStephen M. Cameron 		 */
26651f310bdeSStephen M. Cameron 		hpsa_set_bus_target_lun(device,
26661f310bdeSStephen M. Cameron 			1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
26671f310bdeSStephen M. Cameron 		return;
2668339b2b14SStephen M. Cameron 	}
26691f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
2670edd16368SStephen M. Cameron }
2671edd16368SStephen M. Cameron 
2672edd16368SStephen M. Cameron /*
2673edd16368SStephen M. Cameron  * If there is no lun 0 on a target, linux won't find any devices.
26744f4eb9f1SScott Teel  * For the external targets (arrays), we have to manually detect the enclosure
2675edd16368SStephen M. Cameron  * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
2676edd16368SStephen M. Cameron  * it for some reason.  *tmpdevice is the target we're adding,
2677edd16368SStephen M. Cameron  * this_device is a pointer into the current element of currentsd[]
2678edd16368SStephen M. Cameron  * that we're building up in update_scsi_devices(), below.
2679edd16368SStephen M. Cameron  * lunzerobits is a bitmap that tracks which targets already have a
2680edd16368SStephen M. Cameron  * lun 0 assigned.
2681edd16368SStephen M. Cameron  * Returns 1 if an enclosure was added, 0 if not.
2682edd16368SStephen M. Cameron  */
26834f4eb9f1SScott Teel static int add_ext_target_dev(struct ctlr_info *h,
2684edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *tmpdevice,
268501a02ffcSStephen M. Cameron 	struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
26864f4eb9f1SScott Teel 	unsigned long lunzerobits[], int *n_ext_target_devs)
2687edd16368SStephen M. Cameron {
2688edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
2689edd16368SStephen M. Cameron 
26901f310bdeSStephen M. Cameron 	if (test_bit(tmpdevice->target, lunzerobits))
2691edd16368SStephen M. Cameron 		return 0; /* There is already a lun 0 on this target. */
2692edd16368SStephen M. Cameron 
2693edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes))
2694edd16368SStephen M. Cameron 		return 0; /* It's the logical targets that may lack lun 0. */
2695edd16368SStephen M. Cameron 
26964f4eb9f1SScott Teel 	if (!is_ext_target(h, tmpdevice))
26974f4eb9f1SScott Teel 		return 0; /* Only external target devices have this problem. */
2698edd16368SStephen M. Cameron 
26991f310bdeSStephen M. Cameron 	if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
2700edd16368SStephen M. Cameron 		return 0;
2701edd16368SStephen M. Cameron 
2702c4f8a299SStephen M. Cameron 	memset(scsi3addr, 0, 8);
27031f310bdeSStephen M. Cameron 	scsi3addr[3] = tmpdevice->target;
2704edd16368SStephen M. Cameron 	if (is_hba_lunid(scsi3addr))
2705edd16368SStephen M. Cameron 		return 0; /* Don't add the RAID controller here. */
2706edd16368SStephen M. Cameron 
2707339b2b14SStephen M. Cameron 	if (is_scsi_rev_5(h))
2708339b2b14SStephen M. Cameron 		return 0; /* p1210m doesn't need to do this. */
2709339b2b14SStephen M. Cameron 
27104f4eb9f1SScott Teel 	if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
2711aca4a520SScott Teel 		dev_warn(&h->pdev->dev, "Maximum number of external "
2712aca4a520SScott Teel 			"target devices exceeded.  Check your hardware "
2713edd16368SStephen M. Cameron 			"configuration.");
2714edd16368SStephen M. Cameron 		return 0;
2715edd16368SStephen M. Cameron 	}
2716edd16368SStephen M. Cameron 
27170b0e1d6cSStephen M. Cameron 	if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
2718edd16368SStephen M. Cameron 		return 0;
27194f4eb9f1SScott Teel 	(*n_ext_target_devs)++;
27201f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(this_device,
27211f310bdeSStephen M. Cameron 				tmpdevice->bus, tmpdevice->target, 0);
27221f310bdeSStephen M. Cameron 	set_bit(tmpdevice->target, lunzerobits);
2723edd16368SStephen M. Cameron 	return 1;
2724edd16368SStephen M. Cameron }
2725edd16368SStephen M. Cameron 
2726edd16368SStephen M. Cameron /*
272754b6e9e9SScott Teel  * Get address of physical disk used for an ioaccel2 mode command:
272854b6e9e9SScott Teel  *	1. Extract ioaccel2 handle from the command.
272954b6e9e9SScott Teel  *	2. Find a matching ioaccel2 handle from list of physical disks.
273054b6e9e9SScott Teel  *	3. Return:
273154b6e9e9SScott Teel  *		1 and set scsi3addr to address of matching physical
273254b6e9e9SScott Teel  *		0 if no matching physical disk was found.
273354b6e9e9SScott Teel  */
273454b6e9e9SScott Teel static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
273554b6e9e9SScott Teel 	struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
273654b6e9e9SScott Teel {
273754b6e9e9SScott Teel 	struct ReportExtendedLUNdata *physicals = NULL;
273854b6e9e9SScott Teel 	int responsesize = 24;	/* size of physical extended response */
273954b6e9e9SScott Teel 	int extended = 2;	/* flag forces reporting 'other dev info'. */
274054b6e9e9SScott Teel 	int reportsize = sizeof(*physicals) + HPSA_MAX_PHYS_LUN * responsesize;
274154b6e9e9SScott Teel 	u32 nphysicals = 0;	/* number of reported physical devs */
274254b6e9e9SScott Teel 	int found = 0;		/* found match (1) or not (0) */
274354b6e9e9SScott Teel 	u32 find;		/* handle we need to match */
274454b6e9e9SScott Teel 	int i;
274554b6e9e9SScott Teel 	struct scsi_cmnd *scmd;	/* scsi command within request being aborted */
274654b6e9e9SScott Teel 	struct hpsa_scsi_dev_t *d; /* device of request being aborted */
274754b6e9e9SScott Teel 	struct io_accel2_cmd *c2a; /* ioaccel2 command to abort */
27482b08b3e9SDon Brace 	__le32 it_nexus;	/* 4 byte device handle for the ioaccel2 cmd */
27492b08b3e9SDon Brace 	__le32 scsi_nexus;	/* 4 byte device handle for the ioaccel2 cmd */
275054b6e9e9SScott Teel 
275154b6e9e9SScott Teel 	if (ioaccel2_cmd_to_abort->cmd_type != CMD_IOACCEL2)
275254b6e9e9SScott Teel 		return 0; /* no match */
275354b6e9e9SScott Teel 
275454b6e9e9SScott Teel 	/* point to the ioaccel2 device handle */
275554b6e9e9SScott Teel 	c2a = &h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
275654b6e9e9SScott Teel 	if (c2a == NULL)
275754b6e9e9SScott Teel 		return 0; /* no match */
275854b6e9e9SScott Teel 
275954b6e9e9SScott Teel 	scmd = (struct scsi_cmnd *) ioaccel2_cmd_to_abort->scsi_cmd;
276054b6e9e9SScott Teel 	if (scmd == NULL)
276154b6e9e9SScott Teel 		return 0; /* no match */
276254b6e9e9SScott Teel 
276354b6e9e9SScott Teel 	d = scmd->device->hostdata;
276454b6e9e9SScott Teel 	if (d == NULL)
276554b6e9e9SScott Teel 		return 0; /* no match */
276654b6e9e9SScott Teel 
276750a0decfSStephen M. Cameron 	it_nexus = cpu_to_le32(d->ioaccel_handle);
27682b08b3e9SDon Brace 	scsi_nexus = c2a->scsi_nexus;
27692b08b3e9SDon Brace 	find = le32_to_cpu(c2a->scsi_nexus);
277054b6e9e9SScott Teel 
27712ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
27722ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
27732ba8bfc8SStephen 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",
27742ba8bfc8SStephen M. Cameron 			__func__, scsi_nexus,
27752ba8bfc8SStephen M. Cameron 			d->device_id[0], d->device_id[1], d->device_id[2],
27762ba8bfc8SStephen M. Cameron 			d->device_id[3], d->device_id[4], d->device_id[5],
27772ba8bfc8SStephen M. Cameron 			d->device_id[6], d->device_id[7], d->device_id[8],
27782ba8bfc8SStephen M. Cameron 			d->device_id[9], d->device_id[10], d->device_id[11],
27792ba8bfc8SStephen M. Cameron 			d->device_id[12], d->device_id[13], d->device_id[14],
27802ba8bfc8SStephen M. Cameron 			d->device_id[15]);
27812ba8bfc8SStephen M. Cameron 
278254b6e9e9SScott Teel 	/* Get the list of physical devices */
278354b6e9e9SScott Teel 	physicals = kzalloc(reportsize, GFP_KERNEL);
27843b51a7a3SJoe Handzik 	if (physicals == NULL)
27853b51a7a3SJoe Handzik 		return 0;
278654b6e9e9SScott Teel 	if (hpsa_scsi_do_report_phys_luns(h, (struct ReportLUNdata *) physicals,
278754b6e9e9SScott Teel 		reportsize, extended)) {
278854b6e9e9SScott Teel 		dev_err(&h->pdev->dev,
278954b6e9e9SScott Teel 			"Can't lookup %s device handle: report physical LUNs failed.\n",
279054b6e9e9SScott Teel 			"HP SSD Smart Path");
279154b6e9e9SScott Teel 		kfree(physicals);
279254b6e9e9SScott Teel 		return 0;
279354b6e9e9SScott Teel 	}
279454b6e9e9SScott Teel 	nphysicals = be32_to_cpu(*((__be32 *)physicals->LUNListLength)) /
279554b6e9e9SScott Teel 							responsesize;
279654b6e9e9SScott Teel 
279754b6e9e9SScott Teel 	/* find ioaccel2 handle in list of physicals: */
279854b6e9e9SScott Teel 	for (i = 0; i < nphysicals; i++) {
2799d5b5d964SStephen M. Cameron 		struct ext_report_lun_entry *entry = &physicals->LUN[i];
2800d5b5d964SStephen M. Cameron 
280154b6e9e9SScott Teel 		/* handle is in bytes 28-31 of each lun */
2802d5b5d964SStephen M. Cameron 		if (entry->ioaccel_handle != find)
280354b6e9e9SScott Teel 			continue; /* didn't match */
280454b6e9e9SScott Teel 		found = 1;
2805d5b5d964SStephen M. Cameron 		memcpy(scsi3addr, entry->lunid, 8);
28062ba8bfc8SStephen M. Cameron 		if (h->raid_offload_debug > 0)
28072ba8bfc8SStephen M. Cameron 			dev_info(&h->pdev->dev,
2808d5b5d964SStephen M. Cameron 				"%s: Searched h=0x%08x, Found h=0x%08x, scsiaddr 0x%8phN\n",
28092ba8bfc8SStephen M. Cameron 				__func__, find,
2810d5b5d964SStephen M. Cameron 				entry->ioaccel_handle, scsi3addr);
281154b6e9e9SScott Teel 		break; /* found it */
281254b6e9e9SScott Teel 	}
281354b6e9e9SScott Teel 
281454b6e9e9SScott Teel 	kfree(physicals);
281554b6e9e9SScott Teel 	if (found)
281654b6e9e9SScott Teel 		return 1;
281754b6e9e9SScott Teel 	else
281854b6e9e9SScott Teel 		return 0;
281954b6e9e9SScott Teel 
282054b6e9e9SScott Teel }
282154b6e9e9SScott Teel /*
2822edd16368SStephen M. Cameron  * Do CISS_REPORT_PHYS and CISS_REPORT_LOG.  Data is returned in physdev,
2823edd16368SStephen M. Cameron  * logdev.  The number of luns in physdev and logdev are returned in
2824edd16368SStephen M. Cameron  * *nphysicals and *nlogicals, respectively.
2825edd16368SStephen M. Cameron  * Returns 0 on success, -1 otherwise.
2826edd16368SStephen M. Cameron  */
2827edd16368SStephen M. Cameron static int hpsa_gather_lun_info(struct ctlr_info *h,
282892084715SStephen M. Cameron 	int reportphyslunsize, int reportloglunsize,
2829283b4a9bSStephen M. Cameron 	struct ReportLUNdata *physdev, u32 *nphysicals, int *physical_mode,
283001a02ffcSStephen M. Cameron 	struct ReportLUNdata *logdev, u32 *nlogicals)
2831edd16368SStephen M. Cameron {
2832283b4a9bSStephen M. Cameron 	int physical_entry_size = 8;
2833283b4a9bSStephen M. Cameron 
2834283b4a9bSStephen M. Cameron 	*physical_mode = 0;
2835283b4a9bSStephen M. Cameron 
2836283b4a9bSStephen M. Cameron 	/* For I/O accelerator mode we need to read physical device handles */
2837317d4adfSMike MIller 	if (h->transMethod & CFGTBL_Trans_io_accel1 ||
2838317d4adfSMike MIller 		h->transMethod & CFGTBL_Trans_io_accel2) {
2839283b4a9bSStephen M. Cameron 		*physical_mode = HPSA_REPORT_PHYS_EXTENDED;
2840283b4a9bSStephen M. Cameron 		physical_entry_size = 24;
2841283b4a9bSStephen M. Cameron 	}
284292084715SStephen M. Cameron 	if (hpsa_scsi_do_report_phys_luns(h, physdev, reportphyslunsize,
2843283b4a9bSStephen M. Cameron 							*physical_mode)) {
2844edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
2845edd16368SStephen M. Cameron 		return -1;
2846edd16368SStephen M. Cameron 	}
2847283b4a9bSStephen M. Cameron 	*nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) /
2848283b4a9bSStephen M. Cameron 							physical_entry_size;
2849edd16368SStephen M. Cameron 	if (*nphysicals > HPSA_MAX_PHYS_LUN) {
2850edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
2851edd16368SStephen M. Cameron 			"  %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2852edd16368SStephen M. Cameron 			*nphysicals - HPSA_MAX_PHYS_LUN);
2853edd16368SStephen M. Cameron 		*nphysicals = HPSA_MAX_PHYS_LUN;
2854edd16368SStephen M. Cameron 	}
285592084715SStephen M. Cameron 	if (hpsa_scsi_do_report_log_luns(h, logdev, reportloglunsize)) {
2856edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
2857edd16368SStephen M. Cameron 		return -1;
2858edd16368SStephen M. Cameron 	}
28596df1e954SStephen M. Cameron 	*nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
2860edd16368SStephen M. Cameron 	/* Reject Logicals in excess of our max capability. */
2861edd16368SStephen M. Cameron 	if (*nlogicals > HPSA_MAX_LUN) {
2862edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
2863edd16368SStephen M. Cameron 			"maximum logical LUNs (%d) exceeded.  "
2864edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_LUN,
2865edd16368SStephen M. Cameron 			*nlogicals - HPSA_MAX_LUN);
2866edd16368SStephen M. Cameron 			*nlogicals = HPSA_MAX_LUN;
2867edd16368SStephen M. Cameron 	}
2868edd16368SStephen M. Cameron 	if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
2869edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
2870edd16368SStephen M. Cameron 			"maximum logical + physical LUNs (%d) exceeded. "
2871edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2872edd16368SStephen M. Cameron 			*nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
2873edd16368SStephen M. Cameron 		*nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
2874edd16368SStephen M. Cameron 	}
2875edd16368SStephen M. Cameron 	return 0;
2876edd16368SStephen M. Cameron }
2877edd16368SStephen M. Cameron 
287842a91641SDon Brace static u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position,
287942a91641SDon Brace 	int i, int nphysicals, int nlogicals,
2880a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list,
2881339b2b14SStephen M. Cameron 	struct ReportLUNdata *logdev_list)
2882339b2b14SStephen M. Cameron {
2883339b2b14SStephen M. Cameron 	/* Helper function, figure out where the LUN ID info is coming from
2884339b2b14SStephen M. Cameron 	 * given index i, lists of physical and logical devices, where in
2885339b2b14SStephen M. Cameron 	 * the list the raid controller is supposed to appear (first or last)
2886339b2b14SStephen M. Cameron 	 */
2887339b2b14SStephen M. Cameron 
2888339b2b14SStephen M. Cameron 	int logicals_start = nphysicals + (raid_ctlr_position == 0);
2889339b2b14SStephen M. Cameron 	int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
2890339b2b14SStephen M. Cameron 
2891339b2b14SStephen M. Cameron 	if (i == raid_ctlr_position)
2892339b2b14SStephen M. Cameron 		return RAID_CTLR_LUNID;
2893339b2b14SStephen M. Cameron 
2894339b2b14SStephen M. Cameron 	if (i < logicals_start)
2895d5b5d964SStephen M. Cameron 		return &physdev_list->LUN[i -
2896d5b5d964SStephen M. Cameron 				(raid_ctlr_position == 0)].lunid[0];
2897339b2b14SStephen M. Cameron 
2898339b2b14SStephen M. Cameron 	if (i < last_device)
2899339b2b14SStephen M. Cameron 		return &logdev_list->LUN[i - nphysicals -
2900339b2b14SStephen M. Cameron 			(raid_ctlr_position == 0)][0];
2901339b2b14SStephen M. Cameron 	BUG();
2902339b2b14SStephen M. Cameron 	return NULL;
2903339b2b14SStephen M. Cameron }
2904339b2b14SStephen M. Cameron 
2905316b221aSStephen M. Cameron static int hpsa_hba_mode_enabled(struct ctlr_info *h)
2906316b221aSStephen M. Cameron {
2907316b221aSStephen M. Cameron 	int rc;
29086e8e8088SJoe Handzik 	int hba_mode_enabled;
2909316b221aSStephen M. Cameron 	struct bmic_controller_parameters *ctlr_params;
2910316b221aSStephen M. Cameron 	ctlr_params = kzalloc(sizeof(struct bmic_controller_parameters),
2911316b221aSStephen M. Cameron 		GFP_KERNEL);
2912316b221aSStephen M. Cameron 
2913316b221aSStephen M. Cameron 	if (!ctlr_params)
291496444fbbSJoe Handzik 		return -ENOMEM;
2915316b221aSStephen M. Cameron 	rc = hpsa_bmic_ctrl_mode_sense(h, RAID_CTLR_LUNID, 0, ctlr_params,
2916316b221aSStephen M. Cameron 		sizeof(struct bmic_controller_parameters));
291796444fbbSJoe Handzik 	if (rc) {
2918316b221aSStephen M. Cameron 		kfree(ctlr_params);
291996444fbbSJoe Handzik 		return rc;
2920316b221aSStephen M. Cameron 	}
29216e8e8088SJoe Handzik 
29226e8e8088SJoe Handzik 	hba_mode_enabled =
29236e8e8088SJoe Handzik 		((ctlr_params->nvram_flags & HBA_MODE_ENABLED_FLAG) != 0);
29246e8e8088SJoe Handzik 	kfree(ctlr_params);
29256e8e8088SJoe Handzik 	return hba_mode_enabled;
2926316b221aSStephen M. Cameron }
2927316b221aSStephen M. Cameron 
2928edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
2929edd16368SStephen M. Cameron {
2930edd16368SStephen M. Cameron 	/* the idea here is we could get notified
2931edd16368SStephen M. Cameron 	 * that some devices have changed, so we do a report
2932edd16368SStephen M. Cameron 	 * physical luns and report logical luns cmd, and adjust
2933edd16368SStephen M. Cameron 	 * our list of devices accordingly.
2934edd16368SStephen M. Cameron 	 *
2935edd16368SStephen M. Cameron 	 * The scsi3addr's of devices won't change so long as the
2936edd16368SStephen M. Cameron 	 * adapter is not reset.  That means we can rescan and
2937edd16368SStephen M. Cameron 	 * tell which devices we already know about, vs. new
2938edd16368SStephen M. Cameron 	 * devices, vs.  disappearing devices.
2939edd16368SStephen M. Cameron 	 */
2940a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list = NULL;
2941edd16368SStephen M. Cameron 	struct ReportLUNdata *logdev_list = NULL;
294201a02ffcSStephen M. Cameron 	u32 nphysicals = 0;
294301a02ffcSStephen M. Cameron 	u32 nlogicals = 0;
2944283b4a9bSStephen M. Cameron 	int physical_mode = 0;
294501a02ffcSStephen M. Cameron 	u32 ndev_allocated = 0;
2946edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
2947edd16368SStephen M. Cameron 	int ncurrent = 0;
29484f4eb9f1SScott Teel 	int i, n_ext_target_devs, ndevs_to_allocate;
2949339b2b14SStephen M. Cameron 	int raid_ctlr_position;
29502bbf5c7fSJoe Handzik 	int rescan_hba_mode;
2951aca4a520SScott Teel 	DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
2952edd16368SStephen M. Cameron 
2953cfe5badcSScott Teel 	currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
295492084715SStephen M. Cameron 	physdev_list = kzalloc(sizeof(*physdev_list), GFP_KERNEL);
295592084715SStephen M. Cameron 	logdev_list = kzalloc(sizeof(*logdev_list), GFP_KERNEL);
2956edd16368SStephen M. Cameron 	tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
2957edd16368SStephen M. Cameron 
29580b0e1d6cSStephen M. Cameron 	if (!currentsd || !physdev_list || !logdev_list || !tmpdevice) {
2959edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory\n");
2960edd16368SStephen M. Cameron 		goto out;
2961edd16368SStephen M. Cameron 	}
2962edd16368SStephen M. Cameron 	memset(lunzerobits, 0, sizeof(lunzerobits));
2963edd16368SStephen M. Cameron 
2964316b221aSStephen M. Cameron 	rescan_hba_mode = hpsa_hba_mode_enabled(h);
296596444fbbSJoe Handzik 	if (rescan_hba_mode < 0)
296696444fbbSJoe Handzik 		goto out;
2967316b221aSStephen M. Cameron 
2968316b221aSStephen M. Cameron 	if (!h->hba_mode_enabled && rescan_hba_mode)
2969316b221aSStephen M. Cameron 		dev_warn(&h->pdev->dev, "HBA mode enabled\n");
2970316b221aSStephen M. Cameron 	else if (h->hba_mode_enabled && !rescan_hba_mode)
2971316b221aSStephen M. Cameron 		dev_warn(&h->pdev->dev, "HBA mode disabled\n");
2972316b221aSStephen M. Cameron 
2973316b221aSStephen M. Cameron 	h->hba_mode_enabled = rescan_hba_mode;
2974316b221aSStephen M. Cameron 
297592084715SStephen M. Cameron 	if (hpsa_gather_lun_info(h,
297692084715SStephen M. Cameron 			sizeof(*physdev_list), sizeof(*logdev_list),
2977a93aa1feSMatt Gates 			(struct ReportLUNdata *) physdev_list, &nphysicals,
2978283b4a9bSStephen M. Cameron 			&physical_mode, logdev_list, &nlogicals))
2979edd16368SStephen M. Cameron 		goto out;
2980edd16368SStephen M. Cameron 
2981aca4a520SScott Teel 	/* We might see up to the maximum number of logical and physical disks
2982aca4a520SScott Teel 	 * plus external target devices, and a device for the local RAID
2983aca4a520SScott Teel 	 * controller.
2984edd16368SStephen M. Cameron 	 */
2985aca4a520SScott Teel 	ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
2986edd16368SStephen M. Cameron 
2987edd16368SStephen M. Cameron 	/* Allocate the per device structures */
2988edd16368SStephen M. Cameron 	for (i = 0; i < ndevs_to_allocate; i++) {
2989b7ec021fSScott Teel 		if (i >= HPSA_MAX_DEVICES) {
2990b7ec021fSScott Teel 			dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
2991b7ec021fSScott Teel 				"  %d devices ignored.\n", HPSA_MAX_DEVICES,
2992b7ec021fSScott Teel 				ndevs_to_allocate - HPSA_MAX_DEVICES);
2993b7ec021fSScott Teel 			break;
2994b7ec021fSScott Teel 		}
2995b7ec021fSScott Teel 
2996edd16368SStephen M. Cameron 		currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
2997edd16368SStephen M. Cameron 		if (!currentsd[i]) {
2998edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
2999edd16368SStephen M. Cameron 				__FILE__, __LINE__);
3000edd16368SStephen M. Cameron 			goto out;
3001edd16368SStephen M. Cameron 		}
3002edd16368SStephen M. Cameron 		ndev_allocated++;
3003edd16368SStephen M. Cameron 	}
3004edd16368SStephen M. Cameron 
30058645291bSStephen M. Cameron 	if (is_scsi_rev_5(h))
3006339b2b14SStephen M. Cameron 		raid_ctlr_position = 0;
3007339b2b14SStephen M. Cameron 	else
3008339b2b14SStephen M. Cameron 		raid_ctlr_position = nphysicals + nlogicals;
3009339b2b14SStephen M. Cameron 
3010edd16368SStephen M. Cameron 	/* adjust our table of devices */
30114f4eb9f1SScott Teel 	n_ext_target_devs = 0;
3012edd16368SStephen M. Cameron 	for (i = 0; i < nphysicals + nlogicals + 1; i++) {
30130b0e1d6cSStephen M. Cameron 		u8 *lunaddrbytes, is_OBDR = 0;
3014edd16368SStephen M. Cameron 
3015edd16368SStephen M. Cameron 		/* Figure out where the LUN ID info is coming from */
3016339b2b14SStephen M. Cameron 		lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
3017339b2b14SStephen M. Cameron 			i, nphysicals, nlogicals, physdev_list, logdev_list);
3018edd16368SStephen M. Cameron 		/* skip masked physical devices. */
3019339b2b14SStephen M. Cameron 		if (lunaddrbytes[3] & 0xC0 &&
3020339b2b14SStephen M. Cameron 			i < nphysicals + (raid_ctlr_position == 0))
3021edd16368SStephen M. Cameron 			continue;
3022edd16368SStephen M. Cameron 
3023edd16368SStephen M. Cameron 		/* Get device type, vendor, model, device id */
30240b0e1d6cSStephen M. Cameron 		if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
30250b0e1d6cSStephen M. Cameron 							&is_OBDR))
3026edd16368SStephen M. Cameron 			continue; /* skip it if we can't talk to it. */
30271f310bdeSStephen M. Cameron 		figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
3028edd16368SStephen M. Cameron 		this_device = currentsd[ncurrent];
3029edd16368SStephen M. Cameron 
3030edd16368SStephen M. Cameron 		/*
30314f4eb9f1SScott Teel 		 * For external target devices, we have to insert a LUN 0 which
3032edd16368SStephen M. Cameron 		 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
3033edd16368SStephen M. Cameron 		 * is nonetheless an enclosure device there.  We have to
3034edd16368SStephen M. Cameron 		 * present that otherwise linux won't find anything if
3035edd16368SStephen M. Cameron 		 * there is no lun 0.
3036edd16368SStephen M. Cameron 		 */
30374f4eb9f1SScott Teel 		if (add_ext_target_dev(h, tmpdevice, this_device,
30381f310bdeSStephen M. Cameron 				lunaddrbytes, lunzerobits,
30394f4eb9f1SScott Teel 				&n_ext_target_devs)) {
3040edd16368SStephen M. Cameron 			ncurrent++;
3041edd16368SStephen M. Cameron 			this_device = currentsd[ncurrent];
3042edd16368SStephen M. Cameron 		}
3043edd16368SStephen M. Cameron 
3044edd16368SStephen M. Cameron 		*this_device = *tmpdevice;
3045edd16368SStephen M. Cameron 
3046edd16368SStephen M. Cameron 		switch (this_device->devtype) {
30470b0e1d6cSStephen M. Cameron 		case TYPE_ROM:
3048edd16368SStephen M. Cameron 			/* We don't *really* support actual CD-ROM devices,
3049edd16368SStephen M. Cameron 			 * just "One Button Disaster Recovery" tape drive
3050edd16368SStephen M. Cameron 			 * which temporarily pretends to be a CD-ROM drive.
3051edd16368SStephen M. Cameron 			 * So we check that the device is really an OBDR tape
3052edd16368SStephen M. Cameron 			 * device by checking for "$DR-10" in bytes 43-48 of
3053edd16368SStephen M. Cameron 			 * the inquiry data.
3054edd16368SStephen M. Cameron 			 */
30550b0e1d6cSStephen M. Cameron 			if (is_OBDR)
3056edd16368SStephen M. Cameron 				ncurrent++;
3057edd16368SStephen M. Cameron 			break;
3058edd16368SStephen M. Cameron 		case TYPE_DISK:
3059316b221aSStephen M. Cameron 			if (h->hba_mode_enabled) {
3060316b221aSStephen M. Cameron 				/* never use raid mapper in HBA mode */
3061316b221aSStephen M. Cameron 				this_device->offload_enabled = 0;
3062316b221aSStephen M. Cameron 				ncurrent++;
3063316b221aSStephen M. Cameron 				break;
3064316b221aSStephen M. Cameron 			} else if (h->acciopath_status) {
3065283b4a9bSStephen M. Cameron 				if (i >= nphysicals) {
3066283b4a9bSStephen M. Cameron 					ncurrent++;
3067edd16368SStephen M. Cameron 					break;
3068283b4a9bSStephen M. Cameron 				}
3069316b221aSStephen M. Cameron 			} else {
3070316b221aSStephen M. Cameron 				if (i < nphysicals)
3071316b221aSStephen M. Cameron 					break;
3072316b221aSStephen M. Cameron 				ncurrent++;
3073316b221aSStephen M. Cameron 				break;
3074316b221aSStephen M. Cameron 			}
3075283b4a9bSStephen M. Cameron 			if (physical_mode == HPSA_REPORT_PHYS_EXTENDED) {
3076e1f7de0cSMatt Gates 				memcpy(&this_device->ioaccel_handle,
3077e1f7de0cSMatt Gates 					&lunaddrbytes[20],
3078e1f7de0cSMatt Gates 					sizeof(this_device->ioaccel_handle));
3079edd16368SStephen M. Cameron 				ncurrent++;
3080283b4a9bSStephen M. Cameron 			}
3081edd16368SStephen M. Cameron 			break;
3082edd16368SStephen M. Cameron 		case TYPE_TAPE:
3083edd16368SStephen M. Cameron 		case TYPE_MEDIUM_CHANGER:
3084edd16368SStephen M. Cameron 			ncurrent++;
3085edd16368SStephen M. Cameron 			break;
3086edd16368SStephen M. Cameron 		case TYPE_RAID:
3087edd16368SStephen M. Cameron 			/* Only present the Smartarray HBA as a RAID controller.
3088edd16368SStephen M. Cameron 			 * If it's a RAID controller other than the HBA itself
3089edd16368SStephen M. Cameron 			 * (an external RAID controller, MSA500 or similar)
3090edd16368SStephen M. Cameron 			 * don't present it.
3091edd16368SStephen M. Cameron 			 */
3092edd16368SStephen M. Cameron 			if (!is_hba_lunid(lunaddrbytes))
3093edd16368SStephen M. Cameron 				break;
3094edd16368SStephen M. Cameron 			ncurrent++;
3095edd16368SStephen M. Cameron 			break;
3096edd16368SStephen M. Cameron 		default:
3097edd16368SStephen M. Cameron 			break;
3098edd16368SStephen M. Cameron 		}
3099cfe5badcSScott Teel 		if (ncurrent >= HPSA_MAX_DEVICES)
3100edd16368SStephen M. Cameron 			break;
3101edd16368SStephen M. Cameron 	}
3102edd16368SStephen M. Cameron 	adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
3103edd16368SStephen M. Cameron out:
3104edd16368SStephen M. Cameron 	kfree(tmpdevice);
3105edd16368SStephen M. Cameron 	for (i = 0; i < ndev_allocated; i++)
3106edd16368SStephen M. Cameron 		kfree(currentsd[i]);
3107edd16368SStephen M. Cameron 	kfree(currentsd);
3108edd16368SStephen M. Cameron 	kfree(physdev_list);
3109edd16368SStephen M. Cameron 	kfree(logdev_list);
3110edd16368SStephen M. Cameron }
3111edd16368SStephen M. Cameron 
3112c7ee65b3SWebb Scales /*
3113c7ee65b3SWebb Scales  * hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
3114edd16368SStephen M. Cameron  * dma mapping  and fills in the scatter gather entries of the
3115edd16368SStephen M. Cameron  * hpsa command, cp.
3116edd16368SStephen M. Cameron  */
311733a2ffceSStephen M. Cameron static int hpsa_scatter_gather(struct ctlr_info *h,
3118edd16368SStephen M. Cameron 		struct CommandList *cp,
3119edd16368SStephen M. Cameron 		struct scsi_cmnd *cmd)
3120edd16368SStephen M. Cameron {
3121edd16368SStephen M. Cameron 	unsigned int len;
3122edd16368SStephen M. Cameron 	struct scatterlist *sg;
312301a02ffcSStephen M. Cameron 	u64 addr64;
312433a2ffceSStephen M. Cameron 	int use_sg, i, sg_index, chained;
312533a2ffceSStephen M. Cameron 	struct SGDescriptor *curr_sg;
3126edd16368SStephen M. Cameron 
312733a2ffceSStephen M. Cameron 	BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
3128edd16368SStephen M. Cameron 
3129edd16368SStephen M. Cameron 	use_sg = scsi_dma_map(cmd);
3130edd16368SStephen M. Cameron 	if (use_sg < 0)
3131edd16368SStephen M. Cameron 		return use_sg;
3132edd16368SStephen M. Cameron 
3133edd16368SStephen M. Cameron 	if (!use_sg)
3134edd16368SStephen M. Cameron 		goto sglist_finished;
3135edd16368SStephen M. Cameron 
313633a2ffceSStephen M. Cameron 	curr_sg = cp->SG;
313733a2ffceSStephen M. Cameron 	chained = 0;
313833a2ffceSStephen M. Cameron 	sg_index = 0;
3139edd16368SStephen M. Cameron 	scsi_for_each_sg(cmd, sg, use_sg, i) {
314033a2ffceSStephen M. Cameron 		if (i == h->max_cmd_sg_entries - 1 &&
314133a2ffceSStephen M. Cameron 			use_sg > h->max_cmd_sg_entries) {
314233a2ffceSStephen M. Cameron 			chained = 1;
314333a2ffceSStephen M. Cameron 			curr_sg = h->cmd_sg_list[cp->cmdindex];
314433a2ffceSStephen M. Cameron 			sg_index = 0;
314533a2ffceSStephen M. Cameron 		}
314601a02ffcSStephen M. Cameron 		addr64 = (u64) sg_dma_address(sg);
3147edd16368SStephen M. Cameron 		len  = sg_dma_len(sg);
314850a0decfSStephen M. Cameron 		curr_sg->Addr = cpu_to_le64(addr64);
314950a0decfSStephen M. Cameron 		curr_sg->Len = cpu_to_le32(len);
315050a0decfSStephen M. Cameron 		curr_sg->Ext = cpu_to_le32(0);
315133a2ffceSStephen M. Cameron 		curr_sg++;
315233a2ffceSStephen M. Cameron 	}
315350a0decfSStephen M. Cameron 	(--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
315433a2ffceSStephen M. Cameron 
315533a2ffceSStephen M. Cameron 	if (use_sg + chained > h->maxSG)
315633a2ffceSStephen M. Cameron 		h->maxSG = use_sg + chained;
315733a2ffceSStephen M. Cameron 
315833a2ffceSStephen M. Cameron 	if (chained) {
315933a2ffceSStephen M. Cameron 		cp->Header.SGList = h->max_cmd_sg_entries;
316050a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(use_sg + 1);
3161e2bea6dfSStephen M. Cameron 		if (hpsa_map_sg_chain_block(h, cp)) {
3162e2bea6dfSStephen M. Cameron 			scsi_dma_unmap(cmd);
3163e2bea6dfSStephen M. Cameron 			return -1;
3164e2bea6dfSStephen M. Cameron 		}
316533a2ffceSStephen M. Cameron 		return 0;
3166edd16368SStephen M. Cameron 	}
3167edd16368SStephen M. Cameron 
3168edd16368SStephen M. Cameron sglist_finished:
3169edd16368SStephen M. Cameron 
317001a02ffcSStephen M. Cameron 	cp->Header.SGList = (u8) use_sg;   /* no. SGs contig in this cmd */
3171c7ee65b3SWebb Scales 	cp->Header.SGTotal = cpu_to_le16(use_sg); /* total sgs in cmd list */
3172edd16368SStephen M. Cameron 	return 0;
3173edd16368SStephen M. Cameron }
3174edd16368SStephen M. Cameron 
3175283b4a9bSStephen M. Cameron #define IO_ACCEL_INELIGIBLE (1)
3176283b4a9bSStephen M. Cameron static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
3177283b4a9bSStephen M. Cameron {
3178283b4a9bSStephen M. Cameron 	int is_write = 0;
3179283b4a9bSStephen M. Cameron 	u32 block;
3180283b4a9bSStephen M. Cameron 	u32 block_cnt;
3181283b4a9bSStephen M. Cameron 
3182283b4a9bSStephen M. Cameron 	/* Perform some CDB fixups if needed using 10 byte reads/writes only */
3183283b4a9bSStephen M. Cameron 	switch (cdb[0]) {
3184283b4a9bSStephen M. Cameron 	case WRITE_6:
3185283b4a9bSStephen M. Cameron 	case WRITE_12:
3186283b4a9bSStephen M. Cameron 		is_write = 1;
3187283b4a9bSStephen M. Cameron 	case READ_6:
3188283b4a9bSStephen M. Cameron 	case READ_12:
3189283b4a9bSStephen M. Cameron 		if (*cdb_len == 6) {
3190283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 8) | cdb[3];
3191283b4a9bSStephen M. Cameron 			block_cnt = cdb[4];
3192283b4a9bSStephen M. Cameron 		} else {
3193283b4a9bSStephen M. Cameron 			BUG_ON(*cdb_len != 12);
3194283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 24) |
3195283b4a9bSStephen M. Cameron 				(((u32) cdb[3]) << 16) |
3196283b4a9bSStephen M. Cameron 				(((u32) cdb[4]) << 8) |
3197283b4a9bSStephen M. Cameron 				cdb[5];
3198283b4a9bSStephen M. Cameron 			block_cnt =
3199283b4a9bSStephen M. Cameron 				(((u32) cdb[6]) << 24) |
3200283b4a9bSStephen M. Cameron 				(((u32) cdb[7]) << 16) |
3201283b4a9bSStephen M. Cameron 				(((u32) cdb[8]) << 8) |
3202283b4a9bSStephen M. Cameron 				cdb[9];
3203283b4a9bSStephen M. Cameron 		}
3204283b4a9bSStephen M. Cameron 		if (block_cnt > 0xffff)
3205283b4a9bSStephen M. Cameron 			return IO_ACCEL_INELIGIBLE;
3206283b4a9bSStephen M. Cameron 
3207283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
3208283b4a9bSStephen M. Cameron 		cdb[1] = 0;
3209283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (block >> 24);
3210283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (block >> 16);
3211283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (block >> 8);
3212283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (block);
3213283b4a9bSStephen M. Cameron 		cdb[6] = 0;
3214283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (block_cnt >> 8);
3215283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (block_cnt);
3216283b4a9bSStephen M. Cameron 		cdb[9] = 0;
3217283b4a9bSStephen M. Cameron 		*cdb_len = 10;
3218283b4a9bSStephen M. Cameron 		break;
3219283b4a9bSStephen M. Cameron 	}
3220283b4a9bSStephen M. Cameron 	return 0;
3221283b4a9bSStephen M. Cameron }
3222283b4a9bSStephen M. Cameron 
3223c349775eSScott Teel static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
3224283b4a9bSStephen M. Cameron 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
3225283b4a9bSStephen M. Cameron 	u8 *scsi3addr)
3226e1f7de0cSMatt Gates {
3227e1f7de0cSMatt Gates 	struct scsi_cmnd *cmd = c->scsi_cmd;
3228e1f7de0cSMatt Gates 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
3229e1f7de0cSMatt Gates 	unsigned int len;
3230e1f7de0cSMatt Gates 	unsigned int total_len = 0;
3231e1f7de0cSMatt Gates 	struct scatterlist *sg;
3232e1f7de0cSMatt Gates 	u64 addr64;
3233e1f7de0cSMatt Gates 	int use_sg, i;
3234e1f7de0cSMatt Gates 	struct SGDescriptor *curr_sg;
3235e1f7de0cSMatt Gates 	u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
3236e1f7de0cSMatt Gates 
3237283b4a9bSStephen M. Cameron 	/* TODO: implement chaining support */
3238283b4a9bSStephen M. Cameron 	if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
3239283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3240283b4a9bSStephen M. Cameron 
3241e1f7de0cSMatt Gates 	BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
3242e1f7de0cSMatt Gates 
3243283b4a9bSStephen M. Cameron 	if (fixup_ioaccel_cdb(cdb, &cdb_len))
3244283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3245283b4a9bSStephen M. Cameron 
3246e1f7de0cSMatt Gates 	c->cmd_type = CMD_IOACCEL1;
3247e1f7de0cSMatt Gates 
3248e1f7de0cSMatt Gates 	/* Adjust the DMA address to point to the accelerated command buffer */
3249e1f7de0cSMatt Gates 	c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
3250e1f7de0cSMatt Gates 				(c->cmdindex * sizeof(*cp));
3251e1f7de0cSMatt Gates 	BUG_ON(c->busaddr & 0x0000007F);
3252e1f7de0cSMatt Gates 
3253e1f7de0cSMatt Gates 	use_sg = scsi_dma_map(cmd);
3254e1f7de0cSMatt Gates 	if (use_sg < 0)
3255e1f7de0cSMatt Gates 		return use_sg;
3256e1f7de0cSMatt Gates 
3257e1f7de0cSMatt Gates 	if (use_sg) {
3258e1f7de0cSMatt Gates 		curr_sg = cp->SG;
3259e1f7de0cSMatt Gates 		scsi_for_each_sg(cmd, sg, use_sg, i) {
3260e1f7de0cSMatt Gates 			addr64 = (u64) sg_dma_address(sg);
3261e1f7de0cSMatt Gates 			len  = sg_dma_len(sg);
3262e1f7de0cSMatt Gates 			total_len += len;
326350a0decfSStephen M. Cameron 			curr_sg->Addr = cpu_to_le64(addr64);
326450a0decfSStephen M. Cameron 			curr_sg->Len = cpu_to_le32(len);
326550a0decfSStephen M. Cameron 			curr_sg->Ext = cpu_to_le32(0);
3266e1f7de0cSMatt Gates 			curr_sg++;
3267e1f7de0cSMatt Gates 		}
326850a0decfSStephen M. Cameron 		(--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
3269e1f7de0cSMatt Gates 
3270e1f7de0cSMatt Gates 		switch (cmd->sc_data_direction) {
3271e1f7de0cSMatt Gates 		case DMA_TO_DEVICE:
3272e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_OUT;
3273e1f7de0cSMatt Gates 			break;
3274e1f7de0cSMatt Gates 		case DMA_FROM_DEVICE:
3275e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_IN;
3276e1f7de0cSMatt Gates 			break;
3277e1f7de0cSMatt Gates 		case DMA_NONE:
3278e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_NODATAXFER;
3279e1f7de0cSMatt Gates 			break;
3280e1f7de0cSMatt Gates 		default:
3281e1f7de0cSMatt Gates 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3282e1f7de0cSMatt Gates 			cmd->sc_data_direction);
3283e1f7de0cSMatt Gates 			BUG();
3284e1f7de0cSMatt Gates 			break;
3285e1f7de0cSMatt Gates 		}
3286e1f7de0cSMatt Gates 	} else {
3287e1f7de0cSMatt Gates 		control |= IOACCEL1_CONTROL_NODATAXFER;
3288e1f7de0cSMatt Gates 	}
3289e1f7de0cSMatt Gates 
3290c349775eSScott Teel 	c->Header.SGList = use_sg;
3291e1f7de0cSMatt Gates 	/* Fill out the command structure to submit */
32922b08b3e9SDon Brace 	cp->dev_handle = cpu_to_le16(ioaccel_handle & 0xFFFF);
32932b08b3e9SDon Brace 	cp->transfer_len = cpu_to_le32(total_len);
32942b08b3e9SDon Brace 	cp->io_flags = cpu_to_le16(IOACCEL1_IOFLAGS_IO_REQ |
32952b08b3e9SDon Brace 			(cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK));
32962b08b3e9SDon Brace 	cp->control = cpu_to_le32(control);
3297283b4a9bSStephen M. Cameron 	memcpy(cp->CDB, cdb, cdb_len);
3298283b4a9bSStephen M. Cameron 	memcpy(cp->CISS_LUN, scsi3addr, 8);
3299c349775eSScott Teel 	/* Tag was already set at init time. */
3300e1f7de0cSMatt Gates 	enqueue_cmd_and_start_io(h, c);
3301e1f7de0cSMatt Gates 	return 0;
3302e1f7de0cSMatt Gates }
3303edd16368SStephen M. Cameron 
3304283b4a9bSStephen M. Cameron /*
3305283b4a9bSStephen M. Cameron  * Queue a command directly to a device behind the controller using the
3306283b4a9bSStephen M. Cameron  * I/O accelerator path.
3307283b4a9bSStephen M. Cameron  */
3308283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
3309283b4a9bSStephen M. Cameron 	struct CommandList *c)
3310283b4a9bSStephen M. Cameron {
3311283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
3312283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3313283b4a9bSStephen M. Cameron 
3314283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
3315283b4a9bSStephen M. Cameron 		cmd->cmnd, cmd->cmd_len, dev->scsi3addr);
3316283b4a9bSStephen M. Cameron }
3317283b4a9bSStephen M. Cameron 
3318dd0e19f3SScott Teel /*
3319dd0e19f3SScott Teel  * Set encryption parameters for the ioaccel2 request
3320dd0e19f3SScott Teel  */
3321dd0e19f3SScott Teel static void set_encrypt_ioaccel2(struct ctlr_info *h,
3322dd0e19f3SScott Teel 	struct CommandList *c, struct io_accel2_cmd *cp)
3323dd0e19f3SScott Teel {
3324dd0e19f3SScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
3325dd0e19f3SScott Teel 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3326dd0e19f3SScott Teel 	struct raid_map_data *map = &dev->raid_map;
3327dd0e19f3SScott Teel 	u64 first_block;
3328dd0e19f3SScott Teel 
3329dd0e19f3SScott Teel 	BUG_ON(!(dev->offload_config && dev->offload_enabled));
3330dd0e19f3SScott Teel 
3331dd0e19f3SScott Teel 	/* Are we doing encryption on this device */
33322b08b3e9SDon Brace 	if (!(le16_to_cpu(map->flags) & RAID_MAP_FLAG_ENCRYPT_ON))
3333dd0e19f3SScott Teel 		return;
3334dd0e19f3SScott Teel 	/* Set the data encryption key index. */
3335dd0e19f3SScott Teel 	cp->dekindex = map->dekindex;
3336dd0e19f3SScott Teel 
3337dd0e19f3SScott Teel 	/* Set the encryption enable flag, encoded into direction field. */
3338dd0e19f3SScott Teel 	cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
3339dd0e19f3SScott Teel 
3340dd0e19f3SScott Teel 	/* Set encryption tweak values based on logical block address
3341dd0e19f3SScott Teel 	 * If block size is 512, tweak value is LBA.
3342dd0e19f3SScott Teel 	 * For other block sizes, tweak is (LBA * block size)/ 512)
3343dd0e19f3SScott Teel 	 */
3344dd0e19f3SScott Teel 	switch (cmd->cmnd[0]) {
3345dd0e19f3SScott Teel 	/* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
3346dd0e19f3SScott Teel 	case WRITE_6:
3347dd0e19f3SScott Teel 	case READ_6:
33482b08b3e9SDon Brace 		first_block = get_unaligned_be16(&cmd->cmnd[2]);
3349dd0e19f3SScott Teel 		break;
3350dd0e19f3SScott Teel 	case WRITE_10:
3351dd0e19f3SScott Teel 	case READ_10:
3352dd0e19f3SScott Teel 	/* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
3353dd0e19f3SScott Teel 	case WRITE_12:
3354dd0e19f3SScott Teel 	case READ_12:
33552b08b3e9SDon Brace 		first_block = get_unaligned_be32(&cmd->cmnd[2]);
3356dd0e19f3SScott Teel 		break;
3357dd0e19f3SScott Teel 	case WRITE_16:
3358dd0e19f3SScott Teel 	case READ_16:
33592b08b3e9SDon Brace 		first_block = get_unaligned_be64(&cmd->cmnd[2]);
3360dd0e19f3SScott Teel 		break;
3361dd0e19f3SScott Teel 	default:
3362dd0e19f3SScott Teel 		dev_err(&h->pdev->dev,
33632b08b3e9SDon Brace 			"ERROR: %s: size (0x%x) not supported for encryption\n",
33642b08b3e9SDon Brace 			__func__, cmd->cmnd[0]);
3365dd0e19f3SScott Teel 		BUG();
3366dd0e19f3SScott Teel 		break;
3367dd0e19f3SScott Teel 	}
33682b08b3e9SDon Brace 
33692b08b3e9SDon Brace 	if (le32_to_cpu(map->volume_blk_size) != 512)
33702b08b3e9SDon Brace 		first_block = first_block *
33712b08b3e9SDon Brace 				le32_to_cpu(map->volume_blk_size)/512;
33722b08b3e9SDon Brace 
33732b08b3e9SDon Brace 	cp->tweak_lower = cpu_to_le32(first_block);
33742b08b3e9SDon Brace 	cp->tweak_upper = cpu_to_le32(first_block >> 32);
3375dd0e19f3SScott Teel }
3376dd0e19f3SScott Teel 
3377c349775eSScott Teel static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
3378c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
3379c349775eSScott Teel 	u8 *scsi3addr)
3380c349775eSScott Teel {
3381c349775eSScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
3382c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
3383c349775eSScott Teel 	struct ioaccel2_sg_element *curr_sg;
3384c349775eSScott Teel 	int use_sg, i;
3385c349775eSScott Teel 	struct scatterlist *sg;
3386c349775eSScott Teel 	u64 addr64;
3387c349775eSScott Teel 	u32 len;
3388c349775eSScott Teel 	u32 total_len = 0;
3389c349775eSScott Teel 
3390c349775eSScott Teel 	if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
3391c349775eSScott Teel 		return IO_ACCEL_INELIGIBLE;
3392c349775eSScott Teel 
3393c349775eSScott Teel 	if (fixup_ioaccel_cdb(cdb, &cdb_len))
3394c349775eSScott Teel 		return IO_ACCEL_INELIGIBLE;
3395c349775eSScott Teel 	c->cmd_type = CMD_IOACCEL2;
3396c349775eSScott Teel 	/* Adjust the DMA address to point to the accelerated command buffer */
3397c349775eSScott Teel 	c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
3398c349775eSScott Teel 				(c->cmdindex * sizeof(*cp));
3399c349775eSScott Teel 	BUG_ON(c->busaddr & 0x0000007F);
3400c349775eSScott Teel 
3401c349775eSScott Teel 	memset(cp, 0, sizeof(*cp));
3402c349775eSScott Teel 	cp->IU_type = IOACCEL2_IU_TYPE;
3403c349775eSScott Teel 
3404c349775eSScott Teel 	use_sg = scsi_dma_map(cmd);
3405c349775eSScott Teel 	if (use_sg < 0)
3406c349775eSScott Teel 		return use_sg;
3407c349775eSScott Teel 
3408c349775eSScott Teel 	if (use_sg) {
3409c349775eSScott Teel 		BUG_ON(use_sg > IOACCEL2_MAXSGENTRIES);
3410c349775eSScott Teel 		curr_sg = cp->sg;
3411c349775eSScott Teel 		scsi_for_each_sg(cmd, sg, use_sg, i) {
3412c349775eSScott Teel 			addr64 = (u64) sg_dma_address(sg);
3413c349775eSScott Teel 			len  = sg_dma_len(sg);
3414c349775eSScott Teel 			total_len += len;
3415c349775eSScott Teel 			curr_sg->address = cpu_to_le64(addr64);
3416c349775eSScott Teel 			curr_sg->length = cpu_to_le32(len);
3417c349775eSScott Teel 			curr_sg->reserved[0] = 0;
3418c349775eSScott Teel 			curr_sg->reserved[1] = 0;
3419c349775eSScott Teel 			curr_sg->reserved[2] = 0;
3420c349775eSScott Teel 			curr_sg->chain_indicator = 0;
3421c349775eSScott Teel 			curr_sg++;
3422c349775eSScott Teel 		}
3423c349775eSScott Teel 
3424c349775eSScott Teel 		switch (cmd->sc_data_direction) {
3425c349775eSScott Teel 		case DMA_TO_DEVICE:
3426dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3427dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_OUT;
3428c349775eSScott Teel 			break;
3429c349775eSScott Teel 		case DMA_FROM_DEVICE:
3430dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3431dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_IN;
3432c349775eSScott Teel 			break;
3433c349775eSScott Teel 		case DMA_NONE:
3434dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3435dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_NO_DATA;
3436c349775eSScott Teel 			break;
3437c349775eSScott Teel 		default:
3438c349775eSScott Teel 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3439c349775eSScott Teel 				cmd->sc_data_direction);
3440c349775eSScott Teel 			BUG();
3441c349775eSScott Teel 			break;
3442c349775eSScott Teel 		}
3443c349775eSScott Teel 	} else {
3444dd0e19f3SScott Teel 		cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3445dd0e19f3SScott Teel 		cp->direction |= IOACCEL2_DIR_NO_DATA;
3446c349775eSScott Teel 	}
3447dd0e19f3SScott Teel 
3448dd0e19f3SScott Teel 	/* Set encryption parameters, if necessary */
3449dd0e19f3SScott Teel 	set_encrypt_ioaccel2(h, c, cp);
3450dd0e19f3SScott Teel 
34512b08b3e9SDon Brace 	cp->scsi_nexus = cpu_to_le32(ioaccel_handle);
34522b08b3e9SDon Brace 	cp->Tag = cpu_to_le32(c->cmdindex << DIRECT_LOOKUP_SHIFT |
34532b08b3e9SDon Brace 				DIRECT_LOOKUP_BIT);
3454c349775eSScott Teel 	memcpy(cp->cdb, cdb, sizeof(cp->cdb));
3455c349775eSScott Teel 
3456c349775eSScott Teel 	/* fill in sg elements */
3457c349775eSScott Teel 	cp->sg_count = (u8) use_sg;
3458c349775eSScott Teel 
3459c349775eSScott Teel 	cp->data_len = cpu_to_le32(total_len);
3460c349775eSScott Teel 	cp->err_ptr = cpu_to_le64(c->busaddr +
3461c349775eSScott Teel 			offsetof(struct io_accel2_cmd, error_data));
346250a0decfSStephen M. Cameron 	cp->err_len = cpu_to_le32(sizeof(cp->error_data));
3463c349775eSScott Teel 
3464c349775eSScott Teel 	enqueue_cmd_and_start_io(h, c);
3465c349775eSScott Teel 	return 0;
3466c349775eSScott Teel }
3467c349775eSScott Teel 
3468c349775eSScott Teel /*
3469c349775eSScott Teel  * Queue a command to the correct I/O accelerator path.
3470c349775eSScott Teel  */
3471c349775eSScott Teel static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
3472c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
3473c349775eSScott Teel 	u8 *scsi3addr)
3474c349775eSScott Teel {
3475c349775eSScott Teel 	if (h->transMethod & CFGTBL_Trans_io_accel1)
3476c349775eSScott Teel 		return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
3477c349775eSScott Teel 						cdb, cdb_len, scsi3addr);
3478c349775eSScott Teel 	else
3479c349775eSScott Teel 		return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
3480c349775eSScott Teel 						cdb, cdb_len, scsi3addr);
3481c349775eSScott Teel }
3482c349775eSScott Teel 
34836b80b18fSScott Teel static void raid_map_helper(struct raid_map_data *map,
34846b80b18fSScott Teel 		int offload_to_mirror, u32 *map_index, u32 *current_group)
34856b80b18fSScott Teel {
34866b80b18fSScott Teel 	if (offload_to_mirror == 0)  {
34876b80b18fSScott Teel 		/* use physical disk in the first mirrored group. */
34882b08b3e9SDon Brace 		*map_index %= le16_to_cpu(map->data_disks_per_row);
34896b80b18fSScott Teel 		return;
34906b80b18fSScott Teel 	}
34916b80b18fSScott Teel 	do {
34926b80b18fSScott Teel 		/* determine mirror group that *map_index indicates */
34932b08b3e9SDon Brace 		*current_group = *map_index /
34942b08b3e9SDon Brace 			le16_to_cpu(map->data_disks_per_row);
34956b80b18fSScott Teel 		if (offload_to_mirror == *current_group)
34966b80b18fSScott Teel 			continue;
34972b08b3e9SDon Brace 		if (*current_group < le16_to_cpu(map->layout_map_count) - 1) {
34986b80b18fSScott Teel 			/* select map index from next group */
34992b08b3e9SDon Brace 			*map_index += le16_to_cpu(map->data_disks_per_row);
35006b80b18fSScott Teel 			(*current_group)++;
35016b80b18fSScott Teel 		} else {
35026b80b18fSScott Teel 			/* select map index from first group */
35032b08b3e9SDon Brace 			*map_index %= le16_to_cpu(map->data_disks_per_row);
35046b80b18fSScott Teel 			*current_group = 0;
35056b80b18fSScott Teel 		}
35066b80b18fSScott Teel 	} while (offload_to_mirror != *current_group);
35076b80b18fSScott Teel }
35086b80b18fSScott Teel 
3509283b4a9bSStephen M. Cameron /*
3510283b4a9bSStephen M. Cameron  * Attempt to perform offload RAID mapping for a logical volume I/O.
3511283b4a9bSStephen M. Cameron  */
3512283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
3513283b4a9bSStephen M. Cameron 	struct CommandList *c)
3514283b4a9bSStephen M. Cameron {
3515283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
3516283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3517283b4a9bSStephen M. Cameron 	struct raid_map_data *map = &dev->raid_map;
3518283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map->data[0];
3519283b4a9bSStephen M. Cameron 	int is_write = 0;
3520283b4a9bSStephen M. Cameron 	u32 map_index;
3521283b4a9bSStephen M. Cameron 	u64 first_block, last_block;
3522283b4a9bSStephen M. Cameron 	u32 block_cnt;
3523283b4a9bSStephen M. Cameron 	u32 blocks_per_row;
3524283b4a9bSStephen M. Cameron 	u64 first_row, last_row;
3525283b4a9bSStephen M. Cameron 	u32 first_row_offset, last_row_offset;
3526283b4a9bSStephen M. Cameron 	u32 first_column, last_column;
35276b80b18fSScott Teel 	u64 r0_first_row, r0_last_row;
35286b80b18fSScott Teel 	u32 r5or6_blocks_per_row;
35296b80b18fSScott Teel 	u64 r5or6_first_row, r5or6_last_row;
35306b80b18fSScott Teel 	u32 r5or6_first_row_offset, r5or6_last_row_offset;
35316b80b18fSScott Teel 	u32 r5or6_first_column, r5or6_last_column;
35326b80b18fSScott Teel 	u32 total_disks_per_row;
35336b80b18fSScott Teel 	u32 stripesize;
35346b80b18fSScott Teel 	u32 first_group, last_group, current_group;
3535283b4a9bSStephen M. Cameron 	u32 map_row;
3536283b4a9bSStephen M. Cameron 	u32 disk_handle;
3537283b4a9bSStephen M. Cameron 	u64 disk_block;
3538283b4a9bSStephen M. Cameron 	u32 disk_block_cnt;
3539283b4a9bSStephen M. Cameron 	u8 cdb[16];
3540283b4a9bSStephen M. Cameron 	u8 cdb_len;
35412b08b3e9SDon Brace 	u16 strip_size;
3542283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
3543283b4a9bSStephen M. Cameron 	u64 tmpdiv;
3544283b4a9bSStephen M. Cameron #endif
35456b80b18fSScott Teel 	int offload_to_mirror;
3546283b4a9bSStephen M. Cameron 
3547283b4a9bSStephen M. Cameron 	BUG_ON(!(dev->offload_config && dev->offload_enabled));
3548283b4a9bSStephen M. Cameron 
3549283b4a9bSStephen M. Cameron 	/* check for valid opcode, get LBA and block count */
3550283b4a9bSStephen M. Cameron 	switch (cmd->cmnd[0]) {
3551283b4a9bSStephen M. Cameron 	case WRITE_6:
3552283b4a9bSStephen M. Cameron 		is_write = 1;
3553283b4a9bSStephen M. Cameron 	case READ_6:
3554283b4a9bSStephen M. Cameron 		first_block =
3555283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 8) |
3556283b4a9bSStephen M. Cameron 			cmd->cmnd[3];
3557283b4a9bSStephen M. Cameron 		block_cnt = cmd->cmnd[4];
35583fa89a04SStephen M. Cameron 		if (block_cnt == 0)
35593fa89a04SStephen M. Cameron 			block_cnt = 256;
3560283b4a9bSStephen M. Cameron 		break;
3561283b4a9bSStephen M. Cameron 	case WRITE_10:
3562283b4a9bSStephen M. Cameron 		is_write = 1;
3563283b4a9bSStephen M. Cameron 	case READ_10:
3564283b4a9bSStephen M. Cameron 		first_block =
3565283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
3566283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
3567283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
3568283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
3569283b4a9bSStephen M. Cameron 		block_cnt =
3570283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 8) |
3571283b4a9bSStephen M. Cameron 			cmd->cmnd[8];
3572283b4a9bSStephen M. Cameron 		break;
3573283b4a9bSStephen M. Cameron 	case WRITE_12:
3574283b4a9bSStephen M. Cameron 		is_write = 1;
3575283b4a9bSStephen M. Cameron 	case READ_12:
3576283b4a9bSStephen M. Cameron 		first_block =
3577283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
3578283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
3579283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
3580283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
3581283b4a9bSStephen M. Cameron 		block_cnt =
3582283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[6]) << 24) |
3583283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 16) |
3584283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[8]) << 8) |
3585283b4a9bSStephen M. Cameron 		cmd->cmnd[9];
3586283b4a9bSStephen M. Cameron 		break;
3587283b4a9bSStephen M. Cameron 	case WRITE_16:
3588283b4a9bSStephen M. Cameron 		is_write = 1;
3589283b4a9bSStephen M. Cameron 	case READ_16:
3590283b4a9bSStephen M. Cameron 		first_block =
3591283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 56) |
3592283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 48) |
3593283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 40) |
3594283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[5]) << 32) |
3595283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[6]) << 24) |
3596283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[7]) << 16) |
3597283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[8]) << 8) |
3598283b4a9bSStephen M. Cameron 			cmd->cmnd[9];
3599283b4a9bSStephen M. Cameron 		block_cnt =
3600283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[10]) << 24) |
3601283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[11]) << 16) |
3602283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[12]) << 8) |
3603283b4a9bSStephen M. Cameron 			cmd->cmnd[13];
3604283b4a9bSStephen M. Cameron 		break;
3605283b4a9bSStephen M. Cameron 	default:
3606283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
3607283b4a9bSStephen M. Cameron 	}
3608283b4a9bSStephen M. Cameron 	last_block = first_block + block_cnt - 1;
3609283b4a9bSStephen M. Cameron 
3610283b4a9bSStephen M. Cameron 	/* check for write to non-RAID-0 */
3611283b4a9bSStephen M. Cameron 	if (is_write && dev->raid_level != 0)
3612283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3613283b4a9bSStephen M. Cameron 
3614283b4a9bSStephen M. Cameron 	/* check for invalid block or wraparound */
36152b08b3e9SDon Brace 	if (last_block >= le64_to_cpu(map->volume_blk_cnt) ||
36162b08b3e9SDon Brace 		last_block < first_block)
3617283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3618283b4a9bSStephen M. Cameron 
3619283b4a9bSStephen M. Cameron 	/* calculate stripe information for the request */
36202b08b3e9SDon Brace 	blocks_per_row = le16_to_cpu(map->data_disks_per_row) *
36212b08b3e9SDon Brace 				le16_to_cpu(map->strip_size);
36222b08b3e9SDon Brace 	strip_size = le16_to_cpu(map->strip_size);
3623283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
3624283b4a9bSStephen M. Cameron 	tmpdiv = first_block;
3625283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
3626283b4a9bSStephen M. Cameron 	first_row = tmpdiv;
3627283b4a9bSStephen M. Cameron 	tmpdiv = last_block;
3628283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
3629283b4a9bSStephen M. Cameron 	last_row = tmpdiv;
3630283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
3631283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
3632283b4a9bSStephen M. Cameron 	tmpdiv = first_row_offset;
36332b08b3e9SDon Brace 	(void) do_div(tmpdiv, strip_size);
3634283b4a9bSStephen M. Cameron 	first_column = tmpdiv;
3635283b4a9bSStephen M. Cameron 	tmpdiv = last_row_offset;
36362b08b3e9SDon Brace 	(void) do_div(tmpdiv, strip_size);
3637283b4a9bSStephen M. Cameron 	last_column = tmpdiv;
3638283b4a9bSStephen M. Cameron #else
3639283b4a9bSStephen M. Cameron 	first_row = first_block / blocks_per_row;
3640283b4a9bSStephen M. Cameron 	last_row = last_block / blocks_per_row;
3641283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
3642283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
36432b08b3e9SDon Brace 	first_column = first_row_offset / strip_size;
36442b08b3e9SDon Brace 	last_column = last_row_offset / strip_size;
3645283b4a9bSStephen M. Cameron #endif
3646283b4a9bSStephen M. Cameron 
3647283b4a9bSStephen M. Cameron 	/* if this isn't a single row/column then give to the controller */
3648283b4a9bSStephen M. Cameron 	if ((first_row != last_row) || (first_column != last_column))
3649283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3650283b4a9bSStephen M. Cameron 
3651283b4a9bSStephen M. Cameron 	/* proceeding with driver mapping */
36522b08b3e9SDon Brace 	total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
36532b08b3e9SDon Brace 				le16_to_cpu(map->metadata_disks_per_row);
3654283b4a9bSStephen M. Cameron 	map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
36552b08b3e9SDon Brace 				le16_to_cpu(map->row_cnt);
36566b80b18fSScott Teel 	map_index = (map_row * total_disks_per_row) + first_column;
36576b80b18fSScott Teel 
36586b80b18fSScott Teel 	switch (dev->raid_level) {
36596b80b18fSScott Teel 	case HPSA_RAID_0:
36606b80b18fSScott Teel 		break; /* nothing special to do */
36616b80b18fSScott Teel 	case HPSA_RAID_1:
36626b80b18fSScott Teel 		/* Handles load balance across RAID 1 members.
36636b80b18fSScott Teel 		 * (2-drive R1 and R10 with even # of drives.)
36646b80b18fSScott Teel 		 * Appropriate for SSDs, not optimal for HDDs
3665283b4a9bSStephen M. Cameron 		 */
36662b08b3e9SDon Brace 		BUG_ON(le16_to_cpu(map->layout_map_count) != 2);
3667283b4a9bSStephen M. Cameron 		if (dev->offload_to_mirror)
36682b08b3e9SDon Brace 			map_index += le16_to_cpu(map->data_disks_per_row);
3669283b4a9bSStephen M. Cameron 		dev->offload_to_mirror = !dev->offload_to_mirror;
36706b80b18fSScott Teel 		break;
36716b80b18fSScott Teel 	case HPSA_RAID_ADM:
36726b80b18fSScott Teel 		/* Handles N-way mirrors  (R1-ADM)
36736b80b18fSScott Teel 		 * and R10 with # of drives divisible by 3.)
36746b80b18fSScott Teel 		 */
36752b08b3e9SDon Brace 		BUG_ON(le16_to_cpu(map->layout_map_count) != 3);
36766b80b18fSScott Teel 
36776b80b18fSScott Teel 		offload_to_mirror = dev->offload_to_mirror;
36786b80b18fSScott Teel 		raid_map_helper(map, offload_to_mirror,
36796b80b18fSScott Teel 				&map_index, &current_group);
36806b80b18fSScott Teel 		/* set mirror group to use next time */
36816b80b18fSScott Teel 		offload_to_mirror =
36822b08b3e9SDon Brace 			(offload_to_mirror >=
36832b08b3e9SDon Brace 			le16_to_cpu(map->layout_map_count) - 1)
36846b80b18fSScott Teel 			? 0 : offload_to_mirror + 1;
36856b80b18fSScott Teel 		dev->offload_to_mirror = offload_to_mirror;
36866b80b18fSScott Teel 		/* Avoid direct use of dev->offload_to_mirror within this
36876b80b18fSScott Teel 		 * function since multiple threads might simultaneously
36886b80b18fSScott Teel 		 * increment it beyond the range of dev->layout_map_count -1.
36896b80b18fSScott Teel 		 */
36906b80b18fSScott Teel 		break;
36916b80b18fSScott Teel 	case HPSA_RAID_5:
36926b80b18fSScott Teel 	case HPSA_RAID_6:
36932b08b3e9SDon Brace 		if (le16_to_cpu(map->layout_map_count) <= 1)
36946b80b18fSScott Teel 			break;
36956b80b18fSScott Teel 
36966b80b18fSScott Teel 		/* Verify first and last block are in same RAID group */
36976b80b18fSScott Teel 		r5or6_blocks_per_row =
36982b08b3e9SDon Brace 			le16_to_cpu(map->strip_size) *
36992b08b3e9SDon Brace 			le16_to_cpu(map->data_disks_per_row);
37006b80b18fSScott Teel 		BUG_ON(r5or6_blocks_per_row == 0);
37012b08b3e9SDon Brace 		stripesize = r5or6_blocks_per_row *
37022b08b3e9SDon Brace 			le16_to_cpu(map->layout_map_count);
37036b80b18fSScott Teel #if BITS_PER_LONG == 32
37046b80b18fSScott Teel 		tmpdiv = first_block;
37056b80b18fSScott Teel 		first_group = do_div(tmpdiv, stripesize);
37066b80b18fSScott Teel 		tmpdiv = first_group;
37076b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
37086b80b18fSScott Teel 		first_group = tmpdiv;
37096b80b18fSScott Teel 		tmpdiv = last_block;
37106b80b18fSScott Teel 		last_group = do_div(tmpdiv, stripesize);
37116b80b18fSScott Teel 		tmpdiv = last_group;
37126b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
37136b80b18fSScott Teel 		last_group = tmpdiv;
37146b80b18fSScott Teel #else
37156b80b18fSScott Teel 		first_group = (first_block % stripesize) / r5or6_blocks_per_row;
37166b80b18fSScott Teel 		last_group = (last_block % stripesize) / r5or6_blocks_per_row;
37176b80b18fSScott Teel #endif
3718000ff7c2SStephen M. Cameron 		if (first_group != last_group)
37196b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
37206b80b18fSScott Teel 
37216b80b18fSScott Teel 		/* Verify request is in a single row of RAID 5/6 */
37226b80b18fSScott Teel #if BITS_PER_LONG == 32
37236b80b18fSScott Teel 		tmpdiv = first_block;
37246b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
37256b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row = tmpdiv;
37266b80b18fSScott Teel 		tmpdiv = last_block;
37276b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
37286b80b18fSScott Teel 		r5or6_last_row = r0_last_row = tmpdiv;
37296b80b18fSScott Teel #else
37306b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row =
37316b80b18fSScott Teel 						first_block / stripesize;
37326b80b18fSScott Teel 		r5or6_last_row = r0_last_row = last_block / stripesize;
37336b80b18fSScott Teel #endif
37346b80b18fSScott Teel 		if (r5or6_first_row != r5or6_last_row)
37356b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
37366b80b18fSScott Teel 
37376b80b18fSScott Teel 
37386b80b18fSScott Teel 		/* Verify request is in a single column */
37396b80b18fSScott Teel #if BITS_PER_LONG == 32
37406b80b18fSScott Teel 		tmpdiv = first_block;
37416b80b18fSScott Teel 		first_row_offset = do_div(tmpdiv, stripesize);
37426b80b18fSScott Teel 		tmpdiv = first_row_offset;
37436b80b18fSScott Teel 		first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
37446b80b18fSScott Teel 		r5or6_first_row_offset = first_row_offset;
37456b80b18fSScott Teel 		tmpdiv = last_block;
37466b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, stripesize);
37476b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
37486b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
37496b80b18fSScott Teel 		tmpdiv = r5or6_first_row_offset;
37506b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
37516b80b18fSScott Teel 		first_column = r5or6_first_column = tmpdiv;
37526b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
37536b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
37546b80b18fSScott Teel 		r5or6_last_column = tmpdiv;
37556b80b18fSScott Teel #else
37566b80b18fSScott Teel 		first_row_offset = r5or6_first_row_offset =
37576b80b18fSScott Teel 			(u32)((first_block % stripesize) %
37586b80b18fSScott Teel 						r5or6_blocks_per_row);
37596b80b18fSScott Teel 
37606b80b18fSScott Teel 		r5or6_last_row_offset =
37616b80b18fSScott Teel 			(u32)((last_block % stripesize) %
37626b80b18fSScott Teel 						r5or6_blocks_per_row);
37636b80b18fSScott Teel 
37646b80b18fSScott Teel 		first_column = r5or6_first_column =
37652b08b3e9SDon Brace 			r5or6_first_row_offset / le16_to_cpu(map->strip_size);
37666b80b18fSScott Teel 		r5or6_last_column =
37672b08b3e9SDon Brace 			r5or6_last_row_offset / le16_to_cpu(map->strip_size);
37686b80b18fSScott Teel #endif
37696b80b18fSScott Teel 		if (r5or6_first_column != r5or6_last_column)
37706b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
37716b80b18fSScott Teel 
37726b80b18fSScott Teel 		/* Request is eligible */
37736b80b18fSScott Teel 		map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
37742b08b3e9SDon Brace 			le16_to_cpu(map->row_cnt);
37756b80b18fSScott Teel 
37766b80b18fSScott Teel 		map_index = (first_group *
37772b08b3e9SDon Brace 			(le16_to_cpu(map->row_cnt) * total_disks_per_row)) +
37786b80b18fSScott Teel 			(map_row * total_disks_per_row) + first_column;
37796b80b18fSScott Teel 		break;
37806b80b18fSScott Teel 	default:
37816b80b18fSScott Teel 		return IO_ACCEL_INELIGIBLE;
3782283b4a9bSStephen M. Cameron 	}
37836b80b18fSScott Teel 
3784283b4a9bSStephen M. Cameron 	disk_handle = dd[map_index].ioaccel_handle;
37852b08b3e9SDon Brace 	disk_block = le64_to_cpu(map->disk_starting_blk) +
37862b08b3e9SDon Brace 			first_row * le16_to_cpu(map->strip_size) +
37872b08b3e9SDon Brace 			(first_row_offset - first_column *
37882b08b3e9SDon Brace 			le16_to_cpu(map->strip_size));
3789283b4a9bSStephen M. Cameron 	disk_block_cnt = block_cnt;
3790283b4a9bSStephen M. Cameron 
3791283b4a9bSStephen M. Cameron 	/* handle differing logical/physical block sizes */
3792283b4a9bSStephen M. Cameron 	if (map->phys_blk_shift) {
3793283b4a9bSStephen M. Cameron 		disk_block <<= map->phys_blk_shift;
3794283b4a9bSStephen M. Cameron 		disk_block_cnt <<= map->phys_blk_shift;
3795283b4a9bSStephen M. Cameron 	}
3796283b4a9bSStephen M. Cameron 	BUG_ON(disk_block_cnt > 0xffff);
3797283b4a9bSStephen M. Cameron 
3798283b4a9bSStephen M. Cameron 	/* build the new CDB for the physical disk I/O */
3799283b4a9bSStephen M. Cameron 	if (disk_block > 0xffffffff) {
3800283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_16 : READ_16;
3801283b4a9bSStephen M. Cameron 		cdb[1] = 0;
3802283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 56);
3803283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 48);
3804283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 40);
3805283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block >> 32);
3806283b4a9bSStephen M. Cameron 		cdb[6] = (u8) (disk_block >> 24);
3807283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block >> 16);
3808283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block >> 8);
3809283b4a9bSStephen M. Cameron 		cdb[9] = (u8) (disk_block);
3810283b4a9bSStephen M. Cameron 		cdb[10] = (u8) (disk_block_cnt >> 24);
3811283b4a9bSStephen M. Cameron 		cdb[11] = (u8) (disk_block_cnt >> 16);
3812283b4a9bSStephen M. Cameron 		cdb[12] = (u8) (disk_block_cnt >> 8);
3813283b4a9bSStephen M. Cameron 		cdb[13] = (u8) (disk_block_cnt);
3814283b4a9bSStephen M. Cameron 		cdb[14] = 0;
3815283b4a9bSStephen M. Cameron 		cdb[15] = 0;
3816283b4a9bSStephen M. Cameron 		cdb_len = 16;
3817283b4a9bSStephen M. Cameron 	} else {
3818283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
3819283b4a9bSStephen M. Cameron 		cdb[1] = 0;
3820283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 24);
3821283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 16);
3822283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 8);
3823283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block);
3824283b4a9bSStephen M. Cameron 		cdb[6] = 0;
3825283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block_cnt >> 8);
3826283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block_cnt);
3827283b4a9bSStephen M. Cameron 		cdb[9] = 0;
3828283b4a9bSStephen M. Cameron 		cdb_len = 10;
3829283b4a9bSStephen M. Cameron 	}
3830283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
3831283b4a9bSStephen M. Cameron 						dev->scsi3addr);
3832283b4a9bSStephen M. Cameron }
3833283b4a9bSStephen M. Cameron 
3834763aadbfSNicholas Bellinger /*
3835763aadbfSNicholas Bellinger  * Running in struct Scsi_Host->host_lock less mode using LLD internal
3836763aadbfSNicholas Bellinger  * struct ctlr_info *h->lock w/ spin_lock_irqsave() protection.
3837763aadbfSNicholas Bellinger  */
3838763aadbfSNicholas Bellinger static int hpsa_scsi_queue_command(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
3839edd16368SStephen M. Cameron {
3840edd16368SStephen M. Cameron 	struct ctlr_info *h;
3841edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
3842edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
3843edd16368SStephen M. Cameron 	struct CommandList *c;
3844283b4a9bSStephen M. Cameron 	int rc = 0;
3845edd16368SStephen M. Cameron 
3846edd16368SStephen M. Cameron 	/* Get the ptr to our adapter structure out of cmd->host. */
3847edd16368SStephen M. Cameron 	h = sdev_to_hba(cmd->device);
3848edd16368SStephen M. Cameron 	dev = cmd->device->hostdata;
3849edd16368SStephen M. Cameron 	if (!dev) {
3850edd16368SStephen M. Cameron 		cmd->result = DID_NO_CONNECT << 16;
3851763aadbfSNicholas Bellinger 		cmd->scsi_done(cmd);
3852edd16368SStephen M. Cameron 		return 0;
3853edd16368SStephen M. Cameron 	}
3854edd16368SStephen M. Cameron 	memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
3855edd16368SStephen M. Cameron 
3856094963daSStephen M. Cameron 	if (unlikely(lockup_detected(h))) {
3857a0c12413SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
3858763aadbfSNicholas Bellinger 		cmd->scsi_done(cmd);
3859a0c12413SStephen M. Cameron 		return 0;
3860a0c12413SStephen M. Cameron 	}
3861e16a33adSMatt Gates 	c = cmd_alloc(h);
3862edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
3863edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
3864edd16368SStephen M. Cameron 		return SCSI_MLQUEUE_HOST_BUSY;
3865edd16368SStephen M. Cameron 	}
3866edd16368SStephen M. Cameron 
3867edd16368SStephen M. Cameron 	/* Fill in the command list header */
3868edd16368SStephen M. Cameron 	/* save c in case we have to abort it  */
3869edd16368SStephen M. Cameron 	cmd->host_scribble = (unsigned char *) c;
3870edd16368SStephen M. Cameron 
3871edd16368SStephen M. Cameron 	c->cmd_type = CMD_SCSI;
3872edd16368SStephen M. Cameron 	c->scsi_cmd = cmd;
3873e1f7de0cSMatt Gates 
3874283b4a9bSStephen M. Cameron 	/* Call alternate submit routine for I/O accelerated commands.
3875283b4a9bSStephen M. Cameron 	 * Retries always go down the normal I/O path.
3876283b4a9bSStephen M. Cameron 	 */
3877283b4a9bSStephen M. Cameron 	if (likely(cmd->retries == 0 &&
3878da0697bdSScott Teel 		cmd->request->cmd_type == REQ_TYPE_FS &&
3879da0697bdSScott Teel 		h->acciopath_status)) {
3880283b4a9bSStephen M. Cameron 		if (dev->offload_enabled) {
3881283b4a9bSStephen M. Cameron 			rc = hpsa_scsi_ioaccel_raid_map(h, c);
3882283b4a9bSStephen M. Cameron 			if (rc == 0)
3883283b4a9bSStephen M. Cameron 				return 0; /* Sent on ioaccel path */
3884283b4a9bSStephen M. Cameron 			if (rc < 0) {   /* scsi_dma_map failed. */
3885283b4a9bSStephen M. Cameron 				cmd_free(h, c);
3886283b4a9bSStephen M. Cameron 				return SCSI_MLQUEUE_HOST_BUSY;
3887283b4a9bSStephen M. Cameron 			}
3888283b4a9bSStephen M. Cameron 		} else if (dev->ioaccel_handle) {
3889283b4a9bSStephen M. Cameron 			rc = hpsa_scsi_ioaccel_direct_map(h, c);
3890283b4a9bSStephen M. Cameron 			if (rc == 0)
3891283b4a9bSStephen M. Cameron 				return 0; /* Sent on direct map path */
3892283b4a9bSStephen M. Cameron 			if (rc < 0) {   /* scsi_dma_map failed. */
3893283b4a9bSStephen M. Cameron 				cmd_free(h, c);
3894283b4a9bSStephen M. Cameron 				return SCSI_MLQUEUE_HOST_BUSY;
3895283b4a9bSStephen M. Cameron 			}
3896283b4a9bSStephen M. Cameron 		}
3897283b4a9bSStephen M. Cameron 	}
3898e1f7de0cSMatt Gates 
3899edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;  /* unused in simple mode */
3900edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
390150a0decfSStephen M. Cameron 	c->Header.tag = cpu_to_le64((c->cmdindex << DIRECT_LOOKUP_SHIFT) |
390250a0decfSStephen M. Cameron 					DIRECT_LOOKUP_BIT);
3903edd16368SStephen M. Cameron 
3904edd16368SStephen M. Cameron 	/* Fill in the request block... */
3905edd16368SStephen M. Cameron 
3906edd16368SStephen M. Cameron 	c->Request.Timeout = 0;
3907edd16368SStephen M. Cameron 	memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
3908edd16368SStephen M. Cameron 	BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
3909edd16368SStephen M. Cameron 	c->Request.CDBLen = cmd->cmd_len;
3910edd16368SStephen M. Cameron 	memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
3911edd16368SStephen M. Cameron 	switch (cmd->sc_data_direction) {
3912edd16368SStephen M. Cameron 	case DMA_TO_DEVICE:
3913a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
3914a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_WRITE);
3915edd16368SStephen M. Cameron 		break;
3916edd16368SStephen M. Cameron 	case DMA_FROM_DEVICE:
3917a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
3918a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_READ);
3919edd16368SStephen M. Cameron 		break;
3920edd16368SStephen M. Cameron 	case DMA_NONE:
3921a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
3922a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_NONE);
3923edd16368SStephen M. Cameron 		break;
3924edd16368SStephen M. Cameron 	case DMA_BIDIRECTIONAL:
3925edd16368SStephen M. Cameron 		/* This can happen if a buggy application does a scsi passthru
3926edd16368SStephen M. Cameron 		 * and sets both inlen and outlen to non-zero. ( see
3927edd16368SStephen M. Cameron 		 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
3928edd16368SStephen M. Cameron 		 */
3929edd16368SStephen M. Cameron 
3930a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
3931a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_RSVD);
3932edd16368SStephen M. Cameron 		/* This is technically wrong, and hpsa controllers should
3933edd16368SStephen M. Cameron 		 * reject it with CMD_INVALID, which is the most correct
3934edd16368SStephen M. Cameron 		 * response, but non-fibre backends appear to let it
3935edd16368SStephen M. Cameron 		 * slide by, and give the same results as if this field
3936edd16368SStephen M. Cameron 		 * were set correctly.  Either way is acceptable for
3937edd16368SStephen M. Cameron 		 * our purposes here.
3938edd16368SStephen M. Cameron 		 */
3939edd16368SStephen M. Cameron 
3940edd16368SStephen M. Cameron 		break;
3941edd16368SStephen M. Cameron 
3942edd16368SStephen M. Cameron 	default:
3943edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3944edd16368SStephen M. Cameron 			cmd->sc_data_direction);
3945edd16368SStephen M. Cameron 		BUG();
3946edd16368SStephen M. Cameron 		break;
3947edd16368SStephen M. Cameron 	}
3948edd16368SStephen M. Cameron 
394933a2ffceSStephen M. Cameron 	if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
3950edd16368SStephen M. Cameron 		cmd_free(h, c);
3951edd16368SStephen M. Cameron 		return SCSI_MLQUEUE_HOST_BUSY;
3952edd16368SStephen M. Cameron 	}
3953edd16368SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
3954edd16368SStephen M. Cameron 	/* the cmd'll come back via intr handler in complete_scsi_command()  */
3955edd16368SStephen M. Cameron 	return 0;
3956edd16368SStephen M. Cameron }
3957edd16368SStephen M. Cameron 
39585f389360SStephen M. Cameron static int do_not_scan_if_controller_locked_up(struct ctlr_info *h)
39595f389360SStephen M. Cameron {
39605f389360SStephen M. Cameron 	unsigned long flags;
39615f389360SStephen M. Cameron 
39625f389360SStephen M. Cameron 	/*
39635f389360SStephen M. Cameron 	 * Don't let rescans be initiated on a controller known
39645f389360SStephen M. Cameron 	 * to be locked up.  If the controller locks up *during*
39655f389360SStephen M. Cameron 	 * a rescan, that thread is probably hosed, but at least
39665f389360SStephen M. Cameron 	 * we can prevent new rescan threads from piling up on a
39675f389360SStephen M. Cameron 	 * locked up controller.
39685f389360SStephen M. Cameron 	 */
3969094963daSStephen M. Cameron 	if (unlikely(lockup_detected(h))) {
39705f389360SStephen M. Cameron 		spin_lock_irqsave(&h->scan_lock, flags);
39715f389360SStephen M. Cameron 		h->scan_finished = 1;
39725f389360SStephen M. Cameron 		wake_up_all(&h->scan_wait_queue);
39735f389360SStephen M. Cameron 		spin_unlock_irqrestore(&h->scan_lock, flags);
39745f389360SStephen M. Cameron 		return 1;
39755f389360SStephen M. Cameron 	}
39765f389360SStephen M. Cameron 	return 0;
39775f389360SStephen M. Cameron }
39785f389360SStephen M. Cameron 
3979a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *sh)
3980a08a8471SStephen M. Cameron {
3981a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
3982a08a8471SStephen M. Cameron 	unsigned long flags;
3983a08a8471SStephen M. Cameron 
39845f389360SStephen M. Cameron 	if (do_not_scan_if_controller_locked_up(h))
39855f389360SStephen M. Cameron 		return;
39865f389360SStephen M. Cameron 
3987a08a8471SStephen M. Cameron 	/* wait until any scan already in progress is finished. */
3988a08a8471SStephen M. Cameron 	while (1) {
3989a08a8471SStephen M. Cameron 		spin_lock_irqsave(&h->scan_lock, flags);
3990a08a8471SStephen M. Cameron 		if (h->scan_finished)
3991a08a8471SStephen M. Cameron 			break;
3992a08a8471SStephen M. Cameron 		spin_unlock_irqrestore(&h->scan_lock, flags);
3993a08a8471SStephen M. Cameron 		wait_event(h->scan_wait_queue, h->scan_finished);
3994a08a8471SStephen M. Cameron 		/* Note: We don't need to worry about a race between this
3995a08a8471SStephen M. Cameron 		 * thread and driver unload because the midlayer will
3996a08a8471SStephen M. Cameron 		 * have incremented the reference count, so unload won't
3997a08a8471SStephen M. Cameron 		 * happen if we're in here.
3998a08a8471SStephen M. Cameron 		 */
3999a08a8471SStephen M. Cameron 	}
4000a08a8471SStephen M. Cameron 	h->scan_finished = 0; /* mark scan as in progress */
4001a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4002a08a8471SStephen M. Cameron 
40035f389360SStephen M. Cameron 	if (do_not_scan_if_controller_locked_up(h))
40045f389360SStephen M. Cameron 		return;
40055f389360SStephen M. Cameron 
4006a08a8471SStephen M. Cameron 	hpsa_update_scsi_devices(h, h->scsi_host->host_no);
4007a08a8471SStephen M. Cameron 
4008a08a8471SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
4009a08a8471SStephen M. Cameron 	h->scan_finished = 1; /* mark scan as finished. */
4010a08a8471SStephen M. Cameron 	wake_up_all(&h->scan_wait_queue);
4011a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4012a08a8471SStephen M. Cameron }
4013a08a8471SStephen M. Cameron 
40147c0a0229SDon Brace static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth)
40157c0a0229SDon Brace {
40167c0a0229SDon Brace 	struct ctlr_info *h = sdev_to_hba(sdev);
40177c0a0229SDon Brace 
40187c0a0229SDon Brace 	if (qdepth < 1)
40197c0a0229SDon Brace 		qdepth = 1;
40207c0a0229SDon Brace 	else
40217c0a0229SDon Brace 		if (qdepth > h->nr_cmds)
40227c0a0229SDon Brace 			qdepth = h->nr_cmds;
40237c0a0229SDon Brace 	scsi_change_queue_depth(sdev, qdepth);
40247c0a0229SDon Brace 	return sdev->queue_depth;
40257c0a0229SDon Brace }
40267c0a0229SDon Brace 
4027a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
4028a08a8471SStephen M. Cameron 	unsigned long elapsed_time)
4029a08a8471SStephen M. Cameron {
4030a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
4031a08a8471SStephen M. Cameron 	unsigned long flags;
4032a08a8471SStephen M. Cameron 	int finished;
4033a08a8471SStephen M. Cameron 
4034a08a8471SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
4035a08a8471SStephen M. Cameron 	finished = h->scan_finished;
4036a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4037a08a8471SStephen M. Cameron 	return finished;
4038a08a8471SStephen M. Cameron }
4039a08a8471SStephen M. Cameron 
4040edd16368SStephen M. Cameron static void hpsa_unregister_scsi(struct ctlr_info *h)
4041edd16368SStephen M. Cameron {
4042edd16368SStephen M. Cameron 	/* we are being forcibly unloaded, and may not refuse. */
4043edd16368SStephen M. Cameron 	scsi_remove_host(h->scsi_host);
4044edd16368SStephen M. Cameron 	scsi_host_put(h->scsi_host);
4045edd16368SStephen M. Cameron 	h->scsi_host = NULL;
4046edd16368SStephen M. Cameron }
4047edd16368SStephen M. Cameron 
4048edd16368SStephen M. Cameron static int hpsa_register_scsi(struct ctlr_info *h)
4049edd16368SStephen M. Cameron {
4050b705690dSStephen M. Cameron 	struct Scsi_Host *sh;
4051b705690dSStephen M. Cameron 	int error;
4052edd16368SStephen M. Cameron 
4053b705690dSStephen M. Cameron 	sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
4054b705690dSStephen M. Cameron 	if (sh == NULL)
4055b705690dSStephen M. Cameron 		goto fail;
4056b705690dSStephen M. Cameron 
4057b705690dSStephen M. Cameron 	sh->io_port = 0;
4058b705690dSStephen M. Cameron 	sh->n_io_port = 0;
4059b705690dSStephen M. Cameron 	sh->this_id = -1;
4060b705690dSStephen M. Cameron 	sh->max_channel = 3;
4061b705690dSStephen M. Cameron 	sh->max_cmd_len = MAX_COMMAND_SIZE;
4062b705690dSStephen M. Cameron 	sh->max_lun = HPSA_MAX_LUN;
4063b705690dSStephen M. Cameron 	sh->max_id = HPSA_MAX_LUN;
4064d54c5c24SStephen Cameron 	sh->can_queue = h->nr_cmds -
4065d54c5c24SStephen Cameron 			HPSA_CMDS_RESERVED_FOR_ABORTS -
4066d54c5c24SStephen Cameron 			HPSA_CMDS_RESERVED_FOR_DRIVER -
4067d54c5c24SStephen Cameron 			HPSA_MAX_CONCURRENT_PASSTHRUS;
4068316b221aSStephen M. Cameron 	if (h->hba_mode_enabled)
4069316b221aSStephen M. Cameron 		sh->cmd_per_lun = 7;
4070316b221aSStephen M. Cameron 	else
4071d54c5c24SStephen Cameron 		sh->cmd_per_lun = sh->can_queue;
4072b705690dSStephen M. Cameron 	sh->sg_tablesize = h->maxsgentries;
4073b705690dSStephen M. Cameron 	h->scsi_host = sh;
4074b705690dSStephen M. Cameron 	sh->hostdata[0] = (unsigned long) h;
4075b705690dSStephen M. Cameron 	sh->irq = h->intr[h->intr_mode];
4076b705690dSStephen M. Cameron 	sh->unique_id = sh->irq;
4077b705690dSStephen M. Cameron 	error = scsi_add_host(sh, &h->pdev->dev);
4078b705690dSStephen M. Cameron 	if (error)
4079b705690dSStephen M. Cameron 		goto fail_host_put;
4080b705690dSStephen M. Cameron 	scsi_scan_host(sh);
4081b705690dSStephen M. Cameron 	return 0;
4082b705690dSStephen M. Cameron 
4083b705690dSStephen M. Cameron  fail_host_put:
4084b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_add_host"
4085b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
4086b705690dSStephen M. Cameron 	scsi_host_put(sh);
4087b705690dSStephen M. Cameron 	return error;
4088b705690dSStephen M. Cameron  fail:
4089b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_host_alloc"
4090b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
4091b705690dSStephen M. Cameron 	return -ENOMEM;
4092edd16368SStephen M. Cameron }
4093edd16368SStephen M. Cameron 
4094edd16368SStephen M. Cameron static int wait_for_device_to_become_ready(struct ctlr_info *h,
4095edd16368SStephen M. Cameron 	unsigned char lunaddr[])
4096edd16368SStephen M. Cameron {
40978919358eSTomas Henzl 	int rc;
4098edd16368SStephen M. Cameron 	int count = 0;
4099edd16368SStephen M. Cameron 	int waittime = 1; /* seconds */
4100edd16368SStephen M. Cameron 	struct CommandList *c;
4101edd16368SStephen M. Cameron 
4102*45fcb86eSStephen Cameron 	c = cmd_alloc(h);
4103edd16368SStephen M. Cameron 	if (!c) {
4104edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in "
4105edd16368SStephen M. Cameron 			"wait_for_device_to_become_ready.\n");
4106edd16368SStephen M. Cameron 		return IO_ERROR;
4107edd16368SStephen M. Cameron 	}
4108edd16368SStephen M. Cameron 
4109edd16368SStephen M. Cameron 	/* Send test unit ready until device ready, or give up. */
4110edd16368SStephen M. Cameron 	while (count < HPSA_TUR_RETRY_LIMIT) {
4111edd16368SStephen M. Cameron 
4112edd16368SStephen M. Cameron 		/* Wait for a bit.  do this first, because if we send
4113edd16368SStephen M. Cameron 		 * the TUR right away, the reset will just abort it.
4114edd16368SStephen M. Cameron 		 */
4115edd16368SStephen M. Cameron 		msleep(1000 * waittime);
4116edd16368SStephen M. Cameron 		count++;
41178919358eSTomas Henzl 		rc = 0; /* Device ready. */
4118edd16368SStephen M. Cameron 
4119edd16368SStephen M. Cameron 		/* Increase wait time with each try, up to a point. */
4120edd16368SStephen M. Cameron 		if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
4121edd16368SStephen M. Cameron 			waittime = waittime * 2;
4122edd16368SStephen M. Cameron 
4123a2dac136SStephen M. Cameron 		/* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
4124a2dac136SStephen M. Cameron 		(void) fill_cmd(c, TEST_UNIT_READY, h,
4125a2dac136SStephen M. Cameron 				NULL, 0, 0, lunaddr, TYPE_CMD);
4126edd16368SStephen M. Cameron 		hpsa_scsi_do_simple_cmd_core(h, c);
4127edd16368SStephen M. Cameron 		/* no unmap needed here because no data xfer. */
4128edd16368SStephen M. Cameron 
4129edd16368SStephen M. Cameron 		if (c->err_info->CommandStatus == CMD_SUCCESS)
4130edd16368SStephen M. Cameron 			break;
4131edd16368SStephen M. Cameron 
4132edd16368SStephen M. Cameron 		if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
4133edd16368SStephen M. Cameron 			c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
4134edd16368SStephen M. Cameron 			(c->err_info->SenseInfo[2] == NO_SENSE ||
4135edd16368SStephen M. Cameron 			c->err_info->SenseInfo[2] == UNIT_ATTENTION))
4136edd16368SStephen M. Cameron 			break;
4137edd16368SStephen M. Cameron 
4138edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "waiting %d secs "
4139edd16368SStephen M. Cameron 			"for device to become ready.\n", waittime);
4140edd16368SStephen M. Cameron 		rc = 1; /* device not ready. */
4141edd16368SStephen M. Cameron 	}
4142edd16368SStephen M. Cameron 
4143edd16368SStephen M. Cameron 	if (rc)
4144edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "giving up on device.\n");
4145edd16368SStephen M. Cameron 	else
4146edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "device is ready.\n");
4147edd16368SStephen M. Cameron 
4148*45fcb86eSStephen Cameron 	cmd_free(h, c);
4149edd16368SStephen M. Cameron 	return rc;
4150edd16368SStephen M. Cameron }
4151edd16368SStephen M. Cameron 
4152edd16368SStephen M. Cameron /* Need at least one of these error handlers to keep ../scsi/hosts.c from
4153edd16368SStephen M. Cameron  * complaining.  Doing a host- or bus-reset can't do anything good here.
4154edd16368SStephen M. Cameron  */
4155edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
4156edd16368SStephen M. Cameron {
4157edd16368SStephen M. Cameron 	int rc;
4158edd16368SStephen M. Cameron 	struct ctlr_info *h;
4159edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
4160edd16368SStephen M. Cameron 
4161edd16368SStephen M. Cameron 	/* find the controller to which the command to be aborted was sent */
4162edd16368SStephen M. Cameron 	h = sdev_to_hba(scsicmd->device);
4163edd16368SStephen M. Cameron 	if (h == NULL) /* paranoia */
4164edd16368SStephen M. Cameron 		return FAILED;
4165edd16368SStephen M. Cameron 	dev = scsicmd->device->hostdata;
4166edd16368SStephen M. Cameron 	if (!dev) {
4167edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
4168edd16368SStephen M. Cameron 			"device lookup failed.\n");
4169edd16368SStephen M. Cameron 		return FAILED;
4170edd16368SStephen M. Cameron 	}
4171d416b0c7SStephen M. Cameron 	dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
4172d416b0c7SStephen M. Cameron 		h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
4173edd16368SStephen M. Cameron 	/* send a reset to the SCSI LUN which the command was sent to */
4174bf711ac6SScott Teel 	rc = hpsa_send_reset(h, dev->scsi3addr, HPSA_RESET_TYPE_LUN);
4175edd16368SStephen M. Cameron 	if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
4176edd16368SStephen M. Cameron 		return SUCCESS;
4177edd16368SStephen M. Cameron 
4178edd16368SStephen M. Cameron 	dev_warn(&h->pdev->dev, "resetting device failed.\n");
4179edd16368SStephen M. Cameron 	return FAILED;
4180edd16368SStephen M. Cameron }
4181edd16368SStephen M. Cameron 
41826cba3f19SStephen M. Cameron static void swizzle_abort_tag(u8 *tag)
41836cba3f19SStephen M. Cameron {
41846cba3f19SStephen M. Cameron 	u8 original_tag[8];
41856cba3f19SStephen M. Cameron 
41866cba3f19SStephen M. Cameron 	memcpy(original_tag, tag, 8);
41876cba3f19SStephen M. Cameron 	tag[0] = original_tag[3];
41886cba3f19SStephen M. Cameron 	tag[1] = original_tag[2];
41896cba3f19SStephen M. Cameron 	tag[2] = original_tag[1];
41906cba3f19SStephen M. Cameron 	tag[3] = original_tag[0];
41916cba3f19SStephen M. Cameron 	tag[4] = original_tag[7];
41926cba3f19SStephen M. Cameron 	tag[5] = original_tag[6];
41936cba3f19SStephen M. Cameron 	tag[6] = original_tag[5];
41946cba3f19SStephen M. Cameron 	tag[7] = original_tag[4];
41956cba3f19SStephen M. Cameron }
41966cba3f19SStephen M. Cameron 
419717eb87d2SScott Teel static void hpsa_get_tag(struct ctlr_info *h,
41982b08b3e9SDon Brace 	struct CommandList *c, __le32 *taglower, __le32 *tagupper)
419917eb87d2SScott Teel {
42002b08b3e9SDon Brace 	u64 tag;
420117eb87d2SScott Teel 	if (c->cmd_type == CMD_IOACCEL1) {
420217eb87d2SScott Teel 		struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
420317eb87d2SScott Teel 			&h->ioaccel_cmd_pool[c->cmdindex];
42042b08b3e9SDon Brace 		tag = le64_to_cpu(cm1->tag);
42052b08b3e9SDon Brace 		*tagupper = cpu_to_le32(tag >> 32);
42062b08b3e9SDon Brace 		*taglower = cpu_to_le32(tag);
420754b6e9e9SScott Teel 		return;
420854b6e9e9SScott Teel 	}
420954b6e9e9SScott Teel 	if (c->cmd_type == CMD_IOACCEL2) {
421054b6e9e9SScott Teel 		struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
421154b6e9e9SScott Teel 			&h->ioaccel2_cmd_pool[c->cmdindex];
4212dd0e19f3SScott Teel 		/* upper tag not used in ioaccel2 mode */
4213dd0e19f3SScott Teel 		memset(tagupper, 0, sizeof(*tagupper));
4214dd0e19f3SScott Teel 		*taglower = cm2->Tag;
421554b6e9e9SScott Teel 		return;
421654b6e9e9SScott Teel 	}
42172b08b3e9SDon Brace 	tag = le64_to_cpu(c->Header.tag);
42182b08b3e9SDon Brace 	*tagupper = cpu_to_le32(tag >> 32);
42192b08b3e9SDon Brace 	*taglower = cpu_to_le32(tag);
422017eb87d2SScott Teel }
422154b6e9e9SScott Teel 
422275167d2cSStephen M. Cameron static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
42236cba3f19SStephen M. Cameron 	struct CommandList *abort, int swizzle)
422475167d2cSStephen M. Cameron {
422575167d2cSStephen M. Cameron 	int rc = IO_OK;
422675167d2cSStephen M. Cameron 	struct CommandList *c;
422775167d2cSStephen M. Cameron 	struct ErrorInfo *ei;
42282b08b3e9SDon Brace 	__le32 tagupper, taglower;
422975167d2cSStephen M. Cameron 
4230*45fcb86eSStephen Cameron 	c = cmd_alloc(h);
423175167d2cSStephen M. Cameron 	if (c == NULL) {	/* trouble... */
4232*45fcb86eSStephen Cameron 		dev_warn(&h->pdev->dev, "cmd_alloc returned NULL!\n");
423375167d2cSStephen M. Cameron 		return -ENOMEM;
423475167d2cSStephen M. Cameron 	}
423575167d2cSStephen M. Cameron 
4236a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no buffer to map */
4237a2dac136SStephen M. Cameron 	(void) fill_cmd(c, HPSA_ABORT_MSG, h, abort,
4238a2dac136SStephen M. Cameron 		0, 0, scsi3addr, TYPE_MSG);
42396cba3f19SStephen M. Cameron 	if (swizzle)
42406cba3f19SStephen M. Cameron 		swizzle_abort_tag(&c->Request.CDB[4]);
424175167d2cSStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core(h, c);
424217eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
424375167d2cSStephen M. Cameron 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd_core completed.\n",
424417eb87d2SScott Teel 		__func__, tagupper, taglower);
424575167d2cSStephen M. Cameron 	/* no unmap needed here because no data xfer. */
424675167d2cSStephen M. Cameron 
424775167d2cSStephen M. Cameron 	ei = c->err_info;
424875167d2cSStephen M. Cameron 	switch (ei->CommandStatus) {
424975167d2cSStephen M. Cameron 	case CMD_SUCCESS:
425075167d2cSStephen M. Cameron 		break;
425175167d2cSStephen M. Cameron 	case CMD_UNABORTABLE: /* Very common, don't make noise. */
425275167d2cSStephen M. Cameron 		rc = -1;
425375167d2cSStephen M. Cameron 		break;
425475167d2cSStephen M. Cameron 	default:
425575167d2cSStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
425617eb87d2SScott Teel 			__func__, tagupper, taglower);
4257d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
425875167d2cSStephen M. Cameron 		rc = -1;
425975167d2cSStephen M. Cameron 		break;
426075167d2cSStephen M. Cameron 	}
4261*45fcb86eSStephen Cameron 	cmd_free(h, c);
4262dd0e19f3SScott Teel 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n",
4263dd0e19f3SScott Teel 		__func__, tagupper, taglower);
426475167d2cSStephen M. Cameron 	return rc;
426575167d2cSStephen M. Cameron }
426675167d2cSStephen M. Cameron 
426775167d2cSStephen M. Cameron /*
426875167d2cSStephen M. Cameron  * hpsa_find_cmd_in_queue
426975167d2cSStephen M. Cameron  *
427075167d2cSStephen M. Cameron  * Used to determine whether a command (find) is still present
427175167d2cSStephen M. Cameron  * in queue_head.   Optionally excludes the last element of queue_head.
427275167d2cSStephen M. Cameron  *
427375167d2cSStephen M. Cameron  * This is used to avoid unnecessary aborts.  Commands in h->reqQ have
427475167d2cSStephen M. Cameron  * not yet been submitted, and so can be aborted by the driver without
427575167d2cSStephen M. Cameron  * sending an abort to the hardware.
427675167d2cSStephen M. Cameron  *
427775167d2cSStephen M. Cameron  * Returns pointer to command if found in queue, NULL otherwise.
427875167d2cSStephen M. Cameron  */
427975167d2cSStephen M. Cameron static struct CommandList *hpsa_find_cmd_in_queue(struct ctlr_info *h,
428075167d2cSStephen M. Cameron 			struct scsi_cmnd *find, struct list_head *queue_head)
428175167d2cSStephen M. Cameron {
428275167d2cSStephen M. Cameron 	unsigned long flags;
428375167d2cSStephen M. Cameron 	struct CommandList *c = NULL;	/* ptr into cmpQ */
428475167d2cSStephen M. Cameron 
428575167d2cSStephen M. Cameron 	if (!find)
428642a91641SDon Brace 		return NULL;
428775167d2cSStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
428875167d2cSStephen M. Cameron 	list_for_each_entry(c, queue_head, list) {
428975167d2cSStephen M. Cameron 		if (c->scsi_cmd == NULL) /* e.g.: passthru ioctl */
429075167d2cSStephen M. Cameron 			continue;
429175167d2cSStephen M. Cameron 		if (c->scsi_cmd == find) {
429275167d2cSStephen M. Cameron 			spin_unlock_irqrestore(&h->lock, flags);
429375167d2cSStephen M. Cameron 			return c;
429475167d2cSStephen M. Cameron 		}
429575167d2cSStephen M. Cameron 	}
429675167d2cSStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
429775167d2cSStephen M. Cameron 	return NULL;
429875167d2cSStephen M. Cameron }
429975167d2cSStephen M. Cameron 
43006cba3f19SStephen M. Cameron static struct CommandList *hpsa_find_cmd_in_queue_by_tag(struct ctlr_info *h,
43016cba3f19SStephen M. Cameron 					u8 *tag, struct list_head *queue_head)
43026cba3f19SStephen M. Cameron {
43036cba3f19SStephen M. Cameron 	unsigned long flags;
43046cba3f19SStephen M. Cameron 	struct CommandList *c;
43056cba3f19SStephen M. Cameron 
43066cba3f19SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
43076cba3f19SStephen M. Cameron 	list_for_each_entry(c, queue_head, list) {
430850a0decfSStephen M. Cameron 		if (memcmp(&c->Header.tag, tag, 8) != 0)
43096cba3f19SStephen M. Cameron 			continue;
43106cba3f19SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
43116cba3f19SStephen M. Cameron 		return c;
43126cba3f19SStephen M. Cameron 	}
43136cba3f19SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
43146cba3f19SStephen M. Cameron 	return NULL;
43156cba3f19SStephen M. Cameron }
43166cba3f19SStephen M. Cameron 
431754b6e9e9SScott Teel /* ioaccel2 path firmware cannot handle abort task requests.
431854b6e9e9SScott Teel  * Change abort requests to physical target reset, and send to the
431954b6e9e9SScott Teel  * address of the physical disk used for the ioaccel 2 command.
432054b6e9e9SScott Teel  * Return 0 on success (IO_OK)
432154b6e9e9SScott Teel  *	 -1 on failure
432254b6e9e9SScott Teel  */
432354b6e9e9SScott Teel 
432454b6e9e9SScott Teel static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
432554b6e9e9SScott Teel 	unsigned char *scsi3addr, struct CommandList *abort)
432654b6e9e9SScott Teel {
432754b6e9e9SScott Teel 	int rc = IO_OK;
432854b6e9e9SScott Teel 	struct scsi_cmnd *scmd; /* scsi command within request being aborted */
432954b6e9e9SScott Teel 	struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
433054b6e9e9SScott Teel 	unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
433154b6e9e9SScott Teel 	unsigned char *psa = &phys_scsi3addr[0];
433254b6e9e9SScott Teel 
433354b6e9e9SScott Teel 	/* Get a pointer to the hpsa logical device. */
433454b6e9e9SScott Teel 	scmd = (struct scsi_cmnd *) abort->scsi_cmd;
433554b6e9e9SScott Teel 	dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
433654b6e9e9SScott Teel 	if (dev == NULL) {
433754b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
433854b6e9e9SScott Teel 			"Cannot abort: no device pointer for command.\n");
433954b6e9e9SScott Teel 			return -1; /* not abortable */
434054b6e9e9SScott Teel 	}
434154b6e9e9SScott Teel 
43422ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
43432ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
43442ba8bfc8SStephen 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",
43452ba8bfc8SStephen M. Cameron 			h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
43462ba8bfc8SStephen M. Cameron 			scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
43472ba8bfc8SStephen M. Cameron 			scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]);
43482ba8bfc8SStephen M. Cameron 
434954b6e9e9SScott Teel 	if (!dev->offload_enabled) {
435054b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
435154b6e9e9SScott Teel 			"Can't abort: device is not operating in HP SSD Smart Path mode.\n");
435254b6e9e9SScott Teel 		return -1; /* not abortable */
435354b6e9e9SScott Teel 	}
435454b6e9e9SScott Teel 
435554b6e9e9SScott Teel 	/* Incoming scsi3addr is logical addr. We need physical disk addr. */
435654b6e9e9SScott Teel 	if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
435754b6e9e9SScott Teel 		dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
435854b6e9e9SScott Teel 		return -1; /* not abortable */
435954b6e9e9SScott Teel 	}
436054b6e9e9SScott Teel 
436154b6e9e9SScott Teel 	/* send the reset */
43622ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
43632ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
43642ba8bfc8SStephen M. Cameron 			"Reset as abort: Resetting physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
43652ba8bfc8SStephen M. Cameron 			psa[0], psa[1], psa[2], psa[3],
43662ba8bfc8SStephen M. Cameron 			psa[4], psa[5], psa[6], psa[7]);
436754b6e9e9SScott Teel 	rc = hpsa_send_reset(h, psa, HPSA_RESET_TYPE_TARGET);
436854b6e9e9SScott Teel 	if (rc != 0) {
436954b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
437054b6e9e9SScott Teel 			"Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
437154b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
437254b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
437354b6e9e9SScott Teel 		return rc; /* failed to reset */
437454b6e9e9SScott Teel 	}
437554b6e9e9SScott Teel 
437654b6e9e9SScott Teel 	/* wait for device to recover */
437754b6e9e9SScott Teel 	if (wait_for_device_to_become_ready(h, psa) != 0) {
437854b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
437954b6e9e9SScott Teel 			"Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
438054b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
438154b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
438254b6e9e9SScott Teel 		return -1;  /* failed to recover */
438354b6e9e9SScott Teel 	}
438454b6e9e9SScott Teel 
438554b6e9e9SScott Teel 	/* device recovered */
438654b6e9e9SScott Teel 	dev_info(&h->pdev->dev,
438754b6e9e9SScott Teel 		"Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
438854b6e9e9SScott Teel 		psa[0], psa[1], psa[2], psa[3],
438954b6e9e9SScott Teel 		psa[4], psa[5], psa[6], psa[7]);
439054b6e9e9SScott Teel 
439154b6e9e9SScott Teel 	return rc; /* success */
439254b6e9e9SScott Teel }
439354b6e9e9SScott Teel 
43946cba3f19SStephen M. Cameron /* Some Smart Arrays need the abort tag swizzled, and some don't.  It's hard to
43956cba3f19SStephen M. Cameron  * tell which kind we're dealing with, so we send the abort both ways.  There
43966cba3f19SStephen M. Cameron  * shouldn't be any collisions between swizzled and unswizzled tags due to the
43976cba3f19SStephen M. Cameron  * way we construct our tags but we check anyway in case the assumptions which
43986cba3f19SStephen M. Cameron  * make this true someday become false.
43996cba3f19SStephen M. Cameron  */
44006cba3f19SStephen M. Cameron static int hpsa_send_abort_both_ways(struct ctlr_info *h,
44016cba3f19SStephen M. Cameron 	unsigned char *scsi3addr, struct CommandList *abort)
44026cba3f19SStephen M. Cameron {
44036cba3f19SStephen M. Cameron 	u8 swizzled_tag[8];
44046cba3f19SStephen M. Cameron 	struct CommandList *c;
44056cba3f19SStephen M. Cameron 	int rc = 0, rc2 = 0;
44066cba3f19SStephen M. Cameron 
440754b6e9e9SScott Teel 	/* ioccelerator mode 2 commands should be aborted via the
440854b6e9e9SScott Teel 	 * accelerated path, since RAID path is unaware of these commands,
440954b6e9e9SScott Teel 	 * but underlying firmware can't handle abort TMF.
441054b6e9e9SScott Teel 	 * Change abort to physical device reset.
441154b6e9e9SScott Teel 	 */
441254b6e9e9SScott Teel 	if (abort->cmd_type == CMD_IOACCEL2)
441354b6e9e9SScott Teel 		return hpsa_send_reset_as_abort_ioaccel2(h, scsi3addr, abort);
441454b6e9e9SScott Teel 
44156cba3f19SStephen M. Cameron 	/* we do not expect to find the swizzled tag in our queue, but
44166cba3f19SStephen M. Cameron 	 * check anyway just to be sure the assumptions which make this
44176cba3f19SStephen M. Cameron 	 * the case haven't become wrong.
44186cba3f19SStephen M. Cameron 	 */
44196cba3f19SStephen M. Cameron 	memcpy(swizzled_tag, &abort->Request.CDB[4], 8);
44206cba3f19SStephen M. Cameron 	swizzle_abort_tag(swizzled_tag);
44216cba3f19SStephen M. Cameron 	c = hpsa_find_cmd_in_queue_by_tag(h, swizzled_tag, &h->cmpQ);
44226cba3f19SStephen M. Cameron 	if (c != NULL) {
44236cba3f19SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Unexpectedly found byte-swapped tag in completion queue.\n");
44246cba3f19SStephen M. Cameron 		return hpsa_send_abort(h, scsi3addr, abort, 0);
44256cba3f19SStephen M. Cameron 	}
44266cba3f19SStephen M. Cameron 	rc = hpsa_send_abort(h, scsi3addr, abort, 0);
44276cba3f19SStephen M. Cameron 
44286cba3f19SStephen M. Cameron 	/* if the command is still in our queue, we can't conclude that it was
44296cba3f19SStephen M. Cameron 	 * aborted (it might have just completed normally) but in any case
44306cba3f19SStephen M. Cameron 	 * we don't need to try to abort it another way.
44316cba3f19SStephen M. Cameron 	 */
44326cba3f19SStephen M. Cameron 	c = hpsa_find_cmd_in_queue(h, abort->scsi_cmd, &h->cmpQ);
44336cba3f19SStephen M. Cameron 	if (c)
44346cba3f19SStephen M. Cameron 		rc2 = hpsa_send_abort(h, scsi3addr, abort, 1);
44356cba3f19SStephen M. Cameron 	return rc && rc2;
44366cba3f19SStephen M. Cameron }
44376cba3f19SStephen M. Cameron 
443875167d2cSStephen M. Cameron /* Send an abort for the specified command.
443975167d2cSStephen M. Cameron  *	If the device and controller support it,
444075167d2cSStephen M. Cameron  *		send a task abort request.
444175167d2cSStephen M. Cameron  */
444275167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
444375167d2cSStephen M. Cameron {
444475167d2cSStephen M. Cameron 
444575167d2cSStephen M. Cameron 	int i, rc;
444675167d2cSStephen M. Cameron 	struct ctlr_info *h;
444775167d2cSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
444875167d2cSStephen M. Cameron 	struct CommandList *abort; /* pointer to command to be aborted */
444975167d2cSStephen M. Cameron 	struct CommandList *found;
445075167d2cSStephen M. Cameron 	struct scsi_cmnd *as;	/* ptr to scsi cmd inside aborted command. */
445175167d2cSStephen M. Cameron 	char msg[256];		/* For debug messaging. */
445275167d2cSStephen M. Cameron 	int ml = 0;
44532b08b3e9SDon Brace 	__le32 tagupper, taglower;
445475167d2cSStephen M. Cameron 
445575167d2cSStephen M. Cameron 	/* Find the controller of the command to be aborted */
445675167d2cSStephen M. Cameron 	h = sdev_to_hba(sc->device);
445775167d2cSStephen M. Cameron 	if (WARN(h == NULL,
445875167d2cSStephen M. Cameron 			"ABORT REQUEST FAILED, Controller lookup failed.\n"))
445975167d2cSStephen M. Cameron 		return FAILED;
446075167d2cSStephen M. Cameron 
446175167d2cSStephen M. Cameron 	/* Check that controller supports some kind of task abort */
446275167d2cSStephen M. Cameron 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
446375167d2cSStephen M. Cameron 		!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
446475167d2cSStephen M. Cameron 		return FAILED;
446575167d2cSStephen M. Cameron 
446675167d2cSStephen M. Cameron 	memset(msg, 0, sizeof(msg));
44679cb78c16SHannes Reinecke 	ml += sprintf(msg+ml, "ABORT REQUEST on C%d:B%d:T%d:L%llu ",
446875167d2cSStephen M. Cameron 		h->scsi_host->host_no, sc->device->channel,
446975167d2cSStephen M. Cameron 		sc->device->id, sc->device->lun);
447075167d2cSStephen M. Cameron 
447175167d2cSStephen M. Cameron 	/* Find the device of the command to be aborted */
447275167d2cSStephen M. Cameron 	dev = sc->device->hostdata;
447375167d2cSStephen M. Cameron 	if (!dev) {
447475167d2cSStephen M. Cameron 		dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
447575167d2cSStephen M. Cameron 				msg);
447675167d2cSStephen M. Cameron 		return FAILED;
447775167d2cSStephen M. Cameron 	}
447875167d2cSStephen M. Cameron 
447975167d2cSStephen M. Cameron 	/* Get SCSI command to be aborted */
448075167d2cSStephen M. Cameron 	abort = (struct CommandList *) sc->host_scribble;
448175167d2cSStephen M. Cameron 	if (abort == NULL) {
448275167d2cSStephen M. Cameron 		dev_err(&h->pdev->dev, "%s FAILED, Command to abort is NULL.\n",
448375167d2cSStephen M. Cameron 				msg);
448475167d2cSStephen M. Cameron 		return FAILED;
448575167d2cSStephen M. Cameron 	}
448617eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
448717eb87d2SScott Teel 	ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
448875167d2cSStephen M. Cameron 	as  = (struct scsi_cmnd *) abort->scsi_cmd;
448975167d2cSStephen M. Cameron 	if (as != NULL)
449075167d2cSStephen M. Cameron 		ml += sprintf(msg+ml, "Command:0x%x SN:0x%lx ",
449175167d2cSStephen M. Cameron 			as->cmnd[0], as->serial_number);
449275167d2cSStephen M. Cameron 	dev_dbg(&h->pdev->dev, "%s\n", msg);
449375167d2cSStephen M. Cameron 	dev_warn(&h->pdev->dev, "Abort request on C%d:B%d:T%d:L%d\n",
449475167d2cSStephen M. Cameron 		h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
449575167d2cSStephen M. Cameron 
449675167d2cSStephen M. Cameron 	/* Search reqQ to See if command is queued but not submitted,
449775167d2cSStephen M. Cameron 	 * if so, complete the command with aborted status and remove
449875167d2cSStephen M. Cameron 	 * it from the reqQ.
449975167d2cSStephen M. Cameron 	 */
450075167d2cSStephen M. Cameron 	found = hpsa_find_cmd_in_queue(h, sc, &h->reqQ);
450175167d2cSStephen M. Cameron 	if (found) {
450275167d2cSStephen M. Cameron 		found->err_info->CommandStatus = CMD_ABORTED;
450375167d2cSStephen M. Cameron 		finish_cmd(found);
450475167d2cSStephen M. Cameron 		dev_info(&h->pdev->dev, "%s Request SUCCEEDED (driver queue).\n",
450575167d2cSStephen M. Cameron 				msg);
450675167d2cSStephen M. Cameron 		return SUCCESS;
450775167d2cSStephen M. Cameron 	}
450875167d2cSStephen M. Cameron 
450975167d2cSStephen M. Cameron 	/* not in reqQ, if also not in cmpQ, must have already completed */
451075167d2cSStephen M. Cameron 	found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
451175167d2cSStephen M. Cameron 	if (!found)  {
4512d6ebd0f7SStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s Request SUCCEEDED (not known to driver).\n",
451375167d2cSStephen M. Cameron 				msg);
451475167d2cSStephen M. Cameron 		return SUCCESS;
451575167d2cSStephen M. Cameron 	}
451675167d2cSStephen M. Cameron 
451775167d2cSStephen M. Cameron 	/*
451875167d2cSStephen M. Cameron 	 * Command is in flight, or possibly already completed
451975167d2cSStephen M. Cameron 	 * by the firmware (but not to the scsi mid layer) but we can't
452075167d2cSStephen M. Cameron 	 * distinguish which.  Send the abort down.
452175167d2cSStephen M. Cameron 	 */
45226cba3f19SStephen M. Cameron 	rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort);
452375167d2cSStephen M. Cameron 	if (rc != 0) {
452475167d2cSStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s Request FAILED.\n", msg);
452575167d2cSStephen M. Cameron 		dev_warn(&h->pdev->dev, "FAILED abort on device C%d:B%d:T%d:L%d\n",
452675167d2cSStephen M. Cameron 			h->scsi_host->host_no,
452775167d2cSStephen M. Cameron 			dev->bus, dev->target, dev->lun);
452875167d2cSStephen M. Cameron 		return FAILED;
452975167d2cSStephen M. Cameron 	}
453075167d2cSStephen M. Cameron 	dev_info(&h->pdev->dev, "%s REQUEST SUCCEEDED.\n", msg);
453175167d2cSStephen M. Cameron 
453275167d2cSStephen M. Cameron 	/* If the abort(s) above completed and actually aborted the
453375167d2cSStephen M. Cameron 	 * command, then the command to be aborted should already be
453475167d2cSStephen M. Cameron 	 * completed.  If not, wait around a bit more to see if they
453575167d2cSStephen M. Cameron 	 * manage to complete normally.
453675167d2cSStephen M. Cameron 	 */
453775167d2cSStephen M. Cameron #define ABORT_COMPLETE_WAIT_SECS 30
453875167d2cSStephen M. Cameron 	for (i = 0; i < ABORT_COMPLETE_WAIT_SECS * 10; i++) {
453975167d2cSStephen M. Cameron 		found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
454075167d2cSStephen M. Cameron 		if (!found)
454175167d2cSStephen M. Cameron 			return SUCCESS;
454275167d2cSStephen M. Cameron 		msleep(100);
454375167d2cSStephen M. Cameron 	}
454475167d2cSStephen M. Cameron 	dev_warn(&h->pdev->dev, "%s FAILED. Aborted command has not completed after %d seconds.\n",
454575167d2cSStephen M. Cameron 		msg, ABORT_COMPLETE_WAIT_SECS);
454675167d2cSStephen M. Cameron 	return FAILED;
454775167d2cSStephen M. Cameron }
454875167d2cSStephen M. Cameron 
454975167d2cSStephen M. Cameron 
4550edd16368SStephen M. Cameron /*
4551edd16368SStephen M. Cameron  * For operations that cannot sleep, a command block is allocated at init,
4552edd16368SStephen M. Cameron  * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
4553edd16368SStephen M. Cameron  * which ones are free or in use.  Lock must be held when calling this.
4554edd16368SStephen M. Cameron  * cmd_free() is the complement.
4555edd16368SStephen M. Cameron  */
4556edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h)
4557edd16368SStephen M. Cameron {
4558edd16368SStephen M. Cameron 	struct CommandList *c;
4559edd16368SStephen M. Cameron 	int i;
4560edd16368SStephen M. Cameron 	union u64bit temp64;
4561edd16368SStephen M. Cameron 	dma_addr_t cmd_dma_handle, err_dma_handle;
45624c413128SStephen M. Cameron 	int loopcount;
4563edd16368SStephen M. Cameron 
45644c413128SStephen M. Cameron 	/* There is some *extremely* small but non-zero chance that that
45654c413128SStephen M. Cameron 	 * multiple threads could get in here, and one thread could
45664c413128SStephen M. Cameron 	 * be scanning through the list of bits looking for a free
45674c413128SStephen M. Cameron 	 * one, but the free ones are always behind him, and other
45684c413128SStephen M. Cameron 	 * threads sneak in behind him and eat them before he can
45694c413128SStephen M. Cameron 	 * get to them, so that while there is always a free one, a
45704c413128SStephen M. Cameron 	 * very unlucky thread might be starved anyway, never able to
45714c413128SStephen M. Cameron 	 * beat the other threads.  In reality, this happens so
45724c413128SStephen M. Cameron 	 * infrequently as to be indistinguishable from never.
45734c413128SStephen M. Cameron 	 */
45744c413128SStephen M. Cameron 
45754c413128SStephen M. Cameron 	loopcount = 0;
4576edd16368SStephen M. Cameron 	do {
4577edd16368SStephen M. Cameron 		i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
45784c413128SStephen M. Cameron 		if (i == h->nr_cmds)
45794c413128SStephen M. Cameron 			i = 0;
45804c413128SStephen M. Cameron 		loopcount++;
45814c413128SStephen M. Cameron 	} while (test_and_set_bit(i & (BITS_PER_LONG - 1),
45824c413128SStephen M. Cameron 		  h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0 &&
45834c413128SStephen M. Cameron 		loopcount < 10);
45844c413128SStephen M. Cameron 
45854c413128SStephen M. Cameron 	/* Thread got starved?  We do not expect this to ever happen. */
45864c413128SStephen M. Cameron 	if (loopcount >= 10)
4587edd16368SStephen M. Cameron 		return NULL;
4588e16a33adSMatt Gates 
4589edd16368SStephen M. Cameron 	c = h->cmd_pool + i;
4590edd16368SStephen M. Cameron 	memset(c, 0, sizeof(*c));
4591edd16368SStephen M. Cameron 	cmd_dma_handle = h->cmd_pool_dhandle
4592edd16368SStephen M. Cameron 	    + i * sizeof(*c);
4593edd16368SStephen M. Cameron 	c->err_info = h->errinfo_pool + i;
4594edd16368SStephen M. Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
4595edd16368SStephen M. Cameron 	err_dma_handle = h->errinfo_pool_dhandle
4596edd16368SStephen M. Cameron 	    + i * sizeof(*c->err_info);
4597edd16368SStephen M. Cameron 
4598edd16368SStephen M. Cameron 	c->cmdindex = i;
4599edd16368SStephen M. Cameron 
46009e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&c->list);
460101a02ffcSStephen M. Cameron 	c->busaddr = (u32) cmd_dma_handle;
460201a02ffcSStephen M. Cameron 	temp64.val = (u64) err_dma_handle;
460350a0decfSStephen M. Cameron 	c->ErrDesc.Addr = cpu_to_le64(err_dma_handle);
460450a0decfSStephen M. Cameron 	c->ErrDesc.Len = cpu_to_le32(sizeof(*c->err_info));
4605edd16368SStephen M. Cameron 
4606edd16368SStephen M. Cameron 	c->h = h;
4607edd16368SStephen M. Cameron 	return c;
4608edd16368SStephen M. Cameron }
4609edd16368SStephen M. Cameron 
4610edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c)
4611edd16368SStephen M. Cameron {
4612edd16368SStephen M. Cameron 	int i;
4613edd16368SStephen M. Cameron 
4614edd16368SStephen M. Cameron 	i = c - h->cmd_pool;
4615edd16368SStephen M. Cameron 	clear_bit(i & (BITS_PER_LONG - 1),
4616edd16368SStephen M. Cameron 		  h->cmd_pool_bits + (i / BITS_PER_LONG));
4617edd16368SStephen M. Cameron }
4618edd16368SStephen M. Cameron 
4619edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
4620edd16368SStephen M. Cameron 
462142a91641SDon Brace static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd,
462242a91641SDon Brace 	void __user *arg)
4623edd16368SStephen M. Cameron {
4624edd16368SStephen M. Cameron 	IOCTL32_Command_struct __user *arg32 =
4625edd16368SStephen M. Cameron 	    (IOCTL32_Command_struct __user *) arg;
4626edd16368SStephen M. Cameron 	IOCTL_Command_struct arg64;
4627edd16368SStephen M. Cameron 	IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
4628edd16368SStephen M. Cameron 	int err;
4629edd16368SStephen M. Cameron 	u32 cp;
4630edd16368SStephen M. Cameron 
4631938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
4632edd16368SStephen M. Cameron 	err = 0;
4633edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
4634edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
4635edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
4636edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
4637edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
4638edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
4639edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
4640edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
4641edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
4642edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
4643edd16368SStephen M. Cameron 
4644edd16368SStephen M. Cameron 	if (err)
4645edd16368SStephen M. Cameron 		return -EFAULT;
4646edd16368SStephen M. Cameron 
464742a91641SDon Brace 	err = hpsa_ioctl(dev, CCISS_PASSTHRU, p);
4648edd16368SStephen M. Cameron 	if (err)
4649edd16368SStephen M. Cameron 		return err;
4650edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
4651edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
4652edd16368SStephen M. Cameron 	if (err)
4653edd16368SStephen M. Cameron 		return -EFAULT;
4654edd16368SStephen M. Cameron 	return err;
4655edd16368SStephen M. Cameron }
4656edd16368SStephen M. Cameron 
4657edd16368SStephen M. Cameron static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
465842a91641SDon Brace 	int cmd, void __user *arg)
4659edd16368SStephen M. Cameron {
4660edd16368SStephen M. Cameron 	BIG_IOCTL32_Command_struct __user *arg32 =
4661edd16368SStephen M. Cameron 	    (BIG_IOCTL32_Command_struct __user *) arg;
4662edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct arg64;
4663edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct __user *p =
4664edd16368SStephen M. Cameron 	    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(arg64.malloc_size, &arg32->malloc_size);
4678edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
4679edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
4680edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
4681edd16368SStephen M. Cameron 
4682edd16368SStephen M. Cameron 	if (err)
4683edd16368SStephen M. Cameron 		return -EFAULT;
4684edd16368SStephen M. Cameron 
468542a91641SDon Brace 	err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, p);
4686edd16368SStephen M. Cameron 	if (err)
4687edd16368SStephen M. Cameron 		return err;
4688edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
4689edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
4690edd16368SStephen M. Cameron 	if (err)
4691edd16368SStephen M. Cameron 		return -EFAULT;
4692edd16368SStephen M. Cameron 	return err;
4693edd16368SStephen M. Cameron }
469471fe75a7SStephen M. Cameron 
469542a91641SDon Brace static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
469671fe75a7SStephen M. Cameron {
469771fe75a7SStephen M. Cameron 	switch (cmd) {
469871fe75a7SStephen M. Cameron 	case CCISS_GETPCIINFO:
469971fe75a7SStephen M. Cameron 	case CCISS_GETINTINFO:
470071fe75a7SStephen M. Cameron 	case CCISS_SETINTINFO:
470171fe75a7SStephen M. Cameron 	case CCISS_GETNODENAME:
470271fe75a7SStephen M. Cameron 	case CCISS_SETNODENAME:
470371fe75a7SStephen M. Cameron 	case CCISS_GETHEARTBEAT:
470471fe75a7SStephen M. Cameron 	case CCISS_GETBUSTYPES:
470571fe75a7SStephen M. Cameron 	case CCISS_GETFIRMVER:
470671fe75a7SStephen M. Cameron 	case CCISS_GETDRIVVER:
470771fe75a7SStephen M. Cameron 	case CCISS_REVALIDVOLS:
470871fe75a7SStephen M. Cameron 	case CCISS_DEREGDISK:
470971fe75a7SStephen M. Cameron 	case CCISS_REGNEWDISK:
471071fe75a7SStephen M. Cameron 	case CCISS_REGNEWD:
471171fe75a7SStephen M. Cameron 	case CCISS_RESCANDISK:
471271fe75a7SStephen M. Cameron 	case CCISS_GETLUNINFO:
471371fe75a7SStephen M. Cameron 		return hpsa_ioctl(dev, cmd, arg);
471471fe75a7SStephen M. Cameron 
471571fe75a7SStephen M. Cameron 	case CCISS_PASSTHRU32:
471671fe75a7SStephen M. Cameron 		return hpsa_ioctl32_passthru(dev, cmd, arg);
471771fe75a7SStephen M. Cameron 	case CCISS_BIG_PASSTHRU32:
471871fe75a7SStephen M. Cameron 		return hpsa_ioctl32_big_passthru(dev, cmd, arg);
471971fe75a7SStephen M. Cameron 
472071fe75a7SStephen M. Cameron 	default:
472171fe75a7SStephen M. Cameron 		return -ENOIOCTLCMD;
472271fe75a7SStephen M. Cameron 	}
472371fe75a7SStephen M. Cameron }
4724edd16368SStephen M. Cameron #endif
4725edd16368SStephen M. Cameron 
4726edd16368SStephen M. Cameron static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
4727edd16368SStephen M. Cameron {
4728edd16368SStephen M. Cameron 	struct hpsa_pci_info pciinfo;
4729edd16368SStephen M. Cameron 
4730edd16368SStephen M. Cameron 	if (!argp)
4731edd16368SStephen M. Cameron 		return -EINVAL;
4732edd16368SStephen M. Cameron 	pciinfo.domain = pci_domain_nr(h->pdev->bus);
4733edd16368SStephen M. Cameron 	pciinfo.bus = h->pdev->bus->number;
4734edd16368SStephen M. Cameron 	pciinfo.dev_fn = h->pdev->devfn;
4735edd16368SStephen M. Cameron 	pciinfo.board_id = h->board_id;
4736edd16368SStephen M. Cameron 	if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
4737edd16368SStephen M. Cameron 		return -EFAULT;
4738edd16368SStephen M. Cameron 	return 0;
4739edd16368SStephen M. Cameron }
4740edd16368SStephen M. Cameron 
4741edd16368SStephen M. Cameron static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
4742edd16368SStephen M. Cameron {
4743edd16368SStephen M. Cameron 	DriverVer_type DriverVer;
4744edd16368SStephen M. Cameron 	unsigned char vmaj, vmin, vsubmin;
4745edd16368SStephen M. Cameron 	int rc;
4746edd16368SStephen M. Cameron 
4747edd16368SStephen M. Cameron 	rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
4748edd16368SStephen M. Cameron 		&vmaj, &vmin, &vsubmin);
4749edd16368SStephen M. Cameron 	if (rc != 3) {
4750edd16368SStephen M. Cameron 		dev_info(&h->pdev->dev, "driver version string '%s' "
4751edd16368SStephen M. Cameron 			"unrecognized.", HPSA_DRIVER_VERSION);
4752edd16368SStephen M. Cameron 		vmaj = 0;
4753edd16368SStephen M. Cameron 		vmin = 0;
4754edd16368SStephen M. Cameron 		vsubmin = 0;
4755edd16368SStephen M. Cameron 	}
4756edd16368SStephen M. Cameron 	DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
4757edd16368SStephen M. Cameron 	if (!argp)
4758edd16368SStephen M. Cameron 		return -EINVAL;
4759edd16368SStephen M. Cameron 	if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
4760edd16368SStephen M. Cameron 		return -EFAULT;
4761edd16368SStephen M. Cameron 	return 0;
4762edd16368SStephen M. Cameron }
4763edd16368SStephen M. Cameron 
4764edd16368SStephen M. Cameron static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
4765edd16368SStephen M. Cameron {
4766edd16368SStephen M. Cameron 	IOCTL_Command_struct iocommand;
4767edd16368SStephen M. Cameron 	struct CommandList *c;
4768edd16368SStephen M. Cameron 	char *buff = NULL;
476950a0decfSStephen M. Cameron 	u64 temp64;
4770c1f63c8fSStephen M. Cameron 	int rc = 0;
4771edd16368SStephen M. Cameron 
4772edd16368SStephen M. Cameron 	if (!argp)
4773edd16368SStephen M. Cameron 		return -EINVAL;
4774edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
4775edd16368SStephen M. Cameron 		return -EPERM;
4776edd16368SStephen M. Cameron 	if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
4777edd16368SStephen M. Cameron 		return -EFAULT;
4778edd16368SStephen M. Cameron 	if ((iocommand.buf_size < 1) &&
4779edd16368SStephen M. Cameron 	    (iocommand.Request.Type.Direction != XFER_NONE)) {
4780edd16368SStephen M. Cameron 		return -EINVAL;
4781edd16368SStephen M. Cameron 	}
4782edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
4783edd16368SStephen M. Cameron 		buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
4784edd16368SStephen M. Cameron 		if (buff == NULL)
4785edd16368SStephen M. Cameron 			return -EFAULT;
47869233fb10SStephen M. Cameron 		if (iocommand.Request.Type.Direction & XFER_WRITE) {
4787edd16368SStephen M. Cameron 			/* Copy the data into the buffer we created */
4788b03a7771SStephen M. Cameron 			if (copy_from_user(buff, iocommand.buf,
4789b03a7771SStephen M. Cameron 				iocommand.buf_size)) {
4790c1f63c8fSStephen M. Cameron 				rc = -EFAULT;
4791c1f63c8fSStephen M. Cameron 				goto out_kfree;
4792edd16368SStephen M. Cameron 			}
4793b03a7771SStephen M. Cameron 		} else {
4794edd16368SStephen M. Cameron 			memset(buff, 0, iocommand.buf_size);
4795b03a7771SStephen M. Cameron 		}
4796b03a7771SStephen M. Cameron 	}
4797*45fcb86eSStephen Cameron 	c = cmd_alloc(h);
4798edd16368SStephen M. Cameron 	if (c == NULL) {
4799c1f63c8fSStephen M. Cameron 		rc = -ENOMEM;
4800c1f63c8fSStephen M. Cameron 		goto out_kfree;
4801edd16368SStephen M. Cameron 	}
4802edd16368SStephen M. Cameron 	/* Fill in the command type */
4803edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
4804edd16368SStephen M. Cameron 	/* Fill in Command Header */
4805edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0; /* unused in simple mode */
4806edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {	/* buffer to fill */
4807edd16368SStephen M. Cameron 		c->Header.SGList = 1;
480850a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(1);
4809edd16368SStephen M. Cameron 	} else	{ /* no buffers to fill */
4810edd16368SStephen M. Cameron 		c->Header.SGList = 0;
481150a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(0);
4812edd16368SStephen M. Cameron 	}
4813edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
4814edd16368SStephen M. Cameron 	/* use the kernel address the cmd block for tag */
48152b08b3e9SDon Brace 	c->Header.tag = cpu_to_le64(c->busaddr);
4816edd16368SStephen M. Cameron 
4817edd16368SStephen M. Cameron 	/* Fill in Request block */
4818edd16368SStephen M. Cameron 	memcpy(&c->Request, &iocommand.Request,
4819edd16368SStephen M. Cameron 		sizeof(c->Request));
4820edd16368SStephen M. Cameron 
4821edd16368SStephen M. Cameron 	/* Fill in the scatter gather information */
4822edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
482350a0decfSStephen M. Cameron 		temp64 = pci_map_single(h->pdev, buff,
4824edd16368SStephen M. Cameron 			iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
482550a0decfSStephen M. Cameron 		if (dma_mapping_error(&h->pdev->dev, (dma_addr_t) temp64)) {
482650a0decfSStephen M. Cameron 			c->SG[0].Addr = cpu_to_le64(0);
482750a0decfSStephen M. Cameron 			c->SG[0].Len = cpu_to_le32(0);
4828bcc48ffaSStephen M. Cameron 			rc = -ENOMEM;
4829bcc48ffaSStephen M. Cameron 			goto out;
4830bcc48ffaSStephen M. Cameron 		}
483150a0decfSStephen M. Cameron 		c->SG[0].Addr = cpu_to_le64(temp64);
483250a0decfSStephen M. Cameron 		c->SG[0].Len = cpu_to_le32(iocommand.buf_size);
483350a0decfSStephen M. Cameron 		c->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* not chaining */
4834edd16368SStephen M. Cameron 	}
4835a0c12413SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
4836c2dd32e0SStephen M. Cameron 	if (iocommand.buf_size > 0)
4837edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
4838edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
4839edd16368SStephen M. Cameron 
4840edd16368SStephen M. Cameron 	/* Copy the error information out */
4841edd16368SStephen M. Cameron 	memcpy(&iocommand.error_info, c->err_info,
4842edd16368SStephen M. Cameron 		sizeof(iocommand.error_info));
4843edd16368SStephen M. Cameron 	if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
4844c1f63c8fSStephen M. Cameron 		rc = -EFAULT;
4845c1f63c8fSStephen M. Cameron 		goto out;
4846edd16368SStephen M. Cameron 	}
48479233fb10SStephen M. Cameron 	if ((iocommand.Request.Type.Direction & XFER_READ) &&
4848b03a7771SStephen M. Cameron 		iocommand.buf_size > 0) {
4849edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
4850edd16368SStephen M. Cameron 		if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
4851c1f63c8fSStephen M. Cameron 			rc = -EFAULT;
4852c1f63c8fSStephen M. Cameron 			goto out;
4853edd16368SStephen M. Cameron 		}
4854edd16368SStephen M. Cameron 	}
4855c1f63c8fSStephen M. Cameron out:
4856*45fcb86eSStephen Cameron 	cmd_free(h, c);
4857c1f63c8fSStephen M. Cameron out_kfree:
4858c1f63c8fSStephen M. Cameron 	kfree(buff);
4859c1f63c8fSStephen M. Cameron 	return rc;
4860edd16368SStephen M. Cameron }
4861edd16368SStephen M. Cameron 
4862edd16368SStephen M. Cameron static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
4863edd16368SStephen M. Cameron {
4864edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct *ioc;
4865edd16368SStephen M. Cameron 	struct CommandList *c;
4866edd16368SStephen M. Cameron 	unsigned char **buff = NULL;
4867edd16368SStephen M. Cameron 	int *buff_size = NULL;
486850a0decfSStephen M. Cameron 	u64 temp64;
4869edd16368SStephen M. Cameron 	BYTE sg_used = 0;
4870edd16368SStephen M. Cameron 	int status = 0;
487101a02ffcSStephen M. Cameron 	u32 left;
487201a02ffcSStephen M. Cameron 	u32 sz;
4873edd16368SStephen M. Cameron 	BYTE __user *data_ptr;
4874edd16368SStephen M. Cameron 
4875edd16368SStephen M. Cameron 	if (!argp)
4876edd16368SStephen M. Cameron 		return -EINVAL;
4877edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
4878edd16368SStephen M. Cameron 		return -EPERM;
4879edd16368SStephen M. Cameron 	ioc = (BIG_IOCTL_Command_struct *)
4880edd16368SStephen M. Cameron 	    kmalloc(sizeof(*ioc), GFP_KERNEL);
4881edd16368SStephen M. Cameron 	if (!ioc) {
4882edd16368SStephen M. Cameron 		status = -ENOMEM;
4883edd16368SStephen M. Cameron 		goto cleanup1;
4884edd16368SStephen M. Cameron 	}
4885edd16368SStephen M. Cameron 	if (copy_from_user(ioc, argp, sizeof(*ioc))) {
4886edd16368SStephen M. Cameron 		status = -EFAULT;
4887edd16368SStephen M. Cameron 		goto cleanup1;
4888edd16368SStephen M. Cameron 	}
4889edd16368SStephen M. Cameron 	if ((ioc->buf_size < 1) &&
4890edd16368SStephen M. Cameron 	    (ioc->Request.Type.Direction != XFER_NONE)) {
4891edd16368SStephen M. Cameron 		status = -EINVAL;
4892edd16368SStephen M. Cameron 		goto cleanup1;
4893edd16368SStephen M. Cameron 	}
4894edd16368SStephen M. Cameron 	/* Check kmalloc limits  using all SGs */
4895edd16368SStephen M. Cameron 	if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
4896edd16368SStephen M. Cameron 		status = -EINVAL;
4897edd16368SStephen M. Cameron 		goto cleanup1;
4898edd16368SStephen M. Cameron 	}
4899d66ae08bSStephen M. Cameron 	if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
4900edd16368SStephen M. Cameron 		status = -EINVAL;
4901edd16368SStephen M. Cameron 		goto cleanup1;
4902edd16368SStephen M. Cameron 	}
4903d66ae08bSStephen M. Cameron 	buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
4904edd16368SStephen M. Cameron 	if (!buff) {
4905edd16368SStephen M. Cameron 		status = -ENOMEM;
4906edd16368SStephen M. Cameron 		goto cleanup1;
4907edd16368SStephen M. Cameron 	}
4908d66ae08bSStephen M. Cameron 	buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
4909edd16368SStephen M. Cameron 	if (!buff_size) {
4910edd16368SStephen M. Cameron 		status = -ENOMEM;
4911edd16368SStephen M. Cameron 		goto cleanup1;
4912edd16368SStephen M. Cameron 	}
4913edd16368SStephen M. Cameron 	left = ioc->buf_size;
4914edd16368SStephen M. Cameron 	data_ptr = ioc->buf;
4915edd16368SStephen M. Cameron 	while (left) {
4916edd16368SStephen M. Cameron 		sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
4917edd16368SStephen M. Cameron 		buff_size[sg_used] = sz;
4918edd16368SStephen M. Cameron 		buff[sg_used] = kmalloc(sz, GFP_KERNEL);
4919edd16368SStephen M. Cameron 		if (buff[sg_used] == NULL) {
4920edd16368SStephen M. Cameron 			status = -ENOMEM;
4921edd16368SStephen M. Cameron 			goto cleanup1;
4922edd16368SStephen M. Cameron 		}
49239233fb10SStephen M. Cameron 		if (ioc->Request.Type.Direction & XFER_WRITE) {
4924edd16368SStephen M. Cameron 			if (copy_from_user(buff[sg_used], data_ptr, sz)) {
49250758f4f7SStephen M. Cameron 				status = -EFAULT;
4926edd16368SStephen M. Cameron 				goto cleanup1;
4927edd16368SStephen M. Cameron 			}
4928edd16368SStephen M. Cameron 		} else
4929edd16368SStephen M. Cameron 			memset(buff[sg_used], 0, sz);
4930edd16368SStephen M. Cameron 		left -= sz;
4931edd16368SStephen M. Cameron 		data_ptr += sz;
4932edd16368SStephen M. Cameron 		sg_used++;
4933edd16368SStephen M. Cameron 	}
4934*45fcb86eSStephen Cameron 	c = cmd_alloc(h);
4935edd16368SStephen M. Cameron 	if (c == NULL) {
4936edd16368SStephen M. Cameron 		status = -ENOMEM;
4937edd16368SStephen M. Cameron 		goto cleanup1;
4938edd16368SStephen M. Cameron 	}
4939edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
4940edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
494150a0decfSStephen M. Cameron 	c->Header.SGList = (u8) sg_used;
494250a0decfSStephen M. Cameron 	c->Header.SGTotal = cpu_to_le16(sg_used);
4943edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
49442b08b3e9SDon Brace 	c->Header.tag = cpu_to_le64(c->busaddr);
4945edd16368SStephen M. Cameron 	memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
4946edd16368SStephen M. Cameron 	if (ioc->buf_size > 0) {
4947edd16368SStephen M. Cameron 		int i;
4948edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
494950a0decfSStephen M. Cameron 			temp64 = pci_map_single(h->pdev, buff[i],
4950edd16368SStephen M. Cameron 				    buff_size[i], PCI_DMA_BIDIRECTIONAL);
495150a0decfSStephen M. Cameron 			if (dma_mapping_error(&h->pdev->dev,
495250a0decfSStephen M. Cameron 							(dma_addr_t) temp64)) {
495350a0decfSStephen M. Cameron 				c->SG[i].Addr = cpu_to_le64(0);
495450a0decfSStephen M. Cameron 				c->SG[i].Len = cpu_to_le32(0);
4955bcc48ffaSStephen M. Cameron 				hpsa_pci_unmap(h->pdev, c, i,
4956bcc48ffaSStephen M. Cameron 					PCI_DMA_BIDIRECTIONAL);
4957bcc48ffaSStephen M. Cameron 				status = -ENOMEM;
4958e2d4a1f6SStephen M. Cameron 				goto cleanup0;
4959bcc48ffaSStephen M. Cameron 			}
496050a0decfSStephen M. Cameron 			c->SG[i].Addr = cpu_to_le64(temp64);
496150a0decfSStephen M. Cameron 			c->SG[i].Len = cpu_to_le32(buff_size[i]);
496250a0decfSStephen M. Cameron 			c->SG[i].Ext = cpu_to_le32(0);
4963edd16368SStephen M. Cameron 		}
496450a0decfSStephen M. Cameron 		c->SG[--i].Ext = cpu_to_le32(HPSA_SG_LAST);
4965edd16368SStephen M. Cameron 	}
4966a0c12413SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
4967b03a7771SStephen M. Cameron 	if (sg_used)
4968edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
4969edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
4970edd16368SStephen M. Cameron 	/* Copy the error information out */
4971edd16368SStephen M. Cameron 	memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
4972edd16368SStephen M. Cameron 	if (copy_to_user(argp, ioc, sizeof(*ioc))) {
4973edd16368SStephen M. Cameron 		status = -EFAULT;
4974e2d4a1f6SStephen M. Cameron 		goto cleanup0;
4975edd16368SStephen M. Cameron 	}
49769233fb10SStephen M. Cameron 	if ((ioc->Request.Type.Direction & XFER_READ) && ioc->buf_size > 0) {
49772b08b3e9SDon Brace 		int i;
49782b08b3e9SDon Brace 
4979edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
4980edd16368SStephen M. Cameron 		BYTE __user *ptr = ioc->buf;
4981edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
4982edd16368SStephen M. Cameron 			if (copy_to_user(ptr, buff[i], buff_size[i])) {
4983edd16368SStephen M. Cameron 				status = -EFAULT;
4984e2d4a1f6SStephen M. Cameron 				goto cleanup0;
4985edd16368SStephen M. Cameron 			}
4986edd16368SStephen M. Cameron 			ptr += buff_size[i];
4987edd16368SStephen M. Cameron 		}
4988edd16368SStephen M. Cameron 	}
4989edd16368SStephen M. Cameron 	status = 0;
4990e2d4a1f6SStephen M. Cameron cleanup0:
4991*45fcb86eSStephen Cameron 	cmd_free(h, c);
4992edd16368SStephen M. Cameron cleanup1:
4993edd16368SStephen M. Cameron 	if (buff) {
49942b08b3e9SDon Brace 		int i;
49952b08b3e9SDon Brace 
4996edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++)
4997edd16368SStephen M. Cameron 			kfree(buff[i]);
4998edd16368SStephen M. Cameron 		kfree(buff);
4999edd16368SStephen M. Cameron 	}
5000edd16368SStephen M. Cameron 	kfree(buff_size);
5001edd16368SStephen M. Cameron 	kfree(ioc);
5002edd16368SStephen M. Cameron 	return status;
5003edd16368SStephen M. Cameron }
5004edd16368SStephen M. Cameron 
5005edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
5006edd16368SStephen M. Cameron 	struct CommandList *c)
5007edd16368SStephen M. Cameron {
5008edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
5009edd16368SStephen M. Cameron 			c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
5010edd16368SStephen M. Cameron 		(void) check_for_unit_attention(h, c);
5011edd16368SStephen M. Cameron }
50120390f0c0SStephen M. Cameron 
50130390f0c0SStephen M. Cameron static int increment_passthru_count(struct ctlr_info *h)
50140390f0c0SStephen M. Cameron {
50150390f0c0SStephen M. Cameron 	unsigned long flags;
50160390f0c0SStephen M. Cameron 
50170390f0c0SStephen M. Cameron 	spin_lock_irqsave(&h->passthru_count_lock, flags);
50180390f0c0SStephen M. Cameron 	if (h->passthru_count >= HPSA_MAX_CONCURRENT_PASSTHRUS) {
50190390f0c0SStephen M. Cameron 		spin_unlock_irqrestore(&h->passthru_count_lock, flags);
50200390f0c0SStephen M. Cameron 		return -1;
50210390f0c0SStephen M. Cameron 	}
50220390f0c0SStephen M. Cameron 	h->passthru_count++;
50230390f0c0SStephen M. Cameron 	spin_unlock_irqrestore(&h->passthru_count_lock, flags);
50240390f0c0SStephen M. Cameron 	return 0;
50250390f0c0SStephen M. Cameron }
50260390f0c0SStephen M. Cameron 
50270390f0c0SStephen M. Cameron static void decrement_passthru_count(struct ctlr_info *h)
50280390f0c0SStephen M. Cameron {
50290390f0c0SStephen M. Cameron 	unsigned long flags;
50300390f0c0SStephen M. Cameron 
50310390f0c0SStephen M. Cameron 	spin_lock_irqsave(&h->passthru_count_lock, flags);
50320390f0c0SStephen M. Cameron 	if (h->passthru_count <= 0) {
50330390f0c0SStephen M. Cameron 		spin_unlock_irqrestore(&h->passthru_count_lock, flags);
50340390f0c0SStephen M. Cameron 		/* not expecting to get here. */
50350390f0c0SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Bug detected, passthru_count seems to be incorrect.\n");
50360390f0c0SStephen M. Cameron 		return;
50370390f0c0SStephen M. Cameron 	}
50380390f0c0SStephen M. Cameron 	h->passthru_count--;
50390390f0c0SStephen M. Cameron 	spin_unlock_irqrestore(&h->passthru_count_lock, flags);
50400390f0c0SStephen M. Cameron }
50410390f0c0SStephen M. Cameron 
5042edd16368SStephen M. Cameron /*
5043edd16368SStephen M. Cameron  * ioctl
5044edd16368SStephen M. Cameron  */
504542a91641SDon Brace static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
5046edd16368SStephen M. Cameron {
5047edd16368SStephen M. Cameron 	struct ctlr_info *h;
5048edd16368SStephen M. Cameron 	void __user *argp = (void __user *)arg;
50490390f0c0SStephen M. Cameron 	int rc;
5050edd16368SStephen M. Cameron 
5051edd16368SStephen M. Cameron 	h = sdev_to_hba(dev);
5052edd16368SStephen M. Cameron 
5053edd16368SStephen M. Cameron 	switch (cmd) {
5054edd16368SStephen M. Cameron 	case CCISS_DEREGDISK:
5055edd16368SStephen M. Cameron 	case CCISS_REGNEWDISK:
5056edd16368SStephen M. Cameron 	case CCISS_REGNEWD:
5057a08a8471SStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
5058edd16368SStephen M. Cameron 		return 0;
5059edd16368SStephen M. Cameron 	case CCISS_GETPCIINFO:
5060edd16368SStephen M. Cameron 		return hpsa_getpciinfo_ioctl(h, argp);
5061edd16368SStephen M. Cameron 	case CCISS_GETDRIVVER:
5062edd16368SStephen M. Cameron 		return hpsa_getdrivver_ioctl(h, argp);
5063edd16368SStephen M. Cameron 	case CCISS_PASSTHRU:
50640390f0c0SStephen M. Cameron 		if (increment_passthru_count(h))
50650390f0c0SStephen M. Cameron 			return -EAGAIN;
50660390f0c0SStephen M. Cameron 		rc = hpsa_passthru_ioctl(h, argp);
50670390f0c0SStephen M. Cameron 		decrement_passthru_count(h);
50680390f0c0SStephen M. Cameron 		return rc;
5069edd16368SStephen M. Cameron 	case CCISS_BIG_PASSTHRU:
50700390f0c0SStephen M. Cameron 		if (increment_passthru_count(h))
50710390f0c0SStephen M. Cameron 			return -EAGAIN;
50720390f0c0SStephen M. Cameron 		rc = hpsa_big_passthru_ioctl(h, argp);
50730390f0c0SStephen M. Cameron 		decrement_passthru_count(h);
50740390f0c0SStephen M. Cameron 		return rc;
5075edd16368SStephen M. Cameron 	default:
5076edd16368SStephen M. Cameron 		return -ENOTTY;
5077edd16368SStephen M. Cameron 	}
5078edd16368SStephen M. Cameron }
5079edd16368SStephen M. Cameron 
50806f039790SGreg Kroah-Hartman static int hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
50816f039790SGreg Kroah-Hartman 				u8 reset_type)
508264670ac8SStephen M. Cameron {
508364670ac8SStephen M. Cameron 	struct CommandList *c;
508464670ac8SStephen M. Cameron 
508564670ac8SStephen M. Cameron 	c = cmd_alloc(h);
508664670ac8SStephen M. Cameron 	if (!c)
508764670ac8SStephen M. Cameron 		return -ENOMEM;
5088a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map */
5089a2dac136SStephen M. Cameron 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
509064670ac8SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_MSG);
509164670ac8SStephen M. Cameron 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
509264670ac8SStephen M. Cameron 	c->waiting = NULL;
509364670ac8SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
509464670ac8SStephen M. Cameron 	/* Don't wait for completion, the reset won't complete.  Don't free
509564670ac8SStephen M. Cameron 	 * the command either.  This is the last command we will send before
509664670ac8SStephen M. Cameron 	 * re-initializing everything, so it doesn't matter and won't leak.
509764670ac8SStephen M. Cameron 	 */
509864670ac8SStephen M. Cameron 	return 0;
509964670ac8SStephen M. Cameron }
510064670ac8SStephen M. Cameron 
5101a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
5102b7bb24ebSStephen M. Cameron 	void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
5103edd16368SStephen M. Cameron 	int cmd_type)
5104edd16368SStephen M. Cameron {
5105edd16368SStephen M. Cameron 	int pci_dir = XFER_NONE;
510675167d2cSStephen M. Cameron 	struct CommandList *a; /* for commands to be aborted */
5107edd16368SStephen M. Cameron 
5108edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5109edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
5110edd16368SStephen M. Cameron 	if (buff != NULL && size > 0) {
5111edd16368SStephen M. Cameron 		c->Header.SGList = 1;
511250a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(1);
5113edd16368SStephen M. Cameron 	} else {
5114edd16368SStephen M. Cameron 		c->Header.SGList = 0;
511550a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(0);
5116edd16368SStephen M. Cameron 	}
51172b08b3e9SDon Brace 	c->Header.tag = cpu_to_le64(c->busaddr);
5118edd16368SStephen M. Cameron 	memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
5119edd16368SStephen M. Cameron 
5120edd16368SStephen M. Cameron 	if (cmd_type == TYPE_CMD) {
5121edd16368SStephen M. Cameron 		switch (cmd) {
5122edd16368SStephen M. Cameron 		case HPSA_INQUIRY:
5123edd16368SStephen M. Cameron 			/* are we trying to read a vital product page */
5124b7bb24ebSStephen M. Cameron 			if (page_code & VPD_PAGE) {
5125edd16368SStephen M. Cameron 				c->Request.CDB[1] = 0x01;
5126b7bb24ebSStephen M. Cameron 				c->Request.CDB[2] = (page_code & 0xff);
5127edd16368SStephen M. Cameron 			}
5128edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
5129a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5130a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5131edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5132edd16368SStephen M. Cameron 			c->Request.CDB[0] = HPSA_INQUIRY;
5133edd16368SStephen M. Cameron 			c->Request.CDB[4] = size & 0xFF;
5134edd16368SStephen M. Cameron 			break;
5135edd16368SStephen M. Cameron 		case HPSA_REPORT_LOG:
5136edd16368SStephen M. Cameron 		case HPSA_REPORT_PHYS:
5137edd16368SStephen M. Cameron 			/* Talking to controller so It's a physical command
5138edd16368SStephen M. Cameron 			   mode = 00 target = 0.  Nothing to write.
5139edd16368SStephen M. Cameron 			 */
5140edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
5141a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5142a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5143edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5144edd16368SStephen M. Cameron 			c->Request.CDB[0] = cmd;
5145edd16368SStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
5146edd16368SStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5147edd16368SStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5148edd16368SStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
5149edd16368SStephen M. Cameron 			break;
5150edd16368SStephen M. Cameron 		case HPSA_CACHE_FLUSH:
5151edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
5152a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5153a505b86fSStephen M. Cameron 					TYPE_ATTR_DIR(cmd_type,
5154a505b86fSStephen M. Cameron 						ATTR_SIMPLE, XFER_WRITE);
5155edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5156edd16368SStephen M. Cameron 			c->Request.CDB[0] = BMIC_WRITE;
5157edd16368SStephen M. Cameron 			c->Request.CDB[6] = BMIC_CACHE_FLUSH;
5158bb158eabSStephen M. Cameron 			c->Request.CDB[7] = (size >> 8) & 0xFF;
5159bb158eabSStephen M. Cameron 			c->Request.CDB[8] = size & 0xFF;
5160edd16368SStephen M. Cameron 			break;
5161edd16368SStephen M. Cameron 		case TEST_UNIT_READY:
5162edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
5163a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5164a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
5165edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5166edd16368SStephen M. Cameron 			break;
5167283b4a9bSStephen M. Cameron 		case HPSA_GET_RAID_MAP:
5168283b4a9bSStephen M. Cameron 			c->Request.CDBLen = 12;
5169a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5170a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5171283b4a9bSStephen M. Cameron 			c->Request.Timeout = 0;
5172283b4a9bSStephen M. Cameron 			c->Request.CDB[0] = HPSA_CISS_READ;
5173283b4a9bSStephen M. Cameron 			c->Request.CDB[1] = cmd;
5174283b4a9bSStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
5175283b4a9bSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5176283b4a9bSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5177283b4a9bSStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
5178283b4a9bSStephen M. Cameron 			break;
5179316b221aSStephen M. Cameron 		case BMIC_SENSE_CONTROLLER_PARAMETERS:
5180316b221aSStephen M. Cameron 			c->Request.CDBLen = 10;
5181a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5182a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
5183316b221aSStephen M. Cameron 			c->Request.Timeout = 0;
5184316b221aSStephen M. Cameron 			c->Request.CDB[0] = BMIC_READ;
5185316b221aSStephen M. Cameron 			c->Request.CDB[6] = BMIC_SENSE_CONTROLLER_PARAMETERS;
5186316b221aSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5187316b221aSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5188316b221aSStephen M. Cameron 			break;
5189edd16368SStephen M. Cameron 		default:
5190edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
5191edd16368SStephen M. Cameron 			BUG();
5192a2dac136SStephen M. Cameron 			return -1;
5193edd16368SStephen M. Cameron 		}
5194edd16368SStephen M. Cameron 	} else if (cmd_type == TYPE_MSG) {
5195edd16368SStephen M. Cameron 		switch (cmd) {
5196edd16368SStephen M. Cameron 
5197edd16368SStephen M. Cameron 		case  HPSA_DEVICE_RESET_MSG:
5198edd16368SStephen M. Cameron 			c->Request.CDBLen = 16;
5199a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5200a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
5201edd16368SStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
520264670ac8SStephen M. Cameron 			memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
520364670ac8SStephen M. Cameron 			c->Request.CDB[0] =  cmd;
520421e89afdSStephen M. Cameron 			c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
5205edd16368SStephen M. Cameron 			/* If bytes 4-7 are zero, it means reset the */
5206edd16368SStephen M. Cameron 			/* LunID device */
5207edd16368SStephen M. Cameron 			c->Request.CDB[4] = 0x00;
5208edd16368SStephen M. Cameron 			c->Request.CDB[5] = 0x00;
5209edd16368SStephen M. Cameron 			c->Request.CDB[6] = 0x00;
5210edd16368SStephen M. Cameron 			c->Request.CDB[7] = 0x00;
5211edd16368SStephen M. Cameron 			break;
521275167d2cSStephen M. Cameron 		case  HPSA_ABORT_MSG:
521375167d2cSStephen M. Cameron 			a = buff;       /* point to command to be aborted */
52142b08b3e9SDon Brace 			dev_dbg(&h->pdev->dev,
52152b08b3e9SDon Brace 				"Abort Tag:0x%016llx request Tag:0x%016llx",
521650a0decfSStephen M. Cameron 				a->Header.tag, c->Header.tag);
521775167d2cSStephen M. Cameron 			c->Request.CDBLen = 16;
5218a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
5219a505b86fSStephen M. Cameron 					TYPE_ATTR_DIR(cmd_type,
5220a505b86fSStephen M. Cameron 						ATTR_SIMPLE, XFER_WRITE);
522175167d2cSStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
522275167d2cSStephen M. Cameron 			c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
522375167d2cSStephen M. Cameron 			c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
522475167d2cSStephen M. Cameron 			c->Request.CDB[2] = 0x00; /* reserved */
522575167d2cSStephen M. Cameron 			c->Request.CDB[3] = 0x00; /* reserved */
522675167d2cSStephen M. Cameron 			/* Tag to abort goes in CDB[4]-CDB[11] */
52272b08b3e9SDon Brace 			memcpy(&c->Request.CDB[4], &a->Header.tag,
52282b08b3e9SDon Brace 				sizeof(a->Header.tag));
522975167d2cSStephen M. Cameron 			c->Request.CDB[12] = 0x00; /* reserved */
523075167d2cSStephen M. Cameron 			c->Request.CDB[13] = 0x00; /* reserved */
523175167d2cSStephen M. Cameron 			c->Request.CDB[14] = 0x00; /* reserved */
523275167d2cSStephen M. Cameron 			c->Request.CDB[15] = 0x00; /* reserved */
523375167d2cSStephen M. Cameron 		break;
5234edd16368SStephen M. Cameron 		default:
5235edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown message type %d\n",
5236edd16368SStephen M. Cameron 				cmd);
5237edd16368SStephen M. Cameron 			BUG();
5238edd16368SStephen M. Cameron 		}
5239edd16368SStephen M. Cameron 	} else {
5240edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
5241edd16368SStephen M. Cameron 		BUG();
5242edd16368SStephen M. Cameron 	}
5243edd16368SStephen M. Cameron 
5244a505b86fSStephen M. Cameron 	switch (GET_DIR(c->Request.type_attr_dir)) {
5245edd16368SStephen M. Cameron 	case XFER_READ:
5246edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_FROMDEVICE;
5247edd16368SStephen M. Cameron 		break;
5248edd16368SStephen M. Cameron 	case XFER_WRITE:
5249edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_TODEVICE;
5250edd16368SStephen M. Cameron 		break;
5251edd16368SStephen M. Cameron 	case XFER_NONE:
5252edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_NONE;
5253edd16368SStephen M. Cameron 		break;
5254edd16368SStephen M. Cameron 	default:
5255edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_BIDIRECTIONAL;
5256edd16368SStephen M. Cameron 	}
5257a2dac136SStephen M. Cameron 	if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
5258a2dac136SStephen M. Cameron 		return -1;
5259a2dac136SStephen M. Cameron 	return 0;
5260edd16368SStephen M. Cameron }
5261edd16368SStephen M. Cameron 
5262edd16368SStephen M. Cameron /*
5263edd16368SStephen M. Cameron  * Map (physical) PCI mem into (virtual) kernel space
5264edd16368SStephen M. Cameron  */
5265edd16368SStephen M. Cameron static void __iomem *remap_pci_mem(ulong base, ulong size)
5266edd16368SStephen M. Cameron {
5267edd16368SStephen M. Cameron 	ulong page_base = ((ulong) base) & PAGE_MASK;
5268edd16368SStephen M. Cameron 	ulong page_offs = ((ulong) base) - page_base;
5269088ba34cSStephen M. Cameron 	void __iomem *page_remapped = ioremap_nocache(page_base,
5270088ba34cSStephen M. Cameron 		page_offs + size);
5271edd16368SStephen M. Cameron 
5272edd16368SStephen M. Cameron 	return page_remapped ? (page_remapped + page_offs) : NULL;
5273edd16368SStephen M. Cameron }
5274edd16368SStephen M. Cameron 
5275edd16368SStephen M. Cameron /* Takes cmds off the submission queue and sends them to the hardware,
5276edd16368SStephen M. Cameron  * then puts them on the queue of cmds waiting for completion.
52770b57075dSStephen M. Cameron  * Assumes h->lock is held
5278edd16368SStephen M. Cameron  */
52790b57075dSStephen M. Cameron static void start_io(struct ctlr_info *h, unsigned long *flags)
5280edd16368SStephen M. Cameron {
5281edd16368SStephen M. Cameron 	struct CommandList *c;
5282edd16368SStephen M. Cameron 
52839e0fc764SStephen M. Cameron 	while (!list_empty(&h->reqQ)) {
52849e0fc764SStephen M. Cameron 		c = list_entry(h->reqQ.next, struct CommandList, list);
5285edd16368SStephen M. Cameron 		/* can't do anything if fifo is full */
5286edd16368SStephen M. Cameron 		if ((h->access.fifo_full(h))) {
5287396883e2SStephen M. Cameron 			h->fifo_recently_full = 1;
5288edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "fifo full\n");
5289edd16368SStephen M. Cameron 			break;
5290edd16368SStephen M. Cameron 		}
5291396883e2SStephen M. Cameron 		h->fifo_recently_full = 0;
5292edd16368SStephen M. Cameron 
5293edd16368SStephen M. Cameron 		/* Get the first entry from the Request Q */
5294edd16368SStephen M. Cameron 		removeQ(c);
5295edd16368SStephen M. Cameron 		h->Qdepth--;
5296edd16368SStephen M. Cameron 
5297edd16368SStephen M. Cameron 		/* Put job onto the completed Q */
5298edd16368SStephen M. Cameron 		addQ(&h->cmpQ, c);
52990cbf768eSStephen M. Cameron 		atomic_inc(&h->commands_outstanding);
53000b57075dSStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, *flags);
53010cbf768eSStephen M. Cameron 		/* Tell the controller execute command */
5302e16a33adSMatt Gates 		h->access.submit_command(h, c);
53030b57075dSStephen M. Cameron 		spin_lock_irqsave(&h->lock, *flags);
5304edd16368SStephen M. Cameron 	}
53050b57075dSStephen M. Cameron }
53060b57075dSStephen M. Cameron 
53070b57075dSStephen M. Cameron static void lock_and_start_io(struct ctlr_info *h)
53080b57075dSStephen M. Cameron {
53090b57075dSStephen M. Cameron 	unsigned long flags;
53100b57075dSStephen M. Cameron 
53110b57075dSStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
53120b57075dSStephen M. Cameron 	start_io(h, &flags);
5313e16a33adSMatt Gates 	spin_unlock_irqrestore(&h->lock, flags);
5314edd16368SStephen M. Cameron }
5315edd16368SStephen M. Cameron 
5316254f796bSMatt Gates static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
5317edd16368SStephen M. Cameron {
5318254f796bSMatt Gates 	return h->access.command_completed(h, q);
5319edd16368SStephen M. Cameron }
5320edd16368SStephen M. Cameron 
5321900c5440SStephen M. Cameron static inline bool interrupt_pending(struct ctlr_info *h)
5322edd16368SStephen M. Cameron {
5323edd16368SStephen M. Cameron 	return h->access.intr_pending(h);
5324edd16368SStephen M. Cameron }
5325edd16368SStephen M. Cameron 
5326edd16368SStephen M. Cameron static inline long interrupt_not_for_us(struct ctlr_info *h)
5327edd16368SStephen M. Cameron {
532810f66018SStephen M. Cameron 	return (h->access.intr_pending(h) == 0) ||
532910f66018SStephen M. Cameron 		(h->interrupts_enabled == 0);
5330edd16368SStephen M. Cameron }
5331edd16368SStephen M. Cameron 
533201a02ffcSStephen M. Cameron static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
533301a02ffcSStephen M. Cameron 	u32 raw_tag)
5334edd16368SStephen M. Cameron {
5335edd16368SStephen M. Cameron 	if (unlikely(tag_index >= h->nr_cmds)) {
5336edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
5337edd16368SStephen M. Cameron 		return 1;
5338edd16368SStephen M. Cameron 	}
5339edd16368SStephen M. Cameron 	return 0;
5340edd16368SStephen M. Cameron }
5341edd16368SStephen M. Cameron 
53425a3d16f5SStephen M. Cameron static inline void finish_cmd(struct CommandList *c)
5343edd16368SStephen M. Cameron {
5344e16a33adSMatt Gates 	unsigned long flags;
5345396883e2SStephen M. Cameron 	int io_may_be_stalled = 0;
5346396883e2SStephen M. Cameron 	struct ctlr_info *h = c->h;
53470cbf768eSStephen M. Cameron 	int count;
5348e16a33adSMatt Gates 
5349396883e2SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
5350edd16368SStephen M. Cameron 	removeQ(c);
5351396883e2SStephen M. Cameron 
5352396883e2SStephen M. Cameron 	/*
5353396883e2SStephen M. Cameron 	 * Check for possibly stalled i/o.
5354396883e2SStephen M. Cameron 	 *
5355396883e2SStephen M. Cameron 	 * If a fifo_full condition is encountered, requests will back up
5356396883e2SStephen M. Cameron 	 * in h->reqQ.  This queue is only emptied out by start_io which is
5357396883e2SStephen M. Cameron 	 * only called when a new i/o request comes in.  If no i/o's are
5358396883e2SStephen M. Cameron 	 * forthcoming, the i/o's in h->reqQ can get stuck.  So we call
5359396883e2SStephen M. Cameron 	 * start_io from here if we detect such a danger.
5360396883e2SStephen M. Cameron 	 *
5361396883e2SStephen M. Cameron 	 * Normally, we shouldn't hit this case, but pounding on the
5362396883e2SStephen M. Cameron 	 * CCISS_PASSTHRU ioctl can provoke it.  Only call start_io if
5363396883e2SStephen M. Cameron 	 * commands_outstanding is low.  We want to avoid calling
5364396883e2SStephen M. Cameron 	 * start_io from in here as much as possible, and esp. don't
5365396883e2SStephen M. Cameron 	 * want to get in a cycle where we call start_io every time
5366396883e2SStephen M. Cameron 	 * through here.
5367396883e2SStephen M. Cameron 	 */
53680cbf768eSStephen M. Cameron 	count = atomic_read(&h->commands_outstanding);
5369396883e2SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
53700cbf768eSStephen M. Cameron 	if (unlikely(h->fifo_recently_full) && count < 5)
53710cbf768eSStephen M. Cameron 		io_may_be_stalled = 1;
5372396883e2SStephen M. Cameron 
5373e85c5974SStephen M. Cameron 	dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
5374c349775eSScott Teel 	if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
5375c349775eSScott Teel 			|| c->cmd_type == CMD_IOACCEL2))
53761fb011fbSStephen M. Cameron 		complete_scsi_command(c);
5377edd16368SStephen M. Cameron 	else if (c->cmd_type == CMD_IOCTL_PEND)
5378edd16368SStephen M. Cameron 		complete(c->waiting);
5379396883e2SStephen M. Cameron 	if (unlikely(io_may_be_stalled))
53800b57075dSStephen M. Cameron 		lock_and_start_io(h);
5381edd16368SStephen M. Cameron }
5382edd16368SStephen M. Cameron 
5383a104c99fSStephen M. Cameron static inline u32 hpsa_tag_contains_index(u32 tag)
5384a104c99fSStephen M. Cameron {
5385a104c99fSStephen M. Cameron 	return tag & DIRECT_LOOKUP_BIT;
5386a104c99fSStephen M. Cameron }
5387a104c99fSStephen M. Cameron 
5388a104c99fSStephen M. Cameron static inline u32 hpsa_tag_to_index(u32 tag)
5389a104c99fSStephen M. Cameron {
5390a104c99fSStephen M. Cameron 	return tag >> DIRECT_LOOKUP_SHIFT;
5391a104c99fSStephen M. Cameron }
5392a104c99fSStephen M. Cameron 
5393a9a3a273SStephen M. Cameron 
5394a9a3a273SStephen M. Cameron static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
5395a104c99fSStephen M. Cameron {
5396a9a3a273SStephen M. Cameron #define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
5397a9a3a273SStephen M. Cameron #define HPSA_SIMPLE_ERROR_BITS 0x03
5398960a30e7SStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
5399a9a3a273SStephen M. Cameron 		return tag & ~HPSA_SIMPLE_ERROR_BITS;
5400a9a3a273SStephen M. Cameron 	return tag & ~HPSA_PERF_ERROR_BITS;
5401a104c99fSStephen M. Cameron }
5402a104c99fSStephen M. Cameron 
5403303932fdSDon Brace /* process completion of an indexed ("direct lookup") command */
54041d94f94dSStephen M. Cameron static inline void process_indexed_cmd(struct ctlr_info *h,
5405303932fdSDon Brace 	u32 raw_tag)
5406303932fdSDon Brace {
5407303932fdSDon Brace 	u32 tag_index;
5408303932fdSDon Brace 	struct CommandList *c;
5409303932fdSDon Brace 
5410303932fdSDon Brace 	tag_index = hpsa_tag_to_index(raw_tag);
54111d94f94dSStephen M. Cameron 	if (!bad_tag(h, tag_index, raw_tag)) {
5412303932fdSDon Brace 		c = h->cmd_pool + tag_index;
54135a3d16f5SStephen M. Cameron 		finish_cmd(c);
54141d94f94dSStephen M. Cameron 	}
5415303932fdSDon Brace }
5416303932fdSDon Brace 
5417303932fdSDon Brace /* process completion of a non-indexed command */
54181d94f94dSStephen M. Cameron static inline void process_nonindexed_cmd(struct ctlr_info *h,
5419303932fdSDon Brace 	u32 raw_tag)
5420303932fdSDon Brace {
5421303932fdSDon Brace 	u32 tag;
5422303932fdSDon Brace 	struct CommandList *c = NULL;
5423e16a33adSMatt Gates 	unsigned long flags;
5424303932fdSDon Brace 
5425a9a3a273SStephen M. Cameron 	tag = hpsa_tag_discard_error_bits(h, raw_tag);
5426e16a33adSMatt Gates 	spin_lock_irqsave(&h->lock, flags);
54279e0fc764SStephen M. Cameron 	list_for_each_entry(c, &h->cmpQ, list) {
5428303932fdSDon Brace 		if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
5429e16a33adSMatt Gates 			spin_unlock_irqrestore(&h->lock, flags);
54305a3d16f5SStephen M. Cameron 			finish_cmd(c);
54311d94f94dSStephen M. Cameron 			return;
5432303932fdSDon Brace 		}
5433303932fdSDon Brace 	}
5434e16a33adSMatt Gates 	spin_unlock_irqrestore(&h->lock, flags);
5435303932fdSDon Brace 	bad_tag(h, h->nr_cmds + 1, raw_tag);
5436303932fdSDon Brace }
5437303932fdSDon Brace 
543864670ac8SStephen M. Cameron /* Some controllers, like p400, will give us one interrupt
543964670ac8SStephen M. Cameron  * after a soft reset, even if we turned interrupts off.
544064670ac8SStephen M. Cameron  * Only need to check for this in the hpsa_xxx_discard_completions
544164670ac8SStephen M. Cameron  * functions.
544264670ac8SStephen M. Cameron  */
544364670ac8SStephen M. Cameron static int ignore_bogus_interrupt(struct ctlr_info *h)
544464670ac8SStephen M. Cameron {
544564670ac8SStephen M. Cameron 	if (likely(!reset_devices))
544664670ac8SStephen M. Cameron 		return 0;
544764670ac8SStephen M. Cameron 
544864670ac8SStephen M. Cameron 	if (likely(h->interrupts_enabled))
544964670ac8SStephen M. Cameron 		return 0;
545064670ac8SStephen M. Cameron 
545164670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
545264670ac8SStephen M. Cameron 		"(known firmware bug.)  Ignoring.\n");
545364670ac8SStephen M. Cameron 
545464670ac8SStephen M. Cameron 	return 1;
545564670ac8SStephen M. Cameron }
545664670ac8SStephen M. Cameron 
5457254f796bSMatt Gates /*
5458254f796bSMatt Gates  * Convert &h->q[x] (passed to interrupt handlers) back to h.
5459254f796bSMatt Gates  * Relies on (h-q[x] == x) being true for x such that
5460254f796bSMatt Gates  * 0 <= x < MAX_REPLY_QUEUES.
5461254f796bSMatt Gates  */
5462254f796bSMatt Gates static struct ctlr_info *queue_to_hba(u8 *queue)
546364670ac8SStephen M. Cameron {
5464254f796bSMatt Gates 	return container_of((queue - *queue), struct ctlr_info, q[0]);
5465254f796bSMatt Gates }
5466254f796bSMatt Gates 
5467254f796bSMatt Gates static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
5468254f796bSMatt Gates {
5469254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
5470254f796bSMatt Gates 	u8 q = *(u8 *) queue;
547164670ac8SStephen M. Cameron 	u32 raw_tag;
547264670ac8SStephen M. Cameron 
547364670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
547464670ac8SStephen M. Cameron 		return IRQ_NONE;
547564670ac8SStephen M. Cameron 
547664670ac8SStephen M. Cameron 	if (interrupt_not_for_us(h))
547764670ac8SStephen M. Cameron 		return IRQ_NONE;
5478a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
547964670ac8SStephen M. Cameron 	while (interrupt_pending(h)) {
5480254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
548164670ac8SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY)
5482254f796bSMatt Gates 			raw_tag = next_command(h, q);
548364670ac8SStephen M. Cameron 	}
548464670ac8SStephen M. Cameron 	return IRQ_HANDLED;
548564670ac8SStephen M. Cameron }
548664670ac8SStephen M. Cameron 
5487254f796bSMatt Gates static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
548864670ac8SStephen M. Cameron {
5489254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
549064670ac8SStephen M. Cameron 	u32 raw_tag;
5491254f796bSMatt Gates 	u8 q = *(u8 *) queue;
549264670ac8SStephen M. Cameron 
549364670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
549464670ac8SStephen M. Cameron 		return IRQ_NONE;
549564670ac8SStephen M. Cameron 
5496a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
5497254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
549864670ac8SStephen M. Cameron 	while (raw_tag != FIFO_EMPTY)
5499254f796bSMatt Gates 		raw_tag = next_command(h, q);
550064670ac8SStephen M. Cameron 	return IRQ_HANDLED;
550164670ac8SStephen M. Cameron }
550264670ac8SStephen M. Cameron 
5503254f796bSMatt Gates static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
5504edd16368SStephen M. Cameron {
5505254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba((u8 *) queue);
5506303932fdSDon Brace 	u32 raw_tag;
5507254f796bSMatt Gates 	u8 q = *(u8 *) queue;
5508edd16368SStephen M. Cameron 
5509edd16368SStephen M. Cameron 	if (interrupt_not_for_us(h))
5510edd16368SStephen M. Cameron 		return IRQ_NONE;
5511a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
551210f66018SStephen M. Cameron 	while (interrupt_pending(h)) {
5513254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
551410f66018SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY) {
55151d94f94dSStephen M. Cameron 			if (likely(hpsa_tag_contains_index(raw_tag)))
55161d94f94dSStephen M. Cameron 				process_indexed_cmd(h, raw_tag);
551710f66018SStephen M. Cameron 			else
55181d94f94dSStephen M. Cameron 				process_nonindexed_cmd(h, raw_tag);
5519254f796bSMatt Gates 			raw_tag = next_command(h, q);
552010f66018SStephen M. Cameron 		}
552110f66018SStephen M. Cameron 	}
552210f66018SStephen M. Cameron 	return IRQ_HANDLED;
552310f66018SStephen M. Cameron }
552410f66018SStephen M. Cameron 
5525254f796bSMatt Gates static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
552610f66018SStephen M. Cameron {
5527254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
552810f66018SStephen M. Cameron 	u32 raw_tag;
5529254f796bSMatt Gates 	u8 q = *(u8 *) queue;
553010f66018SStephen M. Cameron 
5531a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
5532254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
5533303932fdSDon Brace 	while (raw_tag != FIFO_EMPTY) {
55341d94f94dSStephen M. Cameron 		if (likely(hpsa_tag_contains_index(raw_tag)))
55351d94f94dSStephen M. Cameron 			process_indexed_cmd(h, raw_tag);
5536303932fdSDon Brace 		else
55371d94f94dSStephen M. Cameron 			process_nonindexed_cmd(h, raw_tag);
5538254f796bSMatt Gates 		raw_tag = next_command(h, q);
5539edd16368SStephen M. Cameron 	}
5540edd16368SStephen M. Cameron 	return IRQ_HANDLED;
5541edd16368SStephen M. Cameron }
5542edd16368SStephen M. Cameron 
5543a9a3a273SStephen M. Cameron /* Send a message CDB to the firmware. Careful, this only works
5544a9a3a273SStephen M. Cameron  * in simple mode, not performant mode due to the tag lookup.
5545a9a3a273SStephen M. Cameron  * We only ever use this immediately after a controller reset.
5546a9a3a273SStephen M. Cameron  */
55476f039790SGreg Kroah-Hartman static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
5548edd16368SStephen M. Cameron 			unsigned char type)
5549edd16368SStephen M. Cameron {
5550edd16368SStephen M. Cameron 	struct Command {
5551edd16368SStephen M. Cameron 		struct CommandListHeader CommandHeader;
5552edd16368SStephen M. Cameron 		struct RequestBlock Request;
5553edd16368SStephen M. Cameron 		struct ErrDescriptor ErrorDescriptor;
5554edd16368SStephen M. Cameron 	};
5555edd16368SStephen M. Cameron 	struct Command *cmd;
5556edd16368SStephen M. Cameron 	static const size_t cmd_sz = sizeof(*cmd) +
5557edd16368SStephen M. Cameron 					sizeof(cmd->ErrorDescriptor);
5558edd16368SStephen M. Cameron 	dma_addr_t paddr64;
55592b08b3e9SDon Brace 	__le32 paddr32;
55602b08b3e9SDon Brace 	u32 tag;
5561edd16368SStephen M. Cameron 	void __iomem *vaddr;
5562edd16368SStephen M. Cameron 	int i, err;
5563edd16368SStephen M. Cameron 
5564edd16368SStephen M. Cameron 	vaddr = pci_ioremap_bar(pdev, 0);
5565edd16368SStephen M. Cameron 	if (vaddr == NULL)
5566edd16368SStephen M. Cameron 		return -ENOMEM;
5567edd16368SStephen M. Cameron 
5568edd16368SStephen M. Cameron 	/* The Inbound Post Queue only accepts 32-bit physical addresses for the
5569edd16368SStephen M. Cameron 	 * CCISS commands, so they must be allocated from the lower 4GiB of
5570edd16368SStephen M. Cameron 	 * memory.
5571edd16368SStephen M. Cameron 	 */
5572edd16368SStephen M. Cameron 	err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
5573edd16368SStephen M. Cameron 	if (err) {
5574edd16368SStephen M. Cameron 		iounmap(vaddr);
55751eaec8f3SRobert Elliott 		return err;
5576edd16368SStephen M. Cameron 	}
5577edd16368SStephen M. Cameron 
5578edd16368SStephen M. Cameron 	cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
5579edd16368SStephen M. Cameron 	if (cmd == NULL) {
5580edd16368SStephen M. Cameron 		iounmap(vaddr);
5581edd16368SStephen M. Cameron 		return -ENOMEM;
5582edd16368SStephen M. Cameron 	}
5583edd16368SStephen M. Cameron 
5584edd16368SStephen M. Cameron 	/* This must fit, because of the 32-bit consistent DMA mask.  Also,
5585edd16368SStephen M. Cameron 	 * although there's no guarantee, we assume that the address is at
5586edd16368SStephen M. Cameron 	 * least 4-byte aligned (most likely, it's page-aligned).
5587edd16368SStephen M. Cameron 	 */
55882b08b3e9SDon Brace 	paddr32 = cpu_to_le32(paddr64);
5589edd16368SStephen M. Cameron 
5590edd16368SStephen M. Cameron 	cmd->CommandHeader.ReplyQueue = 0;
5591edd16368SStephen M. Cameron 	cmd->CommandHeader.SGList = 0;
559250a0decfSStephen M. Cameron 	cmd->CommandHeader.SGTotal = cpu_to_le16(0);
55932b08b3e9SDon Brace 	cmd->CommandHeader.tag = cpu_to_le64(paddr64);
5594edd16368SStephen M. Cameron 	memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
5595edd16368SStephen M. Cameron 
5596edd16368SStephen M. Cameron 	cmd->Request.CDBLen = 16;
5597a505b86fSStephen M. Cameron 	cmd->Request.type_attr_dir =
5598a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_MSG, ATTR_HEADOFQUEUE, XFER_NONE);
5599edd16368SStephen M. Cameron 	cmd->Request.Timeout = 0; /* Don't time out */
5600edd16368SStephen M. Cameron 	cmd->Request.CDB[0] = opcode;
5601edd16368SStephen M. Cameron 	cmd->Request.CDB[1] = type;
5602edd16368SStephen M. Cameron 	memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
560350a0decfSStephen M. Cameron 	cmd->ErrorDescriptor.Addr =
56042b08b3e9SDon Brace 			cpu_to_le64((le32_to_cpu(paddr32) + sizeof(*cmd)));
560550a0decfSStephen M. Cameron 	cmd->ErrorDescriptor.Len = cpu_to_le32(sizeof(struct ErrorInfo));
5606edd16368SStephen M. Cameron 
56072b08b3e9SDon Brace 	writel(le32_to_cpu(paddr32), vaddr + SA5_REQUEST_PORT_OFFSET);
5608edd16368SStephen M. Cameron 
5609edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
5610edd16368SStephen M. Cameron 		tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
56112b08b3e9SDon Brace 		if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr64)
5612edd16368SStephen M. Cameron 			break;
5613edd16368SStephen M. Cameron 		msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
5614edd16368SStephen M. Cameron 	}
5615edd16368SStephen M. Cameron 
5616edd16368SStephen M. Cameron 	iounmap(vaddr);
5617edd16368SStephen M. Cameron 
5618edd16368SStephen M. Cameron 	/* we leak the DMA buffer here ... no choice since the controller could
5619edd16368SStephen M. Cameron 	 *  still complete the command.
5620edd16368SStephen M. Cameron 	 */
5621edd16368SStephen M. Cameron 	if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
5622edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
5623edd16368SStephen M. Cameron 			opcode, type);
5624edd16368SStephen M. Cameron 		return -ETIMEDOUT;
5625edd16368SStephen M. Cameron 	}
5626edd16368SStephen M. Cameron 
5627edd16368SStephen M. Cameron 	pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
5628edd16368SStephen M. Cameron 
5629edd16368SStephen M. Cameron 	if (tag & HPSA_ERROR_BIT) {
5630edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
5631edd16368SStephen M. Cameron 			opcode, type);
5632edd16368SStephen M. Cameron 		return -EIO;
5633edd16368SStephen M. Cameron 	}
5634edd16368SStephen M. Cameron 
5635edd16368SStephen M. Cameron 	dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
5636edd16368SStephen M. Cameron 		opcode, type);
5637edd16368SStephen M. Cameron 	return 0;
5638edd16368SStephen M. Cameron }
5639edd16368SStephen M. Cameron 
5640edd16368SStephen M. Cameron #define hpsa_noop(p) hpsa_message(p, 3, 0)
5641edd16368SStephen M. Cameron 
56421df8552aSStephen M. Cameron static int hpsa_controller_hard_reset(struct pci_dev *pdev,
564342a91641SDon Brace 	void __iomem *vaddr, u32 use_doorbell)
5644edd16368SStephen M. Cameron {
5645edd16368SStephen M. Cameron 
56461df8552aSStephen M. Cameron 	if (use_doorbell) {
56471df8552aSStephen M. Cameron 		/* For everything after the P600, the PCI power state method
56481df8552aSStephen M. Cameron 		 * of resetting the controller doesn't work, so we have this
56491df8552aSStephen M. Cameron 		 * other way using the doorbell register.
5650edd16368SStephen M. Cameron 		 */
56511df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using doorbell to reset controller\n");
5652cf0b08d0SStephen M. Cameron 		writel(use_doorbell, vaddr + SA5_DOORBELL);
565385009239SStephen M. Cameron 
565400701a96SJustin Lindley 		/* PMC hardware guys tell us we need a 10 second delay after
565585009239SStephen M. Cameron 		 * doorbell reset and before any attempt to talk to the board
565685009239SStephen M. Cameron 		 * at all to ensure that this actually works and doesn't fall
565785009239SStephen M. Cameron 		 * over in some weird corner cases.
565885009239SStephen M. Cameron 		 */
565900701a96SJustin Lindley 		msleep(10000);
56601df8552aSStephen M. Cameron 	} else { /* Try to do it the PCI power state way */
5661edd16368SStephen M. Cameron 
5662edd16368SStephen M. Cameron 		/* Quoting from the Open CISS Specification: "The Power
5663edd16368SStephen M. Cameron 		 * Management Control/Status Register (CSR) controls the power
5664edd16368SStephen M. Cameron 		 * state of the device.  The normal operating state is D0,
5665edd16368SStephen M. Cameron 		 * CSR=00h.  The software off state is D3, CSR=03h.  To reset
56661df8552aSStephen M. Cameron 		 * the controller, place the interface device in D3 then to D0,
56671df8552aSStephen M. Cameron 		 * this causes a secondary PCI reset which will reset the
56681df8552aSStephen M. Cameron 		 * controller." */
5669edd16368SStephen M. Cameron 
56702662cab8SDon Brace 		int rc = 0;
56712662cab8SDon Brace 
56721df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using PCI PM to reset controller\n");
56732662cab8SDon Brace 
5674edd16368SStephen M. Cameron 		/* enter the D3hot power management state */
56752662cab8SDon Brace 		rc = pci_set_power_state(pdev, PCI_D3hot);
56762662cab8SDon Brace 		if (rc)
56772662cab8SDon Brace 			return rc;
5678edd16368SStephen M. Cameron 
5679edd16368SStephen M. Cameron 		msleep(500);
5680edd16368SStephen M. Cameron 
5681edd16368SStephen M. Cameron 		/* enter the D0 power management state */
56822662cab8SDon Brace 		rc = pci_set_power_state(pdev, PCI_D0);
56832662cab8SDon Brace 		if (rc)
56842662cab8SDon Brace 			return rc;
5685c4853efeSMike Miller 
5686c4853efeSMike Miller 		/*
5687c4853efeSMike Miller 		 * The P600 requires a small delay when changing states.
5688c4853efeSMike Miller 		 * Otherwise we may think the board did not reset and we bail.
5689c4853efeSMike Miller 		 * This for kdump only and is particular to the P600.
5690c4853efeSMike Miller 		 */
5691c4853efeSMike Miller 		msleep(500);
56921df8552aSStephen M. Cameron 	}
56931df8552aSStephen M. Cameron 	return 0;
56941df8552aSStephen M. Cameron }
56951df8552aSStephen M. Cameron 
56966f039790SGreg Kroah-Hartman static void init_driver_version(char *driver_version, int len)
5697580ada3cSStephen M. Cameron {
5698580ada3cSStephen M. Cameron 	memset(driver_version, 0, len);
5699f79cfec6SStephen M. Cameron 	strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
5700580ada3cSStephen M. Cameron }
5701580ada3cSStephen M. Cameron 
57026f039790SGreg Kroah-Hartman static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
5703580ada3cSStephen M. Cameron {
5704580ada3cSStephen M. Cameron 	char *driver_version;
5705580ada3cSStephen M. Cameron 	int i, size = sizeof(cfgtable->driver_version);
5706580ada3cSStephen M. Cameron 
5707580ada3cSStephen M. Cameron 	driver_version = kmalloc(size, GFP_KERNEL);
5708580ada3cSStephen M. Cameron 	if (!driver_version)
5709580ada3cSStephen M. Cameron 		return -ENOMEM;
5710580ada3cSStephen M. Cameron 
5711580ada3cSStephen M. Cameron 	init_driver_version(driver_version, size);
5712580ada3cSStephen M. Cameron 	for (i = 0; i < size; i++)
5713580ada3cSStephen M. Cameron 		writeb(driver_version[i], &cfgtable->driver_version[i]);
5714580ada3cSStephen M. Cameron 	kfree(driver_version);
5715580ada3cSStephen M. Cameron 	return 0;
5716580ada3cSStephen M. Cameron }
5717580ada3cSStephen M. Cameron 
57186f039790SGreg Kroah-Hartman static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
57196f039790SGreg Kroah-Hartman 					  unsigned char *driver_ver)
5720580ada3cSStephen M. Cameron {
5721580ada3cSStephen M. Cameron 	int i;
5722580ada3cSStephen M. Cameron 
5723580ada3cSStephen M. Cameron 	for (i = 0; i < sizeof(cfgtable->driver_version); i++)
5724580ada3cSStephen M. Cameron 		driver_ver[i] = readb(&cfgtable->driver_version[i]);
5725580ada3cSStephen M. Cameron }
5726580ada3cSStephen M. Cameron 
57276f039790SGreg Kroah-Hartman static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
5728580ada3cSStephen M. Cameron {
5729580ada3cSStephen M. Cameron 
5730580ada3cSStephen M. Cameron 	char *driver_ver, *old_driver_ver;
5731580ada3cSStephen M. Cameron 	int rc, size = sizeof(cfgtable->driver_version);
5732580ada3cSStephen M. Cameron 
5733580ada3cSStephen M. Cameron 	old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
5734580ada3cSStephen M. Cameron 	if (!old_driver_ver)
5735580ada3cSStephen M. Cameron 		return -ENOMEM;
5736580ada3cSStephen M. Cameron 	driver_ver = old_driver_ver + size;
5737580ada3cSStephen M. Cameron 
5738580ada3cSStephen M. Cameron 	/* After a reset, the 32 bytes of "driver version" in the cfgtable
5739580ada3cSStephen M. Cameron 	 * should have been changed, otherwise we know the reset failed.
5740580ada3cSStephen M. Cameron 	 */
5741580ada3cSStephen M. Cameron 	init_driver_version(old_driver_ver, size);
5742580ada3cSStephen M. Cameron 	read_driver_ver_from_cfgtable(cfgtable, driver_ver);
5743580ada3cSStephen M. Cameron 	rc = !memcmp(driver_ver, old_driver_ver, size);
5744580ada3cSStephen M. Cameron 	kfree(old_driver_ver);
5745580ada3cSStephen M. Cameron 	return rc;
5746580ada3cSStephen M. Cameron }
57471df8552aSStephen M. Cameron /* This does a hard reset of the controller using PCI power management
57481df8552aSStephen M. Cameron  * states or the using the doorbell register.
57491df8552aSStephen M. Cameron  */
57506f039790SGreg Kroah-Hartman static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev)
57511df8552aSStephen M. Cameron {
57521df8552aSStephen M. Cameron 	u64 cfg_offset;
57531df8552aSStephen M. Cameron 	u32 cfg_base_addr;
57541df8552aSStephen M. Cameron 	u64 cfg_base_addr_index;
57551df8552aSStephen M. Cameron 	void __iomem *vaddr;
57561df8552aSStephen M. Cameron 	unsigned long paddr;
5757580ada3cSStephen M. Cameron 	u32 misc_fw_support;
5758270d05deSStephen M. Cameron 	int rc;
57591df8552aSStephen M. Cameron 	struct CfgTable __iomem *cfgtable;
5760cf0b08d0SStephen M. Cameron 	u32 use_doorbell;
576118867659SStephen M. Cameron 	u32 board_id;
5762270d05deSStephen M. Cameron 	u16 command_register;
57631df8552aSStephen M. Cameron 
57641df8552aSStephen M. Cameron 	/* For controllers as old as the P600, this is very nearly
57651df8552aSStephen M. Cameron 	 * the same thing as
57661df8552aSStephen M. Cameron 	 *
57671df8552aSStephen M. Cameron 	 * pci_save_state(pci_dev);
57681df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D3hot);
57691df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D0);
57701df8552aSStephen M. Cameron 	 * pci_restore_state(pci_dev);
57711df8552aSStephen M. Cameron 	 *
57721df8552aSStephen M. Cameron 	 * For controllers newer than the P600, the pci power state
57731df8552aSStephen M. Cameron 	 * method of resetting doesn't work so we have another way
57741df8552aSStephen M. Cameron 	 * using the doorbell register.
57751df8552aSStephen M. Cameron 	 */
577618867659SStephen M. Cameron 
577725c1e56aSStephen M. Cameron 	rc = hpsa_lookup_board_id(pdev, &board_id);
577860f923b9SRobert Elliott 	if (rc < 0) {
577960f923b9SRobert Elliott 		dev_warn(&pdev->dev, "Board ID not found\n");
578060f923b9SRobert Elliott 		return rc;
578160f923b9SRobert Elliott 	}
578260f923b9SRobert Elliott 	if (!ctlr_is_resettable(board_id)) {
578360f923b9SRobert Elliott 		dev_warn(&pdev->dev, "Controller not resettable\n");
578425c1e56aSStephen M. Cameron 		return -ENODEV;
578525c1e56aSStephen M. Cameron 	}
578646380786SStephen M. Cameron 
578746380786SStephen M. Cameron 	/* if controller is soft- but not hard resettable... */
578846380786SStephen M. Cameron 	if (!ctlr_is_hard_resettable(board_id))
578946380786SStephen M. Cameron 		return -ENOTSUPP; /* try soft reset later. */
579018867659SStephen M. Cameron 
5791270d05deSStephen M. Cameron 	/* Save the PCI command register */
5792270d05deSStephen M. Cameron 	pci_read_config_word(pdev, 4, &command_register);
5793270d05deSStephen M. Cameron 	pci_save_state(pdev);
57941df8552aSStephen M. Cameron 
57951df8552aSStephen M. Cameron 	/* find the first memory BAR, so we can find the cfg table */
57961df8552aSStephen M. Cameron 	rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
57971df8552aSStephen M. Cameron 	if (rc)
57981df8552aSStephen M. Cameron 		return rc;
57991df8552aSStephen M. Cameron 	vaddr = remap_pci_mem(paddr, 0x250);
58001df8552aSStephen M. Cameron 	if (!vaddr)
58011df8552aSStephen M. Cameron 		return -ENOMEM;
58021df8552aSStephen M. Cameron 
58031df8552aSStephen M. Cameron 	/* find cfgtable in order to check if reset via doorbell is supported */
58041df8552aSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
58051df8552aSStephen M. Cameron 					&cfg_base_addr_index, &cfg_offset);
58061df8552aSStephen M. Cameron 	if (rc)
58071df8552aSStephen M. Cameron 		goto unmap_vaddr;
58081df8552aSStephen M. Cameron 	cfgtable = remap_pci_mem(pci_resource_start(pdev,
58091df8552aSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
58101df8552aSStephen M. Cameron 	if (!cfgtable) {
58111df8552aSStephen M. Cameron 		rc = -ENOMEM;
58121df8552aSStephen M. Cameron 		goto unmap_vaddr;
58131df8552aSStephen M. Cameron 	}
5814580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(cfgtable);
5815580ada3cSStephen M. Cameron 	if (rc)
581603741d95STomas Henzl 		goto unmap_cfgtable;
58171df8552aSStephen M. Cameron 
5818cf0b08d0SStephen M. Cameron 	/* If reset via doorbell register is supported, use that.
5819cf0b08d0SStephen M. Cameron 	 * There are two such methods.  Favor the newest method.
5820cf0b08d0SStephen M. Cameron 	 */
58211df8552aSStephen M. Cameron 	misc_fw_support = readl(&cfgtable->misc_fw_support);
5822cf0b08d0SStephen M. Cameron 	use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
5823cf0b08d0SStephen M. Cameron 	if (use_doorbell) {
5824cf0b08d0SStephen M. Cameron 		use_doorbell = DOORBELL_CTLR_RESET2;
5825cf0b08d0SStephen M. Cameron 	} else {
58261df8552aSStephen M. Cameron 		use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
5827cf0b08d0SStephen M. Cameron 		if (use_doorbell) {
5828050f7147SStephen Cameron 			dev_warn(&pdev->dev,
5829050f7147SStephen Cameron 				"Soft reset not supported. Firmware update is required.\n");
583064670ac8SStephen M. Cameron 			rc = -ENOTSUPP; /* try soft reset */
5831cf0b08d0SStephen M. Cameron 			goto unmap_cfgtable;
5832cf0b08d0SStephen M. Cameron 		}
5833cf0b08d0SStephen M. Cameron 	}
58341df8552aSStephen M. Cameron 
58351df8552aSStephen M. Cameron 	rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
58361df8552aSStephen M. Cameron 	if (rc)
58371df8552aSStephen M. Cameron 		goto unmap_cfgtable;
5838edd16368SStephen M. Cameron 
5839270d05deSStephen M. Cameron 	pci_restore_state(pdev);
5840270d05deSStephen M. Cameron 	pci_write_config_word(pdev, 4, command_register);
5841edd16368SStephen M. Cameron 
58421df8552aSStephen M. Cameron 	/* Some devices (notably the HP Smart Array 5i Controller)
58431df8552aSStephen M. Cameron 	   need a little pause here */
58441df8552aSStephen M. Cameron 	msleep(HPSA_POST_RESET_PAUSE_MSECS);
58451df8552aSStephen M. Cameron 
5846fe5389c8SStephen M. Cameron 	rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
5847fe5389c8SStephen M. Cameron 	if (rc) {
5848fe5389c8SStephen M. Cameron 		dev_warn(&pdev->dev,
5849050f7147SStephen Cameron 			"Failed waiting for board to become ready after hard reset\n");
5850fe5389c8SStephen M. Cameron 		goto unmap_cfgtable;
5851fe5389c8SStephen M. Cameron 	}
5852fe5389c8SStephen M. Cameron 
5853580ada3cSStephen M. Cameron 	rc = controller_reset_failed(vaddr);
5854580ada3cSStephen M. Cameron 	if (rc < 0)
5855580ada3cSStephen M. Cameron 		goto unmap_cfgtable;
5856580ada3cSStephen M. Cameron 	if (rc) {
585764670ac8SStephen M. Cameron 		dev_warn(&pdev->dev, "Unable to successfully reset "
585864670ac8SStephen M. Cameron 			"controller. Will try soft reset.\n");
585964670ac8SStephen M. Cameron 		rc = -ENOTSUPP;
5860580ada3cSStephen M. Cameron 	} else {
586164670ac8SStephen M. Cameron 		dev_info(&pdev->dev, "board ready after hard reset.\n");
58621df8552aSStephen M. Cameron 	}
58631df8552aSStephen M. Cameron 
58641df8552aSStephen M. Cameron unmap_cfgtable:
58651df8552aSStephen M. Cameron 	iounmap(cfgtable);
58661df8552aSStephen M. Cameron 
58671df8552aSStephen M. Cameron unmap_vaddr:
58681df8552aSStephen M. Cameron 	iounmap(vaddr);
58691df8552aSStephen M. Cameron 	return rc;
5870edd16368SStephen M. Cameron }
5871edd16368SStephen M. Cameron 
5872edd16368SStephen M. Cameron /*
5873edd16368SStephen M. Cameron  *  We cannot read the structure directly, for portability we must use
5874edd16368SStephen M. Cameron  *   the io functions.
5875edd16368SStephen M. Cameron  *   This is for debug only.
5876edd16368SStephen M. Cameron  */
587742a91641SDon Brace static void print_cfg_table(struct device *dev, struct CfgTable __iomem *tb)
5878edd16368SStephen M. Cameron {
587958f8665cSStephen M. Cameron #ifdef HPSA_DEBUG
5880edd16368SStephen M. Cameron 	int i;
5881edd16368SStephen M. Cameron 	char temp_name[17];
5882edd16368SStephen M. Cameron 
5883edd16368SStephen M. Cameron 	dev_info(dev, "Controller Configuration information\n");
5884edd16368SStephen M. Cameron 	dev_info(dev, "------------------------------------\n");
5885edd16368SStephen M. Cameron 	for (i = 0; i < 4; i++)
5886edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->Signature[i]));
5887edd16368SStephen M. Cameron 	temp_name[4] = '\0';
5888edd16368SStephen M. Cameron 	dev_info(dev, "   Signature = %s\n", temp_name);
5889edd16368SStephen M. Cameron 	dev_info(dev, "   Spec Number = %d\n", readl(&(tb->SpecValence)));
5890edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods supported = 0x%x\n",
5891edd16368SStephen M. Cameron 	       readl(&(tb->TransportSupport)));
5892edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods active = 0x%x\n",
5893edd16368SStephen M. Cameron 	       readl(&(tb->TransportActive)));
5894edd16368SStephen M. Cameron 	dev_info(dev, "   Requested transport Method = 0x%x\n",
5895edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.TransportRequest)));
5896edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Delay = 0x%x\n",
5897edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntDelay)));
5898edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Count = 0x%x\n",
5899edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntCount)));
590069d6e33dSRobert Elliott 	dev_info(dev, "   Max outstanding commands = %d\n",
5901edd16368SStephen M. Cameron 	       readl(&(tb->CmdsOutMax)));
5902edd16368SStephen M. Cameron 	dev_info(dev, "   Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
5903edd16368SStephen M. Cameron 	for (i = 0; i < 16; i++)
5904edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->ServerName[i]));
5905edd16368SStephen M. Cameron 	temp_name[16] = '\0';
5906edd16368SStephen M. Cameron 	dev_info(dev, "   Server Name = %s\n", temp_name);
5907edd16368SStephen M. Cameron 	dev_info(dev, "   Heartbeat Counter = 0x%x\n\n\n",
5908edd16368SStephen M. Cameron 		readl(&(tb->HeartBeat)));
5909edd16368SStephen M. Cameron #endif				/* HPSA_DEBUG */
591058f8665cSStephen M. Cameron }
5911edd16368SStephen M. Cameron 
5912edd16368SStephen M. Cameron static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
5913edd16368SStephen M. Cameron {
5914edd16368SStephen M. Cameron 	int i, offset, mem_type, bar_type;
5915edd16368SStephen M. Cameron 
5916edd16368SStephen M. Cameron 	if (pci_bar_addr == PCI_BASE_ADDRESS_0)	/* looking for BAR zero? */
5917edd16368SStephen M. Cameron 		return 0;
5918edd16368SStephen M. Cameron 	offset = 0;
5919edd16368SStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
5920edd16368SStephen M. Cameron 		bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
5921edd16368SStephen M. Cameron 		if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
5922edd16368SStephen M. Cameron 			offset += 4;
5923edd16368SStephen M. Cameron 		else {
5924edd16368SStephen M. Cameron 			mem_type = pci_resource_flags(pdev, i) &
5925edd16368SStephen M. Cameron 			    PCI_BASE_ADDRESS_MEM_TYPE_MASK;
5926edd16368SStephen M. Cameron 			switch (mem_type) {
5927edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_32:
5928edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_1M:
5929edd16368SStephen M. Cameron 				offset += 4;	/* 32 bit */
5930edd16368SStephen M. Cameron 				break;
5931edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_64:
5932edd16368SStephen M. Cameron 				offset += 8;
5933edd16368SStephen M. Cameron 				break;
5934edd16368SStephen M. Cameron 			default:	/* reserved in PCI 2.2 */
5935edd16368SStephen M. Cameron 				dev_warn(&pdev->dev,
5936edd16368SStephen M. Cameron 				       "base address is invalid\n");
5937edd16368SStephen M. Cameron 				return -1;
5938edd16368SStephen M. Cameron 				break;
5939edd16368SStephen M. Cameron 			}
5940edd16368SStephen M. Cameron 		}
5941edd16368SStephen M. Cameron 		if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
5942edd16368SStephen M. Cameron 			return i + 1;
5943edd16368SStephen M. Cameron 	}
5944edd16368SStephen M. Cameron 	return -1;
5945edd16368SStephen M. Cameron }
5946edd16368SStephen M. Cameron 
5947edd16368SStephen M. Cameron /* If MSI/MSI-X is supported by the kernel we will try to enable it on
5948050f7147SStephen Cameron  * controllers that are capable. If not, we use legacy INTx mode.
5949edd16368SStephen M. Cameron  */
5950edd16368SStephen M. Cameron 
59516f039790SGreg Kroah-Hartman static void hpsa_interrupt_mode(struct ctlr_info *h)
5952edd16368SStephen M. Cameron {
5953edd16368SStephen M. Cameron #ifdef CONFIG_PCI_MSI
5954254f796bSMatt Gates 	int err, i;
5955254f796bSMatt Gates 	struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
5956254f796bSMatt Gates 
5957254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++) {
5958254f796bSMatt Gates 		hpsa_msix_entries[i].vector = 0;
5959254f796bSMatt Gates 		hpsa_msix_entries[i].entry = i;
5960254f796bSMatt Gates 	}
5961edd16368SStephen M. Cameron 
5962edd16368SStephen M. Cameron 	/* Some boards advertise MSI but don't really support it */
59636b3f4c52SStephen M. Cameron 	if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
59646b3f4c52SStephen M. Cameron 	    (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
5965edd16368SStephen M. Cameron 		goto default_int_mode;
596655c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
5967050f7147SStephen Cameron 		dev_info(&h->pdev->dev, "MSI-X capable controller\n");
5968eee0f03aSHannes Reinecke 		h->msix_vector = MAX_REPLY_QUEUES;
5969f89439bcSStephen M. Cameron 		if (h->msix_vector > num_online_cpus())
5970f89439bcSStephen M. Cameron 			h->msix_vector = num_online_cpus();
597118fce3c4SAlexander Gordeev 		err = pci_enable_msix_range(h->pdev, hpsa_msix_entries,
597218fce3c4SAlexander Gordeev 					    1, h->msix_vector);
597318fce3c4SAlexander Gordeev 		if (err < 0) {
597418fce3c4SAlexander Gordeev 			dev_warn(&h->pdev->dev, "MSI-X init failed %d\n", err);
597518fce3c4SAlexander Gordeev 			h->msix_vector = 0;
597618fce3c4SAlexander Gordeev 			goto single_msi_mode;
597718fce3c4SAlexander Gordeev 		} else if (err < h->msix_vector) {
597855c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
5979edd16368SStephen M. Cameron 			       "available\n", err);
5980eee0f03aSHannes Reinecke 		}
598118fce3c4SAlexander Gordeev 		h->msix_vector = err;
5982eee0f03aSHannes Reinecke 		for (i = 0; i < h->msix_vector; i++)
5983eee0f03aSHannes Reinecke 			h->intr[i] = hpsa_msix_entries[i].vector;
5984eee0f03aSHannes Reinecke 		return;
5985edd16368SStephen M. Cameron 	}
598618fce3c4SAlexander Gordeev single_msi_mode:
598755c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
5988050f7147SStephen Cameron 		dev_info(&h->pdev->dev, "MSI capable controller\n");
598955c06c71SStephen M. Cameron 		if (!pci_enable_msi(h->pdev))
5990edd16368SStephen M. Cameron 			h->msi_vector = 1;
5991edd16368SStephen M. Cameron 		else
599255c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "MSI init failed\n");
5993edd16368SStephen M. Cameron 	}
5994edd16368SStephen M. Cameron default_int_mode:
5995edd16368SStephen M. Cameron #endif				/* CONFIG_PCI_MSI */
5996edd16368SStephen M. Cameron 	/* if we get here we're going to use the default interrupt mode */
5997a9a3a273SStephen M. Cameron 	h->intr[h->intr_mode] = h->pdev->irq;
5998edd16368SStephen M. Cameron }
5999edd16368SStephen M. Cameron 
60006f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
6001e5c880d1SStephen M. Cameron {
6002e5c880d1SStephen M. Cameron 	int i;
6003e5c880d1SStephen M. Cameron 	u32 subsystem_vendor_id, subsystem_device_id;
6004e5c880d1SStephen M. Cameron 
6005e5c880d1SStephen M. Cameron 	subsystem_vendor_id = pdev->subsystem_vendor;
6006e5c880d1SStephen M. Cameron 	subsystem_device_id = pdev->subsystem_device;
6007e5c880d1SStephen M. Cameron 	*board_id = ((subsystem_device_id << 16) & 0xffff0000) |
6008e5c880d1SStephen M. Cameron 		    subsystem_vendor_id;
6009e5c880d1SStephen M. Cameron 
6010e5c880d1SStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(products); i++)
6011e5c880d1SStephen M. Cameron 		if (*board_id == products[i].board_id)
6012e5c880d1SStephen M. Cameron 			return i;
6013e5c880d1SStephen M. Cameron 
60146798cc0aSStephen M. Cameron 	if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
60156798cc0aSStephen M. Cameron 		subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
60166798cc0aSStephen M. Cameron 		!hpsa_allow_any) {
6017e5c880d1SStephen M. Cameron 		dev_warn(&pdev->dev, "unrecognized board ID: "
6018e5c880d1SStephen M. Cameron 			"0x%08x, ignoring.\n", *board_id);
6019e5c880d1SStephen M. Cameron 			return -ENODEV;
6020e5c880d1SStephen M. Cameron 	}
6021e5c880d1SStephen M. Cameron 	return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
6022e5c880d1SStephen M. Cameron }
6023e5c880d1SStephen M. Cameron 
60246f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
60253a7774ceSStephen M. Cameron 				    unsigned long *memory_bar)
60263a7774ceSStephen M. Cameron {
60273a7774ceSStephen M. Cameron 	int i;
60283a7774ceSStephen M. Cameron 
60293a7774ceSStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
603012d2cd47SStephen M. Cameron 		if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
60313a7774ceSStephen M. Cameron 			/* addressing mode bits already removed */
603212d2cd47SStephen M. Cameron 			*memory_bar = pci_resource_start(pdev, i);
603312d2cd47SStephen M. Cameron 			dev_dbg(&pdev->dev, "memory BAR = %lx\n",
60343a7774ceSStephen M. Cameron 				*memory_bar);
60353a7774ceSStephen M. Cameron 			return 0;
60363a7774ceSStephen M. Cameron 		}
603712d2cd47SStephen M. Cameron 	dev_warn(&pdev->dev, "no memory BAR found\n");
60383a7774ceSStephen M. Cameron 	return -ENODEV;
60393a7774ceSStephen M. Cameron }
60403a7774ceSStephen M. Cameron 
60416f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
60426f039790SGreg Kroah-Hartman 				     int wait_for_ready)
60432c4c8c8bSStephen M. Cameron {
6044fe5389c8SStephen M. Cameron 	int i, iterations;
60452c4c8c8bSStephen M. Cameron 	u32 scratchpad;
6046fe5389c8SStephen M. Cameron 	if (wait_for_ready)
6047fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_READY_ITERATIONS;
6048fe5389c8SStephen M. Cameron 	else
6049fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
60502c4c8c8bSStephen M. Cameron 
6051fe5389c8SStephen M. Cameron 	for (i = 0; i < iterations; i++) {
6052fe5389c8SStephen M. Cameron 		scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
6053fe5389c8SStephen M. Cameron 		if (wait_for_ready) {
60542c4c8c8bSStephen M. Cameron 			if (scratchpad == HPSA_FIRMWARE_READY)
60552c4c8c8bSStephen M. Cameron 				return 0;
6056fe5389c8SStephen M. Cameron 		} else {
6057fe5389c8SStephen M. Cameron 			if (scratchpad != HPSA_FIRMWARE_READY)
6058fe5389c8SStephen M. Cameron 				return 0;
6059fe5389c8SStephen M. Cameron 		}
60602c4c8c8bSStephen M. Cameron 		msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
60612c4c8c8bSStephen M. Cameron 	}
6062fe5389c8SStephen M. Cameron 	dev_warn(&pdev->dev, "board not ready, timed out.\n");
60632c4c8c8bSStephen M. Cameron 	return -ENODEV;
60642c4c8c8bSStephen M. Cameron }
60652c4c8c8bSStephen M. Cameron 
60666f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
60676f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
6068a51fd47fSStephen M. Cameron 			       u64 *cfg_offset)
6069a51fd47fSStephen M. Cameron {
6070a51fd47fSStephen M. Cameron 	*cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
6071a51fd47fSStephen M. Cameron 	*cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
6072a51fd47fSStephen M. Cameron 	*cfg_base_addr &= (u32) 0x0000ffff;
6073a51fd47fSStephen M. Cameron 	*cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
6074a51fd47fSStephen M. Cameron 	if (*cfg_base_addr_index == -1) {
6075a51fd47fSStephen M. Cameron 		dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
6076a51fd47fSStephen M. Cameron 		return -ENODEV;
6077a51fd47fSStephen M. Cameron 	}
6078a51fd47fSStephen M. Cameron 	return 0;
6079a51fd47fSStephen M. Cameron }
6080a51fd47fSStephen M. Cameron 
60816f039790SGreg Kroah-Hartman static int hpsa_find_cfgtables(struct ctlr_info *h)
6082edd16368SStephen M. Cameron {
608301a02ffcSStephen M. Cameron 	u64 cfg_offset;
608401a02ffcSStephen M. Cameron 	u32 cfg_base_addr;
608501a02ffcSStephen M. Cameron 	u64 cfg_base_addr_index;
6086303932fdSDon Brace 	u32 trans_offset;
6087a51fd47fSStephen M. Cameron 	int rc;
608877c4495cSStephen M. Cameron 
6089a51fd47fSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
6090a51fd47fSStephen M. Cameron 		&cfg_base_addr_index, &cfg_offset);
6091a51fd47fSStephen M. Cameron 	if (rc)
6092a51fd47fSStephen M. Cameron 		return rc;
609377c4495cSStephen M. Cameron 	h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
6094a51fd47fSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
6095cd3c81c4SRobert Elliott 	if (!h->cfgtable) {
6096cd3c81c4SRobert Elliott 		dev_err(&h->pdev->dev, "Failed mapping cfgtable\n");
609777c4495cSStephen M. Cameron 		return -ENOMEM;
6098cd3c81c4SRobert Elliott 	}
6099580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(h->cfgtable);
6100580ada3cSStephen M. Cameron 	if (rc)
6101580ada3cSStephen M. Cameron 		return rc;
610277c4495cSStephen M. Cameron 	/* Find performant mode table. */
6103a51fd47fSStephen M. Cameron 	trans_offset = readl(&h->cfgtable->TransMethodOffset);
610477c4495cSStephen M. Cameron 	h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
610577c4495cSStephen M. Cameron 				cfg_base_addr_index)+cfg_offset+trans_offset,
610677c4495cSStephen M. Cameron 				sizeof(*h->transtable));
610777c4495cSStephen M. Cameron 	if (!h->transtable)
610877c4495cSStephen M. Cameron 		return -ENOMEM;
610977c4495cSStephen M. Cameron 	return 0;
611077c4495cSStephen M. Cameron }
611177c4495cSStephen M. Cameron 
61126f039790SGreg Kroah-Hartman static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
6113cba3d38bSStephen M. Cameron {
6114cba3d38bSStephen M. Cameron 	h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
611572ceeaecSStephen M. Cameron 
611672ceeaecSStephen M. Cameron 	/* Limit commands in memory limited kdump scenario. */
611772ceeaecSStephen M. Cameron 	if (reset_devices && h->max_commands > 32)
611872ceeaecSStephen M. Cameron 		h->max_commands = 32;
611972ceeaecSStephen M. Cameron 
6120cba3d38bSStephen M. Cameron 	if (h->max_commands < 16) {
6121cba3d38bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "Controller reports "
6122cba3d38bSStephen M. Cameron 			"max supported commands of %d, an obvious lie. "
6123cba3d38bSStephen M. Cameron 			"Using 16.  Ensure that firmware is up to date.\n",
6124cba3d38bSStephen M. Cameron 			h->max_commands);
6125cba3d38bSStephen M. Cameron 		h->max_commands = 16;
6126cba3d38bSStephen M. Cameron 	}
6127cba3d38bSStephen M. Cameron }
6128cba3d38bSStephen M. Cameron 
6129c7ee65b3SWebb Scales /* If the controller reports that the total max sg entries is greater than 512,
6130c7ee65b3SWebb Scales  * then we know that chained SG blocks work.  (Original smart arrays did not
6131c7ee65b3SWebb Scales  * support chained SG blocks and would return zero for max sg entries.)
6132c7ee65b3SWebb Scales  */
6133c7ee65b3SWebb Scales static int hpsa_supports_chained_sg_blocks(struct ctlr_info *h)
6134c7ee65b3SWebb Scales {
6135c7ee65b3SWebb Scales 	return h->maxsgentries > 512;
6136c7ee65b3SWebb Scales }
6137c7ee65b3SWebb Scales 
6138b93d7536SStephen M. Cameron /* Interrogate the hardware for some limits:
6139b93d7536SStephen M. Cameron  * max commands, max SG elements without chaining, and with chaining,
6140b93d7536SStephen M. Cameron  * SG chain block size, etc.
6141b93d7536SStephen M. Cameron  */
61426f039790SGreg Kroah-Hartman static void hpsa_find_board_params(struct ctlr_info *h)
6143b93d7536SStephen M. Cameron {
6144cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
6145*45fcb86eSStephen Cameron 	h->nr_cmds = h->max_commands;
6146b93d7536SStephen M. Cameron 	h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
6147283b4a9bSStephen M. Cameron 	h->fw_support = readl(&(h->cfgtable->misc_fw_support));
6148c7ee65b3SWebb Scales 	if (hpsa_supports_chained_sg_blocks(h)) {
6149c7ee65b3SWebb Scales 		/* Limit in-command s/g elements to 32 save dma'able memory. */
6150b93d7536SStephen M. Cameron 		h->max_cmd_sg_entries = 32;
61511a63ea6fSWebb Scales 		h->chainsize = h->maxsgentries - h->max_cmd_sg_entries;
6152b93d7536SStephen M. Cameron 		h->maxsgentries--; /* save one for chain pointer */
6153b93d7536SStephen M. Cameron 	} else {
6154c7ee65b3SWebb Scales 		/*
6155c7ee65b3SWebb Scales 		 * Original smart arrays supported at most 31 s/g entries
6156c7ee65b3SWebb Scales 		 * embedded inline in the command (trying to use more
6157c7ee65b3SWebb Scales 		 * would lock up the controller)
6158c7ee65b3SWebb Scales 		 */
6159c7ee65b3SWebb Scales 		h->max_cmd_sg_entries = 31;
61601a63ea6fSWebb Scales 		h->maxsgentries = 31; /* default to traditional values */
6161c7ee65b3SWebb Scales 		h->chainsize = 0;
6162b93d7536SStephen M. Cameron 	}
616375167d2cSStephen M. Cameron 
616475167d2cSStephen M. Cameron 	/* Find out what task management functions are supported and cache */
616575167d2cSStephen M. Cameron 	h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
61660e7a7fceSScott Teel 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
61670e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
61680e7a7fceSScott Teel 	if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
61690e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
6170b93d7536SStephen M. Cameron }
6171b93d7536SStephen M. Cameron 
617276c46e49SStephen M. Cameron static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
617376c46e49SStephen M. Cameron {
61740fc9fd40SAkinobu Mita 	if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
6175050f7147SStephen Cameron 		dev_err(&h->pdev->dev, "not a valid CISS config table\n");
617676c46e49SStephen M. Cameron 		return false;
617776c46e49SStephen M. Cameron 	}
617876c46e49SStephen M. Cameron 	return true;
617976c46e49SStephen M. Cameron }
618076c46e49SStephen M. Cameron 
618197a5e98cSStephen M. Cameron static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
6182f7c39101SStephen M. Cameron {
618397a5e98cSStephen M. Cameron 	u32 driver_support;
6184f7c39101SStephen M. Cameron 
618597a5e98cSStephen M. Cameron 	driver_support = readl(&(h->cfgtable->driver_support));
61860b9e7b74SArnd Bergmann 	/* Need to enable prefetch in the SCSI core for 6400 in x86 */
61870b9e7b74SArnd Bergmann #ifdef CONFIG_X86
618897a5e98cSStephen M. Cameron 	driver_support |= ENABLE_SCSI_PREFETCH;
6189f7c39101SStephen M. Cameron #endif
619028e13446SStephen M. Cameron 	driver_support |= ENABLE_UNIT_ATTN;
619128e13446SStephen M. Cameron 	writel(driver_support, &(h->cfgtable->driver_support));
6192f7c39101SStephen M. Cameron }
6193f7c39101SStephen M. Cameron 
61943d0eab67SStephen M. Cameron /* Disable DMA prefetch for the P600.  Otherwise an ASIC bug may result
61953d0eab67SStephen M. Cameron  * in a prefetch beyond physical memory.
61963d0eab67SStephen M. Cameron  */
61973d0eab67SStephen M. Cameron static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
61983d0eab67SStephen M. Cameron {
61993d0eab67SStephen M. Cameron 	u32 dma_prefetch;
62003d0eab67SStephen M. Cameron 
62013d0eab67SStephen M. Cameron 	if (h->board_id != 0x3225103C)
62023d0eab67SStephen M. Cameron 		return;
62033d0eab67SStephen M. Cameron 	dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
62043d0eab67SStephen M. Cameron 	dma_prefetch |= 0x8000;
62053d0eab67SStephen M. Cameron 	writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
62063d0eab67SStephen M. Cameron }
62073d0eab67SStephen M. Cameron 
620876438d08SStephen M. Cameron static void hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
620976438d08SStephen M. Cameron {
621076438d08SStephen M. Cameron 	int i;
621176438d08SStephen M. Cameron 	u32 doorbell_value;
621276438d08SStephen M. Cameron 	unsigned long flags;
621376438d08SStephen M. Cameron 	/* wait until the clear_event_notify bit 6 is cleared by controller. */
621476438d08SStephen M. Cameron 	for (i = 0; i < MAX_CONFIG_WAIT; i++) {
621576438d08SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
621676438d08SStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
621776438d08SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
621876438d08SStephen M. Cameron 		if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
621976438d08SStephen M. Cameron 			break;
622076438d08SStephen M. Cameron 		/* delay and try again */
622176438d08SStephen M. Cameron 		msleep(20);
622276438d08SStephen M. Cameron 	}
622376438d08SStephen M. Cameron }
622476438d08SStephen M. Cameron 
62256f039790SGreg Kroah-Hartman static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
6226eb6b2ae9SStephen M. Cameron {
6227eb6b2ae9SStephen M. Cameron 	int i;
62286eaf46fdSStephen M. Cameron 	u32 doorbell_value;
62296eaf46fdSStephen M. Cameron 	unsigned long flags;
6230eb6b2ae9SStephen M. Cameron 
6231eb6b2ae9SStephen M. Cameron 	/* under certain very rare conditions, this can take awhile.
6232eb6b2ae9SStephen M. Cameron 	 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
6233eb6b2ae9SStephen M. Cameron 	 * as we enter this code.)
6234eb6b2ae9SStephen M. Cameron 	 */
6235eb6b2ae9SStephen M. Cameron 	for (i = 0; i < MAX_CONFIG_WAIT; i++) {
62366eaf46fdSStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
62376eaf46fdSStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
62386eaf46fdSStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
6239382be668SDan Carpenter 		if (!(doorbell_value & CFGTBL_ChangeReq))
6240eb6b2ae9SStephen M. Cameron 			break;
6241eb6b2ae9SStephen M. Cameron 		/* delay and try again */
624260d3f5b0SStephen M. Cameron 		usleep_range(10000, 20000);
6243eb6b2ae9SStephen M. Cameron 	}
62443f4336f3SStephen M. Cameron }
62453f4336f3SStephen M. Cameron 
62466f039790SGreg Kroah-Hartman static int hpsa_enter_simple_mode(struct ctlr_info *h)
62473f4336f3SStephen M. Cameron {
62483f4336f3SStephen M. Cameron 	u32 trans_support;
62493f4336f3SStephen M. Cameron 
62503f4336f3SStephen M. Cameron 	trans_support = readl(&(h->cfgtable->TransportSupport));
62513f4336f3SStephen M. Cameron 	if (!(trans_support & SIMPLE_MODE))
62523f4336f3SStephen M. Cameron 		return -ENOTSUPP;
62533f4336f3SStephen M. Cameron 
62543f4336f3SStephen M. Cameron 	h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
6255283b4a9bSStephen M. Cameron 
62563f4336f3SStephen M. Cameron 	/* Update the field, and then ring the doorbell */
62573f4336f3SStephen M. Cameron 	writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
6258b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
62593f4336f3SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
62603f4336f3SStephen M. Cameron 	hpsa_wait_for_mode_change_ack(h);
6261eb6b2ae9SStephen M. Cameron 	print_cfg_table(&h->pdev->dev, h->cfgtable);
6262283b4a9bSStephen M. Cameron 	if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
6263283b4a9bSStephen M. Cameron 		goto error;
6264960a30e7SStephen M. Cameron 	h->transMethod = CFGTBL_Trans_Simple;
6265eb6b2ae9SStephen M. Cameron 	return 0;
6266283b4a9bSStephen M. Cameron error:
6267050f7147SStephen Cameron 	dev_err(&h->pdev->dev, "failed to enter simple mode\n");
6268283b4a9bSStephen M. Cameron 	return -ENODEV;
6269eb6b2ae9SStephen M. Cameron }
6270eb6b2ae9SStephen M. Cameron 
62716f039790SGreg Kroah-Hartman static int hpsa_pci_init(struct ctlr_info *h)
627277c4495cSStephen M. Cameron {
6273eb6b2ae9SStephen M. Cameron 	int prod_index, err;
6274edd16368SStephen M. Cameron 
6275e5c880d1SStephen M. Cameron 	prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
6276e5c880d1SStephen M. Cameron 	if (prod_index < 0)
627760f923b9SRobert Elliott 		return prod_index;
6278e5c880d1SStephen M. Cameron 	h->product_name = products[prod_index].product_name;
6279e5c880d1SStephen M. Cameron 	h->access = *(products[prod_index].access);
6280e5c880d1SStephen M. Cameron 
6281e5a44df8SMatthew Garrett 	pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
6282e5a44df8SMatthew Garrett 			       PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
6283e5a44df8SMatthew Garrett 
628455c06c71SStephen M. Cameron 	err = pci_enable_device(h->pdev);
6285edd16368SStephen M. Cameron 	if (err) {
628655c06c71SStephen M. Cameron 		dev_warn(&h->pdev->dev, "unable to enable PCI device\n");
6287edd16368SStephen M. Cameron 		return err;
6288edd16368SStephen M. Cameron 	}
6289edd16368SStephen M. Cameron 
6290f79cfec6SStephen M. Cameron 	err = pci_request_regions(h->pdev, HPSA);
6291edd16368SStephen M. Cameron 	if (err) {
629255c06c71SStephen M. Cameron 		dev_err(&h->pdev->dev,
629355c06c71SStephen M. Cameron 			"cannot obtain PCI resources, aborting\n");
6294edd16368SStephen M. Cameron 		return err;
6295edd16368SStephen M. Cameron 	}
62964fa604e1SRobert Elliott 
62974fa604e1SRobert Elliott 	pci_set_master(h->pdev);
62984fa604e1SRobert Elliott 
62996b3f4c52SStephen M. Cameron 	hpsa_interrupt_mode(h);
630012d2cd47SStephen M. Cameron 	err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
63013a7774ceSStephen M. Cameron 	if (err)
6302edd16368SStephen M. Cameron 		goto err_out_free_res;
6303edd16368SStephen M. Cameron 	h->vaddr = remap_pci_mem(h->paddr, 0x250);
6304204892e9SStephen M. Cameron 	if (!h->vaddr) {
6305204892e9SStephen M. Cameron 		err = -ENOMEM;
6306204892e9SStephen M. Cameron 		goto err_out_free_res;
6307204892e9SStephen M. Cameron 	}
6308fe5389c8SStephen M. Cameron 	err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
63092c4c8c8bSStephen M. Cameron 	if (err)
6310edd16368SStephen M. Cameron 		goto err_out_free_res;
631177c4495cSStephen M. Cameron 	err = hpsa_find_cfgtables(h);
631277c4495cSStephen M. Cameron 	if (err)
6313edd16368SStephen M. Cameron 		goto err_out_free_res;
6314b93d7536SStephen M. Cameron 	hpsa_find_board_params(h);
6315edd16368SStephen M. Cameron 
631676c46e49SStephen M. Cameron 	if (!hpsa_CISS_signature_present(h)) {
6317edd16368SStephen M. Cameron 		err = -ENODEV;
6318edd16368SStephen M. Cameron 		goto err_out_free_res;
6319edd16368SStephen M. Cameron 	}
632097a5e98cSStephen M. Cameron 	hpsa_set_driver_support_bits(h);
63213d0eab67SStephen M. Cameron 	hpsa_p600_dma_prefetch_quirk(h);
6322eb6b2ae9SStephen M. Cameron 	err = hpsa_enter_simple_mode(h);
6323eb6b2ae9SStephen M. Cameron 	if (err)
6324edd16368SStephen M. Cameron 		goto err_out_free_res;
6325edd16368SStephen M. Cameron 	return 0;
6326edd16368SStephen M. Cameron 
6327edd16368SStephen M. Cameron err_out_free_res:
6328204892e9SStephen M. Cameron 	if (h->transtable)
6329204892e9SStephen M. Cameron 		iounmap(h->transtable);
6330204892e9SStephen M. Cameron 	if (h->cfgtable)
6331204892e9SStephen M. Cameron 		iounmap(h->cfgtable);
6332204892e9SStephen M. Cameron 	if (h->vaddr)
6333204892e9SStephen M. Cameron 		iounmap(h->vaddr);
6334f0bd0b68SStephen M. Cameron 	pci_disable_device(h->pdev);
633555c06c71SStephen M. Cameron 	pci_release_regions(h->pdev);
6336edd16368SStephen M. Cameron 	return err;
6337edd16368SStephen M. Cameron }
6338edd16368SStephen M. Cameron 
63396f039790SGreg Kroah-Hartman static void hpsa_hba_inquiry(struct ctlr_info *h)
6340339b2b14SStephen M. Cameron {
6341339b2b14SStephen M. Cameron 	int rc;
6342339b2b14SStephen M. Cameron 
6343339b2b14SStephen M. Cameron #define HBA_INQUIRY_BYTE_COUNT 64
6344339b2b14SStephen M. Cameron 	h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
6345339b2b14SStephen M. Cameron 	if (!h->hba_inquiry_data)
6346339b2b14SStephen M. Cameron 		return;
6347339b2b14SStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
6348339b2b14SStephen M. Cameron 		h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
6349339b2b14SStephen M. Cameron 	if (rc != 0) {
6350339b2b14SStephen M. Cameron 		kfree(h->hba_inquiry_data);
6351339b2b14SStephen M. Cameron 		h->hba_inquiry_data = NULL;
6352339b2b14SStephen M. Cameron 	}
6353339b2b14SStephen M. Cameron }
6354339b2b14SStephen M. Cameron 
63556f039790SGreg Kroah-Hartman static int hpsa_init_reset_devices(struct pci_dev *pdev)
6356edd16368SStephen M. Cameron {
63571df8552aSStephen M. Cameron 	int rc, i;
63583b747298STomas Henzl 	void __iomem *vaddr;
6359edd16368SStephen M. Cameron 
63604c2a8c40SStephen M. Cameron 	if (!reset_devices)
63614c2a8c40SStephen M. Cameron 		return 0;
63624c2a8c40SStephen M. Cameron 
6363132aa220STomas Henzl 	/* kdump kernel is loading, we don't know in which state is
6364132aa220STomas Henzl 	 * the pci interface. The dev->enable_cnt is equal zero
6365132aa220STomas Henzl 	 * so we call enable+disable, wait a while and switch it on.
6366132aa220STomas Henzl 	 */
6367132aa220STomas Henzl 	rc = pci_enable_device(pdev);
6368132aa220STomas Henzl 	if (rc) {
6369132aa220STomas Henzl 		dev_warn(&pdev->dev, "Failed to enable PCI device\n");
6370132aa220STomas Henzl 		return -ENODEV;
6371132aa220STomas Henzl 	}
6372132aa220STomas Henzl 	pci_disable_device(pdev);
6373132aa220STomas Henzl 	msleep(260);			/* a randomly chosen number */
6374132aa220STomas Henzl 	rc = pci_enable_device(pdev);
6375132aa220STomas Henzl 	if (rc) {
6376132aa220STomas Henzl 		dev_warn(&pdev->dev, "failed to enable device.\n");
6377132aa220STomas Henzl 		return -ENODEV;
6378132aa220STomas Henzl 	}
63794fa604e1SRobert Elliott 
6380859c75abSTomas Henzl 	pci_set_master(pdev);
63814fa604e1SRobert Elliott 
63823b747298STomas Henzl 	vaddr = pci_ioremap_bar(pdev, 0);
63833b747298STomas Henzl 	if (vaddr == NULL) {
63843b747298STomas Henzl 		rc = -ENOMEM;
63853b747298STomas Henzl 		goto out_disable;
63863b747298STomas Henzl 	}
63873b747298STomas Henzl 	writel(SA5_INTR_OFF, vaddr + SA5_REPLY_INTR_MASK_OFFSET);
63883b747298STomas Henzl 	iounmap(vaddr);
63893b747298STomas Henzl 
63901df8552aSStephen M. Cameron 	/* Reset the controller with a PCI power-cycle or via doorbell */
63911df8552aSStephen M. Cameron 	rc = hpsa_kdump_hard_reset_controller(pdev);
6392edd16368SStephen M. Cameron 
63931df8552aSStephen M. Cameron 	/* -ENOTSUPP here means we cannot reset the controller
63941df8552aSStephen M. Cameron 	 * but it's already (and still) up and running in
639518867659SStephen M. Cameron 	 * "performant mode".  Or, it might be 640x, which can't reset
639618867659SStephen M. Cameron 	 * due to concerns about shared bbwc between 6402/6404 pair.
63971df8552aSStephen M. Cameron 	 */
6398adf1b3a3SRobert Elliott 	if (rc)
6399132aa220STomas Henzl 		goto out_disable;
6400edd16368SStephen M. Cameron 
6401edd16368SStephen M. Cameron 	/* Now try to get the controller to respond to a no-op */
64021ba66c9cSRobert Elliott 	dev_info(&pdev->dev, "Waiting for controller to respond to no-op\n");
6403edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
6404edd16368SStephen M. Cameron 		if (hpsa_noop(pdev) == 0)
6405edd16368SStephen M. Cameron 			break;
6406edd16368SStephen M. Cameron 		else
6407edd16368SStephen M. Cameron 			dev_warn(&pdev->dev, "no-op failed%s\n",
6408edd16368SStephen M. Cameron 					(i < 11 ? "; re-trying" : ""));
6409edd16368SStephen M. Cameron 	}
6410132aa220STomas Henzl 
6411132aa220STomas Henzl out_disable:
6412132aa220STomas Henzl 
6413132aa220STomas Henzl 	pci_disable_device(pdev);
6414132aa220STomas Henzl 	return rc;
6415edd16368SStephen M. Cameron }
6416edd16368SStephen M. Cameron 
64176f039790SGreg Kroah-Hartman static int hpsa_allocate_cmd_pool(struct ctlr_info *h)
64182e9d1b36SStephen M. Cameron {
64192e9d1b36SStephen M. Cameron 	h->cmd_pool_bits = kzalloc(
64202e9d1b36SStephen M. Cameron 		DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
64212e9d1b36SStephen M. Cameron 		sizeof(unsigned long), GFP_KERNEL);
64222e9d1b36SStephen M. Cameron 	h->cmd_pool = pci_alloc_consistent(h->pdev,
64232e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->cmd_pool),
64242e9d1b36SStephen M. Cameron 		    &(h->cmd_pool_dhandle));
64252e9d1b36SStephen M. Cameron 	h->errinfo_pool = pci_alloc_consistent(h->pdev,
64262e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->errinfo_pool),
64272e9d1b36SStephen M. Cameron 		    &(h->errinfo_pool_dhandle));
64282e9d1b36SStephen M. Cameron 	if ((h->cmd_pool_bits == NULL)
64292e9d1b36SStephen M. Cameron 	    || (h->cmd_pool == NULL)
64302e9d1b36SStephen M. Cameron 	    || (h->errinfo_pool == NULL)) {
64312e9d1b36SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory in %s", __func__);
64322c143342SRobert Elliott 		goto clean_up;
64332e9d1b36SStephen M. Cameron 	}
64342e9d1b36SStephen M. Cameron 	return 0;
64352c143342SRobert Elliott clean_up:
64362c143342SRobert Elliott 	hpsa_free_cmd_pool(h);
64372c143342SRobert Elliott 	return -ENOMEM;
64382e9d1b36SStephen M. Cameron }
64392e9d1b36SStephen M. Cameron 
64402e9d1b36SStephen M. Cameron static void hpsa_free_cmd_pool(struct ctlr_info *h)
64412e9d1b36SStephen M. Cameron {
64422e9d1b36SStephen M. Cameron 	kfree(h->cmd_pool_bits);
64432e9d1b36SStephen M. Cameron 	if (h->cmd_pool)
64442e9d1b36SStephen M. Cameron 		pci_free_consistent(h->pdev,
64452e9d1b36SStephen M. Cameron 			    h->nr_cmds * sizeof(struct CommandList),
64462e9d1b36SStephen M. Cameron 			    h->cmd_pool, h->cmd_pool_dhandle);
6447aca9012aSStephen M. Cameron 	if (h->ioaccel2_cmd_pool)
6448aca9012aSStephen M. Cameron 		pci_free_consistent(h->pdev,
6449aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
6450aca9012aSStephen M. Cameron 			h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
64512e9d1b36SStephen M. Cameron 	if (h->errinfo_pool)
64522e9d1b36SStephen M. Cameron 		pci_free_consistent(h->pdev,
64532e9d1b36SStephen M. Cameron 			    h->nr_cmds * sizeof(struct ErrorInfo),
64542e9d1b36SStephen M. Cameron 			    h->errinfo_pool,
64552e9d1b36SStephen M. Cameron 			    h->errinfo_pool_dhandle);
6456e1f7de0cSMatt Gates 	if (h->ioaccel_cmd_pool)
6457e1f7de0cSMatt Gates 		pci_free_consistent(h->pdev,
6458e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(struct io_accel1_cmd),
6459e1f7de0cSMatt Gates 			h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
64602e9d1b36SStephen M. Cameron }
64612e9d1b36SStephen M. Cameron 
646241b3cf08SStephen M. Cameron static void hpsa_irq_affinity_hints(struct ctlr_info *h)
646341b3cf08SStephen M. Cameron {
6464ec429952SFabian Frederick 	int i, cpu;
646541b3cf08SStephen M. Cameron 
646641b3cf08SStephen M. Cameron 	cpu = cpumask_first(cpu_online_mask);
646741b3cf08SStephen M. Cameron 	for (i = 0; i < h->msix_vector; i++) {
6468ec429952SFabian Frederick 		irq_set_affinity_hint(h->intr[i], get_cpu_mask(cpu));
646941b3cf08SStephen M. Cameron 		cpu = cpumask_next(cpu, cpu_online_mask);
647041b3cf08SStephen M. Cameron 	}
647141b3cf08SStephen M. Cameron }
647241b3cf08SStephen M. Cameron 
6473ec501a18SRobert Elliott /* clear affinity hints and free MSI-X, MSI, or legacy INTx vectors */
6474ec501a18SRobert Elliott static void hpsa_free_irqs(struct ctlr_info *h)
6475ec501a18SRobert Elliott {
6476ec501a18SRobert Elliott 	int i;
6477ec501a18SRobert Elliott 
6478ec501a18SRobert Elliott 	if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
6479ec501a18SRobert Elliott 		/* Single reply queue, only one irq to free */
6480ec501a18SRobert Elliott 		i = h->intr_mode;
6481ec501a18SRobert Elliott 		irq_set_affinity_hint(h->intr[i], NULL);
6482ec501a18SRobert Elliott 		free_irq(h->intr[i], &h->q[i]);
6483ec501a18SRobert Elliott 		return;
6484ec501a18SRobert Elliott 	}
6485ec501a18SRobert Elliott 
6486ec501a18SRobert Elliott 	for (i = 0; i < h->msix_vector; i++) {
6487ec501a18SRobert Elliott 		irq_set_affinity_hint(h->intr[i], NULL);
6488ec501a18SRobert Elliott 		free_irq(h->intr[i], &h->q[i]);
6489ec501a18SRobert Elliott 	}
6490a4e17fc1SRobert Elliott 	for (; i < MAX_REPLY_QUEUES; i++)
6491a4e17fc1SRobert Elliott 		h->q[i] = 0;
6492ec501a18SRobert Elliott }
6493ec501a18SRobert Elliott 
64949ee61794SRobert Elliott /* returns 0 on success; cleans up and returns -Enn on error */
64959ee61794SRobert Elliott static int hpsa_request_irqs(struct ctlr_info *h,
64960ae01a32SStephen M. Cameron 	irqreturn_t (*msixhandler)(int, void *),
64970ae01a32SStephen M. Cameron 	irqreturn_t (*intxhandler)(int, void *))
64980ae01a32SStephen M. Cameron {
6499254f796bSMatt Gates 	int rc, i;
65000ae01a32SStephen M. Cameron 
6501254f796bSMatt Gates 	/*
6502254f796bSMatt Gates 	 * initialize h->q[x] = x so that interrupt handlers know which
6503254f796bSMatt Gates 	 * queue to process.
6504254f796bSMatt Gates 	 */
6505254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++)
6506254f796bSMatt Gates 		h->q[i] = (u8) i;
6507254f796bSMatt Gates 
6508eee0f03aSHannes Reinecke 	if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
6509254f796bSMatt Gates 		/* If performant mode and MSI-X, use multiple reply queues */
6510a4e17fc1SRobert Elliott 		for (i = 0; i < h->msix_vector; i++) {
6511254f796bSMatt Gates 			rc = request_irq(h->intr[i], msixhandler,
6512254f796bSMatt Gates 					0, h->devname,
6513254f796bSMatt Gates 					&h->q[i]);
6514a4e17fc1SRobert Elliott 			if (rc) {
6515a4e17fc1SRobert Elliott 				int j;
6516a4e17fc1SRobert Elliott 
6517a4e17fc1SRobert Elliott 				dev_err(&h->pdev->dev,
6518a4e17fc1SRobert Elliott 					"failed to get irq %d for %s\n",
6519a4e17fc1SRobert Elliott 				       h->intr[i], h->devname);
6520a4e17fc1SRobert Elliott 				for (j = 0; j < i; j++) {
6521a4e17fc1SRobert Elliott 					free_irq(h->intr[j], &h->q[j]);
6522a4e17fc1SRobert Elliott 					h->q[j] = 0;
6523a4e17fc1SRobert Elliott 				}
6524a4e17fc1SRobert Elliott 				for (; j < MAX_REPLY_QUEUES; j++)
6525a4e17fc1SRobert Elliott 					h->q[j] = 0;
6526a4e17fc1SRobert Elliott 				return rc;
6527a4e17fc1SRobert Elliott 			}
6528a4e17fc1SRobert Elliott 		}
652941b3cf08SStephen M. Cameron 		hpsa_irq_affinity_hints(h);
6530254f796bSMatt Gates 	} else {
6531254f796bSMatt Gates 		/* Use single reply pool */
6532eee0f03aSHannes Reinecke 		if (h->msix_vector > 0 || h->msi_vector) {
6533254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
6534254f796bSMatt Gates 				msixhandler, 0, h->devname,
6535254f796bSMatt Gates 				&h->q[h->intr_mode]);
6536254f796bSMatt Gates 		} else {
6537254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
6538254f796bSMatt Gates 				intxhandler, IRQF_SHARED, h->devname,
6539254f796bSMatt Gates 				&h->q[h->intr_mode]);
6540254f796bSMatt Gates 		}
6541254f796bSMatt Gates 	}
65420ae01a32SStephen M. Cameron 	if (rc) {
65430ae01a32SStephen M. Cameron 		dev_err(&h->pdev->dev, "unable to get irq %d for %s\n",
65440ae01a32SStephen M. Cameron 		       h->intr[h->intr_mode], h->devname);
65450ae01a32SStephen M. Cameron 		return -ENODEV;
65460ae01a32SStephen M. Cameron 	}
65470ae01a32SStephen M. Cameron 	return 0;
65480ae01a32SStephen M. Cameron }
65490ae01a32SStephen M. Cameron 
65506f039790SGreg Kroah-Hartman static int hpsa_kdump_soft_reset(struct ctlr_info *h)
655164670ac8SStephen M. Cameron {
655264670ac8SStephen M. Cameron 	if (hpsa_send_host_reset(h, RAID_CTLR_LUNID,
655364670ac8SStephen M. Cameron 		HPSA_RESET_TYPE_CONTROLLER)) {
655464670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Resetting array controller failed.\n");
655564670ac8SStephen M. Cameron 		return -EIO;
655664670ac8SStephen M. Cameron 	}
655764670ac8SStephen M. Cameron 
655864670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
655964670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) {
656064670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
656164670ac8SStephen M. Cameron 		return -1;
656264670ac8SStephen M. Cameron 	}
656364670ac8SStephen M. Cameron 
656464670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
656564670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) {
656664670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Board failed to become ready "
656764670ac8SStephen M. Cameron 			"after soft reset.\n");
656864670ac8SStephen M. Cameron 		return -1;
656964670ac8SStephen M. Cameron 	}
657064670ac8SStephen M. Cameron 
657164670ac8SStephen M. Cameron 	return 0;
657264670ac8SStephen M. Cameron }
657364670ac8SStephen M. Cameron 
65740097f0f4SStephen M. Cameron static void hpsa_free_irqs_and_disable_msix(struct ctlr_info *h)
657564670ac8SStephen M. Cameron {
6576ec501a18SRobert Elliott 	hpsa_free_irqs(h);
657764670ac8SStephen M. Cameron #ifdef CONFIG_PCI_MSI
65780097f0f4SStephen M. Cameron 	if (h->msix_vector) {
65790097f0f4SStephen M. Cameron 		if (h->pdev->msix_enabled)
658064670ac8SStephen M. Cameron 			pci_disable_msix(h->pdev);
65810097f0f4SStephen M. Cameron 	} else if (h->msi_vector) {
65820097f0f4SStephen M. Cameron 		if (h->pdev->msi_enabled)
658364670ac8SStephen M. Cameron 			pci_disable_msi(h->pdev);
65840097f0f4SStephen M. Cameron 	}
658564670ac8SStephen M. Cameron #endif /* CONFIG_PCI_MSI */
65860097f0f4SStephen M. Cameron }
65870097f0f4SStephen M. Cameron 
6588072b0518SStephen M. Cameron static void hpsa_free_reply_queues(struct ctlr_info *h)
6589072b0518SStephen M. Cameron {
6590072b0518SStephen M. Cameron 	int i;
6591072b0518SStephen M. Cameron 
6592072b0518SStephen M. Cameron 	for (i = 0; i < h->nreply_queues; i++) {
6593072b0518SStephen M. Cameron 		if (!h->reply_queue[i].head)
6594072b0518SStephen M. Cameron 			continue;
6595072b0518SStephen M. Cameron 		pci_free_consistent(h->pdev, h->reply_queue_size,
6596072b0518SStephen M. Cameron 			h->reply_queue[i].head, h->reply_queue[i].busaddr);
6597072b0518SStephen M. Cameron 		h->reply_queue[i].head = NULL;
6598072b0518SStephen M. Cameron 		h->reply_queue[i].busaddr = 0;
6599072b0518SStephen M. Cameron 	}
6600072b0518SStephen M. Cameron }
6601072b0518SStephen M. Cameron 
66020097f0f4SStephen M. Cameron static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
66030097f0f4SStephen M. Cameron {
66040097f0f4SStephen M. Cameron 	hpsa_free_irqs_and_disable_msix(h);
660564670ac8SStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
660664670ac8SStephen M. Cameron 	hpsa_free_cmd_pool(h);
6607e1f7de0cSMatt Gates 	kfree(h->ioaccel1_blockFetchTable);
660864670ac8SStephen M. Cameron 	kfree(h->blockFetchTable);
6609072b0518SStephen M. Cameron 	hpsa_free_reply_queues(h);
661064670ac8SStephen M. Cameron 	if (h->vaddr)
661164670ac8SStephen M. Cameron 		iounmap(h->vaddr);
661264670ac8SStephen M. Cameron 	if (h->transtable)
661364670ac8SStephen M. Cameron 		iounmap(h->transtable);
661464670ac8SStephen M. Cameron 	if (h->cfgtable)
661564670ac8SStephen M. Cameron 		iounmap(h->cfgtable);
6616132aa220STomas Henzl 	pci_disable_device(h->pdev);
661764670ac8SStephen M. Cameron 	pci_release_regions(h->pdev);
661864670ac8SStephen M. Cameron 	kfree(h);
661964670ac8SStephen M. Cameron }
662064670ac8SStephen M. Cameron 
6621a0c12413SStephen M. Cameron /* Called when controller lockup detected. */
6622a0c12413SStephen M. Cameron static void fail_all_cmds_on_list(struct ctlr_info *h, struct list_head *list)
6623a0c12413SStephen M. Cameron {
6624a0c12413SStephen M. Cameron 	struct CommandList *c = NULL;
6625a0c12413SStephen M. Cameron 
6626a0c12413SStephen M. Cameron 	assert_spin_locked(&h->lock);
6627a0c12413SStephen M. Cameron 	/* Mark all outstanding commands as failed and complete them. */
6628a0c12413SStephen M. Cameron 	while (!list_empty(list)) {
6629a0c12413SStephen M. Cameron 		c = list_entry(list->next, struct CommandList, list);
6630a0c12413SStephen M. Cameron 		c->err_info->CommandStatus = CMD_HARDWARE_ERR;
66315a3d16f5SStephen M. Cameron 		finish_cmd(c);
6632a0c12413SStephen M. Cameron 	}
6633a0c12413SStephen M. Cameron }
6634a0c12413SStephen M. Cameron 
6635094963daSStephen M. Cameron static void set_lockup_detected_for_all_cpus(struct ctlr_info *h, u32 value)
6636094963daSStephen M. Cameron {
6637094963daSStephen M. Cameron 	int i, cpu;
6638094963daSStephen M. Cameron 
6639094963daSStephen M. Cameron 	cpu = cpumask_first(cpu_online_mask);
6640094963daSStephen M. Cameron 	for (i = 0; i < num_online_cpus(); i++) {
6641094963daSStephen M. Cameron 		u32 *lockup_detected;
6642094963daSStephen M. Cameron 		lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
6643094963daSStephen M. Cameron 		*lockup_detected = value;
6644094963daSStephen M. Cameron 		cpu = cpumask_next(cpu, cpu_online_mask);
6645094963daSStephen M. Cameron 	}
6646094963daSStephen M. Cameron 	wmb(); /* be sure the per-cpu variables are out to memory */
6647094963daSStephen M. Cameron }
6648094963daSStephen M. Cameron 
6649a0c12413SStephen M. Cameron static void controller_lockup_detected(struct ctlr_info *h)
6650a0c12413SStephen M. Cameron {
6651a0c12413SStephen M. Cameron 	unsigned long flags;
6652094963daSStephen M. Cameron 	u32 lockup_detected;
6653a0c12413SStephen M. Cameron 
6654a0c12413SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
6655a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
6656094963daSStephen M. Cameron 	lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
6657094963daSStephen M. Cameron 	if (!lockup_detected) {
6658094963daSStephen M. Cameron 		/* no heartbeat, but controller gave us a zero. */
6659094963daSStephen M. Cameron 		dev_warn(&h->pdev->dev,
6660094963daSStephen M. Cameron 			"lockup detected but scratchpad register is zero\n");
6661094963daSStephen M. Cameron 		lockup_detected = 0xffffffff;
6662094963daSStephen M. Cameron 	}
6663094963daSStephen M. Cameron 	set_lockup_detected_for_all_cpus(h, lockup_detected);
6664a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
6665a0c12413SStephen M. Cameron 	dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x\n",
6666094963daSStephen M. Cameron 			lockup_detected);
6667a0c12413SStephen M. Cameron 	pci_disable_device(h->pdev);
6668a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
6669a0c12413SStephen M. Cameron 	fail_all_cmds_on_list(h, &h->cmpQ);
6670a0c12413SStephen M. Cameron 	fail_all_cmds_on_list(h, &h->reqQ);
6671a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
6672a0c12413SStephen M. Cameron }
6673a0c12413SStephen M. Cameron 
6674a0c12413SStephen M. Cameron static void detect_controller_lockup(struct ctlr_info *h)
6675a0c12413SStephen M. Cameron {
6676a0c12413SStephen M. Cameron 	u64 now;
6677a0c12413SStephen M. Cameron 	u32 heartbeat;
6678a0c12413SStephen M. Cameron 	unsigned long flags;
6679a0c12413SStephen M. Cameron 
6680a0c12413SStephen M. Cameron 	now = get_jiffies_64();
6681a0c12413SStephen M. Cameron 	/* If we've received an interrupt recently, we're ok. */
6682a0c12413SStephen M. Cameron 	if (time_after64(h->last_intr_timestamp +
6683e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
6684a0c12413SStephen M. Cameron 		return;
6685a0c12413SStephen M. Cameron 
6686a0c12413SStephen M. Cameron 	/*
6687a0c12413SStephen M. Cameron 	 * If we've already checked the heartbeat recently, we're ok.
6688a0c12413SStephen M. Cameron 	 * This could happen if someone sends us a signal. We
6689a0c12413SStephen M. Cameron 	 * otherwise don't care about signals in this thread.
6690a0c12413SStephen M. Cameron 	 */
6691a0c12413SStephen M. Cameron 	if (time_after64(h->last_heartbeat_timestamp +
6692e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
6693a0c12413SStephen M. Cameron 		return;
6694a0c12413SStephen M. Cameron 
6695a0c12413SStephen M. Cameron 	/* If heartbeat has not changed since we last looked, we're not ok. */
6696a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
6697a0c12413SStephen M. Cameron 	heartbeat = readl(&h->cfgtable->HeartBeat);
6698a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
6699a0c12413SStephen M. Cameron 	if (h->last_heartbeat == heartbeat) {
6700a0c12413SStephen M. Cameron 		controller_lockup_detected(h);
6701a0c12413SStephen M. Cameron 		return;
6702a0c12413SStephen M. Cameron 	}
6703a0c12413SStephen M. Cameron 
6704a0c12413SStephen M. Cameron 	/* We're ok. */
6705a0c12413SStephen M. Cameron 	h->last_heartbeat = heartbeat;
6706a0c12413SStephen M. Cameron 	h->last_heartbeat_timestamp = now;
6707a0c12413SStephen M. Cameron }
6708a0c12413SStephen M. Cameron 
67099846590eSStephen M. Cameron static void hpsa_ack_ctlr_events(struct ctlr_info *h)
671076438d08SStephen M. Cameron {
671176438d08SStephen M. Cameron 	int i;
671276438d08SStephen M. Cameron 	char *event_type;
671376438d08SStephen M. Cameron 
6714e863d68eSScott Teel 	/* Clear the driver-requested rescan flag */
6715e863d68eSScott Teel 	h->drv_req_rescan = 0;
6716e863d68eSScott Teel 
671776438d08SStephen M. Cameron 	/* Ask the controller to clear the events we're handling. */
67181f7cee8cSStephen M. Cameron 	if ((h->transMethod & (CFGTBL_Trans_io_accel1
67191f7cee8cSStephen M. Cameron 			| CFGTBL_Trans_io_accel2)) &&
672076438d08SStephen M. Cameron 		(h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
672176438d08SStephen M. Cameron 		 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
672276438d08SStephen M. Cameron 
672376438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
672476438d08SStephen M. Cameron 			event_type = "state change";
672576438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
672676438d08SStephen M. Cameron 			event_type = "configuration change";
672776438d08SStephen M. Cameron 		/* Stop sending new RAID offload reqs via the IO accelerator */
672876438d08SStephen M. Cameron 		scsi_block_requests(h->scsi_host);
672976438d08SStephen M. Cameron 		for (i = 0; i < h->ndevices; i++)
673076438d08SStephen M. Cameron 			h->dev[i]->offload_enabled = 0;
673123100dd9SStephen M. Cameron 		hpsa_drain_accel_commands(h);
673276438d08SStephen M. Cameron 		/* Set 'accelerator path config change' bit */
673376438d08SStephen M. Cameron 		dev_warn(&h->pdev->dev,
673476438d08SStephen M. Cameron 			"Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
673576438d08SStephen M. Cameron 			h->events, event_type);
673676438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
673776438d08SStephen M. Cameron 		/* Set the "clear event notify field update" bit 6 */
673876438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
673976438d08SStephen M. Cameron 		/* Wait until ctlr clears 'clear event notify field', bit 6 */
674076438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
674176438d08SStephen M. Cameron 		scsi_unblock_requests(h->scsi_host);
674276438d08SStephen M. Cameron 	} else {
674376438d08SStephen M. Cameron 		/* Acknowledge controller notification events. */
674476438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
674576438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
674676438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
674776438d08SStephen M. Cameron #if 0
674876438d08SStephen M. Cameron 		writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
674976438d08SStephen M. Cameron 		hpsa_wait_for_mode_change_ack(h);
675076438d08SStephen M. Cameron #endif
675176438d08SStephen M. Cameron 	}
67529846590eSStephen M. Cameron 	return;
675376438d08SStephen M. Cameron }
675476438d08SStephen M. Cameron 
675576438d08SStephen M. Cameron /* Check a register on the controller to see if there are configuration
675676438d08SStephen M. Cameron  * changes (added/changed/removed logical drives, etc.) which mean that
6757e863d68eSScott Teel  * we should rescan the controller for devices.
6758e863d68eSScott Teel  * Also check flag for driver-initiated rescan.
675976438d08SStephen M. Cameron  */
67609846590eSStephen M. Cameron static int hpsa_ctlr_needs_rescan(struct ctlr_info *h)
676176438d08SStephen M. Cameron {
67629846590eSStephen M. Cameron 	if (h->drv_req_rescan)
67639846590eSStephen M. Cameron 		return 1;
67649846590eSStephen M. Cameron 
676576438d08SStephen M. Cameron 	if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
67669846590eSStephen M. Cameron 		return 0;
676776438d08SStephen M. Cameron 
676876438d08SStephen M. Cameron 	h->events = readl(&(h->cfgtable->event_notify));
67699846590eSStephen M. Cameron 	return h->events & RESCAN_REQUIRED_EVENT_BITS;
67709846590eSStephen M. Cameron }
677176438d08SStephen M. Cameron 
677276438d08SStephen M. Cameron /*
67739846590eSStephen M. Cameron  * Check if any of the offline devices have become ready
677476438d08SStephen M. Cameron  */
67759846590eSStephen M. Cameron static int hpsa_offline_devices_ready(struct ctlr_info *h)
67769846590eSStephen M. Cameron {
67779846590eSStephen M. Cameron 	unsigned long flags;
67789846590eSStephen M. Cameron 	struct offline_device_entry *d;
67799846590eSStephen M. Cameron 	struct list_head *this, *tmp;
67809846590eSStephen M. Cameron 
67819846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
67829846590eSStephen M. Cameron 	list_for_each_safe(this, tmp, &h->offline_device_list) {
67839846590eSStephen M. Cameron 		d = list_entry(this, struct offline_device_entry,
67849846590eSStephen M. Cameron 				offline_list);
67859846590eSStephen M. Cameron 		spin_unlock_irqrestore(&h->offline_device_lock, flags);
6786d1fea47cSStephen M. Cameron 		if (!hpsa_volume_offline(h, d->scsi3addr)) {
6787d1fea47cSStephen M. Cameron 			spin_lock_irqsave(&h->offline_device_lock, flags);
6788d1fea47cSStephen M. Cameron 			list_del(&d->offline_list);
6789d1fea47cSStephen M. Cameron 			spin_unlock_irqrestore(&h->offline_device_lock, flags);
67909846590eSStephen M. Cameron 			return 1;
6791d1fea47cSStephen M. Cameron 		}
67929846590eSStephen M. Cameron 		spin_lock_irqsave(&h->offline_device_lock, flags);
679376438d08SStephen M. Cameron 	}
67949846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
67959846590eSStephen M. Cameron 	return 0;
67969846590eSStephen M. Cameron }
67979846590eSStephen M. Cameron 
679876438d08SStephen M. Cameron 
67998a98db73SStephen M. Cameron static void hpsa_monitor_ctlr_worker(struct work_struct *work)
6800a0c12413SStephen M. Cameron {
6801a0c12413SStephen M. Cameron 	unsigned long flags;
68028a98db73SStephen M. Cameron 	struct ctlr_info *h = container_of(to_delayed_work(work),
68038a98db73SStephen M. Cameron 					struct ctlr_info, monitor_ctlr_work);
6804a0c12413SStephen M. Cameron 	detect_controller_lockup(h);
6805094963daSStephen M. Cameron 	if (lockup_detected(h))
68068a98db73SStephen M. Cameron 		return;
68079846590eSStephen M. Cameron 
68089846590eSStephen M. Cameron 	if (hpsa_ctlr_needs_rescan(h) || hpsa_offline_devices_ready(h)) {
68099846590eSStephen M. Cameron 		scsi_host_get(h->scsi_host);
68109846590eSStephen M. Cameron 		h->drv_req_rescan = 0;
68119846590eSStephen M. Cameron 		hpsa_ack_ctlr_events(h);
68129846590eSStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
68139846590eSStephen M. Cameron 		scsi_host_put(h->scsi_host);
68149846590eSStephen M. Cameron 	}
68159846590eSStephen M. Cameron 
68168a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
68178a98db73SStephen M. Cameron 	if (h->remove_in_progress) {
68188a98db73SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
6819a0c12413SStephen M. Cameron 		return;
6820a0c12413SStephen M. Cameron 	}
68218a98db73SStephen M. Cameron 	schedule_delayed_work(&h->monitor_ctlr_work,
68228a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
68238a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
6824a0c12413SStephen M. Cameron }
6825a0c12413SStephen M. Cameron 
68266f039790SGreg Kroah-Hartman static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
68274c2a8c40SStephen M. Cameron {
68284c2a8c40SStephen M. Cameron 	int dac, rc;
68294c2a8c40SStephen M. Cameron 	struct ctlr_info *h;
683064670ac8SStephen M. Cameron 	int try_soft_reset = 0;
683164670ac8SStephen M. Cameron 	unsigned long flags;
68324c2a8c40SStephen M. Cameron 
68334c2a8c40SStephen M. Cameron 	if (number_of_controllers == 0)
68344c2a8c40SStephen M. Cameron 		printk(KERN_INFO DRIVER_NAME "\n");
68354c2a8c40SStephen M. Cameron 
68364c2a8c40SStephen M. Cameron 	rc = hpsa_init_reset_devices(pdev);
683764670ac8SStephen M. Cameron 	if (rc) {
683864670ac8SStephen M. Cameron 		if (rc != -ENOTSUPP)
68394c2a8c40SStephen M. Cameron 			return rc;
684064670ac8SStephen M. Cameron 		/* If the reset fails in a particular way (it has no way to do
684164670ac8SStephen M. Cameron 		 * a proper hard reset, so returns -ENOTSUPP) we can try to do
684264670ac8SStephen M. Cameron 		 * a soft reset once we get the controller configured up to the
684364670ac8SStephen M. Cameron 		 * point that it can accept a command.
684464670ac8SStephen M. Cameron 		 */
684564670ac8SStephen M. Cameron 		try_soft_reset = 1;
684664670ac8SStephen M. Cameron 		rc = 0;
684764670ac8SStephen M. Cameron 	}
684864670ac8SStephen M. Cameron 
684964670ac8SStephen M. Cameron reinit_after_soft_reset:
68504c2a8c40SStephen M. Cameron 
6851303932fdSDon Brace 	/* Command structures must be aligned on a 32-byte boundary because
6852303932fdSDon Brace 	 * the 5 lower bits of the address are used by the hardware. and by
6853303932fdSDon Brace 	 * the driver.  See comments in hpsa.h for more info.
6854303932fdSDon Brace 	 */
6855303932fdSDon Brace 	BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
6856edd16368SStephen M. Cameron 	h = kzalloc(sizeof(*h), GFP_KERNEL);
6857edd16368SStephen M. Cameron 	if (!h)
6858ecd9aad4SStephen M. Cameron 		return -ENOMEM;
6859edd16368SStephen M. Cameron 
686055c06c71SStephen M. Cameron 	h->pdev = pdev;
6861a9a3a273SStephen M. Cameron 	h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
68629e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&h->cmpQ);
68639e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&h->reqQ);
68649846590eSStephen M. Cameron 	INIT_LIST_HEAD(&h->offline_device_list);
68656eaf46fdSStephen M. Cameron 	spin_lock_init(&h->lock);
68669846590eSStephen M. Cameron 	spin_lock_init(&h->offline_device_lock);
68676eaf46fdSStephen M. Cameron 	spin_lock_init(&h->scan_lock);
68680390f0c0SStephen M. Cameron 	spin_lock_init(&h->passthru_count_lock);
6869094963daSStephen M. Cameron 
6870094963daSStephen M. Cameron 	/* Allocate and clear per-cpu variable lockup_detected */
6871094963daSStephen M. Cameron 	h->lockup_detected = alloc_percpu(u32);
68722a5ac326SStephen M. Cameron 	if (!h->lockup_detected) {
68732a5ac326SStephen M. Cameron 		rc = -ENOMEM;
6874094963daSStephen M. Cameron 		goto clean1;
68752a5ac326SStephen M. Cameron 	}
6876094963daSStephen M. Cameron 	set_lockup_detected_for_all_cpus(h, 0);
6877094963daSStephen M. Cameron 
687855c06c71SStephen M. Cameron 	rc = hpsa_pci_init(h);
6879ecd9aad4SStephen M. Cameron 	if (rc != 0)
6880edd16368SStephen M. Cameron 		goto clean1;
6881edd16368SStephen M. Cameron 
6882f79cfec6SStephen M. Cameron 	sprintf(h->devname, HPSA "%d", number_of_controllers);
6883edd16368SStephen M. Cameron 	h->ctlr = number_of_controllers;
6884edd16368SStephen M. Cameron 	number_of_controllers++;
6885edd16368SStephen M. Cameron 
6886edd16368SStephen M. Cameron 	/* configure PCI DMA stuff */
6887ecd9aad4SStephen M. Cameron 	rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
6888ecd9aad4SStephen M. Cameron 	if (rc == 0) {
6889edd16368SStephen M. Cameron 		dac = 1;
6890ecd9aad4SStephen M. Cameron 	} else {
6891ecd9aad4SStephen M. Cameron 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6892ecd9aad4SStephen M. Cameron 		if (rc == 0) {
6893edd16368SStephen M. Cameron 			dac = 0;
6894ecd9aad4SStephen M. Cameron 		} else {
6895edd16368SStephen M. Cameron 			dev_err(&pdev->dev, "no suitable DMA available\n");
6896edd16368SStephen M. Cameron 			goto clean1;
6897edd16368SStephen M. Cameron 		}
6898ecd9aad4SStephen M. Cameron 	}
6899edd16368SStephen M. Cameron 
6900edd16368SStephen M. Cameron 	/* make sure the board interrupts are off */
6901edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
690210f66018SStephen M. Cameron 
69039ee61794SRobert Elliott 	if (hpsa_request_irqs(h, do_hpsa_intr_msi, do_hpsa_intr_intx))
6904edd16368SStephen M. Cameron 		goto clean2;
6905303932fdSDon Brace 	dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
6906303932fdSDon Brace 	       h->devname, pdev->device,
6907a9a3a273SStephen M. Cameron 	       h->intr[h->intr_mode], dac ? "" : " not");
69088947fd10SRobert Elliott 	rc = hpsa_allocate_cmd_pool(h);
69098947fd10SRobert Elliott 	if (rc)
69108947fd10SRobert Elliott 		goto clean2_and_free_irqs;
691133a2ffceSStephen M. Cameron 	if (hpsa_allocate_sg_chain_blocks(h))
691233a2ffceSStephen M. Cameron 		goto clean4;
6913a08a8471SStephen M. Cameron 	init_waitqueue_head(&h->scan_wait_queue);
6914a08a8471SStephen M. Cameron 	h->scan_finished = 1; /* no scan currently in progress */
6915edd16368SStephen M. Cameron 
6916edd16368SStephen M. Cameron 	pci_set_drvdata(pdev, h);
69179a41338eSStephen M. Cameron 	h->ndevices = 0;
6918316b221aSStephen M. Cameron 	h->hba_mode_enabled = 0;
69199a41338eSStephen M. Cameron 	h->scsi_host = NULL;
69209a41338eSStephen M. Cameron 	spin_lock_init(&h->devlock);
692164670ac8SStephen M. Cameron 	hpsa_put_ctlr_into_performant_mode(h);
692264670ac8SStephen M. Cameron 
692364670ac8SStephen M. Cameron 	/* At this point, the controller is ready to take commands.
692464670ac8SStephen M. Cameron 	 * Now, if reset_devices and the hard reset didn't work, try
692564670ac8SStephen M. Cameron 	 * the soft reset and see if that works.
692664670ac8SStephen M. Cameron 	 */
692764670ac8SStephen M. Cameron 	if (try_soft_reset) {
692864670ac8SStephen M. Cameron 
692964670ac8SStephen M. Cameron 		/* This is kind of gross.  We may or may not get a completion
693064670ac8SStephen M. Cameron 		 * from the soft reset command, and if we do, then the value
693164670ac8SStephen M. Cameron 		 * from the fifo may or may not be valid.  So, we wait 10 secs
693264670ac8SStephen M. Cameron 		 * after the reset throwing away any completions we get during
693364670ac8SStephen M. Cameron 		 * that time.  Unregister the interrupt handler and register
693464670ac8SStephen M. Cameron 		 * fake ones to scoop up any residual completions.
693564670ac8SStephen M. Cameron 		 */
693664670ac8SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
693764670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
693864670ac8SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
6939ec501a18SRobert Elliott 		hpsa_free_irqs(h);
69409ee61794SRobert Elliott 		rc = hpsa_request_irqs(h, hpsa_msix_discard_completions,
694164670ac8SStephen M. Cameron 					hpsa_intx_discard_completions);
694264670ac8SStephen M. Cameron 		if (rc) {
69439ee61794SRobert Elliott 			dev_warn(&h->pdev->dev,
69449ee61794SRobert Elliott 				"Failed to request_irq after soft reset.\n");
694564670ac8SStephen M. Cameron 			goto clean4;
694664670ac8SStephen M. Cameron 		}
694764670ac8SStephen M. Cameron 
694864670ac8SStephen M. Cameron 		rc = hpsa_kdump_soft_reset(h);
694964670ac8SStephen M. Cameron 		if (rc)
695064670ac8SStephen M. Cameron 			/* Neither hard nor soft reset worked, we're hosed. */
695164670ac8SStephen M. Cameron 			goto clean4;
695264670ac8SStephen M. Cameron 
695364670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev, "Board READY.\n");
695464670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev,
695564670ac8SStephen M. Cameron 			"Waiting for stale completions to drain.\n");
695664670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_ON);
695764670ac8SStephen M. Cameron 		msleep(10000);
695864670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
695964670ac8SStephen M. Cameron 
696064670ac8SStephen M. Cameron 		rc = controller_reset_failed(h->cfgtable);
696164670ac8SStephen M. Cameron 		if (rc)
696264670ac8SStephen M. Cameron 			dev_info(&h->pdev->dev,
696364670ac8SStephen M. Cameron 				"Soft reset appears to have failed.\n");
696464670ac8SStephen M. Cameron 
696564670ac8SStephen M. Cameron 		/* since the controller's reset, we have to go back and re-init
696664670ac8SStephen M. Cameron 		 * everything.  Easiest to just forget what we've done and do it
696764670ac8SStephen M. Cameron 		 * all over again.
696864670ac8SStephen M. Cameron 		 */
696964670ac8SStephen M. Cameron 		hpsa_undo_allocations_after_kdump_soft_reset(h);
697064670ac8SStephen M. Cameron 		try_soft_reset = 0;
697164670ac8SStephen M. Cameron 		if (rc)
697264670ac8SStephen M. Cameron 			/* don't go to clean4, we already unallocated */
697364670ac8SStephen M. Cameron 			return -ENODEV;
697464670ac8SStephen M. Cameron 
697564670ac8SStephen M. Cameron 		goto reinit_after_soft_reset;
697664670ac8SStephen M. Cameron 	}
6977edd16368SStephen M. Cameron 
6978da0697bdSScott Teel 		/* Enable Accelerated IO path at driver layer */
6979da0697bdSScott Teel 		h->acciopath_status = 1;
6980da0697bdSScott Teel 
6981e863d68eSScott Teel 	h->drv_req_rescan = 0;
6982e863d68eSScott Teel 
6983edd16368SStephen M. Cameron 	/* Turn the interrupts on so we can service requests */
6984edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_ON);
6985edd16368SStephen M. Cameron 
6986339b2b14SStephen M. Cameron 	hpsa_hba_inquiry(h);
6987edd16368SStephen M. Cameron 	hpsa_register_scsi(h);	/* hook ourselves into SCSI subsystem */
69888a98db73SStephen M. Cameron 
69898a98db73SStephen M. Cameron 	/* Monitor the controller for firmware lockups */
69908a98db73SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
69918a98db73SStephen M. Cameron 	INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
69928a98db73SStephen M. Cameron 	schedule_delayed_work(&h->monitor_ctlr_work,
69938a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
699488bf6d62SStephen M. Cameron 	return 0;
6995edd16368SStephen M. Cameron 
6996edd16368SStephen M. Cameron clean4:
699733a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
69982e9d1b36SStephen M. Cameron 	hpsa_free_cmd_pool(h);
69998947fd10SRobert Elliott clean2_and_free_irqs:
7000ec501a18SRobert Elliott 	hpsa_free_irqs(h);
7001edd16368SStephen M. Cameron clean2:
7002edd16368SStephen M. Cameron clean1:
7003094963daSStephen M. Cameron 	if (h->lockup_detected)
7004094963daSStephen M. Cameron 		free_percpu(h->lockup_detected);
7005edd16368SStephen M. Cameron 	kfree(h);
7006ecd9aad4SStephen M. Cameron 	return rc;
7007edd16368SStephen M. Cameron }
7008edd16368SStephen M. Cameron 
7009edd16368SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h)
7010edd16368SStephen M. Cameron {
7011edd16368SStephen M. Cameron 	char *flush_buf;
7012edd16368SStephen M. Cameron 	struct CommandList *c;
7013702890e3SStephen M. Cameron 
7014702890e3SStephen M. Cameron 	/* Don't bother trying to flush the cache if locked up */
7015094963daSStephen M. Cameron 	if (unlikely(lockup_detected(h)))
7016702890e3SStephen M. Cameron 		return;
7017edd16368SStephen M. Cameron 	flush_buf = kzalloc(4, GFP_KERNEL);
7018edd16368SStephen M. Cameron 	if (!flush_buf)
7019edd16368SStephen M. Cameron 		return;
7020edd16368SStephen M. Cameron 
7021*45fcb86eSStephen Cameron 	c = cmd_alloc(h);
7022edd16368SStephen M. Cameron 	if (!c) {
7023*45fcb86eSStephen Cameron 		dev_warn(&h->pdev->dev, "cmd_alloc returned NULL!\n");
7024edd16368SStephen M. Cameron 		goto out_of_memory;
7025edd16368SStephen M. Cameron 	}
7026a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
7027a2dac136SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_CMD)) {
7028a2dac136SStephen M. Cameron 		goto out;
7029a2dac136SStephen M. Cameron 	}
7030edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
7031edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus != 0)
7032a2dac136SStephen M. Cameron out:
7033edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
7034edd16368SStephen M. Cameron 			"error flushing cache on controller\n");
7035*45fcb86eSStephen Cameron 	cmd_free(h, c);
7036edd16368SStephen M. Cameron out_of_memory:
7037edd16368SStephen M. Cameron 	kfree(flush_buf);
7038edd16368SStephen M. Cameron }
7039edd16368SStephen M. Cameron 
7040edd16368SStephen M. Cameron static void hpsa_shutdown(struct pci_dev *pdev)
7041edd16368SStephen M. Cameron {
7042edd16368SStephen M. Cameron 	struct ctlr_info *h;
7043edd16368SStephen M. Cameron 
7044edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
7045edd16368SStephen M. Cameron 	/* Turn board interrupts off  and send the flush cache command
7046edd16368SStephen M. Cameron 	 * sendcmd will turn off interrupt, and send the flush...
7047edd16368SStephen M. Cameron 	 * To write all data in the battery backed cache to disks
7048edd16368SStephen M. Cameron 	 */
7049edd16368SStephen M. Cameron 	hpsa_flush_cache(h);
7050edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
70510097f0f4SStephen M. Cameron 	hpsa_free_irqs_and_disable_msix(h);
7052edd16368SStephen M. Cameron }
7053edd16368SStephen M. Cameron 
70546f039790SGreg Kroah-Hartman static void hpsa_free_device_info(struct ctlr_info *h)
705555e14e76SStephen M. Cameron {
705655e14e76SStephen M. Cameron 	int i;
705755e14e76SStephen M. Cameron 
705855e14e76SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++)
705955e14e76SStephen M. Cameron 		kfree(h->dev[i]);
706055e14e76SStephen M. Cameron }
706155e14e76SStephen M. Cameron 
70626f039790SGreg Kroah-Hartman static void hpsa_remove_one(struct pci_dev *pdev)
7063edd16368SStephen M. Cameron {
7064edd16368SStephen M. Cameron 	struct ctlr_info *h;
70658a98db73SStephen M. Cameron 	unsigned long flags;
7066edd16368SStephen M. Cameron 
7067edd16368SStephen M. Cameron 	if (pci_get_drvdata(pdev) == NULL) {
7068edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "unable to remove device\n");
7069edd16368SStephen M. Cameron 		return;
7070edd16368SStephen M. Cameron 	}
7071edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
70728a98db73SStephen M. Cameron 
70738a98db73SStephen M. Cameron 	/* Get rid of any controller monitoring work items */
70748a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
70758a98db73SStephen M. Cameron 	h->remove_in_progress = 1;
70768a98db73SStephen M. Cameron 	cancel_delayed_work(&h->monitor_ctlr_work);
70778a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
70788a98db73SStephen M. Cameron 
7079edd16368SStephen M. Cameron 	hpsa_unregister_scsi(h);	/* unhook from SCSI subsystem */
7080edd16368SStephen M. Cameron 	hpsa_shutdown(pdev);
7081edd16368SStephen M. Cameron 	iounmap(h->vaddr);
7082204892e9SStephen M. Cameron 	iounmap(h->transtable);
7083204892e9SStephen M. Cameron 	iounmap(h->cfgtable);
708455e14e76SStephen M. Cameron 	hpsa_free_device_info(h);
708533a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
7086edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev,
7087edd16368SStephen M. Cameron 		h->nr_cmds * sizeof(struct CommandList),
7088edd16368SStephen M. Cameron 		h->cmd_pool, h->cmd_pool_dhandle);
7089edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev,
7090edd16368SStephen M. Cameron 		h->nr_cmds * sizeof(struct ErrorInfo),
7091edd16368SStephen M. Cameron 		h->errinfo_pool, h->errinfo_pool_dhandle);
7092072b0518SStephen M. Cameron 	hpsa_free_reply_queues(h);
7093edd16368SStephen M. Cameron 	kfree(h->cmd_pool_bits);
7094303932fdSDon Brace 	kfree(h->blockFetchTable);
7095e1f7de0cSMatt Gates 	kfree(h->ioaccel1_blockFetchTable);
7096aca9012aSStephen M. Cameron 	kfree(h->ioaccel2_blockFetchTable);
7097339b2b14SStephen M. Cameron 	kfree(h->hba_inquiry_data);
7098f0bd0b68SStephen M. Cameron 	pci_disable_device(pdev);
7099edd16368SStephen M. Cameron 	pci_release_regions(pdev);
7100094963daSStephen M. Cameron 	free_percpu(h->lockup_detected);
7101edd16368SStephen M. Cameron 	kfree(h);
7102edd16368SStephen M. Cameron }
7103edd16368SStephen M. Cameron 
7104edd16368SStephen M. Cameron static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
7105edd16368SStephen M. Cameron 	__attribute__((unused)) pm_message_t state)
7106edd16368SStephen M. Cameron {
7107edd16368SStephen M. Cameron 	return -ENOSYS;
7108edd16368SStephen M. Cameron }
7109edd16368SStephen M. Cameron 
7110edd16368SStephen M. Cameron static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
7111edd16368SStephen M. Cameron {
7112edd16368SStephen M. Cameron 	return -ENOSYS;
7113edd16368SStephen M. Cameron }
7114edd16368SStephen M. Cameron 
7115edd16368SStephen M. Cameron static struct pci_driver hpsa_pci_driver = {
7116f79cfec6SStephen M. Cameron 	.name = HPSA,
7117edd16368SStephen M. Cameron 	.probe = hpsa_init_one,
71186f039790SGreg Kroah-Hartman 	.remove = hpsa_remove_one,
7119edd16368SStephen M. Cameron 	.id_table = hpsa_pci_device_id,	/* id_table */
7120edd16368SStephen M. Cameron 	.shutdown = hpsa_shutdown,
7121edd16368SStephen M. Cameron 	.suspend = hpsa_suspend,
7122edd16368SStephen M. Cameron 	.resume = hpsa_resume,
7123edd16368SStephen M. Cameron };
7124edd16368SStephen M. Cameron 
7125303932fdSDon Brace /* Fill in bucket_map[], given nsgs (the max number of
7126303932fdSDon Brace  * scatter gather elements supported) and bucket[],
7127303932fdSDon Brace  * which is an array of 8 integers.  The bucket[] array
7128303932fdSDon Brace  * contains 8 different DMA transfer sizes (in 16
7129303932fdSDon Brace  * byte increments) which the controller uses to fetch
7130303932fdSDon Brace  * commands.  This function fills in bucket_map[], which
7131303932fdSDon Brace  * maps a given number of scatter gather elements to one of
7132303932fdSDon Brace  * the 8 DMA transfer sizes.  The point of it is to allow the
7133303932fdSDon Brace  * controller to only do as much DMA as needed to fetch the
7134303932fdSDon Brace  * command, with the DMA transfer size encoded in the lower
7135303932fdSDon Brace  * bits of the command address.
7136303932fdSDon Brace  */
7137303932fdSDon Brace static void  calc_bucket_map(int bucket[], int num_buckets,
71382b08b3e9SDon Brace 	int nsgs, int min_blocks, u32 *bucket_map)
7139303932fdSDon Brace {
7140303932fdSDon Brace 	int i, j, b, size;
7141303932fdSDon Brace 
7142303932fdSDon Brace 	/* Note, bucket_map must have nsgs+1 entries. */
7143303932fdSDon Brace 	for (i = 0; i <= nsgs; i++) {
7144303932fdSDon Brace 		/* Compute size of a command with i SG entries */
7145e1f7de0cSMatt Gates 		size = i + min_blocks;
7146303932fdSDon Brace 		b = num_buckets; /* Assume the biggest bucket */
7147303932fdSDon Brace 		/* Find the bucket that is just big enough */
7148e1f7de0cSMatt Gates 		for (j = 0; j < num_buckets; j++) {
7149303932fdSDon Brace 			if (bucket[j] >= size) {
7150303932fdSDon Brace 				b = j;
7151303932fdSDon Brace 				break;
7152303932fdSDon Brace 			}
7153303932fdSDon Brace 		}
7154303932fdSDon Brace 		/* for a command with i SG entries, use bucket b. */
7155303932fdSDon Brace 		bucket_map[i] = b;
7156303932fdSDon Brace 	}
7157303932fdSDon Brace }
7158303932fdSDon Brace 
7159e1f7de0cSMatt Gates static void hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
7160303932fdSDon Brace {
71616c311b57SStephen M. Cameron 	int i;
71626c311b57SStephen M. Cameron 	unsigned long register_value;
7163e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
7164e1f7de0cSMatt Gates 			(trans_support & CFGTBL_Trans_use_short_tags) |
7165e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix |
7166b9af4937SStephen M. Cameron 			(trans_support & (CFGTBL_Trans_io_accel1 |
7167b9af4937SStephen M. Cameron 				CFGTBL_Trans_io_accel2));
7168e1f7de0cSMatt Gates 	struct access_method access = SA5_performant_access;
7169def342bdSStephen M. Cameron 
7170def342bdSStephen M. Cameron 	/* This is a bit complicated.  There are 8 registers on
7171def342bdSStephen M. Cameron 	 * the controller which we write to to tell it 8 different
7172def342bdSStephen M. Cameron 	 * sizes of commands which there may be.  It's a way of
7173def342bdSStephen M. Cameron 	 * reducing the DMA done to fetch each command.  Encoded into
7174def342bdSStephen M. Cameron 	 * each command's tag are 3 bits which communicate to the controller
7175def342bdSStephen M. Cameron 	 * which of the eight sizes that command fits within.  The size of
7176def342bdSStephen M. Cameron 	 * each command depends on how many scatter gather entries there are.
7177def342bdSStephen M. Cameron 	 * Each SG entry requires 16 bytes.  The eight registers are programmed
7178def342bdSStephen M. Cameron 	 * with the number of 16-byte blocks a command of that size requires.
7179def342bdSStephen M. Cameron 	 * The smallest command possible requires 5 such 16 byte blocks.
7180d66ae08bSStephen M. Cameron 	 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
7181def342bdSStephen M. Cameron 	 * blocks.  Note, this only extends to the SG entries contained
7182def342bdSStephen M. Cameron 	 * within the command block, and does not extend to chained blocks
7183def342bdSStephen M. Cameron 	 * of SG elements.   bft[] contains the eight values we write to
7184def342bdSStephen M. Cameron 	 * the registers.  They are not evenly distributed, but have more
7185def342bdSStephen M. Cameron 	 * sizes for small commands, and fewer sizes for larger commands.
7186def342bdSStephen M. Cameron 	 */
7187d66ae08bSStephen M. Cameron 	int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
7188b9af4937SStephen M. Cameron #define MIN_IOACCEL2_BFT_ENTRY 5
7189b9af4937SStephen M. Cameron #define HPSA_IOACCEL2_HEADER_SZ 4
7190b9af4937SStephen M. Cameron 	int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
7191b9af4937SStephen M. Cameron 			13, 14, 15, 16, 17, 18, 19,
7192b9af4937SStephen M. Cameron 			HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
7193b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
7194b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
7195b9af4937SStephen M. Cameron 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
7196b9af4937SStephen M. Cameron 				 16 * MIN_IOACCEL2_BFT_ENTRY);
7197b9af4937SStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
7198d66ae08bSStephen M. Cameron 	BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
7199303932fdSDon Brace 	/*  5 = 1 s/g entry or 4k
7200303932fdSDon Brace 	 *  6 = 2 s/g entry or 8k
7201303932fdSDon Brace 	 *  8 = 4 s/g entry or 16k
7202303932fdSDon Brace 	 * 10 = 6 s/g entry or 24k
7203303932fdSDon Brace 	 */
7204303932fdSDon Brace 
7205b3a52e79SStephen M. Cameron 	/* If the controller supports either ioaccel method then
7206b3a52e79SStephen M. Cameron 	 * we can also use the RAID stack submit path that does not
7207b3a52e79SStephen M. Cameron 	 * perform the superfluous readl() after each command submission.
7208b3a52e79SStephen M. Cameron 	 */
7209b3a52e79SStephen M. Cameron 	if (trans_support & (CFGTBL_Trans_io_accel1 | CFGTBL_Trans_io_accel2))
7210b3a52e79SStephen M. Cameron 		access = SA5_performant_access_no_read;
7211b3a52e79SStephen M. Cameron 
7212303932fdSDon Brace 	/* Controller spec: zero out this buffer. */
7213072b0518SStephen M. Cameron 	for (i = 0; i < h->nreply_queues; i++)
7214072b0518SStephen M. Cameron 		memset(h->reply_queue[i].head, 0, h->reply_queue_size);
7215303932fdSDon Brace 
7216d66ae08bSStephen M. Cameron 	bft[7] = SG_ENTRIES_IN_CMD + 4;
7217d66ae08bSStephen M. Cameron 	calc_bucket_map(bft, ARRAY_SIZE(bft),
7218e1f7de0cSMatt Gates 				SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
7219303932fdSDon Brace 	for (i = 0; i < 8; i++)
7220303932fdSDon Brace 		writel(bft[i], &h->transtable->BlockFetch[i]);
7221303932fdSDon Brace 
7222303932fdSDon Brace 	/* size of controller ring buffer */
7223303932fdSDon Brace 	writel(h->max_commands, &h->transtable->RepQSize);
7224254f796bSMatt Gates 	writel(h->nreply_queues, &h->transtable->RepQCount);
7225303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrLow32);
7226303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrHigh32);
7227254f796bSMatt Gates 
7228254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
7229254f796bSMatt Gates 		writel(0, &h->transtable->RepQAddr[i].upper);
7230072b0518SStephen M. Cameron 		writel(h->reply_queue[i].busaddr,
7231254f796bSMatt Gates 			&h->transtable->RepQAddr[i].lower);
7232254f796bSMatt Gates 	}
7233254f796bSMatt Gates 
7234b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
7235e1f7de0cSMatt Gates 	writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
7236e1f7de0cSMatt Gates 	/*
7237e1f7de0cSMatt Gates 	 * enable outbound interrupt coalescing in accelerator mode;
7238e1f7de0cSMatt Gates 	 */
7239e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
7240e1f7de0cSMatt Gates 		access = SA5_ioaccel_mode1_access;
7241e1f7de0cSMatt Gates 		writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
7242e1f7de0cSMatt Gates 		writel(4, &h->cfgtable->HostWrite.CoalIntCount);
7243c349775eSScott Teel 	} else {
7244c349775eSScott Teel 		if (trans_support & CFGTBL_Trans_io_accel2) {
7245c349775eSScott Teel 			access = SA5_ioaccel_mode2_access;
7246c349775eSScott Teel 			writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
7247c349775eSScott Teel 			writel(4, &h->cfgtable->HostWrite.CoalIntCount);
7248c349775eSScott Teel 		}
7249e1f7de0cSMatt Gates 	}
7250303932fdSDon Brace 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
72513f4336f3SStephen M. Cameron 	hpsa_wait_for_mode_change_ack(h);
7252303932fdSDon Brace 	register_value = readl(&(h->cfgtable->TransportActive));
7253303932fdSDon Brace 	if (!(register_value & CFGTBL_Trans_Performant)) {
7254050f7147SStephen Cameron 		dev_err(&h->pdev->dev,
7255050f7147SStephen Cameron 			"performant mode problem - transport not active\n");
7256303932fdSDon Brace 		return;
7257303932fdSDon Brace 	}
7258960a30e7SStephen M. Cameron 	/* Change the access methods to the performant access methods */
7259e1f7de0cSMatt Gates 	h->access = access;
7260e1f7de0cSMatt Gates 	h->transMethod = transMethod;
7261e1f7de0cSMatt Gates 
7262b9af4937SStephen M. Cameron 	if (!((trans_support & CFGTBL_Trans_io_accel1) ||
7263b9af4937SStephen M. Cameron 		(trans_support & CFGTBL_Trans_io_accel2)))
7264e1f7de0cSMatt Gates 		return;
7265e1f7de0cSMatt Gates 
7266b9af4937SStephen M. Cameron 	if (trans_support & CFGTBL_Trans_io_accel1) {
7267e1f7de0cSMatt Gates 		/* Set up I/O accelerator mode */
7268e1f7de0cSMatt Gates 		for (i = 0; i < h->nreply_queues; i++) {
7269e1f7de0cSMatt Gates 			writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
7270e1f7de0cSMatt Gates 			h->reply_queue[i].current_entry =
7271e1f7de0cSMatt Gates 				readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
7272e1f7de0cSMatt Gates 		}
7273283b4a9bSStephen M. Cameron 		bft[7] = h->ioaccel_maxsg + 8;
7274283b4a9bSStephen M. Cameron 		calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
7275e1f7de0cSMatt Gates 				h->ioaccel1_blockFetchTable);
7276e1f7de0cSMatt Gates 
7277e1f7de0cSMatt Gates 		/* initialize all reply queue entries to unused */
7278072b0518SStephen M. Cameron 		for (i = 0; i < h->nreply_queues; i++)
7279072b0518SStephen M. Cameron 			memset(h->reply_queue[i].head,
7280072b0518SStephen M. Cameron 				(u8) IOACCEL_MODE1_REPLY_UNUSED,
7281072b0518SStephen M. Cameron 				h->reply_queue_size);
7282e1f7de0cSMatt Gates 
7283e1f7de0cSMatt Gates 		/* set all the constant fields in the accelerator command
7284e1f7de0cSMatt Gates 		 * frames once at init time to save CPU cycles later.
7285e1f7de0cSMatt Gates 		 */
7286e1f7de0cSMatt Gates 		for (i = 0; i < h->nr_cmds; i++) {
7287e1f7de0cSMatt Gates 			struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
7288e1f7de0cSMatt Gates 
7289e1f7de0cSMatt Gates 			cp->function = IOACCEL1_FUNCTION_SCSIIO;
7290e1f7de0cSMatt Gates 			cp->err_info = (u32) (h->errinfo_pool_dhandle +
7291e1f7de0cSMatt Gates 					(i * sizeof(struct ErrorInfo)));
7292e1f7de0cSMatt Gates 			cp->err_info_len = sizeof(struct ErrorInfo);
7293e1f7de0cSMatt Gates 			cp->sgl_offset = IOACCEL1_SGLOFFSET;
72942b08b3e9SDon Brace 			cp->host_context_flags =
72952b08b3e9SDon Brace 				cpu_to_le16(IOACCEL1_HCFLAGS_CISS_FORMAT);
7296e1f7de0cSMatt Gates 			cp->timeout_sec = 0;
7297e1f7de0cSMatt Gates 			cp->ReplyQueue = 0;
729850a0decfSStephen M. Cameron 			cp->tag =
729950a0decfSStephen M. Cameron 				cpu_to_le64((i << DIRECT_LOOKUP_SHIFT) |
730050a0decfSStephen M. Cameron 						DIRECT_LOOKUP_BIT);
730150a0decfSStephen M. Cameron 			cp->host_addr =
730250a0decfSStephen M. Cameron 				cpu_to_le64(h->ioaccel_cmd_pool_dhandle +
7303e1f7de0cSMatt Gates 					(i * sizeof(struct io_accel1_cmd)));
7304e1f7de0cSMatt Gates 		}
7305b9af4937SStephen M. Cameron 	} else if (trans_support & CFGTBL_Trans_io_accel2) {
7306b9af4937SStephen M. Cameron 		u64 cfg_offset, cfg_base_addr_index;
7307b9af4937SStephen M. Cameron 		u32 bft2_offset, cfg_base_addr;
7308b9af4937SStephen M. Cameron 		int rc;
7309b9af4937SStephen M. Cameron 
7310b9af4937SStephen M. Cameron 		rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
7311b9af4937SStephen M. Cameron 			&cfg_base_addr_index, &cfg_offset);
7312b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
7313b9af4937SStephen M. Cameron 		bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
7314b9af4937SStephen M. Cameron 		calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
7315b9af4937SStephen M. Cameron 				4, h->ioaccel2_blockFetchTable);
7316b9af4937SStephen M. Cameron 		bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
7317b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct CfgTable,
7318b9af4937SStephen M. Cameron 				io_accel_request_size_offset) != 0xb8);
7319b9af4937SStephen M. Cameron 		h->ioaccel2_bft2_regs =
7320b9af4937SStephen M. Cameron 			remap_pci_mem(pci_resource_start(h->pdev,
7321b9af4937SStephen M. Cameron 					cfg_base_addr_index) +
7322b9af4937SStephen M. Cameron 					cfg_offset + bft2_offset,
7323b9af4937SStephen M. Cameron 					ARRAY_SIZE(bft2) *
7324b9af4937SStephen M. Cameron 					sizeof(*h->ioaccel2_bft2_regs));
7325b9af4937SStephen M. Cameron 		for (i = 0; i < ARRAY_SIZE(bft2); i++)
7326b9af4937SStephen M. Cameron 			writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
7327b9af4937SStephen M. Cameron 	}
7328b9af4937SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
7329b9af4937SStephen M. Cameron 	hpsa_wait_for_mode_change_ack(h);
7330e1f7de0cSMatt Gates }
7331e1f7de0cSMatt Gates 
7332e1f7de0cSMatt Gates static int hpsa_alloc_ioaccel_cmd_and_bft(struct ctlr_info *h)
7333e1f7de0cSMatt Gates {
7334283b4a9bSStephen M. Cameron 	h->ioaccel_maxsg =
7335283b4a9bSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
7336283b4a9bSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
7337283b4a9bSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
7338283b4a9bSStephen M. Cameron 
7339e1f7de0cSMatt Gates 	/* Command structures must be aligned on a 128-byte boundary
7340e1f7de0cSMatt Gates 	 * because the 7 lower bits of the address are used by the
7341e1f7de0cSMatt Gates 	 * hardware.
7342e1f7de0cSMatt Gates 	 */
7343e1f7de0cSMatt Gates 	BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
7344e1f7de0cSMatt Gates 			IOACCEL1_COMMANDLIST_ALIGNMENT);
7345e1f7de0cSMatt Gates 	h->ioaccel_cmd_pool =
7346e1f7de0cSMatt Gates 		pci_alloc_consistent(h->pdev,
7347e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
7348e1f7de0cSMatt Gates 			&(h->ioaccel_cmd_pool_dhandle));
7349e1f7de0cSMatt Gates 
7350e1f7de0cSMatt Gates 	h->ioaccel1_blockFetchTable =
7351283b4a9bSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
7352e1f7de0cSMatt Gates 				sizeof(u32)), GFP_KERNEL);
7353e1f7de0cSMatt Gates 
7354e1f7de0cSMatt Gates 	if ((h->ioaccel_cmd_pool == NULL) ||
7355e1f7de0cSMatt Gates 		(h->ioaccel1_blockFetchTable == NULL))
7356e1f7de0cSMatt Gates 		goto clean_up;
7357e1f7de0cSMatt Gates 
7358e1f7de0cSMatt Gates 	memset(h->ioaccel_cmd_pool, 0,
7359e1f7de0cSMatt Gates 		h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
7360e1f7de0cSMatt Gates 	return 0;
7361e1f7de0cSMatt Gates 
7362e1f7de0cSMatt Gates clean_up:
7363e1f7de0cSMatt Gates 	if (h->ioaccel_cmd_pool)
7364e1f7de0cSMatt Gates 		pci_free_consistent(h->pdev,
7365e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
7366e1f7de0cSMatt Gates 			h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
7367e1f7de0cSMatt Gates 	kfree(h->ioaccel1_blockFetchTable);
7368e1f7de0cSMatt Gates 	return 1;
73696c311b57SStephen M. Cameron }
73706c311b57SStephen M. Cameron 
7371aca9012aSStephen M. Cameron static int ioaccel2_alloc_cmds_and_bft(struct ctlr_info *h)
7372aca9012aSStephen M. Cameron {
7373aca9012aSStephen M. Cameron 	/* Allocate ioaccel2 mode command blocks and block fetch table */
7374aca9012aSStephen M. Cameron 
7375aca9012aSStephen M. Cameron 	h->ioaccel_maxsg =
7376aca9012aSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
7377aca9012aSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
7378aca9012aSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
7379aca9012aSStephen M. Cameron 
7380aca9012aSStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
7381aca9012aSStephen M. Cameron 			IOACCEL2_COMMANDLIST_ALIGNMENT);
7382aca9012aSStephen M. Cameron 	h->ioaccel2_cmd_pool =
7383aca9012aSStephen M. Cameron 		pci_alloc_consistent(h->pdev,
7384aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
7385aca9012aSStephen M. Cameron 			&(h->ioaccel2_cmd_pool_dhandle));
7386aca9012aSStephen M. Cameron 
7387aca9012aSStephen M. Cameron 	h->ioaccel2_blockFetchTable =
7388aca9012aSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
7389aca9012aSStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
7390aca9012aSStephen M. Cameron 
7391aca9012aSStephen M. Cameron 	if ((h->ioaccel2_cmd_pool == NULL) ||
7392aca9012aSStephen M. Cameron 		(h->ioaccel2_blockFetchTable == NULL))
7393aca9012aSStephen M. Cameron 		goto clean_up;
7394aca9012aSStephen M. Cameron 
7395aca9012aSStephen M. Cameron 	memset(h->ioaccel2_cmd_pool, 0,
7396aca9012aSStephen M. Cameron 		h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
7397aca9012aSStephen M. Cameron 	return 0;
7398aca9012aSStephen M. Cameron 
7399aca9012aSStephen M. Cameron clean_up:
7400aca9012aSStephen M. Cameron 	if (h->ioaccel2_cmd_pool)
7401aca9012aSStephen M. Cameron 		pci_free_consistent(h->pdev,
7402aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
7403aca9012aSStephen M. Cameron 			h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
7404aca9012aSStephen M. Cameron 	kfree(h->ioaccel2_blockFetchTable);
7405aca9012aSStephen M. Cameron 	return 1;
7406aca9012aSStephen M. Cameron }
7407aca9012aSStephen M. Cameron 
74086f039790SGreg Kroah-Hartman static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
74096c311b57SStephen M. Cameron {
74106c311b57SStephen M. Cameron 	u32 trans_support;
7411e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
7412e1f7de0cSMatt Gates 					CFGTBL_Trans_use_short_tags;
7413254f796bSMatt Gates 	int i;
74146c311b57SStephen M. Cameron 
741502ec19c8SStephen M. Cameron 	if (hpsa_simple_mode)
741602ec19c8SStephen M. Cameron 		return;
741702ec19c8SStephen M. Cameron 
741867c99a72Sscameron@beardog.cce.hp.com 	trans_support = readl(&(h->cfgtable->TransportSupport));
741967c99a72Sscameron@beardog.cce.hp.com 	if (!(trans_support & PERFORMANT_MODE))
742067c99a72Sscameron@beardog.cce.hp.com 		return;
742167c99a72Sscameron@beardog.cce.hp.com 
7422e1f7de0cSMatt Gates 	/* Check for I/O accelerator mode support */
7423e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
7424e1f7de0cSMatt Gates 		transMethod |= CFGTBL_Trans_io_accel1 |
7425e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix;
7426e1f7de0cSMatt Gates 		if (hpsa_alloc_ioaccel_cmd_and_bft(h))
7427e1f7de0cSMatt Gates 			goto clean_up;
7428aca9012aSStephen M. Cameron 	} else {
7429aca9012aSStephen M. Cameron 		if (trans_support & CFGTBL_Trans_io_accel2) {
7430aca9012aSStephen M. Cameron 				transMethod |= CFGTBL_Trans_io_accel2 |
7431aca9012aSStephen M. Cameron 				CFGTBL_Trans_enable_directed_msix;
7432aca9012aSStephen M. Cameron 		if (ioaccel2_alloc_cmds_and_bft(h))
7433aca9012aSStephen M. Cameron 			goto clean_up;
7434aca9012aSStephen M. Cameron 		}
7435e1f7de0cSMatt Gates 	}
7436e1f7de0cSMatt Gates 
7437eee0f03aSHannes Reinecke 	h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
7438cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
74396c311b57SStephen M. Cameron 	/* Performant mode ring buffer and supporting data structures */
7440072b0518SStephen M. Cameron 	h->reply_queue_size = h->max_commands * sizeof(u64);
74416c311b57SStephen M. Cameron 
7442254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
7443072b0518SStephen M. Cameron 		h->reply_queue[i].head = pci_alloc_consistent(h->pdev,
7444072b0518SStephen M. Cameron 						h->reply_queue_size,
7445072b0518SStephen M. Cameron 						&(h->reply_queue[i].busaddr));
7446072b0518SStephen M. Cameron 		if (!h->reply_queue[i].head)
7447072b0518SStephen M. Cameron 			goto clean_up;
7448254f796bSMatt Gates 		h->reply_queue[i].size = h->max_commands;
7449254f796bSMatt Gates 		h->reply_queue[i].wraparound = 1;  /* spec: init to 1 */
7450254f796bSMatt Gates 		h->reply_queue[i].current_entry = 0;
7451254f796bSMatt Gates 	}
7452254f796bSMatt Gates 
74536c311b57SStephen M. Cameron 	/* Need a block fetch table for performant mode */
7454d66ae08bSStephen M. Cameron 	h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
74556c311b57SStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
7456072b0518SStephen M. Cameron 	if (!h->blockFetchTable)
74576c311b57SStephen M. Cameron 		goto clean_up;
74586c311b57SStephen M. Cameron 
7459e1f7de0cSMatt Gates 	hpsa_enter_performant_mode(h, trans_support);
7460303932fdSDon Brace 	return;
7461303932fdSDon Brace 
7462303932fdSDon Brace clean_up:
7463072b0518SStephen M. Cameron 	hpsa_free_reply_queues(h);
7464303932fdSDon Brace 	kfree(h->blockFetchTable);
7465303932fdSDon Brace }
7466303932fdSDon Brace 
746723100dd9SStephen M. Cameron static int is_accelerated_cmd(struct CommandList *c)
746876438d08SStephen M. Cameron {
746923100dd9SStephen M. Cameron 	return c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_IOACCEL2;
747023100dd9SStephen M. Cameron }
747123100dd9SStephen M. Cameron 
747223100dd9SStephen M. Cameron static void hpsa_drain_accel_commands(struct ctlr_info *h)
747323100dd9SStephen M. Cameron {
747423100dd9SStephen M. Cameron 	struct CommandList *c = NULL;
747576438d08SStephen M. Cameron 	unsigned long flags;
747623100dd9SStephen M. Cameron 	int accel_cmds_out;
747776438d08SStephen M. Cameron 
747876438d08SStephen M. Cameron 	do { /* wait for all outstanding commands to drain out */
747923100dd9SStephen M. Cameron 		accel_cmds_out = 0;
748076438d08SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
748123100dd9SStephen M. Cameron 		list_for_each_entry(c, &h->cmpQ, list)
748223100dd9SStephen M. Cameron 			accel_cmds_out += is_accelerated_cmd(c);
748323100dd9SStephen M. Cameron 		list_for_each_entry(c, &h->reqQ, list)
748423100dd9SStephen M. Cameron 			accel_cmds_out += is_accelerated_cmd(c);
748576438d08SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
748623100dd9SStephen M. Cameron 		if (accel_cmds_out <= 0)
748776438d08SStephen M. Cameron 			break;
748876438d08SStephen M. Cameron 		msleep(100);
748976438d08SStephen M. Cameron 	} while (1);
749076438d08SStephen M. Cameron }
749176438d08SStephen M. Cameron 
7492edd16368SStephen M. Cameron /*
7493edd16368SStephen M. Cameron  *  This is it.  Register the PCI driver information for the cards we control
7494edd16368SStephen M. Cameron  *  the OS will call our registered routines when it finds one of our cards.
7495edd16368SStephen M. Cameron  */
7496edd16368SStephen M. Cameron static int __init hpsa_init(void)
7497edd16368SStephen M. Cameron {
749831468401SMike Miller 	return pci_register_driver(&hpsa_pci_driver);
7499edd16368SStephen M. Cameron }
7500edd16368SStephen M. Cameron 
7501edd16368SStephen M. Cameron static void __exit hpsa_cleanup(void)
7502edd16368SStephen M. Cameron {
7503edd16368SStephen M. Cameron 	pci_unregister_driver(&hpsa_pci_driver);
7504edd16368SStephen M. Cameron }
7505edd16368SStephen M. Cameron 
7506e1f7de0cSMatt Gates static void __attribute__((unused)) verify_offsets(void)
7507e1f7de0cSMatt Gates {
7508e1f7de0cSMatt Gates #define VERIFY_OFFSET(member, offset) \
7509dd0e19f3SScott Teel 	BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
7510dd0e19f3SScott Teel 
7511dd0e19f3SScott Teel 	VERIFY_OFFSET(structure_size, 0);
7512dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_size, 4);
7513dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_cnt, 8);
7514dd0e19f3SScott Teel 	VERIFY_OFFSET(phys_blk_shift, 16);
7515dd0e19f3SScott Teel 	VERIFY_OFFSET(parity_rotation_shift, 17);
7516dd0e19f3SScott Teel 	VERIFY_OFFSET(strip_size, 18);
7517dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_starting_blk, 20);
7518dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_blk_cnt, 28);
7519dd0e19f3SScott Teel 	VERIFY_OFFSET(data_disks_per_row, 36);
7520dd0e19f3SScott Teel 	VERIFY_OFFSET(metadata_disks_per_row, 38);
7521dd0e19f3SScott Teel 	VERIFY_OFFSET(row_cnt, 40);
7522dd0e19f3SScott Teel 	VERIFY_OFFSET(layout_map_count, 42);
7523dd0e19f3SScott Teel 	VERIFY_OFFSET(flags, 44);
7524dd0e19f3SScott Teel 	VERIFY_OFFSET(dekindex, 46);
7525dd0e19f3SScott Teel 	/* VERIFY_OFFSET(reserved, 48 */
7526dd0e19f3SScott Teel 	VERIFY_OFFSET(data, 64);
7527dd0e19f3SScott Teel 
7528dd0e19f3SScott Teel #undef VERIFY_OFFSET
7529dd0e19f3SScott Teel 
7530dd0e19f3SScott Teel #define VERIFY_OFFSET(member, offset) \
7531b66cc250SMike Miller 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
7532b66cc250SMike Miller 
7533b66cc250SMike Miller 	VERIFY_OFFSET(IU_type, 0);
7534b66cc250SMike Miller 	VERIFY_OFFSET(direction, 1);
7535b66cc250SMike Miller 	VERIFY_OFFSET(reply_queue, 2);
7536b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved1, 3);  */
7537b66cc250SMike Miller 	VERIFY_OFFSET(scsi_nexus, 4);
7538b66cc250SMike Miller 	VERIFY_OFFSET(Tag, 8);
7539b66cc250SMike Miller 	VERIFY_OFFSET(cdb, 16);
7540b66cc250SMike Miller 	VERIFY_OFFSET(cciss_lun, 32);
7541b66cc250SMike Miller 	VERIFY_OFFSET(data_len, 40);
7542b66cc250SMike Miller 	VERIFY_OFFSET(cmd_priority_task_attr, 44);
7543b66cc250SMike Miller 	VERIFY_OFFSET(sg_count, 45);
7544b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved3 */
7545b66cc250SMike Miller 	VERIFY_OFFSET(err_ptr, 48);
7546b66cc250SMike Miller 	VERIFY_OFFSET(err_len, 56);
7547b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved4  */
7548b66cc250SMike Miller 	VERIFY_OFFSET(sg, 64);
7549b66cc250SMike Miller 
7550b66cc250SMike Miller #undef VERIFY_OFFSET
7551b66cc250SMike Miller 
7552b66cc250SMike Miller #define VERIFY_OFFSET(member, offset) \
7553e1f7de0cSMatt Gates 	BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
7554e1f7de0cSMatt Gates 
7555e1f7de0cSMatt Gates 	VERIFY_OFFSET(dev_handle, 0x00);
7556e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved1, 0x02);
7557e1f7de0cSMatt Gates 	VERIFY_OFFSET(function, 0x03);
7558e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved2, 0x04);
7559e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info, 0x0C);
7560e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved3, 0x10);
7561e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info_len, 0x12);
7562e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved4, 0x13);
7563e1f7de0cSMatt Gates 	VERIFY_OFFSET(sgl_offset, 0x14);
7564e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved5, 0x15);
7565e1f7de0cSMatt Gates 	VERIFY_OFFSET(transfer_len, 0x1C);
7566e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved6, 0x20);
7567e1f7de0cSMatt Gates 	VERIFY_OFFSET(io_flags, 0x24);
7568e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved7, 0x26);
7569e1f7de0cSMatt Gates 	VERIFY_OFFSET(LUN, 0x34);
7570e1f7de0cSMatt Gates 	VERIFY_OFFSET(control, 0x3C);
7571e1f7de0cSMatt Gates 	VERIFY_OFFSET(CDB, 0x40);
7572e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved8, 0x50);
7573e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_context_flags, 0x60);
7574e1f7de0cSMatt Gates 	VERIFY_OFFSET(timeout_sec, 0x62);
7575e1f7de0cSMatt Gates 	VERIFY_OFFSET(ReplyQueue, 0x64);
7576e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved9, 0x65);
757750a0decfSStephen M. Cameron 	VERIFY_OFFSET(tag, 0x68);
7578e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_addr, 0x70);
7579e1f7de0cSMatt Gates 	VERIFY_OFFSET(CISS_LUN, 0x78);
7580e1f7de0cSMatt Gates 	VERIFY_OFFSET(SG, 0x78 + 8);
7581e1f7de0cSMatt Gates #undef VERIFY_OFFSET
7582e1f7de0cSMatt Gates }
7583e1f7de0cSMatt Gates 
7584edd16368SStephen M. Cameron module_init(hpsa_init);
7585edd16368SStephen M. Cameron module_exit(hpsa_cleanup);
7586