xref: /openbmc/linux/drivers/scsi/hpsa.c (revision 5ca0120447ae8d485e2ee5100f25b6645e3e320f)
1edd16368SStephen M. Cameron /*
2edd16368SStephen M. Cameron  *    Disk Array driver for HP Smart Array SAS controllers
31358f6dcSDon Brace  *    Copyright 2014-2015 PMC-Sierra, Inc.
41358f6dcSDon Brace  *    Copyright 2000,2009-2015 Hewlett-Packard Development Company, L.P.
5edd16368SStephen M. Cameron  *
6edd16368SStephen M. Cameron  *    This program is free software; you can redistribute it and/or modify
7edd16368SStephen M. Cameron  *    it under the terms of the GNU General Public License as published by
8edd16368SStephen M. Cameron  *    the Free Software Foundation; version 2 of the License.
9edd16368SStephen M. Cameron  *
10edd16368SStephen M. Cameron  *    This program is distributed in the hope that it will be useful,
11edd16368SStephen M. Cameron  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
12edd16368SStephen M. Cameron  *    MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13edd16368SStephen M. Cameron  *    NON INFRINGEMENT.  See the GNU General Public License for more details.
14edd16368SStephen M. Cameron  *
151358f6dcSDon Brace  *    Questions/Comments/Bugfixes to storagedev@pmcs.com
16edd16368SStephen M. Cameron  *
17edd16368SStephen M. Cameron  */
18edd16368SStephen M. Cameron 
19edd16368SStephen M. Cameron #include <linux/module.h>
20edd16368SStephen M. Cameron #include <linux/interrupt.h>
21edd16368SStephen M. Cameron #include <linux/types.h>
22edd16368SStephen M. Cameron #include <linux/pci.h>
23e5a44df8SMatthew Garrett #include <linux/pci-aspm.h>
24edd16368SStephen M. Cameron #include <linux/kernel.h>
25edd16368SStephen M. Cameron #include <linux/slab.h>
26edd16368SStephen M. Cameron #include <linux/delay.h>
27edd16368SStephen M. Cameron #include <linux/fs.h>
28edd16368SStephen M. Cameron #include <linux/timer.h>
29edd16368SStephen M. Cameron #include <linux/init.h>
30edd16368SStephen M. Cameron #include <linux/spinlock.h>
31edd16368SStephen M. Cameron #include <linux/compat.h>
32edd16368SStephen M. Cameron #include <linux/blktrace_api.h>
33edd16368SStephen M. Cameron #include <linux/uaccess.h>
34edd16368SStephen M. Cameron #include <linux/io.h>
35edd16368SStephen M. Cameron #include <linux/dma-mapping.h>
36edd16368SStephen M. Cameron #include <linux/completion.h>
37edd16368SStephen M. Cameron #include <linux/moduleparam.h>
38edd16368SStephen M. Cameron #include <scsi/scsi.h>
39edd16368SStephen M. Cameron #include <scsi/scsi_cmnd.h>
40edd16368SStephen M. Cameron #include <scsi/scsi_device.h>
41edd16368SStephen M. Cameron #include <scsi/scsi_host.h>
42667e23d4SStephen M. Cameron #include <scsi/scsi_tcq.h>
439437ac43SStephen Cameron #include <scsi/scsi_eh.h>
4473153fe5SWebb Scales #include <scsi/scsi_dbg.h>
45edd16368SStephen M. Cameron #include <linux/cciss_ioctl.h>
46edd16368SStephen M. Cameron #include <linux/string.h>
47edd16368SStephen M. Cameron #include <linux/bitmap.h>
4860063497SArun Sharma #include <linux/atomic.h>
49a0c12413SStephen M. Cameron #include <linux/jiffies.h>
5042a91641SDon Brace #include <linux/percpu-defs.h>
51094963daSStephen M. Cameron #include <linux/percpu.h>
522b08b3e9SDon Brace #include <asm/unaligned.h>
53283b4a9bSStephen M. Cameron #include <asm/div64.h>
54edd16368SStephen M. Cameron #include "hpsa_cmd.h"
55edd16368SStephen M. Cameron #include "hpsa.h"
56edd16368SStephen M. Cameron 
57edd16368SStephen M. Cameron /* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */
58f532a3f9SDon Brace #define HPSA_DRIVER_VERSION "3.4.10-0"
59edd16368SStephen M. Cameron #define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
60f79cfec6SStephen M. Cameron #define HPSA "hpsa"
61edd16368SStephen M. Cameron 
62007e7aa9SRobert Elliott /* How long to wait for CISS doorbell communication */
63007e7aa9SRobert Elliott #define CLEAR_EVENT_WAIT_INTERVAL 20	/* ms for each msleep() call */
64007e7aa9SRobert Elliott #define MODE_CHANGE_WAIT_INTERVAL 10	/* ms for each msleep() call */
65007e7aa9SRobert Elliott #define MAX_CLEAR_EVENT_WAIT 30000	/* times 20 ms = 600 s */
66007e7aa9SRobert Elliott #define MAX_MODE_CHANGE_WAIT 2000	/* times 10 ms = 20 s */
67edd16368SStephen M. Cameron #define MAX_IOCTL_CONFIG_WAIT 1000
68edd16368SStephen M. Cameron 
69edd16368SStephen M. Cameron /*define how many times we will try a command because of bus resets */
70edd16368SStephen M. Cameron #define MAX_CMD_RETRIES 3
71edd16368SStephen M. Cameron 
72edd16368SStephen M. Cameron /* Embedded module documentation macros - see modules.h */
73edd16368SStephen M. Cameron MODULE_AUTHOR("Hewlett-Packard Company");
74edd16368SStephen M. Cameron MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
75edd16368SStephen M. Cameron 	HPSA_DRIVER_VERSION);
76edd16368SStephen M. Cameron MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
77edd16368SStephen M. Cameron MODULE_VERSION(HPSA_DRIVER_VERSION);
78edd16368SStephen M. Cameron MODULE_LICENSE("GPL");
79edd16368SStephen M. Cameron 
80edd16368SStephen M. Cameron static int hpsa_allow_any;
81edd16368SStephen M. Cameron module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
82edd16368SStephen M. Cameron MODULE_PARM_DESC(hpsa_allow_any,
83edd16368SStephen M. Cameron 		"Allow hpsa driver to access unknown HP Smart Array hardware");
8402ec19c8SStephen M. Cameron static int hpsa_simple_mode;
8502ec19c8SStephen M. Cameron module_param(hpsa_simple_mode, int, S_IRUGO|S_IWUSR);
8602ec19c8SStephen M. Cameron MODULE_PARM_DESC(hpsa_simple_mode,
8702ec19c8SStephen M. Cameron 	"Use 'simple mode' rather than 'performant mode'");
88edd16368SStephen M. Cameron 
89edd16368SStephen M. Cameron /* define the PCI info for the cards we can control */
90edd16368SStephen M. Cameron static const struct pci_device_id hpsa_pci_device_id[] = {
91edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3241},
92edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3243},
93edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3245},
94edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3247},
95edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3249},
96163dbcd8SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x324A},
97163dbcd8SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x324B},
98f8b01eb9SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3233},
999143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3350},
1009143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3351},
1019143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3352},
1029143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3353},
1039143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3354},
1049143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3355},
1059143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3356},
106fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1921},
107fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1922},
108fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1923},
109fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1924},
110fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1926},
111fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1928},
11297b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1929},
11397b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BD},
11497b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BE},
11597b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BF},
11697b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C0},
11797b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C1},
11897b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C2},
11997b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C3},
12097b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C4},
12197b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C5},
1223b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C6},
12397b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C7},
12497b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C8},
12597b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C9},
1263b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CA},
1273b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CB},
1283b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CC},
1293b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CD},
1303b7a45e5SJoe Handzik 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21CE},
131fdfa4b6dSDon Brace 	{PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0580},
132cbb47dcbSDon Brace 	{PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0581},
133cbb47dcbSDon Brace 	{PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0582},
134cbb47dcbSDon Brace 	{PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0583},
135cbb47dcbSDon Brace 	{PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0584},
136cbb47dcbSDon Brace 	{PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0585},
1378e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0076},
1388e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0087},
1398e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x007D},
1408e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0088},
1418e616a5eSStephen M. Cameron 	{PCI_VENDOR_ID_HP, 0x333f, 0x103c, 0x333f},
142edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_ANY_ID,	PCI_ANY_ID, PCI_ANY_ID,
143edd16368SStephen M. Cameron 		PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
144edd16368SStephen M. Cameron 	{0,}
145edd16368SStephen M. Cameron };
146edd16368SStephen M. Cameron 
147edd16368SStephen M. Cameron MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
148edd16368SStephen M. Cameron 
149edd16368SStephen M. Cameron /*  board_id = Subsystem Device ID & Vendor ID
150edd16368SStephen M. Cameron  *  product = Marketing Name for the board
151edd16368SStephen M. Cameron  *  access = Address of the struct of function pointers
152edd16368SStephen M. Cameron  */
153edd16368SStephen M. Cameron static struct board_type products[] = {
154edd16368SStephen M. Cameron 	{0x3241103C, "Smart Array P212", &SA5_access},
155edd16368SStephen M. Cameron 	{0x3243103C, "Smart Array P410", &SA5_access},
156edd16368SStephen M. Cameron 	{0x3245103C, "Smart Array P410i", &SA5_access},
157edd16368SStephen M. Cameron 	{0x3247103C, "Smart Array P411", &SA5_access},
158edd16368SStephen M. Cameron 	{0x3249103C, "Smart Array P812", &SA5_access},
159163dbcd8SMike Miller 	{0x324A103C, "Smart Array P712m", &SA5_access},
160163dbcd8SMike Miller 	{0x324B103C, "Smart Array P711m", &SA5_access},
1617d2cce58SStephen M. Cameron 	{0x3233103C, "HP StorageWorks 1210m", &SA5_access}, /* alias of 333f */
162fe0c9610SMike Miller 	{0x3350103C, "Smart Array P222", &SA5_access},
163fe0c9610SMike Miller 	{0x3351103C, "Smart Array P420", &SA5_access},
164fe0c9610SMike Miller 	{0x3352103C, "Smart Array P421", &SA5_access},
165fe0c9610SMike Miller 	{0x3353103C, "Smart Array P822", &SA5_access},
166fe0c9610SMike Miller 	{0x3354103C, "Smart Array P420i", &SA5_access},
167fe0c9610SMike Miller 	{0x3355103C, "Smart Array P220i", &SA5_access},
168fe0c9610SMike Miller 	{0x3356103C, "Smart Array P721m", &SA5_access},
1691fd6c8e3SMike Miller 	{0x1921103C, "Smart Array P830i", &SA5_access},
1701fd6c8e3SMike Miller 	{0x1922103C, "Smart Array P430", &SA5_access},
1711fd6c8e3SMike Miller 	{0x1923103C, "Smart Array P431", &SA5_access},
1721fd6c8e3SMike Miller 	{0x1924103C, "Smart Array P830", &SA5_access},
1731fd6c8e3SMike Miller 	{0x1926103C, "Smart Array P731m", &SA5_access},
1741fd6c8e3SMike Miller 	{0x1928103C, "Smart Array P230i", &SA5_access},
1751fd6c8e3SMike Miller 	{0x1929103C, "Smart Array P530", &SA5_access},
17627fb8137SDon Brace 	{0x21BD103C, "Smart Array P244br", &SA5_access},
17727fb8137SDon Brace 	{0x21BE103C, "Smart Array P741m", &SA5_access},
17827fb8137SDon Brace 	{0x21BF103C, "Smart HBA H240ar", &SA5_access},
17927fb8137SDon Brace 	{0x21C0103C, "Smart Array P440ar", &SA5_access},
180c8ae0ab1SDon Brace 	{0x21C1103C, "Smart Array P840ar", &SA5_access},
18127fb8137SDon Brace 	{0x21C2103C, "Smart Array P440", &SA5_access},
18227fb8137SDon Brace 	{0x21C3103C, "Smart Array P441", &SA5_access},
18397b9f53dSMike Miller 	{0x21C4103C, "Smart Array", &SA5_access},
18427fb8137SDon Brace 	{0x21C5103C, "Smart Array P841", &SA5_access},
18527fb8137SDon Brace 	{0x21C6103C, "Smart HBA H244br", &SA5_access},
18627fb8137SDon Brace 	{0x21C7103C, "Smart HBA H240", &SA5_access},
18727fb8137SDon Brace 	{0x21C8103C, "Smart HBA H241", &SA5_access},
18897b9f53dSMike Miller 	{0x21C9103C, "Smart Array", &SA5_access},
18927fb8137SDon Brace 	{0x21CA103C, "Smart Array P246br", &SA5_access},
19027fb8137SDon Brace 	{0x21CB103C, "Smart Array P840", &SA5_access},
1913b7a45e5SJoe Handzik 	{0x21CC103C, "Smart Array", &SA5_access},
1923b7a45e5SJoe Handzik 	{0x21CD103C, "Smart Array", &SA5_access},
19327fb8137SDon Brace 	{0x21CE103C, "Smart HBA", &SA5_access},
194fdfa4b6dSDon Brace 	{0x05809005, "SmartHBA-SA", &SA5_access},
195cbb47dcbSDon Brace 	{0x05819005, "SmartHBA-SA 8i", &SA5_access},
196cbb47dcbSDon Brace 	{0x05829005, "SmartHBA-SA 8i8e", &SA5_access},
197cbb47dcbSDon Brace 	{0x05839005, "SmartHBA-SA 8e", &SA5_access},
198cbb47dcbSDon Brace 	{0x05849005, "SmartHBA-SA 16i", &SA5_access},
199cbb47dcbSDon Brace 	{0x05859005, "SmartHBA-SA 4i4e", &SA5_access},
2008e616a5eSStephen M. Cameron 	{0x00761590, "HP Storage P1224 Array Controller", &SA5_access},
2018e616a5eSStephen M. Cameron 	{0x00871590, "HP Storage P1224e Array Controller", &SA5_access},
2028e616a5eSStephen M. Cameron 	{0x007D1590, "HP Storage P1228 Array Controller", &SA5_access},
2038e616a5eSStephen M. Cameron 	{0x00881590, "HP Storage P1228e Array Controller", &SA5_access},
2048e616a5eSStephen M. Cameron 	{0x333f103c, "HP StorageWorks 1210m Array Controller", &SA5_access},
205edd16368SStephen M. Cameron 	{0xFFFF103C, "Unknown Smart Array", &SA5_access},
206edd16368SStephen M. Cameron };
207edd16368SStephen M. Cameron 
208a58e7e53SWebb Scales #define SCSI_CMD_BUSY ((struct scsi_cmnd *)&hpsa_cmd_busy)
209a58e7e53SWebb Scales static const struct scsi_cmnd hpsa_cmd_busy;
210a58e7e53SWebb Scales #define SCSI_CMD_IDLE ((struct scsi_cmnd *)&hpsa_cmd_idle)
211a58e7e53SWebb Scales static const struct scsi_cmnd hpsa_cmd_idle;
212edd16368SStephen M. Cameron static int number_of_controllers;
213edd16368SStephen M. Cameron 
21410f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id);
21510f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id);
21642a91641SDon Brace static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg);
217edd16368SStephen M. Cameron 
218edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
21942a91641SDon Brace static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd,
22042a91641SDon Brace 	void __user *arg);
221edd16368SStephen M. Cameron #endif
222edd16368SStephen M. Cameron 
223edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c);
224edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h);
22573153fe5SWebb Scales static void cmd_tagged_free(struct ctlr_info *h, struct CommandList *c);
22673153fe5SWebb Scales static struct CommandList *cmd_tagged_alloc(struct ctlr_info *h,
22773153fe5SWebb Scales 					    struct scsi_cmnd *scmd);
228a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
229b7bb24ebSStephen M. Cameron 	void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
230edd16368SStephen M. Cameron 	int cmd_type);
2312c143342SRobert Elliott static void hpsa_free_cmd_pool(struct ctlr_info *h);
232b7bb24ebSStephen M. Cameron #define VPD_PAGE (1 << 8)
233edd16368SStephen M. Cameron 
234f281233dSJeff Garzik static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
235a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *);
236a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
237a08a8471SStephen M. Cameron 	unsigned long elapsed_time);
2387c0a0229SDon Brace static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth);
239edd16368SStephen M. Cameron 
240edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
24175167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *scsicmd);
242edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev);
24341ce4c35SStephen Cameron static int hpsa_slave_configure(struct scsi_device *sdev);
244edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev);
245edd16368SStephen M. Cameron 
246edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
247edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
248edd16368SStephen M. Cameron 	struct CommandList *c);
249edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
250edd16368SStephen M. Cameron 	struct CommandList *c);
251303932fdSDon Brace /* performant mode helper functions */
252303932fdSDon Brace static void calc_bucket_map(int *bucket, int num_buckets,
2532b08b3e9SDon Brace 	int nsgs, int min_blocks, u32 *bucket_map);
254105a3dbcSRobert Elliott static void hpsa_free_performant_mode(struct ctlr_info *h);
255105a3dbcSRobert Elliott static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
256254f796bSMatt Gates static inline u32 next_command(struct ctlr_info *h, u8 q);
2576f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
2586f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
2591df8552aSStephen M. Cameron 			       u64 *cfg_offset);
2606f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
2611df8552aSStephen M. Cameron 				    unsigned long *memory_bar);
2626f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
2636f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
2646f039790SGreg Kroah-Hartman 				     int wait_for_ready);
26575167d2cSStephen M. Cameron static inline void finish_cmd(struct CommandList *c);
266c706a795SRobert Elliott static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h);
267fe5389c8SStephen M. Cameron #define BOARD_NOT_READY 0
268fe5389c8SStephen M. Cameron #define BOARD_READY 1
26923100dd9SStephen M. Cameron static void hpsa_drain_accel_commands(struct ctlr_info *h);
27076438d08SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h);
271c349775eSScott Teel static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
272c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
27303383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk);
274080ef1ccSDon Brace static void hpsa_command_resubmit_worker(struct work_struct *work);
27525163bd5SWebb Scales static u32 lockup_detected(struct ctlr_info *h);
27625163bd5SWebb Scales static int detect_controller_lockup(struct ctlr_info *h);
2778270b862SJoe Handzik static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device);
278edd16368SStephen M. Cameron 
279edd16368SStephen M. Cameron static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
280edd16368SStephen M. Cameron {
281edd16368SStephen M. Cameron 	unsigned long *priv = shost_priv(sdev->host);
282edd16368SStephen M. Cameron 	return (struct ctlr_info *) *priv;
283edd16368SStephen M. Cameron }
284edd16368SStephen M. Cameron 
285a23513e8SStephen M. Cameron static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
286a23513e8SStephen M. Cameron {
287a23513e8SStephen M. Cameron 	unsigned long *priv = shost_priv(sh);
288a23513e8SStephen M. Cameron 	return (struct ctlr_info *) *priv;
289a23513e8SStephen M. Cameron }
290a23513e8SStephen M. Cameron 
291a58e7e53SWebb Scales static inline bool hpsa_is_cmd_idle(struct CommandList *c)
292a58e7e53SWebb Scales {
293a58e7e53SWebb Scales 	return c->scsi_cmd == SCSI_CMD_IDLE;
294a58e7e53SWebb Scales }
295a58e7e53SWebb Scales 
296d604f533SWebb Scales static inline bool hpsa_is_pending_event(struct CommandList *c)
297d604f533SWebb Scales {
298d604f533SWebb Scales 	return c->abort_pending || c->reset_pending;
299d604f533SWebb Scales }
300d604f533SWebb Scales 
3019437ac43SStephen Cameron /* extract sense key, asc, and ascq from sense data.  -1 means invalid. */
3029437ac43SStephen Cameron static void decode_sense_data(const u8 *sense_data, int sense_data_len,
3039437ac43SStephen Cameron 			u8 *sense_key, u8 *asc, u8 *ascq)
3049437ac43SStephen Cameron {
3059437ac43SStephen Cameron 	struct scsi_sense_hdr sshdr;
3069437ac43SStephen Cameron 	bool rc;
3079437ac43SStephen Cameron 
3089437ac43SStephen Cameron 	*sense_key = -1;
3099437ac43SStephen Cameron 	*asc = -1;
3109437ac43SStephen Cameron 	*ascq = -1;
3119437ac43SStephen Cameron 
3129437ac43SStephen Cameron 	if (sense_data_len < 1)
3139437ac43SStephen Cameron 		return;
3149437ac43SStephen Cameron 
3159437ac43SStephen Cameron 	rc = scsi_normalize_sense(sense_data, sense_data_len, &sshdr);
3169437ac43SStephen Cameron 	if (rc) {
3179437ac43SStephen Cameron 		*sense_key = sshdr.sense_key;
3189437ac43SStephen Cameron 		*asc = sshdr.asc;
3199437ac43SStephen Cameron 		*ascq = sshdr.ascq;
3209437ac43SStephen Cameron 	}
3219437ac43SStephen Cameron }
3229437ac43SStephen Cameron 
323edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
324edd16368SStephen M. Cameron 	struct CommandList *c)
325edd16368SStephen M. Cameron {
3269437ac43SStephen Cameron 	u8 sense_key, asc, ascq;
3279437ac43SStephen Cameron 	int sense_len;
3289437ac43SStephen Cameron 
3299437ac43SStephen Cameron 	if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
3309437ac43SStephen Cameron 		sense_len = sizeof(c->err_info->SenseInfo);
3319437ac43SStephen Cameron 	else
3329437ac43SStephen Cameron 		sense_len = c->err_info->SenseLen;
3339437ac43SStephen Cameron 
3349437ac43SStephen Cameron 	decode_sense_data(c->err_info->SenseInfo, sense_len,
3359437ac43SStephen Cameron 				&sense_key, &asc, &ascq);
33681c27557SDon Brace 	if (sense_key != UNIT_ATTENTION || asc == 0xff)
337edd16368SStephen M. Cameron 		return 0;
338edd16368SStephen M. Cameron 
3399437ac43SStephen Cameron 	switch (asc) {
340edd16368SStephen M. Cameron 	case STATE_CHANGED:
3419437ac43SStephen Cameron 		dev_warn(&h->pdev->dev,
3422946e82bSRobert Elliott 			"%s: a state change detected, command retried\n",
3432946e82bSRobert Elliott 			h->devname);
344edd16368SStephen M. Cameron 		break;
345edd16368SStephen M. Cameron 	case LUN_FAILED:
3467f73695aSStephen M. Cameron 		dev_warn(&h->pdev->dev,
3472946e82bSRobert Elliott 			"%s: LUN failure detected\n", h->devname);
348edd16368SStephen M. Cameron 		break;
349edd16368SStephen M. Cameron 	case REPORT_LUNS_CHANGED:
3507f73695aSStephen M. Cameron 		dev_warn(&h->pdev->dev,
3512946e82bSRobert Elliott 			"%s: report LUN data changed\n", h->devname);
352edd16368SStephen M. Cameron 	/*
3534f4eb9f1SScott Teel 	 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
3544f4eb9f1SScott Teel 	 * target (array) devices.
355edd16368SStephen M. Cameron 	 */
356edd16368SStephen M. Cameron 		break;
357edd16368SStephen M. Cameron 	case POWER_OR_RESET:
3582946e82bSRobert Elliott 		dev_warn(&h->pdev->dev,
3592946e82bSRobert Elliott 			"%s: a power on or device reset detected\n",
3602946e82bSRobert Elliott 			h->devname);
361edd16368SStephen M. Cameron 		break;
362edd16368SStephen M. Cameron 	case UNIT_ATTENTION_CLEARED:
3632946e82bSRobert Elliott 		dev_warn(&h->pdev->dev,
3642946e82bSRobert Elliott 			"%s: unit attention cleared by another initiator\n",
3652946e82bSRobert Elliott 			h->devname);
366edd16368SStephen M. Cameron 		break;
367edd16368SStephen M. Cameron 	default:
3682946e82bSRobert Elliott 		dev_warn(&h->pdev->dev,
3692946e82bSRobert Elliott 			"%s: unknown unit attention detected\n",
3702946e82bSRobert Elliott 			h->devname);
371edd16368SStephen M. Cameron 		break;
372edd16368SStephen M. Cameron 	}
373edd16368SStephen M. Cameron 	return 1;
374edd16368SStephen M. Cameron }
375edd16368SStephen M. Cameron 
376852af20aSMatt Bondurant static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
377852af20aSMatt Bondurant {
378852af20aSMatt Bondurant 	if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
379852af20aSMatt Bondurant 		(c->err_info->ScsiStatus != SAM_STAT_BUSY &&
380852af20aSMatt Bondurant 		 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
381852af20aSMatt Bondurant 		return 0;
382852af20aSMatt Bondurant 	dev_warn(&h->pdev->dev, HPSA "device busy");
383852af20aSMatt Bondurant 	return 1;
384852af20aSMatt Bondurant }
385852af20aSMatt Bondurant 
386e985c58fSStephen Cameron static u32 lockup_detected(struct ctlr_info *h);
387e985c58fSStephen Cameron static ssize_t host_show_lockup_detected(struct device *dev,
388e985c58fSStephen Cameron 		struct device_attribute *attr, char *buf)
389e985c58fSStephen Cameron {
390e985c58fSStephen Cameron 	int ld;
391e985c58fSStephen Cameron 	struct ctlr_info *h;
392e985c58fSStephen Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
393e985c58fSStephen Cameron 
394e985c58fSStephen Cameron 	h = shost_to_hba(shost);
395e985c58fSStephen Cameron 	ld = lockup_detected(h);
396e985c58fSStephen Cameron 
397e985c58fSStephen Cameron 	return sprintf(buf, "ld=%d\n", ld);
398e985c58fSStephen Cameron }
399e985c58fSStephen Cameron 
400da0697bdSScott Teel static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
401da0697bdSScott Teel 					 struct device_attribute *attr,
402da0697bdSScott Teel 					 const char *buf, size_t count)
403da0697bdSScott Teel {
404da0697bdSScott Teel 	int status, len;
405da0697bdSScott Teel 	struct ctlr_info *h;
406da0697bdSScott Teel 	struct Scsi_Host *shost = class_to_shost(dev);
407da0697bdSScott Teel 	char tmpbuf[10];
408da0697bdSScott Teel 
409da0697bdSScott Teel 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
410da0697bdSScott Teel 		return -EACCES;
411da0697bdSScott Teel 	len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
412da0697bdSScott Teel 	strncpy(tmpbuf, buf, len);
413da0697bdSScott Teel 	tmpbuf[len] = '\0';
414da0697bdSScott Teel 	if (sscanf(tmpbuf, "%d", &status) != 1)
415da0697bdSScott Teel 		return -EINVAL;
416da0697bdSScott Teel 	h = shost_to_hba(shost);
417da0697bdSScott Teel 	h->acciopath_status = !!status;
418da0697bdSScott Teel 	dev_warn(&h->pdev->dev,
419da0697bdSScott Teel 		"hpsa: HP SSD Smart Path %s via sysfs update.\n",
420da0697bdSScott Teel 		h->acciopath_status ? "enabled" : "disabled");
421da0697bdSScott Teel 	return count;
422da0697bdSScott Teel }
423da0697bdSScott Teel 
4242ba8bfc8SStephen M. Cameron static ssize_t host_store_raid_offload_debug(struct device *dev,
4252ba8bfc8SStephen M. Cameron 					 struct device_attribute *attr,
4262ba8bfc8SStephen M. Cameron 					 const char *buf, size_t count)
4272ba8bfc8SStephen M. Cameron {
4282ba8bfc8SStephen M. Cameron 	int debug_level, len;
4292ba8bfc8SStephen M. Cameron 	struct ctlr_info *h;
4302ba8bfc8SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
4312ba8bfc8SStephen M. Cameron 	char tmpbuf[10];
4322ba8bfc8SStephen M. Cameron 
4332ba8bfc8SStephen M. Cameron 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
4342ba8bfc8SStephen M. Cameron 		return -EACCES;
4352ba8bfc8SStephen M. Cameron 	len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
4362ba8bfc8SStephen M. Cameron 	strncpy(tmpbuf, buf, len);
4372ba8bfc8SStephen M. Cameron 	tmpbuf[len] = '\0';
4382ba8bfc8SStephen M. Cameron 	if (sscanf(tmpbuf, "%d", &debug_level) != 1)
4392ba8bfc8SStephen M. Cameron 		return -EINVAL;
4402ba8bfc8SStephen M. Cameron 	if (debug_level < 0)
4412ba8bfc8SStephen M. Cameron 		debug_level = 0;
4422ba8bfc8SStephen M. Cameron 	h = shost_to_hba(shost);
4432ba8bfc8SStephen M. Cameron 	h->raid_offload_debug = debug_level;
4442ba8bfc8SStephen M. Cameron 	dev_warn(&h->pdev->dev, "hpsa: Set raid_offload_debug level = %d\n",
4452ba8bfc8SStephen M. Cameron 		h->raid_offload_debug);
4462ba8bfc8SStephen M. Cameron 	return count;
4472ba8bfc8SStephen M. Cameron }
4482ba8bfc8SStephen M. Cameron 
449edd16368SStephen M. Cameron static ssize_t host_store_rescan(struct device *dev,
450edd16368SStephen M. Cameron 				 struct device_attribute *attr,
451edd16368SStephen M. Cameron 				 const char *buf, size_t count)
452edd16368SStephen M. Cameron {
453edd16368SStephen M. Cameron 	struct ctlr_info *h;
454edd16368SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
455a23513e8SStephen M. Cameron 	h = shost_to_hba(shost);
45631468401SMike Miller 	hpsa_scan_start(h->scsi_host);
457edd16368SStephen M. Cameron 	return count;
458edd16368SStephen M. Cameron }
459edd16368SStephen M. Cameron 
460d28ce020SStephen M. Cameron static ssize_t host_show_firmware_revision(struct device *dev,
461d28ce020SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
462d28ce020SStephen M. Cameron {
463d28ce020SStephen M. Cameron 	struct ctlr_info *h;
464d28ce020SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
465d28ce020SStephen M. Cameron 	unsigned char *fwrev;
466d28ce020SStephen M. Cameron 
467d28ce020SStephen M. Cameron 	h = shost_to_hba(shost);
468d28ce020SStephen M. Cameron 	if (!h->hba_inquiry_data)
469d28ce020SStephen M. Cameron 		return 0;
470d28ce020SStephen M. Cameron 	fwrev = &h->hba_inquiry_data[32];
471d28ce020SStephen M. Cameron 	return snprintf(buf, 20, "%c%c%c%c\n",
472d28ce020SStephen M. Cameron 		fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
473d28ce020SStephen M. Cameron }
474d28ce020SStephen M. Cameron 
47594a13649SStephen M. Cameron static ssize_t host_show_commands_outstanding(struct device *dev,
47694a13649SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
47794a13649SStephen M. Cameron {
47894a13649SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
47994a13649SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(shost);
48094a13649SStephen M. Cameron 
4810cbf768eSStephen M. Cameron 	return snprintf(buf, 20, "%d\n",
4820cbf768eSStephen M. Cameron 			atomic_read(&h->commands_outstanding));
48394a13649SStephen M. Cameron }
48494a13649SStephen M. Cameron 
485745a7a25SStephen M. Cameron static ssize_t host_show_transport_mode(struct device *dev,
486745a7a25SStephen M. Cameron 	struct device_attribute *attr, char *buf)
487745a7a25SStephen M. Cameron {
488745a7a25SStephen M. Cameron 	struct ctlr_info *h;
489745a7a25SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
490745a7a25SStephen M. Cameron 
491745a7a25SStephen M. Cameron 	h = shost_to_hba(shost);
492745a7a25SStephen M. Cameron 	return snprintf(buf, 20, "%s\n",
493960a30e7SStephen M. Cameron 		h->transMethod & CFGTBL_Trans_Performant ?
494745a7a25SStephen M. Cameron 			"performant" : "simple");
495745a7a25SStephen M. Cameron }
496745a7a25SStephen M. Cameron 
497da0697bdSScott Teel static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
498da0697bdSScott Teel 	struct device_attribute *attr, char *buf)
499da0697bdSScott Teel {
500da0697bdSScott Teel 	struct ctlr_info *h;
501da0697bdSScott Teel 	struct Scsi_Host *shost = class_to_shost(dev);
502da0697bdSScott Teel 
503da0697bdSScott Teel 	h = shost_to_hba(shost);
504da0697bdSScott Teel 	return snprintf(buf, 30, "HP SSD Smart Path %s\n",
505da0697bdSScott Teel 		(h->acciopath_status == 1) ?  "enabled" : "disabled");
506da0697bdSScott Teel }
507da0697bdSScott Teel 
50846380786SStephen M. Cameron /* List of controllers which cannot be hard reset on kexec with reset_devices */
509941b1cdaSStephen M. Cameron static u32 unresettable_controller[] = {
510941b1cdaSStephen M. Cameron 	0x324a103C, /* Smart Array P712m */
511941b1cdaSStephen M. Cameron 	0x324b103C, /* Smart Array P711m */
512941b1cdaSStephen M. Cameron 	0x3223103C, /* Smart Array P800 */
513941b1cdaSStephen M. Cameron 	0x3234103C, /* Smart Array P400 */
514941b1cdaSStephen M. Cameron 	0x3235103C, /* Smart Array P400i */
515941b1cdaSStephen M. Cameron 	0x3211103C, /* Smart Array E200i */
516941b1cdaSStephen M. Cameron 	0x3212103C, /* Smart Array E200 */
517941b1cdaSStephen M. Cameron 	0x3213103C, /* Smart Array E200i */
518941b1cdaSStephen M. Cameron 	0x3214103C, /* Smart Array E200i */
519941b1cdaSStephen M. Cameron 	0x3215103C, /* Smart Array E200i */
520941b1cdaSStephen M. Cameron 	0x3237103C, /* Smart Array E500 */
521941b1cdaSStephen M. Cameron 	0x323D103C, /* Smart Array P700m */
5227af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
523941b1cdaSStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
524941b1cdaSStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
5255a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
5265a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
5275a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
5285a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
5295a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
5305a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
531941b1cdaSStephen M. Cameron };
532941b1cdaSStephen M. Cameron 
53346380786SStephen M. Cameron /* List of controllers which cannot even be soft reset */
53446380786SStephen M. Cameron static u32 soft_unresettable_controller[] = {
5357af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
5365a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
5375a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
5385a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
5395a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
5405a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
5415a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
54246380786SStephen M. Cameron 	/* Exclude 640x boards.  These are two pci devices in one slot
54346380786SStephen M. Cameron 	 * which share a battery backed cache module.  One controls the
54446380786SStephen M. Cameron 	 * cache, the other accesses the cache through the one that controls
54546380786SStephen M. Cameron 	 * it.  If we reset the one controlling the cache, the other will
54646380786SStephen M. Cameron 	 * likely not be happy.  Just forbid resetting this conjoined mess.
54746380786SStephen M. Cameron 	 * The 640x isn't really supported by hpsa anyway.
54846380786SStephen M. Cameron 	 */
54946380786SStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
55046380786SStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
55146380786SStephen M. Cameron };
55246380786SStephen M. Cameron 
5539b5c48c2SStephen Cameron static u32 needs_abort_tags_swizzled[] = {
5549b5c48c2SStephen Cameron 	0x323D103C, /* Smart Array P700m */
5559b5c48c2SStephen Cameron 	0x324a103C, /* Smart Array P712m */
5569b5c48c2SStephen Cameron 	0x324b103C, /* SmartArray P711m */
5579b5c48c2SStephen Cameron };
5589b5c48c2SStephen Cameron 
5599b5c48c2SStephen Cameron static int board_id_in_array(u32 a[], int nelems, u32 board_id)
560941b1cdaSStephen M. Cameron {
561941b1cdaSStephen M. Cameron 	int i;
562941b1cdaSStephen M. Cameron 
5639b5c48c2SStephen Cameron 	for (i = 0; i < nelems; i++)
5649b5c48c2SStephen Cameron 		if (a[i] == board_id)
565941b1cdaSStephen M. Cameron 			return 1;
5669b5c48c2SStephen Cameron 	return 0;
5679b5c48c2SStephen Cameron }
5689b5c48c2SStephen Cameron 
5699b5c48c2SStephen Cameron static int ctlr_is_hard_resettable(u32 board_id)
5709b5c48c2SStephen Cameron {
5719b5c48c2SStephen Cameron 	return !board_id_in_array(unresettable_controller,
5729b5c48c2SStephen Cameron 			ARRAY_SIZE(unresettable_controller), board_id);
573941b1cdaSStephen M. Cameron }
574941b1cdaSStephen M. Cameron 
57546380786SStephen M. Cameron static int ctlr_is_soft_resettable(u32 board_id)
57646380786SStephen M. Cameron {
5779b5c48c2SStephen Cameron 	return !board_id_in_array(soft_unresettable_controller,
5789b5c48c2SStephen Cameron 			ARRAY_SIZE(soft_unresettable_controller), board_id);
57946380786SStephen M. Cameron }
58046380786SStephen M. Cameron 
58146380786SStephen M. Cameron static int ctlr_is_resettable(u32 board_id)
58246380786SStephen M. Cameron {
58346380786SStephen M. Cameron 	return ctlr_is_hard_resettable(board_id) ||
58446380786SStephen M. Cameron 		ctlr_is_soft_resettable(board_id);
58546380786SStephen M. Cameron }
58646380786SStephen M. Cameron 
5879b5c48c2SStephen Cameron static int ctlr_needs_abort_tags_swizzled(u32 board_id)
5889b5c48c2SStephen Cameron {
5899b5c48c2SStephen Cameron 	return board_id_in_array(needs_abort_tags_swizzled,
5909b5c48c2SStephen Cameron 			ARRAY_SIZE(needs_abort_tags_swizzled), board_id);
5919b5c48c2SStephen Cameron }
5929b5c48c2SStephen Cameron 
593941b1cdaSStephen M. Cameron static ssize_t host_show_resettable(struct device *dev,
594941b1cdaSStephen M. Cameron 	struct device_attribute *attr, char *buf)
595941b1cdaSStephen M. Cameron {
596941b1cdaSStephen M. Cameron 	struct ctlr_info *h;
597941b1cdaSStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
598941b1cdaSStephen M. Cameron 
599941b1cdaSStephen M. Cameron 	h = shost_to_hba(shost);
60046380786SStephen M. Cameron 	return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
601941b1cdaSStephen M. Cameron }
602941b1cdaSStephen M. Cameron 
603edd16368SStephen M. Cameron static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
604edd16368SStephen M. Cameron {
605edd16368SStephen M. Cameron 	return (scsi3addr[3] & 0xC0) == 0x40;
606edd16368SStephen M. Cameron }
607edd16368SStephen M. Cameron 
608f2ef0ce7SRobert Elliott static const char * const raid_label[] = { "0", "4", "1(+0)", "5", "5+1", "6",
609f2ef0ce7SRobert Elliott 	"1(+0)ADM", "UNKNOWN"
610edd16368SStephen M. Cameron };
6116b80b18fSScott Teel #define HPSA_RAID_0	0
6126b80b18fSScott Teel #define HPSA_RAID_4	1
6136b80b18fSScott Teel #define HPSA_RAID_1	2	/* also used for RAID 10 */
6146b80b18fSScott Teel #define HPSA_RAID_5	3	/* also used for RAID 50 */
6156b80b18fSScott Teel #define HPSA_RAID_51	4
6166b80b18fSScott Teel #define HPSA_RAID_6	5	/* also used for RAID 60 */
6176b80b18fSScott Teel #define HPSA_RAID_ADM	6	/* also used for RAID 1+0 ADM */
618edd16368SStephen M. Cameron #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
619edd16368SStephen M. Cameron 
620edd16368SStephen M. Cameron static ssize_t raid_level_show(struct device *dev,
621edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
622edd16368SStephen M. Cameron {
623edd16368SStephen M. Cameron 	ssize_t l = 0;
62482a72c0aSStephen M. Cameron 	unsigned char rlevel;
625edd16368SStephen M. Cameron 	struct ctlr_info *h;
626edd16368SStephen M. Cameron 	struct scsi_device *sdev;
627edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
628edd16368SStephen M. Cameron 	unsigned long flags;
629edd16368SStephen M. Cameron 
630edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
631edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
632edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
633edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
634edd16368SStephen M. Cameron 	if (!hdev) {
635edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
636edd16368SStephen M. Cameron 		return -ENODEV;
637edd16368SStephen M. Cameron 	}
638edd16368SStephen M. Cameron 
639edd16368SStephen M. Cameron 	/* Is this even a logical drive? */
640edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
641edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
642edd16368SStephen M. Cameron 		l = snprintf(buf, PAGE_SIZE, "N/A\n");
643edd16368SStephen M. Cameron 		return l;
644edd16368SStephen M. Cameron 	}
645edd16368SStephen M. Cameron 
646edd16368SStephen M. Cameron 	rlevel = hdev->raid_level;
647edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
64882a72c0aSStephen M. Cameron 	if (rlevel > RAID_UNKNOWN)
649edd16368SStephen M. Cameron 		rlevel = RAID_UNKNOWN;
650edd16368SStephen M. Cameron 	l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
651edd16368SStephen M. Cameron 	return l;
652edd16368SStephen M. Cameron }
653edd16368SStephen M. Cameron 
654edd16368SStephen M. Cameron static ssize_t lunid_show(struct device *dev,
655edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
656edd16368SStephen M. Cameron {
657edd16368SStephen M. Cameron 	struct ctlr_info *h;
658edd16368SStephen M. Cameron 	struct scsi_device *sdev;
659edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
660edd16368SStephen M. Cameron 	unsigned long flags;
661edd16368SStephen M. Cameron 	unsigned char lunid[8];
662edd16368SStephen M. Cameron 
663edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
664edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
665edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
666edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
667edd16368SStephen M. Cameron 	if (!hdev) {
668edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
669edd16368SStephen M. Cameron 		return -ENODEV;
670edd16368SStephen M. Cameron 	}
671edd16368SStephen M. Cameron 	memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
672edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
673edd16368SStephen M. Cameron 	return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
674edd16368SStephen M. Cameron 		lunid[0], lunid[1], lunid[2], lunid[3],
675edd16368SStephen M. Cameron 		lunid[4], lunid[5], lunid[6], lunid[7]);
676edd16368SStephen M. Cameron }
677edd16368SStephen M. Cameron 
678edd16368SStephen M. Cameron static ssize_t unique_id_show(struct device *dev,
679edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
680edd16368SStephen M. Cameron {
681edd16368SStephen M. Cameron 	struct ctlr_info *h;
682edd16368SStephen M. Cameron 	struct scsi_device *sdev;
683edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
684edd16368SStephen M. Cameron 	unsigned long flags;
685edd16368SStephen M. Cameron 	unsigned char sn[16];
686edd16368SStephen M. Cameron 
687edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
688edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
689edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
690edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
691edd16368SStephen M. Cameron 	if (!hdev) {
692edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
693edd16368SStephen M. Cameron 		return -ENODEV;
694edd16368SStephen M. Cameron 	}
695edd16368SStephen M. Cameron 	memcpy(sn, hdev->device_id, sizeof(sn));
696edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
697edd16368SStephen M. Cameron 	return snprintf(buf, 16 * 2 + 2,
698edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X"
699edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X\n",
700edd16368SStephen M. Cameron 			sn[0], sn[1], sn[2], sn[3],
701edd16368SStephen M. Cameron 			sn[4], sn[5], sn[6], sn[7],
702edd16368SStephen M. Cameron 			sn[8], sn[9], sn[10], sn[11],
703edd16368SStephen M. Cameron 			sn[12], sn[13], sn[14], sn[15]);
704edd16368SStephen M. Cameron }
705edd16368SStephen M. Cameron 
706c1988684SScott Teel static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
707c1988684SScott Teel 	     struct device_attribute *attr, char *buf)
708c1988684SScott Teel {
709c1988684SScott Teel 	struct ctlr_info *h;
710c1988684SScott Teel 	struct scsi_device *sdev;
711c1988684SScott Teel 	struct hpsa_scsi_dev_t *hdev;
712c1988684SScott Teel 	unsigned long flags;
713c1988684SScott Teel 	int offload_enabled;
714c1988684SScott Teel 
715c1988684SScott Teel 	sdev = to_scsi_device(dev);
716c1988684SScott Teel 	h = sdev_to_hba(sdev);
717c1988684SScott Teel 	spin_lock_irqsave(&h->lock, flags);
718c1988684SScott Teel 	hdev = sdev->hostdata;
719c1988684SScott Teel 	if (!hdev) {
720c1988684SScott Teel 		spin_unlock_irqrestore(&h->lock, flags);
721c1988684SScott Teel 		return -ENODEV;
722c1988684SScott Teel 	}
723c1988684SScott Teel 	offload_enabled = hdev->offload_enabled;
724c1988684SScott Teel 	spin_unlock_irqrestore(&h->lock, flags);
725c1988684SScott Teel 	return snprintf(buf, 20, "%d\n", offload_enabled);
726c1988684SScott Teel }
727c1988684SScott Teel 
7288270b862SJoe Handzik #define MAX_PATHS 8
7298270b862SJoe Handzik #define PATH_STRING_LEN 50
7308270b862SJoe Handzik 
7318270b862SJoe Handzik static ssize_t path_info_show(struct device *dev,
7328270b862SJoe Handzik 	     struct device_attribute *attr, char *buf)
7338270b862SJoe Handzik {
7348270b862SJoe Handzik 	struct ctlr_info *h;
7358270b862SJoe Handzik 	struct scsi_device *sdev;
7368270b862SJoe Handzik 	struct hpsa_scsi_dev_t *hdev;
7378270b862SJoe Handzik 	unsigned long flags;
7388270b862SJoe Handzik 	int i;
7398270b862SJoe Handzik 	int output_len = 0;
7408270b862SJoe Handzik 	u8 box;
7418270b862SJoe Handzik 	u8 bay;
7428270b862SJoe Handzik 	u8 path_map_index = 0;
7438270b862SJoe Handzik 	char *active;
7448270b862SJoe Handzik 	unsigned char phys_connector[2];
7458270b862SJoe Handzik 	unsigned char path[MAX_PATHS][PATH_STRING_LEN];
7468270b862SJoe Handzik 
7478270b862SJoe Handzik 	memset(path, 0, MAX_PATHS * PATH_STRING_LEN);
7488270b862SJoe Handzik 	sdev = to_scsi_device(dev);
7498270b862SJoe Handzik 	h = sdev_to_hba(sdev);
7508270b862SJoe Handzik 	spin_lock_irqsave(&h->devlock, flags);
7518270b862SJoe Handzik 	hdev = sdev->hostdata;
7528270b862SJoe Handzik 	if (!hdev) {
7538270b862SJoe Handzik 		spin_unlock_irqrestore(&h->devlock, flags);
7548270b862SJoe Handzik 		return -ENODEV;
7558270b862SJoe Handzik 	}
7568270b862SJoe Handzik 
7578270b862SJoe Handzik 	bay = hdev->bay;
7588270b862SJoe Handzik 	for (i = 0; i < MAX_PATHS; i++) {
7598270b862SJoe Handzik 		path_map_index = 1<<i;
7608270b862SJoe Handzik 		if (i == hdev->active_path_index)
7618270b862SJoe Handzik 			active = "Active";
7628270b862SJoe Handzik 		else if (hdev->path_map & path_map_index)
7638270b862SJoe Handzik 			active = "Inactive";
7648270b862SJoe Handzik 		else
7658270b862SJoe Handzik 			continue;
7668270b862SJoe Handzik 
7678270b862SJoe Handzik 		output_len = snprintf(path[i],
7688270b862SJoe Handzik 				PATH_STRING_LEN, "[%d:%d:%d:%d] %20.20s ",
7698270b862SJoe Handzik 				h->scsi_host->host_no,
7708270b862SJoe Handzik 				hdev->bus, hdev->target, hdev->lun,
7718270b862SJoe Handzik 				scsi_device_type(hdev->devtype));
7728270b862SJoe Handzik 
7738270b862SJoe Handzik 		if (is_ext_target(h, hdev) ||
7748270b862SJoe Handzik 			(hdev->devtype == TYPE_RAID) ||
7758270b862SJoe Handzik 			is_logical_dev_addr_mode(hdev->scsi3addr)) {
7768270b862SJoe Handzik 			output_len += snprintf(path[i] + output_len,
7778270b862SJoe Handzik 						PATH_STRING_LEN, "%s\n",
7788270b862SJoe Handzik 						active);
7798270b862SJoe Handzik 			continue;
7808270b862SJoe Handzik 		}
7818270b862SJoe Handzik 
7828270b862SJoe Handzik 		box = hdev->box[i];
7838270b862SJoe Handzik 		memcpy(&phys_connector, &hdev->phys_connector[i],
7848270b862SJoe Handzik 			sizeof(phys_connector));
7858270b862SJoe Handzik 		if (phys_connector[0] < '0')
7868270b862SJoe Handzik 			phys_connector[0] = '0';
7878270b862SJoe Handzik 		if (phys_connector[1] < '0')
7888270b862SJoe Handzik 			phys_connector[1] = '0';
7898270b862SJoe Handzik 		if (hdev->phys_connector[i] > 0)
7908270b862SJoe Handzik 			output_len += snprintf(path[i] + output_len,
7918270b862SJoe Handzik 				PATH_STRING_LEN,
7928270b862SJoe Handzik 				"PORT: %.2s ",
7938270b862SJoe Handzik 				phys_connector);
794b9092b79SKevin Barnett 		if (hdev->devtype == TYPE_DISK &&
795b9092b79SKevin Barnett 			hdev->expose_state != HPSA_DO_NOT_EXPOSE) {
7968270b862SJoe Handzik 			if (box == 0 || box == 0xFF) {
7978270b862SJoe Handzik 				output_len += snprintf(path[i] + output_len,
7988270b862SJoe Handzik 					PATH_STRING_LEN,
7998270b862SJoe Handzik 					"BAY: %hhu %s\n",
8008270b862SJoe Handzik 					bay, active);
8018270b862SJoe Handzik 			} else {
8028270b862SJoe Handzik 				output_len += snprintf(path[i] + output_len,
8038270b862SJoe Handzik 					PATH_STRING_LEN,
8048270b862SJoe Handzik 					"BOX: %hhu BAY: %hhu %s\n",
8058270b862SJoe Handzik 					box, bay, active);
8068270b862SJoe Handzik 			}
8078270b862SJoe Handzik 		} else if (box != 0 && box != 0xFF) {
8088270b862SJoe Handzik 			output_len += snprintf(path[i] + output_len,
8098270b862SJoe Handzik 				PATH_STRING_LEN, "BOX: %hhu %s\n",
8108270b862SJoe Handzik 				box, active);
8118270b862SJoe Handzik 		} else
8128270b862SJoe Handzik 			output_len += snprintf(path[i] + output_len,
8138270b862SJoe Handzik 				PATH_STRING_LEN, "%s\n", active);
8148270b862SJoe Handzik 	}
8158270b862SJoe Handzik 
8168270b862SJoe Handzik 	spin_unlock_irqrestore(&h->devlock, flags);
8178270b862SJoe Handzik 	return snprintf(buf, output_len+1, "%s%s%s%s%s%s%s%s",
8188270b862SJoe Handzik 		path[0], path[1], path[2], path[3],
8198270b862SJoe Handzik 		path[4], path[5], path[6], path[7]);
8208270b862SJoe Handzik }
8218270b862SJoe Handzik 
8223f5eac3aSStephen M. Cameron static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
8233f5eac3aSStephen M. Cameron static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
8243f5eac3aSStephen M. Cameron static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
8253f5eac3aSStephen M. Cameron static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
826c1988684SScott Teel static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
827c1988684SScott Teel 			host_show_hp_ssd_smart_path_enabled, NULL);
8288270b862SJoe Handzik static DEVICE_ATTR(path_info, S_IRUGO, path_info_show, NULL);
829da0697bdSScott Teel static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
830da0697bdSScott Teel 		host_show_hp_ssd_smart_path_status,
831da0697bdSScott Teel 		host_store_hp_ssd_smart_path_status);
8322ba8bfc8SStephen M. Cameron static DEVICE_ATTR(raid_offload_debug, S_IWUSR, NULL,
8332ba8bfc8SStephen M. Cameron 			host_store_raid_offload_debug);
8343f5eac3aSStephen M. Cameron static DEVICE_ATTR(firmware_revision, S_IRUGO,
8353f5eac3aSStephen M. Cameron 	host_show_firmware_revision, NULL);
8363f5eac3aSStephen M. Cameron static DEVICE_ATTR(commands_outstanding, S_IRUGO,
8373f5eac3aSStephen M. Cameron 	host_show_commands_outstanding, NULL);
8383f5eac3aSStephen M. Cameron static DEVICE_ATTR(transport_mode, S_IRUGO,
8393f5eac3aSStephen M. Cameron 	host_show_transport_mode, NULL);
840941b1cdaSStephen M. Cameron static DEVICE_ATTR(resettable, S_IRUGO,
841941b1cdaSStephen M. Cameron 	host_show_resettable, NULL);
842e985c58fSStephen Cameron static DEVICE_ATTR(lockup_detected, S_IRUGO,
843e985c58fSStephen Cameron 	host_show_lockup_detected, NULL);
8443f5eac3aSStephen M. Cameron 
8453f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_sdev_attrs[] = {
8463f5eac3aSStephen M. Cameron 	&dev_attr_raid_level,
8473f5eac3aSStephen M. Cameron 	&dev_attr_lunid,
8483f5eac3aSStephen M. Cameron 	&dev_attr_unique_id,
849c1988684SScott Teel 	&dev_attr_hp_ssd_smart_path_enabled,
8508270b862SJoe Handzik 	&dev_attr_path_info,
851e985c58fSStephen Cameron 	&dev_attr_lockup_detected,
8523f5eac3aSStephen M. Cameron 	NULL,
8533f5eac3aSStephen M. Cameron };
8543f5eac3aSStephen M. Cameron 
8553f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_shost_attrs[] = {
8563f5eac3aSStephen M. Cameron 	&dev_attr_rescan,
8573f5eac3aSStephen M. Cameron 	&dev_attr_firmware_revision,
8583f5eac3aSStephen M. Cameron 	&dev_attr_commands_outstanding,
8593f5eac3aSStephen M. Cameron 	&dev_attr_transport_mode,
860941b1cdaSStephen M. Cameron 	&dev_attr_resettable,
861da0697bdSScott Teel 	&dev_attr_hp_ssd_smart_path_status,
8622ba8bfc8SStephen M. Cameron 	&dev_attr_raid_offload_debug,
8633f5eac3aSStephen M. Cameron 	NULL,
8643f5eac3aSStephen M. Cameron };
8653f5eac3aSStephen M. Cameron 
86641ce4c35SStephen Cameron #define HPSA_NRESERVED_CMDS	(HPSA_CMDS_RESERVED_FOR_ABORTS + \
86741ce4c35SStephen Cameron 		HPSA_CMDS_RESERVED_FOR_DRIVER + HPSA_MAX_CONCURRENT_PASSTHRUS)
86841ce4c35SStephen Cameron 
8693f5eac3aSStephen M. Cameron static struct scsi_host_template hpsa_driver_template = {
8703f5eac3aSStephen M. Cameron 	.module			= THIS_MODULE,
871f79cfec6SStephen M. Cameron 	.name			= HPSA,
872f79cfec6SStephen M. Cameron 	.proc_name		= HPSA,
8733f5eac3aSStephen M. Cameron 	.queuecommand		= hpsa_scsi_queue_command,
8743f5eac3aSStephen M. Cameron 	.scan_start		= hpsa_scan_start,
8753f5eac3aSStephen M. Cameron 	.scan_finished		= hpsa_scan_finished,
8767c0a0229SDon Brace 	.change_queue_depth	= hpsa_change_queue_depth,
8773f5eac3aSStephen M. Cameron 	.this_id		= -1,
8783f5eac3aSStephen M. Cameron 	.use_clustering		= ENABLE_CLUSTERING,
87975167d2cSStephen M. Cameron 	.eh_abort_handler	= hpsa_eh_abort_handler,
8803f5eac3aSStephen M. Cameron 	.eh_device_reset_handler = hpsa_eh_device_reset_handler,
8813f5eac3aSStephen M. Cameron 	.ioctl			= hpsa_ioctl,
8823f5eac3aSStephen M. Cameron 	.slave_alloc		= hpsa_slave_alloc,
88341ce4c35SStephen Cameron 	.slave_configure	= hpsa_slave_configure,
8843f5eac3aSStephen M. Cameron 	.slave_destroy		= hpsa_slave_destroy,
8853f5eac3aSStephen M. Cameron #ifdef CONFIG_COMPAT
8863f5eac3aSStephen M. Cameron 	.compat_ioctl		= hpsa_compat_ioctl,
8873f5eac3aSStephen M. Cameron #endif
8883f5eac3aSStephen M. Cameron 	.sdev_attrs = hpsa_sdev_attrs,
8893f5eac3aSStephen M. Cameron 	.shost_attrs = hpsa_shost_attrs,
890c0d6a4d1SStephen M. Cameron 	.max_sectors = 8192,
89154b2b50cSMartin K. Petersen 	.no_write_same = 1,
8923f5eac3aSStephen M. Cameron };
8933f5eac3aSStephen M. Cameron 
894254f796bSMatt Gates static inline u32 next_command(struct ctlr_info *h, u8 q)
8953f5eac3aSStephen M. Cameron {
8963f5eac3aSStephen M. Cameron 	u32 a;
897072b0518SStephen M. Cameron 	struct reply_queue_buffer *rq = &h->reply_queue[q];
8983f5eac3aSStephen M. Cameron 
899e1f7de0cSMatt Gates 	if (h->transMethod & CFGTBL_Trans_io_accel1)
900e1f7de0cSMatt Gates 		return h->access.command_completed(h, q);
901e1f7de0cSMatt Gates 
9023f5eac3aSStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
903254f796bSMatt Gates 		return h->access.command_completed(h, q);
9043f5eac3aSStephen M. Cameron 
905254f796bSMatt Gates 	if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
906254f796bSMatt Gates 		a = rq->head[rq->current_entry];
907254f796bSMatt Gates 		rq->current_entry++;
9080cbf768eSStephen M. Cameron 		atomic_dec(&h->commands_outstanding);
9093f5eac3aSStephen M. Cameron 	} else {
9103f5eac3aSStephen M. Cameron 		a = FIFO_EMPTY;
9113f5eac3aSStephen M. Cameron 	}
9123f5eac3aSStephen M. Cameron 	/* Check for wraparound */
913254f796bSMatt Gates 	if (rq->current_entry == h->max_commands) {
914254f796bSMatt Gates 		rq->current_entry = 0;
915254f796bSMatt Gates 		rq->wraparound ^= 1;
9163f5eac3aSStephen M. Cameron 	}
9173f5eac3aSStephen M. Cameron 	return a;
9183f5eac3aSStephen M. Cameron }
9193f5eac3aSStephen M. Cameron 
920c349775eSScott Teel /*
921c349775eSScott Teel  * There are some special bits in the bus address of the
922c349775eSScott Teel  * command that we have to set for the controller to know
923c349775eSScott Teel  * how to process the command:
924c349775eSScott Teel  *
925c349775eSScott Teel  * Normal performant mode:
926c349775eSScott Teel  * bit 0: 1 means performant mode, 0 means simple mode.
927c349775eSScott Teel  * bits 1-3 = block fetch table entry
928c349775eSScott Teel  * bits 4-6 = command type (== 0)
929c349775eSScott Teel  *
930c349775eSScott Teel  * ioaccel1 mode:
931c349775eSScott Teel  * bit 0 = "performant mode" bit.
932c349775eSScott Teel  * bits 1-3 = block fetch table entry
933c349775eSScott Teel  * bits 4-6 = command type (== 110)
934c349775eSScott Teel  * (command type is needed because ioaccel1 mode
935c349775eSScott Teel  * commands are submitted through the same register as normal
936c349775eSScott Teel  * mode commands, so this is how the controller knows whether
937c349775eSScott Teel  * the command is normal mode or ioaccel1 mode.)
938c349775eSScott Teel  *
939c349775eSScott Teel  * ioaccel2 mode:
940c349775eSScott Teel  * bit 0 = "performant mode" bit.
941c349775eSScott Teel  * bits 1-4 = block fetch table entry (note extra bit)
942c349775eSScott Teel  * bits 4-6 = not needed, because ioaccel2 mode has
943c349775eSScott Teel  * a separate special register for submitting commands.
944c349775eSScott Teel  */
945c349775eSScott Teel 
94625163bd5SWebb Scales /*
94725163bd5SWebb Scales  * set_performant_mode: Modify the tag for cciss performant
9483f5eac3aSStephen M. Cameron  * set bit 0 for pull model, bits 3-1 for block fetch
9493f5eac3aSStephen M. Cameron  * register number
9503f5eac3aSStephen M. Cameron  */
95125163bd5SWebb Scales #define DEFAULT_REPLY_QUEUE (-1)
95225163bd5SWebb Scales static void set_performant_mode(struct ctlr_info *h, struct CommandList *c,
95325163bd5SWebb Scales 					int reply_queue)
9543f5eac3aSStephen M. Cameron {
955254f796bSMatt Gates 	if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
9563f5eac3aSStephen M. Cameron 		c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
95725163bd5SWebb Scales 		if (unlikely(!h->msix_vector))
95825163bd5SWebb Scales 			return;
95925163bd5SWebb Scales 		if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
960254f796bSMatt Gates 			c->Header.ReplyQueue =
961804a5cb5SJohn Kacur 				raw_smp_processor_id() % h->nreply_queues;
96225163bd5SWebb Scales 		else
96325163bd5SWebb Scales 			c->Header.ReplyQueue = reply_queue % h->nreply_queues;
964254f796bSMatt Gates 	}
9653f5eac3aSStephen M. Cameron }
9663f5eac3aSStephen M. Cameron 
967c349775eSScott Teel static void set_ioaccel1_performant_mode(struct ctlr_info *h,
96825163bd5SWebb Scales 						struct CommandList *c,
96925163bd5SWebb Scales 						int reply_queue)
970c349775eSScott Teel {
971c349775eSScott Teel 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
972c349775eSScott Teel 
97325163bd5SWebb Scales 	/*
97425163bd5SWebb Scales 	 * Tell the controller to post the reply to the queue for this
975c349775eSScott Teel 	 * processor.  This seems to give the best I/O throughput.
976c349775eSScott Teel 	 */
97725163bd5SWebb Scales 	if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
978c349775eSScott Teel 		cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
97925163bd5SWebb Scales 	else
98025163bd5SWebb Scales 		cp->ReplyQueue = reply_queue % h->nreply_queues;
98125163bd5SWebb Scales 	/*
98225163bd5SWebb Scales 	 * Set the bits in the address sent down to include:
983c349775eSScott Teel 	 *  - performant mode bit (bit 0)
984c349775eSScott Teel 	 *  - pull count (bits 1-3)
985c349775eSScott Teel 	 *  - command type (bits 4-6)
986c349775eSScott Teel 	 */
987c349775eSScott Teel 	c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
988c349775eSScott Teel 					IOACCEL1_BUSADDR_CMDTYPE;
989c349775eSScott Teel }
990c349775eSScott Teel 
9918be986ccSStephen Cameron static void set_ioaccel2_tmf_performant_mode(struct ctlr_info *h,
9928be986ccSStephen Cameron 						struct CommandList *c,
9938be986ccSStephen Cameron 						int reply_queue)
9948be986ccSStephen Cameron {
9958be986ccSStephen Cameron 	struct hpsa_tmf_struct *cp = (struct hpsa_tmf_struct *)
9968be986ccSStephen Cameron 		&h->ioaccel2_cmd_pool[c->cmdindex];
9978be986ccSStephen Cameron 
9988be986ccSStephen Cameron 	/* Tell the controller to post the reply to the queue for this
9998be986ccSStephen Cameron 	 * processor.  This seems to give the best I/O throughput.
10008be986ccSStephen Cameron 	 */
10018be986ccSStephen Cameron 	if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
10028be986ccSStephen Cameron 		cp->reply_queue = smp_processor_id() % h->nreply_queues;
10038be986ccSStephen Cameron 	else
10048be986ccSStephen Cameron 		cp->reply_queue = reply_queue % h->nreply_queues;
10058be986ccSStephen Cameron 	/* Set the bits in the address sent down to include:
10068be986ccSStephen Cameron 	 *  - performant mode bit not used in ioaccel mode 2
10078be986ccSStephen Cameron 	 *  - pull count (bits 0-3)
10088be986ccSStephen Cameron 	 *  - command type isn't needed for ioaccel2
10098be986ccSStephen Cameron 	 */
10108be986ccSStephen Cameron 	c->busaddr |= h->ioaccel2_blockFetchTable[0];
10118be986ccSStephen Cameron }
10128be986ccSStephen Cameron 
1013c349775eSScott Teel static void set_ioaccel2_performant_mode(struct ctlr_info *h,
101425163bd5SWebb Scales 						struct CommandList *c,
101525163bd5SWebb Scales 						int reply_queue)
1016c349775eSScott Teel {
1017c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
1018c349775eSScott Teel 
101925163bd5SWebb Scales 	/*
102025163bd5SWebb Scales 	 * Tell the controller to post the reply to the queue for this
1021c349775eSScott Teel 	 * processor.  This seems to give the best I/O throughput.
1022c349775eSScott Teel 	 */
102325163bd5SWebb Scales 	if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
1024c349775eSScott Teel 		cp->reply_queue = smp_processor_id() % h->nreply_queues;
102525163bd5SWebb Scales 	else
102625163bd5SWebb Scales 		cp->reply_queue = reply_queue % h->nreply_queues;
102725163bd5SWebb Scales 	/*
102825163bd5SWebb Scales 	 * Set the bits in the address sent down to include:
1029c349775eSScott Teel 	 *  - performant mode bit not used in ioaccel mode 2
1030c349775eSScott Teel 	 *  - pull count (bits 0-3)
1031c349775eSScott Teel 	 *  - command type isn't needed for ioaccel2
1032c349775eSScott Teel 	 */
1033c349775eSScott Teel 	c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
1034c349775eSScott Teel }
1035c349775eSScott Teel 
1036e85c5974SStephen M. Cameron static int is_firmware_flash_cmd(u8 *cdb)
1037e85c5974SStephen M. Cameron {
1038e85c5974SStephen M. Cameron 	return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
1039e85c5974SStephen M. Cameron }
1040e85c5974SStephen M. Cameron 
1041e85c5974SStephen M. Cameron /*
1042e85c5974SStephen M. Cameron  * During firmware flash, the heartbeat register may not update as frequently
1043e85c5974SStephen M. Cameron  * as it should.  So we dial down lockup detection during firmware flash. and
1044e85c5974SStephen M. Cameron  * dial it back up when firmware flash completes.
1045e85c5974SStephen M. Cameron  */
1046e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
1047e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
1048e85c5974SStephen M. Cameron static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
1049e85c5974SStephen M. Cameron 		struct CommandList *c)
1050e85c5974SStephen M. Cameron {
1051e85c5974SStephen M. Cameron 	if (!is_firmware_flash_cmd(c->Request.CDB))
1052e85c5974SStephen M. Cameron 		return;
1053e85c5974SStephen M. Cameron 	atomic_inc(&h->firmware_flash_in_progress);
1054e85c5974SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
1055e85c5974SStephen M. Cameron }
1056e85c5974SStephen M. Cameron 
1057e85c5974SStephen M. Cameron static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
1058e85c5974SStephen M. Cameron 		struct CommandList *c)
1059e85c5974SStephen M. Cameron {
1060e85c5974SStephen M. Cameron 	if (is_firmware_flash_cmd(c->Request.CDB) &&
1061e85c5974SStephen M. Cameron 		atomic_dec_and_test(&h->firmware_flash_in_progress))
1062e85c5974SStephen M. Cameron 		h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
1063e85c5974SStephen M. Cameron }
1064e85c5974SStephen M. Cameron 
106525163bd5SWebb Scales static void __enqueue_cmd_and_start_io(struct ctlr_info *h,
106625163bd5SWebb Scales 	struct CommandList *c, int reply_queue)
10673f5eac3aSStephen M. Cameron {
1068c05e8866SStephen Cameron 	dial_down_lockup_detection_during_fw_flash(h, c);
1069c05e8866SStephen Cameron 	atomic_inc(&h->commands_outstanding);
1070c349775eSScott Teel 	switch (c->cmd_type) {
1071c349775eSScott Teel 	case CMD_IOACCEL1:
107225163bd5SWebb Scales 		set_ioaccel1_performant_mode(h, c, reply_queue);
1073c05e8866SStephen Cameron 		writel(c->busaddr, h->vaddr + SA5_REQUEST_PORT_OFFSET);
1074c349775eSScott Teel 		break;
1075c349775eSScott Teel 	case CMD_IOACCEL2:
107625163bd5SWebb Scales 		set_ioaccel2_performant_mode(h, c, reply_queue);
1077c05e8866SStephen Cameron 		writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
1078c349775eSScott Teel 		break;
10798be986ccSStephen Cameron 	case IOACCEL2_TMF:
10808be986ccSStephen Cameron 		set_ioaccel2_tmf_performant_mode(h, c, reply_queue);
10818be986ccSStephen Cameron 		writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
10828be986ccSStephen Cameron 		break;
1083c349775eSScott Teel 	default:
108425163bd5SWebb Scales 		set_performant_mode(h, c, reply_queue);
1085f2405db8SDon Brace 		h->access.submit_command(h, c);
10863f5eac3aSStephen M. Cameron 	}
1087c05e8866SStephen Cameron }
10883f5eac3aSStephen M. Cameron 
1089a58e7e53SWebb Scales static void enqueue_cmd_and_start_io(struct ctlr_info *h, struct CommandList *c)
109025163bd5SWebb Scales {
1091d604f533SWebb Scales 	if (unlikely(hpsa_is_pending_event(c)))
1092a58e7e53SWebb Scales 		return finish_cmd(c);
1093a58e7e53SWebb Scales 
109425163bd5SWebb Scales 	__enqueue_cmd_and_start_io(h, c, DEFAULT_REPLY_QUEUE);
109525163bd5SWebb Scales }
109625163bd5SWebb Scales 
10973f5eac3aSStephen M. Cameron static inline int is_hba_lunid(unsigned char scsi3addr[])
10983f5eac3aSStephen M. Cameron {
10993f5eac3aSStephen M. Cameron 	return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
11003f5eac3aSStephen M. Cameron }
11013f5eac3aSStephen M. Cameron 
11023f5eac3aSStephen M. Cameron static inline int is_scsi_rev_5(struct ctlr_info *h)
11033f5eac3aSStephen M. Cameron {
11043f5eac3aSStephen M. Cameron 	if (!h->hba_inquiry_data)
11053f5eac3aSStephen M. Cameron 		return 0;
11063f5eac3aSStephen M. Cameron 	if ((h->hba_inquiry_data[2] & 0x07) == 5)
11073f5eac3aSStephen M. Cameron 		return 1;
11083f5eac3aSStephen M. Cameron 	return 0;
11093f5eac3aSStephen M. Cameron }
11103f5eac3aSStephen M. Cameron 
1111edd16368SStephen M. Cameron static int hpsa_find_target_lun(struct ctlr_info *h,
1112edd16368SStephen M. Cameron 	unsigned char scsi3addr[], int bus, int *target, int *lun)
1113edd16368SStephen M. Cameron {
1114edd16368SStephen M. Cameron 	/* finds an unused bus, target, lun for a new physical device
1115edd16368SStephen M. Cameron 	 * assumes h->devlock is held
1116edd16368SStephen M. Cameron 	 */
1117edd16368SStephen M. Cameron 	int i, found = 0;
1118cfe5badcSScott Teel 	DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
1119edd16368SStephen M. Cameron 
1120263d9401SAkinobu Mita 	bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
1121edd16368SStephen M. Cameron 
1122edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1123edd16368SStephen M. Cameron 		if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
1124263d9401SAkinobu Mita 			__set_bit(h->dev[i]->target, lun_taken);
1125edd16368SStephen M. Cameron 	}
1126edd16368SStephen M. Cameron 
1127263d9401SAkinobu Mita 	i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
1128263d9401SAkinobu Mita 	if (i < HPSA_MAX_DEVICES) {
1129edd16368SStephen M. Cameron 		/* *bus = 1; */
1130edd16368SStephen M. Cameron 		*target = i;
1131edd16368SStephen M. Cameron 		*lun = 0;
1132edd16368SStephen M. Cameron 		found = 1;
1133edd16368SStephen M. Cameron 	}
1134edd16368SStephen M. Cameron 	return !found;
1135edd16368SStephen M. Cameron }
1136edd16368SStephen M. Cameron 
11370d96ef5fSWebb Scales static inline void hpsa_show_dev_msg(const char *level, struct ctlr_info *h,
11380d96ef5fSWebb Scales 	struct hpsa_scsi_dev_t *dev, char *description)
11390d96ef5fSWebb Scales {
11400d96ef5fSWebb Scales 	dev_printk(level, &h->pdev->dev,
11410d96ef5fSWebb Scales 			"scsi %d:%d:%d:%d: %s %s %.8s %.16s RAID-%s SSDSmartPathCap%c En%c Exp=%d\n",
11420d96ef5fSWebb Scales 			h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
11430d96ef5fSWebb Scales 			description,
11440d96ef5fSWebb Scales 			scsi_device_type(dev->devtype),
11450d96ef5fSWebb Scales 			dev->vendor,
11460d96ef5fSWebb Scales 			dev->model,
11470d96ef5fSWebb Scales 			dev->raid_level > RAID_UNKNOWN ?
11480d96ef5fSWebb Scales 				"RAID-?" : raid_label[dev->raid_level],
11490d96ef5fSWebb Scales 			dev->offload_config ? '+' : '-',
11500d96ef5fSWebb Scales 			dev->offload_enabled ? '+' : '-',
11510d96ef5fSWebb Scales 			dev->expose_state);
11520d96ef5fSWebb Scales }
11530d96ef5fSWebb Scales 
1154edd16368SStephen M. Cameron /* Add an entry into h->dev[] array. */
1155edd16368SStephen M. Cameron static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
1156edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *device,
1157edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *added[], int *nadded)
1158edd16368SStephen M. Cameron {
1159edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
1160edd16368SStephen M. Cameron 	int n = h->ndevices;
1161edd16368SStephen M. Cameron 	int i;
1162edd16368SStephen M. Cameron 	unsigned char addr1[8], addr2[8];
1163edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1164edd16368SStephen M. Cameron 
1165cfe5badcSScott Teel 	if (n >= HPSA_MAX_DEVICES) {
1166edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "too many devices, some will be "
1167edd16368SStephen M. Cameron 			"inaccessible.\n");
1168edd16368SStephen M. Cameron 		return -1;
1169edd16368SStephen M. Cameron 	}
1170edd16368SStephen M. Cameron 
1171edd16368SStephen M. Cameron 	/* physical devices do not have lun or target assigned until now. */
1172edd16368SStephen M. Cameron 	if (device->lun != -1)
1173edd16368SStephen M. Cameron 		/* Logical device, lun is already assigned. */
1174edd16368SStephen M. Cameron 		goto lun_assigned;
1175edd16368SStephen M. Cameron 
1176edd16368SStephen M. Cameron 	/* If this device a non-zero lun of a multi-lun device
1177edd16368SStephen M. Cameron 	 * byte 4 of the 8-byte LUN addr will contain the logical
11782b08b3e9SDon Brace 	 * unit no, zero otherwise.
1179edd16368SStephen M. Cameron 	 */
1180edd16368SStephen M. Cameron 	if (device->scsi3addr[4] == 0) {
1181edd16368SStephen M. Cameron 		/* This is not a non-zero lun of a multi-lun device */
1182edd16368SStephen M. Cameron 		if (hpsa_find_target_lun(h, device->scsi3addr,
1183edd16368SStephen M. Cameron 			device->bus, &device->target, &device->lun) != 0)
1184edd16368SStephen M. Cameron 			return -1;
1185edd16368SStephen M. Cameron 		goto lun_assigned;
1186edd16368SStephen M. Cameron 	}
1187edd16368SStephen M. Cameron 
1188edd16368SStephen M. Cameron 	/* This is a non-zero lun of a multi-lun device.
1189edd16368SStephen M. Cameron 	 * Search through our list and find the device which
1190edd16368SStephen M. Cameron 	 * has the same 8 byte LUN address, excepting byte 4.
1191edd16368SStephen M. Cameron 	 * Assign the same bus and target for this new LUN.
1192edd16368SStephen M. Cameron 	 * Use the logical unit number from the firmware.
1193edd16368SStephen M. Cameron 	 */
1194edd16368SStephen M. Cameron 	memcpy(addr1, device->scsi3addr, 8);
1195edd16368SStephen M. Cameron 	addr1[4] = 0;
1196edd16368SStephen M. Cameron 	for (i = 0; i < n; i++) {
1197edd16368SStephen M. Cameron 		sd = h->dev[i];
1198edd16368SStephen M. Cameron 		memcpy(addr2, sd->scsi3addr, 8);
1199edd16368SStephen M. Cameron 		addr2[4] = 0;
1200edd16368SStephen M. Cameron 		/* differ only in byte 4? */
1201edd16368SStephen M. Cameron 		if (memcmp(addr1, addr2, 8) == 0) {
1202edd16368SStephen M. Cameron 			device->bus = sd->bus;
1203edd16368SStephen M. Cameron 			device->target = sd->target;
1204edd16368SStephen M. Cameron 			device->lun = device->scsi3addr[4];
1205edd16368SStephen M. Cameron 			break;
1206edd16368SStephen M. Cameron 		}
1207edd16368SStephen M. Cameron 	}
1208edd16368SStephen M. Cameron 	if (device->lun == -1) {
1209edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
1210edd16368SStephen M. Cameron 			" suspect firmware bug or unsupported hardware "
1211edd16368SStephen M. Cameron 			"configuration.\n");
1212edd16368SStephen M. Cameron 			return -1;
1213edd16368SStephen M. Cameron 	}
1214edd16368SStephen M. Cameron 
1215edd16368SStephen M. Cameron lun_assigned:
1216edd16368SStephen M. Cameron 
1217edd16368SStephen M. Cameron 	h->dev[n] = device;
1218edd16368SStephen M. Cameron 	h->ndevices++;
1219edd16368SStephen M. Cameron 	added[*nadded] = device;
1220edd16368SStephen M. Cameron 	(*nadded)++;
12210d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_INFO, h, device,
12220d96ef5fSWebb Scales 		device->expose_state & HPSA_SCSI_ADD ? "added" : "masked");
1223a473d86cSRobert Elliott 	device->offload_to_be_enabled = device->offload_enabled;
1224a473d86cSRobert Elliott 	device->offload_enabled = 0;
1225edd16368SStephen M. Cameron 	return 0;
1226edd16368SStephen M. Cameron }
1227edd16368SStephen M. Cameron 
1228bd9244f7SScott Teel /* Update an entry in h->dev[] array. */
1229bd9244f7SScott Teel static void hpsa_scsi_update_entry(struct ctlr_info *h, int hostno,
1230bd9244f7SScott Teel 	int entry, struct hpsa_scsi_dev_t *new_entry)
1231bd9244f7SScott Teel {
1232a473d86cSRobert Elliott 	int offload_enabled;
1233bd9244f7SScott Teel 	/* assumes h->devlock is held */
1234bd9244f7SScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
1235bd9244f7SScott Teel 
1236bd9244f7SScott Teel 	/* Raid level changed. */
1237bd9244f7SScott Teel 	h->dev[entry]->raid_level = new_entry->raid_level;
1238250fb125SStephen M. Cameron 
123903383736SDon Brace 	/* Raid offload parameters changed.  Careful about the ordering. */
124003383736SDon Brace 	if (new_entry->offload_config && new_entry->offload_enabled) {
124103383736SDon Brace 		/*
124203383736SDon Brace 		 * if drive is newly offload_enabled, we want to copy the
124303383736SDon Brace 		 * raid map data first.  If previously offload_enabled and
124403383736SDon Brace 		 * offload_config were set, raid map data had better be
124503383736SDon Brace 		 * the same as it was before.  if raid map data is changed
124603383736SDon Brace 		 * then it had better be the case that
124703383736SDon Brace 		 * h->dev[entry]->offload_enabled is currently 0.
124803383736SDon Brace 		 */
12499fb0de2dSStephen M. Cameron 		h->dev[entry]->raid_map = new_entry->raid_map;
125003383736SDon Brace 		h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
125103383736SDon Brace 	}
1252a3144e0bSJoe Handzik 	if (new_entry->hba_ioaccel_enabled) {
1253a3144e0bSJoe Handzik 		h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
1254a3144e0bSJoe Handzik 		wmb(); /* set ioaccel_handle *before* hba_ioaccel_enabled */
1255a3144e0bSJoe Handzik 	}
1256a3144e0bSJoe Handzik 	h->dev[entry]->hba_ioaccel_enabled = new_entry->hba_ioaccel_enabled;
125703383736SDon Brace 	h->dev[entry]->offload_config = new_entry->offload_config;
125803383736SDon Brace 	h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
125903383736SDon Brace 	h->dev[entry]->queue_depth = new_entry->queue_depth;
1260250fb125SStephen M. Cameron 
126141ce4c35SStephen Cameron 	/*
126241ce4c35SStephen Cameron 	 * We can turn off ioaccel offload now, but need to delay turning
126341ce4c35SStephen Cameron 	 * it on until we can update h->dev[entry]->phys_disk[], but we
126441ce4c35SStephen Cameron 	 * can't do that until all the devices are updated.
126541ce4c35SStephen Cameron 	 */
126641ce4c35SStephen Cameron 	h->dev[entry]->offload_to_be_enabled = new_entry->offload_enabled;
126741ce4c35SStephen Cameron 	if (!new_entry->offload_enabled)
126841ce4c35SStephen Cameron 		h->dev[entry]->offload_enabled = 0;
126941ce4c35SStephen Cameron 
1270a473d86cSRobert Elliott 	offload_enabled = h->dev[entry]->offload_enabled;
1271a473d86cSRobert Elliott 	h->dev[entry]->offload_enabled = h->dev[entry]->offload_to_be_enabled;
12720d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_INFO, h, h->dev[entry], "updated");
1273a473d86cSRobert Elliott 	h->dev[entry]->offload_enabled = offload_enabled;
1274bd9244f7SScott Teel }
1275bd9244f7SScott Teel 
12762a8ccf31SStephen M. Cameron /* Replace an entry from h->dev[] array. */
12772a8ccf31SStephen M. Cameron static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
12782a8ccf31SStephen M. Cameron 	int entry, struct hpsa_scsi_dev_t *new_entry,
12792a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *added[], int *nadded,
12802a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
12812a8ccf31SStephen M. Cameron {
12822a8ccf31SStephen M. Cameron 	/* assumes h->devlock is held */
1283cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
12842a8ccf31SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
12852a8ccf31SStephen M. Cameron 	(*nremoved)++;
128601350d05SStephen M. Cameron 
128701350d05SStephen M. Cameron 	/*
128801350d05SStephen M. Cameron 	 * New physical devices won't have target/lun assigned yet
128901350d05SStephen M. Cameron 	 * so we need to preserve the values in the slot we are replacing.
129001350d05SStephen M. Cameron 	 */
129101350d05SStephen M. Cameron 	if (new_entry->target == -1) {
129201350d05SStephen M. Cameron 		new_entry->target = h->dev[entry]->target;
129301350d05SStephen M. Cameron 		new_entry->lun = h->dev[entry]->lun;
129401350d05SStephen M. Cameron 	}
129501350d05SStephen M. Cameron 
12962a8ccf31SStephen M. Cameron 	h->dev[entry] = new_entry;
12972a8ccf31SStephen M. Cameron 	added[*nadded] = new_entry;
12982a8ccf31SStephen M. Cameron 	(*nadded)++;
12990d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_INFO, h, new_entry, "replaced");
1300a473d86cSRobert Elliott 	new_entry->offload_to_be_enabled = new_entry->offload_enabled;
1301a473d86cSRobert Elliott 	new_entry->offload_enabled = 0;
13022a8ccf31SStephen M. Cameron }
13032a8ccf31SStephen M. Cameron 
1304edd16368SStephen M. Cameron /* Remove an entry from h->dev[] array. */
1305edd16368SStephen M. Cameron static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
1306edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
1307edd16368SStephen M. Cameron {
1308edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
1309edd16368SStephen M. Cameron 	int i;
1310edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1311edd16368SStephen M. Cameron 
1312cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
1313edd16368SStephen M. Cameron 
1314edd16368SStephen M. Cameron 	sd = h->dev[entry];
1315edd16368SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
1316edd16368SStephen M. Cameron 	(*nremoved)++;
1317edd16368SStephen M. Cameron 
1318edd16368SStephen M. Cameron 	for (i = entry; i < h->ndevices-1; i++)
1319edd16368SStephen M. Cameron 		h->dev[i] = h->dev[i+1];
1320edd16368SStephen M. Cameron 	h->ndevices--;
13210d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_INFO, h, sd, "removed");
1322edd16368SStephen M. Cameron }
1323edd16368SStephen M. Cameron 
1324edd16368SStephen M. Cameron #define SCSI3ADDR_EQ(a, b) ( \
1325edd16368SStephen M. Cameron 	(a)[7] == (b)[7] && \
1326edd16368SStephen M. Cameron 	(a)[6] == (b)[6] && \
1327edd16368SStephen M. Cameron 	(a)[5] == (b)[5] && \
1328edd16368SStephen M. Cameron 	(a)[4] == (b)[4] && \
1329edd16368SStephen M. Cameron 	(a)[3] == (b)[3] && \
1330edd16368SStephen M. Cameron 	(a)[2] == (b)[2] && \
1331edd16368SStephen M. Cameron 	(a)[1] == (b)[1] && \
1332edd16368SStephen M. Cameron 	(a)[0] == (b)[0])
1333edd16368SStephen M. Cameron 
1334edd16368SStephen M. Cameron static void fixup_botched_add(struct ctlr_info *h,
1335edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *added)
1336edd16368SStephen M. Cameron {
1337edd16368SStephen M. Cameron 	/* called when scsi_add_device fails in order to re-adjust
1338edd16368SStephen M. Cameron 	 * h->dev[] to match the mid layer's view.
1339edd16368SStephen M. Cameron 	 */
1340edd16368SStephen M. Cameron 	unsigned long flags;
1341edd16368SStephen M. Cameron 	int i, j;
1342edd16368SStephen M. Cameron 
1343edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
1344edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1345edd16368SStephen M. Cameron 		if (h->dev[i] == added) {
1346edd16368SStephen M. Cameron 			for (j = i; j < h->ndevices-1; j++)
1347edd16368SStephen M. Cameron 				h->dev[j] = h->dev[j+1];
1348edd16368SStephen M. Cameron 			h->ndevices--;
1349edd16368SStephen M. Cameron 			break;
1350edd16368SStephen M. Cameron 		}
1351edd16368SStephen M. Cameron 	}
1352edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
1353edd16368SStephen M. Cameron 	kfree(added);
1354edd16368SStephen M. Cameron }
1355edd16368SStephen M. Cameron 
1356edd16368SStephen M. Cameron static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
1357edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev2)
1358edd16368SStephen M. Cameron {
1359edd16368SStephen M. Cameron 	/* we compare everything except lun and target as these
1360edd16368SStephen M. Cameron 	 * are not yet assigned.  Compare parts likely
1361edd16368SStephen M. Cameron 	 * to differ first
1362edd16368SStephen M. Cameron 	 */
1363edd16368SStephen M. Cameron 	if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
1364edd16368SStephen M. Cameron 		sizeof(dev1->scsi3addr)) != 0)
1365edd16368SStephen M. Cameron 		return 0;
1366edd16368SStephen M. Cameron 	if (memcmp(dev1->device_id, dev2->device_id,
1367edd16368SStephen M. Cameron 		sizeof(dev1->device_id)) != 0)
1368edd16368SStephen M. Cameron 		return 0;
1369edd16368SStephen M. Cameron 	if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
1370edd16368SStephen M. Cameron 		return 0;
1371edd16368SStephen M. Cameron 	if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
1372edd16368SStephen M. Cameron 		return 0;
1373edd16368SStephen M. Cameron 	if (dev1->devtype != dev2->devtype)
1374edd16368SStephen M. Cameron 		return 0;
1375edd16368SStephen M. Cameron 	if (dev1->bus != dev2->bus)
1376edd16368SStephen M. Cameron 		return 0;
1377edd16368SStephen M. Cameron 	return 1;
1378edd16368SStephen M. Cameron }
1379edd16368SStephen M. Cameron 
1380bd9244f7SScott Teel static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
1381bd9244f7SScott Teel 	struct hpsa_scsi_dev_t *dev2)
1382bd9244f7SScott Teel {
1383bd9244f7SScott Teel 	/* Device attributes that can change, but don't mean
1384bd9244f7SScott Teel 	 * that the device is a different device, nor that the OS
1385bd9244f7SScott Teel 	 * needs to be told anything about the change.
1386bd9244f7SScott Teel 	 */
1387bd9244f7SScott Teel 	if (dev1->raid_level != dev2->raid_level)
1388bd9244f7SScott Teel 		return 1;
1389250fb125SStephen M. Cameron 	if (dev1->offload_config != dev2->offload_config)
1390250fb125SStephen M. Cameron 		return 1;
1391250fb125SStephen M. Cameron 	if (dev1->offload_enabled != dev2->offload_enabled)
1392250fb125SStephen M. Cameron 		return 1;
139393849508SDon Brace 	if (!is_logical_dev_addr_mode(dev1->scsi3addr))
139403383736SDon Brace 		if (dev1->queue_depth != dev2->queue_depth)
139503383736SDon Brace 			return 1;
1396bd9244f7SScott Teel 	return 0;
1397bd9244f7SScott Teel }
1398bd9244f7SScott Teel 
1399edd16368SStephen M. Cameron /* Find needle in haystack.  If exact match found, return DEVICE_SAME,
1400edd16368SStephen M. Cameron  * and return needle location in *index.  If scsi3addr matches, but not
1401edd16368SStephen M. Cameron  * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
1402bd9244f7SScott Teel  * location in *index.
1403bd9244f7SScott Teel  * In the case of a minor device attribute change, such as RAID level, just
1404bd9244f7SScott Teel  * return DEVICE_UPDATED, along with the updated device's location in index.
1405bd9244f7SScott Teel  * If needle not found, return DEVICE_NOT_FOUND.
1406edd16368SStephen M. Cameron  */
1407edd16368SStephen M. Cameron static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
1408edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *haystack[], int haystack_size,
1409edd16368SStephen M. Cameron 	int *index)
1410edd16368SStephen M. Cameron {
1411edd16368SStephen M. Cameron 	int i;
1412edd16368SStephen M. Cameron #define DEVICE_NOT_FOUND 0
1413edd16368SStephen M. Cameron #define DEVICE_CHANGED 1
1414edd16368SStephen M. Cameron #define DEVICE_SAME 2
1415bd9244f7SScott Teel #define DEVICE_UPDATED 3
1416edd16368SStephen M. Cameron 	for (i = 0; i < haystack_size; i++) {
141723231048SStephen M. Cameron 		if (haystack[i] == NULL) /* previously removed. */
141823231048SStephen M. Cameron 			continue;
1419edd16368SStephen M. Cameron 		if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
1420edd16368SStephen M. Cameron 			*index = i;
1421bd9244f7SScott Teel 			if (device_is_the_same(needle, haystack[i])) {
1422bd9244f7SScott Teel 				if (device_updated(needle, haystack[i]))
1423bd9244f7SScott Teel 					return DEVICE_UPDATED;
1424edd16368SStephen M. Cameron 				return DEVICE_SAME;
1425bd9244f7SScott Teel 			} else {
14269846590eSStephen M. Cameron 				/* Keep offline devices offline */
14279846590eSStephen M. Cameron 				if (needle->volume_offline)
14289846590eSStephen M. Cameron 					return DEVICE_NOT_FOUND;
1429edd16368SStephen M. Cameron 				return DEVICE_CHANGED;
1430edd16368SStephen M. Cameron 			}
1431edd16368SStephen M. Cameron 		}
1432bd9244f7SScott Teel 	}
1433edd16368SStephen M. Cameron 	*index = -1;
1434edd16368SStephen M. Cameron 	return DEVICE_NOT_FOUND;
1435edd16368SStephen M. Cameron }
1436edd16368SStephen M. Cameron 
14379846590eSStephen M. Cameron static void hpsa_monitor_offline_device(struct ctlr_info *h,
14389846590eSStephen M. Cameron 					unsigned char scsi3addr[])
14399846590eSStephen M. Cameron {
14409846590eSStephen M. Cameron 	struct offline_device_entry *device;
14419846590eSStephen M. Cameron 	unsigned long flags;
14429846590eSStephen M. Cameron 
14439846590eSStephen M. Cameron 	/* Check to see if device is already on the list */
14449846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
14459846590eSStephen M. Cameron 	list_for_each_entry(device, &h->offline_device_list, offline_list) {
14469846590eSStephen M. Cameron 		if (memcmp(device->scsi3addr, scsi3addr,
14479846590eSStephen M. Cameron 			sizeof(device->scsi3addr)) == 0) {
14489846590eSStephen M. Cameron 			spin_unlock_irqrestore(&h->offline_device_lock, flags);
14499846590eSStephen M. Cameron 			return;
14509846590eSStephen M. Cameron 		}
14519846590eSStephen M. Cameron 	}
14529846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
14539846590eSStephen M. Cameron 
14549846590eSStephen M. Cameron 	/* Device is not on the list, add it. */
14559846590eSStephen M. Cameron 	device = kmalloc(sizeof(*device), GFP_KERNEL);
14569846590eSStephen M. Cameron 	if (!device) {
14579846590eSStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in %s\n", __func__);
14589846590eSStephen M. Cameron 		return;
14599846590eSStephen M. Cameron 	}
14609846590eSStephen M. Cameron 	memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
14619846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
14629846590eSStephen M. Cameron 	list_add_tail(&device->offline_list, &h->offline_device_list);
14639846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
14649846590eSStephen M. Cameron }
14659846590eSStephen M. Cameron 
14669846590eSStephen M. Cameron /* Print a message explaining various offline volume states */
14679846590eSStephen M. Cameron static void hpsa_show_volume_status(struct ctlr_info *h,
14689846590eSStephen M. Cameron 	struct hpsa_scsi_dev_t *sd)
14699846590eSStephen M. Cameron {
14709846590eSStephen M. Cameron 	if (sd->volume_offline == HPSA_VPD_LV_STATUS_UNSUPPORTED)
14719846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
14729846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume status is not available through vital product data pages.\n",
14739846590eSStephen M. Cameron 			h->scsi_host->host_no,
14749846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
14759846590eSStephen M. Cameron 	switch (sd->volume_offline) {
14769846590eSStephen M. Cameron 	case HPSA_LV_OK:
14779846590eSStephen M. Cameron 		break;
14789846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ERASE:
14799846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
14809846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing background erase process.\n",
14819846590eSStephen M. Cameron 			h->scsi_host->host_no,
14829846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
14839846590eSStephen M. Cameron 		break;
1484*5ca01204SScott Benesh 	case HPSA_LV_NOT_AVAILABLE:
1485*5ca01204SScott Benesh 		dev_info(&h->pdev->dev,
1486*5ca01204SScott Benesh 			"C%d:B%d:T%d:L%d Volume is waiting for transforming volume.\n",
1487*5ca01204SScott Benesh 			h->scsi_host->host_no,
1488*5ca01204SScott Benesh 			sd->bus, sd->target, sd->lun);
1489*5ca01204SScott Benesh 		break;
14909846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_RPI:
14919846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
1492*5ca01204SScott Benesh 			"C%d:B%d:T%d:L%d Volume is undergoing rapid parity init.\n",
14939846590eSStephen M. Cameron 			h->scsi_host->host_no,
14949846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
14959846590eSStephen M. Cameron 		break;
14969846590eSStephen M. Cameron 	case HPSA_LV_PENDING_RPI:
14979846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
14989846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is queued for rapid parity initialization process.\n",
14999846590eSStephen M. Cameron 			h->scsi_host->host_no,
15009846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
15019846590eSStephen M. Cameron 		break;
15029846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_NO_KEY:
15039846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
15049846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because key is not present.\n",
15059846590eSStephen M. Cameron 			h->scsi_host->host_no,
15069846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
15079846590eSStephen M. Cameron 		break;
15089846590eSStephen M. Cameron 	case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
15099846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
15109846590eSStephen 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",
15119846590eSStephen M. Cameron 			h->scsi_host->host_no,
15129846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
15139846590eSStephen M. Cameron 		break;
15149846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION:
15159846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
15169846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing encryption process.\n",
15179846590eSStephen M. Cameron 			h->scsi_host->host_no,
15189846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
15199846590eSStephen M. Cameron 		break;
15209846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
15219846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
15229846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is undergoing encryption re-keying process.\n",
15239846590eSStephen M. Cameron 			h->scsi_host->host_no,
15249846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
15259846590eSStephen M. Cameron 		break;
15269846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
15279846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
15289846590eSStephen 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",
15299846590eSStephen M. Cameron 			h->scsi_host->host_no,
15309846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
15319846590eSStephen M. Cameron 		break;
15329846590eSStephen M. Cameron 	case HPSA_LV_PENDING_ENCRYPTION:
15339846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
15349846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is pending migration to encrypted state, but process has not started.\n",
15359846590eSStephen M. Cameron 			h->scsi_host->host_no,
15369846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
15379846590eSStephen M. Cameron 		break;
15389846590eSStephen M. Cameron 	case HPSA_LV_PENDING_ENCRYPTION_REKEYING:
15399846590eSStephen M. Cameron 		dev_info(&h->pdev->dev,
15409846590eSStephen M. Cameron 			"C%d:B%d:T%d:L%d Volume is encrypted and is pending encryption rekeying.\n",
15419846590eSStephen M. Cameron 			h->scsi_host->host_no,
15429846590eSStephen M. Cameron 			sd->bus, sd->target, sd->lun);
15439846590eSStephen M. Cameron 		break;
15449846590eSStephen M. Cameron 	}
15459846590eSStephen M. Cameron }
15469846590eSStephen M. Cameron 
154703383736SDon Brace /*
154803383736SDon Brace  * Figure the list of physical drive pointers for a logical drive with
154903383736SDon Brace  * raid offload configured.
155003383736SDon Brace  */
155103383736SDon Brace static void hpsa_figure_phys_disk_ptrs(struct ctlr_info *h,
155203383736SDon Brace 				struct hpsa_scsi_dev_t *dev[], int ndevices,
155303383736SDon Brace 				struct hpsa_scsi_dev_t *logical_drive)
155403383736SDon Brace {
155503383736SDon Brace 	struct raid_map_data *map = &logical_drive->raid_map;
155603383736SDon Brace 	struct raid_map_disk_data *dd = &map->data[0];
155703383736SDon Brace 	int i, j;
155803383736SDon Brace 	int total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
155903383736SDon Brace 				le16_to_cpu(map->metadata_disks_per_row);
156003383736SDon Brace 	int nraid_map_entries = le16_to_cpu(map->row_cnt) *
156103383736SDon Brace 				le16_to_cpu(map->layout_map_count) *
156203383736SDon Brace 				total_disks_per_row;
156303383736SDon Brace 	int nphys_disk = le16_to_cpu(map->layout_map_count) *
156403383736SDon Brace 				total_disks_per_row;
156503383736SDon Brace 	int qdepth;
156603383736SDon Brace 
156703383736SDon Brace 	if (nraid_map_entries > RAID_MAP_MAX_ENTRIES)
156803383736SDon Brace 		nraid_map_entries = RAID_MAP_MAX_ENTRIES;
156903383736SDon Brace 
1570d604f533SWebb Scales 	logical_drive->nphysical_disks = nraid_map_entries;
1571d604f533SWebb Scales 
157203383736SDon Brace 	qdepth = 0;
157303383736SDon Brace 	for (i = 0; i < nraid_map_entries; i++) {
157403383736SDon Brace 		logical_drive->phys_disk[i] = NULL;
157503383736SDon Brace 		if (!logical_drive->offload_config)
157603383736SDon Brace 			continue;
157703383736SDon Brace 		for (j = 0; j < ndevices; j++) {
157803383736SDon Brace 			if (dev[j]->devtype != TYPE_DISK)
157903383736SDon Brace 				continue;
158003383736SDon Brace 			if (is_logical_dev_addr_mode(dev[j]->scsi3addr))
158103383736SDon Brace 				continue;
158203383736SDon Brace 			if (dev[j]->ioaccel_handle != dd[i].ioaccel_handle)
158303383736SDon Brace 				continue;
158403383736SDon Brace 
158503383736SDon Brace 			logical_drive->phys_disk[i] = dev[j];
158603383736SDon Brace 			if (i < nphys_disk)
158703383736SDon Brace 				qdepth = min(h->nr_cmds, qdepth +
158803383736SDon Brace 				    logical_drive->phys_disk[i]->queue_depth);
158903383736SDon Brace 			break;
159003383736SDon Brace 		}
159103383736SDon Brace 
159203383736SDon Brace 		/*
159303383736SDon Brace 		 * This can happen if a physical drive is removed and
159403383736SDon Brace 		 * the logical drive is degraded.  In that case, the RAID
159503383736SDon Brace 		 * map data will refer to a physical disk which isn't actually
159603383736SDon Brace 		 * present.  And in that case offload_enabled should already
159703383736SDon Brace 		 * be 0, but we'll turn it off here just in case
159803383736SDon Brace 		 */
159903383736SDon Brace 		if (!logical_drive->phys_disk[i]) {
160003383736SDon Brace 			logical_drive->offload_enabled = 0;
160141ce4c35SStephen Cameron 			logical_drive->offload_to_be_enabled = 0;
160241ce4c35SStephen Cameron 			logical_drive->queue_depth = 8;
160303383736SDon Brace 		}
160403383736SDon Brace 	}
160503383736SDon Brace 	if (nraid_map_entries)
160603383736SDon Brace 		/*
160703383736SDon Brace 		 * This is correct for reads, too high for full stripe writes,
160803383736SDon Brace 		 * way too high for partial stripe writes
160903383736SDon Brace 		 */
161003383736SDon Brace 		logical_drive->queue_depth = qdepth;
161103383736SDon Brace 	else
161203383736SDon Brace 		logical_drive->queue_depth = h->nr_cmds;
161303383736SDon Brace }
161403383736SDon Brace 
161503383736SDon Brace static void hpsa_update_log_drive_phys_drive_ptrs(struct ctlr_info *h,
161603383736SDon Brace 				struct hpsa_scsi_dev_t *dev[], int ndevices)
161703383736SDon Brace {
161803383736SDon Brace 	int i;
161903383736SDon Brace 
162003383736SDon Brace 	for (i = 0; i < ndevices; i++) {
162103383736SDon Brace 		if (dev[i]->devtype != TYPE_DISK)
162203383736SDon Brace 			continue;
162303383736SDon Brace 		if (!is_logical_dev_addr_mode(dev[i]->scsi3addr))
162403383736SDon Brace 			continue;
162541ce4c35SStephen Cameron 
162641ce4c35SStephen Cameron 		/*
162741ce4c35SStephen Cameron 		 * If offload is currently enabled, the RAID map and
162841ce4c35SStephen Cameron 		 * phys_disk[] assignment *better* not be changing
162941ce4c35SStephen Cameron 		 * and since it isn't changing, we do not need to
163041ce4c35SStephen Cameron 		 * update it.
163141ce4c35SStephen Cameron 		 */
163241ce4c35SStephen Cameron 		if (dev[i]->offload_enabled)
163341ce4c35SStephen Cameron 			continue;
163441ce4c35SStephen Cameron 
163503383736SDon Brace 		hpsa_figure_phys_disk_ptrs(h, dev, ndevices, dev[i]);
163603383736SDon Brace 	}
163703383736SDon Brace }
163803383736SDon Brace 
16394967bd3eSStephen M. Cameron static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
1640edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd[], int nsds)
1641edd16368SStephen M. Cameron {
1642edd16368SStephen M. Cameron 	/* sd contains scsi3 addresses and devtypes, and inquiry
1643edd16368SStephen M. Cameron 	 * data.  This function takes what's in sd to be the current
1644edd16368SStephen M. Cameron 	 * reality and updates h->dev[] to reflect that reality.
1645edd16368SStephen M. Cameron 	 */
1646edd16368SStephen M. Cameron 	int i, entry, device_change, changes = 0;
1647edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *csd;
1648edd16368SStephen M. Cameron 	unsigned long flags;
1649edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **added, **removed;
1650edd16368SStephen M. Cameron 	int nadded, nremoved;
1651edd16368SStephen M. Cameron 	struct Scsi_Host *sh = NULL;
1652edd16368SStephen M. Cameron 
1653cfe5badcSScott Teel 	added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
1654cfe5badcSScott Teel 	removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
1655edd16368SStephen M. Cameron 
1656edd16368SStephen M. Cameron 	if (!added || !removed) {
1657edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in "
1658edd16368SStephen M. Cameron 			"adjust_hpsa_scsi_table\n");
1659edd16368SStephen M. Cameron 		goto free_and_out;
1660edd16368SStephen M. Cameron 	}
1661edd16368SStephen M. Cameron 
1662edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1663edd16368SStephen M. Cameron 
1664edd16368SStephen M. Cameron 	/* find any devices in h->dev[] that are not in
1665edd16368SStephen M. Cameron 	 * sd[] and remove them from h->dev[], and for any
1666edd16368SStephen M. Cameron 	 * devices which have changed, remove the old device
1667edd16368SStephen M. Cameron 	 * info and add the new device info.
1668bd9244f7SScott Teel 	 * If minor device attributes change, just update
1669bd9244f7SScott Teel 	 * the existing device structure.
1670edd16368SStephen M. Cameron 	 */
1671edd16368SStephen M. Cameron 	i = 0;
1672edd16368SStephen M. Cameron 	nremoved = 0;
1673edd16368SStephen M. Cameron 	nadded = 0;
1674edd16368SStephen M. Cameron 	while (i < h->ndevices) {
1675edd16368SStephen M. Cameron 		csd = h->dev[i];
1676edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
1677edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
1678edd16368SStephen M. Cameron 			changes++;
1679edd16368SStephen M. Cameron 			hpsa_scsi_remove_entry(h, hostno, i,
1680edd16368SStephen M. Cameron 				removed, &nremoved);
1681edd16368SStephen M. Cameron 			continue; /* remove ^^^, hence i not incremented */
1682edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
1683edd16368SStephen M. Cameron 			changes++;
16842a8ccf31SStephen M. Cameron 			hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
16852a8ccf31SStephen M. Cameron 				added, &nadded, removed, &nremoved);
1686c7f172dcSStephen M. Cameron 			/* Set it to NULL to prevent it from being freed
1687c7f172dcSStephen M. Cameron 			 * at the bottom of hpsa_update_scsi_devices()
1688c7f172dcSStephen M. Cameron 			 */
1689c7f172dcSStephen M. Cameron 			sd[entry] = NULL;
1690bd9244f7SScott Teel 		} else if (device_change == DEVICE_UPDATED) {
1691bd9244f7SScott Teel 			hpsa_scsi_update_entry(h, hostno, i, sd[entry]);
1692edd16368SStephen M. Cameron 		}
1693edd16368SStephen M. Cameron 		i++;
1694edd16368SStephen M. Cameron 	}
1695edd16368SStephen M. Cameron 
1696edd16368SStephen M. Cameron 	/* Now, make sure every device listed in sd[] is also
1697edd16368SStephen M. Cameron 	 * listed in h->dev[], adding them if they aren't found
1698edd16368SStephen M. Cameron 	 */
1699edd16368SStephen M. Cameron 
1700edd16368SStephen M. Cameron 	for (i = 0; i < nsds; i++) {
1701edd16368SStephen M. Cameron 		if (!sd[i]) /* if already added above. */
1702edd16368SStephen M. Cameron 			continue;
17039846590eSStephen M. Cameron 
17049846590eSStephen M. Cameron 		/* Don't add devices which are NOT READY, FORMAT IN PROGRESS
17059846590eSStephen M. Cameron 		 * as the SCSI mid-layer does not handle such devices well.
17069846590eSStephen M. Cameron 		 * It relentlessly loops sending TUR at 3Hz, then READ(10)
17079846590eSStephen M. Cameron 		 * at 160Hz, and prevents the system from coming up.
17089846590eSStephen M. Cameron 		 */
17099846590eSStephen M. Cameron 		if (sd[i]->volume_offline) {
17109846590eSStephen M. Cameron 			hpsa_show_volume_status(h, sd[i]);
17110d96ef5fSWebb Scales 			hpsa_show_dev_msg(KERN_INFO, h, sd[i], "offline");
17129846590eSStephen M. Cameron 			continue;
17139846590eSStephen M. Cameron 		}
17149846590eSStephen M. Cameron 
1715edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1716edd16368SStephen M. Cameron 					h->ndevices, &entry);
1717edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
1718edd16368SStephen M. Cameron 			changes++;
1719edd16368SStephen M. Cameron 			if (hpsa_scsi_add_entry(h, hostno, sd[i],
1720edd16368SStephen M. Cameron 				added, &nadded) != 0)
1721edd16368SStephen M. Cameron 				break;
1722edd16368SStephen M. Cameron 			sd[i] = NULL; /* prevent from being freed later. */
1723edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
1724edd16368SStephen M. Cameron 			/* should never happen... */
1725edd16368SStephen M. Cameron 			changes++;
1726edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev,
1727edd16368SStephen M. Cameron 				"device unexpectedly changed.\n");
1728edd16368SStephen M. Cameron 			/* but if it does happen, we just ignore that device */
1729edd16368SStephen M. Cameron 		}
1730edd16368SStephen M. Cameron 	}
173141ce4c35SStephen Cameron 	hpsa_update_log_drive_phys_drive_ptrs(h, h->dev, h->ndevices);
173241ce4c35SStephen Cameron 
173341ce4c35SStephen Cameron 	/* Now that h->dev[]->phys_disk[] is coherent, we can enable
173441ce4c35SStephen Cameron 	 * any logical drives that need it enabled.
173541ce4c35SStephen Cameron 	 */
173641ce4c35SStephen Cameron 	for (i = 0; i < h->ndevices; i++)
173741ce4c35SStephen Cameron 		h->dev[i]->offload_enabled = h->dev[i]->offload_to_be_enabled;
173841ce4c35SStephen Cameron 
1739edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1740edd16368SStephen M. Cameron 
17419846590eSStephen M. Cameron 	/* Monitor devices which are in one of several NOT READY states to be
17429846590eSStephen M. Cameron 	 * brought online later. This must be done without holding h->devlock,
17439846590eSStephen M. Cameron 	 * so don't touch h->dev[]
17449846590eSStephen M. Cameron 	 */
17459846590eSStephen M. Cameron 	for (i = 0; i < nsds; i++) {
17469846590eSStephen M. Cameron 		if (!sd[i]) /* if already added above. */
17479846590eSStephen M. Cameron 			continue;
17489846590eSStephen M. Cameron 		if (sd[i]->volume_offline)
17499846590eSStephen M. Cameron 			hpsa_monitor_offline_device(h, sd[i]->scsi3addr);
17509846590eSStephen M. Cameron 	}
17519846590eSStephen M. Cameron 
1752edd16368SStephen M. Cameron 	/* Don't notify scsi mid layer of any changes the first time through
1753edd16368SStephen M. Cameron 	 * (or if there are no changes) scsi_scan_host will do it later the
1754edd16368SStephen M. Cameron 	 * first time through.
1755edd16368SStephen M. Cameron 	 */
1756edd16368SStephen M. Cameron 	if (hostno == -1 || !changes)
1757edd16368SStephen M. Cameron 		goto free_and_out;
1758edd16368SStephen M. Cameron 
1759edd16368SStephen M. Cameron 	sh = h->scsi_host;
1760edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any removed devices */
1761edd16368SStephen M. Cameron 	for (i = 0; i < nremoved; i++) {
176241ce4c35SStephen Cameron 		if (removed[i]->expose_state & HPSA_SCSI_ADD) {
1763edd16368SStephen M. Cameron 			struct scsi_device *sdev =
1764edd16368SStephen M. Cameron 				scsi_device_lookup(sh, removed[i]->bus,
1765edd16368SStephen M. Cameron 					removed[i]->target, removed[i]->lun);
1766edd16368SStephen M. Cameron 			if (sdev != NULL) {
1767edd16368SStephen M. Cameron 				scsi_remove_device(sdev);
1768edd16368SStephen M. Cameron 				scsi_device_put(sdev);
1769edd16368SStephen M. Cameron 			} else {
177041ce4c35SStephen Cameron 				/*
177141ce4c35SStephen Cameron 				 * We don't expect to get here.
1772edd16368SStephen M. Cameron 				 * future cmds to this device will get selection
1773edd16368SStephen M. Cameron 				 * timeout as if the device was gone.
1774edd16368SStephen M. Cameron 				 */
17750d96ef5fSWebb Scales 				hpsa_show_dev_msg(KERN_WARNING, h, removed[i],
17760d96ef5fSWebb Scales 					"didn't find device for removal.");
1777edd16368SStephen M. Cameron 			}
177841ce4c35SStephen Cameron 		}
1779edd16368SStephen M. Cameron 		kfree(removed[i]);
1780edd16368SStephen M. Cameron 		removed[i] = NULL;
1781edd16368SStephen M. Cameron 	}
1782edd16368SStephen M. Cameron 
1783edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any added devices */
1784edd16368SStephen M. Cameron 	for (i = 0; i < nadded; i++) {
178541ce4c35SStephen Cameron 		if (!(added[i]->expose_state & HPSA_SCSI_ADD))
178641ce4c35SStephen Cameron 			continue;
1787edd16368SStephen M. Cameron 		if (scsi_add_device(sh, added[i]->bus,
1788edd16368SStephen M. Cameron 			added[i]->target, added[i]->lun) == 0)
1789edd16368SStephen M. Cameron 			continue;
17900d96ef5fSWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, added[i],
17910d96ef5fSWebb Scales 					"addition failed, device not added.");
1792edd16368SStephen M. Cameron 		/* now we have to remove it from h->dev,
1793edd16368SStephen M. Cameron 		 * since it didn't get added to scsi mid layer
1794edd16368SStephen M. Cameron 		 */
1795edd16368SStephen M. Cameron 		fixup_botched_add(h, added[i]);
1796105a3dbcSRobert Elliott 		added[i] = NULL;
1797edd16368SStephen M. Cameron 	}
1798edd16368SStephen M. Cameron 
1799edd16368SStephen M. Cameron free_and_out:
1800edd16368SStephen M. Cameron 	kfree(added);
1801edd16368SStephen M. Cameron 	kfree(removed);
1802edd16368SStephen M. Cameron }
1803edd16368SStephen M. Cameron 
1804edd16368SStephen M. Cameron /*
18059e03aa2fSJoe Perches  * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
1806edd16368SStephen M. Cameron  * Assume's h->devlock is held.
1807edd16368SStephen M. Cameron  */
1808edd16368SStephen M. Cameron static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1809edd16368SStephen M. Cameron 	int bus, int target, int lun)
1810edd16368SStephen M. Cameron {
1811edd16368SStephen M. Cameron 	int i;
1812edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1813edd16368SStephen M. Cameron 
1814edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1815edd16368SStephen M. Cameron 		sd = h->dev[i];
1816edd16368SStephen M. Cameron 		if (sd->bus == bus && sd->target == target && sd->lun == lun)
1817edd16368SStephen M. Cameron 			return sd;
1818edd16368SStephen M. Cameron 	}
1819edd16368SStephen M. Cameron 	return NULL;
1820edd16368SStephen M. Cameron }
1821edd16368SStephen M. Cameron 
1822edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev)
1823edd16368SStephen M. Cameron {
1824edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1825edd16368SStephen M. Cameron 	unsigned long flags;
1826edd16368SStephen M. Cameron 	struct ctlr_info *h;
1827edd16368SStephen M. Cameron 
1828edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
1829edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1830edd16368SStephen M. Cameron 	sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1831edd16368SStephen M. Cameron 		sdev_id(sdev), sdev->lun);
183241ce4c35SStephen Cameron 	if (likely(sd)) {
183303383736SDon Brace 		atomic_set(&sd->ioaccel_cmds_out, 0);
183441ce4c35SStephen Cameron 		sdev->hostdata = (sd->expose_state & HPSA_SCSI_ADD) ? sd : NULL;
183541ce4c35SStephen Cameron 	} else
183641ce4c35SStephen Cameron 		sdev->hostdata = NULL;
1837edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1838edd16368SStephen M. Cameron 	return 0;
1839edd16368SStephen M. Cameron }
1840edd16368SStephen M. Cameron 
184141ce4c35SStephen Cameron /* configure scsi device based on internal per-device structure */
184241ce4c35SStephen Cameron static int hpsa_slave_configure(struct scsi_device *sdev)
184341ce4c35SStephen Cameron {
184441ce4c35SStephen Cameron 	struct hpsa_scsi_dev_t *sd;
184541ce4c35SStephen Cameron 	int queue_depth;
184641ce4c35SStephen Cameron 
184741ce4c35SStephen Cameron 	sd = sdev->hostdata;
184841ce4c35SStephen Cameron 	sdev->no_uld_attach = !sd || !(sd->expose_state & HPSA_ULD_ATTACH);
184941ce4c35SStephen Cameron 
185041ce4c35SStephen Cameron 	if (sd)
185141ce4c35SStephen Cameron 		queue_depth = sd->queue_depth != 0 ?
185241ce4c35SStephen Cameron 			sd->queue_depth : sdev->host->can_queue;
185341ce4c35SStephen Cameron 	else
185441ce4c35SStephen Cameron 		queue_depth = sdev->host->can_queue;
185541ce4c35SStephen Cameron 
185641ce4c35SStephen Cameron 	scsi_change_queue_depth(sdev, queue_depth);
185741ce4c35SStephen Cameron 
185841ce4c35SStephen Cameron 	return 0;
185941ce4c35SStephen Cameron }
186041ce4c35SStephen Cameron 
1861edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev)
1862edd16368SStephen M. Cameron {
1863bcc44255SStephen M. Cameron 	/* nothing to do. */
1864edd16368SStephen M. Cameron }
1865edd16368SStephen M. Cameron 
1866d9a729f3SWebb Scales static void hpsa_free_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
1867d9a729f3SWebb Scales {
1868d9a729f3SWebb Scales 	int i;
1869d9a729f3SWebb Scales 
1870d9a729f3SWebb Scales 	if (!h->ioaccel2_cmd_sg_list)
1871d9a729f3SWebb Scales 		return;
1872d9a729f3SWebb Scales 	for (i = 0; i < h->nr_cmds; i++) {
1873d9a729f3SWebb Scales 		kfree(h->ioaccel2_cmd_sg_list[i]);
1874d9a729f3SWebb Scales 		h->ioaccel2_cmd_sg_list[i] = NULL;
1875d9a729f3SWebb Scales 	}
1876d9a729f3SWebb Scales 	kfree(h->ioaccel2_cmd_sg_list);
1877d9a729f3SWebb Scales 	h->ioaccel2_cmd_sg_list = NULL;
1878d9a729f3SWebb Scales }
1879d9a729f3SWebb Scales 
1880d9a729f3SWebb Scales static int hpsa_allocate_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
1881d9a729f3SWebb Scales {
1882d9a729f3SWebb Scales 	int i;
1883d9a729f3SWebb Scales 
1884d9a729f3SWebb Scales 	if (h->chainsize <= 0)
1885d9a729f3SWebb Scales 		return 0;
1886d9a729f3SWebb Scales 
1887d9a729f3SWebb Scales 	h->ioaccel2_cmd_sg_list =
1888d9a729f3SWebb Scales 		kzalloc(sizeof(*h->ioaccel2_cmd_sg_list) * h->nr_cmds,
1889d9a729f3SWebb Scales 					GFP_KERNEL);
1890d9a729f3SWebb Scales 	if (!h->ioaccel2_cmd_sg_list)
1891d9a729f3SWebb Scales 		return -ENOMEM;
1892d9a729f3SWebb Scales 	for (i = 0; i < h->nr_cmds; i++) {
1893d9a729f3SWebb Scales 		h->ioaccel2_cmd_sg_list[i] =
1894d9a729f3SWebb Scales 			kmalloc(sizeof(*h->ioaccel2_cmd_sg_list[i]) *
1895d9a729f3SWebb Scales 					h->maxsgentries, GFP_KERNEL);
1896d9a729f3SWebb Scales 		if (!h->ioaccel2_cmd_sg_list[i])
1897d9a729f3SWebb Scales 			goto clean;
1898d9a729f3SWebb Scales 	}
1899d9a729f3SWebb Scales 	return 0;
1900d9a729f3SWebb Scales 
1901d9a729f3SWebb Scales clean:
1902d9a729f3SWebb Scales 	hpsa_free_ioaccel2_sg_chain_blocks(h);
1903d9a729f3SWebb Scales 	return -ENOMEM;
1904d9a729f3SWebb Scales }
1905d9a729f3SWebb Scales 
190633a2ffceSStephen M. Cameron static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
190733a2ffceSStephen M. Cameron {
190833a2ffceSStephen M. Cameron 	int i;
190933a2ffceSStephen M. Cameron 
191033a2ffceSStephen M. Cameron 	if (!h->cmd_sg_list)
191133a2ffceSStephen M. Cameron 		return;
191233a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
191333a2ffceSStephen M. Cameron 		kfree(h->cmd_sg_list[i]);
191433a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = NULL;
191533a2ffceSStephen M. Cameron 	}
191633a2ffceSStephen M. Cameron 	kfree(h->cmd_sg_list);
191733a2ffceSStephen M. Cameron 	h->cmd_sg_list = NULL;
191833a2ffceSStephen M. Cameron }
191933a2ffceSStephen M. Cameron 
1920105a3dbcSRobert Elliott static int hpsa_alloc_sg_chain_blocks(struct ctlr_info *h)
192133a2ffceSStephen M. Cameron {
192233a2ffceSStephen M. Cameron 	int i;
192333a2ffceSStephen M. Cameron 
192433a2ffceSStephen M. Cameron 	if (h->chainsize <= 0)
192533a2ffceSStephen M. Cameron 		return 0;
192633a2ffceSStephen M. Cameron 
192733a2ffceSStephen M. Cameron 	h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
192833a2ffceSStephen M. Cameron 				GFP_KERNEL);
19293d4e6af8SRobert Elliott 	if (!h->cmd_sg_list) {
19303d4e6af8SRobert Elliott 		dev_err(&h->pdev->dev, "Failed to allocate SG list\n");
193133a2ffceSStephen M. Cameron 		return -ENOMEM;
19323d4e6af8SRobert Elliott 	}
193333a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
193433a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
193533a2ffceSStephen M. Cameron 						h->chainsize, GFP_KERNEL);
19363d4e6af8SRobert Elliott 		if (!h->cmd_sg_list[i]) {
19373d4e6af8SRobert Elliott 			dev_err(&h->pdev->dev, "Failed to allocate cmd SG\n");
193833a2ffceSStephen M. Cameron 			goto clean;
193933a2ffceSStephen M. Cameron 		}
19403d4e6af8SRobert Elliott 	}
194133a2ffceSStephen M. Cameron 	return 0;
194233a2ffceSStephen M. Cameron 
194333a2ffceSStephen M. Cameron clean:
194433a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
194533a2ffceSStephen M. Cameron 	return -ENOMEM;
194633a2ffceSStephen M. Cameron }
194733a2ffceSStephen M. Cameron 
1948d9a729f3SWebb Scales static int hpsa_map_ioaccel2_sg_chain_block(struct ctlr_info *h,
1949d9a729f3SWebb Scales 	struct io_accel2_cmd *cp, struct CommandList *c)
1950d9a729f3SWebb Scales {
1951d9a729f3SWebb Scales 	struct ioaccel2_sg_element *chain_block;
1952d9a729f3SWebb Scales 	u64 temp64;
1953d9a729f3SWebb Scales 	u32 chain_size;
1954d9a729f3SWebb Scales 
1955d9a729f3SWebb Scales 	chain_block = h->ioaccel2_cmd_sg_list[c->cmdindex];
1956d9a729f3SWebb Scales 	chain_size = le32_to_cpu(cp->data_len);
1957d9a729f3SWebb Scales 	temp64 = pci_map_single(h->pdev, chain_block, chain_size,
1958d9a729f3SWebb Scales 				PCI_DMA_TODEVICE);
1959d9a729f3SWebb Scales 	if (dma_mapping_error(&h->pdev->dev, temp64)) {
1960d9a729f3SWebb Scales 		/* prevent subsequent unmapping */
1961d9a729f3SWebb Scales 		cp->sg->address = 0;
1962d9a729f3SWebb Scales 		return -1;
1963d9a729f3SWebb Scales 	}
1964d9a729f3SWebb Scales 	cp->sg->address = cpu_to_le64(temp64);
1965d9a729f3SWebb Scales 	return 0;
1966d9a729f3SWebb Scales }
1967d9a729f3SWebb Scales 
1968d9a729f3SWebb Scales static void hpsa_unmap_ioaccel2_sg_chain_block(struct ctlr_info *h,
1969d9a729f3SWebb Scales 	struct io_accel2_cmd *cp)
1970d9a729f3SWebb Scales {
1971d9a729f3SWebb Scales 	struct ioaccel2_sg_element *chain_sg;
1972d9a729f3SWebb Scales 	u64 temp64;
1973d9a729f3SWebb Scales 	u32 chain_size;
1974d9a729f3SWebb Scales 
1975d9a729f3SWebb Scales 	chain_sg = cp->sg;
1976d9a729f3SWebb Scales 	temp64 = le64_to_cpu(chain_sg->address);
1977d9a729f3SWebb Scales 	chain_size = le32_to_cpu(cp->data_len);
1978d9a729f3SWebb Scales 	pci_unmap_single(h->pdev, temp64, chain_size, PCI_DMA_TODEVICE);
1979d9a729f3SWebb Scales }
1980d9a729f3SWebb Scales 
1981e2bea6dfSStephen M. Cameron static int hpsa_map_sg_chain_block(struct ctlr_info *h,
198233a2ffceSStephen M. Cameron 	struct CommandList *c)
198333a2ffceSStephen M. Cameron {
198433a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg, *chain_block;
198533a2ffceSStephen M. Cameron 	u64 temp64;
198650a0decfSStephen M. Cameron 	u32 chain_len;
198733a2ffceSStephen M. Cameron 
198833a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
198933a2ffceSStephen M. Cameron 	chain_block = h->cmd_sg_list[c->cmdindex];
199050a0decfSStephen M. Cameron 	chain_sg->Ext = cpu_to_le32(HPSA_SG_CHAIN);
199150a0decfSStephen M. Cameron 	chain_len = sizeof(*chain_sg) *
19922b08b3e9SDon Brace 		(le16_to_cpu(c->Header.SGTotal) - h->max_cmd_sg_entries);
199350a0decfSStephen M. Cameron 	chain_sg->Len = cpu_to_le32(chain_len);
199450a0decfSStephen M. Cameron 	temp64 = pci_map_single(h->pdev, chain_block, chain_len,
199533a2ffceSStephen M. Cameron 				PCI_DMA_TODEVICE);
1996e2bea6dfSStephen M. Cameron 	if (dma_mapping_error(&h->pdev->dev, temp64)) {
1997e2bea6dfSStephen M. Cameron 		/* prevent subsequent unmapping */
199850a0decfSStephen M. Cameron 		chain_sg->Addr = cpu_to_le64(0);
1999e2bea6dfSStephen M. Cameron 		return -1;
2000e2bea6dfSStephen M. Cameron 	}
200150a0decfSStephen M. Cameron 	chain_sg->Addr = cpu_to_le64(temp64);
2002e2bea6dfSStephen M. Cameron 	return 0;
200333a2ffceSStephen M. Cameron }
200433a2ffceSStephen M. Cameron 
200533a2ffceSStephen M. Cameron static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
200633a2ffceSStephen M. Cameron 	struct CommandList *c)
200733a2ffceSStephen M. Cameron {
200833a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg;
200933a2ffceSStephen M. Cameron 
201050a0decfSStephen M. Cameron 	if (le16_to_cpu(c->Header.SGTotal) <= h->max_cmd_sg_entries)
201133a2ffceSStephen M. Cameron 		return;
201233a2ffceSStephen M. Cameron 
201333a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
201450a0decfSStephen M. Cameron 	pci_unmap_single(h->pdev, le64_to_cpu(chain_sg->Addr),
201550a0decfSStephen M. Cameron 			le32_to_cpu(chain_sg->Len), PCI_DMA_TODEVICE);
201633a2ffceSStephen M. Cameron }
201733a2ffceSStephen M. Cameron 
2018a09c1441SScott Teel 
2019a09c1441SScott Teel /* Decode the various types of errors on ioaccel2 path.
2020a09c1441SScott Teel  * Return 1 for any error that should generate a RAID path retry.
2021a09c1441SScott Teel  * Return 0 for errors that don't require a RAID path retry.
2022a09c1441SScott Teel  */
2023a09c1441SScott Teel static int handle_ioaccel_mode2_error(struct ctlr_info *h,
2024c349775eSScott Teel 					struct CommandList *c,
2025c349775eSScott Teel 					struct scsi_cmnd *cmd,
2026c349775eSScott Teel 					struct io_accel2_cmd *c2)
2027c349775eSScott Teel {
2028c349775eSScott Teel 	int data_len;
2029a09c1441SScott Teel 	int retry = 0;
2030c40820d5SJoe Handzik 	u32 ioaccel2_resid = 0;
2031c349775eSScott Teel 
2032c349775eSScott Teel 	switch (c2->error_data.serv_response) {
2033c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_COMPLETE:
2034c349775eSScott Teel 		switch (c2->error_data.status) {
2035c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
2036c349775eSScott Teel 			break;
2037c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
2038ee6b1889SStephen M. Cameron 			cmd->result |= SAM_STAT_CHECK_CONDITION;
2039c349775eSScott Teel 			if (c2->error_data.data_present !=
2040ee6b1889SStephen M. Cameron 					IOACCEL2_SENSE_DATA_PRESENT) {
2041ee6b1889SStephen M. Cameron 				memset(cmd->sense_buffer, 0,
2042ee6b1889SStephen M. Cameron 					SCSI_SENSE_BUFFERSIZE);
2043c349775eSScott Teel 				break;
2044ee6b1889SStephen M. Cameron 			}
2045c349775eSScott Teel 			/* copy the sense data */
2046c349775eSScott Teel 			data_len = c2->error_data.sense_data_len;
2047c349775eSScott Teel 			if (data_len > SCSI_SENSE_BUFFERSIZE)
2048c349775eSScott Teel 				data_len = SCSI_SENSE_BUFFERSIZE;
2049c349775eSScott Teel 			if (data_len > sizeof(c2->error_data.sense_data_buff))
2050c349775eSScott Teel 				data_len =
2051c349775eSScott Teel 					sizeof(c2->error_data.sense_data_buff);
2052c349775eSScott Teel 			memcpy(cmd->sense_buffer,
2053c349775eSScott Teel 				c2->error_data.sense_data_buff, data_len);
2054a09c1441SScott Teel 			retry = 1;
2055c349775eSScott Teel 			break;
2056c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
2057a09c1441SScott Teel 			retry = 1;
2058c349775eSScott Teel 			break;
2059c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
2060a09c1441SScott Teel 			retry = 1;
2061c349775eSScott Teel 			break;
2062c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
20634a8da22bSStephen Cameron 			retry = 1;
2064c349775eSScott Teel 			break;
2065c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
2066a09c1441SScott Teel 			retry = 1;
2067c349775eSScott Teel 			break;
2068c349775eSScott Teel 		default:
2069a09c1441SScott Teel 			retry = 1;
2070c349775eSScott Teel 			break;
2071c349775eSScott Teel 		}
2072c349775eSScott Teel 		break;
2073c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_FAILURE:
2074c40820d5SJoe Handzik 		switch (c2->error_data.status) {
2075c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_IO_ERROR:
2076c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_IO_ABORTED:
2077c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_OVERRUN:
2078c40820d5SJoe Handzik 			retry = 1;
2079c40820d5SJoe Handzik 			break;
2080c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_UNDERRUN:
2081c40820d5SJoe Handzik 			cmd->result = (DID_OK << 16);		/* host byte */
2082c40820d5SJoe Handzik 			cmd->result |= (COMMAND_COMPLETE << 8);	/* msg byte */
2083c40820d5SJoe Handzik 			ioaccel2_resid = get_unaligned_le32(
2084c40820d5SJoe Handzik 						&c2->error_data.resid_cnt[0]);
2085c40820d5SJoe Handzik 			scsi_set_resid(cmd, ioaccel2_resid);
2086c40820d5SJoe Handzik 			break;
2087c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_NO_PATH_TO_DEVICE:
2088c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_INVALID_DEVICE:
2089c40820d5SJoe Handzik 		case IOACCEL2_STATUS_SR_IOACCEL_DISABLED:
2090c40820d5SJoe Handzik 			/* We will get an event from ctlr to trigger rescan */
2091c40820d5SJoe Handzik 			retry = 1;
2092c40820d5SJoe Handzik 			break;
2093c40820d5SJoe Handzik 		default:
2094c40820d5SJoe Handzik 			retry = 1;
2095c40820d5SJoe Handzik 		}
2096c349775eSScott Teel 		break;
2097c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
2098c349775eSScott Teel 		break;
2099c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
2100c349775eSScott Teel 		break;
2101c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
2102a09c1441SScott Teel 		retry = 1;
2103c349775eSScott Teel 		break;
2104c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
2105c349775eSScott Teel 		break;
2106c349775eSScott Teel 	default:
2107a09c1441SScott Teel 		retry = 1;
2108c349775eSScott Teel 		break;
2109c349775eSScott Teel 	}
2110a09c1441SScott Teel 
2111a09c1441SScott Teel 	return retry;	/* retry on raid path? */
2112c349775eSScott Teel }
2113c349775eSScott Teel 
2114a58e7e53SWebb Scales static void hpsa_cmd_resolve_events(struct ctlr_info *h,
2115a58e7e53SWebb Scales 		struct CommandList *c)
2116a58e7e53SWebb Scales {
2117d604f533SWebb Scales 	bool do_wake = false;
2118d604f533SWebb Scales 
2119a58e7e53SWebb Scales 	/*
2120a58e7e53SWebb Scales 	 * Prevent the following race in the abort handler:
2121a58e7e53SWebb Scales 	 *
2122a58e7e53SWebb Scales 	 * 1. LLD is requested to abort a SCSI command
2123a58e7e53SWebb Scales 	 * 2. The SCSI command completes
2124a58e7e53SWebb Scales 	 * 3. The struct CommandList associated with step 2 is made available
2125a58e7e53SWebb Scales 	 * 4. New I/O request to LLD to another LUN re-uses struct CommandList
2126a58e7e53SWebb Scales 	 * 5. Abort handler follows scsi_cmnd->host_scribble and
2127a58e7e53SWebb Scales 	 *    finds struct CommandList and tries to aborts it
2128a58e7e53SWebb Scales 	 * Now we have aborted the wrong command.
2129a58e7e53SWebb Scales 	 *
2130d604f533SWebb Scales 	 * Reset c->scsi_cmd here so that the abort or reset handler will know
2131d604f533SWebb Scales 	 * this command has completed.  Then, check to see if the handler is
2132a58e7e53SWebb Scales 	 * waiting for this command, and, if so, wake it.
2133a58e7e53SWebb Scales 	 */
2134a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_IDLE;
2135d604f533SWebb Scales 	mb();	/* Declare command idle before checking for pending events. */
2136a58e7e53SWebb Scales 	if (c->abort_pending) {
2137d604f533SWebb Scales 		do_wake = true;
2138a58e7e53SWebb Scales 		c->abort_pending = false;
2139a58e7e53SWebb Scales 	}
2140d604f533SWebb Scales 	if (c->reset_pending) {
2141d604f533SWebb Scales 		unsigned long flags;
2142d604f533SWebb Scales 		struct hpsa_scsi_dev_t *dev;
2143d604f533SWebb Scales 
2144d604f533SWebb Scales 		/*
2145d604f533SWebb Scales 		 * There appears to be a reset pending; lock the lock and
2146d604f533SWebb Scales 		 * reconfirm.  If so, then decrement the count of outstanding
2147d604f533SWebb Scales 		 * commands and wake the reset command if this is the last one.
2148d604f533SWebb Scales 		 */
2149d604f533SWebb Scales 		spin_lock_irqsave(&h->lock, flags);
2150d604f533SWebb Scales 		dev = c->reset_pending;		/* Re-fetch under the lock. */
2151d604f533SWebb Scales 		if (dev && atomic_dec_and_test(&dev->reset_cmds_out))
2152d604f533SWebb Scales 			do_wake = true;
2153d604f533SWebb Scales 		c->reset_pending = NULL;
2154d604f533SWebb Scales 		spin_unlock_irqrestore(&h->lock, flags);
2155d604f533SWebb Scales 	}
2156d604f533SWebb Scales 
2157d604f533SWebb Scales 	if (do_wake)
2158d604f533SWebb Scales 		wake_up_all(&h->event_sync_wait_queue);
2159a58e7e53SWebb Scales }
2160a58e7e53SWebb Scales 
216173153fe5SWebb Scales static void hpsa_cmd_resolve_and_free(struct ctlr_info *h,
216273153fe5SWebb Scales 				      struct CommandList *c)
216373153fe5SWebb Scales {
216473153fe5SWebb Scales 	hpsa_cmd_resolve_events(h, c);
216573153fe5SWebb Scales 	cmd_tagged_free(h, c);
216673153fe5SWebb Scales }
216773153fe5SWebb Scales 
21688a0ff92cSWebb Scales static void hpsa_cmd_free_and_done(struct ctlr_info *h,
21698a0ff92cSWebb Scales 		struct CommandList *c, struct scsi_cmnd *cmd)
21708a0ff92cSWebb Scales {
217173153fe5SWebb Scales 	hpsa_cmd_resolve_and_free(h, c);
21728a0ff92cSWebb Scales 	cmd->scsi_done(cmd);
21738a0ff92cSWebb Scales }
21748a0ff92cSWebb Scales 
21758a0ff92cSWebb Scales static void hpsa_retry_cmd(struct ctlr_info *h, struct CommandList *c)
21768a0ff92cSWebb Scales {
21778a0ff92cSWebb Scales 	INIT_WORK(&c->work, hpsa_command_resubmit_worker);
21788a0ff92cSWebb Scales 	queue_work_on(raw_smp_processor_id(), h->resubmit_wq, &c->work);
21798a0ff92cSWebb Scales }
21808a0ff92cSWebb Scales 
2181a58e7e53SWebb Scales static void hpsa_set_scsi_cmd_aborted(struct scsi_cmnd *cmd)
2182a58e7e53SWebb Scales {
2183a58e7e53SWebb Scales 	cmd->result = DID_ABORT << 16;
2184a58e7e53SWebb Scales }
2185a58e7e53SWebb Scales 
2186a58e7e53SWebb Scales static void hpsa_cmd_abort_and_free(struct ctlr_info *h, struct CommandList *c,
2187a58e7e53SWebb Scales 				    struct scsi_cmnd *cmd)
2188a58e7e53SWebb Scales {
2189a58e7e53SWebb Scales 	hpsa_set_scsi_cmd_aborted(cmd);
2190a58e7e53SWebb Scales 	dev_warn(&h->pdev->dev, "CDB %16phN was aborted with status 0x%x\n",
2191a58e7e53SWebb Scales 			 c->Request.CDB, c->err_info->ScsiStatus);
219273153fe5SWebb Scales 	hpsa_cmd_resolve_and_free(h, c);
2193a58e7e53SWebb Scales }
2194a58e7e53SWebb Scales 
2195c349775eSScott Teel static void process_ioaccel2_completion(struct ctlr_info *h,
2196c349775eSScott Teel 		struct CommandList *c, struct scsi_cmnd *cmd,
2197c349775eSScott Teel 		struct hpsa_scsi_dev_t *dev)
2198c349775eSScott Teel {
2199c349775eSScott Teel 	struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
2200c349775eSScott Teel 
2201c349775eSScott Teel 	/* check for good status */
2202c349775eSScott Teel 	if (likely(c2->error_data.serv_response == 0 &&
22038a0ff92cSWebb Scales 			c2->error_data.status == 0))
22048a0ff92cSWebb Scales 		return hpsa_cmd_free_and_done(h, c, cmd);
2205c349775eSScott Teel 
22068a0ff92cSWebb Scales 	/*
22078a0ff92cSWebb Scales 	 * Any RAID offload error results in retry which will use
2208c349775eSScott Teel 	 * the normal I/O path so the controller can handle whatever's
2209c349775eSScott Teel 	 * wrong.
2210c349775eSScott Teel 	 */
2211c349775eSScott Teel 	if (is_logical_dev_addr_mode(dev->scsi3addr) &&
2212c349775eSScott Teel 		c2->error_data.serv_response ==
2213c349775eSScott Teel 			IOACCEL2_SERV_RESPONSE_FAILURE) {
2214080ef1ccSDon Brace 		if (c2->error_data.status ==
2215080ef1ccSDon Brace 			IOACCEL2_STATUS_SR_IOACCEL_DISABLED)
2216c349775eSScott Teel 			dev->offload_enabled = 0;
22178a0ff92cSWebb Scales 
22188a0ff92cSWebb Scales 		return hpsa_retry_cmd(h, c);
2219080ef1ccSDon Brace 	}
2220080ef1ccSDon Brace 
2221080ef1ccSDon Brace 	if (handle_ioaccel_mode2_error(h, c, cmd, c2))
22228a0ff92cSWebb Scales 		return hpsa_retry_cmd(h, c);
2223080ef1ccSDon Brace 
22248a0ff92cSWebb Scales 	return hpsa_cmd_free_and_done(h, c, cmd);
2225c349775eSScott Teel }
2226c349775eSScott Teel 
22279437ac43SStephen Cameron /* Returns 0 on success, < 0 otherwise. */
22289437ac43SStephen Cameron static int hpsa_evaluate_tmf_status(struct ctlr_info *h,
22299437ac43SStephen Cameron 					struct CommandList *cp)
22309437ac43SStephen Cameron {
22319437ac43SStephen Cameron 	u8 tmf_status = cp->err_info->ScsiStatus;
22329437ac43SStephen Cameron 
22339437ac43SStephen Cameron 	switch (tmf_status) {
22349437ac43SStephen Cameron 	case CISS_TMF_COMPLETE:
22359437ac43SStephen Cameron 		/*
22369437ac43SStephen Cameron 		 * CISS_TMF_COMPLETE never happens, instead,
22379437ac43SStephen Cameron 		 * ei->CommandStatus == 0 for this case.
22389437ac43SStephen Cameron 		 */
22399437ac43SStephen Cameron 	case CISS_TMF_SUCCESS:
22409437ac43SStephen Cameron 		return 0;
22419437ac43SStephen Cameron 	case CISS_TMF_INVALID_FRAME:
22429437ac43SStephen Cameron 	case CISS_TMF_NOT_SUPPORTED:
22439437ac43SStephen Cameron 	case CISS_TMF_FAILED:
22449437ac43SStephen Cameron 	case CISS_TMF_WRONG_LUN:
22459437ac43SStephen Cameron 	case CISS_TMF_OVERLAPPED_TAG:
22469437ac43SStephen Cameron 		break;
22479437ac43SStephen Cameron 	default:
22489437ac43SStephen Cameron 		dev_warn(&h->pdev->dev, "Unknown TMF status: 0x%02x\n",
22499437ac43SStephen Cameron 				tmf_status);
22509437ac43SStephen Cameron 		break;
22519437ac43SStephen Cameron 	}
22529437ac43SStephen Cameron 	return -tmf_status;
22539437ac43SStephen Cameron }
22549437ac43SStephen Cameron 
22551fb011fbSStephen M. Cameron static void complete_scsi_command(struct CommandList *cp)
2256edd16368SStephen M. Cameron {
2257edd16368SStephen M. Cameron 	struct scsi_cmnd *cmd;
2258edd16368SStephen M. Cameron 	struct ctlr_info *h;
2259edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2260283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
2261d9a729f3SWebb Scales 	struct io_accel2_cmd *c2;
2262edd16368SStephen M. Cameron 
22639437ac43SStephen Cameron 	u8 sense_key;
22649437ac43SStephen Cameron 	u8 asc;      /* additional sense code */
22659437ac43SStephen Cameron 	u8 ascq;     /* additional sense code qualifier */
2266db111e18SStephen M. Cameron 	unsigned long sense_data_size;
2267edd16368SStephen M. Cameron 
2268edd16368SStephen M. Cameron 	ei = cp->err_info;
22697fa3030cSStephen Cameron 	cmd = cp->scsi_cmd;
2270edd16368SStephen M. Cameron 	h = cp->h;
2271283b4a9bSStephen M. Cameron 	dev = cmd->device->hostdata;
2272d9a729f3SWebb Scales 	c2 = &h->ioaccel2_cmd_pool[cp->cmdindex];
2273edd16368SStephen M. Cameron 
2274edd16368SStephen M. Cameron 	scsi_dma_unmap(cmd); /* undo the DMA mappings */
2275e1f7de0cSMatt Gates 	if ((cp->cmd_type == CMD_SCSI) &&
22762b08b3e9SDon Brace 		(le16_to_cpu(cp->Header.SGTotal) > h->max_cmd_sg_entries))
227733a2ffceSStephen M. Cameron 		hpsa_unmap_sg_chain_block(h, cp);
2278edd16368SStephen M. Cameron 
2279d9a729f3SWebb Scales 	if ((cp->cmd_type == CMD_IOACCEL2) &&
2280d9a729f3SWebb Scales 		(c2->sg[0].chain_indicator == IOACCEL2_CHAIN))
2281d9a729f3SWebb Scales 		hpsa_unmap_ioaccel2_sg_chain_block(h, c2);
2282d9a729f3SWebb Scales 
2283edd16368SStephen M. Cameron 	cmd->result = (DID_OK << 16); 		/* host byte */
2284edd16368SStephen M. Cameron 	cmd->result |= (COMMAND_COMPLETE << 8);	/* msg byte */
2285c349775eSScott Teel 
228603383736SDon Brace 	if (cp->cmd_type == CMD_IOACCEL2 || cp->cmd_type == CMD_IOACCEL1)
228703383736SDon Brace 		atomic_dec(&cp->phys_disk->ioaccel_cmds_out);
228803383736SDon Brace 
228925163bd5SWebb Scales 	/*
229025163bd5SWebb Scales 	 * We check for lockup status here as it may be set for
229125163bd5SWebb Scales 	 * CMD_SCSI, CMD_IOACCEL1 and CMD_IOACCEL2 commands by
229225163bd5SWebb Scales 	 * fail_all_oustanding_cmds()
229325163bd5SWebb Scales 	 */
229425163bd5SWebb Scales 	if (unlikely(ei->CommandStatus == CMD_CTLR_LOCKUP)) {
229525163bd5SWebb Scales 		/* DID_NO_CONNECT will prevent a retry */
229625163bd5SWebb Scales 		cmd->result = DID_NO_CONNECT << 16;
22978a0ff92cSWebb Scales 		return hpsa_cmd_free_and_done(h, cp, cmd);
229825163bd5SWebb Scales 	}
229925163bd5SWebb Scales 
2300d604f533SWebb Scales 	if ((unlikely(hpsa_is_pending_event(cp)))) {
2301d604f533SWebb Scales 		if (cp->reset_pending)
2302d604f533SWebb Scales 			return hpsa_cmd_resolve_and_free(h, cp);
2303d604f533SWebb Scales 		if (cp->abort_pending)
2304d604f533SWebb Scales 			return hpsa_cmd_abort_and_free(h, cp, cmd);
2305d604f533SWebb Scales 	}
2306d604f533SWebb Scales 
2307c349775eSScott Teel 	if (cp->cmd_type == CMD_IOACCEL2)
2308c349775eSScott Teel 		return process_ioaccel2_completion(h, cp, cmd, dev);
2309c349775eSScott Teel 
23106aa4c361SRobert Elliott 	scsi_set_resid(cmd, ei->ResidualCnt);
23118a0ff92cSWebb Scales 	if (ei->CommandStatus == 0)
23128a0ff92cSWebb Scales 		return hpsa_cmd_free_and_done(h, cp, cmd);
23136aa4c361SRobert Elliott 
2314e1f7de0cSMatt Gates 	/* For I/O accelerator commands, copy over some fields to the normal
2315e1f7de0cSMatt Gates 	 * CISS header used below for error handling.
2316e1f7de0cSMatt Gates 	 */
2317e1f7de0cSMatt Gates 	if (cp->cmd_type == CMD_IOACCEL1) {
2318e1f7de0cSMatt Gates 		struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
23192b08b3e9SDon Brace 		cp->Header.SGList = scsi_sg_count(cmd);
23202b08b3e9SDon Brace 		cp->Header.SGTotal = cpu_to_le16(cp->Header.SGList);
23212b08b3e9SDon Brace 		cp->Request.CDBLen = le16_to_cpu(c->io_flags) &
23222b08b3e9SDon Brace 			IOACCEL1_IOFLAGS_CDBLEN_MASK;
232350a0decfSStephen M. Cameron 		cp->Header.tag = c->tag;
2324e1f7de0cSMatt Gates 		memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
2325e1f7de0cSMatt Gates 		memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
2326283b4a9bSStephen M. Cameron 
2327283b4a9bSStephen M. Cameron 		/* Any RAID offload error results in retry which will use
2328283b4a9bSStephen M. Cameron 		 * the normal I/O path so the controller can handle whatever's
2329283b4a9bSStephen M. Cameron 		 * wrong.
2330283b4a9bSStephen M. Cameron 		 */
2331283b4a9bSStephen M. Cameron 		if (is_logical_dev_addr_mode(dev->scsi3addr)) {
2332283b4a9bSStephen M. Cameron 			if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
2333283b4a9bSStephen M. Cameron 				dev->offload_enabled = 0;
23348a0ff92cSWebb Scales 			return hpsa_retry_cmd(h, cp);
2335283b4a9bSStephen M. Cameron 		}
2336e1f7de0cSMatt Gates 	}
2337e1f7de0cSMatt Gates 
2338edd16368SStephen M. Cameron 	/* an error has occurred */
2339edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
2340edd16368SStephen M. Cameron 
2341edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
23429437ac43SStephen Cameron 		cmd->result |= ei->ScsiStatus;
23439437ac43SStephen Cameron 		/* copy the sense data */
23449437ac43SStephen Cameron 		if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
23459437ac43SStephen Cameron 			sense_data_size = SCSI_SENSE_BUFFERSIZE;
23469437ac43SStephen Cameron 		else
23479437ac43SStephen Cameron 			sense_data_size = sizeof(ei->SenseInfo);
23489437ac43SStephen Cameron 		if (ei->SenseLen < sense_data_size)
23499437ac43SStephen Cameron 			sense_data_size = ei->SenseLen;
23509437ac43SStephen Cameron 		memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
23519437ac43SStephen Cameron 		if (ei->ScsiStatus)
23529437ac43SStephen Cameron 			decode_sense_data(ei->SenseInfo, sense_data_size,
23539437ac43SStephen Cameron 				&sense_key, &asc, &ascq);
2354edd16368SStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
23551d3b3609SMatt Gates 			if (sense_key == ABORTED_COMMAND) {
23562e311fbaSStephen M. Cameron 				cmd->result |= DID_SOFT_ERROR << 16;
23571d3b3609SMatt Gates 				break;
23581d3b3609SMatt Gates 			}
2359edd16368SStephen M. Cameron 			break;
2360edd16368SStephen M. Cameron 		}
2361edd16368SStephen M. Cameron 		/* Problem was not a check condition
2362edd16368SStephen M. Cameron 		 * Pass it up to the upper layers...
2363edd16368SStephen M. Cameron 		 */
2364edd16368SStephen M. Cameron 		if (ei->ScsiStatus) {
2365edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
2366edd16368SStephen M. Cameron 				"Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
2367edd16368SStephen M. Cameron 				"Returning result: 0x%x\n",
2368edd16368SStephen M. Cameron 				cp, ei->ScsiStatus,
2369edd16368SStephen M. Cameron 				sense_key, asc, ascq,
2370edd16368SStephen M. Cameron 				cmd->result);
2371edd16368SStephen M. Cameron 		} else {  /* scsi status is zero??? How??? */
2372edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
2373edd16368SStephen M. Cameron 				"Returning no connection.\n", cp),
2374edd16368SStephen M. Cameron 
2375edd16368SStephen M. Cameron 			/* Ordinarily, this case should never happen,
2376edd16368SStephen M. Cameron 			 * but there is a bug in some released firmware
2377edd16368SStephen M. Cameron 			 * revisions that allows it to happen if, for
2378edd16368SStephen M. Cameron 			 * example, a 4100 backplane loses power and
2379edd16368SStephen M. Cameron 			 * the tape drive is in it.  We assume that
2380edd16368SStephen M. Cameron 			 * it's a fatal error of some kind because we
2381edd16368SStephen M. Cameron 			 * can't show that it wasn't. We will make it
2382edd16368SStephen M. Cameron 			 * look like selection timeout since that is
2383edd16368SStephen M. Cameron 			 * the most common reason for this to occur,
2384edd16368SStephen M. Cameron 			 * and it's severe enough.
2385edd16368SStephen M. Cameron 			 */
2386edd16368SStephen M. Cameron 
2387edd16368SStephen M. Cameron 			cmd->result = DID_NO_CONNECT << 16;
2388edd16368SStephen M. Cameron 		}
2389edd16368SStephen M. Cameron 		break;
2390edd16368SStephen M. Cameron 
2391edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
2392edd16368SStephen M. Cameron 		break;
2393edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
2394f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev,
2395f42e81e1SStephen Cameron 			"CDB %16phN data overrun\n", cp->Request.CDB);
2396edd16368SStephen M. Cameron 		break;
2397edd16368SStephen M. Cameron 	case CMD_INVALID: {
2398edd16368SStephen M. Cameron 		/* print_bytes(cp, sizeof(*cp), 1, 0);
2399edd16368SStephen M. Cameron 		print_cmd(cp); */
2400edd16368SStephen M. Cameron 		/* We get CMD_INVALID if you address a non-existent device
2401edd16368SStephen M. Cameron 		 * instead of a selection timeout (no response).  You will
2402edd16368SStephen M. Cameron 		 * see this if you yank out a drive, then try to access it.
2403edd16368SStephen M. Cameron 		 * This is kind of a shame because it means that any other
2404edd16368SStephen M. Cameron 		 * CMD_INVALID (e.g. driver bug) will get interpreted as a
2405edd16368SStephen M. Cameron 		 * missing target. */
2406edd16368SStephen M. Cameron 		cmd->result = DID_NO_CONNECT << 16;
2407edd16368SStephen M. Cameron 	}
2408edd16368SStephen M. Cameron 		break;
2409edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
2410256d0eaaSStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2411f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : protocol error\n",
2412f42e81e1SStephen Cameron 				cp->Request.CDB);
2413edd16368SStephen M. Cameron 		break;
2414edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
2415edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2416f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : hardware error\n",
2417f42e81e1SStephen Cameron 			cp->Request.CDB);
2418edd16368SStephen M. Cameron 		break;
2419edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
2420edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2421f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : connection lost\n",
2422f42e81e1SStephen Cameron 			cp->Request.CDB);
2423edd16368SStephen M. Cameron 		break;
2424edd16368SStephen M. Cameron 	case CMD_ABORTED:
2425a58e7e53SWebb Scales 		/* Return now to avoid calling scsi_done(). */
2426a58e7e53SWebb Scales 		return hpsa_cmd_abort_and_free(h, cp, cmd);
2427edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
2428edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2429f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : abort failed\n",
2430f42e81e1SStephen Cameron 			cp->Request.CDB);
2431edd16368SStephen M. Cameron 		break;
2432edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
2433f6e76055SStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
2434f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN : unsolicited abort\n",
2435f42e81e1SStephen Cameron 			cp->Request.CDB);
2436edd16368SStephen M. Cameron 		break;
2437edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
2438edd16368SStephen M. Cameron 		cmd->result = DID_TIME_OUT << 16;
2439f42e81e1SStephen Cameron 		dev_warn(&h->pdev->dev, "CDB %16phN timed out\n",
2440f42e81e1SStephen Cameron 			cp->Request.CDB);
2441edd16368SStephen M. Cameron 		break;
24421d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
24431d5e2ed0SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
24441d5e2ed0SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Command unabortable\n");
24451d5e2ed0SStephen M. Cameron 		break;
24469437ac43SStephen Cameron 	case CMD_TMF_STATUS:
24479437ac43SStephen Cameron 		if (hpsa_evaluate_tmf_status(h, cp)) /* TMF failed? */
24489437ac43SStephen Cameron 			cmd->result = DID_ERROR << 16;
24499437ac43SStephen Cameron 		break;
2450283b4a9bSStephen M. Cameron 	case CMD_IOACCEL_DISABLED:
2451283b4a9bSStephen M. Cameron 		/* This only handles the direct pass-through case since RAID
2452283b4a9bSStephen M. Cameron 		 * offload is handled above.  Just attempt a retry.
2453283b4a9bSStephen M. Cameron 		 */
2454283b4a9bSStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16;
2455283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev,
2456283b4a9bSStephen M. Cameron 				"cp %p had HP SSD Smart Path error\n", cp);
2457283b4a9bSStephen M. Cameron 		break;
2458edd16368SStephen M. Cameron 	default:
2459edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2460edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
2461edd16368SStephen M. Cameron 				cp, ei->CommandStatus);
2462edd16368SStephen M. Cameron 	}
24638a0ff92cSWebb Scales 
24648a0ff92cSWebb Scales 	return hpsa_cmd_free_and_done(h, cp, cmd);
2465edd16368SStephen M. Cameron }
2466edd16368SStephen M. Cameron 
2467edd16368SStephen M. Cameron static void hpsa_pci_unmap(struct pci_dev *pdev,
2468edd16368SStephen M. Cameron 	struct CommandList *c, int sg_used, int data_direction)
2469edd16368SStephen M. Cameron {
2470edd16368SStephen M. Cameron 	int i;
2471edd16368SStephen M. Cameron 
247250a0decfSStephen M. Cameron 	for (i = 0; i < sg_used; i++)
247350a0decfSStephen M. Cameron 		pci_unmap_single(pdev, (dma_addr_t) le64_to_cpu(c->SG[i].Addr),
247450a0decfSStephen M. Cameron 				le32_to_cpu(c->SG[i].Len),
2475edd16368SStephen M. Cameron 				data_direction);
2476edd16368SStephen M. Cameron }
2477edd16368SStephen M. Cameron 
2478a2dac136SStephen M. Cameron static int hpsa_map_one(struct pci_dev *pdev,
2479edd16368SStephen M. Cameron 		struct CommandList *cp,
2480edd16368SStephen M. Cameron 		unsigned char *buf,
2481edd16368SStephen M. Cameron 		size_t buflen,
2482edd16368SStephen M. Cameron 		int data_direction)
2483edd16368SStephen M. Cameron {
248401a02ffcSStephen M. Cameron 	u64 addr64;
2485edd16368SStephen M. Cameron 
2486edd16368SStephen M. Cameron 	if (buflen == 0 || data_direction == PCI_DMA_NONE) {
2487edd16368SStephen M. Cameron 		cp->Header.SGList = 0;
248850a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(0);
2489a2dac136SStephen M. Cameron 		return 0;
2490edd16368SStephen M. Cameron 	}
2491edd16368SStephen M. Cameron 
249250a0decfSStephen M. Cameron 	addr64 = pci_map_single(pdev, buf, buflen, data_direction);
2493eceaae18SShuah Khan 	if (dma_mapping_error(&pdev->dev, addr64)) {
2494a2dac136SStephen M. Cameron 		/* Prevent subsequent unmap of something never mapped */
2495eceaae18SShuah Khan 		cp->Header.SGList = 0;
249650a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(0);
2497a2dac136SStephen M. Cameron 		return -1;
2498eceaae18SShuah Khan 	}
249950a0decfSStephen M. Cameron 	cp->SG[0].Addr = cpu_to_le64(addr64);
250050a0decfSStephen M. Cameron 	cp->SG[0].Len = cpu_to_le32(buflen);
250150a0decfSStephen M. Cameron 	cp->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* we are not chaining */
250250a0decfSStephen M. Cameron 	cp->Header.SGList = 1;   /* no. SGs contig in this cmd */
250350a0decfSStephen M. Cameron 	cp->Header.SGTotal = cpu_to_le16(1); /* total sgs in cmd list */
2504a2dac136SStephen M. Cameron 	return 0;
2505edd16368SStephen M. Cameron }
2506edd16368SStephen M. Cameron 
250725163bd5SWebb Scales #define NO_TIMEOUT ((unsigned long) -1)
250825163bd5SWebb Scales #define DEFAULT_TIMEOUT 30000 /* milliseconds */
250925163bd5SWebb Scales static int hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
251025163bd5SWebb Scales 	struct CommandList *c, int reply_queue, unsigned long timeout_msecs)
2511edd16368SStephen M. Cameron {
2512edd16368SStephen M. Cameron 	DECLARE_COMPLETION_ONSTACK(wait);
2513edd16368SStephen M. Cameron 
2514edd16368SStephen M. Cameron 	c->waiting = &wait;
251525163bd5SWebb Scales 	__enqueue_cmd_and_start_io(h, c, reply_queue);
251625163bd5SWebb Scales 	if (timeout_msecs == NO_TIMEOUT) {
251725163bd5SWebb Scales 		/* TODO: get rid of this no-timeout thing */
251825163bd5SWebb Scales 		wait_for_completion_io(&wait);
251925163bd5SWebb Scales 		return IO_OK;
252025163bd5SWebb Scales 	}
252125163bd5SWebb Scales 	if (!wait_for_completion_io_timeout(&wait,
252225163bd5SWebb Scales 					msecs_to_jiffies(timeout_msecs))) {
252325163bd5SWebb Scales 		dev_warn(&h->pdev->dev, "Command timed out.\n");
252425163bd5SWebb Scales 		return -ETIMEDOUT;
252525163bd5SWebb Scales 	}
252625163bd5SWebb Scales 	return IO_OK;
252725163bd5SWebb Scales }
252825163bd5SWebb Scales 
252925163bd5SWebb Scales static int hpsa_scsi_do_simple_cmd(struct ctlr_info *h, struct CommandList *c,
253025163bd5SWebb Scales 				   int reply_queue, unsigned long timeout_msecs)
253125163bd5SWebb Scales {
253225163bd5SWebb Scales 	if (unlikely(lockup_detected(h))) {
253325163bd5SWebb Scales 		c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
253425163bd5SWebb Scales 		return IO_OK;
253525163bd5SWebb Scales 	}
253625163bd5SWebb Scales 	return hpsa_scsi_do_simple_cmd_core(h, c, reply_queue, timeout_msecs);
2537edd16368SStephen M. Cameron }
2538edd16368SStephen M. Cameron 
2539094963daSStephen M. Cameron static u32 lockup_detected(struct ctlr_info *h)
2540094963daSStephen M. Cameron {
2541094963daSStephen M. Cameron 	int cpu;
2542094963daSStephen M. Cameron 	u32 rc, *lockup_detected;
2543094963daSStephen M. Cameron 
2544094963daSStephen M. Cameron 	cpu = get_cpu();
2545094963daSStephen M. Cameron 	lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
2546094963daSStephen M. Cameron 	rc = *lockup_detected;
2547094963daSStephen M. Cameron 	put_cpu();
2548094963daSStephen M. Cameron 	return rc;
2549094963daSStephen M. Cameron }
2550094963daSStephen M. Cameron 
25519c2fc160SStephen M. Cameron #define MAX_DRIVER_CMD_RETRIES 25
255225163bd5SWebb Scales static int hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
255325163bd5SWebb Scales 	struct CommandList *c, int data_direction, unsigned long timeout_msecs)
2554edd16368SStephen M. Cameron {
25559c2fc160SStephen M. Cameron 	int backoff_time = 10, retry_count = 0;
255625163bd5SWebb Scales 	int rc;
2557edd16368SStephen M. Cameron 
2558edd16368SStephen M. Cameron 	do {
25597630abd0SJoe Perches 		memset(c->err_info, 0, sizeof(*c->err_info));
256025163bd5SWebb Scales 		rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
256125163bd5SWebb Scales 						  timeout_msecs);
256225163bd5SWebb Scales 		if (rc)
256325163bd5SWebb Scales 			break;
2564edd16368SStephen M. Cameron 		retry_count++;
25659c2fc160SStephen M. Cameron 		if (retry_count > 3) {
25669c2fc160SStephen M. Cameron 			msleep(backoff_time);
25679c2fc160SStephen M. Cameron 			if (backoff_time < 1000)
25689c2fc160SStephen M. Cameron 				backoff_time *= 2;
25699c2fc160SStephen M. Cameron 		}
2570852af20aSMatt Bondurant 	} while ((check_for_unit_attention(h, c) ||
25719c2fc160SStephen M. Cameron 			check_for_busy(h, c)) &&
25729c2fc160SStephen M. Cameron 			retry_count <= MAX_DRIVER_CMD_RETRIES);
2573edd16368SStephen M. Cameron 	hpsa_pci_unmap(h->pdev, c, 1, data_direction);
257425163bd5SWebb Scales 	if (retry_count > MAX_DRIVER_CMD_RETRIES)
257525163bd5SWebb Scales 		rc = -EIO;
257625163bd5SWebb Scales 	return rc;
2577edd16368SStephen M. Cameron }
2578edd16368SStephen M. Cameron 
2579d1e8beacSStephen M. Cameron static void hpsa_print_cmd(struct ctlr_info *h, char *txt,
2580d1e8beacSStephen M. Cameron 				struct CommandList *c)
2581edd16368SStephen M. Cameron {
2582d1e8beacSStephen M. Cameron 	const u8 *cdb = c->Request.CDB;
2583d1e8beacSStephen M. Cameron 	const u8 *lun = c->Header.LUN.LunAddrBytes;
2584edd16368SStephen M. Cameron 
2585d1e8beacSStephen M. Cameron 	dev_warn(&h->pdev->dev, "%s: LUN:%02x%02x%02x%02x%02x%02x%02x%02x"
2586d1e8beacSStephen M. Cameron 	" CDB:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
2587d1e8beacSStephen M. Cameron 		txt, lun[0], lun[1], lun[2], lun[3],
2588d1e8beacSStephen M. Cameron 		lun[4], lun[5], lun[6], lun[7],
2589d1e8beacSStephen M. Cameron 		cdb[0], cdb[1], cdb[2], cdb[3],
2590d1e8beacSStephen M. Cameron 		cdb[4], cdb[5], cdb[6], cdb[7],
2591d1e8beacSStephen M. Cameron 		cdb[8], cdb[9], cdb[10], cdb[11],
2592d1e8beacSStephen M. Cameron 		cdb[12], cdb[13], cdb[14], cdb[15]);
2593d1e8beacSStephen M. Cameron }
2594d1e8beacSStephen M. Cameron 
2595d1e8beacSStephen M. Cameron static void hpsa_scsi_interpret_error(struct ctlr_info *h,
2596d1e8beacSStephen M. Cameron 			struct CommandList *cp)
2597d1e8beacSStephen M. Cameron {
2598d1e8beacSStephen M. Cameron 	const struct ErrorInfo *ei = cp->err_info;
2599d1e8beacSStephen M. Cameron 	struct device *d = &cp->h->pdev->dev;
26009437ac43SStephen Cameron 	u8 sense_key, asc, ascq;
26019437ac43SStephen Cameron 	int sense_len;
2602d1e8beacSStephen M. Cameron 
2603edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
2604edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
26059437ac43SStephen Cameron 		if (ei->SenseLen > sizeof(ei->SenseInfo))
26069437ac43SStephen Cameron 			sense_len = sizeof(ei->SenseInfo);
26079437ac43SStephen Cameron 		else
26089437ac43SStephen Cameron 			sense_len = ei->SenseLen;
26099437ac43SStephen Cameron 		decode_sense_data(ei->SenseInfo, sense_len,
26109437ac43SStephen Cameron 					&sense_key, &asc, &ascq);
2611d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "SCSI status", cp);
2612d1e8beacSStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION)
26139437ac43SStephen Cameron 			dev_warn(d, "SCSI Status = 02, Sense key = 0x%02x, ASC = 0x%02x, ASCQ = 0x%02x\n",
26149437ac43SStephen Cameron 				sense_key, asc, ascq);
2615d1e8beacSStephen M. Cameron 		else
26169437ac43SStephen Cameron 			dev_warn(d, "SCSI Status = 0x%02x\n", ei->ScsiStatus);
2617edd16368SStephen M. Cameron 		if (ei->ScsiStatus == 0)
2618edd16368SStephen M. Cameron 			dev_warn(d, "SCSI status is abnormally zero.  "
2619edd16368SStephen M. Cameron 			"(probably indicates selection timeout "
2620edd16368SStephen M. Cameron 			"reported incorrectly due to a known "
2621edd16368SStephen M. Cameron 			"firmware bug, circa July, 2001.)\n");
2622edd16368SStephen M. Cameron 		break;
2623edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
2624edd16368SStephen M. Cameron 		break;
2625edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
2626d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "overrun condition", cp);
2627edd16368SStephen M. Cameron 		break;
2628edd16368SStephen M. Cameron 	case CMD_INVALID: {
2629edd16368SStephen M. Cameron 		/* controller unfortunately reports SCSI passthru's
2630edd16368SStephen M. Cameron 		 * to non-existent targets as invalid commands.
2631edd16368SStephen M. Cameron 		 */
2632d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "invalid command", cp);
2633d1e8beacSStephen M. Cameron 		dev_warn(d, "probably means device no longer present\n");
2634edd16368SStephen M. Cameron 		}
2635edd16368SStephen M. Cameron 		break;
2636edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
2637d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "protocol error", cp);
2638edd16368SStephen M. Cameron 		break;
2639edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
2640d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "hardware error", cp);
2641edd16368SStephen M. Cameron 		break;
2642edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
2643d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "connection lost", cp);
2644edd16368SStephen M. Cameron 		break;
2645edd16368SStephen M. Cameron 	case CMD_ABORTED:
2646d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "aborted", cp);
2647edd16368SStephen M. Cameron 		break;
2648edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
2649d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "abort failed", cp);
2650edd16368SStephen M. Cameron 		break;
2651edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
2652d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unsolicited abort", cp);
2653edd16368SStephen M. Cameron 		break;
2654edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
2655d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "timed out", cp);
2656edd16368SStephen M. Cameron 		break;
26571d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
2658d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unabortable", cp);
26591d5e2ed0SStephen M. Cameron 		break;
266025163bd5SWebb Scales 	case CMD_CTLR_LOCKUP:
266125163bd5SWebb Scales 		hpsa_print_cmd(h, "controller lockup detected", cp);
266225163bd5SWebb Scales 		break;
2663edd16368SStephen M. Cameron 	default:
2664d1e8beacSStephen M. Cameron 		hpsa_print_cmd(h, "unknown status", cp);
2665d1e8beacSStephen M. Cameron 		dev_warn(d, "Unknown command status %x\n",
2666edd16368SStephen M. Cameron 				ei->CommandStatus);
2667edd16368SStephen M. Cameron 	}
2668edd16368SStephen M. Cameron }
2669edd16368SStephen M. Cameron 
2670edd16368SStephen M. Cameron static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
2671b7bb24ebSStephen M. Cameron 			u16 page, unsigned char *buf,
2672edd16368SStephen M. Cameron 			unsigned char bufsize)
2673edd16368SStephen M. Cameron {
2674edd16368SStephen M. Cameron 	int rc = IO_OK;
2675edd16368SStephen M. Cameron 	struct CommandList *c;
2676edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2677edd16368SStephen M. Cameron 
267845fcb86eSStephen Cameron 	c = cmd_alloc(h);
2679edd16368SStephen M. Cameron 
2680a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
2681a2dac136SStephen M. Cameron 			page, scsi3addr, TYPE_CMD)) {
2682a2dac136SStephen M. Cameron 		rc = -1;
2683a2dac136SStephen M. Cameron 		goto out;
2684a2dac136SStephen M. Cameron 	}
268525163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
268625163bd5SWebb Scales 					PCI_DMA_FROMDEVICE, NO_TIMEOUT);
268725163bd5SWebb Scales 	if (rc)
268825163bd5SWebb Scales 		goto out;
2689edd16368SStephen M. Cameron 	ei = c->err_info;
2690edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2691d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2692edd16368SStephen M. Cameron 		rc = -1;
2693edd16368SStephen M. Cameron 	}
2694a2dac136SStephen M. Cameron out:
269545fcb86eSStephen Cameron 	cmd_free(h, c);
2696edd16368SStephen M. Cameron 	return rc;
2697edd16368SStephen M. Cameron }
2698edd16368SStephen M. Cameron 
2699bf711ac6SScott Teel static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
270025163bd5SWebb Scales 	u8 reset_type, int reply_queue)
2701edd16368SStephen M. Cameron {
2702edd16368SStephen M. Cameron 	int rc = IO_OK;
2703edd16368SStephen M. Cameron 	struct CommandList *c;
2704edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2705edd16368SStephen M. Cameron 
270645fcb86eSStephen Cameron 	c = cmd_alloc(h);
2707edd16368SStephen M. Cameron 
2708edd16368SStephen M. Cameron 
2709a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map. */
2710bf711ac6SScott Teel 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
2711bf711ac6SScott Teel 			scsi3addr, TYPE_MSG);
2712bf711ac6SScott Teel 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to LUN reset */
271325163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
271425163bd5SWebb Scales 	if (rc) {
271525163bd5SWebb Scales 		dev_warn(&h->pdev->dev, "Failed to send reset command\n");
271625163bd5SWebb Scales 		goto out;
271725163bd5SWebb Scales 	}
2718edd16368SStephen M. Cameron 	/* no unmap needed here because no data xfer. */
2719edd16368SStephen M. Cameron 
2720edd16368SStephen M. Cameron 	ei = c->err_info;
2721edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0) {
2722d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
2723edd16368SStephen M. Cameron 		rc = -1;
2724edd16368SStephen M. Cameron 	}
272525163bd5SWebb Scales out:
272645fcb86eSStephen Cameron 	cmd_free(h, c);
2727edd16368SStephen M. Cameron 	return rc;
2728edd16368SStephen M. Cameron }
2729edd16368SStephen M. Cameron 
2730d604f533SWebb Scales static bool hpsa_cmd_dev_match(struct ctlr_info *h, struct CommandList *c,
2731d604f533SWebb Scales 			       struct hpsa_scsi_dev_t *dev,
2732d604f533SWebb Scales 			       unsigned char *scsi3addr)
2733d604f533SWebb Scales {
2734d604f533SWebb Scales 	int i;
2735d604f533SWebb Scales 	bool match = false;
2736d604f533SWebb Scales 	struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
2737d604f533SWebb Scales 	struct hpsa_tmf_struct *ac = (struct hpsa_tmf_struct *) c2;
2738d604f533SWebb Scales 
2739d604f533SWebb Scales 	if (hpsa_is_cmd_idle(c))
2740d604f533SWebb Scales 		return false;
2741d604f533SWebb Scales 
2742d604f533SWebb Scales 	switch (c->cmd_type) {
2743d604f533SWebb Scales 	case CMD_SCSI:
2744d604f533SWebb Scales 	case CMD_IOCTL_PEND:
2745d604f533SWebb Scales 		match = !memcmp(scsi3addr, &c->Header.LUN.LunAddrBytes,
2746d604f533SWebb Scales 				sizeof(c->Header.LUN.LunAddrBytes));
2747d604f533SWebb Scales 		break;
2748d604f533SWebb Scales 
2749d604f533SWebb Scales 	case CMD_IOACCEL1:
2750d604f533SWebb Scales 	case CMD_IOACCEL2:
2751d604f533SWebb Scales 		if (c->phys_disk == dev) {
2752d604f533SWebb Scales 			/* HBA mode match */
2753d604f533SWebb Scales 			match = true;
2754d604f533SWebb Scales 		} else {
2755d604f533SWebb Scales 			/* Possible RAID mode -- check each phys dev. */
2756d604f533SWebb Scales 			/* FIXME:  Do we need to take out a lock here?  If
2757d604f533SWebb Scales 			 * so, we could just call hpsa_get_pdisk_of_ioaccel2()
2758d604f533SWebb Scales 			 * instead. */
2759d604f533SWebb Scales 			for (i = 0; i < dev->nphysical_disks && !match; i++) {
2760d604f533SWebb Scales 				/* FIXME: an alternate test might be
2761d604f533SWebb Scales 				 *
2762d604f533SWebb Scales 				 * match = dev->phys_disk[i]->ioaccel_handle
2763d604f533SWebb Scales 				 *              == c2->scsi_nexus;      */
2764d604f533SWebb Scales 				match = dev->phys_disk[i] == c->phys_disk;
2765d604f533SWebb Scales 			}
2766d604f533SWebb Scales 		}
2767d604f533SWebb Scales 		break;
2768d604f533SWebb Scales 
2769d604f533SWebb Scales 	case IOACCEL2_TMF:
2770d604f533SWebb Scales 		for (i = 0; i < dev->nphysical_disks && !match; i++) {
2771d604f533SWebb Scales 			match = dev->phys_disk[i]->ioaccel_handle ==
2772d604f533SWebb Scales 					le32_to_cpu(ac->it_nexus);
2773d604f533SWebb Scales 		}
2774d604f533SWebb Scales 		break;
2775d604f533SWebb Scales 
2776d604f533SWebb Scales 	case 0:		/* The command is in the middle of being initialized. */
2777d604f533SWebb Scales 		match = false;
2778d604f533SWebb Scales 		break;
2779d604f533SWebb Scales 
2780d604f533SWebb Scales 	default:
2781d604f533SWebb Scales 		dev_err(&h->pdev->dev, "unexpected cmd_type: %d\n",
2782d604f533SWebb Scales 			c->cmd_type);
2783d604f533SWebb Scales 		BUG();
2784d604f533SWebb Scales 	}
2785d604f533SWebb Scales 
2786d604f533SWebb Scales 	return match;
2787d604f533SWebb Scales }
2788d604f533SWebb Scales 
2789d604f533SWebb Scales static int hpsa_do_reset(struct ctlr_info *h, struct hpsa_scsi_dev_t *dev,
2790d604f533SWebb Scales 	unsigned char *scsi3addr, u8 reset_type, int reply_queue)
2791d604f533SWebb Scales {
2792d604f533SWebb Scales 	int i;
2793d604f533SWebb Scales 	int rc = 0;
2794d604f533SWebb Scales 
2795d604f533SWebb Scales 	/* We can really only handle one reset at a time */
2796d604f533SWebb Scales 	if (mutex_lock_interruptible(&h->reset_mutex) == -EINTR) {
2797d604f533SWebb Scales 		dev_warn(&h->pdev->dev, "concurrent reset wait interrupted.\n");
2798d604f533SWebb Scales 		return -EINTR;
2799d604f533SWebb Scales 	}
2800d604f533SWebb Scales 
2801d604f533SWebb Scales 	BUG_ON(atomic_read(&dev->reset_cmds_out) != 0);
2802d604f533SWebb Scales 
2803d604f533SWebb Scales 	for (i = 0; i < h->nr_cmds; i++) {
2804d604f533SWebb Scales 		struct CommandList *c = h->cmd_pool + i;
2805d604f533SWebb Scales 		int refcount = atomic_inc_return(&c->refcount);
2806d604f533SWebb Scales 
2807d604f533SWebb Scales 		if (refcount > 1 && hpsa_cmd_dev_match(h, c, dev, scsi3addr)) {
2808d604f533SWebb Scales 			unsigned long flags;
2809d604f533SWebb Scales 
2810d604f533SWebb Scales 			/*
2811d604f533SWebb Scales 			 * Mark the target command as having a reset pending,
2812d604f533SWebb Scales 			 * then lock a lock so that the command cannot complete
2813d604f533SWebb Scales 			 * while we're considering it.  If the command is not
2814d604f533SWebb Scales 			 * idle then count it; otherwise revoke the event.
2815d604f533SWebb Scales 			 */
2816d604f533SWebb Scales 			c->reset_pending = dev;
2817d604f533SWebb Scales 			spin_lock_irqsave(&h->lock, flags);	/* Implied MB */
2818d604f533SWebb Scales 			if (!hpsa_is_cmd_idle(c))
2819d604f533SWebb Scales 				atomic_inc(&dev->reset_cmds_out);
2820d604f533SWebb Scales 			else
2821d604f533SWebb Scales 				c->reset_pending = NULL;
2822d604f533SWebb Scales 			spin_unlock_irqrestore(&h->lock, flags);
2823d604f533SWebb Scales 		}
2824d604f533SWebb Scales 
2825d604f533SWebb Scales 		cmd_free(h, c);
2826d604f533SWebb Scales 	}
2827d604f533SWebb Scales 
2828d604f533SWebb Scales 	rc = hpsa_send_reset(h, scsi3addr, reset_type, reply_queue);
2829d604f533SWebb Scales 	if (!rc)
2830d604f533SWebb Scales 		wait_event(h->event_sync_wait_queue,
2831d604f533SWebb Scales 			atomic_read(&dev->reset_cmds_out) == 0 ||
2832d604f533SWebb Scales 			lockup_detected(h));
2833d604f533SWebb Scales 
2834d604f533SWebb Scales 	if (unlikely(lockup_detected(h))) {
2835d604f533SWebb Scales 		dev_warn(&h->pdev->dev,
2836d604f533SWebb Scales 			 "Controller lockup detected during reset wait\n");
2837d604f533SWebb Scales 		rc = -ENODEV;
2838d604f533SWebb Scales 	}
2839d604f533SWebb Scales 
2840d604f533SWebb Scales 	if (unlikely(rc))
2841d604f533SWebb Scales 		atomic_set(&dev->reset_cmds_out, 0);
2842d604f533SWebb Scales 
2843d604f533SWebb Scales 	mutex_unlock(&h->reset_mutex);
2844d604f533SWebb Scales 	return rc;
2845d604f533SWebb Scales }
2846d604f533SWebb Scales 
2847edd16368SStephen M. Cameron static void hpsa_get_raid_level(struct ctlr_info *h,
2848edd16368SStephen M. Cameron 	unsigned char *scsi3addr, unsigned char *raid_level)
2849edd16368SStephen M. Cameron {
2850edd16368SStephen M. Cameron 	int rc;
2851edd16368SStephen M. Cameron 	unsigned char *buf;
2852edd16368SStephen M. Cameron 
2853edd16368SStephen M. Cameron 	*raid_level = RAID_UNKNOWN;
2854edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2855edd16368SStephen M. Cameron 	if (!buf)
2856edd16368SStephen M. Cameron 		return;
2857b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0xC1, buf, 64);
2858edd16368SStephen M. Cameron 	if (rc == 0)
2859edd16368SStephen M. Cameron 		*raid_level = buf[8];
2860edd16368SStephen M. Cameron 	if (*raid_level > RAID_UNKNOWN)
2861edd16368SStephen M. Cameron 		*raid_level = RAID_UNKNOWN;
2862edd16368SStephen M. Cameron 	kfree(buf);
2863edd16368SStephen M. Cameron 	return;
2864edd16368SStephen M. Cameron }
2865edd16368SStephen M. Cameron 
2866283b4a9bSStephen M. Cameron #define HPSA_MAP_DEBUG
2867283b4a9bSStephen M. Cameron #ifdef HPSA_MAP_DEBUG
2868283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
2869283b4a9bSStephen M. Cameron 				struct raid_map_data *map_buff)
2870283b4a9bSStephen M. Cameron {
2871283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map_buff->data[0];
2872283b4a9bSStephen M. Cameron 	int map, row, col;
2873283b4a9bSStephen M. Cameron 	u16 map_cnt, row_cnt, disks_per_row;
2874283b4a9bSStephen M. Cameron 
2875283b4a9bSStephen M. Cameron 	if (rc != 0)
2876283b4a9bSStephen M. Cameron 		return;
2877283b4a9bSStephen M. Cameron 
28782ba8bfc8SStephen M. Cameron 	/* Show details only if debugging has been activated. */
28792ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug < 2)
28802ba8bfc8SStephen M. Cameron 		return;
28812ba8bfc8SStephen M. Cameron 
2882283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "structure_size = %u\n",
2883283b4a9bSStephen M. Cameron 				le32_to_cpu(map_buff->structure_size));
2884283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
2885283b4a9bSStephen M. Cameron 			le32_to_cpu(map_buff->volume_blk_size));
2886283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
2887283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->volume_blk_cnt));
2888283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
2889283b4a9bSStephen M. Cameron 			map_buff->phys_blk_shift);
2890283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
2891283b4a9bSStephen M. Cameron 			map_buff->parity_rotation_shift);
2892283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "strip_size = %u\n",
2893283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->strip_size));
2894283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
2895283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_starting_blk));
2896283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
2897283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_blk_cnt));
2898283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
2899283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->data_disks_per_row));
2900283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
2901283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->metadata_disks_per_row));
2902283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "row_cnt = %u\n",
2903283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->row_cnt));
2904283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "layout_map_count = %u\n",
2905283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->layout_map_count));
29062b08b3e9SDon Brace 	dev_info(&h->pdev->dev, "flags = 0x%x\n",
2907dd0e19f3SScott Teel 			le16_to_cpu(map_buff->flags));
29082b08b3e9SDon Brace 	dev_info(&h->pdev->dev, "encrypytion = %s\n",
29092b08b3e9SDon Brace 			le16_to_cpu(map_buff->flags) &
29102b08b3e9SDon Brace 			RAID_MAP_FLAG_ENCRYPT_ON ?  "ON" : "OFF");
2911dd0e19f3SScott Teel 	dev_info(&h->pdev->dev, "dekindex = %u\n",
2912dd0e19f3SScott Teel 			le16_to_cpu(map_buff->dekindex));
2913283b4a9bSStephen M. Cameron 	map_cnt = le16_to_cpu(map_buff->layout_map_count);
2914283b4a9bSStephen M. Cameron 	for (map = 0; map < map_cnt; map++) {
2915283b4a9bSStephen M. Cameron 		dev_info(&h->pdev->dev, "Map%u:\n", map);
2916283b4a9bSStephen M. Cameron 		row_cnt = le16_to_cpu(map_buff->row_cnt);
2917283b4a9bSStephen M. Cameron 		for (row = 0; row < row_cnt; row++) {
2918283b4a9bSStephen M. Cameron 			dev_info(&h->pdev->dev, "  Row%u:\n", row);
2919283b4a9bSStephen M. Cameron 			disks_per_row =
2920283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->data_disks_per_row);
2921283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2922283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2923283b4a9bSStephen M. Cameron 					"    D%02u: h=0x%04x xor=%u,%u\n",
2924283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2925283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2926283b4a9bSStephen M. Cameron 			disks_per_row =
2927283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->metadata_disks_per_row);
2928283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2929283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2930283b4a9bSStephen M. Cameron 					"    M%02u: h=0x%04x xor=%u,%u\n",
2931283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2932283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2933283b4a9bSStephen M. Cameron 		}
2934283b4a9bSStephen M. Cameron 	}
2935283b4a9bSStephen M. Cameron }
2936283b4a9bSStephen M. Cameron #else
2937283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
2938283b4a9bSStephen M. Cameron 			__attribute__((unused)) int rc,
2939283b4a9bSStephen M. Cameron 			__attribute__((unused)) struct raid_map_data *map_buff)
2940283b4a9bSStephen M. Cameron {
2941283b4a9bSStephen M. Cameron }
2942283b4a9bSStephen M. Cameron #endif
2943283b4a9bSStephen M. Cameron 
2944283b4a9bSStephen M. Cameron static int hpsa_get_raid_map(struct ctlr_info *h,
2945283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2946283b4a9bSStephen M. Cameron {
2947283b4a9bSStephen M. Cameron 	int rc = 0;
2948283b4a9bSStephen M. Cameron 	struct CommandList *c;
2949283b4a9bSStephen M. Cameron 	struct ErrorInfo *ei;
2950283b4a9bSStephen M. Cameron 
295145fcb86eSStephen Cameron 	c = cmd_alloc(h);
2952bf43caf3SRobert Elliott 
2953283b4a9bSStephen M. Cameron 	if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
2954283b4a9bSStephen M. Cameron 			sizeof(this_device->raid_map), 0,
2955283b4a9bSStephen M. Cameron 			scsi3addr, TYPE_CMD)) {
29562dd02d74SRobert Elliott 		dev_warn(&h->pdev->dev, "hpsa_get_raid_map fill_cmd failed\n");
29572dd02d74SRobert Elliott 		cmd_free(h, c);
29582dd02d74SRobert Elliott 		return -1;
2959283b4a9bSStephen M. Cameron 	}
296025163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
296125163bd5SWebb Scales 					PCI_DMA_FROMDEVICE, NO_TIMEOUT);
296225163bd5SWebb Scales 	if (rc)
296325163bd5SWebb Scales 		goto out;
2964283b4a9bSStephen M. Cameron 	ei = c->err_info;
2965283b4a9bSStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2966d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
296725163bd5SWebb Scales 		rc = -1;
296825163bd5SWebb Scales 		goto out;
2969283b4a9bSStephen M. Cameron 	}
297045fcb86eSStephen Cameron 	cmd_free(h, c);
2971283b4a9bSStephen M. Cameron 
2972283b4a9bSStephen M. Cameron 	/* @todo in the future, dynamically allocate RAID map memory */
2973283b4a9bSStephen M. Cameron 	if (le32_to_cpu(this_device->raid_map.structure_size) >
2974283b4a9bSStephen M. Cameron 				sizeof(this_device->raid_map)) {
2975283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
2976283b4a9bSStephen M. Cameron 		rc = -1;
2977283b4a9bSStephen M. Cameron 	}
2978283b4a9bSStephen M. Cameron 	hpsa_debug_map_buff(h, rc, &this_device->raid_map);
2979283b4a9bSStephen M. Cameron 	return rc;
298025163bd5SWebb Scales out:
298125163bd5SWebb Scales 	cmd_free(h, c);
298225163bd5SWebb Scales 	return rc;
2983283b4a9bSStephen M. Cameron }
2984283b4a9bSStephen M. Cameron 
298503383736SDon Brace static int hpsa_bmic_id_physical_device(struct ctlr_info *h,
298603383736SDon Brace 		unsigned char scsi3addr[], u16 bmic_device_index,
298703383736SDon Brace 		struct bmic_identify_physical_device *buf, size_t bufsize)
298803383736SDon Brace {
298903383736SDon Brace 	int rc = IO_OK;
299003383736SDon Brace 	struct CommandList *c;
299103383736SDon Brace 	struct ErrorInfo *ei;
299203383736SDon Brace 
299303383736SDon Brace 	c = cmd_alloc(h);
299403383736SDon Brace 	rc = fill_cmd(c, BMIC_IDENTIFY_PHYSICAL_DEVICE, h, buf, bufsize,
299503383736SDon Brace 		0, RAID_CTLR_LUNID, TYPE_CMD);
299603383736SDon Brace 	if (rc)
299703383736SDon Brace 		goto out;
299803383736SDon Brace 
299903383736SDon Brace 	c->Request.CDB[2] = bmic_device_index & 0xff;
300003383736SDon Brace 	c->Request.CDB[9] = (bmic_device_index >> 8) & 0xff;
300103383736SDon Brace 
300225163bd5SWebb Scales 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE,
300325163bd5SWebb Scales 						NO_TIMEOUT);
300403383736SDon Brace 	ei = c->err_info;
300503383736SDon Brace 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
300603383736SDon Brace 		hpsa_scsi_interpret_error(h, c);
300703383736SDon Brace 		rc = -1;
300803383736SDon Brace 	}
300903383736SDon Brace out:
301003383736SDon Brace 	cmd_free(h, c);
301103383736SDon Brace 	return rc;
301203383736SDon Brace }
301303383736SDon Brace 
30141b70150aSStephen M. Cameron static int hpsa_vpd_page_supported(struct ctlr_info *h,
30151b70150aSStephen M. Cameron 	unsigned char scsi3addr[], u8 page)
30161b70150aSStephen M. Cameron {
30171b70150aSStephen M. Cameron 	int rc;
30181b70150aSStephen M. Cameron 	int i;
30191b70150aSStephen M. Cameron 	int pages;
30201b70150aSStephen M. Cameron 	unsigned char *buf, bufsize;
30211b70150aSStephen M. Cameron 
30221b70150aSStephen M. Cameron 	buf = kzalloc(256, GFP_KERNEL);
30231b70150aSStephen M. Cameron 	if (!buf)
30241b70150aSStephen M. Cameron 		return 0;
30251b70150aSStephen M. Cameron 
30261b70150aSStephen M. Cameron 	/* Get the size of the page list first */
30271b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
30281b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
30291b70150aSStephen M. Cameron 				buf, HPSA_VPD_HEADER_SZ);
30301b70150aSStephen M. Cameron 	if (rc != 0)
30311b70150aSStephen M. Cameron 		goto exit_unsupported;
30321b70150aSStephen M. Cameron 	pages = buf[3];
30331b70150aSStephen M. Cameron 	if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
30341b70150aSStephen M. Cameron 		bufsize = pages + HPSA_VPD_HEADER_SZ;
30351b70150aSStephen M. Cameron 	else
30361b70150aSStephen M. Cameron 		bufsize = 255;
30371b70150aSStephen M. Cameron 
30381b70150aSStephen M. Cameron 	/* Get the whole VPD page list */
30391b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
30401b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
30411b70150aSStephen M. Cameron 				buf, bufsize);
30421b70150aSStephen M. Cameron 	if (rc != 0)
30431b70150aSStephen M. Cameron 		goto exit_unsupported;
30441b70150aSStephen M. Cameron 
30451b70150aSStephen M. Cameron 	pages = buf[3];
30461b70150aSStephen M. Cameron 	for (i = 1; i <= pages; i++)
30471b70150aSStephen M. Cameron 		if (buf[3 + i] == page)
30481b70150aSStephen M. Cameron 			goto exit_supported;
30491b70150aSStephen M. Cameron exit_unsupported:
30501b70150aSStephen M. Cameron 	kfree(buf);
30511b70150aSStephen M. Cameron 	return 0;
30521b70150aSStephen M. Cameron exit_supported:
30531b70150aSStephen M. Cameron 	kfree(buf);
30541b70150aSStephen M. Cameron 	return 1;
30551b70150aSStephen M. Cameron }
30561b70150aSStephen M. Cameron 
3057283b4a9bSStephen M. Cameron static void hpsa_get_ioaccel_status(struct ctlr_info *h,
3058283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
3059283b4a9bSStephen M. Cameron {
3060283b4a9bSStephen M. Cameron 	int rc;
3061283b4a9bSStephen M. Cameron 	unsigned char *buf;
3062283b4a9bSStephen M. Cameron 	u8 ioaccel_status;
3063283b4a9bSStephen M. Cameron 
3064283b4a9bSStephen M. Cameron 	this_device->offload_config = 0;
3065283b4a9bSStephen M. Cameron 	this_device->offload_enabled = 0;
306641ce4c35SStephen Cameron 	this_device->offload_to_be_enabled = 0;
3067283b4a9bSStephen M. Cameron 
3068283b4a9bSStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
3069283b4a9bSStephen M. Cameron 	if (!buf)
3070283b4a9bSStephen M. Cameron 		return;
30711b70150aSStephen M. Cameron 	if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
30721b70150aSStephen M. Cameron 		goto out;
3073283b4a9bSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
3074b7bb24ebSStephen M. Cameron 			VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
3075283b4a9bSStephen M. Cameron 	if (rc != 0)
3076283b4a9bSStephen M. Cameron 		goto out;
3077283b4a9bSStephen M. Cameron 
3078283b4a9bSStephen M. Cameron #define IOACCEL_STATUS_BYTE 4
3079283b4a9bSStephen M. Cameron #define OFFLOAD_CONFIGURED_BIT 0x01
3080283b4a9bSStephen M. Cameron #define OFFLOAD_ENABLED_BIT 0x02
3081283b4a9bSStephen M. Cameron 	ioaccel_status = buf[IOACCEL_STATUS_BYTE];
3082283b4a9bSStephen M. Cameron 	this_device->offload_config =
3083283b4a9bSStephen M. Cameron 		!!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
3084283b4a9bSStephen M. Cameron 	if (this_device->offload_config) {
3085283b4a9bSStephen M. Cameron 		this_device->offload_enabled =
3086283b4a9bSStephen M. Cameron 			!!(ioaccel_status & OFFLOAD_ENABLED_BIT);
3087283b4a9bSStephen M. Cameron 		if (hpsa_get_raid_map(h, scsi3addr, this_device))
3088283b4a9bSStephen M. Cameron 			this_device->offload_enabled = 0;
3089283b4a9bSStephen M. Cameron 	}
309041ce4c35SStephen Cameron 	this_device->offload_to_be_enabled = this_device->offload_enabled;
3091283b4a9bSStephen M. Cameron out:
3092283b4a9bSStephen M. Cameron 	kfree(buf);
3093283b4a9bSStephen M. Cameron 	return;
3094283b4a9bSStephen M. Cameron }
3095283b4a9bSStephen M. Cameron 
3096edd16368SStephen M. Cameron /* Get the device id from inquiry page 0x83 */
3097edd16368SStephen M. Cameron static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
3098edd16368SStephen M. Cameron 	unsigned char *device_id, int buflen)
3099edd16368SStephen M. Cameron {
3100edd16368SStephen M. Cameron 	int rc;
3101edd16368SStephen M. Cameron 	unsigned char *buf;
3102edd16368SStephen M. Cameron 
3103edd16368SStephen M. Cameron 	if (buflen > 16)
3104edd16368SStephen M. Cameron 		buflen = 16;
3105edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
3106edd16368SStephen M. Cameron 	if (!buf)
3107a84d794dSStephen M. Cameron 		return -ENOMEM;
3108b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0x83, buf, 64);
3109edd16368SStephen M. Cameron 	if (rc == 0)
3110edd16368SStephen M. Cameron 		memcpy(device_id, &buf[8], buflen);
3111edd16368SStephen M. Cameron 	kfree(buf);
3112edd16368SStephen M. Cameron 	return rc != 0;
3113edd16368SStephen M. Cameron }
3114edd16368SStephen M. Cameron 
3115edd16368SStephen M. Cameron static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
311603383736SDon Brace 		void *buf, int bufsize,
3117edd16368SStephen M. Cameron 		int extended_response)
3118edd16368SStephen M. Cameron {
3119edd16368SStephen M. Cameron 	int rc = IO_OK;
3120edd16368SStephen M. Cameron 	struct CommandList *c;
3121edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
3122edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
3123edd16368SStephen M. Cameron 
312445fcb86eSStephen Cameron 	c = cmd_alloc(h);
3125bf43caf3SRobert Elliott 
3126e89c0ae7SStephen M. Cameron 	/* address the controller */
3127e89c0ae7SStephen M. Cameron 	memset(scsi3addr, 0, sizeof(scsi3addr));
3128a2dac136SStephen M. Cameron 	if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
3129a2dac136SStephen M. Cameron 		buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
3130a2dac136SStephen M. Cameron 		rc = -1;
3131a2dac136SStephen M. Cameron 		goto out;
3132a2dac136SStephen M. Cameron 	}
3133edd16368SStephen M. Cameron 	if (extended_response)
3134edd16368SStephen M. Cameron 		c->Request.CDB[1] = extended_response;
313525163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
313625163bd5SWebb Scales 					PCI_DMA_FROMDEVICE, NO_TIMEOUT);
313725163bd5SWebb Scales 	if (rc)
313825163bd5SWebb Scales 		goto out;
3139edd16368SStephen M. Cameron 	ei = c->err_info;
3140edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 &&
3141edd16368SStephen M. Cameron 	    ei->CommandStatus != CMD_DATA_UNDERRUN) {
3142d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
3143edd16368SStephen M. Cameron 		rc = -1;
3144283b4a9bSStephen M. Cameron 	} else {
314503383736SDon Brace 		struct ReportLUNdata *rld = buf;
314603383736SDon Brace 
314703383736SDon Brace 		if (rld->extended_response_flag != extended_response) {
3148283b4a9bSStephen M. Cameron 			dev_err(&h->pdev->dev,
3149283b4a9bSStephen M. Cameron 				"report luns requested format %u, got %u\n",
3150283b4a9bSStephen M. Cameron 				extended_response,
315103383736SDon Brace 				rld->extended_response_flag);
3152283b4a9bSStephen M. Cameron 			rc = -1;
3153283b4a9bSStephen M. Cameron 		}
3154edd16368SStephen M. Cameron 	}
3155a2dac136SStephen M. Cameron out:
315645fcb86eSStephen Cameron 	cmd_free(h, c);
3157edd16368SStephen M. Cameron 	return rc;
3158edd16368SStephen M. Cameron }
3159edd16368SStephen M. Cameron 
3160edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
316103383736SDon Brace 		struct ReportExtendedLUNdata *buf, int bufsize)
3162edd16368SStephen M. Cameron {
316303383736SDon Brace 	return hpsa_scsi_do_report_luns(h, 0, buf, bufsize,
316403383736SDon Brace 						HPSA_REPORT_PHYS_EXTENDED);
3165edd16368SStephen M. Cameron }
3166edd16368SStephen M. Cameron 
3167edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
3168edd16368SStephen M. Cameron 		struct ReportLUNdata *buf, int bufsize)
3169edd16368SStephen M. Cameron {
3170edd16368SStephen M. Cameron 	return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
3171edd16368SStephen M. Cameron }
3172edd16368SStephen M. Cameron 
3173edd16368SStephen M. Cameron static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
3174edd16368SStephen M. Cameron 	int bus, int target, int lun)
3175edd16368SStephen M. Cameron {
3176edd16368SStephen M. Cameron 	device->bus = bus;
3177edd16368SStephen M. Cameron 	device->target = target;
3178edd16368SStephen M. Cameron 	device->lun = lun;
3179edd16368SStephen M. Cameron }
3180edd16368SStephen M. Cameron 
31819846590eSStephen M. Cameron /* Use VPD inquiry to get details of volume status */
31829846590eSStephen M. Cameron static int hpsa_get_volume_status(struct ctlr_info *h,
31839846590eSStephen M. Cameron 					unsigned char scsi3addr[])
31849846590eSStephen M. Cameron {
31859846590eSStephen M. Cameron 	int rc;
31869846590eSStephen M. Cameron 	int status;
31879846590eSStephen M. Cameron 	int size;
31889846590eSStephen M. Cameron 	unsigned char *buf;
31899846590eSStephen M. Cameron 
31909846590eSStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
31919846590eSStephen M. Cameron 	if (!buf)
31929846590eSStephen M. Cameron 		return HPSA_VPD_LV_STATUS_UNSUPPORTED;
31939846590eSStephen M. Cameron 
31949846590eSStephen M. Cameron 	/* Does controller have VPD for logical volume status? */
319524a4b078SStephen M. Cameron 	if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_STATUS))
31969846590eSStephen M. Cameron 		goto exit_failed;
31979846590eSStephen M. Cameron 
31989846590eSStephen M. Cameron 	/* Get the size of the VPD return buffer */
31999846590eSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
32009846590eSStephen M. Cameron 					buf, HPSA_VPD_HEADER_SZ);
320124a4b078SStephen M. Cameron 	if (rc != 0)
32029846590eSStephen M. Cameron 		goto exit_failed;
32039846590eSStephen M. Cameron 	size = buf[3];
32049846590eSStephen M. Cameron 
32059846590eSStephen M. Cameron 	/* Now get the whole VPD buffer */
32069846590eSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
32079846590eSStephen M. Cameron 					buf, size + HPSA_VPD_HEADER_SZ);
320824a4b078SStephen M. Cameron 	if (rc != 0)
32099846590eSStephen M. Cameron 		goto exit_failed;
32109846590eSStephen M. Cameron 	status = buf[4]; /* status byte */
32119846590eSStephen M. Cameron 
32129846590eSStephen M. Cameron 	kfree(buf);
32139846590eSStephen M. Cameron 	return status;
32149846590eSStephen M. Cameron exit_failed:
32159846590eSStephen M. Cameron 	kfree(buf);
32169846590eSStephen M. Cameron 	return HPSA_VPD_LV_STATUS_UNSUPPORTED;
32179846590eSStephen M. Cameron }
32189846590eSStephen M. Cameron 
32199846590eSStephen M. Cameron /* Determine offline status of a volume.
32209846590eSStephen M. Cameron  * Return either:
32219846590eSStephen M. Cameron  *  0 (not offline)
322267955ba3SStephen M. Cameron  *  0xff (offline for unknown reasons)
32239846590eSStephen M. Cameron  *  # (integer code indicating one of several NOT READY states
32249846590eSStephen M. Cameron  *     describing why a volume is to be kept offline)
32259846590eSStephen M. Cameron  */
322667955ba3SStephen M. Cameron static int hpsa_volume_offline(struct ctlr_info *h,
32279846590eSStephen M. Cameron 					unsigned char scsi3addr[])
32289846590eSStephen M. Cameron {
32299846590eSStephen M. Cameron 	struct CommandList *c;
32309437ac43SStephen Cameron 	unsigned char *sense;
32319437ac43SStephen Cameron 	u8 sense_key, asc, ascq;
32329437ac43SStephen Cameron 	int sense_len;
323325163bd5SWebb Scales 	int rc, ldstat = 0;
32349846590eSStephen M. Cameron 	u16 cmd_status;
32359846590eSStephen M. Cameron 	u8 scsi_status;
32369846590eSStephen M. Cameron #define ASC_LUN_NOT_READY 0x04
32379846590eSStephen M. Cameron #define ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS 0x04
32389846590eSStephen M. Cameron #define ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ 0x02
32399846590eSStephen M. Cameron 
32409846590eSStephen M. Cameron 	c = cmd_alloc(h);
3241bf43caf3SRobert Elliott 
32429846590eSStephen M. Cameron 	(void) fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, scsi3addr, TYPE_CMD);
324325163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
324425163bd5SWebb Scales 	if (rc) {
324525163bd5SWebb Scales 		cmd_free(h, c);
324625163bd5SWebb Scales 		return 0;
324725163bd5SWebb Scales 	}
32489846590eSStephen M. Cameron 	sense = c->err_info->SenseInfo;
32499437ac43SStephen Cameron 	if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
32509437ac43SStephen Cameron 		sense_len = sizeof(c->err_info->SenseInfo);
32519437ac43SStephen Cameron 	else
32529437ac43SStephen Cameron 		sense_len = c->err_info->SenseLen;
32539437ac43SStephen Cameron 	decode_sense_data(sense, sense_len, &sense_key, &asc, &ascq);
32549846590eSStephen M. Cameron 	cmd_status = c->err_info->CommandStatus;
32559846590eSStephen M. Cameron 	scsi_status = c->err_info->ScsiStatus;
32569846590eSStephen M. Cameron 	cmd_free(h, c);
32579846590eSStephen M. Cameron 	/* Is the volume 'not ready'? */
32589846590eSStephen M. Cameron 	if (cmd_status != CMD_TARGET_STATUS ||
32599846590eSStephen M. Cameron 		scsi_status != SAM_STAT_CHECK_CONDITION ||
32609846590eSStephen M. Cameron 		sense_key != NOT_READY ||
32619846590eSStephen M. Cameron 		asc != ASC_LUN_NOT_READY)  {
32629846590eSStephen M. Cameron 		return 0;
32639846590eSStephen M. Cameron 	}
32649846590eSStephen M. Cameron 
32659846590eSStephen M. Cameron 	/* Determine the reason for not ready state */
32669846590eSStephen M. Cameron 	ldstat = hpsa_get_volume_status(h, scsi3addr);
32679846590eSStephen M. Cameron 
32689846590eSStephen M. Cameron 	/* Keep volume offline in certain cases: */
32699846590eSStephen M. Cameron 	switch (ldstat) {
32709846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ERASE:
3271*5ca01204SScott Benesh 	case HPSA_LV_NOT_AVAILABLE:
32729846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_RPI:
32739846590eSStephen M. Cameron 	case HPSA_LV_PENDING_RPI:
32749846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_NO_KEY:
32759846590eSStephen M. Cameron 	case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
32769846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION:
32779846590eSStephen M. Cameron 	case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
32789846590eSStephen M. Cameron 	case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
32799846590eSStephen M. Cameron 		return ldstat;
32809846590eSStephen M. Cameron 	case HPSA_VPD_LV_STATUS_UNSUPPORTED:
32819846590eSStephen M. Cameron 		/* If VPD status page isn't available,
32829846590eSStephen M. Cameron 		 * use ASC/ASCQ to determine state
32839846590eSStephen M. Cameron 		 */
32849846590eSStephen M. Cameron 		if ((ascq == ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS) ||
32859846590eSStephen M. Cameron 			(ascq == ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ))
32869846590eSStephen M. Cameron 			return ldstat;
32879846590eSStephen M. Cameron 		break;
32889846590eSStephen M. Cameron 	default:
32899846590eSStephen M. Cameron 		break;
32909846590eSStephen M. Cameron 	}
32919846590eSStephen M. Cameron 	return 0;
32929846590eSStephen M. Cameron }
32939846590eSStephen M. Cameron 
32949b5c48c2SStephen Cameron /*
32959b5c48c2SStephen Cameron  * Find out if a logical device supports aborts by simply trying one.
32969b5c48c2SStephen Cameron  * Smart Array may claim not to support aborts on logical drives, but
32979b5c48c2SStephen Cameron  * if a MSA2000 * is connected, the drives on that will be presented
32989b5c48c2SStephen Cameron  * by the Smart Array as logical drives, and aborts may be sent to
32999b5c48c2SStephen Cameron  * those devices successfully.  So the simplest way to find out is
33009b5c48c2SStephen Cameron  * to simply try an abort and see how the device responds.
33019b5c48c2SStephen Cameron  */
33029b5c48c2SStephen Cameron static int hpsa_device_supports_aborts(struct ctlr_info *h,
33039b5c48c2SStephen Cameron 					unsigned char *scsi3addr)
33049b5c48c2SStephen Cameron {
33059b5c48c2SStephen Cameron 	struct CommandList *c;
33069b5c48c2SStephen Cameron 	struct ErrorInfo *ei;
33079b5c48c2SStephen Cameron 	int rc = 0;
33089b5c48c2SStephen Cameron 
33099b5c48c2SStephen Cameron 	u64 tag = (u64) -1; /* bogus tag */
33109b5c48c2SStephen Cameron 
33119b5c48c2SStephen Cameron 	/* Assume that physical devices support aborts */
33129b5c48c2SStephen Cameron 	if (!is_logical_dev_addr_mode(scsi3addr))
33139b5c48c2SStephen Cameron 		return 1;
33149b5c48c2SStephen Cameron 
33159b5c48c2SStephen Cameron 	c = cmd_alloc(h);
3316bf43caf3SRobert Elliott 
33179b5c48c2SStephen Cameron 	(void) fill_cmd(c, HPSA_ABORT_MSG, h, &tag, 0, 0, scsi3addr, TYPE_MSG);
33189b5c48c2SStephen Cameron 	(void) hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
33199b5c48c2SStephen Cameron 	/* no unmap needed here because no data xfer. */
33209b5c48c2SStephen Cameron 	ei = c->err_info;
33219b5c48c2SStephen Cameron 	switch (ei->CommandStatus) {
33229b5c48c2SStephen Cameron 	case CMD_INVALID:
33239b5c48c2SStephen Cameron 		rc = 0;
33249b5c48c2SStephen Cameron 		break;
33259b5c48c2SStephen Cameron 	case CMD_UNABORTABLE:
33269b5c48c2SStephen Cameron 	case CMD_ABORT_FAILED:
33279b5c48c2SStephen Cameron 		rc = 1;
33289b5c48c2SStephen Cameron 		break;
33299437ac43SStephen Cameron 	case CMD_TMF_STATUS:
33309437ac43SStephen Cameron 		rc = hpsa_evaluate_tmf_status(h, c);
33319437ac43SStephen Cameron 		break;
33329b5c48c2SStephen Cameron 	default:
33339b5c48c2SStephen Cameron 		rc = 0;
33349b5c48c2SStephen Cameron 		break;
33359b5c48c2SStephen Cameron 	}
33369b5c48c2SStephen Cameron 	cmd_free(h, c);
33379b5c48c2SStephen Cameron 	return rc;
33389b5c48c2SStephen Cameron }
33399b5c48c2SStephen Cameron 
3340edd16368SStephen M. Cameron static int hpsa_update_device_info(struct ctlr_info *h,
33410b0e1d6cSStephen M. Cameron 	unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
33420b0e1d6cSStephen M. Cameron 	unsigned char *is_OBDR_device)
3343edd16368SStephen M. Cameron {
33440b0e1d6cSStephen M. Cameron 
33450b0e1d6cSStephen M. Cameron #define OBDR_SIG_OFFSET 43
33460b0e1d6cSStephen M. Cameron #define OBDR_TAPE_SIG "$DR-10"
33470b0e1d6cSStephen M. Cameron #define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
33480b0e1d6cSStephen M. Cameron #define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
33490b0e1d6cSStephen M. Cameron 
3350ea6d3bc3SStephen M. Cameron 	unsigned char *inq_buff;
33510b0e1d6cSStephen M. Cameron 	unsigned char *obdr_sig;
3352edd16368SStephen M. Cameron 
3353ea6d3bc3SStephen M. Cameron 	inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
3354edd16368SStephen M. Cameron 	if (!inq_buff)
3355edd16368SStephen M. Cameron 		goto bail_out;
3356edd16368SStephen M. Cameron 
3357edd16368SStephen M. Cameron 	/* Do an inquiry to the device to see what it is. */
3358edd16368SStephen M. Cameron 	if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
3359edd16368SStephen M. Cameron 		(unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
3360edd16368SStephen M. Cameron 		/* Inquiry failed (msg printed already) */
3361edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev,
3362edd16368SStephen M. Cameron 			"hpsa_update_device_info: inquiry failed\n");
3363edd16368SStephen M. Cameron 		goto bail_out;
3364edd16368SStephen M. Cameron 	}
3365edd16368SStephen M. Cameron 
3366edd16368SStephen M. Cameron 	this_device->devtype = (inq_buff[0] & 0x1f);
3367edd16368SStephen M. Cameron 	memcpy(this_device->scsi3addr, scsi3addr, 8);
3368edd16368SStephen M. Cameron 	memcpy(this_device->vendor, &inq_buff[8],
3369edd16368SStephen M. Cameron 		sizeof(this_device->vendor));
3370edd16368SStephen M. Cameron 	memcpy(this_device->model, &inq_buff[16],
3371edd16368SStephen M. Cameron 		sizeof(this_device->model));
3372edd16368SStephen M. Cameron 	memset(this_device->device_id, 0,
3373edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
3374edd16368SStephen M. Cameron 	hpsa_get_device_id(h, scsi3addr, this_device->device_id,
3375edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
3376edd16368SStephen M. Cameron 
3377edd16368SStephen M. Cameron 	if (this_device->devtype == TYPE_DISK &&
3378283b4a9bSStephen M. Cameron 		is_logical_dev_addr_mode(scsi3addr)) {
337967955ba3SStephen M. Cameron 		int volume_offline;
338067955ba3SStephen M. Cameron 
3381edd16368SStephen M. Cameron 		hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
3382283b4a9bSStephen M. Cameron 		if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
3383283b4a9bSStephen M. Cameron 			hpsa_get_ioaccel_status(h, scsi3addr, this_device);
338467955ba3SStephen M. Cameron 		volume_offline = hpsa_volume_offline(h, scsi3addr);
338567955ba3SStephen M. Cameron 		if (volume_offline < 0 || volume_offline > 0xff)
338667955ba3SStephen M. Cameron 			volume_offline = HPSA_VPD_LV_STATUS_UNSUPPORTED;
338767955ba3SStephen M. Cameron 		this_device->volume_offline = volume_offline & 0xff;
3388283b4a9bSStephen M. Cameron 	} else {
3389edd16368SStephen M. Cameron 		this_device->raid_level = RAID_UNKNOWN;
3390283b4a9bSStephen M. Cameron 		this_device->offload_config = 0;
3391283b4a9bSStephen M. Cameron 		this_device->offload_enabled = 0;
339241ce4c35SStephen Cameron 		this_device->offload_to_be_enabled = 0;
3393a3144e0bSJoe Handzik 		this_device->hba_ioaccel_enabled = 0;
33949846590eSStephen M. Cameron 		this_device->volume_offline = 0;
339503383736SDon Brace 		this_device->queue_depth = h->nr_cmds;
3396283b4a9bSStephen M. Cameron 	}
3397edd16368SStephen M. Cameron 
33980b0e1d6cSStephen M. Cameron 	if (is_OBDR_device) {
33990b0e1d6cSStephen M. Cameron 		/* See if this is a One-Button-Disaster-Recovery device
34000b0e1d6cSStephen M. Cameron 		 * by looking for "$DR-10" at offset 43 in inquiry data.
34010b0e1d6cSStephen M. Cameron 		 */
34020b0e1d6cSStephen M. Cameron 		obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
34030b0e1d6cSStephen M. Cameron 		*is_OBDR_device = (this_device->devtype == TYPE_ROM &&
34040b0e1d6cSStephen M. Cameron 					strncmp(obdr_sig, OBDR_TAPE_SIG,
34050b0e1d6cSStephen M. Cameron 						OBDR_SIG_LEN) == 0);
34060b0e1d6cSStephen M. Cameron 	}
3407edd16368SStephen M. Cameron 	kfree(inq_buff);
3408edd16368SStephen M. Cameron 	return 0;
3409edd16368SStephen M. Cameron 
3410edd16368SStephen M. Cameron bail_out:
3411edd16368SStephen M. Cameron 	kfree(inq_buff);
3412edd16368SStephen M. Cameron 	return 1;
3413edd16368SStephen M. Cameron }
3414edd16368SStephen M. Cameron 
34159b5c48c2SStephen Cameron static void hpsa_update_device_supports_aborts(struct ctlr_info *h,
34169b5c48c2SStephen Cameron 			struct hpsa_scsi_dev_t *dev, u8 *scsi3addr)
34179b5c48c2SStephen Cameron {
34189b5c48c2SStephen Cameron 	unsigned long flags;
34199b5c48c2SStephen Cameron 	int rc, entry;
34209b5c48c2SStephen Cameron 	/*
34219b5c48c2SStephen Cameron 	 * See if this device supports aborts.  If we already know
34229b5c48c2SStephen Cameron 	 * the device, we already know if it supports aborts, otherwise
34239b5c48c2SStephen Cameron 	 * we have to find out if it supports aborts by trying one.
34249b5c48c2SStephen Cameron 	 */
34259b5c48c2SStephen Cameron 	spin_lock_irqsave(&h->devlock, flags);
34269b5c48c2SStephen Cameron 	rc = hpsa_scsi_find_entry(dev, h->dev, h->ndevices, &entry);
34279b5c48c2SStephen Cameron 	if ((rc == DEVICE_SAME || rc == DEVICE_UPDATED) &&
34289b5c48c2SStephen Cameron 		entry >= 0 && entry < h->ndevices) {
34299b5c48c2SStephen Cameron 		dev->supports_aborts = h->dev[entry]->supports_aborts;
34309b5c48c2SStephen Cameron 		spin_unlock_irqrestore(&h->devlock, flags);
34319b5c48c2SStephen Cameron 	} else {
34329b5c48c2SStephen Cameron 		spin_unlock_irqrestore(&h->devlock, flags);
34339b5c48c2SStephen Cameron 		dev->supports_aborts =
34349b5c48c2SStephen Cameron 				hpsa_device_supports_aborts(h, scsi3addr);
34359b5c48c2SStephen Cameron 		if (dev->supports_aborts < 0)
34369b5c48c2SStephen Cameron 			dev->supports_aborts = 0;
34379b5c48c2SStephen Cameron 	}
34389b5c48c2SStephen Cameron }
34399b5c48c2SStephen Cameron 
34404f4eb9f1SScott Teel static unsigned char *ext_target_model[] = {
3441edd16368SStephen M. Cameron 	"MSA2012",
3442edd16368SStephen M. Cameron 	"MSA2024",
3443edd16368SStephen M. Cameron 	"MSA2312",
3444edd16368SStephen M. Cameron 	"MSA2324",
3445fda38518SStephen M. Cameron 	"P2000 G3 SAS",
3446e06c8e5cSStephen M. Cameron 	"MSA 2040 SAS",
3447edd16368SStephen M. Cameron 	NULL,
3448edd16368SStephen M. Cameron };
3449edd16368SStephen M. Cameron 
34504f4eb9f1SScott Teel static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
3451edd16368SStephen M. Cameron {
3452edd16368SStephen M. Cameron 	int i;
3453edd16368SStephen M. Cameron 
34544f4eb9f1SScott Teel 	for (i = 0; ext_target_model[i]; i++)
34554f4eb9f1SScott Teel 		if (strncmp(device->model, ext_target_model[i],
34564f4eb9f1SScott Teel 			strlen(ext_target_model[i])) == 0)
3457edd16368SStephen M. Cameron 			return 1;
3458edd16368SStephen M. Cameron 	return 0;
3459edd16368SStephen M. Cameron }
3460edd16368SStephen M. Cameron 
3461edd16368SStephen M. Cameron /* Helper function to assign bus, target, lun mapping of devices.
34624f4eb9f1SScott Teel  * Puts non-external target logical volumes on bus 0, external target logical
3463edd16368SStephen M. Cameron  * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
3464edd16368SStephen M. Cameron  * Logical drive target and lun are assigned at this time, but
3465edd16368SStephen M. Cameron  * physical device lun and target assignment are deferred (assigned
3466edd16368SStephen M. Cameron  * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
3467edd16368SStephen M. Cameron  */
3468edd16368SStephen M. Cameron static void figure_bus_target_lun(struct ctlr_info *h,
34691f310bdeSStephen M. Cameron 	u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
3470edd16368SStephen M. Cameron {
34711f310bdeSStephen M. Cameron 	u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
3472edd16368SStephen M. Cameron 
34731f310bdeSStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes)) {
34741f310bdeSStephen M. Cameron 		/* physical device, target and lun filled in later */
34751f310bdeSStephen M. Cameron 		if (is_hba_lunid(lunaddrbytes))
34761f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
34771f310bdeSStephen M. Cameron 		else
34781f310bdeSStephen M. Cameron 			/* defer target, lun assignment for physical devices */
34791f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 2, -1, -1);
34801f310bdeSStephen M. Cameron 		return;
34811f310bdeSStephen M. Cameron 	}
34821f310bdeSStephen M. Cameron 	/* It's a logical device */
34834f4eb9f1SScott Teel 	if (is_ext_target(h, device)) {
34844f4eb9f1SScott Teel 		/* external target way, put logicals on bus 1
3485339b2b14SStephen M. Cameron 		 * and match target/lun numbers box
34861f310bdeSStephen M. Cameron 		 * reports, other smart array, bus 0, target 0, match lunid
3487339b2b14SStephen M. Cameron 		 */
34881f310bdeSStephen M. Cameron 		hpsa_set_bus_target_lun(device,
34891f310bdeSStephen M. Cameron 			1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
34901f310bdeSStephen M. Cameron 		return;
3491339b2b14SStephen M. Cameron 	}
34921f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
3493edd16368SStephen M. Cameron }
3494edd16368SStephen M. Cameron 
3495edd16368SStephen M. Cameron /*
3496edd16368SStephen M. Cameron  * If there is no lun 0 on a target, linux won't find any devices.
34974f4eb9f1SScott Teel  * For the external targets (arrays), we have to manually detect the enclosure
3498edd16368SStephen M. Cameron  * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
3499edd16368SStephen M. Cameron  * it for some reason.  *tmpdevice is the target we're adding,
3500edd16368SStephen M. Cameron  * this_device is a pointer into the current element of currentsd[]
3501edd16368SStephen M. Cameron  * that we're building up in update_scsi_devices(), below.
3502edd16368SStephen M. Cameron  * lunzerobits is a bitmap that tracks which targets already have a
3503edd16368SStephen M. Cameron  * lun 0 assigned.
3504edd16368SStephen M. Cameron  * Returns 1 if an enclosure was added, 0 if not.
3505edd16368SStephen M. Cameron  */
35064f4eb9f1SScott Teel static int add_ext_target_dev(struct ctlr_info *h,
3507edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *tmpdevice,
350801a02ffcSStephen M. Cameron 	struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
35094f4eb9f1SScott Teel 	unsigned long lunzerobits[], int *n_ext_target_devs)
3510edd16368SStephen M. Cameron {
3511edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
3512edd16368SStephen M. Cameron 
35131f310bdeSStephen M. Cameron 	if (test_bit(tmpdevice->target, lunzerobits))
3514edd16368SStephen M. Cameron 		return 0; /* There is already a lun 0 on this target. */
3515edd16368SStephen M. Cameron 
3516edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes))
3517edd16368SStephen M. Cameron 		return 0; /* It's the logical targets that may lack lun 0. */
3518edd16368SStephen M. Cameron 
35194f4eb9f1SScott Teel 	if (!is_ext_target(h, tmpdevice))
35204f4eb9f1SScott Teel 		return 0; /* Only external target devices have this problem. */
3521edd16368SStephen M. Cameron 
35221f310bdeSStephen M. Cameron 	if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
3523edd16368SStephen M. Cameron 		return 0;
3524edd16368SStephen M. Cameron 
3525c4f8a299SStephen M. Cameron 	memset(scsi3addr, 0, 8);
35261f310bdeSStephen M. Cameron 	scsi3addr[3] = tmpdevice->target;
3527edd16368SStephen M. Cameron 	if (is_hba_lunid(scsi3addr))
3528edd16368SStephen M. Cameron 		return 0; /* Don't add the RAID controller here. */
3529edd16368SStephen M. Cameron 
3530339b2b14SStephen M. Cameron 	if (is_scsi_rev_5(h))
3531339b2b14SStephen M. Cameron 		return 0; /* p1210m doesn't need to do this. */
3532339b2b14SStephen M. Cameron 
35334f4eb9f1SScott Teel 	if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
3534aca4a520SScott Teel 		dev_warn(&h->pdev->dev, "Maximum number of external "
3535aca4a520SScott Teel 			"target devices exceeded.  Check your hardware "
3536edd16368SStephen M. Cameron 			"configuration.");
3537edd16368SStephen M. Cameron 		return 0;
3538edd16368SStephen M. Cameron 	}
3539edd16368SStephen M. Cameron 
35400b0e1d6cSStephen M. Cameron 	if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
3541edd16368SStephen M. Cameron 		return 0;
35424f4eb9f1SScott Teel 	(*n_ext_target_devs)++;
35431f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(this_device,
35441f310bdeSStephen M. Cameron 				tmpdevice->bus, tmpdevice->target, 0);
35459b5c48c2SStephen Cameron 	hpsa_update_device_supports_aborts(h, this_device, scsi3addr);
35461f310bdeSStephen M. Cameron 	set_bit(tmpdevice->target, lunzerobits);
3547edd16368SStephen M. Cameron 	return 1;
3548edd16368SStephen M. Cameron }
3549edd16368SStephen M. Cameron 
3550edd16368SStephen M. Cameron /*
355154b6e9e9SScott Teel  * Get address of physical disk used for an ioaccel2 mode command:
355254b6e9e9SScott Teel  *	1. Extract ioaccel2 handle from the command.
355354b6e9e9SScott Teel  *	2. Find a matching ioaccel2 handle from list of physical disks.
355454b6e9e9SScott Teel  *	3. Return:
355554b6e9e9SScott Teel  *		1 and set scsi3addr to address of matching physical
355654b6e9e9SScott Teel  *		0 if no matching physical disk was found.
355754b6e9e9SScott Teel  */
355854b6e9e9SScott Teel static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
355954b6e9e9SScott Teel 	struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
356054b6e9e9SScott Teel {
356141ce4c35SStephen Cameron 	struct io_accel2_cmd *c2 =
356241ce4c35SStephen Cameron 			&h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
356341ce4c35SStephen Cameron 	unsigned long flags;
356454b6e9e9SScott Teel 	int i;
356554b6e9e9SScott Teel 
356641ce4c35SStephen Cameron 	spin_lock_irqsave(&h->devlock, flags);
356741ce4c35SStephen Cameron 	for (i = 0; i < h->ndevices; i++)
356841ce4c35SStephen Cameron 		if (h->dev[i]->ioaccel_handle == le32_to_cpu(c2->scsi_nexus)) {
356941ce4c35SStephen Cameron 			memcpy(scsi3addr, h->dev[i]->scsi3addr,
357041ce4c35SStephen Cameron 				sizeof(h->dev[i]->scsi3addr));
357141ce4c35SStephen Cameron 			spin_unlock_irqrestore(&h->devlock, flags);
357254b6e9e9SScott Teel 			return 1;
357354b6e9e9SScott Teel 		}
357441ce4c35SStephen Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
357541ce4c35SStephen Cameron 	return 0;
357641ce4c35SStephen Cameron }
357741ce4c35SStephen Cameron 
357854b6e9e9SScott Teel /*
3579edd16368SStephen M. Cameron  * Do CISS_REPORT_PHYS and CISS_REPORT_LOG.  Data is returned in physdev,
3580edd16368SStephen M. Cameron  * logdev.  The number of luns in physdev and logdev are returned in
3581edd16368SStephen M. Cameron  * *nphysicals and *nlogicals, respectively.
3582edd16368SStephen M. Cameron  * Returns 0 on success, -1 otherwise.
3583edd16368SStephen M. Cameron  */
3584edd16368SStephen M. Cameron static int hpsa_gather_lun_info(struct ctlr_info *h,
358503383736SDon Brace 	struct ReportExtendedLUNdata *physdev, u32 *nphysicals,
358601a02ffcSStephen M. Cameron 	struct ReportLUNdata *logdev, u32 *nlogicals)
3587edd16368SStephen M. Cameron {
358803383736SDon Brace 	if (hpsa_scsi_do_report_phys_luns(h, physdev, sizeof(*physdev))) {
3589edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
3590edd16368SStephen M. Cameron 		return -1;
3591edd16368SStephen M. Cameron 	}
359203383736SDon Brace 	*nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 24;
3593edd16368SStephen M. Cameron 	if (*nphysicals > HPSA_MAX_PHYS_LUN) {
359403383736SDon Brace 		dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded. %d LUNs ignored.\n",
359503383736SDon Brace 			HPSA_MAX_PHYS_LUN, *nphysicals - HPSA_MAX_PHYS_LUN);
3596edd16368SStephen M. Cameron 		*nphysicals = HPSA_MAX_PHYS_LUN;
3597edd16368SStephen M. Cameron 	}
359803383736SDon Brace 	if (hpsa_scsi_do_report_log_luns(h, logdev, sizeof(*logdev))) {
3599edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
3600edd16368SStephen M. Cameron 		return -1;
3601edd16368SStephen M. Cameron 	}
36026df1e954SStephen M. Cameron 	*nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
3603edd16368SStephen M. Cameron 	/* Reject Logicals in excess of our max capability. */
3604edd16368SStephen M. Cameron 	if (*nlogicals > HPSA_MAX_LUN) {
3605edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
3606edd16368SStephen M. Cameron 			"maximum logical LUNs (%d) exceeded.  "
3607edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_LUN,
3608edd16368SStephen M. Cameron 			*nlogicals - HPSA_MAX_LUN);
3609edd16368SStephen M. Cameron 			*nlogicals = HPSA_MAX_LUN;
3610edd16368SStephen M. Cameron 	}
3611edd16368SStephen M. Cameron 	if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
3612edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
3613edd16368SStephen M. Cameron 			"maximum logical + physical LUNs (%d) exceeded. "
3614edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
3615edd16368SStephen M. Cameron 			*nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
3616edd16368SStephen M. Cameron 		*nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
3617edd16368SStephen M. Cameron 	}
3618edd16368SStephen M. Cameron 	return 0;
3619edd16368SStephen M. Cameron }
3620edd16368SStephen M. Cameron 
362142a91641SDon Brace static u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position,
362242a91641SDon Brace 	int i, int nphysicals, int nlogicals,
3623a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list,
3624339b2b14SStephen M. Cameron 	struct ReportLUNdata *logdev_list)
3625339b2b14SStephen M. Cameron {
3626339b2b14SStephen M. Cameron 	/* Helper function, figure out where the LUN ID info is coming from
3627339b2b14SStephen M. Cameron 	 * given index i, lists of physical and logical devices, where in
3628339b2b14SStephen M. Cameron 	 * the list the raid controller is supposed to appear (first or last)
3629339b2b14SStephen M. Cameron 	 */
3630339b2b14SStephen M. Cameron 
3631339b2b14SStephen M. Cameron 	int logicals_start = nphysicals + (raid_ctlr_position == 0);
3632339b2b14SStephen M. Cameron 	int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
3633339b2b14SStephen M. Cameron 
3634339b2b14SStephen M. Cameron 	if (i == raid_ctlr_position)
3635339b2b14SStephen M. Cameron 		return RAID_CTLR_LUNID;
3636339b2b14SStephen M. Cameron 
3637339b2b14SStephen M. Cameron 	if (i < logicals_start)
3638d5b5d964SStephen M. Cameron 		return &physdev_list->LUN[i -
3639d5b5d964SStephen M. Cameron 				(raid_ctlr_position == 0)].lunid[0];
3640339b2b14SStephen M. Cameron 
3641339b2b14SStephen M. Cameron 	if (i < last_device)
3642339b2b14SStephen M. Cameron 		return &logdev_list->LUN[i - nphysicals -
3643339b2b14SStephen M. Cameron 			(raid_ctlr_position == 0)][0];
3644339b2b14SStephen M. Cameron 	BUG();
3645339b2b14SStephen M. Cameron 	return NULL;
3646339b2b14SStephen M. Cameron }
3647339b2b14SStephen M. Cameron 
364803383736SDon Brace /* get physical drive ioaccel handle and queue depth */
364903383736SDon Brace static void hpsa_get_ioaccel_drive_info(struct ctlr_info *h,
365003383736SDon Brace 		struct hpsa_scsi_dev_t *dev,
365103383736SDon Brace 		u8 *lunaddrbytes,
365203383736SDon Brace 		struct bmic_identify_physical_device *id_phys)
365303383736SDon Brace {
365403383736SDon Brace 	int rc;
365503383736SDon Brace 	struct ext_report_lun_entry *rle =
365603383736SDon Brace 		(struct ext_report_lun_entry *) lunaddrbytes;
365703383736SDon Brace 
365803383736SDon Brace 	dev->ioaccel_handle = rle->ioaccel_handle;
3659a3144e0bSJoe Handzik 	if (PHYS_IOACCEL(lunaddrbytes) && dev->ioaccel_handle)
3660a3144e0bSJoe Handzik 		dev->hba_ioaccel_enabled = 1;
366103383736SDon Brace 	memset(id_phys, 0, sizeof(*id_phys));
366203383736SDon Brace 	rc = hpsa_bmic_id_physical_device(h, lunaddrbytes,
366303383736SDon Brace 			GET_BMIC_DRIVE_NUMBER(lunaddrbytes), id_phys,
366403383736SDon Brace 			sizeof(*id_phys));
366503383736SDon Brace 	if (!rc)
366603383736SDon Brace 		/* Reserve space for FW operations */
366703383736SDon Brace #define DRIVE_CMDS_RESERVED_FOR_FW 2
366803383736SDon Brace #define DRIVE_QUEUE_DEPTH 7
366903383736SDon Brace 		dev->queue_depth =
367003383736SDon Brace 			le16_to_cpu(id_phys->current_queue_depth_limit) -
367103383736SDon Brace 				DRIVE_CMDS_RESERVED_FOR_FW;
367203383736SDon Brace 	else
367303383736SDon Brace 		dev->queue_depth = DRIVE_QUEUE_DEPTH; /* conservative */
367403383736SDon Brace 	atomic_set(&dev->ioaccel_cmds_out, 0);
3675d604f533SWebb Scales 	atomic_set(&dev->reset_cmds_out, 0);
367603383736SDon Brace }
367703383736SDon Brace 
36788270b862SJoe Handzik static void hpsa_get_path_info(struct hpsa_scsi_dev_t *this_device,
36798270b862SJoe Handzik 	u8 *lunaddrbytes,
36808270b862SJoe Handzik 	struct bmic_identify_physical_device *id_phys)
36818270b862SJoe Handzik {
36828270b862SJoe Handzik 	if (PHYS_IOACCEL(lunaddrbytes)
36838270b862SJoe Handzik 		&& this_device->ioaccel_handle)
36848270b862SJoe Handzik 		this_device->hba_ioaccel_enabled = 1;
36858270b862SJoe Handzik 
36868270b862SJoe Handzik 	memcpy(&this_device->active_path_index,
36878270b862SJoe Handzik 		&id_phys->active_path_number,
36888270b862SJoe Handzik 		sizeof(this_device->active_path_index));
36898270b862SJoe Handzik 	memcpy(&this_device->path_map,
36908270b862SJoe Handzik 		&id_phys->redundant_path_present_map,
36918270b862SJoe Handzik 		sizeof(this_device->path_map));
36928270b862SJoe Handzik 	memcpy(&this_device->box,
36938270b862SJoe Handzik 		&id_phys->alternate_paths_phys_box_on_port,
36948270b862SJoe Handzik 		sizeof(this_device->box));
36958270b862SJoe Handzik 	memcpy(&this_device->phys_connector,
36968270b862SJoe Handzik 		&id_phys->alternate_paths_phys_connector,
36978270b862SJoe Handzik 		sizeof(this_device->phys_connector));
36988270b862SJoe Handzik 	memcpy(&this_device->bay,
36998270b862SJoe Handzik 		&id_phys->phys_bay_in_box,
37008270b862SJoe Handzik 		sizeof(this_device->bay));
37018270b862SJoe Handzik }
37028270b862SJoe Handzik 
3703edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
3704edd16368SStephen M. Cameron {
3705edd16368SStephen M. Cameron 	/* the idea here is we could get notified
3706edd16368SStephen M. Cameron 	 * that some devices have changed, so we do a report
3707edd16368SStephen M. Cameron 	 * physical luns and report logical luns cmd, and adjust
3708edd16368SStephen M. Cameron 	 * our list of devices accordingly.
3709edd16368SStephen M. Cameron 	 *
3710edd16368SStephen M. Cameron 	 * The scsi3addr's of devices won't change so long as the
3711edd16368SStephen M. Cameron 	 * adapter is not reset.  That means we can rescan and
3712edd16368SStephen M. Cameron 	 * tell which devices we already know about, vs. new
3713edd16368SStephen M. Cameron 	 * devices, vs.  disappearing devices.
3714edd16368SStephen M. Cameron 	 */
3715a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list = NULL;
3716edd16368SStephen M. Cameron 	struct ReportLUNdata *logdev_list = NULL;
371703383736SDon Brace 	struct bmic_identify_physical_device *id_phys = NULL;
371801a02ffcSStephen M. Cameron 	u32 nphysicals = 0;
371901a02ffcSStephen M. Cameron 	u32 nlogicals = 0;
372001a02ffcSStephen M. Cameron 	u32 ndev_allocated = 0;
3721edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
3722edd16368SStephen M. Cameron 	int ncurrent = 0;
37234f4eb9f1SScott Teel 	int i, n_ext_target_devs, ndevs_to_allocate;
3724339b2b14SStephen M. Cameron 	int raid_ctlr_position;
3725aca4a520SScott Teel 	DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
3726edd16368SStephen M. Cameron 
3727cfe5badcSScott Teel 	currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
372892084715SStephen M. Cameron 	physdev_list = kzalloc(sizeof(*physdev_list), GFP_KERNEL);
372992084715SStephen M. Cameron 	logdev_list = kzalloc(sizeof(*logdev_list), GFP_KERNEL);
3730edd16368SStephen M. Cameron 	tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
373103383736SDon Brace 	id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
3732edd16368SStephen M. Cameron 
373303383736SDon Brace 	if (!currentsd || !physdev_list || !logdev_list ||
373403383736SDon Brace 		!tmpdevice || !id_phys) {
3735edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory\n");
3736edd16368SStephen M. Cameron 		goto out;
3737edd16368SStephen M. Cameron 	}
3738edd16368SStephen M. Cameron 	memset(lunzerobits, 0, sizeof(lunzerobits));
3739edd16368SStephen M. Cameron 
374003383736SDon Brace 	if (hpsa_gather_lun_info(h, physdev_list, &nphysicals,
374103383736SDon Brace 			logdev_list, &nlogicals))
3742edd16368SStephen M. Cameron 		goto out;
3743edd16368SStephen M. Cameron 
3744aca4a520SScott Teel 	/* We might see up to the maximum number of logical and physical disks
3745aca4a520SScott Teel 	 * plus external target devices, and a device for the local RAID
3746aca4a520SScott Teel 	 * controller.
3747edd16368SStephen M. Cameron 	 */
3748aca4a520SScott Teel 	ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
3749edd16368SStephen M. Cameron 
3750edd16368SStephen M. Cameron 	/* Allocate the per device structures */
3751edd16368SStephen M. Cameron 	for (i = 0; i < ndevs_to_allocate; i++) {
3752b7ec021fSScott Teel 		if (i >= HPSA_MAX_DEVICES) {
3753b7ec021fSScott Teel 			dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
3754b7ec021fSScott Teel 				"  %d devices ignored.\n", HPSA_MAX_DEVICES,
3755b7ec021fSScott Teel 				ndevs_to_allocate - HPSA_MAX_DEVICES);
3756b7ec021fSScott Teel 			break;
3757b7ec021fSScott Teel 		}
3758b7ec021fSScott Teel 
3759edd16368SStephen M. Cameron 		currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
3760edd16368SStephen M. Cameron 		if (!currentsd[i]) {
3761edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
3762edd16368SStephen M. Cameron 				__FILE__, __LINE__);
3763edd16368SStephen M. Cameron 			goto out;
3764edd16368SStephen M. Cameron 		}
3765edd16368SStephen M. Cameron 		ndev_allocated++;
3766edd16368SStephen M. Cameron 	}
3767edd16368SStephen M. Cameron 
37688645291bSStephen M. Cameron 	if (is_scsi_rev_5(h))
3769339b2b14SStephen M. Cameron 		raid_ctlr_position = 0;
3770339b2b14SStephen M. Cameron 	else
3771339b2b14SStephen M. Cameron 		raid_ctlr_position = nphysicals + nlogicals;
3772339b2b14SStephen M. Cameron 
3773edd16368SStephen M. Cameron 	/* adjust our table of devices */
37744f4eb9f1SScott Teel 	n_ext_target_devs = 0;
3775edd16368SStephen M. Cameron 	for (i = 0; i < nphysicals + nlogicals + 1; i++) {
37760b0e1d6cSStephen M. Cameron 		u8 *lunaddrbytes, is_OBDR = 0;
3777edd16368SStephen M. Cameron 
3778edd16368SStephen M. Cameron 		/* Figure out where the LUN ID info is coming from */
3779339b2b14SStephen M. Cameron 		lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
3780339b2b14SStephen M. Cameron 			i, nphysicals, nlogicals, physdev_list, logdev_list);
378141ce4c35SStephen Cameron 
378241ce4c35SStephen Cameron 		/* skip masked non-disk devices */
378341ce4c35SStephen Cameron 		if (MASKED_DEVICE(lunaddrbytes))
378441ce4c35SStephen Cameron 			if (i < nphysicals + (raid_ctlr_position == 0) &&
378541ce4c35SStephen Cameron 				NON_DISK_PHYS_DEV(lunaddrbytes))
3786edd16368SStephen M. Cameron 				continue;
3787edd16368SStephen M. Cameron 
3788edd16368SStephen M. Cameron 		/* Get device type, vendor, model, device id */
37890b0e1d6cSStephen M. Cameron 		if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
37900b0e1d6cSStephen M. Cameron 							&is_OBDR))
3791edd16368SStephen M. Cameron 			continue; /* skip it if we can't talk to it. */
37921f310bdeSStephen M. Cameron 		figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
37939b5c48c2SStephen Cameron 		hpsa_update_device_supports_aborts(h, tmpdevice, lunaddrbytes);
3794edd16368SStephen M. Cameron 		this_device = currentsd[ncurrent];
3795edd16368SStephen M. Cameron 
3796edd16368SStephen M. Cameron 		/*
37974f4eb9f1SScott Teel 		 * For external target devices, we have to insert a LUN 0 which
3798edd16368SStephen M. Cameron 		 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
3799edd16368SStephen M. Cameron 		 * is nonetheless an enclosure device there.  We have to
3800edd16368SStephen M. Cameron 		 * present that otherwise linux won't find anything if
3801edd16368SStephen M. Cameron 		 * there is no lun 0.
3802edd16368SStephen M. Cameron 		 */
38034f4eb9f1SScott Teel 		if (add_ext_target_dev(h, tmpdevice, this_device,
38041f310bdeSStephen M. Cameron 				lunaddrbytes, lunzerobits,
38054f4eb9f1SScott Teel 				&n_ext_target_devs)) {
3806edd16368SStephen M. Cameron 			ncurrent++;
3807edd16368SStephen M. Cameron 			this_device = currentsd[ncurrent];
3808edd16368SStephen M. Cameron 		}
3809edd16368SStephen M. Cameron 
3810edd16368SStephen M. Cameron 		*this_device = *tmpdevice;
3811edd16368SStephen M. Cameron 
381241ce4c35SStephen Cameron 		/* do not expose masked devices */
381341ce4c35SStephen Cameron 		if (MASKED_DEVICE(lunaddrbytes) &&
381441ce4c35SStephen Cameron 			i < nphysicals + (raid_ctlr_position == 0)) {
381541ce4c35SStephen Cameron 			this_device->expose_state = HPSA_DO_NOT_EXPOSE;
381641ce4c35SStephen Cameron 		} else {
381741ce4c35SStephen Cameron 			this_device->expose_state =
381841ce4c35SStephen Cameron 					HPSA_SG_ATTACH | HPSA_ULD_ATTACH;
381941ce4c35SStephen Cameron 		}
382041ce4c35SStephen Cameron 
3821edd16368SStephen M. Cameron 		switch (this_device->devtype) {
38220b0e1d6cSStephen M. Cameron 		case TYPE_ROM:
3823edd16368SStephen M. Cameron 			/* We don't *really* support actual CD-ROM devices,
3824edd16368SStephen M. Cameron 			 * just "One Button Disaster Recovery" tape drive
3825edd16368SStephen M. Cameron 			 * which temporarily pretends to be a CD-ROM drive.
3826edd16368SStephen M. Cameron 			 * So we check that the device is really an OBDR tape
3827edd16368SStephen M. Cameron 			 * device by checking for "$DR-10" in bytes 43-48 of
3828edd16368SStephen M. Cameron 			 * the inquiry data.
3829edd16368SStephen M. Cameron 			 */
38300b0e1d6cSStephen M. Cameron 			if (is_OBDR)
3831edd16368SStephen M. Cameron 				ncurrent++;
3832edd16368SStephen M. Cameron 			break;
3833edd16368SStephen M. Cameron 		case TYPE_DISK:
3834b9092b79SKevin Barnett 			if (i < nphysicals + (raid_ctlr_position == 0)) {
3835b9092b79SKevin Barnett 				/* The disk is in HBA mode. */
3836b9092b79SKevin Barnett 				/* Never use RAID mapper in HBA mode. */
3837ecf418d1SJoe Handzik 				this_device->offload_enabled = 0;
383803383736SDon Brace 				hpsa_get_ioaccel_drive_info(h, this_device,
383903383736SDon Brace 					lunaddrbytes, id_phys);
3840b9092b79SKevin Barnett 				hpsa_get_path_info(this_device, lunaddrbytes,
3841b9092b79SKevin Barnett 							id_phys);
3842b9092b79SKevin Barnett 			}
3843edd16368SStephen M. Cameron 			ncurrent++;
3844edd16368SStephen M. Cameron 			break;
3845edd16368SStephen M. Cameron 		case TYPE_TAPE:
3846edd16368SStephen M. Cameron 		case TYPE_MEDIUM_CHANGER:
384741ce4c35SStephen Cameron 		case TYPE_ENCLOSURE:
384841ce4c35SStephen Cameron 			ncurrent++;
384941ce4c35SStephen Cameron 			break;
3850edd16368SStephen M. Cameron 		case TYPE_RAID:
3851edd16368SStephen M. Cameron 			/* Only present the Smartarray HBA as a RAID controller.
3852edd16368SStephen M. Cameron 			 * If it's a RAID controller other than the HBA itself
3853edd16368SStephen M. Cameron 			 * (an external RAID controller, MSA500 or similar)
3854edd16368SStephen M. Cameron 			 * don't present it.
3855edd16368SStephen M. Cameron 			 */
3856edd16368SStephen M. Cameron 			if (!is_hba_lunid(lunaddrbytes))
3857edd16368SStephen M. Cameron 				break;
3858edd16368SStephen M. Cameron 			ncurrent++;
3859edd16368SStephen M. Cameron 			break;
3860edd16368SStephen M. Cameron 		default:
3861edd16368SStephen M. Cameron 			break;
3862edd16368SStephen M. Cameron 		}
3863cfe5badcSScott Teel 		if (ncurrent >= HPSA_MAX_DEVICES)
3864edd16368SStephen M. Cameron 			break;
3865edd16368SStephen M. Cameron 	}
3866edd16368SStephen M. Cameron 	adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
3867edd16368SStephen M. Cameron out:
3868edd16368SStephen M. Cameron 	kfree(tmpdevice);
3869edd16368SStephen M. Cameron 	for (i = 0; i < ndev_allocated; i++)
3870edd16368SStephen M. Cameron 		kfree(currentsd[i]);
3871edd16368SStephen M. Cameron 	kfree(currentsd);
3872edd16368SStephen M. Cameron 	kfree(physdev_list);
3873edd16368SStephen M. Cameron 	kfree(logdev_list);
387403383736SDon Brace 	kfree(id_phys);
3875edd16368SStephen M. Cameron }
3876edd16368SStephen M. Cameron 
3877ec5cbf04SWebb Scales static void hpsa_set_sg_descriptor(struct SGDescriptor *desc,
3878ec5cbf04SWebb Scales 				   struct scatterlist *sg)
3879ec5cbf04SWebb Scales {
3880ec5cbf04SWebb Scales 	u64 addr64 = (u64) sg_dma_address(sg);
3881ec5cbf04SWebb Scales 	unsigned int len = sg_dma_len(sg);
3882ec5cbf04SWebb Scales 
3883ec5cbf04SWebb Scales 	desc->Addr = cpu_to_le64(addr64);
3884ec5cbf04SWebb Scales 	desc->Len = cpu_to_le32(len);
3885ec5cbf04SWebb Scales 	desc->Ext = 0;
3886ec5cbf04SWebb Scales }
3887ec5cbf04SWebb Scales 
3888c7ee65b3SWebb Scales /*
3889c7ee65b3SWebb Scales  * hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
3890edd16368SStephen M. Cameron  * dma mapping  and fills in the scatter gather entries of the
3891edd16368SStephen M. Cameron  * hpsa command, cp.
3892edd16368SStephen M. Cameron  */
389333a2ffceSStephen M. Cameron static int hpsa_scatter_gather(struct ctlr_info *h,
3894edd16368SStephen M. Cameron 		struct CommandList *cp,
3895edd16368SStephen M. Cameron 		struct scsi_cmnd *cmd)
3896edd16368SStephen M. Cameron {
3897edd16368SStephen M. Cameron 	struct scatterlist *sg;
3898b3a7ba7cSWebb Scales 	int use_sg, i, sg_limit, chained, last_sg;
389933a2ffceSStephen M. Cameron 	struct SGDescriptor *curr_sg;
3900edd16368SStephen M. Cameron 
390133a2ffceSStephen M. Cameron 	BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
3902edd16368SStephen M. Cameron 
3903edd16368SStephen M. Cameron 	use_sg = scsi_dma_map(cmd);
3904edd16368SStephen M. Cameron 	if (use_sg < 0)
3905edd16368SStephen M. Cameron 		return use_sg;
3906edd16368SStephen M. Cameron 
3907edd16368SStephen M. Cameron 	if (!use_sg)
3908edd16368SStephen M. Cameron 		goto sglist_finished;
3909edd16368SStephen M. Cameron 
3910b3a7ba7cSWebb Scales 	/*
3911b3a7ba7cSWebb Scales 	 * If the number of entries is greater than the max for a single list,
3912b3a7ba7cSWebb Scales 	 * then we have a chained list; we will set up all but one entry in the
3913b3a7ba7cSWebb Scales 	 * first list (the last entry is saved for link information);
3914b3a7ba7cSWebb Scales 	 * otherwise, we don't have a chained list and we'll set up at each of
3915b3a7ba7cSWebb Scales 	 * the entries in the one list.
3916b3a7ba7cSWebb Scales 	 */
391733a2ffceSStephen M. Cameron 	curr_sg = cp->SG;
3918b3a7ba7cSWebb Scales 	chained = use_sg > h->max_cmd_sg_entries;
3919b3a7ba7cSWebb Scales 	sg_limit = chained ? h->max_cmd_sg_entries - 1 : use_sg;
3920b3a7ba7cSWebb Scales 	last_sg = scsi_sg_count(cmd) - 1;
3921b3a7ba7cSWebb Scales 	scsi_for_each_sg(cmd, sg, sg_limit, i) {
3922ec5cbf04SWebb Scales 		hpsa_set_sg_descriptor(curr_sg, sg);
392333a2ffceSStephen M. Cameron 		curr_sg++;
392433a2ffceSStephen M. Cameron 	}
3925ec5cbf04SWebb Scales 
3926b3a7ba7cSWebb Scales 	if (chained) {
3927b3a7ba7cSWebb Scales 		/*
3928b3a7ba7cSWebb Scales 		 * Continue with the chained list.  Set curr_sg to the chained
3929b3a7ba7cSWebb Scales 		 * list.  Modify the limit to the total count less the entries
3930b3a7ba7cSWebb Scales 		 * we've already set up.  Resume the scan at the list entry
3931b3a7ba7cSWebb Scales 		 * where the previous loop left off.
3932b3a7ba7cSWebb Scales 		 */
3933b3a7ba7cSWebb Scales 		curr_sg = h->cmd_sg_list[cp->cmdindex];
3934b3a7ba7cSWebb Scales 		sg_limit = use_sg - sg_limit;
3935b3a7ba7cSWebb Scales 		for_each_sg(sg, sg, sg_limit, i) {
3936b3a7ba7cSWebb Scales 			hpsa_set_sg_descriptor(curr_sg, sg);
3937b3a7ba7cSWebb Scales 			curr_sg++;
3938b3a7ba7cSWebb Scales 		}
3939b3a7ba7cSWebb Scales 	}
3940b3a7ba7cSWebb Scales 
3941ec5cbf04SWebb Scales 	/* Back the pointer up to the last entry and mark it as "last". */
3942b3a7ba7cSWebb Scales 	(curr_sg - 1)->Ext = cpu_to_le32(HPSA_SG_LAST);
394333a2ffceSStephen M. Cameron 
394433a2ffceSStephen M. Cameron 	if (use_sg + chained > h->maxSG)
394533a2ffceSStephen M. Cameron 		h->maxSG = use_sg + chained;
394633a2ffceSStephen M. Cameron 
394733a2ffceSStephen M. Cameron 	if (chained) {
394833a2ffceSStephen M. Cameron 		cp->Header.SGList = h->max_cmd_sg_entries;
394950a0decfSStephen M. Cameron 		cp->Header.SGTotal = cpu_to_le16(use_sg + 1);
3950e2bea6dfSStephen M. Cameron 		if (hpsa_map_sg_chain_block(h, cp)) {
3951e2bea6dfSStephen M. Cameron 			scsi_dma_unmap(cmd);
3952e2bea6dfSStephen M. Cameron 			return -1;
3953e2bea6dfSStephen M. Cameron 		}
395433a2ffceSStephen M. Cameron 		return 0;
3955edd16368SStephen M. Cameron 	}
3956edd16368SStephen M. Cameron 
3957edd16368SStephen M. Cameron sglist_finished:
3958edd16368SStephen M. Cameron 
395901a02ffcSStephen M. Cameron 	cp->Header.SGList = (u8) use_sg;   /* no. SGs contig in this cmd */
3960c7ee65b3SWebb Scales 	cp->Header.SGTotal = cpu_to_le16(use_sg); /* total sgs in cmd list */
3961edd16368SStephen M. Cameron 	return 0;
3962edd16368SStephen M. Cameron }
3963edd16368SStephen M. Cameron 
3964283b4a9bSStephen M. Cameron #define IO_ACCEL_INELIGIBLE (1)
3965283b4a9bSStephen M. Cameron static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
3966283b4a9bSStephen M. Cameron {
3967283b4a9bSStephen M. Cameron 	int is_write = 0;
3968283b4a9bSStephen M. Cameron 	u32 block;
3969283b4a9bSStephen M. Cameron 	u32 block_cnt;
3970283b4a9bSStephen M. Cameron 
3971283b4a9bSStephen M. Cameron 	/* Perform some CDB fixups if needed using 10 byte reads/writes only */
3972283b4a9bSStephen M. Cameron 	switch (cdb[0]) {
3973283b4a9bSStephen M. Cameron 	case WRITE_6:
3974283b4a9bSStephen M. Cameron 	case WRITE_12:
3975283b4a9bSStephen M. Cameron 		is_write = 1;
3976283b4a9bSStephen M. Cameron 	case READ_6:
3977283b4a9bSStephen M. Cameron 	case READ_12:
3978283b4a9bSStephen M. Cameron 		if (*cdb_len == 6) {
3979283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 8) | cdb[3];
3980283b4a9bSStephen M. Cameron 			block_cnt = cdb[4];
3981283b4a9bSStephen M. Cameron 		} else {
3982283b4a9bSStephen M. Cameron 			BUG_ON(*cdb_len != 12);
3983283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 24) |
3984283b4a9bSStephen M. Cameron 				(((u32) cdb[3]) << 16) |
3985283b4a9bSStephen M. Cameron 				(((u32) cdb[4]) << 8) |
3986283b4a9bSStephen M. Cameron 				cdb[5];
3987283b4a9bSStephen M. Cameron 			block_cnt =
3988283b4a9bSStephen M. Cameron 				(((u32) cdb[6]) << 24) |
3989283b4a9bSStephen M. Cameron 				(((u32) cdb[7]) << 16) |
3990283b4a9bSStephen M. Cameron 				(((u32) cdb[8]) << 8) |
3991283b4a9bSStephen M. Cameron 				cdb[9];
3992283b4a9bSStephen M. Cameron 		}
3993283b4a9bSStephen M. Cameron 		if (block_cnt > 0xffff)
3994283b4a9bSStephen M. Cameron 			return IO_ACCEL_INELIGIBLE;
3995283b4a9bSStephen M. Cameron 
3996283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
3997283b4a9bSStephen M. Cameron 		cdb[1] = 0;
3998283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (block >> 24);
3999283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (block >> 16);
4000283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (block >> 8);
4001283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (block);
4002283b4a9bSStephen M. Cameron 		cdb[6] = 0;
4003283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (block_cnt >> 8);
4004283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (block_cnt);
4005283b4a9bSStephen M. Cameron 		cdb[9] = 0;
4006283b4a9bSStephen M. Cameron 		*cdb_len = 10;
4007283b4a9bSStephen M. Cameron 		break;
4008283b4a9bSStephen M. Cameron 	}
4009283b4a9bSStephen M. Cameron 	return 0;
4010283b4a9bSStephen M. Cameron }
4011283b4a9bSStephen M. Cameron 
4012c349775eSScott Teel static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
4013283b4a9bSStephen M. Cameron 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
401403383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
4015e1f7de0cSMatt Gates {
4016e1f7de0cSMatt Gates 	struct scsi_cmnd *cmd = c->scsi_cmd;
4017e1f7de0cSMatt Gates 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
4018e1f7de0cSMatt Gates 	unsigned int len;
4019e1f7de0cSMatt Gates 	unsigned int total_len = 0;
4020e1f7de0cSMatt Gates 	struct scatterlist *sg;
4021e1f7de0cSMatt Gates 	u64 addr64;
4022e1f7de0cSMatt Gates 	int use_sg, i;
4023e1f7de0cSMatt Gates 	struct SGDescriptor *curr_sg;
4024e1f7de0cSMatt Gates 	u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
4025e1f7de0cSMatt Gates 
4026283b4a9bSStephen M. Cameron 	/* TODO: implement chaining support */
402703383736SDon Brace 	if (scsi_sg_count(cmd) > h->ioaccel_maxsg) {
402803383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
4029283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
403003383736SDon Brace 	}
4031283b4a9bSStephen M. Cameron 
4032e1f7de0cSMatt Gates 	BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
4033e1f7de0cSMatt Gates 
403403383736SDon Brace 	if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
403503383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
4036283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
403703383736SDon Brace 	}
4038283b4a9bSStephen M. Cameron 
4039e1f7de0cSMatt Gates 	c->cmd_type = CMD_IOACCEL1;
4040e1f7de0cSMatt Gates 
4041e1f7de0cSMatt Gates 	/* Adjust the DMA address to point to the accelerated command buffer */
4042e1f7de0cSMatt Gates 	c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
4043e1f7de0cSMatt Gates 				(c->cmdindex * sizeof(*cp));
4044e1f7de0cSMatt Gates 	BUG_ON(c->busaddr & 0x0000007F);
4045e1f7de0cSMatt Gates 
4046e1f7de0cSMatt Gates 	use_sg = scsi_dma_map(cmd);
404703383736SDon Brace 	if (use_sg < 0) {
404803383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
4049e1f7de0cSMatt Gates 		return use_sg;
405003383736SDon Brace 	}
4051e1f7de0cSMatt Gates 
4052e1f7de0cSMatt Gates 	if (use_sg) {
4053e1f7de0cSMatt Gates 		curr_sg = cp->SG;
4054e1f7de0cSMatt Gates 		scsi_for_each_sg(cmd, sg, use_sg, i) {
4055e1f7de0cSMatt Gates 			addr64 = (u64) sg_dma_address(sg);
4056e1f7de0cSMatt Gates 			len  = sg_dma_len(sg);
4057e1f7de0cSMatt Gates 			total_len += len;
405850a0decfSStephen M. Cameron 			curr_sg->Addr = cpu_to_le64(addr64);
405950a0decfSStephen M. Cameron 			curr_sg->Len = cpu_to_le32(len);
406050a0decfSStephen M. Cameron 			curr_sg->Ext = cpu_to_le32(0);
4061e1f7de0cSMatt Gates 			curr_sg++;
4062e1f7de0cSMatt Gates 		}
406350a0decfSStephen M. Cameron 		(--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
4064e1f7de0cSMatt Gates 
4065e1f7de0cSMatt Gates 		switch (cmd->sc_data_direction) {
4066e1f7de0cSMatt Gates 		case DMA_TO_DEVICE:
4067e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_OUT;
4068e1f7de0cSMatt Gates 			break;
4069e1f7de0cSMatt Gates 		case DMA_FROM_DEVICE:
4070e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_IN;
4071e1f7de0cSMatt Gates 			break;
4072e1f7de0cSMatt Gates 		case DMA_NONE:
4073e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_NODATAXFER;
4074e1f7de0cSMatt Gates 			break;
4075e1f7de0cSMatt Gates 		default:
4076e1f7de0cSMatt Gates 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4077e1f7de0cSMatt Gates 			cmd->sc_data_direction);
4078e1f7de0cSMatt Gates 			BUG();
4079e1f7de0cSMatt Gates 			break;
4080e1f7de0cSMatt Gates 		}
4081e1f7de0cSMatt Gates 	} else {
4082e1f7de0cSMatt Gates 		control |= IOACCEL1_CONTROL_NODATAXFER;
4083e1f7de0cSMatt Gates 	}
4084e1f7de0cSMatt Gates 
4085c349775eSScott Teel 	c->Header.SGList = use_sg;
4086e1f7de0cSMatt Gates 	/* Fill out the command structure to submit */
40872b08b3e9SDon Brace 	cp->dev_handle = cpu_to_le16(ioaccel_handle & 0xFFFF);
40882b08b3e9SDon Brace 	cp->transfer_len = cpu_to_le32(total_len);
40892b08b3e9SDon Brace 	cp->io_flags = cpu_to_le16(IOACCEL1_IOFLAGS_IO_REQ |
40902b08b3e9SDon Brace 			(cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK));
40912b08b3e9SDon Brace 	cp->control = cpu_to_le32(control);
4092283b4a9bSStephen M. Cameron 	memcpy(cp->CDB, cdb, cdb_len);
4093283b4a9bSStephen M. Cameron 	memcpy(cp->CISS_LUN, scsi3addr, 8);
4094c349775eSScott Teel 	/* Tag was already set at init time. */
4095e1f7de0cSMatt Gates 	enqueue_cmd_and_start_io(h, c);
4096e1f7de0cSMatt Gates 	return 0;
4097e1f7de0cSMatt Gates }
4098edd16368SStephen M. Cameron 
4099283b4a9bSStephen M. Cameron /*
4100283b4a9bSStephen M. Cameron  * Queue a command directly to a device behind the controller using the
4101283b4a9bSStephen M. Cameron  * I/O accelerator path.
4102283b4a9bSStephen M. Cameron  */
4103283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
4104283b4a9bSStephen M. Cameron 	struct CommandList *c)
4105283b4a9bSStephen M. Cameron {
4106283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
4107283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4108283b4a9bSStephen M. Cameron 
410903383736SDon Brace 	c->phys_disk = dev;
411003383736SDon Brace 
4111283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
411203383736SDon Brace 		cmd->cmnd, cmd->cmd_len, dev->scsi3addr, dev);
4113283b4a9bSStephen M. Cameron }
4114283b4a9bSStephen M. Cameron 
4115dd0e19f3SScott Teel /*
4116dd0e19f3SScott Teel  * Set encryption parameters for the ioaccel2 request
4117dd0e19f3SScott Teel  */
4118dd0e19f3SScott Teel static void set_encrypt_ioaccel2(struct ctlr_info *h,
4119dd0e19f3SScott Teel 	struct CommandList *c, struct io_accel2_cmd *cp)
4120dd0e19f3SScott Teel {
4121dd0e19f3SScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
4122dd0e19f3SScott Teel 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4123dd0e19f3SScott Teel 	struct raid_map_data *map = &dev->raid_map;
4124dd0e19f3SScott Teel 	u64 first_block;
4125dd0e19f3SScott Teel 
4126dd0e19f3SScott Teel 	/* Are we doing encryption on this device */
41272b08b3e9SDon Brace 	if (!(le16_to_cpu(map->flags) & RAID_MAP_FLAG_ENCRYPT_ON))
4128dd0e19f3SScott Teel 		return;
4129dd0e19f3SScott Teel 	/* Set the data encryption key index. */
4130dd0e19f3SScott Teel 	cp->dekindex = map->dekindex;
4131dd0e19f3SScott Teel 
4132dd0e19f3SScott Teel 	/* Set the encryption enable flag, encoded into direction field. */
4133dd0e19f3SScott Teel 	cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
4134dd0e19f3SScott Teel 
4135dd0e19f3SScott Teel 	/* Set encryption tweak values based on logical block address
4136dd0e19f3SScott Teel 	 * If block size is 512, tweak value is LBA.
4137dd0e19f3SScott Teel 	 * For other block sizes, tweak is (LBA * block size)/ 512)
4138dd0e19f3SScott Teel 	 */
4139dd0e19f3SScott Teel 	switch (cmd->cmnd[0]) {
4140dd0e19f3SScott Teel 	/* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
4141dd0e19f3SScott Teel 	case WRITE_6:
4142dd0e19f3SScott Teel 	case READ_6:
41432b08b3e9SDon Brace 		first_block = get_unaligned_be16(&cmd->cmnd[2]);
4144dd0e19f3SScott Teel 		break;
4145dd0e19f3SScott Teel 	case WRITE_10:
4146dd0e19f3SScott Teel 	case READ_10:
4147dd0e19f3SScott Teel 	/* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
4148dd0e19f3SScott Teel 	case WRITE_12:
4149dd0e19f3SScott Teel 	case READ_12:
41502b08b3e9SDon Brace 		first_block = get_unaligned_be32(&cmd->cmnd[2]);
4151dd0e19f3SScott Teel 		break;
4152dd0e19f3SScott Teel 	case WRITE_16:
4153dd0e19f3SScott Teel 	case READ_16:
41542b08b3e9SDon Brace 		first_block = get_unaligned_be64(&cmd->cmnd[2]);
4155dd0e19f3SScott Teel 		break;
4156dd0e19f3SScott Teel 	default:
4157dd0e19f3SScott Teel 		dev_err(&h->pdev->dev,
41582b08b3e9SDon Brace 			"ERROR: %s: size (0x%x) not supported for encryption\n",
41592b08b3e9SDon Brace 			__func__, cmd->cmnd[0]);
4160dd0e19f3SScott Teel 		BUG();
4161dd0e19f3SScott Teel 		break;
4162dd0e19f3SScott Teel 	}
41632b08b3e9SDon Brace 
41642b08b3e9SDon Brace 	if (le32_to_cpu(map->volume_blk_size) != 512)
41652b08b3e9SDon Brace 		first_block = first_block *
41662b08b3e9SDon Brace 				le32_to_cpu(map->volume_blk_size)/512;
41672b08b3e9SDon Brace 
41682b08b3e9SDon Brace 	cp->tweak_lower = cpu_to_le32(first_block);
41692b08b3e9SDon Brace 	cp->tweak_upper = cpu_to_le32(first_block >> 32);
4170dd0e19f3SScott Teel }
4171dd0e19f3SScott Teel 
4172c349775eSScott Teel static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
4173c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
417403383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
4175c349775eSScott Teel {
4176c349775eSScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
4177c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
4178c349775eSScott Teel 	struct ioaccel2_sg_element *curr_sg;
4179c349775eSScott Teel 	int use_sg, i;
4180c349775eSScott Teel 	struct scatterlist *sg;
4181c349775eSScott Teel 	u64 addr64;
4182c349775eSScott Teel 	u32 len;
4183c349775eSScott Teel 	u32 total_len = 0;
4184c349775eSScott Teel 
4185d9a729f3SWebb Scales 	BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
4186c349775eSScott Teel 
418703383736SDon Brace 	if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
418803383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
4189c349775eSScott Teel 		return IO_ACCEL_INELIGIBLE;
419003383736SDon Brace 	}
419103383736SDon Brace 
4192c349775eSScott Teel 	c->cmd_type = CMD_IOACCEL2;
4193c349775eSScott Teel 	/* Adjust the DMA address to point to the accelerated command buffer */
4194c349775eSScott Teel 	c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
4195c349775eSScott Teel 				(c->cmdindex * sizeof(*cp));
4196c349775eSScott Teel 	BUG_ON(c->busaddr & 0x0000007F);
4197c349775eSScott Teel 
4198c349775eSScott Teel 	memset(cp, 0, sizeof(*cp));
4199c349775eSScott Teel 	cp->IU_type = IOACCEL2_IU_TYPE;
4200c349775eSScott Teel 
4201c349775eSScott Teel 	use_sg = scsi_dma_map(cmd);
420203383736SDon Brace 	if (use_sg < 0) {
420303383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
4204c349775eSScott Teel 		return use_sg;
420503383736SDon Brace 	}
4206c349775eSScott Teel 
4207c349775eSScott Teel 	if (use_sg) {
4208c349775eSScott Teel 		curr_sg = cp->sg;
4209d9a729f3SWebb Scales 		if (use_sg > h->ioaccel_maxsg) {
4210d9a729f3SWebb Scales 			addr64 = le64_to_cpu(
4211d9a729f3SWebb Scales 				h->ioaccel2_cmd_sg_list[c->cmdindex]->address);
4212d9a729f3SWebb Scales 			curr_sg->address = cpu_to_le64(addr64);
4213d9a729f3SWebb Scales 			curr_sg->length = 0;
4214d9a729f3SWebb Scales 			curr_sg->reserved[0] = 0;
4215d9a729f3SWebb Scales 			curr_sg->reserved[1] = 0;
4216d9a729f3SWebb Scales 			curr_sg->reserved[2] = 0;
4217d9a729f3SWebb Scales 			curr_sg->chain_indicator = 0x80;
4218d9a729f3SWebb Scales 
4219d9a729f3SWebb Scales 			curr_sg = h->ioaccel2_cmd_sg_list[c->cmdindex];
4220d9a729f3SWebb Scales 		}
4221c349775eSScott Teel 		scsi_for_each_sg(cmd, sg, use_sg, i) {
4222c349775eSScott Teel 			addr64 = (u64) sg_dma_address(sg);
4223c349775eSScott Teel 			len  = sg_dma_len(sg);
4224c349775eSScott Teel 			total_len += len;
4225c349775eSScott Teel 			curr_sg->address = cpu_to_le64(addr64);
4226c349775eSScott Teel 			curr_sg->length = cpu_to_le32(len);
4227c349775eSScott Teel 			curr_sg->reserved[0] = 0;
4228c349775eSScott Teel 			curr_sg->reserved[1] = 0;
4229c349775eSScott Teel 			curr_sg->reserved[2] = 0;
4230c349775eSScott Teel 			curr_sg->chain_indicator = 0;
4231c349775eSScott Teel 			curr_sg++;
4232c349775eSScott Teel 		}
4233c349775eSScott Teel 
4234c349775eSScott Teel 		switch (cmd->sc_data_direction) {
4235c349775eSScott Teel 		case DMA_TO_DEVICE:
4236dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4237dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_OUT;
4238c349775eSScott Teel 			break;
4239c349775eSScott Teel 		case DMA_FROM_DEVICE:
4240dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4241dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_IN;
4242c349775eSScott Teel 			break;
4243c349775eSScott Teel 		case DMA_NONE:
4244dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4245dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_NO_DATA;
4246c349775eSScott Teel 			break;
4247c349775eSScott Teel 		default:
4248c349775eSScott Teel 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4249c349775eSScott Teel 				cmd->sc_data_direction);
4250c349775eSScott Teel 			BUG();
4251c349775eSScott Teel 			break;
4252c349775eSScott Teel 		}
4253c349775eSScott Teel 	} else {
4254dd0e19f3SScott Teel 		cp->direction &= ~IOACCEL2_DIRECTION_MASK;
4255dd0e19f3SScott Teel 		cp->direction |= IOACCEL2_DIR_NO_DATA;
4256c349775eSScott Teel 	}
4257dd0e19f3SScott Teel 
4258dd0e19f3SScott Teel 	/* Set encryption parameters, if necessary */
4259dd0e19f3SScott Teel 	set_encrypt_ioaccel2(h, c, cp);
4260dd0e19f3SScott Teel 
42612b08b3e9SDon Brace 	cp->scsi_nexus = cpu_to_le32(ioaccel_handle);
4262f2405db8SDon Brace 	cp->Tag = cpu_to_le32(c->cmdindex << DIRECT_LOOKUP_SHIFT);
4263c349775eSScott Teel 	memcpy(cp->cdb, cdb, sizeof(cp->cdb));
4264c349775eSScott Teel 
4265c349775eSScott Teel 	cp->data_len = cpu_to_le32(total_len);
4266c349775eSScott Teel 	cp->err_ptr = cpu_to_le64(c->busaddr +
4267c349775eSScott Teel 			offsetof(struct io_accel2_cmd, error_data));
426850a0decfSStephen M. Cameron 	cp->err_len = cpu_to_le32(sizeof(cp->error_data));
4269c349775eSScott Teel 
4270d9a729f3SWebb Scales 	/* fill in sg elements */
4271d9a729f3SWebb Scales 	if (use_sg > h->ioaccel_maxsg) {
4272d9a729f3SWebb Scales 		cp->sg_count = 1;
4273d9a729f3SWebb Scales 		if (hpsa_map_ioaccel2_sg_chain_block(h, cp, c)) {
4274d9a729f3SWebb Scales 			atomic_dec(&phys_disk->ioaccel_cmds_out);
4275d9a729f3SWebb Scales 			scsi_dma_unmap(cmd);
4276d9a729f3SWebb Scales 			return -1;
4277d9a729f3SWebb Scales 		}
4278d9a729f3SWebb Scales 	} else
4279d9a729f3SWebb Scales 		cp->sg_count = (u8) use_sg;
4280d9a729f3SWebb Scales 
4281c349775eSScott Teel 	enqueue_cmd_and_start_io(h, c);
4282c349775eSScott Teel 	return 0;
4283c349775eSScott Teel }
4284c349775eSScott Teel 
4285c349775eSScott Teel /*
4286c349775eSScott Teel  * Queue a command to the correct I/O accelerator path.
4287c349775eSScott Teel  */
4288c349775eSScott Teel static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
4289c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
429003383736SDon Brace 	u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
4291c349775eSScott Teel {
429203383736SDon Brace 	/* Try to honor the device's queue depth */
429303383736SDon Brace 	if (atomic_inc_return(&phys_disk->ioaccel_cmds_out) >
429403383736SDon Brace 					phys_disk->queue_depth) {
429503383736SDon Brace 		atomic_dec(&phys_disk->ioaccel_cmds_out);
429603383736SDon Brace 		return IO_ACCEL_INELIGIBLE;
429703383736SDon Brace 	}
4298c349775eSScott Teel 	if (h->transMethod & CFGTBL_Trans_io_accel1)
4299c349775eSScott Teel 		return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
430003383736SDon Brace 						cdb, cdb_len, scsi3addr,
430103383736SDon Brace 						phys_disk);
4302c349775eSScott Teel 	else
4303c349775eSScott Teel 		return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
430403383736SDon Brace 						cdb, cdb_len, scsi3addr,
430503383736SDon Brace 						phys_disk);
4306c349775eSScott Teel }
4307c349775eSScott Teel 
43086b80b18fSScott Teel static void raid_map_helper(struct raid_map_data *map,
43096b80b18fSScott Teel 		int offload_to_mirror, u32 *map_index, u32 *current_group)
43106b80b18fSScott Teel {
43116b80b18fSScott Teel 	if (offload_to_mirror == 0)  {
43126b80b18fSScott Teel 		/* use physical disk in the first mirrored group. */
43132b08b3e9SDon Brace 		*map_index %= le16_to_cpu(map->data_disks_per_row);
43146b80b18fSScott Teel 		return;
43156b80b18fSScott Teel 	}
43166b80b18fSScott Teel 	do {
43176b80b18fSScott Teel 		/* determine mirror group that *map_index indicates */
43182b08b3e9SDon Brace 		*current_group = *map_index /
43192b08b3e9SDon Brace 			le16_to_cpu(map->data_disks_per_row);
43206b80b18fSScott Teel 		if (offload_to_mirror == *current_group)
43216b80b18fSScott Teel 			continue;
43222b08b3e9SDon Brace 		if (*current_group < le16_to_cpu(map->layout_map_count) - 1) {
43236b80b18fSScott Teel 			/* select map index from next group */
43242b08b3e9SDon Brace 			*map_index += le16_to_cpu(map->data_disks_per_row);
43256b80b18fSScott Teel 			(*current_group)++;
43266b80b18fSScott Teel 		} else {
43276b80b18fSScott Teel 			/* select map index from first group */
43282b08b3e9SDon Brace 			*map_index %= le16_to_cpu(map->data_disks_per_row);
43296b80b18fSScott Teel 			*current_group = 0;
43306b80b18fSScott Teel 		}
43316b80b18fSScott Teel 	} while (offload_to_mirror != *current_group);
43326b80b18fSScott Teel }
43336b80b18fSScott Teel 
4334283b4a9bSStephen M. Cameron /*
4335283b4a9bSStephen M. Cameron  * Attempt to perform offload RAID mapping for a logical volume I/O.
4336283b4a9bSStephen M. Cameron  */
4337283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
4338283b4a9bSStephen M. Cameron 	struct CommandList *c)
4339283b4a9bSStephen M. Cameron {
4340283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
4341283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4342283b4a9bSStephen M. Cameron 	struct raid_map_data *map = &dev->raid_map;
4343283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map->data[0];
4344283b4a9bSStephen M. Cameron 	int is_write = 0;
4345283b4a9bSStephen M. Cameron 	u32 map_index;
4346283b4a9bSStephen M. Cameron 	u64 first_block, last_block;
4347283b4a9bSStephen M. Cameron 	u32 block_cnt;
4348283b4a9bSStephen M. Cameron 	u32 blocks_per_row;
4349283b4a9bSStephen M. Cameron 	u64 first_row, last_row;
4350283b4a9bSStephen M. Cameron 	u32 first_row_offset, last_row_offset;
4351283b4a9bSStephen M. Cameron 	u32 first_column, last_column;
43526b80b18fSScott Teel 	u64 r0_first_row, r0_last_row;
43536b80b18fSScott Teel 	u32 r5or6_blocks_per_row;
43546b80b18fSScott Teel 	u64 r5or6_first_row, r5or6_last_row;
43556b80b18fSScott Teel 	u32 r5or6_first_row_offset, r5or6_last_row_offset;
43566b80b18fSScott Teel 	u32 r5or6_first_column, r5or6_last_column;
43576b80b18fSScott Teel 	u32 total_disks_per_row;
43586b80b18fSScott Teel 	u32 stripesize;
43596b80b18fSScott Teel 	u32 first_group, last_group, current_group;
4360283b4a9bSStephen M. Cameron 	u32 map_row;
4361283b4a9bSStephen M. Cameron 	u32 disk_handle;
4362283b4a9bSStephen M. Cameron 	u64 disk_block;
4363283b4a9bSStephen M. Cameron 	u32 disk_block_cnt;
4364283b4a9bSStephen M. Cameron 	u8 cdb[16];
4365283b4a9bSStephen M. Cameron 	u8 cdb_len;
43662b08b3e9SDon Brace 	u16 strip_size;
4367283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
4368283b4a9bSStephen M. Cameron 	u64 tmpdiv;
4369283b4a9bSStephen M. Cameron #endif
43706b80b18fSScott Teel 	int offload_to_mirror;
4371283b4a9bSStephen M. Cameron 
4372283b4a9bSStephen M. Cameron 	/* check for valid opcode, get LBA and block count */
4373283b4a9bSStephen M. Cameron 	switch (cmd->cmnd[0]) {
4374283b4a9bSStephen M. Cameron 	case WRITE_6:
4375283b4a9bSStephen M. Cameron 		is_write = 1;
4376283b4a9bSStephen M. Cameron 	case READ_6:
4377283b4a9bSStephen M. Cameron 		first_block =
4378283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 8) |
4379283b4a9bSStephen M. Cameron 			cmd->cmnd[3];
4380283b4a9bSStephen M. Cameron 		block_cnt = cmd->cmnd[4];
43813fa89a04SStephen M. Cameron 		if (block_cnt == 0)
43823fa89a04SStephen M. Cameron 			block_cnt = 256;
4383283b4a9bSStephen M. Cameron 		break;
4384283b4a9bSStephen M. Cameron 	case WRITE_10:
4385283b4a9bSStephen M. Cameron 		is_write = 1;
4386283b4a9bSStephen M. Cameron 	case READ_10:
4387283b4a9bSStephen M. Cameron 		first_block =
4388283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
4389283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
4390283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
4391283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
4392283b4a9bSStephen M. Cameron 		block_cnt =
4393283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 8) |
4394283b4a9bSStephen M. Cameron 			cmd->cmnd[8];
4395283b4a9bSStephen M. Cameron 		break;
4396283b4a9bSStephen M. Cameron 	case WRITE_12:
4397283b4a9bSStephen M. Cameron 		is_write = 1;
4398283b4a9bSStephen M. Cameron 	case READ_12:
4399283b4a9bSStephen M. Cameron 		first_block =
4400283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
4401283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
4402283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
4403283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
4404283b4a9bSStephen M. Cameron 		block_cnt =
4405283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[6]) << 24) |
4406283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 16) |
4407283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[8]) << 8) |
4408283b4a9bSStephen M. Cameron 		cmd->cmnd[9];
4409283b4a9bSStephen M. Cameron 		break;
4410283b4a9bSStephen M. Cameron 	case WRITE_16:
4411283b4a9bSStephen M. Cameron 		is_write = 1;
4412283b4a9bSStephen M. Cameron 	case READ_16:
4413283b4a9bSStephen M. Cameron 		first_block =
4414283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 56) |
4415283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 48) |
4416283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 40) |
4417283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[5]) << 32) |
4418283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[6]) << 24) |
4419283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[7]) << 16) |
4420283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[8]) << 8) |
4421283b4a9bSStephen M. Cameron 			cmd->cmnd[9];
4422283b4a9bSStephen M. Cameron 		block_cnt =
4423283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[10]) << 24) |
4424283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[11]) << 16) |
4425283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[12]) << 8) |
4426283b4a9bSStephen M. Cameron 			cmd->cmnd[13];
4427283b4a9bSStephen M. Cameron 		break;
4428283b4a9bSStephen M. Cameron 	default:
4429283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
4430283b4a9bSStephen M. Cameron 	}
4431283b4a9bSStephen M. Cameron 	last_block = first_block + block_cnt - 1;
4432283b4a9bSStephen M. Cameron 
4433283b4a9bSStephen M. Cameron 	/* check for write to non-RAID-0 */
4434283b4a9bSStephen M. Cameron 	if (is_write && dev->raid_level != 0)
4435283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
4436283b4a9bSStephen M. Cameron 
4437283b4a9bSStephen M. Cameron 	/* check for invalid block or wraparound */
44382b08b3e9SDon Brace 	if (last_block >= le64_to_cpu(map->volume_blk_cnt) ||
44392b08b3e9SDon Brace 		last_block < first_block)
4440283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
4441283b4a9bSStephen M. Cameron 
4442283b4a9bSStephen M. Cameron 	/* calculate stripe information for the request */
44432b08b3e9SDon Brace 	blocks_per_row = le16_to_cpu(map->data_disks_per_row) *
44442b08b3e9SDon Brace 				le16_to_cpu(map->strip_size);
44452b08b3e9SDon Brace 	strip_size = le16_to_cpu(map->strip_size);
4446283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
4447283b4a9bSStephen M. Cameron 	tmpdiv = first_block;
4448283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
4449283b4a9bSStephen M. Cameron 	first_row = tmpdiv;
4450283b4a9bSStephen M. Cameron 	tmpdiv = last_block;
4451283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
4452283b4a9bSStephen M. Cameron 	last_row = tmpdiv;
4453283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
4454283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
4455283b4a9bSStephen M. Cameron 	tmpdiv = first_row_offset;
44562b08b3e9SDon Brace 	(void) do_div(tmpdiv, strip_size);
4457283b4a9bSStephen M. Cameron 	first_column = tmpdiv;
4458283b4a9bSStephen M. Cameron 	tmpdiv = last_row_offset;
44592b08b3e9SDon Brace 	(void) do_div(tmpdiv, strip_size);
4460283b4a9bSStephen M. Cameron 	last_column = tmpdiv;
4461283b4a9bSStephen M. Cameron #else
4462283b4a9bSStephen M. Cameron 	first_row = first_block / blocks_per_row;
4463283b4a9bSStephen M. Cameron 	last_row = last_block / blocks_per_row;
4464283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
4465283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
44662b08b3e9SDon Brace 	first_column = first_row_offset / strip_size;
44672b08b3e9SDon Brace 	last_column = last_row_offset / strip_size;
4468283b4a9bSStephen M. Cameron #endif
4469283b4a9bSStephen M. Cameron 
4470283b4a9bSStephen M. Cameron 	/* if this isn't a single row/column then give to the controller */
4471283b4a9bSStephen M. Cameron 	if ((first_row != last_row) || (first_column != last_column))
4472283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
4473283b4a9bSStephen M. Cameron 
4474283b4a9bSStephen M. Cameron 	/* proceeding with driver mapping */
44752b08b3e9SDon Brace 	total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
44762b08b3e9SDon Brace 				le16_to_cpu(map->metadata_disks_per_row);
4477283b4a9bSStephen M. Cameron 	map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
44782b08b3e9SDon Brace 				le16_to_cpu(map->row_cnt);
44796b80b18fSScott Teel 	map_index = (map_row * total_disks_per_row) + first_column;
44806b80b18fSScott Teel 
44816b80b18fSScott Teel 	switch (dev->raid_level) {
44826b80b18fSScott Teel 	case HPSA_RAID_0:
44836b80b18fSScott Teel 		break; /* nothing special to do */
44846b80b18fSScott Teel 	case HPSA_RAID_1:
44856b80b18fSScott Teel 		/* Handles load balance across RAID 1 members.
44866b80b18fSScott Teel 		 * (2-drive R1 and R10 with even # of drives.)
44876b80b18fSScott Teel 		 * Appropriate for SSDs, not optimal for HDDs
4488283b4a9bSStephen M. Cameron 		 */
44892b08b3e9SDon Brace 		BUG_ON(le16_to_cpu(map->layout_map_count) != 2);
4490283b4a9bSStephen M. Cameron 		if (dev->offload_to_mirror)
44912b08b3e9SDon Brace 			map_index += le16_to_cpu(map->data_disks_per_row);
4492283b4a9bSStephen M. Cameron 		dev->offload_to_mirror = !dev->offload_to_mirror;
44936b80b18fSScott Teel 		break;
44946b80b18fSScott Teel 	case HPSA_RAID_ADM:
44956b80b18fSScott Teel 		/* Handles N-way mirrors  (R1-ADM)
44966b80b18fSScott Teel 		 * and R10 with # of drives divisible by 3.)
44976b80b18fSScott Teel 		 */
44982b08b3e9SDon Brace 		BUG_ON(le16_to_cpu(map->layout_map_count) != 3);
44996b80b18fSScott Teel 
45006b80b18fSScott Teel 		offload_to_mirror = dev->offload_to_mirror;
45016b80b18fSScott Teel 		raid_map_helper(map, offload_to_mirror,
45026b80b18fSScott Teel 				&map_index, &current_group);
45036b80b18fSScott Teel 		/* set mirror group to use next time */
45046b80b18fSScott Teel 		offload_to_mirror =
45052b08b3e9SDon Brace 			(offload_to_mirror >=
45062b08b3e9SDon Brace 			le16_to_cpu(map->layout_map_count) - 1)
45076b80b18fSScott Teel 			? 0 : offload_to_mirror + 1;
45086b80b18fSScott Teel 		dev->offload_to_mirror = offload_to_mirror;
45096b80b18fSScott Teel 		/* Avoid direct use of dev->offload_to_mirror within this
45106b80b18fSScott Teel 		 * function since multiple threads might simultaneously
45116b80b18fSScott Teel 		 * increment it beyond the range of dev->layout_map_count -1.
45126b80b18fSScott Teel 		 */
45136b80b18fSScott Teel 		break;
45146b80b18fSScott Teel 	case HPSA_RAID_5:
45156b80b18fSScott Teel 	case HPSA_RAID_6:
45162b08b3e9SDon Brace 		if (le16_to_cpu(map->layout_map_count) <= 1)
45176b80b18fSScott Teel 			break;
45186b80b18fSScott Teel 
45196b80b18fSScott Teel 		/* Verify first and last block are in same RAID group */
45206b80b18fSScott Teel 		r5or6_blocks_per_row =
45212b08b3e9SDon Brace 			le16_to_cpu(map->strip_size) *
45222b08b3e9SDon Brace 			le16_to_cpu(map->data_disks_per_row);
45236b80b18fSScott Teel 		BUG_ON(r5or6_blocks_per_row == 0);
45242b08b3e9SDon Brace 		stripesize = r5or6_blocks_per_row *
45252b08b3e9SDon Brace 			le16_to_cpu(map->layout_map_count);
45266b80b18fSScott Teel #if BITS_PER_LONG == 32
45276b80b18fSScott Teel 		tmpdiv = first_block;
45286b80b18fSScott Teel 		first_group = do_div(tmpdiv, stripesize);
45296b80b18fSScott Teel 		tmpdiv = first_group;
45306b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
45316b80b18fSScott Teel 		first_group = tmpdiv;
45326b80b18fSScott Teel 		tmpdiv = last_block;
45336b80b18fSScott Teel 		last_group = do_div(tmpdiv, stripesize);
45346b80b18fSScott Teel 		tmpdiv = last_group;
45356b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
45366b80b18fSScott Teel 		last_group = tmpdiv;
45376b80b18fSScott Teel #else
45386b80b18fSScott Teel 		first_group = (first_block % stripesize) / r5or6_blocks_per_row;
45396b80b18fSScott Teel 		last_group = (last_block % stripesize) / r5or6_blocks_per_row;
45406b80b18fSScott Teel #endif
4541000ff7c2SStephen M. Cameron 		if (first_group != last_group)
45426b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
45436b80b18fSScott Teel 
45446b80b18fSScott Teel 		/* Verify request is in a single row of RAID 5/6 */
45456b80b18fSScott Teel #if BITS_PER_LONG == 32
45466b80b18fSScott Teel 		tmpdiv = first_block;
45476b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
45486b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row = tmpdiv;
45496b80b18fSScott Teel 		tmpdiv = last_block;
45506b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
45516b80b18fSScott Teel 		r5or6_last_row = r0_last_row = tmpdiv;
45526b80b18fSScott Teel #else
45536b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row =
45546b80b18fSScott Teel 						first_block / stripesize;
45556b80b18fSScott Teel 		r5or6_last_row = r0_last_row = last_block / stripesize;
45566b80b18fSScott Teel #endif
45576b80b18fSScott Teel 		if (r5or6_first_row != r5or6_last_row)
45586b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
45596b80b18fSScott Teel 
45606b80b18fSScott Teel 
45616b80b18fSScott Teel 		/* Verify request is in a single column */
45626b80b18fSScott Teel #if BITS_PER_LONG == 32
45636b80b18fSScott Teel 		tmpdiv = first_block;
45646b80b18fSScott Teel 		first_row_offset = do_div(tmpdiv, stripesize);
45656b80b18fSScott Teel 		tmpdiv = first_row_offset;
45666b80b18fSScott Teel 		first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
45676b80b18fSScott Teel 		r5or6_first_row_offset = first_row_offset;
45686b80b18fSScott Teel 		tmpdiv = last_block;
45696b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, stripesize);
45706b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
45716b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
45726b80b18fSScott Teel 		tmpdiv = r5or6_first_row_offset;
45736b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
45746b80b18fSScott Teel 		first_column = r5or6_first_column = tmpdiv;
45756b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
45766b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
45776b80b18fSScott Teel 		r5or6_last_column = tmpdiv;
45786b80b18fSScott Teel #else
45796b80b18fSScott Teel 		first_row_offset = r5or6_first_row_offset =
45806b80b18fSScott Teel 			(u32)((first_block % stripesize) %
45816b80b18fSScott Teel 						r5or6_blocks_per_row);
45826b80b18fSScott Teel 
45836b80b18fSScott Teel 		r5or6_last_row_offset =
45846b80b18fSScott Teel 			(u32)((last_block % stripesize) %
45856b80b18fSScott Teel 						r5or6_blocks_per_row);
45866b80b18fSScott Teel 
45876b80b18fSScott Teel 		first_column = r5or6_first_column =
45882b08b3e9SDon Brace 			r5or6_first_row_offset / le16_to_cpu(map->strip_size);
45896b80b18fSScott Teel 		r5or6_last_column =
45902b08b3e9SDon Brace 			r5or6_last_row_offset / le16_to_cpu(map->strip_size);
45916b80b18fSScott Teel #endif
45926b80b18fSScott Teel 		if (r5or6_first_column != r5or6_last_column)
45936b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
45946b80b18fSScott Teel 
45956b80b18fSScott Teel 		/* Request is eligible */
45966b80b18fSScott Teel 		map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
45972b08b3e9SDon Brace 			le16_to_cpu(map->row_cnt);
45986b80b18fSScott Teel 
45996b80b18fSScott Teel 		map_index = (first_group *
46002b08b3e9SDon Brace 			(le16_to_cpu(map->row_cnt) * total_disks_per_row)) +
46016b80b18fSScott Teel 			(map_row * total_disks_per_row) + first_column;
46026b80b18fSScott Teel 		break;
46036b80b18fSScott Teel 	default:
46046b80b18fSScott Teel 		return IO_ACCEL_INELIGIBLE;
4605283b4a9bSStephen M. Cameron 	}
46066b80b18fSScott Teel 
460707543e0cSStephen Cameron 	if (unlikely(map_index >= RAID_MAP_MAX_ENTRIES))
460807543e0cSStephen Cameron 		return IO_ACCEL_INELIGIBLE;
460907543e0cSStephen Cameron 
461003383736SDon Brace 	c->phys_disk = dev->phys_disk[map_index];
461103383736SDon Brace 
4612283b4a9bSStephen M. Cameron 	disk_handle = dd[map_index].ioaccel_handle;
46132b08b3e9SDon Brace 	disk_block = le64_to_cpu(map->disk_starting_blk) +
46142b08b3e9SDon Brace 			first_row * le16_to_cpu(map->strip_size) +
46152b08b3e9SDon Brace 			(first_row_offset - first_column *
46162b08b3e9SDon Brace 			le16_to_cpu(map->strip_size));
4617283b4a9bSStephen M. Cameron 	disk_block_cnt = block_cnt;
4618283b4a9bSStephen M. Cameron 
4619283b4a9bSStephen M. Cameron 	/* handle differing logical/physical block sizes */
4620283b4a9bSStephen M. Cameron 	if (map->phys_blk_shift) {
4621283b4a9bSStephen M. Cameron 		disk_block <<= map->phys_blk_shift;
4622283b4a9bSStephen M. Cameron 		disk_block_cnt <<= map->phys_blk_shift;
4623283b4a9bSStephen M. Cameron 	}
4624283b4a9bSStephen M. Cameron 	BUG_ON(disk_block_cnt > 0xffff);
4625283b4a9bSStephen M. Cameron 
4626283b4a9bSStephen M. Cameron 	/* build the new CDB for the physical disk I/O */
4627283b4a9bSStephen M. Cameron 	if (disk_block > 0xffffffff) {
4628283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_16 : READ_16;
4629283b4a9bSStephen M. Cameron 		cdb[1] = 0;
4630283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 56);
4631283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 48);
4632283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 40);
4633283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block >> 32);
4634283b4a9bSStephen M. Cameron 		cdb[6] = (u8) (disk_block >> 24);
4635283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block >> 16);
4636283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block >> 8);
4637283b4a9bSStephen M. Cameron 		cdb[9] = (u8) (disk_block);
4638283b4a9bSStephen M. Cameron 		cdb[10] = (u8) (disk_block_cnt >> 24);
4639283b4a9bSStephen M. Cameron 		cdb[11] = (u8) (disk_block_cnt >> 16);
4640283b4a9bSStephen M. Cameron 		cdb[12] = (u8) (disk_block_cnt >> 8);
4641283b4a9bSStephen M. Cameron 		cdb[13] = (u8) (disk_block_cnt);
4642283b4a9bSStephen M. Cameron 		cdb[14] = 0;
4643283b4a9bSStephen M. Cameron 		cdb[15] = 0;
4644283b4a9bSStephen M. Cameron 		cdb_len = 16;
4645283b4a9bSStephen M. Cameron 	} else {
4646283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
4647283b4a9bSStephen M. Cameron 		cdb[1] = 0;
4648283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 24);
4649283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 16);
4650283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 8);
4651283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block);
4652283b4a9bSStephen M. Cameron 		cdb[6] = 0;
4653283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block_cnt >> 8);
4654283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block_cnt);
4655283b4a9bSStephen M. Cameron 		cdb[9] = 0;
4656283b4a9bSStephen M. Cameron 		cdb_len = 10;
4657283b4a9bSStephen M. Cameron 	}
4658283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
465903383736SDon Brace 						dev->scsi3addr,
466003383736SDon Brace 						dev->phys_disk[map_index]);
4661283b4a9bSStephen M. Cameron }
4662283b4a9bSStephen M. Cameron 
466325163bd5SWebb Scales /*
466425163bd5SWebb Scales  * Submit commands down the "normal" RAID stack path
466525163bd5SWebb Scales  * All callers to hpsa_ciss_submit must check lockup_detected
466625163bd5SWebb Scales  * beforehand, before (opt.) and after calling cmd_alloc
466725163bd5SWebb Scales  */
4668574f05d3SStephen Cameron static int hpsa_ciss_submit(struct ctlr_info *h,
4669574f05d3SStephen Cameron 	struct CommandList *c, struct scsi_cmnd *cmd,
4670574f05d3SStephen Cameron 	unsigned char scsi3addr[])
4671edd16368SStephen M. Cameron {
4672edd16368SStephen M. Cameron 	cmd->host_scribble = (unsigned char *) c;
4673edd16368SStephen M. Cameron 	c->cmd_type = CMD_SCSI;
4674edd16368SStephen M. Cameron 	c->scsi_cmd = cmd;
4675edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;  /* unused in simple mode */
4676edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
4677f2405db8SDon Brace 	c->Header.tag = cpu_to_le64((c->cmdindex << DIRECT_LOOKUP_SHIFT));
4678edd16368SStephen M. Cameron 
4679edd16368SStephen M. Cameron 	/* Fill in the request block... */
4680edd16368SStephen M. Cameron 
4681edd16368SStephen M. Cameron 	c->Request.Timeout = 0;
4682edd16368SStephen M. Cameron 	BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
4683edd16368SStephen M. Cameron 	c->Request.CDBLen = cmd->cmd_len;
4684edd16368SStephen M. Cameron 	memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
4685edd16368SStephen M. Cameron 	switch (cmd->sc_data_direction) {
4686edd16368SStephen M. Cameron 	case DMA_TO_DEVICE:
4687a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4688a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_WRITE);
4689edd16368SStephen M. Cameron 		break;
4690edd16368SStephen M. Cameron 	case DMA_FROM_DEVICE:
4691a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4692a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_READ);
4693edd16368SStephen M. Cameron 		break;
4694edd16368SStephen M. Cameron 	case DMA_NONE:
4695a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4696a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_NONE);
4697edd16368SStephen M. Cameron 		break;
4698edd16368SStephen M. Cameron 	case DMA_BIDIRECTIONAL:
4699edd16368SStephen M. Cameron 		/* This can happen if a buggy application does a scsi passthru
4700edd16368SStephen M. Cameron 		 * and sets both inlen and outlen to non-zero. ( see
4701edd16368SStephen M. Cameron 		 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
4702edd16368SStephen M. Cameron 		 */
4703edd16368SStephen M. Cameron 
4704a505b86fSStephen M. Cameron 		c->Request.type_attr_dir =
4705a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_RSVD);
4706edd16368SStephen M. Cameron 		/* This is technically wrong, and hpsa controllers should
4707edd16368SStephen M. Cameron 		 * reject it with CMD_INVALID, which is the most correct
4708edd16368SStephen M. Cameron 		 * response, but non-fibre backends appear to let it
4709edd16368SStephen M. Cameron 		 * slide by, and give the same results as if this field
4710edd16368SStephen M. Cameron 		 * were set correctly.  Either way is acceptable for
4711edd16368SStephen M. Cameron 		 * our purposes here.
4712edd16368SStephen M. Cameron 		 */
4713edd16368SStephen M. Cameron 
4714edd16368SStephen M. Cameron 		break;
4715edd16368SStephen M. Cameron 
4716edd16368SStephen M. Cameron 	default:
4717edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "unknown data direction: %d\n",
4718edd16368SStephen M. Cameron 			cmd->sc_data_direction);
4719edd16368SStephen M. Cameron 		BUG();
4720edd16368SStephen M. Cameron 		break;
4721edd16368SStephen M. Cameron 	}
4722edd16368SStephen M. Cameron 
472333a2ffceSStephen M. Cameron 	if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
472473153fe5SWebb Scales 		hpsa_cmd_resolve_and_free(h, c);
4725edd16368SStephen M. Cameron 		return SCSI_MLQUEUE_HOST_BUSY;
4726edd16368SStephen M. Cameron 	}
4727edd16368SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
4728edd16368SStephen M. Cameron 	/* the cmd'll come back via intr handler in complete_scsi_command()  */
4729edd16368SStephen M. Cameron 	return 0;
4730edd16368SStephen M. Cameron }
4731edd16368SStephen M. Cameron 
4732360c73bdSStephen Cameron static void hpsa_cmd_init(struct ctlr_info *h, int index,
4733360c73bdSStephen Cameron 				struct CommandList *c)
4734360c73bdSStephen Cameron {
4735360c73bdSStephen Cameron 	dma_addr_t cmd_dma_handle, err_dma_handle;
4736360c73bdSStephen Cameron 
4737360c73bdSStephen Cameron 	/* Zero out all of commandlist except the last field, refcount */
4738360c73bdSStephen Cameron 	memset(c, 0, offsetof(struct CommandList, refcount));
4739360c73bdSStephen Cameron 	c->Header.tag = cpu_to_le64((u64) (index << DIRECT_LOOKUP_SHIFT));
4740360c73bdSStephen Cameron 	cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
4741360c73bdSStephen Cameron 	c->err_info = h->errinfo_pool + index;
4742360c73bdSStephen Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
4743360c73bdSStephen Cameron 	err_dma_handle = h->errinfo_pool_dhandle
4744360c73bdSStephen Cameron 	    + index * sizeof(*c->err_info);
4745360c73bdSStephen Cameron 	c->cmdindex = index;
4746360c73bdSStephen Cameron 	c->busaddr = (u32) cmd_dma_handle;
4747360c73bdSStephen Cameron 	c->ErrDesc.Addr = cpu_to_le64((u64) err_dma_handle);
4748360c73bdSStephen Cameron 	c->ErrDesc.Len = cpu_to_le32((u32) sizeof(*c->err_info));
4749360c73bdSStephen Cameron 	c->h = h;
4750a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_IDLE;
4751360c73bdSStephen Cameron }
4752360c73bdSStephen Cameron 
4753360c73bdSStephen Cameron static void hpsa_preinitialize_commands(struct ctlr_info *h)
4754360c73bdSStephen Cameron {
4755360c73bdSStephen Cameron 	int i;
4756360c73bdSStephen Cameron 
4757360c73bdSStephen Cameron 	for (i = 0; i < h->nr_cmds; i++) {
4758360c73bdSStephen Cameron 		struct CommandList *c = h->cmd_pool + i;
4759360c73bdSStephen Cameron 
4760360c73bdSStephen Cameron 		hpsa_cmd_init(h, i, c);
4761360c73bdSStephen Cameron 		atomic_set(&c->refcount, 0);
4762360c73bdSStephen Cameron 	}
4763360c73bdSStephen Cameron }
4764360c73bdSStephen Cameron 
4765360c73bdSStephen Cameron static inline void hpsa_cmd_partial_init(struct ctlr_info *h, int index,
4766360c73bdSStephen Cameron 				struct CommandList *c)
4767360c73bdSStephen Cameron {
4768360c73bdSStephen Cameron 	dma_addr_t cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
4769360c73bdSStephen Cameron 
477073153fe5SWebb Scales 	BUG_ON(c->cmdindex != index);
477173153fe5SWebb Scales 
4772360c73bdSStephen Cameron 	memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
4773360c73bdSStephen Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
4774360c73bdSStephen Cameron 	c->busaddr = (u32) cmd_dma_handle;
4775360c73bdSStephen Cameron }
4776360c73bdSStephen Cameron 
4777592a0ad5SWebb Scales static int hpsa_ioaccel_submit(struct ctlr_info *h,
4778592a0ad5SWebb Scales 		struct CommandList *c, struct scsi_cmnd *cmd,
4779592a0ad5SWebb Scales 		unsigned char *scsi3addr)
4780592a0ad5SWebb Scales {
4781592a0ad5SWebb Scales 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
4782592a0ad5SWebb Scales 	int rc = IO_ACCEL_INELIGIBLE;
4783592a0ad5SWebb Scales 
4784592a0ad5SWebb Scales 	cmd->host_scribble = (unsigned char *) c;
4785592a0ad5SWebb Scales 
4786592a0ad5SWebb Scales 	if (dev->offload_enabled) {
4787592a0ad5SWebb Scales 		hpsa_cmd_init(h, c->cmdindex, c);
4788592a0ad5SWebb Scales 		c->cmd_type = CMD_SCSI;
4789592a0ad5SWebb Scales 		c->scsi_cmd = cmd;
4790592a0ad5SWebb Scales 		rc = hpsa_scsi_ioaccel_raid_map(h, c);
4791592a0ad5SWebb Scales 		if (rc < 0)     /* scsi_dma_map failed. */
4792592a0ad5SWebb Scales 			rc = SCSI_MLQUEUE_HOST_BUSY;
4793a3144e0bSJoe Handzik 	} else if (dev->hba_ioaccel_enabled) {
4794592a0ad5SWebb Scales 		hpsa_cmd_init(h, c->cmdindex, c);
4795592a0ad5SWebb Scales 		c->cmd_type = CMD_SCSI;
4796592a0ad5SWebb Scales 		c->scsi_cmd = cmd;
4797592a0ad5SWebb Scales 		rc = hpsa_scsi_ioaccel_direct_map(h, c);
4798592a0ad5SWebb Scales 		if (rc < 0)     /* scsi_dma_map failed. */
4799592a0ad5SWebb Scales 			rc = SCSI_MLQUEUE_HOST_BUSY;
4800592a0ad5SWebb Scales 	}
4801592a0ad5SWebb Scales 	return rc;
4802592a0ad5SWebb Scales }
4803592a0ad5SWebb Scales 
4804080ef1ccSDon Brace static void hpsa_command_resubmit_worker(struct work_struct *work)
4805080ef1ccSDon Brace {
4806080ef1ccSDon Brace 	struct scsi_cmnd *cmd;
4807080ef1ccSDon Brace 	struct hpsa_scsi_dev_t *dev;
48088a0ff92cSWebb Scales 	struct CommandList *c = container_of(work, struct CommandList, work);
4809080ef1ccSDon Brace 
4810080ef1ccSDon Brace 	cmd = c->scsi_cmd;
4811080ef1ccSDon Brace 	dev = cmd->device->hostdata;
4812080ef1ccSDon Brace 	if (!dev) {
4813080ef1ccSDon Brace 		cmd->result = DID_NO_CONNECT << 16;
48148a0ff92cSWebb Scales 		return hpsa_cmd_free_and_done(c->h, c, cmd);
4815080ef1ccSDon Brace 	}
4816d604f533SWebb Scales 	if (c->reset_pending)
4817d604f533SWebb Scales 		return hpsa_cmd_resolve_and_free(c->h, c);
4818a58e7e53SWebb Scales 	if (c->abort_pending)
4819a58e7e53SWebb Scales 		return hpsa_cmd_abort_and_free(c->h, c, cmd);
4820592a0ad5SWebb Scales 	if (c->cmd_type == CMD_IOACCEL2) {
4821592a0ad5SWebb Scales 		struct ctlr_info *h = c->h;
4822592a0ad5SWebb Scales 		struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
4823592a0ad5SWebb Scales 		int rc;
4824592a0ad5SWebb Scales 
4825592a0ad5SWebb Scales 		if (c2->error_data.serv_response ==
4826592a0ad5SWebb Scales 				IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL) {
4827592a0ad5SWebb Scales 			rc = hpsa_ioaccel_submit(h, c, cmd, dev->scsi3addr);
4828592a0ad5SWebb Scales 			if (rc == 0)
4829592a0ad5SWebb Scales 				return;
4830592a0ad5SWebb Scales 			if (rc == SCSI_MLQUEUE_HOST_BUSY) {
4831592a0ad5SWebb Scales 				/*
4832592a0ad5SWebb Scales 				 * If we get here, it means dma mapping failed.
4833592a0ad5SWebb Scales 				 * Try again via scsi mid layer, which will
4834592a0ad5SWebb Scales 				 * then get SCSI_MLQUEUE_HOST_BUSY.
4835592a0ad5SWebb Scales 				 */
4836592a0ad5SWebb Scales 				cmd->result = DID_IMM_RETRY << 16;
48378a0ff92cSWebb Scales 				return hpsa_cmd_free_and_done(h, c, cmd);
4838592a0ad5SWebb Scales 			}
4839592a0ad5SWebb Scales 			/* else, fall thru and resubmit down CISS path */
4840592a0ad5SWebb Scales 		}
4841592a0ad5SWebb Scales 	}
4842360c73bdSStephen Cameron 	hpsa_cmd_partial_init(c->h, c->cmdindex, c);
4843080ef1ccSDon Brace 	if (hpsa_ciss_submit(c->h, c, cmd, dev->scsi3addr)) {
4844080ef1ccSDon Brace 		/*
4845080ef1ccSDon Brace 		 * If we get here, it means dma mapping failed. Try
4846080ef1ccSDon Brace 		 * again via scsi mid layer, which will then get
4847080ef1ccSDon Brace 		 * SCSI_MLQUEUE_HOST_BUSY.
4848592a0ad5SWebb Scales 		 *
4849592a0ad5SWebb Scales 		 * hpsa_ciss_submit will have already freed c
4850592a0ad5SWebb Scales 		 * if it encountered a dma mapping failure.
4851080ef1ccSDon Brace 		 */
4852080ef1ccSDon Brace 		cmd->result = DID_IMM_RETRY << 16;
4853080ef1ccSDon Brace 		cmd->scsi_done(cmd);
4854080ef1ccSDon Brace 	}
4855080ef1ccSDon Brace }
4856080ef1ccSDon Brace 
4857574f05d3SStephen Cameron /* Running in struct Scsi_Host->host_lock less mode */
4858574f05d3SStephen Cameron static int hpsa_scsi_queue_command(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
4859574f05d3SStephen Cameron {
4860574f05d3SStephen Cameron 	struct ctlr_info *h;
4861574f05d3SStephen Cameron 	struct hpsa_scsi_dev_t *dev;
4862574f05d3SStephen Cameron 	unsigned char scsi3addr[8];
4863574f05d3SStephen Cameron 	struct CommandList *c;
4864574f05d3SStephen Cameron 	int rc = 0;
4865574f05d3SStephen Cameron 
4866574f05d3SStephen Cameron 	/* Get the ptr to our adapter structure out of cmd->host. */
4867574f05d3SStephen Cameron 	h = sdev_to_hba(cmd->device);
486873153fe5SWebb Scales 
486973153fe5SWebb Scales 	BUG_ON(cmd->request->tag < 0);
487073153fe5SWebb Scales 
4871574f05d3SStephen Cameron 	dev = cmd->device->hostdata;
4872574f05d3SStephen Cameron 	if (!dev) {
4873574f05d3SStephen Cameron 		cmd->result = DID_NO_CONNECT << 16;
4874574f05d3SStephen Cameron 		cmd->scsi_done(cmd);
4875574f05d3SStephen Cameron 		return 0;
4876574f05d3SStephen Cameron 	}
487773153fe5SWebb Scales 
4878574f05d3SStephen Cameron 	memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
4879574f05d3SStephen Cameron 
4880574f05d3SStephen Cameron 	if (unlikely(lockup_detected(h))) {
488125163bd5SWebb Scales 		cmd->result = DID_NO_CONNECT << 16;
4882574f05d3SStephen Cameron 		cmd->scsi_done(cmd);
4883574f05d3SStephen Cameron 		return 0;
4884574f05d3SStephen Cameron 	}
488573153fe5SWebb Scales 	c = cmd_tagged_alloc(h, cmd);
4886574f05d3SStephen Cameron 
4887407863cbSStephen Cameron 	/*
4888407863cbSStephen Cameron 	 * Call alternate submit routine for I/O accelerated commands.
4889574f05d3SStephen Cameron 	 * Retries always go down the normal I/O path.
4890574f05d3SStephen Cameron 	 */
4891574f05d3SStephen Cameron 	if (likely(cmd->retries == 0 &&
4892574f05d3SStephen Cameron 		cmd->request->cmd_type == REQ_TYPE_FS &&
4893574f05d3SStephen Cameron 		h->acciopath_status)) {
4894592a0ad5SWebb Scales 		rc = hpsa_ioaccel_submit(h, c, cmd, scsi3addr);
4895574f05d3SStephen Cameron 		if (rc == 0)
4896592a0ad5SWebb Scales 			return 0;
4897592a0ad5SWebb Scales 		if (rc == SCSI_MLQUEUE_HOST_BUSY) {
489873153fe5SWebb Scales 			hpsa_cmd_resolve_and_free(h, c);
4899574f05d3SStephen Cameron 			return SCSI_MLQUEUE_HOST_BUSY;
4900574f05d3SStephen Cameron 		}
4901574f05d3SStephen Cameron 	}
4902574f05d3SStephen Cameron 	return hpsa_ciss_submit(h, c, cmd, scsi3addr);
4903574f05d3SStephen Cameron }
4904574f05d3SStephen Cameron 
49058ebc9248SWebb Scales static void hpsa_scan_complete(struct ctlr_info *h)
49065f389360SStephen M. Cameron {
49075f389360SStephen M. Cameron 	unsigned long flags;
49085f389360SStephen M. Cameron 
49095f389360SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
49105f389360SStephen M. Cameron 	h->scan_finished = 1;
49115f389360SStephen M. Cameron 	wake_up_all(&h->scan_wait_queue);
49125f389360SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
49135f389360SStephen M. Cameron }
49145f389360SStephen M. Cameron 
4915a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *sh)
4916a08a8471SStephen M. Cameron {
4917a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
4918a08a8471SStephen M. Cameron 	unsigned long flags;
4919a08a8471SStephen M. Cameron 
49208ebc9248SWebb Scales 	/*
49218ebc9248SWebb Scales 	 * Don't let rescans be initiated on a controller known to be locked
49228ebc9248SWebb Scales 	 * up.  If the controller locks up *during* a rescan, that thread is
49238ebc9248SWebb Scales 	 * probably hosed, but at least we can prevent new rescan threads from
49248ebc9248SWebb Scales 	 * piling up on a locked up controller.
49258ebc9248SWebb Scales 	 */
49268ebc9248SWebb Scales 	if (unlikely(lockup_detected(h)))
49278ebc9248SWebb Scales 		return hpsa_scan_complete(h);
49285f389360SStephen M. Cameron 
4929a08a8471SStephen M. Cameron 	/* wait until any scan already in progress is finished. */
4930a08a8471SStephen M. Cameron 	while (1) {
4931a08a8471SStephen M. Cameron 		spin_lock_irqsave(&h->scan_lock, flags);
4932a08a8471SStephen M. Cameron 		if (h->scan_finished)
4933a08a8471SStephen M. Cameron 			break;
4934a08a8471SStephen M. Cameron 		spin_unlock_irqrestore(&h->scan_lock, flags);
4935a08a8471SStephen M. Cameron 		wait_event(h->scan_wait_queue, h->scan_finished);
4936a08a8471SStephen M. Cameron 		/* Note: We don't need to worry about a race between this
4937a08a8471SStephen M. Cameron 		 * thread and driver unload because the midlayer will
4938a08a8471SStephen M. Cameron 		 * have incremented the reference count, so unload won't
4939a08a8471SStephen M. Cameron 		 * happen if we're in here.
4940a08a8471SStephen M. Cameron 		 */
4941a08a8471SStephen M. Cameron 	}
4942a08a8471SStephen M. Cameron 	h->scan_finished = 0; /* mark scan as in progress */
4943a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4944a08a8471SStephen M. Cameron 
49458ebc9248SWebb Scales 	if (unlikely(lockup_detected(h)))
49468ebc9248SWebb Scales 		return hpsa_scan_complete(h);
49475f389360SStephen M. Cameron 
4948a08a8471SStephen M. Cameron 	hpsa_update_scsi_devices(h, h->scsi_host->host_no);
4949a08a8471SStephen M. Cameron 
49508ebc9248SWebb Scales 	hpsa_scan_complete(h);
4951a08a8471SStephen M. Cameron }
4952a08a8471SStephen M. Cameron 
49537c0a0229SDon Brace static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth)
49547c0a0229SDon Brace {
495503383736SDon Brace 	struct hpsa_scsi_dev_t *logical_drive = sdev->hostdata;
495603383736SDon Brace 
495703383736SDon Brace 	if (!logical_drive)
495803383736SDon Brace 		return -ENODEV;
49597c0a0229SDon Brace 
49607c0a0229SDon Brace 	if (qdepth < 1)
49617c0a0229SDon Brace 		qdepth = 1;
496203383736SDon Brace 	else if (qdepth > logical_drive->queue_depth)
496303383736SDon Brace 		qdepth = logical_drive->queue_depth;
496403383736SDon Brace 
496503383736SDon Brace 	return scsi_change_queue_depth(sdev, qdepth);
49667c0a0229SDon Brace }
49677c0a0229SDon Brace 
4968a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
4969a08a8471SStephen M. Cameron 	unsigned long elapsed_time)
4970a08a8471SStephen M. Cameron {
4971a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
4972a08a8471SStephen M. Cameron 	unsigned long flags;
4973a08a8471SStephen M. Cameron 	int finished;
4974a08a8471SStephen M. Cameron 
4975a08a8471SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
4976a08a8471SStephen M. Cameron 	finished = h->scan_finished;
4977a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
4978a08a8471SStephen M. Cameron 	return finished;
4979a08a8471SStephen M. Cameron }
4980a08a8471SStephen M. Cameron 
49812946e82bSRobert Elliott static int hpsa_scsi_host_alloc(struct ctlr_info *h)
4982edd16368SStephen M. Cameron {
4983b705690dSStephen M. Cameron 	struct Scsi_Host *sh;
4984b705690dSStephen M. Cameron 	int error;
4985edd16368SStephen M. Cameron 
4986b705690dSStephen M. Cameron 	sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
49872946e82bSRobert Elliott 	if (sh == NULL) {
49882946e82bSRobert Elliott 		dev_err(&h->pdev->dev, "scsi_host_alloc failed\n");
49892946e82bSRobert Elliott 		return -ENOMEM;
49902946e82bSRobert Elliott 	}
4991b705690dSStephen M. Cameron 
4992b705690dSStephen M. Cameron 	sh->io_port = 0;
4993b705690dSStephen M. Cameron 	sh->n_io_port = 0;
4994b705690dSStephen M. Cameron 	sh->this_id = -1;
4995b705690dSStephen M. Cameron 	sh->max_channel = 3;
4996b705690dSStephen M. Cameron 	sh->max_cmd_len = MAX_COMMAND_SIZE;
4997b705690dSStephen M. Cameron 	sh->max_lun = HPSA_MAX_LUN;
4998b705690dSStephen M. Cameron 	sh->max_id = HPSA_MAX_LUN;
499941ce4c35SStephen Cameron 	sh->can_queue = h->nr_cmds - HPSA_NRESERVED_CMDS;
5000d54c5c24SStephen Cameron 	sh->cmd_per_lun = sh->can_queue;
5001b705690dSStephen M. Cameron 	sh->sg_tablesize = h->maxsgentries;
5002b705690dSStephen M. Cameron 	sh->hostdata[0] = (unsigned long) h;
5003b705690dSStephen M. Cameron 	sh->irq = h->intr[h->intr_mode];
5004b705690dSStephen M. Cameron 	sh->unique_id = sh->irq;
500573153fe5SWebb Scales 	error = scsi_init_shared_tag_map(sh, sh->can_queue);
500673153fe5SWebb Scales 	if (error) {
500773153fe5SWebb Scales 		dev_err(&h->pdev->dev,
500873153fe5SWebb Scales 			"%s: scsi_init_shared_tag_map failed for controller %d\n",
500973153fe5SWebb Scales 			__func__, h->ctlr);
5010b705690dSStephen M. Cameron 			scsi_host_put(sh);
5011b705690dSStephen M. Cameron 			return error;
50122946e82bSRobert Elliott 	}
50132946e82bSRobert Elliott 	h->scsi_host = sh;
50142946e82bSRobert Elliott 	return 0;
50152946e82bSRobert Elliott }
50162946e82bSRobert Elliott 
50172946e82bSRobert Elliott static int hpsa_scsi_add_host(struct ctlr_info *h)
50182946e82bSRobert Elliott {
50192946e82bSRobert Elliott 	int rv;
50202946e82bSRobert Elliott 
50212946e82bSRobert Elliott 	rv = scsi_add_host(h->scsi_host, &h->pdev->dev);
50222946e82bSRobert Elliott 	if (rv) {
50232946e82bSRobert Elliott 		dev_err(&h->pdev->dev, "scsi_add_host failed\n");
50242946e82bSRobert Elliott 		return rv;
50252946e82bSRobert Elliott 	}
50262946e82bSRobert Elliott 	scsi_scan_host(h->scsi_host);
50272946e82bSRobert Elliott 	return 0;
5028edd16368SStephen M. Cameron }
5029edd16368SStephen M. Cameron 
5030b69324ffSWebb Scales /*
503173153fe5SWebb Scales  * The block layer has already gone to the trouble of picking out a unique,
503273153fe5SWebb Scales  * small-integer tag for this request.  We use an offset from that value as
503373153fe5SWebb Scales  * an index to select our command block.  (The offset allows us to reserve the
503473153fe5SWebb Scales  * low-numbered entries for our own uses.)
503573153fe5SWebb Scales  */
503673153fe5SWebb Scales static int hpsa_get_cmd_index(struct scsi_cmnd *scmd)
503773153fe5SWebb Scales {
503873153fe5SWebb Scales 	int idx = scmd->request->tag;
503973153fe5SWebb Scales 
504073153fe5SWebb Scales 	if (idx < 0)
504173153fe5SWebb Scales 		return idx;
504273153fe5SWebb Scales 
504373153fe5SWebb Scales 	/* Offset to leave space for internal cmds. */
504473153fe5SWebb Scales 	return idx += HPSA_NRESERVED_CMDS;
504573153fe5SWebb Scales }
504673153fe5SWebb Scales 
504773153fe5SWebb Scales /*
5048b69324ffSWebb Scales  * Send a TEST_UNIT_READY command to the specified LUN using the specified
5049b69324ffSWebb Scales  * reply queue; returns zero if the unit is ready, and non-zero otherwise.
5050b69324ffSWebb Scales  */
5051b69324ffSWebb Scales static int hpsa_send_test_unit_ready(struct ctlr_info *h,
5052b69324ffSWebb Scales 				struct CommandList *c, unsigned char lunaddr[],
5053b69324ffSWebb Scales 				int reply_queue)
5054edd16368SStephen M. Cameron {
50558919358eSTomas Henzl 	int rc;
5056edd16368SStephen M. Cameron 
5057a2dac136SStephen M. Cameron 	/* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
5058a2dac136SStephen M. Cameron 	(void) fill_cmd(c, TEST_UNIT_READY, h,
5059a2dac136SStephen M. Cameron 			NULL, 0, 0, lunaddr, TYPE_CMD);
5060b69324ffSWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
506125163bd5SWebb Scales 	if (rc)
5062b69324ffSWebb Scales 		return rc;
5063edd16368SStephen M. Cameron 	/* no unmap needed here because no data xfer. */
5064edd16368SStephen M. Cameron 
5065b69324ffSWebb Scales 	/* Check if the unit is already ready. */
5066edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus == CMD_SUCCESS)
5067b69324ffSWebb Scales 		return 0;
5068edd16368SStephen M. Cameron 
5069b69324ffSWebb Scales 	/*
5070b69324ffSWebb Scales 	 * The first command sent after reset will receive "unit attention" to
5071b69324ffSWebb Scales 	 * indicate that the LUN has been reset...this is actually what we're
5072b69324ffSWebb Scales 	 * looking for (but, success is good too).
5073b69324ffSWebb Scales 	 */
5074edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
5075edd16368SStephen M. Cameron 		c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
5076edd16368SStephen M. Cameron 			(c->err_info->SenseInfo[2] == NO_SENSE ||
5077edd16368SStephen M. Cameron 			 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
5078b69324ffSWebb Scales 		return 0;
5079b69324ffSWebb Scales 
5080b69324ffSWebb Scales 	return 1;
5081b69324ffSWebb Scales }
5082b69324ffSWebb Scales 
5083b69324ffSWebb Scales /*
5084b69324ffSWebb Scales  * Wait for a TEST_UNIT_READY command to complete, retrying as necessary;
5085b69324ffSWebb Scales  * returns zero when the unit is ready, and non-zero when giving up.
5086b69324ffSWebb Scales  */
5087b69324ffSWebb Scales static int hpsa_wait_for_test_unit_ready(struct ctlr_info *h,
5088b69324ffSWebb Scales 				struct CommandList *c,
5089b69324ffSWebb Scales 				unsigned char lunaddr[], int reply_queue)
5090b69324ffSWebb Scales {
5091b69324ffSWebb Scales 	int rc;
5092b69324ffSWebb Scales 	int count = 0;
5093b69324ffSWebb Scales 	int waittime = 1; /* seconds */
5094b69324ffSWebb Scales 
5095b69324ffSWebb Scales 	/* Send test unit ready until device ready, or give up. */
5096b69324ffSWebb Scales 	for (count = 0; count < HPSA_TUR_RETRY_LIMIT; count++) {
5097b69324ffSWebb Scales 
5098b69324ffSWebb Scales 		/*
5099b69324ffSWebb Scales 		 * Wait for a bit.  do this first, because if we send
5100b69324ffSWebb Scales 		 * the TUR right away, the reset will just abort it.
5101b69324ffSWebb Scales 		 */
5102b69324ffSWebb Scales 		msleep(1000 * waittime);
5103b69324ffSWebb Scales 
5104b69324ffSWebb Scales 		rc = hpsa_send_test_unit_ready(h, c, lunaddr, reply_queue);
5105b69324ffSWebb Scales 		if (!rc)
5106edd16368SStephen M. Cameron 			break;
5107b69324ffSWebb Scales 
5108b69324ffSWebb Scales 		/* Increase wait time with each try, up to a point. */
5109b69324ffSWebb Scales 		if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
5110b69324ffSWebb Scales 			waittime *= 2;
5111b69324ffSWebb Scales 
5112b69324ffSWebb Scales 		dev_warn(&h->pdev->dev,
5113b69324ffSWebb Scales 			 "waiting %d secs for device to become ready.\n",
5114b69324ffSWebb Scales 			 waittime);
5115b69324ffSWebb Scales 	}
5116b69324ffSWebb Scales 
5117b69324ffSWebb Scales 	return rc;
5118b69324ffSWebb Scales }
5119b69324ffSWebb Scales 
5120b69324ffSWebb Scales static int wait_for_device_to_become_ready(struct ctlr_info *h,
5121b69324ffSWebb Scales 					   unsigned char lunaddr[],
5122b69324ffSWebb Scales 					   int reply_queue)
5123b69324ffSWebb Scales {
5124b69324ffSWebb Scales 	int first_queue;
5125b69324ffSWebb Scales 	int last_queue;
5126b69324ffSWebb Scales 	int rq;
5127b69324ffSWebb Scales 	int rc = 0;
5128b69324ffSWebb Scales 	struct CommandList *c;
5129b69324ffSWebb Scales 
5130b69324ffSWebb Scales 	c = cmd_alloc(h);
5131b69324ffSWebb Scales 
5132b69324ffSWebb Scales 	/*
5133b69324ffSWebb Scales 	 * If no specific reply queue was requested, then send the TUR
5134b69324ffSWebb Scales 	 * repeatedly, requesting a reply on each reply queue; otherwise execute
5135b69324ffSWebb Scales 	 * the loop exactly once using only the specified queue.
5136b69324ffSWebb Scales 	 */
5137b69324ffSWebb Scales 	if (reply_queue == DEFAULT_REPLY_QUEUE) {
5138b69324ffSWebb Scales 		first_queue = 0;
5139b69324ffSWebb Scales 		last_queue = h->nreply_queues - 1;
5140b69324ffSWebb Scales 	} else {
5141b69324ffSWebb Scales 		first_queue = reply_queue;
5142b69324ffSWebb Scales 		last_queue = reply_queue;
5143b69324ffSWebb Scales 	}
5144b69324ffSWebb Scales 
5145b69324ffSWebb Scales 	for (rq = first_queue; rq <= last_queue; rq++) {
5146b69324ffSWebb Scales 		rc = hpsa_wait_for_test_unit_ready(h, c, lunaddr, rq);
5147b69324ffSWebb Scales 		if (rc)
5148b69324ffSWebb Scales 			break;
5149edd16368SStephen M. Cameron 	}
5150edd16368SStephen M. Cameron 
5151edd16368SStephen M. Cameron 	if (rc)
5152edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "giving up on device.\n");
5153edd16368SStephen M. Cameron 	else
5154edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "device is ready.\n");
5155edd16368SStephen M. Cameron 
515645fcb86eSStephen Cameron 	cmd_free(h, c);
5157edd16368SStephen M. Cameron 	return rc;
5158edd16368SStephen M. Cameron }
5159edd16368SStephen M. Cameron 
5160edd16368SStephen M. Cameron /* Need at least one of these error handlers to keep ../scsi/hosts.c from
5161edd16368SStephen M. Cameron  * complaining.  Doing a host- or bus-reset can't do anything good here.
5162edd16368SStephen M. Cameron  */
5163edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
5164edd16368SStephen M. Cameron {
5165edd16368SStephen M. Cameron 	int rc;
5166edd16368SStephen M. Cameron 	struct ctlr_info *h;
5167edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
51682dc127bbSDan Carpenter 	char msg[48];
5169edd16368SStephen M. Cameron 
5170edd16368SStephen M. Cameron 	/* find the controller to which the command to be aborted was sent */
5171edd16368SStephen M. Cameron 	h = sdev_to_hba(scsicmd->device);
5172edd16368SStephen M. Cameron 	if (h == NULL) /* paranoia */
5173edd16368SStephen M. Cameron 		return FAILED;
5174e345893bSDon Brace 
5175e345893bSDon Brace 	if (lockup_detected(h))
5176e345893bSDon Brace 		return FAILED;
5177e345893bSDon Brace 
5178edd16368SStephen M. Cameron 	dev = scsicmd->device->hostdata;
5179edd16368SStephen M. Cameron 	if (!dev) {
5180d604f533SWebb Scales 		dev_err(&h->pdev->dev, "%s: device lookup failed\n", __func__);
5181edd16368SStephen M. Cameron 		return FAILED;
5182edd16368SStephen M. Cameron 	}
518325163bd5SWebb Scales 
518425163bd5SWebb Scales 	/* if controller locked up, we can guarantee command won't complete */
518525163bd5SWebb Scales 	if (lockup_detected(h)) {
51862dc127bbSDan Carpenter 		snprintf(msg, sizeof(msg),
51872dc127bbSDan Carpenter 			 "cmd %d RESET FAILED, lockup detected",
518873153fe5SWebb Scales 			 hpsa_get_cmd_index(scsicmd));
518973153fe5SWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
519025163bd5SWebb Scales 		return FAILED;
519125163bd5SWebb Scales 	}
519225163bd5SWebb Scales 
519325163bd5SWebb Scales 	/* this reset request might be the result of a lockup; check */
519425163bd5SWebb Scales 	if (detect_controller_lockup(h)) {
51952dc127bbSDan Carpenter 		snprintf(msg, sizeof(msg),
51962dc127bbSDan Carpenter 			 "cmd %d RESET FAILED, new lockup detected",
519773153fe5SWebb Scales 			 hpsa_get_cmd_index(scsicmd));
519873153fe5SWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
519925163bd5SWebb Scales 		return FAILED;
520025163bd5SWebb Scales 	}
520125163bd5SWebb Scales 
5202d604f533SWebb Scales 	/* Do not attempt on controller */
5203d604f533SWebb Scales 	if (is_hba_lunid(dev->scsi3addr))
5204d604f533SWebb Scales 		return SUCCESS;
5205d604f533SWebb Scales 
520625163bd5SWebb Scales 	hpsa_show_dev_msg(KERN_WARNING, h, dev, "resetting");
520725163bd5SWebb Scales 
5208edd16368SStephen M. Cameron 	/* send a reset to the SCSI LUN which the command was sent to */
5209d604f533SWebb Scales 	rc = hpsa_do_reset(h, dev, dev->scsi3addr, HPSA_RESET_TYPE_LUN,
521025163bd5SWebb Scales 			   DEFAULT_REPLY_QUEUE);
52112dc127bbSDan Carpenter 	snprintf(msg, sizeof(msg), "reset %s",
52122dc127bbSDan Carpenter 		 rc == 0 ? "completed successfully" : "failed");
5213d604f533SWebb Scales 	hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
5214d604f533SWebb Scales 	return rc == 0 ? SUCCESS : FAILED;
5215edd16368SStephen M. Cameron }
5216edd16368SStephen M. Cameron 
52176cba3f19SStephen M. Cameron static void swizzle_abort_tag(u8 *tag)
52186cba3f19SStephen M. Cameron {
52196cba3f19SStephen M. Cameron 	u8 original_tag[8];
52206cba3f19SStephen M. Cameron 
52216cba3f19SStephen M. Cameron 	memcpy(original_tag, tag, 8);
52226cba3f19SStephen M. Cameron 	tag[0] = original_tag[3];
52236cba3f19SStephen M. Cameron 	tag[1] = original_tag[2];
52246cba3f19SStephen M. Cameron 	tag[2] = original_tag[1];
52256cba3f19SStephen M. Cameron 	tag[3] = original_tag[0];
52266cba3f19SStephen M. Cameron 	tag[4] = original_tag[7];
52276cba3f19SStephen M. Cameron 	tag[5] = original_tag[6];
52286cba3f19SStephen M. Cameron 	tag[6] = original_tag[5];
52296cba3f19SStephen M. Cameron 	tag[7] = original_tag[4];
52306cba3f19SStephen M. Cameron }
52316cba3f19SStephen M. Cameron 
523217eb87d2SScott Teel static void hpsa_get_tag(struct ctlr_info *h,
52332b08b3e9SDon Brace 	struct CommandList *c, __le32 *taglower, __le32 *tagupper)
523417eb87d2SScott Teel {
52352b08b3e9SDon Brace 	u64 tag;
523617eb87d2SScott Teel 	if (c->cmd_type == CMD_IOACCEL1) {
523717eb87d2SScott Teel 		struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
523817eb87d2SScott Teel 			&h->ioaccel_cmd_pool[c->cmdindex];
52392b08b3e9SDon Brace 		tag = le64_to_cpu(cm1->tag);
52402b08b3e9SDon Brace 		*tagupper = cpu_to_le32(tag >> 32);
52412b08b3e9SDon Brace 		*taglower = cpu_to_le32(tag);
524254b6e9e9SScott Teel 		return;
524354b6e9e9SScott Teel 	}
524454b6e9e9SScott Teel 	if (c->cmd_type == CMD_IOACCEL2) {
524554b6e9e9SScott Teel 		struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
524654b6e9e9SScott Teel 			&h->ioaccel2_cmd_pool[c->cmdindex];
5247dd0e19f3SScott Teel 		/* upper tag not used in ioaccel2 mode */
5248dd0e19f3SScott Teel 		memset(tagupper, 0, sizeof(*tagupper));
5249dd0e19f3SScott Teel 		*taglower = cm2->Tag;
525054b6e9e9SScott Teel 		return;
525154b6e9e9SScott Teel 	}
52522b08b3e9SDon Brace 	tag = le64_to_cpu(c->Header.tag);
52532b08b3e9SDon Brace 	*tagupper = cpu_to_le32(tag >> 32);
52542b08b3e9SDon Brace 	*taglower = cpu_to_le32(tag);
525517eb87d2SScott Teel }
525654b6e9e9SScott Teel 
525775167d2cSStephen M. Cameron static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
52589b5c48c2SStephen Cameron 	struct CommandList *abort, int reply_queue)
525975167d2cSStephen M. Cameron {
526075167d2cSStephen M. Cameron 	int rc = IO_OK;
526175167d2cSStephen M. Cameron 	struct CommandList *c;
526275167d2cSStephen M. Cameron 	struct ErrorInfo *ei;
52632b08b3e9SDon Brace 	__le32 tagupper, taglower;
526475167d2cSStephen M. Cameron 
526545fcb86eSStephen Cameron 	c = cmd_alloc(h);
526675167d2cSStephen M. Cameron 
5267a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no buffer to map */
52689b5c48c2SStephen Cameron 	(void) fill_cmd(c, HPSA_ABORT_MSG, h, &abort->Header.tag,
5269a2dac136SStephen M. Cameron 		0, 0, scsi3addr, TYPE_MSG);
52709b5c48c2SStephen Cameron 	if (h->needs_abort_tags_swizzled)
52716cba3f19SStephen M. Cameron 		swizzle_abort_tag(&c->Request.CDB[4]);
527225163bd5SWebb Scales 	(void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
527317eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
527425163bd5SWebb Scales 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd(abort) completed.\n",
527517eb87d2SScott Teel 		__func__, tagupper, taglower);
527675167d2cSStephen M. Cameron 	/* no unmap needed here because no data xfer. */
527775167d2cSStephen M. Cameron 
527875167d2cSStephen M. Cameron 	ei = c->err_info;
527975167d2cSStephen M. Cameron 	switch (ei->CommandStatus) {
528075167d2cSStephen M. Cameron 	case CMD_SUCCESS:
528175167d2cSStephen M. Cameron 		break;
52829437ac43SStephen Cameron 	case CMD_TMF_STATUS:
52839437ac43SStephen Cameron 		rc = hpsa_evaluate_tmf_status(h, c);
52849437ac43SStephen Cameron 		break;
528575167d2cSStephen M. Cameron 	case CMD_UNABORTABLE: /* Very common, don't make noise. */
528675167d2cSStephen M. Cameron 		rc = -1;
528775167d2cSStephen M. Cameron 		break;
528875167d2cSStephen M. Cameron 	default:
528975167d2cSStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
529017eb87d2SScott Teel 			__func__, tagupper, taglower);
5291d1e8beacSStephen M. Cameron 		hpsa_scsi_interpret_error(h, c);
529275167d2cSStephen M. Cameron 		rc = -1;
529375167d2cSStephen M. Cameron 		break;
529475167d2cSStephen M. Cameron 	}
529545fcb86eSStephen Cameron 	cmd_free(h, c);
5296dd0e19f3SScott Teel 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n",
5297dd0e19f3SScott Teel 		__func__, tagupper, taglower);
529875167d2cSStephen M. Cameron 	return rc;
529975167d2cSStephen M. Cameron }
530075167d2cSStephen M. Cameron 
53018be986ccSStephen Cameron static void setup_ioaccel2_abort_cmd(struct CommandList *c, struct ctlr_info *h,
53028be986ccSStephen Cameron 	struct CommandList *command_to_abort, int reply_queue)
53038be986ccSStephen Cameron {
53048be986ccSStephen Cameron 	struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
53058be986ccSStephen Cameron 	struct hpsa_tmf_struct *ac = (struct hpsa_tmf_struct *) c2;
53068be986ccSStephen Cameron 	struct io_accel2_cmd *c2a =
53078be986ccSStephen Cameron 		&h->ioaccel2_cmd_pool[command_to_abort->cmdindex];
5308a58e7e53SWebb Scales 	struct scsi_cmnd *scmd = command_to_abort->scsi_cmd;
53098be986ccSStephen Cameron 	struct hpsa_scsi_dev_t *dev = scmd->device->hostdata;
53108be986ccSStephen Cameron 
53118be986ccSStephen Cameron 	/*
53128be986ccSStephen Cameron 	 * We're overlaying struct hpsa_tmf_struct on top of something which
53138be986ccSStephen Cameron 	 * was allocated as a struct io_accel2_cmd, so we better be sure it
53148be986ccSStephen Cameron 	 * actually fits, and doesn't overrun the error info space.
53158be986ccSStephen Cameron 	 */
53168be986ccSStephen Cameron 	BUILD_BUG_ON(sizeof(struct hpsa_tmf_struct) >
53178be986ccSStephen Cameron 			sizeof(struct io_accel2_cmd));
53188be986ccSStephen Cameron 	BUG_ON(offsetof(struct io_accel2_cmd, error_data) <
53198be986ccSStephen Cameron 			offsetof(struct hpsa_tmf_struct, error_len) +
53208be986ccSStephen Cameron 				sizeof(ac->error_len));
53218be986ccSStephen Cameron 
53228be986ccSStephen Cameron 	c->cmd_type = IOACCEL2_TMF;
5323a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_BUSY;
5324a58e7e53SWebb Scales 
53258be986ccSStephen Cameron 	/* Adjust the DMA address to point to the accelerated command buffer */
53268be986ccSStephen Cameron 	c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
53278be986ccSStephen Cameron 				(c->cmdindex * sizeof(struct io_accel2_cmd));
53288be986ccSStephen Cameron 	BUG_ON(c->busaddr & 0x0000007F);
53298be986ccSStephen Cameron 
53308be986ccSStephen Cameron 	memset(ac, 0, sizeof(*c2)); /* yes this is correct */
53318be986ccSStephen Cameron 	ac->iu_type = IOACCEL2_IU_TMF_TYPE;
53328be986ccSStephen Cameron 	ac->reply_queue = reply_queue;
53338be986ccSStephen Cameron 	ac->tmf = IOACCEL2_TMF_ABORT;
53348be986ccSStephen Cameron 	ac->it_nexus = cpu_to_le32(dev->ioaccel_handle);
53358be986ccSStephen Cameron 	memset(ac->lun_id, 0, sizeof(ac->lun_id));
53368be986ccSStephen Cameron 	ac->tag = cpu_to_le64(c->cmdindex << DIRECT_LOOKUP_SHIFT);
53378be986ccSStephen Cameron 	ac->abort_tag = cpu_to_le64(le32_to_cpu(c2a->Tag));
53388be986ccSStephen Cameron 	ac->error_ptr = cpu_to_le64(c->busaddr +
53398be986ccSStephen Cameron 			offsetof(struct io_accel2_cmd, error_data));
53408be986ccSStephen Cameron 	ac->error_len = cpu_to_le32(sizeof(c2->error_data));
53418be986ccSStephen Cameron }
53428be986ccSStephen Cameron 
534354b6e9e9SScott Teel /* ioaccel2 path firmware cannot handle abort task requests.
534454b6e9e9SScott Teel  * Change abort requests to physical target reset, and send to the
534554b6e9e9SScott Teel  * address of the physical disk used for the ioaccel 2 command.
534654b6e9e9SScott Teel  * Return 0 on success (IO_OK)
534754b6e9e9SScott Teel  *	 -1 on failure
534854b6e9e9SScott Teel  */
534954b6e9e9SScott Teel 
535054b6e9e9SScott Teel static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
535125163bd5SWebb Scales 	unsigned char *scsi3addr, struct CommandList *abort, int reply_queue)
535254b6e9e9SScott Teel {
535354b6e9e9SScott Teel 	int rc = IO_OK;
535454b6e9e9SScott Teel 	struct scsi_cmnd *scmd; /* scsi command within request being aborted */
535554b6e9e9SScott Teel 	struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
535654b6e9e9SScott Teel 	unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
535754b6e9e9SScott Teel 	unsigned char *psa = &phys_scsi3addr[0];
535854b6e9e9SScott Teel 
535954b6e9e9SScott Teel 	/* Get a pointer to the hpsa logical device. */
53607fa3030cSStephen Cameron 	scmd = abort->scsi_cmd;
536154b6e9e9SScott Teel 	dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
536254b6e9e9SScott Teel 	if (dev == NULL) {
536354b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
536454b6e9e9SScott Teel 			"Cannot abort: no device pointer for command.\n");
536554b6e9e9SScott Teel 			return -1; /* not abortable */
536654b6e9e9SScott Teel 	}
536754b6e9e9SScott Teel 
53682ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
53692ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
53700d96ef5fSWebb Scales 			"scsi %d:%d:%d:%d %s scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
53712ba8bfc8SStephen M. Cameron 			h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
53720d96ef5fSWebb Scales 			"Reset as abort",
53732ba8bfc8SStephen M. Cameron 			scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
53742ba8bfc8SStephen M. Cameron 			scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]);
53752ba8bfc8SStephen M. Cameron 
537654b6e9e9SScott Teel 	if (!dev->offload_enabled) {
537754b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
537854b6e9e9SScott Teel 			"Can't abort: device is not operating in HP SSD Smart Path mode.\n");
537954b6e9e9SScott Teel 		return -1; /* not abortable */
538054b6e9e9SScott Teel 	}
538154b6e9e9SScott Teel 
538254b6e9e9SScott Teel 	/* Incoming scsi3addr is logical addr. We need physical disk addr. */
538354b6e9e9SScott Teel 	if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
538454b6e9e9SScott Teel 		dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
538554b6e9e9SScott Teel 		return -1; /* not abortable */
538654b6e9e9SScott Teel 	}
538754b6e9e9SScott Teel 
538854b6e9e9SScott Teel 	/* send the reset */
53892ba8bfc8SStephen M. Cameron 	if (h->raid_offload_debug > 0)
53902ba8bfc8SStephen M. Cameron 		dev_info(&h->pdev->dev,
53912ba8bfc8SStephen M. Cameron 			"Reset as abort: Resetting physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
53922ba8bfc8SStephen M. Cameron 			psa[0], psa[1], psa[2], psa[3],
53932ba8bfc8SStephen M. Cameron 			psa[4], psa[5], psa[6], psa[7]);
5394d604f533SWebb Scales 	rc = hpsa_do_reset(h, dev, psa, HPSA_RESET_TYPE_TARGET, reply_queue);
539554b6e9e9SScott Teel 	if (rc != 0) {
539654b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
539754b6e9e9SScott Teel 			"Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
539854b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
539954b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
540054b6e9e9SScott Teel 		return rc; /* failed to reset */
540154b6e9e9SScott Teel 	}
540254b6e9e9SScott Teel 
540354b6e9e9SScott Teel 	/* wait for device to recover */
5404b69324ffSWebb Scales 	if (wait_for_device_to_become_ready(h, psa, reply_queue) != 0) {
540554b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
540654b6e9e9SScott Teel 			"Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
540754b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
540854b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
540954b6e9e9SScott Teel 		return -1;  /* failed to recover */
541054b6e9e9SScott Teel 	}
541154b6e9e9SScott Teel 
541254b6e9e9SScott Teel 	/* device recovered */
541354b6e9e9SScott Teel 	dev_info(&h->pdev->dev,
541454b6e9e9SScott Teel 		"Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
541554b6e9e9SScott Teel 		psa[0], psa[1], psa[2], psa[3],
541654b6e9e9SScott Teel 		psa[4], psa[5], psa[6], psa[7]);
541754b6e9e9SScott Teel 
541854b6e9e9SScott Teel 	return rc; /* success */
541954b6e9e9SScott Teel }
542054b6e9e9SScott Teel 
54218be986ccSStephen Cameron static int hpsa_send_abort_ioaccel2(struct ctlr_info *h,
54228be986ccSStephen Cameron 	struct CommandList *abort, int reply_queue)
54238be986ccSStephen Cameron {
54248be986ccSStephen Cameron 	int rc = IO_OK;
54258be986ccSStephen Cameron 	struct CommandList *c;
54268be986ccSStephen Cameron 	__le32 taglower, tagupper;
54278be986ccSStephen Cameron 	struct hpsa_scsi_dev_t *dev;
54288be986ccSStephen Cameron 	struct io_accel2_cmd *c2;
54298be986ccSStephen Cameron 
54308be986ccSStephen Cameron 	dev = abort->scsi_cmd->device->hostdata;
54318be986ccSStephen Cameron 	if (!dev->offload_enabled && !dev->hba_ioaccel_enabled)
54328be986ccSStephen Cameron 		return -1;
54338be986ccSStephen Cameron 
54348be986ccSStephen Cameron 	c = cmd_alloc(h);
54358be986ccSStephen Cameron 	setup_ioaccel2_abort_cmd(c, h, abort, reply_queue);
54368be986ccSStephen Cameron 	c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
54378be986ccSStephen Cameron 	(void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
54388be986ccSStephen Cameron 	hpsa_get_tag(h, abort, &taglower, &tagupper);
54398be986ccSStephen Cameron 	dev_dbg(&h->pdev->dev,
54408be986ccSStephen Cameron 		"%s: Tag:0x%08x:%08x: do_simple_cmd(ioaccel2 abort) completed.\n",
54418be986ccSStephen Cameron 		__func__, tagupper, taglower);
54428be986ccSStephen Cameron 	/* no unmap needed here because no data xfer. */
54438be986ccSStephen Cameron 
54448be986ccSStephen Cameron 	dev_dbg(&h->pdev->dev,
54458be986ccSStephen Cameron 		"%s: Tag:0x%08x:%08x: abort service response = 0x%02x.\n",
54468be986ccSStephen Cameron 		__func__, tagupper, taglower, c2->error_data.serv_response);
54478be986ccSStephen Cameron 	switch (c2->error_data.serv_response) {
54488be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
54498be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
54508be986ccSStephen Cameron 		rc = 0;
54518be986ccSStephen Cameron 		break;
54528be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
54538be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_FAILURE:
54548be986ccSStephen Cameron 	case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
54558be986ccSStephen Cameron 		rc = -1;
54568be986ccSStephen Cameron 		break;
54578be986ccSStephen Cameron 	default:
54588be986ccSStephen Cameron 		dev_warn(&h->pdev->dev,
54598be986ccSStephen Cameron 			"%s: Tag:0x%08x:%08x: unknown abort service response 0x%02x\n",
54608be986ccSStephen Cameron 			__func__, tagupper, taglower,
54618be986ccSStephen Cameron 			c2->error_data.serv_response);
54628be986ccSStephen Cameron 		rc = -1;
54638be986ccSStephen Cameron 	}
54648be986ccSStephen Cameron 	cmd_free(h, c);
54658be986ccSStephen Cameron 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n", __func__,
54668be986ccSStephen Cameron 		tagupper, taglower);
54678be986ccSStephen Cameron 	return rc;
54688be986ccSStephen Cameron }
54698be986ccSStephen Cameron 
54706cba3f19SStephen M. Cameron static int hpsa_send_abort_both_ways(struct ctlr_info *h,
547125163bd5SWebb Scales 	unsigned char *scsi3addr, struct CommandList *abort, int reply_queue)
54726cba3f19SStephen M. Cameron {
54738be986ccSStephen Cameron 	/*
54748be986ccSStephen Cameron 	 * ioccelerator mode 2 commands should be aborted via the
547554b6e9e9SScott Teel 	 * accelerated path, since RAID path is unaware of these commands,
54768be986ccSStephen Cameron 	 * but not all underlying firmware can handle abort TMF.
54778be986ccSStephen Cameron 	 * Change abort to physical device reset when abort TMF is unsupported.
547854b6e9e9SScott Teel 	 */
54798be986ccSStephen Cameron 	if (abort->cmd_type == CMD_IOACCEL2) {
54808be986ccSStephen Cameron 		if (HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags)
54818be986ccSStephen Cameron 			return hpsa_send_abort_ioaccel2(h, abort,
54828be986ccSStephen Cameron 						reply_queue);
54838be986ccSStephen Cameron 		else
548425163bd5SWebb Scales 			return hpsa_send_reset_as_abort_ioaccel2(h, scsi3addr,
548525163bd5SWebb Scales 							abort, reply_queue);
54868be986ccSStephen Cameron 	}
54879b5c48c2SStephen Cameron 	return hpsa_send_abort(h, scsi3addr, abort, reply_queue);
548825163bd5SWebb Scales }
548925163bd5SWebb Scales 
549025163bd5SWebb Scales /* Find out which reply queue a command was meant to return on */
549125163bd5SWebb Scales static int hpsa_extract_reply_queue(struct ctlr_info *h,
549225163bd5SWebb Scales 					struct CommandList *c)
549325163bd5SWebb Scales {
549425163bd5SWebb Scales 	if (c->cmd_type == CMD_IOACCEL2)
549525163bd5SWebb Scales 		return h->ioaccel2_cmd_pool[c->cmdindex].reply_queue;
549625163bd5SWebb Scales 	return c->Header.ReplyQueue;
54976cba3f19SStephen M. Cameron }
54986cba3f19SStephen M. Cameron 
54999b5c48c2SStephen Cameron /*
55009b5c48c2SStephen Cameron  * Limit concurrency of abort commands to prevent
55019b5c48c2SStephen Cameron  * over-subscription of commands
55029b5c48c2SStephen Cameron  */
55039b5c48c2SStephen Cameron static inline int wait_for_available_abort_cmd(struct ctlr_info *h)
55049b5c48c2SStephen Cameron {
55059b5c48c2SStephen Cameron #define ABORT_CMD_WAIT_MSECS 5000
55069b5c48c2SStephen Cameron 	return !wait_event_timeout(h->abort_cmd_wait_queue,
55079b5c48c2SStephen Cameron 			atomic_dec_if_positive(&h->abort_cmds_available) >= 0,
55089b5c48c2SStephen Cameron 			msecs_to_jiffies(ABORT_CMD_WAIT_MSECS));
55099b5c48c2SStephen Cameron }
55109b5c48c2SStephen Cameron 
551175167d2cSStephen M. Cameron /* Send an abort for the specified command.
551275167d2cSStephen M. Cameron  *	If the device and controller support it,
551375167d2cSStephen M. Cameron  *		send a task abort request.
551475167d2cSStephen M. Cameron  */
551575167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
551675167d2cSStephen M. Cameron {
551775167d2cSStephen M. Cameron 
5518a58e7e53SWebb Scales 	int rc;
551975167d2cSStephen M. Cameron 	struct ctlr_info *h;
552075167d2cSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
552175167d2cSStephen M. Cameron 	struct CommandList *abort; /* pointer to command to be aborted */
552275167d2cSStephen M. Cameron 	struct scsi_cmnd *as;	/* ptr to scsi cmd inside aborted command. */
552375167d2cSStephen M. Cameron 	char msg[256];		/* For debug messaging. */
552475167d2cSStephen M. Cameron 	int ml = 0;
55252b08b3e9SDon Brace 	__le32 tagupper, taglower;
552625163bd5SWebb Scales 	int refcount, reply_queue;
552725163bd5SWebb Scales 
552825163bd5SWebb Scales 	if (sc == NULL)
552925163bd5SWebb Scales 		return FAILED;
553075167d2cSStephen M. Cameron 
55319b5c48c2SStephen Cameron 	if (sc->device == NULL)
55329b5c48c2SStephen Cameron 		return FAILED;
55339b5c48c2SStephen Cameron 
553475167d2cSStephen M. Cameron 	/* Find the controller of the command to be aborted */
553575167d2cSStephen M. Cameron 	h = sdev_to_hba(sc->device);
55369b5c48c2SStephen Cameron 	if (h == NULL)
553775167d2cSStephen M. Cameron 		return FAILED;
553875167d2cSStephen M. Cameron 
553925163bd5SWebb Scales 	/* Find the device of the command to be aborted */
554025163bd5SWebb Scales 	dev = sc->device->hostdata;
554125163bd5SWebb Scales 	if (!dev) {
554225163bd5SWebb Scales 		dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
554325163bd5SWebb Scales 				msg);
5544e345893bSDon Brace 		return FAILED;
554525163bd5SWebb Scales 	}
554625163bd5SWebb Scales 
554725163bd5SWebb Scales 	/* If controller locked up, we can guarantee command won't complete */
554825163bd5SWebb Scales 	if (lockup_detected(h)) {
554925163bd5SWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev,
555025163bd5SWebb Scales 					"ABORT FAILED, lockup detected");
555125163bd5SWebb Scales 		return FAILED;
555225163bd5SWebb Scales 	}
555325163bd5SWebb Scales 
555425163bd5SWebb Scales 	/* This is a good time to check if controller lockup has occurred */
555525163bd5SWebb Scales 	if (detect_controller_lockup(h)) {
555625163bd5SWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev,
555725163bd5SWebb Scales 					"ABORT FAILED, new lockup detected");
555825163bd5SWebb Scales 		return FAILED;
555925163bd5SWebb Scales 	}
5560e345893bSDon Brace 
556175167d2cSStephen M. Cameron 	/* Check that controller supports some kind of task abort */
556275167d2cSStephen M. Cameron 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
556375167d2cSStephen M. Cameron 		!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
556475167d2cSStephen M. Cameron 		return FAILED;
556575167d2cSStephen M. Cameron 
556675167d2cSStephen M. Cameron 	memset(msg, 0, sizeof(msg));
55674b761557SRobert Elliott 	ml += sprintf(msg+ml, "scsi %d:%d:%d:%llu %s %p",
556875167d2cSStephen M. Cameron 		h->scsi_host->host_no, sc->device->channel,
55690d96ef5fSWebb Scales 		sc->device->id, sc->device->lun,
55704b761557SRobert Elliott 		"Aborting command", sc);
557175167d2cSStephen M. Cameron 
557275167d2cSStephen M. Cameron 	/* Get SCSI command to be aborted */
557375167d2cSStephen M. Cameron 	abort = (struct CommandList *) sc->host_scribble;
557475167d2cSStephen M. Cameron 	if (abort == NULL) {
5575281a7fd0SWebb Scales 		/* This can happen if the command already completed. */
5576281a7fd0SWebb Scales 		return SUCCESS;
5577281a7fd0SWebb Scales 	}
5578281a7fd0SWebb Scales 	refcount = atomic_inc_return(&abort->refcount);
5579281a7fd0SWebb Scales 	if (refcount == 1) { /* Command is done already. */
5580281a7fd0SWebb Scales 		cmd_free(h, abort);
5581281a7fd0SWebb Scales 		return SUCCESS;
558275167d2cSStephen M. Cameron 	}
55839b5c48c2SStephen Cameron 
55849b5c48c2SStephen Cameron 	/* Don't bother trying the abort if we know it won't work. */
55859b5c48c2SStephen Cameron 	if (abort->cmd_type != CMD_IOACCEL2 &&
55869b5c48c2SStephen Cameron 		abort->cmd_type != CMD_IOACCEL1 && !dev->supports_aborts) {
55879b5c48c2SStephen Cameron 		cmd_free(h, abort);
55889b5c48c2SStephen Cameron 		return FAILED;
55899b5c48c2SStephen Cameron 	}
55909b5c48c2SStephen Cameron 
5591a58e7e53SWebb Scales 	/*
5592a58e7e53SWebb Scales 	 * Check that we're aborting the right command.
5593a58e7e53SWebb Scales 	 * It's possible the CommandList already completed and got re-used.
5594a58e7e53SWebb Scales 	 */
5595a58e7e53SWebb Scales 	if (abort->scsi_cmd != sc) {
5596a58e7e53SWebb Scales 		cmd_free(h, abort);
5597a58e7e53SWebb Scales 		return SUCCESS;
5598a58e7e53SWebb Scales 	}
5599a58e7e53SWebb Scales 
5600a58e7e53SWebb Scales 	abort->abort_pending = true;
560117eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
560225163bd5SWebb Scales 	reply_queue = hpsa_extract_reply_queue(h, abort);
560317eb87d2SScott Teel 	ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
56047fa3030cSStephen Cameron 	as  = abort->scsi_cmd;
560575167d2cSStephen M. Cameron 	if (as != NULL)
56064b761557SRobert Elliott 		ml += sprintf(msg+ml,
56074b761557SRobert Elliott 			"CDBLen: %d CDB: 0x%02x%02x... SN: 0x%lx ",
56084b761557SRobert Elliott 			as->cmd_len, as->cmnd[0], as->cmnd[1],
56094b761557SRobert Elliott 			as->serial_number);
56104b761557SRobert Elliott 	dev_warn(&h->pdev->dev, "%s BEING SENT\n", msg);
56110d96ef5fSWebb Scales 	hpsa_show_dev_msg(KERN_WARNING, h, dev, "Aborting command");
56124b761557SRobert Elliott 
561375167d2cSStephen M. Cameron 	/*
561475167d2cSStephen M. Cameron 	 * Command is in flight, or possibly already completed
561575167d2cSStephen M. Cameron 	 * by the firmware (but not to the scsi mid layer) but we can't
561675167d2cSStephen M. Cameron 	 * distinguish which.  Send the abort down.
561775167d2cSStephen M. Cameron 	 */
56189b5c48c2SStephen Cameron 	if (wait_for_available_abort_cmd(h)) {
56199b5c48c2SStephen Cameron 		dev_warn(&h->pdev->dev,
56204b761557SRobert Elliott 			"%s FAILED, timeout waiting for an abort command to become available.\n",
56214b761557SRobert Elliott 			msg);
56229b5c48c2SStephen Cameron 		cmd_free(h, abort);
56239b5c48c2SStephen Cameron 		return FAILED;
56249b5c48c2SStephen Cameron 	}
562525163bd5SWebb Scales 	rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort, reply_queue);
56269b5c48c2SStephen Cameron 	atomic_inc(&h->abort_cmds_available);
56279b5c48c2SStephen Cameron 	wake_up_all(&h->abort_cmd_wait_queue);
562875167d2cSStephen M. Cameron 	if (rc != 0) {
56294b761557SRobert Elliott 		dev_warn(&h->pdev->dev, "%s SENT, FAILED\n", msg);
56300d96ef5fSWebb Scales 		hpsa_show_dev_msg(KERN_WARNING, h, dev,
56310d96ef5fSWebb Scales 				"FAILED to abort command");
5632281a7fd0SWebb Scales 		cmd_free(h, abort);
563375167d2cSStephen M. Cameron 		return FAILED;
563475167d2cSStephen M. Cameron 	}
56354b761557SRobert Elliott 	dev_info(&h->pdev->dev, "%s SENT, SUCCESS\n", msg);
5636d604f533SWebb Scales 	wait_event(h->event_sync_wait_queue,
5637a58e7e53SWebb Scales 		   abort->scsi_cmd != sc || lockup_detected(h));
5638281a7fd0SWebb Scales 	cmd_free(h, abort);
5639a58e7e53SWebb Scales 	return !lockup_detected(h) ? SUCCESS : FAILED;
564075167d2cSStephen M. Cameron }
564175167d2cSStephen M. Cameron 
5642edd16368SStephen M. Cameron /*
564373153fe5SWebb Scales  * For operations with an associated SCSI command, a command block is allocated
564473153fe5SWebb Scales  * at init, and managed by cmd_tagged_alloc() and cmd_tagged_free() using the
564573153fe5SWebb Scales  * block request tag as an index into a table of entries.  cmd_tagged_free() is
564673153fe5SWebb Scales  * the complement, although cmd_free() may be called instead.
564773153fe5SWebb Scales  */
564873153fe5SWebb Scales static struct CommandList *cmd_tagged_alloc(struct ctlr_info *h,
564973153fe5SWebb Scales 					    struct scsi_cmnd *scmd)
565073153fe5SWebb Scales {
565173153fe5SWebb Scales 	int idx = hpsa_get_cmd_index(scmd);
565273153fe5SWebb Scales 	struct CommandList *c = h->cmd_pool + idx;
565373153fe5SWebb Scales 
565473153fe5SWebb Scales 	if (idx < HPSA_NRESERVED_CMDS || idx >= h->nr_cmds) {
565573153fe5SWebb Scales 		dev_err(&h->pdev->dev, "Bad block tag: %d not in [%d..%d]\n",
565673153fe5SWebb Scales 			idx, HPSA_NRESERVED_CMDS, h->nr_cmds - 1);
565773153fe5SWebb Scales 		/* The index value comes from the block layer, so if it's out of
565873153fe5SWebb Scales 		 * bounds, it's probably not our bug.
565973153fe5SWebb Scales 		 */
566073153fe5SWebb Scales 		BUG();
566173153fe5SWebb Scales 	}
566273153fe5SWebb Scales 
566373153fe5SWebb Scales 	atomic_inc(&c->refcount);
566473153fe5SWebb Scales 	if (unlikely(!hpsa_is_cmd_idle(c))) {
566573153fe5SWebb Scales 		/*
566673153fe5SWebb Scales 		 * We expect that the SCSI layer will hand us a unique tag
566773153fe5SWebb Scales 		 * value.  Thus, there should never be a collision here between
566873153fe5SWebb Scales 		 * two requests...because if the selected command isn't idle
566973153fe5SWebb Scales 		 * then someone is going to be very disappointed.
567073153fe5SWebb Scales 		 */
567173153fe5SWebb Scales 		dev_err(&h->pdev->dev,
567273153fe5SWebb Scales 			"tag collision (tag=%d) in cmd_tagged_alloc().\n",
567373153fe5SWebb Scales 			idx);
567473153fe5SWebb Scales 		if (c->scsi_cmd != NULL)
567573153fe5SWebb Scales 			scsi_print_command(c->scsi_cmd);
567673153fe5SWebb Scales 		scsi_print_command(scmd);
567773153fe5SWebb Scales 	}
567873153fe5SWebb Scales 
567973153fe5SWebb Scales 	hpsa_cmd_partial_init(h, idx, c);
568073153fe5SWebb Scales 	return c;
568173153fe5SWebb Scales }
568273153fe5SWebb Scales 
568373153fe5SWebb Scales static void cmd_tagged_free(struct ctlr_info *h, struct CommandList *c)
568473153fe5SWebb Scales {
568573153fe5SWebb Scales 	/*
568673153fe5SWebb Scales 	 * Release our reference to the block.  We don't need to do anything
568773153fe5SWebb Scales 	 * else to free it, because it is accessed by index.  (There's no point
568873153fe5SWebb Scales 	 * in checking the result of the decrement, since we cannot guarantee
568973153fe5SWebb Scales 	 * that there isn't a concurrent abort which is also accessing it.)
569073153fe5SWebb Scales 	 */
569173153fe5SWebb Scales 	(void)atomic_dec(&c->refcount);
569273153fe5SWebb Scales }
569373153fe5SWebb Scales 
569473153fe5SWebb Scales /*
5695edd16368SStephen M. Cameron  * For operations that cannot sleep, a command block is allocated at init,
5696edd16368SStephen M. Cameron  * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
5697edd16368SStephen M. Cameron  * which ones are free or in use.  Lock must be held when calling this.
5698edd16368SStephen M. Cameron  * cmd_free() is the complement.
5699bf43caf3SRobert Elliott  * This function never gives up and returns NULL.  If it hangs,
5700bf43caf3SRobert Elliott  * another thread must call cmd_free() to free some tags.
5701edd16368SStephen M. Cameron  */
5702281a7fd0SWebb Scales 
5703edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h)
5704edd16368SStephen M. Cameron {
5705edd16368SStephen M. Cameron 	struct CommandList *c;
5706360c73bdSStephen Cameron 	int refcount, i;
570773153fe5SWebb Scales 	int offset = 0;
5708edd16368SStephen M. Cameron 
570933811026SRobert Elliott 	/*
571033811026SRobert Elliott 	 * There is some *extremely* small but non-zero chance that that
57114c413128SStephen M. Cameron 	 * multiple threads could get in here, and one thread could
57124c413128SStephen M. Cameron 	 * be scanning through the list of bits looking for a free
57134c413128SStephen M. Cameron 	 * one, but the free ones are always behind him, and other
57144c413128SStephen M. Cameron 	 * threads sneak in behind him and eat them before he can
57154c413128SStephen M. Cameron 	 * get to them, so that while there is always a free one, a
57164c413128SStephen M. Cameron 	 * very unlucky thread might be starved anyway, never able to
57174c413128SStephen M. Cameron 	 * beat the other threads.  In reality, this happens so
57184c413128SStephen M. Cameron 	 * infrequently as to be indistinguishable from never.
571973153fe5SWebb Scales 	 *
572073153fe5SWebb Scales 	 * Note that we start allocating commands before the SCSI host structure
572173153fe5SWebb Scales 	 * is initialized.  Since the search starts at bit zero, this
572273153fe5SWebb Scales 	 * all works, since we have at least one command structure available;
572373153fe5SWebb Scales 	 * however, it means that the structures with the low indexes have to be
572473153fe5SWebb Scales 	 * reserved for driver-initiated requests, while requests from the block
572573153fe5SWebb Scales 	 * layer will use the higher indexes.
57264c413128SStephen M. Cameron 	 */
57274c413128SStephen M. Cameron 
5728281a7fd0SWebb Scales 	for (;;) {
572973153fe5SWebb Scales 		i = find_next_zero_bit(h->cmd_pool_bits,
573073153fe5SWebb Scales 					HPSA_NRESERVED_CMDS,
573173153fe5SWebb Scales 					offset);
573273153fe5SWebb Scales 		if (unlikely(i >= HPSA_NRESERVED_CMDS)) {
5733281a7fd0SWebb Scales 			offset = 0;
5734281a7fd0SWebb Scales 			continue;
5735281a7fd0SWebb Scales 		}
5736edd16368SStephen M. Cameron 		c = h->cmd_pool + i;
5737281a7fd0SWebb Scales 		refcount = atomic_inc_return(&c->refcount);
5738281a7fd0SWebb Scales 		if (unlikely(refcount > 1)) {
5739281a7fd0SWebb Scales 			cmd_free(h, c); /* already in use */
574073153fe5SWebb Scales 			offset = (i + 1) % HPSA_NRESERVED_CMDS;
5741281a7fd0SWebb Scales 			continue;
5742281a7fd0SWebb Scales 		}
5743281a7fd0SWebb Scales 		set_bit(i & (BITS_PER_LONG - 1),
5744281a7fd0SWebb Scales 			h->cmd_pool_bits + (i / BITS_PER_LONG));
5745281a7fd0SWebb Scales 		break; /* it's ours now. */
5746281a7fd0SWebb Scales 	}
5747360c73bdSStephen Cameron 	hpsa_cmd_partial_init(h, i, c);
5748edd16368SStephen M. Cameron 	return c;
5749edd16368SStephen M. Cameron }
5750edd16368SStephen M. Cameron 
575173153fe5SWebb Scales /*
575273153fe5SWebb Scales  * This is the complementary operation to cmd_alloc().  Note, however, in some
575373153fe5SWebb Scales  * corner cases it may also be used to free blocks allocated by
575473153fe5SWebb Scales  * cmd_tagged_alloc() in which case the ref-count decrement does the trick and
575573153fe5SWebb Scales  * the clear-bit is harmless.
575673153fe5SWebb Scales  */
5757edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c)
5758edd16368SStephen M. Cameron {
5759281a7fd0SWebb Scales 	if (atomic_dec_and_test(&c->refcount)) {
5760edd16368SStephen M. Cameron 		int i;
5761edd16368SStephen M. Cameron 
5762edd16368SStephen M. Cameron 		i = c - h->cmd_pool;
5763edd16368SStephen M. Cameron 		clear_bit(i & (BITS_PER_LONG - 1),
5764edd16368SStephen M. Cameron 			  h->cmd_pool_bits + (i / BITS_PER_LONG));
5765edd16368SStephen M. Cameron 	}
5766281a7fd0SWebb Scales }
5767edd16368SStephen M. Cameron 
5768edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
5769edd16368SStephen M. Cameron 
577042a91641SDon Brace static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd,
577142a91641SDon Brace 	void __user *arg)
5772edd16368SStephen M. Cameron {
5773edd16368SStephen M. Cameron 	IOCTL32_Command_struct __user *arg32 =
5774edd16368SStephen M. Cameron 	    (IOCTL32_Command_struct __user *) arg;
5775edd16368SStephen M. Cameron 	IOCTL_Command_struct arg64;
5776edd16368SStephen M. Cameron 	IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
5777edd16368SStephen M. Cameron 	int err;
5778edd16368SStephen M. Cameron 	u32 cp;
5779edd16368SStephen M. Cameron 
5780938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
5781edd16368SStephen M. Cameron 	err = 0;
5782edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
5783edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
5784edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
5785edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
5786edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
5787edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
5788edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
5789edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
5790edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
5791edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
5792edd16368SStephen M. Cameron 
5793edd16368SStephen M. Cameron 	if (err)
5794edd16368SStephen M. Cameron 		return -EFAULT;
5795edd16368SStephen M. Cameron 
579642a91641SDon Brace 	err = hpsa_ioctl(dev, CCISS_PASSTHRU, p);
5797edd16368SStephen M. Cameron 	if (err)
5798edd16368SStephen M. Cameron 		return err;
5799edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
5800edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
5801edd16368SStephen M. Cameron 	if (err)
5802edd16368SStephen M. Cameron 		return -EFAULT;
5803edd16368SStephen M. Cameron 	return err;
5804edd16368SStephen M. Cameron }
5805edd16368SStephen M. Cameron 
5806edd16368SStephen M. Cameron static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
580742a91641SDon Brace 	int cmd, void __user *arg)
5808edd16368SStephen M. Cameron {
5809edd16368SStephen M. Cameron 	BIG_IOCTL32_Command_struct __user *arg32 =
5810edd16368SStephen M. Cameron 	    (BIG_IOCTL32_Command_struct __user *) arg;
5811edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct arg64;
5812edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct __user *p =
5813edd16368SStephen M. Cameron 	    compat_alloc_user_space(sizeof(arg64));
5814edd16368SStephen M. Cameron 	int err;
5815edd16368SStephen M. Cameron 	u32 cp;
5816edd16368SStephen M. Cameron 
5817938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
5818edd16368SStephen M. Cameron 	err = 0;
5819edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
5820edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
5821edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
5822edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
5823edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
5824edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
5825edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
5826edd16368SStephen M. Cameron 	err |= get_user(arg64.malloc_size, &arg32->malloc_size);
5827edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
5828edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
5829edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
5830edd16368SStephen M. Cameron 
5831edd16368SStephen M. Cameron 	if (err)
5832edd16368SStephen M. Cameron 		return -EFAULT;
5833edd16368SStephen M. Cameron 
583442a91641SDon Brace 	err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, p);
5835edd16368SStephen M. Cameron 	if (err)
5836edd16368SStephen M. Cameron 		return err;
5837edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
5838edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
5839edd16368SStephen M. Cameron 	if (err)
5840edd16368SStephen M. Cameron 		return -EFAULT;
5841edd16368SStephen M. Cameron 	return err;
5842edd16368SStephen M. Cameron }
584371fe75a7SStephen M. Cameron 
584442a91641SDon Brace static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
584571fe75a7SStephen M. Cameron {
584671fe75a7SStephen M. Cameron 	switch (cmd) {
584771fe75a7SStephen M. Cameron 	case CCISS_GETPCIINFO:
584871fe75a7SStephen M. Cameron 	case CCISS_GETINTINFO:
584971fe75a7SStephen M. Cameron 	case CCISS_SETINTINFO:
585071fe75a7SStephen M. Cameron 	case CCISS_GETNODENAME:
585171fe75a7SStephen M. Cameron 	case CCISS_SETNODENAME:
585271fe75a7SStephen M. Cameron 	case CCISS_GETHEARTBEAT:
585371fe75a7SStephen M. Cameron 	case CCISS_GETBUSTYPES:
585471fe75a7SStephen M. Cameron 	case CCISS_GETFIRMVER:
585571fe75a7SStephen M. Cameron 	case CCISS_GETDRIVVER:
585671fe75a7SStephen M. Cameron 	case CCISS_REVALIDVOLS:
585771fe75a7SStephen M. Cameron 	case CCISS_DEREGDISK:
585871fe75a7SStephen M. Cameron 	case CCISS_REGNEWDISK:
585971fe75a7SStephen M. Cameron 	case CCISS_REGNEWD:
586071fe75a7SStephen M. Cameron 	case CCISS_RESCANDISK:
586171fe75a7SStephen M. Cameron 	case CCISS_GETLUNINFO:
586271fe75a7SStephen M. Cameron 		return hpsa_ioctl(dev, cmd, arg);
586371fe75a7SStephen M. Cameron 
586471fe75a7SStephen M. Cameron 	case CCISS_PASSTHRU32:
586571fe75a7SStephen M. Cameron 		return hpsa_ioctl32_passthru(dev, cmd, arg);
586671fe75a7SStephen M. Cameron 	case CCISS_BIG_PASSTHRU32:
586771fe75a7SStephen M. Cameron 		return hpsa_ioctl32_big_passthru(dev, cmd, arg);
586871fe75a7SStephen M. Cameron 
586971fe75a7SStephen M. Cameron 	default:
587071fe75a7SStephen M. Cameron 		return -ENOIOCTLCMD;
587171fe75a7SStephen M. Cameron 	}
587271fe75a7SStephen M. Cameron }
5873edd16368SStephen M. Cameron #endif
5874edd16368SStephen M. Cameron 
5875edd16368SStephen M. Cameron static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
5876edd16368SStephen M. Cameron {
5877edd16368SStephen M. Cameron 	struct hpsa_pci_info pciinfo;
5878edd16368SStephen M. Cameron 
5879edd16368SStephen M. Cameron 	if (!argp)
5880edd16368SStephen M. Cameron 		return -EINVAL;
5881edd16368SStephen M. Cameron 	pciinfo.domain = pci_domain_nr(h->pdev->bus);
5882edd16368SStephen M. Cameron 	pciinfo.bus = h->pdev->bus->number;
5883edd16368SStephen M. Cameron 	pciinfo.dev_fn = h->pdev->devfn;
5884edd16368SStephen M. Cameron 	pciinfo.board_id = h->board_id;
5885edd16368SStephen M. Cameron 	if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
5886edd16368SStephen M. Cameron 		return -EFAULT;
5887edd16368SStephen M. Cameron 	return 0;
5888edd16368SStephen M. Cameron }
5889edd16368SStephen M. Cameron 
5890edd16368SStephen M. Cameron static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
5891edd16368SStephen M. Cameron {
5892edd16368SStephen M. Cameron 	DriverVer_type DriverVer;
5893edd16368SStephen M. Cameron 	unsigned char vmaj, vmin, vsubmin;
5894edd16368SStephen M. Cameron 	int rc;
5895edd16368SStephen M. Cameron 
5896edd16368SStephen M. Cameron 	rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
5897edd16368SStephen M. Cameron 		&vmaj, &vmin, &vsubmin);
5898edd16368SStephen M. Cameron 	if (rc != 3) {
5899edd16368SStephen M. Cameron 		dev_info(&h->pdev->dev, "driver version string '%s' "
5900edd16368SStephen M. Cameron 			"unrecognized.", HPSA_DRIVER_VERSION);
5901edd16368SStephen M. Cameron 		vmaj = 0;
5902edd16368SStephen M. Cameron 		vmin = 0;
5903edd16368SStephen M. Cameron 		vsubmin = 0;
5904edd16368SStephen M. Cameron 	}
5905edd16368SStephen M. Cameron 	DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
5906edd16368SStephen M. Cameron 	if (!argp)
5907edd16368SStephen M. Cameron 		return -EINVAL;
5908edd16368SStephen M. Cameron 	if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
5909edd16368SStephen M. Cameron 		return -EFAULT;
5910edd16368SStephen M. Cameron 	return 0;
5911edd16368SStephen M. Cameron }
5912edd16368SStephen M. Cameron 
5913edd16368SStephen M. Cameron static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
5914edd16368SStephen M. Cameron {
5915edd16368SStephen M. Cameron 	IOCTL_Command_struct iocommand;
5916edd16368SStephen M. Cameron 	struct CommandList *c;
5917edd16368SStephen M. Cameron 	char *buff = NULL;
591850a0decfSStephen M. Cameron 	u64 temp64;
5919c1f63c8fSStephen M. Cameron 	int rc = 0;
5920edd16368SStephen M. Cameron 
5921edd16368SStephen M. Cameron 	if (!argp)
5922edd16368SStephen M. Cameron 		return -EINVAL;
5923edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
5924edd16368SStephen M. Cameron 		return -EPERM;
5925edd16368SStephen M. Cameron 	if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
5926edd16368SStephen M. Cameron 		return -EFAULT;
5927edd16368SStephen M. Cameron 	if ((iocommand.buf_size < 1) &&
5928edd16368SStephen M. Cameron 	    (iocommand.Request.Type.Direction != XFER_NONE)) {
5929edd16368SStephen M. Cameron 		return -EINVAL;
5930edd16368SStephen M. Cameron 	}
5931edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
5932edd16368SStephen M. Cameron 		buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
5933edd16368SStephen M. Cameron 		if (buff == NULL)
59342dd02d74SRobert Elliott 			return -ENOMEM;
59359233fb10SStephen M. Cameron 		if (iocommand.Request.Type.Direction & XFER_WRITE) {
5936edd16368SStephen M. Cameron 			/* Copy the data into the buffer we created */
5937b03a7771SStephen M. Cameron 			if (copy_from_user(buff, iocommand.buf,
5938b03a7771SStephen M. Cameron 				iocommand.buf_size)) {
5939c1f63c8fSStephen M. Cameron 				rc = -EFAULT;
5940c1f63c8fSStephen M. Cameron 				goto out_kfree;
5941edd16368SStephen M. Cameron 			}
5942b03a7771SStephen M. Cameron 		} else {
5943edd16368SStephen M. Cameron 			memset(buff, 0, iocommand.buf_size);
5944b03a7771SStephen M. Cameron 		}
5945b03a7771SStephen M. Cameron 	}
594645fcb86eSStephen Cameron 	c = cmd_alloc(h);
5947bf43caf3SRobert Elliott 
5948edd16368SStephen M. Cameron 	/* Fill in the command type */
5949edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
5950a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_BUSY;
5951edd16368SStephen M. Cameron 	/* Fill in Command Header */
5952edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0; /* unused in simple mode */
5953edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {	/* buffer to fill */
5954edd16368SStephen M. Cameron 		c->Header.SGList = 1;
595550a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(1);
5956edd16368SStephen M. Cameron 	} else	{ /* no buffers to fill */
5957edd16368SStephen M. Cameron 		c->Header.SGList = 0;
595850a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(0);
5959edd16368SStephen M. Cameron 	}
5960edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
5961edd16368SStephen M. Cameron 
5962edd16368SStephen M. Cameron 	/* Fill in Request block */
5963edd16368SStephen M. Cameron 	memcpy(&c->Request, &iocommand.Request,
5964edd16368SStephen M. Cameron 		sizeof(c->Request));
5965edd16368SStephen M. Cameron 
5966edd16368SStephen M. Cameron 	/* Fill in the scatter gather information */
5967edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
596850a0decfSStephen M. Cameron 		temp64 = pci_map_single(h->pdev, buff,
5969edd16368SStephen M. Cameron 			iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
597050a0decfSStephen M. Cameron 		if (dma_mapping_error(&h->pdev->dev, (dma_addr_t) temp64)) {
597150a0decfSStephen M. Cameron 			c->SG[0].Addr = cpu_to_le64(0);
597250a0decfSStephen M. Cameron 			c->SG[0].Len = cpu_to_le32(0);
5973bcc48ffaSStephen M. Cameron 			rc = -ENOMEM;
5974bcc48ffaSStephen M. Cameron 			goto out;
5975bcc48ffaSStephen M. Cameron 		}
597650a0decfSStephen M. Cameron 		c->SG[0].Addr = cpu_to_le64(temp64);
597750a0decfSStephen M. Cameron 		c->SG[0].Len = cpu_to_le32(iocommand.buf_size);
597850a0decfSStephen M. Cameron 		c->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* not chaining */
5979edd16368SStephen M. Cameron 	}
598025163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
5981c2dd32e0SStephen M. Cameron 	if (iocommand.buf_size > 0)
5982edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
5983edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
598425163bd5SWebb Scales 	if (rc) {
598525163bd5SWebb Scales 		rc = -EIO;
598625163bd5SWebb Scales 		goto out;
598725163bd5SWebb Scales 	}
5988edd16368SStephen M. Cameron 
5989edd16368SStephen M. Cameron 	/* Copy the error information out */
5990edd16368SStephen M. Cameron 	memcpy(&iocommand.error_info, c->err_info,
5991edd16368SStephen M. Cameron 		sizeof(iocommand.error_info));
5992edd16368SStephen M. Cameron 	if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
5993c1f63c8fSStephen M. Cameron 		rc = -EFAULT;
5994c1f63c8fSStephen M. Cameron 		goto out;
5995edd16368SStephen M. Cameron 	}
59969233fb10SStephen M. Cameron 	if ((iocommand.Request.Type.Direction & XFER_READ) &&
5997b03a7771SStephen M. Cameron 		iocommand.buf_size > 0) {
5998edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
5999edd16368SStephen M. Cameron 		if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
6000c1f63c8fSStephen M. Cameron 			rc = -EFAULT;
6001c1f63c8fSStephen M. Cameron 			goto out;
6002edd16368SStephen M. Cameron 		}
6003edd16368SStephen M. Cameron 	}
6004c1f63c8fSStephen M. Cameron out:
600545fcb86eSStephen Cameron 	cmd_free(h, c);
6006c1f63c8fSStephen M. Cameron out_kfree:
6007c1f63c8fSStephen M. Cameron 	kfree(buff);
6008c1f63c8fSStephen M. Cameron 	return rc;
6009edd16368SStephen M. Cameron }
6010edd16368SStephen M. Cameron 
6011edd16368SStephen M. Cameron static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
6012edd16368SStephen M. Cameron {
6013edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct *ioc;
6014edd16368SStephen M. Cameron 	struct CommandList *c;
6015edd16368SStephen M. Cameron 	unsigned char **buff = NULL;
6016edd16368SStephen M. Cameron 	int *buff_size = NULL;
601750a0decfSStephen M. Cameron 	u64 temp64;
6018edd16368SStephen M. Cameron 	BYTE sg_used = 0;
6019edd16368SStephen M. Cameron 	int status = 0;
602001a02ffcSStephen M. Cameron 	u32 left;
602101a02ffcSStephen M. Cameron 	u32 sz;
6022edd16368SStephen M. Cameron 	BYTE __user *data_ptr;
6023edd16368SStephen M. Cameron 
6024edd16368SStephen M. Cameron 	if (!argp)
6025edd16368SStephen M. Cameron 		return -EINVAL;
6026edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
6027edd16368SStephen M. Cameron 		return -EPERM;
6028edd16368SStephen M. Cameron 	ioc = (BIG_IOCTL_Command_struct *)
6029edd16368SStephen M. Cameron 	    kmalloc(sizeof(*ioc), GFP_KERNEL);
6030edd16368SStephen M. Cameron 	if (!ioc) {
6031edd16368SStephen M. Cameron 		status = -ENOMEM;
6032edd16368SStephen M. Cameron 		goto cleanup1;
6033edd16368SStephen M. Cameron 	}
6034edd16368SStephen M. Cameron 	if (copy_from_user(ioc, argp, sizeof(*ioc))) {
6035edd16368SStephen M. Cameron 		status = -EFAULT;
6036edd16368SStephen M. Cameron 		goto cleanup1;
6037edd16368SStephen M. Cameron 	}
6038edd16368SStephen M. Cameron 	if ((ioc->buf_size < 1) &&
6039edd16368SStephen M. Cameron 	    (ioc->Request.Type.Direction != XFER_NONE)) {
6040edd16368SStephen M. Cameron 		status = -EINVAL;
6041edd16368SStephen M. Cameron 		goto cleanup1;
6042edd16368SStephen M. Cameron 	}
6043edd16368SStephen M. Cameron 	/* Check kmalloc limits  using all SGs */
6044edd16368SStephen M. Cameron 	if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
6045edd16368SStephen M. Cameron 		status = -EINVAL;
6046edd16368SStephen M. Cameron 		goto cleanup1;
6047edd16368SStephen M. Cameron 	}
6048d66ae08bSStephen M. Cameron 	if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
6049edd16368SStephen M. Cameron 		status = -EINVAL;
6050edd16368SStephen M. Cameron 		goto cleanup1;
6051edd16368SStephen M. Cameron 	}
6052d66ae08bSStephen M. Cameron 	buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
6053edd16368SStephen M. Cameron 	if (!buff) {
6054edd16368SStephen M. Cameron 		status = -ENOMEM;
6055edd16368SStephen M. Cameron 		goto cleanup1;
6056edd16368SStephen M. Cameron 	}
6057d66ae08bSStephen M. Cameron 	buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
6058edd16368SStephen M. Cameron 	if (!buff_size) {
6059edd16368SStephen M. Cameron 		status = -ENOMEM;
6060edd16368SStephen M. Cameron 		goto cleanup1;
6061edd16368SStephen M. Cameron 	}
6062edd16368SStephen M. Cameron 	left = ioc->buf_size;
6063edd16368SStephen M. Cameron 	data_ptr = ioc->buf;
6064edd16368SStephen M. Cameron 	while (left) {
6065edd16368SStephen M. Cameron 		sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
6066edd16368SStephen M. Cameron 		buff_size[sg_used] = sz;
6067edd16368SStephen M. Cameron 		buff[sg_used] = kmalloc(sz, GFP_KERNEL);
6068edd16368SStephen M. Cameron 		if (buff[sg_used] == NULL) {
6069edd16368SStephen M. Cameron 			status = -ENOMEM;
6070edd16368SStephen M. Cameron 			goto cleanup1;
6071edd16368SStephen M. Cameron 		}
60729233fb10SStephen M. Cameron 		if (ioc->Request.Type.Direction & XFER_WRITE) {
6073edd16368SStephen M. Cameron 			if (copy_from_user(buff[sg_used], data_ptr, sz)) {
60740758f4f7SStephen M. Cameron 				status = -EFAULT;
6075edd16368SStephen M. Cameron 				goto cleanup1;
6076edd16368SStephen M. Cameron 			}
6077edd16368SStephen M. Cameron 		} else
6078edd16368SStephen M. Cameron 			memset(buff[sg_used], 0, sz);
6079edd16368SStephen M. Cameron 		left -= sz;
6080edd16368SStephen M. Cameron 		data_ptr += sz;
6081edd16368SStephen M. Cameron 		sg_used++;
6082edd16368SStephen M. Cameron 	}
608345fcb86eSStephen Cameron 	c = cmd_alloc(h);
6084bf43caf3SRobert Elliott 
6085edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
6086a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_BUSY;
6087edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
608850a0decfSStephen M. Cameron 	c->Header.SGList = (u8) sg_used;
608950a0decfSStephen M. Cameron 	c->Header.SGTotal = cpu_to_le16(sg_used);
6090edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
6091edd16368SStephen M. Cameron 	memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
6092edd16368SStephen M. Cameron 	if (ioc->buf_size > 0) {
6093edd16368SStephen M. Cameron 		int i;
6094edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
609550a0decfSStephen M. Cameron 			temp64 = pci_map_single(h->pdev, buff[i],
6096edd16368SStephen M. Cameron 				    buff_size[i], PCI_DMA_BIDIRECTIONAL);
609750a0decfSStephen M. Cameron 			if (dma_mapping_error(&h->pdev->dev,
609850a0decfSStephen M. Cameron 							(dma_addr_t) temp64)) {
609950a0decfSStephen M. Cameron 				c->SG[i].Addr = cpu_to_le64(0);
610050a0decfSStephen M. Cameron 				c->SG[i].Len = cpu_to_le32(0);
6101bcc48ffaSStephen M. Cameron 				hpsa_pci_unmap(h->pdev, c, i,
6102bcc48ffaSStephen M. Cameron 					PCI_DMA_BIDIRECTIONAL);
6103bcc48ffaSStephen M. Cameron 				status = -ENOMEM;
6104e2d4a1f6SStephen M. Cameron 				goto cleanup0;
6105bcc48ffaSStephen M. Cameron 			}
610650a0decfSStephen M. Cameron 			c->SG[i].Addr = cpu_to_le64(temp64);
610750a0decfSStephen M. Cameron 			c->SG[i].Len = cpu_to_le32(buff_size[i]);
610850a0decfSStephen M. Cameron 			c->SG[i].Ext = cpu_to_le32(0);
6109edd16368SStephen M. Cameron 		}
611050a0decfSStephen M. Cameron 		c->SG[--i].Ext = cpu_to_le32(HPSA_SG_LAST);
6111edd16368SStephen M. Cameron 	}
611225163bd5SWebb Scales 	status = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE, NO_TIMEOUT);
6113b03a7771SStephen M. Cameron 	if (sg_used)
6114edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
6115edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
611625163bd5SWebb Scales 	if (status) {
611725163bd5SWebb Scales 		status = -EIO;
611825163bd5SWebb Scales 		goto cleanup0;
611925163bd5SWebb Scales 	}
612025163bd5SWebb Scales 
6121edd16368SStephen M. Cameron 	/* Copy the error information out */
6122edd16368SStephen M. Cameron 	memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
6123edd16368SStephen M. Cameron 	if (copy_to_user(argp, ioc, sizeof(*ioc))) {
6124edd16368SStephen M. Cameron 		status = -EFAULT;
6125e2d4a1f6SStephen M. Cameron 		goto cleanup0;
6126edd16368SStephen M. Cameron 	}
61279233fb10SStephen M. Cameron 	if ((ioc->Request.Type.Direction & XFER_READ) && ioc->buf_size > 0) {
61282b08b3e9SDon Brace 		int i;
61292b08b3e9SDon Brace 
6130edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
6131edd16368SStephen M. Cameron 		BYTE __user *ptr = ioc->buf;
6132edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
6133edd16368SStephen M. Cameron 			if (copy_to_user(ptr, buff[i], buff_size[i])) {
6134edd16368SStephen M. Cameron 				status = -EFAULT;
6135e2d4a1f6SStephen M. Cameron 				goto cleanup0;
6136edd16368SStephen M. Cameron 			}
6137edd16368SStephen M. Cameron 			ptr += buff_size[i];
6138edd16368SStephen M. Cameron 		}
6139edd16368SStephen M. Cameron 	}
6140edd16368SStephen M. Cameron 	status = 0;
6141e2d4a1f6SStephen M. Cameron cleanup0:
614245fcb86eSStephen Cameron 	cmd_free(h, c);
6143edd16368SStephen M. Cameron cleanup1:
6144edd16368SStephen M. Cameron 	if (buff) {
61452b08b3e9SDon Brace 		int i;
61462b08b3e9SDon Brace 
6147edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++)
6148edd16368SStephen M. Cameron 			kfree(buff[i]);
6149edd16368SStephen M. Cameron 		kfree(buff);
6150edd16368SStephen M. Cameron 	}
6151edd16368SStephen M. Cameron 	kfree(buff_size);
6152edd16368SStephen M. Cameron 	kfree(ioc);
6153edd16368SStephen M. Cameron 	return status;
6154edd16368SStephen M. Cameron }
6155edd16368SStephen M. Cameron 
6156edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
6157edd16368SStephen M. Cameron 	struct CommandList *c)
6158edd16368SStephen M. Cameron {
6159edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
6160edd16368SStephen M. Cameron 			c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
6161edd16368SStephen M. Cameron 		(void) check_for_unit_attention(h, c);
6162edd16368SStephen M. Cameron }
61630390f0c0SStephen M. Cameron 
6164edd16368SStephen M. Cameron /*
6165edd16368SStephen M. Cameron  * ioctl
6166edd16368SStephen M. Cameron  */
616742a91641SDon Brace static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
6168edd16368SStephen M. Cameron {
6169edd16368SStephen M. Cameron 	struct ctlr_info *h;
6170edd16368SStephen M. Cameron 	void __user *argp = (void __user *)arg;
61710390f0c0SStephen M. Cameron 	int rc;
6172edd16368SStephen M. Cameron 
6173edd16368SStephen M. Cameron 	h = sdev_to_hba(dev);
6174edd16368SStephen M. Cameron 
6175edd16368SStephen M. Cameron 	switch (cmd) {
6176edd16368SStephen M. Cameron 	case CCISS_DEREGDISK:
6177edd16368SStephen M. Cameron 	case CCISS_REGNEWDISK:
6178edd16368SStephen M. Cameron 	case CCISS_REGNEWD:
6179a08a8471SStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
6180edd16368SStephen M. Cameron 		return 0;
6181edd16368SStephen M. Cameron 	case CCISS_GETPCIINFO:
6182edd16368SStephen M. Cameron 		return hpsa_getpciinfo_ioctl(h, argp);
6183edd16368SStephen M. Cameron 	case CCISS_GETDRIVVER:
6184edd16368SStephen M. Cameron 		return hpsa_getdrivver_ioctl(h, argp);
6185edd16368SStephen M. Cameron 	case CCISS_PASSTHRU:
618634f0c627SDon Brace 		if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
61870390f0c0SStephen M. Cameron 			return -EAGAIN;
61880390f0c0SStephen M. Cameron 		rc = hpsa_passthru_ioctl(h, argp);
618934f0c627SDon Brace 		atomic_inc(&h->passthru_cmds_avail);
61900390f0c0SStephen M. Cameron 		return rc;
6191edd16368SStephen M. Cameron 	case CCISS_BIG_PASSTHRU:
619234f0c627SDon Brace 		if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
61930390f0c0SStephen M. Cameron 			return -EAGAIN;
61940390f0c0SStephen M. Cameron 		rc = hpsa_big_passthru_ioctl(h, argp);
619534f0c627SDon Brace 		atomic_inc(&h->passthru_cmds_avail);
61960390f0c0SStephen M. Cameron 		return rc;
6197edd16368SStephen M. Cameron 	default:
6198edd16368SStephen M. Cameron 		return -ENOTTY;
6199edd16368SStephen M. Cameron 	}
6200edd16368SStephen M. Cameron }
6201edd16368SStephen M. Cameron 
6202bf43caf3SRobert Elliott static void hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
62036f039790SGreg Kroah-Hartman 				u8 reset_type)
620464670ac8SStephen M. Cameron {
620564670ac8SStephen M. Cameron 	struct CommandList *c;
620664670ac8SStephen M. Cameron 
620764670ac8SStephen M. Cameron 	c = cmd_alloc(h);
6208bf43caf3SRobert Elliott 
6209a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map */
6210a2dac136SStephen M. Cameron 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
621164670ac8SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_MSG);
621264670ac8SStephen M. Cameron 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
621364670ac8SStephen M. Cameron 	c->waiting = NULL;
621464670ac8SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
621564670ac8SStephen M. Cameron 	/* Don't wait for completion, the reset won't complete.  Don't free
621664670ac8SStephen M. Cameron 	 * the command either.  This is the last command we will send before
621764670ac8SStephen M. Cameron 	 * re-initializing everything, so it doesn't matter and won't leak.
621864670ac8SStephen M. Cameron 	 */
6219bf43caf3SRobert Elliott 	return;
622064670ac8SStephen M. Cameron }
622164670ac8SStephen M. Cameron 
6222a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
6223b7bb24ebSStephen M. Cameron 	void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
6224edd16368SStephen M. Cameron 	int cmd_type)
6225edd16368SStephen M. Cameron {
6226edd16368SStephen M. Cameron 	int pci_dir = XFER_NONE;
62279b5c48c2SStephen Cameron 	u64 tag; /* for commands to be aborted */
6228edd16368SStephen M. Cameron 
6229edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
6230a58e7e53SWebb Scales 	c->scsi_cmd = SCSI_CMD_BUSY;
6231edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
6232edd16368SStephen M. Cameron 	if (buff != NULL && size > 0) {
6233edd16368SStephen M. Cameron 		c->Header.SGList = 1;
623450a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(1);
6235edd16368SStephen M. Cameron 	} else {
6236edd16368SStephen M. Cameron 		c->Header.SGList = 0;
623750a0decfSStephen M. Cameron 		c->Header.SGTotal = cpu_to_le16(0);
6238edd16368SStephen M. Cameron 	}
6239edd16368SStephen M. Cameron 	memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
6240edd16368SStephen M. Cameron 
6241edd16368SStephen M. Cameron 	if (cmd_type == TYPE_CMD) {
6242edd16368SStephen M. Cameron 		switch (cmd) {
6243edd16368SStephen M. Cameron 		case HPSA_INQUIRY:
6244edd16368SStephen M. Cameron 			/* are we trying to read a vital product page */
6245b7bb24ebSStephen M. Cameron 			if (page_code & VPD_PAGE) {
6246edd16368SStephen M. Cameron 				c->Request.CDB[1] = 0x01;
6247b7bb24ebSStephen M. Cameron 				c->Request.CDB[2] = (page_code & 0xff);
6248edd16368SStephen M. Cameron 			}
6249edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
6250a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6251a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6252edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
6253edd16368SStephen M. Cameron 			c->Request.CDB[0] = HPSA_INQUIRY;
6254edd16368SStephen M. Cameron 			c->Request.CDB[4] = size & 0xFF;
6255edd16368SStephen M. Cameron 			break;
6256edd16368SStephen M. Cameron 		case HPSA_REPORT_LOG:
6257edd16368SStephen M. Cameron 		case HPSA_REPORT_PHYS:
6258edd16368SStephen M. Cameron 			/* Talking to controller so It's a physical command
6259edd16368SStephen M. Cameron 			   mode = 00 target = 0.  Nothing to write.
6260edd16368SStephen M. Cameron 			 */
6261edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
6262a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6263a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6264edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
6265edd16368SStephen M. Cameron 			c->Request.CDB[0] = cmd;
6266edd16368SStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
6267edd16368SStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
6268edd16368SStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
6269edd16368SStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
6270edd16368SStephen M. Cameron 			break;
6271edd16368SStephen M. Cameron 		case HPSA_CACHE_FLUSH:
6272edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
6273a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6274a505b86fSStephen M. Cameron 					TYPE_ATTR_DIR(cmd_type,
6275a505b86fSStephen M. Cameron 						ATTR_SIMPLE, XFER_WRITE);
6276edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
6277edd16368SStephen M. Cameron 			c->Request.CDB[0] = BMIC_WRITE;
6278edd16368SStephen M. Cameron 			c->Request.CDB[6] = BMIC_CACHE_FLUSH;
6279bb158eabSStephen M. Cameron 			c->Request.CDB[7] = (size >> 8) & 0xFF;
6280bb158eabSStephen M. Cameron 			c->Request.CDB[8] = size & 0xFF;
6281edd16368SStephen M. Cameron 			break;
6282edd16368SStephen M. Cameron 		case TEST_UNIT_READY:
6283edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
6284a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6285a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
6286edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
6287edd16368SStephen M. Cameron 			break;
6288283b4a9bSStephen M. Cameron 		case HPSA_GET_RAID_MAP:
6289283b4a9bSStephen M. Cameron 			c->Request.CDBLen = 12;
6290a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6291a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6292283b4a9bSStephen M. Cameron 			c->Request.Timeout = 0;
6293283b4a9bSStephen M. Cameron 			c->Request.CDB[0] = HPSA_CISS_READ;
6294283b4a9bSStephen M. Cameron 			c->Request.CDB[1] = cmd;
6295283b4a9bSStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
6296283b4a9bSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
6297283b4a9bSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
6298283b4a9bSStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
6299283b4a9bSStephen M. Cameron 			break;
6300316b221aSStephen M. Cameron 		case BMIC_SENSE_CONTROLLER_PARAMETERS:
6301316b221aSStephen M. Cameron 			c->Request.CDBLen = 10;
6302a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6303a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
6304316b221aSStephen M. Cameron 			c->Request.Timeout = 0;
6305316b221aSStephen M. Cameron 			c->Request.CDB[0] = BMIC_READ;
6306316b221aSStephen M. Cameron 			c->Request.CDB[6] = BMIC_SENSE_CONTROLLER_PARAMETERS;
6307316b221aSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
6308316b221aSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
6309316b221aSStephen M. Cameron 			break;
631003383736SDon Brace 		case BMIC_IDENTIFY_PHYSICAL_DEVICE:
631103383736SDon Brace 			c->Request.CDBLen = 10;
631203383736SDon Brace 			c->Request.type_attr_dir =
631303383736SDon Brace 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
631403383736SDon Brace 			c->Request.Timeout = 0;
631503383736SDon Brace 			c->Request.CDB[0] = BMIC_READ;
631603383736SDon Brace 			c->Request.CDB[6] = BMIC_IDENTIFY_PHYSICAL_DEVICE;
631703383736SDon Brace 			c->Request.CDB[7] = (size >> 16) & 0xFF;
631803383736SDon Brace 			c->Request.CDB[8] = (size >> 8) & 0XFF;
631903383736SDon Brace 			break;
6320edd16368SStephen M. Cameron 		default:
6321edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
6322edd16368SStephen M. Cameron 			BUG();
6323a2dac136SStephen M. Cameron 			return -1;
6324edd16368SStephen M. Cameron 		}
6325edd16368SStephen M. Cameron 	} else if (cmd_type == TYPE_MSG) {
6326edd16368SStephen M. Cameron 		switch (cmd) {
6327edd16368SStephen M. Cameron 
6328edd16368SStephen M. Cameron 		case  HPSA_DEVICE_RESET_MSG:
6329edd16368SStephen M. Cameron 			c->Request.CDBLen = 16;
6330a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6331a505b86fSStephen M. Cameron 				TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
6332edd16368SStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
633364670ac8SStephen M. Cameron 			memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
633464670ac8SStephen M. Cameron 			c->Request.CDB[0] =  cmd;
633521e89afdSStephen M. Cameron 			c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
6336edd16368SStephen M. Cameron 			/* If bytes 4-7 are zero, it means reset the */
6337edd16368SStephen M. Cameron 			/* LunID device */
6338edd16368SStephen M. Cameron 			c->Request.CDB[4] = 0x00;
6339edd16368SStephen M. Cameron 			c->Request.CDB[5] = 0x00;
6340edd16368SStephen M. Cameron 			c->Request.CDB[6] = 0x00;
6341edd16368SStephen M. Cameron 			c->Request.CDB[7] = 0x00;
6342edd16368SStephen M. Cameron 			break;
634375167d2cSStephen M. Cameron 		case  HPSA_ABORT_MSG:
63449b5c48c2SStephen Cameron 			memcpy(&tag, buff, sizeof(tag));
63452b08b3e9SDon Brace 			dev_dbg(&h->pdev->dev,
63469b5c48c2SStephen Cameron 				"Abort Tag:0x%016llx using rqst Tag:0x%016llx",
63479b5c48c2SStephen Cameron 				tag, c->Header.tag);
634875167d2cSStephen M. Cameron 			c->Request.CDBLen = 16;
6349a505b86fSStephen M. Cameron 			c->Request.type_attr_dir =
6350a505b86fSStephen M. Cameron 					TYPE_ATTR_DIR(cmd_type,
6351a505b86fSStephen M. Cameron 						ATTR_SIMPLE, XFER_WRITE);
635275167d2cSStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
635375167d2cSStephen M. Cameron 			c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
635475167d2cSStephen M. Cameron 			c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
635575167d2cSStephen M. Cameron 			c->Request.CDB[2] = 0x00; /* reserved */
635675167d2cSStephen M. Cameron 			c->Request.CDB[3] = 0x00; /* reserved */
635775167d2cSStephen M. Cameron 			/* Tag to abort goes in CDB[4]-CDB[11] */
63589b5c48c2SStephen Cameron 			memcpy(&c->Request.CDB[4], &tag, sizeof(tag));
635975167d2cSStephen M. Cameron 			c->Request.CDB[12] = 0x00; /* reserved */
636075167d2cSStephen M. Cameron 			c->Request.CDB[13] = 0x00; /* reserved */
636175167d2cSStephen M. Cameron 			c->Request.CDB[14] = 0x00; /* reserved */
636275167d2cSStephen M. Cameron 			c->Request.CDB[15] = 0x00; /* reserved */
636375167d2cSStephen M. Cameron 		break;
6364edd16368SStephen M. Cameron 		default:
6365edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown message type %d\n",
6366edd16368SStephen M. Cameron 				cmd);
6367edd16368SStephen M. Cameron 			BUG();
6368edd16368SStephen M. Cameron 		}
6369edd16368SStephen M. Cameron 	} else {
6370edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
6371edd16368SStephen M. Cameron 		BUG();
6372edd16368SStephen M. Cameron 	}
6373edd16368SStephen M. Cameron 
6374a505b86fSStephen M. Cameron 	switch (GET_DIR(c->Request.type_attr_dir)) {
6375edd16368SStephen M. Cameron 	case XFER_READ:
6376edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_FROMDEVICE;
6377edd16368SStephen M. Cameron 		break;
6378edd16368SStephen M. Cameron 	case XFER_WRITE:
6379edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_TODEVICE;
6380edd16368SStephen M. Cameron 		break;
6381edd16368SStephen M. Cameron 	case XFER_NONE:
6382edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_NONE;
6383edd16368SStephen M. Cameron 		break;
6384edd16368SStephen M. Cameron 	default:
6385edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_BIDIRECTIONAL;
6386edd16368SStephen M. Cameron 	}
6387a2dac136SStephen M. Cameron 	if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
6388a2dac136SStephen M. Cameron 		return -1;
6389a2dac136SStephen M. Cameron 	return 0;
6390edd16368SStephen M. Cameron }
6391edd16368SStephen M. Cameron 
6392edd16368SStephen M. Cameron /*
6393edd16368SStephen M. Cameron  * Map (physical) PCI mem into (virtual) kernel space
6394edd16368SStephen M. Cameron  */
6395edd16368SStephen M. Cameron static void __iomem *remap_pci_mem(ulong base, ulong size)
6396edd16368SStephen M. Cameron {
6397edd16368SStephen M. Cameron 	ulong page_base = ((ulong) base) & PAGE_MASK;
6398edd16368SStephen M. Cameron 	ulong page_offs = ((ulong) base) - page_base;
6399088ba34cSStephen M. Cameron 	void __iomem *page_remapped = ioremap_nocache(page_base,
6400088ba34cSStephen M. Cameron 		page_offs + size);
6401edd16368SStephen M. Cameron 
6402edd16368SStephen M. Cameron 	return page_remapped ? (page_remapped + page_offs) : NULL;
6403edd16368SStephen M. Cameron }
6404edd16368SStephen M. Cameron 
6405254f796bSMatt Gates static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
6406edd16368SStephen M. Cameron {
6407254f796bSMatt Gates 	return h->access.command_completed(h, q);
6408edd16368SStephen M. Cameron }
6409edd16368SStephen M. Cameron 
6410900c5440SStephen M. Cameron static inline bool interrupt_pending(struct ctlr_info *h)
6411edd16368SStephen M. Cameron {
6412edd16368SStephen M. Cameron 	return h->access.intr_pending(h);
6413edd16368SStephen M. Cameron }
6414edd16368SStephen M. Cameron 
6415edd16368SStephen M. Cameron static inline long interrupt_not_for_us(struct ctlr_info *h)
6416edd16368SStephen M. Cameron {
641710f66018SStephen M. Cameron 	return (h->access.intr_pending(h) == 0) ||
641810f66018SStephen M. Cameron 		(h->interrupts_enabled == 0);
6419edd16368SStephen M. Cameron }
6420edd16368SStephen M. Cameron 
642101a02ffcSStephen M. Cameron static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
642201a02ffcSStephen M. Cameron 	u32 raw_tag)
6423edd16368SStephen M. Cameron {
6424edd16368SStephen M. Cameron 	if (unlikely(tag_index >= h->nr_cmds)) {
6425edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
6426edd16368SStephen M. Cameron 		return 1;
6427edd16368SStephen M. Cameron 	}
6428edd16368SStephen M. Cameron 	return 0;
6429edd16368SStephen M. Cameron }
6430edd16368SStephen M. Cameron 
64315a3d16f5SStephen M. Cameron static inline void finish_cmd(struct CommandList *c)
6432edd16368SStephen M. Cameron {
6433e85c5974SStephen M. Cameron 	dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
6434c349775eSScott Teel 	if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
6435c349775eSScott Teel 			|| c->cmd_type == CMD_IOACCEL2))
64361fb011fbSStephen M. Cameron 		complete_scsi_command(c);
64378be986ccSStephen Cameron 	else if (c->cmd_type == CMD_IOCTL_PEND || c->cmd_type == IOACCEL2_TMF)
6438edd16368SStephen M. Cameron 		complete(c->waiting);
6439a104c99fSStephen M. Cameron }
6440a104c99fSStephen M. Cameron 
6441a9a3a273SStephen M. Cameron 
6442a9a3a273SStephen M. Cameron static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
6443a104c99fSStephen M. Cameron {
6444a9a3a273SStephen M. Cameron #define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
6445a9a3a273SStephen M. Cameron #define HPSA_SIMPLE_ERROR_BITS 0x03
6446960a30e7SStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
6447a9a3a273SStephen M. Cameron 		return tag & ~HPSA_SIMPLE_ERROR_BITS;
6448a9a3a273SStephen M. Cameron 	return tag & ~HPSA_PERF_ERROR_BITS;
6449a104c99fSStephen M. Cameron }
6450a104c99fSStephen M. Cameron 
6451303932fdSDon Brace /* process completion of an indexed ("direct lookup") command */
64521d94f94dSStephen M. Cameron static inline void process_indexed_cmd(struct ctlr_info *h,
6453303932fdSDon Brace 	u32 raw_tag)
6454303932fdSDon Brace {
6455303932fdSDon Brace 	u32 tag_index;
6456303932fdSDon Brace 	struct CommandList *c;
6457303932fdSDon Brace 
6458f2405db8SDon Brace 	tag_index = raw_tag >> DIRECT_LOOKUP_SHIFT;
64591d94f94dSStephen M. Cameron 	if (!bad_tag(h, tag_index, raw_tag)) {
6460303932fdSDon Brace 		c = h->cmd_pool + tag_index;
64615a3d16f5SStephen M. Cameron 		finish_cmd(c);
64621d94f94dSStephen M. Cameron 	}
6463303932fdSDon Brace }
6464303932fdSDon Brace 
646564670ac8SStephen M. Cameron /* Some controllers, like p400, will give us one interrupt
646664670ac8SStephen M. Cameron  * after a soft reset, even if we turned interrupts off.
646764670ac8SStephen M. Cameron  * Only need to check for this in the hpsa_xxx_discard_completions
646864670ac8SStephen M. Cameron  * functions.
646964670ac8SStephen M. Cameron  */
647064670ac8SStephen M. Cameron static int ignore_bogus_interrupt(struct ctlr_info *h)
647164670ac8SStephen M. Cameron {
647264670ac8SStephen M. Cameron 	if (likely(!reset_devices))
647364670ac8SStephen M. Cameron 		return 0;
647464670ac8SStephen M. Cameron 
647564670ac8SStephen M. Cameron 	if (likely(h->interrupts_enabled))
647664670ac8SStephen M. Cameron 		return 0;
647764670ac8SStephen M. Cameron 
647864670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
647964670ac8SStephen M. Cameron 		"(known firmware bug.)  Ignoring.\n");
648064670ac8SStephen M. Cameron 
648164670ac8SStephen M. Cameron 	return 1;
648264670ac8SStephen M. Cameron }
648364670ac8SStephen M. Cameron 
6484254f796bSMatt Gates /*
6485254f796bSMatt Gates  * Convert &h->q[x] (passed to interrupt handlers) back to h.
6486254f796bSMatt Gates  * Relies on (h-q[x] == x) being true for x such that
6487254f796bSMatt Gates  * 0 <= x < MAX_REPLY_QUEUES.
6488254f796bSMatt Gates  */
6489254f796bSMatt Gates static struct ctlr_info *queue_to_hba(u8 *queue)
649064670ac8SStephen M. Cameron {
6491254f796bSMatt Gates 	return container_of((queue - *queue), struct ctlr_info, q[0]);
6492254f796bSMatt Gates }
6493254f796bSMatt Gates 
6494254f796bSMatt Gates static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
6495254f796bSMatt Gates {
6496254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
6497254f796bSMatt Gates 	u8 q = *(u8 *) queue;
649864670ac8SStephen M. Cameron 	u32 raw_tag;
649964670ac8SStephen M. Cameron 
650064670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
650164670ac8SStephen M. Cameron 		return IRQ_NONE;
650264670ac8SStephen M. Cameron 
650364670ac8SStephen M. Cameron 	if (interrupt_not_for_us(h))
650464670ac8SStephen M. Cameron 		return IRQ_NONE;
6505a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
650664670ac8SStephen M. Cameron 	while (interrupt_pending(h)) {
6507254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
650864670ac8SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY)
6509254f796bSMatt Gates 			raw_tag = next_command(h, q);
651064670ac8SStephen M. Cameron 	}
651164670ac8SStephen M. Cameron 	return IRQ_HANDLED;
651264670ac8SStephen M. Cameron }
651364670ac8SStephen M. Cameron 
6514254f796bSMatt Gates static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
651564670ac8SStephen M. Cameron {
6516254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
651764670ac8SStephen M. Cameron 	u32 raw_tag;
6518254f796bSMatt Gates 	u8 q = *(u8 *) queue;
651964670ac8SStephen M. Cameron 
652064670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
652164670ac8SStephen M. Cameron 		return IRQ_NONE;
652264670ac8SStephen M. Cameron 
6523a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
6524254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
652564670ac8SStephen M. Cameron 	while (raw_tag != FIFO_EMPTY)
6526254f796bSMatt Gates 		raw_tag = next_command(h, q);
652764670ac8SStephen M. Cameron 	return IRQ_HANDLED;
652864670ac8SStephen M. Cameron }
652964670ac8SStephen M. Cameron 
6530254f796bSMatt Gates static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
6531edd16368SStephen M. Cameron {
6532254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba((u8 *) queue);
6533303932fdSDon Brace 	u32 raw_tag;
6534254f796bSMatt Gates 	u8 q = *(u8 *) queue;
6535edd16368SStephen M. Cameron 
6536edd16368SStephen M. Cameron 	if (interrupt_not_for_us(h))
6537edd16368SStephen M. Cameron 		return IRQ_NONE;
6538a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
653910f66018SStephen M. Cameron 	while (interrupt_pending(h)) {
6540254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
654110f66018SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY) {
65421d94f94dSStephen M. Cameron 			process_indexed_cmd(h, raw_tag);
6543254f796bSMatt Gates 			raw_tag = next_command(h, q);
654410f66018SStephen M. Cameron 		}
654510f66018SStephen M. Cameron 	}
654610f66018SStephen M. Cameron 	return IRQ_HANDLED;
654710f66018SStephen M. Cameron }
654810f66018SStephen M. Cameron 
6549254f796bSMatt Gates static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
655010f66018SStephen M. Cameron {
6551254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
655210f66018SStephen M. Cameron 	u32 raw_tag;
6553254f796bSMatt Gates 	u8 q = *(u8 *) queue;
655410f66018SStephen M. Cameron 
6555a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
6556254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
6557303932fdSDon Brace 	while (raw_tag != FIFO_EMPTY) {
65581d94f94dSStephen M. Cameron 		process_indexed_cmd(h, raw_tag);
6559254f796bSMatt Gates 		raw_tag = next_command(h, q);
6560edd16368SStephen M. Cameron 	}
6561edd16368SStephen M. Cameron 	return IRQ_HANDLED;
6562edd16368SStephen M. Cameron }
6563edd16368SStephen M. Cameron 
6564a9a3a273SStephen M. Cameron /* Send a message CDB to the firmware. Careful, this only works
6565a9a3a273SStephen M. Cameron  * in simple mode, not performant mode due to the tag lookup.
6566a9a3a273SStephen M. Cameron  * We only ever use this immediately after a controller reset.
6567a9a3a273SStephen M. Cameron  */
65686f039790SGreg Kroah-Hartman static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
6569edd16368SStephen M. Cameron 			unsigned char type)
6570edd16368SStephen M. Cameron {
6571edd16368SStephen M. Cameron 	struct Command {
6572edd16368SStephen M. Cameron 		struct CommandListHeader CommandHeader;
6573edd16368SStephen M. Cameron 		struct RequestBlock Request;
6574edd16368SStephen M. Cameron 		struct ErrDescriptor ErrorDescriptor;
6575edd16368SStephen M. Cameron 	};
6576edd16368SStephen M. Cameron 	struct Command *cmd;
6577edd16368SStephen M. Cameron 	static const size_t cmd_sz = sizeof(*cmd) +
6578edd16368SStephen M. Cameron 					sizeof(cmd->ErrorDescriptor);
6579edd16368SStephen M. Cameron 	dma_addr_t paddr64;
65802b08b3e9SDon Brace 	__le32 paddr32;
65812b08b3e9SDon Brace 	u32 tag;
6582edd16368SStephen M. Cameron 	void __iomem *vaddr;
6583edd16368SStephen M. Cameron 	int i, err;
6584edd16368SStephen M. Cameron 
6585edd16368SStephen M. Cameron 	vaddr = pci_ioremap_bar(pdev, 0);
6586edd16368SStephen M. Cameron 	if (vaddr == NULL)
6587edd16368SStephen M. Cameron 		return -ENOMEM;
6588edd16368SStephen M. Cameron 
6589edd16368SStephen M. Cameron 	/* The Inbound Post Queue only accepts 32-bit physical addresses for the
6590edd16368SStephen M. Cameron 	 * CCISS commands, so they must be allocated from the lower 4GiB of
6591edd16368SStephen M. Cameron 	 * memory.
6592edd16368SStephen M. Cameron 	 */
6593edd16368SStephen M. Cameron 	err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
6594edd16368SStephen M. Cameron 	if (err) {
6595edd16368SStephen M. Cameron 		iounmap(vaddr);
65961eaec8f3SRobert Elliott 		return err;
6597edd16368SStephen M. Cameron 	}
6598edd16368SStephen M. Cameron 
6599edd16368SStephen M. Cameron 	cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
6600edd16368SStephen M. Cameron 	if (cmd == NULL) {
6601edd16368SStephen M. Cameron 		iounmap(vaddr);
6602edd16368SStephen M. Cameron 		return -ENOMEM;
6603edd16368SStephen M. Cameron 	}
6604edd16368SStephen M. Cameron 
6605edd16368SStephen M. Cameron 	/* This must fit, because of the 32-bit consistent DMA mask.  Also,
6606edd16368SStephen M. Cameron 	 * although there's no guarantee, we assume that the address is at
6607edd16368SStephen M. Cameron 	 * least 4-byte aligned (most likely, it's page-aligned).
6608edd16368SStephen M. Cameron 	 */
66092b08b3e9SDon Brace 	paddr32 = cpu_to_le32(paddr64);
6610edd16368SStephen M. Cameron 
6611edd16368SStephen M. Cameron 	cmd->CommandHeader.ReplyQueue = 0;
6612edd16368SStephen M. Cameron 	cmd->CommandHeader.SGList = 0;
661350a0decfSStephen M. Cameron 	cmd->CommandHeader.SGTotal = cpu_to_le16(0);
66142b08b3e9SDon Brace 	cmd->CommandHeader.tag = cpu_to_le64(paddr64);
6615edd16368SStephen M. Cameron 	memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
6616edd16368SStephen M. Cameron 
6617edd16368SStephen M. Cameron 	cmd->Request.CDBLen = 16;
6618a505b86fSStephen M. Cameron 	cmd->Request.type_attr_dir =
6619a505b86fSStephen M. Cameron 			TYPE_ATTR_DIR(TYPE_MSG, ATTR_HEADOFQUEUE, XFER_NONE);
6620edd16368SStephen M. Cameron 	cmd->Request.Timeout = 0; /* Don't time out */
6621edd16368SStephen M. Cameron 	cmd->Request.CDB[0] = opcode;
6622edd16368SStephen M. Cameron 	cmd->Request.CDB[1] = type;
6623edd16368SStephen M. Cameron 	memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
662450a0decfSStephen M. Cameron 	cmd->ErrorDescriptor.Addr =
66252b08b3e9SDon Brace 			cpu_to_le64((le32_to_cpu(paddr32) + sizeof(*cmd)));
662650a0decfSStephen M. Cameron 	cmd->ErrorDescriptor.Len = cpu_to_le32(sizeof(struct ErrorInfo));
6627edd16368SStephen M. Cameron 
66282b08b3e9SDon Brace 	writel(le32_to_cpu(paddr32), vaddr + SA5_REQUEST_PORT_OFFSET);
6629edd16368SStephen M. Cameron 
6630edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
6631edd16368SStephen M. Cameron 		tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
66322b08b3e9SDon Brace 		if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr64)
6633edd16368SStephen M. Cameron 			break;
6634edd16368SStephen M. Cameron 		msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
6635edd16368SStephen M. Cameron 	}
6636edd16368SStephen M. Cameron 
6637edd16368SStephen M. Cameron 	iounmap(vaddr);
6638edd16368SStephen M. Cameron 
6639edd16368SStephen M. Cameron 	/* we leak the DMA buffer here ... no choice since the controller could
6640edd16368SStephen M. Cameron 	 *  still complete the command.
6641edd16368SStephen M. Cameron 	 */
6642edd16368SStephen M. Cameron 	if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
6643edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
6644edd16368SStephen M. Cameron 			opcode, type);
6645edd16368SStephen M. Cameron 		return -ETIMEDOUT;
6646edd16368SStephen M. Cameron 	}
6647edd16368SStephen M. Cameron 
6648edd16368SStephen M. Cameron 	pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
6649edd16368SStephen M. Cameron 
6650edd16368SStephen M. Cameron 	if (tag & HPSA_ERROR_BIT) {
6651edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
6652edd16368SStephen M. Cameron 			opcode, type);
6653edd16368SStephen M. Cameron 		return -EIO;
6654edd16368SStephen M. Cameron 	}
6655edd16368SStephen M. Cameron 
6656edd16368SStephen M. Cameron 	dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
6657edd16368SStephen M. Cameron 		opcode, type);
6658edd16368SStephen M. Cameron 	return 0;
6659edd16368SStephen M. Cameron }
6660edd16368SStephen M. Cameron 
6661edd16368SStephen M. Cameron #define hpsa_noop(p) hpsa_message(p, 3, 0)
6662edd16368SStephen M. Cameron 
66631df8552aSStephen M. Cameron static int hpsa_controller_hard_reset(struct pci_dev *pdev,
666442a91641SDon Brace 	void __iomem *vaddr, u32 use_doorbell)
6665edd16368SStephen M. Cameron {
6666edd16368SStephen M. Cameron 
66671df8552aSStephen M. Cameron 	if (use_doorbell) {
66681df8552aSStephen M. Cameron 		/* For everything after the P600, the PCI power state method
66691df8552aSStephen M. Cameron 		 * of resetting the controller doesn't work, so we have this
66701df8552aSStephen M. Cameron 		 * other way using the doorbell register.
6671edd16368SStephen M. Cameron 		 */
66721df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using doorbell to reset controller\n");
6673cf0b08d0SStephen M. Cameron 		writel(use_doorbell, vaddr + SA5_DOORBELL);
667485009239SStephen M. Cameron 
667500701a96SJustin Lindley 		/* PMC hardware guys tell us we need a 10 second delay after
667685009239SStephen M. Cameron 		 * doorbell reset and before any attempt to talk to the board
667785009239SStephen M. Cameron 		 * at all to ensure that this actually works and doesn't fall
667885009239SStephen M. Cameron 		 * over in some weird corner cases.
667985009239SStephen M. Cameron 		 */
668000701a96SJustin Lindley 		msleep(10000);
66811df8552aSStephen M. Cameron 	} else { /* Try to do it the PCI power state way */
6682edd16368SStephen M. Cameron 
6683edd16368SStephen M. Cameron 		/* Quoting from the Open CISS Specification: "The Power
6684edd16368SStephen M. Cameron 		 * Management Control/Status Register (CSR) controls the power
6685edd16368SStephen M. Cameron 		 * state of the device.  The normal operating state is D0,
6686edd16368SStephen M. Cameron 		 * CSR=00h.  The software off state is D3, CSR=03h.  To reset
66871df8552aSStephen M. Cameron 		 * the controller, place the interface device in D3 then to D0,
66881df8552aSStephen M. Cameron 		 * this causes a secondary PCI reset which will reset the
66891df8552aSStephen M. Cameron 		 * controller." */
6690edd16368SStephen M. Cameron 
66912662cab8SDon Brace 		int rc = 0;
66922662cab8SDon Brace 
66931df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using PCI PM to reset controller\n");
66942662cab8SDon Brace 
6695edd16368SStephen M. Cameron 		/* enter the D3hot power management state */
66962662cab8SDon Brace 		rc = pci_set_power_state(pdev, PCI_D3hot);
66972662cab8SDon Brace 		if (rc)
66982662cab8SDon Brace 			return rc;
6699edd16368SStephen M. Cameron 
6700edd16368SStephen M. Cameron 		msleep(500);
6701edd16368SStephen M. Cameron 
6702edd16368SStephen M. Cameron 		/* enter the D0 power management state */
67032662cab8SDon Brace 		rc = pci_set_power_state(pdev, PCI_D0);
67042662cab8SDon Brace 		if (rc)
67052662cab8SDon Brace 			return rc;
6706c4853efeSMike Miller 
6707c4853efeSMike Miller 		/*
6708c4853efeSMike Miller 		 * The P600 requires a small delay when changing states.
6709c4853efeSMike Miller 		 * Otherwise we may think the board did not reset and we bail.
6710c4853efeSMike Miller 		 * This for kdump only and is particular to the P600.
6711c4853efeSMike Miller 		 */
6712c4853efeSMike Miller 		msleep(500);
67131df8552aSStephen M. Cameron 	}
67141df8552aSStephen M. Cameron 	return 0;
67151df8552aSStephen M. Cameron }
67161df8552aSStephen M. Cameron 
67176f039790SGreg Kroah-Hartman static void init_driver_version(char *driver_version, int len)
6718580ada3cSStephen M. Cameron {
6719580ada3cSStephen M. Cameron 	memset(driver_version, 0, len);
6720f79cfec6SStephen M. Cameron 	strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
6721580ada3cSStephen M. Cameron }
6722580ada3cSStephen M. Cameron 
67236f039790SGreg Kroah-Hartman static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
6724580ada3cSStephen M. Cameron {
6725580ada3cSStephen M. Cameron 	char *driver_version;
6726580ada3cSStephen M. Cameron 	int i, size = sizeof(cfgtable->driver_version);
6727580ada3cSStephen M. Cameron 
6728580ada3cSStephen M. Cameron 	driver_version = kmalloc(size, GFP_KERNEL);
6729580ada3cSStephen M. Cameron 	if (!driver_version)
6730580ada3cSStephen M. Cameron 		return -ENOMEM;
6731580ada3cSStephen M. Cameron 
6732580ada3cSStephen M. Cameron 	init_driver_version(driver_version, size);
6733580ada3cSStephen M. Cameron 	for (i = 0; i < size; i++)
6734580ada3cSStephen M. Cameron 		writeb(driver_version[i], &cfgtable->driver_version[i]);
6735580ada3cSStephen M. Cameron 	kfree(driver_version);
6736580ada3cSStephen M. Cameron 	return 0;
6737580ada3cSStephen M. Cameron }
6738580ada3cSStephen M. Cameron 
67396f039790SGreg Kroah-Hartman static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
67406f039790SGreg Kroah-Hartman 					  unsigned char *driver_ver)
6741580ada3cSStephen M. Cameron {
6742580ada3cSStephen M. Cameron 	int i;
6743580ada3cSStephen M. Cameron 
6744580ada3cSStephen M. Cameron 	for (i = 0; i < sizeof(cfgtable->driver_version); i++)
6745580ada3cSStephen M. Cameron 		driver_ver[i] = readb(&cfgtable->driver_version[i]);
6746580ada3cSStephen M. Cameron }
6747580ada3cSStephen M. Cameron 
67486f039790SGreg Kroah-Hartman static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
6749580ada3cSStephen M. Cameron {
6750580ada3cSStephen M. Cameron 
6751580ada3cSStephen M. Cameron 	char *driver_ver, *old_driver_ver;
6752580ada3cSStephen M. Cameron 	int rc, size = sizeof(cfgtable->driver_version);
6753580ada3cSStephen M. Cameron 
6754580ada3cSStephen M. Cameron 	old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
6755580ada3cSStephen M. Cameron 	if (!old_driver_ver)
6756580ada3cSStephen M. Cameron 		return -ENOMEM;
6757580ada3cSStephen M. Cameron 	driver_ver = old_driver_ver + size;
6758580ada3cSStephen M. Cameron 
6759580ada3cSStephen M. Cameron 	/* After a reset, the 32 bytes of "driver version" in the cfgtable
6760580ada3cSStephen M. Cameron 	 * should have been changed, otherwise we know the reset failed.
6761580ada3cSStephen M. Cameron 	 */
6762580ada3cSStephen M. Cameron 	init_driver_version(old_driver_ver, size);
6763580ada3cSStephen M. Cameron 	read_driver_ver_from_cfgtable(cfgtable, driver_ver);
6764580ada3cSStephen M. Cameron 	rc = !memcmp(driver_ver, old_driver_ver, size);
6765580ada3cSStephen M. Cameron 	kfree(old_driver_ver);
6766580ada3cSStephen M. Cameron 	return rc;
6767580ada3cSStephen M. Cameron }
67681df8552aSStephen M. Cameron /* This does a hard reset of the controller using PCI power management
67691df8552aSStephen M. Cameron  * states or the using the doorbell register.
67701df8552aSStephen M. Cameron  */
67716b6c1cd7STomas Henzl static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev, u32 board_id)
67721df8552aSStephen M. Cameron {
67731df8552aSStephen M. Cameron 	u64 cfg_offset;
67741df8552aSStephen M. Cameron 	u32 cfg_base_addr;
67751df8552aSStephen M. Cameron 	u64 cfg_base_addr_index;
67761df8552aSStephen M. Cameron 	void __iomem *vaddr;
67771df8552aSStephen M. Cameron 	unsigned long paddr;
6778580ada3cSStephen M. Cameron 	u32 misc_fw_support;
6779270d05deSStephen M. Cameron 	int rc;
67801df8552aSStephen M. Cameron 	struct CfgTable __iomem *cfgtable;
6781cf0b08d0SStephen M. Cameron 	u32 use_doorbell;
6782270d05deSStephen M. Cameron 	u16 command_register;
67831df8552aSStephen M. Cameron 
67841df8552aSStephen M. Cameron 	/* For controllers as old as the P600, this is very nearly
67851df8552aSStephen M. Cameron 	 * the same thing as
67861df8552aSStephen M. Cameron 	 *
67871df8552aSStephen M. Cameron 	 * pci_save_state(pci_dev);
67881df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D3hot);
67891df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D0);
67901df8552aSStephen M. Cameron 	 * pci_restore_state(pci_dev);
67911df8552aSStephen M. Cameron 	 *
67921df8552aSStephen M. Cameron 	 * For controllers newer than the P600, the pci power state
67931df8552aSStephen M. Cameron 	 * method of resetting doesn't work so we have another way
67941df8552aSStephen M. Cameron 	 * using the doorbell register.
67951df8552aSStephen M. Cameron 	 */
679618867659SStephen M. Cameron 
679760f923b9SRobert Elliott 	if (!ctlr_is_resettable(board_id)) {
679860f923b9SRobert Elliott 		dev_warn(&pdev->dev, "Controller not resettable\n");
679925c1e56aSStephen M. Cameron 		return -ENODEV;
680025c1e56aSStephen M. Cameron 	}
680146380786SStephen M. Cameron 
680246380786SStephen M. Cameron 	/* if controller is soft- but not hard resettable... */
680346380786SStephen M. Cameron 	if (!ctlr_is_hard_resettable(board_id))
680446380786SStephen M. Cameron 		return -ENOTSUPP; /* try soft reset later. */
680518867659SStephen M. Cameron 
6806270d05deSStephen M. Cameron 	/* Save the PCI command register */
6807270d05deSStephen M. Cameron 	pci_read_config_word(pdev, 4, &command_register);
6808270d05deSStephen M. Cameron 	pci_save_state(pdev);
68091df8552aSStephen M. Cameron 
68101df8552aSStephen M. Cameron 	/* find the first memory BAR, so we can find the cfg table */
68111df8552aSStephen M. Cameron 	rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
68121df8552aSStephen M. Cameron 	if (rc)
68131df8552aSStephen M. Cameron 		return rc;
68141df8552aSStephen M. Cameron 	vaddr = remap_pci_mem(paddr, 0x250);
68151df8552aSStephen M. Cameron 	if (!vaddr)
68161df8552aSStephen M. Cameron 		return -ENOMEM;
68171df8552aSStephen M. Cameron 
68181df8552aSStephen M. Cameron 	/* find cfgtable in order to check if reset via doorbell is supported */
68191df8552aSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
68201df8552aSStephen M. Cameron 					&cfg_base_addr_index, &cfg_offset);
68211df8552aSStephen M. Cameron 	if (rc)
68221df8552aSStephen M. Cameron 		goto unmap_vaddr;
68231df8552aSStephen M. Cameron 	cfgtable = remap_pci_mem(pci_resource_start(pdev,
68241df8552aSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
68251df8552aSStephen M. Cameron 	if (!cfgtable) {
68261df8552aSStephen M. Cameron 		rc = -ENOMEM;
68271df8552aSStephen M. Cameron 		goto unmap_vaddr;
68281df8552aSStephen M. Cameron 	}
6829580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(cfgtable);
6830580ada3cSStephen M. Cameron 	if (rc)
683103741d95STomas Henzl 		goto unmap_cfgtable;
68321df8552aSStephen M. Cameron 
6833cf0b08d0SStephen M. Cameron 	/* If reset via doorbell register is supported, use that.
6834cf0b08d0SStephen M. Cameron 	 * There are two such methods.  Favor the newest method.
6835cf0b08d0SStephen M. Cameron 	 */
68361df8552aSStephen M. Cameron 	misc_fw_support = readl(&cfgtable->misc_fw_support);
6837cf0b08d0SStephen M. Cameron 	use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
6838cf0b08d0SStephen M. Cameron 	if (use_doorbell) {
6839cf0b08d0SStephen M. Cameron 		use_doorbell = DOORBELL_CTLR_RESET2;
6840cf0b08d0SStephen M. Cameron 	} else {
68411df8552aSStephen M. Cameron 		use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
6842cf0b08d0SStephen M. Cameron 		if (use_doorbell) {
6843050f7147SStephen Cameron 			dev_warn(&pdev->dev,
6844050f7147SStephen Cameron 				"Soft reset not supported. Firmware update is required.\n");
684564670ac8SStephen M. Cameron 			rc = -ENOTSUPP; /* try soft reset */
6846cf0b08d0SStephen M. Cameron 			goto unmap_cfgtable;
6847cf0b08d0SStephen M. Cameron 		}
6848cf0b08d0SStephen M. Cameron 	}
68491df8552aSStephen M. Cameron 
68501df8552aSStephen M. Cameron 	rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
68511df8552aSStephen M. Cameron 	if (rc)
68521df8552aSStephen M. Cameron 		goto unmap_cfgtable;
6853edd16368SStephen M. Cameron 
6854270d05deSStephen M. Cameron 	pci_restore_state(pdev);
6855270d05deSStephen M. Cameron 	pci_write_config_word(pdev, 4, command_register);
6856edd16368SStephen M. Cameron 
68571df8552aSStephen M. Cameron 	/* Some devices (notably the HP Smart Array 5i Controller)
68581df8552aSStephen M. Cameron 	   need a little pause here */
68591df8552aSStephen M. Cameron 	msleep(HPSA_POST_RESET_PAUSE_MSECS);
68601df8552aSStephen M. Cameron 
6861fe5389c8SStephen M. Cameron 	rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
6862fe5389c8SStephen M. Cameron 	if (rc) {
6863fe5389c8SStephen M. Cameron 		dev_warn(&pdev->dev,
6864050f7147SStephen Cameron 			"Failed waiting for board to become ready after hard reset\n");
6865fe5389c8SStephen M. Cameron 		goto unmap_cfgtable;
6866fe5389c8SStephen M. Cameron 	}
6867fe5389c8SStephen M. Cameron 
6868580ada3cSStephen M. Cameron 	rc = controller_reset_failed(vaddr);
6869580ada3cSStephen M. Cameron 	if (rc < 0)
6870580ada3cSStephen M. Cameron 		goto unmap_cfgtable;
6871580ada3cSStephen M. Cameron 	if (rc) {
687264670ac8SStephen M. Cameron 		dev_warn(&pdev->dev, "Unable to successfully reset "
687364670ac8SStephen M. Cameron 			"controller. Will try soft reset.\n");
687464670ac8SStephen M. Cameron 		rc = -ENOTSUPP;
6875580ada3cSStephen M. Cameron 	} else {
687664670ac8SStephen M. Cameron 		dev_info(&pdev->dev, "board ready after hard reset.\n");
68771df8552aSStephen M. Cameron 	}
68781df8552aSStephen M. Cameron 
68791df8552aSStephen M. Cameron unmap_cfgtable:
68801df8552aSStephen M. Cameron 	iounmap(cfgtable);
68811df8552aSStephen M. Cameron 
68821df8552aSStephen M. Cameron unmap_vaddr:
68831df8552aSStephen M. Cameron 	iounmap(vaddr);
68841df8552aSStephen M. Cameron 	return rc;
6885edd16368SStephen M. Cameron }
6886edd16368SStephen M. Cameron 
6887edd16368SStephen M. Cameron /*
6888edd16368SStephen M. Cameron  *  We cannot read the structure directly, for portability we must use
6889edd16368SStephen M. Cameron  *   the io functions.
6890edd16368SStephen M. Cameron  *   This is for debug only.
6891edd16368SStephen M. Cameron  */
689242a91641SDon Brace static void print_cfg_table(struct device *dev, struct CfgTable __iomem *tb)
6893edd16368SStephen M. Cameron {
689458f8665cSStephen M. Cameron #ifdef HPSA_DEBUG
6895edd16368SStephen M. Cameron 	int i;
6896edd16368SStephen M. Cameron 	char temp_name[17];
6897edd16368SStephen M. Cameron 
6898edd16368SStephen M. Cameron 	dev_info(dev, "Controller Configuration information\n");
6899edd16368SStephen M. Cameron 	dev_info(dev, "------------------------------------\n");
6900edd16368SStephen M. Cameron 	for (i = 0; i < 4; i++)
6901edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->Signature[i]));
6902edd16368SStephen M. Cameron 	temp_name[4] = '\0';
6903edd16368SStephen M. Cameron 	dev_info(dev, "   Signature = %s\n", temp_name);
6904edd16368SStephen M. Cameron 	dev_info(dev, "   Spec Number = %d\n", readl(&(tb->SpecValence)));
6905edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods supported = 0x%x\n",
6906edd16368SStephen M. Cameron 	       readl(&(tb->TransportSupport)));
6907edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods active = 0x%x\n",
6908edd16368SStephen M. Cameron 	       readl(&(tb->TransportActive)));
6909edd16368SStephen M. Cameron 	dev_info(dev, "   Requested transport Method = 0x%x\n",
6910edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.TransportRequest)));
6911edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Delay = 0x%x\n",
6912edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntDelay)));
6913edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Count = 0x%x\n",
6914edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntCount)));
691569d6e33dSRobert Elliott 	dev_info(dev, "   Max outstanding commands = %d\n",
6916edd16368SStephen M. Cameron 	       readl(&(tb->CmdsOutMax)));
6917edd16368SStephen M. Cameron 	dev_info(dev, "   Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
6918edd16368SStephen M. Cameron 	for (i = 0; i < 16; i++)
6919edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->ServerName[i]));
6920edd16368SStephen M. Cameron 	temp_name[16] = '\0';
6921edd16368SStephen M. Cameron 	dev_info(dev, "   Server Name = %s\n", temp_name);
6922edd16368SStephen M. Cameron 	dev_info(dev, "   Heartbeat Counter = 0x%x\n\n\n",
6923edd16368SStephen M. Cameron 		readl(&(tb->HeartBeat)));
6924edd16368SStephen M. Cameron #endif				/* HPSA_DEBUG */
692558f8665cSStephen M. Cameron }
6926edd16368SStephen M. Cameron 
6927edd16368SStephen M. Cameron static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
6928edd16368SStephen M. Cameron {
6929edd16368SStephen M. Cameron 	int i, offset, mem_type, bar_type;
6930edd16368SStephen M. Cameron 
6931edd16368SStephen M. Cameron 	if (pci_bar_addr == PCI_BASE_ADDRESS_0)	/* looking for BAR zero? */
6932edd16368SStephen M. Cameron 		return 0;
6933edd16368SStephen M. Cameron 	offset = 0;
6934edd16368SStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
6935edd16368SStephen M. Cameron 		bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
6936edd16368SStephen M. Cameron 		if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
6937edd16368SStephen M. Cameron 			offset += 4;
6938edd16368SStephen M. Cameron 		else {
6939edd16368SStephen M. Cameron 			mem_type = pci_resource_flags(pdev, i) &
6940edd16368SStephen M. Cameron 			    PCI_BASE_ADDRESS_MEM_TYPE_MASK;
6941edd16368SStephen M. Cameron 			switch (mem_type) {
6942edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_32:
6943edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_1M:
6944edd16368SStephen M. Cameron 				offset += 4;	/* 32 bit */
6945edd16368SStephen M. Cameron 				break;
6946edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_64:
6947edd16368SStephen M. Cameron 				offset += 8;
6948edd16368SStephen M. Cameron 				break;
6949edd16368SStephen M. Cameron 			default:	/* reserved in PCI 2.2 */
6950edd16368SStephen M. Cameron 				dev_warn(&pdev->dev,
6951edd16368SStephen M. Cameron 				       "base address is invalid\n");
6952edd16368SStephen M. Cameron 				return -1;
6953edd16368SStephen M. Cameron 				break;
6954edd16368SStephen M. Cameron 			}
6955edd16368SStephen M. Cameron 		}
6956edd16368SStephen M. Cameron 		if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
6957edd16368SStephen M. Cameron 			return i + 1;
6958edd16368SStephen M. Cameron 	}
6959edd16368SStephen M. Cameron 	return -1;
6960edd16368SStephen M. Cameron }
6961edd16368SStephen M. Cameron 
6962cc64c817SRobert Elliott static void hpsa_disable_interrupt_mode(struct ctlr_info *h)
6963cc64c817SRobert Elliott {
6964cc64c817SRobert Elliott 	if (h->msix_vector) {
6965cc64c817SRobert Elliott 		if (h->pdev->msix_enabled)
6966cc64c817SRobert Elliott 			pci_disable_msix(h->pdev);
6967105a3dbcSRobert Elliott 		h->msix_vector = 0;
6968cc64c817SRobert Elliott 	} else if (h->msi_vector) {
6969cc64c817SRobert Elliott 		if (h->pdev->msi_enabled)
6970cc64c817SRobert Elliott 			pci_disable_msi(h->pdev);
6971105a3dbcSRobert Elliott 		h->msi_vector = 0;
6972cc64c817SRobert Elliott 	}
6973cc64c817SRobert Elliott }
6974cc64c817SRobert Elliott 
6975edd16368SStephen M. Cameron /* If MSI/MSI-X is supported by the kernel we will try to enable it on
6976050f7147SStephen Cameron  * controllers that are capable. If not, we use legacy INTx mode.
6977edd16368SStephen M. Cameron  */
69786f039790SGreg Kroah-Hartman static void hpsa_interrupt_mode(struct ctlr_info *h)
6979edd16368SStephen M. Cameron {
6980edd16368SStephen M. Cameron #ifdef CONFIG_PCI_MSI
6981254f796bSMatt Gates 	int err, i;
6982254f796bSMatt Gates 	struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
6983254f796bSMatt Gates 
6984254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++) {
6985254f796bSMatt Gates 		hpsa_msix_entries[i].vector = 0;
6986254f796bSMatt Gates 		hpsa_msix_entries[i].entry = i;
6987254f796bSMatt Gates 	}
6988edd16368SStephen M. Cameron 
6989edd16368SStephen M. Cameron 	/* Some boards advertise MSI but don't really support it */
69906b3f4c52SStephen M. Cameron 	if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
69916b3f4c52SStephen M. Cameron 	    (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
6992edd16368SStephen M. Cameron 		goto default_int_mode;
699355c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
6994050f7147SStephen Cameron 		dev_info(&h->pdev->dev, "MSI-X capable controller\n");
6995eee0f03aSHannes Reinecke 		h->msix_vector = MAX_REPLY_QUEUES;
6996f89439bcSStephen M. Cameron 		if (h->msix_vector > num_online_cpus())
6997f89439bcSStephen M. Cameron 			h->msix_vector = num_online_cpus();
699818fce3c4SAlexander Gordeev 		err = pci_enable_msix_range(h->pdev, hpsa_msix_entries,
699918fce3c4SAlexander Gordeev 					    1, h->msix_vector);
700018fce3c4SAlexander Gordeev 		if (err < 0) {
700118fce3c4SAlexander Gordeev 			dev_warn(&h->pdev->dev, "MSI-X init failed %d\n", err);
700218fce3c4SAlexander Gordeev 			h->msix_vector = 0;
700318fce3c4SAlexander Gordeev 			goto single_msi_mode;
700418fce3c4SAlexander Gordeev 		} else if (err < h->msix_vector) {
700555c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
7006edd16368SStephen M. Cameron 			       "available\n", err);
7007eee0f03aSHannes Reinecke 		}
700818fce3c4SAlexander Gordeev 		h->msix_vector = err;
7009eee0f03aSHannes Reinecke 		for (i = 0; i < h->msix_vector; i++)
7010eee0f03aSHannes Reinecke 			h->intr[i] = hpsa_msix_entries[i].vector;
7011eee0f03aSHannes Reinecke 		return;
7012edd16368SStephen M. Cameron 	}
701318fce3c4SAlexander Gordeev single_msi_mode:
701455c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
7015050f7147SStephen Cameron 		dev_info(&h->pdev->dev, "MSI capable controller\n");
701655c06c71SStephen M. Cameron 		if (!pci_enable_msi(h->pdev))
7017edd16368SStephen M. Cameron 			h->msi_vector = 1;
7018edd16368SStephen M. Cameron 		else
701955c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "MSI init failed\n");
7020edd16368SStephen M. Cameron 	}
7021edd16368SStephen M. Cameron default_int_mode:
7022edd16368SStephen M. Cameron #endif				/* CONFIG_PCI_MSI */
7023edd16368SStephen M. Cameron 	/* if we get here we're going to use the default interrupt mode */
7024a9a3a273SStephen M. Cameron 	h->intr[h->intr_mode] = h->pdev->irq;
7025edd16368SStephen M. Cameron }
7026edd16368SStephen M. Cameron 
70276f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
7028e5c880d1SStephen M. Cameron {
7029e5c880d1SStephen M. Cameron 	int i;
7030e5c880d1SStephen M. Cameron 	u32 subsystem_vendor_id, subsystem_device_id;
7031e5c880d1SStephen M. Cameron 
7032e5c880d1SStephen M. Cameron 	subsystem_vendor_id = pdev->subsystem_vendor;
7033e5c880d1SStephen M. Cameron 	subsystem_device_id = pdev->subsystem_device;
7034e5c880d1SStephen M. Cameron 	*board_id = ((subsystem_device_id << 16) & 0xffff0000) |
7035e5c880d1SStephen M. Cameron 		    subsystem_vendor_id;
7036e5c880d1SStephen M. Cameron 
7037e5c880d1SStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(products); i++)
7038e5c880d1SStephen M. Cameron 		if (*board_id == products[i].board_id)
7039e5c880d1SStephen M. Cameron 			return i;
7040e5c880d1SStephen M. Cameron 
70416798cc0aSStephen M. Cameron 	if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
70426798cc0aSStephen M. Cameron 		subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
70436798cc0aSStephen M. Cameron 		!hpsa_allow_any) {
7044e5c880d1SStephen M. Cameron 		dev_warn(&pdev->dev, "unrecognized board ID: "
7045e5c880d1SStephen M. Cameron 			"0x%08x, ignoring.\n", *board_id);
7046e5c880d1SStephen M. Cameron 			return -ENODEV;
7047e5c880d1SStephen M. Cameron 	}
7048e5c880d1SStephen M. Cameron 	return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
7049e5c880d1SStephen M. Cameron }
7050e5c880d1SStephen M. Cameron 
70516f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
70523a7774ceSStephen M. Cameron 				    unsigned long *memory_bar)
70533a7774ceSStephen M. Cameron {
70543a7774ceSStephen M. Cameron 	int i;
70553a7774ceSStephen M. Cameron 
70563a7774ceSStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
705712d2cd47SStephen M. Cameron 		if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
70583a7774ceSStephen M. Cameron 			/* addressing mode bits already removed */
705912d2cd47SStephen M. Cameron 			*memory_bar = pci_resource_start(pdev, i);
706012d2cd47SStephen M. Cameron 			dev_dbg(&pdev->dev, "memory BAR = %lx\n",
70613a7774ceSStephen M. Cameron 				*memory_bar);
70623a7774ceSStephen M. Cameron 			return 0;
70633a7774ceSStephen M. Cameron 		}
706412d2cd47SStephen M. Cameron 	dev_warn(&pdev->dev, "no memory BAR found\n");
70653a7774ceSStephen M. Cameron 	return -ENODEV;
70663a7774ceSStephen M. Cameron }
70673a7774ceSStephen M. Cameron 
70686f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
70696f039790SGreg Kroah-Hartman 				     int wait_for_ready)
70702c4c8c8bSStephen M. Cameron {
7071fe5389c8SStephen M. Cameron 	int i, iterations;
70722c4c8c8bSStephen M. Cameron 	u32 scratchpad;
7073fe5389c8SStephen M. Cameron 	if (wait_for_ready)
7074fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_READY_ITERATIONS;
7075fe5389c8SStephen M. Cameron 	else
7076fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
70772c4c8c8bSStephen M. Cameron 
7078fe5389c8SStephen M. Cameron 	for (i = 0; i < iterations; i++) {
7079fe5389c8SStephen M. Cameron 		scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
7080fe5389c8SStephen M. Cameron 		if (wait_for_ready) {
70812c4c8c8bSStephen M. Cameron 			if (scratchpad == HPSA_FIRMWARE_READY)
70822c4c8c8bSStephen M. Cameron 				return 0;
7083fe5389c8SStephen M. Cameron 		} else {
7084fe5389c8SStephen M. Cameron 			if (scratchpad != HPSA_FIRMWARE_READY)
7085fe5389c8SStephen M. Cameron 				return 0;
7086fe5389c8SStephen M. Cameron 		}
70872c4c8c8bSStephen M. Cameron 		msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
70882c4c8c8bSStephen M. Cameron 	}
7089fe5389c8SStephen M. Cameron 	dev_warn(&pdev->dev, "board not ready, timed out.\n");
70902c4c8c8bSStephen M. Cameron 	return -ENODEV;
70912c4c8c8bSStephen M. Cameron }
70922c4c8c8bSStephen M. Cameron 
70936f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
70946f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
7095a51fd47fSStephen M. Cameron 			       u64 *cfg_offset)
7096a51fd47fSStephen M. Cameron {
7097a51fd47fSStephen M. Cameron 	*cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
7098a51fd47fSStephen M. Cameron 	*cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
7099a51fd47fSStephen M. Cameron 	*cfg_base_addr &= (u32) 0x0000ffff;
7100a51fd47fSStephen M. Cameron 	*cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
7101a51fd47fSStephen M. Cameron 	if (*cfg_base_addr_index == -1) {
7102a51fd47fSStephen M. Cameron 		dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
7103a51fd47fSStephen M. Cameron 		return -ENODEV;
7104a51fd47fSStephen M. Cameron 	}
7105a51fd47fSStephen M. Cameron 	return 0;
7106a51fd47fSStephen M. Cameron }
7107a51fd47fSStephen M. Cameron 
7108195f2c65SRobert Elliott static void hpsa_free_cfgtables(struct ctlr_info *h)
7109195f2c65SRobert Elliott {
7110105a3dbcSRobert Elliott 	if (h->transtable) {
7111195f2c65SRobert Elliott 		iounmap(h->transtable);
7112105a3dbcSRobert Elliott 		h->transtable = NULL;
7113105a3dbcSRobert Elliott 	}
7114105a3dbcSRobert Elliott 	if (h->cfgtable) {
7115195f2c65SRobert Elliott 		iounmap(h->cfgtable);
7116105a3dbcSRobert Elliott 		h->cfgtable = NULL;
7117105a3dbcSRobert Elliott 	}
7118195f2c65SRobert Elliott }
7119195f2c65SRobert Elliott 
7120195f2c65SRobert Elliott /* Find and map CISS config table and transfer table
7121195f2c65SRobert Elliott + * several items must be unmapped (freed) later
7122195f2c65SRobert Elliott + * */
71236f039790SGreg Kroah-Hartman static int hpsa_find_cfgtables(struct ctlr_info *h)
7124edd16368SStephen M. Cameron {
712501a02ffcSStephen M. Cameron 	u64 cfg_offset;
712601a02ffcSStephen M. Cameron 	u32 cfg_base_addr;
712701a02ffcSStephen M. Cameron 	u64 cfg_base_addr_index;
7128303932fdSDon Brace 	u32 trans_offset;
7129a51fd47fSStephen M. Cameron 	int rc;
713077c4495cSStephen M. Cameron 
7131a51fd47fSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
7132a51fd47fSStephen M. Cameron 		&cfg_base_addr_index, &cfg_offset);
7133a51fd47fSStephen M. Cameron 	if (rc)
7134a51fd47fSStephen M. Cameron 		return rc;
713577c4495cSStephen M. Cameron 	h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
7136a51fd47fSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
7137cd3c81c4SRobert Elliott 	if (!h->cfgtable) {
7138cd3c81c4SRobert Elliott 		dev_err(&h->pdev->dev, "Failed mapping cfgtable\n");
713977c4495cSStephen M. Cameron 		return -ENOMEM;
7140cd3c81c4SRobert Elliott 	}
7141580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(h->cfgtable);
7142580ada3cSStephen M. Cameron 	if (rc)
7143580ada3cSStephen M. Cameron 		return rc;
714477c4495cSStephen M. Cameron 	/* Find performant mode table. */
7145a51fd47fSStephen M. Cameron 	trans_offset = readl(&h->cfgtable->TransMethodOffset);
714677c4495cSStephen M. Cameron 	h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
714777c4495cSStephen M. Cameron 				cfg_base_addr_index)+cfg_offset+trans_offset,
714877c4495cSStephen M. Cameron 				sizeof(*h->transtable));
7149195f2c65SRobert Elliott 	if (!h->transtable) {
7150195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "Failed mapping transfer table\n");
7151195f2c65SRobert Elliott 		hpsa_free_cfgtables(h);
715277c4495cSStephen M. Cameron 		return -ENOMEM;
7153195f2c65SRobert Elliott 	}
715477c4495cSStephen M. Cameron 	return 0;
715577c4495cSStephen M. Cameron }
715677c4495cSStephen M. Cameron 
71576f039790SGreg Kroah-Hartman static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
7158cba3d38bSStephen M. Cameron {
715941ce4c35SStephen Cameron #define MIN_MAX_COMMANDS 16
716041ce4c35SStephen Cameron 	BUILD_BUG_ON(MIN_MAX_COMMANDS <= HPSA_NRESERVED_CMDS);
716141ce4c35SStephen Cameron 
716241ce4c35SStephen Cameron 	h->max_commands = readl(&h->cfgtable->MaxPerformantModeCommands);
716372ceeaecSStephen M. Cameron 
716472ceeaecSStephen M. Cameron 	/* Limit commands in memory limited kdump scenario. */
716572ceeaecSStephen M. Cameron 	if (reset_devices && h->max_commands > 32)
716672ceeaecSStephen M. Cameron 		h->max_commands = 32;
716772ceeaecSStephen M. Cameron 
716841ce4c35SStephen Cameron 	if (h->max_commands < MIN_MAX_COMMANDS) {
716941ce4c35SStephen Cameron 		dev_warn(&h->pdev->dev,
717041ce4c35SStephen Cameron 			"Controller reports max supported commands of %d Using %d instead. Ensure that firmware is up to date.\n",
717141ce4c35SStephen Cameron 			h->max_commands,
717241ce4c35SStephen Cameron 			MIN_MAX_COMMANDS);
717341ce4c35SStephen Cameron 		h->max_commands = MIN_MAX_COMMANDS;
7174cba3d38bSStephen M. Cameron 	}
7175cba3d38bSStephen M. Cameron }
7176cba3d38bSStephen M. Cameron 
7177c7ee65b3SWebb Scales /* If the controller reports that the total max sg entries is greater than 512,
7178c7ee65b3SWebb Scales  * then we know that chained SG blocks work.  (Original smart arrays did not
7179c7ee65b3SWebb Scales  * support chained SG blocks and would return zero for max sg entries.)
7180c7ee65b3SWebb Scales  */
7181c7ee65b3SWebb Scales static int hpsa_supports_chained_sg_blocks(struct ctlr_info *h)
7182c7ee65b3SWebb Scales {
7183c7ee65b3SWebb Scales 	return h->maxsgentries > 512;
7184c7ee65b3SWebb Scales }
7185c7ee65b3SWebb Scales 
7186b93d7536SStephen M. Cameron /* Interrogate the hardware for some limits:
7187b93d7536SStephen M. Cameron  * max commands, max SG elements without chaining, and with chaining,
7188b93d7536SStephen M. Cameron  * SG chain block size, etc.
7189b93d7536SStephen M. Cameron  */
71906f039790SGreg Kroah-Hartman static void hpsa_find_board_params(struct ctlr_info *h)
7191b93d7536SStephen M. Cameron {
7192cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
719345fcb86eSStephen Cameron 	h->nr_cmds = h->max_commands;
7194b93d7536SStephen M. Cameron 	h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
7195283b4a9bSStephen M. Cameron 	h->fw_support = readl(&(h->cfgtable->misc_fw_support));
7196c7ee65b3SWebb Scales 	if (hpsa_supports_chained_sg_blocks(h)) {
7197c7ee65b3SWebb Scales 		/* Limit in-command s/g elements to 32 save dma'able memory. */
7198b93d7536SStephen M. Cameron 		h->max_cmd_sg_entries = 32;
71991a63ea6fSWebb Scales 		h->chainsize = h->maxsgentries - h->max_cmd_sg_entries;
7200b93d7536SStephen M. Cameron 		h->maxsgentries--; /* save one for chain pointer */
7201b93d7536SStephen M. Cameron 	} else {
7202c7ee65b3SWebb Scales 		/*
7203c7ee65b3SWebb Scales 		 * Original smart arrays supported at most 31 s/g entries
7204c7ee65b3SWebb Scales 		 * embedded inline in the command (trying to use more
7205c7ee65b3SWebb Scales 		 * would lock up the controller)
7206c7ee65b3SWebb Scales 		 */
7207c7ee65b3SWebb Scales 		h->max_cmd_sg_entries = 31;
72081a63ea6fSWebb Scales 		h->maxsgentries = 31; /* default to traditional values */
7209c7ee65b3SWebb Scales 		h->chainsize = 0;
7210b93d7536SStephen M. Cameron 	}
721175167d2cSStephen M. Cameron 
721275167d2cSStephen M. Cameron 	/* Find out what task management functions are supported and cache */
721375167d2cSStephen M. Cameron 	h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
72140e7a7fceSScott Teel 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
72150e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
72160e7a7fceSScott Teel 	if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
72170e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
72188be986ccSStephen Cameron 	if (!(HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags))
72198be986ccSStephen Cameron 		dev_warn(&h->pdev->dev, "HP SSD Smart Path aborts not supported\n");
7220b93d7536SStephen M. Cameron }
7221b93d7536SStephen M. Cameron 
722276c46e49SStephen M. Cameron static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
722376c46e49SStephen M. Cameron {
72240fc9fd40SAkinobu Mita 	if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
7225050f7147SStephen Cameron 		dev_err(&h->pdev->dev, "not a valid CISS config table\n");
722676c46e49SStephen M. Cameron 		return false;
722776c46e49SStephen M. Cameron 	}
722876c46e49SStephen M. Cameron 	return true;
722976c46e49SStephen M. Cameron }
723076c46e49SStephen M. Cameron 
723197a5e98cSStephen M. Cameron static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
7232f7c39101SStephen M. Cameron {
723397a5e98cSStephen M. Cameron 	u32 driver_support;
7234f7c39101SStephen M. Cameron 
723597a5e98cSStephen M. Cameron 	driver_support = readl(&(h->cfgtable->driver_support));
72360b9e7b74SArnd Bergmann 	/* Need to enable prefetch in the SCSI core for 6400 in x86 */
72370b9e7b74SArnd Bergmann #ifdef CONFIG_X86
723897a5e98cSStephen M. Cameron 	driver_support |= ENABLE_SCSI_PREFETCH;
7239f7c39101SStephen M. Cameron #endif
724028e13446SStephen M. Cameron 	driver_support |= ENABLE_UNIT_ATTN;
724128e13446SStephen M. Cameron 	writel(driver_support, &(h->cfgtable->driver_support));
7242f7c39101SStephen M. Cameron }
7243f7c39101SStephen M. Cameron 
72443d0eab67SStephen M. Cameron /* Disable DMA prefetch for the P600.  Otherwise an ASIC bug may result
72453d0eab67SStephen M. Cameron  * in a prefetch beyond physical memory.
72463d0eab67SStephen M. Cameron  */
72473d0eab67SStephen M. Cameron static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
72483d0eab67SStephen M. Cameron {
72493d0eab67SStephen M. Cameron 	u32 dma_prefetch;
72503d0eab67SStephen M. Cameron 
72513d0eab67SStephen M. Cameron 	if (h->board_id != 0x3225103C)
72523d0eab67SStephen M. Cameron 		return;
72533d0eab67SStephen M. Cameron 	dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
72543d0eab67SStephen M. Cameron 	dma_prefetch |= 0x8000;
72553d0eab67SStephen M. Cameron 	writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
72563d0eab67SStephen M. Cameron }
72573d0eab67SStephen M. Cameron 
7258c706a795SRobert Elliott static int hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
725976438d08SStephen M. Cameron {
726076438d08SStephen M. Cameron 	int i;
726176438d08SStephen M. Cameron 	u32 doorbell_value;
726276438d08SStephen M. Cameron 	unsigned long flags;
726376438d08SStephen M. Cameron 	/* wait until the clear_event_notify bit 6 is cleared by controller. */
7264007e7aa9SRobert Elliott 	for (i = 0; i < MAX_CLEAR_EVENT_WAIT; i++) {
726576438d08SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
726676438d08SStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
726776438d08SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
726876438d08SStephen M. Cameron 		if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
7269c706a795SRobert Elliott 			goto done;
727076438d08SStephen M. Cameron 		/* delay and try again */
7271007e7aa9SRobert Elliott 		msleep(CLEAR_EVENT_WAIT_INTERVAL);
727276438d08SStephen M. Cameron 	}
7273c706a795SRobert Elliott 	return -ENODEV;
7274c706a795SRobert Elliott done:
7275c706a795SRobert Elliott 	return 0;
727676438d08SStephen M. Cameron }
727776438d08SStephen M. Cameron 
7278c706a795SRobert Elliott static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
7279eb6b2ae9SStephen M. Cameron {
7280eb6b2ae9SStephen M. Cameron 	int i;
72816eaf46fdSStephen M. Cameron 	u32 doorbell_value;
72826eaf46fdSStephen M. Cameron 	unsigned long flags;
7283eb6b2ae9SStephen M. Cameron 
7284eb6b2ae9SStephen M. Cameron 	/* under certain very rare conditions, this can take awhile.
7285eb6b2ae9SStephen M. Cameron 	 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
7286eb6b2ae9SStephen M. Cameron 	 * as we enter this code.)
7287eb6b2ae9SStephen M. Cameron 	 */
7288007e7aa9SRobert Elliott 	for (i = 0; i < MAX_MODE_CHANGE_WAIT; i++) {
728925163bd5SWebb Scales 		if (h->remove_in_progress)
729025163bd5SWebb Scales 			goto done;
72916eaf46fdSStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
72926eaf46fdSStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
72936eaf46fdSStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
7294382be668SDan Carpenter 		if (!(doorbell_value & CFGTBL_ChangeReq))
7295c706a795SRobert Elliott 			goto done;
7296eb6b2ae9SStephen M. Cameron 		/* delay and try again */
7297007e7aa9SRobert Elliott 		msleep(MODE_CHANGE_WAIT_INTERVAL);
7298eb6b2ae9SStephen M. Cameron 	}
7299c706a795SRobert Elliott 	return -ENODEV;
7300c706a795SRobert Elliott done:
7301c706a795SRobert Elliott 	return 0;
73023f4336f3SStephen M. Cameron }
73033f4336f3SStephen M. Cameron 
7304c706a795SRobert Elliott /* return -ENODEV or other reason on error, 0 on success */
73056f039790SGreg Kroah-Hartman static int hpsa_enter_simple_mode(struct ctlr_info *h)
73063f4336f3SStephen M. Cameron {
73073f4336f3SStephen M. Cameron 	u32 trans_support;
73083f4336f3SStephen M. Cameron 
73093f4336f3SStephen M. Cameron 	trans_support = readl(&(h->cfgtable->TransportSupport));
73103f4336f3SStephen M. Cameron 	if (!(trans_support & SIMPLE_MODE))
73113f4336f3SStephen M. Cameron 		return -ENOTSUPP;
73123f4336f3SStephen M. Cameron 
73133f4336f3SStephen M. Cameron 	h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
7314283b4a9bSStephen M. Cameron 
73153f4336f3SStephen M. Cameron 	/* Update the field, and then ring the doorbell */
73163f4336f3SStephen M. Cameron 	writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
7317b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
73183f4336f3SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
7319c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h))
7320c706a795SRobert Elliott 		goto error;
7321eb6b2ae9SStephen M. Cameron 	print_cfg_table(&h->pdev->dev, h->cfgtable);
7322283b4a9bSStephen M. Cameron 	if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
7323283b4a9bSStephen M. Cameron 		goto error;
7324960a30e7SStephen M. Cameron 	h->transMethod = CFGTBL_Trans_Simple;
7325eb6b2ae9SStephen M. Cameron 	return 0;
7326283b4a9bSStephen M. Cameron error:
7327050f7147SStephen Cameron 	dev_err(&h->pdev->dev, "failed to enter simple mode\n");
7328283b4a9bSStephen M. Cameron 	return -ENODEV;
7329eb6b2ae9SStephen M. Cameron }
7330eb6b2ae9SStephen M. Cameron 
7331195f2c65SRobert Elliott /* free items allocated or mapped by hpsa_pci_init */
7332195f2c65SRobert Elliott static void hpsa_free_pci_init(struct ctlr_info *h)
7333195f2c65SRobert Elliott {
7334195f2c65SRobert Elliott 	hpsa_free_cfgtables(h);			/* pci_init 4 */
7335195f2c65SRobert Elliott 	iounmap(h->vaddr);			/* pci_init 3 */
7336105a3dbcSRobert Elliott 	h->vaddr = NULL;
7337195f2c65SRobert Elliott 	hpsa_disable_interrupt_mode(h);		/* pci_init 2 */
7338943a7021SRobert Elliott 	/*
7339943a7021SRobert Elliott 	 * call pci_disable_device before pci_release_regions per
7340943a7021SRobert Elliott 	 * Documentation/PCI/pci.txt
7341943a7021SRobert Elliott 	 */
7342195f2c65SRobert Elliott 	pci_disable_device(h->pdev);		/* pci_init 1 */
7343943a7021SRobert Elliott 	pci_release_regions(h->pdev);		/* pci_init 2 */
7344195f2c65SRobert Elliott }
7345195f2c65SRobert Elliott 
7346195f2c65SRobert Elliott /* several items must be freed later */
73476f039790SGreg Kroah-Hartman static int hpsa_pci_init(struct ctlr_info *h)
734877c4495cSStephen M. Cameron {
7349eb6b2ae9SStephen M. Cameron 	int prod_index, err;
7350edd16368SStephen M. Cameron 
7351e5c880d1SStephen M. Cameron 	prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
7352e5c880d1SStephen M. Cameron 	if (prod_index < 0)
735360f923b9SRobert Elliott 		return prod_index;
7354e5c880d1SStephen M. Cameron 	h->product_name = products[prod_index].product_name;
7355e5c880d1SStephen M. Cameron 	h->access = *(products[prod_index].access);
7356e5c880d1SStephen M. Cameron 
73579b5c48c2SStephen Cameron 	h->needs_abort_tags_swizzled =
73589b5c48c2SStephen Cameron 		ctlr_needs_abort_tags_swizzled(h->board_id);
73599b5c48c2SStephen Cameron 
7360e5a44df8SMatthew Garrett 	pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
7361e5a44df8SMatthew Garrett 			       PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
7362e5a44df8SMatthew Garrett 
736355c06c71SStephen M. Cameron 	err = pci_enable_device(h->pdev);
7364edd16368SStephen M. Cameron 	if (err) {
7365195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "failed to enable PCI device\n");
7366943a7021SRobert Elliott 		pci_disable_device(h->pdev);
7367edd16368SStephen M. Cameron 		return err;
7368edd16368SStephen M. Cameron 	}
7369edd16368SStephen M. Cameron 
7370f79cfec6SStephen M. Cameron 	err = pci_request_regions(h->pdev, HPSA);
7371edd16368SStephen M. Cameron 	if (err) {
737255c06c71SStephen M. Cameron 		dev_err(&h->pdev->dev,
7373195f2c65SRobert Elliott 			"failed to obtain PCI resources\n");
7374943a7021SRobert Elliott 		pci_disable_device(h->pdev);
7375943a7021SRobert Elliott 		return err;
7376edd16368SStephen M. Cameron 	}
73774fa604e1SRobert Elliott 
73784fa604e1SRobert Elliott 	pci_set_master(h->pdev);
73794fa604e1SRobert Elliott 
73806b3f4c52SStephen M. Cameron 	hpsa_interrupt_mode(h);
738112d2cd47SStephen M. Cameron 	err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
73823a7774ceSStephen M. Cameron 	if (err)
7383195f2c65SRobert Elliott 		goto clean2;	/* intmode+region, pci */
7384edd16368SStephen M. Cameron 	h->vaddr = remap_pci_mem(h->paddr, 0x250);
7385204892e9SStephen M. Cameron 	if (!h->vaddr) {
7386195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "failed to remap PCI mem\n");
7387204892e9SStephen M. Cameron 		err = -ENOMEM;
7388195f2c65SRobert Elliott 		goto clean2;	/* intmode+region, pci */
7389204892e9SStephen M. Cameron 	}
7390fe5389c8SStephen M. Cameron 	err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
73912c4c8c8bSStephen M. Cameron 	if (err)
7392195f2c65SRobert Elliott 		goto clean3;	/* vaddr, intmode+region, pci */
739377c4495cSStephen M. Cameron 	err = hpsa_find_cfgtables(h);
739477c4495cSStephen M. Cameron 	if (err)
7395195f2c65SRobert Elliott 		goto clean3;	/* vaddr, intmode+region, pci */
7396b93d7536SStephen M. Cameron 	hpsa_find_board_params(h);
7397edd16368SStephen M. Cameron 
739876c46e49SStephen M. Cameron 	if (!hpsa_CISS_signature_present(h)) {
7399edd16368SStephen M. Cameron 		err = -ENODEV;
7400195f2c65SRobert Elliott 		goto clean4;	/* cfgtables, vaddr, intmode+region, pci */
7401edd16368SStephen M. Cameron 	}
740297a5e98cSStephen M. Cameron 	hpsa_set_driver_support_bits(h);
74033d0eab67SStephen M. Cameron 	hpsa_p600_dma_prefetch_quirk(h);
7404eb6b2ae9SStephen M. Cameron 	err = hpsa_enter_simple_mode(h);
7405eb6b2ae9SStephen M. Cameron 	if (err)
7406195f2c65SRobert Elliott 		goto clean4;	/* cfgtables, vaddr, intmode+region, pci */
7407edd16368SStephen M. Cameron 	return 0;
7408edd16368SStephen M. Cameron 
7409195f2c65SRobert Elliott clean4:	/* cfgtables, vaddr, intmode+region, pci */
7410195f2c65SRobert Elliott 	hpsa_free_cfgtables(h);
7411195f2c65SRobert Elliott clean3:	/* vaddr, intmode+region, pci */
7412204892e9SStephen M. Cameron 	iounmap(h->vaddr);
7413105a3dbcSRobert Elliott 	h->vaddr = NULL;
7414195f2c65SRobert Elliott clean2:	/* intmode+region, pci */
7415195f2c65SRobert Elliott 	hpsa_disable_interrupt_mode(h);
7416943a7021SRobert Elliott 	/*
7417943a7021SRobert Elliott 	 * call pci_disable_device before pci_release_regions per
7418943a7021SRobert Elliott 	 * Documentation/PCI/pci.txt
7419943a7021SRobert Elliott 	 */
7420195f2c65SRobert Elliott 	pci_disable_device(h->pdev);
7421943a7021SRobert Elliott 	pci_release_regions(h->pdev);
7422edd16368SStephen M. Cameron 	return err;
7423edd16368SStephen M. Cameron }
7424edd16368SStephen M. Cameron 
74256f039790SGreg Kroah-Hartman static void hpsa_hba_inquiry(struct ctlr_info *h)
7426339b2b14SStephen M. Cameron {
7427339b2b14SStephen M. Cameron 	int rc;
7428339b2b14SStephen M. Cameron 
7429339b2b14SStephen M. Cameron #define HBA_INQUIRY_BYTE_COUNT 64
7430339b2b14SStephen M. Cameron 	h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
7431339b2b14SStephen M. Cameron 	if (!h->hba_inquiry_data)
7432339b2b14SStephen M. Cameron 		return;
7433339b2b14SStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
7434339b2b14SStephen M. Cameron 		h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
7435339b2b14SStephen M. Cameron 	if (rc != 0) {
7436339b2b14SStephen M. Cameron 		kfree(h->hba_inquiry_data);
7437339b2b14SStephen M. Cameron 		h->hba_inquiry_data = NULL;
7438339b2b14SStephen M. Cameron 	}
7439339b2b14SStephen M. Cameron }
7440339b2b14SStephen M. Cameron 
74416b6c1cd7STomas Henzl static int hpsa_init_reset_devices(struct pci_dev *pdev, u32 board_id)
7442edd16368SStephen M. Cameron {
74431df8552aSStephen M. Cameron 	int rc, i;
74443b747298STomas Henzl 	void __iomem *vaddr;
7445edd16368SStephen M. Cameron 
74464c2a8c40SStephen M. Cameron 	if (!reset_devices)
74474c2a8c40SStephen M. Cameron 		return 0;
74484c2a8c40SStephen M. Cameron 
7449132aa220STomas Henzl 	/* kdump kernel is loading, we don't know in which state is
7450132aa220STomas Henzl 	 * the pci interface. The dev->enable_cnt is equal zero
7451132aa220STomas Henzl 	 * so we call enable+disable, wait a while and switch it on.
7452132aa220STomas Henzl 	 */
7453132aa220STomas Henzl 	rc = pci_enable_device(pdev);
7454132aa220STomas Henzl 	if (rc) {
7455132aa220STomas Henzl 		dev_warn(&pdev->dev, "Failed to enable PCI device\n");
7456132aa220STomas Henzl 		return -ENODEV;
7457132aa220STomas Henzl 	}
7458132aa220STomas Henzl 	pci_disable_device(pdev);
7459132aa220STomas Henzl 	msleep(260);			/* a randomly chosen number */
7460132aa220STomas Henzl 	rc = pci_enable_device(pdev);
7461132aa220STomas Henzl 	if (rc) {
7462132aa220STomas Henzl 		dev_warn(&pdev->dev, "failed to enable device.\n");
7463132aa220STomas Henzl 		return -ENODEV;
7464132aa220STomas Henzl 	}
74654fa604e1SRobert Elliott 
7466859c75abSTomas Henzl 	pci_set_master(pdev);
74674fa604e1SRobert Elliott 
74683b747298STomas Henzl 	vaddr = pci_ioremap_bar(pdev, 0);
74693b747298STomas Henzl 	if (vaddr == NULL) {
74703b747298STomas Henzl 		rc = -ENOMEM;
74713b747298STomas Henzl 		goto out_disable;
74723b747298STomas Henzl 	}
74733b747298STomas Henzl 	writel(SA5_INTR_OFF, vaddr + SA5_REPLY_INTR_MASK_OFFSET);
74743b747298STomas Henzl 	iounmap(vaddr);
74753b747298STomas Henzl 
74761df8552aSStephen M. Cameron 	/* Reset the controller with a PCI power-cycle or via doorbell */
74776b6c1cd7STomas Henzl 	rc = hpsa_kdump_hard_reset_controller(pdev, board_id);
7478edd16368SStephen M. Cameron 
74791df8552aSStephen M. Cameron 	/* -ENOTSUPP here means we cannot reset the controller
74801df8552aSStephen M. Cameron 	 * but it's already (and still) up and running in
748118867659SStephen M. Cameron 	 * "performant mode".  Or, it might be 640x, which can't reset
748218867659SStephen M. Cameron 	 * due to concerns about shared bbwc between 6402/6404 pair.
74831df8552aSStephen M. Cameron 	 */
7484adf1b3a3SRobert Elliott 	if (rc)
7485132aa220STomas Henzl 		goto out_disable;
7486edd16368SStephen M. Cameron 
7487edd16368SStephen M. Cameron 	/* Now try to get the controller to respond to a no-op */
74881ba66c9cSRobert Elliott 	dev_info(&pdev->dev, "Waiting for controller to respond to no-op\n");
7489edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
7490edd16368SStephen M. Cameron 		if (hpsa_noop(pdev) == 0)
7491edd16368SStephen M. Cameron 			break;
7492edd16368SStephen M. Cameron 		else
7493edd16368SStephen M. Cameron 			dev_warn(&pdev->dev, "no-op failed%s\n",
7494edd16368SStephen M. Cameron 					(i < 11 ? "; re-trying" : ""));
7495edd16368SStephen M. Cameron 	}
7496132aa220STomas Henzl 
7497132aa220STomas Henzl out_disable:
7498132aa220STomas Henzl 
7499132aa220STomas Henzl 	pci_disable_device(pdev);
7500132aa220STomas Henzl 	return rc;
7501edd16368SStephen M. Cameron }
7502edd16368SStephen M. Cameron 
75031fb7c98aSRobert Elliott static void hpsa_free_cmd_pool(struct ctlr_info *h)
75041fb7c98aSRobert Elliott {
75051fb7c98aSRobert Elliott 	kfree(h->cmd_pool_bits);
7506105a3dbcSRobert Elliott 	h->cmd_pool_bits = NULL;
7507105a3dbcSRobert Elliott 	if (h->cmd_pool) {
75081fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
75091fb7c98aSRobert Elliott 				h->nr_cmds * sizeof(struct CommandList),
75101fb7c98aSRobert Elliott 				h->cmd_pool,
75111fb7c98aSRobert Elliott 				h->cmd_pool_dhandle);
7512105a3dbcSRobert Elliott 		h->cmd_pool = NULL;
7513105a3dbcSRobert Elliott 		h->cmd_pool_dhandle = 0;
7514105a3dbcSRobert Elliott 	}
7515105a3dbcSRobert Elliott 	if (h->errinfo_pool) {
75161fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
75171fb7c98aSRobert Elliott 				h->nr_cmds * sizeof(struct ErrorInfo),
75181fb7c98aSRobert Elliott 				h->errinfo_pool,
75191fb7c98aSRobert Elliott 				h->errinfo_pool_dhandle);
7520105a3dbcSRobert Elliott 		h->errinfo_pool = NULL;
7521105a3dbcSRobert Elliott 		h->errinfo_pool_dhandle = 0;
7522105a3dbcSRobert Elliott 	}
75231fb7c98aSRobert Elliott }
75241fb7c98aSRobert Elliott 
7525d37ffbe4SRobert Elliott static int hpsa_alloc_cmd_pool(struct ctlr_info *h)
75262e9d1b36SStephen M. Cameron {
75272e9d1b36SStephen M. Cameron 	h->cmd_pool_bits = kzalloc(
75282e9d1b36SStephen M. Cameron 		DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
75292e9d1b36SStephen M. Cameron 		sizeof(unsigned long), GFP_KERNEL);
75302e9d1b36SStephen M. Cameron 	h->cmd_pool = pci_alloc_consistent(h->pdev,
75312e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->cmd_pool),
75322e9d1b36SStephen M. Cameron 		    &(h->cmd_pool_dhandle));
75332e9d1b36SStephen M. Cameron 	h->errinfo_pool = pci_alloc_consistent(h->pdev,
75342e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->errinfo_pool),
75352e9d1b36SStephen M. Cameron 		    &(h->errinfo_pool_dhandle));
75362e9d1b36SStephen M. Cameron 	if ((h->cmd_pool_bits == NULL)
75372e9d1b36SStephen M. Cameron 	    || (h->cmd_pool == NULL)
75382e9d1b36SStephen M. Cameron 	    || (h->errinfo_pool == NULL)) {
75392e9d1b36SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory in %s", __func__);
75402c143342SRobert Elliott 		goto clean_up;
75412e9d1b36SStephen M. Cameron 	}
7542360c73bdSStephen Cameron 	hpsa_preinitialize_commands(h);
75432e9d1b36SStephen M. Cameron 	return 0;
75442c143342SRobert Elliott clean_up:
75452c143342SRobert Elliott 	hpsa_free_cmd_pool(h);
75462c143342SRobert Elliott 	return -ENOMEM;
75472e9d1b36SStephen M. Cameron }
75482e9d1b36SStephen M. Cameron 
754941b3cf08SStephen M. Cameron static void hpsa_irq_affinity_hints(struct ctlr_info *h)
755041b3cf08SStephen M. Cameron {
7551ec429952SFabian Frederick 	int i, cpu;
755241b3cf08SStephen M. Cameron 
755341b3cf08SStephen M. Cameron 	cpu = cpumask_first(cpu_online_mask);
755441b3cf08SStephen M. Cameron 	for (i = 0; i < h->msix_vector; i++) {
7555ec429952SFabian Frederick 		irq_set_affinity_hint(h->intr[i], get_cpu_mask(cpu));
755641b3cf08SStephen M. Cameron 		cpu = cpumask_next(cpu, cpu_online_mask);
755741b3cf08SStephen M. Cameron 	}
755841b3cf08SStephen M. Cameron }
755941b3cf08SStephen M. Cameron 
7560ec501a18SRobert Elliott /* clear affinity hints and free MSI-X, MSI, or legacy INTx vectors */
7561ec501a18SRobert Elliott static void hpsa_free_irqs(struct ctlr_info *h)
7562ec501a18SRobert Elliott {
7563ec501a18SRobert Elliott 	int i;
7564ec501a18SRobert Elliott 
7565ec501a18SRobert Elliott 	if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
7566ec501a18SRobert Elliott 		/* Single reply queue, only one irq to free */
7567ec501a18SRobert Elliott 		i = h->intr_mode;
7568ec501a18SRobert Elliott 		irq_set_affinity_hint(h->intr[i], NULL);
7569ec501a18SRobert Elliott 		free_irq(h->intr[i], &h->q[i]);
7570105a3dbcSRobert Elliott 		h->q[i] = 0;
7571ec501a18SRobert Elliott 		return;
7572ec501a18SRobert Elliott 	}
7573ec501a18SRobert Elliott 
7574ec501a18SRobert Elliott 	for (i = 0; i < h->msix_vector; i++) {
7575ec501a18SRobert Elliott 		irq_set_affinity_hint(h->intr[i], NULL);
7576ec501a18SRobert Elliott 		free_irq(h->intr[i], &h->q[i]);
7577105a3dbcSRobert Elliott 		h->q[i] = 0;
7578ec501a18SRobert Elliott 	}
7579a4e17fc1SRobert Elliott 	for (; i < MAX_REPLY_QUEUES; i++)
7580a4e17fc1SRobert Elliott 		h->q[i] = 0;
7581ec501a18SRobert Elliott }
7582ec501a18SRobert Elliott 
75839ee61794SRobert Elliott /* returns 0 on success; cleans up and returns -Enn on error */
75849ee61794SRobert Elliott static int hpsa_request_irqs(struct ctlr_info *h,
75850ae01a32SStephen M. Cameron 	irqreturn_t (*msixhandler)(int, void *),
75860ae01a32SStephen M. Cameron 	irqreturn_t (*intxhandler)(int, void *))
75870ae01a32SStephen M. Cameron {
7588254f796bSMatt Gates 	int rc, i;
75890ae01a32SStephen M. Cameron 
7590254f796bSMatt Gates 	/*
7591254f796bSMatt Gates 	 * initialize h->q[x] = x so that interrupt handlers know which
7592254f796bSMatt Gates 	 * queue to process.
7593254f796bSMatt Gates 	 */
7594254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++)
7595254f796bSMatt Gates 		h->q[i] = (u8) i;
7596254f796bSMatt Gates 
7597eee0f03aSHannes Reinecke 	if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
7598254f796bSMatt Gates 		/* If performant mode and MSI-X, use multiple reply queues */
7599a4e17fc1SRobert Elliott 		for (i = 0; i < h->msix_vector; i++) {
76008b47004aSRobert Elliott 			sprintf(h->intrname[i], "%s-msix%d", h->devname, i);
7601254f796bSMatt Gates 			rc = request_irq(h->intr[i], msixhandler,
76028b47004aSRobert Elliott 					0, h->intrname[i],
7603254f796bSMatt Gates 					&h->q[i]);
7604a4e17fc1SRobert Elliott 			if (rc) {
7605a4e17fc1SRobert Elliott 				int j;
7606a4e17fc1SRobert Elliott 
7607a4e17fc1SRobert Elliott 				dev_err(&h->pdev->dev,
7608a4e17fc1SRobert Elliott 					"failed to get irq %d for %s\n",
7609a4e17fc1SRobert Elliott 				       h->intr[i], h->devname);
7610a4e17fc1SRobert Elliott 				for (j = 0; j < i; j++) {
7611a4e17fc1SRobert Elliott 					free_irq(h->intr[j], &h->q[j]);
7612a4e17fc1SRobert Elliott 					h->q[j] = 0;
7613a4e17fc1SRobert Elliott 				}
7614a4e17fc1SRobert Elliott 				for (; j < MAX_REPLY_QUEUES; j++)
7615a4e17fc1SRobert Elliott 					h->q[j] = 0;
7616a4e17fc1SRobert Elliott 				return rc;
7617a4e17fc1SRobert Elliott 			}
7618a4e17fc1SRobert Elliott 		}
761941b3cf08SStephen M. Cameron 		hpsa_irq_affinity_hints(h);
7620254f796bSMatt Gates 	} else {
7621254f796bSMatt Gates 		/* Use single reply pool */
7622eee0f03aSHannes Reinecke 		if (h->msix_vector > 0 || h->msi_vector) {
76238b47004aSRobert Elliott 			if (h->msix_vector)
76248b47004aSRobert Elliott 				sprintf(h->intrname[h->intr_mode],
76258b47004aSRobert Elliott 					"%s-msix", h->devname);
76268b47004aSRobert Elliott 			else
76278b47004aSRobert Elliott 				sprintf(h->intrname[h->intr_mode],
76288b47004aSRobert Elliott 					"%s-msi", h->devname);
7629254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
76308b47004aSRobert Elliott 				msixhandler, 0,
76318b47004aSRobert Elliott 				h->intrname[h->intr_mode],
7632254f796bSMatt Gates 				&h->q[h->intr_mode]);
7633254f796bSMatt Gates 		} else {
76348b47004aSRobert Elliott 			sprintf(h->intrname[h->intr_mode],
76358b47004aSRobert Elliott 				"%s-intx", h->devname);
7636254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
76378b47004aSRobert Elliott 				intxhandler, IRQF_SHARED,
76388b47004aSRobert Elliott 				h->intrname[h->intr_mode],
7639254f796bSMatt Gates 				&h->q[h->intr_mode]);
7640254f796bSMatt Gates 		}
7641105a3dbcSRobert Elliott 		irq_set_affinity_hint(h->intr[h->intr_mode], NULL);
7642254f796bSMatt Gates 	}
76430ae01a32SStephen M. Cameron 	if (rc) {
7644195f2c65SRobert Elliott 		dev_err(&h->pdev->dev, "failed to get irq %d for %s\n",
76450ae01a32SStephen M. Cameron 		       h->intr[h->intr_mode], h->devname);
7646195f2c65SRobert Elliott 		hpsa_free_irqs(h);
76470ae01a32SStephen M. Cameron 		return -ENODEV;
76480ae01a32SStephen M. Cameron 	}
76490ae01a32SStephen M. Cameron 	return 0;
76500ae01a32SStephen M. Cameron }
76510ae01a32SStephen M. Cameron 
76526f039790SGreg Kroah-Hartman static int hpsa_kdump_soft_reset(struct ctlr_info *h)
765364670ac8SStephen M. Cameron {
765439c53f55SRobert Elliott 	int rc;
7655bf43caf3SRobert Elliott 	hpsa_send_host_reset(h, RAID_CTLR_LUNID, HPSA_RESET_TYPE_CONTROLLER);
765664670ac8SStephen M. Cameron 
765764670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
765839c53f55SRobert Elliott 	rc = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY);
765939c53f55SRobert Elliott 	if (rc) {
766064670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
766139c53f55SRobert Elliott 		return rc;
766264670ac8SStephen M. Cameron 	}
766364670ac8SStephen M. Cameron 
766464670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
766539c53f55SRobert Elliott 	rc = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
766639c53f55SRobert Elliott 	if (rc) {
766764670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Board failed to become ready "
766864670ac8SStephen M. Cameron 			"after soft reset.\n");
766939c53f55SRobert Elliott 		return rc;
767064670ac8SStephen M. Cameron 	}
767164670ac8SStephen M. Cameron 
767264670ac8SStephen M. Cameron 	return 0;
767364670ac8SStephen M. Cameron }
767464670ac8SStephen M. Cameron 
7675072b0518SStephen M. Cameron static void hpsa_free_reply_queues(struct ctlr_info *h)
7676072b0518SStephen M. Cameron {
7677072b0518SStephen M. Cameron 	int i;
7678072b0518SStephen M. Cameron 
7679072b0518SStephen M. Cameron 	for (i = 0; i < h->nreply_queues; i++) {
7680072b0518SStephen M. Cameron 		if (!h->reply_queue[i].head)
7681072b0518SStephen M. Cameron 			continue;
76821fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
76831fb7c98aSRobert Elliott 					h->reply_queue_size,
76841fb7c98aSRobert Elliott 					h->reply_queue[i].head,
76851fb7c98aSRobert Elliott 					h->reply_queue[i].busaddr);
7686072b0518SStephen M. Cameron 		h->reply_queue[i].head = NULL;
7687072b0518SStephen M. Cameron 		h->reply_queue[i].busaddr = 0;
7688072b0518SStephen M. Cameron 	}
7689105a3dbcSRobert Elliott 	h->reply_queue_size = 0;
7690072b0518SStephen M. Cameron }
7691072b0518SStephen M. Cameron 
76920097f0f4SStephen M. Cameron static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
76930097f0f4SStephen M. Cameron {
7694105a3dbcSRobert Elliott 	hpsa_free_performant_mode(h);		/* init_one 7 */
7695105a3dbcSRobert Elliott 	hpsa_free_sg_chain_blocks(h);		/* init_one 6 */
7696105a3dbcSRobert Elliott 	hpsa_free_cmd_pool(h);			/* init_one 5 */
7697105a3dbcSRobert Elliott 	hpsa_free_irqs(h);			/* init_one 4 */
76982946e82bSRobert Elliott 	scsi_host_put(h->scsi_host);		/* init_one 3 */
76992946e82bSRobert Elliott 	h->scsi_host = NULL;			/* init_one 3 */
77002946e82bSRobert Elliott 	hpsa_free_pci_init(h);			/* init_one 2_5 */
77019ecd953aSRobert Elliott 	free_percpu(h->lockup_detected);	/* init_one 2 */
77029ecd953aSRobert Elliott 	h->lockup_detected = NULL;		/* init_one 2 */
77039ecd953aSRobert Elliott 	if (h->resubmit_wq) {
77049ecd953aSRobert Elliott 		destroy_workqueue(h->resubmit_wq);	/* init_one 1 */
77059ecd953aSRobert Elliott 		h->resubmit_wq = NULL;
77069ecd953aSRobert Elliott 	}
77079ecd953aSRobert Elliott 	if (h->rescan_ctlr_wq) {
77089ecd953aSRobert Elliott 		destroy_workqueue(h->rescan_ctlr_wq);
77099ecd953aSRobert Elliott 		h->rescan_ctlr_wq = NULL;
77109ecd953aSRobert Elliott 	}
7711105a3dbcSRobert Elliott 	kfree(h);				/* init_one 1 */
771264670ac8SStephen M. Cameron }
771364670ac8SStephen M. Cameron 
7714a0c12413SStephen M. Cameron /* Called when controller lockup detected. */
7715f2405db8SDon Brace static void fail_all_outstanding_cmds(struct ctlr_info *h)
7716a0c12413SStephen M. Cameron {
7717281a7fd0SWebb Scales 	int i, refcount;
7718281a7fd0SWebb Scales 	struct CommandList *c;
771925163bd5SWebb Scales 	int failcount = 0;
7720a0c12413SStephen M. Cameron 
7721080ef1ccSDon Brace 	flush_workqueue(h->resubmit_wq); /* ensure all cmds are fully built */
7722f2405db8SDon Brace 	for (i = 0; i < h->nr_cmds; i++) {
7723f2405db8SDon Brace 		c = h->cmd_pool + i;
7724281a7fd0SWebb Scales 		refcount = atomic_inc_return(&c->refcount);
7725281a7fd0SWebb Scales 		if (refcount > 1) {
772625163bd5SWebb Scales 			c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
77275a3d16f5SStephen M. Cameron 			finish_cmd(c);
7728433b5f4dSStephen Cameron 			atomic_dec(&h->commands_outstanding);
772925163bd5SWebb Scales 			failcount++;
7730a0c12413SStephen M. Cameron 		}
7731281a7fd0SWebb Scales 		cmd_free(h, c);
7732281a7fd0SWebb Scales 	}
773325163bd5SWebb Scales 	dev_warn(&h->pdev->dev,
773425163bd5SWebb Scales 		"failed %d commands in fail_all\n", failcount);
7735a0c12413SStephen M. Cameron }
7736a0c12413SStephen M. Cameron 
7737094963daSStephen M. Cameron static void set_lockup_detected_for_all_cpus(struct ctlr_info *h, u32 value)
7738094963daSStephen M. Cameron {
7739c8ed0010SRusty Russell 	int cpu;
7740094963daSStephen M. Cameron 
7741c8ed0010SRusty Russell 	for_each_online_cpu(cpu) {
7742094963daSStephen M. Cameron 		u32 *lockup_detected;
7743094963daSStephen M. Cameron 		lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
7744094963daSStephen M. Cameron 		*lockup_detected = value;
7745094963daSStephen M. Cameron 	}
7746094963daSStephen M. Cameron 	wmb(); /* be sure the per-cpu variables are out to memory */
7747094963daSStephen M. Cameron }
7748094963daSStephen M. Cameron 
7749a0c12413SStephen M. Cameron static void controller_lockup_detected(struct ctlr_info *h)
7750a0c12413SStephen M. Cameron {
7751a0c12413SStephen M. Cameron 	unsigned long flags;
7752094963daSStephen M. Cameron 	u32 lockup_detected;
7753a0c12413SStephen M. Cameron 
7754a0c12413SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
7755a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
7756094963daSStephen M. Cameron 	lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
7757094963daSStephen M. Cameron 	if (!lockup_detected) {
7758094963daSStephen M. Cameron 		/* no heartbeat, but controller gave us a zero. */
7759094963daSStephen M. Cameron 		dev_warn(&h->pdev->dev,
776025163bd5SWebb Scales 			"lockup detected after %d but scratchpad register is zero\n",
776125163bd5SWebb Scales 			h->heartbeat_sample_interval / HZ);
7762094963daSStephen M. Cameron 		lockup_detected = 0xffffffff;
7763094963daSStephen M. Cameron 	}
7764094963daSStephen M. Cameron 	set_lockup_detected_for_all_cpus(h, lockup_detected);
7765a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
776625163bd5SWebb Scales 	dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x after %d\n",
776725163bd5SWebb Scales 			lockup_detected, h->heartbeat_sample_interval / HZ);
7768a0c12413SStephen M. Cameron 	pci_disable_device(h->pdev);
7769f2405db8SDon Brace 	fail_all_outstanding_cmds(h);
7770a0c12413SStephen M. Cameron }
7771a0c12413SStephen M. Cameron 
777225163bd5SWebb Scales static int detect_controller_lockup(struct ctlr_info *h)
7773a0c12413SStephen M. Cameron {
7774a0c12413SStephen M. Cameron 	u64 now;
7775a0c12413SStephen M. Cameron 	u32 heartbeat;
7776a0c12413SStephen M. Cameron 	unsigned long flags;
7777a0c12413SStephen M. Cameron 
7778a0c12413SStephen M. Cameron 	now = get_jiffies_64();
7779a0c12413SStephen M. Cameron 	/* If we've received an interrupt recently, we're ok. */
7780a0c12413SStephen M. Cameron 	if (time_after64(h->last_intr_timestamp +
7781e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
778225163bd5SWebb Scales 		return false;
7783a0c12413SStephen M. Cameron 
7784a0c12413SStephen M. Cameron 	/*
7785a0c12413SStephen M. Cameron 	 * If we've already checked the heartbeat recently, we're ok.
7786a0c12413SStephen M. Cameron 	 * This could happen if someone sends us a signal. We
7787a0c12413SStephen M. Cameron 	 * otherwise don't care about signals in this thread.
7788a0c12413SStephen M. Cameron 	 */
7789a0c12413SStephen M. Cameron 	if (time_after64(h->last_heartbeat_timestamp +
7790e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
779125163bd5SWebb Scales 		return false;
7792a0c12413SStephen M. Cameron 
7793a0c12413SStephen M. Cameron 	/* If heartbeat has not changed since we last looked, we're not ok. */
7794a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
7795a0c12413SStephen M. Cameron 	heartbeat = readl(&h->cfgtable->HeartBeat);
7796a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
7797a0c12413SStephen M. Cameron 	if (h->last_heartbeat == heartbeat) {
7798a0c12413SStephen M. Cameron 		controller_lockup_detected(h);
779925163bd5SWebb Scales 		return true;
7800a0c12413SStephen M. Cameron 	}
7801a0c12413SStephen M. Cameron 
7802a0c12413SStephen M. Cameron 	/* We're ok. */
7803a0c12413SStephen M. Cameron 	h->last_heartbeat = heartbeat;
7804a0c12413SStephen M. Cameron 	h->last_heartbeat_timestamp = now;
780525163bd5SWebb Scales 	return false;
7806a0c12413SStephen M. Cameron }
7807a0c12413SStephen M. Cameron 
78089846590eSStephen M. Cameron static void hpsa_ack_ctlr_events(struct ctlr_info *h)
780976438d08SStephen M. Cameron {
781076438d08SStephen M. Cameron 	int i;
781176438d08SStephen M. Cameron 	char *event_type;
781276438d08SStephen M. Cameron 
7813e4aa3e6aSStephen Cameron 	if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
7814e4aa3e6aSStephen Cameron 		return;
7815e4aa3e6aSStephen Cameron 
781676438d08SStephen M. Cameron 	/* Ask the controller to clear the events we're handling. */
78171f7cee8cSStephen M. Cameron 	if ((h->transMethod & (CFGTBL_Trans_io_accel1
78181f7cee8cSStephen M. Cameron 			| CFGTBL_Trans_io_accel2)) &&
781976438d08SStephen M. Cameron 		(h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
782076438d08SStephen M. Cameron 		 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
782176438d08SStephen M. Cameron 
782276438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
782376438d08SStephen M. Cameron 			event_type = "state change";
782476438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
782576438d08SStephen M. Cameron 			event_type = "configuration change";
782676438d08SStephen M. Cameron 		/* Stop sending new RAID offload reqs via the IO accelerator */
782776438d08SStephen M. Cameron 		scsi_block_requests(h->scsi_host);
782876438d08SStephen M. Cameron 		for (i = 0; i < h->ndevices; i++)
782976438d08SStephen M. Cameron 			h->dev[i]->offload_enabled = 0;
783023100dd9SStephen M. Cameron 		hpsa_drain_accel_commands(h);
783176438d08SStephen M. Cameron 		/* Set 'accelerator path config change' bit */
783276438d08SStephen M. Cameron 		dev_warn(&h->pdev->dev,
783376438d08SStephen M. Cameron 			"Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
783476438d08SStephen M. Cameron 			h->events, event_type);
783576438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
783676438d08SStephen M. Cameron 		/* Set the "clear event notify field update" bit 6 */
783776438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
783876438d08SStephen M. Cameron 		/* Wait until ctlr clears 'clear event notify field', bit 6 */
783976438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
784076438d08SStephen M. Cameron 		scsi_unblock_requests(h->scsi_host);
784176438d08SStephen M. Cameron 	} else {
784276438d08SStephen M. Cameron 		/* Acknowledge controller notification events. */
784376438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
784476438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
784576438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
784676438d08SStephen M. Cameron #if 0
784776438d08SStephen M. Cameron 		writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
784876438d08SStephen M. Cameron 		hpsa_wait_for_mode_change_ack(h);
784976438d08SStephen M. Cameron #endif
785076438d08SStephen M. Cameron 	}
78519846590eSStephen M. Cameron 	return;
785276438d08SStephen M. Cameron }
785376438d08SStephen M. Cameron 
785476438d08SStephen M. Cameron /* Check a register on the controller to see if there are configuration
785576438d08SStephen M. Cameron  * changes (added/changed/removed logical drives, etc.) which mean that
7856e863d68eSScott Teel  * we should rescan the controller for devices.
7857e863d68eSScott Teel  * Also check flag for driver-initiated rescan.
785876438d08SStephen M. Cameron  */
78599846590eSStephen M. Cameron static int hpsa_ctlr_needs_rescan(struct ctlr_info *h)
786076438d08SStephen M. Cameron {
786176438d08SStephen M. Cameron 	if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
78629846590eSStephen M. Cameron 		return 0;
786376438d08SStephen M. Cameron 
786476438d08SStephen M. Cameron 	h->events = readl(&(h->cfgtable->event_notify));
78659846590eSStephen M. Cameron 	return h->events & RESCAN_REQUIRED_EVENT_BITS;
78669846590eSStephen M. Cameron }
786776438d08SStephen M. Cameron 
786876438d08SStephen M. Cameron /*
78699846590eSStephen M. Cameron  * Check if any of the offline devices have become ready
787076438d08SStephen M. Cameron  */
78719846590eSStephen M. Cameron static int hpsa_offline_devices_ready(struct ctlr_info *h)
78729846590eSStephen M. Cameron {
78739846590eSStephen M. Cameron 	unsigned long flags;
78749846590eSStephen M. Cameron 	struct offline_device_entry *d;
78759846590eSStephen M. Cameron 	struct list_head *this, *tmp;
78769846590eSStephen M. Cameron 
78779846590eSStephen M. Cameron 	spin_lock_irqsave(&h->offline_device_lock, flags);
78789846590eSStephen M. Cameron 	list_for_each_safe(this, tmp, &h->offline_device_list) {
78799846590eSStephen M. Cameron 		d = list_entry(this, struct offline_device_entry,
78809846590eSStephen M. Cameron 				offline_list);
78819846590eSStephen M. Cameron 		spin_unlock_irqrestore(&h->offline_device_lock, flags);
7882d1fea47cSStephen M. Cameron 		if (!hpsa_volume_offline(h, d->scsi3addr)) {
7883d1fea47cSStephen M. Cameron 			spin_lock_irqsave(&h->offline_device_lock, flags);
7884d1fea47cSStephen M. Cameron 			list_del(&d->offline_list);
7885d1fea47cSStephen M. Cameron 			spin_unlock_irqrestore(&h->offline_device_lock, flags);
78869846590eSStephen M. Cameron 			return 1;
7887d1fea47cSStephen M. Cameron 		}
78889846590eSStephen M. Cameron 		spin_lock_irqsave(&h->offline_device_lock, flags);
788976438d08SStephen M. Cameron 	}
78909846590eSStephen M. Cameron 	spin_unlock_irqrestore(&h->offline_device_lock, flags);
78919846590eSStephen M. Cameron 	return 0;
78929846590eSStephen M. Cameron }
78939846590eSStephen M. Cameron 
78946636e7f4SDon Brace static void hpsa_rescan_ctlr_worker(struct work_struct *work)
7895a0c12413SStephen M. Cameron {
7896a0c12413SStephen M. Cameron 	unsigned long flags;
78978a98db73SStephen M. Cameron 	struct ctlr_info *h = container_of(to_delayed_work(work),
78986636e7f4SDon Brace 					struct ctlr_info, rescan_ctlr_work);
78996636e7f4SDon Brace 
79006636e7f4SDon Brace 
79016636e7f4SDon Brace 	if (h->remove_in_progress)
79028a98db73SStephen M. Cameron 		return;
79039846590eSStephen M. Cameron 
79049846590eSStephen M. Cameron 	if (hpsa_ctlr_needs_rescan(h) || hpsa_offline_devices_ready(h)) {
79059846590eSStephen M. Cameron 		scsi_host_get(h->scsi_host);
79069846590eSStephen M. Cameron 		hpsa_ack_ctlr_events(h);
79079846590eSStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
79089846590eSStephen M. Cameron 		scsi_host_put(h->scsi_host);
79099846590eSStephen M. Cameron 	}
79106636e7f4SDon Brace 	spin_lock_irqsave(&h->lock, flags);
79116636e7f4SDon Brace 	if (!h->remove_in_progress)
79126636e7f4SDon Brace 		queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
79136636e7f4SDon Brace 				h->heartbeat_sample_interval);
79146636e7f4SDon Brace 	spin_unlock_irqrestore(&h->lock, flags);
79156636e7f4SDon Brace }
79166636e7f4SDon Brace 
79176636e7f4SDon Brace static void hpsa_monitor_ctlr_worker(struct work_struct *work)
79186636e7f4SDon Brace {
79196636e7f4SDon Brace 	unsigned long flags;
79206636e7f4SDon Brace 	struct ctlr_info *h = container_of(to_delayed_work(work),
79216636e7f4SDon Brace 					struct ctlr_info, monitor_ctlr_work);
79226636e7f4SDon Brace 
79236636e7f4SDon Brace 	detect_controller_lockup(h);
79246636e7f4SDon Brace 	if (lockup_detected(h))
79256636e7f4SDon Brace 		return;
79269846590eSStephen M. Cameron 
79278a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
79286636e7f4SDon Brace 	if (!h->remove_in_progress)
79298a98db73SStephen M. Cameron 		schedule_delayed_work(&h->monitor_ctlr_work,
79308a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
79318a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
7932a0c12413SStephen M. Cameron }
7933a0c12413SStephen M. Cameron 
79346636e7f4SDon Brace static struct workqueue_struct *hpsa_create_controller_wq(struct ctlr_info *h,
79356636e7f4SDon Brace 						char *name)
79366636e7f4SDon Brace {
79376636e7f4SDon Brace 	struct workqueue_struct *wq = NULL;
79386636e7f4SDon Brace 
7939397ea9cbSDon Brace 	wq = alloc_ordered_workqueue("%s_%d_hpsa", 0, name, h->ctlr);
79406636e7f4SDon Brace 	if (!wq)
79416636e7f4SDon Brace 		dev_err(&h->pdev->dev, "failed to create %s workqueue\n", name);
79426636e7f4SDon Brace 
79436636e7f4SDon Brace 	return wq;
79446636e7f4SDon Brace }
79456636e7f4SDon Brace 
79466f039790SGreg Kroah-Hartman static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
79474c2a8c40SStephen M. Cameron {
79484c2a8c40SStephen M. Cameron 	int dac, rc;
79494c2a8c40SStephen M. Cameron 	struct ctlr_info *h;
795064670ac8SStephen M. Cameron 	int try_soft_reset = 0;
795164670ac8SStephen M. Cameron 	unsigned long flags;
79526b6c1cd7STomas Henzl 	u32 board_id;
79534c2a8c40SStephen M. Cameron 
79544c2a8c40SStephen M. Cameron 	if (number_of_controllers == 0)
79554c2a8c40SStephen M. Cameron 		printk(KERN_INFO DRIVER_NAME "\n");
79564c2a8c40SStephen M. Cameron 
79576b6c1cd7STomas Henzl 	rc = hpsa_lookup_board_id(pdev, &board_id);
79586b6c1cd7STomas Henzl 	if (rc < 0) {
79596b6c1cd7STomas Henzl 		dev_warn(&pdev->dev, "Board ID not found\n");
79606b6c1cd7STomas Henzl 		return rc;
79616b6c1cd7STomas Henzl 	}
79626b6c1cd7STomas Henzl 
79636b6c1cd7STomas Henzl 	rc = hpsa_init_reset_devices(pdev, board_id);
796464670ac8SStephen M. Cameron 	if (rc) {
796564670ac8SStephen M. Cameron 		if (rc != -ENOTSUPP)
79664c2a8c40SStephen M. Cameron 			return rc;
796764670ac8SStephen M. Cameron 		/* If the reset fails in a particular way (it has no way to do
796864670ac8SStephen M. Cameron 		 * a proper hard reset, so returns -ENOTSUPP) we can try to do
796964670ac8SStephen M. Cameron 		 * a soft reset once we get the controller configured up to the
797064670ac8SStephen M. Cameron 		 * point that it can accept a command.
797164670ac8SStephen M. Cameron 		 */
797264670ac8SStephen M. Cameron 		try_soft_reset = 1;
797364670ac8SStephen M. Cameron 		rc = 0;
797464670ac8SStephen M. Cameron 	}
797564670ac8SStephen M. Cameron 
797664670ac8SStephen M. Cameron reinit_after_soft_reset:
79774c2a8c40SStephen M. Cameron 
7978303932fdSDon Brace 	/* Command structures must be aligned on a 32-byte boundary because
7979303932fdSDon Brace 	 * the 5 lower bits of the address are used by the hardware. and by
7980303932fdSDon Brace 	 * the driver.  See comments in hpsa.h for more info.
7981303932fdSDon Brace 	 */
7982303932fdSDon Brace 	BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
7983edd16368SStephen M. Cameron 	h = kzalloc(sizeof(*h), GFP_KERNEL);
7984105a3dbcSRobert Elliott 	if (!h) {
7985105a3dbcSRobert Elliott 		dev_err(&pdev->dev, "Failed to allocate controller head\n");
7986ecd9aad4SStephen M. Cameron 		return -ENOMEM;
7987105a3dbcSRobert Elliott 	}
7988edd16368SStephen M. Cameron 
798955c06c71SStephen M. Cameron 	h->pdev = pdev;
7990105a3dbcSRobert Elliott 
7991a9a3a273SStephen M. Cameron 	h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
79929846590eSStephen M. Cameron 	INIT_LIST_HEAD(&h->offline_device_list);
79936eaf46fdSStephen M. Cameron 	spin_lock_init(&h->lock);
79949846590eSStephen M. Cameron 	spin_lock_init(&h->offline_device_lock);
79956eaf46fdSStephen M. Cameron 	spin_lock_init(&h->scan_lock);
799634f0c627SDon Brace 	atomic_set(&h->passthru_cmds_avail, HPSA_MAX_CONCURRENT_PASSTHRUS);
79979b5c48c2SStephen Cameron 	atomic_set(&h->abort_cmds_available, HPSA_CMDS_RESERVED_FOR_ABORTS);
7998094963daSStephen M. Cameron 
7999094963daSStephen M. Cameron 	/* Allocate and clear per-cpu variable lockup_detected */
8000094963daSStephen M. Cameron 	h->lockup_detected = alloc_percpu(u32);
80012a5ac326SStephen M. Cameron 	if (!h->lockup_detected) {
8002105a3dbcSRobert Elliott 		dev_err(&h->pdev->dev, "Failed to allocate lockup detector\n");
80032a5ac326SStephen M. Cameron 		rc = -ENOMEM;
80042efa5929SRobert Elliott 		goto clean1;	/* aer/h */
80052a5ac326SStephen M. Cameron 	}
8006094963daSStephen M. Cameron 	set_lockup_detected_for_all_cpus(h, 0);
8007094963daSStephen M. Cameron 
800855c06c71SStephen M. Cameron 	rc = hpsa_pci_init(h);
8009105a3dbcSRobert Elliott 	if (rc)
80102946e82bSRobert Elliott 		goto clean2;	/* lu, aer/h */
8011edd16368SStephen M. Cameron 
80122946e82bSRobert Elliott 	/* relies on h-> settings made by hpsa_pci_init, including
80132946e82bSRobert Elliott 	 * interrupt_mode h->intr */
80142946e82bSRobert Elliott 	rc = hpsa_scsi_host_alloc(h);
80152946e82bSRobert Elliott 	if (rc)
80162946e82bSRobert Elliott 		goto clean2_5;	/* pci, lu, aer/h */
80172946e82bSRobert Elliott 
80182946e82bSRobert Elliott 	sprintf(h->devname, HPSA "%d", h->scsi_host->host_no);
8019edd16368SStephen M. Cameron 	h->ctlr = number_of_controllers;
8020edd16368SStephen M. Cameron 	number_of_controllers++;
8021edd16368SStephen M. Cameron 
8022edd16368SStephen M. Cameron 	/* configure PCI DMA stuff */
8023ecd9aad4SStephen M. Cameron 	rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
8024ecd9aad4SStephen M. Cameron 	if (rc == 0) {
8025edd16368SStephen M. Cameron 		dac = 1;
8026ecd9aad4SStephen M. Cameron 	} else {
8027ecd9aad4SStephen M. Cameron 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8028ecd9aad4SStephen M. Cameron 		if (rc == 0) {
8029edd16368SStephen M. Cameron 			dac = 0;
8030ecd9aad4SStephen M. Cameron 		} else {
8031edd16368SStephen M. Cameron 			dev_err(&pdev->dev, "no suitable DMA available\n");
80322946e82bSRobert Elliott 			goto clean3;	/* shost, pci, lu, aer/h */
8033edd16368SStephen M. Cameron 		}
8034ecd9aad4SStephen M. Cameron 	}
8035edd16368SStephen M. Cameron 
8036edd16368SStephen M. Cameron 	/* make sure the board interrupts are off */
8037edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
803810f66018SStephen M. Cameron 
8039105a3dbcSRobert Elliott 	rc = hpsa_request_irqs(h, do_hpsa_intr_msi, do_hpsa_intr_intx);
8040105a3dbcSRobert Elliott 	if (rc)
80412946e82bSRobert Elliott 		goto clean3;	/* shost, pci, lu, aer/h */
8042d37ffbe4SRobert Elliott 	rc = hpsa_alloc_cmd_pool(h);
80438947fd10SRobert Elliott 	if (rc)
80442946e82bSRobert Elliott 		goto clean4;	/* irq, shost, pci, lu, aer/h */
8045105a3dbcSRobert Elliott 	rc = hpsa_alloc_sg_chain_blocks(h);
8046105a3dbcSRobert Elliott 	if (rc)
80472946e82bSRobert Elliott 		goto clean5;	/* cmd, irq, shost, pci, lu, aer/h */
8048a08a8471SStephen M. Cameron 	init_waitqueue_head(&h->scan_wait_queue);
80499b5c48c2SStephen Cameron 	init_waitqueue_head(&h->abort_cmd_wait_queue);
8050d604f533SWebb Scales 	init_waitqueue_head(&h->event_sync_wait_queue);
8051d604f533SWebb Scales 	mutex_init(&h->reset_mutex);
8052a08a8471SStephen M. Cameron 	h->scan_finished = 1; /* no scan currently in progress */
8053edd16368SStephen M. Cameron 
8054edd16368SStephen M. Cameron 	pci_set_drvdata(pdev, h);
80559a41338eSStephen M. Cameron 	h->ndevices = 0;
80562946e82bSRobert Elliott 
80579a41338eSStephen M. Cameron 	spin_lock_init(&h->devlock);
8058105a3dbcSRobert Elliott 	rc = hpsa_put_ctlr_into_performant_mode(h);
8059105a3dbcSRobert Elliott 	if (rc)
80602946e82bSRobert Elliott 		goto clean6; /* sg, cmd, irq, shost, pci, lu, aer/h */
80612946e82bSRobert Elliott 
80622946e82bSRobert Elliott 	/* hook into SCSI subsystem */
80632946e82bSRobert Elliott 	rc = hpsa_scsi_add_host(h);
80642946e82bSRobert Elliott 	if (rc)
80652946e82bSRobert Elliott 		goto clean7; /* perf, sg, cmd, irq, shost, pci, lu, aer/h */
80662efa5929SRobert Elliott 
80672efa5929SRobert Elliott 	/* create the resubmit workqueue */
80682efa5929SRobert Elliott 	h->rescan_ctlr_wq = hpsa_create_controller_wq(h, "rescan");
80692efa5929SRobert Elliott 	if (!h->rescan_ctlr_wq) {
80702efa5929SRobert Elliott 		rc = -ENOMEM;
80712efa5929SRobert Elliott 		goto clean7;
80722efa5929SRobert Elliott 	}
80732efa5929SRobert Elliott 
80742efa5929SRobert Elliott 	h->resubmit_wq = hpsa_create_controller_wq(h, "resubmit");
80752efa5929SRobert Elliott 	if (!h->resubmit_wq) {
80762efa5929SRobert Elliott 		rc = -ENOMEM;
80772efa5929SRobert Elliott 		goto clean7;	/* aer/h */
80782efa5929SRobert Elliott 	}
807964670ac8SStephen M. Cameron 
8080105a3dbcSRobert Elliott 	/*
8081105a3dbcSRobert Elliott 	 * At this point, the controller is ready to take commands.
808264670ac8SStephen M. Cameron 	 * Now, if reset_devices and the hard reset didn't work, try
808364670ac8SStephen M. Cameron 	 * the soft reset and see if that works.
808464670ac8SStephen M. Cameron 	 */
808564670ac8SStephen M. Cameron 	if (try_soft_reset) {
808664670ac8SStephen M. Cameron 
808764670ac8SStephen M. Cameron 		/* This is kind of gross.  We may or may not get a completion
808864670ac8SStephen M. Cameron 		 * from the soft reset command, and if we do, then the value
808964670ac8SStephen M. Cameron 		 * from the fifo may or may not be valid.  So, we wait 10 secs
809064670ac8SStephen M. Cameron 		 * after the reset throwing away any completions we get during
809164670ac8SStephen M. Cameron 		 * that time.  Unregister the interrupt handler and register
809264670ac8SStephen M. Cameron 		 * fake ones to scoop up any residual completions.
809364670ac8SStephen M. Cameron 		 */
809464670ac8SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
809564670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
809664670ac8SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
8097ec501a18SRobert Elliott 		hpsa_free_irqs(h);
80989ee61794SRobert Elliott 		rc = hpsa_request_irqs(h, hpsa_msix_discard_completions,
809964670ac8SStephen M. Cameron 					hpsa_intx_discard_completions);
810064670ac8SStephen M. Cameron 		if (rc) {
81019ee61794SRobert Elliott 			dev_warn(&h->pdev->dev,
81029ee61794SRobert Elliott 				"Failed to request_irq after soft reset.\n");
8103d498757cSRobert Elliott 			/*
8104b2ef480cSRobert Elliott 			 * cannot goto clean7 or free_irqs will be called
8105b2ef480cSRobert Elliott 			 * again. Instead, do its work
8106b2ef480cSRobert Elliott 			 */
8107b2ef480cSRobert Elliott 			hpsa_free_performant_mode(h);	/* clean7 */
8108b2ef480cSRobert Elliott 			hpsa_free_sg_chain_blocks(h);	/* clean6 */
8109b2ef480cSRobert Elliott 			hpsa_free_cmd_pool(h);		/* clean5 */
8110b2ef480cSRobert Elliott 			/*
8111b2ef480cSRobert Elliott 			 * skip hpsa_free_irqs(h) clean4 since that
8112b2ef480cSRobert Elliott 			 * was just called before request_irqs failed
8113d498757cSRobert Elliott 			 */
8114d498757cSRobert Elliott 			goto clean3;
811564670ac8SStephen M. Cameron 		}
811664670ac8SStephen M. Cameron 
811764670ac8SStephen M. Cameron 		rc = hpsa_kdump_soft_reset(h);
811864670ac8SStephen M. Cameron 		if (rc)
811964670ac8SStephen M. Cameron 			/* Neither hard nor soft reset worked, we're hosed. */
81207ef7323fSDon Brace 			goto clean7;
812164670ac8SStephen M. Cameron 
812264670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev, "Board READY.\n");
812364670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev,
812464670ac8SStephen M. Cameron 			"Waiting for stale completions to drain.\n");
812564670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_ON);
812664670ac8SStephen M. Cameron 		msleep(10000);
812764670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
812864670ac8SStephen M. Cameron 
812964670ac8SStephen M. Cameron 		rc = controller_reset_failed(h->cfgtable);
813064670ac8SStephen M. Cameron 		if (rc)
813164670ac8SStephen M. Cameron 			dev_info(&h->pdev->dev,
813264670ac8SStephen M. Cameron 				"Soft reset appears to have failed.\n");
813364670ac8SStephen M. Cameron 
813464670ac8SStephen M. Cameron 		/* since the controller's reset, we have to go back and re-init
813564670ac8SStephen M. Cameron 		 * everything.  Easiest to just forget what we've done and do it
813664670ac8SStephen M. Cameron 		 * all over again.
813764670ac8SStephen M. Cameron 		 */
813864670ac8SStephen M. Cameron 		hpsa_undo_allocations_after_kdump_soft_reset(h);
813964670ac8SStephen M. Cameron 		try_soft_reset = 0;
814064670ac8SStephen M. Cameron 		if (rc)
8141b2ef480cSRobert Elliott 			/* don't goto clean, we already unallocated */
814264670ac8SStephen M. Cameron 			return -ENODEV;
814364670ac8SStephen M. Cameron 
814464670ac8SStephen M. Cameron 		goto reinit_after_soft_reset;
814564670ac8SStephen M. Cameron 	}
8146edd16368SStephen M. Cameron 
8147da0697bdSScott Teel 	/* Enable Accelerated IO path at driver layer */
8148da0697bdSScott Teel 	h->acciopath_status = 1;
8149da0697bdSScott Teel 
8150e863d68eSScott Teel 
8151edd16368SStephen M. Cameron 	/* Turn the interrupts on so we can service requests */
8152edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_ON);
8153edd16368SStephen M. Cameron 
8154339b2b14SStephen M. Cameron 	hpsa_hba_inquiry(h);
81558a98db73SStephen M. Cameron 
81568a98db73SStephen M. Cameron 	/* Monitor the controller for firmware lockups */
81578a98db73SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
81588a98db73SStephen M. Cameron 	INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
81598a98db73SStephen M. Cameron 	schedule_delayed_work(&h->monitor_ctlr_work,
81608a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
81616636e7f4SDon Brace 	INIT_DELAYED_WORK(&h->rescan_ctlr_work, hpsa_rescan_ctlr_worker);
81626636e7f4SDon Brace 	queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
81636636e7f4SDon Brace 				h->heartbeat_sample_interval);
816488bf6d62SStephen M. Cameron 	return 0;
8165edd16368SStephen M. Cameron 
81662946e82bSRobert Elliott clean7: /* perf, sg, cmd, irq, shost, pci, lu, aer/h */
8167105a3dbcSRobert Elliott 	hpsa_free_performant_mode(h);
8168105a3dbcSRobert Elliott 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
8169105a3dbcSRobert Elliott clean6: /* sg, cmd, irq, pci, lockup, wq/aer/h */
817033a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
81712946e82bSRobert Elliott clean5: /* cmd, irq, shost, pci, lu, aer/h */
81722e9d1b36SStephen M. Cameron 	hpsa_free_cmd_pool(h);
81732946e82bSRobert Elliott clean4: /* irq, shost, pci, lu, aer/h */
8174ec501a18SRobert Elliott 	hpsa_free_irqs(h);
81752946e82bSRobert Elliott clean3: /* shost, pci, lu, aer/h */
81762946e82bSRobert Elliott 	scsi_host_put(h->scsi_host);
81772946e82bSRobert Elliott 	h->scsi_host = NULL;
81782946e82bSRobert Elliott clean2_5: /* pci, lu, aer/h */
8179195f2c65SRobert Elliott 	hpsa_free_pci_init(h);
81802946e82bSRobert Elliott clean2: /* lu, aer/h */
8181105a3dbcSRobert Elliott 	if (h->lockup_detected) {
8182094963daSStephen M. Cameron 		free_percpu(h->lockup_detected);
8183105a3dbcSRobert Elliott 		h->lockup_detected = NULL;
8184105a3dbcSRobert Elliott 	}
8185105a3dbcSRobert Elliott clean1:	/* wq/aer/h */
8186105a3dbcSRobert Elliott 	if (h->resubmit_wq) {
8187105a3dbcSRobert Elliott 		destroy_workqueue(h->resubmit_wq);
8188105a3dbcSRobert Elliott 		h->resubmit_wq = NULL;
8189105a3dbcSRobert Elliott 	}
8190105a3dbcSRobert Elliott 	if (h->rescan_ctlr_wq) {
8191105a3dbcSRobert Elliott 		destroy_workqueue(h->rescan_ctlr_wq);
8192105a3dbcSRobert Elliott 		h->rescan_ctlr_wq = NULL;
8193105a3dbcSRobert Elliott 	}
8194edd16368SStephen M. Cameron 	kfree(h);
8195ecd9aad4SStephen M. Cameron 	return rc;
8196edd16368SStephen M. Cameron }
8197edd16368SStephen M. Cameron 
8198edd16368SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h)
8199edd16368SStephen M. Cameron {
8200edd16368SStephen M. Cameron 	char *flush_buf;
8201edd16368SStephen M. Cameron 	struct CommandList *c;
820225163bd5SWebb Scales 	int rc;
8203702890e3SStephen M. Cameron 
8204094963daSStephen M. Cameron 	if (unlikely(lockup_detected(h)))
8205702890e3SStephen M. Cameron 		return;
8206edd16368SStephen M. Cameron 	flush_buf = kzalloc(4, GFP_KERNEL);
8207edd16368SStephen M. Cameron 	if (!flush_buf)
8208edd16368SStephen M. Cameron 		return;
8209edd16368SStephen M. Cameron 
821045fcb86eSStephen Cameron 	c = cmd_alloc(h);
8211bf43caf3SRobert Elliott 
8212a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
8213a2dac136SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_CMD)) {
8214a2dac136SStephen M. Cameron 		goto out;
8215a2dac136SStephen M. Cameron 	}
821625163bd5SWebb Scales 	rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
821725163bd5SWebb Scales 					PCI_DMA_TODEVICE, NO_TIMEOUT);
821825163bd5SWebb Scales 	if (rc)
821925163bd5SWebb Scales 		goto out;
8220edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus != 0)
8221a2dac136SStephen M. Cameron out:
8222edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
8223edd16368SStephen M. Cameron 			"error flushing cache on controller\n");
822445fcb86eSStephen Cameron 	cmd_free(h, c);
8225edd16368SStephen M. Cameron 	kfree(flush_buf);
8226edd16368SStephen M. Cameron }
8227edd16368SStephen M. Cameron 
8228edd16368SStephen M. Cameron static void hpsa_shutdown(struct pci_dev *pdev)
8229edd16368SStephen M. Cameron {
8230edd16368SStephen M. Cameron 	struct ctlr_info *h;
8231edd16368SStephen M. Cameron 
8232edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
8233edd16368SStephen M. Cameron 	/* Turn board interrupts off  and send the flush cache command
8234edd16368SStephen M. Cameron 	 * sendcmd will turn off interrupt, and send the flush...
8235edd16368SStephen M. Cameron 	 * To write all data in the battery backed cache to disks
8236edd16368SStephen M. Cameron 	 */
8237edd16368SStephen M. Cameron 	hpsa_flush_cache(h);
8238edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
8239105a3dbcSRobert Elliott 	hpsa_free_irqs(h);			/* init_one 4 */
8240cc64c817SRobert Elliott 	hpsa_disable_interrupt_mode(h);		/* pci_init 2 */
8241edd16368SStephen M. Cameron }
8242edd16368SStephen M. Cameron 
82436f039790SGreg Kroah-Hartman static void hpsa_free_device_info(struct ctlr_info *h)
824455e14e76SStephen M. Cameron {
824555e14e76SStephen M. Cameron 	int i;
824655e14e76SStephen M. Cameron 
8247105a3dbcSRobert Elliott 	for (i = 0; i < h->ndevices; i++) {
824855e14e76SStephen M. Cameron 		kfree(h->dev[i]);
8249105a3dbcSRobert Elliott 		h->dev[i] = NULL;
8250105a3dbcSRobert Elliott 	}
825155e14e76SStephen M. Cameron }
825255e14e76SStephen M. Cameron 
82536f039790SGreg Kroah-Hartman static void hpsa_remove_one(struct pci_dev *pdev)
8254edd16368SStephen M. Cameron {
8255edd16368SStephen M. Cameron 	struct ctlr_info *h;
82568a98db73SStephen M. Cameron 	unsigned long flags;
8257edd16368SStephen M. Cameron 
8258edd16368SStephen M. Cameron 	if (pci_get_drvdata(pdev) == NULL) {
8259edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "unable to remove device\n");
8260edd16368SStephen M. Cameron 		return;
8261edd16368SStephen M. Cameron 	}
8262edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
82638a98db73SStephen M. Cameron 
82648a98db73SStephen M. Cameron 	/* Get rid of any controller monitoring work items */
82658a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
82668a98db73SStephen M. Cameron 	h->remove_in_progress = 1;
82678a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
82686636e7f4SDon Brace 	cancel_delayed_work_sync(&h->monitor_ctlr_work);
82696636e7f4SDon Brace 	cancel_delayed_work_sync(&h->rescan_ctlr_work);
82706636e7f4SDon Brace 	destroy_workqueue(h->rescan_ctlr_wq);
82716636e7f4SDon Brace 	destroy_workqueue(h->resubmit_wq);
8272cc64c817SRobert Elliott 
8273105a3dbcSRobert Elliott 	/* includes hpsa_free_irqs - init_one 4 */
8274195f2c65SRobert Elliott 	/* includes hpsa_disable_interrupt_mode - pci_init 2 */
8275edd16368SStephen M. Cameron 	hpsa_shutdown(pdev);
8276cc64c817SRobert Elliott 
8277105a3dbcSRobert Elliott 	hpsa_free_device_info(h);		/* scan */
8278105a3dbcSRobert Elliott 
82792946e82bSRobert Elliott 	kfree(h->hba_inquiry_data);			/* init_one 10 */
82802946e82bSRobert Elliott 	h->hba_inquiry_data = NULL;			/* init_one 10 */
82812946e82bSRobert Elliott 	if (h->scsi_host)
82822946e82bSRobert Elliott 		scsi_remove_host(h->scsi_host);		/* init_one 8 */
82832946e82bSRobert Elliott 	hpsa_free_ioaccel2_sg_chain_blocks(h);
8284105a3dbcSRobert Elliott 	hpsa_free_performant_mode(h);			/* init_one 7 */
8285105a3dbcSRobert Elliott 	hpsa_free_sg_chain_blocks(h);			/* init_one 6 */
82861fb7c98aSRobert Elliott 	hpsa_free_cmd_pool(h);				/* init_one 5 */
8287105a3dbcSRobert Elliott 
8288105a3dbcSRobert Elliott 	/* hpsa_free_irqs already called via hpsa_shutdown init_one 4 */
8289195f2c65SRobert Elliott 
82902946e82bSRobert Elliott 	scsi_host_put(h->scsi_host);			/* init_one 3 */
82912946e82bSRobert Elliott 	h->scsi_host = NULL;				/* init_one 3 */
82922946e82bSRobert Elliott 
8293195f2c65SRobert Elliott 	/* includes hpsa_disable_interrupt_mode - pci_init 2 */
82942946e82bSRobert Elliott 	hpsa_free_pci_init(h);				/* init_one 2.5 */
8295195f2c65SRobert Elliott 
8296105a3dbcSRobert Elliott 	free_percpu(h->lockup_detected);		/* init_one 2 */
8297105a3dbcSRobert Elliott 	h->lockup_detected = NULL;			/* init_one 2 */
8298105a3dbcSRobert Elliott 	/* (void) pci_disable_pcie_error_reporting(pdev); */	/* init_one 1 */
8299105a3dbcSRobert Elliott 	kfree(h);					/* init_one 1 */
8300edd16368SStephen M. Cameron }
8301edd16368SStephen M. Cameron 
8302edd16368SStephen M. Cameron static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
8303edd16368SStephen M. Cameron 	__attribute__((unused)) pm_message_t state)
8304edd16368SStephen M. Cameron {
8305edd16368SStephen M. Cameron 	return -ENOSYS;
8306edd16368SStephen M. Cameron }
8307edd16368SStephen M. Cameron 
8308edd16368SStephen M. Cameron static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
8309edd16368SStephen M. Cameron {
8310edd16368SStephen M. Cameron 	return -ENOSYS;
8311edd16368SStephen M. Cameron }
8312edd16368SStephen M. Cameron 
8313edd16368SStephen M. Cameron static struct pci_driver hpsa_pci_driver = {
8314f79cfec6SStephen M. Cameron 	.name = HPSA,
8315edd16368SStephen M. Cameron 	.probe = hpsa_init_one,
83166f039790SGreg Kroah-Hartman 	.remove = hpsa_remove_one,
8317edd16368SStephen M. Cameron 	.id_table = hpsa_pci_device_id,	/* id_table */
8318edd16368SStephen M. Cameron 	.shutdown = hpsa_shutdown,
8319edd16368SStephen M. Cameron 	.suspend = hpsa_suspend,
8320edd16368SStephen M. Cameron 	.resume = hpsa_resume,
8321edd16368SStephen M. Cameron };
8322edd16368SStephen M. Cameron 
8323303932fdSDon Brace /* Fill in bucket_map[], given nsgs (the max number of
8324303932fdSDon Brace  * scatter gather elements supported) and bucket[],
8325303932fdSDon Brace  * which is an array of 8 integers.  The bucket[] array
8326303932fdSDon Brace  * contains 8 different DMA transfer sizes (in 16
8327303932fdSDon Brace  * byte increments) which the controller uses to fetch
8328303932fdSDon Brace  * commands.  This function fills in bucket_map[], which
8329303932fdSDon Brace  * maps a given number of scatter gather elements to one of
8330303932fdSDon Brace  * the 8 DMA transfer sizes.  The point of it is to allow the
8331303932fdSDon Brace  * controller to only do as much DMA as needed to fetch the
8332303932fdSDon Brace  * command, with the DMA transfer size encoded in the lower
8333303932fdSDon Brace  * bits of the command address.
8334303932fdSDon Brace  */
8335303932fdSDon Brace static void  calc_bucket_map(int bucket[], int num_buckets,
83362b08b3e9SDon Brace 	int nsgs, int min_blocks, u32 *bucket_map)
8337303932fdSDon Brace {
8338303932fdSDon Brace 	int i, j, b, size;
8339303932fdSDon Brace 
8340303932fdSDon Brace 	/* Note, bucket_map must have nsgs+1 entries. */
8341303932fdSDon Brace 	for (i = 0; i <= nsgs; i++) {
8342303932fdSDon Brace 		/* Compute size of a command with i SG entries */
8343e1f7de0cSMatt Gates 		size = i + min_blocks;
8344303932fdSDon Brace 		b = num_buckets; /* Assume the biggest bucket */
8345303932fdSDon Brace 		/* Find the bucket that is just big enough */
8346e1f7de0cSMatt Gates 		for (j = 0; j < num_buckets; j++) {
8347303932fdSDon Brace 			if (bucket[j] >= size) {
8348303932fdSDon Brace 				b = j;
8349303932fdSDon Brace 				break;
8350303932fdSDon Brace 			}
8351303932fdSDon Brace 		}
8352303932fdSDon Brace 		/* for a command with i SG entries, use bucket b. */
8353303932fdSDon Brace 		bucket_map[i] = b;
8354303932fdSDon Brace 	}
8355303932fdSDon Brace }
8356303932fdSDon Brace 
8357105a3dbcSRobert Elliott /*
8358105a3dbcSRobert Elliott  * return -ENODEV on err, 0 on success (or no action)
8359105a3dbcSRobert Elliott  * allocates numerous items that must be freed later
8360105a3dbcSRobert Elliott  */
8361c706a795SRobert Elliott static int hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
8362303932fdSDon Brace {
83636c311b57SStephen M. Cameron 	int i;
83646c311b57SStephen M. Cameron 	unsigned long register_value;
8365e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
8366e1f7de0cSMatt Gates 			(trans_support & CFGTBL_Trans_use_short_tags) |
8367e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix |
8368b9af4937SStephen M. Cameron 			(trans_support & (CFGTBL_Trans_io_accel1 |
8369b9af4937SStephen M. Cameron 				CFGTBL_Trans_io_accel2));
8370e1f7de0cSMatt Gates 	struct access_method access = SA5_performant_access;
8371def342bdSStephen M. Cameron 
8372def342bdSStephen M. Cameron 	/* This is a bit complicated.  There are 8 registers on
8373def342bdSStephen M. Cameron 	 * the controller which we write to to tell it 8 different
8374def342bdSStephen M. Cameron 	 * sizes of commands which there may be.  It's a way of
8375def342bdSStephen M. Cameron 	 * reducing the DMA done to fetch each command.  Encoded into
8376def342bdSStephen M. Cameron 	 * each command's tag are 3 bits which communicate to the controller
8377def342bdSStephen M. Cameron 	 * which of the eight sizes that command fits within.  The size of
8378def342bdSStephen M. Cameron 	 * each command depends on how many scatter gather entries there are.
8379def342bdSStephen M. Cameron 	 * Each SG entry requires 16 bytes.  The eight registers are programmed
8380def342bdSStephen M. Cameron 	 * with the number of 16-byte blocks a command of that size requires.
8381def342bdSStephen M. Cameron 	 * The smallest command possible requires 5 such 16 byte blocks.
8382d66ae08bSStephen M. Cameron 	 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
8383def342bdSStephen M. Cameron 	 * blocks.  Note, this only extends to the SG entries contained
8384def342bdSStephen M. Cameron 	 * within the command block, and does not extend to chained blocks
8385def342bdSStephen M. Cameron 	 * of SG elements.   bft[] contains the eight values we write to
8386def342bdSStephen M. Cameron 	 * the registers.  They are not evenly distributed, but have more
8387def342bdSStephen M. Cameron 	 * sizes for small commands, and fewer sizes for larger commands.
8388def342bdSStephen M. Cameron 	 */
8389d66ae08bSStephen M. Cameron 	int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
8390b9af4937SStephen M. Cameron #define MIN_IOACCEL2_BFT_ENTRY 5
8391b9af4937SStephen M. Cameron #define HPSA_IOACCEL2_HEADER_SZ 4
8392b9af4937SStephen M. Cameron 	int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
8393b9af4937SStephen M. Cameron 			13, 14, 15, 16, 17, 18, 19,
8394b9af4937SStephen M. Cameron 			HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
8395b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
8396b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
8397b9af4937SStephen M. Cameron 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
8398b9af4937SStephen M. Cameron 				 16 * MIN_IOACCEL2_BFT_ENTRY);
8399b9af4937SStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
8400d66ae08bSStephen M. Cameron 	BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
8401303932fdSDon Brace 	/*  5 = 1 s/g entry or 4k
8402303932fdSDon Brace 	 *  6 = 2 s/g entry or 8k
8403303932fdSDon Brace 	 *  8 = 4 s/g entry or 16k
8404303932fdSDon Brace 	 * 10 = 6 s/g entry or 24k
8405303932fdSDon Brace 	 */
8406303932fdSDon Brace 
8407b3a52e79SStephen M. Cameron 	/* If the controller supports either ioaccel method then
8408b3a52e79SStephen M. Cameron 	 * we can also use the RAID stack submit path that does not
8409b3a52e79SStephen M. Cameron 	 * perform the superfluous readl() after each command submission.
8410b3a52e79SStephen M. Cameron 	 */
8411b3a52e79SStephen M. Cameron 	if (trans_support & (CFGTBL_Trans_io_accel1 | CFGTBL_Trans_io_accel2))
8412b3a52e79SStephen M. Cameron 		access = SA5_performant_access_no_read;
8413b3a52e79SStephen M. Cameron 
8414303932fdSDon Brace 	/* Controller spec: zero out this buffer. */
8415072b0518SStephen M. Cameron 	for (i = 0; i < h->nreply_queues; i++)
8416072b0518SStephen M. Cameron 		memset(h->reply_queue[i].head, 0, h->reply_queue_size);
8417303932fdSDon Brace 
8418d66ae08bSStephen M. Cameron 	bft[7] = SG_ENTRIES_IN_CMD + 4;
8419d66ae08bSStephen M. Cameron 	calc_bucket_map(bft, ARRAY_SIZE(bft),
8420e1f7de0cSMatt Gates 				SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
8421303932fdSDon Brace 	for (i = 0; i < 8; i++)
8422303932fdSDon Brace 		writel(bft[i], &h->transtable->BlockFetch[i]);
8423303932fdSDon Brace 
8424303932fdSDon Brace 	/* size of controller ring buffer */
8425303932fdSDon Brace 	writel(h->max_commands, &h->transtable->RepQSize);
8426254f796bSMatt Gates 	writel(h->nreply_queues, &h->transtable->RepQCount);
8427303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrLow32);
8428303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrHigh32);
8429254f796bSMatt Gates 
8430254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
8431254f796bSMatt Gates 		writel(0, &h->transtable->RepQAddr[i].upper);
8432072b0518SStephen M. Cameron 		writel(h->reply_queue[i].busaddr,
8433254f796bSMatt Gates 			&h->transtable->RepQAddr[i].lower);
8434254f796bSMatt Gates 	}
8435254f796bSMatt Gates 
8436b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
8437e1f7de0cSMatt Gates 	writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
8438e1f7de0cSMatt Gates 	/*
8439e1f7de0cSMatt Gates 	 * enable outbound interrupt coalescing in accelerator mode;
8440e1f7de0cSMatt Gates 	 */
8441e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
8442e1f7de0cSMatt Gates 		access = SA5_ioaccel_mode1_access;
8443e1f7de0cSMatt Gates 		writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
8444e1f7de0cSMatt Gates 		writel(4, &h->cfgtable->HostWrite.CoalIntCount);
8445c349775eSScott Teel 	} else {
8446c349775eSScott Teel 		if (trans_support & CFGTBL_Trans_io_accel2) {
8447c349775eSScott Teel 			access = SA5_ioaccel_mode2_access;
8448c349775eSScott Teel 			writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
8449c349775eSScott Teel 			writel(4, &h->cfgtable->HostWrite.CoalIntCount);
8450c349775eSScott Teel 		}
8451e1f7de0cSMatt Gates 	}
8452303932fdSDon Brace 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
8453c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h)) {
8454c706a795SRobert Elliott 		dev_err(&h->pdev->dev,
8455c706a795SRobert Elliott 			"performant mode problem - doorbell timeout\n");
8456c706a795SRobert Elliott 		return -ENODEV;
8457c706a795SRobert Elliott 	}
8458303932fdSDon Brace 	register_value = readl(&(h->cfgtable->TransportActive));
8459303932fdSDon Brace 	if (!(register_value & CFGTBL_Trans_Performant)) {
8460050f7147SStephen Cameron 		dev_err(&h->pdev->dev,
8461050f7147SStephen Cameron 			"performant mode problem - transport not active\n");
8462c706a795SRobert Elliott 		return -ENODEV;
8463303932fdSDon Brace 	}
8464960a30e7SStephen M. Cameron 	/* Change the access methods to the performant access methods */
8465e1f7de0cSMatt Gates 	h->access = access;
8466e1f7de0cSMatt Gates 	h->transMethod = transMethod;
8467e1f7de0cSMatt Gates 
8468b9af4937SStephen M. Cameron 	if (!((trans_support & CFGTBL_Trans_io_accel1) ||
8469b9af4937SStephen M. Cameron 		(trans_support & CFGTBL_Trans_io_accel2)))
8470c706a795SRobert Elliott 		return 0;
8471e1f7de0cSMatt Gates 
8472b9af4937SStephen M. Cameron 	if (trans_support & CFGTBL_Trans_io_accel1) {
8473e1f7de0cSMatt Gates 		/* Set up I/O accelerator mode */
8474e1f7de0cSMatt Gates 		for (i = 0; i < h->nreply_queues; i++) {
8475e1f7de0cSMatt Gates 			writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
8476e1f7de0cSMatt Gates 			h->reply_queue[i].current_entry =
8477e1f7de0cSMatt Gates 				readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
8478e1f7de0cSMatt Gates 		}
8479283b4a9bSStephen M. Cameron 		bft[7] = h->ioaccel_maxsg + 8;
8480283b4a9bSStephen M. Cameron 		calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
8481e1f7de0cSMatt Gates 				h->ioaccel1_blockFetchTable);
8482e1f7de0cSMatt Gates 
8483e1f7de0cSMatt Gates 		/* initialize all reply queue entries to unused */
8484072b0518SStephen M. Cameron 		for (i = 0; i < h->nreply_queues; i++)
8485072b0518SStephen M. Cameron 			memset(h->reply_queue[i].head,
8486072b0518SStephen M. Cameron 				(u8) IOACCEL_MODE1_REPLY_UNUSED,
8487072b0518SStephen M. Cameron 				h->reply_queue_size);
8488e1f7de0cSMatt Gates 
8489e1f7de0cSMatt Gates 		/* set all the constant fields in the accelerator command
8490e1f7de0cSMatt Gates 		 * frames once at init time to save CPU cycles later.
8491e1f7de0cSMatt Gates 		 */
8492e1f7de0cSMatt Gates 		for (i = 0; i < h->nr_cmds; i++) {
8493e1f7de0cSMatt Gates 			struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
8494e1f7de0cSMatt Gates 
8495e1f7de0cSMatt Gates 			cp->function = IOACCEL1_FUNCTION_SCSIIO;
8496e1f7de0cSMatt Gates 			cp->err_info = (u32) (h->errinfo_pool_dhandle +
8497e1f7de0cSMatt Gates 					(i * sizeof(struct ErrorInfo)));
8498e1f7de0cSMatt Gates 			cp->err_info_len = sizeof(struct ErrorInfo);
8499e1f7de0cSMatt Gates 			cp->sgl_offset = IOACCEL1_SGLOFFSET;
85002b08b3e9SDon Brace 			cp->host_context_flags =
85012b08b3e9SDon Brace 				cpu_to_le16(IOACCEL1_HCFLAGS_CISS_FORMAT);
8502e1f7de0cSMatt Gates 			cp->timeout_sec = 0;
8503e1f7de0cSMatt Gates 			cp->ReplyQueue = 0;
850450a0decfSStephen M. Cameron 			cp->tag =
8505f2405db8SDon Brace 				cpu_to_le64((i << DIRECT_LOOKUP_SHIFT));
850650a0decfSStephen M. Cameron 			cp->host_addr =
850750a0decfSStephen M. Cameron 				cpu_to_le64(h->ioaccel_cmd_pool_dhandle +
8508e1f7de0cSMatt Gates 					(i * sizeof(struct io_accel1_cmd)));
8509e1f7de0cSMatt Gates 		}
8510b9af4937SStephen M. Cameron 	} else if (trans_support & CFGTBL_Trans_io_accel2) {
8511b9af4937SStephen M. Cameron 		u64 cfg_offset, cfg_base_addr_index;
8512b9af4937SStephen M. Cameron 		u32 bft2_offset, cfg_base_addr;
8513b9af4937SStephen M. Cameron 		int rc;
8514b9af4937SStephen M. Cameron 
8515b9af4937SStephen M. Cameron 		rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
8516b9af4937SStephen M. Cameron 			&cfg_base_addr_index, &cfg_offset);
8517b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
8518b9af4937SStephen M. Cameron 		bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
8519b9af4937SStephen M. Cameron 		calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
8520b9af4937SStephen M. Cameron 				4, h->ioaccel2_blockFetchTable);
8521b9af4937SStephen M. Cameron 		bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
8522b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct CfgTable,
8523b9af4937SStephen M. Cameron 				io_accel_request_size_offset) != 0xb8);
8524b9af4937SStephen M. Cameron 		h->ioaccel2_bft2_regs =
8525b9af4937SStephen M. Cameron 			remap_pci_mem(pci_resource_start(h->pdev,
8526b9af4937SStephen M. Cameron 					cfg_base_addr_index) +
8527b9af4937SStephen M. Cameron 					cfg_offset + bft2_offset,
8528b9af4937SStephen M. Cameron 					ARRAY_SIZE(bft2) *
8529b9af4937SStephen M. Cameron 					sizeof(*h->ioaccel2_bft2_regs));
8530b9af4937SStephen M. Cameron 		for (i = 0; i < ARRAY_SIZE(bft2); i++)
8531b9af4937SStephen M. Cameron 			writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
8532b9af4937SStephen M. Cameron 	}
8533b9af4937SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
8534c706a795SRobert Elliott 	if (hpsa_wait_for_mode_change_ack(h)) {
8535c706a795SRobert Elliott 		dev_err(&h->pdev->dev,
8536c706a795SRobert Elliott 			"performant mode problem - enabling ioaccel mode\n");
8537c706a795SRobert Elliott 		return -ENODEV;
8538c706a795SRobert Elliott 	}
8539c706a795SRobert Elliott 	return 0;
8540e1f7de0cSMatt Gates }
8541e1f7de0cSMatt Gates 
85421fb7c98aSRobert Elliott /* Free ioaccel1 mode command blocks and block fetch table */
85431fb7c98aSRobert Elliott static void hpsa_free_ioaccel1_cmd_and_bft(struct ctlr_info *h)
85441fb7c98aSRobert Elliott {
8545105a3dbcSRobert Elliott 	if (h->ioaccel_cmd_pool) {
85461fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
85471fb7c98aSRobert Elliott 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
85481fb7c98aSRobert Elliott 			h->ioaccel_cmd_pool,
85491fb7c98aSRobert Elliott 			h->ioaccel_cmd_pool_dhandle);
8550105a3dbcSRobert Elliott 		h->ioaccel_cmd_pool = NULL;
8551105a3dbcSRobert Elliott 		h->ioaccel_cmd_pool_dhandle = 0;
8552105a3dbcSRobert Elliott 	}
85531fb7c98aSRobert Elliott 	kfree(h->ioaccel1_blockFetchTable);
8554105a3dbcSRobert Elliott 	h->ioaccel1_blockFetchTable = NULL;
85551fb7c98aSRobert Elliott }
85561fb7c98aSRobert Elliott 
8557d37ffbe4SRobert Elliott /* Allocate ioaccel1 mode command blocks and block fetch table */
8558d37ffbe4SRobert Elliott static int hpsa_alloc_ioaccel1_cmd_and_bft(struct ctlr_info *h)
8559e1f7de0cSMatt Gates {
8560283b4a9bSStephen M. Cameron 	h->ioaccel_maxsg =
8561283b4a9bSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
8562283b4a9bSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
8563283b4a9bSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
8564283b4a9bSStephen M. Cameron 
8565e1f7de0cSMatt Gates 	/* Command structures must be aligned on a 128-byte boundary
8566e1f7de0cSMatt Gates 	 * because the 7 lower bits of the address are used by the
8567e1f7de0cSMatt Gates 	 * hardware.
8568e1f7de0cSMatt Gates 	 */
8569e1f7de0cSMatt Gates 	BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
8570e1f7de0cSMatt Gates 			IOACCEL1_COMMANDLIST_ALIGNMENT);
8571e1f7de0cSMatt Gates 	h->ioaccel_cmd_pool =
8572e1f7de0cSMatt Gates 		pci_alloc_consistent(h->pdev,
8573e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
8574e1f7de0cSMatt Gates 			&(h->ioaccel_cmd_pool_dhandle));
8575e1f7de0cSMatt Gates 
8576e1f7de0cSMatt Gates 	h->ioaccel1_blockFetchTable =
8577283b4a9bSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
8578e1f7de0cSMatt Gates 				sizeof(u32)), GFP_KERNEL);
8579e1f7de0cSMatt Gates 
8580e1f7de0cSMatt Gates 	if ((h->ioaccel_cmd_pool == NULL) ||
8581e1f7de0cSMatt Gates 		(h->ioaccel1_blockFetchTable == NULL))
8582e1f7de0cSMatt Gates 		goto clean_up;
8583e1f7de0cSMatt Gates 
8584e1f7de0cSMatt Gates 	memset(h->ioaccel_cmd_pool, 0,
8585e1f7de0cSMatt Gates 		h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
8586e1f7de0cSMatt Gates 	return 0;
8587e1f7de0cSMatt Gates 
8588e1f7de0cSMatt Gates clean_up:
85891fb7c98aSRobert Elliott 	hpsa_free_ioaccel1_cmd_and_bft(h);
85902dd02d74SRobert Elliott 	return -ENOMEM;
85916c311b57SStephen M. Cameron }
85926c311b57SStephen M. Cameron 
85931fb7c98aSRobert Elliott /* Free ioaccel2 mode command blocks and block fetch table */
85941fb7c98aSRobert Elliott static void hpsa_free_ioaccel2_cmd_and_bft(struct ctlr_info *h)
85951fb7c98aSRobert Elliott {
8596d9a729f3SWebb Scales 	hpsa_free_ioaccel2_sg_chain_blocks(h);
8597d9a729f3SWebb Scales 
8598105a3dbcSRobert Elliott 	if (h->ioaccel2_cmd_pool) {
85991fb7c98aSRobert Elliott 		pci_free_consistent(h->pdev,
86001fb7c98aSRobert Elliott 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
86011fb7c98aSRobert Elliott 			h->ioaccel2_cmd_pool,
86021fb7c98aSRobert Elliott 			h->ioaccel2_cmd_pool_dhandle);
8603105a3dbcSRobert Elliott 		h->ioaccel2_cmd_pool = NULL;
8604105a3dbcSRobert Elliott 		h->ioaccel2_cmd_pool_dhandle = 0;
8605105a3dbcSRobert Elliott 	}
86061fb7c98aSRobert Elliott 	kfree(h->ioaccel2_blockFetchTable);
8607105a3dbcSRobert Elliott 	h->ioaccel2_blockFetchTable = NULL;
86081fb7c98aSRobert Elliott }
86091fb7c98aSRobert Elliott 
8610d37ffbe4SRobert Elliott /* Allocate ioaccel2 mode command blocks and block fetch table */
8611d37ffbe4SRobert Elliott static int hpsa_alloc_ioaccel2_cmd_and_bft(struct ctlr_info *h)
8612aca9012aSStephen M. Cameron {
8613d9a729f3SWebb Scales 	int rc;
8614d9a729f3SWebb Scales 
8615aca9012aSStephen M. Cameron 	/* Allocate ioaccel2 mode command blocks and block fetch table */
8616aca9012aSStephen M. Cameron 
8617aca9012aSStephen M. Cameron 	h->ioaccel_maxsg =
8618aca9012aSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
8619aca9012aSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
8620aca9012aSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
8621aca9012aSStephen M. Cameron 
8622aca9012aSStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
8623aca9012aSStephen M. Cameron 			IOACCEL2_COMMANDLIST_ALIGNMENT);
8624aca9012aSStephen M. Cameron 	h->ioaccel2_cmd_pool =
8625aca9012aSStephen M. Cameron 		pci_alloc_consistent(h->pdev,
8626aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
8627aca9012aSStephen M. Cameron 			&(h->ioaccel2_cmd_pool_dhandle));
8628aca9012aSStephen M. Cameron 
8629aca9012aSStephen M. Cameron 	h->ioaccel2_blockFetchTable =
8630aca9012aSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
8631aca9012aSStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
8632aca9012aSStephen M. Cameron 
8633aca9012aSStephen M. Cameron 	if ((h->ioaccel2_cmd_pool == NULL) ||
8634d9a729f3SWebb Scales 		(h->ioaccel2_blockFetchTable == NULL)) {
8635d9a729f3SWebb Scales 		rc = -ENOMEM;
8636d9a729f3SWebb Scales 		goto clean_up;
8637d9a729f3SWebb Scales 	}
8638d9a729f3SWebb Scales 
8639d9a729f3SWebb Scales 	rc = hpsa_allocate_ioaccel2_sg_chain_blocks(h);
8640d9a729f3SWebb Scales 	if (rc)
8641aca9012aSStephen M. Cameron 		goto clean_up;
8642aca9012aSStephen M. Cameron 
8643aca9012aSStephen M. Cameron 	memset(h->ioaccel2_cmd_pool, 0,
8644aca9012aSStephen M. Cameron 		h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
8645aca9012aSStephen M. Cameron 	return 0;
8646aca9012aSStephen M. Cameron 
8647aca9012aSStephen M. Cameron clean_up:
86481fb7c98aSRobert Elliott 	hpsa_free_ioaccel2_cmd_and_bft(h);
8649d9a729f3SWebb Scales 	return rc;
8650aca9012aSStephen M. Cameron }
8651aca9012aSStephen M. Cameron 
8652105a3dbcSRobert Elliott /* Free items allocated by hpsa_put_ctlr_into_performant_mode */
8653105a3dbcSRobert Elliott static void hpsa_free_performant_mode(struct ctlr_info *h)
8654105a3dbcSRobert Elliott {
8655105a3dbcSRobert Elliott 	kfree(h->blockFetchTable);
8656105a3dbcSRobert Elliott 	h->blockFetchTable = NULL;
8657105a3dbcSRobert Elliott 	hpsa_free_reply_queues(h);
8658105a3dbcSRobert Elliott 	hpsa_free_ioaccel1_cmd_and_bft(h);
8659105a3dbcSRobert Elliott 	hpsa_free_ioaccel2_cmd_and_bft(h);
8660105a3dbcSRobert Elliott }
8661105a3dbcSRobert Elliott 
8662105a3dbcSRobert Elliott /* return -ENODEV on error, 0 on success (or no action)
8663105a3dbcSRobert Elliott  * allocates numerous items that must be freed later
8664105a3dbcSRobert Elliott  */
8665105a3dbcSRobert Elliott static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
86666c311b57SStephen M. Cameron {
86676c311b57SStephen M. Cameron 	u32 trans_support;
8668e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
8669e1f7de0cSMatt Gates 					CFGTBL_Trans_use_short_tags;
8670105a3dbcSRobert Elliott 	int i, rc;
86716c311b57SStephen M. Cameron 
867202ec19c8SStephen M. Cameron 	if (hpsa_simple_mode)
8673105a3dbcSRobert Elliott 		return 0;
867402ec19c8SStephen M. Cameron 
867567c99a72Sscameron@beardog.cce.hp.com 	trans_support = readl(&(h->cfgtable->TransportSupport));
867667c99a72Sscameron@beardog.cce.hp.com 	if (!(trans_support & PERFORMANT_MODE))
8677105a3dbcSRobert Elliott 		return 0;
867867c99a72Sscameron@beardog.cce.hp.com 
8679e1f7de0cSMatt Gates 	/* Check for I/O accelerator mode support */
8680e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
8681e1f7de0cSMatt Gates 		transMethod |= CFGTBL_Trans_io_accel1 |
8682e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix;
8683105a3dbcSRobert Elliott 		rc = hpsa_alloc_ioaccel1_cmd_and_bft(h);
8684105a3dbcSRobert Elliott 		if (rc)
8685105a3dbcSRobert Elliott 			return rc;
8686105a3dbcSRobert Elliott 	} else if (trans_support & CFGTBL_Trans_io_accel2) {
8687aca9012aSStephen M. Cameron 		transMethod |= CFGTBL_Trans_io_accel2 |
8688aca9012aSStephen M. Cameron 				CFGTBL_Trans_enable_directed_msix;
8689105a3dbcSRobert Elliott 		rc = hpsa_alloc_ioaccel2_cmd_and_bft(h);
8690105a3dbcSRobert Elliott 		if (rc)
8691105a3dbcSRobert Elliott 			return rc;
8692e1f7de0cSMatt Gates 	}
8693e1f7de0cSMatt Gates 
8694eee0f03aSHannes Reinecke 	h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
8695cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
86966c311b57SStephen M. Cameron 	/* Performant mode ring buffer and supporting data structures */
8697072b0518SStephen M. Cameron 	h->reply_queue_size = h->max_commands * sizeof(u64);
86986c311b57SStephen M. Cameron 
8699254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
8700072b0518SStephen M. Cameron 		h->reply_queue[i].head = pci_alloc_consistent(h->pdev,
8701072b0518SStephen M. Cameron 						h->reply_queue_size,
8702072b0518SStephen M. Cameron 						&(h->reply_queue[i].busaddr));
8703105a3dbcSRobert Elliott 		if (!h->reply_queue[i].head) {
8704105a3dbcSRobert Elliott 			rc = -ENOMEM;
8705105a3dbcSRobert Elliott 			goto clean1;	/* rq, ioaccel */
8706105a3dbcSRobert Elliott 		}
8707254f796bSMatt Gates 		h->reply_queue[i].size = h->max_commands;
8708254f796bSMatt Gates 		h->reply_queue[i].wraparound = 1;  /* spec: init to 1 */
8709254f796bSMatt Gates 		h->reply_queue[i].current_entry = 0;
8710254f796bSMatt Gates 	}
8711254f796bSMatt Gates 
87126c311b57SStephen M. Cameron 	/* Need a block fetch table for performant mode */
8713d66ae08bSStephen M. Cameron 	h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
87146c311b57SStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
8715105a3dbcSRobert Elliott 	if (!h->blockFetchTable) {
8716105a3dbcSRobert Elliott 		rc = -ENOMEM;
8717105a3dbcSRobert Elliott 		goto clean1;	/* rq, ioaccel */
8718105a3dbcSRobert Elliott 	}
87196c311b57SStephen M. Cameron 
8720105a3dbcSRobert Elliott 	rc = hpsa_enter_performant_mode(h, trans_support);
8721105a3dbcSRobert Elliott 	if (rc)
8722105a3dbcSRobert Elliott 		goto clean2;	/* bft, rq, ioaccel */
8723105a3dbcSRobert Elliott 	return 0;
8724303932fdSDon Brace 
8725105a3dbcSRobert Elliott clean2:	/* bft, rq, ioaccel */
8726303932fdSDon Brace 	kfree(h->blockFetchTable);
8727105a3dbcSRobert Elliott 	h->blockFetchTable = NULL;
8728105a3dbcSRobert Elliott clean1:	/* rq, ioaccel */
8729105a3dbcSRobert Elliott 	hpsa_free_reply_queues(h);
8730105a3dbcSRobert Elliott 	hpsa_free_ioaccel1_cmd_and_bft(h);
8731105a3dbcSRobert Elliott 	hpsa_free_ioaccel2_cmd_and_bft(h);
8732105a3dbcSRobert Elliott 	return rc;
8733303932fdSDon Brace }
8734303932fdSDon Brace 
873523100dd9SStephen M. Cameron static int is_accelerated_cmd(struct CommandList *c)
873676438d08SStephen M. Cameron {
873723100dd9SStephen M. Cameron 	return c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_IOACCEL2;
873823100dd9SStephen M. Cameron }
873923100dd9SStephen M. Cameron 
874023100dd9SStephen M. Cameron static void hpsa_drain_accel_commands(struct ctlr_info *h)
874123100dd9SStephen M. Cameron {
874223100dd9SStephen M. Cameron 	struct CommandList *c = NULL;
8743f2405db8SDon Brace 	int i, accel_cmds_out;
8744281a7fd0SWebb Scales 	int refcount;
874576438d08SStephen M. Cameron 
8746f2405db8SDon Brace 	do { /* wait for all outstanding ioaccel commands to drain out */
874723100dd9SStephen M. Cameron 		accel_cmds_out = 0;
8748f2405db8SDon Brace 		for (i = 0; i < h->nr_cmds; i++) {
8749f2405db8SDon Brace 			c = h->cmd_pool + i;
8750281a7fd0SWebb Scales 			refcount = atomic_inc_return(&c->refcount);
8751281a7fd0SWebb Scales 			if (refcount > 1) /* Command is allocated */
875223100dd9SStephen M. Cameron 				accel_cmds_out += is_accelerated_cmd(c);
8753281a7fd0SWebb Scales 			cmd_free(h, c);
8754f2405db8SDon Brace 		}
875523100dd9SStephen M. Cameron 		if (accel_cmds_out <= 0)
875676438d08SStephen M. Cameron 			break;
875776438d08SStephen M. Cameron 		msleep(100);
875876438d08SStephen M. Cameron 	} while (1);
875976438d08SStephen M. Cameron }
876076438d08SStephen M. Cameron 
8761edd16368SStephen M. Cameron /*
8762edd16368SStephen M. Cameron  *  This is it.  Register the PCI driver information for the cards we control
8763edd16368SStephen M. Cameron  *  the OS will call our registered routines when it finds one of our cards.
8764edd16368SStephen M. Cameron  */
8765edd16368SStephen M. Cameron static int __init hpsa_init(void)
8766edd16368SStephen M. Cameron {
876731468401SMike Miller 	return pci_register_driver(&hpsa_pci_driver);
8768edd16368SStephen M. Cameron }
8769edd16368SStephen M. Cameron 
8770edd16368SStephen M. Cameron static void __exit hpsa_cleanup(void)
8771edd16368SStephen M. Cameron {
8772edd16368SStephen M. Cameron 	pci_unregister_driver(&hpsa_pci_driver);
8773edd16368SStephen M. Cameron }
8774edd16368SStephen M. Cameron 
8775e1f7de0cSMatt Gates static void __attribute__((unused)) verify_offsets(void)
8776e1f7de0cSMatt Gates {
8777e1f7de0cSMatt Gates #define VERIFY_OFFSET(member, offset) \
8778dd0e19f3SScott Teel 	BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
8779dd0e19f3SScott Teel 
8780dd0e19f3SScott Teel 	VERIFY_OFFSET(structure_size, 0);
8781dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_size, 4);
8782dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_cnt, 8);
8783dd0e19f3SScott Teel 	VERIFY_OFFSET(phys_blk_shift, 16);
8784dd0e19f3SScott Teel 	VERIFY_OFFSET(parity_rotation_shift, 17);
8785dd0e19f3SScott Teel 	VERIFY_OFFSET(strip_size, 18);
8786dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_starting_blk, 20);
8787dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_blk_cnt, 28);
8788dd0e19f3SScott Teel 	VERIFY_OFFSET(data_disks_per_row, 36);
8789dd0e19f3SScott Teel 	VERIFY_OFFSET(metadata_disks_per_row, 38);
8790dd0e19f3SScott Teel 	VERIFY_OFFSET(row_cnt, 40);
8791dd0e19f3SScott Teel 	VERIFY_OFFSET(layout_map_count, 42);
8792dd0e19f3SScott Teel 	VERIFY_OFFSET(flags, 44);
8793dd0e19f3SScott Teel 	VERIFY_OFFSET(dekindex, 46);
8794dd0e19f3SScott Teel 	/* VERIFY_OFFSET(reserved, 48 */
8795dd0e19f3SScott Teel 	VERIFY_OFFSET(data, 64);
8796dd0e19f3SScott Teel 
8797dd0e19f3SScott Teel #undef VERIFY_OFFSET
8798dd0e19f3SScott Teel 
8799dd0e19f3SScott Teel #define VERIFY_OFFSET(member, offset) \
8800b66cc250SMike Miller 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
8801b66cc250SMike Miller 
8802b66cc250SMike Miller 	VERIFY_OFFSET(IU_type, 0);
8803b66cc250SMike Miller 	VERIFY_OFFSET(direction, 1);
8804b66cc250SMike Miller 	VERIFY_OFFSET(reply_queue, 2);
8805b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved1, 3);  */
8806b66cc250SMike Miller 	VERIFY_OFFSET(scsi_nexus, 4);
8807b66cc250SMike Miller 	VERIFY_OFFSET(Tag, 8);
8808b66cc250SMike Miller 	VERIFY_OFFSET(cdb, 16);
8809b66cc250SMike Miller 	VERIFY_OFFSET(cciss_lun, 32);
8810b66cc250SMike Miller 	VERIFY_OFFSET(data_len, 40);
8811b66cc250SMike Miller 	VERIFY_OFFSET(cmd_priority_task_attr, 44);
8812b66cc250SMike Miller 	VERIFY_OFFSET(sg_count, 45);
8813b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved3 */
8814b66cc250SMike Miller 	VERIFY_OFFSET(err_ptr, 48);
8815b66cc250SMike Miller 	VERIFY_OFFSET(err_len, 56);
8816b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved4  */
8817b66cc250SMike Miller 	VERIFY_OFFSET(sg, 64);
8818b66cc250SMike Miller 
8819b66cc250SMike Miller #undef VERIFY_OFFSET
8820b66cc250SMike Miller 
8821b66cc250SMike Miller #define VERIFY_OFFSET(member, offset) \
8822e1f7de0cSMatt Gates 	BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
8823e1f7de0cSMatt Gates 
8824e1f7de0cSMatt Gates 	VERIFY_OFFSET(dev_handle, 0x00);
8825e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved1, 0x02);
8826e1f7de0cSMatt Gates 	VERIFY_OFFSET(function, 0x03);
8827e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved2, 0x04);
8828e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info, 0x0C);
8829e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved3, 0x10);
8830e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info_len, 0x12);
8831e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved4, 0x13);
8832e1f7de0cSMatt Gates 	VERIFY_OFFSET(sgl_offset, 0x14);
8833e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved5, 0x15);
8834e1f7de0cSMatt Gates 	VERIFY_OFFSET(transfer_len, 0x1C);
8835e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved6, 0x20);
8836e1f7de0cSMatt Gates 	VERIFY_OFFSET(io_flags, 0x24);
8837e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved7, 0x26);
8838e1f7de0cSMatt Gates 	VERIFY_OFFSET(LUN, 0x34);
8839e1f7de0cSMatt Gates 	VERIFY_OFFSET(control, 0x3C);
8840e1f7de0cSMatt Gates 	VERIFY_OFFSET(CDB, 0x40);
8841e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved8, 0x50);
8842e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_context_flags, 0x60);
8843e1f7de0cSMatt Gates 	VERIFY_OFFSET(timeout_sec, 0x62);
8844e1f7de0cSMatt Gates 	VERIFY_OFFSET(ReplyQueue, 0x64);
8845e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved9, 0x65);
884650a0decfSStephen M. Cameron 	VERIFY_OFFSET(tag, 0x68);
8847e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_addr, 0x70);
8848e1f7de0cSMatt Gates 	VERIFY_OFFSET(CISS_LUN, 0x78);
8849e1f7de0cSMatt Gates 	VERIFY_OFFSET(SG, 0x78 + 8);
8850e1f7de0cSMatt Gates #undef VERIFY_OFFSET
8851e1f7de0cSMatt Gates }
8852e1f7de0cSMatt Gates 
8853edd16368SStephen M. Cameron module_init(hpsa_init);
8854edd16368SStephen M. Cameron module_exit(hpsa_cleanup);
8855