xref: /openbmc/linux/drivers/scsi/hpsa.c (revision 9c2fc1605fb917531f7dfb6fa2e84b56332a05b4)
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 '.' */
5731468401SMike Miller #define HPSA_DRIVER_VERSION "2.0.2-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},
92edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x324a},
93edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x324b},
94f8b01eb9SMike Miller 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSE,     0x103C, 0x3233},
959143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3350},
969143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3351},
979143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3352},
989143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3353},
999143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3354},
1009143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3355},
1019143a961Sscameron@beardog.cce.hp.com 	{PCI_VENDOR_ID_HP,     PCI_DEVICE_ID_HP_CISSF,     0x103C, 0x3356},
102edd16368SStephen M. Cameron 	{PCI_VENDOR_ID_HP,     PCI_ANY_ID,	PCI_ANY_ID, PCI_ANY_ID,
103edd16368SStephen M. Cameron 		PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
104edd16368SStephen M. Cameron 	{0,}
105edd16368SStephen M. Cameron };
106edd16368SStephen M. Cameron 
107edd16368SStephen M. Cameron MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
108edd16368SStephen M. Cameron 
109edd16368SStephen M. Cameron /*  board_id = Subsystem Device ID & Vendor ID
110edd16368SStephen M. Cameron  *  product = Marketing Name for the board
111edd16368SStephen M. Cameron  *  access = Address of the struct of function pointers
112edd16368SStephen M. Cameron  */
113edd16368SStephen M. Cameron static struct board_type products[] = {
114edd16368SStephen M. Cameron 	{0x3241103C, "Smart Array P212", &SA5_access},
115edd16368SStephen M. Cameron 	{0x3243103C, "Smart Array P410", &SA5_access},
116edd16368SStephen M. Cameron 	{0x3245103C, "Smart Array P410i", &SA5_access},
117edd16368SStephen M. Cameron 	{0x3247103C, "Smart Array P411", &SA5_access},
118edd16368SStephen M. Cameron 	{0x3249103C, "Smart Array P812", &SA5_access},
119edd16368SStephen M. Cameron 	{0x324a103C, "Smart Array P712m", &SA5_access},
120edd16368SStephen M. Cameron 	{0x324b103C, "Smart Array P711m", &SA5_access},
1219143a961Sscameron@beardog.cce.hp.com 	{0x3350103C, "Smart Array", &SA5_access},
1229143a961Sscameron@beardog.cce.hp.com 	{0x3351103C, "Smart Array", &SA5_access},
1239143a961Sscameron@beardog.cce.hp.com 	{0x3352103C, "Smart Array", &SA5_access},
1249143a961Sscameron@beardog.cce.hp.com 	{0x3353103C, "Smart Array", &SA5_access},
1259143a961Sscameron@beardog.cce.hp.com 	{0x3354103C, "Smart Array", &SA5_access},
1269143a961Sscameron@beardog.cce.hp.com 	{0x3355103C, "Smart Array", &SA5_access},
1279143a961Sscameron@beardog.cce.hp.com 	{0x3356103C, "Smart Array", &SA5_access},
128edd16368SStephen M. Cameron 	{0xFFFF103C, "Unknown Smart Array", &SA5_access},
129edd16368SStephen M. Cameron };
130edd16368SStephen M. Cameron 
131edd16368SStephen M. Cameron static int number_of_controllers;
132edd16368SStephen M. Cameron 
133a0c12413SStephen M. Cameron static struct list_head hpsa_ctlr_list = LIST_HEAD_INIT(hpsa_ctlr_list);
134a0c12413SStephen M. Cameron static spinlock_t lockup_detector_lock;
135a0c12413SStephen M. Cameron static struct task_struct *hpsa_lockup_detector;
136a0c12413SStephen M. Cameron 
13710f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id);
13810f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id);
139edd16368SStephen M. Cameron static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg);
140edd16368SStephen M. Cameron static void start_io(struct ctlr_info *h);
141edd16368SStephen M. Cameron 
142edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
143edd16368SStephen M. Cameron static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg);
144edd16368SStephen M. Cameron #endif
145edd16368SStephen M. Cameron 
146edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c);
147edd16368SStephen M. Cameron static void cmd_special_free(struct ctlr_info *h, struct CommandList *c);
148edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h);
149edd16368SStephen M. Cameron static struct CommandList *cmd_special_alloc(struct ctlr_info *h);
15001a02ffcSStephen M. Cameron static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
15101a02ffcSStephen M. Cameron 	void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
152edd16368SStephen M. Cameron 	int cmd_type);
153edd16368SStephen M. Cameron 
154f281233dSJeff Garzik static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
155a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *);
156a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
157a08a8471SStephen M. Cameron 	unsigned long elapsed_time);
158667e23d4SStephen M. Cameron static int hpsa_change_queue_depth(struct scsi_device *sdev,
159667e23d4SStephen M. Cameron 	int qdepth, int reason);
160edd16368SStephen M. Cameron 
161edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
162edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev);
163edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev);
164edd16368SStephen M. Cameron 
165edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
166edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
167edd16368SStephen M. Cameron 	struct CommandList *c);
168edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
169edd16368SStephen M. Cameron 	struct CommandList *c);
170303932fdSDon Brace /* performant mode helper functions */
171303932fdSDon Brace static void calc_bucket_map(int *bucket, int num_buckets,
172303932fdSDon Brace 	int nsgs, int *bucket_map);
1737136f9a7SStephen M. Cameron static __devinit void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
174303932fdSDon Brace static inline u32 next_command(struct ctlr_info *h);
1751df8552aSStephen M. Cameron static int __devinit hpsa_find_cfg_addrs(struct pci_dev *pdev,
1761df8552aSStephen M. Cameron 	void __iomem *vaddr, u32 *cfg_base_addr, u64 *cfg_base_addr_index,
1771df8552aSStephen M. Cameron 	u64 *cfg_offset);
1781df8552aSStephen M. Cameron static int __devinit hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
1791df8552aSStephen M. Cameron 	unsigned long *memory_bar);
18018867659SStephen M. Cameron static int __devinit hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
181fe5389c8SStephen M. Cameron static int __devinit hpsa_wait_for_board_state(struct pci_dev *pdev,
182fe5389c8SStephen M. Cameron 	void __iomem *vaddr, int wait_for_ready);
183fe5389c8SStephen M. Cameron #define BOARD_NOT_READY 0
184fe5389c8SStephen M. Cameron #define BOARD_READY 1
185edd16368SStephen M. Cameron 
186edd16368SStephen M. Cameron static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
187edd16368SStephen M. Cameron {
188edd16368SStephen M. Cameron 	unsigned long *priv = shost_priv(sdev->host);
189edd16368SStephen M. Cameron 	return (struct ctlr_info *) *priv;
190edd16368SStephen M. Cameron }
191edd16368SStephen M. Cameron 
192a23513e8SStephen M. Cameron static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
193a23513e8SStephen M. Cameron {
194a23513e8SStephen M. Cameron 	unsigned long *priv = shost_priv(sh);
195a23513e8SStephen M. Cameron 	return (struct ctlr_info *) *priv;
196a23513e8SStephen M. Cameron }
197a23513e8SStephen M. Cameron 
198edd16368SStephen M. Cameron static int check_for_unit_attention(struct ctlr_info *h,
199edd16368SStephen M. Cameron 	struct CommandList *c)
200edd16368SStephen M. Cameron {
201edd16368SStephen M. Cameron 	if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
202edd16368SStephen M. Cameron 		return 0;
203edd16368SStephen M. Cameron 
204edd16368SStephen M. Cameron 	switch (c->err_info->SenseInfo[12]) {
205edd16368SStephen M. Cameron 	case STATE_CHANGED:
206f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: a state change "
207edd16368SStephen M. Cameron 			"detected, command retried\n", h->ctlr);
208edd16368SStephen M. Cameron 		break;
209edd16368SStephen M. Cameron 	case LUN_FAILED:
210f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: LUN failure "
211edd16368SStephen M. Cameron 			"detected, action required\n", h->ctlr);
212edd16368SStephen M. Cameron 		break;
213edd16368SStephen M. Cameron 	case REPORT_LUNS_CHANGED:
214f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: report LUN data "
21531468401SMike Miller 			"changed, action required\n", h->ctlr);
216edd16368SStephen M. Cameron 	/*
2174f4eb9f1SScott Teel 	 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
2184f4eb9f1SScott Teel 	 * target (array) devices.
219edd16368SStephen M. Cameron 	 */
220edd16368SStephen M. Cameron 		break;
221edd16368SStephen M. Cameron 	case POWER_OR_RESET:
222f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: a power on "
223edd16368SStephen M. Cameron 			"or device reset detected\n", h->ctlr);
224edd16368SStephen M. Cameron 		break;
225edd16368SStephen M. Cameron 	case UNIT_ATTENTION_CLEARED:
226f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: unit attention "
227edd16368SStephen M. Cameron 		    "cleared by another initiator\n", h->ctlr);
228edd16368SStephen M. Cameron 		break;
229edd16368SStephen M. Cameron 	default:
230f79cfec6SStephen M. Cameron 		dev_warn(&h->pdev->dev, HPSA "%d: unknown "
231edd16368SStephen M. Cameron 			"unit attention detected\n", h->ctlr);
232edd16368SStephen M. Cameron 		break;
233edd16368SStephen M. Cameron 	}
234edd16368SStephen M. Cameron 	return 1;
235edd16368SStephen M. Cameron }
236edd16368SStephen M. Cameron 
237852af20aSMatt Bondurant static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
238852af20aSMatt Bondurant {
239852af20aSMatt Bondurant 	if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
240852af20aSMatt Bondurant 		(c->err_info->ScsiStatus != SAM_STAT_BUSY &&
241852af20aSMatt Bondurant 		 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
242852af20aSMatt Bondurant 		return 0;
243852af20aSMatt Bondurant 	dev_warn(&h->pdev->dev, HPSA "device busy");
244852af20aSMatt Bondurant 	return 1;
245852af20aSMatt Bondurant }
246852af20aSMatt Bondurant 
247edd16368SStephen M. Cameron static ssize_t host_store_rescan(struct device *dev,
248edd16368SStephen M. Cameron 				 struct device_attribute *attr,
249edd16368SStephen M. Cameron 				 const char *buf, size_t count)
250edd16368SStephen M. Cameron {
251edd16368SStephen M. Cameron 	struct ctlr_info *h;
252edd16368SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
253a23513e8SStephen M. Cameron 	h = shost_to_hba(shost);
25431468401SMike Miller 	hpsa_scan_start(h->scsi_host);
255edd16368SStephen M. Cameron 	return count;
256edd16368SStephen M. Cameron }
257edd16368SStephen M. Cameron 
258d28ce020SStephen M. Cameron static ssize_t host_show_firmware_revision(struct device *dev,
259d28ce020SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
260d28ce020SStephen M. Cameron {
261d28ce020SStephen M. Cameron 	struct ctlr_info *h;
262d28ce020SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
263d28ce020SStephen M. Cameron 	unsigned char *fwrev;
264d28ce020SStephen M. Cameron 
265d28ce020SStephen M. Cameron 	h = shost_to_hba(shost);
266d28ce020SStephen M. Cameron 	if (!h->hba_inquiry_data)
267d28ce020SStephen M. Cameron 		return 0;
268d28ce020SStephen M. Cameron 	fwrev = &h->hba_inquiry_data[32];
269d28ce020SStephen M. Cameron 	return snprintf(buf, 20, "%c%c%c%c\n",
270d28ce020SStephen M. Cameron 		fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
271d28ce020SStephen M. Cameron }
272d28ce020SStephen M. Cameron 
27394a13649SStephen M. Cameron static ssize_t host_show_commands_outstanding(struct device *dev,
27494a13649SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
27594a13649SStephen M. Cameron {
27694a13649SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
27794a13649SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(shost);
27894a13649SStephen M. Cameron 
27994a13649SStephen M. Cameron 	return snprintf(buf, 20, "%d\n", h->commands_outstanding);
28094a13649SStephen M. Cameron }
28194a13649SStephen M. Cameron 
282745a7a25SStephen M. Cameron static ssize_t host_show_transport_mode(struct device *dev,
283745a7a25SStephen M. Cameron 	struct device_attribute *attr, char *buf)
284745a7a25SStephen M. Cameron {
285745a7a25SStephen M. Cameron 	struct ctlr_info *h;
286745a7a25SStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
287745a7a25SStephen M. Cameron 
288745a7a25SStephen M. Cameron 	h = shost_to_hba(shost);
289745a7a25SStephen M. Cameron 	return snprintf(buf, 20, "%s\n",
290960a30e7SStephen M. Cameron 		h->transMethod & CFGTBL_Trans_Performant ?
291745a7a25SStephen M. Cameron 			"performant" : "simple");
292745a7a25SStephen M. Cameron }
293745a7a25SStephen M. Cameron 
29446380786SStephen M. Cameron /* List of controllers which cannot be hard reset on kexec with reset_devices */
295941b1cdaSStephen M. Cameron static u32 unresettable_controller[] = {
296941b1cdaSStephen M. Cameron 	0x324a103C, /* Smart Array P712m */
297941b1cdaSStephen M. Cameron 	0x324b103C, /* SmartArray P711m */
298941b1cdaSStephen M. Cameron 	0x3223103C, /* Smart Array P800 */
299941b1cdaSStephen M. Cameron 	0x3234103C, /* Smart Array P400 */
300941b1cdaSStephen M. Cameron 	0x3235103C, /* Smart Array P400i */
301941b1cdaSStephen M. Cameron 	0x3211103C, /* Smart Array E200i */
302941b1cdaSStephen M. Cameron 	0x3212103C, /* Smart Array E200 */
303941b1cdaSStephen M. Cameron 	0x3213103C, /* Smart Array E200i */
304941b1cdaSStephen M. Cameron 	0x3214103C, /* Smart Array E200i */
305941b1cdaSStephen M. Cameron 	0x3215103C, /* Smart Array E200i */
306941b1cdaSStephen M. Cameron 	0x3237103C, /* Smart Array E500 */
307941b1cdaSStephen M. Cameron 	0x323D103C, /* Smart Array P700m */
3087af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
309941b1cdaSStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
310941b1cdaSStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
3115a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
3125a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
3135a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
3145a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
3155a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
3165a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
317941b1cdaSStephen M. Cameron };
318941b1cdaSStephen M. Cameron 
31946380786SStephen M. Cameron /* List of controllers which cannot even be soft reset */
32046380786SStephen M. Cameron static u32 soft_unresettable_controller[] = {
3217af0abbcSTomas Henzl 	0x40800E11, /* Smart Array 5i */
3225a4f934eSTomas Henzl 	0x40700E11, /* Smart Array 5300 */
3235a4f934eSTomas Henzl 	0x40820E11, /* Smart Array 532 */
3245a4f934eSTomas Henzl 	0x40830E11, /* Smart Array 5312 */
3255a4f934eSTomas Henzl 	0x409A0E11, /* Smart Array 641 */
3265a4f934eSTomas Henzl 	0x409B0E11, /* Smart Array 642 */
3275a4f934eSTomas Henzl 	0x40910E11, /* Smart Array 6i */
32846380786SStephen M. Cameron 	/* Exclude 640x boards.  These are two pci devices in one slot
32946380786SStephen M. Cameron 	 * which share a battery backed cache module.  One controls the
33046380786SStephen M. Cameron 	 * cache, the other accesses the cache through the one that controls
33146380786SStephen M. Cameron 	 * it.  If we reset the one controlling the cache, the other will
33246380786SStephen M. Cameron 	 * likely not be happy.  Just forbid resetting this conjoined mess.
33346380786SStephen M. Cameron 	 * The 640x isn't really supported by hpsa anyway.
33446380786SStephen M. Cameron 	 */
33546380786SStephen M. Cameron 	0x409C0E11, /* Smart Array 6400 */
33646380786SStephen M. Cameron 	0x409D0E11, /* Smart Array 6400 EM */
33746380786SStephen M. Cameron };
33846380786SStephen M. Cameron 
33946380786SStephen M. Cameron static int ctlr_is_hard_resettable(u32 board_id)
340941b1cdaSStephen M. Cameron {
341941b1cdaSStephen M. Cameron 	int i;
342941b1cdaSStephen M. Cameron 
343941b1cdaSStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(unresettable_controller); i++)
34446380786SStephen M. Cameron 		if (unresettable_controller[i] == board_id)
345941b1cdaSStephen M. Cameron 			return 0;
346941b1cdaSStephen M. Cameron 	return 1;
347941b1cdaSStephen M. Cameron }
348941b1cdaSStephen M. Cameron 
34946380786SStephen M. Cameron static int ctlr_is_soft_resettable(u32 board_id)
35046380786SStephen M. Cameron {
35146380786SStephen M. Cameron 	int i;
35246380786SStephen M. Cameron 
35346380786SStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(soft_unresettable_controller); i++)
35446380786SStephen M. Cameron 		if (soft_unresettable_controller[i] == board_id)
35546380786SStephen M. Cameron 			return 0;
35646380786SStephen M. Cameron 	return 1;
35746380786SStephen M. Cameron }
35846380786SStephen M. Cameron 
35946380786SStephen M. Cameron static int ctlr_is_resettable(u32 board_id)
36046380786SStephen M. Cameron {
36146380786SStephen M. Cameron 	return ctlr_is_hard_resettable(board_id) ||
36246380786SStephen M. Cameron 		ctlr_is_soft_resettable(board_id);
36346380786SStephen M. Cameron }
36446380786SStephen M. Cameron 
365941b1cdaSStephen M. Cameron static ssize_t host_show_resettable(struct device *dev,
366941b1cdaSStephen M. Cameron 	struct device_attribute *attr, char *buf)
367941b1cdaSStephen M. Cameron {
368941b1cdaSStephen M. Cameron 	struct ctlr_info *h;
369941b1cdaSStephen M. Cameron 	struct Scsi_Host *shost = class_to_shost(dev);
370941b1cdaSStephen M. Cameron 
371941b1cdaSStephen M. Cameron 	h = shost_to_hba(shost);
37246380786SStephen M. Cameron 	return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
373941b1cdaSStephen M. Cameron }
374941b1cdaSStephen M. Cameron 
375edd16368SStephen M. Cameron static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
376edd16368SStephen M. Cameron {
377edd16368SStephen M. Cameron 	return (scsi3addr[3] & 0xC0) == 0x40;
378edd16368SStephen M. Cameron }
379edd16368SStephen M. Cameron 
380edd16368SStephen M. Cameron static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
381edd16368SStephen M. Cameron 	"UNKNOWN"
382edd16368SStephen M. Cameron };
383edd16368SStephen M. Cameron #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
384edd16368SStephen M. Cameron 
385edd16368SStephen M. Cameron static ssize_t raid_level_show(struct device *dev,
386edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
387edd16368SStephen M. Cameron {
388edd16368SStephen M. Cameron 	ssize_t l = 0;
38982a72c0aSStephen M. Cameron 	unsigned char rlevel;
390edd16368SStephen M. Cameron 	struct ctlr_info *h;
391edd16368SStephen M. Cameron 	struct scsi_device *sdev;
392edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
393edd16368SStephen M. Cameron 	unsigned long flags;
394edd16368SStephen M. Cameron 
395edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
396edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
397edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
398edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
399edd16368SStephen M. Cameron 	if (!hdev) {
400edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
401edd16368SStephen M. Cameron 		return -ENODEV;
402edd16368SStephen M. Cameron 	}
403edd16368SStephen M. Cameron 
404edd16368SStephen M. Cameron 	/* Is this even a logical drive? */
405edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
406edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
407edd16368SStephen M. Cameron 		l = snprintf(buf, PAGE_SIZE, "N/A\n");
408edd16368SStephen M. Cameron 		return l;
409edd16368SStephen M. Cameron 	}
410edd16368SStephen M. Cameron 
411edd16368SStephen M. Cameron 	rlevel = hdev->raid_level;
412edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
41382a72c0aSStephen M. Cameron 	if (rlevel > RAID_UNKNOWN)
414edd16368SStephen M. Cameron 		rlevel = RAID_UNKNOWN;
415edd16368SStephen M. Cameron 	l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
416edd16368SStephen M. Cameron 	return l;
417edd16368SStephen M. Cameron }
418edd16368SStephen M. Cameron 
419edd16368SStephen M. Cameron static ssize_t lunid_show(struct device *dev,
420edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
421edd16368SStephen M. Cameron {
422edd16368SStephen M. Cameron 	struct ctlr_info *h;
423edd16368SStephen M. Cameron 	struct scsi_device *sdev;
424edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
425edd16368SStephen M. Cameron 	unsigned long flags;
426edd16368SStephen M. Cameron 	unsigned char lunid[8];
427edd16368SStephen M. Cameron 
428edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
429edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
430edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
431edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
432edd16368SStephen M. Cameron 	if (!hdev) {
433edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
434edd16368SStephen M. Cameron 		return -ENODEV;
435edd16368SStephen M. Cameron 	}
436edd16368SStephen M. Cameron 	memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
437edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
438edd16368SStephen M. Cameron 	return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
439edd16368SStephen M. Cameron 		lunid[0], lunid[1], lunid[2], lunid[3],
440edd16368SStephen M. Cameron 		lunid[4], lunid[5], lunid[6], lunid[7]);
441edd16368SStephen M. Cameron }
442edd16368SStephen M. Cameron 
443edd16368SStephen M. Cameron static ssize_t unique_id_show(struct device *dev,
444edd16368SStephen M. Cameron 	     struct device_attribute *attr, char *buf)
445edd16368SStephen M. Cameron {
446edd16368SStephen M. Cameron 	struct ctlr_info *h;
447edd16368SStephen M. Cameron 	struct scsi_device *sdev;
448edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *hdev;
449edd16368SStephen M. Cameron 	unsigned long flags;
450edd16368SStephen M. Cameron 	unsigned char sn[16];
451edd16368SStephen M. Cameron 
452edd16368SStephen M. Cameron 	sdev = to_scsi_device(dev);
453edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
454edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
455edd16368SStephen M. Cameron 	hdev = sdev->hostdata;
456edd16368SStephen M. Cameron 	if (!hdev) {
457edd16368SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
458edd16368SStephen M. Cameron 		return -ENODEV;
459edd16368SStephen M. Cameron 	}
460edd16368SStephen M. Cameron 	memcpy(sn, hdev->device_id, sizeof(sn));
461edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
462edd16368SStephen M. Cameron 	return snprintf(buf, 16 * 2 + 2,
463edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X"
464edd16368SStephen M. Cameron 			"%02X%02X%02X%02X%02X%02X%02X%02X\n",
465edd16368SStephen M. Cameron 			sn[0], sn[1], sn[2], sn[3],
466edd16368SStephen M. Cameron 			sn[4], sn[5], sn[6], sn[7],
467edd16368SStephen M. Cameron 			sn[8], sn[9], sn[10], sn[11],
468edd16368SStephen M. Cameron 			sn[12], sn[13], sn[14], sn[15]);
469edd16368SStephen M. Cameron }
470edd16368SStephen M. Cameron 
4713f5eac3aSStephen M. Cameron static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
4723f5eac3aSStephen M. Cameron static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
4733f5eac3aSStephen M. Cameron static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
4743f5eac3aSStephen M. Cameron static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
4753f5eac3aSStephen M. Cameron static DEVICE_ATTR(firmware_revision, S_IRUGO,
4763f5eac3aSStephen M. Cameron 	host_show_firmware_revision, NULL);
4773f5eac3aSStephen M. Cameron static DEVICE_ATTR(commands_outstanding, S_IRUGO,
4783f5eac3aSStephen M. Cameron 	host_show_commands_outstanding, NULL);
4793f5eac3aSStephen M. Cameron static DEVICE_ATTR(transport_mode, S_IRUGO,
4803f5eac3aSStephen M. Cameron 	host_show_transport_mode, NULL);
481941b1cdaSStephen M. Cameron static DEVICE_ATTR(resettable, S_IRUGO,
482941b1cdaSStephen M. Cameron 	host_show_resettable, NULL);
4833f5eac3aSStephen M. Cameron 
4843f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_sdev_attrs[] = {
4853f5eac3aSStephen M. Cameron 	&dev_attr_raid_level,
4863f5eac3aSStephen M. Cameron 	&dev_attr_lunid,
4873f5eac3aSStephen M. Cameron 	&dev_attr_unique_id,
4883f5eac3aSStephen M. Cameron 	NULL,
4893f5eac3aSStephen M. Cameron };
4903f5eac3aSStephen M. Cameron 
4913f5eac3aSStephen M. Cameron static struct device_attribute *hpsa_shost_attrs[] = {
4923f5eac3aSStephen M. Cameron 	&dev_attr_rescan,
4933f5eac3aSStephen M. Cameron 	&dev_attr_firmware_revision,
4943f5eac3aSStephen M. Cameron 	&dev_attr_commands_outstanding,
4953f5eac3aSStephen M. Cameron 	&dev_attr_transport_mode,
496941b1cdaSStephen M. Cameron 	&dev_attr_resettable,
4973f5eac3aSStephen M. Cameron 	NULL,
4983f5eac3aSStephen M. Cameron };
4993f5eac3aSStephen M. Cameron 
5003f5eac3aSStephen M. Cameron static struct scsi_host_template hpsa_driver_template = {
5013f5eac3aSStephen M. Cameron 	.module			= THIS_MODULE,
502f79cfec6SStephen M. Cameron 	.name			= HPSA,
503f79cfec6SStephen M. Cameron 	.proc_name		= HPSA,
5043f5eac3aSStephen M. Cameron 	.queuecommand		= hpsa_scsi_queue_command,
5053f5eac3aSStephen M. Cameron 	.scan_start		= hpsa_scan_start,
5063f5eac3aSStephen M. Cameron 	.scan_finished		= hpsa_scan_finished,
5073f5eac3aSStephen M. Cameron 	.change_queue_depth	= hpsa_change_queue_depth,
5083f5eac3aSStephen M. Cameron 	.this_id		= -1,
5093f5eac3aSStephen M. Cameron 	.use_clustering		= ENABLE_CLUSTERING,
5103f5eac3aSStephen M. Cameron 	.eh_device_reset_handler = hpsa_eh_device_reset_handler,
5113f5eac3aSStephen M. Cameron 	.ioctl			= hpsa_ioctl,
5123f5eac3aSStephen M. Cameron 	.slave_alloc		= hpsa_slave_alloc,
5133f5eac3aSStephen M. Cameron 	.slave_destroy		= hpsa_slave_destroy,
5143f5eac3aSStephen M. Cameron #ifdef CONFIG_COMPAT
5153f5eac3aSStephen M. Cameron 	.compat_ioctl		= hpsa_compat_ioctl,
5163f5eac3aSStephen M. Cameron #endif
5173f5eac3aSStephen M. Cameron 	.sdev_attrs = hpsa_sdev_attrs,
5183f5eac3aSStephen M. Cameron 	.shost_attrs = hpsa_shost_attrs,
519c0d6a4d1SStephen M. Cameron 	.max_sectors = 8192,
5203f5eac3aSStephen M. Cameron };
5213f5eac3aSStephen M. Cameron 
5223f5eac3aSStephen M. Cameron 
5233f5eac3aSStephen M. Cameron /* Enqueuing and dequeuing functions for cmdlists. */
5243f5eac3aSStephen M. Cameron static inline void addQ(struct list_head *list, struct CommandList *c)
5253f5eac3aSStephen M. Cameron {
5263f5eac3aSStephen M. Cameron 	list_add_tail(&c->list, list);
5273f5eac3aSStephen M. Cameron }
5283f5eac3aSStephen M. Cameron 
5293f5eac3aSStephen M. Cameron static inline u32 next_command(struct ctlr_info *h)
5303f5eac3aSStephen M. Cameron {
5313f5eac3aSStephen M. Cameron 	u32 a;
5323f5eac3aSStephen M. Cameron 
5333f5eac3aSStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
5343f5eac3aSStephen M. Cameron 		return h->access.command_completed(h);
5353f5eac3aSStephen M. Cameron 
5363f5eac3aSStephen M. Cameron 	if ((*(h->reply_pool_head) & 1) == (h->reply_pool_wraparound)) {
5373f5eac3aSStephen M. Cameron 		a = *(h->reply_pool_head); /* Next cmd in ring buffer */
5383f5eac3aSStephen M. Cameron 		(h->reply_pool_head)++;
5393f5eac3aSStephen M. Cameron 		h->commands_outstanding--;
5403f5eac3aSStephen M. Cameron 	} else {
5413f5eac3aSStephen M. Cameron 		a = FIFO_EMPTY;
5423f5eac3aSStephen M. Cameron 	}
5433f5eac3aSStephen M. Cameron 	/* Check for wraparound */
5443f5eac3aSStephen M. Cameron 	if (h->reply_pool_head == (h->reply_pool + h->max_commands)) {
5453f5eac3aSStephen M. Cameron 		h->reply_pool_head = h->reply_pool;
5463f5eac3aSStephen M. Cameron 		h->reply_pool_wraparound ^= 1;
5473f5eac3aSStephen M. Cameron 	}
5483f5eac3aSStephen M. Cameron 	return a;
5493f5eac3aSStephen M. Cameron }
5503f5eac3aSStephen M. Cameron 
5513f5eac3aSStephen M. Cameron /* set_performant_mode: Modify the tag for cciss performant
5523f5eac3aSStephen M. Cameron  * set bit 0 for pull model, bits 3-1 for block fetch
5533f5eac3aSStephen M. Cameron  * register number
5543f5eac3aSStephen M. Cameron  */
5553f5eac3aSStephen M. Cameron static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
5563f5eac3aSStephen M. Cameron {
5573f5eac3aSStephen M. Cameron 	if (likely(h->transMethod & CFGTBL_Trans_Performant))
5583f5eac3aSStephen M. Cameron 		c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
5593f5eac3aSStephen M. Cameron }
5603f5eac3aSStephen M. Cameron 
5613f5eac3aSStephen M. Cameron static void enqueue_cmd_and_start_io(struct ctlr_info *h,
5623f5eac3aSStephen M. Cameron 	struct CommandList *c)
5633f5eac3aSStephen M. Cameron {
5643f5eac3aSStephen M. Cameron 	unsigned long flags;
5653f5eac3aSStephen M. Cameron 
5663f5eac3aSStephen M. Cameron 	set_performant_mode(h, c);
5673f5eac3aSStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
5683f5eac3aSStephen M. Cameron 	addQ(&h->reqQ, c);
5693f5eac3aSStephen M. Cameron 	h->Qdepth++;
5703f5eac3aSStephen M. Cameron 	start_io(h);
5713f5eac3aSStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
5723f5eac3aSStephen M. Cameron }
5733f5eac3aSStephen M. Cameron 
5743f5eac3aSStephen M. Cameron static inline void removeQ(struct CommandList *c)
5753f5eac3aSStephen M. Cameron {
5763f5eac3aSStephen M. Cameron 	if (WARN_ON(list_empty(&c->list)))
5773f5eac3aSStephen M. Cameron 		return;
5783f5eac3aSStephen M. Cameron 	list_del_init(&c->list);
5793f5eac3aSStephen M. Cameron }
5803f5eac3aSStephen M. Cameron 
5813f5eac3aSStephen M. Cameron static inline int is_hba_lunid(unsigned char scsi3addr[])
5823f5eac3aSStephen M. Cameron {
5833f5eac3aSStephen M. Cameron 	return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
5843f5eac3aSStephen M. Cameron }
5853f5eac3aSStephen M. Cameron 
5863f5eac3aSStephen M. Cameron static inline int is_scsi_rev_5(struct ctlr_info *h)
5873f5eac3aSStephen M. Cameron {
5883f5eac3aSStephen M. Cameron 	if (!h->hba_inquiry_data)
5893f5eac3aSStephen M. Cameron 		return 0;
5903f5eac3aSStephen M. Cameron 	if ((h->hba_inquiry_data[2] & 0x07) == 5)
5913f5eac3aSStephen M. Cameron 		return 1;
5923f5eac3aSStephen M. Cameron 	return 0;
5933f5eac3aSStephen M. Cameron }
5943f5eac3aSStephen M. Cameron 
595edd16368SStephen M. Cameron static int hpsa_find_target_lun(struct ctlr_info *h,
596edd16368SStephen M. Cameron 	unsigned char scsi3addr[], int bus, int *target, int *lun)
597edd16368SStephen M. Cameron {
598edd16368SStephen M. Cameron 	/* finds an unused bus, target, lun for a new physical device
599edd16368SStephen M. Cameron 	 * assumes h->devlock is held
600edd16368SStephen M. Cameron 	 */
601edd16368SStephen M. Cameron 	int i, found = 0;
602cfe5badcSScott Teel 	DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
603edd16368SStephen M. Cameron 
604263d9401SAkinobu Mita 	bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
605edd16368SStephen M. Cameron 
606edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
607edd16368SStephen M. Cameron 		if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
608263d9401SAkinobu Mita 			__set_bit(h->dev[i]->target, lun_taken);
609edd16368SStephen M. Cameron 	}
610edd16368SStephen M. Cameron 
611263d9401SAkinobu Mita 	i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
612263d9401SAkinobu Mita 	if (i < HPSA_MAX_DEVICES) {
613edd16368SStephen M. Cameron 		/* *bus = 1; */
614edd16368SStephen M. Cameron 		*target = i;
615edd16368SStephen M. Cameron 		*lun = 0;
616edd16368SStephen M. Cameron 		found = 1;
617edd16368SStephen M. Cameron 	}
618edd16368SStephen M. Cameron 	return !found;
619edd16368SStephen M. Cameron }
620edd16368SStephen M. Cameron 
621edd16368SStephen M. Cameron /* Add an entry into h->dev[] array. */
622edd16368SStephen M. Cameron static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
623edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *device,
624edd16368SStephen M. Cameron 		struct hpsa_scsi_dev_t *added[], int *nadded)
625edd16368SStephen M. Cameron {
626edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
627edd16368SStephen M. Cameron 	int n = h->ndevices;
628edd16368SStephen M. Cameron 	int i;
629edd16368SStephen M. Cameron 	unsigned char addr1[8], addr2[8];
630edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
631edd16368SStephen M. Cameron 
632cfe5badcSScott Teel 	if (n >= HPSA_MAX_DEVICES) {
633edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "too many devices, some will be "
634edd16368SStephen M. Cameron 			"inaccessible.\n");
635edd16368SStephen M. Cameron 		return -1;
636edd16368SStephen M. Cameron 	}
637edd16368SStephen M. Cameron 
638edd16368SStephen M. Cameron 	/* physical devices do not have lun or target assigned until now. */
639edd16368SStephen M. Cameron 	if (device->lun != -1)
640edd16368SStephen M. Cameron 		/* Logical device, lun is already assigned. */
641edd16368SStephen M. Cameron 		goto lun_assigned;
642edd16368SStephen M. Cameron 
643edd16368SStephen M. Cameron 	/* If this device a non-zero lun of a multi-lun device
644edd16368SStephen M. Cameron 	 * byte 4 of the 8-byte LUN addr will contain the logical
645edd16368SStephen M. Cameron 	 * unit no, zero otherise.
646edd16368SStephen M. Cameron 	 */
647edd16368SStephen M. Cameron 	if (device->scsi3addr[4] == 0) {
648edd16368SStephen M. Cameron 		/* This is not a non-zero lun of a multi-lun device */
649edd16368SStephen M. Cameron 		if (hpsa_find_target_lun(h, device->scsi3addr,
650edd16368SStephen M. Cameron 			device->bus, &device->target, &device->lun) != 0)
651edd16368SStephen M. Cameron 			return -1;
652edd16368SStephen M. Cameron 		goto lun_assigned;
653edd16368SStephen M. Cameron 	}
654edd16368SStephen M. Cameron 
655edd16368SStephen M. Cameron 	/* This is a non-zero lun of a multi-lun device.
656edd16368SStephen M. Cameron 	 * Search through our list and find the device which
657edd16368SStephen M. Cameron 	 * has the same 8 byte LUN address, excepting byte 4.
658edd16368SStephen M. Cameron 	 * Assign the same bus and target for this new LUN.
659edd16368SStephen M. Cameron 	 * Use the logical unit number from the firmware.
660edd16368SStephen M. Cameron 	 */
661edd16368SStephen M. Cameron 	memcpy(addr1, device->scsi3addr, 8);
662edd16368SStephen M. Cameron 	addr1[4] = 0;
663edd16368SStephen M. Cameron 	for (i = 0; i < n; i++) {
664edd16368SStephen M. Cameron 		sd = h->dev[i];
665edd16368SStephen M. Cameron 		memcpy(addr2, sd->scsi3addr, 8);
666edd16368SStephen M. Cameron 		addr2[4] = 0;
667edd16368SStephen M. Cameron 		/* differ only in byte 4? */
668edd16368SStephen M. Cameron 		if (memcmp(addr1, addr2, 8) == 0) {
669edd16368SStephen M. Cameron 			device->bus = sd->bus;
670edd16368SStephen M. Cameron 			device->target = sd->target;
671edd16368SStephen M. Cameron 			device->lun = device->scsi3addr[4];
672edd16368SStephen M. Cameron 			break;
673edd16368SStephen M. Cameron 		}
674edd16368SStephen M. Cameron 	}
675edd16368SStephen M. Cameron 	if (device->lun == -1) {
676edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
677edd16368SStephen M. Cameron 			" suspect firmware bug or unsupported hardware "
678edd16368SStephen M. Cameron 			"configuration.\n");
679edd16368SStephen M. Cameron 			return -1;
680edd16368SStephen M. Cameron 	}
681edd16368SStephen M. Cameron 
682edd16368SStephen M. Cameron lun_assigned:
683edd16368SStephen M. Cameron 
684edd16368SStephen M. Cameron 	h->dev[n] = device;
685edd16368SStephen M. Cameron 	h->ndevices++;
686edd16368SStephen M. Cameron 	added[*nadded] = device;
687edd16368SStephen M. Cameron 	(*nadded)++;
688edd16368SStephen M. Cameron 
689edd16368SStephen M. Cameron 	/* initially, (before registering with scsi layer) we don't
690edd16368SStephen M. Cameron 	 * know our hostno and we don't want to print anything first
691edd16368SStephen M. Cameron 	 * time anyway (the scsi layer's inquiries will show that info)
692edd16368SStephen M. Cameron 	 */
693edd16368SStephen M. Cameron 	/* if (hostno != -1) */
694edd16368SStephen M. Cameron 		dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
695edd16368SStephen M. Cameron 			scsi_device_type(device->devtype), hostno,
696edd16368SStephen M. Cameron 			device->bus, device->target, device->lun);
697edd16368SStephen M. Cameron 	return 0;
698edd16368SStephen M. Cameron }
699edd16368SStephen M. Cameron 
700bd9244f7SScott Teel /* Update an entry in h->dev[] array. */
701bd9244f7SScott Teel static void hpsa_scsi_update_entry(struct ctlr_info *h, int hostno,
702bd9244f7SScott Teel 	int entry, struct hpsa_scsi_dev_t *new_entry)
703bd9244f7SScott Teel {
704bd9244f7SScott Teel 	/* assumes h->devlock is held */
705bd9244f7SScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
706bd9244f7SScott Teel 
707bd9244f7SScott Teel 	/* Raid level changed. */
708bd9244f7SScott Teel 	h->dev[entry]->raid_level = new_entry->raid_level;
709bd9244f7SScott Teel 	dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d updated.\n",
710bd9244f7SScott Teel 		scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
711bd9244f7SScott Teel 		new_entry->target, new_entry->lun);
712bd9244f7SScott Teel }
713bd9244f7SScott Teel 
7142a8ccf31SStephen M. Cameron /* Replace an entry from h->dev[] array. */
7152a8ccf31SStephen M. Cameron static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
7162a8ccf31SStephen M. Cameron 	int entry, struct hpsa_scsi_dev_t *new_entry,
7172a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *added[], int *nadded,
7182a8ccf31SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
7192a8ccf31SStephen M. Cameron {
7202a8ccf31SStephen M. Cameron 	/* assumes h->devlock is held */
721cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
7222a8ccf31SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
7232a8ccf31SStephen M. Cameron 	(*nremoved)++;
72401350d05SStephen M. Cameron 
72501350d05SStephen M. Cameron 	/*
72601350d05SStephen M. Cameron 	 * New physical devices won't have target/lun assigned yet
72701350d05SStephen M. Cameron 	 * so we need to preserve the values in the slot we are replacing.
72801350d05SStephen M. Cameron 	 */
72901350d05SStephen M. Cameron 	if (new_entry->target == -1) {
73001350d05SStephen M. Cameron 		new_entry->target = h->dev[entry]->target;
73101350d05SStephen M. Cameron 		new_entry->lun = h->dev[entry]->lun;
73201350d05SStephen M. Cameron 	}
73301350d05SStephen M. Cameron 
7342a8ccf31SStephen M. Cameron 	h->dev[entry] = new_entry;
7352a8ccf31SStephen M. Cameron 	added[*nadded] = new_entry;
7362a8ccf31SStephen M. Cameron 	(*nadded)++;
7372a8ccf31SStephen M. Cameron 	dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n",
7382a8ccf31SStephen M. Cameron 		scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
7392a8ccf31SStephen M. Cameron 			new_entry->target, new_entry->lun);
7402a8ccf31SStephen M. Cameron }
7412a8ccf31SStephen M. Cameron 
742edd16368SStephen M. Cameron /* Remove an entry from h->dev[] array. */
743edd16368SStephen M. Cameron static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
744edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *removed[], int *nremoved)
745edd16368SStephen M. Cameron {
746edd16368SStephen M. Cameron 	/* assumes h->devlock is held */
747edd16368SStephen M. Cameron 	int i;
748edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
749edd16368SStephen M. Cameron 
750cfe5badcSScott Teel 	BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
751edd16368SStephen M. Cameron 
752edd16368SStephen M. Cameron 	sd = h->dev[entry];
753edd16368SStephen M. Cameron 	removed[*nremoved] = h->dev[entry];
754edd16368SStephen M. Cameron 	(*nremoved)++;
755edd16368SStephen M. Cameron 
756edd16368SStephen M. Cameron 	for (i = entry; i < h->ndevices-1; i++)
757edd16368SStephen M. Cameron 		h->dev[i] = h->dev[i+1];
758edd16368SStephen M. Cameron 	h->ndevices--;
759edd16368SStephen M. Cameron 	dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
760edd16368SStephen M. Cameron 		scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
761edd16368SStephen M. Cameron 		sd->lun);
762edd16368SStephen M. Cameron }
763edd16368SStephen M. Cameron 
764edd16368SStephen M. Cameron #define SCSI3ADDR_EQ(a, b) ( \
765edd16368SStephen M. Cameron 	(a)[7] == (b)[7] && \
766edd16368SStephen M. Cameron 	(a)[6] == (b)[6] && \
767edd16368SStephen M. Cameron 	(a)[5] == (b)[5] && \
768edd16368SStephen M. Cameron 	(a)[4] == (b)[4] && \
769edd16368SStephen M. Cameron 	(a)[3] == (b)[3] && \
770edd16368SStephen M. Cameron 	(a)[2] == (b)[2] && \
771edd16368SStephen M. Cameron 	(a)[1] == (b)[1] && \
772edd16368SStephen M. Cameron 	(a)[0] == (b)[0])
773edd16368SStephen M. Cameron 
774edd16368SStephen M. Cameron static void fixup_botched_add(struct ctlr_info *h,
775edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *added)
776edd16368SStephen M. Cameron {
777edd16368SStephen M. Cameron 	/* called when scsi_add_device fails in order to re-adjust
778edd16368SStephen M. Cameron 	 * h->dev[] to match the mid layer's view.
779edd16368SStephen M. Cameron 	 */
780edd16368SStephen M. Cameron 	unsigned long flags;
781edd16368SStephen M. Cameron 	int i, j;
782edd16368SStephen M. Cameron 
783edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
784edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
785edd16368SStephen M. Cameron 		if (h->dev[i] == added) {
786edd16368SStephen M. Cameron 			for (j = i; j < h->ndevices-1; j++)
787edd16368SStephen M. Cameron 				h->dev[j] = h->dev[j+1];
788edd16368SStephen M. Cameron 			h->ndevices--;
789edd16368SStephen M. Cameron 			break;
790edd16368SStephen M. Cameron 		}
791edd16368SStephen M. Cameron 	}
792edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
793edd16368SStephen M. Cameron 	kfree(added);
794edd16368SStephen M. Cameron }
795edd16368SStephen M. Cameron 
796edd16368SStephen M. Cameron static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
797edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev2)
798edd16368SStephen M. Cameron {
799edd16368SStephen M. Cameron 	/* we compare everything except lun and target as these
800edd16368SStephen M. Cameron 	 * are not yet assigned.  Compare parts likely
801edd16368SStephen M. Cameron 	 * to differ first
802edd16368SStephen M. Cameron 	 */
803edd16368SStephen M. Cameron 	if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
804edd16368SStephen M. Cameron 		sizeof(dev1->scsi3addr)) != 0)
805edd16368SStephen M. Cameron 		return 0;
806edd16368SStephen M. Cameron 	if (memcmp(dev1->device_id, dev2->device_id,
807edd16368SStephen M. Cameron 		sizeof(dev1->device_id)) != 0)
808edd16368SStephen M. Cameron 		return 0;
809edd16368SStephen M. Cameron 	if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
810edd16368SStephen M. Cameron 		return 0;
811edd16368SStephen M. Cameron 	if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
812edd16368SStephen M. Cameron 		return 0;
813edd16368SStephen M. Cameron 	if (dev1->devtype != dev2->devtype)
814edd16368SStephen M. Cameron 		return 0;
815edd16368SStephen M. Cameron 	if (dev1->bus != dev2->bus)
816edd16368SStephen M. Cameron 		return 0;
817edd16368SStephen M. Cameron 	return 1;
818edd16368SStephen M. Cameron }
819edd16368SStephen M. Cameron 
820bd9244f7SScott Teel static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
821bd9244f7SScott Teel 	struct hpsa_scsi_dev_t *dev2)
822bd9244f7SScott Teel {
823bd9244f7SScott Teel 	/* Device attributes that can change, but don't mean
824bd9244f7SScott Teel 	 * that the device is a different device, nor that the OS
825bd9244f7SScott Teel 	 * needs to be told anything about the change.
826bd9244f7SScott Teel 	 */
827bd9244f7SScott Teel 	if (dev1->raid_level != dev2->raid_level)
828bd9244f7SScott Teel 		return 1;
829bd9244f7SScott Teel 	return 0;
830bd9244f7SScott Teel }
831bd9244f7SScott Teel 
832edd16368SStephen M. Cameron /* Find needle in haystack.  If exact match found, return DEVICE_SAME,
833edd16368SStephen M. Cameron  * and return needle location in *index.  If scsi3addr matches, but not
834edd16368SStephen M. Cameron  * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
835bd9244f7SScott Teel  * location in *index.
836bd9244f7SScott Teel  * In the case of a minor device attribute change, such as RAID level, just
837bd9244f7SScott Teel  * return DEVICE_UPDATED, along with the updated device's location in index.
838bd9244f7SScott Teel  * If needle not found, return DEVICE_NOT_FOUND.
839edd16368SStephen M. Cameron  */
840edd16368SStephen M. Cameron static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
841edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *haystack[], int haystack_size,
842edd16368SStephen M. Cameron 	int *index)
843edd16368SStephen M. Cameron {
844edd16368SStephen M. Cameron 	int i;
845edd16368SStephen M. Cameron #define DEVICE_NOT_FOUND 0
846edd16368SStephen M. Cameron #define DEVICE_CHANGED 1
847edd16368SStephen M. Cameron #define DEVICE_SAME 2
848bd9244f7SScott Teel #define DEVICE_UPDATED 3
849edd16368SStephen M. Cameron 	for (i = 0; i < haystack_size; i++) {
85023231048SStephen M. Cameron 		if (haystack[i] == NULL) /* previously removed. */
85123231048SStephen M. Cameron 			continue;
852edd16368SStephen M. Cameron 		if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
853edd16368SStephen M. Cameron 			*index = i;
854bd9244f7SScott Teel 			if (device_is_the_same(needle, haystack[i])) {
855bd9244f7SScott Teel 				if (device_updated(needle, haystack[i]))
856bd9244f7SScott Teel 					return DEVICE_UPDATED;
857edd16368SStephen M. Cameron 				return DEVICE_SAME;
858bd9244f7SScott Teel 			} else {
859edd16368SStephen M. Cameron 				return DEVICE_CHANGED;
860edd16368SStephen M. Cameron 			}
861edd16368SStephen M. Cameron 		}
862bd9244f7SScott Teel 	}
863edd16368SStephen M. Cameron 	*index = -1;
864edd16368SStephen M. Cameron 	return DEVICE_NOT_FOUND;
865edd16368SStephen M. Cameron }
866edd16368SStephen M. Cameron 
8674967bd3eSStephen M. Cameron static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
868edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd[], int nsds)
869edd16368SStephen M. Cameron {
870edd16368SStephen M. Cameron 	/* sd contains scsi3 addresses and devtypes, and inquiry
871edd16368SStephen M. Cameron 	 * data.  This function takes what's in sd to be the current
872edd16368SStephen M. Cameron 	 * reality and updates h->dev[] to reflect that reality.
873edd16368SStephen M. Cameron 	 */
874edd16368SStephen M. Cameron 	int i, entry, device_change, changes = 0;
875edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *csd;
876edd16368SStephen M. Cameron 	unsigned long flags;
877edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **added, **removed;
878edd16368SStephen M. Cameron 	int nadded, nremoved;
879edd16368SStephen M. Cameron 	struct Scsi_Host *sh = NULL;
880edd16368SStephen M. Cameron 
881cfe5badcSScott Teel 	added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
882cfe5badcSScott Teel 	removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
883edd16368SStephen M. Cameron 
884edd16368SStephen M. Cameron 	if (!added || !removed) {
885edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in "
886edd16368SStephen M. Cameron 			"adjust_hpsa_scsi_table\n");
887edd16368SStephen M. Cameron 		goto free_and_out;
888edd16368SStephen M. Cameron 	}
889edd16368SStephen M. Cameron 
890edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
891edd16368SStephen M. Cameron 
892edd16368SStephen M. Cameron 	/* find any devices in h->dev[] that are not in
893edd16368SStephen M. Cameron 	 * sd[] and remove them from h->dev[], and for any
894edd16368SStephen M. Cameron 	 * devices which have changed, remove the old device
895edd16368SStephen M. Cameron 	 * info and add the new device info.
896bd9244f7SScott Teel 	 * If minor device attributes change, just update
897bd9244f7SScott Teel 	 * the existing device structure.
898edd16368SStephen M. Cameron 	 */
899edd16368SStephen M. Cameron 	i = 0;
900edd16368SStephen M. Cameron 	nremoved = 0;
901edd16368SStephen M. Cameron 	nadded = 0;
902edd16368SStephen M. Cameron 	while (i < h->ndevices) {
903edd16368SStephen M. Cameron 		csd = h->dev[i];
904edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
905edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
906edd16368SStephen M. Cameron 			changes++;
907edd16368SStephen M. Cameron 			hpsa_scsi_remove_entry(h, hostno, i,
908edd16368SStephen M. Cameron 				removed, &nremoved);
909edd16368SStephen M. Cameron 			continue; /* remove ^^^, hence i not incremented */
910edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
911edd16368SStephen M. Cameron 			changes++;
9122a8ccf31SStephen M. Cameron 			hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
9132a8ccf31SStephen M. Cameron 				added, &nadded, removed, &nremoved);
914c7f172dcSStephen M. Cameron 			/* Set it to NULL to prevent it from being freed
915c7f172dcSStephen M. Cameron 			 * at the bottom of hpsa_update_scsi_devices()
916c7f172dcSStephen M. Cameron 			 */
917c7f172dcSStephen M. Cameron 			sd[entry] = NULL;
918bd9244f7SScott Teel 		} else if (device_change == DEVICE_UPDATED) {
919bd9244f7SScott Teel 			hpsa_scsi_update_entry(h, hostno, i, sd[entry]);
920edd16368SStephen M. Cameron 		}
921edd16368SStephen M. Cameron 		i++;
922edd16368SStephen M. Cameron 	}
923edd16368SStephen M. Cameron 
924edd16368SStephen M. Cameron 	/* Now, make sure every device listed in sd[] is also
925edd16368SStephen M. Cameron 	 * listed in h->dev[], adding them if they aren't found
926edd16368SStephen M. Cameron 	 */
927edd16368SStephen M. Cameron 
928edd16368SStephen M. Cameron 	for (i = 0; i < nsds; i++) {
929edd16368SStephen M. Cameron 		if (!sd[i]) /* if already added above. */
930edd16368SStephen M. Cameron 			continue;
931edd16368SStephen M. Cameron 		device_change = hpsa_scsi_find_entry(sd[i], h->dev,
932edd16368SStephen M. Cameron 					h->ndevices, &entry);
933edd16368SStephen M. Cameron 		if (device_change == DEVICE_NOT_FOUND) {
934edd16368SStephen M. Cameron 			changes++;
935edd16368SStephen M. Cameron 			if (hpsa_scsi_add_entry(h, hostno, sd[i],
936edd16368SStephen M. Cameron 				added, &nadded) != 0)
937edd16368SStephen M. Cameron 				break;
938edd16368SStephen M. Cameron 			sd[i] = NULL; /* prevent from being freed later. */
939edd16368SStephen M. Cameron 		} else if (device_change == DEVICE_CHANGED) {
940edd16368SStephen M. Cameron 			/* should never happen... */
941edd16368SStephen M. Cameron 			changes++;
942edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev,
943edd16368SStephen M. Cameron 				"device unexpectedly changed.\n");
944edd16368SStephen M. Cameron 			/* but if it does happen, we just ignore that device */
945edd16368SStephen M. Cameron 		}
946edd16368SStephen M. Cameron 	}
947edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
948edd16368SStephen M. Cameron 
949edd16368SStephen M. Cameron 	/* Don't notify scsi mid layer of any changes the first time through
950edd16368SStephen M. Cameron 	 * (or if there are no changes) scsi_scan_host will do it later the
951edd16368SStephen M. Cameron 	 * first time through.
952edd16368SStephen M. Cameron 	 */
953edd16368SStephen M. Cameron 	if (hostno == -1 || !changes)
954edd16368SStephen M. Cameron 		goto free_and_out;
955edd16368SStephen M. Cameron 
956edd16368SStephen M. Cameron 	sh = h->scsi_host;
957edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any removed devices */
958edd16368SStephen M. Cameron 	for (i = 0; i < nremoved; i++) {
959edd16368SStephen M. Cameron 		struct scsi_device *sdev =
960edd16368SStephen M. Cameron 			scsi_device_lookup(sh, removed[i]->bus,
961edd16368SStephen M. Cameron 				removed[i]->target, removed[i]->lun);
962edd16368SStephen M. Cameron 		if (sdev != NULL) {
963edd16368SStephen M. Cameron 			scsi_remove_device(sdev);
964edd16368SStephen M. Cameron 			scsi_device_put(sdev);
965edd16368SStephen M. Cameron 		} else {
966edd16368SStephen M. Cameron 			/* We don't expect to get here.
967edd16368SStephen M. Cameron 			 * future cmds to this device will get selection
968edd16368SStephen M. Cameron 			 * timeout as if the device was gone.
969edd16368SStephen M. Cameron 			 */
970edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
971edd16368SStephen M. Cameron 				" for removal.", hostno, removed[i]->bus,
972edd16368SStephen M. Cameron 				removed[i]->target, removed[i]->lun);
973edd16368SStephen M. Cameron 		}
974edd16368SStephen M. Cameron 		kfree(removed[i]);
975edd16368SStephen M. Cameron 		removed[i] = NULL;
976edd16368SStephen M. Cameron 	}
977edd16368SStephen M. Cameron 
978edd16368SStephen M. Cameron 	/* Notify scsi mid layer of any added devices */
979edd16368SStephen M. Cameron 	for (i = 0; i < nadded; i++) {
980edd16368SStephen M. Cameron 		if (scsi_add_device(sh, added[i]->bus,
981edd16368SStephen M. Cameron 			added[i]->target, added[i]->lun) == 0)
982edd16368SStephen M. Cameron 			continue;
983edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
984edd16368SStephen M. Cameron 			"device not added.\n", hostno, added[i]->bus,
985edd16368SStephen M. Cameron 			added[i]->target, added[i]->lun);
986edd16368SStephen M. Cameron 		/* now we have to remove it from h->dev,
987edd16368SStephen M. Cameron 		 * since it didn't get added to scsi mid layer
988edd16368SStephen M. Cameron 		 */
989edd16368SStephen M. Cameron 		fixup_botched_add(h, added[i]);
990edd16368SStephen M. Cameron 	}
991edd16368SStephen M. Cameron 
992edd16368SStephen M. Cameron free_and_out:
993edd16368SStephen M. Cameron 	kfree(added);
994edd16368SStephen M. Cameron 	kfree(removed);
995edd16368SStephen M. Cameron }
996edd16368SStephen M. Cameron 
997edd16368SStephen M. Cameron /*
998edd16368SStephen M. Cameron  * Lookup bus/target/lun and retrun corresponding struct hpsa_scsi_dev_t *
999edd16368SStephen M. Cameron  * Assume's h->devlock is held.
1000edd16368SStephen M. Cameron  */
1001edd16368SStephen M. Cameron static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1002edd16368SStephen M. Cameron 	int bus, int target, int lun)
1003edd16368SStephen M. Cameron {
1004edd16368SStephen M. Cameron 	int i;
1005edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1006edd16368SStephen M. Cameron 
1007edd16368SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++) {
1008edd16368SStephen M. Cameron 		sd = h->dev[i];
1009edd16368SStephen M. Cameron 		if (sd->bus == bus && sd->target == target && sd->lun == lun)
1010edd16368SStephen M. Cameron 			return sd;
1011edd16368SStephen M. Cameron 	}
1012edd16368SStephen M. Cameron 	return NULL;
1013edd16368SStephen M. Cameron }
1014edd16368SStephen M. Cameron 
1015edd16368SStephen M. Cameron /* link sdev->hostdata to our per-device structure. */
1016edd16368SStephen M. Cameron static int hpsa_slave_alloc(struct scsi_device *sdev)
1017edd16368SStephen M. Cameron {
1018edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *sd;
1019edd16368SStephen M. Cameron 	unsigned long flags;
1020edd16368SStephen M. Cameron 	struct ctlr_info *h;
1021edd16368SStephen M. Cameron 
1022edd16368SStephen M. Cameron 	h = sdev_to_hba(sdev);
1023edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->devlock, flags);
1024edd16368SStephen M. Cameron 	sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1025edd16368SStephen M. Cameron 		sdev_id(sdev), sdev->lun);
1026edd16368SStephen M. Cameron 	if (sd != NULL)
1027edd16368SStephen M. Cameron 		sdev->hostdata = sd;
1028edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->devlock, flags);
1029edd16368SStephen M. Cameron 	return 0;
1030edd16368SStephen M. Cameron }
1031edd16368SStephen M. Cameron 
1032edd16368SStephen M. Cameron static void hpsa_slave_destroy(struct scsi_device *sdev)
1033edd16368SStephen M. Cameron {
1034bcc44255SStephen M. Cameron 	/* nothing to do. */
1035edd16368SStephen M. Cameron }
1036edd16368SStephen M. Cameron 
103733a2ffceSStephen M. Cameron static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
103833a2ffceSStephen M. Cameron {
103933a2ffceSStephen M. Cameron 	int i;
104033a2ffceSStephen M. Cameron 
104133a2ffceSStephen M. Cameron 	if (!h->cmd_sg_list)
104233a2ffceSStephen M. Cameron 		return;
104333a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
104433a2ffceSStephen M. Cameron 		kfree(h->cmd_sg_list[i]);
104533a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = NULL;
104633a2ffceSStephen M. Cameron 	}
104733a2ffceSStephen M. Cameron 	kfree(h->cmd_sg_list);
104833a2ffceSStephen M. Cameron 	h->cmd_sg_list = NULL;
104933a2ffceSStephen M. Cameron }
105033a2ffceSStephen M. Cameron 
105133a2ffceSStephen M. Cameron static int hpsa_allocate_sg_chain_blocks(struct ctlr_info *h)
105233a2ffceSStephen M. Cameron {
105333a2ffceSStephen M. Cameron 	int i;
105433a2ffceSStephen M. Cameron 
105533a2ffceSStephen M. Cameron 	if (h->chainsize <= 0)
105633a2ffceSStephen M. Cameron 		return 0;
105733a2ffceSStephen M. Cameron 
105833a2ffceSStephen M. Cameron 	h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
105933a2ffceSStephen M. Cameron 				GFP_KERNEL);
106033a2ffceSStephen M. Cameron 	if (!h->cmd_sg_list)
106133a2ffceSStephen M. Cameron 		return -ENOMEM;
106233a2ffceSStephen M. Cameron 	for (i = 0; i < h->nr_cmds; i++) {
106333a2ffceSStephen M. Cameron 		h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
106433a2ffceSStephen M. Cameron 						h->chainsize, GFP_KERNEL);
106533a2ffceSStephen M. Cameron 		if (!h->cmd_sg_list[i])
106633a2ffceSStephen M. Cameron 			goto clean;
106733a2ffceSStephen M. Cameron 	}
106833a2ffceSStephen M. Cameron 	return 0;
106933a2ffceSStephen M. Cameron 
107033a2ffceSStephen M. Cameron clean:
107133a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
107233a2ffceSStephen M. Cameron 	return -ENOMEM;
107333a2ffceSStephen M. Cameron }
107433a2ffceSStephen M. Cameron 
107533a2ffceSStephen M. Cameron static void hpsa_map_sg_chain_block(struct ctlr_info *h,
107633a2ffceSStephen M. Cameron 	struct CommandList *c)
107733a2ffceSStephen M. Cameron {
107833a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg, *chain_block;
107933a2ffceSStephen M. Cameron 	u64 temp64;
108033a2ffceSStephen M. Cameron 
108133a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
108233a2ffceSStephen M. Cameron 	chain_block = h->cmd_sg_list[c->cmdindex];
108333a2ffceSStephen M. Cameron 	chain_sg->Ext = HPSA_SG_CHAIN;
108433a2ffceSStephen M. Cameron 	chain_sg->Len = sizeof(*chain_sg) *
108533a2ffceSStephen M. Cameron 		(c->Header.SGTotal - h->max_cmd_sg_entries);
108633a2ffceSStephen M. Cameron 	temp64 = pci_map_single(h->pdev, chain_block, chain_sg->Len,
108733a2ffceSStephen M. Cameron 				PCI_DMA_TODEVICE);
108833a2ffceSStephen M. Cameron 	chain_sg->Addr.lower = (u32) (temp64 & 0x0FFFFFFFFULL);
108933a2ffceSStephen M. Cameron 	chain_sg->Addr.upper = (u32) ((temp64 >> 32) & 0x0FFFFFFFFULL);
109033a2ffceSStephen M. Cameron }
109133a2ffceSStephen M. Cameron 
109233a2ffceSStephen M. Cameron static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
109333a2ffceSStephen M. Cameron 	struct CommandList *c)
109433a2ffceSStephen M. Cameron {
109533a2ffceSStephen M. Cameron 	struct SGDescriptor *chain_sg;
109633a2ffceSStephen M. Cameron 	union u64bit temp64;
109733a2ffceSStephen M. Cameron 
109833a2ffceSStephen M. Cameron 	if (c->Header.SGTotal <= h->max_cmd_sg_entries)
109933a2ffceSStephen M. Cameron 		return;
110033a2ffceSStephen M. Cameron 
110133a2ffceSStephen M. Cameron 	chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
110233a2ffceSStephen M. Cameron 	temp64.val32.lower = chain_sg->Addr.lower;
110333a2ffceSStephen M. Cameron 	temp64.val32.upper = chain_sg->Addr.upper;
110433a2ffceSStephen M. Cameron 	pci_unmap_single(h->pdev, temp64.val, chain_sg->Len, PCI_DMA_TODEVICE);
110533a2ffceSStephen M. Cameron }
110633a2ffceSStephen M. Cameron 
11071fb011fbSStephen M. Cameron static void complete_scsi_command(struct CommandList *cp)
1108edd16368SStephen M. Cameron {
1109edd16368SStephen M. Cameron 	struct scsi_cmnd *cmd;
1110edd16368SStephen M. Cameron 	struct ctlr_info *h;
1111edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
1112edd16368SStephen M. Cameron 
1113edd16368SStephen M. Cameron 	unsigned char sense_key;
1114edd16368SStephen M. Cameron 	unsigned char asc;      /* additional sense code */
1115edd16368SStephen M. Cameron 	unsigned char ascq;     /* additional sense code qualifier */
1116db111e18SStephen M. Cameron 	unsigned long sense_data_size;
1117edd16368SStephen M. Cameron 
1118edd16368SStephen M. Cameron 	ei = cp->err_info;
1119edd16368SStephen M. Cameron 	cmd = (struct scsi_cmnd *) cp->scsi_cmd;
1120edd16368SStephen M. Cameron 	h = cp->h;
1121edd16368SStephen M. Cameron 
1122edd16368SStephen M. Cameron 	scsi_dma_unmap(cmd); /* undo the DMA mappings */
112333a2ffceSStephen M. Cameron 	if (cp->Header.SGTotal > h->max_cmd_sg_entries)
112433a2ffceSStephen M. Cameron 		hpsa_unmap_sg_chain_block(h, cp);
1125edd16368SStephen M. Cameron 
1126edd16368SStephen M. Cameron 	cmd->result = (DID_OK << 16); 		/* host byte */
1127edd16368SStephen M. Cameron 	cmd->result |= (COMMAND_COMPLETE << 8);	/* msg byte */
11285512672fSStephen M. Cameron 	cmd->result |= ei->ScsiStatus;
1129edd16368SStephen M. Cameron 
1130edd16368SStephen M. Cameron 	/* copy the sense data whether we need to or not. */
1131db111e18SStephen M. Cameron 	if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
1132db111e18SStephen M. Cameron 		sense_data_size = SCSI_SENSE_BUFFERSIZE;
1133db111e18SStephen M. Cameron 	else
1134db111e18SStephen M. Cameron 		sense_data_size = sizeof(ei->SenseInfo);
1135db111e18SStephen M. Cameron 	if (ei->SenseLen < sense_data_size)
1136db111e18SStephen M. Cameron 		sense_data_size = ei->SenseLen;
1137db111e18SStephen M. Cameron 
1138db111e18SStephen M. Cameron 	memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
1139edd16368SStephen M. Cameron 	scsi_set_resid(cmd, ei->ResidualCnt);
1140edd16368SStephen M. Cameron 
1141edd16368SStephen M. Cameron 	if (ei->CommandStatus == 0) {
1142edd16368SStephen M. Cameron 		cmd->scsi_done(cmd);
1143edd16368SStephen M. Cameron 		cmd_free(h, cp);
1144edd16368SStephen M. Cameron 		return;
1145edd16368SStephen M. Cameron 	}
1146edd16368SStephen M. Cameron 
1147edd16368SStephen M. Cameron 	/* an error has occurred */
1148edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
1149edd16368SStephen M. Cameron 
1150edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
1151edd16368SStephen M. Cameron 		if (ei->ScsiStatus) {
1152edd16368SStephen M. Cameron 			/* Get sense key */
1153edd16368SStephen M. Cameron 			sense_key = 0xf & ei->SenseInfo[2];
1154edd16368SStephen M. Cameron 			/* Get additional sense code */
1155edd16368SStephen M. Cameron 			asc = ei->SenseInfo[12];
1156edd16368SStephen M. Cameron 			/* Get addition sense code qualifier */
1157edd16368SStephen M. Cameron 			ascq = ei->SenseInfo[13];
1158edd16368SStephen M. Cameron 		}
1159edd16368SStephen M. Cameron 
1160edd16368SStephen M. Cameron 		if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
1161edd16368SStephen M. Cameron 			if (check_for_unit_attention(h, cp)) {
1162edd16368SStephen M. Cameron 				cmd->result = DID_SOFT_ERROR << 16;
1163edd16368SStephen M. Cameron 				break;
1164edd16368SStephen M. Cameron 			}
1165edd16368SStephen M. Cameron 			if (sense_key == ILLEGAL_REQUEST) {
1166edd16368SStephen M. Cameron 				/*
1167edd16368SStephen M. Cameron 				 * SCSI REPORT_LUNS is commonly unsupported on
1168edd16368SStephen M. Cameron 				 * Smart Array.  Suppress noisy complaint.
1169edd16368SStephen M. Cameron 				 */
1170edd16368SStephen M. Cameron 				if (cp->Request.CDB[0] == REPORT_LUNS)
1171edd16368SStephen M. Cameron 					break;
1172edd16368SStephen M. Cameron 
1173edd16368SStephen M. Cameron 				/* If ASC/ASCQ indicate Logical Unit
1174edd16368SStephen M. Cameron 				 * Not Supported condition,
1175edd16368SStephen M. Cameron 				 */
1176edd16368SStephen M. Cameron 				if ((asc == 0x25) && (ascq == 0x0)) {
1177edd16368SStephen M. Cameron 					dev_warn(&h->pdev->dev, "cp %p "
1178edd16368SStephen M. Cameron 						"has check condition\n", cp);
1179edd16368SStephen M. Cameron 					break;
1180edd16368SStephen M. Cameron 				}
1181edd16368SStephen M. Cameron 			}
1182edd16368SStephen M. Cameron 
1183edd16368SStephen M. Cameron 			if (sense_key == NOT_READY) {
1184edd16368SStephen M. Cameron 				/* If Sense is Not Ready, Logical Unit
1185edd16368SStephen M. Cameron 				 * Not ready, Manual Intervention
1186edd16368SStephen M. Cameron 				 * required
1187edd16368SStephen M. Cameron 				 */
1188edd16368SStephen M. Cameron 				if ((asc == 0x04) && (ascq == 0x03)) {
1189edd16368SStephen M. Cameron 					dev_warn(&h->pdev->dev, "cp %p "
1190edd16368SStephen M. Cameron 						"has check condition: unit "
1191edd16368SStephen M. Cameron 						"not ready, manual "
1192edd16368SStephen M. Cameron 						"intervention required\n", cp);
1193edd16368SStephen M. Cameron 					break;
1194edd16368SStephen M. Cameron 				}
1195edd16368SStephen M. Cameron 			}
11961d3b3609SMatt Gates 			if (sense_key == ABORTED_COMMAND) {
11971d3b3609SMatt Gates 				/* Aborted command is retryable */
11981d3b3609SMatt Gates 				dev_warn(&h->pdev->dev, "cp %p "
11991d3b3609SMatt Gates 					"has check condition: aborted command: "
12001d3b3609SMatt Gates 					"ASC: 0x%x, ASCQ: 0x%x\n",
12011d3b3609SMatt Gates 					cp, asc, ascq);
12021d3b3609SMatt Gates 				cmd->result = DID_SOFT_ERROR << 16;
12031d3b3609SMatt Gates 				break;
12041d3b3609SMatt Gates 			}
1205edd16368SStephen M. Cameron 			/* Must be some other type of check condition */
120621b8e4efSStephen M. Cameron 			dev_dbg(&h->pdev->dev, "cp %p has check condition: "
1207edd16368SStephen M. Cameron 					"unknown type: "
1208edd16368SStephen M. Cameron 					"Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1209edd16368SStephen M. Cameron 					"Returning result: 0x%x, "
1210edd16368SStephen M. Cameron 					"cmd=[%02x %02x %02x %02x %02x "
1211807be732SMike Miller 					"%02x %02x %02x %02x %02x %02x "
1212edd16368SStephen M. Cameron 					"%02x %02x %02x %02x %02x]\n",
1213edd16368SStephen M. Cameron 					cp, sense_key, asc, ascq,
1214edd16368SStephen M. Cameron 					cmd->result,
1215edd16368SStephen M. Cameron 					cmd->cmnd[0], cmd->cmnd[1],
1216edd16368SStephen M. Cameron 					cmd->cmnd[2], cmd->cmnd[3],
1217edd16368SStephen M. Cameron 					cmd->cmnd[4], cmd->cmnd[5],
1218edd16368SStephen M. Cameron 					cmd->cmnd[6], cmd->cmnd[7],
1219807be732SMike Miller 					cmd->cmnd[8], cmd->cmnd[9],
1220807be732SMike Miller 					cmd->cmnd[10], cmd->cmnd[11],
1221807be732SMike Miller 					cmd->cmnd[12], cmd->cmnd[13],
1222807be732SMike Miller 					cmd->cmnd[14], cmd->cmnd[15]);
1223edd16368SStephen M. Cameron 			break;
1224edd16368SStephen M. Cameron 		}
1225edd16368SStephen M. Cameron 
1226edd16368SStephen M. Cameron 
1227edd16368SStephen M. Cameron 		/* Problem was not a check condition
1228edd16368SStephen M. Cameron 		 * Pass it up to the upper layers...
1229edd16368SStephen M. Cameron 		 */
1230edd16368SStephen M. Cameron 		if (ei->ScsiStatus) {
1231edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
1232edd16368SStephen M. Cameron 				"Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1233edd16368SStephen M. Cameron 				"Returning result: 0x%x\n",
1234edd16368SStephen M. Cameron 				cp, ei->ScsiStatus,
1235edd16368SStephen M. Cameron 				sense_key, asc, ascq,
1236edd16368SStephen M. Cameron 				cmd->result);
1237edd16368SStephen M. Cameron 		} else {  /* scsi status is zero??? How??? */
1238edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
1239edd16368SStephen M. Cameron 				"Returning no connection.\n", cp),
1240edd16368SStephen M. Cameron 
1241edd16368SStephen M. Cameron 			/* Ordinarily, this case should never happen,
1242edd16368SStephen M. Cameron 			 * but there is a bug in some released firmware
1243edd16368SStephen M. Cameron 			 * revisions that allows it to happen if, for
1244edd16368SStephen M. Cameron 			 * example, a 4100 backplane loses power and
1245edd16368SStephen M. Cameron 			 * the tape drive is in it.  We assume that
1246edd16368SStephen M. Cameron 			 * it's a fatal error of some kind because we
1247edd16368SStephen M. Cameron 			 * can't show that it wasn't. We will make it
1248edd16368SStephen M. Cameron 			 * look like selection timeout since that is
1249edd16368SStephen M. Cameron 			 * the most common reason for this to occur,
1250edd16368SStephen M. Cameron 			 * and it's severe enough.
1251edd16368SStephen M. Cameron 			 */
1252edd16368SStephen M. Cameron 
1253edd16368SStephen M. Cameron 			cmd->result = DID_NO_CONNECT << 16;
1254edd16368SStephen M. Cameron 		}
1255edd16368SStephen M. Cameron 		break;
1256edd16368SStephen M. Cameron 
1257edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1258edd16368SStephen M. Cameron 		break;
1259edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
1260edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p has"
1261edd16368SStephen M. Cameron 			" completed with data overrun "
1262edd16368SStephen M. Cameron 			"reported\n", cp);
1263edd16368SStephen M. Cameron 		break;
1264edd16368SStephen M. Cameron 	case CMD_INVALID: {
1265edd16368SStephen M. Cameron 		/* print_bytes(cp, sizeof(*cp), 1, 0);
1266edd16368SStephen M. Cameron 		print_cmd(cp); */
1267edd16368SStephen M. Cameron 		/* We get CMD_INVALID if you address a non-existent device
1268edd16368SStephen M. Cameron 		 * instead of a selection timeout (no response).  You will
1269edd16368SStephen M. Cameron 		 * see this if you yank out a drive, then try to access it.
1270edd16368SStephen M. Cameron 		 * This is kind of a shame because it means that any other
1271edd16368SStephen M. Cameron 		 * CMD_INVALID (e.g. driver bug) will get interpreted as a
1272edd16368SStephen M. Cameron 		 * missing target. */
1273edd16368SStephen M. Cameron 		cmd->result = DID_NO_CONNECT << 16;
1274edd16368SStephen M. Cameron 	}
1275edd16368SStephen M. Cameron 		break;
1276edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
1277edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p has "
1278edd16368SStephen M. Cameron 			"protocol error \n", cp);
1279edd16368SStephen M. Cameron 		break;
1280edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
1281edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1282edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p had  hardware error\n", cp);
1283edd16368SStephen M. Cameron 		break;
1284edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
1285edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1286edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
1287edd16368SStephen M. Cameron 		break;
1288edd16368SStephen M. Cameron 	case CMD_ABORTED:
1289edd16368SStephen M. Cameron 		cmd->result = DID_ABORT << 16;
1290edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
1291edd16368SStephen M. Cameron 				cp, ei->ScsiStatus);
1292edd16368SStephen M. Cameron 		break;
1293edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
1294edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1295edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
1296edd16368SStephen M. Cameron 		break;
1297edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
1298f6e76055SStephen M. Cameron 		cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
1299f6e76055SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p aborted due to an unsolicited "
1300edd16368SStephen M. Cameron 			"abort\n", cp);
1301edd16368SStephen M. Cameron 		break;
1302edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
1303edd16368SStephen M. Cameron 		cmd->result = DID_TIME_OUT << 16;
1304edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
1305edd16368SStephen M. Cameron 		break;
13061d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
13071d5e2ed0SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
13081d5e2ed0SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Command unabortable\n");
13091d5e2ed0SStephen M. Cameron 		break;
1310edd16368SStephen M. Cameron 	default:
1311edd16368SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
1312edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
1313edd16368SStephen M. Cameron 				cp, ei->CommandStatus);
1314edd16368SStephen M. Cameron 	}
1315edd16368SStephen M. Cameron 	cmd->scsi_done(cmd);
1316edd16368SStephen M. Cameron 	cmd_free(h, cp);
1317edd16368SStephen M. Cameron }
1318edd16368SStephen M. Cameron 
1319edd16368SStephen M. Cameron static void hpsa_pci_unmap(struct pci_dev *pdev,
1320edd16368SStephen M. Cameron 	struct CommandList *c, int sg_used, int data_direction)
1321edd16368SStephen M. Cameron {
1322edd16368SStephen M. Cameron 	int i;
1323edd16368SStephen M. Cameron 	union u64bit addr64;
1324edd16368SStephen M. Cameron 
1325edd16368SStephen M. Cameron 	for (i = 0; i < sg_used; i++) {
1326edd16368SStephen M. Cameron 		addr64.val32.lower = c->SG[i].Addr.lower;
1327edd16368SStephen M. Cameron 		addr64.val32.upper = c->SG[i].Addr.upper;
1328edd16368SStephen M. Cameron 		pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len,
1329edd16368SStephen M. Cameron 			data_direction);
1330edd16368SStephen M. Cameron 	}
1331edd16368SStephen M. Cameron }
1332edd16368SStephen M. Cameron 
1333edd16368SStephen M. Cameron static void hpsa_map_one(struct pci_dev *pdev,
1334edd16368SStephen M. Cameron 		struct CommandList *cp,
1335edd16368SStephen M. Cameron 		unsigned char *buf,
1336edd16368SStephen M. Cameron 		size_t buflen,
1337edd16368SStephen M. Cameron 		int data_direction)
1338edd16368SStephen M. Cameron {
133901a02ffcSStephen M. Cameron 	u64 addr64;
1340edd16368SStephen M. Cameron 
1341edd16368SStephen M. Cameron 	if (buflen == 0 || data_direction == PCI_DMA_NONE) {
1342edd16368SStephen M. Cameron 		cp->Header.SGList = 0;
1343edd16368SStephen M. Cameron 		cp->Header.SGTotal = 0;
1344edd16368SStephen M. Cameron 		return;
1345edd16368SStephen M. Cameron 	}
1346edd16368SStephen M. Cameron 
134701a02ffcSStephen M. Cameron 	addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction);
1348edd16368SStephen M. Cameron 	cp->SG[0].Addr.lower =
134901a02ffcSStephen M. Cameron 	  (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
1350edd16368SStephen M. Cameron 	cp->SG[0].Addr.upper =
135101a02ffcSStephen M. Cameron 	  (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
1352edd16368SStephen M. Cameron 	cp->SG[0].Len = buflen;
135301a02ffcSStephen M. Cameron 	cp->Header.SGList = (u8) 1;   /* no. SGs contig in this cmd */
135401a02ffcSStephen M. Cameron 	cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */
1355edd16368SStephen M. Cameron }
1356edd16368SStephen M. Cameron 
1357edd16368SStephen M. Cameron static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
1358edd16368SStephen M. Cameron 	struct CommandList *c)
1359edd16368SStephen M. Cameron {
1360edd16368SStephen M. Cameron 	DECLARE_COMPLETION_ONSTACK(wait);
1361edd16368SStephen M. Cameron 
1362edd16368SStephen M. Cameron 	c->waiting = &wait;
1363edd16368SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
1364edd16368SStephen M. Cameron 	wait_for_completion(&wait);
1365edd16368SStephen M. Cameron }
1366edd16368SStephen M. Cameron 
1367a0c12413SStephen M. Cameron static void hpsa_scsi_do_simple_cmd_core_if_no_lockup(struct ctlr_info *h,
1368a0c12413SStephen M. Cameron 	struct CommandList *c)
1369a0c12413SStephen M. Cameron {
1370a0c12413SStephen M. Cameron 	unsigned long flags;
1371a0c12413SStephen M. Cameron 
1372a0c12413SStephen M. Cameron 	/* If controller lockup detected, fake a hardware error. */
1373a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
1374a0c12413SStephen M. Cameron 	if (unlikely(h->lockup_detected)) {
1375a0c12413SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
1376a0c12413SStephen M. Cameron 		c->err_info->CommandStatus = CMD_HARDWARE_ERR;
1377a0c12413SStephen M. Cameron 	} else {
1378a0c12413SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
1379a0c12413SStephen M. Cameron 		hpsa_scsi_do_simple_cmd_core(h, c);
1380a0c12413SStephen M. Cameron 	}
1381a0c12413SStephen M. Cameron }
1382a0c12413SStephen M. Cameron 
1383*9c2fc160SStephen M. Cameron #define MAX_DRIVER_CMD_RETRIES 25
1384edd16368SStephen M. Cameron static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
1385edd16368SStephen M. Cameron 	struct CommandList *c, int data_direction)
1386edd16368SStephen M. Cameron {
1387*9c2fc160SStephen M. Cameron 	int backoff_time = 10, retry_count = 0;
1388edd16368SStephen M. Cameron 
1389edd16368SStephen M. Cameron 	do {
13907630abd0SJoe Perches 		memset(c->err_info, 0, sizeof(*c->err_info));
1391edd16368SStephen M. Cameron 		hpsa_scsi_do_simple_cmd_core(h, c);
1392edd16368SStephen M. Cameron 		retry_count++;
1393*9c2fc160SStephen M. Cameron 		if (retry_count > 3) {
1394*9c2fc160SStephen M. Cameron 			msleep(backoff_time);
1395*9c2fc160SStephen M. Cameron 			if (backoff_time < 1000)
1396*9c2fc160SStephen M. Cameron 				backoff_time *= 2;
1397*9c2fc160SStephen M. Cameron 		}
1398852af20aSMatt Bondurant 	} while ((check_for_unit_attention(h, c) ||
1399*9c2fc160SStephen M. Cameron 			check_for_busy(h, c)) &&
1400*9c2fc160SStephen M. Cameron 			retry_count <= MAX_DRIVER_CMD_RETRIES);
1401edd16368SStephen M. Cameron 	hpsa_pci_unmap(h->pdev, c, 1, data_direction);
1402edd16368SStephen M. Cameron }
1403edd16368SStephen M. Cameron 
1404edd16368SStephen M. Cameron static void hpsa_scsi_interpret_error(struct CommandList *cp)
1405edd16368SStephen M. Cameron {
1406edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
1407edd16368SStephen M. Cameron 	struct device *d = &cp->h->pdev->dev;
1408edd16368SStephen M. Cameron 
1409edd16368SStephen M. Cameron 	ei = cp->err_info;
1410edd16368SStephen M. Cameron 	switch (ei->CommandStatus) {
1411edd16368SStephen M. Cameron 	case CMD_TARGET_STATUS:
1412edd16368SStephen M. Cameron 		dev_warn(d, "cmd %p has completed with errors\n", cp);
1413edd16368SStephen M. Cameron 		dev_warn(d, "cmd %p has SCSI Status = %x\n", cp,
1414edd16368SStephen M. Cameron 				ei->ScsiStatus);
1415edd16368SStephen M. Cameron 		if (ei->ScsiStatus == 0)
1416edd16368SStephen M. Cameron 			dev_warn(d, "SCSI status is abnormally zero.  "
1417edd16368SStephen M. Cameron 			"(probably indicates selection timeout "
1418edd16368SStephen M. Cameron 			"reported incorrectly due to a known "
1419edd16368SStephen M. Cameron 			"firmware bug, circa July, 2001.)\n");
1420edd16368SStephen M. Cameron 		break;
1421edd16368SStephen M. Cameron 	case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1422edd16368SStephen M. Cameron 			dev_info(d, "UNDERRUN\n");
1423edd16368SStephen M. Cameron 		break;
1424edd16368SStephen M. Cameron 	case CMD_DATA_OVERRUN:
1425edd16368SStephen M. Cameron 		dev_warn(d, "cp %p has completed with data overrun\n", cp);
1426edd16368SStephen M. Cameron 		break;
1427edd16368SStephen M. Cameron 	case CMD_INVALID: {
1428edd16368SStephen M. Cameron 		/* controller unfortunately reports SCSI passthru's
1429edd16368SStephen M. Cameron 		 * to non-existent targets as invalid commands.
1430edd16368SStephen M. Cameron 		 */
1431edd16368SStephen M. Cameron 		dev_warn(d, "cp %p is reported invalid (probably means "
1432edd16368SStephen M. Cameron 			"target device no longer present)\n", cp);
1433edd16368SStephen M. Cameron 		/* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
1434edd16368SStephen M. Cameron 		print_cmd(cp);  */
1435edd16368SStephen M. Cameron 		}
1436edd16368SStephen M. Cameron 		break;
1437edd16368SStephen M. Cameron 	case CMD_PROTOCOL_ERR:
1438edd16368SStephen M. Cameron 		dev_warn(d, "cp %p has protocol error \n", cp);
1439edd16368SStephen M. Cameron 		break;
1440edd16368SStephen M. Cameron 	case CMD_HARDWARE_ERR:
1441edd16368SStephen M. Cameron 		/* cmd->result = DID_ERROR << 16; */
1442edd16368SStephen M. Cameron 		dev_warn(d, "cp %p had hardware error\n", cp);
1443edd16368SStephen M. Cameron 		break;
1444edd16368SStephen M. Cameron 	case CMD_CONNECTION_LOST:
1445edd16368SStephen M. Cameron 		dev_warn(d, "cp %p had connection lost\n", cp);
1446edd16368SStephen M. Cameron 		break;
1447edd16368SStephen M. Cameron 	case CMD_ABORTED:
1448edd16368SStephen M. Cameron 		dev_warn(d, "cp %p was aborted\n", cp);
1449edd16368SStephen M. Cameron 		break;
1450edd16368SStephen M. Cameron 	case CMD_ABORT_FAILED:
1451edd16368SStephen M. Cameron 		dev_warn(d, "cp %p reports abort failed\n", cp);
1452edd16368SStephen M. Cameron 		break;
1453edd16368SStephen M. Cameron 	case CMD_UNSOLICITED_ABORT:
1454edd16368SStephen M. Cameron 		dev_warn(d, "cp %p aborted due to an unsolicited abort\n", cp);
1455edd16368SStephen M. Cameron 		break;
1456edd16368SStephen M. Cameron 	case CMD_TIMEOUT:
1457edd16368SStephen M. Cameron 		dev_warn(d, "cp %p timed out\n", cp);
1458edd16368SStephen M. Cameron 		break;
14591d5e2ed0SStephen M. Cameron 	case CMD_UNABORTABLE:
14601d5e2ed0SStephen M. Cameron 		dev_warn(d, "Command unabortable\n");
14611d5e2ed0SStephen M. Cameron 		break;
1462edd16368SStephen M. Cameron 	default:
1463edd16368SStephen M. Cameron 		dev_warn(d, "cp %p returned unknown status %x\n", cp,
1464edd16368SStephen M. Cameron 				ei->CommandStatus);
1465edd16368SStephen M. Cameron 	}
1466edd16368SStephen M. Cameron }
1467edd16368SStephen M. Cameron 
1468edd16368SStephen M. Cameron static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
1469edd16368SStephen M. Cameron 			unsigned char page, unsigned char *buf,
1470edd16368SStephen M. Cameron 			unsigned char bufsize)
1471edd16368SStephen M. Cameron {
1472edd16368SStephen M. Cameron 	int rc = IO_OK;
1473edd16368SStephen M. Cameron 	struct CommandList *c;
1474edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
1475edd16368SStephen M. Cameron 
1476edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
1477edd16368SStephen M. Cameron 
1478edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
1479edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1480ecd9aad4SStephen M. Cameron 		return -ENOMEM;
1481edd16368SStephen M. Cameron 	}
1482edd16368SStephen M. Cameron 
1483edd16368SStephen M. Cameron 	fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize, page, scsi3addr, TYPE_CMD);
1484edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1485edd16368SStephen M. Cameron 	ei = c->err_info;
1486edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
1487edd16368SStephen M. Cameron 		hpsa_scsi_interpret_error(c);
1488edd16368SStephen M. Cameron 		rc = -1;
1489edd16368SStephen M. Cameron 	}
1490edd16368SStephen M. Cameron 	cmd_special_free(h, c);
1491edd16368SStephen M. Cameron 	return rc;
1492edd16368SStephen M. Cameron }
1493edd16368SStephen M. Cameron 
1494edd16368SStephen M. Cameron static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr)
1495edd16368SStephen M. Cameron {
1496edd16368SStephen M. Cameron 	int rc = IO_OK;
1497edd16368SStephen M. Cameron 	struct CommandList *c;
1498edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
1499edd16368SStephen M. Cameron 
1500edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
1501edd16368SStephen M. Cameron 
1502edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
1503edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1504e9ea04a6SStephen M. Cameron 		return -ENOMEM;
1505edd16368SStephen M. Cameron 	}
1506edd16368SStephen M. Cameron 
1507edd16368SStephen M. Cameron 	fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0, scsi3addr, TYPE_MSG);
1508edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core(h, c);
1509edd16368SStephen M. Cameron 	/* no unmap needed here because no data xfer. */
1510edd16368SStephen M. Cameron 
1511edd16368SStephen M. Cameron 	ei = c->err_info;
1512edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0) {
1513edd16368SStephen M. Cameron 		hpsa_scsi_interpret_error(c);
1514edd16368SStephen M. Cameron 		rc = -1;
1515edd16368SStephen M. Cameron 	}
1516edd16368SStephen M. Cameron 	cmd_special_free(h, c);
1517edd16368SStephen M. Cameron 	return rc;
1518edd16368SStephen M. Cameron }
1519edd16368SStephen M. Cameron 
1520edd16368SStephen M. Cameron static void hpsa_get_raid_level(struct ctlr_info *h,
1521edd16368SStephen M. Cameron 	unsigned char *scsi3addr, unsigned char *raid_level)
1522edd16368SStephen M. Cameron {
1523edd16368SStephen M. Cameron 	int rc;
1524edd16368SStephen M. Cameron 	unsigned char *buf;
1525edd16368SStephen M. Cameron 
1526edd16368SStephen M. Cameron 	*raid_level = RAID_UNKNOWN;
1527edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
1528edd16368SStephen M. Cameron 	if (!buf)
1529edd16368SStephen M. Cameron 		return;
1530edd16368SStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0xC1, buf, 64);
1531edd16368SStephen M. Cameron 	if (rc == 0)
1532edd16368SStephen M. Cameron 		*raid_level = buf[8];
1533edd16368SStephen M. Cameron 	if (*raid_level > RAID_UNKNOWN)
1534edd16368SStephen M. Cameron 		*raid_level = RAID_UNKNOWN;
1535edd16368SStephen M. Cameron 	kfree(buf);
1536edd16368SStephen M. Cameron 	return;
1537edd16368SStephen M. Cameron }
1538edd16368SStephen M. Cameron 
1539edd16368SStephen M. Cameron /* Get the device id from inquiry page 0x83 */
1540edd16368SStephen M. Cameron static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
1541edd16368SStephen M. Cameron 	unsigned char *device_id, int buflen)
1542edd16368SStephen M. Cameron {
1543edd16368SStephen M. Cameron 	int rc;
1544edd16368SStephen M. Cameron 	unsigned char *buf;
1545edd16368SStephen M. Cameron 
1546edd16368SStephen M. Cameron 	if (buflen > 16)
1547edd16368SStephen M. Cameron 		buflen = 16;
1548edd16368SStephen M. Cameron 	buf = kzalloc(64, GFP_KERNEL);
1549edd16368SStephen M. Cameron 	if (!buf)
1550edd16368SStephen M. Cameron 		return -1;
1551edd16368SStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0x83, buf, 64);
1552edd16368SStephen M. Cameron 	if (rc == 0)
1553edd16368SStephen M. Cameron 		memcpy(device_id, &buf[8], buflen);
1554edd16368SStephen M. Cameron 	kfree(buf);
1555edd16368SStephen M. Cameron 	return rc != 0;
1556edd16368SStephen M. Cameron }
1557edd16368SStephen M. Cameron 
1558edd16368SStephen M. Cameron static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
1559edd16368SStephen M. Cameron 		struct ReportLUNdata *buf, int bufsize,
1560edd16368SStephen M. Cameron 		int extended_response)
1561edd16368SStephen M. Cameron {
1562edd16368SStephen M. Cameron 	int rc = IO_OK;
1563edd16368SStephen M. Cameron 	struct CommandList *c;
1564edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
1565edd16368SStephen M. Cameron 	struct ErrorInfo *ei;
1566edd16368SStephen M. Cameron 
1567edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
1568edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
1569edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1570edd16368SStephen M. Cameron 		return -1;
1571edd16368SStephen M. Cameron 	}
1572e89c0ae7SStephen M. Cameron 	/* address the controller */
1573e89c0ae7SStephen M. Cameron 	memset(scsi3addr, 0, sizeof(scsi3addr));
1574edd16368SStephen M. Cameron 	fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
1575edd16368SStephen M. Cameron 		buf, bufsize, 0, scsi3addr, TYPE_CMD);
1576edd16368SStephen M. Cameron 	if (extended_response)
1577edd16368SStephen M. Cameron 		c->Request.CDB[1] = extended_response;
1578edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1579edd16368SStephen M. Cameron 	ei = c->err_info;
1580edd16368SStephen M. Cameron 	if (ei->CommandStatus != 0 &&
1581edd16368SStephen M. Cameron 	    ei->CommandStatus != CMD_DATA_UNDERRUN) {
1582edd16368SStephen M. Cameron 		hpsa_scsi_interpret_error(c);
1583edd16368SStephen M. Cameron 		rc = -1;
1584edd16368SStephen M. Cameron 	}
1585edd16368SStephen M. Cameron 	cmd_special_free(h, c);
1586edd16368SStephen M. Cameron 	return rc;
1587edd16368SStephen M. Cameron }
1588edd16368SStephen M. Cameron 
1589edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
1590edd16368SStephen M. Cameron 		struct ReportLUNdata *buf,
1591edd16368SStephen M. Cameron 		int bufsize, int extended_response)
1592edd16368SStephen M. Cameron {
1593edd16368SStephen M. Cameron 	return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
1594edd16368SStephen M. Cameron }
1595edd16368SStephen M. Cameron 
1596edd16368SStephen M. Cameron static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
1597edd16368SStephen M. Cameron 		struct ReportLUNdata *buf, int bufsize)
1598edd16368SStephen M. Cameron {
1599edd16368SStephen M. Cameron 	return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
1600edd16368SStephen M. Cameron }
1601edd16368SStephen M. Cameron 
1602edd16368SStephen M. Cameron static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
1603edd16368SStephen M. Cameron 	int bus, int target, int lun)
1604edd16368SStephen M. Cameron {
1605edd16368SStephen M. Cameron 	device->bus = bus;
1606edd16368SStephen M. Cameron 	device->target = target;
1607edd16368SStephen M. Cameron 	device->lun = lun;
1608edd16368SStephen M. Cameron }
1609edd16368SStephen M. Cameron 
1610edd16368SStephen M. Cameron static int hpsa_update_device_info(struct ctlr_info *h,
16110b0e1d6cSStephen M. Cameron 	unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
16120b0e1d6cSStephen M. Cameron 	unsigned char *is_OBDR_device)
1613edd16368SStephen M. Cameron {
16140b0e1d6cSStephen M. Cameron 
16150b0e1d6cSStephen M. Cameron #define OBDR_SIG_OFFSET 43
16160b0e1d6cSStephen M. Cameron #define OBDR_TAPE_SIG "$DR-10"
16170b0e1d6cSStephen M. Cameron #define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
16180b0e1d6cSStephen M. Cameron #define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
16190b0e1d6cSStephen M. Cameron 
1620ea6d3bc3SStephen M. Cameron 	unsigned char *inq_buff;
16210b0e1d6cSStephen M. Cameron 	unsigned char *obdr_sig;
1622edd16368SStephen M. Cameron 
1623ea6d3bc3SStephen M. Cameron 	inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
1624edd16368SStephen M. Cameron 	if (!inq_buff)
1625edd16368SStephen M. Cameron 		goto bail_out;
1626edd16368SStephen M. Cameron 
1627edd16368SStephen M. Cameron 	/* Do an inquiry to the device to see what it is. */
1628edd16368SStephen M. Cameron 	if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
1629edd16368SStephen M. Cameron 		(unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
1630edd16368SStephen M. Cameron 		/* Inquiry failed (msg printed already) */
1631edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev,
1632edd16368SStephen M. Cameron 			"hpsa_update_device_info: inquiry failed\n");
1633edd16368SStephen M. Cameron 		goto bail_out;
1634edd16368SStephen M. Cameron 	}
1635edd16368SStephen M. Cameron 
1636edd16368SStephen M. Cameron 	this_device->devtype = (inq_buff[0] & 0x1f);
1637edd16368SStephen M. Cameron 	memcpy(this_device->scsi3addr, scsi3addr, 8);
1638edd16368SStephen M. Cameron 	memcpy(this_device->vendor, &inq_buff[8],
1639edd16368SStephen M. Cameron 		sizeof(this_device->vendor));
1640edd16368SStephen M. Cameron 	memcpy(this_device->model, &inq_buff[16],
1641edd16368SStephen M. Cameron 		sizeof(this_device->model));
1642edd16368SStephen M. Cameron 	memset(this_device->device_id, 0,
1643edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
1644edd16368SStephen M. Cameron 	hpsa_get_device_id(h, scsi3addr, this_device->device_id,
1645edd16368SStephen M. Cameron 		sizeof(this_device->device_id));
1646edd16368SStephen M. Cameron 
1647edd16368SStephen M. Cameron 	if (this_device->devtype == TYPE_DISK &&
1648edd16368SStephen M. Cameron 		is_logical_dev_addr_mode(scsi3addr))
1649edd16368SStephen M. Cameron 		hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
1650edd16368SStephen M. Cameron 	else
1651edd16368SStephen M. Cameron 		this_device->raid_level = RAID_UNKNOWN;
1652edd16368SStephen M. Cameron 
16530b0e1d6cSStephen M. Cameron 	if (is_OBDR_device) {
16540b0e1d6cSStephen M. Cameron 		/* See if this is a One-Button-Disaster-Recovery device
16550b0e1d6cSStephen M. Cameron 		 * by looking for "$DR-10" at offset 43 in inquiry data.
16560b0e1d6cSStephen M. Cameron 		 */
16570b0e1d6cSStephen M. Cameron 		obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
16580b0e1d6cSStephen M. Cameron 		*is_OBDR_device = (this_device->devtype == TYPE_ROM &&
16590b0e1d6cSStephen M. Cameron 					strncmp(obdr_sig, OBDR_TAPE_SIG,
16600b0e1d6cSStephen M. Cameron 						OBDR_SIG_LEN) == 0);
16610b0e1d6cSStephen M. Cameron 	}
16620b0e1d6cSStephen M. Cameron 
1663edd16368SStephen M. Cameron 	kfree(inq_buff);
1664edd16368SStephen M. Cameron 	return 0;
1665edd16368SStephen M. Cameron 
1666edd16368SStephen M. Cameron bail_out:
1667edd16368SStephen M. Cameron 	kfree(inq_buff);
1668edd16368SStephen M. Cameron 	return 1;
1669edd16368SStephen M. Cameron }
1670edd16368SStephen M. Cameron 
16714f4eb9f1SScott Teel static unsigned char *ext_target_model[] = {
1672edd16368SStephen M. Cameron 	"MSA2012",
1673edd16368SStephen M. Cameron 	"MSA2024",
1674edd16368SStephen M. Cameron 	"MSA2312",
1675edd16368SStephen M. Cameron 	"MSA2324",
1676fda38518SStephen M. Cameron 	"P2000 G3 SAS",
1677edd16368SStephen M. Cameron 	NULL,
1678edd16368SStephen M. Cameron };
1679edd16368SStephen M. Cameron 
16804f4eb9f1SScott Teel static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
1681edd16368SStephen M. Cameron {
1682edd16368SStephen M. Cameron 	int i;
1683edd16368SStephen M. Cameron 
16844f4eb9f1SScott Teel 	for (i = 0; ext_target_model[i]; i++)
16854f4eb9f1SScott Teel 		if (strncmp(device->model, ext_target_model[i],
16864f4eb9f1SScott Teel 			strlen(ext_target_model[i])) == 0)
1687edd16368SStephen M. Cameron 			return 1;
1688edd16368SStephen M. Cameron 	return 0;
1689edd16368SStephen M. Cameron }
1690edd16368SStephen M. Cameron 
1691edd16368SStephen M. Cameron /* Helper function to assign bus, target, lun mapping of devices.
16924f4eb9f1SScott Teel  * Puts non-external target logical volumes on bus 0, external target logical
1693edd16368SStephen M. Cameron  * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
1694edd16368SStephen M. Cameron  * Logical drive target and lun are assigned at this time, but
1695edd16368SStephen M. Cameron  * physical device lun and target assignment are deferred (assigned
1696edd16368SStephen M. Cameron  * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
1697edd16368SStephen M. Cameron  */
1698edd16368SStephen M. Cameron static void figure_bus_target_lun(struct ctlr_info *h,
16991f310bdeSStephen M. Cameron 	u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
1700edd16368SStephen M. Cameron {
17011f310bdeSStephen M. Cameron 	u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
1702edd16368SStephen M. Cameron 
17031f310bdeSStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes)) {
17041f310bdeSStephen M. Cameron 		/* physical device, target and lun filled in later */
17051f310bdeSStephen M. Cameron 		if (is_hba_lunid(lunaddrbytes))
17061f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
17071f310bdeSStephen M. Cameron 		else
17081f310bdeSStephen M. Cameron 			/* defer target, lun assignment for physical devices */
17091f310bdeSStephen M. Cameron 			hpsa_set_bus_target_lun(device, 2, -1, -1);
17101f310bdeSStephen M. Cameron 		return;
17111f310bdeSStephen M. Cameron 	}
17121f310bdeSStephen M. Cameron 	/* It's a logical device */
17134f4eb9f1SScott Teel 	if (is_ext_target(h, device)) {
17144f4eb9f1SScott Teel 		/* external target way, put logicals on bus 1
1715339b2b14SStephen M. Cameron 		 * and match target/lun numbers box
17161f310bdeSStephen M. Cameron 		 * reports, other smart array, bus 0, target 0, match lunid
1717339b2b14SStephen M. Cameron 		 */
17181f310bdeSStephen M. Cameron 		hpsa_set_bus_target_lun(device,
17191f310bdeSStephen M. Cameron 			1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
17201f310bdeSStephen M. Cameron 		return;
1721339b2b14SStephen M. Cameron 	}
17221f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
1723edd16368SStephen M. Cameron }
1724edd16368SStephen M. Cameron 
1725edd16368SStephen M. Cameron /*
1726edd16368SStephen M. Cameron  * If there is no lun 0 on a target, linux won't find any devices.
17274f4eb9f1SScott Teel  * For the external targets (arrays), we have to manually detect the enclosure
1728edd16368SStephen M. Cameron  * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
1729edd16368SStephen M. Cameron  * it for some reason.  *tmpdevice is the target we're adding,
1730edd16368SStephen M. Cameron  * this_device is a pointer into the current element of currentsd[]
1731edd16368SStephen M. Cameron  * that we're building up in update_scsi_devices(), below.
1732edd16368SStephen M. Cameron  * lunzerobits is a bitmap that tracks which targets already have a
1733edd16368SStephen M. Cameron  * lun 0 assigned.
1734edd16368SStephen M. Cameron  * Returns 1 if an enclosure was added, 0 if not.
1735edd16368SStephen M. Cameron  */
17364f4eb9f1SScott Teel static int add_ext_target_dev(struct ctlr_info *h,
1737edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *tmpdevice,
173801a02ffcSStephen M. Cameron 	struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
17394f4eb9f1SScott Teel 	unsigned long lunzerobits[], int *n_ext_target_devs)
1740edd16368SStephen M. Cameron {
1741edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
1742edd16368SStephen M. Cameron 
17431f310bdeSStephen M. Cameron 	if (test_bit(tmpdevice->target, lunzerobits))
1744edd16368SStephen M. Cameron 		return 0; /* There is already a lun 0 on this target. */
1745edd16368SStephen M. Cameron 
1746edd16368SStephen M. Cameron 	if (!is_logical_dev_addr_mode(lunaddrbytes))
1747edd16368SStephen M. Cameron 		return 0; /* It's the logical targets that may lack lun 0. */
1748edd16368SStephen M. Cameron 
17494f4eb9f1SScott Teel 	if (!is_ext_target(h, tmpdevice))
17504f4eb9f1SScott Teel 		return 0; /* Only external target devices have this problem. */
1751edd16368SStephen M. Cameron 
17521f310bdeSStephen M. Cameron 	if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
1753edd16368SStephen M. Cameron 		return 0;
1754edd16368SStephen M. Cameron 
1755c4f8a299SStephen M. Cameron 	memset(scsi3addr, 0, 8);
17561f310bdeSStephen M. Cameron 	scsi3addr[3] = tmpdevice->target;
1757edd16368SStephen M. Cameron 	if (is_hba_lunid(scsi3addr))
1758edd16368SStephen M. Cameron 		return 0; /* Don't add the RAID controller here. */
1759edd16368SStephen M. Cameron 
1760339b2b14SStephen M. Cameron 	if (is_scsi_rev_5(h))
1761339b2b14SStephen M. Cameron 		return 0; /* p1210m doesn't need to do this. */
1762339b2b14SStephen M. Cameron 
17634f4eb9f1SScott Teel 	if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
1764aca4a520SScott Teel 		dev_warn(&h->pdev->dev, "Maximum number of external "
1765aca4a520SScott Teel 			"target devices exceeded.  Check your hardware "
1766edd16368SStephen M. Cameron 			"configuration.");
1767edd16368SStephen M. Cameron 		return 0;
1768edd16368SStephen M. Cameron 	}
1769edd16368SStephen M. Cameron 
17700b0e1d6cSStephen M. Cameron 	if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
1771edd16368SStephen M. Cameron 		return 0;
17724f4eb9f1SScott Teel 	(*n_ext_target_devs)++;
17731f310bdeSStephen M. Cameron 	hpsa_set_bus_target_lun(this_device,
17741f310bdeSStephen M. Cameron 				tmpdevice->bus, tmpdevice->target, 0);
17751f310bdeSStephen M. Cameron 	set_bit(tmpdevice->target, lunzerobits);
1776edd16368SStephen M. Cameron 	return 1;
1777edd16368SStephen M. Cameron }
1778edd16368SStephen M. Cameron 
1779edd16368SStephen M. Cameron /*
1780edd16368SStephen M. Cameron  * Do CISS_REPORT_PHYS and CISS_REPORT_LOG.  Data is returned in physdev,
1781edd16368SStephen M. Cameron  * logdev.  The number of luns in physdev and logdev are returned in
1782edd16368SStephen M. Cameron  * *nphysicals and *nlogicals, respectively.
1783edd16368SStephen M. Cameron  * Returns 0 on success, -1 otherwise.
1784edd16368SStephen M. Cameron  */
1785edd16368SStephen M. Cameron static int hpsa_gather_lun_info(struct ctlr_info *h,
1786edd16368SStephen M. Cameron 	int reportlunsize,
178701a02ffcSStephen M. Cameron 	struct ReportLUNdata *physdev, u32 *nphysicals,
178801a02ffcSStephen M. Cameron 	struct ReportLUNdata *logdev, u32 *nlogicals)
1789edd16368SStephen M. Cameron {
1790edd16368SStephen M. Cameron 	if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize, 0)) {
1791edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
1792edd16368SStephen M. Cameron 		return -1;
1793edd16368SStephen M. Cameron 	}
17946df1e954SStephen M. Cameron 	*nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 8;
1795edd16368SStephen M. Cameron 	if (*nphysicals > HPSA_MAX_PHYS_LUN) {
1796edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
1797edd16368SStephen M. Cameron 			"  %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
1798edd16368SStephen M. Cameron 			*nphysicals - HPSA_MAX_PHYS_LUN);
1799edd16368SStephen M. Cameron 		*nphysicals = HPSA_MAX_PHYS_LUN;
1800edd16368SStephen M. Cameron 	}
1801edd16368SStephen M. Cameron 	if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
1802edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
1803edd16368SStephen M. Cameron 		return -1;
1804edd16368SStephen M. Cameron 	}
18056df1e954SStephen M. Cameron 	*nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
1806edd16368SStephen M. Cameron 	/* Reject Logicals in excess of our max capability. */
1807edd16368SStephen M. Cameron 	if (*nlogicals > HPSA_MAX_LUN) {
1808edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
1809edd16368SStephen M. Cameron 			"maximum logical LUNs (%d) exceeded.  "
1810edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_LUN,
1811edd16368SStephen M. Cameron 			*nlogicals - HPSA_MAX_LUN);
1812edd16368SStephen M. Cameron 			*nlogicals = HPSA_MAX_LUN;
1813edd16368SStephen M. Cameron 	}
1814edd16368SStephen M. Cameron 	if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
1815edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
1816edd16368SStephen M. Cameron 			"maximum logical + physical LUNs (%d) exceeded. "
1817edd16368SStephen M. Cameron 			"%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
1818edd16368SStephen M. Cameron 			*nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
1819edd16368SStephen M. Cameron 		*nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
1820edd16368SStephen M. Cameron 	}
1821edd16368SStephen M. Cameron 	return 0;
1822edd16368SStephen M. Cameron }
1823edd16368SStephen M. Cameron 
1824339b2b14SStephen M. Cameron u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i,
1825339b2b14SStephen M. Cameron 	int nphysicals, int nlogicals, struct ReportLUNdata *physdev_list,
1826339b2b14SStephen M. Cameron 	struct ReportLUNdata *logdev_list)
1827339b2b14SStephen M. Cameron {
1828339b2b14SStephen M. Cameron 	/* Helper function, figure out where the LUN ID info is coming from
1829339b2b14SStephen M. Cameron 	 * given index i, lists of physical and logical devices, where in
1830339b2b14SStephen M. Cameron 	 * the list the raid controller is supposed to appear (first or last)
1831339b2b14SStephen M. Cameron 	 */
1832339b2b14SStephen M. Cameron 
1833339b2b14SStephen M. Cameron 	int logicals_start = nphysicals + (raid_ctlr_position == 0);
1834339b2b14SStephen M. Cameron 	int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
1835339b2b14SStephen M. Cameron 
1836339b2b14SStephen M. Cameron 	if (i == raid_ctlr_position)
1837339b2b14SStephen M. Cameron 		return RAID_CTLR_LUNID;
1838339b2b14SStephen M. Cameron 
1839339b2b14SStephen M. Cameron 	if (i < logicals_start)
1840339b2b14SStephen M. Cameron 		return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0];
1841339b2b14SStephen M. Cameron 
1842339b2b14SStephen M. Cameron 	if (i < last_device)
1843339b2b14SStephen M. Cameron 		return &logdev_list->LUN[i - nphysicals -
1844339b2b14SStephen M. Cameron 			(raid_ctlr_position == 0)][0];
1845339b2b14SStephen M. Cameron 	BUG();
1846339b2b14SStephen M. Cameron 	return NULL;
1847339b2b14SStephen M. Cameron }
1848339b2b14SStephen M. Cameron 
1849edd16368SStephen M. Cameron static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
1850edd16368SStephen M. Cameron {
1851edd16368SStephen M. Cameron 	/* the idea here is we could get notified
1852edd16368SStephen M. Cameron 	 * that some devices have changed, so we do a report
1853edd16368SStephen M. Cameron 	 * physical luns and report logical luns cmd, and adjust
1854edd16368SStephen M. Cameron 	 * our list of devices accordingly.
1855edd16368SStephen M. Cameron 	 *
1856edd16368SStephen M. Cameron 	 * The scsi3addr's of devices won't change so long as the
1857edd16368SStephen M. Cameron 	 * adapter is not reset.  That means we can rescan and
1858edd16368SStephen M. Cameron 	 * tell which devices we already know about, vs. new
1859edd16368SStephen M. Cameron 	 * devices, vs.  disappearing devices.
1860edd16368SStephen M. Cameron 	 */
1861edd16368SStephen M. Cameron 	struct ReportLUNdata *physdev_list = NULL;
1862edd16368SStephen M. Cameron 	struct ReportLUNdata *logdev_list = NULL;
186301a02ffcSStephen M. Cameron 	u32 nphysicals = 0;
186401a02ffcSStephen M. Cameron 	u32 nlogicals = 0;
186501a02ffcSStephen M. Cameron 	u32 ndev_allocated = 0;
1866edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
1867edd16368SStephen M. Cameron 	int ncurrent = 0;
1868edd16368SStephen M. Cameron 	int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 8;
18694f4eb9f1SScott Teel 	int i, n_ext_target_devs, ndevs_to_allocate;
1870339b2b14SStephen M. Cameron 	int raid_ctlr_position;
1871aca4a520SScott Teel 	DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
1872edd16368SStephen M. Cameron 
1873cfe5badcSScott Teel 	currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
1874edd16368SStephen M. Cameron 	physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
1875edd16368SStephen M. Cameron 	logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
1876edd16368SStephen M. Cameron 	tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
1877edd16368SStephen M. Cameron 
18780b0e1d6cSStephen M. Cameron 	if (!currentsd || !physdev_list || !logdev_list || !tmpdevice) {
1879edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory\n");
1880edd16368SStephen M. Cameron 		goto out;
1881edd16368SStephen M. Cameron 	}
1882edd16368SStephen M. Cameron 	memset(lunzerobits, 0, sizeof(lunzerobits));
1883edd16368SStephen M. Cameron 
1884edd16368SStephen M. Cameron 	if (hpsa_gather_lun_info(h, reportlunsize, physdev_list, &nphysicals,
1885edd16368SStephen M. Cameron 			logdev_list, &nlogicals))
1886edd16368SStephen M. Cameron 		goto out;
1887edd16368SStephen M. Cameron 
1888aca4a520SScott Teel 	/* We might see up to the maximum number of logical and physical disks
1889aca4a520SScott Teel 	 * plus external target devices, and a device for the local RAID
1890aca4a520SScott Teel 	 * controller.
1891edd16368SStephen M. Cameron 	 */
1892aca4a520SScott Teel 	ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
1893edd16368SStephen M. Cameron 
1894edd16368SStephen M. Cameron 	/* Allocate the per device structures */
1895edd16368SStephen M. Cameron 	for (i = 0; i < ndevs_to_allocate; i++) {
1896b7ec021fSScott Teel 		if (i >= HPSA_MAX_DEVICES) {
1897b7ec021fSScott Teel 			dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
1898b7ec021fSScott Teel 				"  %d devices ignored.\n", HPSA_MAX_DEVICES,
1899b7ec021fSScott Teel 				ndevs_to_allocate - HPSA_MAX_DEVICES);
1900b7ec021fSScott Teel 			break;
1901b7ec021fSScott Teel 		}
1902b7ec021fSScott Teel 
1903edd16368SStephen M. Cameron 		currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
1904edd16368SStephen M. Cameron 		if (!currentsd[i]) {
1905edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
1906edd16368SStephen M. Cameron 				__FILE__, __LINE__);
1907edd16368SStephen M. Cameron 			goto out;
1908edd16368SStephen M. Cameron 		}
1909edd16368SStephen M. Cameron 		ndev_allocated++;
1910edd16368SStephen M. Cameron 	}
1911edd16368SStephen M. Cameron 
1912339b2b14SStephen M. Cameron 	if (unlikely(is_scsi_rev_5(h)))
1913339b2b14SStephen M. Cameron 		raid_ctlr_position = 0;
1914339b2b14SStephen M. Cameron 	else
1915339b2b14SStephen M. Cameron 		raid_ctlr_position = nphysicals + nlogicals;
1916339b2b14SStephen M. Cameron 
1917edd16368SStephen M. Cameron 	/* adjust our table of devices */
19184f4eb9f1SScott Teel 	n_ext_target_devs = 0;
1919edd16368SStephen M. Cameron 	for (i = 0; i < nphysicals + nlogicals + 1; i++) {
19200b0e1d6cSStephen M. Cameron 		u8 *lunaddrbytes, is_OBDR = 0;
1921edd16368SStephen M. Cameron 
1922edd16368SStephen M. Cameron 		/* Figure out where the LUN ID info is coming from */
1923339b2b14SStephen M. Cameron 		lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
1924339b2b14SStephen M. Cameron 			i, nphysicals, nlogicals, physdev_list, logdev_list);
1925edd16368SStephen M. Cameron 		/* skip masked physical devices. */
1926339b2b14SStephen M. Cameron 		if (lunaddrbytes[3] & 0xC0 &&
1927339b2b14SStephen M. Cameron 			i < nphysicals + (raid_ctlr_position == 0))
1928edd16368SStephen M. Cameron 			continue;
1929edd16368SStephen M. Cameron 
1930edd16368SStephen M. Cameron 		/* Get device type, vendor, model, device id */
19310b0e1d6cSStephen M. Cameron 		if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
19320b0e1d6cSStephen M. Cameron 							&is_OBDR))
1933edd16368SStephen M. Cameron 			continue; /* skip it if we can't talk to it. */
19341f310bdeSStephen M. Cameron 		figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
1935edd16368SStephen M. Cameron 		this_device = currentsd[ncurrent];
1936edd16368SStephen M. Cameron 
1937edd16368SStephen M. Cameron 		/*
19384f4eb9f1SScott Teel 		 * For external target devices, we have to insert a LUN 0 which
1939edd16368SStephen M. Cameron 		 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
1940edd16368SStephen M. Cameron 		 * is nonetheless an enclosure device there.  We have to
1941edd16368SStephen M. Cameron 		 * present that otherwise linux won't find anything if
1942edd16368SStephen M. Cameron 		 * there is no lun 0.
1943edd16368SStephen M. Cameron 		 */
19444f4eb9f1SScott Teel 		if (add_ext_target_dev(h, tmpdevice, this_device,
19451f310bdeSStephen M. Cameron 				lunaddrbytes, lunzerobits,
19464f4eb9f1SScott Teel 				&n_ext_target_devs)) {
1947edd16368SStephen M. Cameron 			ncurrent++;
1948edd16368SStephen M. Cameron 			this_device = currentsd[ncurrent];
1949edd16368SStephen M. Cameron 		}
1950edd16368SStephen M. Cameron 
1951edd16368SStephen M. Cameron 		*this_device = *tmpdevice;
1952edd16368SStephen M. Cameron 
1953edd16368SStephen M. Cameron 		switch (this_device->devtype) {
19540b0e1d6cSStephen M. Cameron 		case TYPE_ROM:
1955edd16368SStephen M. Cameron 			/* We don't *really* support actual CD-ROM devices,
1956edd16368SStephen M. Cameron 			 * just "One Button Disaster Recovery" tape drive
1957edd16368SStephen M. Cameron 			 * which temporarily pretends to be a CD-ROM drive.
1958edd16368SStephen M. Cameron 			 * So we check that the device is really an OBDR tape
1959edd16368SStephen M. Cameron 			 * device by checking for "$DR-10" in bytes 43-48 of
1960edd16368SStephen M. Cameron 			 * the inquiry data.
1961edd16368SStephen M. Cameron 			 */
19620b0e1d6cSStephen M. Cameron 			if (is_OBDR)
1963edd16368SStephen M. Cameron 				ncurrent++;
1964edd16368SStephen M. Cameron 			break;
1965edd16368SStephen M. Cameron 		case TYPE_DISK:
1966edd16368SStephen M. Cameron 			if (i < nphysicals)
1967edd16368SStephen M. Cameron 				break;
1968edd16368SStephen M. Cameron 			ncurrent++;
1969edd16368SStephen M. Cameron 			break;
1970edd16368SStephen M. Cameron 		case TYPE_TAPE:
1971edd16368SStephen M. Cameron 		case TYPE_MEDIUM_CHANGER:
1972edd16368SStephen M. Cameron 			ncurrent++;
1973edd16368SStephen M. Cameron 			break;
1974edd16368SStephen M. Cameron 		case TYPE_RAID:
1975edd16368SStephen M. Cameron 			/* Only present the Smartarray HBA as a RAID controller.
1976edd16368SStephen M. Cameron 			 * If it's a RAID controller other than the HBA itself
1977edd16368SStephen M. Cameron 			 * (an external RAID controller, MSA500 or similar)
1978edd16368SStephen M. Cameron 			 * don't present it.
1979edd16368SStephen M. Cameron 			 */
1980edd16368SStephen M. Cameron 			if (!is_hba_lunid(lunaddrbytes))
1981edd16368SStephen M. Cameron 				break;
1982edd16368SStephen M. Cameron 			ncurrent++;
1983edd16368SStephen M. Cameron 			break;
1984edd16368SStephen M. Cameron 		default:
1985edd16368SStephen M. Cameron 			break;
1986edd16368SStephen M. Cameron 		}
1987cfe5badcSScott Teel 		if (ncurrent >= HPSA_MAX_DEVICES)
1988edd16368SStephen M. Cameron 			break;
1989edd16368SStephen M. Cameron 	}
1990edd16368SStephen M. Cameron 	adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
1991edd16368SStephen M. Cameron out:
1992edd16368SStephen M. Cameron 	kfree(tmpdevice);
1993edd16368SStephen M. Cameron 	for (i = 0; i < ndev_allocated; i++)
1994edd16368SStephen M. Cameron 		kfree(currentsd[i]);
1995edd16368SStephen M. Cameron 	kfree(currentsd);
1996edd16368SStephen M. Cameron 	kfree(physdev_list);
1997edd16368SStephen M. Cameron 	kfree(logdev_list);
1998edd16368SStephen M. Cameron }
1999edd16368SStephen M. Cameron 
2000edd16368SStephen M. Cameron /* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
2001edd16368SStephen M. Cameron  * dma mapping  and fills in the scatter gather entries of the
2002edd16368SStephen M. Cameron  * hpsa command, cp.
2003edd16368SStephen M. Cameron  */
200433a2ffceSStephen M. Cameron static int hpsa_scatter_gather(struct ctlr_info *h,
2005edd16368SStephen M. Cameron 		struct CommandList *cp,
2006edd16368SStephen M. Cameron 		struct scsi_cmnd *cmd)
2007edd16368SStephen M. Cameron {
2008edd16368SStephen M. Cameron 	unsigned int len;
2009edd16368SStephen M. Cameron 	struct scatterlist *sg;
201001a02ffcSStephen M. Cameron 	u64 addr64;
201133a2ffceSStephen M. Cameron 	int use_sg, i, sg_index, chained;
201233a2ffceSStephen M. Cameron 	struct SGDescriptor *curr_sg;
2013edd16368SStephen M. Cameron 
201433a2ffceSStephen M. Cameron 	BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
2015edd16368SStephen M. Cameron 
2016edd16368SStephen M. Cameron 	use_sg = scsi_dma_map(cmd);
2017edd16368SStephen M. Cameron 	if (use_sg < 0)
2018edd16368SStephen M. Cameron 		return use_sg;
2019edd16368SStephen M. Cameron 
2020edd16368SStephen M. Cameron 	if (!use_sg)
2021edd16368SStephen M. Cameron 		goto sglist_finished;
2022edd16368SStephen M. Cameron 
202333a2ffceSStephen M. Cameron 	curr_sg = cp->SG;
202433a2ffceSStephen M. Cameron 	chained = 0;
202533a2ffceSStephen M. Cameron 	sg_index = 0;
2026edd16368SStephen M. Cameron 	scsi_for_each_sg(cmd, sg, use_sg, i) {
202733a2ffceSStephen M. Cameron 		if (i == h->max_cmd_sg_entries - 1 &&
202833a2ffceSStephen M. Cameron 			use_sg > h->max_cmd_sg_entries) {
202933a2ffceSStephen M. Cameron 			chained = 1;
203033a2ffceSStephen M. Cameron 			curr_sg = h->cmd_sg_list[cp->cmdindex];
203133a2ffceSStephen M. Cameron 			sg_index = 0;
203233a2ffceSStephen M. Cameron 		}
203301a02ffcSStephen M. Cameron 		addr64 = (u64) sg_dma_address(sg);
2034edd16368SStephen M. Cameron 		len  = sg_dma_len(sg);
203533a2ffceSStephen M. Cameron 		curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
203633a2ffceSStephen M. Cameron 		curr_sg->Addr.upper = (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
203733a2ffceSStephen M. Cameron 		curr_sg->Len = len;
203833a2ffceSStephen M. Cameron 		curr_sg->Ext = 0;  /* we are not chaining */
203933a2ffceSStephen M. Cameron 		curr_sg++;
204033a2ffceSStephen M. Cameron 	}
204133a2ffceSStephen M. Cameron 
204233a2ffceSStephen M. Cameron 	if (use_sg + chained > h->maxSG)
204333a2ffceSStephen M. Cameron 		h->maxSG = use_sg + chained;
204433a2ffceSStephen M. Cameron 
204533a2ffceSStephen M. Cameron 	if (chained) {
204633a2ffceSStephen M. Cameron 		cp->Header.SGList = h->max_cmd_sg_entries;
204733a2ffceSStephen M. Cameron 		cp->Header.SGTotal = (u16) (use_sg + 1);
204833a2ffceSStephen M. Cameron 		hpsa_map_sg_chain_block(h, cp);
204933a2ffceSStephen M. Cameron 		return 0;
2050edd16368SStephen M. Cameron 	}
2051edd16368SStephen M. Cameron 
2052edd16368SStephen M. Cameron sglist_finished:
2053edd16368SStephen M. Cameron 
205401a02ffcSStephen M. Cameron 	cp->Header.SGList = (u8) use_sg;   /* no. SGs contig in this cmd */
205501a02ffcSStephen M. Cameron 	cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */
2056edd16368SStephen M. Cameron 	return 0;
2057edd16368SStephen M. Cameron }
2058edd16368SStephen M. Cameron 
2059edd16368SStephen M. Cameron 
2060f281233dSJeff Garzik static int hpsa_scsi_queue_command_lck(struct scsi_cmnd *cmd,
2061edd16368SStephen M. Cameron 	void (*done)(struct scsi_cmnd *))
2062edd16368SStephen M. Cameron {
2063edd16368SStephen M. Cameron 	struct ctlr_info *h;
2064edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
2065edd16368SStephen M. Cameron 	unsigned char scsi3addr[8];
2066edd16368SStephen M. Cameron 	struct CommandList *c;
2067edd16368SStephen M. Cameron 	unsigned long flags;
2068edd16368SStephen M. Cameron 
2069edd16368SStephen M. Cameron 	/* Get the ptr to our adapter structure out of cmd->host. */
2070edd16368SStephen M. Cameron 	h = sdev_to_hba(cmd->device);
2071edd16368SStephen M. Cameron 	dev = cmd->device->hostdata;
2072edd16368SStephen M. Cameron 	if (!dev) {
2073edd16368SStephen M. Cameron 		cmd->result = DID_NO_CONNECT << 16;
2074edd16368SStephen M. Cameron 		done(cmd);
2075edd16368SStephen M. Cameron 		return 0;
2076edd16368SStephen M. Cameron 	}
2077edd16368SStephen M. Cameron 	memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
2078edd16368SStephen M. Cameron 
2079edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
2080a0c12413SStephen M. Cameron 	if (unlikely(h->lockup_detected)) {
2081a0c12413SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
2082a0c12413SStephen M. Cameron 		cmd->result = DID_ERROR << 16;
2083a0c12413SStephen M. Cameron 		done(cmd);
2084a0c12413SStephen M. Cameron 		return 0;
2085a0c12413SStephen M. Cameron 	}
2086a0c12413SStephen M. Cameron 	/* Need a lock as this is being allocated from the pool */
2087edd16368SStephen M. Cameron 	c = cmd_alloc(h);
2088edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
2089edd16368SStephen M. Cameron 	if (c == NULL) {			/* trouble... */
2090edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
2091edd16368SStephen M. Cameron 		return SCSI_MLQUEUE_HOST_BUSY;
2092edd16368SStephen M. Cameron 	}
2093edd16368SStephen M. Cameron 
2094edd16368SStephen M. Cameron 	/* Fill in the command list header */
2095edd16368SStephen M. Cameron 
2096edd16368SStephen M. Cameron 	cmd->scsi_done = done;    /* save this for use by completion code */
2097edd16368SStephen M. Cameron 
2098edd16368SStephen M. Cameron 	/* save c in case we have to abort it  */
2099edd16368SStephen M. Cameron 	cmd->host_scribble = (unsigned char *) c;
2100edd16368SStephen M. Cameron 
2101edd16368SStephen M. Cameron 	c->cmd_type = CMD_SCSI;
2102edd16368SStephen M. Cameron 	c->scsi_cmd = cmd;
2103edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;  /* unused in simple mode */
2104edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
2105303932fdSDon Brace 	c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT);
2106303932fdSDon Brace 	c->Header.Tag.lower |= DIRECT_LOOKUP_BIT;
2107edd16368SStephen M. Cameron 
2108edd16368SStephen M. Cameron 	/* Fill in the request block... */
2109edd16368SStephen M. Cameron 
2110edd16368SStephen M. Cameron 	c->Request.Timeout = 0;
2111edd16368SStephen M. Cameron 	memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
2112edd16368SStephen M. Cameron 	BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
2113edd16368SStephen M. Cameron 	c->Request.CDBLen = cmd->cmd_len;
2114edd16368SStephen M. Cameron 	memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
2115edd16368SStephen M. Cameron 	c->Request.Type.Type = TYPE_CMD;
2116edd16368SStephen M. Cameron 	c->Request.Type.Attribute = ATTR_SIMPLE;
2117edd16368SStephen M. Cameron 	switch (cmd->sc_data_direction) {
2118edd16368SStephen M. Cameron 	case DMA_TO_DEVICE:
2119edd16368SStephen M. Cameron 		c->Request.Type.Direction = XFER_WRITE;
2120edd16368SStephen M. Cameron 		break;
2121edd16368SStephen M. Cameron 	case DMA_FROM_DEVICE:
2122edd16368SStephen M. Cameron 		c->Request.Type.Direction = XFER_READ;
2123edd16368SStephen M. Cameron 		break;
2124edd16368SStephen M. Cameron 	case DMA_NONE:
2125edd16368SStephen M. Cameron 		c->Request.Type.Direction = XFER_NONE;
2126edd16368SStephen M. Cameron 		break;
2127edd16368SStephen M. Cameron 	case DMA_BIDIRECTIONAL:
2128edd16368SStephen M. Cameron 		/* This can happen if a buggy application does a scsi passthru
2129edd16368SStephen M. Cameron 		 * and sets both inlen and outlen to non-zero. ( see
2130edd16368SStephen M. Cameron 		 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
2131edd16368SStephen M. Cameron 		 */
2132edd16368SStephen M. Cameron 
2133edd16368SStephen M. Cameron 		c->Request.Type.Direction = XFER_RSVD;
2134edd16368SStephen M. Cameron 		/* This is technically wrong, and hpsa controllers should
2135edd16368SStephen M. Cameron 		 * reject it with CMD_INVALID, which is the most correct
2136edd16368SStephen M. Cameron 		 * response, but non-fibre backends appear to let it
2137edd16368SStephen M. Cameron 		 * slide by, and give the same results as if this field
2138edd16368SStephen M. Cameron 		 * were set correctly.  Either way is acceptable for
2139edd16368SStephen M. Cameron 		 * our purposes here.
2140edd16368SStephen M. Cameron 		 */
2141edd16368SStephen M. Cameron 
2142edd16368SStephen M. Cameron 		break;
2143edd16368SStephen M. Cameron 
2144edd16368SStephen M. Cameron 	default:
2145edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "unknown data direction: %d\n",
2146edd16368SStephen M. Cameron 			cmd->sc_data_direction);
2147edd16368SStephen M. Cameron 		BUG();
2148edd16368SStephen M. Cameron 		break;
2149edd16368SStephen M. Cameron 	}
2150edd16368SStephen M. Cameron 
215133a2ffceSStephen M. Cameron 	if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
2152edd16368SStephen M. Cameron 		cmd_free(h, c);
2153edd16368SStephen M. Cameron 		return SCSI_MLQUEUE_HOST_BUSY;
2154edd16368SStephen M. Cameron 	}
2155edd16368SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
2156edd16368SStephen M. Cameron 	/* the cmd'll come back via intr handler in complete_scsi_command()  */
2157edd16368SStephen M. Cameron 	return 0;
2158edd16368SStephen M. Cameron }
2159edd16368SStephen M. Cameron 
2160f281233dSJeff Garzik static DEF_SCSI_QCMD(hpsa_scsi_queue_command)
2161f281233dSJeff Garzik 
2162a08a8471SStephen M. Cameron static void hpsa_scan_start(struct Scsi_Host *sh)
2163a08a8471SStephen M. Cameron {
2164a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
2165a08a8471SStephen M. Cameron 	unsigned long flags;
2166a08a8471SStephen M. Cameron 
2167a08a8471SStephen M. Cameron 	/* wait until any scan already in progress is finished. */
2168a08a8471SStephen M. Cameron 	while (1) {
2169a08a8471SStephen M. Cameron 		spin_lock_irqsave(&h->scan_lock, flags);
2170a08a8471SStephen M. Cameron 		if (h->scan_finished)
2171a08a8471SStephen M. Cameron 			break;
2172a08a8471SStephen M. Cameron 		spin_unlock_irqrestore(&h->scan_lock, flags);
2173a08a8471SStephen M. Cameron 		wait_event(h->scan_wait_queue, h->scan_finished);
2174a08a8471SStephen M. Cameron 		/* Note: We don't need to worry about a race between this
2175a08a8471SStephen M. Cameron 		 * thread and driver unload because the midlayer will
2176a08a8471SStephen M. Cameron 		 * have incremented the reference count, so unload won't
2177a08a8471SStephen M. Cameron 		 * happen if we're in here.
2178a08a8471SStephen M. Cameron 		 */
2179a08a8471SStephen M. Cameron 	}
2180a08a8471SStephen M. Cameron 	h->scan_finished = 0; /* mark scan as in progress */
2181a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
2182a08a8471SStephen M. Cameron 
2183a08a8471SStephen M. Cameron 	hpsa_update_scsi_devices(h, h->scsi_host->host_no);
2184a08a8471SStephen M. Cameron 
2185a08a8471SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
2186a08a8471SStephen M. Cameron 	h->scan_finished = 1; /* mark scan as finished. */
2187a08a8471SStephen M. Cameron 	wake_up_all(&h->scan_wait_queue);
2188a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
2189a08a8471SStephen M. Cameron }
2190a08a8471SStephen M. Cameron 
2191a08a8471SStephen M. Cameron static int hpsa_scan_finished(struct Scsi_Host *sh,
2192a08a8471SStephen M. Cameron 	unsigned long elapsed_time)
2193a08a8471SStephen M. Cameron {
2194a08a8471SStephen M. Cameron 	struct ctlr_info *h = shost_to_hba(sh);
2195a08a8471SStephen M. Cameron 	unsigned long flags;
2196a08a8471SStephen M. Cameron 	int finished;
2197a08a8471SStephen M. Cameron 
2198a08a8471SStephen M. Cameron 	spin_lock_irqsave(&h->scan_lock, flags);
2199a08a8471SStephen M. Cameron 	finished = h->scan_finished;
2200a08a8471SStephen M. Cameron 	spin_unlock_irqrestore(&h->scan_lock, flags);
2201a08a8471SStephen M. Cameron 	return finished;
2202a08a8471SStephen M. Cameron }
2203a08a8471SStephen M. Cameron 
2204667e23d4SStephen M. Cameron static int hpsa_change_queue_depth(struct scsi_device *sdev,
2205667e23d4SStephen M. Cameron 	int qdepth, int reason)
2206667e23d4SStephen M. Cameron {
2207667e23d4SStephen M. Cameron 	struct ctlr_info *h = sdev_to_hba(sdev);
2208667e23d4SStephen M. Cameron 
2209667e23d4SStephen M. Cameron 	if (reason != SCSI_QDEPTH_DEFAULT)
2210667e23d4SStephen M. Cameron 		return -ENOTSUPP;
2211667e23d4SStephen M. Cameron 
2212667e23d4SStephen M. Cameron 	if (qdepth < 1)
2213667e23d4SStephen M. Cameron 		qdepth = 1;
2214667e23d4SStephen M. Cameron 	else
2215667e23d4SStephen M. Cameron 		if (qdepth > h->nr_cmds)
2216667e23d4SStephen M. Cameron 			qdepth = h->nr_cmds;
2217667e23d4SStephen M. Cameron 	scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
2218667e23d4SStephen M. Cameron 	return sdev->queue_depth;
2219667e23d4SStephen M. Cameron }
2220667e23d4SStephen M. Cameron 
2221edd16368SStephen M. Cameron static void hpsa_unregister_scsi(struct ctlr_info *h)
2222edd16368SStephen M. Cameron {
2223edd16368SStephen M. Cameron 	/* we are being forcibly unloaded, and may not refuse. */
2224edd16368SStephen M. Cameron 	scsi_remove_host(h->scsi_host);
2225edd16368SStephen M. Cameron 	scsi_host_put(h->scsi_host);
2226edd16368SStephen M. Cameron 	h->scsi_host = NULL;
2227edd16368SStephen M. Cameron }
2228edd16368SStephen M. Cameron 
2229edd16368SStephen M. Cameron static int hpsa_register_scsi(struct ctlr_info *h)
2230edd16368SStephen M. Cameron {
2231b705690dSStephen M. Cameron 	struct Scsi_Host *sh;
2232b705690dSStephen M. Cameron 	int error;
2233edd16368SStephen M. Cameron 
2234b705690dSStephen M. Cameron 	sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
2235b705690dSStephen M. Cameron 	if (sh == NULL)
2236b705690dSStephen M. Cameron 		goto fail;
2237b705690dSStephen M. Cameron 
2238b705690dSStephen M. Cameron 	sh->io_port = 0;
2239b705690dSStephen M. Cameron 	sh->n_io_port = 0;
2240b705690dSStephen M. Cameron 	sh->this_id = -1;
2241b705690dSStephen M. Cameron 	sh->max_channel = 3;
2242b705690dSStephen M. Cameron 	sh->max_cmd_len = MAX_COMMAND_SIZE;
2243b705690dSStephen M. Cameron 	sh->max_lun = HPSA_MAX_LUN;
2244b705690dSStephen M. Cameron 	sh->max_id = HPSA_MAX_LUN;
2245b705690dSStephen M. Cameron 	sh->can_queue = h->nr_cmds;
2246b705690dSStephen M. Cameron 	sh->cmd_per_lun = h->nr_cmds;
2247b705690dSStephen M. Cameron 	sh->sg_tablesize = h->maxsgentries;
2248b705690dSStephen M. Cameron 	h->scsi_host = sh;
2249b705690dSStephen M. Cameron 	sh->hostdata[0] = (unsigned long) h;
2250b705690dSStephen M. Cameron 	sh->irq = h->intr[h->intr_mode];
2251b705690dSStephen M. Cameron 	sh->unique_id = sh->irq;
2252b705690dSStephen M. Cameron 	error = scsi_add_host(sh, &h->pdev->dev);
2253b705690dSStephen M. Cameron 	if (error)
2254b705690dSStephen M. Cameron 		goto fail_host_put;
2255b705690dSStephen M. Cameron 	scsi_scan_host(sh);
2256b705690dSStephen M. Cameron 	return 0;
2257b705690dSStephen M. Cameron 
2258b705690dSStephen M. Cameron  fail_host_put:
2259b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_add_host"
2260b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
2261b705690dSStephen M. Cameron 	scsi_host_put(sh);
2262b705690dSStephen M. Cameron 	return error;
2263b705690dSStephen M. Cameron  fail:
2264b705690dSStephen M. Cameron 	dev_err(&h->pdev->dev, "%s: scsi_host_alloc"
2265b705690dSStephen M. Cameron 		" failed for controller %d\n", __func__, h->ctlr);
2266b705690dSStephen M. Cameron 	return -ENOMEM;
2267edd16368SStephen M. Cameron }
2268edd16368SStephen M. Cameron 
2269edd16368SStephen M. Cameron static int wait_for_device_to_become_ready(struct ctlr_info *h,
2270edd16368SStephen M. Cameron 	unsigned char lunaddr[])
2271edd16368SStephen M. Cameron {
2272edd16368SStephen M. Cameron 	int rc = 0;
2273edd16368SStephen M. Cameron 	int count = 0;
2274edd16368SStephen M. Cameron 	int waittime = 1; /* seconds */
2275edd16368SStephen M. Cameron 	struct CommandList *c;
2276edd16368SStephen M. Cameron 
2277edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
2278edd16368SStephen M. Cameron 	if (!c) {
2279edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "out of memory in "
2280edd16368SStephen M. Cameron 			"wait_for_device_to_become_ready.\n");
2281edd16368SStephen M. Cameron 		return IO_ERROR;
2282edd16368SStephen M. Cameron 	}
2283edd16368SStephen M. Cameron 
2284edd16368SStephen M. Cameron 	/* Send test unit ready until device ready, or give up. */
2285edd16368SStephen M. Cameron 	while (count < HPSA_TUR_RETRY_LIMIT) {
2286edd16368SStephen M. Cameron 
2287edd16368SStephen M. Cameron 		/* Wait for a bit.  do this first, because if we send
2288edd16368SStephen M. Cameron 		 * the TUR right away, the reset will just abort it.
2289edd16368SStephen M. Cameron 		 */
2290edd16368SStephen M. Cameron 		msleep(1000 * waittime);
2291edd16368SStephen M. Cameron 		count++;
2292edd16368SStephen M. Cameron 
2293edd16368SStephen M. Cameron 		/* Increase wait time with each try, up to a point. */
2294edd16368SStephen M. Cameron 		if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
2295edd16368SStephen M. Cameron 			waittime = waittime * 2;
2296edd16368SStephen M. Cameron 
2297edd16368SStephen M. Cameron 		/* Send the Test Unit Ready */
2298edd16368SStephen M. Cameron 		fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, lunaddr, TYPE_CMD);
2299edd16368SStephen M. Cameron 		hpsa_scsi_do_simple_cmd_core(h, c);
2300edd16368SStephen M. Cameron 		/* no unmap needed here because no data xfer. */
2301edd16368SStephen M. Cameron 
2302edd16368SStephen M. Cameron 		if (c->err_info->CommandStatus == CMD_SUCCESS)
2303edd16368SStephen M. Cameron 			break;
2304edd16368SStephen M. Cameron 
2305edd16368SStephen M. Cameron 		if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
2306edd16368SStephen M. Cameron 			c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
2307edd16368SStephen M. Cameron 			(c->err_info->SenseInfo[2] == NO_SENSE ||
2308edd16368SStephen M. Cameron 			c->err_info->SenseInfo[2] == UNIT_ATTENTION))
2309edd16368SStephen M. Cameron 			break;
2310edd16368SStephen M. Cameron 
2311edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "waiting %d secs "
2312edd16368SStephen M. Cameron 			"for device to become ready.\n", waittime);
2313edd16368SStephen M. Cameron 		rc = 1; /* device not ready. */
2314edd16368SStephen M. Cameron 	}
2315edd16368SStephen M. Cameron 
2316edd16368SStephen M. Cameron 	if (rc)
2317edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "giving up on device.\n");
2318edd16368SStephen M. Cameron 	else
2319edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "device is ready.\n");
2320edd16368SStephen M. Cameron 
2321edd16368SStephen M. Cameron 	cmd_special_free(h, c);
2322edd16368SStephen M. Cameron 	return rc;
2323edd16368SStephen M. Cameron }
2324edd16368SStephen M. Cameron 
2325edd16368SStephen M. Cameron /* Need at least one of these error handlers to keep ../scsi/hosts.c from
2326edd16368SStephen M. Cameron  * complaining.  Doing a host- or bus-reset can't do anything good here.
2327edd16368SStephen M. Cameron  */
2328edd16368SStephen M. Cameron static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
2329edd16368SStephen M. Cameron {
2330edd16368SStephen M. Cameron 	int rc;
2331edd16368SStephen M. Cameron 	struct ctlr_info *h;
2332edd16368SStephen M. Cameron 	struct hpsa_scsi_dev_t *dev;
2333edd16368SStephen M. Cameron 
2334edd16368SStephen M. Cameron 	/* find the controller to which the command to be aborted was sent */
2335edd16368SStephen M. Cameron 	h = sdev_to_hba(scsicmd->device);
2336edd16368SStephen M. Cameron 	if (h == NULL) /* paranoia */
2337edd16368SStephen M. Cameron 		return FAILED;
2338edd16368SStephen M. Cameron 	dev = scsicmd->device->hostdata;
2339edd16368SStephen M. Cameron 	if (!dev) {
2340edd16368SStephen M. Cameron 		dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
2341edd16368SStephen M. Cameron 			"device lookup failed.\n");
2342edd16368SStephen M. Cameron 		return FAILED;
2343edd16368SStephen M. Cameron 	}
2344d416b0c7SStephen M. Cameron 	dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
2345d416b0c7SStephen M. Cameron 		h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
2346edd16368SStephen M. Cameron 	/* send a reset to the SCSI LUN which the command was sent to */
2347edd16368SStephen M. Cameron 	rc = hpsa_send_reset(h, dev->scsi3addr);
2348edd16368SStephen M. Cameron 	if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
2349edd16368SStephen M. Cameron 		return SUCCESS;
2350edd16368SStephen M. Cameron 
2351edd16368SStephen M. Cameron 	dev_warn(&h->pdev->dev, "resetting device failed.\n");
2352edd16368SStephen M. Cameron 	return FAILED;
2353edd16368SStephen M. Cameron }
2354edd16368SStephen M. Cameron 
2355edd16368SStephen M. Cameron /*
2356edd16368SStephen M. Cameron  * For operations that cannot sleep, a command block is allocated at init,
2357edd16368SStephen M. Cameron  * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
2358edd16368SStephen M. Cameron  * which ones are free or in use.  Lock must be held when calling this.
2359edd16368SStephen M. Cameron  * cmd_free() is the complement.
2360edd16368SStephen M. Cameron  */
2361edd16368SStephen M. Cameron static struct CommandList *cmd_alloc(struct ctlr_info *h)
2362edd16368SStephen M. Cameron {
2363edd16368SStephen M. Cameron 	struct CommandList *c;
2364edd16368SStephen M. Cameron 	int i;
2365edd16368SStephen M. Cameron 	union u64bit temp64;
2366edd16368SStephen M. Cameron 	dma_addr_t cmd_dma_handle, err_dma_handle;
2367edd16368SStephen M. Cameron 
2368edd16368SStephen M. Cameron 	do {
2369edd16368SStephen M. Cameron 		i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
2370edd16368SStephen M. Cameron 		if (i == h->nr_cmds)
2371edd16368SStephen M. Cameron 			return NULL;
2372edd16368SStephen M. Cameron 	} while (test_and_set_bit
2373edd16368SStephen M. Cameron 		 (i & (BITS_PER_LONG - 1),
2374edd16368SStephen M. Cameron 		  h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
2375edd16368SStephen M. Cameron 	c = h->cmd_pool + i;
2376edd16368SStephen M. Cameron 	memset(c, 0, sizeof(*c));
2377edd16368SStephen M. Cameron 	cmd_dma_handle = h->cmd_pool_dhandle
2378edd16368SStephen M. Cameron 	    + i * sizeof(*c);
2379edd16368SStephen M. Cameron 	c->err_info = h->errinfo_pool + i;
2380edd16368SStephen M. Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
2381edd16368SStephen M. Cameron 	err_dma_handle = h->errinfo_pool_dhandle
2382edd16368SStephen M. Cameron 	    + i * sizeof(*c->err_info);
2383edd16368SStephen M. Cameron 	h->nr_allocs++;
2384edd16368SStephen M. Cameron 
2385edd16368SStephen M. Cameron 	c->cmdindex = i;
2386edd16368SStephen M. Cameron 
23879e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&c->list);
238801a02ffcSStephen M. Cameron 	c->busaddr = (u32) cmd_dma_handle;
238901a02ffcSStephen M. Cameron 	temp64.val = (u64) err_dma_handle;
2390edd16368SStephen M. Cameron 	c->ErrDesc.Addr.lower = temp64.val32.lower;
2391edd16368SStephen M. Cameron 	c->ErrDesc.Addr.upper = temp64.val32.upper;
2392edd16368SStephen M. Cameron 	c->ErrDesc.Len = sizeof(*c->err_info);
2393edd16368SStephen M. Cameron 
2394edd16368SStephen M. Cameron 	c->h = h;
2395edd16368SStephen M. Cameron 	return c;
2396edd16368SStephen M. Cameron }
2397edd16368SStephen M. Cameron 
2398edd16368SStephen M. Cameron /* For operations that can wait for kmalloc to possibly sleep,
2399edd16368SStephen M. Cameron  * this routine can be called. Lock need not be held to call
2400edd16368SStephen M. Cameron  * cmd_special_alloc. cmd_special_free() is the complement.
2401edd16368SStephen M. Cameron  */
2402edd16368SStephen M. Cameron static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
2403edd16368SStephen M. Cameron {
2404edd16368SStephen M. Cameron 	struct CommandList *c;
2405edd16368SStephen M. Cameron 	union u64bit temp64;
2406edd16368SStephen M. Cameron 	dma_addr_t cmd_dma_handle, err_dma_handle;
2407edd16368SStephen M. Cameron 
2408edd16368SStephen M. Cameron 	c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
2409edd16368SStephen M. Cameron 	if (c == NULL)
2410edd16368SStephen M. Cameron 		return NULL;
2411edd16368SStephen M. Cameron 	memset(c, 0, sizeof(*c));
2412edd16368SStephen M. Cameron 
2413edd16368SStephen M. Cameron 	c->cmdindex = -1;
2414edd16368SStephen M. Cameron 
2415edd16368SStephen M. Cameron 	c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info),
2416edd16368SStephen M. Cameron 		    &err_dma_handle);
2417edd16368SStephen M. Cameron 
2418edd16368SStephen M. Cameron 	if (c->err_info == NULL) {
2419edd16368SStephen M. Cameron 		pci_free_consistent(h->pdev,
2420edd16368SStephen M. Cameron 			sizeof(*c), c, cmd_dma_handle);
2421edd16368SStephen M. Cameron 		return NULL;
2422edd16368SStephen M. Cameron 	}
2423edd16368SStephen M. Cameron 	memset(c->err_info, 0, sizeof(*c->err_info));
2424edd16368SStephen M. Cameron 
24259e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&c->list);
242601a02ffcSStephen M. Cameron 	c->busaddr = (u32) cmd_dma_handle;
242701a02ffcSStephen M. Cameron 	temp64.val = (u64) err_dma_handle;
2428edd16368SStephen M. Cameron 	c->ErrDesc.Addr.lower = temp64.val32.lower;
2429edd16368SStephen M. Cameron 	c->ErrDesc.Addr.upper = temp64.val32.upper;
2430edd16368SStephen M. Cameron 	c->ErrDesc.Len = sizeof(*c->err_info);
2431edd16368SStephen M. Cameron 
2432edd16368SStephen M. Cameron 	c->h = h;
2433edd16368SStephen M. Cameron 	return c;
2434edd16368SStephen M. Cameron }
2435edd16368SStephen M. Cameron 
2436edd16368SStephen M. Cameron static void cmd_free(struct ctlr_info *h, struct CommandList *c)
2437edd16368SStephen M. Cameron {
2438edd16368SStephen M. Cameron 	int i;
2439edd16368SStephen M. Cameron 
2440edd16368SStephen M. Cameron 	i = c - h->cmd_pool;
2441edd16368SStephen M. Cameron 	clear_bit(i & (BITS_PER_LONG - 1),
2442edd16368SStephen M. Cameron 		  h->cmd_pool_bits + (i / BITS_PER_LONG));
2443edd16368SStephen M. Cameron 	h->nr_frees++;
2444edd16368SStephen M. Cameron }
2445edd16368SStephen M. Cameron 
2446edd16368SStephen M. Cameron static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
2447edd16368SStephen M. Cameron {
2448edd16368SStephen M. Cameron 	union u64bit temp64;
2449edd16368SStephen M. Cameron 
2450edd16368SStephen M. Cameron 	temp64.val32.lower = c->ErrDesc.Addr.lower;
2451edd16368SStephen M. Cameron 	temp64.val32.upper = c->ErrDesc.Addr.upper;
2452edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev, sizeof(*c->err_info),
2453edd16368SStephen M. Cameron 			    c->err_info, (dma_addr_t) temp64.val);
2454edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev, sizeof(*c),
2455d896f3f3SStephen M. Cameron 			    c, (dma_addr_t) (c->busaddr & DIRECT_LOOKUP_MASK));
2456edd16368SStephen M. Cameron }
2457edd16368SStephen M. Cameron 
2458edd16368SStephen M. Cameron #ifdef CONFIG_COMPAT
2459edd16368SStephen M. Cameron 
2460edd16368SStephen M. Cameron static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
2461edd16368SStephen M. Cameron {
2462edd16368SStephen M. Cameron 	IOCTL32_Command_struct __user *arg32 =
2463edd16368SStephen M. Cameron 	    (IOCTL32_Command_struct __user *) arg;
2464edd16368SStephen M. Cameron 	IOCTL_Command_struct arg64;
2465edd16368SStephen M. Cameron 	IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
2466edd16368SStephen M. Cameron 	int err;
2467edd16368SStephen M. Cameron 	u32 cp;
2468edd16368SStephen M. Cameron 
2469938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
2470edd16368SStephen M. Cameron 	err = 0;
2471edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
2472edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
2473edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
2474edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
2475edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
2476edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
2477edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
2478edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
2479edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
2480edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
2481edd16368SStephen M. Cameron 
2482edd16368SStephen M. Cameron 	if (err)
2483edd16368SStephen M. Cameron 		return -EFAULT;
2484edd16368SStephen M. Cameron 
2485e39eeaedSStephen M. Cameron 	err = hpsa_ioctl(dev, CCISS_PASSTHRU, (void *)p);
2486edd16368SStephen M. Cameron 	if (err)
2487edd16368SStephen M. Cameron 		return err;
2488edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
2489edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
2490edd16368SStephen M. Cameron 	if (err)
2491edd16368SStephen M. Cameron 		return -EFAULT;
2492edd16368SStephen M. Cameron 	return err;
2493edd16368SStephen M. Cameron }
2494edd16368SStephen M. Cameron 
2495edd16368SStephen M. Cameron static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
2496edd16368SStephen M. Cameron 	int cmd, void *arg)
2497edd16368SStephen M. Cameron {
2498edd16368SStephen M. Cameron 	BIG_IOCTL32_Command_struct __user *arg32 =
2499edd16368SStephen M. Cameron 	    (BIG_IOCTL32_Command_struct __user *) arg;
2500edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct arg64;
2501edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct __user *p =
2502edd16368SStephen M. Cameron 	    compat_alloc_user_space(sizeof(arg64));
2503edd16368SStephen M. Cameron 	int err;
2504edd16368SStephen M. Cameron 	u32 cp;
2505edd16368SStephen M. Cameron 
2506938abd84SVasiliy Kulikov 	memset(&arg64, 0, sizeof(arg64));
2507edd16368SStephen M. Cameron 	err = 0;
2508edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
2509edd16368SStephen M. Cameron 			   sizeof(arg64.LUN_info));
2510edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.Request, &arg32->Request,
2511edd16368SStephen M. Cameron 			   sizeof(arg64.Request));
2512edd16368SStephen M. Cameron 	err |= copy_from_user(&arg64.error_info, &arg32->error_info,
2513edd16368SStephen M. Cameron 			   sizeof(arg64.error_info));
2514edd16368SStephen M. Cameron 	err |= get_user(arg64.buf_size, &arg32->buf_size);
2515edd16368SStephen M. Cameron 	err |= get_user(arg64.malloc_size, &arg32->malloc_size);
2516edd16368SStephen M. Cameron 	err |= get_user(cp, &arg32->buf);
2517edd16368SStephen M. Cameron 	arg64.buf = compat_ptr(cp);
2518edd16368SStephen M. Cameron 	err |= copy_to_user(p, &arg64, sizeof(arg64));
2519edd16368SStephen M. Cameron 
2520edd16368SStephen M. Cameron 	if (err)
2521edd16368SStephen M. Cameron 		return -EFAULT;
2522edd16368SStephen M. Cameron 
2523e39eeaedSStephen M. Cameron 	err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
2524edd16368SStephen M. Cameron 	if (err)
2525edd16368SStephen M. Cameron 		return err;
2526edd16368SStephen M. Cameron 	err |= copy_in_user(&arg32->error_info, &p->error_info,
2527edd16368SStephen M. Cameron 			 sizeof(arg32->error_info));
2528edd16368SStephen M. Cameron 	if (err)
2529edd16368SStephen M. Cameron 		return -EFAULT;
2530edd16368SStephen M. Cameron 	return err;
2531edd16368SStephen M. Cameron }
253271fe75a7SStephen M. Cameron 
253371fe75a7SStephen M. Cameron static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
253471fe75a7SStephen M. Cameron {
253571fe75a7SStephen M. Cameron 	switch (cmd) {
253671fe75a7SStephen M. Cameron 	case CCISS_GETPCIINFO:
253771fe75a7SStephen M. Cameron 	case CCISS_GETINTINFO:
253871fe75a7SStephen M. Cameron 	case CCISS_SETINTINFO:
253971fe75a7SStephen M. Cameron 	case CCISS_GETNODENAME:
254071fe75a7SStephen M. Cameron 	case CCISS_SETNODENAME:
254171fe75a7SStephen M. Cameron 	case CCISS_GETHEARTBEAT:
254271fe75a7SStephen M. Cameron 	case CCISS_GETBUSTYPES:
254371fe75a7SStephen M. Cameron 	case CCISS_GETFIRMVER:
254471fe75a7SStephen M. Cameron 	case CCISS_GETDRIVVER:
254571fe75a7SStephen M. Cameron 	case CCISS_REVALIDVOLS:
254671fe75a7SStephen M. Cameron 	case CCISS_DEREGDISK:
254771fe75a7SStephen M. Cameron 	case CCISS_REGNEWDISK:
254871fe75a7SStephen M. Cameron 	case CCISS_REGNEWD:
254971fe75a7SStephen M. Cameron 	case CCISS_RESCANDISK:
255071fe75a7SStephen M. Cameron 	case CCISS_GETLUNINFO:
255171fe75a7SStephen M. Cameron 		return hpsa_ioctl(dev, cmd, arg);
255271fe75a7SStephen M. Cameron 
255371fe75a7SStephen M. Cameron 	case CCISS_PASSTHRU32:
255471fe75a7SStephen M. Cameron 		return hpsa_ioctl32_passthru(dev, cmd, arg);
255571fe75a7SStephen M. Cameron 	case CCISS_BIG_PASSTHRU32:
255671fe75a7SStephen M. Cameron 		return hpsa_ioctl32_big_passthru(dev, cmd, arg);
255771fe75a7SStephen M. Cameron 
255871fe75a7SStephen M. Cameron 	default:
255971fe75a7SStephen M. Cameron 		return -ENOIOCTLCMD;
256071fe75a7SStephen M. Cameron 	}
256171fe75a7SStephen M. Cameron }
2562edd16368SStephen M. Cameron #endif
2563edd16368SStephen M. Cameron 
2564edd16368SStephen M. Cameron static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
2565edd16368SStephen M. Cameron {
2566edd16368SStephen M. Cameron 	struct hpsa_pci_info pciinfo;
2567edd16368SStephen M. Cameron 
2568edd16368SStephen M. Cameron 	if (!argp)
2569edd16368SStephen M. Cameron 		return -EINVAL;
2570edd16368SStephen M. Cameron 	pciinfo.domain = pci_domain_nr(h->pdev->bus);
2571edd16368SStephen M. Cameron 	pciinfo.bus = h->pdev->bus->number;
2572edd16368SStephen M. Cameron 	pciinfo.dev_fn = h->pdev->devfn;
2573edd16368SStephen M. Cameron 	pciinfo.board_id = h->board_id;
2574edd16368SStephen M. Cameron 	if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
2575edd16368SStephen M. Cameron 		return -EFAULT;
2576edd16368SStephen M. Cameron 	return 0;
2577edd16368SStephen M. Cameron }
2578edd16368SStephen M. Cameron 
2579edd16368SStephen M. Cameron static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
2580edd16368SStephen M. Cameron {
2581edd16368SStephen M. Cameron 	DriverVer_type DriverVer;
2582edd16368SStephen M. Cameron 	unsigned char vmaj, vmin, vsubmin;
2583edd16368SStephen M. Cameron 	int rc;
2584edd16368SStephen M. Cameron 
2585edd16368SStephen M. Cameron 	rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
2586edd16368SStephen M. Cameron 		&vmaj, &vmin, &vsubmin);
2587edd16368SStephen M. Cameron 	if (rc != 3) {
2588edd16368SStephen M. Cameron 		dev_info(&h->pdev->dev, "driver version string '%s' "
2589edd16368SStephen M. Cameron 			"unrecognized.", HPSA_DRIVER_VERSION);
2590edd16368SStephen M. Cameron 		vmaj = 0;
2591edd16368SStephen M. Cameron 		vmin = 0;
2592edd16368SStephen M. Cameron 		vsubmin = 0;
2593edd16368SStephen M. Cameron 	}
2594edd16368SStephen M. Cameron 	DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
2595edd16368SStephen M. Cameron 	if (!argp)
2596edd16368SStephen M. Cameron 		return -EINVAL;
2597edd16368SStephen M. Cameron 	if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
2598edd16368SStephen M. Cameron 		return -EFAULT;
2599edd16368SStephen M. Cameron 	return 0;
2600edd16368SStephen M. Cameron }
2601edd16368SStephen M. Cameron 
2602edd16368SStephen M. Cameron static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
2603edd16368SStephen M. Cameron {
2604edd16368SStephen M. Cameron 	IOCTL_Command_struct iocommand;
2605edd16368SStephen M. Cameron 	struct CommandList *c;
2606edd16368SStephen M. Cameron 	char *buff = NULL;
2607edd16368SStephen M. Cameron 	union u64bit temp64;
2608edd16368SStephen M. Cameron 
2609edd16368SStephen M. Cameron 	if (!argp)
2610edd16368SStephen M. Cameron 		return -EINVAL;
2611edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
2612edd16368SStephen M. Cameron 		return -EPERM;
2613edd16368SStephen M. Cameron 	if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
2614edd16368SStephen M. Cameron 		return -EFAULT;
2615edd16368SStephen M. Cameron 	if ((iocommand.buf_size < 1) &&
2616edd16368SStephen M. Cameron 	    (iocommand.Request.Type.Direction != XFER_NONE)) {
2617edd16368SStephen M. Cameron 		return -EINVAL;
2618edd16368SStephen M. Cameron 	}
2619edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
2620edd16368SStephen M. Cameron 		buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
2621edd16368SStephen M. Cameron 		if (buff == NULL)
2622edd16368SStephen M. Cameron 			return -EFAULT;
2623edd16368SStephen M. Cameron 		if (iocommand.Request.Type.Direction == XFER_WRITE) {
2624edd16368SStephen M. Cameron 			/* Copy the data into the buffer we created */
2625b03a7771SStephen M. Cameron 			if (copy_from_user(buff, iocommand.buf,
2626b03a7771SStephen M. Cameron 				iocommand.buf_size)) {
2627edd16368SStephen M. Cameron 				kfree(buff);
2628edd16368SStephen M. Cameron 				return -EFAULT;
2629edd16368SStephen M. Cameron 			}
2630b03a7771SStephen M. Cameron 		} else {
2631edd16368SStephen M. Cameron 			memset(buff, 0, iocommand.buf_size);
2632b03a7771SStephen M. Cameron 		}
2633b03a7771SStephen M. Cameron 	}
2634edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
2635edd16368SStephen M. Cameron 	if (c == NULL) {
2636edd16368SStephen M. Cameron 		kfree(buff);
2637edd16368SStephen M. Cameron 		return -ENOMEM;
2638edd16368SStephen M. Cameron 	}
2639edd16368SStephen M. Cameron 	/* Fill in the command type */
2640edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
2641edd16368SStephen M. Cameron 	/* Fill in Command Header */
2642edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0; /* unused in simple mode */
2643edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {	/* buffer to fill */
2644edd16368SStephen M. Cameron 		c->Header.SGList = 1;
2645edd16368SStephen M. Cameron 		c->Header.SGTotal = 1;
2646edd16368SStephen M. Cameron 	} else	{ /* no buffers to fill */
2647edd16368SStephen M. Cameron 		c->Header.SGList = 0;
2648edd16368SStephen M. Cameron 		c->Header.SGTotal = 0;
2649edd16368SStephen M. Cameron 	}
2650edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
2651edd16368SStephen M. Cameron 	/* use the kernel address the cmd block for tag */
2652edd16368SStephen M. Cameron 	c->Header.Tag.lower = c->busaddr;
2653edd16368SStephen M. Cameron 
2654edd16368SStephen M. Cameron 	/* Fill in Request block */
2655edd16368SStephen M. Cameron 	memcpy(&c->Request, &iocommand.Request,
2656edd16368SStephen M. Cameron 		sizeof(c->Request));
2657edd16368SStephen M. Cameron 
2658edd16368SStephen M. Cameron 	/* Fill in the scatter gather information */
2659edd16368SStephen M. Cameron 	if (iocommand.buf_size > 0) {
2660edd16368SStephen M. Cameron 		temp64.val = pci_map_single(h->pdev, buff,
2661edd16368SStephen M. Cameron 			iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
2662edd16368SStephen M. Cameron 		c->SG[0].Addr.lower = temp64.val32.lower;
2663edd16368SStephen M. Cameron 		c->SG[0].Addr.upper = temp64.val32.upper;
2664edd16368SStephen M. Cameron 		c->SG[0].Len = iocommand.buf_size;
2665edd16368SStephen M. Cameron 		c->SG[0].Ext = 0; /* we are not chaining*/
2666edd16368SStephen M. Cameron 	}
2667a0c12413SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
2668c2dd32e0SStephen M. Cameron 	if (iocommand.buf_size > 0)
2669edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
2670edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
2671edd16368SStephen M. Cameron 
2672edd16368SStephen M. Cameron 	/* Copy the error information out */
2673edd16368SStephen M. Cameron 	memcpy(&iocommand.error_info, c->err_info,
2674edd16368SStephen M. Cameron 		sizeof(iocommand.error_info));
2675edd16368SStephen M. Cameron 	if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
2676edd16368SStephen M. Cameron 		kfree(buff);
2677edd16368SStephen M. Cameron 		cmd_special_free(h, c);
2678edd16368SStephen M. Cameron 		return -EFAULT;
2679edd16368SStephen M. Cameron 	}
2680b03a7771SStephen M. Cameron 	if (iocommand.Request.Type.Direction == XFER_READ &&
2681b03a7771SStephen M. Cameron 		iocommand.buf_size > 0) {
2682edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
2683edd16368SStephen M. Cameron 		if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
2684edd16368SStephen M. Cameron 			kfree(buff);
2685edd16368SStephen M. Cameron 			cmd_special_free(h, c);
2686edd16368SStephen M. Cameron 			return -EFAULT;
2687edd16368SStephen M. Cameron 		}
2688edd16368SStephen M. Cameron 	}
2689edd16368SStephen M. Cameron 	kfree(buff);
2690edd16368SStephen M. Cameron 	cmd_special_free(h, c);
2691edd16368SStephen M. Cameron 	return 0;
2692edd16368SStephen M. Cameron }
2693edd16368SStephen M. Cameron 
2694edd16368SStephen M. Cameron static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
2695edd16368SStephen M. Cameron {
2696edd16368SStephen M. Cameron 	BIG_IOCTL_Command_struct *ioc;
2697edd16368SStephen M. Cameron 	struct CommandList *c;
2698edd16368SStephen M. Cameron 	unsigned char **buff = NULL;
2699edd16368SStephen M. Cameron 	int *buff_size = NULL;
2700edd16368SStephen M. Cameron 	union u64bit temp64;
2701edd16368SStephen M. Cameron 	BYTE sg_used = 0;
2702edd16368SStephen M. Cameron 	int status = 0;
2703edd16368SStephen M. Cameron 	int i;
270401a02ffcSStephen M. Cameron 	u32 left;
270501a02ffcSStephen M. Cameron 	u32 sz;
2706edd16368SStephen M. Cameron 	BYTE __user *data_ptr;
2707edd16368SStephen M. Cameron 
2708edd16368SStephen M. Cameron 	if (!argp)
2709edd16368SStephen M. Cameron 		return -EINVAL;
2710edd16368SStephen M. Cameron 	if (!capable(CAP_SYS_RAWIO))
2711edd16368SStephen M. Cameron 		return -EPERM;
2712edd16368SStephen M. Cameron 	ioc = (BIG_IOCTL_Command_struct *)
2713edd16368SStephen M. Cameron 	    kmalloc(sizeof(*ioc), GFP_KERNEL);
2714edd16368SStephen M. Cameron 	if (!ioc) {
2715edd16368SStephen M. Cameron 		status = -ENOMEM;
2716edd16368SStephen M. Cameron 		goto cleanup1;
2717edd16368SStephen M. Cameron 	}
2718edd16368SStephen M. Cameron 	if (copy_from_user(ioc, argp, sizeof(*ioc))) {
2719edd16368SStephen M. Cameron 		status = -EFAULT;
2720edd16368SStephen M. Cameron 		goto cleanup1;
2721edd16368SStephen M. Cameron 	}
2722edd16368SStephen M. Cameron 	if ((ioc->buf_size < 1) &&
2723edd16368SStephen M. Cameron 	    (ioc->Request.Type.Direction != XFER_NONE)) {
2724edd16368SStephen M. Cameron 		status = -EINVAL;
2725edd16368SStephen M. Cameron 		goto cleanup1;
2726edd16368SStephen M. Cameron 	}
2727edd16368SStephen M. Cameron 	/* Check kmalloc limits  using all SGs */
2728edd16368SStephen M. Cameron 	if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
2729edd16368SStephen M. Cameron 		status = -EINVAL;
2730edd16368SStephen M. Cameron 		goto cleanup1;
2731edd16368SStephen M. Cameron 	}
2732d66ae08bSStephen M. Cameron 	if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
2733edd16368SStephen M. Cameron 		status = -EINVAL;
2734edd16368SStephen M. Cameron 		goto cleanup1;
2735edd16368SStephen M. Cameron 	}
2736d66ae08bSStephen M. Cameron 	buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
2737edd16368SStephen M. Cameron 	if (!buff) {
2738edd16368SStephen M. Cameron 		status = -ENOMEM;
2739edd16368SStephen M. Cameron 		goto cleanup1;
2740edd16368SStephen M. Cameron 	}
2741d66ae08bSStephen M. Cameron 	buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
2742edd16368SStephen M. Cameron 	if (!buff_size) {
2743edd16368SStephen M. Cameron 		status = -ENOMEM;
2744edd16368SStephen M. Cameron 		goto cleanup1;
2745edd16368SStephen M. Cameron 	}
2746edd16368SStephen M. Cameron 	left = ioc->buf_size;
2747edd16368SStephen M. Cameron 	data_ptr = ioc->buf;
2748edd16368SStephen M. Cameron 	while (left) {
2749edd16368SStephen M. Cameron 		sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
2750edd16368SStephen M. Cameron 		buff_size[sg_used] = sz;
2751edd16368SStephen M. Cameron 		buff[sg_used] = kmalloc(sz, GFP_KERNEL);
2752edd16368SStephen M. Cameron 		if (buff[sg_used] == NULL) {
2753edd16368SStephen M. Cameron 			status = -ENOMEM;
2754edd16368SStephen M. Cameron 			goto cleanup1;
2755edd16368SStephen M. Cameron 		}
2756edd16368SStephen M. Cameron 		if (ioc->Request.Type.Direction == XFER_WRITE) {
2757edd16368SStephen M. Cameron 			if (copy_from_user(buff[sg_used], data_ptr, sz)) {
2758edd16368SStephen M. Cameron 				status = -ENOMEM;
2759edd16368SStephen M. Cameron 				goto cleanup1;
2760edd16368SStephen M. Cameron 			}
2761edd16368SStephen M. Cameron 		} else
2762edd16368SStephen M. Cameron 			memset(buff[sg_used], 0, sz);
2763edd16368SStephen M. Cameron 		left -= sz;
2764edd16368SStephen M. Cameron 		data_ptr += sz;
2765edd16368SStephen M. Cameron 		sg_used++;
2766edd16368SStephen M. Cameron 	}
2767edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
2768edd16368SStephen M. Cameron 	if (c == NULL) {
2769edd16368SStephen M. Cameron 		status = -ENOMEM;
2770edd16368SStephen M. Cameron 		goto cleanup1;
2771edd16368SStephen M. Cameron 	}
2772edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
2773edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
2774b03a7771SStephen M. Cameron 	c->Header.SGList = c->Header.SGTotal = sg_used;
2775edd16368SStephen M. Cameron 	memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
2776edd16368SStephen M. Cameron 	c->Header.Tag.lower = c->busaddr;
2777edd16368SStephen M. Cameron 	memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
2778edd16368SStephen M. Cameron 	if (ioc->buf_size > 0) {
2779edd16368SStephen M. Cameron 		int i;
2780edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
2781edd16368SStephen M. Cameron 			temp64.val = pci_map_single(h->pdev, buff[i],
2782edd16368SStephen M. Cameron 				    buff_size[i], PCI_DMA_BIDIRECTIONAL);
2783edd16368SStephen M. Cameron 			c->SG[i].Addr.lower = temp64.val32.lower;
2784edd16368SStephen M. Cameron 			c->SG[i].Addr.upper = temp64.val32.upper;
2785edd16368SStephen M. Cameron 			c->SG[i].Len = buff_size[i];
2786edd16368SStephen M. Cameron 			/* we are not chaining */
2787edd16368SStephen M. Cameron 			c->SG[i].Ext = 0;
2788edd16368SStephen M. Cameron 		}
2789edd16368SStephen M. Cameron 	}
2790a0c12413SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
2791b03a7771SStephen M. Cameron 	if (sg_used)
2792edd16368SStephen M. Cameron 		hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
2793edd16368SStephen M. Cameron 	check_ioctl_unit_attention(h, c);
2794edd16368SStephen M. Cameron 	/* Copy the error information out */
2795edd16368SStephen M. Cameron 	memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
2796edd16368SStephen M. Cameron 	if (copy_to_user(argp, ioc, sizeof(*ioc))) {
2797edd16368SStephen M. Cameron 		cmd_special_free(h, c);
2798edd16368SStephen M. Cameron 		status = -EFAULT;
2799edd16368SStephen M. Cameron 		goto cleanup1;
2800edd16368SStephen M. Cameron 	}
2801b03a7771SStephen M. Cameron 	if (ioc->Request.Type.Direction == XFER_READ && ioc->buf_size > 0) {
2802edd16368SStephen M. Cameron 		/* Copy the data out of the buffer we created */
2803edd16368SStephen M. Cameron 		BYTE __user *ptr = ioc->buf;
2804edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++) {
2805edd16368SStephen M. Cameron 			if (copy_to_user(ptr, buff[i], buff_size[i])) {
2806edd16368SStephen M. Cameron 				cmd_special_free(h, c);
2807edd16368SStephen M. Cameron 				status = -EFAULT;
2808edd16368SStephen M. Cameron 				goto cleanup1;
2809edd16368SStephen M. Cameron 			}
2810edd16368SStephen M. Cameron 			ptr += buff_size[i];
2811edd16368SStephen M. Cameron 		}
2812edd16368SStephen M. Cameron 	}
2813edd16368SStephen M. Cameron 	cmd_special_free(h, c);
2814edd16368SStephen M. Cameron 	status = 0;
2815edd16368SStephen M. Cameron cleanup1:
2816edd16368SStephen M. Cameron 	if (buff) {
2817edd16368SStephen M. Cameron 		for (i = 0; i < sg_used; i++)
2818edd16368SStephen M. Cameron 			kfree(buff[i]);
2819edd16368SStephen M. Cameron 		kfree(buff);
2820edd16368SStephen M. Cameron 	}
2821edd16368SStephen M. Cameron 	kfree(buff_size);
2822edd16368SStephen M. Cameron 	kfree(ioc);
2823edd16368SStephen M. Cameron 	return status;
2824edd16368SStephen M. Cameron }
2825edd16368SStephen M. Cameron 
2826edd16368SStephen M. Cameron static void check_ioctl_unit_attention(struct ctlr_info *h,
2827edd16368SStephen M. Cameron 	struct CommandList *c)
2828edd16368SStephen M. Cameron {
2829edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
2830edd16368SStephen M. Cameron 			c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
2831edd16368SStephen M. Cameron 		(void) check_for_unit_attention(h, c);
2832edd16368SStephen M. Cameron }
2833edd16368SStephen M. Cameron /*
2834edd16368SStephen M. Cameron  * ioctl
2835edd16368SStephen M. Cameron  */
2836edd16368SStephen M. Cameron static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
2837edd16368SStephen M. Cameron {
2838edd16368SStephen M. Cameron 	struct ctlr_info *h;
2839edd16368SStephen M. Cameron 	void __user *argp = (void __user *)arg;
2840edd16368SStephen M. Cameron 
2841edd16368SStephen M. Cameron 	h = sdev_to_hba(dev);
2842edd16368SStephen M. Cameron 
2843edd16368SStephen M. Cameron 	switch (cmd) {
2844edd16368SStephen M. Cameron 	case CCISS_DEREGDISK:
2845edd16368SStephen M. Cameron 	case CCISS_REGNEWDISK:
2846edd16368SStephen M. Cameron 	case CCISS_REGNEWD:
2847a08a8471SStephen M. Cameron 		hpsa_scan_start(h->scsi_host);
2848edd16368SStephen M. Cameron 		return 0;
2849edd16368SStephen M. Cameron 	case CCISS_GETPCIINFO:
2850edd16368SStephen M. Cameron 		return hpsa_getpciinfo_ioctl(h, argp);
2851edd16368SStephen M. Cameron 	case CCISS_GETDRIVVER:
2852edd16368SStephen M. Cameron 		return hpsa_getdrivver_ioctl(h, argp);
2853edd16368SStephen M. Cameron 	case CCISS_PASSTHRU:
2854edd16368SStephen M. Cameron 		return hpsa_passthru_ioctl(h, argp);
2855edd16368SStephen M. Cameron 	case CCISS_BIG_PASSTHRU:
2856edd16368SStephen M. Cameron 		return hpsa_big_passthru_ioctl(h, argp);
2857edd16368SStephen M. Cameron 	default:
2858edd16368SStephen M. Cameron 		return -ENOTTY;
2859edd16368SStephen M. Cameron 	}
2860edd16368SStephen M. Cameron }
2861edd16368SStephen M. Cameron 
286264670ac8SStephen M. Cameron static int __devinit hpsa_send_host_reset(struct ctlr_info *h,
286364670ac8SStephen M. Cameron 	unsigned char *scsi3addr, u8 reset_type)
286464670ac8SStephen M. Cameron {
286564670ac8SStephen M. Cameron 	struct CommandList *c;
286664670ac8SStephen M. Cameron 
286764670ac8SStephen M. Cameron 	c = cmd_alloc(h);
286864670ac8SStephen M. Cameron 	if (!c)
286964670ac8SStephen M. Cameron 		return -ENOMEM;
287064670ac8SStephen M. Cameron 	fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
287164670ac8SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_MSG);
287264670ac8SStephen M. Cameron 	c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
287364670ac8SStephen M. Cameron 	c->waiting = NULL;
287464670ac8SStephen M. Cameron 	enqueue_cmd_and_start_io(h, c);
287564670ac8SStephen M. Cameron 	/* Don't wait for completion, the reset won't complete.  Don't free
287664670ac8SStephen M. Cameron 	 * the command either.  This is the last command we will send before
287764670ac8SStephen M. Cameron 	 * re-initializing everything, so it doesn't matter and won't leak.
287864670ac8SStephen M. Cameron 	 */
287964670ac8SStephen M. Cameron 	return 0;
288064670ac8SStephen M. Cameron }
288164670ac8SStephen M. Cameron 
288201a02ffcSStephen M. Cameron static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
288301a02ffcSStephen M. Cameron 	void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
2884edd16368SStephen M. Cameron 	int cmd_type)
2885edd16368SStephen M. Cameron {
2886edd16368SStephen M. Cameron 	int pci_dir = XFER_NONE;
2887edd16368SStephen M. Cameron 
2888edd16368SStephen M. Cameron 	c->cmd_type = CMD_IOCTL_PEND;
2889edd16368SStephen M. Cameron 	c->Header.ReplyQueue = 0;
2890edd16368SStephen M. Cameron 	if (buff != NULL && size > 0) {
2891edd16368SStephen M. Cameron 		c->Header.SGList = 1;
2892edd16368SStephen M. Cameron 		c->Header.SGTotal = 1;
2893edd16368SStephen M. Cameron 	} else {
2894edd16368SStephen M. Cameron 		c->Header.SGList = 0;
2895edd16368SStephen M. Cameron 		c->Header.SGTotal = 0;
2896edd16368SStephen M. Cameron 	}
2897edd16368SStephen M. Cameron 	c->Header.Tag.lower = c->busaddr;
2898edd16368SStephen M. Cameron 	memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
2899edd16368SStephen M. Cameron 
2900edd16368SStephen M. Cameron 	c->Request.Type.Type = cmd_type;
2901edd16368SStephen M. Cameron 	if (cmd_type == TYPE_CMD) {
2902edd16368SStephen M. Cameron 		switch (cmd) {
2903edd16368SStephen M. Cameron 		case HPSA_INQUIRY:
2904edd16368SStephen M. Cameron 			/* are we trying to read a vital product page */
2905edd16368SStephen M. Cameron 			if (page_code != 0) {
2906edd16368SStephen M. Cameron 				c->Request.CDB[1] = 0x01;
2907edd16368SStephen M. Cameron 				c->Request.CDB[2] = page_code;
2908edd16368SStephen M. Cameron 			}
2909edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
2910edd16368SStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
2911edd16368SStephen M. Cameron 			c->Request.Type.Direction = XFER_READ;
2912edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
2913edd16368SStephen M. Cameron 			c->Request.CDB[0] = HPSA_INQUIRY;
2914edd16368SStephen M. Cameron 			c->Request.CDB[4] = size & 0xFF;
2915edd16368SStephen M. Cameron 			break;
2916edd16368SStephen M. Cameron 		case HPSA_REPORT_LOG:
2917edd16368SStephen M. Cameron 		case HPSA_REPORT_PHYS:
2918edd16368SStephen M. Cameron 			/* Talking to controller so It's a physical command
2919edd16368SStephen M. Cameron 			   mode = 00 target = 0.  Nothing to write.
2920edd16368SStephen M. Cameron 			 */
2921edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
2922edd16368SStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
2923edd16368SStephen M. Cameron 			c->Request.Type.Direction = XFER_READ;
2924edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
2925edd16368SStephen M. Cameron 			c->Request.CDB[0] = cmd;
2926edd16368SStephen M. Cameron 			c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
2927edd16368SStephen M. Cameron 			c->Request.CDB[7] = (size >> 16) & 0xFF;
2928edd16368SStephen M. Cameron 			c->Request.CDB[8] = (size >> 8) & 0xFF;
2929edd16368SStephen M. Cameron 			c->Request.CDB[9] = size & 0xFF;
2930edd16368SStephen M. Cameron 			break;
2931edd16368SStephen M. Cameron 		case HPSA_CACHE_FLUSH:
2932edd16368SStephen M. Cameron 			c->Request.CDBLen = 12;
2933edd16368SStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
2934edd16368SStephen M. Cameron 			c->Request.Type.Direction = XFER_WRITE;
2935edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
2936edd16368SStephen M. Cameron 			c->Request.CDB[0] = BMIC_WRITE;
2937edd16368SStephen M. Cameron 			c->Request.CDB[6] = BMIC_CACHE_FLUSH;
2938bb158eabSStephen M. Cameron 			c->Request.CDB[7] = (size >> 8) & 0xFF;
2939bb158eabSStephen M. Cameron 			c->Request.CDB[8] = size & 0xFF;
2940edd16368SStephen M. Cameron 			break;
2941edd16368SStephen M. Cameron 		case TEST_UNIT_READY:
2942edd16368SStephen M. Cameron 			c->Request.CDBLen = 6;
2943edd16368SStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
2944edd16368SStephen M. Cameron 			c->Request.Type.Direction = XFER_NONE;
2945edd16368SStephen M. Cameron 			c->Request.Timeout = 0;
2946edd16368SStephen M. Cameron 			break;
2947edd16368SStephen M. Cameron 		default:
2948edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
2949edd16368SStephen M. Cameron 			BUG();
2950edd16368SStephen M. Cameron 			return;
2951edd16368SStephen M. Cameron 		}
2952edd16368SStephen M. Cameron 	} else if (cmd_type == TYPE_MSG) {
2953edd16368SStephen M. Cameron 		switch (cmd) {
2954edd16368SStephen M. Cameron 
2955edd16368SStephen M. Cameron 		case  HPSA_DEVICE_RESET_MSG:
2956edd16368SStephen M. Cameron 			c->Request.CDBLen = 16;
2957edd16368SStephen M. Cameron 			c->Request.Type.Type =  1; /* It is a MSG not a CMD */
2958edd16368SStephen M. Cameron 			c->Request.Type.Attribute = ATTR_SIMPLE;
2959edd16368SStephen M. Cameron 			c->Request.Type.Direction = XFER_NONE;
2960edd16368SStephen M. Cameron 			c->Request.Timeout = 0; /* Don't time out */
296164670ac8SStephen M. Cameron 			memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
296264670ac8SStephen M. Cameron 			c->Request.CDB[0] =  cmd;
2963edd16368SStephen M. Cameron 			c->Request.CDB[1] = 0x03;  /* Reset target above */
2964edd16368SStephen M. Cameron 			/* If bytes 4-7 are zero, it means reset the */
2965edd16368SStephen M. Cameron 			/* LunID device */
2966edd16368SStephen M. Cameron 			c->Request.CDB[4] = 0x00;
2967edd16368SStephen M. Cameron 			c->Request.CDB[5] = 0x00;
2968edd16368SStephen M. Cameron 			c->Request.CDB[6] = 0x00;
2969edd16368SStephen M. Cameron 			c->Request.CDB[7] = 0x00;
2970edd16368SStephen M. Cameron 		break;
2971edd16368SStephen M. Cameron 
2972edd16368SStephen M. Cameron 		default:
2973edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "unknown message type %d\n",
2974edd16368SStephen M. Cameron 				cmd);
2975edd16368SStephen M. Cameron 			BUG();
2976edd16368SStephen M. Cameron 		}
2977edd16368SStephen M. Cameron 	} else {
2978edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
2979edd16368SStephen M. Cameron 		BUG();
2980edd16368SStephen M. Cameron 	}
2981edd16368SStephen M. Cameron 
2982edd16368SStephen M. Cameron 	switch (c->Request.Type.Direction) {
2983edd16368SStephen M. Cameron 	case XFER_READ:
2984edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_FROMDEVICE;
2985edd16368SStephen M. Cameron 		break;
2986edd16368SStephen M. Cameron 	case XFER_WRITE:
2987edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_TODEVICE;
2988edd16368SStephen M. Cameron 		break;
2989edd16368SStephen M. Cameron 	case XFER_NONE:
2990edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_NONE;
2991edd16368SStephen M. Cameron 		break;
2992edd16368SStephen M. Cameron 	default:
2993edd16368SStephen M. Cameron 		pci_dir = PCI_DMA_BIDIRECTIONAL;
2994edd16368SStephen M. Cameron 	}
2995edd16368SStephen M. Cameron 
2996edd16368SStephen M. Cameron 	hpsa_map_one(h->pdev, c, buff, size, pci_dir);
2997edd16368SStephen M. Cameron 
2998edd16368SStephen M. Cameron 	return;
2999edd16368SStephen M. Cameron }
3000edd16368SStephen M. Cameron 
3001edd16368SStephen M. Cameron /*
3002edd16368SStephen M. Cameron  * Map (physical) PCI mem into (virtual) kernel space
3003edd16368SStephen M. Cameron  */
3004edd16368SStephen M. Cameron static void __iomem *remap_pci_mem(ulong base, ulong size)
3005edd16368SStephen M. Cameron {
3006edd16368SStephen M. Cameron 	ulong page_base = ((ulong) base) & PAGE_MASK;
3007edd16368SStephen M. Cameron 	ulong page_offs = ((ulong) base) - page_base;
3008edd16368SStephen M. Cameron 	void __iomem *page_remapped = ioremap(page_base, page_offs + size);
3009edd16368SStephen M. Cameron 
3010edd16368SStephen M. Cameron 	return page_remapped ? (page_remapped + page_offs) : NULL;
3011edd16368SStephen M. Cameron }
3012edd16368SStephen M. Cameron 
3013edd16368SStephen M. Cameron /* Takes cmds off the submission queue and sends them to the hardware,
3014edd16368SStephen M. Cameron  * then puts them on the queue of cmds waiting for completion.
3015edd16368SStephen M. Cameron  */
3016edd16368SStephen M. Cameron static void start_io(struct ctlr_info *h)
3017edd16368SStephen M. Cameron {
3018edd16368SStephen M. Cameron 	struct CommandList *c;
3019edd16368SStephen M. Cameron 
30209e0fc764SStephen M. Cameron 	while (!list_empty(&h->reqQ)) {
30219e0fc764SStephen M. Cameron 		c = list_entry(h->reqQ.next, struct CommandList, list);
3022edd16368SStephen M. Cameron 		/* can't do anything if fifo is full */
3023edd16368SStephen M. Cameron 		if ((h->access.fifo_full(h))) {
3024edd16368SStephen M. Cameron 			dev_warn(&h->pdev->dev, "fifo full\n");
3025edd16368SStephen M. Cameron 			break;
3026edd16368SStephen M. Cameron 		}
3027edd16368SStephen M. Cameron 
3028edd16368SStephen M. Cameron 		/* Get the first entry from the Request Q */
3029edd16368SStephen M. Cameron 		removeQ(c);
3030edd16368SStephen M. Cameron 		h->Qdepth--;
3031edd16368SStephen M. Cameron 
3032edd16368SStephen M. Cameron 		/* Tell the controller execute command */
3033edd16368SStephen M. Cameron 		h->access.submit_command(h, c);
3034edd16368SStephen M. Cameron 
3035edd16368SStephen M. Cameron 		/* Put job onto the completed Q */
3036edd16368SStephen M. Cameron 		addQ(&h->cmpQ, c);
3037edd16368SStephen M. Cameron 	}
3038edd16368SStephen M. Cameron }
3039edd16368SStephen M. Cameron 
3040edd16368SStephen M. Cameron static inline unsigned long get_next_completion(struct ctlr_info *h)
3041edd16368SStephen M. Cameron {
3042edd16368SStephen M. Cameron 	return h->access.command_completed(h);
3043edd16368SStephen M. Cameron }
3044edd16368SStephen M. Cameron 
3045900c5440SStephen M. Cameron static inline bool interrupt_pending(struct ctlr_info *h)
3046edd16368SStephen M. Cameron {
3047edd16368SStephen M. Cameron 	return h->access.intr_pending(h);
3048edd16368SStephen M. Cameron }
3049edd16368SStephen M. Cameron 
3050edd16368SStephen M. Cameron static inline long interrupt_not_for_us(struct ctlr_info *h)
3051edd16368SStephen M. Cameron {
305210f66018SStephen M. Cameron 	return (h->access.intr_pending(h) == 0) ||
305310f66018SStephen M. Cameron 		(h->interrupts_enabled == 0);
3054edd16368SStephen M. Cameron }
3055edd16368SStephen M. Cameron 
305601a02ffcSStephen M. Cameron static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
305701a02ffcSStephen M. Cameron 	u32 raw_tag)
3058edd16368SStephen M. Cameron {
3059edd16368SStephen M. Cameron 	if (unlikely(tag_index >= h->nr_cmds)) {
3060edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
3061edd16368SStephen M. Cameron 		return 1;
3062edd16368SStephen M. Cameron 	}
3063edd16368SStephen M. Cameron 	return 0;
3064edd16368SStephen M. Cameron }
3065edd16368SStephen M. Cameron 
306601a02ffcSStephen M. Cameron static inline void finish_cmd(struct CommandList *c, u32 raw_tag)
3067edd16368SStephen M. Cameron {
3068edd16368SStephen M. Cameron 	removeQ(c);
3069edd16368SStephen M. Cameron 	if (likely(c->cmd_type == CMD_SCSI))
30701fb011fbSStephen M. Cameron 		complete_scsi_command(c);
3071edd16368SStephen M. Cameron 	else if (c->cmd_type == CMD_IOCTL_PEND)
3072edd16368SStephen M. Cameron 		complete(c->waiting);
3073edd16368SStephen M. Cameron }
3074edd16368SStephen M. Cameron 
3075a104c99fSStephen M. Cameron static inline u32 hpsa_tag_contains_index(u32 tag)
3076a104c99fSStephen M. Cameron {
3077a104c99fSStephen M. Cameron 	return tag & DIRECT_LOOKUP_BIT;
3078a104c99fSStephen M. Cameron }
3079a104c99fSStephen M. Cameron 
3080a104c99fSStephen M. Cameron static inline u32 hpsa_tag_to_index(u32 tag)
3081a104c99fSStephen M. Cameron {
3082a104c99fSStephen M. Cameron 	return tag >> DIRECT_LOOKUP_SHIFT;
3083a104c99fSStephen M. Cameron }
3084a104c99fSStephen M. Cameron 
3085a9a3a273SStephen M. Cameron 
3086a9a3a273SStephen M. Cameron static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
3087a104c99fSStephen M. Cameron {
3088a9a3a273SStephen M. Cameron #define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
3089a9a3a273SStephen M. Cameron #define HPSA_SIMPLE_ERROR_BITS 0x03
3090960a30e7SStephen M. Cameron 	if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
3091a9a3a273SStephen M. Cameron 		return tag & ~HPSA_SIMPLE_ERROR_BITS;
3092a9a3a273SStephen M. Cameron 	return tag & ~HPSA_PERF_ERROR_BITS;
3093a104c99fSStephen M. Cameron }
3094a104c99fSStephen M. Cameron 
3095303932fdSDon Brace /* process completion of an indexed ("direct lookup") command */
3096303932fdSDon Brace static inline u32 process_indexed_cmd(struct ctlr_info *h,
3097303932fdSDon Brace 	u32 raw_tag)
3098303932fdSDon Brace {
3099303932fdSDon Brace 	u32 tag_index;
3100303932fdSDon Brace 	struct CommandList *c;
3101303932fdSDon Brace 
3102303932fdSDon Brace 	tag_index = hpsa_tag_to_index(raw_tag);
3103303932fdSDon Brace 	if (bad_tag(h, tag_index, raw_tag))
3104303932fdSDon Brace 		return next_command(h);
3105303932fdSDon Brace 	c = h->cmd_pool + tag_index;
3106303932fdSDon Brace 	finish_cmd(c, raw_tag);
3107303932fdSDon Brace 	return next_command(h);
3108303932fdSDon Brace }
3109303932fdSDon Brace 
3110303932fdSDon Brace /* process completion of a non-indexed command */
3111303932fdSDon Brace static inline u32 process_nonindexed_cmd(struct ctlr_info *h,
3112303932fdSDon Brace 	u32 raw_tag)
3113303932fdSDon Brace {
3114303932fdSDon Brace 	u32 tag;
3115303932fdSDon Brace 	struct CommandList *c = NULL;
3116303932fdSDon Brace 
3117a9a3a273SStephen M. Cameron 	tag = hpsa_tag_discard_error_bits(h, raw_tag);
31189e0fc764SStephen M. Cameron 	list_for_each_entry(c, &h->cmpQ, list) {
3119303932fdSDon Brace 		if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
3120303932fdSDon Brace 			finish_cmd(c, raw_tag);
3121303932fdSDon Brace 			return next_command(h);
3122303932fdSDon Brace 		}
3123303932fdSDon Brace 	}
3124303932fdSDon Brace 	bad_tag(h, h->nr_cmds + 1, raw_tag);
3125303932fdSDon Brace 	return next_command(h);
3126303932fdSDon Brace }
3127303932fdSDon Brace 
312864670ac8SStephen M. Cameron /* Some controllers, like p400, will give us one interrupt
312964670ac8SStephen M. Cameron  * after a soft reset, even if we turned interrupts off.
313064670ac8SStephen M. Cameron  * Only need to check for this in the hpsa_xxx_discard_completions
313164670ac8SStephen M. Cameron  * functions.
313264670ac8SStephen M. Cameron  */
313364670ac8SStephen M. Cameron static int ignore_bogus_interrupt(struct ctlr_info *h)
313464670ac8SStephen M. Cameron {
313564670ac8SStephen M. Cameron 	if (likely(!reset_devices))
313664670ac8SStephen M. Cameron 		return 0;
313764670ac8SStephen M. Cameron 
313864670ac8SStephen M. Cameron 	if (likely(h->interrupts_enabled))
313964670ac8SStephen M. Cameron 		return 0;
314064670ac8SStephen M. Cameron 
314164670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
314264670ac8SStephen M. Cameron 		"(known firmware bug.)  Ignoring.\n");
314364670ac8SStephen M. Cameron 
314464670ac8SStephen M. Cameron 	return 1;
314564670ac8SStephen M. Cameron }
314664670ac8SStephen M. Cameron 
314764670ac8SStephen M. Cameron static irqreturn_t hpsa_intx_discard_completions(int irq, void *dev_id)
314864670ac8SStephen M. Cameron {
314964670ac8SStephen M. Cameron 	struct ctlr_info *h = dev_id;
315064670ac8SStephen M. Cameron 	unsigned long flags;
315164670ac8SStephen M. Cameron 	u32 raw_tag;
315264670ac8SStephen M. Cameron 
315364670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
315464670ac8SStephen M. Cameron 		return IRQ_NONE;
315564670ac8SStephen M. Cameron 
315664670ac8SStephen M. Cameron 	if (interrupt_not_for_us(h))
315764670ac8SStephen M. Cameron 		return IRQ_NONE;
315864670ac8SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
3159a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
316064670ac8SStephen M. Cameron 	while (interrupt_pending(h)) {
316164670ac8SStephen M. Cameron 		raw_tag = get_next_completion(h);
316264670ac8SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY)
316364670ac8SStephen M. Cameron 			raw_tag = next_command(h);
316464670ac8SStephen M. Cameron 	}
316564670ac8SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
316664670ac8SStephen M. Cameron 	return IRQ_HANDLED;
316764670ac8SStephen M. Cameron }
316864670ac8SStephen M. Cameron 
316964670ac8SStephen M. Cameron static irqreturn_t hpsa_msix_discard_completions(int irq, void *dev_id)
317064670ac8SStephen M. Cameron {
317164670ac8SStephen M. Cameron 	struct ctlr_info *h = dev_id;
317264670ac8SStephen M. Cameron 	unsigned long flags;
317364670ac8SStephen M. Cameron 	u32 raw_tag;
317464670ac8SStephen M. Cameron 
317564670ac8SStephen M. Cameron 	if (ignore_bogus_interrupt(h))
317664670ac8SStephen M. Cameron 		return IRQ_NONE;
317764670ac8SStephen M. Cameron 
317864670ac8SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
3179a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
318064670ac8SStephen M. Cameron 	raw_tag = get_next_completion(h);
318164670ac8SStephen M. Cameron 	while (raw_tag != FIFO_EMPTY)
318264670ac8SStephen M. Cameron 		raw_tag = next_command(h);
318364670ac8SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
318464670ac8SStephen M. Cameron 	return IRQ_HANDLED;
318564670ac8SStephen M. Cameron }
318664670ac8SStephen M. Cameron 
318710f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id)
3188edd16368SStephen M. Cameron {
3189edd16368SStephen M. Cameron 	struct ctlr_info *h = dev_id;
3190edd16368SStephen M. Cameron 	unsigned long flags;
3191303932fdSDon Brace 	u32 raw_tag;
3192edd16368SStephen M. Cameron 
3193edd16368SStephen M. Cameron 	if (interrupt_not_for_us(h))
3194edd16368SStephen M. Cameron 		return IRQ_NONE;
3195edd16368SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
3196a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
319710f66018SStephen M. Cameron 	while (interrupt_pending(h)) {
319810f66018SStephen M. Cameron 		raw_tag = get_next_completion(h);
319910f66018SStephen M. Cameron 		while (raw_tag != FIFO_EMPTY) {
320010f66018SStephen M. Cameron 			if (hpsa_tag_contains_index(raw_tag))
320110f66018SStephen M. Cameron 				raw_tag = process_indexed_cmd(h, raw_tag);
320210f66018SStephen M. Cameron 			else
320310f66018SStephen M. Cameron 				raw_tag = process_nonindexed_cmd(h, raw_tag);
320410f66018SStephen M. Cameron 		}
320510f66018SStephen M. Cameron 	}
320610f66018SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
320710f66018SStephen M. Cameron 	return IRQ_HANDLED;
320810f66018SStephen M. Cameron }
320910f66018SStephen M. Cameron 
321010f66018SStephen M. Cameron static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id)
321110f66018SStephen M. Cameron {
321210f66018SStephen M. Cameron 	struct ctlr_info *h = dev_id;
321310f66018SStephen M. Cameron 	unsigned long flags;
321410f66018SStephen M. Cameron 	u32 raw_tag;
321510f66018SStephen M. Cameron 
321610f66018SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
3217a0c12413SStephen M. Cameron 	h->last_intr_timestamp = get_jiffies_64();
3218303932fdSDon Brace 	raw_tag = get_next_completion(h);
3219303932fdSDon Brace 	while (raw_tag != FIFO_EMPTY) {
3220303932fdSDon Brace 		if (hpsa_tag_contains_index(raw_tag))
3221303932fdSDon Brace 			raw_tag = process_indexed_cmd(h, raw_tag);
3222303932fdSDon Brace 		else
3223303932fdSDon Brace 			raw_tag = process_nonindexed_cmd(h, raw_tag);
3224edd16368SStephen M. Cameron 	}
3225edd16368SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
3226edd16368SStephen M. Cameron 	return IRQ_HANDLED;
3227edd16368SStephen M. Cameron }
3228edd16368SStephen M. Cameron 
3229a9a3a273SStephen M. Cameron /* Send a message CDB to the firmware. Careful, this only works
3230a9a3a273SStephen M. Cameron  * in simple mode, not performant mode due to the tag lookup.
3231a9a3a273SStephen M. Cameron  * We only ever use this immediately after a controller reset.
3232a9a3a273SStephen M. Cameron  */
3233edd16368SStephen M. Cameron static __devinit int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
3234edd16368SStephen M. Cameron 						unsigned char type)
3235edd16368SStephen M. Cameron {
3236edd16368SStephen M. Cameron 	struct Command {
3237edd16368SStephen M. Cameron 		struct CommandListHeader CommandHeader;
3238edd16368SStephen M. Cameron 		struct RequestBlock Request;
3239edd16368SStephen M. Cameron 		struct ErrDescriptor ErrorDescriptor;
3240edd16368SStephen M. Cameron 	};
3241edd16368SStephen M. Cameron 	struct Command *cmd;
3242edd16368SStephen M. Cameron 	static const size_t cmd_sz = sizeof(*cmd) +
3243edd16368SStephen M. Cameron 					sizeof(cmd->ErrorDescriptor);
3244edd16368SStephen M. Cameron 	dma_addr_t paddr64;
3245edd16368SStephen M. Cameron 	uint32_t paddr32, tag;
3246edd16368SStephen M. Cameron 	void __iomem *vaddr;
3247edd16368SStephen M. Cameron 	int i, err;
3248edd16368SStephen M. Cameron 
3249edd16368SStephen M. Cameron 	vaddr = pci_ioremap_bar(pdev, 0);
3250edd16368SStephen M. Cameron 	if (vaddr == NULL)
3251edd16368SStephen M. Cameron 		return -ENOMEM;
3252edd16368SStephen M. Cameron 
3253edd16368SStephen M. Cameron 	/* The Inbound Post Queue only accepts 32-bit physical addresses for the
3254edd16368SStephen M. Cameron 	 * CCISS commands, so they must be allocated from the lower 4GiB of
3255edd16368SStephen M. Cameron 	 * memory.
3256edd16368SStephen M. Cameron 	 */
3257edd16368SStephen M. Cameron 	err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
3258edd16368SStephen M. Cameron 	if (err) {
3259edd16368SStephen M. Cameron 		iounmap(vaddr);
3260edd16368SStephen M. Cameron 		return -ENOMEM;
3261edd16368SStephen M. Cameron 	}
3262edd16368SStephen M. Cameron 
3263edd16368SStephen M. Cameron 	cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
3264edd16368SStephen M. Cameron 	if (cmd == NULL) {
3265edd16368SStephen M. Cameron 		iounmap(vaddr);
3266edd16368SStephen M. Cameron 		return -ENOMEM;
3267edd16368SStephen M. Cameron 	}
3268edd16368SStephen M. Cameron 
3269edd16368SStephen M. Cameron 	/* This must fit, because of the 32-bit consistent DMA mask.  Also,
3270edd16368SStephen M. Cameron 	 * although there's no guarantee, we assume that the address is at
3271edd16368SStephen M. Cameron 	 * least 4-byte aligned (most likely, it's page-aligned).
3272edd16368SStephen M. Cameron 	 */
3273edd16368SStephen M. Cameron 	paddr32 = paddr64;
3274edd16368SStephen M. Cameron 
3275edd16368SStephen M. Cameron 	cmd->CommandHeader.ReplyQueue = 0;
3276edd16368SStephen M. Cameron 	cmd->CommandHeader.SGList = 0;
3277edd16368SStephen M. Cameron 	cmd->CommandHeader.SGTotal = 0;
3278edd16368SStephen M. Cameron 	cmd->CommandHeader.Tag.lower = paddr32;
3279edd16368SStephen M. Cameron 	cmd->CommandHeader.Tag.upper = 0;
3280edd16368SStephen M. Cameron 	memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
3281edd16368SStephen M. Cameron 
3282edd16368SStephen M. Cameron 	cmd->Request.CDBLen = 16;
3283edd16368SStephen M. Cameron 	cmd->Request.Type.Type = TYPE_MSG;
3284edd16368SStephen M. Cameron 	cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
3285edd16368SStephen M. Cameron 	cmd->Request.Type.Direction = XFER_NONE;
3286edd16368SStephen M. Cameron 	cmd->Request.Timeout = 0; /* Don't time out */
3287edd16368SStephen M. Cameron 	cmd->Request.CDB[0] = opcode;
3288edd16368SStephen M. Cameron 	cmd->Request.CDB[1] = type;
3289edd16368SStephen M. Cameron 	memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
3290edd16368SStephen M. Cameron 	cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
3291edd16368SStephen M. Cameron 	cmd->ErrorDescriptor.Addr.upper = 0;
3292edd16368SStephen M. Cameron 	cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
3293edd16368SStephen M. Cameron 
3294edd16368SStephen M. Cameron 	writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
3295edd16368SStephen M. Cameron 
3296edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
3297edd16368SStephen M. Cameron 		tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
3298a9a3a273SStephen M. Cameron 		if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr32)
3299edd16368SStephen M. Cameron 			break;
3300edd16368SStephen M. Cameron 		msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
3301edd16368SStephen M. Cameron 	}
3302edd16368SStephen M. Cameron 
3303edd16368SStephen M. Cameron 	iounmap(vaddr);
3304edd16368SStephen M. Cameron 
3305edd16368SStephen M. Cameron 	/* we leak the DMA buffer here ... no choice since the controller could
3306edd16368SStephen M. Cameron 	 *  still complete the command.
3307edd16368SStephen M. Cameron 	 */
3308edd16368SStephen M. Cameron 	if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
3309edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
3310edd16368SStephen M. Cameron 			opcode, type);
3311edd16368SStephen M. Cameron 		return -ETIMEDOUT;
3312edd16368SStephen M. Cameron 	}
3313edd16368SStephen M. Cameron 
3314edd16368SStephen M. Cameron 	pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
3315edd16368SStephen M. Cameron 
3316edd16368SStephen M. Cameron 	if (tag & HPSA_ERROR_BIT) {
3317edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
3318edd16368SStephen M. Cameron 			opcode, type);
3319edd16368SStephen M. Cameron 		return -EIO;
3320edd16368SStephen M. Cameron 	}
3321edd16368SStephen M. Cameron 
3322edd16368SStephen M. Cameron 	dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
3323edd16368SStephen M. Cameron 		opcode, type);
3324edd16368SStephen M. Cameron 	return 0;
3325edd16368SStephen M. Cameron }
3326edd16368SStephen M. Cameron 
3327edd16368SStephen M. Cameron #define hpsa_noop(p) hpsa_message(p, 3, 0)
3328edd16368SStephen M. Cameron 
33291df8552aSStephen M. Cameron static int hpsa_controller_hard_reset(struct pci_dev *pdev,
3330cf0b08d0SStephen M. Cameron 	void * __iomem vaddr, u32 use_doorbell)
3331edd16368SStephen M. Cameron {
33321df8552aSStephen M. Cameron 	u16 pmcsr;
33331df8552aSStephen M. Cameron 	int pos;
3334edd16368SStephen M. Cameron 
33351df8552aSStephen M. Cameron 	if (use_doorbell) {
33361df8552aSStephen M. Cameron 		/* For everything after the P600, the PCI power state method
33371df8552aSStephen M. Cameron 		 * of resetting the controller doesn't work, so we have this
33381df8552aSStephen M. Cameron 		 * other way using the doorbell register.
3339edd16368SStephen M. Cameron 		 */
33401df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using doorbell to reset controller\n");
3341cf0b08d0SStephen M. Cameron 		writel(use_doorbell, vaddr + SA5_DOORBELL);
33421df8552aSStephen M. Cameron 	} else { /* Try to do it the PCI power state way */
3343edd16368SStephen M. Cameron 
3344edd16368SStephen M. Cameron 		/* Quoting from the Open CISS Specification: "The Power
3345edd16368SStephen M. Cameron 		 * Management Control/Status Register (CSR) controls the power
3346edd16368SStephen M. Cameron 		 * state of the device.  The normal operating state is D0,
3347edd16368SStephen M. Cameron 		 * CSR=00h.  The software off state is D3, CSR=03h.  To reset
33481df8552aSStephen M. Cameron 		 * the controller, place the interface device in D3 then to D0,
33491df8552aSStephen M. Cameron 		 * this causes a secondary PCI reset which will reset the
33501df8552aSStephen M. Cameron 		 * controller." */
3351edd16368SStephen M. Cameron 
33521df8552aSStephen M. Cameron 		pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
33531df8552aSStephen M. Cameron 		if (pos == 0) {
33541df8552aSStephen M. Cameron 			dev_err(&pdev->dev,
33551df8552aSStephen M. Cameron 				"hpsa_reset_controller: "
33561df8552aSStephen M. Cameron 				"PCI PM not supported\n");
33571df8552aSStephen M. Cameron 			return -ENODEV;
33581df8552aSStephen M. Cameron 		}
33591df8552aSStephen M. Cameron 		dev_info(&pdev->dev, "using PCI PM to reset controller\n");
3360edd16368SStephen M. Cameron 		/* enter the D3hot power management state */
3361edd16368SStephen M. Cameron 		pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
3362edd16368SStephen M. Cameron 		pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
3363edd16368SStephen M. Cameron 		pmcsr |= PCI_D3hot;
3364edd16368SStephen M. Cameron 		pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
3365edd16368SStephen M. Cameron 
3366edd16368SStephen M. Cameron 		msleep(500);
3367edd16368SStephen M. Cameron 
3368edd16368SStephen M. Cameron 		/* enter the D0 power management state */
3369edd16368SStephen M. Cameron 		pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
3370edd16368SStephen M. Cameron 		pmcsr |= PCI_D0;
3371edd16368SStephen M. Cameron 		pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
3372c4853efeSMike Miller 
3373c4853efeSMike Miller 		/*
3374c4853efeSMike Miller 		 * The P600 requires a small delay when changing states.
3375c4853efeSMike Miller 		 * Otherwise we may think the board did not reset and we bail.
3376c4853efeSMike Miller 		 * This for kdump only and is particular to the P600.
3377c4853efeSMike Miller 		 */
3378c4853efeSMike Miller 		msleep(500);
33791df8552aSStephen M. Cameron 	}
33801df8552aSStephen M. Cameron 	return 0;
33811df8552aSStephen M. Cameron }
33821df8552aSStephen M. Cameron 
3383580ada3cSStephen M. Cameron static __devinit void init_driver_version(char *driver_version, int len)
3384580ada3cSStephen M. Cameron {
3385580ada3cSStephen M. Cameron 	memset(driver_version, 0, len);
3386f79cfec6SStephen M. Cameron 	strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
3387580ada3cSStephen M. Cameron }
3388580ada3cSStephen M. Cameron 
3389580ada3cSStephen M. Cameron static __devinit int write_driver_ver_to_cfgtable(
3390580ada3cSStephen M. Cameron 	struct CfgTable __iomem *cfgtable)
3391580ada3cSStephen M. Cameron {
3392580ada3cSStephen M. Cameron 	char *driver_version;
3393580ada3cSStephen M. Cameron 	int i, size = sizeof(cfgtable->driver_version);
3394580ada3cSStephen M. Cameron 
3395580ada3cSStephen M. Cameron 	driver_version = kmalloc(size, GFP_KERNEL);
3396580ada3cSStephen M. Cameron 	if (!driver_version)
3397580ada3cSStephen M. Cameron 		return -ENOMEM;
3398580ada3cSStephen M. Cameron 
3399580ada3cSStephen M. Cameron 	init_driver_version(driver_version, size);
3400580ada3cSStephen M. Cameron 	for (i = 0; i < size; i++)
3401580ada3cSStephen M. Cameron 		writeb(driver_version[i], &cfgtable->driver_version[i]);
3402580ada3cSStephen M. Cameron 	kfree(driver_version);
3403580ada3cSStephen M. Cameron 	return 0;
3404580ada3cSStephen M. Cameron }
3405580ada3cSStephen M. Cameron 
3406580ada3cSStephen M. Cameron static __devinit void read_driver_ver_from_cfgtable(
3407580ada3cSStephen M. Cameron 	struct CfgTable __iomem *cfgtable, unsigned char *driver_ver)
3408580ada3cSStephen M. Cameron {
3409580ada3cSStephen M. Cameron 	int i;
3410580ada3cSStephen M. Cameron 
3411580ada3cSStephen M. Cameron 	for (i = 0; i < sizeof(cfgtable->driver_version); i++)
3412580ada3cSStephen M. Cameron 		driver_ver[i] = readb(&cfgtable->driver_version[i]);
3413580ada3cSStephen M. Cameron }
3414580ada3cSStephen M. Cameron 
3415580ada3cSStephen M. Cameron static __devinit int controller_reset_failed(
3416580ada3cSStephen M. Cameron 	struct CfgTable __iomem *cfgtable)
3417580ada3cSStephen M. Cameron {
3418580ada3cSStephen M. Cameron 
3419580ada3cSStephen M. Cameron 	char *driver_ver, *old_driver_ver;
3420580ada3cSStephen M. Cameron 	int rc, size = sizeof(cfgtable->driver_version);
3421580ada3cSStephen M. Cameron 
3422580ada3cSStephen M. Cameron 	old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
3423580ada3cSStephen M. Cameron 	if (!old_driver_ver)
3424580ada3cSStephen M. Cameron 		return -ENOMEM;
3425580ada3cSStephen M. Cameron 	driver_ver = old_driver_ver + size;
3426580ada3cSStephen M. Cameron 
3427580ada3cSStephen M. Cameron 	/* After a reset, the 32 bytes of "driver version" in the cfgtable
3428580ada3cSStephen M. Cameron 	 * should have been changed, otherwise we know the reset failed.
3429580ada3cSStephen M. Cameron 	 */
3430580ada3cSStephen M. Cameron 	init_driver_version(old_driver_ver, size);
3431580ada3cSStephen M. Cameron 	read_driver_ver_from_cfgtable(cfgtable, driver_ver);
3432580ada3cSStephen M. Cameron 	rc = !memcmp(driver_ver, old_driver_ver, size);
3433580ada3cSStephen M. Cameron 	kfree(old_driver_ver);
3434580ada3cSStephen M. Cameron 	return rc;
3435580ada3cSStephen M. Cameron }
34361df8552aSStephen M. Cameron /* This does a hard reset of the controller using PCI power management
34371df8552aSStephen M. Cameron  * states or the using the doorbell register.
34381df8552aSStephen M. Cameron  */
34391df8552aSStephen M. Cameron static __devinit int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev)
34401df8552aSStephen M. Cameron {
34411df8552aSStephen M. Cameron 	u64 cfg_offset;
34421df8552aSStephen M. Cameron 	u32 cfg_base_addr;
34431df8552aSStephen M. Cameron 	u64 cfg_base_addr_index;
34441df8552aSStephen M. Cameron 	void __iomem *vaddr;
34451df8552aSStephen M. Cameron 	unsigned long paddr;
3446580ada3cSStephen M. Cameron 	u32 misc_fw_support;
3447270d05deSStephen M. Cameron 	int rc;
34481df8552aSStephen M. Cameron 	struct CfgTable __iomem *cfgtable;
3449cf0b08d0SStephen M. Cameron 	u32 use_doorbell;
345018867659SStephen M. Cameron 	u32 board_id;
3451270d05deSStephen M. Cameron 	u16 command_register;
34521df8552aSStephen M. Cameron 
34531df8552aSStephen M. Cameron 	/* For controllers as old as the P600, this is very nearly
34541df8552aSStephen M. Cameron 	 * the same thing as
34551df8552aSStephen M. Cameron 	 *
34561df8552aSStephen M. Cameron 	 * pci_save_state(pci_dev);
34571df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D3hot);
34581df8552aSStephen M. Cameron 	 * pci_set_power_state(pci_dev, PCI_D0);
34591df8552aSStephen M. Cameron 	 * pci_restore_state(pci_dev);
34601df8552aSStephen M. Cameron 	 *
34611df8552aSStephen M. Cameron 	 * For controllers newer than the P600, the pci power state
34621df8552aSStephen M. Cameron 	 * method of resetting doesn't work so we have another way
34631df8552aSStephen M. Cameron 	 * using the doorbell register.
34641df8552aSStephen M. Cameron 	 */
346518867659SStephen M. Cameron 
346625c1e56aSStephen M. Cameron 	rc = hpsa_lookup_board_id(pdev, &board_id);
346746380786SStephen M. Cameron 	if (rc < 0 || !ctlr_is_resettable(board_id)) {
346825c1e56aSStephen M. Cameron 		dev_warn(&pdev->dev, "Not resetting device.\n");
346925c1e56aSStephen M. Cameron 		return -ENODEV;
347025c1e56aSStephen M. Cameron 	}
347146380786SStephen M. Cameron 
347246380786SStephen M. Cameron 	/* if controller is soft- but not hard resettable... */
347346380786SStephen M. Cameron 	if (!ctlr_is_hard_resettable(board_id))
347446380786SStephen M. Cameron 		return -ENOTSUPP; /* try soft reset later. */
347518867659SStephen M. Cameron 
3476270d05deSStephen M. Cameron 	/* Save the PCI command register */
3477270d05deSStephen M. Cameron 	pci_read_config_word(pdev, 4, &command_register);
3478270d05deSStephen M. Cameron 	/* Turn the board off.  This is so that later pci_restore_state()
3479270d05deSStephen M. Cameron 	 * won't turn the board on before the rest of config space is ready.
3480270d05deSStephen M. Cameron 	 */
3481270d05deSStephen M. Cameron 	pci_disable_device(pdev);
3482270d05deSStephen M. Cameron 	pci_save_state(pdev);
34831df8552aSStephen M. Cameron 
34841df8552aSStephen M. Cameron 	/* find the first memory BAR, so we can find the cfg table */
34851df8552aSStephen M. Cameron 	rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
34861df8552aSStephen M. Cameron 	if (rc)
34871df8552aSStephen M. Cameron 		return rc;
34881df8552aSStephen M. Cameron 	vaddr = remap_pci_mem(paddr, 0x250);
34891df8552aSStephen M. Cameron 	if (!vaddr)
34901df8552aSStephen M. Cameron 		return -ENOMEM;
34911df8552aSStephen M. Cameron 
34921df8552aSStephen M. Cameron 	/* find cfgtable in order to check if reset via doorbell is supported */
34931df8552aSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
34941df8552aSStephen M. Cameron 					&cfg_base_addr_index, &cfg_offset);
34951df8552aSStephen M. Cameron 	if (rc)
34961df8552aSStephen M. Cameron 		goto unmap_vaddr;
34971df8552aSStephen M. Cameron 	cfgtable = remap_pci_mem(pci_resource_start(pdev,
34981df8552aSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
34991df8552aSStephen M. Cameron 	if (!cfgtable) {
35001df8552aSStephen M. Cameron 		rc = -ENOMEM;
35011df8552aSStephen M. Cameron 		goto unmap_vaddr;
35021df8552aSStephen M. Cameron 	}
3503580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(cfgtable);
3504580ada3cSStephen M. Cameron 	if (rc)
3505580ada3cSStephen M. Cameron 		goto unmap_vaddr;
35061df8552aSStephen M. Cameron 
3507cf0b08d0SStephen M. Cameron 	/* If reset via doorbell register is supported, use that.
3508cf0b08d0SStephen M. Cameron 	 * There are two such methods.  Favor the newest method.
3509cf0b08d0SStephen M. Cameron 	 */
35101df8552aSStephen M. Cameron 	misc_fw_support = readl(&cfgtable->misc_fw_support);
3511cf0b08d0SStephen M. Cameron 	use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
3512cf0b08d0SStephen M. Cameron 	if (use_doorbell) {
3513cf0b08d0SStephen M. Cameron 		use_doorbell = DOORBELL_CTLR_RESET2;
3514cf0b08d0SStephen M. Cameron 	} else {
35151df8552aSStephen M. Cameron 		use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
3516cf0b08d0SStephen M. Cameron 		if (use_doorbell) {
3517fba63097SMike Miller 			dev_warn(&pdev->dev, "Soft reset not supported. "
3518fba63097SMike Miller 				"Firmware update is required.\n");
351964670ac8SStephen M. Cameron 			rc = -ENOTSUPP; /* try soft reset */
3520cf0b08d0SStephen M. Cameron 			goto unmap_cfgtable;
3521cf0b08d0SStephen M. Cameron 		}
3522cf0b08d0SStephen M. Cameron 	}
35231df8552aSStephen M. Cameron 
35241df8552aSStephen M. Cameron 	rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
35251df8552aSStephen M. Cameron 	if (rc)
35261df8552aSStephen M. Cameron 		goto unmap_cfgtable;
3527edd16368SStephen M. Cameron 
3528270d05deSStephen M. Cameron 	pci_restore_state(pdev);
3529270d05deSStephen M. Cameron 	rc = pci_enable_device(pdev);
3530270d05deSStephen M. Cameron 	if (rc) {
3531270d05deSStephen M. Cameron 		dev_warn(&pdev->dev, "failed to enable device.\n");
3532270d05deSStephen M. Cameron 		goto unmap_cfgtable;
3533edd16368SStephen M. Cameron 	}
3534270d05deSStephen M. Cameron 	pci_write_config_word(pdev, 4, command_register);
3535edd16368SStephen M. Cameron 
35361df8552aSStephen M. Cameron 	/* Some devices (notably the HP Smart Array 5i Controller)
35371df8552aSStephen M. Cameron 	   need a little pause here */
35381df8552aSStephen M. Cameron 	msleep(HPSA_POST_RESET_PAUSE_MSECS);
35391df8552aSStephen M. Cameron 
3540fe5389c8SStephen M. Cameron 	/* Wait for board to become not ready, then ready. */
35412b870cb3SStephen M. Cameron 	dev_info(&pdev->dev, "Waiting for board to reset.\n");
3542fe5389c8SStephen M. Cameron 	rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_NOT_READY);
354364670ac8SStephen M. Cameron 	if (rc) {
3544fe5389c8SStephen M. Cameron 		dev_warn(&pdev->dev,
354564670ac8SStephen M. Cameron 			"failed waiting for board to reset."
354664670ac8SStephen M. Cameron 			" Will try soft reset.\n");
354764670ac8SStephen M. Cameron 		rc = -ENOTSUPP; /* Not expected, but try soft reset later */
354864670ac8SStephen M. Cameron 		goto unmap_cfgtable;
354964670ac8SStephen M. Cameron 	}
3550fe5389c8SStephen M. Cameron 	rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
3551fe5389c8SStephen M. Cameron 	if (rc) {
3552fe5389c8SStephen M. Cameron 		dev_warn(&pdev->dev,
355364670ac8SStephen M. Cameron 			"failed waiting for board to become ready "
355464670ac8SStephen M. Cameron 			"after hard reset\n");
3555fe5389c8SStephen M. Cameron 		goto unmap_cfgtable;
3556fe5389c8SStephen M. Cameron 	}
3557fe5389c8SStephen M. Cameron 
3558580ada3cSStephen M. Cameron 	rc = controller_reset_failed(vaddr);
3559580ada3cSStephen M. Cameron 	if (rc < 0)
3560580ada3cSStephen M. Cameron 		goto unmap_cfgtable;
3561580ada3cSStephen M. Cameron 	if (rc) {
356264670ac8SStephen M. Cameron 		dev_warn(&pdev->dev, "Unable to successfully reset "
356364670ac8SStephen M. Cameron 			"controller. Will try soft reset.\n");
356464670ac8SStephen M. Cameron 		rc = -ENOTSUPP;
3565580ada3cSStephen M. Cameron 	} else {
356664670ac8SStephen M. Cameron 		dev_info(&pdev->dev, "board ready after hard reset.\n");
35671df8552aSStephen M. Cameron 	}
35681df8552aSStephen M. Cameron 
35691df8552aSStephen M. Cameron unmap_cfgtable:
35701df8552aSStephen M. Cameron 	iounmap(cfgtable);
35711df8552aSStephen M. Cameron 
35721df8552aSStephen M. Cameron unmap_vaddr:
35731df8552aSStephen M. Cameron 	iounmap(vaddr);
35741df8552aSStephen M. Cameron 	return rc;
3575edd16368SStephen M. Cameron }
3576edd16368SStephen M. Cameron 
3577edd16368SStephen M. Cameron /*
3578edd16368SStephen M. Cameron  *  We cannot read the structure directly, for portability we must use
3579edd16368SStephen M. Cameron  *   the io functions.
3580edd16368SStephen M. Cameron  *   This is for debug only.
3581edd16368SStephen M. Cameron  */
3582edd16368SStephen M. Cameron static void print_cfg_table(struct device *dev, struct CfgTable *tb)
3583edd16368SStephen M. Cameron {
358458f8665cSStephen M. Cameron #ifdef HPSA_DEBUG
3585edd16368SStephen M. Cameron 	int i;
3586edd16368SStephen M. Cameron 	char temp_name[17];
3587edd16368SStephen M. Cameron 
3588edd16368SStephen M. Cameron 	dev_info(dev, "Controller Configuration information\n");
3589edd16368SStephen M. Cameron 	dev_info(dev, "------------------------------------\n");
3590edd16368SStephen M. Cameron 	for (i = 0; i < 4; i++)
3591edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->Signature[i]));
3592edd16368SStephen M. Cameron 	temp_name[4] = '\0';
3593edd16368SStephen M. Cameron 	dev_info(dev, "   Signature = %s\n", temp_name);
3594edd16368SStephen M. Cameron 	dev_info(dev, "   Spec Number = %d\n", readl(&(tb->SpecValence)));
3595edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods supported = 0x%x\n",
3596edd16368SStephen M. Cameron 	       readl(&(tb->TransportSupport)));
3597edd16368SStephen M. Cameron 	dev_info(dev, "   Transport methods active = 0x%x\n",
3598edd16368SStephen M. Cameron 	       readl(&(tb->TransportActive)));
3599edd16368SStephen M. Cameron 	dev_info(dev, "   Requested transport Method = 0x%x\n",
3600edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.TransportRequest)));
3601edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Delay = 0x%x\n",
3602edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntDelay)));
3603edd16368SStephen M. Cameron 	dev_info(dev, "   Coalesce Interrupt Count = 0x%x\n",
3604edd16368SStephen M. Cameron 	       readl(&(tb->HostWrite.CoalIntCount)));
3605edd16368SStephen M. Cameron 	dev_info(dev, "   Max outstanding commands = 0x%d\n",
3606edd16368SStephen M. Cameron 	       readl(&(tb->CmdsOutMax)));
3607edd16368SStephen M. Cameron 	dev_info(dev, "   Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
3608edd16368SStephen M. Cameron 	for (i = 0; i < 16; i++)
3609edd16368SStephen M. Cameron 		temp_name[i] = readb(&(tb->ServerName[i]));
3610edd16368SStephen M. Cameron 	temp_name[16] = '\0';
3611edd16368SStephen M. Cameron 	dev_info(dev, "   Server Name = %s\n", temp_name);
3612edd16368SStephen M. Cameron 	dev_info(dev, "   Heartbeat Counter = 0x%x\n\n\n",
3613edd16368SStephen M. Cameron 		readl(&(tb->HeartBeat)));
3614edd16368SStephen M. Cameron #endif				/* HPSA_DEBUG */
361558f8665cSStephen M. Cameron }
3616edd16368SStephen M. Cameron 
3617edd16368SStephen M. Cameron static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
3618edd16368SStephen M. Cameron {
3619edd16368SStephen M. Cameron 	int i, offset, mem_type, bar_type;
3620edd16368SStephen M. Cameron 
3621edd16368SStephen M. Cameron 	if (pci_bar_addr == PCI_BASE_ADDRESS_0)	/* looking for BAR zero? */
3622edd16368SStephen M. Cameron 		return 0;
3623edd16368SStephen M. Cameron 	offset = 0;
3624edd16368SStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
3625edd16368SStephen M. Cameron 		bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
3626edd16368SStephen M. Cameron 		if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
3627edd16368SStephen M. Cameron 			offset += 4;
3628edd16368SStephen M. Cameron 		else {
3629edd16368SStephen M. Cameron 			mem_type = pci_resource_flags(pdev, i) &
3630edd16368SStephen M. Cameron 			    PCI_BASE_ADDRESS_MEM_TYPE_MASK;
3631edd16368SStephen M. Cameron 			switch (mem_type) {
3632edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_32:
3633edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_1M:
3634edd16368SStephen M. Cameron 				offset += 4;	/* 32 bit */
3635edd16368SStephen M. Cameron 				break;
3636edd16368SStephen M. Cameron 			case PCI_BASE_ADDRESS_MEM_TYPE_64:
3637edd16368SStephen M. Cameron 				offset += 8;
3638edd16368SStephen M. Cameron 				break;
3639edd16368SStephen M. Cameron 			default:	/* reserved in PCI 2.2 */
3640edd16368SStephen M. Cameron 				dev_warn(&pdev->dev,
3641edd16368SStephen M. Cameron 				       "base address is invalid\n");
3642edd16368SStephen M. Cameron 				return -1;
3643edd16368SStephen M. Cameron 				break;
3644edd16368SStephen M. Cameron 			}
3645edd16368SStephen M. Cameron 		}
3646edd16368SStephen M. Cameron 		if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
3647edd16368SStephen M. Cameron 			return i + 1;
3648edd16368SStephen M. Cameron 	}
3649edd16368SStephen M. Cameron 	return -1;
3650edd16368SStephen M. Cameron }
3651edd16368SStephen M. Cameron 
3652edd16368SStephen M. Cameron /* If MSI/MSI-X is supported by the kernel we will try to enable it on
3653edd16368SStephen M. Cameron  * controllers that are capable. If not, we use IO-APIC mode.
3654edd16368SStephen M. Cameron  */
3655edd16368SStephen M. Cameron 
36566b3f4c52SStephen M. Cameron static void __devinit hpsa_interrupt_mode(struct ctlr_info *h)
3657edd16368SStephen M. Cameron {
3658edd16368SStephen M. Cameron #ifdef CONFIG_PCI_MSI
3659edd16368SStephen M. Cameron 	int err;
3660edd16368SStephen M. Cameron 	struct msix_entry hpsa_msix_entries[4] = { {0, 0}, {0, 1},
3661edd16368SStephen M. Cameron 	{0, 2}, {0, 3}
3662edd16368SStephen M. Cameron 	};
3663edd16368SStephen M. Cameron 
3664edd16368SStephen M. Cameron 	/* Some boards advertise MSI but don't really support it */
36656b3f4c52SStephen M. Cameron 	if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
36666b3f4c52SStephen M. Cameron 	    (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
3667edd16368SStephen M. Cameron 		goto default_int_mode;
366855c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
366955c06c71SStephen M. Cameron 		dev_info(&h->pdev->dev, "MSIX\n");
367055c06c71SStephen M. Cameron 		err = pci_enable_msix(h->pdev, hpsa_msix_entries, 4);
3671edd16368SStephen M. Cameron 		if (!err) {
3672edd16368SStephen M. Cameron 			h->intr[0] = hpsa_msix_entries[0].vector;
3673edd16368SStephen M. Cameron 			h->intr[1] = hpsa_msix_entries[1].vector;
3674edd16368SStephen M. Cameron 			h->intr[2] = hpsa_msix_entries[2].vector;
3675edd16368SStephen M. Cameron 			h->intr[3] = hpsa_msix_entries[3].vector;
3676edd16368SStephen M. Cameron 			h->msix_vector = 1;
3677edd16368SStephen M. Cameron 			return;
3678edd16368SStephen M. Cameron 		}
3679edd16368SStephen M. Cameron 		if (err > 0) {
368055c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
3681edd16368SStephen M. Cameron 			       "available\n", err);
3682edd16368SStephen M. Cameron 			goto default_int_mode;
3683edd16368SStephen M. Cameron 		} else {
368455c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "MSI-X init failed %d\n",
3685edd16368SStephen M. Cameron 			       err);
3686edd16368SStephen M. Cameron 			goto default_int_mode;
3687edd16368SStephen M. Cameron 		}
3688edd16368SStephen M. Cameron 	}
368955c06c71SStephen M. Cameron 	if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
369055c06c71SStephen M. Cameron 		dev_info(&h->pdev->dev, "MSI\n");
369155c06c71SStephen M. Cameron 		if (!pci_enable_msi(h->pdev))
3692edd16368SStephen M. Cameron 			h->msi_vector = 1;
3693edd16368SStephen M. Cameron 		else
369455c06c71SStephen M. Cameron 			dev_warn(&h->pdev->dev, "MSI init failed\n");
3695edd16368SStephen M. Cameron 	}
3696edd16368SStephen M. Cameron default_int_mode:
3697edd16368SStephen M. Cameron #endif				/* CONFIG_PCI_MSI */
3698edd16368SStephen M. Cameron 	/* if we get here we're going to use the default interrupt mode */
3699a9a3a273SStephen M. Cameron 	h->intr[h->intr_mode] = h->pdev->irq;
3700edd16368SStephen M. Cameron }
3701edd16368SStephen M. Cameron 
3702e5c880d1SStephen M. Cameron static int __devinit hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
3703e5c880d1SStephen M. Cameron {
3704e5c880d1SStephen M. Cameron 	int i;
3705e5c880d1SStephen M. Cameron 	u32 subsystem_vendor_id, subsystem_device_id;
3706e5c880d1SStephen M. Cameron 
3707e5c880d1SStephen M. Cameron 	subsystem_vendor_id = pdev->subsystem_vendor;
3708e5c880d1SStephen M. Cameron 	subsystem_device_id = pdev->subsystem_device;
3709e5c880d1SStephen M. Cameron 	*board_id = ((subsystem_device_id << 16) & 0xffff0000) |
3710e5c880d1SStephen M. Cameron 		    subsystem_vendor_id;
3711e5c880d1SStephen M. Cameron 
3712e5c880d1SStephen M. Cameron 	for (i = 0; i < ARRAY_SIZE(products); i++)
3713e5c880d1SStephen M. Cameron 		if (*board_id == products[i].board_id)
3714e5c880d1SStephen M. Cameron 			return i;
3715e5c880d1SStephen M. Cameron 
37166798cc0aSStephen M. Cameron 	if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
37176798cc0aSStephen M. Cameron 		subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
37186798cc0aSStephen M. Cameron 		!hpsa_allow_any) {
3719e5c880d1SStephen M. Cameron 		dev_warn(&pdev->dev, "unrecognized board ID: "
3720e5c880d1SStephen M. Cameron 			"0x%08x, ignoring.\n", *board_id);
3721e5c880d1SStephen M. Cameron 			return -ENODEV;
3722e5c880d1SStephen M. Cameron 	}
3723e5c880d1SStephen M. Cameron 	return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
3724e5c880d1SStephen M. Cameron }
3725e5c880d1SStephen M. Cameron 
372612d2cd47SStephen M. Cameron static int __devinit hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
37273a7774ceSStephen M. Cameron 	unsigned long *memory_bar)
37283a7774ceSStephen M. Cameron {
37293a7774ceSStephen M. Cameron 	int i;
37303a7774ceSStephen M. Cameron 
37313a7774ceSStephen M. Cameron 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
373212d2cd47SStephen M. Cameron 		if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
37333a7774ceSStephen M. Cameron 			/* addressing mode bits already removed */
373412d2cd47SStephen M. Cameron 			*memory_bar = pci_resource_start(pdev, i);
373512d2cd47SStephen M. Cameron 			dev_dbg(&pdev->dev, "memory BAR = %lx\n",
37363a7774ceSStephen M. Cameron 				*memory_bar);
37373a7774ceSStephen M. Cameron 			return 0;
37383a7774ceSStephen M. Cameron 		}
373912d2cd47SStephen M. Cameron 	dev_warn(&pdev->dev, "no memory BAR found\n");
37403a7774ceSStephen M. Cameron 	return -ENODEV;
37413a7774ceSStephen M. Cameron }
37423a7774ceSStephen M. Cameron 
3743fe5389c8SStephen M. Cameron static int __devinit hpsa_wait_for_board_state(struct pci_dev *pdev,
3744fe5389c8SStephen M. Cameron 	void __iomem *vaddr, int wait_for_ready)
37452c4c8c8bSStephen M. Cameron {
3746fe5389c8SStephen M. Cameron 	int i, iterations;
37472c4c8c8bSStephen M. Cameron 	u32 scratchpad;
3748fe5389c8SStephen M. Cameron 	if (wait_for_ready)
3749fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_READY_ITERATIONS;
3750fe5389c8SStephen M. Cameron 	else
3751fe5389c8SStephen M. Cameron 		iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
37522c4c8c8bSStephen M. Cameron 
3753fe5389c8SStephen M. Cameron 	for (i = 0; i < iterations; i++) {
3754fe5389c8SStephen M. Cameron 		scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
3755fe5389c8SStephen M. Cameron 		if (wait_for_ready) {
37562c4c8c8bSStephen M. Cameron 			if (scratchpad == HPSA_FIRMWARE_READY)
37572c4c8c8bSStephen M. Cameron 				return 0;
3758fe5389c8SStephen M. Cameron 		} else {
3759fe5389c8SStephen M. Cameron 			if (scratchpad != HPSA_FIRMWARE_READY)
3760fe5389c8SStephen M. Cameron 				return 0;
3761fe5389c8SStephen M. Cameron 		}
37622c4c8c8bSStephen M. Cameron 		msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
37632c4c8c8bSStephen M. Cameron 	}
3764fe5389c8SStephen M. Cameron 	dev_warn(&pdev->dev, "board not ready, timed out.\n");
37652c4c8c8bSStephen M. Cameron 	return -ENODEV;
37662c4c8c8bSStephen M. Cameron }
37672c4c8c8bSStephen M. Cameron 
3768a51fd47fSStephen M. Cameron static int __devinit hpsa_find_cfg_addrs(struct pci_dev *pdev,
3769a51fd47fSStephen M. Cameron 	void __iomem *vaddr, u32 *cfg_base_addr, u64 *cfg_base_addr_index,
3770a51fd47fSStephen M. Cameron 	u64 *cfg_offset)
3771a51fd47fSStephen M. Cameron {
3772a51fd47fSStephen M. Cameron 	*cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
3773a51fd47fSStephen M. Cameron 	*cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
3774a51fd47fSStephen M. Cameron 	*cfg_base_addr &= (u32) 0x0000ffff;
3775a51fd47fSStephen M. Cameron 	*cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
3776a51fd47fSStephen M. Cameron 	if (*cfg_base_addr_index == -1) {
3777a51fd47fSStephen M. Cameron 		dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
3778a51fd47fSStephen M. Cameron 		return -ENODEV;
3779a51fd47fSStephen M. Cameron 	}
3780a51fd47fSStephen M. Cameron 	return 0;
3781a51fd47fSStephen M. Cameron }
3782a51fd47fSStephen M. Cameron 
378377c4495cSStephen M. Cameron static int __devinit hpsa_find_cfgtables(struct ctlr_info *h)
3784edd16368SStephen M. Cameron {
378501a02ffcSStephen M. Cameron 	u64 cfg_offset;
378601a02ffcSStephen M. Cameron 	u32 cfg_base_addr;
378701a02ffcSStephen M. Cameron 	u64 cfg_base_addr_index;
3788303932fdSDon Brace 	u32 trans_offset;
3789a51fd47fSStephen M. Cameron 	int rc;
379077c4495cSStephen M. Cameron 
3791a51fd47fSStephen M. Cameron 	rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
3792a51fd47fSStephen M. Cameron 		&cfg_base_addr_index, &cfg_offset);
3793a51fd47fSStephen M. Cameron 	if (rc)
3794a51fd47fSStephen M. Cameron 		return rc;
379577c4495cSStephen M. Cameron 	h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
3796a51fd47fSStephen M. Cameron 		       cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
379777c4495cSStephen M. Cameron 	if (!h->cfgtable)
379877c4495cSStephen M. Cameron 		return -ENOMEM;
3799580ada3cSStephen M. Cameron 	rc = write_driver_ver_to_cfgtable(h->cfgtable);
3800580ada3cSStephen M. Cameron 	if (rc)
3801580ada3cSStephen M. Cameron 		return rc;
380277c4495cSStephen M. Cameron 	/* Find performant mode table. */
3803a51fd47fSStephen M. Cameron 	trans_offset = readl(&h->cfgtable->TransMethodOffset);
380477c4495cSStephen M. Cameron 	h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
380577c4495cSStephen M. Cameron 				cfg_base_addr_index)+cfg_offset+trans_offset,
380677c4495cSStephen M. Cameron 				sizeof(*h->transtable));
380777c4495cSStephen M. Cameron 	if (!h->transtable)
380877c4495cSStephen M. Cameron 		return -ENOMEM;
380977c4495cSStephen M. Cameron 	return 0;
381077c4495cSStephen M. Cameron }
381177c4495cSStephen M. Cameron 
3812cba3d38bSStephen M. Cameron static void __devinit hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
3813cba3d38bSStephen M. Cameron {
3814cba3d38bSStephen M. Cameron 	h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
381572ceeaecSStephen M. Cameron 
381672ceeaecSStephen M. Cameron 	/* Limit commands in memory limited kdump scenario. */
381772ceeaecSStephen M. Cameron 	if (reset_devices && h->max_commands > 32)
381872ceeaecSStephen M. Cameron 		h->max_commands = 32;
381972ceeaecSStephen M. Cameron 
3820cba3d38bSStephen M. Cameron 	if (h->max_commands < 16) {
3821cba3d38bSStephen M. Cameron 		dev_warn(&h->pdev->dev, "Controller reports "
3822cba3d38bSStephen M. Cameron 			"max supported commands of %d, an obvious lie. "
3823cba3d38bSStephen M. Cameron 			"Using 16.  Ensure that firmware is up to date.\n",
3824cba3d38bSStephen M. Cameron 			h->max_commands);
3825cba3d38bSStephen M. Cameron 		h->max_commands = 16;
3826cba3d38bSStephen M. Cameron 	}
3827cba3d38bSStephen M. Cameron }
3828cba3d38bSStephen M. Cameron 
3829b93d7536SStephen M. Cameron /* Interrogate the hardware for some limits:
3830b93d7536SStephen M. Cameron  * max commands, max SG elements without chaining, and with chaining,
3831b93d7536SStephen M. Cameron  * SG chain block size, etc.
3832b93d7536SStephen M. Cameron  */
3833b93d7536SStephen M. Cameron static void __devinit hpsa_find_board_params(struct ctlr_info *h)
3834b93d7536SStephen M. Cameron {
3835cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
3836b93d7536SStephen M. Cameron 	h->nr_cmds = h->max_commands - 4; /* Allow room for some ioctls */
3837b93d7536SStephen M. Cameron 	h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
3838b93d7536SStephen M. Cameron 	/*
3839b93d7536SStephen M. Cameron 	 * Limit in-command s/g elements to 32 save dma'able memory.
3840b93d7536SStephen M. Cameron 	 * Howvever spec says if 0, use 31
3841b93d7536SStephen M. Cameron 	 */
3842b93d7536SStephen M. Cameron 	h->max_cmd_sg_entries = 31;
3843b93d7536SStephen M. Cameron 	if (h->maxsgentries > 512) {
3844b93d7536SStephen M. Cameron 		h->max_cmd_sg_entries = 32;
3845b93d7536SStephen M. Cameron 		h->chainsize = h->maxsgentries - h->max_cmd_sg_entries + 1;
3846b93d7536SStephen M. Cameron 		h->maxsgentries--; /* save one for chain pointer */
3847b93d7536SStephen M. Cameron 	} else {
3848b93d7536SStephen M. Cameron 		h->maxsgentries = 31; /* default to traditional values */
3849b93d7536SStephen M. Cameron 		h->chainsize = 0;
3850b93d7536SStephen M. Cameron 	}
3851b93d7536SStephen M. Cameron }
3852b93d7536SStephen M. Cameron 
385376c46e49SStephen M. Cameron static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
385476c46e49SStephen M. Cameron {
38550fc9fd40SAkinobu Mita 	if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
385676c46e49SStephen M. Cameron 		dev_warn(&h->pdev->dev, "not a valid CISS config table\n");
385776c46e49SStephen M. Cameron 		return false;
385876c46e49SStephen M. Cameron 	}
385976c46e49SStephen M. Cameron 	return true;
386076c46e49SStephen M. Cameron }
386176c46e49SStephen M. Cameron 
3862f7c39101SStephen M. Cameron /* Need to enable prefetch in the SCSI core for 6400 in x86 */
3863f7c39101SStephen M. Cameron static inline void hpsa_enable_scsi_prefetch(struct ctlr_info *h)
3864f7c39101SStephen M. Cameron {
3865f7c39101SStephen M. Cameron #ifdef CONFIG_X86
3866f7c39101SStephen M. Cameron 	u32 prefetch;
3867f7c39101SStephen M. Cameron 
3868f7c39101SStephen M. Cameron 	prefetch = readl(&(h->cfgtable->SCSI_Prefetch));
3869f7c39101SStephen M. Cameron 	prefetch |= 0x100;
3870f7c39101SStephen M. Cameron 	writel(prefetch, &(h->cfgtable->SCSI_Prefetch));
3871f7c39101SStephen M. Cameron #endif
3872f7c39101SStephen M. Cameron }
3873f7c39101SStephen M. Cameron 
38743d0eab67SStephen M. Cameron /* Disable DMA prefetch for the P600.  Otherwise an ASIC bug may result
38753d0eab67SStephen M. Cameron  * in a prefetch beyond physical memory.
38763d0eab67SStephen M. Cameron  */
38773d0eab67SStephen M. Cameron static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
38783d0eab67SStephen M. Cameron {
38793d0eab67SStephen M. Cameron 	u32 dma_prefetch;
38803d0eab67SStephen M. Cameron 
38813d0eab67SStephen M. Cameron 	if (h->board_id != 0x3225103C)
38823d0eab67SStephen M. Cameron 		return;
38833d0eab67SStephen M. Cameron 	dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
38843d0eab67SStephen M. Cameron 	dma_prefetch |= 0x8000;
38853d0eab67SStephen M. Cameron 	writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
38863d0eab67SStephen M. Cameron }
38873d0eab67SStephen M. Cameron 
38883f4336f3SStephen M. Cameron static void __devinit hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
3889eb6b2ae9SStephen M. Cameron {
3890eb6b2ae9SStephen M. Cameron 	int i;
38916eaf46fdSStephen M. Cameron 	u32 doorbell_value;
38926eaf46fdSStephen M. Cameron 	unsigned long flags;
3893eb6b2ae9SStephen M. Cameron 
3894eb6b2ae9SStephen M. Cameron 	/* under certain very rare conditions, this can take awhile.
3895eb6b2ae9SStephen M. Cameron 	 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
3896eb6b2ae9SStephen M. Cameron 	 * as we enter this code.)
3897eb6b2ae9SStephen M. Cameron 	 */
3898eb6b2ae9SStephen M. Cameron 	for (i = 0; i < MAX_CONFIG_WAIT; i++) {
38996eaf46fdSStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
39006eaf46fdSStephen M. Cameron 		doorbell_value = readl(h->vaddr + SA5_DOORBELL);
39016eaf46fdSStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
3902382be668SDan Carpenter 		if (!(doorbell_value & CFGTBL_ChangeReq))
3903eb6b2ae9SStephen M. Cameron 			break;
3904eb6b2ae9SStephen M. Cameron 		/* delay and try again */
390560d3f5b0SStephen M. Cameron 		usleep_range(10000, 20000);
3906eb6b2ae9SStephen M. Cameron 	}
39073f4336f3SStephen M. Cameron }
39083f4336f3SStephen M. Cameron 
39093f4336f3SStephen M. Cameron static int __devinit hpsa_enter_simple_mode(struct ctlr_info *h)
39103f4336f3SStephen M. Cameron {
39113f4336f3SStephen M. Cameron 	u32 trans_support;
39123f4336f3SStephen M. Cameron 
39133f4336f3SStephen M. Cameron 	trans_support = readl(&(h->cfgtable->TransportSupport));
39143f4336f3SStephen M. Cameron 	if (!(trans_support & SIMPLE_MODE))
39153f4336f3SStephen M. Cameron 		return -ENOTSUPP;
39163f4336f3SStephen M. Cameron 
39173f4336f3SStephen M. Cameron 	h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
39183f4336f3SStephen M. Cameron 	/* Update the field, and then ring the doorbell */
39193f4336f3SStephen M. Cameron 	writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
39203f4336f3SStephen M. Cameron 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
39213f4336f3SStephen M. Cameron 	hpsa_wait_for_mode_change_ack(h);
3922eb6b2ae9SStephen M. Cameron 	print_cfg_table(&h->pdev->dev, h->cfgtable);
3923eb6b2ae9SStephen M. Cameron 	if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) {
3924eb6b2ae9SStephen M. Cameron 		dev_warn(&h->pdev->dev,
3925eb6b2ae9SStephen M. Cameron 			"unable to get board into simple mode\n");
3926eb6b2ae9SStephen M. Cameron 		return -ENODEV;
3927eb6b2ae9SStephen M. Cameron 	}
3928960a30e7SStephen M. Cameron 	h->transMethod = CFGTBL_Trans_Simple;
3929eb6b2ae9SStephen M. Cameron 	return 0;
3930eb6b2ae9SStephen M. Cameron }
3931eb6b2ae9SStephen M. Cameron 
393277c4495cSStephen M. Cameron static int __devinit hpsa_pci_init(struct ctlr_info *h)
393377c4495cSStephen M. Cameron {
3934eb6b2ae9SStephen M. Cameron 	int prod_index, err;
3935edd16368SStephen M. Cameron 
3936e5c880d1SStephen M. Cameron 	prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
3937e5c880d1SStephen M. Cameron 	if (prod_index < 0)
3938edd16368SStephen M. Cameron 		return -ENODEV;
3939e5c880d1SStephen M. Cameron 	h->product_name = products[prod_index].product_name;
3940e5c880d1SStephen M. Cameron 	h->access = *(products[prod_index].access);
3941e5c880d1SStephen M. Cameron 
3942e5a44df8SMatthew Garrett 	pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
3943e5a44df8SMatthew Garrett 			       PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
3944e5a44df8SMatthew Garrett 
394555c06c71SStephen M. Cameron 	err = pci_enable_device(h->pdev);
3946edd16368SStephen M. Cameron 	if (err) {
394755c06c71SStephen M. Cameron 		dev_warn(&h->pdev->dev, "unable to enable PCI device\n");
3948edd16368SStephen M. Cameron 		return err;
3949edd16368SStephen M. Cameron 	}
3950edd16368SStephen M. Cameron 
39515cb460a6SStephen M. Cameron 	/* Enable bus mastering (pci_disable_device may disable this) */
39525cb460a6SStephen M. Cameron 	pci_set_master(h->pdev);
39535cb460a6SStephen M. Cameron 
3954f79cfec6SStephen M. Cameron 	err = pci_request_regions(h->pdev, HPSA);
3955edd16368SStephen M. Cameron 	if (err) {
395655c06c71SStephen M. Cameron 		dev_err(&h->pdev->dev,
395755c06c71SStephen M. Cameron 			"cannot obtain PCI resources, aborting\n");
3958edd16368SStephen M. Cameron 		return err;
3959edd16368SStephen M. Cameron 	}
39606b3f4c52SStephen M. Cameron 	hpsa_interrupt_mode(h);
396112d2cd47SStephen M. Cameron 	err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
39623a7774ceSStephen M. Cameron 	if (err)
3963edd16368SStephen M. Cameron 		goto err_out_free_res;
3964edd16368SStephen M. Cameron 	h->vaddr = remap_pci_mem(h->paddr, 0x250);
3965204892e9SStephen M. Cameron 	if (!h->vaddr) {
3966204892e9SStephen M. Cameron 		err = -ENOMEM;
3967204892e9SStephen M. Cameron 		goto err_out_free_res;
3968204892e9SStephen M. Cameron 	}
3969fe5389c8SStephen M. Cameron 	err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
39702c4c8c8bSStephen M. Cameron 	if (err)
3971edd16368SStephen M. Cameron 		goto err_out_free_res;
397277c4495cSStephen M. Cameron 	err = hpsa_find_cfgtables(h);
397377c4495cSStephen M. Cameron 	if (err)
3974edd16368SStephen M. Cameron 		goto err_out_free_res;
3975b93d7536SStephen M. Cameron 	hpsa_find_board_params(h);
3976edd16368SStephen M. Cameron 
397776c46e49SStephen M. Cameron 	if (!hpsa_CISS_signature_present(h)) {
3978edd16368SStephen M. Cameron 		err = -ENODEV;
3979edd16368SStephen M. Cameron 		goto err_out_free_res;
3980edd16368SStephen M. Cameron 	}
3981f7c39101SStephen M. Cameron 	hpsa_enable_scsi_prefetch(h);
39823d0eab67SStephen M. Cameron 	hpsa_p600_dma_prefetch_quirk(h);
3983eb6b2ae9SStephen M. Cameron 	err = hpsa_enter_simple_mode(h);
3984eb6b2ae9SStephen M. Cameron 	if (err)
3985edd16368SStephen M. Cameron 		goto err_out_free_res;
3986edd16368SStephen M. Cameron 	return 0;
3987edd16368SStephen M. Cameron 
3988edd16368SStephen M. Cameron err_out_free_res:
3989204892e9SStephen M. Cameron 	if (h->transtable)
3990204892e9SStephen M. Cameron 		iounmap(h->transtable);
3991204892e9SStephen M. Cameron 	if (h->cfgtable)
3992204892e9SStephen M. Cameron 		iounmap(h->cfgtable);
3993204892e9SStephen M. Cameron 	if (h->vaddr)
3994204892e9SStephen M. Cameron 		iounmap(h->vaddr);
3995f0bd0b68SStephen M. Cameron 	pci_disable_device(h->pdev);
399655c06c71SStephen M. Cameron 	pci_release_regions(h->pdev);
3997edd16368SStephen M. Cameron 	return err;
3998edd16368SStephen M. Cameron }
3999edd16368SStephen M. Cameron 
4000339b2b14SStephen M. Cameron static void __devinit hpsa_hba_inquiry(struct ctlr_info *h)
4001339b2b14SStephen M. Cameron {
4002339b2b14SStephen M. Cameron 	int rc;
4003339b2b14SStephen M. Cameron 
4004339b2b14SStephen M. Cameron #define HBA_INQUIRY_BYTE_COUNT 64
4005339b2b14SStephen M. Cameron 	h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
4006339b2b14SStephen M. Cameron 	if (!h->hba_inquiry_data)
4007339b2b14SStephen M. Cameron 		return;
4008339b2b14SStephen M. Cameron 	rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
4009339b2b14SStephen M. Cameron 		h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
4010339b2b14SStephen M. Cameron 	if (rc != 0) {
4011339b2b14SStephen M. Cameron 		kfree(h->hba_inquiry_data);
4012339b2b14SStephen M. Cameron 		h->hba_inquiry_data = NULL;
4013339b2b14SStephen M. Cameron 	}
4014339b2b14SStephen M. Cameron }
4015339b2b14SStephen M. Cameron 
40164c2a8c40SStephen M. Cameron static __devinit int hpsa_init_reset_devices(struct pci_dev *pdev)
4017edd16368SStephen M. Cameron {
40181df8552aSStephen M. Cameron 	int rc, i;
4019edd16368SStephen M. Cameron 
40204c2a8c40SStephen M. Cameron 	if (!reset_devices)
40214c2a8c40SStephen M. Cameron 		return 0;
40224c2a8c40SStephen M. Cameron 
40231df8552aSStephen M. Cameron 	/* Reset the controller with a PCI power-cycle or via doorbell */
40241df8552aSStephen M. Cameron 	rc = hpsa_kdump_hard_reset_controller(pdev);
4025edd16368SStephen M. Cameron 
40261df8552aSStephen M. Cameron 	/* -ENOTSUPP here means we cannot reset the controller
40271df8552aSStephen M. Cameron 	 * but it's already (and still) up and running in
402818867659SStephen M. Cameron 	 * "performant mode".  Or, it might be 640x, which can't reset
402918867659SStephen M. Cameron 	 * due to concerns about shared bbwc between 6402/6404 pair.
40301df8552aSStephen M. Cameron 	 */
40311df8552aSStephen M. Cameron 	if (rc == -ENOTSUPP)
403264670ac8SStephen M. Cameron 		return rc; /* just try to do the kdump anyhow. */
40331df8552aSStephen M. Cameron 	if (rc)
40341df8552aSStephen M. Cameron 		return -ENODEV;
4035edd16368SStephen M. Cameron 
4036edd16368SStephen M. Cameron 	/* Now try to get the controller to respond to a no-op */
40372b870cb3SStephen M. Cameron 	dev_warn(&pdev->dev, "Waiting for controller to respond to no-op\n");
4038edd16368SStephen M. Cameron 	for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
4039edd16368SStephen M. Cameron 		if (hpsa_noop(pdev) == 0)
4040edd16368SStephen M. Cameron 			break;
4041edd16368SStephen M. Cameron 		else
4042edd16368SStephen M. Cameron 			dev_warn(&pdev->dev, "no-op failed%s\n",
4043edd16368SStephen M. Cameron 					(i < 11 ? "; re-trying" : ""));
4044edd16368SStephen M. Cameron 	}
40454c2a8c40SStephen M. Cameron 	return 0;
4046edd16368SStephen M. Cameron }
4047edd16368SStephen M. Cameron 
40482e9d1b36SStephen M. Cameron static __devinit int hpsa_allocate_cmd_pool(struct ctlr_info *h)
40492e9d1b36SStephen M. Cameron {
40502e9d1b36SStephen M. Cameron 	h->cmd_pool_bits = kzalloc(
40512e9d1b36SStephen M. Cameron 		DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
40522e9d1b36SStephen M. Cameron 		sizeof(unsigned long), GFP_KERNEL);
40532e9d1b36SStephen M. Cameron 	h->cmd_pool = pci_alloc_consistent(h->pdev,
40542e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->cmd_pool),
40552e9d1b36SStephen M. Cameron 		    &(h->cmd_pool_dhandle));
40562e9d1b36SStephen M. Cameron 	h->errinfo_pool = pci_alloc_consistent(h->pdev,
40572e9d1b36SStephen M. Cameron 		    h->nr_cmds * sizeof(*h->errinfo_pool),
40582e9d1b36SStephen M. Cameron 		    &(h->errinfo_pool_dhandle));
40592e9d1b36SStephen M. Cameron 	if ((h->cmd_pool_bits == NULL)
40602e9d1b36SStephen M. Cameron 	    || (h->cmd_pool == NULL)
40612e9d1b36SStephen M. Cameron 	    || (h->errinfo_pool == NULL)) {
40622e9d1b36SStephen M. Cameron 		dev_err(&h->pdev->dev, "out of memory in %s", __func__);
40632e9d1b36SStephen M. Cameron 		return -ENOMEM;
40642e9d1b36SStephen M. Cameron 	}
40652e9d1b36SStephen M. Cameron 	return 0;
40662e9d1b36SStephen M. Cameron }
40672e9d1b36SStephen M. Cameron 
40682e9d1b36SStephen M. Cameron static void hpsa_free_cmd_pool(struct ctlr_info *h)
40692e9d1b36SStephen M. Cameron {
40702e9d1b36SStephen M. Cameron 	kfree(h->cmd_pool_bits);
40712e9d1b36SStephen M. Cameron 	if (h->cmd_pool)
40722e9d1b36SStephen M. Cameron 		pci_free_consistent(h->pdev,
40732e9d1b36SStephen M. Cameron 			    h->nr_cmds * sizeof(struct CommandList),
40742e9d1b36SStephen M. Cameron 			    h->cmd_pool, h->cmd_pool_dhandle);
40752e9d1b36SStephen M. Cameron 	if (h->errinfo_pool)
40762e9d1b36SStephen M. Cameron 		pci_free_consistent(h->pdev,
40772e9d1b36SStephen M. Cameron 			    h->nr_cmds * sizeof(struct ErrorInfo),
40782e9d1b36SStephen M. Cameron 			    h->errinfo_pool,
40792e9d1b36SStephen M. Cameron 			    h->errinfo_pool_dhandle);
40802e9d1b36SStephen M. Cameron }
40812e9d1b36SStephen M. Cameron 
40820ae01a32SStephen M. Cameron static int hpsa_request_irq(struct ctlr_info *h,
40830ae01a32SStephen M. Cameron 	irqreturn_t (*msixhandler)(int, void *),
40840ae01a32SStephen M. Cameron 	irqreturn_t (*intxhandler)(int, void *))
40850ae01a32SStephen M. Cameron {
40860ae01a32SStephen M. Cameron 	int rc;
40870ae01a32SStephen M. Cameron 
40880ae01a32SStephen M. Cameron 	if (h->msix_vector || h->msi_vector)
40890ae01a32SStephen M. Cameron 		rc = request_irq(h->intr[h->intr_mode], msixhandler,
409045bcf018SStephen M. Cameron 				0, h->devname, h);
40910ae01a32SStephen M. Cameron 	else
40920ae01a32SStephen M. Cameron 		rc = request_irq(h->intr[h->intr_mode], intxhandler,
409345bcf018SStephen M. Cameron 				IRQF_SHARED, h->devname, h);
40940ae01a32SStephen M. Cameron 	if (rc) {
40950ae01a32SStephen M. Cameron 		dev_err(&h->pdev->dev, "unable to get irq %d for %s\n",
40960ae01a32SStephen M. Cameron 		       h->intr[h->intr_mode], h->devname);
40970ae01a32SStephen M. Cameron 		return -ENODEV;
40980ae01a32SStephen M. Cameron 	}
40990ae01a32SStephen M. Cameron 	return 0;
41000ae01a32SStephen M. Cameron }
41010ae01a32SStephen M. Cameron 
410264670ac8SStephen M. Cameron static int __devinit hpsa_kdump_soft_reset(struct ctlr_info *h)
410364670ac8SStephen M. Cameron {
410464670ac8SStephen M. Cameron 	if (hpsa_send_host_reset(h, RAID_CTLR_LUNID,
410564670ac8SStephen M. Cameron 		HPSA_RESET_TYPE_CONTROLLER)) {
410664670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Resetting array controller failed.\n");
410764670ac8SStephen M. Cameron 		return -EIO;
410864670ac8SStephen M. Cameron 	}
410964670ac8SStephen M. Cameron 
411064670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
411164670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) {
411264670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
411364670ac8SStephen M. Cameron 		return -1;
411464670ac8SStephen M. Cameron 	}
411564670ac8SStephen M. Cameron 
411664670ac8SStephen M. Cameron 	dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
411764670ac8SStephen M. Cameron 	if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) {
411864670ac8SStephen M. Cameron 		dev_warn(&h->pdev->dev, "Board failed to become ready "
411964670ac8SStephen M. Cameron 			"after soft reset.\n");
412064670ac8SStephen M. Cameron 		return -1;
412164670ac8SStephen M. Cameron 	}
412264670ac8SStephen M. Cameron 
412364670ac8SStephen M. Cameron 	return 0;
412464670ac8SStephen M. Cameron }
412564670ac8SStephen M. Cameron 
412664670ac8SStephen M. Cameron static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
412764670ac8SStephen M. Cameron {
412864670ac8SStephen M. Cameron 	free_irq(h->intr[h->intr_mode], h);
412964670ac8SStephen M. Cameron #ifdef CONFIG_PCI_MSI
413064670ac8SStephen M. Cameron 	if (h->msix_vector)
413164670ac8SStephen M. Cameron 		pci_disable_msix(h->pdev);
413264670ac8SStephen M. Cameron 	else if (h->msi_vector)
413364670ac8SStephen M. Cameron 		pci_disable_msi(h->pdev);
413464670ac8SStephen M. Cameron #endif /* CONFIG_PCI_MSI */
413564670ac8SStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
413664670ac8SStephen M. Cameron 	hpsa_free_cmd_pool(h);
413764670ac8SStephen M. Cameron 	kfree(h->blockFetchTable);
413864670ac8SStephen M. Cameron 	pci_free_consistent(h->pdev, h->reply_pool_size,
413964670ac8SStephen M. Cameron 		h->reply_pool, h->reply_pool_dhandle);
414064670ac8SStephen M. Cameron 	if (h->vaddr)
414164670ac8SStephen M. Cameron 		iounmap(h->vaddr);
414264670ac8SStephen M. Cameron 	if (h->transtable)
414364670ac8SStephen M. Cameron 		iounmap(h->transtable);
414464670ac8SStephen M. Cameron 	if (h->cfgtable)
414564670ac8SStephen M. Cameron 		iounmap(h->cfgtable);
414664670ac8SStephen M. Cameron 	pci_release_regions(h->pdev);
414764670ac8SStephen M. Cameron 	kfree(h);
414864670ac8SStephen M. Cameron }
414964670ac8SStephen M. Cameron 
4150a0c12413SStephen M. Cameron static void remove_ctlr_from_lockup_detector_list(struct ctlr_info *h)
4151a0c12413SStephen M. Cameron {
4152a0c12413SStephen M. Cameron 	assert_spin_locked(&lockup_detector_lock);
4153a0c12413SStephen M. Cameron 	if (!hpsa_lockup_detector)
4154a0c12413SStephen M. Cameron 		return;
4155a0c12413SStephen M. Cameron 	if (h->lockup_detected)
4156a0c12413SStephen M. Cameron 		return; /* already stopped the lockup detector */
4157a0c12413SStephen M. Cameron 	list_del(&h->lockup_list);
4158a0c12413SStephen M. Cameron }
4159a0c12413SStephen M. Cameron 
4160a0c12413SStephen M. Cameron /* Called when controller lockup detected. */
4161a0c12413SStephen M. Cameron static void fail_all_cmds_on_list(struct ctlr_info *h, struct list_head *list)
4162a0c12413SStephen M. Cameron {
4163a0c12413SStephen M. Cameron 	struct CommandList *c = NULL;
4164a0c12413SStephen M. Cameron 
4165a0c12413SStephen M. Cameron 	assert_spin_locked(&h->lock);
4166a0c12413SStephen M. Cameron 	/* Mark all outstanding commands as failed and complete them. */
4167a0c12413SStephen M. Cameron 	while (!list_empty(list)) {
4168a0c12413SStephen M. Cameron 		c = list_entry(list->next, struct CommandList, list);
4169a0c12413SStephen M. Cameron 		c->err_info->CommandStatus = CMD_HARDWARE_ERR;
4170a0c12413SStephen M. Cameron 		finish_cmd(c, c->Header.Tag.lower);
4171a0c12413SStephen M. Cameron 	}
4172a0c12413SStephen M. Cameron }
4173a0c12413SStephen M. Cameron 
4174a0c12413SStephen M. Cameron static void controller_lockup_detected(struct ctlr_info *h)
4175a0c12413SStephen M. Cameron {
4176a0c12413SStephen M. Cameron 	unsigned long flags;
4177a0c12413SStephen M. Cameron 
4178a0c12413SStephen M. Cameron 	assert_spin_locked(&lockup_detector_lock);
4179a0c12413SStephen M. Cameron 	remove_ctlr_from_lockup_detector_list(h);
4180a0c12413SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
4181a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
4182a0c12413SStephen M. Cameron 	h->lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
4183a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
4184a0c12413SStephen M. Cameron 	dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x\n",
4185a0c12413SStephen M. Cameron 			h->lockup_detected);
4186a0c12413SStephen M. Cameron 	pci_disable_device(h->pdev);
4187a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
4188a0c12413SStephen M. Cameron 	fail_all_cmds_on_list(h, &h->cmpQ);
4189a0c12413SStephen M. Cameron 	fail_all_cmds_on_list(h, &h->reqQ);
4190a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
4191a0c12413SStephen M. Cameron }
4192a0c12413SStephen M. Cameron 
4193a0c12413SStephen M. Cameron #define HEARTBEAT_SAMPLE_INTERVAL (10 * HZ)
4194a0c12413SStephen M. Cameron #define HEARTBEAT_CHECK_MINIMUM_INTERVAL (HEARTBEAT_SAMPLE_INTERVAL / 2)
4195a0c12413SStephen M. Cameron 
4196a0c12413SStephen M. Cameron static void detect_controller_lockup(struct ctlr_info *h)
4197a0c12413SStephen M. Cameron {
4198a0c12413SStephen M. Cameron 	u64 now;
4199a0c12413SStephen M. Cameron 	u32 heartbeat;
4200a0c12413SStephen M. Cameron 	unsigned long flags;
4201a0c12413SStephen M. Cameron 
4202a0c12413SStephen M. Cameron 	assert_spin_locked(&lockup_detector_lock);
4203a0c12413SStephen M. Cameron 	now = get_jiffies_64();
4204a0c12413SStephen M. Cameron 	/* If we've received an interrupt recently, we're ok. */
4205a0c12413SStephen M. Cameron 	if (time_after64(h->last_intr_timestamp +
4206a0c12413SStephen M. Cameron 				(HEARTBEAT_CHECK_MINIMUM_INTERVAL), now))
4207a0c12413SStephen M. Cameron 		return;
4208a0c12413SStephen M. Cameron 
4209a0c12413SStephen M. Cameron 	/*
4210a0c12413SStephen M. Cameron 	 * If we've already checked the heartbeat recently, we're ok.
4211a0c12413SStephen M. Cameron 	 * This could happen if someone sends us a signal. We
4212a0c12413SStephen M. Cameron 	 * otherwise don't care about signals in this thread.
4213a0c12413SStephen M. Cameron 	 */
4214a0c12413SStephen M. Cameron 	if (time_after64(h->last_heartbeat_timestamp +
4215a0c12413SStephen M. Cameron 				(HEARTBEAT_CHECK_MINIMUM_INTERVAL), now))
4216a0c12413SStephen M. Cameron 		return;
4217a0c12413SStephen M. Cameron 
4218a0c12413SStephen M. Cameron 	/* If heartbeat has not changed since we last looked, we're not ok. */
4219a0c12413SStephen M. Cameron 	spin_lock_irqsave(&h->lock, flags);
4220a0c12413SStephen M. Cameron 	heartbeat = readl(&h->cfgtable->HeartBeat);
4221a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&h->lock, flags);
4222a0c12413SStephen M. Cameron 	if (h->last_heartbeat == heartbeat) {
4223a0c12413SStephen M. Cameron 		controller_lockup_detected(h);
4224a0c12413SStephen M. Cameron 		return;
4225a0c12413SStephen M. Cameron 	}
4226a0c12413SStephen M. Cameron 
4227a0c12413SStephen M. Cameron 	/* We're ok. */
4228a0c12413SStephen M. Cameron 	h->last_heartbeat = heartbeat;
4229a0c12413SStephen M. Cameron 	h->last_heartbeat_timestamp = now;
4230a0c12413SStephen M. Cameron }
4231a0c12413SStephen M. Cameron 
4232a0c12413SStephen M. Cameron static int detect_controller_lockup_thread(void *notused)
4233a0c12413SStephen M. Cameron {
4234a0c12413SStephen M. Cameron 	struct ctlr_info *h;
4235a0c12413SStephen M. Cameron 	unsigned long flags;
4236a0c12413SStephen M. Cameron 
4237a0c12413SStephen M. Cameron 	while (1) {
4238a0c12413SStephen M. Cameron 		struct list_head *this, *tmp;
4239a0c12413SStephen M. Cameron 
4240a0c12413SStephen M. Cameron 		schedule_timeout_interruptible(HEARTBEAT_SAMPLE_INTERVAL);
4241a0c12413SStephen M. Cameron 		if (kthread_should_stop())
4242a0c12413SStephen M. Cameron 			break;
4243a0c12413SStephen M. Cameron 		spin_lock_irqsave(&lockup_detector_lock, flags);
4244a0c12413SStephen M. Cameron 		list_for_each_safe(this, tmp, &hpsa_ctlr_list) {
4245a0c12413SStephen M. Cameron 			h = list_entry(this, struct ctlr_info, lockup_list);
4246a0c12413SStephen M. Cameron 			detect_controller_lockup(h);
4247a0c12413SStephen M. Cameron 		}
4248a0c12413SStephen M. Cameron 		spin_unlock_irqrestore(&lockup_detector_lock, flags);
4249a0c12413SStephen M. Cameron 	}
4250a0c12413SStephen M. Cameron 	return 0;
4251a0c12413SStephen M. Cameron }
4252a0c12413SStephen M. Cameron 
4253a0c12413SStephen M. Cameron static void add_ctlr_to_lockup_detector_list(struct ctlr_info *h)
4254a0c12413SStephen M. Cameron {
4255a0c12413SStephen M. Cameron 	unsigned long flags;
4256a0c12413SStephen M. Cameron 
4257a0c12413SStephen M. Cameron 	spin_lock_irqsave(&lockup_detector_lock, flags);
4258a0c12413SStephen M. Cameron 	list_add_tail(&h->lockup_list, &hpsa_ctlr_list);
4259a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&lockup_detector_lock, flags);
4260a0c12413SStephen M. Cameron }
4261a0c12413SStephen M. Cameron 
4262a0c12413SStephen M. Cameron static void start_controller_lockup_detector(struct ctlr_info *h)
4263a0c12413SStephen M. Cameron {
4264a0c12413SStephen M. Cameron 	/* Start the lockup detector thread if not already started */
4265a0c12413SStephen M. Cameron 	if (!hpsa_lockup_detector) {
4266a0c12413SStephen M. Cameron 		spin_lock_init(&lockup_detector_lock);
4267a0c12413SStephen M. Cameron 		hpsa_lockup_detector =
4268a0c12413SStephen M. Cameron 			kthread_run(detect_controller_lockup_thread,
4269f79cfec6SStephen M. Cameron 						NULL, HPSA);
4270a0c12413SStephen M. Cameron 	}
4271a0c12413SStephen M. Cameron 	if (!hpsa_lockup_detector) {
4272a0c12413SStephen M. Cameron 		dev_warn(&h->pdev->dev,
4273a0c12413SStephen M. Cameron 			"Could not start lockup detector thread\n");
4274a0c12413SStephen M. Cameron 		return;
4275a0c12413SStephen M. Cameron 	}
4276a0c12413SStephen M. Cameron 	add_ctlr_to_lockup_detector_list(h);
4277a0c12413SStephen M. Cameron }
4278a0c12413SStephen M. Cameron 
4279a0c12413SStephen M. Cameron static void stop_controller_lockup_detector(struct ctlr_info *h)
4280a0c12413SStephen M. Cameron {
4281a0c12413SStephen M. Cameron 	unsigned long flags;
4282a0c12413SStephen M. Cameron 
4283a0c12413SStephen M. Cameron 	spin_lock_irqsave(&lockup_detector_lock, flags);
4284a0c12413SStephen M. Cameron 	remove_ctlr_from_lockup_detector_list(h);
4285a0c12413SStephen M. Cameron 	/* If the list of ctlr's to monitor is empty, stop the thread */
4286a0c12413SStephen M. Cameron 	if (list_empty(&hpsa_ctlr_list)) {
4287775bf277SStephen M. Cameron 		spin_unlock_irqrestore(&lockup_detector_lock, flags);
4288a0c12413SStephen M. Cameron 		kthread_stop(hpsa_lockup_detector);
4289775bf277SStephen M. Cameron 		spin_lock_irqsave(&lockup_detector_lock, flags);
4290a0c12413SStephen M. Cameron 		hpsa_lockup_detector = NULL;
4291a0c12413SStephen M. Cameron 	}
4292a0c12413SStephen M. Cameron 	spin_unlock_irqrestore(&lockup_detector_lock, flags);
4293a0c12413SStephen M. Cameron }
4294a0c12413SStephen M. Cameron 
42954c2a8c40SStephen M. Cameron static int __devinit hpsa_init_one(struct pci_dev *pdev,
42964c2a8c40SStephen M. Cameron 				    const struct pci_device_id *ent)
42974c2a8c40SStephen M. Cameron {
42984c2a8c40SStephen M. Cameron 	int dac, rc;
42994c2a8c40SStephen M. Cameron 	struct ctlr_info *h;
430064670ac8SStephen M. Cameron 	int try_soft_reset = 0;
430164670ac8SStephen M. Cameron 	unsigned long flags;
43024c2a8c40SStephen M. Cameron 
43034c2a8c40SStephen M. Cameron 	if (number_of_controllers == 0)
43044c2a8c40SStephen M. Cameron 		printk(KERN_INFO DRIVER_NAME "\n");
43054c2a8c40SStephen M. Cameron 
43064c2a8c40SStephen M. Cameron 	rc = hpsa_init_reset_devices(pdev);
430764670ac8SStephen M. Cameron 	if (rc) {
430864670ac8SStephen M. Cameron 		if (rc != -ENOTSUPP)
43094c2a8c40SStephen M. Cameron 			return rc;
431064670ac8SStephen M. Cameron 		/* If the reset fails in a particular way (it has no way to do
431164670ac8SStephen M. Cameron 		 * a proper hard reset, so returns -ENOTSUPP) we can try to do
431264670ac8SStephen M. Cameron 		 * a soft reset once we get the controller configured up to the
431364670ac8SStephen M. Cameron 		 * point that it can accept a command.
431464670ac8SStephen M. Cameron 		 */
431564670ac8SStephen M. Cameron 		try_soft_reset = 1;
431664670ac8SStephen M. Cameron 		rc = 0;
431764670ac8SStephen M. Cameron 	}
431864670ac8SStephen M. Cameron 
431964670ac8SStephen M. Cameron reinit_after_soft_reset:
43204c2a8c40SStephen M. Cameron 
4321303932fdSDon Brace 	/* Command structures must be aligned on a 32-byte boundary because
4322303932fdSDon Brace 	 * the 5 lower bits of the address are used by the hardware. and by
4323303932fdSDon Brace 	 * the driver.  See comments in hpsa.h for more info.
4324303932fdSDon Brace 	 */
4325303932fdSDon Brace #define COMMANDLIST_ALIGNMENT 32
4326303932fdSDon Brace 	BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
4327edd16368SStephen M. Cameron 	h = kzalloc(sizeof(*h), GFP_KERNEL);
4328edd16368SStephen M. Cameron 	if (!h)
4329ecd9aad4SStephen M. Cameron 		return -ENOMEM;
4330edd16368SStephen M. Cameron 
433155c06c71SStephen M. Cameron 	h->pdev = pdev;
4332a9a3a273SStephen M. Cameron 	h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
43339e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&h->cmpQ);
43349e0fc764SStephen M. Cameron 	INIT_LIST_HEAD(&h->reqQ);
43356eaf46fdSStephen M. Cameron 	spin_lock_init(&h->lock);
43366eaf46fdSStephen M. Cameron 	spin_lock_init(&h->scan_lock);
433755c06c71SStephen M. Cameron 	rc = hpsa_pci_init(h);
4338ecd9aad4SStephen M. Cameron 	if (rc != 0)
4339edd16368SStephen M. Cameron 		goto clean1;
4340edd16368SStephen M. Cameron 
4341f79cfec6SStephen M. Cameron 	sprintf(h->devname, HPSA "%d", number_of_controllers);
4342edd16368SStephen M. Cameron 	h->ctlr = number_of_controllers;
4343edd16368SStephen M. Cameron 	number_of_controllers++;
4344edd16368SStephen M. Cameron 
4345edd16368SStephen M. Cameron 	/* configure PCI DMA stuff */
4346ecd9aad4SStephen M. Cameron 	rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4347ecd9aad4SStephen M. Cameron 	if (rc == 0) {
4348edd16368SStephen M. Cameron 		dac = 1;
4349ecd9aad4SStephen M. Cameron 	} else {
4350ecd9aad4SStephen M. Cameron 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4351ecd9aad4SStephen M. Cameron 		if (rc == 0) {
4352edd16368SStephen M. Cameron 			dac = 0;
4353ecd9aad4SStephen M. Cameron 		} else {
4354edd16368SStephen M. Cameron 			dev_err(&pdev->dev, "no suitable DMA available\n");
4355edd16368SStephen M. Cameron 			goto clean1;
4356edd16368SStephen M. Cameron 		}
4357ecd9aad4SStephen M. Cameron 	}
4358edd16368SStephen M. Cameron 
4359edd16368SStephen M. Cameron 	/* make sure the board interrupts are off */
4360edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
436110f66018SStephen M. Cameron 
43620ae01a32SStephen M. Cameron 	if (hpsa_request_irq(h, do_hpsa_intr_msi, do_hpsa_intr_intx))
4363edd16368SStephen M. Cameron 		goto clean2;
4364303932fdSDon Brace 	dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
4365303932fdSDon Brace 	       h->devname, pdev->device,
4366a9a3a273SStephen M. Cameron 	       h->intr[h->intr_mode], dac ? "" : " not");
43672e9d1b36SStephen M. Cameron 	if (hpsa_allocate_cmd_pool(h))
4368edd16368SStephen M. Cameron 		goto clean4;
436933a2ffceSStephen M. Cameron 	if (hpsa_allocate_sg_chain_blocks(h))
437033a2ffceSStephen M. Cameron 		goto clean4;
4371a08a8471SStephen M. Cameron 	init_waitqueue_head(&h->scan_wait_queue);
4372a08a8471SStephen M. Cameron 	h->scan_finished = 1; /* no scan currently in progress */
4373edd16368SStephen M. Cameron 
4374edd16368SStephen M. Cameron 	pci_set_drvdata(pdev, h);
43759a41338eSStephen M. Cameron 	h->ndevices = 0;
43769a41338eSStephen M. Cameron 	h->scsi_host = NULL;
43779a41338eSStephen M. Cameron 	spin_lock_init(&h->devlock);
437864670ac8SStephen M. Cameron 	hpsa_put_ctlr_into_performant_mode(h);
437964670ac8SStephen M. Cameron 
438064670ac8SStephen M. Cameron 	/* At this point, the controller is ready to take commands.
438164670ac8SStephen M. Cameron 	 * Now, if reset_devices and the hard reset didn't work, try
438264670ac8SStephen M. Cameron 	 * the soft reset and see if that works.
438364670ac8SStephen M. Cameron 	 */
438464670ac8SStephen M. Cameron 	if (try_soft_reset) {
438564670ac8SStephen M. Cameron 
438664670ac8SStephen M. Cameron 		/* This is kind of gross.  We may or may not get a completion
438764670ac8SStephen M. Cameron 		 * from the soft reset command, and if we do, then the value
438864670ac8SStephen M. Cameron 		 * from the fifo may or may not be valid.  So, we wait 10 secs
438964670ac8SStephen M. Cameron 		 * after the reset throwing away any completions we get during
439064670ac8SStephen M. Cameron 		 * that time.  Unregister the interrupt handler and register
439164670ac8SStephen M. Cameron 		 * fake ones to scoop up any residual completions.
439264670ac8SStephen M. Cameron 		 */
439364670ac8SStephen M. Cameron 		spin_lock_irqsave(&h->lock, flags);
439464670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
439564670ac8SStephen M. Cameron 		spin_unlock_irqrestore(&h->lock, flags);
439664670ac8SStephen M. Cameron 		free_irq(h->intr[h->intr_mode], h);
439764670ac8SStephen M. Cameron 		rc = hpsa_request_irq(h, hpsa_msix_discard_completions,
439864670ac8SStephen M. Cameron 					hpsa_intx_discard_completions);
439964670ac8SStephen M. Cameron 		if (rc) {
440064670ac8SStephen M. Cameron 			dev_warn(&h->pdev->dev, "Failed to request_irq after "
440164670ac8SStephen M. Cameron 				"soft reset.\n");
440264670ac8SStephen M. Cameron 			goto clean4;
440364670ac8SStephen M. Cameron 		}
440464670ac8SStephen M. Cameron 
440564670ac8SStephen M. Cameron 		rc = hpsa_kdump_soft_reset(h);
440664670ac8SStephen M. Cameron 		if (rc)
440764670ac8SStephen M. Cameron 			/* Neither hard nor soft reset worked, we're hosed. */
440864670ac8SStephen M. Cameron 			goto clean4;
440964670ac8SStephen M. Cameron 
441064670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev, "Board READY.\n");
441164670ac8SStephen M. Cameron 		dev_info(&h->pdev->dev,
441264670ac8SStephen M. Cameron 			"Waiting for stale completions to drain.\n");
441364670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_ON);
441464670ac8SStephen M. Cameron 		msleep(10000);
441564670ac8SStephen M. Cameron 		h->access.set_intr_mask(h, HPSA_INTR_OFF);
441664670ac8SStephen M. Cameron 
441764670ac8SStephen M. Cameron 		rc = controller_reset_failed(h->cfgtable);
441864670ac8SStephen M. Cameron 		if (rc)
441964670ac8SStephen M. Cameron 			dev_info(&h->pdev->dev,
442064670ac8SStephen M. Cameron 				"Soft reset appears to have failed.\n");
442164670ac8SStephen M. Cameron 
442264670ac8SStephen M. Cameron 		/* since the controller's reset, we have to go back and re-init
442364670ac8SStephen M. Cameron 		 * everything.  Easiest to just forget what we've done and do it
442464670ac8SStephen M. Cameron 		 * all over again.
442564670ac8SStephen M. Cameron 		 */
442664670ac8SStephen M. Cameron 		hpsa_undo_allocations_after_kdump_soft_reset(h);
442764670ac8SStephen M. Cameron 		try_soft_reset = 0;
442864670ac8SStephen M. Cameron 		if (rc)
442964670ac8SStephen M. Cameron 			/* don't go to clean4, we already unallocated */
443064670ac8SStephen M. Cameron 			return -ENODEV;
443164670ac8SStephen M. Cameron 
443264670ac8SStephen M. Cameron 		goto reinit_after_soft_reset;
443364670ac8SStephen M. Cameron 	}
4434edd16368SStephen M. Cameron 
4435edd16368SStephen M. Cameron 	/* Turn the interrupts on so we can service requests */
4436edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_ON);
4437edd16368SStephen M. Cameron 
4438339b2b14SStephen M. Cameron 	hpsa_hba_inquiry(h);
4439edd16368SStephen M. Cameron 	hpsa_register_scsi(h);	/* hook ourselves into SCSI subsystem */
4440a0c12413SStephen M. Cameron 	start_controller_lockup_detector(h);
4441edd16368SStephen M. Cameron 	return 1;
4442edd16368SStephen M. Cameron 
4443edd16368SStephen M. Cameron clean4:
444433a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
44452e9d1b36SStephen M. Cameron 	hpsa_free_cmd_pool(h);
4446a9a3a273SStephen M. Cameron 	free_irq(h->intr[h->intr_mode], h);
4447edd16368SStephen M. Cameron clean2:
4448edd16368SStephen M. Cameron clean1:
4449edd16368SStephen M. Cameron 	kfree(h);
4450ecd9aad4SStephen M. Cameron 	return rc;
4451edd16368SStephen M. Cameron }
4452edd16368SStephen M. Cameron 
4453edd16368SStephen M. Cameron static void hpsa_flush_cache(struct ctlr_info *h)
4454edd16368SStephen M. Cameron {
4455edd16368SStephen M. Cameron 	char *flush_buf;
4456edd16368SStephen M. Cameron 	struct CommandList *c;
4457edd16368SStephen M. Cameron 
4458edd16368SStephen M. Cameron 	flush_buf = kzalloc(4, GFP_KERNEL);
4459edd16368SStephen M. Cameron 	if (!flush_buf)
4460edd16368SStephen M. Cameron 		return;
4461edd16368SStephen M. Cameron 
4462edd16368SStephen M. Cameron 	c = cmd_special_alloc(h);
4463edd16368SStephen M. Cameron 	if (!c) {
4464edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
4465edd16368SStephen M. Cameron 		goto out_of_memory;
4466edd16368SStephen M. Cameron 	}
4467edd16368SStephen M. Cameron 	fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
4468edd16368SStephen M. Cameron 		RAID_CTLR_LUNID, TYPE_CMD);
4469edd16368SStephen M. Cameron 	hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
4470edd16368SStephen M. Cameron 	if (c->err_info->CommandStatus != 0)
4471edd16368SStephen M. Cameron 		dev_warn(&h->pdev->dev,
4472edd16368SStephen M. Cameron 			"error flushing cache on controller\n");
4473edd16368SStephen M. Cameron 	cmd_special_free(h, c);
4474edd16368SStephen M. Cameron out_of_memory:
4475edd16368SStephen M. Cameron 	kfree(flush_buf);
4476edd16368SStephen M. Cameron }
4477edd16368SStephen M. Cameron 
4478edd16368SStephen M. Cameron static void hpsa_shutdown(struct pci_dev *pdev)
4479edd16368SStephen M. Cameron {
4480edd16368SStephen M. Cameron 	struct ctlr_info *h;
4481edd16368SStephen M. Cameron 
4482edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
4483edd16368SStephen M. Cameron 	/* Turn board interrupts off  and send the flush cache command
4484edd16368SStephen M. Cameron 	 * sendcmd will turn off interrupt, and send the flush...
4485edd16368SStephen M. Cameron 	 * To write all data in the battery backed cache to disks
4486edd16368SStephen M. Cameron 	 */
4487edd16368SStephen M. Cameron 	hpsa_flush_cache(h);
4488edd16368SStephen M. Cameron 	h->access.set_intr_mask(h, HPSA_INTR_OFF);
4489a9a3a273SStephen M. Cameron 	free_irq(h->intr[h->intr_mode], h);
4490edd16368SStephen M. Cameron #ifdef CONFIG_PCI_MSI
4491edd16368SStephen M. Cameron 	if (h->msix_vector)
4492edd16368SStephen M. Cameron 		pci_disable_msix(h->pdev);
4493edd16368SStephen M. Cameron 	else if (h->msi_vector)
4494edd16368SStephen M. Cameron 		pci_disable_msi(h->pdev);
4495edd16368SStephen M. Cameron #endif				/* CONFIG_PCI_MSI */
4496edd16368SStephen M. Cameron }
4497edd16368SStephen M. Cameron 
449855e14e76SStephen M. Cameron static void __devexit hpsa_free_device_info(struct ctlr_info *h)
449955e14e76SStephen M. Cameron {
450055e14e76SStephen M. Cameron 	int i;
450155e14e76SStephen M. Cameron 
450255e14e76SStephen M. Cameron 	for (i = 0; i < h->ndevices; i++)
450355e14e76SStephen M. Cameron 		kfree(h->dev[i]);
450455e14e76SStephen M. Cameron }
450555e14e76SStephen M. Cameron 
4506edd16368SStephen M. Cameron static void __devexit hpsa_remove_one(struct pci_dev *pdev)
4507edd16368SStephen M. Cameron {
4508edd16368SStephen M. Cameron 	struct ctlr_info *h;
4509edd16368SStephen M. Cameron 
4510edd16368SStephen M. Cameron 	if (pci_get_drvdata(pdev) == NULL) {
4511edd16368SStephen M. Cameron 		dev_err(&pdev->dev, "unable to remove device\n");
4512edd16368SStephen M. Cameron 		return;
4513edd16368SStephen M. Cameron 	}
4514edd16368SStephen M. Cameron 	h = pci_get_drvdata(pdev);
4515a0c12413SStephen M. Cameron 	stop_controller_lockup_detector(h);
4516edd16368SStephen M. Cameron 	hpsa_unregister_scsi(h);	/* unhook from SCSI subsystem */
4517edd16368SStephen M. Cameron 	hpsa_shutdown(pdev);
4518edd16368SStephen M. Cameron 	iounmap(h->vaddr);
4519204892e9SStephen M. Cameron 	iounmap(h->transtable);
4520204892e9SStephen M. Cameron 	iounmap(h->cfgtable);
452155e14e76SStephen M. Cameron 	hpsa_free_device_info(h);
452233a2ffceSStephen M. Cameron 	hpsa_free_sg_chain_blocks(h);
4523edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev,
4524edd16368SStephen M. Cameron 		h->nr_cmds * sizeof(struct CommandList),
4525edd16368SStephen M. Cameron 		h->cmd_pool, h->cmd_pool_dhandle);
4526edd16368SStephen M. Cameron 	pci_free_consistent(h->pdev,
4527edd16368SStephen M. Cameron 		h->nr_cmds * sizeof(struct ErrorInfo),
4528edd16368SStephen M. Cameron 		h->errinfo_pool, h->errinfo_pool_dhandle);
4529303932fdSDon Brace 	pci_free_consistent(h->pdev, h->reply_pool_size,
4530303932fdSDon Brace 		h->reply_pool, h->reply_pool_dhandle);
4531edd16368SStephen M. Cameron 	kfree(h->cmd_pool_bits);
4532303932fdSDon Brace 	kfree(h->blockFetchTable);
4533339b2b14SStephen M. Cameron 	kfree(h->hba_inquiry_data);
4534f0bd0b68SStephen M. Cameron 	pci_disable_device(pdev);
4535edd16368SStephen M. Cameron 	pci_release_regions(pdev);
4536edd16368SStephen M. Cameron 	pci_set_drvdata(pdev, NULL);
4537edd16368SStephen M. Cameron 	kfree(h);
4538edd16368SStephen M. Cameron }
4539edd16368SStephen M. Cameron 
4540edd16368SStephen M. Cameron static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
4541edd16368SStephen M. Cameron 	__attribute__((unused)) pm_message_t state)
4542edd16368SStephen M. Cameron {
4543edd16368SStephen M. Cameron 	return -ENOSYS;
4544edd16368SStephen M. Cameron }
4545edd16368SStephen M. Cameron 
4546edd16368SStephen M. Cameron static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
4547edd16368SStephen M. Cameron {
4548edd16368SStephen M. Cameron 	return -ENOSYS;
4549edd16368SStephen M. Cameron }
4550edd16368SStephen M. Cameron 
4551edd16368SStephen M. Cameron static struct pci_driver hpsa_pci_driver = {
4552f79cfec6SStephen M. Cameron 	.name = HPSA,
4553edd16368SStephen M. Cameron 	.probe = hpsa_init_one,
4554edd16368SStephen M. Cameron 	.remove = __devexit_p(hpsa_remove_one),
4555edd16368SStephen M. Cameron 	.id_table = hpsa_pci_device_id,	/* id_table */
4556edd16368SStephen M. Cameron 	.shutdown = hpsa_shutdown,
4557edd16368SStephen M. Cameron 	.suspend = hpsa_suspend,
4558edd16368SStephen M. Cameron 	.resume = hpsa_resume,
4559edd16368SStephen M. Cameron };
4560edd16368SStephen M. Cameron 
4561303932fdSDon Brace /* Fill in bucket_map[], given nsgs (the max number of
4562303932fdSDon Brace  * scatter gather elements supported) and bucket[],
4563303932fdSDon Brace  * which is an array of 8 integers.  The bucket[] array
4564303932fdSDon Brace  * contains 8 different DMA transfer sizes (in 16
4565303932fdSDon Brace  * byte increments) which the controller uses to fetch
4566303932fdSDon Brace  * commands.  This function fills in bucket_map[], which
4567303932fdSDon Brace  * maps a given number of scatter gather elements to one of
4568303932fdSDon Brace  * the 8 DMA transfer sizes.  The point of it is to allow the
4569303932fdSDon Brace  * controller to only do as much DMA as needed to fetch the
4570303932fdSDon Brace  * command, with the DMA transfer size encoded in the lower
4571303932fdSDon Brace  * bits of the command address.
4572303932fdSDon Brace  */
4573303932fdSDon Brace static void  calc_bucket_map(int bucket[], int num_buckets,
4574303932fdSDon Brace 	int nsgs, int *bucket_map)
4575303932fdSDon Brace {
4576303932fdSDon Brace 	int i, j, b, size;
4577303932fdSDon Brace 
4578303932fdSDon Brace 	/* even a command with 0 SGs requires 4 blocks */
4579303932fdSDon Brace #define MINIMUM_TRANSFER_BLOCKS 4
4580303932fdSDon Brace #define NUM_BUCKETS 8
4581303932fdSDon Brace 	/* Note, bucket_map must have nsgs+1 entries. */
4582303932fdSDon Brace 	for (i = 0; i <= nsgs; i++) {
4583303932fdSDon Brace 		/* Compute size of a command with i SG entries */
4584303932fdSDon Brace 		size = i + MINIMUM_TRANSFER_BLOCKS;
4585303932fdSDon Brace 		b = num_buckets; /* Assume the biggest bucket */
4586303932fdSDon Brace 		/* Find the bucket that is just big enough */
4587303932fdSDon Brace 		for (j = 0; j < 8; j++) {
4588303932fdSDon Brace 			if (bucket[j] >= size) {
4589303932fdSDon Brace 				b = j;
4590303932fdSDon Brace 				break;
4591303932fdSDon Brace 			}
4592303932fdSDon Brace 		}
4593303932fdSDon Brace 		/* for a command with i SG entries, use bucket b. */
4594303932fdSDon Brace 		bucket_map[i] = b;
4595303932fdSDon Brace 	}
4596303932fdSDon Brace }
4597303932fdSDon Brace 
4598960a30e7SStephen M. Cameron static __devinit void hpsa_enter_performant_mode(struct ctlr_info *h,
4599960a30e7SStephen M. Cameron 	u32 use_short_tags)
4600303932fdSDon Brace {
46016c311b57SStephen M. Cameron 	int i;
46026c311b57SStephen M. Cameron 	unsigned long register_value;
4603def342bdSStephen M. Cameron 
4604def342bdSStephen M. Cameron 	/* This is a bit complicated.  There are 8 registers on
4605def342bdSStephen M. Cameron 	 * the controller which we write to to tell it 8 different
4606def342bdSStephen M. Cameron 	 * sizes of commands which there may be.  It's a way of
4607def342bdSStephen M. Cameron 	 * reducing the DMA done to fetch each command.  Encoded into
4608def342bdSStephen M. Cameron 	 * each command's tag are 3 bits which communicate to the controller
4609def342bdSStephen M. Cameron 	 * which of the eight sizes that command fits within.  The size of
4610def342bdSStephen M. Cameron 	 * each command depends on how many scatter gather entries there are.
4611def342bdSStephen M. Cameron 	 * Each SG entry requires 16 bytes.  The eight registers are programmed
4612def342bdSStephen M. Cameron 	 * with the number of 16-byte blocks a command of that size requires.
4613def342bdSStephen M. Cameron 	 * The smallest command possible requires 5 such 16 byte blocks.
4614d66ae08bSStephen M. Cameron 	 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
4615def342bdSStephen M. Cameron 	 * blocks.  Note, this only extends to the SG entries contained
4616def342bdSStephen M. Cameron 	 * within the command block, and does not extend to chained blocks
4617def342bdSStephen M. Cameron 	 * of SG elements.   bft[] contains the eight values we write to
4618def342bdSStephen M. Cameron 	 * the registers.  They are not evenly distributed, but have more
4619def342bdSStephen M. Cameron 	 * sizes for small commands, and fewer sizes for larger commands.
4620def342bdSStephen M. Cameron 	 */
4621d66ae08bSStephen M. Cameron 	int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
4622d66ae08bSStephen M. Cameron 	BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
4623303932fdSDon Brace 	/*  5 = 1 s/g entry or 4k
4624303932fdSDon Brace 	 *  6 = 2 s/g entry or 8k
4625303932fdSDon Brace 	 *  8 = 4 s/g entry or 16k
4626303932fdSDon Brace 	 * 10 = 6 s/g entry or 24k
4627303932fdSDon Brace 	 */
4628303932fdSDon Brace 
4629303932fdSDon Brace 	h->reply_pool_wraparound = 1; /* spec: init to 1 */
4630303932fdSDon Brace 
4631303932fdSDon Brace 	/* Controller spec: zero out this buffer. */
4632303932fdSDon Brace 	memset(h->reply_pool, 0, h->reply_pool_size);
4633303932fdSDon Brace 	h->reply_pool_head = h->reply_pool;
4634303932fdSDon Brace 
4635d66ae08bSStephen M. Cameron 	bft[7] = SG_ENTRIES_IN_CMD + 4;
4636d66ae08bSStephen M. Cameron 	calc_bucket_map(bft, ARRAY_SIZE(bft),
4637d66ae08bSStephen M. Cameron 				SG_ENTRIES_IN_CMD, h->blockFetchTable);
4638303932fdSDon Brace 	for (i = 0; i < 8; i++)
4639303932fdSDon Brace 		writel(bft[i], &h->transtable->BlockFetch[i]);
4640303932fdSDon Brace 
4641303932fdSDon Brace 	/* size of controller ring buffer */
4642303932fdSDon Brace 	writel(h->max_commands, &h->transtable->RepQSize);
4643303932fdSDon Brace 	writel(1, &h->transtable->RepQCount);
4644303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrLow32);
4645303932fdSDon Brace 	writel(0, &h->transtable->RepQCtrAddrHigh32);
4646303932fdSDon Brace 	writel(h->reply_pool_dhandle, &h->transtable->RepQAddr0Low32);
4647303932fdSDon Brace 	writel(0, &h->transtable->RepQAddr0High32);
4648960a30e7SStephen M. Cameron 	writel(CFGTBL_Trans_Performant | use_short_tags,
4649303932fdSDon Brace 		&(h->cfgtable->HostWrite.TransportRequest));
4650303932fdSDon Brace 	writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
46513f4336f3SStephen M. Cameron 	hpsa_wait_for_mode_change_ack(h);
4652303932fdSDon Brace 	register_value = readl(&(h->cfgtable->TransportActive));
4653303932fdSDon Brace 	if (!(register_value & CFGTBL_Trans_Performant)) {
4654303932fdSDon Brace 		dev_warn(&h->pdev->dev, "unable to get board into"
4655303932fdSDon Brace 					" performant mode\n");
4656303932fdSDon Brace 		return;
4657303932fdSDon Brace 	}
4658960a30e7SStephen M. Cameron 	/* Change the access methods to the performant access methods */
4659960a30e7SStephen M. Cameron 	h->access = SA5_performant_access;
4660960a30e7SStephen M. Cameron 	h->transMethod = CFGTBL_Trans_Performant;
46616c311b57SStephen M. Cameron }
46626c311b57SStephen M. Cameron 
46636c311b57SStephen M. Cameron static __devinit void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
46646c311b57SStephen M. Cameron {
46656c311b57SStephen M. Cameron 	u32 trans_support;
46666c311b57SStephen M. Cameron 
466702ec19c8SStephen M. Cameron 	if (hpsa_simple_mode)
466802ec19c8SStephen M. Cameron 		return;
466902ec19c8SStephen M. Cameron 
46706c311b57SStephen M. Cameron 	trans_support = readl(&(h->cfgtable->TransportSupport));
46716c311b57SStephen M. Cameron 	if (!(trans_support & PERFORMANT_MODE))
46726c311b57SStephen M. Cameron 		return;
46736c311b57SStephen M. Cameron 
4674cba3d38bSStephen M. Cameron 	hpsa_get_max_perf_mode_cmds(h);
46756c311b57SStephen M. Cameron 	/* Performant mode ring buffer and supporting data structures */
46766c311b57SStephen M. Cameron 	h->reply_pool_size = h->max_commands * sizeof(u64);
46776c311b57SStephen M. Cameron 	h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size,
46786c311b57SStephen M. Cameron 				&(h->reply_pool_dhandle));
46796c311b57SStephen M. Cameron 
46806c311b57SStephen M. Cameron 	/* Need a block fetch table for performant mode */
4681d66ae08bSStephen M. Cameron 	h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
46826c311b57SStephen M. Cameron 				sizeof(u32)), GFP_KERNEL);
46836c311b57SStephen M. Cameron 
46846c311b57SStephen M. Cameron 	if ((h->reply_pool == NULL)
46856c311b57SStephen M. Cameron 		|| (h->blockFetchTable == NULL))
46866c311b57SStephen M. Cameron 		goto clean_up;
46876c311b57SStephen M. Cameron 
4688960a30e7SStephen M. Cameron 	hpsa_enter_performant_mode(h,
4689960a30e7SStephen M. Cameron 		trans_support & CFGTBL_Trans_use_short_tags);
4690303932fdSDon Brace 
4691303932fdSDon Brace 	return;
4692303932fdSDon Brace 
4693303932fdSDon Brace clean_up:
4694303932fdSDon Brace 	if (h->reply_pool)
4695303932fdSDon Brace 		pci_free_consistent(h->pdev, h->reply_pool_size,
4696303932fdSDon Brace 			h->reply_pool, h->reply_pool_dhandle);
4697303932fdSDon Brace 	kfree(h->blockFetchTable);
4698303932fdSDon Brace }
4699303932fdSDon Brace 
4700edd16368SStephen M. Cameron /*
4701edd16368SStephen M. Cameron  *  This is it.  Register the PCI driver information for the cards we control
4702edd16368SStephen M. Cameron  *  the OS will call our registered routines when it finds one of our cards.
4703edd16368SStephen M. Cameron  */
4704edd16368SStephen M. Cameron static int __init hpsa_init(void)
4705edd16368SStephen M. Cameron {
470631468401SMike Miller 	return pci_register_driver(&hpsa_pci_driver);
4707edd16368SStephen M. Cameron }
4708edd16368SStephen M. Cameron 
4709edd16368SStephen M. Cameron static void __exit hpsa_cleanup(void)
4710edd16368SStephen M. Cameron {
4711edd16368SStephen M. Cameron 	pci_unregister_driver(&hpsa_pci_driver);
4712edd16368SStephen M. Cameron }
4713edd16368SStephen M. Cameron 
4714edd16368SStephen M. Cameron module_init(hpsa_init);
4715edd16368SStephen M. Cameron module_exit(hpsa_cleanup);
4716