xref: /openbmc/linux/drivers/scsi/hpsa.c (revision 9233fb10f35ab523f788e635ff07c355bb31bbdd)
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 '.' */
569a993302SStephen M. Cameron #define HPSA_DRIVER_VERSION "3.4.4-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},
1183b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C6},
11997b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C7},
12097b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C8},
12197b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C9},
1223b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CA},
1233b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CB},
1243b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CC},
1253b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CD},
1263b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CE},
1278e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0076},
1288e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0087},
1298e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x007D},
1308e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0088},
1318e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP, 0x333f, 0x103c, 0x333f},
132edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_ANY_ID,	PCI_ANY_ID, PCI_ANY_ID,
133edd16368SStephen M. Cameron 		PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
134edd16368SStephen M. Cameron 	{0,}
135edd16368SStephen M. Cameron };
136edd16368SStephen M. Cameron 
137edd16368SStephen M. Cameron MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
138edd16368SStephen M. Cameron 
139edd16368SStephen M. Cameron /*  board_id = Subsystem Device ID & Vendor ID
140edd16368SStephen M. Cameron  *  product = Marketing Name for the board
141edd16368SStephen M. Cameron  *  access = Address of the struct of function pointers
142edd16368SStephen M. Cameron  */
143edd16368SStephen M. Cameron static struct board_type products[] = {
144edd16368SStephen M. Cameron 	{0x3241103C, "Smart Array P212", &SA5_access},
145edd16368SStephen M. Cameron 	{0x3243103C, "Smart Array P410", &SA5_access},
146edd16368SStephen M. Cameron 	{0x3245103C, "Smart Array P410i", &SA5_access},
147edd16368SStephen M. Cameron 	{0x3247103C, "Smart Array P411", &SA5_access},
148edd16368SStephen M. Cameron 	{0x3249103C, "Smart Array P812", &SA5_access},
149163dbcd8SMike Miller 	{0x324A103C, "Smart Array P712m", &SA5_access},
150163dbcd8SMike Miller 	{0x324B103C, "Smart Array P711m", &SA5_access},
151fe0c9610SMike Miller 	{0x3350103C, "Smart Array P222", &SA5_access},
152fe0c9610SMike Miller 	{0x3351103C, "Smart Array P420", &SA5_access},
153fe0c9610SMike Miller 	{0x3352103C, "Smart Array P421", &SA5_access},
154fe0c9610SMike Miller 	{0x3353103C, "Smart Array P822", &SA5_access},
155fe0c9610SMike Miller 	{0x3354103C, "Smart Array P420i", &SA5_access},
156fe0c9610SMike Miller 	{0x3355103C, "Smart Array P220i", &SA5_access},
157fe0c9610SMike Miller 	{0x3356103C, "Smart Array P721m", &SA5_access},
1581fd6c8e3SMike Miller 	{0x1921103C, "Smart Array P830i", &SA5_access},
1591fd6c8e3SMike Miller 	{0x1922103C, "Smart Array P430", &SA5_access},
1601fd6c8e3SMike Miller 	{0x1923103C, "Smart Array P431", &SA5_access},
1611fd6c8e3SMike Miller 	{0x1924103C, "Smart Array P830", &SA5_access},
1621fd6c8e3SMike Miller 	{0x1926103C, "Smart Array P731m", &SA5_access},
1631fd6c8e3SMike Miller 	{0x1928103C, "Smart Array P230i", &SA5_access},
1641fd6c8e3SMike Miller 	{0x1929103C, "Smart Array P530", &SA5_access},
16597b9f53dSMike Miller 	{0x21BD103C, "Smart Array", &SA5_access},
16697b9f53dSMike Miller 	{0x21BE103C, "Smart Array", &SA5_access},
16797b9f53dSMike Miller 	{0x21BF103C, "Smart Array", &SA5_access},
16897b9f53dSMike Miller 	{0x21C0103C, "Smart Array", &SA5_access},
16997b9f53dSMike Miller 	{0x21C1103C, "Smart Array", &SA5_access},
17097b9f53dSMike Miller 	{0x21C2103C, "Smart Array", &SA5_access},
17197b9f53dSMike Miller 	{0x21C3103C, "Smart Array", &SA5_access},
17297b9f53dSMike Miller 	{0x21C4103C, "Smart Array", &SA5_access},
17397b9f53dSMike Miller 	{0x21C5103C, "Smart Array", &SA5_access},
1743b7a45e5SJoe Handzik 	{0x21C6103C, "Smart Array", &SA5_access},
17597b9f53dSMike Miller 	{0x21C7103C, "Smart Array", &SA5_access},
17697b9f53dSMike Miller 	{0x21C8103C, "Smart Array", &SA5_access},
17797b9f53dSMike Miller 	{0x21C9103C, "Smart Array", &SA5_access},
1783b7a45e5SJoe Handzik 	{0x21CA103C, "Smart Array", &SA5_access},
1793b7a45e5SJoe Handzik 	{0x21CB103C, "Smart Array", &SA5_access},
1803b7a45e5SJoe Handzik 	{0x21CC103C, "Smart Array", &SA5_access},
1813b7a45e5SJoe Handzik 	{0x21CD103C, "Smart Array", &SA5_access},
1823b7a45e5SJoe Handzik 	{0x21CE103C, "Smart Array", &SA5_access},
1838e616a5eSStephen M. Cameron 	{0x00761590, "HP Storage P1224 Array Controller", &SA5_access},
1848e616a5eSStephen M. Cameron 	{0x00871590, "HP Storage P1224e Array Controller", &SA5_access},
1858e616a5eSStephen M. Cameron 	{0x007D1590, "HP Storage P1228 Array Controller", &SA5_access},
1868e616a5eSStephen M. Cameron 	{0x00881590, "HP Storage P1228e Array Controller", &SA5_access},
1878e616a5eSStephen M. Cameron 	{0x333f103c, "HP StorageWorks 1210m Array Controller", &SA5_access},
188edd16368SStephen M. Cameron 	{0xFFFF103C, "Unknown Smart Array", &SA5_access},
189edd16368SStephen M. Cameron };
190edd16368SStephen M. Cameron 
191edd16368SStephen M. Cameron static int number_of_controllers;
192edd16368SStephen M. Cameron 
19310f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id);
19410f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id);
195edd16368SStephen M. Cameron static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg);
196edd16368SStephen M. Cameron static void start_io(struct ctlr_info *h);
197edd16368SStephen M. Cameron 
198edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
199edd16368SStephen M. Cameron static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg);
200edd16368SStephen M. Cameron #endif
201edd16368SStephen M. Cameron 
202edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c);
203edd16368SStephen M. Cameron static void cmd_special_free(struct ctlr_info *h, struct CommandList *c);
204edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h);
205edd16368SStephen M. Cameron static struct CommandList *cmd_special_alloc(struct ctlr_info *h);
206a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
207b7bb24ebSStephen M. Cameron 	void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
208edd16368SStephen M. Cameron 	int cmd_type);
209b7bb24ebSStephen M. Cameron #define VPD_PAGE (1 << 8)
210edd16368SStephen M. Cameron 
211f281233dSJeff Garzik static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
212a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *);
213a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
214a08a8471SStephen M. Cameron 	unsigned long elapsed_time);
215667e23d4SStephen M. Cameron static int hpsa_change_queue_depth(struct scsi_device *sdev,
216667e23d4SStephen M. Cameron 	int qdepth, int reason);
217edd16368SStephen M. Cameron 
218edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
21975167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *scsicmd);
220edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev);
221edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev);
222edd16368SStephen M. Cameron 
223edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
224edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
225edd16368SStephen M. Cameron 	struct CommandList *c);
226edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
227edd16368SStephen M. Cameron 	struct CommandList *c);
228303932fdSDon Brace /* performant mode helper functions */
229303932fdSDon Brace static void calc_bucket_map(int *bucket, int num_buckets,
230e1f7de0cSMatt Gates 	int nsgs, int min_blocks, int *bucket_map);
2316f039790SGreg Kroah-Hartman static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
232254f796bSMatt Gates static inline u32 next_command(struct ctlr_info *h, u8 q);
2336f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
2346f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
2351df8552aSStephen M. Cameron 			       u64 *cfg_offset);
2366f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
2371df8552aSStephen M. Cameron 				    unsigned long *memory_bar);
2386f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
2396f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
2406f039790SGreg Kroah-Hartman 				     int wait_for_ready);
24175167d2cSStephen M. Cameron static inline void finish_cmd(struct CommandList *c);
242283b4a9bSStephen M. Cameron static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h);
243fe5389c8SStephen M. Cameron #define BOARD_NOT_READY 0
244fe5389c8SStephen M. Cameron #define BOARD_READY 1
24523100dd9SStephen M. Cameron static void hpsa_drain_accel_commands(struct ctlr_info *h);
24676438d08SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h);
247c349775eSScott Teel static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
248c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
249c349775eSScott Teel 	u8 *scsi3addr);
250edd16368SStephen M. Cameron 
251edd16368SStephen M. Cameron static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
252edd16368SStephen M. Cameron {
253edd16368SStephen M. Cameron 	unsigned long *priv = shost_priv(sdev->host);
254edd16368SStephen M. Cameron 	return (struct ctlr_info *) *priv;
255edd16368SStephen M. Cameron }
256edd16368SStephen M. Cameron 
257a23513e8SStephen M. Cameron static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
258a23513e8SStephen M. Cameron {
259a23513e8SStephen M. Cameron 	unsigned long *priv = shost_priv(sh);
260a23513e8SStephen M. Cameron 	return (struct ctlr_info *) *priv;
261a23513e8SStephen M. Cameron }
262a23513e8SStephen M. Cameron 
263edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
264edd16368SStephen M. Cameron 	struct CommandList *c)
265edd16368SStephen M. Cameron {
266edd16368SStephen M. Cameron 	if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
267edd16368SStephen M. Cameron 		return 0;
268edd16368SStephen M. Cameron 
269edd16368SStephen M. Cameron 	switch (c->err_info->SenseInfo[12]) {
270edd16368SStephen M. Cameron 	case STATE_CHANGED:
271f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: a state change "
272edd16368SStephen M. Cameron 			"detected, command retried\n", h->ctlr);
273edd16368SStephen M. Cameron 		break;
274edd16368SStephen M. Cameron 	case LUN_FAILED:
275f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: LUN failure "
276edd16368SStephen M. Cameron 			"detected, action required\n", h->ctlr);
277edd16368SStephen M. Cameron 		break;
278edd16368SStephen M. Cameron 	case REPORT_LUNS_CHANGED:
279f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: report LUN data "
28031468401SMike Miller 			"changed, action required\n", h->ctlr);
281edd16368SStephen M. Cameron 	/*
2824f4eb9f1SScott Teel 	 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
2834f4eb9f1SScott Teel 	 * target (array) devices.
284edd16368SStephen M. Cameron 	 */
285edd16368SStephen M. Cameron 		break;
286edd16368SStephen M. Cameron 	case POWER_OR_RESET:
287f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: a power on "
288edd16368SStephen M. Cameron 			"or device reset detected\n", h->ctlr);
289edd16368SStephen M. Cameron 		break;
290edd16368SStephen M. Cameron 	case UNIT_ATTENTION_CLEARED:
291f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: unit attention "
292edd16368SStephen M. Cameron 		    "cleared by another initiator\n", h->ctlr);
293edd16368SStephen M. Cameron 		break;
294edd16368SStephen M. Cameron 	default:
295f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: unknown "
296edd16368SStephen M. Cameron 			"unit attention detected\n", h->ctlr);
297edd16368SStephen M. Cameron 		break;
298edd16368SStephen M. Cameron 	}
299edd16368SStephen M. Cameron 	return 1;
300edd16368SStephen M. Cameron }
301edd16368SStephen M. Cameron 
302852af20aSMatt Bondurant static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
303852af20aSMatt Bondurant {
304852af20aSMatt Bondurant 	if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
305852af20aSMatt Bondurant 		(c->err_info->ScsiStatus != SAM_STAT_BUSY &&
306852af20aSMatt Bondurant 		 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
307852af20aSMatt Bondurant 		return 0;
308852af20aSMatt Bondurant 	dev_warn(&h->pdev->dev, HPSA "device busy");
309852af20aSMatt Bondurant 	return 1;
310852af20aSMatt Bondurant }
311852af20aSMatt Bondurant 
312da0697bdSScott Teel static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
313da0697bdSScott Teel 					 struct device_attribute *attr,
314da0697bdSScott Teel 					 const char *buf, size_t count)
315da0697bdSScott Teel {
316da0697bdSScott Teel 	int status, len;
317da0697bdSScott Teel 	struct ctlr_info *h;
318da0697bdSScott Teel 	struct Scsi_Host *shost = class_to_shost(dev);
319da0697bdSScott Teel 	char tmpbuf[10];
320da0697bdSScott Teel 
321da0697bdSScott Teel 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
322da0697bdSScott Teel 		return -EACCES;
323da0697bdSScott Teel 	len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
324da0697bdSScott Teel 	strncpy(tmpbuf, buf, len);
325da0697bdSScott Teel 	tmpbuf[len] = '\0';
326da0697bdSScott Teel 	if (sscanf(tmpbuf, "%d", &status) != 1)
327da0697bdSScott Teel 		return -EINVAL;
328da0697bdSScott Teel 	h = shost_to_hba(shost);
329da0697bdSScott Teel 	h->acciopath_status = !!status;
330da0697bdSScott Teel 	dev_warn(&h->pdev->dev,
331da0697bdSScott Teel 		"hpsa: HP SSD Smart Path %s via sysfs update.\n",
332da0697bdSScott Teel 		h->acciopath_status ? "enabled" : "disabled");
333da0697bdSScott Teel 	return count;
334da0697bdSScott Teel }
335da0697bdSScott Teel 
3362ba8bfc8SStephen M. Cameron static ssize_t host_store_raid_offload_debug(struct device *dev,
3372ba8bfc8SStephen M. Cameron 					 struct device_attribute *attr,
3382ba8bfc8SStephen M. Cameron 					 const char *buf, size_t count)
3392ba8bfc8SStephen M. Cameron {
3402ba8bfc8SStephen M. Cameron 	int debug_level, len;
3412ba8bfc8SStephen M. Cameron 	struct ctlr_info *h;
3422ba8bfc8SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
3432ba8bfc8SStephen M. Cameron 	char tmpbuf[10];
3442ba8bfc8SStephen M. Cameron 
3452ba8bfc8SStephen M. Cameron 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
3462ba8bfc8SStephen M. Cameron 		return -EACCES;
3472ba8bfc8SStephen M. Cameron 	len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
3482ba8bfc8SStephen M. Cameron 	strncpy(tmpbuf, buf, len);
3492ba8bfc8SStephen M. Cameron 	tmpbuf[len] = '\0';
3502ba8bfc8SStephen M. Cameron 	if (sscanf(tmpbuf, "%d", &debug_level) != 1)
3512ba8bfc8SStephen M. Cameron 		return -EINVAL;
3522ba8bfc8SStephen M. Cameron 	if (debug_level < 0)
3532ba8bfc8SStephen M. Cameron 		debug_level = 0;
3542ba8bfc8SStephen M. Cameron 	h = shost_to_hba(shost);
3552ba8bfc8SStephen M. Cameron 	h->raid_offload_debug = debug_level;
3562ba8bfc8SStephen M. Cameron 	dev_warn(&h->pdev->dev, "hpsa: Set raid_offload_debug level = %d\n",
3572ba8bfc8SStephen M. Cameron 		h->raid_offload_debug);
3582ba8bfc8SStephen M. Cameron 	return count;
3592ba8bfc8SStephen M. Cameron }
3602ba8bfc8SStephen M. Cameron 
361edd16368SStephen M. Cameron static ssize_t host_store_rescan(struct device *dev,
362edd16368SStephen M. Cameron 				 struct device_attribute *attr,
363edd16368SStephen M. Cameron 				 const char *buf, size_t count)
364edd16368SStephen M. Cameron {
365edd16368SStephen M. Cameron 	struct ctlr_info *h;
366edd16368SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
367a23513e8SStephen M. Cameron 	h = shost_to_hba(shost);
36831468401SMike Miller 	hpsa_scan_start(h->scsi_host);
369edd16368SStephen M. Cameron 	return count;
370edd16368SStephen M. Cameron }
371edd16368SStephen M. Cameron 
372d28ce020SStephen M. Cameron static ssize_t host_show_firmware_revision(struct device *dev,
373d28ce020SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
374d28ce020SStephen M. Cameron {
375d28ce020SStephen M. Cameron 	struct ctlr_info *h;
376d28ce020SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
377d28ce020SStephen M. Cameron 	unsigned char *fwrev;
378d28ce020SStephen M. Cameron 
379d28ce020SStephen M. Cameron 	h = shost_to_hba(shost);
380d28ce020SStephen M. Cameron 	if (!h->hba_inquiry_data)
381d28ce020SStephen M. Cameron 		return 0;
382d28ce020SStephen M. Cameron 	fwrev = &h->hba_inquiry_data[32];
383d28ce020SStephen M. Cameron 	return snprintf(buf, 20, "%c%c%c%c\n",
384d28ce020SStephen M. Cameron 		fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
385d28ce020SStephen M. Cameron }
386d28ce020SStephen M. Cameron 
38794a13649SStephen M. Cameron static ssize_t host_show_commands_outstanding(struct device *dev,
38894a13649SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
38994a13649SStephen M. Cameron {
39094a13649SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
39194a13649SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(shost);
39294a13649SStephen M. Cameron 
39394a13649SStephen M. Cameron 	return snprintf(buf, 20, "%d\n", h->commands_outstanding);
39494a13649SStephen M. Cameron }
39594a13649SStephen M. Cameron 
396745a7a25SStephen M. Cameron static ssize_t host_show_transport_mode(struct device *dev,
397745a7a25SStephen M. Cameron 	struct device_attribute *attr, char *buf)
398745a7a25SStephen M. Cameron {
399745a7a25SStephen M. Cameron 	struct ctlr_info *h;
400745a7a25SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
401745a7a25SStephen M. Cameron 
402745a7a25SStephen M. Cameron 	h = shost_to_hba(shost);
403745a7a25SStephen M. Cameron 	return snprintf(buf, 20, "%s\n",
404960a30e7SStephen M. Cameron 		h->transMethod & CFGTBL_Trans_Performant ?
405745a7a25SStephen M. Cameron 			"performant" : "simple");
406745a7a25SStephen M. Cameron }
407745a7a25SStephen M. Cameron 
408da0697bdSScott Teel static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
409da0697bdSScott Teel 	struct device_attribute *attr, char *buf)
410da0697bdSScott Teel {
411da0697bdSScott Teel 	struct ctlr_info *h;
412da0697bdSScott Teel 	struct Scsi_Host *shost = class_to_shost(dev);
413da0697bdSScott Teel 
414da0697bdSScott Teel 	h = shost_to_hba(shost);
415da0697bdSScott Teel 	return snprintf(buf, 30, "HP SSD Smart Path %s\n",
416da0697bdSScott Teel 		(h->acciopath_status == 1) ?  "enabled" : "disabled");
417da0697bdSScott Teel }
418da0697bdSScott Teel 
41946380786SStephen M. Cameron /* List of controllers which cannot be hard reset on kexec with reset_devices */
420941b1cdaSStephen M. Cameron static u32 unresettable_controller[] = {
421941b1cdaSStephen M. Cameron 	0x324a103C, /* Smart Array P712m */
422941b1cdaSStephen M. Cameron 	0x324b103C, /* SmartArray P711m */
423941b1cdaSStephen M. Cameron 	0x3223103C, /* Smart Array P800 */
424941b1cdaSStephen M. Cameron 	0x3234103C, /* Smart Array P400 */
425941b1cdaSStephen M. Cameron 	0x3235103C, /* Smart Array P400i */
426941b1cdaSStephen M. Cameron 	0x3211103C, /* Smart Array E200i */
427941b1cdaSStephen M. Cameron 	0x3212103C, /* Smart Array E200 */
428941b1cdaSStephen M. Cameron 	0x3213103C, /* Smart Array E200i */
429941b1cdaSStephen M. Cameron 	0x3214103C, /* Smart Array E200i */
430941b1cdaSStephen M. Cameron 	0x3215103C, /* Smart Array E200i */
431941b1cdaSStephen M. Cameron 	0x3237103C, /* Smart Array E500 */
432941b1cdaSStephen M. Cameron 	0x323D103C, /* Smart Array P700m */
4337af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
434941b1cdaSStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
435941b1cdaSStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
4365a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
4375a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
4385a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
4395a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
4405a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
4415a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
442941b1cdaSStephen M. Cameron };
443941b1cdaSStephen M. Cameron 
44446380786SStephen M. Cameron /* List of controllers which cannot even be soft reset */
44546380786SStephen M. Cameron static u32 soft_unresettable_controller[] = {
4467af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
4475a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
4485a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
4495a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
4505a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
4515a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
4525a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
45346380786SStephen M. Cameron 	/* Exclude 640x boards.  These are two pci devices in one slot
45446380786SStephen M. Cameron 	 * which share a battery backed cache module.  One controls the
45546380786SStephen M. Cameron 	 * cache, the other accesses the cache through the one that controls
45646380786SStephen M. Cameron 	 * it.  If we reset the one controlling the cache, the other will
45746380786SStephen M. Cameron 	 * likely not be happy.  Just forbid resetting this conjoined mess.
45846380786SStephen M. Cameron 	 * The 640x isn't really supported by hpsa anyway.
45946380786SStephen M. Cameron 	 */
46046380786SStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
46146380786SStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
46246380786SStephen M. Cameron };
46346380786SStephen M. Cameron 
46446380786SStephen M. Cameron static int ctlr_is_hard_resettable(u32 board_id)
465941b1cdaSStephen M. Cameron {
466941b1cdaSStephen M. Cameron 	int i;
467941b1cdaSStephen M. Cameron 
468941b1cdaSStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(unresettable_controller); i++)
46946380786SStephen M. Cameron 		if (unresettable_controller[i] == board_id)
470941b1cdaSStephen M. Cameron 			return 0;
471941b1cdaSStephen M. Cameron 	return 1;
472941b1cdaSStephen M. Cameron }
473941b1cdaSStephen M. Cameron 
47446380786SStephen M. Cameron static int ctlr_is_soft_resettable(u32 board_id)
47546380786SStephen M. Cameron {
47646380786SStephen M. Cameron 	int i;
47746380786SStephen M. Cameron 
47846380786SStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(soft_unresettable_controller); i++)
47946380786SStephen M. Cameron 		if (soft_unresettable_controller[i] == board_id)
48046380786SStephen M. Cameron 			return 0;
48146380786SStephen M. Cameron 	return 1;
48246380786SStephen M. Cameron }
48346380786SStephen M. Cameron 
48446380786SStephen M. Cameron static int ctlr_is_resettable(u32 board_id)
48546380786SStephen M. Cameron {
48646380786SStephen M. Cameron 	return ctlr_is_hard_resettable(board_id) ||
48746380786SStephen M. Cameron 		ctlr_is_soft_resettable(board_id);
48846380786SStephen M. Cameron }
48946380786SStephen M. Cameron 
490941b1cdaSStephen M. Cameron static ssize_t host_show_resettable(struct device *dev,
491941b1cdaSStephen M. Cameron 	struct device_attribute *attr, char *buf)
492941b1cdaSStephen M. Cameron {
493941b1cdaSStephen M. Cameron 	struct ctlr_info *h;
494941b1cdaSStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
495941b1cdaSStephen M. Cameron 
496941b1cdaSStephen M. Cameron 	h = shost_to_hba(shost);
49746380786SStephen M. Cameron 	return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
498941b1cdaSStephen M. Cameron }
499941b1cdaSStephen M. Cameron 
500edd16368SStephen M. Cameron static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
501edd16368SStephen M. Cameron {
502edd16368SStephen M. Cameron 	return (scsi3addr[3] & 0xC0) == 0x40;
503edd16368SStephen M. Cameron }
504edd16368SStephen M. Cameron 
505edd16368SStephen M. Cameron static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
506d82357eaSMike Miller 	"1(ADM)", "UNKNOWN"
507edd16368SStephen M. Cameron };
5086b80b18fSScott Teel #define HPSA_RAID_0	0
5096b80b18fSScott Teel #define HPSA_RAID_4	1
5106b80b18fSScott Teel #define HPSA_RAID_1	2	/* also used for RAID 10 */
5116b80b18fSScott Teel #define HPSA_RAID_5	3	/* also used for RAID 50 */
5126b80b18fSScott Teel #define HPSA_RAID_51	4
5136b80b18fSScott Teel #define HPSA_RAID_6	5	/* also used for RAID 60 */
5146b80b18fSScott Teel #define HPSA_RAID_ADM	6	/* also used for RAID 1+0 ADM */
515edd16368SStephen M. Cameron #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
516edd16368SStephen M. Cameron 
517edd16368SStephen M. Cameron static ssize_t raid_level_show(struct device *dev,
518edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
519edd16368SStephen M. Cameron {
520edd16368SStephen M. Cameron 	ssize_t l = 0;
52182a72c0aSStephen M. Cameron 	unsigned char rlevel;
522edd16368SStephen M. Cameron 	struct ctlr_info *h;
523edd16368SStephen M. Cameron 	struct scsi_device *sdev;
524edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
525edd16368SStephen M. Cameron 	unsigned long flags;
526edd16368SStephen M. Cameron 
527edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
528edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
529edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
530edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
531edd16368SStephen M. Cameron 	if (!hdev) {
532edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
533edd16368SStephen M. Cameron 		return -ENODEV;
534edd16368SStephen M. Cameron 	}
535edd16368SStephen M. Cameron 
536edd16368SStephen M. Cameron 	/* Is this even a logical drive? */
537edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
538edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
539edd16368SStephen M. Cameron 		l = snprintf(buf, PAGE_SIZE, "N/A\n");
540edd16368SStephen M. Cameron 		return l;
541edd16368SStephen M. Cameron 	}
542edd16368SStephen M. Cameron 
543edd16368SStephen M. Cameron 	rlevel = hdev->raid_level;
544edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
54582a72c0aSStephen M. Cameron 	if (rlevel > RAID_UNKNOWN)
546edd16368SStephen M. Cameron 		rlevel = RAID_UNKNOWN;
547edd16368SStephen M. Cameron 	l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
548edd16368SStephen M. Cameron 	return l;
549edd16368SStephen M. Cameron }
550edd16368SStephen M. Cameron 
551edd16368SStephen M. Cameron static ssize_t lunid_show(struct device *dev,
552edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
553edd16368SStephen M. Cameron {
554edd16368SStephen M. Cameron 	struct ctlr_info *h;
555edd16368SStephen M. Cameron 	struct scsi_device *sdev;
556edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
557edd16368SStephen M. Cameron 	unsigned long flags;
558edd16368SStephen M. Cameron 	unsigned char lunid[8];
559edd16368SStephen M. Cameron 
560edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
561edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
562edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
563edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
564edd16368SStephen M. Cameron 	if (!hdev) {
565edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
566edd16368SStephen M. Cameron 		return -ENODEV;
567edd16368SStephen M. Cameron 	}
568edd16368SStephen M. Cameron 	memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
569edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
570edd16368SStephen M. Cameron 	return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
571edd16368SStephen M. Cameron 		lunid[0], lunid[1], lunid[2], lunid[3],
572edd16368SStephen M. Cameron 		lunid[4], lunid[5], lunid[6], lunid[7]);
573edd16368SStephen M. Cameron }
574edd16368SStephen M. Cameron 
575edd16368SStephen M. Cameron static ssize_t unique_id_show(struct device *dev,
576edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
577edd16368SStephen M. Cameron {
578edd16368SStephen M. Cameron 	struct ctlr_info *h;
579edd16368SStephen M. Cameron 	struct scsi_device *sdev;
580edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
581edd16368SStephen M. Cameron 	unsigned long flags;
582edd16368SStephen M. Cameron 	unsigned char sn[16];
583edd16368SStephen M. Cameron 
584edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
585edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
586edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
587edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
588edd16368SStephen M. Cameron 	if (!hdev) {
589edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
590edd16368SStephen M. Cameron 		return -ENODEV;
591edd16368SStephen M. Cameron 	}
592edd16368SStephen M. Cameron 	memcpy(sn, hdev->device_id, sizeof(sn));
593edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
594edd16368SStephen M. Cameron 	return snprintf(buf, 16 * 2 + 2,
595edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X"
596edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X\n",
597edd16368SStephen M. Cameron 			sn[0], sn[1], sn[2], sn[3],
598edd16368SStephen M. Cameron 			sn[4], sn[5], sn[6], sn[7],
599edd16368SStephen M. Cameron 			sn[8], sn[9], sn[10], sn[11],
600edd16368SStephen M. Cameron 			sn[12], sn[13], sn[14], sn[15]);
601edd16368SStephen M. Cameron }
602edd16368SStephen M. Cameron 
603c1988684SScott Teel static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
604c1988684SScott Teel 	     struct device_attribute *attr, char *buf)
605c1988684SScott Teel {
606c1988684SScott Teel 	struct ctlr_info *h;
607c1988684SScott Teel 	struct scsi_device *sdev;
608c1988684SScott Teel 	struct hpsa_scsi_dev_t *hdev;
609c1988684SScott Teel 	unsigned long flags;
610c1988684SScott Teel 	int offload_enabled;
611c1988684SScott Teel 
612c1988684SScott Teel 	sdev = to_scsi_device(dev);
613c1988684SScott Teel 	h = sdev_to_hba(sdev);
614c1988684SScott Teel 	spin_lock_irqsave(&h->lock, flags);
615c1988684SScott Teel 	hdev = sdev->hostdata;
616c1988684SScott Teel 	if (!hdev) {
617c1988684SScott Teel 		spin_unlock_irqrestore(&h->lock, flags);
618c1988684SScott Teel 		return -ENODEV;
619c1988684SScott Teel 	}
620c1988684SScott Teel 	offload_enabled = hdev->offload_enabled;
621c1988684SScott Teel 	spin_unlock_irqrestore(&h->lock, flags);
622c1988684SScott Teel 	return snprintf(buf, 20, "%d\n", offload_enabled);
623c1988684SScott Teel }
624c1988684SScott Teel 
6253f5eac3aSStephen M. Cameron static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
6263f5eac3aSStephen M. Cameron static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
6273f5eac3aSStephen M. Cameron static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
6283f5eac3aSStephen M. Cameron static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
629c1988684SScott Teel static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
630c1988684SScott Teel 			host_show_hp_ssd_smart_path_enabled, NULL);
631da0697bdSScott Teel static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
632da0697bdSScott Teel 		host_show_hp_ssd_smart_path_status,
633da0697bdSScott Teel 		host_store_hp_ssd_smart_path_status);
6342ba8bfc8SStephen M. Cameron static DEVICE_ATTR(raid_offload_debug, S_IWUSR, NULL,
6352ba8bfc8SStephen M. Cameron 			host_store_raid_offload_debug);
6363f5eac3aSStephen M. Cameron static DEVICE_ATTR(firmware_revision, S_IRUGO,
6373f5eac3aSStephen M. Cameron 	host_show_firmware_revision, NULL);
6383f5eac3aSStephen M. Cameron static DEVICE_ATTR(commands_outstanding, S_IRUGO,
6393f5eac3aSStephen M. Cameron 	host_show_commands_outstanding, NULL);
6403f5eac3aSStephen M. Cameron static DEVICE_ATTR(transport_mode, S_IRUGO,
6413f5eac3aSStephen M. Cameron 	host_show_transport_mode, NULL);
642941b1cdaSStephen M. Cameron static DEVICE_ATTR(resettable, S_IRUGO,
643941b1cdaSStephen M. Cameron 	host_show_resettable, NULL);
6443f5eac3aSStephen M. Cameron 
6453f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_sdev_attrs[] = {
6463f5eac3aSStephen M. Cameron 	&dev_attr_raid_level,
6473f5eac3aSStephen M. Cameron 	&dev_attr_lunid,
6483f5eac3aSStephen M. Cameron 	&dev_attr_unique_id,
649c1988684SScott Teel 	&dev_attr_hp_ssd_smart_path_enabled,
6503f5eac3aSStephen M. Cameron 	NULL,
6513f5eac3aSStephen M. Cameron };
6523f5eac3aSStephen M. Cameron 
6533f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_shost_attrs[] = {
6543f5eac3aSStephen M. Cameron 	&dev_attr_rescan,
6553f5eac3aSStephen M. Cameron 	&dev_attr_firmware_revision,
6563f5eac3aSStephen M. Cameron 	&dev_attr_commands_outstanding,
6573f5eac3aSStephen M. Cameron 	&dev_attr_transport_mode,
658941b1cdaSStephen M. Cameron 	&dev_attr_resettable,
659da0697bdSScott Teel 	&dev_attr_hp_ssd_smart_path_status,
6602ba8bfc8SStephen M. Cameron 	&dev_attr_raid_offload_debug,
6613f5eac3aSStephen M. Cameron 	NULL,
6623f5eac3aSStephen M. Cameron };
6633f5eac3aSStephen M. Cameron 
6643f5eac3aSStephen M. Cameron static struct scsi_host_template hpsa_driver_template = {
6653f5eac3aSStephen M. Cameron 	.module			= THIS_MODULE,
666f79cfec6SStephen M. Cameron 	.name			= HPSA,
667f79cfec6SStephen M. Cameron 	.proc_name		= HPSA,
6683f5eac3aSStephen M. Cameron 	.queuecommand		= hpsa_scsi_queue_command,
6693f5eac3aSStephen M. Cameron 	.scan_start		= hpsa_scan_start,
6703f5eac3aSStephen M. Cameron 	.scan_finished		= hpsa_scan_finished,
6713f5eac3aSStephen M. Cameron 	.change_queue_depth	= hpsa_change_queue_depth,
6723f5eac3aSStephen M. Cameron 	.this_id		= -1,
6733f5eac3aSStephen M. Cameron 	.use_clustering		= ENABLE_CLUSTERING,
67475167d2cSStephen M. Cameron 	.eh_abort_handler	= hpsa_eh_abort_handler,
6753f5eac3aSStephen M. Cameron 	.eh_device_reset_handler = hpsa_eh_device_reset_handler,
6763f5eac3aSStephen M. Cameron 	.ioctl			= hpsa_ioctl,
6773f5eac3aSStephen M. Cameron 	.slave_alloc		= hpsa_slave_alloc,
6783f5eac3aSStephen M. Cameron 	.slave_destroy		= hpsa_slave_destroy,
6793f5eac3aSStephen M. Cameron #ifdef CONFIG_COMPAT
6803f5eac3aSStephen M. Cameron 	.compat_ioctl		= hpsa_compat_ioctl,
6813f5eac3aSStephen M. Cameron #endif
6823f5eac3aSStephen M. Cameron 	.sdev_attrs = hpsa_sdev_attrs,
6833f5eac3aSStephen M. Cameron 	.shost_attrs = hpsa_shost_attrs,
684c0d6a4d1SStephen M. Cameron 	.max_sectors = 8192,
68554b2b50cSMartin K. Petersen 	.no_write_same = 1,
6863f5eac3aSStephen M. Cameron };
6873f5eac3aSStephen M. Cameron 
6883f5eac3aSStephen M. Cameron 
6893f5eac3aSStephen M. Cameron /* Enqueuing and dequeuing functions for cmdlists. */
6903f5eac3aSStephen M. Cameron static inline void addQ(struct list_head *list, struct CommandList *c)
6913f5eac3aSStephen M. Cameron {
6923f5eac3aSStephen M. Cameron 	list_add_tail(&c->list, list);
6933f5eac3aSStephen M. Cameron }
6943f5eac3aSStephen M. Cameron 
695254f796bSMatt Gates static inline u32 next_command(struct ctlr_info *h, u8 q)
6963f5eac3aSStephen M. Cameron {
6973f5eac3aSStephen M. Cameron 	u32 a;
698254f796bSMatt Gates 	struct reply_pool *rq = &h->reply_queue[q];
699e16a33adSMatt Gates 	unsigned long flags;
7003f5eac3aSStephen M. Cameron 
701e1f7de0cSMatt Gates 	if (h->transMethod & CFGTBL_Trans_io_accel1)
702e1f7de0cSMatt Gates 		return h->access.command_completed(h, q);
703e1f7de0cSMatt Gates 
7043f5eac3aSStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
705254f796bSMatt Gates 		return h->access.command_completed(h, q);
7063f5eac3aSStephen M. Cameron 
707254f796bSMatt Gates 	if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
708254f796bSMatt Gates 		a = rq->head[rq->current_entry];
709254f796bSMatt Gates 		rq->current_entry++;
710e16a33adSMatt Gates 		spin_lock_irqsave(&h->lock, flags);
7113f5eac3aSStephen M. Cameron 		h->commands_outstanding--;
712e16a33adSMatt Gates 		spin_unlock_irqrestore(&h->lock, flags);
7133f5eac3aSStephen M. Cameron 	} else {
7143f5eac3aSStephen M. Cameron 		a = FIFO_EMPTY;
7153f5eac3aSStephen M. Cameron 	}
7163f5eac3aSStephen M. Cameron 	/* Check for wraparound */
717254f796bSMatt Gates 	if (rq->current_entry == h->max_commands) {
718254f796bSMatt Gates 		rq->current_entry = 0;
719254f796bSMatt Gates 		rq->wraparound ^= 1;
7203f5eac3aSStephen M. Cameron 	}
7213f5eac3aSStephen M. Cameron 	return a;
7223f5eac3aSStephen M. Cameron }
7233f5eac3aSStephen M. Cameron 
724c349775eSScott Teel /*
725c349775eSScott Teel  * There are some special bits in the bus address of the
726c349775eSScott Teel  * command that we have to set for the controller to know
727c349775eSScott Teel  * how to process the command:
728c349775eSScott Teel  *
729c349775eSScott Teel  * Normal performant mode:
730c349775eSScott Teel  * bit 0: 1 means performant mode, 0 means simple mode.
731c349775eSScott Teel  * bits 1-3 = block fetch table entry
732c349775eSScott Teel  * bits 4-6 = command type (== 0)
733c349775eSScott Teel  *
734c349775eSScott Teel  * ioaccel1 mode:
735c349775eSScott Teel  * bit 0 = "performant mode" bit.
736c349775eSScott Teel  * bits 1-3 = block fetch table entry
737c349775eSScott Teel  * bits 4-6 = command type (== 110)
738c349775eSScott Teel  * (command type is needed because ioaccel1 mode
739c349775eSScott Teel  * commands are submitted through the same register as normal
740c349775eSScott Teel  * mode commands, so this is how the controller knows whether
741c349775eSScott Teel  * the command is normal mode or ioaccel1 mode.)
742c349775eSScott Teel  *
743c349775eSScott Teel  * ioaccel2 mode:
744c349775eSScott Teel  * bit 0 = "performant mode" bit.
745c349775eSScott Teel  * bits 1-4 = block fetch table entry (note extra bit)
746c349775eSScott Teel  * bits 4-6 = not needed, because ioaccel2 mode has
747c349775eSScott Teel  * a separate special register for submitting commands.
748c349775eSScott Teel  */
749c349775eSScott Teel 
7503f5eac3aSStephen M. Cameron /* set_performant_mode: Modify the tag for cciss performant
7513f5eac3aSStephen M. Cameron  * set bit 0 for pull model, bits 3-1 for block fetch
7523f5eac3aSStephen M. Cameron  * register number
7533f5eac3aSStephen M. Cameron  */
7543f5eac3aSStephen M. Cameron static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
7553f5eac3aSStephen M. Cameron {
756254f796bSMatt Gates 	if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
7573f5eac3aSStephen M. Cameron 		c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
758eee0f03aSHannes Reinecke 		if (likely(h->msix_vector > 0))
759254f796bSMatt Gates 			c->Header.ReplyQueue =
760804a5cb5SJohn Kacur 				raw_smp_processor_id() % h->nreply_queues;
761254f796bSMatt Gates 	}
7623f5eac3aSStephen M. Cameron }
7633f5eac3aSStephen M. Cameron 
764c349775eSScott Teel static void set_ioaccel1_performant_mode(struct ctlr_info *h,
765c349775eSScott Teel 						struct CommandList *c)
766c349775eSScott Teel {
767c349775eSScott Teel 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
768c349775eSScott Teel 
769c349775eSScott Teel 	/* Tell the controller to post the reply to the queue for this
770c349775eSScott Teel 	 * processor.  This seems to give the best I/O throughput.
771c349775eSScott Teel 	 */
772c349775eSScott Teel 	cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
773c349775eSScott Teel 	/* Set the bits in the address sent down to include:
774c349775eSScott Teel 	 *  - performant mode bit (bit 0)
775c349775eSScott Teel 	 *  - pull count (bits 1-3)
776c349775eSScott Teel 	 *  - command type (bits 4-6)
777c349775eSScott Teel 	 */
778c349775eSScott Teel 	c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
779c349775eSScott Teel 					IOACCEL1_BUSADDR_CMDTYPE;
780c349775eSScott Teel }
781c349775eSScott Teel 
782c349775eSScott Teel static void set_ioaccel2_performant_mode(struct ctlr_info *h,
783c349775eSScott Teel 						struct CommandList *c)
784c349775eSScott Teel {
785c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
786c349775eSScott Teel 
787c349775eSScott Teel 	/* Tell the controller to post the reply to the queue for this
788c349775eSScott Teel 	 * processor.  This seems to give the best I/O throughput.
789c349775eSScott Teel 	 */
790c349775eSScott Teel 	cp->reply_queue = smp_processor_id() % h->nreply_queues;
791c349775eSScott Teel 	/* Set the bits in the address sent down to include:
792c349775eSScott Teel 	 *  - performant mode bit not used in ioaccel mode 2
793c349775eSScott Teel 	 *  - pull count (bits 0-3)
794c349775eSScott Teel 	 *  - command type isn't needed for ioaccel2
795c349775eSScott Teel 	 */
796c349775eSScott Teel 	c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
797c349775eSScott Teel }
798c349775eSScott Teel 
799e85c5974SStephen M. Cameron static int is_firmware_flash_cmd(u8 *cdb)
800e85c5974SStephen M. Cameron {
801e85c5974SStephen M. Cameron 	return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
802e85c5974SStephen M. Cameron }
803e85c5974SStephen M. Cameron 
804e85c5974SStephen M. Cameron /*
805e85c5974SStephen M. Cameron  * During firmware flash, the heartbeat register may not update as frequently
806e85c5974SStephen M. Cameron  * as it should.  So we dial down lockup detection during firmware flash. and
807e85c5974SStephen M. Cameron  * dial it back up when firmware flash completes.
808e85c5974SStephen M. Cameron  */
809e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
810e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
811e85c5974SStephen M. Cameron static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
812e85c5974SStephen M. Cameron 		struct CommandList *c)
813e85c5974SStephen M. Cameron {
814e85c5974SStephen M. Cameron 	if (!is_firmware_flash_cmd(c->Request.CDB))
815e85c5974SStephen M. Cameron 		return;
816e85c5974SStephen M. Cameron 	atomic_inc(&h->firmware_flash_in_progress);
817e85c5974SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
818e85c5974SStephen M. Cameron }
819e85c5974SStephen M. Cameron 
820e85c5974SStephen M. Cameron static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
821e85c5974SStephen M. Cameron 		struct CommandList *c)
822e85c5974SStephen M. Cameron {
823e85c5974SStephen M. Cameron 	if (is_firmware_flash_cmd(c->Request.CDB) &&
824e85c5974SStephen M. Cameron 		atomic_dec_and_test(&h->firmware_flash_in_progress))
825e85c5974SStephen M. Cameron 		h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
826e85c5974SStephen M. Cameron }
827e85c5974SStephen M. Cameron 
8283f5eac3aSStephen M. Cameron static void enqueue_cmd_and_start_io(struct ctlr_info *h,
8293f5eac3aSStephen M. Cameron 	struct CommandList *c)
8303f5eac3aSStephen M. Cameron {
8313f5eac3aSStephen M. Cameron 	unsigned long flags;
8323f5eac3aSStephen M. Cameron 
833c349775eSScott Teel 	switch (c->cmd_type) {
834c349775eSScott Teel 	case CMD_IOACCEL1:
835c349775eSScott Teel 		set_ioaccel1_performant_mode(h, c);
836c349775eSScott Teel 		break;
837c349775eSScott Teel 	case CMD_IOACCEL2:
838c349775eSScott Teel 		set_ioaccel2_performant_mode(h, c);
839c349775eSScott Teel 		break;
840c349775eSScott Teel 	default:
8413f5eac3aSStephen M. Cameron 		set_performant_mode(h, c);
842c349775eSScott Teel 	}
843e85c5974SStephen M. Cameron 	dial_down_lockup_detection_during_fw_flash(h, c);
8443f5eac3aSStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
8453f5eac3aSStephen M. Cameron 	addQ(&h->reqQ, c);
8463f5eac3aSStephen M. Cameron 	h->Qdepth++;
8473f5eac3aSStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
848e16a33adSMatt Gates 	start_io(h);
8493f5eac3aSStephen M. Cameron }
8503f5eac3aSStephen M. Cameron 
8513f5eac3aSStephen M. Cameron static inline void removeQ(struct CommandList *c)
8523f5eac3aSStephen M. Cameron {
8533f5eac3aSStephen M. Cameron 	if (WARN_ON(list_empty(&c->list)))
8543f5eac3aSStephen M. Cameron 		return;
8553f5eac3aSStephen M. Cameron 	list_del_init(&c->list);
8563f5eac3aSStephen M. Cameron }
8573f5eac3aSStephen M. Cameron 
8583f5eac3aSStephen M. Cameron static inline int is_hba_lunid(unsigned char scsi3addr[])
8593f5eac3aSStephen M. Cameron {
8603f5eac3aSStephen M. Cameron 	return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
8613f5eac3aSStephen M. Cameron }
8623f5eac3aSStephen M. Cameron 
8633f5eac3aSStephen M. Cameron static inline int is_scsi_rev_5(struct ctlr_info *h)
8643f5eac3aSStephen M. Cameron {
8653f5eac3aSStephen M. Cameron 	if (!h->hba_inquiry_data)
8663f5eac3aSStephen M. Cameron 		return 0;
8673f5eac3aSStephen M. Cameron 	if ((h->hba_inquiry_data[2] & 0x07) == 5)
8683f5eac3aSStephen M. Cameron 		return 1;
8693f5eac3aSStephen M. Cameron 	return 0;
8703f5eac3aSStephen M. Cameron }
8713f5eac3aSStephen M. Cameron 
872edd16368SStephen M. Cameron static int hpsa_find_target_lun(struct ctlr_info *h,
873edd16368SStephen M. Cameron 	unsigned char scsi3addr[], int bus, int *target, int *lun)
874edd16368SStephen M. Cameron {
875edd16368SStephen M. Cameron 	/* finds an unused bus, target, lun for a new physical device
876edd16368SStephen M. Cameron 	 * assumes h->devlock is held
877edd16368SStephen M. Cameron 	 */
878edd16368SStephen M. Cameron 	int i, found = 0;
879cfe5badcSScott Teel 	DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
880edd16368SStephen M. Cameron 
881263d9401SAkinobu Mita 	bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
882edd16368SStephen M. Cameron 
883edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
884edd16368SStephen M. Cameron 		if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
885263d9401SAkinobu Mita 			__set_bit(h->dev[i]->target, lun_taken);
886edd16368SStephen M. Cameron 	}
887edd16368SStephen M. Cameron 
888263d9401SAkinobu Mita 	i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
889263d9401SAkinobu Mita 	if (i < HPSA_MAX_DEVICES) {
890edd16368SStephen M. Cameron 		/* *bus = 1; */
891edd16368SStephen M. Cameron 		*target = i;
892edd16368SStephen M. Cameron 		*lun = 0;
893edd16368SStephen M. Cameron 		found = 1;
894edd16368SStephen M. Cameron 	}
895edd16368SStephen M. Cameron 	return !found;
896edd16368SStephen M. Cameron }
897edd16368SStephen M. Cameron 
898edd16368SStephen M. Cameron /* Add an entry into h->dev[] array. */
899edd16368SStephen M. Cameron static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
900edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *device,
901edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *added[], int *nadded)
902edd16368SStephen M. Cameron {
903edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
904edd16368SStephen M. Cameron 	int n = h->ndevices;
905edd16368SStephen M. Cameron 	int i;
906edd16368SStephen M. Cameron 	unsigned char addr1[8], addr2[8];
907edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
908edd16368SStephen M. Cameron 
909cfe5badcSScott Teel 	if (n >= HPSA_MAX_DEVICES) {
910edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "too many devices, some will be "
911edd16368SStephen M. Cameron 			"inaccessible.\n");
912edd16368SStephen M. Cameron 		return -1;
913edd16368SStephen M. Cameron 	}
914edd16368SStephen M. Cameron 
915edd16368SStephen M. Cameron 	/* physical devices do not have lun or target assigned until now. */
916edd16368SStephen M. Cameron 	if (device->lun != -1)
917edd16368SStephen M. Cameron 		/* Logical device, lun is already assigned. */
918edd16368SStephen M. Cameron 		goto lun_assigned;
919edd16368SStephen M. Cameron 
920edd16368SStephen M. Cameron 	/* If this device a non-zero lun of a multi-lun device
921edd16368SStephen M. Cameron 	 * byte 4 of the 8-byte LUN addr will contain the logical
922edd16368SStephen M. Cameron 	 * unit no, zero otherise.
923edd16368SStephen M. Cameron 	 */
924edd16368SStephen M. Cameron 	if (device->scsi3addr[4] == 0) {
925edd16368SStephen M. Cameron 		/* This is not a non-zero lun of a multi-lun device */
926edd16368SStephen M. Cameron 		if (hpsa_find_target_lun(h, device->scsi3addr,
927edd16368SStephen M. Cameron 			device->bus, &device->target, &device->lun) != 0)
928edd16368SStephen M. Cameron 			return -1;
929edd16368SStephen M. Cameron 		goto lun_assigned;
930edd16368SStephen M. Cameron 	}
931edd16368SStephen M. Cameron 
932edd16368SStephen M. Cameron 	/* This is a non-zero lun of a multi-lun device.
933edd16368SStephen M. Cameron 	 * Search through our list and find the device which
934edd16368SStephen M. Cameron 	 * has the same 8 byte LUN address, excepting byte 4.
935edd16368SStephen M. Cameron 	 * Assign the same bus and target for this new LUN.
936edd16368SStephen M. Cameron 	 * Use the logical unit number from the firmware.
937edd16368SStephen M. Cameron 	 */
938edd16368SStephen M. Cameron 	memcpy(addr1, device->scsi3addr, 8);
939edd16368SStephen M. Cameron 	addr1[4] = 0;
940edd16368SStephen M. Cameron 	for (i = 0; i < n; i++) {
941edd16368SStephen M. Cameron 		sd = h->dev[i];
942edd16368SStephen M. Cameron 		memcpy(addr2, sd->scsi3addr, 8);
943edd16368SStephen M. Cameron 		addr2[4] = 0;
944edd16368SStephen M. Cameron 		/* differ only in byte 4? */
945edd16368SStephen M. Cameron 		if (memcmp(addr1, addr2, 8) == 0) {
946edd16368SStephen M. Cameron 			device->bus = sd->bus;
947edd16368SStephen M. Cameron 			device->target = sd->target;
948edd16368SStephen M. Cameron 			device->lun = device->scsi3addr[4];
949edd16368SStephen M. Cameron 			break;
950edd16368SStephen M. Cameron 		}
951edd16368SStephen M. Cameron 	}
952edd16368SStephen M. Cameron 	if (device->lun == -1) {
953edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
954edd16368SStephen M. Cameron 			" suspect firmware bug or unsupported hardware "
955edd16368SStephen M. Cameron 			"configuration.\n");
956edd16368SStephen M. Cameron 			return -1;
957edd16368SStephen M. Cameron 	}
958edd16368SStephen M. Cameron 
959edd16368SStephen M. Cameron lun_assigned:
960edd16368SStephen M. Cameron 
961edd16368SStephen M. Cameron 	h->dev[n] = device;
962edd16368SStephen M. Cameron 	h->ndevices++;
963edd16368SStephen M. Cameron 	added[*nadded] = device;
964edd16368SStephen M. Cameron 	(*nadded)++;
965edd16368SStephen M. Cameron 
966edd16368SStephen M. Cameron 	/* initially, (before registering with scsi layer) we don't
967edd16368SStephen M. Cameron 	 * know our hostno and we don't want to print anything first
968edd16368SStephen M. Cameron 	 * time anyway (the scsi layer's inquiries will show that info)
969edd16368SStephen M. Cameron 	 */
970edd16368SStephen M. Cameron 	/* if (hostno != -1) */
971edd16368SStephen M. Cameron 		dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
972edd16368SStephen M. Cameron 			scsi_device_type(device->devtype), hostno,
973edd16368SStephen M. Cameron 			device->bus, device->target, device->lun);
974edd16368SStephen M. Cameron 	return 0;
975edd16368SStephen M. Cameron }
976edd16368SStephen M. Cameron 
977bd9244f7SScott Teel /* Update an entry in h->dev[] array. */
978bd9244f7SScott Teel static void hpsa_scsi_update_entry(struct ctlr_info *h, int hostno,
979bd9244f7SScott Teel 	int entry, struct hpsa_scsi_dev_t *new_entry)
980bd9244f7SScott Teel {
981bd9244f7SScott Teel 	/* assumes h->devlock is held */
982bd9244f7SScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
983bd9244f7SScott Teel 
984bd9244f7SScott Teel 	/* Raid level changed. */
985bd9244f7SScott Teel 	h->dev[entry]->raid_level = new_entry->raid_level;
986250fb125SStephen M. Cameron 
987250fb125SStephen M. Cameron 	/* Raid offload parameters changed. */
988250fb125SStephen M. Cameron 	h->dev[entry]->offload_config = new_entry->offload_config;
989250fb125SStephen M. Cameron 	h->dev[entry]->offload_enabled = new_entry->offload_enabled;
9909fb0de2dSStephen M. Cameron 	h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
9919fb0de2dSStephen M. Cameron 	h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
9929fb0de2dSStephen M. Cameron 	h->dev[entry]->raid_map = new_entry->raid_map;
993250fb125SStephen M. Cameron 
994bd9244f7SScott Teel 	dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d updated.\n",
995bd9244f7SScott Teel 		scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
996bd9244f7SScott Teel 		new_entry->target, new_entry->lun);
997bd9244f7SScott Teel }
998bd9244f7SScott Teel 
9992a8ccf31SStephen M. Cameron /* Replace an entry from h->dev[] array. */
10002a8ccf31SStephen M. Cameron static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
10012a8ccf31SStephen M. Cameron 	int entry, struct hpsa_scsi_dev_t *new_entry,
10022a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *added[], int *nadded,
10032a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
10042a8ccf31SStephen M. Cameron {
10052a8ccf31SStephen M. Cameron 	/* assumes h->devlock is held */
1006cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
10072a8ccf31SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
10082a8ccf31SStephen M. Cameron 	(*nremoved)++;
100901350d05SStephen M. Cameron 
101001350d05SStephen M. Cameron 	/*
101101350d05SStephen M. Cameron 	 * New physical devices won't have target/lun assigned yet
101201350d05SStephen M. Cameron 	 * so we need to preserve the values in the slot we are replacing.
101301350d05SStephen M. Cameron 	 */
101401350d05SStephen M. Cameron 	if (new_entry->target == -1) {
101501350d05SStephen M. Cameron 		new_entry->target = h->dev[entry]->target;
101601350d05SStephen M. Cameron 		new_entry->lun = h->dev[entry]->lun;
101701350d05SStephen M. Cameron 	}
101801350d05SStephen M. Cameron 
10192a8ccf31SStephen M. Cameron 	h->dev[entry] = new_entry;
10202a8ccf31SStephen M. Cameron 	added[*nadded] = new_entry;
10212a8ccf31SStephen M. Cameron 	(*nadded)++;
10222a8ccf31SStephen M. Cameron 	dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n",
10232a8ccf31SStephen M. Cameron 		scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
10242a8ccf31SStephen M. Cameron 			new_entry->target, new_entry->lun);
10252a8ccf31SStephen M. Cameron }
10262a8ccf31SStephen M. Cameron 
1027edd16368SStephen M. Cameron /* Remove an entry from h->dev[] array. */
1028edd16368SStephen M. Cameron static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
1029edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
1030edd16368SStephen M. Cameron {
1031edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
1032edd16368SStephen M. Cameron 	int i;
1033edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1034edd16368SStephen M. Cameron 
1035cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
1036edd16368SStephen M. Cameron 
1037edd16368SStephen M. Cameron 	sd = h->dev[entry];
1038edd16368SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
1039edd16368SStephen M. Cameron 	(*nremoved)++;
1040edd16368SStephen M. Cameron 
1041edd16368SStephen M. Cameron 	for (i = entry; i < h->ndevices-1; i++)
1042edd16368SStephen M. Cameron 		h->dev[i] = h->dev[i+1];
1043edd16368SStephen M. Cameron 	h->ndevices--;
1044edd16368SStephen M. Cameron 	dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
1045edd16368SStephen M. Cameron 		scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
1046edd16368SStephen M. Cameron 		sd->lun);
1047edd16368SStephen M. Cameron }
1048edd16368SStephen M. Cameron 
1049edd16368SStephen M. Cameron #define SCSI3ADDR_EQ(a, b) ( \
1050edd16368SStephen M. Cameron 	(a)[7] == (b)[7] && \
1051edd16368SStephen M. Cameron 	(a)[6] == (b)[6] && \
1052edd16368SStephen M. Cameron 	(a)[5] == (b)[5] && \
1053edd16368SStephen M. Cameron 	(a)[4] == (b)[4] && \
1054edd16368SStephen M. Cameron 	(a)[3] == (b)[3] && \
1055edd16368SStephen M. Cameron 	(a)[2] == (b)[2] && \
1056edd16368SStephen M. Cameron 	(a)[1] == (b)[1] && \
1057edd16368SStephen M. Cameron 	(a)[0] == (b)[0])
1058edd16368SStephen M. Cameron 
1059edd16368SStephen M. Cameron static void fixup_botched_add(struct ctlr_info *h,
1060edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *added)
1061edd16368SStephen M. Cameron {
1062edd16368SStephen M. Cameron 	/* called when scsi_add_device fails in order to re-adjust
1063edd16368SStephen M. Cameron 	 * h->dev[] to match the mid layer's view.
1064edd16368SStephen M. Cameron 	 */
1065edd16368SStephen M. Cameron 	unsigned long flags;
1066edd16368SStephen M. Cameron 	int i, j;
1067edd16368SStephen M. Cameron 
1068edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
1069edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1070edd16368SStephen M. Cameron 		if (h->dev[i] == added) {
1071edd16368SStephen M. Cameron 			for (j = i; j < h->ndevices-1; j++)
1072edd16368SStephen M. Cameron 				h->dev[j] = h->dev[j+1];
1073edd16368SStephen M. Cameron 			h->ndevices--;
1074edd16368SStephen M. Cameron 			break;
1075edd16368SStephen M. Cameron 		}
1076edd16368SStephen M. Cameron 	}
1077edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
1078edd16368SStephen M. Cameron 	kfree(added);
1079edd16368SStephen M. Cameron }
1080edd16368SStephen M. Cameron 
1081edd16368SStephen M. Cameron static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
1082edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev2)
1083edd16368SStephen M. Cameron {
1084edd16368SStephen M. Cameron 	/* we compare everything except lun and target as these
1085edd16368SStephen M. Cameron 	 * are not yet assigned.  Compare parts likely
1086edd16368SStephen M. Cameron 	 * to differ first
1087edd16368SStephen M. Cameron 	 */
1088edd16368SStephen M. Cameron 	if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
1089edd16368SStephen M. Cameron 		sizeof(dev1->scsi3addr)) != 0)
1090edd16368SStephen M. Cameron 		return 0;
1091edd16368SStephen M. Cameron 	if (memcmp(dev1->device_id, dev2->device_id,
1092edd16368SStephen M. Cameron 		sizeof(dev1->device_id)) != 0)
1093edd16368SStephen M. Cameron 		return 0;
1094edd16368SStephen M. Cameron 	if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
1095edd16368SStephen M. Cameron 		return 0;
1096edd16368SStephen M. Cameron 	if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
1097edd16368SStephen M. Cameron 		return 0;
1098edd16368SStephen M. Cameron 	if (dev1->devtype != dev2->devtype)
1099edd16368SStephen M. Cameron 		return 0;
1100edd16368SStephen M. Cameron 	if (dev1->bus != dev2->bus)
1101edd16368SStephen M. Cameron 		return 0;
1102edd16368SStephen M. Cameron 	return 1;
1103edd16368SStephen M. Cameron }
1104edd16368SStephen M. Cameron 
1105bd9244f7SScott Teel static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
1106bd9244f7SScott Teel 	struct hpsa_scsi_dev_t *dev2)
1107bd9244f7SScott Teel {
1108bd9244f7SScott Teel 	/* Device attributes that can change, but don't mean
1109bd9244f7SScott Teel 	 * that the device is a different device, nor that the OS
1110bd9244f7SScott Teel 	 * needs to be told anything about the change.
1111bd9244f7SScott Teel 	 */
1112bd9244f7SScott Teel 	if (dev1->raid_level != dev2->raid_level)
1113bd9244f7SScott Teel 		return 1;
1114250fb125SStephen M. Cameron 	if (dev1->offload_config != dev2->offload_config)
1115250fb125SStephen M. Cameron 		return 1;
1116250fb125SStephen M. Cameron 	if (dev1->offload_enabled != dev2->offload_enabled)
1117250fb125SStephen M. Cameron 		return 1;
1118bd9244f7SScott Teel 	return 0;
1119bd9244f7SScott Teel }
1120bd9244f7SScott Teel 
1121edd16368SStephen M. Cameron /* Find needle in haystack.  If exact match found, return DEVICE_SAME,
1122edd16368SStephen M. Cameron  * and return needle location in *index.  If scsi3addr matches, but not
1123edd16368SStephen M. Cameron  * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
1124bd9244f7SScott Teel  * location in *index.
1125bd9244f7SScott Teel  * In the case of a minor device attribute change, such as RAID level, just
1126bd9244f7SScott Teel  * return DEVICE_UPDATED, along with the updated device's location in index.
1127bd9244f7SScott Teel  * If needle not found, return DEVICE_NOT_FOUND.
1128edd16368SStephen M. Cameron  */
1129edd16368SStephen M. Cameron static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
1130edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *haystack[], int haystack_size,
1131edd16368SStephen M. Cameron 	int *index)
1132edd16368SStephen M. Cameron {
1133edd16368SStephen M. Cameron 	int i;
1134edd16368SStephen M. Cameron #define DEVICE_NOT_FOUND 0
1135edd16368SStephen M. Cameron #define DEVICE_CHANGED 1
1136edd16368SStephen M. Cameron #define DEVICE_SAME 2
1137bd9244f7SScott Teel #define DEVICE_UPDATED 3
1138edd16368SStephen M. Cameron 	for (i = 0; i < haystack_size; i++) {
113923231048SStephen M. Cameron 		if (haystack[i] == NULL) /* previously removed. */
114023231048SStephen M. Cameron 			continue;
1141edd16368SStephen M. Cameron 		if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
1142edd16368SStephen M. Cameron 			*index = i;
1143bd9244f7SScott Teel 			if (device_is_the_same(needle, haystack[i])) {
1144bd9244f7SScott Teel 				if (device_updated(needle, haystack[i]))
1145bd9244f7SScott Teel 					return DEVICE_UPDATED;
1146edd16368SStephen M. Cameron 				return DEVICE_SAME;
1147bd9244f7SScott Teel 			} else {
11489846590eSStephen M. Cameron 				/* Keep offline devices offline */
11499846590eSStephen M. Cameron 				if (needle->volume_offline)
11509846590eSStephen M. Cameron 					return DEVICE_NOT_FOUND;
1151edd16368SStephen M. Cameron 				return DEVICE_CHANGED;
1152edd16368SStephen M. Cameron 			}
1153edd16368SStephen M. Cameron 		}
1154bd9244f7SScott Teel 	}
1155edd16368SStephen M. Cameron 	*index = -1;
1156edd16368SStephen M. Cameron 	return DEVICE_NOT_FOUND;
1157edd16368SStephen M. Cameron }
1158edd16368SStephen M. Cameron 
11599846590eSStephen M. Cameron static void hpsa_monitor_offline_device(struct ctlr_info *h,
11609846590eSStephen M. Cameron 					unsigned char scsi3addr[])
11619846590eSStephen M. Cameron {
11629846590eSStephen M. Cameron 	struct offline_device_entry *device;
11639846590eSStephen M. Cameron 	unsigned long flags;
11649846590eSStephen M. Cameron 
11659846590eSStephen M. Cameron 	/* Check to see if device is already on the list */
11669846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
11679846590eSStephen M. Cameron 	list_for_each_entry(device, &h->offline_device_list, offline_list) {
11689846590eSStephen M. Cameron 		if (memcmp(device->scsi3addr, scsi3addr,
11699846590eSStephen M. Cameron 			sizeof(device->scsi3addr)) == 0) {
11709846590eSStephen M. Cameron 			spin_unlock_irqrestore(&h->offline_device_lock, flags);
11719846590eSStephen M. Cameron 			return;
11729846590eSStephen M. Cameron 		}
11739846590eSStephen M. Cameron 	}
11749846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
11759846590eSStephen M. Cameron 
11769846590eSStephen M. Cameron 	/* Device is not on the list, add it. */
11779846590eSStephen M. Cameron 	device = kmalloc(sizeof(*device), GFP_KERNEL);
11789846590eSStephen M. Cameron 	if (!device) {
11799846590eSStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in %s\n", __func__);
11809846590eSStephen M. Cameron 		return;
11819846590eSStephen M. Cameron 	}
11829846590eSStephen M. Cameron 	memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
11839846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
11849846590eSStephen M. Cameron 	list_add_tail(&device->offline_list, &h->offline_device_list);
11859846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
11869846590eSStephen M. Cameron }
11879846590eSStephen M. Cameron 
11889846590eSStephen M. Cameron /* Print a message explaining various offline volume states */
11899846590eSStephen M. Cameron static void hpsa_show_volume_status(struct ctlr_info *h,
11909846590eSStephen M. Cameron 	struct hpsa_scsi_dev_t *sd)
11919846590eSStephen M. Cameron {
11929846590eSStephen M. Cameron 	if (sd->volume_offline == HPSA_VPD_LV_STATUS_UNSUPPORTED)
11939846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
11949846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume status is not available through vital product data pages.\n",
11959846590eSStephen M. Cameron 			h->scsi_host->host_no,
11969846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
11979846590eSStephen M. Cameron 	switch (sd->volume_offline) {
11989846590eSStephen M. Cameron 	case HPSA_LV_OK:
11999846590eSStephen M. Cameron 		break;
12009846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ERASE:
12019846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12029846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing background erase process.\n",
12039846590eSStephen M. Cameron 			h->scsi_host->host_no,
12049846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12059846590eSStephen M. Cameron 		break;
12069846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_RPI:
12079846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12089846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing rapid parity initialization process.\n",
12099846590eSStephen M. Cameron 			h->scsi_host->host_no,
12109846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12119846590eSStephen M. Cameron 		break;
12129846590eSStephen M. Cameron 	case HPSA_LV_PENDING_RPI:
12139846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12149846590eSStephen M. Cameron 				"C%d:B%d:T%d:L%d Volume is queued for rapid parity initialization process.\n",
12159846590eSStephen M. Cameron 				h->scsi_host->host_no,
12169846590eSStephen M. Cameron 				sd->bus, sd->target, sd->lun);
12179846590eSStephen M. Cameron 		break;
12189846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_NO_KEY:
12199846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12209846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because key is not present.\n",
12219846590eSStephen M. Cameron 			h->scsi_host->host_no,
12229846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12239846590eSStephen M. Cameron 		break;
12249846590eSStephen M. Cameron 	case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
12259846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12269846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is not encrypted and cannot be accessed because controller is in encryption-only mode.\n",
12279846590eSStephen M. Cameron 			h->scsi_host->host_no,
12289846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12299846590eSStephen M. Cameron 		break;
12309846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION:
12319846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12329846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing encryption process.\n",
12339846590eSStephen M. Cameron 			h->scsi_host->host_no,
12349846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12359846590eSStephen M. Cameron 		break;
12369846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
12379846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12389846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing encryption re-keying process.\n",
12399846590eSStephen M. Cameron 			h->scsi_host->host_no,
12409846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12419846590eSStephen M. Cameron 		break;
12429846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
12439846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12449846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because controller does not have encryption enabled.\n",
12459846590eSStephen M. Cameron 			h->scsi_host->host_no,
12469846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12479846590eSStephen M. Cameron 		break;
12489846590eSStephen M. Cameron 	case HPSA_LV_PENDING_ENCRYPTION:
12499846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12509846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is pending migration to encrypted state, but process has not started.\n",
12519846590eSStephen M. Cameron 			h->scsi_host->host_no,
12529846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12539846590eSStephen M. Cameron 		break;
12549846590eSStephen M. Cameron 	case HPSA_LV_PENDING_ENCRYPTION_REKEYING:
12559846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
12569846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is encrypted and is pending encryption rekeying.\n",
12579846590eSStephen M. Cameron 			h->scsi_host->host_no,
12589846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
12599846590eSStephen M. Cameron 		break;
12609846590eSStephen M. Cameron 	}
12619846590eSStephen M. Cameron }
12629846590eSStephen M. Cameron 
12634967bd3eSStephen M. Cameron static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
1264edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd[], int nsds)
1265edd16368SStephen M. Cameron {
1266edd16368SStephen M. Cameron 	/* sd contains scsi3 addresses and devtypes, and inquiry
1267edd16368SStephen M. Cameron 	 * data.  This function takes what's in sd to be the current
1268edd16368SStephen M. Cameron 	 * reality and updates h->dev[] to reflect that reality.
1269edd16368SStephen M. Cameron 	 */
1270edd16368SStephen M. Cameron 	int i, entry, device_change, changes = 0;
1271edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *csd;
1272edd16368SStephen M. Cameron 	unsigned long flags;
1273edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **added, **removed;
1274edd16368SStephen M. Cameron 	int nadded, nremoved;
1275edd16368SStephen M. Cameron 	struct Scsi_Host *sh = NULL;
1276edd16368SStephen M. Cameron 
1277cfe5badcSScott Teel 	added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
1278cfe5badcSScott Teel 	removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
1279edd16368SStephen M. Cameron 
1280edd16368SStephen M. Cameron 	if (!added || !removed) {
1281edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in "
1282edd16368SStephen M. Cameron 			"adjust_hpsa_scsi_table\n");
1283edd16368SStephen M. Cameron 		goto free_and_out;
1284edd16368SStephen M. Cameron 	}
1285edd16368SStephen M. Cameron 
1286edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1287edd16368SStephen M. Cameron 
1288edd16368SStephen M. Cameron 	/* find any devices in h->dev[] that are not in
1289edd16368SStephen M. Cameron 	 * sd[] and remove them from h->dev[], and for any
1290edd16368SStephen M. Cameron 	 * devices which have changed, remove the old device
1291edd16368SStephen M. Cameron 	 * info and add the new device info.
1292bd9244f7SScott Teel 	 * If minor device attributes change, just update
1293bd9244f7SScott Teel 	 * the existing device structure.
1294edd16368SStephen M. Cameron 	 */
1295edd16368SStephen M. Cameron 	i = 0;
1296edd16368SStephen M. Cameron 	nremoved = 0;
1297edd16368SStephen M. Cameron 	nadded = 0;
1298edd16368SStephen M. Cameron 	while (i < h->ndevices) {
1299edd16368SStephen M. Cameron 		csd = h->dev[i];
1300edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
1301edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
1302edd16368SStephen M. Cameron 			changes++;
1303edd16368SStephen M. Cameron 			hpsa_scsi_remove_entry(h, hostno, i,
1304edd16368SStephen M. Cameron 				removed, &nremoved);
1305edd16368SStephen M. Cameron 			continue; /* remove ^^^, hence i not incremented */
1306edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
1307edd16368SStephen M. Cameron 			changes++;
13082a8ccf31SStephen M. Cameron 			hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
13092a8ccf31SStephen M. Cameron 				added, &nadded, removed, &nremoved);
1310c7f172dcSStephen M. Cameron 			/* Set it to NULL to prevent it from being freed
1311c7f172dcSStephen M. Cameron 			 * at the bottom of hpsa_update_scsi_devices()
1312c7f172dcSStephen M. Cameron 			 */
1313c7f172dcSStephen M. Cameron 			sd[entry] = NULL;
1314bd9244f7SScott Teel 		} else if (device_change == DEVICE_UPDATED) {
1315bd9244f7SScott Teel 			hpsa_scsi_update_entry(h, hostno, i, sd[entry]);
1316edd16368SStephen M. Cameron 		}
1317edd16368SStephen M. Cameron 		i++;
1318edd16368SStephen M. Cameron 	}
1319edd16368SStephen M. Cameron 
1320edd16368SStephen M. Cameron 	/* Now, make sure every device listed in sd[] is also
1321edd16368SStephen M. Cameron 	 * listed in h->dev[], adding them if they aren't found
1322edd16368SStephen M. Cameron 	 */
1323edd16368SStephen M. Cameron 
1324edd16368SStephen M. Cameron 	for (i = 0; i < nsds; i++) {
1325edd16368SStephen M. Cameron 		if (!sd[i]) /* if already added above. */
1326edd16368SStephen M. Cameron 			continue;
13279846590eSStephen M. Cameron 
13289846590eSStephen M. Cameron 		/* Don't add devices which are NOT READY, FORMAT IN PROGRESS
13299846590eSStephen M. Cameron 		 * as the SCSI mid-layer does not handle such devices well.
13309846590eSStephen M. Cameron 		 * It relentlessly loops sending TUR at 3Hz, then READ(10)
13319846590eSStephen M. Cameron 		 * at 160Hz, and prevents the system from coming up.
13329846590eSStephen M. Cameron 		 */
13339846590eSStephen M. Cameron 		if (sd[i]->volume_offline) {
13349846590eSStephen M. Cameron 			hpsa_show_volume_status(h, sd[i]);
13359846590eSStephen M. Cameron 			dev_info(&h->pdev->dev, "c%db%dt%dl%d: temporarily offline\n",
13369846590eSStephen M. Cameron 				h->scsi_host->host_no,
13379846590eSStephen M. Cameron 				sd[i]->bus, sd[i]->target, sd[i]->lun);
13389846590eSStephen M. Cameron 			continue;
13399846590eSStephen M. Cameron 		}
13409846590eSStephen M. Cameron 
1341edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1342edd16368SStephen M. Cameron 					h->ndevices, &entry);
1343edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
1344edd16368SStephen M. Cameron 			changes++;
1345edd16368SStephen M. Cameron 			if (hpsa_scsi_add_entry(h, hostno, sd[i],
1346edd16368SStephen M. Cameron 				added, &nadded) != 0)
1347edd16368SStephen M. Cameron 				break;
1348edd16368SStephen M. Cameron 			sd[i] = NULL; /* prevent from being freed later. */
1349edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
1350edd16368SStephen M. Cameron 			/* should never happen... */
1351edd16368SStephen M. Cameron 			changes++;
1352edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev,
1353edd16368SStephen M. Cameron 				"device unexpectedly changed.\n");
1354edd16368SStephen M. Cameron 			/* but if it does happen, we just ignore that device */
1355edd16368SStephen M. Cameron 		}
1356edd16368SStephen M. Cameron 	}
1357edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1358edd16368SStephen M. Cameron 
13599846590eSStephen M. Cameron 	/* Monitor devices which are in one of several NOT READY states to be
13609846590eSStephen M. Cameron 	 * brought online later. This must be done without holding h->devlock,
13619846590eSStephen M. Cameron 	 * so don't touch h->dev[]
13629846590eSStephen M. Cameron 	 */
13639846590eSStephen M. Cameron 	for (i = 0; i < nsds; i++) {
13649846590eSStephen M. Cameron 		if (!sd[i]) /* if already added above. */
13659846590eSStephen M. Cameron 			continue;
13669846590eSStephen M. Cameron 		if (sd[i]->volume_offline)
13679846590eSStephen M. Cameron 			hpsa_monitor_offline_device(h, sd[i]->scsi3addr);
13689846590eSStephen M. Cameron 	}
13699846590eSStephen M. Cameron 
1370edd16368SStephen M. Cameron 	/* Don't notify scsi mid layer of any changes the first time through
1371edd16368SStephen M. Cameron 	 * (or if there are no changes) scsi_scan_host will do it later the
1372edd16368SStephen M. Cameron 	 * first time through.
1373edd16368SStephen M. Cameron 	 */
1374edd16368SStephen M. Cameron 	if (hostno == -1 || !changes)
1375edd16368SStephen M. Cameron 		goto free_and_out;
1376edd16368SStephen M. Cameron 
1377edd16368SStephen M. Cameron 	sh = h->scsi_host;
1378edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any removed devices */
1379edd16368SStephen M. Cameron 	for (i = 0; i < nremoved; i++) {
1380edd16368SStephen M. Cameron 		struct scsi_device *sdev =
1381edd16368SStephen M. Cameron 			scsi_device_lookup(sh, removed[i]->bus,
1382edd16368SStephen M. Cameron 				removed[i]->target, removed[i]->lun);
1383edd16368SStephen M. Cameron 		if (sdev != NULL) {
1384edd16368SStephen M. Cameron 			scsi_remove_device(sdev);
1385edd16368SStephen M. Cameron 			scsi_device_put(sdev);
1386edd16368SStephen M. Cameron 		} else {
1387edd16368SStephen M. Cameron 			/* We don't expect to get here.
1388edd16368SStephen M. Cameron 			 * future cmds to this device will get selection
1389edd16368SStephen M. Cameron 			 * timeout as if the device was gone.
1390edd16368SStephen M. Cameron 			 */
1391edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
1392edd16368SStephen M. Cameron 				" for removal.", hostno, removed[i]->bus,
1393edd16368SStephen M. Cameron 				removed[i]->target, removed[i]->lun);
1394edd16368SStephen M. Cameron 		}
1395edd16368SStephen M. Cameron 		kfree(removed[i]);
1396edd16368SStephen M. Cameron 		removed[i] = NULL;
1397edd16368SStephen M. Cameron 	}
1398edd16368SStephen M. Cameron 
1399edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any added devices */
1400edd16368SStephen M. Cameron 	for (i = 0; i < nadded; i++) {
1401edd16368SStephen M. Cameron 		if (scsi_add_device(sh, added[i]->bus,
1402edd16368SStephen M. Cameron 			added[i]->target, added[i]->lun) == 0)
1403edd16368SStephen M. Cameron 			continue;
1404edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
1405edd16368SStephen M. Cameron 			"device not added.\n", hostno, added[i]->bus,
1406edd16368SStephen M. Cameron 			added[i]->target, added[i]->lun);
1407edd16368SStephen M. Cameron 		/* now we have to remove it from h->dev,
1408edd16368SStephen M. Cameron 		 * since it didn't get added to scsi mid layer
1409edd16368SStephen M. Cameron 		 */
1410edd16368SStephen M. Cameron 		fixup_botched_add(h, added[i]);
1411edd16368SStephen M. Cameron 	}
1412edd16368SStephen M. Cameron 
1413edd16368SStephen M. Cameron free_and_out:
1414edd16368SStephen M. Cameron 	kfree(added);
1415edd16368SStephen M. Cameron 	kfree(removed);
1416edd16368SStephen M. Cameron }
1417edd16368SStephen M. Cameron 
1418edd16368SStephen M. Cameron /*
14199e03aa2fSJoe Perches  * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
1420edd16368SStephen M. Cameron  * Assume's h->devlock is held.
1421edd16368SStephen M. Cameron  */
1422edd16368SStephen M. Cameron static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1423edd16368SStephen M. Cameron 	int bus, int target, int lun)
1424edd16368SStephen M. Cameron {
1425edd16368SStephen M. Cameron 	int i;
1426edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1427edd16368SStephen M. Cameron 
1428edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1429edd16368SStephen M. Cameron 		sd = h->dev[i];
1430edd16368SStephen M. Cameron 		if (sd->bus == bus && sd->target == target && sd->lun == lun)
1431edd16368SStephen M. Cameron 			return sd;
1432edd16368SStephen M. Cameron 	}
1433edd16368SStephen M. Cameron 	return NULL;
1434edd16368SStephen M. Cameron }
1435edd16368SStephen M. Cameron 
1436edd16368SStephen M. Cameron /* link sdev->hostdata to our per-device structure. */
1437edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev)
1438edd16368SStephen M. Cameron {
1439edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1440edd16368SStephen M. Cameron 	unsigned long flags;
1441edd16368SStephen M. Cameron 	struct ctlr_info *h;
1442edd16368SStephen M. Cameron 
1443edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
1444edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1445edd16368SStephen M. Cameron 	sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1446edd16368SStephen M. Cameron 		sdev_id(sdev), sdev->lun);
1447edd16368SStephen M. Cameron 	if (sd != NULL)
1448edd16368SStephen M. Cameron 		sdev->hostdata = sd;
1449edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1450edd16368SStephen M. Cameron 	return 0;
1451edd16368SStephen M. Cameron }
1452edd16368SStephen M. Cameron 
1453edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev)
1454edd16368SStephen M. Cameron {
1455bcc44255SStephen M. Cameron 	/* nothing to do. */
1456edd16368SStephen M. Cameron }
1457edd16368SStephen M. Cameron 
145833a2ffceSStephen M. Cameron static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
145933a2ffceSStephen M. Cameron {
146033a2ffceSStephen M. Cameron 	int i;
146133a2ffceSStephen M. Cameron 
146233a2ffceSStephen M. Cameron 	if (!h->cmd_sg_list)
146333a2ffceSStephen M. Cameron 		return;
146433a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
146533a2ffceSStephen M. Cameron 		kfree(h->cmd_sg_list[i]);
146633a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = NULL;
146733a2ffceSStephen M. Cameron 	}
146833a2ffceSStephen M. Cameron 	kfree(h->cmd_sg_list);
146933a2ffceSStephen M. Cameron 	h->cmd_sg_list = NULL;
147033a2ffceSStephen M. Cameron }
147133a2ffceSStephen M. Cameron 
147233a2ffceSStephen M. Cameron static int hpsa_allocate_sg_chain_blocks(struct ctlr_info *h)
147333a2ffceSStephen M. Cameron {
147433a2ffceSStephen M. Cameron 	int i;
147533a2ffceSStephen M. Cameron 
147633a2ffceSStephen M. Cameron 	if (h->chainsize <= 0)
147733a2ffceSStephen M. Cameron 		return 0;
147833a2ffceSStephen M. Cameron 
147933a2ffceSStephen M. Cameron 	h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
148033a2ffceSStephen M. Cameron 				GFP_KERNEL);
148133a2ffceSStephen M. Cameron 	if (!h->cmd_sg_list)
148233a2ffceSStephen M. Cameron 		return -ENOMEM;
148333a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
148433a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
148533a2ffceSStephen M. Cameron 						h->chainsize, GFP_KERNEL);
148633a2ffceSStephen M. Cameron 		if (!h->cmd_sg_list[i])
148733a2ffceSStephen M. Cameron 			goto clean;
148833a2ffceSStephen M. Cameron 	}
148933a2ffceSStephen M. Cameron 	return 0;
149033a2ffceSStephen M. Cameron 
149133a2ffceSStephen M. Cameron clean:
149233a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
149333a2ffceSStephen M. Cameron 	return -ENOMEM;
149433a2ffceSStephen M. Cameron }
149533a2ffceSStephen M. Cameron 
1496e2bea6dfSStephen M. Cameron static int hpsa_map_sg_chain_block(struct ctlr_info *h,
149733a2ffceSStephen M. Cameron 	struct CommandList *c)
149833a2ffceSStephen M. Cameron {
149933a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg, *chain_block;
150033a2ffceSStephen M. Cameron 	u64 temp64;
150133a2ffceSStephen M. Cameron 
150233a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
150333a2ffceSStephen M. Cameron 	chain_block = h->cmd_sg_list[c->cmdindex];
150433a2ffceSStephen M. Cameron 	chain_sg->Ext = HPSA_SG_CHAIN;
150533a2ffceSStephen M. Cameron 	chain_sg->Len = sizeof(*chain_sg) *
150633a2ffceSStephen M. Cameron 		(c->Header.SGTotal - h->max_cmd_sg_entries);
150733a2ffceSStephen M. Cameron 	temp64 = pci_map_single(h->pdev, chain_block, chain_sg->Len,
150833a2ffceSStephen M. Cameron 				PCI_DMA_TODEVICE);
1509e2bea6dfSStephen M. Cameron 	if (dma_mapping_error(&h->pdev->dev, temp64)) {
1510e2bea6dfSStephen M. Cameron 		/* prevent subsequent unmapping */
1511e2bea6dfSStephen M. Cameron 		chain_sg->Addr.lower = 0;
1512e2bea6dfSStephen M. Cameron 		chain_sg->Addr.upper = 0;
1513e2bea6dfSStephen M. Cameron 		return -1;
1514e2bea6dfSStephen M. Cameron 	}
151533a2ffceSStephen M. Cameron 	chain_sg->Addr.lower = (u32) (temp64 & 0x0FFFFFFFFULL);
151633a2ffceSStephen M. Cameron 	chain_sg->Addr.upper = (u32) ((temp64 >> 32) & 0x0FFFFFFFFULL);
1517e2bea6dfSStephen M. Cameron 	return 0;
151833a2ffceSStephen M. Cameron }
151933a2ffceSStephen M. Cameron 
152033a2ffceSStephen M. Cameron static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
152133a2ffceSStephen M. Cameron 	struct CommandList *c)
152233a2ffceSStephen M. Cameron {
152333a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg;
152433a2ffceSStephen M. Cameron 	union u64bit temp64;
152533a2ffceSStephen M. Cameron 
152633a2ffceSStephen M. Cameron 	if (c->Header.SGTotal <= h->max_cmd_sg_entries)
152733a2ffceSStephen M. Cameron 		return;
152833a2ffceSStephen M. Cameron 
152933a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
153033a2ffceSStephen M. Cameron 	temp64.val32.lower = chain_sg->Addr.lower;
153133a2ffceSStephen M. Cameron 	temp64.val32.upper = chain_sg->Addr.upper;
153233a2ffceSStephen M. Cameron 	pci_unmap_single(h->pdev, temp64.val, chain_sg->Len, PCI_DMA_TODEVICE);
153333a2ffceSStephen M. Cameron }
153433a2ffceSStephen M. Cameron 
1535a09c1441SScott Teel 
1536a09c1441SScott Teel /* Decode the various types of errors on ioaccel2 path.
1537a09c1441SScott Teel  * Return 1 for any error that should generate a RAID path retry.
1538a09c1441SScott Teel  * Return 0 for errors that don't require a RAID path retry.
1539a09c1441SScott Teel  */
1540a09c1441SScott Teel static int handle_ioaccel_mode2_error(struct ctlr_info *h,
1541c349775eSScott Teel 					struct CommandList *c,
1542c349775eSScott Teel 					struct scsi_cmnd *cmd,
1543c349775eSScott Teel 					struct io_accel2_cmd *c2)
1544c349775eSScott Teel {
1545c349775eSScott Teel 	int data_len;
1546a09c1441SScott Teel 	int retry = 0;
1547c349775eSScott Teel 
1548c349775eSScott Teel 	switch (c2->error_data.serv_response) {
1549c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_COMPLETE:
1550c349775eSScott Teel 		switch (c2->error_data.status) {
1551c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
1552c349775eSScott Teel 			break;
1553c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
1554c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1555c349775eSScott Teel 				"%s: task complete with check condition.\n",
1556c349775eSScott Teel 				"HP SSD Smart Path");
1557c349775eSScott Teel 			if (c2->error_data.data_present !=
1558c349775eSScott Teel 					IOACCEL2_SENSE_DATA_PRESENT)
1559c349775eSScott Teel 				break;
1560c349775eSScott Teel 			/* copy the sense data */
1561c349775eSScott Teel 			data_len = c2->error_data.sense_data_len;
1562c349775eSScott Teel 			if (data_len > SCSI_SENSE_BUFFERSIZE)
1563c349775eSScott Teel 				data_len = SCSI_SENSE_BUFFERSIZE;
1564c349775eSScott Teel 			if (data_len > sizeof(c2->error_data.sense_data_buff))
1565c349775eSScott Teel 				data_len =
1566c349775eSScott Teel 					sizeof(c2->error_data.sense_data_buff);
1567c349775eSScott Teel 			memcpy(cmd->sense_buffer,
1568c349775eSScott Teel 				c2->error_data.sense_data_buff, data_len);
1569c349775eSScott Teel 			cmd->result |= SAM_STAT_CHECK_CONDITION;
1570a09c1441SScott Teel 			retry = 1;
1571c349775eSScott Teel 			break;
1572c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
1573c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1574c349775eSScott Teel 				"%s: task complete with BUSY status.\n",
1575c349775eSScott Teel 				"HP SSD Smart Path");
1576a09c1441SScott Teel 			retry = 1;
1577c349775eSScott Teel 			break;
1578c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
1579c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1580c349775eSScott Teel 				"%s: task complete with reservation conflict.\n",
1581c349775eSScott Teel 				"HP SSD Smart Path");
1582a09c1441SScott Teel 			retry = 1;
1583c349775eSScott Teel 			break;
1584c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
1585c349775eSScott Teel 			/* Make scsi midlayer do unlimited retries */
1586c349775eSScott Teel 			cmd->result = DID_IMM_RETRY << 16;
1587c349775eSScott Teel 			break;
1588c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
1589c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1590c349775eSScott Teel 				"%s: task complete with aborted status.\n",
1591c349775eSScott Teel 				"HP SSD Smart Path");
1592a09c1441SScott Teel 			retry = 1;
1593c349775eSScott Teel 			break;
1594c349775eSScott Teel 		default:
1595c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1596c349775eSScott Teel 				"%s: task complete with unrecognized status: 0x%02x\n",
1597c349775eSScott Teel 				"HP SSD Smart Path", c2->error_data.status);
1598a09c1441SScott Teel 			retry = 1;
1599c349775eSScott Teel 			break;
1600c349775eSScott Teel 		}
1601c349775eSScott Teel 		break;
1602c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_FAILURE:
1603c349775eSScott Teel 		/* don't expect to get here. */
1604c349775eSScott Teel 		dev_warn(&h->pdev->dev,
1605c349775eSScott Teel 			"unexpected delivery or target failure, status = 0x%02x\n",
1606c349775eSScott Teel 			c2->error_data.status);
1607a09c1441SScott Teel 		retry = 1;
1608c349775eSScott Teel 		break;
1609c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
1610c349775eSScott Teel 		break;
1611c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
1612c349775eSScott Teel 		break;
1613c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
1614c349775eSScott Teel 		dev_warn(&h->pdev->dev, "task management function rejected.\n");
1615a09c1441SScott Teel 		retry = 1;
1616c349775eSScott Teel 		break;
1617c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
1618c349775eSScott Teel 		dev_warn(&h->pdev->dev, "task management function invalid LUN\n");
1619c349775eSScott Teel 		break;
1620c349775eSScott Teel 	default:
1621c349775eSScott Teel 		dev_warn(&h->pdev->dev,
1622c349775eSScott Teel 			"%s: Unrecognized server response: 0x%02x\n",
1623a09c1441SScott Teel 			"HP SSD Smart Path",
1624a09c1441SScott Teel 			c2->error_data.serv_response);
1625a09c1441SScott Teel 		retry = 1;
1626c349775eSScott Teel 		break;
1627c349775eSScott Teel 	}
1628a09c1441SScott Teel 
1629a09c1441SScott Teel 	return retry;	/* retry on raid path? */
1630c349775eSScott Teel }
1631c349775eSScott Teel 
1632c349775eSScott Teel static void process_ioaccel2_completion(struct ctlr_info *h,
1633c349775eSScott Teel 		struct CommandList *c, struct scsi_cmnd *cmd,
1634c349775eSScott Teel 		struct hpsa_scsi_dev_t *dev)
1635c349775eSScott Teel {
1636c349775eSScott Teel 	struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
1637a09c1441SScott Teel 	int raid_retry = 0;
1638c349775eSScott Teel 
1639c349775eSScott Teel 	/* check for good status */
1640c349775eSScott Teel 	if (likely(c2->error_data.serv_response == 0 &&
1641c349775eSScott Teel 			c2->error_data.status == 0)) {
1642c349775eSScott Teel 		cmd_free(h, c);
1643c349775eSScott Teel 		cmd->scsi_done(cmd);
1644c349775eSScott Teel 		return;
1645c349775eSScott Teel 	}
1646c349775eSScott Teel 
1647c349775eSScott Teel 	/* Any RAID offload error results in retry which will use
1648c349775eSScott Teel 	 * the normal I/O path so the controller can handle whatever's
1649c349775eSScott Teel 	 * wrong.
1650c349775eSScott Teel 	 */
1651c349775eSScott Teel 	if (is_logical_dev_addr_mode(dev->scsi3addr) &&
1652c349775eSScott Teel 		c2->error_data.serv_response ==
1653c349775eSScott Teel 			IOACCEL2_SERV_RESPONSE_FAILURE) {
1654a09c1441SScott Teel 		if (c2->error_data.status ==
1655c349775eSScott Teel 			IOACCEL2_STATUS_SR_IOACCEL_DISABLED)
1656c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1657a09c1441SScott Teel 				"%s: Path is unavailable, retrying on standard path.\n",
1658a09c1441SScott Teel 				"HP SSD Smart Path");
1659a09c1441SScott Teel 		else
1660a09c1441SScott Teel 			dev_warn(&h->pdev->dev,
1661a09c1441SScott Teel 				"%s: Error 0x%02x, retrying on standard path.\n",
1662c349775eSScott Teel 				"HP SSD Smart Path", c2->error_data.status);
1663a09c1441SScott Teel 
1664c349775eSScott Teel 		dev->offload_enabled = 0;
1665e863d68eSScott Teel 		h->drv_req_rescan = 1;	/* schedule controller for a rescan */
1666c349775eSScott Teel 		cmd->result = DID_SOFT_ERROR << 16;
1667c349775eSScott Teel 		cmd_free(h, c);
1668c349775eSScott Teel 		cmd->scsi_done(cmd);
1669c349775eSScott Teel 		return;
1670c349775eSScott Teel 	}
1671a09c1441SScott Teel 	raid_retry = handle_ioaccel_mode2_error(h, c, cmd, c2);
1672a09c1441SScott Teel 	/* If error found, disable Smart Path, schedule a rescan,
1673a09c1441SScott Teel 	 * and force a retry on the standard path.
1674a09c1441SScott Teel 	 */
1675a09c1441SScott Teel 	if (raid_retry) {
1676a09c1441SScott Teel 		dev_warn(&h->pdev->dev, "%s: Retrying on standard path.\n",
1677a09c1441SScott Teel 			"HP SSD Smart Path");
1678a09c1441SScott Teel 		dev->offload_enabled = 0; /* Disable Smart Path */
1679a09c1441SScott Teel 		h->drv_req_rescan = 1;	  /* schedule controller rescan */
1680a09c1441SScott Teel 		cmd->result = DID_SOFT_ERROR << 16;
1681a09c1441SScott Teel 	}
1682c349775eSScott Teel 	cmd_free(h, c);
1683c349775eSScott Teel 	cmd->scsi_done(cmd);
1684c349775eSScott Teel }
1685c349775eSScott Teel 
16861fb011fbSStephen M. Cameron static void complete_scsi_command(struct CommandList *cp)
1687edd16368SStephen M. Cameron {
1688edd16368SStephen M. Cameron 	struct scsi_cmnd *cmd;
1689edd16368SStephen M. Cameron 	struct ctlr_info *h;
1690edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
1691283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
1692edd16368SStephen M. Cameron 
1693edd16368SStephen M. Cameron 	unsigned char sense_key;
1694edd16368SStephen M. Cameron 	unsigned char asc;      /* additional sense code */
1695edd16368SStephen M. Cameron 	unsigned char ascq;     /* additional sense code qualifier */
1696db111e18SStephen M. Cameron 	unsigned long sense_data_size;
1697edd16368SStephen M. Cameron 
1698edd16368SStephen M. Cameron 	ei = cp->err_info;
1699edd16368SStephen M. Cameron 	cmd = (struct scsi_cmnd *) cp->scsi_cmd;
1700edd16368SStephen M. Cameron 	h = cp->h;
1701283b4a9bSStephen M. Cameron 	dev = cmd->device->hostdata;
1702edd16368SStephen M. Cameron 
1703edd16368SStephen M. Cameron 	scsi_dma_unmap(cmd); /* undo the DMA mappings */
1704e1f7de0cSMatt Gates 	if ((cp->cmd_type == CMD_SCSI) &&
1705e1f7de0cSMatt Gates 		(cp->Header.SGTotal > h->max_cmd_sg_entries))
170633a2ffceSStephen M. Cameron 		hpsa_unmap_sg_chain_block(h, cp);
1707edd16368SStephen M. Cameron 
1708edd16368SStephen M. Cameron 	cmd->result = (DID_OK << 16); 		/* host byte */
1709edd16368SStephen M. Cameron 	cmd->result |= (COMMAND_COMPLETE << 8);	/* msg byte */
1710c349775eSScott Teel 
1711c349775eSScott Teel 	if (cp->cmd_type == CMD_IOACCEL2)
1712c349775eSScott Teel 		return process_ioaccel2_completion(h, cp, cmd, dev);
1713c349775eSScott Teel 
17145512672fSStephen M. Cameron 	cmd->result |= ei->ScsiStatus;
1715edd16368SStephen M. Cameron 
1716edd16368SStephen M. Cameron 	/* copy the sense data whether we need to or not. */
1717db111e18SStephen M. Cameron 	if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
1718db111e18SStephen M. Cameron 		sense_data_size = SCSI_SENSE_BUFFERSIZE;
1719db111e18SStephen M. Cameron 	else
1720db111e18SStephen M. Cameron 		sense_data_size = sizeof(ei->SenseInfo);
1721db111e18SStephen M. Cameron 	if (ei->SenseLen < sense_data_size)
1722db111e18SStephen M. Cameron 		sense_data_size = ei->SenseLen;
1723db111e18SStephen M. Cameron 
1724db111e18SStephen M. Cameron 	memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
1725edd16368SStephen M. Cameron 	scsi_set_resid(cmd, ei->ResidualCnt);
1726edd16368SStephen M. Cameron 
1727edd16368SStephen M. Cameron 	if (ei->CommandStatus == 0) {
1728edd16368SStephen M. Cameron 		cmd_free(h, cp);
17292cc5bfafSTomas Henzl 		cmd->scsi_done(cmd);
1730edd16368SStephen M. Cameron 		return;
1731edd16368SStephen M. Cameron 	}
1732edd16368SStephen M. Cameron 
1733e1f7de0cSMatt Gates 	/* For I/O accelerator commands, copy over some fields to the normal
1734e1f7de0cSMatt Gates 	 * CISS header used below for error handling.
1735e1f7de0cSMatt Gates 	 */
1736e1f7de0cSMatt Gates 	if (cp->cmd_type == CMD_IOACCEL1) {
1737e1f7de0cSMatt Gates 		struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
1738e1f7de0cSMatt Gates 		cp->Header.SGList = cp->Header.SGTotal = scsi_sg_count(cmd);
1739e1f7de0cSMatt Gates 		cp->Request.CDBLen = c->io_flags & IOACCEL1_IOFLAGS_CDBLEN_MASK;
1740e1f7de0cSMatt Gates 		cp->Header.Tag.lower = c->Tag.lower;
1741e1f7de0cSMatt Gates 		cp->Header.Tag.upper = c->Tag.upper;
1742e1f7de0cSMatt Gates 		memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
1743e1f7de0cSMatt Gates 		memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
1744283b4a9bSStephen M. Cameron 
1745283b4a9bSStephen M. Cameron 		/* Any RAID offload error results in retry which will use
1746283b4a9bSStephen M. Cameron 		 * the normal I/O path so the controller can handle whatever's
1747283b4a9bSStephen M. Cameron 		 * wrong.
1748283b4a9bSStephen M. Cameron 		 */
1749283b4a9bSStephen M. Cameron 		if (is_logical_dev_addr_mode(dev->scsi3addr)) {
1750283b4a9bSStephen M. Cameron 			if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
1751283b4a9bSStephen M. Cameron 				dev->offload_enabled = 0;
1752283b4a9bSStephen M. Cameron 			cmd->result = DID_SOFT_ERROR << 16;
1753283b4a9bSStephen M. Cameron 			cmd_free(h, cp);
1754283b4a9bSStephen M. Cameron 			cmd->scsi_done(cmd);
1755283b4a9bSStephen M. Cameron 			return;
1756283b4a9bSStephen M. Cameron 		}
1757e1f7de0cSMatt Gates 	}
1758e1f7de0cSMatt Gates 
1759edd16368SStephen M. Cameron 	/* an error has occurred */
1760edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
1761edd16368SStephen M. Cameron 
1762edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
1763edd16368SStephen M. Cameron 		if (ei->ScsiStatus) {
1764edd16368SStephen M. Cameron 			/* Get sense key */
1765edd16368SStephen M. Cameron 			sense_key = 0xf & ei->SenseInfo[2];
1766edd16368SStephen M. Cameron 			/* Get additional sense code */
1767edd16368SStephen M. Cameron 			asc = ei->SenseInfo[12];
1768edd16368SStephen M. Cameron 			/* Get addition sense code qualifier */
1769edd16368SStephen M. Cameron 			ascq = ei->SenseInfo[13];
1770edd16368SStephen M. Cameron 		}
1771edd16368SStephen M. Cameron 
1772edd16368SStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
17733ce438dfSMatt Gates 			if (check_for_unit_attention(h, cp))
1774edd16368SStephen M. Cameron 				break;
1775edd16368SStephen M. Cameron 			if (sense_key == ILLEGAL_REQUEST) {
1776edd16368SStephen M. Cameron 				/*
1777edd16368SStephen M. Cameron 				 * SCSI REPORT_LUNS is commonly unsupported on
1778edd16368SStephen M. Cameron 				 * Smart Array.  Suppress noisy complaint.
1779edd16368SStephen M. Cameron 				 */
1780edd16368SStephen M. Cameron 				if (cp->Request.CDB[0] == REPORT_LUNS)
1781edd16368SStephen M. Cameron 					break;
1782edd16368SStephen M. Cameron 
1783edd16368SStephen M. Cameron 				/* If ASC/ASCQ indicate Logical Unit
1784edd16368SStephen M. Cameron 				 * Not Supported condition,
1785edd16368SStephen M. Cameron 				 */
1786edd16368SStephen M. Cameron 				if ((asc == 0x25) && (ascq == 0x0)) {
1787edd16368SStephen M. Cameron 					dev_warn(&h->pdev->dev, "cp %p "
1788edd16368SStephen M. Cameron 						"has check condition\n", cp);
1789edd16368SStephen M. Cameron 					break;
1790edd16368SStephen M. Cameron 				}
1791edd16368SStephen M. Cameron 			}
1792edd16368SStephen M. Cameron 
1793edd16368SStephen M. Cameron 			if (sense_key == NOT_READY) {
1794edd16368SStephen M. Cameron 				/* If Sense is Not Ready, Logical Unit
1795edd16368SStephen M. Cameron 				 * Not ready, Manual Intervention
1796edd16368SStephen M. Cameron 				 * required
1797edd16368SStephen M. Cameron 				 */
1798edd16368SStephen M. Cameron 				if ((asc == 0x04) && (ascq == 0x03)) {
1799edd16368SStephen M. Cameron 					dev_warn(&h->pdev->dev, "cp %p "
1800edd16368SStephen M. Cameron 						"has check condition: unit "
1801edd16368SStephen M. Cameron 						"not ready, manual "
1802edd16368SStephen M. Cameron 						"intervention required\n", cp);
1803edd16368SStephen M. Cameron 					break;
1804edd16368SStephen M. Cameron 				}
1805edd16368SStephen M. Cameron 			}
18061d3b3609SMatt Gates 			if (sense_key == ABORTED_COMMAND) {
18071d3b3609SMatt Gates 				/* Aborted command is retryable */
18081d3b3609SMatt Gates 				dev_warn(&h->pdev->dev, "cp %p "
18091d3b3609SMatt Gates 					"has check condition: aborted command: "
18101d3b3609SMatt Gates 					"ASC: 0x%x, ASCQ: 0x%x\n",
18111d3b3609SMatt Gates 					cp, asc, ascq);
18122e311fbaSStephen M. Cameron 				cmd->result |= DID_SOFT_ERROR << 16;
18131d3b3609SMatt Gates 				break;
18141d3b3609SMatt Gates 			}
1815edd16368SStephen M. Cameron 			/* Must be some other type of check condition */
181621b8e4efSStephen M. Cameron 			dev_dbg(&h->pdev->dev, "cp %p has check condition: "
1817edd16368SStephen M. Cameron 					"unknown type: "
1818edd16368SStephen M. Cameron 					"Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1819edd16368SStephen M. Cameron 					"Returning result: 0x%x, "
1820edd16368SStephen M. Cameron 					"cmd=[%02x %02x %02x %02x %02x "
1821807be732SMike Miller 					"%02x %02x %02x %02x %02x %02x "
1822edd16368SStephen M. Cameron 					"%02x %02x %02x %02x %02x]\n",
1823edd16368SStephen M. Cameron 					cp, sense_key, asc, ascq,
1824edd16368SStephen M. Cameron 					cmd->result,
1825edd16368SStephen M. Cameron 					cmd->cmnd[0], cmd->cmnd[1],
1826edd16368SStephen M. Cameron 					cmd->cmnd[2], cmd->cmnd[3],
1827edd16368SStephen M. Cameron 					cmd->cmnd[4], cmd->cmnd[5],
1828edd16368SStephen M. Cameron 					cmd->cmnd[6], cmd->cmnd[7],
1829807be732SMike Miller 					cmd->cmnd[8], cmd->cmnd[9],
1830807be732SMike Miller 					cmd->cmnd[10], cmd->cmnd[11],
1831807be732SMike Miller 					cmd->cmnd[12], cmd->cmnd[13],
1832807be732SMike Miller 					cmd->cmnd[14], cmd->cmnd[15]);
1833edd16368SStephen M. Cameron 			break;
1834edd16368SStephen M. Cameron 		}
1835edd16368SStephen M. Cameron 
1836edd16368SStephen M. Cameron 
1837edd16368SStephen M. Cameron 		/* Problem was not a check condition
1838edd16368SStephen M. Cameron 		 * Pass it up to the upper layers...
1839edd16368SStephen M. Cameron 		 */
1840edd16368SStephen M. Cameron 		if (ei->ScsiStatus) {
1841edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
1842edd16368SStephen M. Cameron 				"Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1843edd16368SStephen M. Cameron 				"Returning result: 0x%x\n",
1844edd16368SStephen M. Cameron 				cp, ei->ScsiStatus,
1845edd16368SStephen M. Cameron 				sense_key, asc, ascq,
1846edd16368SStephen M. Cameron 				cmd->result);
1847edd16368SStephen M. Cameron 		} else {  /* scsi status is zero??? How??? */
1848edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
1849edd16368SStephen M. Cameron 				"Returning no connection.\n", cp),
1850edd16368SStephen M. Cameron 
1851edd16368SStephen M. Cameron 			/* Ordinarily, this case should never happen,
1852edd16368SStephen M. Cameron 			 * but there is a bug in some released firmware
1853edd16368SStephen M. Cameron 			 * revisions that allows it to happen if, for
1854edd16368SStephen M. Cameron 			 * example, a 4100 backplane loses power and
1855edd16368SStephen M. Cameron 			 * the tape drive is in it.  We assume that
1856edd16368SStephen M. Cameron 			 * it's a fatal error of some kind because we
1857edd16368SStephen M. Cameron 			 * can't show that it wasn't. We will make it
1858edd16368SStephen M. Cameron 			 * look like selection timeout since that is
1859edd16368SStephen M. Cameron 			 * the most common reason for this to occur,
1860edd16368SStephen M. Cameron 			 * and it's severe enough.
1861edd16368SStephen M. Cameron 			 */
1862edd16368SStephen M. Cameron 
1863edd16368SStephen M. Cameron 			cmd->result = DID_NO_CONNECT << 16;
1864edd16368SStephen M. Cameron 		}
1865edd16368SStephen M. Cameron 		break;
1866edd16368SStephen M. Cameron 
1867edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1868edd16368SStephen M. Cameron 		break;
1869edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
1870edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p has"
1871edd16368SStephen M. Cameron 			" completed with data overrun "
1872edd16368SStephen M. Cameron 			"reported\n", cp);
1873edd16368SStephen M. Cameron 		break;
1874edd16368SStephen M. Cameron 	case CMD_INVALID: {
1875edd16368SStephen M. Cameron 		/* print_bytes(cp, sizeof(*cp), 1, 0);
1876edd16368SStephen M. Cameron 		print_cmd(cp); */
1877edd16368SStephen M. Cameron 		/* We get CMD_INVALID if you address a non-existent device
1878edd16368SStephen M. Cameron 		 * instead of a selection timeout (no response).  You will
1879edd16368SStephen M. Cameron 		 * see this if you yank out a drive, then try to access it.
1880edd16368SStephen M. Cameron 		 * This is kind of a shame because it means that any other
1881edd16368SStephen M. Cameron 		 * CMD_INVALID (e.g. driver bug) will get interpreted as a
1882edd16368SStephen M. Cameron 		 * missing target. */
1883edd16368SStephen M. Cameron 		cmd->result = DID_NO_CONNECT << 16;
1884edd16368SStephen M. Cameron 	}
1885edd16368SStephen M. Cameron 		break;
1886edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
1887256d0eaaSStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1888edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p has "
1889edd16368SStephen M. Cameron 			"protocol error\n", cp);
1890edd16368SStephen M. Cameron 		break;
1891edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
1892edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1893edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p had  hardware error\n", cp);
1894edd16368SStephen M. Cameron 		break;
1895edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
1896edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1897edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
1898edd16368SStephen M. Cameron 		break;
1899edd16368SStephen M. Cameron 	case CMD_ABORTED:
1900edd16368SStephen M. Cameron 		cmd->result = DID_ABORT << 16;
1901edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
1902edd16368SStephen M. Cameron 				cp, ei->ScsiStatus);
1903edd16368SStephen M. Cameron 		break;
1904edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
1905edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1906edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
1907edd16368SStephen M. Cameron 		break;
1908edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
1909f6e76055SStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
1910f6e76055SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p aborted due to an unsolicited "
1911edd16368SStephen M. Cameron 			"abort\n", cp);
1912edd16368SStephen M. Cameron 		break;
1913edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
1914edd16368SStephen M. Cameron 		cmd->result = DID_TIME_OUT << 16;
1915edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
1916edd16368SStephen M. Cameron 		break;
19171d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
19181d5e2ed0SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
19191d5e2ed0SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Command unabortable\n");
19201d5e2ed0SStephen M. Cameron 		break;
1921283b4a9bSStephen M. Cameron 	case CMD_IOACCEL_DISABLED:
1922283b4a9bSStephen M. Cameron 		/* This only handles the direct pass-through case since RAID
1923283b4a9bSStephen M. Cameron 		 * offload is handled above.  Just attempt a retry.
1924283b4a9bSStephen M. Cameron 		 */
1925283b4a9bSStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16;
1926283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev,
1927283b4a9bSStephen M. Cameron 				"cp %p had HP SSD Smart Path error\n", cp);
1928283b4a9bSStephen M. Cameron 		break;
1929edd16368SStephen M. Cameron 	default:
1930edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1931edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
1932edd16368SStephen M. Cameron 				cp, ei->CommandStatus);
1933edd16368SStephen M. Cameron 	}
1934edd16368SStephen M. Cameron 	cmd_free(h, cp);
19352cc5bfafSTomas Henzl 	cmd->scsi_done(cmd);
1936edd16368SStephen M. Cameron }
1937edd16368SStephen M. Cameron 
1938edd16368SStephen M. Cameron static void hpsa_pci_unmap(struct pci_dev *pdev,
1939edd16368SStephen M. Cameron 	struct CommandList *c, int sg_used, int data_direction)
1940edd16368SStephen M. Cameron {
1941edd16368SStephen M. Cameron 	int i;
1942edd16368SStephen M. Cameron 	union u64bit addr64;
1943edd16368SStephen M. Cameron 
1944edd16368SStephen M. Cameron 	for (i = 0; i < sg_used; i++) {
1945edd16368SStephen M. Cameron 		addr64.val32.lower = c->SG[i].Addr.lower;
1946edd16368SStephen M. Cameron 		addr64.val32.upper = c->SG[i].Addr.upper;
1947edd16368SStephen M. Cameron 		pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len,
1948edd16368SStephen M. Cameron 			data_direction);
1949edd16368SStephen M. Cameron 	}
1950edd16368SStephen M. Cameron }
1951edd16368SStephen M. Cameron 
1952a2dac136SStephen M. Cameron static int hpsa_map_one(struct pci_dev *pdev,
1953edd16368SStephen M. Cameron 		struct CommandList *cp,
1954edd16368SStephen M. Cameron 		unsigned char *buf,
1955edd16368SStephen M. Cameron 		size_t buflen,
1956edd16368SStephen M. Cameron 		int data_direction)
1957edd16368SStephen M. Cameron {
195801a02ffcSStephen M. Cameron 	u64 addr64;
1959edd16368SStephen M. Cameron 
1960edd16368SStephen M. Cameron 	if (buflen == 0 || data_direction == PCI_DMA_NONE) {
1961edd16368SStephen M. Cameron 		cp->Header.SGList = 0;
1962edd16368SStephen M. Cameron 		cp->Header.SGTotal = 0;
1963a2dac136SStephen M. Cameron 		return 0;
1964edd16368SStephen M. Cameron 	}
1965edd16368SStephen M. Cameron 
196601a02ffcSStephen M. Cameron 	addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction);
1967eceaae18SShuah Khan 	if (dma_mapping_error(&pdev->dev, addr64)) {
1968a2dac136SStephen M. Cameron 		/* Prevent subsequent unmap of something never mapped */
1969eceaae18SShuah Khan 		cp->Header.SGList = 0;
1970eceaae18SShuah Khan 		cp->Header.SGTotal = 0;
1971a2dac136SStephen M. Cameron 		return -1;
1972eceaae18SShuah Khan 	}
1973edd16368SStephen M. Cameron 	cp->SG[0].Addr.lower =
197401a02ffcSStephen M. Cameron 	  (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
1975edd16368SStephen M. Cameron 	cp->SG[0].Addr.upper =
197601a02ffcSStephen M. Cameron 	  (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
1977edd16368SStephen M. Cameron 	cp->SG[0].Len = buflen;
1978e1d9cbfaSMatt Gates 	cp->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining */
197901a02ffcSStephen M. Cameron 	cp->Header.SGList = (u8) 1;   /* no. SGs contig in this cmd */
198001a02ffcSStephen M. Cameron 	cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */
1981a2dac136SStephen M. Cameron 	return 0;
1982edd16368SStephen M. Cameron }
1983edd16368SStephen M. Cameron 
1984edd16368SStephen M. Cameron static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
1985edd16368SStephen M. Cameron 	struct CommandList *c)
1986edd16368SStephen M. Cameron {
1987edd16368SStephen M. Cameron 	DECLARE_COMPLETION_ONSTACK(wait);
1988edd16368SStephen M. Cameron 
1989edd16368SStephen M. Cameron 	c->waiting = &wait;
1990edd16368SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
1991edd16368SStephen M. Cameron 	wait_for_completion(&wait);
1992edd16368SStephen M. Cameron }
1993edd16368SStephen M. Cameron 
1994a0c12413SStephen M. Cameron static void hpsa_scsi_do_simple_cmd_core_if_no_lockup(struct ctlr_info *h,
1995a0c12413SStephen M. Cameron 	struct CommandList *c)
1996a0c12413SStephen M. Cameron {
1997a0c12413SStephen M. Cameron 	unsigned long flags;
1998a0c12413SStephen M. Cameron 
1999a0c12413SStephen M. Cameron 	/* If controller lockup detected, fake a hardware error. */
2000a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
2001a0c12413SStephen M. Cameron 	if (unlikely(h->lockup_detected)) {
2002a0c12413SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
2003a0c12413SStephen M. Cameron 		c->err_info->CommandStatus = CMD_HARDWARE_ERR;
2004a0c12413SStephen M. Cameron 	} else {
2005a0c12413SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
2006a0c12413SStephen M. Cameron 		hpsa_scsi_do_simple_cmd_core(h, c);
2007a0c12413SStephen M. Cameron 	}
2008a0c12413SStephen M. Cameron }
2009a0c12413SStephen M. Cameron 
20109c2fc160SStephen M. Cameron #define MAX_DRIVER_CMD_RETRIES 25
2011edd16368SStephen M. Cameron static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
2012edd16368SStephen M. Cameron 	struct CommandList *c, int data_direction)
2013edd16368SStephen M. Cameron {
20149c2fc160SStephen M. Cameron 	int backoff_time = 10, retry_count = 0;
2015edd16368SStephen M. Cameron 
2016edd16368SStephen M. Cameron 	do {
20177630abd0SJoe Perches 		memset(c->err_info, 0, sizeof(*c->err_info));
2018edd16368SStephen M. Cameron 		hpsa_scsi_do_simple_cmd_core(h, c);
2019edd16368SStephen M. Cameron 		retry_count++;
20209c2fc160SStephen M. Cameron 		if (retry_count > 3) {
20219c2fc160SStephen M. Cameron 			msleep(backoff_time);
20229c2fc160SStephen M. Cameron 			if (backoff_time < 1000)
20239c2fc160SStephen M. Cameron 				backoff_time *= 2;
20249c2fc160SStephen M. Cameron 		}
2025852af20aSMatt Bondurant 	} while ((check_for_unit_attention(h, c) ||
20269c2fc160SStephen M. Cameron 			check_for_busy(h, c)) &&
20279c2fc160SStephen M. Cameron 			retry_count <= MAX_DRIVER_CMD_RETRIES);
2028edd16368SStephen M. Cameron 	hpsa_pci_unmap(h->pdev, c, 1, data_direction);
2029edd16368SStephen M. Cameron }
2030edd16368SStephen M. Cameron 
2031d1e8beacSStephen M. Cameron static void hpsa_print_cmd(struct ctlr_info *h, char *txt,
2032d1e8beacSStephen M. Cameron 				struct CommandList *c)
2033edd16368SStephen M. Cameron {
2034d1e8beacSStephen M. Cameron 	const u8 *cdb = c->Request.CDB;
2035d1e8beacSStephen M. Cameron 	const u8 *lun = c->Header.LUN.LunAddrBytes;
2036edd16368SStephen M. Cameron 
2037d1e8beacSStephen M. Cameron 	dev_warn(&h->pdev->dev, "%s: LUN:%02x%02x%02x%02x%02x%02x%02x%02x"
2038d1e8beacSStephen M. Cameron 	" CDB:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
2039d1e8beacSStephen M. Cameron 		txt, lun[0], lun[1], lun[2], lun[3],
2040d1e8beacSStephen M. Cameron 		lun[4], lun[5], lun[6], lun[7],
2041d1e8beacSStephen M. Cameron 		cdb[0], cdb[1], cdb[2], cdb[3],
2042d1e8beacSStephen M. Cameron 		cdb[4], cdb[5], cdb[6], cdb[7],
2043d1e8beacSStephen M. Cameron 		cdb[8], cdb[9], cdb[10], cdb[11],
2044d1e8beacSStephen M. Cameron 		cdb[12], cdb[13], cdb[14], cdb[15]);
2045d1e8beacSStephen M. Cameron }
2046d1e8beacSStephen M. Cameron 
2047d1e8beacSStephen M. Cameron static void hpsa_scsi_interpret_error(struct ctlr_info *h,
2048d1e8beacSStephen M. Cameron 			struct CommandList *cp)
2049d1e8beacSStephen M. Cameron {
2050d1e8beacSStephen M. Cameron 	const struct ErrorInfo *ei = cp->err_info;
2051d1e8beacSStephen M. Cameron 	struct device *d = &cp->h->pdev->dev;
2052d1e8beacSStephen M. Cameron 	const u8 *sd = ei->SenseInfo;
2053d1e8beacSStephen M. Cameron 
2054edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
2055edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
2056d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "SCSI status", cp);
2057d1e8beacSStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION)
2058d1e8beacSStephen M. Cameron 			dev_warn(d, "SCSI Status = 02, Sense key = %02x, ASC = %02x, ASCQ = %02x\n",
2059d1e8beacSStephen M. Cameron 				sd[2] & 0x0f, sd[12], sd[13]);
2060d1e8beacSStephen M. Cameron 		else
2061d1e8beacSStephen M. Cameron 			dev_warn(d, "SCSI Status = %02x\n", ei->ScsiStatus);
2062edd16368SStephen M. Cameron 		if (ei->ScsiStatus == 0)
2063edd16368SStephen M. Cameron 			dev_warn(d, "SCSI status is abnormally zero.  "
2064edd16368SStephen M. Cameron 			"(probably indicates selection timeout "
2065edd16368SStephen M. Cameron 			"reported incorrectly due to a known "
2066edd16368SStephen M. Cameron 			"firmware bug, circa July, 2001.)\n");
2067edd16368SStephen M. Cameron 		break;
2068edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
2069edd16368SStephen M. Cameron 		break;
2070edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
2071d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "overrun condition", cp);
2072edd16368SStephen M. Cameron 		break;
2073edd16368SStephen M. Cameron 	case CMD_INVALID: {
2074edd16368SStephen M. Cameron 		/* controller unfortunately reports SCSI passthru's
2075edd16368SStephen M. Cameron 		 * to non-existent targets as invalid commands.
2076edd16368SStephen M. Cameron 		 */
2077d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "invalid command", cp);
2078d1e8beacSStephen M. Cameron 		dev_warn(d, "probably means device no longer present\n");
2079edd16368SStephen M. Cameron 		}
2080edd16368SStephen M. Cameron 		break;
2081edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
2082d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "protocol error", cp);
2083edd16368SStephen M. Cameron 		break;
2084edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
2085d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "hardware error", cp);
2086edd16368SStephen M. Cameron 		break;
2087edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
2088d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "connection lost", cp);
2089edd16368SStephen M. Cameron 		break;
2090edd16368SStephen M. Cameron 	case CMD_ABORTED:
2091d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "aborted", cp);
2092edd16368SStephen M. Cameron 		break;
2093edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
2094d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "abort failed", cp);
2095edd16368SStephen M. Cameron 		break;
2096edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
2097d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unsolicited abort", cp);
2098edd16368SStephen M. Cameron 		break;
2099edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
2100d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "timed out", cp);
2101edd16368SStephen M. Cameron 		break;
21021d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
2103d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unabortable", cp);
21041d5e2ed0SStephen M. Cameron 		break;
2105edd16368SStephen M. Cameron 	default:
2106d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unknown status", cp);
2107d1e8beacSStephen M. Cameron 		dev_warn(d, "Unknown command status %x\n",
2108edd16368SStephen M. Cameron 				ei->CommandStatus);
2109edd16368SStephen M. Cameron 	}
2110edd16368SStephen M. Cameron }
2111edd16368SStephen M. Cameron 
2112edd16368SStephen M. Cameron static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
2113b7bb24ebSStephen M. Cameron 			u16 page, unsigned char *buf,
2114edd16368SStephen M. Cameron 			unsigned char bufsize)
2115edd16368SStephen M. Cameron {
2116edd16368SStephen M. Cameron 	int rc = IO_OK;
2117edd16368SStephen M. Cameron 	struct CommandList *c;
2118edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2119edd16368SStephen M. Cameron 
2120edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
2121edd16368SStephen M. Cameron 
2122edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
2123edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2124ecd9aad4SStephen M. Cameron 		return -ENOMEM;
2125edd16368SStephen M. Cameron 	}
2126edd16368SStephen M. Cameron 
2127a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
2128a2dac136SStephen M. Cameron 			page, scsi3addr, TYPE_CMD)) {
2129a2dac136SStephen M. Cameron 		rc = -1;
2130a2dac136SStephen M. Cameron 		goto out;
2131a2dac136SStephen M. Cameron 	}
2132edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2133edd16368SStephen M. Cameron 	ei = c->err_info;
2134edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2135d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2136edd16368SStephen M. Cameron 		rc = -1;
2137edd16368SStephen M. Cameron 	}
2138a2dac136SStephen M. Cameron out:
2139edd16368SStephen M. Cameron 	cmd_special_free(h, c);
2140edd16368SStephen M. Cameron 	return rc;
2141edd16368SStephen M. Cameron }
2142edd16368SStephen M. Cameron 
2143316b221aSStephen M. Cameron static int hpsa_bmic_ctrl_mode_sense(struct ctlr_info *h,
2144316b221aSStephen M. Cameron 		unsigned char *scsi3addr, unsigned char page,
2145316b221aSStephen M. Cameron 		struct bmic_controller_parameters *buf, size_t bufsize)
2146316b221aSStephen M. Cameron {
2147316b221aSStephen M. Cameron 	int rc = IO_OK;
2148316b221aSStephen M. Cameron 	struct CommandList *c;
2149316b221aSStephen M. Cameron 	struct ErrorInfo *ei;
2150316b221aSStephen M. Cameron 
2151316b221aSStephen M. Cameron 	c = cmd_special_alloc(h);
2152316b221aSStephen M. Cameron 
2153316b221aSStephen M. Cameron 	if (c == NULL) {			/* trouble... */
2154316b221aSStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2155316b221aSStephen M. Cameron 		return -ENOMEM;
2156316b221aSStephen M. Cameron 	}
2157316b221aSStephen M. Cameron 
2158316b221aSStephen M. Cameron 	if (fill_cmd(c, BMIC_SENSE_CONTROLLER_PARAMETERS, h, buf, bufsize,
2159316b221aSStephen M. Cameron 			page, scsi3addr, TYPE_CMD)) {
2160316b221aSStephen M. Cameron 		rc = -1;
2161316b221aSStephen M. Cameron 		goto out;
2162316b221aSStephen M. Cameron 	}
2163316b221aSStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2164316b221aSStephen M. Cameron 	ei = c->err_info;
2165316b221aSStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2166316b221aSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2167316b221aSStephen M. Cameron 		rc = -1;
2168316b221aSStephen M. Cameron 	}
2169316b221aSStephen M. Cameron out:
2170316b221aSStephen M. Cameron 	cmd_special_free(h, c);
2171316b221aSStephen M. Cameron 	return rc;
2172316b221aSStephen M. Cameron 	}
2173316b221aSStephen M. Cameron 
2174bf711ac6SScott Teel static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
2175bf711ac6SScott Teel 	u8 reset_type)
2176edd16368SStephen M. Cameron {
2177edd16368SStephen M. Cameron 	int rc = IO_OK;
2178edd16368SStephen M. Cameron 	struct CommandList *c;
2179edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2180edd16368SStephen M. Cameron 
2181edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
2182edd16368SStephen M. Cameron 
2183edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
2184edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2185e9ea04a6SStephen M. Cameron 		return -ENOMEM;
2186edd16368SStephen M. Cameron 	}
2187edd16368SStephen M. Cameron 
2188a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map. */
2189bf711ac6SScott Teel 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
2190bf711ac6SScott Teel 			scsi3addr, TYPE_MSG);
2191bf711ac6SScott Teel 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to LUN reset */
2192edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core(h, c);
2193edd16368SStephen M. Cameron 	/* no unmap needed here because no data xfer. */
2194edd16368SStephen M. Cameron 
2195edd16368SStephen M. Cameron 	ei = c->err_info;
2196edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0) {
2197d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2198edd16368SStephen M. Cameron 		rc = -1;
2199edd16368SStephen M. Cameron 	}
2200edd16368SStephen M. Cameron 	cmd_special_free(h, c);
2201edd16368SStephen M. Cameron 	return rc;
2202edd16368SStephen M. Cameron }
2203edd16368SStephen M. Cameron 
2204edd16368SStephen M. Cameron static void hpsa_get_raid_level(struct ctlr_info *h,
2205edd16368SStephen M. Cameron 	unsigned char *scsi3addr, unsigned char *raid_level)
2206edd16368SStephen M. Cameron {
2207edd16368SStephen M. Cameron 	int rc;
2208edd16368SStephen M. Cameron 	unsigned char *buf;
2209edd16368SStephen M. Cameron 
2210edd16368SStephen M. Cameron 	*raid_level = RAID_UNKNOWN;
2211edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2212edd16368SStephen M. Cameron 	if (!buf)
2213edd16368SStephen M. Cameron 		return;
2214b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0xC1, buf, 64);
2215edd16368SStephen M. Cameron 	if (rc == 0)
2216edd16368SStephen M. Cameron 		*raid_level = buf[8];
2217edd16368SStephen M. Cameron 	if (*raid_level > RAID_UNKNOWN)
2218edd16368SStephen M. Cameron 		*raid_level = RAID_UNKNOWN;
2219edd16368SStephen M. Cameron 	kfree(buf);
2220edd16368SStephen M. Cameron 	return;
2221edd16368SStephen M. Cameron }
2222edd16368SStephen M. Cameron 
2223283b4a9bSStephen M. Cameron #define HPSA_MAP_DEBUG
2224283b4a9bSStephen M. Cameron #ifdef HPSA_MAP_DEBUG
2225283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
2226283b4a9bSStephen M. Cameron 				struct raid_map_data *map_buff)
2227283b4a9bSStephen M. Cameron {
2228283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map_buff->data[0];
2229283b4a9bSStephen M. Cameron 	int map, row, col;
2230283b4a9bSStephen M. Cameron 	u16 map_cnt, row_cnt, disks_per_row;
2231283b4a9bSStephen M. Cameron 
2232283b4a9bSStephen M. Cameron 	if (rc != 0)
2233283b4a9bSStephen M. Cameron 		return;
2234283b4a9bSStephen M. Cameron 
22352ba8bfc8SStephen M. Cameron 	/* Show details only if debugging has been activated. */
22362ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug < 2)
22372ba8bfc8SStephen M. Cameron 		return;
22382ba8bfc8SStephen M. Cameron 
2239283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "structure_size = %u\n",
2240283b4a9bSStephen M. Cameron 				le32_to_cpu(map_buff->structure_size));
2241283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
2242283b4a9bSStephen M. Cameron 			le32_to_cpu(map_buff->volume_blk_size));
2243283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
2244283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->volume_blk_cnt));
2245283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
2246283b4a9bSStephen M. Cameron 			map_buff->phys_blk_shift);
2247283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
2248283b4a9bSStephen M. Cameron 			map_buff->parity_rotation_shift);
2249283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "strip_size = %u\n",
2250283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->strip_size));
2251283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
2252283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_starting_blk));
2253283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
2254283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_blk_cnt));
2255283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
2256283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->data_disks_per_row));
2257283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
2258283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->metadata_disks_per_row));
2259283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "row_cnt = %u\n",
2260283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->row_cnt));
2261283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "layout_map_count = %u\n",
2262283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->layout_map_count));
2263dd0e19f3SScott Teel 	dev_info(&h->pdev->dev, "flags = %u\n",
2264dd0e19f3SScott Teel 			le16_to_cpu(map_buff->flags));
2265dd0e19f3SScott Teel 	if (map_buff->flags & RAID_MAP_FLAG_ENCRYPT_ON)
2266dd0e19f3SScott Teel 		dev_info(&h->pdev->dev, "encrypytion = ON\n");
2267dd0e19f3SScott Teel 	else
2268dd0e19f3SScott Teel 		dev_info(&h->pdev->dev, "encrypytion = OFF\n");
2269dd0e19f3SScott Teel 	dev_info(&h->pdev->dev, "dekindex = %u\n",
2270dd0e19f3SScott Teel 			le16_to_cpu(map_buff->dekindex));
2271283b4a9bSStephen M. Cameron 
2272283b4a9bSStephen M. Cameron 	map_cnt = le16_to_cpu(map_buff->layout_map_count);
2273283b4a9bSStephen M. Cameron 	for (map = 0; map < map_cnt; map++) {
2274283b4a9bSStephen M. Cameron 		dev_info(&h->pdev->dev, "Map%u:\n", map);
2275283b4a9bSStephen M. Cameron 		row_cnt = le16_to_cpu(map_buff->row_cnt);
2276283b4a9bSStephen M. Cameron 		for (row = 0; row < row_cnt; row++) {
2277283b4a9bSStephen M. Cameron 			dev_info(&h->pdev->dev, "  Row%u:\n", row);
2278283b4a9bSStephen M. Cameron 			disks_per_row =
2279283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->data_disks_per_row);
2280283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2281283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2282283b4a9bSStephen M. Cameron 					"    D%02u: h=0x%04x xor=%u,%u\n",
2283283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2284283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2285283b4a9bSStephen M. Cameron 			disks_per_row =
2286283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->metadata_disks_per_row);
2287283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2288283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2289283b4a9bSStephen M. Cameron 					"    M%02u: h=0x%04x xor=%u,%u\n",
2290283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2291283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2292283b4a9bSStephen M. Cameron 		}
2293283b4a9bSStephen M. Cameron 	}
2294283b4a9bSStephen M. Cameron }
2295283b4a9bSStephen M. Cameron #else
2296283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
2297283b4a9bSStephen M. Cameron 			__attribute__((unused)) int rc,
2298283b4a9bSStephen M. Cameron 			__attribute__((unused)) struct raid_map_data *map_buff)
2299283b4a9bSStephen M. Cameron {
2300283b4a9bSStephen M. Cameron }
2301283b4a9bSStephen M. Cameron #endif
2302283b4a9bSStephen M. Cameron 
2303283b4a9bSStephen M. Cameron static int hpsa_get_raid_map(struct ctlr_info *h,
2304283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2305283b4a9bSStephen M. Cameron {
2306283b4a9bSStephen M. Cameron 	int rc = 0;
2307283b4a9bSStephen M. Cameron 	struct CommandList *c;
2308283b4a9bSStephen M. Cameron 	struct ErrorInfo *ei;
2309283b4a9bSStephen M. Cameron 
2310283b4a9bSStephen M. Cameron 	c = cmd_special_alloc(h);
2311283b4a9bSStephen M. Cameron 	if (c == NULL) {
2312283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2313283b4a9bSStephen M. Cameron 		return -ENOMEM;
2314283b4a9bSStephen M. Cameron 	}
2315283b4a9bSStephen M. Cameron 	if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
2316283b4a9bSStephen M. Cameron 			sizeof(this_device->raid_map), 0,
2317283b4a9bSStephen M. Cameron 			scsi3addr, TYPE_CMD)) {
2318283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "Out of memory in hpsa_get_raid_map()\n");
2319283b4a9bSStephen M. Cameron 		cmd_special_free(h, c);
2320283b4a9bSStephen M. Cameron 		return -ENOMEM;
2321283b4a9bSStephen M. Cameron 	}
2322283b4a9bSStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2323283b4a9bSStephen M. Cameron 	ei = c->err_info;
2324283b4a9bSStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2325d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2326283b4a9bSStephen M. Cameron 		cmd_special_free(h, c);
2327283b4a9bSStephen M. Cameron 		return -1;
2328283b4a9bSStephen M. Cameron 	}
2329283b4a9bSStephen M. Cameron 	cmd_special_free(h, c);
2330283b4a9bSStephen M. Cameron 
2331283b4a9bSStephen M. Cameron 	/* @todo in the future, dynamically allocate RAID map memory */
2332283b4a9bSStephen M. Cameron 	if (le32_to_cpu(this_device->raid_map.structure_size) >
2333283b4a9bSStephen M. Cameron 				sizeof(this_device->raid_map)) {
2334283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
2335283b4a9bSStephen M. Cameron 		rc = -1;
2336283b4a9bSStephen M. Cameron 	}
2337283b4a9bSStephen M. Cameron 	hpsa_debug_map_buff(h, rc, &this_device->raid_map);
2338283b4a9bSStephen M. Cameron 	return rc;
2339283b4a9bSStephen M. Cameron }
2340283b4a9bSStephen M. Cameron 
23411b70150aSStephen M. Cameron static int hpsa_vpd_page_supported(struct ctlr_info *h,
23421b70150aSStephen M. Cameron 	unsigned char scsi3addr[], u8 page)
23431b70150aSStephen M. Cameron {
23441b70150aSStephen M. Cameron 	int rc;
23451b70150aSStephen M. Cameron 	int i;
23461b70150aSStephen M. Cameron 	int pages;
23471b70150aSStephen M. Cameron 	unsigned char *buf, bufsize;
23481b70150aSStephen M. Cameron 
23491b70150aSStephen M. Cameron 	buf = kzalloc(256, GFP_KERNEL);
23501b70150aSStephen M. Cameron 	if (!buf)
23511b70150aSStephen M. Cameron 		return 0;
23521b70150aSStephen M. Cameron 
23531b70150aSStephen M. Cameron 	/* Get the size of the page list first */
23541b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
23551b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
23561b70150aSStephen M. Cameron 				buf, HPSA_VPD_HEADER_SZ);
23571b70150aSStephen M. Cameron 	if (rc != 0)
23581b70150aSStephen M. Cameron 		goto exit_unsupported;
23591b70150aSStephen M. Cameron 	pages = buf[3];
23601b70150aSStephen M. Cameron 	if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
23611b70150aSStephen M. Cameron 		bufsize = pages + HPSA_VPD_HEADER_SZ;
23621b70150aSStephen M. Cameron 	else
23631b70150aSStephen M. Cameron 		bufsize = 255;
23641b70150aSStephen M. Cameron 
23651b70150aSStephen M. Cameron 	/* Get the whole VPD page list */
23661b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
23671b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
23681b70150aSStephen M. Cameron 				buf, bufsize);
23691b70150aSStephen M. Cameron 	if (rc != 0)
23701b70150aSStephen M. Cameron 		goto exit_unsupported;
23711b70150aSStephen M. Cameron 
23721b70150aSStephen M. Cameron 	pages = buf[3];
23731b70150aSStephen M. Cameron 	for (i = 1; i <= pages; i++)
23741b70150aSStephen M. Cameron 		if (buf[3 + i] == page)
23751b70150aSStephen M. Cameron 			goto exit_supported;
23761b70150aSStephen M. Cameron exit_unsupported:
23771b70150aSStephen M. Cameron 	kfree(buf);
23781b70150aSStephen M. Cameron 	return 0;
23791b70150aSStephen M. Cameron exit_supported:
23801b70150aSStephen M. Cameron 	kfree(buf);
23811b70150aSStephen M. Cameron 	return 1;
23821b70150aSStephen M. Cameron }
23831b70150aSStephen M. Cameron 
2384283b4a9bSStephen M. Cameron static void hpsa_get_ioaccel_status(struct ctlr_info *h,
2385283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2386283b4a9bSStephen M. Cameron {
2387283b4a9bSStephen M. Cameron 	int rc;
2388283b4a9bSStephen M. Cameron 	unsigned char *buf;
2389283b4a9bSStephen M. Cameron 	u8 ioaccel_status;
2390283b4a9bSStephen M. Cameron 
2391283b4a9bSStephen M. Cameron 	this_device->offload_config = 0;
2392283b4a9bSStephen M. Cameron 	this_device->offload_enabled = 0;
2393283b4a9bSStephen M. Cameron 
2394283b4a9bSStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2395283b4a9bSStephen M. Cameron 	if (!buf)
2396283b4a9bSStephen M. Cameron 		return;
23971b70150aSStephen M. Cameron 	if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
23981b70150aSStephen M. Cameron 		goto out;
2399283b4a9bSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
2400b7bb24ebSStephen M. Cameron 			VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
2401283b4a9bSStephen M. Cameron 	if (rc != 0)
2402283b4a9bSStephen M. Cameron 		goto out;
2403283b4a9bSStephen M. Cameron 
2404283b4a9bSStephen M. Cameron #define IOACCEL_STATUS_BYTE 4
2405283b4a9bSStephen M. Cameron #define OFFLOAD_CONFIGURED_BIT 0x01
2406283b4a9bSStephen M. Cameron #define OFFLOAD_ENABLED_BIT 0x02
2407283b4a9bSStephen M. Cameron 	ioaccel_status = buf[IOACCEL_STATUS_BYTE];
2408283b4a9bSStephen M. Cameron 	this_device->offload_config =
2409283b4a9bSStephen M. Cameron 		!!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
2410283b4a9bSStephen M. Cameron 	if (this_device->offload_config) {
2411283b4a9bSStephen M. Cameron 		this_device->offload_enabled =
2412283b4a9bSStephen M. Cameron 			!!(ioaccel_status & OFFLOAD_ENABLED_BIT);
2413283b4a9bSStephen M. Cameron 		if (hpsa_get_raid_map(h, scsi3addr, this_device))
2414283b4a9bSStephen M. Cameron 			this_device->offload_enabled = 0;
2415283b4a9bSStephen M. Cameron 	}
2416283b4a9bSStephen M. Cameron out:
2417283b4a9bSStephen M. Cameron 	kfree(buf);
2418283b4a9bSStephen M. Cameron 	return;
2419283b4a9bSStephen M. Cameron }
2420283b4a9bSStephen M. Cameron 
2421edd16368SStephen M. Cameron /* Get the device id from inquiry page 0x83 */
2422edd16368SStephen M. Cameron static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
2423edd16368SStephen M. Cameron 	unsigned char *device_id, int buflen)
2424edd16368SStephen M. Cameron {
2425edd16368SStephen M. Cameron 	int rc;
2426edd16368SStephen M. Cameron 	unsigned char *buf;
2427edd16368SStephen M. Cameron 
2428edd16368SStephen M. Cameron 	if (buflen > 16)
2429edd16368SStephen M. Cameron 		buflen = 16;
2430edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2431edd16368SStephen M. Cameron 	if (!buf)
2432edd16368SStephen M. Cameron 		return -1;
2433b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0x83, buf, 64);
2434edd16368SStephen M. Cameron 	if (rc == 0)
2435edd16368SStephen M. Cameron 		memcpy(device_id, &buf[8], buflen);
2436edd16368SStephen M. Cameron 	kfree(buf);
2437edd16368SStephen M. Cameron 	return rc != 0;
2438edd16368SStephen M. Cameron }
2439edd16368SStephen M. Cameron 
2440edd16368SStephen M. Cameron static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
2441edd16368SStephen M. Cameron 		struct ReportLUNdata *buf, int bufsize,
2442edd16368SStephen M. Cameron 		int extended_response)
2443edd16368SStephen M. Cameron {
2444edd16368SStephen M. Cameron 	int rc = IO_OK;
2445edd16368SStephen M. Cameron 	struct CommandList *c;
2446edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
2447edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2448edd16368SStephen M. Cameron 
2449edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
2450edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
2451edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2452edd16368SStephen M. Cameron 		return -1;
2453edd16368SStephen M. Cameron 	}
2454e89c0ae7SStephen M. Cameron 	/* address the controller */
2455e89c0ae7SStephen M. Cameron 	memset(scsi3addr, 0, sizeof(scsi3addr));
2456a2dac136SStephen M. Cameron 	if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
2457a2dac136SStephen M. Cameron 		buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
2458a2dac136SStephen M. Cameron 		rc = -1;
2459a2dac136SStephen M. Cameron 		goto out;
2460a2dac136SStephen M. Cameron 	}
2461edd16368SStephen M. Cameron 	if (extended_response)
2462edd16368SStephen M. Cameron 		c->Request.CDB[1] = extended_response;
2463edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2464edd16368SStephen M. Cameron 	ei = c->err_info;
2465edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 &&
2466edd16368SStephen M. Cameron 	    ei->CommandStatus != CMD_DATA_UNDERRUN) {
2467d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2468edd16368SStephen M. Cameron 		rc = -1;
2469283b4a9bSStephen M. Cameron 	} else {
2470283b4a9bSStephen M. Cameron 		if (buf->extended_response_flag != extended_response) {
2471283b4a9bSStephen M. Cameron 			dev_err(&h->pdev->dev,
2472283b4a9bSStephen M. Cameron 				"report luns requested format %u, got %u\n",
2473283b4a9bSStephen M. Cameron 				extended_response,
2474283b4a9bSStephen M. Cameron 				buf->extended_response_flag);
2475283b4a9bSStephen M. Cameron 			rc = -1;
2476283b4a9bSStephen M. Cameron 		}
2477edd16368SStephen M. Cameron 	}
2478a2dac136SStephen M. Cameron out:
2479edd16368SStephen M. Cameron 	cmd_special_free(h, c);
2480edd16368SStephen M. Cameron 	return rc;
2481edd16368SStephen M. Cameron }
2482edd16368SStephen M. Cameron 
2483edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
2484edd16368SStephen M. Cameron 		struct ReportLUNdata *buf,
2485edd16368SStephen M. Cameron 		int bufsize, int extended_response)
2486edd16368SStephen M. Cameron {
2487edd16368SStephen M. Cameron 	return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
2488edd16368SStephen M. Cameron }
2489edd16368SStephen M. Cameron 
2490edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
2491edd16368SStephen M. Cameron 		struct ReportLUNdata *buf, int bufsize)
2492edd16368SStephen M. Cameron {
2493edd16368SStephen M. Cameron 	return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
2494edd16368SStephen M. Cameron }
2495edd16368SStephen M. Cameron 
2496edd16368SStephen M. Cameron static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
2497edd16368SStephen M. Cameron 	int bus, int target, int lun)
2498edd16368SStephen M. Cameron {
2499edd16368SStephen M. Cameron 	device->bus = bus;
2500edd16368SStephen M. Cameron 	device->target = target;
2501edd16368SStephen M. Cameron 	device->lun = lun;
2502edd16368SStephen M. Cameron }
2503edd16368SStephen M. Cameron 
25049846590eSStephen M. Cameron /* Use VPD inquiry to get details of volume status */
25059846590eSStephen M. Cameron static int hpsa_get_volume_status(struct ctlr_info *h,
25069846590eSStephen M. Cameron 					unsigned char scsi3addr[])
25079846590eSStephen M. Cameron {
25089846590eSStephen M. Cameron 	int rc;
25099846590eSStephen M. Cameron 	int status;
25109846590eSStephen M. Cameron 	int size;
25119846590eSStephen M. Cameron 	unsigned char *buf;
25129846590eSStephen M. Cameron 
25139846590eSStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
25149846590eSStephen M. Cameron 	if (!buf)
25159846590eSStephen M. Cameron 		return HPSA_VPD_LV_STATUS_UNSUPPORTED;
25169846590eSStephen M. Cameron 
25179846590eSStephen M. Cameron 	/* Does controller have VPD for logical volume status? */
25189846590eSStephen M. Cameron 	if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_STATUS)) {
25199846590eSStephen M. Cameron 		dev_warn(&h->pdev->dev, "Logical volume status VPD page is unsupported.\n");
25209846590eSStephen M. Cameron 		goto exit_failed;
25219846590eSStephen M. Cameron 	}
25229846590eSStephen M. Cameron 
25239846590eSStephen M. Cameron 	/* Get the size of the VPD return buffer */
25249846590eSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
25259846590eSStephen M. Cameron 					buf, HPSA_VPD_HEADER_SZ);
25269846590eSStephen M. Cameron 	if (rc != 0) {
25279846590eSStephen M. Cameron 		dev_warn(&h->pdev->dev, "Logical volume status VPD inquiry failed.\n");
25289846590eSStephen M. Cameron 		goto exit_failed;
25299846590eSStephen M. Cameron 	}
25309846590eSStephen M. Cameron 	size = buf[3];
25319846590eSStephen M. Cameron 
25329846590eSStephen M. Cameron 	/* Now get the whole VPD buffer */
25339846590eSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
25349846590eSStephen M. Cameron 					buf, size + HPSA_VPD_HEADER_SZ);
25359846590eSStephen M. Cameron 	if (rc != 0) {
25369846590eSStephen M. Cameron 		dev_warn(&h->pdev->dev, "Logical volume status VPD inquiry failed.\n");
25379846590eSStephen M. Cameron 		goto exit_failed;
25389846590eSStephen M. Cameron 	}
25399846590eSStephen M. Cameron 	status = buf[4]; /* status byte */
25409846590eSStephen M. Cameron 
25419846590eSStephen M. Cameron 	kfree(buf);
25429846590eSStephen M. Cameron 	return status;
25439846590eSStephen M. Cameron exit_failed:
25449846590eSStephen M. Cameron 	kfree(buf);
25459846590eSStephen M. Cameron 	return HPSA_VPD_LV_STATUS_UNSUPPORTED;
25469846590eSStephen M. Cameron }
25479846590eSStephen M. Cameron 
25489846590eSStephen M. Cameron /* Determine offline status of a volume.
25499846590eSStephen M. Cameron  * Return either:
25509846590eSStephen M. Cameron  *  0 (not offline)
25519846590eSStephen M. Cameron  * -1 (offline for unknown reasons)
25529846590eSStephen M. Cameron  *  # (integer code indicating one of several NOT READY states
25539846590eSStephen M. Cameron  *     describing why a volume is to be kept offline)
25549846590eSStephen M. Cameron  */
25559846590eSStephen M. Cameron static unsigned char hpsa_volume_offline(struct ctlr_info *h,
25569846590eSStephen M. Cameron 					unsigned char scsi3addr[])
25579846590eSStephen M. Cameron {
25589846590eSStephen M. Cameron 	struct CommandList *c;
25599846590eSStephen M. Cameron 	unsigned char *sense, sense_key, asc, ascq;
25609846590eSStephen M. Cameron 	int ldstat = 0;
25619846590eSStephen M. Cameron 	u16 cmd_status;
25629846590eSStephen M. Cameron 	u8 scsi_status;
25639846590eSStephen M. Cameron #define ASC_LUN_NOT_READY 0x04
25649846590eSStephen M. Cameron #define ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS 0x04
25659846590eSStephen M. Cameron #define ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ 0x02
25669846590eSStephen M. Cameron 
25679846590eSStephen M. Cameron 	c = cmd_alloc(h);
25689846590eSStephen M. Cameron 	if (!c)
25699846590eSStephen M. Cameron 		return 0;
25709846590eSStephen M. Cameron 	(void) fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, scsi3addr, TYPE_CMD);
25719846590eSStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core(h, c);
25729846590eSStephen M. Cameron 	sense = c->err_info->SenseInfo;
25739846590eSStephen M. Cameron 	sense_key = sense[2];
25749846590eSStephen M. Cameron 	asc = sense[12];
25759846590eSStephen M. Cameron 	ascq = sense[13];
25769846590eSStephen M. Cameron 	cmd_status = c->err_info->CommandStatus;
25779846590eSStephen M. Cameron 	scsi_status = c->err_info->ScsiStatus;
25789846590eSStephen M. Cameron 	cmd_free(h, c);
25799846590eSStephen M. Cameron 	/* Is the volume 'not ready'? */
25809846590eSStephen M. Cameron 	if (cmd_status != CMD_TARGET_STATUS ||
25819846590eSStephen M. Cameron 		scsi_status != SAM_STAT_CHECK_CONDITION ||
25829846590eSStephen M. Cameron 		sense_key != NOT_READY ||
25839846590eSStephen M. Cameron 		asc != ASC_LUN_NOT_READY)  {
25849846590eSStephen M. Cameron 		return 0;
25859846590eSStephen M. Cameron 	}
25869846590eSStephen M. Cameron 
25879846590eSStephen M. Cameron 	/* Determine the reason for not ready state */
25889846590eSStephen M. Cameron 	ldstat = hpsa_get_volume_status(h, scsi3addr);
25899846590eSStephen M. Cameron 
25909846590eSStephen M. Cameron 	/* Keep volume offline in certain cases: */
25919846590eSStephen M. Cameron 	switch (ldstat) {
25929846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ERASE:
25939846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_RPI:
25949846590eSStephen M. Cameron 	case HPSA_LV_PENDING_RPI:
25959846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_NO_KEY:
25969846590eSStephen M. Cameron 	case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
25979846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION:
25989846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
25999846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
26009846590eSStephen M. Cameron 		return ldstat;
26019846590eSStephen M. Cameron 	case HPSA_VPD_LV_STATUS_UNSUPPORTED:
26029846590eSStephen M. Cameron 		/* If VPD status page isn't available,
26039846590eSStephen M. Cameron 		 * use ASC/ASCQ to determine state
26049846590eSStephen M. Cameron 		 */
26059846590eSStephen M. Cameron 		if ((ascq == ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS) ||
26069846590eSStephen M. Cameron 			(ascq == ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ))
26079846590eSStephen M. Cameron 			return ldstat;
26089846590eSStephen M. Cameron 		break;
26099846590eSStephen M. Cameron 	default:
26109846590eSStephen M. Cameron 		break;
26119846590eSStephen M. Cameron 	}
26129846590eSStephen M. Cameron 	return 0;
26139846590eSStephen M. Cameron }
26149846590eSStephen M. Cameron 
2615edd16368SStephen M. Cameron static int hpsa_update_device_info(struct ctlr_info *h,
26160b0e1d6cSStephen M. Cameron 	unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
26170b0e1d6cSStephen M. Cameron 	unsigned char *is_OBDR_device)
2618edd16368SStephen M. Cameron {
26190b0e1d6cSStephen M. Cameron 
26200b0e1d6cSStephen M. Cameron #define OBDR_SIG_OFFSET 43
26210b0e1d6cSStephen M. Cameron #define OBDR_TAPE_SIG "$DR-10"
26220b0e1d6cSStephen M. Cameron #define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
26230b0e1d6cSStephen M. Cameron #define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
26240b0e1d6cSStephen M. Cameron 
2625ea6d3bc3SStephen M. Cameron 	unsigned char *inq_buff;
26260b0e1d6cSStephen M. Cameron 	unsigned char *obdr_sig;
2627edd16368SStephen M. Cameron 
2628ea6d3bc3SStephen M. Cameron 	inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
2629edd16368SStephen M. Cameron 	if (!inq_buff)
2630edd16368SStephen M. Cameron 		goto bail_out;
2631edd16368SStephen M. Cameron 
2632edd16368SStephen M. Cameron 	/* Do an inquiry to the device to see what it is. */
2633edd16368SStephen M. Cameron 	if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
2634edd16368SStephen M. Cameron 		(unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
2635edd16368SStephen M. Cameron 		/* Inquiry failed (msg printed already) */
2636edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev,
2637edd16368SStephen M. Cameron 			"hpsa_update_device_info: inquiry failed\n");
2638edd16368SStephen M. Cameron 		goto bail_out;
2639edd16368SStephen M. Cameron 	}
2640edd16368SStephen M. Cameron 
2641edd16368SStephen M. Cameron 	this_device->devtype = (inq_buff[0] & 0x1f);
2642edd16368SStephen M. Cameron 	memcpy(this_device->scsi3addr, scsi3addr, 8);
2643edd16368SStephen M. Cameron 	memcpy(this_device->vendor, &inq_buff[8],
2644edd16368SStephen M. Cameron 		sizeof(this_device->vendor));
2645edd16368SStephen M. Cameron 	memcpy(this_device->model, &inq_buff[16],
2646edd16368SStephen M. Cameron 		sizeof(this_device->model));
2647edd16368SStephen M. Cameron 	memset(this_device->device_id, 0,
2648edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
2649edd16368SStephen M. Cameron 	hpsa_get_device_id(h, scsi3addr, this_device->device_id,
2650edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
2651edd16368SStephen M. Cameron 
2652edd16368SStephen M. Cameron 	if (this_device->devtype == TYPE_DISK &&
2653283b4a9bSStephen M. Cameron 		is_logical_dev_addr_mode(scsi3addr)) {
2654edd16368SStephen M. Cameron 		hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
2655283b4a9bSStephen M. Cameron 		if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
2656283b4a9bSStephen M. Cameron 			hpsa_get_ioaccel_status(h, scsi3addr, this_device);
26579846590eSStephen M. Cameron 		this_device->volume_offline =
26589846590eSStephen M. Cameron 			hpsa_volume_offline(h, scsi3addr);
2659283b4a9bSStephen M. Cameron 	} else {
2660edd16368SStephen M. Cameron 		this_device->raid_level = RAID_UNKNOWN;
2661283b4a9bSStephen M. Cameron 		this_device->offload_config = 0;
2662283b4a9bSStephen M. Cameron 		this_device->offload_enabled = 0;
26639846590eSStephen M. Cameron 		this_device->volume_offline = 0;
2664283b4a9bSStephen M. Cameron 	}
2665edd16368SStephen M. Cameron 
26660b0e1d6cSStephen M. Cameron 	if (is_OBDR_device) {
26670b0e1d6cSStephen M. Cameron 		/* See if this is a One-Button-Disaster-Recovery device
26680b0e1d6cSStephen M. Cameron 		 * by looking for "$DR-10" at offset 43 in inquiry data.
26690b0e1d6cSStephen M. Cameron 		 */
26700b0e1d6cSStephen M. Cameron 		obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
26710b0e1d6cSStephen M. Cameron 		*is_OBDR_device = (this_device->devtype == TYPE_ROM &&
26720b0e1d6cSStephen M. Cameron 					strncmp(obdr_sig, OBDR_TAPE_SIG,
26730b0e1d6cSStephen M. Cameron 						OBDR_SIG_LEN) == 0);
26740b0e1d6cSStephen M. Cameron 	}
26750b0e1d6cSStephen M. Cameron 
2676edd16368SStephen M. Cameron 	kfree(inq_buff);
2677edd16368SStephen M. Cameron 	return 0;
2678edd16368SStephen M. Cameron 
2679edd16368SStephen M. Cameron bail_out:
2680edd16368SStephen M. Cameron 	kfree(inq_buff);
2681edd16368SStephen M. Cameron 	return 1;
2682edd16368SStephen M. Cameron }
2683edd16368SStephen M. Cameron 
26844f4eb9f1SScott Teel static unsigned char *ext_target_model[] = {
2685edd16368SStephen M. Cameron 	"MSA2012",
2686edd16368SStephen M. Cameron 	"MSA2024",
2687edd16368SStephen M. Cameron 	"MSA2312",
2688edd16368SStephen M. Cameron 	"MSA2324",
2689fda38518SStephen M. Cameron 	"P2000 G3 SAS",
2690e06c8e5cSStephen M. Cameron 	"MSA 2040 SAS",
2691edd16368SStephen M. Cameron 	NULL,
2692edd16368SStephen M. Cameron };
2693edd16368SStephen M. Cameron 
26944f4eb9f1SScott Teel static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
2695edd16368SStephen M. Cameron {
2696edd16368SStephen M. Cameron 	int i;
2697edd16368SStephen M. Cameron 
26984f4eb9f1SScott Teel 	for (i = 0; ext_target_model[i]; i++)
26994f4eb9f1SScott Teel 		if (strncmp(device->model, ext_target_model[i],
27004f4eb9f1SScott Teel 			strlen(ext_target_model[i])) == 0)
2701edd16368SStephen M. Cameron 			return 1;
2702edd16368SStephen M. Cameron 	return 0;
2703edd16368SStephen M. Cameron }
2704edd16368SStephen M. Cameron 
2705edd16368SStephen M. Cameron /* Helper function to assign bus, target, lun mapping of devices.
27064f4eb9f1SScott Teel  * Puts non-external target logical volumes on bus 0, external target logical
2707edd16368SStephen M. Cameron  * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
2708edd16368SStephen M. Cameron  * Logical drive target and lun are assigned at this time, but
2709edd16368SStephen M. Cameron  * physical device lun and target assignment are deferred (assigned
2710edd16368SStephen M. Cameron  * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
2711edd16368SStephen M. Cameron  */
2712edd16368SStephen M. Cameron static void figure_bus_target_lun(struct ctlr_info *h,
27131f310bdeSStephen M. Cameron 	u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
2714edd16368SStephen M. Cameron {
27151f310bdeSStephen M. Cameron 	u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
2716edd16368SStephen M. Cameron 
27171f310bdeSStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes)) {
27181f310bdeSStephen M. Cameron 		/* physical device, target and lun filled in later */
27191f310bdeSStephen M. Cameron 		if (is_hba_lunid(lunaddrbytes))
27201f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
27211f310bdeSStephen M. Cameron 		else
27221f310bdeSStephen M. Cameron 			/* defer target, lun assignment for physical devices */
27231f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 2, -1, -1);
27241f310bdeSStephen M. Cameron 		return;
27251f310bdeSStephen M. Cameron 	}
27261f310bdeSStephen M. Cameron 	/* It's a logical device */
27274f4eb9f1SScott Teel 	if (is_ext_target(h, device)) {
27284f4eb9f1SScott Teel 		/* external target way, put logicals on bus 1
2729339b2b14SStephen M. Cameron 		 * and match target/lun numbers box
27301f310bdeSStephen M. Cameron 		 * reports, other smart array, bus 0, target 0, match lunid
2731339b2b14SStephen M. Cameron 		 */
27321f310bdeSStephen M. Cameron 		hpsa_set_bus_target_lun(device,
27331f310bdeSStephen M. Cameron 			1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
27341f310bdeSStephen M. Cameron 		return;
2735339b2b14SStephen M. Cameron 	}
27361f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
2737edd16368SStephen M. Cameron }
2738edd16368SStephen M. Cameron 
2739edd16368SStephen M. Cameron /*
2740edd16368SStephen M. Cameron  * If there is no lun 0 on a target, linux won't find any devices.
27414f4eb9f1SScott Teel  * For the external targets (arrays), we have to manually detect the enclosure
2742edd16368SStephen M. Cameron  * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
2743edd16368SStephen M. Cameron  * it for some reason.  *tmpdevice is the target we're adding,
2744edd16368SStephen M. Cameron  * this_device is a pointer into the current element of currentsd[]
2745edd16368SStephen M. Cameron  * that we're building up in update_scsi_devices(), below.
2746edd16368SStephen M. Cameron  * lunzerobits is a bitmap that tracks which targets already have a
2747edd16368SStephen M. Cameron  * lun 0 assigned.
2748edd16368SStephen M. Cameron  * Returns 1 if an enclosure was added, 0 if not.
2749edd16368SStephen M. Cameron  */
27504f4eb9f1SScott Teel static int add_ext_target_dev(struct ctlr_info *h,
2751edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *tmpdevice,
275201a02ffcSStephen M. Cameron 	struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
27534f4eb9f1SScott Teel 	unsigned long lunzerobits[], int *n_ext_target_devs)
2754edd16368SStephen M. Cameron {
2755edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
2756edd16368SStephen M. Cameron 
27571f310bdeSStephen M. Cameron 	if (test_bit(tmpdevice->target, lunzerobits))
2758edd16368SStephen M. Cameron 		return 0; /* There is already a lun 0 on this target. */
2759edd16368SStephen M. Cameron 
2760edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes))
2761edd16368SStephen M. Cameron 		return 0; /* It's the logical targets that may lack lun 0. */
2762edd16368SStephen M. Cameron 
27634f4eb9f1SScott Teel 	if (!is_ext_target(h, tmpdevice))
27644f4eb9f1SScott Teel 		return 0; /* Only external target devices have this problem. */
2765edd16368SStephen M. Cameron 
27661f310bdeSStephen M. Cameron 	if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
2767edd16368SStephen M. Cameron 		return 0;
2768edd16368SStephen M. Cameron 
2769c4f8a299SStephen M. Cameron 	memset(scsi3addr, 0, 8);
27701f310bdeSStephen M. Cameron 	scsi3addr[3] = tmpdevice->target;
2771edd16368SStephen M. Cameron 	if (is_hba_lunid(scsi3addr))
2772edd16368SStephen M. Cameron 		return 0; /* Don't add the RAID controller here. */
2773edd16368SStephen M. Cameron 
2774339b2b14SStephen M. Cameron 	if (is_scsi_rev_5(h))
2775339b2b14SStephen M. Cameron 		return 0; /* p1210m doesn't need to do this. */
2776339b2b14SStephen M. Cameron 
27774f4eb9f1SScott Teel 	if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
2778aca4a520SScott Teel 		dev_warn(&h->pdev->dev, "Maximum number of external "
2779aca4a520SScott Teel 			"target devices exceeded.  Check your hardware "
2780edd16368SStephen M. Cameron 			"configuration.");
2781edd16368SStephen M. Cameron 		return 0;
2782edd16368SStephen M. Cameron 	}
2783edd16368SStephen M. Cameron 
27840b0e1d6cSStephen M. Cameron 	if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
2785edd16368SStephen M. Cameron 		return 0;
27864f4eb9f1SScott Teel 	(*n_ext_target_devs)++;
27871f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(this_device,
27881f310bdeSStephen M. Cameron 				tmpdevice->bus, tmpdevice->target, 0);
27891f310bdeSStephen M. Cameron 	set_bit(tmpdevice->target, lunzerobits);
2790edd16368SStephen M. Cameron 	return 1;
2791edd16368SStephen M. Cameron }
2792edd16368SStephen M. Cameron 
2793edd16368SStephen M. Cameron /*
279454b6e9e9SScott Teel  * Get address of physical disk used for an ioaccel2 mode command:
279554b6e9e9SScott Teel  *	1. Extract ioaccel2 handle from the command.
279654b6e9e9SScott Teel  *	2. Find a matching ioaccel2 handle from list of physical disks.
279754b6e9e9SScott Teel  *	3. Return:
279854b6e9e9SScott Teel  *		1 and set scsi3addr to address of matching physical
279954b6e9e9SScott Teel  *		0 if no matching physical disk was found.
280054b6e9e9SScott Teel  */
280154b6e9e9SScott Teel static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
280254b6e9e9SScott Teel 	struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
280354b6e9e9SScott Teel {
280454b6e9e9SScott Teel 	struct ReportExtendedLUNdata *physicals = NULL;
280554b6e9e9SScott Teel 	int responsesize = 24;	/* size of physical extended response */
280654b6e9e9SScott Teel 	int extended = 2;	/* flag forces reporting 'other dev info'. */
280754b6e9e9SScott Teel 	int reportsize = sizeof(*physicals) + HPSA_MAX_PHYS_LUN * responsesize;
280854b6e9e9SScott Teel 	u32 nphysicals = 0;	/* number of reported physical devs */
280954b6e9e9SScott Teel 	int found = 0;		/* found match (1) or not (0) */
281054b6e9e9SScott Teel 	u32 find;		/* handle we need to match */
281154b6e9e9SScott Teel 	int i;
281254b6e9e9SScott Teel 	struct scsi_cmnd *scmd;	/* scsi command within request being aborted */
281354b6e9e9SScott Teel 	struct hpsa_scsi_dev_t *d; /* device of request being aborted */
281454b6e9e9SScott Teel 	struct io_accel2_cmd *c2a; /* ioaccel2 command to abort */
281554b6e9e9SScott Teel 	u32 it_nexus;		/* 4 byte device handle for the ioaccel2 cmd */
281654b6e9e9SScott Teel 	u32 scsi_nexus;		/* 4 byte device handle for the ioaccel2 cmd */
281754b6e9e9SScott Teel 
281854b6e9e9SScott Teel 	if (ioaccel2_cmd_to_abort->cmd_type != CMD_IOACCEL2)
281954b6e9e9SScott Teel 		return 0; /* no match */
282054b6e9e9SScott Teel 
282154b6e9e9SScott Teel 	/* point to the ioaccel2 device handle */
282254b6e9e9SScott Teel 	c2a = &h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
282354b6e9e9SScott Teel 	if (c2a == NULL)
282454b6e9e9SScott Teel 		return 0; /* no match */
282554b6e9e9SScott Teel 
282654b6e9e9SScott Teel 	scmd = (struct scsi_cmnd *) ioaccel2_cmd_to_abort->scsi_cmd;
282754b6e9e9SScott Teel 	if (scmd == NULL)
282854b6e9e9SScott Teel 		return 0; /* no match */
282954b6e9e9SScott Teel 
283054b6e9e9SScott Teel 	d = scmd->device->hostdata;
283154b6e9e9SScott Teel 	if (d == NULL)
283254b6e9e9SScott Teel 		return 0; /* no match */
283354b6e9e9SScott Teel 
283454b6e9e9SScott Teel 	it_nexus = cpu_to_le32((u32) d->ioaccel_handle);
283554b6e9e9SScott Teel 	scsi_nexus = cpu_to_le32((u32) c2a->scsi_nexus);
283654b6e9e9SScott Teel 	find = c2a->scsi_nexus;
283754b6e9e9SScott Teel 
28382ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
28392ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
28402ba8bfc8SStephen 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",
28412ba8bfc8SStephen M. Cameron 			__func__, scsi_nexus,
28422ba8bfc8SStephen M. Cameron 			d->device_id[0], d->device_id[1], d->device_id[2],
28432ba8bfc8SStephen M. Cameron 			d->device_id[3], d->device_id[4], d->device_id[5],
28442ba8bfc8SStephen M. Cameron 			d->device_id[6], d->device_id[7], d->device_id[8],
28452ba8bfc8SStephen M. Cameron 			d->device_id[9], d->device_id[10], d->device_id[11],
28462ba8bfc8SStephen M. Cameron 			d->device_id[12], d->device_id[13], d->device_id[14],
28472ba8bfc8SStephen M. Cameron 			d->device_id[15]);
28482ba8bfc8SStephen M. Cameron 
284954b6e9e9SScott Teel 	/* Get the list of physical devices */
285054b6e9e9SScott Teel 	physicals = kzalloc(reportsize, GFP_KERNEL);
28513b51a7a3SJoe Handzik 	if (physicals == NULL)
28523b51a7a3SJoe Handzik 		return 0;
285354b6e9e9SScott Teel 	if (hpsa_scsi_do_report_phys_luns(h, (struct ReportLUNdata *) physicals,
285454b6e9e9SScott Teel 		reportsize, extended)) {
285554b6e9e9SScott Teel 		dev_err(&h->pdev->dev,
285654b6e9e9SScott Teel 			"Can't lookup %s device handle: report physical LUNs failed.\n",
285754b6e9e9SScott Teel 			"HP SSD Smart Path");
285854b6e9e9SScott Teel 		kfree(physicals);
285954b6e9e9SScott Teel 		return 0;
286054b6e9e9SScott Teel 	}
286154b6e9e9SScott Teel 	nphysicals = be32_to_cpu(*((__be32 *)physicals->LUNListLength)) /
286254b6e9e9SScott Teel 							responsesize;
286354b6e9e9SScott Teel 
286454b6e9e9SScott Teel 
286554b6e9e9SScott Teel 	/* find ioaccel2 handle in list of physicals: */
286654b6e9e9SScott Teel 	for (i = 0; i < nphysicals; i++) {
286754b6e9e9SScott Teel 		/* handle is in bytes 28-31 of each lun */
286854b6e9e9SScott Teel 		if (memcmp(&((struct ReportExtendedLUNdata *)
286954b6e9e9SScott Teel 				physicals)->LUN[i][20], &find, 4) != 0) {
287054b6e9e9SScott Teel 			continue; /* didn't match */
287154b6e9e9SScott Teel 		}
287254b6e9e9SScott Teel 		found = 1;
287354b6e9e9SScott Teel 		memcpy(scsi3addr, &((struct ReportExtendedLUNdata *)
287454b6e9e9SScott Teel 					physicals)->LUN[i][0], 8);
28752ba8bfc8SStephen M. Cameron 		if (h->raid_offload_debug > 0)
28762ba8bfc8SStephen M. Cameron 			dev_info(&h->pdev->dev,
28772ba8bfc8SStephen M. Cameron 				"%s: Searched h=0x%08x, Found h=0x%08x, scsiaddr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
28782ba8bfc8SStephen M. Cameron 				__func__, find,
28792ba8bfc8SStephen M. Cameron 				((struct ReportExtendedLUNdata *)
28802ba8bfc8SStephen M. Cameron 					physicals)->LUN[i][20],
28812ba8bfc8SStephen M. Cameron 				scsi3addr[0], scsi3addr[1], scsi3addr[2],
28822ba8bfc8SStephen M. Cameron 				scsi3addr[3], scsi3addr[4], scsi3addr[5],
28832ba8bfc8SStephen M. Cameron 				scsi3addr[6], scsi3addr[7]);
288454b6e9e9SScott Teel 		break; /* found it */
288554b6e9e9SScott Teel 	}
288654b6e9e9SScott Teel 
288754b6e9e9SScott Teel 	kfree(physicals);
288854b6e9e9SScott Teel 	if (found)
288954b6e9e9SScott Teel 		return 1;
289054b6e9e9SScott Teel 	else
289154b6e9e9SScott Teel 		return 0;
289254b6e9e9SScott Teel 
289354b6e9e9SScott Teel }
289454b6e9e9SScott Teel /*
2895edd16368SStephen M. Cameron  * Do CISS_REPORT_PHYS and CISS_REPORT_LOG.  Data is returned in physdev,
2896edd16368SStephen M. Cameron  * logdev.  The number of luns in physdev and logdev are returned in
2897edd16368SStephen M. Cameron  * *nphysicals and *nlogicals, respectively.
2898edd16368SStephen M. Cameron  * Returns 0 on success, -1 otherwise.
2899edd16368SStephen M. Cameron  */
2900edd16368SStephen M. Cameron static int hpsa_gather_lun_info(struct ctlr_info *h,
2901edd16368SStephen M. Cameron 	int reportlunsize,
2902283b4a9bSStephen M. Cameron 	struct ReportLUNdata *physdev, u32 *nphysicals, int *physical_mode,
290301a02ffcSStephen M. Cameron 	struct ReportLUNdata *logdev, u32 *nlogicals)
2904edd16368SStephen M. Cameron {
2905283b4a9bSStephen M. Cameron 	int physical_entry_size = 8;
2906283b4a9bSStephen M. Cameron 
2907283b4a9bSStephen M. Cameron 	*physical_mode = 0;
2908283b4a9bSStephen M. Cameron 
2909283b4a9bSStephen M. Cameron 	/* For I/O accelerator mode we need to read physical device handles */
2910317d4adfSMike MIller 	if (h->transMethod & CFGTBL_Trans_io_accel1 ||
2911317d4adfSMike MIller 		h->transMethod & CFGTBL_Trans_io_accel2) {
2912283b4a9bSStephen M. Cameron 		*physical_mode = HPSA_REPORT_PHYS_EXTENDED;
2913283b4a9bSStephen M. Cameron 		physical_entry_size = 24;
2914283b4a9bSStephen M. Cameron 	}
2915a93aa1feSMatt Gates 	if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize,
2916283b4a9bSStephen M. Cameron 							*physical_mode)) {
2917edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
2918edd16368SStephen M. Cameron 		return -1;
2919edd16368SStephen M. Cameron 	}
2920283b4a9bSStephen M. Cameron 	*nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) /
2921283b4a9bSStephen M. Cameron 							physical_entry_size;
2922edd16368SStephen M. Cameron 	if (*nphysicals > HPSA_MAX_PHYS_LUN) {
2923edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
2924edd16368SStephen M. Cameron 			"  %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2925edd16368SStephen M. Cameron 			*nphysicals - HPSA_MAX_PHYS_LUN);
2926edd16368SStephen M. Cameron 		*nphysicals = HPSA_MAX_PHYS_LUN;
2927edd16368SStephen M. Cameron 	}
2928edd16368SStephen M. Cameron 	if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
2929edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
2930edd16368SStephen M. Cameron 		return -1;
2931edd16368SStephen M. Cameron 	}
29326df1e954SStephen M. Cameron 	*nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
2933edd16368SStephen M. Cameron 	/* Reject Logicals in excess of our max capability. */
2934edd16368SStephen M. Cameron 	if (*nlogicals > HPSA_MAX_LUN) {
2935edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
2936edd16368SStephen M. Cameron 			"maximum logical LUNs (%d) exceeded.  "
2937edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_LUN,
2938edd16368SStephen M. Cameron 			*nlogicals - HPSA_MAX_LUN);
2939edd16368SStephen M. Cameron 			*nlogicals = HPSA_MAX_LUN;
2940edd16368SStephen M. Cameron 	}
2941edd16368SStephen M. Cameron 	if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
2942edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
2943edd16368SStephen M. Cameron 			"maximum logical + physical LUNs (%d) exceeded. "
2944edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2945edd16368SStephen M. Cameron 			*nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
2946edd16368SStephen M. Cameron 		*nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
2947edd16368SStephen M. Cameron 	}
2948edd16368SStephen M. Cameron 	return 0;
2949edd16368SStephen M. Cameron }
2950edd16368SStephen M. Cameron 
2951339b2b14SStephen M. Cameron u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i,
2952a93aa1feSMatt Gates 	int nphysicals, int nlogicals,
2953a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list,
2954339b2b14SStephen M. Cameron 	struct ReportLUNdata *logdev_list)
2955339b2b14SStephen M. Cameron {
2956339b2b14SStephen M. Cameron 	/* Helper function, figure out where the LUN ID info is coming from
2957339b2b14SStephen M. Cameron 	 * given index i, lists of physical and logical devices, where in
2958339b2b14SStephen M. Cameron 	 * the list the raid controller is supposed to appear (first or last)
2959339b2b14SStephen M. Cameron 	 */
2960339b2b14SStephen M. Cameron 
2961339b2b14SStephen M. Cameron 	int logicals_start = nphysicals + (raid_ctlr_position == 0);
2962339b2b14SStephen M. Cameron 	int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
2963339b2b14SStephen M. Cameron 
2964339b2b14SStephen M. Cameron 	if (i == raid_ctlr_position)
2965339b2b14SStephen M. Cameron 		return RAID_CTLR_LUNID;
2966339b2b14SStephen M. Cameron 
2967339b2b14SStephen M. Cameron 	if (i < logicals_start)
2968339b2b14SStephen M. Cameron 		return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0];
2969339b2b14SStephen M. Cameron 
2970339b2b14SStephen M. Cameron 	if (i < last_device)
2971339b2b14SStephen M. Cameron 		return &logdev_list->LUN[i - nphysicals -
2972339b2b14SStephen M. Cameron 			(raid_ctlr_position == 0)][0];
2973339b2b14SStephen M. Cameron 	BUG();
2974339b2b14SStephen M. Cameron 	return NULL;
2975339b2b14SStephen M. Cameron }
2976339b2b14SStephen M. Cameron 
2977316b221aSStephen M. Cameron static int hpsa_hba_mode_enabled(struct ctlr_info *h)
2978316b221aSStephen M. Cameron {
2979316b221aSStephen M. Cameron 	int rc;
29806e8e8088SJoe Handzik 	int hba_mode_enabled;
2981316b221aSStephen M. Cameron 	struct bmic_controller_parameters *ctlr_params;
2982316b221aSStephen M. Cameron 	ctlr_params = kzalloc(sizeof(struct bmic_controller_parameters),
2983316b221aSStephen M. Cameron 		GFP_KERNEL);
2984316b221aSStephen M. Cameron 
2985316b221aSStephen M. Cameron 	if (!ctlr_params)
298696444fbbSJoe Handzik 		return -ENOMEM;
2987316b221aSStephen M. Cameron 	rc = hpsa_bmic_ctrl_mode_sense(h, RAID_CTLR_LUNID, 0, ctlr_params,
2988316b221aSStephen M. Cameron 		sizeof(struct bmic_controller_parameters));
298996444fbbSJoe Handzik 	if (rc) {
2990316b221aSStephen M. Cameron 		kfree(ctlr_params);
299196444fbbSJoe Handzik 		return rc;
2992316b221aSStephen M. Cameron 	}
29936e8e8088SJoe Handzik 
29946e8e8088SJoe Handzik 	hba_mode_enabled =
29956e8e8088SJoe Handzik 		((ctlr_params->nvram_flags & HBA_MODE_ENABLED_FLAG) != 0);
29966e8e8088SJoe Handzik 	kfree(ctlr_params);
29976e8e8088SJoe Handzik 	return hba_mode_enabled;
2998316b221aSStephen M. Cameron }
2999316b221aSStephen M. Cameron 
3000edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
3001edd16368SStephen M. Cameron {
3002edd16368SStephen M. Cameron 	/* the idea here is we could get notified
3003edd16368SStephen M. Cameron 	 * that some devices have changed, so we do a report
3004edd16368SStephen M. Cameron 	 * physical luns and report logical luns cmd, and adjust
3005edd16368SStephen M. Cameron 	 * our list of devices accordingly.
3006edd16368SStephen M. Cameron 	 *
3007edd16368SStephen M. Cameron 	 * The scsi3addr's of devices won't change so long as the
3008edd16368SStephen M. Cameron 	 * adapter is not reset.  That means we can rescan and
3009edd16368SStephen M. Cameron 	 * tell which devices we already know about, vs. new
3010edd16368SStephen M. Cameron 	 * devices, vs.  disappearing devices.
3011edd16368SStephen M. Cameron 	 */
3012a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list = NULL;
3013edd16368SStephen M. Cameron 	struct ReportLUNdata *logdev_list = NULL;
301401a02ffcSStephen M. Cameron 	u32 nphysicals = 0;
301501a02ffcSStephen M. Cameron 	u32 nlogicals = 0;
3016283b4a9bSStephen M. Cameron 	int physical_mode = 0;
301701a02ffcSStephen M. Cameron 	u32 ndev_allocated = 0;
3018edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
3019edd16368SStephen M. Cameron 	int ncurrent = 0;
3020283b4a9bSStephen M. Cameron 	int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 24;
30214f4eb9f1SScott Teel 	int i, n_ext_target_devs, ndevs_to_allocate;
3022339b2b14SStephen M. Cameron 	int raid_ctlr_position;
30232bbf5c7fSJoe Handzik 	int rescan_hba_mode;
3024aca4a520SScott Teel 	DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
3025edd16368SStephen M. Cameron 
3026cfe5badcSScott Teel 	currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
3027edd16368SStephen M. Cameron 	physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
3028edd16368SStephen M. Cameron 	logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
3029edd16368SStephen M. Cameron 	tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
3030edd16368SStephen M. Cameron 
30310b0e1d6cSStephen M. Cameron 	if (!currentsd || !physdev_list || !logdev_list || !tmpdevice) {
3032edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory\n");
3033edd16368SStephen M. Cameron 		goto out;
3034edd16368SStephen M. Cameron 	}
3035edd16368SStephen M. Cameron 	memset(lunzerobits, 0, sizeof(lunzerobits));
3036edd16368SStephen M. Cameron 
3037316b221aSStephen M. Cameron 	rescan_hba_mode = hpsa_hba_mode_enabled(h);
303896444fbbSJoe Handzik 	if (rescan_hba_mode < 0)
303996444fbbSJoe Handzik 		goto out;
3040316b221aSStephen M. Cameron 
3041316b221aSStephen M. Cameron 	if (!h->hba_mode_enabled && rescan_hba_mode)
3042316b221aSStephen M. Cameron 		dev_warn(&h->pdev->dev, "HBA mode enabled\n");
3043316b221aSStephen M. Cameron 	else if (h->hba_mode_enabled && !rescan_hba_mode)
3044316b221aSStephen M. Cameron 		dev_warn(&h->pdev->dev, "HBA mode disabled\n");
3045316b221aSStephen M. Cameron 
3046316b221aSStephen M. Cameron 	h->hba_mode_enabled = rescan_hba_mode;
3047316b221aSStephen M. Cameron 
3048a93aa1feSMatt Gates 	if (hpsa_gather_lun_info(h, reportlunsize,
3049a93aa1feSMatt Gates 			(struct ReportLUNdata *) physdev_list, &nphysicals,
3050283b4a9bSStephen M. Cameron 			&physical_mode, logdev_list, &nlogicals))
3051edd16368SStephen M. Cameron 		goto out;
3052edd16368SStephen M. Cameron 
3053aca4a520SScott Teel 	/* We might see up to the maximum number of logical and physical disks
3054aca4a520SScott Teel 	 * plus external target devices, and a device for the local RAID
3055aca4a520SScott Teel 	 * controller.
3056edd16368SStephen M. Cameron 	 */
3057aca4a520SScott Teel 	ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
3058edd16368SStephen M. Cameron 
3059edd16368SStephen M. Cameron 	/* Allocate the per device structures */
3060edd16368SStephen M. Cameron 	for (i = 0; i < ndevs_to_allocate; i++) {
3061b7ec021fSScott Teel 		if (i >= HPSA_MAX_DEVICES) {
3062b7ec021fSScott Teel 			dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
3063b7ec021fSScott Teel 				"  %d devices ignored.\n", HPSA_MAX_DEVICES,
3064b7ec021fSScott Teel 				ndevs_to_allocate - HPSA_MAX_DEVICES);
3065b7ec021fSScott Teel 			break;
3066b7ec021fSScott Teel 		}
3067b7ec021fSScott Teel 
3068edd16368SStephen M. Cameron 		currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
3069edd16368SStephen M. Cameron 		if (!currentsd[i]) {
3070edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
3071edd16368SStephen M. Cameron 				__FILE__, __LINE__);
3072edd16368SStephen M. Cameron 			goto out;
3073edd16368SStephen M. Cameron 		}
3074edd16368SStephen M. Cameron 		ndev_allocated++;
3075edd16368SStephen M. Cameron 	}
3076edd16368SStephen M. Cameron 
3077339b2b14SStephen M. Cameron 	if (unlikely(is_scsi_rev_5(h)))
3078339b2b14SStephen M. Cameron 		raid_ctlr_position = 0;
3079339b2b14SStephen M. Cameron 	else
3080339b2b14SStephen M. Cameron 		raid_ctlr_position = nphysicals + nlogicals;
3081339b2b14SStephen M. Cameron 
3082edd16368SStephen M. Cameron 	/* adjust our table of devices */
30834f4eb9f1SScott Teel 	n_ext_target_devs = 0;
3084edd16368SStephen M. Cameron 	for (i = 0; i < nphysicals + nlogicals + 1; i++) {
30850b0e1d6cSStephen M. Cameron 		u8 *lunaddrbytes, is_OBDR = 0;
3086edd16368SStephen M. Cameron 
3087edd16368SStephen M. Cameron 		/* Figure out where the LUN ID info is coming from */
3088339b2b14SStephen M. Cameron 		lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
3089339b2b14SStephen M. Cameron 			i, nphysicals, nlogicals, physdev_list, logdev_list);
3090edd16368SStephen M. Cameron 		/* skip masked physical devices. */
3091339b2b14SStephen M. Cameron 		if (lunaddrbytes[3] & 0xC0 &&
3092339b2b14SStephen M. Cameron 			i < nphysicals + (raid_ctlr_position == 0))
3093edd16368SStephen M. Cameron 			continue;
3094edd16368SStephen M. Cameron 
3095edd16368SStephen M. Cameron 		/* Get device type, vendor, model, device id */
30960b0e1d6cSStephen M. Cameron 		if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
30970b0e1d6cSStephen M. Cameron 							&is_OBDR))
3098edd16368SStephen M. Cameron 			continue; /* skip it if we can't talk to it. */
30991f310bdeSStephen M. Cameron 		figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
3100edd16368SStephen M. Cameron 		this_device = currentsd[ncurrent];
3101edd16368SStephen M. Cameron 
3102edd16368SStephen M. Cameron 		/*
31034f4eb9f1SScott Teel 		 * For external target devices, we have to insert a LUN 0 which
3104edd16368SStephen M. Cameron 		 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
3105edd16368SStephen M. Cameron 		 * is nonetheless an enclosure device there.  We have to
3106edd16368SStephen M. Cameron 		 * present that otherwise linux won't find anything if
3107edd16368SStephen M. Cameron 		 * there is no lun 0.
3108edd16368SStephen M. Cameron 		 */
31094f4eb9f1SScott Teel 		if (add_ext_target_dev(h, tmpdevice, this_device,
31101f310bdeSStephen M. Cameron 				lunaddrbytes, lunzerobits,
31114f4eb9f1SScott Teel 				&n_ext_target_devs)) {
3112edd16368SStephen M. Cameron 			ncurrent++;
3113edd16368SStephen M. Cameron 			this_device = currentsd[ncurrent];
3114edd16368SStephen M. Cameron 		}
3115edd16368SStephen M. Cameron 
3116edd16368SStephen M. Cameron 		*this_device = *tmpdevice;
3117edd16368SStephen M. Cameron 
3118edd16368SStephen M. Cameron 		switch (this_device->devtype) {
31190b0e1d6cSStephen M. Cameron 		case TYPE_ROM:
3120edd16368SStephen M. Cameron 			/* We don't *really* support actual CD-ROM devices,
3121edd16368SStephen M. Cameron 			 * just "One Button Disaster Recovery" tape drive
3122edd16368SStephen M. Cameron 			 * which temporarily pretends to be a CD-ROM drive.
3123edd16368SStephen M. Cameron 			 * So we check that the device is really an OBDR tape
3124edd16368SStephen M. Cameron 			 * device by checking for "$DR-10" in bytes 43-48 of
3125edd16368SStephen M. Cameron 			 * the inquiry data.
3126edd16368SStephen M. Cameron 			 */
31270b0e1d6cSStephen M. Cameron 			if (is_OBDR)
3128edd16368SStephen M. Cameron 				ncurrent++;
3129edd16368SStephen M. Cameron 			break;
3130edd16368SStephen M. Cameron 		case TYPE_DISK:
3131316b221aSStephen M. Cameron 			if (h->hba_mode_enabled) {
3132316b221aSStephen M. Cameron 				/* never use raid mapper in HBA mode */
3133316b221aSStephen M. Cameron 				this_device->offload_enabled = 0;
3134316b221aSStephen M. Cameron 				ncurrent++;
3135316b221aSStephen M. Cameron 				break;
3136316b221aSStephen M. Cameron 			} else if (h->acciopath_status) {
3137283b4a9bSStephen M. Cameron 				if (i >= nphysicals) {
3138283b4a9bSStephen M. Cameron 					ncurrent++;
3139edd16368SStephen M. Cameron 					break;
3140283b4a9bSStephen M. Cameron 				}
3141316b221aSStephen M. Cameron 			} else {
3142316b221aSStephen M. Cameron 				if (i < nphysicals)
3143316b221aSStephen M. Cameron 					break;
3144316b221aSStephen M. Cameron 				ncurrent++;
3145316b221aSStephen M. Cameron 				break;
3146316b221aSStephen M. Cameron 			}
3147283b4a9bSStephen M. Cameron 			if (physical_mode == HPSA_REPORT_PHYS_EXTENDED) {
3148e1f7de0cSMatt Gates 				memcpy(&this_device->ioaccel_handle,
3149e1f7de0cSMatt Gates 					&lunaddrbytes[20],
3150e1f7de0cSMatt Gates 					sizeof(this_device->ioaccel_handle));
3151edd16368SStephen M. Cameron 				ncurrent++;
3152283b4a9bSStephen M. Cameron 			}
3153edd16368SStephen M. Cameron 			break;
3154edd16368SStephen M. Cameron 		case TYPE_TAPE:
3155edd16368SStephen M. Cameron 		case TYPE_MEDIUM_CHANGER:
3156edd16368SStephen M. Cameron 			ncurrent++;
3157edd16368SStephen M. Cameron 			break;
3158edd16368SStephen M. Cameron 		case TYPE_RAID:
3159edd16368SStephen M. Cameron 			/* Only present the Smartarray HBA as a RAID controller.
3160edd16368SStephen M. Cameron 			 * If it's a RAID controller other than the HBA itself
3161edd16368SStephen M. Cameron 			 * (an external RAID controller, MSA500 or similar)
3162edd16368SStephen M. Cameron 			 * don't present it.
3163edd16368SStephen M. Cameron 			 */
3164edd16368SStephen M. Cameron 			if (!is_hba_lunid(lunaddrbytes))
3165edd16368SStephen M. Cameron 				break;
3166edd16368SStephen M. Cameron 			ncurrent++;
3167edd16368SStephen M. Cameron 			break;
3168edd16368SStephen M. Cameron 		default:
3169edd16368SStephen M. Cameron 			break;
3170edd16368SStephen M. Cameron 		}
3171cfe5badcSScott Teel 		if (ncurrent >= HPSA_MAX_DEVICES)
3172edd16368SStephen M. Cameron 			break;
3173edd16368SStephen M. Cameron 	}
3174edd16368SStephen M. Cameron 	adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
3175edd16368SStephen M. Cameron out:
3176edd16368SStephen M. Cameron 	kfree(tmpdevice);
3177edd16368SStephen M. Cameron 	for (i = 0; i < ndev_allocated; i++)
3178edd16368SStephen M. Cameron 		kfree(currentsd[i]);
3179edd16368SStephen M. Cameron 	kfree(currentsd);
3180edd16368SStephen M. Cameron 	kfree(physdev_list);
3181edd16368SStephen M. Cameron 	kfree(logdev_list);
3182edd16368SStephen M. Cameron }
3183edd16368SStephen M. Cameron 
3184edd16368SStephen M. Cameron /* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
3185edd16368SStephen M. Cameron  * dma mapping  and fills in the scatter gather entries of the
3186edd16368SStephen M. Cameron  * hpsa command, cp.
3187edd16368SStephen M. Cameron  */
318833a2ffceSStephen M. Cameron static int hpsa_scatter_gather(struct ctlr_info *h,
3189edd16368SStephen M. Cameron 		struct CommandList *cp,
3190edd16368SStephen M. Cameron 		struct scsi_cmnd *cmd)
3191edd16368SStephen M. Cameron {
3192edd16368SStephen M. Cameron 	unsigned int len;
3193edd16368SStephen M. Cameron 	struct scatterlist *sg;
319401a02ffcSStephen M. Cameron 	u64 addr64;
319533a2ffceSStephen M. Cameron 	int use_sg, i, sg_index, chained;
319633a2ffceSStephen M. Cameron 	struct SGDescriptor *curr_sg;
3197edd16368SStephen M. Cameron 
319833a2ffceSStephen M. Cameron 	BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
3199edd16368SStephen M. Cameron 
3200edd16368SStephen M. Cameron 	use_sg = scsi_dma_map(cmd);
3201edd16368SStephen M. Cameron 	if (use_sg < 0)
3202edd16368SStephen M. Cameron 		return use_sg;
3203edd16368SStephen M. Cameron 
3204edd16368SStephen M. Cameron 	if (!use_sg)
3205edd16368SStephen M. Cameron 		goto sglist_finished;
3206edd16368SStephen M. Cameron 
320733a2ffceSStephen M. Cameron 	curr_sg = cp->SG;
320833a2ffceSStephen M. Cameron 	chained = 0;
320933a2ffceSStephen M. Cameron 	sg_index = 0;
3210edd16368SStephen M. Cameron 	scsi_for_each_sg(cmd, sg, use_sg, i) {
321133a2ffceSStephen M. Cameron 		if (i == h->max_cmd_sg_entries - 1 &&
321233a2ffceSStephen M. Cameron 			use_sg > h->max_cmd_sg_entries) {
321333a2ffceSStephen M. Cameron 			chained = 1;
321433a2ffceSStephen M. Cameron 			curr_sg = h->cmd_sg_list[cp->cmdindex];
321533a2ffceSStephen M. Cameron 			sg_index = 0;
321633a2ffceSStephen M. Cameron 		}
321701a02ffcSStephen M. Cameron 		addr64 = (u64) sg_dma_address(sg);
3218edd16368SStephen M. Cameron 		len  = sg_dma_len(sg);
321933a2ffceSStephen M. Cameron 		curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
322033a2ffceSStephen M. Cameron 		curr_sg->Addr.upper = (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
322133a2ffceSStephen M. Cameron 		curr_sg->Len = len;
3222e1d9cbfaSMatt Gates 		curr_sg->Ext = (i < scsi_sg_count(cmd) - 1) ? 0 : HPSA_SG_LAST;
322333a2ffceSStephen M. Cameron 		curr_sg++;
322433a2ffceSStephen M. Cameron 	}
322533a2ffceSStephen M. Cameron 
322633a2ffceSStephen M. Cameron 	if (use_sg + chained > h->maxSG)
322733a2ffceSStephen M. Cameron 		h->maxSG = use_sg + chained;
322833a2ffceSStephen M. Cameron 
322933a2ffceSStephen M. Cameron 	if (chained) {
323033a2ffceSStephen M. Cameron 		cp->Header.SGList = h->max_cmd_sg_entries;
323133a2ffceSStephen M. Cameron 		cp->Header.SGTotal = (u16) (use_sg + 1);
3232e2bea6dfSStephen M. Cameron 		if (hpsa_map_sg_chain_block(h, cp)) {
3233e2bea6dfSStephen M. Cameron 			scsi_dma_unmap(cmd);
3234e2bea6dfSStephen M. Cameron 			return -1;
3235e2bea6dfSStephen M. Cameron 		}
323633a2ffceSStephen M. Cameron 		return 0;
3237edd16368SStephen M. Cameron 	}
3238edd16368SStephen M. Cameron 
3239edd16368SStephen M. Cameron sglist_finished:
3240edd16368SStephen M. Cameron 
324101a02ffcSStephen M. Cameron 	cp->Header.SGList = (u8) use_sg;   /* no. SGs contig in this cmd */
324201a02ffcSStephen M. Cameron 	cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */
3243edd16368SStephen M. Cameron 	return 0;
3244edd16368SStephen M. Cameron }
3245edd16368SStephen M. Cameron 
3246283b4a9bSStephen M. Cameron #define IO_ACCEL_INELIGIBLE (1)
3247283b4a9bSStephen M. Cameron static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
3248283b4a9bSStephen M. Cameron {
3249283b4a9bSStephen M. Cameron 	int is_write = 0;
3250283b4a9bSStephen M. Cameron 	u32 block;
3251283b4a9bSStephen M. Cameron 	u32 block_cnt;
3252283b4a9bSStephen M. Cameron 
3253283b4a9bSStephen M. Cameron 	/* Perform some CDB fixups if needed using 10 byte reads/writes only */
3254283b4a9bSStephen M. Cameron 	switch (cdb[0]) {
3255283b4a9bSStephen M. Cameron 	case WRITE_6:
3256283b4a9bSStephen M. Cameron 	case WRITE_12:
3257283b4a9bSStephen M. Cameron 		is_write = 1;
3258283b4a9bSStephen M. Cameron 	case READ_6:
3259283b4a9bSStephen M. Cameron 	case READ_12:
3260283b4a9bSStephen M. Cameron 		if (*cdb_len == 6) {
3261283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 8) | cdb[3];
3262283b4a9bSStephen M. Cameron 			block_cnt = cdb[4];
3263283b4a9bSStephen M. Cameron 		} else {
3264283b4a9bSStephen M. Cameron 			BUG_ON(*cdb_len != 12);
3265283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 24) |
3266283b4a9bSStephen M. Cameron 				(((u32) cdb[3]) << 16) |
3267283b4a9bSStephen M. Cameron 				(((u32) cdb[4]) << 8) |
3268283b4a9bSStephen M. Cameron 				cdb[5];
3269283b4a9bSStephen M. Cameron 			block_cnt =
3270283b4a9bSStephen M. Cameron 				(((u32) cdb[6]) << 24) |
3271283b4a9bSStephen M. Cameron 				(((u32) cdb[7]) << 16) |
3272283b4a9bSStephen M. Cameron 				(((u32) cdb[8]) << 8) |
3273283b4a9bSStephen M. Cameron 				cdb[9];
3274283b4a9bSStephen M. Cameron 		}
3275283b4a9bSStephen M. Cameron 		if (block_cnt > 0xffff)
3276283b4a9bSStephen M. Cameron 			return IO_ACCEL_INELIGIBLE;
3277283b4a9bSStephen M. Cameron 
3278283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
3279283b4a9bSStephen M. Cameron 		cdb[1] = 0;
3280283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (block >> 24);
3281283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (block >> 16);
3282283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (block >> 8);
3283283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (block);
3284283b4a9bSStephen M. Cameron 		cdb[6] = 0;
3285283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (block_cnt >> 8);
3286283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (block_cnt);
3287283b4a9bSStephen M. Cameron 		cdb[9] = 0;
3288283b4a9bSStephen M. Cameron 		*cdb_len = 10;
3289283b4a9bSStephen M. Cameron 		break;
3290283b4a9bSStephen M. Cameron 	}
3291283b4a9bSStephen M. Cameron 	return 0;
3292283b4a9bSStephen M. Cameron }
3293283b4a9bSStephen M. Cameron 
3294c349775eSScott Teel static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
3295283b4a9bSStephen M. Cameron 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
3296283b4a9bSStephen M. Cameron 	u8 *scsi3addr)
3297e1f7de0cSMatt Gates {
3298e1f7de0cSMatt Gates 	struct scsi_cmnd *cmd = c->scsi_cmd;
3299e1f7de0cSMatt Gates 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
3300e1f7de0cSMatt Gates 	unsigned int len;
3301e1f7de0cSMatt Gates 	unsigned int total_len = 0;
3302e1f7de0cSMatt Gates 	struct scatterlist *sg;
3303e1f7de0cSMatt Gates 	u64 addr64;
3304e1f7de0cSMatt Gates 	int use_sg, i;
3305e1f7de0cSMatt Gates 	struct SGDescriptor *curr_sg;
3306e1f7de0cSMatt Gates 	u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
3307e1f7de0cSMatt Gates 
3308283b4a9bSStephen M. Cameron 	/* TODO: implement chaining support */
3309283b4a9bSStephen M. Cameron 	if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
3310283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3311283b4a9bSStephen M. Cameron 
3312e1f7de0cSMatt Gates 	BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
3313e1f7de0cSMatt Gates 
3314283b4a9bSStephen M. Cameron 	if (fixup_ioaccel_cdb(cdb, &cdb_len))
3315283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3316283b4a9bSStephen M. Cameron 
3317e1f7de0cSMatt Gates 	c->cmd_type = CMD_IOACCEL1;
3318e1f7de0cSMatt Gates 
3319e1f7de0cSMatt Gates 	/* Adjust the DMA address to point to the accelerated command buffer */
3320e1f7de0cSMatt Gates 	c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
3321e1f7de0cSMatt Gates 				(c->cmdindex * sizeof(*cp));
3322e1f7de0cSMatt Gates 	BUG_ON(c->busaddr & 0x0000007F);
3323e1f7de0cSMatt Gates 
3324e1f7de0cSMatt Gates 	use_sg = scsi_dma_map(cmd);
3325e1f7de0cSMatt Gates 	if (use_sg < 0)
3326e1f7de0cSMatt Gates 		return use_sg;
3327e1f7de0cSMatt Gates 
3328e1f7de0cSMatt Gates 	if (use_sg) {
3329e1f7de0cSMatt Gates 		curr_sg = cp->SG;
3330e1f7de0cSMatt Gates 		scsi_for_each_sg(cmd, sg, use_sg, i) {
3331e1f7de0cSMatt Gates 			addr64 = (u64) sg_dma_address(sg);
3332e1f7de0cSMatt Gates 			len  = sg_dma_len(sg);
3333e1f7de0cSMatt Gates 			total_len += len;
3334e1f7de0cSMatt Gates 			curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
3335e1f7de0cSMatt Gates 			curr_sg->Addr.upper =
3336e1f7de0cSMatt Gates 				(u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
3337e1f7de0cSMatt Gates 			curr_sg->Len = len;
3338e1f7de0cSMatt Gates 
3339e1f7de0cSMatt Gates 			if (i == (scsi_sg_count(cmd) - 1))
3340e1f7de0cSMatt Gates 				curr_sg->Ext = HPSA_SG_LAST;
3341e1f7de0cSMatt Gates 			else
3342e1f7de0cSMatt Gates 				curr_sg->Ext = 0;  /* we are not chaining */
3343e1f7de0cSMatt Gates 			curr_sg++;
3344e1f7de0cSMatt Gates 		}
3345e1f7de0cSMatt Gates 
3346e1f7de0cSMatt Gates 		switch (cmd->sc_data_direction) {
3347e1f7de0cSMatt Gates 		case DMA_TO_DEVICE:
3348e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_OUT;
3349e1f7de0cSMatt Gates 			break;
3350e1f7de0cSMatt Gates 		case DMA_FROM_DEVICE:
3351e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_IN;
3352e1f7de0cSMatt Gates 			break;
3353e1f7de0cSMatt Gates 		case DMA_NONE:
3354e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_NODATAXFER;
3355e1f7de0cSMatt Gates 			break;
3356e1f7de0cSMatt Gates 		default:
3357e1f7de0cSMatt Gates 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3358e1f7de0cSMatt Gates 			cmd->sc_data_direction);
3359e1f7de0cSMatt Gates 			BUG();
3360e1f7de0cSMatt Gates 			break;
3361e1f7de0cSMatt Gates 		}
3362e1f7de0cSMatt Gates 	} else {
3363e1f7de0cSMatt Gates 		control |= IOACCEL1_CONTROL_NODATAXFER;
3364e1f7de0cSMatt Gates 	}
3365e1f7de0cSMatt Gates 
3366c349775eSScott Teel 	c->Header.SGList = use_sg;
3367e1f7de0cSMatt Gates 	/* Fill out the command structure to submit */
3368283b4a9bSStephen M. Cameron 	cp->dev_handle = ioaccel_handle & 0xFFFF;
3369e1f7de0cSMatt Gates 	cp->transfer_len = total_len;
3370e1f7de0cSMatt Gates 	cp->io_flags = IOACCEL1_IOFLAGS_IO_REQ |
3371283b4a9bSStephen M. Cameron 			(cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK);
3372e1f7de0cSMatt Gates 	cp->control = control;
3373283b4a9bSStephen M. Cameron 	memcpy(cp->CDB, cdb, cdb_len);
3374283b4a9bSStephen M. Cameron 	memcpy(cp->CISS_LUN, scsi3addr, 8);
3375c349775eSScott Teel 	/* Tag was already set at init time. */
3376e1f7de0cSMatt Gates 	enqueue_cmd_and_start_io(h, c);
3377e1f7de0cSMatt Gates 	return 0;
3378e1f7de0cSMatt Gates }
3379edd16368SStephen M. Cameron 
3380283b4a9bSStephen M. Cameron /*
3381283b4a9bSStephen M. Cameron  * Queue a command directly to a device behind the controller using the
3382283b4a9bSStephen M. Cameron  * I/O accelerator path.
3383283b4a9bSStephen M. Cameron  */
3384283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
3385283b4a9bSStephen M. Cameron 	struct CommandList *c)
3386283b4a9bSStephen M. Cameron {
3387283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
3388283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3389283b4a9bSStephen M. Cameron 
3390283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
3391283b4a9bSStephen M. Cameron 		cmd->cmnd, cmd->cmd_len, dev->scsi3addr);
3392283b4a9bSStephen M. Cameron }
3393283b4a9bSStephen M. Cameron 
3394dd0e19f3SScott Teel /*
3395dd0e19f3SScott Teel  * Set encryption parameters for the ioaccel2 request
3396dd0e19f3SScott Teel  */
3397dd0e19f3SScott Teel static void set_encrypt_ioaccel2(struct ctlr_info *h,
3398dd0e19f3SScott Teel 	struct CommandList *c, struct io_accel2_cmd *cp)
3399dd0e19f3SScott Teel {
3400dd0e19f3SScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
3401dd0e19f3SScott Teel 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3402dd0e19f3SScott Teel 	struct raid_map_data *map = &dev->raid_map;
3403dd0e19f3SScott Teel 	u64 first_block;
3404dd0e19f3SScott Teel 
3405dd0e19f3SScott Teel 	BUG_ON(!(dev->offload_config && dev->offload_enabled));
3406dd0e19f3SScott Teel 
3407dd0e19f3SScott Teel 	/* Are we doing encryption on this device */
3408dd0e19f3SScott Teel 	if (!(map->flags & RAID_MAP_FLAG_ENCRYPT_ON))
3409dd0e19f3SScott Teel 		return;
3410dd0e19f3SScott Teel 	/* Set the data encryption key index. */
3411dd0e19f3SScott Teel 	cp->dekindex = map->dekindex;
3412dd0e19f3SScott Teel 
3413dd0e19f3SScott Teel 	/* Set the encryption enable flag, encoded into direction field. */
3414dd0e19f3SScott Teel 	cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
3415dd0e19f3SScott Teel 
3416dd0e19f3SScott Teel 	/* Set encryption tweak values based on logical block address
3417dd0e19f3SScott Teel 	 * If block size is 512, tweak value is LBA.
3418dd0e19f3SScott Teel 	 * For other block sizes, tweak is (LBA * block size)/ 512)
3419dd0e19f3SScott Teel 	 */
3420dd0e19f3SScott Teel 	switch (cmd->cmnd[0]) {
3421dd0e19f3SScott Teel 	/* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
3422dd0e19f3SScott Teel 	case WRITE_6:
3423dd0e19f3SScott Teel 	case READ_6:
3424dd0e19f3SScott Teel 		if (map->volume_blk_size == 512) {
3425dd0e19f3SScott Teel 			cp->tweak_lower =
3426dd0e19f3SScott Teel 				(((u32) cmd->cmnd[2]) << 8) |
3427dd0e19f3SScott Teel 					cmd->cmnd[3];
3428dd0e19f3SScott Teel 			cp->tweak_upper = 0;
3429dd0e19f3SScott Teel 		} else {
3430dd0e19f3SScott Teel 			first_block =
3431dd0e19f3SScott Teel 				(((u64) cmd->cmnd[2]) << 8) |
3432dd0e19f3SScott Teel 					cmd->cmnd[3];
3433dd0e19f3SScott Teel 			first_block = (first_block * map->volume_blk_size)/512;
3434dd0e19f3SScott Teel 			cp->tweak_lower = (u32)first_block;
3435dd0e19f3SScott Teel 			cp->tweak_upper = (u32)(first_block >> 32);
3436dd0e19f3SScott Teel 		}
3437dd0e19f3SScott Teel 		break;
3438dd0e19f3SScott Teel 	case WRITE_10:
3439dd0e19f3SScott Teel 	case READ_10:
3440dd0e19f3SScott Teel 		if (map->volume_blk_size == 512) {
3441dd0e19f3SScott Teel 			cp->tweak_lower =
3442dd0e19f3SScott Teel 				(((u32) cmd->cmnd[2]) << 24) |
3443dd0e19f3SScott Teel 				(((u32) cmd->cmnd[3]) << 16) |
3444dd0e19f3SScott Teel 				(((u32) cmd->cmnd[4]) << 8) |
3445dd0e19f3SScott Teel 					cmd->cmnd[5];
3446dd0e19f3SScott Teel 			cp->tweak_upper = 0;
3447dd0e19f3SScott Teel 		} else {
3448dd0e19f3SScott Teel 			first_block =
3449dd0e19f3SScott Teel 				(((u64) cmd->cmnd[2]) << 24) |
3450dd0e19f3SScott Teel 				(((u64) cmd->cmnd[3]) << 16) |
3451dd0e19f3SScott Teel 				(((u64) cmd->cmnd[4]) << 8) |
3452dd0e19f3SScott Teel 					cmd->cmnd[5];
3453dd0e19f3SScott Teel 			first_block = (first_block * map->volume_blk_size)/512;
3454dd0e19f3SScott Teel 			cp->tweak_lower = (u32)first_block;
3455dd0e19f3SScott Teel 			cp->tweak_upper = (u32)(first_block >> 32);
3456dd0e19f3SScott Teel 		}
3457dd0e19f3SScott Teel 		break;
3458dd0e19f3SScott Teel 	/* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
3459dd0e19f3SScott Teel 	case WRITE_12:
3460dd0e19f3SScott Teel 	case READ_12:
3461dd0e19f3SScott Teel 		if (map->volume_blk_size == 512) {
3462dd0e19f3SScott Teel 			cp->tweak_lower =
3463dd0e19f3SScott Teel 				(((u32) cmd->cmnd[2]) << 24) |
3464dd0e19f3SScott Teel 				(((u32) cmd->cmnd[3]) << 16) |
3465dd0e19f3SScott Teel 				(((u32) cmd->cmnd[4]) << 8) |
3466dd0e19f3SScott Teel 					cmd->cmnd[5];
3467dd0e19f3SScott Teel 			cp->tweak_upper = 0;
3468dd0e19f3SScott Teel 		} else {
3469dd0e19f3SScott Teel 			first_block =
3470dd0e19f3SScott Teel 				(((u64) cmd->cmnd[2]) << 24) |
3471dd0e19f3SScott Teel 				(((u64) cmd->cmnd[3]) << 16) |
3472dd0e19f3SScott Teel 				(((u64) cmd->cmnd[4]) << 8) |
3473dd0e19f3SScott Teel 					cmd->cmnd[5];
3474dd0e19f3SScott Teel 			first_block = (first_block * map->volume_blk_size)/512;
3475dd0e19f3SScott Teel 			cp->tweak_lower = (u32)first_block;
3476dd0e19f3SScott Teel 			cp->tweak_upper = (u32)(first_block >> 32);
3477dd0e19f3SScott Teel 		}
3478dd0e19f3SScott Teel 		break;
3479dd0e19f3SScott Teel 	case WRITE_16:
3480dd0e19f3SScott Teel 	case READ_16:
3481dd0e19f3SScott Teel 		if (map->volume_blk_size == 512) {
3482dd0e19f3SScott Teel 			cp->tweak_lower =
3483dd0e19f3SScott Teel 				(((u32) cmd->cmnd[6]) << 24) |
3484dd0e19f3SScott Teel 				(((u32) cmd->cmnd[7]) << 16) |
3485dd0e19f3SScott Teel 				(((u32) cmd->cmnd[8]) << 8) |
3486dd0e19f3SScott Teel 					cmd->cmnd[9];
3487dd0e19f3SScott Teel 			cp->tweak_upper =
3488dd0e19f3SScott Teel 				(((u32) cmd->cmnd[2]) << 24) |
3489dd0e19f3SScott Teel 				(((u32) cmd->cmnd[3]) << 16) |
3490dd0e19f3SScott Teel 				(((u32) cmd->cmnd[4]) << 8) |
3491dd0e19f3SScott Teel 					cmd->cmnd[5];
3492dd0e19f3SScott Teel 		} else {
3493dd0e19f3SScott Teel 			first_block =
3494dd0e19f3SScott Teel 				(((u64) cmd->cmnd[2]) << 56) |
3495dd0e19f3SScott Teel 				(((u64) cmd->cmnd[3]) << 48) |
3496dd0e19f3SScott Teel 				(((u64) cmd->cmnd[4]) << 40) |
3497dd0e19f3SScott Teel 				(((u64) cmd->cmnd[5]) << 32) |
3498dd0e19f3SScott Teel 				(((u64) cmd->cmnd[6]) << 24) |
3499dd0e19f3SScott Teel 				(((u64) cmd->cmnd[7]) << 16) |
3500dd0e19f3SScott Teel 				(((u64) cmd->cmnd[8]) << 8) |
3501dd0e19f3SScott Teel 					cmd->cmnd[9];
3502dd0e19f3SScott Teel 			first_block = (first_block * map->volume_blk_size)/512;
3503dd0e19f3SScott Teel 			cp->tweak_lower = (u32)first_block;
3504dd0e19f3SScott Teel 			cp->tweak_upper = (u32)(first_block >> 32);
3505dd0e19f3SScott Teel 		}
3506dd0e19f3SScott Teel 		break;
3507dd0e19f3SScott Teel 	default:
3508dd0e19f3SScott Teel 		dev_err(&h->pdev->dev,
3509dd0e19f3SScott Teel 			"ERROR: %s: IOACCEL request CDB size not supported for encryption\n",
3510dd0e19f3SScott Teel 			__func__);
3511dd0e19f3SScott Teel 		BUG();
3512dd0e19f3SScott Teel 		break;
3513dd0e19f3SScott Teel 	}
3514dd0e19f3SScott Teel }
3515dd0e19f3SScott Teel 
3516c349775eSScott Teel static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
3517c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
3518c349775eSScott Teel 	u8 *scsi3addr)
3519c349775eSScott Teel {
3520c349775eSScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
3521c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
3522c349775eSScott Teel 	struct ioaccel2_sg_element *curr_sg;
3523c349775eSScott Teel 	int use_sg, i;
3524c349775eSScott Teel 	struct scatterlist *sg;
3525c349775eSScott Teel 	u64 addr64;
3526c349775eSScott Teel 	u32 len;
3527c349775eSScott Teel 	u32 total_len = 0;
3528c349775eSScott Teel 
3529c349775eSScott Teel 	if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
3530c349775eSScott Teel 		return IO_ACCEL_INELIGIBLE;
3531c349775eSScott Teel 
3532c349775eSScott Teel 	if (fixup_ioaccel_cdb(cdb, &cdb_len))
3533c349775eSScott Teel 		return IO_ACCEL_INELIGIBLE;
3534c349775eSScott Teel 	c->cmd_type = CMD_IOACCEL2;
3535c349775eSScott Teel 	/* Adjust the DMA address to point to the accelerated command buffer */
3536c349775eSScott Teel 	c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
3537c349775eSScott Teel 				(c->cmdindex * sizeof(*cp));
3538c349775eSScott Teel 	BUG_ON(c->busaddr & 0x0000007F);
3539c349775eSScott Teel 
3540c349775eSScott Teel 	memset(cp, 0, sizeof(*cp));
3541c349775eSScott Teel 	cp->IU_type = IOACCEL2_IU_TYPE;
3542c349775eSScott Teel 
3543c349775eSScott Teel 	use_sg = scsi_dma_map(cmd);
3544c349775eSScott Teel 	if (use_sg < 0)
3545c349775eSScott Teel 		return use_sg;
3546c349775eSScott Teel 
3547c349775eSScott Teel 	if (use_sg) {
3548c349775eSScott Teel 		BUG_ON(use_sg > IOACCEL2_MAXSGENTRIES);
3549c349775eSScott Teel 		curr_sg = cp->sg;
3550c349775eSScott Teel 		scsi_for_each_sg(cmd, sg, use_sg, i) {
3551c349775eSScott Teel 			addr64 = (u64) sg_dma_address(sg);
3552c349775eSScott Teel 			len  = sg_dma_len(sg);
3553c349775eSScott Teel 			total_len += len;
3554c349775eSScott Teel 			curr_sg->address = cpu_to_le64(addr64);
3555c349775eSScott Teel 			curr_sg->length = cpu_to_le32(len);
3556c349775eSScott Teel 			curr_sg->reserved[0] = 0;
3557c349775eSScott Teel 			curr_sg->reserved[1] = 0;
3558c349775eSScott Teel 			curr_sg->reserved[2] = 0;
3559c349775eSScott Teel 			curr_sg->chain_indicator = 0;
3560c349775eSScott Teel 			curr_sg++;
3561c349775eSScott Teel 		}
3562c349775eSScott Teel 
3563c349775eSScott Teel 		switch (cmd->sc_data_direction) {
3564c349775eSScott Teel 		case DMA_TO_DEVICE:
3565dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3566dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_OUT;
3567c349775eSScott Teel 			break;
3568c349775eSScott Teel 		case DMA_FROM_DEVICE:
3569dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3570dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_IN;
3571c349775eSScott Teel 			break;
3572c349775eSScott Teel 		case DMA_NONE:
3573dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3574dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_NO_DATA;
3575c349775eSScott Teel 			break;
3576c349775eSScott Teel 		default:
3577c349775eSScott Teel 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3578c349775eSScott Teel 				cmd->sc_data_direction);
3579c349775eSScott Teel 			BUG();
3580c349775eSScott Teel 			break;
3581c349775eSScott Teel 		}
3582c349775eSScott Teel 	} else {
3583dd0e19f3SScott Teel 		cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3584dd0e19f3SScott Teel 		cp->direction |= IOACCEL2_DIR_NO_DATA;
3585c349775eSScott Teel 	}
3586dd0e19f3SScott Teel 
3587dd0e19f3SScott Teel 	/* Set encryption parameters, if necessary */
3588dd0e19f3SScott Teel 	set_encrypt_ioaccel2(h, c, cp);
3589dd0e19f3SScott Teel 
3590c349775eSScott Teel 	cp->scsi_nexus = ioaccel_handle;
3591dd0e19f3SScott Teel 	cp->Tag = (c->cmdindex << DIRECT_LOOKUP_SHIFT) |
3592c349775eSScott Teel 				DIRECT_LOOKUP_BIT;
3593c349775eSScott Teel 	memcpy(cp->cdb, cdb, sizeof(cp->cdb));
3594c349775eSScott Teel 
3595c349775eSScott Teel 	/* fill in sg elements */
3596c349775eSScott Teel 	cp->sg_count = (u8) use_sg;
3597c349775eSScott Teel 
3598c349775eSScott Teel 	cp->data_len = cpu_to_le32(total_len);
3599c349775eSScott Teel 	cp->err_ptr = cpu_to_le64(c->busaddr +
3600c349775eSScott Teel 			offsetof(struct io_accel2_cmd, error_data));
3601c349775eSScott Teel 	cp->err_len = cpu_to_le32((u32) sizeof(cp->error_data));
3602c349775eSScott Teel 
3603c349775eSScott Teel 	enqueue_cmd_and_start_io(h, c);
3604c349775eSScott Teel 	return 0;
3605c349775eSScott Teel }
3606c349775eSScott Teel 
3607c349775eSScott Teel /*
3608c349775eSScott Teel  * Queue a command to the correct I/O accelerator path.
3609c349775eSScott Teel  */
3610c349775eSScott Teel static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
3611c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
3612c349775eSScott Teel 	u8 *scsi3addr)
3613c349775eSScott Teel {
3614c349775eSScott Teel 	if (h->transMethod & CFGTBL_Trans_io_accel1)
3615c349775eSScott Teel 		return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
3616c349775eSScott Teel 						cdb, cdb_len, scsi3addr);
3617c349775eSScott Teel 	else
3618c349775eSScott Teel 		return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
3619c349775eSScott Teel 						cdb, cdb_len, scsi3addr);
3620c349775eSScott Teel }
3621c349775eSScott Teel 
36226b80b18fSScott Teel static void raid_map_helper(struct raid_map_data *map,
36236b80b18fSScott Teel 		int offload_to_mirror, u32 *map_index, u32 *current_group)
36246b80b18fSScott Teel {
36256b80b18fSScott Teel 	if (offload_to_mirror == 0)  {
36266b80b18fSScott Teel 		/* use physical disk in the first mirrored group. */
36276b80b18fSScott Teel 		*map_index %= map->data_disks_per_row;
36286b80b18fSScott Teel 		return;
36296b80b18fSScott Teel 	}
36306b80b18fSScott Teel 	do {
36316b80b18fSScott Teel 		/* determine mirror group that *map_index indicates */
36326b80b18fSScott Teel 		*current_group = *map_index / map->data_disks_per_row;
36336b80b18fSScott Teel 		if (offload_to_mirror == *current_group)
36346b80b18fSScott Teel 			continue;
36356b80b18fSScott Teel 		if (*current_group < (map->layout_map_count - 1)) {
36366b80b18fSScott Teel 			/* select map index from next group */
36376b80b18fSScott Teel 			*map_index += map->data_disks_per_row;
36386b80b18fSScott Teel 			(*current_group)++;
36396b80b18fSScott Teel 		} else {
36406b80b18fSScott Teel 			/* select map index from first group */
36416b80b18fSScott Teel 			*map_index %= map->data_disks_per_row;
36426b80b18fSScott Teel 			*current_group = 0;
36436b80b18fSScott Teel 		}
36446b80b18fSScott Teel 	} while (offload_to_mirror != *current_group);
36456b80b18fSScott Teel }
36466b80b18fSScott Teel 
3647283b4a9bSStephen M. Cameron /*
3648283b4a9bSStephen M. Cameron  * Attempt to perform offload RAID mapping for a logical volume I/O.
3649283b4a9bSStephen M. Cameron  */
3650283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
3651283b4a9bSStephen M. Cameron 	struct CommandList *c)
3652283b4a9bSStephen M. Cameron {
3653283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
3654283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3655283b4a9bSStephen M. Cameron 	struct raid_map_data *map = &dev->raid_map;
3656283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map->data[0];
3657283b4a9bSStephen M. Cameron 	int is_write = 0;
3658283b4a9bSStephen M. Cameron 	u32 map_index;
3659283b4a9bSStephen M. Cameron 	u64 first_block, last_block;
3660283b4a9bSStephen M. Cameron 	u32 block_cnt;
3661283b4a9bSStephen M. Cameron 	u32 blocks_per_row;
3662283b4a9bSStephen M. Cameron 	u64 first_row, last_row;
3663283b4a9bSStephen M. Cameron 	u32 first_row_offset, last_row_offset;
3664283b4a9bSStephen M. Cameron 	u32 first_column, last_column;
36656b80b18fSScott Teel 	u64 r0_first_row, r0_last_row;
36666b80b18fSScott Teel 	u32 r5or6_blocks_per_row;
36676b80b18fSScott Teel 	u64 r5or6_first_row, r5or6_last_row;
36686b80b18fSScott Teel 	u32 r5or6_first_row_offset, r5or6_last_row_offset;
36696b80b18fSScott Teel 	u32 r5or6_first_column, r5or6_last_column;
36706b80b18fSScott Teel 	u32 total_disks_per_row;
36716b80b18fSScott Teel 	u32 stripesize;
36726b80b18fSScott Teel 	u32 first_group, last_group, current_group;
3673283b4a9bSStephen M. Cameron 	u32 map_row;
3674283b4a9bSStephen M. Cameron 	u32 disk_handle;
3675283b4a9bSStephen M. Cameron 	u64 disk_block;
3676283b4a9bSStephen M. Cameron 	u32 disk_block_cnt;
3677283b4a9bSStephen M. Cameron 	u8 cdb[16];
3678283b4a9bSStephen M. Cameron 	u8 cdb_len;
3679283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
3680283b4a9bSStephen M. Cameron 	u64 tmpdiv;
3681283b4a9bSStephen M. Cameron #endif
36826b80b18fSScott Teel 	int offload_to_mirror;
3683283b4a9bSStephen M. Cameron 
3684283b4a9bSStephen M. Cameron 	BUG_ON(!(dev->offload_config && dev->offload_enabled));
3685283b4a9bSStephen M. Cameron 
3686283b4a9bSStephen M. Cameron 	/* check for valid opcode, get LBA and block count */
3687283b4a9bSStephen M. Cameron 	switch (cmd->cmnd[0]) {
3688283b4a9bSStephen M. Cameron 	case WRITE_6:
3689283b4a9bSStephen M. Cameron 		is_write = 1;
3690283b4a9bSStephen M. Cameron 	case READ_6:
3691283b4a9bSStephen M. Cameron 		first_block =
3692283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 8) |
3693283b4a9bSStephen M. Cameron 			cmd->cmnd[3];
3694283b4a9bSStephen M. Cameron 		block_cnt = cmd->cmnd[4];
3695283b4a9bSStephen M. Cameron 		break;
3696283b4a9bSStephen M. Cameron 	case WRITE_10:
3697283b4a9bSStephen M. Cameron 		is_write = 1;
3698283b4a9bSStephen M. Cameron 	case READ_10:
3699283b4a9bSStephen M. Cameron 		first_block =
3700283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
3701283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
3702283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
3703283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
3704283b4a9bSStephen M. Cameron 		block_cnt =
3705283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 8) |
3706283b4a9bSStephen M. Cameron 			cmd->cmnd[8];
3707283b4a9bSStephen M. Cameron 		break;
3708283b4a9bSStephen M. Cameron 	case WRITE_12:
3709283b4a9bSStephen M. Cameron 		is_write = 1;
3710283b4a9bSStephen M. Cameron 	case READ_12:
3711283b4a9bSStephen M. Cameron 		first_block =
3712283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
3713283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
3714283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
3715283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
3716283b4a9bSStephen M. Cameron 		block_cnt =
3717283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[6]) << 24) |
3718283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 16) |
3719283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[8]) << 8) |
3720283b4a9bSStephen M. Cameron 		cmd->cmnd[9];
3721283b4a9bSStephen M. Cameron 		break;
3722283b4a9bSStephen M. Cameron 	case WRITE_16:
3723283b4a9bSStephen M. Cameron 		is_write = 1;
3724283b4a9bSStephen M. Cameron 	case READ_16:
3725283b4a9bSStephen M. Cameron 		first_block =
3726283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 56) |
3727283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 48) |
3728283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 40) |
3729283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[5]) << 32) |
3730283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[6]) << 24) |
3731283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[7]) << 16) |
3732283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[8]) << 8) |
3733283b4a9bSStephen M. Cameron 			cmd->cmnd[9];
3734283b4a9bSStephen M. Cameron 		block_cnt =
3735283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[10]) << 24) |
3736283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[11]) << 16) |
3737283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[12]) << 8) |
3738283b4a9bSStephen M. Cameron 			cmd->cmnd[13];
3739283b4a9bSStephen M. Cameron 		break;
3740283b4a9bSStephen M. Cameron 	default:
3741283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
3742283b4a9bSStephen M. Cameron 	}
3743283b4a9bSStephen M. Cameron 	BUG_ON(block_cnt == 0);
3744283b4a9bSStephen M. Cameron 	last_block = first_block + block_cnt - 1;
3745283b4a9bSStephen M. Cameron 
3746283b4a9bSStephen M. Cameron 	/* check for write to non-RAID-0 */
3747283b4a9bSStephen M. Cameron 	if (is_write && dev->raid_level != 0)
3748283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3749283b4a9bSStephen M. Cameron 
3750283b4a9bSStephen M. Cameron 	/* check for invalid block or wraparound */
3751283b4a9bSStephen M. Cameron 	if (last_block >= map->volume_blk_cnt || last_block < first_block)
3752283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3753283b4a9bSStephen M. Cameron 
3754283b4a9bSStephen M. Cameron 	/* calculate stripe information for the request */
3755283b4a9bSStephen M. Cameron 	blocks_per_row = map->data_disks_per_row * map->strip_size;
3756283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
3757283b4a9bSStephen M. Cameron 	tmpdiv = first_block;
3758283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
3759283b4a9bSStephen M. Cameron 	first_row = tmpdiv;
3760283b4a9bSStephen M. Cameron 	tmpdiv = last_block;
3761283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
3762283b4a9bSStephen M. Cameron 	last_row = tmpdiv;
3763283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
3764283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
3765283b4a9bSStephen M. Cameron 	tmpdiv = first_row_offset;
3766283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv,  map->strip_size);
3767283b4a9bSStephen M. Cameron 	first_column = tmpdiv;
3768283b4a9bSStephen M. Cameron 	tmpdiv = last_row_offset;
3769283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, map->strip_size);
3770283b4a9bSStephen M. Cameron 	last_column = tmpdiv;
3771283b4a9bSStephen M. Cameron #else
3772283b4a9bSStephen M. Cameron 	first_row = first_block / blocks_per_row;
3773283b4a9bSStephen M. Cameron 	last_row = last_block / blocks_per_row;
3774283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
3775283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
3776283b4a9bSStephen M. Cameron 	first_column = first_row_offset / map->strip_size;
3777283b4a9bSStephen M. Cameron 	last_column = last_row_offset / map->strip_size;
3778283b4a9bSStephen M. Cameron #endif
3779283b4a9bSStephen M. Cameron 
3780283b4a9bSStephen M. Cameron 	/* if this isn't a single row/column then give to the controller */
3781283b4a9bSStephen M. Cameron 	if ((first_row != last_row) || (first_column != last_column))
3782283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3783283b4a9bSStephen M. Cameron 
3784283b4a9bSStephen M. Cameron 	/* proceeding with driver mapping */
37856b80b18fSScott Teel 	total_disks_per_row = map->data_disks_per_row +
37866b80b18fSScott Teel 				map->metadata_disks_per_row;
3787283b4a9bSStephen M. Cameron 	map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
3788283b4a9bSStephen M. Cameron 				map->row_cnt;
37896b80b18fSScott Teel 	map_index = (map_row * total_disks_per_row) + first_column;
37906b80b18fSScott Teel 
37916b80b18fSScott Teel 	switch (dev->raid_level) {
37926b80b18fSScott Teel 	case HPSA_RAID_0:
37936b80b18fSScott Teel 		break; /* nothing special to do */
37946b80b18fSScott Teel 	case HPSA_RAID_1:
37956b80b18fSScott Teel 		/* Handles load balance across RAID 1 members.
37966b80b18fSScott Teel 		 * (2-drive R1 and R10 with even # of drives.)
37976b80b18fSScott Teel 		 * Appropriate for SSDs, not optimal for HDDs
3798283b4a9bSStephen M. Cameron 		 */
37996b80b18fSScott Teel 		BUG_ON(map->layout_map_count != 2);
3800283b4a9bSStephen M. Cameron 		if (dev->offload_to_mirror)
3801283b4a9bSStephen M. Cameron 			map_index += map->data_disks_per_row;
3802283b4a9bSStephen M. Cameron 		dev->offload_to_mirror = !dev->offload_to_mirror;
38036b80b18fSScott Teel 		break;
38046b80b18fSScott Teel 	case HPSA_RAID_ADM:
38056b80b18fSScott Teel 		/* Handles N-way mirrors  (R1-ADM)
38066b80b18fSScott Teel 		 * and R10 with # of drives divisible by 3.)
38076b80b18fSScott Teel 		 */
38086b80b18fSScott Teel 		BUG_ON(map->layout_map_count != 3);
38096b80b18fSScott Teel 
38106b80b18fSScott Teel 		offload_to_mirror = dev->offload_to_mirror;
38116b80b18fSScott Teel 		raid_map_helper(map, offload_to_mirror,
38126b80b18fSScott Teel 				&map_index, &current_group);
38136b80b18fSScott Teel 		/* set mirror group to use next time */
38146b80b18fSScott Teel 		offload_to_mirror =
38156b80b18fSScott Teel 			(offload_to_mirror >= map->layout_map_count - 1)
38166b80b18fSScott Teel 			? 0 : offload_to_mirror + 1;
38176b80b18fSScott Teel 		/* FIXME: remove after debug/dev */
38186b80b18fSScott Teel 		BUG_ON(offload_to_mirror >= map->layout_map_count);
38196b80b18fSScott Teel 		dev_warn(&h->pdev->dev,
38206b80b18fSScott Teel 			"DEBUG: Using physical disk map index %d from mirror group %d\n",
38216b80b18fSScott Teel 			map_index, offload_to_mirror);
38226b80b18fSScott Teel 		dev->offload_to_mirror = offload_to_mirror;
38236b80b18fSScott Teel 		/* Avoid direct use of dev->offload_to_mirror within this
38246b80b18fSScott Teel 		 * function since multiple threads might simultaneously
38256b80b18fSScott Teel 		 * increment it beyond the range of dev->layout_map_count -1.
38266b80b18fSScott Teel 		 */
38276b80b18fSScott Teel 		break;
38286b80b18fSScott Teel 	case HPSA_RAID_5:
38296b80b18fSScott Teel 	case HPSA_RAID_6:
38306b80b18fSScott Teel 		if (map->layout_map_count <= 1)
38316b80b18fSScott Teel 			break;
38326b80b18fSScott Teel 
38336b80b18fSScott Teel 		/* Verify first and last block are in same RAID group */
38346b80b18fSScott Teel 		r5or6_blocks_per_row =
38356b80b18fSScott Teel 			map->strip_size * map->data_disks_per_row;
38366b80b18fSScott Teel 		BUG_ON(r5or6_blocks_per_row == 0);
38376b80b18fSScott Teel 		stripesize = r5or6_blocks_per_row * map->layout_map_count;
38386b80b18fSScott Teel #if BITS_PER_LONG == 32
38396b80b18fSScott Teel 		tmpdiv = first_block;
38406b80b18fSScott Teel 		first_group = do_div(tmpdiv, stripesize);
38416b80b18fSScott Teel 		tmpdiv = first_group;
38426b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
38436b80b18fSScott Teel 		first_group = tmpdiv;
38446b80b18fSScott Teel 		tmpdiv = last_block;
38456b80b18fSScott Teel 		last_group = do_div(tmpdiv, stripesize);
38466b80b18fSScott Teel 		tmpdiv = last_group;
38476b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
38486b80b18fSScott Teel 		last_group = tmpdiv;
38496b80b18fSScott Teel #else
38506b80b18fSScott Teel 		first_group = (first_block % stripesize) / r5or6_blocks_per_row;
38516b80b18fSScott Teel 		last_group = (last_block % stripesize) / r5or6_blocks_per_row;
38526b80b18fSScott Teel #endif
3853000ff7c2SStephen M. Cameron 		if (first_group != last_group)
38546b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
38556b80b18fSScott Teel 
38566b80b18fSScott Teel 		/* Verify request is in a single row of RAID 5/6 */
38576b80b18fSScott Teel #if BITS_PER_LONG == 32
38586b80b18fSScott Teel 		tmpdiv = first_block;
38596b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
38606b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row = tmpdiv;
38616b80b18fSScott Teel 		tmpdiv = last_block;
38626b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
38636b80b18fSScott Teel 		r5or6_last_row = r0_last_row = tmpdiv;
38646b80b18fSScott Teel #else
38656b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row =
38666b80b18fSScott Teel 						first_block / stripesize;
38676b80b18fSScott Teel 		r5or6_last_row = r0_last_row = last_block / stripesize;
38686b80b18fSScott Teel #endif
38696b80b18fSScott Teel 		if (r5or6_first_row != r5or6_last_row)
38706b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
38716b80b18fSScott Teel 
38726b80b18fSScott Teel 
38736b80b18fSScott Teel 		/* Verify request is in a single column */
38746b80b18fSScott Teel #if BITS_PER_LONG == 32
38756b80b18fSScott Teel 		tmpdiv = first_block;
38766b80b18fSScott Teel 		first_row_offset = do_div(tmpdiv, stripesize);
38776b80b18fSScott Teel 		tmpdiv = first_row_offset;
38786b80b18fSScott Teel 		first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
38796b80b18fSScott Teel 		r5or6_first_row_offset = first_row_offset;
38806b80b18fSScott Teel 		tmpdiv = last_block;
38816b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, stripesize);
38826b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
38836b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
38846b80b18fSScott Teel 		tmpdiv = r5or6_first_row_offset;
38856b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
38866b80b18fSScott Teel 		first_column = r5or6_first_column = tmpdiv;
38876b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
38886b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
38896b80b18fSScott Teel 		r5or6_last_column = tmpdiv;
38906b80b18fSScott Teel #else
38916b80b18fSScott Teel 		first_row_offset = r5or6_first_row_offset =
38926b80b18fSScott Teel 			(u32)((first_block % stripesize) %
38936b80b18fSScott Teel 						r5or6_blocks_per_row);
38946b80b18fSScott Teel 
38956b80b18fSScott Teel 		r5or6_last_row_offset =
38966b80b18fSScott Teel 			(u32)((last_block % stripesize) %
38976b80b18fSScott Teel 						r5or6_blocks_per_row);
38986b80b18fSScott Teel 
38996b80b18fSScott Teel 		first_column = r5or6_first_column =
39006b80b18fSScott Teel 			r5or6_first_row_offset / map->strip_size;
39016b80b18fSScott Teel 		r5or6_last_column =
39026b80b18fSScott Teel 			r5or6_last_row_offset / map->strip_size;
39036b80b18fSScott Teel #endif
39046b80b18fSScott Teel 		if (r5or6_first_column != r5or6_last_column)
39056b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
39066b80b18fSScott Teel 
39076b80b18fSScott Teel 		/* Request is eligible */
39086b80b18fSScott Teel 		map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
39096b80b18fSScott Teel 			map->row_cnt;
39106b80b18fSScott Teel 
39116b80b18fSScott Teel 		map_index = (first_group *
39126b80b18fSScott Teel 			(map->row_cnt * total_disks_per_row)) +
39136b80b18fSScott Teel 			(map_row * total_disks_per_row) + first_column;
39146b80b18fSScott Teel 		break;
39156b80b18fSScott Teel 	default:
39166b80b18fSScott Teel 		return IO_ACCEL_INELIGIBLE;
3917283b4a9bSStephen M. Cameron 	}
39186b80b18fSScott Teel 
3919283b4a9bSStephen M. Cameron 	disk_handle = dd[map_index].ioaccel_handle;
3920283b4a9bSStephen M. Cameron 	disk_block = map->disk_starting_blk + (first_row * map->strip_size) +
3921283b4a9bSStephen M. Cameron 			(first_row_offset - (first_column * map->strip_size));
3922283b4a9bSStephen M. Cameron 	disk_block_cnt = block_cnt;
3923283b4a9bSStephen M. Cameron 
3924283b4a9bSStephen M. Cameron 	/* handle differing logical/physical block sizes */
3925283b4a9bSStephen M. Cameron 	if (map->phys_blk_shift) {
3926283b4a9bSStephen M. Cameron 		disk_block <<= map->phys_blk_shift;
3927283b4a9bSStephen M. Cameron 		disk_block_cnt <<= map->phys_blk_shift;
3928283b4a9bSStephen M. Cameron 	}
3929283b4a9bSStephen M. Cameron 	BUG_ON(disk_block_cnt > 0xffff);
3930283b4a9bSStephen M. Cameron 
3931283b4a9bSStephen M. Cameron 	/* build the new CDB for the physical disk I/O */
3932283b4a9bSStephen M. Cameron 	if (disk_block > 0xffffffff) {
3933283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_16 : READ_16;
3934283b4a9bSStephen M. Cameron 		cdb[1] = 0;
3935283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 56);
3936283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 48);
3937283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 40);
3938283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block >> 32);
3939283b4a9bSStephen M. Cameron 		cdb[6] = (u8) (disk_block >> 24);
3940283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block >> 16);
3941283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block >> 8);
3942283b4a9bSStephen M. Cameron 		cdb[9] = (u8) (disk_block);
3943283b4a9bSStephen M. Cameron 		cdb[10] = (u8) (disk_block_cnt >> 24);
3944283b4a9bSStephen M. Cameron 		cdb[11] = (u8) (disk_block_cnt >> 16);
3945283b4a9bSStephen M. Cameron 		cdb[12] = (u8) (disk_block_cnt >> 8);
3946283b4a9bSStephen M. Cameron 		cdb[13] = (u8) (disk_block_cnt);
3947283b4a9bSStephen M. Cameron 		cdb[14] = 0;
3948283b4a9bSStephen M. Cameron 		cdb[15] = 0;
3949283b4a9bSStephen M. Cameron 		cdb_len = 16;
3950283b4a9bSStephen M. Cameron 	} else {
3951283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
3952283b4a9bSStephen M. Cameron 		cdb[1] = 0;
3953283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 24);
3954283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 16);
3955283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 8);
3956283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block);
3957283b4a9bSStephen M. Cameron 		cdb[6] = 0;
3958283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block_cnt >> 8);
3959283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block_cnt);
3960283b4a9bSStephen M. Cameron 		cdb[9] = 0;
3961283b4a9bSStephen M. Cameron 		cdb_len = 10;
3962283b4a9bSStephen M. Cameron 	}
3963283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
3964283b4a9bSStephen M. Cameron 						dev->scsi3addr);
3965283b4a9bSStephen M. Cameron }
3966283b4a9bSStephen M. Cameron 
3967f281233dSJeff Garzik static int hpsa_scsi_queue_command_lck(struct scsi_cmnd *cmd,
3968edd16368SStephen M. Cameron 	void (*done)(struct scsi_cmnd *))
3969edd16368SStephen M. Cameron {
3970edd16368SStephen M. Cameron 	struct ctlr_info *h;
3971edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
3972edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
3973edd16368SStephen M. Cameron 	struct CommandList *c;
3974edd16368SStephen M. Cameron 	unsigned long flags;
3975283b4a9bSStephen M. Cameron 	int rc = 0;
3976edd16368SStephen M. Cameron 
3977edd16368SStephen M. Cameron 	/* Get the ptr to our adapter structure out of cmd->host. */
3978edd16368SStephen M. Cameron 	h = sdev_to_hba(cmd->device);
3979edd16368SStephen M. Cameron 	dev = cmd->device->hostdata;
3980edd16368SStephen M. Cameron 	if (!dev) {
3981edd16368SStephen M. Cameron 		cmd->result = DID_NO_CONNECT << 16;
3982edd16368SStephen M. Cameron 		done(cmd);
3983edd16368SStephen M. Cameron 		return 0;
3984edd16368SStephen M. Cameron 	}
3985edd16368SStephen M. Cameron 	memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
3986edd16368SStephen M. Cameron 
3987edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
3988a0c12413SStephen M. Cameron 	if (unlikely(h->lockup_detected)) {
3989a0c12413SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
3990a0c12413SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
3991a0c12413SStephen M. Cameron 		done(cmd);
3992a0c12413SStephen M. Cameron 		return 0;
3993a0c12413SStephen M. Cameron 	}
3994edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
3995e16a33adSMatt Gates 	c = cmd_alloc(h);
3996edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
3997edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
3998edd16368SStephen M. Cameron 		return SCSI_MLQUEUE_HOST_BUSY;
3999edd16368SStephen M. Cameron 	}
4000edd16368SStephen M. Cameron 
4001edd16368SStephen M. Cameron 	/* Fill in the command list header */
4002edd16368SStephen M. Cameron 
4003edd16368SStephen M. Cameron 	cmd->scsi_done = done;    /* save this for use by completion code */
4004edd16368SStephen M. Cameron 
4005edd16368SStephen M. Cameron 	/* save c in case we have to abort it  */
4006edd16368SStephen M. Cameron 	cmd->host_scribble = (unsigned char *) c;
4007edd16368SStephen M. Cameron 
4008edd16368SStephen M. Cameron 	c->cmd_type = CMD_SCSI;
4009edd16368SStephen M. Cameron 	c->scsi_cmd = cmd;
4010e1f7de0cSMatt Gates 
4011283b4a9bSStephen M. Cameron 	/* Call alternate submit routine for I/O accelerated commands.
4012283b4a9bSStephen M. Cameron 	 * Retries always go down the normal I/O path.
4013283b4a9bSStephen M. Cameron 	 */
4014283b4a9bSStephen M. Cameron 	if (likely(cmd->retries == 0 &&
4015da0697bdSScott Teel 		cmd->request->cmd_type == REQ_TYPE_FS &&
4016da0697bdSScott Teel 		h->acciopath_status)) {
4017283b4a9bSStephen M. Cameron 		if (dev->offload_enabled) {
4018283b4a9bSStephen M. Cameron 			rc = hpsa_scsi_ioaccel_raid_map(h, c);
4019283b4a9bSStephen M. Cameron 			if (rc == 0)
4020283b4a9bSStephen M. Cameron 				return 0; /* Sent on ioaccel path */
4021283b4a9bSStephen M. Cameron 			if (rc < 0) {   /* scsi_dma_map failed. */
4022283b4a9bSStephen M. Cameron 				cmd_free(h, c);
4023283b4a9bSStephen M. Cameron 				return SCSI_MLQUEUE_HOST_BUSY;
4024283b4a9bSStephen M. Cameron 			}
4025283b4a9bSStephen M. Cameron 		} else if (dev->ioaccel_handle) {
4026283b4a9bSStephen M. Cameron 			rc = hpsa_scsi_ioaccel_direct_map(h, c);
4027283b4a9bSStephen M. Cameron 			if (rc == 0)
4028283b4a9bSStephen M. Cameron 				return 0; /* Sent on direct map path */
4029283b4a9bSStephen M. Cameron 			if (rc < 0) {   /* scsi_dma_map failed. */
4030283b4a9bSStephen M. Cameron 				cmd_free(h, c);
4031283b4a9bSStephen M. Cameron 				return SCSI_MLQUEUE_HOST_BUSY;
4032283b4a9bSStephen M. Cameron 			}
4033283b4a9bSStephen M. Cameron 		}
4034283b4a9bSStephen M. Cameron 	}
4035e1f7de0cSMatt Gates 
4036edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;  /* unused in simple mode */
4037edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
4038303932fdSDon Brace 	c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT);
4039303932fdSDon Brace 	c->Header.Tag.lower |= DIRECT_LOOKUP_BIT;
4040edd16368SStephen M. Cameron 
4041edd16368SStephen M. Cameron 	/* Fill in the request block... */
4042edd16368SStephen M. Cameron 
4043edd16368SStephen M. Cameron 	c->Request.Timeout = 0;
4044edd16368SStephen M. Cameron 	memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
4045edd16368SStephen M. Cameron 	BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
4046edd16368SStephen M. Cameron 	c->Request.CDBLen = cmd->cmd_len;
4047edd16368SStephen M. Cameron 	memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
4048edd16368SStephen M. Cameron 	c->Request.Type.Type = TYPE_CMD;
4049edd16368SStephen M. Cameron 	c->Request.Type.Attribute = ATTR_SIMPLE;
4050edd16368SStephen M. Cameron 	switch (cmd->sc_data_direction) {
4051edd16368SStephen M. Cameron 	case DMA_TO_DEVICE:
4052edd16368SStephen M. Cameron 		c->Request.Type.Direction = XFER_WRITE;
4053edd16368SStephen M. Cameron 		break;
4054edd16368SStephen M. Cameron 	case DMA_FROM_DEVICE:
4055edd16368SStephen M. Cameron 		c->Request.Type.Direction = XFER_READ;
4056edd16368SStephen M. Cameron 		break;
4057edd16368SStephen M. Cameron 	case DMA_NONE:
4058edd16368SStephen M. Cameron 		c->Request.Type.Direction = XFER_NONE;
4059edd16368SStephen M. Cameron 		break;
4060edd16368SStephen M. Cameron 	case DMA_BIDIRECTIONAL:
4061edd16368SStephen M. Cameron 		/* This can happen if a buggy application does a scsi passthru
4062edd16368SStephen M. Cameron 		 * and sets both inlen and outlen to non-zero. ( see
4063edd16368SStephen M. Cameron 		 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
4064edd16368SStephen M. Cameron 		 */
4065edd16368SStephen M. Cameron 
4066edd16368SStephen M. Cameron 		c->Request.Type.Direction = XFER_RSVD;
4067edd16368SStephen M. Cameron 		/* This is technically wrong, and hpsa controllers should
4068edd16368SStephen M. Cameron 		 * reject it with CMD_INVALID, which is the most correct
4069edd16368SStephen M. Cameron 		 * response, but non-fibre backends appear to let it
4070edd16368SStephen M. Cameron 		 * slide by, and give the same results as if this field
4071edd16368SStephen M. Cameron 		 * were set correctly.  Either way is acceptable for
4072edd16368SStephen M. Cameron 		 * our purposes here.
4073edd16368SStephen M. Cameron 		 */
4074edd16368SStephen M. Cameron 
4075edd16368SStephen M. Cameron 		break;
4076edd16368SStephen M. Cameron 
4077edd16368SStephen M. Cameron 	default:
4078edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4079edd16368SStephen M. Cameron 			cmd->sc_data_direction);
4080edd16368SStephen M. Cameron 		BUG();
4081edd16368SStephen M. Cameron 		break;
4082edd16368SStephen M. Cameron 	}
4083edd16368SStephen M. Cameron 
408433a2ffceSStephen M. Cameron 	if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
4085edd16368SStephen M. Cameron 		cmd_free(h, c);
4086edd16368SStephen M. Cameron 		return SCSI_MLQUEUE_HOST_BUSY;
4087edd16368SStephen M. Cameron 	}
4088edd16368SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
4089edd16368SStephen M. Cameron 	/* the cmd'll come back via intr handler in complete_scsi_command()  */
4090edd16368SStephen M. Cameron 	return 0;
4091edd16368SStephen M. Cameron }
4092edd16368SStephen M. Cameron 
4093f281233dSJeff Garzik static DEF_SCSI_QCMD(hpsa_scsi_queue_command)
4094f281233dSJeff Garzik 
40955f389360SStephen M. Cameron static int do_not_scan_if_controller_locked_up(struct ctlr_info *h)
40965f389360SStephen M. Cameron {
40975f389360SStephen M. Cameron 	unsigned long flags;
40985f389360SStephen M. Cameron 
40995f389360SStephen M. Cameron 	/*
41005f389360SStephen M. Cameron 	 * Don't let rescans be initiated on a controller known
41015f389360SStephen M. Cameron 	 * to be locked up.  If the controller locks up *during*
41025f389360SStephen M. Cameron 	 * a rescan, that thread is probably hosed, but at least
41035f389360SStephen M. Cameron 	 * we can prevent new rescan threads from piling up on a
41045f389360SStephen M. Cameron 	 * locked up controller.
41055f389360SStephen M. Cameron 	 */
41065f389360SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
41075f389360SStephen M. Cameron 	if (unlikely(h->lockup_detected)) {
41085f389360SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
41095f389360SStephen M. Cameron 		spin_lock_irqsave(&h->scan_lock, flags);
41105f389360SStephen M. Cameron 		h->scan_finished = 1;
41115f389360SStephen M. Cameron 		wake_up_all(&h->scan_wait_queue);
41125f389360SStephen M. Cameron 		spin_unlock_irqrestore(&h->scan_lock, flags);
41135f389360SStephen M. Cameron 		return 1;
41145f389360SStephen M. Cameron 	}
41155f389360SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
41165f389360SStephen M. Cameron 	return 0;
41175f389360SStephen M. Cameron }
41185f389360SStephen M. Cameron 
4119a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *sh)
4120a08a8471SStephen M. Cameron {
4121a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
4122a08a8471SStephen M. Cameron 	unsigned long flags;
4123a08a8471SStephen M. Cameron 
41245f389360SStephen M. Cameron 	if (do_not_scan_if_controller_locked_up(h))
41255f389360SStephen M. Cameron 		return;
41265f389360SStephen M. Cameron 
4127a08a8471SStephen M. Cameron 	/* wait until any scan already in progress is finished. */
4128a08a8471SStephen M. Cameron 	while (1) {
4129a08a8471SStephen M. Cameron 		spin_lock_irqsave(&h->scan_lock, flags);
4130a08a8471SStephen M. Cameron 		if (h->scan_finished)
4131a08a8471SStephen M. Cameron 			break;
4132a08a8471SStephen M. Cameron 		spin_unlock_irqrestore(&h->scan_lock, flags);
4133a08a8471SStephen M. Cameron 		wait_event(h->scan_wait_queue, h->scan_finished);
4134a08a8471SStephen M. Cameron 		/* Note: We don't need to worry about a race between this
4135a08a8471SStephen M. Cameron 		 * thread and driver unload because the midlayer will
4136a08a8471SStephen M. Cameron 		 * have incremented the reference count, so unload won't
4137a08a8471SStephen M. Cameron 		 * happen if we're in here.
4138a08a8471SStephen M. Cameron 		 */
4139a08a8471SStephen M. Cameron 	}
4140a08a8471SStephen M. Cameron 	h->scan_finished = 0; /* mark scan as in progress */
4141a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4142a08a8471SStephen M. Cameron 
41435f389360SStephen M. Cameron 	if (do_not_scan_if_controller_locked_up(h))
41445f389360SStephen M. Cameron 		return;
41455f389360SStephen M. Cameron 
4146a08a8471SStephen M. Cameron 	hpsa_update_scsi_devices(h, h->scsi_host->host_no);
4147a08a8471SStephen M. Cameron 
4148a08a8471SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
4149a08a8471SStephen M. Cameron 	h->scan_finished = 1; /* mark scan as finished. */
4150a08a8471SStephen M. Cameron 	wake_up_all(&h->scan_wait_queue);
4151a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4152a08a8471SStephen M. Cameron }
4153a08a8471SStephen M. Cameron 
4154a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
4155a08a8471SStephen M. Cameron 	unsigned long elapsed_time)
4156a08a8471SStephen M. Cameron {
4157a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
4158a08a8471SStephen M. Cameron 	unsigned long flags;
4159a08a8471SStephen M. Cameron 	int finished;
4160a08a8471SStephen M. Cameron 
4161a08a8471SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
4162a08a8471SStephen M. Cameron 	finished = h->scan_finished;
4163a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4164a08a8471SStephen M. Cameron 	return finished;
4165a08a8471SStephen M. Cameron }
4166a08a8471SStephen M. Cameron 
4167667e23d4SStephen M. Cameron static int hpsa_change_queue_depth(struct scsi_device *sdev,
4168667e23d4SStephen M. Cameron 	int qdepth, int reason)
4169667e23d4SStephen M. Cameron {
4170667e23d4SStephen M. Cameron 	struct ctlr_info *h = sdev_to_hba(sdev);
4171667e23d4SStephen M. Cameron 
4172667e23d4SStephen M. Cameron 	if (reason != SCSI_QDEPTH_DEFAULT)
4173667e23d4SStephen M. Cameron 		return -ENOTSUPP;
4174667e23d4SStephen M. Cameron 
4175667e23d4SStephen M. Cameron 	if (qdepth < 1)
4176667e23d4SStephen M. Cameron 		qdepth = 1;
4177667e23d4SStephen M. Cameron 	else
4178667e23d4SStephen M. Cameron 		if (qdepth > h->nr_cmds)
4179667e23d4SStephen M. Cameron 			qdepth = h->nr_cmds;
4180667e23d4SStephen M. Cameron 	scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
4181667e23d4SStephen M. Cameron 	return sdev->queue_depth;
4182667e23d4SStephen M. Cameron }
4183667e23d4SStephen M. Cameron 
4184edd16368SStephen M. Cameron static void hpsa_unregister_scsi(struct ctlr_info *h)
4185edd16368SStephen M. Cameron {
4186edd16368SStephen M. Cameron 	/* we are being forcibly unloaded, and may not refuse. */
4187edd16368SStephen M. Cameron 	scsi_remove_host(h->scsi_host);
4188edd16368SStephen M. Cameron 	scsi_host_put(h->scsi_host);
4189edd16368SStephen M. Cameron 	h->scsi_host = NULL;
4190edd16368SStephen M. Cameron }
4191edd16368SStephen M. Cameron 
4192edd16368SStephen M. Cameron static int hpsa_register_scsi(struct ctlr_info *h)
4193edd16368SStephen M. Cameron {
4194b705690dSStephen M. Cameron 	struct Scsi_Host *sh;
4195b705690dSStephen M. Cameron 	int error;
4196edd16368SStephen M. Cameron 
4197b705690dSStephen M. Cameron 	sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
4198b705690dSStephen M. Cameron 	if (sh == NULL)
4199b705690dSStephen M. Cameron 		goto fail;
4200b705690dSStephen M. Cameron 
4201b705690dSStephen M. Cameron 	sh->io_port = 0;
4202b705690dSStephen M. Cameron 	sh->n_io_port = 0;
4203b705690dSStephen M. Cameron 	sh->this_id = -1;
4204b705690dSStephen M. Cameron 	sh->max_channel = 3;
4205b705690dSStephen M. Cameron 	sh->max_cmd_len = MAX_COMMAND_SIZE;
4206b705690dSStephen M. Cameron 	sh->max_lun = HPSA_MAX_LUN;
4207b705690dSStephen M. Cameron 	sh->max_id = HPSA_MAX_LUN;
4208b705690dSStephen M. Cameron 	sh->can_queue = h->nr_cmds;
4209316b221aSStephen M. Cameron 	if (h->hba_mode_enabled)
4210316b221aSStephen M. Cameron 		sh->cmd_per_lun = 7;
4211316b221aSStephen M. Cameron 	else
4212b705690dSStephen M. Cameron 		sh->cmd_per_lun = h->nr_cmds;
4213b705690dSStephen M. Cameron 	sh->sg_tablesize = h->maxsgentries;
4214b705690dSStephen M. Cameron 	h->scsi_host = sh;
4215b705690dSStephen M. Cameron 	sh->hostdata[0] = (unsigned long) h;
4216b705690dSStephen M. Cameron 	sh->irq = h->intr[h->intr_mode];
4217b705690dSStephen M. Cameron 	sh->unique_id = sh->irq;
4218b705690dSStephen M. Cameron 	error = scsi_add_host(sh, &h->pdev->dev);
4219b705690dSStephen M. Cameron 	if (error)
4220b705690dSStephen M. Cameron 		goto fail_host_put;
4221b705690dSStephen M. Cameron 	scsi_scan_host(sh);
4222b705690dSStephen M. Cameron 	return 0;
4223b705690dSStephen M. Cameron 
4224b705690dSStephen M. Cameron  fail_host_put:
4225b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_add_host"
4226b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
4227b705690dSStephen M. Cameron 	scsi_host_put(sh);
4228b705690dSStephen M. Cameron 	return error;
4229b705690dSStephen M. Cameron  fail:
4230b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_host_alloc"
4231b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
4232b705690dSStephen M. Cameron 	return -ENOMEM;
4233edd16368SStephen M. Cameron }
4234edd16368SStephen M. Cameron 
4235edd16368SStephen M. Cameron static int wait_for_device_to_become_ready(struct ctlr_info *h,
4236edd16368SStephen M. Cameron 	unsigned char lunaddr[])
4237edd16368SStephen M. Cameron {
42388919358eSTomas Henzl 	int rc;
4239edd16368SStephen M. Cameron 	int count = 0;
4240edd16368SStephen M. Cameron 	int waittime = 1; /* seconds */
4241edd16368SStephen M. Cameron 	struct CommandList *c;
4242edd16368SStephen M. Cameron 
4243edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
4244edd16368SStephen M. Cameron 	if (!c) {
4245edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in "
4246edd16368SStephen M. Cameron 			"wait_for_device_to_become_ready.\n");
4247edd16368SStephen M. Cameron 		return IO_ERROR;
4248edd16368SStephen M. Cameron 	}
4249edd16368SStephen M. Cameron 
4250edd16368SStephen M. Cameron 	/* Send test unit ready until device ready, or give up. */
4251edd16368SStephen M. Cameron 	while (count < HPSA_TUR_RETRY_LIMIT) {
4252edd16368SStephen M. Cameron 
4253edd16368SStephen M. Cameron 		/* Wait for a bit.  do this first, because if we send
4254edd16368SStephen M. Cameron 		 * the TUR right away, the reset will just abort it.
4255edd16368SStephen M. Cameron 		 */
4256edd16368SStephen M. Cameron 		msleep(1000 * waittime);
4257edd16368SStephen M. Cameron 		count++;
42588919358eSTomas Henzl 		rc = 0; /* Device ready. */
4259edd16368SStephen M. Cameron 
4260edd16368SStephen M. Cameron 		/* Increase wait time with each try, up to a point. */
4261edd16368SStephen M. Cameron 		if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
4262edd16368SStephen M. Cameron 			waittime = waittime * 2;
4263edd16368SStephen M. Cameron 
4264a2dac136SStephen M. Cameron 		/* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
4265a2dac136SStephen M. Cameron 		(void) fill_cmd(c, TEST_UNIT_READY, h,
4266a2dac136SStephen M. Cameron 				NULL, 0, 0, lunaddr, TYPE_CMD);
4267edd16368SStephen M. Cameron 		hpsa_scsi_do_simple_cmd_core(h, c);
4268edd16368SStephen M. Cameron 		/* no unmap needed here because no data xfer. */
4269edd16368SStephen M. Cameron 
4270edd16368SStephen M. Cameron 		if (c->err_info->CommandStatus == CMD_SUCCESS)
4271edd16368SStephen M. Cameron 			break;
4272edd16368SStephen M. Cameron 
4273edd16368SStephen M. Cameron 		if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
4274edd16368SStephen M. Cameron 			c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
4275edd16368SStephen M. Cameron 			(c->err_info->SenseInfo[2] == NO_SENSE ||
4276edd16368SStephen M. Cameron 			c->err_info->SenseInfo[2] == UNIT_ATTENTION))
4277edd16368SStephen M. Cameron 			break;
4278edd16368SStephen M. Cameron 
4279edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "waiting %d secs "
4280edd16368SStephen M. Cameron 			"for device to become ready.\n", waittime);
4281edd16368SStephen M. Cameron 		rc = 1; /* device not ready. */
4282edd16368SStephen M. Cameron 	}
4283edd16368SStephen M. Cameron 
4284edd16368SStephen M. Cameron 	if (rc)
4285edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "giving up on device.\n");
4286edd16368SStephen M. Cameron 	else
4287edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "device is ready.\n");
4288edd16368SStephen M. Cameron 
4289edd16368SStephen M. Cameron 	cmd_special_free(h, c);
4290edd16368SStephen M. Cameron 	return rc;
4291edd16368SStephen M. Cameron }
4292edd16368SStephen M. Cameron 
4293edd16368SStephen M. Cameron /* Need at least one of these error handlers to keep ../scsi/hosts.c from
4294edd16368SStephen M. Cameron  * complaining.  Doing a host- or bus-reset can't do anything good here.
4295edd16368SStephen M. Cameron  */
4296edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
4297edd16368SStephen M. Cameron {
4298edd16368SStephen M. Cameron 	int rc;
4299edd16368SStephen M. Cameron 	struct ctlr_info *h;
4300edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
4301edd16368SStephen M. Cameron 
4302edd16368SStephen M. Cameron 	/* find the controller to which the command to be aborted was sent */
4303edd16368SStephen M. Cameron 	h = sdev_to_hba(scsicmd->device);
4304edd16368SStephen M. Cameron 	if (h == NULL) /* paranoia */
4305edd16368SStephen M. Cameron 		return FAILED;
4306edd16368SStephen M. Cameron 	dev = scsicmd->device->hostdata;
4307edd16368SStephen M. Cameron 	if (!dev) {
4308edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
4309edd16368SStephen M. Cameron 			"device lookup failed.\n");
4310edd16368SStephen M. Cameron 		return FAILED;
4311edd16368SStephen M. Cameron 	}
4312d416b0c7SStephen M. Cameron 	dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
4313d416b0c7SStephen M. Cameron 		h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
4314edd16368SStephen M. Cameron 	/* send a reset to the SCSI LUN which the command was sent to */
4315bf711ac6SScott Teel 	rc = hpsa_send_reset(h, dev->scsi3addr, HPSA_RESET_TYPE_LUN);
4316edd16368SStephen M. Cameron 	if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
4317edd16368SStephen M. Cameron 		return SUCCESS;
4318edd16368SStephen M. Cameron 
4319edd16368SStephen M. Cameron 	dev_warn(&h->pdev->dev, "resetting device failed.\n");
4320edd16368SStephen M. Cameron 	return FAILED;
4321edd16368SStephen M. Cameron }
4322edd16368SStephen M. Cameron 
43236cba3f19SStephen M. Cameron static void swizzle_abort_tag(u8 *tag)
43246cba3f19SStephen M. Cameron {
43256cba3f19SStephen M. Cameron 	u8 original_tag[8];
43266cba3f19SStephen M. Cameron 
43276cba3f19SStephen M. Cameron 	memcpy(original_tag, tag, 8);
43286cba3f19SStephen M. Cameron 	tag[0] = original_tag[3];
43296cba3f19SStephen M. Cameron 	tag[1] = original_tag[2];
43306cba3f19SStephen M. Cameron 	tag[2] = original_tag[1];
43316cba3f19SStephen M. Cameron 	tag[3] = original_tag[0];
43326cba3f19SStephen M. Cameron 	tag[4] = original_tag[7];
43336cba3f19SStephen M. Cameron 	tag[5] = original_tag[6];
43346cba3f19SStephen M. Cameron 	tag[6] = original_tag[5];
43356cba3f19SStephen M. Cameron 	tag[7] = original_tag[4];
43366cba3f19SStephen M. Cameron }
43376cba3f19SStephen M. Cameron 
433817eb87d2SScott Teel static void hpsa_get_tag(struct ctlr_info *h,
433917eb87d2SScott Teel 	struct CommandList *c, u32 *taglower, u32 *tagupper)
434017eb87d2SScott Teel {
434117eb87d2SScott Teel 	if (c->cmd_type == CMD_IOACCEL1) {
434217eb87d2SScott Teel 		struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
434317eb87d2SScott Teel 			&h->ioaccel_cmd_pool[c->cmdindex];
434417eb87d2SScott Teel 		*tagupper = cm1->Tag.upper;
434517eb87d2SScott Teel 		*taglower = cm1->Tag.lower;
434654b6e9e9SScott Teel 		return;
434754b6e9e9SScott Teel 	}
434854b6e9e9SScott Teel 	if (c->cmd_type == CMD_IOACCEL2) {
434954b6e9e9SScott Teel 		struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
435054b6e9e9SScott Teel 			&h->ioaccel2_cmd_pool[c->cmdindex];
4351dd0e19f3SScott Teel 		/* upper tag not used in ioaccel2 mode */
4352dd0e19f3SScott Teel 		memset(tagupper, 0, sizeof(*tagupper));
4353dd0e19f3SScott Teel 		*taglower = cm2->Tag;
435454b6e9e9SScott Teel 		return;
435554b6e9e9SScott Teel 	}
435617eb87d2SScott Teel 	*tagupper = c->Header.Tag.upper;
435717eb87d2SScott Teel 	*taglower = c->Header.Tag.lower;
435817eb87d2SScott Teel }
435954b6e9e9SScott Teel 
436017eb87d2SScott Teel 
436175167d2cSStephen M. Cameron static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
43626cba3f19SStephen M. Cameron 	struct CommandList *abort, int swizzle)
436375167d2cSStephen M. Cameron {
436475167d2cSStephen M. Cameron 	int rc = IO_OK;
436575167d2cSStephen M. Cameron 	struct CommandList *c;
436675167d2cSStephen M. Cameron 	struct ErrorInfo *ei;
436717eb87d2SScott Teel 	u32 tagupper, taglower;
436875167d2cSStephen M. Cameron 
436975167d2cSStephen M. Cameron 	c = cmd_special_alloc(h);
437075167d2cSStephen M. Cameron 	if (c == NULL) {	/* trouble... */
437175167d2cSStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
437275167d2cSStephen M. Cameron 		return -ENOMEM;
437375167d2cSStephen M. Cameron 	}
437475167d2cSStephen M. Cameron 
4375a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no buffer to map */
4376a2dac136SStephen M. Cameron 	(void) fill_cmd(c, HPSA_ABORT_MSG, h, abort,
4377a2dac136SStephen M. Cameron 		0, 0, scsi3addr, TYPE_MSG);
43786cba3f19SStephen M. Cameron 	if (swizzle)
43796cba3f19SStephen M. Cameron 		swizzle_abort_tag(&c->Request.CDB[4]);
438075167d2cSStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core(h, c);
438117eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
438275167d2cSStephen M. Cameron 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd_core completed.\n",
438317eb87d2SScott Teel 		__func__, tagupper, taglower);
438475167d2cSStephen M. Cameron 	/* no unmap needed here because no data xfer. */
438575167d2cSStephen M. Cameron 
438675167d2cSStephen M. Cameron 	ei = c->err_info;
438775167d2cSStephen M. Cameron 	switch (ei->CommandStatus) {
438875167d2cSStephen M. Cameron 	case CMD_SUCCESS:
438975167d2cSStephen M. Cameron 		break;
439075167d2cSStephen M. Cameron 	case CMD_UNABORTABLE: /* Very common, don't make noise. */
439175167d2cSStephen M. Cameron 		rc = -1;
439275167d2cSStephen M. Cameron 		break;
439375167d2cSStephen M. Cameron 	default:
439475167d2cSStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
439517eb87d2SScott Teel 			__func__, tagupper, taglower);
4396d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
439775167d2cSStephen M. Cameron 		rc = -1;
439875167d2cSStephen M. Cameron 		break;
439975167d2cSStephen M. Cameron 	}
440075167d2cSStephen M. Cameron 	cmd_special_free(h, c);
4401dd0e19f3SScott Teel 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n",
4402dd0e19f3SScott Teel 		__func__, tagupper, taglower);
440375167d2cSStephen M. Cameron 	return rc;
440475167d2cSStephen M. Cameron }
440575167d2cSStephen M. Cameron 
440675167d2cSStephen M. Cameron /*
440775167d2cSStephen M. Cameron  * hpsa_find_cmd_in_queue
440875167d2cSStephen M. Cameron  *
440975167d2cSStephen M. Cameron  * Used to determine whether a command (find) is still present
441075167d2cSStephen M. Cameron  * in queue_head.   Optionally excludes the last element of queue_head.
441175167d2cSStephen M. Cameron  *
441275167d2cSStephen M. Cameron  * This is used to avoid unnecessary aborts.  Commands in h->reqQ have
441375167d2cSStephen M. Cameron  * not yet been submitted, and so can be aborted by the driver without
441475167d2cSStephen M. Cameron  * sending an abort to the hardware.
441575167d2cSStephen M. Cameron  *
441675167d2cSStephen M. Cameron  * Returns pointer to command if found in queue, NULL otherwise.
441775167d2cSStephen M. Cameron  */
441875167d2cSStephen M. Cameron static struct CommandList *hpsa_find_cmd_in_queue(struct ctlr_info *h,
441975167d2cSStephen M. Cameron 			struct scsi_cmnd *find, struct list_head *queue_head)
442075167d2cSStephen M. Cameron {
442175167d2cSStephen M. Cameron 	unsigned long flags;
442275167d2cSStephen M. Cameron 	struct CommandList *c = NULL;	/* ptr into cmpQ */
442375167d2cSStephen M. Cameron 
442475167d2cSStephen M. Cameron 	if (!find)
442575167d2cSStephen M. Cameron 		return 0;
442675167d2cSStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
442775167d2cSStephen M. Cameron 	list_for_each_entry(c, queue_head, list) {
442875167d2cSStephen M. Cameron 		if (c->scsi_cmd == NULL) /* e.g.: passthru ioctl */
442975167d2cSStephen M. Cameron 			continue;
443075167d2cSStephen M. Cameron 		if (c->scsi_cmd == find) {
443175167d2cSStephen M. Cameron 			spin_unlock_irqrestore(&h->lock, flags);
443275167d2cSStephen M. Cameron 			return c;
443375167d2cSStephen M. Cameron 		}
443475167d2cSStephen M. Cameron 	}
443575167d2cSStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
443675167d2cSStephen M. Cameron 	return NULL;
443775167d2cSStephen M. Cameron }
443875167d2cSStephen M. Cameron 
44396cba3f19SStephen M. Cameron static struct CommandList *hpsa_find_cmd_in_queue_by_tag(struct ctlr_info *h,
44406cba3f19SStephen M. Cameron 					u8 *tag, struct list_head *queue_head)
44416cba3f19SStephen M. Cameron {
44426cba3f19SStephen M. Cameron 	unsigned long flags;
44436cba3f19SStephen M. Cameron 	struct CommandList *c;
44446cba3f19SStephen M. Cameron 
44456cba3f19SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
44466cba3f19SStephen M. Cameron 	list_for_each_entry(c, queue_head, list) {
44476cba3f19SStephen M. Cameron 		if (memcmp(&c->Header.Tag, tag, 8) != 0)
44486cba3f19SStephen M. Cameron 			continue;
44496cba3f19SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
44506cba3f19SStephen M. Cameron 		return c;
44516cba3f19SStephen M. Cameron 	}
44526cba3f19SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
44536cba3f19SStephen M. Cameron 	return NULL;
44546cba3f19SStephen M. Cameron }
44556cba3f19SStephen M. Cameron 
445654b6e9e9SScott Teel /* ioaccel2 path firmware cannot handle abort task requests.
445754b6e9e9SScott Teel  * Change abort requests to physical target reset, and send to the
445854b6e9e9SScott Teel  * address of the physical disk used for the ioaccel 2 command.
445954b6e9e9SScott Teel  * Return 0 on success (IO_OK)
446054b6e9e9SScott Teel  *	 -1 on failure
446154b6e9e9SScott Teel  */
446254b6e9e9SScott Teel 
446354b6e9e9SScott Teel static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
446454b6e9e9SScott Teel 	unsigned char *scsi3addr, struct CommandList *abort)
446554b6e9e9SScott Teel {
446654b6e9e9SScott Teel 	int rc = IO_OK;
446754b6e9e9SScott Teel 	struct scsi_cmnd *scmd; /* scsi command within request being aborted */
446854b6e9e9SScott Teel 	struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
446954b6e9e9SScott Teel 	unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
447054b6e9e9SScott Teel 	unsigned char *psa = &phys_scsi3addr[0];
447154b6e9e9SScott Teel 
447254b6e9e9SScott Teel 	/* Get a pointer to the hpsa logical device. */
447354b6e9e9SScott Teel 	scmd = (struct scsi_cmnd *) abort->scsi_cmd;
447454b6e9e9SScott Teel 	dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
447554b6e9e9SScott Teel 	if (dev == NULL) {
447654b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
447754b6e9e9SScott Teel 			"Cannot abort: no device pointer for command.\n");
447854b6e9e9SScott Teel 			return -1; /* not abortable */
447954b6e9e9SScott Teel 	}
448054b6e9e9SScott Teel 
44812ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
44822ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
44832ba8bfc8SStephen 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",
44842ba8bfc8SStephen M. Cameron 			h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
44852ba8bfc8SStephen M. Cameron 			scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
44862ba8bfc8SStephen M. Cameron 			scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]);
44872ba8bfc8SStephen M. Cameron 
448854b6e9e9SScott Teel 	if (!dev->offload_enabled) {
448954b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
449054b6e9e9SScott Teel 			"Can't abort: device is not operating in HP SSD Smart Path mode.\n");
449154b6e9e9SScott Teel 		return -1; /* not abortable */
449254b6e9e9SScott Teel 	}
449354b6e9e9SScott Teel 
449454b6e9e9SScott Teel 	/* Incoming scsi3addr is logical addr. We need physical disk addr. */
449554b6e9e9SScott Teel 	if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
449654b6e9e9SScott Teel 		dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
449754b6e9e9SScott Teel 		return -1; /* not abortable */
449854b6e9e9SScott Teel 	}
449954b6e9e9SScott Teel 
450054b6e9e9SScott Teel 	/* send the reset */
45012ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
45022ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
45032ba8bfc8SStephen M. Cameron 			"Reset as abort: Resetting physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
45042ba8bfc8SStephen M. Cameron 			psa[0], psa[1], psa[2], psa[3],
45052ba8bfc8SStephen M. Cameron 			psa[4], psa[5], psa[6], psa[7]);
450654b6e9e9SScott Teel 	rc = hpsa_send_reset(h, psa, HPSA_RESET_TYPE_TARGET);
450754b6e9e9SScott Teel 	if (rc != 0) {
450854b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
450954b6e9e9SScott Teel 			"Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
451054b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
451154b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
451254b6e9e9SScott Teel 		return rc; /* failed to reset */
451354b6e9e9SScott Teel 	}
451454b6e9e9SScott Teel 
451554b6e9e9SScott Teel 	/* wait for device to recover */
451654b6e9e9SScott Teel 	if (wait_for_device_to_become_ready(h, psa) != 0) {
451754b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
451854b6e9e9SScott Teel 			"Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
451954b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
452054b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
452154b6e9e9SScott Teel 		return -1;  /* failed to recover */
452254b6e9e9SScott Teel 	}
452354b6e9e9SScott Teel 
452454b6e9e9SScott Teel 	/* device recovered */
452554b6e9e9SScott Teel 	dev_info(&h->pdev->dev,
452654b6e9e9SScott Teel 		"Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
452754b6e9e9SScott Teel 		psa[0], psa[1], psa[2], psa[3],
452854b6e9e9SScott Teel 		psa[4], psa[5], psa[6], psa[7]);
452954b6e9e9SScott Teel 
453054b6e9e9SScott Teel 	return rc; /* success */
453154b6e9e9SScott Teel }
453254b6e9e9SScott Teel 
45336cba3f19SStephen M. Cameron /* Some Smart Arrays need the abort tag swizzled, and some don't.  It's hard to
45346cba3f19SStephen M. Cameron  * tell which kind we're dealing with, so we send the abort both ways.  There
45356cba3f19SStephen M. Cameron  * shouldn't be any collisions between swizzled and unswizzled tags due to the
45366cba3f19SStephen M. Cameron  * way we construct our tags but we check anyway in case the assumptions which
45376cba3f19SStephen M. Cameron  * make this true someday become false.
45386cba3f19SStephen M. Cameron  */
45396cba3f19SStephen M. Cameron static int hpsa_send_abort_both_ways(struct ctlr_info *h,
45406cba3f19SStephen M. Cameron 	unsigned char *scsi3addr, struct CommandList *abort)
45416cba3f19SStephen M. Cameron {
45426cba3f19SStephen M. Cameron 	u8 swizzled_tag[8];
45436cba3f19SStephen M. Cameron 	struct CommandList *c;
45446cba3f19SStephen M. Cameron 	int rc = 0, rc2 = 0;
45456cba3f19SStephen M. Cameron 
454654b6e9e9SScott Teel 	/* ioccelerator mode 2 commands should be aborted via the
454754b6e9e9SScott Teel 	 * accelerated path, since RAID path is unaware of these commands,
454854b6e9e9SScott Teel 	 * but underlying firmware can't handle abort TMF.
454954b6e9e9SScott Teel 	 * Change abort to physical device reset.
455054b6e9e9SScott Teel 	 */
455154b6e9e9SScott Teel 	if (abort->cmd_type == CMD_IOACCEL2)
455254b6e9e9SScott Teel 		return hpsa_send_reset_as_abort_ioaccel2(h, scsi3addr, abort);
455354b6e9e9SScott Teel 
45546cba3f19SStephen M. Cameron 	/* we do not expect to find the swizzled tag in our queue, but
45556cba3f19SStephen M. Cameron 	 * check anyway just to be sure the assumptions which make this
45566cba3f19SStephen M. Cameron 	 * the case haven't become wrong.
45576cba3f19SStephen M. Cameron 	 */
45586cba3f19SStephen M. Cameron 	memcpy(swizzled_tag, &abort->Request.CDB[4], 8);
45596cba3f19SStephen M. Cameron 	swizzle_abort_tag(swizzled_tag);
45606cba3f19SStephen M. Cameron 	c = hpsa_find_cmd_in_queue_by_tag(h, swizzled_tag, &h->cmpQ);
45616cba3f19SStephen M. Cameron 	if (c != NULL) {
45626cba3f19SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Unexpectedly found byte-swapped tag in completion queue.\n");
45636cba3f19SStephen M. Cameron 		return hpsa_send_abort(h, scsi3addr, abort, 0);
45646cba3f19SStephen M. Cameron 	}
45656cba3f19SStephen M. Cameron 	rc = hpsa_send_abort(h, scsi3addr, abort, 0);
45666cba3f19SStephen M. Cameron 
45676cba3f19SStephen M. Cameron 	/* if the command is still in our queue, we can't conclude that it was
45686cba3f19SStephen M. Cameron 	 * aborted (it might have just completed normally) but in any case
45696cba3f19SStephen M. Cameron 	 * we don't need to try to abort it another way.
45706cba3f19SStephen M. Cameron 	 */
45716cba3f19SStephen M. Cameron 	c = hpsa_find_cmd_in_queue(h, abort->scsi_cmd, &h->cmpQ);
45726cba3f19SStephen M. Cameron 	if (c)
45736cba3f19SStephen M. Cameron 		rc2 = hpsa_send_abort(h, scsi3addr, abort, 1);
45746cba3f19SStephen M. Cameron 	return rc && rc2;
45756cba3f19SStephen M. Cameron }
45766cba3f19SStephen M. Cameron 
457775167d2cSStephen M. Cameron /* Send an abort for the specified command.
457875167d2cSStephen M. Cameron  *	If the device and controller support it,
457975167d2cSStephen M. Cameron  *		send a task abort request.
458075167d2cSStephen M. Cameron  */
458175167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
458275167d2cSStephen M. Cameron {
458375167d2cSStephen M. Cameron 
458475167d2cSStephen M. Cameron 	int i, rc;
458575167d2cSStephen M. Cameron 	struct ctlr_info *h;
458675167d2cSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
458775167d2cSStephen M. Cameron 	struct CommandList *abort; /* pointer to command to be aborted */
458875167d2cSStephen M. Cameron 	struct CommandList *found;
458975167d2cSStephen M. Cameron 	struct scsi_cmnd *as;	/* ptr to scsi cmd inside aborted command. */
459075167d2cSStephen M. Cameron 	char msg[256];		/* For debug messaging. */
459175167d2cSStephen M. Cameron 	int ml = 0;
459217eb87d2SScott Teel 	u32 tagupper, taglower;
459375167d2cSStephen M. Cameron 
459475167d2cSStephen M. Cameron 	/* Find the controller of the command to be aborted */
459575167d2cSStephen M. Cameron 	h = sdev_to_hba(sc->device);
459675167d2cSStephen M. Cameron 	if (WARN(h == NULL,
459775167d2cSStephen M. Cameron 			"ABORT REQUEST FAILED, Controller lookup failed.\n"))
459875167d2cSStephen M. Cameron 		return FAILED;
459975167d2cSStephen M. Cameron 
460075167d2cSStephen M. Cameron 	/* Check that controller supports some kind of task abort */
460175167d2cSStephen M. Cameron 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
460275167d2cSStephen M. Cameron 		!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
460375167d2cSStephen M. Cameron 		return FAILED;
460475167d2cSStephen M. Cameron 
460575167d2cSStephen M. Cameron 	memset(msg, 0, sizeof(msg));
460675167d2cSStephen M. Cameron 	ml += sprintf(msg+ml, "ABORT REQUEST on C%d:B%d:T%d:L%d ",
460775167d2cSStephen M. Cameron 		h->scsi_host->host_no, sc->device->channel,
460875167d2cSStephen M. Cameron 		sc->device->id, sc->device->lun);
460975167d2cSStephen M. Cameron 
461075167d2cSStephen M. Cameron 	/* Find the device of the command to be aborted */
461175167d2cSStephen M. Cameron 	dev = sc->device->hostdata;
461275167d2cSStephen M. Cameron 	if (!dev) {
461375167d2cSStephen M. Cameron 		dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
461475167d2cSStephen M. Cameron 				msg);
461575167d2cSStephen M. Cameron 		return FAILED;
461675167d2cSStephen M. Cameron 	}
461775167d2cSStephen M. Cameron 
461875167d2cSStephen M. Cameron 	/* Get SCSI command to be aborted */
461975167d2cSStephen M. Cameron 	abort = (struct CommandList *) sc->host_scribble;
462075167d2cSStephen M. Cameron 	if (abort == NULL) {
462175167d2cSStephen M. Cameron 		dev_err(&h->pdev->dev, "%s FAILED, Command to abort is NULL.\n",
462275167d2cSStephen M. Cameron 				msg);
462375167d2cSStephen M. Cameron 		return FAILED;
462475167d2cSStephen M. Cameron 	}
462517eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
462617eb87d2SScott Teel 	ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
462775167d2cSStephen M. Cameron 	as  = (struct scsi_cmnd *) abort->scsi_cmd;
462875167d2cSStephen M. Cameron 	if (as != NULL)
462975167d2cSStephen M. Cameron 		ml += sprintf(msg+ml, "Command:0x%x SN:0x%lx ",
463075167d2cSStephen M. Cameron 			as->cmnd[0], as->serial_number);
463175167d2cSStephen M. Cameron 	dev_dbg(&h->pdev->dev, "%s\n", msg);
463275167d2cSStephen M. Cameron 	dev_warn(&h->pdev->dev, "Abort request on C%d:B%d:T%d:L%d\n",
463375167d2cSStephen M. Cameron 		h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
463475167d2cSStephen M. Cameron 
463575167d2cSStephen M. Cameron 	/* Search reqQ to See if command is queued but not submitted,
463675167d2cSStephen M. Cameron 	 * if so, complete the command with aborted status and remove
463775167d2cSStephen M. Cameron 	 * it from the reqQ.
463875167d2cSStephen M. Cameron 	 */
463975167d2cSStephen M. Cameron 	found = hpsa_find_cmd_in_queue(h, sc, &h->reqQ);
464075167d2cSStephen M. Cameron 	if (found) {
464175167d2cSStephen M. Cameron 		found->err_info->CommandStatus = CMD_ABORTED;
464275167d2cSStephen M. Cameron 		finish_cmd(found);
464375167d2cSStephen M. Cameron 		dev_info(&h->pdev->dev, "%s Request SUCCEEDED (driver queue).\n",
464475167d2cSStephen M. Cameron 				msg);
464575167d2cSStephen M. Cameron 		return SUCCESS;
464675167d2cSStephen M. Cameron 	}
464775167d2cSStephen M. Cameron 
464875167d2cSStephen M. Cameron 	/* not in reqQ, if also not in cmpQ, must have already completed */
464975167d2cSStephen M. Cameron 	found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
465075167d2cSStephen M. Cameron 	if (!found)  {
4651d6ebd0f7SStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s Request SUCCEEDED (not known to driver).\n",
465275167d2cSStephen M. Cameron 				msg);
465375167d2cSStephen M. Cameron 		return SUCCESS;
465475167d2cSStephen M. Cameron 	}
465575167d2cSStephen M. Cameron 
465675167d2cSStephen M. Cameron 	/*
465775167d2cSStephen M. Cameron 	 * Command is in flight, or possibly already completed
465875167d2cSStephen M. Cameron 	 * by the firmware (but not to the scsi mid layer) but we can't
465975167d2cSStephen M. Cameron 	 * distinguish which.  Send the abort down.
466075167d2cSStephen M. Cameron 	 */
46616cba3f19SStephen M. Cameron 	rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort);
466275167d2cSStephen M. Cameron 	if (rc != 0) {
466375167d2cSStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s Request FAILED.\n", msg);
466475167d2cSStephen M. Cameron 		dev_warn(&h->pdev->dev, "FAILED abort on device C%d:B%d:T%d:L%d\n",
466575167d2cSStephen M. Cameron 			h->scsi_host->host_no,
466675167d2cSStephen M. Cameron 			dev->bus, dev->target, dev->lun);
466775167d2cSStephen M. Cameron 		return FAILED;
466875167d2cSStephen M. Cameron 	}
466975167d2cSStephen M. Cameron 	dev_info(&h->pdev->dev, "%s REQUEST SUCCEEDED.\n", msg);
467075167d2cSStephen M. Cameron 
467175167d2cSStephen M. Cameron 	/* If the abort(s) above completed and actually aborted the
467275167d2cSStephen M. Cameron 	 * command, then the command to be aborted should already be
467375167d2cSStephen M. Cameron 	 * completed.  If not, wait around a bit more to see if they
467475167d2cSStephen M. Cameron 	 * manage to complete normally.
467575167d2cSStephen M. Cameron 	 */
467675167d2cSStephen M. Cameron #define ABORT_COMPLETE_WAIT_SECS 30
467775167d2cSStephen M. Cameron 	for (i = 0; i < ABORT_COMPLETE_WAIT_SECS * 10; i++) {
467875167d2cSStephen M. Cameron 		found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
467975167d2cSStephen M. Cameron 		if (!found)
468075167d2cSStephen M. Cameron 			return SUCCESS;
468175167d2cSStephen M. Cameron 		msleep(100);
468275167d2cSStephen M. Cameron 	}
468375167d2cSStephen M. Cameron 	dev_warn(&h->pdev->dev, "%s FAILED. Aborted command has not completed after %d seconds.\n",
468475167d2cSStephen M. Cameron 		msg, ABORT_COMPLETE_WAIT_SECS);
468575167d2cSStephen M. Cameron 	return FAILED;
468675167d2cSStephen M. Cameron }
468775167d2cSStephen M. Cameron 
468875167d2cSStephen M. Cameron 
4689edd16368SStephen M. Cameron /*
4690edd16368SStephen M. Cameron  * For operations that cannot sleep, a command block is allocated at init,
4691edd16368SStephen M. Cameron  * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
4692edd16368SStephen M. Cameron  * which ones are free or in use.  Lock must be held when calling this.
4693edd16368SStephen M. Cameron  * cmd_free() is the complement.
4694edd16368SStephen M. Cameron  */
4695edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h)
4696edd16368SStephen M. Cameron {
4697edd16368SStephen M. Cameron 	struct CommandList *c;
4698edd16368SStephen M. Cameron 	int i;
4699edd16368SStephen M. Cameron 	union u64bit temp64;
4700edd16368SStephen M. Cameron 	dma_addr_t cmd_dma_handle, err_dma_handle;
4701e16a33adSMatt Gates 	unsigned long flags;
4702edd16368SStephen M. Cameron 
4703e16a33adSMatt Gates 	spin_lock_irqsave(&h->lock, flags);
4704edd16368SStephen M. Cameron 	do {
4705edd16368SStephen M. Cameron 		i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
4706e16a33adSMatt Gates 		if (i == h->nr_cmds) {
4707e16a33adSMatt Gates 			spin_unlock_irqrestore(&h->lock, flags);
4708edd16368SStephen M. Cameron 			return NULL;
4709e16a33adSMatt Gates 		}
4710edd16368SStephen M. Cameron 	} while (test_and_set_bit
4711edd16368SStephen M. Cameron 		 (i & (BITS_PER_LONG - 1),
4712edd16368SStephen M. Cameron 		  h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
4713e16a33adSMatt Gates 	spin_unlock_irqrestore(&h->lock, flags);
4714e16a33adSMatt Gates 
4715edd16368SStephen M. Cameron 	c = h->cmd_pool + i;
4716edd16368SStephen M. Cameron 	memset(c, 0, sizeof(*c));
4717edd16368SStephen M. Cameron 	cmd_dma_handle = h->cmd_pool_dhandle
4718edd16368SStephen M. Cameron 	    + i * sizeof(*c);
4719edd16368SStephen M. Cameron 	c->err_info = h->errinfo_pool + i;
4720edd16368SStephen M. Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
4721edd16368SStephen M. Cameron 	err_dma_handle = h->errinfo_pool_dhandle
4722edd16368SStephen M. Cameron 	    + i * sizeof(*c->err_info);
4723edd16368SStephen M. Cameron 
4724edd16368SStephen M. Cameron 	c->cmdindex = i;
4725edd16368SStephen M. Cameron 
47269e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&c->list);
472701a02ffcSStephen M. Cameron 	c->busaddr = (u32) cmd_dma_handle;
472801a02ffcSStephen M. Cameron 	temp64.val = (u64) err_dma_handle;
4729edd16368SStephen M. Cameron 	c->ErrDesc.Addr.lower = temp64.val32.lower;
4730edd16368SStephen M. Cameron 	c->ErrDesc.Addr.upper = temp64.val32.upper;
4731edd16368SStephen M. Cameron 	c->ErrDesc.Len = sizeof(*c->err_info);
4732edd16368SStephen M. Cameron 
4733edd16368SStephen M. Cameron 	c->h = h;
4734edd16368SStephen M. Cameron 	return c;
4735edd16368SStephen M. Cameron }
4736edd16368SStephen M. Cameron 
4737edd16368SStephen M. Cameron /* For operations that can wait for kmalloc to possibly sleep,
4738edd16368SStephen M. Cameron  * this routine can be called. Lock need not be held to call
4739edd16368SStephen M. Cameron  * cmd_special_alloc. cmd_special_free() is the complement.
4740edd16368SStephen M. Cameron  */
4741edd16368SStephen M. Cameron static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
4742edd16368SStephen M. Cameron {
4743edd16368SStephen M. Cameron 	struct CommandList *c;
4744edd16368SStephen M. Cameron 	union u64bit temp64;
4745edd16368SStephen M. Cameron 	dma_addr_t cmd_dma_handle, err_dma_handle;
4746edd16368SStephen M. Cameron 
4747edd16368SStephen M. Cameron 	c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
4748edd16368SStephen M. Cameron 	if (c == NULL)
4749edd16368SStephen M. Cameron 		return NULL;
4750edd16368SStephen M. Cameron 	memset(c, 0, sizeof(*c));
4751edd16368SStephen M. Cameron 
4752e1f7de0cSMatt Gates 	c->cmd_type = CMD_SCSI;
4753edd16368SStephen M. Cameron 	c->cmdindex = -1;
4754edd16368SStephen M. Cameron 
4755edd16368SStephen M. Cameron 	c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info),
4756edd16368SStephen M. Cameron 		    &err_dma_handle);
4757edd16368SStephen M. Cameron 
4758edd16368SStephen M. Cameron 	if (c->err_info == NULL) {
4759edd16368SStephen M. Cameron 		pci_free_consistent(h->pdev,
4760edd16368SStephen M. Cameron 			sizeof(*c), c, cmd_dma_handle);
4761edd16368SStephen M. Cameron 		return NULL;
4762edd16368SStephen M. Cameron 	}
4763edd16368SStephen M. Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
4764edd16368SStephen M. Cameron 
47659e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&c->list);
476601a02ffcSStephen M. Cameron 	c->busaddr = (u32) cmd_dma_handle;
476701a02ffcSStephen M. Cameron 	temp64.val = (u64) err_dma_handle;
4768edd16368SStephen M. Cameron 	c->ErrDesc.Addr.lower = temp64.val32.lower;
4769edd16368SStephen M. Cameron 	c->ErrDesc.Addr.upper = temp64.val32.upper;
4770edd16368SStephen M. Cameron 	c->ErrDesc.Len = sizeof(*c->err_info);
4771edd16368SStephen M. Cameron 
4772edd16368SStephen M. Cameron 	c->h = h;
4773edd16368SStephen M. Cameron 	return c;
4774edd16368SStephen M. Cameron }
4775edd16368SStephen M. Cameron 
4776edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c)
4777edd16368SStephen M. Cameron {
4778edd16368SStephen M. Cameron 	int i;
4779e16a33adSMatt Gates 	unsigned long flags;
4780edd16368SStephen M. Cameron 
4781edd16368SStephen M. Cameron 	i = c - h->cmd_pool;
4782e16a33adSMatt Gates 	spin_lock_irqsave(&h->lock, flags);
4783edd16368SStephen M. Cameron 	clear_bit(i & (BITS_PER_LONG - 1),
4784edd16368SStephen M. Cameron 		  h->cmd_pool_bits + (i / BITS_PER_LONG));
4785e16a33adSMatt Gates 	spin_unlock_irqrestore(&h->lock, flags);
4786edd16368SStephen M. Cameron }
4787edd16368SStephen M. Cameron 
4788edd16368SStephen M. Cameron static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
4789edd16368SStephen M. Cameron {
4790edd16368SStephen M. Cameron 	union u64bit temp64;
4791edd16368SStephen M. Cameron 
4792edd16368SStephen M. Cameron 	temp64.val32.lower = c->ErrDesc.Addr.lower;
4793edd16368SStephen M. Cameron 	temp64.val32.upper = c->ErrDesc.Addr.upper;
4794edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev, sizeof(*c->err_info),
4795edd16368SStephen M. Cameron 			    c->err_info, (dma_addr_t) temp64.val);
4796edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev, sizeof(*c),
4797d896f3f3SStephen M. Cameron 			    c, (dma_addr_t) (c->busaddr & DIRECT_LOOKUP_MASK));
4798edd16368SStephen M. Cameron }
4799edd16368SStephen M. Cameron 
4800edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
4801edd16368SStephen M. Cameron 
4802edd16368SStephen M. Cameron static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
4803edd16368SStephen M. Cameron {
4804edd16368SStephen M. Cameron 	IOCTL32_Command_struct __user *arg32 =
4805edd16368SStephen M. Cameron 	    (IOCTL32_Command_struct __user *) arg;
4806edd16368SStephen M. Cameron 	IOCTL_Command_struct arg64;
4807edd16368SStephen M. Cameron 	IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
4808edd16368SStephen M. Cameron 	int err;
4809edd16368SStephen M. Cameron 	u32 cp;
4810edd16368SStephen M. Cameron 
4811938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
4812edd16368SStephen M. Cameron 	err = 0;
4813edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
4814edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
4815edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
4816edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
4817edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
4818edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
4819edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
4820edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
4821edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
4822edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
4823edd16368SStephen M. Cameron 
4824edd16368SStephen M. Cameron 	if (err)
4825edd16368SStephen M. Cameron 		return -EFAULT;
4826edd16368SStephen M. Cameron 
4827e39eeaedSStephen M. Cameron 	err = hpsa_ioctl(dev, CCISS_PASSTHRU, (void *)p);
4828edd16368SStephen M. Cameron 	if (err)
4829edd16368SStephen M. Cameron 		return err;
4830edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
4831edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
4832edd16368SStephen M. Cameron 	if (err)
4833edd16368SStephen M. Cameron 		return -EFAULT;
4834edd16368SStephen M. Cameron 	return err;
4835edd16368SStephen M. Cameron }
4836edd16368SStephen M. Cameron 
4837edd16368SStephen M. Cameron static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
4838edd16368SStephen M. Cameron 	int cmd, void *arg)
4839edd16368SStephen M. Cameron {
4840edd16368SStephen M. Cameron 	BIG_IOCTL32_Command_struct __user *arg32 =
4841edd16368SStephen M. Cameron 	    (BIG_IOCTL32_Command_struct __user *) arg;
4842edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct arg64;
4843edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct __user *p =
4844edd16368SStephen M. Cameron 	    compat_alloc_user_space(sizeof(arg64));
4845edd16368SStephen M. Cameron 	int err;
4846edd16368SStephen M. Cameron 	u32 cp;
4847edd16368SStephen M. Cameron 
4848938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
4849edd16368SStephen M. Cameron 	err = 0;
4850edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
4851edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
4852edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
4853edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
4854edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
4855edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
4856edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
4857edd16368SStephen M. Cameron 	err |= get_user(arg64.malloc_size, &arg32->malloc_size);
4858edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
4859edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
4860edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
4861edd16368SStephen M. Cameron 
4862edd16368SStephen M. Cameron 	if (err)
4863edd16368SStephen M. Cameron 		return -EFAULT;
4864edd16368SStephen M. Cameron 
4865e39eeaedSStephen M. Cameron 	err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
4866edd16368SStephen M. Cameron 	if (err)
4867edd16368SStephen M. Cameron 		return err;
4868edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
4869edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
4870edd16368SStephen M. Cameron 	if (err)
4871edd16368SStephen M. Cameron 		return -EFAULT;
4872edd16368SStephen M. Cameron 	return err;
4873edd16368SStephen M. Cameron }
487471fe75a7SStephen M. Cameron 
487571fe75a7SStephen M. Cameron static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
487671fe75a7SStephen M. Cameron {
487771fe75a7SStephen M. Cameron 	switch (cmd) {
487871fe75a7SStephen M. Cameron 	case CCISS_GETPCIINFO:
487971fe75a7SStephen M. Cameron 	case CCISS_GETINTINFO:
488071fe75a7SStephen M. Cameron 	case CCISS_SETINTINFO:
488171fe75a7SStephen M. Cameron 	case CCISS_GETNODENAME:
488271fe75a7SStephen M. Cameron 	case CCISS_SETNODENAME:
488371fe75a7SStephen M. Cameron 	case CCISS_GETHEARTBEAT:
488471fe75a7SStephen M. Cameron 	case CCISS_GETBUSTYPES:
488571fe75a7SStephen M. Cameron 	case CCISS_GETFIRMVER:
488671fe75a7SStephen M. Cameron 	case CCISS_GETDRIVVER:
488771fe75a7SStephen M. Cameron 	case CCISS_REVALIDVOLS:
488871fe75a7SStephen M. Cameron 	case CCISS_DEREGDISK:
488971fe75a7SStephen M. Cameron 	case CCISS_REGNEWDISK:
489071fe75a7SStephen M. Cameron 	case CCISS_REGNEWD:
489171fe75a7SStephen M. Cameron 	case CCISS_RESCANDISK:
489271fe75a7SStephen M. Cameron 	case CCISS_GETLUNINFO:
489371fe75a7SStephen M. Cameron 		return hpsa_ioctl(dev, cmd, arg);
489471fe75a7SStephen M. Cameron 
489571fe75a7SStephen M. Cameron 	case CCISS_PASSTHRU32:
489671fe75a7SStephen M. Cameron 		return hpsa_ioctl32_passthru(dev, cmd, arg);
489771fe75a7SStephen M. Cameron 	case CCISS_BIG_PASSTHRU32:
489871fe75a7SStephen M. Cameron 		return hpsa_ioctl32_big_passthru(dev, cmd, arg);
489971fe75a7SStephen M. Cameron 
490071fe75a7SStephen M. Cameron 	default:
490171fe75a7SStephen M. Cameron 		return -ENOIOCTLCMD;
490271fe75a7SStephen M. Cameron 	}
490371fe75a7SStephen M. Cameron }
4904edd16368SStephen M. Cameron #endif
4905edd16368SStephen M. Cameron 
4906edd16368SStephen M. Cameron static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
4907edd16368SStephen M. Cameron {
4908edd16368SStephen M. Cameron 	struct hpsa_pci_info pciinfo;
4909edd16368SStephen M. Cameron 
4910edd16368SStephen M. Cameron 	if (!argp)
4911edd16368SStephen M. Cameron 		return -EINVAL;
4912edd16368SStephen M. Cameron 	pciinfo.domain = pci_domain_nr(h->pdev->bus);
4913edd16368SStephen M. Cameron 	pciinfo.bus = h->pdev->bus->number;
4914edd16368SStephen M. Cameron 	pciinfo.dev_fn = h->pdev->devfn;
4915edd16368SStephen M. Cameron 	pciinfo.board_id = h->board_id;
4916edd16368SStephen M. Cameron 	if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
4917edd16368SStephen M. Cameron 		return -EFAULT;
4918edd16368SStephen M. Cameron 	return 0;
4919edd16368SStephen M. Cameron }
4920edd16368SStephen M. Cameron 
4921edd16368SStephen M. Cameron static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
4922edd16368SStephen M. Cameron {
4923edd16368SStephen M. Cameron 	DriverVer_type DriverVer;
4924edd16368SStephen M. Cameron 	unsigned char vmaj, vmin, vsubmin;
4925edd16368SStephen M. Cameron 	int rc;
4926edd16368SStephen M. Cameron 
4927edd16368SStephen M. Cameron 	rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
4928edd16368SStephen M. Cameron 		&vmaj, &vmin, &vsubmin);
4929edd16368SStephen M. Cameron 	if (rc != 3) {
4930edd16368SStephen M. Cameron 		dev_info(&h->pdev->dev, "driver version string '%s' "
4931edd16368SStephen M. Cameron 			"unrecognized.", HPSA_DRIVER_VERSION);
4932edd16368SStephen M. Cameron 		vmaj = 0;
4933edd16368SStephen M. Cameron 		vmin = 0;
4934edd16368SStephen M. Cameron 		vsubmin = 0;
4935edd16368SStephen M. Cameron 	}
4936edd16368SStephen M. Cameron 	DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
4937edd16368SStephen M. Cameron 	if (!argp)
4938edd16368SStephen M. Cameron 		return -EINVAL;
4939edd16368SStephen M. Cameron 	if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
4940edd16368SStephen M. Cameron 		return -EFAULT;
4941edd16368SStephen M. Cameron 	return 0;
4942edd16368SStephen M. Cameron }
4943edd16368SStephen M. Cameron 
4944edd16368SStephen M. Cameron static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
4945edd16368SStephen M. Cameron {
4946edd16368SStephen M. Cameron 	IOCTL_Command_struct iocommand;
4947edd16368SStephen M. Cameron 	struct CommandList *c;
4948edd16368SStephen M. Cameron 	char *buff = NULL;
4949edd16368SStephen M. Cameron 	union u64bit temp64;
4950c1f63c8fSStephen M. Cameron 	int rc = 0;
4951edd16368SStephen M. Cameron 
4952edd16368SStephen M. Cameron 	if (!argp)
4953edd16368SStephen M. Cameron 		return -EINVAL;
4954edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
4955edd16368SStephen M. Cameron 		return -EPERM;
4956edd16368SStephen M. Cameron 	if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
4957edd16368SStephen M. Cameron 		return -EFAULT;
4958edd16368SStephen M. Cameron 	if ((iocommand.buf_size < 1) &&
4959edd16368SStephen M. Cameron 	    (iocommand.Request.Type.Direction != XFER_NONE)) {
4960edd16368SStephen M. Cameron 		return -EINVAL;
4961edd16368SStephen M. Cameron 	}
4962edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
4963edd16368SStephen M. Cameron 		buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
4964edd16368SStephen M. Cameron 		if (buff == NULL)
4965edd16368SStephen M. Cameron 			return -EFAULT;
4966*9233fb10SStephen M. Cameron 		if (iocommand.Request.Type.Direction & XFER_WRITE) {
4967edd16368SStephen M. Cameron 			/* Copy the data into the buffer we created */
4968b03a7771SStephen M. Cameron 			if (copy_from_user(buff, iocommand.buf,
4969b03a7771SStephen M. Cameron 				iocommand.buf_size)) {
4970c1f63c8fSStephen M. Cameron 				rc = -EFAULT;
4971c1f63c8fSStephen M. Cameron 				goto out_kfree;
4972edd16368SStephen M. Cameron 			}
4973b03a7771SStephen M. Cameron 		} else {
4974edd16368SStephen M. Cameron 			memset(buff, 0, iocommand.buf_size);
4975b03a7771SStephen M. Cameron 		}
4976b03a7771SStephen M. Cameron 	}
4977edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
4978edd16368SStephen M. Cameron 	if (c == NULL) {
4979c1f63c8fSStephen M. Cameron 		rc = -ENOMEM;
4980c1f63c8fSStephen M. Cameron 		goto out_kfree;
4981edd16368SStephen M. Cameron 	}
4982edd16368SStephen M. Cameron 	/* Fill in the command type */
4983edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
4984edd16368SStephen M. Cameron 	/* Fill in Command Header */
4985edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0; /* unused in simple mode */
4986edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {	/* buffer to fill */
4987edd16368SStephen M. Cameron 		c->Header.SGList = 1;
4988edd16368SStephen M. Cameron 		c->Header.SGTotal = 1;
4989edd16368SStephen M. Cameron 	} else	{ /* no buffers to fill */
4990edd16368SStephen M. Cameron 		c->Header.SGList = 0;
4991edd16368SStephen M. Cameron 		c->Header.SGTotal = 0;
4992edd16368SStephen M. Cameron 	}
4993edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
4994edd16368SStephen M. Cameron 	/* use the kernel address the cmd block for tag */
4995edd16368SStephen M. Cameron 	c->Header.Tag.lower = c->busaddr;
4996edd16368SStephen M. Cameron 
4997edd16368SStephen M. Cameron 	/* Fill in Request block */
4998edd16368SStephen M. Cameron 	memcpy(&c->Request, &iocommand.Request,
4999edd16368SStephen M. Cameron 		sizeof(c->Request));
5000edd16368SStephen M. Cameron 
5001edd16368SStephen M. Cameron 	/* Fill in the scatter gather information */
5002edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
5003edd16368SStephen M. Cameron 		temp64.val = pci_map_single(h->pdev, buff,
5004edd16368SStephen M. Cameron 			iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
5005bcc48ffaSStephen M. Cameron 		if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
5006bcc48ffaSStephen M. Cameron 			c->SG[0].Addr.lower = 0;
5007bcc48ffaSStephen M. Cameron 			c->SG[0].Addr.upper = 0;
5008bcc48ffaSStephen M. Cameron 			c->SG[0].Len = 0;
5009bcc48ffaSStephen M. Cameron 			rc = -ENOMEM;
5010bcc48ffaSStephen M. Cameron 			goto out;
5011bcc48ffaSStephen M. Cameron 		}
5012edd16368SStephen M. Cameron 		c->SG[0].Addr.lower = temp64.val32.lower;
5013edd16368SStephen M. Cameron 		c->SG[0].Addr.upper = temp64.val32.upper;
5014edd16368SStephen M. Cameron 		c->SG[0].Len = iocommand.buf_size;
5015e1d9cbfaSMatt Gates 		c->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining*/
5016edd16368SStephen M. Cameron 	}
5017a0c12413SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
5018c2dd32e0SStephen M. Cameron 	if (iocommand.buf_size > 0)
5019edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
5020edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
5021edd16368SStephen M. Cameron 
5022edd16368SStephen M. Cameron 	/* Copy the error information out */
5023edd16368SStephen M. Cameron 	memcpy(&iocommand.error_info, c->err_info,
5024edd16368SStephen M. Cameron 		sizeof(iocommand.error_info));
5025edd16368SStephen M. Cameron 	if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
5026c1f63c8fSStephen M. Cameron 		rc = -EFAULT;
5027c1f63c8fSStephen M. Cameron 		goto out;
5028edd16368SStephen M. Cameron 	}
5029*9233fb10SStephen M. Cameron 	if ((iocommand.Request.Type.Direction & XFER_READ) &&
5030b03a7771SStephen M. Cameron 		iocommand.buf_size > 0) {
5031edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
5032edd16368SStephen M. Cameron 		if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
5033c1f63c8fSStephen M. Cameron 			rc = -EFAULT;
5034c1f63c8fSStephen M. Cameron 			goto out;
5035edd16368SStephen M. Cameron 		}
5036edd16368SStephen M. Cameron 	}
5037c1f63c8fSStephen M. Cameron out:
5038edd16368SStephen M. Cameron 	cmd_special_free(h, c);
5039c1f63c8fSStephen M. Cameron out_kfree:
5040c1f63c8fSStephen M. Cameron 	kfree(buff);
5041c1f63c8fSStephen M. Cameron 	return rc;
5042edd16368SStephen M. Cameron }
5043edd16368SStephen M. Cameron 
5044edd16368SStephen M. Cameron static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
5045edd16368SStephen M. Cameron {
5046edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct *ioc;
5047edd16368SStephen M. Cameron 	struct CommandList *c;
5048edd16368SStephen M. Cameron 	unsigned char **buff = NULL;
5049edd16368SStephen M. Cameron 	int *buff_size = NULL;
5050edd16368SStephen M. Cameron 	union u64bit temp64;
5051edd16368SStephen M. Cameron 	BYTE sg_used = 0;
5052edd16368SStephen M. Cameron 	int status = 0;
5053edd16368SStephen M. Cameron 	int i;
505401a02ffcSStephen M. Cameron 	u32 left;
505501a02ffcSStephen M. Cameron 	u32 sz;
5056edd16368SStephen M. Cameron 	BYTE __user *data_ptr;
5057edd16368SStephen M. Cameron 
5058edd16368SStephen M. Cameron 	if (!argp)
5059edd16368SStephen M. Cameron 		return -EINVAL;
5060edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
5061edd16368SStephen M. Cameron 		return -EPERM;
5062edd16368SStephen M. Cameron 	ioc = (BIG_IOCTL_Command_struct *)
5063edd16368SStephen M. Cameron 	    kmalloc(sizeof(*ioc), GFP_KERNEL);
5064edd16368SStephen M. Cameron 	if (!ioc) {
5065edd16368SStephen M. Cameron 		status = -ENOMEM;
5066edd16368SStephen M. Cameron 		goto cleanup1;
5067edd16368SStephen M. Cameron 	}
5068edd16368SStephen M. Cameron 	if (copy_from_user(ioc, argp, sizeof(*ioc))) {
5069edd16368SStephen M. Cameron 		status = -EFAULT;
5070edd16368SStephen M. Cameron 		goto cleanup1;
5071edd16368SStephen M. Cameron 	}
5072edd16368SStephen M. Cameron 	if ((ioc->buf_size < 1) &&
5073edd16368SStephen M. Cameron 	    (ioc->Request.Type.Direction != XFER_NONE)) {
5074edd16368SStephen M. Cameron 		status = -EINVAL;
5075edd16368SStephen M. Cameron 		goto cleanup1;
5076edd16368SStephen M. Cameron 	}
5077edd16368SStephen M. Cameron 	/* Check kmalloc limits  using all SGs */
5078edd16368SStephen M. Cameron 	if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
5079edd16368SStephen M. Cameron 		status = -EINVAL;
5080edd16368SStephen M. Cameron 		goto cleanup1;
5081edd16368SStephen M. Cameron 	}
5082d66ae08bSStephen M. Cameron 	if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
5083edd16368SStephen M. Cameron 		status = -EINVAL;
5084edd16368SStephen M. Cameron 		goto cleanup1;
5085edd16368SStephen M. Cameron 	}
5086d66ae08bSStephen M. Cameron 	buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
5087edd16368SStephen M. Cameron 	if (!buff) {
5088edd16368SStephen M. Cameron 		status = -ENOMEM;
5089edd16368SStephen M. Cameron 		goto cleanup1;
5090edd16368SStephen M. Cameron 	}
5091d66ae08bSStephen M. Cameron 	buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
5092edd16368SStephen M. Cameron 	if (!buff_size) {
5093edd16368SStephen M. Cameron 		status = -ENOMEM;
5094edd16368SStephen M. Cameron 		goto cleanup1;
5095edd16368SStephen M. Cameron 	}
5096edd16368SStephen M. Cameron 	left = ioc->buf_size;
5097edd16368SStephen M. Cameron 	data_ptr = ioc->buf;
5098edd16368SStephen M. Cameron 	while (left) {
5099edd16368SStephen M. Cameron 		sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
5100edd16368SStephen M. Cameron 		buff_size[sg_used] = sz;
5101edd16368SStephen M. Cameron 		buff[sg_used] = kmalloc(sz, GFP_KERNEL);
5102edd16368SStephen M. Cameron 		if (buff[sg_used] == NULL) {
5103edd16368SStephen M. Cameron 			status = -ENOMEM;
5104edd16368SStephen M. Cameron 			goto cleanup1;
5105edd16368SStephen M. Cameron 		}
5106*9233fb10SStephen M. Cameron 		if (ioc->Request.Type.Direction & XFER_WRITE) {
5107edd16368SStephen M. Cameron 			if (copy_from_user(buff[sg_used], data_ptr, sz)) {
5108edd16368SStephen M. Cameron 				status = -ENOMEM;
5109edd16368SStephen M. Cameron 				goto cleanup1;
5110edd16368SStephen M. Cameron 			}
5111edd16368SStephen M. Cameron 		} else
5112edd16368SStephen M. Cameron 			memset(buff[sg_used], 0, sz);
5113edd16368SStephen M. Cameron 		left -= sz;
5114edd16368SStephen M. Cameron 		data_ptr += sz;
5115edd16368SStephen M. Cameron 		sg_used++;
5116edd16368SStephen M. Cameron 	}
5117edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
5118edd16368SStephen M. Cameron 	if (c == NULL) {
5119edd16368SStephen M. Cameron 		status = -ENOMEM;
5120edd16368SStephen M. Cameron 		goto cleanup1;
5121edd16368SStephen M. Cameron 	}
5122edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5123edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
5124b03a7771SStephen M. Cameron 	c->Header.SGList = c->Header.SGTotal = sg_used;
5125edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
5126edd16368SStephen M. Cameron 	c->Header.Tag.lower = c->busaddr;
5127edd16368SStephen M. Cameron 	memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
5128edd16368SStephen M. Cameron 	if (ioc->buf_size > 0) {
5129edd16368SStephen M. Cameron 		int i;
5130edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
5131edd16368SStephen M. Cameron 			temp64.val = pci_map_single(h->pdev, buff[i],
5132edd16368SStephen M. Cameron 				    buff_size[i], PCI_DMA_BIDIRECTIONAL);
5133bcc48ffaSStephen M. Cameron 			if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
5134bcc48ffaSStephen M. Cameron 				c->SG[i].Addr.lower = 0;
5135bcc48ffaSStephen M. Cameron 				c->SG[i].Addr.upper = 0;
5136bcc48ffaSStephen M. Cameron 				c->SG[i].Len = 0;
5137bcc48ffaSStephen M. Cameron 				hpsa_pci_unmap(h->pdev, c, i,
5138bcc48ffaSStephen M. Cameron 					PCI_DMA_BIDIRECTIONAL);
5139bcc48ffaSStephen M. Cameron 				status = -ENOMEM;
5140e2d4a1f6SStephen M. Cameron 				goto cleanup0;
5141bcc48ffaSStephen M. Cameron 			}
5142edd16368SStephen M. Cameron 			c->SG[i].Addr.lower = temp64.val32.lower;
5143edd16368SStephen M. Cameron 			c->SG[i].Addr.upper = temp64.val32.upper;
5144edd16368SStephen M. Cameron 			c->SG[i].Len = buff_size[i];
5145e1d9cbfaSMatt Gates 			c->SG[i].Ext = i < sg_used - 1 ? 0 : HPSA_SG_LAST;
5146edd16368SStephen M. Cameron 		}
5147edd16368SStephen M. Cameron 	}
5148a0c12413SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
5149b03a7771SStephen M. Cameron 	if (sg_used)
5150edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
5151edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
5152edd16368SStephen M. Cameron 	/* Copy the error information out */
5153edd16368SStephen M. Cameron 	memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
5154edd16368SStephen M. Cameron 	if (copy_to_user(argp, ioc, sizeof(*ioc))) {
5155edd16368SStephen M. Cameron 		status = -EFAULT;
5156e2d4a1f6SStephen M. Cameron 		goto cleanup0;
5157edd16368SStephen M. Cameron 	}
5158*9233fb10SStephen M. Cameron 	if ((ioc->Request.Type.Direction & XFER_READ) && ioc->buf_size > 0) {
5159edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
5160edd16368SStephen M. Cameron 		BYTE __user *ptr = ioc->buf;
5161edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
5162edd16368SStephen M. Cameron 			if (copy_to_user(ptr, buff[i], buff_size[i])) {
5163edd16368SStephen M. Cameron 				status = -EFAULT;
5164e2d4a1f6SStephen M. Cameron 				goto cleanup0;
5165edd16368SStephen M. Cameron 			}
5166edd16368SStephen M. Cameron 			ptr += buff_size[i];
5167edd16368SStephen M. Cameron 		}
5168edd16368SStephen M. Cameron 	}
5169edd16368SStephen M. Cameron 	status = 0;
5170e2d4a1f6SStephen M. Cameron cleanup0:
5171e2d4a1f6SStephen M. Cameron 	cmd_special_free(h, c);
5172edd16368SStephen M. Cameron cleanup1:
5173edd16368SStephen M. Cameron 	if (buff) {
5174edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++)
5175edd16368SStephen M. Cameron 			kfree(buff[i]);
5176edd16368SStephen M. Cameron 		kfree(buff);
5177edd16368SStephen M. Cameron 	}
5178edd16368SStephen M. Cameron 	kfree(buff_size);
5179edd16368SStephen M. Cameron 	kfree(ioc);
5180edd16368SStephen M. Cameron 	return status;
5181edd16368SStephen M. Cameron }
5182edd16368SStephen M. Cameron 
5183edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
5184edd16368SStephen M. Cameron 	struct CommandList *c)
5185edd16368SStephen M. Cameron {
5186edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
5187edd16368SStephen M. Cameron 			c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
5188edd16368SStephen M. Cameron 		(void) check_for_unit_attention(h, c);
5189edd16368SStephen M. Cameron }
51900390f0c0SStephen M. Cameron 
51910390f0c0SStephen M. Cameron static int increment_passthru_count(struct ctlr_info *h)
51920390f0c0SStephen M. Cameron {
51930390f0c0SStephen M. Cameron 	unsigned long flags;
51940390f0c0SStephen M. Cameron 
51950390f0c0SStephen M. Cameron 	spin_lock_irqsave(&h->passthru_count_lock, flags);
51960390f0c0SStephen M. Cameron 	if (h->passthru_count >= HPSA_MAX_CONCURRENT_PASSTHRUS) {
51970390f0c0SStephen M. Cameron 		spin_unlock_irqrestore(&h->passthru_count_lock, flags);
51980390f0c0SStephen M. Cameron 		return -1;
51990390f0c0SStephen M. Cameron 	}
52000390f0c0SStephen M. Cameron 	h->passthru_count++;
52010390f0c0SStephen M. Cameron 	spin_unlock_irqrestore(&h->passthru_count_lock, flags);
52020390f0c0SStephen M. Cameron 	return 0;
52030390f0c0SStephen M. Cameron }
52040390f0c0SStephen M. Cameron 
52050390f0c0SStephen M. Cameron static void decrement_passthru_count(struct ctlr_info *h)
52060390f0c0SStephen M. Cameron {
52070390f0c0SStephen M. Cameron 	unsigned long flags;
52080390f0c0SStephen M. Cameron 
52090390f0c0SStephen M. Cameron 	spin_lock_irqsave(&h->passthru_count_lock, flags);
52100390f0c0SStephen M. Cameron 	if (h->passthru_count <= 0) {
52110390f0c0SStephen M. Cameron 		spin_unlock_irqrestore(&h->passthru_count_lock, flags);
52120390f0c0SStephen M. Cameron 		/* not expecting to get here. */
52130390f0c0SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Bug detected, passthru_count seems to be incorrect.\n");
52140390f0c0SStephen M. Cameron 		return;
52150390f0c0SStephen M. Cameron 	}
52160390f0c0SStephen M. Cameron 	h->passthru_count--;
52170390f0c0SStephen M. Cameron 	spin_unlock_irqrestore(&h->passthru_count_lock, flags);
52180390f0c0SStephen M. Cameron }
52190390f0c0SStephen M. Cameron 
5220edd16368SStephen M. Cameron /*
5221edd16368SStephen M. Cameron  * ioctl
5222edd16368SStephen M. Cameron  */
5223edd16368SStephen M. Cameron static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
5224edd16368SStephen M. Cameron {
5225edd16368SStephen M. Cameron 	struct ctlr_info *h;
5226edd16368SStephen M. Cameron 	void __user *argp = (void __user *)arg;
52270390f0c0SStephen M. Cameron 	int rc;
5228edd16368SStephen M. Cameron 
5229edd16368SStephen M. Cameron 	h = sdev_to_hba(dev);
5230edd16368SStephen M. Cameron 
5231edd16368SStephen M. Cameron 	switch (cmd) {
5232edd16368SStephen M. Cameron 	case CCISS_DEREGDISK:
5233edd16368SStephen M. Cameron 	case CCISS_REGNEWDISK:
5234edd16368SStephen M. Cameron 	case CCISS_REGNEWD:
5235a08a8471SStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
5236edd16368SStephen M. Cameron 		return 0;
5237edd16368SStephen M. Cameron 	case CCISS_GETPCIINFO:
5238edd16368SStephen M. Cameron 		return hpsa_getpciinfo_ioctl(h, argp);
5239edd16368SStephen M. Cameron 	case CCISS_GETDRIVVER:
5240edd16368SStephen M. Cameron 		return hpsa_getdrivver_ioctl(h, argp);
5241edd16368SStephen M. Cameron 	case CCISS_PASSTHRU:
52420390f0c0SStephen M. Cameron 		if (increment_passthru_count(h))
52430390f0c0SStephen M. Cameron 			return -EAGAIN;
52440390f0c0SStephen M. Cameron 		rc = hpsa_passthru_ioctl(h, argp);
52450390f0c0SStephen M. Cameron 		decrement_passthru_count(h);
52460390f0c0SStephen M. Cameron 		return rc;
5247edd16368SStephen M. Cameron 	case CCISS_BIG_PASSTHRU:
52480390f0c0SStephen M. Cameron 		if (increment_passthru_count(h))
52490390f0c0SStephen M. Cameron 			return -EAGAIN;
52500390f0c0SStephen M. Cameron 		rc = hpsa_big_passthru_ioctl(h, argp);
52510390f0c0SStephen M. Cameron 		decrement_passthru_count(h);
52520390f0c0SStephen M. Cameron 		return rc;
5253edd16368SStephen M. Cameron 	default:
5254edd16368SStephen M. Cameron 		return -ENOTTY;
5255edd16368SStephen M. Cameron 	}
5256edd16368SStephen M. Cameron }
5257edd16368SStephen M. Cameron 
52586f039790SGreg Kroah-Hartman static int hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
52596f039790SGreg Kroah-Hartman 				u8 reset_type)
526064670ac8SStephen M. Cameron {
526164670ac8SStephen M. Cameron 	struct CommandList *c;
526264670ac8SStephen M. Cameron 
526364670ac8SStephen M. Cameron 	c = cmd_alloc(h);
526464670ac8SStephen M. Cameron 	if (!c)
526564670ac8SStephen M. Cameron 		return -ENOMEM;
5266a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map */
5267a2dac136SStephen M. Cameron 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
526864670ac8SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_MSG);
526964670ac8SStephen M. Cameron 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
527064670ac8SStephen M. Cameron 	c->waiting = NULL;
527164670ac8SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
527264670ac8SStephen M. Cameron 	/* Don't wait for completion, the reset won't complete.  Don't free
527364670ac8SStephen M. Cameron 	 * the command either.  This is the last command we will send before
527464670ac8SStephen M. Cameron 	 * re-initializing everything, so it doesn't matter and won't leak.
527564670ac8SStephen M. Cameron 	 */
527664670ac8SStephen M. Cameron 	return 0;
527764670ac8SStephen M. Cameron }
527864670ac8SStephen M. Cameron 
5279a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
5280b7bb24ebSStephen M. Cameron 	void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
5281edd16368SStephen M. Cameron 	int cmd_type)
5282edd16368SStephen M. Cameron {
5283edd16368SStephen M. Cameron 	int pci_dir = XFER_NONE;
528475167d2cSStephen M. Cameron 	struct CommandList *a; /* for commands to be aborted */
5285edd16368SStephen M. Cameron 
5286edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5287edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
5288edd16368SStephen M. Cameron 	if (buff != NULL && size > 0) {
5289edd16368SStephen M. Cameron 		c->Header.SGList = 1;
5290edd16368SStephen M. Cameron 		c->Header.SGTotal = 1;
5291edd16368SStephen M. Cameron 	} else {
5292edd16368SStephen M. Cameron 		c->Header.SGList = 0;
5293edd16368SStephen M. Cameron 		c->Header.SGTotal = 0;
5294edd16368SStephen M. Cameron 	}
5295edd16368SStephen M. Cameron 	c->Header.Tag.lower = c->busaddr;
5296edd16368SStephen M. Cameron 	memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
5297edd16368SStephen M. Cameron 
5298edd16368SStephen M. Cameron 	c->Request.Type.Type = cmd_type;
5299edd16368SStephen M. Cameron 	if (cmd_type == TYPE_CMD) {
5300edd16368SStephen M. Cameron 		switch (cmd) {
5301edd16368SStephen M. Cameron 		case HPSA_INQUIRY:
5302edd16368SStephen M. Cameron 			/* are we trying to read a vital product page */
5303b7bb24ebSStephen M. Cameron 			if (page_code & VPD_PAGE) {
5304edd16368SStephen M. Cameron 				c->Request.CDB[1] = 0x01;
5305b7bb24ebSStephen M. Cameron 				c->Request.CDB[2] = (page_code & 0xff);
5306edd16368SStephen M. Cameron 			}
5307edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
5308edd16368SStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
5309edd16368SStephen M. Cameron 			c->Request.Type.Direction = XFER_READ;
5310edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5311edd16368SStephen M. Cameron 			c->Request.CDB[0] = HPSA_INQUIRY;
5312edd16368SStephen M. Cameron 			c->Request.CDB[4] = size & 0xFF;
5313edd16368SStephen M. Cameron 			break;
5314edd16368SStephen M. Cameron 		case HPSA_REPORT_LOG:
5315edd16368SStephen M. Cameron 		case HPSA_REPORT_PHYS:
5316edd16368SStephen M. Cameron 			/* Talking to controller so It's a physical command
5317edd16368SStephen M. Cameron 			   mode = 00 target = 0.  Nothing to write.
5318edd16368SStephen M. Cameron 			 */
5319edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
5320edd16368SStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
5321edd16368SStephen M. Cameron 			c->Request.Type.Direction = XFER_READ;
5322edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5323edd16368SStephen M. Cameron 			c->Request.CDB[0] = cmd;
5324edd16368SStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
5325edd16368SStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5326edd16368SStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5327edd16368SStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
5328edd16368SStephen M. Cameron 			break;
5329edd16368SStephen M. Cameron 		case HPSA_CACHE_FLUSH:
5330edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
5331edd16368SStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
5332edd16368SStephen M. Cameron 			c->Request.Type.Direction = XFER_WRITE;
5333edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5334edd16368SStephen M. Cameron 			c->Request.CDB[0] = BMIC_WRITE;
5335edd16368SStephen M. Cameron 			c->Request.CDB[6] = BMIC_CACHE_FLUSH;
5336bb158eabSStephen M. Cameron 			c->Request.CDB[7] = (size >> 8) & 0xFF;
5337bb158eabSStephen M. Cameron 			c->Request.CDB[8] = size & 0xFF;
5338edd16368SStephen M. Cameron 			break;
5339edd16368SStephen M. Cameron 		case TEST_UNIT_READY:
5340edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
5341edd16368SStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
5342edd16368SStephen M. Cameron 			c->Request.Type.Direction = XFER_NONE;
5343edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
5344edd16368SStephen M. Cameron 			break;
5345283b4a9bSStephen M. Cameron 		case HPSA_GET_RAID_MAP:
5346283b4a9bSStephen M. Cameron 			c->Request.CDBLen = 12;
5347283b4a9bSStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
5348283b4a9bSStephen M. Cameron 			c->Request.Type.Direction = XFER_READ;
5349283b4a9bSStephen M. Cameron 			c->Request.Timeout = 0;
5350283b4a9bSStephen M. Cameron 			c->Request.CDB[0] = HPSA_CISS_READ;
5351283b4a9bSStephen M. Cameron 			c->Request.CDB[1] = cmd;
5352283b4a9bSStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
5353283b4a9bSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5354283b4a9bSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5355283b4a9bSStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
5356283b4a9bSStephen M. Cameron 			break;
5357316b221aSStephen M. Cameron 		case BMIC_SENSE_CONTROLLER_PARAMETERS:
5358316b221aSStephen M. Cameron 			c->Request.CDBLen = 10;
5359316b221aSStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
5360316b221aSStephen M. Cameron 			c->Request.Type.Direction = XFER_READ;
5361316b221aSStephen M. Cameron 			c->Request.Timeout = 0;
5362316b221aSStephen M. Cameron 			c->Request.CDB[0] = BMIC_READ;
5363316b221aSStephen M. Cameron 			c->Request.CDB[6] = BMIC_SENSE_CONTROLLER_PARAMETERS;
5364316b221aSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
5365316b221aSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
5366316b221aSStephen M. Cameron 			break;
5367edd16368SStephen M. Cameron 		default:
5368edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
5369edd16368SStephen M. Cameron 			BUG();
5370a2dac136SStephen M. Cameron 			return -1;
5371edd16368SStephen M. Cameron 		}
5372edd16368SStephen M. Cameron 	} else if (cmd_type == TYPE_MSG) {
5373edd16368SStephen M. Cameron 		switch (cmd) {
5374edd16368SStephen M. Cameron 
5375edd16368SStephen M. Cameron 		case  HPSA_DEVICE_RESET_MSG:
5376edd16368SStephen M. Cameron 			c->Request.CDBLen = 16;
5377edd16368SStephen M. Cameron 			c->Request.Type.Type =  1; /* It is a MSG not a CMD */
5378edd16368SStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
5379edd16368SStephen M. Cameron 			c->Request.Type.Direction = XFER_NONE;
5380edd16368SStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
538164670ac8SStephen M. Cameron 			memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
538264670ac8SStephen M. Cameron 			c->Request.CDB[0] =  cmd;
538321e89afdSStephen M. Cameron 			c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
5384edd16368SStephen M. Cameron 			/* If bytes 4-7 are zero, it means reset the */
5385edd16368SStephen M. Cameron 			/* LunID device */
5386edd16368SStephen M. Cameron 			c->Request.CDB[4] = 0x00;
5387edd16368SStephen M. Cameron 			c->Request.CDB[5] = 0x00;
5388edd16368SStephen M. Cameron 			c->Request.CDB[6] = 0x00;
5389edd16368SStephen M. Cameron 			c->Request.CDB[7] = 0x00;
5390edd16368SStephen M. Cameron 			break;
539175167d2cSStephen M. Cameron 		case  HPSA_ABORT_MSG:
539275167d2cSStephen M. Cameron 			a = buff;       /* point to command to be aborted */
539375167d2cSStephen M. Cameron 			dev_dbg(&h->pdev->dev, "Abort Tag:0x%08x:%08x using request Tag:0x%08x:%08x\n",
539475167d2cSStephen M. Cameron 				a->Header.Tag.upper, a->Header.Tag.lower,
539575167d2cSStephen M. Cameron 				c->Header.Tag.upper, c->Header.Tag.lower);
539675167d2cSStephen M. Cameron 			c->Request.CDBLen = 16;
539775167d2cSStephen M. Cameron 			c->Request.Type.Type = TYPE_MSG;
539875167d2cSStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
539975167d2cSStephen M. Cameron 			c->Request.Type.Direction = XFER_WRITE;
540075167d2cSStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
540175167d2cSStephen M. Cameron 			c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
540275167d2cSStephen M. Cameron 			c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
540375167d2cSStephen M. Cameron 			c->Request.CDB[2] = 0x00; /* reserved */
540475167d2cSStephen M. Cameron 			c->Request.CDB[3] = 0x00; /* reserved */
540575167d2cSStephen M. Cameron 			/* Tag to abort goes in CDB[4]-CDB[11] */
540675167d2cSStephen M. Cameron 			c->Request.CDB[4] = a->Header.Tag.lower & 0xFF;
540775167d2cSStephen M. Cameron 			c->Request.CDB[5] = (a->Header.Tag.lower >> 8) & 0xFF;
540875167d2cSStephen M. Cameron 			c->Request.CDB[6] = (a->Header.Tag.lower >> 16) & 0xFF;
540975167d2cSStephen M. Cameron 			c->Request.CDB[7] = (a->Header.Tag.lower >> 24) & 0xFF;
541075167d2cSStephen M. Cameron 			c->Request.CDB[8] = a->Header.Tag.upper & 0xFF;
541175167d2cSStephen M. Cameron 			c->Request.CDB[9] = (a->Header.Tag.upper >> 8) & 0xFF;
541275167d2cSStephen M. Cameron 			c->Request.CDB[10] = (a->Header.Tag.upper >> 16) & 0xFF;
541375167d2cSStephen M. Cameron 			c->Request.CDB[11] = (a->Header.Tag.upper >> 24) & 0xFF;
541475167d2cSStephen M. Cameron 			c->Request.CDB[12] = 0x00; /* reserved */
541575167d2cSStephen M. Cameron 			c->Request.CDB[13] = 0x00; /* reserved */
541675167d2cSStephen M. Cameron 			c->Request.CDB[14] = 0x00; /* reserved */
541775167d2cSStephen M. Cameron 			c->Request.CDB[15] = 0x00; /* reserved */
541875167d2cSStephen M. Cameron 		break;
5419edd16368SStephen M. Cameron 		default:
5420edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown message type %d\n",
5421edd16368SStephen M. Cameron 				cmd);
5422edd16368SStephen M. Cameron 			BUG();
5423edd16368SStephen M. Cameron 		}
5424edd16368SStephen M. Cameron 	} else {
5425edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
5426edd16368SStephen M. Cameron 		BUG();
5427edd16368SStephen M. Cameron 	}
5428edd16368SStephen M. Cameron 
5429edd16368SStephen M. Cameron 	switch (c->Request.Type.Direction) {
5430edd16368SStephen M. Cameron 	case XFER_READ:
5431edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_FROMDEVICE;
5432edd16368SStephen M. Cameron 		break;
5433edd16368SStephen M. Cameron 	case XFER_WRITE:
5434edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_TODEVICE;
5435edd16368SStephen M. Cameron 		break;
5436edd16368SStephen M. Cameron 	case XFER_NONE:
5437edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_NONE;
5438edd16368SStephen M. Cameron 		break;
5439edd16368SStephen M. Cameron 	default:
5440edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_BIDIRECTIONAL;
5441edd16368SStephen M. Cameron 	}
5442a2dac136SStephen M. Cameron 	if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
5443a2dac136SStephen M. Cameron 		return -1;
5444a2dac136SStephen M. Cameron 	return 0;
5445edd16368SStephen M. Cameron }
5446edd16368SStephen M. Cameron 
5447edd16368SStephen M. Cameron /*
5448edd16368SStephen M. Cameron  * Map (physical) PCI mem into (virtual) kernel space
5449edd16368SStephen M. Cameron  */
5450edd16368SStephen M. Cameron static void __iomem *remap_pci_mem(ulong base, ulong size)
5451edd16368SStephen M. Cameron {
5452edd16368SStephen M. Cameron 	ulong page_base = ((ulong) base) & PAGE_MASK;
5453edd16368SStephen M. Cameron 	ulong page_offs = ((ulong) base) - page_base;
5454088ba34cSStephen M. Cameron 	void __iomem *page_remapped = ioremap_nocache(page_base,
5455088ba34cSStephen M. Cameron 		page_offs + size);
5456edd16368SStephen M. Cameron 
5457edd16368SStephen M. Cameron 	return page_remapped ? (page_remapped + page_offs) : NULL;
5458edd16368SStephen M. Cameron }
5459edd16368SStephen M. Cameron 
5460edd16368SStephen M. Cameron /* Takes cmds off the submission queue and sends them to the hardware,
5461edd16368SStephen M. Cameron  * then puts them on the queue of cmds waiting for completion.
5462edd16368SStephen M. Cameron  */
5463edd16368SStephen M. Cameron static void start_io(struct ctlr_info *h)
5464edd16368SStephen M. Cameron {
5465edd16368SStephen M. Cameron 	struct CommandList *c;
5466e16a33adSMatt Gates 	unsigned long flags;
5467edd16368SStephen M. Cameron 
5468e16a33adSMatt Gates 	spin_lock_irqsave(&h->lock, flags);
54699e0fc764SStephen M. Cameron 	while (!list_empty(&h->reqQ)) {
54709e0fc764SStephen M. Cameron 		c = list_entry(h->reqQ.next, struct CommandList, list);
5471edd16368SStephen M. Cameron 		/* can't do anything if fifo is full */
5472edd16368SStephen M. Cameron 		if ((h->access.fifo_full(h))) {
5473396883e2SStephen M. Cameron 			h->fifo_recently_full = 1;
5474edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "fifo full\n");
5475edd16368SStephen M. Cameron 			break;
5476edd16368SStephen M. Cameron 		}
5477396883e2SStephen M. Cameron 		h->fifo_recently_full = 0;
5478edd16368SStephen M. Cameron 
5479edd16368SStephen M. Cameron 		/* Get the first entry from the Request Q */
5480edd16368SStephen M. Cameron 		removeQ(c);
5481edd16368SStephen M. Cameron 		h->Qdepth--;
5482edd16368SStephen M. Cameron 
5483edd16368SStephen M. Cameron 		/* Put job onto the completed Q */
5484edd16368SStephen M. Cameron 		addQ(&h->cmpQ, c);
5485e16a33adSMatt Gates 
5486e16a33adSMatt Gates 		/* Must increment commands_outstanding before unlocking
5487e16a33adSMatt Gates 		 * and submitting to avoid race checking for fifo full
5488e16a33adSMatt Gates 		 * condition.
5489e16a33adSMatt Gates 		 */
5490e16a33adSMatt Gates 		h->commands_outstanding++;
5491e16a33adSMatt Gates 
5492e16a33adSMatt Gates 		/* Tell the controller execute command */
5493e16a33adSMatt Gates 		spin_unlock_irqrestore(&h->lock, flags);
5494e16a33adSMatt Gates 		h->access.submit_command(h, c);
5495e16a33adSMatt Gates 		spin_lock_irqsave(&h->lock, flags);
5496edd16368SStephen M. Cameron 	}
5497e16a33adSMatt Gates 	spin_unlock_irqrestore(&h->lock, flags);
5498edd16368SStephen M. Cameron }
5499edd16368SStephen M. Cameron 
5500254f796bSMatt Gates static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
5501edd16368SStephen M. Cameron {
5502254f796bSMatt Gates 	return h->access.command_completed(h, q);
5503edd16368SStephen M. Cameron }
5504edd16368SStephen M. Cameron 
5505900c5440SStephen M. Cameron static inline bool interrupt_pending(struct ctlr_info *h)
5506edd16368SStephen M. Cameron {
5507edd16368SStephen M. Cameron 	return h->access.intr_pending(h);
5508edd16368SStephen M. Cameron }
5509edd16368SStephen M. Cameron 
5510edd16368SStephen M. Cameron static inline long interrupt_not_for_us(struct ctlr_info *h)
5511edd16368SStephen M. Cameron {
551210f66018SStephen M. Cameron 	return (h->access.intr_pending(h) == 0) ||
551310f66018SStephen M. Cameron 		(h->interrupts_enabled == 0);
5514edd16368SStephen M. Cameron }
5515edd16368SStephen M. Cameron 
551601a02ffcSStephen M. Cameron static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
551701a02ffcSStephen M. Cameron 	u32 raw_tag)
5518edd16368SStephen M. Cameron {
5519edd16368SStephen M. Cameron 	if (unlikely(tag_index >= h->nr_cmds)) {
5520edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
5521edd16368SStephen M. Cameron 		return 1;
5522edd16368SStephen M. Cameron 	}
5523edd16368SStephen M. Cameron 	return 0;
5524edd16368SStephen M. Cameron }
5525edd16368SStephen M. Cameron 
55265a3d16f5SStephen M. Cameron static inline void finish_cmd(struct CommandList *c)
5527edd16368SStephen M. Cameron {
5528e16a33adSMatt Gates 	unsigned long flags;
5529396883e2SStephen M. Cameron 	int io_may_be_stalled = 0;
5530396883e2SStephen M. Cameron 	struct ctlr_info *h = c->h;
5531e16a33adSMatt Gates 
5532396883e2SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
5533edd16368SStephen M. Cameron 	removeQ(c);
5534396883e2SStephen M. Cameron 
5535396883e2SStephen M. Cameron 	/*
5536396883e2SStephen M. Cameron 	 * Check for possibly stalled i/o.
5537396883e2SStephen M. Cameron 	 *
5538396883e2SStephen M. Cameron 	 * If a fifo_full condition is encountered, requests will back up
5539396883e2SStephen M. Cameron 	 * in h->reqQ.  This queue is only emptied out by start_io which is
5540396883e2SStephen M. Cameron 	 * only called when a new i/o request comes in.  If no i/o's are
5541396883e2SStephen M. Cameron 	 * forthcoming, the i/o's in h->reqQ can get stuck.  So we call
5542396883e2SStephen M. Cameron 	 * start_io from here if we detect such a danger.
5543396883e2SStephen M. Cameron 	 *
5544396883e2SStephen M. Cameron 	 * Normally, we shouldn't hit this case, but pounding on the
5545396883e2SStephen M. Cameron 	 * CCISS_PASSTHRU ioctl can provoke it.  Only call start_io if
5546396883e2SStephen M. Cameron 	 * commands_outstanding is low.  We want to avoid calling
5547396883e2SStephen M. Cameron 	 * start_io from in here as much as possible, and esp. don't
5548396883e2SStephen M. Cameron 	 * want to get in a cycle where we call start_io every time
5549396883e2SStephen M. Cameron 	 * through here.
5550396883e2SStephen M. Cameron 	 */
5551396883e2SStephen M. Cameron 	if (unlikely(h->fifo_recently_full) &&
5552396883e2SStephen M. Cameron 		h->commands_outstanding < 5)
5553396883e2SStephen M. Cameron 		io_may_be_stalled = 1;
5554396883e2SStephen M. Cameron 
5555396883e2SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
5556396883e2SStephen M. Cameron 
5557e85c5974SStephen M. Cameron 	dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
5558c349775eSScott Teel 	if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
5559c349775eSScott Teel 			|| c->cmd_type == CMD_IOACCEL2))
55601fb011fbSStephen M. Cameron 		complete_scsi_command(c);
5561edd16368SStephen M. Cameron 	else if (c->cmd_type == CMD_IOCTL_PEND)
5562edd16368SStephen M. Cameron 		complete(c->waiting);
5563396883e2SStephen M. Cameron 	if (unlikely(io_may_be_stalled))
5564396883e2SStephen M. Cameron 		start_io(h);
5565edd16368SStephen M. Cameron }
5566edd16368SStephen M. Cameron 
5567a104c99fSStephen M. Cameron static inline u32 hpsa_tag_contains_index(u32 tag)
5568a104c99fSStephen M. Cameron {
5569a104c99fSStephen M. Cameron 	return tag & DIRECT_LOOKUP_BIT;
5570a104c99fSStephen M. Cameron }
5571a104c99fSStephen M. Cameron 
5572a104c99fSStephen M. Cameron static inline u32 hpsa_tag_to_index(u32 tag)
5573a104c99fSStephen M. Cameron {
5574a104c99fSStephen M. Cameron 	return tag >> DIRECT_LOOKUP_SHIFT;
5575a104c99fSStephen M. Cameron }
5576a104c99fSStephen M. Cameron 
5577a9a3a273SStephen M. Cameron 
5578a9a3a273SStephen M. Cameron static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
5579a104c99fSStephen M. Cameron {
5580a9a3a273SStephen M. Cameron #define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
5581a9a3a273SStephen M. Cameron #define HPSA_SIMPLE_ERROR_BITS 0x03
5582960a30e7SStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
5583a9a3a273SStephen M. Cameron 		return tag & ~HPSA_SIMPLE_ERROR_BITS;
5584a9a3a273SStephen M. Cameron 	return tag & ~HPSA_PERF_ERROR_BITS;
5585a104c99fSStephen M. Cameron }
5586a104c99fSStephen M. Cameron 
5587303932fdSDon Brace /* process completion of an indexed ("direct lookup") command */
55881d94f94dSStephen M. Cameron static inline void process_indexed_cmd(struct ctlr_info *h,
5589303932fdSDon Brace 	u32 raw_tag)
5590303932fdSDon Brace {
5591303932fdSDon Brace 	u32 tag_index;
5592303932fdSDon Brace 	struct CommandList *c;
5593303932fdSDon Brace 
5594303932fdSDon Brace 	tag_index = hpsa_tag_to_index(raw_tag);
55951d94f94dSStephen M. Cameron 	if (!bad_tag(h, tag_index, raw_tag)) {
5596303932fdSDon Brace 		c = h->cmd_pool + tag_index;
55975a3d16f5SStephen M. Cameron 		finish_cmd(c);
55981d94f94dSStephen M. Cameron 	}
5599303932fdSDon Brace }
5600303932fdSDon Brace 
5601303932fdSDon Brace /* process completion of a non-indexed command */
56021d94f94dSStephen M. Cameron static inline void process_nonindexed_cmd(struct ctlr_info *h,
5603303932fdSDon Brace 	u32 raw_tag)
5604303932fdSDon Brace {
5605303932fdSDon Brace 	u32 tag;
5606303932fdSDon Brace 	struct CommandList *c = NULL;
5607e16a33adSMatt Gates 	unsigned long flags;
5608303932fdSDon Brace 
5609a9a3a273SStephen M. Cameron 	tag = hpsa_tag_discard_error_bits(h, raw_tag);
5610e16a33adSMatt Gates 	spin_lock_irqsave(&h->lock, flags);
56119e0fc764SStephen M. Cameron 	list_for_each_entry(c, &h->cmpQ, list) {
5612303932fdSDon Brace 		if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
5613e16a33adSMatt Gates 			spin_unlock_irqrestore(&h->lock, flags);
56145a3d16f5SStephen M. Cameron 			finish_cmd(c);
56151d94f94dSStephen M. Cameron 			return;
5616303932fdSDon Brace 		}
5617303932fdSDon Brace 	}
5618e16a33adSMatt Gates 	spin_unlock_irqrestore(&h->lock, flags);
5619303932fdSDon Brace 	bad_tag(h, h->nr_cmds + 1, raw_tag);
5620303932fdSDon Brace }
5621303932fdSDon Brace 
562264670ac8SStephen M. Cameron /* Some controllers, like p400, will give us one interrupt
562364670ac8SStephen M. Cameron  * after a soft reset, even if we turned interrupts off.
562464670ac8SStephen M. Cameron  * Only need to check for this in the hpsa_xxx_discard_completions
562564670ac8SStephen M. Cameron  * functions.
562664670ac8SStephen M. Cameron  */
562764670ac8SStephen M. Cameron static int ignore_bogus_interrupt(struct ctlr_info *h)
562864670ac8SStephen M. Cameron {
562964670ac8SStephen M. Cameron 	if (likely(!reset_devices))
563064670ac8SStephen M. Cameron 		return 0;
563164670ac8SStephen M. Cameron 
563264670ac8SStephen M. Cameron 	if (likely(h->interrupts_enabled))
563364670ac8SStephen M. Cameron 		return 0;
563464670ac8SStephen M. Cameron 
563564670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
563664670ac8SStephen M. Cameron 		"(known firmware bug.)  Ignoring.\n");
563764670ac8SStephen M. Cameron 
563864670ac8SStephen M. Cameron 	return 1;
563964670ac8SStephen M. Cameron }
564064670ac8SStephen M. Cameron 
5641254f796bSMatt Gates /*
5642254f796bSMatt Gates  * Convert &h->q[x] (passed to interrupt handlers) back to h.
5643254f796bSMatt Gates  * Relies on (h-q[x] == x) being true for x such that
5644254f796bSMatt Gates  * 0 <= x < MAX_REPLY_QUEUES.
5645254f796bSMatt Gates  */
5646254f796bSMatt Gates static struct ctlr_info *queue_to_hba(u8 *queue)
564764670ac8SStephen M. Cameron {
5648254f796bSMatt Gates 	return container_of((queue - *queue), struct ctlr_info, q[0]);
5649254f796bSMatt Gates }
5650254f796bSMatt Gates 
5651254f796bSMatt Gates static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
5652254f796bSMatt Gates {
5653254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
5654254f796bSMatt Gates 	u8 q = *(u8 *) queue;
565564670ac8SStephen M. Cameron 	u32 raw_tag;
565664670ac8SStephen M. Cameron 
565764670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
565864670ac8SStephen M. Cameron 		return IRQ_NONE;
565964670ac8SStephen M. Cameron 
566064670ac8SStephen M. Cameron 	if (interrupt_not_for_us(h))
566164670ac8SStephen M. Cameron 		return IRQ_NONE;
5662a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
566364670ac8SStephen M. Cameron 	while (interrupt_pending(h)) {
5664254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
566564670ac8SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY)
5666254f796bSMatt Gates 			raw_tag = next_command(h, q);
566764670ac8SStephen M. Cameron 	}
566864670ac8SStephen M. Cameron 	return IRQ_HANDLED;
566964670ac8SStephen M. Cameron }
567064670ac8SStephen M. Cameron 
5671254f796bSMatt Gates static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
567264670ac8SStephen M. Cameron {
5673254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
567464670ac8SStephen M. Cameron 	u32 raw_tag;
5675254f796bSMatt Gates 	u8 q = *(u8 *) queue;
567664670ac8SStephen M. Cameron 
567764670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
567864670ac8SStephen M. Cameron 		return IRQ_NONE;
567964670ac8SStephen M. Cameron 
5680a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
5681254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
568264670ac8SStephen M. Cameron 	while (raw_tag != FIFO_EMPTY)
5683254f796bSMatt Gates 		raw_tag = next_command(h, q);
568464670ac8SStephen M. Cameron 	return IRQ_HANDLED;
568564670ac8SStephen M. Cameron }
568664670ac8SStephen M. Cameron 
5687254f796bSMatt Gates static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
5688edd16368SStephen M. Cameron {
5689254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba((u8 *) queue);
5690303932fdSDon Brace 	u32 raw_tag;
5691254f796bSMatt Gates 	u8 q = *(u8 *) queue;
5692edd16368SStephen M. Cameron 
5693edd16368SStephen M. Cameron 	if (interrupt_not_for_us(h))
5694edd16368SStephen M. Cameron 		return IRQ_NONE;
5695a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
569610f66018SStephen M. Cameron 	while (interrupt_pending(h)) {
5697254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
569810f66018SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY) {
56991d94f94dSStephen M. Cameron 			if (likely(hpsa_tag_contains_index(raw_tag)))
57001d94f94dSStephen M. Cameron 				process_indexed_cmd(h, raw_tag);
570110f66018SStephen M. Cameron 			else
57021d94f94dSStephen M. Cameron 				process_nonindexed_cmd(h, raw_tag);
5703254f796bSMatt Gates 			raw_tag = next_command(h, q);
570410f66018SStephen M. Cameron 		}
570510f66018SStephen M. Cameron 	}
570610f66018SStephen M. Cameron 	return IRQ_HANDLED;
570710f66018SStephen M. Cameron }
570810f66018SStephen M. Cameron 
5709254f796bSMatt Gates static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
571010f66018SStephen M. Cameron {
5711254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
571210f66018SStephen M. Cameron 	u32 raw_tag;
5713254f796bSMatt Gates 	u8 q = *(u8 *) queue;
571410f66018SStephen M. Cameron 
5715a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
5716254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
5717303932fdSDon Brace 	while (raw_tag != FIFO_EMPTY) {
57181d94f94dSStephen M. Cameron 		if (likely(hpsa_tag_contains_index(raw_tag)))
57191d94f94dSStephen M. Cameron 			process_indexed_cmd(h, raw_tag);
5720303932fdSDon Brace 		else
57211d94f94dSStephen M. Cameron 			process_nonindexed_cmd(h, raw_tag);
5722254f796bSMatt Gates 		raw_tag = next_command(h, q);
5723edd16368SStephen M. Cameron 	}
5724edd16368SStephen M. Cameron 	return IRQ_HANDLED;
5725edd16368SStephen M. Cameron }
5726edd16368SStephen M. Cameron 
5727a9a3a273SStephen M. Cameron /* Send a message CDB to the firmware. Careful, this only works
5728a9a3a273SStephen M. Cameron  * in simple mode, not performant mode due to the tag lookup.
5729a9a3a273SStephen M. Cameron  * We only ever use this immediately after a controller reset.
5730a9a3a273SStephen M. Cameron  */
57316f039790SGreg Kroah-Hartman static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
5732edd16368SStephen M. Cameron 			unsigned char type)
5733edd16368SStephen M. Cameron {
5734edd16368SStephen M. Cameron 	struct Command {
5735edd16368SStephen M. Cameron 		struct CommandListHeader CommandHeader;
5736edd16368SStephen M. Cameron 		struct RequestBlock Request;
5737edd16368SStephen M. Cameron 		struct ErrDescriptor ErrorDescriptor;
5738edd16368SStephen M. Cameron 	};
5739edd16368SStephen M. Cameron 	struct Command *cmd;
5740edd16368SStephen M. Cameron 	static const size_t cmd_sz = sizeof(*cmd) +
5741edd16368SStephen M. Cameron 					sizeof(cmd->ErrorDescriptor);
5742edd16368SStephen M. Cameron 	dma_addr_t paddr64;
5743edd16368SStephen M. Cameron 	uint32_t paddr32, tag;
5744edd16368SStephen M. Cameron 	void __iomem *vaddr;
5745edd16368SStephen M. Cameron 	int i, err;
5746edd16368SStephen M. Cameron 
5747edd16368SStephen M. Cameron 	vaddr = pci_ioremap_bar(pdev, 0);
5748edd16368SStephen M. Cameron 	if (vaddr == NULL)
5749edd16368SStephen M. Cameron 		return -ENOMEM;
5750edd16368SStephen M. Cameron 
5751edd16368SStephen M. Cameron 	/* The Inbound Post Queue only accepts 32-bit physical addresses for the
5752edd16368SStephen M. Cameron 	 * CCISS commands, so they must be allocated from the lower 4GiB of
5753edd16368SStephen M. Cameron 	 * memory.
5754edd16368SStephen M. Cameron 	 */
5755edd16368SStephen M. Cameron 	err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
5756edd16368SStephen M. Cameron 	if (err) {
5757edd16368SStephen M. Cameron 		iounmap(vaddr);
5758edd16368SStephen M. Cameron 		return -ENOMEM;
5759edd16368SStephen M. Cameron 	}
5760edd16368SStephen M. Cameron 
5761edd16368SStephen M. Cameron 	cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
5762edd16368SStephen M. Cameron 	if (cmd == NULL) {
5763edd16368SStephen M. Cameron 		iounmap(vaddr);
5764edd16368SStephen M. Cameron 		return -ENOMEM;
5765edd16368SStephen M. Cameron 	}
5766edd16368SStephen M. Cameron 
5767edd16368SStephen M. Cameron 	/* This must fit, because of the 32-bit consistent DMA mask.  Also,
5768edd16368SStephen M. Cameron 	 * although there's no guarantee, we assume that the address is at
5769edd16368SStephen M. Cameron 	 * least 4-byte aligned (most likely, it's page-aligned).
5770edd16368SStephen M. Cameron 	 */
5771edd16368SStephen M. Cameron 	paddr32 = paddr64;
5772edd16368SStephen M. Cameron 
5773edd16368SStephen M. Cameron 	cmd->CommandHeader.ReplyQueue = 0;
5774edd16368SStephen M. Cameron 	cmd->CommandHeader.SGList = 0;
5775edd16368SStephen M. Cameron 	cmd->CommandHeader.SGTotal = 0;
5776edd16368SStephen M. Cameron 	cmd->CommandHeader.Tag.lower = paddr32;
5777edd16368SStephen M. Cameron 	cmd->CommandHeader.Tag.upper = 0;
5778edd16368SStephen M. Cameron 	memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
5779edd16368SStephen M. Cameron 
5780edd16368SStephen M. Cameron 	cmd->Request.CDBLen = 16;
5781edd16368SStephen M. Cameron 	cmd->Request.Type.Type = TYPE_MSG;
5782edd16368SStephen M. Cameron 	cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
5783edd16368SStephen M. Cameron 	cmd->Request.Type.Direction = XFER_NONE;
5784edd16368SStephen M. Cameron 	cmd->Request.Timeout = 0; /* Don't time out */
5785edd16368SStephen M. Cameron 	cmd->Request.CDB[0] = opcode;
5786edd16368SStephen M. Cameron 	cmd->Request.CDB[1] = type;
5787edd16368SStephen M. Cameron 	memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
5788edd16368SStephen M. Cameron 	cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
5789edd16368SStephen M. Cameron 	cmd->ErrorDescriptor.Addr.upper = 0;
5790edd16368SStephen M. Cameron 	cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
5791edd16368SStephen M. Cameron 
5792edd16368SStephen M. Cameron 	writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
5793edd16368SStephen M. Cameron 
5794edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
5795edd16368SStephen M. Cameron 		tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
5796a9a3a273SStephen M. Cameron 		if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr32)
5797edd16368SStephen M. Cameron 			break;
5798edd16368SStephen M. Cameron 		msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
5799edd16368SStephen M. Cameron 	}
5800edd16368SStephen M. Cameron 
5801edd16368SStephen M. Cameron 	iounmap(vaddr);
5802edd16368SStephen M. Cameron 
5803edd16368SStephen M. Cameron 	/* we leak the DMA buffer here ... no choice since the controller could
5804edd16368SStephen M. Cameron 	 *  still complete the command.
5805edd16368SStephen M. Cameron 	 */
5806edd16368SStephen M. Cameron 	if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
5807edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
5808edd16368SStephen M. Cameron 			opcode, type);
5809edd16368SStephen M. Cameron 		return -ETIMEDOUT;
5810edd16368SStephen M. Cameron 	}
5811edd16368SStephen M. Cameron 
5812edd16368SStephen M. Cameron 	pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
5813edd16368SStephen M. Cameron 
5814edd16368SStephen M. Cameron 	if (tag & HPSA_ERROR_BIT) {
5815edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
5816edd16368SStephen M. Cameron 			opcode, type);
5817edd16368SStephen M. Cameron 		return -EIO;
5818edd16368SStephen M. Cameron 	}
5819edd16368SStephen M. Cameron 
5820edd16368SStephen M. Cameron 	dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
5821edd16368SStephen M. Cameron 		opcode, type);
5822edd16368SStephen M. Cameron 	return 0;
5823edd16368SStephen M. Cameron }
5824edd16368SStephen M. Cameron 
5825edd16368SStephen M. Cameron #define hpsa_noop(p) hpsa_message(p, 3, 0)
5826edd16368SStephen M. Cameron 
58271df8552aSStephen M. Cameron static int hpsa_controller_hard_reset(struct pci_dev *pdev,
5828cf0b08d0SStephen M. Cameron 	void * __iomem vaddr, u32 use_doorbell)
5829edd16368SStephen M. Cameron {
58301df8552aSStephen M. Cameron 	u16 pmcsr;
58311df8552aSStephen M. Cameron 	int pos;
5832edd16368SStephen M. Cameron 
58331df8552aSStephen M. Cameron 	if (use_doorbell) {
58341df8552aSStephen M. Cameron 		/* For everything after the P600, the PCI power state method
58351df8552aSStephen M. Cameron 		 * of resetting the controller doesn't work, so we have this
58361df8552aSStephen M. Cameron 		 * other way using the doorbell register.
5837edd16368SStephen M. Cameron 		 */
58381df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using doorbell to reset controller\n");
5839cf0b08d0SStephen M. Cameron 		writel(use_doorbell, vaddr + SA5_DOORBELL);
584085009239SStephen M. Cameron 
584185009239SStephen M. Cameron 		/* PMC hardware guys tell us we need a 5 second delay after
584285009239SStephen M. Cameron 		 * doorbell reset and before any attempt to talk to the board
584385009239SStephen M. Cameron 		 * at all to ensure that this actually works and doesn't fall
584485009239SStephen M. Cameron 		 * over in some weird corner cases.
584585009239SStephen M. Cameron 		 */
584685009239SStephen M. Cameron 		msleep(5000);
58471df8552aSStephen M. Cameron 	} else { /* Try to do it the PCI power state way */
5848edd16368SStephen M. Cameron 
5849edd16368SStephen M. Cameron 		/* Quoting from the Open CISS Specification: "The Power
5850edd16368SStephen M. Cameron 		 * Management Control/Status Register (CSR) controls the power
5851edd16368SStephen M. Cameron 		 * state of the device.  The normal operating state is D0,
5852edd16368SStephen M. Cameron 		 * CSR=00h.  The software off state is D3, CSR=03h.  To reset
58531df8552aSStephen M. Cameron 		 * the controller, place the interface device in D3 then to D0,
58541df8552aSStephen M. Cameron 		 * this causes a secondary PCI reset which will reset the
58551df8552aSStephen M. Cameron 		 * controller." */
5856edd16368SStephen M. Cameron 
58571df8552aSStephen M. Cameron 		pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
58581df8552aSStephen M. Cameron 		if (pos == 0) {
58591df8552aSStephen M. Cameron 			dev_err(&pdev->dev,
58601df8552aSStephen M. Cameron 				"hpsa_reset_controller: "
58611df8552aSStephen M. Cameron 				"PCI PM not supported\n");
58621df8552aSStephen M. Cameron 			return -ENODEV;
58631df8552aSStephen M. Cameron 		}
58641df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using PCI PM to reset controller\n");
5865edd16368SStephen M. Cameron 		/* enter the D3hot power management state */
5866edd16368SStephen M. Cameron 		pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
5867edd16368SStephen M. Cameron 		pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
5868edd16368SStephen M. Cameron 		pmcsr |= PCI_D3hot;
5869edd16368SStephen M. Cameron 		pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
5870edd16368SStephen M. Cameron 
5871edd16368SStephen M. Cameron 		msleep(500);
5872edd16368SStephen M. Cameron 
5873edd16368SStephen M. Cameron 		/* enter the D0 power management state */
5874edd16368SStephen M. Cameron 		pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
5875edd16368SStephen M. Cameron 		pmcsr |= PCI_D0;
5876edd16368SStephen M. Cameron 		pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
5877c4853efeSMike Miller 
5878c4853efeSMike Miller 		/*
5879c4853efeSMike Miller 		 * The P600 requires a small delay when changing states.
5880c4853efeSMike Miller 		 * Otherwise we may think the board did not reset and we bail.
5881c4853efeSMike Miller 		 * This for kdump only and is particular to the P600.
5882c4853efeSMike Miller 		 */
5883c4853efeSMike Miller 		msleep(500);
58841df8552aSStephen M. Cameron 	}
58851df8552aSStephen M. Cameron 	return 0;
58861df8552aSStephen M. Cameron }
58871df8552aSStephen M. Cameron 
58886f039790SGreg Kroah-Hartman static void init_driver_version(char *driver_version, int len)
5889580ada3cSStephen M. Cameron {
5890580ada3cSStephen M. Cameron 	memset(driver_version, 0, len);
5891f79cfec6SStephen M. Cameron 	strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
5892580ada3cSStephen M. Cameron }
5893580ada3cSStephen M. Cameron 
58946f039790SGreg Kroah-Hartman static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
5895580ada3cSStephen M. Cameron {
5896580ada3cSStephen M. Cameron 	char *driver_version;
5897580ada3cSStephen M. Cameron 	int i, size = sizeof(cfgtable->driver_version);
5898580ada3cSStephen M. Cameron 
5899580ada3cSStephen M. Cameron 	driver_version = kmalloc(size, GFP_KERNEL);
5900580ada3cSStephen M. Cameron 	if (!driver_version)
5901580ada3cSStephen M. Cameron 		return -ENOMEM;
5902580ada3cSStephen M. Cameron 
5903580ada3cSStephen M. Cameron 	init_driver_version(driver_version, size);
5904580ada3cSStephen M. Cameron 	for (i = 0; i < size; i++)
5905580ada3cSStephen M. Cameron 		writeb(driver_version[i], &cfgtable->driver_version[i]);
5906580ada3cSStephen M. Cameron 	kfree(driver_version);
5907580ada3cSStephen M. Cameron 	return 0;
5908580ada3cSStephen M. Cameron }
5909580ada3cSStephen M. Cameron 
59106f039790SGreg Kroah-Hartman static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
59116f039790SGreg Kroah-Hartman 					  unsigned char *driver_ver)
5912580ada3cSStephen M. Cameron {
5913580ada3cSStephen M. Cameron 	int i;
5914580ada3cSStephen M. Cameron 
5915580ada3cSStephen M. Cameron 	for (i = 0; i < sizeof(cfgtable->driver_version); i++)
5916580ada3cSStephen M. Cameron 		driver_ver[i] = readb(&cfgtable->driver_version[i]);
5917580ada3cSStephen M. Cameron }
5918580ada3cSStephen M. Cameron 
59196f039790SGreg Kroah-Hartman static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
5920580ada3cSStephen M. Cameron {
5921580ada3cSStephen M. Cameron 
5922580ada3cSStephen M. Cameron 	char *driver_ver, *old_driver_ver;
5923580ada3cSStephen M. Cameron 	int rc, size = sizeof(cfgtable->driver_version);
5924580ada3cSStephen M. Cameron 
5925580ada3cSStephen M. Cameron 	old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
5926580ada3cSStephen M. Cameron 	if (!old_driver_ver)
5927580ada3cSStephen M. Cameron 		return -ENOMEM;
5928580ada3cSStephen M. Cameron 	driver_ver = old_driver_ver + size;
5929580ada3cSStephen M. Cameron 
5930580ada3cSStephen M. Cameron 	/* After a reset, the 32 bytes of "driver version" in the cfgtable
5931580ada3cSStephen M. Cameron 	 * should have been changed, otherwise we know the reset failed.
5932580ada3cSStephen M. Cameron 	 */
5933580ada3cSStephen M. Cameron 	init_driver_version(old_driver_ver, size);
5934580ada3cSStephen M. Cameron 	read_driver_ver_from_cfgtable(cfgtable, driver_ver);
5935580ada3cSStephen M. Cameron 	rc = !memcmp(driver_ver, old_driver_ver, size);
5936580ada3cSStephen M. Cameron 	kfree(old_driver_ver);
5937580ada3cSStephen M. Cameron 	return rc;
5938580ada3cSStephen M. Cameron }
59391df8552aSStephen M. Cameron /* This does a hard reset of the controller using PCI power management
59401df8552aSStephen M. Cameron  * states or the using the doorbell register.
59411df8552aSStephen M. Cameron  */
59426f039790SGreg Kroah-Hartman static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev)
59431df8552aSStephen M. Cameron {
59441df8552aSStephen M. Cameron 	u64 cfg_offset;
59451df8552aSStephen M. Cameron 	u32 cfg_base_addr;
59461df8552aSStephen M. Cameron 	u64 cfg_base_addr_index;
59471df8552aSStephen M. Cameron 	void __iomem *vaddr;
59481df8552aSStephen M. Cameron 	unsigned long paddr;
5949580ada3cSStephen M. Cameron 	u32 misc_fw_support;
5950270d05deSStephen M. Cameron 	int rc;
59511df8552aSStephen M. Cameron 	struct CfgTable __iomem *cfgtable;
5952cf0b08d0SStephen M. Cameron 	u32 use_doorbell;
595318867659SStephen M. Cameron 	u32 board_id;
5954270d05deSStephen M. Cameron 	u16 command_register;
59551df8552aSStephen M. Cameron 
59561df8552aSStephen M. Cameron 	/* For controllers as old as the P600, this is very nearly
59571df8552aSStephen M. Cameron 	 * the same thing as
59581df8552aSStephen M. Cameron 	 *
59591df8552aSStephen M. Cameron 	 * pci_save_state(pci_dev);
59601df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D3hot);
59611df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D0);
59621df8552aSStephen M. Cameron 	 * pci_restore_state(pci_dev);
59631df8552aSStephen M. Cameron 	 *
59641df8552aSStephen M. Cameron 	 * For controllers newer than the P600, the pci power state
59651df8552aSStephen M. Cameron 	 * method of resetting doesn't work so we have another way
59661df8552aSStephen M. Cameron 	 * using the doorbell register.
59671df8552aSStephen M. Cameron 	 */
596818867659SStephen M. Cameron 
596925c1e56aSStephen M. Cameron 	rc = hpsa_lookup_board_id(pdev, &board_id);
597046380786SStephen M. Cameron 	if (rc < 0 || !ctlr_is_resettable(board_id)) {
597125c1e56aSStephen M. Cameron 		dev_warn(&pdev->dev, "Not resetting device.\n");
597225c1e56aSStephen M. Cameron 		return -ENODEV;
597325c1e56aSStephen M. Cameron 	}
597446380786SStephen M. Cameron 
597546380786SStephen M. Cameron 	/* if controller is soft- but not hard resettable... */
597646380786SStephen M. Cameron 	if (!ctlr_is_hard_resettable(board_id))
597746380786SStephen M. Cameron 		return -ENOTSUPP; /* try soft reset later. */
597818867659SStephen M. Cameron 
5979270d05deSStephen M. Cameron 	/* Save the PCI command register */
5980270d05deSStephen M. Cameron 	pci_read_config_word(pdev, 4, &command_register);
5981270d05deSStephen M. Cameron 	/* Turn the board off.  This is so that later pci_restore_state()
5982270d05deSStephen M. Cameron 	 * won't turn the board on before the rest of config space is ready.
5983270d05deSStephen M. Cameron 	 */
5984270d05deSStephen M. Cameron 	pci_disable_device(pdev);
5985270d05deSStephen M. Cameron 	pci_save_state(pdev);
59861df8552aSStephen M. Cameron 
59871df8552aSStephen M. Cameron 	/* find the first memory BAR, so we can find the cfg table */
59881df8552aSStephen M. Cameron 	rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
59891df8552aSStephen M. Cameron 	if (rc)
59901df8552aSStephen M. Cameron 		return rc;
59911df8552aSStephen M. Cameron 	vaddr = remap_pci_mem(paddr, 0x250);
59921df8552aSStephen M. Cameron 	if (!vaddr)
59931df8552aSStephen M. Cameron 		return -ENOMEM;
59941df8552aSStephen M. Cameron 
59951df8552aSStephen M. Cameron 	/* find cfgtable in order to check if reset via doorbell is supported */
59961df8552aSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
59971df8552aSStephen M. Cameron 					&cfg_base_addr_index, &cfg_offset);
59981df8552aSStephen M. Cameron 	if (rc)
59991df8552aSStephen M. Cameron 		goto unmap_vaddr;
60001df8552aSStephen M. Cameron 	cfgtable = remap_pci_mem(pci_resource_start(pdev,
60011df8552aSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
60021df8552aSStephen M. Cameron 	if (!cfgtable) {
60031df8552aSStephen M. Cameron 		rc = -ENOMEM;
60041df8552aSStephen M. Cameron 		goto unmap_vaddr;
60051df8552aSStephen M. Cameron 	}
6006580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(cfgtable);
6007580ada3cSStephen M. Cameron 	if (rc)
6008580ada3cSStephen M. Cameron 		goto unmap_vaddr;
60091df8552aSStephen M. Cameron 
6010cf0b08d0SStephen M. Cameron 	/* If reset via doorbell register is supported, use that.
6011cf0b08d0SStephen M. Cameron 	 * There are two such methods.  Favor the newest method.
6012cf0b08d0SStephen M. Cameron 	 */
60131df8552aSStephen M. Cameron 	misc_fw_support = readl(&cfgtable->misc_fw_support);
6014cf0b08d0SStephen M. Cameron 	use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
6015cf0b08d0SStephen M. Cameron 	if (use_doorbell) {
6016cf0b08d0SStephen M. Cameron 		use_doorbell = DOORBELL_CTLR_RESET2;
6017cf0b08d0SStephen M. Cameron 	} else {
60181df8552aSStephen M. Cameron 		use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
6019cf0b08d0SStephen M. Cameron 		if (use_doorbell) {
6020fba63097SMike Miller 			dev_warn(&pdev->dev, "Soft reset not supported. "
6021fba63097SMike Miller 				"Firmware update is required.\n");
602264670ac8SStephen M. Cameron 			rc = -ENOTSUPP; /* try soft reset */
6023cf0b08d0SStephen M. Cameron 			goto unmap_cfgtable;
6024cf0b08d0SStephen M. Cameron 		}
6025cf0b08d0SStephen M. Cameron 	}
60261df8552aSStephen M. Cameron 
60271df8552aSStephen M. Cameron 	rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
60281df8552aSStephen M. Cameron 	if (rc)
60291df8552aSStephen M. Cameron 		goto unmap_cfgtable;
6030edd16368SStephen M. Cameron 
6031270d05deSStephen M. Cameron 	pci_restore_state(pdev);
6032270d05deSStephen M. Cameron 	rc = pci_enable_device(pdev);
6033270d05deSStephen M. Cameron 	if (rc) {
6034270d05deSStephen M. Cameron 		dev_warn(&pdev->dev, "failed to enable device.\n");
6035270d05deSStephen M. Cameron 		goto unmap_cfgtable;
6036edd16368SStephen M. Cameron 	}
6037270d05deSStephen M. Cameron 	pci_write_config_word(pdev, 4, command_register);
6038edd16368SStephen M. Cameron 
60391df8552aSStephen M. Cameron 	/* Some devices (notably the HP Smart Array 5i Controller)
60401df8552aSStephen M. Cameron 	   need a little pause here */
60411df8552aSStephen M. Cameron 	msleep(HPSA_POST_RESET_PAUSE_MSECS);
60421df8552aSStephen M. Cameron 
6043fe5389c8SStephen M. Cameron 	rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
6044fe5389c8SStephen M. Cameron 	if (rc) {
6045fe5389c8SStephen M. Cameron 		dev_warn(&pdev->dev,
604664670ac8SStephen M. Cameron 			"failed waiting for board to become ready "
604764670ac8SStephen M. Cameron 			"after hard reset\n");
6048fe5389c8SStephen M. Cameron 		goto unmap_cfgtable;
6049fe5389c8SStephen M. Cameron 	}
6050fe5389c8SStephen M. Cameron 
6051580ada3cSStephen M. Cameron 	rc = controller_reset_failed(vaddr);
6052580ada3cSStephen M. Cameron 	if (rc < 0)
6053580ada3cSStephen M. Cameron 		goto unmap_cfgtable;
6054580ada3cSStephen M. Cameron 	if (rc) {
605564670ac8SStephen M. Cameron 		dev_warn(&pdev->dev, "Unable to successfully reset "
605664670ac8SStephen M. Cameron 			"controller. Will try soft reset.\n");
605764670ac8SStephen M. Cameron 		rc = -ENOTSUPP;
6058580ada3cSStephen M. Cameron 	} else {
605964670ac8SStephen M. Cameron 		dev_info(&pdev->dev, "board ready after hard reset.\n");
60601df8552aSStephen M. Cameron 	}
60611df8552aSStephen M. Cameron 
60621df8552aSStephen M. Cameron unmap_cfgtable:
60631df8552aSStephen M. Cameron 	iounmap(cfgtable);
60641df8552aSStephen M. Cameron 
60651df8552aSStephen M. Cameron unmap_vaddr:
60661df8552aSStephen M. Cameron 	iounmap(vaddr);
60671df8552aSStephen M. Cameron 	return rc;
6068edd16368SStephen M. Cameron }
6069edd16368SStephen M. Cameron 
6070edd16368SStephen M. Cameron /*
6071edd16368SStephen M. Cameron  *  We cannot read the structure directly, for portability we must use
6072edd16368SStephen M. Cameron  *   the io functions.
6073edd16368SStephen M. Cameron  *   This is for debug only.
6074edd16368SStephen M. Cameron  */
6075edd16368SStephen M. Cameron static void print_cfg_table(struct device *dev, struct CfgTable *tb)
6076edd16368SStephen M. Cameron {
607758f8665cSStephen M. Cameron #ifdef HPSA_DEBUG
6078edd16368SStephen M. Cameron 	int i;
6079edd16368SStephen M. Cameron 	char temp_name[17];
6080edd16368SStephen M. Cameron 
6081edd16368SStephen M. Cameron 	dev_info(dev, "Controller Configuration information\n");
6082edd16368SStephen M. Cameron 	dev_info(dev, "------------------------------------\n");
6083edd16368SStephen M. Cameron 	for (i = 0; i < 4; i++)
6084edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->Signature[i]));
6085edd16368SStephen M. Cameron 	temp_name[4] = '\0';
6086edd16368SStephen M. Cameron 	dev_info(dev, "   Signature = %s\n", temp_name);
6087edd16368SStephen M. Cameron 	dev_info(dev, "   Spec Number = %d\n", readl(&(tb->SpecValence)));
6088edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods supported = 0x%x\n",
6089edd16368SStephen M. Cameron 	       readl(&(tb->TransportSupport)));
6090edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods active = 0x%x\n",
6091edd16368SStephen M. Cameron 	       readl(&(tb->TransportActive)));
6092edd16368SStephen M. Cameron 	dev_info(dev, "   Requested transport Method = 0x%x\n",
6093edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.TransportRequest)));
6094edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Delay = 0x%x\n",
6095edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntDelay)));
6096edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Count = 0x%x\n",
6097edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntCount)));
6098edd16368SStephen M. Cameron 	dev_info(dev, "   Max outstanding commands = 0x%d\n",
6099edd16368SStephen M. Cameron 	       readl(&(tb->CmdsOutMax)));
6100edd16368SStephen M. Cameron 	dev_info(dev, "   Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
6101edd16368SStephen M. Cameron 	for (i = 0; i < 16; i++)
6102edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->ServerName[i]));
6103edd16368SStephen M. Cameron 	temp_name[16] = '\0';
6104edd16368SStephen M. Cameron 	dev_info(dev, "   Server Name = %s\n", temp_name);
6105edd16368SStephen M. Cameron 	dev_info(dev, "   Heartbeat Counter = 0x%x\n\n\n",
6106edd16368SStephen M. Cameron 		readl(&(tb->HeartBeat)));
6107edd16368SStephen M. Cameron #endif				/* HPSA_DEBUG */
610858f8665cSStephen M. Cameron }
6109edd16368SStephen M. Cameron 
6110edd16368SStephen M. Cameron static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
6111edd16368SStephen M. Cameron {
6112edd16368SStephen M. Cameron 	int i, offset, mem_type, bar_type;
6113edd16368SStephen M. Cameron 
6114edd16368SStephen M. Cameron 	if (pci_bar_addr == PCI_BASE_ADDRESS_0)	/* looking for BAR zero? */
6115edd16368SStephen M. Cameron 		return 0;
6116edd16368SStephen M. Cameron 	offset = 0;
6117edd16368SStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
6118edd16368SStephen M. Cameron 		bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
6119edd16368SStephen M. Cameron 		if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
6120edd16368SStephen M. Cameron 			offset += 4;
6121edd16368SStephen M. Cameron 		else {
6122edd16368SStephen M. Cameron 			mem_type = pci_resource_flags(pdev, i) &
6123edd16368SStephen M. Cameron 			    PCI_BASE_ADDRESS_MEM_TYPE_MASK;
6124edd16368SStephen M. Cameron 			switch (mem_type) {
6125edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_32:
6126edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_1M:
6127edd16368SStephen M. Cameron 				offset += 4;	/* 32 bit */
6128edd16368SStephen M. Cameron 				break;
6129edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_64:
6130edd16368SStephen M. Cameron 				offset += 8;
6131edd16368SStephen M. Cameron 				break;
6132edd16368SStephen M. Cameron 			default:	/* reserved in PCI 2.2 */
6133edd16368SStephen M. Cameron 				dev_warn(&pdev->dev,
6134edd16368SStephen M. Cameron 				       "base address is invalid\n");
6135edd16368SStephen M. Cameron 				return -1;
6136edd16368SStephen M. Cameron 				break;
6137edd16368SStephen M. Cameron 			}
6138edd16368SStephen M. Cameron 		}
6139edd16368SStephen M. Cameron 		if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
6140edd16368SStephen M. Cameron 			return i + 1;
6141edd16368SStephen M. Cameron 	}
6142edd16368SStephen M. Cameron 	return -1;
6143edd16368SStephen M. Cameron }
6144edd16368SStephen M. Cameron 
6145edd16368SStephen M. Cameron /* If MSI/MSI-X is supported by the kernel we will try to enable it on
6146edd16368SStephen M. Cameron  * controllers that are capable. If not, we use IO-APIC mode.
6147edd16368SStephen M. Cameron  */
6148edd16368SStephen M. Cameron 
61496f039790SGreg Kroah-Hartman static void hpsa_interrupt_mode(struct ctlr_info *h)
6150edd16368SStephen M. Cameron {
6151edd16368SStephen M. Cameron #ifdef CONFIG_PCI_MSI
6152254f796bSMatt Gates 	int err, i;
6153254f796bSMatt Gates 	struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
6154254f796bSMatt Gates 
6155254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++) {
6156254f796bSMatt Gates 		hpsa_msix_entries[i].vector = 0;
6157254f796bSMatt Gates 		hpsa_msix_entries[i].entry = i;
6158254f796bSMatt Gates 	}
6159edd16368SStephen M. Cameron 
6160edd16368SStephen M. Cameron 	/* Some boards advertise MSI but don't really support it */
61616b3f4c52SStephen M. Cameron 	if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
61626b3f4c52SStephen M. Cameron 	    (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
6163edd16368SStephen M. Cameron 		goto default_int_mode;
616455c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
616555c06c71SStephen M. Cameron 		dev_info(&h->pdev->dev, "MSIX\n");
6166eee0f03aSHannes Reinecke 		h->msix_vector = MAX_REPLY_QUEUES;
6167254f796bSMatt Gates 		err = pci_enable_msix(h->pdev, hpsa_msix_entries,
6168eee0f03aSHannes Reinecke 				      h->msix_vector);
6169edd16368SStephen M. Cameron 		if (err > 0) {
617055c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
6171edd16368SStephen M. Cameron 			       "available\n", err);
6172eee0f03aSHannes Reinecke 			h->msix_vector = err;
6173eee0f03aSHannes Reinecke 			err = pci_enable_msix(h->pdev, hpsa_msix_entries,
6174eee0f03aSHannes Reinecke 					      h->msix_vector);
6175eee0f03aSHannes Reinecke 		}
6176eee0f03aSHannes Reinecke 		if (!err) {
6177eee0f03aSHannes Reinecke 			for (i = 0; i < h->msix_vector; i++)
6178eee0f03aSHannes Reinecke 				h->intr[i] = hpsa_msix_entries[i].vector;
6179eee0f03aSHannes Reinecke 			return;
6180edd16368SStephen M. Cameron 		} else {
618155c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "MSI-X init failed %d\n",
6182edd16368SStephen M. Cameron 			       err);
6183eee0f03aSHannes Reinecke 			h->msix_vector = 0;
6184edd16368SStephen M. Cameron 			goto default_int_mode;
6185edd16368SStephen M. Cameron 		}
6186edd16368SStephen M. Cameron 	}
618755c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
618855c06c71SStephen M. Cameron 		dev_info(&h->pdev->dev, "MSI\n");
618955c06c71SStephen M. Cameron 		if (!pci_enable_msi(h->pdev))
6190edd16368SStephen M. Cameron 			h->msi_vector = 1;
6191edd16368SStephen M. Cameron 		else
619255c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "MSI init failed\n");
6193edd16368SStephen M. Cameron 	}
6194edd16368SStephen M. Cameron default_int_mode:
6195edd16368SStephen M. Cameron #endif				/* CONFIG_PCI_MSI */
6196edd16368SStephen M. Cameron 	/* if we get here we're going to use the default interrupt mode */
6197a9a3a273SStephen M. Cameron 	h->intr[h->intr_mode] = h->pdev->irq;
6198edd16368SStephen M. Cameron }
6199edd16368SStephen M. Cameron 
62006f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
6201e5c880d1SStephen M. Cameron {
6202e5c880d1SStephen M. Cameron 	int i;
6203e5c880d1SStephen M. Cameron 	u32 subsystem_vendor_id, subsystem_device_id;
6204e5c880d1SStephen M. Cameron 
6205e5c880d1SStephen M. Cameron 	subsystem_vendor_id = pdev->subsystem_vendor;
6206e5c880d1SStephen M. Cameron 	subsystem_device_id = pdev->subsystem_device;
6207e5c880d1SStephen M. Cameron 	*board_id = ((subsystem_device_id << 16) & 0xffff0000) |
6208e5c880d1SStephen M. Cameron 		    subsystem_vendor_id;
6209e5c880d1SStephen M. Cameron 
6210e5c880d1SStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(products); i++)
6211e5c880d1SStephen M. Cameron 		if (*board_id == products[i].board_id)
6212e5c880d1SStephen M. Cameron 			return i;
6213e5c880d1SStephen M. Cameron 
62146798cc0aSStephen M. Cameron 	if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
62156798cc0aSStephen M. Cameron 		subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
62166798cc0aSStephen M. Cameron 		!hpsa_allow_any) {
6217e5c880d1SStephen M. Cameron 		dev_warn(&pdev->dev, "unrecognized board ID: "
6218e5c880d1SStephen M. Cameron 			"0x%08x, ignoring.\n", *board_id);
6219e5c880d1SStephen M. Cameron 			return -ENODEV;
6220e5c880d1SStephen M. Cameron 	}
6221e5c880d1SStephen M. Cameron 	return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
6222e5c880d1SStephen M. Cameron }
6223e5c880d1SStephen M. Cameron 
62246f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
62253a7774ceSStephen M. Cameron 				    unsigned long *memory_bar)
62263a7774ceSStephen M. Cameron {
62273a7774ceSStephen M. Cameron 	int i;
62283a7774ceSStephen M. Cameron 
62293a7774ceSStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
623012d2cd47SStephen M. Cameron 		if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
62313a7774ceSStephen M. Cameron 			/* addressing mode bits already removed */
623212d2cd47SStephen M. Cameron 			*memory_bar = pci_resource_start(pdev, i);
623312d2cd47SStephen M. Cameron 			dev_dbg(&pdev->dev, "memory BAR = %lx\n",
62343a7774ceSStephen M. Cameron 				*memory_bar);
62353a7774ceSStephen M. Cameron 			return 0;
62363a7774ceSStephen M. Cameron 		}
623712d2cd47SStephen M. Cameron 	dev_warn(&pdev->dev, "no memory BAR found\n");
62383a7774ceSStephen M. Cameron 	return -ENODEV;
62393a7774ceSStephen M. Cameron }
62403a7774ceSStephen M. Cameron 
62416f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
62426f039790SGreg Kroah-Hartman 				     int wait_for_ready)
62432c4c8c8bSStephen M. Cameron {
6244fe5389c8SStephen M. Cameron 	int i, iterations;
62452c4c8c8bSStephen M. Cameron 	u32 scratchpad;
6246fe5389c8SStephen M. Cameron 	if (wait_for_ready)
6247fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_READY_ITERATIONS;
6248fe5389c8SStephen M. Cameron 	else
6249fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
62502c4c8c8bSStephen M. Cameron 
6251fe5389c8SStephen M. Cameron 	for (i = 0; i < iterations; i++) {
6252fe5389c8SStephen M. Cameron 		scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
6253fe5389c8SStephen M. Cameron 		if (wait_for_ready) {
62542c4c8c8bSStephen M. Cameron 			if (scratchpad == HPSA_FIRMWARE_READY)
62552c4c8c8bSStephen M. Cameron 				return 0;
6256fe5389c8SStephen M. Cameron 		} else {
6257fe5389c8SStephen M. Cameron 			if (scratchpad != HPSA_FIRMWARE_READY)
6258fe5389c8SStephen M. Cameron 				return 0;
6259fe5389c8SStephen M. Cameron 		}
62602c4c8c8bSStephen M. Cameron 		msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
62612c4c8c8bSStephen M. Cameron 	}
6262fe5389c8SStephen M. Cameron 	dev_warn(&pdev->dev, "board not ready, timed out.\n");
62632c4c8c8bSStephen M. Cameron 	return -ENODEV;
62642c4c8c8bSStephen M. Cameron }
62652c4c8c8bSStephen M. Cameron 
62666f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
62676f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
6268a51fd47fSStephen M. Cameron 			       u64 *cfg_offset)
6269a51fd47fSStephen M. Cameron {
6270a51fd47fSStephen M. Cameron 	*cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
6271a51fd47fSStephen M. Cameron 	*cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
6272a51fd47fSStephen M. Cameron 	*cfg_base_addr &= (u32) 0x0000ffff;
6273a51fd47fSStephen M. Cameron 	*cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
6274a51fd47fSStephen M. Cameron 	if (*cfg_base_addr_index == -1) {
6275a51fd47fSStephen M. Cameron 		dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
6276a51fd47fSStephen M. Cameron 		return -ENODEV;
6277a51fd47fSStephen M. Cameron 	}
6278a51fd47fSStephen M. Cameron 	return 0;
6279a51fd47fSStephen M. Cameron }
6280a51fd47fSStephen M. Cameron 
62816f039790SGreg Kroah-Hartman static int hpsa_find_cfgtables(struct ctlr_info *h)
6282edd16368SStephen M. Cameron {
628301a02ffcSStephen M. Cameron 	u64 cfg_offset;
628401a02ffcSStephen M. Cameron 	u32 cfg_base_addr;
628501a02ffcSStephen M. Cameron 	u64 cfg_base_addr_index;
6286303932fdSDon Brace 	u32 trans_offset;
6287a51fd47fSStephen M. Cameron 	int rc;
628877c4495cSStephen M. Cameron 
6289a51fd47fSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
6290a51fd47fSStephen M. Cameron 		&cfg_base_addr_index, &cfg_offset);
6291a51fd47fSStephen M. Cameron 	if (rc)
6292a51fd47fSStephen M. Cameron 		return rc;
629377c4495cSStephen M. Cameron 	h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
6294a51fd47fSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
629577c4495cSStephen M. Cameron 	if (!h->cfgtable)
629677c4495cSStephen M. Cameron 		return -ENOMEM;
6297580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(h->cfgtable);
6298580ada3cSStephen M. Cameron 	if (rc)
6299580ada3cSStephen M. Cameron 		return rc;
630077c4495cSStephen M. Cameron 	/* Find performant mode table. */
6301a51fd47fSStephen M. Cameron 	trans_offset = readl(&h->cfgtable->TransMethodOffset);
630277c4495cSStephen M. Cameron 	h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
630377c4495cSStephen M. Cameron 				cfg_base_addr_index)+cfg_offset+trans_offset,
630477c4495cSStephen M. Cameron 				sizeof(*h->transtable));
630577c4495cSStephen M. Cameron 	if (!h->transtable)
630677c4495cSStephen M. Cameron 		return -ENOMEM;
630777c4495cSStephen M. Cameron 	return 0;
630877c4495cSStephen M. Cameron }
630977c4495cSStephen M. Cameron 
63106f039790SGreg Kroah-Hartman static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
6311cba3d38bSStephen M. Cameron {
6312cba3d38bSStephen M. Cameron 	h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
631372ceeaecSStephen M. Cameron 
631472ceeaecSStephen M. Cameron 	/* Limit commands in memory limited kdump scenario. */
631572ceeaecSStephen M. Cameron 	if (reset_devices && h->max_commands > 32)
631672ceeaecSStephen M. Cameron 		h->max_commands = 32;
631772ceeaecSStephen M. Cameron 
6318cba3d38bSStephen M. Cameron 	if (h->max_commands < 16) {
6319cba3d38bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "Controller reports "
6320cba3d38bSStephen M. Cameron 			"max supported commands of %d, an obvious lie. "
6321cba3d38bSStephen M. Cameron 			"Using 16.  Ensure that firmware is up to date.\n",
6322cba3d38bSStephen M. Cameron 			h->max_commands);
6323cba3d38bSStephen M. Cameron 		h->max_commands = 16;
6324cba3d38bSStephen M. Cameron 	}
6325cba3d38bSStephen M. Cameron }
6326cba3d38bSStephen M. Cameron 
6327b93d7536SStephen M. Cameron /* Interrogate the hardware for some limits:
6328b93d7536SStephen M. Cameron  * max commands, max SG elements without chaining, and with chaining,
6329b93d7536SStephen M. Cameron  * SG chain block size, etc.
6330b93d7536SStephen M. Cameron  */
63316f039790SGreg Kroah-Hartman static void hpsa_find_board_params(struct ctlr_info *h)
6332b93d7536SStephen M. Cameron {
6333cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
6334b93d7536SStephen M. Cameron 	h->nr_cmds = h->max_commands - 4; /* Allow room for some ioctls */
6335b93d7536SStephen M. Cameron 	h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
6336283b4a9bSStephen M. Cameron 	h->fw_support = readl(&(h->cfgtable->misc_fw_support));
6337b93d7536SStephen M. Cameron 	/*
6338b93d7536SStephen M. Cameron 	 * Limit in-command s/g elements to 32 save dma'able memory.
6339b93d7536SStephen M. Cameron 	 * Howvever spec says if 0, use 31
6340b93d7536SStephen M. Cameron 	 */
6341b93d7536SStephen M. Cameron 	h->max_cmd_sg_entries = 31;
6342b93d7536SStephen M. Cameron 	if (h->maxsgentries > 512) {
6343b93d7536SStephen M. Cameron 		h->max_cmd_sg_entries = 32;
6344b93d7536SStephen M. Cameron 		h->chainsize = h->maxsgentries - h->max_cmd_sg_entries + 1;
6345b93d7536SStephen M. Cameron 		h->maxsgentries--; /* save one for chain pointer */
6346b93d7536SStephen M. Cameron 	} else {
6347b93d7536SStephen M. Cameron 		h->maxsgentries = 31; /* default to traditional values */
6348b93d7536SStephen M. Cameron 		h->chainsize = 0;
6349b93d7536SStephen M. Cameron 	}
635075167d2cSStephen M. Cameron 
635175167d2cSStephen M. Cameron 	/* Find out what task management functions are supported and cache */
635275167d2cSStephen M. Cameron 	h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
63530e7a7fceSScott Teel 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
63540e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
63550e7a7fceSScott Teel 	if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
63560e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
6357b93d7536SStephen M. Cameron }
6358b93d7536SStephen M. Cameron 
635976c46e49SStephen M. Cameron static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
636076c46e49SStephen M. Cameron {
63610fc9fd40SAkinobu Mita 	if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
636276c46e49SStephen M. Cameron 		dev_warn(&h->pdev->dev, "not a valid CISS config table\n");
636376c46e49SStephen M. Cameron 		return false;
636476c46e49SStephen M. Cameron 	}
636576c46e49SStephen M. Cameron 	return true;
636676c46e49SStephen M. Cameron }
636776c46e49SStephen M. Cameron 
636897a5e98cSStephen M. Cameron static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
6369f7c39101SStephen M. Cameron {
637097a5e98cSStephen M. Cameron 	u32 driver_support;
6371f7c39101SStephen M. Cameron 
637228e13446SStephen M. Cameron #ifdef CONFIG_X86
637328e13446SStephen M. Cameron 	/* Need to enable prefetch in the SCSI core for 6400 in x86 */
637497a5e98cSStephen M. Cameron 	driver_support = readl(&(h->cfgtable->driver_support));
637597a5e98cSStephen M. Cameron 	driver_support |= ENABLE_SCSI_PREFETCH;
6376f7c39101SStephen M. Cameron #endif
637728e13446SStephen M. Cameron 	driver_support |= ENABLE_UNIT_ATTN;
637828e13446SStephen M. Cameron 	writel(driver_support, &(h->cfgtable->driver_support));
6379f7c39101SStephen M. Cameron }
6380f7c39101SStephen M. Cameron 
63813d0eab67SStephen M. Cameron /* Disable DMA prefetch for the P600.  Otherwise an ASIC bug may result
63823d0eab67SStephen M. Cameron  * in a prefetch beyond physical memory.
63833d0eab67SStephen M. Cameron  */
63843d0eab67SStephen M. Cameron static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
63853d0eab67SStephen M. Cameron {
63863d0eab67SStephen M. Cameron 	u32 dma_prefetch;
63873d0eab67SStephen M. Cameron 
63883d0eab67SStephen M. Cameron 	if (h->board_id != 0x3225103C)
63893d0eab67SStephen M. Cameron 		return;
63903d0eab67SStephen M. Cameron 	dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
63913d0eab67SStephen M. Cameron 	dma_prefetch |= 0x8000;
63923d0eab67SStephen M. Cameron 	writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
63933d0eab67SStephen M. Cameron }
63943d0eab67SStephen M. Cameron 
639576438d08SStephen M. Cameron static void hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
639676438d08SStephen M. Cameron {
639776438d08SStephen M. Cameron 	int i;
639876438d08SStephen M. Cameron 	u32 doorbell_value;
639976438d08SStephen M. Cameron 	unsigned long flags;
640076438d08SStephen M. Cameron 	/* wait until the clear_event_notify bit 6 is cleared by controller. */
640176438d08SStephen M. Cameron 	for (i = 0; i < MAX_CONFIG_WAIT; i++) {
640276438d08SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
640376438d08SStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
640476438d08SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
640576438d08SStephen M. Cameron 		if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
640676438d08SStephen M. Cameron 			break;
640776438d08SStephen M. Cameron 		/* delay and try again */
640876438d08SStephen M. Cameron 		msleep(20);
640976438d08SStephen M. Cameron 	}
641076438d08SStephen M. Cameron }
641176438d08SStephen M. Cameron 
64126f039790SGreg Kroah-Hartman static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
6413eb6b2ae9SStephen M. Cameron {
6414eb6b2ae9SStephen M. Cameron 	int i;
64156eaf46fdSStephen M. Cameron 	u32 doorbell_value;
64166eaf46fdSStephen M. Cameron 	unsigned long flags;
6417eb6b2ae9SStephen M. Cameron 
6418eb6b2ae9SStephen M. Cameron 	/* under certain very rare conditions, this can take awhile.
6419eb6b2ae9SStephen M. Cameron 	 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
6420eb6b2ae9SStephen M. Cameron 	 * as we enter this code.)
6421eb6b2ae9SStephen M. Cameron 	 */
6422eb6b2ae9SStephen M. Cameron 	for (i = 0; i < MAX_CONFIG_WAIT; i++) {
64236eaf46fdSStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
64246eaf46fdSStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
64256eaf46fdSStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
6426382be668SDan Carpenter 		if (!(doorbell_value & CFGTBL_ChangeReq))
6427eb6b2ae9SStephen M. Cameron 			break;
6428eb6b2ae9SStephen M. Cameron 		/* delay and try again */
642960d3f5b0SStephen M. Cameron 		usleep_range(10000, 20000);
6430eb6b2ae9SStephen M. Cameron 	}
64313f4336f3SStephen M. Cameron }
64323f4336f3SStephen M. Cameron 
64336f039790SGreg Kroah-Hartman static int hpsa_enter_simple_mode(struct ctlr_info *h)
64343f4336f3SStephen M. Cameron {
64353f4336f3SStephen M. Cameron 	u32 trans_support;
64363f4336f3SStephen M. Cameron 
64373f4336f3SStephen M. Cameron 	trans_support = readl(&(h->cfgtable->TransportSupport));
64383f4336f3SStephen M. Cameron 	if (!(trans_support & SIMPLE_MODE))
64393f4336f3SStephen M. Cameron 		return -ENOTSUPP;
64403f4336f3SStephen M. Cameron 
64413f4336f3SStephen M. Cameron 	h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
6442283b4a9bSStephen M. Cameron 
64433f4336f3SStephen M. Cameron 	/* Update the field, and then ring the doorbell */
64443f4336f3SStephen M. Cameron 	writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
6445b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
64463f4336f3SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
64473f4336f3SStephen M. Cameron 	hpsa_wait_for_mode_change_ack(h);
6448eb6b2ae9SStephen M. Cameron 	print_cfg_table(&h->pdev->dev, h->cfgtable);
6449283b4a9bSStephen M. Cameron 	if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
6450283b4a9bSStephen M. Cameron 		goto error;
6451960a30e7SStephen M. Cameron 	h->transMethod = CFGTBL_Trans_Simple;
6452eb6b2ae9SStephen M. Cameron 	return 0;
6453283b4a9bSStephen M. Cameron error:
6454283b4a9bSStephen M. Cameron 	dev_warn(&h->pdev->dev, "unable to get board into simple mode\n");
6455283b4a9bSStephen M. Cameron 	return -ENODEV;
6456eb6b2ae9SStephen M. Cameron }
6457eb6b2ae9SStephen M. Cameron 
64586f039790SGreg Kroah-Hartman static int hpsa_pci_init(struct ctlr_info *h)
645977c4495cSStephen M. Cameron {
6460eb6b2ae9SStephen M. Cameron 	int prod_index, err;
6461edd16368SStephen M. Cameron 
6462e5c880d1SStephen M. Cameron 	prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
6463e5c880d1SStephen M. Cameron 	if (prod_index < 0)
6464edd16368SStephen M. Cameron 		return -ENODEV;
6465e5c880d1SStephen M. Cameron 	h->product_name = products[prod_index].product_name;
6466e5c880d1SStephen M. Cameron 	h->access = *(products[prod_index].access);
6467e5c880d1SStephen M. Cameron 
6468e5a44df8SMatthew Garrett 	pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
6469e5a44df8SMatthew Garrett 			       PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
6470e5a44df8SMatthew Garrett 
647155c06c71SStephen M. Cameron 	err = pci_enable_device(h->pdev);
6472edd16368SStephen M. Cameron 	if (err) {
647355c06c71SStephen M. Cameron 		dev_warn(&h->pdev->dev, "unable to enable PCI device\n");
6474edd16368SStephen M. Cameron 		return err;
6475edd16368SStephen M. Cameron 	}
6476edd16368SStephen M. Cameron 
64775cb460a6SStephen M. Cameron 	/* Enable bus mastering (pci_disable_device may disable this) */
64785cb460a6SStephen M. Cameron 	pci_set_master(h->pdev);
64795cb460a6SStephen M. Cameron 
6480f79cfec6SStephen M. Cameron 	err = pci_request_regions(h->pdev, HPSA);
6481edd16368SStephen M. Cameron 	if (err) {
648255c06c71SStephen M. Cameron 		dev_err(&h->pdev->dev,
648355c06c71SStephen M. Cameron 			"cannot obtain PCI resources, aborting\n");
6484edd16368SStephen M. Cameron 		return err;
6485edd16368SStephen M. Cameron 	}
64866b3f4c52SStephen M. Cameron 	hpsa_interrupt_mode(h);
648712d2cd47SStephen M. Cameron 	err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
64883a7774ceSStephen M. Cameron 	if (err)
6489edd16368SStephen M. Cameron 		goto err_out_free_res;
6490edd16368SStephen M. Cameron 	h->vaddr = remap_pci_mem(h->paddr, 0x250);
6491204892e9SStephen M. Cameron 	if (!h->vaddr) {
6492204892e9SStephen M. Cameron 		err = -ENOMEM;
6493204892e9SStephen M. Cameron 		goto err_out_free_res;
6494204892e9SStephen M. Cameron 	}
6495fe5389c8SStephen M. Cameron 	err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
64962c4c8c8bSStephen M. Cameron 	if (err)
6497edd16368SStephen M. Cameron 		goto err_out_free_res;
649877c4495cSStephen M. Cameron 	err = hpsa_find_cfgtables(h);
649977c4495cSStephen M. Cameron 	if (err)
6500edd16368SStephen M. Cameron 		goto err_out_free_res;
6501b93d7536SStephen M. Cameron 	hpsa_find_board_params(h);
6502edd16368SStephen M. Cameron 
650376c46e49SStephen M. Cameron 	if (!hpsa_CISS_signature_present(h)) {
6504edd16368SStephen M. Cameron 		err = -ENODEV;
6505edd16368SStephen M. Cameron 		goto err_out_free_res;
6506edd16368SStephen M. Cameron 	}
650797a5e98cSStephen M. Cameron 	hpsa_set_driver_support_bits(h);
65083d0eab67SStephen M. Cameron 	hpsa_p600_dma_prefetch_quirk(h);
6509eb6b2ae9SStephen M. Cameron 	err = hpsa_enter_simple_mode(h);
6510eb6b2ae9SStephen M. Cameron 	if (err)
6511edd16368SStephen M. Cameron 		goto err_out_free_res;
6512edd16368SStephen M. Cameron 	return 0;
6513edd16368SStephen M. Cameron 
6514edd16368SStephen M. Cameron err_out_free_res:
6515204892e9SStephen M. Cameron 	if (h->transtable)
6516204892e9SStephen M. Cameron 		iounmap(h->transtable);
6517204892e9SStephen M. Cameron 	if (h->cfgtable)
6518204892e9SStephen M. Cameron 		iounmap(h->cfgtable);
6519204892e9SStephen M. Cameron 	if (h->vaddr)
6520204892e9SStephen M. Cameron 		iounmap(h->vaddr);
6521f0bd0b68SStephen M. Cameron 	pci_disable_device(h->pdev);
652255c06c71SStephen M. Cameron 	pci_release_regions(h->pdev);
6523edd16368SStephen M. Cameron 	return err;
6524edd16368SStephen M. Cameron }
6525edd16368SStephen M. Cameron 
65266f039790SGreg Kroah-Hartman static void hpsa_hba_inquiry(struct ctlr_info *h)
6527339b2b14SStephen M. Cameron {
6528339b2b14SStephen M. Cameron 	int rc;
6529339b2b14SStephen M. Cameron 
6530339b2b14SStephen M. Cameron #define HBA_INQUIRY_BYTE_COUNT 64
6531339b2b14SStephen M. Cameron 	h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
6532339b2b14SStephen M. Cameron 	if (!h->hba_inquiry_data)
6533339b2b14SStephen M. Cameron 		return;
6534339b2b14SStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
6535339b2b14SStephen M. Cameron 		h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
6536339b2b14SStephen M. Cameron 	if (rc != 0) {
6537339b2b14SStephen M. Cameron 		kfree(h->hba_inquiry_data);
6538339b2b14SStephen M. Cameron 		h->hba_inquiry_data = NULL;
6539339b2b14SStephen M. Cameron 	}
6540339b2b14SStephen M. Cameron }
6541339b2b14SStephen M. Cameron 
65426f039790SGreg Kroah-Hartman static int hpsa_init_reset_devices(struct pci_dev *pdev)
6543edd16368SStephen M. Cameron {
65441df8552aSStephen M. Cameron 	int rc, i;
6545edd16368SStephen M. Cameron 
65464c2a8c40SStephen M. Cameron 	if (!reset_devices)
65474c2a8c40SStephen M. Cameron 		return 0;
65484c2a8c40SStephen M. Cameron 
65491df8552aSStephen M. Cameron 	/* Reset the controller with a PCI power-cycle or via doorbell */
65501df8552aSStephen M. Cameron 	rc = hpsa_kdump_hard_reset_controller(pdev);
6551edd16368SStephen M. Cameron 
65521df8552aSStephen M. Cameron 	/* -ENOTSUPP here means we cannot reset the controller
65531df8552aSStephen M. Cameron 	 * but it's already (and still) up and running in
655418867659SStephen M. Cameron 	 * "performant mode".  Or, it might be 640x, which can't reset
655518867659SStephen M. Cameron 	 * due to concerns about shared bbwc between 6402/6404 pair.
65561df8552aSStephen M. Cameron 	 */
65571df8552aSStephen M. Cameron 	if (rc == -ENOTSUPP)
655864670ac8SStephen M. Cameron 		return rc; /* just try to do the kdump anyhow. */
65591df8552aSStephen M. Cameron 	if (rc)
65601df8552aSStephen M. Cameron 		return -ENODEV;
6561edd16368SStephen M. Cameron 
6562edd16368SStephen M. Cameron 	/* Now try to get the controller to respond to a no-op */
65632b870cb3SStephen M. Cameron 	dev_warn(&pdev->dev, "Waiting for controller to respond to no-op\n");
6564edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
6565edd16368SStephen M. Cameron 		if (hpsa_noop(pdev) == 0)
6566edd16368SStephen M. Cameron 			break;
6567edd16368SStephen M. Cameron 		else
6568edd16368SStephen M. Cameron 			dev_warn(&pdev->dev, "no-op failed%s\n",
6569edd16368SStephen M. Cameron 					(i < 11 ? "; re-trying" : ""));
6570edd16368SStephen M. Cameron 	}
65714c2a8c40SStephen M. Cameron 	return 0;
6572edd16368SStephen M. Cameron }
6573edd16368SStephen M. Cameron 
65746f039790SGreg Kroah-Hartman static int hpsa_allocate_cmd_pool(struct ctlr_info *h)
65752e9d1b36SStephen M. Cameron {
65762e9d1b36SStephen M. Cameron 	h->cmd_pool_bits = kzalloc(
65772e9d1b36SStephen M. Cameron 		DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
65782e9d1b36SStephen M. Cameron 		sizeof(unsigned long), GFP_KERNEL);
65792e9d1b36SStephen M. Cameron 	h->cmd_pool = pci_alloc_consistent(h->pdev,
65802e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->cmd_pool),
65812e9d1b36SStephen M. Cameron 		    &(h->cmd_pool_dhandle));
65822e9d1b36SStephen M. Cameron 	h->errinfo_pool = pci_alloc_consistent(h->pdev,
65832e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->errinfo_pool),
65842e9d1b36SStephen M. Cameron 		    &(h->errinfo_pool_dhandle));
65852e9d1b36SStephen M. Cameron 	if ((h->cmd_pool_bits == NULL)
65862e9d1b36SStephen M. Cameron 	    || (h->cmd_pool == NULL)
65872e9d1b36SStephen M. Cameron 	    || (h->errinfo_pool == NULL)) {
65882e9d1b36SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory in %s", __func__);
65892e9d1b36SStephen M. Cameron 		return -ENOMEM;
65902e9d1b36SStephen M. Cameron 	}
65912e9d1b36SStephen M. Cameron 	return 0;
65922e9d1b36SStephen M. Cameron }
65932e9d1b36SStephen M. Cameron 
65942e9d1b36SStephen M. Cameron static void hpsa_free_cmd_pool(struct ctlr_info *h)
65952e9d1b36SStephen M. Cameron {
65962e9d1b36SStephen M. Cameron 	kfree(h->cmd_pool_bits);
65972e9d1b36SStephen M. Cameron 	if (h->cmd_pool)
65982e9d1b36SStephen M. Cameron 		pci_free_consistent(h->pdev,
65992e9d1b36SStephen M. Cameron 			    h->nr_cmds * sizeof(struct CommandList),
66002e9d1b36SStephen M. Cameron 			    h->cmd_pool, h->cmd_pool_dhandle);
6601aca9012aSStephen M. Cameron 	if (h->ioaccel2_cmd_pool)
6602aca9012aSStephen M. Cameron 		pci_free_consistent(h->pdev,
6603aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
6604aca9012aSStephen M. Cameron 			h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
66052e9d1b36SStephen M. Cameron 	if (h->errinfo_pool)
66062e9d1b36SStephen M. Cameron 		pci_free_consistent(h->pdev,
66072e9d1b36SStephen M. Cameron 			    h->nr_cmds * sizeof(struct ErrorInfo),
66082e9d1b36SStephen M. Cameron 			    h->errinfo_pool,
66092e9d1b36SStephen M. Cameron 			    h->errinfo_pool_dhandle);
6610e1f7de0cSMatt Gates 	if (h->ioaccel_cmd_pool)
6611e1f7de0cSMatt Gates 		pci_free_consistent(h->pdev,
6612e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(struct io_accel1_cmd),
6613e1f7de0cSMatt Gates 			h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
66142e9d1b36SStephen M. Cameron }
66152e9d1b36SStephen M. Cameron 
66160ae01a32SStephen M. Cameron static int hpsa_request_irq(struct ctlr_info *h,
66170ae01a32SStephen M. Cameron 	irqreturn_t (*msixhandler)(int, void *),
66180ae01a32SStephen M. Cameron 	irqreturn_t (*intxhandler)(int, void *))
66190ae01a32SStephen M. Cameron {
6620254f796bSMatt Gates 	int rc, i;
66210ae01a32SStephen M. Cameron 
6622254f796bSMatt Gates 	/*
6623254f796bSMatt Gates 	 * initialize h->q[x] = x so that interrupt handlers know which
6624254f796bSMatt Gates 	 * queue to process.
6625254f796bSMatt Gates 	 */
6626254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++)
6627254f796bSMatt Gates 		h->q[i] = (u8) i;
6628254f796bSMatt Gates 
6629eee0f03aSHannes Reinecke 	if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
6630254f796bSMatt Gates 		/* If performant mode and MSI-X, use multiple reply queues */
6631eee0f03aSHannes Reinecke 		for (i = 0; i < h->msix_vector; i++)
6632254f796bSMatt Gates 			rc = request_irq(h->intr[i], msixhandler,
6633254f796bSMatt Gates 					0, h->devname,
6634254f796bSMatt Gates 					&h->q[i]);
6635254f796bSMatt Gates 	} else {
6636254f796bSMatt Gates 		/* Use single reply pool */
6637eee0f03aSHannes Reinecke 		if (h->msix_vector > 0 || h->msi_vector) {
6638254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
6639254f796bSMatt Gates 				msixhandler, 0, h->devname,
6640254f796bSMatt Gates 				&h->q[h->intr_mode]);
6641254f796bSMatt Gates 		} else {
6642254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
6643254f796bSMatt Gates 				intxhandler, IRQF_SHARED, h->devname,
6644254f796bSMatt Gates 				&h->q[h->intr_mode]);
6645254f796bSMatt Gates 		}
6646254f796bSMatt Gates 	}
66470ae01a32SStephen M. Cameron 	if (rc) {
66480ae01a32SStephen M. Cameron 		dev_err(&h->pdev->dev, "unable to get irq %d for %s\n",
66490ae01a32SStephen M. Cameron 		       h->intr[h->intr_mode], h->devname);
66500ae01a32SStephen M. Cameron 		return -ENODEV;
66510ae01a32SStephen M. Cameron 	}
66520ae01a32SStephen M. Cameron 	return 0;
66530ae01a32SStephen M. Cameron }
66540ae01a32SStephen M. Cameron 
66556f039790SGreg Kroah-Hartman static int hpsa_kdump_soft_reset(struct ctlr_info *h)
665664670ac8SStephen M. Cameron {
665764670ac8SStephen M. Cameron 	if (hpsa_send_host_reset(h, RAID_CTLR_LUNID,
665864670ac8SStephen M. Cameron 		HPSA_RESET_TYPE_CONTROLLER)) {
665964670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Resetting array controller failed.\n");
666064670ac8SStephen M. Cameron 		return -EIO;
666164670ac8SStephen M. Cameron 	}
666264670ac8SStephen M. Cameron 
666364670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
666464670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) {
666564670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
666664670ac8SStephen M. Cameron 		return -1;
666764670ac8SStephen M. Cameron 	}
666864670ac8SStephen M. Cameron 
666964670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
667064670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) {
667164670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Board failed to become ready "
667264670ac8SStephen M. Cameron 			"after soft reset.\n");
667364670ac8SStephen M. Cameron 		return -1;
667464670ac8SStephen M. Cameron 	}
667564670ac8SStephen M. Cameron 
667664670ac8SStephen M. Cameron 	return 0;
667764670ac8SStephen M. Cameron }
667864670ac8SStephen M. Cameron 
6679254f796bSMatt Gates static void free_irqs(struct ctlr_info *h)
6680254f796bSMatt Gates {
6681254f796bSMatt Gates 	int i;
6682254f796bSMatt Gates 
6683254f796bSMatt Gates 	if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
6684254f796bSMatt Gates 		/* Single reply queue, only one irq to free */
6685254f796bSMatt Gates 		i = h->intr_mode;
6686254f796bSMatt Gates 		free_irq(h->intr[i], &h->q[i]);
6687254f796bSMatt Gates 		return;
6688254f796bSMatt Gates 	}
6689254f796bSMatt Gates 
6690eee0f03aSHannes Reinecke 	for (i = 0; i < h->msix_vector; i++)
6691254f796bSMatt Gates 		free_irq(h->intr[i], &h->q[i]);
6692254f796bSMatt Gates }
6693254f796bSMatt Gates 
66940097f0f4SStephen M. Cameron static void hpsa_free_irqs_and_disable_msix(struct ctlr_info *h)
669564670ac8SStephen M. Cameron {
6696254f796bSMatt Gates 	free_irqs(h);
669764670ac8SStephen M. Cameron #ifdef CONFIG_PCI_MSI
66980097f0f4SStephen M. Cameron 	if (h->msix_vector) {
66990097f0f4SStephen M. Cameron 		if (h->pdev->msix_enabled)
670064670ac8SStephen M. Cameron 			pci_disable_msix(h->pdev);
67010097f0f4SStephen M. Cameron 	} else if (h->msi_vector) {
67020097f0f4SStephen M. Cameron 		if (h->pdev->msi_enabled)
670364670ac8SStephen M. Cameron 			pci_disable_msi(h->pdev);
67040097f0f4SStephen M. Cameron 	}
670564670ac8SStephen M. Cameron #endif /* CONFIG_PCI_MSI */
67060097f0f4SStephen M. Cameron }
67070097f0f4SStephen M. Cameron 
67080097f0f4SStephen M. Cameron static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
67090097f0f4SStephen M. Cameron {
67100097f0f4SStephen M. Cameron 	hpsa_free_irqs_and_disable_msix(h);
671164670ac8SStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
671264670ac8SStephen M. Cameron 	hpsa_free_cmd_pool(h);
6713e1f7de0cSMatt Gates 	kfree(h->ioaccel1_blockFetchTable);
671464670ac8SStephen M. Cameron 	kfree(h->blockFetchTable);
671564670ac8SStephen M. Cameron 	pci_free_consistent(h->pdev, h->reply_pool_size,
671664670ac8SStephen M. Cameron 		h->reply_pool, h->reply_pool_dhandle);
671764670ac8SStephen M. Cameron 	if (h->vaddr)
671864670ac8SStephen M. Cameron 		iounmap(h->vaddr);
671964670ac8SStephen M. Cameron 	if (h->transtable)
672064670ac8SStephen M. Cameron 		iounmap(h->transtable);
672164670ac8SStephen M. Cameron 	if (h->cfgtable)
672264670ac8SStephen M. Cameron 		iounmap(h->cfgtable);
672364670ac8SStephen M. Cameron 	pci_release_regions(h->pdev);
672464670ac8SStephen M. Cameron 	kfree(h);
672564670ac8SStephen M. Cameron }
672664670ac8SStephen M. Cameron 
6727a0c12413SStephen M. Cameron /* Called when controller lockup detected. */
6728a0c12413SStephen M. Cameron static void fail_all_cmds_on_list(struct ctlr_info *h, struct list_head *list)
6729a0c12413SStephen M. Cameron {
6730a0c12413SStephen M. Cameron 	struct CommandList *c = NULL;
6731a0c12413SStephen M. Cameron 
6732a0c12413SStephen M. Cameron 	assert_spin_locked(&h->lock);
6733a0c12413SStephen M. Cameron 	/* Mark all outstanding commands as failed and complete them. */
6734a0c12413SStephen M. Cameron 	while (!list_empty(list)) {
6735a0c12413SStephen M. Cameron 		c = list_entry(list->next, struct CommandList, list);
6736a0c12413SStephen M. Cameron 		c->err_info->CommandStatus = CMD_HARDWARE_ERR;
67375a3d16f5SStephen M. Cameron 		finish_cmd(c);
6738a0c12413SStephen M. Cameron 	}
6739a0c12413SStephen M. Cameron }
6740a0c12413SStephen M. Cameron 
6741a0c12413SStephen M. Cameron static void controller_lockup_detected(struct ctlr_info *h)
6742a0c12413SStephen M. Cameron {
6743a0c12413SStephen M. Cameron 	unsigned long flags;
6744a0c12413SStephen M. Cameron 
6745a0c12413SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
6746a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
6747a0c12413SStephen M. Cameron 	h->lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
6748a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
6749a0c12413SStephen M. Cameron 	dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x\n",
6750a0c12413SStephen M. Cameron 			h->lockup_detected);
6751a0c12413SStephen M. Cameron 	pci_disable_device(h->pdev);
6752a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
6753a0c12413SStephen M. Cameron 	fail_all_cmds_on_list(h, &h->cmpQ);
6754a0c12413SStephen M. Cameron 	fail_all_cmds_on_list(h, &h->reqQ);
6755a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
6756a0c12413SStephen M. Cameron }
6757a0c12413SStephen M. Cameron 
6758a0c12413SStephen M. Cameron static void detect_controller_lockup(struct ctlr_info *h)
6759a0c12413SStephen M. Cameron {
6760a0c12413SStephen M. Cameron 	u64 now;
6761a0c12413SStephen M. Cameron 	u32 heartbeat;
6762a0c12413SStephen M. Cameron 	unsigned long flags;
6763a0c12413SStephen M. Cameron 
6764a0c12413SStephen M. Cameron 	now = get_jiffies_64();
6765a0c12413SStephen M. Cameron 	/* If we've received an interrupt recently, we're ok. */
6766a0c12413SStephen M. Cameron 	if (time_after64(h->last_intr_timestamp +
6767e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
6768a0c12413SStephen M. Cameron 		return;
6769a0c12413SStephen M. Cameron 
6770a0c12413SStephen M. Cameron 	/*
6771a0c12413SStephen M. Cameron 	 * If we've already checked the heartbeat recently, we're ok.
6772a0c12413SStephen M. Cameron 	 * This could happen if someone sends us a signal. We
6773a0c12413SStephen M. Cameron 	 * otherwise don't care about signals in this thread.
6774a0c12413SStephen M. Cameron 	 */
6775a0c12413SStephen M. Cameron 	if (time_after64(h->last_heartbeat_timestamp +
6776e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
6777a0c12413SStephen M. Cameron 		return;
6778a0c12413SStephen M. Cameron 
6779a0c12413SStephen M. Cameron 	/* If heartbeat has not changed since we last looked, we're not ok. */
6780a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
6781a0c12413SStephen M. Cameron 	heartbeat = readl(&h->cfgtable->HeartBeat);
6782a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
6783a0c12413SStephen M. Cameron 	if (h->last_heartbeat == heartbeat) {
6784a0c12413SStephen M. Cameron 		controller_lockup_detected(h);
6785a0c12413SStephen M. Cameron 		return;
6786a0c12413SStephen M. Cameron 	}
6787a0c12413SStephen M. Cameron 
6788a0c12413SStephen M. Cameron 	/* We're ok. */
6789a0c12413SStephen M. Cameron 	h->last_heartbeat = heartbeat;
6790a0c12413SStephen M. Cameron 	h->last_heartbeat_timestamp = now;
6791a0c12413SStephen M. Cameron }
6792a0c12413SStephen M. Cameron 
67939846590eSStephen M. Cameron static void hpsa_ack_ctlr_events(struct ctlr_info *h)
679476438d08SStephen M. Cameron {
679576438d08SStephen M. Cameron 	int i;
679676438d08SStephen M. Cameron 	char *event_type;
679776438d08SStephen M. Cameron 
6798e863d68eSScott Teel 	/* Clear the driver-requested rescan flag */
6799e863d68eSScott Teel 	h->drv_req_rescan = 0;
6800e863d68eSScott Teel 
680176438d08SStephen M. Cameron 	/* Ask the controller to clear the events we're handling. */
68021f7cee8cSStephen M. Cameron 	if ((h->transMethod & (CFGTBL_Trans_io_accel1
68031f7cee8cSStephen M. Cameron 			| CFGTBL_Trans_io_accel2)) &&
680476438d08SStephen M. Cameron 		(h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
680576438d08SStephen M. Cameron 		 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
680676438d08SStephen M. Cameron 
680776438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
680876438d08SStephen M. Cameron 			event_type = "state change";
680976438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
681076438d08SStephen M. Cameron 			event_type = "configuration change";
681176438d08SStephen M. Cameron 		/* Stop sending new RAID offload reqs via the IO accelerator */
681276438d08SStephen M. Cameron 		scsi_block_requests(h->scsi_host);
681376438d08SStephen M. Cameron 		for (i = 0; i < h->ndevices; i++)
681476438d08SStephen M. Cameron 			h->dev[i]->offload_enabled = 0;
681523100dd9SStephen M. Cameron 		hpsa_drain_accel_commands(h);
681676438d08SStephen M. Cameron 		/* Set 'accelerator path config change' bit */
681776438d08SStephen M. Cameron 		dev_warn(&h->pdev->dev,
681876438d08SStephen M. Cameron 			"Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
681976438d08SStephen M. Cameron 			h->events, event_type);
682076438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
682176438d08SStephen M. Cameron 		/* Set the "clear event notify field update" bit 6 */
682276438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
682376438d08SStephen M. Cameron 		/* Wait until ctlr clears 'clear event notify field', bit 6 */
682476438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
682576438d08SStephen M. Cameron 		scsi_unblock_requests(h->scsi_host);
682676438d08SStephen M. Cameron 	} else {
682776438d08SStephen M. Cameron 		/* Acknowledge controller notification events. */
682876438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
682976438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
683076438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
683176438d08SStephen M. Cameron #if 0
683276438d08SStephen M. Cameron 		writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
683376438d08SStephen M. Cameron 		hpsa_wait_for_mode_change_ack(h);
683476438d08SStephen M. Cameron #endif
683576438d08SStephen M. Cameron 	}
68369846590eSStephen M. Cameron 	return;
683776438d08SStephen M. Cameron }
683876438d08SStephen M. Cameron 
683976438d08SStephen M. Cameron /* Check a register on the controller to see if there are configuration
684076438d08SStephen M. Cameron  * changes (added/changed/removed logical drives, etc.) which mean that
6841e863d68eSScott Teel  * we should rescan the controller for devices.
6842e863d68eSScott Teel  * Also check flag for driver-initiated rescan.
684376438d08SStephen M. Cameron  */
68449846590eSStephen M. Cameron static int hpsa_ctlr_needs_rescan(struct ctlr_info *h)
684576438d08SStephen M. Cameron {
68469846590eSStephen M. Cameron 	if (h->drv_req_rescan)
68479846590eSStephen M. Cameron 		return 1;
68489846590eSStephen M. Cameron 
684976438d08SStephen M. Cameron 	if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
68509846590eSStephen M. Cameron 		return 0;
685176438d08SStephen M. Cameron 
685276438d08SStephen M. Cameron 	h->events = readl(&(h->cfgtable->event_notify));
68539846590eSStephen M. Cameron 	return h->events & RESCAN_REQUIRED_EVENT_BITS;
68549846590eSStephen M. Cameron }
685576438d08SStephen M. Cameron 
685676438d08SStephen M. Cameron /*
68579846590eSStephen M. Cameron  * Check if any of the offline devices have become ready
685876438d08SStephen M. Cameron  */
68599846590eSStephen M. Cameron static int hpsa_offline_devices_ready(struct ctlr_info *h)
68609846590eSStephen M. Cameron {
68619846590eSStephen M. Cameron 	unsigned long flags;
68629846590eSStephen M. Cameron 	struct offline_device_entry *d;
68639846590eSStephen M. Cameron 	struct list_head *this, *tmp;
68649846590eSStephen M. Cameron 
68659846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
68669846590eSStephen M. Cameron 	list_for_each_safe(this, tmp, &h->offline_device_list) {
68679846590eSStephen M. Cameron 		d = list_entry(this, struct offline_device_entry,
68689846590eSStephen M. Cameron 				offline_list);
68699846590eSStephen M. Cameron 		spin_unlock_irqrestore(&h->offline_device_lock, flags);
68709846590eSStephen M. Cameron 		if (!hpsa_volume_offline(h, d->scsi3addr))
68719846590eSStephen M. Cameron 			return 1;
68729846590eSStephen M. Cameron 		spin_lock_irqsave(&h->offline_device_lock, flags);
687376438d08SStephen M. Cameron 	}
68749846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
68759846590eSStephen M. Cameron 	return 0;
68769846590eSStephen M. Cameron }
68779846590eSStephen M. Cameron 
687876438d08SStephen M. Cameron 
68798a98db73SStephen M. Cameron static void hpsa_monitor_ctlr_worker(struct work_struct *work)
6880a0c12413SStephen M. Cameron {
6881a0c12413SStephen M. Cameron 	unsigned long flags;
68828a98db73SStephen M. Cameron 	struct ctlr_info *h = container_of(to_delayed_work(work),
68838a98db73SStephen M. Cameron 					struct ctlr_info, monitor_ctlr_work);
6884a0c12413SStephen M. Cameron 	detect_controller_lockup(h);
68858a98db73SStephen M. Cameron 	if (h->lockup_detected)
68868a98db73SStephen M. Cameron 		return;
68879846590eSStephen M. Cameron 
68889846590eSStephen M. Cameron 	if (hpsa_ctlr_needs_rescan(h) || hpsa_offline_devices_ready(h)) {
68899846590eSStephen M. Cameron 		scsi_host_get(h->scsi_host);
68909846590eSStephen M. Cameron 		h->drv_req_rescan = 0;
68919846590eSStephen M. Cameron 		hpsa_ack_ctlr_events(h);
68929846590eSStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
68939846590eSStephen M. Cameron 		scsi_host_put(h->scsi_host);
68949846590eSStephen M. Cameron 	}
68959846590eSStephen M. Cameron 
68968a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
68978a98db73SStephen M. Cameron 	if (h->remove_in_progress) {
68988a98db73SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
6899a0c12413SStephen M. Cameron 		return;
6900a0c12413SStephen M. Cameron 	}
69018a98db73SStephen M. Cameron 	schedule_delayed_work(&h->monitor_ctlr_work,
69028a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
69038a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
6904a0c12413SStephen M. Cameron }
6905a0c12413SStephen M. Cameron 
69066f039790SGreg Kroah-Hartman static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
69074c2a8c40SStephen M. Cameron {
69084c2a8c40SStephen M. Cameron 	int dac, rc;
69094c2a8c40SStephen M. Cameron 	struct ctlr_info *h;
691064670ac8SStephen M. Cameron 	int try_soft_reset = 0;
691164670ac8SStephen M. Cameron 	unsigned long flags;
69124c2a8c40SStephen M. Cameron 
69134c2a8c40SStephen M. Cameron 	if (number_of_controllers == 0)
69144c2a8c40SStephen M. Cameron 		printk(KERN_INFO DRIVER_NAME "\n");
69154c2a8c40SStephen M. Cameron 
69164c2a8c40SStephen M. Cameron 	rc = hpsa_init_reset_devices(pdev);
691764670ac8SStephen M. Cameron 	if (rc) {
691864670ac8SStephen M. Cameron 		if (rc != -ENOTSUPP)
69194c2a8c40SStephen M. Cameron 			return rc;
692064670ac8SStephen M. Cameron 		/* If the reset fails in a particular way (it has no way to do
692164670ac8SStephen M. Cameron 		 * a proper hard reset, so returns -ENOTSUPP) we can try to do
692264670ac8SStephen M. Cameron 		 * a soft reset once we get the controller configured up to the
692364670ac8SStephen M. Cameron 		 * point that it can accept a command.
692464670ac8SStephen M. Cameron 		 */
692564670ac8SStephen M. Cameron 		try_soft_reset = 1;
692664670ac8SStephen M. Cameron 		rc = 0;
692764670ac8SStephen M. Cameron 	}
692864670ac8SStephen M. Cameron 
692964670ac8SStephen M. Cameron reinit_after_soft_reset:
69304c2a8c40SStephen M. Cameron 
6931303932fdSDon Brace 	/* Command structures must be aligned on a 32-byte boundary because
6932303932fdSDon Brace 	 * the 5 lower bits of the address are used by the hardware. and by
6933303932fdSDon Brace 	 * the driver.  See comments in hpsa.h for more info.
6934303932fdSDon Brace 	 */
6935283b4a9bSStephen M. Cameron #define COMMANDLIST_ALIGNMENT 128
6936303932fdSDon Brace 	BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
6937edd16368SStephen M. Cameron 	h = kzalloc(sizeof(*h), GFP_KERNEL);
6938edd16368SStephen M. Cameron 	if (!h)
6939ecd9aad4SStephen M. Cameron 		return -ENOMEM;
6940edd16368SStephen M. Cameron 
694155c06c71SStephen M. Cameron 	h->pdev = pdev;
6942a9a3a273SStephen M. Cameron 	h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
69439e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&h->cmpQ);
69449e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&h->reqQ);
69459846590eSStephen M. Cameron 	INIT_LIST_HEAD(&h->offline_device_list);
69466eaf46fdSStephen M. Cameron 	spin_lock_init(&h->lock);
69479846590eSStephen M. Cameron 	spin_lock_init(&h->offline_device_lock);
69486eaf46fdSStephen M. Cameron 	spin_lock_init(&h->scan_lock);
69490390f0c0SStephen M. Cameron 	spin_lock_init(&h->passthru_count_lock);
695055c06c71SStephen M. Cameron 	rc = hpsa_pci_init(h);
6951ecd9aad4SStephen M. Cameron 	if (rc != 0)
6952edd16368SStephen M. Cameron 		goto clean1;
6953edd16368SStephen M. Cameron 
6954f79cfec6SStephen M. Cameron 	sprintf(h->devname, HPSA "%d", number_of_controllers);
6955edd16368SStephen M. Cameron 	h->ctlr = number_of_controllers;
6956edd16368SStephen M. Cameron 	number_of_controllers++;
6957edd16368SStephen M. Cameron 
6958edd16368SStephen M. Cameron 	/* configure PCI DMA stuff */
6959ecd9aad4SStephen M. Cameron 	rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
6960ecd9aad4SStephen M. Cameron 	if (rc == 0) {
6961edd16368SStephen M. Cameron 		dac = 1;
6962ecd9aad4SStephen M. Cameron 	} else {
6963ecd9aad4SStephen M. Cameron 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6964ecd9aad4SStephen M. Cameron 		if (rc == 0) {
6965edd16368SStephen M. Cameron 			dac = 0;
6966ecd9aad4SStephen M. Cameron 		} else {
6967edd16368SStephen M. Cameron 			dev_err(&pdev->dev, "no suitable DMA available\n");
6968edd16368SStephen M. Cameron 			goto clean1;
6969edd16368SStephen M. Cameron 		}
6970ecd9aad4SStephen M. Cameron 	}
6971edd16368SStephen M. Cameron 
6972edd16368SStephen M. Cameron 	/* make sure the board interrupts are off */
6973edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
697410f66018SStephen M. Cameron 
69750ae01a32SStephen M. Cameron 	if (hpsa_request_irq(h, do_hpsa_intr_msi, do_hpsa_intr_intx))
6976edd16368SStephen M. Cameron 		goto clean2;
6977303932fdSDon Brace 	dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
6978303932fdSDon Brace 	       h->devname, pdev->device,
6979a9a3a273SStephen M. Cameron 	       h->intr[h->intr_mode], dac ? "" : " not");
69802e9d1b36SStephen M. Cameron 	if (hpsa_allocate_cmd_pool(h))
6981edd16368SStephen M. Cameron 		goto clean4;
698233a2ffceSStephen M. Cameron 	if (hpsa_allocate_sg_chain_blocks(h))
698333a2ffceSStephen M. Cameron 		goto clean4;
6984a08a8471SStephen M. Cameron 	init_waitqueue_head(&h->scan_wait_queue);
6985a08a8471SStephen M. Cameron 	h->scan_finished = 1; /* no scan currently in progress */
6986edd16368SStephen M. Cameron 
6987edd16368SStephen M. Cameron 	pci_set_drvdata(pdev, h);
69889a41338eSStephen M. Cameron 	h->ndevices = 0;
6989316b221aSStephen M. Cameron 	h->hba_mode_enabled = 0;
69909a41338eSStephen M. Cameron 	h->scsi_host = NULL;
69919a41338eSStephen M. Cameron 	spin_lock_init(&h->devlock);
699264670ac8SStephen M. Cameron 	hpsa_put_ctlr_into_performant_mode(h);
699364670ac8SStephen M. Cameron 
699464670ac8SStephen M. Cameron 	/* At this point, the controller is ready to take commands.
699564670ac8SStephen M. Cameron 	 * Now, if reset_devices and the hard reset didn't work, try
699664670ac8SStephen M. Cameron 	 * the soft reset and see if that works.
699764670ac8SStephen M. Cameron 	 */
699864670ac8SStephen M. Cameron 	if (try_soft_reset) {
699964670ac8SStephen M. Cameron 
700064670ac8SStephen M. Cameron 		/* This is kind of gross.  We may or may not get a completion
700164670ac8SStephen M. Cameron 		 * from the soft reset command, and if we do, then the value
700264670ac8SStephen M. Cameron 		 * from the fifo may or may not be valid.  So, we wait 10 secs
700364670ac8SStephen M. Cameron 		 * after the reset throwing away any completions we get during
700464670ac8SStephen M. Cameron 		 * that time.  Unregister the interrupt handler and register
700564670ac8SStephen M. Cameron 		 * fake ones to scoop up any residual completions.
700664670ac8SStephen M. Cameron 		 */
700764670ac8SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
700864670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
700964670ac8SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
7010254f796bSMatt Gates 		free_irqs(h);
701164670ac8SStephen M. Cameron 		rc = hpsa_request_irq(h, hpsa_msix_discard_completions,
701264670ac8SStephen M. Cameron 					hpsa_intx_discard_completions);
701364670ac8SStephen M. Cameron 		if (rc) {
701464670ac8SStephen M. Cameron 			dev_warn(&h->pdev->dev, "Failed to request_irq after "
701564670ac8SStephen M. Cameron 				"soft reset.\n");
701664670ac8SStephen M. Cameron 			goto clean4;
701764670ac8SStephen M. Cameron 		}
701864670ac8SStephen M. Cameron 
701964670ac8SStephen M. Cameron 		rc = hpsa_kdump_soft_reset(h);
702064670ac8SStephen M. Cameron 		if (rc)
702164670ac8SStephen M. Cameron 			/* Neither hard nor soft reset worked, we're hosed. */
702264670ac8SStephen M. Cameron 			goto clean4;
702364670ac8SStephen M. Cameron 
702464670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev, "Board READY.\n");
702564670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev,
702664670ac8SStephen M. Cameron 			"Waiting for stale completions to drain.\n");
702764670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_ON);
702864670ac8SStephen M. Cameron 		msleep(10000);
702964670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
703064670ac8SStephen M. Cameron 
703164670ac8SStephen M. Cameron 		rc = controller_reset_failed(h->cfgtable);
703264670ac8SStephen M. Cameron 		if (rc)
703364670ac8SStephen M. Cameron 			dev_info(&h->pdev->dev,
703464670ac8SStephen M. Cameron 				"Soft reset appears to have failed.\n");
703564670ac8SStephen M. Cameron 
703664670ac8SStephen M. Cameron 		/* since the controller's reset, we have to go back and re-init
703764670ac8SStephen M. Cameron 		 * everything.  Easiest to just forget what we've done and do it
703864670ac8SStephen M. Cameron 		 * all over again.
703964670ac8SStephen M. Cameron 		 */
704064670ac8SStephen M. Cameron 		hpsa_undo_allocations_after_kdump_soft_reset(h);
704164670ac8SStephen M. Cameron 		try_soft_reset = 0;
704264670ac8SStephen M. Cameron 		if (rc)
704364670ac8SStephen M. Cameron 			/* don't go to clean4, we already unallocated */
704464670ac8SStephen M. Cameron 			return -ENODEV;
704564670ac8SStephen M. Cameron 
704664670ac8SStephen M. Cameron 		goto reinit_after_soft_reset;
704764670ac8SStephen M. Cameron 	}
7048edd16368SStephen M. Cameron 
7049da0697bdSScott Teel 		/* Enable Accelerated IO path at driver layer */
7050da0697bdSScott Teel 		h->acciopath_status = 1;
7051da0697bdSScott Teel 
7052e863d68eSScott Teel 	h->drv_req_rescan = 0;
7053e863d68eSScott Teel 
7054edd16368SStephen M. Cameron 	/* Turn the interrupts on so we can service requests */
7055edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_ON);
7056edd16368SStephen M. Cameron 
7057339b2b14SStephen M. Cameron 	hpsa_hba_inquiry(h);
7058edd16368SStephen M. Cameron 	hpsa_register_scsi(h);	/* hook ourselves into SCSI subsystem */
70598a98db73SStephen M. Cameron 
70608a98db73SStephen M. Cameron 	/* Monitor the controller for firmware lockups */
70618a98db73SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
70628a98db73SStephen M. Cameron 	INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
70638a98db73SStephen M. Cameron 	schedule_delayed_work(&h->monitor_ctlr_work,
70648a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
706588bf6d62SStephen M. Cameron 	return 0;
7066edd16368SStephen M. Cameron 
7067edd16368SStephen M. Cameron clean4:
706833a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
70692e9d1b36SStephen M. Cameron 	hpsa_free_cmd_pool(h);
7070254f796bSMatt Gates 	free_irqs(h);
7071edd16368SStephen M. Cameron clean2:
7072edd16368SStephen M. Cameron clean1:
7073edd16368SStephen M. Cameron 	kfree(h);
7074ecd9aad4SStephen M. Cameron 	return rc;
7075edd16368SStephen M. Cameron }
7076edd16368SStephen M. Cameron 
7077edd16368SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h)
7078edd16368SStephen M. Cameron {
7079edd16368SStephen M. Cameron 	char *flush_buf;
7080edd16368SStephen M. Cameron 	struct CommandList *c;
7081702890e3SStephen M. Cameron 	unsigned long flags;
7082702890e3SStephen M. Cameron 
7083702890e3SStephen M. Cameron 	/* Don't bother trying to flush the cache if locked up */
7084702890e3SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
7085702890e3SStephen M. Cameron 	if (unlikely(h->lockup_detected)) {
7086702890e3SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
7087702890e3SStephen M. Cameron 		return;
7088702890e3SStephen M. Cameron 	}
7089702890e3SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
7090edd16368SStephen M. Cameron 
7091edd16368SStephen M. Cameron 	flush_buf = kzalloc(4, GFP_KERNEL);
7092edd16368SStephen M. Cameron 	if (!flush_buf)
7093edd16368SStephen M. Cameron 		return;
7094edd16368SStephen M. Cameron 
7095edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
7096edd16368SStephen M. Cameron 	if (!c) {
7097edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
7098edd16368SStephen M. Cameron 		goto out_of_memory;
7099edd16368SStephen M. Cameron 	}
7100a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
7101a2dac136SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_CMD)) {
7102a2dac136SStephen M. Cameron 		goto out;
7103a2dac136SStephen M. Cameron 	}
7104edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
7105edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus != 0)
7106a2dac136SStephen M. Cameron out:
7107edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
7108edd16368SStephen M. Cameron 			"error flushing cache on controller\n");
7109edd16368SStephen M. Cameron 	cmd_special_free(h, c);
7110edd16368SStephen M. Cameron out_of_memory:
7111edd16368SStephen M. Cameron 	kfree(flush_buf);
7112edd16368SStephen M. Cameron }
7113edd16368SStephen M. Cameron 
7114edd16368SStephen M. Cameron static void hpsa_shutdown(struct pci_dev *pdev)
7115edd16368SStephen M. Cameron {
7116edd16368SStephen M. Cameron 	struct ctlr_info *h;
7117edd16368SStephen M. Cameron 
7118edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
7119edd16368SStephen M. Cameron 	/* Turn board interrupts off  and send the flush cache command
7120edd16368SStephen M. Cameron 	 * sendcmd will turn off interrupt, and send the flush...
7121edd16368SStephen M. Cameron 	 * To write all data in the battery backed cache to disks
7122edd16368SStephen M. Cameron 	 */
7123edd16368SStephen M. Cameron 	hpsa_flush_cache(h);
7124edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
71250097f0f4SStephen M. Cameron 	hpsa_free_irqs_and_disable_msix(h);
7126edd16368SStephen M. Cameron }
7127edd16368SStephen M. Cameron 
71286f039790SGreg Kroah-Hartman static void hpsa_free_device_info(struct ctlr_info *h)
712955e14e76SStephen M. Cameron {
713055e14e76SStephen M. Cameron 	int i;
713155e14e76SStephen M. Cameron 
713255e14e76SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++)
713355e14e76SStephen M. Cameron 		kfree(h->dev[i]);
713455e14e76SStephen M. Cameron }
713555e14e76SStephen M. Cameron 
71366f039790SGreg Kroah-Hartman static void hpsa_remove_one(struct pci_dev *pdev)
7137edd16368SStephen M. Cameron {
7138edd16368SStephen M. Cameron 	struct ctlr_info *h;
71398a98db73SStephen M. Cameron 	unsigned long flags;
7140edd16368SStephen M. Cameron 
7141edd16368SStephen M. Cameron 	if (pci_get_drvdata(pdev) == NULL) {
7142edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "unable to remove device\n");
7143edd16368SStephen M. Cameron 		return;
7144edd16368SStephen M. Cameron 	}
7145edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
71468a98db73SStephen M. Cameron 
71478a98db73SStephen M. Cameron 	/* Get rid of any controller monitoring work items */
71488a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
71498a98db73SStephen M. Cameron 	h->remove_in_progress = 1;
71508a98db73SStephen M. Cameron 	cancel_delayed_work(&h->monitor_ctlr_work);
71518a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
71528a98db73SStephen M. Cameron 
7153edd16368SStephen M. Cameron 	hpsa_unregister_scsi(h);	/* unhook from SCSI subsystem */
7154edd16368SStephen M. Cameron 	hpsa_shutdown(pdev);
7155edd16368SStephen M. Cameron 	iounmap(h->vaddr);
7156204892e9SStephen M. Cameron 	iounmap(h->transtable);
7157204892e9SStephen M. Cameron 	iounmap(h->cfgtable);
715855e14e76SStephen M. Cameron 	hpsa_free_device_info(h);
715933a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
7160edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev,
7161edd16368SStephen M. Cameron 		h->nr_cmds * sizeof(struct CommandList),
7162edd16368SStephen M. Cameron 		h->cmd_pool, h->cmd_pool_dhandle);
7163edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev,
7164edd16368SStephen M. Cameron 		h->nr_cmds * sizeof(struct ErrorInfo),
7165edd16368SStephen M. Cameron 		h->errinfo_pool, h->errinfo_pool_dhandle);
7166303932fdSDon Brace 	pci_free_consistent(h->pdev, h->reply_pool_size,
7167303932fdSDon Brace 		h->reply_pool, h->reply_pool_dhandle);
7168edd16368SStephen M. Cameron 	kfree(h->cmd_pool_bits);
7169303932fdSDon Brace 	kfree(h->blockFetchTable);
7170e1f7de0cSMatt Gates 	kfree(h->ioaccel1_blockFetchTable);
7171aca9012aSStephen M. Cameron 	kfree(h->ioaccel2_blockFetchTable);
7172339b2b14SStephen M. Cameron 	kfree(h->hba_inquiry_data);
7173f0bd0b68SStephen M. Cameron 	pci_disable_device(pdev);
7174edd16368SStephen M. Cameron 	pci_release_regions(pdev);
7175edd16368SStephen M. Cameron 	kfree(h);
7176edd16368SStephen M. Cameron }
7177edd16368SStephen M. Cameron 
7178edd16368SStephen M. Cameron static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
7179edd16368SStephen M. Cameron 	__attribute__((unused)) pm_message_t state)
7180edd16368SStephen M. Cameron {
7181edd16368SStephen M. Cameron 	return -ENOSYS;
7182edd16368SStephen M. Cameron }
7183edd16368SStephen M. Cameron 
7184edd16368SStephen M. Cameron static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
7185edd16368SStephen M. Cameron {
7186edd16368SStephen M. Cameron 	return -ENOSYS;
7187edd16368SStephen M. Cameron }
7188edd16368SStephen M. Cameron 
7189edd16368SStephen M. Cameron static struct pci_driver hpsa_pci_driver = {
7190f79cfec6SStephen M. Cameron 	.name = HPSA,
7191edd16368SStephen M. Cameron 	.probe = hpsa_init_one,
71926f039790SGreg Kroah-Hartman 	.remove = hpsa_remove_one,
7193edd16368SStephen M. Cameron 	.id_table = hpsa_pci_device_id,	/* id_table */
7194edd16368SStephen M. Cameron 	.shutdown = hpsa_shutdown,
7195edd16368SStephen M. Cameron 	.suspend = hpsa_suspend,
7196edd16368SStephen M. Cameron 	.resume = hpsa_resume,
7197edd16368SStephen M. Cameron };
7198edd16368SStephen M. Cameron 
7199303932fdSDon Brace /* Fill in bucket_map[], given nsgs (the max number of
7200303932fdSDon Brace  * scatter gather elements supported) and bucket[],
7201303932fdSDon Brace  * which is an array of 8 integers.  The bucket[] array
7202303932fdSDon Brace  * contains 8 different DMA transfer sizes (in 16
7203303932fdSDon Brace  * byte increments) which the controller uses to fetch
7204303932fdSDon Brace  * commands.  This function fills in bucket_map[], which
7205303932fdSDon Brace  * maps a given number of scatter gather elements to one of
7206303932fdSDon Brace  * the 8 DMA transfer sizes.  The point of it is to allow the
7207303932fdSDon Brace  * controller to only do as much DMA as needed to fetch the
7208303932fdSDon Brace  * command, with the DMA transfer size encoded in the lower
7209303932fdSDon Brace  * bits of the command address.
7210303932fdSDon Brace  */
7211303932fdSDon Brace static void  calc_bucket_map(int bucket[], int num_buckets,
7212e1f7de0cSMatt Gates 	int nsgs, int min_blocks, int *bucket_map)
7213303932fdSDon Brace {
7214303932fdSDon Brace 	int i, j, b, size;
7215303932fdSDon Brace 
7216303932fdSDon Brace 	/* Note, bucket_map must have nsgs+1 entries. */
7217303932fdSDon Brace 	for (i = 0; i <= nsgs; i++) {
7218303932fdSDon Brace 		/* Compute size of a command with i SG entries */
7219e1f7de0cSMatt Gates 		size = i + min_blocks;
7220303932fdSDon Brace 		b = num_buckets; /* Assume the biggest bucket */
7221303932fdSDon Brace 		/* Find the bucket that is just big enough */
7222e1f7de0cSMatt Gates 		for (j = 0; j < num_buckets; j++) {
7223303932fdSDon Brace 			if (bucket[j] >= size) {
7224303932fdSDon Brace 				b = j;
7225303932fdSDon Brace 				break;
7226303932fdSDon Brace 			}
7227303932fdSDon Brace 		}
7228303932fdSDon Brace 		/* for a command with i SG entries, use bucket b. */
7229303932fdSDon Brace 		bucket_map[i] = b;
7230303932fdSDon Brace 	}
7231303932fdSDon Brace }
7232303932fdSDon Brace 
7233e1f7de0cSMatt Gates static void hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
7234303932fdSDon Brace {
72356c311b57SStephen M. Cameron 	int i;
72366c311b57SStephen M. Cameron 	unsigned long register_value;
7237e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
7238e1f7de0cSMatt Gates 			(trans_support & CFGTBL_Trans_use_short_tags) |
7239e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix |
7240b9af4937SStephen M. Cameron 			(trans_support & (CFGTBL_Trans_io_accel1 |
7241b9af4937SStephen M. Cameron 				CFGTBL_Trans_io_accel2));
7242e1f7de0cSMatt Gates 	struct access_method access = SA5_performant_access;
7243def342bdSStephen M. Cameron 
7244def342bdSStephen M. Cameron 	/* This is a bit complicated.  There are 8 registers on
7245def342bdSStephen M. Cameron 	 * the controller which we write to to tell it 8 different
7246def342bdSStephen M. Cameron 	 * sizes of commands which there may be.  It's a way of
7247def342bdSStephen M. Cameron 	 * reducing the DMA done to fetch each command.  Encoded into
7248def342bdSStephen M. Cameron 	 * each command's tag are 3 bits which communicate to the controller
7249def342bdSStephen M. Cameron 	 * which of the eight sizes that command fits within.  The size of
7250def342bdSStephen M. Cameron 	 * each command depends on how many scatter gather entries there are.
7251def342bdSStephen M. Cameron 	 * Each SG entry requires 16 bytes.  The eight registers are programmed
7252def342bdSStephen M. Cameron 	 * with the number of 16-byte blocks a command of that size requires.
7253def342bdSStephen M. Cameron 	 * The smallest command possible requires 5 such 16 byte blocks.
7254d66ae08bSStephen M. Cameron 	 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
7255def342bdSStephen M. Cameron 	 * blocks.  Note, this only extends to the SG entries contained
7256def342bdSStephen M. Cameron 	 * within the command block, and does not extend to chained blocks
7257def342bdSStephen M. Cameron 	 * of SG elements.   bft[] contains the eight values we write to
7258def342bdSStephen M. Cameron 	 * the registers.  They are not evenly distributed, but have more
7259def342bdSStephen M. Cameron 	 * sizes for small commands, and fewer sizes for larger commands.
7260def342bdSStephen M. Cameron 	 */
7261d66ae08bSStephen M. Cameron 	int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
7262b9af4937SStephen M. Cameron #define MIN_IOACCEL2_BFT_ENTRY 5
7263b9af4937SStephen M. Cameron #define HPSA_IOACCEL2_HEADER_SZ 4
7264b9af4937SStephen M. Cameron 	int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
7265b9af4937SStephen M. Cameron 			13, 14, 15, 16, 17, 18, 19,
7266b9af4937SStephen M. Cameron 			HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
7267b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
7268b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
7269b9af4937SStephen M. Cameron 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
7270b9af4937SStephen M. Cameron 				 16 * MIN_IOACCEL2_BFT_ENTRY);
7271b9af4937SStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
7272d66ae08bSStephen M. Cameron 	BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
7273303932fdSDon Brace 	/*  5 = 1 s/g entry or 4k
7274303932fdSDon Brace 	 *  6 = 2 s/g entry or 8k
7275303932fdSDon Brace 	 *  8 = 4 s/g entry or 16k
7276303932fdSDon Brace 	 * 10 = 6 s/g entry or 24k
7277303932fdSDon Brace 	 */
7278303932fdSDon Brace 
7279303932fdSDon Brace 	/* Controller spec: zero out this buffer. */
7280303932fdSDon Brace 	memset(h->reply_pool, 0, h->reply_pool_size);
7281303932fdSDon Brace 
7282d66ae08bSStephen M. Cameron 	bft[7] = SG_ENTRIES_IN_CMD + 4;
7283d66ae08bSStephen M. Cameron 	calc_bucket_map(bft, ARRAY_SIZE(bft),
7284e1f7de0cSMatt Gates 				SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
7285303932fdSDon Brace 	for (i = 0; i < 8; i++)
7286303932fdSDon Brace 		writel(bft[i], &h->transtable->BlockFetch[i]);
7287303932fdSDon Brace 
7288303932fdSDon Brace 	/* size of controller ring buffer */
7289303932fdSDon Brace 	writel(h->max_commands, &h->transtable->RepQSize);
7290254f796bSMatt Gates 	writel(h->nreply_queues, &h->transtable->RepQCount);
7291303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrLow32);
7292303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrHigh32);
7293254f796bSMatt Gates 
7294254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
7295254f796bSMatt Gates 		writel(0, &h->transtable->RepQAddr[i].upper);
7296254f796bSMatt Gates 		writel(h->reply_pool_dhandle +
7297254f796bSMatt Gates 			(h->max_commands * sizeof(u64) * i),
7298254f796bSMatt Gates 			&h->transtable->RepQAddr[i].lower);
7299254f796bSMatt Gates 	}
7300254f796bSMatt Gates 
7301b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
7302e1f7de0cSMatt Gates 	writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
7303e1f7de0cSMatt Gates 	/*
7304e1f7de0cSMatt Gates 	 * enable outbound interrupt coalescing in accelerator mode;
7305e1f7de0cSMatt Gates 	 */
7306e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
7307e1f7de0cSMatt Gates 		access = SA5_ioaccel_mode1_access;
7308e1f7de0cSMatt Gates 		writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
7309e1f7de0cSMatt Gates 		writel(4, &h->cfgtable->HostWrite.CoalIntCount);
7310c349775eSScott Teel 	} else {
7311c349775eSScott Teel 		if (trans_support & CFGTBL_Trans_io_accel2) {
7312c349775eSScott Teel 			access = SA5_ioaccel_mode2_access;
7313c349775eSScott Teel 			writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
7314c349775eSScott Teel 			writel(4, &h->cfgtable->HostWrite.CoalIntCount);
7315c349775eSScott Teel 		}
7316e1f7de0cSMatt Gates 	}
7317303932fdSDon Brace 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
73183f4336f3SStephen M. Cameron 	hpsa_wait_for_mode_change_ack(h);
7319303932fdSDon Brace 	register_value = readl(&(h->cfgtable->TransportActive));
7320303932fdSDon Brace 	if (!(register_value & CFGTBL_Trans_Performant)) {
7321303932fdSDon Brace 		dev_warn(&h->pdev->dev, "unable to get board into"
7322303932fdSDon Brace 					" performant mode\n");
7323303932fdSDon Brace 		return;
7324303932fdSDon Brace 	}
7325960a30e7SStephen M. Cameron 	/* Change the access methods to the performant access methods */
7326e1f7de0cSMatt Gates 	h->access = access;
7327e1f7de0cSMatt Gates 	h->transMethod = transMethod;
7328e1f7de0cSMatt Gates 
7329b9af4937SStephen M. Cameron 	if (!((trans_support & CFGTBL_Trans_io_accel1) ||
7330b9af4937SStephen M. Cameron 		(trans_support & CFGTBL_Trans_io_accel2)))
7331e1f7de0cSMatt Gates 		return;
7332e1f7de0cSMatt Gates 
7333b9af4937SStephen M. Cameron 	if (trans_support & CFGTBL_Trans_io_accel1) {
7334e1f7de0cSMatt Gates 		/* Set up I/O accelerator mode */
7335e1f7de0cSMatt Gates 		for (i = 0; i < h->nreply_queues; i++) {
7336e1f7de0cSMatt Gates 			writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
7337e1f7de0cSMatt Gates 			h->reply_queue[i].current_entry =
7338e1f7de0cSMatt Gates 				readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
7339e1f7de0cSMatt Gates 		}
7340283b4a9bSStephen M. Cameron 		bft[7] = h->ioaccel_maxsg + 8;
7341283b4a9bSStephen M. Cameron 		calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
7342e1f7de0cSMatt Gates 				h->ioaccel1_blockFetchTable);
7343e1f7de0cSMatt Gates 
7344e1f7de0cSMatt Gates 		/* initialize all reply queue entries to unused */
7345e1f7de0cSMatt Gates 		memset(h->reply_pool, (u8) IOACCEL_MODE1_REPLY_UNUSED,
7346e1f7de0cSMatt Gates 				h->reply_pool_size);
7347e1f7de0cSMatt Gates 
7348e1f7de0cSMatt Gates 		/* set all the constant fields in the accelerator command
7349e1f7de0cSMatt Gates 		 * frames once at init time to save CPU cycles later.
7350e1f7de0cSMatt Gates 		 */
7351e1f7de0cSMatt Gates 		for (i = 0; i < h->nr_cmds; i++) {
7352e1f7de0cSMatt Gates 			struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
7353e1f7de0cSMatt Gates 
7354e1f7de0cSMatt Gates 			cp->function = IOACCEL1_FUNCTION_SCSIIO;
7355e1f7de0cSMatt Gates 			cp->err_info = (u32) (h->errinfo_pool_dhandle +
7356e1f7de0cSMatt Gates 					(i * sizeof(struct ErrorInfo)));
7357e1f7de0cSMatt Gates 			cp->err_info_len = sizeof(struct ErrorInfo);
7358e1f7de0cSMatt Gates 			cp->sgl_offset = IOACCEL1_SGLOFFSET;
7359e1f7de0cSMatt Gates 			cp->host_context_flags = IOACCEL1_HCFLAGS_CISS_FORMAT;
7360e1f7de0cSMatt Gates 			cp->timeout_sec = 0;
7361e1f7de0cSMatt Gates 			cp->ReplyQueue = 0;
7362b9af4937SStephen M. Cameron 			cp->Tag.lower = (i << DIRECT_LOOKUP_SHIFT) |
7363b9af4937SStephen M. Cameron 						DIRECT_LOOKUP_BIT;
7364e1f7de0cSMatt Gates 			cp->Tag.upper = 0;
7365b9af4937SStephen M. Cameron 			cp->host_addr.lower =
7366b9af4937SStephen M. Cameron 				(u32) (h->ioaccel_cmd_pool_dhandle +
7367e1f7de0cSMatt Gates 					(i * sizeof(struct io_accel1_cmd)));
7368e1f7de0cSMatt Gates 			cp->host_addr.upper = 0;
7369e1f7de0cSMatt Gates 		}
7370b9af4937SStephen M. Cameron 	} else if (trans_support & CFGTBL_Trans_io_accel2) {
7371b9af4937SStephen M. Cameron 		u64 cfg_offset, cfg_base_addr_index;
7372b9af4937SStephen M. Cameron 		u32 bft2_offset, cfg_base_addr;
7373b9af4937SStephen M. Cameron 		int rc;
7374b9af4937SStephen M. Cameron 
7375b9af4937SStephen M. Cameron 		rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
7376b9af4937SStephen M. Cameron 			&cfg_base_addr_index, &cfg_offset);
7377b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
7378b9af4937SStephen M. Cameron 		bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
7379b9af4937SStephen M. Cameron 		calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
7380b9af4937SStephen M. Cameron 				4, h->ioaccel2_blockFetchTable);
7381b9af4937SStephen M. Cameron 		bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
7382b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct CfgTable,
7383b9af4937SStephen M. Cameron 				io_accel_request_size_offset) != 0xb8);
7384b9af4937SStephen M. Cameron 		h->ioaccel2_bft2_regs =
7385b9af4937SStephen M. Cameron 			remap_pci_mem(pci_resource_start(h->pdev,
7386b9af4937SStephen M. Cameron 					cfg_base_addr_index) +
7387b9af4937SStephen M. Cameron 					cfg_offset + bft2_offset,
7388b9af4937SStephen M. Cameron 					ARRAY_SIZE(bft2) *
7389b9af4937SStephen M. Cameron 					sizeof(*h->ioaccel2_bft2_regs));
7390b9af4937SStephen M. Cameron 		for (i = 0; i < ARRAY_SIZE(bft2); i++)
7391b9af4937SStephen M. Cameron 			writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
7392b9af4937SStephen M. Cameron 	}
7393b9af4937SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
7394b9af4937SStephen M. Cameron 	hpsa_wait_for_mode_change_ack(h);
7395e1f7de0cSMatt Gates }
7396e1f7de0cSMatt Gates 
7397e1f7de0cSMatt Gates static int hpsa_alloc_ioaccel_cmd_and_bft(struct ctlr_info *h)
7398e1f7de0cSMatt Gates {
7399283b4a9bSStephen M. Cameron 	h->ioaccel_maxsg =
7400283b4a9bSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
7401283b4a9bSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
7402283b4a9bSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
7403283b4a9bSStephen M. Cameron 
7404e1f7de0cSMatt Gates 	/* Command structures must be aligned on a 128-byte boundary
7405e1f7de0cSMatt Gates 	 * because the 7 lower bits of the address are used by the
7406e1f7de0cSMatt Gates 	 * hardware.
7407e1f7de0cSMatt Gates 	 */
7408e1f7de0cSMatt Gates #define IOACCEL1_COMMANDLIST_ALIGNMENT 128
7409e1f7de0cSMatt Gates 	BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
7410e1f7de0cSMatt Gates 			IOACCEL1_COMMANDLIST_ALIGNMENT);
7411e1f7de0cSMatt Gates 	h->ioaccel_cmd_pool =
7412e1f7de0cSMatt Gates 		pci_alloc_consistent(h->pdev,
7413e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
7414e1f7de0cSMatt Gates 			&(h->ioaccel_cmd_pool_dhandle));
7415e1f7de0cSMatt Gates 
7416e1f7de0cSMatt Gates 	h->ioaccel1_blockFetchTable =
7417283b4a9bSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
7418e1f7de0cSMatt Gates 				sizeof(u32)), GFP_KERNEL);
7419e1f7de0cSMatt Gates 
7420e1f7de0cSMatt Gates 	if ((h->ioaccel_cmd_pool == NULL) ||
7421e1f7de0cSMatt Gates 		(h->ioaccel1_blockFetchTable == NULL))
7422e1f7de0cSMatt Gates 		goto clean_up;
7423e1f7de0cSMatt Gates 
7424e1f7de0cSMatt Gates 	memset(h->ioaccel_cmd_pool, 0,
7425e1f7de0cSMatt Gates 		h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
7426e1f7de0cSMatt Gates 	return 0;
7427e1f7de0cSMatt Gates 
7428e1f7de0cSMatt Gates clean_up:
7429e1f7de0cSMatt Gates 	if (h->ioaccel_cmd_pool)
7430e1f7de0cSMatt Gates 		pci_free_consistent(h->pdev,
7431e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
7432e1f7de0cSMatt Gates 			h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
7433e1f7de0cSMatt Gates 	kfree(h->ioaccel1_blockFetchTable);
7434e1f7de0cSMatt Gates 	return 1;
74356c311b57SStephen M. Cameron }
74366c311b57SStephen M. Cameron 
7437aca9012aSStephen M. Cameron static int ioaccel2_alloc_cmds_and_bft(struct ctlr_info *h)
7438aca9012aSStephen M. Cameron {
7439aca9012aSStephen M. Cameron 	/* Allocate ioaccel2 mode command blocks and block fetch table */
7440aca9012aSStephen M. Cameron 
7441aca9012aSStephen M. Cameron 	h->ioaccel_maxsg =
7442aca9012aSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
7443aca9012aSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
7444aca9012aSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
7445aca9012aSStephen M. Cameron 
7446aca9012aSStephen M. Cameron #define IOACCEL2_COMMANDLIST_ALIGNMENT 128
7447aca9012aSStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
7448aca9012aSStephen M. Cameron 			IOACCEL2_COMMANDLIST_ALIGNMENT);
7449aca9012aSStephen M. Cameron 	h->ioaccel2_cmd_pool =
7450aca9012aSStephen M. Cameron 		pci_alloc_consistent(h->pdev,
7451aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
7452aca9012aSStephen M. Cameron 			&(h->ioaccel2_cmd_pool_dhandle));
7453aca9012aSStephen M. Cameron 
7454aca9012aSStephen M. Cameron 	h->ioaccel2_blockFetchTable =
7455aca9012aSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
7456aca9012aSStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
7457aca9012aSStephen M. Cameron 
7458aca9012aSStephen M. Cameron 	if ((h->ioaccel2_cmd_pool == NULL) ||
7459aca9012aSStephen M. Cameron 		(h->ioaccel2_blockFetchTable == NULL))
7460aca9012aSStephen M. Cameron 		goto clean_up;
7461aca9012aSStephen M. Cameron 
7462aca9012aSStephen M. Cameron 	memset(h->ioaccel2_cmd_pool, 0,
7463aca9012aSStephen M. Cameron 		h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
7464aca9012aSStephen M. Cameron 	return 0;
7465aca9012aSStephen M. Cameron 
7466aca9012aSStephen M. Cameron clean_up:
7467aca9012aSStephen M. Cameron 	if (h->ioaccel2_cmd_pool)
7468aca9012aSStephen M. Cameron 		pci_free_consistent(h->pdev,
7469aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
7470aca9012aSStephen M. Cameron 			h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
7471aca9012aSStephen M. Cameron 	kfree(h->ioaccel2_blockFetchTable);
7472aca9012aSStephen M. Cameron 	return 1;
7473aca9012aSStephen M. Cameron }
7474aca9012aSStephen M. Cameron 
74756f039790SGreg Kroah-Hartman static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
74766c311b57SStephen M. Cameron {
74776c311b57SStephen M. Cameron 	u32 trans_support;
7478e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
7479e1f7de0cSMatt Gates 					CFGTBL_Trans_use_short_tags;
7480254f796bSMatt Gates 	int i;
74816c311b57SStephen M. Cameron 
748202ec19c8SStephen M. Cameron 	if (hpsa_simple_mode)
748302ec19c8SStephen M. Cameron 		return;
748402ec19c8SStephen M. Cameron 
748567c99a72Sscameron@beardog.cce.hp.com 	trans_support = readl(&(h->cfgtable->TransportSupport));
748667c99a72Sscameron@beardog.cce.hp.com 	if (!(trans_support & PERFORMANT_MODE))
748767c99a72Sscameron@beardog.cce.hp.com 		return;
748867c99a72Sscameron@beardog.cce.hp.com 
7489e1f7de0cSMatt Gates 	/* Check for I/O accelerator mode support */
7490e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
7491e1f7de0cSMatt Gates 		transMethod |= CFGTBL_Trans_io_accel1 |
7492e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix;
7493e1f7de0cSMatt Gates 		if (hpsa_alloc_ioaccel_cmd_and_bft(h))
7494e1f7de0cSMatt Gates 			goto clean_up;
7495aca9012aSStephen M. Cameron 	} else {
7496aca9012aSStephen M. Cameron 		if (trans_support & CFGTBL_Trans_io_accel2) {
7497aca9012aSStephen M. Cameron 				transMethod |= CFGTBL_Trans_io_accel2 |
7498aca9012aSStephen M. Cameron 				CFGTBL_Trans_enable_directed_msix;
7499aca9012aSStephen M. Cameron 		if (ioaccel2_alloc_cmds_and_bft(h))
7500aca9012aSStephen M. Cameron 			goto clean_up;
7501aca9012aSStephen M. Cameron 		}
7502e1f7de0cSMatt Gates 	}
7503e1f7de0cSMatt Gates 
7504e1f7de0cSMatt Gates 	/* TODO, check that this next line h->nreply_queues is correct */
7505eee0f03aSHannes Reinecke 	h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
7506cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
75076c311b57SStephen M. Cameron 	/* Performant mode ring buffer and supporting data structures */
7508254f796bSMatt Gates 	h->reply_pool_size = h->max_commands * sizeof(u64) * h->nreply_queues;
75096c311b57SStephen M. Cameron 	h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size,
75106c311b57SStephen M. Cameron 				&(h->reply_pool_dhandle));
75116c311b57SStephen M. Cameron 
7512254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
7513254f796bSMatt Gates 		h->reply_queue[i].head = &h->reply_pool[h->max_commands * i];
7514254f796bSMatt Gates 		h->reply_queue[i].size = h->max_commands;
7515254f796bSMatt Gates 		h->reply_queue[i].wraparound = 1;  /* spec: init to 1 */
7516254f796bSMatt Gates 		h->reply_queue[i].current_entry = 0;
7517254f796bSMatt Gates 	}
7518254f796bSMatt Gates 
75196c311b57SStephen M. Cameron 	/* Need a block fetch table for performant mode */
7520d66ae08bSStephen M. Cameron 	h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
75216c311b57SStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
75226c311b57SStephen M. Cameron 
75236c311b57SStephen M. Cameron 	if ((h->reply_pool == NULL)
75246c311b57SStephen M. Cameron 		|| (h->blockFetchTable == NULL))
75256c311b57SStephen M. Cameron 		goto clean_up;
75266c311b57SStephen M. Cameron 
7527e1f7de0cSMatt Gates 	hpsa_enter_performant_mode(h, trans_support);
7528303932fdSDon Brace 	return;
7529303932fdSDon Brace 
7530303932fdSDon Brace clean_up:
7531303932fdSDon Brace 	if (h->reply_pool)
7532303932fdSDon Brace 		pci_free_consistent(h->pdev, h->reply_pool_size,
7533303932fdSDon Brace 			h->reply_pool, h->reply_pool_dhandle);
7534303932fdSDon Brace 	kfree(h->blockFetchTable);
7535303932fdSDon Brace }
7536303932fdSDon Brace 
753723100dd9SStephen M. Cameron static int is_accelerated_cmd(struct CommandList *c)
753876438d08SStephen M. Cameron {
753923100dd9SStephen M. Cameron 	return c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_IOACCEL2;
754023100dd9SStephen M. Cameron }
754123100dd9SStephen M. Cameron 
754223100dd9SStephen M. Cameron static void hpsa_drain_accel_commands(struct ctlr_info *h)
754323100dd9SStephen M. Cameron {
754423100dd9SStephen M. Cameron 	struct CommandList *c = NULL;
754576438d08SStephen M. Cameron 	unsigned long flags;
754623100dd9SStephen M. Cameron 	int accel_cmds_out;
754776438d08SStephen M. Cameron 
754876438d08SStephen M. Cameron 	do { /* wait for all outstanding commands to drain out */
754923100dd9SStephen M. Cameron 		accel_cmds_out = 0;
755076438d08SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
755123100dd9SStephen M. Cameron 		list_for_each_entry(c, &h->cmpQ, list)
755223100dd9SStephen M. Cameron 			accel_cmds_out += is_accelerated_cmd(c);
755323100dd9SStephen M. Cameron 		list_for_each_entry(c, &h->reqQ, list)
755423100dd9SStephen M. Cameron 			accel_cmds_out += is_accelerated_cmd(c);
755576438d08SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
755623100dd9SStephen M. Cameron 		if (accel_cmds_out <= 0)
755776438d08SStephen M. Cameron 			break;
755876438d08SStephen M. Cameron 		msleep(100);
755976438d08SStephen M. Cameron 	} while (1);
756076438d08SStephen M. Cameron }
756176438d08SStephen M. Cameron 
7562edd16368SStephen M. Cameron /*
7563edd16368SStephen M. Cameron  *  This is it.  Register the PCI driver information for the cards we control
7564edd16368SStephen M. Cameron  *  the OS will call our registered routines when it finds one of our cards.
7565edd16368SStephen M. Cameron  */
7566edd16368SStephen M. Cameron static int __init hpsa_init(void)
7567edd16368SStephen M. Cameron {
756831468401SMike Miller 	return pci_register_driver(&hpsa_pci_driver);
7569edd16368SStephen M. Cameron }
7570edd16368SStephen M. Cameron 
7571edd16368SStephen M. Cameron static void __exit hpsa_cleanup(void)
7572edd16368SStephen M. Cameron {
7573edd16368SStephen M. Cameron 	pci_unregister_driver(&hpsa_pci_driver);
7574edd16368SStephen M. Cameron }
7575edd16368SStephen M. Cameron 
7576e1f7de0cSMatt Gates static void __attribute__((unused)) verify_offsets(void)
7577e1f7de0cSMatt Gates {
7578e1f7de0cSMatt Gates #define VERIFY_OFFSET(member, offset) \
7579dd0e19f3SScott Teel 	BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
7580dd0e19f3SScott Teel 
7581dd0e19f3SScott Teel 	VERIFY_OFFSET(structure_size, 0);
7582dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_size, 4);
7583dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_cnt, 8);
7584dd0e19f3SScott Teel 	VERIFY_OFFSET(phys_blk_shift, 16);
7585dd0e19f3SScott Teel 	VERIFY_OFFSET(parity_rotation_shift, 17);
7586dd0e19f3SScott Teel 	VERIFY_OFFSET(strip_size, 18);
7587dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_starting_blk, 20);
7588dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_blk_cnt, 28);
7589dd0e19f3SScott Teel 	VERIFY_OFFSET(data_disks_per_row, 36);
7590dd0e19f3SScott Teel 	VERIFY_OFFSET(metadata_disks_per_row, 38);
7591dd0e19f3SScott Teel 	VERIFY_OFFSET(row_cnt, 40);
7592dd0e19f3SScott Teel 	VERIFY_OFFSET(layout_map_count, 42);
7593dd0e19f3SScott Teel 	VERIFY_OFFSET(flags, 44);
7594dd0e19f3SScott Teel 	VERIFY_OFFSET(dekindex, 46);
7595dd0e19f3SScott Teel 	/* VERIFY_OFFSET(reserved, 48 */
7596dd0e19f3SScott Teel 	VERIFY_OFFSET(data, 64);
7597dd0e19f3SScott Teel 
7598dd0e19f3SScott Teel #undef VERIFY_OFFSET
7599dd0e19f3SScott Teel 
7600dd0e19f3SScott Teel #define VERIFY_OFFSET(member, offset) \
7601b66cc250SMike Miller 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
7602b66cc250SMike Miller 
7603b66cc250SMike Miller 	VERIFY_OFFSET(IU_type, 0);
7604b66cc250SMike Miller 	VERIFY_OFFSET(direction, 1);
7605b66cc250SMike Miller 	VERIFY_OFFSET(reply_queue, 2);
7606b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved1, 3);  */
7607b66cc250SMike Miller 	VERIFY_OFFSET(scsi_nexus, 4);
7608b66cc250SMike Miller 	VERIFY_OFFSET(Tag, 8);
7609b66cc250SMike Miller 	VERIFY_OFFSET(cdb, 16);
7610b66cc250SMike Miller 	VERIFY_OFFSET(cciss_lun, 32);
7611b66cc250SMike Miller 	VERIFY_OFFSET(data_len, 40);
7612b66cc250SMike Miller 	VERIFY_OFFSET(cmd_priority_task_attr, 44);
7613b66cc250SMike Miller 	VERIFY_OFFSET(sg_count, 45);
7614b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved3 */
7615b66cc250SMike Miller 	VERIFY_OFFSET(err_ptr, 48);
7616b66cc250SMike Miller 	VERIFY_OFFSET(err_len, 56);
7617b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved4  */
7618b66cc250SMike Miller 	VERIFY_OFFSET(sg, 64);
7619b66cc250SMike Miller 
7620b66cc250SMike Miller #undef VERIFY_OFFSET
7621b66cc250SMike Miller 
7622b66cc250SMike Miller #define VERIFY_OFFSET(member, offset) \
7623e1f7de0cSMatt Gates 	BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
7624e1f7de0cSMatt Gates 
7625e1f7de0cSMatt Gates 	VERIFY_OFFSET(dev_handle, 0x00);
7626e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved1, 0x02);
7627e1f7de0cSMatt Gates 	VERIFY_OFFSET(function, 0x03);
7628e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved2, 0x04);
7629e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info, 0x0C);
7630e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved3, 0x10);
7631e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info_len, 0x12);
7632e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved4, 0x13);
7633e1f7de0cSMatt Gates 	VERIFY_OFFSET(sgl_offset, 0x14);
7634e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved5, 0x15);
7635e1f7de0cSMatt Gates 	VERIFY_OFFSET(transfer_len, 0x1C);
7636e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved6, 0x20);
7637e1f7de0cSMatt Gates 	VERIFY_OFFSET(io_flags, 0x24);
7638e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved7, 0x26);
7639e1f7de0cSMatt Gates 	VERIFY_OFFSET(LUN, 0x34);
7640e1f7de0cSMatt Gates 	VERIFY_OFFSET(control, 0x3C);
7641e1f7de0cSMatt Gates 	VERIFY_OFFSET(CDB, 0x40);
7642e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved8, 0x50);
7643e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_context_flags, 0x60);
7644e1f7de0cSMatt Gates 	VERIFY_OFFSET(timeout_sec, 0x62);
7645e1f7de0cSMatt Gates 	VERIFY_OFFSET(ReplyQueue, 0x64);
7646e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved9, 0x65);
7647e1f7de0cSMatt Gates 	VERIFY_OFFSET(Tag, 0x68);
7648e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_addr, 0x70);
7649e1f7de0cSMatt Gates 	VERIFY_OFFSET(CISS_LUN, 0x78);
7650e1f7de0cSMatt Gates 	VERIFY_OFFSET(SG, 0x78 + 8);
7651e1f7de0cSMatt Gates #undef VERIFY_OFFSET
7652e1f7de0cSMatt Gates }
7653e1f7de0cSMatt Gates 
7654edd16368SStephen M. Cameron module_init(hpsa_init);
7655edd16368SStephen M. Cameron module_exit(hpsa_cleanup);
7656