xref: /openbmc/linux/drivers/scsi/hpsa.c (revision 9846590edadb3c961fed095d6b3c0af947230e69)
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>
51283b4a9bSStephen M. Cameron #include <asm/div64.h>
52edd16368SStephen M. Cameron #include "hpsa_cmd.h"
53edd16368SStephen M. Cameron #include "hpsa.h"
54edd16368SStephen M. Cameron 
55edd16368SStephen M. Cameron /* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */
56e481cce8SMike Miller #define HPSA_DRIVER_VERSION "3.4.0-1"
57edd16368SStephen M. Cameron #define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
58f79cfec6SStephen M. Cameron #define HPSA "hpsa"
59edd16368SStephen M. Cameron 
60edd16368SStephen M. Cameron /* How long to wait (in milliseconds) for board to go into simple mode */
61edd16368SStephen M. Cameron #define MAX_CONFIG_WAIT 30000
62edd16368SStephen M. Cameron #define MAX_IOCTL_CONFIG_WAIT 1000
63edd16368SStephen M. Cameron 
64edd16368SStephen M. Cameron /*define how many times we will try a command because of bus resets */
65edd16368SStephen M. Cameron #define MAX_CMD_RETRIES 3
66edd16368SStephen M. Cameron 
67edd16368SStephen M. Cameron /* Embedded module documentation macros - see modules.h */
68edd16368SStephen M. Cameron MODULE_AUTHOR("Hewlett-Packard Company");
69edd16368SStephen M. Cameron MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
70edd16368SStephen M. Cameron 	HPSA_DRIVER_VERSION);
71edd16368SStephen M. Cameron MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
72edd16368SStephen M. Cameron MODULE_VERSION(HPSA_DRIVER_VERSION);
73edd16368SStephen M. Cameron MODULE_LICENSE("GPL");
74edd16368SStephen M. Cameron 
75edd16368SStephen M. Cameron static int hpsa_allow_any;
76edd16368SStephen M. Cameron module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
77edd16368SStephen M. Cameron MODULE_PARM_DESC(hpsa_allow_any,
78edd16368SStephen M. Cameron 		"Allow hpsa driver to access unknown HP Smart Array hardware");
7902ec19c8SStephen M. Cameron static int hpsa_simple_mode;
8002ec19c8SStephen M. Cameron module_param(hpsa_simple_mode, int, S_IRUGO|S_IWUSR);
8102ec19c8SStephen M. Cameron MODULE_PARM_DESC(hpsa_simple_mode,
8202ec19c8SStephen M. Cameron 	"Use 'simple mode' rather than 'performant mode'");
83edd16368SStephen M. Cameron 
84edd16368SStephen M. Cameron /* define the PCI info for the cards we can control */
85edd16368SStephen M. Cameron static const struct pci_device_id hpsa_pci_device_id[] = {
86edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3241},
87edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3243},
88edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3245},
89edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3247},
90edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3249},
91163dbcd8SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x324A},
92163dbcd8SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x324B},
93f8b01eb9SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3233},
949143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3350},
959143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3351},
969143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3352},
979143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3353},
989143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3354},
999143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3355},
1009143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3356},
101fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1921},
102fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1922},
103fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1923},
104fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1924},
105fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1925},
106fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1926},
107fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1928},
10897b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1929},
10997b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BD},
11097b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BE},
11197b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BF},
11297b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C0},
11397b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C1},
11497b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C2},
11597b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C3},
11697b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C4},
11797b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C5},
11897b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C7},
11997b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C8},
12097b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C9},
1218e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0076},
1228e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0087},
1238e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x007D},
1248e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0088},
1258e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP, 0x333f, 0x103c, 0x333f},
126edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_ANY_ID,	PCI_ANY_ID, PCI_ANY_ID,
127edd16368SStephen M. Cameron 		PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
128edd16368SStephen M. Cameron 	{0,}
129edd16368SStephen M. Cameron };
130edd16368SStephen M. Cameron 
131edd16368SStephen M. Cameron MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
132edd16368SStephen M. Cameron 
133edd16368SStephen M. Cameron /*  board_id = Subsystem Device ID & Vendor ID
134edd16368SStephen M. Cameron  *  product = Marketing Name for the board
135edd16368SStephen M. Cameron  *  access = Address of the struct of function pointers
136edd16368SStephen M. Cameron  */
137edd16368SStephen M. Cameron static struct board_type products[] = {
138edd16368SStephen M. Cameron 	{0x3241103C, "Smart Array P212", &SA5_access},
139edd16368SStephen M. Cameron 	{0x3243103C, "Smart Array P410", &SA5_access},
140edd16368SStephen M. Cameron 	{0x3245103C, "Smart Array P410i", &SA5_access},
141edd16368SStephen M. Cameron 	{0x3247103C, "Smart Array P411", &SA5_access},
142edd16368SStephen M. Cameron 	{0x3249103C, "Smart Array P812", &SA5_access},
143163dbcd8SMike Miller 	{0x324A103C, "Smart Array P712m", &SA5_access},
144163dbcd8SMike Miller 	{0x324B103C, "Smart Array P711m", &SA5_access},
145fe0c9610SMike Miller 	{0x3350103C, "Smart Array P222", &SA5_access},
146fe0c9610SMike Miller 	{0x3351103C, "Smart Array P420", &SA5_access},
147fe0c9610SMike Miller 	{0x3352103C, "Smart Array P421", &SA5_access},
148fe0c9610SMike Miller 	{0x3353103C, "Smart Array P822", &SA5_access},
149fe0c9610SMike Miller 	{0x3354103C, "Smart Array P420i", &SA5_access},
150fe0c9610SMike Miller 	{0x3355103C, "Smart Array P220i", &SA5_access},
151fe0c9610SMike Miller 	{0x3356103C, "Smart Array P721m", &SA5_access},
1521fd6c8e3SMike Miller 	{0x1921103C, "Smart Array P830i", &SA5_access},
1531fd6c8e3SMike Miller 	{0x1922103C, "Smart Array P430", &SA5_access},
1541fd6c8e3SMike Miller 	{0x1923103C, "Smart Array P431", &SA5_access},
1551fd6c8e3SMike Miller 	{0x1924103C, "Smart Array P830", &SA5_access},
1561fd6c8e3SMike Miller 	{0x1926103C, "Smart Array P731m", &SA5_access},
1571fd6c8e3SMike Miller 	{0x1928103C, "Smart Array P230i", &SA5_access},
1581fd6c8e3SMike Miller 	{0x1929103C, "Smart Array P530", &SA5_access},
15997b9f53dSMike Miller 	{0x21BD103C, "Smart Array", &SA5_access},
16097b9f53dSMike Miller 	{0x21BE103C, "Smart Array", &SA5_access},
16197b9f53dSMike Miller 	{0x21BF103C, "Smart Array", &SA5_access},
16297b9f53dSMike Miller 	{0x21C0103C, "Smart Array", &SA5_access},
16397b9f53dSMike Miller 	{0x21C1103C, "Smart Array", &SA5_access},
16497b9f53dSMike Miller 	{0x21C2103C, "Smart Array", &SA5_access},
16597b9f53dSMike Miller 	{0x21C3103C, "Smart Array", &SA5_access},
16697b9f53dSMike Miller 	{0x21C4103C, "Smart Array", &SA5_access},
16797b9f53dSMike Miller 	{0x21C5103C, "Smart Array", &SA5_access},
16897b9f53dSMike Miller 	{0x21C7103C, "Smart Array", &SA5_access},
16997b9f53dSMike Miller 	{0x21C8103C, "Smart Array", &SA5_access},
17097b9f53dSMike Miller 	{0x21C9103C, "Smart Array", &SA5_access},
1718e616a5eSStephen M. Cameron 	{0x00761590, "HP Storage P1224 Array Controller", &SA5_access},
1728e616a5eSStephen M. Cameron 	{0x00871590, "HP Storage P1224e Array Controller", &SA5_access},
1738e616a5eSStephen M. Cameron 	{0x007D1590, "HP Storage P1228 Array Controller", &SA5_access},
1748e616a5eSStephen M. Cameron 	{0x00881590, "HP Storage P1228e Array Controller", &SA5_access},
1758e616a5eSStephen M. Cameron 	{0x333f103c, "HP StorageWorks 1210m Array Controller", &SA5_access},
176edd16368SStephen M. Cameron 	{0xFFFF103C, "Unknown Smart Array", &SA5_access},
177edd16368SStephen M. Cameron };
178edd16368SStephen M. Cameron 
179edd16368SStephen M. Cameron static int number_of_controllers;
180edd16368SStephen M. Cameron 
18110f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id);
18210f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id);
183edd16368SStephen M. Cameron static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg);
184edd16368SStephen M. Cameron static void start_io(struct ctlr_info *h);
185edd16368SStephen M. Cameron 
186edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
187edd16368SStephen M. Cameron static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg);
188edd16368SStephen M. Cameron #endif
189edd16368SStephen M. Cameron 
190edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c);
191edd16368SStephen M. Cameron static void cmd_special_free(struct ctlr_info *h, struct CommandList *c);
192edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h);
193edd16368SStephen M. Cameron static struct CommandList *cmd_special_alloc(struct ctlr_info *h);
194a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
195b7bb24ebSStephen M. Cameron 	void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
196edd16368SStephen M. Cameron 	int cmd_type);
197b7bb24ebSStephen M. Cameron #define VPD_PAGE (1 << 8)
198edd16368SStephen M. Cameron 
199f281233dSJeff Garzik static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
200a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *);
201a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
202a08a8471SStephen M. Cameron 	unsigned long elapsed_time);
203667e23d4SStephen M. Cameron static int hpsa_change_queue_depth(struct scsi_device *sdev,
204667e23d4SStephen M. Cameron 	int qdepth, int reason);
205edd16368SStephen M. Cameron 
206edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
20775167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *scsicmd);
208edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev);
209edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev);
210edd16368SStephen M. Cameron 
211edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
212edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
213edd16368SStephen M. Cameron 	struct CommandList *c);
214edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
215edd16368SStephen M. Cameron 	struct CommandList *c);
216303932fdSDon Brace /* performant mode helper functions */
217303932fdSDon Brace static void calc_bucket_map(int *bucket, int num_buckets,
218e1f7de0cSMatt Gates 	int nsgs, int min_blocks, int *bucket_map);
2196f039790SGreg Kroah-Hartman static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
220254f796bSMatt Gates static inline u32 next_command(struct ctlr_info *h, u8 q);
2216f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
2226f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
2231df8552aSStephen M. Cameron 			       u64 *cfg_offset);
2246f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
2251df8552aSStephen M. Cameron 				    unsigned long *memory_bar);
2266f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
2276f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
2286f039790SGreg Kroah-Hartman 				     int wait_for_ready);
22975167d2cSStephen M. Cameron static inline void finish_cmd(struct CommandList *c);
230283b4a9bSStephen M. Cameron static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h);
231fe5389c8SStephen M. Cameron #define BOARD_NOT_READY 0
232fe5389c8SStephen M. Cameron #define BOARD_READY 1
23323100dd9SStephen M. Cameron static void hpsa_drain_accel_commands(struct ctlr_info *h);
23476438d08SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h);
235c349775eSScott Teel static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
236c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
237c349775eSScott Teel 	u8 *scsi3addr);
238edd16368SStephen M. Cameron 
239edd16368SStephen M. Cameron static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
240edd16368SStephen M. Cameron {
241edd16368SStephen M. Cameron 	unsigned long *priv = shost_priv(sdev->host);
242edd16368SStephen M. Cameron 	return (struct ctlr_info *) *priv;
243edd16368SStephen M. Cameron }
244edd16368SStephen M. Cameron 
245a23513e8SStephen M. Cameron static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
246a23513e8SStephen M. Cameron {
247a23513e8SStephen M. Cameron 	unsigned long *priv = shost_priv(sh);
248a23513e8SStephen M. Cameron 	return (struct ctlr_info *) *priv;
249a23513e8SStephen M. Cameron }
250a23513e8SStephen M. Cameron 
251edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
252edd16368SStephen M. Cameron 	struct CommandList *c)
253edd16368SStephen M. Cameron {
254edd16368SStephen M. Cameron 	if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
255edd16368SStephen M. Cameron 		return 0;
256edd16368SStephen M. Cameron 
257edd16368SStephen M. Cameron 	switch (c->err_info->SenseInfo[12]) {
258edd16368SStephen M. Cameron 	case STATE_CHANGED:
259f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: a state change "
260edd16368SStephen M. Cameron 			"detected, command retried\n", h->ctlr);
261edd16368SStephen M. Cameron 		break;
262edd16368SStephen M. Cameron 	case LUN_FAILED:
263f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: LUN failure "
264edd16368SStephen M. Cameron 			"detected, action required\n", h->ctlr);
265edd16368SStephen M. Cameron 		break;
266edd16368SStephen M. Cameron 	case REPORT_LUNS_CHANGED:
267f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: report LUN data "
26831468401SMike Miller 			"changed, action required\n", h->ctlr);
269edd16368SStephen M. Cameron 	/*
2704f4eb9f1SScott Teel 	 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
2714f4eb9f1SScott Teel 	 * target (array) devices.
272edd16368SStephen M. Cameron 	 */
273edd16368SStephen M. Cameron 		break;
274edd16368SStephen M. Cameron 	case POWER_OR_RESET:
275f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: a power on "
276edd16368SStephen M. Cameron 			"or device reset detected\n", h->ctlr);
277edd16368SStephen M. Cameron 		break;
278edd16368SStephen M. Cameron 	case UNIT_ATTENTION_CLEARED:
279f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: unit attention "
280edd16368SStephen M. Cameron 		    "cleared by another initiator\n", h->ctlr);
281edd16368SStephen M. Cameron 		break;
282edd16368SStephen M. Cameron 	default:
283f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: unknown "
284edd16368SStephen M. Cameron 			"unit attention detected\n", h->ctlr);
285edd16368SStephen M. Cameron 		break;
286edd16368SStephen M. Cameron 	}
287edd16368SStephen M. Cameron 	return 1;
288edd16368SStephen M. Cameron }
289edd16368SStephen M. Cameron 
290852af20aSMatt Bondurant static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
291852af20aSMatt Bondurant {
292852af20aSMatt Bondurant 	if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
293852af20aSMatt Bondurant 		(c->err_info->ScsiStatus != SAM_STAT_BUSY &&
294852af20aSMatt Bondurant 		 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
295852af20aSMatt Bondurant 		return 0;
296852af20aSMatt Bondurant 	dev_warn(&h->pdev->dev, HPSA "device busy");
297852af20aSMatt Bondurant 	return 1;
298852af20aSMatt Bondurant }
299852af20aSMatt Bondurant 
300da0697bdSScott Teel static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
301da0697bdSScott Teel 					 struct device_attribute *attr,
302da0697bdSScott Teel 					 const char *buf, size_t count)
303da0697bdSScott Teel {
304da0697bdSScott Teel 	int status, len;
305da0697bdSScott Teel 	struct ctlr_info *h;
306da0697bdSScott Teel 	struct Scsi_Host *shost = class_to_shost(dev);
307da0697bdSScott Teel 	char tmpbuf[10];
308da0697bdSScott Teel 
309da0697bdSScott Teel 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
310da0697bdSScott Teel 		return -EACCES;
311da0697bdSScott Teel 	len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
312da0697bdSScott Teel 	strncpy(tmpbuf, buf, len);
313da0697bdSScott Teel 	tmpbuf[len] = '\0';
314da0697bdSScott Teel 	if (sscanf(tmpbuf, "%d", &status) != 1)
315da0697bdSScott Teel 		return -EINVAL;
316da0697bdSScott Teel 	h = shost_to_hba(shost);
317da0697bdSScott Teel 	h->acciopath_status = !!status;
318da0697bdSScott Teel 	dev_warn(&h->pdev->dev,
319da0697bdSScott Teel 		"hpsa: HP SSD Smart Path %s via sysfs update.\n",
320da0697bdSScott Teel 		h->acciopath_status ? "enabled" : "disabled");
321da0697bdSScott Teel 	return count;
322da0697bdSScott Teel }
323da0697bdSScott Teel 
3242ba8bfc8SStephen M. Cameron static ssize_t host_store_raid_offload_debug(struct device *dev,
3252ba8bfc8SStephen M. Cameron 					 struct device_attribute *attr,
3262ba8bfc8SStephen M. Cameron 					 const char *buf, size_t count)
3272ba8bfc8SStephen M. Cameron {
3282ba8bfc8SStephen M. Cameron 	int debug_level, len;
3292ba8bfc8SStephen M. Cameron 	struct ctlr_info *h;
3302ba8bfc8SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
3312ba8bfc8SStephen M. Cameron 	char tmpbuf[10];
3322ba8bfc8SStephen M. Cameron 
3332ba8bfc8SStephen M. Cameron 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
3342ba8bfc8SStephen M. Cameron 		return -EACCES;
3352ba8bfc8SStephen M. Cameron 	len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
3362ba8bfc8SStephen M. Cameron 	strncpy(tmpbuf, buf, len);
3372ba8bfc8SStephen M. Cameron 	tmpbuf[len] = '\0';
3382ba8bfc8SStephen M. Cameron 	if (sscanf(tmpbuf, "%d", &debug_level) != 1)
3392ba8bfc8SStephen M. Cameron 		return -EINVAL;
3402ba8bfc8SStephen M. Cameron 	if (debug_level < 0)
3412ba8bfc8SStephen M. Cameron 		debug_level = 0;
3422ba8bfc8SStephen M. Cameron 	h = shost_to_hba(shost);
3432ba8bfc8SStephen M. Cameron 	h->raid_offload_debug = debug_level;
3442ba8bfc8SStephen M. Cameron 	dev_warn(&h->pdev->dev, "hpsa: Set raid_offload_debug level = %d\n",
3452ba8bfc8SStephen M. Cameron 		h->raid_offload_debug);
3462ba8bfc8SStephen M. Cameron 	return count;
3472ba8bfc8SStephen M. Cameron }
3482ba8bfc8SStephen M. Cameron 
349edd16368SStephen M. Cameron static ssize_t host_store_rescan(struct device *dev,
350edd16368SStephen M. Cameron 				 struct device_attribute *attr,
351edd16368SStephen M. Cameron 				 const char *buf, size_t count)
352edd16368SStephen M. Cameron {
353edd16368SStephen M. Cameron 	struct ctlr_info *h;
354edd16368SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
355a23513e8SStephen M. Cameron 	h = shost_to_hba(shost);
35631468401SMike Miller 	hpsa_scan_start(h->scsi_host);
357edd16368SStephen M. Cameron 	return count;
358edd16368SStephen M. Cameron }
359edd16368SStephen M. Cameron 
360d28ce020SStephen M. Cameron static ssize_t host_show_firmware_revision(struct device *dev,
361d28ce020SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
362d28ce020SStephen M. Cameron {
363d28ce020SStephen M. Cameron 	struct ctlr_info *h;
364d28ce020SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
365d28ce020SStephen M. Cameron 	unsigned char *fwrev;
366d28ce020SStephen M. Cameron 
367d28ce020SStephen M. Cameron 	h = shost_to_hba(shost);
368d28ce020SStephen M. Cameron 	if (!h->hba_inquiry_data)
369d28ce020SStephen M. Cameron 		return 0;
370d28ce020SStephen M. Cameron 	fwrev = &h->hba_inquiry_data[32];
371d28ce020SStephen M. Cameron 	return snprintf(buf, 20, "%c%c%c%c\n",
372d28ce020SStephen M. Cameron 		fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
373d28ce020SStephen M. Cameron }
374d28ce020SStephen M. Cameron 
37594a13649SStephen M. Cameron static ssize_t host_show_commands_outstanding(struct device *dev,
37694a13649SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
37794a13649SStephen M. Cameron {
37894a13649SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
37994a13649SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(shost);
38094a13649SStephen M. Cameron 
38194a13649SStephen M. Cameron 	return snprintf(buf, 20, "%d\n", h->commands_outstanding);
38294a13649SStephen M. Cameron }
38394a13649SStephen M. Cameron 
384745a7a25SStephen M. Cameron static ssize_t host_show_transport_mode(struct device *dev,
385745a7a25SStephen M. Cameron 	struct device_attribute *attr, char *buf)
386745a7a25SStephen M. Cameron {
387745a7a25SStephen M. Cameron 	struct ctlr_info *h;
388745a7a25SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
389745a7a25SStephen M. Cameron 
390745a7a25SStephen M. Cameron 	h = shost_to_hba(shost);
391745a7a25SStephen M. Cameron 	return snprintf(buf, 20, "%s\n",
392960a30e7SStephen M. Cameron 		h->transMethod & CFGTBL_Trans_Performant ?
393745a7a25SStephen M. Cameron 			"performant" : "simple");
394745a7a25SStephen M. Cameron }
395745a7a25SStephen M. Cameron 
396da0697bdSScott Teel static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
397da0697bdSScott Teel 	struct device_attribute *attr, char *buf)
398da0697bdSScott Teel {
399da0697bdSScott Teel 	struct ctlr_info *h;
400da0697bdSScott Teel 	struct Scsi_Host *shost = class_to_shost(dev);
401da0697bdSScott Teel 
402da0697bdSScott Teel 	h = shost_to_hba(shost);
403da0697bdSScott Teel 	return snprintf(buf, 30, "HP SSD Smart Path %s\n",
404da0697bdSScott Teel 		(h->acciopath_status == 1) ?  "enabled" : "disabled");
405da0697bdSScott Teel }
406da0697bdSScott Teel 
40746380786SStephen M. Cameron /* List of controllers which cannot be hard reset on kexec with reset_devices */
408941b1cdaSStephen M. Cameron static u32 unresettable_controller[] = {
409941b1cdaSStephen M. Cameron 	0x324a103C, /* Smart Array P712m */
410941b1cdaSStephen M. Cameron 	0x324b103C, /* SmartArray P711m */
411941b1cdaSStephen M. Cameron 	0x3223103C, /* Smart Array P800 */
412941b1cdaSStephen M. Cameron 	0x3234103C, /* Smart Array P400 */
413941b1cdaSStephen M. Cameron 	0x3235103C, /* Smart Array P400i */
414941b1cdaSStephen M. Cameron 	0x3211103C, /* Smart Array E200i */
415941b1cdaSStephen M. Cameron 	0x3212103C, /* Smart Array E200 */
416941b1cdaSStephen M. Cameron 	0x3213103C, /* Smart Array E200i */
417941b1cdaSStephen M. Cameron 	0x3214103C, /* Smart Array E200i */
418941b1cdaSStephen M. Cameron 	0x3215103C, /* Smart Array E200i */
419941b1cdaSStephen M. Cameron 	0x3237103C, /* Smart Array E500 */
420941b1cdaSStephen M. Cameron 	0x323D103C, /* Smart Array P700m */
4217af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
422941b1cdaSStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
423941b1cdaSStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
4245a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
4255a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
4265a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
4275a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
4285a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
4295a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
430941b1cdaSStephen M. Cameron };
431941b1cdaSStephen M. Cameron 
43246380786SStephen M. Cameron /* List of controllers which cannot even be soft reset */
43346380786SStephen M. Cameron static u32 soft_unresettable_controller[] = {
4347af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
4355a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
4365a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
4375a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
4385a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
4395a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
4405a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
44146380786SStephen M. Cameron 	/* Exclude 640x boards.  These are two pci devices in one slot
44246380786SStephen M. Cameron 	 * which share a battery backed cache module.  One controls the
44346380786SStephen M. Cameron 	 * cache, the other accesses the cache through the one that controls
44446380786SStephen M. Cameron 	 * it.  If we reset the one controlling the cache, the other will
44546380786SStephen M. Cameron 	 * likely not be happy.  Just forbid resetting this conjoined mess.
44646380786SStephen M. Cameron 	 * The 640x isn't really supported by hpsa anyway.
44746380786SStephen M. Cameron 	 */
44846380786SStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
44946380786SStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
45046380786SStephen M. Cameron };
45146380786SStephen M. Cameron 
45246380786SStephen M. Cameron static int ctlr_is_hard_resettable(u32 board_id)
453941b1cdaSStephen M. Cameron {
454941b1cdaSStephen M. Cameron 	int i;
455941b1cdaSStephen M. Cameron 
456941b1cdaSStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(unresettable_controller); i++)
45746380786SStephen M. Cameron 		if (unresettable_controller[i] == board_id)
458941b1cdaSStephen M. Cameron 			return 0;
459941b1cdaSStephen M. Cameron 	return 1;
460941b1cdaSStephen M. Cameron }
461941b1cdaSStephen M. Cameron 
46246380786SStephen M. Cameron static int ctlr_is_soft_resettable(u32 board_id)
46346380786SStephen M. Cameron {
46446380786SStephen M. Cameron 	int i;
46546380786SStephen M. Cameron 
46646380786SStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(soft_unresettable_controller); i++)
46746380786SStephen M. Cameron 		if (soft_unresettable_controller[i] == board_id)
46846380786SStephen M. Cameron 			return 0;
46946380786SStephen M. Cameron 	return 1;
47046380786SStephen M. Cameron }
47146380786SStephen M. Cameron 
47246380786SStephen M. Cameron static int ctlr_is_resettable(u32 board_id)
47346380786SStephen M. Cameron {
47446380786SStephen M. Cameron 	return ctlr_is_hard_resettable(board_id) ||
47546380786SStephen M. Cameron 		ctlr_is_soft_resettable(board_id);
47646380786SStephen M. Cameron }
47746380786SStephen M. Cameron 
478941b1cdaSStephen M. Cameron static ssize_t host_show_resettable(struct device *dev,
479941b1cdaSStephen M. Cameron 	struct device_attribute *attr, char *buf)
480941b1cdaSStephen M. Cameron {
481941b1cdaSStephen M. Cameron 	struct ctlr_info *h;
482941b1cdaSStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
483941b1cdaSStephen M. Cameron 
484941b1cdaSStephen M. Cameron 	h = shost_to_hba(shost);
48546380786SStephen M. Cameron 	return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
486941b1cdaSStephen M. Cameron }
487941b1cdaSStephen M. Cameron 
488edd16368SStephen M. Cameron static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
489edd16368SStephen M. Cameron {
490edd16368SStephen M. Cameron 	return (scsi3addr[3] & 0xC0) == 0x40;
491edd16368SStephen M. Cameron }
492edd16368SStephen M. Cameron 
493edd16368SStephen M. Cameron static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
494d82357eaSMike Miller 	"1(ADM)", "UNKNOWN"
495edd16368SStephen M. Cameron };
4966b80b18fSScott Teel #define HPSA_RAID_0	0
4976b80b18fSScott Teel #define HPSA_RAID_4	1
4986b80b18fSScott Teel #define HPSA_RAID_1	2	/* also used for RAID 10 */
4996b80b18fSScott Teel #define HPSA_RAID_5	3	/* also used for RAID 50 */
5006b80b18fSScott Teel #define HPSA_RAID_51	4
5016b80b18fSScott Teel #define HPSA_RAID_6	5	/* also used for RAID 60 */
5026b80b18fSScott Teel #define HPSA_RAID_ADM	6	/* also used for RAID 1+0 ADM */
503edd16368SStephen M. Cameron #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
504edd16368SStephen M. Cameron 
505edd16368SStephen M. Cameron static ssize_t raid_level_show(struct device *dev,
506edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
507edd16368SStephen M. Cameron {
508edd16368SStephen M. Cameron 	ssize_t l = 0;
50982a72c0aSStephen M. Cameron 	unsigned char rlevel;
510edd16368SStephen M. Cameron 	struct ctlr_info *h;
511edd16368SStephen M. Cameron 	struct scsi_device *sdev;
512edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
513edd16368SStephen M. Cameron 	unsigned long flags;
514edd16368SStephen M. Cameron 
515edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
516edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
517edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
518edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
519edd16368SStephen M. Cameron 	if (!hdev) {
520edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
521edd16368SStephen M. Cameron 		return -ENODEV;
522edd16368SStephen M. Cameron 	}
523edd16368SStephen M. Cameron 
524edd16368SStephen M. Cameron 	/* Is this even a logical drive? */
525edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
526edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
527edd16368SStephen M. Cameron 		l = snprintf(buf, PAGE_SIZE, "N/A\n");
528edd16368SStephen M. Cameron 		return l;
529edd16368SStephen M. Cameron 	}
530edd16368SStephen M. Cameron 
531edd16368SStephen M. Cameron 	rlevel = hdev->raid_level;
532edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
53382a72c0aSStephen M. Cameron 	if (rlevel > RAID_UNKNOWN)
534edd16368SStephen M. Cameron 		rlevel = RAID_UNKNOWN;
535edd16368SStephen M. Cameron 	l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
536edd16368SStephen M. Cameron 	return l;
537edd16368SStephen M. Cameron }
538edd16368SStephen M. Cameron 
539edd16368SStephen M. Cameron static ssize_t lunid_show(struct device *dev,
540edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
541edd16368SStephen M. Cameron {
542edd16368SStephen M. Cameron 	struct ctlr_info *h;
543edd16368SStephen M. Cameron 	struct scsi_device *sdev;
544edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
545edd16368SStephen M. Cameron 	unsigned long flags;
546edd16368SStephen M. Cameron 	unsigned char lunid[8];
547edd16368SStephen M. Cameron 
548edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
549edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
550edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
551edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
552edd16368SStephen M. Cameron 	if (!hdev) {
553edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
554edd16368SStephen M. Cameron 		return -ENODEV;
555edd16368SStephen M. Cameron 	}
556edd16368SStephen M. Cameron 	memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
557edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
558edd16368SStephen M. Cameron 	return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
559edd16368SStephen M. Cameron 		lunid[0], lunid[1], lunid[2], lunid[3],
560edd16368SStephen M. Cameron 		lunid[4], lunid[5], lunid[6], lunid[7]);
561edd16368SStephen M. Cameron }
562edd16368SStephen M. Cameron 
563edd16368SStephen M. Cameron static ssize_t unique_id_show(struct device *dev,
564edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
565edd16368SStephen M. Cameron {
566edd16368SStephen M. Cameron 	struct ctlr_info *h;
567edd16368SStephen M. Cameron 	struct scsi_device *sdev;
568edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
569edd16368SStephen M. Cameron 	unsigned long flags;
570edd16368SStephen M. Cameron 	unsigned char sn[16];
571edd16368SStephen M. Cameron 
572edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
573edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
574edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
575edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
576edd16368SStephen M. Cameron 	if (!hdev) {
577edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
578edd16368SStephen M. Cameron 		return -ENODEV;
579edd16368SStephen M. Cameron 	}
580edd16368SStephen M. Cameron 	memcpy(sn, hdev->device_id, sizeof(sn));
581edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
582edd16368SStephen M. Cameron 	return snprintf(buf, 16 * 2 + 2,
583edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X"
584edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X\n",
585edd16368SStephen M. Cameron 			sn[0], sn[1], sn[2], sn[3],
586edd16368SStephen M. Cameron 			sn[4], sn[5], sn[6], sn[7],
587edd16368SStephen M. Cameron 			sn[8], sn[9], sn[10], sn[11],
588edd16368SStephen M. Cameron 			sn[12], sn[13], sn[14], sn[15]);
589edd16368SStephen M. Cameron }
590edd16368SStephen M. Cameron 
591c1988684SScott Teel static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
592c1988684SScott Teel 	     struct device_attribute *attr, char *buf)
593c1988684SScott Teel {
594c1988684SScott Teel 	struct ctlr_info *h;
595c1988684SScott Teel 	struct scsi_device *sdev;
596c1988684SScott Teel 	struct hpsa_scsi_dev_t *hdev;
597c1988684SScott Teel 	unsigned long flags;
598c1988684SScott Teel 	int offload_enabled;
599c1988684SScott Teel 
600c1988684SScott Teel 	sdev = to_scsi_device(dev);
601c1988684SScott Teel 	h = sdev_to_hba(sdev);
602c1988684SScott Teel 	spin_lock_irqsave(&h->lock, flags);
603c1988684SScott Teel 	hdev = sdev->hostdata;
604c1988684SScott Teel 	if (!hdev) {
605c1988684SScott Teel 		spin_unlock_irqrestore(&h->lock, flags);
606c1988684SScott Teel 		return -ENODEV;
607c1988684SScott Teel 	}
608c1988684SScott Teel 	offload_enabled = hdev->offload_enabled;
609c1988684SScott Teel 	spin_unlock_irqrestore(&h->lock, flags);
610c1988684SScott Teel 	return snprintf(buf, 20, "%d\n", offload_enabled);
611c1988684SScott Teel }
612c1988684SScott Teel 
6133f5eac3aSStephen M. Cameron static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
6143f5eac3aSStephen M. Cameron static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
6153f5eac3aSStephen M. Cameron static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
6163f5eac3aSStephen M. Cameron static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
617c1988684SScott Teel static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
618c1988684SScott Teel 			host_show_hp_ssd_smart_path_enabled, NULL);
619da0697bdSScott Teel static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
620da0697bdSScott Teel 		host_show_hp_ssd_smart_path_status,
621da0697bdSScott Teel 		host_store_hp_ssd_smart_path_status);
6222ba8bfc8SStephen M. Cameron static DEVICE_ATTR(raid_offload_debug, S_IWUSR, NULL,
6232ba8bfc8SStephen M. Cameron 			host_store_raid_offload_debug);
6243f5eac3aSStephen M. Cameron static DEVICE_ATTR(firmware_revision, S_IRUGO,
6253f5eac3aSStephen M. Cameron 	host_show_firmware_revision, NULL);
6263f5eac3aSStephen M. Cameron static DEVICE_ATTR(commands_outstanding, S_IRUGO,
6273f5eac3aSStephen M. Cameron 	host_show_commands_outstanding, NULL);
6283f5eac3aSStephen M. Cameron static DEVICE_ATTR(transport_mode, S_IRUGO,
6293f5eac3aSStephen M. Cameron 	host_show_transport_mode, NULL);
630941b1cdaSStephen M. Cameron static DEVICE_ATTR(resettable, S_IRUGO,
631941b1cdaSStephen M. Cameron 	host_show_resettable, NULL);
6323f5eac3aSStephen M. Cameron 
6333f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_sdev_attrs[] = {
6343f5eac3aSStephen M. Cameron 	&dev_attr_raid_level,
6353f5eac3aSStephen M. Cameron 	&dev_attr_lunid,
6363f5eac3aSStephen M. Cameron 	&dev_attr_unique_id,
637c1988684SScott Teel 	&dev_attr_hp_ssd_smart_path_enabled,
6383f5eac3aSStephen M. Cameron 	NULL,
6393f5eac3aSStephen M. Cameron };
6403f5eac3aSStephen M. Cameron 
6413f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_shost_attrs[] = {
6423f5eac3aSStephen M. Cameron 	&dev_attr_rescan,
6433f5eac3aSStephen M. Cameron 	&dev_attr_firmware_revision,
6443f5eac3aSStephen M. Cameron 	&dev_attr_commands_outstanding,
6453f5eac3aSStephen M. Cameron 	&dev_attr_transport_mode,
646941b1cdaSStephen M. Cameron 	&dev_attr_resettable,
647da0697bdSScott Teel 	&dev_attr_hp_ssd_smart_path_status,
6482ba8bfc8SStephen M. Cameron 	&dev_attr_raid_offload_debug,
6493f5eac3aSStephen M. Cameron 	NULL,
6503f5eac3aSStephen M. Cameron };
6513f5eac3aSStephen M. Cameron 
6523f5eac3aSStephen M. Cameron static struct scsi_host_template hpsa_driver_template = {
6533f5eac3aSStephen M. Cameron 	.module			= THIS_MODULE,
654f79cfec6SStephen M. Cameron 	.name			= HPSA,
655f79cfec6SStephen M. Cameron 	.proc_name		= HPSA,
6563f5eac3aSStephen M. Cameron 	.queuecommand		= hpsa_scsi_queue_command,
6573f5eac3aSStephen M. Cameron 	.scan_start		= hpsa_scan_start,
6583f5eac3aSStephen M. Cameron 	.scan_finished		= hpsa_scan_finished,
6593f5eac3aSStephen M. Cameron 	.change_queue_depth	= hpsa_change_queue_depth,
6603f5eac3aSStephen M. Cameron 	.this_id		= -1,
6613f5eac3aSStephen M. Cameron 	.use_clustering		= ENABLE_CLUSTERING,
66275167d2cSStephen M. Cameron 	.eh_abort_handler	= hpsa_eh_abort_handler,
6633f5eac3aSStephen M. Cameron 	.eh_device_reset_handler = hpsa_eh_device_reset_handler,
6643f5eac3aSStephen M. Cameron 	.ioctl			= hpsa_ioctl,
6653f5eac3aSStephen M. Cameron 	.slave_alloc		= hpsa_slave_alloc,
6663f5eac3aSStephen M. Cameron 	.slave_destroy		= hpsa_slave_destroy,
6673f5eac3aSStephen M. Cameron #ifdef CONFIG_COMPAT
6683f5eac3aSStephen M. Cameron 	.compat_ioctl		= hpsa_compat_ioctl,
6693f5eac3aSStephen M. Cameron #endif
6703f5eac3aSStephen M. Cameron 	.sdev_attrs = hpsa_sdev_attrs,
6713f5eac3aSStephen M. Cameron 	.shost_attrs = hpsa_shost_attrs,
672c0d6a4d1SStephen M. Cameron 	.max_sectors = 8192,
67354b2b50cSMartin K. Petersen 	.no_write_same = 1,
6743f5eac3aSStephen M. Cameron };
6753f5eac3aSStephen M. Cameron 
6763f5eac3aSStephen M. Cameron 
6773f5eac3aSStephen M. Cameron /* Enqueuing and dequeuing functions for cmdlists. */
6783f5eac3aSStephen M. Cameron static inline void addQ(struct list_head *list, struct CommandList *c)
6793f5eac3aSStephen M. Cameron {
6803f5eac3aSStephen M. Cameron 	list_add_tail(&c->list, list);
6813f5eac3aSStephen M. Cameron }
6823f5eac3aSStephen M. Cameron 
683254f796bSMatt Gates static inline u32 next_command(struct ctlr_info *h, u8 q)
6843f5eac3aSStephen M. Cameron {
6853f5eac3aSStephen M. Cameron 	u32 a;
686254f796bSMatt Gates 	struct reply_pool *rq = &h->reply_queue[q];
687e16a33adSMatt Gates 	unsigned long flags;
6883f5eac3aSStephen M. Cameron 
689e1f7de0cSMatt Gates 	if (h->transMethod & CFGTBL_Trans_io_accel1)
690e1f7de0cSMatt Gates 		return h->access.command_completed(h, q);
691e1f7de0cSMatt Gates 
6923f5eac3aSStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
693254f796bSMatt Gates 		return h->access.command_completed(h, q);
6943f5eac3aSStephen M. Cameron 
695254f796bSMatt Gates 	if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
696254f796bSMatt Gates 		a = rq->head[rq->current_entry];
697254f796bSMatt Gates 		rq->current_entry++;
698e16a33adSMatt Gates 		spin_lock_irqsave(&h->lock, flags);
6993f5eac3aSStephen M. Cameron 		h->commands_outstanding--;
700e16a33adSMatt Gates 		spin_unlock_irqrestore(&h->lock, flags);
7013f5eac3aSStephen M. Cameron 	} else {
7023f5eac3aSStephen M. Cameron 		a = FIFO_EMPTY;
7033f5eac3aSStephen M. Cameron 	}
7043f5eac3aSStephen M. Cameron 	/* Check for wraparound */
705254f796bSMatt Gates 	if (rq->current_entry == h->max_commands) {
706254f796bSMatt Gates 		rq->current_entry = 0;
707254f796bSMatt Gates 		rq->wraparound ^= 1;
7083f5eac3aSStephen M. Cameron 	}
7093f5eac3aSStephen M. Cameron 	return a;
7103f5eac3aSStephen M. Cameron }
7113f5eac3aSStephen M. Cameron 
712c349775eSScott Teel /*
713c349775eSScott Teel  * There are some special bits in the bus address of the
714c349775eSScott Teel  * command that we have to set for the controller to know
715c349775eSScott Teel  * how to process the command:
716c349775eSScott Teel  *
717c349775eSScott Teel  * Normal performant mode:
718c349775eSScott Teel  * bit 0: 1 means performant mode, 0 means simple mode.
719c349775eSScott Teel  * bits 1-3 = block fetch table entry
720c349775eSScott Teel  * bits 4-6 = command type (== 0)
721c349775eSScott Teel  *
722c349775eSScott Teel  * ioaccel1 mode:
723c349775eSScott Teel  * bit 0 = "performant mode" bit.
724c349775eSScott Teel  * bits 1-3 = block fetch table entry
725c349775eSScott Teel  * bits 4-6 = command type (== 110)
726c349775eSScott Teel  * (command type is needed because ioaccel1 mode
727c349775eSScott Teel  * commands are submitted through the same register as normal
728c349775eSScott Teel  * mode commands, so this is how the controller knows whether
729c349775eSScott Teel  * the command is normal mode or ioaccel1 mode.)
730c349775eSScott Teel  *
731c349775eSScott Teel  * ioaccel2 mode:
732c349775eSScott Teel  * bit 0 = "performant mode" bit.
733c349775eSScott Teel  * bits 1-4 = block fetch table entry (note extra bit)
734c349775eSScott Teel  * bits 4-6 = not needed, because ioaccel2 mode has
735c349775eSScott Teel  * a separate special register for submitting commands.
736c349775eSScott Teel  */
737c349775eSScott Teel 
7383f5eac3aSStephen M. Cameron /* set_performant_mode: Modify the tag for cciss performant
7393f5eac3aSStephen M. Cameron  * set bit 0 for pull model, bits 3-1 for block fetch
7403f5eac3aSStephen M. Cameron  * register number
7413f5eac3aSStephen M. Cameron  */
7423f5eac3aSStephen M. Cameron static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
7433f5eac3aSStephen M. Cameron {
744254f796bSMatt Gates 	if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
7453f5eac3aSStephen M. Cameron 		c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
746eee0f03aSHannes Reinecke 		if (likely(h->msix_vector > 0))
747254f796bSMatt Gates 			c->Header.ReplyQueue =
748804a5cb5SJohn Kacur 				raw_smp_processor_id() % h->nreply_queues;
749254f796bSMatt Gates 	}
7503f5eac3aSStephen M. Cameron }
7513f5eac3aSStephen M. Cameron 
752c349775eSScott Teel static void set_ioaccel1_performant_mode(struct ctlr_info *h,
753c349775eSScott Teel 						struct CommandList *c)
754c349775eSScott Teel {
755c349775eSScott Teel 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
756c349775eSScott Teel 
757c349775eSScott Teel 	/* Tell the controller to post the reply to the queue for this
758c349775eSScott Teel 	 * processor.  This seems to give the best I/O throughput.
759c349775eSScott Teel 	 */
760c349775eSScott Teel 	cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
761c349775eSScott Teel 	/* Set the bits in the address sent down to include:
762c349775eSScott Teel 	 *  - performant mode bit (bit 0)
763c349775eSScott Teel 	 *  - pull count (bits 1-3)
764c349775eSScott Teel 	 *  - command type (bits 4-6)
765c349775eSScott Teel 	 */
766c349775eSScott Teel 	c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
767c349775eSScott Teel 					IOACCEL1_BUSADDR_CMDTYPE;
768c349775eSScott Teel }
769c349775eSScott Teel 
770c349775eSScott Teel static void set_ioaccel2_performant_mode(struct ctlr_info *h,
771c349775eSScott Teel 						struct CommandList *c)
772c349775eSScott Teel {
773c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
774c349775eSScott Teel 
775c349775eSScott Teel 	/* Tell the controller to post the reply to the queue for this
776c349775eSScott Teel 	 * processor.  This seems to give the best I/O throughput.
777c349775eSScott Teel 	 */
778c349775eSScott Teel 	cp->reply_queue = smp_processor_id() % h->nreply_queues;
779c349775eSScott Teel 	/* Set the bits in the address sent down to include:
780c349775eSScott Teel 	 *  - performant mode bit not used in ioaccel mode 2
781c349775eSScott Teel 	 *  - pull count (bits 0-3)
782c349775eSScott Teel 	 *  - command type isn't needed for ioaccel2
783c349775eSScott Teel 	 */
784c349775eSScott Teel 	c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
785c349775eSScott Teel }
786c349775eSScott Teel 
787e85c5974SStephen M. Cameron static int is_firmware_flash_cmd(u8 *cdb)
788e85c5974SStephen M. Cameron {
789e85c5974SStephen M. Cameron 	return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
790e85c5974SStephen M. Cameron }
791e85c5974SStephen M. Cameron 
792e85c5974SStephen M. Cameron /*
793e85c5974SStephen M. Cameron  * During firmware flash, the heartbeat register may not update as frequently
794e85c5974SStephen M. Cameron  * as it should.  So we dial down lockup detection during firmware flash. and
795e85c5974SStephen M. Cameron  * dial it back up when firmware flash completes.
796e85c5974SStephen M. Cameron  */
797e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
798e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
799e85c5974SStephen M. Cameron static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
800e85c5974SStephen M. Cameron 		struct CommandList *c)
801e85c5974SStephen M. Cameron {
802e85c5974SStephen M. Cameron 	if (!is_firmware_flash_cmd(c->Request.CDB))
803e85c5974SStephen M. Cameron 		return;
804e85c5974SStephen M. Cameron 	atomic_inc(&h->firmware_flash_in_progress);
805e85c5974SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
806e85c5974SStephen M. Cameron }
807e85c5974SStephen M. Cameron 
808e85c5974SStephen M. Cameron static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
809e85c5974SStephen M. Cameron 		struct CommandList *c)
810e85c5974SStephen M. Cameron {
811e85c5974SStephen M. Cameron 	if (is_firmware_flash_cmd(c->Request.CDB) &&
812e85c5974SStephen M. Cameron 		atomic_dec_and_test(&h->firmware_flash_in_progress))
813e85c5974SStephen M. Cameron 		h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
814e85c5974SStephen M. Cameron }
815e85c5974SStephen M. Cameron 
8163f5eac3aSStephen M. Cameron static void enqueue_cmd_and_start_io(struct ctlr_info *h,
8173f5eac3aSStephen M. Cameron 	struct CommandList *c)
8183f5eac3aSStephen M. Cameron {
8193f5eac3aSStephen M. Cameron 	unsigned long flags;
8203f5eac3aSStephen M. Cameron 
821c349775eSScott Teel 	switch (c->cmd_type) {
822c349775eSScott Teel 	case CMD_IOACCEL1:
823c349775eSScott Teel 		set_ioaccel1_performant_mode(h, c);
824c349775eSScott Teel 		break;
825c349775eSScott Teel 	case CMD_IOACCEL2:
826c349775eSScott Teel 		set_ioaccel2_performant_mode(h, c);
827c349775eSScott Teel 		break;
828c349775eSScott Teel 	default:
8293f5eac3aSStephen M. Cameron 		set_performant_mode(h, c);
830c349775eSScott Teel 	}
831e85c5974SStephen M. Cameron 	dial_down_lockup_detection_during_fw_flash(h, c);
8323f5eac3aSStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
8333f5eac3aSStephen M. Cameron 	addQ(&h->reqQ, c);
8343f5eac3aSStephen M. Cameron 	h->Qdepth++;
8353f5eac3aSStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
836e16a33adSMatt Gates 	start_io(h);
8373f5eac3aSStephen M. Cameron }
8383f5eac3aSStephen M. Cameron 
8393f5eac3aSStephen M. Cameron static inline void removeQ(struct CommandList *c)
8403f5eac3aSStephen M. Cameron {
8413f5eac3aSStephen M. Cameron 	if (WARN_ON(list_empty(&c->list)))
8423f5eac3aSStephen M. Cameron 		return;
8433f5eac3aSStephen M. Cameron 	list_del_init(&c->list);
8443f5eac3aSStephen M. Cameron }
8453f5eac3aSStephen M. Cameron 
8463f5eac3aSStephen M. Cameron static inline int is_hba_lunid(unsigned char scsi3addr[])
8473f5eac3aSStephen M. Cameron {
8483f5eac3aSStephen M. Cameron 	return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
8493f5eac3aSStephen M. Cameron }
8503f5eac3aSStephen M. Cameron 
8513f5eac3aSStephen M. Cameron static inline int is_scsi_rev_5(struct ctlr_info *h)
8523f5eac3aSStephen M. Cameron {
8533f5eac3aSStephen M. Cameron 	if (!h->hba_inquiry_data)
8543f5eac3aSStephen M. Cameron 		return 0;
8553f5eac3aSStephen M. Cameron 	if ((h->hba_inquiry_data[2] & 0x07) == 5)
8563f5eac3aSStephen M. Cameron 		return 1;
8573f5eac3aSStephen M. Cameron 	return 0;
8583f5eac3aSStephen M. Cameron }
8593f5eac3aSStephen M. Cameron 
860edd16368SStephen M. Cameron static int hpsa_find_target_lun(struct ctlr_info *h,
861edd16368SStephen M. Cameron 	unsigned char scsi3addr[], int bus, int *target, int *lun)
862edd16368SStephen M. Cameron {
863edd16368SStephen M. Cameron 	/* finds an unused bus, target, lun for a new physical device
864edd16368SStephen M. Cameron 	 * assumes h->devlock is held
865edd16368SStephen M. Cameron 	 */
866edd16368SStephen M. Cameron 	int i, found = 0;
867cfe5badcSScott Teel 	DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
868edd16368SStephen M. Cameron 
869263d9401SAkinobu Mita 	bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
870edd16368SStephen M. Cameron 
871edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
872edd16368SStephen M. Cameron 		if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
873263d9401SAkinobu Mita 			__set_bit(h->dev[i]->target, lun_taken);
874edd16368SStephen M. Cameron 	}
875edd16368SStephen M. Cameron 
876263d9401SAkinobu Mita 	i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
877263d9401SAkinobu Mita 	if (i < HPSA_MAX_DEVICES) {
878edd16368SStephen M. Cameron 		/* *bus = 1; */
879edd16368SStephen M. Cameron 		*target = i;
880edd16368SStephen M. Cameron 		*lun = 0;
881edd16368SStephen M. Cameron 		found = 1;
882edd16368SStephen M. Cameron 	}
883edd16368SStephen M. Cameron 	return !found;
884edd16368SStephen M. Cameron }
885edd16368SStephen M. Cameron 
886edd16368SStephen M. Cameron /* Add an entry into h->dev[] array. */
887edd16368SStephen M. Cameron static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
888edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *device,
889edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *added[], int *nadded)
890edd16368SStephen M. Cameron {
891edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
892edd16368SStephen M. Cameron 	int n = h->ndevices;
893edd16368SStephen M. Cameron 	int i;
894edd16368SStephen M. Cameron 	unsigned char addr1[8], addr2[8];
895edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
896edd16368SStephen M. Cameron 
897cfe5badcSScott Teel 	if (n >= HPSA_MAX_DEVICES) {
898edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "too many devices, some will be "
899edd16368SStephen M. Cameron 			"inaccessible.\n");
900edd16368SStephen M. Cameron 		return -1;
901edd16368SStephen M. Cameron 	}
902edd16368SStephen M. Cameron 
903edd16368SStephen M. Cameron 	/* physical devices do not have lun or target assigned until now. */
904edd16368SStephen M. Cameron 	if (device->lun != -1)
905edd16368SStephen M. Cameron 		/* Logical device, lun is already assigned. */
906edd16368SStephen M. Cameron 		goto lun_assigned;
907edd16368SStephen M. Cameron 
908edd16368SStephen M. Cameron 	/* If this device a non-zero lun of a multi-lun device
909edd16368SStephen M. Cameron 	 * byte 4 of the 8-byte LUN addr will contain the logical
910edd16368SStephen M. Cameron 	 * unit no, zero otherise.
911edd16368SStephen M. Cameron 	 */
912edd16368SStephen M. Cameron 	if (device->scsi3addr[4] == 0) {
913edd16368SStephen M. Cameron 		/* This is not a non-zero lun of a multi-lun device */
914edd16368SStephen M. Cameron 		if (hpsa_find_target_lun(h, device->scsi3addr,
915edd16368SStephen M. Cameron 			device->bus, &device->target, &device->lun) != 0)
916edd16368SStephen M. Cameron 			return -1;
917edd16368SStephen M. Cameron 		goto lun_assigned;
918edd16368SStephen M. Cameron 	}
919edd16368SStephen M. Cameron 
920edd16368SStephen M. Cameron 	/* This is a non-zero lun of a multi-lun device.
921edd16368SStephen M. Cameron 	 * Search through our list and find the device which
922edd16368SStephen M. Cameron 	 * has the same 8 byte LUN address, excepting byte 4.
923edd16368SStephen M. Cameron 	 * Assign the same bus and target for this new LUN.
924edd16368SStephen M. Cameron 	 * Use the logical unit number from the firmware.
925edd16368SStephen M. Cameron 	 */
926edd16368SStephen M. Cameron 	memcpy(addr1, device->scsi3addr, 8);
927edd16368SStephen M. Cameron 	addr1[4] = 0;
928edd16368SStephen M. Cameron 	for (i = 0; i < n; i++) {
929edd16368SStephen M. Cameron 		sd = h->dev[i];
930edd16368SStephen M. Cameron 		memcpy(addr2, sd->scsi3addr, 8);
931edd16368SStephen M. Cameron 		addr2[4] = 0;
932edd16368SStephen M. Cameron 		/* differ only in byte 4? */
933edd16368SStephen M. Cameron 		if (memcmp(addr1, addr2, 8) == 0) {
934edd16368SStephen M. Cameron 			device->bus = sd->bus;
935edd16368SStephen M. Cameron 			device->target = sd->target;
936edd16368SStephen M. Cameron 			device->lun = device->scsi3addr[4];
937edd16368SStephen M. Cameron 			break;
938edd16368SStephen M. Cameron 		}
939edd16368SStephen M. Cameron 	}
940edd16368SStephen M. Cameron 	if (device->lun == -1) {
941edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
942edd16368SStephen M. Cameron 			" suspect firmware bug or unsupported hardware "
943edd16368SStephen M. Cameron 			"configuration.\n");
944edd16368SStephen M. Cameron 			return -1;
945edd16368SStephen M. Cameron 	}
946edd16368SStephen M. Cameron 
947edd16368SStephen M. Cameron lun_assigned:
948edd16368SStephen M. Cameron 
949edd16368SStephen M. Cameron 	h->dev[n] = device;
950edd16368SStephen M. Cameron 	h->ndevices++;
951edd16368SStephen M. Cameron 	added[*nadded] = device;
952edd16368SStephen M. Cameron 	(*nadded)++;
953edd16368SStephen M. Cameron 
954edd16368SStephen M. Cameron 	/* initially, (before registering with scsi layer) we don't
955edd16368SStephen M. Cameron 	 * know our hostno and we don't want to print anything first
956edd16368SStephen M. Cameron 	 * time anyway (the scsi layer's inquiries will show that info)
957edd16368SStephen M. Cameron 	 */
958edd16368SStephen M. Cameron 	/* if (hostno != -1) */
959edd16368SStephen M. Cameron 		dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
960edd16368SStephen M. Cameron 			scsi_device_type(device->devtype), hostno,
961edd16368SStephen M. Cameron 			device->bus, device->target, device->lun);
962edd16368SStephen M. Cameron 	return 0;
963edd16368SStephen M. Cameron }
964edd16368SStephen M. Cameron 
965bd9244f7SScott Teel /* Update an entry in h->dev[] array. */
966bd9244f7SScott Teel static void hpsa_scsi_update_entry(struct ctlr_info *h, int hostno,
967bd9244f7SScott Teel 	int entry, struct hpsa_scsi_dev_t *new_entry)
968bd9244f7SScott Teel {
969bd9244f7SScott Teel 	/* assumes h->devlock is held */
970bd9244f7SScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
971bd9244f7SScott Teel 
972bd9244f7SScott Teel 	/* Raid level changed. */
973bd9244f7SScott Teel 	h->dev[entry]->raid_level = new_entry->raid_level;
974250fb125SStephen M. Cameron 
975250fb125SStephen M. Cameron 	/* Raid offload parameters changed. */
976250fb125SStephen M. Cameron 	h->dev[entry]->offload_config = new_entry->offload_config;
977250fb125SStephen M. Cameron 	h->dev[entry]->offload_enabled = new_entry->offload_enabled;
9789fb0de2dSStephen M. Cameron 	h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
9799fb0de2dSStephen M. Cameron 	h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
9809fb0de2dSStephen M. Cameron 	h->dev[entry]->raid_map = new_entry->raid_map;
981250fb125SStephen M. Cameron 
982bd9244f7SScott Teel 	dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d updated.\n",
983bd9244f7SScott Teel 		scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
984bd9244f7SScott Teel 		new_entry->target, new_entry->lun);
985bd9244f7SScott Teel }
986bd9244f7SScott Teel 
9872a8ccf31SStephen M. Cameron /* Replace an entry from h->dev[] array. */
9882a8ccf31SStephen M. Cameron static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
9892a8ccf31SStephen M. Cameron 	int entry, struct hpsa_scsi_dev_t *new_entry,
9902a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *added[], int *nadded,
9912a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
9922a8ccf31SStephen M. Cameron {
9932a8ccf31SStephen M. Cameron 	/* assumes h->devlock is held */
994cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
9952a8ccf31SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
9962a8ccf31SStephen M. Cameron 	(*nremoved)++;
99701350d05SStephen M. Cameron 
99801350d05SStephen M. Cameron 	/*
99901350d05SStephen M. Cameron 	 * New physical devices won't have target/lun assigned yet
100001350d05SStephen M. Cameron 	 * so we need to preserve the values in the slot we are replacing.
100101350d05SStephen M. Cameron 	 */
100201350d05SStephen M. Cameron 	if (new_entry->target == -1) {
100301350d05SStephen M. Cameron 		new_entry->target = h->dev[entry]->target;
100401350d05SStephen M. Cameron 		new_entry->lun = h->dev[entry]->lun;
100501350d05SStephen M. Cameron 	}
100601350d05SStephen M. Cameron 
10072a8ccf31SStephen M. Cameron 	h->dev[entry] = new_entry;
10082a8ccf31SStephen M. Cameron 	added[*nadded] = new_entry;
10092a8ccf31SStephen M. Cameron 	(*nadded)++;
10102a8ccf31SStephen M. Cameron 	dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n",
10112a8ccf31SStephen M. Cameron 		scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
10122a8ccf31SStephen M. Cameron 			new_entry->target, new_entry->lun);
10132a8ccf31SStephen M. Cameron }
10142a8ccf31SStephen M. Cameron 
1015edd16368SStephen M. Cameron /* Remove an entry from h->dev[] array. */
1016edd16368SStephen M. Cameron static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
1017edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
1018edd16368SStephen M. Cameron {
1019edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
1020edd16368SStephen M. Cameron 	int i;
1021edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1022edd16368SStephen M. Cameron 
1023cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
1024edd16368SStephen M. Cameron 
1025edd16368SStephen M. Cameron 	sd = h->dev[entry];
1026edd16368SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
1027edd16368SStephen M. Cameron 	(*nremoved)++;
1028edd16368SStephen M. Cameron 
1029edd16368SStephen M. Cameron 	for (i = entry; i < h->ndevices-1; i++)
1030edd16368SStephen M. Cameron 		h->dev[i] = h->dev[i+1];
1031edd16368SStephen M. Cameron 	h->ndevices--;
1032edd16368SStephen M. Cameron 	dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
1033edd16368SStephen M. Cameron 		scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
1034edd16368SStephen M. Cameron 		sd->lun);
1035edd16368SStephen M. Cameron }
1036edd16368SStephen M. Cameron 
1037edd16368SStephen M. Cameron #define SCSI3ADDR_EQ(a, b) ( \
1038edd16368SStephen M. Cameron 	(a)[7] == (b)[7] && \
1039edd16368SStephen M. Cameron 	(a)[6] == (b)[6] && \
1040edd16368SStephen M. Cameron 	(a)[5] == (b)[5] && \
1041edd16368SStephen M. Cameron 	(a)[4] == (b)[4] && \
1042edd16368SStephen M. Cameron 	(a)[3] == (b)[3] && \
1043edd16368SStephen M. Cameron 	(a)[2] == (b)[2] && \
1044edd16368SStephen M. Cameron 	(a)[1] == (b)[1] && \
1045edd16368SStephen M. Cameron 	(a)[0] == (b)[0])
1046edd16368SStephen M. Cameron 
1047edd16368SStephen M. Cameron static void fixup_botched_add(struct ctlr_info *h,
1048edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *added)
1049edd16368SStephen M. Cameron {
1050edd16368SStephen M. Cameron 	/* called when scsi_add_device fails in order to re-adjust
1051edd16368SStephen M. Cameron 	 * h->dev[] to match the mid layer's view.
1052edd16368SStephen M. Cameron 	 */
1053edd16368SStephen M. Cameron 	unsigned long flags;
1054edd16368SStephen M. Cameron 	int i, j;
1055edd16368SStephen M. Cameron 
1056edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
1057edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1058edd16368SStephen M. Cameron 		if (h->dev[i] == added) {
1059edd16368SStephen M. Cameron 			for (j = i; j < h->ndevices-1; j++)
1060edd16368SStephen M. Cameron 				h->dev[j] = h->dev[j+1];
1061edd16368SStephen M. Cameron 			h->ndevices--;
1062edd16368SStephen M. Cameron 			break;
1063edd16368SStephen M. Cameron 		}
1064edd16368SStephen M. Cameron 	}
1065edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
1066edd16368SStephen M. Cameron 	kfree(added);
1067edd16368SStephen M. Cameron }
1068edd16368SStephen M. Cameron 
1069edd16368SStephen M. Cameron static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
1070edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev2)
1071edd16368SStephen M. Cameron {
1072edd16368SStephen M. Cameron 	/* we compare everything except lun and target as these
1073edd16368SStephen M. Cameron 	 * are not yet assigned.  Compare parts likely
1074edd16368SStephen M. Cameron 	 * to differ first
1075edd16368SStephen M. Cameron 	 */
1076edd16368SStephen M. Cameron 	if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
1077edd16368SStephen M. Cameron 		sizeof(dev1->scsi3addr)) != 0)
1078edd16368SStephen M. Cameron 		return 0;
1079edd16368SStephen M. Cameron 	if (memcmp(dev1->device_id, dev2->device_id,
1080edd16368SStephen M. Cameron 		sizeof(dev1->device_id)) != 0)
1081edd16368SStephen M. Cameron 		return 0;
1082edd16368SStephen M. Cameron 	if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
1083edd16368SStephen M. Cameron 		return 0;
1084edd16368SStephen M. Cameron 	if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
1085edd16368SStephen M. Cameron 		return 0;
1086edd16368SStephen M. Cameron 	if (dev1->devtype != dev2->devtype)
1087edd16368SStephen M. Cameron 		return 0;
1088edd16368SStephen M. Cameron 	if (dev1->bus != dev2->bus)
1089edd16368SStephen M. Cameron 		return 0;
1090edd16368SStephen M. Cameron 	return 1;
1091edd16368SStephen M. Cameron }
1092edd16368SStephen M. Cameron 
1093bd9244f7SScott Teel static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
1094bd9244f7SScott Teel 	struct hpsa_scsi_dev_t *dev2)
1095bd9244f7SScott Teel {
1096bd9244f7SScott Teel 	/* Device attributes that can change, but don't mean
1097bd9244f7SScott Teel 	 * that the device is a different device, nor that the OS
1098bd9244f7SScott Teel 	 * needs to be told anything about the change.
1099bd9244f7SScott Teel 	 */
1100bd9244f7SScott Teel 	if (dev1->raid_level != dev2->raid_level)
1101bd9244f7SScott Teel 		return 1;
1102250fb125SStephen M. Cameron 	if (dev1->offload_config != dev2->offload_config)
1103250fb125SStephen M. Cameron 		return 1;
1104250fb125SStephen M. Cameron 	if (dev1->offload_enabled != dev2->offload_enabled)
1105250fb125SStephen M. Cameron 		return 1;
1106bd9244f7SScott Teel 	return 0;
1107bd9244f7SScott Teel }
1108bd9244f7SScott Teel 
1109edd16368SStephen M. Cameron /* Find needle in haystack.  If exact match found, return DEVICE_SAME,
1110edd16368SStephen M. Cameron  * and return needle location in *index.  If scsi3addr matches, but not
1111edd16368SStephen M. Cameron  * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
1112bd9244f7SScott Teel  * location in *index.
1113bd9244f7SScott Teel  * In the case of a minor device attribute change, such as RAID level, just
1114bd9244f7SScott Teel  * return DEVICE_UPDATED, along with the updated device's location in index.
1115bd9244f7SScott Teel  * If needle not found, return DEVICE_NOT_FOUND.
1116edd16368SStephen M. Cameron  */
1117edd16368SStephen M. Cameron static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
1118edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *haystack[], int haystack_size,
1119edd16368SStephen M. Cameron 	int *index)
1120edd16368SStephen M. Cameron {
1121edd16368SStephen M. Cameron 	int i;
1122edd16368SStephen M. Cameron #define DEVICE_NOT_FOUND 0
1123edd16368SStephen M. Cameron #define DEVICE_CHANGED 1
1124edd16368SStephen M. Cameron #define DEVICE_SAME 2
1125bd9244f7SScott Teel #define DEVICE_UPDATED 3
1126edd16368SStephen M. Cameron 	for (i = 0; i < haystack_size; i++) {
112723231048SStephen M. Cameron 		if (haystack[i] == NULL) /* previously removed. */
112823231048SStephen M. Cameron 			continue;
1129edd16368SStephen M. Cameron 		if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
1130edd16368SStephen M. Cameron 			*index = i;
1131bd9244f7SScott Teel 			if (device_is_the_same(needle, haystack[i])) {
1132bd9244f7SScott Teel 				if (device_updated(needle, haystack[i]))
1133bd9244f7SScott Teel 					return DEVICE_UPDATED;
1134edd16368SStephen M. Cameron 				return DEVICE_SAME;
1135bd9244f7SScott Teel 			} else {
1136*9846590eSStephen M. Cameron 				/* Keep offline devices offline */
1137*9846590eSStephen M. Cameron 				if (needle->volume_offline)
1138*9846590eSStephen M. Cameron 					return DEVICE_NOT_FOUND;
1139edd16368SStephen M. Cameron 				return DEVICE_CHANGED;
1140edd16368SStephen M. Cameron 			}
1141edd16368SStephen M. Cameron 		}
1142bd9244f7SScott Teel 	}
1143edd16368SStephen M. Cameron 	*index = -1;
1144edd16368SStephen M. Cameron 	return DEVICE_NOT_FOUND;
1145edd16368SStephen M. Cameron }
1146edd16368SStephen M. Cameron 
1147*9846590eSStephen M. Cameron static void hpsa_monitor_offline_device(struct ctlr_info *h,
1148*9846590eSStephen M. Cameron 					unsigned char scsi3addr[])
1149*9846590eSStephen M. Cameron {
1150*9846590eSStephen M. Cameron 	struct offline_device_entry *device;
1151*9846590eSStephen M. Cameron 	unsigned long flags;
1152*9846590eSStephen M. Cameron 
1153*9846590eSStephen M. Cameron 	/* Check to see if device is already on the list */
1154*9846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
1155*9846590eSStephen M. Cameron 	list_for_each_entry(device, &h->offline_device_list, offline_list) {
1156*9846590eSStephen M. Cameron 		if (memcmp(device->scsi3addr, scsi3addr,
1157*9846590eSStephen M. Cameron 			sizeof(device->scsi3addr)) == 0) {
1158*9846590eSStephen M. Cameron 			spin_unlock_irqrestore(&h->offline_device_lock, flags);
1159*9846590eSStephen M. Cameron 			return;
1160*9846590eSStephen M. Cameron 		}
1161*9846590eSStephen M. Cameron 	}
1162*9846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
1163*9846590eSStephen M. Cameron 
1164*9846590eSStephen M. Cameron 	/* Device is not on the list, add it. */
1165*9846590eSStephen M. Cameron 	device = kmalloc(sizeof(*device), GFP_KERNEL);
1166*9846590eSStephen M. Cameron 	if (!device) {
1167*9846590eSStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in %s\n", __func__);
1168*9846590eSStephen M. Cameron 		return;
1169*9846590eSStephen M. Cameron 	}
1170*9846590eSStephen M. Cameron 	memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
1171*9846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
1172*9846590eSStephen M. Cameron 	list_add_tail(&device->offline_list, &h->offline_device_list);
1173*9846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
1174*9846590eSStephen M. Cameron }
1175*9846590eSStephen M. Cameron 
1176*9846590eSStephen M. Cameron /* Print a message explaining various offline volume states */
1177*9846590eSStephen M. Cameron static void hpsa_show_volume_status(struct ctlr_info *h,
1178*9846590eSStephen M. Cameron 	struct hpsa_scsi_dev_t *sd)
1179*9846590eSStephen M. Cameron {
1180*9846590eSStephen M. Cameron 	if (sd->volume_offline == HPSA_VPD_LV_STATUS_UNSUPPORTED)
1181*9846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
1182*9846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume status is not available through vital product data pages.\n",
1183*9846590eSStephen M. Cameron 			h->scsi_host->host_no,
1184*9846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
1185*9846590eSStephen M. Cameron 	switch (sd->volume_offline) {
1186*9846590eSStephen M. Cameron 	case HPSA_LV_OK:
1187*9846590eSStephen M. Cameron 		break;
1188*9846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ERASE:
1189*9846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
1190*9846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing background erase process.\n",
1191*9846590eSStephen M. Cameron 			h->scsi_host->host_no,
1192*9846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
1193*9846590eSStephen M. Cameron 		break;
1194*9846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_RPI:
1195*9846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
1196*9846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing rapid parity initialization process.\n",
1197*9846590eSStephen M. Cameron 			h->scsi_host->host_no,
1198*9846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
1199*9846590eSStephen M. Cameron 		break;
1200*9846590eSStephen M. Cameron 	case HPSA_LV_PENDING_RPI:
1201*9846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
1202*9846590eSStephen M. Cameron 				"C%d:B%d:T%d:L%d Volume is queued for rapid parity initialization process.\n",
1203*9846590eSStephen M. Cameron 				h->scsi_host->host_no,
1204*9846590eSStephen M. Cameron 				sd->bus, sd->target, sd->lun);
1205*9846590eSStephen M. Cameron 		break;
1206*9846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_NO_KEY:
1207*9846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
1208*9846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because key is not present.\n",
1209*9846590eSStephen M. Cameron 			h->scsi_host->host_no,
1210*9846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
1211*9846590eSStephen M. Cameron 		break;
1212*9846590eSStephen M. Cameron 	case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
1213*9846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
1214*9846590eSStephen 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",
1215*9846590eSStephen M. Cameron 			h->scsi_host->host_no,
1216*9846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
1217*9846590eSStephen M. Cameron 		break;
1218*9846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION:
1219*9846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
1220*9846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing encryption process.\n",
1221*9846590eSStephen M. Cameron 			h->scsi_host->host_no,
1222*9846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
1223*9846590eSStephen M. Cameron 		break;
1224*9846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
1225*9846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
1226*9846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing encryption re-keying process.\n",
1227*9846590eSStephen M. Cameron 			h->scsi_host->host_no,
1228*9846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
1229*9846590eSStephen M. Cameron 		break;
1230*9846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
1231*9846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
1232*9846590eSStephen 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",
1233*9846590eSStephen M. Cameron 			h->scsi_host->host_no,
1234*9846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
1235*9846590eSStephen M. Cameron 		break;
1236*9846590eSStephen M. Cameron 	case HPSA_LV_PENDING_ENCRYPTION:
1237*9846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
1238*9846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is pending migration to encrypted state, but process has not started.\n",
1239*9846590eSStephen M. Cameron 			h->scsi_host->host_no,
1240*9846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
1241*9846590eSStephen M. Cameron 		break;
1242*9846590eSStephen M. Cameron 	case HPSA_LV_PENDING_ENCRYPTION_REKEYING:
1243*9846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
1244*9846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is encrypted and is pending encryption rekeying.\n",
1245*9846590eSStephen M. Cameron 			h->scsi_host->host_no,
1246*9846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
1247*9846590eSStephen M. Cameron 		break;
1248*9846590eSStephen M. Cameron 	}
1249*9846590eSStephen M. Cameron }
1250*9846590eSStephen M. Cameron 
12514967bd3eSStephen M. Cameron static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
1252edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd[], int nsds)
1253edd16368SStephen M. Cameron {
1254edd16368SStephen M. Cameron 	/* sd contains scsi3 addresses and devtypes, and inquiry
1255edd16368SStephen M. Cameron 	 * data.  This function takes what's in sd to be the current
1256edd16368SStephen M. Cameron 	 * reality and updates h->dev[] to reflect that reality.
1257edd16368SStephen M. Cameron 	 */
1258edd16368SStephen M. Cameron 	int i, entry, device_change, changes = 0;
1259edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *csd;
1260edd16368SStephen M. Cameron 	unsigned long flags;
1261edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **added, **removed;
1262edd16368SStephen M. Cameron 	int nadded, nremoved;
1263edd16368SStephen M. Cameron 	struct Scsi_Host *sh = NULL;
1264edd16368SStephen M. Cameron 
1265cfe5badcSScott Teel 	added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
1266cfe5badcSScott Teel 	removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
1267edd16368SStephen M. Cameron 
1268edd16368SStephen M. Cameron 	if (!added || !removed) {
1269edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in "
1270edd16368SStephen M. Cameron 			"adjust_hpsa_scsi_table\n");
1271edd16368SStephen M. Cameron 		goto free_and_out;
1272edd16368SStephen M. Cameron 	}
1273edd16368SStephen M. Cameron 
1274edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1275edd16368SStephen M. Cameron 
1276edd16368SStephen M. Cameron 	/* find any devices in h->dev[] that are not in
1277edd16368SStephen M. Cameron 	 * sd[] and remove them from h->dev[], and for any
1278edd16368SStephen M. Cameron 	 * devices which have changed, remove the old device
1279edd16368SStephen M. Cameron 	 * info and add the new device info.
1280bd9244f7SScott Teel 	 * If minor device attributes change, just update
1281bd9244f7SScott Teel 	 * the existing device structure.
1282edd16368SStephen M. Cameron 	 */
1283edd16368SStephen M. Cameron 	i = 0;
1284edd16368SStephen M. Cameron 	nremoved = 0;
1285edd16368SStephen M. Cameron 	nadded = 0;
1286edd16368SStephen M. Cameron 	while (i < h->ndevices) {
1287edd16368SStephen M. Cameron 		csd = h->dev[i];
1288edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
1289edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
1290edd16368SStephen M. Cameron 			changes++;
1291edd16368SStephen M. Cameron 			hpsa_scsi_remove_entry(h, hostno, i,
1292edd16368SStephen M. Cameron 				removed, &nremoved);
1293edd16368SStephen M. Cameron 			continue; /* remove ^^^, hence i not incremented */
1294edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
1295edd16368SStephen M. Cameron 			changes++;
12962a8ccf31SStephen M. Cameron 			hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
12972a8ccf31SStephen M. Cameron 				added, &nadded, removed, &nremoved);
1298c7f172dcSStephen M. Cameron 			/* Set it to NULL to prevent it from being freed
1299c7f172dcSStephen M. Cameron 			 * at the bottom of hpsa_update_scsi_devices()
1300c7f172dcSStephen M. Cameron 			 */
1301c7f172dcSStephen M. Cameron 			sd[entry] = NULL;
1302bd9244f7SScott Teel 		} else if (device_change == DEVICE_UPDATED) {
1303bd9244f7SScott Teel 			hpsa_scsi_update_entry(h, hostno, i, sd[entry]);
1304edd16368SStephen M. Cameron 		}
1305edd16368SStephen M. Cameron 		i++;
1306edd16368SStephen M. Cameron 	}
1307edd16368SStephen M. Cameron 
1308edd16368SStephen M. Cameron 	/* Now, make sure every device listed in sd[] is also
1309edd16368SStephen M. Cameron 	 * listed in h->dev[], adding them if they aren't found
1310edd16368SStephen M. Cameron 	 */
1311edd16368SStephen M. Cameron 
1312edd16368SStephen M. Cameron 	for (i = 0; i < nsds; i++) {
1313edd16368SStephen M. Cameron 		if (!sd[i]) /* if already added above. */
1314edd16368SStephen M. Cameron 			continue;
1315*9846590eSStephen M. Cameron 
1316*9846590eSStephen M. Cameron 		/* Don't add devices which are NOT READY, FORMAT IN PROGRESS
1317*9846590eSStephen M. Cameron 		 * as the SCSI mid-layer does not handle such devices well.
1318*9846590eSStephen M. Cameron 		 * It relentlessly loops sending TUR at 3Hz, then READ(10)
1319*9846590eSStephen M. Cameron 		 * at 160Hz, and prevents the system from coming up.
1320*9846590eSStephen M. Cameron 		 */
1321*9846590eSStephen M. Cameron 		if (sd[i]->volume_offline) {
1322*9846590eSStephen M. Cameron 			hpsa_show_volume_status(h, sd[i]);
1323*9846590eSStephen M. Cameron 			dev_info(&h->pdev->dev, "c%db%dt%dl%d: temporarily offline\n",
1324*9846590eSStephen M. Cameron 				h->scsi_host->host_no,
1325*9846590eSStephen M. Cameron 				sd[i]->bus, sd[i]->target, sd[i]->lun);
1326*9846590eSStephen M. Cameron 			continue;
1327*9846590eSStephen M. Cameron 		}
1328*9846590eSStephen M. Cameron 
1329edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1330edd16368SStephen M. Cameron 					h->ndevices, &entry);
1331edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
1332edd16368SStephen M. Cameron 			changes++;
1333edd16368SStephen M. Cameron 			if (hpsa_scsi_add_entry(h, hostno, sd[i],
1334edd16368SStephen M. Cameron 				added, &nadded) != 0)
1335edd16368SStephen M. Cameron 				break;
1336edd16368SStephen M. Cameron 			sd[i] = NULL; /* prevent from being freed later. */
1337edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
1338edd16368SStephen M. Cameron 			/* should never happen... */
1339edd16368SStephen M. Cameron 			changes++;
1340edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev,
1341edd16368SStephen M. Cameron 				"device unexpectedly changed.\n");
1342edd16368SStephen M. Cameron 			/* but if it does happen, we just ignore that device */
1343edd16368SStephen M. Cameron 		}
1344edd16368SStephen M. Cameron 	}
1345edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1346edd16368SStephen M. Cameron 
1347*9846590eSStephen M. Cameron 	/* Monitor devices which are in one of several NOT READY states to be
1348*9846590eSStephen M. Cameron 	 * brought online later. This must be done without holding h->devlock,
1349*9846590eSStephen M. Cameron 	 * so don't touch h->dev[]
1350*9846590eSStephen M. Cameron 	 */
1351*9846590eSStephen M. Cameron 	for (i = 0; i < nsds; i++) {
1352*9846590eSStephen M. Cameron 		if (!sd[i]) /* if already added above. */
1353*9846590eSStephen M. Cameron 			continue;
1354*9846590eSStephen M. Cameron 		if (sd[i]->volume_offline)
1355*9846590eSStephen M. Cameron 			hpsa_monitor_offline_device(h, sd[i]->scsi3addr);
1356*9846590eSStephen M. Cameron 	}
1357*9846590eSStephen M. Cameron 
1358edd16368SStephen M. Cameron 	/* Don't notify scsi mid layer of any changes the first time through
1359edd16368SStephen M. Cameron 	 * (or if there are no changes) scsi_scan_host will do it later the
1360edd16368SStephen M. Cameron 	 * first time through.
1361edd16368SStephen M. Cameron 	 */
1362edd16368SStephen M. Cameron 	if (hostno == -1 || !changes)
1363edd16368SStephen M. Cameron 		goto free_and_out;
1364edd16368SStephen M. Cameron 
1365edd16368SStephen M. Cameron 	sh = h->scsi_host;
1366edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any removed devices */
1367edd16368SStephen M. Cameron 	for (i = 0; i < nremoved; i++) {
1368edd16368SStephen M. Cameron 		struct scsi_device *sdev =
1369edd16368SStephen M. Cameron 			scsi_device_lookup(sh, removed[i]->bus,
1370edd16368SStephen M. Cameron 				removed[i]->target, removed[i]->lun);
1371edd16368SStephen M. Cameron 		if (sdev != NULL) {
1372edd16368SStephen M. Cameron 			scsi_remove_device(sdev);
1373edd16368SStephen M. Cameron 			scsi_device_put(sdev);
1374edd16368SStephen M. Cameron 		} else {
1375edd16368SStephen M. Cameron 			/* We don't expect to get here.
1376edd16368SStephen M. Cameron 			 * future cmds to this device will get selection
1377edd16368SStephen M. Cameron 			 * timeout as if the device was gone.
1378edd16368SStephen M. Cameron 			 */
1379edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
1380edd16368SStephen M. Cameron 				" for removal.", hostno, removed[i]->bus,
1381edd16368SStephen M. Cameron 				removed[i]->target, removed[i]->lun);
1382edd16368SStephen M. Cameron 		}
1383edd16368SStephen M. Cameron 		kfree(removed[i]);
1384edd16368SStephen M. Cameron 		removed[i] = NULL;
1385edd16368SStephen M. Cameron 	}
1386edd16368SStephen M. Cameron 
1387edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any added devices */
1388edd16368SStephen M. Cameron 	for (i = 0; i < nadded; i++) {
1389edd16368SStephen M. Cameron 		if (scsi_add_device(sh, added[i]->bus,
1390edd16368SStephen M. Cameron 			added[i]->target, added[i]->lun) == 0)
1391edd16368SStephen M. Cameron 			continue;
1392edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
1393edd16368SStephen M. Cameron 			"device not added.\n", hostno, added[i]->bus,
1394edd16368SStephen M. Cameron 			added[i]->target, added[i]->lun);
1395edd16368SStephen M. Cameron 		/* now we have to remove it from h->dev,
1396edd16368SStephen M. Cameron 		 * since it didn't get added to scsi mid layer
1397edd16368SStephen M. Cameron 		 */
1398edd16368SStephen M. Cameron 		fixup_botched_add(h, added[i]);
1399edd16368SStephen M. Cameron 	}
1400edd16368SStephen M. Cameron 
1401edd16368SStephen M. Cameron free_and_out:
1402edd16368SStephen M. Cameron 	kfree(added);
1403edd16368SStephen M. Cameron 	kfree(removed);
1404edd16368SStephen M. Cameron }
1405edd16368SStephen M. Cameron 
1406edd16368SStephen M. Cameron /*
14079e03aa2fSJoe Perches  * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
1408edd16368SStephen M. Cameron  * Assume's h->devlock is held.
1409edd16368SStephen M. Cameron  */
1410edd16368SStephen M. Cameron static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1411edd16368SStephen M. Cameron 	int bus, int target, int lun)
1412edd16368SStephen M. Cameron {
1413edd16368SStephen M. Cameron 	int i;
1414edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1415edd16368SStephen M. Cameron 
1416edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1417edd16368SStephen M. Cameron 		sd = h->dev[i];
1418edd16368SStephen M. Cameron 		if (sd->bus == bus && sd->target == target && sd->lun == lun)
1419edd16368SStephen M. Cameron 			return sd;
1420edd16368SStephen M. Cameron 	}
1421edd16368SStephen M. Cameron 	return NULL;
1422edd16368SStephen M. Cameron }
1423edd16368SStephen M. Cameron 
1424edd16368SStephen M. Cameron /* link sdev->hostdata to our per-device structure. */
1425edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev)
1426edd16368SStephen M. Cameron {
1427edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1428edd16368SStephen M. Cameron 	unsigned long flags;
1429edd16368SStephen M. Cameron 	struct ctlr_info *h;
1430edd16368SStephen M. Cameron 
1431edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
1432edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1433edd16368SStephen M. Cameron 	sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1434edd16368SStephen M. Cameron 		sdev_id(sdev), sdev->lun);
1435edd16368SStephen M. Cameron 	if (sd != NULL)
1436edd16368SStephen M. Cameron 		sdev->hostdata = sd;
1437edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1438edd16368SStephen M. Cameron 	return 0;
1439edd16368SStephen M. Cameron }
1440edd16368SStephen M. Cameron 
1441edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev)
1442edd16368SStephen M. Cameron {
1443bcc44255SStephen M. Cameron 	/* nothing to do. */
1444edd16368SStephen M. Cameron }
1445edd16368SStephen M. Cameron 
144633a2ffceSStephen M. Cameron static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
144733a2ffceSStephen M. Cameron {
144833a2ffceSStephen M. Cameron 	int i;
144933a2ffceSStephen M. Cameron 
145033a2ffceSStephen M. Cameron 	if (!h->cmd_sg_list)
145133a2ffceSStephen M. Cameron 		return;
145233a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
145333a2ffceSStephen M. Cameron 		kfree(h->cmd_sg_list[i]);
145433a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = NULL;
145533a2ffceSStephen M. Cameron 	}
145633a2ffceSStephen M. Cameron 	kfree(h->cmd_sg_list);
145733a2ffceSStephen M. Cameron 	h->cmd_sg_list = NULL;
145833a2ffceSStephen M. Cameron }
145933a2ffceSStephen M. Cameron 
146033a2ffceSStephen M. Cameron static int hpsa_allocate_sg_chain_blocks(struct ctlr_info *h)
146133a2ffceSStephen M. Cameron {
146233a2ffceSStephen M. Cameron 	int i;
146333a2ffceSStephen M. Cameron 
146433a2ffceSStephen M. Cameron 	if (h->chainsize <= 0)
146533a2ffceSStephen M. Cameron 		return 0;
146633a2ffceSStephen M. Cameron 
146733a2ffceSStephen M. Cameron 	h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
146833a2ffceSStephen M. Cameron 				GFP_KERNEL);
146933a2ffceSStephen M. Cameron 	if (!h->cmd_sg_list)
147033a2ffceSStephen M. Cameron 		return -ENOMEM;
147133a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
147233a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
147333a2ffceSStephen M. Cameron 						h->chainsize, GFP_KERNEL);
147433a2ffceSStephen M. Cameron 		if (!h->cmd_sg_list[i])
147533a2ffceSStephen M. Cameron 			goto clean;
147633a2ffceSStephen M. Cameron 	}
147733a2ffceSStephen M. Cameron 	return 0;
147833a2ffceSStephen M. Cameron 
147933a2ffceSStephen M. Cameron clean:
148033a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
148133a2ffceSStephen M. Cameron 	return -ENOMEM;
148233a2ffceSStephen M. Cameron }
148333a2ffceSStephen M. Cameron 
1484e2bea6dfSStephen M. Cameron static int hpsa_map_sg_chain_block(struct ctlr_info *h,
148533a2ffceSStephen M. Cameron 	struct CommandList *c)
148633a2ffceSStephen M. Cameron {
148733a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg, *chain_block;
148833a2ffceSStephen M. Cameron 	u64 temp64;
148933a2ffceSStephen M. Cameron 
149033a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
149133a2ffceSStephen M. Cameron 	chain_block = h->cmd_sg_list[c->cmdindex];
149233a2ffceSStephen M. Cameron 	chain_sg->Ext = HPSA_SG_CHAIN;
149333a2ffceSStephen M. Cameron 	chain_sg->Len = sizeof(*chain_sg) *
149433a2ffceSStephen M. Cameron 		(c->Header.SGTotal - h->max_cmd_sg_entries);
149533a2ffceSStephen M. Cameron 	temp64 = pci_map_single(h->pdev, chain_block, chain_sg->Len,
149633a2ffceSStephen M. Cameron 				PCI_DMA_TODEVICE);
1497e2bea6dfSStephen M. Cameron 	if (dma_mapping_error(&h->pdev->dev, temp64)) {
1498e2bea6dfSStephen M. Cameron 		/* prevent subsequent unmapping */
1499e2bea6dfSStephen M. Cameron 		chain_sg->Addr.lower = 0;
1500e2bea6dfSStephen M. Cameron 		chain_sg->Addr.upper = 0;
1501e2bea6dfSStephen M. Cameron 		return -1;
1502e2bea6dfSStephen M. Cameron 	}
150333a2ffceSStephen M. Cameron 	chain_sg->Addr.lower = (u32) (temp64 & 0x0FFFFFFFFULL);
150433a2ffceSStephen M. Cameron 	chain_sg->Addr.upper = (u32) ((temp64 >> 32) & 0x0FFFFFFFFULL);
1505e2bea6dfSStephen M. Cameron 	return 0;
150633a2ffceSStephen M. Cameron }
150733a2ffceSStephen M. Cameron 
150833a2ffceSStephen M. Cameron static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
150933a2ffceSStephen M. Cameron 	struct CommandList *c)
151033a2ffceSStephen M. Cameron {
151133a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg;
151233a2ffceSStephen M. Cameron 	union u64bit temp64;
151333a2ffceSStephen M. Cameron 
151433a2ffceSStephen M. Cameron 	if (c->Header.SGTotal <= h->max_cmd_sg_entries)
151533a2ffceSStephen M. Cameron 		return;
151633a2ffceSStephen M. Cameron 
151733a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
151833a2ffceSStephen M. Cameron 	temp64.val32.lower = chain_sg->Addr.lower;
151933a2ffceSStephen M. Cameron 	temp64.val32.upper = chain_sg->Addr.upper;
152033a2ffceSStephen M. Cameron 	pci_unmap_single(h->pdev, temp64.val, chain_sg->Len, PCI_DMA_TODEVICE);
152133a2ffceSStephen M. Cameron }
152233a2ffceSStephen M. Cameron 
1523a09c1441SScott Teel 
1524a09c1441SScott Teel /* Decode the various types of errors on ioaccel2 path.
1525a09c1441SScott Teel  * Return 1 for any error that should generate a RAID path retry.
1526a09c1441SScott Teel  * Return 0 for errors that don't require a RAID path retry.
1527a09c1441SScott Teel  */
1528a09c1441SScott Teel static int handle_ioaccel_mode2_error(struct ctlr_info *h,
1529c349775eSScott Teel 					struct CommandList *c,
1530c349775eSScott Teel 					struct scsi_cmnd *cmd,
1531c349775eSScott Teel 					struct io_accel2_cmd *c2)
1532c349775eSScott Teel {
1533c349775eSScott Teel 	int data_len;
1534a09c1441SScott Teel 	int retry = 0;
1535c349775eSScott Teel 
1536c349775eSScott Teel 	switch (c2->error_data.serv_response) {
1537c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_COMPLETE:
1538c349775eSScott Teel 		switch (c2->error_data.status) {
1539c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
1540c349775eSScott Teel 			break;
1541c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
1542c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1543c349775eSScott Teel 				"%s: task complete with check condition.\n",
1544c349775eSScott Teel 				"HP SSD Smart Path");
1545c349775eSScott Teel 			if (c2->error_data.data_present !=
1546c349775eSScott Teel 					IOACCEL2_SENSE_DATA_PRESENT)
1547c349775eSScott Teel 				break;
1548c349775eSScott Teel 			/* copy the sense data */
1549c349775eSScott Teel 			data_len = c2->error_data.sense_data_len;
1550c349775eSScott Teel 			if (data_len > SCSI_SENSE_BUFFERSIZE)
1551c349775eSScott Teel 				data_len = SCSI_SENSE_BUFFERSIZE;
1552c349775eSScott Teel 			if (data_len > sizeof(c2->error_data.sense_data_buff))
1553c349775eSScott Teel 				data_len =
1554c349775eSScott Teel 					sizeof(c2->error_data.sense_data_buff);
1555c349775eSScott Teel 			memcpy(cmd->sense_buffer,
1556c349775eSScott Teel 				c2->error_data.sense_data_buff, data_len);
1557c349775eSScott Teel 			cmd->result |= SAM_STAT_CHECK_CONDITION;
1558a09c1441SScott Teel 			retry = 1;
1559c349775eSScott Teel 			break;
1560c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
1561c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1562c349775eSScott Teel 				"%s: task complete with BUSY status.\n",
1563c349775eSScott Teel 				"HP SSD Smart Path");
1564a09c1441SScott Teel 			retry = 1;
1565c349775eSScott Teel 			break;
1566c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
1567c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1568c349775eSScott Teel 				"%s: task complete with reservation conflict.\n",
1569c349775eSScott Teel 				"HP SSD Smart Path");
1570a09c1441SScott Teel 			retry = 1;
1571c349775eSScott Teel 			break;
1572c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
1573c349775eSScott Teel 			/* Make scsi midlayer do unlimited retries */
1574c349775eSScott Teel 			cmd->result = DID_IMM_RETRY << 16;
1575c349775eSScott Teel 			break;
1576c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
1577c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1578c349775eSScott Teel 				"%s: task complete with aborted status.\n",
1579c349775eSScott Teel 				"HP SSD Smart Path");
1580a09c1441SScott Teel 			retry = 1;
1581c349775eSScott Teel 			break;
1582c349775eSScott Teel 		default:
1583c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1584c349775eSScott Teel 				"%s: task complete with unrecognized status: 0x%02x\n",
1585c349775eSScott Teel 				"HP SSD Smart Path", c2->error_data.status);
1586a09c1441SScott Teel 			retry = 1;
1587c349775eSScott Teel 			break;
1588c349775eSScott Teel 		}
1589c349775eSScott Teel 		break;
1590c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_FAILURE:
1591c349775eSScott Teel 		/* don't expect to get here. */
1592c349775eSScott Teel 		dev_warn(&h->pdev->dev,
1593c349775eSScott Teel 			"unexpected delivery or target failure, status = 0x%02x\n",
1594c349775eSScott Teel 			c2->error_data.status);
1595a09c1441SScott Teel 		retry = 1;
1596c349775eSScott Teel 		break;
1597c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
1598c349775eSScott Teel 		break;
1599c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
1600c349775eSScott Teel 		break;
1601c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
1602c349775eSScott Teel 		dev_warn(&h->pdev->dev, "task management function rejected.\n");
1603a09c1441SScott Teel 		retry = 1;
1604c349775eSScott Teel 		break;
1605c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
1606c349775eSScott Teel 		dev_warn(&h->pdev->dev, "task management function invalid LUN\n");
1607c349775eSScott Teel 		break;
1608c349775eSScott Teel 	default:
1609c349775eSScott Teel 		dev_warn(&h->pdev->dev,
1610c349775eSScott Teel 			"%s: Unrecognized server response: 0x%02x\n",
1611a09c1441SScott Teel 			"HP SSD Smart Path",
1612a09c1441SScott Teel 			c2->error_data.serv_response);
1613a09c1441SScott Teel 		retry = 1;
1614c349775eSScott Teel 		break;
1615c349775eSScott Teel 	}
1616a09c1441SScott Teel 
1617a09c1441SScott Teel 	return retry;	/* retry on raid path? */
1618c349775eSScott Teel }
1619c349775eSScott Teel 
1620c349775eSScott Teel static void process_ioaccel2_completion(struct ctlr_info *h,
1621c349775eSScott Teel 		struct CommandList *c, struct scsi_cmnd *cmd,
1622c349775eSScott Teel 		struct hpsa_scsi_dev_t *dev)
1623c349775eSScott Teel {
1624c349775eSScott Teel 	struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
1625a09c1441SScott Teel 	int raid_retry = 0;
1626c349775eSScott Teel 
1627c349775eSScott Teel 	/* check for good status */
1628c349775eSScott Teel 	if (likely(c2->error_data.serv_response == 0 &&
1629c349775eSScott Teel 			c2->error_data.status == 0)) {
1630c349775eSScott Teel 		cmd_free(h, c);
1631c349775eSScott Teel 		cmd->scsi_done(cmd);
1632c349775eSScott Teel 		return;
1633c349775eSScott Teel 	}
1634c349775eSScott Teel 
1635c349775eSScott Teel 	/* Any RAID offload error results in retry which will use
1636c349775eSScott Teel 	 * the normal I/O path so the controller can handle whatever's
1637c349775eSScott Teel 	 * wrong.
1638c349775eSScott Teel 	 */
1639c349775eSScott Teel 	if (is_logical_dev_addr_mode(dev->scsi3addr) &&
1640c349775eSScott Teel 		c2->error_data.serv_response ==
1641c349775eSScott Teel 			IOACCEL2_SERV_RESPONSE_FAILURE) {
1642a09c1441SScott Teel 		if (c2->error_data.status ==
1643c349775eSScott Teel 			IOACCEL2_STATUS_SR_IOACCEL_DISABLED)
1644c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1645a09c1441SScott Teel 				"%s: Path is unavailable, retrying on standard path.\n",
1646a09c1441SScott Teel 				"HP SSD Smart Path");
1647a09c1441SScott Teel 		else
1648a09c1441SScott Teel 			dev_warn(&h->pdev->dev,
1649a09c1441SScott Teel 				"%s: Error 0x%02x, retrying on standard path.\n",
1650c349775eSScott Teel 				"HP SSD Smart Path", c2->error_data.status);
1651a09c1441SScott Teel 
1652c349775eSScott Teel 		dev->offload_enabled = 0;
1653e863d68eSScott Teel 		h->drv_req_rescan = 1;	/* schedule controller for a rescan */
1654c349775eSScott Teel 		cmd->result = DID_SOFT_ERROR << 16;
1655c349775eSScott Teel 		cmd_free(h, c);
1656c349775eSScott Teel 		cmd->scsi_done(cmd);
1657c349775eSScott Teel 		return;
1658c349775eSScott Teel 	}
1659a09c1441SScott Teel 	raid_retry = handle_ioaccel_mode2_error(h, c, cmd, c2);
1660a09c1441SScott Teel 	/* If error found, disable Smart Path, schedule a rescan,
1661a09c1441SScott Teel 	 * and force a retry on the standard path.
1662a09c1441SScott Teel 	 */
1663a09c1441SScott Teel 	if (raid_retry) {
1664a09c1441SScott Teel 		dev_warn(&h->pdev->dev, "%s: Retrying on standard path.\n",
1665a09c1441SScott Teel 			"HP SSD Smart Path");
1666a09c1441SScott Teel 		dev->offload_enabled = 0; /* Disable Smart Path */
1667a09c1441SScott Teel 		h->drv_req_rescan = 1;	  /* schedule controller rescan */
1668a09c1441SScott Teel 		cmd->result = DID_SOFT_ERROR << 16;
1669a09c1441SScott Teel 	}
1670c349775eSScott Teel 	cmd_free(h, c);
1671c349775eSScott Teel 	cmd->scsi_done(cmd);
1672c349775eSScott Teel }
1673c349775eSScott Teel 
16741fb011fbSStephen M. Cameron static void complete_scsi_command(struct CommandList *cp)
1675edd16368SStephen M. Cameron {
1676edd16368SStephen M. Cameron 	struct scsi_cmnd *cmd;
1677edd16368SStephen M. Cameron 	struct ctlr_info *h;
1678edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
1679283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
1680edd16368SStephen M. Cameron 
1681edd16368SStephen M. Cameron 	unsigned char sense_key;
1682edd16368SStephen M. Cameron 	unsigned char asc;      /* additional sense code */
1683edd16368SStephen M. Cameron 	unsigned char ascq;     /* additional sense code qualifier */
1684db111e18SStephen M. Cameron 	unsigned long sense_data_size;
1685edd16368SStephen M. Cameron 
1686edd16368SStephen M. Cameron 	ei = cp->err_info;
1687edd16368SStephen M. Cameron 	cmd = (struct scsi_cmnd *) cp->scsi_cmd;
1688edd16368SStephen M. Cameron 	h = cp->h;
1689283b4a9bSStephen M. Cameron 	dev = cmd->device->hostdata;
1690edd16368SStephen M. Cameron 
1691edd16368SStephen M. Cameron 	scsi_dma_unmap(cmd); /* undo the DMA mappings */
1692e1f7de0cSMatt Gates 	if ((cp->cmd_type == CMD_SCSI) &&
1693e1f7de0cSMatt Gates 		(cp->Header.SGTotal > h->max_cmd_sg_entries))
169433a2ffceSStephen M. Cameron 		hpsa_unmap_sg_chain_block(h, cp);
1695edd16368SStephen M. Cameron 
1696edd16368SStephen M. Cameron 	cmd->result = (DID_OK << 16); 		/* host byte */
1697edd16368SStephen M. Cameron 	cmd->result |= (COMMAND_COMPLETE << 8);	/* msg byte */
1698c349775eSScott Teel 
1699c349775eSScott Teel 	if (cp->cmd_type == CMD_IOACCEL2)
1700c349775eSScott Teel 		return process_ioaccel2_completion(h, cp, cmd, dev);
1701c349775eSScott Teel 
17025512672fSStephen M. Cameron 	cmd->result |= ei->ScsiStatus;
1703edd16368SStephen M. Cameron 
1704edd16368SStephen M. Cameron 	/* copy the sense data whether we need to or not. */
1705db111e18SStephen M. Cameron 	if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
1706db111e18SStephen M. Cameron 		sense_data_size = SCSI_SENSE_BUFFERSIZE;
1707db111e18SStephen M. Cameron 	else
1708db111e18SStephen M. Cameron 		sense_data_size = sizeof(ei->SenseInfo);
1709db111e18SStephen M. Cameron 	if (ei->SenseLen < sense_data_size)
1710db111e18SStephen M. Cameron 		sense_data_size = ei->SenseLen;
1711db111e18SStephen M. Cameron 
1712db111e18SStephen M. Cameron 	memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
1713edd16368SStephen M. Cameron 	scsi_set_resid(cmd, ei->ResidualCnt);
1714edd16368SStephen M. Cameron 
1715edd16368SStephen M. Cameron 	if (ei->CommandStatus == 0) {
1716edd16368SStephen M. Cameron 		cmd_free(h, cp);
17172cc5bfafSTomas Henzl 		cmd->scsi_done(cmd);
1718edd16368SStephen M. Cameron 		return;
1719edd16368SStephen M. Cameron 	}
1720edd16368SStephen M. Cameron 
1721e1f7de0cSMatt Gates 	/* For I/O accelerator commands, copy over some fields to the normal
1722e1f7de0cSMatt Gates 	 * CISS header used below for error handling.
1723e1f7de0cSMatt Gates 	 */
1724e1f7de0cSMatt Gates 	if (cp->cmd_type == CMD_IOACCEL1) {
1725e1f7de0cSMatt Gates 		struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
1726e1f7de0cSMatt Gates 		cp->Header.SGList = cp->Header.SGTotal = scsi_sg_count(cmd);
1727e1f7de0cSMatt Gates 		cp->Request.CDBLen = c->io_flags & IOACCEL1_IOFLAGS_CDBLEN_MASK;
1728e1f7de0cSMatt Gates 		cp->Header.Tag.lower = c->Tag.lower;
1729e1f7de0cSMatt Gates 		cp->Header.Tag.upper = c->Tag.upper;
1730e1f7de0cSMatt Gates 		memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
1731e1f7de0cSMatt Gates 		memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
1732283b4a9bSStephen M. Cameron 
1733283b4a9bSStephen M. Cameron 		/* Any RAID offload error results in retry which will use
1734283b4a9bSStephen M. Cameron 		 * the normal I/O path so the controller can handle whatever's
1735283b4a9bSStephen M. Cameron 		 * wrong.
1736283b4a9bSStephen M. Cameron 		 */
1737283b4a9bSStephen M. Cameron 		if (is_logical_dev_addr_mode(dev->scsi3addr)) {
1738283b4a9bSStephen M. Cameron 			if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
1739283b4a9bSStephen M. Cameron 				dev->offload_enabled = 0;
1740283b4a9bSStephen M. Cameron 			cmd->result = DID_SOFT_ERROR << 16;
1741283b4a9bSStephen M. Cameron 			cmd_free(h, cp);
1742283b4a9bSStephen M. Cameron 			cmd->scsi_done(cmd);
1743283b4a9bSStephen M. Cameron 			return;
1744283b4a9bSStephen M. Cameron 		}
1745e1f7de0cSMatt Gates 	}
1746e1f7de0cSMatt Gates 
1747edd16368SStephen M. Cameron 	/* an error has occurred */
1748edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
1749edd16368SStephen M. Cameron 
1750edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
1751edd16368SStephen M. Cameron 		if (ei->ScsiStatus) {
1752edd16368SStephen M. Cameron 			/* Get sense key */
1753edd16368SStephen M. Cameron 			sense_key = 0xf & ei->SenseInfo[2];
1754edd16368SStephen M. Cameron 			/* Get additional sense code */
1755edd16368SStephen M. Cameron 			asc = ei->SenseInfo[12];
1756edd16368SStephen M. Cameron 			/* Get addition sense code qualifier */
1757edd16368SStephen M. Cameron 			ascq = ei->SenseInfo[13];
1758edd16368SStephen M. Cameron 		}
1759edd16368SStephen M. Cameron 
1760edd16368SStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
17613ce438dfSMatt Gates 			if (check_for_unit_attention(h, cp))
1762edd16368SStephen M. Cameron 				break;
1763edd16368SStephen M. Cameron 			if (sense_key == ILLEGAL_REQUEST) {
1764edd16368SStephen M. Cameron 				/*
1765edd16368SStephen M. Cameron 				 * SCSI REPORT_LUNS is commonly unsupported on
1766edd16368SStephen M. Cameron 				 * Smart Array.  Suppress noisy complaint.
1767edd16368SStephen M. Cameron 				 */
1768edd16368SStephen M. Cameron 				if (cp->Request.CDB[0] == REPORT_LUNS)
1769edd16368SStephen M. Cameron 					break;
1770edd16368SStephen M. Cameron 
1771edd16368SStephen M. Cameron 				/* If ASC/ASCQ indicate Logical Unit
1772edd16368SStephen M. Cameron 				 * Not Supported condition,
1773edd16368SStephen M. Cameron 				 */
1774edd16368SStephen M. Cameron 				if ((asc == 0x25) && (ascq == 0x0)) {
1775edd16368SStephen M. Cameron 					dev_warn(&h->pdev->dev, "cp %p "
1776edd16368SStephen M. Cameron 						"has check condition\n", cp);
1777edd16368SStephen M. Cameron 					break;
1778edd16368SStephen M. Cameron 				}
1779edd16368SStephen M. Cameron 			}
1780edd16368SStephen M. Cameron 
1781edd16368SStephen M. Cameron 			if (sense_key == NOT_READY) {
1782edd16368SStephen M. Cameron 				/* If Sense is Not Ready, Logical Unit
1783edd16368SStephen M. Cameron 				 * Not ready, Manual Intervention
1784edd16368SStephen M. Cameron 				 * required
1785edd16368SStephen M. Cameron 				 */
1786edd16368SStephen M. Cameron 				if ((asc == 0x04) && (ascq == 0x03)) {
1787edd16368SStephen M. Cameron 					dev_warn(&h->pdev->dev, "cp %p "
1788edd16368SStephen M. Cameron 						"has check condition: unit "
1789edd16368SStephen M. Cameron 						"not ready, manual "
1790edd16368SStephen M. Cameron 						"intervention required\n", cp);
1791edd16368SStephen M. Cameron 					break;
1792edd16368SStephen M. Cameron 				}
1793edd16368SStephen M. Cameron 			}
17941d3b3609SMatt Gates 			if (sense_key == ABORTED_COMMAND) {
17951d3b3609SMatt Gates 				/* Aborted command is retryable */
17961d3b3609SMatt Gates 				dev_warn(&h->pdev->dev, "cp %p "
17971d3b3609SMatt Gates 					"has check condition: aborted command: "
17981d3b3609SMatt Gates 					"ASC: 0x%x, ASCQ: 0x%x\n",
17991d3b3609SMatt Gates 					cp, asc, ascq);
18002e311fbaSStephen M. Cameron 				cmd->result |= DID_SOFT_ERROR << 16;
18011d3b3609SMatt Gates 				break;
18021d3b3609SMatt Gates 			}
1803edd16368SStephen M. Cameron 			/* Must be some other type of check condition */
180421b8e4efSStephen M. Cameron 			dev_dbg(&h->pdev->dev, "cp %p has check condition: "
1805edd16368SStephen M. Cameron 					"unknown type: "
1806edd16368SStephen M. Cameron 					"Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1807edd16368SStephen M. Cameron 					"Returning result: 0x%x, "
1808edd16368SStephen M. Cameron 					"cmd=[%02x %02x %02x %02x %02x "
1809807be732SMike Miller 					"%02x %02x %02x %02x %02x %02x "
1810edd16368SStephen M. Cameron 					"%02x %02x %02x %02x %02x]\n",
1811edd16368SStephen M. Cameron 					cp, sense_key, asc, ascq,
1812edd16368SStephen M. Cameron 					cmd->result,
1813edd16368SStephen M. Cameron 					cmd->cmnd[0], cmd->cmnd[1],
1814edd16368SStephen M. Cameron 					cmd->cmnd[2], cmd->cmnd[3],
1815edd16368SStephen M. Cameron 					cmd->cmnd[4], cmd->cmnd[5],
1816edd16368SStephen M. Cameron 					cmd->cmnd[6], cmd->cmnd[7],
1817807be732SMike Miller 					cmd->cmnd[8], cmd->cmnd[9],
1818807be732SMike Miller 					cmd->cmnd[10], cmd->cmnd[11],
1819807be732SMike Miller 					cmd->cmnd[12], cmd->cmnd[13],
1820807be732SMike Miller 					cmd->cmnd[14], cmd->cmnd[15]);
1821edd16368SStephen M. Cameron 			break;
1822edd16368SStephen M. Cameron 		}
1823edd16368SStephen M. Cameron 
1824edd16368SStephen M. Cameron 
1825edd16368SStephen M. Cameron 		/* Problem was not a check condition
1826edd16368SStephen M. Cameron 		 * Pass it up to the upper layers...
1827edd16368SStephen M. Cameron 		 */
1828edd16368SStephen M. Cameron 		if (ei->ScsiStatus) {
1829edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
1830edd16368SStephen M. Cameron 				"Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1831edd16368SStephen M. Cameron 				"Returning result: 0x%x\n",
1832edd16368SStephen M. Cameron 				cp, ei->ScsiStatus,
1833edd16368SStephen M. Cameron 				sense_key, asc, ascq,
1834edd16368SStephen M. Cameron 				cmd->result);
1835edd16368SStephen M. Cameron 		} else {  /* scsi status is zero??? How??? */
1836edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
1837edd16368SStephen M. Cameron 				"Returning no connection.\n", cp),
1838edd16368SStephen M. Cameron 
1839edd16368SStephen M. Cameron 			/* Ordinarily, this case should never happen,
1840edd16368SStephen M. Cameron 			 * but there is a bug in some released firmware
1841edd16368SStephen M. Cameron 			 * revisions that allows it to happen if, for
1842edd16368SStephen M. Cameron 			 * example, a 4100 backplane loses power and
1843edd16368SStephen M. Cameron 			 * the tape drive is in it.  We assume that
1844edd16368SStephen M. Cameron 			 * it's a fatal error of some kind because we
1845edd16368SStephen M. Cameron 			 * can't show that it wasn't. We will make it
1846edd16368SStephen M. Cameron 			 * look like selection timeout since that is
1847edd16368SStephen M. Cameron 			 * the most common reason for this to occur,
1848edd16368SStephen M. Cameron 			 * and it's severe enough.
1849edd16368SStephen M. Cameron 			 */
1850edd16368SStephen M. Cameron 
1851edd16368SStephen M. Cameron 			cmd->result = DID_NO_CONNECT << 16;
1852edd16368SStephen M. Cameron 		}
1853edd16368SStephen M. Cameron 		break;
1854edd16368SStephen M. Cameron 
1855edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1856edd16368SStephen M. Cameron 		break;
1857edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
1858edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p has"
1859edd16368SStephen M. Cameron 			" completed with data overrun "
1860edd16368SStephen M. Cameron 			"reported\n", cp);
1861edd16368SStephen M. Cameron 		break;
1862edd16368SStephen M. Cameron 	case CMD_INVALID: {
1863edd16368SStephen M. Cameron 		/* print_bytes(cp, sizeof(*cp), 1, 0);
1864edd16368SStephen M. Cameron 		print_cmd(cp); */
1865edd16368SStephen M. Cameron 		/* We get CMD_INVALID if you address a non-existent device
1866edd16368SStephen M. Cameron 		 * instead of a selection timeout (no response).  You will
1867edd16368SStephen M. Cameron 		 * see this if you yank out a drive, then try to access it.
1868edd16368SStephen M. Cameron 		 * This is kind of a shame because it means that any other
1869edd16368SStephen M. Cameron 		 * CMD_INVALID (e.g. driver bug) will get interpreted as a
1870edd16368SStephen M. Cameron 		 * missing target. */
1871edd16368SStephen M. Cameron 		cmd->result = DID_NO_CONNECT << 16;
1872edd16368SStephen M. Cameron 	}
1873edd16368SStephen M. Cameron 		break;
1874edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
1875256d0eaaSStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1876edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p has "
1877edd16368SStephen M. Cameron 			"protocol error\n", cp);
1878edd16368SStephen M. Cameron 		break;
1879edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
1880edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1881edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p had  hardware error\n", cp);
1882edd16368SStephen M. Cameron 		break;
1883edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
1884edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1885edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
1886edd16368SStephen M. Cameron 		break;
1887edd16368SStephen M. Cameron 	case CMD_ABORTED:
1888edd16368SStephen M. Cameron 		cmd->result = DID_ABORT << 16;
1889edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
1890edd16368SStephen M. Cameron 				cp, ei->ScsiStatus);
1891edd16368SStephen M. Cameron 		break;
1892edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
1893edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1894edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
1895edd16368SStephen M. Cameron 		break;
1896edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
1897f6e76055SStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
1898f6e76055SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p aborted due to an unsolicited "
1899edd16368SStephen M. Cameron 			"abort\n", cp);
1900edd16368SStephen M. Cameron 		break;
1901edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
1902edd16368SStephen M. Cameron 		cmd->result = DID_TIME_OUT << 16;
1903edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
1904edd16368SStephen M. Cameron 		break;
19051d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
19061d5e2ed0SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
19071d5e2ed0SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Command unabortable\n");
19081d5e2ed0SStephen M. Cameron 		break;
1909283b4a9bSStephen M. Cameron 	case CMD_IOACCEL_DISABLED:
1910283b4a9bSStephen M. Cameron 		/* This only handles the direct pass-through case since RAID
1911283b4a9bSStephen M. Cameron 		 * offload is handled above.  Just attempt a retry.
1912283b4a9bSStephen M. Cameron 		 */
1913283b4a9bSStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16;
1914283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev,
1915283b4a9bSStephen M. Cameron 				"cp %p had HP SSD Smart Path error\n", cp);
1916283b4a9bSStephen M. Cameron 		break;
1917edd16368SStephen M. Cameron 	default:
1918edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1919edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
1920edd16368SStephen M. Cameron 				cp, ei->CommandStatus);
1921edd16368SStephen M. Cameron 	}
1922edd16368SStephen M. Cameron 	cmd_free(h, cp);
19232cc5bfafSTomas Henzl 	cmd->scsi_done(cmd);
1924edd16368SStephen M. Cameron }
1925edd16368SStephen M. Cameron 
1926edd16368SStephen M. Cameron static void hpsa_pci_unmap(struct pci_dev *pdev,
1927edd16368SStephen M. Cameron 	struct CommandList *c, int sg_used, int data_direction)
1928edd16368SStephen M. Cameron {
1929edd16368SStephen M. Cameron 	int i;
1930edd16368SStephen M. Cameron 	union u64bit addr64;
1931edd16368SStephen M. Cameron 
1932edd16368SStephen M. Cameron 	for (i = 0; i < sg_used; i++) {
1933edd16368SStephen M. Cameron 		addr64.val32.lower = c->SG[i].Addr.lower;
1934edd16368SStephen M. Cameron 		addr64.val32.upper = c->SG[i].Addr.upper;
1935edd16368SStephen M. Cameron 		pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len,
1936edd16368SStephen M. Cameron 			data_direction);
1937edd16368SStephen M. Cameron 	}
1938edd16368SStephen M. Cameron }
1939edd16368SStephen M. Cameron 
1940a2dac136SStephen M. Cameron static int hpsa_map_one(struct pci_dev *pdev,
1941edd16368SStephen M. Cameron 		struct CommandList *cp,
1942edd16368SStephen M. Cameron 		unsigned char *buf,
1943edd16368SStephen M. Cameron 		size_t buflen,
1944edd16368SStephen M. Cameron 		int data_direction)
1945edd16368SStephen M. Cameron {
194601a02ffcSStephen M. Cameron 	u64 addr64;
1947edd16368SStephen M. Cameron 
1948edd16368SStephen M. Cameron 	if (buflen == 0 || data_direction == PCI_DMA_NONE) {
1949edd16368SStephen M. Cameron 		cp->Header.SGList = 0;
1950edd16368SStephen M. Cameron 		cp->Header.SGTotal = 0;
1951a2dac136SStephen M. Cameron 		return 0;
1952edd16368SStephen M. Cameron 	}
1953edd16368SStephen M. Cameron 
195401a02ffcSStephen M. Cameron 	addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction);
1955eceaae18SShuah Khan 	if (dma_mapping_error(&pdev->dev, addr64)) {
1956a2dac136SStephen M. Cameron 		/* Prevent subsequent unmap of something never mapped */
1957eceaae18SShuah Khan 		cp->Header.SGList = 0;
1958eceaae18SShuah Khan 		cp->Header.SGTotal = 0;
1959a2dac136SStephen M. Cameron 		return -1;
1960eceaae18SShuah Khan 	}
1961edd16368SStephen M. Cameron 	cp->SG[0].Addr.lower =
196201a02ffcSStephen M. Cameron 	  (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
1963edd16368SStephen M. Cameron 	cp->SG[0].Addr.upper =
196401a02ffcSStephen M. Cameron 	  (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
1965edd16368SStephen M. Cameron 	cp->SG[0].Len = buflen;
1966e1d9cbfaSMatt Gates 	cp->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining */
196701a02ffcSStephen M. Cameron 	cp->Header.SGList = (u8) 1;   /* no. SGs contig in this cmd */
196801a02ffcSStephen M. Cameron 	cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */
1969a2dac136SStephen M. Cameron 	return 0;
1970edd16368SStephen M. Cameron }
1971edd16368SStephen M. Cameron 
1972edd16368SStephen M. Cameron static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
1973edd16368SStephen M. Cameron 	struct CommandList *c)
1974edd16368SStephen M. Cameron {
1975edd16368SStephen M. Cameron 	DECLARE_COMPLETION_ONSTACK(wait);
1976edd16368SStephen M. Cameron 
1977edd16368SStephen M. Cameron 	c->waiting = &wait;
1978edd16368SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
1979edd16368SStephen M. Cameron 	wait_for_completion(&wait);
1980edd16368SStephen M. Cameron }
1981edd16368SStephen M. Cameron 
1982a0c12413SStephen M. Cameron static void hpsa_scsi_do_simple_cmd_core_if_no_lockup(struct ctlr_info *h,
1983a0c12413SStephen M. Cameron 	struct CommandList *c)
1984a0c12413SStephen M. Cameron {
1985a0c12413SStephen M. Cameron 	unsigned long flags;
1986a0c12413SStephen M. Cameron 
1987a0c12413SStephen M. Cameron 	/* If controller lockup detected, fake a hardware error. */
1988a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
1989a0c12413SStephen M. Cameron 	if (unlikely(h->lockup_detected)) {
1990a0c12413SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
1991a0c12413SStephen M. Cameron 		c->err_info->CommandStatus = CMD_HARDWARE_ERR;
1992a0c12413SStephen M. Cameron 	} else {
1993a0c12413SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
1994a0c12413SStephen M. Cameron 		hpsa_scsi_do_simple_cmd_core(h, c);
1995a0c12413SStephen M. Cameron 	}
1996a0c12413SStephen M. Cameron }
1997a0c12413SStephen M. Cameron 
19989c2fc160SStephen M. Cameron #define MAX_DRIVER_CMD_RETRIES 25
1999edd16368SStephen M. Cameron static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
2000edd16368SStephen M. Cameron 	struct CommandList *c, int data_direction)
2001edd16368SStephen M. Cameron {
20029c2fc160SStephen M. Cameron 	int backoff_time = 10, retry_count = 0;
2003edd16368SStephen M. Cameron 
2004edd16368SStephen M. Cameron 	do {
20057630abd0SJoe Perches 		memset(c->err_info, 0, sizeof(*c->err_info));
2006edd16368SStephen M. Cameron 		hpsa_scsi_do_simple_cmd_core(h, c);
2007edd16368SStephen M. Cameron 		retry_count++;
20089c2fc160SStephen M. Cameron 		if (retry_count > 3) {
20099c2fc160SStephen M. Cameron 			msleep(backoff_time);
20109c2fc160SStephen M. Cameron 			if (backoff_time < 1000)
20119c2fc160SStephen M. Cameron 				backoff_time *= 2;
20129c2fc160SStephen M. Cameron 		}
2013852af20aSMatt Bondurant 	} while ((check_for_unit_attention(h, c) ||
20149c2fc160SStephen M. Cameron 			check_for_busy(h, c)) &&
20159c2fc160SStephen M. Cameron 			retry_count <= MAX_DRIVER_CMD_RETRIES);
2016edd16368SStephen M. Cameron 	hpsa_pci_unmap(h->pdev, c, 1, data_direction);
2017edd16368SStephen M. Cameron }
2018edd16368SStephen M. Cameron 
2019d1e8beacSStephen M. Cameron static void hpsa_print_cmd(struct ctlr_info *h, char *txt,
2020d1e8beacSStephen M. Cameron 				struct CommandList *c)
2021edd16368SStephen M. Cameron {
2022d1e8beacSStephen M. Cameron 	const u8 *cdb = c->Request.CDB;
2023d1e8beacSStephen M. Cameron 	const u8 *lun = c->Header.LUN.LunAddrBytes;
2024edd16368SStephen M. Cameron 
2025d1e8beacSStephen M. Cameron 	dev_warn(&h->pdev->dev, "%s: LUN:%02x%02x%02x%02x%02x%02x%02x%02x"
2026d1e8beacSStephen M. Cameron 	" CDB:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
2027d1e8beacSStephen M. Cameron 		txt, lun[0], lun[1], lun[2], lun[3],
2028d1e8beacSStephen M. Cameron 		lun[4], lun[5], lun[6], lun[7],
2029d1e8beacSStephen M. Cameron 		cdb[0], cdb[1], cdb[2], cdb[3],
2030d1e8beacSStephen M. Cameron 		cdb[4], cdb[5], cdb[6], cdb[7],
2031d1e8beacSStephen M. Cameron 		cdb[8], cdb[9], cdb[10], cdb[11],
2032d1e8beacSStephen M. Cameron 		cdb[12], cdb[13], cdb[14], cdb[15]);
2033d1e8beacSStephen M. Cameron }
2034d1e8beacSStephen M. Cameron 
2035d1e8beacSStephen M. Cameron static void hpsa_scsi_interpret_error(struct ctlr_info *h,
2036d1e8beacSStephen M. Cameron 			struct CommandList *cp)
2037d1e8beacSStephen M. Cameron {
2038d1e8beacSStephen M. Cameron 	const struct ErrorInfo *ei = cp->err_info;
2039d1e8beacSStephen M. Cameron 	struct device *d = &cp->h->pdev->dev;
2040d1e8beacSStephen M. Cameron 	const u8 *sd = ei->SenseInfo;
2041d1e8beacSStephen M. Cameron 
2042edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
2043edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
2044d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "SCSI status", cp);
2045d1e8beacSStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION)
2046d1e8beacSStephen M. Cameron 			dev_warn(d, "SCSI Status = 02, Sense key = %02x, ASC = %02x, ASCQ = %02x\n",
2047d1e8beacSStephen M. Cameron 				sd[2] & 0x0f, sd[12], sd[13]);
2048d1e8beacSStephen M. Cameron 		else
2049d1e8beacSStephen M. Cameron 			dev_warn(d, "SCSI Status = %02x\n", ei->ScsiStatus);
2050edd16368SStephen M. Cameron 		if (ei->ScsiStatus == 0)
2051edd16368SStephen M. Cameron 			dev_warn(d, "SCSI status is abnormally zero.  "
2052edd16368SStephen M. Cameron 			"(probably indicates selection timeout "
2053edd16368SStephen M. Cameron 			"reported incorrectly due to a known "
2054edd16368SStephen M. Cameron 			"firmware bug, circa July, 2001.)\n");
2055edd16368SStephen M. Cameron 		break;
2056edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
2057edd16368SStephen M. Cameron 		break;
2058edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
2059d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "overrun condition", cp);
2060edd16368SStephen M. Cameron 		break;
2061edd16368SStephen M. Cameron 	case CMD_INVALID: {
2062edd16368SStephen M. Cameron 		/* controller unfortunately reports SCSI passthru's
2063edd16368SStephen M. Cameron 		 * to non-existent targets as invalid commands.
2064edd16368SStephen M. Cameron 		 */
2065d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "invalid command", cp);
2066d1e8beacSStephen M. Cameron 		dev_warn(d, "probably means device no longer present\n");
2067edd16368SStephen M. Cameron 		}
2068edd16368SStephen M. Cameron 		break;
2069edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
2070d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "protocol error", cp);
2071edd16368SStephen M. Cameron 		break;
2072edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
2073d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "hardware error", cp);
2074edd16368SStephen M. Cameron 		break;
2075edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
2076d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "connection lost", cp);
2077edd16368SStephen M. Cameron 		break;
2078edd16368SStephen M. Cameron 	case CMD_ABORTED:
2079d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "aborted", cp);
2080edd16368SStephen M. Cameron 		break;
2081edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
2082d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "abort failed", cp);
2083edd16368SStephen M. Cameron 		break;
2084edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
2085d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unsolicited abort", cp);
2086edd16368SStephen M. Cameron 		break;
2087edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
2088d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "timed out", cp);
2089edd16368SStephen M. Cameron 		break;
20901d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
2091d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unabortable", cp);
20921d5e2ed0SStephen M. Cameron 		break;
2093edd16368SStephen M. Cameron 	default:
2094d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unknown status", cp);
2095d1e8beacSStephen M. Cameron 		dev_warn(d, "Unknown command status %x\n",
2096edd16368SStephen M. Cameron 				ei->CommandStatus);
2097edd16368SStephen M. Cameron 	}
2098edd16368SStephen M. Cameron }
2099edd16368SStephen M. Cameron 
2100edd16368SStephen M. Cameron static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
2101b7bb24ebSStephen M. Cameron 			u16 page, unsigned char *buf,
2102edd16368SStephen M. Cameron 			unsigned char bufsize)
2103edd16368SStephen M. Cameron {
2104edd16368SStephen M. Cameron 	int rc = IO_OK;
2105edd16368SStephen M. Cameron 	struct CommandList *c;
2106edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2107edd16368SStephen M. Cameron 
2108edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
2109edd16368SStephen M. Cameron 
2110edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
2111edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2112ecd9aad4SStephen M. Cameron 		return -ENOMEM;
2113edd16368SStephen M. Cameron 	}
2114edd16368SStephen M. Cameron 
2115a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
2116a2dac136SStephen M. Cameron 			page, scsi3addr, TYPE_CMD)) {
2117a2dac136SStephen M. Cameron 		rc = -1;
2118a2dac136SStephen M. Cameron 		goto out;
2119a2dac136SStephen M. Cameron 	}
2120edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2121edd16368SStephen M. Cameron 	ei = c->err_info;
2122edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2123d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2124edd16368SStephen M. Cameron 		rc = -1;
2125edd16368SStephen M. Cameron 	}
2126a2dac136SStephen M. Cameron out:
2127edd16368SStephen M. Cameron 	cmd_special_free(h, c);
2128edd16368SStephen M. Cameron 	return rc;
2129edd16368SStephen M. Cameron }
2130edd16368SStephen M. Cameron 
2131bf711ac6SScott Teel static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
2132bf711ac6SScott Teel 	u8 reset_type)
2133edd16368SStephen M. Cameron {
2134edd16368SStephen M. Cameron 	int rc = IO_OK;
2135edd16368SStephen M. Cameron 	struct CommandList *c;
2136edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2137edd16368SStephen M. Cameron 
2138edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
2139edd16368SStephen M. Cameron 
2140edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
2141edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2142e9ea04a6SStephen M. Cameron 		return -ENOMEM;
2143edd16368SStephen M. Cameron 	}
2144edd16368SStephen M. Cameron 
2145a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map. */
2146bf711ac6SScott Teel 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
2147bf711ac6SScott Teel 			scsi3addr, TYPE_MSG);
2148bf711ac6SScott Teel 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to LUN reset */
2149edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core(h, c);
2150edd16368SStephen M. Cameron 	/* no unmap needed here because no data xfer. */
2151edd16368SStephen M. Cameron 
2152edd16368SStephen M. Cameron 	ei = c->err_info;
2153edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0) {
2154d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2155edd16368SStephen M. Cameron 		rc = -1;
2156edd16368SStephen M. Cameron 	}
2157edd16368SStephen M. Cameron 	cmd_special_free(h, c);
2158edd16368SStephen M. Cameron 	return rc;
2159edd16368SStephen M. Cameron }
2160edd16368SStephen M. Cameron 
2161edd16368SStephen M. Cameron static void hpsa_get_raid_level(struct ctlr_info *h,
2162edd16368SStephen M. Cameron 	unsigned char *scsi3addr, unsigned char *raid_level)
2163edd16368SStephen M. Cameron {
2164edd16368SStephen M. Cameron 	int rc;
2165edd16368SStephen M. Cameron 	unsigned char *buf;
2166edd16368SStephen M. Cameron 
2167edd16368SStephen M. Cameron 	*raid_level = RAID_UNKNOWN;
2168edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2169edd16368SStephen M. Cameron 	if (!buf)
2170edd16368SStephen M. Cameron 		return;
2171b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0xC1, buf, 64);
2172edd16368SStephen M. Cameron 	if (rc == 0)
2173edd16368SStephen M. Cameron 		*raid_level = buf[8];
2174edd16368SStephen M. Cameron 	if (*raid_level > RAID_UNKNOWN)
2175edd16368SStephen M. Cameron 		*raid_level = RAID_UNKNOWN;
2176edd16368SStephen M. Cameron 	kfree(buf);
2177edd16368SStephen M. Cameron 	return;
2178edd16368SStephen M. Cameron }
2179edd16368SStephen M. Cameron 
2180283b4a9bSStephen M. Cameron #define HPSA_MAP_DEBUG
2181283b4a9bSStephen M. Cameron #ifdef HPSA_MAP_DEBUG
2182283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
2183283b4a9bSStephen M. Cameron 				struct raid_map_data *map_buff)
2184283b4a9bSStephen M. Cameron {
2185283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map_buff->data[0];
2186283b4a9bSStephen M. Cameron 	int map, row, col;
2187283b4a9bSStephen M. Cameron 	u16 map_cnt, row_cnt, disks_per_row;
2188283b4a9bSStephen M. Cameron 
2189283b4a9bSStephen M. Cameron 	if (rc != 0)
2190283b4a9bSStephen M. Cameron 		return;
2191283b4a9bSStephen M. Cameron 
21922ba8bfc8SStephen M. Cameron 	/* Show details only if debugging has been activated. */
21932ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug < 2)
21942ba8bfc8SStephen M. Cameron 		return;
21952ba8bfc8SStephen M. Cameron 
2196283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "structure_size = %u\n",
2197283b4a9bSStephen M. Cameron 				le32_to_cpu(map_buff->structure_size));
2198283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
2199283b4a9bSStephen M. Cameron 			le32_to_cpu(map_buff->volume_blk_size));
2200283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
2201283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->volume_blk_cnt));
2202283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
2203283b4a9bSStephen M. Cameron 			map_buff->phys_blk_shift);
2204283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
2205283b4a9bSStephen M. Cameron 			map_buff->parity_rotation_shift);
2206283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "strip_size = %u\n",
2207283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->strip_size));
2208283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
2209283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_starting_blk));
2210283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
2211283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_blk_cnt));
2212283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
2213283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->data_disks_per_row));
2214283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
2215283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->metadata_disks_per_row));
2216283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "row_cnt = %u\n",
2217283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->row_cnt));
2218283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "layout_map_count = %u\n",
2219283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->layout_map_count));
2220dd0e19f3SScott Teel 	dev_info(&h->pdev->dev, "flags = %u\n",
2221dd0e19f3SScott Teel 			le16_to_cpu(map_buff->flags));
2222dd0e19f3SScott Teel 	if (map_buff->flags & RAID_MAP_FLAG_ENCRYPT_ON)
2223dd0e19f3SScott Teel 		dev_info(&h->pdev->dev, "encrypytion = ON\n");
2224dd0e19f3SScott Teel 	else
2225dd0e19f3SScott Teel 		dev_info(&h->pdev->dev, "encrypytion = OFF\n");
2226dd0e19f3SScott Teel 	dev_info(&h->pdev->dev, "dekindex = %u\n",
2227dd0e19f3SScott Teel 			le16_to_cpu(map_buff->dekindex));
2228283b4a9bSStephen M. Cameron 
2229283b4a9bSStephen M. Cameron 	map_cnt = le16_to_cpu(map_buff->layout_map_count);
2230283b4a9bSStephen M. Cameron 	for (map = 0; map < map_cnt; map++) {
2231283b4a9bSStephen M. Cameron 		dev_info(&h->pdev->dev, "Map%u:\n", map);
2232283b4a9bSStephen M. Cameron 		row_cnt = le16_to_cpu(map_buff->row_cnt);
2233283b4a9bSStephen M. Cameron 		for (row = 0; row < row_cnt; row++) {
2234283b4a9bSStephen M. Cameron 			dev_info(&h->pdev->dev, "  Row%u:\n", row);
2235283b4a9bSStephen M. Cameron 			disks_per_row =
2236283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->data_disks_per_row);
2237283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2238283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2239283b4a9bSStephen M. Cameron 					"    D%02u: h=0x%04x xor=%u,%u\n",
2240283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2241283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2242283b4a9bSStephen M. Cameron 			disks_per_row =
2243283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->metadata_disks_per_row);
2244283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2245283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2246283b4a9bSStephen M. Cameron 					"    M%02u: h=0x%04x xor=%u,%u\n",
2247283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2248283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2249283b4a9bSStephen M. Cameron 		}
2250283b4a9bSStephen M. Cameron 	}
2251283b4a9bSStephen M. Cameron }
2252283b4a9bSStephen M. Cameron #else
2253283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
2254283b4a9bSStephen M. Cameron 			__attribute__((unused)) int rc,
2255283b4a9bSStephen M. Cameron 			__attribute__((unused)) struct raid_map_data *map_buff)
2256283b4a9bSStephen M. Cameron {
2257283b4a9bSStephen M. Cameron }
2258283b4a9bSStephen M. Cameron #endif
2259283b4a9bSStephen M. Cameron 
2260283b4a9bSStephen M. Cameron static int hpsa_get_raid_map(struct ctlr_info *h,
2261283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2262283b4a9bSStephen M. Cameron {
2263283b4a9bSStephen M. Cameron 	int rc = 0;
2264283b4a9bSStephen M. Cameron 	struct CommandList *c;
2265283b4a9bSStephen M. Cameron 	struct ErrorInfo *ei;
2266283b4a9bSStephen M. Cameron 
2267283b4a9bSStephen M. Cameron 	c = cmd_special_alloc(h);
2268283b4a9bSStephen M. Cameron 	if (c == NULL) {
2269283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2270283b4a9bSStephen M. Cameron 		return -ENOMEM;
2271283b4a9bSStephen M. Cameron 	}
2272283b4a9bSStephen M. Cameron 	if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
2273283b4a9bSStephen M. Cameron 			sizeof(this_device->raid_map), 0,
2274283b4a9bSStephen M. Cameron 			scsi3addr, TYPE_CMD)) {
2275283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "Out of memory in hpsa_get_raid_map()\n");
2276283b4a9bSStephen M. Cameron 		cmd_special_free(h, c);
2277283b4a9bSStephen M. Cameron 		return -ENOMEM;
2278283b4a9bSStephen M. Cameron 	}
2279283b4a9bSStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2280283b4a9bSStephen M. Cameron 	ei = c->err_info;
2281283b4a9bSStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2282d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2283283b4a9bSStephen M. Cameron 		cmd_special_free(h, c);
2284283b4a9bSStephen M. Cameron 		return -1;
2285283b4a9bSStephen M. Cameron 	}
2286283b4a9bSStephen M. Cameron 	cmd_special_free(h, c);
2287283b4a9bSStephen M. Cameron 
2288283b4a9bSStephen M. Cameron 	/* @todo in the future, dynamically allocate RAID map memory */
2289283b4a9bSStephen M. Cameron 	if (le32_to_cpu(this_device->raid_map.structure_size) >
2290283b4a9bSStephen M. Cameron 				sizeof(this_device->raid_map)) {
2291283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
2292283b4a9bSStephen M. Cameron 		rc = -1;
2293283b4a9bSStephen M. Cameron 	}
2294283b4a9bSStephen M. Cameron 	hpsa_debug_map_buff(h, rc, &this_device->raid_map);
2295283b4a9bSStephen M. Cameron 	return rc;
2296283b4a9bSStephen M. Cameron }
2297283b4a9bSStephen M. Cameron 
22981b70150aSStephen M. Cameron static int hpsa_vpd_page_supported(struct ctlr_info *h,
22991b70150aSStephen M. Cameron 	unsigned char scsi3addr[], u8 page)
23001b70150aSStephen M. Cameron {
23011b70150aSStephen M. Cameron 	int rc;
23021b70150aSStephen M. Cameron 	int i;
23031b70150aSStephen M. Cameron 	int pages;
23041b70150aSStephen M. Cameron 	unsigned char *buf, bufsize;
23051b70150aSStephen M. Cameron 
23061b70150aSStephen M. Cameron 	buf = kzalloc(256, GFP_KERNEL);
23071b70150aSStephen M. Cameron 	if (!buf)
23081b70150aSStephen M. Cameron 		return 0;
23091b70150aSStephen M. Cameron 
23101b70150aSStephen M. Cameron 	/* Get the size of the page list first */
23111b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
23121b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
23131b70150aSStephen M. Cameron 				buf, HPSA_VPD_HEADER_SZ);
23141b70150aSStephen M. Cameron 	if (rc != 0)
23151b70150aSStephen M. Cameron 		goto exit_unsupported;
23161b70150aSStephen M. Cameron 	pages = buf[3];
23171b70150aSStephen M. Cameron 	if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
23181b70150aSStephen M. Cameron 		bufsize = pages + HPSA_VPD_HEADER_SZ;
23191b70150aSStephen M. Cameron 	else
23201b70150aSStephen M. Cameron 		bufsize = 255;
23211b70150aSStephen M. Cameron 
23221b70150aSStephen M. Cameron 	/* Get the whole VPD page list */
23231b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
23241b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
23251b70150aSStephen M. Cameron 				buf, bufsize);
23261b70150aSStephen M. Cameron 	if (rc != 0)
23271b70150aSStephen M. Cameron 		goto exit_unsupported;
23281b70150aSStephen M. Cameron 
23291b70150aSStephen M. Cameron 	pages = buf[3];
23301b70150aSStephen M. Cameron 	for (i = 1; i <= pages; i++)
23311b70150aSStephen M. Cameron 		if (buf[3 + i] == page)
23321b70150aSStephen M. Cameron 			goto exit_supported;
23331b70150aSStephen M. Cameron exit_unsupported:
23341b70150aSStephen M. Cameron 	kfree(buf);
23351b70150aSStephen M. Cameron 	return 0;
23361b70150aSStephen M. Cameron exit_supported:
23371b70150aSStephen M. Cameron 	kfree(buf);
23381b70150aSStephen M. Cameron 	return 1;
23391b70150aSStephen M. Cameron }
23401b70150aSStephen M. Cameron 
2341283b4a9bSStephen M. Cameron static void hpsa_get_ioaccel_status(struct ctlr_info *h,
2342283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2343283b4a9bSStephen M. Cameron {
2344283b4a9bSStephen M. Cameron 	int rc;
2345283b4a9bSStephen M. Cameron 	unsigned char *buf;
2346283b4a9bSStephen M. Cameron 	u8 ioaccel_status;
2347283b4a9bSStephen M. Cameron 
2348283b4a9bSStephen M. Cameron 	this_device->offload_config = 0;
2349283b4a9bSStephen M. Cameron 	this_device->offload_enabled = 0;
2350283b4a9bSStephen M. Cameron 
2351283b4a9bSStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2352283b4a9bSStephen M. Cameron 	if (!buf)
2353283b4a9bSStephen M. Cameron 		return;
23541b70150aSStephen M. Cameron 	if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
23551b70150aSStephen M. Cameron 		goto out;
2356283b4a9bSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
2357b7bb24ebSStephen M. Cameron 			VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
2358283b4a9bSStephen M. Cameron 	if (rc != 0)
2359283b4a9bSStephen M. Cameron 		goto out;
2360283b4a9bSStephen M. Cameron 
2361283b4a9bSStephen M. Cameron #define IOACCEL_STATUS_BYTE 4
2362283b4a9bSStephen M. Cameron #define OFFLOAD_CONFIGURED_BIT 0x01
2363283b4a9bSStephen M. Cameron #define OFFLOAD_ENABLED_BIT 0x02
2364283b4a9bSStephen M. Cameron 	ioaccel_status = buf[IOACCEL_STATUS_BYTE];
2365283b4a9bSStephen M. Cameron 	this_device->offload_config =
2366283b4a9bSStephen M. Cameron 		!!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
2367283b4a9bSStephen M. Cameron 	if (this_device->offload_config) {
2368283b4a9bSStephen M. Cameron 		this_device->offload_enabled =
2369283b4a9bSStephen M. Cameron 			!!(ioaccel_status & OFFLOAD_ENABLED_BIT);
2370283b4a9bSStephen M. Cameron 		if (hpsa_get_raid_map(h, scsi3addr, this_device))
2371283b4a9bSStephen M. Cameron 			this_device->offload_enabled = 0;
2372283b4a9bSStephen M. Cameron 	}
2373283b4a9bSStephen M. Cameron out:
2374283b4a9bSStephen M. Cameron 	kfree(buf);
2375283b4a9bSStephen M. Cameron 	return;
2376283b4a9bSStephen M. Cameron }
2377283b4a9bSStephen M. Cameron 
2378edd16368SStephen M. Cameron /* Get the device id from inquiry page 0x83 */
2379edd16368SStephen M. Cameron static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
2380edd16368SStephen M. Cameron 	unsigned char *device_id, int buflen)
2381edd16368SStephen M. Cameron {
2382edd16368SStephen M. Cameron 	int rc;
2383edd16368SStephen M. Cameron 	unsigned char *buf;
2384edd16368SStephen M. Cameron 
2385edd16368SStephen M. Cameron 	if (buflen > 16)
2386edd16368SStephen M. Cameron 		buflen = 16;
2387edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2388edd16368SStephen M. Cameron 	if (!buf)
2389edd16368SStephen M. Cameron 		return -1;
2390b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0x83, buf, 64);
2391edd16368SStephen M. Cameron 	if (rc == 0)
2392edd16368SStephen M. Cameron 		memcpy(device_id, &buf[8], buflen);
2393edd16368SStephen M. Cameron 	kfree(buf);
2394edd16368SStephen M. Cameron 	return rc != 0;
2395edd16368SStephen M. Cameron }
2396edd16368SStephen M. Cameron 
2397edd16368SStephen M. Cameron static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
2398edd16368SStephen M. Cameron 		struct ReportLUNdata *buf, int bufsize,
2399edd16368SStephen M. Cameron 		int extended_response)
2400edd16368SStephen M. Cameron {
2401edd16368SStephen M. Cameron 	int rc = IO_OK;
2402edd16368SStephen M. Cameron 	struct CommandList *c;
2403edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
2404edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2405edd16368SStephen M. Cameron 
2406edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
2407edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
2408edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2409edd16368SStephen M. Cameron 		return -1;
2410edd16368SStephen M. Cameron 	}
2411e89c0ae7SStephen M. Cameron 	/* address the controller */
2412e89c0ae7SStephen M. Cameron 	memset(scsi3addr, 0, sizeof(scsi3addr));
2413a2dac136SStephen M. Cameron 	if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
2414a2dac136SStephen M. Cameron 		buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
2415a2dac136SStephen M. Cameron 		rc = -1;
2416a2dac136SStephen M. Cameron 		goto out;
2417a2dac136SStephen M. Cameron 	}
2418edd16368SStephen M. Cameron 	if (extended_response)
2419edd16368SStephen M. Cameron 		c->Request.CDB[1] = extended_response;
2420edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2421edd16368SStephen M. Cameron 	ei = c->err_info;
2422edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 &&
2423edd16368SStephen M. Cameron 	    ei->CommandStatus != CMD_DATA_UNDERRUN) {
2424d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2425edd16368SStephen M. Cameron 		rc = -1;
2426283b4a9bSStephen M. Cameron 	} else {
2427283b4a9bSStephen M. Cameron 		if (buf->extended_response_flag != extended_response) {
2428283b4a9bSStephen M. Cameron 			dev_err(&h->pdev->dev,
2429283b4a9bSStephen M. Cameron 				"report luns requested format %u, got %u\n",
2430283b4a9bSStephen M. Cameron 				extended_response,
2431283b4a9bSStephen M. Cameron 				buf->extended_response_flag);
2432283b4a9bSStephen M. Cameron 			rc = -1;
2433283b4a9bSStephen M. Cameron 		}
2434edd16368SStephen M. Cameron 	}
2435a2dac136SStephen M. Cameron out:
2436edd16368SStephen M. Cameron 	cmd_special_free(h, c);
2437edd16368SStephen M. Cameron 	return rc;
2438edd16368SStephen M. Cameron }
2439edd16368SStephen M. Cameron 
2440edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
2441edd16368SStephen M. Cameron 		struct ReportLUNdata *buf,
2442edd16368SStephen M. Cameron 		int bufsize, int extended_response)
2443edd16368SStephen M. Cameron {
2444edd16368SStephen M. Cameron 	return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
2445edd16368SStephen M. Cameron }
2446edd16368SStephen M. Cameron 
2447edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
2448edd16368SStephen M. Cameron 		struct ReportLUNdata *buf, int bufsize)
2449edd16368SStephen M. Cameron {
2450edd16368SStephen M. Cameron 	return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
2451edd16368SStephen M. Cameron }
2452edd16368SStephen M. Cameron 
2453edd16368SStephen M. Cameron static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
2454edd16368SStephen M. Cameron 	int bus, int target, int lun)
2455edd16368SStephen M. Cameron {
2456edd16368SStephen M. Cameron 	device->bus = bus;
2457edd16368SStephen M. Cameron 	device->target = target;
2458edd16368SStephen M. Cameron 	device->lun = lun;
2459edd16368SStephen M. Cameron }
2460edd16368SStephen M. Cameron 
2461*9846590eSStephen M. Cameron /* Use VPD inquiry to get details of volume status */
2462*9846590eSStephen M. Cameron static int hpsa_get_volume_status(struct ctlr_info *h,
2463*9846590eSStephen M. Cameron 					unsigned char scsi3addr[])
2464*9846590eSStephen M. Cameron {
2465*9846590eSStephen M. Cameron 	int rc;
2466*9846590eSStephen M. Cameron 	int status;
2467*9846590eSStephen M. Cameron 	int size;
2468*9846590eSStephen M. Cameron 	unsigned char *buf;
2469*9846590eSStephen M. Cameron 
2470*9846590eSStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2471*9846590eSStephen M. Cameron 	if (!buf)
2472*9846590eSStephen M. Cameron 		return HPSA_VPD_LV_STATUS_UNSUPPORTED;
2473*9846590eSStephen M. Cameron 
2474*9846590eSStephen M. Cameron 	/* Does controller have VPD for logical volume status? */
2475*9846590eSStephen M. Cameron 	if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_STATUS)) {
2476*9846590eSStephen M. Cameron 		dev_warn(&h->pdev->dev, "Logical volume status VPD page is unsupported.\n");
2477*9846590eSStephen M. Cameron 		goto exit_failed;
2478*9846590eSStephen M. Cameron 	}
2479*9846590eSStephen M. Cameron 
2480*9846590eSStephen M. Cameron 	/* Get the size of the VPD return buffer */
2481*9846590eSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
2482*9846590eSStephen M. Cameron 					buf, HPSA_VPD_HEADER_SZ);
2483*9846590eSStephen M. Cameron 	if (rc != 0) {
2484*9846590eSStephen M. Cameron 		dev_warn(&h->pdev->dev, "Logical volume status VPD inquiry failed.\n");
2485*9846590eSStephen M. Cameron 		goto exit_failed;
2486*9846590eSStephen M. Cameron 	}
2487*9846590eSStephen M. Cameron 	size = buf[3];
2488*9846590eSStephen M. Cameron 
2489*9846590eSStephen M. Cameron 	/* Now get the whole VPD buffer */
2490*9846590eSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
2491*9846590eSStephen M. Cameron 					buf, size + HPSA_VPD_HEADER_SZ);
2492*9846590eSStephen M. Cameron 	if (rc != 0) {
2493*9846590eSStephen M. Cameron 		dev_warn(&h->pdev->dev, "Logical volume status VPD inquiry failed.\n");
2494*9846590eSStephen M. Cameron 		goto exit_failed;
2495*9846590eSStephen M. Cameron 	}
2496*9846590eSStephen M. Cameron 	status = buf[4]; /* status byte */
2497*9846590eSStephen M. Cameron 
2498*9846590eSStephen M. Cameron 	kfree(buf);
2499*9846590eSStephen M. Cameron 	return status;
2500*9846590eSStephen M. Cameron exit_failed:
2501*9846590eSStephen M. Cameron 	kfree(buf);
2502*9846590eSStephen M. Cameron 	return HPSA_VPD_LV_STATUS_UNSUPPORTED;
2503*9846590eSStephen M. Cameron }
2504*9846590eSStephen M. Cameron 
2505*9846590eSStephen M. Cameron /* Determine offline status of a volume.
2506*9846590eSStephen M. Cameron  * Return either:
2507*9846590eSStephen M. Cameron  *  0 (not offline)
2508*9846590eSStephen M. Cameron  * -1 (offline for unknown reasons)
2509*9846590eSStephen M. Cameron  *  # (integer code indicating one of several NOT READY states
2510*9846590eSStephen M. Cameron  *     describing why a volume is to be kept offline)
2511*9846590eSStephen M. Cameron  */
2512*9846590eSStephen M. Cameron static unsigned char hpsa_volume_offline(struct ctlr_info *h,
2513*9846590eSStephen M. Cameron 					unsigned char scsi3addr[])
2514*9846590eSStephen M. Cameron {
2515*9846590eSStephen M. Cameron 	struct CommandList *c;
2516*9846590eSStephen M. Cameron 	unsigned char *sense, sense_key, asc, ascq;
2517*9846590eSStephen M. Cameron 	int ldstat = 0;
2518*9846590eSStephen M. Cameron 	u16 cmd_status;
2519*9846590eSStephen M. Cameron 	u8 scsi_status;
2520*9846590eSStephen M. Cameron #define ASC_LUN_NOT_READY 0x04
2521*9846590eSStephen M. Cameron #define ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS 0x04
2522*9846590eSStephen M. Cameron #define ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ 0x02
2523*9846590eSStephen M. Cameron 
2524*9846590eSStephen M. Cameron 	c = cmd_alloc(h);
2525*9846590eSStephen M. Cameron 	if (!c)
2526*9846590eSStephen M. Cameron 		return 0;
2527*9846590eSStephen M. Cameron 	(void) fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, scsi3addr, TYPE_CMD);
2528*9846590eSStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core(h, c);
2529*9846590eSStephen M. Cameron 	sense = c->err_info->SenseInfo;
2530*9846590eSStephen M. Cameron 	sense_key = sense[2];
2531*9846590eSStephen M. Cameron 	asc = sense[12];
2532*9846590eSStephen M. Cameron 	ascq = sense[13];
2533*9846590eSStephen M. Cameron 	cmd_status = c->err_info->CommandStatus;
2534*9846590eSStephen M. Cameron 	scsi_status = c->err_info->ScsiStatus;
2535*9846590eSStephen M. Cameron 	cmd_free(h, c);
2536*9846590eSStephen M. Cameron 	/* Is the volume 'not ready'? */
2537*9846590eSStephen M. Cameron 	if (cmd_status != CMD_TARGET_STATUS ||
2538*9846590eSStephen M. Cameron 		scsi_status != SAM_STAT_CHECK_CONDITION ||
2539*9846590eSStephen M. Cameron 		sense_key != NOT_READY ||
2540*9846590eSStephen M. Cameron 		asc != ASC_LUN_NOT_READY)  {
2541*9846590eSStephen M. Cameron 		return 0;
2542*9846590eSStephen M. Cameron 	}
2543*9846590eSStephen M. Cameron 
2544*9846590eSStephen M. Cameron 	/* Determine the reason for not ready state */
2545*9846590eSStephen M. Cameron 	ldstat = hpsa_get_volume_status(h, scsi3addr);
2546*9846590eSStephen M. Cameron 
2547*9846590eSStephen M. Cameron 	/* Keep volume offline in certain cases: */
2548*9846590eSStephen M. Cameron 	switch (ldstat) {
2549*9846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ERASE:
2550*9846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_RPI:
2551*9846590eSStephen M. Cameron 	case HPSA_LV_PENDING_RPI:
2552*9846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_NO_KEY:
2553*9846590eSStephen M. Cameron 	case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
2554*9846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION:
2555*9846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
2556*9846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
2557*9846590eSStephen M. Cameron 		return ldstat;
2558*9846590eSStephen M. Cameron 	case HPSA_VPD_LV_STATUS_UNSUPPORTED:
2559*9846590eSStephen M. Cameron 		/* If VPD status page isn't available,
2560*9846590eSStephen M. Cameron 		 * use ASC/ASCQ to determine state
2561*9846590eSStephen M. Cameron 		 */
2562*9846590eSStephen M. Cameron 		if ((ascq == ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS) ||
2563*9846590eSStephen M. Cameron 			(ascq == ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ))
2564*9846590eSStephen M. Cameron 			return ldstat;
2565*9846590eSStephen M. Cameron 		break;
2566*9846590eSStephen M. Cameron 	default:
2567*9846590eSStephen M. Cameron 		break;
2568*9846590eSStephen M. Cameron 	}
2569*9846590eSStephen M. Cameron 	return 0;
2570*9846590eSStephen M. Cameron }
2571*9846590eSStephen M. Cameron 
2572edd16368SStephen M. Cameron static int hpsa_update_device_info(struct ctlr_info *h,
25730b0e1d6cSStephen M. Cameron 	unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
25740b0e1d6cSStephen M. Cameron 	unsigned char *is_OBDR_device)
2575edd16368SStephen M. Cameron {
25760b0e1d6cSStephen M. Cameron 
25770b0e1d6cSStephen M. Cameron #define OBDR_SIG_OFFSET 43
25780b0e1d6cSStephen M. Cameron #define OBDR_TAPE_SIG "$DR-10"
25790b0e1d6cSStephen M. Cameron #define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
25800b0e1d6cSStephen M. Cameron #define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
25810b0e1d6cSStephen M. Cameron 
2582ea6d3bc3SStephen M. Cameron 	unsigned char *inq_buff;
25830b0e1d6cSStephen M. Cameron 	unsigned char *obdr_sig;
2584edd16368SStephen M. Cameron 
2585ea6d3bc3SStephen M. Cameron 	inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
2586edd16368SStephen M. Cameron 	if (!inq_buff)
2587edd16368SStephen M. Cameron 		goto bail_out;
2588edd16368SStephen M. Cameron 
2589edd16368SStephen M. Cameron 	/* Do an inquiry to the device to see what it is. */
2590edd16368SStephen M. Cameron 	if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
2591edd16368SStephen M. Cameron 		(unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
2592edd16368SStephen M. Cameron 		/* Inquiry failed (msg printed already) */
2593edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev,
2594edd16368SStephen M. Cameron 			"hpsa_update_device_info: inquiry failed\n");
2595edd16368SStephen M. Cameron 		goto bail_out;
2596edd16368SStephen M. Cameron 	}
2597edd16368SStephen M. Cameron 
2598edd16368SStephen M. Cameron 	this_device->devtype = (inq_buff[0] & 0x1f);
2599edd16368SStephen M. Cameron 	memcpy(this_device->scsi3addr, scsi3addr, 8);
2600edd16368SStephen M. Cameron 	memcpy(this_device->vendor, &inq_buff[8],
2601edd16368SStephen M. Cameron 		sizeof(this_device->vendor));
2602edd16368SStephen M. Cameron 	memcpy(this_device->model, &inq_buff[16],
2603edd16368SStephen M. Cameron 		sizeof(this_device->model));
2604edd16368SStephen M. Cameron 	memset(this_device->device_id, 0,
2605edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
2606edd16368SStephen M. Cameron 	hpsa_get_device_id(h, scsi3addr, this_device->device_id,
2607edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
2608edd16368SStephen M. Cameron 
2609edd16368SStephen M. Cameron 	if (this_device->devtype == TYPE_DISK &&
2610283b4a9bSStephen M. Cameron 		is_logical_dev_addr_mode(scsi3addr)) {
2611edd16368SStephen M. Cameron 		hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
2612283b4a9bSStephen M. Cameron 		if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
2613283b4a9bSStephen M. Cameron 			hpsa_get_ioaccel_status(h, scsi3addr, this_device);
2614*9846590eSStephen M. Cameron 		this_device->volume_offline =
2615*9846590eSStephen M. Cameron 			hpsa_volume_offline(h, scsi3addr);
2616283b4a9bSStephen M. Cameron 	} else {
2617edd16368SStephen M. Cameron 		this_device->raid_level = RAID_UNKNOWN;
2618283b4a9bSStephen M. Cameron 		this_device->offload_config = 0;
2619283b4a9bSStephen M. Cameron 		this_device->offload_enabled = 0;
2620*9846590eSStephen M. Cameron 		this_device->volume_offline = 0;
2621283b4a9bSStephen M. Cameron 	}
2622edd16368SStephen M. Cameron 
26230b0e1d6cSStephen M. Cameron 	if (is_OBDR_device) {
26240b0e1d6cSStephen M. Cameron 		/* See if this is a One-Button-Disaster-Recovery device
26250b0e1d6cSStephen M. Cameron 		 * by looking for "$DR-10" at offset 43 in inquiry data.
26260b0e1d6cSStephen M. Cameron 		 */
26270b0e1d6cSStephen M. Cameron 		obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
26280b0e1d6cSStephen M. Cameron 		*is_OBDR_device = (this_device->devtype == TYPE_ROM &&
26290b0e1d6cSStephen M. Cameron 					strncmp(obdr_sig, OBDR_TAPE_SIG,
26300b0e1d6cSStephen M. Cameron 						OBDR_SIG_LEN) == 0);
26310b0e1d6cSStephen M. Cameron 	}
26320b0e1d6cSStephen M. Cameron 
2633edd16368SStephen M. Cameron 	kfree(inq_buff);
2634edd16368SStephen M. Cameron 	return 0;
2635edd16368SStephen M. Cameron 
2636edd16368SStephen M. Cameron bail_out:
2637edd16368SStephen M. Cameron 	kfree(inq_buff);
2638edd16368SStephen M. Cameron 	return 1;
2639edd16368SStephen M. Cameron }
2640edd16368SStephen M. Cameron 
26414f4eb9f1SScott Teel static unsigned char *ext_target_model[] = {
2642edd16368SStephen M. Cameron 	"MSA2012",
2643edd16368SStephen M. Cameron 	"MSA2024",
2644edd16368SStephen M. Cameron 	"MSA2312",
2645edd16368SStephen M. Cameron 	"MSA2324",
2646fda38518SStephen M. Cameron 	"P2000 G3 SAS",
2647e06c8e5cSStephen M. Cameron 	"MSA 2040 SAS",
2648edd16368SStephen M. Cameron 	NULL,
2649edd16368SStephen M. Cameron };
2650edd16368SStephen M. Cameron 
26514f4eb9f1SScott Teel static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
2652edd16368SStephen M. Cameron {
2653edd16368SStephen M. Cameron 	int i;
2654edd16368SStephen M. Cameron 
26554f4eb9f1SScott Teel 	for (i = 0; ext_target_model[i]; i++)
26564f4eb9f1SScott Teel 		if (strncmp(device->model, ext_target_model[i],
26574f4eb9f1SScott Teel 			strlen(ext_target_model[i])) == 0)
2658edd16368SStephen M. Cameron 			return 1;
2659edd16368SStephen M. Cameron 	return 0;
2660edd16368SStephen M. Cameron }
2661edd16368SStephen M. Cameron 
2662edd16368SStephen M. Cameron /* Helper function to assign bus, target, lun mapping of devices.
26634f4eb9f1SScott Teel  * Puts non-external target logical volumes on bus 0, external target logical
2664edd16368SStephen M. Cameron  * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
2665edd16368SStephen M. Cameron  * Logical drive target and lun are assigned at this time, but
2666edd16368SStephen M. Cameron  * physical device lun and target assignment are deferred (assigned
2667edd16368SStephen M. Cameron  * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
2668edd16368SStephen M. Cameron  */
2669edd16368SStephen M. Cameron static void figure_bus_target_lun(struct ctlr_info *h,
26701f310bdeSStephen M. Cameron 	u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
2671edd16368SStephen M. Cameron {
26721f310bdeSStephen M. Cameron 	u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
2673edd16368SStephen M. Cameron 
26741f310bdeSStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes)) {
26751f310bdeSStephen M. Cameron 		/* physical device, target and lun filled in later */
26761f310bdeSStephen M. Cameron 		if (is_hba_lunid(lunaddrbytes))
26771f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
26781f310bdeSStephen M. Cameron 		else
26791f310bdeSStephen M. Cameron 			/* defer target, lun assignment for physical devices */
26801f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 2, -1, -1);
26811f310bdeSStephen M. Cameron 		return;
26821f310bdeSStephen M. Cameron 	}
26831f310bdeSStephen M. Cameron 	/* It's a logical device */
26844f4eb9f1SScott Teel 	if (is_ext_target(h, device)) {
26854f4eb9f1SScott Teel 		/* external target way, put logicals on bus 1
2686339b2b14SStephen M. Cameron 		 * and match target/lun numbers box
26871f310bdeSStephen M. Cameron 		 * reports, other smart array, bus 0, target 0, match lunid
2688339b2b14SStephen M. Cameron 		 */
26891f310bdeSStephen M. Cameron 		hpsa_set_bus_target_lun(device,
26901f310bdeSStephen M. Cameron 			1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
26911f310bdeSStephen M. Cameron 		return;
2692339b2b14SStephen M. Cameron 	}
26931f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
2694edd16368SStephen M. Cameron }
2695edd16368SStephen M. Cameron 
2696edd16368SStephen M. Cameron /*
2697edd16368SStephen M. Cameron  * If there is no lun 0 on a target, linux won't find any devices.
26984f4eb9f1SScott Teel  * For the external targets (arrays), we have to manually detect the enclosure
2699edd16368SStephen M. Cameron  * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
2700edd16368SStephen M. Cameron  * it for some reason.  *tmpdevice is the target we're adding,
2701edd16368SStephen M. Cameron  * this_device is a pointer into the current element of currentsd[]
2702edd16368SStephen M. Cameron  * that we're building up in update_scsi_devices(), below.
2703edd16368SStephen M. Cameron  * lunzerobits is a bitmap that tracks which targets already have a
2704edd16368SStephen M. Cameron  * lun 0 assigned.
2705edd16368SStephen M. Cameron  * Returns 1 if an enclosure was added, 0 if not.
2706edd16368SStephen M. Cameron  */
27074f4eb9f1SScott Teel static int add_ext_target_dev(struct ctlr_info *h,
2708edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *tmpdevice,
270901a02ffcSStephen M. Cameron 	struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
27104f4eb9f1SScott Teel 	unsigned long lunzerobits[], int *n_ext_target_devs)
2711edd16368SStephen M. Cameron {
2712edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
2713edd16368SStephen M. Cameron 
27141f310bdeSStephen M. Cameron 	if (test_bit(tmpdevice->target, lunzerobits))
2715edd16368SStephen M. Cameron 		return 0; /* There is already a lun 0 on this target. */
2716edd16368SStephen M. Cameron 
2717edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes))
2718edd16368SStephen M. Cameron 		return 0; /* It's the logical targets that may lack lun 0. */
2719edd16368SStephen M. Cameron 
27204f4eb9f1SScott Teel 	if (!is_ext_target(h, tmpdevice))
27214f4eb9f1SScott Teel 		return 0; /* Only external target devices have this problem. */
2722edd16368SStephen M. Cameron 
27231f310bdeSStephen M. Cameron 	if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
2724edd16368SStephen M. Cameron 		return 0;
2725edd16368SStephen M. Cameron 
2726c4f8a299SStephen M. Cameron 	memset(scsi3addr, 0, 8);
27271f310bdeSStephen M. Cameron 	scsi3addr[3] = tmpdevice->target;
2728edd16368SStephen M. Cameron 	if (is_hba_lunid(scsi3addr))
2729edd16368SStephen M. Cameron 		return 0; /* Don't add the RAID controller here. */
2730edd16368SStephen M. Cameron 
2731339b2b14SStephen M. Cameron 	if (is_scsi_rev_5(h))
2732339b2b14SStephen M. Cameron 		return 0; /* p1210m doesn't need to do this. */
2733339b2b14SStephen M. Cameron 
27344f4eb9f1SScott Teel 	if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
2735aca4a520SScott Teel 		dev_warn(&h->pdev->dev, "Maximum number of external "
2736aca4a520SScott Teel 			"target devices exceeded.  Check your hardware "
2737edd16368SStephen M. Cameron 			"configuration.");
2738edd16368SStephen M. Cameron 		return 0;
2739edd16368SStephen M. Cameron 	}
2740edd16368SStephen M. Cameron 
27410b0e1d6cSStephen M. Cameron 	if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
2742edd16368SStephen M. Cameron 		return 0;
27434f4eb9f1SScott Teel 	(*n_ext_target_devs)++;
27441f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(this_device,
27451f310bdeSStephen M. Cameron 				tmpdevice->bus, tmpdevice->target, 0);
27461f310bdeSStephen M. Cameron 	set_bit(tmpdevice->target, lunzerobits);
2747edd16368SStephen M. Cameron 	return 1;
2748edd16368SStephen M. Cameron }
2749edd16368SStephen M. Cameron 
2750edd16368SStephen M. Cameron /*
275154b6e9e9SScott Teel  * Get address of physical disk used for an ioaccel2 mode command:
275254b6e9e9SScott Teel  *	1. Extract ioaccel2 handle from the command.
275354b6e9e9SScott Teel  *	2. Find a matching ioaccel2 handle from list of physical disks.
275454b6e9e9SScott Teel  *	3. Return:
275554b6e9e9SScott Teel  *		1 and set scsi3addr to address of matching physical
275654b6e9e9SScott Teel  *		0 if no matching physical disk was found.
275754b6e9e9SScott Teel  */
275854b6e9e9SScott Teel static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
275954b6e9e9SScott Teel 	struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
276054b6e9e9SScott Teel {
276154b6e9e9SScott Teel 	struct ReportExtendedLUNdata *physicals = NULL;
276254b6e9e9SScott Teel 	int responsesize = 24;	/* size of physical extended response */
276354b6e9e9SScott Teel 	int extended = 2;	/* flag forces reporting 'other dev info'. */
276454b6e9e9SScott Teel 	int reportsize = sizeof(*physicals) + HPSA_MAX_PHYS_LUN * responsesize;
276554b6e9e9SScott Teel 	u32 nphysicals = 0;	/* number of reported physical devs */
276654b6e9e9SScott Teel 	int found = 0;		/* found match (1) or not (0) */
276754b6e9e9SScott Teel 	u32 find;		/* handle we need to match */
276854b6e9e9SScott Teel 	int i;
276954b6e9e9SScott Teel 	struct scsi_cmnd *scmd;	/* scsi command within request being aborted */
277054b6e9e9SScott Teel 	struct hpsa_scsi_dev_t *d; /* device of request being aborted */
277154b6e9e9SScott Teel 	struct io_accel2_cmd *c2a; /* ioaccel2 command to abort */
277254b6e9e9SScott Teel 	u32 it_nexus;		/* 4 byte device handle for the ioaccel2 cmd */
277354b6e9e9SScott Teel 	u32 scsi_nexus;		/* 4 byte device handle for the ioaccel2 cmd */
277454b6e9e9SScott Teel 
277554b6e9e9SScott Teel 	if (ioaccel2_cmd_to_abort->cmd_type != CMD_IOACCEL2)
277654b6e9e9SScott Teel 		return 0; /* no match */
277754b6e9e9SScott Teel 
277854b6e9e9SScott Teel 	/* point to the ioaccel2 device handle */
277954b6e9e9SScott Teel 	c2a = &h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
278054b6e9e9SScott Teel 	if (c2a == NULL)
278154b6e9e9SScott Teel 		return 0; /* no match */
278254b6e9e9SScott Teel 
278354b6e9e9SScott Teel 	scmd = (struct scsi_cmnd *) ioaccel2_cmd_to_abort->scsi_cmd;
278454b6e9e9SScott Teel 	if (scmd == NULL)
278554b6e9e9SScott Teel 		return 0; /* no match */
278654b6e9e9SScott Teel 
278754b6e9e9SScott Teel 	d = scmd->device->hostdata;
278854b6e9e9SScott Teel 	if (d == NULL)
278954b6e9e9SScott Teel 		return 0; /* no match */
279054b6e9e9SScott Teel 
279154b6e9e9SScott Teel 	it_nexus = cpu_to_le32((u32) d->ioaccel_handle);
279254b6e9e9SScott Teel 	scsi_nexus = cpu_to_le32((u32) c2a->scsi_nexus);
279354b6e9e9SScott Teel 	find = c2a->scsi_nexus;
279454b6e9e9SScott Teel 
27952ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
27962ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
27972ba8bfc8SStephen 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",
27982ba8bfc8SStephen M. Cameron 			__func__, scsi_nexus,
27992ba8bfc8SStephen M. Cameron 			d->device_id[0], d->device_id[1], d->device_id[2],
28002ba8bfc8SStephen M. Cameron 			d->device_id[3], d->device_id[4], d->device_id[5],
28012ba8bfc8SStephen M. Cameron 			d->device_id[6], d->device_id[7], d->device_id[8],
28022ba8bfc8SStephen M. Cameron 			d->device_id[9], d->device_id[10], d->device_id[11],
28032ba8bfc8SStephen M. Cameron 			d->device_id[12], d->device_id[13], d->device_id[14],
28042ba8bfc8SStephen M. Cameron 			d->device_id[15]);
28052ba8bfc8SStephen M. Cameron 
280654b6e9e9SScott Teel 	/* Get the list of physical devices */
280754b6e9e9SScott Teel 	physicals = kzalloc(reportsize, GFP_KERNEL);
280854b6e9e9SScott Teel 	if (hpsa_scsi_do_report_phys_luns(h, (struct ReportLUNdata *) physicals,
280954b6e9e9SScott Teel 		reportsize, extended)) {
281054b6e9e9SScott Teel 		dev_err(&h->pdev->dev,
281154b6e9e9SScott Teel 			"Can't lookup %s device handle: report physical LUNs failed.\n",
281254b6e9e9SScott Teel 			"HP SSD Smart Path");
281354b6e9e9SScott Teel 		kfree(physicals);
281454b6e9e9SScott Teel 		return 0;
281554b6e9e9SScott Teel 	}
281654b6e9e9SScott Teel 	nphysicals = be32_to_cpu(*((__be32 *)physicals->LUNListLength)) /
281754b6e9e9SScott Teel 							responsesize;
281854b6e9e9SScott Teel 
281954b6e9e9SScott Teel 
282054b6e9e9SScott Teel 	/* find ioaccel2 handle in list of physicals: */
282154b6e9e9SScott Teel 	for (i = 0; i < nphysicals; i++) {
282254b6e9e9SScott Teel 		/* handle is in bytes 28-31 of each lun */
282354b6e9e9SScott Teel 		if (memcmp(&((struct ReportExtendedLUNdata *)
282454b6e9e9SScott Teel 				physicals)->LUN[i][20], &find, 4) != 0) {
282554b6e9e9SScott Teel 			continue; /* didn't match */
282654b6e9e9SScott Teel 		}
282754b6e9e9SScott Teel 		found = 1;
282854b6e9e9SScott Teel 		memcpy(scsi3addr, &((struct ReportExtendedLUNdata *)
282954b6e9e9SScott Teel 					physicals)->LUN[i][0], 8);
28302ba8bfc8SStephen M. Cameron 		if (h->raid_offload_debug > 0)
28312ba8bfc8SStephen M. Cameron 			dev_info(&h->pdev->dev,
28322ba8bfc8SStephen M. Cameron 				"%s: Searched h=0x%08x, Found h=0x%08x, scsiaddr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
28332ba8bfc8SStephen M. Cameron 				__func__, find,
28342ba8bfc8SStephen M. Cameron 				((struct ReportExtendedLUNdata *)
28352ba8bfc8SStephen M. Cameron 					physicals)->LUN[i][20],
28362ba8bfc8SStephen M. Cameron 				scsi3addr[0], scsi3addr[1], scsi3addr[2],
28372ba8bfc8SStephen M. Cameron 				scsi3addr[3], scsi3addr[4], scsi3addr[5],
28382ba8bfc8SStephen M. Cameron 				scsi3addr[6], scsi3addr[7]);
283954b6e9e9SScott Teel 		break; /* found it */
284054b6e9e9SScott Teel 	}
284154b6e9e9SScott Teel 
284254b6e9e9SScott Teel 	kfree(physicals);
284354b6e9e9SScott Teel 	if (found)
284454b6e9e9SScott Teel 		return 1;
284554b6e9e9SScott Teel 	else
284654b6e9e9SScott Teel 		return 0;
284754b6e9e9SScott Teel 
284854b6e9e9SScott Teel }
284954b6e9e9SScott Teel /*
2850edd16368SStephen M. Cameron  * Do CISS_REPORT_PHYS and CISS_REPORT_LOG.  Data is returned in physdev,
2851edd16368SStephen M. Cameron  * logdev.  The number of luns in physdev and logdev are returned in
2852edd16368SStephen M. Cameron  * *nphysicals and *nlogicals, respectively.
2853edd16368SStephen M. Cameron  * Returns 0 on success, -1 otherwise.
2854edd16368SStephen M. Cameron  */
2855edd16368SStephen M. Cameron static int hpsa_gather_lun_info(struct ctlr_info *h,
2856edd16368SStephen M. Cameron 	int reportlunsize,
2857283b4a9bSStephen M. Cameron 	struct ReportLUNdata *physdev, u32 *nphysicals, int *physical_mode,
285801a02ffcSStephen M. Cameron 	struct ReportLUNdata *logdev, u32 *nlogicals)
2859edd16368SStephen M. Cameron {
2860283b4a9bSStephen M. Cameron 	int physical_entry_size = 8;
2861283b4a9bSStephen M. Cameron 
2862283b4a9bSStephen M. Cameron 	*physical_mode = 0;
2863283b4a9bSStephen M. Cameron 
2864283b4a9bSStephen M. Cameron 	/* For I/O accelerator mode we need to read physical device handles */
2865317d4adfSMike MIller 	if (h->transMethod & CFGTBL_Trans_io_accel1 ||
2866317d4adfSMike MIller 		h->transMethod & CFGTBL_Trans_io_accel2) {
2867283b4a9bSStephen M. Cameron 		*physical_mode = HPSA_REPORT_PHYS_EXTENDED;
2868283b4a9bSStephen M. Cameron 		physical_entry_size = 24;
2869283b4a9bSStephen M. Cameron 	}
2870a93aa1feSMatt Gates 	if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize,
2871283b4a9bSStephen M. Cameron 							*physical_mode)) {
2872edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
2873edd16368SStephen M. Cameron 		return -1;
2874edd16368SStephen M. Cameron 	}
2875283b4a9bSStephen M. Cameron 	*nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) /
2876283b4a9bSStephen M. Cameron 							physical_entry_size;
2877edd16368SStephen M. Cameron 	if (*nphysicals > HPSA_MAX_PHYS_LUN) {
2878edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
2879edd16368SStephen M. Cameron 			"  %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2880edd16368SStephen M. Cameron 			*nphysicals - HPSA_MAX_PHYS_LUN);
2881edd16368SStephen M. Cameron 		*nphysicals = HPSA_MAX_PHYS_LUN;
2882edd16368SStephen M. Cameron 	}
2883edd16368SStephen M. Cameron 	if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
2884edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
2885edd16368SStephen M. Cameron 		return -1;
2886edd16368SStephen M. Cameron 	}
28876df1e954SStephen M. Cameron 	*nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
2888edd16368SStephen M. Cameron 	/* Reject Logicals in excess of our max capability. */
2889edd16368SStephen M. Cameron 	if (*nlogicals > HPSA_MAX_LUN) {
2890edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
2891edd16368SStephen M. Cameron 			"maximum logical LUNs (%d) exceeded.  "
2892edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_LUN,
2893edd16368SStephen M. Cameron 			*nlogicals - HPSA_MAX_LUN);
2894edd16368SStephen M. Cameron 			*nlogicals = HPSA_MAX_LUN;
2895edd16368SStephen M. Cameron 	}
2896edd16368SStephen M. Cameron 	if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
2897edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
2898edd16368SStephen M. Cameron 			"maximum logical + physical LUNs (%d) exceeded. "
2899edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2900edd16368SStephen M. Cameron 			*nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
2901edd16368SStephen M. Cameron 		*nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
2902edd16368SStephen M. Cameron 	}
2903edd16368SStephen M. Cameron 	return 0;
2904edd16368SStephen M. Cameron }
2905edd16368SStephen M. Cameron 
2906339b2b14SStephen M. Cameron u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i,
2907a93aa1feSMatt Gates 	int nphysicals, int nlogicals,
2908a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list,
2909339b2b14SStephen M. Cameron 	struct ReportLUNdata *logdev_list)
2910339b2b14SStephen M. Cameron {
2911339b2b14SStephen M. Cameron 	/* Helper function, figure out where the LUN ID info is coming from
2912339b2b14SStephen M. Cameron 	 * given index i, lists of physical and logical devices, where in
2913339b2b14SStephen M. Cameron 	 * the list the raid controller is supposed to appear (first or last)
2914339b2b14SStephen M. Cameron 	 */
2915339b2b14SStephen M. Cameron 
2916339b2b14SStephen M. Cameron 	int logicals_start = nphysicals + (raid_ctlr_position == 0);
2917339b2b14SStephen M. Cameron 	int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
2918339b2b14SStephen M. Cameron 
2919339b2b14SStephen M. Cameron 	if (i == raid_ctlr_position)
2920339b2b14SStephen M. Cameron 		return RAID_CTLR_LUNID;
2921339b2b14SStephen M. Cameron 
2922339b2b14SStephen M. Cameron 	if (i < logicals_start)
2923339b2b14SStephen M. Cameron 		return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0];
2924339b2b14SStephen M. Cameron 
2925339b2b14SStephen M. Cameron 	if (i < last_device)
2926339b2b14SStephen M. Cameron 		return &logdev_list->LUN[i - nphysicals -
2927339b2b14SStephen M. Cameron 			(raid_ctlr_position == 0)][0];
2928339b2b14SStephen M. Cameron 	BUG();
2929339b2b14SStephen M. Cameron 	return NULL;
2930339b2b14SStephen M. Cameron }
2931339b2b14SStephen M. Cameron 
2932edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
2933edd16368SStephen M. Cameron {
2934edd16368SStephen M. Cameron 	/* the idea here is we could get notified
2935edd16368SStephen M. Cameron 	 * that some devices have changed, so we do a report
2936edd16368SStephen M. Cameron 	 * physical luns and report logical luns cmd, and adjust
2937edd16368SStephen M. Cameron 	 * our list of devices accordingly.
2938edd16368SStephen M. Cameron 	 *
2939edd16368SStephen M. Cameron 	 * The scsi3addr's of devices won't change so long as the
2940edd16368SStephen M. Cameron 	 * adapter is not reset.  That means we can rescan and
2941edd16368SStephen M. Cameron 	 * tell which devices we already know about, vs. new
2942edd16368SStephen M. Cameron 	 * devices, vs.  disappearing devices.
2943edd16368SStephen M. Cameron 	 */
2944a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list = NULL;
2945edd16368SStephen M. Cameron 	struct ReportLUNdata *logdev_list = NULL;
294601a02ffcSStephen M. Cameron 	u32 nphysicals = 0;
294701a02ffcSStephen M. Cameron 	u32 nlogicals = 0;
2948283b4a9bSStephen M. Cameron 	int physical_mode = 0;
294901a02ffcSStephen M. Cameron 	u32 ndev_allocated = 0;
2950edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
2951edd16368SStephen M. Cameron 	int ncurrent = 0;
2952283b4a9bSStephen M. Cameron 	int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 24;
29534f4eb9f1SScott Teel 	int i, n_ext_target_devs, ndevs_to_allocate;
2954339b2b14SStephen M. Cameron 	int raid_ctlr_position;
2955aca4a520SScott Teel 	DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
2956edd16368SStephen M. Cameron 
2957cfe5badcSScott Teel 	currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
2958edd16368SStephen M. Cameron 	physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
2959edd16368SStephen M. Cameron 	logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
2960edd16368SStephen M. Cameron 	tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
2961edd16368SStephen M. Cameron 
29620b0e1d6cSStephen M. Cameron 	if (!currentsd || !physdev_list || !logdev_list || !tmpdevice) {
2963edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory\n");
2964edd16368SStephen M. Cameron 		goto out;
2965edd16368SStephen M. Cameron 	}
2966edd16368SStephen M. Cameron 	memset(lunzerobits, 0, sizeof(lunzerobits));
2967edd16368SStephen M. Cameron 
2968a93aa1feSMatt Gates 	if (hpsa_gather_lun_info(h, reportlunsize,
2969a93aa1feSMatt Gates 			(struct ReportLUNdata *) physdev_list, &nphysicals,
2970283b4a9bSStephen M. Cameron 			&physical_mode, logdev_list, &nlogicals))
2971edd16368SStephen M. Cameron 		goto out;
2972edd16368SStephen M. Cameron 
2973aca4a520SScott Teel 	/* We might see up to the maximum number of logical and physical disks
2974aca4a520SScott Teel 	 * plus external target devices, and a device for the local RAID
2975aca4a520SScott Teel 	 * controller.
2976edd16368SStephen M. Cameron 	 */
2977aca4a520SScott Teel 	ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
2978edd16368SStephen M. Cameron 
2979edd16368SStephen M. Cameron 	/* Allocate the per device structures */
2980edd16368SStephen M. Cameron 	for (i = 0; i < ndevs_to_allocate; i++) {
2981b7ec021fSScott Teel 		if (i >= HPSA_MAX_DEVICES) {
2982b7ec021fSScott Teel 			dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
2983b7ec021fSScott Teel 				"  %d devices ignored.\n", HPSA_MAX_DEVICES,
2984b7ec021fSScott Teel 				ndevs_to_allocate - HPSA_MAX_DEVICES);
2985b7ec021fSScott Teel 			break;
2986b7ec021fSScott Teel 		}
2987b7ec021fSScott Teel 
2988edd16368SStephen M. Cameron 		currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
2989edd16368SStephen M. Cameron 		if (!currentsd[i]) {
2990edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
2991edd16368SStephen M. Cameron 				__FILE__, __LINE__);
2992edd16368SStephen M. Cameron 			goto out;
2993edd16368SStephen M. Cameron 		}
2994edd16368SStephen M. Cameron 		ndev_allocated++;
2995edd16368SStephen M. Cameron 	}
2996edd16368SStephen M. Cameron 
2997339b2b14SStephen M. Cameron 	if (unlikely(is_scsi_rev_5(h)))
2998339b2b14SStephen M. Cameron 		raid_ctlr_position = 0;
2999339b2b14SStephen M. Cameron 	else
3000339b2b14SStephen M. Cameron 		raid_ctlr_position = nphysicals + nlogicals;
3001339b2b14SStephen M. Cameron 
3002edd16368SStephen M. Cameron 	/* adjust our table of devices */
30034f4eb9f1SScott Teel 	n_ext_target_devs = 0;
3004edd16368SStephen M. Cameron 	for (i = 0; i < nphysicals + nlogicals + 1; i++) {
30050b0e1d6cSStephen M. Cameron 		u8 *lunaddrbytes, is_OBDR = 0;
3006edd16368SStephen M. Cameron 
3007edd16368SStephen M. Cameron 		/* Figure out where the LUN ID info is coming from */
3008339b2b14SStephen M. Cameron 		lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
3009339b2b14SStephen M. Cameron 			i, nphysicals, nlogicals, physdev_list, logdev_list);
3010edd16368SStephen M. Cameron 		/* skip masked physical devices. */
3011339b2b14SStephen M. Cameron 		if (lunaddrbytes[3] & 0xC0 &&
3012339b2b14SStephen M. Cameron 			i < nphysicals + (raid_ctlr_position == 0))
3013edd16368SStephen M. Cameron 			continue;
3014edd16368SStephen M. Cameron 
3015edd16368SStephen M. Cameron 		/* Get device type, vendor, model, device id */
30160b0e1d6cSStephen M. Cameron 		if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
30170b0e1d6cSStephen M. Cameron 							&is_OBDR))
3018edd16368SStephen M. Cameron 			continue; /* skip it if we can't talk to it. */
30191f310bdeSStephen M. Cameron 		figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
3020edd16368SStephen M. Cameron 		this_device = currentsd[ncurrent];
3021edd16368SStephen M. Cameron 
3022edd16368SStephen M. Cameron 		/*
30234f4eb9f1SScott Teel 		 * For external target devices, we have to insert a LUN 0 which
3024edd16368SStephen M. Cameron 		 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
3025edd16368SStephen M. Cameron 		 * is nonetheless an enclosure device there.  We have to
3026edd16368SStephen M. Cameron 		 * present that otherwise linux won't find anything if
3027edd16368SStephen M. Cameron 		 * there is no lun 0.
3028edd16368SStephen M. Cameron 		 */
30294f4eb9f1SScott Teel 		if (add_ext_target_dev(h, tmpdevice, this_device,
30301f310bdeSStephen M. Cameron 				lunaddrbytes, lunzerobits,
30314f4eb9f1SScott Teel 				&n_ext_target_devs)) {
3032edd16368SStephen M. Cameron 			ncurrent++;
3033edd16368SStephen M. Cameron 			this_device = currentsd[ncurrent];
3034edd16368SStephen M. Cameron 		}
3035edd16368SStephen M. Cameron 
3036edd16368SStephen M. Cameron 		*this_device = *tmpdevice;
3037edd16368SStephen M. Cameron 
3038edd16368SStephen M. Cameron 		switch (this_device->devtype) {
30390b0e1d6cSStephen M. Cameron 		case TYPE_ROM:
3040edd16368SStephen M. Cameron 			/* We don't *really* support actual CD-ROM devices,
3041edd16368SStephen M. Cameron 			 * just "One Button Disaster Recovery" tape drive
3042edd16368SStephen M. Cameron 			 * which temporarily pretends to be a CD-ROM drive.
3043edd16368SStephen M. Cameron 			 * So we check that the device is really an OBDR tape
3044edd16368SStephen M. Cameron 			 * device by checking for "$DR-10" in bytes 43-48 of
3045edd16368SStephen M. Cameron 			 * the inquiry data.
3046edd16368SStephen M. Cameron 			 */
30470b0e1d6cSStephen M. Cameron 			if (is_OBDR)
3048edd16368SStephen M. Cameron 				ncurrent++;
3049edd16368SStephen M. Cameron 			break;
3050edd16368SStephen M. Cameron 		case TYPE_DISK:
3051283b4a9bSStephen M. Cameron 			if (i >= nphysicals) {
3052283b4a9bSStephen M. Cameron 				ncurrent++;
3053edd16368SStephen M. Cameron 				break;
3054283b4a9bSStephen M. Cameron 			}
3055283b4a9bSStephen M. Cameron 			if (physical_mode == HPSA_REPORT_PHYS_EXTENDED) {
3056e1f7de0cSMatt Gates 				memcpy(&this_device->ioaccel_handle,
3057e1f7de0cSMatt Gates 					&lunaddrbytes[20],
3058e1f7de0cSMatt Gates 					sizeof(this_device->ioaccel_handle));
3059edd16368SStephen M. Cameron 				ncurrent++;
3060283b4a9bSStephen M. Cameron 			}
3061edd16368SStephen M. Cameron 			break;
3062edd16368SStephen M. Cameron 		case TYPE_TAPE:
3063edd16368SStephen M. Cameron 		case TYPE_MEDIUM_CHANGER:
3064edd16368SStephen M. Cameron 			ncurrent++;
3065edd16368SStephen M. Cameron 			break;
3066edd16368SStephen M. Cameron 		case TYPE_RAID:
3067edd16368SStephen M. Cameron 			/* Only present the Smartarray HBA as a RAID controller.
3068edd16368SStephen M. Cameron 			 * If it's a RAID controller other than the HBA itself
3069edd16368SStephen M. Cameron 			 * (an external RAID controller, MSA500 or similar)
3070edd16368SStephen M. Cameron 			 * don't present it.
3071edd16368SStephen M. Cameron 			 */
3072edd16368SStephen M. Cameron 			if (!is_hba_lunid(lunaddrbytes))
3073edd16368SStephen M. Cameron 				break;
3074edd16368SStephen M. Cameron 			ncurrent++;
3075edd16368SStephen M. Cameron 			break;
3076edd16368SStephen M. Cameron 		default:
3077edd16368SStephen M. Cameron 			break;
3078edd16368SStephen M. Cameron 		}
3079cfe5badcSScott Teel 		if (ncurrent >= HPSA_MAX_DEVICES)
3080edd16368SStephen M. Cameron 			break;
3081edd16368SStephen M. Cameron 	}
3082edd16368SStephen M. Cameron 	adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
3083edd16368SStephen M. Cameron out:
3084edd16368SStephen M. Cameron 	kfree(tmpdevice);
3085edd16368SStephen M. Cameron 	for (i = 0; i < ndev_allocated; i++)
3086edd16368SStephen M. Cameron 		kfree(currentsd[i]);
3087edd16368SStephen M. Cameron 	kfree(currentsd);
3088edd16368SStephen M. Cameron 	kfree(physdev_list);
3089edd16368SStephen M. Cameron 	kfree(logdev_list);
3090edd16368SStephen M. Cameron }
3091edd16368SStephen M. Cameron 
3092edd16368SStephen M. Cameron /* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
3093edd16368SStephen M. Cameron  * dma mapping  and fills in the scatter gather entries of the
3094edd16368SStephen M. Cameron  * hpsa command, cp.
3095edd16368SStephen M. Cameron  */
309633a2ffceSStephen M. Cameron static int hpsa_scatter_gather(struct ctlr_info *h,
3097edd16368SStephen M. Cameron 		struct CommandList *cp,
3098edd16368SStephen M. Cameron 		struct scsi_cmnd *cmd)
3099edd16368SStephen M. Cameron {
3100edd16368SStephen M. Cameron 	unsigned int len;
3101edd16368SStephen M. Cameron 	struct scatterlist *sg;
310201a02ffcSStephen M. Cameron 	u64 addr64;
310333a2ffceSStephen M. Cameron 	int use_sg, i, sg_index, chained;
310433a2ffceSStephen M. Cameron 	struct SGDescriptor *curr_sg;
3105edd16368SStephen M. Cameron 
310633a2ffceSStephen M. Cameron 	BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
3107edd16368SStephen M. Cameron 
3108edd16368SStephen M. Cameron 	use_sg = scsi_dma_map(cmd);
3109edd16368SStephen M. Cameron 	if (use_sg < 0)
3110edd16368SStephen M. Cameron 		return use_sg;
3111edd16368SStephen M. Cameron 
3112edd16368SStephen M. Cameron 	if (!use_sg)
3113edd16368SStephen M. Cameron 		goto sglist_finished;
3114edd16368SStephen M. Cameron 
311533a2ffceSStephen M. Cameron 	curr_sg = cp->SG;
311633a2ffceSStephen M. Cameron 	chained = 0;
311733a2ffceSStephen M. Cameron 	sg_index = 0;
3118edd16368SStephen M. Cameron 	scsi_for_each_sg(cmd, sg, use_sg, i) {
311933a2ffceSStephen M. Cameron 		if (i == h->max_cmd_sg_entries - 1 &&
312033a2ffceSStephen M. Cameron 			use_sg > h->max_cmd_sg_entries) {
312133a2ffceSStephen M. Cameron 			chained = 1;
312233a2ffceSStephen M. Cameron 			curr_sg = h->cmd_sg_list[cp->cmdindex];
312333a2ffceSStephen M. Cameron 			sg_index = 0;
312433a2ffceSStephen M. Cameron 		}
312501a02ffcSStephen M. Cameron 		addr64 = (u64) sg_dma_address(sg);
3126edd16368SStephen M. Cameron 		len  = sg_dma_len(sg);
312733a2ffceSStephen M. Cameron 		curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
312833a2ffceSStephen M. Cameron 		curr_sg->Addr.upper = (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
312933a2ffceSStephen M. Cameron 		curr_sg->Len = len;
3130e1d9cbfaSMatt Gates 		curr_sg->Ext = (i < scsi_sg_count(cmd) - 1) ? 0 : HPSA_SG_LAST;
313133a2ffceSStephen M. Cameron 		curr_sg++;
313233a2ffceSStephen M. Cameron 	}
313333a2ffceSStephen M. Cameron 
313433a2ffceSStephen M. Cameron 	if (use_sg + chained > h->maxSG)
313533a2ffceSStephen M. Cameron 		h->maxSG = use_sg + chained;
313633a2ffceSStephen M. Cameron 
313733a2ffceSStephen M. Cameron 	if (chained) {
313833a2ffceSStephen M. Cameron 		cp->Header.SGList = h->max_cmd_sg_entries;
313933a2ffceSStephen M. Cameron 		cp->Header.SGTotal = (u16) (use_sg + 1);
3140e2bea6dfSStephen M. Cameron 		if (hpsa_map_sg_chain_block(h, cp)) {
3141e2bea6dfSStephen M. Cameron 			scsi_dma_unmap(cmd);
3142e2bea6dfSStephen M. Cameron 			return -1;
3143e2bea6dfSStephen M. Cameron 		}
314433a2ffceSStephen M. Cameron 		return 0;
3145edd16368SStephen M. Cameron 	}
3146edd16368SStephen M. Cameron 
3147edd16368SStephen M. Cameron sglist_finished:
3148edd16368SStephen M. Cameron 
314901a02ffcSStephen M. Cameron 	cp->Header.SGList = (u8) use_sg;   /* no. SGs contig in this cmd */
315001a02ffcSStephen M. Cameron 	cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */
3151edd16368SStephen M. Cameron 	return 0;
3152edd16368SStephen M. Cameron }
3153edd16368SStephen M. Cameron 
3154283b4a9bSStephen M. Cameron #define IO_ACCEL_INELIGIBLE (1)
3155283b4a9bSStephen M. Cameron static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
3156283b4a9bSStephen M. Cameron {
3157283b4a9bSStephen M. Cameron 	int is_write = 0;
3158283b4a9bSStephen M. Cameron 	u32 block;
3159283b4a9bSStephen M. Cameron 	u32 block_cnt;
3160283b4a9bSStephen M. Cameron 
3161283b4a9bSStephen M. Cameron 	/* Perform some CDB fixups if needed using 10 byte reads/writes only */
3162283b4a9bSStephen M. Cameron 	switch (cdb[0]) {
3163283b4a9bSStephen M. Cameron 	case WRITE_6:
3164283b4a9bSStephen M. Cameron 	case WRITE_12:
3165283b4a9bSStephen M. Cameron 		is_write = 1;
3166283b4a9bSStephen M. Cameron 	case READ_6:
3167283b4a9bSStephen M. Cameron 	case READ_12:
3168283b4a9bSStephen M. Cameron 		if (*cdb_len == 6) {
3169283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 8) | cdb[3];
3170283b4a9bSStephen M. Cameron 			block_cnt = cdb[4];
3171283b4a9bSStephen M. Cameron 		} else {
3172283b4a9bSStephen M. Cameron 			BUG_ON(*cdb_len != 12);
3173283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 24) |
3174283b4a9bSStephen M. Cameron 				(((u32) cdb[3]) << 16) |
3175283b4a9bSStephen M. Cameron 				(((u32) cdb[4]) << 8) |
3176283b4a9bSStephen M. Cameron 				cdb[5];
3177283b4a9bSStephen M. Cameron 			block_cnt =
3178283b4a9bSStephen M. Cameron 				(((u32) cdb[6]) << 24) |
3179283b4a9bSStephen M. Cameron 				(((u32) cdb[7]) << 16) |
3180283b4a9bSStephen M. Cameron 				(((u32) cdb[8]) << 8) |
3181283b4a9bSStephen M. Cameron 				cdb[9];
3182283b4a9bSStephen M. Cameron 		}
3183283b4a9bSStephen M. Cameron 		if (block_cnt > 0xffff)
3184283b4a9bSStephen M. Cameron 			return IO_ACCEL_INELIGIBLE;
3185283b4a9bSStephen M. Cameron 
3186283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
3187283b4a9bSStephen M. Cameron 		cdb[1] = 0;
3188283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (block >> 24);
3189283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (block >> 16);
3190283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (block >> 8);
3191283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (block);
3192283b4a9bSStephen M. Cameron 		cdb[6] = 0;
3193283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (block_cnt >> 8);
3194283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (block_cnt);
3195283b4a9bSStephen M. Cameron 		cdb[9] = 0;
3196283b4a9bSStephen M. Cameron 		*cdb_len = 10;
3197283b4a9bSStephen M. Cameron 		break;
3198283b4a9bSStephen M. Cameron 	}
3199283b4a9bSStephen M. Cameron 	return 0;
3200283b4a9bSStephen M. Cameron }
3201283b4a9bSStephen M. Cameron 
3202c349775eSScott Teel static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
3203283b4a9bSStephen M. Cameron 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
3204283b4a9bSStephen M. Cameron 	u8 *scsi3addr)
3205e1f7de0cSMatt Gates {
3206e1f7de0cSMatt Gates 	struct scsi_cmnd *cmd = c->scsi_cmd;
3207e1f7de0cSMatt Gates 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
3208e1f7de0cSMatt Gates 	unsigned int len;
3209e1f7de0cSMatt Gates 	unsigned int total_len = 0;
3210e1f7de0cSMatt Gates 	struct scatterlist *sg;
3211e1f7de0cSMatt Gates 	u64 addr64;
3212e1f7de0cSMatt Gates 	int use_sg, i;
3213e1f7de0cSMatt Gates 	struct SGDescriptor *curr_sg;
3214e1f7de0cSMatt Gates 	u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
3215e1f7de0cSMatt Gates 
3216283b4a9bSStephen M. Cameron 	/* TODO: implement chaining support */
3217283b4a9bSStephen M. Cameron 	if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
3218283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3219283b4a9bSStephen M. Cameron 
3220e1f7de0cSMatt Gates 	BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
3221e1f7de0cSMatt Gates 
3222283b4a9bSStephen M. Cameron 	if (fixup_ioaccel_cdb(cdb, &cdb_len))
3223283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3224283b4a9bSStephen M. Cameron 
3225e1f7de0cSMatt Gates 	c->cmd_type = CMD_IOACCEL1;
3226e1f7de0cSMatt Gates 
3227e1f7de0cSMatt Gates 	/* Adjust the DMA address to point to the accelerated command buffer */
3228e1f7de0cSMatt Gates 	c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
3229e1f7de0cSMatt Gates 				(c->cmdindex * sizeof(*cp));
3230e1f7de0cSMatt Gates 	BUG_ON(c->busaddr & 0x0000007F);
3231e1f7de0cSMatt Gates 
3232e1f7de0cSMatt Gates 	use_sg = scsi_dma_map(cmd);
3233e1f7de0cSMatt Gates 	if (use_sg < 0)
3234e1f7de0cSMatt Gates 		return use_sg;
3235e1f7de0cSMatt Gates 
3236e1f7de0cSMatt Gates 	if (use_sg) {
3237e1f7de0cSMatt Gates 		curr_sg = cp->SG;
3238e1f7de0cSMatt Gates 		scsi_for_each_sg(cmd, sg, use_sg, i) {
3239e1f7de0cSMatt Gates 			addr64 = (u64) sg_dma_address(sg);
3240e1f7de0cSMatt Gates 			len  = sg_dma_len(sg);
3241e1f7de0cSMatt Gates 			total_len += len;
3242e1f7de0cSMatt Gates 			curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
3243e1f7de0cSMatt Gates 			curr_sg->Addr.upper =
3244e1f7de0cSMatt Gates 				(u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
3245e1f7de0cSMatt Gates 			curr_sg->Len = len;
3246e1f7de0cSMatt Gates 
3247e1f7de0cSMatt Gates 			if (i == (scsi_sg_count(cmd) - 1))
3248e1f7de0cSMatt Gates 				curr_sg->Ext = HPSA_SG_LAST;
3249e1f7de0cSMatt Gates 			else
3250e1f7de0cSMatt Gates 				curr_sg->Ext = 0;  /* we are not chaining */
3251e1f7de0cSMatt Gates 			curr_sg++;
3252e1f7de0cSMatt Gates 		}
3253e1f7de0cSMatt Gates 
3254e1f7de0cSMatt Gates 		switch (cmd->sc_data_direction) {
3255e1f7de0cSMatt Gates 		case DMA_TO_DEVICE:
3256e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_OUT;
3257e1f7de0cSMatt Gates 			break;
3258e1f7de0cSMatt Gates 		case DMA_FROM_DEVICE:
3259e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_IN;
3260e1f7de0cSMatt Gates 			break;
3261e1f7de0cSMatt Gates 		case DMA_NONE:
3262e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_NODATAXFER;
3263e1f7de0cSMatt Gates 			break;
3264e1f7de0cSMatt Gates 		default:
3265e1f7de0cSMatt Gates 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3266e1f7de0cSMatt Gates 			cmd->sc_data_direction);
3267e1f7de0cSMatt Gates 			BUG();
3268e1f7de0cSMatt Gates 			break;
3269e1f7de0cSMatt Gates 		}
3270e1f7de0cSMatt Gates 	} else {
3271e1f7de0cSMatt Gates 		control |= IOACCEL1_CONTROL_NODATAXFER;
3272e1f7de0cSMatt Gates 	}
3273e1f7de0cSMatt Gates 
3274c349775eSScott Teel 	c->Header.SGList = use_sg;
3275e1f7de0cSMatt Gates 	/* Fill out the command structure to submit */
3276283b4a9bSStephen M. Cameron 	cp->dev_handle = ioaccel_handle & 0xFFFF;
3277e1f7de0cSMatt Gates 	cp->transfer_len = total_len;
3278e1f7de0cSMatt Gates 	cp->io_flags = IOACCEL1_IOFLAGS_IO_REQ |
3279283b4a9bSStephen M. Cameron 			(cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK);
3280e1f7de0cSMatt Gates 	cp->control = control;
3281283b4a9bSStephen M. Cameron 	memcpy(cp->CDB, cdb, cdb_len);
3282283b4a9bSStephen M. Cameron 	memcpy(cp->CISS_LUN, scsi3addr, 8);
3283c349775eSScott Teel 	/* Tag was already set at init time. */
3284e1f7de0cSMatt Gates 	enqueue_cmd_and_start_io(h, c);
3285e1f7de0cSMatt Gates 	return 0;
3286e1f7de0cSMatt Gates }
3287edd16368SStephen M. Cameron 
3288283b4a9bSStephen M. Cameron /*
3289283b4a9bSStephen M. Cameron  * Queue a command directly to a device behind the controller using the
3290283b4a9bSStephen M. Cameron  * I/O accelerator path.
3291283b4a9bSStephen M. Cameron  */
3292283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
3293283b4a9bSStephen M. Cameron 	struct CommandList *c)
3294283b4a9bSStephen M. Cameron {
3295283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
3296283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3297283b4a9bSStephen M. Cameron 
3298283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
3299283b4a9bSStephen M. Cameron 		cmd->cmnd, cmd->cmd_len, dev->scsi3addr);
3300283b4a9bSStephen M. Cameron }
3301283b4a9bSStephen M. Cameron 
3302dd0e19f3SScott Teel /*
3303dd0e19f3SScott Teel  * Set encryption parameters for the ioaccel2 request
3304dd0e19f3SScott Teel  */
3305dd0e19f3SScott Teel static void set_encrypt_ioaccel2(struct ctlr_info *h,
3306dd0e19f3SScott Teel 	struct CommandList *c, struct io_accel2_cmd *cp)
3307dd0e19f3SScott Teel {
3308dd0e19f3SScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
3309dd0e19f3SScott Teel 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3310dd0e19f3SScott Teel 	struct raid_map_data *map = &dev->raid_map;
3311dd0e19f3SScott Teel 	u64 first_block;
3312dd0e19f3SScott Teel 
3313dd0e19f3SScott Teel 	BUG_ON(!(dev->offload_config && dev->offload_enabled));
3314dd0e19f3SScott Teel 
3315dd0e19f3SScott Teel 	/* Are we doing encryption on this device */
3316dd0e19f3SScott Teel 	if (!(map->flags & RAID_MAP_FLAG_ENCRYPT_ON))
3317dd0e19f3SScott Teel 		return;
3318dd0e19f3SScott Teel 	/* Set the data encryption key index. */
3319dd0e19f3SScott Teel 	cp->dekindex = map->dekindex;
3320dd0e19f3SScott Teel 
3321dd0e19f3SScott Teel 	/* Set the encryption enable flag, encoded into direction field. */
3322dd0e19f3SScott Teel 	cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
3323dd0e19f3SScott Teel 
3324dd0e19f3SScott Teel 	/* Set encryption tweak values based on logical block address
3325dd0e19f3SScott Teel 	 * If block size is 512, tweak value is LBA.
3326dd0e19f3SScott Teel 	 * For other block sizes, tweak is (LBA * block size)/ 512)
3327dd0e19f3SScott Teel 	 */
3328dd0e19f3SScott Teel 	switch (cmd->cmnd[0]) {
3329dd0e19f3SScott Teel 	/* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
3330dd0e19f3SScott Teel 	case WRITE_6:
3331dd0e19f3SScott Teel 	case READ_6:
3332dd0e19f3SScott Teel 		if (map->volume_blk_size == 512) {
3333dd0e19f3SScott Teel 			cp->tweak_lower =
3334dd0e19f3SScott Teel 				(((u32) cmd->cmnd[2]) << 8) |
3335dd0e19f3SScott Teel 					cmd->cmnd[3];
3336dd0e19f3SScott Teel 			cp->tweak_upper = 0;
3337dd0e19f3SScott Teel 		} else {
3338dd0e19f3SScott Teel 			first_block =
3339dd0e19f3SScott Teel 				(((u64) cmd->cmnd[2]) << 8) |
3340dd0e19f3SScott Teel 					cmd->cmnd[3];
3341dd0e19f3SScott Teel 			first_block = (first_block * map->volume_blk_size)/512;
3342dd0e19f3SScott Teel 			cp->tweak_lower = (u32)first_block;
3343dd0e19f3SScott Teel 			cp->tweak_upper = (u32)(first_block >> 32);
3344dd0e19f3SScott Teel 		}
3345dd0e19f3SScott Teel 		break;
3346dd0e19f3SScott Teel 	case WRITE_10:
3347dd0e19f3SScott Teel 	case READ_10:
3348dd0e19f3SScott Teel 		if (map->volume_blk_size == 512) {
3349dd0e19f3SScott Teel 			cp->tweak_lower =
3350dd0e19f3SScott Teel 				(((u32) cmd->cmnd[2]) << 24) |
3351dd0e19f3SScott Teel 				(((u32) cmd->cmnd[3]) << 16) |
3352dd0e19f3SScott Teel 				(((u32) cmd->cmnd[4]) << 8) |
3353dd0e19f3SScott Teel 					cmd->cmnd[5];
3354dd0e19f3SScott Teel 			cp->tweak_upper = 0;
3355dd0e19f3SScott Teel 		} else {
3356dd0e19f3SScott Teel 			first_block =
3357dd0e19f3SScott Teel 				(((u64) cmd->cmnd[2]) << 24) |
3358dd0e19f3SScott Teel 				(((u64) cmd->cmnd[3]) << 16) |
3359dd0e19f3SScott Teel 				(((u64) cmd->cmnd[4]) << 8) |
3360dd0e19f3SScott Teel 					cmd->cmnd[5];
3361dd0e19f3SScott Teel 			first_block = (first_block * map->volume_blk_size)/512;
3362dd0e19f3SScott Teel 			cp->tweak_lower = (u32)first_block;
3363dd0e19f3SScott Teel 			cp->tweak_upper = (u32)(first_block >> 32);
3364dd0e19f3SScott Teel 		}
3365dd0e19f3SScott Teel 		break;
3366dd0e19f3SScott Teel 	/* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
3367dd0e19f3SScott Teel 	case WRITE_12:
3368dd0e19f3SScott Teel 	case READ_12:
3369dd0e19f3SScott Teel 		if (map->volume_blk_size == 512) {
3370dd0e19f3SScott Teel 			cp->tweak_lower =
3371dd0e19f3SScott Teel 				(((u32) cmd->cmnd[2]) << 24) |
3372dd0e19f3SScott Teel 				(((u32) cmd->cmnd[3]) << 16) |
3373dd0e19f3SScott Teel 				(((u32) cmd->cmnd[4]) << 8) |
3374dd0e19f3SScott Teel 					cmd->cmnd[5];
3375dd0e19f3SScott Teel 			cp->tweak_upper = 0;
3376dd0e19f3SScott Teel 		} else {
3377dd0e19f3SScott Teel 			first_block =
3378dd0e19f3SScott Teel 				(((u64) cmd->cmnd[2]) << 24) |
3379dd0e19f3SScott Teel 				(((u64) cmd->cmnd[3]) << 16) |
3380dd0e19f3SScott Teel 				(((u64) cmd->cmnd[4]) << 8) |
3381dd0e19f3SScott Teel 					cmd->cmnd[5];
3382dd0e19f3SScott Teel 			first_block = (first_block * map->volume_blk_size)/512;
3383dd0e19f3SScott Teel 			cp->tweak_lower = (u32)first_block;
3384dd0e19f3SScott Teel 			cp->tweak_upper = (u32)(first_block >> 32);
3385dd0e19f3SScott Teel 		}
3386dd0e19f3SScott Teel 		break;
3387dd0e19f3SScott Teel 	case WRITE_16:
3388dd0e19f3SScott Teel 	case READ_16:
3389dd0e19f3SScott Teel 		if (map->volume_blk_size == 512) {
3390dd0e19f3SScott Teel 			cp->tweak_lower =
3391dd0e19f3SScott Teel 				(((u32) cmd->cmnd[6]) << 24) |
3392dd0e19f3SScott Teel 				(((u32) cmd->cmnd[7]) << 16) |
3393dd0e19f3SScott Teel 				(((u32) cmd->cmnd[8]) << 8) |
3394dd0e19f3SScott Teel 					cmd->cmnd[9];
3395dd0e19f3SScott Teel 			cp->tweak_upper =
3396dd0e19f3SScott Teel 				(((u32) cmd->cmnd[2]) << 24) |
3397dd0e19f3SScott Teel 				(((u32) cmd->cmnd[3]) << 16) |
3398dd0e19f3SScott Teel 				(((u32) cmd->cmnd[4]) << 8) |
3399dd0e19f3SScott Teel 					cmd->cmnd[5];
3400dd0e19f3SScott Teel 		} else {
3401dd0e19f3SScott Teel 			first_block =
3402dd0e19f3SScott Teel 				(((u64) cmd->cmnd[2]) << 56) |
3403dd0e19f3SScott Teel 				(((u64) cmd->cmnd[3]) << 48) |
3404dd0e19f3SScott Teel 				(((u64) cmd->cmnd[4]) << 40) |
3405dd0e19f3SScott Teel 				(((u64) cmd->cmnd[5]) << 32) |
3406dd0e19f3SScott Teel 				(((u64) cmd->cmnd[6]) << 24) |
3407dd0e19f3SScott Teel 				(((u64) cmd->cmnd[7]) << 16) |
3408dd0e19f3SScott Teel 				(((u64) cmd->cmnd[8]) << 8) |
3409dd0e19f3SScott Teel 					cmd->cmnd[9];
3410dd0e19f3SScott Teel 			first_block = (first_block * map->volume_blk_size)/512;
3411dd0e19f3SScott Teel 			cp->tweak_lower = (u32)first_block;
3412dd0e19f3SScott Teel 			cp->tweak_upper = (u32)(first_block >> 32);
3413dd0e19f3SScott Teel 		}
3414dd0e19f3SScott Teel 		break;
3415dd0e19f3SScott Teel 	default:
3416dd0e19f3SScott Teel 		dev_err(&h->pdev->dev,
3417dd0e19f3SScott Teel 			"ERROR: %s: IOACCEL request CDB size not supported for encryption\n",
3418dd0e19f3SScott Teel 			__func__);
3419dd0e19f3SScott Teel 		BUG();
3420dd0e19f3SScott Teel 		break;
3421dd0e19f3SScott Teel 	}
3422dd0e19f3SScott Teel }
3423dd0e19f3SScott Teel 
3424c349775eSScott Teel static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
3425c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
3426c349775eSScott Teel 	u8 *scsi3addr)
3427c349775eSScott Teel {
3428c349775eSScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
3429c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
3430c349775eSScott Teel 	struct ioaccel2_sg_element *curr_sg;
3431c349775eSScott Teel 	int use_sg, i;
3432c349775eSScott Teel 	struct scatterlist *sg;
3433c349775eSScott Teel 	u64 addr64;
3434c349775eSScott Teel 	u32 len;
3435c349775eSScott Teel 	u32 total_len = 0;
3436c349775eSScott Teel 
3437c349775eSScott Teel 	if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
3438c349775eSScott Teel 		return IO_ACCEL_INELIGIBLE;
3439c349775eSScott Teel 
3440c349775eSScott Teel 	if (fixup_ioaccel_cdb(cdb, &cdb_len))
3441c349775eSScott Teel 		return IO_ACCEL_INELIGIBLE;
3442c349775eSScott Teel 	c->cmd_type = CMD_IOACCEL2;
3443c349775eSScott Teel 	/* Adjust the DMA address to point to the accelerated command buffer */
3444c349775eSScott Teel 	c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
3445c349775eSScott Teel 				(c->cmdindex * sizeof(*cp));
3446c349775eSScott Teel 	BUG_ON(c->busaddr & 0x0000007F);
3447c349775eSScott Teel 
3448c349775eSScott Teel 	memset(cp, 0, sizeof(*cp));
3449c349775eSScott Teel 	cp->IU_type = IOACCEL2_IU_TYPE;
3450c349775eSScott Teel 
3451c349775eSScott Teel 	use_sg = scsi_dma_map(cmd);
3452c349775eSScott Teel 	if (use_sg < 0)
3453c349775eSScott Teel 		return use_sg;
3454c349775eSScott Teel 
3455c349775eSScott Teel 	if (use_sg) {
3456c349775eSScott Teel 		BUG_ON(use_sg > IOACCEL2_MAXSGENTRIES);
3457c349775eSScott Teel 		curr_sg = cp->sg;
3458c349775eSScott Teel 		scsi_for_each_sg(cmd, sg, use_sg, i) {
3459c349775eSScott Teel 			addr64 = (u64) sg_dma_address(sg);
3460c349775eSScott Teel 			len  = sg_dma_len(sg);
3461c349775eSScott Teel 			total_len += len;
3462c349775eSScott Teel 			curr_sg->address = cpu_to_le64(addr64);
3463c349775eSScott Teel 			curr_sg->length = cpu_to_le32(len);
3464c349775eSScott Teel 			curr_sg->reserved[0] = 0;
3465c349775eSScott Teel 			curr_sg->reserved[1] = 0;
3466c349775eSScott Teel 			curr_sg->reserved[2] = 0;
3467c349775eSScott Teel 			curr_sg->chain_indicator = 0;
3468c349775eSScott Teel 			curr_sg++;
3469c349775eSScott Teel 		}
3470c349775eSScott Teel 
3471c349775eSScott Teel 		switch (cmd->sc_data_direction) {
3472c349775eSScott Teel 		case DMA_TO_DEVICE:
3473dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3474dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_OUT;
3475c349775eSScott Teel 			break;
3476c349775eSScott Teel 		case DMA_FROM_DEVICE:
3477dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3478dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_IN;
3479c349775eSScott Teel 			break;
3480c349775eSScott Teel 		case DMA_NONE:
3481dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3482dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_NO_DATA;
3483c349775eSScott Teel 			break;
3484c349775eSScott Teel 		default:
3485c349775eSScott Teel 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3486c349775eSScott Teel 				cmd->sc_data_direction);
3487c349775eSScott Teel 			BUG();
3488c349775eSScott Teel 			break;
3489c349775eSScott Teel 		}
3490c349775eSScott Teel 	} else {
3491dd0e19f3SScott Teel 		cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3492dd0e19f3SScott Teel 		cp->direction |= IOACCEL2_DIR_NO_DATA;
3493c349775eSScott Teel 	}
3494dd0e19f3SScott Teel 
3495dd0e19f3SScott Teel 	/* Set encryption parameters, if necessary */
3496dd0e19f3SScott Teel 	set_encrypt_ioaccel2(h, c, cp);
3497dd0e19f3SScott Teel 
3498c349775eSScott Teel 	cp->scsi_nexus = ioaccel_handle;
3499dd0e19f3SScott Teel 	cp->Tag = (c->cmdindex << DIRECT_LOOKUP_SHIFT) |
3500c349775eSScott Teel 				DIRECT_LOOKUP_BIT;
3501c349775eSScott Teel 	memcpy(cp->cdb, cdb, sizeof(cp->cdb));
3502c349775eSScott Teel 	memset(cp->cciss_lun, 0, sizeof(cp->cciss_lun));
3503c349775eSScott Teel 	cp->cmd_priority_task_attr = 0;
3504c349775eSScott Teel 
3505c349775eSScott Teel 	/* fill in sg elements */
3506c349775eSScott Teel 	cp->sg_count = (u8) use_sg;
3507c349775eSScott Teel 
3508c349775eSScott Teel 	cp->data_len = cpu_to_le32(total_len);
3509c349775eSScott Teel 	cp->err_ptr = cpu_to_le64(c->busaddr +
3510c349775eSScott Teel 			offsetof(struct io_accel2_cmd, error_data));
3511c349775eSScott Teel 	cp->err_len = cpu_to_le32((u32) sizeof(cp->error_data));
3512c349775eSScott Teel 
3513c349775eSScott Teel 	enqueue_cmd_and_start_io(h, c);
3514c349775eSScott Teel 	return 0;
3515c349775eSScott Teel }
3516c349775eSScott Teel 
3517c349775eSScott Teel /*
3518c349775eSScott Teel  * Queue a command to the correct I/O accelerator path.
3519c349775eSScott Teel  */
3520c349775eSScott Teel static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
3521c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
3522c349775eSScott Teel 	u8 *scsi3addr)
3523c349775eSScott Teel {
3524c349775eSScott Teel 	if (h->transMethod & CFGTBL_Trans_io_accel1)
3525c349775eSScott Teel 		return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
3526c349775eSScott Teel 						cdb, cdb_len, scsi3addr);
3527c349775eSScott Teel 	else
3528c349775eSScott Teel 		return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
3529c349775eSScott Teel 						cdb, cdb_len, scsi3addr);
3530c349775eSScott Teel }
3531c349775eSScott Teel 
35326b80b18fSScott Teel static void raid_map_helper(struct raid_map_data *map,
35336b80b18fSScott Teel 		int offload_to_mirror, u32 *map_index, u32 *current_group)
35346b80b18fSScott Teel {
35356b80b18fSScott Teel 	if (offload_to_mirror == 0)  {
35366b80b18fSScott Teel 		/* use physical disk in the first mirrored group. */
35376b80b18fSScott Teel 		*map_index %= map->data_disks_per_row;
35386b80b18fSScott Teel 		return;
35396b80b18fSScott Teel 	}
35406b80b18fSScott Teel 	do {
35416b80b18fSScott Teel 		/* determine mirror group that *map_index indicates */
35426b80b18fSScott Teel 		*current_group = *map_index / map->data_disks_per_row;
35436b80b18fSScott Teel 		if (offload_to_mirror == *current_group)
35446b80b18fSScott Teel 			continue;
35456b80b18fSScott Teel 		if (*current_group < (map->layout_map_count - 1)) {
35466b80b18fSScott Teel 			/* select map index from next group */
35476b80b18fSScott Teel 			*map_index += map->data_disks_per_row;
35486b80b18fSScott Teel 			(*current_group)++;
35496b80b18fSScott Teel 		} else {
35506b80b18fSScott Teel 			/* select map index from first group */
35516b80b18fSScott Teel 			*map_index %= map->data_disks_per_row;
35526b80b18fSScott Teel 			*current_group = 0;
35536b80b18fSScott Teel 		}
35546b80b18fSScott Teel 	} while (offload_to_mirror != *current_group);
35556b80b18fSScott Teel }
35566b80b18fSScott Teel 
3557283b4a9bSStephen M. Cameron /*
3558283b4a9bSStephen M. Cameron  * Attempt to perform offload RAID mapping for a logical volume I/O.
3559283b4a9bSStephen M. Cameron  */
3560283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
3561283b4a9bSStephen M. Cameron 	struct CommandList *c)
3562283b4a9bSStephen M. Cameron {
3563283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
3564283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3565283b4a9bSStephen M. Cameron 	struct raid_map_data *map = &dev->raid_map;
3566283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map->data[0];
3567283b4a9bSStephen M. Cameron 	int is_write = 0;
3568283b4a9bSStephen M. Cameron 	u32 map_index;
3569283b4a9bSStephen M. Cameron 	u64 first_block, last_block;
3570283b4a9bSStephen M. Cameron 	u32 block_cnt;
3571283b4a9bSStephen M. Cameron 	u32 blocks_per_row;
3572283b4a9bSStephen M. Cameron 	u64 first_row, last_row;
3573283b4a9bSStephen M. Cameron 	u32 first_row_offset, last_row_offset;
3574283b4a9bSStephen M. Cameron 	u32 first_column, last_column;
35756b80b18fSScott Teel 	u64 r0_first_row, r0_last_row;
35766b80b18fSScott Teel 	u32 r5or6_blocks_per_row;
35776b80b18fSScott Teel 	u64 r5or6_first_row, r5or6_last_row;
35786b80b18fSScott Teel 	u32 r5or6_first_row_offset, r5or6_last_row_offset;
35796b80b18fSScott Teel 	u32 r5or6_first_column, r5or6_last_column;
35806b80b18fSScott Teel 	u32 total_disks_per_row;
35816b80b18fSScott Teel 	u32 stripesize;
35826b80b18fSScott Teel 	u32 first_group, last_group, current_group;
3583283b4a9bSStephen M. Cameron 	u32 map_row;
3584283b4a9bSStephen M. Cameron 	u32 disk_handle;
3585283b4a9bSStephen M. Cameron 	u64 disk_block;
3586283b4a9bSStephen M. Cameron 	u32 disk_block_cnt;
3587283b4a9bSStephen M. Cameron 	u8 cdb[16];
3588283b4a9bSStephen M. Cameron 	u8 cdb_len;
3589283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
3590283b4a9bSStephen M. Cameron 	u64 tmpdiv;
3591283b4a9bSStephen M. Cameron #endif
35926b80b18fSScott Teel 	int offload_to_mirror;
3593283b4a9bSStephen M. Cameron 
3594283b4a9bSStephen M. Cameron 	BUG_ON(!(dev->offload_config && dev->offload_enabled));
3595283b4a9bSStephen M. Cameron 
3596283b4a9bSStephen M. Cameron 	/* check for valid opcode, get LBA and block count */
3597283b4a9bSStephen M. Cameron 	switch (cmd->cmnd[0]) {
3598283b4a9bSStephen M. Cameron 	case WRITE_6:
3599283b4a9bSStephen M. Cameron 		is_write = 1;
3600283b4a9bSStephen M. Cameron 	case READ_6:
3601283b4a9bSStephen M. Cameron 		first_block =
3602283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 8) |
3603283b4a9bSStephen M. Cameron 			cmd->cmnd[3];
3604283b4a9bSStephen M. Cameron 		block_cnt = cmd->cmnd[4];
3605283b4a9bSStephen M. Cameron 		break;
3606283b4a9bSStephen M. Cameron 	case WRITE_10:
3607283b4a9bSStephen M. Cameron 		is_write = 1;
3608283b4a9bSStephen M. Cameron 	case READ_10:
3609283b4a9bSStephen M. Cameron 		first_block =
3610283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
3611283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
3612283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
3613283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
3614283b4a9bSStephen M. Cameron 		block_cnt =
3615283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 8) |
3616283b4a9bSStephen M. Cameron 			cmd->cmnd[8];
3617283b4a9bSStephen M. Cameron 		break;
3618283b4a9bSStephen M. Cameron 	case WRITE_12:
3619283b4a9bSStephen M. Cameron 		is_write = 1;
3620283b4a9bSStephen M. Cameron 	case READ_12:
3621283b4a9bSStephen M. Cameron 		first_block =
3622283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
3623283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
3624283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
3625283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
3626283b4a9bSStephen M. Cameron 		block_cnt =
3627283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[6]) << 24) |
3628283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 16) |
3629283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[8]) << 8) |
3630283b4a9bSStephen M. Cameron 		cmd->cmnd[9];
3631283b4a9bSStephen M. Cameron 		break;
3632283b4a9bSStephen M. Cameron 	case WRITE_16:
3633283b4a9bSStephen M. Cameron 		is_write = 1;
3634283b4a9bSStephen M. Cameron 	case READ_16:
3635283b4a9bSStephen M. Cameron 		first_block =
3636283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 56) |
3637283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 48) |
3638283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 40) |
3639283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[5]) << 32) |
3640283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[6]) << 24) |
3641283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[7]) << 16) |
3642283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[8]) << 8) |
3643283b4a9bSStephen M. Cameron 			cmd->cmnd[9];
3644283b4a9bSStephen M. Cameron 		block_cnt =
3645283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[10]) << 24) |
3646283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[11]) << 16) |
3647283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[12]) << 8) |
3648283b4a9bSStephen M. Cameron 			cmd->cmnd[13];
3649283b4a9bSStephen M. Cameron 		break;
3650283b4a9bSStephen M. Cameron 	default:
3651283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
3652283b4a9bSStephen M. Cameron 	}
3653283b4a9bSStephen M. Cameron 	BUG_ON(block_cnt == 0);
3654283b4a9bSStephen M. Cameron 	last_block = first_block + block_cnt - 1;
3655283b4a9bSStephen M. Cameron 
3656283b4a9bSStephen M. Cameron 	/* check for write to non-RAID-0 */
3657283b4a9bSStephen M. Cameron 	if (is_write && dev->raid_level != 0)
3658283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3659283b4a9bSStephen M. Cameron 
3660283b4a9bSStephen M. Cameron 	/* check for invalid block or wraparound */
3661283b4a9bSStephen M. Cameron 	if (last_block >= map->volume_blk_cnt || last_block < first_block)
3662283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3663283b4a9bSStephen M. Cameron 
3664283b4a9bSStephen M. Cameron 	/* calculate stripe information for the request */
3665283b4a9bSStephen M. Cameron 	blocks_per_row = map->data_disks_per_row * map->strip_size;
3666283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
3667283b4a9bSStephen M. Cameron 	tmpdiv = first_block;
3668283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
3669283b4a9bSStephen M. Cameron 	first_row = tmpdiv;
3670283b4a9bSStephen M. Cameron 	tmpdiv = last_block;
3671283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
3672283b4a9bSStephen M. Cameron 	last_row = tmpdiv;
3673283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
3674283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
3675283b4a9bSStephen M. Cameron 	tmpdiv = first_row_offset;
3676283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv,  map->strip_size);
3677283b4a9bSStephen M. Cameron 	first_column = tmpdiv;
3678283b4a9bSStephen M. Cameron 	tmpdiv = last_row_offset;
3679283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, map->strip_size);
3680283b4a9bSStephen M. Cameron 	last_column = tmpdiv;
3681283b4a9bSStephen M. Cameron #else
3682283b4a9bSStephen M. Cameron 	first_row = first_block / blocks_per_row;
3683283b4a9bSStephen M. Cameron 	last_row = last_block / blocks_per_row;
3684283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
3685283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
3686283b4a9bSStephen M. Cameron 	first_column = first_row_offset / map->strip_size;
3687283b4a9bSStephen M. Cameron 	last_column = last_row_offset / map->strip_size;
3688283b4a9bSStephen M. Cameron #endif
3689283b4a9bSStephen M. Cameron 
3690283b4a9bSStephen M. Cameron 	/* if this isn't a single row/column then give to the controller */
3691283b4a9bSStephen M. Cameron 	if ((first_row != last_row) || (first_column != last_column))
3692283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3693283b4a9bSStephen M. Cameron 
3694283b4a9bSStephen M. Cameron 	/* proceeding with driver mapping */
36956b80b18fSScott Teel 	total_disks_per_row = map->data_disks_per_row +
36966b80b18fSScott Teel 				map->metadata_disks_per_row;
3697283b4a9bSStephen M. Cameron 	map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
3698283b4a9bSStephen M. Cameron 				map->row_cnt;
36996b80b18fSScott Teel 	map_index = (map_row * total_disks_per_row) + first_column;
37006b80b18fSScott Teel 
37016b80b18fSScott Teel 	switch (dev->raid_level) {
37026b80b18fSScott Teel 	case HPSA_RAID_0:
37036b80b18fSScott Teel 		break; /* nothing special to do */
37046b80b18fSScott Teel 	case HPSA_RAID_1:
37056b80b18fSScott Teel 		/* Handles load balance across RAID 1 members.
37066b80b18fSScott Teel 		 * (2-drive R1 and R10 with even # of drives.)
37076b80b18fSScott Teel 		 * Appropriate for SSDs, not optimal for HDDs
3708283b4a9bSStephen M. Cameron 		 */
37096b80b18fSScott Teel 		BUG_ON(map->layout_map_count != 2);
3710283b4a9bSStephen M. Cameron 		if (dev->offload_to_mirror)
3711283b4a9bSStephen M. Cameron 			map_index += map->data_disks_per_row;
3712283b4a9bSStephen M. Cameron 		dev->offload_to_mirror = !dev->offload_to_mirror;
37136b80b18fSScott Teel 		break;
37146b80b18fSScott Teel 	case HPSA_RAID_ADM:
37156b80b18fSScott Teel 		/* Handles N-way mirrors  (R1-ADM)
37166b80b18fSScott Teel 		 * and R10 with # of drives divisible by 3.)
37176b80b18fSScott Teel 		 */
37186b80b18fSScott Teel 		BUG_ON(map->layout_map_count != 3);
37196b80b18fSScott Teel 
37206b80b18fSScott Teel 		offload_to_mirror = dev->offload_to_mirror;
37216b80b18fSScott Teel 		raid_map_helper(map, offload_to_mirror,
37226b80b18fSScott Teel 				&map_index, &current_group);
37236b80b18fSScott Teel 		/* set mirror group to use next time */
37246b80b18fSScott Teel 		offload_to_mirror =
37256b80b18fSScott Teel 			(offload_to_mirror >= map->layout_map_count - 1)
37266b80b18fSScott Teel 			? 0 : offload_to_mirror + 1;
37276b80b18fSScott Teel 		/* FIXME: remove after debug/dev */
37286b80b18fSScott Teel 		BUG_ON(offload_to_mirror >= map->layout_map_count);
37296b80b18fSScott Teel 		dev_warn(&h->pdev->dev,
37306b80b18fSScott Teel 			"DEBUG: Using physical disk map index %d from mirror group %d\n",
37316b80b18fSScott Teel 			map_index, offload_to_mirror);
37326b80b18fSScott Teel 		dev->offload_to_mirror = offload_to_mirror;
37336b80b18fSScott Teel 		/* Avoid direct use of dev->offload_to_mirror within this
37346b80b18fSScott Teel 		 * function since multiple threads might simultaneously
37356b80b18fSScott Teel 		 * increment it beyond the range of dev->layout_map_count -1.
37366b80b18fSScott Teel 		 */
37376b80b18fSScott Teel 		break;
37386b80b18fSScott Teel 	case HPSA_RAID_5:
37396b80b18fSScott Teel 	case HPSA_RAID_6:
37406b80b18fSScott Teel 		if (map->layout_map_count <= 1)
37416b80b18fSScott Teel 			break;
37426b80b18fSScott Teel 
37436b80b18fSScott Teel 		/* Verify first and last block are in same RAID group */
37446b80b18fSScott Teel 		r5or6_blocks_per_row =
37456b80b18fSScott Teel 			map->strip_size * map->data_disks_per_row;
37466b80b18fSScott Teel 		BUG_ON(r5or6_blocks_per_row == 0);
37476b80b18fSScott Teel 		stripesize = r5or6_blocks_per_row * map->layout_map_count;
37486b80b18fSScott Teel #if BITS_PER_LONG == 32
37496b80b18fSScott Teel 		tmpdiv = first_block;
37506b80b18fSScott Teel 		first_group = do_div(tmpdiv, stripesize);
37516b80b18fSScott Teel 		tmpdiv = first_group;
37526b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
37536b80b18fSScott Teel 		first_group = tmpdiv;
37546b80b18fSScott Teel 		tmpdiv = last_block;
37556b80b18fSScott Teel 		last_group = do_div(tmpdiv, stripesize);
37566b80b18fSScott Teel 		tmpdiv = last_group;
37576b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
37586b80b18fSScott Teel 		last_group = tmpdiv;
37596b80b18fSScott Teel #else
37606b80b18fSScott Teel 		first_group = (first_block % stripesize) / r5or6_blocks_per_row;
37616b80b18fSScott Teel 		last_group = (last_block % stripesize) / r5or6_blocks_per_row;
37626b80b18fSScott Teel 		if (first_group != last_group)
37636b80b18fSScott Teel #endif
37646b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
37656b80b18fSScott Teel 
37666b80b18fSScott Teel 		/* Verify request is in a single row of RAID 5/6 */
37676b80b18fSScott Teel #if BITS_PER_LONG == 32
37686b80b18fSScott Teel 		tmpdiv = first_block;
37696b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
37706b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row = tmpdiv;
37716b80b18fSScott Teel 		tmpdiv = last_block;
37726b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
37736b80b18fSScott Teel 		r5or6_last_row = r0_last_row = tmpdiv;
37746b80b18fSScott Teel #else
37756b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row =
37766b80b18fSScott Teel 						first_block / stripesize;
37776b80b18fSScott Teel 		r5or6_last_row = r0_last_row = last_block / stripesize;
37786b80b18fSScott Teel #endif
37796b80b18fSScott Teel 		if (r5or6_first_row != r5or6_last_row)
37806b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
37816b80b18fSScott Teel 
37826b80b18fSScott Teel 
37836b80b18fSScott Teel 		/* Verify request is in a single column */
37846b80b18fSScott Teel #if BITS_PER_LONG == 32
37856b80b18fSScott Teel 		tmpdiv = first_block;
37866b80b18fSScott Teel 		first_row_offset = do_div(tmpdiv, stripesize);
37876b80b18fSScott Teel 		tmpdiv = first_row_offset;
37886b80b18fSScott Teel 		first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
37896b80b18fSScott Teel 		r5or6_first_row_offset = first_row_offset;
37906b80b18fSScott Teel 		tmpdiv = last_block;
37916b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, stripesize);
37926b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
37936b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
37946b80b18fSScott Teel 		tmpdiv = r5or6_first_row_offset;
37956b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
37966b80b18fSScott Teel 		first_column = r5or6_first_column = tmpdiv;
37976b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
37986b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
37996b80b18fSScott Teel 		r5or6_last_column = tmpdiv;
38006b80b18fSScott Teel #else
38016b80b18fSScott Teel 		first_row_offset = r5or6_first_row_offset =
38026b80b18fSScott Teel 			(u32)((first_block % stripesize) %
38036b80b18fSScott Teel 						r5or6_blocks_per_row);
38046b80b18fSScott Teel 
38056b80b18fSScott Teel 		r5or6_last_row_offset =
38066b80b18fSScott Teel 			(u32)((last_block % stripesize) %
38076b80b18fSScott Teel 						r5or6_blocks_per_row);
38086b80b18fSScott Teel 
38096b80b18fSScott Teel 		first_column = r5or6_first_column =
38106b80b18fSScott Teel 			r5or6_first_row_offset / map->strip_size;
38116b80b18fSScott Teel 		r5or6_last_column =
38126b80b18fSScott Teel 			r5or6_last_row_offset / map->strip_size;
38136b80b18fSScott Teel #endif
38146b80b18fSScott Teel 		if (r5or6_first_column != r5or6_last_column)
38156b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
38166b80b18fSScott Teel 
38176b80b18fSScott Teel 		/* Request is eligible */
38186b80b18fSScott Teel 		map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
38196b80b18fSScott Teel 			map->row_cnt;
38206b80b18fSScott Teel 
38216b80b18fSScott Teel 		map_index = (first_group *
38226b80b18fSScott Teel 			(map->row_cnt * total_disks_per_row)) +
38236b80b18fSScott Teel 			(map_row * total_disks_per_row) + first_column;
38246b80b18fSScott Teel 		break;
38256b80b18fSScott Teel 	default:
38266b80b18fSScott Teel 		return IO_ACCEL_INELIGIBLE;
3827283b4a9bSStephen M. Cameron 	}
38286b80b18fSScott Teel 
3829283b4a9bSStephen M. Cameron 	disk_handle = dd[map_index].ioaccel_handle;
3830283b4a9bSStephen M. Cameron 	disk_block = map->disk_starting_blk + (first_row * map->strip_size) +
3831283b4a9bSStephen M. Cameron 			(first_row_offset - (first_column * map->strip_size));
3832283b4a9bSStephen M. Cameron 	disk_block_cnt = block_cnt;
3833283b4a9bSStephen M. Cameron 
3834283b4a9bSStephen M. Cameron 	/* handle differing logical/physical block sizes */
3835283b4a9bSStephen M. Cameron 	if (map->phys_blk_shift) {
3836283b4a9bSStephen M. Cameron 		disk_block <<= map->phys_blk_shift;
3837283b4a9bSStephen M. Cameron 		disk_block_cnt <<= map->phys_blk_shift;
3838283b4a9bSStephen M. Cameron 	}
3839283b4a9bSStephen M. Cameron 	BUG_ON(disk_block_cnt > 0xffff);
3840283b4a9bSStephen M. Cameron 
3841283b4a9bSStephen M. Cameron 	/* build the new CDB for the physical disk I/O */
3842283b4a9bSStephen M. Cameron 	if (disk_block > 0xffffffff) {
3843283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_16 : READ_16;
3844283b4a9bSStephen M. Cameron 		cdb[1] = 0;
3845283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 56);
3846283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 48);
3847283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 40);
3848283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block >> 32);
3849283b4a9bSStephen M. Cameron 		cdb[6] = (u8) (disk_block >> 24);
3850283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block >> 16);
3851283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block >> 8);
3852283b4a9bSStephen M. Cameron 		cdb[9] = (u8) (disk_block);
3853283b4a9bSStephen M. Cameron 		cdb[10] = (u8) (disk_block_cnt >> 24);
3854283b4a9bSStephen M. Cameron 		cdb[11] = (u8) (disk_block_cnt >> 16);
3855283b4a9bSStephen M. Cameron 		cdb[12] = (u8) (disk_block_cnt >> 8);
3856283b4a9bSStephen M. Cameron 		cdb[13] = (u8) (disk_block_cnt);
3857283b4a9bSStephen M. Cameron 		cdb[14] = 0;
3858283b4a9bSStephen M. Cameron 		cdb[15] = 0;
3859283b4a9bSStephen M. Cameron 		cdb_len = 16;
3860283b4a9bSStephen M. Cameron 	} else {
3861283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
3862283b4a9bSStephen M. Cameron 		cdb[1] = 0;
3863283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 24);
3864283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 16);
3865283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 8);
3866283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block);
3867283b4a9bSStephen M. Cameron 		cdb[6] = 0;
3868283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block_cnt >> 8);
3869283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block_cnt);
3870283b4a9bSStephen M. Cameron 		cdb[9] = 0;
3871283b4a9bSStephen M. Cameron 		cdb_len = 10;
3872283b4a9bSStephen M. Cameron 	}
3873283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
3874283b4a9bSStephen M. Cameron 						dev->scsi3addr);
3875283b4a9bSStephen M. Cameron }
3876283b4a9bSStephen M. Cameron 
3877f281233dSJeff Garzik static int hpsa_scsi_queue_command_lck(struct scsi_cmnd *cmd,
3878edd16368SStephen M. Cameron 	void (*done)(struct scsi_cmnd *))
3879edd16368SStephen M. Cameron {
3880edd16368SStephen M. Cameron 	struct ctlr_info *h;
3881edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
3882edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
3883edd16368SStephen M. Cameron 	struct CommandList *c;
3884edd16368SStephen M. Cameron 	unsigned long flags;
3885283b4a9bSStephen M. Cameron 	int rc = 0;
3886edd16368SStephen M. Cameron 
3887edd16368SStephen M. Cameron 	/* Get the ptr to our adapter structure out of cmd->host. */
3888edd16368SStephen M. Cameron 	h = sdev_to_hba(cmd->device);
3889edd16368SStephen M. Cameron 	dev = cmd->device->hostdata;
3890edd16368SStephen M. Cameron 	if (!dev) {
3891edd16368SStephen M. Cameron 		cmd->result = DID_NO_CONNECT << 16;
3892edd16368SStephen M. Cameron 		done(cmd);
3893edd16368SStephen M. Cameron 		return 0;
3894edd16368SStephen M. Cameron 	}
3895edd16368SStephen M. Cameron 	memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
3896edd16368SStephen M. Cameron 
3897edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
3898a0c12413SStephen M. Cameron 	if (unlikely(h->lockup_detected)) {
3899a0c12413SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
3900a0c12413SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
3901a0c12413SStephen M. Cameron 		done(cmd);
3902a0c12413SStephen M. Cameron 		return 0;
3903a0c12413SStephen M. Cameron 	}
3904edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
3905e16a33adSMatt Gates 	c = cmd_alloc(h);
3906edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
3907edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
3908edd16368SStephen M. Cameron 		return SCSI_MLQUEUE_HOST_BUSY;
3909edd16368SStephen M. Cameron 	}
3910edd16368SStephen M. Cameron 
3911edd16368SStephen M. Cameron 	/* Fill in the command list header */
3912edd16368SStephen M. Cameron 
3913edd16368SStephen M. Cameron 	cmd->scsi_done = done;    /* save this for use by completion code */
3914edd16368SStephen M. Cameron 
3915edd16368SStephen M. Cameron 	/* save c in case we have to abort it  */
3916edd16368SStephen M. Cameron 	cmd->host_scribble = (unsigned char *) c;
3917edd16368SStephen M. Cameron 
3918edd16368SStephen M. Cameron 	c->cmd_type = CMD_SCSI;
3919edd16368SStephen M. Cameron 	c->scsi_cmd = cmd;
3920e1f7de0cSMatt Gates 
3921283b4a9bSStephen M. Cameron 	/* Call alternate submit routine for I/O accelerated commands.
3922283b4a9bSStephen M. Cameron 	 * Retries always go down the normal I/O path.
3923283b4a9bSStephen M. Cameron 	 */
3924283b4a9bSStephen M. Cameron 	if (likely(cmd->retries == 0 &&
3925da0697bdSScott Teel 		cmd->request->cmd_type == REQ_TYPE_FS &&
3926da0697bdSScott Teel 		h->acciopath_status)) {
3927283b4a9bSStephen M. Cameron 		if (dev->offload_enabled) {
3928283b4a9bSStephen M. Cameron 			rc = hpsa_scsi_ioaccel_raid_map(h, c);
3929283b4a9bSStephen M. Cameron 			if (rc == 0)
3930283b4a9bSStephen M. Cameron 				return 0; /* Sent on ioaccel path */
3931283b4a9bSStephen M. Cameron 			if (rc < 0) {   /* scsi_dma_map failed. */
3932283b4a9bSStephen M. Cameron 				cmd_free(h, c);
3933283b4a9bSStephen M. Cameron 				return SCSI_MLQUEUE_HOST_BUSY;
3934283b4a9bSStephen M. Cameron 			}
3935283b4a9bSStephen M. Cameron 		} else if (dev->ioaccel_handle) {
3936283b4a9bSStephen M. Cameron 			rc = hpsa_scsi_ioaccel_direct_map(h, c);
3937283b4a9bSStephen M. Cameron 			if (rc == 0)
3938283b4a9bSStephen M. Cameron 				return 0; /* Sent on direct map path */
3939283b4a9bSStephen M. Cameron 			if (rc < 0) {   /* scsi_dma_map failed. */
3940283b4a9bSStephen M. Cameron 				cmd_free(h, c);
3941283b4a9bSStephen M. Cameron 				return SCSI_MLQUEUE_HOST_BUSY;
3942283b4a9bSStephen M. Cameron 			}
3943283b4a9bSStephen M. Cameron 		}
3944283b4a9bSStephen M. Cameron 	}
3945e1f7de0cSMatt Gates 
3946edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;  /* unused in simple mode */
3947edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
3948303932fdSDon Brace 	c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT);
3949303932fdSDon Brace 	c->Header.Tag.lower |= DIRECT_LOOKUP_BIT;
3950edd16368SStephen M. Cameron 
3951edd16368SStephen M. Cameron 	/* Fill in the request block... */
3952edd16368SStephen M. Cameron 
3953edd16368SStephen M. Cameron 	c->Request.Timeout = 0;
3954edd16368SStephen M. Cameron 	memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
3955edd16368SStephen M. Cameron 	BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
3956edd16368SStephen M. Cameron 	c->Request.CDBLen = cmd->cmd_len;
3957edd16368SStephen M. Cameron 	memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
3958edd16368SStephen M. Cameron 	c->Request.Type.Type = TYPE_CMD;
3959edd16368SStephen M. Cameron 	c->Request.Type.Attribute = ATTR_SIMPLE;
3960edd16368SStephen M. Cameron 	switch (cmd->sc_data_direction) {
3961edd16368SStephen M. Cameron 	case DMA_TO_DEVICE:
3962edd16368SStephen M. Cameron 		c->Request.Type.Direction = XFER_WRITE;
3963edd16368SStephen M. Cameron 		break;
3964edd16368SStephen M. Cameron 	case DMA_FROM_DEVICE:
3965edd16368SStephen M. Cameron 		c->Request.Type.Direction = XFER_READ;
3966edd16368SStephen M. Cameron 		break;
3967edd16368SStephen M. Cameron 	case DMA_NONE:
3968edd16368SStephen M. Cameron 		c->Request.Type.Direction = XFER_NONE;
3969edd16368SStephen M. Cameron 		break;
3970edd16368SStephen M. Cameron 	case DMA_BIDIRECTIONAL:
3971edd16368SStephen M. Cameron 		/* This can happen if a buggy application does a scsi passthru
3972edd16368SStephen M. Cameron 		 * and sets both inlen and outlen to non-zero. ( see
3973edd16368SStephen M. Cameron 		 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
3974edd16368SStephen M. Cameron 		 */
3975edd16368SStephen M. Cameron 
3976edd16368SStephen M. Cameron 		c->Request.Type.Direction = XFER_RSVD;
3977edd16368SStephen M. Cameron 		/* This is technically wrong, and hpsa controllers should
3978edd16368SStephen M. Cameron 		 * reject it with CMD_INVALID, which is the most correct
3979edd16368SStephen M. Cameron 		 * response, but non-fibre backends appear to let it
3980edd16368SStephen M. Cameron 		 * slide by, and give the same results as if this field
3981edd16368SStephen M. Cameron 		 * were set correctly.  Either way is acceptable for
3982edd16368SStephen M. Cameron 		 * our purposes here.
3983edd16368SStephen M. Cameron 		 */
3984edd16368SStephen M. Cameron 
3985edd16368SStephen M. Cameron 		break;
3986edd16368SStephen M. Cameron 
3987edd16368SStephen M. Cameron 	default:
3988edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3989edd16368SStephen M. Cameron 			cmd->sc_data_direction);
3990edd16368SStephen M. Cameron 		BUG();
3991edd16368SStephen M. Cameron 		break;
3992edd16368SStephen M. Cameron 	}
3993edd16368SStephen M. Cameron 
399433a2ffceSStephen M. Cameron 	if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
3995edd16368SStephen M. Cameron 		cmd_free(h, c);
3996edd16368SStephen M. Cameron 		return SCSI_MLQUEUE_HOST_BUSY;
3997edd16368SStephen M. Cameron 	}
3998edd16368SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
3999edd16368SStephen M. Cameron 	/* the cmd'll come back via intr handler in complete_scsi_command()  */
4000edd16368SStephen M. Cameron 	return 0;
4001edd16368SStephen M. Cameron }
4002edd16368SStephen M. Cameron 
4003f281233dSJeff Garzik static DEF_SCSI_QCMD(hpsa_scsi_queue_command)
4004f281233dSJeff Garzik 
40055f389360SStephen M. Cameron static int do_not_scan_if_controller_locked_up(struct ctlr_info *h)
40065f389360SStephen M. Cameron {
40075f389360SStephen M. Cameron 	unsigned long flags;
40085f389360SStephen M. Cameron 
40095f389360SStephen M. Cameron 	/*
40105f389360SStephen M. Cameron 	 * Don't let rescans be initiated on a controller known
40115f389360SStephen M. Cameron 	 * to be locked up.  If the controller locks up *during*
40125f389360SStephen M. Cameron 	 * a rescan, that thread is probably hosed, but at least
40135f389360SStephen M. Cameron 	 * we can prevent new rescan threads from piling up on a
40145f389360SStephen M. Cameron 	 * locked up controller.
40155f389360SStephen M. Cameron 	 */
40165f389360SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
40175f389360SStephen M. Cameron 	if (unlikely(h->lockup_detected)) {
40185f389360SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
40195f389360SStephen M. Cameron 		spin_lock_irqsave(&h->scan_lock, flags);
40205f389360SStephen M. Cameron 		h->scan_finished = 1;
40215f389360SStephen M. Cameron 		wake_up_all(&h->scan_wait_queue);
40225f389360SStephen M. Cameron 		spin_unlock_irqrestore(&h->scan_lock, flags);
40235f389360SStephen M. Cameron 		return 1;
40245f389360SStephen M. Cameron 	}
40255f389360SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
40265f389360SStephen M. Cameron 	return 0;
40275f389360SStephen M. Cameron }
40285f389360SStephen M. Cameron 
4029a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *sh)
4030a08a8471SStephen M. Cameron {
4031a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
4032a08a8471SStephen M. Cameron 	unsigned long flags;
4033a08a8471SStephen M. Cameron 
40345f389360SStephen M. Cameron 	if (do_not_scan_if_controller_locked_up(h))
40355f389360SStephen M. Cameron 		return;
40365f389360SStephen M. Cameron 
4037a08a8471SStephen M. Cameron 	/* wait until any scan already in progress is finished. */
4038a08a8471SStephen M. Cameron 	while (1) {
4039a08a8471SStephen M. Cameron 		spin_lock_irqsave(&h->scan_lock, flags);
4040a08a8471SStephen M. Cameron 		if (h->scan_finished)
4041a08a8471SStephen M. Cameron 			break;
4042a08a8471SStephen M. Cameron 		spin_unlock_irqrestore(&h->scan_lock, flags);
4043a08a8471SStephen M. Cameron 		wait_event(h->scan_wait_queue, h->scan_finished);
4044a08a8471SStephen M. Cameron 		/* Note: We don't need to worry about a race between this
4045a08a8471SStephen M. Cameron 		 * thread and driver unload because the midlayer will
4046a08a8471SStephen M. Cameron 		 * have incremented the reference count, so unload won't
4047a08a8471SStephen M. Cameron 		 * happen if we're in here.
4048a08a8471SStephen M. Cameron 		 */
4049a08a8471SStephen M. Cameron 	}
4050a08a8471SStephen M. Cameron 	h->scan_finished = 0; /* mark scan as in progress */
4051a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4052a08a8471SStephen M. Cameron 
40535f389360SStephen M. Cameron 	if (do_not_scan_if_controller_locked_up(h))
40545f389360SStephen M. Cameron 		return;
40555f389360SStephen M. Cameron 
4056a08a8471SStephen M. Cameron 	hpsa_update_scsi_devices(h, h->scsi_host->host_no);
4057a08a8471SStephen M. Cameron 
4058a08a8471SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
4059a08a8471SStephen M. Cameron 	h->scan_finished = 1; /* mark scan as finished. */
4060a08a8471SStephen M. Cameron 	wake_up_all(&h->scan_wait_queue);
4061a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4062a08a8471SStephen M. Cameron }
4063a08a8471SStephen M. Cameron 
4064a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
4065a08a8471SStephen M. Cameron 	unsigned long elapsed_time)
4066a08a8471SStephen M. Cameron {
4067a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
4068a08a8471SStephen M. Cameron 	unsigned long flags;
4069a08a8471SStephen M. Cameron 	int finished;
4070a08a8471SStephen M. Cameron 
4071a08a8471SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
4072a08a8471SStephen M. Cameron 	finished = h->scan_finished;
4073a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4074a08a8471SStephen M. Cameron 	return finished;
4075a08a8471SStephen M. Cameron }
4076a08a8471SStephen M. Cameron 
4077667e23d4SStephen M. Cameron static int hpsa_change_queue_depth(struct scsi_device *sdev,
4078667e23d4SStephen M. Cameron 	int qdepth, int reason)
4079667e23d4SStephen M. Cameron {
4080667e23d4SStephen M. Cameron 	struct ctlr_info *h = sdev_to_hba(sdev);
4081667e23d4SStephen M. Cameron 
4082667e23d4SStephen M. Cameron 	if (reason != SCSI_QDEPTH_DEFAULT)
4083667e23d4SStephen M. Cameron 		return -ENOTSUPP;
4084667e23d4SStephen M. Cameron 
4085667e23d4SStephen M. Cameron 	if (qdepth < 1)
4086667e23d4SStephen M. Cameron 		qdepth = 1;
4087667e23d4SStephen M. Cameron 	else
4088667e23d4SStephen M. Cameron 		if (qdepth > h->nr_cmds)
4089667e23d4SStephen M. Cameron 			qdepth = h->nr_cmds;
4090667e23d4SStephen M. Cameron 	scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
4091667e23d4SStephen M. Cameron 	return sdev->queue_depth;
4092667e23d4SStephen M. Cameron }
4093667e23d4SStephen M. Cameron 
4094edd16368SStephen M. Cameron static void hpsa_unregister_scsi(struct ctlr_info *h)
4095edd16368SStephen M. Cameron {
4096edd16368SStephen M. Cameron 	/* we are being forcibly unloaded, and may not refuse. */
4097edd16368SStephen M. Cameron 	scsi_remove_host(h->scsi_host);
4098edd16368SStephen M. Cameron 	scsi_host_put(h->scsi_host);
4099edd16368SStephen M. Cameron 	h->scsi_host = NULL;
4100edd16368SStephen M. Cameron }
4101edd16368SStephen M. Cameron 
4102edd16368SStephen M. Cameron static int hpsa_register_scsi(struct ctlr_info *h)
4103edd16368SStephen M. Cameron {
4104b705690dSStephen M. Cameron 	struct Scsi_Host *sh;
4105b705690dSStephen M. Cameron 	int error;
4106edd16368SStephen M. Cameron 
4107b705690dSStephen M. Cameron 	sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
4108b705690dSStephen M. Cameron 	if (sh == NULL)
4109b705690dSStephen M. Cameron 		goto fail;
4110b705690dSStephen M. Cameron 
4111b705690dSStephen M. Cameron 	sh->io_port = 0;
4112b705690dSStephen M. Cameron 	sh->n_io_port = 0;
4113b705690dSStephen M. Cameron 	sh->this_id = -1;
4114b705690dSStephen M. Cameron 	sh->max_channel = 3;
4115b705690dSStephen M. Cameron 	sh->max_cmd_len = MAX_COMMAND_SIZE;
4116b705690dSStephen M. Cameron 	sh->max_lun = HPSA_MAX_LUN;
4117b705690dSStephen M. Cameron 	sh->max_id = HPSA_MAX_LUN;
4118b705690dSStephen M. Cameron 	sh->can_queue = h->nr_cmds;
4119b705690dSStephen M. Cameron 	sh->cmd_per_lun = h->nr_cmds;
4120b705690dSStephen M. Cameron 	sh->sg_tablesize = h->maxsgentries;
4121b705690dSStephen M. Cameron 	h->scsi_host = sh;
4122b705690dSStephen M. Cameron 	sh->hostdata[0] = (unsigned long) h;
4123b705690dSStephen M. Cameron 	sh->irq = h->intr[h->intr_mode];
4124b705690dSStephen M. Cameron 	sh->unique_id = sh->irq;
4125b705690dSStephen M. Cameron 	error = scsi_add_host(sh, &h->pdev->dev);
4126b705690dSStephen M. Cameron 	if (error)
4127b705690dSStephen M. Cameron 		goto fail_host_put;
4128b705690dSStephen M. Cameron 	scsi_scan_host(sh);
4129b705690dSStephen M. Cameron 	return 0;
4130b705690dSStephen M. Cameron 
4131b705690dSStephen M. Cameron  fail_host_put:
4132b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_add_host"
4133b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
4134b705690dSStephen M. Cameron 	scsi_host_put(sh);
4135b705690dSStephen M. Cameron 	return error;
4136b705690dSStephen M. Cameron  fail:
4137b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_host_alloc"
4138b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
4139b705690dSStephen M. Cameron 	return -ENOMEM;
4140edd16368SStephen M. Cameron }
4141edd16368SStephen M. Cameron 
4142edd16368SStephen M. Cameron static int wait_for_device_to_become_ready(struct ctlr_info *h,
4143edd16368SStephen M. Cameron 	unsigned char lunaddr[])
4144edd16368SStephen M. Cameron {
4145edd16368SStephen M. Cameron 	int rc = 0;
4146edd16368SStephen M. Cameron 	int count = 0;
4147edd16368SStephen M. Cameron 	int waittime = 1; /* seconds */
4148edd16368SStephen M. Cameron 	struct CommandList *c;
4149edd16368SStephen M. Cameron 
4150edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
4151edd16368SStephen M. Cameron 	if (!c) {
4152edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in "
4153edd16368SStephen M. Cameron 			"wait_for_device_to_become_ready.\n");
4154edd16368SStephen M. Cameron 		return IO_ERROR;
4155edd16368SStephen M. Cameron 	}
4156edd16368SStephen M. Cameron 
4157edd16368SStephen M. Cameron 	/* Send test unit ready until device ready, or give up. */
4158edd16368SStephen M. Cameron 	while (count < HPSA_TUR_RETRY_LIMIT) {
4159edd16368SStephen M. Cameron 
4160edd16368SStephen M. Cameron 		/* Wait for a bit.  do this first, because if we send
4161edd16368SStephen M. Cameron 		 * the TUR right away, the reset will just abort it.
4162edd16368SStephen M. Cameron 		 */
4163edd16368SStephen M. Cameron 		msleep(1000 * waittime);
4164edd16368SStephen M. Cameron 		count++;
4165edd16368SStephen M. Cameron 
4166edd16368SStephen M. Cameron 		/* Increase wait time with each try, up to a point. */
4167edd16368SStephen M. Cameron 		if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
4168edd16368SStephen M. Cameron 			waittime = waittime * 2;
4169edd16368SStephen M. Cameron 
4170a2dac136SStephen M. Cameron 		/* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
4171a2dac136SStephen M. Cameron 		(void) fill_cmd(c, TEST_UNIT_READY, h,
4172a2dac136SStephen M. Cameron 				NULL, 0, 0, lunaddr, TYPE_CMD);
4173edd16368SStephen M. Cameron 		hpsa_scsi_do_simple_cmd_core(h, c);
4174edd16368SStephen M. Cameron 		/* no unmap needed here because no data xfer. */
4175edd16368SStephen M. Cameron 
4176edd16368SStephen M. Cameron 		if (c->err_info->CommandStatus == CMD_SUCCESS)
4177edd16368SStephen M. Cameron 			break;
4178edd16368SStephen M. Cameron 
4179edd16368SStephen M. Cameron 		if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
4180edd16368SStephen M. Cameron 			c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
4181edd16368SStephen M. Cameron 			(c->err_info->SenseInfo[2] == NO_SENSE ||
4182edd16368SStephen M. Cameron 			c->err_info->SenseInfo[2] == UNIT_ATTENTION))
4183edd16368SStephen M. Cameron 			break;
4184edd16368SStephen M. Cameron 
4185edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "waiting %d secs "
4186edd16368SStephen M. Cameron 			"for device to become ready.\n", waittime);
4187edd16368SStephen M. Cameron 		rc = 1; /* device not ready. */
4188edd16368SStephen M. Cameron 	}
4189edd16368SStephen M. Cameron 
4190edd16368SStephen M. Cameron 	if (rc)
4191edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "giving up on device.\n");
4192edd16368SStephen M. Cameron 	else
4193edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "device is ready.\n");
4194edd16368SStephen M. Cameron 
4195edd16368SStephen M. Cameron 	cmd_special_free(h, c);
4196edd16368SStephen M. Cameron 	return rc;
4197edd16368SStephen M. Cameron }
4198edd16368SStephen M. Cameron 
4199edd16368SStephen M. Cameron /* Need at least one of these error handlers to keep ../scsi/hosts.c from
4200edd16368SStephen M. Cameron  * complaining.  Doing a host- or bus-reset can't do anything good here.
4201edd16368SStephen M. Cameron  */
4202edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
4203edd16368SStephen M. Cameron {
4204edd16368SStephen M. Cameron 	int rc;
4205edd16368SStephen M. Cameron 	struct ctlr_info *h;
4206edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
4207edd16368SStephen M. Cameron 
4208edd16368SStephen M. Cameron 	/* find the controller to which the command to be aborted was sent */
4209edd16368SStephen M. Cameron 	h = sdev_to_hba(scsicmd->device);
4210edd16368SStephen M. Cameron 	if (h == NULL) /* paranoia */
4211edd16368SStephen M. Cameron 		return FAILED;
4212edd16368SStephen M. Cameron 	dev = scsicmd->device->hostdata;
4213edd16368SStephen M. Cameron 	if (!dev) {
4214edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
4215edd16368SStephen M. Cameron 			"device lookup failed.\n");
4216edd16368SStephen M. Cameron 		return FAILED;
4217edd16368SStephen M. Cameron 	}
4218d416b0c7SStephen M. Cameron 	dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
4219d416b0c7SStephen M. Cameron 		h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
4220edd16368SStephen M. Cameron 	/* send a reset to the SCSI LUN which the command was sent to */
4221bf711ac6SScott Teel 	rc = hpsa_send_reset(h, dev->scsi3addr, HPSA_RESET_TYPE_LUN);
4222edd16368SStephen M. Cameron 	if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
4223edd16368SStephen M. Cameron 		return SUCCESS;
4224edd16368SStephen M. Cameron 
4225edd16368SStephen M. Cameron 	dev_warn(&h->pdev->dev, "resetting device failed.\n");
4226edd16368SStephen M. Cameron 	return FAILED;
4227edd16368SStephen M. Cameron }
4228edd16368SStephen M. Cameron 
42296cba3f19SStephen M. Cameron static void swizzle_abort_tag(u8 *tag)
42306cba3f19SStephen M. Cameron {
42316cba3f19SStephen M. Cameron 	u8 original_tag[8];
42326cba3f19SStephen M. Cameron 
42336cba3f19SStephen M. Cameron 	memcpy(original_tag, tag, 8);
42346cba3f19SStephen M. Cameron 	tag[0] = original_tag[3];
42356cba3f19SStephen M. Cameron 	tag[1] = original_tag[2];
42366cba3f19SStephen M. Cameron 	tag[2] = original_tag[1];
42376cba3f19SStephen M. Cameron 	tag[3] = original_tag[0];
42386cba3f19SStephen M. Cameron 	tag[4] = original_tag[7];
42396cba3f19SStephen M. Cameron 	tag[5] = original_tag[6];
42406cba3f19SStephen M. Cameron 	tag[6] = original_tag[5];
42416cba3f19SStephen M. Cameron 	tag[7] = original_tag[4];
42426cba3f19SStephen M. Cameron }
42436cba3f19SStephen M. Cameron 
424417eb87d2SScott Teel static void hpsa_get_tag(struct ctlr_info *h,
424517eb87d2SScott Teel 	struct CommandList *c, u32 *taglower, u32 *tagupper)
424617eb87d2SScott Teel {
424717eb87d2SScott Teel 	if (c->cmd_type == CMD_IOACCEL1) {
424817eb87d2SScott Teel 		struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
424917eb87d2SScott Teel 			&h->ioaccel_cmd_pool[c->cmdindex];
425017eb87d2SScott Teel 		*tagupper = cm1->Tag.upper;
425117eb87d2SScott Teel 		*taglower = cm1->Tag.lower;
425254b6e9e9SScott Teel 		return;
425354b6e9e9SScott Teel 	}
425454b6e9e9SScott Teel 	if (c->cmd_type == CMD_IOACCEL2) {
425554b6e9e9SScott Teel 		struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
425654b6e9e9SScott Teel 			&h->ioaccel2_cmd_pool[c->cmdindex];
4257dd0e19f3SScott Teel 		/* upper tag not used in ioaccel2 mode */
4258dd0e19f3SScott Teel 		memset(tagupper, 0, sizeof(*tagupper));
4259dd0e19f3SScott Teel 		*taglower = cm2->Tag;
426054b6e9e9SScott Teel 		return;
426154b6e9e9SScott Teel 	}
426217eb87d2SScott Teel 	*tagupper = c->Header.Tag.upper;
426317eb87d2SScott Teel 	*taglower = c->Header.Tag.lower;
426417eb87d2SScott Teel }
426554b6e9e9SScott Teel 
426617eb87d2SScott Teel 
426775167d2cSStephen M. Cameron static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
42686cba3f19SStephen M. Cameron 	struct CommandList *abort, int swizzle)
426975167d2cSStephen M. Cameron {
427075167d2cSStephen M. Cameron 	int rc = IO_OK;
427175167d2cSStephen M. Cameron 	struct CommandList *c;
427275167d2cSStephen M. Cameron 	struct ErrorInfo *ei;
427317eb87d2SScott Teel 	u32 tagupper, taglower;
427475167d2cSStephen M. Cameron 
427575167d2cSStephen M. Cameron 	c = cmd_special_alloc(h);
427675167d2cSStephen M. Cameron 	if (c == NULL) {	/* trouble... */
427775167d2cSStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
427875167d2cSStephen M. Cameron 		return -ENOMEM;
427975167d2cSStephen M. Cameron 	}
428075167d2cSStephen M. Cameron 
4281a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no buffer to map */
4282a2dac136SStephen M. Cameron 	(void) fill_cmd(c, HPSA_ABORT_MSG, h, abort,
4283a2dac136SStephen M. Cameron 		0, 0, scsi3addr, TYPE_MSG);
42846cba3f19SStephen M. Cameron 	if (swizzle)
42856cba3f19SStephen M. Cameron 		swizzle_abort_tag(&c->Request.CDB[4]);
428675167d2cSStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core(h, c);
428717eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
428875167d2cSStephen M. Cameron 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd_core completed.\n",
428917eb87d2SScott Teel 		__func__, tagupper, taglower);
429075167d2cSStephen M. Cameron 	/* no unmap needed here because no data xfer. */
429175167d2cSStephen M. Cameron 
429275167d2cSStephen M. Cameron 	ei = c->err_info;
429375167d2cSStephen M. Cameron 	switch (ei->CommandStatus) {
429475167d2cSStephen M. Cameron 	case CMD_SUCCESS:
429575167d2cSStephen M. Cameron 		break;
429675167d2cSStephen M. Cameron 	case CMD_UNABORTABLE: /* Very common, don't make noise. */
429775167d2cSStephen M. Cameron 		rc = -1;
429875167d2cSStephen M. Cameron 		break;
429975167d2cSStephen M. Cameron 	default:
430075167d2cSStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
430117eb87d2SScott Teel 			__func__, tagupper, taglower);
4302d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
430375167d2cSStephen M. Cameron 		rc = -1;
430475167d2cSStephen M. Cameron 		break;
430575167d2cSStephen M. Cameron 	}
430675167d2cSStephen M. Cameron 	cmd_special_free(h, c);
4307dd0e19f3SScott Teel 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n",
4308dd0e19f3SScott Teel 		__func__, tagupper, taglower);
430975167d2cSStephen M. Cameron 	return rc;
431075167d2cSStephen M. Cameron }
431175167d2cSStephen M. Cameron 
431275167d2cSStephen M. Cameron /*
431375167d2cSStephen M. Cameron  * hpsa_find_cmd_in_queue
431475167d2cSStephen M. Cameron  *
431575167d2cSStephen M. Cameron  * Used to determine whether a command (find) is still present
431675167d2cSStephen M. Cameron  * in queue_head.   Optionally excludes the last element of queue_head.
431775167d2cSStephen M. Cameron  *
431875167d2cSStephen M. Cameron  * This is used to avoid unnecessary aborts.  Commands in h->reqQ have
431975167d2cSStephen M. Cameron  * not yet been submitted, and so can be aborted by the driver without
432075167d2cSStephen M. Cameron  * sending an abort to the hardware.
432175167d2cSStephen M. Cameron  *
432275167d2cSStephen M. Cameron  * Returns pointer to command if found in queue, NULL otherwise.
432375167d2cSStephen M. Cameron  */
432475167d2cSStephen M. Cameron static struct CommandList *hpsa_find_cmd_in_queue(struct ctlr_info *h,
432575167d2cSStephen M. Cameron 			struct scsi_cmnd *find, struct list_head *queue_head)
432675167d2cSStephen M. Cameron {
432775167d2cSStephen M. Cameron 	unsigned long flags;
432875167d2cSStephen M. Cameron 	struct CommandList *c = NULL;	/* ptr into cmpQ */
432975167d2cSStephen M. Cameron 
433075167d2cSStephen M. Cameron 	if (!find)
433175167d2cSStephen M. Cameron 		return 0;
433275167d2cSStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
433375167d2cSStephen M. Cameron 	list_for_each_entry(c, queue_head, list) {
433475167d2cSStephen M. Cameron 		if (c->scsi_cmd == NULL) /* e.g.: passthru ioctl */
433575167d2cSStephen M. Cameron 			continue;
433675167d2cSStephen M. Cameron 		if (c->scsi_cmd == find) {
433775167d2cSStephen M. Cameron 			spin_unlock_irqrestore(&h->lock, flags);
433875167d2cSStephen M. Cameron 			return c;
433975167d2cSStephen M. Cameron 		}
434075167d2cSStephen M. Cameron 	}
434175167d2cSStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
434275167d2cSStephen M. Cameron 	return NULL;
434375167d2cSStephen M. Cameron }
434475167d2cSStephen M. Cameron 
43456cba3f19SStephen M. Cameron static struct CommandList *hpsa_find_cmd_in_queue_by_tag(struct ctlr_info *h,
43466cba3f19SStephen M. Cameron 					u8 *tag, struct list_head *queue_head)
43476cba3f19SStephen M. Cameron {
43486cba3f19SStephen M. Cameron 	unsigned long flags;
43496cba3f19SStephen M. Cameron 	struct CommandList *c;
43506cba3f19SStephen M. Cameron 
43516cba3f19SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
43526cba3f19SStephen M. Cameron 	list_for_each_entry(c, queue_head, list) {
43536cba3f19SStephen M. Cameron 		if (memcmp(&c->Header.Tag, tag, 8) != 0)
43546cba3f19SStephen M. Cameron 			continue;
43556cba3f19SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
43566cba3f19SStephen M. Cameron 		return c;
43576cba3f19SStephen M. Cameron 	}
43586cba3f19SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
43596cba3f19SStephen M. Cameron 	return NULL;
43606cba3f19SStephen M. Cameron }
43616cba3f19SStephen M. Cameron 
436254b6e9e9SScott Teel /* ioaccel2 path firmware cannot handle abort task requests.
436354b6e9e9SScott Teel  * Change abort requests to physical target reset, and send to the
436454b6e9e9SScott Teel  * address of the physical disk used for the ioaccel 2 command.
436554b6e9e9SScott Teel  * Return 0 on success (IO_OK)
436654b6e9e9SScott Teel  *	 -1 on failure
436754b6e9e9SScott Teel  */
436854b6e9e9SScott Teel 
436954b6e9e9SScott Teel static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
437054b6e9e9SScott Teel 	unsigned char *scsi3addr, struct CommandList *abort)
437154b6e9e9SScott Teel {
437254b6e9e9SScott Teel 	int rc = IO_OK;
437354b6e9e9SScott Teel 	struct scsi_cmnd *scmd; /* scsi command within request being aborted */
437454b6e9e9SScott Teel 	struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
437554b6e9e9SScott Teel 	unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
437654b6e9e9SScott Teel 	unsigned char *psa = &phys_scsi3addr[0];
437754b6e9e9SScott Teel 
437854b6e9e9SScott Teel 	/* Get a pointer to the hpsa logical device. */
437954b6e9e9SScott Teel 	scmd = (struct scsi_cmnd *) abort->scsi_cmd;
438054b6e9e9SScott Teel 	dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
438154b6e9e9SScott Teel 	if (dev == NULL) {
438254b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
438354b6e9e9SScott Teel 			"Cannot abort: no device pointer for command.\n");
438454b6e9e9SScott Teel 			return -1; /* not abortable */
438554b6e9e9SScott Teel 	}
438654b6e9e9SScott Teel 
43872ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
43882ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
43892ba8bfc8SStephen 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",
43902ba8bfc8SStephen M. Cameron 			h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
43912ba8bfc8SStephen M. Cameron 			scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
43922ba8bfc8SStephen M. Cameron 			scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]);
43932ba8bfc8SStephen M. Cameron 
439454b6e9e9SScott Teel 	if (!dev->offload_enabled) {
439554b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
439654b6e9e9SScott Teel 			"Can't abort: device is not operating in HP SSD Smart Path mode.\n");
439754b6e9e9SScott Teel 		return -1; /* not abortable */
439854b6e9e9SScott Teel 	}
439954b6e9e9SScott Teel 
440054b6e9e9SScott Teel 	/* Incoming scsi3addr is logical addr. We need physical disk addr. */
440154b6e9e9SScott Teel 	if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
440254b6e9e9SScott Teel 		dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
440354b6e9e9SScott Teel 		return -1; /* not abortable */
440454b6e9e9SScott Teel 	}
440554b6e9e9SScott Teel 
440654b6e9e9SScott Teel 	/* send the reset */
44072ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
44082ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
44092ba8bfc8SStephen M. Cameron 			"Reset as abort: Resetting physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
44102ba8bfc8SStephen M. Cameron 			psa[0], psa[1], psa[2], psa[3],
44112ba8bfc8SStephen M. Cameron 			psa[4], psa[5], psa[6], psa[7]);
441254b6e9e9SScott Teel 	rc = hpsa_send_reset(h, psa, HPSA_RESET_TYPE_TARGET);
441354b6e9e9SScott Teel 	if (rc != 0) {
441454b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
441554b6e9e9SScott Teel 			"Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
441654b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
441754b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
441854b6e9e9SScott Teel 		return rc; /* failed to reset */
441954b6e9e9SScott Teel 	}
442054b6e9e9SScott Teel 
442154b6e9e9SScott Teel 	/* wait for device to recover */
442254b6e9e9SScott Teel 	if (wait_for_device_to_become_ready(h, psa) != 0) {
442354b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
442454b6e9e9SScott Teel 			"Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
442554b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
442654b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
442754b6e9e9SScott Teel 		return -1;  /* failed to recover */
442854b6e9e9SScott Teel 	}
442954b6e9e9SScott Teel 
443054b6e9e9SScott Teel 	/* device recovered */
443154b6e9e9SScott Teel 	dev_info(&h->pdev->dev,
443254b6e9e9SScott Teel 		"Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
443354b6e9e9SScott Teel 		psa[0], psa[1], psa[2], psa[3],
443454b6e9e9SScott Teel 		psa[4], psa[5], psa[6], psa[7]);
443554b6e9e9SScott Teel 
443654b6e9e9SScott Teel 	return rc; /* success */
443754b6e9e9SScott Teel }
443854b6e9e9SScott Teel 
44396cba3f19SStephen M. Cameron /* Some Smart Arrays need the abort tag swizzled, and some don't.  It's hard to
44406cba3f19SStephen M. Cameron  * tell which kind we're dealing with, so we send the abort both ways.  There
44416cba3f19SStephen M. Cameron  * shouldn't be any collisions between swizzled and unswizzled tags due to the
44426cba3f19SStephen M. Cameron  * way we construct our tags but we check anyway in case the assumptions which
44436cba3f19SStephen M. Cameron  * make this true someday become false.
44446cba3f19SStephen M. Cameron  */
44456cba3f19SStephen M. Cameron static int hpsa_send_abort_both_ways(struct ctlr_info *h,
44466cba3f19SStephen M. Cameron 	unsigned char *scsi3addr, struct CommandList *abort)
44476cba3f19SStephen M. Cameron {
44486cba3f19SStephen M. Cameron 	u8 swizzled_tag[8];
44496cba3f19SStephen M. Cameron 	struct CommandList *c;
44506cba3f19SStephen M. Cameron 	int rc = 0, rc2 = 0;
44516cba3f19SStephen M. Cameron 
445254b6e9e9SScott Teel 	/* ioccelerator mode 2 commands should be aborted via the
445354b6e9e9SScott Teel 	 * accelerated path, since RAID path is unaware of these commands,
445454b6e9e9SScott Teel 	 * but underlying firmware can't handle abort TMF.
445554b6e9e9SScott Teel 	 * Change abort to physical device reset.
445654b6e9e9SScott Teel 	 */
445754b6e9e9SScott Teel 	if (abort->cmd_type == CMD_IOACCEL2)
445854b6e9e9SScott Teel 		return hpsa_send_reset_as_abort_ioaccel2(h, scsi3addr, abort);
445954b6e9e9SScott Teel 
44606cba3f19SStephen M. Cameron 	/* we do not expect to find the swizzled tag in our queue, but
44616cba3f19SStephen M. Cameron 	 * check anyway just to be sure the assumptions which make this
44626cba3f19SStephen M. Cameron 	 * the case haven't become wrong.
44636cba3f19SStephen M. Cameron 	 */
44646cba3f19SStephen M. Cameron 	memcpy(swizzled_tag, &abort->Request.CDB[4], 8);
44656cba3f19SStephen M. Cameron 	swizzle_abort_tag(swizzled_tag);
44666cba3f19SStephen M. Cameron 	c = hpsa_find_cmd_in_queue_by_tag(h, swizzled_tag, &h->cmpQ);
44676cba3f19SStephen M. Cameron 	if (c != NULL) {
44686cba3f19SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Unexpectedly found byte-swapped tag in completion queue.\n");
44696cba3f19SStephen M. Cameron 		return hpsa_send_abort(h, scsi3addr, abort, 0);
44706cba3f19SStephen M. Cameron 	}
44716cba3f19SStephen M. Cameron 	rc = hpsa_send_abort(h, scsi3addr, abort, 0);
44726cba3f19SStephen M. Cameron 
44736cba3f19SStephen M. Cameron 	/* if the command is still in our queue, we can't conclude that it was
44746cba3f19SStephen M. Cameron 	 * aborted (it might have just completed normally) but in any case
44756cba3f19SStephen M. Cameron 	 * we don't need to try to abort it another way.
44766cba3f19SStephen M. Cameron 	 */
44776cba3f19SStephen M. Cameron 	c = hpsa_find_cmd_in_queue(h, abort->scsi_cmd, &h->cmpQ);
44786cba3f19SStephen M. Cameron 	if (c)
44796cba3f19SStephen M. Cameron 		rc2 = hpsa_send_abort(h, scsi3addr, abort, 1);
44806cba3f19SStephen M. Cameron 	return rc && rc2;
44816cba3f19SStephen M. Cameron }
44826cba3f19SStephen M. Cameron 
448375167d2cSStephen M. Cameron /* Send an abort for the specified command.
448475167d2cSStephen M. Cameron  *	If the device and controller support it,
448575167d2cSStephen M. Cameron  *		send a task abort request.
448675167d2cSStephen M. Cameron  */
448775167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
448875167d2cSStephen M. Cameron {
448975167d2cSStephen M. Cameron 
449075167d2cSStephen M. Cameron 	int i, rc;
449175167d2cSStephen M. Cameron 	struct ctlr_info *h;
449275167d2cSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
449375167d2cSStephen M. Cameron 	struct CommandList *abort; /* pointer to command to be aborted */
449475167d2cSStephen M. Cameron 	struct CommandList *found;
449575167d2cSStephen M. Cameron 	struct scsi_cmnd *as;	/* ptr to scsi cmd inside aborted command. */
449675167d2cSStephen M. Cameron 	char msg[256];		/* For debug messaging. */
449775167d2cSStephen M. Cameron 	int ml = 0;
449817eb87d2SScott Teel 	u32 tagupper, taglower;
449975167d2cSStephen M. Cameron 
450075167d2cSStephen M. Cameron 	/* Find the controller of the command to be aborted */
450175167d2cSStephen M. Cameron 	h = sdev_to_hba(sc->device);
450275167d2cSStephen M. Cameron 	if (WARN(h == NULL,
450375167d2cSStephen M. Cameron 			"ABORT REQUEST FAILED, Controller lookup failed.\n"))
450475167d2cSStephen M. Cameron 		return FAILED;
450575167d2cSStephen M. Cameron 
450675167d2cSStephen M. Cameron 	/* Check that controller supports some kind of task abort */
450775167d2cSStephen M. Cameron 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
450875167d2cSStephen M. Cameron 		!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
450975167d2cSStephen M. Cameron 		return FAILED;
451075167d2cSStephen M. Cameron 
451175167d2cSStephen M. Cameron 	memset(msg, 0, sizeof(msg));
451275167d2cSStephen M. Cameron 	ml += sprintf(msg+ml, "ABORT REQUEST on C%d:B%d:T%d:L%d ",
451375167d2cSStephen M. Cameron 		h->scsi_host->host_no, sc->device->channel,
451475167d2cSStephen M. Cameron 		sc->device->id, sc->device->lun);
451575167d2cSStephen M. Cameron 
451675167d2cSStephen M. Cameron 	/* Find the device of the command to be aborted */
451775167d2cSStephen M. Cameron 	dev = sc->device->hostdata;
451875167d2cSStephen M. Cameron 	if (!dev) {
451975167d2cSStephen M. Cameron 		dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
452075167d2cSStephen M. Cameron 				msg);
452175167d2cSStephen M. Cameron 		return FAILED;
452275167d2cSStephen M. Cameron 	}
452375167d2cSStephen M. Cameron 
452475167d2cSStephen M. Cameron 	/* Get SCSI command to be aborted */
452575167d2cSStephen M. Cameron 	abort = (struct CommandList *) sc->host_scribble;
452675167d2cSStephen M. Cameron 	if (abort == NULL) {
452775167d2cSStephen M. Cameron 		dev_err(&h->pdev->dev, "%s FAILED, Command to abort is NULL.\n",
452875167d2cSStephen M. Cameron 				msg);
452975167d2cSStephen M. Cameron 		return FAILED;
453075167d2cSStephen M. Cameron 	}
453117eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
453217eb87d2SScott Teel 	ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
453375167d2cSStephen M. Cameron 	as  = (struct scsi_cmnd *) abort->scsi_cmd;
453475167d2cSStephen M. Cameron 	if (as != NULL)
453575167d2cSStephen M. Cameron 		ml += sprintf(msg+ml, "Command:0x%x SN:0x%lx ",
453675167d2cSStephen M. Cameron 			as->cmnd[0], as->serial_number);
453775167d2cSStephen M. Cameron 	dev_dbg(&h->pdev->dev, "%s\n", msg);
453875167d2cSStephen M. Cameron 	dev_warn(&h->pdev->dev, "Abort request on C%d:B%d:T%d:L%d\n",
453975167d2cSStephen M. Cameron 		h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
454075167d2cSStephen M. Cameron 
454175167d2cSStephen M. Cameron 	/* Search reqQ to See if command is queued but not submitted,
454275167d2cSStephen M. Cameron 	 * if so, complete the command with aborted status and remove
454375167d2cSStephen M. Cameron 	 * it from the reqQ.
454475167d2cSStephen M. Cameron 	 */
454575167d2cSStephen M. Cameron 	found = hpsa_find_cmd_in_queue(h, sc, &h->reqQ);
454675167d2cSStephen M. Cameron 	if (found) {
454775167d2cSStephen M. Cameron 		found->err_info->CommandStatus = CMD_ABORTED;
454875167d2cSStephen M. Cameron 		finish_cmd(found);
454975167d2cSStephen M. Cameron 		dev_info(&h->pdev->dev, "%s Request SUCCEEDED (driver queue).\n",
455075167d2cSStephen M. Cameron 				msg);
455175167d2cSStephen M. Cameron 		return SUCCESS;
455275167d2cSStephen M. Cameron 	}
455375167d2cSStephen M. Cameron 
455475167d2cSStephen M. Cameron 	/* not in reqQ, if also not in cmpQ, must have already completed */
455575167d2cSStephen M. Cameron 	found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
455675167d2cSStephen M. Cameron 	if (!found)  {
4557d6ebd0f7SStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s Request SUCCEEDED (not known to driver).\n",
455875167d2cSStephen M. Cameron 				msg);
455975167d2cSStephen M. Cameron 		return SUCCESS;
456075167d2cSStephen M. Cameron 	}
456175167d2cSStephen M. Cameron 
456275167d2cSStephen M. Cameron 	/*
456375167d2cSStephen M. Cameron 	 * Command is in flight, or possibly already completed
456475167d2cSStephen M. Cameron 	 * by the firmware (but not to the scsi mid layer) but we can't
456575167d2cSStephen M. Cameron 	 * distinguish which.  Send the abort down.
456675167d2cSStephen M. Cameron 	 */
45676cba3f19SStephen M. Cameron 	rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort);
456875167d2cSStephen M. Cameron 	if (rc != 0) {
456975167d2cSStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s Request FAILED.\n", msg);
457075167d2cSStephen M. Cameron 		dev_warn(&h->pdev->dev, "FAILED abort on device C%d:B%d:T%d:L%d\n",
457175167d2cSStephen M. Cameron 			h->scsi_host->host_no,
457275167d2cSStephen M. Cameron 			dev->bus, dev->target, dev->lun);
457375167d2cSStephen M. Cameron 		return FAILED;
457475167d2cSStephen M. Cameron 	}
457575167d2cSStephen M. Cameron 	dev_info(&h->pdev->dev, "%s REQUEST SUCCEEDED.\n", msg);
457675167d2cSStephen M. Cameron 
457775167d2cSStephen M. Cameron 	/* If the abort(s) above completed and actually aborted the
457875167d2cSStephen M. Cameron 	 * command, then the command to be aborted should already be
457975167d2cSStephen M. Cameron 	 * completed.  If not, wait around a bit more to see if they
458075167d2cSStephen M. Cameron 	 * manage to complete normally.
458175167d2cSStephen M. Cameron 	 */
458275167d2cSStephen M. Cameron #define ABORT_COMPLETE_WAIT_SECS 30
458375167d2cSStephen M. Cameron 	for (i = 0; i < ABORT_COMPLETE_WAIT_SECS * 10; i++) {
458475167d2cSStephen M. Cameron 		found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
458575167d2cSStephen M. Cameron 		if (!found)
458675167d2cSStephen M. Cameron 			return SUCCESS;
458775167d2cSStephen M. Cameron 		msleep(100);
458875167d2cSStephen M. Cameron 	}
458975167d2cSStephen M. Cameron 	dev_warn(&h->pdev->dev, "%s FAILED. Aborted command has not completed after %d seconds.\n",
459075167d2cSStephen M. Cameron 		msg, ABORT_COMPLETE_WAIT_SECS);
459175167d2cSStephen M. Cameron 	return FAILED;
459275167d2cSStephen M. Cameron }
459375167d2cSStephen M. Cameron 
459475167d2cSStephen M. Cameron 
4595edd16368SStephen M. Cameron /*
4596edd16368SStephen M. Cameron  * For operations that cannot sleep, a command block is allocated at init,
4597edd16368SStephen M. Cameron  * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
4598edd16368SStephen M. Cameron  * which ones are free or in use.  Lock must be held when calling this.
4599edd16368SStephen M. Cameron  * cmd_free() is the complement.
4600edd16368SStephen M. Cameron  */
4601edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h)
4602edd16368SStephen M. Cameron {
4603edd16368SStephen M. Cameron 	struct CommandList *c;
4604edd16368SStephen M. Cameron 	int i;
4605edd16368SStephen M. Cameron 	union u64bit temp64;
4606edd16368SStephen M. Cameron 	dma_addr_t cmd_dma_handle, err_dma_handle;
4607e16a33adSMatt Gates 	unsigned long flags;
4608edd16368SStephen M. Cameron 
4609e16a33adSMatt Gates 	spin_lock_irqsave(&h->lock, flags);
4610edd16368SStephen M. Cameron 	do {
4611edd16368SStephen M. Cameron 		i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
4612e16a33adSMatt Gates 		if (i == h->nr_cmds) {
4613e16a33adSMatt Gates 			spin_unlock_irqrestore(&h->lock, flags);
4614edd16368SStephen M. Cameron 			return NULL;
4615e16a33adSMatt Gates 		}
4616edd16368SStephen M. Cameron 	} while (test_and_set_bit
4617edd16368SStephen M. Cameron 		 (i & (BITS_PER_LONG - 1),
4618edd16368SStephen M. Cameron 		  h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
4619e16a33adSMatt Gates 	spin_unlock_irqrestore(&h->lock, flags);
4620e16a33adSMatt Gates 
4621edd16368SStephen M. Cameron 	c = h->cmd_pool + i;
4622edd16368SStephen M. Cameron 	memset(c, 0, sizeof(*c));
4623edd16368SStephen M. Cameron 	cmd_dma_handle = h->cmd_pool_dhandle
4624edd16368SStephen M. Cameron 	    + i * sizeof(*c);
4625edd16368SStephen M. Cameron 	c->err_info = h->errinfo_pool + i;
4626edd16368SStephen M. Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
4627edd16368SStephen M. Cameron 	err_dma_handle = h->errinfo_pool_dhandle
4628edd16368SStephen M. Cameron 	    + i * sizeof(*c->err_info);
4629edd16368SStephen M. Cameron 
4630edd16368SStephen M. Cameron 	c->cmdindex = i;
4631edd16368SStephen M. Cameron 
46329e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&c->list);
463301a02ffcSStephen M. Cameron 	c->busaddr = (u32) cmd_dma_handle;
463401a02ffcSStephen M. Cameron 	temp64.val = (u64) err_dma_handle;
4635edd16368SStephen M. Cameron 	c->ErrDesc.Addr.lower = temp64.val32.lower;
4636edd16368SStephen M. Cameron 	c->ErrDesc.Addr.upper = temp64.val32.upper;
4637edd16368SStephen M. Cameron 	c->ErrDesc.Len = sizeof(*c->err_info);
4638edd16368SStephen M. Cameron 
4639edd16368SStephen M. Cameron 	c->h = h;
4640edd16368SStephen M. Cameron 	return c;
4641edd16368SStephen M. Cameron }
4642edd16368SStephen M. Cameron 
4643edd16368SStephen M. Cameron /* For operations that can wait for kmalloc to possibly sleep,
4644edd16368SStephen M. Cameron  * this routine can be called. Lock need not be held to call
4645edd16368SStephen M. Cameron  * cmd_special_alloc. cmd_special_free() is the complement.
4646edd16368SStephen M. Cameron  */
4647edd16368SStephen M. Cameron static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
4648edd16368SStephen M. Cameron {
4649edd16368SStephen M. Cameron 	struct CommandList *c;
4650edd16368SStephen M. Cameron 	union u64bit temp64;
4651edd16368SStephen M. Cameron 	dma_addr_t cmd_dma_handle, err_dma_handle;
4652edd16368SStephen M. Cameron 
4653edd16368SStephen M. Cameron 	c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
4654edd16368SStephen M. Cameron 	if (c == NULL)
4655edd16368SStephen M. Cameron 		return NULL;
4656edd16368SStephen M. Cameron 	memset(c, 0, sizeof(*c));
4657edd16368SStephen M. Cameron 
4658e1f7de0cSMatt Gates 	c->cmd_type = CMD_SCSI;
4659edd16368SStephen M. Cameron 	c->cmdindex = -1;
4660edd16368SStephen M. Cameron 
4661edd16368SStephen M. Cameron 	c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info),
4662edd16368SStephen M. Cameron 		    &err_dma_handle);
4663edd16368SStephen M. Cameron 
4664edd16368SStephen M. Cameron 	if (c->err_info == NULL) {
4665edd16368SStephen M. Cameron 		pci_free_consistent(h->pdev,
4666edd16368SStephen M. Cameron 			sizeof(*c), c, cmd_dma_handle);
4667edd16368SStephen M. Cameron 		return NULL;
4668edd16368SStephen M. Cameron 	}
4669edd16368SStephen M. Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
4670edd16368SStephen M. Cameron 
46719e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&c->list);
467201a02ffcSStephen M. Cameron 	c->busaddr = (u32) cmd_dma_handle;
467301a02ffcSStephen M. Cameron 	temp64.val = (u64) err_dma_handle;
4674edd16368SStephen M. Cameron 	c->ErrDesc.Addr.lower = temp64.val32.lower;
4675edd16368SStephen M. Cameron 	c->ErrDesc.Addr.upper = temp64.val32.upper;
4676edd16368SStephen M. Cameron 	c->ErrDesc.Len = sizeof(*c->err_info);
4677edd16368SStephen M. Cameron 
4678edd16368SStephen M. Cameron 	c->h = h;
4679edd16368SStephen M. Cameron 	return c;
4680edd16368SStephen M. Cameron }
4681edd16368SStephen M. Cameron 
4682edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c)
4683edd16368SStephen M. Cameron {
4684edd16368SStephen M. Cameron 	int i;
4685e16a33adSMatt Gates 	unsigned long flags;
4686edd16368SStephen M. Cameron 
4687edd16368SStephen M. Cameron 	i = c - h->cmd_pool;
4688e16a33adSMatt Gates 	spin_lock_irqsave(&h->lock, flags);
4689edd16368SStephen M. Cameron 	clear_bit(i & (BITS_PER_LONG - 1),
4690edd16368SStephen M. Cameron 		  h->cmd_pool_bits + (i / BITS_PER_LONG));
4691e16a33adSMatt Gates 	spin_unlock_irqrestore(&h->lock, flags);
4692edd16368SStephen M. Cameron }
4693edd16368SStephen M. Cameron 
4694edd16368SStephen M. Cameron static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
4695edd16368SStephen M. Cameron {
4696edd16368SStephen M. Cameron 	union u64bit temp64;
4697edd16368SStephen M. Cameron 
4698edd16368SStephen M. Cameron 	temp64.val32.lower = c->ErrDesc.Addr.lower;
4699edd16368SStephen M. Cameron 	temp64.val32.upper = c->ErrDesc.Addr.upper;
4700edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev, sizeof(*c->err_info),
4701edd16368SStephen M. Cameron 			    c->err_info, (dma_addr_t) temp64.val);
4702edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev, sizeof(*c),
4703d896f3f3SStephen M. Cameron 			    c, (dma_addr_t) (c->busaddr & DIRECT_LOOKUP_MASK));
4704edd16368SStephen M. Cameron }
4705edd16368SStephen M. Cameron 
4706edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
4707edd16368SStephen M. Cameron 
4708edd16368SStephen M. Cameron static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
4709edd16368SStephen M. Cameron {
4710edd16368SStephen M. Cameron 	IOCTL32_Command_struct __user *arg32 =
4711edd16368SStephen M. Cameron 	    (IOCTL32_Command_struct __user *) arg;
4712edd16368SStephen M. Cameron 	IOCTL_Command_struct arg64;
4713edd16368SStephen M. Cameron 	IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
4714edd16368SStephen M. Cameron 	int err;
4715edd16368SStephen M. Cameron 	u32 cp;
4716edd16368SStephen M. Cameron 
4717938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
4718edd16368SStephen M. Cameron 	err = 0;
4719edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
4720edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
4721edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
4722edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
4723edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
4724edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
4725edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
4726edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
4727edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
4728edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
4729edd16368SStephen M. Cameron 
4730edd16368SStephen M. Cameron 	if (err)
4731edd16368SStephen M. Cameron 		return -EFAULT;
4732edd16368SStephen M. Cameron 
4733e39eeaedSStephen M. Cameron 	err = hpsa_ioctl(dev, CCISS_PASSTHRU, (void *)p);
4734edd16368SStephen M. Cameron 	if (err)
4735edd16368SStephen M. Cameron 		return err;
4736edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
4737edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
4738edd16368SStephen M. Cameron 	if (err)
4739edd16368SStephen M. Cameron 		return -EFAULT;
4740edd16368SStephen M. Cameron 	return err;
4741edd16368SStephen M. Cameron }
4742edd16368SStephen M. Cameron 
4743edd16368SStephen M. Cameron static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
4744edd16368SStephen M. Cameron 	int cmd, void *arg)
4745edd16368SStephen M. Cameron {
4746edd16368SStephen M. Cameron 	BIG_IOCTL32_Command_struct __user *arg32 =
4747edd16368SStephen M. Cameron 	    (BIG_IOCTL32_Command_struct __user *) arg;
4748edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct arg64;
4749edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct __user *p =
4750edd16368SStephen M. Cameron 	    compat_alloc_user_space(sizeof(arg64));
4751edd16368SStephen M. Cameron 	int err;
4752edd16368SStephen M. Cameron 	u32 cp;
4753edd16368SStephen M. Cameron 
4754938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
4755edd16368SStephen M. Cameron 	err = 0;
4756edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
4757edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
4758edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
4759edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
4760edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
4761edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
4762edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
4763edd16368SStephen M. Cameron 	err |= get_user(arg64.malloc_size, &arg32->malloc_size);
4764edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
4765edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
4766edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
4767edd16368SStephen M. Cameron 
4768edd16368SStephen M. Cameron 	if (err)
4769edd16368SStephen M. Cameron 		return -EFAULT;
4770edd16368SStephen M. Cameron 
4771e39eeaedSStephen M. Cameron 	err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
4772edd16368SStephen M. Cameron 	if (err)
4773edd16368SStephen M. Cameron 		return err;
4774edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
4775edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
4776edd16368SStephen M. Cameron 	if (err)
4777edd16368SStephen M. Cameron 		return -EFAULT;
4778edd16368SStephen M. Cameron 	return err;
4779edd16368SStephen M. Cameron }
478071fe75a7SStephen M. Cameron 
478171fe75a7SStephen M. Cameron static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
478271fe75a7SStephen M. Cameron {
478371fe75a7SStephen M. Cameron 	switch (cmd) {
478471fe75a7SStephen M. Cameron 	case CCISS_GETPCIINFO:
478571fe75a7SStephen M. Cameron 	case CCISS_GETINTINFO:
478671fe75a7SStephen M. Cameron 	case CCISS_SETINTINFO:
478771fe75a7SStephen M. Cameron 	case CCISS_GETNODENAME:
478871fe75a7SStephen M. Cameron 	case CCISS_SETNODENAME:
478971fe75a7SStephen M. Cameron 	case CCISS_GETHEARTBEAT:
479071fe75a7SStephen M. Cameron 	case CCISS_GETBUSTYPES:
479171fe75a7SStephen M. Cameron 	case CCISS_GETFIRMVER:
479271fe75a7SStephen M. Cameron 	case CCISS_GETDRIVVER:
479371fe75a7SStephen M. Cameron 	case CCISS_REVALIDVOLS:
479471fe75a7SStephen M. Cameron 	case CCISS_DEREGDISK:
479571fe75a7SStephen M. Cameron 	case CCISS_REGNEWDISK:
479671fe75a7SStephen M. Cameron 	case CCISS_REGNEWD:
479771fe75a7SStephen M. Cameron 	case CCISS_RESCANDISK:
479871fe75a7SStephen M. Cameron 	case CCISS_GETLUNINFO:
479971fe75a7SStephen M. Cameron 		return hpsa_ioctl(dev, cmd, arg);
480071fe75a7SStephen M. Cameron 
480171fe75a7SStephen M. Cameron 	case CCISS_PASSTHRU32:
480271fe75a7SStephen M. Cameron 		return hpsa_ioctl32_passthru(dev, cmd, arg);
480371fe75a7SStephen M. Cameron 	case CCISS_BIG_PASSTHRU32:
480471fe75a7SStephen M. Cameron 		return hpsa_ioctl32_big_passthru(dev, cmd, arg);
480571fe75a7SStephen M. Cameron 
480671fe75a7SStephen M. Cameron 	default:
480771fe75a7SStephen M. Cameron 		return -ENOIOCTLCMD;
480871fe75a7SStephen M. Cameron 	}
480971fe75a7SStephen M. Cameron }
4810edd16368SStephen M. Cameron #endif
4811edd16368SStephen M. Cameron 
4812edd16368SStephen M. Cameron static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
4813edd16368SStephen M. Cameron {
4814edd16368SStephen M. Cameron 	struct hpsa_pci_info pciinfo;
4815edd16368SStephen M. Cameron 
4816edd16368SStephen M. Cameron 	if (!argp)
4817edd16368SStephen M. Cameron 		return -EINVAL;
4818edd16368SStephen M. Cameron 	pciinfo.domain = pci_domain_nr(h->pdev->bus);
4819edd16368SStephen M. Cameron 	pciinfo.bus = h->pdev->bus->number;
4820edd16368SStephen M. Cameron 	pciinfo.dev_fn = h->pdev->devfn;
4821edd16368SStephen M. Cameron 	pciinfo.board_id = h->board_id;
4822edd16368SStephen M. Cameron 	if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
4823edd16368SStephen M. Cameron 		return -EFAULT;
4824edd16368SStephen M. Cameron 	return 0;
4825edd16368SStephen M. Cameron }
4826edd16368SStephen M. Cameron 
4827edd16368SStephen M. Cameron static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
4828edd16368SStephen M. Cameron {
4829edd16368SStephen M. Cameron 	DriverVer_type DriverVer;
4830edd16368SStephen M. Cameron 	unsigned char vmaj, vmin, vsubmin;
4831edd16368SStephen M. Cameron 	int rc;
4832edd16368SStephen M. Cameron 
4833edd16368SStephen M. Cameron 	rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
4834edd16368SStephen M. Cameron 		&vmaj, &vmin, &vsubmin);
4835edd16368SStephen M. Cameron 	if (rc != 3) {
4836edd16368SStephen M. Cameron 		dev_info(&h->pdev->dev, "driver version string '%s' "
4837edd16368SStephen M. Cameron 			"unrecognized.", HPSA_DRIVER_VERSION);
4838edd16368SStephen M. Cameron 		vmaj = 0;
4839edd16368SStephen M. Cameron 		vmin = 0;
4840edd16368SStephen M. Cameron 		vsubmin = 0;
4841edd16368SStephen M. Cameron 	}
4842edd16368SStephen M. Cameron 	DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
4843edd16368SStephen M. Cameron 	if (!argp)
4844edd16368SStephen M. Cameron 		return -EINVAL;
4845edd16368SStephen M. Cameron 	if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
4846edd16368SStephen M. Cameron 		return -EFAULT;
4847edd16368SStephen M. Cameron 	return 0;
4848edd16368SStephen M. Cameron }
4849edd16368SStephen M. Cameron 
4850edd16368SStephen M. Cameron static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
4851edd16368SStephen M. Cameron {
4852edd16368SStephen M. Cameron 	IOCTL_Command_struct iocommand;
4853edd16368SStephen M. Cameron 	struct CommandList *c;
4854edd16368SStephen M. Cameron 	char *buff = NULL;
4855edd16368SStephen M. Cameron 	union u64bit temp64;
4856c1f63c8fSStephen M. Cameron 	int rc = 0;
4857edd16368SStephen M. Cameron 
4858edd16368SStephen M. Cameron 	if (!argp)
4859edd16368SStephen M. Cameron 		return -EINVAL;
4860edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
4861edd16368SStephen M. Cameron 		return -EPERM;
4862edd16368SStephen M. Cameron 	if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
4863edd16368SStephen M. Cameron 		return -EFAULT;
4864edd16368SStephen M. Cameron 	if ((iocommand.buf_size < 1) &&
4865edd16368SStephen M. Cameron 	    (iocommand.Request.Type.Direction != XFER_NONE)) {
4866edd16368SStephen M. Cameron 		return -EINVAL;
4867edd16368SStephen M. Cameron 	}
4868edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
4869edd16368SStephen M. Cameron 		buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
4870edd16368SStephen M. Cameron 		if (buff == NULL)
4871edd16368SStephen M. Cameron 			return -EFAULT;
4872edd16368SStephen M. Cameron 		if (iocommand.Request.Type.Direction == XFER_WRITE) {
4873edd16368SStephen M. Cameron 			/* Copy the data into the buffer we created */
4874b03a7771SStephen M. Cameron 			if (copy_from_user(buff, iocommand.buf,
4875b03a7771SStephen M. Cameron 				iocommand.buf_size)) {
4876c1f63c8fSStephen M. Cameron 				rc = -EFAULT;
4877c1f63c8fSStephen M. Cameron 				goto out_kfree;
4878edd16368SStephen M. Cameron 			}
4879b03a7771SStephen M. Cameron 		} else {
4880edd16368SStephen M. Cameron 			memset(buff, 0, iocommand.buf_size);
4881b03a7771SStephen M. Cameron 		}
4882b03a7771SStephen M. Cameron 	}
4883edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
4884edd16368SStephen M. Cameron 	if (c == NULL) {
4885c1f63c8fSStephen M. Cameron 		rc = -ENOMEM;
4886c1f63c8fSStephen M. Cameron 		goto out_kfree;
4887edd16368SStephen M. Cameron 	}
4888edd16368SStephen M. Cameron 	/* Fill in the command type */
4889edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
4890edd16368SStephen M. Cameron 	/* Fill in Command Header */
4891edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0; /* unused in simple mode */
4892edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {	/* buffer to fill */
4893edd16368SStephen M. Cameron 		c->Header.SGList = 1;
4894edd16368SStephen M. Cameron 		c->Header.SGTotal = 1;
4895edd16368SStephen M. Cameron 	} else	{ /* no buffers to fill */
4896edd16368SStephen M. Cameron 		c->Header.SGList = 0;
4897edd16368SStephen M. Cameron 		c->Header.SGTotal = 0;
4898edd16368SStephen M. Cameron 	}
4899edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
4900edd16368SStephen M. Cameron 	/* use the kernel address the cmd block for tag */
4901edd16368SStephen M. Cameron 	c->Header.Tag.lower = c->busaddr;
4902edd16368SStephen M. Cameron 
4903edd16368SStephen M. Cameron 	/* Fill in Request block */
4904edd16368SStephen M. Cameron 	memcpy(&c->Request, &iocommand.Request,
4905edd16368SStephen M. Cameron 		sizeof(c->Request));
4906edd16368SStephen M. Cameron 
4907edd16368SStephen M. Cameron 	/* Fill in the scatter gather information */
4908edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
4909edd16368SStephen M. Cameron 		temp64.val = pci_map_single(h->pdev, buff,
4910edd16368SStephen M. Cameron 			iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
4911bcc48ffaSStephen M. Cameron 		if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
4912bcc48ffaSStephen M. Cameron 			c->SG[0].Addr.lower = 0;
4913bcc48ffaSStephen M. Cameron 			c->SG[0].Addr.upper = 0;
4914bcc48ffaSStephen M. Cameron 			c->SG[0].Len = 0;
4915bcc48ffaSStephen M. Cameron 			rc = -ENOMEM;
4916bcc48ffaSStephen M. Cameron 			goto out;
4917bcc48ffaSStephen M. Cameron 		}
4918edd16368SStephen M. Cameron 		c->SG[0].Addr.lower = temp64.val32.lower;
4919edd16368SStephen M. Cameron 		c->SG[0].Addr.upper = temp64.val32.upper;
4920edd16368SStephen M. Cameron 		c->SG[0].Len = iocommand.buf_size;
4921e1d9cbfaSMatt Gates 		c->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining*/
4922edd16368SStephen M. Cameron 	}
4923a0c12413SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
4924c2dd32e0SStephen M. Cameron 	if (iocommand.buf_size > 0)
4925edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
4926edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
4927edd16368SStephen M. Cameron 
4928edd16368SStephen M. Cameron 	/* Copy the error information out */
4929edd16368SStephen M. Cameron 	memcpy(&iocommand.error_info, c->err_info,
4930edd16368SStephen M. Cameron 		sizeof(iocommand.error_info));
4931edd16368SStephen M. Cameron 	if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
4932c1f63c8fSStephen M. Cameron 		rc = -EFAULT;
4933c1f63c8fSStephen M. Cameron 		goto out;
4934edd16368SStephen M. Cameron 	}
4935b03a7771SStephen M. Cameron 	if (iocommand.Request.Type.Direction == XFER_READ &&
4936b03a7771SStephen M. Cameron 		iocommand.buf_size > 0) {
4937edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
4938edd16368SStephen M. Cameron 		if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
4939c1f63c8fSStephen M. Cameron 			rc = -EFAULT;
4940c1f63c8fSStephen M. Cameron 			goto out;
4941edd16368SStephen M. Cameron 		}
4942edd16368SStephen M. Cameron 	}
4943c1f63c8fSStephen M. Cameron out:
4944edd16368SStephen M. Cameron 	cmd_special_free(h, c);
4945c1f63c8fSStephen M. Cameron out_kfree:
4946c1f63c8fSStephen M. Cameron 	kfree(buff);
4947c1f63c8fSStephen M. Cameron 	return rc;
4948edd16368SStephen M. Cameron }
4949edd16368SStephen M. Cameron 
4950edd16368SStephen M. Cameron static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
4951edd16368SStephen M. Cameron {
4952edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct *ioc;
4953edd16368SStephen M. Cameron 	struct CommandList *c;
4954edd16368SStephen M. Cameron 	unsigned char **buff = NULL;
4955edd16368SStephen M. Cameron 	int *buff_size = NULL;
4956edd16368SStephen M. Cameron 	union u64bit temp64;
4957edd16368SStephen M. Cameron 	BYTE sg_used = 0;
4958edd16368SStephen M. Cameron 	int status = 0;
4959edd16368SStephen M. Cameron 	int i;
496001a02ffcSStephen M. Cameron 	u32 left;
496101a02ffcSStephen M. Cameron 	u32 sz;
4962edd16368SStephen M. Cameron 	BYTE __user *data_ptr;
4963edd16368SStephen M. Cameron 
4964edd16368SStephen M. Cameron 	if (!argp)
4965edd16368SStephen M. Cameron 		return -EINVAL;
4966edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
4967edd16368SStephen M. Cameron 		return -EPERM;
4968edd16368SStephen M. Cameron 	ioc = (BIG_IOCTL_Command_struct *)
4969edd16368SStephen M. Cameron 	    kmalloc(sizeof(*ioc), GFP_KERNEL);
4970edd16368SStephen M. Cameron 	if (!ioc) {
4971edd16368SStephen M. Cameron 		status = -ENOMEM;
4972edd16368SStephen M. Cameron 		goto cleanup1;
4973edd16368SStephen M. Cameron 	}
4974edd16368SStephen M. Cameron 	if (copy_from_user(ioc, argp, sizeof(*ioc))) {
4975edd16368SStephen M. Cameron 		status = -EFAULT;
4976edd16368SStephen M. Cameron 		goto cleanup1;
4977edd16368SStephen M. Cameron 	}
4978edd16368SStephen M. Cameron 	if ((ioc->buf_size < 1) &&
4979edd16368SStephen M. Cameron 	    (ioc->Request.Type.Direction != XFER_NONE)) {
4980edd16368SStephen M. Cameron 		status = -EINVAL;
4981edd16368SStephen M. Cameron 		goto cleanup1;
4982edd16368SStephen M. Cameron 	}
4983edd16368SStephen M. Cameron 	/* Check kmalloc limits  using all SGs */
4984edd16368SStephen M. Cameron 	if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
4985edd16368SStephen M. Cameron 		status = -EINVAL;
4986edd16368SStephen M. Cameron 		goto cleanup1;
4987edd16368SStephen M. Cameron 	}
4988d66ae08bSStephen M. Cameron 	if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
4989edd16368SStephen M. Cameron 		status = -EINVAL;
4990edd16368SStephen M. Cameron 		goto cleanup1;
4991edd16368SStephen M. Cameron 	}
4992d66ae08bSStephen M. Cameron 	buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
4993edd16368SStephen M. Cameron 	if (!buff) {
4994edd16368SStephen M. Cameron 		status = -ENOMEM;
4995edd16368SStephen M. Cameron 		goto cleanup1;
4996edd16368SStephen M. Cameron 	}
4997d66ae08bSStephen M. Cameron 	buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
4998edd16368SStephen M. Cameron 	if (!buff_size) {
4999edd16368SStephen M. Cameron 		status = -ENOMEM;
5000edd16368SStephen M. Cameron 		goto cleanup1;
5001edd16368SStephen M. Cameron 	}
5002edd16368SStephen M. Cameron 	left = ioc->buf_size;
5003edd16368SStephen M. Cameron 	data_ptr = ioc->buf;
5004edd16368SStephen M. Cameron 	while (left) {
5005edd16368SStephen M. Cameron 		sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
5006edd16368SStephen M. Cameron 		buff_size[sg_used] = sz;
5007edd16368SStephen M. Cameron 		buff[sg_used] = kmalloc(sz, GFP_KERNEL);
5008edd16368SStephen M. Cameron 		if (buff[sg_used] == NULL) {
5009edd16368SStephen M. Cameron 			status = -ENOMEM;
5010edd16368SStephen M. Cameron 			goto cleanup1;
5011edd16368SStephen M. Cameron 		}
5012edd16368SStephen M. Cameron 		if (ioc->Request.Type.Direction == XFER_WRITE) {
5013edd16368SStephen M. Cameron 			if (copy_from_user(buff[sg_used], data_ptr, sz)) {
5014edd16368SStephen M. Cameron 				status = -ENOMEM;
5015edd16368SStephen M. Cameron 				goto cleanup1;
5016edd16368SStephen M. Cameron 			}
5017edd16368SStephen M. Cameron 		} else
5018edd16368SStephen M. Cameron 			memset(buff[sg_used], 0, sz);
5019edd16368SStephen M. Cameron 		left -= sz;
5020edd16368SStephen M. Cameron 		data_ptr += sz;
5021edd16368SStephen M. Cameron 		sg_used++;
5022edd16368SStephen M. Cameron 	}
5023edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
5024edd16368SStephen M. Cameron 	if (c == NULL) {
5025edd16368SStephen M. Cameron 		status = -ENOMEM;
5026edd16368SStephen M. Cameron 		goto cleanup1;
5027edd16368SStephen M. Cameron 	}
5028edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5029edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
5030b03a7771SStephen M. Cameron 	c->Header.SGList = c->Header.SGTotal = sg_used;
5031edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
5032edd16368SStephen M. Cameron 	c->Header.Tag.lower = c->busaddr;
5033edd16368SStephen M. Cameron 	memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
5034edd16368SStephen M. Cameron 	if (ioc->buf_size > 0) {
5035edd16368SStephen M. Cameron 		int i;
5036edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
5037edd16368SStephen M. Cameron 			temp64.val = pci_map_single(h->pdev, buff[i],
5038edd16368SStephen M. Cameron 				    buff_size[i], PCI_DMA_BIDIRECTIONAL);
5039bcc48ffaSStephen M. Cameron 			if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
5040bcc48ffaSStephen M. Cameron 				c->SG[i].Addr.lower = 0;
5041bcc48ffaSStephen M. Cameron 				c->SG[i].Addr.upper = 0;
5042bcc48ffaSStephen M. Cameron 				c->SG[i].Len = 0;
5043bcc48ffaSStephen M. Cameron 				hpsa_pci_unmap(h->pdev, c, i,
5044bcc48ffaSStephen M. Cameron 					PCI_DMA_BIDIRECTIONAL);
5045bcc48ffaSStephen M. Cameron 				status = -ENOMEM;
5046e2d4a1f6SStephen M. Cameron 				goto cleanup0;
5047bcc48ffaSStephen M. Cameron 			}
5048edd16368SStephen M. Cameron 			c->SG[i].Addr.lower = temp64.val32.lower;
5049edd16368SStephen M. Cameron 			c->SG[i].Addr.upper = temp64.val32.upper;
5050edd16368SStephen M. Cameron 			c->SG[i].Len = buff_size[i];
5051e1d9cbfaSMatt Gates 			c->SG[i].Ext = i < sg_used - 1 ? 0 : HPSA_SG_LAST;
5052edd16368SStephen M. Cameron 		}
5053edd16368SStephen M. Cameron 	}
5054a0c12413SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
5055b03a7771SStephen M. Cameron 	if (sg_used)
5056edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
5057edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
5058edd16368SStephen M. Cameron 	/* Copy the error information out */
5059edd16368SStephen M. Cameron 	memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
5060edd16368SStephen M. Cameron 	if (copy_to_user(argp, ioc, sizeof(*ioc))) {
5061edd16368SStephen M. Cameron 		status = -EFAULT;
5062e2d4a1f6SStephen M. Cameron 		goto cleanup0;
5063edd16368SStephen M. Cameron 	}
5064b03a7771SStephen M. Cameron 	if (ioc->Request.Type.Direction == XFER_READ && ioc->buf_size > 0) {
5065edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
5066edd16368SStephen M. Cameron 		BYTE __user *ptr = ioc->buf;
5067edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
5068edd16368SStephen M. Cameron 			if (copy_to_user(ptr, buff[i], buff_size[i])) {
5069edd16368SStephen M. Cameron 				status = -EFAULT;
5070e2d4a1f6SStephen M. Cameron 				goto cleanup0;
5071edd16368SStephen M. Cameron 			}
5072edd16368SStephen M. Cameron 			ptr += buff_size[i];
5073edd16368SStephen M. Cameron 		}
5074edd16368SStephen M. Cameron 	}
5075edd16368SStephen M. Cameron 	status = 0;
5076e2d4a1f6SStephen M. Cameron cleanup0:
5077e2d4a1f6SStephen M. Cameron 	cmd_special_free(h, c);
5078edd16368SStephen M. Cameron cleanup1:
5079edd16368SStephen M. Cameron 	if (buff) {
5080edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++)
5081edd16368SStephen M. Cameron 			kfree(buff[i]);
5082edd16368SStephen M. Cameron 		kfree(buff);
5083edd16368SStephen M. Cameron 	}
5084edd16368SStephen M. Cameron 	kfree(buff_size);
5085edd16368SStephen M. Cameron 	kfree(ioc);
5086edd16368SStephen M. Cameron 	return status;
5087edd16368SStephen M. Cameron }
5088edd16368SStephen M. Cameron 
5089edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
5090edd16368SStephen M. Cameron 	struct CommandList *c)
5091edd16368SStephen M. Cameron {
5092edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
5093edd16368SStephen M. Cameron 			c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
5094edd16368SStephen M. Cameron 		(void) check_for_unit_attention(h, c);
5095edd16368SStephen M. Cameron }
50960390f0c0SStephen M. Cameron 
50970390f0c0SStephen M. Cameron static int increment_passthru_count(struct ctlr_info *h)
50980390f0c0SStephen M. Cameron {
50990390f0c0SStephen M. Cameron 	unsigned long flags;
51000390f0c0SStephen M. Cameron 
51010390f0c0SStephen M. Cameron 	spin_lock_irqsave(&h->passthru_count_lock, flags);
51020390f0c0SStephen M. Cameron 	if (h->passthru_count >= HPSA_MAX_CONCURRENT_PASSTHRUS) {
51030390f0c0SStephen M. Cameron 		spin_unlock_irqrestore(&h->passthru_count_lock, flags);
51040390f0c0SStephen M. Cameron 		return -1;
51050390f0c0SStephen M. Cameron 	}
51060390f0c0SStephen M. Cameron 	h->passthru_count++;
51070390f0c0SStephen M. Cameron 	spin_unlock_irqrestore(&h->passthru_count_lock, flags);
51080390f0c0SStephen M. Cameron 	return 0;
51090390f0c0SStephen M. Cameron }
51100390f0c0SStephen M. Cameron 
51110390f0c0SStephen M. Cameron static void decrement_passthru_count(struct ctlr_info *h)
51120390f0c0SStephen M. Cameron {
51130390f0c0SStephen M. Cameron 	unsigned long flags;
51140390f0c0SStephen M. Cameron 
51150390f0c0SStephen M. Cameron 	spin_lock_irqsave(&h->passthru_count_lock, flags);
51160390f0c0SStephen M. Cameron 	if (h->passthru_count <= 0) {
51170390f0c0SStephen M. Cameron 		spin_unlock_irqrestore(&h->passthru_count_lock, flags);
51180390f0c0SStephen M. Cameron 		/* not expecting to get here. */
51190390f0c0SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Bug detected, passthru_count seems to be incorrect.\n");
51200390f0c0SStephen M. Cameron 		return;
51210390f0c0SStephen M. Cameron 	}
51220390f0c0SStephen M. Cameron 	h->passthru_count--;
51230390f0c0SStephen M. Cameron 	spin_unlock_irqrestore(&h->passthru_count_lock, flags);
51240390f0c0SStephen M. Cameron }
51250390f0c0SStephen M. Cameron 
5126edd16368SStephen M. Cameron /*
5127edd16368SStephen M. Cameron  * ioctl
5128edd16368SStephen M. Cameron  */
5129edd16368SStephen M. Cameron static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
5130edd16368SStephen M. Cameron {
5131edd16368SStephen M. Cameron 	struct ctlr_info *h;
5132edd16368SStephen M. Cameron 	void __user *argp = (void __user *)arg;
51330390f0c0SStephen M. Cameron 	int rc;
5134edd16368SStephen M. Cameron 
5135edd16368SStephen M. Cameron 	h = sdev_to_hba(dev);
5136edd16368SStephen M. Cameron 
5137edd16368SStephen M. Cameron 	switch (cmd) {
5138edd16368SStephen M. Cameron 	case CCISS_DEREGDISK:
5139edd16368SStephen M. Cameron 	case CCISS_REGNEWDISK:
5140edd16368SStephen M. Cameron 	case CCISS_REGNEWD:
5141a08a8471SStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
5142edd16368SStephen M. Cameron 		return 0;
5143edd16368SStephen M. Cameron 	case CCISS_GETPCIINFO:
5144edd16368SStephen M. Cameron 		return hpsa_getpciinfo_ioctl(h, argp);
5145edd16368SStephen M. Cameron 	case CCISS_GETDRIVVER:
5146edd16368SStephen M. Cameron 		return hpsa_getdrivver_ioctl(h, argp);
5147edd16368SStephen M. Cameron 	case CCISS_PASSTHRU:
51480390f0c0SStephen M. Cameron 		if (increment_passthru_count(h))
51490390f0c0SStephen M. Cameron 			return -EAGAIN;
51500390f0c0SStephen M. Cameron 		rc = hpsa_passthru_ioctl(h, argp);
51510390f0c0SStephen M. Cameron 		decrement_passthru_count(h);
51520390f0c0SStephen M. Cameron 		return rc;
5153edd16368SStephen M. Cameron 	case CCISS_BIG_PASSTHRU:
51540390f0c0SStephen M. Cameron 		if (increment_passthru_count(h))
51550390f0c0SStephen M. Cameron 			return -EAGAIN;
51560390f0c0SStephen M. Cameron 		rc = hpsa_big_passthru_ioctl(h, argp);
51570390f0c0SStephen M. Cameron 		decrement_passthru_count(h);
51580390f0c0SStephen M. Cameron 		return rc;
5159edd16368SStephen M. Cameron 	default:
5160edd16368SStephen M. Cameron 		return -ENOTTY;
5161edd16368SStephen M. Cameron 	}
5162edd16368SStephen M. Cameron }
5163edd16368SStephen M. Cameron 
51646f039790SGreg Kroah-Hartman static int hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
51656f039790SGreg Kroah-Hartman 				u8 reset_type)
516664670ac8SStephen M. Cameron {
516764670ac8SStephen M. Cameron 	struct CommandList *c;
516864670ac8SStephen M. Cameron 
516964670ac8SStephen M. Cameron 	c = cmd_alloc(h);
517064670ac8SStephen M. Cameron 	if (!c)
517164670ac8SStephen M. Cameron 		return -ENOMEM;
5172a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map */
5173a2dac136SStephen M. Cameron 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
517464670ac8SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_MSG);
517564670ac8SStephen M. Cameron 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
517664670ac8SStephen M. Cameron 	c->waiting = NULL;
517764670ac8SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
517864670ac8SStephen M. Cameron 	/* Don't wait for completion, the reset won't complete.  Don't free
517964670ac8SStephen M. Cameron 	 * the command either.  This is the last command we will send before
518064670ac8SStephen M. Cameron 	 * re-initializing everything, so it doesn't matter and won't leak.
518164670ac8SStephen M. Cameron 	 */
518264670ac8SStephen M. Cameron 	return 0;
518364670ac8SStephen M. Cameron }
518464670ac8SStephen M. Cameron 
5185a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
5186b7bb24ebSStephen M. Cameron 	void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
5187edd16368SStephen M. Cameron 	int cmd_type)
5188edd16368SStephen M. Cameron {
5189edd16368SStephen M. Cameron 	int pci_dir = XFER_NONE;
519075167d2cSStephen M. Cameron 	struct CommandList *a; /* for commands to be aborted */
5191edd16368SStephen M. Cameron 
5192edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5193edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
5194edd16368SStephen M. Cameron 	if (buff != NULL && size > 0) {
5195edd16368SStephen M. Cameron 		c->Header.SGList = 1;
5196edd16368SStephen M. Cameron 		c->Header.SGTotal = 1;
5197edd16368SStephen M. Cameron 	} else {
5198edd16368SStephen M. Cameron 		c->Header.SGList = 0;
5199edd16368SStephen M. Cameron 		c->Header.SGTotal = 0;
5200edd16368SStephen M. Cameron 	}
5201edd16368SStephen M. Cameron 	c->Header.Tag.lower = c->busaddr;
5202edd16368SStephen M. Cameron 	memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
5203edd16368SStephen M. Cameron 
5204edd16368SStephen M. Cameron 	c->Request.Type.Type = cmd_type;
5205edd16368SStephen M. Cameron 	if (cmd_type == TYPE_CMD) {
5206edd16368SStephen M. Cameron 		switch (cmd) {
5207edd16368SStephen M. Cameron 		case HPSA_INQUIRY:
5208edd16368SStephen M. Cameron 			/* are we trying to read a vital product page */
5209b7bb24ebSStephen M. Cameron 			if (page_code & VPD_PAGE) {
5210edd16368SStephen M. Cameron 				c->Request.CDB[1] = 0x01;
5211b7bb24ebSStephen M. Cameron 				c->Request.CDB[2] = (page_code & 0xff);
5212edd16368SStephen M. Cameron 			}
5213edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
5214edd16368SStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
5215edd16368SStephen M. Cameron 			c->Request.Type.Direction = XFER_READ;
5216edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5217edd16368SStephen M. Cameron 			c->Request.CDB[0] = HPSA_INQUIRY;
5218edd16368SStephen M. Cameron 			c->Request.CDB[4] = size & 0xFF;
5219edd16368SStephen M. Cameron 			break;
5220edd16368SStephen M. Cameron 		case HPSA_REPORT_LOG:
5221edd16368SStephen M. Cameron 		case HPSA_REPORT_PHYS:
5222edd16368SStephen M. Cameron 			/* Talking to controller so It's a physical command
5223edd16368SStephen M. Cameron 			   mode = 00 target = 0.  Nothing to write.
5224edd16368SStephen M. Cameron 			 */
5225edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
5226edd16368SStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
5227edd16368SStephen M. Cameron 			c->Request.Type.Direction = XFER_READ;
5228edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5229edd16368SStephen M. Cameron 			c->Request.CDB[0] = cmd;
5230edd16368SStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
5231edd16368SStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5232edd16368SStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5233edd16368SStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
5234edd16368SStephen M. Cameron 			break;
5235edd16368SStephen M. Cameron 		case HPSA_CACHE_FLUSH:
5236edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
5237edd16368SStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
5238edd16368SStephen M. Cameron 			c->Request.Type.Direction = XFER_WRITE;
5239edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5240edd16368SStephen M. Cameron 			c->Request.CDB[0] = BMIC_WRITE;
5241edd16368SStephen M. Cameron 			c->Request.CDB[6] = BMIC_CACHE_FLUSH;
5242bb158eabSStephen M. Cameron 			c->Request.CDB[7] = (size >> 8) & 0xFF;
5243bb158eabSStephen M. Cameron 			c->Request.CDB[8] = size & 0xFF;
5244edd16368SStephen M. Cameron 			break;
5245edd16368SStephen M. Cameron 		case TEST_UNIT_READY:
5246edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
5247edd16368SStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
5248edd16368SStephen M. Cameron 			c->Request.Type.Direction = XFER_NONE;
5249edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5250edd16368SStephen M. Cameron 			break;
5251283b4a9bSStephen M. Cameron 		case HPSA_GET_RAID_MAP:
5252283b4a9bSStephen M. Cameron 			c->Request.CDBLen = 12;
5253283b4a9bSStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
5254283b4a9bSStephen M. Cameron 			c->Request.Type.Direction = XFER_READ;
5255283b4a9bSStephen M. Cameron 			c->Request.Timeout = 0;
5256283b4a9bSStephen M. Cameron 			c->Request.CDB[0] = HPSA_CISS_READ;
5257283b4a9bSStephen M. Cameron 			c->Request.CDB[1] = cmd;
5258283b4a9bSStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
5259283b4a9bSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5260283b4a9bSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5261283b4a9bSStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
5262283b4a9bSStephen M. Cameron 			break;
5263edd16368SStephen M. Cameron 		default:
5264edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
5265edd16368SStephen M. Cameron 			BUG();
5266a2dac136SStephen M. Cameron 			return -1;
5267edd16368SStephen M. Cameron 		}
5268edd16368SStephen M. Cameron 	} else if (cmd_type == TYPE_MSG) {
5269edd16368SStephen M. Cameron 		switch (cmd) {
5270edd16368SStephen M. Cameron 
5271edd16368SStephen M. Cameron 		case  HPSA_DEVICE_RESET_MSG:
5272edd16368SStephen M. Cameron 			c->Request.CDBLen = 16;
5273edd16368SStephen M. Cameron 			c->Request.Type.Type =  1; /* It is a MSG not a CMD */
5274edd16368SStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
5275edd16368SStephen M. Cameron 			c->Request.Type.Direction = XFER_NONE;
5276edd16368SStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
527764670ac8SStephen M. Cameron 			memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
527864670ac8SStephen M. Cameron 			c->Request.CDB[0] =  cmd;
527921e89afdSStephen M. Cameron 			c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
5280edd16368SStephen M. Cameron 			/* If bytes 4-7 are zero, it means reset the */
5281edd16368SStephen M. Cameron 			/* LunID device */
5282edd16368SStephen M. Cameron 			c->Request.CDB[4] = 0x00;
5283edd16368SStephen M. Cameron 			c->Request.CDB[5] = 0x00;
5284edd16368SStephen M. Cameron 			c->Request.CDB[6] = 0x00;
5285edd16368SStephen M. Cameron 			c->Request.CDB[7] = 0x00;
5286edd16368SStephen M. Cameron 			break;
528775167d2cSStephen M. Cameron 		case  HPSA_ABORT_MSG:
528875167d2cSStephen M. Cameron 			a = buff;       /* point to command to be aborted */
528975167d2cSStephen M. Cameron 			dev_dbg(&h->pdev->dev, "Abort Tag:0x%08x:%08x using request Tag:0x%08x:%08x\n",
529075167d2cSStephen M. Cameron 				a->Header.Tag.upper, a->Header.Tag.lower,
529175167d2cSStephen M. Cameron 				c->Header.Tag.upper, c->Header.Tag.lower);
529275167d2cSStephen M. Cameron 			c->Request.CDBLen = 16;
529375167d2cSStephen M. Cameron 			c->Request.Type.Type = TYPE_MSG;
529475167d2cSStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
529575167d2cSStephen M. Cameron 			c->Request.Type.Direction = XFER_WRITE;
529675167d2cSStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
529775167d2cSStephen M. Cameron 			c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
529875167d2cSStephen M. Cameron 			c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
529975167d2cSStephen M. Cameron 			c->Request.CDB[2] = 0x00; /* reserved */
530075167d2cSStephen M. Cameron 			c->Request.CDB[3] = 0x00; /* reserved */
530175167d2cSStephen M. Cameron 			/* Tag to abort goes in CDB[4]-CDB[11] */
530275167d2cSStephen M. Cameron 			c->Request.CDB[4] = a->Header.Tag.lower & 0xFF;
530375167d2cSStephen M. Cameron 			c->Request.CDB[5] = (a->Header.Tag.lower >> 8) & 0xFF;
530475167d2cSStephen M. Cameron 			c->Request.CDB[6] = (a->Header.Tag.lower >> 16) & 0xFF;
530575167d2cSStephen M. Cameron 			c->Request.CDB[7] = (a->Header.Tag.lower >> 24) & 0xFF;
530675167d2cSStephen M. Cameron 			c->Request.CDB[8] = a->Header.Tag.upper & 0xFF;
530775167d2cSStephen M. Cameron 			c->Request.CDB[9] = (a->Header.Tag.upper >> 8) & 0xFF;
530875167d2cSStephen M. Cameron 			c->Request.CDB[10] = (a->Header.Tag.upper >> 16) & 0xFF;
530975167d2cSStephen M. Cameron 			c->Request.CDB[11] = (a->Header.Tag.upper >> 24) & 0xFF;
531075167d2cSStephen M. Cameron 			c->Request.CDB[12] = 0x00; /* reserved */
531175167d2cSStephen M. Cameron 			c->Request.CDB[13] = 0x00; /* reserved */
531275167d2cSStephen M. Cameron 			c->Request.CDB[14] = 0x00; /* reserved */
531375167d2cSStephen M. Cameron 			c->Request.CDB[15] = 0x00; /* reserved */
531475167d2cSStephen M. Cameron 		break;
5315edd16368SStephen M. Cameron 		default:
5316edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown message type %d\n",
5317edd16368SStephen M. Cameron 				cmd);
5318edd16368SStephen M. Cameron 			BUG();
5319edd16368SStephen M. Cameron 		}
5320edd16368SStephen M. Cameron 	} else {
5321edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
5322edd16368SStephen M. Cameron 		BUG();
5323edd16368SStephen M. Cameron 	}
5324edd16368SStephen M. Cameron 
5325edd16368SStephen M. Cameron 	switch (c->Request.Type.Direction) {
5326edd16368SStephen M. Cameron 	case XFER_READ:
5327edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_FROMDEVICE;
5328edd16368SStephen M. Cameron 		break;
5329edd16368SStephen M. Cameron 	case XFER_WRITE:
5330edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_TODEVICE;
5331edd16368SStephen M. Cameron 		break;
5332edd16368SStephen M. Cameron 	case XFER_NONE:
5333edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_NONE;
5334edd16368SStephen M. Cameron 		break;
5335edd16368SStephen M. Cameron 	default:
5336edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_BIDIRECTIONAL;
5337edd16368SStephen M. Cameron 	}
5338a2dac136SStephen M. Cameron 	if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
5339a2dac136SStephen M. Cameron 		return -1;
5340a2dac136SStephen M. Cameron 	return 0;
5341edd16368SStephen M. Cameron }
5342edd16368SStephen M. Cameron 
5343edd16368SStephen M. Cameron /*
5344edd16368SStephen M. Cameron  * Map (physical) PCI mem into (virtual) kernel space
5345edd16368SStephen M. Cameron  */
5346edd16368SStephen M. Cameron static void __iomem *remap_pci_mem(ulong base, ulong size)
5347edd16368SStephen M. Cameron {
5348edd16368SStephen M. Cameron 	ulong page_base = ((ulong) base) & PAGE_MASK;
5349edd16368SStephen M. Cameron 	ulong page_offs = ((ulong) base) - page_base;
5350088ba34cSStephen M. Cameron 	void __iomem *page_remapped = ioremap_nocache(page_base,
5351088ba34cSStephen M. Cameron 		page_offs + size);
5352edd16368SStephen M. Cameron 
5353edd16368SStephen M. Cameron 	return page_remapped ? (page_remapped + page_offs) : NULL;
5354edd16368SStephen M. Cameron }
5355edd16368SStephen M. Cameron 
5356edd16368SStephen M. Cameron /* Takes cmds off the submission queue and sends them to the hardware,
5357edd16368SStephen M. Cameron  * then puts them on the queue of cmds waiting for completion.
5358edd16368SStephen M. Cameron  */
5359edd16368SStephen M. Cameron static void start_io(struct ctlr_info *h)
5360edd16368SStephen M. Cameron {
5361edd16368SStephen M. Cameron 	struct CommandList *c;
5362e16a33adSMatt Gates 	unsigned long flags;
5363edd16368SStephen M. Cameron 
5364e16a33adSMatt Gates 	spin_lock_irqsave(&h->lock, flags);
53659e0fc764SStephen M. Cameron 	while (!list_empty(&h->reqQ)) {
53669e0fc764SStephen M. Cameron 		c = list_entry(h->reqQ.next, struct CommandList, list);
5367edd16368SStephen M. Cameron 		/* can't do anything if fifo is full */
5368edd16368SStephen M. Cameron 		if ((h->access.fifo_full(h))) {
5369396883e2SStephen M. Cameron 			h->fifo_recently_full = 1;
5370edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "fifo full\n");
5371edd16368SStephen M. Cameron 			break;
5372edd16368SStephen M. Cameron 		}
5373396883e2SStephen M. Cameron 		h->fifo_recently_full = 0;
5374edd16368SStephen M. Cameron 
5375edd16368SStephen M. Cameron 		/* Get the first entry from the Request Q */
5376edd16368SStephen M. Cameron 		removeQ(c);
5377edd16368SStephen M. Cameron 		h->Qdepth--;
5378edd16368SStephen M. Cameron 
5379edd16368SStephen M. Cameron 		/* Put job onto the completed Q */
5380edd16368SStephen M. Cameron 		addQ(&h->cmpQ, c);
5381e16a33adSMatt Gates 
5382e16a33adSMatt Gates 		/* Must increment commands_outstanding before unlocking
5383e16a33adSMatt Gates 		 * and submitting to avoid race checking for fifo full
5384e16a33adSMatt Gates 		 * condition.
5385e16a33adSMatt Gates 		 */
5386e16a33adSMatt Gates 		h->commands_outstanding++;
5387e16a33adSMatt Gates 		if (h->commands_outstanding > h->max_outstanding)
5388e16a33adSMatt Gates 			h->max_outstanding = h->commands_outstanding;
5389e16a33adSMatt Gates 
5390e16a33adSMatt Gates 		/* Tell the controller execute command */
5391e16a33adSMatt Gates 		spin_unlock_irqrestore(&h->lock, flags);
5392e16a33adSMatt Gates 		h->access.submit_command(h, c);
5393e16a33adSMatt Gates 		spin_lock_irqsave(&h->lock, flags);
5394edd16368SStephen M. Cameron 	}
5395e16a33adSMatt Gates 	spin_unlock_irqrestore(&h->lock, flags);
5396edd16368SStephen M. Cameron }
5397edd16368SStephen M. Cameron 
5398254f796bSMatt Gates static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
5399edd16368SStephen M. Cameron {
5400254f796bSMatt Gates 	return h->access.command_completed(h, q);
5401edd16368SStephen M. Cameron }
5402edd16368SStephen M. Cameron 
5403900c5440SStephen M. Cameron static inline bool interrupt_pending(struct ctlr_info *h)
5404edd16368SStephen M. Cameron {
5405edd16368SStephen M. Cameron 	return h->access.intr_pending(h);
5406edd16368SStephen M. Cameron }
5407edd16368SStephen M. Cameron 
5408edd16368SStephen M. Cameron static inline long interrupt_not_for_us(struct ctlr_info *h)
5409edd16368SStephen M. Cameron {
541010f66018SStephen M. Cameron 	return (h->access.intr_pending(h) == 0) ||
541110f66018SStephen M. Cameron 		(h->interrupts_enabled == 0);
5412edd16368SStephen M. Cameron }
5413edd16368SStephen M. Cameron 
541401a02ffcSStephen M. Cameron static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
541501a02ffcSStephen M. Cameron 	u32 raw_tag)
5416edd16368SStephen M. Cameron {
5417edd16368SStephen M. Cameron 	if (unlikely(tag_index >= h->nr_cmds)) {
5418edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
5419edd16368SStephen M. Cameron 		return 1;
5420edd16368SStephen M. Cameron 	}
5421edd16368SStephen M. Cameron 	return 0;
5422edd16368SStephen M. Cameron }
5423edd16368SStephen M. Cameron 
54245a3d16f5SStephen M. Cameron static inline void finish_cmd(struct CommandList *c)
5425edd16368SStephen M. Cameron {
5426e16a33adSMatt Gates 	unsigned long flags;
5427396883e2SStephen M. Cameron 	int io_may_be_stalled = 0;
5428396883e2SStephen M. Cameron 	struct ctlr_info *h = c->h;
5429e16a33adSMatt Gates 
5430396883e2SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
5431edd16368SStephen M. Cameron 	removeQ(c);
5432396883e2SStephen M. Cameron 
5433396883e2SStephen M. Cameron 	/*
5434396883e2SStephen M. Cameron 	 * Check for possibly stalled i/o.
5435396883e2SStephen M. Cameron 	 *
5436396883e2SStephen M. Cameron 	 * If a fifo_full condition is encountered, requests will back up
5437396883e2SStephen M. Cameron 	 * in h->reqQ.  This queue is only emptied out by start_io which is
5438396883e2SStephen M. Cameron 	 * only called when a new i/o request comes in.  If no i/o's are
5439396883e2SStephen M. Cameron 	 * forthcoming, the i/o's in h->reqQ can get stuck.  So we call
5440396883e2SStephen M. Cameron 	 * start_io from here if we detect such a danger.
5441396883e2SStephen M. Cameron 	 *
5442396883e2SStephen M. Cameron 	 * Normally, we shouldn't hit this case, but pounding on the
5443396883e2SStephen M. Cameron 	 * CCISS_PASSTHRU ioctl can provoke it.  Only call start_io if
5444396883e2SStephen M. Cameron 	 * commands_outstanding is low.  We want to avoid calling
5445396883e2SStephen M. Cameron 	 * start_io from in here as much as possible, and esp. don't
5446396883e2SStephen M. Cameron 	 * want to get in a cycle where we call start_io every time
5447396883e2SStephen M. Cameron 	 * through here.
5448396883e2SStephen M. Cameron 	 */
5449396883e2SStephen M. Cameron 	if (unlikely(h->fifo_recently_full) &&
5450396883e2SStephen M. Cameron 		h->commands_outstanding < 5)
5451396883e2SStephen M. Cameron 		io_may_be_stalled = 1;
5452396883e2SStephen M. Cameron 
5453396883e2SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
5454396883e2SStephen M. Cameron 
5455e85c5974SStephen M. Cameron 	dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
5456c349775eSScott Teel 	if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
5457c349775eSScott Teel 			|| c->cmd_type == CMD_IOACCEL2))
54581fb011fbSStephen M. Cameron 		complete_scsi_command(c);
5459edd16368SStephen M. Cameron 	else if (c->cmd_type == CMD_IOCTL_PEND)
5460edd16368SStephen M. Cameron 		complete(c->waiting);
5461396883e2SStephen M. Cameron 	if (unlikely(io_may_be_stalled))
5462396883e2SStephen M. Cameron 		start_io(h);
5463edd16368SStephen M. Cameron }
5464edd16368SStephen M. Cameron 
5465a104c99fSStephen M. Cameron static inline u32 hpsa_tag_contains_index(u32 tag)
5466a104c99fSStephen M. Cameron {
5467a104c99fSStephen M. Cameron 	return tag & DIRECT_LOOKUP_BIT;
5468a104c99fSStephen M. Cameron }
5469a104c99fSStephen M. Cameron 
5470a104c99fSStephen M. Cameron static inline u32 hpsa_tag_to_index(u32 tag)
5471a104c99fSStephen M. Cameron {
5472a104c99fSStephen M. Cameron 	return tag >> DIRECT_LOOKUP_SHIFT;
5473a104c99fSStephen M. Cameron }
5474a104c99fSStephen M. Cameron 
5475a9a3a273SStephen M. Cameron 
5476a9a3a273SStephen M. Cameron static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
5477a104c99fSStephen M. Cameron {
5478a9a3a273SStephen M. Cameron #define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
5479a9a3a273SStephen M. Cameron #define HPSA_SIMPLE_ERROR_BITS 0x03
5480960a30e7SStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
5481a9a3a273SStephen M. Cameron 		return tag & ~HPSA_SIMPLE_ERROR_BITS;
5482a9a3a273SStephen M. Cameron 	return tag & ~HPSA_PERF_ERROR_BITS;
5483a104c99fSStephen M. Cameron }
5484a104c99fSStephen M. Cameron 
5485303932fdSDon Brace /* process completion of an indexed ("direct lookup") command */
54861d94f94dSStephen M. Cameron static inline void process_indexed_cmd(struct ctlr_info *h,
5487303932fdSDon Brace 	u32 raw_tag)
5488303932fdSDon Brace {
5489303932fdSDon Brace 	u32 tag_index;
5490303932fdSDon Brace 	struct CommandList *c;
5491303932fdSDon Brace 
5492303932fdSDon Brace 	tag_index = hpsa_tag_to_index(raw_tag);
54931d94f94dSStephen M. Cameron 	if (!bad_tag(h, tag_index, raw_tag)) {
5494303932fdSDon Brace 		c = h->cmd_pool + tag_index;
54955a3d16f5SStephen M. Cameron 		finish_cmd(c);
54961d94f94dSStephen M. Cameron 	}
5497303932fdSDon Brace }
5498303932fdSDon Brace 
5499303932fdSDon Brace /* process completion of a non-indexed command */
55001d94f94dSStephen M. Cameron static inline void process_nonindexed_cmd(struct ctlr_info *h,
5501303932fdSDon Brace 	u32 raw_tag)
5502303932fdSDon Brace {
5503303932fdSDon Brace 	u32 tag;
5504303932fdSDon Brace 	struct CommandList *c = NULL;
5505e16a33adSMatt Gates 	unsigned long flags;
5506303932fdSDon Brace 
5507a9a3a273SStephen M. Cameron 	tag = hpsa_tag_discard_error_bits(h, raw_tag);
5508e16a33adSMatt Gates 	spin_lock_irqsave(&h->lock, flags);
55099e0fc764SStephen M. Cameron 	list_for_each_entry(c, &h->cmpQ, list) {
5510303932fdSDon Brace 		if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
5511e16a33adSMatt Gates 			spin_unlock_irqrestore(&h->lock, flags);
55125a3d16f5SStephen M. Cameron 			finish_cmd(c);
55131d94f94dSStephen M. Cameron 			return;
5514303932fdSDon Brace 		}
5515303932fdSDon Brace 	}
5516e16a33adSMatt Gates 	spin_unlock_irqrestore(&h->lock, flags);
5517303932fdSDon Brace 	bad_tag(h, h->nr_cmds + 1, raw_tag);
5518303932fdSDon Brace }
5519303932fdSDon Brace 
552064670ac8SStephen M. Cameron /* Some controllers, like p400, will give us one interrupt
552164670ac8SStephen M. Cameron  * after a soft reset, even if we turned interrupts off.
552264670ac8SStephen M. Cameron  * Only need to check for this in the hpsa_xxx_discard_completions
552364670ac8SStephen M. Cameron  * functions.
552464670ac8SStephen M. Cameron  */
552564670ac8SStephen M. Cameron static int ignore_bogus_interrupt(struct ctlr_info *h)
552664670ac8SStephen M. Cameron {
552764670ac8SStephen M. Cameron 	if (likely(!reset_devices))
552864670ac8SStephen M. Cameron 		return 0;
552964670ac8SStephen M. Cameron 
553064670ac8SStephen M. Cameron 	if (likely(h->interrupts_enabled))
553164670ac8SStephen M. Cameron 		return 0;
553264670ac8SStephen M. Cameron 
553364670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
553464670ac8SStephen M. Cameron 		"(known firmware bug.)  Ignoring.\n");
553564670ac8SStephen M. Cameron 
553664670ac8SStephen M. Cameron 	return 1;
553764670ac8SStephen M. Cameron }
553864670ac8SStephen M. Cameron 
5539254f796bSMatt Gates /*
5540254f796bSMatt Gates  * Convert &h->q[x] (passed to interrupt handlers) back to h.
5541254f796bSMatt Gates  * Relies on (h-q[x] == x) being true for x such that
5542254f796bSMatt Gates  * 0 <= x < MAX_REPLY_QUEUES.
5543254f796bSMatt Gates  */
5544254f796bSMatt Gates static struct ctlr_info *queue_to_hba(u8 *queue)
554564670ac8SStephen M. Cameron {
5546254f796bSMatt Gates 	return container_of((queue - *queue), struct ctlr_info, q[0]);
5547254f796bSMatt Gates }
5548254f796bSMatt Gates 
5549254f796bSMatt Gates static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
5550254f796bSMatt Gates {
5551254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
5552254f796bSMatt Gates 	u8 q = *(u8 *) queue;
555364670ac8SStephen M. Cameron 	u32 raw_tag;
555464670ac8SStephen M. Cameron 
555564670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
555664670ac8SStephen M. Cameron 		return IRQ_NONE;
555764670ac8SStephen M. Cameron 
555864670ac8SStephen M. Cameron 	if (interrupt_not_for_us(h))
555964670ac8SStephen M. Cameron 		return IRQ_NONE;
5560a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
556164670ac8SStephen M. Cameron 	while (interrupt_pending(h)) {
5562254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
556364670ac8SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY)
5564254f796bSMatt Gates 			raw_tag = next_command(h, q);
556564670ac8SStephen M. Cameron 	}
556664670ac8SStephen M. Cameron 	return IRQ_HANDLED;
556764670ac8SStephen M. Cameron }
556864670ac8SStephen M. Cameron 
5569254f796bSMatt Gates static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
557064670ac8SStephen M. Cameron {
5571254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
557264670ac8SStephen M. Cameron 	u32 raw_tag;
5573254f796bSMatt Gates 	u8 q = *(u8 *) queue;
557464670ac8SStephen M. Cameron 
557564670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
557664670ac8SStephen M. Cameron 		return IRQ_NONE;
557764670ac8SStephen M. Cameron 
5578a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
5579254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
558064670ac8SStephen M. Cameron 	while (raw_tag != FIFO_EMPTY)
5581254f796bSMatt Gates 		raw_tag = next_command(h, q);
558264670ac8SStephen M. Cameron 	return IRQ_HANDLED;
558364670ac8SStephen M. Cameron }
558464670ac8SStephen M. Cameron 
5585254f796bSMatt Gates static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
5586edd16368SStephen M. Cameron {
5587254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba((u8 *) queue);
5588303932fdSDon Brace 	u32 raw_tag;
5589254f796bSMatt Gates 	u8 q = *(u8 *) queue;
5590edd16368SStephen M. Cameron 
5591edd16368SStephen M. Cameron 	if (interrupt_not_for_us(h))
5592edd16368SStephen M. Cameron 		return IRQ_NONE;
5593a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
559410f66018SStephen M. Cameron 	while (interrupt_pending(h)) {
5595254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
559610f66018SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY) {
55971d94f94dSStephen M. Cameron 			if (likely(hpsa_tag_contains_index(raw_tag)))
55981d94f94dSStephen M. Cameron 				process_indexed_cmd(h, raw_tag);
559910f66018SStephen M. Cameron 			else
56001d94f94dSStephen M. Cameron 				process_nonindexed_cmd(h, raw_tag);
5601254f796bSMatt Gates 			raw_tag = next_command(h, q);
560210f66018SStephen M. Cameron 		}
560310f66018SStephen M. Cameron 	}
560410f66018SStephen M. Cameron 	return IRQ_HANDLED;
560510f66018SStephen M. Cameron }
560610f66018SStephen M. Cameron 
5607254f796bSMatt Gates static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
560810f66018SStephen M. Cameron {
5609254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
561010f66018SStephen M. Cameron 	u32 raw_tag;
5611254f796bSMatt Gates 	u8 q = *(u8 *) queue;
561210f66018SStephen M. Cameron 
5613a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
5614254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
5615303932fdSDon Brace 	while (raw_tag != FIFO_EMPTY) {
56161d94f94dSStephen M. Cameron 		if (likely(hpsa_tag_contains_index(raw_tag)))
56171d94f94dSStephen M. Cameron 			process_indexed_cmd(h, raw_tag);
5618303932fdSDon Brace 		else
56191d94f94dSStephen M. Cameron 			process_nonindexed_cmd(h, raw_tag);
5620254f796bSMatt Gates 		raw_tag = next_command(h, q);
5621edd16368SStephen M. Cameron 	}
5622edd16368SStephen M. Cameron 	return IRQ_HANDLED;
5623edd16368SStephen M. Cameron }
5624edd16368SStephen M. Cameron 
5625a9a3a273SStephen M. Cameron /* Send a message CDB to the firmware. Careful, this only works
5626a9a3a273SStephen M. Cameron  * in simple mode, not performant mode due to the tag lookup.
5627a9a3a273SStephen M. Cameron  * We only ever use this immediately after a controller reset.
5628a9a3a273SStephen M. Cameron  */
56296f039790SGreg Kroah-Hartman static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
5630edd16368SStephen M. Cameron 			unsigned char type)
5631edd16368SStephen M. Cameron {
5632edd16368SStephen M. Cameron 	struct Command {
5633edd16368SStephen M. Cameron 		struct CommandListHeader CommandHeader;
5634edd16368SStephen M. Cameron 		struct RequestBlock Request;
5635edd16368SStephen M. Cameron 		struct ErrDescriptor ErrorDescriptor;
5636edd16368SStephen M. Cameron 	};
5637edd16368SStephen M. Cameron 	struct Command *cmd;
5638edd16368SStephen M. Cameron 	static const size_t cmd_sz = sizeof(*cmd) +
5639edd16368SStephen M. Cameron 					sizeof(cmd->ErrorDescriptor);
5640edd16368SStephen M. Cameron 	dma_addr_t paddr64;
5641edd16368SStephen M. Cameron 	uint32_t paddr32, tag;
5642edd16368SStephen M. Cameron 	void __iomem *vaddr;
5643edd16368SStephen M. Cameron 	int i, err;
5644edd16368SStephen M. Cameron 
5645edd16368SStephen M. Cameron 	vaddr = pci_ioremap_bar(pdev, 0);
5646edd16368SStephen M. Cameron 	if (vaddr == NULL)
5647edd16368SStephen M. Cameron 		return -ENOMEM;
5648edd16368SStephen M. Cameron 
5649edd16368SStephen M. Cameron 	/* The Inbound Post Queue only accepts 32-bit physical addresses for the
5650edd16368SStephen M. Cameron 	 * CCISS commands, so they must be allocated from the lower 4GiB of
5651edd16368SStephen M. Cameron 	 * memory.
5652edd16368SStephen M. Cameron 	 */
5653edd16368SStephen M. Cameron 	err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
5654edd16368SStephen M. Cameron 	if (err) {
5655edd16368SStephen M. Cameron 		iounmap(vaddr);
5656edd16368SStephen M. Cameron 		return -ENOMEM;
5657edd16368SStephen M. Cameron 	}
5658edd16368SStephen M. Cameron 
5659edd16368SStephen M. Cameron 	cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
5660edd16368SStephen M. Cameron 	if (cmd == NULL) {
5661edd16368SStephen M. Cameron 		iounmap(vaddr);
5662edd16368SStephen M. Cameron 		return -ENOMEM;
5663edd16368SStephen M. Cameron 	}
5664edd16368SStephen M. Cameron 
5665edd16368SStephen M. Cameron 	/* This must fit, because of the 32-bit consistent DMA mask.  Also,
5666edd16368SStephen M. Cameron 	 * although there's no guarantee, we assume that the address is at
5667edd16368SStephen M. Cameron 	 * least 4-byte aligned (most likely, it's page-aligned).
5668edd16368SStephen M. Cameron 	 */
5669edd16368SStephen M. Cameron 	paddr32 = paddr64;
5670edd16368SStephen M. Cameron 
5671edd16368SStephen M. Cameron 	cmd->CommandHeader.ReplyQueue = 0;
5672edd16368SStephen M. Cameron 	cmd->CommandHeader.SGList = 0;
5673edd16368SStephen M. Cameron 	cmd->CommandHeader.SGTotal = 0;
5674edd16368SStephen M. Cameron 	cmd->CommandHeader.Tag.lower = paddr32;
5675edd16368SStephen M. Cameron 	cmd->CommandHeader.Tag.upper = 0;
5676edd16368SStephen M. Cameron 	memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
5677edd16368SStephen M. Cameron 
5678edd16368SStephen M. Cameron 	cmd->Request.CDBLen = 16;
5679edd16368SStephen M. Cameron 	cmd->Request.Type.Type = TYPE_MSG;
5680edd16368SStephen M. Cameron 	cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
5681edd16368SStephen M. Cameron 	cmd->Request.Type.Direction = XFER_NONE;
5682edd16368SStephen M. Cameron 	cmd->Request.Timeout = 0; /* Don't time out */
5683edd16368SStephen M. Cameron 	cmd->Request.CDB[0] = opcode;
5684edd16368SStephen M. Cameron 	cmd->Request.CDB[1] = type;
5685edd16368SStephen M. Cameron 	memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
5686edd16368SStephen M. Cameron 	cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
5687edd16368SStephen M. Cameron 	cmd->ErrorDescriptor.Addr.upper = 0;
5688edd16368SStephen M. Cameron 	cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
5689edd16368SStephen M. Cameron 
5690edd16368SStephen M. Cameron 	writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
5691edd16368SStephen M. Cameron 
5692edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
5693edd16368SStephen M. Cameron 		tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
5694a9a3a273SStephen M. Cameron 		if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr32)
5695edd16368SStephen M. Cameron 			break;
5696edd16368SStephen M. Cameron 		msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
5697edd16368SStephen M. Cameron 	}
5698edd16368SStephen M. Cameron 
5699edd16368SStephen M. Cameron 	iounmap(vaddr);
5700edd16368SStephen M. Cameron 
5701edd16368SStephen M. Cameron 	/* we leak the DMA buffer here ... no choice since the controller could
5702edd16368SStephen M. Cameron 	 *  still complete the command.
5703edd16368SStephen M. Cameron 	 */
5704edd16368SStephen M. Cameron 	if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
5705edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
5706edd16368SStephen M. Cameron 			opcode, type);
5707edd16368SStephen M. Cameron 		return -ETIMEDOUT;
5708edd16368SStephen M. Cameron 	}
5709edd16368SStephen M. Cameron 
5710edd16368SStephen M. Cameron 	pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
5711edd16368SStephen M. Cameron 
5712edd16368SStephen M. Cameron 	if (tag & HPSA_ERROR_BIT) {
5713edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
5714edd16368SStephen M. Cameron 			opcode, type);
5715edd16368SStephen M. Cameron 		return -EIO;
5716edd16368SStephen M. Cameron 	}
5717edd16368SStephen M. Cameron 
5718edd16368SStephen M. Cameron 	dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
5719edd16368SStephen M. Cameron 		opcode, type);
5720edd16368SStephen M. Cameron 	return 0;
5721edd16368SStephen M. Cameron }
5722edd16368SStephen M. Cameron 
5723edd16368SStephen M. Cameron #define hpsa_noop(p) hpsa_message(p, 3, 0)
5724edd16368SStephen M. Cameron 
57251df8552aSStephen M. Cameron static int hpsa_controller_hard_reset(struct pci_dev *pdev,
5726cf0b08d0SStephen M. Cameron 	void * __iomem vaddr, u32 use_doorbell)
5727edd16368SStephen M. Cameron {
57281df8552aSStephen M. Cameron 	u16 pmcsr;
57291df8552aSStephen M. Cameron 	int pos;
5730edd16368SStephen M. Cameron 
57311df8552aSStephen M. Cameron 	if (use_doorbell) {
57321df8552aSStephen M. Cameron 		/* For everything after the P600, the PCI power state method
57331df8552aSStephen M. Cameron 		 * of resetting the controller doesn't work, so we have this
57341df8552aSStephen M. Cameron 		 * other way using the doorbell register.
5735edd16368SStephen M. Cameron 		 */
57361df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using doorbell to reset controller\n");
5737cf0b08d0SStephen M. Cameron 		writel(use_doorbell, vaddr + SA5_DOORBELL);
573885009239SStephen M. Cameron 
573985009239SStephen M. Cameron 		/* PMC hardware guys tell us we need a 5 second delay after
574085009239SStephen M. Cameron 		 * doorbell reset and before any attempt to talk to the board
574185009239SStephen M. Cameron 		 * at all to ensure that this actually works and doesn't fall
574285009239SStephen M. Cameron 		 * over in some weird corner cases.
574385009239SStephen M. Cameron 		 */
574485009239SStephen M. Cameron 		msleep(5000);
57451df8552aSStephen M. Cameron 	} else { /* Try to do it the PCI power state way */
5746edd16368SStephen M. Cameron 
5747edd16368SStephen M. Cameron 		/* Quoting from the Open CISS Specification: "The Power
5748edd16368SStephen M. Cameron 		 * Management Control/Status Register (CSR) controls the power
5749edd16368SStephen M. Cameron 		 * state of the device.  The normal operating state is D0,
5750edd16368SStephen M. Cameron 		 * CSR=00h.  The software off state is D3, CSR=03h.  To reset
57511df8552aSStephen M. Cameron 		 * the controller, place the interface device in D3 then to D0,
57521df8552aSStephen M. Cameron 		 * this causes a secondary PCI reset which will reset the
57531df8552aSStephen M. Cameron 		 * controller." */
5754edd16368SStephen M. Cameron 
57551df8552aSStephen M. Cameron 		pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
57561df8552aSStephen M. Cameron 		if (pos == 0) {
57571df8552aSStephen M. Cameron 			dev_err(&pdev->dev,
57581df8552aSStephen M. Cameron 				"hpsa_reset_controller: "
57591df8552aSStephen M. Cameron 				"PCI PM not supported\n");
57601df8552aSStephen M. Cameron 			return -ENODEV;
57611df8552aSStephen M. Cameron 		}
57621df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using PCI PM to reset controller\n");
5763edd16368SStephen M. Cameron 		/* enter the D3hot power management state */
5764edd16368SStephen M. Cameron 		pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
5765edd16368SStephen M. Cameron 		pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
5766edd16368SStephen M. Cameron 		pmcsr |= PCI_D3hot;
5767edd16368SStephen M. Cameron 		pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
5768edd16368SStephen M. Cameron 
5769edd16368SStephen M. Cameron 		msleep(500);
5770edd16368SStephen M. Cameron 
5771edd16368SStephen M. Cameron 		/* enter the D0 power management state */
5772edd16368SStephen M. Cameron 		pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
5773edd16368SStephen M. Cameron 		pmcsr |= PCI_D0;
5774edd16368SStephen M. Cameron 		pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
5775c4853efeSMike Miller 
5776c4853efeSMike Miller 		/*
5777c4853efeSMike Miller 		 * The P600 requires a small delay when changing states.
5778c4853efeSMike Miller 		 * Otherwise we may think the board did not reset and we bail.
5779c4853efeSMike Miller 		 * This for kdump only and is particular to the P600.
5780c4853efeSMike Miller 		 */
5781c4853efeSMike Miller 		msleep(500);
57821df8552aSStephen M. Cameron 	}
57831df8552aSStephen M. Cameron 	return 0;
57841df8552aSStephen M. Cameron }
57851df8552aSStephen M. Cameron 
57866f039790SGreg Kroah-Hartman static void init_driver_version(char *driver_version, int len)
5787580ada3cSStephen M. Cameron {
5788580ada3cSStephen M. Cameron 	memset(driver_version, 0, len);
5789f79cfec6SStephen M. Cameron 	strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
5790580ada3cSStephen M. Cameron }
5791580ada3cSStephen M. Cameron 
57926f039790SGreg Kroah-Hartman static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
5793580ada3cSStephen M. Cameron {
5794580ada3cSStephen M. Cameron 	char *driver_version;
5795580ada3cSStephen M. Cameron 	int i, size = sizeof(cfgtable->driver_version);
5796580ada3cSStephen M. Cameron 
5797580ada3cSStephen M. Cameron 	driver_version = kmalloc(size, GFP_KERNEL);
5798580ada3cSStephen M. Cameron 	if (!driver_version)
5799580ada3cSStephen M. Cameron 		return -ENOMEM;
5800580ada3cSStephen M. Cameron 
5801580ada3cSStephen M. Cameron 	init_driver_version(driver_version, size);
5802580ada3cSStephen M. Cameron 	for (i = 0; i < size; i++)
5803580ada3cSStephen M. Cameron 		writeb(driver_version[i], &cfgtable->driver_version[i]);
5804580ada3cSStephen M. Cameron 	kfree(driver_version);
5805580ada3cSStephen M. Cameron 	return 0;
5806580ada3cSStephen M. Cameron }
5807580ada3cSStephen M. Cameron 
58086f039790SGreg Kroah-Hartman static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
58096f039790SGreg Kroah-Hartman 					  unsigned char *driver_ver)
5810580ada3cSStephen M. Cameron {
5811580ada3cSStephen M. Cameron 	int i;
5812580ada3cSStephen M. Cameron 
5813580ada3cSStephen M. Cameron 	for (i = 0; i < sizeof(cfgtable->driver_version); i++)
5814580ada3cSStephen M. Cameron 		driver_ver[i] = readb(&cfgtable->driver_version[i]);
5815580ada3cSStephen M. Cameron }
5816580ada3cSStephen M. Cameron 
58176f039790SGreg Kroah-Hartman static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
5818580ada3cSStephen M. Cameron {
5819580ada3cSStephen M. Cameron 
5820580ada3cSStephen M. Cameron 	char *driver_ver, *old_driver_ver;
5821580ada3cSStephen M. Cameron 	int rc, size = sizeof(cfgtable->driver_version);
5822580ada3cSStephen M. Cameron 
5823580ada3cSStephen M. Cameron 	old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
5824580ada3cSStephen M. Cameron 	if (!old_driver_ver)
5825580ada3cSStephen M. Cameron 		return -ENOMEM;
5826580ada3cSStephen M. Cameron 	driver_ver = old_driver_ver + size;
5827580ada3cSStephen M. Cameron 
5828580ada3cSStephen M. Cameron 	/* After a reset, the 32 bytes of "driver version" in the cfgtable
5829580ada3cSStephen M. Cameron 	 * should have been changed, otherwise we know the reset failed.
5830580ada3cSStephen M. Cameron 	 */
5831580ada3cSStephen M. Cameron 	init_driver_version(old_driver_ver, size);
5832580ada3cSStephen M. Cameron 	read_driver_ver_from_cfgtable(cfgtable, driver_ver);
5833580ada3cSStephen M. Cameron 	rc = !memcmp(driver_ver, old_driver_ver, size);
5834580ada3cSStephen M. Cameron 	kfree(old_driver_ver);
5835580ada3cSStephen M. Cameron 	return rc;
5836580ada3cSStephen M. Cameron }
58371df8552aSStephen M. Cameron /* This does a hard reset of the controller using PCI power management
58381df8552aSStephen M. Cameron  * states or the using the doorbell register.
58391df8552aSStephen M. Cameron  */
58406f039790SGreg Kroah-Hartman static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev)
58411df8552aSStephen M. Cameron {
58421df8552aSStephen M. Cameron 	u64 cfg_offset;
58431df8552aSStephen M. Cameron 	u32 cfg_base_addr;
58441df8552aSStephen M. Cameron 	u64 cfg_base_addr_index;
58451df8552aSStephen M. Cameron 	void __iomem *vaddr;
58461df8552aSStephen M. Cameron 	unsigned long paddr;
5847580ada3cSStephen M. Cameron 	u32 misc_fw_support;
5848270d05deSStephen M. Cameron 	int rc;
58491df8552aSStephen M. Cameron 	struct CfgTable __iomem *cfgtable;
5850cf0b08d0SStephen M. Cameron 	u32 use_doorbell;
585118867659SStephen M. Cameron 	u32 board_id;
5852270d05deSStephen M. Cameron 	u16 command_register;
58531df8552aSStephen M. Cameron 
58541df8552aSStephen M. Cameron 	/* For controllers as old as the P600, this is very nearly
58551df8552aSStephen M. Cameron 	 * the same thing as
58561df8552aSStephen M. Cameron 	 *
58571df8552aSStephen M. Cameron 	 * pci_save_state(pci_dev);
58581df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D3hot);
58591df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D0);
58601df8552aSStephen M. Cameron 	 * pci_restore_state(pci_dev);
58611df8552aSStephen M. Cameron 	 *
58621df8552aSStephen M. Cameron 	 * For controllers newer than the P600, the pci power state
58631df8552aSStephen M. Cameron 	 * method of resetting doesn't work so we have another way
58641df8552aSStephen M. Cameron 	 * using the doorbell register.
58651df8552aSStephen M. Cameron 	 */
586618867659SStephen M. Cameron 
586725c1e56aSStephen M. Cameron 	rc = hpsa_lookup_board_id(pdev, &board_id);
586846380786SStephen M. Cameron 	if (rc < 0 || !ctlr_is_resettable(board_id)) {
586925c1e56aSStephen M. Cameron 		dev_warn(&pdev->dev, "Not resetting device.\n");
587025c1e56aSStephen M. Cameron 		return -ENODEV;
587125c1e56aSStephen M. Cameron 	}
587246380786SStephen M. Cameron 
587346380786SStephen M. Cameron 	/* if controller is soft- but not hard resettable... */
587446380786SStephen M. Cameron 	if (!ctlr_is_hard_resettable(board_id))
587546380786SStephen M. Cameron 		return -ENOTSUPP; /* try soft reset later. */
587618867659SStephen M. Cameron 
5877270d05deSStephen M. Cameron 	/* Save the PCI command register */
5878270d05deSStephen M. Cameron 	pci_read_config_word(pdev, 4, &command_register);
5879270d05deSStephen M. Cameron 	/* Turn the board off.  This is so that later pci_restore_state()
5880270d05deSStephen M. Cameron 	 * won't turn the board on before the rest of config space is ready.
5881270d05deSStephen M. Cameron 	 */
5882270d05deSStephen M. Cameron 	pci_disable_device(pdev);
5883270d05deSStephen M. Cameron 	pci_save_state(pdev);
58841df8552aSStephen M. Cameron 
58851df8552aSStephen M. Cameron 	/* find the first memory BAR, so we can find the cfg table */
58861df8552aSStephen M. Cameron 	rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
58871df8552aSStephen M. Cameron 	if (rc)
58881df8552aSStephen M. Cameron 		return rc;
58891df8552aSStephen M. Cameron 	vaddr = remap_pci_mem(paddr, 0x250);
58901df8552aSStephen M. Cameron 	if (!vaddr)
58911df8552aSStephen M. Cameron 		return -ENOMEM;
58921df8552aSStephen M. Cameron 
58931df8552aSStephen M. Cameron 	/* find cfgtable in order to check if reset via doorbell is supported */
58941df8552aSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
58951df8552aSStephen M. Cameron 					&cfg_base_addr_index, &cfg_offset);
58961df8552aSStephen M. Cameron 	if (rc)
58971df8552aSStephen M. Cameron 		goto unmap_vaddr;
58981df8552aSStephen M. Cameron 	cfgtable = remap_pci_mem(pci_resource_start(pdev,
58991df8552aSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
59001df8552aSStephen M. Cameron 	if (!cfgtable) {
59011df8552aSStephen M. Cameron 		rc = -ENOMEM;
59021df8552aSStephen M. Cameron 		goto unmap_vaddr;
59031df8552aSStephen M. Cameron 	}
5904580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(cfgtable);
5905580ada3cSStephen M. Cameron 	if (rc)
5906580ada3cSStephen M. Cameron 		goto unmap_vaddr;
59071df8552aSStephen M. Cameron 
5908cf0b08d0SStephen M. Cameron 	/* If reset via doorbell register is supported, use that.
5909cf0b08d0SStephen M. Cameron 	 * There are two such methods.  Favor the newest method.
5910cf0b08d0SStephen M. Cameron 	 */
59111df8552aSStephen M. Cameron 	misc_fw_support = readl(&cfgtable->misc_fw_support);
5912cf0b08d0SStephen M. Cameron 	use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
5913cf0b08d0SStephen M. Cameron 	if (use_doorbell) {
5914cf0b08d0SStephen M. Cameron 		use_doorbell = DOORBELL_CTLR_RESET2;
5915cf0b08d0SStephen M. Cameron 	} else {
59161df8552aSStephen M. Cameron 		use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
5917cf0b08d0SStephen M. Cameron 		if (use_doorbell) {
5918fba63097SMike Miller 			dev_warn(&pdev->dev, "Soft reset not supported. "
5919fba63097SMike Miller 				"Firmware update is required.\n");
592064670ac8SStephen M. Cameron 			rc = -ENOTSUPP; /* try soft reset */
5921cf0b08d0SStephen M. Cameron 			goto unmap_cfgtable;
5922cf0b08d0SStephen M. Cameron 		}
5923cf0b08d0SStephen M. Cameron 	}
59241df8552aSStephen M. Cameron 
59251df8552aSStephen M. Cameron 	rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
59261df8552aSStephen M. Cameron 	if (rc)
59271df8552aSStephen M. Cameron 		goto unmap_cfgtable;
5928edd16368SStephen M. Cameron 
5929270d05deSStephen M. Cameron 	pci_restore_state(pdev);
5930270d05deSStephen M. Cameron 	rc = pci_enable_device(pdev);
5931270d05deSStephen M. Cameron 	if (rc) {
5932270d05deSStephen M. Cameron 		dev_warn(&pdev->dev, "failed to enable device.\n");
5933270d05deSStephen M. Cameron 		goto unmap_cfgtable;
5934edd16368SStephen M. Cameron 	}
5935270d05deSStephen M. Cameron 	pci_write_config_word(pdev, 4, command_register);
5936edd16368SStephen M. Cameron 
59371df8552aSStephen M. Cameron 	/* Some devices (notably the HP Smart Array 5i Controller)
59381df8552aSStephen M. Cameron 	   need a little pause here */
59391df8552aSStephen M. Cameron 	msleep(HPSA_POST_RESET_PAUSE_MSECS);
59401df8552aSStephen M. Cameron 
5941fe5389c8SStephen M. Cameron 	rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
5942fe5389c8SStephen M. Cameron 	if (rc) {
5943fe5389c8SStephen M. Cameron 		dev_warn(&pdev->dev,
594464670ac8SStephen M. Cameron 			"failed waiting for board to become ready "
594564670ac8SStephen M. Cameron 			"after hard reset\n");
5946fe5389c8SStephen M. Cameron 		goto unmap_cfgtable;
5947fe5389c8SStephen M. Cameron 	}
5948fe5389c8SStephen M. Cameron 
5949580ada3cSStephen M. Cameron 	rc = controller_reset_failed(vaddr);
5950580ada3cSStephen M. Cameron 	if (rc < 0)
5951580ada3cSStephen M. Cameron 		goto unmap_cfgtable;
5952580ada3cSStephen M. Cameron 	if (rc) {
595364670ac8SStephen M. Cameron 		dev_warn(&pdev->dev, "Unable to successfully reset "
595464670ac8SStephen M. Cameron 			"controller. Will try soft reset.\n");
595564670ac8SStephen M. Cameron 		rc = -ENOTSUPP;
5956580ada3cSStephen M. Cameron 	} else {
595764670ac8SStephen M. Cameron 		dev_info(&pdev->dev, "board ready after hard reset.\n");
59581df8552aSStephen M. Cameron 	}
59591df8552aSStephen M. Cameron 
59601df8552aSStephen M. Cameron unmap_cfgtable:
59611df8552aSStephen M. Cameron 	iounmap(cfgtable);
59621df8552aSStephen M. Cameron 
59631df8552aSStephen M. Cameron unmap_vaddr:
59641df8552aSStephen M. Cameron 	iounmap(vaddr);
59651df8552aSStephen M. Cameron 	return rc;
5966edd16368SStephen M. Cameron }
5967edd16368SStephen M. Cameron 
5968edd16368SStephen M. Cameron /*
5969edd16368SStephen M. Cameron  *  We cannot read the structure directly, for portability we must use
5970edd16368SStephen M. Cameron  *   the io functions.
5971edd16368SStephen M. Cameron  *   This is for debug only.
5972edd16368SStephen M. Cameron  */
5973edd16368SStephen M. Cameron static void print_cfg_table(struct device *dev, struct CfgTable *tb)
5974edd16368SStephen M. Cameron {
597558f8665cSStephen M. Cameron #ifdef HPSA_DEBUG
5976edd16368SStephen M. Cameron 	int i;
5977edd16368SStephen M. Cameron 	char temp_name[17];
5978edd16368SStephen M. Cameron 
5979edd16368SStephen M. Cameron 	dev_info(dev, "Controller Configuration information\n");
5980edd16368SStephen M. Cameron 	dev_info(dev, "------------------------------------\n");
5981edd16368SStephen M. Cameron 	for (i = 0; i < 4; i++)
5982edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->Signature[i]));
5983edd16368SStephen M. Cameron 	temp_name[4] = '\0';
5984edd16368SStephen M. Cameron 	dev_info(dev, "   Signature = %s\n", temp_name);
5985edd16368SStephen M. Cameron 	dev_info(dev, "   Spec Number = %d\n", readl(&(tb->SpecValence)));
5986edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods supported = 0x%x\n",
5987edd16368SStephen M. Cameron 	       readl(&(tb->TransportSupport)));
5988edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods active = 0x%x\n",
5989edd16368SStephen M. Cameron 	       readl(&(tb->TransportActive)));
5990edd16368SStephen M. Cameron 	dev_info(dev, "   Requested transport Method = 0x%x\n",
5991edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.TransportRequest)));
5992edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Delay = 0x%x\n",
5993edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntDelay)));
5994edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Count = 0x%x\n",
5995edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntCount)));
5996edd16368SStephen M. Cameron 	dev_info(dev, "   Max outstanding commands = 0x%d\n",
5997edd16368SStephen M. Cameron 	       readl(&(tb->CmdsOutMax)));
5998edd16368SStephen M. Cameron 	dev_info(dev, "   Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
5999edd16368SStephen M. Cameron 	for (i = 0; i < 16; i++)
6000edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->ServerName[i]));
6001edd16368SStephen M. Cameron 	temp_name[16] = '\0';
6002edd16368SStephen M. Cameron 	dev_info(dev, "   Server Name = %s\n", temp_name);
6003edd16368SStephen M. Cameron 	dev_info(dev, "   Heartbeat Counter = 0x%x\n\n\n",
6004edd16368SStephen M. Cameron 		readl(&(tb->HeartBeat)));
6005edd16368SStephen M. Cameron #endif				/* HPSA_DEBUG */
600658f8665cSStephen M. Cameron }
6007edd16368SStephen M. Cameron 
6008edd16368SStephen M. Cameron static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
6009edd16368SStephen M. Cameron {
6010edd16368SStephen M. Cameron 	int i, offset, mem_type, bar_type;
6011edd16368SStephen M. Cameron 
6012edd16368SStephen M. Cameron 	if (pci_bar_addr == PCI_BASE_ADDRESS_0)	/* looking for BAR zero? */
6013edd16368SStephen M. Cameron 		return 0;
6014edd16368SStephen M. Cameron 	offset = 0;
6015edd16368SStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
6016edd16368SStephen M. Cameron 		bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
6017edd16368SStephen M. Cameron 		if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
6018edd16368SStephen M. Cameron 			offset += 4;
6019edd16368SStephen M. Cameron 		else {
6020edd16368SStephen M. Cameron 			mem_type = pci_resource_flags(pdev, i) &
6021edd16368SStephen M. Cameron 			    PCI_BASE_ADDRESS_MEM_TYPE_MASK;
6022edd16368SStephen M. Cameron 			switch (mem_type) {
6023edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_32:
6024edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_1M:
6025edd16368SStephen M. Cameron 				offset += 4;	/* 32 bit */
6026edd16368SStephen M. Cameron 				break;
6027edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_64:
6028edd16368SStephen M. Cameron 				offset += 8;
6029edd16368SStephen M. Cameron 				break;
6030edd16368SStephen M. Cameron 			default:	/* reserved in PCI 2.2 */
6031edd16368SStephen M. Cameron 				dev_warn(&pdev->dev,
6032edd16368SStephen M. Cameron 				       "base address is invalid\n");
6033edd16368SStephen M. Cameron 				return -1;
6034edd16368SStephen M. Cameron 				break;
6035edd16368SStephen M. Cameron 			}
6036edd16368SStephen M. Cameron 		}
6037edd16368SStephen M. Cameron 		if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
6038edd16368SStephen M. Cameron 			return i + 1;
6039edd16368SStephen M. Cameron 	}
6040edd16368SStephen M. Cameron 	return -1;
6041edd16368SStephen M. Cameron }
6042edd16368SStephen M. Cameron 
6043edd16368SStephen M. Cameron /* If MSI/MSI-X is supported by the kernel we will try to enable it on
6044edd16368SStephen M. Cameron  * controllers that are capable. If not, we use IO-APIC mode.
6045edd16368SStephen M. Cameron  */
6046edd16368SStephen M. Cameron 
60476f039790SGreg Kroah-Hartman static void hpsa_interrupt_mode(struct ctlr_info *h)
6048edd16368SStephen M. Cameron {
6049edd16368SStephen M. Cameron #ifdef CONFIG_PCI_MSI
6050254f796bSMatt Gates 	int err, i;
6051254f796bSMatt Gates 	struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
6052254f796bSMatt Gates 
6053254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++) {
6054254f796bSMatt Gates 		hpsa_msix_entries[i].vector = 0;
6055254f796bSMatt Gates 		hpsa_msix_entries[i].entry = i;
6056254f796bSMatt Gates 	}
6057edd16368SStephen M. Cameron 
6058edd16368SStephen M. Cameron 	/* Some boards advertise MSI but don't really support it */
60596b3f4c52SStephen M. Cameron 	if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
60606b3f4c52SStephen M. Cameron 	    (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
6061edd16368SStephen M. Cameron 		goto default_int_mode;
606255c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
606355c06c71SStephen M. Cameron 		dev_info(&h->pdev->dev, "MSIX\n");
6064eee0f03aSHannes Reinecke 		h->msix_vector = MAX_REPLY_QUEUES;
6065254f796bSMatt Gates 		err = pci_enable_msix(h->pdev, hpsa_msix_entries,
6066eee0f03aSHannes Reinecke 				      h->msix_vector);
6067edd16368SStephen M. Cameron 		if (err > 0) {
606855c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
6069edd16368SStephen M. Cameron 			       "available\n", err);
6070eee0f03aSHannes Reinecke 			h->msix_vector = err;
6071eee0f03aSHannes Reinecke 			err = pci_enable_msix(h->pdev, hpsa_msix_entries,
6072eee0f03aSHannes Reinecke 					      h->msix_vector);
6073eee0f03aSHannes Reinecke 		}
6074eee0f03aSHannes Reinecke 		if (!err) {
6075eee0f03aSHannes Reinecke 			for (i = 0; i < h->msix_vector; i++)
6076eee0f03aSHannes Reinecke 				h->intr[i] = hpsa_msix_entries[i].vector;
6077eee0f03aSHannes Reinecke 			return;
6078edd16368SStephen M. Cameron 		} else {
607955c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "MSI-X init failed %d\n",
6080edd16368SStephen M. Cameron 			       err);
6081eee0f03aSHannes Reinecke 			h->msix_vector = 0;
6082edd16368SStephen M. Cameron 			goto default_int_mode;
6083edd16368SStephen M. Cameron 		}
6084edd16368SStephen M. Cameron 	}
608555c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
608655c06c71SStephen M. Cameron 		dev_info(&h->pdev->dev, "MSI\n");
608755c06c71SStephen M. Cameron 		if (!pci_enable_msi(h->pdev))
6088edd16368SStephen M. Cameron 			h->msi_vector = 1;
6089edd16368SStephen M. Cameron 		else
609055c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "MSI init failed\n");
6091edd16368SStephen M. Cameron 	}
6092edd16368SStephen M. Cameron default_int_mode:
6093edd16368SStephen M. Cameron #endif				/* CONFIG_PCI_MSI */
6094edd16368SStephen M. Cameron 	/* if we get here we're going to use the default interrupt mode */
6095a9a3a273SStephen M. Cameron 	h->intr[h->intr_mode] = h->pdev->irq;
6096edd16368SStephen M. Cameron }
6097edd16368SStephen M. Cameron 
60986f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
6099e5c880d1SStephen M. Cameron {
6100e5c880d1SStephen M. Cameron 	int i;
6101e5c880d1SStephen M. Cameron 	u32 subsystem_vendor_id, subsystem_device_id;
6102e5c880d1SStephen M. Cameron 
6103e5c880d1SStephen M. Cameron 	subsystem_vendor_id = pdev->subsystem_vendor;
6104e5c880d1SStephen M. Cameron 	subsystem_device_id = pdev->subsystem_device;
6105e5c880d1SStephen M. Cameron 	*board_id = ((subsystem_device_id << 16) & 0xffff0000) |
6106e5c880d1SStephen M. Cameron 		    subsystem_vendor_id;
6107e5c880d1SStephen M. Cameron 
6108e5c880d1SStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(products); i++)
6109e5c880d1SStephen M. Cameron 		if (*board_id == products[i].board_id)
6110e5c880d1SStephen M. Cameron 			return i;
6111e5c880d1SStephen M. Cameron 
61126798cc0aSStephen M. Cameron 	if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
61136798cc0aSStephen M. Cameron 		subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
61146798cc0aSStephen M. Cameron 		!hpsa_allow_any) {
6115e5c880d1SStephen M. Cameron 		dev_warn(&pdev->dev, "unrecognized board ID: "
6116e5c880d1SStephen M. Cameron 			"0x%08x, ignoring.\n", *board_id);
6117e5c880d1SStephen M. Cameron 			return -ENODEV;
6118e5c880d1SStephen M. Cameron 	}
6119e5c880d1SStephen M. Cameron 	return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
6120e5c880d1SStephen M. Cameron }
6121e5c880d1SStephen M. Cameron 
61226f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
61233a7774ceSStephen M. Cameron 				    unsigned long *memory_bar)
61243a7774ceSStephen M. Cameron {
61253a7774ceSStephen M. Cameron 	int i;
61263a7774ceSStephen M. Cameron 
61273a7774ceSStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
612812d2cd47SStephen M. Cameron 		if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
61293a7774ceSStephen M. Cameron 			/* addressing mode bits already removed */
613012d2cd47SStephen M. Cameron 			*memory_bar = pci_resource_start(pdev, i);
613112d2cd47SStephen M. Cameron 			dev_dbg(&pdev->dev, "memory BAR = %lx\n",
61323a7774ceSStephen M. Cameron 				*memory_bar);
61333a7774ceSStephen M. Cameron 			return 0;
61343a7774ceSStephen M. Cameron 		}
613512d2cd47SStephen M. Cameron 	dev_warn(&pdev->dev, "no memory BAR found\n");
61363a7774ceSStephen M. Cameron 	return -ENODEV;
61373a7774ceSStephen M. Cameron }
61383a7774ceSStephen M. Cameron 
61396f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
61406f039790SGreg Kroah-Hartman 				     int wait_for_ready)
61412c4c8c8bSStephen M. Cameron {
6142fe5389c8SStephen M. Cameron 	int i, iterations;
61432c4c8c8bSStephen M. Cameron 	u32 scratchpad;
6144fe5389c8SStephen M. Cameron 	if (wait_for_ready)
6145fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_READY_ITERATIONS;
6146fe5389c8SStephen M. Cameron 	else
6147fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
61482c4c8c8bSStephen M. Cameron 
6149fe5389c8SStephen M. Cameron 	for (i = 0; i < iterations; i++) {
6150fe5389c8SStephen M. Cameron 		scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
6151fe5389c8SStephen M. Cameron 		if (wait_for_ready) {
61522c4c8c8bSStephen M. Cameron 			if (scratchpad == HPSA_FIRMWARE_READY)
61532c4c8c8bSStephen M. Cameron 				return 0;
6154fe5389c8SStephen M. Cameron 		} else {
6155fe5389c8SStephen M. Cameron 			if (scratchpad != HPSA_FIRMWARE_READY)
6156fe5389c8SStephen M. Cameron 				return 0;
6157fe5389c8SStephen M. Cameron 		}
61582c4c8c8bSStephen M. Cameron 		msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
61592c4c8c8bSStephen M. Cameron 	}
6160fe5389c8SStephen M. Cameron 	dev_warn(&pdev->dev, "board not ready, timed out.\n");
61612c4c8c8bSStephen M. Cameron 	return -ENODEV;
61622c4c8c8bSStephen M. Cameron }
61632c4c8c8bSStephen M. Cameron 
61646f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
61656f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
6166a51fd47fSStephen M. Cameron 			       u64 *cfg_offset)
6167a51fd47fSStephen M. Cameron {
6168a51fd47fSStephen M. Cameron 	*cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
6169a51fd47fSStephen M. Cameron 	*cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
6170a51fd47fSStephen M. Cameron 	*cfg_base_addr &= (u32) 0x0000ffff;
6171a51fd47fSStephen M. Cameron 	*cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
6172a51fd47fSStephen M. Cameron 	if (*cfg_base_addr_index == -1) {
6173a51fd47fSStephen M. Cameron 		dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
6174a51fd47fSStephen M. Cameron 		return -ENODEV;
6175a51fd47fSStephen M. Cameron 	}
6176a51fd47fSStephen M. Cameron 	return 0;
6177a51fd47fSStephen M. Cameron }
6178a51fd47fSStephen M. Cameron 
61796f039790SGreg Kroah-Hartman static int hpsa_find_cfgtables(struct ctlr_info *h)
6180edd16368SStephen M. Cameron {
618101a02ffcSStephen M. Cameron 	u64 cfg_offset;
618201a02ffcSStephen M. Cameron 	u32 cfg_base_addr;
618301a02ffcSStephen M. Cameron 	u64 cfg_base_addr_index;
6184303932fdSDon Brace 	u32 trans_offset;
6185a51fd47fSStephen M. Cameron 	int rc;
618677c4495cSStephen M. Cameron 
6187a51fd47fSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
6188a51fd47fSStephen M. Cameron 		&cfg_base_addr_index, &cfg_offset);
6189a51fd47fSStephen M. Cameron 	if (rc)
6190a51fd47fSStephen M. Cameron 		return rc;
619177c4495cSStephen M. Cameron 	h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
6192a51fd47fSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
619377c4495cSStephen M. Cameron 	if (!h->cfgtable)
619477c4495cSStephen M. Cameron 		return -ENOMEM;
6195580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(h->cfgtable);
6196580ada3cSStephen M. Cameron 	if (rc)
6197580ada3cSStephen M. Cameron 		return rc;
619877c4495cSStephen M. Cameron 	/* Find performant mode table. */
6199a51fd47fSStephen M. Cameron 	trans_offset = readl(&h->cfgtable->TransMethodOffset);
620077c4495cSStephen M. Cameron 	h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
620177c4495cSStephen M. Cameron 				cfg_base_addr_index)+cfg_offset+trans_offset,
620277c4495cSStephen M. Cameron 				sizeof(*h->transtable));
620377c4495cSStephen M. Cameron 	if (!h->transtable)
620477c4495cSStephen M. Cameron 		return -ENOMEM;
620577c4495cSStephen M. Cameron 	return 0;
620677c4495cSStephen M. Cameron }
620777c4495cSStephen M. Cameron 
62086f039790SGreg Kroah-Hartman static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
6209cba3d38bSStephen M. Cameron {
6210cba3d38bSStephen M. Cameron 	h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
621172ceeaecSStephen M. Cameron 
621272ceeaecSStephen M. Cameron 	/* Limit commands in memory limited kdump scenario. */
621372ceeaecSStephen M. Cameron 	if (reset_devices && h->max_commands > 32)
621472ceeaecSStephen M. Cameron 		h->max_commands = 32;
621572ceeaecSStephen M. Cameron 
6216cba3d38bSStephen M. Cameron 	if (h->max_commands < 16) {
6217cba3d38bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "Controller reports "
6218cba3d38bSStephen M. Cameron 			"max supported commands of %d, an obvious lie. "
6219cba3d38bSStephen M. Cameron 			"Using 16.  Ensure that firmware is up to date.\n",
6220cba3d38bSStephen M. Cameron 			h->max_commands);
6221cba3d38bSStephen M. Cameron 		h->max_commands = 16;
6222cba3d38bSStephen M. Cameron 	}
6223cba3d38bSStephen M. Cameron }
6224cba3d38bSStephen M. Cameron 
6225b93d7536SStephen M. Cameron /* Interrogate the hardware for some limits:
6226b93d7536SStephen M. Cameron  * max commands, max SG elements without chaining, and with chaining,
6227b93d7536SStephen M. Cameron  * SG chain block size, etc.
6228b93d7536SStephen M. Cameron  */
62296f039790SGreg Kroah-Hartman static void hpsa_find_board_params(struct ctlr_info *h)
6230b93d7536SStephen M. Cameron {
6231cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
6232b93d7536SStephen M. Cameron 	h->nr_cmds = h->max_commands - 4; /* Allow room for some ioctls */
6233b93d7536SStephen M. Cameron 	h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
6234283b4a9bSStephen M. Cameron 	h->fw_support = readl(&(h->cfgtable->misc_fw_support));
6235b93d7536SStephen M. Cameron 	/*
6236b93d7536SStephen M. Cameron 	 * Limit in-command s/g elements to 32 save dma'able memory.
6237b93d7536SStephen M. Cameron 	 * Howvever spec says if 0, use 31
6238b93d7536SStephen M. Cameron 	 */
6239b93d7536SStephen M. Cameron 	h->max_cmd_sg_entries = 31;
6240b93d7536SStephen M. Cameron 	if (h->maxsgentries > 512) {
6241b93d7536SStephen M. Cameron 		h->max_cmd_sg_entries = 32;
6242b93d7536SStephen M. Cameron 		h->chainsize = h->maxsgentries - h->max_cmd_sg_entries + 1;
6243b93d7536SStephen M. Cameron 		h->maxsgentries--; /* save one for chain pointer */
6244b93d7536SStephen M. Cameron 	} else {
6245b93d7536SStephen M. Cameron 		h->maxsgentries = 31; /* default to traditional values */
6246b93d7536SStephen M. Cameron 		h->chainsize = 0;
6247b93d7536SStephen M. Cameron 	}
624875167d2cSStephen M. Cameron 
624975167d2cSStephen M. Cameron 	/* Find out what task management functions are supported and cache */
625075167d2cSStephen M. Cameron 	h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
62510e7a7fceSScott Teel 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
62520e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
62530e7a7fceSScott Teel 	if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
62540e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
6255b93d7536SStephen M. Cameron }
6256b93d7536SStephen M. Cameron 
625776c46e49SStephen M. Cameron static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
625876c46e49SStephen M. Cameron {
62590fc9fd40SAkinobu Mita 	if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
626076c46e49SStephen M. Cameron 		dev_warn(&h->pdev->dev, "not a valid CISS config table\n");
626176c46e49SStephen M. Cameron 		return false;
626276c46e49SStephen M. Cameron 	}
626376c46e49SStephen M. Cameron 	return true;
626476c46e49SStephen M. Cameron }
626576c46e49SStephen M. Cameron 
626697a5e98cSStephen M. Cameron static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
6267f7c39101SStephen M. Cameron {
626897a5e98cSStephen M. Cameron 	u32 driver_support;
6269f7c39101SStephen M. Cameron 
627028e13446SStephen M. Cameron #ifdef CONFIG_X86
627128e13446SStephen M. Cameron 	/* Need to enable prefetch in the SCSI core for 6400 in x86 */
627297a5e98cSStephen M. Cameron 	driver_support = readl(&(h->cfgtable->driver_support));
627397a5e98cSStephen M. Cameron 	driver_support |= ENABLE_SCSI_PREFETCH;
6274f7c39101SStephen M. Cameron #endif
627528e13446SStephen M. Cameron 	driver_support |= ENABLE_UNIT_ATTN;
627628e13446SStephen M. Cameron 	writel(driver_support, &(h->cfgtable->driver_support));
6277f7c39101SStephen M. Cameron }
6278f7c39101SStephen M. Cameron 
62793d0eab67SStephen M. Cameron /* Disable DMA prefetch for the P600.  Otherwise an ASIC bug may result
62803d0eab67SStephen M. Cameron  * in a prefetch beyond physical memory.
62813d0eab67SStephen M. Cameron  */
62823d0eab67SStephen M. Cameron static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
62833d0eab67SStephen M. Cameron {
62843d0eab67SStephen M. Cameron 	u32 dma_prefetch;
62853d0eab67SStephen M. Cameron 
62863d0eab67SStephen M. Cameron 	if (h->board_id != 0x3225103C)
62873d0eab67SStephen M. Cameron 		return;
62883d0eab67SStephen M. Cameron 	dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
62893d0eab67SStephen M. Cameron 	dma_prefetch |= 0x8000;
62903d0eab67SStephen M. Cameron 	writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
62913d0eab67SStephen M. Cameron }
62923d0eab67SStephen M. Cameron 
629376438d08SStephen M. Cameron static void hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
629476438d08SStephen M. Cameron {
629576438d08SStephen M. Cameron 	int i;
629676438d08SStephen M. Cameron 	u32 doorbell_value;
629776438d08SStephen M. Cameron 	unsigned long flags;
629876438d08SStephen M. Cameron 	/* wait until the clear_event_notify bit 6 is cleared by controller. */
629976438d08SStephen M. Cameron 	for (i = 0; i < MAX_CONFIG_WAIT; i++) {
630076438d08SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
630176438d08SStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
630276438d08SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
630376438d08SStephen M. Cameron 		if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
630476438d08SStephen M. Cameron 			break;
630576438d08SStephen M. Cameron 		/* delay and try again */
630676438d08SStephen M. Cameron 		msleep(20);
630776438d08SStephen M. Cameron 	}
630876438d08SStephen M. Cameron }
630976438d08SStephen M. Cameron 
63106f039790SGreg Kroah-Hartman static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
6311eb6b2ae9SStephen M. Cameron {
6312eb6b2ae9SStephen M. Cameron 	int i;
63136eaf46fdSStephen M. Cameron 	u32 doorbell_value;
63146eaf46fdSStephen M. Cameron 	unsigned long flags;
6315eb6b2ae9SStephen M. Cameron 
6316eb6b2ae9SStephen M. Cameron 	/* under certain very rare conditions, this can take awhile.
6317eb6b2ae9SStephen M. Cameron 	 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
6318eb6b2ae9SStephen M. Cameron 	 * as we enter this code.)
6319eb6b2ae9SStephen M. Cameron 	 */
6320eb6b2ae9SStephen M. Cameron 	for (i = 0; i < MAX_CONFIG_WAIT; i++) {
63216eaf46fdSStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
63226eaf46fdSStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
63236eaf46fdSStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
6324382be668SDan Carpenter 		if (!(doorbell_value & CFGTBL_ChangeReq))
6325eb6b2ae9SStephen M. Cameron 			break;
6326eb6b2ae9SStephen M. Cameron 		/* delay and try again */
632760d3f5b0SStephen M. Cameron 		usleep_range(10000, 20000);
6328eb6b2ae9SStephen M. Cameron 	}
63293f4336f3SStephen M. Cameron }
63303f4336f3SStephen M. Cameron 
63316f039790SGreg Kroah-Hartman static int hpsa_enter_simple_mode(struct ctlr_info *h)
63323f4336f3SStephen M. Cameron {
63333f4336f3SStephen M. Cameron 	u32 trans_support;
63343f4336f3SStephen M. Cameron 
63353f4336f3SStephen M. Cameron 	trans_support = readl(&(h->cfgtable->TransportSupport));
63363f4336f3SStephen M. Cameron 	if (!(trans_support & SIMPLE_MODE))
63373f4336f3SStephen M. Cameron 		return -ENOTSUPP;
63383f4336f3SStephen M. Cameron 
63393f4336f3SStephen M. Cameron 	h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
6340283b4a9bSStephen M. Cameron 
63413f4336f3SStephen M. Cameron 	/* Update the field, and then ring the doorbell */
63423f4336f3SStephen M. Cameron 	writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
6343b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
63443f4336f3SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
63453f4336f3SStephen M. Cameron 	hpsa_wait_for_mode_change_ack(h);
6346eb6b2ae9SStephen M. Cameron 	print_cfg_table(&h->pdev->dev, h->cfgtable);
6347283b4a9bSStephen M. Cameron 	if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
6348283b4a9bSStephen M. Cameron 		goto error;
6349960a30e7SStephen M. Cameron 	h->transMethod = CFGTBL_Trans_Simple;
6350eb6b2ae9SStephen M. Cameron 	return 0;
6351283b4a9bSStephen M. Cameron error:
6352283b4a9bSStephen M. Cameron 	dev_warn(&h->pdev->dev, "unable to get board into simple mode\n");
6353283b4a9bSStephen M. Cameron 	return -ENODEV;
6354eb6b2ae9SStephen M. Cameron }
6355eb6b2ae9SStephen M. Cameron 
63566f039790SGreg Kroah-Hartman static int hpsa_pci_init(struct ctlr_info *h)
635777c4495cSStephen M. Cameron {
6358eb6b2ae9SStephen M. Cameron 	int prod_index, err;
6359edd16368SStephen M. Cameron 
6360e5c880d1SStephen M. Cameron 	prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
6361e5c880d1SStephen M. Cameron 	if (prod_index < 0)
6362edd16368SStephen M. Cameron 		return -ENODEV;
6363e5c880d1SStephen M. Cameron 	h->product_name = products[prod_index].product_name;
6364e5c880d1SStephen M. Cameron 	h->access = *(products[prod_index].access);
6365e5c880d1SStephen M. Cameron 
6366e5a44df8SMatthew Garrett 	pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
6367e5a44df8SMatthew Garrett 			       PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
6368e5a44df8SMatthew Garrett 
636955c06c71SStephen M. Cameron 	err = pci_enable_device(h->pdev);
6370edd16368SStephen M. Cameron 	if (err) {
637155c06c71SStephen M. Cameron 		dev_warn(&h->pdev->dev, "unable to enable PCI device\n");
6372edd16368SStephen M. Cameron 		return err;
6373edd16368SStephen M. Cameron 	}
6374edd16368SStephen M. Cameron 
63755cb460a6SStephen M. Cameron 	/* Enable bus mastering (pci_disable_device may disable this) */
63765cb460a6SStephen M. Cameron 	pci_set_master(h->pdev);
63775cb460a6SStephen M. Cameron 
6378f79cfec6SStephen M. Cameron 	err = pci_request_regions(h->pdev, HPSA);
6379edd16368SStephen M. Cameron 	if (err) {
638055c06c71SStephen M. Cameron 		dev_err(&h->pdev->dev,
638155c06c71SStephen M. Cameron 			"cannot obtain PCI resources, aborting\n");
6382edd16368SStephen M. Cameron 		return err;
6383edd16368SStephen M. Cameron 	}
63846b3f4c52SStephen M. Cameron 	hpsa_interrupt_mode(h);
638512d2cd47SStephen M. Cameron 	err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
63863a7774ceSStephen M. Cameron 	if (err)
6387edd16368SStephen M. Cameron 		goto err_out_free_res;
6388edd16368SStephen M. Cameron 	h->vaddr = remap_pci_mem(h->paddr, 0x250);
6389204892e9SStephen M. Cameron 	if (!h->vaddr) {
6390204892e9SStephen M. Cameron 		err = -ENOMEM;
6391204892e9SStephen M. Cameron 		goto err_out_free_res;
6392204892e9SStephen M. Cameron 	}
6393fe5389c8SStephen M. Cameron 	err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
63942c4c8c8bSStephen M. Cameron 	if (err)
6395edd16368SStephen M. Cameron 		goto err_out_free_res;
639677c4495cSStephen M. Cameron 	err = hpsa_find_cfgtables(h);
639777c4495cSStephen M. Cameron 	if (err)
6398edd16368SStephen M. Cameron 		goto err_out_free_res;
6399b93d7536SStephen M. Cameron 	hpsa_find_board_params(h);
6400edd16368SStephen M. Cameron 
640176c46e49SStephen M. Cameron 	if (!hpsa_CISS_signature_present(h)) {
6402edd16368SStephen M. Cameron 		err = -ENODEV;
6403edd16368SStephen M. Cameron 		goto err_out_free_res;
6404edd16368SStephen M. Cameron 	}
640597a5e98cSStephen M. Cameron 	hpsa_set_driver_support_bits(h);
64063d0eab67SStephen M. Cameron 	hpsa_p600_dma_prefetch_quirk(h);
6407eb6b2ae9SStephen M. Cameron 	err = hpsa_enter_simple_mode(h);
6408eb6b2ae9SStephen M. Cameron 	if (err)
6409edd16368SStephen M. Cameron 		goto err_out_free_res;
6410edd16368SStephen M. Cameron 	return 0;
6411edd16368SStephen M. Cameron 
6412edd16368SStephen M. Cameron err_out_free_res:
6413204892e9SStephen M. Cameron 	if (h->transtable)
6414204892e9SStephen M. Cameron 		iounmap(h->transtable);
6415204892e9SStephen M. Cameron 	if (h->cfgtable)
6416204892e9SStephen M. Cameron 		iounmap(h->cfgtable);
6417204892e9SStephen M. Cameron 	if (h->vaddr)
6418204892e9SStephen M. Cameron 		iounmap(h->vaddr);
6419f0bd0b68SStephen M. Cameron 	pci_disable_device(h->pdev);
642055c06c71SStephen M. Cameron 	pci_release_regions(h->pdev);
6421edd16368SStephen M. Cameron 	return err;
6422edd16368SStephen M. Cameron }
6423edd16368SStephen M. Cameron 
64246f039790SGreg Kroah-Hartman static void hpsa_hba_inquiry(struct ctlr_info *h)
6425339b2b14SStephen M. Cameron {
6426339b2b14SStephen M. Cameron 	int rc;
6427339b2b14SStephen M. Cameron 
6428339b2b14SStephen M. Cameron #define HBA_INQUIRY_BYTE_COUNT 64
6429339b2b14SStephen M. Cameron 	h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
6430339b2b14SStephen M. Cameron 	if (!h->hba_inquiry_data)
6431339b2b14SStephen M. Cameron 		return;
6432339b2b14SStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
6433339b2b14SStephen M. Cameron 		h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
6434339b2b14SStephen M. Cameron 	if (rc != 0) {
6435339b2b14SStephen M. Cameron 		kfree(h->hba_inquiry_data);
6436339b2b14SStephen M. Cameron 		h->hba_inquiry_data = NULL;
6437339b2b14SStephen M. Cameron 	}
6438339b2b14SStephen M. Cameron }
6439339b2b14SStephen M. Cameron 
64406f039790SGreg Kroah-Hartman static int hpsa_init_reset_devices(struct pci_dev *pdev)
6441edd16368SStephen M. Cameron {
64421df8552aSStephen M. Cameron 	int rc, i;
6443edd16368SStephen M. Cameron 
64444c2a8c40SStephen M. Cameron 	if (!reset_devices)
64454c2a8c40SStephen M. Cameron 		return 0;
64464c2a8c40SStephen M. Cameron 
64471df8552aSStephen M. Cameron 	/* Reset the controller with a PCI power-cycle or via doorbell */
64481df8552aSStephen M. Cameron 	rc = hpsa_kdump_hard_reset_controller(pdev);
6449edd16368SStephen M. Cameron 
64501df8552aSStephen M. Cameron 	/* -ENOTSUPP here means we cannot reset the controller
64511df8552aSStephen M. Cameron 	 * but it's already (and still) up and running in
645218867659SStephen M. Cameron 	 * "performant mode".  Or, it might be 640x, which can't reset
645318867659SStephen M. Cameron 	 * due to concerns about shared bbwc between 6402/6404 pair.
64541df8552aSStephen M. Cameron 	 */
64551df8552aSStephen M. Cameron 	if (rc == -ENOTSUPP)
645664670ac8SStephen M. Cameron 		return rc; /* just try to do the kdump anyhow. */
64571df8552aSStephen M. Cameron 	if (rc)
64581df8552aSStephen M. Cameron 		return -ENODEV;
6459edd16368SStephen M. Cameron 
6460edd16368SStephen M. Cameron 	/* Now try to get the controller to respond to a no-op */
64612b870cb3SStephen M. Cameron 	dev_warn(&pdev->dev, "Waiting for controller to respond to no-op\n");
6462edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
6463edd16368SStephen M. Cameron 		if (hpsa_noop(pdev) == 0)
6464edd16368SStephen M. Cameron 			break;
6465edd16368SStephen M. Cameron 		else
6466edd16368SStephen M. Cameron 			dev_warn(&pdev->dev, "no-op failed%s\n",
6467edd16368SStephen M. Cameron 					(i < 11 ? "; re-trying" : ""));
6468edd16368SStephen M. Cameron 	}
64694c2a8c40SStephen M. Cameron 	return 0;
6470edd16368SStephen M. Cameron }
6471edd16368SStephen M. Cameron 
64726f039790SGreg Kroah-Hartman static int hpsa_allocate_cmd_pool(struct ctlr_info *h)
64732e9d1b36SStephen M. Cameron {
64742e9d1b36SStephen M. Cameron 	h->cmd_pool_bits = kzalloc(
64752e9d1b36SStephen M. Cameron 		DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
64762e9d1b36SStephen M. Cameron 		sizeof(unsigned long), GFP_KERNEL);
64772e9d1b36SStephen M. Cameron 	h->cmd_pool = pci_alloc_consistent(h->pdev,
64782e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->cmd_pool),
64792e9d1b36SStephen M. Cameron 		    &(h->cmd_pool_dhandle));
64802e9d1b36SStephen M. Cameron 	h->errinfo_pool = pci_alloc_consistent(h->pdev,
64812e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->errinfo_pool),
64822e9d1b36SStephen M. Cameron 		    &(h->errinfo_pool_dhandle));
64832e9d1b36SStephen M. Cameron 	if ((h->cmd_pool_bits == NULL)
64842e9d1b36SStephen M. Cameron 	    || (h->cmd_pool == NULL)
64852e9d1b36SStephen M. Cameron 	    || (h->errinfo_pool == NULL)) {
64862e9d1b36SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory in %s", __func__);
64872e9d1b36SStephen M. Cameron 		return -ENOMEM;
64882e9d1b36SStephen M. Cameron 	}
64892e9d1b36SStephen M. Cameron 	return 0;
64902e9d1b36SStephen M. Cameron }
64912e9d1b36SStephen M. Cameron 
64922e9d1b36SStephen M. Cameron static void hpsa_free_cmd_pool(struct ctlr_info *h)
64932e9d1b36SStephen M. Cameron {
64942e9d1b36SStephen M. Cameron 	kfree(h->cmd_pool_bits);
64952e9d1b36SStephen M. Cameron 	if (h->cmd_pool)
64962e9d1b36SStephen M. Cameron 		pci_free_consistent(h->pdev,
64972e9d1b36SStephen M. Cameron 			    h->nr_cmds * sizeof(struct CommandList),
64982e9d1b36SStephen M. Cameron 			    h->cmd_pool, h->cmd_pool_dhandle);
6499aca9012aSStephen M. Cameron 	if (h->ioaccel2_cmd_pool)
6500aca9012aSStephen M. Cameron 		pci_free_consistent(h->pdev,
6501aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
6502aca9012aSStephen M. Cameron 			h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
65032e9d1b36SStephen M. Cameron 	if (h->errinfo_pool)
65042e9d1b36SStephen M. Cameron 		pci_free_consistent(h->pdev,
65052e9d1b36SStephen M. Cameron 			    h->nr_cmds * sizeof(struct ErrorInfo),
65062e9d1b36SStephen M. Cameron 			    h->errinfo_pool,
65072e9d1b36SStephen M. Cameron 			    h->errinfo_pool_dhandle);
6508e1f7de0cSMatt Gates 	if (h->ioaccel_cmd_pool)
6509e1f7de0cSMatt Gates 		pci_free_consistent(h->pdev,
6510e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(struct io_accel1_cmd),
6511e1f7de0cSMatt Gates 			h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
65122e9d1b36SStephen M. Cameron }
65132e9d1b36SStephen M. Cameron 
65140ae01a32SStephen M. Cameron static int hpsa_request_irq(struct ctlr_info *h,
65150ae01a32SStephen M. Cameron 	irqreturn_t (*msixhandler)(int, void *),
65160ae01a32SStephen M. Cameron 	irqreturn_t (*intxhandler)(int, void *))
65170ae01a32SStephen M. Cameron {
6518254f796bSMatt Gates 	int rc, i;
65190ae01a32SStephen M. Cameron 
6520254f796bSMatt Gates 	/*
6521254f796bSMatt Gates 	 * initialize h->q[x] = x so that interrupt handlers know which
6522254f796bSMatt Gates 	 * queue to process.
6523254f796bSMatt Gates 	 */
6524254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++)
6525254f796bSMatt Gates 		h->q[i] = (u8) i;
6526254f796bSMatt Gates 
6527eee0f03aSHannes Reinecke 	if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
6528254f796bSMatt Gates 		/* If performant mode and MSI-X, use multiple reply queues */
6529eee0f03aSHannes Reinecke 		for (i = 0; i < h->msix_vector; i++)
6530254f796bSMatt Gates 			rc = request_irq(h->intr[i], msixhandler,
6531254f796bSMatt Gates 					0, h->devname,
6532254f796bSMatt Gates 					&h->q[i]);
6533254f796bSMatt Gates 	} else {
6534254f796bSMatt Gates 		/* Use single reply pool */
6535eee0f03aSHannes Reinecke 		if (h->msix_vector > 0 || h->msi_vector) {
6536254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
6537254f796bSMatt Gates 				msixhandler, 0, h->devname,
6538254f796bSMatt Gates 				&h->q[h->intr_mode]);
6539254f796bSMatt Gates 		} else {
6540254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
6541254f796bSMatt Gates 				intxhandler, IRQF_SHARED, h->devname,
6542254f796bSMatt Gates 				&h->q[h->intr_mode]);
6543254f796bSMatt Gates 		}
6544254f796bSMatt Gates 	}
65450ae01a32SStephen M. Cameron 	if (rc) {
65460ae01a32SStephen M. Cameron 		dev_err(&h->pdev->dev, "unable to get irq %d for %s\n",
65470ae01a32SStephen M. Cameron 		       h->intr[h->intr_mode], h->devname);
65480ae01a32SStephen M. Cameron 		return -ENODEV;
65490ae01a32SStephen M. Cameron 	}
65500ae01a32SStephen M. Cameron 	return 0;
65510ae01a32SStephen M. Cameron }
65520ae01a32SStephen M. Cameron 
65536f039790SGreg Kroah-Hartman static int hpsa_kdump_soft_reset(struct ctlr_info *h)
655464670ac8SStephen M. Cameron {
655564670ac8SStephen M. Cameron 	if (hpsa_send_host_reset(h, RAID_CTLR_LUNID,
655664670ac8SStephen M. Cameron 		HPSA_RESET_TYPE_CONTROLLER)) {
655764670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Resetting array controller failed.\n");
655864670ac8SStephen M. Cameron 		return -EIO;
655964670ac8SStephen M. Cameron 	}
656064670ac8SStephen M. Cameron 
656164670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
656264670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) {
656364670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
656464670ac8SStephen M. Cameron 		return -1;
656564670ac8SStephen M. Cameron 	}
656664670ac8SStephen M. Cameron 
656764670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
656864670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) {
656964670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Board failed to become ready "
657064670ac8SStephen M. Cameron 			"after soft reset.\n");
657164670ac8SStephen M. Cameron 		return -1;
657264670ac8SStephen M. Cameron 	}
657364670ac8SStephen M. Cameron 
657464670ac8SStephen M. Cameron 	return 0;
657564670ac8SStephen M. Cameron }
657664670ac8SStephen M. Cameron 
6577254f796bSMatt Gates static void free_irqs(struct ctlr_info *h)
6578254f796bSMatt Gates {
6579254f796bSMatt Gates 	int i;
6580254f796bSMatt Gates 
6581254f796bSMatt Gates 	if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
6582254f796bSMatt Gates 		/* Single reply queue, only one irq to free */
6583254f796bSMatt Gates 		i = h->intr_mode;
6584254f796bSMatt Gates 		free_irq(h->intr[i], &h->q[i]);
6585254f796bSMatt Gates 		return;
6586254f796bSMatt Gates 	}
6587254f796bSMatt Gates 
6588eee0f03aSHannes Reinecke 	for (i = 0; i < h->msix_vector; i++)
6589254f796bSMatt Gates 		free_irq(h->intr[i], &h->q[i]);
6590254f796bSMatt Gates }
6591254f796bSMatt Gates 
65920097f0f4SStephen M. Cameron static void hpsa_free_irqs_and_disable_msix(struct ctlr_info *h)
659364670ac8SStephen M. Cameron {
6594254f796bSMatt Gates 	free_irqs(h);
659564670ac8SStephen M. Cameron #ifdef CONFIG_PCI_MSI
65960097f0f4SStephen M. Cameron 	if (h->msix_vector) {
65970097f0f4SStephen M. Cameron 		if (h->pdev->msix_enabled)
659864670ac8SStephen M. Cameron 			pci_disable_msix(h->pdev);
65990097f0f4SStephen M. Cameron 	} else if (h->msi_vector) {
66000097f0f4SStephen M. Cameron 		if (h->pdev->msi_enabled)
660164670ac8SStephen M. Cameron 			pci_disable_msi(h->pdev);
66020097f0f4SStephen M. Cameron 	}
660364670ac8SStephen M. Cameron #endif /* CONFIG_PCI_MSI */
66040097f0f4SStephen M. Cameron }
66050097f0f4SStephen M. Cameron 
66060097f0f4SStephen M. Cameron static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
66070097f0f4SStephen M. Cameron {
66080097f0f4SStephen M. Cameron 	hpsa_free_irqs_and_disable_msix(h);
660964670ac8SStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
661064670ac8SStephen M. Cameron 	hpsa_free_cmd_pool(h);
6611e1f7de0cSMatt Gates 	kfree(h->ioaccel1_blockFetchTable);
661264670ac8SStephen M. Cameron 	kfree(h->blockFetchTable);
661364670ac8SStephen M. Cameron 	pci_free_consistent(h->pdev, h->reply_pool_size,
661464670ac8SStephen M. Cameron 		h->reply_pool, h->reply_pool_dhandle);
661564670ac8SStephen M. Cameron 	if (h->vaddr)
661664670ac8SStephen M. Cameron 		iounmap(h->vaddr);
661764670ac8SStephen M. Cameron 	if (h->transtable)
661864670ac8SStephen M. Cameron 		iounmap(h->transtable);
661964670ac8SStephen M. Cameron 	if (h->cfgtable)
662064670ac8SStephen M. Cameron 		iounmap(h->cfgtable);
662164670ac8SStephen M. Cameron 	pci_release_regions(h->pdev);
662264670ac8SStephen M. Cameron 	kfree(h);
662364670ac8SStephen M. Cameron }
662464670ac8SStephen M. Cameron 
6625a0c12413SStephen M. Cameron /* Called when controller lockup detected. */
6626a0c12413SStephen M. Cameron static void fail_all_cmds_on_list(struct ctlr_info *h, struct list_head *list)
6627a0c12413SStephen M. Cameron {
6628a0c12413SStephen M. Cameron 	struct CommandList *c = NULL;
6629a0c12413SStephen M. Cameron 
6630a0c12413SStephen M. Cameron 	assert_spin_locked(&h->lock);
6631a0c12413SStephen M. Cameron 	/* Mark all outstanding commands as failed and complete them. */
6632a0c12413SStephen M. Cameron 	while (!list_empty(list)) {
6633a0c12413SStephen M. Cameron 		c = list_entry(list->next, struct CommandList, list);
6634a0c12413SStephen M. Cameron 		c->err_info->CommandStatus = CMD_HARDWARE_ERR;
66355a3d16f5SStephen M. Cameron 		finish_cmd(c);
6636a0c12413SStephen M. Cameron 	}
6637a0c12413SStephen M. Cameron }
6638a0c12413SStephen M. Cameron 
6639a0c12413SStephen M. Cameron static void controller_lockup_detected(struct ctlr_info *h)
6640a0c12413SStephen M. Cameron {
6641a0c12413SStephen M. Cameron 	unsigned long flags;
6642a0c12413SStephen M. Cameron 
6643a0c12413SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
6644a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
6645a0c12413SStephen M. Cameron 	h->lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
6646a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
6647a0c12413SStephen M. Cameron 	dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x\n",
6648a0c12413SStephen M. Cameron 			h->lockup_detected);
6649a0c12413SStephen M. Cameron 	pci_disable_device(h->pdev);
6650a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
6651a0c12413SStephen M. Cameron 	fail_all_cmds_on_list(h, &h->cmpQ);
6652a0c12413SStephen M. Cameron 	fail_all_cmds_on_list(h, &h->reqQ);
6653a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
6654a0c12413SStephen M. Cameron }
6655a0c12413SStephen M. Cameron 
6656a0c12413SStephen M. Cameron static void detect_controller_lockup(struct ctlr_info *h)
6657a0c12413SStephen M. Cameron {
6658a0c12413SStephen M. Cameron 	u64 now;
6659a0c12413SStephen M. Cameron 	u32 heartbeat;
6660a0c12413SStephen M. Cameron 	unsigned long flags;
6661a0c12413SStephen M. Cameron 
6662a0c12413SStephen M. Cameron 	now = get_jiffies_64();
6663a0c12413SStephen M. Cameron 	/* If we've received an interrupt recently, we're ok. */
6664a0c12413SStephen M. Cameron 	if (time_after64(h->last_intr_timestamp +
6665e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
6666a0c12413SStephen M. Cameron 		return;
6667a0c12413SStephen M. Cameron 
6668a0c12413SStephen M. Cameron 	/*
6669a0c12413SStephen M. Cameron 	 * If we've already checked the heartbeat recently, we're ok.
6670a0c12413SStephen M. Cameron 	 * This could happen if someone sends us a signal. We
6671a0c12413SStephen M. Cameron 	 * otherwise don't care about signals in this thread.
6672a0c12413SStephen M. Cameron 	 */
6673a0c12413SStephen M. Cameron 	if (time_after64(h->last_heartbeat_timestamp +
6674e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
6675a0c12413SStephen M. Cameron 		return;
6676a0c12413SStephen M. Cameron 
6677a0c12413SStephen M. Cameron 	/* If heartbeat has not changed since we last looked, we're not ok. */
6678a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
6679a0c12413SStephen M. Cameron 	heartbeat = readl(&h->cfgtable->HeartBeat);
6680a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
6681a0c12413SStephen M. Cameron 	if (h->last_heartbeat == heartbeat) {
6682a0c12413SStephen M. Cameron 		controller_lockup_detected(h);
6683a0c12413SStephen M. Cameron 		return;
6684a0c12413SStephen M. Cameron 	}
6685a0c12413SStephen M. Cameron 
6686a0c12413SStephen M. Cameron 	/* We're ok. */
6687a0c12413SStephen M. Cameron 	h->last_heartbeat = heartbeat;
6688a0c12413SStephen M. Cameron 	h->last_heartbeat_timestamp = now;
6689a0c12413SStephen M. Cameron }
6690a0c12413SStephen M. Cameron 
6691*9846590eSStephen M. Cameron static void hpsa_ack_ctlr_events(struct ctlr_info *h)
669276438d08SStephen M. Cameron {
669376438d08SStephen M. Cameron 	int i;
669476438d08SStephen M. Cameron 	char *event_type;
669576438d08SStephen M. Cameron 
6696e863d68eSScott Teel 	/* Clear the driver-requested rescan flag */
6697e863d68eSScott Teel 	h->drv_req_rescan = 0;
6698e863d68eSScott Teel 
669976438d08SStephen M. Cameron 	/* Ask the controller to clear the events we're handling. */
67001f7cee8cSStephen M. Cameron 	if ((h->transMethod & (CFGTBL_Trans_io_accel1
67011f7cee8cSStephen M. Cameron 			| CFGTBL_Trans_io_accel2)) &&
670276438d08SStephen M. Cameron 		(h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
670376438d08SStephen M. Cameron 		 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
670476438d08SStephen M. Cameron 
670576438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
670676438d08SStephen M. Cameron 			event_type = "state change";
670776438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
670876438d08SStephen M. Cameron 			event_type = "configuration change";
670976438d08SStephen M. Cameron 		/* Stop sending new RAID offload reqs via the IO accelerator */
671076438d08SStephen M. Cameron 		scsi_block_requests(h->scsi_host);
671176438d08SStephen M. Cameron 		for (i = 0; i < h->ndevices; i++)
671276438d08SStephen M. Cameron 			h->dev[i]->offload_enabled = 0;
671323100dd9SStephen M. Cameron 		hpsa_drain_accel_commands(h);
671476438d08SStephen M. Cameron 		/* Set 'accelerator path config change' bit */
671576438d08SStephen M. Cameron 		dev_warn(&h->pdev->dev,
671676438d08SStephen M. Cameron 			"Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
671776438d08SStephen M. Cameron 			h->events, event_type);
671876438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
671976438d08SStephen M. Cameron 		/* Set the "clear event notify field update" bit 6 */
672076438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
672176438d08SStephen M. Cameron 		/* Wait until ctlr clears 'clear event notify field', bit 6 */
672276438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
672376438d08SStephen M. Cameron 		scsi_unblock_requests(h->scsi_host);
672476438d08SStephen M. Cameron 	} else {
672576438d08SStephen M. Cameron 		/* Acknowledge controller notification events. */
672676438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
672776438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
672876438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
672976438d08SStephen M. Cameron #if 0
673076438d08SStephen M. Cameron 		writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
673176438d08SStephen M. Cameron 		hpsa_wait_for_mode_change_ack(h);
673276438d08SStephen M. Cameron #endif
673376438d08SStephen M. Cameron 	}
6734*9846590eSStephen M. Cameron 	return;
673576438d08SStephen M. Cameron }
673676438d08SStephen M. Cameron 
673776438d08SStephen M. Cameron /* Check a register on the controller to see if there are configuration
673876438d08SStephen M. Cameron  * changes (added/changed/removed logical drives, etc.) which mean that
6739e863d68eSScott Teel  * we should rescan the controller for devices.
6740e863d68eSScott Teel  * Also check flag for driver-initiated rescan.
674176438d08SStephen M. Cameron  */
6742*9846590eSStephen M. Cameron static int hpsa_ctlr_needs_rescan(struct ctlr_info *h)
674376438d08SStephen M. Cameron {
6744*9846590eSStephen M. Cameron 	if (h->drv_req_rescan)
6745*9846590eSStephen M. Cameron 		return 1;
6746*9846590eSStephen M. Cameron 
674776438d08SStephen M. Cameron 	if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
6748*9846590eSStephen M. Cameron 		return 0;
674976438d08SStephen M. Cameron 
675076438d08SStephen M. Cameron 	h->events = readl(&(h->cfgtable->event_notify));
6751*9846590eSStephen M. Cameron 	return h->events & RESCAN_REQUIRED_EVENT_BITS;
6752*9846590eSStephen M. Cameron }
675376438d08SStephen M. Cameron 
675476438d08SStephen M. Cameron /*
6755*9846590eSStephen M. Cameron  * Check if any of the offline devices have become ready
675676438d08SStephen M. Cameron  */
6757*9846590eSStephen M. Cameron static int hpsa_offline_devices_ready(struct ctlr_info *h)
6758*9846590eSStephen M. Cameron {
6759*9846590eSStephen M. Cameron 	unsigned long flags;
6760*9846590eSStephen M. Cameron 	struct offline_device_entry *d;
6761*9846590eSStephen M. Cameron 	struct list_head *this, *tmp;
6762*9846590eSStephen M. Cameron 
6763*9846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
6764*9846590eSStephen M. Cameron 	list_for_each_safe(this, tmp, &h->offline_device_list) {
6765*9846590eSStephen M. Cameron 		d = list_entry(this, struct offline_device_entry,
6766*9846590eSStephen M. Cameron 				offline_list);
6767*9846590eSStephen M. Cameron 		spin_unlock_irqrestore(&h->offline_device_lock, flags);
6768*9846590eSStephen M. Cameron 		if (!hpsa_volume_offline(h, d->scsi3addr))
6769*9846590eSStephen M. Cameron 			return 1;
6770*9846590eSStephen M. Cameron 		spin_lock_irqsave(&h->offline_device_lock, flags);
677176438d08SStephen M. Cameron 	}
6772*9846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
6773*9846590eSStephen M. Cameron 	return 0;
6774*9846590eSStephen M. Cameron }
6775*9846590eSStephen M. Cameron 
677676438d08SStephen M. Cameron 
67778a98db73SStephen M. Cameron static void hpsa_monitor_ctlr_worker(struct work_struct *work)
6778a0c12413SStephen M. Cameron {
6779a0c12413SStephen M. Cameron 	unsigned long flags;
67808a98db73SStephen M. Cameron 	struct ctlr_info *h = container_of(to_delayed_work(work),
67818a98db73SStephen M. Cameron 					struct ctlr_info, monitor_ctlr_work);
6782a0c12413SStephen M. Cameron 	detect_controller_lockup(h);
67838a98db73SStephen M. Cameron 	if (h->lockup_detected)
67848a98db73SStephen M. Cameron 		return;
6785*9846590eSStephen M. Cameron 
6786*9846590eSStephen M. Cameron 	if (hpsa_ctlr_needs_rescan(h) || hpsa_offline_devices_ready(h)) {
6787*9846590eSStephen M. Cameron 		scsi_host_get(h->scsi_host);
6788*9846590eSStephen M. Cameron 		h->drv_req_rescan = 0;
6789*9846590eSStephen M. Cameron 		hpsa_ack_ctlr_events(h);
6790*9846590eSStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
6791*9846590eSStephen M. Cameron 		scsi_host_put(h->scsi_host);
6792*9846590eSStephen M. Cameron 	}
6793*9846590eSStephen M. Cameron 
67948a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
67958a98db73SStephen M. Cameron 	if (h->remove_in_progress) {
67968a98db73SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
6797a0c12413SStephen M. Cameron 		return;
6798a0c12413SStephen M. Cameron 	}
67998a98db73SStephen M. Cameron 	schedule_delayed_work(&h->monitor_ctlr_work,
68008a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
68018a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
6802a0c12413SStephen M. Cameron }
6803a0c12413SStephen M. Cameron 
68046f039790SGreg Kroah-Hartman static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
68054c2a8c40SStephen M. Cameron {
68064c2a8c40SStephen M. Cameron 	int dac, rc;
68074c2a8c40SStephen M. Cameron 	struct ctlr_info *h;
680864670ac8SStephen M. Cameron 	int try_soft_reset = 0;
680964670ac8SStephen M. Cameron 	unsigned long flags;
68104c2a8c40SStephen M. Cameron 
68114c2a8c40SStephen M. Cameron 	if (number_of_controllers == 0)
68124c2a8c40SStephen M. Cameron 		printk(KERN_INFO DRIVER_NAME "\n");
68134c2a8c40SStephen M. Cameron 
68144c2a8c40SStephen M. Cameron 	rc = hpsa_init_reset_devices(pdev);
681564670ac8SStephen M. Cameron 	if (rc) {
681664670ac8SStephen M. Cameron 		if (rc != -ENOTSUPP)
68174c2a8c40SStephen M. Cameron 			return rc;
681864670ac8SStephen M. Cameron 		/* If the reset fails in a particular way (it has no way to do
681964670ac8SStephen M. Cameron 		 * a proper hard reset, so returns -ENOTSUPP) we can try to do
682064670ac8SStephen M. Cameron 		 * a soft reset once we get the controller configured up to the
682164670ac8SStephen M. Cameron 		 * point that it can accept a command.
682264670ac8SStephen M. Cameron 		 */
682364670ac8SStephen M. Cameron 		try_soft_reset = 1;
682464670ac8SStephen M. Cameron 		rc = 0;
682564670ac8SStephen M. Cameron 	}
682664670ac8SStephen M. Cameron 
682764670ac8SStephen M. Cameron reinit_after_soft_reset:
68284c2a8c40SStephen M. Cameron 
6829303932fdSDon Brace 	/* Command structures must be aligned on a 32-byte boundary because
6830303932fdSDon Brace 	 * the 5 lower bits of the address are used by the hardware. and by
6831303932fdSDon Brace 	 * the driver.  See comments in hpsa.h for more info.
6832303932fdSDon Brace 	 */
6833283b4a9bSStephen M. Cameron #define COMMANDLIST_ALIGNMENT 128
6834303932fdSDon Brace 	BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
6835edd16368SStephen M. Cameron 	h = kzalloc(sizeof(*h), GFP_KERNEL);
6836edd16368SStephen M. Cameron 	if (!h)
6837ecd9aad4SStephen M. Cameron 		return -ENOMEM;
6838edd16368SStephen M. Cameron 
683955c06c71SStephen M. Cameron 	h->pdev = pdev;
6840a9a3a273SStephen M. Cameron 	h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
68419e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&h->cmpQ);
68429e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&h->reqQ);
6843*9846590eSStephen M. Cameron 	INIT_LIST_HEAD(&h->offline_device_list);
68446eaf46fdSStephen M. Cameron 	spin_lock_init(&h->lock);
6845*9846590eSStephen M. Cameron 	spin_lock_init(&h->offline_device_lock);
68466eaf46fdSStephen M. Cameron 	spin_lock_init(&h->scan_lock);
68470390f0c0SStephen M. Cameron 	spin_lock_init(&h->passthru_count_lock);
684855c06c71SStephen M. Cameron 	rc = hpsa_pci_init(h);
6849ecd9aad4SStephen M. Cameron 	if (rc != 0)
6850edd16368SStephen M. Cameron 		goto clean1;
6851edd16368SStephen M. Cameron 
6852f79cfec6SStephen M. Cameron 	sprintf(h->devname, HPSA "%d", number_of_controllers);
6853edd16368SStephen M. Cameron 	h->ctlr = number_of_controllers;
6854edd16368SStephen M. Cameron 	number_of_controllers++;
6855edd16368SStephen M. Cameron 
6856edd16368SStephen M. Cameron 	/* configure PCI DMA stuff */
6857ecd9aad4SStephen M. Cameron 	rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
6858ecd9aad4SStephen M. Cameron 	if (rc == 0) {
6859edd16368SStephen M. Cameron 		dac = 1;
6860ecd9aad4SStephen M. Cameron 	} else {
6861ecd9aad4SStephen M. Cameron 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6862ecd9aad4SStephen M. Cameron 		if (rc == 0) {
6863edd16368SStephen M. Cameron 			dac = 0;
6864ecd9aad4SStephen M. Cameron 		} else {
6865edd16368SStephen M. Cameron 			dev_err(&pdev->dev, "no suitable DMA available\n");
6866edd16368SStephen M. Cameron 			goto clean1;
6867edd16368SStephen M. Cameron 		}
6868ecd9aad4SStephen M. Cameron 	}
6869edd16368SStephen M. Cameron 
6870edd16368SStephen M. Cameron 	/* make sure the board interrupts are off */
6871edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
687210f66018SStephen M. Cameron 
68730ae01a32SStephen M. Cameron 	if (hpsa_request_irq(h, do_hpsa_intr_msi, do_hpsa_intr_intx))
6874edd16368SStephen M. Cameron 		goto clean2;
6875303932fdSDon Brace 	dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
6876303932fdSDon Brace 	       h->devname, pdev->device,
6877a9a3a273SStephen M. Cameron 	       h->intr[h->intr_mode], dac ? "" : " not");
68782e9d1b36SStephen M. Cameron 	if (hpsa_allocate_cmd_pool(h))
6879edd16368SStephen M. Cameron 		goto clean4;
688033a2ffceSStephen M. Cameron 	if (hpsa_allocate_sg_chain_blocks(h))
688133a2ffceSStephen M. Cameron 		goto clean4;
6882a08a8471SStephen M. Cameron 	init_waitqueue_head(&h->scan_wait_queue);
6883a08a8471SStephen M. Cameron 	h->scan_finished = 1; /* no scan currently in progress */
6884edd16368SStephen M. Cameron 
6885edd16368SStephen M. Cameron 	pci_set_drvdata(pdev, h);
68869a41338eSStephen M. Cameron 	h->ndevices = 0;
68879a41338eSStephen M. Cameron 	h->scsi_host = NULL;
68889a41338eSStephen M. Cameron 	spin_lock_init(&h->devlock);
688964670ac8SStephen M. Cameron 	hpsa_put_ctlr_into_performant_mode(h);
689064670ac8SStephen M. Cameron 
689164670ac8SStephen M. Cameron 	/* At this point, the controller is ready to take commands.
689264670ac8SStephen M. Cameron 	 * Now, if reset_devices and the hard reset didn't work, try
689364670ac8SStephen M. Cameron 	 * the soft reset and see if that works.
689464670ac8SStephen M. Cameron 	 */
689564670ac8SStephen M. Cameron 	if (try_soft_reset) {
689664670ac8SStephen M. Cameron 
689764670ac8SStephen M. Cameron 		/* This is kind of gross.  We may or may not get a completion
689864670ac8SStephen M. Cameron 		 * from the soft reset command, and if we do, then the value
689964670ac8SStephen M. Cameron 		 * from the fifo may or may not be valid.  So, we wait 10 secs
690064670ac8SStephen M. Cameron 		 * after the reset throwing away any completions we get during
690164670ac8SStephen M. Cameron 		 * that time.  Unregister the interrupt handler and register
690264670ac8SStephen M. Cameron 		 * fake ones to scoop up any residual completions.
690364670ac8SStephen M. Cameron 		 */
690464670ac8SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
690564670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
690664670ac8SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
6907254f796bSMatt Gates 		free_irqs(h);
690864670ac8SStephen M. Cameron 		rc = hpsa_request_irq(h, hpsa_msix_discard_completions,
690964670ac8SStephen M. Cameron 					hpsa_intx_discard_completions);
691064670ac8SStephen M. Cameron 		if (rc) {
691164670ac8SStephen M. Cameron 			dev_warn(&h->pdev->dev, "Failed to request_irq after "
691264670ac8SStephen M. Cameron 				"soft reset.\n");
691364670ac8SStephen M. Cameron 			goto clean4;
691464670ac8SStephen M. Cameron 		}
691564670ac8SStephen M. Cameron 
691664670ac8SStephen M. Cameron 		rc = hpsa_kdump_soft_reset(h);
691764670ac8SStephen M. Cameron 		if (rc)
691864670ac8SStephen M. Cameron 			/* Neither hard nor soft reset worked, we're hosed. */
691964670ac8SStephen M. Cameron 			goto clean4;
692064670ac8SStephen M. Cameron 
692164670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev, "Board READY.\n");
692264670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev,
692364670ac8SStephen M. Cameron 			"Waiting for stale completions to drain.\n");
692464670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_ON);
692564670ac8SStephen M. Cameron 		msleep(10000);
692664670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
692764670ac8SStephen M. Cameron 
692864670ac8SStephen M. Cameron 		rc = controller_reset_failed(h->cfgtable);
692964670ac8SStephen M. Cameron 		if (rc)
693064670ac8SStephen M. Cameron 			dev_info(&h->pdev->dev,
693164670ac8SStephen M. Cameron 				"Soft reset appears to have failed.\n");
693264670ac8SStephen M. Cameron 
693364670ac8SStephen M. Cameron 		/* since the controller's reset, we have to go back and re-init
693464670ac8SStephen M. Cameron 		 * everything.  Easiest to just forget what we've done and do it
693564670ac8SStephen M. Cameron 		 * all over again.
693664670ac8SStephen M. Cameron 		 */
693764670ac8SStephen M. Cameron 		hpsa_undo_allocations_after_kdump_soft_reset(h);
693864670ac8SStephen M. Cameron 		try_soft_reset = 0;
693964670ac8SStephen M. Cameron 		if (rc)
694064670ac8SStephen M. Cameron 			/* don't go to clean4, we already unallocated */
694164670ac8SStephen M. Cameron 			return -ENODEV;
694264670ac8SStephen M. Cameron 
694364670ac8SStephen M. Cameron 		goto reinit_after_soft_reset;
694464670ac8SStephen M. Cameron 	}
6945edd16368SStephen M. Cameron 
6946da0697bdSScott Teel 	/* Enable Accelerated IO path at driver layer */
6947da0697bdSScott Teel 	h->acciopath_status = 1;
6948da0697bdSScott Teel 
6949e863d68eSScott Teel 	h->drv_req_rescan = 0;
6950e863d68eSScott Teel 
6951edd16368SStephen M. Cameron 	/* Turn the interrupts on so we can service requests */
6952edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_ON);
6953edd16368SStephen M. Cameron 
6954339b2b14SStephen M. Cameron 	hpsa_hba_inquiry(h);
6955edd16368SStephen M. Cameron 	hpsa_register_scsi(h);	/* hook ourselves into SCSI subsystem */
69568a98db73SStephen M. Cameron 
69578a98db73SStephen M. Cameron 	/* Monitor the controller for firmware lockups */
69588a98db73SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
69598a98db73SStephen M. Cameron 	INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
69608a98db73SStephen M. Cameron 	schedule_delayed_work(&h->monitor_ctlr_work,
69618a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
696288bf6d62SStephen M. Cameron 	return 0;
6963edd16368SStephen M. Cameron 
6964edd16368SStephen M. Cameron clean4:
696533a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
69662e9d1b36SStephen M. Cameron 	hpsa_free_cmd_pool(h);
6967254f796bSMatt Gates 	free_irqs(h);
6968edd16368SStephen M. Cameron clean2:
6969edd16368SStephen M. Cameron clean1:
6970edd16368SStephen M. Cameron 	kfree(h);
6971ecd9aad4SStephen M. Cameron 	return rc;
6972edd16368SStephen M. Cameron }
6973edd16368SStephen M. Cameron 
6974edd16368SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h)
6975edd16368SStephen M. Cameron {
6976edd16368SStephen M. Cameron 	char *flush_buf;
6977edd16368SStephen M. Cameron 	struct CommandList *c;
6978702890e3SStephen M. Cameron 	unsigned long flags;
6979702890e3SStephen M. Cameron 
6980702890e3SStephen M. Cameron 	/* Don't bother trying to flush the cache if locked up */
6981702890e3SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
6982702890e3SStephen M. Cameron 	if (unlikely(h->lockup_detected)) {
6983702890e3SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
6984702890e3SStephen M. Cameron 		return;
6985702890e3SStephen M. Cameron 	}
6986702890e3SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
6987edd16368SStephen M. Cameron 
6988edd16368SStephen M. Cameron 	flush_buf = kzalloc(4, GFP_KERNEL);
6989edd16368SStephen M. Cameron 	if (!flush_buf)
6990edd16368SStephen M. Cameron 		return;
6991edd16368SStephen M. Cameron 
6992edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
6993edd16368SStephen M. Cameron 	if (!c) {
6994edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
6995edd16368SStephen M. Cameron 		goto out_of_memory;
6996edd16368SStephen M. Cameron 	}
6997a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
6998a2dac136SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_CMD)) {
6999a2dac136SStephen M. Cameron 		goto out;
7000a2dac136SStephen M. Cameron 	}
7001edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
7002edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus != 0)
7003a2dac136SStephen M. Cameron out:
7004edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
7005edd16368SStephen M. Cameron 			"error flushing cache on controller\n");
7006edd16368SStephen M. Cameron 	cmd_special_free(h, c);
7007edd16368SStephen M. Cameron out_of_memory:
7008edd16368SStephen M. Cameron 	kfree(flush_buf);
7009edd16368SStephen M. Cameron }
7010edd16368SStephen M. Cameron 
7011edd16368SStephen M. Cameron static void hpsa_shutdown(struct pci_dev *pdev)
7012edd16368SStephen M. Cameron {
7013edd16368SStephen M. Cameron 	struct ctlr_info *h;
7014edd16368SStephen M. Cameron 
7015edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
7016edd16368SStephen M. Cameron 	/* Turn board interrupts off  and send the flush cache command
7017edd16368SStephen M. Cameron 	 * sendcmd will turn off interrupt, and send the flush...
7018edd16368SStephen M. Cameron 	 * To write all data in the battery backed cache to disks
7019edd16368SStephen M. Cameron 	 */
7020edd16368SStephen M. Cameron 	hpsa_flush_cache(h);
7021edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
70220097f0f4SStephen M. Cameron 	hpsa_free_irqs_and_disable_msix(h);
7023edd16368SStephen M. Cameron }
7024edd16368SStephen M. Cameron 
70256f039790SGreg Kroah-Hartman static void hpsa_free_device_info(struct ctlr_info *h)
702655e14e76SStephen M. Cameron {
702755e14e76SStephen M. Cameron 	int i;
702855e14e76SStephen M. Cameron 
702955e14e76SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++)
703055e14e76SStephen M. Cameron 		kfree(h->dev[i]);
703155e14e76SStephen M. Cameron }
703255e14e76SStephen M. Cameron 
70336f039790SGreg Kroah-Hartman static void hpsa_remove_one(struct pci_dev *pdev)
7034edd16368SStephen M. Cameron {
7035edd16368SStephen M. Cameron 	struct ctlr_info *h;
70368a98db73SStephen M. Cameron 	unsigned long flags;
7037edd16368SStephen M. Cameron 
7038edd16368SStephen M. Cameron 	if (pci_get_drvdata(pdev) == NULL) {
7039edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "unable to remove device\n");
7040edd16368SStephen M. Cameron 		return;
7041edd16368SStephen M. Cameron 	}
7042edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
70438a98db73SStephen M. Cameron 
70448a98db73SStephen M. Cameron 	/* Get rid of any controller monitoring work items */
70458a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
70468a98db73SStephen M. Cameron 	h->remove_in_progress = 1;
70478a98db73SStephen M. Cameron 	cancel_delayed_work(&h->monitor_ctlr_work);
70488a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
70498a98db73SStephen M. Cameron 
7050edd16368SStephen M. Cameron 	hpsa_unregister_scsi(h);	/* unhook from SCSI subsystem */
7051edd16368SStephen M. Cameron 	hpsa_shutdown(pdev);
7052edd16368SStephen M. Cameron 	iounmap(h->vaddr);
7053204892e9SStephen M. Cameron 	iounmap(h->transtable);
7054204892e9SStephen M. Cameron 	iounmap(h->cfgtable);
705555e14e76SStephen M. Cameron 	hpsa_free_device_info(h);
705633a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
7057edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev,
7058edd16368SStephen M. Cameron 		h->nr_cmds * sizeof(struct CommandList),
7059edd16368SStephen M. Cameron 		h->cmd_pool, h->cmd_pool_dhandle);
7060edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev,
7061edd16368SStephen M. Cameron 		h->nr_cmds * sizeof(struct ErrorInfo),
7062edd16368SStephen M. Cameron 		h->errinfo_pool, h->errinfo_pool_dhandle);
7063303932fdSDon Brace 	pci_free_consistent(h->pdev, h->reply_pool_size,
7064303932fdSDon Brace 		h->reply_pool, h->reply_pool_dhandle);
7065edd16368SStephen M. Cameron 	kfree(h->cmd_pool_bits);
7066303932fdSDon Brace 	kfree(h->blockFetchTable);
7067e1f7de0cSMatt Gates 	kfree(h->ioaccel1_blockFetchTable);
7068aca9012aSStephen M. Cameron 	kfree(h->ioaccel2_blockFetchTable);
7069339b2b14SStephen M. Cameron 	kfree(h->hba_inquiry_data);
7070f0bd0b68SStephen M. Cameron 	pci_disable_device(pdev);
7071edd16368SStephen M. Cameron 	pci_release_regions(pdev);
7072edd16368SStephen M. Cameron 	kfree(h);
7073edd16368SStephen M. Cameron }
7074edd16368SStephen M. Cameron 
7075edd16368SStephen M. Cameron static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
7076edd16368SStephen M. Cameron 	__attribute__((unused)) pm_message_t state)
7077edd16368SStephen M. Cameron {
7078edd16368SStephen M. Cameron 	return -ENOSYS;
7079edd16368SStephen M. Cameron }
7080edd16368SStephen M. Cameron 
7081edd16368SStephen M. Cameron static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
7082edd16368SStephen M. Cameron {
7083edd16368SStephen M. Cameron 	return -ENOSYS;
7084edd16368SStephen M. Cameron }
7085edd16368SStephen M. Cameron 
7086edd16368SStephen M. Cameron static struct pci_driver hpsa_pci_driver = {
7087f79cfec6SStephen M. Cameron 	.name = HPSA,
7088edd16368SStephen M. Cameron 	.probe = hpsa_init_one,
70896f039790SGreg Kroah-Hartman 	.remove = hpsa_remove_one,
7090edd16368SStephen M. Cameron 	.id_table = hpsa_pci_device_id,	/* id_table */
7091edd16368SStephen M. Cameron 	.shutdown = hpsa_shutdown,
7092edd16368SStephen M. Cameron 	.suspend = hpsa_suspend,
7093edd16368SStephen M. Cameron 	.resume = hpsa_resume,
7094edd16368SStephen M. Cameron };
7095edd16368SStephen M. Cameron 
7096303932fdSDon Brace /* Fill in bucket_map[], given nsgs (the max number of
7097303932fdSDon Brace  * scatter gather elements supported) and bucket[],
7098303932fdSDon Brace  * which is an array of 8 integers.  The bucket[] array
7099303932fdSDon Brace  * contains 8 different DMA transfer sizes (in 16
7100303932fdSDon Brace  * byte increments) which the controller uses to fetch
7101303932fdSDon Brace  * commands.  This function fills in bucket_map[], which
7102303932fdSDon Brace  * maps a given number of scatter gather elements to one of
7103303932fdSDon Brace  * the 8 DMA transfer sizes.  The point of it is to allow the
7104303932fdSDon Brace  * controller to only do as much DMA as needed to fetch the
7105303932fdSDon Brace  * command, with the DMA transfer size encoded in the lower
7106303932fdSDon Brace  * bits of the command address.
7107303932fdSDon Brace  */
7108303932fdSDon Brace static void  calc_bucket_map(int bucket[], int num_buckets,
7109e1f7de0cSMatt Gates 	int nsgs, int min_blocks, int *bucket_map)
7110303932fdSDon Brace {
7111303932fdSDon Brace 	int i, j, b, size;
7112303932fdSDon Brace 
7113303932fdSDon Brace 	/* Note, bucket_map must have nsgs+1 entries. */
7114303932fdSDon Brace 	for (i = 0; i <= nsgs; i++) {
7115303932fdSDon Brace 		/* Compute size of a command with i SG entries */
7116e1f7de0cSMatt Gates 		size = i + min_blocks;
7117303932fdSDon Brace 		b = num_buckets; /* Assume the biggest bucket */
7118303932fdSDon Brace 		/* Find the bucket that is just big enough */
7119e1f7de0cSMatt Gates 		for (j = 0; j < num_buckets; j++) {
7120303932fdSDon Brace 			if (bucket[j] >= size) {
7121303932fdSDon Brace 				b = j;
7122303932fdSDon Brace 				break;
7123303932fdSDon Brace 			}
7124303932fdSDon Brace 		}
7125303932fdSDon Brace 		/* for a command with i SG entries, use bucket b. */
7126303932fdSDon Brace 		bucket_map[i] = b;
7127303932fdSDon Brace 	}
7128303932fdSDon Brace }
7129303932fdSDon Brace 
7130e1f7de0cSMatt Gates static void hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
7131303932fdSDon Brace {
71326c311b57SStephen M. Cameron 	int i;
71336c311b57SStephen M. Cameron 	unsigned long register_value;
7134e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
7135e1f7de0cSMatt Gates 			(trans_support & CFGTBL_Trans_use_short_tags) |
7136e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix |
7137b9af4937SStephen M. Cameron 			(trans_support & (CFGTBL_Trans_io_accel1 |
7138b9af4937SStephen M. Cameron 				CFGTBL_Trans_io_accel2));
7139e1f7de0cSMatt Gates 	struct access_method access = SA5_performant_access;
7140def342bdSStephen M. Cameron 
7141def342bdSStephen M. Cameron 	/* This is a bit complicated.  There are 8 registers on
7142def342bdSStephen M. Cameron 	 * the controller which we write to to tell it 8 different
7143def342bdSStephen M. Cameron 	 * sizes of commands which there may be.  It's a way of
7144def342bdSStephen M. Cameron 	 * reducing the DMA done to fetch each command.  Encoded into
7145def342bdSStephen M. Cameron 	 * each command's tag are 3 bits which communicate to the controller
7146def342bdSStephen M. Cameron 	 * which of the eight sizes that command fits within.  The size of
7147def342bdSStephen M. Cameron 	 * each command depends on how many scatter gather entries there are.
7148def342bdSStephen M. Cameron 	 * Each SG entry requires 16 bytes.  The eight registers are programmed
7149def342bdSStephen M. Cameron 	 * with the number of 16-byte blocks a command of that size requires.
7150def342bdSStephen M. Cameron 	 * The smallest command possible requires 5 such 16 byte blocks.
7151d66ae08bSStephen M. Cameron 	 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
7152def342bdSStephen M. Cameron 	 * blocks.  Note, this only extends to the SG entries contained
7153def342bdSStephen M. Cameron 	 * within the command block, and does not extend to chained blocks
7154def342bdSStephen M. Cameron 	 * of SG elements.   bft[] contains the eight values we write to
7155def342bdSStephen M. Cameron 	 * the registers.  They are not evenly distributed, but have more
7156def342bdSStephen M. Cameron 	 * sizes for small commands, and fewer sizes for larger commands.
7157def342bdSStephen M. Cameron 	 */
7158d66ae08bSStephen M. Cameron 	int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
7159b9af4937SStephen M. Cameron #define MIN_IOACCEL2_BFT_ENTRY 5
7160b9af4937SStephen M. Cameron #define HPSA_IOACCEL2_HEADER_SZ 4
7161b9af4937SStephen M. Cameron 	int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
7162b9af4937SStephen M. Cameron 			13, 14, 15, 16, 17, 18, 19,
7163b9af4937SStephen M. Cameron 			HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
7164b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
7165b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
7166b9af4937SStephen M. Cameron 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
7167b9af4937SStephen M. Cameron 				 16 * MIN_IOACCEL2_BFT_ENTRY);
7168b9af4937SStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
7169d66ae08bSStephen M. Cameron 	BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
7170303932fdSDon Brace 	/*  5 = 1 s/g entry or 4k
7171303932fdSDon Brace 	 *  6 = 2 s/g entry or 8k
7172303932fdSDon Brace 	 *  8 = 4 s/g entry or 16k
7173303932fdSDon Brace 	 * 10 = 6 s/g entry or 24k
7174303932fdSDon Brace 	 */
7175303932fdSDon Brace 
7176303932fdSDon Brace 	/* Controller spec: zero out this buffer. */
7177303932fdSDon Brace 	memset(h->reply_pool, 0, h->reply_pool_size);
7178303932fdSDon Brace 
7179d66ae08bSStephen M. Cameron 	bft[7] = SG_ENTRIES_IN_CMD + 4;
7180d66ae08bSStephen M. Cameron 	calc_bucket_map(bft, ARRAY_SIZE(bft),
7181e1f7de0cSMatt Gates 				SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
7182303932fdSDon Brace 	for (i = 0; i < 8; i++)
7183303932fdSDon Brace 		writel(bft[i], &h->transtable->BlockFetch[i]);
7184303932fdSDon Brace 
7185303932fdSDon Brace 	/* size of controller ring buffer */
7186303932fdSDon Brace 	writel(h->max_commands, &h->transtable->RepQSize);
7187254f796bSMatt Gates 	writel(h->nreply_queues, &h->transtable->RepQCount);
7188303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrLow32);
7189303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrHigh32);
7190254f796bSMatt Gates 
7191254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
7192254f796bSMatt Gates 		writel(0, &h->transtable->RepQAddr[i].upper);
7193254f796bSMatt Gates 		writel(h->reply_pool_dhandle +
7194254f796bSMatt Gates 			(h->max_commands * sizeof(u64) * i),
7195254f796bSMatt Gates 			&h->transtable->RepQAddr[i].lower);
7196254f796bSMatt Gates 	}
7197254f796bSMatt Gates 
7198b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
7199e1f7de0cSMatt Gates 	writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
7200e1f7de0cSMatt Gates 	/*
7201e1f7de0cSMatt Gates 	 * enable outbound interrupt coalescing in accelerator mode;
7202e1f7de0cSMatt Gates 	 */
7203e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
7204e1f7de0cSMatt Gates 		access = SA5_ioaccel_mode1_access;
7205e1f7de0cSMatt Gates 		writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
7206e1f7de0cSMatt Gates 		writel(4, &h->cfgtable->HostWrite.CoalIntCount);
7207c349775eSScott Teel 	} else {
7208c349775eSScott Teel 		if (trans_support & CFGTBL_Trans_io_accel2) {
7209c349775eSScott Teel 			access = SA5_ioaccel_mode2_access;
7210c349775eSScott Teel 			writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
7211c349775eSScott Teel 			writel(4, &h->cfgtable->HostWrite.CoalIntCount);
7212c349775eSScott Teel 		}
7213e1f7de0cSMatt Gates 	}
7214303932fdSDon Brace 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
72153f4336f3SStephen M. Cameron 	hpsa_wait_for_mode_change_ack(h);
7216303932fdSDon Brace 	register_value = readl(&(h->cfgtable->TransportActive));
7217303932fdSDon Brace 	if (!(register_value & CFGTBL_Trans_Performant)) {
7218303932fdSDon Brace 		dev_warn(&h->pdev->dev, "unable to get board into"
7219303932fdSDon Brace 					" performant mode\n");
7220303932fdSDon Brace 		return;
7221303932fdSDon Brace 	}
7222960a30e7SStephen M. Cameron 	/* Change the access methods to the performant access methods */
7223e1f7de0cSMatt Gates 	h->access = access;
7224e1f7de0cSMatt Gates 	h->transMethod = transMethod;
7225e1f7de0cSMatt Gates 
7226b9af4937SStephen M. Cameron 	if (!((trans_support & CFGTBL_Trans_io_accel1) ||
7227b9af4937SStephen M. Cameron 		(trans_support & CFGTBL_Trans_io_accel2)))
7228e1f7de0cSMatt Gates 		return;
7229e1f7de0cSMatt Gates 
7230b9af4937SStephen M. Cameron 	if (trans_support & CFGTBL_Trans_io_accel1) {
7231e1f7de0cSMatt Gates 		/* Set up I/O accelerator mode */
7232e1f7de0cSMatt Gates 		for (i = 0; i < h->nreply_queues; i++) {
7233e1f7de0cSMatt Gates 			writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
7234e1f7de0cSMatt Gates 			h->reply_queue[i].current_entry =
7235e1f7de0cSMatt Gates 				readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
7236e1f7de0cSMatt Gates 		}
7237283b4a9bSStephen M. Cameron 		bft[7] = h->ioaccel_maxsg + 8;
7238283b4a9bSStephen M. Cameron 		calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
7239e1f7de0cSMatt Gates 				h->ioaccel1_blockFetchTable);
7240e1f7de0cSMatt Gates 
7241e1f7de0cSMatt Gates 		/* initialize all reply queue entries to unused */
7242e1f7de0cSMatt Gates 		memset(h->reply_pool, (u8) IOACCEL_MODE1_REPLY_UNUSED,
7243e1f7de0cSMatt Gates 				h->reply_pool_size);
7244e1f7de0cSMatt Gates 
7245e1f7de0cSMatt Gates 		/* set all the constant fields in the accelerator command
7246e1f7de0cSMatt Gates 		 * frames once at init time to save CPU cycles later.
7247e1f7de0cSMatt Gates 		 */
7248e1f7de0cSMatt Gates 		for (i = 0; i < h->nr_cmds; i++) {
7249e1f7de0cSMatt Gates 			struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
7250e1f7de0cSMatt Gates 
7251e1f7de0cSMatt Gates 			cp->function = IOACCEL1_FUNCTION_SCSIIO;
7252e1f7de0cSMatt Gates 			cp->err_info = (u32) (h->errinfo_pool_dhandle +
7253e1f7de0cSMatt Gates 					(i * sizeof(struct ErrorInfo)));
7254e1f7de0cSMatt Gates 			cp->err_info_len = sizeof(struct ErrorInfo);
7255e1f7de0cSMatt Gates 			cp->sgl_offset = IOACCEL1_SGLOFFSET;
7256e1f7de0cSMatt Gates 			cp->host_context_flags = IOACCEL1_HCFLAGS_CISS_FORMAT;
7257e1f7de0cSMatt Gates 			cp->timeout_sec = 0;
7258e1f7de0cSMatt Gates 			cp->ReplyQueue = 0;
7259b9af4937SStephen M. Cameron 			cp->Tag.lower = (i << DIRECT_LOOKUP_SHIFT) |
7260b9af4937SStephen M. Cameron 						DIRECT_LOOKUP_BIT;
7261e1f7de0cSMatt Gates 			cp->Tag.upper = 0;
7262b9af4937SStephen M. Cameron 			cp->host_addr.lower =
7263b9af4937SStephen M. Cameron 				(u32) (h->ioaccel_cmd_pool_dhandle +
7264e1f7de0cSMatt Gates 					(i * sizeof(struct io_accel1_cmd)));
7265e1f7de0cSMatt Gates 			cp->host_addr.upper = 0;
7266e1f7de0cSMatt Gates 		}
7267b9af4937SStephen M. Cameron 	} else if (trans_support & CFGTBL_Trans_io_accel2) {
7268b9af4937SStephen M. Cameron 		u64 cfg_offset, cfg_base_addr_index;
7269b9af4937SStephen M. Cameron 		u32 bft2_offset, cfg_base_addr;
7270b9af4937SStephen M. Cameron 		int rc;
7271b9af4937SStephen M. Cameron 
7272b9af4937SStephen M. Cameron 		rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
7273b9af4937SStephen M. Cameron 			&cfg_base_addr_index, &cfg_offset);
7274b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
7275b9af4937SStephen M. Cameron 		bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
7276b9af4937SStephen M. Cameron 		calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
7277b9af4937SStephen M. Cameron 				4, h->ioaccel2_blockFetchTable);
7278b9af4937SStephen M. Cameron 		bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
7279b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct CfgTable,
7280b9af4937SStephen M. Cameron 				io_accel_request_size_offset) != 0xb8);
7281b9af4937SStephen M. Cameron 		h->ioaccel2_bft2_regs =
7282b9af4937SStephen M. Cameron 			remap_pci_mem(pci_resource_start(h->pdev,
7283b9af4937SStephen M. Cameron 					cfg_base_addr_index) +
7284b9af4937SStephen M. Cameron 					cfg_offset + bft2_offset,
7285b9af4937SStephen M. Cameron 					ARRAY_SIZE(bft2) *
7286b9af4937SStephen M. Cameron 					sizeof(*h->ioaccel2_bft2_regs));
7287b9af4937SStephen M. Cameron 		for (i = 0; i < ARRAY_SIZE(bft2); i++)
7288b9af4937SStephen M. Cameron 			writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
7289b9af4937SStephen M. Cameron 	}
7290b9af4937SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
7291b9af4937SStephen M. Cameron 	hpsa_wait_for_mode_change_ack(h);
7292e1f7de0cSMatt Gates }
7293e1f7de0cSMatt Gates 
7294e1f7de0cSMatt Gates static int hpsa_alloc_ioaccel_cmd_and_bft(struct ctlr_info *h)
7295e1f7de0cSMatt Gates {
7296283b4a9bSStephen M. Cameron 	h->ioaccel_maxsg =
7297283b4a9bSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
7298283b4a9bSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
7299283b4a9bSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
7300283b4a9bSStephen M. Cameron 
7301e1f7de0cSMatt Gates 	/* Command structures must be aligned on a 128-byte boundary
7302e1f7de0cSMatt Gates 	 * because the 7 lower bits of the address are used by the
7303e1f7de0cSMatt Gates 	 * hardware.
7304e1f7de0cSMatt Gates 	 */
7305e1f7de0cSMatt Gates #define IOACCEL1_COMMANDLIST_ALIGNMENT 128
7306e1f7de0cSMatt Gates 	BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
7307e1f7de0cSMatt Gates 			IOACCEL1_COMMANDLIST_ALIGNMENT);
7308e1f7de0cSMatt Gates 	h->ioaccel_cmd_pool =
7309e1f7de0cSMatt Gates 		pci_alloc_consistent(h->pdev,
7310e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
7311e1f7de0cSMatt Gates 			&(h->ioaccel_cmd_pool_dhandle));
7312e1f7de0cSMatt Gates 
7313e1f7de0cSMatt Gates 	h->ioaccel1_blockFetchTable =
7314283b4a9bSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
7315e1f7de0cSMatt Gates 				sizeof(u32)), GFP_KERNEL);
7316e1f7de0cSMatt Gates 
7317e1f7de0cSMatt Gates 	if ((h->ioaccel_cmd_pool == NULL) ||
7318e1f7de0cSMatt Gates 		(h->ioaccel1_blockFetchTable == NULL))
7319e1f7de0cSMatt Gates 		goto clean_up;
7320e1f7de0cSMatt Gates 
7321e1f7de0cSMatt Gates 	memset(h->ioaccel_cmd_pool, 0,
7322e1f7de0cSMatt Gates 		h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
7323e1f7de0cSMatt Gates 	return 0;
7324e1f7de0cSMatt Gates 
7325e1f7de0cSMatt Gates clean_up:
7326e1f7de0cSMatt Gates 	if (h->ioaccel_cmd_pool)
7327e1f7de0cSMatt Gates 		pci_free_consistent(h->pdev,
7328e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
7329e1f7de0cSMatt Gates 			h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
7330e1f7de0cSMatt Gates 	kfree(h->ioaccel1_blockFetchTable);
7331e1f7de0cSMatt Gates 	return 1;
73326c311b57SStephen M. Cameron }
73336c311b57SStephen M. Cameron 
7334aca9012aSStephen M. Cameron static int ioaccel2_alloc_cmds_and_bft(struct ctlr_info *h)
7335aca9012aSStephen M. Cameron {
7336aca9012aSStephen M. Cameron 	/* Allocate ioaccel2 mode command blocks and block fetch table */
7337aca9012aSStephen M. Cameron 
7338aca9012aSStephen M. Cameron 	h->ioaccel_maxsg =
7339aca9012aSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
7340aca9012aSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
7341aca9012aSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
7342aca9012aSStephen M. Cameron 
7343aca9012aSStephen M. Cameron #define IOACCEL2_COMMANDLIST_ALIGNMENT 128
7344aca9012aSStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
7345aca9012aSStephen M. Cameron 			IOACCEL2_COMMANDLIST_ALIGNMENT);
7346aca9012aSStephen M. Cameron 	h->ioaccel2_cmd_pool =
7347aca9012aSStephen M. Cameron 		pci_alloc_consistent(h->pdev,
7348aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
7349aca9012aSStephen M. Cameron 			&(h->ioaccel2_cmd_pool_dhandle));
7350aca9012aSStephen M. Cameron 
7351aca9012aSStephen M. Cameron 	h->ioaccel2_blockFetchTable =
7352aca9012aSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
7353aca9012aSStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
7354aca9012aSStephen M. Cameron 
7355aca9012aSStephen M. Cameron 	if ((h->ioaccel2_cmd_pool == NULL) ||
7356aca9012aSStephen M. Cameron 		(h->ioaccel2_blockFetchTable == NULL))
7357aca9012aSStephen M. Cameron 		goto clean_up;
7358aca9012aSStephen M. Cameron 
7359aca9012aSStephen M. Cameron 	memset(h->ioaccel2_cmd_pool, 0,
7360aca9012aSStephen M. Cameron 		h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
7361aca9012aSStephen M. Cameron 	return 0;
7362aca9012aSStephen M. Cameron 
7363aca9012aSStephen M. Cameron clean_up:
7364aca9012aSStephen M. Cameron 	if (h->ioaccel2_cmd_pool)
7365aca9012aSStephen M. Cameron 		pci_free_consistent(h->pdev,
7366aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
7367aca9012aSStephen M. Cameron 			h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
7368aca9012aSStephen M. Cameron 	kfree(h->ioaccel2_blockFetchTable);
7369aca9012aSStephen M. Cameron 	return 1;
7370aca9012aSStephen M. Cameron }
7371aca9012aSStephen M. Cameron 
73726f039790SGreg Kroah-Hartman static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
73736c311b57SStephen M. Cameron {
73746c311b57SStephen M. Cameron 	u32 trans_support;
7375e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
7376e1f7de0cSMatt Gates 					CFGTBL_Trans_use_short_tags;
7377254f796bSMatt Gates 	int i;
73786c311b57SStephen M. Cameron 
737902ec19c8SStephen M. Cameron 	if (hpsa_simple_mode)
738002ec19c8SStephen M. Cameron 		return;
738102ec19c8SStephen M. Cameron 
7382e1f7de0cSMatt Gates 	/* Check for I/O accelerator mode support */
7383e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
7384e1f7de0cSMatt Gates 		transMethod |= CFGTBL_Trans_io_accel1 |
7385e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix;
7386e1f7de0cSMatt Gates 		if (hpsa_alloc_ioaccel_cmd_and_bft(h))
7387e1f7de0cSMatt Gates 			goto clean_up;
7388aca9012aSStephen M. Cameron 	} else {
7389aca9012aSStephen M. Cameron 		if (trans_support & CFGTBL_Trans_io_accel2) {
7390aca9012aSStephen M. Cameron 				transMethod |= CFGTBL_Trans_io_accel2 |
7391aca9012aSStephen M. Cameron 				CFGTBL_Trans_enable_directed_msix;
7392aca9012aSStephen M. Cameron 		if (ioaccel2_alloc_cmds_and_bft(h))
7393aca9012aSStephen M. Cameron 			goto clean_up;
7394aca9012aSStephen M. Cameron 		}
7395e1f7de0cSMatt Gates 	}
7396e1f7de0cSMatt Gates 
7397e1f7de0cSMatt Gates 	/* TODO, check that this next line h->nreply_queues is correct */
73986c311b57SStephen M. Cameron 	trans_support = readl(&(h->cfgtable->TransportSupport));
73996c311b57SStephen M. Cameron 	if (!(trans_support & PERFORMANT_MODE))
74006c311b57SStephen M. Cameron 		return;
74016c311b57SStephen M. Cameron 
7402eee0f03aSHannes Reinecke 	h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
7403cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
74046c311b57SStephen M. Cameron 	/* Performant mode ring buffer and supporting data structures */
7405254f796bSMatt Gates 	h->reply_pool_size = h->max_commands * sizeof(u64) * h->nreply_queues;
74066c311b57SStephen M. Cameron 	h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size,
74076c311b57SStephen M. Cameron 				&(h->reply_pool_dhandle));
74086c311b57SStephen M. Cameron 
7409254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
7410254f796bSMatt Gates 		h->reply_queue[i].head = &h->reply_pool[h->max_commands * i];
7411254f796bSMatt Gates 		h->reply_queue[i].size = h->max_commands;
7412254f796bSMatt Gates 		h->reply_queue[i].wraparound = 1;  /* spec: init to 1 */
7413254f796bSMatt Gates 		h->reply_queue[i].current_entry = 0;
7414254f796bSMatt Gates 	}
7415254f796bSMatt Gates 
74166c311b57SStephen M. Cameron 	/* Need a block fetch table for performant mode */
7417d66ae08bSStephen M. Cameron 	h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
74186c311b57SStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
74196c311b57SStephen M. Cameron 
74206c311b57SStephen M. Cameron 	if ((h->reply_pool == NULL)
74216c311b57SStephen M. Cameron 		|| (h->blockFetchTable == NULL))
74226c311b57SStephen M. Cameron 		goto clean_up;
74236c311b57SStephen M. Cameron 
7424e1f7de0cSMatt Gates 	hpsa_enter_performant_mode(h, trans_support);
7425303932fdSDon Brace 	return;
7426303932fdSDon Brace 
7427303932fdSDon Brace clean_up:
7428303932fdSDon Brace 	if (h->reply_pool)
7429303932fdSDon Brace 		pci_free_consistent(h->pdev, h->reply_pool_size,
7430303932fdSDon Brace 			h->reply_pool, h->reply_pool_dhandle);
7431303932fdSDon Brace 	kfree(h->blockFetchTable);
7432303932fdSDon Brace }
7433303932fdSDon Brace 
743423100dd9SStephen M. Cameron static int is_accelerated_cmd(struct CommandList *c)
743576438d08SStephen M. Cameron {
743623100dd9SStephen M. Cameron 	return c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_IOACCEL2;
743723100dd9SStephen M. Cameron }
743823100dd9SStephen M. Cameron 
743923100dd9SStephen M. Cameron static void hpsa_drain_accel_commands(struct ctlr_info *h)
744023100dd9SStephen M. Cameron {
744123100dd9SStephen M. Cameron 	struct CommandList *c = NULL;
744276438d08SStephen M. Cameron 	unsigned long flags;
744323100dd9SStephen M. Cameron 	int accel_cmds_out;
744476438d08SStephen M. Cameron 
744576438d08SStephen M. Cameron 	do { /* wait for all outstanding commands to drain out */
744623100dd9SStephen M. Cameron 		accel_cmds_out = 0;
744776438d08SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
744823100dd9SStephen M. Cameron 		list_for_each_entry(c, &h->cmpQ, list)
744923100dd9SStephen M. Cameron 			accel_cmds_out += is_accelerated_cmd(c);
745023100dd9SStephen M. Cameron 		list_for_each_entry(c, &h->reqQ, list)
745123100dd9SStephen M. Cameron 			accel_cmds_out += is_accelerated_cmd(c);
745276438d08SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
745323100dd9SStephen M. Cameron 		if (accel_cmds_out <= 0)
745476438d08SStephen M. Cameron 			break;
745576438d08SStephen M. Cameron 		msleep(100);
745676438d08SStephen M. Cameron 	} while (1);
745776438d08SStephen M. Cameron }
745876438d08SStephen M. Cameron 
7459edd16368SStephen M. Cameron /*
7460edd16368SStephen M. Cameron  *  This is it.  Register the PCI driver information for the cards we control
7461edd16368SStephen M. Cameron  *  the OS will call our registered routines when it finds one of our cards.
7462edd16368SStephen M. Cameron  */
7463edd16368SStephen M. Cameron static int __init hpsa_init(void)
7464edd16368SStephen M. Cameron {
746531468401SMike Miller 	return pci_register_driver(&hpsa_pci_driver);
7466edd16368SStephen M. Cameron }
7467edd16368SStephen M. Cameron 
7468edd16368SStephen M. Cameron static void __exit hpsa_cleanup(void)
7469edd16368SStephen M. Cameron {
7470edd16368SStephen M. Cameron 	pci_unregister_driver(&hpsa_pci_driver);
7471edd16368SStephen M. Cameron }
7472edd16368SStephen M. Cameron 
7473e1f7de0cSMatt Gates static void __attribute__((unused)) verify_offsets(void)
7474e1f7de0cSMatt Gates {
7475e1f7de0cSMatt Gates #define VERIFY_OFFSET(member, offset) \
7476dd0e19f3SScott Teel 	BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
7477dd0e19f3SScott Teel 
7478dd0e19f3SScott Teel 	VERIFY_OFFSET(structure_size, 0);
7479dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_size, 4);
7480dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_cnt, 8);
7481dd0e19f3SScott Teel 	VERIFY_OFFSET(phys_blk_shift, 16);
7482dd0e19f3SScott Teel 	VERIFY_OFFSET(parity_rotation_shift, 17);
7483dd0e19f3SScott Teel 	VERIFY_OFFSET(strip_size, 18);
7484dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_starting_blk, 20);
7485dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_blk_cnt, 28);
7486dd0e19f3SScott Teel 	VERIFY_OFFSET(data_disks_per_row, 36);
7487dd0e19f3SScott Teel 	VERIFY_OFFSET(metadata_disks_per_row, 38);
7488dd0e19f3SScott Teel 	VERIFY_OFFSET(row_cnt, 40);
7489dd0e19f3SScott Teel 	VERIFY_OFFSET(layout_map_count, 42);
7490dd0e19f3SScott Teel 	VERIFY_OFFSET(flags, 44);
7491dd0e19f3SScott Teel 	VERIFY_OFFSET(dekindex, 46);
7492dd0e19f3SScott Teel 	/* VERIFY_OFFSET(reserved, 48 */
7493dd0e19f3SScott Teel 	VERIFY_OFFSET(data, 64);
7494dd0e19f3SScott Teel 
7495dd0e19f3SScott Teel #undef VERIFY_OFFSET
7496dd0e19f3SScott Teel 
7497dd0e19f3SScott Teel #define VERIFY_OFFSET(member, offset) \
7498b66cc250SMike Miller 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
7499b66cc250SMike Miller 
7500b66cc250SMike Miller 	VERIFY_OFFSET(IU_type, 0);
7501b66cc250SMike Miller 	VERIFY_OFFSET(direction, 1);
7502b66cc250SMike Miller 	VERIFY_OFFSET(reply_queue, 2);
7503b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved1, 3);  */
7504b66cc250SMike Miller 	VERIFY_OFFSET(scsi_nexus, 4);
7505b66cc250SMike Miller 	VERIFY_OFFSET(Tag, 8);
7506b66cc250SMike Miller 	VERIFY_OFFSET(cdb, 16);
7507b66cc250SMike Miller 	VERIFY_OFFSET(cciss_lun, 32);
7508b66cc250SMike Miller 	VERIFY_OFFSET(data_len, 40);
7509b66cc250SMike Miller 	VERIFY_OFFSET(cmd_priority_task_attr, 44);
7510b66cc250SMike Miller 	VERIFY_OFFSET(sg_count, 45);
7511b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved3 */
7512b66cc250SMike Miller 	VERIFY_OFFSET(err_ptr, 48);
7513b66cc250SMike Miller 	VERIFY_OFFSET(err_len, 56);
7514b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved4  */
7515b66cc250SMike Miller 	VERIFY_OFFSET(sg, 64);
7516b66cc250SMike Miller 
7517b66cc250SMike Miller #undef VERIFY_OFFSET
7518b66cc250SMike Miller 
7519b66cc250SMike Miller #define VERIFY_OFFSET(member, offset) \
7520e1f7de0cSMatt Gates 	BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
7521e1f7de0cSMatt Gates 
7522e1f7de0cSMatt Gates 	VERIFY_OFFSET(dev_handle, 0x00);
7523e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved1, 0x02);
7524e1f7de0cSMatt Gates 	VERIFY_OFFSET(function, 0x03);
7525e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved2, 0x04);
7526e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info, 0x0C);
7527e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved3, 0x10);
7528e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info_len, 0x12);
7529e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved4, 0x13);
7530e1f7de0cSMatt Gates 	VERIFY_OFFSET(sgl_offset, 0x14);
7531e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved5, 0x15);
7532e1f7de0cSMatt Gates 	VERIFY_OFFSET(transfer_len, 0x1C);
7533e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved6, 0x20);
7534e1f7de0cSMatt Gates 	VERIFY_OFFSET(io_flags, 0x24);
7535e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved7, 0x26);
7536e1f7de0cSMatt Gates 	VERIFY_OFFSET(LUN, 0x34);
7537e1f7de0cSMatt Gates 	VERIFY_OFFSET(control, 0x3C);
7538e1f7de0cSMatt Gates 	VERIFY_OFFSET(CDB, 0x40);
7539e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved8, 0x50);
7540e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_context_flags, 0x60);
7541e1f7de0cSMatt Gates 	VERIFY_OFFSET(timeout_sec, 0x62);
7542e1f7de0cSMatt Gates 	VERIFY_OFFSET(ReplyQueue, 0x64);
7543e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved9, 0x65);
7544e1f7de0cSMatt Gates 	VERIFY_OFFSET(Tag, 0x68);
7545e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_addr, 0x70);
7546e1f7de0cSMatt Gates 	VERIFY_OFFSET(CISS_LUN, 0x78);
7547e1f7de0cSMatt Gates 	VERIFY_OFFSET(SG, 0x78 + 8);
7548e1f7de0cSMatt Gates #undef VERIFY_OFFSET
7549e1f7de0cSMatt Gates }
7550e1f7de0cSMatt Gates 
7551edd16368SStephen M. Cameron module_init(hpsa_init);
7552edd16368SStephen M. Cameron module_exit(hpsa_cleanup);
7553