xref: /openbmc/linux/drivers/scsi/hpsa.c (revision 88bf6d62db4393fa03a58bada9d746312d5b496f)
1edd16368SStephen M. Cameron /*
2edd16368SStephen M. Cameron  *    Disk Array driver for HP Smart Array SAS controllers
3edd16368SStephen M. Cameron  *    Copyright 2000, 2009 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/seq_file.h>
33edd16368SStephen M. Cameron #include <linux/init.h>
34edd16368SStephen M. Cameron #include <linux/spinlock.h>
35edd16368SStephen M. Cameron #include <linux/compat.h>
36edd16368SStephen M. Cameron #include <linux/blktrace_api.h>
37edd16368SStephen M. Cameron #include <linux/uaccess.h>
38edd16368SStephen M. Cameron #include <linux/io.h>
39edd16368SStephen M. Cameron #include <linux/dma-mapping.h>
40edd16368SStephen M. Cameron #include <linux/completion.h>
41edd16368SStephen M. Cameron #include <linux/moduleparam.h>
42edd16368SStephen M. Cameron #include <scsi/scsi.h>
43edd16368SStephen M. Cameron #include <scsi/scsi_cmnd.h>
44edd16368SStephen M. Cameron #include <scsi/scsi_device.h>
45edd16368SStephen M. Cameron #include <scsi/scsi_host.h>
46667e23d4SStephen M. Cameron #include <scsi/scsi_tcq.h>
47edd16368SStephen M. Cameron #include <linux/cciss_ioctl.h>
48edd16368SStephen M. Cameron #include <linux/string.h>
49edd16368SStephen M. Cameron #include <linux/bitmap.h>
5060063497SArun Sharma #include <linux/atomic.h>
51edd16368SStephen M. Cameron #include <linux/kthread.h>
52a0c12413SStephen M. Cameron #include <linux/jiffies.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},
99163dbcd8SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x334D},
1009143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3354},
1019143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3355},
1029143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3356},
103fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1921},
104fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1922},
105fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1923},
106fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1924},
107fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1925},
108fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1926},
109fe0c9610SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1928},
11097b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSH,     0x103C, 0x1929},
11197b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BD},
11297b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BE},
11397b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21BF},
11497b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C0},
11597b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C1},
11697b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C2},
11797b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C3},
11897b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C4},
11997b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C5},
12097b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C7},
12197b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C8},
12297b9f53dSMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSI,     0x103C, 0x21C9},
123edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_ANY_ID,	PCI_ANY_ID, PCI_ANY_ID,
124edd16368SStephen M. Cameron 		PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
125edd16368SStephen M. Cameron 	{0,}
126edd16368SStephen M. Cameron };
127edd16368SStephen M. Cameron 
128edd16368SStephen M. Cameron MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
129edd16368SStephen M. Cameron 
130edd16368SStephen M. Cameron /*  board_id = Subsystem Device ID & Vendor ID
131edd16368SStephen M. Cameron  *  product = Marketing Name for the board
132edd16368SStephen M. Cameron  *  access = Address of the struct of function pointers
133edd16368SStephen M. Cameron  */
134edd16368SStephen M. Cameron static struct board_type products[] = {
135edd16368SStephen M. Cameron 	{0x3241103C, "Smart Array P212", &SA5_access},
136edd16368SStephen M. Cameron 	{0x3243103C, "Smart Array P410", &SA5_access},
137edd16368SStephen M. Cameron 	{0x3245103C, "Smart Array P410i", &SA5_access},
138edd16368SStephen M. Cameron 	{0x3247103C, "Smart Array P411", &SA5_access},
139edd16368SStephen M. Cameron 	{0x3249103C, "Smart Array P812", &SA5_access},
140163dbcd8SMike Miller 	{0x324A103C, "Smart Array P712m", &SA5_access},
141163dbcd8SMike Miller 	{0x324B103C, "Smart Array P711m", &SA5_access},
142fe0c9610SMike Miller 	{0x3350103C, "Smart Array P222", &SA5_access},
143fe0c9610SMike Miller 	{0x3351103C, "Smart Array P420", &SA5_access},
144fe0c9610SMike Miller 	{0x3352103C, "Smart Array P421", &SA5_access},
145fe0c9610SMike Miller 	{0x3353103C, "Smart Array P822", &SA5_access},
146163dbcd8SMike Miller 	{0x334D103C, "Smart Array P822se", &SA5_access},
147fe0c9610SMike Miller 	{0x3354103C, "Smart Array P420i", &SA5_access},
148fe0c9610SMike Miller 	{0x3355103C, "Smart Array P220i", &SA5_access},
149fe0c9610SMike Miller 	{0x3356103C, "Smart Array P721m", &SA5_access},
1501fd6c8e3SMike Miller 	{0x1921103C, "Smart Array P830i", &SA5_access},
1511fd6c8e3SMike Miller 	{0x1922103C, "Smart Array P430", &SA5_access},
1521fd6c8e3SMike Miller 	{0x1923103C, "Smart Array P431", &SA5_access},
1531fd6c8e3SMike Miller 	{0x1924103C, "Smart Array P830", &SA5_access},
1541fd6c8e3SMike Miller 	{0x1926103C, "Smart Array P731m", &SA5_access},
1551fd6c8e3SMike Miller 	{0x1928103C, "Smart Array P230i", &SA5_access},
1561fd6c8e3SMike Miller 	{0x1929103C, "Smart Array P530", &SA5_access},
15797b9f53dSMike Miller 	{0x21BD103C, "Smart Array", &SA5_access},
15897b9f53dSMike Miller 	{0x21BE103C, "Smart Array", &SA5_access},
15997b9f53dSMike Miller 	{0x21BF103C, "Smart Array", &SA5_access},
16097b9f53dSMike Miller 	{0x21C0103C, "Smart Array", &SA5_access},
16197b9f53dSMike Miller 	{0x21C1103C, "Smart Array", &SA5_access},
16297b9f53dSMike Miller 	{0x21C2103C, "Smart Array", &SA5_access},
16397b9f53dSMike Miller 	{0x21C3103C, "Smart Array", &SA5_access},
16497b9f53dSMike Miller 	{0x21C4103C, "Smart Array", &SA5_access},
16597b9f53dSMike Miller 	{0x21C5103C, "Smart Array", &SA5_access},
16697b9f53dSMike Miller 	{0x21C7103C, "Smart Array", &SA5_access},
16797b9f53dSMike Miller 	{0x21C8103C, "Smart Array", &SA5_access},
16897b9f53dSMike Miller 	{0x21C9103C, "Smart Array", &SA5_access},
169edd16368SStephen M. Cameron 	{0xFFFF103C, "Unknown Smart Array", &SA5_access},
170edd16368SStephen M. Cameron };
171edd16368SStephen M. Cameron 
172edd16368SStephen M. Cameron static int number_of_controllers;
173edd16368SStephen M. Cameron 
174a0c12413SStephen M. Cameron static struct list_head hpsa_ctlr_list = LIST_HEAD_INIT(hpsa_ctlr_list);
175a0c12413SStephen M. Cameron static spinlock_t lockup_detector_lock;
176a0c12413SStephen M. Cameron static struct task_struct *hpsa_lockup_detector;
177a0c12413SStephen M. Cameron 
17810f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id);
17910f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id);
180edd16368SStephen M. Cameron static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg);
181edd16368SStephen M. Cameron static void start_io(struct ctlr_info *h);
182edd16368SStephen M. Cameron 
183edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
184edd16368SStephen M. Cameron static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg);
185edd16368SStephen M. Cameron #endif
186edd16368SStephen M. Cameron 
187edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c);
188edd16368SStephen M. Cameron static void cmd_special_free(struct ctlr_info *h, struct CommandList *c);
189edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h);
190edd16368SStephen M. Cameron static struct CommandList *cmd_special_alloc(struct ctlr_info *h);
191a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
19201a02ffcSStephen M. Cameron 	void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
193edd16368SStephen M. Cameron 	int cmd_type);
194edd16368SStephen M. Cameron 
195f281233dSJeff Garzik static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
196a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *);
197a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
198a08a8471SStephen M. Cameron 	unsigned long elapsed_time);
199667e23d4SStephen M. Cameron static int hpsa_change_queue_depth(struct scsi_device *sdev,
200667e23d4SStephen M. Cameron 	int qdepth, int reason);
201edd16368SStephen M. Cameron 
202edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
20375167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *scsicmd);
204edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev);
205edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev);
206edd16368SStephen M. Cameron 
207edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
208edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
209edd16368SStephen M. Cameron 	struct CommandList *c);
210edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
211edd16368SStephen M. Cameron 	struct CommandList *c);
212303932fdSDon Brace /* performant mode helper functions */
213303932fdSDon Brace static void calc_bucket_map(int *bucket, int num_buckets,
214303932fdSDon Brace 	int nsgs, int *bucket_map);
2156f039790SGreg Kroah-Hartman static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
216254f796bSMatt Gates static inline u32 next_command(struct ctlr_info *h, u8 q);
2176f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
2186f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
2191df8552aSStephen M. Cameron 			       u64 *cfg_offset);
2206f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
2211df8552aSStephen M. Cameron 				    unsigned long *memory_bar);
2226f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
2236f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
2246f039790SGreg Kroah-Hartman 				     int wait_for_ready);
22575167d2cSStephen M. Cameron static inline void finish_cmd(struct CommandList *c);
226fe5389c8SStephen M. Cameron #define BOARD_NOT_READY 0
227fe5389c8SStephen M. Cameron #define BOARD_READY 1
228edd16368SStephen M. Cameron 
229edd16368SStephen M. Cameron static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
230edd16368SStephen M. Cameron {
231edd16368SStephen M. Cameron 	unsigned long *priv = shost_priv(sdev->host);
232edd16368SStephen M. Cameron 	return (struct ctlr_info *) *priv;
233edd16368SStephen M. Cameron }
234edd16368SStephen M. Cameron 
235a23513e8SStephen M. Cameron static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
236a23513e8SStephen M. Cameron {
237a23513e8SStephen M. Cameron 	unsigned long *priv = shost_priv(sh);
238a23513e8SStephen M. Cameron 	return (struct ctlr_info *) *priv;
239a23513e8SStephen M. Cameron }
240a23513e8SStephen M. Cameron 
241edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
242edd16368SStephen M. Cameron 	struct CommandList *c)
243edd16368SStephen M. Cameron {
244edd16368SStephen M. Cameron 	if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
245edd16368SStephen M. Cameron 		return 0;
246edd16368SStephen M. Cameron 
247edd16368SStephen M. Cameron 	switch (c->err_info->SenseInfo[12]) {
248edd16368SStephen M. Cameron 	case STATE_CHANGED:
249f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: a state change "
250edd16368SStephen M. Cameron 			"detected, command retried\n", h->ctlr);
251edd16368SStephen M. Cameron 		break;
252edd16368SStephen M. Cameron 	case LUN_FAILED:
253f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: LUN failure "
254edd16368SStephen M. Cameron 			"detected, action required\n", h->ctlr);
255edd16368SStephen M. Cameron 		break;
256edd16368SStephen M. Cameron 	case REPORT_LUNS_CHANGED:
257f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: report LUN data "
25831468401SMike Miller 			"changed, action required\n", h->ctlr);
259edd16368SStephen M. Cameron 	/*
2604f4eb9f1SScott Teel 	 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
2614f4eb9f1SScott Teel 	 * target (array) devices.
262edd16368SStephen M. Cameron 	 */
263edd16368SStephen M. Cameron 		break;
264edd16368SStephen M. Cameron 	case POWER_OR_RESET:
265f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: a power on "
266edd16368SStephen M. Cameron 			"or device reset detected\n", h->ctlr);
267edd16368SStephen M. Cameron 		break;
268edd16368SStephen M. Cameron 	case UNIT_ATTENTION_CLEARED:
269f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: unit attention "
270edd16368SStephen M. Cameron 		    "cleared by another initiator\n", h->ctlr);
271edd16368SStephen M. Cameron 		break;
272edd16368SStephen M. Cameron 	default:
273f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: unknown "
274edd16368SStephen M. Cameron 			"unit attention detected\n", h->ctlr);
275edd16368SStephen M. Cameron 		break;
276edd16368SStephen M. Cameron 	}
277edd16368SStephen M. Cameron 	return 1;
278edd16368SStephen M. Cameron }
279edd16368SStephen M. Cameron 
280852af20aSMatt Bondurant static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
281852af20aSMatt Bondurant {
282852af20aSMatt Bondurant 	if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
283852af20aSMatt Bondurant 		(c->err_info->ScsiStatus != SAM_STAT_BUSY &&
284852af20aSMatt Bondurant 		 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
285852af20aSMatt Bondurant 		return 0;
286852af20aSMatt Bondurant 	dev_warn(&h->pdev->dev, HPSA "device busy");
287852af20aSMatt Bondurant 	return 1;
288852af20aSMatt Bondurant }
289852af20aSMatt Bondurant 
290edd16368SStephen M. Cameron static ssize_t host_store_rescan(struct device *dev,
291edd16368SStephen M. Cameron 				 struct device_attribute *attr,
292edd16368SStephen M. Cameron 				 const char *buf, size_t count)
293edd16368SStephen M. Cameron {
294edd16368SStephen M. Cameron 	struct ctlr_info *h;
295edd16368SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
296a23513e8SStephen M. Cameron 	h = shost_to_hba(shost);
29731468401SMike Miller 	hpsa_scan_start(h->scsi_host);
298edd16368SStephen M. Cameron 	return count;
299edd16368SStephen M. Cameron }
300edd16368SStephen M. Cameron 
301d28ce020SStephen M. Cameron static ssize_t host_show_firmware_revision(struct device *dev,
302d28ce020SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
303d28ce020SStephen M. Cameron {
304d28ce020SStephen M. Cameron 	struct ctlr_info *h;
305d28ce020SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
306d28ce020SStephen M. Cameron 	unsigned char *fwrev;
307d28ce020SStephen M. Cameron 
308d28ce020SStephen M. Cameron 	h = shost_to_hba(shost);
309d28ce020SStephen M. Cameron 	if (!h->hba_inquiry_data)
310d28ce020SStephen M. Cameron 		return 0;
311d28ce020SStephen M. Cameron 	fwrev = &h->hba_inquiry_data[32];
312d28ce020SStephen M. Cameron 	return snprintf(buf, 20, "%c%c%c%c\n",
313d28ce020SStephen M. Cameron 		fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
314d28ce020SStephen M. Cameron }
315d28ce020SStephen M. Cameron 
31694a13649SStephen M. Cameron static ssize_t host_show_commands_outstanding(struct device *dev,
31794a13649SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
31894a13649SStephen M. Cameron {
31994a13649SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
32094a13649SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(shost);
32194a13649SStephen M. Cameron 
32294a13649SStephen M. Cameron 	return snprintf(buf, 20, "%d\n", h->commands_outstanding);
32394a13649SStephen M. Cameron }
32494a13649SStephen M. Cameron 
325745a7a25SStephen M. Cameron static ssize_t host_show_transport_mode(struct device *dev,
326745a7a25SStephen M. Cameron 	struct device_attribute *attr, char *buf)
327745a7a25SStephen M. Cameron {
328745a7a25SStephen M. Cameron 	struct ctlr_info *h;
329745a7a25SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
330745a7a25SStephen M. Cameron 
331745a7a25SStephen M. Cameron 	h = shost_to_hba(shost);
332745a7a25SStephen M. Cameron 	return snprintf(buf, 20, "%s\n",
333960a30e7SStephen M. Cameron 		h->transMethod & CFGTBL_Trans_Performant ?
334745a7a25SStephen M. Cameron 			"performant" : "simple");
335745a7a25SStephen M. Cameron }
336745a7a25SStephen M. Cameron 
33746380786SStephen M. Cameron /* List of controllers which cannot be hard reset on kexec with reset_devices */
338941b1cdaSStephen M. Cameron static u32 unresettable_controller[] = {
339941b1cdaSStephen M. Cameron 	0x324a103C, /* Smart Array P712m */
340941b1cdaSStephen M. Cameron 	0x324b103C, /* SmartArray P711m */
341941b1cdaSStephen M. Cameron 	0x3223103C, /* Smart Array P800 */
342941b1cdaSStephen M. Cameron 	0x3234103C, /* Smart Array P400 */
343941b1cdaSStephen M. Cameron 	0x3235103C, /* Smart Array P400i */
344941b1cdaSStephen M. Cameron 	0x3211103C, /* Smart Array E200i */
345941b1cdaSStephen M. Cameron 	0x3212103C, /* Smart Array E200 */
346941b1cdaSStephen M. Cameron 	0x3213103C, /* Smart Array E200i */
347941b1cdaSStephen M. Cameron 	0x3214103C, /* Smart Array E200i */
348941b1cdaSStephen M. Cameron 	0x3215103C, /* Smart Array E200i */
349941b1cdaSStephen M. Cameron 	0x3237103C, /* Smart Array E500 */
350941b1cdaSStephen M. Cameron 	0x323D103C, /* Smart Array P700m */
3517af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
352941b1cdaSStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
353941b1cdaSStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
3545a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
3555a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
3565a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
3575a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
3585a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
3595a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
360941b1cdaSStephen M. Cameron };
361941b1cdaSStephen M. Cameron 
36246380786SStephen M. Cameron /* List of controllers which cannot even be soft reset */
36346380786SStephen M. Cameron static u32 soft_unresettable_controller[] = {
3647af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
3655a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
3665a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
3675a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
3685a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
3695a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
3705a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
37146380786SStephen M. Cameron 	/* Exclude 640x boards.  These are two pci devices in one slot
37246380786SStephen M. Cameron 	 * which share a battery backed cache module.  One controls the
37346380786SStephen M. Cameron 	 * cache, the other accesses the cache through the one that controls
37446380786SStephen M. Cameron 	 * it.  If we reset the one controlling the cache, the other will
37546380786SStephen M. Cameron 	 * likely not be happy.  Just forbid resetting this conjoined mess.
37646380786SStephen M. Cameron 	 * The 640x isn't really supported by hpsa anyway.
37746380786SStephen M. Cameron 	 */
37846380786SStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
37946380786SStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
38046380786SStephen M. Cameron };
38146380786SStephen M. Cameron 
38246380786SStephen M. Cameron static int ctlr_is_hard_resettable(u32 board_id)
383941b1cdaSStephen M. Cameron {
384941b1cdaSStephen M. Cameron 	int i;
385941b1cdaSStephen M. Cameron 
386941b1cdaSStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(unresettable_controller); i++)
38746380786SStephen M. Cameron 		if (unresettable_controller[i] == board_id)
388941b1cdaSStephen M. Cameron 			return 0;
389941b1cdaSStephen M. Cameron 	return 1;
390941b1cdaSStephen M. Cameron }
391941b1cdaSStephen M. Cameron 
39246380786SStephen M. Cameron static int ctlr_is_soft_resettable(u32 board_id)
39346380786SStephen M. Cameron {
39446380786SStephen M. Cameron 	int i;
39546380786SStephen M. Cameron 
39646380786SStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(soft_unresettable_controller); i++)
39746380786SStephen M. Cameron 		if (soft_unresettable_controller[i] == board_id)
39846380786SStephen M. Cameron 			return 0;
39946380786SStephen M. Cameron 	return 1;
40046380786SStephen M. Cameron }
40146380786SStephen M. Cameron 
40246380786SStephen M. Cameron static int ctlr_is_resettable(u32 board_id)
40346380786SStephen M. Cameron {
40446380786SStephen M. Cameron 	return ctlr_is_hard_resettable(board_id) ||
40546380786SStephen M. Cameron 		ctlr_is_soft_resettable(board_id);
40646380786SStephen M. Cameron }
40746380786SStephen M. Cameron 
408941b1cdaSStephen M. Cameron static ssize_t host_show_resettable(struct device *dev,
409941b1cdaSStephen M. Cameron 	struct device_attribute *attr, char *buf)
410941b1cdaSStephen M. Cameron {
411941b1cdaSStephen M. Cameron 	struct ctlr_info *h;
412941b1cdaSStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
413941b1cdaSStephen M. Cameron 
414941b1cdaSStephen M. Cameron 	h = shost_to_hba(shost);
41546380786SStephen M. Cameron 	return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
416941b1cdaSStephen M. Cameron }
417941b1cdaSStephen M. Cameron 
418edd16368SStephen M. Cameron static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
419edd16368SStephen M. Cameron {
420edd16368SStephen M. Cameron 	return (scsi3addr[3] & 0xC0) == 0x40;
421edd16368SStephen M. Cameron }
422edd16368SStephen M. Cameron 
423edd16368SStephen M. Cameron static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
424d82357eaSMike Miller 	"1(ADM)", "UNKNOWN"
425edd16368SStephen M. Cameron };
426edd16368SStephen M. Cameron #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
427edd16368SStephen M. Cameron 
428edd16368SStephen M. Cameron static ssize_t raid_level_show(struct device *dev,
429edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
430edd16368SStephen M. Cameron {
431edd16368SStephen M. Cameron 	ssize_t l = 0;
43282a72c0aSStephen M. Cameron 	unsigned char rlevel;
433edd16368SStephen M. Cameron 	struct ctlr_info *h;
434edd16368SStephen M. Cameron 	struct scsi_device *sdev;
435edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
436edd16368SStephen M. Cameron 	unsigned long flags;
437edd16368SStephen M. Cameron 
438edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
439edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
440edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
441edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
442edd16368SStephen M. Cameron 	if (!hdev) {
443edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
444edd16368SStephen M. Cameron 		return -ENODEV;
445edd16368SStephen M. Cameron 	}
446edd16368SStephen M. Cameron 
447edd16368SStephen M. Cameron 	/* Is this even a logical drive? */
448edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
449edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
450edd16368SStephen M. Cameron 		l = snprintf(buf, PAGE_SIZE, "N/A\n");
451edd16368SStephen M. Cameron 		return l;
452edd16368SStephen M. Cameron 	}
453edd16368SStephen M. Cameron 
454edd16368SStephen M. Cameron 	rlevel = hdev->raid_level;
455edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
45682a72c0aSStephen M. Cameron 	if (rlevel > RAID_UNKNOWN)
457edd16368SStephen M. Cameron 		rlevel = RAID_UNKNOWN;
458edd16368SStephen M. Cameron 	l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
459edd16368SStephen M. Cameron 	return l;
460edd16368SStephen M. Cameron }
461edd16368SStephen M. Cameron 
462edd16368SStephen M. Cameron static ssize_t lunid_show(struct device *dev,
463edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
464edd16368SStephen M. Cameron {
465edd16368SStephen M. Cameron 	struct ctlr_info *h;
466edd16368SStephen M. Cameron 	struct scsi_device *sdev;
467edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
468edd16368SStephen M. Cameron 	unsigned long flags;
469edd16368SStephen M. Cameron 	unsigned char lunid[8];
470edd16368SStephen M. Cameron 
471edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
472edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
473edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
474edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
475edd16368SStephen M. Cameron 	if (!hdev) {
476edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
477edd16368SStephen M. Cameron 		return -ENODEV;
478edd16368SStephen M. Cameron 	}
479edd16368SStephen M. Cameron 	memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
480edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
481edd16368SStephen M. Cameron 	return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
482edd16368SStephen M. Cameron 		lunid[0], lunid[1], lunid[2], lunid[3],
483edd16368SStephen M. Cameron 		lunid[4], lunid[5], lunid[6], lunid[7]);
484edd16368SStephen M. Cameron }
485edd16368SStephen M. Cameron 
486edd16368SStephen M. Cameron static ssize_t unique_id_show(struct device *dev,
487edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
488edd16368SStephen M. Cameron {
489edd16368SStephen M. Cameron 	struct ctlr_info *h;
490edd16368SStephen M. Cameron 	struct scsi_device *sdev;
491edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
492edd16368SStephen M. Cameron 	unsigned long flags;
493edd16368SStephen M. Cameron 	unsigned char sn[16];
494edd16368SStephen M. Cameron 
495edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
496edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
497edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
498edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
499edd16368SStephen M. Cameron 	if (!hdev) {
500edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
501edd16368SStephen M. Cameron 		return -ENODEV;
502edd16368SStephen M. Cameron 	}
503edd16368SStephen M. Cameron 	memcpy(sn, hdev->device_id, sizeof(sn));
504edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
505edd16368SStephen M. Cameron 	return snprintf(buf, 16 * 2 + 2,
506edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X"
507edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X\n",
508edd16368SStephen M. Cameron 			sn[0], sn[1], sn[2], sn[3],
509edd16368SStephen M. Cameron 			sn[4], sn[5], sn[6], sn[7],
510edd16368SStephen M. Cameron 			sn[8], sn[9], sn[10], sn[11],
511edd16368SStephen M. Cameron 			sn[12], sn[13], sn[14], sn[15]);
512edd16368SStephen M. Cameron }
513edd16368SStephen M. Cameron 
5143f5eac3aSStephen M. Cameron static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
5153f5eac3aSStephen M. Cameron static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
5163f5eac3aSStephen M. Cameron static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
5173f5eac3aSStephen M. Cameron static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
5183f5eac3aSStephen M. Cameron static DEVICE_ATTR(firmware_revision, S_IRUGO,
5193f5eac3aSStephen M. Cameron 	host_show_firmware_revision, NULL);
5203f5eac3aSStephen M. Cameron static DEVICE_ATTR(commands_outstanding, S_IRUGO,
5213f5eac3aSStephen M. Cameron 	host_show_commands_outstanding, NULL);
5223f5eac3aSStephen M. Cameron static DEVICE_ATTR(transport_mode, S_IRUGO,
5233f5eac3aSStephen M. Cameron 	host_show_transport_mode, NULL);
524941b1cdaSStephen M. Cameron static DEVICE_ATTR(resettable, S_IRUGO,
525941b1cdaSStephen M. Cameron 	host_show_resettable, NULL);
5263f5eac3aSStephen M. Cameron 
5273f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_sdev_attrs[] = {
5283f5eac3aSStephen M. Cameron 	&dev_attr_raid_level,
5293f5eac3aSStephen M. Cameron 	&dev_attr_lunid,
5303f5eac3aSStephen M. Cameron 	&dev_attr_unique_id,
5313f5eac3aSStephen M. Cameron 	NULL,
5323f5eac3aSStephen M. Cameron };
5333f5eac3aSStephen M. Cameron 
5343f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_shost_attrs[] = {
5353f5eac3aSStephen M. Cameron 	&dev_attr_rescan,
5363f5eac3aSStephen M. Cameron 	&dev_attr_firmware_revision,
5373f5eac3aSStephen M. Cameron 	&dev_attr_commands_outstanding,
5383f5eac3aSStephen M. Cameron 	&dev_attr_transport_mode,
539941b1cdaSStephen M. Cameron 	&dev_attr_resettable,
5403f5eac3aSStephen M. Cameron 	NULL,
5413f5eac3aSStephen M. Cameron };
5423f5eac3aSStephen M. Cameron 
5433f5eac3aSStephen M. Cameron static struct scsi_host_template hpsa_driver_template = {
5443f5eac3aSStephen M. Cameron 	.module			= THIS_MODULE,
545f79cfec6SStephen M. Cameron 	.name			= HPSA,
546f79cfec6SStephen M. Cameron 	.proc_name		= HPSA,
5473f5eac3aSStephen M. Cameron 	.queuecommand		= hpsa_scsi_queue_command,
5483f5eac3aSStephen M. Cameron 	.scan_start		= hpsa_scan_start,
5493f5eac3aSStephen M. Cameron 	.scan_finished		= hpsa_scan_finished,
5503f5eac3aSStephen M. Cameron 	.change_queue_depth	= hpsa_change_queue_depth,
5513f5eac3aSStephen M. Cameron 	.this_id		= -1,
5523f5eac3aSStephen M. Cameron 	.use_clustering		= ENABLE_CLUSTERING,
55375167d2cSStephen M. Cameron 	.eh_abort_handler	= hpsa_eh_abort_handler,
5543f5eac3aSStephen M. Cameron 	.eh_device_reset_handler = hpsa_eh_device_reset_handler,
5553f5eac3aSStephen M. Cameron 	.ioctl			= hpsa_ioctl,
5563f5eac3aSStephen M. Cameron 	.slave_alloc		= hpsa_slave_alloc,
5573f5eac3aSStephen M. Cameron 	.slave_destroy		= hpsa_slave_destroy,
5583f5eac3aSStephen M. Cameron #ifdef CONFIG_COMPAT
5593f5eac3aSStephen M. Cameron 	.compat_ioctl		= hpsa_compat_ioctl,
5603f5eac3aSStephen M. Cameron #endif
5613f5eac3aSStephen M. Cameron 	.sdev_attrs = hpsa_sdev_attrs,
5623f5eac3aSStephen M. Cameron 	.shost_attrs = hpsa_shost_attrs,
563c0d6a4d1SStephen M. Cameron 	.max_sectors = 8192,
56454b2b50cSMartin K. Petersen 	.no_write_same = 1,
5653f5eac3aSStephen M. Cameron };
5663f5eac3aSStephen M. Cameron 
5673f5eac3aSStephen M. Cameron 
5683f5eac3aSStephen M. Cameron /* Enqueuing and dequeuing functions for cmdlists. */
5693f5eac3aSStephen M. Cameron static inline void addQ(struct list_head *list, struct CommandList *c)
5703f5eac3aSStephen M. Cameron {
5713f5eac3aSStephen M. Cameron 	list_add_tail(&c->list, list);
5723f5eac3aSStephen M. Cameron }
5733f5eac3aSStephen M. Cameron 
574254f796bSMatt Gates static inline u32 next_command(struct ctlr_info *h, u8 q)
5753f5eac3aSStephen M. Cameron {
5763f5eac3aSStephen M. Cameron 	u32 a;
577254f796bSMatt Gates 	struct reply_pool *rq = &h->reply_queue[q];
578e16a33adSMatt Gates 	unsigned long flags;
5793f5eac3aSStephen M. Cameron 
5803f5eac3aSStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
581254f796bSMatt Gates 		return h->access.command_completed(h, q);
5823f5eac3aSStephen M. Cameron 
583254f796bSMatt Gates 	if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
584254f796bSMatt Gates 		a = rq->head[rq->current_entry];
585254f796bSMatt Gates 		rq->current_entry++;
586e16a33adSMatt Gates 		spin_lock_irqsave(&h->lock, flags);
5873f5eac3aSStephen M. Cameron 		h->commands_outstanding--;
588e16a33adSMatt Gates 		spin_unlock_irqrestore(&h->lock, flags);
5893f5eac3aSStephen M. Cameron 	} else {
5903f5eac3aSStephen M. Cameron 		a = FIFO_EMPTY;
5913f5eac3aSStephen M. Cameron 	}
5923f5eac3aSStephen M. Cameron 	/* Check for wraparound */
593254f796bSMatt Gates 	if (rq->current_entry == h->max_commands) {
594254f796bSMatt Gates 		rq->current_entry = 0;
595254f796bSMatt Gates 		rq->wraparound ^= 1;
5963f5eac3aSStephen M. Cameron 	}
5973f5eac3aSStephen M. Cameron 	return a;
5983f5eac3aSStephen M. Cameron }
5993f5eac3aSStephen M. Cameron 
6003f5eac3aSStephen M. Cameron /* set_performant_mode: Modify the tag for cciss performant
6013f5eac3aSStephen M. Cameron  * set bit 0 for pull model, bits 3-1 for block fetch
6023f5eac3aSStephen M. Cameron  * register number
6033f5eac3aSStephen M. Cameron  */
6043f5eac3aSStephen M. Cameron static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
6053f5eac3aSStephen M. Cameron {
606254f796bSMatt Gates 	if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
6073f5eac3aSStephen M. Cameron 		c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
608254f796bSMatt Gates 		if (likely(h->msix_vector))
609254f796bSMatt Gates 			c->Header.ReplyQueue =
610804a5cb5SJohn Kacur 				raw_smp_processor_id() % h->nreply_queues;
611254f796bSMatt Gates 	}
6123f5eac3aSStephen M. Cameron }
6133f5eac3aSStephen M. Cameron 
614e85c5974SStephen M. Cameron static int is_firmware_flash_cmd(u8 *cdb)
615e85c5974SStephen M. Cameron {
616e85c5974SStephen M. Cameron 	return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
617e85c5974SStephen M. Cameron }
618e85c5974SStephen M. Cameron 
619e85c5974SStephen M. Cameron /*
620e85c5974SStephen M. Cameron  * During firmware flash, the heartbeat register may not update as frequently
621e85c5974SStephen M. Cameron  * as it should.  So we dial down lockup detection during firmware flash. and
622e85c5974SStephen M. Cameron  * dial it back up when firmware flash completes.
623e85c5974SStephen M. Cameron  */
624e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
625e85c5974SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
626e85c5974SStephen M. Cameron static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
627e85c5974SStephen M. Cameron 		struct CommandList *c)
628e85c5974SStephen M. Cameron {
629e85c5974SStephen M. Cameron 	if (!is_firmware_flash_cmd(c->Request.CDB))
630e85c5974SStephen M. Cameron 		return;
631e85c5974SStephen M. Cameron 	atomic_inc(&h->firmware_flash_in_progress);
632e85c5974SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
633e85c5974SStephen M. Cameron }
634e85c5974SStephen M. Cameron 
635e85c5974SStephen M. Cameron static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
636e85c5974SStephen M. Cameron 		struct CommandList *c)
637e85c5974SStephen M. Cameron {
638e85c5974SStephen M. Cameron 	if (is_firmware_flash_cmd(c->Request.CDB) &&
639e85c5974SStephen M. Cameron 		atomic_dec_and_test(&h->firmware_flash_in_progress))
640e85c5974SStephen M. Cameron 		h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
641e85c5974SStephen M. Cameron }
642e85c5974SStephen M. Cameron 
6433f5eac3aSStephen M. Cameron static void enqueue_cmd_and_start_io(struct ctlr_info *h,
6443f5eac3aSStephen M. Cameron 	struct CommandList *c)
6453f5eac3aSStephen M. Cameron {
6463f5eac3aSStephen M. Cameron 	unsigned long flags;
6473f5eac3aSStephen M. Cameron 
6483f5eac3aSStephen M. Cameron 	set_performant_mode(h, c);
649e85c5974SStephen M. Cameron 	dial_down_lockup_detection_during_fw_flash(h, c);
6503f5eac3aSStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
6513f5eac3aSStephen M. Cameron 	addQ(&h->reqQ, c);
6523f5eac3aSStephen M. Cameron 	h->Qdepth++;
6533f5eac3aSStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
654e16a33adSMatt Gates 	start_io(h);
6553f5eac3aSStephen M. Cameron }
6563f5eac3aSStephen M. Cameron 
6573f5eac3aSStephen M. Cameron static inline void removeQ(struct CommandList *c)
6583f5eac3aSStephen M. Cameron {
6593f5eac3aSStephen M. Cameron 	if (WARN_ON(list_empty(&c->list)))
6603f5eac3aSStephen M. Cameron 		return;
6613f5eac3aSStephen M. Cameron 	list_del_init(&c->list);
6623f5eac3aSStephen M. Cameron }
6633f5eac3aSStephen M. Cameron 
6643f5eac3aSStephen M. Cameron static inline int is_hba_lunid(unsigned char scsi3addr[])
6653f5eac3aSStephen M. Cameron {
6663f5eac3aSStephen M. Cameron 	return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
6673f5eac3aSStephen M. Cameron }
6683f5eac3aSStephen M. Cameron 
6693f5eac3aSStephen M. Cameron static inline int is_scsi_rev_5(struct ctlr_info *h)
6703f5eac3aSStephen M. Cameron {
6713f5eac3aSStephen M. Cameron 	if (!h->hba_inquiry_data)
6723f5eac3aSStephen M. Cameron 		return 0;
6733f5eac3aSStephen M. Cameron 	if ((h->hba_inquiry_data[2] & 0x07) == 5)
6743f5eac3aSStephen M. Cameron 		return 1;
6753f5eac3aSStephen M. Cameron 	return 0;
6763f5eac3aSStephen M. Cameron }
6773f5eac3aSStephen M. Cameron 
678edd16368SStephen M. Cameron static int hpsa_find_target_lun(struct ctlr_info *h,
679edd16368SStephen M. Cameron 	unsigned char scsi3addr[], int bus, int *target, int *lun)
680edd16368SStephen M. Cameron {
681edd16368SStephen M. Cameron 	/* finds an unused bus, target, lun for a new physical device
682edd16368SStephen M. Cameron 	 * assumes h->devlock is held
683edd16368SStephen M. Cameron 	 */
684edd16368SStephen M. Cameron 	int i, found = 0;
685cfe5badcSScott Teel 	DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
686edd16368SStephen M. Cameron 
687263d9401SAkinobu Mita 	bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
688edd16368SStephen M. Cameron 
689edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
690edd16368SStephen M. Cameron 		if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
691263d9401SAkinobu Mita 			__set_bit(h->dev[i]->target, lun_taken);
692edd16368SStephen M. Cameron 	}
693edd16368SStephen M. Cameron 
694263d9401SAkinobu Mita 	i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
695263d9401SAkinobu Mita 	if (i < HPSA_MAX_DEVICES) {
696edd16368SStephen M. Cameron 		/* *bus = 1; */
697edd16368SStephen M. Cameron 		*target = i;
698edd16368SStephen M. Cameron 		*lun = 0;
699edd16368SStephen M. Cameron 		found = 1;
700edd16368SStephen M. Cameron 	}
701edd16368SStephen M. Cameron 	return !found;
702edd16368SStephen M. Cameron }
703edd16368SStephen M. Cameron 
704edd16368SStephen M. Cameron /* Add an entry into h->dev[] array. */
705edd16368SStephen M. Cameron static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
706edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *device,
707edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *added[], int *nadded)
708edd16368SStephen M. Cameron {
709edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
710edd16368SStephen M. Cameron 	int n = h->ndevices;
711edd16368SStephen M. Cameron 	int i;
712edd16368SStephen M. Cameron 	unsigned char addr1[8], addr2[8];
713edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
714edd16368SStephen M. Cameron 
715cfe5badcSScott Teel 	if (n >= HPSA_MAX_DEVICES) {
716edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "too many devices, some will be "
717edd16368SStephen M. Cameron 			"inaccessible.\n");
718edd16368SStephen M. Cameron 		return -1;
719edd16368SStephen M. Cameron 	}
720edd16368SStephen M. Cameron 
721edd16368SStephen M. Cameron 	/* physical devices do not have lun or target assigned until now. */
722edd16368SStephen M. Cameron 	if (device->lun != -1)
723edd16368SStephen M. Cameron 		/* Logical device, lun is already assigned. */
724edd16368SStephen M. Cameron 		goto lun_assigned;
725edd16368SStephen M. Cameron 
726edd16368SStephen M. Cameron 	/* If this device a non-zero lun of a multi-lun device
727edd16368SStephen M. Cameron 	 * byte 4 of the 8-byte LUN addr will contain the logical
728edd16368SStephen M. Cameron 	 * unit no, zero otherise.
729edd16368SStephen M. Cameron 	 */
730edd16368SStephen M. Cameron 	if (device->scsi3addr[4] == 0) {
731edd16368SStephen M. Cameron 		/* This is not a non-zero lun of a multi-lun device */
732edd16368SStephen M. Cameron 		if (hpsa_find_target_lun(h, device->scsi3addr,
733edd16368SStephen M. Cameron 			device->bus, &device->target, &device->lun) != 0)
734edd16368SStephen M. Cameron 			return -1;
735edd16368SStephen M. Cameron 		goto lun_assigned;
736edd16368SStephen M. Cameron 	}
737edd16368SStephen M. Cameron 
738edd16368SStephen M. Cameron 	/* This is a non-zero lun of a multi-lun device.
739edd16368SStephen M. Cameron 	 * Search through our list and find the device which
740edd16368SStephen M. Cameron 	 * has the same 8 byte LUN address, excepting byte 4.
741edd16368SStephen M. Cameron 	 * Assign the same bus and target for this new LUN.
742edd16368SStephen M. Cameron 	 * Use the logical unit number from the firmware.
743edd16368SStephen M. Cameron 	 */
744edd16368SStephen M. Cameron 	memcpy(addr1, device->scsi3addr, 8);
745edd16368SStephen M. Cameron 	addr1[4] = 0;
746edd16368SStephen M. Cameron 	for (i = 0; i < n; i++) {
747edd16368SStephen M. Cameron 		sd = h->dev[i];
748edd16368SStephen M. Cameron 		memcpy(addr2, sd->scsi3addr, 8);
749edd16368SStephen M. Cameron 		addr2[4] = 0;
750edd16368SStephen M. Cameron 		/* differ only in byte 4? */
751edd16368SStephen M. Cameron 		if (memcmp(addr1, addr2, 8) == 0) {
752edd16368SStephen M. Cameron 			device->bus = sd->bus;
753edd16368SStephen M. Cameron 			device->target = sd->target;
754edd16368SStephen M. Cameron 			device->lun = device->scsi3addr[4];
755edd16368SStephen M. Cameron 			break;
756edd16368SStephen M. Cameron 		}
757edd16368SStephen M. Cameron 	}
758edd16368SStephen M. Cameron 	if (device->lun == -1) {
759edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
760edd16368SStephen M. Cameron 			" suspect firmware bug or unsupported hardware "
761edd16368SStephen M. Cameron 			"configuration.\n");
762edd16368SStephen M. Cameron 			return -1;
763edd16368SStephen M. Cameron 	}
764edd16368SStephen M. Cameron 
765edd16368SStephen M. Cameron lun_assigned:
766edd16368SStephen M. Cameron 
767edd16368SStephen M. Cameron 	h->dev[n] = device;
768edd16368SStephen M. Cameron 	h->ndevices++;
769edd16368SStephen M. Cameron 	added[*nadded] = device;
770edd16368SStephen M. Cameron 	(*nadded)++;
771edd16368SStephen M. Cameron 
772edd16368SStephen M. Cameron 	/* initially, (before registering with scsi layer) we don't
773edd16368SStephen M. Cameron 	 * know our hostno and we don't want to print anything first
774edd16368SStephen M. Cameron 	 * time anyway (the scsi layer's inquiries will show that info)
775edd16368SStephen M. Cameron 	 */
776edd16368SStephen M. Cameron 	/* if (hostno != -1) */
777edd16368SStephen M. Cameron 		dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
778edd16368SStephen M. Cameron 			scsi_device_type(device->devtype), hostno,
779edd16368SStephen M. Cameron 			device->bus, device->target, device->lun);
780edd16368SStephen M. Cameron 	return 0;
781edd16368SStephen M. Cameron }
782edd16368SStephen M. Cameron 
783bd9244f7SScott Teel /* Update an entry in h->dev[] array. */
784bd9244f7SScott Teel static void hpsa_scsi_update_entry(struct ctlr_info *h, int hostno,
785bd9244f7SScott Teel 	int entry, struct hpsa_scsi_dev_t *new_entry)
786bd9244f7SScott Teel {
787bd9244f7SScott Teel 	/* assumes h->devlock is held */
788bd9244f7SScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
789bd9244f7SScott Teel 
790bd9244f7SScott Teel 	/* Raid level changed. */
791bd9244f7SScott Teel 	h->dev[entry]->raid_level = new_entry->raid_level;
792bd9244f7SScott Teel 	dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d updated.\n",
793bd9244f7SScott Teel 		scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
794bd9244f7SScott Teel 		new_entry->target, new_entry->lun);
795bd9244f7SScott Teel }
796bd9244f7SScott Teel 
7972a8ccf31SStephen M. Cameron /* Replace an entry from h->dev[] array. */
7982a8ccf31SStephen M. Cameron static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
7992a8ccf31SStephen M. Cameron 	int entry, struct hpsa_scsi_dev_t *new_entry,
8002a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *added[], int *nadded,
8012a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
8022a8ccf31SStephen M. Cameron {
8032a8ccf31SStephen M. Cameron 	/* assumes h->devlock is held */
804cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
8052a8ccf31SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
8062a8ccf31SStephen M. Cameron 	(*nremoved)++;
80701350d05SStephen M. Cameron 
80801350d05SStephen M. Cameron 	/*
80901350d05SStephen M. Cameron 	 * New physical devices won't have target/lun assigned yet
81001350d05SStephen M. Cameron 	 * so we need to preserve the values in the slot we are replacing.
81101350d05SStephen M. Cameron 	 */
81201350d05SStephen M. Cameron 	if (new_entry->target == -1) {
81301350d05SStephen M. Cameron 		new_entry->target = h->dev[entry]->target;
81401350d05SStephen M. Cameron 		new_entry->lun = h->dev[entry]->lun;
81501350d05SStephen M. Cameron 	}
81601350d05SStephen M. Cameron 
8172a8ccf31SStephen M. Cameron 	h->dev[entry] = new_entry;
8182a8ccf31SStephen M. Cameron 	added[*nadded] = new_entry;
8192a8ccf31SStephen M. Cameron 	(*nadded)++;
8202a8ccf31SStephen M. Cameron 	dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n",
8212a8ccf31SStephen M. Cameron 		scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
8222a8ccf31SStephen M. Cameron 			new_entry->target, new_entry->lun);
8232a8ccf31SStephen M. Cameron }
8242a8ccf31SStephen M. Cameron 
825edd16368SStephen M. Cameron /* Remove an entry from h->dev[] array. */
826edd16368SStephen M. Cameron static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
827edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
828edd16368SStephen M. Cameron {
829edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
830edd16368SStephen M. Cameron 	int i;
831edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
832edd16368SStephen M. Cameron 
833cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
834edd16368SStephen M. Cameron 
835edd16368SStephen M. Cameron 	sd = h->dev[entry];
836edd16368SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
837edd16368SStephen M. Cameron 	(*nremoved)++;
838edd16368SStephen M. Cameron 
839edd16368SStephen M. Cameron 	for (i = entry; i < h->ndevices-1; i++)
840edd16368SStephen M. Cameron 		h->dev[i] = h->dev[i+1];
841edd16368SStephen M. Cameron 	h->ndevices--;
842edd16368SStephen M. Cameron 	dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
843edd16368SStephen M. Cameron 		scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
844edd16368SStephen M. Cameron 		sd->lun);
845edd16368SStephen M. Cameron }
846edd16368SStephen M. Cameron 
847edd16368SStephen M. Cameron #define SCSI3ADDR_EQ(a, b) ( \
848edd16368SStephen M. Cameron 	(a)[7] == (b)[7] && \
849edd16368SStephen M. Cameron 	(a)[6] == (b)[6] && \
850edd16368SStephen M. Cameron 	(a)[5] == (b)[5] && \
851edd16368SStephen M. Cameron 	(a)[4] == (b)[4] && \
852edd16368SStephen M. Cameron 	(a)[3] == (b)[3] && \
853edd16368SStephen M. Cameron 	(a)[2] == (b)[2] && \
854edd16368SStephen M. Cameron 	(a)[1] == (b)[1] && \
855edd16368SStephen M. Cameron 	(a)[0] == (b)[0])
856edd16368SStephen M. Cameron 
857edd16368SStephen M. Cameron static void fixup_botched_add(struct ctlr_info *h,
858edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *added)
859edd16368SStephen M. Cameron {
860edd16368SStephen M. Cameron 	/* called when scsi_add_device fails in order to re-adjust
861edd16368SStephen M. Cameron 	 * h->dev[] to match the mid layer's view.
862edd16368SStephen M. Cameron 	 */
863edd16368SStephen M. Cameron 	unsigned long flags;
864edd16368SStephen M. Cameron 	int i, j;
865edd16368SStephen M. Cameron 
866edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
867edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
868edd16368SStephen M. Cameron 		if (h->dev[i] == added) {
869edd16368SStephen M. Cameron 			for (j = i; j < h->ndevices-1; j++)
870edd16368SStephen M. Cameron 				h->dev[j] = h->dev[j+1];
871edd16368SStephen M. Cameron 			h->ndevices--;
872edd16368SStephen M. Cameron 			break;
873edd16368SStephen M. Cameron 		}
874edd16368SStephen M. Cameron 	}
875edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
876edd16368SStephen M. Cameron 	kfree(added);
877edd16368SStephen M. Cameron }
878edd16368SStephen M. Cameron 
879edd16368SStephen M. Cameron static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
880edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev2)
881edd16368SStephen M. Cameron {
882edd16368SStephen M. Cameron 	/* we compare everything except lun and target as these
883edd16368SStephen M. Cameron 	 * are not yet assigned.  Compare parts likely
884edd16368SStephen M. Cameron 	 * to differ first
885edd16368SStephen M. Cameron 	 */
886edd16368SStephen M. Cameron 	if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
887edd16368SStephen M. Cameron 		sizeof(dev1->scsi3addr)) != 0)
888edd16368SStephen M. Cameron 		return 0;
889edd16368SStephen M. Cameron 	if (memcmp(dev1->device_id, dev2->device_id,
890edd16368SStephen M. Cameron 		sizeof(dev1->device_id)) != 0)
891edd16368SStephen M. Cameron 		return 0;
892edd16368SStephen M. Cameron 	if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
893edd16368SStephen M. Cameron 		return 0;
894edd16368SStephen M. Cameron 	if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
895edd16368SStephen M. Cameron 		return 0;
896edd16368SStephen M. Cameron 	if (dev1->devtype != dev2->devtype)
897edd16368SStephen M. Cameron 		return 0;
898edd16368SStephen M. Cameron 	if (dev1->bus != dev2->bus)
899edd16368SStephen M. Cameron 		return 0;
900edd16368SStephen M. Cameron 	return 1;
901edd16368SStephen M. Cameron }
902edd16368SStephen M. Cameron 
903bd9244f7SScott Teel static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
904bd9244f7SScott Teel 	struct hpsa_scsi_dev_t *dev2)
905bd9244f7SScott Teel {
906bd9244f7SScott Teel 	/* Device attributes that can change, but don't mean
907bd9244f7SScott Teel 	 * that the device is a different device, nor that the OS
908bd9244f7SScott Teel 	 * needs to be told anything about the change.
909bd9244f7SScott Teel 	 */
910bd9244f7SScott Teel 	if (dev1->raid_level != dev2->raid_level)
911bd9244f7SScott Teel 		return 1;
912bd9244f7SScott Teel 	return 0;
913bd9244f7SScott Teel }
914bd9244f7SScott Teel 
915edd16368SStephen M. Cameron /* Find needle in haystack.  If exact match found, return DEVICE_SAME,
916edd16368SStephen M. Cameron  * and return needle location in *index.  If scsi3addr matches, but not
917edd16368SStephen M. Cameron  * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
918bd9244f7SScott Teel  * location in *index.
919bd9244f7SScott Teel  * In the case of a minor device attribute change, such as RAID level, just
920bd9244f7SScott Teel  * return DEVICE_UPDATED, along with the updated device's location in index.
921bd9244f7SScott Teel  * If needle not found, return DEVICE_NOT_FOUND.
922edd16368SStephen M. Cameron  */
923edd16368SStephen M. Cameron static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
924edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *haystack[], int haystack_size,
925edd16368SStephen M. Cameron 	int *index)
926edd16368SStephen M. Cameron {
927edd16368SStephen M. Cameron 	int i;
928edd16368SStephen M. Cameron #define DEVICE_NOT_FOUND 0
929edd16368SStephen M. Cameron #define DEVICE_CHANGED 1
930edd16368SStephen M. Cameron #define DEVICE_SAME 2
931bd9244f7SScott Teel #define DEVICE_UPDATED 3
932edd16368SStephen M. Cameron 	for (i = 0; i < haystack_size; i++) {
93323231048SStephen M. Cameron 		if (haystack[i] == NULL) /* previously removed. */
93423231048SStephen M. Cameron 			continue;
935edd16368SStephen M. Cameron 		if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
936edd16368SStephen M. Cameron 			*index = i;
937bd9244f7SScott Teel 			if (device_is_the_same(needle, haystack[i])) {
938bd9244f7SScott Teel 				if (device_updated(needle, haystack[i]))
939bd9244f7SScott Teel 					return DEVICE_UPDATED;
940edd16368SStephen M. Cameron 				return DEVICE_SAME;
941bd9244f7SScott Teel 			} else {
942edd16368SStephen M. Cameron 				return DEVICE_CHANGED;
943edd16368SStephen M. Cameron 			}
944edd16368SStephen M. Cameron 		}
945bd9244f7SScott Teel 	}
946edd16368SStephen M. Cameron 	*index = -1;
947edd16368SStephen M. Cameron 	return DEVICE_NOT_FOUND;
948edd16368SStephen M. Cameron }
949edd16368SStephen M. Cameron 
9504967bd3eSStephen M. Cameron static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
951edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd[], int nsds)
952edd16368SStephen M. Cameron {
953edd16368SStephen M. Cameron 	/* sd contains scsi3 addresses and devtypes, and inquiry
954edd16368SStephen M. Cameron 	 * data.  This function takes what's in sd to be the current
955edd16368SStephen M. Cameron 	 * reality and updates h->dev[] to reflect that reality.
956edd16368SStephen M. Cameron 	 */
957edd16368SStephen M. Cameron 	int i, entry, device_change, changes = 0;
958edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *csd;
959edd16368SStephen M. Cameron 	unsigned long flags;
960edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **added, **removed;
961edd16368SStephen M. Cameron 	int nadded, nremoved;
962edd16368SStephen M. Cameron 	struct Scsi_Host *sh = NULL;
963edd16368SStephen M. Cameron 
964cfe5badcSScott Teel 	added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
965cfe5badcSScott Teel 	removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
966edd16368SStephen M. Cameron 
967edd16368SStephen M. Cameron 	if (!added || !removed) {
968edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in "
969edd16368SStephen M. Cameron 			"adjust_hpsa_scsi_table\n");
970edd16368SStephen M. Cameron 		goto free_and_out;
971edd16368SStephen M. Cameron 	}
972edd16368SStephen M. Cameron 
973edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
974edd16368SStephen M. Cameron 
975edd16368SStephen M. Cameron 	/* find any devices in h->dev[] that are not in
976edd16368SStephen M. Cameron 	 * sd[] and remove them from h->dev[], and for any
977edd16368SStephen M. Cameron 	 * devices which have changed, remove the old device
978edd16368SStephen M. Cameron 	 * info and add the new device info.
979bd9244f7SScott Teel 	 * If minor device attributes change, just update
980bd9244f7SScott Teel 	 * the existing device structure.
981edd16368SStephen M. Cameron 	 */
982edd16368SStephen M. Cameron 	i = 0;
983edd16368SStephen M. Cameron 	nremoved = 0;
984edd16368SStephen M. Cameron 	nadded = 0;
985edd16368SStephen M. Cameron 	while (i < h->ndevices) {
986edd16368SStephen M. Cameron 		csd = h->dev[i];
987edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
988edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
989edd16368SStephen M. Cameron 			changes++;
990edd16368SStephen M. Cameron 			hpsa_scsi_remove_entry(h, hostno, i,
991edd16368SStephen M. Cameron 				removed, &nremoved);
992edd16368SStephen M. Cameron 			continue; /* remove ^^^, hence i not incremented */
993edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
994edd16368SStephen M. Cameron 			changes++;
9952a8ccf31SStephen M. Cameron 			hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
9962a8ccf31SStephen M. Cameron 				added, &nadded, removed, &nremoved);
997c7f172dcSStephen M. Cameron 			/* Set it to NULL to prevent it from being freed
998c7f172dcSStephen M. Cameron 			 * at the bottom of hpsa_update_scsi_devices()
999c7f172dcSStephen M. Cameron 			 */
1000c7f172dcSStephen M. Cameron 			sd[entry] = NULL;
1001bd9244f7SScott Teel 		} else if (device_change == DEVICE_UPDATED) {
1002bd9244f7SScott Teel 			hpsa_scsi_update_entry(h, hostno, i, sd[entry]);
1003edd16368SStephen M. Cameron 		}
1004edd16368SStephen M. Cameron 		i++;
1005edd16368SStephen M. Cameron 	}
1006edd16368SStephen M. Cameron 
1007edd16368SStephen M. Cameron 	/* Now, make sure every device listed in sd[] is also
1008edd16368SStephen M. Cameron 	 * listed in h->dev[], adding them if they aren't found
1009edd16368SStephen M. Cameron 	 */
1010edd16368SStephen M. Cameron 
1011edd16368SStephen M. Cameron 	for (i = 0; i < nsds; i++) {
1012edd16368SStephen M. Cameron 		if (!sd[i]) /* if already added above. */
1013edd16368SStephen M. Cameron 			continue;
1014edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1015edd16368SStephen M. Cameron 					h->ndevices, &entry);
1016edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
1017edd16368SStephen M. Cameron 			changes++;
1018edd16368SStephen M. Cameron 			if (hpsa_scsi_add_entry(h, hostno, sd[i],
1019edd16368SStephen M. Cameron 				added, &nadded) != 0)
1020edd16368SStephen M. Cameron 				break;
1021edd16368SStephen M. Cameron 			sd[i] = NULL; /* prevent from being freed later. */
1022edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
1023edd16368SStephen M. Cameron 			/* should never happen... */
1024edd16368SStephen M. Cameron 			changes++;
1025edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev,
1026edd16368SStephen M. Cameron 				"device unexpectedly changed.\n");
1027edd16368SStephen M. Cameron 			/* but if it does happen, we just ignore that device */
1028edd16368SStephen M. Cameron 		}
1029edd16368SStephen M. Cameron 	}
1030edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1031edd16368SStephen M. Cameron 
1032edd16368SStephen M. Cameron 	/* Don't notify scsi mid layer of any changes the first time through
1033edd16368SStephen M. Cameron 	 * (or if there are no changes) scsi_scan_host will do it later the
1034edd16368SStephen M. Cameron 	 * first time through.
1035edd16368SStephen M. Cameron 	 */
1036edd16368SStephen M. Cameron 	if (hostno == -1 || !changes)
1037edd16368SStephen M. Cameron 		goto free_and_out;
1038edd16368SStephen M. Cameron 
1039edd16368SStephen M. Cameron 	sh = h->scsi_host;
1040edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any removed devices */
1041edd16368SStephen M. Cameron 	for (i = 0; i < nremoved; i++) {
1042edd16368SStephen M. Cameron 		struct scsi_device *sdev =
1043edd16368SStephen M. Cameron 			scsi_device_lookup(sh, removed[i]->bus,
1044edd16368SStephen M. Cameron 				removed[i]->target, removed[i]->lun);
1045edd16368SStephen M. Cameron 		if (sdev != NULL) {
1046edd16368SStephen M. Cameron 			scsi_remove_device(sdev);
1047edd16368SStephen M. Cameron 			scsi_device_put(sdev);
1048edd16368SStephen M. Cameron 		} else {
1049edd16368SStephen M. Cameron 			/* We don't expect to get here.
1050edd16368SStephen M. Cameron 			 * future cmds to this device will get selection
1051edd16368SStephen M. Cameron 			 * timeout as if the device was gone.
1052edd16368SStephen M. Cameron 			 */
1053edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
1054edd16368SStephen M. Cameron 				" for removal.", hostno, removed[i]->bus,
1055edd16368SStephen M. Cameron 				removed[i]->target, removed[i]->lun);
1056edd16368SStephen M. Cameron 		}
1057edd16368SStephen M. Cameron 		kfree(removed[i]);
1058edd16368SStephen M. Cameron 		removed[i] = NULL;
1059edd16368SStephen M. Cameron 	}
1060edd16368SStephen M. Cameron 
1061edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any added devices */
1062edd16368SStephen M. Cameron 	for (i = 0; i < nadded; i++) {
1063edd16368SStephen M. Cameron 		if (scsi_add_device(sh, added[i]->bus,
1064edd16368SStephen M. Cameron 			added[i]->target, added[i]->lun) == 0)
1065edd16368SStephen M. Cameron 			continue;
1066edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
1067edd16368SStephen M. Cameron 			"device not added.\n", hostno, added[i]->bus,
1068edd16368SStephen M. Cameron 			added[i]->target, added[i]->lun);
1069edd16368SStephen M. Cameron 		/* now we have to remove it from h->dev,
1070edd16368SStephen M. Cameron 		 * since it didn't get added to scsi mid layer
1071edd16368SStephen M. Cameron 		 */
1072edd16368SStephen M. Cameron 		fixup_botched_add(h, added[i]);
1073edd16368SStephen M. Cameron 	}
1074edd16368SStephen M. Cameron 
1075edd16368SStephen M. Cameron free_and_out:
1076edd16368SStephen M. Cameron 	kfree(added);
1077edd16368SStephen M. Cameron 	kfree(removed);
1078edd16368SStephen M. Cameron }
1079edd16368SStephen M. Cameron 
1080edd16368SStephen M. Cameron /*
10819e03aa2fSJoe Perches  * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
1082edd16368SStephen M. Cameron  * Assume's h->devlock is held.
1083edd16368SStephen M. Cameron  */
1084edd16368SStephen M. Cameron static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1085edd16368SStephen M. Cameron 	int bus, int target, int lun)
1086edd16368SStephen M. Cameron {
1087edd16368SStephen M. Cameron 	int i;
1088edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1089edd16368SStephen M. Cameron 
1090edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1091edd16368SStephen M. Cameron 		sd = h->dev[i];
1092edd16368SStephen M. Cameron 		if (sd->bus == bus && sd->target == target && sd->lun == lun)
1093edd16368SStephen M. Cameron 			return sd;
1094edd16368SStephen M. Cameron 	}
1095edd16368SStephen M. Cameron 	return NULL;
1096edd16368SStephen M. Cameron }
1097edd16368SStephen M. Cameron 
1098edd16368SStephen M. Cameron /* link sdev->hostdata to our per-device structure. */
1099edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev)
1100edd16368SStephen M. Cameron {
1101edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1102edd16368SStephen M. Cameron 	unsigned long flags;
1103edd16368SStephen M. Cameron 	struct ctlr_info *h;
1104edd16368SStephen M. Cameron 
1105edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
1106edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1107edd16368SStephen M. Cameron 	sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1108edd16368SStephen M. Cameron 		sdev_id(sdev), sdev->lun);
1109edd16368SStephen M. Cameron 	if (sd != NULL)
1110edd16368SStephen M. Cameron 		sdev->hostdata = sd;
1111edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1112edd16368SStephen M. Cameron 	return 0;
1113edd16368SStephen M. Cameron }
1114edd16368SStephen M. Cameron 
1115edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev)
1116edd16368SStephen M. Cameron {
1117bcc44255SStephen M. Cameron 	/* nothing to do. */
1118edd16368SStephen M. Cameron }
1119edd16368SStephen M. Cameron 
112033a2ffceSStephen M. Cameron static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
112133a2ffceSStephen M. Cameron {
112233a2ffceSStephen M. Cameron 	int i;
112333a2ffceSStephen M. Cameron 
112433a2ffceSStephen M. Cameron 	if (!h->cmd_sg_list)
112533a2ffceSStephen M. Cameron 		return;
112633a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
112733a2ffceSStephen M. Cameron 		kfree(h->cmd_sg_list[i]);
112833a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = NULL;
112933a2ffceSStephen M. Cameron 	}
113033a2ffceSStephen M. Cameron 	kfree(h->cmd_sg_list);
113133a2ffceSStephen M. Cameron 	h->cmd_sg_list = NULL;
113233a2ffceSStephen M. Cameron }
113333a2ffceSStephen M. Cameron 
113433a2ffceSStephen M. Cameron static int hpsa_allocate_sg_chain_blocks(struct ctlr_info *h)
113533a2ffceSStephen M. Cameron {
113633a2ffceSStephen M. Cameron 	int i;
113733a2ffceSStephen M. Cameron 
113833a2ffceSStephen M. Cameron 	if (h->chainsize <= 0)
113933a2ffceSStephen M. Cameron 		return 0;
114033a2ffceSStephen M. Cameron 
114133a2ffceSStephen M. Cameron 	h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
114233a2ffceSStephen M. Cameron 				GFP_KERNEL);
114333a2ffceSStephen M. Cameron 	if (!h->cmd_sg_list)
114433a2ffceSStephen M. Cameron 		return -ENOMEM;
114533a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
114633a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
114733a2ffceSStephen M. Cameron 						h->chainsize, GFP_KERNEL);
114833a2ffceSStephen M. Cameron 		if (!h->cmd_sg_list[i])
114933a2ffceSStephen M. Cameron 			goto clean;
115033a2ffceSStephen M. Cameron 	}
115133a2ffceSStephen M. Cameron 	return 0;
115233a2ffceSStephen M. Cameron 
115333a2ffceSStephen M. Cameron clean:
115433a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
115533a2ffceSStephen M. Cameron 	return -ENOMEM;
115633a2ffceSStephen M. Cameron }
115733a2ffceSStephen M. Cameron 
1158e2bea6dfSStephen M. Cameron static int hpsa_map_sg_chain_block(struct ctlr_info *h,
115933a2ffceSStephen M. Cameron 	struct CommandList *c)
116033a2ffceSStephen M. Cameron {
116133a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg, *chain_block;
116233a2ffceSStephen M. Cameron 	u64 temp64;
116333a2ffceSStephen M. Cameron 
116433a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
116533a2ffceSStephen M. Cameron 	chain_block = h->cmd_sg_list[c->cmdindex];
116633a2ffceSStephen M. Cameron 	chain_sg->Ext = HPSA_SG_CHAIN;
116733a2ffceSStephen M. Cameron 	chain_sg->Len = sizeof(*chain_sg) *
116833a2ffceSStephen M. Cameron 		(c->Header.SGTotal - h->max_cmd_sg_entries);
116933a2ffceSStephen M. Cameron 	temp64 = pci_map_single(h->pdev, chain_block, chain_sg->Len,
117033a2ffceSStephen M. Cameron 				PCI_DMA_TODEVICE);
1171e2bea6dfSStephen M. Cameron 	if (dma_mapping_error(&h->pdev->dev, temp64)) {
1172e2bea6dfSStephen M. Cameron 		/* prevent subsequent unmapping */
1173e2bea6dfSStephen M. Cameron 		chain_sg->Addr.lower = 0;
1174e2bea6dfSStephen M. Cameron 		chain_sg->Addr.upper = 0;
1175e2bea6dfSStephen M. Cameron 		return -1;
1176e2bea6dfSStephen M. Cameron 	}
117733a2ffceSStephen M. Cameron 	chain_sg->Addr.lower = (u32) (temp64 & 0x0FFFFFFFFULL);
117833a2ffceSStephen M. Cameron 	chain_sg->Addr.upper = (u32) ((temp64 >> 32) & 0x0FFFFFFFFULL);
1179e2bea6dfSStephen M. Cameron 	return 0;
118033a2ffceSStephen M. Cameron }
118133a2ffceSStephen M. Cameron 
118233a2ffceSStephen M. Cameron static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
118333a2ffceSStephen M. Cameron 	struct CommandList *c)
118433a2ffceSStephen M. Cameron {
118533a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg;
118633a2ffceSStephen M. Cameron 	union u64bit temp64;
118733a2ffceSStephen M. Cameron 
118833a2ffceSStephen M. Cameron 	if (c->Header.SGTotal <= h->max_cmd_sg_entries)
118933a2ffceSStephen M. Cameron 		return;
119033a2ffceSStephen M. Cameron 
119133a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
119233a2ffceSStephen M. Cameron 	temp64.val32.lower = chain_sg->Addr.lower;
119333a2ffceSStephen M. Cameron 	temp64.val32.upper = chain_sg->Addr.upper;
119433a2ffceSStephen M. Cameron 	pci_unmap_single(h->pdev, temp64.val, chain_sg->Len, PCI_DMA_TODEVICE);
119533a2ffceSStephen M. Cameron }
119633a2ffceSStephen M. Cameron 
11971fb011fbSStephen M. Cameron static void complete_scsi_command(struct CommandList *cp)
1198edd16368SStephen M. Cameron {
1199edd16368SStephen M. Cameron 	struct scsi_cmnd *cmd;
1200edd16368SStephen M. Cameron 	struct ctlr_info *h;
1201edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
1202edd16368SStephen M. Cameron 
1203edd16368SStephen M. Cameron 	unsigned char sense_key;
1204edd16368SStephen M. Cameron 	unsigned char asc;      /* additional sense code */
1205edd16368SStephen M. Cameron 	unsigned char ascq;     /* additional sense code qualifier */
1206db111e18SStephen M. Cameron 	unsigned long sense_data_size;
1207edd16368SStephen M. Cameron 
1208edd16368SStephen M. Cameron 	ei = cp->err_info;
1209edd16368SStephen M. Cameron 	cmd = (struct scsi_cmnd *) cp->scsi_cmd;
1210edd16368SStephen M. Cameron 	h = cp->h;
1211edd16368SStephen M. Cameron 
1212edd16368SStephen M. Cameron 	scsi_dma_unmap(cmd); /* undo the DMA mappings */
121333a2ffceSStephen M. Cameron 	if (cp->Header.SGTotal > h->max_cmd_sg_entries)
121433a2ffceSStephen M. Cameron 		hpsa_unmap_sg_chain_block(h, cp);
1215edd16368SStephen M. Cameron 
1216edd16368SStephen M. Cameron 	cmd->result = (DID_OK << 16); 		/* host byte */
1217edd16368SStephen M. Cameron 	cmd->result |= (COMMAND_COMPLETE << 8);	/* msg byte */
12185512672fSStephen M. Cameron 	cmd->result |= ei->ScsiStatus;
1219edd16368SStephen M. Cameron 
1220edd16368SStephen M. Cameron 	/* copy the sense data whether we need to or not. */
1221db111e18SStephen M. Cameron 	if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
1222db111e18SStephen M. Cameron 		sense_data_size = SCSI_SENSE_BUFFERSIZE;
1223db111e18SStephen M. Cameron 	else
1224db111e18SStephen M. Cameron 		sense_data_size = sizeof(ei->SenseInfo);
1225db111e18SStephen M. Cameron 	if (ei->SenseLen < sense_data_size)
1226db111e18SStephen M. Cameron 		sense_data_size = ei->SenseLen;
1227db111e18SStephen M. Cameron 
1228db111e18SStephen M. Cameron 	memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
1229edd16368SStephen M. Cameron 	scsi_set_resid(cmd, ei->ResidualCnt);
1230edd16368SStephen M. Cameron 
1231edd16368SStephen M. Cameron 	if (ei->CommandStatus == 0) {
1232edd16368SStephen M. Cameron 		cmd_free(h, cp);
12332cc5bfafSTomas Henzl 		cmd->scsi_done(cmd);
1234edd16368SStephen M. Cameron 		return;
1235edd16368SStephen M. Cameron 	}
1236edd16368SStephen M. Cameron 
1237edd16368SStephen M. Cameron 	/* an error has occurred */
1238edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
1239edd16368SStephen M. Cameron 
1240edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
1241edd16368SStephen M. Cameron 		if (ei->ScsiStatus) {
1242edd16368SStephen M. Cameron 			/* Get sense key */
1243edd16368SStephen M. Cameron 			sense_key = 0xf & ei->SenseInfo[2];
1244edd16368SStephen M. Cameron 			/* Get additional sense code */
1245edd16368SStephen M. Cameron 			asc = ei->SenseInfo[12];
1246edd16368SStephen M. Cameron 			/* Get addition sense code qualifier */
1247edd16368SStephen M. Cameron 			ascq = ei->SenseInfo[13];
1248edd16368SStephen M. Cameron 		}
1249edd16368SStephen M. Cameron 
1250edd16368SStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
1251edd16368SStephen M. Cameron 			if (check_for_unit_attention(h, cp)) {
1252edd16368SStephen M. Cameron 				cmd->result = DID_SOFT_ERROR << 16;
1253edd16368SStephen M. Cameron 				break;
1254edd16368SStephen M. Cameron 			}
1255edd16368SStephen M. Cameron 			if (sense_key == ILLEGAL_REQUEST) {
1256edd16368SStephen M. Cameron 				/*
1257edd16368SStephen M. Cameron 				 * SCSI REPORT_LUNS is commonly unsupported on
1258edd16368SStephen M. Cameron 				 * Smart Array.  Suppress noisy complaint.
1259edd16368SStephen M. Cameron 				 */
1260edd16368SStephen M. Cameron 				if (cp->Request.CDB[0] == REPORT_LUNS)
1261edd16368SStephen M. Cameron 					break;
1262edd16368SStephen M. Cameron 
1263edd16368SStephen M. Cameron 				/* If ASC/ASCQ indicate Logical Unit
1264edd16368SStephen M. Cameron 				 * Not Supported condition,
1265edd16368SStephen M. Cameron 				 */
1266edd16368SStephen M. Cameron 				if ((asc == 0x25) && (ascq == 0x0)) {
1267edd16368SStephen M. Cameron 					dev_warn(&h->pdev->dev, "cp %p "
1268edd16368SStephen M. Cameron 						"has check condition\n", cp);
1269edd16368SStephen M. Cameron 					break;
1270edd16368SStephen M. Cameron 				}
1271edd16368SStephen M. Cameron 			}
1272edd16368SStephen M. Cameron 
1273edd16368SStephen M. Cameron 			if (sense_key == NOT_READY) {
1274edd16368SStephen M. Cameron 				/* If Sense is Not Ready, Logical Unit
1275edd16368SStephen M. Cameron 				 * Not ready, Manual Intervention
1276edd16368SStephen M. Cameron 				 * required
1277edd16368SStephen M. Cameron 				 */
1278edd16368SStephen M. Cameron 				if ((asc == 0x04) && (ascq == 0x03)) {
1279edd16368SStephen M. Cameron 					dev_warn(&h->pdev->dev, "cp %p "
1280edd16368SStephen M. Cameron 						"has check condition: unit "
1281edd16368SStephen M. Cameron 						"not ready, manual "
1282edd16368SStephen M. Cameron 						"intervention required\n", cp);
1283edd16368SStephen M. Cameron 					break;
1284edd16368SStephen M. Cameron 				}
1285edd16368SStephen M. Cameron 			}
12861d3b3609SMatt Gates 			if (sense_key == ABORTED_COMMAND) {
12871d3b3609SMatt Gates 				/* Aborted command is retryable */
12881d3b3609SMatt Gates 				dev_warn(&h->pdev->dev, "cp %p "
12891d3b3609SMatt Gates 					"has check condition: aborted command: "
12901d3b3609SMatt Gates 					"ASC: 0x%x, ASCQ: 0x%x\n",
12911d3b3609SMatt Gates 					cp, asc, ascq);
12922e311fbaSStephen M. Cameron 				cmd->result |= DID_SOFT_ERROR << 16;
12931d3b3609SMatt Gates 				break;
12941d3b3609SMatt Gates 			}
1295edd16368SStephen M. Cameron 			/* Must be some other type of check condition */
129621b8e4efSStephen M. Cameron 			dev_dbg(&h->pdev->dev, "cp %p has check condition: "
1297edd16368SStephen M. Cameron 					"unknown type: "
1298edd16368SStephen M. Cameron 					"Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1299edd16368SStephen M. Cameron 					"Returning result: 0x%x, "
1300edd16368SStephen M. Cameron 					"cmd=[%02x %02x %02x %02x %02x "
1301807be732SMike Miller 					"%02x %02x %02x %02x %02x %02x "
1302edd16368SStephen M. Cameron 					"%02x %02x %02x %02x %02x]\n",
1303edd16368SStephen M. Cameron 					cp, sense_key, asc, ascq,
1304edd16368SStephen M. Cameron 					cmd->result,
1305edd16368SStephen M. Cameron 					cmd->cmnd[0], cmd->cmnd[1],
1306edd16368SStephen M. Cameron 					cmd->cmnd[2], cmd->cmnd[3],
1307edd16368SStephen M. Cameron 					cmd->cmnd[4], cmd->cmnd[5],
1308edd16368SStephen M. Cameron 					cmd->cmnd[6], cmd->cmnd[7],
1309807be732SMike Miller 					cmd->cmnd[8], cmd->cmnd[9],
1310807be732SMike Miller 					cmd->cmnd[10], cmd->cmnd[11],
1311807be732SMike Miller 					cmd->cmnd[12], cmd->cmnd[13],
1312807be732SMike Miller 					cmd->cmnd[14], cmd->cmnd[15]);
1313edd16368SStephen M. Cameron 			break;
1314edd16368SStephen M. Cameron 		}
1315edd16368SStephen M. Cameron 
1316edd16368SStephen M. Cameron 
1317edd16368SStephen M. Cameron 		/* Problem was not a check condition
1318edd16368SStephen M. Cameron 		 * Pass it up to the upper layers...
1319edd16368SStephen M. Cameron 		 */
1320edd16368SStephen M. Cameron 		if (ei->ScsiStatus) {
1321edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
1322edd16368SStephen M. Cameron 				"Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1323edd16368SStephen M. Cameron 				"Returning result: 0x%x\n",
1324edd16368SStephen M. Cameron 				cp, ei->ScsiStatus,
1325edd16368SStephen M. Cameron 				sense_key, asc, ascq,
1326edd16368SStephen M. Cameron 				cmd->result);
1327edd16368SStephen M. Cameron 		} else {  /* scsi status is zero??? How??? */
1328edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
1329edd16368SStephen M. Cameron 				"Returning no connection.\n", cp),
1330edd16368SStephen M. Cameron 
1331edd16368SStephen M. Cameron 			/* Ordinarily, this case should never happen,
1332edd16368SStephen M. Cameron 			 * but there is a bug in some released firmware
1333edd16368SStephen M. Cameron 			 * revisions that allows it to happen if, for
1334edd16368SStephen M. Cameron 			 * example, a 4100 backplane loses power and
1335edd16368SStephen M. Cameron 			 * the tape drive is in it.  We assume that
1336edd16368SStephen M. Cameron 			 * it's a fatal error of some kind because we
1337edd16368SStephen M. Cameron 			 * can't show that it wasn't. We will make it
1338edd16368SStephen M. Cameron 			 * look like selection timeout since that is
1339edd16368SStephen M. Cameron 			 * the most common reason for this to occur,
1340edd16368SStephen M. Cameron 			 * and it's severe enough.
1341edd16368SStephen M. Cameron 			 */
1342edd16368SStephen M. Cameron 
1343edd16368SStephen M. Cameron 			cmd->result = DID_NO_CONNECT << 16;
1344edd16368SStephen M. Cameron 		}
1345edd16368SStephen M. Cameron 		break;
1346edd16368SStephen M. Cameron 
1347edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1348edd16368SStephen M. Cameron 		break;
1349edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
1350edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p has"
1351edd16368SStephen M. Cameron 			" completed with data overrun "
1352edd16368SStephen M. Cameron 			"reported\n", cp);
1353edd16368SStephen M. Cameron 		break;
1354edd16368SStephen M. Cameron 	case CMD_INVALID: {
1355edd16368SStephen M. Cameron 		/* print_bytes(cp, sizeof(*cp), 1, 0);
1356edd16368SStephen M. Cameron 		print_cmd(cp); */
1357edd16368SStephen M. Cameron 		/* We get CMD_INVALID if you address a non-existent device
1358edd16368SStephen M. Cameron 		 * instead of a selection timeout (no response).  You will
1359edd16368SStephen M. Cameron 		 * see this if you yank out a drive, then try to access it.
1360edd16368SStephen M. Cameron 		 * This is kind of a shame because it means that any other
1361edd16368SStephen M. Cameron 		 * CMD_INVALID (e.g. driver bug) will get interpreted as a
1362edd16368SStephen M. Cameron 		 * missing target. */
1363edd16368SStephen M. Cameron 		cmd->result = DID_NO_CONNECT << 16;
1364edd16368SStephen M. Cameron 	}
1365edd16368SStephen M. Cameron 		break;
1366edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
1367256d0eaaSStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1368edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p has "
1369edd16368SStephen M. Cameron 			"protocol error\n", cp);
1370edd16368SStephen M. Cameron 		break;
1371edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
1372edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1373edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p had  hardware error\n", cp);
1374edd16368SStephen M. Cameron 		break;
1375edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
1376edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1377edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
1378edd16368SStephen M. Cameron 		break;
1379edd16368SStephen M. Cameron 	case CMD_ABORTED:
1380edd16368SStephen M. Cameron 		cmd->result = DID_ABORT << 16;
1381edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
1382edd16368SStephen M. Cameron 				cp, ei->ScsiStatus);
1383edd16368SStephen M. Cameron 		break;
1384edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
1385edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1386edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
1387edd16368SStephen M. Cameron 		break;
1388edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
1389f6e76055SStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
1390f6e76055SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p aborted due to an unsolicited "
1391edd16368SStephen M. Cameron 			"abort\n", cp);
1392edd16368SStephen M. Cameron 		break;
1393edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
1394edd16368SStephen M. Cameron 		cmd->result = DID_TIME_OUT << 16;
1395edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
1396edd16368SStephen M. Cameron 		break;
13971d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
13981d5e2ed0SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
13991d5e2ed0SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Command unabortable\n");
14001d5e2ed0SStephen M. Cameron 		break;
1401edd16368SStephen M. Cameron 	default:
1402edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1403edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
1404edd16368SStephen M. Cameron 				cp, ei->CommandStatus);
1405edd16368SStephen M. Cameron 	}
1406edd16368SStephen M. Cameron 	cmd_free(h, cp);
14072cc5bfafSTomas Henzl 	cmd->scsi_done(cmd);
1408edd16368SStephen M. Cameron }
1409edd16368SStephen M. Cameron 
1410edd16368SStephen M. Cameron static void hpsa_pci_unmap(struct pci_dev *pdev,
1411edd16368SStephen M. Cameron 	struct CommandList *c, int sg_used, int data_direction)
1412edd16368SStephen M. Cameron {
1413edd16368SStephen M. Cameron 	int i;
1414edd16368SStephen M. Cameron 	union u64bit addr64;
1415edd16368SStephen M. Cameron 
1416edd16368SStephen M. Cameron 	for (i = 0; i < sg_used; i++) {
1417edd16368SStephen M. Cameron 		addr64.val32.lower = c->SG[i].Addr.lower;
1418edd16368SStephen M. Cameron 		addr64.val32.upper = c->SG[i].Addr.upper;
1419edd16368SStephen M. Cameron 		pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len,
1420edd16368SStephen M. Cameron 			data_direction);
1421edd16368SStephen M. Cameron 	}
1422edd16368SStephen M. Cameron }
1423edd16368SStephen M. Cameron 
1424a2dac136SStephen M. Cameron static int hpsa_map_one(struct pci_dev *pdev,
1425edd16368SStephen M. Cameron 		struct CommandList *cp,
1426edd16368SStephen M. Cameron 		unsigned char *buf,
1427edd16368SStephen M. Cameron 		size_t buflen,
1428edd16368SStephen M. Cameron 		int data_direction)
1429edd16368SStephen M. Cameron {
143001a02ffcSStephen M. Cameron 	u64 addr64;
1431edd16368SStephen M. Cameron 
1432edd16368SStephen M. Cameron 	if (buflen == 0 || data_direction == PCI_DMA_NONE) {
1433edd16368SStephen M. Cameron 		cp->Header.SGList = 0;
1434edd16368SStephen M. Cameron 		cp->Header.SGTotal = 0;
1435a2dac136SStephen M. Cameron 		return 0;
1436edd16368SStephen M. Cameron 	}
1437edd16368SStephen M. Cameron 
143801a02ffcSStephen M. Cameron 	addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction);
1439eceaae18SShuah Khan 	if (dma_mapping_error(&pdev->dev, addr64)) {
1440a2dac136SStephen M. Cameron 		/* Prevent subsequent unmap of something never mapped */
1441eceaae18SShuah Khan 		cp->Header.SGList = 0;
1442eceaae18SShuah Khan 		cp->Header.SGTotal = 0;
1443a2dac136SStephen M. Cameron 		return -1;
1444eceaae18SShuah Khan 	}
1445edd16368SStephen M. Cameron 	cp->SG[0].Addr.lower =
144601a02ffcSStephen M. Cameron 	  (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
1447edd16368SStephen M. Cameron 	cp->SG[0].Addr.upper =
144801a02ffcSStephen M. Cameron 	  (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
1449edd16368SStephen M. Cameron 	cp->SG[0].Len = buflen;
145001a02ffcSStephen M. Cameron 	cp->Header.SGList = (u8) 1;   /* no. SGs contig in this cmd */
145101a02ffcSStephen M. Cameron 	cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */
1452a2dac136SStephen M. Cameron 	return 0;
1453edd16368SStephen M. Cameron }
1454edd16368SStephen M. Cameron 
1455edd16368SStephen M. Cameron static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
1456edd16368SStephen M. Cameron 	struct CommandList *c)
1457edd16368SStephen M. Cameron {
1458edd16368SStephen M. Cameron 	DECLARE_COMPLETION_ONSTACK(wait);
1459edd16368SStephen M. Cameron 
1460edd16368SStephen M. Cameron 	c->waiting = &wait;
1461edd16368SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
1462edd16368SStephen M. Cameron 	wait_for_completion(&wait);
1463edd16368SStephen M. Cameron }
1464edd16368SStephen M. Cameron 
1465a0c12413SStephen M. Cameron static void hpsa_scsi_do_simple_cmd_core_if_no_lockup(struct ctlr_info *h,
1466a0c12413SStephen M. Cameron 	struct CommandList *c)
1467a0c12413SStephen M. Cameron {
1468a0c12413SStephen M. Cameron 	unsigned long flags;
1469a0c12413SStephen M. Cameron 
1470a0c12413SStephen M. Cameron 	/* If controller lockup detected, fake a hardware error. */
1471a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
1472a0c12413SStephen M. Cameron 	if (unlikely(h->lockup_detected)) {
1473a0c12413SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
1474a0c12413SStephen M. Cameron 		c->err_info->CommandStatus = CMD_HARDWARE_ERR;
1475a0c12413SStephen M. Cameron 	} else {
1476a0c12413SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
1477a0c12413SStephen M. Cameron 		hpsa_scsi_do_simple_cmd_core(h, c);
1478a0c12413SStephen M. Cameron 	}
1479a0c12413SStephen M. Cameron }
1480a0c12413SStephen M. Cameron 
14819c2fc160SStephen M. Cameron #define MAX_DRIVER_CMD_RETRIES 25
1482edd16368SStephen M. Cameron static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
1483edd16368SStephen M. Cameron 	struct CommandList *c, int data_direction)
1484edd16368SStephen M. Cameron {
14859c2fc160SStephen M. Cameron 	int backoff_time = 10, retry_count = 0;
1486edd16368SStephen M. Cameron 
1487edd16368SStephen M. Cameron 	do {
14887630abd0SJoe Perches 		memset(c->err_info, 0, sizeof(*c->err_info));
1489edd16368SStephen M. Cameron 		hpsa_scsi_do_simple_cmd_core(h, c);
1490edd16368SStephen M. Cameron 		retry_count++;
14919c2fc160SStephen M. Cameron 		if (retry_count > 3) {
14929c2fc160SStephen M. Cameron 			msleep(backoff_time);
14939c2fc160SStephen M. Cameron 			if (backoff_time < 1000)
14949c2fc160SStephen M. Cameron 				backoff_time *= 2;
14959c2fc160SStephen M. Cameron 		}
1496852af20aSMatt Bondurant 	} while ((check_for_unit_attention(h, c) ||
14979c2fc160SStephen M. Cameron 			check_for_busy(h, c)) &&
14989c2fc160SStephen M. Cameron 			retry_count <= MAX_DRIVER_CMD_RETRIES);
1499edd16368SStephen M. Cameron 	hpsa_pci_unmap(h->pdev, c, 1, data_direction);
1500edd16368SStephen M. Cameron }
1501edd16368SStephen M. Cameron 
1502edd16368SStephen M. Cameron static void hpsa_scsi_interpret_error(struct CommandList *cp)
1503edd16368SStephen M. Cameron {
1504edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
1505edd16368SStephen M. Cameron 	struct device *d = &cp->h->pdev->dev;
1506edd16368SStephen M. Cameron 
1507edd16368SStephen M. Cameron 	ei = cp->err_info;
1508edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
1509edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
1510edd16368SStephen M. Cameron 		dev_warn(d, "cmd %p has completed with errors\n", cp);
1511edd16368SStephen M. Cameron 		dev_warn(d, "cmd %p has SCSI Status = %x\n", cp,
1512edd16368SStephen M. Cameron 				ei->ScsiStatus);
1513edd16368SStephen M. Cameron 		if (ei->ScsiStatus == 0)
1514edd16368SStephen M. Cameron 			dev_warn(d, "SCSI status is abnormally zero.  "
1515edd16368SStephen M. Cameron 			"(probably indicates selection timeout "
1516edd16368SStephen M. Cameron 			"reported incorrectly due to a known "
1517edd16368SStephen M. Cameron 			"firmware bug, circa July, 2001.)\n");
1518edd16368SStephen M. Cameron 		break;
1519edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1520edd16368SStephen M. Cameron 			dev_info(d, "UNDERRUN\n");
1521edd16368SStephen M. Cameron 		break;
1522edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
1523edd16368SStephen M. Cameron 		dev_warn(d, "cp %p has completed with data overrun\n", cp);
1524edd16368SStephen M. Cameron 		break;
1525edd16368SStephen M. Cameron 	case CMD_INVALID: {
1526edd16368SStephen M. Cameron 		/* controller unfortunately reports SCSI passthru's
1527edd16368SStephen M. Cameron 		 * to non-existent targets as invalid commands.
1528edd16368SStephen M. Cameron 		 */
1529edd16368SStephen M. Cameron 		dev_warn(d, "cp %p is reported invalid (probably means "
1530edd16368SStephen M. Cameron 			"target device no longer present)\n", cp);
1531edd16368SStephen M. Cameron 		/* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
1532edd16368SStephen M. Cameron 		print_cmd(cp);  */
1533edd16368SStephen M. Cameron 		}
1534edd16368SStephen M. Cameron 		break;
1535edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
1536edd16368SStephen M. Cameron 		dev_warn(d, "cp %p has protocol error \n", cp);
1537edd16368SStephen M. Cameron 		break;
1538edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
1539edd16368SStephen M. Cameron 		/* cmd->result = DID_ERROR << 16; */
1540edd16368SStephen M. Cameron 		dev_warn(d, "cp %p had hardware error\n", cp);
1541edd16368SStephen M. Cameron 		break;
1542edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
1543edd16368SStephen M. Cameron 		dev_warn(d, "cp %p had connection lost\n", cp);
1544edd16368SStephen M. Cameron 		break;
1545edd16368SStephen M. Cameron 	case CMD_ABORTED:
1546edd16368SStephen M. Cameron 		dev_warn(d, "cp %p was aborted\n", cp);
1547edd16368SStephen M. Cameron 		break;
1548edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
1549edd16368SStephen M. Cameron 		dev_warn(d, "cp %p reports abort failed\n", cp);
1550edd16368SStephen M. Cameron 		break;
1551edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
1552edd16368SStephen M. Cameron 		dev_warn(d, "cp %p aborted due to an unsolicited abort\n", cp);
1553edd16368SStephen M. Cameron 		break;
1554edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
1555edd16368SStephen M. Cameron 		dev_warn(d, "cp %p timed out\n", cp);
1556edd16368SStephen M. Cameron 		break;
15571d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
15581d5e2ed0SStephen M. Cameron 		dev_warn(d, "Command unabortable\n");
15591d5e2ed0SStephen M. Cameron 		break;
1560edd16368SStephen M. Cameron 	default:
1561edd16368SStephen M. Cameron 		dev_warn(d, "cp %p returned unknown status %x\n", cp,
1562edd16368SStephen M. Cameron 				ei->CommandStatus);
1563edd16368SStephen M. Cameron 	}
1564edd16368SStephen M. Cameron }
1565edd16368SStephen M. Cameron 
1566edd16368SStephen M. Cameron static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
1567edd16368SStephen M. Cameron 			unsigned char page, unsigned char *buf,
1568edd16368SStephen M. Cameron 			unsigned char bufsize)
1569edd16368SStephen M. Cameron {
1570edd16368SStephen M. Cameron 	int rc = IO_OK;
1571edd16368SStephen M. Cameron 	struct CommandList *c;
1572edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
1573edd16368SStephen M. Cameron 
1574edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
1575edd16368SStephen M. Cameron 
1576edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
1577edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1578ecd9aad4SStephen M. Cameron 		return -ENOMEM;
1579edd16368SStephen M. Cameron 	}
1580edd16368SStephen M. Cameron 
1581a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
1582a2dac136SStephen M. Cameron 			page, scsi3addr, TYPE_CMD)) {
1583a2dac136SStephen M. Cameron 		rc = -1;
1584a2dac136SStephen M. Cameron 		goto out;
1585a2dac136SStephen M. Cameron 	}
1586edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1587edd16368SStephen M. Cameron 	ei = c->err_info;
1588edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
1589edd16368SStephen M. Cameron 		hpsa_scsi_interpret_error(c);
1590edd16368SStephen M. Cameron 		rc = -1;
1591edd16368SStephen M. Cameron 	}
1592a2dac136SStephen M. Cameron out:
1593edd16368SStephen M. Cameron 	cmd_special_free(h, c);
1594edd16368SStephen M. Cameron 	return rc;
1595edd16368SStephen M. Cameron }
1596edd16368SStephen M. Cameron 
1597edd16368SStephen M. Cameron static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr)
1598edd16368SStephen M. Cameron {
1599edd16368SStephen M. Cameron 	int rc = IO_OK;
1600edd16368SStephen M. Cameron 	struct CommandList *c;
1601edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
1602edd16368SStephen M. Cameron 
1603edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
1604edd16368SStephen M. Cameron 
1605edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
1606edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1607e9ea04a6SStephen M. Cameron 		return -ENOMEM;
1608edd16368SStephen M. Cameron 	}
1609edd16368SStephen M. Cameron 
1610a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map. */
1611a2dac136SStephen M. Cameron 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h,
1612a2dac136SStephen M. Cameron 			NULL, 0, 0, scsi3addr, TYPE_MSG);
1613edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core(h, c);
1614edd16368SStephen M. Cameron 	/* no unmap needed here because no data xfer. */
1615edd16368SStephen M. Cameron 
1616edd16368SStephen M. Cameron 	ei = c->err_info;
1617edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0) {
1618edd16368SStephen M. Cameron 		hpsa_scsi_interpret_error(c);
1619edd16368SStephen M. Cameron 		rc = -1;
1620edd16368SStephen M. Cameron 	}
1621edd16368SStephen M. Cameron 	cmd_special_free(h, c);
1622edd16368SStephen M. Cameron 	return rc;
1623edd16368SStephen M. Cameron }
1624edd16368SStephen M. Cameron 
1625edd16368SStephen M. Cameron static void hpsa_get_raid_level(struct ctlr_info *h,
1626edd16368SStephen M. Cameron 	unsigned char *scsi3addr, unsigned char *raid_level)
1627edd16368SStephen M. Cameron {
1628edd16368SStephen M. Cameron 	int rc;
1629edd16368SStephen M. Cameron 	unsigned char *buf;
1630edd16368SStephen M. Cameron 
1631edd16368SStephen M. Cameron 	*raid_level = RAID_UNKNOWN;
1632edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
1633edd16368SStephen M. Cameron 	if (!buf)
1634edd16368SStephen M. Cameron 		return;
1635edd16368SStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0xC1, buf, 64);
1636edd16368SStephen M. Cameron 	if (rc == 0)
1637edd16368SStephen M. Cameron 		*raid_level = buf[8];
1638edd16368SStephen M. Cameron 	if (*raid_level > RAID_UNKNOWN)
1639edd16368SStephen M. Cameron 		*raid_level = RAID_UNKNOWN;
1640edd16368SStephen M. Cameron 	kfree(buf);
1641edd16368SStephen M. Cameron 	return;
1642edd16368SStephen M. Cameron }
1643edd16368SStephen M. Cameron 
1644edd16368SStephen M. Cameron /* Get the device id from inquiry page 0x83 */
1645edd16368SStephen M. Cameron static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
1646edd16368SStephen M. Cameron 	unsigned char *device_id, int buflen)
1647edd16368SStephen M. Cameron {
1648edd16368SStephen M. Cameron 	int rc;
1649edd16368SStephen M. Cameron 	unsigned char *buf;
1650edd16368SStephen M. Cameron 
1651edd16368SStephen M. Cameron 	if (buflen > 16)
1652edd16368SStephen M. Cameron 		buflen = 16;
1653edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
1654edd16368SStephen M. Cameron 	if (!buf)
1655edd16368SStephen M. Cameron 		return -1;
1656edd16368SStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0x83, buf, 64);
1657edd16368SStephen M. Cameron 	if (rc == 0)
1658edd16368SStephen M. Cameron 		memcpy(device_id, &buf[8], buflen);
1659edd16368SStephen M. Cameron 	kfree(buf);
1660edd16368SStephen M. Cameron 	return rc != 0;
1661edd16368SStephen M. Cameron }
1662edd16368SStephen M. Cameron 
1663edd16368SStephen M. Cameron static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
1664edd16368SStephen M. Cameron 		struct ReportLUNdata *buf, int bufsize,
1665edd16368SStephen M. Cameron 		int extended_response)
1666edd16368SStephen M. Cameron {
1667edd16368SStephen M. Cameron 	int rc = IO_OK;
1668edd16368SStephen M. Cameron 	struct CommandList *c;
1669edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
1670edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
1671edd16368SStephen M. Cameron 
1672edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
1673edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
1674edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1675edd16368SStephen M. Cameron 		return -1;
1676edd16368SStephen M. Cameron 	}
1677e89c0ae7SStephen M. Cameron 	/* address the controller */
1678e89c0ae7SStephen M. Cameron 	memset(scsi3addr, 0, sizeof(scsi3addr));
1679a2dac136SStephen M. Cameron 	if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
1680a2dac136SStephen M. Cameron 		buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
1681a2dac136SStephen M. Cameron 		rc = -1;
1682a2dac136SStephen M. Cameron 		goto out;
1683a2dac136SStephen M. Cameron 	}
1684edd16368SStephen M. Cameron 	if (extended_response)
1685edd16368SStephen M. Cameron 		c->Request.CDB[1] = extended_response;
1686edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1687edd16368SStephen M. Cameron 	ei = c->err_info;
1688edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 &&
1689edd16368SStephen M. Cameron 	    ei->CommandStatus != CMD_DATA_UNDERRUN) {
1690edd16368SStephen M. Cameron 		hpsa_scsi_interpret_error(c);
1691edd16368SStephen M. Cameron 		rc = -1;
1692edd16368SStephen M. Cameron 	}
1693a2dac136SStephen M. Cameron out:
1694edd16368SStephen M. Cameron 	cmd_special_free(h, c);
1695edd16368SStephen M. Cameron 	return rc;
1696edd16368SStephen M. Cameron }
1697edd16368SStephen M. Cameron 
1698edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
1699edd16368SStephen M. Cameron 		struct ReportLUNdata *buf,
1700edd16368SStephen M. Cameron 		int bufsize, int extended_response)
1701edd16368SStephen M. Cameron {
1702edd16368SStephen M. Cameron 	return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
1703edd16368SStephen M. Cameron }
1704edd16368SStephen M. Cameron 
1705edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
1706edd16368SStephen M. Cameron 		struct ReportLUNdata *buf, int bufsize)
1707edd16368SStephen M. Cameron {
1708edd16368SStephen M. Cameron 	return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
1709edd16368SStephen M. Cameron }
1710edd16368SStephen M. Cameron 
1711edd16368SStephen M. Cameron static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
1712edd16368SStephen M. Cameron 	int bus, int target, int lun)
1713edd16368SStephen M. Cameron {
1714edd16368SStephen M. Cameron 	device->bus = bus;
1715edd16368SStephen M. Cameron 	device->target = target;
1716edd16368SStephen M. Cameron 	device->lun = lun;
1717edd16368SStephen M. Cameron }
1718edd16368SStephen M. Cameron 
1719edd16368SStephen M. Cameron static int hpsa_update_device_info(struct ctlr_info *h,
17200b0e1d6cSStephen M. Cameron 	unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
17210b0e1d6cSStephen M. Cameron 	unsigned char *is_OBDR_device)
1722edd16368SStephen M. Cameron {
17230b0e1d6cSStephen M. Cameron 
17240b0e1d6cSStephen M. Cameron #define OBDR_SIG_OFFSET 43
17250b0e1d6cSStephen M. Cameron #define OBDR_TAPE_SIG "$DR-10"
17260b0e1d6cSStephen M. Cameron #define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
17270b0e1d6cSStephen M. Cameron #define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
17280b0e1d6cSStephen M. Cameron 
1729ea6d3bc3SStephen M. Cameron 	unsigned char *inq_buff;
17300b0e1d6cSStephen M. Cameron 	unsigned char *obdr_sig;
1731edd16368SStephen M. Cameron 
1732ea6d3bc3SStephen M. Cameron 	inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
1733edd16368SStephen M. Cameron 	if (!inq_buff)
1734edd16368SStephen M. Cameron 		goto bail_out;
1735edd16368SStephen M. Cameron 
1736edd16368SStephen M. Cameron 	/* Do an inquiry to the device to see what it is. */
1737edd16368SStephen M. Cameron 	if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
1738edd16368SStephen M. Cameron 		(unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
1739edd16368SStephen M. Cameron 		/* Inquiry failed (msg printed already) */
1740edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev,
1741edd16368SStephen M. Cameron 			"hpsa_update_device_info: inquiry failed\n");
1742edd16368SStephen M. Cameron 		goto bail_out;
1743edd16368SStephen M. Cameron 	}
1744edd16368SStephen M. Cameron 
1745edd16368SStephen M. Cameron 	this_device->devtype = (inq_buff[0] & 0x1f);
1746edd16368SStephen M. Cameron 	memcpy(this_device->scsi3addr, scsi3addr, 8);
1747edd16368SStephen M. Cameron 	memcpy(this_device->vendor, &inq_buff[8],
1748edd16368SStephen M. Cameron 		sizeof(this_device->vendor));
1749edd16368SStephen M. Cameron 	memcpy(this_device->model, &inq_buff[16],
1750edd16368SStephen M. Cameron 		sizeof(this_device->model));
1751edd16368SStephen M. Cameron 	memset(this_device->device_id, 0,
1752edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
1753edd16368SStephen M. Cameron 	hpsa_get_device_id(h, scsi3addr, this_device->device_id,
1754edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
1755edd16368SStephen M. Cameron 
1756edd16368SStephen M. Cameron 	if (this_device->devtype == TYPE_DISK &&
1757edd16368SStephen M. Cameron 		is_logical_dev_addr_mode(scsi3addr))
1758edd16368SStephen M. Cameron 		hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
1759edd16368SStephen M. Cameron 	else
1760edd16368SStephen M. Cameron 		this_device->raid_level = RAID_UNKNOWN;
1761edd16368SStephen M. Cameron 
17620b0e1d6cSStephen M. Cameron 	if (is_OBDR_device) {
17630b0e1d6cSStephen M. Cameron 		/* See if this is a One-Button-Disaster-Recovery device
17640b0e1d6cSStephen M. Cameron 		 * by looking for "$DR-10" at offset 43 in inquiry data.
17650b0e1d6cSStephen M. Cameron 		 */
17660b0e1d6cSStephen M. Cameron 		obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
17670b0e1d6cSStephen M. Cameron 		*is_OBDR_device = (this_device->devtype == TYPE_ROM &&
17680b0e1d6cSStephen M. Cameron 					strncmp(obdr_sig, OBDR_TAPE_SIG,
17690b0e1d6cSStephen M. Cameron 						OBDR_SIG_LEN) == 0);
17700b0e1d6cSStephen M. Cameron 	}
17710b0e1d6cSStephen M. Cameron 
1772edd16368SStephen M. Cameron 	kfree(inq_buff);
1773edd16368SStephen M. Cameron 	return 0;
1774edd16368SStephen M. Cameron 
1775edd16368SStephen M. Cameron bail_out:
1776edd16368SStephen M. Cameron 	kfree(inq_buff);
1777edd16368SStephen M. Cameron 	return 1;
1778edd16368SStephen M. Cameron }
1779edd16368SStephen M. Cameron 
17804f4eb9f1SScott Teel static unsigned char *ext_target_model[] = {
1781edd16368SStephen M. Cameron 	"MSA2012",
1782edd16368SStephen M. Cameron 	"MSA2024",
1783edd16368SStephen M. Cameron 	"MSA2312",
1784edd16368SStephen M. Cameron 	"MSA2324",
1785fda38518SStephen M. Cameron 	"P2000 G3 SAS",
1786edd16368SStephen M. Cameron 	NULL,
1787edd16368SStephen M. Cameron };
1788edd16368SStephen M. Cameron 
17894f4eb9f1SScott Teel static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
1790edd16368SStephen M. Cameron {
1791edd16368SStephen M. Cameron 	int i;
1792edd16368SStephen M. Cameron 
17934f4eb9f1SScott Teel 	for (i = 0; ext_target_model[i]; i++)
17944f4eb9f1SScott Teel 		if (strncmp(device->model, ext_target_model[i],
17954f4eb9f1SScott Teel 			strlen(ext_target_model[i])) == 0)
1796edd16368SStephen M. Cameron 			return 1;
1797edd16368SStephen M. Cameron 	return 0;
1798edd16368SStephen M. Cameron }
1799edd16368SStephen M. Cameron 
1800edd16368SStephen M. Cameron /* Helper function to assign bus, target, lun mapping of devices.
18014f4eb9f1SScott Teel  * Puts non-external target logical volumes on bus 0, external target logical
1802edd16368SStephen M. Cameron  * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
1803edd16368SStephen M. Cameron  * Logical drive target and lun are assigned at this time, but
1804edd16368SStephen M. Cameron  * physical device lun and target assignment are deferred (assigned
1805edd16368SStephen M. Cameron  * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
1806edd16368SStephen M. Cameron  */
1807edd16368SStephen M. Cameron static void figure_bus_target_lun(struct ctlr_info *h,
18081f310bdeSStephen M. Cameron 	u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
1809edd16368SStephen M. Cameron {
18101f310bdeSStephen M. Cameron 	u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
1811edd16368SStephen M. Cameron 
18121f310bdeSStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes)) {
18131f310bdeSStephen M. Cameron 		/* physical device, target and lun filled in later */
18141f310bdeSStephen M. Cameron 		if (is_hba_lunid(lunaddrbytes))
18151f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
18161f310bdeSStephen M. Cameron 		else
18171f310bdeSStephen M. Cameron 			/* defer target, lun assignment for physical devices */
18181f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 2, -1, -1);
18191f310bdeSStephen M. Cameron 		return;
18201f310bdeSStephen M. Cameron 	}
18211f310bdeSStephen M. Cameron 	/* It's a logical device */
18224f4eb9f1SScott Teel 	if (is_ext_target(h, device)) {
18234f4eb9f1SScott Teel 		/* external target way, put logicals on bus 1
1824339b2b14SStephen M. Cameron 		 * and match target/lun numbers box
18251f310bdeSStephen M. Cameron 		 * reports, other smart array, bus 0, target 0, match lunid
1826339b2b14SStephen M. Cameron 		 */
18271f310bdeSStephen M. Cameron 		hpsa_set_bus_target_lun(device,
18281f310bdeSStephen M. Cameron 			1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
18291f310bdeSStephen M. Cameron 		return;
1830339b2b14SStephen M. Cameron 	}
18311f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
1832edd16368SStephen M. Cameron }
1833edd16368SStephen M. Cameron 
1834edd16368SStephen M. Cameron /*
1835edd16368SStephen M. Cameron  * If there is no lun 0 on a target, linux won't find any devices.
18364f4eb9f1SScott Teel  * For the external targets (arrays), we have to manually detect the enclosure
1837edd16368SStephen M. Cameron  * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
1838edd16368SStephen M. Cameron  * it for some reason.  *tmpdevice is the target we're adding,
1839edd16368SStephen M. Cameron  * this_device is a pointer into the current element of currentsd[]
1840edd16368SStephen M. Cameron  * that we're building up in update_scsi_devices(), below.
1841edd16368SStephen M. Cameron  * lunzerobits is a bitmap that tracks which targets already have a
1842edd16368SStephen M. Cameron  * lun 0 assigned.
1843edd16368SStephen M. Cameron  * Returns 1 if an enclosure was added, 0 if not.
1844edd16368SStephen M. Cameron  */
18454f4eb9f1SScott Teel static int add_ext_target_dev(struct ctlr_info *h,
1846edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *tmpdevice,
184701a02ffcSStephen M. Cameron 	struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
18484f4eb9f1SScott Teel 	unsigned long lunzerobits[], int *n_ext_target_devs)
1849edd16368SStephen M. Cameron {
1850edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
1851edd16368SStephen M. Cameron 
18521f310bdeSStephen M. Cameron 	if (test_bit(tmpdevice->target, lunzerobits))
1853edd16368SStephen M. Cameron 		return 0; /* There is already a lun 0 on this target. */
1854edd16368SStephen M. Cameron 
1855edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes))
1856edd16368SStephen M. Cameron 		return 0; /* It's the logical targets that may lack lun 0. */
1857edd16368SStephen M. Cameron 
18584f4eb9f1SScott Teel 	if (!is_ext_target(h, tmpdevice))
18594f4eb9f1SScott Teel 		return 0; /* Only external target devices have this problem. */
1860edd16368SStephen M. Cameron 
18611f310bdeSStephen M. Cameron 	if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
1862edd16368SStephen M. Cameron 		return 0;
1863edd16368SStephen M. Cameron 
1864c4f8a299SStephen M. Cameron 	memset(scsi3addr, 0, 8);
18651f310bdeSStephen M. Cameron 	scsi3addr[3] = tmpdevice->target;
1866edd16368SStephen M. Cameron 	if (is_hba_lunid(scsi3addr))
1867edd16368SStephen M. Cameron 		return 0; /* Don't add the RAID controller here. */
1868edd16368SStephen M. Cameron 
1869339b2b14SStephen M. Cameron 	if (is_scsi_rev_5(h))
1870339b2b14SStephen M. Cameron 		return 0; /* p1210m doesn't need to do this. */
1871339b2b14SStephen M. Cameron 
18724f4eb9f1SScott Teel 	if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
1873aca4a520SScott Teel 		dev_warn(&h->pdev->dev, "Maximum number of external "
1874aca4a520SScott Teel 			"target devices exceeded.  Check your hardware "
1875edd16368SStephen M. Cameron 			"configuration.");
1876edd16368SStephen M. Cameron 		return 0;
1877edd16368SStephen M. Cameron 	}
1878edd16368SStephen M. Cameron 
18790b0e1d6cSStephen M. Cameron 	if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
1880edd16368SStephen M. Cameron 		return 0;
18814f4eb9f1SScott Teel 	(*n_ext_target_devs)++;
18821f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(this_device,
18831f310bdeSStephen M. Cameron 				tmpdevice->bus, tmpdevice->target, 0);
18841f310bdeSStephen M. Cameron 	set_bit(tmpdevice->target, lunzerobits);
1885edd16368SStephen M. Cameron 	return 1;
1886edd16368SStephen M. Cameron }
1887edd16368SStephen M. Cameron 
1888edd16368SStephen M. Cameron /*
1889edd16368SStephen M. Cameron  * Do CISS_REPORT_PHYS and CISS_REPORT_LOG.  Data is returned in physdev,
1890edd16368SStephen M. Cameron  * logdev.  The number of luns in physdev and logdev are returned in
1891edd16368SStephen M. Cameron  * *nphysicals and *nlogicals, respectively.
1892edd16368SStephen M. Cameron  * Returns 0 on success, -1 otherwise.
1893edd16368SStephen M. Cameron  */
1894edd16368SStephen M. Cameron static int hpsa_gather_lun_info(struct ctlr_info *h,
1895edd16368SStephen M. Cameron 	int reportlunsize,
189601a02ffcSStephen M. Cameron 	struct ReportLUNdata *physdev, u32 *nphysicals,
189701a02ffcSStephen M. Cameron 	struct ReportLUNdata *logdev, u32 *nlogicals)
1898edd16368SStephen M. Cameron {
1899edd16368SStephen M. Cameron 	if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize, 0)) {
1900edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
1901edd16368SStephen M. Cameron 		return -1;
1902edd16368SStephen M. Cameron 	}
19036df1e954SStephen M. Cameron 	*nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 8;
1904edd16368SStephen M. Cameron 	if (*nphysicals > HPSA_MAX_PHYS_LUN) {
1905edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
1906edd16368SStephen M. Cameron 			"  %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
1907edd16368SStephen M. Cameron 			*nphysicals - HPSA_MAX_PHYS_LUN);
1908edd16368SStephen M. Cameron 		*nphysicals = HPSA_MAX_PHYS_LUN;
1909edd16368SStephen M. Cameron 	}
1910edd16368SStephen M. Cameron 	if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
1911edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
1912edd16368SStephen M. Cameron 		return -1;
1913edd16368SStephen M. Cameron 	}
19146df1e954SStephen M. Cameron 	*nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
1915edd16368SStephen M. Cameron 	/* Reject Logicals in excess of our max capability. */
1916edd16368SStephen M. Cameron 	if (*nlogicals > HPSA_MAX_LUN) {
1917edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
1918edd16368SStephen M. Cameron 			"maximum logical LUNs (%d) exceeded.  "
1919edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_LUN,
1920edd16368SStephen M. Cameron 			*nlogicals - HPSA_MAX_LUN);
1921edd16368SStephen M. Cameron 			*nlogicals = HPSA_MAX_LUN;
1922edd16368SStephen M. Cameron 	}
1923edd16368SStephen M. Cameron 	if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
1924edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
1925edd16368SStephen M. Cameron 			"maximum logical + physical LUNs (%d) exceeded. "
1926edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
1927edd16368SStephen M. Cameron 			*nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
1928edd16368SStephen M. Cameron 		*nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
1929edd16368SStephen M. Cameron 	}
1930edd16368SStephen M. Cameron 	return 0;
1931edd16368SStephen M. Cameron }
1932edd16368SStephen M. Cameron 
1933339b2b14SStephen M. Cameron u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i,
1934339b2b14SStephen M. Cameron 	int nphysicals, int nlogicals, struct ReportLUNdata *physdev_list,
1935339b2b14SStephen M. Cameron 	struct ReportLUNdata *logdev_list)
1936339b2b14SStephen M. Cameron {
1937339b2b14SStephen M. Cameron 	/* Helper function, figure out where the LUN ID info is coming from
1938339b2b14SStephen M. Cameron 	 * given index i, lists of physical and logical devices, where in
1939339b2b14SStephen M. Cameron 	 * the list the raid controller is supposed to appear (first or last)
1940339b2b14SStephen M. Cameron 	 */
1941339b2b14SStephen M. Cameron 
1942339b2b14SStephen M. Cameron 	int logicals_start = nphysicals + (raid_ctlr_position == 0);
1943339b2b14SStephen M. Cameron 	int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
1944339b2b14SStephen M. Cameron 
1945339b2b14SStephen M. Cameron 	if (i == raid_ctlr_position)
1946339b2b14SStephen M. Cameron 		return RAID_CTLR_LUNID;
1947339b2b14SStephen M. Cameron 
1948339b2b14SStephen M. Cameron 	if (i < logicals_start)
1949339b2b14SStephen M. Cameron 		return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0];
1950339b2b14SStephen M. Cameron 
1951339b2b14SStephen M. Cameron 	if (i < last_device)
1952339b2b14SStephen M. Cameron 		return &logdev_list->LUN[i - nphysicals -
1953339b2b14SStephen M. Cameron 			(raid_ctlr_position == 0)][0];
1954339b2b14SStephen M. Cameron 	BUG();
1955339b2b14SStephen M. Cameron 	return NULL;
1956339b2b14SStephen M. Cameron }
1957339b2b14SStephen M. Cameron 
1958edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
1959edd16368SStephen M. Cameron {
1960edd16368SStephen M. Cameron 	/* the idea here is we could get notified
1961edd16368SStephen M. Cameron 	 * that some devices have changed, so we do a report
1962edd16368SStephen M. Cameron 	 * physical luns and report logical luns cmd, and adjust
1963edd16368SStephen M. Cameron 	 * our list of devices accordingly.
1964edd16368SStephen M. Cameron 	 *
1965edd16368SStephen M. Cameron 	 * The scsi3addr's of devices won't change so long as the
1966edd16368SStephen M. Cameron 	 * adapter is not reset.  That means we can rescan and
1967edd16368SStephen M. Cameron 	 * tell which devices we already know about, vs. new
1968edd16368SStephen M. Cameron 	 * devices, vs.  disappearing devices.
1969edd16368SStephen M. Cameron 	 */
1970edd16368SStephen M. Cameron 	struct ReportLUNdata *physdev_list = NULL;
1971edd16368SStephen M. Cameron 	struct ReportLUNdata *logdev_list = NULL;
197201a02ffcSStephen M. Cameron 	u32 nphysicals = 0;
197301a02ffcSStephen M. Cameron 	u32 nlogicals = 0;
197401a02ffcSStephen M. Cameron 	u32 ndev_allocated = 0;
1975edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
1976edd16368SStephen M. Cameron 	int ncurrent = 0;
1977edd16368SStephen M. Cameron 	int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 8;
19784f4eb9f1SScott Teel 	int i, n_ext_target_devs, ndevs_to_allocate;
1979339b2b14SStephen M. Cameron 	int raid_ctlr_position;
1980aca4a520SScott Teel 	DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
1981edd16368SStephen M. Cameron 
1982cfe5badcSScott Teel 	currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
1983edd16368SStephen M. Cameron 	physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
1984edd16368SStephen M. Cameron 	logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
1985edd16368SStephen M. Cameron 	tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
1986edd16368SStephen M. Cameron 
19870b0e1d6cSStephen M. Cameron 	if (!currentsd || !physdev_list || !logdev_list || !tmpdevice) {
1988edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory\n");
1989edd16368SStephen M. Cameron 		goto out;
1990edd16368SStephen M. Cameron 	}
1991edd16368SStephen M. Cameron 	memset(lunzerobits, 0, sizeof(lunzerobits));
1992edd16368SStephen M. Cameron 
1993edd16368SStephen M. Cameron 	if (hpsa_gather_lun_info(h, reportlunsize, physdev_list, &nphysicals,
1994edd16368SStephen M. Cameron 			logdev_list, &nlogicals))
1995edd16368SStephen M. Cameron 		goto out;
1996edd16368SStephen M. Cameron 
1997aca4a520SScott Teel 	/* We might see up to the maximum number of logical and physical disks
1998aca4a520SScott Teel 	 * plus external target devices, and a device for the local RAID
1999aca4a520SScott Teel 	 * controller.
2000edd16368SStephen M. Cameron 	 */
2001aca4a520SScott Teel 	ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
2002edd16368SStephen M. Cameron 
2003edd16368SStephen M. Cameron 	/* Allocate the per device structures */
2004edd16368SStephen M. Cameron 	for (i = 0; i < ndevs_to_allocate; i++) {
2005b7ec021fSScott Teel 		if (i >= HPSA_MAX_DEVICES) {
2006b7ec021fSScott Teel 			dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
2007b7ec021fSScott Teel 				"  %d devices ignored.\n", HPSA_MAX_DEVICES,
2008b7ec021fSScott Teel 				ndevs_to_allocate - HPSA_MAX_DEVICES);
2009b7ec021fSScott Teel 			break;
2010b7ec021fSScott Teel 		}
2011b7ec021fSScott Teel 
2012edd16368SStephen M. Cameron 		currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
2013edd16368SStephen M. Cameron 		if (!currentsd[i]) {
2014edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
2015edd16368SStephen M. Cameron 				__FILE__, __LINE__);
2016edd16368SStephen M. Cameron 			goto out;
2017edd16368SStephen M. Cameron 		}
2018edd16368SStephen M. Cameron 		ndev_allocated++;
2019edd16368SStephen M. Cameron 	}
2020edd16368SStephen M. Cameron 
2021339b2b14SStephen M. Cameron 	if (unlikely(is_scsi_rev_5(h)))
2022339b2b14SStephen M. Cameron 		raid_ctlr_position = 0;
2023339b2b14SStephen M. Cameron 	else
2024339b2b14SStephen M. Cameron 		raid_ctlr_position = nphysicals + nlogicals;
2025339b2b14SStephen M. Cameron 
2026edd16368SStephen M. Cameron 	/* adjust our table of devices */
20274f4eb9f1SScott Teel 	n_ext_target_devs = 0;
2028edd16368SStephen M. Cameron 	for (i = 0; i < nphysicals + nlogicals + 1; i++) {
20290b0e1d6cSStephen M. Cameron 		u8 *lunaddrbytes, is_OBDR = 0;
2030edd16368SStephen M. Cameron 
2031edd16368SStephen M. Cameron 		/* Figure out where the LUN ID info is coming from */
2032339b2b14SStephen M. Cameron 		lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
2033339b2b14SStephen M. Cameron 			i, nphysicals, nlogicals, physdev_list, logdev_list);
2034edd16368SStephen M. Cameron 		/* skip masked physical devices. */
2035339b2b14SStephen M. Cameron 		if (lunaddrbytes[3] & 0xC0 &&
2036339b2b14SStephen M. Cameron 			i < nphysicals + (raid_ctlr_position == 0))
2037edd16368SStephen M. Cameron 			continue;
2038edd16368SStephen M. Cameron 
2039edd16368SStephen M. Cameron 		/* Get device type, vendor, model, device id */
20400b0e1d6cSStephen M. Cameron 		if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
20410b0e1d6cSStephen M. Cameron 							&is_OBDR))
2042edd16368SStephen M. Cameron 			continue; /* skip it if we can't talk to it. */
20431f310bdeSStephen M. Cameron 		figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
2044edd16368SStephen M. Cameron 		this_device = currentsd[ncurrent];
2045edd16368SStephen M. Cameron 
2046edd16368SStephen M. Cameron 		/*
20474f4eb9f1SScott Teel 		 * For external target devices, we have to insert a LUN 0 which
2048edd16368SStephen M. Cameron 		 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
2049edd16368SStephen M. Cameron 		 * is nonetheless an enclosure device there.  We have to
2050edd16368SStephen M. Cameron 		 * present that otherwise linux won't find anything if
2051edd16368SStephen M. Cameron 		 * there is no lun 0.
2052edd16368SStephen M. Cameron 		 */
20534f4eb9f1SScott Teel 		if (add_ext_target_dev(h, tmpdevice, this_device,
20541f310bdeSStephen M. Cameron 				lunaddrbytes, lunzerobits,
20554f4eb9f1SScott Teel 				&n_ext_target_devs)) {
2056edd16368SStephen M. Cameron 			ncurrent++;
2057edd16368SStephen M. Cameron 			this_device = currentsd[ncurrent];
2058edd16368SStephen M. Cameron 		}
2059edd16368SStephen M. Cameron 
2060edd16368SStephen M. Cameron 		*this_device = *tmpdevice;
2061edd16368SStephen M. Cameron 
2062edd16368SStephen M. Cameron 		switch (this_device->devtype) {
20630b0e1d6cSStephen M. Cameron 		case TYPE_ROM:
2064edd16368SStephen M. Cameron 			/* We don't *really* support actual CD-ROM devices,
2065edd16368SStephen M. Cameron 			 * just "One Button Disaster Recovery" tape drive
2066edd16368SStephen M. Cameron 			 * which temporarily pretends to be a CD-ROM drive.
2067edd16368SStephen M. Cameron 			 * So we check that the device is really an OBDR tape
2068edd16368SStephen M. Cameron 			 * device by checking for "$DR-10" in bytes 43-48 of
2069edd16368SStephen M. Cameron 			 * the inquiry data.
2070edd16368SStephen M. Cameron 			 */
20710b0e1d6cSStephen M. Cameron 			if (is_OBDR)
2072edd16368SStephen M. Cameron 				ncurrent++;
2073edd16368SStephen M. Cameron 			break;
2074edd16368SStephen M. Cameron 		case TYPE_DISK:
2075edd16368SStephen M. Cameron 			if (i < nphysicals)
2076edd16368SStephen M. Cameron 				break;
2077edd16368SStephen M. Cameron 			ncurrent++;
2078edd16368SStephen M. Cameron 			break;
2079edd16368SStephen M. Cameron 		case TYPE_TAPE:
2080edd16368SStephen M. Cameron 		case TYPE_MEDIUM_CHANGER:
2081edd16368SStephen M. Cameron 			ncurrent++;
2082edd16368SStephen M. Cameron 			break;
2083edd16368SStephen M. Cameron 		case TYPE_RAID:
2084edd16368SStephen M. Cameron 			/* Only present the Smartarray HBA as a RAID controller.
2085edd16368SStephen M. Cameron 			 * If it's a RAID controller other than the HBA itself
2086edd16368SStephen M. Cameron 			 * (an external RAID controller, MSA500 or similar)
2087edd16368SStephen M. Cameron 			 * don't present it.
2088edd16368SStephen M. Cameron 			 */
2089edd16368SStephen M. Cameron 			if (!is_hba_lunid(lunaddrbytes))
2090edd16368SStephen M. Cameron 				break;
2091edd16368SStephen M. Cameron 			ncurrent++;
2092edd16368SStephen M. Cameron 			break;
2093edd16368SStephen M. Cameron 		default:
2094edd16368SStephen M. Cameron 			break;
2095edd16368SStephen M. Cameron 		}
2096cfe5badcSScott Teel 		if (ncurrent >= HPSA_MAX_DEVICES)
2097edd16368SStephen M. Cameron 			break;
2098edd16368SStephen M. Cameron 	}
2099edd16368SStephen M. Cameron 	adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
2100edd16368SStephen M. Cameron out:
2101edd16368SStephen M. Cameron 	kfree(tmpdevice);
2102edd16368SStephen M. Cameron 	for (i = 0; i < ndev_allocated; i++)
2103edd16368SStephen M. Cameron 		kfree(currentsd[i]);
2104edd16368SStephen M. Cameron 	kfree(currentsd);
2105edd16368SStephen M. Cameron 	kfree(physdev_list);
2106edd16368SStephen M. Cameron 	kfree(logdev_list);
2107edd16368SStephen M. Cameron }
2108edd16368SStephen M. Cameron 
2109edd16368SStephen M. Cameron /* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
2110edd16368SStephen M. Cameron  * dma mapping  and fills in the scatter gather entries of the
2111edd16368SStephen M. Cameron  * hpsa command, cp.
2112edd16368SStephen M. Cameron  */
211333a2ffceSStephen M. Cameron static int hpsa_scatter_gather(struct ctlr_info *h,
2114edd16368SStephen M. Cameron 		struct CommandList *cp,
2115edd16368SStephen M. Cameron 		struct scsi_cmnd *cmd)
2116edd16368SStephen M. Cameron {
2117edd16368SStephen M. Cameron 	unsigned int len;
2118edd16368SStephen M. Cameron 	struct scatterlist *sg;
211901a02ffcSStephen M. Cameron 	u64 addr64;
212033a2ffceSStephen M. Cameron 	int use_sg, i, sg_index, chained;
212133a2ffceSStephen M. Cameron 	struct SGDescriptor *curr_sg;
2122edd16368SStephen M. Cameron 
212333a2ffceSStephen M. Cameron 	BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
2124edd16368SStephen M. Cameron 
2125edd16368SStephen M. Cameron 	use_sg = scsi_dma_map(cmd);
2126edd16368SStephen M. Cameron 	if (use_sg < 0)
2127edd16368SStephen M. Cameron 		return use_sg;
2128edd16368SStephen M. Cameron 
2129edd16368SStephen M. Cameron 	if (!use_sg)
2130edd16368SStephen M. Cameron 		goto sglist_finished;
2131edd16368SStephen M. Cameron 
213233a2ffceSStephen M. Cameron 	curr_sg = cp->SG;
213333a2ffceSStephen M. Cameron 	chained = 0;
213433a2ffceSStephen M. Cameron 	sg_index = 0;
2135edd16368SStephen M. Cameron 	scsi_for_each_sg(cmd, sg, use_sg, i) {
213633a2ffceSStephen M. Cameron 		if (i == h->max_cmd_sg_entries - 1 &&
213733a2ffceSStephen M. Cameron 			use_sg > h->max_cmd_sg_entries) {
213833a2ffceSStephen M. Cameron 			chained = 1;
213933a2ffceSStephen M. Cameron 			curr_sg = h->cmd_sg_list[cp->cmdindex];
214033a2ffceSStephen M. Cameron 			sg_index = 0;
214133a2ffceSStephen M. Cameron 		}
214201a02ffcSStephen M. Cameron 		addr64 = (u64) sg_dma_address(sg);
2143edd16368SStephen M. Cameron 		len  = sg_dma_len(sg);
214433a2ffceSStephen M. Cameron 		curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
214533a2ffceSStephen M. Cameron 		curr_sg->Addr.upper = (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
214633a2ffceSStephen M. Cameron 		curr_sg->Len = len;
214733a2ffceSStephen M. Cameron 		curr_sg->Ext = 0;  /* we are not chaining */
214833a2ffceSStephen M. Cameron 		curr_sg++;
214933a2ffceSStephen M. Cameron 	}
215033a2ffceSStephen M. Cameron 
215133a2ffceSStephen M. Cameron 	if (use_sg + chained > h->maxSG)
215233a2ffceSStephen M. Cameron 		h->maxSG = use_sg + chained;
215333a2ffceSStephen M. Cameron 
215433a2ffceSStephen M. Cameron 	if (chained) {
215533a2ffceSStephen M. Cameron 		cp->Header.SGList = h->max_cmd_sg_entries;
215633a2ffceSStephen M. Cameron 		cp->Header.SGTotal = (u16) (use_sg + 1);
2157e2bea6dfSStephen M. Cameron 		if (hpsa_map_sg_chain_block(h, cp)) {
2158e2bea6dfSStephen M. Cameron 			scsi_dma_unmap(cmd);
2159e2bea6dfSStephen M. Cameron 			return -1;
2160e2bea6dfSStephen M. Cameron 		}
216133a2ffceSStephen M. Cameron 		return 0;
2162edd16368SStephen M. Cameron 	}
2163edd16368SStephen M. Cameron 
2164edd16368SStephen M. Cameron sglist_finished:
2165edd16368SStephen M. Cameron 
216601a02ffcSStephen M. Cameron 	cp->Header.SGList = (u8) use_sg;   /* no. SGs contig in this cmd */
216701a02ffcSStephen M. Cameron 	cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */
2168edd16368SStephen M. Cameron 	return 0;
2169edd16368SStephen M. Cameron }
2170edd16368SStephen M. Cameron 
2171edd16368SStephen M. Cameron 
2172f281233dSJeff Garzik static int hpsa_scsi_queue_command_lck(struct scsi_cmnd *cmd,
2173edd16368SStephen M. Cameron 	void (*done)(struct scsi_cmnd *))
2174edd16368SStephen M. Cameron {
2175edd16368SStephen M. Cameron 	struct ctlr_info *h;
2176edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
2177edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
2178edd16368SStephen M. Cameron 	struct CommandList *c;
2179edd16368SStephen M. Cameron 	unsigned long flags;
2180edd16368SStephen M. Cameron 
2181edd16368SStephen M. Cameron 	/* Get the ptr to our adapter structure out of cmd->host. */
2182edd16368SStephen M. Cameron 	h = sdev_to_hba(cmd->device);
2183edd16368SStephen M. Cameron 	dev = cmd->device->hostdata;
2184edd16368SStephen M. Cameron 	if (!dev) {
2185edd16368SStephen M. Cameron 		cmd->result = DID_NO_CONNECT << 16;
2186edd16368SStephen M. Cameron 		done(cmd);
2187edd16368SStephen M. Cameron 		return 0;
2188edd16368SStephen M. Cameron 	}
2189edd16368SStephen M. Cameron 	memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
2190edd16368SStephen M. Cameron 
2191edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
2192a0c12413SStephen M. Cameron 	if (unlikely(h->lockup_detected)) {
2193a0c12413SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
2194a0c12413SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2195a0c12413SStephen M. Cameron 		done(cmd);
2196a0c12413SStephen M. Cameron 		return 0;
2197a0c12413SStephen M. Cameron 	}
2198edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
2199e16a33adSMatt Gates 	c = cmd_alloc(h);
2200edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
2201edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
2202edd16368SStephen M. Cameron 		return SCSI_MLQUEUE_HOST_BUSY;
2203edd16368SStephen M. Cameron 	}
2204edd16368SStephen M. Cameron 
2205edd16368SStephen M. Cameron 	/* Fill in the command list header */
2206edd16368SStephen M. Cameron 
2207edd16368SStephen M. Cameron 	cmd->scsi_done = done;    /* save this for use by completion code */
2208edd16368SStephen M. Cameron 
2209edd16368SStephen M. Cameron 	/* save c in case we have to abort it  */
2210edd16368SStephen M. Cameron 	cmd->host_scribble = (unsigned char *) c;
2211edd16368SStephen M. Cameron 
2212edd16368SStephen M. Cameron 	c->cmd_type = CMD_SCSI;
2213edd16368SStephen M. Cameron 	c->scsi_cmd = cmd;
2214edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;  /* unused in simple mode */
2215edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
2216303932fdSDon Brace 	c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT);
2217303932fdSDon Brace 	c->Header.Tag.lower |= DIRECT_LOOKUP_BIT;
2218edd16368SStephen M. Cameron 
2219edd16368SStephen M. Cameron 	/* Fill in the request block... */
2220edd16368SStephen M. Cameron 
2221edd16368SStephen M. Cameron 	c->Request.Timeout = 0;
2222edd16368SStephen M. Cameron 	memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
2223edd16368SStephen M. Cameron 	BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
2224edd16368SStephen M. Cameron 	c->Request.CDBLen = cmd->cmd_len;
2225edd16368SStephen M. Cameron 	memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
2226edd16368SStephen M. Cameron 	c->Request.Type.Type = TYPE_CMD;
2227edd16368SStephen M. Cameron 	c->Request.Type.Attribute = ATTR_SIMPLE;
2228edd16368SStephen M. Cameron 	switch (cmd->sc_data_direction) {
2229edd16368SStephen M. Cameron 	case DMA_TO_DEVICE:
2230edd16368SStephen M. Cameron 		c->Request.Type.Direction = XFER_WRITE;
2231edd16368SStephen M. Cameron 		break;
2232edd16368SStephen M. Cameron 	case DMA_FROM_DEVICE:
2233edd16368SStephen M. Cameron 		c->Request.Type.Direction = XFER_READ;
2234edd16368SStephen M. Cameron 		break;
2235edd16368SStephen M. Cameron 	case DMA_NONE:
2236edd16368SStephen M. Cameron 		c->Request.Type.Direction = XFER_NONE;
2237edd16368SStephen M. Cameron 		break;
2238edd16368SStephen M. Cameron 	case DMA_BIDIRECTIONAL:
2239edd16368SStephen M. Cameron 		/* This can happen if a buggy application does a scsi passthru
2240edd16368SStephen M. Cameron 		 * and sets both inlen and outlen to non-zero. ( see
2241edd16368SStephen M. Cameron 		 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
2242edd16368SStephen M. Cameron 		 */
2243edd16368SStephen M. Cameron 
2244edd16368SStephen M. Cameron 		c->Request.Type.Direction = XFER_RSVD;
2245edd16368SStephen M. Cameron 		/* This is technically wrong, and hpsa controllers should
2246edd16368SStephen M. Cameron 		 * reject it with CMD_INVALID, which is the most correct
2247edd16368SStephen M. Cameron 		 * response, but non-fibre backends appear to let it
2248edd16368SStephen M. Cameron 		 * slide by, and give the same results as if this field
2249edd16368SStephen M. Cameron 		 * were set correctly.  Either way is acceptable for
2250edd16368SStephen M. Cameron 		 * our purposes here.
2251edd16368SStephen M. Cameron 		 */
2252edd16368SStephen M. Cameron 
2253edd16368SStephen M. Cameron 		break;
2254edd16368SStephen M. Cameron 
2255edd16368SStephen M. Cameron 	default:
2256edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "unknown data direction: %d\n",
2257edd16368SStephen M. Cameron 			cmd->sc_data_direction);
2258edd16368SStephen M. Cameron 		BUG();
2259edd16368SStephen M. Cameron 		break;
2260edd16368SStephen M. Cameron 	}
2261edd16368SStephen M. Cameron 
226233a2ffceSStephen M. Cameron 	if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
2263edd16368SStephen M. Cameron 		cmd_free(h, c);
2264edd16368SStephen M. Cameron 		return SCSI_MLQUEUE_HOST_BUSY;
2265edd16368SStephen M. Cameron 	}
2266edd16368SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
2267edd16368SStephen M. Cameron 	/* the cmd'll come back via intr handler in complete_scsi_command()  */
2268edd16368SStephen M. Cameron 	return 0;
2269edd16368SStephen M. Cameron }
2270edd16368SStephen M. Cameron 
2271f281233dSJeff Garzik static DEF_SCSI_QCMD(hpsa_scsi_queue_command)
2272f281233dSJeff Garzik 
2273a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *sh)
2274a08a8471SStephen M. Cameron {
2275a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
2276a08a8471SStephen M. Cameron 	unsigned long flags;
2277a08a8471SStephen M. Cameron 
2278a08a8471SStephen M. Cameron 	/* wait until any scan already in progress is finished. */
2279a08a8471SStephen M. Cameron 	while (1) {
2280a08a8471SStephen M. Cameron 		spin_lock_irqsave(&h->scan_lock, flags);
2281a08a8471SStephen M. Cameron 		if (h->scan_finished)
2282a08a8471SStephen M. Cameron 			break;
2283a08a8471SStephen M. Cameron 		spin_unlock_irqrestore(&h->scan_lock, flags);
2284a08a8471SStephen M. Cameron 		wait_event(h->scan_wait_queue, h->scan_finished);
2285a08a8471SStephen M. Cameron 		/* Note: We don't need to worry about a race between this
2286a08a8471SStephen M. Cameron 		 * thread and driver unload because the midlayer will
2287a08a8471SStephen M. Cameron 		 * have incremented the reference count, so unload won't
2288a08a8471SStephen M. Cameron 		 * happen if we're in here.
2289a08a8471SStephen M. Cameron 		 */
2290a08a8471SStephen M. Cameron 	}
2291a08a8471SStephen M. Cameron 	h->scan_finished = 0; /* mark scan as in progress */
2292a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
2293a08a8471SStephen M. Cameron 
2294a08a8471SStephen M. Cameron 	hpsa_update_scsi_devices(h, h->scsi_host->host_no);
2295a08a8471SStephen M. Cameron 
2296a08a8471SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
2297a08a8471SStephen M. Cameron 	h->scan_finished = 1; /* mark scan as finished. */
2298a08a8471SStephen M. Cameron 	wake_up_all(&h->scan_wait_queue);
2299a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
2300a08a8471SStephen M. Cameron }
2301a08a8471SStephen M. Cameron 
2302a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
2303a08a8471SStephen M. Cameron 	unsigned long elapsed_time)
2304a08a8471SStephen M. Cameron {
2305a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
2306a08a8471SStephen M. Cameron 	unsigned long flags;
2307a08a8471SStephen M. Cameron 	int finished;
2308a08a8471SStephen M. Cameron 
2309a08a8471SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
2310a08a8471SStephen M. Cameron 	finished = h->scan_finished;
2311a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
2312a08a8471SStephen M. Cameron 	return finished;
2313a08a8471SStephen M. Cameron }
2314a08a8471SStephen M. Cameron 
2315667e23d4SStephen M. Cameron static int hpsa_change_queue_depth(struct scsi_device *sdev,
2316667e23d4SStephen M. Cameron 	int qdepth, int reason)
2317667e23d4SStephen M. Cameron {
2318667e23d4SStephen M. Cameron 	struct ctlr_info *h = sdev_to_hba(sdev);
2319667e23d4SStephen M. Cameron 
2320667e23d4SStephen M. Cameron 	if (reason != SCSI_QDEPTH_DEFAULT)
2321667e23d4SStephen M. Cameron 		return -ENOTSUPP;
2322667e23d4SStephen M. Cameron 
2323667e23d4SStephen M. Cameron 	if (qdepth < 1)
2324667e23d4SStephen M. Cameron 		qdepth = 1;
2325667e23d4SStephen M. Cameron 	else
2326667e23d4SStephen M. Cameron 		if (qdepth > h->nr_cmds)
2327667e23d4SStephen M. Cameron 			qdepth = h->nr_cmds;
2328667e23d4SStephen M. Cameron 	scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
2329667e23d4SStephen M. Cameron 	return sdev->queue_depth;
2330667e23d4SStephen M. Cameron }
2331667e23d4SStephen M. Cameron 
2332edd16368SStephen M. Cameron static void hpsa_unregister_scsi(struct ctlr_info *h)
2333edd16368SStephen M. Cameron {
2334edd16368SStephen M. Cameron 	/* we are being forcibly unloaded, and may not refuse. */
2335edd16368SStephen M. Cameron 	scsi_remove_host(h->scsi_host);
2336edd16368SStephen M. Cameron 	scsi_host_put(h->scsi_host);
2337edd16368SStephen M. Cameron 	h->scsi_host = NULL;
2338edd16368SStephen M. Cameron }
2339edd16368SStephen M. Cameron 
2340edd16368SStephen M. Cameron static int hpsa_register_scsi(struct ctlr_info *h)
2341edd16368SStephen M. Cameron {
2342b705690dSStephen M. Cameron 	struct Scsi_Host *sh;
2343b705690dSStephen M. Cameron 	int error;
2344edd16368SStephen M. Cameron 
2345b705690dSStephen M. Cameron 	sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
2346b705690dSStephen M. Cameron 	if (sh == NULL)
2347b705690dSStephen M. Cameron 		goto fail;
2348b705690dSStephen M. Cameron 
2349b705690dSStephen M. Cameron 	sh->io_port = 0;
2350b705690dSStephen M. Cameron 	sh->n_io_port = 0;
2351b705690dSStephen M. Cameron 	sh->this_id = -1;
2352b705690dSStephen M. Cameron 	sh->max_channel = 3;
2353b705690dSStephen M. Cameron 	sh->max_cmd_len = MAX_COMMAND_SIZE;
2354b705690dSStephen M. Cameron 	sh->max_lun = HPSA_MAX_LUN;
2355b705690dSStephen M. Cameron 	sh->max_id = HPSA_MAX_LUN;
2356b705690dSStephen M. Cameron 	sh->can_queue = h->nr_cmds;
2357b705690dSStephen M. Cameron 	sh->cmd_per_lun = h->nr_cmds;
2358b705690dSStephen M. Cameron 	sh->sg_tablesize = h->maxsgentries;
2359b705690dSStephen M. Cameron 	h->scsi_host = sh;
2360b705690dSStephen M. Cameron 	sh->hostdata[0] = (unsigned long) h;
2361b705690dSStephen M. Cameron 	sh->irq = h->intr[h->intr_mode];
2362b705690dSStephen M. Cameron 	sh->unique_id = sh->irq;
2363b705690dSStephen M. Cameron 	error = scsi_add_host(sh, &h->pdev->dev);
2364b705690dSStephen M. Cameron 	if (error)
2365b705690dSStephen M. Cameron 		goto fail_host_put;
2366b705690dSStephen M. Cameron 	scsi_scan_host(sh);
2367b705690dSStephen M. Cameron 	return 0;
2368b705690dSStephen M. Cameron 
2369b705690dSStephen M. Cameron  fail_host_put:
2370b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_add_host"
2371b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
2372b705690dSStephen M. Cameron 	scsi_host_put(sh);
2373b705690dSStephen M. Cameron 	return error;
2374b705690dSStephen M. Cameron  fail:
2375b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_host_alloc"
2376b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
2377b705690dSStephen M. Cameron 	return -ENOMEM;
2378edd16368SStephen M. Cameron }
2379edd16368SStephen M. Cameron 
2380edd16368SStephen M. Cameron static int wait_for_device_to_become_ready(struct ctlr_info *h,
2381edd16368SStephen M. Cameron 	unsigned char lunaddr[])
2382edd16368SStephen M. Cameron {
2383edd16368SStephen M. Cameron 	int rc = 0;
2384edd16368SStephen M. Cameron 	int count = 0;
2385edd16368SStephen M. Cameron 	int waittime = 1; /* seconds */
2386edd16368SStephen M. Cameron 	struct CommandList *c;
2387edd16368SStephen M. Cameron 
2388edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
2389edd16368SStephen M. Cameron 	if (!c) {
2390edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in "
2391edd16368SStephen M. Cameron 			"wait_for_device_to_become_ready.\n");
2392edd16368SStephen M. Cameron 		return IO_ERROR;
2393edd16368SStephen M. Cameron 	}
2394edd16368SStephen M. Cameron 
2395edd16368SStephen M. Cameron 	/* Send test unit ready until device ready, or give up. */
2396edd16368SStephen M. Cameron 	while (count < HPSA_TUR_RETRY_LIMIT) {
2397edd16368SStephen M. Cameron 
2398edd16368SStephen M. Cameron 		/* Wait for a bit.  do this first, because if we send
2399edd16368SStephen M. Cameron 		 * the TUR right away, the reset will just abort it.
2400edd16368SStephen M. Cameron 		 */
2401edd16368SStephen M. Cameron 		msleep(1000 * waittime);
2402edd16368SStephen M. Cameron 		count++;
2403edd16368SStephen M. Cameron 
2404edd16368SStephen M. Cameron 		/* Increase wait time with each try, up to a point. */
2405edd16368SStephen M. Cameron 		if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
2406edd16368SStephen M. Cameron 			waittime = waittime * 2;
2407edd16368SStephen M. Cameron 
2408a2dac136SStephen M. Cameron 		/* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
2409a2dac136SStephen M. Cameron 		(void) fill_cmd(c, TEST_UNIT_READY, h,
2410a2dac136SStephen M. Cameron 				NULL, 0, 0, lunaddr, TYPE_CMD);
2411edd16368SStephen M. Cameron 		hpsa_scsi_do_simple_cmd_core(h, c);
2412edd16368SStephen M. Cameron 		/* no unmap needed here because no data xfer. */
2413edd16368SStephen M. Cameron 
2414edd16368SStephen M. Cameron 		if (c->err_info->CommandStatus == CMD_SUCCESS)
2415edd16368SStephen M. Cameron 			break;
2416edd16368SStephen M. Cameron 
2417edd16368SStephen M. Cameron 		if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
2418edd16368SStephen M. Cameron 			c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
2419edd16368SStephen M. Cameron 			(c->err_info->SenseInfo[2] == NO_SENSE ||
2420edd16368SStephen M. Cameron 			c->err_info->SenseInfo[2] == UNIT_ATTENTION))
2421edd16368SStephen M. Cameron 			break;
2422edd16368SStephen M. Cameron 
2423edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "waiting %d secs "
2424edd16368SStephen M. Cameron 			"for device to become ready.\n", waittime);
2425edd16368SStephen M. Cameron 		rc = 1; /* device not ready. */
2426edd16368SStephen M. Cameron 	}
2427edd16368SStephen M. Cameron 
2428edd16368SStephen M. Cameron 	if (rc)
2429edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "giving up on device.\n");
2430edd16368SStephen M. Cameron 	else
2431edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "device is ready.\n");
2432edd16368SStephen M. Cameron 
2433edd16368SStephen M. Cameron 	cmd_special_free(h, c);
2434edd16368SStephen M. Cameron 	return rc;
2435edd16368SStephen M. Cameron }
2436edd16368SStephen M. Cameron 
2437edd16368SStephen M. Cameron /* Need at least one of these error handlers to keep ../scsi/hosts.c from
2438edd16368SStephen M. Cameron  * complaining.  Doing a host- or bus-reset can't do anything good here.
2439edd16368SStephen M. Cameron  */
2440edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
2441edd16368SStephen M. Cameron {
2442edd16368SStephen M. Cameron 	int rc;
2443edd16368SStephen M. Cameron 	struct ctlr_info *h;
2444edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
2445edd16368SStephen M. Cameron 
2446edd16368SStephen M. Cameron 	/* find the controller to which the command to be aborted was sent */
2447edd16368SStephen M. Cameron 	h = sdev_to_hba(scsicmd->device);
2448edd16368SStephen M. Cameron 	if (h == NULL) /* paranoia */
2449edd16368SStephen M. Cameron 		return FAILED;
2450edd16368SStephen M. Cameron 	dev = scsicmd->device->hostdata;
2451edd16368SStephen M. Cameron 	if (!dev) {
2452edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
2453edd16368SStephen M. Cameron 			"device lookup failed.\n");
2454edd16368SStephen M. Cameron 		return FAILED;
2455edd16368SStephen M. Cameron 	}
2456d416b0c7SStephen M. Cameron 	dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
2457d416b0c7SStephen M. Cameron 		h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
2458edd16368SStephen M. Cameron 	/* send a reset to the SCSI LUN which the command was sent to */
2459edd16368SStephen M. Cameron 	rc = hpsa_send_reset(h, dev->scsi3addr);
2460edd16368SStephen M. Cameron 	if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
2461edd16368SStephen M. Cameron 		return SUCCESS;
2462edd16368SStephen M. Cameron 
2463edd16368SStephen M. Cameron 	dev_warn(&h->pdev->dev, "resetting device failed.\n");
2464edd16368SStephen M. Cameron 	return FAILED;
2465edd16368SStephen M. Cameron }
2466edd16368SStephen M. Cameron 
24676cba3f19SStephen M. Cameron static void swizzle_abort_tag(u8 *tag)
24686cba3f19SStephen M. Cameron {
24696cba3f19SStephen M. Cameron 	u8 original_tag[8];
24706cba3f19SStephen M. Cameron 
24716cba3f19SStephen M. Cameron 	memcpy(original_tag, tag, 8);
24726cba3f19SStephen M. Cameron 	tag[0] = original_tag[3];
24736cba3f19SStephen M. Cameron 	tag[1] = original_tag[2];
24746cba3f19SStephen M. Cameron 	tag[2] = original_tag[1];
24756cba3f19SStephen M. Cameron 	tag[3] = original_tag[0];
24766cba3f19SStephen M. Cameron 	tag[4] = original_tag[7];
24776cba3f19SStephen M. Cameron 	tag[5] = original_tag[6];
24786cba3f19SStephen M. Cameron 	tag[6] = original_tag[5];
24796cba3f19SStephen M. Cameron 	tag[7] = original_tag[4];
24806cba3f19SStephen M. Cameron }
24816cba3f19SStephen M. Cameron 
248275167d2cSStephen M. Cameron static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
24836cba3f19SStephen M. Cameron 	struct CommandList *abort, int swizzle)
248475167d2cSStephen M. Cameron {
248575167d2cSStephen M. Cameron 	int rc = IO_OK;
248675167d2cSStephen M. Cameron 	struct CommandList *c;
248775167d2cSStephen M. Cameron 	struct ErrorInfo *ei;
248875167d2cSStephen M. Cameron 
248975167d2cSStephen M. Cameron 	c = cmd_special_alloc(h);
249075167d2cSStephen M. Cameron 	if (c == NULL) {	/* trouble... */
249175167d2cSStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
249275167d2cSStephen M. Cameron 		return -ENOMEM;
249375167d2cSStephen M. Cameron 	}
249475167d2cSStephen M. Cameron 
2495a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no buffer to map */
2496a2dac136SStephen M. Cameron 	(void) fill_cmd(c, HPSA_ABORT_MSG, h, abort,
2497a2dac136SStephen M. Cameron 		0, 0, scsi3addr, TYPE_MSG);
24986cba3f19SStephen M. Cameron 	if (swizzle)
24996cba3f19SStephen M. Cameron 		swizzle_abort_tag(&c->Request.CDB[4]);
250075167d2cSStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core(h, c);
250175167d2cSStephen M. Cameron 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd_core completed.\n",
250275167d2cSStephen M. Cameron 		__func__, abort->Header.Tag.upper, abort->Header.Tag.lower);
250375167d2cSStephen M. Cameron 	/* no unmap needed here because no data xfer. */
250475167d2cSStephen M. Cameron 
250575167d2cSStephen M. Cameron 	ei = c->err_info;
250675167d2cSStephen M. Cameron 	switch (ei->CommandStatus) {
250775167d2cSStephen M. Cameron 	case CMD_SUCCESS:
250875167d2cSStephen M. Cameron 		break;
250975167d2cSStephen M. Cameron 	case CMD_UNABORTABLE: /* Very common, don't make noise. */
251075167d2cSStephen M. Cameron 		rc = -1;
251175167d2cSStephen M. Cameron 		break;
251275167d2cSStephen M. Cameron 	default:
251375167d2cSStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
251475167d2cSStephen M. Cameron 			__func__, abort->Header.Tag.upper,
251575167d2cSStephen M. Cameron 			abort->Header.Tag.lower);
251675167d2cSStephen M. Cameron 		hpsa_scsi_interpret_error(c);
251775167d2cSStephen M. Cameron 		rc = -1;
251875167d2cSStephen M. Cameron 		break;
251975167d2cSStephen M. Cameron 	}
252075167d2cSStephen M. Cameron 	cmd_special_free(h, c);
252175167d2cSStephen M. Cameron 	dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n", __func__,
252275167d2cSStephen M. Cameron 		abort->Header.Tag.upper, abort->Header.Tag.lower);
252375167d2cSStephen M. Cameron 	return rc;
252475167d2cSStephen M. Cameron }
252575167d2cSStephen M. Cameron 
252675167d2cSStephen M. Cameron /*
252775167d2cSStephen M. Cameron  * hpsa_find_cmd_in_queue
252875167d2cSStephen M. Cameron  *
252975167d2cSStephen M. Cameron  * Used to determine whether a command (find) is still present
253075167d2cSStephen M. Cameron  * in queue_head.   Optionally excludes the last element of queue_head.
253175167d2cSStephen M. Cameron  *
253275167d2cSStephen M. Cameron  * This is used to avoid unnecessary aborts.  Commands in h->reqQ have
253375167d2cSStephen M. Cameron  * not yet been submitted, and so can be aborted by the driver without
253475167d2cSStephen M. Cameron  * sending an abort to the hardware.
253575167d2cSStephen M. Cameron  *
253675167d2cSStephen M. Cameron  * Returns pointer to command if found in queue, NULL otherwise.
253775167d2cSStephen M. Cameron  */
253875167d2cSStephen M. Cameron static struct CommandList *hpsa_find_cmd_in_queue(struct ctlr_info *h,
253975167d2cSStephen M. Cameron 			struct scsi_cmnd *find, struct list_head *queue_head)
254075167d2cSStephen M. Cameron {
254175167d2cSStephen M. Cameron 	unsigned long flags;
254275167d2cSStephen M. Cameron 	struct CommandList *c = NULL;	/* ptr into cmpQ */
254375167d2cSStephen M. Cameron 
254475167d2cSStephen M. Cameron 	if (!find)
254575167d2cSStephen M. Cameron 		return 0;
254675167d2cSStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
254775167d2cSStephen M. Cameron 	list_for_each_entry(c, queue_head, list) {
254875167d2cSStephen M. Cameron 		if (c->scsi_cmd == NULL) /* e.g.: passthru ioctl */
254975167d2cSStephen M. Cameron 			continue;
255075167d2cSStephen M. Cameron 		if (c->scsi_cmd == find) {
255175167d2cSStephen M. Cameron 			spin_unlock_irqrestore(&h->lock, flags);
255275167d2cSStephen M. Cameron 			return c;
255375167d2cSStephen M. Cameron 		}
255475167d2cSStephen M. Cameron 	}
255575167d2cSStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
255675167d2cSStephen M. Cameron 	return NULL;
255775167d2cSStephen M. Cameron }
255875167d2cSStephen M. Cameron 
25596cba3f19SStephen M. Cameron static struct CommandList *hpsa_find_cmd_in_queue_by_tag(struct ctlr_info *h,
25606cba3f19SStephen M. Cameron 					u8 *tag, struct list_head *queue_head)
25616cba3f19SStephen M. Cameron {
25626cba3f19SStephen M. Cameron 	unsigned long flags;
25636cba3f19SStephen M. Cameron 	struct CommandList *c;
25646cba3f19SStephen M. Cameron 
25656cba3f19SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
25666cba3f19SStephen M. Cameron 	list_for_each_entry(c, queue_head, list) {
25676cba3f19SStephen M. Cameron 		if (memcmp(&c->Header.Tag, tag, 8) != 0)
25686cba3f19SStephen M. Cameron 			continue;
25696cba3f19SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
25706cba3f19SStephen M. Cameron 		return c;
25716cba3f19SStephen M. Cameron 	}
25726cba3f19SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
25736cba3f19SStephen M. Cameron 	return NULL;
25746cba3f19SStephen M. Cameron }
25756cba3f19SStephen M. Cameron 
25766cba3f19SStephen M. Cameron /* Some Smart Arrays need the abort tag swizzled, and some don't.  It's hard to
25776cba3f19SStephen M. Cameron  * tell which kind we're dealing with, so we send the abort both ways.  There
25786cba3f19SStephen M. Cameron  * shouldn't be any collisions between swizzled and unswizzled tags due to the
25796cba3f19SStephen M. Cameron  * way we construct our tags but we check anyway in case the assumptions which
25806cba3f19SStephen M. Cameron  * make this true someday become false.
25816cba3f19SStephen M. Cameron  */
25826cba3f19SStephen M. Cameron static int hpsa_send_abort_both_ways(struct ctlr_info *h,
25836cba3f19SStephen M. Cameron 	unsigned char *scsi3addr, struct CommandList *abort)
25846cba3f19SStephen M. Cameron {
25856cba3f19SStephen M. Cameron 	u8 swizzled_tag[8];
25866cba3f19SStephen M. Cameron 	struct CommandList *c;
25876cba3f19SStephen M. Cameron 	int rc = 0, rc2 = 0;
25886cba3f19SStephen M. Cameron 
25896cba3f19SStephen M. Cameron 	/* we do not expect to find the swizzled tag in our queue, but
25906cba3f19SStephen M. Cameron 	 * check anyway just to be sure the assumptions which make this
25916cba3f19SStephen M. Cameron 	 * the case haven't become wrong.
25926cba3f19SStephen M. Cameron 	 */
25936cba3f19SStephen M. Cameron 	memcpy(swizzled_tag, &abort->Request.CDB[4], 8);
25946cba3f19SStephen M. Cameron 	swizzle_abort_tag(swizzled_tag);
25956cba3f19SStephen M. Cameron 	c = hpsa_find_cmd_in_queue_by_tag(h, swizzled_tag, &h->cmpQ);
25966cba3f19SStephen M. Cameron 	if (c != NULL) {
25976cba3f19SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Unexpectedly found byte-swapped tag in completion queue.\n");
25986cba3f19SStephen M. Cameron 		return hpsa_send_abort(h, scsi3addr, abort, 0);
25996cba3f19SStephen M. Cameron 	}
26006cba3f19SStephen M. Cameron 	rc = hpsa_send_abort(h, scsi3addr, abort, 0);
26016cba3f19SStephen M. Cameron 
26026cba3f19SStephen M. Cameron 	/* if the command is still in our queue, we can't conclude that it was
26036cba3f19SStephen M. Cameron 	 * aborted (it might have just completed normally) but in any case
26046cba3f19SStephen M. Cameron 	 * we don't need to try to abort it another way.
26056cba3f19SStephen M. Cameron 	 */
26066cba3f19SStephen M. Cameron 	c = hpsa_find_cmd_in_queue(h, abort->scsi_cmd, &h->cmpQ);
26076cba3f19SStephen M. Cameron 	if (c)
26086cba3f19SStephen M. Cameron 		rc2 = hpsa_send_abort(h, scsi3addr, abort, 1);
26096cba3f19SStephen M. Cameron 	return rc && rc2;
26106cba3f19SStephen M. Cameron }
26116cba3f19SStephen M. Cameron 
261275167d2cSStephen M. Cameron /* Send an abort for the specified command.
261375167d2cSStephen M. Cameron  *	If the device and controller support it,
261475167d2cSStephen M. Cameron  *		send a task abort request.
261575167d2cSStephen M. Cameron  */
261675167d2cSStephen M. Cameron static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
261775167d2cSStephen M. Cameron {
261875167d2cSStephen M. Cameron 
261975167d2cSStephen M. Cameron 	int i, rc;
262075167d2cSStephen M. Cameron 	struct ctlr_info *h;
262175167d2cSStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
262275167d2cSStephen M. Cameron 	struct CommandList *abort; /* pointer to command to be aborted */
262375167d2cSStephen M. Cameron 	struct CommandList *found;
262475167d2cSStephen M. Cameron 	struct scsi_cmnd *as;	/* ptr to scsi cmd inside aborted command. */
262575167d2cSStephen M. Cameron 	char msg[256];		/* For debug messaging. */
262675167d2cSStephen M. Cameron 	int ml = 0;
262775167d2cSStephen M. Cameron 
262875167d2cSStephen M. Cameron 	/* Find the controller of the command to be aborted */
262975167d2cSStephen M. Cameron 	h = sdev_to_hba(sc->device);
263075167d2cSStephen M. Cameron 	if (WARN(h == NULL,
263175167d2cSStephen M. Cameron 			"ABORT REQUEST FAILED, Controller lookup failed.\n"))
263275167d2cSStephen M. Cameron 		return FAILED;
263375167d2cSStephen M. Cameron 
263475167d2cSStephen M. Cameron 	/* Check that controller supports some kind of task abort */
263575167d2cSStephen M. Cameron 	if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
263675167d2cSStephen M. Cameron 		!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
263775167d2cSStephen M. Cameron 		return FAILED;
263875167d2cSStephen M. Cameron 
263975167d2cSStephen M. Cameron 	memset(msg, 0, sizeof(msg));
264075167d2cSStephen M. Cameron 	ml += sprintf(msg+ml, "ABORT REQUEST on C%d:B%d:T%d:L%d ",
264175167d2cSStephen M. Cameron 		h->scsi_host->host_no, sc->device->channel,
264275167d2cSStephen M. Cameron 		sc->device->id, sc->device->lun);
264375167d2cSStephen M. Cameron 
264475167d2cSStephen M. Cameron 	/* Find the device of the command to be aborted */
264575167d2cSStephen M. Cameron 	dev = sc->device->hostdata;
264675167d2cSStephen M. Cameron 	if (!dev) {
264775167d2cSStephen M. Cameron 		dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
264875167d2cSStephen M. Cameron 				msg);
264975167d2cSStephen M. Cameron 		return FAILED;
265075167d2cSStephen M. Cameron 	}
265175167d2cSStephen M. Cameron 
265275167d2cSStephen M. Cameron 	/* Get SCSI command to be aborted */
265375167d2cSStephen M. Cameron 	abort = (struct CommandList *) sc->host_scribble;
265475167d2cSStephen M. Cameron 	if (abort == NULL) {
265575167d2cSStephen M. Cameron 		dev_err(&h->pdev->dev, "%s FAILED, Command to abort is NULL.\n",
265675167d2cSStephen M. Cameron 				msg);
265775167d2cSStephen M. Cameron 		return FAILED;
265875167d2cSStephen M. Cameron 	}
265975167d2cSStephen M. Cameron 
266075167d2cSStephen M. Cameron 	ml += sprintf(msg+ml, "Tag:0x%08x:%08x ",
266175167d2cSStephen M. Cameron 		abort->Header.Tag.upper, abort->Header.Tag.lower);
266275167d2cSStephen M. Cameron 	as  = (struct scsi_cmnd *) abort->scsi_cmd;
266375167d2cSStephen M. Cameron 	if (as != NULL)
266475167d2cSStephen M. Cameron 		ml += sprintf(msg+ml, "Command:0x%x SN:0x%lx ",
266575167d2cSStephen M. Cameron 			as->cmnd[0], as->serial_number);
266675167d2cSStephen M. Cameron 	dev_dbg(&h->pdev->dev, "%s\n", msg);
266775167d2cSStephen M. Cameron 	dev_warn(&h->pdev->dev, "Abort request on C%d:B%d:T%d:L%d\n",
266875167d2cSStephen M. Cameron 		h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
266975167d2cSStephen M. Cameron 
267075167d2cSStephen M. Cameron 	/* Search reqQ to See if command is queued but not submitted,
267175167d2cSStephen M. Cameron 	 * if so, complete the command with aborted status and remove
267275167d2cSStephen M. Cameron 	 * it from the reqQ.
267375167d2cSStephen M. Cameron 	 */
267475167d2cSStephen M. Cameron 	found = hpsa_find_cmd_in_queue(h, sc, &h->reqQ);
267575167d2cSStephen M. Cameron 	if (found) {
267675167d2cSStephen M. Cameron 		found->err_info->CommandStatus = CMD_ABORTED;
267775167d2cSStephen M. Cameron 		finish_cmd(found);
267875167d2cSStephen M. Cameron 		dev_info(&h->pdev->dev, "%s Request SUCCEEDED (driver queue).\n",
267975167d2cSStephen M. Cameron 				msg);
268075167d2cSStephen M. Cameron 		return SUCCESS;
268175167d2cSStephen M. Cameron 	}
268275167d2cSStephen M. Cameron 
268375167d2cSStephen M. Cameron 	/* not in reqQ, if also not in cmpQ, must have already completed */
268475167d2cSStephen M. Cameron 	found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
268575167d2cSStephen M. Cameron 	if (!found)  {
2686d6ebd0f7SStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s Request SUCCEEDED (not known to driver).\n",
268775167d2cSStephen M. Cameron 				msg);
268875167d2cSStephen M. Cameron 		return SUCCESS;
268975167d2cSStephen M. Cameron 	}
269075167d2cSStephen M. Cameron 
269175167d2cSStephen M. Cameron 	/*
269275167d2cSStephen M. Cameron 	 * Command is in flight, or possibly already completed
269375167d2cSStephen M. Cameron 	 * by the firmware (but not to the scsi mid layer) but we can't
269475167d2cSStephen M. Cameron 	 * distinguish which.  Send the abort down.
269575167d2cSStephen M. Cameron 	 */
26966cba3f19SStephen M. Cameron 	rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort);
269775167d2cSStephen M. Cameron 	if (rc != 0) {
269875167d2cSStephen M. Cameron 		dev_dbg(&h->pdev->dev, "%s Request FAILED.\n", msg);
269975167d2cSStephen M. Cameron 		dev_warn(&h->pdev->dev, "FAILED abort on device C%d:B%d:T%d:L%d\n",
270075167d2cSStephen M. Cameron 			h->scsi_host->host_no,
270175167d2cSStephen M. Cameron 			dev->bus, dev->target, dev->lun);
270275167d2cSStephen M. Cameron 		return FAILED;
270375167d2cSStephen M. Cameron 	}
270475167d2cSStephen M. Cameron 	dev_info(&h->pdev->dev, "%s REQUEST SUCCEEDED.\n", msg);
270575167d2cSStephen M. Cameron 
270675167d2cSStephen M. Cameron 	/* If the abort(s) above completed and actually aborted the
270775167d2cSStephen M. Cameron 	 * command, then the command to be aborted should already be
270875167d2cSStephen M. Cameron 	 * completed.  If not, wait around a bit more to see if they
270975167d2cSStephen M. Cameron 	 * manage to complete normally.
271075167d2cSStephen M. Cameron 	 */
271175167d2cSStephen M. Cameron #define ABORT_COMPLETE_WAIT_SECS 30
271275167d2cSStephen M. Cameron 	for (i = 0; i < ABORT_COMPLETE_WAIT_SECS * 10; i++) {
271375167d2cSStephen M. Cameron 		found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
271475167d2cSStephen M. Cameron 		if (!found)
271575167d2cSStephen M. Cameron 			return SUCCESS;
271675167d2cSStephen M. Cameron 		msleep(100);
271775167d2cSStephen M. Cameron 	}
271875167d2cSStephen M. Cameron 	dev_warn(&h->pdev->dev, "%s FAILED. Aborted command has not completed after %d seconds.\n",
271975167d2cSStephen M. Cameron 		msg, ABORT_COMPLETE_WAIT_SECS);
272075167d2cSStephen M. Cameron 	return FAILED;
272175167d2cSStephen M. Cameron }
272275167d2cSStephen M. Cameron 
272375167d2cSStephen M. Cameron 
2724edd16368SStephen M. Cameron /*
2725edd16368SStephen M. Cameron  * For operations that cannot sleep, a command block is allocated at init,
2726edd16368SStephen M. Cameron  * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
2727edd16368SStephen M. Cameron  * which ones are free or in use.  Lock must be held when calling this.
2728edd16368SStephen M. Cameron  * cmd_free() is the complement.
2729edd16368SStephen M. Cameron  */
2730edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h)
2731edd16368SStephen M. Cameron {
2732edd16368SStephen M. Cameron 	struct CommandList *c;
2733edd16368SStephen M. Cameron 	int i;
2734edd16368SStephen M. Cameron 	union u64bit temp64;
2735edd16368SStephen M. Cameron 	dma_addr_t cmd_dma_handle, err_dma_handle;
2736e16a33adSMatt Gates 	unsigned long flags;
2737edd16368SStephen M. Cameron 
2738e16a33adSMatt Gates 	spin_lock_irqsave(&h->lock, flags);
2739edd16368SStephen M. Cameron 	do {
2740edd16368SStephen M. Cameron 		i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
2741e16a33adSMatt Gates 		if (i == h->nr_cmds) {
2742e16a33adSMatt Gates 			spin_unlock_irqrestore(&h->lock, flags);
2743edd16368SStephen M. Cameron 			return NULL;
2744e16a33adSMatt Gates 		}
2745edd16368SStephen M. Cameron 	} while (test_and_set_bit
2746edd16368SStephen M. Cameron 		 (i & (BITS_PER_LONG - 1),
2747edd16368SStephen M. Cameron 		  h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
2748e16a33adSMatt Gates 	spin_unlock_irqrestore(&h->lock, flags);
2749e16a33adSMatt Gates 
2750edd16368SStephen M. Cameron 	c = h->cmd_pool + i;
2751edd16368SStephen M. Cameron 	memset(c, 0, sizeof(*c));
2752edd16368SStephen M. Cameron 	cmd_dma_handle = h->cmd_pool_dhandle
2753edd16368SStephen M. Cameron 	    + i * sizeof(*c);
2754edd16368SStephen M. Cameron 	c->err_info = h->errinfo_pool + i;
2755edd16368SStephen M. Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
2756edd16368SStephen M. Cameron 	err_dma_handle = h->errinfo_pool_dhandle
2757edd16368SStephen M. Cameron 	    + i * sizeof(*c->err_info);
2758edd16368SStephen M. Cameron 
2759edd16368SStephen M. Cameron 	c->cmdindex = i;
2760edd16368SStephen M. Cameron 
27619e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&c->list);
276201a02ffcSStephen M. Cameron 	c->busaddr = (u32) cmd_dma_handle;
276301a02ffcSStephen M. Cameron 	temp64.val = (u64) err_dma_handle;
2764edd16368SStephen M. Cameron 	c->ErrDesc.Addr.lower = temp64.val32.lower;
2765edd16368SStephen M. Cameron 	c->ErrDesc.Addr.upper = temp64.val32.upper;
2766edd16368SStephen M. Cameron 	c->ErrDesc.Len = sizeof(*c->err_info);
2767edd16368SStephen M. Cameron 
2768edd16368SStephen M. Cameron 	c->h = h;
2769edd16368SStephen M. Cameron 	return c;
2770edd16368SStephen M. Cameron }
2771edd16368SStephen M. Cameron 
2772edd16368SStephen M. Cameron /* For operations that can wait for kmalloc to possibly sleep,
2773edd16368SStephen M. Cameron  * this routine can be called. Lock need not be held to call
2774edd16368SStephen M. Cameron  * cmd_special_alloc. cmd_special_free() is the complement.
2775edd16368SStephen M. Cameron  */
2776edd16368SStephen M. Cameron static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
2777edd16368SStephen M. Cameron {
2778edd16368SStephen M. Cameron 	struct CommandList *c;
2779edd16368SStephen M. Cameron 	union u64bit temp64;
2780edd16368SStephen M. Cameron 	dma_addr_t cmd_dma_handle, err_dma_handle;
2781edd16368SStephen M. Cameron 
2782edd16368SStephen M. Cameron 	c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
2783edd16368SStephen M. Cameron 	if (c == NULL)
2784edd16368SStephen M. Cameron 		return NULL;
2785edd16368SStephen M. Cameron 	memset(c, 0, sizeof(*c));
2786edd16368SStephen M. Cameron 
2787edd16368SStephen M. Cameron 	c->cmdindex = -1;
2788edd16368SStephen M. Cameron 
2789edd16368SStephen M. Cameron 	c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info),
2790edd16368SStephen M. Cameron 		    &err_dma_handle);
2791edd16368SStephen M. Cameron 
2792edd16368SStephen M. Cameron 	if (c->err_info == NULL) {
2793edd16368SStephen M. Cameron 		pci_free_consistent(h->pdev,
2794edd16368SStephen M. Cameron 			sizeof(*c), c, cmd_dma_handle);
2795edd16368SStephen M. Cameron 		return NULL;
2796edd16368SStephen M. Cameron 	}
2797edd16368SStephen M. Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
2798edd16368SStephen M. Cameron 
27999e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&c->list);
280001a02ffcSStephen M. Cameron 	c->busaddr = (u32) cmd_dma_handle;
280101a02ffcSStephen M. Cameron 	temp64.val = (u64) err_dma_handle;
2802edd16368SStephen M. Cameron 	c->ErrDesc.Addr.lower = temp64.val32.lower;
2803edd16368SStephen M. Cameron 	c->ErrDesc.Addr.upper = temp64.val32.upper;
2804edd16368SStephen M. Cameron 	c->ErrDesc.Len = sizeof(*c->err_info);
2805edd16368SStephen M. Cameron 
2806edd16368SStephen M. Cameron 	c->h = h;
2807edd16368SStephen M. Cameron 	return c;
2808edd16368SStephen M. Cameron }
2809edd16368SStephen M. Cameron 
2810edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c)
2811edd16368SStephen M. Cameron {
2812edd16368SStephen M. Cameron 	int i;
2813e16a33adSMatt Gates 	unsigned long flags;
2814edd16368SStephen M. Cameron 
2815edd16368SStephen M. Cameron 	i = c - h->cmd_pool;
2816e16a33adSMatt Gates 	spin_lock_irqsave(&h->lock, flags);
2817edd16368SStephen M. Cameron 	clear_bit(i & (BITS_PER_LONG - 1),
2818edd16368SStephen M. Cameron 		  h->cmd_pool_bits + (i / BITS_PER_LONG));
2819e16a33adSMatt Gates 	spin_unlock_irqrestore(&h->lock, flags);
2820edd16368SStephen M. Cameron }
2821edd16368SStephen M. Cameron 
2822edd16368SStephen M. Cameron static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
2823edd16368SStephen M. Cameron {
2824edd16368SStephen M. Cameron 	union u64bit temp64;
2825edd16368SStephen M. Cameron 
2826edd16368SStephen M. Cameron 	temp64.val32.lower = c->ErrDesc.Addr.lower;
2827edd16368SStephen M. Cameron 	temp64.val32.upper = c->ErrDesc.Addr.upper;
2828edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev, sizeof(*c->err_info),
2829edd16368SStephen M. Cameron 			    c->err_info, (dma_addr_t) temp64.val);
2830edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev, sizeof(*c),
2831d896f3f3SStephen M. Cameron 			    c, (dma_addr_t) (c->busaddr & DIRECT_LOOKUP_MASK));
2832edd16368SStephen M. Cameron }
2833edd16368SStephen M. Cameron 
2834edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
2835edd16368SStephen M. Cameron 
2836edd16368SStephen M. Cameron static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
2837edd16368SStephen M. Cameron {
2838edd16368SStephen M. Cameron 	IOCTL32_Command_struct __user *arg32 =
2839edd16368SStephen M. Cameron 	    (IOCTL32_Command_struct __user *) arg;
2840edd16368SStephen M. Cameron 	IOCTL_Command_struct arg64;
2841edd16368SStephen M. Cameron 	IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
2842edd16368SStephen M. Cameron 	int err;
2843edd16368SStephen M. Cameron 	u32 cp;
2844edd16368SStephen M. Cameron 
2845938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
2846edd16368SStephen M. Cameron 	err = 0;
2847edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
2848edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
2849edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
2850edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
2851edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
2852edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
2853edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
2854edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
2855edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
2856edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
2857edd16368SStephen M. Cameron 
2858edd16368SStephen M. Cameron 	if (err)
2859edd16368SStephen M. Cameron 		return -EFAULT;
2860edd16368SStephen M. Cameron 
2861e39eeaedSStephen M. Cameron 	err = hpsa_ioctl(dev, CCISS_PASSTHRU, (void *)p);
2862edd16368SStephen M. Cameron 	if (err)
2863edd16368SStephen M. Cameron 		return err;
2864edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
2865edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
2866edd16368SStephen M. Cameron 	if (err)
2867edd16368SStephen M. Cameron 		return -EFAULT;
2868edd16368SStephen M. Cameron 	return err;
2869edd16368SStephen M. Cameron }
2870edd16368SStephen M. Cameron 
2871edd16368SStephen M. Cameron static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
2872edd16368SStephen M. Cameron 	int cmd, void *arg)
2873edd16368SStephen M. Cameron {
2874edd16368SStephen M. Cameron 	BIG_IOCTL32_Command_struct __user *arg32 =
2875edd16368SStephen M. Cameron 	    (BIG_IOCTL32_Command_struct __user *) arg;
2876edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct arg64;
2877edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct __user *p =
2878edd16368SStephen M. Cameron 	    compat_alloc_user_space(sizeof(arg64));
2879edd16368SStephen M. Cameron 	int err;
2880edd16368SStephen M. Cameron 	u32 cp;
2881edd16368SStephen M. Cameron 
2882938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
2883edd16368SStephen M. Cameron 	err = 0;
2884edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
2885edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
2886edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
2887edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
2888edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
2889edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
2890edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
2891edd16368SStephen M. Cameron 	err |= get_user(arg64.malloc_size, &arg32->malloc_size);
2892edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
2893edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
2894edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
2895edd16368SStephen M. Cameron 
2896edd16368SStephen M. Cameron 	if (err)
2897edd16368SStephen M. Cameron 		return -EFAULT;
2898edd16368SStephen M. Cameron 
2899e39eeaedSStephen M. Cameron 	err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
2900edd16368SStephen M. Cameron 	if (err)
2901edd16368SStephen M. Cameron 		return err;
2902edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
2903edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
2904edd16368SStephen M. Cameron 	if (err)
2905edd16368SStephen M. Cameron 		return -EFAULT;
2906edd16368SStephen M. Cameron 	return err;
2907edd16368SStephen M. Cameron }
290871fe75a7SStephen M. Cameron 
290971fe75a7SStephen M. Cameron static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
291071fe75a7SStephen M. Cameron {
291171fe75a7SStephen M. Cameron 	switch (cmd) {
291271fe75a7SStephen M. Cameron 	case CCISS_GETPCIINFO:
291371fe75a7SStephen M. Cameron 	case CCISS_GETINTINFO:
291471fe75a7SStephen M. Cameron 	case CCISS_SETINTINFO:
291571fe75a7SStephen M. Cameron 	case CCISS_GETNODENAME:
291671fe75a7SStephen M. Cameron 	case CCISS_SETNODENAME:
291771fe75a7SStephen M. Cameron 	case CCISS_GETHEARTBEAT:
291871fe75a7SStephen M. Cameron 	case CCISS_GETBUSTYPES:
291971fe75a7SStephen M. Cameron 	case CCISS_GETFIRMVER:
292071fe75a7SStephen M. Cameron 	case CCISS_GETDRIVVER:
292171fe75a7SStephen M. Cameron 	case CCISS_REVALIDVOLS:
292271fe75a7SStephen M. Cameron 	case CCISS_DEREGDISK:
292371fe75a7SStephen M. Cameron 	case CCISS_REGNEWDISK:
292471fe75a7SStephen M. Cameron 	case CCISS_REGNEWD:
292571fe75a7SStephen M. Cameron 	case CCISS_RESCANDISK:
292671fe75a7SStephen M. Cameron 	case CCISS_GETLUNINFO:
292771fe75a7SStephen M. Cameron 		return hpsa_ioctl(dev, cmd, arg);
292871fe75a7SStephen M. Cameron 
292971fe75a7SStephen M. Cameron 	case CCISS_PASSTHRU32:
293071fe75a7SStephen M. Cameron 		return hpsa_ioctl32_passthru(dev, cmd, arg);
293171fe75a7SStephen M. Cameron 	case CCISS_BIG_PASSTHRU32:
293271fe75a7SStephen M. Cameron 		return hpsa_ioctl32_big_passthru(dev, cmd, arg);
293371fe75a7SStephen M. Cameron 
293471fe75a7SStephen M. Cameron 	default:
293571fe75a7SStephen M. Cameron 		return -ENOIOCTLCMD;
293671fe75a7SStephen M. Cameron 	}
293771fe75a7SStephen M. Cameron }
2938edd16368SStephen M. Cameron #endif
2939edd16368SStephen M. Cameron 
2940edd16368SStephen M. Cameron static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
2941edd16368SStephen M. Cameron {
2942edd16368SStephen M. Cameron 	struct hpsa_pci_info pciinfo;
2943edd16368SStephen M. Cameron 
2944edd16368SStephen M. Cameron 	if (!argp)
2945edd16368SStephen M. Cameron 		return -EINVAL;
2946edd16368SStephen M. Cameron 	pciinfo.domain = pci_domain_nr(h->pdev->bus);
2947edd16368SStephen M. Cameron 	pciinfo.bus = h->pdev->bus->number;
2948edd16368SStephen M. Cameron 	pciinfo.dev_fn = h->pdev->devfn;
2949edd16368SStephen M. Cameron 	pciinfo.board_id = h->board_id;
2950edd16368SStephen M. Cameron 	if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
2951edd16368SStephen M. Cameron 		return -EFAULT;
2952edd16368SStephen M. Cameron 	return 0;
2953edd16368SStephen M. Cameron }
2954edd16368SStephen M. Cameron 
2955edd16368SStephen M. Cameron static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
2956edd16368SStephen M. Cameron {
2957edd16368SStephen M. Cameron 	DriverVer_type DriverVer;
2958edd16368SStephen M. Cameron 	unsigned char vmaj, vmin, vsubmin;
2959edd16368SStephen M. Cameron 	int rc;
2960edd16368SStephen M. Cameron 
2961edd16368SStephen M. Cameron 	rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
2962edd16368SStephen M. Cameron 		&vmaj, &vmin, &vsubmin);
2963edd16368SStephen M. Cameron 	if (rc != 3) {
2964edd16368SStephen M. Cameron 		dev_info(&h->pdev->dev, "driver version string '%s' "
2965edd16368SStephen M. Cameron 			"unrecognized.", HPSA_DRIVER_VERSION);
2966edd16368SStephen M. Cameron 		vmaj = 0;
2967edd16368SStephen M. Cameron 		vmin = 0;
2968edd16368SStephen M. Cameron 		vsubmin = 0;
2969edd16368SStephen M. Cameron 	}
2970edd16368SStephen M. Cameron 	DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
2971edd16368SStephen M. Cameron 	if (!argp)
2972edd16368SStephen M. Cameron 		return -EINVAL;
2973edd16368SStephen M. Cameron 	if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
2974edd16368SStephen M. Cameron 		return -EFAULT;
2975edd16368SStephen M. Cameron 	return 0;
2976edd16368SStephen M. Cameron }
2977edd16368SStephen M. Cameron 
2978edd16368SStephen M. Cameron static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
2979edd16368SStephen M. Cameron {
2980edd16368SStephen M. Cameron 	IOCTL_Command_struct iocommand;
2981edd16368SStephen M. Cameron 	struct CommandList *c;
2982edd16368SStephen M. Cameron 	char *buff = NULL;
2983edd16368SStephen M. Cameron 	union u64bit temp64;
2984c1f63c8fSStephen M. Cameron 	int rc = 0;
2985edd16368SStephen M. Cameron 
2986edd16368SStephen M. Cameron 	if (!argp)
2987edd16368SStephen M. Cameron 		return -EINVAL;
2988edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
2989edd16368SStephen M. Cameron 		return -EPERM;
2990edd16368SStephen M. Cameron 	if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
2991edd16368SStephen M. Cameron 		return -EFAULT;
2992edd16368SStephen M. Cameron 	if ((iocommand.buf_size < 1) &&
2993edd16368SStephen M. Cameron 	    (iocommand.Request.Type.Direction != XFER_NONE)) {
2994edd16368SStephen M. Cameron 		return -EINVAL;
2995edd16368SStephen M. Cameron 	}
2996edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
2997edd16368SStephen M. Cameron 		buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
2998edd16368SStephen M. Cameron 		if (buff == NULL)
2999edd16368SStephen M. Cameron 			return -EFAULT;
3000edd16368SStephen M. Cameron 		if (iocommand.Request.Type.Direction == XFER_WRITE) {
3001edd16368SStephen M. Cameron 			/* Copy the data into the buffer we created */
3002b03a7771SStephen M. Cameron 			if (copy_from_user(buff, iocommand.buf,
3003b03a7771SStephen M. Cameron 				iocommand.buf_size)) {
3004c1f63c8fSStephen M. Cameron 				rc = -EFAULT;
3005c1f63c8fSStephen M. Cameron 				goto out_kfree;
3006edd16368SStephen M. Cameron 			}
3007b03a7771SStephen M. Cameron 		} else {
3008edd16368SStephen M. Cameron 			memset(buff, 0, iocommand.buf_size);
3009b03a7771SStephen M. Cameron 		}
3010b03a7771SStephen M. Cameron 	}
3011edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
3012edd16368SStephen M. Cameron 	if (c == NULL) {
3013c1f63c8fSStephen M. Cameron 		rc = -ENOMEM;
3014c1f63c8fSStephen M. Cameron 		goto out_kfree;
3015edd16368SStephen M. Cameron 	}
3016edd16368SStephen M. Cameron 	/* Fill in the command type */
3017edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
3018edd16368SStephen M. Cameron 	/* Fill in Command Header */
3019edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0; /* unused in simple mode */
3020edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {	/* buffer to fill */
3021edd16368SStephen M. Cameron 		c->Header.SGList = 1;
3022edd16368SStephen M. Cameron 		c->Header.SGTotal = 1;
3023edd16368SStephen M. Cameron 	} else	{ /* no buffers to fill */
3024edd16368SStephen M. Cameron 		c->Header.SGList = 0;
3025edd16368SStephen M. Cameron 		c->Header.SGTotal = 0;
3026edd16368SStephen M. Cameron 	}
3027edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
3028edd16368SStephen M. Cameron 	/* use the kernel address the cmd block for tag */
3029edd16368SStephen M. Cameron 	c->Header.Tag.lower = c->busaddr;
3030edd16368SStephen M. Cameron 
3031edd16368SStephen M. Cameron 	/* Fill in Request block */
3032edd16368SStephen M. Cameron 	memcpy(&c->Request, &iocommand.Request,
3033edd16368SStephen M. Cameron 		sizeof(c->Request));
3034edd16368SStephen M. Cameron 
3035edd16368SStephen M. Cameron 	/* Fill in the scatter gather information */
3036edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
3037edd16368SStephen M. Cameron 		temp64.val = pci_map_single(h->pdev, buff,
3038edd16368SStephen M. Cameron 			iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
3039bcc48ffaSStephen M. Cameron 		if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
3040bcc48ffaSStephen M. Cameron 			c->SG[0].Addr.lower = 0;
3041bcc48ffaSStephen M. Cameron 			c->SG[0].Addr.upper = 0;
3042bcc48ffaSStephen M. Cameron 			c->SG[0].Len = 0;
3043bcc48ffaSStephen M. Cameron 			rc = -ENOMEM;
3044bcc48ffaSStephen M. Cameron 			goto out;
3045bcc48ffaSStephen M. Cameron 		}
3046edd16368SStephen M. Cameron 		c->SG[0].Addr.lower = temp64.val32.lower;
3047edd16368SStephen M. Cameron 		c->SG[0].Addr.upper = temp64.val32.upper;
3048edd16368SStephen M. Cameron 		c->SG[0].Len = iocommand.buf_size;
3049edd16368SStephen M. Cameron 		c->SG[0].Ext = 0; /* we are not chaining*/
3050edd16368SStephen M. Cameron 	}
3051a0c12413SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
3052c2dd32e0SStephen M. Cameron 	if (iocommand.buf_size > 0)
3053edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
3054edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
3055edd16368SStephen M. Cameron 
3056edd16368SStephen M. Cameron 	/* Copy the error information out */
3057edd16368SStephen M. Cameron 	memcpy(&iocommand.error_info, c->err_info,
3058edd16368SStephen M. Cameron 		sizeof(iocommand.error_info));
3059edd16368SStephen M. Cameron 	if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
3060c1f63c8fSStephen M. Cameron 		rc = -EFAULT;
3061c1f63c8fSStephen M. Cameron 		goto out;
3062edd16368SStephen M. Cameron 	}
3063b03a7771SStephen M. Cameron 	if (iocommand.Request.Type.Direction == XFER_READ &&
3064b03a7771SStephen M. Cameron 		iocommand.buf_size > 0) {
3065edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
3066edd16368SStephen M. Cameron 		if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
3067c1f63c8fSStephen M. Cameron 			rc = -EFAULT;
3068c1f63c8fSStephen M. Cameron 			goto out;
3069edd16368SStephen M. Cameron 		}
3070edd16368SStephen M. Cameron 	}
3071c1f63c8fSStephen M. Cameron out:
3072edd16368SStephen M. Cameron 	cmd_special_free(h, c);
3073c1f63c8fSStephen M. Cameron out_kfree:
3074c1f63c8fSStephen M. Cameron 	kfree(buff);
3075c1f63c8fSStephen M. Cameron 	return rc;
3076edd16368SStephen M. Cameron }
3077edd16368SStephen M. Cameron 
3078edd16368SStephen M. Cameron static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
3079edd16368SStephen M. Cameron {
3080edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct *ioc;
3081edd16368SStephen M. Cameron 	struct CommandList *c;
3082edd16368SStephen M. Cameron 	unsigned char **buff = NULL;
3083edd16368SStephen M. Cameron 	int *buff_size = NULL;
3084edd16368SStephen M. Cameron 	union u64bit temp64;
3085edd16368SStephen M. Cameron 	BYTE sg_used = 0;
3086edd16368SStephen M. Cameron 	int status = 0;
3087edd16368SStephen M. Cameron 	int i;
308801a02ffcSStephen M. Cameron 	u32 left;
308901a02ffcSStephen M. Cameron 	u32 sz;
3090edd16368SStephen M. Cameron 	BYTE __user *data_ptr;
3091edd16368SStephen M. Cameron 
3092edd16368SStephen M. Cameron 	if (!argp)
3093edd16368SStephen M. Cameron 		return -EINVAL;
3094edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
3095edd16368SStephen M. Cameron 		return -EPERM;
3096edd16368SStephen M. Cameron 	ioc = (BIG_IOCTL_Command_struct *)
3097edd16368SStephen M. Cameron 	    kmalloc(sizeof(*ioc), GFP_KERNEL);
3098edd16368SStephen M. Cameron 	if (!ioc) {
3099edd16368SStephen M. Cameron 		status = -ENOMEM;
3100edd16368SStephen M. Cameron 		goto cleanup1;
3101edd16368SStephen M. Cameron 	}
3102edd16368SStephen M. Cameron 	if (copy_from_user(ioc, argp, sizeof(*ioc))) {
3103edd16368SStephen M. Cameron 		status = -EFAULT;
3104edd16368SStephen M. Cameron 		goto cleanup1;
3105edd16368SStephen M. Cameron 	}
3106edd16368SStephen M. Cameron 	if ((ioc->buf_size < 1) &&
3107edd16368SStephen M. Cameron 	    (ioc->Request.Type.Direction != XFER_NONE)) {
3108edd16368SStephen M. Cameron 		status = -EINVAL;
3109edd16368SStephen M. Cameron 		goto cleanup1;
3110edd16368SStephen M. Cameron 	}
3111edd16368SStephen M. Cameron 	/* Check kmalloc limits  using all SGs */
3112edd16368SStephen M. Cameron 	if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
3113edd16368SStephen M. Cameron 		status = -EINVAL;
3114edd16368SStephen M. Cameron 		goto cleanup1;
3115edd16368SStephen M. Cameron 	}
3116d66ae08bSStephen M. Cameron 	if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
3117edd16368SStephen M. Cameron 		status = -EINVAL;
3118edd16368SStephen M. Cameron 		goto cleanup1;
3119edd16368SStephen M. Cameron 	}
3120d66ae08bSStephen M. Cameron 	buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
3121edd16368SStephen M. Cameron 	if (!buff) {
3122edd16368SStephen M. Cameron 		status = -ENOMEM;
3123edd16368SStephen M. Cameron 		goto cleanup1;
3124edd16368SStephen M. Cameron 	}
3125d66ae08bSStephen M. Cameron 	buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
3126edd16368SStephen M. Cameron 	if (!buff_size) {
3127edd16368SStephen M. Cameron 		status = -ENOMEM;
3128edd16368SStephen M. Cameron 		goto cleanup1;
3129edd16368SStephen M. Cameron 	}
3130edd16368SStephen M. Cameron 	left = ioc->buf_size;
3131edd16368SStephen M. Cameron 	data_ptr = ioc->buf;
3132edd16368SStephen M. Cameron 	while (left) {
3133edd16368SStephen M. Cameron 		sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
3134edd16368SStephen M. Cameron 		buff_size[sg_used] = sz;
3135edd16368SStephen M. Cameron 		buff[sg_used] = kmalloc(sz, GFP_KERNEL);
3136edd16368SStephen M. Cameron 		if (buff[sg_used] == NULL) {
3137edd16368SStephen M. Cameron 			status = -ENOMEM;
3138edd16368SStephen M. Cameron 			goto cleanup1;
3139edd16368SStephen M. Cameron 		}
3140edd16368SStephen M. Cameron 		if (ioc->Request.Type.Direction == XFER_WRITE) {
3141edd16368SStephen M. Cameron 			if (copy_from_user(buff[sg_used], data_ptr, sz)) {
3142edd16368SStephen M. Cameron 				status = -ENOMEM;
3143edd16368SStephen M. Cameron 				goto cleanup1;
3144edd16368SStephen M. Cameron 			}
3145edd16368SStephen M. Cameron 		} else
3146edd16368SStephen M. Cameron 			memset(buff[sg_used], 0, sz);
3147edd16368SStephen M. Cameron 		left -= sz;
3148edd16368SStephen M. Cameron 		data_ptr += sz;
3149edd16368SStephen M. Cameron 		sg_used++;
3150edd16368SStephen M. Cameron 	}
3151edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
3152edd16368SStephen M. Cameron 	if (c == NULL) {
3153edd16368SStephen M. Cameron 		status = -ENOMEM;
3154edd16368SStephen M. Cameron 		goto cleanup1;
3155edd16368SStephen M. Cameron 	}
3156edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
3157edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
3158b03a7771SStephen M. Cameron 	c->Header.SGList = c->Header.SGTotal = sg_used;
3159edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
3160edd16368SStephen M. Cameron 	c->Header.Tag.lower = c->busaddr;
3161edd16368SStephen M. Cameron 	memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
3162edd16368SStephen M. Cameron 	if (ioc->buf_size > 0) {
3163edd16368SStephen M. Cameron 		int i;
3164edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
3165edd16368SStephen M. Cameron 			temp64.val = pci_map_single(h->pdev, buff[i],
3166edd16368SStephen M. Cameron 				    buff_size[i], PCI_DMA_BIDIRECTIONAL);
3167bcc48ffaSStephen M. Cameron 			if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
3168bcc48ffaSStephen M. Cameron 				c->SG[i].Addr.lower = 0;
3169bcc48ffaSStephen M. Cameron 				c->SG[i].Addr.upper = 0;
3170bcc48ffaSStephen M. Cameron 				c->SG[i].Len = 0;
3171bcc48ffaSStephen M. Cameron 				hpsa_pci_unmap(h->pdev, c, i,
3172bcc48ffaSStephen M. Cameron 					PCI_DMA_BIDIRECTIONAL);
3173bcc48ffaSStephen M. Cameron 				status = -ENOMEM;
3174bcc48ffaSStephen M. Cameron 				goto cleanup1;
3175bcc48ffaSStephen M. Cameron 			}
3176edd16368SStephen M. Cameron 			c->SG[i].Addr.lower = temp64.val32.lower;
3177edd16368SStephen M. Cameron 			c->SG[i].Addr.upper = temp64.val32.upper;
3178edd16368SStephen M. Cameron 			c->SG[i].Len = buff_size[i];
3179edd16368SStephen M. Cameron 			/* we are not chaining */
3180edd16368SStephen M. Cameron 			c->SG[i].Ext = 0;
3181edd16368SStephen M. Cameron 		}
3182edd16368SStephen M. Cameron 	}
3183a0c12413SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
3184b03a7771SStephen M. Cameron 	if (sg_used)
3185edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
3186edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
3187edd16368SStephen M. Cameron 	/* Copy the error information out */
3188edd16368SStephen M. Cameron 	memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
3189edd16368SStephen M. Cameron 	if (copy_to_user(argp, ioc, sizeof(*ioc))) {
3190edd16368SStephen M. Cameron 		cmd_special_free(h, c);
3191edd16368SStephen M. Cameron 		status = -EFAULT;
3192edd16368SStephen M. Cameron 		goto cleanup1;
3193edd16368SStephen M. Cameron 	}
3194b03a7771SStephen M. Cameron 	if (ioc->Request.Type.Direction == XFER_READ && ioc->buf_size > 0) {
3195edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
3196edd16368SStephen M. Cameron 		BYTE __user *ptr = ioc->buf;
3197edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
3198edd16368SStephen M. Cameron 			if (copy_to_user(ptr, buff[i], buff_size[i])) {
3199edd16368SStephen M. Cameron 				cmd_special_free(h, c);
3200edd16368SStephen M. Cameron 				status = -EFAULT;
3201edd16368SStephen M. Cameron 				goto cleanup1;
3202edd16368SStephen M. Cameron 			}
3203edd16368SStephen M. Cameron 			ptr += buff_size[i];
3204edd16368SStephen M. Cameron 		}
3205edd16368SStephen M. Cameron 	}
3206edd16368SStephen M. Cameron 	cmd_special_free(h, c);
3207edd16368SStephen M. Cameron 	status = 0;
3208edd16368SStephen M. Cameron cleanup1:
3209edd16368SStephen M. Cameron 	if (buff) {
3210edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++)
3211edd16368SStephen M. Cameron 			kfree(buff[i]);
3212edd16368SStephen M. Cameron 		kfree(buff);
3213edd16368SStephen M. Cameron 	}
3214edd16368SStephen M. Cameron 	kfree(buff_size);
3215edd16368SStephen M. Cameron 	kfree(ioc);
3216edd16368SStephen M. Cameron 	return status;
3217edd16368SStephen M. Cameron }
3218edd16368SStephen M. Cameron 
3219edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
3220edd16368SStephen M. Cameron 	struct CommandList *c)
3221edd16368SStephen M. Cameron {
3222edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
3223edd16368SStephen M. Cameron 			c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
3224edd16368SStephen M. Cameron 		(void) check_for_unit_attention(h, c);
3225edd16368SStephen M. Cameron }
3226edd16368SStephen M. Cameron /*
3227edd16368SStephen M. Cameron  * ioctl
3228edd16368SStephen M. Cameron  */
3229edd16368SStephen M. Cameron static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
3230edd16368SStephen M. Cameron {
3231edd16368SStephen M. Cameron 	struct ctlr_info *h;
3232edd16368SStephen M. Cameron 	void __user *argp = (void __user *)arg;
3233edd16368SStephen M. Cameron 
3234edd16368SStephen M. Cameron 	h = sdev_to_hba(dev);
3235edd16368SStephen M. Cameron 
3236edd16368SStephen M. Cameron 	switch (cmd) {
3237edd16368SStephen M. Cameron 	case CCISS_DEREGDISK:
3238edd16368SStephen M. Cameron 	case CCISS_REGNEWDISK:
3239edd16368SStephen M. Cameron 	case CCISS_REGNEWD:
3240a08a8471SStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
3241edd16368SStephen M. Cameron 		return 0;
3242edd16368SStephen M. Cameron 	case CCISS_GETPCIINFO:
3243edd16368SStephen M. Cameron 		return hpsa_getpciinfo_ioctl(h, argp);
3244edd16368SStephen M. Cameron 	case CCISS_GETDRIVVER:
3245edd16368SStephen M. Cameron 		return hpsa_getdrivver_ioctl(h, argp);
3246edd16368SStephen M. Cameron 	case CCISS_PASSTHRU:
3247edd16368SStephen M. Cameron 		return hpsa_passthru_ioctl(h, argp);
3248edd16368SStephen M. Cameron 	case CCISS_BIG_PASSTHRU:
3249edd16368SStephen M. Cameron 		return hpsa_big_passthru_ioctl(h, argp);
3250edd16368SStephen M. Cameron 	default:
3251edd16368SStephen M. Cameron 		return -ENOTTY;
3252edd16368SStephen M. Cameron 	}
3253edd16368SStephen M. Cameron }
3254edd16368SStephen M. Cameron 
32556f039790SGreg Kroah-Hartman static int hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
32566f039790SGreg Kroah-Hartman 				u8 reset_type)
325764670ac8SStephen M. Cameron {
325864670ac8SStephen M. Cameron 	struct CommandList *c;
325964670ac8SStephen M. Cameron 
326064670ac8SStephen M. Cameron 	c = cmd_alloc(h);
326164670ac8SStephen M. Cameron 	if (!c)
326264670ac8SStephen M. Cameron 		return -ENOMEM;
3263a2dac136SStephen M. Cameron 	/* fill_cmd can't fail here, no data buffer to map */
3264a2dac136SStephen M. Cameron 	(void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
326564670ac8SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_MSG);
326664670ac8SStephen M. Cameron 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
326764670ac8SStephen M. Cameron 	c->waiting = NULL;
326864670ac8SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
326964670ac8SStephen M. Cameron 	/* Don't wait for completion, the reset won't complete.  Don't free
327064670ac8SStephen M. Cameron 	 * the command either.  This is the last command we will send before
327164670ac8SStephen M. Cameron 	 * re-initializing everything, so it doesn't matter and won't leak.
327264670ac8SStephen M. Cameron 	 */
327364670ac8SStephen M. Cameron 	return 0;
327464670ac8SStephen M. Cameron }
327564670ac8SStephen M. Cameron 
3276a2dac136SStephen M. Cameron static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
327701a02ffcSStephen M. Cameron 	void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
3278edd16368SStephen M. Cameron 	int cmd_type)
3279edd16368SStephen M. Cameron {
3280edd16368SStephen M. Cameron 	int pci_dir = XFER_NONE;
328175167d2cSStephen M. Cameron 	struct CommandList *a; /* for commands to be aborted */
3282edd16368SStephen M. Cameron 
3283edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
3284edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
3285edd16368SStephen M. Cameron 	if (buff != NULL && size > 0) {
3286edd16368SStephen M. Cameron 		c->Header.SGList = 1;
3287edd16368SStephen M. Cameron 		c->Header.SGTotal = 1;
3288edd16368SStephen M. Cameron 	} else {
3289edd16368SStephen M. Cameron 		c->Header.SGList = 0;
3290edd16368SStephen M. Cameron 		c->Header.SGTotal = 0;
3291edd16368SStephen M. Cameron 	}
3292edd16368SStephen M. Cameron 	c->Header.Tag.lower = c->busaddr;
3293edd16368SStephen M. Cameron 	memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
3294edd16368SStephen M. Cameron 
3295edd16368SStephen M. Cameron 	c->Request.Type.Type = cmd_type;
3296edd16368SStephen M. Cameron 	if (cmd_type == TYPE_CMD) {
3297edd16368SStephen M. Cameron 		switch (cmd) {
3298edd16368SStephen M. Cameron 		case HPSA_INQUIRY:
3299edd16368SStephen M. Cameron 			/* are we trying to read a vital product page */
3300edd16368SStephen M. Cameron 			if (page_code != 0) {
3301edd16368SStephen M. Cameron 				c->Request.CDB[1] = 0x01;
3302edd16368SStephen M. Cameron 				c->Request.CDB[2] = page_code;
3303edd16368SStephen M. Cameron 			}
3304edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
3305edd16368SStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
3306edd16368SStephen M. Cameron 			c->Request.Type.Direction = XFER_READ;
3307edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
3308edd16368SStephen M. Cameron 			c->Request.CDB[0] = HPSA_INQUIRY;
3309edd16368SStephen M. Cameron 			c->Request.CDB[4] = size & 0xFF;
3310edd16368SStephen M. Cameron 			break;
3311edd16368SStephen M. Cameron 		case HPSA_REPORT_LOG:
3312edd16368SStephen M. Cameron 		case HPSA_REPORT_PHYS:
3313edd16368SStephen M. Cameron 			/* Talking to controller so It's a physical command
3314edd16368SStephen M. Cameron 			   mode = 00 target = 0.  Nothing to write.
3315edd16368SStephen M. Cameron 			 */
3316edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
3317edd16368SStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
3318edd16368SStephen M. Cameron 			c->Request.Type.Direction = XFER_READ;
3319edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
3320edd16368SStephen M. Cameron 			c->Request.CDB[0] = cmd;
3321edd16368SStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
3322edd16368SStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
3323edd16368SStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
3324edd16368SStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
3325edd16368SStephen M. Cameron 			break;
3326edd16368SStephen M. Cameron 		case HPSA_CACHE_FLUSH:
3327edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
3328edd16368SStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
3329edd16368SStephen M. Cameron 			c->Request.Type.Direction = XFER_WRITE;
3330edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
3331edd16368SStephen M. Cameron 			c->Request.CDB[0] = BMIC_WRITE;
3332edd16368SStephen M. Cameron 			c->Request.CDB[6] = BMIC_CACHE_FLUSH;
3333bb158eabSStephen M. Cameron 			c->Request.CDB[7] = (size >> 8) & 0xFF;
3334bb158eabSStephen M. Cameron 			c->Request.CDB[8] = size & 0xFF;
3335edd16368SStephen M. Cameron 			break;
3336edd16368SStephen M. Cameron 		case TEST_UNIT_READY:
3337edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
3338edd16368SStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
3339edd16368SStephen M. Cameron 			c->Request.Type.Direction = XFER_NONE;
3340edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
3341edd16368SStephen M. Cameron 			break;
3342edd16368SStephen M. Cameron 		default:
3343edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
3344edd16368SStephen M. Cameron 			BUG();
3345a2dac136SStephen M. Cameron 			return -1;
3346edd16368SStephen M. Cameron 		}
3347edd16368SStephen M. Cameron 	} else if (cmd_type == TYPE_MSG) {
3348edd16368SStephen M. Cameron 		switch (cmd) {
3349edd16368SStephen M. Cameron 
3350edd16368SStephen M. Cameron 		case  HPSA_DEVICE_RESET_MSG:
3351edd16368SStephen M. Cameron 			c->Request.CDBLen = 16;
3352edd16368SStephen M. Cameron 			c->Request.Type.Type =  1; /* It is a MSG not a CMD */
3353edd16368SStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
3354edd16368SStephen M. Cameron 			c->Request.Type.Direction = XFER_NONE;
3355edd16368SStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
335664670ac8SStephen M. Cameron 			memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
335764670ac8SStephen M. Cameron 			c->Request.CDB[0] =  cmd;
335821e89afdSStephen M. Cameron 			c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
3359edd16368SStephen M. Cameron 			/* If bytes 4-7 are zero, it means reset the */
3360edd16368SStephen M. Cameron 			/* LunID device */
3361edd16368SStephen M. Cameron 			c->Request.CDB[4] = 0x00;
3362edd16368SStephen M. Cameron 			c->Request.CDB[5] = 0x00;
3363edd16368SStephen M. Cameron 			c->Request.CDB[6] = 0x00;
3364edd16368SStephen M. Cameron 			c->Request.CDB[7] = 0x00;
3365edd16368SStephen M. Cameron 			break;
336675167d2cSStephen M. Cameron 		case  HPSA_ABORT_MSG:
336775167d2cSStephen M. Cameron 			a = buff;       /* point to command to be aborted */
336875167d2cSStephen M. Cameron 			dev_dbg(&h->pdev->dev, "Abort Tag:0x%08x:%08x using request Tag:0x%08x:%08x\n",
336975167d2cSStephen M. Cameron 				a->Header.Tag.upper, a->Header.Tag.lower,
337075167d2cSStephen M. Cameron 				c->Header.Tag.upper, c->Header.Tag.lower);
337175167d2cSStephen M. Cameron 			c->Request.CDBLen = 16;
337275167d2cSStephen M. Cameron 			c->Request.Type.Type = TYPE_MSG;
337375167d2cSStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
337475167d2cSStephen M. Cameron 			c->Request.Type.Direction = XFER_WRITE;
337575167d2cSStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
337675167d2cSStephen M. Cameron 			c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
337775167d2cSStephen M. Cameron 			c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
337875167d2cSStephen M. Cameron 			c->Request.CDB[2] = 0x00; /* reserved */
337975167d2cSStephen M. Cameron 			c->Request.CDB[3] = 0x00; /* reserved */
338075167d2cSStephen M. Cameron 			/* Tag to abort goes in CDB[4]-CDB[11] */
338175167d2cSStephen M. Cameron 			c->Request.CDB[4] = a->Header.Tag.lower & 0xFF;
338275167d2cSStephen M. Cameron 			c->Request.CDB[5] = (a->Header.Tag.lower >> 8) & 0xFF;
338375167d2cSStephen M. Cameron 			c->Request.CDB[6] = (a->Header.Tag.lower >> 16) & 0xFF;
338475167d2cSStephen M. Cameron 			c->Request.CDB[7] = (a->Header.Tag.lower >> 24) & 0xFF;
338575167d2cSStephen M. Cameron 			c->Request.CDB[8] = a->Header.Tag.upper & 0xFF;
338675167d2cSStephen M. Cameron 			c->Request.CDB[9] = (a->Header.Tag.upper >> 8) & 0xFF;
338775167d2cSStephen M. Cameron 			c->Request.CDB[10] = (a->Header.Tag.upper >> 16) & 0xFF;
338875167d2cSStephen M. Cameron 			c->Request.CDB[11] = (a->Header.Tag.upper >> 24) & 0xFF;
338975167d2cSStephen M. Cameron 			c->Request.CDB[12] = 0x00; /* reserved */
339075167d2cSStephen M. Cameron 			c->Request.CDB[13] = 0x00; /* reserved */
339175167d2cSStephen M. Cameron 			c->Request.CDB[14] = 0x00; /* reserved */
339275167d2cSStephen M. Cameron 			c->Request.CDB[15] = 0x00; /* reserved */
339375167d2cSStephen M. Cameron 		break;
3394edd16368SStephen M. Cameron 		default:
3395edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown message type %d\n",
3396edd16368SStephen M. Cameron 				cmd);
3397edd16368SStephen M. Cameron 			BUG();
3398edd16368SStephen M. Cameron 		}
3399edd16368SStephen M. Cameron 	} else {
3400edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
3401edd16368SStephen M. Cameron 		BUG();
3402edd16368SStephen M. Cameron 	}
3403edd16368SStephen M. Cameron 
3404edd16368SStephen M. Cameron 	switch (c->Request.Type.Direction) {
3405edd16368SStephen M. Cameron 	case XFER_READ:
3406edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_FROMDEVICE;
3407edd16368SStephen M. Cameron 		break;
3408edd16368SStephen M. Cameron 	case XFER_WRITE:
3409edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_TODEVICE;
3410edd16368SStephen M. Cameron 		break;
3411edd16368SStephen M. Cameron 	case XFER_NONE:
3412edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_NONE;
3413edd16368SStephen M. Cameron 		break;
3414edd16368SStephen M. Cameron 	default:
3415edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_BIDIRECTIONAL;
3416edd16368SStephen M. Cameron 	}
3417a2dac136SStephen M. Cameron 	if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
3418a2dac136SStephen M. Cameron 		return -1;
3419a2dac136SStephen M. Cameron 	return 0;
3420edd16368SStephen M. Cameron }
3421edd16368SStephen M. Cameron 
3422edd16368SStephen M. Cameron /*
3423edd16368SStephen M. Cameron  * Map (physical) PCI mem into (virtual) kernel space
3424edd16368SStephen M. Cameron  */
3425edd16368SStephen M. Cameron static void __iomem *remap_pci_mem(ulong base, ulong size)
3426edd16368SStephen M. Cameron {
3427edd16368SStephen M. Cameron 	ulong page_base = ((ulong) base) & PAGE_MASK;
3428edd16368SStephen M. Cameron 	ulong page_offs = ((ulong) base) - page_base;
3429088ba34cSStephen M. Cameron 	void __iomem *page_remapped = ioremap_nocache(page_base,
3430088ba34cSStephen M. Cameron 		page_offs + size);
3431edd16368SStephen M. Cameron 
3432edd16368SStephen M. Cameron 	return page_remapped ? (page_remapped + page_offs) : NULL;
3433edd16368SStephen M. Cameron }
3434edd16368SStephen M. Cameron 
3435edd16368SStephen M. Cameron /* Takes cmds off the submission queue and sends them to the hardware,
3436edd16368SStephen M. Cameron  * then puts them on the queue of cmds waiting for completion.
3437edd16368SStephen M. Cameron  */
3438edd16368SStephen M. Cameron static void start_io(struct ctlr_info *h)
3439edd16368SStephen M. Cameron {
3440edd16368SStephen M. Cameron 	struct CommandList *c;
3441e16a33adSMatt Gates 	unsigned long flags;
3442edd16368SStephen M. Cameron 
3443e16a33adSMatt Gates 	spin_lock_irqsave(&h->lock, flags);
34449e0fc764SStephen M. Cameron 	while (!list_empty(&h->reqQ)) {
34459e0fc764SStephen M. Cameron 		c = list_entry(h->reqQ.next, struct CommandList, list);
3446edd16368SStephen M. Cameron 		/* can't do anything if fifo is full */
3447edd16368SStephen M. Cameron 		if ((h->access.fifo_full(h))) {
3448edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "fifo full\n");
3449edd16368SStephen M. Cameron 			break;
3450edd16368SStephen M. Cameron 		}
3451edd16368SStephen M. Cameron 
3452edd16368SStephen M. Cameron 		/* Get the first entry from the Request Q */
3453edd16368SStephen M. Cameron 		removeQ(c);
3454edd16368SStephen M. Cameron 		h->Qdepth--;
3455edd16368SStephen M. Cameron 
3456edd16368SStephen M. Cameron 		/* Put job onto the completed Q */
3457edd16368SStephen M. Cameron 		addQ(&h->cmpQ, c);
3458e16a33adSMatt Gates 
3459e16a33adSMatt Gates 		/* Must increment commands_outstanding before unlocking
3460e16a33adSMatt Gates 		 * and submitting to avoid race checking for fifo full
3461e16a33adSMatt Gates 		 * condition.
3462e16a33adSMatt Gates 		 */
3463e16a33adSMatt Gates 		h->commands_outstanding++;
3464e16a33adSMatt Gates 		if (h->commands_outstanding > h->max_outstanding)
3465e16a33adSMatt Gates 			h->max_outstanding = h->commands_outstanding;
3466e16a33adSMatt Gates 
3467e16a33adSMatt Gates 		/* Tell the controller execute command */
3468e16a33adSMatt Gates 		spin_unlock_irqrestore(&h->lock, flags);
3469e16a33adSMatt Gates 		h->access.submit_command(h, c);
3470e16a33adSMatt Gates 		spin_lock_irqsave(&h->lock, flags);
3471edd16368SStephen M. Cameron 	}
3472e16a33adSMatt Gates 	spin_unlock_irqrestore(&h->lock, flags);
3473edd16368SStephen M. Cameron }
3474edd16368SStephen M. Cameron 
3475254f796bSMatt Gates static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
3476edd16368SStephen M. Cameron {
3477254f796bSMatt Gates 	return h->access.command_completed(h, q);
3478edd16368SStephen M. Cameron }
3479edd16368SStephen M. Cameron 
3480900c5440SStephen M. Cameron static inline bool interrupt_pending(struct ctlr_info *h)
3481edd16368SStephen M. Cameron {
3482edd16368SStephen M. Cameron 	return h->access.intr_pending(h);
3483edd16368SStephen M. Cameron }
3484edd16368SStephen M. Cameron 
3485edd16368SStephen M. Cameron static inline long interrupt_not_for_us(struct ctlr_info *h)
3486edd16368SStephen M. Cameron {
348710f66018SStephen M. Cameron 	return (h->access.intr_pending(h) == 0) ||
348810f66018SStephen M. Cameron 		(h->interrupts_enabled == 0);
3489edd16368SStephen M. Cameron }
3490edd16368SStephen M. Cameron 
349101a02ffcSStephen M. Cameron static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
349201a02ffcSStephen M. Cameron 	u32 raw_tag)
3493edd16368SStephen M. Cameron {
3494edd16368SStephen M. Cameron 	if (unlikely(tag_index >= h->nr_cmds)) {
3495edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
3496edd16368SStephen M. Cameron 		return 1;
3497edd16368SStephen M. Cameron 	}
3498edd16368SStephen M. Cameron 	return 0;
3499edd16368SStephen M. Cameron }
3500edd16368SStephen M. Cameron 
35015a3d16f5SStephen M. Cameron static inline void finish_cmd(struct CommandList *c)
3502edd16368SStephen M. Cameron {
3503e16a33adSMatt Gates 	unsigned long flags;
3504e16a33adSMatt Gates 
3505e16a33adSMatt Gates 	spin_lock_irqsave(&c->h->lock, flags);
3506edd16368SStephen M. Cameron 	removeQ(c);
3507e16a33adSMatt Gates 	spin_unlock_irqrestore(&c->h->lock, flags);
3508e85c5974SStephen M. Cameron 	dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
3509edd16368SStephen M. Cameron 	if (likely(c->cmd_type == CMD_SCSI))
35101fb011fbSStephen M. Cameron 		complete_scsi_command(c);
3511edd16368SStephen M. Cameron 	else if (c->cmd_type == CMD_IOCTL_PEND)
3512edd16368SStephen M. Cameron 		complete(c->waiting);
3513edd16368SStephen M. Cameron }
3514edd16368SStephen M. Cameron 
3515a104c99fSStephen M. Cameron static inline u32 hpsa_tag_contains_index(u32 tag)
3516a104c99fSStephen M. Cameron {
3517a104c99fSStephen M. Cameron 	return tag & DIRECT_LOOKUP_BIT;
3518a104c99fSStephen M. Cameron }
3519a104c99fSStephen M. Cameron 
3520a104c99fSStephen M. Cameron static inline u32 hpsa_tag_to_index(u32 tag)
3521a104c99fSStephen M. Cameron {
3522a104c99fSStephen M. Cameron 	return tag >> DIRECT_LOOKUP_SHIFT;
3523a104c99fSStephen M. Cameron }
3524a104c99fSStephen M. Cameron 
3525a9a3a273SStephen M. Cameron 
3526a9a3a273SStephen M. Cameron static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
3527a104c99fSStephen M. Cameron {
3528a9a3a273SStephen M. Cameron #define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
3529a9a3a273SStephen M. Cameron #define HPSA_SIMPLE_ERROR_BITS 0x03
3530960a30e7SStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
3531a9a3a273SStephen M. Cameron 		return tag & ~HPSA_SIMPLE_ERROR_BITS;
3532a9a3a273SStephen M. Cameron 	return tag & ~HPSA_PERF_ERROR_BITS;
3533a104c99fSStephen M. Cameron }
3534a104c99fSStephen M. Cameron 
3535303932fdSDon Brace /* process completion of an indexed ("direct lookup") command */
35361d94f94dSStephen M. Cameron static inline void process_indexed_cmd(struct ctlr_info *h,
3537303932fdSDon Brace 	u32 raw_tag)
3538303932fdSDon Brace {
3539303932fdSDon Brace 	u32 tag_index;
3540303932fdSDon Brace 	struct CommandList *c;
3541303932fdSDon Brace 
3542303932fdSDon Brace 	tag_index = hpsa_tag_to_index(raw_tag);
35431d94f94dSStephen M. Cameron 	if (!bad_tag(h, tag_index, raw_tag)) {
3544303932fdSDon Brace 		c = h->cmd_pool + tag_index;
35455a3d16f5SStephen M. Cameron 		finish_cmd(c);
35461d94f94dSStephen M. Cameron 	}
3547303932fdSDon Brace }
3548303932fdSDon Brace 
3549303932fdSDon Brace /* process completion of a non-indexed command */
35501d94f94dSStephen M. Cameron static inline void process_nonindexed_cmd(struct ctlr_info *h,
3551303932fdSDon Brace 	u32 raw_tag)
3552303932fdSDon Brace {
3553303932fdSDon Brace 	u32 tag;
3554303932fdSDon Brace 	struct CommandList *c = NULL;
3555e16a33adSMatt Gates 	unsigned long flags;
3556303932fdSDon Brace 
3557a9a3a273SStephen M. Cameron 	tag = hpsa_tag_discard_error_bits(h, raw_tag);
3558e16a33adSMatt Gates 	spin_lock_irqsave(&h->lock, flags);
35599e0fc764SStephen M. Cameron 	list_for_each_entry(c, &h->cmpQ, list) {
3560303932fdSDon Brace 		if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
3561e16a33adSMatt Gates 			spin_unlock_irqrestore(&h->lock, flags);
35625a3d16f5SStephen M. Cameron 			finish_cmd(c);
35631d94f94dSStephen M. Cameron 			return;
3564303932fdSDon Brace 		}
3565303932fdSDon Brace 	}
3566e16a33adSMatt Gates 	spin_unlock_irqrestore(&h->lock, flags);
3567303932fdSDon Brace 	bad_tag(h, h->nr_cmds + 1, raw_tag);
3568303932fdSDon Brace }
3569303932fdSDon Brace 
357064670ac8SStephen M. Cameron /* Some controllers, like p400, will give us one interrupt
357164670ac8SStephen M. Cameron  * after a soft reset, even if we turned interrupts off.
357264670ac8SStephen M. Cameron  * Only need to check for this in the hpsa_xxx_discard_completions
357364670ac8SStephen M. Cameron  * functions.
357464670ac8SStephen M. Cameron  */
357564670ac8SStephen M. Cameron static int ignore_bogus_interrupt(struct ctlr_info *h)
357664670ac8SStephen M. Cameron {
357764670ac8SStephen M. Cameron 	if (likely(!reset_devices))
357864670ac8SStephen M. Cameron 		return 0;
357964670ac8SStephen M. Cameron 
358064670ac8SStephen M. Cameron 	if (likely(h->interrupts_enabled))
358164670ac8SStephen M. Cameron 		return 0;
358264670ac8SStephen M. Cameron 
358364670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
358464670ac8SStephen M. Cameron 		"(known firmware bug.)  Ignoring.\n");
358564670ac8SStephen M. Cameron 
358664670ac8SStephen M. Cameron 	return 1;
358764670ac8SStephen M. Cameron }
358864670ac8SStephen M. Cameron 
3589254f796bSMatt Gates /*
3590254f796bSMatt Gates  * Convert &h->q[x] (passed to interrupt handlers) back to h.
3591254f796bSMatt Gates  * Relies on (h-q[x] == x) being true for x such that
3592254f796bSMatt Gates  * 0 <= x < MAX_REPLY_QUEUES.
3593254f796bSMatt Gates  */
3594254f796bSMatt Gates static struct ctlr_info *queue_to_hba(u8 *queue)
359564670ac8SStephen M. Cameron {
3596254f796bSMatt Gates 	return container_of((queue - *queue), struct ctlr_info, q[0]);
3597254f796bSMatt Gates }
3598254f796bSMatt Gates 
3599254f796bSMatt Gates static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
3600254f796bSMatt Gates {
3601254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
3602254f796bSMatt Gates 	u8 q = *(u8 *) queue;
360364670ac8SStephen M. Cameron 	u32 raw_tag;
360464670ac8SStephen M. Cameron 
360564670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
360664670ac8SStephen M. Cameron 		return IRQ_NONE;
360764670ac8SStephen M. Cameron 
360864670ac8SStephen M. Cameron 	if (interrupt_not_for_us(h))
360964670ac8SStephen M. Cameron 		return IRQ_NONE;
3610a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
361164670ac8SStephen M. Cameron 	while (interrupt_pending(h)) {
3612254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
361364670ac8SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY)
3614254f796bSMatt Gates 			raw_tag = next_command(h, q);
361564670ac8SStephen M. Cameron 	}
361664670ac8SStephen M. Cameron 	return IRQ_HANDLED;
361764670ac8SStephen M. Cameron }
361864670ac8SStephen M. Cameron 
3619254f796bSMatt Gates static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
362064670ac8SStephen M. Cameron {
3621254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
362264670ac8SStephen M. Cameron 	u32 raw_tag;
3623254f796bSMatt Gates 	u8 q = *(u8 *) queue;
362464670ac8SStephen M. Cameron 
362564670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
362664670ac8SStephen M. Cameron 		return IRQ_NONE;
362764670ac8SStephen M. Cameron 
3628a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
3629254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
363064670ac8SStephen M. Cameron 	while (raw_tag != FIFO_EMPTY)
3631254f796bSMatt Gates 		raw_tag = next_command(h, q);
363264670ac8SStephen M. Cameron 	return IRQ_HANDLED;
363364670ac8SStephen M. Cameron }
363464670ac8SStephen M. Cameron 
3635254f796bSMatt Gates static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
3636edd16368SStephen M. Cameron {
3637254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba((u8 *) queue);
3638303932fdSDon Brace 	u32 raw_tag;
3639254f796bSMatt Gates 	u8 q = *(u8 *) queue;
3640edd16368SStephen M. Cameron 
3641edd16368SStephen M. Cameron 	if (interrupt_not_for_us(h))
3642edd16368SStephen M. Cameron 		return IRQ_NONE;
3643a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
364410f66018SStephen M. Cameron 	while (interrupt_pending(h)) {
3645254f796bSMatt Gates 		raw_tag = get_next_completion(h, q);
364610f66018SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY) {
36471d94f94dSStephen M. Cameron 			if (likely(hpsa_tag_contains_index(raw_tag)))
36481d94f94dSStephen M. Cameron 				process_indexed_cmd(h, raw_tag);
364910f66018SStephen M. Cameron 			else
36501d94f94dSStephen M. Cameron 				process_nonindexed_cmd(h, raw_tag);
3651254f796bSMatt Gates 			raw_tag = next_command(h, q);
365210f66018SStephen M. Cameron 		}
365310f66018SStephen M. Cameron 	}
365410f66018SStephen M. Cameron 	return IRQ_HANDLED;
365510f66018SStephen M. Cameron }
365610f66018SStephen M. Cameron 
3657254f796bSMatt Gates static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
365810f66018SStephen M. Cameron {
3659254f796bSMatt Gates 	struct ctlr_info *h = queue_to_hba(queue);
366010f66018SStephen M. Cameron 	u32 raw_tag;
3661254f796bSMatt Gates 	u8 q = *(u8 *) queue;
366210f66018SStephen M. Cameron 
3663a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
3664254f796bSMatt Gates 	raw_tag = get_next_completion(h, q);
3665303932fdSDon Brace 	while (raw_tag != FIFO_EMPTY) {
36661d94f94dSStephen M. Cameron 		if (likely(hpsa_tag_contains_index(raw_tag)))
36671d94f94dSStephen M. Cameron 			process_indexed_cmd(h, raw_tag);
3668303932fdSDon Brace 		else
36691d94f94dSStephen M. Cameron 			process_nonindexed_cmd(h, raw_tag);
3670254f796bSMatt Gates 		raw_tag = next_command(h, q);
3671edd16368SStephen M. Cameron 	}
3672edd16368SStephen M. Cameron 	return IRQ_HANDLED;
3673edd16368SStephen M. Cameron }
3674edd16368SStephen M. Cameron 
3675a9a3a273SStephen M. Cameron /* Send a message CDB to the firmware. Careful, this only works
3676a9a3a273SStephen M. Cameron  * in simple mode, not performant mode due to the tag lookup.
3677a9a3a273SStephen M. Cameron  * We only ever use this immediately after a controller reset.
3678a9a3a273SStephen M. Cameron  */
36796f039790SGreg Kroah-Hartman static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
3680edd16368SStephen M. Cameron 			unsigned char type)
3681edd16368SStephen M. Cameron {
3682edd16368SStephen M. Cameron 	struct Command {
3683edd16368SStephen M. Cameron 		struct CommandListHeader CommandHeader;
3684edd16368SStephen M. Cameron 		struct RequestBlock Request;
3685edd16368SStephen M. Cameron 		struct ErrDescriptor ErrorDescriptor;
3686edd16368SStephen M. Cameron 	};
3687edd16368SStephen M. Cameron 	struct Command *cmd;
3688edd16368SStephen M. Cameron 	static const size_t cmd_sz = sizeof(*cmd) +
3689edd16368SStephen M. Cameron 					sizeof(cmd->ErrorDescriptor);
3690edd16368SStephen M. Cameron 	dma_addr_t paddr64;
3691edd16368SStephen M. Cameron 	uint32_t paddr32, tag;
3692edd16368SStephen M. Cameron 	void __iomem *vaddr;
3693edd16368SStephen M. Cameron 	int i, err;
3694edd16368SStephen M. Cameron 
3695edd16368SStephen M. Cameron 	vaddr = pci_ioremap_bar(pdev, 0);
3696edd16368SStephen M. Cameron 	if (vaddr == NULL)
3697edd16368SStephen M. Cameron 		return -ENOMEM;
3698edd16368SStephen M. Cameron 
3699edd16368SStephen M. Cameron 	/* The Inbound Post Queue only accepts 32-bit physical addresses for the
3700edd16368SStephen M. Cameron 	 * CCISS commands, so they must be allocated from the lower 4GiB of
3701edd16368SStephen M. Cameron 	 * memory.
3702edd16368SStephen M. Cameron 	 */
3703edd16368SStephen M. Cameron 	err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
3704edd16368SStephen M. Cameron 	if (err) {
3705edd16368SStephen M. Cameron 		iounmap(vaddr);
3706edd16368SStephen M. Cameron 		return -ENOMEM;
3707edd16368SStephen M. Cameron 	}
3708edd16368SStephen M. Cameron 
3709edd16368SStephen M. Cameron 	cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
3710edd16368SStephen M. Cameron 	if (cmd == NULL) {
3711edd16368SStephen M. Cameron 		iounmap(vaddr);
3712edd16368SStephen M. Cameron 		return -ENOMEM;
3713edd16368SStephen M. Cameron 	}
3714edd16368SStephen M. Cameron 
3715edd16368SStephen M. Cameron 	/* This must fit, because of the 32-bit consistent DMA mask.  Also,
3716edd16368SStephen M. Cameron 	 * although there's no guarantee, we assume that the address is at
3717edd16368SStephen M. Cameron 	 * least 4-byte aligned (most likely, it's page-aligned).
3718edd16368SStephen M. Cameron 	 */
3719edd16368SStephen M. Cameron 	paddr32 = paddr64;
3720edd16368SStephen M. Cameron 
3721edd16368SStephen M. Cameron 	cmd->CommandHeader.ReplyQueue = 0;
3722edd16368SStephen M. Cameron 	cmd->CommandHeader.SGList = 0;
3723edd16368SStephen M. Cameron 	cmd->CommandHeader.SGTotal = 0;
3724edd16368SStephen M. Cameron 	cmd->CommandHeader.Tag.lower = paddr32;
3725edd16368SStephen M. Cameron 	cmd->CommandHeader.Tag.upper = 0;
3726edd16368SStephen M. Cameron 	memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
3727edd16368SStephen M. Cameron 
3728edd16368SStephen M. Cameron 	cmd->Request.CDBLen = 16;
3729edd16368SStephen M. Cameron 	cmd->Request.Type.Type = TYPE_MSG;
3730edd16368SStephen M. Cameron 	cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
3731edd16368SStephen M. Cameron 	cmd->Request.Type.Direction = XFER_NONE;
3732edd16368SStephen M. Cameron 	cmd->Request.Timeout = 0; /* Don't time out */
3733edd16368SStephen M. Cameron 	cmd->Request.CDB[0] = opcode;
3734edd16368SStephen M. Cameron 	cmd->Request.CDB[1] = type;
3735edd16368SStephen M. Cameron 	memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
3736edd16368SStephen M. Cameron 	cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
3737edd16368SStephen M. Cameron 	cmd->ErrorDescriptor.Addr.upper = 0;
3738edd16368SStephen M. Cameron 	cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
3739edd16368SStephen M. Cameron 
3740edd16368SStephen M. Cameron 	writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
3741edd16368SStephen M. Cameron 
3742edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
3743edd16368SStephen M. Cameron 		tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
3744a9a3a273SStephen M. Cameron 		if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr32)
3745edd16368SStephen M. Cameron 			break;
3746edd16368SStephen M. Cameron 		msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
3747edd16368SStephen M. Cameron 	}
3748edd16368SStephen M. Cameron 
3749edd16368SStephen M. Cameron 	iounmap(vaddr);
3750edd16368SStephen M. Cameron 
3751edd16368SStephen M. Cameron 	/* we leak the DMA buffer here ... no choice since the controller could
3752edd16368SStephen M. Cameron 	 *  still complete the command.
3753edd16368SStephen M. Cameron 	 */
3754edd16368SStephen M. Cameron 	if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
3755edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
3756edd16368SStephen M. Cameron 			opcode, type);
3757edd16368SStephen M. Cameron 		return -ETIMEDOUT;
3758edd16368SStephen M. Cameron 	}
3759edd16368SStephen M. Cameron 
3760edd16368SStephen M. Cameron 	pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
3761edd16368SStephen M. Cameron 
3762edd16368SStephen M. Cameron 	if (tag & HPSA_ERROR_BIT) {
3763edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
3764edd16368SStephen M. Cameron 			opcode, type);
3765edd16368SStephen M. Cameron 		return -EIO;
3766edd16368SStephen M. Cameron 	}
3767edd16368SStephen M. Cameron 
3768edd16368SStephen M. Cameron 	dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
3769edd16368SStephen M. Cameron 		opcode, type);
3770edd16368SStephen M. Cameron 	return 0;
3771edd16368SStephen M. Cameron }
3772edd16368SStephen M. Cameron 
3773edd16368SStephen M. Cameron #define hpsa_noop(p) hpsa_message(p, 3, 0)
3774edd16368SStephen M. Cameron 
37751df8552aSStephen M. Cameron static int hpsa_controller_hard_reset(struct pci_dev *pdev,
3776cf0b08d0SStephen M. Cameron 	void * __iomem vaddr, u32 use_doorbell)
3777edd16368SStephen M. Cameron {
37781df8552aSStephen M. Cameron 	u16 pmcsr;
37791df8552aSStephen M. Cameron 	int pos;
3780edd16368SStephen M. Cameron 
37811df8552aSStephen M. Cameron 	if (use_doorbell) {
37821df8552aSStephen M. Cameron 		/* For everything after the P600, the PCI power state method
37831df8552aSStephen M. Cameron 		 * of resetting the controller doesn't work, so we have this
37841df8552aSStephen M. Cameron 		 * other way using the doorbell register.
3785edd16368SStephen M. Cameron 		 */
37861df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using doorbell to reset controller\n");
3787cf0b08d0SStephen M. Cameron 		writel(use_doorbell, vaddr + SA5_DOORBELL);
37881df8552aSStephen M. Cameron 	} else { /* Try to do it the PCI power state way */
3789edd16368SStephen M. Cameron 
3790edd16368SStephen M. Cameron 		/* Quoting from the Open CISS Specification: "The Power
3791edd16368SStephen M. Cameron 		 * Management Control/Status Register (CSR) controls the power
3792edd16368SStephen M. Cameron 		 * state of the device.  The normal operating state is D0,
3793edd16368SStephen M. Cameron 		 * CSR=00h.  The software off state is D3, CSR=03h.  To reset
37941df8552aSStephen M. Cameron 		 * the controller, place the interface device in D3 then to D0,
37951df8552aSStephen M. Cameron 		 * this causes a secondary PCI reset which will reset the
37961df8552aSStephen M. Cameron 		 * controller." */
3797edd16368SStephen M. Cameron 
37981df8552aSStephen M. Cameron 		pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
37991df8552aSStephen M. Cameron 		if (pos == 0) {
38001df8552aSStephen M. Cameron 			dev_err(&pdev->dev,
38011df8552aSStephen M. Cameron 				"hpsa_reset_controller: "
38021df8552aSStephen M. Cameron 				"PCI PM not supported\n");
38031df8552aSStephen M. Cameron 			return -ENODEV;
38041df8552aSStephen M. Cameron 		}
38051df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using PCI PM to reset controller\n");
3806edd16368SStephen M. Cameron 		/* enter the D3hot power management state */
3807edd16368SStephen M. Cameron 		pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
3808edd16368SStephen M. Cameron 		pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
3809edd16368SStephen M. Cameron 		pmcsr |= PCI_D3hot;
3810edd16368SStephen M. Cameron 		pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
3811edd16368SStephen M. Cameron 
3812edd16368SStephen M. Cameron 		msleep(500);
3813edd16368SStephen M. Cameron 
3814edd16368SStephen M. Cameron 		/* enter the D0 power management state */
3815edd16368SStephen M. Cameron 		pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
3816edd16368SStephen M. Cameron 		pmcsr |= PCI_D0;
3817edd16368SStephen M. Cameron 		pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
3818c4853efeSMike Miller 
3819c4853efeSMike Miller 		/*
3820c4853efeSMike Miller 		 * The P600 requires a small delay when changing states.
3821c4853efeSMike Miller 		 * Otherwise we may think the board did not reset and we bail.
3822c4853efeSMike Miller 		 * This for kdump only and is particular to the P600.
3823c4853efeSMike Miller 		 */
3824c4853efeSMike Miller 		msleep(500);
38251df8552aSStephen M. Cameron 	}
38261df8552aSStephen M. Cameron 	return 0;
38271df8552aSStephen M. Cameron }
38281df8552aSStephen M. Cameron 
38296f039790SGreg Kroah-Hartman static void init_driver_version(char *driver_version, int len)
3830580ada3cSStephen M. Cameron {
3831580ada3cSStephen M. Cameron 	memset(driver_version, 0, len);
3832f79cfec6SStephen M. Cameron 	strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
3833580ada3cSStephen M. Cameron }
3834580ada3cSStephen M. Cameron 
38356f039790SGreg Kroah-Hartman static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
3836580ada3cSStephen M. Cameron {
3837580ada3cSStephen M. Cameron 	char *driver_version;
3838580ada3cSStephen M. Cameron 	int i, size = sizeof(cfgtable->driver_version);
3839580ada3cSStephen M. Cameron 
3840580ada3cSStephen M. Cameron 	driver_version = kmalloc(size, GFP_KERNEL);
3841580ada3cSStephen M. Cameron 	if (!driver_version)
3842580ada3cSStephen M. Cameron 		return -ENOMEM;
3843580ada3cSStephen M. Cameron 
3844580ada3cSStephen M. Cameron 	init_driver_version(driver_version, size);
3845580ada3cSStephen M. Cameron 	for (i = 0; i < size; i++)
3846580ada3cSStephen M. Cameron 		writeb(driver_version[i], &cfgtable->driver_version[i]);
3847580ada3cSStephen M. Cameron 	kfree(driver_version);
3848580ada3cSStephen M. Cameron 	return 0;
3849580ada3cSStephen M. Cameron }
3850580ada3cSStephen M. Cameron 
38516f039790SGreg Kroah-Hartman static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
38526f039790SGreg Kroah-Hartman 					  unsigned char *driver_ver)
3853580ada3cSStephen M. Cameron {
3854580ada3cSStephen M. Cameron 	int i;
3855580ada3cSStephen M. Cameron 
3856580ada3cSStephen M. Cameron 	for (i = 0; i < sizeof(cfgtable->driver_version); i++)
3857580ada3cSStephen M. Cameron 		driver_ver[i] = readb(&cfgtable->driver_version[i]);
3858580ada3cSStephen M. Cameron }
3859580ada3cSStephen M. Cameron 
38606f039790SGreg Kroah-Hartman static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
3861580ada3cSStephen M. Cameron {
3862580ada3cSStephen M. Cameron 
3863580ada3cSStephen M. Cameron 	char *driver_ver, *old_driver_ver;
3864580ada3cSStephen M. Cameron 	int rc, size = sizeof(cfgtable->driver_version);
3865580ada3cSStephen M. Cameron 
3866580ada3cSStephen M. Cameron 	old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
3867580ada3cSStephen M. Cameron 	if (!old_driver_ver)
3868580ada3cSStephen M. Cameron 		return -ENOMEM;
3869580ada3cSStephen M. Cameron 	driver_ver = old_driver_ver + size;
3870580ada3cSStephen M. Cameron 
3871580ada3cSStephen M. Cameron 	/* After a reset, the 32 bytes of "driver version" in the cfgtable
3872580ada3cSStephen M. Cameron 	 * should have been changed, otherwise we know the reset failed.
3873580ada3cSStephen M. Cameron 	 */
3874580ada3cSStephen M. Cameron 	init_driver_version(old_driver_ver, size);
3875580ada3cSStephen M. Cameron 	read_driver_ver_from_cfgtable(cfgtable, driver_ver);
3876580ada3cSStephen M. Cameron 	rc = !memcmp(driver_ver, old_driver_ver, size);
3877580ada3cSStephen M. Cameron 	kfree(old_driver_ver);
3878580ada3cSStephen M. Cameron 	return rc;
3879580ada3cSStephen M. Cameron }
38801df8552aSStephen M. Cameron /* This does a hard reset of the controller using PCI power management
38811df8552aSStephen M. Cameron  * states or the using the doorbell register.
38821df8552aSStephen M. Cameron  */
38836f039790SGreg Kroah-Hartman static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev)
38841df8552aSStephen M. Cameron {
38851df8552aSStephen M. Cameron 	u64 cfg_offset;
38861df8552aSStephen M. Cameron 	u32 cfg_base_addr;
38871df8552aSStephen M. Cameron 	u64 cfg_base_addr_index;
38881df8552aSStephen M. Cameron 	void __iomem *vaddr;
38891df8552aSStephen M. Cameron 	unsigned long paddr;
3890580ada3cSStephen M. Cameron 	u32 misc_fw_support;
3891270d05deSStephen M. Cameron 	int rc;
38921df8552aSStephen M. Cameron 	struct CfgTable __iomem *cfgtable;
3893cf0b08d0SStephen M. Cameron 	u32 use_doorbell;
389418867659SStephen M. Cameron 	u32 board_id;
3895270d05deSStephen M. Cameron 	u16 command_register;
38961df8552aSStephen M. Cameron 
38971df8552aSStephen M. Cameron 	/* For controllers as old as the P600, this is very nearly
38981df8552aSStephen M. Cameron 	 * the same thing as
38991df8552aSStephen M. Cameron 	 *
39001df8552aSStephen M. Cameron 	 * pci_save_state(pci_dev);
39011df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D3hot);
39021df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D0);
39031df8552aSStephen M. Cameron 	 * pci_restore_state(pci_dev);
39041df8552aSStephen M. Cameron 	 *
39051df8552aSStephen M. Cameron 	 * For controllers newer than the P600, the pci power state
39061df8552aSStephen M. Cameron 	 * method of resetting doesn't work so we have another way
39071df8552aSStephen M. Cameron 	 * using the doorbell register.
39081df8552aSStephen M. Cameron 	 */
390918867659SStephen M. Cameron 
391025c1e56aSStephen M. Cameron 	rc = hpsa_lookup_board_id(pdev, &board_id);
391146380786SStephen M. Cameron 	if (rc < 0 || !ctlr_is_resettable(board_id)) {
391225c1e56aSStephen M. Cameron 		dev_warn(&pdev->dev, "Not resetting device.\n");
391325c1e56aSStephen M. Cameron 		return -ENODEV;
391425c1e56aSStephen M. Cameron 	}
391546380786SStephen M. Cameron 
391646380786SStephen M. Cameron 	/* if controller is soft- but not hard resettable... */
391746380786SStephen M. Cameron 	if (!ctlr_is_hard_resettable(board_id))
391846380786SStephen M. Cameron 		return -ENOTSUPP; /* try soft reset later. */
391918867659SStephen M. Cameron 
3920270d05deSStephen M. Cameron 	/* Save the PCI command register */
3921270d05deSStephen M. Cameron 	pci_read_config_word(pdev, 4, &command_register);
3922270d05deSStephen M. Cameron 	/* Turn the board off.  This is so that later pci_restore_state()
3923270d05deSStephen M. Cameron 	 * won't turn the board on before the rest of config space is ready.
3924270d05deSStephen M. Cameron 	 */
3925270d05deSStephen M. Cameron 	pci_disable_device(pdev);
3926270d05deSStephen M. Cameron 	pci_save_state(pdev);
39271df8552aSStephen M. Cameron 
39281df8552aSStephen M. Cameron 	/* find the first memory BAR, so we can find the cfg table */
39291df8552aSStephen M. Cameron 	rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
39301df8552aSStephen M. Cameron 	if (rc)
39311df8552aSStephen M. Cameron 		return rc;
39321df8552aSStephen M. Cameron 	vaddr = remap_pci_mem(paddr, 0x250);
39331df8552aSStephen M. Cameron 	if (!vaddr)
39341df8552aSStephen M. Cameron 		return -ENOMEM;
39351df8552aSStephen M. Cameron 
39361df8552aSStephen M. Cameron 	/* find cfgtable in order to check if reset via doorbell is supported */
39371df8552aSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
39381df8552aSStephen M. Cameron 					&cfg_base_addr_index, &cfg_offset);
39391df8552aSStephen M. Cameron 	if (rc)
39401df8552aSStephen M. Cameron 		goto unmap_vaddr;
39411df8552aSStephen M. Cameron 	cfgtable = remap_pci_mem(pci_resource_start(pdev,
39421df8552aSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
39431df8552aSStephen M. Cameron 	if (!cfgtable) {
39441df8552aSStephen M. Cameron 		rc = -ENOMEM;
39451df8552aSStephen M. Cameron 		goto unmap_vaddr;
39461df8552aSStephen M. Cameron 	}
3947580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(cfgtable);
3948580ada3cSStephen M. Cameron 	if (rc)
3949580ada3cSStephen M. Cameron 		goto unmap_vaddr;
39501df8552aSStephen M. Cameron 
3951cf0b08d0SStephen M. Cameron 	/* If reset via doorbell register is supported, use that.
3952cf0b08d0SStephen M. Cameron 	 * There are two such methods.  Favor the newest method.
3953cf0b08d0SStephen M. Cameron 	 */
39541df8552aSStephen M. Cameron 	misc_fw_support = readl(&cfgtable->misc_fw_support);
3955cf0b08d0SStephen M. Cameron 	use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
3956cf0b08d0SStephen M. Cameron 	if (use_doorbell) {
3957cf0b08d0SStephen M. Cameron 		use_doorbell = DOORBELL_CTLR_RESET2;
3958cf0b08d0SStephen M. Cameron 	} else {
39591df8552aSStephen M. Cameron 		use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
3960cf0b08d0SStephen M. Cameron 		if (use_doorbell) {
3961fba63097SMike Miller 			dev_warn(&pdev->dev, "Soft reset not supported. "
3962fba63097SMike Miller 				"Firmware update is required.\n");
396364670ac8SStephen M. Cameron 			rc = -ENOTSUPP; /* try soft reset */
3964cf0b08d0SStephen M. Cameron 			goto unmap_cfgtable;
3965cf0b08d0SStephen M. Cameron 		}
3966cf0b08d0SStephen M. Cameron 	}
39671df8552aSStephen M. Cameron 
39681df8552aSStephen M. Cameron 	rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
39691df8552aSStephen M. Cameron 	if (rc)
39701df8552aSStephen M. Cameron 		goto unmap_cfgtable;
3971edd16368SStephen M. Cameron 
3972270d05deSStephen M. Cameron 	pci_restore_state(pdev);
3973270d05deSStephen M. Cameron 	rc = pci_enable_device(pdev);
3974270d05deSStephen M. Cameron 	if (rc) {
3975270d05deSStephen M. Cameron 		dev_warn(&pdev->dev, "failed to enable device.\n");
3976270d05deSStephen M. Cameron 		goto unmap_cfgtable;
3977edd16368SStephen M. Cameron 	}
3978270d05deSStephen M. Cameron 	pci_write_config_word(pdev, 4, command_register);
3979edd16368SStephen M. Cameron 
39801df8552aSStephen M. Cameron 	/* Some devices (notably the HP Smart Array 5i Controller)
39811df8552aSStephen M. Cameron 	   need a little pause here */
39821df8552aSStephen M. Cameron 	msleep(HPSA_POST_RESET_PAUSE_MSECS);
39831df8552aSStephen M. Cameron 
3984fe5389c8SStephen M. Cameron 	/* Wait for board to become not ready, then ready. */
39852b870cb3SStephen M. Cameron 	dev_info(&pdev->dev, "Waiting for board to reset.\n");
3986fe5389c8SStephen M. Cameron 	rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_NOT_READY);
398764670ac8SStephen M. Cameron 	if (rc) {
3988fe5389c8SStephen M. Cameron 		dev_warn(&pdev->dev,
398964670ac8SStephen M. Cameron 			"failed waiting for board to reset."
399064670ac8SStephen M. Cameron 			" Will try soft reset.\n");
399164670ac8SStephen M. Cameron 		rc = -ENOTSUPP; /* Not expected, but try soft reset later */
399264670ac8SStephen M. Cameron 		goto unmap_cfgtable;
399364670ac8SStephen M. Cameron 	}
3994fe5389c8SStephen M. Cameron 	rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
3995fe5389c8SStephen M. Cameron 	if (rc) {
3996fe5389c8SStephen M. Cameron 		dev_warn(&pdev->dev,
399764670ac8SStephen M. Cameron 			"failed waiting for board to become ready "
399864670ac8SStephen M. Cameron 			"after hard reset\n");
3999fe5389c8SStephen M. Cameron 		goto unmap_cfgtable;
4000fe5389c8SStephen M. Cameron 	}
4001fe5389c8SStephen M. Cameron 
4002580ada3cSStephen M. Cameron 	rc = controller_reset_failed(vaddr);
4003580ada3cSStephen M. Cameron 	if (rc < 0)
4004580ada3cSStephen M. Cameron 		goto unmap_cfgtable;
4005580ada3cSStephen M. Cameron 	if (rc) {
400664670ac8SStephen M. Cameron 		dev_warn(&pdev->dev, "Unable to successfully reset "
400764670ac8SStephen M. Cameron 			"controller. Will try soft reset.\n");
400864670ac8SStephen M. Cameron 		rc = -ENOTSUPP;
4009580ada3cSStephen M. Cameron 	} else {
401064670ac8SStephen M. Cameron 		dev_info(&pdev->dev, "board ready after hard reset.\n");
40111df8552aSStephen M. Cameron 	}
40121df8552aSStephen M. Cameron 
40131df8552aSStephen M. Cameron unmap_cfgtable:
40141df8552aSStephen M. Cameron 	iounmap(cfgtable);
40151df8552aSStephen M. Cameron 
40161df8552aSStephen M. Cameron unmap_vaddr:
40171df8552aSStephen M. Cameron 	iounmap(vaddr);
40181df8552aSStephen M. Cameron 	return rc;
4019edd16368SStephen M. Cameron }
4020edd16368SStephen M. Cameron 
4021edd16368SStephen M. Cameron /*
4022edd16368SStephen M. Cameron  *  We cannot read the structure directly, for portability we must use
4023edd16368SStephen M. Cameron  *   the io functions.
4024edd16368SStephen M. Cameron  *   This is for debug only.
4025edd16368SStephen M. Cameron  */
4026edd16368SStephen M. Cameron static void print_cfg_table(struct device *dev, struct CfgTable *tb)
4027edd16368SStephen M. Cameron {
402858f8665cSStephen M. Cameron #ifdef HPSA_DEBUG
4029edd16368SStephen M. Cameron 	int i;
4030edd16368SStephen M. Cameron 	char temp_name[17];
4031edd16368SStephen M. Cameron 
4032edd16368SStephen M. Cameron 	dev_info(dev, "Controller Configuration information\n");
4033edd16368SStephen M. Cameron 	dev_info(dev, "------------------------------------\n");
4034edd16368SStephen M. Cameron 	for (i = 0; i < 4; i++)
4035edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->Signature[i]));
4036edd16368SStephen M. Cameron 	temp_name[4] = '\0';
4037edd16368SStephen M. Cameron 	dev_info(dev, "   Signature = %s\n", temp_name);
4038edd16368SStephen M. Cameron 	dev_info(dev, "   Spec Number = %d\n", readl(&(tb->SpecValence)));
4039edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods supported = 0x%x\n",
4040edd16368SStephen M. Cameron 	       readl(&(tb->TransportSupport)));
4041edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods active = 0x%x\n",
4042edd16368SStephen M. Cameron 	       readl(&(tb->TransportActive)));
4043edd16368SStephen M. Cameron 	dev_info(dev, "   Requested transport Method = 0x%x\n",
4044edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.TransportRequest)));
4045edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Delay = 0x%x\n",
4046edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntDelay)));
4047edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Count = 0x%x\n",
4048edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntCount)));
4049edd16368SStephen M. Cameron 	dev_info(dev, "   Max outstanding commands = 0x%d\n",
4050edd16368SStephen M. Cameron 	       readl(&(tb->CmdsOutMax)));
4051edd16368SStephen M. Cameron 	dev_info(dev, "   Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
4052edd16368SStephen M. Cameron 	for (i = 0; i < 16; i++)
4053edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->ServerName[i]));
4054edd16368SStephen M. Cameron 	temp_name[16] = '\0';
4055edd16368SStephen M. Cameron 	dev_info(dev, "   Server Name = %s\n", temp_name);
4056edd16368SStephen M. Cameron 	dev_info(dev, "   Heartbeat Counter = 0x%x\n\n\n",
4057edd16368SStephen M. Cameron 		readl(&(tb->HeartBeat)));
4058edd16368SStephen M. Cameron #endif				/* HPSA_DEBUG */
405958f8665cSStephen M. Cameron }
4060edd16368SStephen M. Cameron 
4061edd16368SStephen M. Cameron static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
4062edd16368SStephen M. Cameron {
4063edd16368SStephen M. Cameron 	int i, offset, mem_type, bar_type;
4064edd16368SStephen M. Cameron 
4065edd16368SStephen M. Cameron 	if (pci_bar_addr == PCI_BASE_ADDRESS_0)	/* looking for BAR zero? */
4066edd16368SStephen M. Cameron 		return 0;
4067edd16368SStephen M. Cameron 	offset = 0;
4068edd16368SStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
4069edd16368SStephen M. Cameron 		bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
4070edd16368SStephen M. Cameron 		if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
4071edd16368SStephen M. Cameron 			offset += 4;
4072edd16368SStephen M. Cameron 		else {
4073edd16368SStephen M. Cameron 			mem_type = pci_resource_flags(pdev, i) &
4074edd16368SStephen M. Cameron 			    PCI_BASE_ADDRESS_MEM_TYPE_MASK;
4075edd16368SStephen M. Cameron 			switch (mem_type) {
4076edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_32:
4077edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_1M:
4078edd16368SStephen M. Cameron 				offset += 4;	/* 32 bit */
4079edd16368SStephen M. Cameron 				break;
4080edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_64:
4081edd16368SStephen M. Cameron 				offset += 8;
4082edd16368SStephen M. Cameron 				break;
4083edd16368SStephen M. Cameron 			default:	/* reserved in PCI 2.2 */
4084edd16368SStephen M. Cameron 				dev_warn(&pdev->dev,
4085edd16368SStephen M. Cameron 				       "base address is invalid\n");
4086edd16368SStephen M. Cameron 				return -1;
4087edd16368SStephen M. Cameron 				break;
4088edd16368SStephen M. Cameron 			}
4089edd16368SStephen M. Cameron 		}
4090edd16368SStephen M. Cameron 		if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
4091edd16368SStephen M. Cameron 			return i + 1;
4092edd16368SStephen M. Cameron 	}
4093edd16368SStephen M. Cameron 	return -1;
4094edd16368SStephen M. Cameron }
4095edd16368SStephen M. Cameron 
4096edd16368SStephen M. Cameron /* If MSI/MSI-X is supported by the kernel we will try to enable it on
4097edd16368SStephen M. Cameron  * controllers that are capable. If not, we use IO-APIC mode.
4098edd16368SStephen M. Cameron  */
4099edd16368SStephen M. Cameron 
41006f039790SGreg Kroah-Hartman static void hpsa_interrupt_mode(struct ctlr_info *h)
4101edd16368SStephen M. Cameron {
4102edd16368SStephen M. Cameron #ifdef CONFIG_PCI_MSI
4103254f796bSMatt Gates 	int err, i;
4104254f796bSMatt Gates 	struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
4105254f796bSMatt Gates 
4106254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++) {
4107254f796bSMatt Gates 		hpsa_msix_entries[i].vector = 0;
4108254f796bSMatt Gates 		hpsa_msix_entries[i].entry = i;
4109254f796bSMatt Gates 	}
4110edd16368SStephen M. Cameron 
4111edd16368SStephen M. Cameron 	/* Some boards advertise MSI but don't really support it */
41126b3f4c52SStephen M. Cameron 	if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
41136b3f4c52SStephen M. Cameron 	    (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
4114edd16368SStephen M. Cameron 		goto default_int_mode;
411555c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
411655c06c71SStephen M. Cameron 		dev_info(&h->pdev->dev, "MSIX\n");
4117254f796bSMatt Gates 		err = pci_enable_msix(h->pdev, hpsa_msix_entries,
4118254f796bSMatt Gates 						MAX_REPLY_QUEUES);
4119edd16368SStephen M. Cameron 		if (!err) {
4120254f796bSMatt Gates 			for (i = 0; i < MAX_REPLY_QUEUES; i++)
4121254f796bSMatt Gates 				h->intr[i] = hpsa_msix_entries[i].vector;
4122edd16368SStephen M. Cameron 			h->msix_vector = 1;
4123edd16368SStephen M. Cameron 			return;
4124edd16368SStephen M. Cameron 		}
4125edd16368SStephen M. Cameron 		if (err > 0) {
412655c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
4127edd16368SStephen M. Cameron 			       "available\n", err);
4128edd16368SStephen M. Cameron 			goto default_int_mode;
4129edd16368SStephen M. Cameron 		} else {
413055c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "MSI-X init failed %d\n",
4131edd16368SStephen M. Cameron 			       err);
4132edd16368SStephen M. Cameron 			goto default_int_mode;
4133edd16368SStephen M. Cameron 		}
4134edd16368SStephen M. Cameron 	}
413555c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
413655c06c71SStephen M. Cameron 		dev_info(&h->pdev->dev, "MSI\n");
413755c06c71SStephen M. Cameron 		if (!pci_enable_msi(h->pdev))
4138edd16368SStephen M. Cameron 			h->msi_vector = 1;
4139edd16368SStephen M. Cameron 		else
414055c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "MSI init failed\n");
4141edd16368SStephen M. Cameron 	}
4142edd16368SStephen M. Cameron default_int_mode:
4143edd16368SStephen M. Cameron #endif				/* CONFIG_PCI_MSI */
4144edd16368SStephen M. Cameron 	/* if we get here we're going to use the default interrupt mode */
4145a9a3a273SStephen M. Cameron 	h->intr[h->intr_mode] = h->pdev->irq;
4146edd16368SStephen M. Cameron }
4147edd16368SStephen M. Cameron 
41486f039790SGreg Kroah-Hartman static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
4149e5c880d1SStephen M. Cameron {
4150e5c880d1SStephen M. Cameron 	int i;
4151e5c880d1SStephen M. Cameron 	u32 subsystem_vendor_id, subsystem_device_id;
4152e5c880d1SStephen M. Cameron 
4153e5c880d1SStephen M. Cameron 	subsystem_vendor_id = pdev->subsystem_vendor;
4154e5c880d1SStephen M. Cameron 	subsystem_device_id = pdev->subsystem_device;
4155e5c880d1SStephen M. Cameron 	*board_id = ((subsystem_device_id << 16) & 0xffff0000) |
4156e5c880d1SStephen M. Cameron 		    subsystem_vendor_id;
4157e5c880d1SStephen M. Cameron 
4158e5c880d1SStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(products); i++)
4159e5c880d1SStephen M. Cameron 		if (*board_id == products[i].board_id)
4160e5c880d1SStephen M. Cameron 			return i;
4161e5c880d1SStephen M. Cameron 
41626798cc0aSStephen M. Cameron 	if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
41636798cc0aSStephen M. Cameron 		subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
41646798cc0aSStephen M. Cameron 		!hpsa_allow_any) {
4165e5c880d1SStephen M. Cameron 		dev_warn(&pdev->dev, "unrecognized board ID: "
4166e5c880d1SStephen M. Cameron 			"0x%08x, ignoring.\n", *board_id);
4167e5c880d1SStephen M. Cameron 			return -ENODEV;
4168e5c880d1SStephen M. Cameron 	}
4169e5c880d1SStephen M. Cameron 	return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
4170e5c880d1SStephen M. Cameron }
4171e5c880d1SStephen M. Cameron 
41726f039790SGreg Kroah-Hartman static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
41733a7774ceSStephen M. Cameron 				    unsigned long *memory_bar)
41743a7774ceSStephen M. Cameron {
41753a7774ceSStephen M. Cameron 	int i;
41763a7774ceSStephen M. Cameron 
41773a7774ceSStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
417812d2cd47SStephen M. Cameron 		if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
41793a7774ceSStephen M. Cameron 			/* addressing mode bits already removed */
418012d2cd47SStephen M. Cameron 			*memory_bar = pci_resource_start(pdev, i);
418112d2cd47SStephen M. Cameron 			dev_dbg(&pdev->dev, "memory BAR = %lx\n",
41823a7774ceSStephen M. Cameron 				*memory_bar);
41833a7774ceSStephen M. Cameron 			return 0;
41843a7774ceSStephen M. Cameron 		}
418512d2cd47SStephen M. Cameron 	dev_warn(&pdev->dev, "no memory BAR found\n");
41863a7774ceSStephen M. Cameron 	return -ENODEV;
41873a7774ceSStephen M. Cameron }
41883a7774ceSStephen M. Cameron 
41896f039790SGreg Kroah-Hartman static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
41906f039790SGreg Kroah-Hartman 				     int wait_for_ready)
41912c4c8c8bSStephen M. Cameron {
4192fe5389c8SStephen M. Cameron 	int i, iterations;
41932c4c8c8bSStephen M. Cameron 	u32 scratchpad;
4194fe5389c8SStephen M. Cameron 	if (wait_for_ready)
4195fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_READY_ITERATIONS;
4196fe5389c8SStephen M. Cameron 	else
4197fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
41982c4c8c8bSStephen M. Cameron 
4199fe5389c8SStephen M. Cameron 	for (i = 0; i < iterations; i++) {
4200fe5389c8SStephen M. Cameron 		scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
4201fe5389c8SStephen M. Cameron 		if (wait_for_ready) {
42022c4c8c8bSStephen M. Cameron 			if (scratchpad == HPSA_FIRMWARE_READY)
42032c4c8c8bSStephen M. Cameron 				return 0;
4204fe5389c8SStephen M. Cameron 		} else {
4205fe5389c8SStephen M. Cameron 			if (scratchpad != HPSA_FIRMWARE_READY)
4206fe5389c8SStephen M. Cameron 				return 0;
4207fe5389c8SStephen M. Cameron 		}
42082c4c8c8bSStephen M. Cameron 		msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
42092c4c8c8bSStephen M. Cameron 	}
4210fe5389c8SStephen M. Cameron 	dev_warn(&pdev->dev, "board not ready, timed out.\n");
42112c4c8c8bSStephen M. Cameron 	return -ENODEV;
42122c4c8c8bSStephen M. Cameron }
42132c4c8c8bSStephen M. Cameron 
42146f039790SGreg Kroah-Hartman static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
42156f039790SGreg Kroah-Hartman 			       u32 *cfg_base_addr, u64 *cfg_base_addr_index,
4216a51fd47fSStephen M. Cameron 			       u64 *cfg_offset)
4217a51fd47fSStephen M. Cameron {
4218a51fd47fSStephen M. Cameron 	*cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
4219a51fd47fSStephen M. Cameron 	*cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
4220a51fd47fSStephen M. Cameron 	*cfg_base_addr &= (u32) 0x0000ffff;
4221a51fd47fSStephen M. Cameron 	*cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
4222a51fd47fSStephen M. Cameron 	if (*cfg_base_addr_index == -1) {
4223a51fd47fSStephen M. Cameron 		dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
4224a51fd47fSStephen M. Cameron 		return -ENODEV;
4225a51fd47fSStephen M. Cameron 	}
4226a51fd47fSStephen M. Cameron 	return 0;
4227a51fd47fSStephen M. Cameron }
4228a51fd47fSStephen M. Cameron 
42296f039790SGreg Kroah-Hartman static int hpsa_find_cfgtables(struct ctlr_info *h)
4230edd16368SStephen M. Cameron {
423101a02ffcSStephen M. Cameron 	u64 cfg_offset;
423201a02ffcSStephen M. Cameron 	u32 cfg_base_addr;
423301a02ffcSStephen M. Cameron 	u64 cfg_base_addr_index;
4234303932fdSDon Brace 	u32 trans_offset;
4235a51fd47fSStephen M. Cameron 	int rc;
423677c4495cSStephen M. Cameron 
4237a51fd47fSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
4238a51fd47fSStephen M. Cameron 		&cfg_base_addr_index, &cfg_offset);
4239a51fd47fSStephen M. Cameron 	if (rc)
4240a51fd47fSStephen M. Cameron 		return rc;
424177c4495cSStephen M. Cameron 	h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
4242a51fd47fSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
424377c4495cSStephen M. Cameron 	if (!h->cfgtable)
424477c4495cSStephen M. Cameron 		return -ENOMEM;
4245580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(h->cfgtable);
4246580ada3cSStephen M. Cameron 	if (rc)
4247580ada3cSStephen M. Cameron 		return rc;
424877c4495cSStephen M. Cameron 	/* Find performant mode table. */
4249a51fd47fSStephen M. Cameron 	trans_offset = readl(&h->cfgtable->TransMethodOffset);
425077c4495cSStephen M. Cameron 	h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
425177c4495cSStephen M. Cameron 				cfg_base_addr_index)+cfg_offset+trans_offset,
425277c4495cSStephen M. Cameron 				sizeof(*h->transtable));
425377c4495cSStephen M. Cameron 	if (!h->transtable)
425477c4495cSStephen M. Cameron 		return -ENOMEM;
425577c4495cSStephen M. Cameron 	return 0;
425677c4495cSStephen M. Cameron }
425777c4495cSStephen M. Cameron 
42586f039790SGreg Kroah-Hartman static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
4259cba3d38bSStephen M. Cameron {
4260cba3d38bSStephen M. Cameron 	h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
426172ceeaecSStephen M. Cameron 
426272ceeaecSStephen M. Cameron 	/* Limit commands in memory limited kdump scenario. */
426372ceeaecSStephen M. Cameron 	if (reset_devices && h->max_commands > 32)
426472ceeaecSStephen M. Cameron 		h->max_commands = 32;
426572ceeaecSStephen M. Cameron 
4266cba3d38bSStephen M. Cameron 	if (h->max_commands < 16) {
4267cba3d38bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "Controller reports "
4268cba3d38bSStephen M. Cameron 			"max supported commands of %d, an obvious lie. "
4269cba3d38bSStephen M. Cameron 			"Using 16.  Ensure that firmware is up to date.\n",
4270cba3d38bSStephen M. Cameron 			h->max_commands);
4271cba3d38bSStephen M. Cameron 		h->max_commands = 16;
4272cba3d38bSStephen M. Cameron 	}
4273cba3d38bSStephen M. Cameron }
4274cba3d38bSStephen M. Cameron 
4275b93d7536SStephen M. Cameron /* Interrogate the hardware for some limits:
4276b93d7536SStephen M. Cameron  * max commands, max SG elements without chaining, and with chaining,
4277b93d7536SStephen M. Cameron  * SG chain block size, etc.
4278b93d7536SStephen M. Cameron  */
42796f039790SGreg Kroah-Hartman static void hpsa_find_board_params(struct ctlr_info *h)
4280b93d7536SStephen M. Cameron {
4281cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
4282b93d7536SStephen M. Cameron 	h->nr_cmds = h->max_commands - 4; /* Allow room for some ioctls */
4283b93d7536SStephen M. Cameron 	h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
4284b93d7536SStephen M. Cameron 	/*
4285b93d7536SStephen M. Cameron 	 * Limit in-command s/g elements to 32 save dma'able memory.
4286b93d7536SStephen M. Cameron 	 * Howvever spec says if 0, use 31
4287b93d7536SStephen M. Cameron 	 */
4288b93d7536SStephen M. Cameron 	h->max_cmd_sg_entries = 31;
4289b93d7536SStephen M. Cameron 	if (h->maxsgentries > 512) {
4290b93d7536SStephen M. Cameron 		h->max_cmd_sg_entries = 32;
4291b93d7536SStephen M. Cameron 		h->chainsize = h->maxsgentries - h->max_cmd_sg_entries + 1;
4292b93d7536SStephen M. Cameron 		h->maxsgentries--; /* save one for chain pointer */
4293b93d7536SStephen M. Cameron 	} else {
4294b93d7536SStephen M. Cameron 		h->maxsgentries = 31; /* default to traditional values */
4295b93d7536SStephen M. Cameron 		h->chainsize = 0;
4296b93d7536SStephen M. Cameron 	}
429775167d2cSStephen M. Cameron 
429875167d2cSStephen M. Cameron 	/* Find out what task management functions are supported and cache */
429975167d2cSStephen M. Cameron 	h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
4300b93d7536SStephen M. Cameron }
4301b93d7536SStephen M. Cameron 
430276c46e49SStephen M. Cameron static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
430376c46e49SStephen M. Cameron {
43040fc9fd40SAkinobu Mita 	if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
430576c46e49SStephen M. Cameron 		dev_warn(&h->pdev->dev, "not a valid CISS config table\n");
430676c46e49SStephen M. Cameron 		return false;
430776c46e49SStephen M. Cameron 	}
430876c46e49SStephen M. Cameron 	return true;
430976c46e49SStephen M. Cameron }
431076c46e49SStephen M. Cameron 
4311f7c39101SStephen M. Cameron /* Need to enable prefetch in the SCSI core for 6400 in x86 */
4312f7c39101SStephen M. Cameron static inline void hpsa_enable_scsi_prefetch(struct ctlr_info *h)
4313f7c39101SStephen M. Cameron {
4314f7c39101SStephen M. Cameron #ifdef CONFIG_X86
4315f7c39101SStephen M. Cameron 	u32 prefetch;
4316f7c39101SStephen M. Cameron 
4317f7c39101SStephen M. Cameron 	prefetch = readl(&(h->cfgtable->SCSI_Prefetch));
4318f7c39101SStephen M. Cameron 	prefetch |= 0x100;
4319f7c39101SStephen M. Cameron 	writel(prefetch, &(h->cfgtable->SCSI_Prefetch));
4320f7c39101SStephen M. Cameron #endif
4321f7c39101SStephen M. Cameron }
4322f7c39101SStephen M. Cameron 
43233d0eab67SStephen M. Cameron /* Disable DMA prefetch for the P600.  Otherwise an ASIC bug may result
43243d0eab67SStephen M. Cameron  * in a prefetch beyond physical memory.
43253d0eab67SStephen M. Cameron  */
43263d0eab67SStephen M. Cameron static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
43273d0eab67SStephen M. Cameron {
43283d0eab67SStephen M. Cameron 	u32 dma_prefetch;
43293d0eab67SStephen M. Cameron 
43303d0eab67SStephen M. Cameron 	if (h->board_id != 0x3225103C)
43313d0eab67SStephen M. Cameron 		return;
43323d0eab67SStephen M. Cameron 	dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
43333d0eab67SStephen M. Cameron 	dma_prefetch |= 0x8000;
43343d0eab67SStephen M. Cameron 	writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
43353d0eab67SStephen M. Cameron }
43363d0eab67SStephen M. Cameron 
43376f039790SGreg Kroah-Hartman static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
4338eb6b2ae9SStephen M. Cameron {
4339eb6b2ae9SStephen M. Cameron 	int i;
43406eaf46fdSStephen M. Cameron 	u32 doorbell_value;
43416eaf46fdSStephen M. Cameron 	unsigned long flags;
4342eb6b2ae9SStephen M. Cameron 
4343eb6b2ae9SStephen M. Cameron 	/* under certain very rare conditions, this can take awhile.
4344eb6b2ae9SStephen M. Cameron 	 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
4345eb6b2ae9SStephen M. Cameron 	 * as we enter this code.)
4346eb6b2ae9SStephen M. Cameron 	 */
4347eb6b2ae9SStephen M. Cameron 	for (i = 0; i < MAX_CONFIG_WAIT; i++) {
43486eaf46fdSStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
43496eaf46fdSStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
43506eaf46fdSStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
4351382be668SDan Carpenter 		if (!(doorbell_value & CFGTBL_ChangeReq))
4352eb6b2ae9SStephen M. Cameron 			break;
4353eb6b2ae9SStephen M. Cameron 		/* delay and try again */
435460d3f5b0SStephen M. Cameron 		usleep_range(10000, 20000);
4355eb6b2ae9SStephen M. Cameron 	}
43563f4336f3SStephen M. Cameron }
43573f4336f3SStephen M. Cameron 
43586f039790SGreg Kroah-Hartman static int hpsa_enter_simple_mode(struct ctlr_info *h)
43593f4336f3SStephen M. Cameron {
43603f4336f3SStephen M. Cameron 	u32 trans_support;
43613f4336f3SStephen M. Cameron 
43623f4336f3SStephen M. Cameron 	trans_support = readl(&(h->cfgtable->TransportSupport));
43633f4336f3SStephen M. Cameron 	if (!(trans_support & SIMPLE_MODE))
43643f4336f3SStephen M. Cameron 		return -ENOTSUPP;
43653f4336f3SStephen M. Cameron 
43663f4336f3SStephen M. Cameron 	h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
43673f4336f3SStephen M. Cameron 	/* Update the field, and then ring the doorbell */
43683f4336f3SStephen M. Cameron 	writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
43693f4336f3SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
43703f4336f3SStephen M. Cameron 	hpsa_wait_for_mode_change_ack(h);
4371eb6b2ae9SStephen M. Cameron 	print_cfg_table(&h->pdev->dev, h->cfgtable);
4372eb6b2ae9SStephen M. Cameron 	if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) {
4373eb6b2ae9SStephen M. Cameron 		dev_warn(&h->pdev->dev,
4374eb6b2ae9SStephen M. Cameron 			"unable to get board into simple mode\n");
4375eb6b2ae9SStephen M. Cameron 		return -ENODEV;
4376eb6b2ae9SStephen M. Cameron 	}
4377960a30e7SStephen M. Cameron 	h->transMethod = CFGTBL_Trans_Simple;
4378eb6b2ae9SStephen M. Cameron 	return 0;
4379eb6b2ae9SStephen M. Cameron }
4380eb6b2ae9SStephen M. Cameron 
43816f039790SGreg Kroah-Hartman static int hpsa_pci_init(struct ctlr_info *h)
438277c4495cSStephen M. Cameron {
4383eb6b2ae9SStephen M. Cameron 	int prod_index, err;
4384edd16368SStephen M. Cameron 
4385e5c880d1SStephen M. Cameron 	prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
4386e5c880d1SStephen M. Cameron 	if (prod_index < 0)
4387edd16368SStephen M. Cameron 		return -ENODEV;
4388e5c880d1SStephen M. Cameron 	h->product_name = products[prod_index].product_name;
4389e5c880d1SStephen M. Cameron 	h->access = *(products[prod_index].access);
4390e5c880d1SStephen M. Cameron 
4391e5a44df8SMatthew Garrett 	pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
4392e5a44df8SMatthew Garrett 			       PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
4393e5a44df8SMatthew Garrett 
439455c06c71SStephen M. Cameron 	err = pci_enable_device(h->pdev);
4395edd16368SStephen M. Cameron 	if (err) {
439655c06c71SStephen M. Cameron 		dev_warn(&h->pdev->dev, "unable to enable PCI device\n");
4397edd16368SStephen M. Cameron 		return err;
4398edd16368SStephen M. Cameron 	}
4399edd16368SStephen M. Cameron 
44005cb460a6SStephen M. Cameron 	/* Enable bus mastering (pci_disable_device may disable this) */
44015cb460a6SStephen M. Cameron 	pci_set_master(h->pdev);
44025cb460a6SStephen M. Cameron 
4403f79cfec6SStephen M. Cameron 	err = pci_request_regions(h->pdev, HPSA);
4404edd16368SStephen M. Cameron 	if (err) {
440555c06c71SStephen M. Cameron 		dev_err(&h->pdev->dev,
440655c06c71SStephen M. Cameron 			"cannot obtain PCI resources, aborting\n");
4407edd16368SStephen M. Cameron 		return err;
4408edd16368SStephen M. Cameron 	}
44096b3f4c52SStephen M. Cameron 	hpsa_interrupt_mode(h);
441012d2cd47SStephen M. Cameron 	err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
44113a7774ceSStephen M. Cameron 	if (err)
4412edd16368SStephen M. Cameron 		goto err_out_free_res;
4413edd16368SStephen M. Cameron 	h->vaddr = remap_pci_mem(h->paddr, 0x250);
4414204892e9SStephen M. Cameron 	if (!h->vaddr) {
4415204892e9SStephen M. Cameron 		err = -ENOMEM;
4416204892e9SStephen M. Cameron 		goto err_out_free_res;
4417204892e9SStephen M. Cameron 	}
4418fe5389c8SStephen M. Cameron 	err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
44192c4c8c8bSStephen M. Cameron 	if (err)
4420edd16368SStephen M. Cameron 		goto err_out_free_res;
442177c4495cSStephen M. Cameron 	err = hpsa_find_cfgtables(h);
442277c4495cSStephen M. Cameron 	if (err)
4423edd16368SStephen M. Cameron 		goto err_out_free_res;
4424b93d7536SStephen M. Cameron 	hpsa_find_board_params(h);
4425edd16368SStephen M. Cameron 
442676c46e49SStephen M. Cameron 	if (!hpsa_CISS_signature_present(h)) {
4427edd16368SStephen M. Cameron 		err = -ENODEV;
4428edd16368SStephen M. Cameron 		goto err_out_free_res;
4429edd16368SStephen M. Cameron 	}
4430f7c39101SStephen M. Cameron 	hpsa_enable_scsi_prefetch(h);
44313d0eab67SStephen M. Cameron 	hpsa_p600_dma_prefetch_quirk(h);
4432eb6b2ae9SStephen M. Cameron 	err = hpsa_enter_simple_mode(h);
4433eb6b2ae9SStephen M. Cameron 	if (err)
4434edd16368SStephen M. Cameron 		goto err_out_free_res;
4435edd16368SStephen M. Cameron 	return 0;
4436edd16368SStephen M. Cameron 
4437edd16368SStephen M. Cameron err_out_free_res:
4438204892e9SStephen M. Cameron 	if (h->transtable)
4439204892e9SStephen M. Cameron 		iounmap(h->transtable);
4440204892e9SStephen M. Cameron 	if (h->cfgtable)
4441204892e9SStephen M. Cameron 		iounmap(h->cfgtable);
4442204892e9SStephen M. Cameron 	if (h->vaddr)
4443204892e9SStephen M. Cameron 		iounmap(h->vaddr);
4444f0bd0b68SStephen M. Cameron 	pci_disable_device(h->pdev);
444555c06c71SStephen M. Cameron 	pci_release_regions(h->pdev);
4446edd16368SStephen M. Cameron 	return err;
4447edd16368SStephen M. Cameron }
4448edd16368SStephen M. Cameron 
44496f039790SGreg Kroah-Hartman static void hpsa_hba_inquiry(struct ctlr_info *h)
4450339b2b14SStephen M. Cameron {
4451339b2b14SStephen M. Cameron 	int rc;
4452339b2b14SStephen M. Cameron 
4453339b2b14SStephen M. Cameron #define HBA_INQUIRY_BYTE_COUNT 64
4454339b2b14SStephen M. Cameron 	h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
4455339b2b14SStephen M. Cameron 	if (!h->hba_inquiry_data)
4456339b2b14SStephen M. Cameron 		return;
4457339b2b14SStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
4458339b2b14SStephen M. Cameron 		h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
4459339b2b14SStephen M. Cameron 	if (rc != 0) {
4460339b2b14SStephen M. Cameron 		kfree(h->hba_inquiry_data);
4461339b2b14SStephen M. Cameron 		h->hba_inquiry_data = NULL;
4462339b2b14SStephen M. Cameron 	}
4463339b2b14SStephen M. Cameron }
4464339b2b14SStephen M. Cameron 
44656f039790SGreg Kroah-Hartman static int hpsa_init_reset_devices(struct pci_dev *pdev)
4466edd16368SStephen M. Cameron {
44671df8552aSStephen M. Cameron 	int rc, i;
4468edd16368SStephen M. Cameron 
44694c2a8c40SStephen M. Cameron 	if (!reset_devices)
44704c2a8c40SStephen M. Cameron 		return 0;
44714c2a8c40SStephen M. Cameron 
44721df8552aSStephen M. Cameron 	/* Reset the controller with a PCI power-cycle or via doorbell */
44731df8552aSStephen M. Cameron 	rc = hpsa_kdump_hard_reset_controller(pdev);
4474edd16368SStephen M. Cameron 
44751df8552aSStephen M. Cameron 	/* -ENOTSUPP here means we cannot reset the controller
44761df8552aSStephen M. Cameron 	 * but it's already (and still) up and running in
447718867659SStephen M. Cameron 	 * "performant mode".  Or, it might be 640x, which can't reset
447818867659SStephen M. Cameron 	 * due to concerns about shared bbwc between 6402/6404 pair.
44791df8552aSStephen M. Cameron 	 */
44801df8552aSStephen M. Cameron 	if (rc == -ENOTSUPP)
448164670ac8SStephen M. Cameron 		return rc; /* just try to do the kdump anyhow. */
44821df8552aSStephen M. Cameron 	if (rc)
44831df8552aSStephen M. Cameron 		return -ENODEV;
4484edd16368SStephen M. Cameron 
4485edd16368SStephen M. Cameron 	/* Now try to get the controller to respond to a no-op */
44862b870cb3SStephen M. Cameron 	dev_warn(&pdev->dev, "Waiting for controller to respond to no-op\n");
4487edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
4488edd16368SStephen M. Cameron 		if (hpsa_noop(pdev) == 0)
4489edd16368SStephen M. Cameron 			break;
4490edd16368SStephen M. Cameron 		else
4491edd16368SStephen M. Cameron 			dev_warn(&pdev->dev, "no-op failed%s\n",
4492edd16368SStephen M. Cameron 					(i < 11 ? "; re-trying" : ""));
4493edd16368SStephen M. Cameron 	}
44944c2a8c40SStephen M. Cameron 	return 0;
4495edd16368SStephen M. Cameron }
4496edd16368SStephen M. Cameron 
44976f039790SGreg Kroah-Hartman static int hpsa_allocate_cmd_pool(struct ctlr_info *h)
44982e9d1b36SStephen M. Cameron {
44992e9d1b36SStephen M. Cameron 	h->cmd_pool_bits = kzalloc(
45002e9d1b36SStephen M. Cameron 		DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
45012e9d1b36SStephen M. Cameron 		sizeof(unsigned long), GFP_KERNEL);
45022e9d1b36SStephen M. Cameron 	h->cmd_pool = pci_alloc_consistent(h->pdev,
45032e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->cmd_pool),
45042e9d1b36SStephen M. Cameron 		    &(h->cmd_pool_dhandle));
45052e9d1b36SStephen M. Cameron 	h->errinfo_pool = pci_alloc_consistent(h->pdev,
45062e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->errinfo_pool),
45072e9d1b36SStephen M. Cameron 		    &(h->errinfo_pool_dhandle));
45082e9d1b36SStephen M. Cameron 	if ((h->cmd_pool_bits == NULL)
45092e9d1b36SStephen M. Cameron 	    || (h->cmd_pool == NULL)
45102e9d1b36SStephen M. Cameron 	    || (h->errinfo_pool == NULL)) {
45112e9d1b36SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory in %s", __func__);
45122e9d1b36SStephen M. Cameron 		return -ENOMEM;
45132e9d1b36SStephen M. Cameron 	}
45142e9d1b36SStephen M. Cameron 	return 0;
45152e9d1b36SStephen M. Cameron }
45162e9d1b36SStephen M. Cameron 
45172e9d1b36SStephen M. Cameron static void hpsa_free_cmd_pool(struct ctlr_info *h)
45182e9d1b36SStephen M. Cameron {
45192e9d1b36SStephen M. Cameron 	kfree(h->cmd_pool_bits);
45202e9d1b36SStephen M. Cameron 	if (h->cmd_pool)
45212e9d1b36SStephen M. Cameron 		pci_free_consistent(h->pdev,
45222e9d1b36SStephen M. Cameron 			    h->nr_cmds * sizeof(struct CommandList),
45232e9d1b36SStephen M. Cameron 			    h->cmd_pool, h->cmd_pool_dhandle);
45242e9d1b36SStephen M. Cameron 	if (h->errinfo_pool)
45252e9d1b36SStephen M. Cameron 		pci_free_consistent(h->pdev,
45262e9d1b36SStephen M. Cameron 			    h->nr_cmds * sizeof(struct ErrorInfo),
45272e9d1b36SStephen M. Cameron 			    h->errinfo_pool,
45282e9d1b36SStephen M. Cameron 			    h->errinfo_pool_dhandle);
45292e9d1b36SStephen M. Cameron }
45302e9d1b36SStephen M. Cameron 
45310ae01a32SStephen M. Cameron static int hpsa_request_irq(struct ctlr_info *h,
45320ae01a32SStephen M. Cameron 	irqreturn_t (*msixhandler)(int, void *),
45330ae01a32SStephen M. Cameron 	irqreturn_t (*intxhandler)(int, void *))
45340ae01a32SStephen M. Cameron {
4535254f796bSMatt Gates 	int rc, i;
45360ae01a32SStephen M. Cameron 
4537254f796bSMatt Gates 	/*
4538254f796bSMatt Gates 	 * initialize h->q[x] = x so that interrupt handlers know which
4539254f796bSMatt Gates 	 * queue to process.
4540254f796bSMatt Gates 	 */
4541254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++)
4542254f796bSMatt Gates 		h->q[i] = (u8) i;
4543254f796bSMatt Gates 
4544254f796bSMatt Gates 	if (h->intr_mode == PERF_MODE_INT && h->msix_vector) {
4545254f796bSMatt Gates 		/* If performant mode and MSI-X, use multiple reply queues */
4546254f796bSMatt Gates 		for (i = 0; i < MAX_REPLY_QUEUES; i++)
4547254f796bSMatt Gates 			rc = request_irq(h->intr[i], msixhandler,
4548254f796bSMatt Gates 					0, h->devname,
4549254f796bSMatt Gates 					&h->q[i]);
4550254f796bSMatt Gates 	} else {
4551254f796bSMatt Gates 		/* Use single reply pool */
4552254f796bSMatt Gates 		if (h->msix_vector || h->msi_vector) {
4553254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
4554254f796bSMatt Gates 				msixhandler, 0, h->devname,
4555254f796bSMatt Gates 				&h->q[h->intr_mode]);
4556254f796bSMatt Gates 		} else {
4557254f796bSMatt Gates 			rc = request_irq(h->intr[h->intr_mode],
4558254f796bSMatt Gates 				intxhandler, IRQF_SHARED, h->devname,
4559254f796bSMatt Gates 				&h->q[h->intr_mode]);
4560254f796bSMatt Gates 		}
4561254f796bSMatt Gates 	}
45620ae01a32SStephen M. Cameron 	if (rc) {
45630ae01a32SStephen M. Cameron 		dev_err(&h->pdev->dev, "unable to get irq %d for %s\n",
45640ae01a32SStephen M. Cameron 		       h->intr[h->intr_mode], h->devname);
45650ae01a32SStephen M. Cameron 		return -ENODEV;
45660ae01a32SStephen M. Cameron 	}
45670ae01a32SStephen M. Cameron 	return 0;
45680ae01a32SStephen M. Cameron }
45690ae01a32SStephen M. Cameron 
45706f039790SGreg Kroah-Hartman static int hpsa_kdump_soft_reset(struct ctlr_info *h)
457164670ac8SStephen M. Cameron {
457264670ac8SStephen M. Cameron 	if (hpsa_send_host_reset(h, RAID_CTLR_LUNID,
457364670ac8SStephen M. Cameron 		HPSA_RESET_TYPE_CONTROLLER)) {
457464670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Resetting array controller failed.\n");
457564670ac8SStephen M. Cameron 		return -EIO;
457664670ac8SStephen M. Cameron 	}
457764670ac8SStephen M. Cameron 
457864670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
457964670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) {
458064670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
458164670ac8SStephen M. Cameron 		return -1;
458264670ac8SStephen M. Cameron 	}
458364670ac8SStephen M. Cameron 
458464670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
458564670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) {
458664670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Board failed to become ready "
458764670ac8SStephen M. Cameron 			"after soft reset.\n");
458864670ac8SStephen M. Cameron 		return -1;
458964670ac8SStephen M. Cameron 	}
459064670ac8SStephen M. Cameron 
459164670ac8SStephen M. Cameron 	return 0;
459264670ac8SStephen M. Cameron }
459364670ac8SStephen M. Cameron 
4594254f796bSMatt Gates static void free_irqs(struct ctlr_info *h)
4595254f796bSMatt Gates {
4596254f796bSMatt Gates 	int i;
4597254f796bSMatt Gates 
4598254f796bSMatt Gates 	if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
4599254f796bSMatt Gates 		/* Single reply queue, only one irq to free */
4600254f796bSMatt Gates 		i = h->intr_mode;
4601254f796bSMatt Gates 		free_irq(h->intr[i], &h->q[i]);
4602254f796bSMatt Gates 		return;
4603254f796bSMatt Gates 	}
4604254f796bSMatt Gates 
4605254f796bSMatt Gates 	for (i = 0; i < MAX_REPLY_QUEUES; i++)
4606254f796bSMatt Gates 		free_irq(h->intr[i], &h->q[i]);
4607254f796bSMatt Gates }
4608254f796bSMatt Gates 
46090097f0f4SStephen M. Cameron static void hpsa_free_irqs_and_disable_msix(struct ctlr_info *h)
461064670ac8SStephen M. Cameron {
4611254f796bSMatt Gates 	free_irqs(h);
461264670ac8SStephen M. Cameron #ifdef CONFIG_PCI_MSI
46130097f0f4SStephen M. Cameron 	if (h->msix_vector) {
46140097f0f4SStephen M. Cameron 		if (h->pdev->msix_enabled)
461564670ac8SStephen M. Cameron 			pci_disable_msix(h->pdev);
46160097f0f4SStephen M. Cameron 	} else if (h->msi_vector) {
46170097f0f4SStephen M. Cameron 		if (h->pdev->msi_enabled)
461864670ac8SStephen M. Cameron 			pci_disable_msi(h->pdev);
46190097f0f4SStephen M. Cameron 	}
462064670ac8SStephen M. Cameron #endif /* CONFIG_PCI_MSI */
46210097f0f4SStephen M. Cameron }
46220097f0f4SStephen M. Cameron 
46230097f0f4SStephen M. Cameron static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
46240097f0f4SStephen M. Cameron {
46250097f0f4SStephen M. Cameron 	hpsa_free_irqs_and_disable_msix(h);
462664670ac8SStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
462764670ac8SStephen M. Cameron 	hpsa_free_cmd_pool(h);
462864670ac8SStephen M. Cameron 	kfree(h->blockFetchTable);
462964670ac8SStephen M. Cameron 	pci_free_consistent(h->pdev, h->reply_pool_size,
463064670ac8SStephen M. Cameron 		h->reply_pool, h->reply_pool_dhandle);
463164670ac8SStephen M. Cameron 	if (h->vaddr)
463264670ac8SStephen M. Cameron 		iounmap(h->vaddr);
463364670ac8SStephen M. Cameron 	if (h->transtable)
463464670ac8SStephen M. Cameron 		iounmap(h->transtable);
463564670ac8SStephen M. Cameron 	if (h->cfgtable)
463664670ac8SStephen M. Cameron 		iounmap(h->cfgtable);
463764670ac8SStephen M. Cameron 	pci_release_regions(h->pdev);
463864670ac8SStephen M. Cameron 	kfree(h);
463964670ac8SStephen M. Cameron }
464064670ac8SStephen M. Cameron 
4641a0c12413SStephen M. Cameron static void remove_ctlr_from_lockup_detector_list(struct ctlr_info *h)
4642a0c12413SStephen M. Cameron {
4643a0c12413SStephen M. Cameron 	assert_spin_locked(&lockup_detector_lock);
4644a0c12413SStephen M. Cameron 	if (!hpsa_lockup_detector)
4645a0c12413SStephen M. Cameron 		return;
4646a0c12413SStephen M. Cameron 	if (h->lockup_detected)
4647a0c12413SStephen M. Cameron 		return; /* already stopped the lockup detector */
4648a0c12413SStephen M. Cameron 	list_del(&h->lockup_list);
4649a0c12413SStephen M. Cameron }
4650a0c12413SStephen M. Cameron 
4651a0c12413SStephen M. Cameron /* Called when controller lockup detected. */
4652a0c12413SStephen M. Cameron static void fail_all_cmds_on_list(struct ctlr_info *h, struct list_head *list)
4653a0c12413SStephen M. Cameron {
4654a0c12413SStephen M. Cameron 	struct CommandList *c = NULL;
4655a0c12413SStephen M. Cameron 
4656a0c12413SStephen M. Cameron 	assert_spin_locked(&h->lock);
4657a0c12413SStephen M. Cameron 	/* Mark all outstanding commands as failed and complete them. */
4658a0c12413SStephen M. Cameron 	while (!list_empty(list)) {
4659a0c12413SStephen M. Cameron 		c = list_entry(list->next, struct CommandList, list);
4660a0c12413SStephen M. Cameron 		c->err_info->CommandStatus = CMD_HARDWARE_ERR;
46615a3d16f5SStephen M. Cameron 		finish_cmd(c);
4662a0c12413SStephen M. Cameron 	}
4663a0c12413SStephen M. Cameron }
4664a0c12413SStephen M. Cameron 
4665a0c12413SStephen M. Cameron static void controller_lockup_detected(struct ctlr_info *h)
4666a0c12413SStephen M. Cameron {
4667a0c12413SStephen M. Cameron 	unsigned long flags;
4668a0c12413SStephen M. Cameron 
4669a0c12413SStephen M. Cameron 	assert_spin_locked(&lockup_detector_lock);
4670a0c12413SStephen M. Cameron 	remove_ctlr_from_lockup_detector_list(h);
4671a0c12413SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
4672a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
4673a0c12413SStephen M. Cameron 	h->lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
4674a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
4675a0c12413SStephen M. Cameron 	dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x\n",
4676a0c12413SStephen M. Cameron 			h->lockup_detected);
4677a0c12413SStephen M. Cameron 	pci_disable_device(h->pdev);
4678a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
4679a0c12413SStephen M. Cameron 	fail_all_cmds_on_list(h, &h->cmpQ);
4680a0c12413SStephen M. Cameron 	fail_all_cmds_on_list(h, &h->reqQ);
4681a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
4682a0c12413SStephen M. Cameron }
4683a0c12413SStephen M. Cameron 
4684a0c12413SStephen M. Cameron static void detect_controller_lockup(struct ctlr_info *h)
4685a0c12413SStephen M. Cameron {
4686a0c12413SStephen M. Cameron 	u64 now;
4687a0c12413SStephen M. Cameron 	u32 heartbeat;
4688a0c12413SStephen M. Cameron 	unsigned long flags;
4689a0c12413SStephen M. Cameron 
4690a0c12413SStephen M. Cameron 	assert_spin_locked(&lockup_detector_lock);
4691a0c12413SStephen M. Cameron 	now = get_jiffies_64();
4692a0c12413SStephen M. Cameron 	/* If we've received an interrupt recently, we're ok. */
4693a0c12413SStephen M. Cameron 	if (time_after64(h->last_intr_timestamp +
4694e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
4695a0c12413SStephen M. Cameron 		return;
4696a0c12413SStephen M. Cameron 
4697a0c12413SStephen M. Cameron 	/*
4698a0c12413SStephen M. Cameron 	 * If we've already checked the heartbeat recently, we're ok.
4699a0c12413SStephen M. Cameron 	 * This could happen if someone sends us a signal. We
4700a0c12413SStephen M. Cameron 	 * otherwise don't care about signals in this thread.
4701a0c12413SStephen M. Cameron 	 */
4702a0c12413SStephen M. Cameron 	if (time_after64(h->last_heartbeat_timestamp +
4703e85c5974SStephen M. Cameron 				(h->heartbeat_sample_interval), now))
4704a0c12413SStephen M. Cameron 		return;
4705a0c12413SStephen M. Cameron 
4706a0c12413SStephen M. Cameron 	/* If heartbeat has not changed since we last looked, we're not ok. */
4707a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
4708a0c12413SStephen M. Cameron 	heartbeat = readl(&h->cfgtable->HeartBeat);
4709a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
4710a0c12413SStephen M. Cameron 	if (h->last_heartbeat == heartbeat) {
4711a0c12413SStephen M. Cameron 		controller_lockup_detected(h);
4712a0c12413SStephen M. Cameron 		return;
4713a0c12413SStephen M. Cameron 	}
4714a0c12413SStephen M. Cameron 
4715a0c12413SStephen M. Cameron 	/* We're ok. */
4716a0c12413SStephen M. Cameron 	h->last_heartbeat = heartbeat;
4717a0c12413SStephen M. Cameron 	h->last_heartbeat_timestamp = now;
4718a0c12413SStephen M. Cameron }
4719a0c12413SStephen M. Cameron 
4720a0c12413SStephen M. Cameron static int detect_controller_lockup_thread(void *notused)
4721a0c12413SStephen M. Cameron {
4722a0c12413SStephen M. Cameron 	struct ctlr_info *h;
4723a0c12413SStephen M. Cameron 	unsigned long flags;
4724a0c12413SStephen M. Cameron 
4725a0c12413SStephen M. Cameron 	while (1) {
4726a0c12413SStephen M. Cameron 		struct list_head *this, *tmp;
4727a0c12413SStephen M. Cameron 
4728a0c12413SStephen M. Cameron 		schedule_timeout_interruptible(HEARTBEAT_SAMPLE_INTERVAL);
4729a0c12413SStephen M. Cameron 		if (kthread_should_stop())
4730a0c12413SStephen M. Cameron 			break;
4731a0c12413SStephen M. Cameron 		spin_lock_irqsave(&lockup_detector_lock, flags);
4732a0c12413SStephen M. Cameron 		list_for_each_safe(this, tmp, &hpsa_ctlr_list) {
4733a0c12413SStephen M. Cameron 			h = list_entry(this, struct ctlr_info, lockup_list);
4734a0c12413SStephen M. Cameron 			detect_controller_lockup(h);
4735a0c12413SStephen M. Cameron 		}
4736a0c12413SStephen M. Cameron 		spin_unlock_irqrestore(&lockup_detector_lock, flags);
4737a0c12413SStephen M. Cameron 	}
4738a0c12413SStephen M. Cameron 	return 0;
4739a0c12413SStephen M. Cameron }
4740a0c12413SStephen M. Cameron 
4741a0c12413SStephen M. Cameron static void add_ctlr_to_lockup_detector_list(struct ctlr_info *h)
4742a0c12413SStephen M. Cameron {
4743a0c12413SStephen M. Cameron 	unsigned long flags;
4744a0c12413SStephen M. Cameron 
4745e85c5974SStephen M. Cameron 	h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
4746a0c12413SStephen M. Cameron 	spin_lock_irqsave(&lockup_detector_lock, flags);
4747a0c12413SStephen M. Cameron 	list_add_tail(&h->lockup_list, &hpsa_ctlr_list);
4748a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&lockup_detector_lock, flags);
4749a0c12413SStephen M. Cameron }
4750a0c12413SStephen M. Cameron 
4751a0c12413SStephen M. Cameron static void start_controller_lockup_detector(struct ctlr_info *h)
4752a0c12413SStephen M. Cameron {
4753a0c12413SStephen M. Cameron 	/* Start the lockup detector thread if not already started */
4754a0c12413SStephen M. Cameron 	if (!hpsa_lockup_detector) {
4755a0c12413SStephen M. Cameron 		spin_lock_init(&lockup_detector_lock);
4756a0c12413SStephen M. Cameron 		hpsa_lockup_detector =
4757a0c12413SStephen M. Cameron 			kthread_run(detect_controller_lockup_thread,
4758f79cfec6SStephen M. Cameron 						NULL, HPSA);
4759a0c12413SStephen M. Cameron 	}
4760a0c12413SStephen M. Cameron 	if (!hpsa_lockup_detector) {
4761a0c12413SStephen M. Cameron 		dev_warn(&h->pdev->dev,
4762a0c12413SStephen M. Cameron 			"Could not start lockup detector thread\n");
4763a0c12413SStephen M. Cameron 		return;
4764a0c12413SStephen M. Cameron 	}
4765a0c12413SStephen M. Cameron 	add_ctlr_to_lockup_detector_list(h);
4766a0c12413SStephen M. Cameron }
4767a0c12413SStephen M. Cameron 
4768a0c12413SStephen M. Cameron static void stop_controller_lockup_detector(struct ctlr_info *h)
4769a0c12413SStephen M. Cameron {
4770a0c12413SStephen M. Cameron 	unsigned long flags;
4771a0c12413SStephen M. Cameron 
4772a0c12413SStephen M. Cameron 	spin_lock_irqsave(&lockup_detector_lock, flags);
4773a0c12413SStephen M. Cameron 	remove_ctlr_from_lockup_detector_list(h);
4774a0c12413SStephen M. Cameron 	/* If the list of ctlr's to monitor is empty, stop the thread */
4775a0c12413SStephen M. Cameron 	if (list_empty(&hpsa_ctlr_list)) {
4776775bf277SStephen M. Cameron 		spin_unlock_irqrestore(&lockup_detector_lock, flags);
4777a0c12413SStephen M. Cameron 		kthread_stop(hpsa_lockup_detector);
4778775bf277SStephen M. Cameron 		spin_lock_irqsave(&lockup_detector_lock, flags);
4779a0c12413SStephen M. Cameron 		hpsa_lockup_detector = NULL;
4780a0c12413SStephen M. Cameron 	}
4781a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&lockup_detector_lock, flags);
4782a0c12413SStephen M. Cameron }
4783a0c12413SStephen M. Cameron 
47846f039790SGreg Kroah-Hartman static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
47854c2a8c40SStephen M. Cameron {
47864c2a8c40SStephen M. Cameron 	int dac, rc;
47874c2a8c40SStephen M. Cameron 	struct ctlr_info *h;
478864670ac8SStephen M. Cameron 	int try_soft_reset = 0;
478964670ac8SStephen M. Cameron 	unsigned long flags;
47904c2a8c40SStephen M. Cameron 
47914c2a8c40SStephen M. Cameron 	if (number_of_controllers == 0)
47924c2a8c40SStephen M. Cameron 		printk(KERN_INFO DRIVER_NAME "\n");
47934c2a8c40SStephen M. Cameron 
47944c2a8c40SStephen M. Cameron 	rc = hpsa_init_reset_devices(pdev);
479564670ac8SStephen M. Cameron 	if (rc) {
479664670ac8SStephen M. Cameron 		if (rc != -ENOTSUPP)
47974c2a8c40SStephen M. Cameron 			return rc;
479864670ac8SStephen M. Cameron 		/* If the reset fails in a particular way (it has no way to do
479964670ac8SStephen M. Cameron 		 * a proper hard reset, so returns -ENOTSUPP) we can try to do
480064670ac8SStephen M. Cameron 		 * a soft reset once we get the controller configured up to the
480164670ac8SStephen M. Cameron 		 * point that it can accept a command.
480264670ac8SStephen M. Cameron 		 */
480364670ac8SStephen M. Cameron 		try_soft_reset = 1;
480464670ac8SStephen M. Cameron 		rc = 0;
480564670ac8SStephen M. Cameron 	}
480664670ac8SStephen M. Cameron 
480764670ac8SStephen M. Cameron reinit_after_soft_reset:
48084c2a8c40SStephen M. Cameron 
4809303932fdSDon Brace 	/* Command structures must be aligned on a 32-byte boundary because
4810303932fdSDon Brace 	 * the 5 lower bits of the address are used by the hardware. and by
4811303932fdSDon Brace 	 * the driver.  See comments in hpsa.h for more info.
4812303932fdSDon Brace 	 */
4813303932fdSDon Brace #define COMMANDLIST_ALIGNMENT 32
4814303932fdSDon Brace 	BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
4815edd16368SStephen M. Cameron 	h = kzalloc(sizeof(*h), GFP_KERNEL);
4816edd16368SStephen M. Cameron 	if (!h)
4817ecd9aad4SStephen M. Cameron 		return -ENOMEM;
4818edd16368SStephen M. Cameron 
481955c06c71SStephen M. Cameron 	h->pdev = pdev;
4820a9a3a273SStephen M. Cameron 	h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
48219e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&h->cmpQ);
48229e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&h->reqQ);
48236eaf46fdSStephen M. Cameron 	spin_lock_init(&h->lock);
48246eaf46fdSStephen M. Cameron 	spin_lock_init(&h->scan_lock);
482555c06c71SStephen M. Cameron 	rc = hpsa_pci_init(h);
4826ecd9aad4SStephen M. Cameron 	if (rc != 0)
4827edd16368SStephen M. Cameron 		goto clean1;
4828edd16368SStephen M. Cameron 
4829f79cfec6SStephen M. Cameron 	sprintf(h->devname, HPSA "%d", number_of_controllers);
4830edd16368SStephen M. Cameron 	h->ctlr = number_of_controllers;
4831edd16368SStephen M. Cameron 	number_of_controllers++;
4832edd16368SStephen M. Cameron 
4833edd16368SStephen M. Cameron 	/* configure PCI DMA stuff */
4834ecd9aad4SStephen M. Cameron 	rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4835ecd9aad4SStephen M. Cameron 	if (rc == 0) {
4836edd16368SStephen M. Cameron 		dac = 1;
4837ecd9aad4SStephen M. Cameron 	} else {
4838ecd9aad4SStephen M. Cameron 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4839ecd9aad4SStephen M. Cameron 		if (rc == 0) {
4840edd16368SStephen M. Cameron 			dac = 0;
4841ecd9aad4SStephen M. Cameron 		} else {
4842edd16368SStephen M. Cameron 			dev_err(&pdev->dev, "no suitable DMA available\n");
4843edd16368SStephen M. Cameron 			goto clean1;
4844edd16368SStephen M. Cameron 		}
4845ecd9aad4SStephen M. Cameron 	}
4846edd16368SStephen M. Cameron 
4847edd16368SStephen M. Cameron 	/* make sure the board interrupts are off */
4848edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
484910f66018SStephen M. Cameron 
48500ae01a32SStephen M. Cameron 	if (hpsa_request_irq(h, do_hpsa_intr_msi, do_hpsa_intr_intx))
4851edd16368SStephen M. Cameron 		goto clean2;
4852303932fdSDon Brace 	dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
4853303932fdSDon Brace 	       h->devname, pdev->device,
4854a9a3a273SStephen M. Cameron 	       h->intr[h->intr_mode], dac ? "" : " not");
48552e9d1b36SStephen M. Cameron 	if (hpsa_allocate_cmd_pool(h))
4856edd16368SStephen M. Cameron 		goto clean4;
485733a2ffceSStephen M. Cameron 	if (hpsa_allocate_sg_chain_blocks(h))
485833a2ffceSStephen M. Cameron 		goto clean4;
4859a08a8471SStephen M. Cameron 	init_waitqueue_head(&h->scan_wait_queue);
4860a08a8471SStephen M. Cameron 	h->scan_finished = 1; /* no scan currently in progress */
4861edd16368SStephen M. Cameron 
4862edd16368SStephen M. Cameron 	pci_set_drvdata(pdev, h);
48639a41338eSStephen M. Cameron 	h->ndevices = 0;
48649a41338eSStephen M. Cameron 	h->scsi_host = NULL;
48659a41338eSStephen M. Cameron 	spin_lock_init(&h->devlock);
486664670ac8SStephen M. Cameron 	hpsa_put_ctlr_into_performant_mode(h);
486764670ac8SStephen M. Cameron 
486864670ac8SStephen M. Cameron 	/* At this point, the controller is ready to take commands.
486964670ac8SStephen M. Cameron 	 * Now, if reset_devices and the hard reset didn't work, try
487064670ac8SStephen M. Cameron 	 * the soft reset and see if that works.
487164670ac8SStephen M. Cameron 	 */
487264670ac8SStephen M. Cameron 	if (try_soft_reset) {
487364670ac8SStephen M. Cameron 
487464670ac8SStephen M. Cameron 		/* This is kind of gross.  We may or may not get a completion
487564670ac8SStephen M. Cameron 		 * from the soft reset command, and if we do, then the value
487664670ac8SStephen M. Cameron 		 * from the fifo may or may not be valid.  So, we wait 10 secs
487764670ac8SStephen M. Cameron 		 * after the reset throwing away any completions we get during
487864670ac8SStephen M. Cameron 		 * that time.  Unregister the interrupt handler and register
487964670ac8SStephen M. Cameron 		 * fake ones to scoop up any residual completions.
488064670ac8SStephen M. Cameron 		 */
488164670ac8SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
488264670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
488364670ac8SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
4884254f796bSMatt Gates 		free_irqs(h);
488564670ac8SStephen M. Cameron 		rc = hpsa_request_irq(h, hpsa_msix_discard_completions,
488664670ac8SStephen M. Cameron 					hpsa_intx_discard_completions);
488764670ac8SStephen M. Cameron 		if (rc) {
488864670ac8SStephen M. Cameron 			dev_warn(&h->pdev->dev, "Failed to request_irq after "
488964670ac8SStephen M. Cameron 				"soft reset.\n");
489064670ac8SStephen M. Cameron 			goto clean4;
489164670ac8SStephen M. Cameron 		}
489264670ac8SStephen M. Cameron 
489364670ac8SStephen M. Cameron 		rc = hpsa_kdump_soft_reset(h);
489464670ac8SStephen M. Cameron 		if (rc)
489564670ac8SStephen M. Cameron 			/* Neither hard nor soft reset worked, we're hosed. */
489664670ac8SStephen M. Cameron 			goto clean4;
489764670ac8SStephen M. Cameron 
489864670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev, "Board READY.\n");
489964670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev,
490064670ac8SStephen M. Cameron 			"Waiting for stale completions to drain.\n");
490164670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_ON);
490264670ac8SStephen M. Cameron 		msleep(10000);
490364670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
490464670ac8SStephen M. Cameron 
490564670ac8SStephen M. Cameron 		rc = controller_reset_failed(h->cfgtable);
490664670ac8SStephen M. Cameron 		if (rc)
490764670ac8SStephen M. Cameron 			dev_info(&h->pdev->dev,
490864670ac8SStephen M. Cameron 				"Soft reset appears to have failed.\n");
490964670ac8SStephen M. Cameron 
491064670ac8SStephen M. Cameron 		/* since the controller's reset, we have to go back and re-init
491164670ac8SStephen M. Cameron 		 * everything.  Easiest to just forget what we've done and do it
491264670ac8SStephen M. Cameron 		 * all over again.
491364670ac8SStephen M. Cameron 		 */
491464670ac8SStephen M. Cameron 		hpsa_undo_allocations_after_kdump_soft_reset(h);
491564670ac8SStephen M. Cameron 		try_soft_reset = 0;
491664670ac8SStephen M. Cameron 		if (rc)
491764670ac8SStephen M. Cameron 			/* don't go to clean4, we already unallocated */
491864670ac8SStephen M. Cameron 			return -ENODEV;
491964670ac8SStephen M. Cameron 
492064670ac8SStephen M. Cameron 		goto reinit_after_soft_reset;
492164670ac8SStephen M. Cameron 	}
4922edd16368SStephen M. Cameron 
4923edd16368SStephen M. Cameron 	/* Turn the interrupts on so we can service requests */
4924edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_ON);
4925edd16368SStephen M. Cameron 
4926339b2b14SStephen M. Cameron 	hpsa_hba_inquiry(h);
4927edd16368SStephen M. Cameron 	hpsa_register_scsi(h);	/* hook ourselves into SCSI subsystem */
4928a0c12413SStephen M. Cameron 	start_controller_lockup_detector(h);
4929*88bf6d62SStephen M. Cameron 	return 0;
4930edd16368SStephen M. Cameron 
4931edd16368SStephen M. Cameron clean4:
493233a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
49332e9d1b36SStephen M. Cameron 	hpsa_free_cmd_pool(h);
4934254f796bSMatt Gates 	free_irqs(h);
4935edd16368SStephen M. Cameron clean2:
4936edd16368SStephen M. Cameron clean1:
4937edd16368SStephen M. Cameron 	kfree(h);
4938ecd9aad4SStephen M. Cameron 	return rc;
4939edd16368SStephen M. Cameron }
4940edd16368SStephen M. Cameron 
4941edd16368SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h)
4942edd16368SStephen M. Cameron {
4943edd16368SStephen M. Cameron 	char *flush_buf;
4944edd16368SStephen M. Cameron 	struct CommandList *c;
4945edd16368SStephen M. Cameron 
4946edd16368SStephen M. Cameron 	flush_buf = kzalloc(4, GFP_KERNEL);
4947edd16368SStephen M. Cameron 	if (!flush_buf)
4948edd16368SStephen M. Cameron 		return;
4949edd16368SStephen M. Cameron 
4950edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
4951edd16368SStephen M. Cameron 	if (!c) {
4952edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
4953edd16368SStephen M. Cameron 		goto out_of_memory;
4954edd16368SStephen M. Cameron 	}
4955a2dac136SStephen M. Cameron 	if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
4956a2dac136SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_CMD)) {
4957a2dac136SStephen M. Cameron 		goto out;
4958a2dac136SStephen M. Cameron 	}
4959edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
4960edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus != 0)
4961a2dac136SStephen M. Cameron out:
4962edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
4963edd16368SStephen M. Cameron 			"error flushing cache on controller\n");
4964edd16368SStephen M. Cameron 	cmd_special_free(h, c);
4965edd16368SStephen M. Cameron out_of_memory:
4966edd16368SStephen M. Cameron 	kfree(flush_buf);
4967edd16368SStephen M. Cameron }
4968edd16368SStephen M. Cameron 
4969edd16368SStephen M. Cameron static void hpsa_shutdown(struct pci_dev *pdev)
4970edd16368SStephen M. Cameron {
4971edd16368SStephen M. Cameron 	struct ctlr_info *h;
4972edd16368SStephen M. Cameron 
4973edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
4974edd16368SStephen M. Cameron 	/* Turn board interrupts off  and send the flush cache command
4975edd16368SStephen M. Cameron 	 * sendcmd will turn off interrupt, and send the flush...
4976edd16368SStephen M. Cameron 	 * To write all data in the battery backed cache to disks
4977edd16368SStephen M. Cameron 	 */
4978edd16368SStephen M. Cameron 	hpsa_flush_cache(h);
4979edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
49800097f0f4SStephen M. Cameron 	hpsa_free_irqs_and_disable_msix(h);
4981edd16368SStephen M. Cameron }
4982edd16368SStephen M. Cameron 
49836f039790SGreg Kroah-Hartman static void hpsa_free_device_info(struct ctlr_info *h)
498455e14e76SStephen M. Cameron {
498555e14e76SStephen M. Cameron 	int i;
498655e14e76SStephen M. Cameron 
498755e14e76SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++)
498855e14e76SStephen M. Cameron 		kfree(h->dev[i]);
498955e14e76SStephen M. Cameron }
499055e14e76SStephen M. Cameron 
49916f039790SGreg Kroah-Hartman static void hpsa_remove_one(struct pci_dev *pdev)
4992edd16368SStephen M. Cameron {
4993edd16368SStephen M. Cameron 	struct ctlr_info *h;
4994edd16368SStephen M. Cameron 
4995edd16368SStephen M. Cameron 	if (pci_get_drvdata(pdev) == NULL) {
4996edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "unable to remove device\n");
4997edd16368SStephen M. Cameron 		return;
4998edd16368SStephen M. Cameron 	}
4999edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
5000a0c12413SStephen M. Cameron 	stop_controller_lockup_detector(h);
5001edd16368SStephen M. Cameron 	hpsa_unregister_scsi(h);	/* unhook from SCSI subsystem */
5002edd16368SStephen M. Cameron 	hpsa_shutdown(pdev);
5003edd16368SStephen M. Cameron 	iounmap(h->vaddr);
5004204892e9SStephen M. Cameron 	iounmap(h->transtable);
5005204892e9SStephen M. Cameron 	iounmap(h->cfgtable);
500655e14e76SStephen M. Cameron 	hpsa_free_device_info(h);
500733a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
5008edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev,
5009edd16368SStephen M. Cameron 		h->nr_cmds * sizeof(struct CommandList),
5010edd16368SStephen M. Cameron 		h->cmd_pool, h->cmd_pool_dhandle);
5011edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev,
5012edd16368SStephen M. Cameron 		h->nr_cmds * sizeof(struct ErrorInfo),
5013edd16368SStephen M. Cameron 		h->errinfo_pool, h->errinfo_pool_dhandle);
5014303932fdSDon Brace 	pci_free_consistent(h->pdev, h->reply_pool_size,
5015303932fdSDon Brace 		h->reply_pool, h->reply_pool_dhandle);
5016edd16368SStephen M. Cameron 	kfree(h->cmd_pool_bits);
5017303932fdSDon Brace 	kfree(h->blockFetchTable);
5018339b2b14SStephen M. Cameron 	kfree(h->hba_inquiry_data);
5019f0bd0b68SStephen M. Cameron 	pci_disable_device(pdev);
5020edd16368SStephen M. Cameron 	pci_release_regions(pdev);
5021edd16368SStephen M. Cameron 	kfree(h);
5022edd16368SStephen M. Cameron }
5023edd16368SStephen M. Cameron 
5024edd16368SStephen M. Cameron static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
5025edd16368SStephen M. Cameron 	__attribute__((unused)) pm_message_t state)
5026edd16368SStephen M. Cameron {
5027edd16368SStephen M. Cameron 	return -ENOSYS;
5028edd16368SStephen M. Cameron }
5029edd16368SStephen M. Cameron 
5030edd16368SStephen M. Cameron static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
5031edd16368SStephen M. Cameron {
5032edd16368SStephen M. Cameron 	return -ENOSYS;
5033edd16368SStephen M. Cameron }
5034edd16368SStephen M. Cameron 
5035edd16368SStephen M. Cameron static struct pci_driver hpsa_pci_driver = {
5036f79cfec6SStephen M. Cameron 	.name = HPSA,
5037edd16368SStephen M. Cameron 	.probe = hpsa_init_one,
50386f039790SGreg Kroah-Hartman 	.remove = hpsa_remove_one,
5039edd16368SStephen M. Cameron 	.id_table = hpsa_pci_device_id,	/* id_table */
5040edd16368SStephen M. Cameron 	.shutdown = hpsa_shutdown,
5041edd16368SStephen M. Cameron 	.suspend = hpsa_suspend,
5042edd16368SStephen M. Cameron 	.resume = hpsa_resume,
5043edd16368SStephen M. Cameron };
5044edd16368SStephen M. Cameron 
5045303932fdSDon Brace /* Fill in bucket_map[], given nsgs (the max number of
5046303932fdSDon Brace  * scatter gather elements supported) and bucket[],
5047303932fdSDon Brace  * which is an array of 8 integers.  The bucket[] array
5048303932fdSDon Brace  * contains 8 different DMA transfer sizes (in 16
5049303932fdSDon Brace  * byte increments) which the controller uses to fetch
5050303932fdSDon Brace  * commands.  This function fills in bucket_map[], which
5051303932fdSDon Brace  * maps a given number of scatter gather elements to one of
5052303932fdSDon Brace  * the 8 DMA transfer sizes.  The point of it is to allow the
5053303932fdSDon Brace  * controller to only do as much DMA as needed to fetch the
5054303932fdSDon Brace  * command, with the DMA transfer size encoded in the lower
5055303932fdSDon Brace  * bits of the command address.
5056303932fdSDon Brace  */
5057303932fdSDon Brace static void  calc_bucket_map(int bucket[], int num_buckets,
5058303932fdSDon Brace 	int nsgs, int *bucket_map)
5059303932fdSDon Brace {
5060303932fdSDon Brace 	int i, j, b, size;
5061303932fdSDon Brace 
5062303932fdSDon Brace 	/* even a command with 0 SGs requires 4 blocks */
5063303932fdSDon Brace #define MINIMUM_TRANSFER_BLOCKS 4
5064303932fdSDon Brace #define NUM_BUCKETS 8
5065303932fdSDon Brace 	/* Note, bucket_map must have nsgs+1 entries. */
5066303932fdSDon Brace 	for (i = 0; i <= nsgs; i++) {
5067303932fdSDon Brace 		/* Compute size of a command with i SG entries */
5068303932fdSDon Brace 		size = i + MINIMUM_TRANSFER_BLOCKS;
5069303932fdSDon Brace 		b = num_buckets; /* Assume the biggest bucket */
5070303932fdSDon Brace 		/* Find the bucket that is just big enough */
5071303932fdSDon Brace 		for (j = 0; j < 8; j++) {
5072303932fdSDon Brace 			if (bucket[j] >= size) {
5073303932fdSDon Brace 				b = j;
5074303932fdSDon Brace 				break;
5075303932fdSDon Brace 			}
5076303932fdSDon Brace 		}
5077303932fdSDon Brace 		/* for a command with i SG entries, use bucket b. */
5078303932fdSDon Brace 		bucket_map[i] = b;
5079303932fdSDon Brace 	}
5080303932fdSDon Brace }
5081303932fdSDon Brace 
50826f039790SGreg Kroah-Hartman static void hpsa_enter_performant_mode(struct ctlr_info *h, u32 use_short_tags)
5083303932fdSDon Brace {
50846c311b57SStephen M. Cameron 	int i;
50856c311b57SStephen M. Cameron 	unsigned long register_value;
5086def342bdSStephen M. Cameron 
5087def342bdSStephen M. Cameron 	/* This is a bit complicated.  There are 8 registers on
5088def342bdSStephen M. Cameron 	 * the controller which we write to to tell it 8 different
5089def342bdSStephen M. Cameron 	 * sizes of commands which there may be.  It's a way of
5090def342bdSStephen M. Cameron 	 * reducing the DMA done to fetch each command.  Encoded into
5091def342bdSStephen M. Cameron 	 * each command's tag are 3 bits which communicate to the controller
5092def342bdSStephen M. Cameron 	 * which of the eight sizes that command fits within.  The size of
5093def342bdSStephen M. Cameron 	 * each command depends on how many scatter gather entries there are.
5094def342bdSStephen M. Cameron 	 * Each SG entry requires 16 bytes.  The eight registers are programmed
5095def342bdSStephen M. Cameron 	 * with the number of 16-byte blocks a command of that size requires.
5096def342bdSStephen M. Cameron 	 * The smallest command possible requires 5 such 16 byte blocks.
5097d66ae08bSStephen M. Cameron 	 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
5098def342bdSStephen M. Cameron 	 * blocks.  Note, this only extends to the SG entries contained
5099def342bdSStephen M. Cameron 	 * within the command block, and does not extend to chained blocks
5100def342bdSStephen M. Cameron 	 * of SG elements.   bft[] contains the eight values we write to
5101def342bdSStephen M. Cameron 	 * the registers.  They are not evenly distributed, but have more
5102def342bdSStephen M. Cameron 	 * sizes for small commands, and fewer sizes for larger commands.
5103def342bdSStephen M. Cameron 	 */
5104d66ae08bSStephen M. Cameron 	int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
5105d66ae08bSStephen M. Cameron 	BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
5106303932fdSDon Brace 	/*  5 = 1 s/g entry or 4k
5107303932fdSDon Brace 	 *  6 = 2 s/g entry or 8k
5108303932fdSDon Brace 	 *  8 = 4 s/g entry or 16k
5109303932fdSDon Brace 	 * 10 = 6 s/g entry or 24k
5110303932fdSDon Brace 	 */
5111303932fdSDon Brace 
5112303932fdSDon Brace 	/* Controller spec: zero out this buffer. */
5113303932fdSDon Brace 	memset(h->reply_pool, 0, h->reply_pool_size);
5114303932fdSDon Brace 
5115d66ae08bSStephen M. Cameron 	bft[7] = SG_ENTRIES_IN_CMD + 4;
5116d66ae08bSStephen M. Cameron 	calc_bucket_map(bft, ARRAY_SIZE(bft),
5117d66ae08bSStephen M. Cameron 				SG_ENTRIES_IN_CMD, h->blockFetchTable);
5118303932fdSDon Brace 	for (i = 0; i < 8; i++)
5119303932fdSDon Brace 		writel(bft[i], &h->transtable->BlockFetch[i]);
5120303932fdSDon Brace 
5121303932fdSDon Brace 	/* size of controller ring buffer */
5122303932fdSDon Brace 	writel(h->max_commands, &h->transtable->RepQSize);
5123254f796bSMatt Gates 	writel(h->nreply_queues, &h->transtable->RepQCount);
5124303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrLow32);
5125303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrHigh32);
5126254f796bSMatt Gates 
5127254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
5128254f796bSMatt Gates 		writel(0, &h->transtable->RepQAddr[i].upper);
5129254f796bSMatt Gates 		writel(h->reply_pool_dhandle +
5130254f796bSMatt Gates 			(h->max_commands * sizeof(u64) * i),
5131254f796bSMatt Gates 			&h->transtable->RepQAddr[i].lower);
5132254f796bSMatt Gates 	}
5133254f796bSMatt Gates 
5134254f796bSMatt Gates 	writel(CFGTBL_Trans_Performant | use_short_tags |
5135254f796bSMatt Gates 		CFGTBL_Trans_enable_directed_msix,
5136303932fdSDon Brace 		&(h->cfgtable->HostWrite.TransportRequest));
5137303932fdSDon Brace 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
51383f4336f3SStephen M. Cameron 	hpsa_wait_for_mode_change_ack(h);
5139303932fdSDon Brace 	register_value = readl(&(h->cfgtable->TransportActive));
5140303932fdSDon Brace 	if (!(register_value & CFGTBL_Trans_Performant)) {
5141303932fdSDon Brace 		dev_warn(&h->pdev->dev, "unable to get board into"
5142303932fdSDon Brace 					" performant mode\n");
5143303932fdSDon Brace 		return;
5144303932fdSDon Brace 	}
5145960a30e7SStephen M. Cameron 	/* Change the access methods to the performant access methods */
5146960a30e7SStephen M. Cameron 	h->access = SA5_performant_access;
5147960a30e7SStephen M. Cameron 	h->transMethod = CFGTBL_Trans_Performant;
51486c311b57SStephen M. Cameron }
51496c311b57SStephen M. Cameron 
51506f039790SGreg Kroah-Hartman static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
51516c311b57SStephen M. Cameron {
51526c311b57SStephen M. Cameron 	u32 trans_support;
5153254f796bSMatt Gates 	int i;
51546c311b57SStephen M. Cameron 
515502ec19c8SStephen M. Cameron 	if (hpsa_simple_mode)
515602ec19c8SStephen M. Cameron 		return;
515702ec19c8SStephen M. Cameron 
51586c311b57SStephen M. Cameron 	trans_support = readl(&(h->cfgtable->TransportSupport));
51596c311b57SStephen M. Cameron 	if (!(trans_support & PERFORMANT_MODE))
51606c311b57SStephen M. Cameron 		return;
51616c311b57SStephen M. Cameron 
5162254f796bSMatt Gates 	h->nreply_queues = h->msix_vector ? MAX_REPLY_QUEUES : 1;
5163cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
51646c311b57SStephen M. Cameron 	/* Performant mode ring buffer and supporting data structures */
5165254f796bSMatt Gates 	h->reply_pool_size = h->max_commands * sizeof(u64) * h->nreply_queues;
51666c311b57SStephen M. Cameron 	h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size,
51676c311b57SStephen M. Cameron 				&(h->reply_pool_dhandle));
51686c311b57SStephen M. Cameron 
5169254f796bSMatt Gates 	for (i = 0; i < h->nreply_queues; i++) {
5170254f796bSMatt Gates 		h->reply_queue[i].head = &h->reply_pool[h->max_commands * i];
5171254f796bSMatt Gates 		h->reply_queue[i].size = h->max_commands;
5172254f796bSMatt Gates 		h->reply_queue[i].wraparound = 1;  /* spec: init to 1 */
5173254f796bSMatt Gates 		h->reply_queue[i].current_entry = 0;
5174254f796bSMatt Gates 	}
5175254f796bSMatt Gates 
51766c311b57SStephen M. Cameron 	/* Need a block fetch table for performant mode */
5177d66ae08bSStephen M. Cameron 	h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
51786c311b57SStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
51796c311b57SStephen M. Cameron 
51806c311b57SStephen M. Cameron 	if ((h->reply_pool == NULL)
51816c311b57SStephen M. Cameron 		|| (h->blockFetchTable == NULL))
51826c311b57SStephen M. Cameron 		goto clean_up;
51836c311b57SStephen M. Cameron 
5184960a30e7SStephen M. Cameron 	hpsa_enter_performant_mode(h,
5185960a30e7SStephen M. Cameron 		trans_support & CFGTBL_Trans_use_short_tags);
5186303932fdSDon Brace 
5187303932fdSDon Brace 	return;
5188303932fdSDon Brace 
5189303932fdSDon Brace clean_up:
5190303932fdSDon Brace 	if (h->reply_pool)
5191303932fdSDon Brace 		pci_free_consistent(h->pdev, h->reply_pool_size,
5192303932fdSDon Brace 			h->reply_pool, h->reply_pool_dhandle);
5193303932fdSDon Brace 	kfree(h->blockFetchTable);
5194303932fdSDon Brace }
5195303932fdSDon Brace 
5196edd16368SStephen M. Cameron /*
5197edd16368SStephen M. Cameron  *  This is it.  Register the PCI driver information for the cards we control
5198edd16368SStephen M. Cameron  *  the OS will call our registered routines when it finds one of our cards.
5199edd16368SStephen M. Cameron  */
5200edd16368SStephen M. Cameron static int __init hpsa_init(void)
5201edd16368SStephen M. Cameron {
520231468401SMike Miller 	return pci_register_driver(&hpsa_pci_driver);
5203edd16368SStephen M. Cameron }
5204edd16368SStephen M. Cameron 
5205edd16368SStephen M. Cameron static void __exit hpsa_cleanup(void)
5206edd16368SStephen M. Cameron {
5207edd16368SStephen M. Cameron 	pci_unregister_driver(&hpsa_pci_driver);
5208edd16368SStephen M. Cameron }
5209edd16368SStephen M. Cameron 
5210edd16368SStephen M. Cameron module_init(hpsa_init);
5211edd16368SStephen M. Cameron module_exit(hpsa_cleanup);
5212