xref: /openbmc/linux/drivers/scsi/hpsa.c (revision dd0e19f3ceb87a768d09ea4726ee33961665bfbb)
1edd16368SStephen M. Cameron /*
2edd16368SStephen M. Cameron  *    Disk Array driver for HP Smart Array SAS controllers
351c35139SScott Teel  *    Copyright 2000, 2014 Hewlett-Packard Development Company, L.P.
4edd16368SStephen M. Cameron  *
5edd16368SStephen M. Cameron  *    This program is free software; you can redistribute it and/or modify
6edd16368SStephen M. Cameron  *    it under the terms of the GNU General Public License as published by
7edd16368SStephen M. Cameron  *    the Free Software Foundation; version 2 of the License.
8edd16368SStephen M. Cameron  *
9edd16368SStephen M. Cameron  *    This program is distributed in the hope that it will be useful,
10edd16368SStephen M. Cameron  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
11edd16368SStephen M. Cameron  *    MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
12edd16368SStephen M. Cameron  *    NON INFRINGEMENT.  See the GNU General Public License for more details.
13edd16368SStephen M. Cameron  *
14edd16368SStephen M. Cameron  *    You should have received a copy of the GNU General Public License
15edd16368SStephen M. Cameron  *    along with this program; if not, write to the Free Software
16edd16368SStephen M. Cameron  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17edd16368SStephen M. Cameron  *
18edd16368SStephen M. Cameron  *    Questions/Comments/Bugfixes to iss_storagedev@hp.com
19edd16368SStephen M. Cameron  *
20edd16368SStephen M. Cameron  */
21edd16368SStephen M. Cameron 
22edd16368SStephen M. Cameron #include <linux/module.h>
23edd16368SStephen M. Cameron #include <linux/interrupt.h>
24edd16368SStephen M. Cameron #include <linux/types.h>
25edd16368SStephen M. Cameron #include <linux/pci.h>
26e5a44df8SMatthew Garrett #include <linux/pci-aspm.h>
27edd16368SStephen M. Cameron #include <linux/kernel.h>
28edd16368SStephen M. Cameron #include <linux/slab.h>
29edd16368SStephen M. Cameron #include <linux/delay.h>
30edd16368SStephen M. Cameron #include <linux/fs.h>
31edd16368SStephen M. Cameron #include <linux/timer.h>
32edd16368SStephen M. Cameron #include <linux/init.h>
33edd16368SStephen M. Cameron #include <linux/spinlock.h>
34edd16368SStephen M. Cameron #include <linux/compat.h>
35edd16368SStephen M. Cameron #include <linux/blktrace_api.h>
36edd16368SStephen M. Cameron #include <linux/uaccess.h>
37edd16368SStephen M. Cameron #include <linux/io.h>
38edd16368SStephen M. Cameron #include <linux/dma-mapping.h>
39edd16368SStephen M. Cameron #include <linux/completion.h>
40edd16368SStephen M. Cameron #include <linux/moduleparam.h>
41edd16368SStephen M. Cameron #include <scsi/scsi.h>
42edd16368SStephen M. Cameron #include <scsi/scsi_cmnd.h>
43edd16368SStephen M. Cameron #include <scsi/scsi_device.h>
44edd16368SStephen M. Cameron #include <scsi/scsi_host.h>
45667e23d4SStephen M. Cameron #include <scsi/scsi_tcq.h>
46edd16368SStephen M. Cameron #include <linux/cciss_ioctl.h>
47edd16368SStephen M. Cameron #include <linux/string.h>
48edd16368SStephen M. Cameron #include <linux/bitmap.h>
4960063497SArun Sharma #include <linux/atomic.h>
50edd16368SStephen M. Cameron #include <linux/kthread.h>
51a0c12413SStephen M. Cameron #include <linux/jiffies.h>
52283b4a9bSStephen M. Cameron #include <asm/div64.h>
53edd16368SStephen M. Cameron #include "hpsa_cmd.h"
54edd16368SStephen M. Cameron #include "hpsa.h"
55edd16368SStephen M. Cameron 
56edd16368SStephen M. Cameron /* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */
57e481cce8SMike Miller #define HPSA_DRIVER_VERSION "3.4.0-1"
58edd16368SStephen M. Cameron #define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
59f79cfec6SStephen M. Cameron #define HPSA "hpsa"
60edd16368SStephen M. Cameron 
61edd16368SStephen M. Cameron /* How long to wait (in milliseconds) for board to go into simple mode */
62edd16368SStephen M. Cameron #define MAX_CONFIG_WAIT 30000
63edd16368SStephen M. Cameron #define MAX_IOCTL_CONFIG_WAIT 1000
64edd16368SStephen M. Cameron 
65edd16368SStephen M. Cameron /*define how many times we will try a command because of bus resets */
66edd16368SStephen M. Cameron #define MAX_CMD_RETRIES 3
67edd16368SStephen M. Cameron 
68edd16368SStephen M. Cameron /* Embedded module documentation macros - see modules.h */
69edd16368SStephen M. Cameron MODULE_AUTHOR("Hewlett-Packard Company");
70edd16368SStephen M. Cameron MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
71edd16368SStephen M. Cameron 	HPSA_DRIVER_VERSION);
72edd16368SStephen M. Cameron MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
73edd16368SStephen M. Cameron MODULE_VERSION(HPSA_DRIVER_VERSION);
74edd16368SStephen M. Cameron MODULE_LICENSE("GPL");
75edd16368SStephen M. Cameron 
76edd16368SStephen M. Cameron static int hpsa_allow_any;
77edd16368SStephen M. Cameron module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
78edd16368SStephen M. Cameron MODULE_PARM_DESC(hpsa_allow_any,
79edd16368SStephen M. Cameron 		"Allow hpsa driver to access unknown HP Smart Array hardware");
8002ec19c8SStephen M. Cameron static int hpsa_simple_mode;
8102ec19c8SStephen M. Cameron module_param(hpsa_simple_mode, int, S_IRUGO|S_IWUSR);
8202ec19c8SStephen M. Cameron MODULE_PARM_DESC(hpsa_simple_mode,
8302ec19c8SStephen M. Cameron 	"Use 'simple mode' rather than 'performant mode'");
84edd16368SStephen M. Cameron 
85edd16368SStephen M. Cameron /* define the PCI info for the cards we can control */
86edd16368SStephen M. Cameron static const struct pci_device_id hpsa_pci_device_id[] = {
87edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3241},
88edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3243},
89edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3245},
90edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3247},
91edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3249},
92163dbcd8SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x324A},
93163dbcd8SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x324B},
94f8b01eb9SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3233},
959143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3350},
969143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3351},
979143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3352},
989143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3353},
999143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3354},
1009143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3355},
1019143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3356},
102fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1921},
103fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1922},
104fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1923},
105fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1924},
106fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1925},
107fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1926},
108fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1928},
10997b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1929},
11097b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BD},
11197b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BE},
11297b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BF},
11397b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C0},
11497b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C1},
11597b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C2},
11697b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C3},
11797b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C4},
11897b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C5},
11997b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C7},
12097b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C8},
12197b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C9},
122edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_ANY_ID,	PCI_ANY_ID, PCI_ANY_ID,
123edd16368SStephen M. Cameron 		PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
124edd16368SStephen M. Cameron 	{0,}
125edd16368SStephen M. Cameron };
126edd16368SStephen M. Cameron 
127edd16368SStephen M. Cameron MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
128edd16368SStephen M. Cameron 
129edd16368SStephen M. Cameron /*  board_id = Subsystem Device ID & Vendor ID
130edd16368SStephen M. Cameron  *  product = Marketing Name for the board
131edd16368SStephen M. Cameron  *  access = Address of the struct of function pointers
132edd16368SStephen M. Cameron  */
133edd16368SStephen M. Cameron static struct board_type products[] = {
134edd16368SStephen M. Cameron 	{0x3241103C, "Smart Array P212", &SA5_access},
135edd16368SStephen M. Cameron 	{0x3243103C, "Smart Array P410", &SA5_access},
136edd16368SStephen M. Cameron 	{0x3245103C, "Smart Array P410i", &SA5_access},
137edd16368SStephen M. Cameron 	{0x3247103C, "Smart Array P411", &SA5_access},
138edd16368SStephen M. Cameron 	{0x3249103C, "Smart Array P812", &SA5_access},
139163dbcd8SMike Miller 	{0x324A103C, "Smart Array P712m", &SA5_access},
140163dbcd8SMike Miller 	{0x324B103C, "Smart Array P711m", &SA5_access},
141fe0c9610SMike Miller 	{0x3350103C, "Smart Array P222", &SA5_access},
142fe0c9610SMike Miller 	{0x3351103C, "Smart Array P420", &SA5_access},
143fe0c9610SMike Miller 	{0x3352103C, "Smart Array P421", &SA5_access},
144fe0c9610SMike Miller 	{0x3353103C, "Smart Array P822", &SA5_access},
145fe0c9610SMike Miller 	{0x3354103C, "Smart Array P420i", &SA5_access},
146fe0c9610SMike Miller 	{0x3355103C, "Smart Array P220i", &SA5_access},
147fe0c9610SMike Miller 	{0x3356103C, "Smart Array P721m", &SA5_access},
1481fd6c8e3SMike Miller 	{0x1921103C, "Smart Array P830i", &SA5_access},
1491fd6c8e3SMike Miller 	{0x1922103C, "Smart Array P430", &SA5_access},
1501fd6c8e3SMike Miller 	{0x1923103C, "Smart Array P431", &SA5_access},
1511fd6c8e3SMike Miller 	{0x1924103C, "Smart Array P830", &SA5_access},
1521fd6c8e3SMike Miller 	{0x1926103C, "Smart Array P731m", &SA5_access},
1531fd6c8e3SMike Miller 	{0x1928103C, "Smart Array P230i", &SA5_access},
1541fd6c8e3SMike Miller 	{0x1929103C, "Smart Array P530", &SA5_access},
15597b9f53dSMike Miller 	{0x21BD103C, "Smart Array", &SA5_access},
15697b9f53dSMike Miller 	{0x21BE103C, "Smart Array", &SA5_access},
15797b9f53dSMike Miller 	{0x21BF103C, "Smart Array", &SA5_access},
15897b9f53dSMike Miller 	{0x21C0103C, "Smart Array", &SA5_access},
15997b9f53dSMike Miller 	{0x21C1103C, "Smart Array", &SA5_access},
16097b9f53dSMike Miller 	{0x21C2103C, "Smart Array", &SA5_access},
16197b9f53dSMike Miller 	{0x21C3103C, "Smart Array", &SA5_access},
16297b9f53dSMike Miller 	{0x21C4103C, "Smart Array", &SA5_access},
16397b9f53dSMike Miller 	{0x21C5103C, "Smart Array", &SA5_access},
16497b9f53dSMike Miller 	{0x21C7103C, "Smart Array", &SA5_access},
16597b9f53dSMike Miller 	{0x21C8103C, "Smart Array", &SA5_access},
16697b9f53dSMike Miller 	{0x21C9103C, "Smart Array", &SA5_access},
167edd16368SStephen M. Cameron 	{0xFFFF103C, "Unknown Smart Array", &SA5_access},
168edd16368SStephen M. Cameron };
169edd16368SStephen M. Cameron 
170edd16368SStephen M. Cameron static int number_of_controllers;
171edd16368SStephen M. Cameron 
17210f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id);
17310f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id);
174edd16368SStephen M. Cameron static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg);
175edd16368SStephen M. Cameron static void start_io(struct ctlr_info *h);
176edd16368SStephen M. Cameron 
177edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
178edd16368SStephen M. Cameron static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg);
179edd16368SStephen M. Cameron #endif
180edd16368SStephen M. Cameron 
181edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c);
182edd16368SStephen M. Cameron static void cmd_special_free(struct ctlr_info *h, struct CommandList *c);
183edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h);
184edd16368SStephen M. Cameron static struct CommandList *cmd_special_alloc(struct ctlr_info *h);
185a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
186b7bb24ebSStephen M. Cameron 	void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
187edd16368SStephen M. Cameron 	int cmd_type);
188b7bb24ebSStephen M. Cameron #define VPD_PAGE (1 << 8)
189edd16368SStephen M. Cameron 
190f281233dSJeff Garzik static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
191a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *);
192a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
193a08a8471SStephen M. Cameron 	unsigned long elapsed_time);
194667e23d4SStephen M. Cameron static int hpsa_change_queue_depth(struct scsi_device *sdev,
195667e23d4SStephen M. Cameron 	int qdepth, int reason);
196edd16368SStephen M. Cameron 
197edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
19875167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *scsicmd);
199edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev);
200edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev);
201edd16368SStephen M. Cameron 
202edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
203edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
204edd16368SStephen M. Cameron 	struct CommandList *c);
205edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
206edd16368SStephen M. Cameron 	struct CommandList *c);
207303932fdSDon Brace /* performant mode helper functions */
208303932fdSDon Brace static void calc_bucket_map(int *bucket, int num_buckets,
209e1f7de0cSMatt Gates 	int nsgs, int min_blocks, int *bucket_map);
2106f039790SGreg Kroah-Hartman static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
211254f796bSMatt Gates static inline u32 next_command(struct ctlr_info *h, u8 q);
2126f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
2136f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
2141df8552aSStephen M. Cameron 			       u64 *cfg_offset);
2156f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
2161df8552aSStephen M. Cameron 				    unsigned long *memory_bar);
2176f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
2186f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
2196f039790SGreg Kroah-Hartman 				     int wait_for_ready);
22075167d2cSStephen M. Cameron static inline void finish_cmd(struct CommandList *c);
221283b4a9bSStephen M. Cameron static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h);
222fe5389c8SStephen M. Cameron #define BOARD_NOT_READY 0
223fe5389c8SStephen M. Cameron #define BOARD_READY 1
22476438d08SStephen M. Cameron static void hpsa_drain_commands(struct ctlr_info *h);
22576438d08SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h);
226c349775eSScott Teel static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
227c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
228c349775eSScott Teel 	u8 *scsi3addr);
229edd16368SStephen M. Cameron 
230edd16368SStephen M. Cameron static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
231edd16368SStephen M. Cameron {
232edd16368SStephen M. Cameron 	unsigned long *priv = shost_priv(sdev->host);
233edd16368SStephen M. Cameron 	return (struct ctlr_info *) *priv;
234edd16368SStephen M. Cameron }
235edd16368SStephen M. Cameron 
236a23513e8SStephen M. Cameron static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
237a23513e8SStephen M. Cameron {
238a23513e8SStephen M. Cameron 	unsigned long *priv = shost_priv(sh);
239a23513e8SStephen M. Cameron 	return (struct ctlr_info *) *priv;
240a23513e8SStephen M. Cameron }
241a23513e8SStephen M. Cameron 
242edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
243edd16368SStephen M. Cameron 	struct CommandList *c)
244edd16368SStephen M. Cameron {
245edd16368SStephen M. Cameron 	if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
246edd16368SStephen M. Cameron 		return 0;
247edd16368SStephen M. Cameron 
248edd16368SStephen M. Cameron 	switch (c->err_info->SenseInfo[12]) {
249edd16368SStephen M. Cameron 	case STATE_CHANGED:
250f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: a state change "
251edd16368SStephen M. Cameron 			"detected, command retried\n", h->ctlr);
252edd16368SStephen M. Cameron 		break;
253edd16368SStephen M. Cameron 	case LUN_FAILED:
254f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: LUN failure "
255edd16368SStephen M. Cameron 			"detected, action required\n", h->ctlr);
256edd16368SStephen M. Cameron 		break;
257edd16368SStephen M. Cameron 	case REPORT_LUNS_CHANGED:
258f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: report LUN data "
25931468401SMike Miller 			"changed, action required\n", h->ctlr);
260edd16368SStephen M. Cameron 	/*
2614f4eb9f1SScott Teel 	 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
2624f4eb9f1SScott Teel 	 * target (array) devices.
263edd16368SStephen M. Cameron 	 */
264edd16368SStephen M. Cameron 		break;
265edd16368SStephen M. Cameron 	case POWER_OR_RESET:
266f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: a power on "
267edd16368SStephen M. Cameron 			"or device reset detected\n", h->ctlr);
268edd16368SStephen M. Cameron 		break;
269edd16368SStephen M. Cameron 	case UNIT_ATTENTION_CLEARED:
270f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: unit attention "
271edd16368SStephen M. Cameron 		    "cleared by another initiator\n", h->ctlr);
272edd16368SStephen M. Cameron 		break;
273edd16368SStephen M. Cameron 	default:
274f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: unknown "
275edd16368SStephen M. Cameron 			"unit attention detected\n", h->ctlr);
276edd16368SStephen M. Cameron 		break;
277edd16368SStephen M. Cameron 	}
278edd16368SStephen M. Cameron 	return 1;
279edd16368SStephen M. Cameron }
280edd16368SStephen M. Cameron 
281852af20aSMatt Bondurant static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
282852af20aSMatt Bondurant {
283852af20aSMatt Bondurant 	if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
284852af20aSMatt Bondurant 		(c->err_info->ScsiStatus != SAM_STAT_BUSY &&
285852af20aSMatt Bondurant 		 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
286852af20aSMatt Bondurant 		return 0;
287852af20aSMatt Bondurant 	dev_warn(&h->pdev->dev, HPSA "device busy");
288852af20aSMatt Bondurant 	return 1;
289852af20aSMatt Bondurant }
290852af20aSMatt Bondurant 
291da0697bdSScott Teel static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
292da0697bdSScott Teel 					 struct device_attribute *attr,
293da0697bdSScott Teel 					 const char *buf, size_t count)
294da0697bdSScott Teel {
295da0697bdSScott Teel 	int status, len;
296da0697bdSScott Teel 	struct ctlr_info *h;
297da0697bdSScott Teel 	struct Scsi_Host *shost = class_to_shost(dev);
298da0697bdSScott Teel 	char tmpbuf[10];
299da0697bdSScott Teel 
300da0697bdSScott Teel 	if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
301da0697bdSScott Teel 		return -EACCES;
302da0697bdSScott Teel 	len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
303da0697bdSScott Teel 	strncpy(tmpbuf, buf, len);
304da0697bdSScott Teel 	tmpbuf[len] = '\0';
305da0697bdSScott Teel 	if (sscanf(tmpbuf, "%d", &status) != 1)
306da0697bdSScott Teel 		return -EINVAL;
307da0697bdSScott Teel 	h = shost_to_hba(shost);
308da0697bdSScott Teel 	h->acciopath_status = !!status;
309da0697bdSScott Teel 	dev_warn(&h->pdev->dev,
310da0697bdSScott Teel 		"hpsa: HP SSD Smart Path %s via sysfs update.\n",
311da0697bdSScott Teel 		h->acciopath_status ? "enabled" : "disabled");
312da0697bdSScott Teel 	return count;
313da0697bdSScott Teel }
314da0697bdSScott Teel 
315edd16368SStephen M. Cameron static ssize_t host_store_rescan(struct device *dev,
316edd16368SStephen M. Cameron 				 struct device_attribute *attr,
317edd16368SStephen M. Cameron 				 const char *buf, size_t count)
318edd16368SStephen M. Cameron {
319edd16368SStephen M. Cameron 	struct ctlr_info *h;
320edd16368SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
321a23513e8SStephen M. Cameron 	h = shost_to_hba(shost);
32231468401SMike Miller 	hpsa_scan_start(h->scsi_host);
323edd16368SStephen M. Cameron 	return count;
324edd16368SStephen M. Cameron }
325edd16368SStephen M. Cameron 
326d28ce020SStephen M. Cameron static ssize_t host_show_firmware_revision(struct device *dev,
327d28ce020SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
328d28ce020SStephen M. Cameron {
329d28ce020SStephen M. Cameron 	struct ctlr_info *h;
330d28ce020SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
331d28ce020SStephen M. Cameron 	unsigned char *fwrev;
332d28ce020SStephen M. Cameron 
333d28ce020SStephen M. Cameron 	h = shost_to_hba(shost);
334d28ce020SStephen M. Cameron 	if (!h->hba_inquiry_data)
335d28ce020SStephen M. Cameron 		return 0;
336d28ce020SStephen M. Cameron 	fwrev = &h->hba_inquiry_data[32];
337d28ce020SStephen M. Cameron 	return snprintf(buf, 20, "%c%c%c%c\n",
338d28ce020SStephen M. Cameron 		fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
339d28ce020SStephen M. Cameron }
340d28ce020SStephen M. Cameron 
34194a13649SStephen M. Cameron static ssize_t host_show_commands_outstanding(struct device *dev,
34294a13649SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
34394a13649SStephen M. Cameron {
34494a13649SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
34594a13649SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(shost);
34694a13649SStephen M. Cameron 
34794a13649SStephen M. Cameron 	return snprintf(buf, 20, "%d\n", h->commands_outstanding);
34894a13649SStephen M. Cameron }
34994a13649SStephen M. Cameron 
350745a7a25SStephen M. Cameron static ssize_t host_show_transport_mode(struct device *dev,
351745a7a25SStephen M. Cameron 	struct device_attribute *attr, char *buf)
352745a7a25SStephen M. Cameron {
353745a7a25SStephen M. Cameron 	struct ctlr_info *h;
354745a7a25SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
355745a7a25SStephen M. Cameron 
356745a7a25SStephen M. Cameron 	h = shost_to_hba(shost);
357745a7a25SStephen M. Cameron 	return snprintf(buf, 20, "%s\n",
358960a30e7SStephen M. Cameron 		h->transMethod & CFGTBL_Trans_Performant ?
359745a7a25SStephen M. Cameron 			"performant" : "simple");
360745a7a25SStephen M. Cameron }
361745a7a25SStephen M. Cameron 
362da0697bdSScott Teel static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
363da0697bdSScott Teel 	struct device_attribute *attr, char *buf)
364da0697bdSScott Teel {
365da0697bdSScott Teel 	struct ctlr_info *h;
366da0697bdSScott Teel 	struct Scsi_Host *shost = class_to_shost(dev);
367da0697bdSScott Teel 
368da0697bdSScott Teel 	h = shost_to_hba(shost);
369da0697bdSScott Teel 	return snprintf(buf, 30, "HP SSD Smart Path %s\n",
370da0697bdSScott Teel 		(h->acciopath_status == 1) ?  "enabled" : "disabled");
371da0697bdSScott Teel }
372da0697bdSScott Teel 
37346380786SStephen M. Cameron /* List of controllers which cannot be hard reset on kexec with reset_devices */
374941b1cdaSStephen M. Cameron static u32 unresettable_controller[] = {
375941b1cdaSStephen M. Cameron 	0x324a103C, /* Smart Array P712m */
376941b1cdaSStephen M. Cameron 	0x324b103C, /* SmartArray P711m */
377941b1cdaSStephen M. Cameron 	0x3223103C, /* Smart Array P800 */
378941b1cdaSStephen M. Cameron 	0x3234103C, /* Smart Array P400 */
379941b1cdaSStephen M. Cameron 	0x3235103C, /* Smart Array P400i */
380941b1cdaSStephen M. Cameron 	0x3211103C, /* Smart Array E200i */
381941b1cdaSStephen M. Cameron 	0x3212103C, /* Smart Array E200 */
382941b1cdaSStephen M. Cameron 	0x3213103C, /* Smart Array E200i */
383941b1cdaSStephen M. Cameron 	0x3214103C, /* Smart Array E200i */
384941b1cdaSStephen M. Cameron 	0x3215103C, /* Smart Array E200i */
385941b1cdaSStephen M. Cameron 	0x3237103C, /* Smart Array E500 */
386941b1cdaSStephen M. Cameron 	0x323D103C, /* Smart Array P700m */
3877af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
388941b1cdaSStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
389941b1cdaSStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
3905a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
3915a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
3925a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
3935a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
3945a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
3955a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
396941b1cdaSStephen M. Cameron };
397941b1cdaSStephen M. Cameron 
39846380786SStephen M. Cameron /* List of controllers which cannot even be soft reset */
39946380786SStephen M. Cameron static u32 soft_unresettable_controller[] = {
4007af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
4015a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
4025a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
4035a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
4045a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
4055a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
4065a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
40746380786SStephen M. Cameron 	/* Exclude 640x boards.  These are two pci devices in one slot
40846380786SStephen M. Cameron 	 * which share a battery backed cache module.  One controls the
40946380786SStephen M. Cameron 	 * cache, the other accesses the cache through the one that controls
41046380786SStephen M. Cameron 	 * it.  If we reset the one controlling the cache, the other will
41146380786SStephen M. Cameron 	 * likely not be happy.  Just forbid resetting this conjoined mess.
41246380786SStephen M. Cameron 	 * The 640x isn't really supported by hpsa anyway.
41346380786SStephen M. Cameron 	 */
41446380786SStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
41546380786SStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
41646380786SStephen M. Cameron };
41746380786SStephen M. Cameron 
41846380786SStephen M. Cameron static int ctlr_is_hard_resettable(u32 board_id)
419941b1cdaSStephen M. Cameron {
420941b1cdaSStephen M. Cameron 	int i;
421941b1cdaSStephen M. Cameron 
422941b1cdaSStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(unresettable_controller); i++)
42346380786SStephen M. Cameron 		if (unresettable_controller[i] == board_id)
424941b1cdaSStephen M. Cameron 			return 0;
425941b1cdaSStephen M. Cameron 	return 1;
426941b1cdaSStephen M. Cameron }
427941b1cdaSStephen M. Cameron 
42846380786SStephen M. Cameron static int ctlr_is_soft_resettable(u32 board_id)
42946380786SStephen M. Cameron {
43046380786SStephen M. Cameron 	int i;
43146380786SStephen M. Cameron 
43246380786SStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(soft_unresettable_controller); i++)
43346380786SStephen M. Cameron 		if (soft_unresettable_controller[i] == board_id)
43446380786SStephen M. Cameron 			return 0;
43546380786SStephen M. Cameron 	return 1;
43646380786SStephen M. Cameron }
43746380786SStephen M. Cameron 
43846380786SStephen M. Cameron static int ctlr_is_resettable(u32 board_id)
43946380786SStephen M. Cameron {
44046380786SStephen M. Cameron 	return ctlr_is_hard_resettable(board_id) ||
44146380786SStephen M. Cameron 		ctlr_is_soft_resettable(board_id);
44246380786SStephen M. Cameron }
44346380786SStephen M. Cameron 
444941b1cdaSStephen M. Cameron static ssize_t host_show_resettable(struct device *dev,
445941b1cdaSStephen M. Cameron 	struct device_attribute *attr, char *buf)
446941b1cdaSStephen M. Cameron {
447941b1cdaSStephen M. Cameron 	struct ctlr_info *h;
448941b1cdaSStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
449941b1cdaSStephen M. Cameron 
450941b1cdaSStephen M. Cameron 	h = shost_to_hba(shost);
45146380786SStephen M. Cameron 	return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
452941b1cdaSStephen M. Cameron }
453941b1cdaSStephen M. Cameron 
454edd16368SStephen M. Cameron static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
455edd16368SStephen M. Cameron {
456edd16368SStephen M. Cameron 	return (scsi3addr[3] & 0xC0) == 0x40;
457edd16368SStephen M. Cameron }
458edd16368SStephen M. Cameron 
459edd16368SStephen M. Cameron static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
460d82357eaSMike Miller 	"1(ADM)", "UNKNOWN"
461edd16368SStephen M. Cameron };
4626b80b18fSScott Teel #define HPSA_RAID_0	0
4636b80b18fSScott Teel #define HPSA_RAID_4	1
4646b80b18fSScott Teel #define HPSA_RAID_1	2	/* also used for RAID 10 */
4656b80b18fSScott Teel #define HPSA_RAID_5	3	/* also used for RAID 50 */
4666b80b18fSScott Teel #define HPSA_RAID_51	4
4676b80b18fSScott Teel #define HPSA_RAID_6	5	/* also used for RAID 60 */
4686b80b18fSScott Teel #define HPSA_RAID_ADM	6	/* also used for RAID 1+0 ADM */
469edd16368SStephen M. Cameron #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
470edd16368SStephen M. Cameron 
471edd16368SStephen M. Cameron static ssize_t raid_level_show(struct device *dev,
472edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
473edd16368SStephen M. Cameron {
474edd16368SStephen M. Cameron 	ssize_t l = 0;
47582a72c0aSStephen M. Cameron 	unsigned char rlevel;
476edd16368SStephen M. Cameron 	struct ctlr_info *h;
477edd16368SStephen M. Cameron 	struct scsi_device *sdev;
478edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
479edd16368SStephen M. Cameron 	unsigned long flags;
480edd16368SStephen M. Cameron 
481edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
482edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
483edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
484edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
485edd16368SStephen M. Cameron 	if (!hdev) {
486edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
487edd16368SStephen M. Cameron 		return -ENODEV;
488edd16368SStephen M. Cameron 	}
489edd16368SStephen M. Cameron 
490edd16368SStephen M. Cameron 	/* Is this even a logical drive? */
491edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
492edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
493edd16368SStephen M. Cameron 		l = snprintf(buf, PAGE_SIZE, "N/A\n");
494edd16368SStephen M. Cameron 		return l;
495edd16368SStephen M. Cameron 	}
496edd16368SStephen M. Cameron 
497edd16368SStephen M. Cameron 	rlevel = hdev->raid_level;
498edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
49982a72c0aSStephen M. Cameron 	if (rlevel > RAID_UNKNOWN)
500edd16368SStephen M. Cameron 		rlevel = RAID_UNKNOWN;
501edd16368SStephen M. Cameron 	l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
502edd16368SStephen M. Cameron 	return l;
503edd16368SStephen M. Cameron }
504edd16368SStephen M. Cameron 
505edd16368SStephen M. Cameron static ssize_t lunid_show(struct device *dev,
506edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
507edd16368SStephen M. Cameron {
508edd16368SStephen M. Cameron 	struct ctlr_info *h;
509edd16368SStephen M. Cameron 	struct scsi_device *sdev;
510edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
511edd16368SStephen M. Cameron 	unsigned long flags;
512edd16368SStephen M. Cameron 	unsigned char lunid[8];
513edd16368SStephen M. Cameron 
514edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
515edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
516edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
517edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
518edd16368SStephen M. Cameron 	if (!hdev) {
519edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
520edd16368SStephen M. Cameron 		return -ENODEV;
521edd16368SStephen M. Cameron 	}
522edd16368SStephen M. Cameron 	memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
523edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
524edd16368SStephen M. Cameron 	return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
525edd16368SStephen M. Cameron 		lunid[0], lunid[1], lunid[2], lunid[3],
526edd16368SStephen M. Cameron 		lunid[4], lunid[5], lunid[6], lunid[7]);
527edd16368SStephen M. Cameron }
528edd16368SStephen M. Cameron 
529edd16368SStephen M. Cameron static ssize_t unique_id_show(struct device *dev,
530edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
531edd16368SStephen M. Cameron {
532edd16368SStephen M. Cameron 	struct ctlr_info *h;
533edd16368SStephen M. Cameron 	struct scsi_device *sdev;
534edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
535edd16368SStephen M. Cameron 	unsigned long flags;
536edd16368SStephen M. Cameron 	unsigned char sn[16];
537edd16368SStephen M. Cameron 
538edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
539edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
540edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
541edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
542edd16368SStephen M. Cameron 	if (!hdev) {
543edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
544edd16368SStephen M. Cameron 		return -ENODEV;
545edd16368SStephen M. Cameron 	}
546edd16368SStephen M. Cameron 	memcpy(sn, hdev->device_id, sizeof(sn));
547edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
548edd16368SStephen M. Cameron 	return snprintf(buf, 16 * 2 + 2,
549edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X"
550edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X\n",
551edd16368SStephen M. Cameron 			sn[0], sn[1], sn[2], sn[3],
552edd16368SStephen M. Cameron 			sn[4], sn[5], sn[6], sn[7],
553edd16368SStephen M. Cameron 			sn[8], sn[9], sn[10], sn[11],
554edd16368SStephen M. Cameron 			sn[12], sn[13], sn[14], sn[15]);
555edd16368SStephen M. Cameron }
556edd16368SStephen M. Cameron 
557c1988684SScott Teel static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
558c1988684SScott Teel 	     struct device_attribute *attr, char *buf)
559c1988684SScott Teel {
560c1988684SScott Teel 	struct ctlr_info *h;
561c1988684SScott Teel 	struct scsi_device *sdev;
562c1988684SScott Teel 	struct hpsa_scsi_dev_t *hdev;
563c1988684SScott Teel 	unsigned long flags;
564c1988684SScott Teel 	int offload_enabled;
565c1988684SScott Teel 
566c1988684SScott Teel 	sdev = to_scsi_device(dev);
567c1988684SScott Teel 	h = sdev_to_hba(sdev);
568c1988684SScott Teel 	spin_lock_irqsave(&h->lock, flags);
569c1988684SScott Teel 	hdev = sdev->hostdata;
570c1988684SScott Teel 	if (!hdev) {
571c1988684SScott Teel 		spin_unlock_irqrestore(&h->lock, flags);
572c1988684SScott Teel 		return -ENODEV;
573c1988684SScott Teel 	}
574c1988684SScott Teel 	offload_enabled = hdev->offload_enabled;
575c1988684SScott Teel 	spin_unlock_irqrestore(&h->lock, flags);
576c1988684SScott Teel 	return snprintf(buf, 20, "%d\n", offload_enabled);
577c1988684SScott Teel }
578c1988684SScott Teel 
5793f5eac3aSStephen M. Cameron static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
5803f5eac3aSStephen M. Cameron static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
5813f5eac3aSStephen M. Cameron static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
5823f5eac3aSStephen M. Cameron static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
583c1988684SScott Teel static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
584c1988684SScott Teel 			host_show_hp_ssd_smart_path_enabled, NULL);
585da0697bdSScott Teel static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
586da0697bdSScott Teel 		host_show_hp_ssd_smart_path_status,
587da0697bdSScott Teel 		host_store_hp_ssd_smart_path_status);
5883f5eac3aSStephen M. Cameron static DEVICE_ATTR(firmware_revision, S_IRUGO,
5893f5eac3aSStephen M. Cameron 	host_show_firmware_revision, NULL);
5903f5eac3aSStephen M. Cameron static DEVICE_ATTR(commands_outstanding, S_IRUGO,
5913f5eac3aSStephen M. Cameron 	host_show_commands_outstanding, NULL);
5923f5eac3aSStephen M. Cameron static DEVICE_ATTR(transport_mode, S_IRUGO,
5933f5eac3aSStephen M. Cameron 	host_show_transport_mode, NULL);
594941b1cdaSStephen M. Cameron static DEVICE_ATTR(resettable, S_IRUGO,
595941b1cdaSStephen M. Cameron 	host_show_resettable, NULL);
5963f5eac3aSStephen M. Cameron 
5973f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_sdev_attrs[] = {
5983f5eac3aSStephen M. Cameron 	&dev_attr_raid_level,
5993f5eac3aSStephen M. Cameron 	&dev_attr_lunid,
6003f5eac3aSStephen M. Cameron 	&dev_attr_unique_id,
601c1988684SScott Teel 	&dev_attr_hp_ssd_smart_path_enabled,
6023f5eac3aSStephen M. Cameron 	NULL,
6033f5eac3aSStephen M. Cameron };
6043f5eac3aSStephen M. Cameron 
6053f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_shost_attrs[] = {
6063f5eac3aSStephen M. Cameron 	&dev_attr_rescan,
6073f5eac3aSStephen M. Cameron 	&dev_attr_firmware_revision,
6083f5eac3aSStephen M. Cameron 	&dev_attr_commands_outstanding,
6093f5eac3aSStephen M. Cameron 	&dev_attr_transport_mode,
610941b1cdaSStephen M. Cameron 	&dev_attr_resettable,
611da0697bdSScott Teel 	&dev_attr_hp_ssd_smart_path_status,
6123f5eac3aSStephen M. Cameron 	NULL,
6133f5eac3aSStephen M. Cameron };
6143f5eac3aSStephen M. Cameron 
6153f5eac3aSStephen M. Cameron static struct scsi_host_template hpsa_driver_template = {
6163f5eac3aSStephen M. Cameron 	.module			= THIS_MODULE,
617f79cfec6SStephen M. Cameron 	.name			= HPSA,
618f79cfec6SStephen M. Cameron 	.proc_name		= HPSA,
6193f5eac3aSStephen M. Cameron 	.queuecommand		= hpsa_scsi_queue_command,
6203f5eac3aSStephen M. Cameron 	.scan_start		= hpsa_scan_start,
6213f5eac3aSStephen M. Cameron 	.scan_finished		= hpsa_scan_finished,
6223f5eac3aSStephen M. Cameron 	.change_queue_depth	= hpsa_change_queue_depth,
6233f5eac3aSStephen M. Cameron 	.this_id		= -1,
6243f5eac3aSStephen M. Cameron 	.use_clustering		= ENABLE_CLUSTERING,
62575167d2cSStephen M. Cameron 	.eh_abort_handler	= hpsa_eh_abort_handler,
6263f5eac3aSStephen M. Cameron 	.eh_device_reset_handler = hpsa_eh_device_reset_handler,
6273f5eac3aSStephen M. Cameron 	.ioctl			= hpsa_ioctl,
6283f5eac3aSStephen M. Cameron 	.slave_alloc		= hpsa_slave_alloc,
6293f5eac3aSStephen M. Cameron 	.slave_destroy		= hpsa_slave_destroy,
6303f5eac3aSStephen M. Cameron #ifdef CONFIG_COMPAT
6313f5eac3aSStephen M. Cameron 	.compat_ioctl		= hpsa_compat_ioctl,
6323f5eac3aSStephen M. Cameron #endif
6333f5eac3aSStephen M. Cameron 	.sdev_attrs = hpsa_sdev_attrs,
6343f5eac3aSStephen M. Cameron 	.shost_attrs = hpsa_shost_attrs,
635c0d6a4d1SStephen M. Cameron 	.max_sectors = 8192,
63654b2b50cSMartin K. Petersen 	.no_write_same = 1,
6373f5eac3aSStephen M. Cameron };
6383f5eac3aSStephen M. Cameron 
6393f5eac3aSStephen M. Cameron 
6403f5eac3aSStephen M. Cameron /* Enqueuing and dequeuing functions for cmdlists. */
6413f5eac3aSStephen M. Cameron static inline void addQ(struct list_head *list, struct CommandList *c)
6423f5eac3aSStephen M. Cameron {
6433f5eac3aSStephen M. Cameron 	list_add_tail(&c->list, list);
6443f5eac3aSStephen M. Cameron }
6453f5eac3aSStephen M. Cameron 
646254f796bSMatt Gates static inline u32 next_command(struct ctlr_info *h, u8 q)
6473f5eac3aSStephen M. Cameron {
6483f5eac3aSStephen M. Cameron 	u32 a;
649254f796bSMatt Gates 	struct reply_pool *rq = &h->reply_queue[q];
650e16a33adSMatt Gates 	unsigned long flags;
6513f5eac3aSStephen M. Cameron 
652e1f7de0cSMatt Gates 	if (h->transMethod & CFGTBL_Trans_io_accel1)
653e1f7de0cSMatt Gates 		return h->access.command_completed(h, q);
654e1f7de0cSMatt Gates 
6553f5eac3aSStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
656254f796bSMatt Gates 		return h->access.command_completed(h, q);
6573f5eac3aSStephen M. Cameron 
658254f796bSMatt Gates 	if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
659254f796bSMatt Gates 		a = rq->head[rq->current_entry];
660254f796bSMatt Gates 		rq->current_entry++;
661e16a33adSMatt Gates 		spin_lock_irqsave(&h->lock, flags);
6623f5eac3aSStephen M. Cameron 		h->commands_outstanding--;
663e16a33adSMatt Gates 		spin_unlock_irqrestore(&h->lock, flags);
6643f5eac3aSStephen M. Cameron 	} else {
6653f5eac3aSStephen M. Cameron 		a = FIFO_EMPTY;
6663f5eac3aSStephen M. Cameron 	}
6673f5eac3aSStephen M. Cameron 	/* Check for wraparound */
668254f796bSMatt Gates 	if (rq->current_entry == h->max_commands) {
669254f796bSMatt Gates 		rq->current_entry = 0;
670254f796bSMatt Gates 		rq->wraparound ^= 1;
6713f5eac3aSStephen M. Cameron 	}
6723f5eac3aSStephen M. Cameron 	return a;
6733f5eac3aSStephen M. Cameron }
6743f5eac3aSStephen M. Cameron 
675c349775eSScott Teel /*
676c349775eSScott Teel  * There are some special bits in the bus address of the
677c349775eSScott Teel  * command that we have to set for the controller to know
678c349775eSScott Teel  * how to process the command:
679c349775eSScott Teel  *
680c349775eSScott Teel  * Normal performant mode:
681c349775eSScott Teel  * bit 0: 1 means performant mode, 0 means simple mode.
682c349775eSScott Teel  * bits 1-3 = block fetch table entry
683c349775eSScott Teel  * bits 4-6 = command type (== 0)
684c349775eSScott Teel  *
685c349775eSScott Teel  * ioaccel1 mode:
686c349775eSScott Teel  * bit 0 = "performant mode" bit.
687c349775eSScott Teel  * bits 1-3 = block fetch table entry
688c349775eSScott Teel  * bits 4-6 = command type (== 110)
689c349775eSScott Teel  * (command type is needed because ioaccel1 mode
690c349775eSScott Teel  * commands are submitted through the same register as normal
691c349775eSScott Teel  * mode commands, so this is how the controller knows whether
692c349775eSScott Teel  * the command is normal mode or ioaccel1 mode.)
693c349775eSScott Teel  *
694c349775eSScott Teel  * ioaccel2 mode:
695c349775eSScott Teel  * bit 0 = "performant mode" bit.
696c349775eSScott Teel  * bits 1-4 = block fetch table entry (note extra bit)
697c349775eSScott Teel  * bits 4-6 = not needed, because ioaccel2 mode has
698c349775eSScott Teel  * a separate special register for submitting commands.
699c349775eSScott Teel  */
700c349775eSScott Teel 
7013f5eac3aSStephen M. Cameron /* set_performant_mode: Modify the tag for cciss performant
7023f5eac3aSStephen M. Cameron  * set bit 0 for pull model, bits 3-1 for block fetch
7033f5eac3aSStephen M. Cameron  * register number
7043f5eac3aSStephen M. Cameron  */
7053f5eac3aSStephen M. Cameron static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
7063f5eac3aSStephen M. Cameron {
707254f796bSMatt Gates 	if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
7083f5eac3aSStephen M. Cameron 		c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
709eee0f03aSHannes Reinecke 		if (likely(h->msix_vector > 0))
710254f796bSMatt Gates 			c->Header.ReplyQueue =
711804a5cb5SJohn Kacur 				raw_smp_processor_id() % h->nreply_queues;
712254f796bSMatt Gates 	}
7133f5eac3aSStephen M. Cameron }
7143f5eac3aSStephen M. Cameron 
715c349775eSScott Teel static void set_ioaccel1_performant_mode(struct ctlr_info *h,
716c349775eSScott Teel 						struct CommandList *c)
717c349775eSScott Teel {
718c349775eSScott Teel 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
719c349775eSScott Teel 
720c349775eSScott Teel 	/* Tell the controller to post the reply to the queue for this
721c349775eSScott Teel 	 * processor.  This seems to give the best I/O throughput.
722c349775eSScott Teel 	 */
723c349775eSScott Teel 	cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
724c349775eSScott Teel 	/* Set the bits in the address sent down to include:
725c349775eSScott Teel 	 *  - performant mode bit (bit 0)
726c349775eSScott Teel 	 *  - pull count (bits 1-3)
727c349775eSScott Teel 	 *  - command type (bits 4-6)
728c349775eSScott Teel 	 */
729c349775eSScott Teel 	c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
730c349775eSScott Teel 					IOACCEL1_BUSADDR_CMDTYPE;
731c349775eSScott Teel }
732c349775eSScott Teel 
733c349775eSScott Teel static void set_ioaccel2_performant_mode(struct ctlr_info *h,
734c349775eSScott Teel 						struct CommandList *c)
735c349775eSScott Teel {
736c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
737c349775eSScott Teel 
738c349775eSScott Teel 	/* Tell the controller to post the reply to the queue for this
739c349775eSScott Teel 	 * processor.  This seems to give the best I/O throughput.
740c349775eSScott Teel 	 */
741c349775eSScott Teel 	cp->reply_queue = smp_processor_id() % h->nreply_queues;
742c349775eSScott Teel 	/* Set the bits in the address sent down to include:
743c349775eSScott Teel 	 *  - performant mode bit not used in ioaccel mode 2
744c349775eSScott Teel 	 *  - pull count (bits 0-3)
745c349775eSScott Teel 	 *  - command type isn't needed for ioaccel2
746c349775eSScott Teel 	 */
747c349775eSScott Teel 	c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
748c349775eSScott Teel }
749c349775eSScott Teel 
750e85c5974SStephen M. Cameron static int is_firmware_flash_cmd(u8 *cdb)
751e85c5974SStephen M. Cameron {
752e85c5974SStephen M. Cameron 	return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
753e85c5974SStephen M. Cameron }
754e85c5974SStephen M. Cameron 
755e85c5974SStephen M. Cameron /*
756e85c5974SStephen M. Cameron  * During firmware flash, the heartbeat register may not update as frequently
757e85c5974SStephen M. Cameron  * as it should.  So we dial down lockup detection during firmware flash. and
758e85c5974SStephen M. Cameron  * dial it back up when firmware flash completes.
759e85c5974SStephen M. Cameron  */
760e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
761e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
762e85c5974SStephen M. Cameron static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
763e85c5974SStephen M. Cameron 		struct CommandList *c)
764e85c5974SStephen M. Cameron {
765e85c5974SStephen M. Cameron 	if (!is_firmware_flash_cmd(c->Request.CDB))
766e85c5974SStephen M. Cameron 		return;
767e85c5974SStephen M. Cameron 	atomic_inc(&h->firmware_flash_in_progress);
768e85c5974SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
769e85c5974SStephen M. Cameron }
770e85c5974SStephen M. Cameron 
771e85c5974SStephen M. Cameron static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
772e85c5974SStephen M. Cameron 		struct CommandList *c)
773e85c5974SStephen M. Cameron {
774e85c5974SStephen M. Cameron 	if (is_firmware_flash_cmd(c->Request.CDB) &&
775e85c5974SStephen M. Cameron 		atomic_dec_and_test(&h->firmware_flash_in_progress))
776e85c5974SStephen M. Cameron 		h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
777e85c5974SStephen M. Cameron }
778e85c5974SStephen M. Cameron 
7793f5eac3aSStephen M. Cameron static void enqueue_cmd_and_start_io(struct ctlr_info *h,
7803f5eac3aSStephen M. Cameron 	struct CommandList *c)
7813f5eac3aSStephen M. Cameron {
7823f5eac3aSStephen M. Cameron 	unsigned long flags;
7833f5eac3aSStephen M. Cameron 
784c349775eSScott Teel 	switch (c->cmd_type) {
785c349775eSScott Teel 	case CMD_IOACCEL1:
786c349775eSScott Teel 		set_ioaccel1_performant_mode(h, c);
787c349775eSScott Teel 		break;
788c349775eSScott Teel 	case CMD_IOACCEL2:
789c349775eSScott Teel 		set_ioaccel2_performant_mode(h, c);
790c349775eSScott Teel 		break;
791c349775eSScott Teel 	default:
7923f5eac3aSStephen M. Cameron 		set_performant_mode(h, c);
793c349775eSScott Teel 	}
794e85c5974SStephen M. Cameron 	dial_down_lockup_detection_during_fw_flash(h, c);
7953f5eac3aSStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
7963f5eac3aSStephen M. Cameron 	addQ(&h->reqQ, c);
7973f5eac3aSStephen M. Cameron 	h->Qdepth++;
7983f5eac3aSStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
799e16a33adSMatt Gates 	start_io(h);
8003f5eac3aSStephen M. Cameron }
8013f5eac3aSStephen M. Cameron 
8023f5eac3aSStephen M. Cameron static inline void removeQ(struct CommandList *c)
8033f5eac3aSStephen M. Cameron {
8043f5eac3aSStephen M. Cameron 	if (WARN_ON(list_empty(&c->list)))
8053f5eac3aSStephen M. Cameron 		return;
8063f5eac3aSStephen M. Cameron 	list_del_init(&c->list);
8073f5eac3aSStephen M. Cameron }
8083f5eac3aSStephen M. Cameron 
8093f5eac3aSStephen M. Cameron static inline int is_hba_lunid(unsigned char scsi3addr[])
8103f5eac3aSStephen M. Cameron {
8113f5eac3aSStephen M. Cameron 	return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
8123f5eac3aSStephen M. Cameron }
8133f5eac3aSStephen M. Cameron 
8143f5eac3aSStephen M. Cameron static inline int is_scsi_rev_5(struct ctlr_info *h)
8153f5eac3aSStephen M. Cameron {
8163f5eac3aSStephen M. Cameron 	if (!h->hba_inquiry_data)
8173f5eac3aSStephen M. Cameron 		return 0;
8183f5eac3aSStephen M. Cameron 	if ((h->hba_inquiry_data[2] & 0x07) == 5)
8193f5eac3aSStephen M. Cameron 		return 1;
8203f5eac3aSStephen M. Cameron 	return 0;
8213f5eac3aSStephen M. Cameron }
8223f5eac3aSStephen M. Cameron 
823edd16368SStephen M. Cameron static int hpsa_find_target_lun(struct ctlr_info *h,
824edd16368SStephen M. Cameron 	unsigned char scsi3addr[], int bus, int *target, int *lun)
825edd16368SStephen M. Cameron {
826edd16368SStephen M. Cameron 	/* finds an unused bus, target, lun for a new physical device
827edd16368SStephen M. Cameron 	 * assumes h->devlock is held
828edd16368SStephen M. Cameron 	 */
829edd16368SStephen M. Cameron 	int i, found = 0;
830cfe5badcSScott Teel 	DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
831edd16368SStephen M. Cameron 
832263d9401SAkinobu Mita 	bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
833edd16368SStephen M. Cameron 
834edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
835edd16368SStephen M. Cameron 		if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
836263d9401SAkinobu Mita 			__set_bit(h->dev[i]->target, lun_taken);
837edd16368SStephen M. Cameron 	}
838edd16368SStephen M. Cameron 
839263d9401SAkinobu Mita 	i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
840263d9401SAkinobu Mita 	if (i < HPSA_MAX_DEVICES) {
841edd16368SStephen M. Cameron 		/* *bus = 1; */
842edd16368SStephen M. Cameron 		*target = i;
843edd16368SStephen M. Cameron 		*lun = 0;
844edd16368SStephen M. Cameron 		found = 1;
845edd16368SStephen M. Cameron 	}
846edd16368SStephen M. Cameron 	return !found;
847edd16368SStephen M. Cameron }
848edd16368SStephen M. Cameron 
849edd16368SStephen M. Cameron /* Add an entry into h->dev[] array. */
850edd16368SStephen M. Cameron static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
851edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *device,
852edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *added[], int *nadded)
853edd16368SStephen M. Cameron {
854edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
855edd16368SStephen M. Cameron 	int n = h->ndevices;
856edd16368SStephen M. Cameron 	int i;
857edd16368SStephen M. Cameron 	unsigned char addr1[8], addr2[8];
858edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
859edd16368SStephen M. Cameron 
860cfe5badcSScott Teel 	if (n >= HPSA_MAX_DEVICES) {
861edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "too many devices, some will be "
862edd16368SStephen M. Cameron 			"inaccessible.\n");
863edd16368SStephen M. Cameron 		return -1;
864edd16368SStephen M. Cameron 	}
865edd16368SStephen M. Cameron 
866edd16368SStephen M. Cameron 	/* physical devices do not have lun or target assigned until now. */
867edd16368SStephen M. Cameron 	if (device->lun != -1)
868edd16368SStephen M. Cameron 		/* Logical device, lun is already assigned. */
869edd16368SStephen M. Cameron 		goto lun_assigned;
870edd16368SStephen M. Cameron 
871edd16368SStephen M. Cameron 	/* If this device a non-zero lun of a multi-lun device
872edd16368SStephen M. Cameron 	 * byte 4 of the 8-byte LUN addr will contain the logical
873edd16368SStephen M. Cameron 	 * unit no, zero otherise.
874edd16368SStephen M. Cameron 	 */
875edd16368SStephen M. Cameron 	if (device->scsi3addr[4] == 0) {
876edd16368SStephen M. Cameron 		/* This is not a non-zero lun of a multi-lun device */
877edd16368SStephen M. Cameron 		if (hpsa_find_target_lun(h, device->scsi3addr,
878edd16368SStephen M. Cameron 			device->bus, &device->target, &device->lun) != 0)
879edd16368SStephen M. Cameron 			return -1;
880edd16368SStephen M. Cameron 		goto lun_assigned;
881edd16368SStephen M. Cameron 	}
882edd16368SStephen M. Cameron 
883edd16368SStephen M. Cameron 	/* This is a non-zero lun of a multi-lun device.
884edd16368SStephen M. Cameron 	 * Search through our list and find the device which
885edd16368SStephen M. Cameron 	 * has the same 8 byte LUN address, excepting byte 4.
886edd16368SStephen M. Cameron 	 * Assign the same bus and target for this new LUN.
887edd16368SStephen M. Cameron 	 * Use the logical unit number from the firmware.
888edd16368SStephen M. Cameron 	 */
889edd16368SStephen M. Cameron 	memcpy(addr1, device->scsi3addr, 8);
890edd16368SStephen M. Cameron 	addr1[4] = 0;
891edd16368SStephen M. Cameron 	for (i = 0; i < n; i++) {
892edd16368SStephen M. Cameron 		sd = h->dev[i];
893edd16368SStephen M. Cameron 		memcpy(addr2, sd->scsi3addr, 8);
894edd16368SStephen M. Cameron 		addr2[4] = 0;
895edd16368SStephen M. Cameron 		/* differ only in byte 4? */
896edd16368SStephen M. Cameron 		if (memcmp(addr1, addr2, 8) == 0) {
897edd16368SStephen M. Cameron 			device->bus = sd->bus;
898edd16368SStephen M. Cameron 			device->target = sd->target;
899edd16368SStephen M. Cameron 			device->lun = device->scsi3addr[4];
900edd16368SStephen M. Cameron 			break;
901edd16368SStephen M. Cameron 		}
902edd16368SStephen M. Cameron 	}
903edd16368SStephen M. Cameron 	if (device->lun == -1) {
904edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
905edd16368SStephen M. Cameron 			" suspect firmware bug or unsupported hardware "
906edd16368SStephen M. Cameron 			"configuration.\n");
907edd16368SStephen M. Cameron 			return -1;
908edd16368SStephen M. Cameron 	}
909edd16368SStephen M. Cameron 
910edd16368SStephen M. Cameron lun_assigned:
911edd16368SStephen M. Cameron 
912edd16368SStephen M. Cameron 	h->dev[n] = device;
913edd16368SStephen M. Cameron 	h->ndevices++;
914edd16368SStephen M. Cameron 	added[*nadded] = device;
915edd16368SStephen M. Cameron 	(*nadded)++;
916edd16368SStephen M. Cameron 
917edd16368SStephen M. Cameron 	/* initially, (before registering with scsi layer) we don't
918edd16368SStephen M. Cameron 	 * know our hostno and we don't want to print anything first
919edd16368SStephen M. Cameron 	 * time anyway (the scsi layer's inquiries will show that info)
920edd16368SStephen M. Cameron 	 */
921edd16368SStephen M. Cameron 	/* if (hostno != -1) */
922edd16368SStephen M. Cameron 		dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
923edd16368SStephen M. Cameron 			scsi_device_type(device->devtype), hostno,
924edd16368SStephen M. Cameron 			device->bus, device->target, device->lun);
925edd16368SStephen M. Cameron 	return 0;
926edd16368SStephen M. Cameron }
927edd16368SStephen M. Cameron 
928bd9244f7SScott Teel /* Update an entry in h->dev[] array. */
929bd9244f7SScott Teel static void hpsa_scsi_update_entry(struct ctlr_info *h, int hostno,
930bd9244f7SScott Teel 	int entry, struct hpsa_scsi_dev_t *new_entry)
931bd9244f7SScott Teel {
932bd9244f7SScott Teel 	/* assumes h->devlock is held */
933bd9244f7SScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
934bd9244f7SScott Teel 
935bd9244f7SScott Teel 	/* Raid level changed. */
936bd9244f7SScott Teel 	h->dev[entry]->raid_level = new_entry->raid_level;
937250fb125SStephen M. Cameron 
938250fb125SStephen M. Cameron 	/* Raid offload parameters changed. */
939250fb125SStephen M. Cameron 	h->dev[entry]->offload_config = new_entry->offload_config;
940250fb125SStephen M. Cameron 	h->dev[entry]->offload_enabled = new_entry->offload_enabled;
9419fb0de2dSStephen M. Cameron 	h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
9429fb0de2dSStephen M. Cameron 	h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
9439fb0de2dSStephen M. Cameron 	h->dev[entry]->raid_map = new_entry->raid_map;
944250fb125SStephen M. Cameron 
945bd9244f7SScott Teel 	dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d updated.\n",
946bd9244f7SScott Teel 		scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
947bd9244f7SScott Teel 		new_entry->target, new_entry->lun);
948bd9244f7SScott Teel }
949bd9244f7SScott Teel 
9502a8ccf31SStephen M. Cameron /* Replace an entry from h->dev[] array. */
9512a8ccf31SStephen M. Cameron static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
9522a8ccf31SStephen M. Cameron 	int entry, struct hpsa_scsi_dev_t *new_entry,
9532a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *added[], int *nadded,
9542a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
9552a8ccf31SStephen M. Cameron {
9562a8ccf31SStephen M. Cameron 	/* assumes h->devlock is held */
957cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
9582a8ccf31SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
9592a8ccf31SStephen M. Cameron 	(*nremoved)++;
96001350d05SStephen M. Cameron 
96101350d05SStephen M. Cameron 	/*
96201350d05SStephen M. Cameron 	 * New physical devices won't have target/lun assigned yet
96301350d05SStephen M. Cameron 	 * so we need to preserve the values in the slot we are replacing.
96401350d05SStephen M. Cameron 	 */
96501350d05SStephen M. Cameron 	if (new_entry->target == -1) {
96601350d05SStephen M. Cameron 		new_entry->target = h->dev[entry]->target;
96701350d05SStephen M. Cameron 		new_entry->lun = h->dev[entry]->lun;
96801350d05SStephen M. Cameron 	}
96901350d05SStephen M. Cameron 
9702a8ccf31SStephen M. Cameron 	h->dev[entry] = new_entry;
9712a8ccf31SStephen M. Cameron 	added[*nadded] = new_entry;
9722a8ccf31SStephen M. Cameron 	(*nadded)++;
9732a8ccf31SStephen M. Cameron 	dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n",
9742a8ccf31SStephen M. Cameron 		scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
9752a8ccf31SStephen M. Cameron 			new_entry->target, new_entry->lun);
9762a8ccf31SStephen M. Cameron }
9772a8ccf31SStephen M. Cameron 
978edd16368SStephen M. Cameron /* Remove an entry from h->dev[] array. */
979edd16368SStephen M. Cameron static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
980edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
981edd16368SStephen M. Cameron {
982edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
983edd16368SStephen M. Cameron 	int i;
984edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
985edd16368SStephen M. Cameron 
986cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
987edd16368SStephen M. Cameron 
988edd16368SStephen M. Cameron 	sd = h->dev[entry];
989edd16368SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
990edd16368SStephen M. Cameron 	(*nremoved)++;
991edd16368SStephen M. Cameron 
992edd16368SStephen M. Cameron 	for (i = entry; i < h->ndevices-1; i++)
993edd16368SStephen M. Cameron 		h->dev[i] = h->dev[i+1];
994edd16368SStephen M. Cameron 	h->ndevices--;
995edd16368SStephen M. Cameron 	dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
996edd16368SStephen M. Cameron 		scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
997edd16368SStephen M. Cameron 		sd->lun);
998edd16368SStephen M. Cameron }
999edd16368SStephen M. Cameron 
1000edd16368SStephen M. Cameron #define SCSI3ADDR_EQ(a, b) ( \
1001edd16368SStephen M. Cameron 	(a)[7] == (b)[7] && \
1002edd16368SStephen M. Cameron 	(a)[6] == (b)[6] && \
1003edd16368SStephen M. Cameron 	(a)[5] == (b)[5] && \
1004edd16368SStephen M. Cameron 	(a)[4] == (b)[4] && \
1005edd16368SStephen M. Cameron 	(a)[3] == (b)[3] && \
1006edd16368SStephen M. Cameron 	(a)[2] == (b)[2] && \
1007edd16368SStephen M. Cameron 	(a)[1] == (b)[1] && \
1008edd16368SStephen M. Cameron 	(a)[0] == (b)[0])
1009edd16368SStephen M. Cameron 
1010edd16368SStephen M. Cameron static void fixup_botched_add(struct ctlr_info *h,
1011edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *added)
1012edd16368SStephen M. Cameron {
1013edd16368SStephen M. Cameron 	/* called when scsi_add_device fails in order to re-adjust
1014edd16368SStephen M. Cameron 	 * h->dev[] to match the mid layer's view.
1015edd16368SStephen M. Cameron 	 */
1016edd16368SStephen M. Cameron 	unsigned long flags;
1017edd16368SStephen M. Cameron 	int i, j;
1018edd16368SStephen M. Cameron 
1019edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
1020edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1021edd16368SStephen M. Cameron 		if (h->dev[i] == added) {
1022edd16368SStephen M. Cameron 			for (j = i; j < h->ndevices-1; j++)
1023edd16368SStephen M. Cameron 				h->dev[j] = h->dev[j+1];
1024edd16368SStephen M. Cameron 			h->ndevices--;
1025edd16368SStephen M. Cameron 			break;
1026edd16368SStephen M. Cameron 		}
1027edd16368SStephen M. Cameron 	}
1028edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
1029edd16368SStephen M. Cameron 	kfree(added);
1030edd16368SStephen M. Cameron }
1031edd16368SStephen M. Cameron 
1032edd16368SStephen M. Cameron static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
1033edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev2)
1034edd16368SStephen M. Cameron {
1035edd16368SStephen M. Cameron 	/* we compare everything except lun and target as these
1036edd16368SStephen M. Cameron 	 * are not yet assigned.  Compare parts likely
1037edd16368SStephen M. Cameron 	 * to differ first
1038edd16368SStephen M. Cameron 	 */
1039edd16368SStephen M. Cameron 	if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
1040edd16368SStephen M. Cameron 		sizeof(dev1->scsi3addr)) != 0)
1041edd16368SStephen M. Cameron 		return 0;
1042edd16368SStephen M. Cameron 	if (memcmp(dev1->device_id, dev2->device_id,
1043edd16368SStephen M. Cameron 		sizeof(dev1->device_id)) != 0)
1044edd16368SStephen M. Cameron 		return 0;
1045edd16368SStephen M. Cameron 	if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
1046edd16368SStephen M. Cameron 		return 0;
1047edd16368SStephen M. Cameron 	if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
1048edd16368SStephen M. Cameron 		return 0;
1049edd16368SStephen M. Cameron 	if (dev1->devtype != dev2->devtype)
1050edd16368SStephen M. Cameron 		return 0;
1051edd16368SStephen M. Cameron 	if (dev1->bus != dev2->bus)
1052edd16368SStephen M. Cameron 		return 0;
1053edd16368SStephen M. Cameron 	return 1;
1054edd16368SStephen M. Cameron }
1055edd16368SStephen M. Cameron 
1056bd9244f7SScott Teel static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
1057bd9244f7SScott Teel 	struct hpsa_scsi_dev_t *dev2)
1058bd9244f7SScott Teel {
1059bd9244f7SScott Teel 	/* Device attributes that can change, but don't mean
1060bd9244f7SScott Teel 	 * that the device is a different device, nor that the OS
1061bd9244f7SScott Teel 	 * needs to be told anything about the change.
1062bd9244f7SScott Teel 	 */
1063bd9244f7SScott Teel 	if (dev1->raid_level != dev2->raid_level)
1064bd9244f7SScott Teel 		return 1;
1065250fb125SStephen M. Cameron 	if (dev1->offload_config != dev2->offload_config)
1066250fb125SStephen M. Cameron 		return 1;
1067250fb125SStephen M. Cameron 	if (dev1->offload_enabled != dev2->offload_enabled)
1068250fb125SStephen M. Cameron 		return 1;
1069bd9244f7SScott Teel 	return 0;
1070bd9244f7SScott Teel }
1071bd9244f7SScott Teel 
1072edd16368SStephen M. Cameron /* Find needle in haystack.  If exact match found, return DEVICE_SAME,
1073edd16368SStephen M. Cameron  * and return needle location in *index.  If scsi3addr matches, but not
1074edd16368SStephen M. Cameron  * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
1075bd9244f7SScott Teel  * location in *index.
1076bd9244f7SScott Teel  * In the case of a minor device attribute change, such as RAID level, just
1077bd9244f7SScott Teel  * return DEVICE_UPDATED, along with the updated device's location in index.
1078bd9244f7SScott Teel  * If needle not found, return DEVICE_NOT_FOUND.
1079edd16368SStephen M. Cameron  */
1080edd16368SStephen M. Cameron static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
1081edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *haystack[], int haystack_size,
1082edd16368SStephen M. Cameron 	int *index)
1083edd16368SStephen M. Cameron {
1084edd16368SStephen M. Cameron 	int i;
1085edd16368SStephen M. Cameron #define DEVICE_NOT_FOUND 0
1086edd16368SStephen M. Cameron #define DEVICE_CHANGED 1
1087edd16368SStephen M. Cameron #define DEVICE_SAME 2
1088bd9244f7SScott Teel #define DEVICE_UPDATED 3
1089edd16368SStephen M. Cameron 	for (i = 0; i < haystack_size; i++) {
109023231048SStephen M. Cameron 		if (haystack[i] == NULL) /* previously removed. */
109123231048SStephen M. Cameron 			continue;
1092edd16368SStephen M. Cameron 		if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
1093edd16368SStephen M. Cameron 			*index = i;
1094bd9244f7SScott Teel 			if (device_is_the_same(needle, haystack[i])) {
1095bd9244f7SScott Teel 				if (device_updated(needle, haystack[i]))
1096bd9244f7SScott Teel 					return DEVICE_UPDATED;
1097edd16368SStephen M. Cameron 				return DEVICE_SAME;
1098bd9244f7SScott Teel 			} else {
1099edd16368SStephen M. Cameron 				return DEVICE_CHANGED;
1100edd16368SStephen M. Cameron 			}
1101edd16368SStephen M. Cameron 		}
1102bd9244f7SScott Teel 	}
1103edd16368SStephen M. Cameron 	*index = -1;
1104edd16368SStephen M. Cameron 	return DEVICE_NOT_FOUND;
1105edd16368SStephen M. Cameron }
1106edd16368SStephen M. Cameron 
11074967bd3eSStephen M. Cameron static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
1108edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd[], int nsds)
1109edd16368SStephen M. Cameron {
1110edd16368SStephen M. Cameron 	/* sd contains scsi3 addresses and devtypes, and inquiry
1111edd16368SStephen M. Cameron 	 * data.  This function takes what's in sd to be the current
1112edd16368SStephen M. Cameron 	 * reality and updates h->dev[] to reflect that reality.
1113edd16368SStephen M. Cameron 	 */
1114edd16368SStephen M. Cameron 	int i, entry, device_change, changes = 0;
1115edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *csd;
1116edd16368SStephen M. Cameron 	unsigned long flags;
1117edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **added, **removed;
1118edd16368SStephen M. Cameron 	int nadded, nremoved;
1119edd16368SStephen M. Cameron 	struct Scsi_Host *sh = NULL;
1120edd16368SStephen M. Cameron 
1121cfe5badcSScott Teel 	added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
1122cfe5badcSScott Teel 	removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
1123edd16368SStephen M. Cameron 
1124edd16368SStephen M. Cameron 	if (!added || !removed) {
1125edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in "
1126edd16368SStephen M. Cameron 			"adjust_hpsa_scsi_table\n");
1127edd16368SStephen M. Cameron 		goto free_and_out;
1128edd16368SStephen M. Cameron 	}
1129edd16368SStephen M. Cameron 
1130edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1131edd16368SStephen M. Cameron 
1132edd16368SStephen M. Cameron 	/* find any devices in h->dev[] that are not in
1133edd16368SStephen M. Cameron 	 * sd[] and remove them from h->dev[], and for any
1134edd16368SStephen M. Cameron 	 * devices which have changed, remove the old device
1135edd16368SStephen M. Cameron 	 * info and add the new device info.
1136bd9244f7SScott Teel 	 * If minor device attributes change, just update
1137bd9244f7SScott Teel 	 * the existing device structure.
1138edd16368SStephen M. Cameron 	 */
1139edd16368SStephen M. Cameron 	i = 0;
1140edd16368SStephen M. Cameron 	nremoved = 0;
1141edd16368SStephen M. Cameron 	nadded = 0;
1142edd16368SStephen M. Cameron 	while (i < h->ndevices) {
1143edd16368SStephen M. Cameron 		csd = h->dev[i];
1144edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
1145edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
1146edd16368SStephen M. Cameron 			changes++;
1147edd16368SStephen M. Cameron 			hpsa_scsi_remove_entry(h, hostno, i,
1148edd16368SStephen M. Cameron 				removed, &nremoved);
1149edd16368SStephen M. Cameron 			continue; /* remove ^^^, hence i not incremented */
1150edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
1151edd16368SStephen M. Cameron 			changes++;
11522a8ccf31SStephen M. Cameron 			hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
11532a8ccf31SStephen M. Cameron 				added, &nadded, removed, &nremoved);
1154c7f172dcSStephen M. Cameron 			/* Set it to NULL to prevent it from being freed
1155c7f172dcSStephen M. Cameron 			 * at the bottom of hpsa_update_scsi_devices()
1156c7f172dcSStephen M. Cameron 			 */
1157c7f172dcSStephen M. Cameron 			sd[entry] = NULL;
1158bd9244f7SScott Teel 		} else if (device_change == DEVICE_UPDATED) {
1159bd9244f7SScott Teel 			hpsa_scsi_update_entry(h, hostno, i, sd[entry]);
1160edd16368SStephen M. Cameron 		}
1161edd16368SStephen M. Cameron 		i++;
1162edd16368SStephen M. Cameron 	}
1163edd16368SStephen M. Cameron 
1164edd16368SStephen M. Cameron 	/* Now, make sure every device listed in sd[] is also
1165edd16368SStephen M. Cameron 	 * listed in h->dev[], adding them if they aren't found
1166edd16368SStephen M. Cameron 	 */
1167edd16368SStephen M. Cameron 
1168edd16368SStephen M. Cameron 	for (i = 0; i < nsds; i++) {
1169edd16368SStephen M. Cameron 		if (!sd[i]) /* if already added above. */
1170edd16368SStephen M. Cameron 			continue;
1171edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1172edd16368SStephen M. Cameron 					h->ndevices, &entry);
1173edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
1174edd16368SStephen M. Cameron 			changes++;
1175edd16368SStephen M. Cameron 			if (hpsa_scsi_add_entry(h, hostno, sd[i],
1176edd16368SStephen M. Cameron 				added, &nadded) != 0)
1177edd16368SStephen M. Cameron 				break;
1178edd16368SStephen M. Cameron 			sd[i] = NULL; /* prevent from being freed later. */
1179edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
1180edd16368SStephen M. Cameron 			/* should never happen... */
1181edd16368SStephen M. Cameron 			changes++;
1182edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev,
1183edd16368SStephen M. Cameron 				"device unexpectedly changed.\n");
1184edd16368SStephen M. Cameron 			/* but if it does happen, we just ignore that device */
1185edd16368SStephen M. Cameron 		}
1186edd16368SStephen M. Cameron 	}
1187edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1188edd16368SStephen M. Cameron 
1189edd16368SStephen M. Cameron 	/* Don't notify scsi mid layer of any changes the first time through
1190edd16368SStephen M. Cameron 	 * (or if there are no changes) scsi_scan_host will do it later the
1191edd16368SStephen M. Cameron 	 * first time through.
1192edd16368SStephen M. Cameron 	 */
1193edd16368SStephen M. Cameron 	if (hostno == -1 || !changes)
1194edd16368SStephen M. Cameron 		goto free_and_out;
1195edd16368SStephen M. Cameron 
1196edd16368SStephen M. Cameron 	sh = h->scsi_host;
1197edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any removed devices */
1198edd16368SStephen M. Cameron 	for (i = 0; i < nremoved; i++) {
1199edd16368SStephen M. Cameron 		struct scsi_device *sdev =
1200edd16368SStephen M. Cameron 			scsi_device_lookup(sh, removed[i]->bus,
1201edd16368SStephen M. Cameron 				removed[i]->target, removed[i]->lun);
1202edd16368SStephen M. Cameron 		if (sdev != NULL) {
1203edd16368SStephen M. Cameron 			scsi_remove_device(sdev);
1204edd16368SStephen M. Cameron 			scsi_device_put(sdev);
1205edd16368SStephen M. Cameron 		} else {
1206edd16368SStephen M. Cameron 			/* We don't expect to get here.
1207edd16368SStephen M. Cameron 			 * future cmds to this device will get selection
1208edd16368SStephen M. Cameron 			 * timeout as if the device was gone.
1209edd16368SStephen M. Cameron 			 */
1210edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
1211edd16368SStephen M. Cameron 				" for removal.", hostno, removed[i]->bus,
1212edd16368SStephen M. Cameron 				removed[i]->target, removed[i]->lun);
1213edd16368SStephen M. Cameron 		}
1214edd16368SStephen M. Cameron 		kfree(removed[i]);
1215edd16368SStephen M. Cameron 		removed[i] = NULL;
1216edd16368SStephen M. Cameron 	}
1217edd16368SStephen M. Cameron 
1218edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any added devices */
1219edd16368SStephen M. Cameron 	for (i = 0; i < nadded; i++) {
1220edd16368SStephen M. Cameron 		if (scsi_add_device(sh, added[i]->bus,
1221edd16368SStephen M. Cameron 			added[i]->target, added[i]->lun) == 0)
1222edd16368SStephen M. Cameron 			continue;
1223edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
1224edd16368SStephen M. Cameron 			"device not added.\n", hostno, added[i]->bus,
1225edd16368SStephen M. Cameron 			added[i]->target, added[i]->lun);
1226edd16368SStephen M. Cameron 		/* now we have to remove it from h->dev,
1227edd16368SStephen M. Cameron 		 * since it didn't get added to scsi mid layer
1228edd16368SStephen M. Cameron 		 */
1229edd16368SStephen M. Cameron 		fixup_botched_add(h, added[i]);
1230edd16368SStephen M. Cameron 	}
1231edd16368SStephen M. Cameron 
1232edd16368SStephen M. Cameron free_and_out:
1233edd16368SStephen M. Cameron 	kfree(added);
1234edd16368SStephen M. Cameron 	kfree(removed);
1235edd16368SStephen M. Cameron }
1236edd16368SStephen M. Cameron 
1237edd16368SStephen M. Cameron /*
12389e03aa2fSJoe Perches  * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
1239edd16368SStephen M. Cameron  * Assume's h->devlock is held.
1240edd16368SStephen M. Cameron  */
1241edd16368SStephen M. Cameron static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1242edd16368SStephen M. Cameron 	int bus, int target, int lun)
1243edd16368SStephen M. Cameron {
1244edd16368SStephen M. Cameron 	int i;
1245edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1246edd16368SStephen M. Cameron 
1247edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1248edd16368SStephen M. Cameron 		sd = h->dev[i];
1249edd16368SStephen M. Cameron 		if (sd->bus == bus && sd->target == target && sd->lun == lun)
1250edd16368SStephen M. Cameron 			return sd;
1251edd16368SStephen M. Cameron 	}
1252edd16368SStephen M. Cameron 	return NULL;
1253edd16368SStephen M. Cameron }
1254edd16368SStephen M. Cameron 
1255edd16368SStephen M. Cameron /* link sdev->hostdata to our per-device structure. */
1256edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev)
1257edd16368SStephen M. Cameron {
1258edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1259edd16368SStephen M. Cameron 	unsigned long flags;
1260edd16368SStephen M. Cameron 	struct ctlr_info *h;
1261edd16368SStephen M. Cameron 
1262edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
1263edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1264edd16368SStephen M. Cameron 	sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1265edd16368SStephen M. Cameron 		sdev_id(sdev), sdev->lun);
1266edd16368SStephen M. Cameron 	if (sd != NULL)
1267edd16368SStephen M. Cameron 		sdev->hostdata = sd;
1268edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1269edd16368SStephen M. Cameron 	return 0;
1270edd16368SStephen M. Cameron }
1271edd16368SStephen M. Cameron 
1272edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev)
1273edd16368SStephen M. Cameron {
1274bcc44255SStephen M. Cameron 	/* nothing to do. */
1275edd16368SStephen M. Cameron }
1276edd16368SStephen M. Cameron 
127733a2ffceSStephen M. Cameron static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
127833a2ffceSStephen M. Cameron {
127933a2ffceSStephen M. Cameron 	int i;
128033a2ffceSStephen M. Cameron 
128133a2ffceSStephen M. Cameron 	if (!h->cmd_sg_list)
128233a2ffceSStephen M. Cameron 		return;
128333a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
128433a2ffceSStephen M. Cameron 		kfree(h->cmd_sg_list[i]);
128533a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = NULL;
128633a2ffceSStephen M. Cameron 	}
128733a2ffceSStephen M. Cameron 	kfree(h->cmd_sg_list);
128833a2ffceSStephen M. Cameron 	h->cmd_sg_list = NULL;
128933a2ffceSStephen M. Cameron }
129033a2ffceSStephen M. Cameron 
129133a2ffceSStephen M. Cameron static int hpsa_allocate_sg_chain_blocks(struct ctlr_info *h)
129233a2ffceSStephen M. Cameron {
129333a2ffceSStephen M. Cameron 	int i;
129433a2ffceSStephen M. Cameron 
129533a2ffceSStephen M. Cameron 	if (h->chainsize <= 0)
129633a2ffceSStephen M. Cameron 		return 0;
129733a2ffceSStephen M. Cameron 
129833a2ffceSStephen M. Cameron 	h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
129933a2ffceSStephen M. Cameron 				GFP_KERNEL);
130033a2ffceSStephen M. Cameron 	if (!h->cmd_sg_list)
130133a2ffceSStephen M. Cameron 		return -ENOMEM;
130233a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
130333a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
130433a2ffceSStephen M. Cameron 						h->chainsize, GFP_KERNEL);
130533a2ffceSStephen M. Cameron 		if (!h->cmd_sg_list[i])
130633a2ffceSStephen M. Cameron 			goto clean;
130733a2ffceSStephen M. Cameron 	}
130833a2ffceSStephen M. Cameron 	return 0;
130933a2ffceSStephen M. Cameron 
131033a2ffceSStephen M. Cameron clean:
131133a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
131233a2ffceSStephen M. Cameron 	return -ENOMEM;
131333a2ffceSStephen M. Cameron }
131433a2ffceSStephen M. Cameron 
1315e2bea6dfSStephen M. Cameron static int hpsa_map_sg_chain_block(struct ctlr_info *h,
131633a2ffceSStephen M. Cameron 	struct CommandList *c)
131733a2ffceSStephen M. Cameron {
131833a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg, *chain_block;
131933a2ffceSStephen M. Cameron 	u64 temp64;
132033a2ffceSStephen M. Cameron 
132133a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
132233a2ffceSStephen M. Cameron 	chain_block = h->cmd_sg_list[c->cmdindex];
132333a2ffceSStephen M. Cameron 	chain_sg->Ext = HPSA_SG_CHAIN;
132433a2ffceSStephen M. Cameron 	chain_sg->Len = sizeof(*chain_sg) *
132533a2ffceSStephen M. Cameron 		(c->Header.SGTotal - h->max_cmd_sg_entries);
132633a2ffceSStephen M. Cameron 	temp64 = pci_map_single(h->pdev, chain_block, chain_sg->Len,
132733a2ffceSStephen M. Cameron 				PCI_DMA_TODEVICE);
1328e2bea6dfSStephen M. Cameron 	if (dma_mapping_error(&h->pdev->dev, temp64)) {
1329e2bea6dfSStephen M. Cameron 		/* prevent subsequent unmapping */
1330e2bea6dfSStephen M. Cameron 		chain_sg->Addr.lower = 0;
1331e2bea6dfSStephen M. Cameron 		chain_sg->Addr.upper = 0;
1332e2bea6dfSStephen M. Cameron 		return -1;
1333e2bea6dfSStephen M. Cameron 	}
133433a2ffceSStephen M. Cameron 	chain_sg->Addr.lower = (u32) (temp64 & 0x0FFFFFFFFULL);
133533a2ffceSStephen M. Cameron 	chain_sg->Addr.upper = (u32) ((temp64 >> 32) & 0x0FFFFFFFFULL);
1336e2bea6dfSStephen M. Cameron 	return 0;
133733a2ffceSStephen M. Cameron }
133833a2ffceSStephen M. Cameron 
133933a2ffceSStephen M. Cameron static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
134033a2ffceSStephen M. Cameron 	struct CommandList *c)
134133a2ffceSStephen M. Cameron {
134233a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg;
134333a2ffceSStephen M. Cameron 	union u64bit temp64;
134433a2ffceSStephen M. Cameron 
134533a2ffceSStephen M. Cameron 	if (c->Header.SGTotal <= h->max_cmd_sg_entries)
134633a2ffceSStephen M. Cameron 		return;
134733a2ffceSStephen M. Cameron 
134833a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
134933a2ffceSStephen M. Cameron 	temp64.val32.lower = chain_sg->Addr.lower;
135033a2ffceSStephen M. Cameron 	temp64.val32.upper = chain_sg->Addr.upper;
135133a2ffceSStephen M. Cameron 	pci_unmap_single(h->pdev, temp64.val, chain_sg->Len, PCI_DMA_TODEVICE);
135233a2ffceSStephen M. Cameron }
135333a2ffceSStephen M. Cameron 
1354a09c1441SScott Teel 
1355a09c1441SScott Teel /* Decode the various types of errors on ioaccel2 path.
1356a09c1441SScott Teel  * Return 1 for any error that should generate a RAID path retry.
1357a09c1441SScott Teel  * Return 0 for errors that don't require a RAID path retry.
1358a09c1441SScott Teel  */
1359a09c1441SScott Teel static int handle_ioaccel_mode2_error(struct ctlr_info *h,
1360c349775eSScott Teel 					struct CommandList *c,
1361c349775eSScott Teel 					struct scsi_cmnd *cmd,
1362c349775eSScott Teel 					struct io_accel2_cmd *c2)
1363c349775eSScott Teel {
1364c349775eSScott Teel 	int data_len;
1365a09c1441SScott Teel 	int retry = 0;
1366c349775eSScott Teel 
1367c349775eSScott Teel 	switch (c2->error_data.serv_response) {
1368c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_COMPLETE:
1369c349775eSScott Teel 		switch (c2->error_data.status) {
1370c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
1371c349775eSScott Teel 			break;
1372c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
1373c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1374c349775eSScott Teel 				"%s: task complete with check condition.\n",
1375c349775eSScott Teel 				"HP SSD Smart Path");
1376c349775eSScott Teel 			if (c2->error_data.data_present !=
1377c349775eSScott Teel 					IOACCEL2_SENSE_DATA_PRESENT)
1378c349775eSScott Teel 				break;
1379c349775eSScott Teel 			/* copy the sense data */
1380c349775eSScott Teel 			data_len = c2->error_data.sense_data_len;
1381c349775eSScott Teel 			if (data_len > SCSI_SENSE_BUFFERSIZE)
1382c349775eSScott Teel 				data_len = SCSI_SENSE_BUFFERSIZE;
1383c349775eSScott Teel 			if (data_len > sizeof(c2->error_data.sense_data_buff))
1384c349775eSScott Teel 				data_len =
1385c349775eSScott Teel 					sizeof(c2->error_data.sense_data_buff);
1386c349775eSScott Teel 			memcpy(cmd->sense_buffer,
1387c349775eSScott Teel 				c2->error_data.sense_data_buff, data_len);
1388c349775eSScott Teel 			cmd->result |= SAM_STAT_CHECK_CONDITION;
1389a09c1441SScott Teel 			retry = 1;
1390c349775eSScott Teel 			break;
1391c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
1392c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1393c349775eSScott Teel 				"%s: task complete with BUSY status.\n",
1394c349775eSScott Teel 				"HP SSD Smart Path");
1395a09c1441SScott Teel 			retry = 1;
1396c349775eSScott Teel 			break;
1397c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
1398c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1399c349775eSScott Teel 				"%s: task complete with reservation conflict.\n",
1400c349775eSScott Teel 				"HP SSD Smart Path");
1401a09c1441SScott Teel 			retry = 1;
1402c349775eSScott Teel 			break;
1403c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
1404c349775eSScott Teel 			/* Make scsi midlayer do unlimited retries */
1405c349775eSScott Teel 			cmd->result = DID_IMM_RETRY << 16;
1406c349775eSScott Teel 			break;
1407c349775eSScott Teel 		case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
1408c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1409c349775eSScott Teel 				"%s: task complete with aborted status.\n",
1410c349775eSScott Teel 				"HP SSD Smart Path");
1411a09c1441SScott Teel 			retry = 1;
1412c349775eSScott Teel 			break;
1413c349775eSScott Teel 		default:
1414c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1415c349775eSScott Teel 				"%s: task complete with unrecognized status: 0x%02x\n",
1416c349775eSScott Teel 				"HP SSD Smart Path", c2->error_data.status);
1417a09c1441SScott Teel 			retry = 1;
1418c349775eSScott Teel 			break;
1419c349775eSScott Teel 		}
1420c349775eSScott Teel 		break;
1421c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_FAILURE:
1422c349775eSScott Teel 		/* don't expect to get here. */
1423c349775eSScott Teel 		dev_warn(&h->pdev->dev,
1424c349775eSScott Teel 			"unexpected delivery or target failure, status = 0x%02x\n",
1425c349775eSScott Teel 			c2->error_data.status);
1426a09c1441SScott Teel 		retry = 1;
1427c349775eSScott Teel 		break;
1428c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
1429c349775eSScott Teel 		break;
1430c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
1431c349775eSScott Teel 		break;
1432c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
1433c349775eSScott Teel 		dev_warn(&h->pdev->dev, "task management function rejected.\n");
1434a09c1441SScott Teel 		retry = 1;
1435c349775eSScott Teel 		break;
1436c349775eSScott Teel 	case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
1437c349775eSScott Teel 		dev_warn(&h->pdev->dev, "task management function invalid LUN\n");
1438c349775eSScott Teel 		break;
1439c349775eSScott Teel 	default:
1440c349775eSScott Teel 		dev_warn(&h->pdev->dev,
1441c349775eSScott Teel 			"%s: Unrecognized server response: 0x%02x\n",
1442a09c1441SScott Teel 			"HP SSD Smart Path",
1443a09c1441SScott Teel 			c2->error_data.serv_response);
1444a09c1441SScott Teel 		retry = 1;
1445c349775eSScott Teel 		break;
1446c349775eSScott Teel 	}
1447a09c1441SScott Teel 
1448a09c1441SScott Teel 	return retry;	/* retry on raid path? */
1449c349775eSScott Teel }
1450c349775eSScott Teel 
1451c349775eSScott Teel static void process_ioaccel2_completion(struct ctlr_info *h,
1452c349775eSScott Teel 		struct CommandList *c, struct scsi_cmnd *cmd,
1453c349775eSScott Teel 		struct hpsa_scsi_dev_t *dev)
1454c349775eSScott Teel {
1455c349775eSScott Teel 	struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
1456a09c1441SScott Teel 	int raid_retry = 0;
1457c349775eSScott Teel 
1458c349775eSScott Teel 	/* check for good status */
1459c349775eSScott Teel 	if (likely(c2->error_data.serv_response == 0 &&
1460c349775eSScott Teel 			c2->error_data.status == 0)) {
1461c349775eSScott Teel 		cmd_free(h, c);
1462c349775eSScott Teel 		cmd->scsi_done(cmd);
1463c349775eSScott Teel 		return;
1464c349775eSScott Teel 	}
1465c349775eSScott Teel 
1466c349775eSScott Teel 	/* Any RAID offload error results in retry which will use
1467c349775eSScott Teel 	 * the normal I/O path so the controller can handle whatever's
1468c349775eSScott Teel 	 * wrong.
1469c349775eSScott Teel 	 */
1470c349775eSScott Teel 	if (is_logical_dev_addr_mode(dev->scsi3addr) &&
1471c349775eSScott Teel 		c2->error_data.serv_response ==
1472c349775eSScott Teel 			IOACCEL2_SERV_RESPONSE_FAILURE) {
1473a09c1441SScott Teel 		if (c2->error_data.status ==
1474c349775eSScott Teel 			IOACCEL2_STATUS_SR_IOACCEL_DISABLED)
1475c349775eSScott Teel 			dev_warn(&h->pdev->dev,
1476a09c1441SScott Teel 				"%s: Path is unavailable, retrying on standard path.\n",
1477a09c1441SScott Teel 				"HP SSD Smart Path");
1478a09c1441SScott Teel 		else
1479a09c1441SScott Teel 			dev_warn(&h->pdev->dev,
1480a09c1441SScott Teel 				"%s: Error 0x%02x, retrying on standard path.\n",
1481c349775eSScott Teel 				"HP SSD Smart Path", c2->error_data.status);
1482a09c1441SScott Teel 
1483c349775eSScott Teel 		dev->offload_enabled = 0;
1484e863d68eSScott Teel 		h->drv_req_rescan = 1;	/* schedule controller for a rescan */
1485c349775eSScott Teel 		cmd->result = DID_SOFT_ERROR << 16;
1486c349775eSScott Teel 		cmd_free(h, c);
1487c349775eSScott Teel 		cmd->scsi_done(cmd);
1488c349775eSScott Teel 		return;
1489c349775eSScott Teel 	}
1490a09c1441SScott Teel 	raid_retry = handle_ioaccel_mode2_error(h, c, cmd, c2);
1491a09c1441SScott Teel 	/* If error found, disable Smart Path, schedule a rescan,
1492a09c1441SScott Teel 	 * and force a retry on the standard path.
1493a09c1441SScott Teel 	 */
1494a09c1441SScott Teel 	if (raid_retry) {
1495a09c1441SScott Teel 		dev_warn(&h->pdev->dev, "%s: Retrying on standard path.\n",
1496a09c1441SScott Teel 			"HP SSD Smart Path");
1497a09c1441SScott Teel 		dev->offload_enabled = 0; /* Disable Smart Path */
1498a09c1441SScott Teel 		h->drv_req_rescan = 1;	  /* schedule controller rescan */
1499a09c1441SScott Teel 		cmd->result = DID_SOFT_ERROR << 16;
1500a09c1441SScott Teel 	}
1501c349775eSScott Teel 	cmd_free(h, c);
1502c349775eSScott Teel 	cmd->scsi_done(cmd);
1503c349775eSScott Teel }
1504c349775eSScott Teel 
15051fb011fbSStephen M. Cameron static void complete_scsi_command(struct CommandList *cp)
1506edd16368SStephen M. Cameron {
1507edd16368SStephen M. Cameron 	struct scsi_cmnd *cmd;
1508edd16368SStephen M. Cameron 	struct ctlr_info *h;
1509edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
1510283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
1511edd16368SStephen M. Cameron 
1512edd16368SStephen M. Cameron 	unsigned char sense_key;
1513edd16368SStephen M. Cameron 	unsigned char asc;      /* additional sense code */
1514edd16368SStephen M. Cameron 	unsigned char ascq;     /* additional sense code qualifier */
1515db111e18SStephen M. Cameron 	unsigned long sense_data_size;
1516edd16368SStephen M. Cameron 
1517edd16368SStephen M. Cameron 	ei = cp->err_info;
1518edd16368SStephen M. Cameron 	cmd = (struct scsi_cmnd *) cp->scsi_cmd;
1519edd16368SStephen M. Cameron 	h = cp->h;
1520283b4a9bSStephen M. Cameron 	dev = cmd->device->hostdata;
1521edd16368SStephen M. Cameron 
1522edd16368SStephen M. Cameron 	scsi_dma_unmap(cmd); /* undo the DMA mappings */
1523e1f7de0cSMatt Gates 	if ((cp->cmd_type == CMD_SCSI) &&
1524e1f7de0cSMatt Gates 		(cp->Header.SGTotal > h->max_cmd_sg_entries))
152533a2ffceSStephen M. Cameron 		hpsa_unmap_sg_chain_block(h, cp);
1526edd16368SStephen M. Cameron 
1527edd16368SStephen M. Cameron 	cmd->result = (DID_OK << 16); 		/* host byte */
1528edd16368SStephen M. Cameron 	cmd->result |= (COMMAND_COMPLETE << 8);	/* msg byte */
1529c349775eSScott Teel 
1530c349775eSScott Teel 	if (cp->cmd_type == CMD_IOACCEL2)
1531c349775eSScott Teel 		return process_ioaccel2_completion(h, cp, cmd, dev);
1532c349775eSScott Teel 
15335512672fSStephen M. Cameron 	cmd->result |= ei->ScsiStatus;
1534edd16368SStephen M. Cameron 
1535edd16368SStephen M. Cameron 	/* copy the sense data whether we need to or not. */
1536db111e18SStephen M. Cameron 	if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
1537db111e18SStephen M. Cameron 		sense_data_size = SCSI_SENSE_BUFFERSIZE;
1538db111e18SStephen M. Cameron 	else
1539db111e18SStephen M. Cameron 		sense_data_size = sizeof(ei->SenseInfo);
1540db111e18SStephen M. Cameron 	if (ei->SenseLen < sense_data_size)
1541db111e18SStephen M. Cameron 		sense_data_size = ei->SenseLen;
1542db111e18SStephen M. Cameron 
1543db111e18SStephen M. Cameron 	memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
1544edd16368SStephen M. Cameron 	scsi_set_resid(cmd, ei->ResidualCnt);
1545edd16368SStephen M. Cameron 
1546edd16368SStephen M. Cameron 	if (ei->CommandStatus == 0) {
1547edd16368SStephen M. Cameron 		cmd_free(h, cp);
15482cc5bfafSTomas Henzl 		cmd->scsi_done(cmd);
1549edd16368SStephen M. Cameron 		return;
1550edd16368SStephen M. Cameron 	}
1551edd16368SStephen M. Cameron 
1552e1f7de0cSMatt Gates 	/* For I/O accelerator commands, copy over some fields to the normal
1553e1f7de0cSMatt Gates 	 * CISS header used below for error handling.
1554e1f7de0cSMatt Gates 	 */
1555e1f7de0cSMatt Gates 	if (cp->cmd_type == CMD_IOACCEL1) {
1556e1f7de0cSMatt Gates 		struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
1557e1f7de0cSMatt Gates 		cp->Header.SGList = cp->Header.SGTotal = scsi_sg_count(cmd);
1558e1f7de0cSMatt Gates 		cp->Request.CDBLen = c->io_flags & IOACCEL1_IOFLAGS_CDBLEN_MASK;
1559e1f7de0cSMatt Gates 		cp->Header.Tag.lower = c->Tag.lower;
1560e1f7de0cSMatt Gates 		cp->Header.Tag.upper = c->Tag.upper;
1561e1f7de0cSMatt Gates 		memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
1562e1f7de0cSMatt Gates 		memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
1563283b4a9bSStephen M. Cameron 
1564283b4a9bSStephen M. Cameron 		/* Any RAID offload error results in retry which will use
1565283b4a9bSStephen M. Cameron 		 * the normal I/O path so the controller can handle whatever's
1566283b4a9bSStephen M. Cameron 		 * wrong.
1567283b4a9bSStephen M. Cameron 		 */
1568283b4a9bSStephen M. Cameron 		if (is_logical_dev_addr_mode(dev->scsi3addr)) {
1569283b4a9bSStephen M. Cameron 			if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
1570283b4a9bSStephen M. Cameron 				dev->offload_enabled = 0;
1571283b4a9bSStephen M. Cameron 			cmd->result = DID_SOFT_ERROR << 16;
1572283b4a9bSStephen M. Cameron 			cmd_free(h, cp);
1573283b4a9bSStephen M. Cameron 			cmd->scsi_done(cmd);
1574283b4a9bSStephen M. Cameron 			return;
1575283b4a9bSStephen M. Cameron 		}
1576e1f7de0cSMatt Gates 	}
1577e1f7de0cSMatt Gates 
1578edd16368SStephen M. Cameron 	/* an error has occurred */
1579edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
1580edd16368SStephen M. Cameron 
1581edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
1582edd16368SStephen M. Cameron 		if (ei->ScsiStatus) {
1583edd16368SStephen M. Cameron 			/* Get sense key */
1584edd16368SStephen M. Cameron 			sense_key = 0xf & ei->SenseInfo[2];
1585edd16368SStephen M. Cameron 			/* Get additional sense code */
1586edd16368SStephen M. Cameron 			asc = ei->SenseInfo[12];
1587edd16368SStephen M. Cameron 			/* Get addition sense code qualifier */
1588edd16368SStephen M. Cameron 			ascq = ei->SenseInfo[13];
1589edd16368SStephen M. Cameron 		}
1590edd16368SStephen M. Cameron 
1591edd16368SStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
15923ce438dfSMatt Gates 			if (check_for_unit_attention(h, cp))
1593edd16368SStephen M. Cameron 				break;
1594edd16368SStephen M. Cameron 			if (sense_key == ILLEGAL_REQUEST) {
1595edd16368SStephen M. Cameron 				/*
1596edd16368SStephen M. Cameron 				 * SCSI REPORT_LUNS is commonly unsupported on
1597edd16368SStephen M. Cameron 				 * Smart Array.  Suppress noisy complaint.
1598edd16368SStephen M. Cameron 				 */
1599edd16368SStephen M. Cameron 				if (cp->Request.CDB[0] == REPORT_LUNS)
1600edd16368SStephen M. Cameron 					break;
1601edd16368SStephen M. Cameron 
1602edd16368SStephen M. Cameron 				/* If ASC/ASCQ indicate Logical Unit
1603edd16368SStephen M. Cameron 				 * Not Supported condition,
1604edd16368SStephen M. Cameron 				 */
1605edd16368SStephen M. Cameron 				if ((asc == 0x25) && (ascq == 0x0)) {
1606edd16368SStephen M. Cameron 					dev_warn(&h->pdev->dev, "cp %p "
1607edd16368SStephen M. Cameron 						"has check condition\n", cp);
1608edd16368SStephen M. Cameron 					break;
1609edd16368SStephen M. Cameron 				}
1610edd16368SStephen M. Cameron 			}
1611edd16368SStephen M. Cameron 
1612edd16368SStephen M. Cameron 			if (sense_key == NOT_READY) {
1613edd16368SStephen M. Cameron 				/* If Sense is Not Ready, Logical Unit
1614edd16368SStephen M. Cameron 				 * Not ready, Manual Intervention
1615edd16368SStephen M. Cameron 				 * required
1616edd16368SStephen M. Cameron 				 */
1617edd16368SStephen M. Cameron 				if ((asc == 0x04) && (ascq == 0x03)) {
1618edd16368SStephen M. Cameron 					dev_warn(&h->pdev->dev, "cp %p "
1619edd16368SStephen M. Cameron 						"has check condition: unit "
1620edd16368SStephen M. Cameron 						"not ready, manual "
1621edd16368SStephen M. Cameron 						"intervention required\n", cp);
1622edd16368SStephen M. Cameron 					break;
1623edd16368SStephen M. Cameron 				}
1624edd16368SStephen M. Cameron 			}
16251d3b3609SMatt Gates 			if (sense_key == ABORTED_COMMAND) {
16261d3b3609SMatt Gates 				/* Aborted command is retryable */
16271d3b3609SMatt Gates 				dev_warn(&h->pdev->dev, "cp %p "
16281d3b3609SMatt Gates 					"has check condition: aborted command: "
16291d3b3609SMatt Gates 					"ASC: 0x%x, ASCQ: 0x%x\n",
16301d3b3609SMatt Gates 					cp, asc, ascq);
16312e311fbaSStephen M. Cameron 				cmd->result |= DID_SOFT_ERROR << 16;
16321d3b3609SMatt Gates 				break;
16331d3b3609SMatt Gates 			}
1634edd16368SStephen M. Cameron 			/* Must be some other type of check condition */
163521b8e4efSStephen M. Cameron 			dev_dbg(&h->pdev->dev, "cp %p has check condition: "
1636edd16368SStephen M. Cameron 					"unknown type: "
1637edd16368SStephen M. Cameron 					"Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1638edd16368SStephen M. Cameron 					"Returning result: 0x%x, "
1639edd16368SStephen M. Cameron 					"cmd=[%02x %02x %02x %02x %02x "
1640807be732SMike Miller 					"%02x %02x %02x %02x %02x %02x "
1641edd16368SStephen M. Cameron 					"%02x %02x %02x %02x %02x]\n",
1642edd16368SStephen M. Cameron 					cp, sense_key, asc, ascq,
1643edd16368SStephen M. Cameron 					cmd->result,
1644edd16368SStephen M. Cameron 					cmd->cmnd[0], cmd->cmnd[1],
1645edd16368SStephen M. Cameron 					cmd->cmnd[2], cmd->cmnd[3],
1646edd16368SStephen M. Cameron 					cmd->cmnd[4], cmd->cmnd[5],
1647edd16368SStephen M. Cameron 					cmd->cmnd[6], cmd->cmnd[7],
1648807be732SMike Miller 					cmd->cmnd[8], cmd->cmnd[9],
1649807be732SMike Miller 					cmd->cmnd[10], cmd->cmnd[11],
1650807be732SMike Miller 					cmd->cmnd[12], cmd->cmnd[13],
1651807be732SMike Miller 					cmd->cmnd[14], cmd->cmnd[15]);
1652edd16368SStephen M. Cameron 			break;
1653edd16368SStephen M. Cameron 		}
1654edd16368SStephen M. Cameron 
1655edd16368SStephen M. Cameron 
1656edd16368SStephen M. Cameron 		/* Problem was not a check condition
1657edd16368SStephen M. Cameron 		 * Pass it up to the upper layers...
1658edd16368SStephen M. Cameron 		 */
1659edd16368SStephen M. Cameron 		if (ei->ScsiStatus) {
1660edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
1661edd16368SStephen M. Cameron 				"Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1662edd16368SStephen M. Cameron 				"Returning result: 0x%x\n",
1663edd16368SStephen M. Cameron 				cp, ei->ScsiStatus,
1664edd16368SStephen M. Cameron 				sense_key, asc, ascq,
1665edd16368SStephen M. Cameron 				cmd->result);
1666edd16368SStephen M. Cameron 		} else {  /* scsi status is zero??? How??? */
1667edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
1668edd16368SStephen M. Cameron 				"Returning no connection.\n", cp),
1669edd16368SStephen M. Cameron 
1670edd16368SStephen M. Cameron 			/* Ordinarily, this case should never happen,
1671edd16368SStephen M. Cameron 			 * but there is a bug in some released firmware
1672edd16368SStephen M. Cameron 			 * revisions that allows it to happen if, for
1673edd16368SStephen M. Cameron 			 * example, a 4100 backplane loses power and
1674edd16368SStephen M. Cameron 			 * the tape drive is in it.  We assume that
1675edd16368SStephen M. Cameron 			 * it's a fatal error of some kind because we
1676edd16368SStephen M. Cameron 			 * can't show that it wasn't. We will make it
1677edd16368SStephen M. Cameron 			 * look like selection timeout since that is
1678edd16368SStephen M. Cameron 			 * the most common reason for this to occur,
1679edd16368SStephen M. Cameron 			 * and it's severe enough.
1680edd16368SStephen M. Cameron 			 */
1681edd16368SStephen M. Cameron 
1682edd16368SStephen M. Cameron 			cmd->result = DID_NO_CONNECT << 16;
1683edd16368SStephen M. Cameron 		}
1684edd16368SStephen M. Cameron 		break;
1685edd16368SStephen M. Cameron 
1686edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1687edd16368SStephen M. Cameron 		break;
1688edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
1689edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p has"
1690edd16368SStephen M. Cameron 			" completed with data overrun "
1691edd16368SStephen M. Cameron 			"reported\n", cp);
1692edd16368SStephen M. Cameron 		break;
1693edd16368SStephen M. Cameron 	case CMD_INVALID: {
1694edd16368SStephen M. Cameron 		/* print_bytes(cp, sizeof(*cp), 1, 0);
1695edd16368SStephen M. Cameron 		print_cmd(cp); */
1696edd16368SStephen M. Cameron 		/* We get CMD_INVALID if you address a non-existent device
1697edd16368SStephen M. Cameron 		 * instead of a selection timeout (no response).  You will
1698edd16368SStephen M. Cameron 		 * see this if you yank out a drive, then try to access it.
1699edd16368SStephen M. Cameron 		 * This is kind of a shame because it means that any other
1700edd16368SStephen M. Cameron 		 * CMD_INVALID (e.g. driver bug) will get interpreted as a
1701edd16368SStephen M. Cameron 		 * missing target. */
1702edd16368SStephen M. Cameron 		cmd->result = DID_NO_CONNECT << 16;
1703edd16368SStephen M. Cameron 	}
1704edd16368SStephen M. Cameron 		break;
1705edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
1706256d0eaaSStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1707edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p has "
1708edd16368SStephen M. Cameron 			"protocol error\n", cp);
1709edd16368SStephen M. Cameron 		break;
1710edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
1711edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1712edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p had  hardware error\n", cp);
1713edd16368SStephen M. Cameron 		break;
1714edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
1715edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1716edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
1717edd16368SStephen M. Cameron 		break;
1718edd16368SStephen M. Cameron 	case CMD_ABORTED:
1719edd16368SStephen M. Cameron 		cmd->result = DID_ABORT << 16;
1720edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
1721edd16368SStephen M. Cameron 				cp, ei->ScsiStatus);
1722edd16368SStephen M. Cameron 		break;
1723edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
1724edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1725edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
1726edd16368SStephen M. Cameron 		break;
1727edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
1728f6e76055SStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
1729f6e76055SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p aborted due to an unsolicited "
1730edd16368SStephen M. Cameron 			"abort\n", cp);
1731edd16368SStephen M. Cameron 		break;
1732edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
1733edd16368SStephen M. Cameron 		cmd->result = DID_TIME_OUT << 16;
1734edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
1735edd16368SStephen M. Cameron 		break;
17361d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
17371d5e2ed0SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
17381d5e2ed0SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Command unabortable\n");
17391d5e2ed0SStephen M. Cameron 		break;
1740283b4a9bSStephen M. Cameron 	case CMD_IOACCEL_DISABLED:
1741283b4a9bSStephen M. Cameron 		/* This only handles the direct pass-through case since RAID
1742283b4a9bSStephen M. Cameron 		 * offload is handled above.  Just attempt a retry.
1743283b4a9bSStephen M. Cameron 		 */
1744283b4a9bSStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16;
1745283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev,
1746283b4a9bSStephen M. Cameron 				"cp %p had HP SSD Smart Path error\n", cp);
1747283b4a9bSStephen M. Cameron 		break;
1748edd16368SStephen M. Cameron 	default:
1749edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1750edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
1751edd16368SStephen M. Cameron 				cp, ei->CommandStatus);
1752edd16368SStephen M. Cameron 	}
1753edd16368SStephen M. Cameron 	cmd_free(h, cp);
17542cc5bfafSTomas Henzl 	cmd->scsi_done(cmd);
1755edd16368SStephen M. Cameron }
1756edd16368SStephen M. Cameron 
1757edd16368SStephen M. Cameron static void hpsa_pci_unmap(struct pci_dev *pdev,
1758edd16368SStephen M. Cameron 	struct CommandList *c, int sg_used, int data_direction)
1759edd16368SStephen M. Cameron {
1760edd16368SStephen M. Cameron 	int i;
1761edd16368SStephen M. Cameron 	union u64bit addr64;
1762edd16368SStephen M. Cameron 
1763edd16368SStephen M. Cameron 	for (i = 0; i < sg_used; i++) {
1764edd16368SStephen M. Cameron 		addr64.val32.lower = c->SG[i].Addr.lower;
1765edd16368SStephen M. Cameron 		addr64.val32.upper = c->SG[i].Addr.upper;
1766edd16368SStephen M. Cameron 		pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len,
1767edd16368SStephen M. Cameron 			data_direction);
1768edd16368SStephen M. Cameron 	}
1769edd16368SStephen M. Cameron }
1770edd16368SStephen M. Cameron 
1771a2dac136SStephen M. Cameron static int hpsa_map_one(struct pci_dev *pdev,
1772edd16368SStephen M. Cameron 		struct CommandList *cp,
1773edd16368SStephen M. Cameron 		unsigned char *buf,
1774edd16368SStephen M. Cameron 		size_t buflen,
1775edd16368SStephen M. Cameron 		int data_direction)
1776edd16368SStephen M. Cameron {
177701a02ffcSStephen M. Cameron 	u64 addr64;
1778edd16368SStephen M. Cameron 
1779edd16368SStephen M. Cameron 	if (buflen == 0 || data_direction == PCI_DMA_NONE) {
1780edd16368SStephen M. Cameron 		cp->Header.SGList = 0;
1781edd16368SStephen M. Cameron 		cp->Header.SGTotal = 0;
1782a2dac136SStephen M. Cameron 		return 0;
1783edd16368SStephen M. Cameron 	}
1784edd16368SStephen M. Cameron 
178501a02ffcSStephen M. Cameron 	addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction);
1786eceaae18SShuah Khan 	if (dma_mapping_error(&pdev->dev, addr64)) {
1787a2dac136SStephen M. Cameron 		/* Prevent subsequent unmap of something never mapped */
1788eceaae18SShuah Khan 		cp->Header.SGList = 0;
1789eceaae18SShuah Khan 		cp->Header.SGTotal = 0;
1790a2dac136SStephen M. Cameron 		return -1;
1791eceaae18SShuah Khan 	}
1792edd16368SStephen M. Cameron 	cp->SG[0].Addr.lower =
179301a02ffcSStephen M. Cameron 	  (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
1794edd16368SStephen M. Cameron 	cp->SG[0].Addr.upper =
179501a02ffcSStephen M. Cameron 	  (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
1796edd16368SStephen M. Cameron 	cp->SG[0].Len = buflen;
1797e1d9cbfaSMatt Gates 	cp->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining */
179801a02ffcSStephen M. Cameron 	cp->Header.SGList = (u8) 1;   /* no. SGs contig in this cmd */
179901a02ffcSStephen M. Cameron 	cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */
1800a2dac136SStephen M. Cameron 	return 0;
1801edd16368SStephen M. Cameron }
1802edd16368SStephen M. Cameron 
1803edd16368SStephen M. Cameron static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
1804edd16368SStephen M. Cameron 	struct CommandList *c)
1805edd16368SStephen M. Cameron {
1806edd16368SStephen M. Cameron 	DECLARE_COMPLETION_ONSTACK(wait);
1807edd16368SStephen M. Cameron 
1808edd16368SStephen M. Cameron 	c->waiting = &wait;
1809edd16368SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
1810edd16368SStephen M. Cameron 	wait_for_completion(&wait);
1811edd16368SStephen M. Cameron }
1812edd16368SStephen M. Cameron 
1813a0c12413SStephen M. Cameron static void hpsa_scsi_do_simple_cmd_core_if_no_lockup(struct ctlr_info *h,
1814a0c12413SStephen M. Cameron 	struct CommandList *c)
1815a0c12413SStephen M. Cameron {
1816a0c12413SStephen M. Cameron 	unsigned long flags;
1817a0c12413SStephen M. Cameron 
1818a0c12413SStephen M. Cameron 	/* If controller lockup detected, fake a hardware error. */
1819a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
1820a0c12413SStephen M. Cameron 	if (unlikely(h->lockup_detected)) {
1821a0c12413SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
1822a0c12413SStephen M. Cameron 		c->err_info->CommandStatus = CMD_HARDWARE_ERR;
1823a0c12413SStephen M. Cameron 	} else {
1824a0c12413SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
1825a0c12413SStephen M. Cameron 		hpsa_scsi_do_simple_cmd_core(h, c);
1826a0c12413SStephen M. Cameron 	}
1827a0c12413SStephen M. Cameron }
1828a0c12413SStephen M. Cameron 
18299c2fc160SStephen M. Cameron #define MAX_DRIVER_CMD_RETRIES 25
1830edd16368SStephen M. Cameron static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
1831edd16368SStephen M. Cameron 	struct CommandList *c, int data_direction)
1832edd16368SStephen M. Cameron {
18339c2fc160SStephen M. Cameron 	int backoff_time = 10, retry_count = 0;
1834edd16368SStephen M. Cameron 
1835edd16368SStephen M. Cameron 	do {
18367630abd0SJoe Perches 		memset(c->err_info, 0, sizeof(*c->err_info));
1837edd16368SStephen M. Cameron 		hpsa_scsi_do_simple_cmd_core(h, c);
1838edd16368SStephen M. Cameron 		retry_count++;
18399c2fc160SStephen M. Cameron 		if (retry_count > 3) {
18409c2fc160SStephen M. Cameron 			msleep(backoff_time);
18419c2fc160SStephen M. Cameron 			if (backoff_time < 1000)
18429c2fc160SStephen M. Cameron 				backoff_time *= 2;
18439c2fc160SStephen M. Cameron 		}
1844852af20aSMatt Bondurant 	} while ((check_for_unit_attention(h, c) ||
18459c2fc160SStephen M. Cameron 			check_for_busy(h, c)) &&
18469c2fc160SStephen M. Cameron 			retry_count <= MAX_DRIVER_CMD_RETRIES);
1847edd16368SStephen M. Cameron 	hpsa_pci_unmap(h->pdev, c, 1, data_direction);
1848edd16368SStephen M. Cameron }
1849edd16368SStephen M. Cameron 
1850edd16368SStephen M. Cameron static void hpsa_scsi_interpret_error(struct CommandList *cp)
1851edd16368SStephen M. Cameron {
1852edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
1853edd16368SStephen M. Cameron 	struct device *d = &cp->h->pdev->dev;
1854edd16368SStephen M. Cameron 
1855edd16368SStephen M. Cameron 	ei = cp->err_info;
1856edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
1857edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
1858edd16368SStephen M. Cameron 		dev_warn(d, "cmd %p has completed with errors\n", cp);
1859edd16368SStephen M. Cameron 		dev_warn(d, "cmd %p has SCSI Status = %x\n", cp,
1860edd16368SStephen M. Cameron 				ei->ScsiStatus);
1861edd16368SStephen M. Cameron 		if (ei->ScsiStatus == 0)
1862edd16368SStephen M. Cameron 			dev_warn(d, "SCSI status is abnormally zero.  "
1863edd16368SStephen M. Cameron 			"(probably indicates selection timeout "
1864edd16368SStephen M. Cameron 			"reported incorrectly due to a known "
1865edd16368SStephen M. Cameron 			"firmware bug, circa July, 2001.)\n");
1866edd16368SStephen M. Cameron 		break;
1867edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1868edd16368SStephen M. Cameron 			dev_info(d, "UNDERRUN\n");
1869edd16368SStephen M. Cameron 		break;
1870edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
1871edd16368SStephen M. Cameron 		dev_warn(d, "cp %p has completed with data overrun\n", cp);
1872edd16368SStephen M. Cameron 		break;
1873edd16368SStephen M. Cameron 	case CMD_INVALID: {
1874edd16368SStephen M. Cameron 		/* controller unfortunately reports SCSI passthru's
1875edd16368SStephen M. Cameron 		 * to non-existent targets as invalid commands.
1876edd16368SStephen M. Cameron 		 */
1877edd16368SStephen M. Cameron 		dev_warn(d, "cp %p is reported invalid (probably means "
1878edd16368SStephen M. Cameron 			"target device no longer present)\n", cp);
1879edd16368SStephen M. Cameron 		/* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
1880edd16368SStephen M. Cameron 		print_cmd(cp);  */
1881edd16368SStephen M. Cameron 		}
1882edd16368SStephen M. Cameron 		break;
1883edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
1884edd16368SStephen M. Cameron 		dev_warn(d, "cp %p has protocol error \n", cp);
1885edd16368SStephen M. Cameron 		break;
1886edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
1887edd16368SStephen M. Cameron 		/* cmd->result = DID_ERROR << 16; */
1888edd16368SStephen M. Cameron 		dev_warn(d, "cp %p had hardware error\n", cp);
1889edd16368SStephen M. Cameron 		break;
1890edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
1891edd16368SStephen M. Cameron 		dev_warn(d, "cp %p had connection lost\n", cp);
1892edd16368SStephen M. Cameron 		break;
1893edd16368SStephen M. Cameron 	case CMD_ABORTED:
1894edd16368SStephen M. Cameron 		dev_warn(d, "cp %p was aborted\n", cp);
1895edd16368SStephen M. Cameron 		break;
1896edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
1897edd16368SStephen M. Cameron 		dev_warn(d, "cp %p reports abort failed\n", cp);
1898edd16368SStephen M. Cameron 		break;
1899edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
1900edd16368SStephen M. Cameron 		dev_warn(d, "cp %p aborted due to an unsolicited abort\n", cp);
1901edd16368SStephen M. Cameron 		break;
1902edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
1903edd16368SStephen M. Cameron 		dev_warn(d, "cp %p timed out\n", cp);
1904edd16368SStephen M. Cameron 		break;
19051d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
19061d5e2ed0SStephen M. Cameron 		dev_warn(d, "Command unabortable\n");
19071d5e2ed0SStephen M. Cameron 		break;
1908edd16368SStephen M. Cameron 	default:
1909edd16368SStephen M. Cameron 		dev_warn(d, "cp %p returned unknown status %x\n", cp,
1910edd16368SStephen M. Cameron 				ei->CommandStatus);
1911edd16368SStephen M. Cameron 	}
1912edd16368SStephen M. Cameron }
1913edd16368SStephen M. Cameron 
1914edd16368SStephen M. Cameron static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
1915b7bb24ebSStephen M. Cameron 			u16 page, unsigned char *buf,
1916edd16368SStephen M. Cameron 			unsigned char bufsize)
1917edd16368SStephen M. Cameron {
1918edd16368SStephen M. Cameron 	int rc = IO_OK;
1919edd16368SStephen M. Cameron 	struct CommandList *c;
1920edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
1921edd16368SStephen M. Cameron 
1922edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
1923edd16368SStephen M. Cameron 
1924edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
1925edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1926ecd9aad4SStephen M. Cameron 		return -ENOMEM;
1927edd16368SStephen M. Cameron 	}
1928edd16368SStephen M. Cameron 
1929a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
1930a2dac136SStephen M. Cameron 			page, scsi3addr, TYPE_CMD)) {
1931a2dac136SStephen M. Cameron 		rc = -1;
1932a2dac136SStephen M. Cameron 		goto out;
1933a2dac136SStephen M. Cameron 	}
1934edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1935edd16368SStephen M. Cameron 	ei = c->err_info;
1936edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
1937edd16368SStephen M. Cameron 		hpsa_scsi_interpret_error(c);
1938edd16368SStephen M. Cameron 		rc = -1;
1939edd16368SStephen M. Cameron 	}
1940a2dac136SStephen M. Cameron out:
1941edd16368SStephen M. Cameron 	cmd_special_free(h, c);
1942edd16368SStephen M. Cameron 	return rc;
1943edd16368SStephen M. Cameron }
1944edd16368SStephen M. Cameron 
1945bf711ac6SScott Teel static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
1946bf711ac6SScott Teel 	u8 reset_type)
1947edd16368SStephen M. Cameron {
1948edd16368SStephen M. Cameron 	int rc = IO_OK;
1949edd16368SStephen M. Cameron 	struct CommandList *c;
1950edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
1951edd16368SStephen M. Cameron 
1952edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
1953edd16368SStephen M. Cameron 
1954edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
1955edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1956e9ea04a6SStephen M. Cameron 		return -ENOMEM;
1957edd16368SStephen M. Cameron 	}
1958edd16368SStephen M. Cameron 
1959a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map. */
1960bf711ac6SScott Teel 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
1961bf711ac6SScott Teel 			scsi3addr, TYPE_MSG);
1962bf711ac6SScott Teel 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to LUN reset */
1963edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core(h, c);
1964edd16368SStephen M. Cameron 	/* no unmap needed here because no data xfer. */
1965edd16368SStephen M. Cameron 
1966edd16368SStephen M. Cameron 	ei = c->err_info;
1967edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0) {
1968edd16368SStephen M. Cameron 		hpsa_scsi_interpret_error(c);
1969edd16368SStephen M. Cameron 		rc = -1;
1970edd16368SStephen M. Cameron 	}
1971edd16368SStephen M. Cameron 	cmd_special_free(h, c);
1972edd16368SStephen M. Cameron 	return rc;
1973edd16368SStephen M. Cameron }
1974edd16368SStephen M. Cameron 
1975edd16368SStephen M. Cameron static void hpsa_get_raid_level(struct ctlr_info *h,
1976edd16368SStephen M. Cameron 	unsigned char *scsi3addr, unsigned char *raid_level)
1977edd16368SStephen M. Cameron {
1978edd16368SStephen M. Cameron 	int rc;
1979edd16368SStephen M. Cameron 	unsigned char *buf;
1980edd16368SStephen M. Cameron 
1981edd16368SStephen M. Cameron 	*raid_level = RAID_UNKNOWN;
1982edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
1983edd16368SStephen M. Cameron 	if (!buf)
1984edd16368SStephen M. Cameron 		return;
1985b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0xC1, buf, 64);
1986edd16368SStephen M. Cameron 	if (rc == 0)
1987edd16368SStephen M. Cameron 		*raid_level = buf[8];
1988edd16368SStephen M. Cameron 	if (*raid_level > RAID_UNKNOWN)
1989edd16368SStephen M. Cameron 		*raid_level = RAID_UNKNOWN;
1990edd16368SStephen M. Cameron 	kfree(buf);
1991edd16368SStephen M. Cameron 	return;
1992edd16368SStephen M. Cameron }
1993edd16368SStephen M. Cameron 
1994283b4a9bSStephen M. Cameron #define HPSA_MAP_DEBUG
1995283b4a9bSStephen M. Cameron #ifdef HPSA_MAP_DEBUG
1996283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
1997283b4a9bSStephen M. Cameron 				struct raid_map_data *map_buff)
1998283b4a9bSStephen M. Cameron {
1999283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map_buff->data[0];
2000283b4a9bSStephen M. Cameron 	int map, row, col;
2001283b4a9bSStephen M. Cameron 	u16 map_cnt, row_cnt, disks_per_row;
2002283b4a9bSStephen M. Cameron 
2003283b4a9bSStephen M. Cameron 	if (rc != 0)
2004283b4a9bSStephen M. Cameron 		return;
2005283b4a9bSStephen M. Cameron 
2006283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "structure_size = %u\n",
2007283b4a9bSStephen M. Cameron 				le32_to_cpu(map_buff->structure_size));
2008283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
2009283b4a9bSStephen M. Cameron 			le32_to_cpu(map_buff->volume_blk_size));
2010283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
2011283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->volume_blk_cnt));
2012283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
2013283b4a9bSStephen M. Cameron 			map_buff->phys_blk_shift);
2014283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
2015283b4a9bSStephen M. Cameron 			map_buff->parity_rotation_shift);
2016283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "strip_size = %u\n",
2017283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->strip_size));
2018283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
2019283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_starting_blk));
2020283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
2021283b4a9bSStephen M. Cameron 			le64_to_cpu(map_buff->disk_blk_cnt));
2022283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
2023283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->data_disks_per_row));
2024283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
2025283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->metadata_disks_per_row));
2026283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "row_cnt = %u\n",
2027283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->row_cnt));
2028283b4a9bSStephen M. Cameron 	dev_info(&h->pdev->dev, "layout_map_count = %u\n",
2029283b4a9bSStephen M. Cameron 			le16_to_cpu(map_buff->layout_map_count));
2030*dd0e19f3SScott Teel 	dev_info(&h->pdev->dev, "flags = %u\n",
2031*dd0e19f3SScott Teel 			le16_to_cpu(map_buff->flags));
2032*dd0e19f3SScott Teel 	if (map_buff->flags & RAID_MAP_FLAG_ENCRYPT_ON)
2033*dd0e19f3SScott Teel 		dev_info(&h->pdev->dev, "encrypytion = ON\n");
2034*dd0e19f3SScott Teel 	else
2035*dd0e19f3SScott Teel 		dev_info(&h->pdev->dev, "encrypytion = OFF\n");
2036*dd0e19f3SScott Teel 	dev_info(&h->pdev->dev, "dekindex = %u\n",
2037*dd0e19f3SScott Teel 			le16_to_cpu(map_buff->dekindex));
2038283b4a9bSStephen M. Cameron 
2039283b4a9bSStephen M. Cameron 	map_cnt = le16_to_cpu(map_buff->layout_map_count);
2040283b4a9bSStephen M. Cameron 	for (map = 0; map < map_cnt; map++) {
2041283b4a9bSStephen M. Cameron 		dev_info(&h->pdev->dev, "Map%u:\n", map);
2042283b4a9bSStephen M. Cameron 		row_cnt = le16_to_cpu(map_buff->row_cnt);
2043283b4a9bSStephen M. Cameron 		for (row = 0; row < row_cnt; row++) {
2044283b4a9bSStephen M. Cameron 			dev_info(&h->pdev->dev, "  Row%u:\n", row);
2045283b4a9bSStephen M. Cameron 			disks_per_row =
2046283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->data_disks_per_row);
2047283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2048283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2049283b4a9bSStephen M. Cameron 					"    D%02u: h=0x%04x xor=%u,%u\n",
2050283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2051283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2052283b4a9bSStephen M. Cameron 			disks_per_row =
2053283b4a9bSStephen M. Cameron 				le16_to_cpu(map_buff->metadata_disks_per_row);
2054283b4a9bSStephen M. Cameron 			for (col = 0; col < disks_per_row; col++, dd++)
2055283b4a9bSStephen M. Cameron 				dev_info(&h->pdev->dev,
2056283b4a9bSStephen M. Cameron 					"    M%02u: h=0x%04x xor=%u,%u\n",
2057283b4a9bSStephen M. Cameron 					col, dd->ioaccel_handle,
2058283b4a9bSStephen M. Cameron 					dd->xor_mult[0], dd->xor_mult[1]);
2059283b4a9bSStephen M. Cameron 		}
2060283b4a9bSStephen M. Cameron 	}
2061283b4a9bSStephen M. Cameron }
2062283b4a9bSStephen M. Cameron #else
2063283b4a9bSStephen M. Cameron static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
2064283b4a9bSStephen M. Cameron 			__attribute__((unused)) int rc,
2065283b4a9bSStephen M. Cameron 			__attribute__((unused)) struct raid_map_data *map_buff)
2066283b4a9bSStephen M. Cameron {
2067283b4a9bSStephen M. Cameron }
2068283b4a9bSStephen M. Cameron #endif
2069283b4a9bSStephen M. Cameron 
2070283b4a9bSStephen M. Cameron static int hpsa_get_raid_map(struct ctlr_info *h,
2071283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2072283b4a9bSStephen M. Cameron {
2073283b4a9bSStephen M. Cameron 	int rc = 0;
2074283b4a9bSStephen M. Cameron 	struct CommandList *c;
2075283b4a9bSStephen M. Cameron 	struct ErrorInfo *ei;
2076283b4a9bSStephen M. Cameron 
2077283b4a9bSStephen M. Cameron 	c = cmd_special_alloc(h);
2078283b4a9bSStephen M. Cameron 	if (c == NULL) {
2079283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2080283b4a9bSStephen M. Cameron 		return -ENOMEM;
2081283b4a9bSStephen M. Cameron 	}
2082283b4a9bSStephen M. Cameron 	if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
2083283b4a9bSStephen M. Cameron 			sizeof(this_device->raid_map), 0,
2084283b4a9bSStephen M. Cameron 			scsi3addr, TYPE_CMD)) {
2085283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "Out of memory in hpsa_get_raid_map()\n");
2086283b4a9bSStephen M. Cameron 		cmd_special_free(h, c);
2087283b4a9bSStephen M. Cameron 		return -ENOMEM;
2088283b4a9bSStephen M. Cameron 	}
2089283b4a9bSStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2090283b4a9bSStephen M. Cameron 	ei = c->err_info;
2091283b4a9bSStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2092283b4a9bSStephen M. Cameron 		hpsa_scsi_interpret_error(c);
2093283b4a9bSStephen M. Cameron 		cmd_special_free(h, c);
2094283b4a9bSStephen M. Cameron 		return -1;
2095283b4a9bSStephen M. Cameron 	}
2096283b4a9bSStephen M. Cameron 	cmd_special_free(h, c);
2097283b4a9bSStephen M. Cameron 
2098283b4a9bSStephen M. Cameron 	/* @todo in the future, dynamically allocate RAID map memory */
2099283b4a9bSStephen M. Cameron 	if (le32_to_cpu(this_device->raid_map.structure_size) >
2100283b4a9bSStephen M. Cameron 				sizeof(this_device->raid_map)) {
2101283b4a9bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
2102283b4a9bSStephen M. Cameron 		rc = -1;
2103283b4a9bSStephen M. Cameron 	}
2104283b4a9bSStephen M. Cameron 	hpsa_debug_map_buff(h, rc, &this_device->raid_map);
2105283b4a9bSStephen M. Cameron 	return rc;
2106283b4a9bSStephen M. Cameron }
2107283b4a9bSStephen M. Cameron 
21081b70150aSStephen M. Cameron static int hpsa_vpd_page_supported(struct ctlr_info *h,
21091b70150aSStephen M. Cameron 	unsigned char scsi3addr[], u8 page)
21101b70150aSStephen M. Cameron {
21111b70150aSStephen M. Cameron 	int rc;
21121b70150aSStephen M. Cameron 	int i;
21131b70150aSStephen M. Cameron 	int pages;
21141b70150aSStephen M. Cameron 	unsigned char *buf, bufsize;
21151b70150aSStephen M. Cameron 
21161b70150aSStephen M. Cameron 	buf = kzalloc(256, GFP_KERNEL);
21171b70150aSStephen M. Cameron 	if (!buf)
21181b70150aSStephen M. Cameron 		return 0;
21191b70150aSStephen M. Cameron 
21201b70150aSStephen M. Cameron 	/* Get the size of the page list first */
21211b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
21221b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
21231b70150aSStephen M. Cameron 				buf, HPSA_VPD_HEADER_SZ);
21241b70150aSStephen M. Cameron 	if (rc != 0)
21251b70150aSStephen M. Cameron 		goto exit_unsupported;
21261b70150aSStephen M. Cameron 	pages = buf[3];
21271b70150aSStephen M. Cameron 	if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
21281b70150aSStephen M. Cameron 		bufsize = pages + HPSA_VPD_HEADER_SZ;
21291b70150aSStephen M. Cameron 	else
21301b70150aSStephen M. Cameron 		bufsize = 255;
21311b70150aSStephen M. Cameron 
21321b70150aSStephen M. Cameron 	/* Get the whole VPD page list */
21331b70150aSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
21341b70150aSStephen M. Cameron 				VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
21351b70150aSStephen M. Cameron 				buf, bufsize);
21361b70150aSStephen M. Cameron 	if (rc != 0)
21371b70150aSStephen M. Cameron 		goto exit_unsupported;
21381b70150aSStephen M. Cameron 
21391b70150aSStephen M. Cameron 	pages = buf[3];
21401b70150aSStephen M. Cameron 	for (i = 1; i <= pages; i++)
21411b70150aSStephen M. Cameron 		if (buf[3 + i] == page)
21421b70150aSStephen M. Cameron 			goto exit_supported;
21431b70150aSStephen M. Cameron exit_unsupported:
21441b70150aSStephen M. Cameron 	kfree(buf);
21451b70150aSStephen M. Cameron 	return 0;
21461b70150aSStephen M. Cameron exit_supported:
21471b70150aSStephen M. Cameron 	kfree(buf);
21481b70150aSStephen M. Cameron 	return 1;
21491b70150aSStephen M. Cameron }
21501b70150aSStephen M. Cameron 
2151283b4a9bSStephen M. Cameron static void hpsa_get_ioaccel_status(struct ctlr_info *h,
2152283b4a9bSStephen M. Cameron 	unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2153283b4a9bSStephen M. Cameron {
2154283b4a9bSStephen M. Cameron 	int rc;
2155283b4a9bSStephen M. Cameron 	unsigned char *buf;
2156283b4a9bSStephen M. Cameron 	u8 ioaccel_status;
2157283b4a9bSStephen M. Cameron 
2158283b4a9bSStephen M. Cameron 	this_device->offload_config = 0;
2159283b4a9bSStephen M. Cameron 	this_device->offload_enabled = 0;
2160283b4a9bSStephen M. Cameron 
2161283b4a9bSStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2162283b4a9bSStephen M. Cameron 	if (!buf)
2163283b4a9bSStephen M. Cameron 		return;
21641b70150aSStephen M. Cameron 	if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
21651b70150aSStephen M. Cameron 		goto out;
2166283b4a9bSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr,
2167b7bb24ebSStephen M. Cameron 			VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
2168283b4a9bSStephen M. Cameron 	if (rc != 0)
2169283b4a9bSStephen M. Cameron 		goto out;
2170283b4a9bSStephen M. Cameron 
2171283b4a9bSStephen M. Cameron #define IOACCEL_STATUS_BYTE 4
2172283b4a9bSStephen M. Cameron #define OFFLOAD_CONFIGURED_BIT 0x01
2173283b4a9bSStephen M. Cameron #define OFFLOAD_ENABLED_BIT 0x02
2174283b4a9bSStephen M. Cameron 	ioaccel_status = buf[IOACCEL_STATUS_BYTE];
2175283b4a9bSStephen M. Cameron 	this_device->offload_config =
2176283b4a9bSStephen M. Cameron 		!!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
2177283b4a9bSStephen M. Cameron 	if (this_device->offload_config) {
2178283b4a9bSStephen M. Cameron 		this_device->offload_enabled =
2179283b4a9bSStephen M. Cameron 			!!(ioaccel_status & OFFLOAD_ENABLED_BIT);
2180283b4a9bSStephen M. Cameron 		if (hpsa_get_raid_map(h, scsi3addr, this_device))
2181283b4a9bSStephen M. Cameron 			this_device->offload_enabled = 0;
2182283b4a9bSStephen M. Cameron 	}
2183283b4a9bSStephen M. Cameron out:
2184283b4a9bSStephen M. Cameron 	kfree(buf);
2185283b4a9bSStephen M. Cameron 	return;
2186283b4a9bSStephen M. Cameron }
2187283b4a9bSStephen M. Cameron 
2188edd16368SStephen M. Cameron /* Get the device id from inquiry page 0x83 */
2189edd16368SStephen M. Cameron static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
2190edd16368SStephen M. Cameron 	unsigned char *device_id, int buflen)
2191edd16368SStephen M. Cameron {
2192edd16368SStephen M. Cameron 	int rc;
2193edd16368SStephen M. Cameron 	unsigned char *buf;
2194edd16368SStephen M. Cameron 
2195edd16368SStephen M. Cameron 	if (buflen > 16)
2196edd16368SStephen M. Cameron 		buflen = 16;
2197edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
2198edd16368SStephen M. Cameron 	if (!buf)
2199edd16368SStephen M. Cameron 		return -1;
2200b7bb24ebSStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0x83, buf, 64);
2201edd16368SStephen M. Cameron 	if (rc == 0)
2202edd16368SStephen M. Cameron 		memcpy(device_id, &buf[8], buflen);
2203edd16368SStephen M. Cameron 	kfree(buf);
2204edd16368SStephen M. Cameron 	return rc != 0;
2205edd16368SStephen M. Cameron }
2206edd16368SStephen M. Cameron 
2207edd16368SStephen M. Cameron static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
2208edd16368SStephen M. Cameron 		struct ReportLUNdata *buf, int bufsize,
2209edd16368SStephen M. Cameron 		int extended_response)
2210edd16368SStephen M. Cameron {
2211edd16368SStephen M. Cameron 	int rc = IO_OK;
2212edd16368SStephen M. Cameron 	struct CommandList *c;
2213edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
2214edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
2215edd16368SStephen M. Cameron 
2216edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
2217edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
2218edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2219edd16368SStephen M. Cameron 		return -1;
2220edd16368SStephen M. Cameron 	}
2221e89c0ae7SStephen M. Cameron 	/* address the controller */
2222e89c0ae7SStephen M. Cameron 	memset(scsi3addr, 0, sizeof(scsi3addr));
2223a2dac136SStephen M. Cameron 	if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
2224a2dac136SStephen M. Cameron 		buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
2225a2dac136SStephen M. Cameron 		rc = -1;
2226a2dac136SStephen M. Cameron 		goto out;
2227a2dac136SStephen M. Cameron 	}
2228edd16368SStephen M. Cameron 	if (extended_response)
2229edd16368SStephen M. Cameron 		c->Request.CDB[1] = extended_response;
2230edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2231edd16368SStephen M. Cameron 	ei = c->err_info;
2232edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 &&
2233edd16368SStephen M. Cameron 	    ei->CommandStatus != CMD_DATA_UNDERRUN) {
2234edd16368SStephen M. Cameron 		hpsa_scsi_interpret_error(c);
2235edd16368SStephen M. Cameron 		rc = -1;
2236283b4a9bSStephen M. Cameron 	} else {
2237283b4a9bSStephen M. Cameron 		if (buf->extended_response_flag != extended_response) {
2238283b4a9bSStephen M. Cameron 			dev_err(&h->pdev->dev,
2239283b4a9bSStephen M. Cameron 				"report luns requested format %u, got %u\n",
2240283b4a9bSStephen M. Cameron 				extended_response,
2241283b4a9bSStephen M. Cameron 				buf->extended_response_flag);
2242283b4a9bSStephen M. Cameron 			rc = -1;
2243283b4a9bSStephen M. Cameron 		}
2244edd16368SStephen M. Cameron 	}
2245a2dac136SStephen M. Cameron out:
2246edd16368SStephen M. Cameron 	cmd_special_free(h, c);
2247edd16368SStephen M. Cameron 	return rc;
2248edd16368SStephen M. Cameron }
2249edd16368SStephen M. Cameron 
2250edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
2251edd16368SStephen M. Cameron 		struct ReportLUNdata *buf,
2252edd16368SStephen M. Cameron 		int bufsize, int extended_response)
2253edd16368SStephen M. Cameron {
2254edd16368SStephen M. Cameron 	return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
2255edd16368SStephen M. Cameron }
2256edd16368SStephen M. Cameron 
2257edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
2258edd16368SStephen M. Cameron 		struct ReportLUNdata *buf, int bufsize)
2259edd16368SStephen M. Cameron {
2260edd16368SStephen M. Cameron 	return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
2261edd16368SStephen M. Cameron }
2262edd16368SStephen M. Cameron 
2263edd16368SStephen M. Cameron static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
2264edd16368SStephen M. Cameron 	int bus, int target, int lun)
2265edd16368SStephen M. Cameron {
2266edd16368SStephen M. Cameron 	device->bus = bus;
2267edd16368SStephen M. Cameron 	device->target = target;
2268edd16368SStephen M. Cameron 	device->lun = lun;
2269edd16368SStephen M. Cameron }
2270edd16368SStephen M. Cameron 
2271edd16368SStephen M. Cameron static int hpsa_update_device_info(struct ctlr_info *h,
22720b0e1d6cSStephen M. Cameron 	unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
22730b0e1d6cSStephen M. Cameron 	unsigned char *is_OBDR_device)
2274edd16368SStephen M. Cameron {
22750b0e1d6cSStephen M. Cameron 
22760b0e1d6cSStephen M. Cameron #define OBDR_SIG_OFFSET 43
22770b0e1d6cSStephen M. Cameron #define OBDR_TAPE_SIG "$DR-10"
22780b0e1d6cSStephen M. Cameron #define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
22790b0e1d6cSStephen M. Cameron #define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
22800b0e1d6cSStephen M. Cameron 
2281ea6d3bc3SStephen M. Cameron 	unsigned char *inq_buff;
22820b0e1d6cSStephen M. Cameron 	unsigned char *obdr_sig;
2283edd16368SStephen M. Cameron 
2284ea6d3bc3SStephen M. Cameron 	inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
2285edd16368SStephen M. Cameron 	if (!inq_buff)
2286edd16368SStephen M. Cameron 		goto bail_out;
2287edd16368SStephen M. Cameron 
2288edd16368SStephen M. Cameron 	/* Do an inquiry to the device to see what it is. */
2289edd16368SStephen M. Cameron 	if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
2290edd16368SStephen M. Cameron 		(unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
2291edd16368SStephen M. Cameron 		/* Inquiry failed (msg printed already) */
2292edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev,
2293edd16368SStephen M. Cameron 			"hpsa_update_device_info: inquiry failed\n");
2294edd16368SStephen M. Cameron 		goto bail_out;
2295edd16368SStephen M. Cameron 	}
2296edd16368SStephen M. Cameron 
2297edd16368SStephen M. Cameron 	this_device->devtype = (inq_buff[0] & 0x1f);
2298edd16368SStephen M. Cameron 	memcpy(this_device->scsi3addr, scsi3addr, 8);
2299edd16368SStephen M. Cameron 	memcpy(this_device->vendor, &inq_buff[8],
2300edd16368SStephen M. Cameron 		sizeof(this_device->vendor));
2301edd16368SStephen M. Cameron 	memcpy(this_device->model, &inq_buff[16],
2302edd16368SStephen M. Cameron 		sizeof(this_device->model));
2303edd16368SStephen M. Cameron 	memset(this_device->device_id, 0,
2304edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
2305edd16368SStephen M. Cameron 	hpsa_get_device_id(h, scsi3addr, this_device->device_id,
2306edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
2307edd16368SStephen M. Cameron 
2308edd16368SStephen M. Cameron 	if (this_device->devtype == TYPE_DISK &&
2309283b4a9bSStephen M. Cameron 		is_logical_dev_addr_mode(scsi3addr)) {
2310edd16368SStephen M. Cameron 		hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
2311283b4a9bSStephen M. Cameron 		if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
2312283b4a9bSStephen M. Cameron 			hpsa_get_ioaccel_status(h, scsi3addr, this_device);
2313283b4a9bSStephen M. Cameron 	} else {
2314edd16368SStephen M. Cameron 		this_device->raid_level = RAID_UNKNOWN;
2315283b4a9bSStephen M. Cameron 		this_device->offload_config = 0;
2316283b4a9bSStephen M. Cameron 		this_device->offload_enabled = 0;
2317283b4a9bSStephen M. Cameron 	}
2318edd16368SStephen M. Cameron 
23190b0e1d6cSStephen M. Cameron 	if (is_OBDR_device) {
23200b0e1d6cSStephen M. Cameron 		/* See if this is a One-Button-Disaster-Recovery device
23210b0e1d6cSStephen M. Cameron 		 * by looking for "$DR-10" at offset 43 in inquiry data.
23220b0e1d6cSStephen M. Cameron 		 */
23230b0e1d6cSStephen M. Cameron 		obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
23240b0e1d6cSStephen M. Cameron 		*is_OBDR_device = (this_device->devtype == TYPE_ROM &&
23250b0e1d6cSStephen M. Cameron 					strncmp(obdr_sig, OBDR_TAPE_SIG,
23260b0e1d6cSStephen M. Cameron 						OBDR_SIG_LEN) == 0);
23270b0e1d6cSStephen M. Cameron 	}
23280b0e1d6cSStephen M. Cameron 
2329edd16368SStephen M. Cameron 	kfree(inq_buff);
2330edd16368SStephen M. Cameron 	return 0;
2331edd16368SStephen M. Cameron 
2332edd16368SStephen M. Cameron bail_out:
2333edd16368SStephen M. Cameron 	kfree(inq_buff);
2334edd16368SStephen M. Cameron 	return 1;
2335edd16368SStephen M. Cameron }
2336edd16368SStephen M. Cameron 
23374f4eb9f1SScott Teel static unsigned char *ext_target_model[] = {
2338edd16368SStephen M. Cameron 	"MSA2012",
2339edd16368SStephen M. Cameron 	"MSA2024",
2340edd16368SStephen M. Cameron 	"MSA2312",
2341edd16368SStephen M. Cameron 	"MSA2324",
2342fda38518SStephen M. Cameron 	"P2000 G3 SAS",
2343e06c8e5cSStephen M. Cameron 	"MSA 2040 SAS",
2344edd16368SStephen M. Cameron 	NULL,
2345edd16368SStephen M. Cameron };
2346edd16368SStephen M. Cameron 
23474f4eb9f1SScott Teel static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
2348edd16368SStephen M. Cameron {
2349edd16368SStephen M. Cameron 	int i;
2350edd16368SStephen M. Cameron 
23514f4eb9f1SScott Teel 	for (i = 0; ext_target_model[i]; i++)
23524f4eb9f1SScott Teel 		if (strncmp(device->model, ext_target_model[i],
23534f4eb9f1SScott Teel 			strlen(ext_target_model[i])) == 0)
2354edd16368SStephen M. Cameron 			return 1;
2355edd16368SStephen M. Cameron 	return 0;
2356edd16368SStephen M. Cameron }
2357edd16368SStephen M. Cameron 
2358edd16368SStephen M. Cameron /* Helper function to assign bus, target, lun mapping of devices.
23594f4eb9f1SScott Teel  * Puts non-external target logical volumes on bus 0, external target logical
2360edd16368SStephen M. Cameron  * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
2361edd16368SStephen M. Cameron  * Logical drive target and lun are assigned at this time, but
2362edd16368SStephen M. Cameron  * physical device lun and target assignment are deferred (assigned
2363edd16368SStephen M. Cameron  * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
2364edd16368SStephen M. Cameron  */
2365edd16368SStephen M. Cameron static void figure_bus_target_lun(struct ctlr_info *h,
23661f310bdeSStephen M. Cameron 	u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
2367edd16368SStephen M. Cameron {
23681f310bdeSStephen M. Cameron 	u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
2369edd16368SStephen M. Cameron 
23701f310bdeSStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes)) {
23711f310bdeSStephen M. Cameron 		/* physical device, target and lun filled in later */
23721f310bdeSStephen M. Cameron 		if (is_hba_lunid(lunaddrbytes))
23731f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
23741f310bdeSStephen M. Cameron 		else
23751f310bdeSStephen M. Cameron 			/* defer target, lun assignment for physical devices */
23761f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 2, -1, -1);
23771f310bdeSStephen M. Cameron 		return;
23781f310bdeSStephen M. Cameron 	}
23791f310bdeSStephen M. Cameron 	/* It's a logical device */
23804f4eb9f1SScott Teel 	if (is_ext_target(h, device)) {
23814f4eb9f1SScott Teel 		/* external target way, put logicals on bus 1
2382339b2b14SStephen M. Cameron 		 * and match target/lun numbers box
23831f310bdeSStephen M. Cameron 		 * reports, other smart array, bus 0, target 0, match lunid
2384339b2b14SStephen M. Cameron 		 */
23851f310bdeSStephen M. Cameron 		hpsa_set_bus_target_lun(device,
23861f310bdeSStephen M. Cameron 			1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
23871f310bdeSStephen M. Cameron 		return;
2388339b2b14SStephen M. Cameron 	}
23891f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
2390edd16368SStephen M. Cameron }
2391edd16368SStephen M. Cameron 
2392edd16368SStephen M. Cameron /*
2393edd16368SStephen M. Cameron  * If there is no lun 0 on a target, linux won't find any devices.
23944f4eb9f1SScott Teel  * For the external targets (arrays), we have to manually detect the enclosure
2395edd16368SStephen M. Cameron  * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
2396edd16368SStephen M. Cameron  * it for some reason.  *tmpdevice is the target we're adding,
2397edd16368SStephen M. Cameron  * this_device is a pointer into the current element of currentsd[]
2398edd16368SStephen M. Cameron  * that we're building up in update_scsi_devices(), below.
2399edd16368SStephen M. Cameron  * lunzerobits is a bitmap that tracks which targets already have a
2400edd16368SStephen M. Cameron  * lun 0 assigned.
2401edd16368SStephen M. Cameron  * Returns 1 if an enclosure was added, 0 if not.
2402edd16368SStephen M. Cameron  */
24034f4eb9f1SScott Teel static int add_ext_target_dev(struct ctlr_info *h,
2404edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *tmpdevice,
240501a02ffcSStephen M. Cameron 	struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
24064f4eb9f1SScott Teel 	unsigned long lunzerobits[], int *n_ext_target_devs)
2407edd16368SStephen M. Cameron {
2408edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
2409edd16368SStephen M. Cameron 
24101f310bdeSStephen M. Cameron 	if (test_bit(tmpdevice->target, lunzerobits))
2411edd16368SStephen M. Cameron 		return 0; /* There is already a lun 0 on this target. */
2412edd16368SStephen M. Cameron 
2413edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes))
2414edd16368SStephen M. Cameron 		return 0; /* It's the logical targets that may lack lun 0. */
2415edd16368SStephen M. Cameron 
24164f4eb9f1SScott Teel 	if (!is_ext_target(h, tmpdevice))
24174f4eb9f1SScott Teel 		return 0; /* Only external target devices have this problem. */
2418edd16368SStephen M. Cameron 
24191f310bdeSStephen M. Cameron 	if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
2420edd16368SStephen M. Cameron 		return 0;
2421edd16368SStephen M. Cameron 
2422c4f8a299SStephen M. Cameron 	memset(scsi3addr, 0, 8);
24231f310bdeSStephen M. Cameron 	scsi3addr[3] = tmpdevice->target;
2424edd16368SStephen M. Cameron 	if (is_hba_lunid(scsi3addr))
2425edd16368SStephen M. Cameron 		return 0; /* Don't add the RAID controller here. */
2426edd16368SStephen M. Cameron 
2427339b2b14SStephen M. Cameron 	if (is_scsi_rev_5(h))
2428339b2b14SStephen M. Cameron 		return 0; /* p1210m doesn't need to do this. */
2429339b2b14SStephen M. Cameron 
24304f4eb9f1SScott Teel 	if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
2431aca4a520SScott Teel 		dev_warn(&h->pdev->dev, "Maximum number of external "
2432aca4a520SScott Teel 			"target devices exceeded.  Check your hardware "
2433edd16368SStephen M. Cameron 			"configuration.");
2434edd16368SStephen M. Cameron 		return 0;
2435edd16368SStephen M. Cameron 	}
2436edd16368SStephen M. Cameron 
24370b0e1d6cSStephen M. Cameron 	if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
2438edd16368SStephen M. Cameron 		return 0;
24394f4eb9f1SScott Teel 	(*n_ext_target_devs)++;
24401f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(this_device,
24411f310bdeSStephen M. Cameron 				tmpdevice->bus, tmpdevice->target, 0);
24421f310bdeSStephen M. Cameron 	set_bit(tmpdevice->target, lunzerobits);
2443edd16368SStephen M. Cameron 	return 1;
2444edd16368SStephen M. Cameron }
2445edd16368SStephen M. Cameron 
2446edd16368SStephen M. Cameron /*
244754b6e9e9SScott Teel  * Get address of physical disk used for an ioaccel2 mode command:
244854b6e9e9SScott Teel  *	1. Extract ioaccel2 handle from the command.
244954b6e9e9SScott Teel  *	2. Find a matching ioaccel2 handle from list of physical disks.
245054b6e9e9SScott Teel  *	3. Return:
245154b6e9e9SScott Teel  *		1 and set scsi3addr to address of matching physical
245254b6e9e9SScott Teel  *		0 if no matching physical disk was found.
245354b6e9e9SScott Teel  */
245454b6e9e9SScott Teel static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
245554b6e9e9SScott Teel 	struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
245654b6e9e9SScott Teel {
245754b6e9e9SScott Teel 	struct ReportExtendedLUNdata *physicals = NULL;
245854b6e9e9SScott Teel 	int responsesize = 24;	/* size of physical extended response */
245954b6e9e9SScott Teel 	int extended = 2;	/* flag forces reporting 'other dev info'. */
246054b6e9e9SScott Teel 	int reportsize = sizeof(*physicals) + HPSA_MAX_PHYS_LUN * responsesize;
246154b6e9e9SScott Teel 	u32 nphysicals = 0;	/* number of reported physical devs */
246254b6e9e9SScott Teel 	int found = 0;		/* found match (1) or not (0) */
246354b6e9e9SScott Teel 	u32 find;		/* handle we need to match */
246454b6e9e9SScott Teel 	int i;
246554b6e9e9SScott Teel 	struct scsi_cmnd *scmd;	/* scsi command within request being aborted */
246654b6e9e9SScott Teel 	struct hpsa_scsi_dev_t *d; /* device of request being aborted */
246754b6e9e9SScott Teel 	struct io_accel2_cmd *c2a; /* ioaccel2 command to abort */
246854b6e9e9SScott Teel 	u32 it_nexus;		/* 4 byte device handle for the ioaccel2 cmd */
246954b6e9e9SScott Teel 	u32 scsi_nexus;		/* 4 byte device handle for the ioaccel2 cmd */
247054b6e9e9SScott Teel 
247154b6e9e9SScott Teel 	if (ioaccel2_cmd_to_abort->cmd_type != CMD_IOACCEL2)
247254b6e9e9SScott Teel 		return 0; /* no match */
247354b6e9e9SScott Teel 
247454b6e9e9SScott Teel 	/* point to the ioaccel2 device handle */
247554b6e9e9SScott Teel 	c2a = &h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
247654b6e9e9SScott Teel 	if (c2a == NULL)
247754b6e9e9SScott Teel 		return 0; /* no match */
247854b6e9e9SScott Teel 
247954b6e9e9SScott Teel 	scmd = (struct scsi_cmnd *) ioaccel2_cmd_to_abort->scsi_cmd;
248054b6e9e9SScott Teel 	if (scmd == NULL)
248154b6e9e9SScott Teel 		return 0; /* no match */
248254b6e9e9SScott Teel 
248354b6e9e9SScott Teel 	d = scmd->device->hostdata;
248454b6e9e9SScott Teel 	if (d == NULL)
248554b6e9e9SScott Teel 		return 0; /* no match */
248654b6e9e9SScott Teel 
248754b6e9e9SScott Teel 	it_nexus = cpu_to_le32((u32) d->ioaccel_handle);
248854b6e9e9SScott Teel 	scsi_nexus = cpu_to_le32((u32) c2a->scsi_nexus);
248954b6e9e9SScott Teel 	find = c2a->scsi_nexus;
249054b6e9e9SScott Teel 
249154b6e9e9SScott Teel 	/* Get the list of physical devices */
249254b6e9e9SScott Teel 	physicals = kzalloc(reportsize, GFP_KERNEL);
249354b6e9e9SScott Teel 	if (hpsa_scsi_do_report_phys_luns(h, (struct ReportLUNdata *) physicals,
249454b6e9e9SScott Teel 		reportsize, extended)) {
249554b6e9e9SScott Teel 		dev_err(&h->pdev->dev,
249654b6e9e9SScott Teel 			"Can't lookup %s device handle: report physical LUNs failed.\n",
249754b6e9e9SScott Teel 			"HP SSD Smart Path");
249854b6e9e9SScott Teel 		kfree(physicals);
249954b6e9e9SScott Teel 		return 0;
250054b6e9e9SScott Teel 	}
250154b6e9e9SScott Teel 	nphysicals = be32_to_cpu(*((__be32 *)physicals->LUNListLength)) /
250254b6e9e9SScott Teel 							responsesize;
250354b6e9e9SScott Teel 
250454b6e9e9SScott Teel 
250554b6e9e9SScott Teel 	/* find ioaccel2 handle in list of physicals: */
250654b6e9e9SScott Teel 	for (i = 0; i < nphysicals; i++) {
250754b6e9e9SScott Teel 		/* handle is in bytes 28-31 of each lun */
250854b6e9e9SScott Teel 		if (memcmp(&((struct ReportExtendedLUNdata *)
250954b6e9e9SScott Teel 				physicals)->LUN[i][20], &find, 4) != 0) {
251054b6e9e9SScott Teel 			continue; /* didn't match */
251154b6e9e9SScott Teel 		}
251254b6e9e9SScott Teel 		found = 1;
251354b6e9e9SScott Teel 		memcpy(scsi3addr, &((struct ReportExtendedLUNdata *)
251454b6e9e9SScott Teel 					physicals)->LUN[i][0], 8);
251554b6e9e9SScott Teel 		break; /* found it */
251654b6e9e9SScott Teel 	}
251754b6e9e9SScott Teel 
251854b6e9e9SScott Teel 	kfree(physicals);
251954b6e9e9SScott Teel 	if (found)
252054b6e9e9SScott Teel 		return 1;
252154b6e9e9SScott Teel 	else
252254b6e9e9SScott Teel 		return 0;
252354b6e9e9SScott Teel 
252454b6e9e9SScott Teel }
252554b6e9e9SScott Teel /*
2526edd16368SStephen M. Cameron  * Do CISS_REPORT_PHYS and CISS_REPORT_LOG.  Data is returned in physdev,
2527edd16368SStephen M. Cameron  * logdev.  The number of luns in physdev and logdev are returned in
2528edd16368SStephen M. Cameron  * *nphysicals and *nlogicals, respectively.
2529edd16368SStephen M. Cameron  * Returns 0 on success, -1 otherwise.
2530edd16368SStephen M. Cameron  */
2531edd16368SStephen M. Cameron static int hpsa_gather_lun_info(struct ctlr_info *h,
2532edd16368SStephen M. Cameron 	int reportlunsize,
2533283b4a9bSStephen M. Cameron 	struct ReportLUNdata *physdev, u32 *nphysicals, int *physical_mode,
253401a02ffcSStephen M. Cameron 	struct ReportLUNdata *logdev, u32 *nlogicals)
2535edd16368SStephen M. Cameron {
2536283b4a9bSStephen M. Cameron 	int physical_entry_size = 8;
2537283b4a9bSStephen M. Cameron 
2538283b4a9bSStephen M. Cameron 	*physical_mode = 0;
2539283b4a9bSStephen M. Cameron 
2540283b4a9bSStephen M. Cameron 	/* For I/O accelerator mode we need to read physical device handles */
2541317d4adfSMike MIller 	if (h->transMethod & CFGTBL_Trans_io_accel1 ||
2542317d4adfSMike MIller 		h->transMethod & CFGTBL_Trans_io_accel2) {
2543283b4a9bSStephen M. Cameron 		*physical_mode = HPSA_REPORT_PHYS_EXTENDED;
2544283b4a9bSStephen M. Cameron 		physical_entry_size = 24;
2545283b4a9bSStephen M. Cameron 	}
2546a93aa1feSMatt Gates 	if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize,
2547283b4a9bSStephen M. Cameron 							*physical_mode)) {
2548edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
2549edd16368SStephen M. Cameron 		return -1;
2550edd16368SStephen M. Cameron 	}
2551283b4a9bSStephen M. Cameron 	*nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) /
2552283b4a9bSStephen M. Cameron 							physical_entry_size;
2553edd16368SStephen M. Cameron 	if (*nphysicals > HPSA_MAX_PHYS_LUN) {
2554edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
2555edd16368SStephen M. Cameron 			"  %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2556edd16368SStephen M. Cameron 			*nphysicals - HPSA_MAX_PHYS_LUN);
2557edd16368SStephen M. Cameron 		*nphysicals = HPSA_MAX_PHYS_LUN;
2558edd16368SStephen M. Cameron 	}
2559edd16368SStephen M. Cameron 	if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
2560edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
2561edd16368SStephen M. Cameron 		return -1;
2562edd16368SStephen M. Cameron 	}
25636df1e954SStephen M. Cameron 	*nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
2564edd16368SStephen M. Cameron 	/* Reject Logicals in excess of our max capability. */
2565edd16368SStephen M. Cameron 	if (*nlogicals > HPSA_MAX_LUN) {
2566edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
2567edd16368SStephen M. Cameron 			"maximum logical LUNs (%d) exceeded.  "
2568edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_LUN,
2569edd16368SStephen M. Cameron 			*nlogicals - HPSA_MAX_LUN);
2570edd16368SStephen M. Cameron 			*nlogicals = HPSA_MAX_LUN;
2571edd16368SStephen M. Cameron 	}
2572edd16368SStephen M. Cameron 	if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
2573edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
2574edd16368SStephen M. Cameron 			"maximum logical + physical LUNs (%d) exceeded. "
2575edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2576edd16368SStephen M. Cameron 			*nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
2577edd16368SStephen M. Cameron 		*nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
2578edd16368SStephen M. Cameron 	}
2579edd16368SStephen M. Cameron 	return 0;
2580edd16368SStephen M. Cameron }
2581edd16368SStephen M. Cameron 
2582339b2b14SStephen M. Cameron u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i,
2583a93aa1feSMatt Gates 	int nphysicals, int nlogicals,
2584a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list,
2585339b2b14SStephen M. Cameron 	struct ReportLUNdata *logdev_list)
2586339b2b14SStephen M. Cameron {
2587339b2b14SStephen M. Cameron 	/* Helper function, figure out where the LUN ID info is coming from
2588339b2b14SStephen M. Cameron 	 * given index i, lists of physical and logical devices, where in
2589339b2b14SStephen M. Cameron 	 * the list the raid controller is supposed to appear (first or last)
2590339b2b14SStephen M. Cameron 	 */
2591339b2b14SStephen M. Cameron 
2592339b2b14SStephen M. Cameron 	int logicals_start = nphysicals + (raid_ctlr_position == 0);
2593339b2b14SStephen M. Cameron 	int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
2594339b2b14SStephen M. Cameron 
2595339b2b14SStephen M. Cameron 	if (i == raid_ctlr_position)
2596339b2b14SStephen M. Cameron 		return RAID_CTLR_LUNID;
2597339b2b14SStephen M. Cameron 
2598339b2b14SStephen M. Cameron 	if (i < logicals_start)
2599339b2b14SStephen M. Cameron 		return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0];
2600339b2b14SStephen M. Cameron 
2601339b2b14SStephen M. Cameron 	if (i < last_device)
2602339b2b14SStephen M. Cameron 		return &logdev_list->LUN[i - nphysicals -
2603339b2b14SStephen M. Cameron 			(raid_ctlr_position == 0)][0];
2604339b2b14SStephen M. Cameron 	BUG();
2605339b2b14SStephen M. Cameron 	return NULL;
2606339b2b14SStephen M. Cameron }
2607339b2b14SStephen M. Cameron 
2608edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
2609edd16368SStephen M. Cameron {
2610edd16368SStephen M. Cameron 	/* the idea here is we could get notified
2611edd16368SStephen M. Cameron 	 * that some devices have changed, so we do a report
2612edd16368SStephen M. Cameron 	 * physical luns and report logical luns cmd, and adjust
2613edd16368SStephen M. Cameron 	 * our list of devices accordingly.
2614edd16368SStephen M. Cameron 	 *
2615edd16368SStephen M. Cameron 	 * The scsi3addr's of devices won't change so long as the
2616edd16368SStephen M. Cameron 	 * adapter is not reset.  That means we can rescan and
2617edd16368SStephen M. Cameron 	 * tell which devices we already know about, vs. new
2618edd16368SStephen M. Cameron 	 * devices, vs.  disappearing devices.
2619edd16368SStephen M. Cameron 	 */
2620a93aa1feSMatt Gates 	struct ReportExtendedLUNdata *physdev_list = NULL;
2621edd16368SStephen M. Cameron 	struct ReportLUNdata *logdev_list = NULL;
262201a02ffcSStephen M. Cameron 	u32 nphysicals = 0;
262301a02ffcSStephen M. Cameron 	u32 nlogicals = 0;
2624283b4a9bSStephen M. Cameron 	int physical_mode = 0;
262501a02ffcSStephen M. Cameron 	u32 ndev_allocated = 0;
2626edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
2627edd16368SStephen M. Cameron 	int ncurrent = 0;
2628283b4a9bSStephen M. Cameron 	int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 24;
26294f4eb9f1SScott Teel 	int i, n_ext_target_devs, ndevs_to_allocate;
2630339b2b14SStephen M. Cameron 	int raid_ctlr_position;
2631aca4a520SScott Teel 	DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
2632edd16368SStephen M. Cameron 
2633cfe5badcSScott Teel 	currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
2634edd16368SStephen M. Cameron 	physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
2635edd16368SStephen M. Cameron 	logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
2636edd16368SStephen M. Cameron 	tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
2637edd16368SStephen M. Cameron 
26380b0e1d6cSStephen M. Cameron 	if (!currentsd || !physdev_list || !logdev_list || !tmpdevice) {
2639edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory\n");
2640edd16368SStephen M. Cameron 		goto out;
2641edd16368SStephen M. Cameron 	}
2642edd16368SStephen M. Cameron 	memset(lunzerobits, 0, sizeof(lunzerobits));
2643edd16368SStephen M. Cameron 
2644a93aa1feSMatt Gates 	if (hpsa_gather_lun_info(h, reportlunsize,
2645a93aa1feSMatt Gates 			(struct ReportLUNdata *) physdev_list, &nphysicals,
2646283b4a9bSStephen M. Cameron 			&physical_mode, logdev_list, &nlogicals))
2647edd16368SStephen M. Cameron 		goto out;
2648edd16368SStephen M. Cameron 
2649aca4a520SScott Teel 	/* We might see up to the maximum number of logical and physical disks
2650aca4a520SScott Teel 	 * plus external target devices, and a device for the local RAID
2651aca4a520SScott Teel 	 * controller.
2652edd16368SStephen M. Cameron 	 */
2653aca4a520SScott Teel 	ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
2654edd16368SStephen M. Cameron 
2655edd16368SStephen M. Cameron 	/* Allocate the per device structures */
2656edd16368SStephen M. Cameron 	for (i = 0; i < ndevs_to_allocate; i++) {
2657b7ec021fSScott Teel 		if (i >= HPSA_MAX_DEVICES) {
2658b7ec021fSScott Teel 			dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
2659b7ec021fSScott Teel 				"  %d devices ignored.\n", HPSA_MAX_DEVICES,
2660b7ec021fSScott Teel 				ndevs_to_allocate - HPSA_MAX_DEVICES);
2661b7ec021fSScott Teel 			break;
2662b7ec021fSScott Teel 		}
2663b7ec021fSScott Teel 
2664edd16368SStephen M. Cameron 		currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
2665edd16368SStephen M. Cameron 		if (!currentsd[i]) {
2666edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
2667edd16368SStephen M. Cameron 				__FILE__, __LINE__);
2668edd16368SStephen M. Cameron 			goto out;
2669edd16368SStephen M. Cameron 		}
2670edd16368SStephen M. Cameron 		ndev_allocated++;
2671edd16368SStephen M. Cameron 	}
2672edd16368SStephen M. Cameron 
2673339b2b14SStephen M. Cameron 	if (unlikely(is_scsi_rev_5(h)))
2674339b2b14SStephen M. Cameron 		raid_ctlr_position = 0;
2675339b2b14SStephen M. Cameron 	else
2676339b2b14SStephen M. Cameron 		raid_ctlr_position = nphysicals + nlogicals;
2677339b2b14SStephen M. Cameron 
2678edd16368SStephen M. Cameron 	/* adjust our table of devices */
26794f4eb9f1SScott Teel 	n_ext_target_devs = 0;
2680edd16368SStephen M. Cameron 	for (i = 0; i < nphysicals + nlogicals + 1; i++) {
26810b0e1d6cSStephen M. Cameron 		u8 *lunaddrbytes, is_OBDR = 0;
2682edd16368SStephen M. Cameron 
2683edd16368SStephen M. Cameron 		/* Figure out where the LUN ID info is coming from */
2684339b2b14SStephen M. Cameron 		lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
2685339b2b14SStephen M. Cameron 			i, nphysicals, nlogicals, physdev_list, logdev_list);
2686edd16368SStephen M. Cameron 		/* skip masked physical devices. */
2687339b2b14SStephen M. Cameron 		if (lunaddrbytes[3] & 0xC0 &&
2688339b2b14SStephen M. Cameron 			i < nphysicals + (raid_ctlr_position == 0))
2689edd16368SStephen M. Cameron 			continue;
2690edd16368SStephen M. Cameron 
2691edd16368SStephen M. Cameron 		/* Get device type, vendor, model, device id */
26920b0e1d6cSStephen M. Cameron 		if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
26930b0e1d6cSStephen M. Cameron 							&is_OBDR))
2694edd16368SStephen M. Cameron 			continue; /* skip it if we can't talk to it. */
26951f310bdeSStephen M. Cameron 		figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
2696edd16368SStephen M. Cameron 		this_device = currentsd[ncurrent];
2697edd16368SStephen M. Cameron 
2698edd16368SStephen M. Cameron 		/*
26994f4eb9f1SScott Teel 		 * For external target devices, we have to insert a LUN 0 which
2700edd16368SStephen M. Cameron 		 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
2701edd16368SStephen M. Cameron 		 * is nonetheless an enclosure device there.  We have to
2702edd16368SStephen M. Cameron 		 * present that otherwise linux won't find anything if
2703edd16368SStephen M. Cameron 		 * there is no lun 0.
2704edd16368SStephen M. Cameron 		 */
27054f4eb9f1SScott Teel 		if (add_ext_target_dev(h, tmpdevice, this_device,
27061f310bdeSStephen M. Cameron 				lunaddrbytes, lunzerobits,
27074f4eb9f1SScott Teel 				&n_ext_target_devs)) {
2708edd16368SStephen M. Cameron 			ncurrent++;
2709edd16368SStephen M. Cameron 			this_device = currentsd[ncurrent];
2710edd16368SStephen M. Cameron 		}
2711edd16368SStephen M. Cameron 
2712edd16368SStephen M. Cameron 		*this_device = *tmpdevice;
2713edd16368SStephen M. Cameron 
2714edd16368SStephen M. Cameron 		switch (this_device->devtype) {
27150b0e1d6cSStephen M. Cameron 		case TYPE_ROM:
2716edd16368SStephen M. Cameron 			/* We don't *really* support actual CD-ROM devices,
2717edd16368SStephen M. Cameron 			 * just "One Button Disaster Recovery" tape drive
2718edd16368SStephen M. Cameron 			 * which temporarily pretends to be a CD-ROM drive.
2719edd16368SStephen M. Cameron 			 * So we check that the device is really an OBDR tape
2720edd16368SStephen M. Cameron 			 * device by checking for "$DR-10" in bytes 43-48 of
2721edd16368SStephen M. Cameron 			 * the inquiry data.
2722edd16368SStephen M. Cameron 			 */
27230b0e1d6cSStephen M. Cameron 			if (is_OBDR)
2724edd16368SStephen M. Cameron 				ncurrent++;
2725edd16368SStephen M. Cameron 			break;
2726edd16368SStephen M. Cameron 		case TYPE_DISK:
2727283b4a9bSStephen M. Cameron 			if (i >= nphysicals) {
2728283b4a9bSStephen M. Cameron 				ncurrent++;
2729edd16368SStephen M. Cameron 				break;
2730283b4a9bSStephen M. Cameron 			}
2731283b4a9bSStephen M. Cameron 			if (physical_mode == HPSA_REPORT_PHYS_EXTENDED) {
2732e1f7de0cSMatt Gates 				memcpy(&this_device->ioaccel_handle,
2733e1f7de0cSMatt Gates 					&lunaddrbytes[20],
2734e1f7de0cSMatt Gates 					sizeof(this_device->ioaccel_handle));
2735edd16368SStephen M. Cameron 				ncurrent++;
2736283b4a9bSStephen M. Cameron 			}
2737edd16368SStephen M. Cameron 			break;
2738edd16368SStephen M. Cameron 		case TYPE_TAPE:
2739edd16368SStephen M. Cameron 		case TYPE_MEDIUM_CHANGER:
2740edd16368SStephen M. Cameron 			ncurrent++;
2741edd16368SStephen M. Cameron 			break;
2742edd16368SStephen M. Cameron 		case TYPE_RAID:
2743edd16368SStephen M. Cameron 			/* Only present the Smartarray HBA as a RAID controller.
2744edd16368SStephen M. Cameron 			 * If it's a RAID controller other than the HBA itself
2745edd16368SStephen M. Cameron 			 * (an external RAID controller, MSA500 or similar)
2746edd16368SStephen M. Cameron 			 * don't present it.
2747edd16368SStephen M. Cameron 			 */
2748edd16368SStephen M. Cameron 			if (!is_hba_lunid(lunaddrbytes))
2749edd16368SStephen M. Cameron 				break;
2750edd16368SStephen M. Cameron 			ncurrent++;
2751edd16368SStephen M. Cameron 			break;
2752edd16368SStephen M. Cameron 		default:
2753edd16368SStephen M. Cameron 			break;
2754edd16368SStephen M. Cameron 		}
2755cfe5badcSScott Teel 		if (ncurrent >= HPSA_MAX_DEVICES)
2756edd16368SStephen M. Cameron 			break;
2757edd16368SStephen M. Cameron 	}
2758edd16368SStephen M. Cameron 	adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
2759edd16368SStephen M. Cameron out:
2760edd16368SStephen M. Cameron 	kfree(tmpdevice);
2761edd16368SStephen M. Cameron 	for (i = 0; i < ndev_allocated; i++)
2762edd16368SStephen M. Cameron 		kfree(currentsd[i]);
2763edd16368SStephen M. Cameron 	kfree(currentsd);
2764edd16368SStephen M. Cameron 	kfree(physdev_list);
2765edd16368SStephen M. Cameron 	kfree(logdev_list);
2766edd16368SStephen M. Cameron }
2767edd16368SStephen M. Cameron 
2768edd16368SStephen M. Cameron /* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
2769edd16368SStephen M. Cameron  * dma mapping  and fills in the scatter gather entries of the
2770edd16368SStephen M. Cameron  * hpsa command, cp.
2771edd16368SStephen M. Cameron  */
277233a2ffceSStephen M. Cameron static int hpsa_scatter_gather(struct ctlr_info *h,
2773edd16368SStephen M. Cameron 		struct CommandList *cp,
2774edd16368SStephen M. Cameron 		struct scsi_cmnd *cmd)
2775edd16368SStephen M. Cameron {
2776edd16368SStephen M. Cameron 	unsigned int len;
2777edd16368SStephen M. Cameron 	struct scatterlist *sg;
277801a02ffcSStephen M. Cameron 	u64 addr64;
277933a2ffceSStephen M. Cameron 	int use_sg, i, sg_index, chained;
278033a2ffceSStephen M. Cameron 	struct SGDescriptor *curr_sg;
2781edd16368SStephen M. Cameron 
278233a2ffceSStephen M. Cameron 	BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
2783edd16368SStephen M. Cameron 
2784edd16368SStephen M. Cameron 	use_sg = scsi_dma_map(cmd);
2785edd16368SStephen M. Cameron 	if (use_sg < 0)
2786edd16368SStephen M. Cameron 		return use_sg;
2787edd16368SStephen M. Cameron 
2788edd16368SStephen M. Cameron 	if (!use_sg)
2789edd16368SStephen M. Cameron 		goto sglist_finished;
2790edd16368SStephen M. Cameron 
279133a2ffceSStephen M. Cameron 	curr_sg = cp->SG;
279233a2ffceSStephen M. Cameron 	chained = 0;
279333a2ffceSStephen M. Cameron 	sg_index = 0;
2794edd16368SStephen M. Cameron 	scsi_for_each_sg(cmd, sg, use_sg, i) {
279533a2ffceSStephen M. Cameron 		if (i == h->max_cmd_sg_entries - 1 &&
279633a2ffceSStephen M. Cameron 			use_sg > h->max_cmd_sg_entries) {
279733a2ffceSStephen M. Cameron 			chained = 1;
279833a2ffceSStephen M. Cameron 			curr_sg = h->cmd_sg_list[cp->cmdindex];
279933a2ffceSStephen M. Cameron 			sg_index = 0;
280033a2ffceSStephen M. Cameron 		}
280101a02ffcSStephen M. Cameron 		addr64 = (u64) sg_dma_address(sg);
2802edd16368SStephen M. Cameron 		len  = sg_dma_len(sg);
280333a2ffceSStephen M. Cameron 		curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
280433a2ffceSStephen M. Cameron 		curr_sg->Addr.upper = (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
280533a2ffceSStephen M. Cameron 		curr_sg->Len = len;
2806e1d9cbfaSMatt Gates 		curr_sg->Ext = (i < scsi_sg_count(cmd) - 1) ? 0 : HPSA_SG_LAST;
280733a2ffceSStephen M. Cameron 		curr_sg++;
280833a2ffceSStephen M. Cameron 	}
280933a2ffceSStephen M. Cameron 
281033a2ffceSStephen M. Cameron 	if (use_sg + chained > h->maxSG)
281133a2ffceSStephen M. Cameron 		h->maxSG = use_sg + chained;
281233a2ffceSStephen M. Cameron 
281333a2ffceSStephen M. Cameron 	if (chained) {
281433a2ffceSStephen M. Cameron 		cp->Header.SGList = h->max_cmd_sg_entries;
281533a2ffceSStephen M. Cameron 		cp->Header.SGTotal = (u16) (use_sg + 1);
2816e2bea6dfSStephen M. Cameron 		if (hpsa_map_sg_chain_block(h, cp)) {
2817e2bea6dfSStephen M. Cameron 			scsi_dma_unmap(cmd);
2818e2bea6dfSStephen M. Cameron 			return -1;
2819e2bea6dfSStephen M. Cameron 		}
282033a2ffceSStephen M. Cameron 		return 0;
2821edd16368SStephen M. Cameron 	}
2822edd16368SStephen M. Cameron 
2823edd16368SStephen M. Cameron sglist_finished:
2824edd16368SStephen M. Cameron 
282501a02ffcSStephen M. Cameron 	cp->Header.SGList = (u8) use_sg;   /* no. SGs contig in this cmd */
282601a02ffcSStephen M. Cameron 	cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */
2827edd16368SStephen M. Cameron 	return 0;
2828edd16368SStephen M. Cameron }
2829edd16368SStephen M. Cameron 
2830283b4a9bSStephen M. Cameron #define IO_ACCEL_INELIGIBLE (1)
2831283b4a9bSStephen M. Cameron static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
2832283b4a9bSStephen M. Cameron {
2833283b4a9bSStephen M. Cameron 	int is_write = 0;
2834283b4a9bSStephen M. Cameron 	u32 block;
2835283b4a9bSStephen M. Cameron 	u32 block_cnt;
2836283b4a9bSStephen M. Cameron 
2837283b4a9bSStephen M. Cameron 	/* Perform some CDB fixups if needed using 10 byte reads/writes only */
2838283b4a9bSStephen M. Cameron 	switch (cdb[0]) {
2839283b4a9bSStephen M. Cameron 	case WRITE_6:
2840283b4a9bSStephen M. Cameron 	case WRITE_12:
2841283b4a9bSStephen M. Cameron 		is_write = 1;
2842283b4a9bSStephen M. Cameron 	case READ_6:
2843283b4a9bSStephen M. Cameron 	case READ_12:
2844283b4a9bSStephen M. Cameron 		if (*cdb_len == 6) {
2845283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 8) | cdb[3];
2846283b4a9bSStephen M. Cameron 			block_cnt = cdb[4];
2847283b4a9bSStephen M. Cameron 		} else {
2848283b4a9bSStephen M. Cameron 			BUG_ON(*cdb_len != 12);
2849283b4a9bSStephen M. Cameron 			block = (((u32) cdb[2]) << 24) |
2850283b4a9bSStephen M. Cameron 				(((u32) cdb[3]) << 16) |
2851283b4a9bSStephen M. Cameron 				(((u32) cdb[4]) << 8) |
2852283b4a9bSStephen M. Cameron 				cdb[5];
2853283b4a9bSStephen M. Cameron 			block_cnt =
2854283b4a9bSStephen M. Cameron 				(((u32) cdb[6]) << 24) |
2855283b4a9bSStephen M. Cameron 				(((u32) cdb[7]) << 16) |
2856283b4a9bSStephen M. Cameron 				(((u32) cdb[8]) << 8) |
2857283b4a9bSStephen M. Cameron 				cdb[9];
2858283b4a9bSStephen M. Cameron 		}
2859283b4a9bSStephen M. Cameron 		if (block_cnt > 0xffff)
2860283b4a9bSStephen M. Cameron 			return IO_ACCEL_INELIGIBLE;
2861283b4a9bSStephen M. Cameron 
2862283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
2863283b4a9bSStephen M. Cameron 		cdb[1] = 0;
2864283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (block >> 24);
2865283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (block >> 16);
2866283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (block >> 8);
2867283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (block);
2868283b4a9bSStephen M. Cameron 		cdb[6] = 0;
2869283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (block_cnt >> 8);
2870283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (block_cnt);
2871283b4a9bSStephen M. Cameron 		cdb[9] = 0;
2872283b4a9bSStephen M. Cameron 		*cdb_len = 10;
2873283b4a9bSStephen M. Cameron 		break;
2874283b4a9bSStephen M. Cameron 	}
2875283b4a9bSStephen M. Cameron 	return 0;
2876283b4a9bSStephen M. Cameron }
2877283b4a9bSStephen M. Cameron 
2878c349775eSScott Teel static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
2879283b4a9bSStephen M. Cameron 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
2880283b4a9bSStephen M. Cameron 	u8 *scsi3addr)
2881e1f7de0cSMatt Gates {
2882e1f7de0cSMatt Gates 	struct scsi_cmnd *cmd = c->scsi_cmd;
2883e1f7de0cSMatt Gates 	struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
2884e1f7de0cSMatt Gates 	unsigned int len;
2885e1f7de0cSMatt Gates 	unsigned int total_len = 0;
2886e1f7de0cSMatt Gates 	struct scatterlist *sg;
2887e1f7de0cSMatt Gates 	u64 addr64;
2888e1f7de0cSMatt Gates 	int use_sg, i;
2889e1f7de0cSMatt Gates 	struct SGDescriptor *curr_sg;
2890e1f7de0cSMatt Gates 	u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
2891e1f7de0cSMatt Gates 
2892283b4a9bSStephen M. Cameron 	/* TODO: implement chaining support */
2893283b4a9bSStephen M. Cameron 	if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
2894283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
2895283b4a9bSStephen M. Cameron 
2896e1f7de0cSMatt Gates 	BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
2897e1f7de0cSMatt Gates 
2898283b4a9bSStephen M. Cameron 	if (fixup_ioaccel_cdb(cdb, &cdb_len))
2899283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
2900283b4a9bSStephen M. Cameron 
2901e1f7de0cSMatt Gates 	c->cmd_type = CMD_IOACCEL1;
2902e1f7de0cSMatt Gates 
2903e1f7de0cSMatt Gates 	/* Adjust the DMA address to point to the accelerated command buffer */
2904e1f7de0cSMatt Gates 	c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
2905e1f7de0cSMatt Gates 				(c->cmdindex * sizeof(*cp));
2906e1f7de0cSMatt Gates 	BUG_ON(c->busaddr & 0x0000007F);
2907e1f7de0cSMatt Gates 
2908e1f7de0cSMatt Gates 	use_sg = scsi_dma_map(cmd);
2909e1f7de0cSMatt Gates 	if (use_sg < 0)
2910e1f7de0cSMatt Gates 		return use_sg;
2911e1f7de0cSMatt Gates 
2912e1f7de0cSMatt Gates 	if (use_sg) {
2913e1f7de0cSMatt Gates 		curr_sg = cp->SG;
2914e1f7de0cSMatt Gates 		scsi_for_each_sg(cmd, sg, use_sg, i) {
2915e1f7de0cSMatt Gates 			addr64 = (u64) sg_dma_address(sg);
2916e1f7de0cSMatt Gates 			len  = sg_dma_len(sg);
2917e1f7de0cSMatt Gates 			total_len += len;
2918e1f7de0cSMatt Gates 			curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
2919e1f7de0cSMatt Gates 			curr_sg->Addr.upper =
2920e1f7de0cSMatt Gates 				(u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
2921e1f7de0cSMatt Gates 			curr_sg->Len = len;
2922e1f7de0cSMatt Gates 
2923e1f7de0cSMatt Gates 			if (i == (scsi_sg_count(cmd) - 1))
2924e1f7de0cSMatt Gates 				curr_sg->Ext = HPSA_SG_LAST;
2925e1f7de0cSMatt Gates 			else
2926e1f7de0cSMatt Gates 				curr_sg->Ext = 0;  /* we are not chaining */
2927e1f7de0cSMatt Gates 			curr_sg++;
2928e1f7de0cSMatt Gates 		}
2929e1f7de0cSMatt Gates 
2930e1f7de0cSMatt Gates 		switch (cmd->sc_data_direction) {
2931e1f7de0cSMatt Gates 		case DMA_TO_DEVICE:
2932e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_OUT;
2933e1f7de0cSMatt Gates 			break;
2934e1f7de0cSMatt Gates 		case DMA_FROM_DEVICE:
2935e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_DATA_IN;
2936e1f7de0cSMatt Gates 			break;
2937e1f7de0cSMatt Gates 		case DMA_NONE:
2938e1f7de0cSMatt Gates 			control |= IOACCEL1_CONTROL_NODATAXFER;
2939e1f7de0cSMatt Gates 			break;
2940e1f7de0cSMatt Gates 		default:
2941e1f7de0cSMatt Gates 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
2942e1f7de0cSMatt Gates 			cmd->sc_data_direction);
2943e1f7de0cSMatt Gates 			BUG();
2944e1f7de0cSMatt Gates 			break;
2945e1f7de0cSMatt Gates 		}
2946e1f7de0cSMatt Gates 	} else {
2947e1f7de0cSMatt Gates 		control |= IOACCEL1_CONTROL_NODATAXFER;
2948e1f7de0cSMatt Gates 	}
2949e1f7de0cSMatt Gates 
2950c349775eSScott Teel 	c->Header.SGList = use_sg;
2951e1f7de0cSMatt Gates 	/* Fill out the command structure to submit */
2952283b4a9bSStephen M. Cameron 	cp->dev_handle = ioaccel_handle & 0xFFFF;
2953e1f7de0cSMatt Gates 	cp->transfer_len = total_len;
2954e1f7de0cSMatt Gates 	cp->io_flags = IOACCEL1_IOFLAGS_IO_REQ |
2955283b4a9bSStephen M. Cameron 			(cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK);
2956e1f7de0cSMatt Gates 	cp->control = control;
2957283b4a9bSStephen M. Cameron 	memcpy(cp->CDB, cdb, cdb_len);
2958283b4a9bSStephen M. Cameron 	memcpy(cp->CISS_LUN, scsi3addr, 8);
2959c349775eSScott Teel 	/* Tag was already set at init time. */
2960e1f7de0cSMatt Gates 	enqueue_cmd_and_start_io(h, c);
2961e1f7de0cSMatt Gates 	return 0;
2962e1f7de0cSMatt Gates }
2963edd16368SStephen M. Cameron 
2964283b4a9bSStephen M. Cameron /*
2965283b4a9bSStephen M. Cameron  * Queue a command directly to a device behind the controller using the
2966283b4a9bSStephen M. Cameron  * I/O accelerator path.
2967283b4a9bSStephen M. Cameron  */
2968283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
2969283b4a9bSStephen M. Cameron 	struct CommandList *c)
2970283b4a9bSStephen M. Cameron {
2971283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
2972283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
2973283b4a9bSStephen M. Cameron 
2974283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
2975283b4a9bSStephen M. Cameron 		cmd->cmnd, cmd->cmd_len, dev->scsi3addr);
2976283b4a9bSStephen M. Cameron }
2977283b4a9bSStephen M. Cameron 
2978*dd0e19f3SScott Teel /*
2979*dd0e19f3SScott Teel  * Set encryption parameters for the ioaccel2 request
2980*dd0e19f3SScott Teel  */
2981*dd0e19f3SScott Teel static void set_encrypt_ioaccel2(struct ctlr_info *h,
2982*dd0e19f3SScott Teel 	struct CommandList *c, struct io_accel2_cmd *cp)
2983*dd0e19f3SScott Teel {
2984*dd0e19f3SScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
2985*dd0e19f3SScott Teel 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
2986*dd0e19f3SScott Teel 	struct raid_map_data *map = &dev->raid_map;
2987*dd0e19f3SScott Teel 	u64 first_block;
2988*dd0e19f3SScott Teel 
2989*dd0e19f3SScott Teel 	BUG_ON(!(dev->offload_config && dev->offload_enabled));
2990*dd0e19f3SScott Teel 
2991*dd0e19f3SScott Teel 	/* Are we doing encryption on this device */
2992*dd0e19f3SScott Teel 	if (!(map->flags & RAID_MAP_FLAG_ENCRYPT_ON))
2993*dd0e19f3SScott Teel 		return;
2994*dd0e19f3SScott Teel 	/* Set the data encryption key index. */
2995*dd0e19f3SScott Teel 	cp->dekindex = map->dekindex;
2996*dd0e19f3SScott Teel 
2997*dd0e19f3SScott Teel 	/* Set the encryption enable flag, encoded into direction field. */
2998*dd0e19f3SScott Teel 	cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
2999*dd0e19f3SScott Teel 
3000*dd0e19f3SScott Teel 	/* Set encryption tweak values based on logical block address
3001*dd0e19f3SScott Teel 	 * If block size is 512, tweak value is LBA.
3002*dd0e19f3SScott Teel 	 * For other block sizes, tweak is (LBA * block size)/ 512)
3003*dd0e19f3SScott Teel 	 */
3004*dd0e19f3SScott Teel 	switch (cmd->cmnd[0]) {
3005*dd0e19f3SScott Teel 	/* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
3006*dd0e19f3SScott Teel 	case WRITE_6:
3007*dd0e19f3SScott Teel 	case READ_6:
3008*dd0e19f3SScott Teel 		if (map->volume_blk_size == 512) {
3009*dd0e19f3SScott Teel 			cp->tweak_lower =
3010*dd0e19f3SScott Teel 				(((u32) cmd->cmnd[2]) << 8) |
3011*dd0e19f3SScott Teel 					cmd->cmnd[3];
3012*dd0e19f3SScott Teel 			cp->tweak_upper = 0;
3013*dd0e19f3SScott Teel 		} else {
3014*dd0e19f3SScott Teel 			first_block =
3015*dd0e19f3SScott Teel 				(((u64) cmd->cmnd[2]) << 8) |
3016*dd0e19f3SScott Teel 					cmd->cmnd[3];
3017*dd0e19f3SScott Teel 			first_block = (first_block * map->volume_blk_size)/512;
3018*dd0e19f3SScott Teel 			cp->tweak_lower = (u32)first_block;
3019*dd0e19f3SScott Teel 			cp->tweak_upper = (u32)(first_block >> 32);
3020*dd0e19f3SScott Teel 		}
3021*dd0e19f3SScott Teel 		break;
3022*dd0e19f3SScott Teel 	case WRITE_10:
3023*dd0e19f3SScott Teel 	case READ_10:
3024*dd0e19f3SScott Teel 		if (map->volume_blk_size == 512) {
3025*dd0e19f3SScott Teel 			cp->tweak_lower =
3026*dd0e19f3SScott Teel 				(((u32) cmd->cmnd[2]) << 24) |
3027*dd0e19f3SScott Teel 				(((u32) cmd->cmnd[3]) << 16) |
3028*dd0e19f3SScott Teel 				(((u32) cmd->cmnd[4]) << 8) |
3029*dd0e19f3SScott Teel 					cmd->cmnd[5];
3030*dd0e19f3SScott Teel 			cp->tweak_upper = 0;
3031*dd0e19f3SScott Teel 		} else {
3032*dd0e19f3SScott Teel 			first_block =
3033*dd0e19f3SScott Teel 				(((u64) cmd->cmnd[2]) << 24) |
3034*dd0e19f3SScott Teel 				(((u64) cmd->cmnd[3]) << 16) |
3035*dd0e19f3SScott Teel 				(((u64) cmd->cmnd[4]) << 8) |
3036*dd0e19f3SScott Teel 					cmd->cmnd[5];
3037*dd0e19f3SScott Teel 			first_block = (first_block * map->volume_blk_size)/512;
3038*dd0e19f3SScott Teel 			cp->tweak_lower = (u32)first_block;
3039*dd0e19f3SScott Teel 			cp->tweak_upper = (u32)(first_block >> 32);
3040*dd0e19f3SScott Teel 		}
3041*dd0e19f3SScott Teel 		break;
3042*dd0e19f3SScott Teel 	/* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
3043*dd0e19f3SScott Teel 	case WRITE_12:
3044*dd0e19f3SScott Teel 	case READ_12:
3045*dd0e19f3SScott Teel 		if (map->volume_blk_size == 512) {
3046*dd0e19f3SScott Teel 			cp->tweak_lower =
3047*dd0e19f3SScott Teel 				(((u32) cmd->cmnd[2]) << 24) |
3048*dd0e19f3SScott Teel 				(((u32) cmd->cmnd[3]) << 16) |
3049*dd0e19f3SScott Teel 				(((u32) cmd->cmnd[4]) << 8) |
3050*dd0e19f3SScott Teel 					cmd->cmnd[5];
3051*dd0e19f3SScott Teel 			cp->tweak_upper = 0;
3052*dd0e19f3SScott Teel 		} else {
3053*dd0e19f3SScott Teel 			first_block =
3054*dd0e19f3SScott Teel 				(((u64) cmd->cmnd[2]) << 24) |
3055*dd0e19f3SScott Teel 				(((u64) cmd->cmnd[3]) << 16) |
3056*dd0e19f3SScott Teel 				(((u64) cmd->cmnd[4]) << 8) |
3057*dd0e19f3SScott Teel 					cmd->cmnd[5];
3058*dd0e19f3SScott Teel 			first_block = (first_block * map->volume_blk_size)/512;
3059*dd0e19f3SScott Teel 			cp->tweak_lower = (u32)first_block;
3060*dd0e19f3SScott Teel 			cp->tweak_upper = (u32)(first_block >> 32);
3061*dd0e19f3SScott Teel 		}
3062*dd0e19f3SScott Teel 		break;
3063*dd0e19f3SScott Teel 	case WRITE_16:
3064*dd0e19f3SScott Teel 	case READ_16:
3065*dd0e19f3SScott Teel 		if (map->volume_blk_size == 512) {
3066*dd0e19f3SScott Teel 			cp->tweak_lower =
3067*dd0e19f3SScott Teel 				(((u32) cmd->cmnd[6]) << 24) |
3068*dd0e19f3SScott Teel 				(((u32) cmd->cmnd[7]) << 16) |
3069*dd0e19f3SScott Teel 				(((u32) cmd->cmnd[8]) << 8) |
3070*dd0e19f3SScott Teel 					cmd->cmnd[9];
3071*dd0e19f3SScott Teel 			cp->tweak_upper =
3072*dd0e19f3SScott Teel 				(((u32) cmd->cmnd[2]) << 24) |
3073*dd0e19f3SScott Teel 				(((u32) cmd->cmnd[3]) << 16) |
3074*dd0e19f3SScott Teel 				(((u32) cmd->cmnd[4]) << 8) |
3075*dd0e19f3SScott Teel 					cmd->cmnd[5];
3076*dd0e19f3SScott Teel 		} else {
3077*dd0e19f3SScott Teel 			first_block =
3078*dd0e19f3SScott Teel 				(((u64) cmd->cmnd[2]) << 56) |
3079*dd0e19f3SScott Teel 				(((u64) cmd->cmnd[3]) << 48) |
3080*dd0e19f3SScott Teel 				(((u64) cmd->cmnd[4]) << 40) |
3081*dd0e19f3SScott Teel 				(((u64) cmd->cmnd[5]) << 32) |
3082*dd0e19f3SScott Teel 				(((u64) cmd->cmnd[6]) << 24) |
3083*dd0e19f3SScott Teel 				(((u64) cmd->cmnd[7]) << 16) |
3084*dd0e19f3SScott Teel 				(((u64) cmd->cmnd[8]) << 8) |
3085*dd0e19f3SScott Teel 					cmd->cmnd[9];
3086*dd0e19f3SScott Teel 			first_block = (first_block * map->volume_blk_size)/512;
3087*dd0e19f3SScott Teel 			cp->tweak_lower = (u32)first_block;
3088*dd0e19f3SScott Teel 			cp->tweak_upper = (u32)(first_block >> 32);
3089*dd0e19f3SScott Teel 		}
3090*dd0e19f3SScott Teel 		break;
3091*dd0e19f3SScott Teel 	default:
3092*dd0e19f3SScott Teel 		dev_err(&h->pdev->dev,
3093*dd0e19f3SScott Teel 			"ERROR: %s: IOACCEL request CDB size not supported for encryption\n",
3094*dd0e19f3SScott Teel 			__func__);
3095*dd0e19f3SScott Teel 		BUG();
3096*dd0e19f3SScott Teel 		break;
3097*dd0e19f3SScott Teel 	}
3098*dd0e19f3SScott Teel }
3099*dd0e19f3SScott Teel 
3100c349775eSScott Teel static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
3101c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
3102c349775eSScott Teel 	u8 *scsi3addr)
3103c349775eSScott Teel {
3104c349775eSScott Teel 	struct scsi_cmnd *cmd = c->scsi_cmd;
3105c349775eSScott Teel 	struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
3106c349775eSScott Teel 	struct ioaccel2_sg_element *curr_sg;
3107c349775eSScott Teel 	int use_sg, i;
3108c349775eSScott Teel 	struct scatterlist *sg;
3109c349775eSScott Teel 	u64 addr64;
3110c349775eSScott Teel 	u32 len;
3111c349775eSScott Teel 	u32 total_len = 0;
3112c349775eSScott Teel 
3113c349775eSScott Teel 	if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
3114c349775eSScott Teel 		return IO_ACCEL_INELIGIBLE;
3115c349775eSScott Teel 
3116c349775eSScott Teel 	if (fixup_ioaccel_cdb(cdb, &cdb_len))
3117c349775eSScott Teel 		return IO_ACCEL_INELIGIBLE;
3118c349775eSScott Teel 	c->cmd_type = CMD_IOACCEL2;
3119c349775eSScott Teel 	/* Adjust the DMA address to point to the accelerated command buffer */
3120c349775eSScott Teel 	c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
3121c349775eSScott Teel 				(c->cmdindex * sizeof(*cp));
3122c349775eSScott Teel 	BUG_ON(c->busaddr & 0x0000007F);
3123c349775eSScott Teel 
3124c349775eSScott Teel 	memset(cp, 0, sizeof(*cp));
3125c349775eSScott Teel 	cp->IU_type = IOACCEL2_IU_TYPE;
3126c349775eSScott Teel 
3127c349775eSScott Teel 	use_sg = scsi_dma_map(cmd);
3128c349775eSScott Teel 	if (use_sg < 0)
3129c349775eSScott Teel 		return use_sg;
3130c349775eSScott Teel 
3131c349775eSScott Teel 	if (use_sg) {
3132c349775eSScott Teel 		BUG_ON(use_sg > IOACCEL2_MAXSGENTRIES);
3133c349775eSScott Teel 		curr_sg = cp->sg;
3134c349775eSScott Teel 		scsi_for_each_sg(cmd, sg, use_sg, i) {
3135c349775eSScott Teel 			addr64 = (u64) sg_dma_address(sg);
3136c349775eSScott Teel 			len  = sg_dma_len(sg);
3137c349775eSScott Teel 			total_len += len;
3138c349775eSScott Teel 			curr_sg->address = cpu_to_le64(addr64);
3139c349775eSScott Teel 			curr_sg->length = cpu_to_le32(len);
3140c349775eSScott Teel 			curr_sg->reserved[0] = 0;
3141c349775eSScott Teel 			curr_sg->reserved[1] = 0;
3142c349775eSScott Teel 			curr_sg->reserved[2] = 0;
3143c349775eSScott Teel 			curr_sg->chain_indicator = 0;
3144c349775eSScott Teel 			curr_sg++;
3145c349775eSScott Teel 		}
3146c349775eSScott Teel 
3147c349775eSScott Teel 		switch (cmd->sc_data_direction) {
3148c349775eSScott Teel 		case DMA_TO_DEVICE:
3149*dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3150*dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_OUT;
3151c349775eSScott Teel 			break;
3152c349775eSScott Teel 		case DMA_FROM_DEVICE:
3153*dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3154*dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_DATA_IN;
3155c349775eSScott Teel 			break;
3156c349775eSScott Teel 		case DMA_NONE:
3157*dd0e19f3SScott Teel 			cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3158*dd0e19f3SScott Teel 			cp->direction |= IOACCEL2_DIR_NO_DATA;
3159c349775eSScott Teel 			break;
3160c349775eSScott Teel 		default:
3161c349775eSScott Teel 			dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3162c349775eSScott Teel 				cmd->sc_data_direction);
3163c349775eSScott Teel 			BUG();
3164c349775eSScott Teel 			break;
3165c349775eSScott Teel 		}
3166c349775eSScott Teel 	} else {
3167*dd0e19f3SScott Teel 		cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3168*dd0e19f3SScott Teel 		cp->direction |= IOACCEL2_DIR_NO_DATA;
3169c349775eSScott Teel 	}
3170*dd0e19f3SScott Teel 
3171*dd0e19f3SScott Teel 	/* Set encryption parameters, if necessary */
3172*dd0e19f3SScott Teel 	set_encrypt_ioaccel2(h, c, cp);
3173*dd0e19f3SScott Teel 
3174c349775eSScott Teel 	cp->scsi_nexus = ioaccel_handle;
3175*dd0e19f3SScott Teel 	cp->Tag = (c->cmdindex << DIRECT_LOOKUP_SHIFT) |
3176c349775eSScott Teel 				DIRECT_LOOKUP_BIT;
3177c349775eSScott Teel 	memcpy(cp->cdb, cdb, sizeof(cp->cdb));
3178c349775eSScott Teel 	memset(cp->cciss_lun, 0, sizeof(cp->cciss_lun));
3179c349775eSScott Teel 	cp->cmd_priority_task_attr = 0;
3180c349775eSScott Teel 
3181c349775eSScott Teel 	/* fill in sg elements */
3182c349775eSScott Teel 	cp->sg_count = (u8) use_sg;
3183c349775eSScott Teel 
3184c349775eSScott Teel 	cp->data_len = cpu_to_le32(total_len);
3185c349775eSScott Teel 	cp->err_ptr = cpu_to_le64(c->busaddr +
3186c349775eSScott Teel 			offsetof(struct io_accel2_cmd, error_data));
3187c349775eSScott Teel 	cp->err_len = cpu_to_le32((u32) sizeof(cp->error_data));
3188c349775eSScott Teel 
3189c349775eSScott Teel 	enqueue_cmd_and_start_io(h, c);
3190c349775eSScott Teel 	return 0;
3191c349775eSScott Teel }
3192c349775eSScott Teel 
3193c349775eSScott Teel /*
3194c349775eSScott Teel  * Queue a command to the correct I/O accelerator path.
3195c349775eSScott Teel  */
3196c349775eSScott Teel static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
3197c349775eSScott Teel 	struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
3198c349775eSScott Teel 	u8 *scsi3addr)
3199c349775eSScott Teel {
3200c349775eSScott Teel 	if (h->transMethod & CFGTBL_Trans_io_accel1)
3201c349775eSScott Teel 		return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
3202c349775eSScott Teel 						cdb, cdb_len, scsi3addr);
3203c349775eSScott Teel 	else
3204c349775eSScott Teel 		return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
3205c349775eSScott Teel 						cdb, cdb_len, scsi3addr);
3206c349775eSScott Teel }
3207c349775eSScott Teel 
32086b80b18fSScott Teel static void raid_map_helper(struct raid_map_data *map,
32096b80b18fSScott Teel 		int offload_to_mirror, u32 *map_index, u32 *current_group)
32106b80b18fSScott Teel {
32116b80b18fSScott Teel 	if (offload_to_mirror == 0)  {
32126b80b18fSScott Teel 		/* use physical disk in the first mirrored group. */
32136b80b18fSScott Teel 		*map_index %= map->data_disks_per_row;
32146b80b18fSScott Teel 		return;
32156b80b18fSScott Teel 	}
32166b80b18fSScott Teel 	do {
32176b80b18fSScott Teel 		/* determine mirror group that *map_index indicates */
32186b80b18fSScott Teel 		*current_group = *map_index / map->data_disks_per_row;
32196b80b18fSScott Teel 		if (offload_to_mirror == *current_group)
32206b80b18fSScott Teel 			continue;
32216b80b18fSScott Teel 		if (*current_group < (map->layout_map_count - 1)) {
32226b80b18fSScott Teel 			/* select map index from next group */
32236b80b18fSScott Teel 			*map_index += map->data_disks_per_row;
32246b80b18fSScott Teel 			(*current_group)++;
32256b80b18fSScott Teel 		} else {
32266b80b18fSScott Teel 			/* select map index from first group */
32276b80b18fSScott Teel 			*map_index %= map->data_disks_per_row;
32286b80b18fSScott Teel 			*current_group = 0;
32296b80b18fSScott Teel 		}
32306b80b18fSScott Teel 	} while (offload_to_mirror != *current_group);
32316b80b18fSScott Teel }
32326b80b18fSScott Teel 
3233283b4a9bSStephen M. Cameron /*
3234283b4a9bSStephen M. Cameron  * Attempt to perform offload RAID mapping for a logical volume I/O.
3235283b4a9bSStephen M. Cameron  */
3236283b4a9bSStephen M. Cameron static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
3237283b4a9bSStephen M. Cameron 	struct CommandList *c)
3238283b4a9bSStephen M. Cameron {
3239283b4a9bSStephen M. Cameron 	struct scsi_cmnd *cmd = c->scsi_cmd;
3240283b4a9bSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3241283b4a9bSStephen M. Cameron 	struct raid_map_data *map = &dev->raid_map;
3242283b4a9bSStephen M. Cameron 	struct raid_map_disk_data *dd = &map->data[0];
3243283b4a9bSStephen M. Cameron 	int is_write = 0;
3244283b4a9bSStephen M. Cameron 	u32 map_index;
3245283b4a9bSStephen M. Cameron 	u64 first_block, last_block;
3246283b4a9bSStephen M. Cameron 	u32 block_cnt;
3247283b4a9bSStephen M. Cameron 	u32 blocks_per_row;
3248283b4a9bSStephen M. Cameron 	u64 first_row, last_row;
3249283b4a9bSStephen M. Cameron 	u32 first_row_offset, last_row_offset;
3250283b4a9bSStephen M. Cameron 	u32 first_column, last_column;
32516b80b18fSScott Teel 	u64 r0_first_row, r0_last_row;
32526b80b18fSScott Teel 	u32 r5or6_blocks_per_row;
32536b80b18fSScott Teel 	u64 r5or6_first_row, r5or6_last_row;
32546b80b18fSScott Teel 	u32 r5or6_first_row_offset, r5or6_last_row_offset;
32556b80b18fSScott Teel 	u32 r5or6_first_column, r5or6_last_column;
32566b80b18fSScott Teel 	u32 total_disks_per_row;
32576b80b18fSScott Teel 	u32 stripesize;
32586b80b18fSScott Teel 	u32 first_group, last_group, current_group;
3259283b4a9bSStephen M. Cameron 	u32 map_row;
3260283b4a9bSStephen M. Cameron 	u32 disk_handle;
3261283b4a9bSStephen M. Cameron 	u64 disk_block;
3262283b4a9bSStephen M. Cameron 	u32 disk_block_cnt;
3263283b4a9bSStephen M. Cameron 	u8 cdb[16];
3264283b4a9bSStephen M. Cameron 	u8 cdb_len;
3265283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
3266283b4a9bSStephen M. Cameron 	u64 tmpdiv;
3267283b4a9bSStephen M. Cameron #endif
32686b80b18fSScott Teel 	int offload_to_mirror;
3269283b4a9bSStephen M. Cameron 
3270283b4a9bSStephen M. Cameron 	BUG_ON(!(dev->offload_config && dev->offload_enabled));
3271283b4a9bSStephen M. Cameron 
3272283b4a9bSStephen M. Cameron 	/* check for valid opcode, get LBA and block count */
3273283b4a9bSStephen M. Cameron 	switch (cmd->cmnd[0]) {
3274283b4a9bSStephen M. Cameron 	case WRITE_6:
3275283b4a9bSStephen M. Cameron 		is_write = 1;
3276283b4a9bSStephen M. Cameron 	case READ_6:
3277283b4a9bSStephen M. Cameron 		first_block =
3278283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 8) |
3279283b4a9bSStephen M. Cameron 			cmd->cmnd[3];
3280283b4a9bSStephen M. Cameron 		block_cnt = cmd->cmnd[4];
3281283b4a9bSStephen M. Cameron 		break;
3282283b4a9bSStephen M. Cameron 	case WRITE_10:
3283283b4a9bSStephen M. Cameron 		is_write = 1;
3284283b4a9bSStephen M. Cameron 	case READ_10:
3285283b4a9bSStephen M. Cameron 		first_block =
3286283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
3287283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
3288283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
3289283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
3290283b4a9bSStephen M. Cameron 		block_cnt =
3291283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 8) |
3292283b4a9bSStephen M. Cameron 			cmd->cmnd[8];
3293283b4a9bSStephen M. Cameron 		break;
3294283b4a9bSStephen M. Cameron 	case WRITE_12:
3295283b4a9bSStephen M. Cameron 		is_write = 1;
3296283b4a9bSStephen M. Cameron 	case READ_12:
3297283b4a9bSStephen M. Cameron 		first_block =
3298283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 24) |
3299283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 16) |
3300283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 8) |
3301283b4a9bSStephen M. Cameron 			cmd->cmnd[5];
3302283b4a9bSStephen M. Cameron 		block_cnt =
3303283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[6]) << 24) |
3304283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[7]) << 16) |
3305283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[8]) << 8) |
3306283b4a9bSStephen M. Cameron 		cmd->cmnd[9];
3307283b4a9bSStephen M. Cameron 		break;
3308283b4a9bSStephen M. Cameron 	case WRITE_16:
3309283b4a9bSStephen M. Cameron 		is_write = 1;
3310283b4a9bSStephen M. Cameron 	case READ_16:
3311283b4a9bSStephen M. Cameron 		first_block =
3312283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[2]) << 56) |
3313283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[3]) << 48) |
3314283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[4]) << 40) |
3315283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[5]) << 32) |
3316283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[6]) << 24) |
3317283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[7]) << 16) |
3318283b4a9bSStephen M. Cameron 			(((u64) cmd->cmnd[8]) << 8) |
3319283b4a9bSStephen M. Cameron 			cmd->cmnd[9];
3320283b4a9bSStephen M. Cameron 		block_cnt =
3321283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[10]) << 24) |
3322283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[11]) << 16) |
3323283b4a9bSStephen M. Cameron 			(((u32) cmd->cmnd[12]) << 8) |
3324283b4a9bSStephen M. Cameron 			cmd->cmnd[13];
3325283b4a9bSStephen M. Cameron 		break;
3326283b4a9bSStephen M. Cameron 	default:
3327283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
3328283b4a9bSStephen M. Cameron 	}
3329283b4a9bSStephen M. Cameron 	BUG_ON(block_cnt == 0);
3330283b4a9bSStephen M. Cameron 	last_block = first_block + block_cnt - 1;
3331283b4a9bSStephen M. Cameron 
3332283b4a9bSStephen M. Cameron 	/* check for write to non-RAID-0 */
3333283b4a9bSStephen M. Cameron 	if (is_write && dev->raid_level != 0)
3334283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3335283b4a9bSStephen M. Cameron 
3336283b4a9bSStephen M. Cameron 	/* check for invalid block or wraparound */
3337283b4a9bSStephen M. Cameron 	if (last_block >= map->volume_blk_cnt || last_block < first_block)
3338283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3339283b4a9bSStephen M. Cameron 
3340283b4a9bSStephen M. Cameron 	/* calculate stripe information for the request */
3341283b4a9bSStephen M. Cameron 	blocks_per_row = map->data_disks_per_row * map->strip_size;
3342283b4a9bSStephen M. Cameron #if BITS_PER_LONG == 32
3343283b4a9bSStephen M. Cameron 	tmpdiv = first_block;
3344283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
3345283b4a9bSStephen M. Cameron 	first_row = tmpdiv;
3346283b4a9bSStephen M. Cameron 	tmpdiv = last_block;
3347283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, blocks_per_row);
3348283b4a9bSStephen M. Cameron 	last_row = tmpdiv;
3349283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
3350283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
3351283b4a9bSStephen M. Cameron 	tmpdiv = first_row_offset;
3352283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv,  map->strip_size);
3353283b4a9bSStephen M. Cameron 	first_column = tmpdiv;
3354283b4a9bSStephen M. Cameron 	tmpdiv = last_row_offset;
3355283b4a9bSStephen M. Cameron 	(void) do_div(tmpdiv, map->strip_size);
3356283b4a9bSStephen M. Cameron 	last_column = tmpdiv;
3357283b4a9bSStephen M. Cameron #else
3358283b4a9bSStephen M. Cameron 	first_row = first_block / blocks_per_row;
3359283b4a9bSStephen M. Cameron 	last_row = last_block / blocks_per_row;
3360283b4a9bSStephen M. Cameron 	first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
3361283b4a9bSStephen M. Cameron 	last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
3362283b4a9bSStephen M. Cameron 	first_column = first_row_offset / map->strip_size;
3363283b4a9bSStephen M. Cameron 	last_column = last_row_offset / map->strip_size;
3364283b4a9bSStephen M. Cameron #endif
3365283b4a9bSStephen M. Cameron 
3366283b4a9bSStephen M. Cameron 	/* if this isn't a single row/column then give to the controller */
3367283b4a9bSStephen M. Cameron 	if ((first_row != last_row) || (first_column != last_column))
3368283b4a9bSStephen M. Cameron 		return IO_ACCEL_INELIGIBLE;
3369283b4a9bSStephen M. Cameron 
3370283b4a9bSStephen M. Cameron 	/* proceeding with driver mapping */
33716b80b18fSScott Teel 	total_disks_per_row = map->data_disks_per_row +
33726b80b18fSScott Teel 				map->metadata_disks_per_row;
3373283b4a9bSStephen M. Cameron 	map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
3374283b4a9bSStephen M. Cameron 				map->row_cnt;
33756b80b18fSScott Teel 	map_index = (map_row * total_disks_per_row) + first_column;
33766b80b18fSScott Teel 
33776b80b18fSScott Teel 	switch (dev->raid_level) {
33786b80b18fSScott Teel 	case HPSA_RAID_0:
33796b80b18fSScott Teel 		break; /* nothing special to do */
33806b80b18fSScott Teel 	case HPSA_RAID_1:
33816b80b18fSScott Teel 		/* Handles load balance across RAID 1 members.
33826b80b18fSScott Teel 		 * (2-drive R1 and R10 with even # of drives.)
33836b80b18fSScott Teel 		 * Appropriate for SSDs, not optimal for HDDs
3384283b4a9bSStephen M. Cameron 		 */
33856b80b18fSScott Teel 		BUG_ON(map->layout_map_count != 2);
3386283b4a9bSStephen M. Cameron 		if (dev->offload_to_mirror)
3387283b4a9bSStephen M. Cameron 			map_index += map->data_disks_per_row;
3388283b4a9bSStephen M. Cameron 		dev->offload_to_mirror = !dev->offload_to_mirror;
33896b80b18fSScott Teel 		break;
33906b80b18fSScott Teel 	case HPSA_RAID_ADM:
33916b80b18fSScott Teel 		/* Handles N-way mirrors  (R1-ADM)
33926b80b18fSScott Teel 		 * and R10 with # of drives divisible by 3.)
33936b80b18fSScott Teel 		 */
33946b80b18fSScott Teel 		BUG_ON(map->layout_map_count != 3);
33956b80b18fSScott Teel 
33966b80b18fSScott Teel 		offload_to_mirror = dev->offload_to_mirror;
33976b80b18fSScott Teel 		raid_map_helper(map, offload_to_mirror,
33986b80b18fSScott Teel 				&map_index, &current_group);
33996b80b18fSScott Teel 		/* set mirror group to use next time */
34006b80b18fSScott Teel 		offload_to_mirror =
34016b80b18fSScott Teel 			(offload_to_mirror >= map->layout_map_count - 1)
34026b80b18fSScott Teel 			? 0 : offload_to_mirror + 1;
34036b80b18fSScott Teel 		/* FIXME: remove after debug/dev */
34046b80b18fSScott Teel 		BUG_ON(offload_to_mirror >= map->layout_map_count);
34056b80b18fSScott Teel 		dev_warn(&h->pdev->dev,
34066b80b18fSScott Teel 			"DEBUG: Using physical disk map index %d from mirror group %d\n",
34076b80b18fSScott Teel 			map_index, offload_to_mirror);
34086b80b18fSScott Teel 		dev->offload_to_mirror = offload_to_mirror;
34096b80b18fSScott Teel 		/* Avoid direct use of dev->offload_to_mirror within this
34106b80b18fSScott Teel 		 * function since multiple threads might simultaneously
34116b80b18fSScott Teel 		 * increment it beyond the range of dev->layout_map_count -1.
34126b80b18fSScott Teel 		 */
34136b80b18fSScott Teel 		break;
34146b80b18fSScott Teel 	case HPSA_RAID_5:
34156b80b18fSScott Teel 	case HPSA_RAID_6:
34166b80b18fSScott Teel 		if (map->layout_map_count <= 1)
34176b80b18fSScott Teel 			break;
34186b80b18fSScott Teel 
34196b80b18fSScott Teel 		/* Verify first and last block are in same RAID group */
34206b80b18fSScott Teel 		r5or6_blocks_per_row =
34216b80b18fSScott Teel 			map->strip_size * map->data_disks_per_row;
34226b80b18fSScott Teel 		BUG_ON(r5or6_blocks_per_row == 0);
34236b80b18fSScott Teel 		stripesize = r5or6_blocks_per_row * map->layout_map_count;
34246b80b18fSScott Teel #if BITS_PER_LONG == 32
34256b80b18fSScott Teel 		tmpdiv = first_block;
34266b80b18fSScott Teel 		first_group = do_div(tmpdiv, stripesize);
34276b80b18fSScott Teel 		tmpdiv = first_group;
34286b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
34296b80b18fSScott Teel 		first_group = tmpdiv;
34306b80b18fSScott Teel 		tmpdiv = last_block;
34316b80b18fSScott Teel 		last_group = do_div(tmpdiv, stripesize);
34326b80b18fSScott Teel 		tmpdiv = last_group;
34336b80b18fSScott Teel 		(void) do_div(tmpdiv, r5or6_blocks_per_row);
34346b80b18fSScott Teel 		last_group = tmpdiv;
34356b80b18fSScott Teel #else
34366b80b18fSScott Teel 		first_group = (first_block % stripesize) / r5or6_blocks_per_row;
34376b80b18fSScott Teel 		last_group = (last_block % stripesize) / r5or6_blocks_per_row;
34386b80b18fSScott Teel 		if (first_group != last_group)
34396b80b18fSScott Teel #endif
34406b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
34416b80b18fSScott Teel 
34426b80b18fSScott Teel 		/* Verify request is in a single row of RAID 5/6 */
34436b80b18fSScott Teel #if BITS_PER_LONG == 32
34446b80b18fSScott Teel 		tmpdiv = first_block;
34456b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
34466b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row = tmpdiv;
34476b80b18fSScott Teel 		tmpdiv = last_block;
34486b80b18fSScott Teel 		(void) do_div(tmpdiv, stripesize);
34496b80b18fSScott Teel 		r5or6_last_row = r0_last_row = tmpdiv;
34506b80b18fSScott Teel #else
34516b80b18fSScott Teel 		first_row = r5or6_first_row = r0_first_row =
34526b80b18fSScott Teel 						first_block / stripesize;
34536b80b18fSScott Teel 		r5or6_last_row = r0_last_row = last_block / stripesize;
34546b80b18fSScott Teel #endif
34556b80b18fSScott Teel 		if (r5or6_first_row != r5or6_last_row)
34566b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
34576b80b18fSScott Teel 
34586b80b18fSScott Teel 
34596b80b18fSScott Teel 		/* Verify request is in a single column */
34606b80b18fSScott Teel #if BITS_PER_LONG == 32
34616b80b18fSScott Teel 		tmpdiv = first_block;
34626b80b18fSScott Teel 		first_row_offset = do_div(tmpdiv, stripesize);
34636b80b18fSScott Teel 		tmpdiv = first_row_offset;
34646b80b18fSScott Teel 		first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
34656b80b18fSScott Teel 		r5or6_first_row_offset = first_row_offset;
34666b80b18fSScott Teel 		tmpdiv = last_block;
34676b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, stripesize);
34686b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
34696b80b18fSScott Teel 		r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
34706b80b18fSScott Teel 		tmpdiv = r5or6_first_row_offset;
34716b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
34726b80b18fSScott Teel 		first_column = r5or6_first_column = tmpdiv;
34736b80b18fSScott Teel 		tmpdiv = r5or6_last_row_offset;
34746b80b18fSScott Teel 		(void) do_div(tmpdiv, map->strip_size);
34756b80b18fSScott Teel 		r5or6_last_column = tmpdiv;
34766b80b18fSScott Teel #else
34776b80b18fSScott Teel 		first_row_offset = r5or6_first_row_offset =
34786b80b18fSScott Teel 			(u32)((first_block % stripesize) %
34796b80b18fSScott Teel 						r5or6_blocks_per_row);
34806b80b18fSScott Teel 
34816b80b18fSScott Teel 		r5or6_last_row_offset =
34826b80b18fSScott Teel 			(u32)((last_block % stripesize) %
34836b80b18fSScott Teel 						r5or6_blocks_per_row);
34846b80b18fSScott Teel 
34856b80b18fSScott Teel 		first_column = r5or6_first_column =
34866b80b18fSScott Teel 			r5or6_first_row_offset / map->strip_size;
34876b80b18fSScott Teel 		r5or6_last_column =
34886b80b18fSScott Teel 			r5or6_last_row_offset / map->strip_size;
34896b80b18fSScott Teel #endif
34906b80b18fSScott Teel 		if (r5or6_first_column != r5or6_last_column)
34916b80b18fSScott Teel 			return IO_ACCEL_INELIGIBLE;
34926b80b18fSScott Teel 
34936b80b18fSScott Teel 		/* Request is eligible */
34946b80b18fSScott Teel 		map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
34956b80b18fSScott Teel 			map->row_cnt;
34966b80b18fSScott Teel 
34976b80b18fSScott Teel 		map_index = (first_group *
34986b80b18fSScott Teel 			(map->row_cnt * total_disks_per_row)) +
34996b80b18fSScott Teel 			(map_row * total_disks_per_row) + first_column;
35006b80b18fSScott Teel 		break;
35016b80b18fSScott Teel 	default:
35026b80b18fSScott Teel 		return IO_ACCEL_INELIGIBLE;
3503283b4a9bSStephen M. Cameron 	}
35046b80b18fSScott Teel 
3505283b4a9bSStephen M. Cameron 	disk_handle = dd[map_index].ioaccel_handle;
3506283b4a9bSStephen M. Cameron 	disk_block = map->disk_starting_blk + (first_row * map->strip_size) +
3507283b4a9bSStephen M. Cameron 			(first_row_offset - (first_column * map->strip_size));
3508283b4a9bSStephen M. Cameron 	disk_block_cnt = block_cnt;
3509283b4a9bSStephen M. Cameron 
3510283b4a9bSStephen M. Cameron 	/* handle differing logical/physical block sizes */
3511283b4a9bSStephen M. Cameron 	if (map->phys_blk_shift) {
3512283b4a9bSStephen M. Cameron 		disk_block <<= map->phys_blk_shift;
3513283b4a9bSStephen M. Cameron 		disk_block_cnt <<= map->phys_blk_shift;
3514283b4a9bSStephen M. Cameron 	}
3515283b4a9bSStephen M. Cameron 	BUG_ON(disk_block_cnt > 0xffff);
3516283b4a9bSStephen M. Cameron 
3517283b4a9bSStephen M. Cameron 	/* build the new CDB for the physical disk I/O */
3518283b4a9bSStephen M. Cameron 	if (disk_block > 0xffffffff) {
3519283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_16 : READ_16;
3520283b4a9bSStephen M. Cameron 		cdb[1] = 0;
3521283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 56);
3522283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 48);
3523283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 40);
3524283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block >> 32);
3525283b4a9bSStephen M. Cameron 		cdb[6] = (u8) (disk_block >> 24);
3526283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block >> 16);
3527283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block >> 8);
3528283b4a9bSStephen M. Cameron 		cdb[9] = (u8) (disk_block);
3529283b4a9bSStephen M. Cameron 		cdb[10] = (u8) (disk_block_cnt >> 24);
3530283b4a9bSStephen M. Cameron 		cdb[11] = (u8) (disk_block_cnt >> 16);
3531283b4a9bSStephen M. Cameron 		cdb[12] = (u8) (disk_block_cnt >> 8);
3532283b4a9bSStephen M. Cameron 		cdb[13] = (u8) (disk_block_cnt);
3533283b4a9bSStephen M. Cameron 		cdb[14] = 0;
3534283b4a9bSStephen M. Cameron 		cdb[15] = 0;
3535283b4a9bSStephen M. Cameron 		cdb_len = 16;
3536283b4a9bSStephen M. Cameron 	} else {
3537283b4a9bSStephen M. Cameron 		cdb[0] = is_write ? WRITE_10 : READ_10;
3538283b4a9bSStephen M. Cameron 		cdb[1] = 0;
3539283b4a9bSStephen M. Cameron 		cdb[2] = (u8) (disk_block >> 24);
3540283b4a9bSStephen M. Cameron 		cdb[3] = (u8) (disk_block >> 16);
3541283b4a9bSStephen M. Cameron 		cdb[4] = (u8) (disk_block >> 8);
3542283b4a9bSStephen M. Cameron 		cdb[5] = (u8) (disk_block);
3543283b4a9bSStephen M. Cameron 		cdb[6] = 0;
3544283b4a9bSStephen M. Cameron 		cdb[7] = (u8) (disk_block_cnt >> 8);
3545283b4a9bSStephen M. Cameron 		cdb[8] = (u8) (disk_block_cnt);
3546283b4a9bSStephen M. Cameron 		cdb[9] = 0;
3547283b4a9bSStephen M. Cameron 		cdb_len = 10;
3548283b4a9bSStephen M. Cameron 	}
3549283b4a9bSStephen M. Cameron 	return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
3550283b4a9bSStephen M. Cameron 						dev->scsi3addr);
3551283b4a9bSStephen M. Cameron }
3552283b4a9bSStephen M. Cameron 
3553f281233dSJeff Garzik static int hpsa_scsi_queue_command_lck(struct scsi_cmnd *cmd,
3554edd16368SStephen M. Cameron 	void (*done)(struct scsi_cmnd *))
3555edd16368SStephen M. Cameron {
3556edd16368SStephen M. Cameron 	struct ctlr_info *h;
3557edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
3558edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
3559edd16368SStephen M. Cameron 	struct CommandList *c;
3560edd16368SStephen M. Cameron 	unsigned long flags;
3561283b4a9bSStephen M. Cameron 	int rc = 0;
3562edd16368SStephen M. Cameron 
3563edd16368SStephen M. Cameron 	/* Get the ptr to our adapter structure out of cmd->host. */
3564edd16368SStephen M. Cameron 	h = sdev_to_hba(cmd->device);
3565edd16368SStephen M. Cameron 	dev = cmd->device->hostdata;
3566edd16368SStephen M. Cameron 	if (!dev) {
3567edd16368SStephen M. Cameron 		cmd->result = DID_NO_CONNECT << 16;
3568edd16368SStephen M. Cameron 		done(cmd);
3569edd16368SStephen M. Cameron 		return 0;
3570edd16368SStephen M. Cameron 	}
3571edd16368SStephen M. Cameron 	memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
3572edd16368SStephen M. Cameron 
3573edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
3574a0c12413SStephen M. Cameron 	if (unlikely(h->lockup_detected)) {
3575a0c12413SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
3576a0c12413SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
3577a0c12413SStephen M. Cameron 		done(cmd);
3578a0c12413SStephen M. Cameron 		return 0;
3579a0c12413SStephen M. Cameron 	}
3580edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
3581e16a33adSMatt Gates 	c = cmd_alloc(h);
3582edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
3583edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
3584edd16368SStephen M. Cameron 		return SCSI_MLQUEUE_HOST_BUSY;
3585edd16368SStephen M. Cameron 	}
3586edd16368SStephen M. Cameron 
3587edd16368SStephen M. Cameron 	/* Fill in the command list header */
3588edd16368SStephen M. Cameron 
3589edd16368SStephen M. Cameron 	cmd->scsi_done = done;    /* save this for use by completion code */
3590edd16368SStephen M. Cameron 
3591edd16368SStephen M. Cameron 	/* save c in case we have to abort it  */
3592edd16368SStephen M. Cameron 	cmd->host_scribble = (unsigned char *) c;
3593edd16368SStephen M. Cameron 
3594edd16368SStephen M. Cameron 	c->cmd_type = CMD_SCSI;
3595edd16368SStephen M. Cameron 	c->scsi_cmd = cmd;
3596e1f7de0cSMatt Gates 
3597283b4a9bSStephen M. Cameron 	/* Call alternate submit routine for I/O accelerated commands.
3598283b4a9bSStephen M. Cameron 	 * Retries always go down the normal I/O path.
3599283b4a9bSStephen M. Cameron 	 */
3600283b4a9bSStephen M. Cameron 	if (likely(cmd->retries == 0 &&
3601da0697bdSScott Teel 		cmd->request->cmd_type == REQ_TYPE_FS &&
3602da0697bdSScott Teel 		h->acciopath_status)) {
3603283b4a9bSStephen M. Cameron 		if (dev->offload_enabled) {
3604283b4a9bSStephen M. Cameron 			rc = hpsa_scsi_ioaccel_raid_map(h, c);
3605283b4a9bSStephen M. Cameron 			if (rc == 0)
3606283b4a9bSStephen M. Cameron 				return 0; /* Sent on ioaccel path */
3607283b4a9bSStephen M. Cameron 			if (rc < 0) {   /* scsi_dma_map failed. */
3608283b4a9bSStephen M. Cameron 				cmd_free(h, c);
3609283b4a9bSStephen M. Cameron 				return SCSI_MLQUEUE_HOST_BUSY;
3610283b4a9bSStephen M. Cameron 			}
3611283b4a9bSStephen M. Cameron 		} else if (dev->ioaccel_handle) {
3612283b4a9bSStephen M. Cameron 			rc = hpsa_scsi_ioaccel_direct_map(h, c);
3613283b4a9bSStephen M. Cameron 			if (rc == 0)
3614283b4a9bSStephen M. Cameron 				return 0; /* Sent on direct map path */
3615283b4a9bSStephen M. Cameron 			if (rc < 0) {   /* scsi_dma_map failed. */
3616283b4a9bSStephen M. Cameron 				cmd_free(h, c);
3617283b4a9bSStephen M. Cameron 				return SCSI_MLQUEUE_HOST_BUSY;
3618283b4a9bSStephen M. Cameron 			}
3619283b4a9bSStephen M. Cameron 		}
3620283b4a9bSStephen M. Cameron 	}
3621e1f7de0cSMatt Gates 
3622edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;  /* unused in simple mode */
3623edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
3624303932fdSDon Brace 	c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT);
3625303932fdSDon Brace 	c->Header.Tag.lower |= DIRECT_LOOKUP_BIT;
3626edd16368SStephen M. Cameron 
3627edd16368SStephen M. Cameron 	/* Fill in the request block... */
3628edd16368SStephen M. Cameron 
3629edd16368SStephen M. Cameron 	c->Request.Timeout = 0;
3630edd16368SStephen M. Cameron 	memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
3631edd16368SStephen M. Cameron 	BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
3632edd16368SStephen M. Cameron 	c->Request.CDBLen = cmd->cmd_len;
3633edd16368SStephen M. Cameron 	memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
3634edd16368SStephen M. Cameron 	c->Request.Type.Type = TYPE_CMD;
3635edd16368SStephen M. Cameron 	c->Request.Type.Attribute = ATTR_SIMPLE;
3636edd16368SStephen M. Cameron 	switch (cmd->sc_data_direction) {
3637edd16368SStephen M. Cameron 	case DMA_TO_DEVICE:
3638edd16368SStephen M. Cameron 		c->Request.Type.Direction = XFER_WRITE;
3639edd16368SStephen M. Cameron 		break;
3640edd16368SStephen M. Cameron 	case DMA_FROM_DEVICE:
3641edd16368SStephen M. Cameron 		c->Request.Type.Direction = XFER_READ;
3642edd16368SStephen M. Cameron 		break;
3643edd16368SStephen M. Cameron 	case DMA_NONE:
3644edd16368SStephen M. Cameron 		c->Request.Type.Direction = XFER_NONE;
3645edd16368SStephen M. Cameron 		break;
3646edd16368SStephen M. Cameron 	case DMA_BIDIRECTIONAL:
3647edd16368SStephen M. Cameron 		/* This can happen if a buggy application does a scsi passthru
3648edd16368SStephen M. Cameron 		 * and sets both inlen and outlen to non-zero. ( see
3649edd16368SStephen M. Cameron 		 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
3650edd16368SStephen M. Cameron 		 */
3651edd16368SStephen M. Cameron 
3652edd16368SStephen M. Cameron 		c->Request.Type.Direction = XFER_RSVD;
3653edd16368SStephen M. Cameron 		/* This is technically wrong, and hpsa controllers should
3654edd16368SStephen M. Cameron 		 * reject it with CMD_INVALID, which is the most correct
3655edd16368SStephen M. Cameron 		 * response, but non-fibre backends appear to let it
3656edd16368SStephen M. Cameron 		 * slide by, and give the same results as if this field
3657edd16368SStephen M. Cameron 		 * were set correctly.  Either way is acceptable for
3658edd16368SStephen M. Cameron 		 * our purposes here.
3659edd16368SStephen M. Cameron 		 */
3660edd16368SStephen M. Cameron 
3661edd16368SStephen M. Cameron 		break;
3662edd16368SStephen M. Cameron 
3663edd16368SStephen M. Cameron 	default:
3664edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3665edd16368SStephen M. Cameron 			cmd->sc_data_direction);
3666edd16368SStephen M. Cameron 		BUG();
3667edd16368SStephen M. Cameron 		break;
3668edd16368SStephen M. Cameron 	}
3669edd16368SStephen M. Cameron 
367033a2ffceSStephen M. Cameron 	if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
3671edd16368SStephen M. Cameron 		cmd_free(h, c);
3672edd16368SStephen M. Cameron 		return SCSI_MLQUEUE_HOST_BUSY;
3673edd16368SStephen M. Cameron 	}
3674edd16368SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
3675edd16368SStephen M. Cameron 	/* the cmd'll come back via intr handler in complete_scsi_command()  */
3676edd16368SStephen M. Cameron 	return 0;
3677edd16368SStephen M. Cameron }
3678edd16368SStephen M. Cameron 
3679f281233dSJeff Garzik static DEF_SCSI_QCMD(hpsa_scsi_queue_command)
3680f281233dSJeff Garzik 
36815f389360SStephen M. Cameron static int do_not_scan_if_controller_locked_up(struct ctlr_info *h)
36825f389360SStephen M. Cameron {
36835f389360SStephen M. Cameron 	unsigned long flags;
36845f389360SStephen M. Cameron 
36855f389360SStephen M. Cameron 	/*
36865f389360SStephen M. Cameron 	 * Don't let rescans be initiated on a controller known
36875f389360SStephen M. Cameron 	 * to be locked up.  If the controller locks up *during*
36885f389360SStephen M. Cameron 	 * a rescan, that thread is probably hosed, but at least
36895f389360SStephen M. Cameron 	 * we can prevent new rescan threads from piling up on a
36905f389360SStephen M. Cameron 	 * locked up controller.
36915f389360SStephen M. Cameron 	 */
36925f389360SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
36935f389360SStephen M. Cameron 	if (unlikely(h->lockup_detected)) {
36945f389360SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
36955f389360SStephen M. Cameron 		spin_lock_irqsave(&h->scan_lock, flags);
36965f389360SStephen M. Cameron 		h->scan_finished = 1;
36975f389360SStephen M. Cameron 		wake_up_all(&h->scan_wait_queue);
36985f389360SStephen M. Cameron 		spin_unlock_irqrestore(&h->scan_lock, flags);
36995f389360SStephen M. Cameron 		return 1;
37005f389360SStephen M. Cameron 	}
37015f389360SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
37025f389360SStephen M. Cameron 	return 0;
37035f389360SStephen M. Cameron }
37045f389360SStephen M. Cameron 
3705a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *sh)
3706a08a8471SStephen M. Cameron {
3707a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
3708a08a8471SStephen M. Cameron 	unsigned long flags;
3709a08a8471SStephen M. Cameron 
37105f389360SStephen M. Cameron 	if (do_not_scan_if_controller_locked_up(h))
37115f389360SStephen M. Cameron 		return;
37125f389360SStephen M. Cameron 
3713a08a8471SStephen M. Cameron 	/* wait until any scan already in progress is finished. */
3714a08a8471SStephen M. Cameron 	while (1) {
3715a08a8471SStephen M. Cameron 		spin_lock_irqsave(&h->scan_lock, flags);
3716a08a8471SStephen M. Cameron 		if (h->scan_finished)
3717a08a8471SStephen M. Cameron 			break;
3718a08a8471SStephen M. Cameron 		spin_unlock_irqrestore(&h->scan_lock, flags);
3719a08a8471SStephen M. Cameron 		wait_event(h->scan_wait_queue, h->scan_finished);
3720a08a8471SStephen M. Cameron 		/* Note: We don't need to worry about a race between this
3721a08a8471SStephen M. Cameron 		 * thread and driver unload because the midlayer will
3722a08a8471SStephen M. Cameron 		 * have incremented the reference count, so unload won't
3723a08a8471SStephen M. Cameron 		 * happen if we're in here.
3724a08a8471SStephen M. Cameron 		 */
3725a08a8471SStephen M. Cameron 	}
3726a08a8471SStephen M. Cameron 	h->scan_finished = 0; /* mark scan as in progress */
3727a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
3728a08a8471SStephen M. Cameron 
37295f389360SStephen M. Cameron 	if (do_not_scan_if_controller_locked_up(h))
37305f389360SStephen M. Cameron 		return;
37315f389360SStephen M. Cameron 
3732a08a8471SStephen M. Cameron 	hpsa_update_scsi_devices(h, h->scsi_host->host_no);
3733a08a8471SStephen M. Cameron 
3734a08a8471SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
3735a08a8471SStephen M. Cameron 	h->scan_finished = 1; /* mark scan as finished. */
3736a08a8471SStephen M. Cameron 	wake_up_all(&h->scan_wait_queue);
3737a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
3738a08a8471SStephen M. Cameron }
3739a08a8471SStephen M. Cameron 
3740a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
3741a08a8471SStephen M. Cameron 	unsigned long elapsed_time)
3742a08a8471SStephen M. Cameron {
3743a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
3744a08a8471SStephen M. Cameron 	unsigned long flags;
3745a08a8471SStephen M. Cameron 	int finished;
3746a08a8471SStephen M. Cameron 
3747a08a8471SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
3748a08a8471SStephen M. Cameron 	finished = h->scan_finished;
3749a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
3750a08a8471SStephen M. Cameron 	return finished;
3751a08a8471SStephen M. Cameron }
3752a08a8471SStephen M. Cameron 
3753667e23d4SStephen M. Cameron static int hpsa_change_queue_depth(struct scsi_device *sdev,
3754667e23d4SStephen M. Cameron 	int qdepth, int reason)
3755667e23d4SStephen M. Cameron {
3756667e23d4SStephen M. Cameron 	struct ctlr_info *h = sdev_to_hba(sdev);
3757667e23d4SStephen M. Cameron 
3758667e23d4SStephen M. Cameron 	if (reason != SCSI_QDEPTH_DEFAULT)
3759667e23d4SStephen M. Cameron 		return -ENOTSUPP;
3760667e23d4SStephen M. Cameron 
3761667e23d4SStephen M. Cameron 	if (qdepth < 1)
3762667e23d4SStephen M. Cameron 		qdepth = 1;
3763667e23d4SStephen M. Cameron 	else
3764667e23d4SStephen M. Cameron 		if (qdepth > h->nr_cmds)
3765667e23d4SStephen M. Cameron 			qdepth = h->nr_cmds;
3766667e23d4SStephen M. Cameron 	scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
3767667e23d4SStephen M. Cameron 	return sdev->queue_depth;
3768667e23d4SStephen M. Cameron }
3769667e23d4SStephen M. Cameron 
3770edd16368SStephen M. Cameron static void hpsa_unregister_scsi(struct ctlr_info *h)
3771edd16368SStephen M. Cameron {
3772edd16368SStephen M. Cameron 	/* we are being forcibly unloaded, and may not refuse. */
3773edd16368SStephen M. Cameron 	scsi_remove_host(h->scsi_host);
3774edd16368SStephen M. Cameron 	scsi_host_put(h->scsi_host);
3775edd16368SStephen M. Cameron 	h->scsi_host = NULL;
3776edd16368SStephen M. Cameron }
3777edd16368SStephen M. Cameron 
3778edd16368SStephen M. Cameron static int hpsa_register_scsi(struct ctlr_info *h)
3779edd16368SStephen M. Cameron {
3780b705690dSStephen M. Cameron 	struct Scsi_Host *sh;
3781b705690dSStephen M. Cameron 	int error;
3782edd16368SStephen M. Cameron 
3783b705690dSStephen M. Cameron 	sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
3784b705690dSStephen M. Cameron 	if (sh == NULL)
3785b705690dSStephen M. Cameron 		goto fail;
3786b705690dSStephen M. Cameron 
3787b705690dSStephen M. Cameron 	sh->io_port = 0;
3788b705690dSStephen M. Cameron 	sh->n_io_port = 0;
3789b705690dSStephen M. Cameron 	sh->this_id = -1;
3790b705690dSStephen M. Cameron 	sh->max_channel = 3;
3791b705690dSStephen M. Cameron 	sh->max_cmd_len = MAX_COMMAND_SIZE;
3792b705690dSStephen M. Cameron 	sh->max_lun = HPSA_MAX_LUN;
3793b705690dSStephen M. Cameron 	sh->max_id = HPSA_MAX_LUN;
3794b705690dSStephen M. Cameron 	sh->can_queue = h->nr_cmds;
3795b705690dSStephen M. Cameron 	sh->cmd_per_lun = h->nr_cmds;
3796b705690dSStephen M. Cameron 	sh->sg_tablesize = h->maxsgentries;
3797b705690dSStephen M. Cameron 	h->scsi_host = sh;
3798b705690dSStephen M. Cameron 	sh->hostdata[0] = (unsigned long) h;
3799b705690dSStephen M. Cameron 	sh->irq = h->intr[h->intr_mode];
3800b705690dSStephen M. Cameron 	sh->unique_id = sh->irq;
3801b705690dSStephen M. Cameron 	error = scsi_add_host(sh, &h->pdev->dev);
3802b705690dSStephen M. Cameron 	if (error)
3803b705690dSStephen M. Cameron 		goto fail_host_put;
3804b705690dSStephen M. Cameron 	scsi_scan_host(sh);
3805b705690dSStephen M. Cameron 	return 0;
3806b705690dSStephen M. Cameron 
3807b705690dSStephen M. Cameron  fail_host_put:
3808b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_add_host"
3809b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
3810b705690dSStephen M. Cameron 	scsi_host_put(sh);
3811b705690dSStephen M. Cameron 	return error;
3812b705690dSStephen M. Cameron  fail:
3813b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_host_alloc"
3814b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
3815b705690dSStephen M. Cameron 	return -ENOMEM;
3816edd16368SStephen M. Cameron }
3817edd16368SStephen M. Cameron 
3818edd16368SStephen M. Cameron static int wait_for_device_to_become_ready(struct ctlr_info *h,
3819edd16368SStephen M. Cameron 	unsigned char lunaddr[])
3820edd16368SStephen M. Cameron {
3821edd16368SStephen M. Cameron 	int rc = 0;
3822edd16368SStephen M. Cameron 	int count = 0;
3823edd16368SStephen M. Cameron 	int waittime = 1; /* seconds */
3824edd16368SStephen M. Cameron 	struct CommandList *c;
3825edd16368SStephen M. Cameron 
3826edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
3827edd16368SStephen M. Cameron 	if (!c) {
3828edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in "
3829edd16368SStephen M. Cameron 			"wait_for_device_to_become_ready.\n");
3830edd16368SStephen M. Cameron 		return IO_ERROR;
3831edd16368SStephen M. Cameron 	}
3832edd16368SStephen M. Cameron 
3833edd16368SStephen M. Cameron 	/* Send test unit ready until device ready, or give up. */
3834edd16368SStephen M. Cameron 	while (count < HPSA_TUR_RETRY_LIMIT) {
3835edd16368SStephen M. Cameron 
3836edd16368SStephen M. Cameron 		/* Wait for a bit.  do this first, because if we send
3837edd16368SStephen M. Cameron 		 * the TUR right away, the reset will just abort it.
3838edd16368SStephen M. Cameron 		 */
3839edd16368SStephen M. Cameron 		msleep(1000 * waittime);
3840edd16368SStephen M. Cameron 		count++;
3841edd16368SStephen M. Cameron 
3842edd16368SStephen M. Cameron 		/* Increase wait time with each try, up to a point. */
3843edd16368SStephen M. Cameron 		if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
3844edd16368SStephen M. Cameron 			waittime = waittime * 2;
3845edd16368SStephen M. Cameron 
3846a2dac136SStephen M. Cameron 		/* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
3847a2dac136SStephen M. Cameron 		(void) fill_cmd(c, TEST_UNIT_READY, h,
3848a2dac136SStephen M. Cameron 				NULL, 0, 0, lunaddr, TYPE_CMD);
3849edd16368SStephen M. Cameron 		hpsa_scsi_do_simple_cmd_core(h, c);
3850edd16368SStephen M. Cameron 		/* no unmap needed here because no data xfer. */
3851edd16368SStephen M. Cameron 
3852edd16368SStephen M. Cameron 		if (c->err_info->CommandStatus == CMD_SUCCESS)
3853edd16368SStephen M. Cameron 			break;
3854edd16368SStephen M. Cameron 
3855edd16368SStephen M. Cameron 		if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
3856edd16368SStephen M. Cameron 			c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
3857edd16368SStephen M. Cameron 			(c->err_info->SenseInfo[2] == NO_SENSE ||
3858edd16368SStephen M. Cameron 			c->err_info->SenseInfo[2] == UNIT_ATTENTION))
3859edd16368SStephen M. Cameron 			break;
3860edd16368SStephen M. Cameron 
3861edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "waiting %d secs "
3862edd16368SStephen M. Cameron 			"for device to become ready.\n", waittime);
3863edd16368SStephen M. Cameron 		rc = 1; /* device not ready. */
3864edd16368SStephen M. Cameron 	}
3865edd16368SStephen M. Cameron 
3866edd16368SStephen M. Cameron 	if (rc)
3867edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "giving up on device.\n");
3868edd16368SStephen M. Cameron 	else
3869edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "device is ready.\n");
3870edd16368SStephen M. Cameron 
3871edd16368SStephen M. Cameron 	cmd_special_free(h, c);
3872edd16368SStephen M. Cameron 	return rc;
3873edd16368SStephen M. Cameron }
3874edd16368SStephen M. Cameron 
3875edd16368SStephen M. Cameron /* Need at least one of these error handlers to keep ../scsi/hosts.c from
3876edd16368SStephen M. Cameron  * complaining.  Doing a host- or bus-reset can't do anything good here.
3877edd16368SStephen M. Cameron  */
3878edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
3879edd16368SStephen M. Cameron {
3880edd16368SStephen M. Cameron 	int rc;
3881edd16368SStephen M. Cameron 	struct ctlr_info *h;
3882edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
3883edd16368SStephen M. Cameron 
3884edd16368SStephen M. Cameron 	/* find the controller to which the command to be aborted was sent */
3885edd16368SStephen M. Cameron 	h = sdev_to_hba(scsicmd->device);
3886edd16368SStephen M. Cameron 	if (h == NULL) /* paranoia */
3887edd16368SStephen M. Cameron 		return FAILED;
3888edd16368SStephen M. Cameron 	dev = scsicmd->device->hostdata;
3889edd16368SStephen M. Cameron 	if (!dev) {
3890edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
3891edd16368SStephen M. Cameron 			"device lookup failed.\n");
3892edd16368SStephen M. Cameron 		return FAILED;
3893edd16368SStephen M. Cameron 	}
3894d416b0c7SStephen M. Cameron 	dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
3895d416b0c7SStephen M. Cameron 		h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
3896edd16368SStephen M. Cameron 	/* send a reset to the SCSI LUN which the command was sent to */
3897bf711ac6SScott Teel 	rc = hpsa_send_reset(h, dev->scsi3addr, HPSA_RESET_TYPE_LUN);
3898edd16368SStephen M. Cameron 	if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
3899edd16368SStephen M. Cameron 		return SUCCESS;
3900edd16368SStephen M. Cameron 
3901edd16368SStephen M. Cameron 	dev_warn(&h->pdev->dev, "resetting device failed.\n");
3902edd16368SStephen M. Cameron 	return FAILED;
3903edd16368SStephen M. Cameron }
3904edd16368SStephen M. Cameron 
39056cba3f19SStephen M. Cameron static void swizzle_abort_tag(u8 *tag)
39066cba3f19SStephen M. Cameron {
39076cba3f19SStephen M. Cameron 	u8 original_tag[8];
39086cba3f19SStephen M. Cameron 
39096cba3f19SStephen M. Cameron 	memcpy(original_tag, tag, 8);
39106cba3f19SStephen M. Cameron 	tag[0] = original_tag[3];
39116cba3f19SStephen M. Cameron 	tag[1] = original_tag[2];
39126cba3f19SStephen M. Cameron 	tag[2] = original_tag[1];
39136cba3f19SStephen M. Cameron 	tag[3] = original_tag[0];
39146cba3f19SStephen M. Cameron 	tag[4] = original_tag[7];
39156cba3f19SStephen M. Cameron 	tag[5] = original_tag[6];
39166cba3f19SStephen M. Cameron 	tag[6] = original_tag[5];
39176cba3f19SStephen M. Cameron 	tag[7] = original_tag[4];
39186cba3f19SStephen M. Cameron }
39196cba3f19SStephen M. Cameron 
392017eb87d2SScott Teel static void hpsa_get_tag(struct ctlr_info *h,
392117eb87d2SScott Teel 	struct CommandList *c, u32 *taglower, u32 *tagupper)
392217eb87d2SScott Teel {
392317eb87d2SScott Teel 	if (c->cmd_type == CMD_IOACCEL1) {
392417eb87d2SScott Teel 		struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
392517eb87d2SScott Teel 			&h->ioaccel_cmd_pool[c->cmdindex];
392617eb87d2SScott Teel 		*tagupper = cm1->Tag.upper;
392717eb87d2SScott Teel 		*taglower = cm1->Tag.lower;
392854b6e9e9SScott Teel 		return;
392954b6e9e9SScott Teel 	}
393054b6e9e9SScott Teel 	if (c->cmd_type == CMD_IOACCEL2) {
393154b6e9e9SScott Teel 		struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
393254b6e9e9SScott Teel 			&h->ioaccel2_cmd_pool[c->cmdindex];
3933*dd0e19f3SScott Teel 		/* upper tag not used in ioaccel2 mode */
3934*dd0e19f3SScott Teel 		memset(tagupper, 0, sizeof(*tagupper));
3935*dd0e19f3SScott Teel 		*taglower = cm2->Tag;
393654b6e9e9SScott Teel 		return;
393754b6e9e9SScott Teel 	}
393817eb87d2SScott Teel 	*tagupper = c->Header.Tag.upper;
393917eb87d2SScott Teel 	*taglower = c->Header.Tag.lower;
394017eb87d2SScott Teel }
394154b6e9e9SScott Teel 
394217eb87d2SScott Teel 
394375167d2cSStephen M. Cameron static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
39446cba3f19SStephen M. Cameron 	struct CommandList *abort, int swizzle)
394575167d2cSStephen M. Cameron {
394675167d2cSStephen M. Cameron 	int rc = IO_OK;
394775167d2cSStephen M. Cameron 	struct CommandList *c;
394875167d2cSStephen M. Cameron 	struct ErrorInfo *ei;
394917eb87d2SScott Teel 	u32 tagupper, taglower;
395075167d2cSStephen M. Cameron 
395175167d2cSStephen M. Cameron 	c = cmd_special_alloc(h);
395275167d2cSStephen M. Cameron 	if (c == NULL) {	/* trouble... */
395375167d2cSStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
395475167d2cSStephen M. Cameron 		return -ENOMEM;
395575167d2cSStephen M. Cameron 	}
395675167d2cSStephen M. Cameron 
3957a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no buffer to map */
3958a2dac136SStephen M. Cameron 	(void) fill_cmd(c, HPSA_ABORT_MSG, h, abort,
3959a2dac136SStephen M. Cameron 		0, 0, scsi3addr, TYPE_MSG);
39606cba3f19SStephen M. Cameron 	if (swizzle)
39616cba3f19SStephen M. Cameron 		swizzle_abort_tag(&c->Request.CDB[4]);
396275167d2cSStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core(h, c);
396317eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
396475167d2cSStephen M. Cameron 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd_core completed.\n",
396517eb87d2SScott Teel 		__func__, tagupper, taglower);
396675167d2cSStephen M. Cameron 	/* no unmap needed here because no data xfer. */
396775167d2cSStephen M. Cameron 
396875167d2cSStephen M. Cameron 	ei = c->err_info;
396975167d2cSStephen M. Cameron 	switch (ei->CommandStatus) {
397075167d2cSStephen M. Cameron 	case CMD_SUCCESS:
397175167d2cSStephen M. Cameron 		break;
397275167d2cSStephen M. Cameron 	case CMD_UNABORTABLE: /* Very common, don't make noise. */
397375167d2cSStephen M. Cameron 		rc = -1;
397475167d2cSStephen M. Cameron 		break;
397575167d2cSStephen M. Cameron 	default:
397675167d2cSStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
397717eb87d2SScott Teel 			__func__, tagupper, taglower);
397875167d2cSStephen M. Cameron 		hpsa_scsi_interpret_error(c);
397975167d2cSStephen M. Cameron 		rc = -1;
398075167d2cSStephen M. Cameron 		break;
398175167d2cSStephen M. Cameron 	}
398275167d2cSStephen M. Cameron 	cmd_special_free(h, c);
3983*dd0e19f3SScott Teel 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n",
3984*dd0e19f3SScott Teel 		__func__, tagupper, taglower);
398575167d2cSStephen M. Cameron 	return rc;
398675167d2cSStephen M. Cameron }
398775167d2cSStephen M. Cameron 
398875167d2cSStephen M. Cameron /*
398975167d2cSStephen M. Cameron  * hpsa_find_cmd_in_queue
399075167d2cSStephen M. Cameron  *
399175167d2cSStephen M. Cameron  * Used to determine whether a command (find) is still present
399275167d2cSStephen M. Cameron  * in queue_head.   Optionally excludes the last element of queue_head.
399375167d2cSStephen M. Cameron  *
399475167d2cSStephen M. Cameron  * This is used to avoid unnecessary aborts.  Commands in h->reqQ have
399575167d2cSStephen M. Cameron  * not yet been submitted, and so can be aborted by the driver without
399675167d2cSStephen M. Cameron  * sending an abort to the hardware.
399775167d2cSStephen M. Cameron  *
399875167d2cSStephen M. Cameron  * Returns pointer to command if found in queue, NULL otherwise.
399975167d2cSStephen M. Cameron  */
400075167d2cSStephen M. Cameron static struct CommandList *hpsa_find_cmd_in_queue(struct ctlr_info *h,
400175167d2cSStephen M. Cameron 			struct scsi_cmnd *find, struct list_head *queue_head)
400275167d2cSStephen M. Cameron {
400375167d2cSStephen M. Cameron 	unsigned long flags;
400475167d2cSStephen M. Cameron 	struct CommandList *c = NULL;	/* ptr into cmpQ */
400575167d2cSStephen M. Cameron 
400675167d2cSStephen M. Cameron 	if (!find)
400775167d2cSStephen M. Cameron 		return 0;
400875167d2cSStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
400975167d2cSStephen M. Cameron 	list_for_each_entry(c, queue_head, list) {
401075167d2cSStephen M. Cameron 		if (c->scsi_cmd == NULL) /* e.g.: passthru ioctl */
401175167d2cSStephen M. Cameron 			continue;
401275167d2cSStephen M. Cameron 		if (c->scsi_cmd == find) {
401375167d2cSStephen M. Cameron 			spin_unlock_irqrestore(&h->lock, flags);
401475167d2cSStephen M. Cameron 			return c;
401575167d2cSStephen M. Cameron 		}
401675167d2cSStephen M. Cameron 	}
401775167d2cSStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
401875167d2cSStephen M. Cameron 	return NULL;
401975167d2cSStephen M. Cameron }
402075167d2cSStephen M. Cameron 
40216cba3f19SStephen M. Cameron static struct CommandList *hpsa_find_cmd_in_queue_by_tag(struct ctlr_info *h,
40226cba3f19SStephen M. Cameron 					u8 *tag, struct list_head *queue_head)
40236cba3f19SStephen M. Cameron {
40246cba3f19SStephen M. Cameron 	unsigned long flags;
40256cba3f19SStephen M. Cameron 	struct CommandList *c;
40266cba3f19SStephen M. Cameron 
40276cba3f19SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
40286cba3f19SStephen M. Cameron 	list_for_each_entry(c, queue_head, list) {
40296cba3f19SStephen M. Cameron 		if (memcmp(&c->Header.Tag, tag, 8) != 0)
40306cba3f19SStephen M. Cameron 			continue;
40316cba3f19SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
40326cba3f19SStephen M. Cameron 		return c;
40336cba3f19SStephen M. Cameron 	}
40346cba3f19SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
40356cba3f19SStephen M. Cameron 	return NULL;
40366cba3f19SStephen M. Cameron }
40376cba3f19SStephen M. Cameron 
403854b6e9e9SScott Teel /* ioaccel2 path firmware cannot handle abort task requests.
403954b6e9e9SScott Teel  * Change abort requests to physical target reset, and send to the
404054b6e9e9SScott Teel  * address of the physical disk used for the ioaccel 2 command.
404154b6e9e9SScott Teel  * Return 0 on success (IO_OK)
404254b6e9e9SScott Teel  *	 -1 on failure
404354b6e9e9SScott Teel  */
404454b6e9e9SScott Teel 
404554b6e9e9SScott Teel static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
404654b6e9e9SScott Teel 	unsigned char *scsi3addr, struct CommandList *abort)
404754b6e9e9SScott Teel {
404854b6e9e9SScott Teel 	int rc = IO_OK;
404954b6e9e9SScott Teel 	struct scsi_cmnd *scmd; /* scsi command within request being aborted */
405054b6e9e9SScott Teel 	struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
405154b6e9e9SScott Teel 	unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
405254b6e9e9SScott Teel 	unsigned char *psa = &phys_scsi3addr[0];
405354b6e9e9SScott Teel 
405454b6e9e9SScott Teel 	/* Get a pointer to the hpsa logical device. */
405554b6e9e9SScott Teel 	scmd = (struct scsi_cmnd *) abort->scsi_cmd;
405654b6e9e9SScott Teel 	dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
405754b6e9e9SScott Teel 	if (dev == NULL) {
405854b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
405954b6e9e9SScott Teel 			"Cannot abort: no device pointer for command.\n");
406054b6e9e9SScott Teel 			return -1; /* not abortable */
406154b6e9e9SScott Teel 	}
406254b6e9e9SScott Teel 
406354b6e9e9SScott Teel 	if (!dev->offload_enabled) {
406454b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
406554b6e9e9SScott Teel 			"Can't abort: device is not operating in HP SSD Smart Path mode.\n");
406654b6e9e9SScott Teel 		return -1; /* not abortable */
406754b6e9e9SScott Teel 	}
406854b6e9e9SScott Teel 
406954b6e9e9SScott Teel 	/* Incoming scsi3addr is logical addr. We need physical disk addr. */
407054b6e9e9SScott Teel 	if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
407154b6e9e9SScott Teel 		dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
407254b6e9e9SScott Teel 		return -1; /* not abortable */
407354b6e9e9SScott Teel 	}
407454b6e9e9SScott Teel 
407554b6e9e9SScott Teel 	/* send the reset */
407654b6e9e9SScott Teel 	rc = hpsa_send_reset(h, psa, HPSA_RESET_TYPE_TARGET);
407754b6e9e9SScott Teel 	if (rc != 0) {
407854b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
407954b6e9e9SScott Teel 			"Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
408054b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
408154b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
408254b6e9e9SScott Teel 		return rc; /* failed to reset */
408354b6e9e9SScott Teel 	}
408454b6e9e9SScott Teel 
408554b6e9e9SScott Teel 	/* wait for device to recover */
408654b6e9e9SScott Teel 	if (wait_for_device_to_become_ready(h, psa) != 0) {
408754b6e9e9SScott Teel 		dev_warn(&h->pdev->dev,
408854b6e9e9SScott Teel 			"Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
408954b6e9e9SScott Teel 			psa[0], psa[1], psa[2], psa[3],
409054b6e9e9SScott Teel 			psa[4], psa[5], psa[6], psa[7]);
409154b6e9e9SScott Teel 		return -1;  /* failed to recover */
409254b6e9e9SScott Teel 	}
409354b6e9e9SScott Teel 
409454b6e9e9SScott Teel 	/* device recovered */
409554b6e9e9SScott Teel 	dev_info(&h->pdev->dev,
409654b6e9e9SScott Teel 		"Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
409754b6e9e9SScott Teel 		psa[0], psa[1], psa[2], psa[3],
409854b6e9e9SScott Teel 		psa[4], psa[5], psa[6], psa[7]);
409954b6e9e9SScott Teel 
410054b6e9e9SScott Teel 	return rc; /* success */
410154b6e9e9SScott Teel }
410254b6e9e9SScott Teel 
41036cba3f19SStephen M. Cameron /* Some Smart Arrays need the abort tag swizzled, and some don't.  It's hard to
41046cba3f19SStephen M. Cameron  * tell which kind we're dealing with, so we send the abort both ways.  There
41056cba3f19SStephen M. Cameron  * shouldn't be any collisions between swizzled and unswizzled tags due to the
41066cba3f19SStephen M. Cameron  * way we construct our tags but we check anyway in case the assumptions which
41076cba3f19SStephen M. Cameron  * make this true someday become false.
41086cba3f19SStephen M. Cameron  */
41096cba3f19SStephen M. Cameron static int hpsa_send_abort_both_ways(struct ctlr_info *h,
41106cba3f19SStephen M. Cameron 	unsigned char *scsi3addr, struct CommandList *abort)
41116cba3f19SStephen M. Cameron {
41126cba3f19SStephen M. Cameron 	u8 swizzled_tag[8];
41136cba3f19SStephen M. Cameron 	struct CommandList *c;
41146cba3f19SStephen M. Cameron 	int rc = 0, rc2 = 0;
41156cba3f19SStephen M. Cameron 
411654b6e9e9SScott Teel 	/* ioccelerator mode 2 commands should be aborted via the
411754b6e9e9SScott Teel 	 * accelerated path, since RAID path is unaware of these commands,
411854b6e9e9SScott Teel 	 * but underlying firmware can't handle abort TMF.
411954b6e9e9SScott Teel 	 * Change abort to physical device reset.
412054b6e9e9SScott Teel 	 */
412154b6e9e9SScott Teel 	if (abort->cmd_type == CMD_IOACCEL2)
412254b6e9e9SScott Teel 		return hpsa_send_reset_as_abort_ioaccel2(h, scsi3addr, abort);
412354b6e9e9SScott Teel 
41246cba3f19SStephen M. Cameron 	/* we do not expect to find the swizzled tag in our queue, but
41256cba3f19SStephen M. Cameron 	 * check anyway just to be sure the assumptions which make this
41266cba3f19SStephen M. Cameron 	 * the case haven't become wrong.
41276cba3f19SStephen M. Cameron 	 */
41286cba3f19SStephen M. Cameron 	memcpy(swizzled_tag, &abort->Request.CDB[4], 8);
41296cba3f19SStephen M. Cameron 	swizzle_abort_tag(swizzled_tag);
41306cba3f19SStephen M. Cameron 	c = hpsa_find_cmd_in_queue_by_tag(h, swizzled_tag, &h->cmpQ);
41316cba3f19SStephen M. Cameron 	if (c != NULL) {
41326cba3f19SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Unexpectedly found byte-swapped tag in completion queue.\n");
41336cba3f19SStephen M. Cameron 		return hpsa_send_abort(h, scsi3addr, abort, 0);
41346cba3f19SStephen M. Cameron 	}
41356cba3f19SStephen M. Cameron 	rc = hpsa_send_abort(h, scsi3addr, abort, 0);
41366cba3f19SStephen M. Cameron 
41376cba3f19SStephen M. Cameron 	/* if the command is still in our queue, we can't conclude that it was
41386cba3f19SStephen M. Cameron 	 * aborted (it might have just completed normally) but in any case
41396cba3f19SStephen M. Cameron 	 * we don't need to try to abort it another way.
41406cba3f19SStephen M. Cameron 	 */
41416cba3f19SStephen M. Cameron 	c = hpsa_find_cmd_in_queue(h, abort->scsi_cmd, &h->cmpQ);
41426cba3f19SStephen M. Cameron 	if (c)
41436cba3f19SStephen M. Cameron 		rc2 = hpsa_send_abort(h, scsi3addr, abort, 1);
41446cba3f19SStephen M. Cameron 	return rc && rc2;
41456cba3f19SStephen M. Cameron }
41466cba3f19SStephen M. Cameron 
414775167d2cSStephen M. Cameron /* Send an abort for the specified command.
414875167d2cSStephen M. Cameron  *	If the device and controller support it,
414975167d2cSStephen M. Cameron  *		send a task abort request.
415075167d2cSStephen M. Cameron  */
415175167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
415275167d2cSStephen M. Cameron {
415375167d2cSStephen M. Cameron 
415475167d2cSStephen M. Cameron 	int i, rc;
415575167d2cSStephen M. Cameron 	struct ctlr_info *h;
415675167d2cSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
415775167d2cSStephen M. Cameron 	struct CommandList *abort; /* pointer to command to be aborted */
415875167d2cSStephen M. Cameron 	struct CommandList *found;
415975167d2cSStephen M. Cameron 	struct scsi_cmnd *as;	/* ptr to scsi cmd inside aborted command. */
416075167d2cSStephen M. Cameron 	char msg[256];		/* For debug messaging. */
416175167d2cSStephen M. Cameron 	int ml = 0;
416217eb87d2SScott Teel 	u32 tagupper, taglower;
416375167d2cSStephen M. Cameron 
416475167d2cSStephen M. Cameron 	/* Find the controller of the command to be aborted */
416575167d2cSStephen M. Cameron 	h = sdev_to_hba(sc->device);
416675167d2cSStephen M. Cameron 	if (WARN(h == NULL,
416775167d2cSStephen M. Cameron 			"ABORT REQUEST FAILED, Controller lookup failed.\n"))
416875167d2cSStephen M. Cameron 		return FAILED;
416975167d2cSStephen M. Cameron 
417075167d2cSStephen M. Cameron 	/* Check that controller supports some kind of task abort */
417175167d2cSStephen M. Cameron 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
417275167d2cSStephen M. Cameron 		!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
417375167d2cSStephen M. Cameron 		return FAILED;
417475167d2cSStephen M. Cameron 
417575167d2cSStephen M. Cameron 	memset(msg, 0, sizeof(msg));
417675167d2cSStephen M. Cameron 	ml += sprintf(msg+ml, "ABORT REQUEST on C%d:B%d:T%d:L%d ",
417775167d2cSStephen M. Cameron 		h->scsi_host->host_no, sc->device->channel,
417875167d2cSStephen M. Cameron 		sc->device->id, sc->device->lun);
417975167d2cSStephen M. Cameron 
418075167d2cSStephen M. Cameron 	/* Find the device of the command to be aborted */
418175167d2cSStephen M. Cameron 	dev = sc->device->hostdata;
418275167d2cSStephen M. Cameron 	if (!dev) {
418375167d2cSStephen M. Cameron 		dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
418475167d2cSStephen M. Cameron 				msg);
418575167d2cSStephen M. Cameron 		return FAILED;
418675167d2cSStephen M. Cameron 	}
418775167d2cSStephen M. Cameron 
418875167d2cSStephen M. Cameron 	/* Get SCSI command to be aborted */
418975167d2cSStephen M. Cameron 	abort = (struct CommandList *) sc->host_scribble;
419075167d2cSStephen M. Cameron 	if (abort == NULL) {
419175167d2cSStephen M. Cameron 		dev_err(&h->pdev->dev, "%s FAILED, Command to abort is NULL.\n",
419275167d2cSStephen M. Cameron 				msg);
419375167d2cSStephen M. Cameron 		return FAILED;
419475167d2cSStephen M. Cameron 	}
419517eb87d2SScott Teel 	hpsa_get_tag(h, abort, &taglower, &tagupper);
419617eb87d2SScott Teel 	ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
419775167d2cSStephen M. Cameron 	as  = (struct scsi_cmnd *) abort->scsi_cmd;
419875167d2cSStephen M. Cameron 	if (as != NULL)
419975167d2cSStephen M. Cameron 		ml += sprintf(msg+ml, "Command:0x%x SN:0x%lx ",
420075167d2cSStephen M. Cameron 			as->cmnd[0], as->serial_number);
420175167d2cSStephen M. Cameron 	dev_dbg(&h->pdev->dev, "%s\n", msg);
420275167d2cSStephen M. Cameron 	dev_warn(&h->pdev->dev, "Abort request on C%d:B%d:T%d:L%d\n",
420375167d2cSStephen M. Cameron 		h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
420475167d2cSStephen M. Cameron 
420575167d2cSStephen M. Cameron 	/* Search reqQ to See if command is queued but not submitted,
420675167d2cSStephen M. Cameron 	 * if so, complete the command with aborted status and remove
420775167d2cSStephen M. Cameron 	 * it from the reqQ.
420875167d2cSStephen M. Cameron 	 */
420975167d2cSStephen M. Cameron 	found = hpsa_find_cmd_in_queue(h, sc, &h->reqQ);
421075167d2cSStephen M. Cameron 	if (found) {
421175167d2cSStephen M. Cameron 		found->err_info->CommandStatus = CMD_ABORTED;
421275167d2cSStephen M. Cameron 		finish_cmd(found);
421375167d2cSStephen M. Cameron 		dev_info(&h->pdev->dev, "%s Request SUCCEEDED (driver queue).\n",
421475167d2cSStephen M. Cameron 				msg);
421575167d2cSStephen M. Cameron 		return SUCCESS;
421675167d2cSStephen M. Cameron 	}
421775167d2cSStephen M. Cameron 
421875167d2cSStephen M. Cameron 	/* not in reqQ, if also not in cmpQ, must have already completed */
421975167d2cSStephen M. Cameron 	found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
422075167d2cSStephen M. Cameron 	if (!found)  {
4221d6ebd0f7SStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s Request SUCCEEDED (not known to driver).\n",
422275167d2cSStephen M. Cameron 				msg);
422375167d2cSStephen M. Cameron 		return SUCCESS;
422475167d2cSStephen M. Cameron 	}
422575167d2cSStephen M. Cameron 
422675167d2cSStephen M. Cameron 	/*
422775167d2cSStephen M. Cameron 	 * Command is in flight, or possibly already completed
422875167d2cSStephen M. Cameron 	 * by the firmware (but not to the scsi mid layer) but we can't
422975167d2cSStephen M. Cameron 	 * distinguish which.  Send the abort down.
423075167d2cSStephen M. Cameron 	 */
42316cba3f19SStephen M. Cameron 	rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort);
423275167d2cSStephen M. Cameron 	if (rc != 0) {
423375167d2cSStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s Request FAILED.\n", msg);
423475167d2cSStephen M. Cameron 		dev_warn(&h->pdev->dev, "FAILED abort on device C%d:B%d:T%d:L%d\n",
423575167d2cSStephen M. Cameron 			h->scsi_host->host_no,
423675167d2cSStephen M. Cameron 			dev->bus, dev->target, dev->lun);
423775167d2cSStephen M. Cameron 		return FAILED;
423875167d2cSStephen M. Cameron 	}
423975167d2cSStephen M. Cameron 	dev_info(&h->pdev->dev, "%s REQUEST SUCCEEDED.\n", msg);
424075167d2cSStephen M. Cameron 
424175167d2cSStephen M. Cameron 	/* If the abort(s) above completed and actually aborted the
424275167d2cSStephen M. Cameron 	 * command, then the command to be aborted should already be
424375167d2cSStephen M. Cameron 	 * completed.  If not, wait around a bit more to see if they
424475167d2cSStephen M. Cameron 	 * manage to complete normally.
424575167d2cSStephen M. Cameron 	 */
424675167d2cSStephen M. Cameron #define ABORT_COMPLETE_WAIT_SECS 30
424775167d2cSStephen M. Cameron 	for (i = 0; i < ABORT_COMPLETE_WAIT_SECS * 10; i++) {
424875167d2cSStephen M. Cameron 		found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
424975167d2cSStephen M. Cameron 		if (!found)
425075167d2cSStephen M. Cameron 			return SUCCESS;
425175167d2cSStephen M. Cameron 		msleep(100);
425275167d2cSStephen M. Cameron 	}
425375167d2cSStephen M. Cameron 	dev_warn(&h->pdev->dev, "%s FAILED. Aborted command has not completed after %d seconds.\n",
425475167d2cSStephen M. Cameron 		msg, ABORT_COMPLETE_WAIT_SECS);
425575167d2cSStephen M. Cameron 	return FAILED;
425675167d2cSStephen M. Cameron }
425775167d2cSStephen M. Cameron 
425875167d2cSStephen M. Cameron 
4259edd16368SStephen M. Cameron /*
4260edd16368SStephen M. Cameron  * For operations that cannot sleep, a command block is allocated at init,
4261edd16368SStephen M. Cameron  * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
4262edd16368SStephen M. Cameron  * which ones are free or in use.  Lock must be held when calling this.
4263edd16368SStephen M. Cameron  * cmd_free() is the complement.
4264edd16368SStephen M. Cameron  */
4265edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h)
4266edd16368SStephen M. Cameron {
4267edd16368SStephen M. Cameron 	struct CommandList *c;
4268edd16368SStephen M. Cameron 	int i;
4269edd16368SStephen M. Cameron 	union u64bit temp64;
4270edd16368SStephen M. Cameron 	dma_addr_t cmd_dma_handle, err_dma_handle;
4271e16a33adSMatt Gates 	unsigned long flags;
4272edd16368SStephen M. Cameron 
4273e16a33adSMatt Gates 	spin_lock_irqsave(&h->lock, flags);
4274edd16368SStephen M. Cameron 	do {
4275edd16368SStephen M. Cameron 		i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
4276e16a33adSMatt Gates 		if (i == h->nr_cmds) {
4277e16a33adSMatt Gates 			spin_unlock_irqrestore(&h->lock, flags);
4278edd16368SStephen M. Cameron 			return NULL;
4279e16a33adSMatt Gates 		}
4280edd16368SStephen M. Cameron 	} while (test_and_set_bit
4281edd16368SStephen M. Cameron 		 (i & (BITS_PER_LONG - 1),
4282edd16368SStephen M. Cameron 		  h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
4283e16a33adSMatt Gates 	spin_unlock_irqrestore(&h->lock, flags);
4284e16a33adSMatt Gates 
4285edd16368SStephen M. Cameron 	c = h->cmd_pool + i;
4286edd16368SStephen M. Cameron 	memset(c, 0, sizeof(*c));
4287edd16368SStephen M. Cameron 	cmd_dma_handle = h->cmd_pool_dhandle
4288edd16368SStephen M. Cameron 	    + i * sizeof(*c);
4289edd16368SStephen M. Cameron 	c->err_info = h->errinfo_pool + i;
4290edd16368SStephen M. Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
4291edd16368SStephen M. Cameron 	err_dma_handle = h->errinfo_pool_dhandle
4292edd16368SStephen M. Cameron 	    + i * sizeof(*c->err_info);
4293edd16368SStephen M. Cameron 
4294edd16368SStephen M. Cameron 	c->cmdindex = i;
4295edd16368SStephen M. Cameron 
42969e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&c->list);
429701a02ffcSStephen M. Cameron 	c->busaddr = (u32) cmd_dma_handle;
429801a02ffcSStephen M. Cameron 	temp64.val = (u64) err_dma_handle;
4299edd16368SStephen M. Cameron 	c->ErrDesc.Addr.lower = temp64.val32.lower;
4300edd16368SStephen M. Cameron 	c->ErrDesc.Addr.upper = temp64.val32.upper;
4301edd16368SStephen M. Cameron 	c->ErrDesc.Len = sizeof(*c->err_info);
4302edd16368SStephen M. Cameron 
4303edd16368SStephen M. Cameron 	c->h = h;
4304edd16368SStephen M. Cameron 	return c;
4305edd16368SStephen M. Cameron }
4306edd16368SStephen M. Cameron 
4307edd16368SStephen M. Cameron /* For operations that can wait for kmalloc to possibly sleep,
4308edd16368SStephen M. Cameron  * this routine can be called. Lock need not be held to call
4309edd16368SStephen M. Cameron  * cmd_special_alloc. cmd_special_free() is the complement.
4310edd16368SStephen M. Cameron  */
4311edd16368SStephen M. Cameron static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
4312edd16368SStephen M. Cameron {
4313edd16368SStephen M. Cameron 	struct CommandList *c;
4314edd16368SStephen M. Cameron 	union u64bit temp64;
4315edd16368SStephen M. Cameron 	dma_addr_t cmd_dma_handle, err_dma_handle;
4316edd16368SStephen M. Cameron 
4317edd16368SStephen M. Cameron 	c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
4318edd16368SStephen M. Cameron 	if (c == NULL)
4319edd16368SStephen M. Cameron 		return NULL;
4320edd16368SStephen M. Cameron 	memset(c, 0, sizeof(*c));
4321edd16368SStephen M. Cameron 
4322e1f7de0cSMatt Gates 	c->cmd_type = CMD_SCSI;
4323edd16368SStephen M. Cameron 	c->cmdindex = -1;
4324edd16368SStephen M. Cameron 
4325edd16368SStephen M. Cameron 	c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info),
4326edd16368SStephen M. Cameron 		    &err_dma_handle);
4327edd16368SStephen M. Cameron 
4328edd16368SStephen M. Cameron 	if (c->err_info == NULL) {
4329edd16368SStephen M. Cameron 		pci_free_consistent(h->pdev,
4330edd16368SStephen M. Cameron 			sizeof(*c), c, cmd_dma_handle);
4331edd16368SStephen M. Cameron 		return NULL;
4332edd16368SStephen M. Cameron 	}
4333edd16368SStephen M. Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
4334edd16368SStephen M. Cameron 
43359e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&c->list);
433601a02ffcSStephen M. Cameron 	c->busaddr = (u32) cmd_dma_handle;
433701a02ffcSStephen M. Cameron 	temp64.val = (u64) err_dma_handle;
4338edd16368SStephen M. Cameron 	c->ErrDesc.Addr.lower = temp64.val32.lower;
4339edd16368SStephen M. Cameron 	c->ErrDesc.Addr.upper = temp64.val32.upper;
4340edd16368SStephen M. Cameron 	c->ErrDesc.Len = sizeof(*c->err_info);
4341edd16368SStephen M. Cameron 
4342edd16368SStephen M. Cameron 	c->h = h;
4343edd16368SStephen M. Cameron 	return c;
4344edd16368SStephen M. Cameron }
4345edd16368SStephen M. Cameron 
4346edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c)
4347edd16368SStephen M. Cameron {
4348edd16368SStephen M. Cameron 	int i;
4349e16a33adSMatt Gates 	unsigned long flags;
4350edd16368SStephen M. Cameron 
4351edd16368SStephen M. Cameron 	i = c - h->cmd_pool;
4352e16a33adSMatt Gates 	spin_lock_irqsave(&h->lock, flags);
4353edd16368SStephen M. Cameron 	clear_bit(i & (BITS_PER_LONG - 1),
4354edd16368SStephen M. Cameron 		  h->cmd_pool_bits + (i / BITS_PER_LONG));
4355e16a33adSMatt Gates 	spin_unlock_irqrestore(&h->lock, flags);
4356edd16368SStephen M. Cameron }
4357edd16368SStephen M. Cameron 
4358edd16368SStephen M. Cameron static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
4359edd16368SStephen M. Cameron {
4360edd16368SStephen M. Cameron 	union u64bit temp64;
4361edd16368SStephen M. Cameron 
4362edd16368SStephen M. Cameron 	temp64.val32.lower = c->ErrDesc.Addr.lower;
4363edd16368SStephen M. Cameron 	temp64.val32.upper = c->ErrDesc.Addr.upper;
4364edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev, sizeof(*c->err_info),
4365edd16368SStephen M. Cameron 			    c->err_info, (dma_addr_t) temp64.val);
4366edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev, sizeof(*c),
4367d896f3f3SStephen M. Cameron 			    c, (dma_addr_t) (c->busaddr & DIRECT_LOOKUP_MASK));
4368edd16368SStephen M. Cameron }
4369edd16368SStephen M. Cameron 
4370edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
4371edd16368SStephen M. Cameron 
4372edd16368SStephen M. Cameron static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
4373edd16368SStephen M. Cameron {
4374edd16368SStephen M. Cameron 	IOCTL32_Command_struct __user *arg32 =
4375edd16368SStephen M. Cameron 	    (IOCTL32_Command_struct __user *) arg;
4376edd16368SStephen M. Cameron 	IOCTL_Command_struct arg64;
4377edd16368SStephen M. Cameron 	IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
4378edd16368SStephen M. Cameron 	int err;
4379edd16368SStephen M. Cameron 	u32 cp;
4380edd16368SStephen M. Cameron 
4381938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
4382edd16368SStephen M. Cameron 	err = 0;
4383edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
4384edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
4385edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
4386edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
4387edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
4388edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
4389edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
4390edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
4391edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
4392edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
4393edd16368SStephen M. Cameron 
4394edd16368SStephen M. Cameron 	if (err)
4395edd16368SStephen M. Cameron 		return -EFAULT;
4396edd16368SStephen M. Cameron 
4397e39eeaedSStephen M. Cameron 	err = hpsa_ioctl(dev, CCISS_PASSTHRU, (void *)p);
4398edd16368SStephen M. Cameron 	if (err)
4399edd16368SStephen M. Cameron 		return err;
4400edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
4401edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
4402edd16368SStephen M. Cameron 	if (err)
4403edd16368SStephen M. Cameron 		return -EFAULT;
4404edd16368SStephen M. Cameron 	return err;
4405edd16368SStephen M. Cameron }
4406edd16368SStephen M. Cameron 
4407edd16368SStephen M. Cameron static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
4408edd16368SStephen M. Cameron 	int cmd, void *arg)
4409edd16368SStephen M. Cameron {
4410edd16368SStephen M. Cameron 	BIG_IOCTL32_Command_struct __user *arg32 =
4411edd16368SStephen M. Cameron 	    (BIG_IOCTL32_Command_struct __user *) arg;
4412edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct arg64;
4413edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct __user *p =
4414edd16368SStephen M. Cameron 	    compat_alloc_user_space(sizeof(arg64));
4415edd16368SStephen M. Cameron 	int err;
4416edd16368SStephen M. Cameron 	u32 cp;
4417edd16368SStephen M. Cameron 
4418938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
4419edd16368SStephen M. Cameron 	err = 0;
4420edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
4421edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
4422edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
4423edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
4424edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
4425edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
4426edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
4427edd16368SStephen M. Cameron 	err |= get_user(arg64.malloc_size, &arg32->malloc_size);
4428edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
4429edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
4430edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
4431edd16368SStephen M. Cameron 
4432edd16368SStephen M. Cameron 	if (err)
4433edd16368SStephen M. Cameron 		return -EFAULT;
4434edd16368SStephen M. Cameron 
4435e39eeaedSStephen M. Cameron 	err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
4436edd16368SStephen M. Cameron 	if (err)
4437edd16368SStephen M. Cameron 		return err;
4438edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
4439edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
4440edd16368SStephen M. Cameron 	if (err)
4441edd16368SStephen M. Cameron 		return -EFAULT;
4442edd16368SStephen M. Cameron 	return err;
4443edd16368SStephen M. Cameron }
444471fe75a7SStephen M. Cameron 
444571fe75a7SStephen M. Cameron static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
444671fe75a7SStephen M. Cameron {
444771fe75a7SStephen M. Cameron 	switch (cmd) {
444871fe75a7SStephen M. Cameron 	case CCISS_GETPCIINFO:
444971fe75a7SStephen M. Cameron 	case CCISS_GETINTINFO:
445071fe75a7SStephen M. Cameron 	case CCISS_SETINTINFO:
445171fe75a7SStephen M. Cameron 	case CCISS_GETNODENAME:
445271fe75a7SStephen M. Cameron 	case CCISS_SETNODENAME:
445371fe75a7SStephen M. Cameron 	case CCISS_GETHEARTBEAT:
445471fe75a7SStephen M. Cameron 	case CCISS_GETBUSTYPES:
445571fe75a7SStephen M. Cameron 	case CCISS_GETFIRMVER:
445671fe75a7SStephen M. Cameron 	case CCISS_GETDRIVVER:
445771fe75a7SStephen M. Cameron 	case CCISS_REVALIDVOLS:
445871fe75a7SStephen M. Cameron 	case CCISS_DEREGDISK:
445971fe75a7SStephen M. Cameron 	case CCISS_REGNEWDISK:
446071fe75a7SStephen M. Cameron 	case CCISS_REGNEWD:
446171fe75a7SStephen M. Cameron 	case CCISS_RESCANDISK:
446271fe75a7SStephen M. Cameron 	case CCISS_GETLUNINFO:
446371fe75a7SStephen M. Cameron 		return hpsa_ioctl(dev, cmd, arg);
446471fe75a7SStephen M. Cameron 
446571fe75a7SStephen M. Cameron 	case CCISS_PASSTHRU32:
446671fe75a7SStephen M. Cameron 		return hpsa_ioctl32_passthru(dev, cmd, arg);
446771fe75a7SStephen M. Cameron 	case CCISS_BIG_PASSTHRU32:
446871fe75a7SStephen M. Cameron 		return hpsa_ioctl32_big_passthru(dev, cmd, arg);
446971fe75a7SStephen M. Cameron 
447071fe75a7SStephen M. Cameron 	default:
447171fe75a7SStephen M. Cameron 		return -ENOIOCTLCMD;
447271fe75a7SStephen M. Cameron 	}
447371fe75a7SStephen M. Cameron }
4474edd16368SStephen M. Cameron #endif
4475edd16368SStephen M. Cameron 
4476edd16368SStephen M. Cameron static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
4477edd16368SStephen M. Cameron {
4478edd16368SStephen M. Cameron 	struct hpsa_pci_info pciinfo;
4479edd16368SStephen M. Cameron 
4480edd16368SStephen M. Cameron 	if (!argp)
4481edd16368SStephen M. Cameron 		return -EINVAL;
4482edd16368SStephen M. Cameron 	pciinfo.domain = pci_domain_nr(h->pdev->bus);
4483edd16368SStephen M. Cameron 	pciinfo.bus = h->pdev->bus->number;
4484edd16368SStephen M. Cameron 	pciinfo.dev_fn = h->pdev->devfn;
4485edd16368SStephen M. Cameron 	pciinfo.board_id = h->board_id;
4486edd16368SStephen M. Cameron 	if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
4487edd16368SStephen M. Cameron 		return -EFAULT;
4488edd16368SStephen M. Cameron 	return 0;
4489edd16368SStephen M. Cameron }
4490edd16368SStephen M. Cameron 
4491edd16368SStephen M. Cameron static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
4492edd16368SStephen M. Cameron {
4493edd16368SStephen M. Cameron 	DriverVer_type DriverVer;
4494edd16368SStephen M. Cameron 	unsigned char vmaj, vmin, vsubmin;
4495edd16368SStephen M. Cameron 	int rc;
4496edd16368SStephen M. Cameron 
4497edd16368SStephen M. Cameron 	rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
4498edd16368SStephen M. Cameron 		&vmaj, &vmin, &vsubmin);
4499edd16368SStephen M. Cameron 	if (rc != 3) {
4500edd16368SStephen M. Cameron 		dev_info(&h->pdev->dev, "driver version string '%s' "
4501edd16368SStephen M. Cameron 			"unrecognized.", HPSA_DRIVER_VERSION);
4502edd16368SStephen M. Cameron 		vmaj = 0;
4503edd16368SStephen M. Cameron 		vmin = 0;
4504edd16368SStephen M. Cameron 		vsubmin = 0;
4505edd16368SStephen M. Cameron 	}
4506edd16368SStephen M. Cameron 	DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
4507edd16368SStephen M. Cameron 	if (!argp)
4508edd16368SStephen M. Cameron 		return -EINVAL;
4509edd16368SStephen M. Cameron 	if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
4510edd16368SStephen M. Cameron 		return -EFAULT;
4511edd16368SStephen M. Cameron 	return 0;
4512edd16368SStephen M. Cameron }
4513edd16368SStephen M. Cameron 
4514edd16368SStephen M. Cameron static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
4515edd16368SStephen M. Cameron {
4516edd16368SStephen M. Cameron 	IOCTL_Command_struct iocommand;
4517edd16368SStephen M. Cameron 	struct CommandList *c;
4518edd16368SStephen M. Cameron 	char *buff = NULL;
4519edd16368SStephen M. Cameron 	union u64bit temp64;
4520c1f63c8fSStephen M. Cameron 	int rc = 0;
4521edd16368SStephen M. Cameron 
4522edd16368SStephen M. Cameron 	if (!argp)
4523edd16368SStephen M. Cameron 		return -EINVAL;
4524edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
4525edd16368SStephen M. Cameron 		return -EPERM;
4526edd16368SStephen M. Cameron 	if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
4527edd16368SStephen M. Cameron 		return -EFAULT;
4528edd16368SStephen M. Cameron 	if ((iocommand.buf_size < 1) &&
4529edd16368SStephen M. Cameron 	    (iocommand.Request.Type.Direction != XFER_NONE)) {
4530edd16368SStephen M. Cameron 		return -EINVAL;
4531edd16368SStephen M. Cameron 	}
4532edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
4533edd16368SStephen M. Cameron 		buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
4534edd16368SStephen M. Cameron 		if (buff == NULL)
4535edd16368SStephen M. Cameron 			return -EFAULT;
4536edd16368SStephen M. Cameron 		if (iocommand.Request.Type.Direction == XFER_WRITE) {
4537edd16368SStephen M. Cameron 			/* Copy the data into the buffer we created */
4538b03a7771SStephen M. Cameron 			if (copy_from_user(buff, iocommand.buf,
4539b03a7771SStephen M. Cameron 				iocommand.buf_size)) {
4540c1f63c8fSStephen M. Cameron 				rc = -EFAULT;
4541c1f63c8fSStephen M. Cameron 				goto out_kfree;
4542edd16368SStephen M. Cameron 			}
4543b03a7771SStephen M. Cameron 		} else {
4544edd16368SStephen M. Cameron 			memset(buff, 0, iocommand.buf_size);
4545b03a7771SStephen M. Cameron 		}
4546b03a7771SStephen M. Cameron 	}
4547edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
4548edd16368SStephen M. Cameron 	if (c == NULL) {
4549c1f63c8fSStephen M. Cameron 		rc = -ENOMEM;
4550c1f63c8fSStephen M. Cameron 		goto out_kfree;
4551edd16368SStephen M. Cameron 	}
4552edd16368SStephen M. Cameron 	/* Fill in the command type */
4553edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
4554edd16368SStephen M. Cameron 	/* Fill in Command Header */
4555edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0; /* unused in simple mode */
4556edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {	/* buffer to fill */
4557edd16368SStephen M. Cameron 		c->Header.SGList = 1;
4558edd16368SStephen M. Cameron 		c->Header.SGTotal = 1;
4559edd16368SStephen M. Cameron 	} else	{ /* no buffers to fill */
4560edd16368SStephen M. Cameron 		c->Header.SGList = 0;
4561edd16368SStephen M. Cameron 		c->Header.SGTotal = 0;
4562edd16368SStephen M. Cameron 	}
4563edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
4564edd16368SStephen M. Cameron 	/* use the kernel address the cmd block for tag */
4565edd16368SStephen M. Cameron 	c->Header.Tag.lower = c->busaddr;
4566edd16368SStephen M. Cameron 
4567edd16368SStephen M. Cameron 	/* Fill in Request block */
4568edd16368SStephen M. Cameron 	memcpy(&c->Request, &iocommand.Request,
4569edd16368SStephen M. Cameron 		sizeof(c->Request));
4570edd16368SStephen M. Cameron 
4571edd16368SStephen M. Cameron 	/* Fill in the scatter gather information */
4572edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
4573edd16368SStephen M. Cameron 		temp64.val = pci_map_single(h->pdev, buff,
4574edd16368SStephen M. Cameron 			iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
4575bcc48ffaSStephen M. Cameron 		if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
4576bcc48ffaSStephen M. Cameron 			c->SG[0].Addr.lower = 0;
4577bcc48ffaSStephen M. Cameron 			c->SG[0].Addr.upper = 0;
4578bcc48ffaSStephen M. Cameron 			c->SG[0].Len = 0;
4579bcc48ffaSStephen M. Cameron 			rc = -ENOMEM;
4580bcc48ffaSStephen M. Cameron 			goto out;
4581bcc48ffaSStephen M. Cameron 		}
4582edd16368SStephen M. Cameron 		c->SG[0].Addr.lower = temp64.val32.lower;
4583edd16368SStephen M. Cameron 		c->SG[0].Addr.upper = temp64.val32.upper;
4584edd16368SStephen M. Cameron 		c->SG[0].Len = iocommand.buf_size;
4585e1d9cbfaSMatt Gates 		c->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining*/
4586edd16368SStephen M. Cameron 	}
4587a0c12413SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
4588c2dd32e0SStephen M. Cameron 	if (iocommand.buf_size > 0)
4589edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
4590edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
4591edd16368SStephen M. Cameron 
4592edd16368SStephen M. Cameron 	/* Copy the error information out */
4593edd16368SStephen M. Cameron 	memcpy(&iocommand.error_info, c->err_info,
4594edd16368SStephen M. Cameron 		sizeof(iocommand.error_info));
4595edd16368SStephen M. Cameron 	if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
4596c1f63c8fSStephen M. Cameron 		rc = -EFAULT;
4597c1f63c8fSStephen M. Cameron 		goto out;
4598edd16368SStephen M. Cameron 	}
4599b03a7771SStephen M. Cameron 	if (iocommand.Request.Type.Direction == XFER_READ &&
4600b03a7771SStephen M. Cameron 		iocommand.buf_size > 0) {
4601edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
4602edd16368SStephen M. Cameron 		if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
4603c1f63c8fSStephen M. Cameron 			rc = -EFAULT;
4604c1f63c8fSStephen M. Cameron 			goto out;
4605edd16368SStephen M. Cameron 		}
4606edd16368SStephen M. Cameron 	}
4607c1f63c8fSStephen M. Cameron out:
4608edd16368SStephen M. Cameron 	cmd_special_free(h, c);
4609c1f63c8fSStephen M. Cameron out_kfree:
4610c1f63c8fSStephen M. Cameron 	kfree(buff);
4611c1f63c8fSStephen M. Cameron 	return rc;
4612edd16368SStephen M. Cameron }
4613edd16368SStephen M. Cameron 
4614edd16368SStephen M. Cameron static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
4615edd16368SStephen M. Cameron {
4616edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct *ioc;
4617edd16368SStephen M. Cameron 	struct CommandList *c;
4618edd16368SStephen M. Cameron 	unsigned char **buff = NULL;
4619edd16368SStephen M. Cameron 	int *buff_size = NULL;
4620edd16368SStephen M. Cameron 	union u64bit temp64;
4621edd16368SStephen M. Cameron 	BYTE sg_used = 0;
4622edd16368SStephen M. Cameron 	int status = 0;
4623edd16368SStephen M. Cameron 	int i;
462401a02ffcSStephen M. Cameron 	u32 left;
462501a02ffcSStephen M. Cameron 	u32 sz;
4626edd16368SStephen M. Cameron 	BYTE __user *data_ptr;
4627edd16368SStephen M. Cameron 
4628edd16368SStephen M. Cameron 	if (!argp)
4629edd16368SStephen M. Cameron 		return -EINVAL;
4630edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
4631edd16368SStephen M. Cameron 		return -EPERM;
4632edd16368SStephen M. Cameron 	ioc = (BIG_IOCTL_Command_struct *)
4633edd16368SStephen M. Cameron 	    kmalloc(sizeof(*ioc), GFP_KERNEL);
4634edd16368SStephen M. Cameron 	if (!ioc) {
4635edd16368SStephen M. Cameron 		status = -ENOMEM;
4636edd16368SStephen M. Cameron 		goto cleanup1;
4637edd16368SStephen M. Cameron 	}
4638edd16368SStephen M. Cameron 	if (copy_from_user(ioc, argp, sizeof(*ioc))) {
4639edd16368SStephen M. Cameron 		status = -EFAULT;
4640edd16368SStephen M. Cameron 		goto cleanup1;
4641edd16368SStephen M. Cameron 	}
4642edd16368SStephen M. Cameron 	if ((ioc->buf_size < 1) &&
4643edd16368SStephen M. Cameron 	    (ioc->Request.Type.Direction != XFER_NONE)) {
4644edd16368SStephen M. Cameron 		status = -EINVAL;
4645edd16368SStephen M. Cameron 		goto cleanup1;
4646edd16368SStephen M. Cameron 	}
4647edd16368SStephen M. Cameron 	/* Check kmalloc limits  using all SGs */
4648edd16368SStephen M. Cameron 	if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
4649edd16368SStephen M. Cameron 		status = -EINVAL;
4650edd16368SStephen M. Cameron 		goto cleanup1;
4651edd16368SStephen M. Cameron 	}
4652d66ae08bSStephen M. Cameron 	if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
4653edd16368SStephen M. Cameron 		status = -EINVAL;
4654edd16368SStephen M. Cameron 		goto cleanup1;
4655edd16368SStephen M. Cameron 	}
4656d66ae08bSStephen M. Cameron 	buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
4657edd16368SStephen M. Cameron 	if (!buff) {
4658edd16368SStephen M. Cameron 		status = -ENOMEM;
4659edd16368SStephen M. Cameron 		goto cleanup1;
4660edd16368SStephen M. Cameron 	}
4661d66ae08bSStephen M. Cameron 	buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
4662edd16368SStephen M. Cameron 	if (!buff_size) {
4663edd16368SStephen M. Cameron 		status = -ENOMEM;
4664edd16368SStephen M. Cameron 		goto cleanup1;
4665edd16368SStephen M. Cameron 	}
4666edd16368SStephen M. Cameron 	left = ioc->buf_size;
4667edd16368SStephen M. Cameron 	data_ptr = ioc->buf;
4668edd16368SStephen M. Cameron 	while (left) {
4669edd16368SStephen M. Cameron 		sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
4670edd16368SStephen M. Cameron 		buff_size[sg_used] = sz;
4671edd16368SStephen M. Cameron 		buff[sg_used] = kmalloc(sz, GFP_KERNEL);
4672edd16368SStephen M. Cameron 		if (buff[sg_used] == NULL) {
4673edd16368SStephen M. Cameron 			status = -ENOMEM;
4674edd16368SStephen M. Cameron 			goto cleanup1;
4675edd16368SStephen M. Cameron 		}
4676edd16368SStephen M. Cameron 		if (ioc->Request.Type.Direction == XFER_WRITE) {
4677edd16368SStephen M. Cameron 			if (copy_from_user(buff[sg_used], data_ptr, sz)) {
4678edd16368SStephen M. Cameron 				status = -ENOMEM;
4679edd16368SStephen M. Cameron 				goto cleanup1;
4680edd16368SStephen M. Cameron 			}
4681edd16368SStephen M. Cameron 		} else
4682edd16368SStephen M. Cameron 			memset(buff[sg_used], 0, sz);
4683edd16368SStephen M. Cameron 		left -= sz;
4684edd16368SStephen M. Cameron 		data_ptr += sz;
4685edd16368SStephen M. Cameron 		sg_used++;
4686edd16368SStephen M. Cameron 	}
4687edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
4688edd16368SStephen M. Cameron 	if (c == NULL) {
4689edd16368SStephen M. Cameron 		status = -ENOMEM;
4690edd16368SStephen M. Cameron 		goto cleanup1;
4691edd16368SStephen M. Cameron 	}
4692edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
4693edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
4694b03a7771SStephen M. Cameron 	c->Header.SGList = c->Header.SGTotal = sg_used;
4695edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
4696edd16368SStephen M. Cameron 	c->Header.Tag.lower = c->busaddr;
4697edd16368SStephen M. Cameron 	memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
4698edd16368SStephen M. Cameron 	if (ioc->buf_size > 0) {
4699edd16368SStephen M. Cameron 		int i;
4700edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
4701edd16368SStephen M. Cameron 			temp64.val = pci_map_single(h->pdev, buff[i],
4702edd16368SStephen M. Cameron 				    buff_size[i], PCI_DMA_BIDIRECTIONAL);
4703bcc48ffaSStephen M. Cameron 			if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
4704bcc48ffaSStephen M. Cameron 				c->SG[i].Addr.lower = 0;
4705bcc48ffaSStephen M. Cameron 				c->SG[i].Addr.upper = 0;
4706bcc48ffaSStephen M. Cameron 				c->SG[i].Len = 0;
4707bcc48ffaSStephen M. Cameron 				hpsa_pci_unmap(h->pdev, c, i,
4708bcc48ffaSStephen M. Cameron 					PCI_DMA_BIDIRECTIONAL);
4709bcc48ffaSStephen M. Cameron 				status = -ENOMEM;
4710e2d4a1f6SStephen M. Cameron 				goto cleanup0;
4711bcc48ffaSStephen M. Cameron 			}
4712edd16368SStephen M. Cameron 			c->SG[i].Addr.lower = temp64.val32.lower;
4713edd16368SStephen M. Cameron 			c->SG[i].Addr.upper = temp64.val32.upper;
4714edd16368SStephen M. Cameron 			c->SG[i].Len = buff_size[i];
4715e1d9cbfaSMatt Gates 			c->SG[i].Ext = i < sg_used - 1 ? 0 : HPSA_SG_LAST;
4716edd16368SStephen M. Cameron 		}
4717edd16368SStephen M. Cameron 	}
4718a0c12413SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
4719b03a7771SStephen M. Cameron 	if (sg_used)
4720edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
4721edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
4722edd16368SStephen M. Cameron 	/* Copy the error information out */
4723edd16368SStephen M. Cameron 	memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
4724edd16368SStephen M. Cameron 	if (copy_to_user(argp, ioc, sizeof(*ioc))) {
4725edd16368SStephen M. Cameron 		status = -EFAULT;
4726e2d4a1f6SStephen M. Cameron 		goto cleanup0;
4727edd16368SStephen M. Cameron 	}
4728b03a7771SStephen M. Cameron 	if (ioc->Request.Type.Direction == XFER_READ && ioc->buf_size > 0) {
4729edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
4730edd16368SStephen M. Cameron 		BYTE __user *ptr = ioc->buf;
4731edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
4732edd16368SStephen M. Cameron 			if (copy_to_user(ptr, buff[i], buff_size[i])) {
4733edd16368SStephen M. Cameron 				status = -EFAULT;
4734e2d4a1f6SStephen M. Cameron 				goto cleanup0;
4735edd16368SStephen M. Cameron 			}
4736edd16368SStephen M. Cameron 			ptr += buff_size[i];
4737edd16368SStephen M. Cameron 		}
4738edd16368SStephen M. Cameron 	}
4739edd16368SStephen M. Cameron 	status = 0;
4740e2d4a1f6SStephen M. Cameron cleanup0:
4741e2d4a1f6SStephen M. Cameron 	cmd_special_free(h, c);
4742edd16368SStephen M. Cameron cleanup1:
4743edd16368SStephen M. Cameron 	if (buff) {
4744edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++)
4745edd16368SStephen M. Cameron 			kfree(buff[i]);
4746edd16368SStephen M. Cameron 		kfree(buff);
4747edd16368SStephen M. Cameron 	}
4748edd16368SStephen M. Cameron 	kfree(buff_size);
4749edd16368SStephen M. Cameron 	kfree(ioc);
4750edd16368SStephen M. Cameron 	return status;
4751edd16368SStephen M. Cameron }
4752edd16368SStephen M. Cameron 
4753edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
4754edd16368SStephen M. Cameron 	struct CommandList *c)
4755edd16368SStephen M. Cameron {
4756edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
4757edd16368SStephen M. Cameron 			c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
4758edd16368SStephen M. Cameron 		(void) check_for_unit_attention(h, c);
4759edd16368SStephen M. Cameron }
47600390f0c0SStephen M. Cameron 
47610390f0c0SStephen M. Cameron static int increment_passthru_count(struct ctlr_info *h)
47620390f0c0SStephen M. Cameron {
47630390f0c0SStephen M. Cameron 	unsigned long flags;
47640390f0c0SStephen M. Cameron 
47650390f0c0SStephen M. Cameron 	spin_lock_irqsave(&h->passthru_count_lock, flags);
47660390f0c0SStephen M. Cameron 	if (h->passthru_count >= HPSA_MAX_CONCURRENT_PASSTHRUS) {
47670390f0c0SStephen M. Cameron 		spin_unlock_irqrestore(&h->passthru_count_lock, flags);
47680390f0c0SStephen M. Cameron 		return -1;
47690390f0c0SStephen M. Cameron 	}
47700390f0c0SStephen M. Cameron 	h->passthru_count++;
47710390f0c0SStephen M. Cameron 	spin_unlock_irqrestore(&h->passthru_count_lock, flags);
47720390f0c0SStephen M. Cameron 	return 0;
47730390f0c0SStephen M. Cameron }
47740390f0c0SStephen M. Cameron 
47750390f0c0SStephen M. Cameron static void decrement_passthru_count(struct ctlr_info *h)
47760390f0c0SStephen M. Cameron {
47770390f0c0SStephen M. Cameron 	unsigned long flags;
47780390f0c0SStephen M. Cameron 
47790390f0c0SStephen M. Cameron 	spin_lock_irqsave(&h->passthru_count_lock, flags);
47800390f0c0SStephen M. Cameron 	if (h->passthru_count <= 0) {
47810390f0c0SStephen M. Cameron 		spin_unlock_irqrestore(&h->passthru_count_lock, flags);
47820390f0c0SStephen M. Cameron 		/* not expecting to get here. */
47830390f0c0SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Bug detected, passthru_count seems to be incorrect.\n");
47840390f0c0SStephen M. Cameron 		return;
47850390f0c0SStephen M. Cameron 	}
47860390f0c0SStephen M. Cameron 	h->passthru_count--;
47870390f0c0SStephen M. Cameron 	spin_unlock_irqrestore(&h->passthru_count_lock, flags);
47880390f0c0SStephen M. Cameron }
47890390f0c0SStephen M. Cameron 
4790edd16368SStephen M. Cameron /*
4791edd16368SStephen M. Cameron  * ioctl
4792edd16368SStephen M. Cameron  */
4793edd16368SStephen M. Cameron static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
4794edd16368SStephen M. Cameron {
4795edd16368SStephen M. Cameron 	struct ctlr_info *h;
4796edd16368SStephen M. Cameron 	void __user *argp = (void __user *)arg;
47970390f0c0SStephen M. Cameron 	int rc;
4798edd16368SStephen M. Cameron 
4799edd16368SStephen M. Cameron 	h = sdev_to_hba(dev);
4800edd16368SStephen M. Cameron 
4801edd16368SStephen M. Cameron 	switch (cmd) {
4802edd16368SStephen M. Cameron 	case CCISS_DEREGDISK:
4803edd16368SStephen M. Cameron 	case CCISS_REGNEWDISK:
4804edd16368SStephen M. Cameron 	case CCISS_REGNEWD:
4805a08a8471SStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
4806edd16368SStephen M. Cameron 		return 0;
4807edd16368SStephen M. Cameron 	case CCISS_GETPCIINFO:
4808edd16368SStephen M. Cameron 		return hpsa_getpciinfo_ioctl(h, argp);
4809edd16368SStephen M. Cameron 	case CCISS_GETDRIVVER:
4810edd16368SStephen M. Cameron 		return hpsa_getdrivver_ioctl(h, argp);
4811edd16368SStephen M. Cameron 	case CCISS_PASSTHRU:
48120390f0c0SStephen M. Cameron 		if (increment_passthru_count(h))
48130390f0c0SStephen M. Cameron 			return -EAGAIN;
48140390f0c0SStephen M. Cameron 		rc = hpsa_passthru_ioctl(h, argp);
48150390f0c0SStephen M. Cameron 		decrement_passthru_count(h);
48160390f0c0SStephen M. Cameron 		return rc;
4817edd16368SStephen M. Cameron 	case CCISS_BIG_PASSTHRU:
48180390f0c0SStephen M. Cameron 		if (increment_passthru_count(h))
48190390f0c0SStephen M. Cameron 			return -EAGAIN;
48200390f0c0SStephen M. Cameron 		rc = hpsa_big_passthru_ioctl(h, argp);
48210390f0c0SStephen M. Cameron 		decrement_passthru_count(h);
48220390f0c0SStephen M. Cameron 		return rc;
4823edd16368SStephen M. Cameron 	default:
4824edd16368SStephen M. Cameron 		return -ENOTTY;
4825edd16368SStephen M. Cameron 	}
4826edd16368SStephen M. Cameron }
4827edd16368SStephen M. Cameron 
48286f039790SGreg Kroah-Hartman static int hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
48296f039790SGreg Kroah-Hartman 				u8 reset_type)
483064670ac8SStephen M. Cameron {
483164670ac8SStephen M. Cameron 	struct CommandList *c;
483264670ac8SStephen M. Cameron 
483364670ac8SStephen M. Cameron 	c = cmd_alloc(h);
483464670ac8SStephen M. Cameron 	if (!c)
483564670ac8SStephen M. Cameron 		return -ENOMEM;
4836a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map */
4837a2dac136SStephen M. Cameron 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
483864670ac8SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_MSG);
483964670ac8SStephen M. Cameron 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
484064670ac8SStephen M. Cameron 	c->waiting = NULL;
484164670ac8SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
484264670ac8SStephen M. Cameron 	/* Don't wait for completion, the reset won't complete.  Don't free
484364670ac8SStephen M. Cameron 	 * the command either.  This is the last command we will send before
484464670ac8SStephen M. Cameron 	 * re-initializing everything, so it doesn't matter and won't leak.
484564670ac8SStephen M. Cameron 	 */
484664670ac8SStephen M. Cameron 	return 0;
484764670ac8SStephen M. Cameron }
484864670ac8SStephen M. Cameron 
4849a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
4850b7bb24ebSStephen M. Cameron 	void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
4851edd16368SStephen M. Cameron 	int cmd_type)
4852edd16368SStephen M. Cameron {
4853edd16368SStephen M. Cameron 	int pci_dir = XFER_NONE;
485475167d2cSStephen M. Cameron 	struct CommandList *a; /* for commands to be aborted */
4855edd16368SStephen M. Cameron 
4856edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
4857edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
4858edd16368SStephen M. Cameron 	if (buff != NULL && size > 0) {
4859edd16368SStephen M. Cameron 		c->Header.SGList = 1;
4860edd16368SStephen M. Cameron 		c->Header.SGTotal = 1;
4861edd16368SStephen M. Cameron 	} else {
4862edd16368SStephen M. Cameron 		c->Header.SGList = 0;
4863edd16368SStephen M. Cameron 		c->Header.SGTotal = 0;
4864edd16368SStephen M. Cameron 	}
4865edd16368SStephen M. Cameron 	c->Header.Tag.lower = c->busaddr;
4866edd16368SStephen M. Cameron 	memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
4867edd16368SStephen M. Cameron 
4868edd16368SStephen M. Cameron 	c->Request.Type.Type = cmd_type;
4869edd16368SStephen M. Cameron 	if (cmd_type == TYPE_CMD) {
4870edd16368SStephen M. Cameron 		switch (cmd) {
4871edd16368SStephen M. Cameron 		case HPSA_INQUIRY:
4872edd16368SStephen M. Cameron 			/* are we trying to read a vital product page */
4873b7bb24ebSStephen M. Cameron 			if (page_code & VPD_PAGE) {
4874edd16368SStephen M. Cameron 				c->Request.CDB[1] = 0x01;
4875b7bb24ebSStephen M. Cameron 				c->Request.CDB[2] = (page_code & 0xff);
4876edd16368SStephen M. Cameron 			}
4877edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
4878edd16368SStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
4879edd16368SStephen M. Cameron 			c->Request.Type.Direction = XFER_READ;
4880edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
4881edd16368SStephen M. Cameron 			c->Request.CDB[0] = HPSA_INQUIRY;
4882edd16368SStephen M. Cameron 			c->Request.CDB[4] = size & 0xFF;
4883edd16368SStephen M. Cameron 			break;
4884edd16368SStephen M. Cameron 		case HPSA_REPORT_LOG:
4885edd16368SStephen M. Cameron 		case HPSA_REPORT_PHYS:
4886edd16368SStephen M. Cameron 			/* Talking to controller so It's a physical command
4887edd16368SStephen M. Cameron 			   mode = 00 target = 0.  Nothing to write.
4888edd16368SStephen M. Cameron 			 */
4889edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
4890edd16368SStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
4891edd16368SStephen M. Cameron 			c->Request.Type.Direction = XFER_READ;
4892edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
4893edd16368SStephen M. Cameron 			c->Request.CDB[0] = cmd;
4894edd16368SStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
4895edd16368SStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
4896edd16368SStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
4897edd16368SStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
4898edd16368SStephen M. Cameron 			break;
4899edd16368SStephen M. Cameron 		case HPSA_CACHE_FLUSH:
4900edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
4901edd16368SStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
4902edd16368SStephen M. Cameron 			c->Request.Type.Direction = XFER_WRITE;
4903edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
4904edd16368SStephen M. Cameron 			c->Request.CDB[0] = BMIC_WRITE;
4905edd16368SStephen M. Cameron 			c->Request.CDB[6] = BMIC_CACHE_FLUSH;
4906bb158eabSStephen M. Cameron 			c->Request.CDB[7] = (size >> 8) & 0xFF;
4907bb158eabSStephen M. Cameron 			c->Request.CDB[8] = size & 0xFF;
4908edd16368SStephen M. Cameron 			break;
4909edd16368SStephen M. Cameron 		case TEST_UNIT_READY:
4910edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
4911edd16368SStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
4912edd16368SStephen M. Cameron 			c->Request.Type.Direction = XFER_NONE;
4913edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
4914edd16368SStephen M. Cameron 			break;
4915283b4a9bSStephen M. Cameron 		case HPSA_GET_RAID_MAP:
4916283b4a9bSStephen M. Cameron 			c->Request.CDBLen = 12;
4917283b4a9bSStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
4918283b4a9bSStephen M. Cameron 			c->Request.Type.Direction = XFER_READ;
4919283b4a9bSStephen M. Cameron 			c->Request.Timeout = 0;
4920283b4a9bSStephen M. Cameron 			c->Request.CDB[0] = HPSA_CISS_READ;
4921283b4a9bSStephen M. Cameron 			c->Request.CDB[1] = cmd;
4922283b4a9bSStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
4923283b4a9bSStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
4924283b4a9bSStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
4925283b4a9bSStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
4926283b4a9bSStephen M. Cameron 			break;
4927edd16368SStephen M. Cameron 		default:
4928edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
4929edd16368SStephen M. Cameron 			BUG();
4930a2dac136SStephen M. Cameron 			return -1;
4931edd16368SStephen M. Cameron 		}
4932edd16368SStephen M. Cameron 	} else if (cmd_type == TYPE_MSG) {
4933edd16368SStephen M. Cameron 		switch (cmd) {
4934edd16368SStephen M. Cameron 
4935edd16368SStephen M. Cameron 		case  HPSA_DEVICE_RESET_MSG:
4936edd16368SStephen M. Cameron 			c->Request.CDBLen = 16;
4937edd16368SStephen M. Cameron 			c->Request.Type.Type =  1; /* It is a MSG not a CMD */
4938edd16368SStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
4939edd16368SStephen M. Cameron 			c->Request.Type.Direction = XFER_NONE;
4940edd16368SStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
494164670ac8SStephen M. Cameron 			memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
494264670ac8SStephen M. Cameron 			c->Request.CDB[0] =  cmd;
494321e89afdSStephen M. Cameron 			c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
4944edd16368SStephen M. Cameron 			/* If bytes 4-7 are zero, it means reset the */
4945edd16368SStephen M. Cameron 			/* LunID device */
4946edd16368SStephen M. Cameron 			c->Request.CDB[4] = 0x00;
4947edd16368SStephen M. Cameron 			c->Request.CDB[5] = 0x00;
4948edd16368SStephen M. Cameron 			c->Request.CDB[6] = 0x00;
4949edd16368SStephen M. Cameron 			c->Request.CDB[7] = 0x00;
4950edd16368SStephen M. Cameron 			break;
495175167d2cSStephen M. Cameron 		case  HPSA_ABORT_MSG:
495275167d2cSStephen M. Cameron 			a = buff;       /* point to command to be aborted */
495375167d2cSStephen M. Cameron 			dev_dbg(&h->pdev->dev, "Abort Tag:0x%08x:%08x using request Tag:0x%08x:%08x\n",
495475167d2cSStephen M. Cameron 				a->Header.Tag.upper, a->Header.Tag.lower,
495575167d2cSStephen M. Cameron 				c->Header.Tag.upper, c->Header.Tag.lower);
495675167d2cSStephen M. Cameron 			c->Request.CDBLen = 16;
495775167d2cSStephen M. Cameron 			c->Request.Type.Type = TYPE_MSG;
495875167d2cSStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
495975167d2cSStephen M. Cameron 			c->Request.Type.Direction = XFER_WRITE;
496075167d2cSStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
496175167d2cSStephen M. Cameron 			c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
496275167d2cSStephen M. Cameron 			c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
496375167d2cSStephen M. Cameron 			c->Request.CDB[2] = 0x00; /* reserved */
496475167d2cSStephen M. Cameron 			c->Request.CDB[3] = 0x00; /* reserved */
496575167d2cSStephen M. Cameron 			/* Tag to abort goes in CDB[4]-CDB[11] */
496675167d2cSStephen M. Cameron 			c->Request.CDB[4] = a->Header.Tag.lower & 0xFF;
496775167d2cSStephen M. Cameron 			c->Request.CDB[5] = (a->Header.Tag.lower >> 8) & 0xFF;
496875167d2cSStephen M. Cameron 			c->Request.CDB[6] = (a->Header.Tag.lower >> 16) & 0xFF;
496975167d2cSStephen M. Cameron 			c->Request.CDB[7] = (a->Header.Tag.lower >> 24) & 0xFF;
497075167d2cSStephen M. Cameron 			c->Request.CDB[8] = a->Header.Tag.upper & 0xFF;
497175167d2cSStephen M. Cameron 			c->Request.CDB[9] = (a->Header.Tag.upper >> 8) & 0xFF;
497275167d2cSStephen M. Cameron 			c->Request.CDB[10] = (a->Header.Tag.upper >> 16) & 0xFF;
497375167d2cSStephen M. Cameron 			c->Request.CDB[11] = (a->Header.Tag.upper >> 24) & 0xFF;
497475167d2cSStephen M. Cameron 			c->Request.CDB[12] = 0x00; /* reserved */
497575167d2cSStephen M. Cameron 			c->Request.CDB[13] = 0x00; /* reserved */
497675167d2cSStephen M. Cameron 			c->Request.CDB[14] = 0x00; /* reserved */
497775167d2cSStephen M. Cameron 			c->Request.CDB[15] = 0x00; /* reserved */
497875167d2cSStephen M. Cameron 		break;
4979edd16368SStephen M. Cameron 		default:
4980edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown message type %d\n",
4981edd16368SStephen M. Cameron 				cmd);
4982edd16368SStephen M. Cameron 			BUG();
4983edd16368SStephen M. Cameron 		}
4984edd16368SStephen M. Cameron 	} else {
4985edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
4986edd16368SStephen M. Cameron 		BUG();
4987edd16368SStephen M. Cameron 	}
4988edd16368SStephen M. Cameron 
4989edd16368SStephen M. Cameron 	switch (c->Request.Type.Direction) {
4990edd16368SStephen M. Cameron 	case XFER_READ:
4991edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_FROMDEVICE;
4992edd16368SStephen M. Cameron 		break;
4993edd16368SStephen M. Cameron 	case XFER_WRITE:
4994edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_TODEVICE;
4995edd16368SStephen M. Cameron 		break;
4996edd16368SStephen M. Cameron 	case XFER_NONE:
4997edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_NONE;
4998edd16368SStephen M. Cameron 		break;
4999edd16368SStephen M. Cameron 	default:
5000edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_BIDIRECTIONAL;
5001edd16368SStephen M. Cameron 	}
5002a2dac136SStephen M. Cameron 	if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
5003a2dac136SStephen M. Cameron 		return -1;
5004a2dac136SStephen M. Cameron 	return 0;
5005edd16368SStephen M. Cameron }
5006edd16368SStephen M. Cameron 
5007edd16368SStephen M. Cameron /*
5008edd16368SStephen M. Cameron  * Map (physical) PCI mem into (virtual) kernel space
5009edd16368SStephen M. Cameron  */
5010edd16368SStephen M. Cameron static void __iomem *remap_pci_mem(ulong base, ulong size)
5011edd16368SStephen M. Cameron {
5012edd16368SStephen M. Cameron 	ulong page_base = ((ulong) base) & PAGE_MASK;
5013edd16368SStephen M. Cameron 	ulong page_offs = ((ulong) base) - page_base;
5014088ba34cSStephen M. Cameron 	void __iomem *page_remapped = ioremap_nocache(page_base,
5015088ba34cSStephen M. Cameron 		page_offs + size);
5016edd16368SStephen M. Cameron 
5017edd16368SStephen M. Cameron 	return page_remapped ? (page_remapped + page_offs) : NULL;
5018edd16368SStephen M. Cameron }
5019edd16368SStephen M. Cameron 
5020edd16368SStephen M. Cameron /* Takes cmds off the submission queue and sends them to the hardware,
5021edd16368SStephen M. Cameron  * then puts them on the queue of cmds waiting for completion.
5022edd16368SStephen M. Cameron  */
5023edd16368SStephen M. Cameron static void start_io(struct ctlr_info *h)
5024edd16368SStephen M. Cameron {
5025edd16368SStephen M. Cameron 	struct CommandList *c;
5026e16a33adSMatt Gates 	unsigned long flags;
5027edd16368SStephen M. Cameron 
5028e16a33adSMatt Gates 	spin_lock_irqsave(&h->lock, flags);
50299e0fc764SStephen M. Cameron 	while (!list_empty(&h->reqQ)) {
50309e0fc764SStephen M. Cameron 		c = list_entry(h->reqQ.next, struct CommandList, list);
5031edd16368SStephen M. Cameron 		/* can't do anything if fifo is full */
5032edd16368SStephen M. Cameron 		if ((h->access.fifo_full(h))) {
5033396883e2SStephen M. Cameron 			h->fifo_recently_full = 1;
5034edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "fifo full\n");
5035edd16368SStephen M. Cameron 			break;
5036edd16368SStephen M. Cameron 		}
5037396883e2SStephen M. Cameron 		h->fifo_recently_full = 0;
5038edd16368SStephen M. Cameron 
5039edd16368SStephen M. Cameron 		/* Get the first entry from the Request Q */
5040edd16368SStephen M. Cameron 		removeQ(c);
5041edd16368SStephen M. Cameron 		h->Qdepth--;
5042edd16368SStephen M. Cameron 
5043edd16368SStephen M. Cameron 		/* Put job onto the completed Q */
5044edd16368SStephen M. Cameron 		addQ(&h->cmpQ, c);
5045e16a33adSMatt Gates 
5046e16a33adSMatt Gates 		/* Must increment commands_outstanding before unlocking
5047e16a33adSMatt Gates 		 * and submitting to avoid race checking for fifo full
5048e16a33adSMatt Gates 		 * condition.
5049e16a33adSMatt Gates 		 */
5050e16a33adSMatt Gates 		h->commands_outstanding++;
5051e16a33adSMatt Gates 		if (h->commands_outstanding > h->max_outstanding)
5052e16a33adSMatt Gates 			h->max_outstanding = h->commands_outstanding;
5053e16a33adSMatt Gates 
5054e16a33adSMatt Gates 		/* Tell the controller execute command */
5055e16a33adSMatt Gates 		spin_unlock_irqrestore(&h->lock, flags);
5056e16a33adSMatt Gates 		h->access.submit_command(h, c);
5057e16a33adSMatt Gates 		spin_lock_irqsave(&h->lock, flags);
5058edd16368SStephen M. Cameron 	}
5059e16a33adSMatt Gates 	spin_unlock_irqrestore(&h->lock, flags);
5060edd16368SStephen M. Cameron }
5061edd16368SStephen M. Cameron 
5062254f796bSMatt Gates static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
5063edd16368SStephen M. Cameron {
5064254f796bSMatt Gates 	return h->access.command_completed(h, q);
5065edd16368SStephen M. Cameron }
5066edd16368SStephen M. Cameron 
5067900c5440SStephen M. Cameron static inline bool interrupt_pending(struct ctlr_info *h)
5068edd16368SStephen M. Cameron {
5069edd16368SStephen M. Cameron 	return h->access.intr_pending(h);
5070edd16368SStephen M. Cameron }
5071edd16368SStephen M. Cameron 
5072edd16368SStephen M. Cameron static inline long interrupt_not_for_us(struct ctlr_info *h)
5073edd16368SStephen M. Cameron {
507410f66018SStephen M. Cameron 	return (h->access.intr_pending(h) == 0) ||
507510f66018SStephen M. Cameron 		(h->interrupts_enabled == 0);
5076edd16368SStephen M. Cameron }
5077edd16368SStephen M. Cameron 
507801a02ffcSStephen M. Cameron static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
507901a02ffcSStephen M. Cameron 	u32 raw_tag)
5080edd16368SStephen M. Cameron {
5081edd16368SStephen M. Cameron 	if (unlikely(tag_index >= h->nr_cmds)) {
5082edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
5083edd16368SStephen M. Cameron 		return 1;
5084edd16368SStephen M. Cameron 	}
5085edd16368SStephen M. Cameron 	return 0;
5086edd16368SStephen M. Cameron }
5087edd16368SStephen M. Cameron 
50885a3d16f5SStephen M. Cameron static inline void finish_cmd(struct CommandList *c)
5089edd16368SStephen M. Cameron {
5090e16a33adSMatt Gates 	unsigned long flags;
5091396883e2SStephen M. Cameron 	int io_may_be_stalled = 0;
5092396883e2SStephen M. Cameron 	struct ctlr_info *h = c->h;
5093e16a33adSMatt Gates 
5094396883e2SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
5095edd16368SStephen M. Cameron 	removeQ(c);
5096396883e2SStephen M. Cameron 
5097396883e2SStephen M. Cameron 	/*
5098396883e2SStephen M. Cameron 	 * Check for possibly stalled i/o.
5099396883e2SStephen M. Cameron 	 *
5100396883e2SStephen M. Cameron 	 * If a fifo_full condition is encountered, requests will back up
5101396883e2SStephen M. Cameron 	 * in h->reqQ.  This queue is only emptied out by start_io which is
5102396883e2SStephen M. Cameron 	 * only called when a new i/o request comes in.  If no i/o's are
5103396883e2SStephen M. Cameron 	 * forthcoming, the i/o's in h->reqQ can get stuck.  So we call
5104396883e2SStephen M. Cameron 	 * start_io from here if we detect such a danger.
5105396883e2SStephen M. Cameron 	 *
5106396883e2SStephen M. Cameron 	 * Normally, we shouldn't hit this case, but pounding on the
5107396883e2SStephen M. Cameron 	 * CCISS_PASSTHRU ioctl can provoke it.  Only call start_io if
5108396883e2SStephen M. Cameron 	 * commands_outstanding is low.  We want to avoid calling
5109396883e2SStephen M. Cameron 	 * start_io from in here as much as possible, and esp. don't
5110396883e2SStephen M. Cameron 	 * want to get in a cycle where we call start_io every time
5111396883e2SStephen M. Cameron 	 * through here.
5112396883e2SStephen M. Cameron 	 */
5113396883e2SStephen M. Cameron 	if (unlikely(h->fifo_recently_full) &&
5114396883e2SStephen M. Cameron 		h->commands_outstanding < 5)
5115396883e2SStephen M. Cameron 		io_may_be_stalled = 1;
5116396883e2SStephen M. Cameron 
5117396883e2SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
5118396883e2SStephen M. Cameron 
5119e85c5974SStephen M. Cameron 	dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
5120c349775eSScott Teel 	if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
5121c349775eSScott Teel 			|| c->cmd_type == CMD_IOACCEL2))
51221fb011fbSStephen M. Cameron 		complete_scsi_command(c);
5123edd16368SStephen M. Cameron 	else if (c->cmd_type == CMD_IOCTL_PEND)
5124edd16368SStephen M. Cameron 		complete(c->waiting);
5125396883e2SStephen M. Cameron 	if (unlikely(io_may_be_stalled))
5126396883e2SStephen M. Cameron 		start_io(h);
5127edd16368SStephen M. Cameron }
5128edd16368SStephen M. Cameron 
5129a104c99fSStephen M. Cameron static inline u32 hpsa_tag_contains_index(u32 tag)
5130a104c99fSStephen M. Cameron {
5131a104c99fSStephen M. Cameron 	return tag & DIRECT_LOOKUP_BIT;
5132a104c99fSStephen M. Cameron }
5133a104c99fSStephen M. Cameron 
5134a104c99fSStephen M. Cameron static inline u32 hpsa_tag_to_index(u32 tag)
5135a104c99fSStephen M. Cameron {
5136a104c99fSStephen M. Cameron 	return tag >> DIRECT_LOOKUP_SHIFT;
5137a104c99fSStephen M. Cameron }
5138a104c99fSStephen M. Cameron 
5139a9a3a273SStephen M. Cameron 
5140a9a3a273SStephen M. Cameron static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
5141a104c99fSStephen M. Cameron {
5142a9a3a273SStephen M. Cameron #define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
5143a9a3a273SStephen M. Cameron #define HPSA_SIMPLE_ERROR_BITS 0x03
5144960a30e7SStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
5145a9a3a273SStephen M. Cameron 		return tag & ~HPSA_SIMPLE_ERROR_BITS;
5146a9a3a273SStephen M. Cameron 	return tag & ~HPSA_PERF_ERROR_BITS;
5147a104c99fSStephen M. Cameron }
5148a104c99fSStephen M. Cameron 
5149303932fdSDon Brace /* process completion of an indexed ("direct lookup") command */
51501d94f94dSStephen M. Cameron static inline void process_indexed_cmd(struct ctlr_info *h,
5151303932fdSDon Brace 	u32 raw_tag)
5152303932fdSDon Brace {
5153303932fdSDon Brace 	u32 tag_index;
5154303932fdSDon Brace 	struct CommandList *c;
5155303932fdSDon Brace 
5156303932fdSDon Brace 	tag_index = hpsa_tag_to_index(raw_tag);
51571d94f94dSStephen M. Cameron 	if (!bad_tag(h, tag_index, raw_tag)) {
5158303932fdSDon Brace 		c = h->cmd_pool + tag_index;
51595a3d16f5SStephen M. Cameron 		finish_cmd(c);
51601d94f94dSStephen M. Cameron 	}
5161303932fdSDon Brace }
5162303932fdSDon Brace 
5163303932fdSDon Brace /* process completion of a non-indexed command */
51641d94f94dSStephen M. Cameron static inline void process_nonindexed_cmd(struct ctlr_info *h,
5165303932fdSDon Brace 	u32 raw_tag)
5166303932fdSDon Brace {
5167303932fdSDon Brace 	u32 tag;
5168303932fdSDon Brace 	struct CommandList *c = NULL;
5169e16a33adSMatt Gates 	unsigned long flags;
5170303932fdSDon Brace 
5171a9a3a273SStephen M. Cameron 	tag = hpsa_tag_discard_error_bits(h, raw_tag);
5172e16a33adSMatt Gates 	spin_lock_irqsave(&h->lock, flags);
51739e0fc764SStephen M. Cameron 	list_for_each_entry(c, &h->cmpQ, list) {
5174303932fdSDon Brace 		if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
5175e16a33adSMatt Gates 			spin_unlock_irqrestore(&h->lock, flags);
51765a3d16f5SStephen M. Cameron 			finish_cmd(c);
51771d94f94dSStephen M. Cameron 			return;
5178303932fdSDon Brace 		}
5179303932fdSDon Brace 	}
5180e16a33adSMatt Gates 	spin_unlock_irqrestore(&h->lock, flags);
5181303932fdSDon Brace 	bad_tag(h, h->nr_cmds + 1, raw_tag);
5182303932fdSDon Brace }
5183303932fdSDon Brace 
518464670ac8SStephen M. Cameron /* Some controllers, like p400, will give us one interrupt
518564670ac8SStephen M. Cameron  * after a soft reset, even if we turned interrupts off.
518664670ac8SStephen M. Cameron  * Only need to check for this in the hpsa_xxx_discard_completions
518764670ac8SStephen M. Cameron  * functions.
518864670ac8SStephen M. Cameron  */
518964670ac8SStephen M. Cameron static int ignore_bogus_interrupt(struct ctlr_info *h)
519064670ac8SStephen M. Cameron {
519164670ac8SStephen M. Cameron 	if (likely(!reset_devices))
519264670ac8SStephen M. Cameron 		return 0;
519364670ac8SStephen M. Cameron 
519464670ac8SStephen M. Cameron 	if (likely(h->interrupts_enabled))
519564670ac8SStephen M. Cameron 		return 0;
519664670ac8SStephen M. Cameron 
519764670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
519864670ac8SStephen M. Cameron 		"(known firmware bug.)  Ignoring.\n");
519964670ac8SStephen M. Cameron 
520064670ac8SStephen M. Cameron 	return 1;
520164670ac8SStephen M. Cameron }
520264670ac8SStephen M. Cameron 
5203254f796bSMatt Gates /*
5204254f796bSMatt Gates  * Convert &h->q[x] (passed to interrupt handlers) back to h.
5205254f796bSMatt Gates  * Relies on (h-q[x] == x) being true for x such that
5206254f796bSMatt Gates  * 0 <= x < MAX_REPLY_QUEUES.
5207254f796bSMatt Gates  */
5208254f796bSMatt Gates static struct ctlr_info *queue_to_hba(u8 *queue)
520964670ac8SStephen M. Cameron {
5210254f796bSMatt Gates 	return container_of((queue - *queue), struct ctlr_info, q[0]);
5211254f796bSMatt Gates }
5212254f796bSMatt Gates 
5213254f796bSMatt Gates static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
5214254f796bSMatt Gates {
5215254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
5216254f796bSMatt Gates 	u8 q = *(u8 *) queue;
521764670ac8SStephen M. Cameron 	u32 raw_tag;
521864670ac8SStephen M. Cameron 
521964670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
522064670ac8SStephen M. Cameron 		return IRQ_NONE;
522164670ac8SStephen M. Cameron 
522264670ac8SStephen M. Cameron 	if (interrupt_not_for_us(h))
522364670ac8SStephen M. Cameron 		return IRQ_NONE;
5224a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
522564670ac8SStephen M. Cameron 	while (interrupt_pending(h)) {
5226254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
522764670ac8SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY)
5228254f796bSMatt Gates 			raw_tag = next_command(h, q);
522964670ac8SStephen M. Cameron 	}
523064670ac8SStephen M. Cameron 	return IRQ_HANDLED;
523164670ac8SStephen M. Cameron }
523264670ac8SStephen M. Cameron 
5233254f796bSMatt Gates static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
523464670ac8SStephen M. Cameron {
5235254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
523664670ac8SStephen M. Cameron 	u32 raw_tag;
5237254f796bSMatt Gates 	u8 q = *(u8 *) queue;
523864670ac8SStephen M. Cameron 
523964670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
524064670ac8SStephen M. Cameron 		return IRQ_NONE;
524164670ac8SStephen M. Cameron 
5242a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
5243254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
524464670ac8SStephen M. Cameron 	while (raw_tag != FIFO_EMPTY)
5245254f796bSMatt Gates 		raw_tag = next_command(h, q);
524664670ac8SStephen M. Cameron 	return IRQ_HANDLED;
524764670ac8SStephen M. Cameron }
524864670ac8SStephen M. Cameron 
5249254f796bSMatt Gates static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
5250edd16368SStephen M. Cameron {
5251254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba((u8 *) queue);
5252303932fdSDon Brace 	u32 raw_tag;
5253254f796bSMatt Gates 	u8 q = *(u8 *) queue;
5254edd16368SStephen M. Cameron 
5255edd16368SStephen M. Cameron 	if (interrupt_not_for_us(h))
5256edd16368SStephen M. Cameron 		return IRQ_NONE;
5257a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
525810f66018SStephen M. Cameron 	while (interrupt_pending(h)) {
5259254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
526010f66018SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY) {
52611d94f94dSStephen M. Cameron 			if (likely(hpsa_tag_contains_index(raw_tag)))
52621d94f94dSStephen M. Cameron 				process_indexed_cmd(h, raw_tag);
526310f66018SStephen M. Cameron 			else
52641d94f94dSStephen M. Cameron 				process_nonindexed_cmd(h, raw_tag);
5265254f796bSMatt Gates 			raw_tag = next_command(h, q);
526610f66018SStephen M. Cameron 		}
526710f66018SStephen M. Cameron 	}
526810f66018SStephen M. Cameron 	return IRQ_HANDLED;
526910f66018SStephen M. Cameron }
527010f66018SStephen M. Cameron 
5271254f796bSMatt Gates static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
527210f66018SStephen M. Cameron {
5273254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
527410f66018SStephen M. Cameron 	u32 raw_tag;
5275254f796bSMatt Gates 	u8 q = *(u8 *) queue;
527610f66018SStephen M. Cameron 
5277a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
5278254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
5279303932fdSDon Brace 	while (raw_tag != FIFO_EMPTY) {
52801d94f94dSStephen M. Cameron 		if (likely(hpsa_tag_contains_index(raw_tag)))
52811d94f94dSStephen M. Cameron 			process_indexed_cmd(h, raw_tag);
5282303932fdSDon Brace 		else
52831d94f94dSStephen M. Cameron 			process_nonindexed_cmd(h, raw_tag);
5284254f796bSMatt Gates 		raw_tag = next_command(h, q);
5285edd16368SStephen M. Cameron 	}
5286edd16368SStephen M. Cameron 	return IRQ_HANDLED;
5287edd16368SStephen M. Cameron }
5288edd16368SStephen M. Cameron 
5289a9a3a273SStephen M. Cameron /* Send a message CDB to the firmware. Careful, this only works
5290a9a3a273SStephen M. Cameron  * in simple mode, not performant mode due to the tag lookup.
5291a9a3a273SStephen M. Cameron  * We only ever use this immediately after a controller reset.
5292a9a3a273SStephen M. Cameron  */
52936f039790SGreg Kroah-Hartman static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
5294edd16368SStephen M. Cameron 			unsigned char type)
5295edd16368SStephen M. Cameron {
5296edd16368SStephen M. Cameron 	struct Command {
5297edd16368SStephen M. Cameron 		struct CommandListHeader CommandHeader;
5298edd16368SStephen M. Cameron 		struct RequestBlock Request;
5299edd16368SStephen M. Cameron 		struct ErrDescriptor ErrorDescriptor;
5300edd16368SStephen M. Cameron 	};
5301edd16368SStephen M. Cameron 	struct Command *cmd;
5302edd16368SStephen M. Cameron 	static const size_t cmd_sz = sizeof(*cmd) +
5303edd16368SStephen M. Cameron 					sizeof(cmd->ErrorDescriptor);
5304edd16368SStephen M. Cameron 	dma_addr_t paddr64;
5305edd16368SStephen M. Cameron 	uint32_t paddr32, tag;
5306edd16368SStephen M. Cameron 	void __iomem *vaddr;
5307edd16368SStephen M. Cameron 	int i, err;
5308edd16368SStephen M. Cameron 
5309edd16368SStephen M. Cameron 	vaddr = pci_ioremap_bar(pdev, 0);
5310edd16368SStephen M. Cameron 	if (vaddr == NULL)
5311edd16368SStephen M. Cameron 		return -ENOMEM;
5312edd16368SStephen M. Cameron 
5313edd16368SStephen M. Cameron 	/* The Inbound Post Queue only accepts 32-bit physical addresses for the
5314edd16368SStephen M. Cameron 	 * CCISS commands, so they must be allocated from the lower 4GiB of
5315edd16368SStephen M. Cameron 	 * memory.
5316edd16368SStephen M. Cameron 	 */
5317edd16368SStephen M. Cameron 	err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
5318edd16368SStephen M. Cameron 	if (err) {
5319edd16368SStephen M. Cameron 		iounmap(vaddr);
5320edd16368SStephen M. Cameron 		return -ENOMEM;
5321edd16368SStephen M. Cameron 	}
5322edd16368SStephen M. Cameron 
5323edd16368SStephen M. Cameron 	cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
5324edd16368SStephen M. Cameron 	if (cmd == NULL) {
5325edd16368SStephen M. Cameron 		iounmap(vaddr);
5326edd16368SStephen M. Cameron 		return -ENOMEM;
5327edd16368SStephen M. Cameron 	}
5328edd16368SStephen M. Cameron 
5329edd16368SStephen M. Cameron 	/* This must fit, because of the 32-bit consistent DMA mask.  Also,
5330edd16368SStephen M. Cameron 	 * although there's no guarantee, we assume that the address is at
5331edd16368SStephen M. Cameron 	 * least 4-byte aligned (most likely, it's page-aligned).
5332edd16368SStephen M. Cameron 	 */
5333edd16368SStephen M. Cameron 	paddr32 = paddr64;
5334edd16368SStephen M. Cameron 
5335edd16368SStephen M. Cameron 	cmd->CommandHeader.ReplyQueue = 0;
5336edd16368SStephen M. Cameron 	cmd->CommandHeader.SGList = 0;
5337edd16368SStephen M. Cameron 	cmd->CommandHeader.SGTotal = 0;
5338edd16368SStephen M. Cameron 	cmd->CommandHeader.Tag.lower = paddr32;
5339edd16368SStephen M. Cameron 	cmd->CommandHeader.Tag.upper = 0;
5340edd16368SStephen M. Cameron 	memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
5341edd16368SStephen M. Cameron 
5342edd16368SStephen M. Cameron 	cmd->Request.CDBLen = 16;
5343edd16368SStephen M. Cameron 	cmd->Request.Type.Type = TYPE_MSG;
5344edd16368SStephen M. Cameron 	cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
5345edd16368SStephen M. Cameron 	cmd->Request.Type.Direction = XFER_NONE;
5346edd16368SStephen M. Cameron 	cmd->Request.Timeout = 0; /* Don't time out */
5347edd16368SStephen M. Cameron 	cmd->Request.CDB[0] = opcode;
5348edd16368SStephen M. Cameron 	cmd->Request.CDB[1] = type;
5349edd16368SStephen M. Cameron 	memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
5350edd16368SStephen M. Cameron 	cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
5351edd16368SStephen M. Cameron 	cmd->ErrorDescriptor.Addr.upper = 0;
5352edd16368SStephen M. Cameron 	cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
5353edd16368SStephen M. Cameron 
5354edd16368SStephen M. Cameron 	writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
5355edd16368SStephen M. Cameron 
5356edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
5357edd16368SStephen M. Cameron 		tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
5358a9a3a273SStephen M. Cameron 		if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr32)
5359edd16368SStephen M. Cameron 			break;
5360edd16368SStephen M. Cameron 		msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
5361edd16368SStephen M. Cameron 	}
5362edd16368SStephen M. Cameron 
5363edd16368SStephen M. Cameron 	iounmap(vaddr);
5364edd16368SStephen M. Cameron 
5365edd16368SStephen M. Cameron 	/* we leak the DMA buffer here ... no choice since the controller could
5366edd16368SStephen M. Cameron 	 *  still complete the command.
5367edd16368SStephen M. Cameron 	 */
5368edd16368SStephen M. Cameron 	if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
5369edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
5370edd16368SStephen M. Cameron 			opcode, type);
5371edd16368SStephen M. Cameron 		return -ETIMEDOUT;
5372edd16368SStephen M. Cameron 	}
5373edd16368SStephen M. Cameron 
5374edd16368SStephen M. Cameron 	pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
5375edd16368SStephen M. Cameron 
5376edd16368SStephen M. Cameron 	if (tag & HPSA_ERROR_BIT) {
5377edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
5378edd16368SStephen M. Cameron 			opcode, type);
5379edd16368SStephen M. Cameron 		return -EIO;
5380edd16368SStephen M. Cameron 	}
5381edd16368SStephen M. Cameron 
5382edd16368SStephen M. Cameron 	dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
5383edd16368SStephen M. Cameron 		opcode, type);
5384edd16368SStephen M. Cameron 	return 0;
5385edd16368SStephen M. Cameron }
5386edd16368SStephen M. Cameron 
5387edd16368SStephen M. Cameron #define hpsa_noop(p) hpsa_message(p, 3, 0)
5388edd16368SStephen M. Cameron 
53891df8552aSStephen M. Cameron static int hpsa_controller_hard_reset(struct pci_dev *pdev,
5390cf0b08d0SStephen M. Cameron 	void * __iomem vaddr, u32 use_doorbell)
5391edd16368SStephen M. Cameron {
53921df8552aSStephen M. Cameron 	u16 pmcsr;
53931df8552aSStephen M. Cameron 	int pos;
5394edd16368SStephen M. Cameron 
53951df8552aSStephen M. Cameron 	if (use_doorbell) {
53961df8552aSStephen M. Cameron 		/* For everything after the P600, the PCI power state method
53971df8552aSStephen M. Cameron 		 * of resetting the controller doesn't work, so we have this
53981df8552aSStephen M. Cameron 		 * other way using the doorbell register.
5399edd16368SStephen M. Cameron 		 */
54001df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using doorbell to reset controller\n");
5401cf0b08d0SStephen M. Cameron 		writel(use_doorbell, vaddr + SA5_DOORBELL);
540285009239SStephen M. Cameron 
540385009239SStephen M. Cameron 		/* PMC hardware guys tell us we need a 5 second delay after
540485009239SStephen M. Cameron 		 * doorbell reset and before any attempt to talk to the board
540585009239SStephen M. Cameron 		 * at all to ensure that this actually works and doesn't fall
540685009239SStephen M. Cameron 		 * over in some weird corner cases.
540785009239SStephen M. Cameron 		 */
540885009239SStephen M. Cameron 		msleep(5000);
54091df8552aSStephen M. Cameron 	} else { /* Try to do it the PCI power state way */
5410edd16368SStephen M. Cameron 
5411edd16368SStephen M. Cameron 		/* Quoting from the Open CISS Specification: "The Power
5412edd16368SStephen M. Cameron 		 * Management Control/Status Register (CSR) controls the power
5413edd16368SStephen M. Cameron 		 * state of the device.  The normal operating state is D0,
5414edd16368SStephen M. Cameron 		 * CSR=00h.  The software off state is D3, CSR=03h.  To reset
54151df8552aSStephen M. Cameron 		 * the controller, place the interface device in D3 then to D0,
54161df8552aSStephen M. Cameron 		 * this causes a secondary PCI reset which will reset the
54171df8552aSStephen M. Cameron 		 * controller." */
5418edd16368SStephen M. Cameron 
54191df8552aSStephen M. Cameron 		pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
54201df8552aSStephen M. Cameron 		if (pos == 0) {
54211df8552aSStephen M. Cameron 			dev_err(&pdev->dev,
54221df8552aSStephen M. Cameron 				"hpsa_reset_controller: "
54231df8552aSStephen M. Cameron 				"PCI PM not supported\n");
54241df8552aSStephen M. Cameron 			return -ENODEV;
54251df8552aSStephen M. Cameron 		}
54261df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using PCI PM to reset controller\n");
5427edd16368SStephen M. Cameron 		/* enter the D3hot power management state */
5428edd16368SStephen M. Cameron 		pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
5429edd16368SStephen M. Cameron 		pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
5430edd16368SStephen M. Cameron 		pmcsr |= PCI_D3hot;
5431edd16368SStephen M. Cameron 		pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
5432edd16368SStephen M. Cameron 
5433edd16368SStephen M. Cameron 		msleep(500);
5434edd16368SStephen M. Cameron 
5435edd16368SStephen M. Cameron 		/* enter the D0 power management state */
5436edd16368SStephen M. Cameron 		pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
5437edd16368SStephen M. Cameron 		pmcsr |= PCI_D0;
5438edd16368SStephen M. Cameron 		pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
5439c4853efeSMike Miller 
5440c4853efeSMike Miller 		/*
5441c4853efeSMike Miller 		 * The P600 requires a small delay when changing states.
5442c4853efeSMike Miller 		 * Otherwise we may think the board did not reset and we bail.
5443c4853efeSMike Miller 		 * This for kdump only and is particular to the P600.
5444c4853efeSMike Miller 		 */
5445c4853efeSMike Miller 		msleep(500);
54461df8552aSStephen M. Cameron 	}
54471df8552aSStephen M. Cameron 	return 0;
54481df8552aSStephen M. Cameron }
54491df8552aSStephen M. Cameron 
54506f039790SGreg Kroah-Hartman static void init_driver_version(char *driver_version, int len)
5451580ada3cSStephen M. Cameron {
5452580ada3cSStephen M. Cameron 	memset(driver_version, 0, len);
5453f79cfec6SStephen M. Cameron 	strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
5454580ada3cSStephen M. Cameron }
5455580ada3cSStephen M. Cameron 
54566f039790SGreg Kroah-Hartman static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
5457580ada3cSStephen M. Cameron {
5458580ada3cSStephen M. Cameron 	char *driver_version;
5459580ada3cSStephen M. Cameron 	int i, size = sizeof(cfgtable->driver_version);
5460580ada3cSStephen M. Cameron 
5461580ada3cSStephen M. Cameron 	driver_version = kmalloc(size, GFP_KERNEL);
5462580ada3cSStephen M. Cameron 	if (!driver_version)
5463580ada3cSStephen M. Cameron 		return -ENOMEM;
5464580ada3cSStephen M. Cameron 
5465580ada3cSStephen M. Cameron 	init_driver_version(driver_version, size);
5466580ada3cSStephen M. Cameron 	for (i = 0; i < size; i++)
5467580ada3cSStephen M. Cameron 		writeb(driver_version[i], &cfgtable->driver_version[i]);
5468580ada3cSStephen M. Cameron 	kfree(driver_version);
5469580ada3cSStephen M. Cameron 	return 0;
5470580ada3cSStephen M. Cameron }
5471580ada3cSStephen M. Cameron 
54726f039790SGreg Kroah-Hartman static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
54736f039790SGreg Kroah-Hartman 					  unsigned char *driver_ver)
5474580ada3cSStephen M. Cameron {
5475580ada3cSStephen M. Cameron 	int i;
5476580ada3cSStephen M. Cameron 
5477580ada3cSStephen M. Cameron 	for (i = 0; i < sizeof(cfgtable->driver_version); i++)
5478580ada3cSStephen M. Cameron 		driver_ver[i] = readb(&cfgtable->driver_version[i]);
5479580ada3cSStephen M. Cameron }
5480580ada3cSStephen M. Cameron 
54816f039790SGreg Kroah-Hartman static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
5482580ada3cSStephen M. Cameron {
5483580ada3cSStephen M. Cameron 
5484580ada3cSStephen M. Cameron 	char *driver_ver, *old_driver_ver;
5485580ada3cSStephen M. Cameron 	int rc, size = sizeof(cfgtable->driver_version);
5486580ada3cSStephen M. Cameron 
5487580ada3cSStephen M. Cameron 	old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
5488580ada3cSStephen M. Cameron 	if (!old_driver_ver)
5489580ada3cSStephen M. Cameron 		return -ENOMEM;
5490580ada3cSStephen M. Cameron 	driver_ver = old_driver_ver + size;
5491580ada3cSStephen M. Cameron 
5492580ada3cSStephen M. Cameron 	/* After a reset, the 32 bytes of "driver version" in the cfgtable
5493580ada3cSStephen M. Cameron 	 * should have been changed, otherwise we know the reset failed.
5494580ada3cSStephen M. Cameron 	 */
5495580ada3cSStephen M. Cameron 	init_driver_version(old_driver_ver, size);
5496580ada3cSStephen M. Cameron 	read_driver_ver_from_cfgtable(cfgtable, driver_ver);
5497580ada3cSStephen M. Cameron 	rc = !memcmp(driver_ver, old_driver_ver, size);
5498580ada3cSStephen M. Cameron 	kfree(old_driver_ver);
5499580ada3cSStephen M. Cameron 	return rc;
5500580ada3cSStephen M. Cameron }
55011df8552aSStephen M. Cameron /* This does a hard reset of the controller using PCI power management
55021df8552aSStephen M. Cameron  * states or the using the doorbell register.
55031df8552aSStephen M. Cameron  */
55046f039790SGreg Kroah-Hartman static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev)
55051df8552aSStephen M. Cameron {
55061df8552aSStephen M. Cameron 	u64 cfg_offset;
55071df8552aSStephen M. Cameron 	u32 cfg_base_addr;
55081df8552aSStephen M. Cameron 	u64 cfg_base_addr_index;
55091df8552aSStephen M. Cameron 	void __iomem *vaddr;
55101df8552aSStephen M. Cameron 	unsigned long paddr;
5511580ada3cSStephen M. Cameron 	u32 misc_fw_support;
5512270d05deSStephen M. Cameron 	int rc;
55131df8552aSStephen M. Cameron 	struct CfgTable __iomem *cfgtable;
5514cf0b08d0SStephen M. Cameron 	u32 use_doorbell;
551518867659SStephen M. Cameron 	u32 board_id;
5516270d05deSStephen M. Cameron 	u16 command_register;
55171df8552aSStephen M. Cameron 
55181df8552aSStephen M. Cameron 	/* For controllers as old as the P600, this is very nearly
55191df8552aSStephen M. Cameron 	 * the same thing as
55201df8552aSStephen M. Cameron 	 *
55211df8552aSStephen M. Cameron 	 * pci_save_state(pci_dev);
55221df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D3hot);
55231df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D0);
55241df8552aSStephen M. Cameron 	 * pci_restore_state(pci_dev);
55251df8552aSStephen M. Cameron 	 *
55261df8552aSStephen M. Cameron 	 * For controllers newer than the P600, the pci power state
55271df8552aSStephen M. Cameron 	 * method of resetting doesn't work so we have another way
55281df8552aSStephen M. Cameron 	 * using the doorbell register.
55291df8552aSStephen M. Cameron 	 */
553018867659SStephen M. Cameron 
553125c1e56aSStephen M. Cameron 	rc = hpsa_lookup_board_id(pdev, &board_id);
553246380786SStephen M. Cameron 	if (rc < 0 || !ctlr_is_resettable(board_id)) {
553325c1e56aSStephen M. Cameron 		dev_warn(&pdev->dev, "Not resetting device.\n");
553425c1e56aSStephen M. Cameron 		return -ENODEV;
553525c1e56aSStephen M. Cameron 	}
553646380786SStephen M. Cameron 
553746380786SStephen M. Cameron 	/* if controller is soft- but not hard resettable... */
553846380786SStephen M. Cameron 	if (!ctlr_is_hard_resettable(board_id))
553946380786SStephen M. Cameron 		return -ENOTSUPP; /* try soft reset later. */
554018867659SStephen M. Cameron 
5541270d05deSStephen M. Cameron 	/* Save the PCI command register */
5542270d05deSStephen M. Cameron 	pci_read_config_word(pdev, 4, &command_register);
5543270d05deSStephen M. Cameron 	/* Turn the board off.  This is so that later pci_restore_state()
5544270d05deSStephen M. Cameron 	 * won't turn the board on before the rest of config space is ready.
5545270d05deSStephen M. Cameron 	 */
5546270d05deSStephen M. Cameron 	pci_disable_device(pdev);
5547270d05deSStephen M. Cameron 	pci_save_state(pdev);
55481df8552aSStephen M. Cameron 
55491df8552aSStephen M. Cameron 	/* find the first memory BAR, so we can find the cfg table */
55501df8552aSStephen M. Cameron 	rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
55511df8552aSStephen M. Cameron 	if (rc)
55521df8552aSStephen M. Cameron 		return rc;
55531df8552aSStephen M. Cameron 	vaddr = remap_pci_mem(paddr, 0x250);
55541df8552aSStephen M. Cameron 	if (!vaddr)
55551df8552aSStephen M. Cameron 		return -ENOMEM;
55561df8552aSStephen M. Cameron 
55571df8552aSStephen M. Cameron 	/* find cfgtable in order to check if reset via doorbell is supported */
55581df8552aSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
55591df8552aSStephen M. Cameron 					&cfg_base_addr_index, &cfg_offset);
55601df8552aSStephen M. Cameron 	if (rc)
55611df8552aSStephen M. Cameron 		goto unmap_vaddr;
55621df8552aSStephen M. Cameron 	cfgtable = remap_pci_mem(pci_resource_start(pdev,
55631df8552aSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
55641df8552aSStephen M. Cameron 	if (!cfgtable) {
55651df8552aSStephen M. Cameron 		rc = -ENOMEM;
55661df8552aSStephen M. Cameron 		goto unmap_vaddr;
55671df8552aSStephen M. Cameron 	}
5568580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(cfgtable);
5569580ada3cSStephen M. Cameron 	if (rc)
5570580ada3cSStephen M. Cameron 		goto unmap_vaddr;
55711df8552aSStephen M. Cameron 
5572cf0b08d0SStephen M. Cameron 	/* If reset via doorbell register is supported, use that.
5573cf0b08d0SStephen M. Cameron 	 * There are two such methods.  Favor the newest method.
5574cf0b08d0SStephen M. Cameron 	 */
55751df8552aSStephen M. Cameron 	misc_fw_support = readl(&cfgtable->misc_fw_support);
5576cf0b08d0SStephen M. Cameron 	use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
5577cf0b08d0SStephen M. Cameron 	if (use_doorbell) {
5578cf0b08d0SStephen M. Cameron 		use_doorbell = DOORBELL_CTLR_RESET2;
5579cf0b08d0SStephen M. Cameron 	} else {
55801df8552aSStephen M. Cameron 		use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
5581cf0b08d0SStephen M. Cameron 		if (use_doorbell) {
5582fba63097SMike Miller 			dev_warn(&pdev->dev, "Soft reset not supported. "
5583fba63097SMike Miller 				"Firmware update is required.\n");
558464670ac8SStephen M. Cameron 			rc = -ENOTSUPP; /* try soft reset */
5585cf0b08d0SStephen M. Cameron 			goto unmap_cfgtable;
5586cf0b08d0SStephen M. Cameron 		}
5587cf0b08d0SStephen M. Cameron 	}
55881df8552aSStephen M. Cameron 
55891df8552aSStephen M. Cameron 	rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
55901df8552aSStephen M. Cameron 	if (rc)
55911df8552aSStephen M. Cameron 		goto unmap_cfgtable;
5592edd16368SStephen M. Cameron 
5593270d05deSStephen M. Cameron 	pci_restore_state(pdev);
5594270d05deSStephen M. Cameron 	rc = pci_enable_device(pdev);
5595270d05deSStephen M. Cameron 	if (rc) {
5596270d05deSStephen M. Cameron 		dev_warn(&pdev->dev, "failed to enable device.\n");
5597270d05deSStephen M. Cameron 		goto unmap_cfgtable;
5598edd16368SStephen M. Cameron 	}
5599270d05deSStephen M. Cameron 	pci_write_config_word(pdev, 4, command_register);
5600edd16368SStephen M. Cameron 
56011df8552aSStephen M. Cameron 	/* Some devices (notably the HP Smart Array 5i Controller)
56021df8552aSStephen M. Cameron 	   need a little pause here */
56031df8552aSStephen M. Cameron 	msleep(HPSA_POST_RESET_PAUSE_MSECS);
56041df8552aSStephen M. Cameron 
5605fe5389c8SStephen M. Cameron 	rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
5606fe5389c8SStephen M. Cameron 	if (rc) {
5607fe5389c8SStephen M. Cameron 		dev_warn(&pdev->dev,
560864670ac8SStephen M. Cameron 			"failed waiting for board to become ready "
560964670ac8SStephen M. Cameron 			"after hard reset\n");
5610fe5389c8SStephen M. Cameron 		goto unmap_cfgtable;
5611fe5389c8SStephen M. Cameron 	}
5612fe5389c8SStephen M. Cameron 
5613580ada3cSStephen M. Cameron 	rc = controller_reset_failed(vaddr);
5614580ada3cSStephen M. Cameron 	if (rc < 0)
5615580ada3cSStephen M. Cameron 		goto unmap_cfgtable;
5616580ada3cSStephen M. Cameron 	if (rc) {
561764670ac8SStephen M. Cameron 		dev_warn(&pdev->dev, "Unable to successfully reset "
561864670ac8SStephen M. Cameron 			"controller. Will try soft reset.\n");
561964670ac8SStephen M. Cameron 		rc = -ENOTSUPP;
5620580ada3cSStephen M. Cameron 	} else {
562164670ac8SStephen M. Cameron 		dev_info(&pdev->dev, "board ready after hard reset.\n");
56221df8552aSStephen M. Cameron 	}
56231df8552aSStephen M. Cameron 
56241df8552aSStephen M. Cameron unmap_cfgtable:
56251df8552aSStephen M. Cameron 	iounmap(cfgtable);
56261df8552aSStephen M. Cameron 
56271df8552aSStephen M. Cameron unmap_vaddr:
56281df8552aSStephen M. Cameron 	iounmap(vaddr);
56291df8552aSStephen M. Cameron 	return rc;
5630edd16368SStephen M. Cameron }
5631edd16368SStephen M. Cameron 
5632edd16368SStephen M. Cameron /*
5633edd16368SStephen M. Cameron  *  We cannot read the structure directly, for portability we must use
5634edd16368SStephen M. Cameron  *   the io functions.
5635edd16368SStephen M. Cameron  *   This is for debug only.
5636edd16368SStephen M. Cameron  */
5637edd16368SStephen M. Cameron static void print_cfg_table(struct device *dev, struct CfgTable *tb)
5638edd16368SStephen M. Cameron {
563958f8665cSStephen M. Cameron #ifdef HPSA_DEBUG
5640edd16368SStephen M. Cameron 	int i;
5641edd16368SStephen M. Cameron 	char temp_name[17];
5642edd16368SStephen M. Cameron 
5643edd16368SStephen M. Cameron 	dev_info(dev, "Controller Configuration information\n");
5644edd16368SStephen M. Cameron 	dev_info(dev, "------------------------------------\n");
5645edd16368SStephen M. Cameron 	for (i = 0; i < 4; i++)
5646edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->Signature[i]));
5647edd16368SStephen M. Cameron 	temp_name[4] = '\0';
5648edd16368SStephen M. Cameron 	dev_info(dev, "   Signature = %s\n", temp_name);
5649edd16368SStephen M. Cameron 	dev_info(dev, "   Spec Number = %d\n", readl(&(tb->SpecValence)));
5650edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods supported = 0x%x\n",
5651edd16368SStephen M. Cameron 	       readl(&(tb->TransportSupport)));
5652edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods active = 0x%x\n",
5653edd16368SStephen M. Cameron 	       readl(&(tb->TransportActive)));
5654edd16368SStephen M. Cameron 	dev_info(dev, "   Requested transport Method = 0x%x\n",
5655edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.TransportRequest)));
5656edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Delay = 0x%x\n",
5657edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntDelay)));
5658edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Count = 0x%x\n",
5659edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntCount)));
5660edd16368SStephen M. Cameron 	dev_info(dev, "   Max outstanding commands = 0x%d\n",
5661edd16368SStephen M. Cameron 	       readl(&(tb->CmdsOutMax)));
5662edd16368SStephen M. Cameron 	dev_info(dev, "   Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
5663edd16368SStephen M. Cameron 	for (i = 0; i < 16; i++)
5664edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->ServerName[i]));
5665edd16368SStephen M. Cameron 	temp_name[16] = '\0';
5666edd16368SStephen M. Cameron 	dev_info(dev, "   Server Name = %s\n", temp_name);
5667edd16368SStephen M. Cameron 	dev_info(dev, "   Heartbeat Counter = 0x%x\n\n\n",
5668edd16368SStephen M. Cameron 		readl(&(tb->HeartBeat)));
5669edd16368SStephen M. Cameron #endif				/* HPSA_DEBUG */
567058f8665cSStephen M. Cameron }
5671edd16368SStephen M. Cameron 
5672edd16368SStephen M. Cameron static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
5673edd16368SStephen M. Cameron {
5674edd16368SStephen M. Cameron 	int i, offset, mem_type, bar_type;
5675edd16368SStephen M. Cameron 
5676edd16368SStephen M. Cameron 	if (pci_bar_addr == PCI_BASE_ADDRESS_0)	/* looking for BAR zero? */
5677edd16368SStephen M. Cameron 		return 0;
5678edd16368SStephen M. Cameron 	offset = 0;
5679edd16368SStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
5680edd16368SStephen M. Cameron 		bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
5681edd16368SStephen M. Cameron 		if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
5682edd16368SStephen M. Cameron 			offset += 4;
5683edd16368SStephen M. Cameron 		else {
5684edd16368SStephen M. Cameron 			mem_type = pci_resource_flags(pdev, i) &
5685edd16368SStephen M. Cameron 			    PCI_BASE_ADDRESS_MEM_TYPE_MASK;
5686edd16368SStephen M. Cameron 			switch (mem_type) {
5687edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_32:
5688edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_1M:
5689edd16368SStephen M. Cameron 				offset += 4;	/* 32 bit */
5690edd16368SStephen M. Cameron 				break;
5691edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_64:
5692edd16368SStephen M. Cameron 				offset += 8;
5693edd16368SStephen M. Cameron 				break;
5694edd16368SStephen M. Cameron 			default:	/* reserved in PCI 2.2 */
5695edd16368SStephen M. Cameron 				dev_warn(&pdev->dev,
5696edd16368SStephen M. Cameron 				       "base address is invalid\n");
5697edd16368SStephen M. Cameron 				return -1;
5698edd16368SStephen M. Cameron 				break;
5699edd16368SStephen M. Cameron 			}
5700edd16368SStephen M. Cameron 		}
5701edd16368SStephen M. Cameron 		if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
5702edd16368SStephen M. Cameron 			return i + 1;
5703edd16368SStephen M. Cameron 	}
5704edd16368SStephen M. Cameron 	return -1;
5705edd16368SStephen M. Cameron }
5706edd16368SStephen M. Cameron 
5707edd16368SStephen M. Cameron /* If MSI/MSI-X is supported by the kernel we will try to enable it on
5708edd16368SStephen M. Cameron  * controllers that are capable. If not, we use IO-APIC mode.
5709edd16368SStephen M. Cameron  */
5710edd16368SStephen M. Cameron 
57116f039790SGreg Kroah-Hartman static void hpsa_interrupt_mode(struct ctlr_info *h)
5712edd16368SStephen M. Cameron {
5713edd16368SStephen M. Cameron #ifdef CONFIG_PCI_MSI
5714254f796bSMatt Gates 	int err, i;
5715254f796bSMatt Gates 	struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
5716254f796bSMatt Gates 
5717254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++) {
5718254f796bSMatt Gates 		hpsa_msix_entries[i].vector = 0;
5719254f796bSMatt Gates 		hpsa_msix_entries[i].entry = i;
5720254f796bSMatt Gates 	}
5721edd16368SStephen M. Cameron 
5722edd16368SStephen M. Cameron 	/* Some boards advertise MSI but don't really support it */
57236b3f4c52SStephen M. Cameron 	if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
57246b3f4c52SStephen M. Cameron 	    (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
5725edd16368SStephen M. Cameron 		goto default_int_mode;
572655c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
572755c06c71SStephen M. Cameron 		dev_info(&h->pdev->dev, "MSIX\n");
5728eee0f03aSHannes Reinecke 		h->msix_vector = MAX_REPLY_QUEUES;
5729254f796bSMatt Gates 		err = pci_enable_msix(h->pdev, hpsa_msix_entries,
5730eee0f03aSHannes Reinecke 				      h->msix_vector);
5731edd16368SStephen M. Cameron 		if (err > 0) {
573255c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
5733edd16368SStephen M. Cameron 			       "available\n", err);
5734eee0f03aSHannes Reinecke 			h->msix_vector = err;
5735eee0f03aSHannes Reinecke 			err = pci_enable_msix(h->pdev, hpsa_msix_entries,
5736eee0f03aSHannes Reinecke 					      h->msix_vector);
5737eee0f03aSHannes Reinecke 		}
5738eee0f03aSHannes Reinecke 		if (!err) {
5739eee0f03aSHannes Reinecke 			for (i = 0; i < h->msix_vector; i++)
5740eee0f03aSHannes Reinecke 				h->intr[i] = hpsa_msix_entries[i].vector;
5741eee0f03aSHannes Reinecke 			return;
5742edd16368SStephen M. Cameron 		} else {
574355c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "MSI-X init failed %d\n",
5744edd16368SStephen M. Cameron 			       err);
5745eee0f03aSHannes Reinecke 			h->msix_vector = 0;
5746edd16368SStephen M. Cameron 			goto default_int_mode;
5747edd16368SStephen M. Cameron 		}
5748edd16368SStephen M. Cameron 	}
574955c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
575055c06c71SStephen M. Cameron 		dev_info(&h->pdev->dev, "MSI\n");
575155c06c71SStephen M. Cameron 		if (!pci_enable_msi(h->pdev))
5752edd16368SStephen M. Cameron 			h->msi_vector = 1;
5753edd16368SStephen M. Cameron 		else
575455c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "MSI init failed\n");
5755edd16368SStephen M. Cameron 	}
5756edd16368SStephen M. Cameron default_int_mode:
5757edd16368SStephen M. Cameron #endif				/* CONFIG_PCI_MSI */
5758edd16368SStephen M. Cameron 	/* if we get here we're going to use the default interrupt mode */
5759a9a3a273SStephen M. Cameron 	h->intr[h->intr_mode] = h->pdev->irq;
5760edd16368SStephen M. Cameron }
5761edd16368SStephen M. Cameron 
57626f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
5763e5c880d1SStephen M. Cameron {
5764e5c880d1SStephen M. Cameron 	int i;
5765e5c880d1SStephen M. Cameron 	u32 subsystem_vendor_id, subsystem_device_id;
5766e5c880d1SStephen M. Cameron 
5767e5c880d1SStephen M. Cameron 	subsystem_vendor_id = pdev->subsystem_vendor;
5768e5c880d1SStephen M. Cameron 	subsystem_device_id = pdev->subsystem_device;
5769e5c880d1SStephen M. Cameron 	*board_id = ((subsystem_device_id << 16) & 0xffff0000) |
5770e5c880d1SStephen M. Cameron 		    subsystem_vendor_id;
5771e5c880d1SStephen M. Cameron 
5772e5c880d1SStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(products); i++)
5773e5c880d1SStephen M. Cameron 		if (*board_id == products[i].board_id)
5774e5c880d1SStephen M. Cameron 			return i;
5775e5c880d1SStephen M. Cameron 
57766798cc0aSStephen M. Cameron 	if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
57776798cc0aSStephen M. Cameron 		subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
57786798cc0aSStephen M. Cameron 		!hpsa_allow_any) {
5779e5c880d1SStephen M. Cameron 		dev_warn(&pdev->dev, "unrecognized board ID: "
5780e5c880d1SStephen M. Cameron 			"0x%08x, ignoring.\n", *board_id);
5781e5c880d1SStephen M. Cameron 			return -ENODEV;
5782e5c880d1SStephen M. Cameron 	}
5783e5c880d1SStephen M. Cameron 	return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
5784e5c880d1SStephen M. Cameron }
5785e5c880d1SStephen M. Cameron 
57866f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
57873a7774ceSStephen M. Cameron 				    unsigned long *memory_bar)
57883a7774ceSStephen M. Cameron {
57893a7774ceSStephen M. Cameron 	int i;
57903a7774ceSStephen M. Cameron 
57913a7774ceSStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
579212d2cd47SStephen M. Cameron 		if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
57933a7774ceSStephen M. Cameron 			/* addressing mode bits already removed */
579412d2cd47SStephen M. Cameron 			*memory_bar = pci_resource_start(pdev, i);
579512d2cd47SStephen M. Cameron 			dev_dbg(&pdev->dev, "memory BAR = %lx\n",
57963a7774ceSStephen M. Cameron 				*memory_bar);
57973a7774ceSStephen M. Cameron 			return 0;
57983a7774ceSStephen M. Cameron 		}
579912d2cd47SStephen M. Cameron 	dev_warn(&pdev->dev, "no memory BAR found\n");
58003a7774ceSStephen M. Cameron 	return -ENODEV;
58013a7774ceSStephen M. Cameron }
58023a7774ceSStephen M. Cameron 
58036f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
58046f039790SGreg Kroah-Hartman 				     int wait_for_ready)
58052c4c8c8bSStephen M. Cameron {
5806fe5389c8SStephen M. Cameron 	int i, iterations;
58072c4c8c8bSStephen M. Cameron 	u32 scratchpad;
5808fe5389c8SStephen M. Cameron 	if (wait_for_ready)
5809fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_READY_ITERATIONS;
5810fe5389c8SStephen M. Cameron 	else
5811fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
58122c4c8c8bSStephen M. Cameron 
5813fe5389c8SStephen M. Cameron 	for (i = 0; i < iterations; i++) {
5814fe5389c8SStephen M. Cameron 		scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
5815fe5389c8SStephen M. Cameron 		if (wait_for_ready) {
58162c4c8c8bSStephen M. Cameron 			if (scratchpad == HPSA_FIRMWARE_READY)
58172c4c8c8bSStephen M. Cameron 				return 0;
5818fe5389c8SStephen M. Cameron 		} else {
5819fe5389c8SStephen M. Cameron 			if (scratchpad != HPSA_FIRMWARE_READY)
5820fe5389c8SStephen M. Cameron 				return 0;
5821fe5389c8SStephen M. Cameron 		}
58222c4c8c8bSStephen M. Cameron 		msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
58232c4c8c8bSStephen M. Cameron 	}
5824fe5389c8SStephen M. Cameron 	dev_warn(&pdev->dev, "board not ready, timed out.\n");
58252c4c8c8bSStephen M. Cameron 	return -ENODEV;
58262c4c8c8bSStephen M. Cameron }
58272c4c8c8bSStephen M. Cameron 
58286f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
58296f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
5830a51fd47fSStephen M. Cameron 			       u64 *cfg_offset)
5831a51fd47fSStephen M. Cameron {
5832a51fd47fSStephen M. Cameron 	*cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
5833a51fd47fSStephen M. Cameron 	*cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
5834a51fd47fSStephen M. Cameron 	*cfg_base_addr &= (u32) 0x0000ffff;
5835a51fd47fSStephen M. Cameron 	*cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
5836a51fd47fSStephen M. Cameron 	if (*cfg_base_addr_index == -1) {
5837a51fd47fSStephen M. Cameron 		dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
5838a51fd47fSStephen M. Cameron 		return -ENODEV;
5839a51fd47fSStephen M. Cameron 	}
5840a51fd47fSStephen M. Cameron 	return 0;
5841a51fd47fSStephen M. Cameron }
5842a51fd47fSStephen M. Cameron 
58436f039790SGreg Kroah-Hartman static int hpsa_find_cfgtables(struct ctlr_info *h)
5844edd16368SStephen M. Cameron {
584501a02ffcSStephen M. Cameron 	u64 cfg_offset;
584601a02ffcSStephen M. Cameron 	u32 cfg_base_addr;
584701a02ffcSStephen M. Cameron 	u64 cfg_base_addr_index;
5848303932fdSDon Brace 	u32 trans_offset;
5849a51fd47fSStephen M. Cameron 	int rc;
585077c4495cSStephen M. Cameron 
5851a51fd47fSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
5852a51fd47fSStephen M. Cameron 		&cfg_base_addr_index, &cfg_offset);
5853a51fd47fSStephen M. Cameron 	if (rc)
5854a51fd47fSStephen M. Cameron 		return rc;
585577c4495cSStephen M. Cameron 	h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
5856a51fd47fSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
585777c4495cSStephen M. Cameron 	if (!h->cfgtable)
585877c4495cSStephen M. Cameron 		return -ENOMEM;
5859580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(h->cfgtable);
5860580ada3cSStephen M. Cameron 	if (rc)
5861580ada3cSStephen M. Cameron 		return rc;
586277c4495cSStephen M. Cameron 	/* Find performant mode table. */
5863a51fd47fSStephen M. Cameron 	trans_offset = readl(&h->cfgtable->TransMethodOffset);
586477c4495cSStephen M. Cameron 	h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
586577c4495cSStephen M. Cameron 				cfg_base_addr_index)+cfg_offset+trans_offset,
586677c4495cSStephen M. Cameron 				sizeof(*h->transtable));
586777c4495cSStephen M. Cameron 	if (!h->transtable)
586877c4495cSStephen M. Cameron 		return -ENOMEM;
586977c4495cSStephen M. Cameron 	return 0;
587077c4495cSStephen M. Cameron }
587177c4495cSStephen M. Cameron 
58726f039790SGreg Kroah-Hartman static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
5873cba3d38bSStephen M. Cameron {
5874cba3d38bSStephen M. Cameron 	h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
587572ceeaecSStephen M. Cameron 
587672ceeaecSStephen M. Cameron 	/* Limit commands in memory limited kdump scenario. */
587772ceeaecSStephen M. Cameron 	if (reset_devices && h->max_commands > 32)
587872ceeaecSStephen M. Cameron 		h->max_commands = 32;
587972ceeaecSStephen M. Cameron 
5880cba3d38bSStephen M. Cameron 	if (h->max_commands < 16) {
5881cba3d38bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "Controller reports "
5882cba3d38bSStephen M. Cameron 			"max supported commands of %d, an obvious lie. "
5883cba3d38bSStephen M. Cameron 			"Using 16.  Ensure that firmware is up to date.\n",
5884cba3d38bSStephen M. Cameron 			h->max_commands);
5885cba3d38bSStephen M. Cameron 		h->max_commands = 16;
5886cba3d38bSStephen M. Cameron 	}
5887cba3d38bSStephen M. Cameron }
5888cba3d38bSStephen M. Cameron 
5889b93d7536SStephen M. Cameron /* Interrogate the hardware for some limits:
5890b93d7536SStephen M. Cameron  * max commands, max SG elements without chaining, and with chaining,
5891b93d7536SStephen M. Cameron  * SG chain block size, etc.
5892b93d7536SStephen M. Cameron  */
58936f039790SGreg Kroah-Hartman static void hpsa_find_board_params(struct ctlr_info *h)
5894b93d7536SStephen M. Cameron {
5895cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
5896b93d7536SStephen M. Cameron 	h->nr_cmds = h->max_commands - 4; /* Allow room for some ioctls */
5897b93d7536SStephen M. Cameron 	h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
5898283b4a9bSStephen M. Cameron 	h->fw_support = readl(&(h->cfgtable->misc_fw_support));
5899b93d7536SStephen M. Cameron 	/*
5900b93d7536SStephen M. Cameron 	 * Limit in-command s/g elements to 32 save dma'able memory.
5901b93d7536SStephen M. Cameron 	 * Howvever spec says if 0, use 31
5902b93d7536SStephen M. Cameron 	 */
5903b93d7536SStephen M. Cameron 	h->max_cmd_sg_entries = 31;
5904b93d7536SStephen M. Cameron 	if (h->maxsgentries > 512) {
5905b93d7536SStephen M. Cameron 		h->max_cmd_sg_entries = 32;
5906b93d7536SStephen M. Cameron 		h->chainsize = h->maxsgentries - h->max_cmd_sg_entries + 1;
5907b93d7536SStephen M. Cameron 		h->maxsgentries--; /* save one for chain pointer */
5908b93d7536SStephen M. Cameron 	} else {
5909b93d7536SStephen M. Cameron 		h->maxsgentries = 31; /* default to traditional values */
5910b93d7536SStephen M. Cameron 		h->chainsize = 0;
5911b93d7536SStephen M. Cameron 	}
591275167d2cSStephen M. Cameron 
591375167d2cSStephen M. Cameron 	/* Find out what task management functions are supported and cache */
591475167d2cSStephen M. Cameron 	h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
59150e7a7fceSScott Teel 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
59160e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
59170e7a7fceSScott Teel 	if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
59180e7a7fceSScott Teel 		dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
5919b93d7536SStephen M. Cameron }
5920b93d7536SStephen M. Cameron 
592176c46e49SStephen M. Cameron static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
592276c46e49SStephen M. Cameron {
59230fc9fd40SAkinobu Mita 	if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
592476c46e49SStephen M. Cameron 		dev_warn(&h->pdev->dev, "not a valid CISS config table\n");
592576c46e49SStephen M. Cameron 		return false;
592676c46e49SStephen M. Cameron 	}
592776c46e49SStephen M. Cameron 	return true;
592876c46e49SStephen M. Cameron }
592976c46e49SStephen M. Cameron 
593097a5e98cSStephen M. Cameron static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
5931f7c39101SStephen M. Cameron {
593297a5e98cSStephen M. Cameron 	u32 driver_support;
5933f7c39101SStephen M. Cameron 
593428e13446SStephen M. Cameron #ifdef CONFIG_X86
593528e13446SStephen M. Cameron 	/* Need to enable prefetch in the SCSI core for 6400 in x86 */
593697a5e98cSStephen M. Cameron 	driver_support = readl(&(h->cfgtable->driver_support));
593797a5e98cSStephen M. Cameron 	driver_support |= ENABLE_SCSI_PREFETCH;
5938f7c39101SStephen M. Cameron #endif
593928e13446SStephen M. Cameron 	driver_support |= ENABLE_UNIT_ATTN;
594028e13446SStephen M. Cameron 	writel(driver_support, &(h->cfgtable->driver_support));
5941f7c39101SStephen M. Cameron }
5942f7c39101SStephen M. Cameron 
59433d0eab67SStephen M. Cameron /* Disable DMA prefetch for the P600.  Otherwise an ASIC bug may result
59443d0eab67SStephen M. Cameron  * in a prefetch beyond physical memory.
59453d0eab67SStephen M. Cameron  */
59463d0eab67SStephen M. Cameron static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
59473d0eab67SStephen M. Cameron {
59483d0eab67SStephen M. Cameron 	u32 dma_prefetch;
59493d0eab67SStephen M. Cameron 
59503d0eab67SStephen M. Cameron 	if (h->board_id != 0x3225103C)
59513d0eab67SStephen M. Cameron 		return;
59523d0eab67SStephen M. Cameron 	dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
59533d0eab67SStephen M. Cameron 	dma_prefetch |= 0x8000;
59543d0eab67SStephen M. Cameron 	writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
59553d0eab67SStephen M. Cameron }
59563d0eab67SStephen M. Cameron 
595776438d08SStephen M. Cameron static void hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
595876438d08SStephen M. Cameron {
595976438d08SStephen M. Cameron 	int i;
596076438d08SStephen M. Cameron 	u32 doorbell_value;
596176438d08SStephen M. Cameron 	unsigned long flags;
596276438d08SStephen M. Cameron 	/* wait until the clear_event_notify bit 6 is cleared by controller. */
596376438d08SStephen M. Cameron 	for (i = 0; i < MAX_CONFIG_WAIT; i++) {
596476438d08SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
596576438d08SStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
596676438d08SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
596776438d08SStephen M. Cameron 		if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
596876438d08SStephen M. Cameron 			break;
596976438d08SStephen M. Cameron 		/* delay and try again */
597076438d08SStephen M. Cameron 		msleep(20);
597176438d08SStephen M. Cameron 	}
597276438d08SStephen M. Cameron }
597376438d08SStephen M. Cameron 
59746f039790SGreg Kroah-Hartman static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
5975eb6b2ae9SStephen M. Cameron {
5976eb6b2ae9SStephen M. Cameron 	int i;
59776eaf46fdSStephen M. Cameron 	u32 doorbell_value;
59786eaf46fdSStephen M. Cameron 	unsigned long flags;
5979eb6b2ae9SStephen M. Cameron 
5980eb6b2ae9SStephen M. Cameron 	/* under certain very rare conditions, this can take awhile.
5981eb6b2ae9SStephen M. Cameron 	 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
5982eb6b2ae9SStephen M. Cameron 	 * as we enter this code.)
5983eb6b2ae9SStephen M. Cameron 	 */
5984eb6b2ae9SStephen M. Cameron 	for (i = 0; i < MAX_CONFIG_WAIT; i++) {
59856eaf46fdSStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
59866eaf46fdSStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
59876eaf46fdSStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
5988382be668SDan Carpenter 		if (!(doorbell_value & CFGTBL_ChangeReq))
5989eb6b2ae9SStephen M. Cameron 			break;
5990eb6b2ae9SStephen M. Cameron 		/* delay and try again */
599160d3f5b0SStephen M. Cameron 		usleep_range(10000, 20000);
5992eb6b2ae9SStephen M. Cameron 	}
59933f4336f3SStephen M. Cameron }
59943f4336f3SStephen M. Cameron 
59956f039790SGreg Kroah-Hartman static int hpsa_enter_simple_mode(struct ctlr_info *h)
59963f4336f3SStephen M. Cameron {
59973f4336f3SStephen M. Cameron 	u32 trans_support;
59983f4336f3SStephen M. Cameron 
59993f4336f3SStephen M. Cameron 	trans_support = readl(&(h->cfgtable->TransportSupport));
60003f4336f3SStephen M. Cameron 	if (!(trans_support & SIMPLE_MODE))
60013f4336f3SStephen M. Cameron 		return -ENOTSUPP;
60023f4336f3SStephen M. Cameron 
60033f4336f3SStephen M. Cameron 	h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
6004283b4a9bSStephen M. Cameron 
60053f4336f3SStephen M. Cameron 	/* Update the field, and then ring the doorbell */
60063f4336f3SStephen M. Cameron 	writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
6007b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
60083f4336f3SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
60093f4336f3SStephen M. Cameron 	hpsa_wait_for_mode_change_ack(h);
6010eb6b2ae9SStephen M. Cameron 	print_cfg_table(&h->pdev->dev, h->cfgtable);
6011283b4a9bSStephen M. Cameron 	if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
6012283b4a9bSStephen M. Cameron 		goto error;
6013960a30e7SStephen M. Cameron 	h->transMethod = CFGTBL_Trans_Simple;
6014eb6b2ae9SStephen M. Cameron 	return 0;
6015283b4a9bSStephen M. Cameron error:
6016283b4a9bSStephen M. Cameron 	dev_warn(&h->pdev->dev, "unable to get board into simple mode\n");
6017283b4a9bSStephen M. Cameron 	return -ENODEV;
6018eb6b2ae9SStephen M. Cameron }
6019eb6b2ae9SStephen M. Cameron 
60206f039790SGreg Kroah-Hartman static int hpsa_pci_init(struct ctlr_info *h)
602177c4495cSStephen M. Cameron {
6022eb6b2ae9SStephen M. Cameron 	int prod_index, err;
6023edd16368SStephen M. Cameron 
6024e5c880d1SStephen M. Cameron 	prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
6025e5c880d1SStephen M. Cameron 	if (prod_index < 0)
6026edd16368SStephen M. Cameron 		return -ENODEV;
6027e5c880d1SStephen M. Cameron 	h->product_name = products[prod_index].product_name;
6028e5c880d1SStephen M. Cameron 	h->access = *(products[prod_index].access);
6029e5c880d1SStephen M. Cameron 
6030e5a44df8SMatthew Garrett 	pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
6031e5a44df8SMatthew Garrett 			       PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
6032e5a44df8SMatthew Garrett 
603355c06c71SStephen M. Cameron 	err = pci_enable_device(h->pdev);
6034edd16368SStephen M. Cameron 	if (err) {
603555c06c71SStephen M. Cameron 		dev_warn(&h->pdev->dev, "unable to enable PCI device\n");
6036edd16368SStephen M. Cameron 		return err;
6037edd16368SStephen M. Cameron 	}
6038edd16368SStephen M. Cameron 
60395cb460a6SStephen M. Cameron 	/* Enable bus mastering (pci_disable_device may disable this) */
60405cb460a6SStephen M. Cameron 	pci_set_master(h->pdev);
60415cb460a6SStephen M. Cameron 
6042f79cfec6SStephen M. Cameron 	err = pci_request_regions(h->pdev, HPSA);
6043edd16368SStephen M. Cameron 	if (err) {
604455c06c71SStephen M. Cameron 		dev_err(&h->pdev->dev,
604555c06c71SStephen M. Cameron 			"cannot obtain PCI resources, aborting\n");
6046edd16368SStephen M. Cameron 		return err;
6047edd16368SStephen M. Cameron 	}
60486b3f4c52SStephen M. Cameron 	hpsa_interrupt_mode(h);
604912d2cd47SStephen M. Cameron 	err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
60503a7774ceSStephen M. Cameron 	if (err)
6051edd16368SStephen M. Cameron 		goto err_out_free_res;
6052edd16368SStephen M. Cameron 	h->vaddr = remap_pci_mem(h->paddr, 0x250);
6053204892e9SStephen M. Cameron 	if (!h->vaddr) {
6054204892e9SStephen M. Cameron 		err = -ENOMEM;
6055204892e9SStephen M. Cameron 		goto err_out_free_res;
6056204892e9SStephen M. Cameron 	}
6057fe5389c8SStephen M. Cameron 	err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
60582c4c8c8bSStephen M. Cameron 	if (err)
6059edd16368SStephen M. Cameron 		goto err_out_free_res;
606077c4495cSStephen M. Cameron 	err = hpsa_find_cfgtables(h);
606177c4495cSStephen M. Cameron 	if (err)
6062edd16368SStephen M. Cameron 		goto err_out_free_res;
6063b93d7536SStephen M. Cameron 	hpsa_find_board_params(h);
6064edd16368SStephen M. Cameron 
606576c46e49SStephen M. Cameron 	if (!hpsa_CISS_signature_present(h)) {
6066edd16368SStephen M. Cameron 		err = -ENODEV;
6067edd16368SStephen M. Cameron 		goto err_out_free_res;
6068edd16368SStephen M. Cameron 	}
606997a5e98cSStephen M. Cameron 	hpsa_set_driver_support_bits(h);
60703d0eab67SStephen M. Cameron 	hpsa_p600_dma_prefetch_quirk(h);
6071eb6b2ae9SStephen M. Cameron 	err = hpsa_enter_simple_mode(h);
6072eb6b2ae9SStephen M. Cameron 	if (err)
6073edd16368SStephen M. Cameron 		goto err_out_free_res;
6074edd16368SStephen M. Cameron 	return 0;
6075edd16368SStephen M. Cameron 
6076edd16368SStephen M. Cameron err_out_free_res:
6077204892e9SStephen M. Cameron 	if (h->transtable)
6078204892e9SStephen M. Cameron 		iounmap(h->transtable);
6079204892e9SStephen M. Cameron 	if (h->cfgtable)
6080204892e9SStephen M. Cameron 		iounmap(h->cfgtable);
6081204892e9SStephen M. Cameron 	if (h->vaddr)
6082204892e9SStephen M. Cameron 		iounmap(h->vaddr);
6083f0bd0b68SStephen M. Cameron 	pci_disable_device(h->pdev);
608455c06c71SStephen M. Cameron 	pci_release_regions(h->pdev);
6085edd16368SStephen M. Cameron 	return err;
6086edd16368SStephen M. Cameron }
6087edd16368SStephen M. Cameron 
60886f039790SGreg Kroah-Hartman static void hpsa_hba_inquiry(struct ctlr_info *h)
6089339b2b14SStephen M. Cameron {
6090339b2b14SStephen M. Cameron 	int rc;
6091339b2b14SStephen M. Cameron 
6092339b2b14SStephen M. Cameron #define HBA_INQUIRY_BYTE_COUNT 64
6093339b2b14SStephen M. Cameron 	h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
6094339b2b14SStephen M. Cameron 	if (!h->hba_inquiry_data)
6095339b2b14SStephen M. Cameron 		return;
6096339b2b14SStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
6097339b2b14SStephen M. Cameron 		h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
6098339b2b14SStephen M. Cameron 	if (rc != 0) {
6099339b2b14SStephen M. Cameron 		kfree(h->hba_inquiry_data);
6100339b2b14SStephen M. Cameron 		h->hba_inquiry_data = NULL;
6101339b2b14SStephen M. Cameron 	}
6102339b2b14SStephen M. Cameron }
6103339b2b14SStephen M. Cameron 
61046f039790SGreg Kroah-Hartman static int hpsa_init_reset_devices(struct pci_dev *pdev)
6105edd16368SStephen M. Cameron {
61061df8552aSStephen M. Cameron 	int rc, i;
6107edd16368SStephen M. Cameron 
61084c2a8c40SStephen M. Cameron 	if (!reset_devices)
61094c2a8c40SStephen M. Cameron 		return 0;
61104c2a8c40SStephen M. Cameron 
61111df8552aSStephen M. Cameron 	/* Reset the controller with a PCI power-cycle or via doorbell */
61121df8552aSStephen M. Cameron 	rc = hpsa_kdump_hard_reset_controller(pdev);
6113edd16368SStephen M. Cameron 
61141df8552aSStephen M. Cameron 	/* -ENOTSUPP here means we cannot reset the controller
61151df8552aSStephen M. Cameron 	 * but it's already (and still) up and running in
611618867659SStephen M. Cameron 	 * "performant mode".  Or, it might be 640x, which can't reset
611718867659SStephen M. Cameron 	 * due to concerns about shared bbwc between 6402/6404 pair.
61181df8552aSStephen M. Cameron 	 */
61191df8552aSStephen M. Cameron 	if (rc == -ENOTSUPP)
612064670ac8SStephen M. Cameron 		return rc; /* just try to do the kdump anyhow. */
61211df8552aSStephen M. Cameron 	if (rc)
61221df8552aSStephen M. Cameron 		return -ENODEV;
6123edd16368SStephen M. Cameron 
6124edd16368SStephen M. Cameron 	/* Now try to get the controller to respond to a no-op */
61252b870cb3SStephen M. Cameron 	dev_warn(&pdev->dev, "Waiting for controller to respond to no-op\n");
6126edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
6127edd16368SStephen M. Cameron 		if (hpsa_noop(pdev) == 0)
6128edd16368SStephen M. Cameron 			break;
6129edd16368SStephen M. Cameron 		else
6130edd16368SStephen M. Cameron 			dev_warn(&pdev->dev, "no-op failed%s\n",
6131edd16368SStephen M. Cameron 					(i < 11 ? "; re-trying" : ""));
6132edd16368SStephen M. Cameron 	}
61334c2a8c40SStephen M. Cameron 	return 0;
6134edd16368SStephen M. Cameron }
6135edd16368SStephen M. Cameron 
61366f039790SGreg Kroah-Hartman static int hpsa_allocate_cmd_pool(struct ctlr_info *h)
61372e9d1b36SStephen M. Cameron {
61382e9d1b36SStephen M. Cameron 	h->cmd_pool_bits = kzalloc(
61392e9d1b36SStephen M. Cameron 		DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
61402e9d1b36SStephen M. Cameron 		sizeof(unsigned long), GFP_KERNEL);
61412e9d1b36SStephen M. Cameron 	h->cmd_pool = pci_alloc_consistent(h->pdev,
61422e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->cmd_pool),
61432e9d1b36SStephen M. Cameron 		    &(h->cmd_pool_dhandle));
61442e9d1b36SStephen M. Cameron 	h->errinfo_pool = pci_alloc_consistent(h->pdev,
61452e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->errinfo_pool),
61462e9d1b36SStephen M. Cameron 		    &(h->errinfo_pool_dhandle));
61472e9d1b36SStephen M. Cameron 	if ((h->cmd_pool_bits == NULL)
61482e9d1b36SStephen M. Cameron 	    || (h->cmd_pool == NULL)
61492e9d1b36SStephen M. Cameron 	    || (h->errinfo_pool == NULL)) {
61502e9d1b36SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory in %s", __func__);
61512e9d1b36SStephen M. Cameron 		return -ENOMEM;
61522e9d1b36SStephen M. Cameron 	}
61532e9d1b36SStephen M. Cameron 	return 0;
61542e9d1b36SStephen M. Cameron }
61552e9d1b36SStephen M. Cameron 
61562e9d1b36SStephen M. Cameron static void hpsa_free_cmd_pool(struct ctlr_info *h)
61572e9d1b36SStephen M. Cameron {
61582e9d1b36SStephen M. Cameron 	kfree(h->cmd_pool_bits);
61592e9d1b36SStephen M. Cameron 	if (h->cmd_pool)
61602e9d1b36SStephen M. Cameron 		pci_free_consistent(h->pdev,
61612e9d1b36SStephen M. Cameron 			    h->nr_cmds * sizeof(struct CommandList),
61622e9d1b36SStephen M. Cameron 			    h->cmd_pool, h->cmd_pool_dhandle);
6163aca9012aSStephen M. Cameron 	if (h->ioaccel2_cmd_pool)
6164aca9012aSStephen M. Cameron 		pci_free_consistent(h->pdev,
6165aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
6166aca9012aSStephen M. Cameron 			h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
61672e9d1b36SStephen M. Cameron 	if (h->errinfo_pool)
61682e9d1b36SStephen M. Cameron 		pci_free_consistent(h->pdev,
61692e9d1b36SStephen M. Cameron 			    h->nr_cmds * sizeof(struct ErrorInfo),
61702e9d1b36SStephen M. Cameron 			    h->errinfo_pool,
61712e9d1b36SStephen M. Cameron 			    h->errinfo_pool_dhandle);
6172e1f7de0cSMatt Gates 	if (h->ioaccel_cmd_pool)
6173e1f7de0cSMatt Gates 		pci_free_consistent(h->pdev,
6174e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(struct io_accel1_cmd),
6175e1f7de0cSMatt Gates 			h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
61762e9d1b36SStephen M. Cameron }
61772e9d1b36SStephen M. Cameron 
61780ae01a32SStephen M. Cameron static int hpsa_request_irq(struct ctlr_info *h,
61790ae01a32SStephen M. Cameron 	irqreturn_t (*msixhandler)(int, void *),
61800ae01a32SStephen M. Cameron 	irqreturn_t (*intxhandler)(int, void *))
61810ae01a32SStephen M. Cameron {
6182254f796bSMatt Gates 	int rc, i;
61830ae01a32SStephen M. Cameron 
6184254f796bSMatt Gates 	/*
6185254f796bSMatt Gates 	 * initialize h->q[x] = x so that interrupt handlers know which
6186254f796bSMatt Gates 	 * queue to process.
6187254f796bSMatt Gates 	 */
6188254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++)
6189254f796bSMatt Gates 		h->q[i] = (u8) i;
6190254f796bSMatt Gates 
6191eee0f03aSHannes Reinecke 	if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
6192254f796bSMatt Gates 		/* If performant mode and MSI-X, use multiple reply queues */
6193eee0f03aSHannes Reinecke 		for (i = 0; i < h->msix_vector; i++)
6194254f796bSMatt Gates 			rc = request_irq(h->intr[i], msixhandler,
6195254f796bSMatt Gates 					0, h->devname,
6196254f796bSMatt Gates 					&h->q[i]);
6197254f796bSMatt Gates 	} else {
6198254f796bSMatt Gates 		/* Use single reply pool */
6199eee0f03aSHannes Reinecke 		if (h->msix_vector > 0 || h->msi_vector) {
6200254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
6201254f796bSMatt Gates 				msixhandler, 0, h->devname,
6202254f796bSMatt Gates 				&h->q[h->intr_mode]);
6203254f796bSMatt Gates 		} else {
6204254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
6205254f796bSMatt Gates 				intxhandler, IRQF_SHARED, h->devname,
6206254f796bSMatt Gates 				&h->q[h->intr_mode]);
6207254f796bSMatt Gates 		}
6208254f796bSMatt Gates 	}
62090ae01a32SStephen M. Cameron 	if (rc) {
62100ae01a32SStephen M. Cameron 		dev_err(&h->pdev->dev, "unable to get irq %d for %s\n",
62110ae01a32SStephen M. Cameron 		       h->intr[h->intr_mode], h->devname);
62120ae01a32SStephen M. Cameron 		return -ENODEV;
62130ae01a32SStephen M. Cameron 	}
62140ae01a32SStephen M. Cameron 	return 0;
62150ae01a32SStephen M. Cameron }
62160ae01a32SStephen M. Cameron 
62176f039790SGreg Kroah-Hartman static int hpsa_kdump_soft_reset(struct ctlr_info *h)
621864670ac8SStephen M. Cameron {
621964670ac8SStephen M. Cameron 	if (hpsa_send_host_reset(h, RAID_CTLR_LUNID,
622064670ac8SStephen M. Cameron 		HPSA_RESET_TYPE_CONTROLLER)) {
622164670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Resetting array controller failed.\n");
622264670ac8SStephen M. Cameron 		return -EIO;
622364670ac8SStephen M. Cameron 	}
622464670ac8SStephen M. Cameron 
622564670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
622664670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) {
622764670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
622864670ac8SStephen M. Cameron 		return -1;
622964670ac8SStephen M. Cameron 	}
623064670ac8SStephen M. Cameron 
623164670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
623264670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) {
623364670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Board failed to become ready "
623464670ac8SStephen M. Cameron 			"after soft reset.\n");
623564670ac8SStephen M. Cameron 		return -1;
623664670ac8SStephen M. Cameron 	}
623764670ac8SStephen M. Cameron 
623864670ac8SStephen M. Cameron 	return 0;
623964670ac8SStephen M. Cameron }
624064670ac8SStephen M. Cameron 
6241254f796bSMatt Gates static void free_irqs(struct ctlr_info *h)
6242254f796bSMatt Gates {
6243254f796bSMatt Gates 	int i;
6244254f796bSMatt Gates 
6245254f796bSMatt Gates 	if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
6246254f796bSMatt Gates 		/* Single reply queue, only one irq to free */
6247254f796bSMatt Gates 		i = h->intr_mode;
6248254f796bSMatt Gates 		free_irq(h->intr[i], &h->q[i]);
6249254f796bSMatt Gates 		return;
6250254f796bSMatt Gates 	}
6251254f796bSMatt Gates 
6252eee0f03aSHannes Reinecke 	for (i = 0; i < h->msix_vector; i++)
6253254f796bSMatt Gates 		free_irq(h->intr[i], &h->q[i]);
6254254f796bSMatt Gates }
6255254f796bSMatt Gates 
62560097f0f4SStephen M. Cameron static void hpsa_free_irqs_and_disable_msix(struct ctlr_info *h)
625764670ac8SStephen M. Cameron {
6258254f796bSMatt Gates 	free_irqs(h);
625964670ac8SStephen M. Cameron #ifdef CONFIG_PCI_MSI
62600097f0f4SStephen M. Cameron 	if (h->msix_vector) {
62610097f0f4SStephen M. Cameron 		if (h->pdev->msix_enabled)
626264670ac8SStephen M. Cameron 			pci_disable_msix(h->pdev);
62630097f0f4SStephen M. Cameron 	} else if (h->msi_vector) {
62640097f0f4SStephen M. Cameron 		if (h->pdev->msi_enabled)
626564670ac8SStephen M. Cameron 			pci_disable_msi(h->pdev);
62660097f0f4SStephen M. Cameron 	}
626764670ac8SStephen M. Cameron #endif /* CONFIG_PCI_MSI */
62680097f0f4SStephen M. Cameron }
62690097f0f4SStephen M. Cameron 
62700097f0f4SStephen M. Cameron static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
62710097f0f4SStephen M. Cameron {
62720097f0f4SStephen M. Cameron 	hpsa_free_irqs_and_disable_msix(h);
627364670ac8SStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
627464670ac8SStephen M. Cameron 	hpsa_free_cmd_pool(h);
6275e1f7de0cSMatt Gates 	kfree(h->ioaccel1_blockFetchTable);
627664670ac8SStephen M. Cameron 	kfree(h->blockFetchTable);
627764670ac8SStephen M. Cameron 	pci_free_consistent(h->pdev, h->reply_pool_size,
627864670ac8SStephen M. Cameron 		h->reply_pool, h->reply_pool_dhandle);
627964670ac8SStephen M. Cameron 	if (h->vaddr)
628064670ac8SStephen M. Cameron 		iounmap(h->vaddr);
628164670ac8SStephen M. Cameron 	if (h->transtable)
628264670ac8SStephen M. Cameron 		iounmap(h->transtable);
628364670ac8SStephen M. Cameron 	if (h->cfgtable)
628464670ac8SStephen M. Cameron 		iounmap(h->cfgtable);
628564670ac8SStephen M. Cameron 	pci_release_regions(h->pdev);
628664670ac8SStephen M. Cameron 	kfree(h);
628764670ac8SStephen M. Cameron }
628864670ac8SStephen M. Cameron 
6289a0c12413SStephen M. Cameron /* Called when controller lockup detected. */
6290a0c12413SStephen M. Cameron static void fail_all_cmds_on_list(struct ctlr_info *h, struct list_head *list)
6291a0c12413SStephen M. Cameron {
6292a0c12413SStephen M. Cameron 	struct CommandList *c = NULL;
6293a0c12413SStephen M. Cameron 
6294a0c12413SStephen M. Cameron 	assert_spin_locked(&h->lock);
6295a0c12413SStephen M. Cameron 	/* Mark all outstanding commands as failed and complete them. */
6296a0c12413SStephen M. Cameron 	while (!list_empty(list)) {
6297a0c12413SStephen M. Cameron 		c = list_entry(list->next, struct CommandList, list);
6298a0c12413SStephen M. Cameron 		c->err_info->CommandStatus = CMD_HARDWARE_ERR;
62995a3d16f5SStephen M. Cameron 		finish_cmd(c);
6300a0c12413SStephen M. Cameron 	}
6301a0c12413SStephen M. Cameron }
6302a0c12413SStephen M. Cameron 
6303a0c12413SStephen M. Cameron static void controller_lockup_detected(struct ctlr_info *h)
6304a0c12413SStephen M. Cameron {
6305a0c12413SStephen M. Cameron 	unsigned long flags;
6306a0c12413SStephen M. Cameron 
6307a0c12413SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
6308a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
6309a0c12413SStephen M. Cameron 	h->lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
6310a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
6311a0c12413SStephen M. Cameron 	dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x\n",
6312a0c12413SStephen M. Cameron 			h->lockup_detected);
6313a0c12413SStephen M. Cameron 	pci_disable_device(h->pdev);
6314a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
6315a0c12413SStephen M. Cameron 	fail_all_cmds_on_list(h, &h->cmpQ);
6316a0c12413SStephen M. Cameron 	fail_all_cmds_on_list(h, &h->reqQ);
6317a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
6318a0c12413SStephen M. Cameron }
6319a0c12413SStephen M. Cameron 
6320a0c12413SStephen M. Cameron static void detect_controller_lockup(struct ctlr_info *h)
6321a0c12413SStephen M. Cameron {
6322a0c12413SStephen M. Cameron 	u64 now;
6323a0c12413SStephen M. Cameron 	u32 heartbeat;
6324a0c12413SStephen M. Cameron 	unsigned long flags;
6325a0c12413SStephen M. Cameron 
6326a0c12413SStephen M. Cameron 	now = get_jiffies_64();
6327a0c12413SStephen M. Cameron 	/* If we've received an interrupt recently, we're ok. */
6328a0c12413SStephen M. Cameron 	if (time_after64(h->last_intr_timestamp +
6329e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
6330a0c12413SStephen M. Cameron 		return;
6331a0c12413SStephen M. Cameron 
6332a0c12413SStephen M. Cameron 	/*
6333a0c12413SStephen M. Cameron 	 * If we've already checked the heartbeat recently, we're ok.
6334a0c12413SStephen M. Cameron 	 * This could happen if someone sends us a signal. We
6335a0c12413SStephen M. Cameron 	 * otherwise don't care about signals in this thread.
6336a0c12413SStephen M. Cameron 	 */
6337a0c12413SStephen M. Cameron 	if (time_after64(h->last_heartbeat_timestamp +
6338e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
6339a0c12413SStephen M. Cameron 		return;
6340a0c12413SStephen M. Cameron 
6341a0c12413SStephen M. Cameron 	/* If heartbeat has not changed since we last looked, we're not ok. */
6342a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
6343a0c12413SStephen M. Cameron 	heartbeat = readl(&h->cfgtable->HeartBeat);
6344a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
6345a0c12413SStephen M. Cameron 	if (h->last_heartbeat == heartbeat) {
6346a0c12413SStephen M. Cameron 		controller_lockup_detected(h);
6347a0c12413SStephen M. Cameron 		return;
6348a0c12413SStephen M. Cameron 	}
6349a0c12413SStephen M. Cameron 
6350a0c12413SStephen M. Cameron 	/* We're ok. */
6351a0c12413SStephen M. Cameron 	h->last_heartbeat = heartbeat;
6352a0c12413SStephen M. Cameron 	h->last_heartbeat_timestamp = now;
6353a0c12413SStephen M. Cameron }
6354a0c12413SStephen M. Cameron 
635576438d08SStephen M. Cameron static int hpsa_kickoff_rescan(struct ctlr_info *h)
635676438d08SStephen M. Cameron {
635776438d08SStephen M. Cameron 	int i;
635876438d08SStephen M. Cameron 	char *event_type;
635976438d08SStephen M. Cameron 
6360e863d68eSScott Teel 	/* Clear the driver-requested rescan flag */
6361e863d68eSScott Teel 	h->drv_req_rescan = 0;
6362e863d68eSScott Teel 
636376438d08SStephen M. Cameron 	/* Ask the controller to clear the events we're handling. */
63641f7cee8cSStephen M. Cameron 	if ((h->transMethod & (CFGTBL_Trans_io_accel1
63651f7cee8cSStephen M. Cameron 			| CFGTBL_Trans_io_accel2)) &&
636676438d08SStephen M. Cameron 		(h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
636776438d08SStephen M. Cameron 		 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
636876438d08SStephen M. Cameron 
636976438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
637076438d08SStephen M. Cameron 			event_type = "state change";
637176438d08SStephen M. Cameron 		if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
637276438d08SStephen M. Cameron 			event_type = "configuration change";
637376438d08SStephen M. Cameron 		/* Stop sending new RAID offload reqs via the IO accelerator */
637476438d08SStephen M. Cameron 		scsi_block_requests(h->scsi_host);
637576438d08SStephen M. Cameron 		for (i = 0; i < h->ndevices; i++)
637676438d08SStephen M. Cameron 			h->dev[i]->offload_enabled = 0;
637776438d08SStephen M. Cameron 		hpsa_drain_commands(h);
637876438d08SStephen M. Cameron 		/* Set 'accelerator path config change' bit */
637976438d08SStephen M. Cameron 		dev_warn(&h->pdev->dev,
638076438d08SStephen M. Cameron 			"Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
638176438d08SStephen M. Cameron 			h->events, event_type);
638276438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
638376438d08SStephen M. Cameron 		/* Set the "clear event notify field update" bit 6 */
638476438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
638576438d08SStephen M. Cameron 		/* Wait until ctlr clears 'clear event notify field', bit 6 */
638676438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
638776438d08SStephen M. Cameron 		scsi_unblock_requests(h->scsi_host);
638876438d08SStephen M. Cameron 	} else {
638976438d08SStephen M. Cameron 		/* Acknowledge controller notification events. */
639076438d08SStephen M. Cameron 		writel(h->events, &(h->cfgtable->clear_event_notify));
639176438d08SStephen M. Cameron 		writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
639276438d08SStephen M. Cameron 		hpsa_wait_for_clear_event_notify_ack(h);
639376438d08SStephen M. Cameron #if 0
639476438d08SStephen M. Cameron 		writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
639576438d08SStephen M. Cameron 		hpsa_wait_for_mode_change_ack(h);
639676438d08SStephen M. Cameron #endif
639776438d08SStephen M. Cameron 	}
639876438d08SStephen M. Cameron 
639976438d08SStephen M. Cameron 	/* Something in the device list may have changed to trigger
640076438d08SStephen M. Cameron 	 * the event, so do a rescan.
640176438d08SStephen M. Cameron 	 */
640276438d08SStephen M. Cameron 	hpsa_scan_start(h->scsi_host);
640376438d08SStephen M. Cameron 	/* release reference taken on scsi host in check_controller_events */
640476438d08SStephen M. Cameron 	scsi_host_put(h->scsi_host);
640576438d08SStephen M. Cameron 	return 0;
640676438d08SStephen M. Cameron }
640776438d08SStephen M. Cameron 
640876438d08SStephen M. Cameron /* Check a register on the controller to see if there are configuration
640976438d08SStephen M. Cameron  * changes (added/changed/removed logical drives, etc.) which mean that
6410e863d68eSScott Teel  * we should rescan the controller for devices.
6411e863d68eSScott Teel  * Also check flag for driver-initiated rescan.
6412e863d68eSScott Teel  * If either flag or controller event indicate rescan, add the controller
641376438d08SStephen M. Cameron  * to the list of controllers needing to be rescanned, and gets a
641476438d08SStephen M. Cameron  * reference to the associated scsi_host.
641576438d08SStephen M. Cameron  */
641676438d08SStephen M. Cameron static void hpsa_ctlr_needs_rescan(struct ctlr_info *h)
641776438d08SStephen M. Cameron {
641876438d08SStephen M. Cameron 	if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
641976438d08SStephen M. Cameron 		return;
642076438d08SStephen M. Cameron 
642176438d08SStephen M. Cameron 	h->events = readl(&(h->cfgtable->event_notify));
6422e863d68eSScott Teel 	if (!h->events && !h->drv_req_rescan)
642376438d08SStephen M. Cameron 		return;
642476438d08SStephen M. Cameron 
642576438d08SStephen M. Cameron 	/*
642676438d08SStephen M. Cameron 	 * Take a reference on scsi host for the duration of the scan
642776438d08SStephen M. Cameron 	 * Release in hpsa_kickoff_rescan().  No lock needed for scan_list
642876438d08SStephen M. Cameron 	 * as only a single thread accesses this list.
642976438d08SStephen M. Cameron 	 */
643076438d08SStephen M. Cameron 	scsi_host_get(h->scsi_host);
643176438d08SStephen M. Cameron 	hpsa_kickoff_rescan(h);
643276438d08SStephen M. Cameron }
643376438d08SStephen M. Cameron 
64348a98db73SStephen M. Cameron static void hpsa_monitor_ctlr_worker(struct work_struct *work)
6435a0c12413SStephen M. Cameron {
6436a0c12413SStephen M. Cameron 	unsigned long flags;
64378a98db73SStephen M. Cameron 	struct ctlr_info *h = container_of(to_delayed_work(work),
64388a98db73SStephen M. Cameron 					struct ctlr_info, monitor_ctlr_work);
6439a0c12413SStephen M. Cameron 	detect_controller_lockup(h);
64408a98db73SStephen M. Cameron 	if (h->lockup_detected)
64418a98db73SStephen M. Cameron 		return;
644276438d08SStephen M. Cameron 	hpsa_ctlr_needs_rescan(h);
64438a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
64448a98db73SStephen M. Cameron 	if (h->remove_in_progress) {
64458a98db73SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
6446a0c12413SStephen M. Cameron 		return;
6447a0c12413SStephen M. Cameron 	}
64488a98db73SStephen M. Cameron 	schedule_delayed_work(&h->monitor_ctlr_work,
64498a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
64508a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
6451a0c12413SStephen M. Cameron }
6452a0c12413SStephen M. Cameron 
64536f039790SGreg Kroah-Hartman static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
64544c2a8c40SStephen M. Cameron {
64554c2a8c40SStephen M. Cameron 	int dac, rc;
64564c2a8c40SStephen M. Cameron 	struct ctlr_info *h;
645764670ac8SStephen M. Cameron 	int try_soft_reset = 0;
645864670ac8SStephen M. Cameron 	unsigned long flags;
64594c2a8c40SStephen M. Cameron 
64604c2a8c40SStephen M. Cameron 	if (number_of_controllers == 0)
64614c2a8c40SStephen M. Cameron 		printk(KERN_INFO DRIVER_NAME "\n");
64624c2a8c40SStephen M. Cameron 
64634c2a8c40SStephen M. Cameron 	rc = hpsa_init_reset_devices(pdev);
646464670ac8SStephen M. Cameron 	if (rc) {
646564670ac8SStephen M. Cameron 		if (rc != -ENOTSUPP)
64664c2a8c40SStephen M. Cameron 			return rc;
646764670ac8SStephen M. Cameron 		/* If the reset fails in a particular way (it has no way to do
646864670ac8SStephen M. Cameron 		 * a proper hard reset, so returns -ENOTSUPP) we can try to do
646964670ac8SStephen M. Cameron 		 * a soft reset once we get the controller configured up to the
647064670ac8SStephen M. Cameron 		 * point that it can accept a command.
647164670ac8SStephen M. Cameron 		 */
647264670ac8SStephen M. Cameron 		try_soft_reset = 1;
647364670ac8SStephen M. Cameron 		rc = 0;
647464670ac8SStephen M. Cameron 	}
647564670ac8SStephen M. Cameron 
647664670ac8SStephen M. Cameron reinit_after_soft_reset:
64774c2a8c40SStephen M. Cameron 
6478303932fdSDon Brace 	/* Command structures must be aligned on a 32-byte boundary because
6479303932fdSDon Brace 	 * the 5 lower bits of the address are used by the hardware. and by
6480303932fdSDon Brace 	 * the driver.  See comments in hpsa.h for more info.
6481303932fdSDon Brace 	 */
6482283b4a9bSStephen M. Cameron #define COMMANDLIST_ALIGNMENT 128
6483303932fdSDon Brace 	BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
6484edd16368SStephen M. Cameron 	h = kzalloc(sizeof(*h), GFP_KERNEL);
6485edd16368SStephen M. Cameron 	if (!h)
6486ecd9aad4SStephen M. Cameron 		return -ENOMEM;
6487edd16368SStephen M. Cameron 
648855c06c71SStephen M. Cameron 	h->pdev = pdev;
6489a9a3a273SStephen M. Cameron 	h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
64909e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&h->cmpQ);
64919e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&h->reqQ);
64926eaf46fdSStephen M. Cameron 	spin_lock_init(&h->lock);
64936eaf46fdSStephen M. Cameron 	spin_lock_init(&h->scan_lock);
64940390f0c0SStephen M. Cameron 	spin_lock_init(&h->passthru_count_lock);
649555c06c71SStephen M. Cameron 	rc = hpsa_pci_init(h);
6496ecd9aad4SStephen M. Cameron 	if (rc != 0)
6497edd16368SStephen M. Cameron 		goto clean1;
6498edd16368SStephen M. Cameron 
6499f79cfec6SStephen M. Cameron 	sprintf(h->devname, HPSA "%d", number_of_controllers);
6500edd16368SStephen M. Cameron 	h->ctlr = number_of_controllers;
6501edd16368SStephen M. Cameron 	number_of_controllers++;
6502edd16368SStephen M. Cameron 
6503edd16368SStephen M. Cameron 	/* configure PCI DMA stuff */
6504ecd9aad4SStephen M. Cameron 	rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
6505ecd9aad4SStephen M. Cameron 	if (rc == 0) {
6506edd16368SStephen M. Cameron 		dac = 1;
6507ecd9aad4SStephen M. Cameron 	} else {
6508ecd9aad4SStephen M. Cameron 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6509ecd9aad4SStephen M. Cameron 		if (rc == 0) {
6510edd16368SStephen M. Cameron 			dac = 0;
6511ecd9aad4SStephen M. Cameron 		} else {
6512edd16368SStephen M. Cameron 			dev_err(&pdev->dev, "no suitable DMA available\n");
6513edd16368SStephen M. Cameron 			goto clean1;
6514edd16368SStephen M. Cameron 		}
6515ecd9aad4SStephen M. Cameron 	}
6516edd16368SStephen M. Cameron 
6517edd16368SStephen M. Cameron 	/* make sure the board interrupts are off */
6518edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
651910f66018SStephen M. Cameron 
65200ae01a32SStephen M. Cameron 	if (hpsa_request_irq(h, do_hpsa_intr_msi, do_hpsa_intr_intx))
6521edd16368SStephen M. Cameron 		goto clean2;
6522303932fdSDon Brace 	dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
6523303932fdSDon Brace 	       h->devname, pdev->device,
6524a9a3a273SStephen M. Cameron 	       h->intr[h->intr_mode], dac ? "" : " not");
65252e9d1b36SStephen M. Cameron 	if (hpsa_allocate_cmd_pool(h))
6526edd16368SStephen M. Cameron 		goto clean4;
652733a2ffceSStephen M. Cameron 	if (hpsa_allocate_sg_chain_blocks(h))
652833a2ffceSStephen M. Cameron 		goto clean4;
6529a08a8471SStephen M. Cameron 	init_waitqueue_head(&h->scan_wait_queue);
6530a08a8471SStephen M. Cameron 	h->scan_finished = 1; /* no scan currently in progress */
6531edd16368SStephen M. Cameron 
6532edd16368SStephen M. Cameron 	pci_set_drvdata(pdev, h);
65339a41338eSStephen M. Cameron 	h->ndevices = 0;
65349a41338eSStephen M. Cameron 	h->scsi_host = NULL;
65359a41338eSStephen M. Cameron 	spin_lock_init(&h->devlock);
653664670ac8SStephen M. Cameron 	hpsa_put_ctlr_into_performant_mode(h);
653764670ac8SStephen M. Cameron 
653864670ac8SStephen M. Cameron 	/* At this point, the controller is ready to take commands.
653964670ac8SStephen M. Cameron 	 * Now, if reset_devices and the hard reset didn't work, try
654064670ac8SStephen M. Cameron 	 * the soft reset and see if that works.
654164670ac8SStephen M. Cameron 	 */
654264670ac8SStephen M. Cameron 	if (try_soft_reset) {
654364670ac8SStephen M. Cameron 
654464670ac8SStephen M. Cameron 		/* This is kind of gross.  We may or may not get a completion
654564670ac8SStephen M. Cameron 		 * from the soft reset command, and if we do, then the value
654664670ac8SStephen M. Cameron 		 * from the fifo may or may not be valid.  So, we wait 10 secs
654764670ac8SStephen M. Cameron 		 * after the reset throwing away any completions we get during
654864670ac8SStephen M. Cameron 		 * that time.  Unregister the interrupt handler and register
654964670ac8SStephen M. Cameron 		 * fake ones to scoop up any residual completions.
655064670ac8SStephen M. Cameron 		 */
655164670ac8SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
655264670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
655364670ac8SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
6554254f796bSMatt Gates 		free_irqs(h);
655564670ac8SStephen M. Cameron 		rc = hpsa_request_irq(h, hpsa_msix_discard_completions,
655664670ac8SStephen M. Cameron 					hpsa_intx_discard_completions);
655764670ac8SStephen M. Cameron 		if (rc) {
655864670ac8SStephen M. Cameron 			dev_warn(&h->pdev->dev, "Failed to request_irq after "
655964670ac8SStephen M. Cameron 				"soft reset.\n");
656064670ac8SStephen M. Cameron 			goto clean4;
656164670ac8SStephen M. Cameron 		}
656264670ac8SStephen M. Cameron 
656364670ac8SStephen M. Cameron 		rc = hpsa_kdump_soft_reset(h);
656464670ac8SStephen M. Cameron 		if (rc)
656564670ac8SStephen M. Cameron 			/* Neither hard nor soft reset worked, we're hosed. */
656664670ac8SStephen M. Cameron 			goto clean4;
656764670ac8SStephen M. Cameron 
656864670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev, "Board READY.\n");
656964670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev,
657064670ac8SStephen M. Cameron 			"Waiting for stale completions to drain.\n");
657164670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_ON);
657264670ac8SStephen M. Cameron 		msleep(10000);
657364670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
657464670ac8SStephen M. Cameron 
657564670ac8SStephen M. Cameron 		rc = controller_reset_failed(h->cfgtable);
657664670ac8SStephen M. Cameron 		if (rc)
657764670ac8SStephen M. Cameron 			dev_info(&h->pdev->dev,
657864670ac8SStephen M. Cameron 				"Soft reset appears to have failed.\n");
657964670ac8SStephen M. Cameron 
658064670ac8SStephen M. Cameron 		/* since the controller's reset, we have to go back and re-init
658164670ac8SStephen M. Cameron 		 * everything.  Easiest to just forget what we've done and do it
658264670ac8SStephen M. Cameron 		 * all over again.
658364670ac8SStephen M. Cameron 		 */
658464670ac8SStephen M. Cameron 		hpsa_undo_allocations_after_kdump_soft_reset(h);
658564670ac8SStephen M. Cameron 		try_soft_reset = 0;
658664670ac8SStephen M. Cameron 		if (rc)
658764670ac8SStephen M. Cameron 			/* don't go to clean4, we already unallocated */
658864670ac8SStephen M. Cameron 			return -ENODEV;
658964670ac8SStephen M. Cameron 
659064670ac8SStephen M. Cameron 		goto reinit_after_soft_reset;
659164670ac8SStephen M. Cameron 	}
6592edd16368SStephen M. Cameron 
6593da0697bdSScott Teel 	/* Enable Accelerated IO path at driver layer */
6594da0697bdSScott Teel 	h->acciopath_status = 1;
6595da0697bdSScott Teel 
6596e863d68eSScott Teel 	h->drv_req_rescan = 0;
6597e863d68eSScott Teel 
6598edd16368SStephen M. Cameron 	/* Turn the interrupts on so we can service requests */
6599edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_ON);
6600edd16368SStephen M. Cameron 
6601339b2b14SStephen M. Cameron 	hpsa_hba_inquiry(h);
6602edd16368SStephen M. Cameron 	hpsa_register_scsi(h);	/* hook ourselves into SCSI subsystem */
66038a98db73SStephen M. Cameron 
66048a98db73SStephen M. Cameron 	/* Monitor the controller for firmware lockups */
66058a98db73SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
66068a98db73SStephen M. Cameron 	INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
66078a98db73SStephen M. Cameron 	schedule_delayed_work(&h->monitor_ctlr_work,
66088a98db73SStephen M. Cameron 				h->heartbeat_sample_interval);
660988bf6d62SStephen M. Cameron 	return 0;
6610edd16368SStephen M. Cameron 
6611edd16368SStephen M. Cameron clean4:
661233a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
66132e9d1b36SStephen M. Cameron 	hpsa_free_cmd_pool(h);
6614254f796bSMatt Gates 	free_irqs(h);
6615edd16368SStephen M. Cameron clean2:
6616edd16368SStephen M. Cameron clean1:
6617edd16368SStephen M. Cameron 	kfree(h);
6618ecd9aad4SStephen M. Cameron 	return rc;
6619edd16368SStephen M. Cameron }
6620edd16368SStephen M. Cameron 
6621edd16368SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h)
6622edd16368SStephen M. Cameron {
6623edd16368SStephen M. Cameron 	char *flush_buf;
6624edd16368SStephen M. Cameron 	struct CommandList *c;
6625702890e3SStephen M. Cameron 	unsigned long flags;
6626702890e3SStephen M. Cameron 
6627702890e3SStephen M. Cameron 	/* Don't bother trying to flush the cache if locked up */
6628702890e3SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
6629702890e3SStephen M. Cameron 	if (unlikely(h->lockup_detected)) {
6630702890e3SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
6631702890e3SStephen M. Cameron 		return;
6632702890e3SStephen M. Cameron 	}
6633702890e3SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
6634edd16368SStephen M. Cameron 
6635edd16368SStephen M. Cameron 	flush_buf = kzalloc(4, GFP_KERNEL);
6636edd16368SStephen M. Cameron 	if (!flush_buf)
6637edd16368SStephen M. Cameron 		return;
6638edd16368SStephen M. Cameron 
6639edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
6640edd16368SStephen M. Cameron 	if (!c) {
6641edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
6642edd16368SStephen M. Cameron 		goto out_of_memory;
6643edd16368SStephen M. Cameron 	}
6644a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
6645a2dac136SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_CMD)) {
6646a2dac136SStephen M. Cameron 		goto out;
6647a2dac136SStephen M. Cameron 	}
6648edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
6649edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus != 0)
6650a2dac136SStephen M. Cameron out:
6651edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
6652edd16368SStephen M. Cameron 			"error flushing cache on controller\n");
6653edd16368SStephen M. Cameron 	cmd_special_free(h, c);
6654edd16368SStephen M. Cameron out_of_memory:
6655edd16368SStephen M. Cameron 	kfree(flush_buf);
6656edd16368SStephen M. Cameron }
6657edd16368SStephen M. Cameron 
6658edd16368SStephen M. Cameron static void hpsa_shutdown(struct pci_dev *pdev)
6659edd16368SStephen M. Cameron {
6660edd16368SStephen M. Cameron 	struct ctlr_info *h;
6661edd16368SStephen M. Cameron 
6662edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
6663edd16368SStephen M. Cameron 	/* Turn board interrupts off  and send the flush cache command
6664edd16368SStephen M. Cameron 	 * sendcmd will turn off interrupt, and send the flush...
6665edd16368SStephen M. Cameron 	 * To write all data in the battery backed cache to disks
6666edd16368SStephen M. Cameron 	 */
6667edd16368SStephen M. Cameron 	hpsa_flush_cache(h);
6668edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
66690097f0f4SStephen M. Cameron 	hpsa_free_irqs_and_disable_msix(h);
6670edd16368SStephen M. Cameron }
6671edd16368SStephen M. Cameron 
66726f039790SGreg Kroah-Hartman static void hpsa_free_device_info(struct ctlr_info *h)
667355e14e76SStephen M. Cameron {
667455e14e76SStephen M. Cameron 	int i;
667555e14e76SStephen M. Cameron 
667655e14e76SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++)
667755e14e76SStephen M. Cameron 		kfree(h->dev[i]);
667855e14e76SStephen M. Cameron }
667955e14e76SStephen M. Cameron 
66806f039790SGreg Kroah-Hartman static void hpsa_remove_one(struct pci_dev *pdev)
6681edd16368SStephen M. Cameron {
6682edd16368SStephen M. Cameron 	struct ctlr_info *h;
66838a98db73SStephen M. Cameron 	unsigned long flags;
6684edd16368SStephen M. Cameron 
6685edd16368SStephen M. Cameron 	if (pci_get_drvdata(pdev) == NULL) {
6686edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "unable to remove device\n");
6687edd16368SStephen M. Cameron 		return;
6688edd16368SStephen M. Cameron 	}
6689edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
66908a98db73SStephen M. Cameron 
66918a98db73SStephen M. Cameron 	/* Get rid of any controller monitoring work items */
66928a98db73SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
66938a98db73SStephen M. Cameron 	h->remove_in_progress = 1;
66948a98db73SStephen M. Cameron 	cancel_delayed_work(&h->monitor_ctlr_work);
66958a98db73SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
66968a98db73SStephen M. Cameron 
6697edd16368SStephen M. Cameron 	hpsa_unregister_scsi(h);	/* unhook from SCSI subsystem */
6698edd16368SStephen M. Cameron 	hpsa_shutdown(pdev);
6699edd16368SStephen M. Cameron 	iounmap(h->vaddr);
6700204892e9SStephen M. Cameron 	iounmap(h->transtable);
6701204892e9SStephen M. Cameron 	iounmap(h->cfgtable);
670255e14e76SStephen M. Cameron 	hpsa_free_device_info(h);
670333a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
6704edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev,
6705edd16368SStephen M. Cameron 		h->nr_cmds * sizeof(struct CommandList),
6706edd16368SStephen M. Cameron 		h->cmd_pool, h->cmd_pool_dhandle);
6707edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev,
6708edd16368SStephen M. Cameron 		h->nr_cmds * sizeof(struct ErrorInfo),
6709edd16368SStephen M. Cameron 		h->errinfo_pool, h->errinfo_pool_dhandle);
6710303932fdSDon Brace 	pci_free_consistent(h->pdev, h->reply_pool_size,
6711303932fdSDon Brace 		h->reply_pool, h->reply_pool_dhandle);
6712edd16368SStephen M. Cameron 	kfree(h->cmd_pool_bits);
6713303932fdSDon Brace 	kfree(h->blockFetchTable);
6714e1f7de0cSMatt Gates 	kfree(h->ioaccel1_blockFetchTable);
6715aca9012aSStephen M. Cameron 	kfree(h->ioaccel2_blockFetchTable);
6716339b2b14SStephen M. Cameron 	kfree(h->hba_inquiry_data);
6717f0bd0b68SStephen M. Cameron 	pci_disable_device(pdev);
6718edd16368SStephen M. Cameron 	pci_release_regions(pdev);
6719edd16368SStephen M. Cameron 	kfree(h);
6720edd16368SStephen M. Cameron }
6721edd16368SStephen M. Cameron 
6722edd16368SStephen M. Cameron static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
6723edd16368SStephen M. Cameron 	__attribute__((unused)) pm_message_t state)
6724edd16368SStephen M. Cameron {
6725edd16368SStephen M. Cameron 	return -ENOSYS;
6726edd16368SStephen M. Cameron }
6727edd16368SStephen M. Cameron 
6728edd16368SStephen M. Cameron static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
6729edd16368SStephen M. Cameron {
6730edd16368SStephen M. Cameron 	return -ENOSYS;
6731edd16368SStephen M. Cameron }
6732edd16368SStephen M. Cameron 
6733edd16368SStephen M. Cameron static struct pci_driver hpsa_pci_driver = {
6734f79cfec6SStephen M. Cameron 	.name = HPSA,
6735edd16368SStephen M. Cameron 	.probe = hpsa_init_one,
67366f039790SGreg Kroah-Hartman 	.remove = hpsa_remove_one,
6737edd16368SStephen M. Cameron 	.id_table = hpsa_pci_device_id,	/* id_table */
6738edd16368SStephen M. Cameron 	.shutdown = hpsa_shutdown,
6739edd16368SStephen M. Cameron 	.suspend = hpsa_suspend,
6740edd16368SStephen M. Cameron 	.resume = hpsa_resume,
6741edd16368SStephen M. Cameron };
6742edd16368SStephen M. Cameron 
6743303932fdSDon Brace /* Fill in bucket_map[], given nsgs (the max number of
6744303932fdSDon Brace  * scatter gather elements supported) and bucket[],
6745303932fdSDon Brace  * which is an array of 8 integers.  The bucket[] array
6746303932fdSDon Brace  * contains 8 different DMA transfer sizes (in 16
6747303932fdSDon Brace  * byte increments) which the controller uses to fetch
6748303932fdSDon Brace  * commands.  This function fills in bucket_map[], which
6749303932fdSDon Brace  * maps a given number of scatter gather elements to one of
6750303932fdSDon Brace  * the 8 DMA transfer sizes.  The point of it is to allow the
6751303932fdSDon Brace  * controller to only do as much DMA as needed to fetch the
6752303932fdSDon Brace  * command, with the DMA transfer size encoded in the lower
6753303932fdSDon Brace  * bits of the command address.
6754303932fdSDon Brace  */
6755303932fdSDon Brace static void  calc_bucket_map(int bucket[], int num_buckets,
6756e1f7de0cSMatt Gates 	int nsgs, int min_blocks, int *bucket_map)
6757303932fdSDon Brace {
6758303932fdSDon Brace 	int i, j, b, size;
6759303932fdSDon Brace 
6760303932fdSDon Brace 	/* Note, bucket_map must have nsgs+1 entries. */
6761303932fdSDon Brace 	for (i = 0; i <= nsgs; i++) {
6762303932fdSDon Brace 		/* Compute size of a command with i SG entries */
6763e1f7de0cSMatt Gates 		size = i + min_blocks;
6764303932fdSDon Brace 		b = num_buckets; /* Assume the biggest bucket */
6765303932fdSDon Brace 		/* Find the bucket that is just big enough */
6766e1f7de0cSMatt Gates 		for (j = 0; j < num_buckets; j++) {
6767303932fdSDon Brace 			if (bucket[j] >= size) {
6768303932fdSDon Brace 				b = j;
6769303932fdSDon Brace 				break;
6770303932fdSDon Brace 			}
6771303932fdSDon Brace 		}
6772303932fdSDon Brace 		/* for a command with i SG entries, use bucket b. */
6773303932fdSDon Brace 		bucket_map[i] = b;
6774303932fdSDon Brace 	}
6775303932fdSDon Brace }
6776303932fdSDon Brace 
6777e1f7de0cSMatt Gates static void hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
6778303932fdSDon Brace {
67796c311b57SStephen M. Cameron 	int i;
67806c311b57SStephen M. Cameron 	unsigned long register_value;
6781e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
6782e1f7de0cSMatt Gates 			(trans_support & CFGTBL_Trans_use_short_tags) |
6783e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix |
6784b9af4937SStephen M. Cameron 			(trans_support & (CFGTBL_Trans_io_accel1 |
6785b9af4937SStephen M. Cameron 				CFGTBL_Trans_io_accel2));
6786e1f7de0cSMatt Gates 	struct access_method access = SA5_performant_access;
6787def342bdSStephen M. Cameron 
6788def342bdSStephen M. Cameron 	/* This is a bit complicated.  There are 8 registers on
6789def342bdSStephen M. Cameron 	 * the controller which we write to to tell it 8 different
6790def342bdSStephen M. Cameron 	 * sizes of commands which there may be.  It's a way of
6791def342bdSStephen M. Cameron 	 * reducing the DMA done to fetch each command.  Encoded into
6792def342bdSStephen M. Cameron 	 * each command's tag are 3 bits which communicate to the controller
6793def342bdSStephen M. Cameron 	 * which of the eight sizes that command fits within.  The size of
6794def342bdSStephen M. Cameron 	 * each command depends on how many scatter gather entries there are.
6795def342bdSStephen M. Cameron 	 * Each SG entry requires 16 bytes.  The eight registers are programmed
6796def342bdSStephen M. Cameron 	 * with the number of 16-byte blocks a command of that size requires.
6797def342bdSStephen M. Cameron 	 * The smallest command possible requires 5 such 16 byte blocks.
6798d66ae08bSStephen M. Cameron 	 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
6799def342bdSStephen M. Cameron 	 * blocks.  Note, this only extends to the SG entries contained
6800def342bdSStephen M. Cameron 	 * within the command block, and does not extend to chained blocks
6801def342bdSStephen M. Cameron 	 * of SG elements.   bft[] contains the eight values we write to
6802def342bdSStephen M. Cameron 	 * the registers.  They are not evenly distributed, but have more
6803def342bdSStephen M. Cameron 	 * sizes for small commands, and fewer sizes for larger commands.
6804def342bdSStephen M. Cameron 	 */
6805d66ae08bSStephen M. Cameron 	int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
6806b9af4937SStephen M. Cameron #define MIN_IOACCEL2_BFT_ENTRY 5
6807b9af4937SStephen M. Cameron #define HPSA_IOACCEL2_HEADER_SZ 4
6808b9af4937SStephen M. Cameron 	int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
6809b9af4937SStephen M. Cameron 			13, 14, 15, 16, 17, 18, 19,
6810b9af4937SStephen M. Cameron 			HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
6811b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
6812b9af4937SStephen M. Cameron 	BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
6813b9af4937SStephen M. Cameron 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
6814b9af4937SStephen M. Cameron 				 16 * MIN_IOACCEL2_BFT_ENTRY);
6815b9af4937SStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
6816d66ae08bSStephen M. Cameron 	BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
6817303932fdSDon Brace 	/*  5 = 1 s/g entry or 4k
6818303932fdSDon Brace 	 *  6 = 2 s/g entry or 8k
6819303932fdSDon Brace 	 *  8 = 4 s/g entry or 16k
6820303932fdSDon Brace 	 * 10 = 6 s/g entry or 24k
6821303932fdSDon Brace 	 */
6822303932fdSDon Brace 
6823303932fdSDon Brace 	/* Controller spec: zero out this buffer. */
6824303932fdSDon Brace 	memset(h->reply_pool, 0, h->reply_pool_size);
6825303932fdSDon Brace 
6826d66ae08bSStephen M. Cameron 	bft[7] = SG_ENTRIES_IN_CMD + 4;
6827d66ae08bSStephen M. Cameron 	calc_bucket_map(bft, ARRAY_SIZE(bft),
6828e1f7de0cSMatt Gates 				SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
6829303932fdSDon Brace 	for (i = 0; i < 8; i++)
6830303932fdSDon Brace 		writel(bft[i], &h->transtable->BlockFetch[i]);
6831303932fdSDon Brace 
6832303932fdSDon Brace 	/* size of controller ring buffer */
6833303932fdSDon Brace 	writel(h->max_commands, &h->transtable->RepQSize);
6834254f796bSMatt Gates 	writel(h->nreply_queues, &h->transtable->RepQCount);
6835303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrLow32);
6836303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrHigh32);
6837254f796bSMatt Gates 
6838254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
6839254f796bSMatt Gates 		writel(0, &h->transtable->RepQAddr[i].upper);
6840254f796bSMatt Gates 		writel(h->reply_pool_dhandle +
6841254f796bSMatt Gates 			(h->max_commands * sizeof(u64) * i),
6842254f796bSMatt Gates 			&h->transtable->RepQAddr[i].lower);
6843254f796bSMatt Gates 	}
6844254f796bSMatt Gates 
6845b9af4937SStephen M. Cameron 	writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
6846e1f7de0cSMatt Gates 	writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
6847e1f7de0cSMatt Gates 	/*
6848e1f7de0cSMatt Gates 	 * enable outbound interrupt coalescing in accelerator mode;
6849e1f7de0cSMatt Gates 	 */
6850e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
6851e1f7de0cSMatt Gates 		access = SA5_ioaccel_mode1_access;
6852e1f7de0cSMatt Gates 		writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
6853e1f7de0cSMatt Gates 		writel(4, &h->cfgtable->HostWrite.CoalIntCount);
6854c349775eSScott Teel 	} else {
6855c349775eSScott Teel 		if (trans_support & CFGTBL_Trans_io_accel2) {
6856c349775eSScott Teel 			access = SA5_ioaccel_mode2_access;
6857c349775eSScott Teel 			writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
6858c349775eSScott Teel 			writel(4, &h->cfgtable->HostWrite.CoalIntCount);
6859c349775eSScott Teel 		}
6860e1f7de0cSMatt Gates 	}
6861303932fdSDon Brace 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
68623f4336f3SStephen M. Cameron 	hpsa_wait_for_mode_change_ack(h);
6863303932fdSDon Brace 	register_value = readl(&(h->cfgtable->TransportActive));
6864303932fdSDon Brace 	if (!(register_value & CFGTBL_Trans_Performant)) {
6865303932fdSDon Brace 		dev_warn(&h->pdev->dev, "unable to get board into"
6866303932fdSDon Brace 					" performant mode\n");
6867303932fdSDon Brace 		return;
6868303932fdSDon Brace 	}
6869960a30e7SStephen M. Cameron 	/* Change the access methods to the performant access methods */
6870e1f7de0cSMatt Gates 	h->access = access;
6871e1f7de0cSMatt Gates 	h->transMethod = transMethod;
6872e1f7de0cSMatt Gates 
6873b9af4937SStephen M. Cameron 	if (!((trans_support & CFGTBL_Trans_io_accel1) ||
6874b9af4937SStephen M. Cameron 		(trans_support & CFGTBL_Trans_io_accel2)))
6875e1f7de0cSMatt Gates 		return;
6876e1f7de0cSMatt Gates 
6877b9af4937SStephen M. Cameron 	if (trans_support & CFGTBL_Trans_io_accel1) {
6878e1f7de0cSMatt Gates 		/* Set up I/O accelerator mode */
6879e1f7de0cSMatt Gates 		for (i = 0; i < h->nreply_queues; i++) {
6880e1f7de0cSMatt Gates 			writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
6881e1f7de0cSMatt Gates 			h->reply_queue[i].current_entry =
6882e1f7de0cSMatt Gates 				readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
6883e1f7de0cSMatt Gates 		}
6884283b4a9bSStephen M. Cameron 		bft[7] = h->ioaccel_maxsg + 8;
6885283b4a9bSStephen M. Cameron 		calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
6886e1f7de0cSMatt Gates 				h->ioaccel1_blockFetchTable);
6887e1f7de0cSMatt Gates 
6888e1f7de0cSMatt Gates 		/* initialize all reply queue entries to unused */
6889e1f7de0cSMatt Gates 		memset(h->reply_pool, (u8) IOACCEL_MODE1_REPLY_UNUSED,
6890e1f7de0cSMatt Gates 				h->reply_pool_size);
6891e1f7de0cSMatt Gates 
6892e1f7de0cSMatt Gates 		/* set all the constant fields in the accelerator command
6893e1f7de0cSMatt Gates 		 * frames once at init time to save CPU cycles later.
6894e1f7de0cSMatt Gates 		 */
6895e1f7de0cSMatt Gates 		for (i = 0; i < h->nr_cmds; i++) {
6896e1f7de0cSMatt Gates 			struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
6897e1f7de0cSMatt Gates 
6898e1f7de0cSMatt Gates 			cp->function = IOACCEL1_FUNCTION_SCSIIO;
6899e1f7de0cSMatt Gates 			cp->err_info = (u32) (h->errinfo_pool_dhandle +
6900e1f7de0cSMatt Gates 					(i * sizeof(struct ErrorInfo)));
6901e1f7de0cSMatt Gates 			cp->err_info_len = sizeof(struct ErrorInfo);
6902e1f7de0cSMatt Gates 			cp->sgl_offset = IOACCEL1_SGLOFFSET;
6903e1f7de0cSMatt Gates 			cp->host_context_flags = IOACCEL1_HCFLAGS_CISS_FORMAT;
6904e1f7de0cSMatt Gates 			cp->timeout_sec = 0;
6905e1f7de0cSMatt Gates 			cp->ReplyQueue = 0;
6906b9af4937SStephen M. Cameron 			cp->Tag.lower = (i << DIRECT_LOOKUP_SHIFT) |
6907b9af4937SStephen M. Cameron 						DIRECT_LOOKUP_BIT;
6908e1f7de0cSMatt Gates 			cp->Tag.upper = 0;
6909b9af4937SStephen M. Cameron 			cp->host_addr.lower =
6910b9af4937SStephen M. Cameron 				(u32) (h->ioaccel_cmd_pool_dhandle +
6911e1f7de0cSMatt Gates 					(i * sizeof(struct io_accel1_cmd)));
6912e1f7de0cSMatt Gates 			cp->host_addr.upper = 0;
6913e1f7de0cSMatt Gates 		}
6914b9af4937SStephen M. Cameron 	} else if (trans_support & CFGTBL_Trans_io_accel2) {
6915b9af4937SStephen M. Cameron 		u64 cfg_offset, cfg_base_addr_index;
6916b9af4937SStephen M. Cameron 		u32 bft2_offset, cfg_base_addr;
6917b9af4937SStephen M. Cameron 		int rc;
6918b9af4937SStephen M. Cameron 
6919b9af4937SStephen M. Cameron 		rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
6920b9af4937SStephen M. Cameron 			&cfg_base_addr_index, &cfg_offset);
6921b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
6922b9af4937SStephen M. Cameron 		bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
6923b9af4937SStephen M. Cameron 		calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
6924b9af4937SStephen M. Cameron 				4, h->ioaccel2_blockFetchTable);
6925b9af4937SStephen M. Cameron 		bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
6926b9af4937SStephen M. Cameron 		BUILD_BUG_ON(offsetof(struct CfgTable,
6927b9af4937SStephen M. Cameron 				io_accel_request_size_offset) != 0xb8);
6928b9af4937SStephen M. Cameron 		h->ioaccel2_bft2_regs =
6929b9af4937SStephen M. Cameron 			remap_pci_mem(pci_resource_start(h->pdev,
6930b9af4937SStephen M. Cameron 					cfg_base_addr_index) +
6931b9af4937SStephen M. Cameron 					cfg_offset + bft2_offset,
6932b9af4937SStephen M. Cameron 					ARRAY_SIZE(bft2) *
6933b9af4937SStephen M. Cameron 					sizeof(*h->ioaccel2_bft2_regs));
6934b9af4937SStephen M. Cameron 		for (i = 0; i < ARRAY_SIZE(bft2); i++)
6935b9af4937SStephen M. Cameron 			writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
6936b9af4937SStephen M. Cameron 	}
6937b9af4937SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
6938b9af4937SStephen M. Cameron 	hpsa_wait_for_mode_change_ack(h);
6939e1f7de0cSMatt Gates }
6940e1f7de0cSMatt Gates 
6941e1f7de0cSMatt Gates static int hpsa_alloc_ioaccel_cmd_and_bft(struct ctlr_info *h)
6942e1f7de0cSMatt Gates {
6943283b4a9bSStephen M. Cameron 	h->ioaccel_maxsg =
6944283b4a9bSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
6945283b4a9bSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
6946283b4a9bSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
6947283b4a9bSStephen M. Cameron 
6948e1f7de0cSMatt Gates 	/* Command structures must be aligned on a 128-byte boundary
6949e1f7de0cSMatt Gates 	 * because the 7 lower bits of the address are used by the
6950e1f7de0cSMatt Gates 	 * hardware.
6951e1f7de0cSMatt Gates 	 */
6952e1f7de0cSMatt Gates #define IOACCEL1_COMMANDLIST_ALIGNMENT 128
6953e1f7de0cSMatt Gates 	BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
6954e1f7de0cSMatt Gates 			IOACCEL1_COMMANDLIST_ALIGNMENT);
6955e1f7de0cSMatt Gates 	h->ioaccel_cmd_pool =
6956e1f7de0cSMatt Gates 		pci_alloc_consistent(h->pdev,
6957e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
6958e1f7de0cSMatt Gates 			&(h->ioaccel_cmd_pool_dhandle));
6959e1f7de0cSMatt Gates 
6960e1f7de0cSMatt Gates 	h->ioaccel1_blockFetchTable =
6961283b4a9bSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
6962e1f7de0cSMatt Gates 				sizeof(u32)), GFP_KERNEL);
6963e1f7de0cSMatt Gates 
6964e1f7de0cSMatt Gates 	if ((h->ioaccel_cmd_pool == NULL) ||
6965e1f7de0cSMatt Gates 		(h->ioaccel1_blockFetchTable == NULL))
6966e1f7de0cSMatt Gates 		goto clean_up;
6967e1f7de0cSMatt Gates 
6968e1f7de0cSMatt Gates 	memset(h->ioaccel_cmd_pool, 0,
6969e1f7de0cSMatt Gates 		h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
6970e1f7de0cSMatt Gates 	return 0;
6971e1f7de0cSMatt Gates 
6972e1f7de0cSMatt Gates clean_up:
6973e1f7de0cSMatt Gates 	if (h->ioaccel_cmd_pool)
6974e1f7de0cSMatt Gates 		pci_free_consistent(h->pdev,
6975e1f7de0cSMatt Gates 			h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
6976e1f7de0cSMatt Gates 			h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
6977e1f7de0cSMatt Gates 	kfree(h->ioaccel1_blockFetchTable);
6978e1f7de0cSMatt Gates 	return 1;
69796c311b57SStephen M. Cameron }
69806c311b57SStephen M. Cameron 
6981aca9012aSStephen M. Cameron static int ioaccel2_alloc_cmds_and_bft(struct ctlr_info *h)
6982aca9012aSStephen M. Cameron {
6983aca9012aSStephen M. Cameron 	/* Allocate ioaccel2 mode command blocks and block fetch table */
6984aca9012aSStephen M. Cameron 
6985aca9012aSStephen M. Cameron 	h->ioaccel_maxsg =
6986aca9012aSStephen M. Cameron 		readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
6987aca9012aSStephen M. Cameron 	if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
6988aca9012aSStephen M. Cameron 		h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
6989aca9012aSStephen M. Cameron 
6990aca9012aSStephen M. Cameron #define IOACCEL2_COMMANDLIST_ALIGNMENT 128
6991aca9012aSStephen M. Cameron 	BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
6992aca9012aSStephen M. Cameron 			IOACCEL2_COMMANDLIST_ALIGNMENT);
6993aca9012aSStephen M. Cameron 	h->ioaccel2_cmd_pool =
6994aca9012aSStephen M. Cameron 		pci_alloc_consistent(h->pdev,
6995aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
6996aca9012aSStephen M. Cameron 			&(h->ioaccel2_cmd_pool_dhandle));
6997aca9012aSStephen M. Cameron 
6998aca9012aSStephen M. Cameron 	h->ioaccel2_blockFetchTable =
6999aca9012aSStephen M. Cameron 		kmalloc(((h->ioaccel_maxsg + 1) *
7000aca9012aSStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
7001aca9012aSStephen M. Cameron 
7002aca9012aSStephen M. Cameron 	if ((h->ioaccel2_cmd_pool == NULL) ||
7003aca9012aSStephen M. Cameron 		(h->ioaccel2_blockFetchTable == NULL))
7004aca9012aSStephen M. Cameron 		goto clean_up;
7005aca9012aSStephen M. Cameron 
7006aca9012aSStephen M. Cameron 	memset(h->ioaccel2_cmd_pool, 0,
7007aca9012aSStephen M. Cameron 		h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
7008aca9012aSStephen M. Cameron 	return 0;
7009aca9012aSStephen M. Cameron 
7010aca9012aSStephen M. Cameron clean_up:
7011aca9012aSStephen M. Cameron 	if (h->ioaccel2_cmd_pool)
7012aca9012aSStephen M. Cameron 		pci_free_consistent(h->pdev,
7013aca9012aSStephen M. Cameron 			h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
7014aca9012aSStephen M. Cameron 			h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
7015aca9012aSStephen M. Cameron 	kfree(h->ioaccel2_blockFetchTable);
7016aca9012aSStephen M. Cameron 	return 1;
7017aca9012aSStephen M. Cameron }
7018aca9012aSStephen M. Cameron 
70196f039790SGreg Kroah-Hartman static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
70206c311b57SStephen M. Cameron {
70216c311b57SStephen M. Cameron 	u32 trans_support;
7022e1f7de0cSMatt Gates 	unsigned long transMethod = CFGTBL_Trans_Performant |
7023e1f7de0cSMatt Gates 					CFGTBL_Trans_use_short_tags;
7024254f796bSMatt Gates 	int i;
70256c311b57SStephen M. Cameron 
702602ec19c8SStephen M. Cameron 	if (hpsa_simple_mode)
702702ec19c8SStephen M. Cameron 		return;
702802ec19c8SStephen M. Cameron 
7029e1f7de0cSMatt Gates 	/* Check for I/O accelerator mode support */
7030e1f7de0cSMatt Gates 	if (trans_support & CFGTBL_Trans_io_accel1) {
7031e1f7de0cSMatt Gates 		transMethod |= CFGTBL_Trans_io_accel1 |
7032e1f7de0cSMatt Gates 				CFGTBL_Trans_enable_directed_msix;
7033e1f7de0cSMatt Gates 		if (hpsa_alloc_ioaccel_cmd_and_bft(h))
7034e1f7de0cSMatt Gates 			goto clean_up;
7035aca9012aSStephen M. Cameron 	} else {
7036aca9012aSStephen M. Cameron 		if (trans_support & CFGTBL_Trans_io_accel2) {
7037aca9012aSStephen M. Cameron 				transMethod |= CFGTBL_Trans_io_accel2 |
7038aca9012aSStephen M. Cameron 				CFGTBL_Trans_enable_directed_msix;
7039aca9012aSStephen M. Cameron 		if (ioaccel2_alloc_cmds_and_bft(h))
7040aca9012aSStephen M. Cameron 			goto clean_up;
7041aca9012aSStephen M. Cameron 		}
7042e1f7de0cSMatt Gates 	}
7043e1f7de0cSMatt Gates 
7044e1f7de0cSMatt Gates 	/* TODO, check that this next line h->nreply_queues is correct */
70456c311b57SStephen M. Cameron 	trans_support = readl(&(h->cfgtable->TransportSupport));
70466c311b57SStephen M. Cameron 	if (!(trans_support & PERFORMANT_MODE))
70476c311b57SStephen M. Cameron 		return;
70486c311b57SStephen M. Cameron 
7049eee0f03aSHannes Reinecke 	h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
7050cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
70516c311b57SStephen M. Cameron 	/* Performant mode ring buffer and supporting data structures */
7052254f796bSMatt Gates 	h->reply_pool_size = h->max_commands * sizeof(u64) * h->nreply_queues;
70536c311b57SStephen M. Cameron 	h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size,
70546c311b57SStephen M. Cameron 				&(h->reply_pool_dhandle));
70556c311b57SStephen M. Cameron 
7056254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
7057254f796bSMatt Gates 		h->reply_queue[i].head = &h->reply_pool[h->max_commands * i];
7058254f796bSMatt Gates 		h->reply_queue[i].size = h->max_commands;
7059254f796bSMatt Gates 		h->reply_queue[i].wraparound = 1;  /* spec: init to 1 */
7060254f796bSMatt Gates 		h->reply_queue[i].current_entry = 0;
7061254f796bSMatt Gates 	}
7062254f796bSMatt Gates 
70636c311b57SStephen M. Cameron 	/* Need a block fetch table for performant mode */
7064d66ae08bSStephen M. Cameron 	h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
70656c311b57SStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
70666c311b57SStephen M. Cameron 
70676c311b57SStephen M. Cameron 	if ((h->reply_pool == NULL)
70686c311b57SStephen M. Cameron 		|| (h->blockFetchTable == NULL))
70696c311b57SStephen M. Cameron 		goto clean_up;
70706c311b57SStephen M. Cameron 
7071e1f7de0cSMatt Gates 	hpsa_enter_performant_mode(h, trans_support);
7072303932fdSDon Brace 	return;
7073303932fdSDon Brace 
7074303932fdSDon Brace clean_up:
7075303932fdSDon Brace 	if (h->reply_pool)
7076303932fdSDon Brace 		pci_free_consistent(h->pdev, h->reply_pool_size,
7077303932fdSDon Brace 			h->reply_pool, h->reply_pool_dhandle);
7078303932fdSDon Brace 	kfree(h->blockFetchTable);
7079303932fdSDon Brace }
7080303932fdSDon Brace 
708176438d08SStephen M. Cameron static void hpsa_drain_commands(struct ctlr_info *h)
708276438d08SStephen M. Cameron {
708376438d08SStephen M. Cameron 	int cmds_out;
708476438d08SStephen M. Cameron 	unsigned long flags;
708576438d08SStephen M. Cameron 
708676438d08SStephen M. Cameron 	do { /* wait for all outstanding commands to drain out */
708776438d08SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
708876438d08SStephen M. Cameron 		cmds_out = h->commands_outstanding;
708976438d08SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
709076438d08SStephen M. Cameron 		if (cmds_out <= 0)
709176438d08SStephen M. Cameron 			break;
709276438d08SStephen M. Cameron 		msleep(100);
709376438d08SStephen M. Cameron 	} while (1);
709476438d08SStephen M. Cameron }
709576438d08SStephen M. Cameron 
7096edd16368SStephen M. Cameron /*
7097edd16368SStephen M. Cameron  *  This is it.  Register the PCI driver information for the cards we control
7098edd16368SStephen M. Cameron  *  the OS will call our registered routines when it finds one of our cards.
7099edd16368SStephen M. Cameron  */
7100edd16368SStephen M. Cameron static int __init hpsa_init(void)
7101edd16368SStephen M. Cameron {
710231468401SMike Miller 	return pci_register_driver(&hpsa_pci_driver);
7103edd16368SStephen M. Cameron }
7104edd16368SStephen M. Cameron 
7105edd16368SStephen M. Cameron static void __exit hpsa_cleanup(void)
7106edd16368SStephen M. Cameron {
7107edd16368SStephen M. Cameron 	pci_unregister_driver(&hpsa_pci_driver);
7108edd16368SStephen M. Cameron }
7109edd16368SStephen M. Cameron 
7110e1f7de0cSMatt Gates static void __attribute__((unused)) verify_offsets(void)
7111e1f7de0cSMatt Gates {
7112e1f7de0cSMatt Gates #define VERIFY_OFFSET(member, offset) \
7113*dd0e19f3SScott Teel 	BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
7114*dd0e19f3SScott Teel 
7115*dd0e19f3SScott Teel 	VERIFY_OFFSET(structure_size, 0);
7116*dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_size, 4);
7117*dd0e19f3SScott Teel 	VERIFY_OFFSET(volume_blk_cnt, 8);
7118*dd0e19f3SScott Teel 	VERIFY_OFFSET(phys_blk_shift, 16);
7119*dd0e19f3SScott Teel 	VERIFY_OFFSET(parity_rotation_shift, 17);
7120*dd0e19f3SScott Teel 	VERIFY_OFFSET(strip_size, 18);
7121*dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_starting_blk, 20);
7122*dd0e19f3SScott Teel 	VERIFY_OFFSET(disk_blk_cnt, 28);
7123*dd0e19f3SScott Teel 	VERIFY_OFFSET(data_disks_per_row, 36);
7124*dd0e19f3SScott Teel 	VERIFY_OFFSET(metadata_disks_per_row, 38);
7125*dd0e19f3SScott Teel 	VERIFY_OFFSET(row_cnt, 40);
7126*dd0e19f3SScott Teel 	VERIFY_OFFSET(layout_map_count, 42);
7127*dd0e19f3SScott Teel 	VERIFY_OFFSET(flags, 44);
7128*dd0e19f3SScott Teel 	VERIFY_OFFSET(dekindex, 46);
7129*dd0e19f3SScott Teel 	/* VERIFY_OFFSET(reserved, 48 */
7130*dd0e19f3SScott Teel 	VERIFY_OFFSET(data, 64);
7131*dd0e19f3SScott Teel 
7132*dd0e19f3SScott Teel #undef VERIFY_OFFSET
7133*dd0e19f3SScott Teel 
7134*dd0e19f3SScott Teel #define VERIFY_OFFSET(member, offset) \
7135b66cc250SMike Miller 	BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
7136b66cc250SMike Miller 
7137b66cc250SMike Miller 	VERIFY_OFFSET(IU_type, 0);
7138b66cc250SMike Miller 	VERIFY_OFFSET(direction, 1);
7139b66cc250SMike Miller 	VERIFY_OFFSET(reply_queue, 2);
7140b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved1, 3);  */
7141b66cc250SMike Miller 	VERIFY_OFFSET(scsi_nexus, 4);
7142b66cc250SMike Miller 	VERIFY_OFFSET(Tag, 8);
7143b66cc250SMike Miller 	VERIFY_OFFSET(cdb, 16);
7144b66cc250SMike Miller 	VERIFY_OFFSET(cciss_lun, 32);
7145b66cc250SMike Miller 	VERIFY_OFFSET(data_len, 40);
7146b66cc250SMike Miller 	VERIFY_OFFSET(cmd_priority_task_attr, 44);
7147b66cc250SMike Miller 	VERIFY_OFFSET(sg_count, 45);
7148b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved3 */
7149b66cc250SMike Miller 	VERIFY_OFFSET(err_ptr, 48);
7150b66cc250SMike Miller 	VERIFY_OFFSET(err_len, 56);
7151b66cc250SMike Miller 	/* VERIFY_OFFSET(reserved4  */
7152b66cc250SMike Miller 	VERIFY_OFFSET(sg, 64);
7153b66cc250SMike Miller 
7154b66cc250SMike Miller #undef VERIFY_OFFSET
7155b66cc250SMike Miller 
7156b66cc250SMike Miller #define VERIFY_OFFSET(member, offset) \
7157e1f7de0cSMatt Gates 	BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
7158e1f7de0cSMatt Gates 
7159e1f7de0cSMatt Gates 	VERIFY_OFFSET(dev_handle, 0x00);
7160e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved1, 0x02);
7161e1f7de0cSMatt Gates 	VERIFY_OFFSET(function, 0x03);
7162e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved2, 0x04);
7163e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info, 0x0C);
7164e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved3, 0x10);
7165e1f7de0cSMatt Gates 	VERIFY_OFFSET(err_info_len, 0x12);
7166e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved4, 0x13);
7167e1f7de0cSMatt Gates 	VERIFY_OFFSET(sgl_offset, 0x14);
7168e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved5, 0x15);
7169e1f7de0cSMatt Gates 	VERIFY_OFFSET(transfer_len, 0x1C);
7170e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved6, 0x20);
7171e1f7de0cSMatt Gates 	VERIFY_OFFSET(io_flags, 0x24);
7172e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved7, 0x26);
7173e1f7de0cSMatt Gates 	VERIFY_OFFSET(LUN, 0x34);
7174e1f7de0cSMatt Gates 	VERIFY_OFFSET(control, 0x3C);
7175e1f7de0cSMatt Gates 	VERIFY_OFFSET(CDB, 0x40);
7176e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved8, 0x50);
7177e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_context_flags, 0x60);
7178e1f7de0cSMatt Gates 	VERIFY_OFFSET(timeout_sec, 0x62);
7179e1f7de0cSMatt Gates 	VERIFY_OFFSET(ReplyQueue, 0x64);
7180e1f7de0cSMatt Gates 	VERIFY_OFFSET(reserved9, 0x65);
7181e1f7de0cSMatt Gates 	VERIFY_OFFSET(Tag, 0x68);
7182e1f7de0cSMatt Gates 	VERIFY_OFFSET(host_addr, 0x70);
7183e1f7de0cSMatt Gates 	VERIFY_OFFSET(CISS_LUN, 0x78);
7184e1f7de0cSMatt Gates 	VERIFY_OFFSET(SG, 0x78 + 8);
7185e1f7de0cSMatt Gates #undef VERIFY_OFFSET
7186e1f7de0cSMatt Gates }
7187e1f7de0cSMatt Gates 
7188edd16368SStephen M. Cameron module_init(hpsa_init);
7189edd16368SStephen M. Cameron module_exit(hpsa_cleanup);
7190