blob: 092d06d4b9c37a3f346006a267e56e5b12ca9fea [file] [log] [blame]
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001/*
2 * Disk Array driver for HP Smart Array SAS controllers
Scott Teel51c35132014-02-18 13:57:26 -06003 * Copyright 2000, 2014 Hewlett-Packard Development Company, L.P.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
12 * NON INFRINGEMENT. See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 *
18 * Questions/Comments/Bugfixes to iss_storagedev@hp.com
19 *
20 */
21
22#include <linux/module.h>
23#include <linux/interrupt.h>
24#include <linux/types.h>
25#include <linux/pci.h>
Matthew Garrette5a44df2011-11-11 11:14:23 -050026#include <linux/pci-aspm.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080027#include <linux/kernel.h>
28#include <linux/slab.h>
29#include <linux/delay.h>
30#include <linux/fs.h>
31#include <linux/timer.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080032#include <linux/init.h>
33#include <linux/spinlock.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080034#include <linux/compat.h>
35#include <linux/blktrace_api.h>
36#include <linux/uaccess.h>
37#include <linux/io.h>
38#include <linux/dma-mapping.h>
39#include <linux/completion.h>
40#include <linux/moduleparam.h>
41#include <scsi/scsi.h>
42#include <scsi/scsi_cmnd.h>
43#include <scsi/scsi_device.h>
44#include <scsi/scsi_host.h>
Stephen M. Cameron667e23d2010-02-25 14:02:51 -060045#include <scsi/scsi_tcq.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080046#include <linux/cciss_ioctl.h>
47#include <linux/string.h>
48#include <linux/bitmap.h>
Arun Sharma600634972011-07-26 16:09:06 -070049#include <linux/atomic.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080050#include <linux/kthread.h>
Stephen M. Camerona0c12412011-10-26 16:22:04 -050051#include <linux/jiffies.h>
Stephen M. Cameron283b4a92014-02-18 13:55:33 -060052#include <asm/div64.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080053#include "hpsa_cmd.h"
54#include "hpsa.h"
55
56/* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */
Mike Millere481cce2013-09-04 15:12:27 -050057#define HPSA_DRIVER_VERSION "3.4.0-1"
Stephen M. Cameronedd16362009-12-08 14:09:11 -080058#define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -060059#define HPSA "hpsa"
Stephen M. Cameronedd16362009-12-08 14:09:11 -080060
61/* How long to wait (in milliseconds) for board to go into simple mode */
62#define MAX_CONFIG_WAIT 30000
63#define MAX_IOCTL_CONFIG_WAIT 1000
64
65/*define how many times we will try a command because of bus resets */
66#define MAX_CMD_RETRIES 3
67
68/* Embedded module documentation macros - see modules.h */
69MODULE_AUTHOR("Hewlett-Packard Company");
70MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
71 HPSA_DRIVER_VERSION);
72MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
73MODULE_VERSION(HPSA_DRIVER_VERSION);
74MODULE_LICENSE("GPL");
75
76static int hpsa_allow_any;
77module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
78MODULE_PARM_DESC(hpsa_allow_any,
79 "Allow hpsa driver to access unknown HP Smart Array hardware");
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -060080static int hpsa_simple_mode;
81module_param(hpsa_simple_mode, int, S_IRUGO|S_IWUSR);
82MODULE_PARM_DESC(hpsa_simple_mode,
83 "Use 'simple mode' rather than 'performant mode'");
Stephen M. Cameronedd16362009-12-08 14:09:11 -080084
85/* define the PCI info for the cards we can control */
86static const struct pci_device_id hpsa_pci_device_id[] = {
Stephen M. Cameronedd16362009-12-08 14:09:11 -080087 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241},
88 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243},
89 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245},
90 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247},
91 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249},
Mike Miller163dbcd2013-09-04 15:11:10 -050092 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324A},
93 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324B},
Mike Millerf8b01eb2010-02-04 08:42:45 -060094 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3233},
scameron@beardog.cce.hp.com9143a962011-03-07 10:44:16 -060095 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3350},
96 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3351},
97 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3352},
98 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3353},
99 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3354},
100 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3355},
101 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3356},
Mike Millerfe0c9612012-09-20 16:05:18 -0500102 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1921},
103 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1922},
104 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1923},
105 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1924},
106 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1925},
107 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1926},
108 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1928},
Mike Miller97b9f532013-09-04 15:05:55 -0500109 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1929},
110 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BD},
111 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BE},
112 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BF},
113 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C0},
114 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C1},
115 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C2},
116 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C3},
117 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C4},
118 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C5},
119 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C7},
120 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C8},
121 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C9},
Mike Miller7c03b872010-12-01 11:16:07 -0600122 {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
Stephen M. Cameron6798cc02010-06-16 13:51:20 -0500123 PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800124 {0,}
125};
126
127MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
128
129/* board_id = Subsystem Device ID & Vendor ID
130 * product = Marketing Name for the board
131 * access = Address of the struct of function pointers
132 */
133static struct board_type products[] = {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800134 {0x3241103C, "Smart Array P212", &SA5_access},
135 {0x3243103C, "Smart Array P410", &SA5_access},
136 {0x3245103C, "Smart Array P410i", &SA5_access},
137 {0x3247103C, "Smart Array P411", &SA5_access},
138 {0x3249103C, "Smart Array P812", &SA5_access},
Mike Miller163dbcd2013-09-04 15:11:10 -0500139 {0x324A103C, "Smart Array P712m", &SA5_access},
140 {0x324B103C, "Smart Array P711m", &SA5_access},
Mike Millerfe0c9612012-09-20 16:05:18 -0500141 {0x3350103C, "Smart Array P222", &SA5_access},
142 {0x3351103C, "Smart Array P420", &SA5_access},
143 {0x3352103C, "Smart Array P421", &SA5_access},
144 {0x3353103C, "Smart Array P822", &SA5_access},
145 {0x3354103C, "Smart Array P420i", &SA5_access},
146 {0x3355103C, "Smart Array P220i", &SA5_access},
147 {0x3356103C, "Smart Array P721m", &SA5_access},
Mike Miller1fd6c8e2013-09-04 15:08:29 -0500148 {0x1921103C, "Smart Array P830i", &SA5_access},
149 {0x1922103C, "Smart Array P430", &SA5_access},
150 {0x1923103C, "Smart Array P431", &SA5_access},
151 {0x1924103C, "Smart Array P830", &SA5_access},
152 {0x1926103C, "Smart Array P731m", &SA5_access},
153 {0x1928103C, "Smart Array P230i", &SA5_access},
154 {0x1929103C, "Smart Array P530", &SA5_access},
Mike Miller97b9f532013-09-04 15:05:55 -0500155 {0x21BD103C, "Smart Array", &SA5_access},
156 {0x21BE103C, "Smart Array", &SA5_access},
157 {0x21BF103C, "Smart Array", &SA5_access},
158 {0x21C0103C, "Smart Array", &SA5_access},
159 {0x21C1103C, "Smart Array", &SA5_access},
160 {0x21C2103C, "Smart Array", &SA5_access},
161 {0x21C3103C, "Smart Array", &SA5_access},
162 {0x21C4103C, "Smart Array", &SA5_access},
163 {0x21C5103C, "Smart Array", &SA5_access},
164 {0x21C7103C, "Smart Array", &SA5_access},
165 {0x21C8103C, "Smart Array", &SA5_access},
166 {0x21C9103C, "Smart Array", &SA5_access},
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800167 {0xFFFF103C, "Unknown Smart Array", &SA5_access},
168};
169
170static int number_of_controllers;
171
Stephen M. Cameron10f66012010-06-16 13:51:50 -0500172static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id);
173static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800174static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg);
175static void start_io(struct ctlr_info *h);
176
177#ifdef CONFIG_COMPAT
178static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg);
179#endif
180
181static void cmd_free(struct ctlr_info *h, struct CommandList *c);
182static void cmd_special_free(struct ctlr_info *h, struct CommandList *c);
183static struct CommandList *cmd_alloc(struct ctlr_info *h);
184static struct CommandList *cmd_special_alloc(struct ctlr_info *h);
Stephen M. Camerona2dac132013-02-20 11:24:41 -0600185static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -0600186 void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800187 int cmd_type);
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -0600188#define VPD_PAGE (1 << 8)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800189
Jeff Garzikf2812332010-11-16 02:10:29 -0500190static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Stephen M. Camerona08a8472010-02-04 08:43:16 -0600191static void hpsa_scan_start(struct Scsi_Host *);
192static int hpsa_scan_finished(struct Scsi_Host *sh,
193 unsigned long elapsed_time);
Stephen M. Cameron667e23d2010-02-25 14:02:51 -0600194static int hpsa_change_queue_depth(struct scsi_device *sdev,
195 int qdepth, int reason);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800196
197static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500198static int hpsa_eh_abort_handler(struct scsi_cmnd *scsicmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800199static int hpsa_slave_alloc(struct scsi_device *sdev);
200static void hpsa_slave_destroy(struct scsi_device *sdev);
201
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800202static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800203static int check_for_unit_attention(struct ctlr_info *h,
204 struct CommandList *c);
205static void check_ioctl_unit_attention(struct ctlr_info *h,
206 struct CommandList *c);
Don Brace303932f2010-02-04 08:42:40 -0600207/* performant mode helper functions */
208static void calc_bucket_map(int *bucket, int num_buckets,
Matt Gatese1f7de02014-02-18 13:55:17 -0600209 int nsgs, int min_blocks, int *bucket_map);
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800210static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
Matt Gates254f7962012-05-01 11:43:06 -0500211static inline u32 next_command(struct ctlr_info *h, u8 q);
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800212static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
213 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
214 u64 *cfg_offset);
215static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
216 unsigned long *memory_bar);
217static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
218static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
219 int wait_for_ready);
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500220static inline void finish_cmd(struct CommandList *c);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -0600221static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h);
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -0600222#define BOARD_NOT_READY 0
223#define BOARD_READY 1
Stephen M. Cameron76438d02014-02-18 13:55:43 -0600224static void hpsa_drain_commands(struct ctlr_info *h);
225static void hpsa_flush_cache(struct ctlr_info *h);
Scott Teelc3497752014-02-18 13:56:34 -0600226static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
227 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
228 u8 *scsi3addr);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800229
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800230static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
231{
232 unsigned long *priv = shost_priv(sdev->host);
233 return (struct ctlr_info *) *priv;
234}
235
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600236static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
237{
238 unsigned long *priv = shost_priv(sh);
239 return (struct ctlr_info *) *priv;
240}
241
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800242static int check_for_unit_attention(struct ctlr_info *h,
243 struct CommandList *c)
244{
245 if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
246 return 0;
247
248 switch (c->err_info->SenseInfo[12]) {
249 case STATE_CHANGED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600250 dev_warn(&h->pdev->dev, HPSA "%d: a state change "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800251 "detected, command retried\n", h->ctlr);
252 break;
253 case LUN_FAILED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600254 dev_warn(&h->pdev->dev, HPSA "%d: LUN failure "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800255 "detected, action required\n", h->ctlr);
256 break;
257 case REPORT_LUNS_CHANGED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600258 dev_warn(&h->pdev->dev, HPSA "%d: report LUN data "
Mike Miller31468402010-02-25 14:03:12 -0600259 "changed, action required\n", h->ctlr);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800260 /*
Scott Teel4f4eb9f2012-01-19 14:01:25 -0600261 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
262 * target (array) devices.
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800263 */
264 break;
265 case POWER_OR_RESET:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600266 dev_warn(&h->pdev->dev, HPSA "%d: a power on "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800267 "or device reset detected\n", h->ctlr);
268 break;
269 case UNIT_ATTENTION_CLEARED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600270 dev_warn(&h->pdev->dev, HPSA "%d: unit attention "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800271 "cleared by another initiator\n", h->ctlr);
272 break;
273 default:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600274 dev_warn(&h->pdev->dev, HPSA "%d: unknown "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800275 "unit attention detected\n", h->ctlr);
276 break;
277 }
278 return 1;
279}
280
Matt Bondurant852af202012-05-01 11:42:35 -0500281static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
282{
283 if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
284 (c->err_info->ScsiStatus != SAM_STAT_BUSY &&
285 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
286 return 0;
287 dev_warn(&h->pdev->dev, HPSA "device busy");
288 return 1;
289}
290
Scott Teelda0697b2014-02-18 13:57:00 -0600291static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
292 struct device_attribute *attr,
293 const char *buf, size_t count)
294{
295 int status, len;
296 struct ctlr_info *h;
297 struct Scsi_Host *shost = class_to_shost(dev);
298 char tmpbuf[10];
299
300 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
301 return -EACCES;
302 len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
303 strncpy(tmpbuf, buf, len);
304 tmpbuf[len] = '\0';
305 if (sscanf(tmpbuf, "%d", &status) != 1)
306 return -EINVAL;
307 h = shost_to_hba(shost);
308 h->acciopath_status = !!status;
309 dev_warn(&h->pdev->dev,
310 "hpsa: HP SSD Smart Path %s via sysfs update.\n",
311 h->acciopath_status ? "enabled" : "disabled");
312 return count;
313}
314
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800315static ssize_t host_store_rescan(struct device *dev,
316 struct device_attribute *attr,
317 const char *buf, size_t count)
318{
319 struct ctlr_info *h;
320 struct Scsi_Host *shost = class_to_shost(dev);
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600321 h = shost_to_hba(shost);
Mike Miller31468402010-02-25 14:03:12 -0600322 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800323 return count;
324}
325
Stephen M. Camerond28ce022010-05-27 15:14:34 -0500326static ssize_t host_show_firmware_revision(struct device *dev,
327 struct device_attribute *attr, char *buf)
328{
329 struct ctlr_info *h;
330 struct Scsi_Host *shost = class_to_shost(dev);
331 unsigned char *fwrev;
332
333 h = shost_to_hba(shost);
334 if (!h->hba_inquiry_data)
335 return 0;
336 fwrev = &h->hba_inquiry_data[32];
337 return snprintf(buf, 20, "%c%c%c%c\n",
338 fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
339}
340
Stephen M. Cameron94a13642011-01-06 14:48:39 -0600341static ssize_t host_show_commands_outstanding(struct device *dev,
342 struct device_attribute *attr, char *buf)
343{
344 struct Scsi_Host *shost = class_to_shost(dev);
345 struct ctlr_info *h = shost_to_hba(shost);
346
347 return snprintf(buf, 20, "%d\n", h->commands_outstanding);
348}
349
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600350static ssize_t host_show_transport_mode(struct device *dev,
351 struct device_attribute *attr, char *buf)
352{
353 struct ctlr_info *h;
354 struct Scsi_Host *shost = class_to_shost(dev);
355
356 h = shost_to_hba(shost);
357 return snprintf(buf, 20, "%s\n",
Stephen M. Cameron960a30e2011-02-15 15:33:03 -0600358 h->transMethod & CFGTBL_Trans_Performant ?
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600359 "performant" : "simple");
360}
361
Scott Teelda0697b2014-02-18 13:57:00 -0600362static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
363 struct device_attribute *attr, char *buf)
364{
365 struct ctlr_info *h;
366 struct Scsi_Host *shost = class_to_shost(dev);
367
368 h = shost_to_hba(shost);
369 return snprintf(buf, 30, "HP SSD Smart Path %s\n",
370 (h->acciopath_status == 1) ? "enabled" : "disabled");
371}
372
Stephen M. Cameron46380782011-05-03 15:00:01 -0500373/* List of controllers which cannot be hard reset on kexec with reset_devices */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600374static u32 unresettable_controller[] = {
375 0x324a103C, /* Smart Array P712m */
376 0x324b103C, /* SmartArray P711m */
377 0x3223103C, /* Smart Array P800 */
378 0x3234103C, /* Smart Array P400 */
379 0x3235103C, /* Smart Array P400i */
380 0x3211103C, /* Smart Array E200i */
381 0x3212103C, /* Smart Array E200 */
382 0x3213103C, /* Smart Array E200i */
383 0x3214103C, /* Smart Array E200i */
384 0x3215103C, /* Smart Array E200i */
385 0x3237103C, /* Smart Array E500 */
386 0x323D103C, /* Smart Array P700m */
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100387 0x40800E11, /* Smart Array 5i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600388 0x409C0E11, /* Smart Array 6400 */
389 0x409D0E11, /* Smart Array 6400 EM */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100390 0x40700E11, /* Smart Array 5300 */
391 0x40820E11, /* Smart Array 532 */
392 0x40830E11, /* Smart Array 5312 */
393 0x409A0E11, /* Smart Array 641 */
394 0x409B0E11, /* Smart Array 642 */
395 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600396};
397
Stephen M. Cameron46380782011-05-03 15:00:01 -0500398/* List of controllers which cannot even be soft reset */
399static u32 soft_unresettable_controller[] = {
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100400 0x40800E11, /* Smart Array 5i */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100401 0x40700E11, /* Smart Array 5300 */
402 0x40820E11, /* Smart Array 532 */
403 0x40830E11, /* Smart Array 5312 */
404 0x409A0E11, /* Smart Array 641 */
405 0x409B0E11, /* Smart Array 642 */
406 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron46380782011-05-03 15:00:01 -0500407 /* Exclude 640x boards. These are two pci devices in one slot
408 * which share a battery backed cache module. One controls the
409 * cache, the other accesses the cache through the one that controls
410 * it. If we reset the one controlling the cache, the other will
411 * likely not be happy. Just forbid resetting this conjoined mess.
412 * The 640x isn't really supported by hpsa anyway.
413 */
414 0x409C0E11, /* Smart Array 6400 */
415 0x409D0E11, /* Smart Array 6400 EM */
416};
417
418static int ctlr_is_hard_resettable(u32 board_id)
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600419{
420 int i;
421
422 for (i = 0; i < ARRAY_SIZE(unresettable_controller); i++)
Stephen M. Cameron46380782011-05-03 15:00:01 -0500423 if (unresettable_controller[i] == board_id)
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600424 return 0;
425 return 1;
426}
427
Stephen M. Cameron46380782011-05-03 15:00:01 -0500428static int ctlr_is_soft_resettable(u32 board_id)
429{
430 int i;
431
432 for (i = 0; i < ARRAY_SIZE(soft_unresettable_controller); i++)
433 if (soft_unresettable_controller[i] == board_id)
434 return 0;
435 return 1;
436}
437
438static int ctlr_is_resettable(u32 board_id)
439{
440 return ctlr_is_hard_resettable(board_id) ||
441 ctlr_is_soft_resettable(board_id);
442}
443
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600444static ssize_t host_show_resettable(struct device *dev,
445 struct device_attribute *attr, char *buf)
446{
447 struct ctlr_info *h;
448 struct Scsi_Host *shost = class_to_shost(dev);
449
450 h = shost_to_hba(shost);
Stephen M. Cameron46380782011-05-03 15:00:01 -0500451 return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600452}
453
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800454static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
455{
456 return (scsi3addr[3] & 0xC0) == 0x40;
457}
458
459static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
Mike Millerd82357e2012-05-01 11:43:32 -0500460 "1(ADM)", "UNKNOWN"
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800461};
Scott Teel6b80b182014-02-18 13:56:55 -0600462#define HPSA_RAID_0 0
463#define HPSA_RAID_4 1
464#define HPSA_RAID_1 2 /* also used for RAID 10 */
465#define HPSA_RAID_5 3 /* also used for RAID 50 */
466#define HPSA_RAID_51 4
467#define HPSA_RAID_6 5 /* also used for RAID 60 */
468#define HPSA_RAID_ADM 6 /* also used for RAID 1+0 ADM */
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800469#define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
470
471static ssize_t raid_level_show(struct device *dev,
472 struct device_attribute *attr, char *buf)
473{
474 ssize_t l = 0;
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600475 unsigned char rlevel;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800476 struct ctlr_info *h;
477 struct scsi_device *sdev;
478 struct hpsa_scsi_dev_t *hdev;
479 unsigned long flags;
480
481 sdev = to_scsi_device(dev);
482 h = sdev_to_hba(sdev);
483 spin_lock_irqsave(&h->lock, flags);
484 hdev = sdev->hostdata;
485 if (!hdev) {
486 spin_unlock_irqrestore(&h->lock, flags);
487 return -ENODEV;
488 }
489
490 /* Is this even a logical drive? */
491 if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
492 spin_unlock_irqrestore(&h->lock, flags);
493 l = snprintf(buf, PAGE_SIZE, "N/A\n");
494 return l;
495 }
496
497 rlevel = hdev->raid_level;
498 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600499 if (rlevel > RAID_UNKNOWN)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800500 rlevel = RAID_UNKNOWN;
501 l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
502 return l;
503}
504
505static ssize_t lunid_show(struct device *dev,
506 struct device_attribute *attr, char *buf)
507{
508 struct ctlr_info *h;
509 struct scsi_device *sdev;
510 struct hpsa_scsi_dev_t *hdev;
511 unsigned long flags;
512 unsigned char lunid[8];
513
514 sdev = to_scsi_device(dev);
515 h = sdev_to_hba(sdev);
516 spin_lock_irqsave(&h->lock, flags);
517 hdev = sdev->hostdata;
518 if (!hdev) {
519 spin_unlock_irqrestore(&h->lock, flags);
520 return -ENODEV;
521 }
522 memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
523 spin_unlock_irqrestore(&h->lock, flags);
524 return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
525 lunid[0], lunid[1], lunid[2], lunid[3],
526 lunid[4], lunid[5], lunid[6], lunid[7]);
527}
528
529static ssize_t unique_id_show(struct device *dev,
530 struct device_attribute *attr, char *buf)
531{
532 struct ctlr_info *h;
533 struct scsi_device *sdev;
534 struct hpsa_scsi_dev_t *hdev;
535 unsigned long flags;
536 unsigned char sn[16];
537
538 sdev = to_scsi_device(dev);
539 h = sdev_to_hba(sdev);
540 spin_lock_irqsave(&h->lock, flags);
541 hdev = sdev->hostdata;
542 if (!hdev) {
543 spin_unlock_irqrestore(&h->lock, flags);
544 return -ENODEV;
545 }
546 memcpy(sn, hdev->device_id, sizeof(sn));
547 spin_unlock_irqrestore(&h->lock, flags);
548 return snprintf(buf, 16 * 2 + 2,
549 "%02X%02X%02X%02X%02X%02X%02X%02X"
550 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
551 sn[0], sn[1], sn[2], sn[3],
552 sn[4], sn[5], sn[6], sn[7],
553 sn[8], sn[9], sn[10], sn[11],
554 sn[12], sn[13], sn[14], sn[15]);
555}
556
Scott Teelc1988682014-02-18 13:55:54 -0600557static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
558 struct device_attribute *attr, char *buf)
559{
560 struct ctlr_info *h;
561 struct scsi_device *sdev;
562 struct hpsa_scsi_dev_t *hdev;
563 unsigned long flags;
564 int offload_enabled;
565
566 sdev = to_scsi_device(dev);
567 h = sdev_to_hba(sdev);
568 spin_lock_irqsave(&h->lock, flags);
569 hdev = sdev->hostdata;
570 if (!hdev) {
571 spin_unlock_irqrestore(&h->lock, flags);
572 return -ENODEV;
573 }
574 offload_enabled = hdev->offload_enabled;
575 spin_unlock_irqrestore(&h->lock, flags);
576 return snprintf(buf, 20, "%d\n", offload_enabled);
577}
578
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600579static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
580static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
581static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
582static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
Scott Teelc1988682014-02-18 13:55:54 -0600583static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
584 host_show_hp_ssd_smart_path_enabled, NULL);
Scott Teelda0697b2014-02-18 13:57:00 -0600585static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
586 host_show_hp_ssd_smart_path_status,
587 host_store_hp_ssd_smart_path_status);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600588static DEVICE_ATTR(firmware_revision, S_IRUGO,
589 host_show_firmware_revision, NULL);
590static DEVICE_ATTR(commands_outstanding, S_IRUGO,
591 host_show_commands_outstanding, NULL);
592static DEVICE_ATTR(transport_mode, S_IRUGO,
593 host_show_transport_mode, NULL);
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600594static DEVICE_ATTR(resettable, S_IRUGO,
595 host_show_resettable, NULL);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600596
597static struct device_attribute *hpsa_sdev_attrs[] = {
598 &dev_attr_raid_level,
599 &dev_attr_lunid,
600 &dev_attr_unique_id,
Scott Teelc1988682014-02-18 13:55:54 -0600601 &dev_attr_hp_ssd_smart_path_enabled,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600602 NULL,
603};
604
605static struct device_attribute *hpsa_shost_attrs[] = {
606 &dev_attr_rescan,
607 &dev_attr_firmware_revision,
608 &dev_attr_commands_outstanding,
609 &dev_attr_transport_mode,
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600610 &dev_attr_resettable,
Scott Teelda0697b2014-02-18 13:57:00 -0600611 &dev_attr_hp_ssd_smart_path_status,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600612 NULL,
613};
614
615static struct scsi_host_template hpsa_driver_template = {
616 .module = THIS_MODULE,
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600617 .name = HPSA,
618 .proc_name = HPSA,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600619 .queuecommand = hpsa_scsi_queue_command,
620 .scan_start = hpsa_scan_start,
621 .scan_finished = hpsa_scan_finished,
622 .change_queue_depth = hpsa_change_queue_depth,
623 .this_id = -1,
624 .use_clustering = ENABLE_CLUSTERING,
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500625 .eh_abort_handler = hpsa_eh_abort_handler,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600626 .eh_device_reset_handler = hpsa_eh_device_reset_handler,
627 .ioctl = hpsa_ioctl,
628 .slave_alloc = hpsa_slave_alloc,
629 .slave_destroy = hpsa_slave_destroy,
630#ifdef CONFIG_COMPAT
631 .compat_ioctl = hpsa_compat_ioctl,
632#endif
633 .sdev_attrs = hpsa_sdev_attrs,
634 .shost_attrs = hpsa_shost_attrs,
Stephen M. Cameronc0d6a4d2011-10-26 16:20:53 -0500635 .max_sectors = 8192,
Martin K. Petersen54b2b502013-10-23 06:25:40 -0400636 .no_write_same = 1,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600637};
638
639
640/* Enqueuing and dequeuing functions for cmdlists. */
641static inline void addQ(struct list_head *list, struct CommandList *c)
642{
643 list_add_tail(&c->list, list);
644}
645
Matt Gates254f7962012-05-01 11:43:06 -0500646static inline u32 next_command(struct ctlr_info *h, u8 q)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600647{
648 u32 a;
Matt Gates254f7962012-05-01 11:43:06 -0500649 struct reply_pool *rq = &h->reply_queue[q];
Matt Gatese16a33a2012-05-01 11:43:11 -0500650 unsigned long flags;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600651
Matt Gatese1f7de02014-02-18 13:55:17 -0600652 if (h->transMethod & CFGTBL_Trans_io_accel1)
653 return h->access.command_completed(h, q);
654
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600655 if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
Matt Gates254f7962012-05-01 11:43:06 -0500656 return h->access.command_completed(h, q);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600657
Matt Gates254f7962012-05-01 11:43:06 -0500658 if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
659 a = rq->head[rq->current_entry];
660 rq->current_entry++;
Matt Gatese16a33a2012-05-01 11:43:11 -0500661 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600662 h->commands_outstanding--;
Matt Gatese16a33a2012-05-01 11:43:11 -0500663 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600664 } else {
665 a = FIFO_EMPTY;
666 }
667 /* Check for wraparound */
Matt Gates254f7962012-05-01 11:43:06 -0500668 if (rq->current_entry == h->max_commands) {
669 rq->current_entry = 0;
670 rq->wraparound ^= 1;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600671 }
672 return a;
673}
674
Scott Teelc3497752014-02-18 13:56:34 -0600675/*
676 * There are some special bits in the bus address of the
677 * command that we have to set for the controller to know
678 * how to process the command:
679 *
680 * Normal performant mode:
681 * bit 0: 1 means performant mode, 0 means simple mode.
682 * bits 1-3 = block fetch table entry
683 * bits 4-6 = command type (== 0)
684 *
685 * ioaccel1 mode:
686 * bit 0 = "performant mode" bit.
687 * bits 1-3 = block fetch table entry
688 * bits 4-6 = command type (== 110)
689 * (command type is needed because ioaccel1 mode
690 * commands are submitted through the same register as normal
691 * mode commands, so this is how the controller knows whether
692 * the command is normal mode or ioaccel1 mode.)
693 *
694 * ioaccel2 mode:
695 * bit 0 = "performant mode" bit.
696 * bits 1-4 = block fetch table entry (note extra bit)
697 * bits 4-6 = not needed, because ioaccel2 mode has
698 * a separate special register for submitting commands.
699 */
700
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600701/* set_performant_mode: Modify the tag for cciss performant
702 * set bit 0 for pull model, bits 3-1 for block fetch
703 * register number
704 */
705static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
706{
Matt Gates254f7962012-05-01 11:43:06 -0500707 if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600708 c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
Hannes Reineckeeee0f032014-01-15 13:30:53 +0100709 if (likely(h->msix_vector > 0))
Matt Gates254f7962012-05-01 11:43:06 -0500710 c->Header.ReplyQueue =
John Kacur804a5cb2013-07-26 16:06:18 +0200711 raw_smp_processor_id() % h->nreply_queues;
Matt Gates254f7962012-05-01 11:43:06 -0500712 }
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600713}
714
Scott Teelc3497752014-02-18 13:56:34 -0600715static void set_ioaccel1_performant_mode(struct ctlr_info *h,
716 struct CommandList *c)
717{
718 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
719
720 /* Tell the controller to post the reply to the queue for this
721 * processor. This seems to give the best I/O throughput.
722 */
723 cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
724 /* Set the bits in the address sent down to include:
725 * - performant mode bit (bit 0)
726 * - pull count (bits 1-3)
727 * - command type (bits 4-6)
728 */
729 c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
730 IOACCEL1_BUSADDR_CMDTYPE;
731}
732
733static void set_ioaccel2_performant_mode(struct ctlr_info *h,
734 struct CommandList *c)
735{
736 struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
737
738 /* Tell the controller to post the reply to the queue for this
739 * processor. This seems to give the best I/O throughput.
740 */
741 cp->reply_queue = smp_processor_id() % h->nreply_queues;
742 /* Set the bits in the address sent down to include:
743 * - performant mode bit not used in ioaccel mode 2
744 * - pull count (bits 0-3)
745 * - command type isn't needed for ioaccel2
746 */
747 c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
748}
749
Stephen M. Camerone85c5972012-05-01 11:43:42 -0500750static int is_firmware_flash_cmd(u8 *cdb)
751{
752 return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
753}
754
755/*
756 * During firmware flash, the heartbeat register may not update as frequently
757 * as it should. So we dial down lockup detection during firmware flash. and
758 * dial it back up when firmware flash completes.
759 */
760#define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
761#define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
762static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
763 struct CommandList *c)
764{
765 if (!is_firmware_flash_cmd(c->Request.CDB))
766 return;
767 atomic_inc(&h->firmware_flash_in_progress);
768 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
769}
770
771static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
772 struct CommandList *c)
773{
774 if (is_firmware_flash_cmd(c->Request.CDB) &&
775 atomic_dec_and_test(&h->firmware_flash_in_progress))
776 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
777}
778
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600779static void enqueue_cmd_and_start_io(struct ctlr_info *h,
780 struct CommandList *c)
781{
782 unsigned long flags;
783
Scott Teelc3497752014-02-18 13:56:34 -0600784 switch (c->cmd_type) {
785 case CMD_IOACCEL1:
786 set_ioaccel1_performant_mode(h, c);
787 break;
788 case CMD_IOACCEL2:
789 set_ioaccel2_performant_mode(h, c);
790 break;
791 default:
792 set_performant_mode(h, c);
793 }
Stephen M. Camerone85c5972012-05-01 11:43:42 -0500794 dial_down_lockup_detection_during_fw_flash(h, c);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600795 spin_lock_irqsave(&h->lock, flags);
796 addQ(&h->reqQ, c);
797 h->Qdepth++;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600798 spin_unlock_irqrestore(&h->lock, flags);
Matt Gatese16a33a2012-05-01 11:43:11 -0500799 start_io(h);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600800}
801
802static inline void removeQ(struct CommandList *c)
803{
804 if (WARN_ON(list_empty(&c->list)))
805 return;
806 list_del_init(&c->list);
807}
808
809static inline int is_hba_lunid(unsigned char scsi3addr[])
810{
811 return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
812}
813
814static inline int is_scsi_rev_5(struct ctlr_info *h)
815{
816 if (!h->hba_inquiry_data)
817 return 0;
818 if ((h->hba_inquiry_data[2] & 0x07) == 5)
819 return 1;
820 return 0;
821}
822
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800823static int hpsa_find_target_lun(struct ctlr_info *h,
824 unsigned char scsi3addr[], int bus, int *target, int *lun)
825{
826 /* finds an unused bus, target, lun for a new physical device
827 * assumes h->devlock is held
828 */
829 int i, found = 0;
Scott Teelcfe5bad2011-10-26 16:21:07 -0500830 DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800831
Akinobu Mita263d9402012-01-21 00:15:27 +0900832 bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800833
834 for (i = 0; i < h->ndevices; i++) {
835 if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
Akinobu Mita263d9402012-01-21 00:15:27 +0900836 __set_bit(h->dev[i]->target, lun_taken);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800837 }
838
Akinobu Mita263d9402012-01-21 00:15:27 +0900839 i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
840 if (i < HPSA_MAX_DEVICES) {
841 /* *bus = 1; */
842 *target = i;
843 *lun = 0;
844 found = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800845 }
846 return !found;
847}
848
849/* Add an entry into h->dev[] array. */
850static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
851 struct hpsa_scsi_dev_t *device,
852 struct hpsa_scsi_dev_t *added[], int *nadded)
853{
854 /* assumes h->devlock is held */
855 int n = h->ndevices;
856 int i;
857 unsigned char addr1[8], addr2[8];
858 struct hpsa_scsi_dev_t *sd;
859
Scott Teelcfe5bad2011-10-26 16:21:07 -0500860 if (n >= HPSA_MAX_DEVICES) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800861 dev_err(&h->pdev->dev, "too many devices, some will be "
862 "inaccessible.\n");
863 return -1;
864 }
865
866 /* physical devices do not have lun or target assigned until now. */
867 if (device->lun != -1)
868 /* Logical device, lun is already assigned. */
869 goto lun_assigned;
870
871 /* If this device a non-zero lun of a multi-lun device
872 * byte 4 of the 8-byte LUN addr will contain the logical
873 * unit no, zero otherise.
874 */
875 if (device->scsi3addr[4] == 0) {
876 /* This is not a non-zero lun of a multi-lun device */
877 if (hpsa_find_target_lun(h, device->scsi3addr,
878 device->bus, &device->target, &device->lun) != 0)
879 return -1;
880 goto lun_assigned;
881 }
882
883 /* This is a non-zero lun of a multi-lun device.
884 * Search through our list and find the device which
885 * has the same 8 byte LUN address, excepting byte 4.
886 * Assign the same bus and target for this new LUN.
887 * Use the logical unit number from the firmware.
888 */
889 memcpy(addr1, device->scsi3addr, 8);
890 addr1[4] = 0;
891 for (i = 0; i < n; i++) {
892 sd = h->dev[i];
893 memcpy(addr2, sd->scsi3addr, 8);
894 addr2[4] = 0;
895 /* differ only in byte 4? */
896 if (memcmp(addr1, addr2, 8) == 0) {
897 device->bus = sd->bus;
898 device->target = sd->target;
899 device->lun = device->scsi3addr[4];
900 break;
901 }
902 }
903 if (device->lun == -1) {
904 dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
905 " suspect firmware bug or unsupported hardware "
906 "configuration.\n");
907 return -1;
908 }
909
910lun_assigned:
911
912 h->dev[n] = device;
913 h->ndevices++;
914 added[*nadded] = device;
915 (*nadded)++;
916
917 /* initially, (before registering with scsi layer) we don't
918 * know our hostno and we don't want to print anything first
919 * time anyway (the scsi layer's inquiries will show that info)
920 */
921 /* if (hostno != -1) */
922 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
923 scsi_device_type(device->devtype), hostno,
924 device->bus, device->target, device->lun);
925 return 0;
926}
927
Scott Teelbd9244f2012-01-19 14:01:30 -0600928/* Update an entry in h->dev[] array. */
929static void hpsa_scsi_update_entry(struct ctlr_info *h, int hostno,
930 int entry, struct hpsa_scsi_dev_t *new_entry)
931{
932 /* assumes h->devlock is held */
933 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
934
935 /* Raid level changed. */
936 h->dev[entry]->raid_level = new_entry->raid_level;
Stephen M. Cameron250fb122014-02-18 13:55:38 -0600937
938 /* Raid offload parameters changed. */
939 h->dev[entry]->offload_config = new_entry->offload_config;
940 h->dev[entry]->offload_enabled = new_entry->offload_enabled;
Stephen M. Cameron9fb0de22014-02-18 13:56:50 -0600941 h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
942 h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
943 h->dev[entry]->raid_map = new_entry->raid_map;
Stephen M. Cameron250fb122014-02-18 13:55:38 -0600944
Scott Teelbd9244f2012-01-19 14:01:30 -0600945 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d updated.\n",
946 scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
947 new_entry->target, new_entry->lun);
948}
949
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -0600950/* Replace an entry from h->dev[] array. */
951static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
952 int entry, struct hpsa_scsi_dev_t *new_entry,
953 struct hpsa_scsi_dev_t *added[], int *nadded,
954 struct hpsa_scsi_dev_t *removed[], int *nremoved)
955{
956 /* assumes h->devlock is held */
Scott Teelcfe5bad2011-10-26 16:21:07 -0500957 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -0600958 removed[*nremoved] = h->dev[entry];
959 (*nremoved)++;
Stephen M. Cameron01350d02011-08-09 08:18:01 -0500960
961 /*
962 * New physical devices won't have target/lun assigned yet
963 * so we need to preserve the values in the slot we are replacing.
964 */
965 if (new_entry->target == -1) {
966 new_entry->target = h->dev[entry]->target;
967 new_entry->lun = h->dev[entry]->lun;
968 }
969
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -0600970 h->dev[entry] = new_entry;
971 added[*nadded] = new_entry;
972 (*nadded)++;
973 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n",
974 scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
975 new_entry->target, new_entry->lun);
976}
977
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800978/* Remove an entry from h->dev[] array. */
979static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
980 struct hpsa_scsi_dev_t *removed[], int *nremoved)
981{
982 /* assumes h->devlock is held */
983 int i;
984 struct hpsa_scsi_dev_t *sd;
985
Scott Teelcfe5bad2011-10-26 16:21:07 -0500986 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800987
988 sd = h->dev[entry];
989 removed[*nremoved] = h->dev[entry];
990 (*nremoved)++;
991
992 for (i = entry; i < h->ndevices-1; i++)
993 h->dev[i] = h->dev[i+1];
994 h->ndevices--;
995 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
996 scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
997 sd->lun);
998}
999
1000#define SCSI3ADDR_EQ(a, b) ( \
1001 (a)[7] == (b)[7] && \
1002 (a)[6] == (b)[6] && \
1003 (a)[5] == (b)[5] && \
1004 (a)[4] == (b)[4] && \
1005 (a)[3] == (b)[3] && \
1006 (a)[2] == (b)[2] && \
1007 (a)[1] == (b)[1] && \
1008 (a)[0] == (b)[0])
1009
1010static void fixup_botched_add(struct ctlr_info *h,
1011 struct hpsa_scsi_dev_t *added)
1012{
1013 /* called when scsi_add_device fails in order to re-adjust
1014 * h->dev[] to match the mid layer's view.
1015 */
1016 unsigned long flags;
1017 int i, j;
1018
1019 spin_lock_irqsave(&h->lock, flags);
1020 for (i = 0; i < h->ndevices; i++) {
1021 if (h->dev[i] == added) {
1022 for (j = i; j < h->ndevices-1; j++)
1023 h->dev[j] = h->dev[j+1];
1024 h->ndevices--;
1025 break;
1026 }
1027 }
1028 spin_unlock_irqrestore(&h->lock, flags);
1029 kfree(added);
1030}
1031
1032static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
1033 struct hpsa_scsi_dev_t *dev2)
1034{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001035 /* we compare everything except lun and target as these
1036 * are not yet assigned. Compare parts likely
1037 * to differ first
1038 */
1039 if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
1040 sizeof(dev1->scsi3addr)) != 0)
1041 return 0;
1042 if (memcmp(dev1->device_id, dev2->device_id,
1043 sizeof(dev1->device_id)) != 0)
1044 return 0;
1045 if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
1046 return 0;
1047 if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
1048 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001049 if (dev1->devtype != dev2->devtype)
1050 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001051 if (dev1->bus != dev2->bus)
1052 return 0;
1053 return 1;
1054}
1055
Scott Teelbd9244f2012-01-19 14:01:30 -06001056static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
1057 struct hpsa_scsi_dev_t *dev2)
1058{
1059 /* Device attributes that can change, but don't mean
1060 * that the device is a different device, nor that the OS
1061 * needs to be told anything about the change.
1062 */
1063 if (dev1->raid_level != dev2->raid_level)
1064 return 1;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001065 if (dev1->offload_config != dev2->offload_config)
1066 return 1;
1067 if (dev1->offload_enabled != dev2->offload_enabled)
1068 return 1;
Scott Teelbd9244f2012-01-19 14:01:30 -06001069 return 0;
1070}
1071
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001072/* Find needle in haystack. If exact match found, return DEVICE_SAME,
1073 * and return needle location in *index. If scsi3addr matches, but not
1074 * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
Scott Teelbd9244f2012-01-19 14:01:30 -06001075 * location in *index.
1076 * In the case of a minor device attribute change, such as RAID level, just
1077 * return DEVICE_UPDATED, along with the updated device's location in index.
1078 * If needle not found, return DEVICE_NOT_FOUND.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001079 */
1080static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
1081 struct hpsa_scsi_dev_t *haystack[], int haystack_size,
1082 int *index)
1083{
1084 int i;
1085#define DEVICE_NOT_FOUND 0
1086#define DEVICE_CHANGED 1
1087#define DEVICE_SAME 2
Scott Teelbd9244f2012-01-19 14:01:30 -06001088#define DEVICE_UPDATED 3
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001089 for (i = 0; i < haystack_size; i++) {
Stephen M. Cameron23231042010-02-04 08:43:36 -06001090 if (haystack[i] == NULL) /* previously removed. */
1091 continue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001092 if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
1093 *index = i;
Scott Teelbd9244f2012-01-19 14:01:30 -06001094 if (device_is_the_same(needle, haystack[i])) {
1095 if (device_updated(needle, haystack[i]))
1096 return DEVICE_UPDATED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001097 return DEVICE_SAME;
Scott Teelbd9244f2012-01-19 14:01:30 -06001098 } else {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001099 return DEVICE_CHANGED;
Scott Teelbd9244f2012-01-19 14:01:30 -06001100 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001101 }
1102 }
1103 *index = -1;
1104 return DEVICE_NOT_FOUND;
1105}
1106
Stephen M. Cameron4967bd32010-02-04 08:41:49 -06001107static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001108 struct hpsa_scsi_dev_t *sd[], int nsds)
1109{
1110 /* sd contains scsi3 addresses and devtypes, and inquiry
1111 * data. This function takes what's in sd to be the current
1112 * reality and updates h->dev[] to reflect that reality.
1113 */
1114 int i, entry, device_change, changes = 0;
1115 struct hpsa_scsi_dev_t *csd;
1116 unsigned long flags;
1117 struct hpsa_scsi_dev_t **added, **removed;
1118 int nadded, nremoved;
1119 struct Scsi_Host *sh = NULL;
1120
Scott Teelcfe5bad2011-10-26 16:21:07 -05001121 added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
1122 removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001123
1124 if (!added || !removed) {
1125 dev_warn(&h->pdev->dev, "out of memory in "
1126 "adjust_hpsa_scsi_table\n");
1127 goto free_and_out;
1128 }
1129
1130 spin_lock_irqsave(&h->devlock, flags);
1131
1132 /* find any devices in h->dev[] that are not in
1133 * sd[] and remove them from h->dev[], and for any
1134 * devices which have changed, remove the old device
1135 * info and add the new device info.
Scott Teelbd9244f2012-01-19 14:01:30 -06001136 * If minor device attributes change, just update
1137 * the existing device structure.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001138 */
1139 i = 0;
1140 nremoved = 0;
1141 nadded = 0;
1142 while (i < h->ndevices) {
1143 csd = h->dev[i];
1144 device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
1145 if (device_change == DEVICE_NOT_FOUND) {
1146 changes++;
1147 hpsa_scsi_remove_entry(h, hostno, i,
1148 removed, &nremoved);
1149 continue; /* remove ^^^, hence i not incremented */
1150 } else if (device_change == DEVICE_CHANGED) {
1151 changes++;
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001152 hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
1153 added, &nadded, removed, &nremoved);
Stephen M. Cameronc7f172d2010-02-04 08:43:31 -06001154 /* Set it to NULL to prevent it from being freed
1155 * at the bottom of hpsa_update_scsi_devices()
1156 */
1157 sd[entry] = NULL;
Scott Teelbd9244f2012-01-19 14:01:30 -06001158 } else if (device_change == DEVICE_UPDATED) {
1159 hpsa_scsi_update_entry(h, hostno, i, sd[entry]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001160 }
1161 i++;
1162 }
1163
1164 /* Now, make sure every device listed in sd[] is also
1165 * listed in h->dev[], adding them if they aren't found
1166 */
1167
1168 for (i = 0; i < nsds; i++) {
1169 if (!sd[i]) /* if already added above. */
1170 continue;
1171 device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1172 h->ndevices, &entry);
1173 if (device_change == DEVICE_NOT_FOUND) {
1174 changes++;
1175 if (hpsa_scsi_add_entry(h, hostno, sd[i],
1176 added, &nadded) != 0)
1177 break;
1178 sd[i] = NULL; /* prevent from being freed later. */
1179 } else if (device_change == DEVICE_CHANGED) {
1180 /* should never happen... */
1181 changes++;
1182 dev_warn(&h->pdev->dev,
1183 "device unexpectedly changed.\n");
1184 /* but if it does happen, we just ignore that device */
1185 }
1186 }
1187 spin_unlock_irqrestore(&h->devlock, flags);
1188
1189 /* Don't notify scsi mid layer of any changes the first time through
1190 * (or if there are no changes) scsi_scan_host will do it later the
1191 * first time through.
1192 */
1193 if (hostno == -1 || !changes)
1194 goto free_and_out;
1195
1196 sh = h->scsi_host;
1197 /* Notify scsi mid layer of any removed devices */
1198 for (i = 0; i < nremoved; i++) {
1199 struct scsi_device *sdev =
1200 scsi_device_lookup(sh, removed[i]->bus,
1201 removed[i]->target, removed[i]->lun);
1202 if (sdev != NULL) {
1203 scsi_remove_device(sdev);
1204 scsi_device_put(sdev);
1205 } else {
1206 /* We don't expect to get here.
1207 * future cmds to this device will get selection
1208 * timeout as if the device was gone.
1209 */
1210 dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
1211 " for removal.", hostno, removed[i]->bus,
1212 removed[i]->target, removed[i]->lun);
1213 }
1214 kfree(removed[i]);
1215 removed[i] = NULL;
1216 }
1217
1218 /* Notify scsi mid layer of any added devices */
1219 for (i = 0; i < nadded; i++) {
1220 if (scsi_add_device(sh, added[i]->bus,
1221 added[i]->target, added[i]->lun) == 0)
1222 continue;
1223 dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
1224 "device not added.\n", hostno, added[i]->bus,
1225 added[i]->target, added[i]->lun);
1226 /* now we have to remove it from h->dev,
1227 * since it didn't get added to scsi mid layer
1228 */
1229 fixup_botched_add(h, added[i]);
1230 }
1231
1232free_and_out:
1233 kfree(added);
1234 kfree(removed);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001235}
1236
1237/*
Joe Perches9e03aa22013-09-03 13:45:58 -07001238 * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001239 * Assume's h->devlock is held.
1240 */
1241static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1242 int bus, int target, int lun)
1243{
1244 int i;
1245 struct hpsa_scsi_dev_t *sd;
1246
1247 for (i = 0; i < h->ndevices; i++) {
1248 sd = h->dev[i];
1249 if (sd->bus == bus && sd->target == target && sd->lun == lun)
1250 return sd;
1251 }
1252 return NULL;
1253}
1254
1255/* link sdev->hostdata to our per-device structure. */
1256static int hpsa_slave_alloc(struct scsi_device *sdev)
1257{
1258 struct hpsa_scsi_dev_t *sd;
1259 unsigned long flags;
1260 struct ctlr_info *h;
1261
1262 h = sdev_to_hba(sdev);
1263 spin_lock_irqsave(&h->devlock, flags);
1264 sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1265 sdev_id(sdev), sdev->lun);
1266 if (sd != NULL)
1267 sdev->hostdata = sd;
1268 spin_unlock_irqrestore(&h->devlock, flags);
1269 return 0;
1270}
1271
1272static void hpsa_slave_destroy(struct scsi_device *sdev)
1273{
Stephen M. Cameronbcc44252010-02-04 08:41:54 -06001274 /* nothing to do. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001275}
1276
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001277static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
1278{
1279 int i;
1280
1281 if (!h->cmd_sg_list)
1282 return;
1283 for (i = 0; i < h->nr_cmds; i++) {
1284 kfree(h->cmd_sg_list[i]);
1285 h->cmd_sg_list[i] = NULL;
1286 }
1287 kfree(h->cmd_sg_list);
1288 h->cmd_sg_list = NULL;
1289}
1290
1291static int hpsa_allocate_sg_chain_blocks(struct ctlr_info *h)
1292{
1293 int i;
1294
1295 if (h->chainsize <= 0)
1296 return 0;
1297
1298 h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
1299 GFP_KERNEL);
1300 if (!h->cmd_sg_list)
1301 return -ENOMEM;
1302 for (i = 0; i < h->nr_cmds; i++) {
1303 h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
1304 h->chainsize, GFP_KERNEL);
1305 if (!h->cmd_sg_list[i])
1306 goto clean;
1307 }
1308 return 0;
1309
1310clean:
1311 hpsa_free_sg_chain_blocks(h);
1312 return -ENOMEM;
1313}
1314
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001315static int hpsa_map_sg_chain_block(struct ctlr_info *h,
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001316 struct CommandList *c)
1317{
1318 struct SGDescriptor *chain_sg, *chain_block;
1319 u64 temp64;
1320
1321 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
1322 chain_block = h->cmd_sg_list[c->cmdindex];
1323 chain_sg->Ext = HPSA_SG_CHAIN;
1324 chain_sg->Len = sizeof(*chain_sg) *
1325 (c->Header.SGTotal - h->max_cmd_sg_entries);
1326 temp64 = pci_map_single(h->pdev, chain_block, chain_sg->Len,
1327 PCI_DMA_TODEVICE);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001328 if (dma_mapping_error(&h->pdev->dev, temp64)) {
1329 /* prevent subsequent unmapping */
1330 chain_sg->Addr.lower = 0;
1331 chain_sg->Addr.upper = 0;
1332 return -1;
1333 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001334 chain_sg->Addr.lower = (u32) (temp64 & 0x0FFFFFFFFULL);
1335 chain_sg->Addr.upper = (u32) ((temp64 >> 32) & 0x0FFFFFFFFULL);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001336 return 0;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001337}
1338
1339static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
1340 struct CommandList *c)
1341{
1342 struct SGDescriptor *chain_sg;
1343 union u64bit temp64;
1344
1345 if (c->Header.SGTotal <= h->max_cmd_sg_entries)
1346 return;
1347
1348 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
1349 temp64.val32.lower = chain_sg->Addr.lower;
1350 temp64.val32.upper = chain_sg->Addr.upper;
1351 pci_unmap_single(h->pdev, temp64.val, chain_sg->Len, PCI_DMA_TODEVICE);
1352}
1353
Scott Teela09c1442014-02-18 13:57:21 -06001354
1355/* Decode the various types of errors on ioaccel2 path.
1356 * Return 1 for any error that should generate a RAID path retry.
1357 * Return 0 for errors that don't require a RAID path retry.
1358 */
1359static int handle_ioaccel_mode2_error(struct ctlr_info *h,
Scott Teelc3497752014-02-18 13:56:34 -06001360 struct CommandList *c,
1361 struct scsi_cmnd *cmd,
1362 struct io_accel2_cmd *c2)
1363{
1364 int data_len;
Scott Teela09c1442014-02-18 13:57:21 -06001365 int retry = 0;
Scott Teelc3497752014-02-18 13:56:34 -06001366
1367 switch (c2->error_data.serv_response) {
1368 case IOACCEL2_SERV_RESPONSE_COMPLETE:
1369 switch (c2->error_data.status) {
1370 case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
1371 break;
1372 case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
1373 dev_warn(&h->pdev->dev,
1374 "%s: task complete with check condition.\n",
1375 "HP SSD Smart Path");
1376 if (c2->error_data.data_present !=
1377 IOACCEL2_SENSE_DATA_PRESENT)
1378 break;
1379 /* copy the sense data */
1380 data_len = c2->error_data.sense_data_len;
1381 if (data_len > SCSI_SENSE_BUFFERSIZE)
1382 data_len = SCSI_SENSE_BUFFERSIZE;
1383 if (data_len > sizeof(c2->error_data.sense_data_buff))
1384 data_len =
1385 sizeof(c2->error_data.sense_data_buff);
1386 memcpy(cmd->sense_buffer,
1387 c2->error_data.sense_data_buff, data_len);
1388 cmd->result |= SAM_STAT_CHECK_CONDITION;
Scott Teela09c1442014-02-18 13:57:21 -06001389 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001390 break;
1391 case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
1392 dev_warn(&h->pdev->dev,
1393 "%s: task complete with BUSY status.\n",
1394 "HP SSD Smart Path");
Scott Teela09c1442014-02-18 13:57:21 -06001395 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001396 break;
1397 case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
1398 dev_warn(&h->pdev->dev,
1399 "%s: task complete with reservation conflict.\n",
1400 "HP SSD Smart Path");
Scott Teela09c1442014-02-18 13:57:21 -06001401 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001402 break;
1403 case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
1404 /* Make scsi midlayer do unlimited retries */
1405 cmd->result = DID_IMM_RETRY << 16;
1406 break;
1407 case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
1408 dev_warn(&h->pdev->dev,
1409 "%s: task complete with aborted status.\n",
1410 "HP SSD Smart Path");
Scott Teela09c1442014-02-18 13:57:21 -06001411 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001412 break;
1413 default:
1414 dev_warn(&h->pdev->dev,
1415 "%s: task complete with unrecognized status: 0x%02x\n",
1416 "HP SSD Smart Path", c2->error_data.status);
Scott Teela09c1442014-02-18 13:57:21 -06001417 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001418 break;
1419 }
1420 break;
1421 case IOACCEL2_SERV_RESPONSE_FAILURE:
1422 /* don't expect to get here. */
1423 dev_warn(&h->pdev->dev,
1424 "unexpected delivery or target failure, status = 0x%02x\n",
1425 c2->error_data.status);
Scott Teela09c1442014-02-18 13:57:21 -06001426 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001427 break;
1428 case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
1429 break;
1430 case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
1431 break;
1432 case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
1433 dev_warn(&h->pdev->dev, "task management function rejected.\n");
Scott Teela09c1442014-02-18 13:57:21 -06001434 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001435 break;
1436 case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
1437 dev_warn(&h->pdev->dev, "task management function invalid LUN\n");
1438 break;
1439 default:
1440 dev_warn(&h->pdev->dev,
1441 "%s: Unrecognized server response: 0x%02x\n",
Scott Teela09c1442014-02-18 13:57:21 -06001442 "HP SSD Smart Path",
1443 c2->error_data.serv_response);
1444 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001445 break;
1446 }
Scott Teela09c1442014-02-18 13:57:21 -06001447
1448 return retry; /* retry on raid path? */
Scott Teelc3497752014-02-18 13:56:34 -06001449}
1450
1451static void process_ioaccel2_completion(struct ctlr_info *h,
1452 struct CommandList *c, struct scsi_cmnd *cmd,
1453 struct hpsa_scsi_dev_t *dev)
1454{
1455 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
Scott Teela09c1442014-02-18 13:57:21 -06001456 int raid_retry = 0;
Scott Teelc3497752014-02-18 13:56:34 -06001457
1458 /* check for good status */
1459 if (likely(c2->error_data.serv_response == 0 &&
1460 c2->error_data.status == 0)) {
1461 cmd_free(h, c);
1462 cmd->scsi_done(cmd);
1463 return;
1464 }
1465
1466 /* Any RAID offload error results in retry which will use
1467 * the normal I/O path so the controller can handle whatever's
1468 * wrong.
1469 */
1470 if (is_logical_dev_addr_mode(dev->scsi3addr) &&
1471 c2->error_data.serv_response ==
1472 IOACCEL2_SERV_RESPONSE_FAILURE) {
Scott Teela09c1442014-02-18 13:57:21 -06001473 if (c2->error_data.status ==
1474 IOACCEL2_STATUS_SR_IOACCEL_DISABLED)
Scott Teelc3497752014-02-18 13:56:34 -06001475 dev_warn(&h->pdev->dev,
Scott Teela09c1442014-02-18 13:57:21 -06001476 "%s: Path is unavailable, retrying on standard path.\n",
1477 "HP SSD Smart Path");
1478 else
1479 dev_warn(&h->pdev->dev,
1480 "%s: Error 0x%02x, retrying on standard path.\n",
Scott Teelc3497752014-02-18 13:56:34 -06001481 "HP SSD Smart Path", c2->error_data.status);
Scott Teela09c1442014-02-18 13:57:21 -06001482
Scott Teelc3497752014-02-18 13:56:34 -06001483 dev->offload_enabled = 0;
Scott Teele863d682014-02-18 13:57:05 -06001484 h->drv_req_rescan = 1; /* schedule controller for a rescan */
Scott Teelc3497752014-02-18 13:56:34 -06001485 cmd->result = DID_SOFT_ERROR << 16;
1486 cmd_free(h, c);
1487 cmd->scsi_done(cmd);
1488 return;
1489 }
Scott Teela09c1442014-02-18 13:57:21 -06001490 raid_retry = handle_ioaccel_mode2_error(h, c, cmd, c2);
1491 /* If error found, disable Smart Path, schedule a rescan,
1492 * and force a retry on the standard path.
1493 */
1494 if (raid_retry) {
1495 dev_warn(&h->pdev->dev, "%s: Retrying on standard path.\n",
1496 "HP SSD Smart Path");
1497 dev->offload_enabled = 0; /* Disable Smart Path */
1498 h->drv_req_rescan = 1; /* schedule controller rescan */
1499 cmd->result = DID_SOFT_ERROR << 16;
1500 }
Scott Teelc3497752014-02-18 13:56:34 -06001501 cmd_free(h, c);
1502 cmd->scsi_done(cmd);
1503}
1504
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05001505static void complete_scsi_command(struct CommandList *cp)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001506{
1507 struct scsi_cmnd *cmd;
1508 struct ctlr_info *h;
1509 struct ErrorInfo *ei;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001510 struct hpsa_scsi_dev_t *dev;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001511
1512 unsigned char sense_key;
1513 unsigned char asc; /* additional sense code */
1514 unsigned char ascq; /* additional sense code qualifier */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05001515 unsigned long sense_data_size;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001516
1517 ei = cp->err_info;
1518 cmd = (struct scsi_cmnd *) cp->scsi_cmd;
1519 h = cp->h;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001520 dev = cmd->device->hostdata;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001521
1522 scsi_dma_unmap(cmd); /* undo the DMA mappings */
Matt Gatese1f7de02014-02-18 13:55:17 -06001523 if ((cp->cmd_type == CMD_SCSI) &&
1524 (cp->Header.SGTotal > h->max_cmd_sg_entries))
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001525 hpsa_unmap_sg_chain_block(h, cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001526
1527 cmd->result = (DID_OK << 16); /* host byte */
1528 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
Scott Teelc3497752014-02-18 13:56:34 -06001529
1530 if (cp->cmd_type == CMD_IOACCEL2)
1531 return process_ioaccel2_completion(h, cp, cmd, dev);
1532
Stephen M. Cameron55126722010-02-25 14:03:01 -06001533 cmd->result |= ei->ScsiStatus;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001534
1535 /* copy the sense data whether we need to or not. */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05001536 if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
1537 sense_data_size = SCSI_SENSE_BUFFERSIZE;
1538 else
1539 sense_data_size = sizeof(ei->SenseInfo);
1540 if (ei->SenseLen < sense_data_size)
1541 sense_data_size = ei->SenseLen;
1542
1543 memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001544 scsi_set_resid(cmd, ei->ResidualCnt);
1545
1546 if (ei->CommandStatus == 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001547 cmd_free(h, cp);
Tomas Henzl2cc5bfa2013-08-01 15:14:00 +02001548 cmd->scsi_done(cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001549 return;
1550 }
1551
Matt Gatese1f7de02014-02-18 13:55:17 -06001552 /* For I/O accelerator commands, copy over some fields to the normal
1553 * CISS header used below for error handling.
1554 */
1555 if (cp->cmd_type == CMD_IOACCEL1) {
1556 struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
1557 cp->Header.SGList = cp->Header.SGTotal = scsi_sg_count(cmd);
1558 cp->Request.CDBLen = c->io_flags & IOACCEL1_IOFLAGS_CDBLEN_MASK;
1559 cp->Header.Tag.lower = c->Tag.lower;
1560 cp->Header.Tag.upper = c->Tag.upper;
1561 memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
1562 memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001563
1564 /* Any RAID offload error results in retry which will use
1565 * the normal I/O path so the controller can handle whatever's
1566 * wrong.
1567 */
1568 if (is_logical_dev_addr_mode(dev->scsi3addr)) {
1569 if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
1570 dev->offload_enabled = 0;
1571 cmd->result = DID_SOFT_ERROR << 16;
1572 cmd_free(h, cp);
1573 cmd->scsi_done(cmd);
1574 return;
1575 }
Matt Gatese1f7de02014-02-18 13:55:17 -06001576 }
1577
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001578 /* an error has occurred */
1579 switch (ei->CommandStatus) {
1580
1581 case CMD_TARGET_STATUS:
1582 if (ei->ScsiStatus) {
1583 /* Get sense key */
1584 sense_key = 0xf & ei->SenseInfo[2];
1585 /* Get additional sense code */
1586 asc = ei->SenseInfo[12];
1587 /* Get addition sense code qualifier */
1588 ascq = ei->SenseInfo[13];
1589 }
1590
1591 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
Matt Gates3ce438d2013-12-04 17:10:36 -06001592 if (check_for_unit_attention(h, cp))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001593 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001594 if (sense_key == ILLEGAL_REQUEST) {
1595 /*
1596 * SCSI REPORT_LUNS is commonly unsupported on
1597 * Smart Array. Suppress noisy complaint.
1598 */
1599 if (cp->Request.CDB[0] == REPORT_LUNS)
1600 break;
1601
1602 /* If ASC/ASCQ indicate Logical Unit
1603 * Not Supported condition,
1604 */
1605 if ((asc == 0x25) && (ascq == 0x0)) {
1606 dev_warn(&h->pdev->dev, "cp %p "
1607 "has check condition\n", cp);
1608 break;
1609 }
1610 }
1611
1612 if (sense_key == NOT_READY) {
1613 /* If Sense is Not Ready, Logical Unit
1614 * Not ready, Manual Intervention
1615 * required
1616 */
1617 if ((asc == 0x04) && (ascq == 0x03)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001618 dev_warn(&h->pdev->dev, "cp %p "
1619 "has check condition: unit "
1620 "not ready, manual "
1621 "intervention required\n", cp);
1622 break;
1623 }
1624 }
Matt Gates1d3b3602010-02-04 08:43:00 -06001625 if (sense_key == ABORTED_COMMAND) {
1626 /* Aborted command is retryable */
1627 dev_warn(&h->pdev->dev, "cp %p "
1628 "has check condition: aborted command: "
1629 "ASC: 0x%x, ASCQ: 0x%x\n",
1630 cp, asc, ascq);
Stephen M. Cameron2e311fb2013-09-23 13:33:41 -05001631 cmd->result |= DID_SOFT_ERROR << 16;
Matt Gates1d3b3602010-02-04 08:43:00 -06001632 break;
1633 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001634 /* Must be some other type of check condition */
Stephen M. Cameron21b8e4e2012-05-01 11:42:25 -05001635 dev_dbg(&h->pdev->dev, "cp %p has check condition: "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001636 "unknown type: "
1637 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1638 "Returning result: 0x%x, "
1639 "cmd=[%02x %02x %02x %02x %02x "
Mike Miller807be732010-02-04 08:43:26 -06001640 "%02x %02x %02x %02x %02x %02x "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001641 "%02x %02x %02x %02x %02x]\n",
1642 cp, sense_key, asc, ascq,
1643 cmd->result,
1644 cmd->cmnd[0], cmd->cmnd[1],
1645 cmd->cmnd[2], cmd->cmnd[3],
1646 cmd->cmnd[4], cmd->cmnd[5],
1647 cmd->cmnd[6], cmd->cmnd[7],
Mike Miller807be732010-02-04 08:43:26 -06001648 cmd->cmnd[8], cmd->cmnd[9],
1649 cmd->cmnd[10], cmd->cmnd[11],
1650 cmd->cmnd[12], cmd->cmnd[13],
1651 cmd->cmnd[14], cmd->cmnd[15]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001652 break;
1653 }
1654
1655
1656 /* Problem was not a check condition
1657 * Pass it up to the upper layers...
1658 */
1659 if (ei->ScsiStatus) {
1660 dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
1661 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1662 "Returning result: 0x%x\n",
1663 cp, ei->ScsiStatus,
1664 sense_key, asc, ascq,
1665 cmd->result);
1666 } else { /* scsi status is zero??? How??? */
1667 dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
1668 "Returning no connection.\n", cp),
1669
1670 /* Ordinarily, this case should never happen,
1671 * but there is a bug in some released firmware
1672 * revisions that allows it to happen if, for
1673 * example, a 4100 backplane loses power and
1674 * the tape drive is in it. We assume that
1675 * it's a fatal error of some kind because we
1676 * can't show that it wasn't. We will make it
1677 * look like selection timeout since that is
1678 * the most common reason for this to occur,
1679 * and it's severe enough.
1680 */
1681
1682 cmd->result = DID_NO_CONNECT << 16;
1683 }
1684 break;
1685
1686 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1687 break;
1688 case CMD_DATA_OVERRUN:
1689 dev_warn(&h->pdev->dev, "cp %p has"
1690 " completed with data overrun "
1691 "reported\n", cp);
1692 break;
1693 case CMD_INVALID: {
1694 /* print_bytes(cp, sizeof(*cp), 1, 0);
1695 print_cmd(cp); */
1696 /* We get CMD_INVALID if you address a non-existent device
1697 * instead of a selection timeout (no response). You will
1698 * see this if you yank out a drive, then try to access it.
1699 * This is kind of a shame because it means that any other
1700 * CMD_INVALID (e.g. driver bug) will get interpreted as a
1701 * missing target. */
1702 cmd->result = DID_NO_CONNECT << 16;
1703 }
1704 break;
1705 case CMD_PROTOCOL_ERR:
Stephen M. Cameron256d0ea2012-09-14 16:34:25 -05001706 cmd->result = DID_ERROR << 16;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001707 dev_warn(&h->pdev->dev, "cp %p has "
Stephen M. Cameron256d0ea2012-09-14 16:34:25 -05001708 "protocol error\n", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001709 break;
1710 case CMD_HARDWARE_ERR:
1711 cmd->result = DID_ERROR << 16;
1712 dev_warn(&h->pdev->dev, "cp %p had hardware error\n", cp);
1713 break;
1714 case CMD_CONNECTION_LOST:
1715 cmd->result = DID_ERROR << 16;
1716 dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
1717 break;
1718 case CMD_ABORTED:
1719 cmd->result = DID_ABORT << 16;
1720 dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
1721 cp, ei->ScsiStatus);
1722 break;
1723 case CMD_ABORT_FAILED:
1724 cmd->result = DID_ERROR << 16;
1725 dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
1726 break;
1727 case CMD_UNSOLICITED_ABORT:
Stephen M. Cameronf6e76052011-07-26 11:08:52 -05001728 cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
1729 dev_warn(&h->pdev->dev, "cp %p aborted due to an unsolicited "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001730 "abort\n", cp);
1731 break;
1732 case CMD_TIMEOUT:
1733 cmd->result = DID_TIME_OUT << 16;
1734 dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
1735 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06001736 case CMD_UNABORTABLE:
1737 cmd->result = DID_ERROR << 16;
1738 dev_warn(&h->pdev->dev, "Command unabortable\n");
1739 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001740 case CMD_IOACCEL_DISABLED:
1741 /* This only handles the direct pass-through case since RAID
1742 * offload is handled above. Just attempt a retry.
1743 */
1744 cmd->result = DID_SOFT_ERROR << 16;
1745 dev_warn(&h->pdev->dev,
1746 "cp %p had HP SSD Smart Path error\n", cp);
1747 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001748 default:
1749 cmd->result = DID_ERROR << 16;
1750 dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
1751 cp, ei->CommandStatus);
1752 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001753 cmd_free(h, cp);
Tomas Henzl2cc5bfa2013-08-01 15:14:00 +02001754 cmd->scsi_done(cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001755}
1756
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001757static void hpsa_pci_unmap(struct pci_dev *pdev,
1758 struct CommandList *c, int sg_used, int data_direction)
1759{
1760 int i;
1761 union u64bit addr64;
1762
1763 for (i = 0; i < sg_used; i++) {
1764 addr64.val32.lower = c->SG[i].Addr.lower;
1765 addr64.val32.upper = c->SG[i].Addr.upper;
1766 pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len,
1767 data_direction);
1768 }
1769}
1770
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001771static int hpsa_map_one(struct pci_dev *pdev,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001772 struct CommandList *cp,
1773 unsigned char *buf,
1774 size_t buflen,
1775 int data_direction)
1776{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001777 u64 addr64;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001778
1779 if (buflen == 0 || data_direction == PCI_DMA_NONE) {
1780 cp->Header.SGList = 0;
1781 cp->Header.SGTotal = 0;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001782 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001783 }
1784
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001785 addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction);
Shuah Khaneceaae12013-02-20 11:24:34 -06001786 if (dma_mapping_error(&pdev->dev, addr64)) {
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001787 /* Prevent subsequent unmap of something never mapped */
Shuah Khaneceaae12013-02-20 11:24:34 -06001788 cp->Header.SGList = 0;
1789 cp->Header.SGTotal = 0;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001790 return -1;
Shuah Khaneceaae12013-02-20 11:24:34 -06001791 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001792 cp->SG[0].Addr.lower =
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001793 (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001794 cp->SG[0].Addr.upper =
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001795 (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001796 cp->SG[0].Len = buflen;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06001797 cp->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining */
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001798 cp->Header.SGList = (u8) 1; /* no. SGs contig in this cmd */
1799 cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001800 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001801}
1802
1803static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
1804 struct CommandList *c)
1805{
1806 DECLARE_COMPLETION_ONSTACK(wait);
1807
1808 c->waiting = &wait;
1809 enqueue_cmd_and_start_io(h, c);
1810 wait_for_completion(&wait);
1811}
1812
Stephen M. Camerona0c12412011-10-26 16:22:04 -05001813static void hpsa_scsi_do_simple_cmd_core_if_no_lockup(struct ctlr_info *h,
1814 struct CommandList *c)
1815{
1816 unsigned long flags;
1817
1818 /* If controller lockup detected, fake a hardware error. */
1819 spin_lock_irqsave(&h->lock, flags);
1820 if (unlikely(h->lockup_detected)) {
1821 spin_unlock_irqrestore(&h->lock, flags);
1822 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
1823 } else {
1824 spin_unlock_irqrestore(&h->lock, flags);
1825 hpsa_scsi_do_simple_cmd_core(h, c);
1826 }
1827}
1828
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001829#define MAX_DRIVER_CMD_RETRIES 25
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001830static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
1831 struct CommandList *c, int data_direction)
1832{
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001833 int backoff_time = 10, retry_count = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001834
1835 do {
Joe Perches7630abd2011-05-08 23:32:40 -07001836 memset(c->err_info, 0, sizeof(*c->err_info));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001837 hpsa_scsi_do_simple_cmd_core(h, c);
1838 retry_count++;
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001839 if (retry_count > 3) {
1840 msleep(backoff_time);
1841 if (backoff_time < 1000)
1842 backoff_time *= 2;
1843 }
Matt Bondurant852af202012-05-01 11:42:35 -05001844 } while ((check_for_unit_attention(h, c) ||
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001845 check_for_busy(h, c)) &&
1846 retry_count <= MAX_DRIVER_CMD_RETRIES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001847 hpsa_pci_unmap(h->pdev, c, 1, data_direction);
1848}
1849
1850static void hpsa_scsi_interpret_error(struct CommandList *cp)
1851{
1852 struct ErrorInfo *ei;
1853 struct device *d = &cp->h->pdev->dev;
1854
1855 ei = cp->err_info;
1856 switch (ei->CommandStatus) {
1857 case CMD_TARGET_STATUS:
1858 dev_warn(d, "cmd %p has completed with errors\n", cp);
1859 dev_warn(d, "cmd %p has SCSI Status = %x\n", cp,
1860 ei->ScsiStatus);
1861 if (ei->ScsiStatus == 0)
1862 dev_warn(d, "SCSI status is abnormally zero. "
1863 "(probably indicates selection timeout "
1864 "reported incorrectly due to a known "
1865 "firmware bug, circa July, 2001.)\n");
1866 break;
1867 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1868 dev_info(d, "UNDERRUN\n");
1869 break;
1870 case CMD_DATA_OVERRUN:
1871 dev_warn(d, "cp %p has completed with data overrun\n", cp);
1872 break;
1873 case CMD_INVALID: {
1874 /* controller unfortunately reports SCSI passthru's
1875 * to non-existent targets as invalid commands.
1876 */
1877 dev_warn(d, "cp %p is reported invalid (probably means "
1878 "target device no longer present)\n", cp);
1879 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
1880 print_cmd(cp); */
1881 }
1882 break;
1883 case CMD_PROTOCOL_ERR:
1884 dev_warn(d, "cp %p has protocol error \n", cp);
1885 break;
1886 case CMD_HARDWARE_ERR:
1887 /* cmd->result = DID_ERROR << 16; */
1888 dev_warn(d, "cp %p had hardware error\n", cp);
1889 break;
1890 case CMD_CONNECTION_LOST:
1891 dev_warn(d, "cp %p had connection lost\n", cp);
1892 break;
1893 case CMD_ABORTED:
1894 dev_warn(d, "cp %p was aborted\n", cp);
1895 break;
1896 case CMD_ABORT_FAILED:
1897 dev_warn(d, "cp %p reports abort failed\n", cp);
1898 break;
1899 case CMD_UNSOLICITED_ABORT:
1900 dev_warn(d, "cp %p aborted due to an unsolicited abort\n", cp);
1901 break;
1902 case CMD_TIMEOUT:
1903 dev_warn(d, "cp %p timed out\n", cp);
1904 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06001905 case CMD_UNABORTABLE:
1906 dev_warn(d, "Command unabortable\n");
1907 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001908 default:
1909 dev_warn(d, "cp %p returned unknown status %x\n", cp,
1910 ei->CommandStatus);
1911 }
1912}
1913
1914static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06001915 u16 page, unsigned char *buf,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001916 unsigned char bufsize)
1917{
1918 int rc = IO_OK;
1919 struct CommandList *c;
1920 struct ErrorInfo *ei;
1921
1922 c = cmd_special_alloc(h);
1923
1924 if (c == NULL) { /* trouble... */
1925 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06001926 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001927 }
1928
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001929 if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
1930 page, scsi3addr, TYPE_CMD)) {
1931 rc = -1;
1932 goto out;
1933 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001934 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1935 ei = c->err_info;
1936 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
1937 hpsa_scsi_interpret_error(c);
1938 rc = -1;
1939 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001940out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001941 cmd_special_free(h, c);
1942 return rc;
1943}
1944
Scott Teelbf711ac2014-02-18 13:56:39 -06001945static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
1946 u8 reset_type)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001947{
1948 int rc = IO_OK;
1949 struct CommandList *c;
1950 struct ErrorInfo *ei;
1951
1952 c = cmd_special_alloc(h);
1953
1954 if (c == NULL) { /* trouble... */
1955 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
Stephen M. Camerone9ea04a2010-02-25 14:03:06 -06001956 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001957 }
1958
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001959 /* fill_cmd can't fail here, no data buffer to map. */
Scott Teelbf711ac2014-02-18 13:56:39 -06001960 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
1961 scsi3addr, TYPE_MSG);
1962 c->Request.CDB[1] = reset_type; /* fill_cmd defaults to LUN reset */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001963 hpsa_scsi_do_simple_cmd_core(h, c);
1964 /* no unmap needed here because no data xfer. */
1965
1966 ei = c->err_info;
1967 if (ei->CommandStatus != 0) {
1968 hpsa_scsi_interpret_error(c);
1969 rc = -1;
1970 }
1971 cmd_special_free(h, c);
1972 return rc;
1973}
1974
1975static void hpsa_get_raid_level(struct ctlr_info *h,
1976 unsigned char *scsi3addr, unsigned char *raid_level)
1977{
1978 int rc;
1979 unsigned char *buf;
1980
1981 *raid_level = RAID_UNKNOWN;
1982 buf = kzalloc(64, GFP_KERNEL);
1983 if (!buf)
1984 return;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06001985 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0xC1, buf, 64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001986 if (rc == 0)
1987 *raid_level = buf[8];
1988 if (*raid_level > RAID_UNKNOWN)
1989 *raid_level = RAID_UNKNOWN;
1990 kfree(buf);
1991 return;
1992}
1993
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001994#define HPSA_MAP_DEBUG
1995#ifdef HPSA_MAP_DEBUG
1996static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
1997 struct raid_map_data *map_buff)
1998{
1999 struct raid_map_disk_data *dd = &map_buff->data[0];
2000 int map, row, col;
2001 u16 map_cnt, row_cnt, disks_per_row;
2002
2003 if (rc != 0)
2004 return;
2005
2006 dev_info(&h->pdev->dev, "structure_size = %u\n",
2007 le32_to_cpu(map_buff->structure_size));
2008 dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
2009 le32_to_cpu(map_buff->volume_blk_size));
2010 dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
2011 le64_to_cpu(map_buff->volume_blk_cnt));
2012 dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
2013 map_buff->phys_blk_shift);
2014 dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
2015 map_buff->parity_rotation_shift);
2016 dev_info(&h->pdev->dev, "strip_size = %u\n",
2017 le16_to_cpu(map_buff->strip_size));
2018 dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
2019 le64_to_cpu(map_buff->disk_starting_blk));
2020 dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
2021 le64_to_cpu(map_buff->disk_blk_cnt));
2022 dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
2023 le16_to_cpu(map_buff->data_disks_per_row));
2024 dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
2025 le16_to_cpu(map_buff->metadata_disks_per_row));
2026 dev_info(&h->pdev->dev, "row_cnt = %u\n",
2027 le16_to_cpu(map_buff->row_cnt));
2028 dev_info(&h->pdev->dev, "layout_map_count = %u\n",
2029 le16_to_cpu(map_buff->layout_map_count));
2030
2031 map_cnt = le16_to_cpu(map_buff->layout_map_count);
2032 for (map = 0; map < map_cnt; map++) {
2033 dev_info(&h->pdev->dev, "Map%u:\n", map);
2034 row_cnt = le16_to_cpu(map_buff->row_cnt);
2035 for (row = 0; row < row_cnt; row++) {
2036 dev_info(&h->pdev->dev, " Row%u:\n", row);
2037 disks_per_row =
2038 le16_to_cpu(map_buff->data_disks_per_row);
2039 for (col = 0; col < disks_per_row; col++, dd++)
2040 dev_info(&h->pdev->dev,
2041 " D%02u: h=0x%04x xor=%u,%u\n",
2042 col, dd->ioaccel_handle,
2043 dd->xor_mult[0], dd->xor_mult[1]);
2044 disks_per_row =
2045 le16_to_cpu(map_buff->metadata_disks_per_row);
2046 for (col = 0; col < disks_per_row; col++, dd++)
2047 dev_info(&h->pdev->dev,
2048 " M%02u: h=0x%04x xor=%u,%u\n",
2049 col, dd->ioaccel_handle,
2050 dd->xor_mult[0], dd->xor_mult[1]);
2051 }
2052 }
2053}
2054#else
2055static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
2056 __attribute__((unused)) int rc,
2057 __attribute__((unused)) struct raid_map_data *map_buff)
2058{
2059}
2060#endif
2061
2062static int hpsa_get_raid_map(struct ctlr_info *h,
2063 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2064{
2065 int rc = 0;
2066 struct CommandList *c;
2067 struct ErrorInfo *ei;
2068
2069 c = cmd_special_alloc(h);
2070 if (c == NULL) {
2071 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2072 return -ENOMEM;
2073 }
2074 if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
2075 sizeof(this_device->raid_map), 0,
2076 scsi3addr, TYPE_CMD)) {
2077 dev_warn(&h->pdev->dev, "Out of memory in hpsa_get_raid_map()\n");
2078 cmd_special_free(h, c);
2079 return -ENOMEM;
2080 }
2081 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2082 ei = c->err_info;
2083 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2084 hpsa_scsi_interpret_error(c);
2085 cmd_special_free(h, c);
2086 return -1;
2087 }
2088 cmd_special_free(h, c);
2089
2090 /* @todo in the future, dynamically allocate RAID map memory */
2091 if (le32_to_cpu(this_device->raid_map.structure_size) >
2092 sizeof(this_device->raid_map)) {
2093 dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
2094 rc = -1;
2095 }
2096 hpsa_debug_map_buff(h, rc, &this_device->raid_map);
2097 return rc;
2098}
2099
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06002100static int hpsa_vpd_page_supported(struct ctlr_info *h,
2101 unsigned char scsi3addr[], u8 page)
2102{
2103 int rc;
2104 int i;
2105 int pages;
2106 unsigned char *buf, bufsize;
2107
2108 buf = kzalloc(256, GFP_KERNEL);
2109 if (!buf)
2110 return 0;
2111
2112 /* Get the size of the page list first */
2113 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
2114 VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
2115 buf, HPSA_VPD_HEADER_SZ);
2116 if (rc != 0)
2117 goto exit_unsupported;
2118 pages = buf[3];
2119 if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
2120 bufsize = pages + HPSA_VPD_HEADER_SZ;
2121 else
2122 bufsize = 255;
2123
2124 /* Get the whole VPD page list */
2125 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
2126 VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
2127 buf, bufsize);
2128 if (rc != 0)
2129 goto exit_unsupported;
2130
2131 pages = buf[3];
2132 for (i = 1; i <= pages; i++)
2133 if (buf[3 + i] == page)
2134 goto exit_supported;
2135exit_unsupported:
2136 kfree(buf);
2137 return 0;
2138exit_supported:
2139 kfree(buf);
2140 return 1;
2141}
2142
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002143static void hpsa_get_ioaccel_status(struct ctlr_info *h,
2144 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2145{
2146 int rc;
2147 unsigned char *buf;
2148 u8 ioaccel_status;
2149
2150 this_device->offload_config = 0;
2151 this_device->offload_enabled = 0;
2152
2153 buf = kzalloc(64, GFP_KERNEL);
2154 if (!buf)
2155 return;
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06002156 if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
2157 goto out;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002158 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06002159 VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002160 if (rc != 0)
2161 goto out;
2162
2163#define IOACCEL_STATUS_BYTE 4
2164#define OFFLOAD_CONFIGURED_BIT 0x01
2165#define OFFLOAD_ENABLED_BIT 0x02
2166 ioaccel_status = buf[IOACCEL_STATUS_BYTE];
2167 this_device->offload_config =
2168 !!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
2169 if (this_device->offload_config) {
2170 this_device->offload_enabled =
2171 !!(ioaccel_status & OFFLOAD_ENABLED_BIT);
2172 if (hpsa_get_raid_map(h, scsi3addr, this_device))
2173 this_device->offload_enabled = 0;
2174 }
2175out:
2176 kfree(buf);
2177 return;
2178}
2179
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002180/* Get the device id from inquiry page 0x83 */
2181static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
2182 unsigned char *device_id, int buflen)
2183{
2184 int rc;
2185 unsigned char *buf;
2186
2187 if (buflen > 16)
2188 buflen = 16;
2189 buf = kzalloc(64, GFP_KERNEL);
2190 if (!buf)
2191 return -1;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06002192 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0x83, buf, 64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002193 if (rc == 0)
2194 memcpy(device_id, &buf[8], buflen);
2195 kfree(buf);
2196 return rc != 0;
2197}
2198
2199static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
2200 struct ReportLUNdata *buf, int bufsize,
2201 int extended_response)
2202{
2203 int rc = IO_OK;
2204 struct CommandList *c;
2205 unsigned char scsi3addr[8];
2206 struct ErrorInfo *ei;
2207
2208 c = cmd_special_alloc(h);
2209 if (c == NULL) { /* trouble... */
2210 dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2211 return -1;
2212 }
Stephen M. Camerone89c0ae2010-02-04 08:42:04 -06002213 /* address the controller */
2214 memset(scsi3addr, 0, sizeof(scsi3addr));
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002215 if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
2216 buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
2217 rc = -1;
2218 goto out;
2219 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002220 if (extended_response)
2221 c->Request.CDB[1] = extended_response;
2222 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2223 ei = c->err_info;
2224 if (ei->CommandStatus != 0 &&
2225 ei->CommandStatus != CMD_DATA_UNDERRUN) {
2226 hpsa_scsi_interpret_error(c);
2227 rc = -1;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002228 } else {
2229 if (buf->extended_response_flag != extended_response) {
2230 dev_err(&h->pdev->dev,
2231 "report luns requested format %u, got %u\n",
2232 extended_response,
2233 buf->extended_response_flag);
2234 rc = -1;
2235 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002236 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002237out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002238 cmd_special_free(h, c);
2239 return rc;
2240}
2241
2242static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
2243 struct ReportLUNdata *buf,
2244 int bufsize, int extended_response)
2245{
2246 return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
2247}
2248
2249static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
2250 struct ReportLUNdata *buf, int bufsize)
2251{
2252 return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
2253}
2254
2255static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
2256 int bus, int target, int lun)
2257{
2258 device->bus = bus;
2259 device->target = target;
2260 device->lun = lun;
2261}
2262
2263static int hpsa_update_device_info(struct ctlr_info *h,
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002264 unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
2265 unsigned char *is_OBDR_device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002266{
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002267
2268#define OBDR_SIG_OFFSET 43
2269#define OBDR_TAPE_SIG "$DR-10"
2270#define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
2271#define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
2272
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06002273 unsigned char *inq_buff;
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002274 unsigned char *obdr_sig;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002275
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06002276 inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002277 if (!inq_buff)
2278 goto bail_out;
2279
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002280 /* Do an inquiry to the device to see what it is. */
2281 if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
2282 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
2283 /* Inquiry failed (msg printed already) */
2284 dev_err(&h->pdev->dev,
2285 "hpsa_update_device_info: inquiry failed\n");
2286 goto bail_out;
2287 }
2288
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002289 this_device->devtype = (inq_buff[0] & 0x1f);
2290 memcpy(this_device->scsi3addr, scsi3addr, 8);
2291 memcpy(this_device->vendor, &inq_buff[8],
2292 sizeof(this_device->vendor));
2293 memcpy(this_device->model, &inq_buff[16],
2294 sizeof(this_device->model));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002295 memset(this_device->device_id, 0,
2296 sizeof(this_device->device_id));
2297 hpsa_get_device_id(h, scsi3addr, this_device->device_id,
2298 sizeof(this_device->device_id));
2299
2300 if (this_device->devtype == TYPE_DISK &&
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002301 is_logical_dev_addr_mode(scsi3addr)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002302 hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002303 if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
2304 hpsa_get_ioaccel_status(h, scsi3addr, this_device);
2305 } else {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002306 this_device->raid_level = RAID_UNKNOWN;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002307 this_device->offload_config = 0;
2308 this_device->offload_enabled = 0;
2309 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002310
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002311 if (is_OBDR_device) {
2312 /* See if this is a One-Button-Disaster-Recovery device
2313 * by looking for "$DR-10" at offset 43 in inquiry data.
2314 */
2315 obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
2316 *is_OBDR_device = (this_device->devtype == TYPE_ROM &&
2317 strncmp(obdr_sig, OBDR_TAPE_SIG,
2318 OBDR_SIG_LEN) == 0);
2319 }
2320
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002321 kfree(inq_buff);
2322 return 0;
2323
2324bail_out:
2325 kfree(inq_buff);
2326 return 1;
2327}
2328
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002329static unsigned char *ext_target_model[] = {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002330 "MSA2012",
2331 "MSA2024",
2332 "MSA2312",
2333 "MSA2324",
Stephen M. Cameronfda38512011-05-03 15:00:07 -05002334 "P2000 G3 SAS",
Stephen M. Camerone06c8e52013-09-23 13:33:56 -05002335 "MSA 2040 SAS",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002336 NULL,
2337};
2338
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002339static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002340{
2341 int i;
2342
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002343 for (i = 0; ext_target_model[i]; i++)
2344 if (strncmp(device->model, ext_target_model[i],
2345 strlen(ext_target_model[i])) == 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002346 return 1;
2347 return 0;
2348}
2349
2350/* Helper function to assign bus, target, lun mapping of devices.
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002351 * Puts non-external target logical volumes on bus 0, external target logical
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002352 * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
2353 * Logical drive target and lun are assigned at this time, but
2354 * physical device lun and target assignment are deferred (assigned
2355 * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
2356 */
2357static void figure_bus_target_lun(struct ctlr_info *h,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002358 u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002359{
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002360 u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002361
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002362 if (!is_logical_dev_addr_mode(lunaddrbytes)) {
2363 /* physical device, target and lun filled in later */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002364 if (is_hba_lunid(lunaddrbytes))
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002365 hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002366 else
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002367 /* defer target, lun assignment for physical devices */
2368 hpsa_set_bus_target_lun(device, 2, -1, -1);
2369 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002370 }
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002371 /* It's a logical device */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002372 if (is_ext_target(h, device)) {
2373 /* external target way, put logicals on bus 1
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002374 * and match target/lun numbers box
2375 * reports, other smart array, bus 0, target 0, match lunid
2376 */
2377 hpsa_set_bus_target_lun(device,
2378 1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
2379 return;
2380 }
2381 hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002382}
2383
2384/*
2385 * If there is no lun 0 on a target, linux won't find any devices.
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002386 * For the external targets (arrays), we have to manually detect the enclosure
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002387 * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
2388 * it for some reason. *tmpdevice is the target we're adding,
2389 * this_device is a pointer into the current element of currentsd[]
2390 * that we're building up in update_scsi_devices(), below.
2391 * lunzerobits is a bitmap that tracks which targets already have a
2392 * lun 0 assigned.
2393 * Returns 1 if an enclosure was added, 0 if not.
2394 */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002395static int add_ext_target_dev(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002396 struct hpsa_scsi_dev_t *tmpdevice,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002397 struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002398 unsigned long lunzerobits[], int *n_ext_target_devs)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002399{
2400 unsigned char scsi3addr[8];
2401
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002402 if (test_bit(tmpdevice->target, lunzerobits))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002403 return 0; /* There is already a lun 0 on this target. */
2404
2405 if (!is_logical_dev_addr_mode(lunaddrbytes))
2406 return 0; /* It's the logical targets that may lack lun 0. */
2407
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002408 if (!is_ext_target(h, tmpdevice))
2409 return 0; /* Only external target devices have this problem. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002410
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002411 if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002412 return 0;
2413
Stephen M. Cameronc4f8a292011-01-07 10:55:43 -06002414 memset(scsi3addr, 0, 8);
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002415 scsi3addr[3] = tmpdevice->target;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002416 if (is_hba_lunid(scsi3addr))
2417 return 0; /* Don't add the RAID controller here. */
2418
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002419 if (is_scsi_rev_5(h))
2420 return 0; /* p1210m doesn't need to do this. */
2421
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002422 if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
Scott Teelaca4a522012-01-19 14:01:19 -06002423 dev_warn(&h->pdev->dev, "Maximum number of external "
2424 "target devices exceeded. Check your hardware "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002425 "configuration.");
2426 return 0;
2427 }
2428
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002429 if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002430 return 0;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002431 (*n_ext_target_devs)++;
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002432 hpsa_set_bus_target_lun(this_device,
2433 tmpdevice->bus, tmpdevice->target, 0);
2434 set_bit(tmpdevice->target, lunzerobits);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002435 return 1;
2436}
2437
2438/*
Scott Teel54b6e9e2014-02-18 13:56:45 -06002439 * Get address of physical disk used for an ioaccel2 mode command:
2440 * 1. Extract ioaccel2 handle from the command.
2441 * 2. Find a matching ioaccel2 handle from list of physical disks.
2442 * 3. Return:
2443 * 1 and set scsi3addr to address of matching physical
2444 * 0 if no matching physical disk was found.
2445 */
2446static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
2447 struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
2448{
2449 struct ReportExtendedLUNdata *physicals = NULL;
2450 int responsesize = 24; /* size of physical extended response */
2451 int extended = 2; /* flag forces reporting 'other dev info'. */
2452 int reportsize = sizeof(*physicals) + HPSA_MAX_PHYS_LUN * responsesize;
2453 u32 nphysicals = 0; /* number of reported physical devs */
2454 int found = 0; /* found match (1) or not (0) */
2455 u32 find; /* handle we need to match */
2456 int i;
2457 struct scsi_cmnd *scmd; /* scsi command within request being aborted */
2458 struct hpsa_scsi_dev_t *d; /* device of request being aborted */
2459 struct io_accel2_cmd *c2a; /* ioaccel2 command to abort */
2460 u32 it_nexus; /* 4 byte device handle for the ioaccel2 cmd */
2461 u32 scsi_nexus; /* 4 byte device handle for the ioaccel2 cmd */
2462
2463 if (ioaccel2_cmd_to_abort->cmd_type != CMD_IOACCEL2)
2464 return 0; /* no match */
2465
2466 /* point to the ioaccel2 device handle */
2467 c2a = &h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
2468 if (c2a == NULL)
2469 return 0; /* no match */
2470
2471 scmd = (struct scsi_cmnd *) ioaccel2_cmd_to_abort->scsi_cmd;
2472 if (scmd == NULL)
2473 return 0; /* no match */
2474
2475 d = scmd->device->hostdata;
2476 if (d == NULL)
2477 return 0; /* no match */
2478
2479 it_nexus = cpu_to_le32((u32) d->ioaccel_handle);
2480 scsi_nexus = cpu_to_le32((u32) c2a->scsi_nexus);
2481 find = c2a->scsi_nexus;
2482
2483 /* Get the list of physical devices */
2484 physicals = kzalloc(reportsize, GFP_KERNEL);
2485 if (hpsa_scsi_do_report_phys_luns(h, (struct ReportLUNdata *) physicals,
2486 reportsize, extended)) {
2487 dev_err(&h->pdev->dev,
2488 "Can't lookup %s device handle: report physical LUNs failed.\n",
2489 "HP SSD Smart Path");
2490 kfree(physicals);
2491 return 0;
2492 }
2493 nphysicals = be32_to_cpu(*((__be32 *)physicals->LUNListLength)) /
2494 responsesize;
2495
2496
2497 /* find ioaccel2 handle in list of physicals: */
2498 for (i = 0; i < nphysicals; i++) {
2499 /* handle is in bytes 28-31 of each lun */
2500 if (memcmp(&((struct ReportExtendedLUNdata *)
2501 physicals)->LUN[i][20], &find, 4) != 0) {
2502 continue; /* didn't match */
2503 }
2504 found = 1;
2505 memcpy(scsi3addr, &((struct ReportExtendedLUNdata *)
2506 physicals)->LUN[i][0], 8);
2507 break; /* found it */
2508 }
2509
2510 kfree(physicals);
2511 if (found)
2512 return 1;
2513 else
2514 return 0;
2515
2516}
2517/*
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002518 * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
2519 * logdev. The number of luns in physdev and logdev are returned in
2520 * *nphysicals and *nlogicals, respectively.
2521 * Returns 0 on success, -1 otherwise.
2522 */
2523static int hpsa_gather_lun_info(struct ctlr_info *h,
2524 int reportlunsize,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002525 struct ReportLUNdata *physdev, u32 *nphysicals, int *physical_mode,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002526 struct ReportLUNdata *logdev, u32 *nlogicals)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002527{
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002528 int physical_entry_size = 8;
2529
2530 *physical_mode = 0;
2531
2532 /* For I/O accelerator mode we need to read physical device handles */
Mike MIller317d4ad2014-02-18 13:56:20 -06002533 if (h->transMethod & CFGTBL_Trans_io_accel1 ||
2534 h->transMethod & CFGTBL_Trans_io_accel2) {
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002535 *physical_mode = HPSA_REPORT_PHYS_EXTENDED;
2536 physical_entry_size = 24;
2537 }
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002538 if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002539 *physical_mode)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002540 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
2541 return -1;
2542 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002543 *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) /
2544 physical_entry_size;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002545 if (*nphysicals > HPSA_MAX_PHYS_LUN) {
2546 dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
2547 " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2548 *nphysicals - HPSA_MAX_PHYS_LUN);
2549 *nphysicals = HPSA_MAX_PHYS_LUN;
2550 }
2551 if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
2552 dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
2553 return -1;
2554 }
Stephen M. Cameron6df1e952010-02-04 08:42:19 -06002555 *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002556 /* Reject Logicals in excess of our max capability. */
2557 if (*nlogicals > HPSA_MAX_LUN) {
2558 dev_warn(&h->pdev->dev,
2559 "maximum logical LUNs (%d) exceeded. "
2560 "%d LUNs ignored.\n", HPSA_MAX_LUN,
2561 *nlogicals - HPSA_MAX_LUN);
2562 *nlogicals = HPSA_MAX_LUN;
2563 }
2564 if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
2565 dev_warn(&h->pdev->dev,
2566 "maximum logical + physical LUNs (%d) exceeded. "
2567 "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2568 *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
2569 *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
2570 }
2571 return 0;
2572}
2573
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002574u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i,
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002575 int nphysicals, int nlogicals,
2576 struct ReportExtendedLUNdata *physdev_list,
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002577 struct ReportLUNdata *logdev_list)
2578{
2579 /* Helper function, figure out where the LUN ID info is coming from
2580 * given index i, lists of physical and logical devices, where in
2581 * the list the raid controller is supposed to appear (first or last)
2582 */
2583
2584 int logicals_start = nphysicals + (raid_ctlr_position == 0);
2585 int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
2586
2587 if (i == raid_ctlr_position)
2588 return RAID_CTLR_LUNID;
2589
2590 if (i < logicals_start)
2591 return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0];
2592
2593 if (i < last_device)
2594 return &logdev_list->LUN[i - nphysicals -
2595 (raid_ctlr_position == 0)][0];
2596 BUG();
2597 return NULL;
2598}
2599
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002600static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
2601{
2602 /* the idea here is we could get notified
2603 * that some devices have changed, so we do a report
2604 * physical luns and report logical luns cmd, and adjust
2605 * our list of devices accordingly.
2606 *
2607 * The scsi3addr's of devices won't change so long as the
2608 * adapter is not reset. That means we can rescan and
2609 * tell which devices we already know about, vs. new
2610 * devices, vs. disappearing devices.
2611 */
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002612 struct ReportExtendedLUNdata *physdev_list = NULL;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002613 struct ReportLUNdata *logdev_list = NULL;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002614 u32 nphysicals = 0;
2615 u32 nlogicals = 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002616 int physical_mode = 0;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002617 u32 ndev_allocated = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002618 struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
2619 int ncurrent = 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002620 int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 24;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002621 int i, n_ext_target_devs, ndevs_to_allocate;
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002622 int raid_ctlr_position;
Scott Teelaca4a522012-01-19 14:01:19 -06002623 DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002624
Scott Teelcfe5bad2011-10-26 16:21:07 -05002625 currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002626 physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
2627 logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002628 tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
2629
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002630 if (!currentsd || !physdev_list || !logdev_list || !tmpdevice) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002631 dev_err(&h->pdev->dev, "out of memory\n");
2632 goto out;
2633 }
2634 memset(lunzerobits, 0, sizeof(lunzerobits));
2635
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002636 if (hpsa_gather_lun_info(h, reportlunsize,
2637 (struct ReportLUNdata *) physdev_list, &nphysicals,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002638 &physical_mode, logdev_list, &nlogicals))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002639 goto out;
2640
Scott Teelaca4a522012-01-19 14:01:19 -06002641 /* We might see up to the maximum number of logical and physical disks
2642 * plus external target devices, and a device for the local RAID
2643 * controller.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002644 */
Scott Teelaca4a522012-01-19 14:01:19 -06002645 ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002646
2647 /* Allocate the per device structures */
2648 for (i = 0; i < ndevs_to_allocate; i++) {
Scott Teelb7ec0212011-10-26 16:21:12 -05002649 if (i >= HPSA_MAX_DEVICES) {
2650 dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
2651 " %d devices ignored.\n", HPSA_MAX_DEVICES,
2652 ndevs_to_allocate - HPSA_MAX_DEVICES);
2653 break;
2654 }
2655
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002656 currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
2657 if (!currentsd[i]) {
2658 dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
2659 __FILE__, __LINE__);
2660 goto out;
2661 }
2662 ndev_allocated++;
2663 }
2664
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002665 if (unlikely(is_scsi_rev_5(h)))
2666 raid_ctlr_position = 0;
2667 else
2668 raid_ctlr_position = nphysicals + nlogicals;
2669
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002670 /* adjust our table of devices */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002671 n_ext_target_devs = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002672 for (i = 0; i < nphysicals + nlogicals + 1; i++) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002673 u8 *lunaddrbytes, is_OBDR = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002674
2675 /* Figure out where the LUN ID info is coming from */
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002676 lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
2677 i, nphysicals, nlogicals, physdev_list, logdev_list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002678 /* skip masked physical devices. */
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002679 if (lunaddrbytes[3] & 0xC0 &&
2680 i < nphysicals + (raid_ctlr_position == 0))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002681 continue;
2682
2683 /* Get device type, vendor, model, device id */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002684 if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
2685 &is_OBDR))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002686 continue; /* skip it if we can't talk to it. */
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002687 figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002688 this_device = currentsd[ncurrent];
2689
2690 /*
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002691 * For external target devices, we have to insert a LUN 0 which
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002692 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
2693 * is nonetheless an enclosure device there. We have to
2694 * present that otherwise linux won't find anything if
2695 * there is no lun 0.
2696 */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002697 if (add_ext_target_dev(h, tmpdevice, this_device,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002698 lunaddrbytes, lunzerobits,
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002699 &n_ext_target_devs)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002700 ncurrent++;
2701 this_device = currentsd[ncurrent];
2702 }
2703
2704 *this_device = *tmpdevice;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002705
2706 switch (this_device->devtype) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002707 case TYPE_ROM:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002708 /* We don't *really* support actual CD-ROM devices,
2709 * just "One Button Disaster Recovery" tape drive
2710 * which temporarily pretends to be a CD-ROM drive.
2711 * So we check that the device is really an OBDR tape
2712 * device by checking for "$DR-10" in bytes 43-48 of
2713 * the inquiry data.
2714 */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002715 if (is_OBDR)
2716 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002717 break;
2718 case TYPE_DISK:
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002719 if (i >= nphysicals) {
2720 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002721 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002722 }
2723 if (physical_mode == HPSA_REPORT_PHYS_EXTENDED) {
2724 memcpy(&this_device->ioaccel_handle,
2725 &lunaddrbytes[20],
2726 sizeof(this_device->ioaccel_handle));
2727 ncurrent++;
2728 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002729 break;
2730 case TYPE_TAPE:
2731 case TYPE_MEDIUM_CHANGER:
2732 ncurrent++;
2733 break;
2734 case TYPE_RAID:
2735 /* Only present the Smartarray HBA as a RAID controller.
2736 * If it's a RAID controller other than the HBA itself
2737 * (an external RAID controller, MSA500 or similar)
2738 * don't present it.
2739 */
2740 if (!is_hba_lunid(lunaddrbytes))
2741 break;
2742 ncurrent++;
2743 break;
2744 default:
2745 break;
2746 }
Scott Teelcfe5bad2011-10-26 16:21:07 -05002747 if (ncurrent >= HPSA_MAX_DEVICES)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002748 break;
2749 }
2750 adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
2751out:
2752 kfree(tmpdevice);
2753 for (i = 0; i < ndev_allocated; i++)
2754 kfree(currentsd[i]);
2755 kfree(currentsd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002756 kfree(physdev_list);
2757 kfree(logdev_list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002758}
2759
2760/* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
2761 * dma mapping and fills in the scatter gather entries of the
2762 * hpsa command, cp.
2763 */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002764static int hpsa_scatter_gather(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002765 struct CommandList *cp,
2766 struct scsi_cmnd *cmd)
2767{
2768 unsigned int len;
2769 struct scatterlist *sg;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002770 u64 addr64;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002771 int use_sg, i, sg_index, chained;
2772 struct SGDescriptor *curr_sg;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002773
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002774 BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002775
2776 use_sg = scsi_dma_map(cmd);
2777 if (use_sg < 0)
2778 return use_sg;
2779
2780 if (!use_sg)
2781 goto sglist_finished;
2782
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002783 curr_sg = cp->SG;
2784 chained = 0;
2785 sg_index = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002786 scsi_for_each_sg(cmd, sg, use_sg, i) {
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002787 if (i == h->max_cmd_sg_entries - 1 &&
2788 use_sg > h->max_cmd_sg_entries) {
2789 chained = 1;
2790 curr_sg = h->cmd_sg_list[cp->cmdindex];
2791 sg_index = 0;
2792 }
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002793 addr64 = (u64) sg_dma_address(sg);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002794 len = sg_dma_len(sg);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002795 curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
2796 curr_sg->Addr.upper = (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
2797 curr_sg->Len = len;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06002798 curr_sg->Ext = (i < scsi_sg_count(cmd) - 1) ? 0 : HPSA_SG_LAST;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002799 curr_sg++;
2800 }
2801
2802 if (use_sg + chained > h->maxSG)
2803 h->maxSG = use_sg + chained;
2804
2805 if (chained) {
2806 cp->Header.SGList = h->max_cmd_sg_entries;
2807 cp->Header.SGTotal = (u16) (use_sg + 1);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002808 if (hpsa_map_sg_chain_block(h, cp)) {
2809 scsi_dma_unmap(cmd);
2810 return -1;
2811 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002812 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002813 }
2814
2815sglist_finished:
2816
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002817 cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
2818 cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002819 return 0;
2820}
2821
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002822#define IO_ACCEL_INELIGIBLE (1)
2823static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
2824{
2825 int is_write = 0;
2826 u32 block;
2827 u32 block_cnt;
2828
2829 /* Perform some CDB fixups if needed using 10 byte reads/writes only */
2830 switch (cdb[0]) {
2831 case WRITE_6:
2832 case WRITE_12:
2833 is_write = 1;
2834 case READ_6:
2835 case READ_12:
2836 if (*cdb_len == 6) {
2837 block = (((u32) cdb[2]) << 8) | cdb[3];
2838 block_cnt = cdb[4];
2839 } else {
2840 BUG_ON(*cdb_len != 12);
2841 block = (((u32) cdb[2]) << 24) |
2842 (((u32) cdb[3]) << 16) |
2843 (((u32) cdb[4]) << 8) |
2844 cdb[5];
2845 block_cnt =
2846 (((u32) cdb[6]) << 24) |
2847 (((u32) cdb[7]) << 16) |
2848 (((u32) cdb[8]) << 8) |
2849 cdb[9];
2850 }
2851 if (block_cnt > 0xffff)
2852 return IO_ACCEL_INELIGIBLE;
2853
2854 cdb[0] = is_write ? WRITE_10 : READ_10;
2855 cdb[1] = 0;
2856 cdb[2] = (u8) (block >> 24);
2857 cdb[3] = (u8) (block >> 16);
2858 cdb[4] = (u8) (block >> 8);
2859 cdb[5] = (u8) (block);
2860 cdb[6] = 0;
2861 cdb[7] = (u8) (block_cnt >> 8);
2862 cdb[8] = (u8) (block_cnt);
2863 cdb[9] = 0;
2864 *cdb_len = 10;
2865 break;
2866 }
2867 return 0;
2868}
2869
Scott Teelc3497752014-02-18 13:56:34 -06002870static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002871 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
2872 u8 *scsi3addr)
Matt Gatese1f7de02014-02-18 13:55:17 -06002873{
2874 struct scsi_cmnd *cmd = c->scsi_cmd;
Matt Gatese1f7de02014-02-18 13:55:17 -06002875 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
2876 unsigned int len;
2877 unsigned int total_len = 0;
2878 struct scatterlist *sg;
2879 u64 addr64;
2880 int use_sg, i;
2881 struct SGDescriptor *curr_sg;
2882 u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
2883
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002884 /* TODO: implement chaining support */
2885 if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
2886 return IO_ACCEL_INELIGIBLE;
2887
Matt Gatese1f7de02014-02-18 13:55:17 -06002888 BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
2889
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002890 if (fixup_ioaccel_cdb(cdb, &cdb_len))
2891 return IO_ACCEL_INELIGIBLE;
2892
Matt Gatese1f7de02014-02-18 13:55:17 -06002893 c->cmd_type = CMD_IOACCEL1;
2894
2895 /* Adjust the DMA address to point to the accelerated command buffer */
2896 c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
2897 (c->cmdindex * sizeof(*cp));
2898 BUG_ON(c->busaddr & 0x0000007F);
2899
2900 use_sg = scsi_dma_map(cmd);
2901 if (use_sg < 0)
2902 return use_sg;
2903
2904 if (use_sg) {
2905 curr_sg = cp->SG;
2906 scsi_for_each_sg(cmd, sg, use_sg, i) {
2907 addr64 = (u64) sg_dma_address(sg);
2908 len = sg_dma_len(sg);
2909 total_len += len;
2910 curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
2911 curr_sg->Addr.upper =
2912 (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
2913 curr_sg->Len = len;
2914
2915 if (i == (scsi_sg_count(cmd) - 1))
2916 curr_sg->Ext = HPSA_SG_LAST;
2917 else
2918 curr_sg->Ext = 0; /* we are not chaining */
2919 curr_sg++;
2920 }
2921
2922 switch (cmd->sc_data_direction) {
2923 case DMA_TO_DEVICE:
2924 control |= IOACCEL1_CONTROL_DATA_OUT;
2925 break;
2926 case DMA_FROM_DEVICE:
2927 control |= IOACCEL1_CONTROL_DATA_IN;
2928 break;
2929 case DMA_NONE:
2930 control |= IOACCEL1_CONTROL_NODATAXFER;
2931 break;
2932 default:
2933 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
2934 cmd->sc_data_direction);
2935 BUG();
2936 break;
2937 }
2938 } else {
2939 control |= IOACCEL1_CONTROL_NODATAXFER;
2940 }
2941
Scott Teelc3497752014-02-18 13:56:34 -06002942 c->Header.SGList = use_sg;
Matt Gatese1f7de02014-02-18 13:55:17 -06002943 /* Fill out the command structure to submit */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002944 cp->dev_handle = ioaccel_handle & 0xFFFF;
Matt Gatese1f7de02014-02-18 13:55:17 -06002945 cp->transfer_len = total_len;
2946 cp->io_flags = IOACCEL1_IOFLAGS_IO_REQ |
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002947 (cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK);
Matt Gatese1f7de02014-02-18 13:55:17 -06002948 cp->control = control;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002949 memcpy(cp->CDB, cdb, cdb_len);
2950 memcpy(cp->CISS_LUN, scsi3addr, 8);
Scott Teelc3497752014-02-18 13:56:34 -06002951 /* Tag was already set at init time. */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002952 enqueue_cmd_and_start_io(h, c);
Matt Gatese1f7de02014-02-18 13:55:17 -06002953 return 0;
2954}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002955
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002956/*
2957 * Queue a command directly to a device behind the controller using the
2958 * I/O accelerator path.
2959 */
2960static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
2961 struct CommandList *c)
2962{
2963 struct scsi_cmnd *cmd = c->scsi_cmd;
2964 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
2965
2966 return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
2967 cmd->cmnd, cmd->cmd_len, dev->scsi3addr);
2968}
2969
Scott Teelc3497752014-02-18 13:56:34 -06002970static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
2971 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
2972 u8 *scsi3addr)
2973{
2974 struct scsi_cmnd *cmd = c->scsi_cmd;
2975 struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
2976 struct ioaccel2_sg_element *curr_sg;
2977 int use_sg, i;
2978 struct scatterlist *sg;
2979 u64 addr64;
2980 u32 len;
2981 u32 total_len = 0;
2982
2983 if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
2984 return IO_ACCEL_INELIGIBLE;
2985
2986 if (fixup_ioaccel_cdb(cdb, &cdb_len))
2987 return IO_ACCEL_INELIGIBLE;
2988 c->cmd_type = CMD_IOACCEL2;
2989 /* Adjust the DMA address to point to the accelerated command buffer */
2990 c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
2991 (c->cmdindex * sizeof(*cp));
2992 BUG_ON(c->busaddr & 0x0000007F);
2993
2994 memset(cp, 0, sizeof(*cp));
2995 cp->IU_type = IOACCEL2_IU_TYPE;
2996
2997 use_sg = scsi_dma_map(cmd);
2998 if (use_sg < 0)
2999 return use_sg;
3000
3001 if (use_sg) {
3002 BUG_ON(use_sg > IOACCEL2_MAXSGENTRIES);
3003 curr_sg = cp->sg;
3004 scsi_for_each_sg(cmd, sg, use_sg, i) {
3005 addr64 = (u64) sg_dma_address(sg);
3006 len = sg_dma_len(sg);
3007 total_len += len;
3008 curr_sg->address = cpu_to_le64(addr64);
3009 curr_sg->length = cpu_to_le32(len);
3010 curr_sg->reserved[0] = 0;
3011 curr_sg->reserved[1] = 0;
3012 curr_sg->reserved[2] = 0;
3013 curr_sg->chain_indicator = 0;
3014 curr_sg++;
3015 }
3016
3017 switch (cmd->sc_data_direction) {
3018 case DMA_TO_DEVICE:
3019 cp->direction = IOACCEL2_DIR_DATA_OUT;
3020 break;
3021 case DMA_FROM_DEVICE:
3022 cp->direction = IOACCEL2_DIR_DATA_IN;
3023 break;
3024 case DMA_NONE:
3025 cp->direction = IOACCEL2_DIR_NO_DATA;
3026 break;
3027 default:
3028 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3029 cmd->sc_data_direction);
3030 BUG();
3031 break;
3032 }
3033 } else {
3034 cp->direction = IOACCEL2_DIR_NO_DATA;
3035 }
3036 cp->scsi_nexus = ioaccel_handle;
3037 cp->Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT) |
3038 DIRECT_LOOKUP_BIT;
3039 memcpy(cp->cdb, cdb, sizeof(cp->cdb));
3040 memset(cp->cciss_lun, 0, sizeof(cp->cciss_lun));
3041 cp->cmd_priority_task_attr = 0;
3042
3043 /* fill in sg elements */
3044 cp->sg_count = (u8) use_sg;
3045
3046 cp->data_len = cpu_to_le32(total_len);
3047 cp->err_ptr = cpu_to_le64(c->busaddr +
3048 offsetof(struct io_accel2_cmd, error_data));
3049 cp->err_len = cpu_to_le32((u32) sizeof(cp->error_data));
3050
3051 enqueue_cmd_and_start_io(h, c);
3052 return 0;
3053}
3054
3055/*
3056 * Queue a command to the correct I/O accelerator path.
3057 */
3058static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
3059 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
3060 u8 *scsi3addr)
3061{
3062 if (h->transMethod & CFGTBL_Trans_io_accel1)
3063 return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
3064 cdb, cdb_len, scsi3addr);
3065 else
3066 return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
3067 cdb, cdb_len, scsi3addr);
3068}
3069
Scott Teel6b80b182014-02-18 13:56:55 -06003070static void raid_map_helper(struct raid_map_data *map,
3071 int offload_to_mirror, u32 *map_index, u32 *current_group)
3072{
3073 if (offload_to_mirror == 0) {
3074 /* use physical disk in the first mirrored group. */
3075 *map_index %= map->data_disks_per_row;
3076 return;
3077 }
3078 do {
3079 /* determine mirror group that *map_index indicates */
3080 *current_group = *map_index / map->data_disks_per_row;
3081 if (offload_to_mirror == *current_group)
3082 continue;
3083 if (*current_group < (map->layout_map_count - 1)) {
3084 /* select map index from next group */
3085 *map_index += map->data_disks_per_row;
3086 (*current_group)++;
3087 } else {
3088 /* select map index from first group */
3089 *map_index %= map->data_disks_per_row;
3090 *current_group = 0;
3091 }
3092 } while (offload_to_mirror != *current_group);
3093}
3094
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003095/*
3096 * Attempt to perform offload RAID mapping for a logical volume I/O.
3097 */
3098static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
3099 struct CommandList *c)
3100{
3101 struct scsi_cmnd *cmd = c->scsi_cmd;
3102 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3103 struct raid_map_data *map = &dev->raid_map;
3104 struct raid_map_disk_data *dd = &map->data[0];
3105 int is_write = 0;
3106 u32 map_index;
3107 u64 first_block, last_block;
3108 u32 block_cnt;
3109 u32 blocks_per_row;
3110 u64 first_row, last_row;
3111 u32 first_row_offset, last_row_offset;
3112 u32 first_column, last_column;
Scott Teel6b80b182014-02-18 13:56:55 -06003113 u64 r0_first_row, r0_last_row;
3114 u32 r5or6_blocks_per_row;
3115 u64 r5or6_first_row, r5or6_last_row;
3116 u32 r5or6_first_row_offset, r5or6_last_row_offset;
3117 u32 r5or6_first_column, r5or6_last_column;
3118 u32 total_disks_per_row;
3119 u32 stripesize;
3120 u32 first_group, last_group, current_group;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003121 u32 map_row;
3122 u32 disk_handle;
3123 u64 disk_block;
3124 u32 disk_block_cnt;
3125 u8 cdb[16];
3126 u8 cdb_len;
3127#if BITS_PER_LONG == 32
3128 u64 tmpdiv;
3129#endif
Scott Teel6b80b182014-02-18 13:56:55 -06003130 int offload_to_mirror;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003131
3132 BUG_ON(!(dev->offload_config && dev->offload_enabled));
3133
3134 /* check for valid opcode, get LBA and block count */
3135 switch (cmd->cmnd[0]) {
3136 case WRITE_6:
3137 is_write = 1;
3138 case READ_6:
3139 first_block =
3140 (((u64) cmd->cmnd[2]) << 8) |
3141 cmd->cmnd[3];
3142 block_cnt = cmd->cmnd[4];
3143 break;
3144 case WRITE_10:
3145 is_write = 1;
3146 case READ_10:
3147 first_block =
3148 (((u64) cmd->cmnd[2]) << 24) |
3149 (((u64) cmd->cmnd[3]) << 16) |
3150 (((u64) cmd->cmnd[4]) << 8) |
3151 cmd->cmnd[5];
3152 block_cnt =
3153 (((u32) cmd->cmnd[7]) << 8) |
3154 cmd->cmnd[8];
3155 break;
3156 case WRITE_12:
3157 is_write = 1;
3158 case READ_12:
3159 first_block =
3160 (((u64) cmd->cmnd[2]) << 24) |
3161 (((u64) cmd->cmnd[3]) << 16) |
3162 (((u64) cmd->cmnd[4]) << 8) |
3163 cmd->cmnd[5];
3164 block_cnt =
3165 (((u32) cmd->cmnd[6]) << 24) |
3166 (((u32) cmd->cmnd[7]) << 16) |
3167 (((u32) cmd->cmnd[8]) << 8) |
3168 cmd->cmnd[9];
3169 break;
3170 case WRITE_16:
3171 is_write = 1;
3172 case READ_16:
3173 first_block =
3174 (((u64) cmd->cmnd[2]) << 56) |
3175 (((u64) cmd->cmnd[3]) << 48) |
3176 (((u64) cmd->cmnd[4]) << 40) |
3177 (((u64) cmd->cmnd[5]) << 32) |
3178 (((u64) cmd->cmnd[6]) << 24) |
3179 (((u64) cmd->cmnd[7]) << 16) |
3180 (((u64) cmd->cmnd[8]) << 8) |
3181 cmd->cmnd[9];
3182 block_cnt =
3183 (((u32) cmd->cmnd[10]) << 24) |
3184 (((u32) cmd->cmnd[11]) << 16) |
3185 (((u32) cmd->cmnd[12]) << 8) |
3186 cmd->cmnd[13];
3187 break;
3188 default:
3189 return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
3190 }
3191 BUG_ON(block_cnt == 0);
3192 last_block = first_block + block_cnt - 1;
3193
3194 /* check for write to non-RAID-0 */
3195 if (is_write && dev->raid_level != 0)
3196 return IO_ACCEL_INELIGIBLE;
3197
3198 /* check for invalid block or wraparound */
3199 if (last_block >= map->volume_blk_cnt || last_block < first_block)
3200 return IO_ACCEL_INELIGIBLE;
3201
3202 /* calculate stripe information for the request */
3203 blocks_per_row = map->data_disks_per_row * map->strip_size;
3204#if BITS_PER_LONG == 32
3205 tmpdiv = first_block;
3206 (void) do_div(tmpdiv, blocks_per_row);
3207 first_row = tmpdiv;
3208 tmpdiv = last_block;
3209 (void) do_div(tmpdiv, blocks_per_row);
3210 last_row = tmpdiv;
3211 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
3212 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
3213 tmpdiv = first_row_offset;
3214 (void) do_div(tmpdiv, map->strip_size);
3215 first_column = tmpdiv;
3216 tmpdiv = last_row_offset;
3217 (void) do_div(tmpdiv, map->strip_size);
3218 last_column = tmpdiv;
3219#else
3220 first_row = first_block / blocks_per_row;
3221 last_row = last_block / blocks_per_row;
3222 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
3223 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
3224 first_column = first_row_offset / map->strip_size;
3225 last_column = last_row_offset / map->strip_size;
3226#endif
3227
3228 /* if this isn't a single row/column then give to the controller */
3229 if ((first_row != last_row) || (first_column != last_column))
3230 return IO_ACCEL_INELIGIBLE;
3231
3232 /* proceeding with driver mapping */
Scott Teel6b80b182014-02-18 13:56:55 -06003233 total_disks_per_row = map->data_disks_per_row +
3234 map->metadata_disks_per_row;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003235 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
3236 map->row_cnt;
Scott Teel6b80b182014-02-18 13:56:55 -06003237 map_index = (map_row * total_disks_per_row) + first_column;
3238
3239 switch (dev->raid_level) {
3240 case HPSA_RAID_0:
3241 break; /* nothing special to do */
3242 case HPSA_RAID_1:
3243 /* Handles load balance across RAID 1 members.
3244 * (2-drive R1 and R10 with even # of drives.)
3245 * Appropriate for SSDs, not optimal for HDDs
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003246 */
Scott Teel6b80b182014-02-18 13:56:55 -06003247 BUG_ON(map->layout_map_count != 2);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003248 if (dev->offload_to_mirror)
3249 map_index += map->data_disks_per_row;
3250 dev->offload_to_mirror = !dev->offload_to_mirror;
Scott Teel6b80b182014-02-18 13:56:55 -06003251 break;
3252 case HPSA_RAID_ADM:
3253 /* Handles N-way mirrors (R1-ADM)
3254 * and R10 with # of drives divisible by 3.)
3255 */
3256 BUG_ON(map->layout_map_count != 3);
3257
3258 offload_to_mirror = dev->offload_to_mirror;
3259 raid_map_helper(map, offload_to_mirror,
3260 &map_index, &current_group);
3261 /* set mirror group to use next time */
3262 offload_to_mirror =
3263 (offload_to_mirror >= map->layout_map_count - 1)
3264 ? 0 : offload_to_mirror + 1;
3265 /* FIXME: remove after debug/dev */
3266 BUG_ON(offload_to_mirror >= map->layout_map_count);
3267 dev_warn(&h->pdev->dev,
3268 "DEBUG: Using physical disk map index %d from mirror group %d\n",
3269 map_index, offload_to_mirror);
3270 dev->offload_to_mirror = offload_to_mirror;
3271 /* Avoid direct use of dev->offload_to_mirror within this
3272 * function since multiple threads might simultaneously
3273 * increment it beyond the range of dev->layout_map_count -1.
3274 */
3275 break;
3276 case HPSA_RAID_5:
3277 case HPSA_RAID_6:
3278 if (map->layout_map_count <= 1)
3279 break;
3280
3281 /* Verify first and last block are in same RAID group */
3282 r5or6_blocks_per_row =
3283 map->strip_size * map->data_disks_per_row;
3284 BUG_ON(r5or6_blocks_per_row == 0);
3285 stripesize = r5or6_blocks_per_row * map->layout_map_count;
3286#if BITS_PER_LONG == 32
3287 tmpdiv = first_block;
3288 first_group = do_div(tmpdiv, stripesize);
3289 tmpdiv = first_group;
3290 (void) do_div(tmpdiv, r5or6_blocks_per_row);
3291 first_group = tmpdiv;
3292 tmpdiv = last_block;
3293 last_group = do_div(tmpdiv, stripesize);
3294 tmpdiv = last_group;
3295 (void) do_div(tmpdiv, r5or6_blocks_per_row);
3296 last_group = tmpdiv;
3297#else
3298 first_group = (first_block % stripesize) / r5or6_blocks_per_row;
3299 last_group = (last_block % stripesize) / r5or6_blocks_per_row;
3300 if (first_group != last_group)
3301#endif
3302 return IO_ACCEL_INELIGIBLE;
3303
3304 /* Verify request is in a single row of RAID 5/6 */
3305#if BITS_PER_LONG == 32
3306 tmpdiv = first_block;
3307 (void) do_div(tmpdiv, stripesize);
3308 first_row = r5or6_first_row = r0_first_row = tmpdiv;
3309 tmpdiv = last_block;
3310 (void) do_div(tmpdiv, stripesize);
3311 r5or6_last_row = r0_last_row = tmpdiv;
3312#else
3313 first_row = r5or6_first_row = r0_first_row =
3314 first_block / stripesize;
3315 r5or6_last_row = r0_last_row = last_block / stripesize;
3316#endif
3317 if (r5or6_first_row != r5or6_last_row)
3318 return IO_ACCEL_INELIGIBLE;
3319
3320
3321 /* Verify request is in a single column */
3322#if BITS_PER_LONG == 32
3323 tmpdiv = first_block;
3324 first_row_offset = do_div(tmpdiv, stripesize);
3325 tmpdiv = first_row_offset;
3326 first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
3327 r5or6_first_row_offset = first_row_offset;
3328 tmpdiv = last_block;
3329 r5or6_last_row_offset = do_div(tmpdiv, stripesize);
3330 tmpdiv = r5or6_last_row_offset;
3331 r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
3332 tmpdiv = r5or6_first_row_offset;
3333 (void) do_div(tmpdiv, map->strip_size);
3334 first_column = r5or6_first_column = tmpdiv;
3335 tmpdiv = r5or6_last_row_offset;
3336 (void) do_div(tmpdiv, map->strip_size);
3337 r5or6_last_column = tmpdiv;
3338#else
3339 first_row_offset = r5or6_first_row_offset =
3340 (u32)((first_block % stripesize) %
3341 r5or6_blocks_per_row);
3342
3343 r5or6_last_row_offset =
3344 (u32)((last_block % stripesize) %
3345 r5or6_blocks_per_row);
3346
3347 first_column = r5or6_first_column =
3348 r5or6_first_row_offset / map->strip_size;
3349 r5or6_last_column =
3350 r5or6_last_row_offset / map->strip_size;
3351#endif
3352 if (r5or6_first_column != r5or6_last_column)
3353 return IO_ACCEL_INELIGIBLE;
3354
3355 /* Request is eligible */
3356 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
3357 map->row_cnt;
3358
3359 map_index = (first_group *
3360 (map->row_cnt * total_disks_per_row)) +
3361 (map_row * total_disks_per_row) + first_column;
3362 break;
3363 default:
3364 return IO_ACCEL_INELIGIBLE;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003365 }
Scott Teel6b80b182014-02-18 13:56:55 -06003366
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003367 disk_handle = dd[map_index].ioaccel_handle;
3368 disk_block = map->disk_starting_blk + (first_row * map->strip_size) +
3369 (first_row_offset - (first_column * map->strip_size));
3370 disk_block_cnt = block_cnt;
3371
3372 /* handle differing logical/physical block sizes */
3373 if (map->phys_blk_shift) {
3374 disk_block <<= map->phys_blk_shift;
3375 disk_block_cnt <<= map->phys_blk_shift;
3376 }
3377 BUG_ON(disk_block_cnt > 0xffff);
3378
3379 /* build the new CDB for the physical disk I/O */
3380 if (disk_block > 0xffffffff) {
3381 cdb[0] = is_write ? WRITE_16 : READ_16;
3382 cdb[1] = 0;
3383 cdb[2] = (u8) (disk_block >> 56);
3384 cdb[3] = (u8) (disk_block >> 48);
3385 cdb[4] = (u8) (disk_block >> 40);
3386 cdb[5] = (u8) (disk_block >> 32);
3387 cdb[6] = (u8) (disk_block >> 24);
3388 cdb[7] = (u8) (disk_block >> 16);
3389 cdb[8] = (u8) (disk_block >> 8);
3390 cdb[9] = (u8) (disk_block);
3391 cdb[10] = (u8) (disk_block_cnt >> 24);
3392 cdb[11] = (u8) (disk_block_cnt >> 16);
3393 cdb[12] = (u8) (disk_block_cnt >> 8);
3394 cdb[13] = (u8) (disk_block_cnt);
3395 cdb[14] = 0;
3396 cdb[15] = 0;
3397 cdb_len = 16;
3398 } else {
3399 cdb[0] = is_write ? WRITE_10 : READ_10;
3400 cdb[1] = 0;
3401 cdb[2] = (u8) (disk_block >> 24);
3402 cdb[3] = (u8) (disk_block >> 16);
3403 cdb[4] = (u8) (disk_block >> 8);
3404 cdb[5] = (u8) (disk_block);
3405 cdb[6] = 0;
3406 cdb[7] = (u8) (disk_block_cnt >> 8);
3407 cdb[8] = (u8) (disk_block_cnt);
3408 cdb[9] = 0;
3409 cdb_len = 10;
3410 }
3411 return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
3412 dev->scsi3addr);
3413}
3414
Jeff Garzikf2812332010-11-16 02:10:29 -05003415static int hpsa_scsi_queue_command_lck(struct scsi_cmnd *cmd,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003416 void (*done)(struct scsi_cmnd *))
3417{
3418 struct ctlr_info *h;
3419 struct hpsa_scsi_dev_t *dev;
3420 unsigned char scsi3addr[8];
3421 struct CommandList *c;
3422 unsigned long flags;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003423 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003424
3425 /* Get the ptr to our adapter structure out of cmd->host. */
3426 h = sdev_to_hba(cmd->device);
3427 dev = cmd->device->hostdata;
3428 if (!dev) {
3429 cmd->result = DID_NO_CONNECT << 16;
3430 done(cmd);
3431 return 0;
3432 }
3433 memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
3434
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003435 spin_lock_irqsave(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05003436 if (unlikely(h->lockup_detected)) {
3437 spin_unlock_irqrestore(&h->lock, flags);
3438 cmd->result = DID_ERROR << 16;
3439 done(cmd);
3440 return 0;
3441 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003442 spin_unlock_irqrestore(&h->lock, flags);
Matt Gatese16a33a2012-05-01 11:43:11 -05003443 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003444 if (c == NULL) { /* trouble... */
3445 dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
3446 return SCSI_MLQUEUE_HOST_BUSY;
3447 }
3448
3449 /* Fill in the command list header */
3450
3451 cmd->scsi_done = done; /* save this for use by completion code */
3452
3453 /* save c in case we have to abort it */
3454 cmd->host_scribble = (unsigned char *) c;
3455
3456 c->cmd_type = CMD_SCSI;
3457 c->scsi_cmd = cmd;
Matt Gatese1f7de02014-02-18 13:55:17 -06003458
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003459 /* Call alternate submit routine for I/O accelerated commands.
3460 * Retries always go down the normal I/O path.
3461 */
3462 if (likely(cmd->retries == 0 &&
Scott Teelda0697b2014-02-18 13:57:00 -06003463 cmd->request->cmd_type == REQ_TYPE_FS &&
3464 h->acciopath_status)) {
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003465 if (dev->offload_enabled) {
3466 rc = hpsa_scsi_ioaccel_raid_map(h, c);
3467 if (rc == 0)
3468 return 0; /* Sent on ioaccel path */
3469 if (rc < 0) { /* scsi_dma_map failed. */
3470 cmd_free(h, c);
3471 return SCSI_MLQUEUE_HOST_BUSY;
3472 }
3473 } else if (dev->ioaccel_handle) {
3474 rc = hpsa_scsi_ioaccel_direct_map(h, c);
3475 if (rc == 0)
3476 return 0; /* Sent on direct map path */
3477 if (rc < 0) { /* scsi_dma_map failed. */
3478 cmd_free(h, c);
3479 return SCSI_MLQUEUE_HOST_BUSY;
3480 }
3481 }
3482 }
Matt Gatese1f7de02014-02-18 13:55:17 -06003483
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003484 c->Header.ReplyQueue = 0; /* unused in simple mode */
3485 memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
Don Brace303932f2010-02-04 08:42:40 -06003486 c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT);
3487 c->Header.Tag.lower |= DIRECT_LOOKUP_BIT;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003488
3489 /* Fill in the request block... */
3490
3491 c->Request.Timeout = 0;
3492 memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
3493 BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
3494 c->Request.CDBLen = cmd->cmd_len;
3495 memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
3496 c->Request.Type.Type = TYPE_CMD;
3497 c->Request.Type.Attribute = ATTR_SIMPLE;
3498 switch (cmd->sc_data_direction) {
3499 case DMA_TO_DEVICE:
3500 c->Request.Type.Direction = XFER_WRITE;
3501 break;
3502 case DMA_FROM_DEVICE:
3503 c->Request.Type.Direction = XFER_READ;
3504 break;
3505 case DMA_NONE:
3506 c->Request.Type.Direction = XFER_NONE;
3507 break;
3508 case DMA_BIDIRECTIONAL:
3509 /* This can happen if a buggy application does a scsi passthru
3510 * and sets both inlen and outlen to non-zero. ( see
3511 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
3512 */
3513
3514 c->Request.Type.Direction = XFER_RSVD;
3515 /* This is technically wrong, and hpsa controllers should
3516 * reject it with CMD_INVALID, which is the most correct
3517 * response, but non-fibre backends appear to let it
3518 * slide by, and give the same results as if this field
3519 * were set correctly. Either way is acceptable for
3520 * our purposes here.
3521 */
3522
3523 break;
3524
3525 default:
3526 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3527 cmd->sc_data_direction);
3528 BUG();
3529 break;
3530 }
3531
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06003532 if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003533 cmd_free(h, c);
3534 return SCSI_MLQUEUE_HOST_BUSY;
3535 }
3536 enqueue_cmd_and_start_io(h, c);
3537 /* the cmd'll come back via intr handler in complete_scsi_command() */
3538 return 0;
3539}
3540
Jeff Garzikf2812332010-11-16 02:10:29 -05003541static DEF_SCSI_QCMD(hpsa_scsi_queue_command)
3542
Stephen M. Cameron5f389362014-02-18 13:55:48 -06003543static int do_not_scan_if_controller_locked_up(struct ctlr_info *h)
3544{
3545 unsigned long flags;
3546
3547 /*
3548 * Don't let rescans be initiated on a controller known
3549 * to be locked up. If the controller locks up *during*
3550 * a rescan, that thread is probably hosed, but at least
3551 * we can prevent new rescan threads from piling up on a
3552 * locked up controller.
3553 */
3554 spin_lock_irqsave(&h->lock, flags);
3555 if (unlikely(h->lockup_detected)) {
3556 spin_unlock_irqrestore(&h->lock, flags);
3557 spin_lock_irqsave(&h->scan_lock, flags);
3558 h->scan_finished = 1;
3559 wake_up_all(&h->scan_wait_queue);
3560 spin_unlock_irqrestore(&h->scan_lock, flags);
3561 return 1;
3562 }
3563 spin_unlock_irqrestore(&h->lock, flags);
3564 return 0;
3565}
3566
Stephen M. Camerona08a8472010-02-04 08:43:16 -06003567static void hpsa_scan_start(struct Scsi_Host *sh)
3568{
3569 struct ctlr_info *h = shost_to_hba(sh);
3570 unsigned long flags;
3571
Stephen M. Cameron5f389362014-02-18 13:55:48 -06003572 if (do_not_scan_if_controller_locked_up(h))
3573 return;
3574
Stephen M. Camerona08a8472010-02-04 08:43:16 -06003575 /* wait until any scan already in progress is finished. */
3576 while (1) {
3577 spin_lock_irqsave(&h->scan_lock, flags);
3578 if (h->scan_finished)
3579 break;
3580 spin_unlock_irqrestore(&h->scan_lock, flags);
3581 wait_event(h->scan_wait_queue, h->scan_finished);
3582 /* Note: We don't need to worry about a race between this
3583 * thread and driver unload because the midlayer will
3584 * have incremented the reference count, so unload won't
3585 * happen if we're in here.
3586 */
3587 }
3588 h->scan_finished = 0; /* mark scan as in progress */
3589 spin_unlock_irqrestore(&h->scan_lock, flags);
3590
Stephen M. Cameron5f389362014-02-18 13:55:48 -06003591 if (do_not_scan_if_controller_locked_up(h))
3592 return;
3593
Stephen M. Camerona08a8472010-02-04 08:43:16 -06003594 hpsa_update_scsi_devices(h, h->scsi_host->host_no);
3595
3596 spin_lock_irqsave(&h->scan_lock, flags);
3597 h->scan_finished = 1; /* mark scan as finished. */
3598 wake_up_all(&h->scan_wait_queue);
3599 spin_unlock_irqrestore(&h->scan_lock, flags);
3600}
3601
3602static int hpsa_scan_finished(struct Scsi_Host *sh,
3603 unsigned long elapsed_time)
3604{
3605 struct ctlr_info *h = shost_to_hba(sh);
3606 unsigned long flags;
3607 int finished;
3608
3609 spin_lock_irqsave(&h->scan_lock, flags);
3610 finished = h->scan_finished;
3611 spin_unlock_irqrestore(&h->scan_lock, flags);
3612 return finished;
3613}
3614
Stephen M. Cameron667e23d2010-02-25 14:02:51 -06003615static int hpsa_change_queue_depth(struct scsi_device *sdev,
3616 int qdepth, int reason)
3617{
3618 struct ctlr_info *h = sdev_to_hba(sdev);
3619
3620 if (reason != SCSI_QDEPTH_DEFAULT)
3621 return -ENOTSUPP;
3622
3623 if (qdepth < 1)
3624 qdepth = 1;
3625 else
3626 if (qdepth > h->nr_cmds)
3627 qdepth = h->nr_cmds;
3628 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
3629 return sdev->queue_depth;
3630}
3631
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003632static void hpsa_unregister_scsi(struct ctlr_info *h)
3633{
3634 /* we are being forcibly unloaded, and may not refuse. */
3635 scsi_remove_host(h->scsi_host);
3636 scsi_host_put(h->scsi_host);
3637 h->scsi_host = NULL;
3638}
3639
3640static int hpsa_register_scsi(struct ctlr_info *h)
3641{
Stephen M. Cameronb7056902012-01-19 14:00:53 -06003642 struct Scsi_Host *sh;
3643 int error;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003644
Stephen M. Cameronb7056902012-01-19 14:00:53 -06003645 sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
3646 if (sh == NULL)
3647 goto fail;
3648
3649 sh->io_port = 0;
3650 sh->n_io_port = 0;
3651 sh->this_id = -1;
3652 sh->max_channel = 3;
3653 sh->max_cmd_len = MAX_COMMAND_SIZE;
3654 sh->max_lun = HPSA_MAX_LUN;
3655 sh->max_id = HPSA_MAX_LUN;
3656 sh->can_queue = h->nr_cmds;
3657 sh->cmd_per_lun = h->nr_cmds;
3658 sh->sg_tablesize = h->maxsgentries;
3659 h->scsi_host = sh;
3660 sh->hostdata[0] = (unsigned long) h;
3661 sh->irq = h->intr[h->intr_mode];
3662 sh->unique_id = sh->irq;
3663 error = scsi_add_host(sh, &h->pdev->dev);
3664 if (error)
3665 goto fail_host_put;
3666 scsi_scan_host(sh);
3667 return 0;
3668
3669 fail_host_put:
3670 dev_err(&h->pdev->dev, "%s: scsi_add_host"
3671 " failed for controller %d\n", __func__, h->ctlr);
3672 scsi_host_put(sh);
3673 return error;
3674 fail:
3675 dev_err(&h->pdev->dev, "%s: scsi_host_alloc"
3676 " failed for controller %d\n", __func__, h->ctlr);
3677 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003678}
3679
3680static int wait_for_device_to_become_ready(struct ctlr_info *h,
3681 unsigned char lunaddr[])
3682{
3683 int rc = 0;
3684 int count = 0;
3685 int waittime = 1; /* seconds */
3686 struct CommandList *c;
3687
3688 c = cmd_special_alloc(h);
3689 if (!c) {
3690 dev_warn(&h->pdev->dev, "out of memory in "
3691 "wait_for_device_to_become_ready.\n");
3692 return IO_ERROR;
3693 }
3694
3695 /* Send test unit ready until device ready, or give up. */
3696 while (count < HPSA_TUR_RETRY_LIMIT) {
3697
3698 /* Wait for a bit. do this first, because if we send
3699 * the TUR right away, the reset will just abort it.
3700 */
3701 msleep(1000 * waittime);
3702 count++;
3703
3704 /* Increase wait time with each try, up to a point. */
3705 if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
3706 waittime = waittime * 2;
3707
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003708 /* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
3709 (void) fill_cmd(c, TEST_UNIT_READY, h,
3710 NULL, 0, 0, lunaddr, TYPE_CMD);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003711 hpsa_scsi_do_simple_cmd_core(h, c);
3712 /* no unmap needed here because no data xfer. */
3713
3714 if (c->err_info->CommandStatus == CMD_SUCCESS)
3715 break;
3716
3717 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
3718 c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
3719 (c->err_info->SenseInfo[2] == NO_SENSE ||
3720 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
3721 break;
3722
3723 dev_warn(&h->pdev->dev, "waiting %d secs "
3724 "for device to become ready.\n", waittime);
3725 rc = 1; /* device not ready. */
3726 }
3727
3728 if (rc)
3729 dev_warn(&h->pdev->dev, "giving up on device.\n");
3730 else
3731 dev_warn(&h->pdev->dev, "device is ready.\n");
3732
3733 cmd_special_free(h, c);
3734 return rc;
3735}
3736
3737/* Need at least one of these error handlers to keep ../scsi/hosts.c from
3738 * complaining. Doing a host- or bus-reset can't do anything good here.
3739 */
3740static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
3741{
3742 int rc;
3743 struct ctlr_info *h;
3744 struct hpsa_scsi_dev_t *dev;
3745
3746 /* find the controller to which the command to be aborted was sent */
3747 h = sdev_to_hba(scsicmd->device);
3748 if (h == NULL) /* paranoia */
3749 return FAILED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003750 dev = scsicmd->device->hostdata;
3751 if (!dev) {
3752 dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
3753 "device lookup failed.\n");
3754 return FAILED;
3755 }
Stephen M. Camerond416b0c2010-02-04 08:43:21 -06003756 dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
3757 h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003758 /* send a reset to the SCSI LUN which the command was sent to */
Scott Teelbf711ac2014-02-18 13:56:39 -06003759 rc = hpsa_send_reset(h, dev->scsi3addr, HPSA_RESET_TYPE_LUN);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003760 if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
3761 return SUCCESS;
3762
3763 dev_warn(&h->pdev->dev, "resetting device failed.\n");
3764 return FAILED;
3765}
3766
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003767static void swizzle_abort_tag(u8 *tag)
3768{
3769 u8 original_tag[8];
3770
3771 memcpy(original_tag, tag, 8);
3772 tag[0] = original_tag[3];
3773 tag[1] = original_tag[2];
3774 tag[2] = original_tag[1];
3775 tag[3] = original_tag[0];
3776 tag[4] = original_tag[7];
3777 tag[5] = original_tag[6];
3778 tag[6] = original_tag[5];
3779 tag[7] = original_tag[4];
3780}
3781
Scott Teel17eb87d2014-02-18 13:55:28 -06003782static void hpsa_get_tag(struct ctlr_info *h,
3783 struct CommandList *c, u32 *taglower, u32 *tagupper)
3784{
3785 if (c->cmd_type == CMD_IOACCEL1) {
3786 struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
3787 &h->ioaccel_cmd_pool[c->cmdindex];
3788 *tagupper = cm1->Tag.upper;
3789 *taglower = cm1->Tag.lower;
Scott Teel54b6e9e2014-02-18 13:56:45 -06003790 return;
Scott Teel17eb87d2014-02-18 13:55:28 -06003791 }
Scott Teel54b6e9e2014-02-18 13:56:45 -06003792 if (c->cmd_type == CMD_IOACCEL2) {
3793 struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
3794 &h->ioaccel2_cmd_pool[c->cmdindex];
3795 *tagupper = cm2->Tag.upper;
3796 *taglower = cm2->Tag.lower;
3797 return;
3798 }
3799 *tagupper = c->Header.Tag.upper;
3800 *taglower = c->Header.Tag.lower;
Scott Teel17eb87d2014-02-18 13:55:28 -06003801}
3802
Scott Teel54b6e9e2014-02-18 13:56:45 -06003803
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003804static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003805 struct CommandList *abort, int swizzle)
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003806{
3807 int rc = IO_OK;
3808 struct CommandList *c;
3809 struct ErrorInfo *ei;
Scott Teel17eb87d2014-02-18 13:55:28 -06003810 u32 tagupper, taglower;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003811
3812 c = cmd_special_alloc(h);
3813 if (c == NULL) { /* trouble... */
3814 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
3815 return -ENOMEM;
3816 }
3817
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003818 /* fill_cmd can't fail here, no buffer to map */
3819 (void) fill_cmd(c, HPSA_ABORT_MSG, h, abort,
3820 0, 0, scsi3addr, TYPE_MSG);
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003821 if (swizzle)
3822 swizzle_abort_tag(&c->Request.CDB[4]);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003823 hpsa_scsi_do_simple_cmd_core(h, c);
Scott Teel17eb87d2014-02-18 13:55:28 -06003824 hpsa_get_tag(h, abort, &taglower, &tagupper);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003825 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd_core completed.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06003826 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003827 /* no unmap needed here because no data xfer. */
3828
3829 ei = c->err_info;
3830 switch (ei->CommandStatus) {
3831 case CMD_SUCCESS:
3832 break;
3833 case CMD_UNABORTABLE: /* Very common, don't make noise. */
3834 rc = -1;
3835 break;
3836 default:
3837 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06003838 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003839 hpsa_scsi_interpret_error(c);
3840 rc = -1;
3841 break;
3842 }
3843 cmd_special_free(h, c);
3844 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n", __func__,
3845 abort->Header.Tag.upper, abort->Header.Tag.lower);
3846 return rc;
3847}
3848
3849/*
3850 * hpsa_find_cmd_in_queue
3851 *
3852 * Used to determine whether a command (find) is still present
3853 * in queue_head. Optionally excludes the last element of queue_head.
3854 *
3855 * This is used to avoid unnecessary aborts. Commands in h->reqQ have
3856 * not yet been submitted, and so can be aborted by the driver without
3857 * sending an abort to the hardware.
3858 *
3859 * Returns pointer to command if found in queue, NULL otherwise.
3860 */
3861static struct CommandList *hpsa_find_cmd_in_queue(struct ctlr_info *h,
3862 struct scsi_cmnd *find, struct list_head *queue_head)
3863{
3864 unsigned long flags;
3865 struct CommandList *c = NULL; /* ptr into cmpQ */
3866
3867 if (!find)
3868 return 0;
3869 spin_lock_irqsave(&h->lock, flags);
3870 list_for_each_entry(c, queue_head, list) {
3871 if (c->scsi_cmd == NULL) /* e.g.: passthru ioctl */
3872 continue;
3873 if (c->scsi_cmd == find) {
3874 spin_unlock_irqrestore(&h->lock, flags);
3875 return c;
3876 }
3877 }
3878 spin_unlock_irqrestore(&h->lock, flags);
3879 return NULL;
3880}
3881
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003882static struct CommandList *hpsa_find_cmd_in_queue_by_tag(struct ctlr_info *h,
3883 u8 *tag, struct list_head *queue_head)
3884{
3885 unsigned long flags;
3886 struct CommandList *c;
3887
3888 spin_lock_irqsave(&h->lock, flags);
3889 list_for_each_entry(c, queue_head, list) {
3890 if (memcmp(&c->Header.Tag, tag, 8) != 0)
3891 continue;
3892 spin_unlock_irqrestore(&h->lock, flags);
3893 return c;
3894 }
3895 spin_unlock_irqrestore(&h->lock, flags);
3896 return NULL;
3897}
3898
Scott Teel54b6e9e2014-02-18 13:56:45 -06003899/* ioaccel2 path firmware cannot handle abort task requests.
3900 * Change abort requests to physical target reset, and send to the
3901 * address of the physical disk used for the ioaccel 2 command.
3902 * Return 0 on success (IO_OK)
3903 * -1 on failure
3904 */
3905
3906static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
3907 unsigned char *scsi3addr, struct CommandList *abort)
3908{
3909 int rc = IO_OK;
3910 struct scsi_cmnd *scmd; /* scsi command within request being aborted */
3911 struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
3912 unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
3913 unsigned char *psa = &phys_scsi3addr[0];
3914
3915 /* Get a pointer to the hpsa logical device. */
3916 scmd = (struct scsi_cmnd *) abort->scsi_cmd;
3917 dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
3918 if (dev == NULL) {
3919 dev_warn(&h->pdev->dev,
3920 "Cannot abort: no device pointer for command.\n");
3921 return -1; /* not abortable */
3922 }
3923
3924 if (!dev->offload_enabled) {
3925 dev_warn(&h->pdev->dev,
3926 "Can't abort: device is not operating in HP SSD Smart Path mode.\n");
3927 return -1; /* not abortable */
3928 }
3929
3930 /* Incoming scsi3addr is logical addr. We need physical disk addr. */
3931 if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
3932 dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
3933 return -1; /* not abortable */
3934 }
3935
3936 /* send the reset */
3937 rc = hpsa_send_reset(h, psa, HPSA_RESET_TYPE_TARGET);
3938 if (rc != 0) {
3939 dev_warn(&h->pdev->dev,
3940 "Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
3941 psa[0], psa[1], psa[2], psa[3],
3942 psa[4], psa[5], psa[6], psa[7]);
3943 return rc; /* failed to reset */
3944 }
3945
3946 /* wait for device to recover */
3947 if (wait_for_device_to_become_ready(h, psa) != 0) {
3948 dev_warn(&h->pdev->dev,
3949 "Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
3950 psa[0], psa[1], psa[2], psa[3],
3951 psa[4], psa[5], psa[6], psa[7]);
3952 return -1; /* failed to recover */
3953 }
3954
3955 /* device recovered */
3956 dev_info(&h->pdev->dev,
3957 "Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
3958 psa[0], psa[1], psa[2], psa[3],
3959 psa[4], psa[5], psa[6], psa[7]);
3960
3961 return rc; /* success */
3962}
3963
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003964/* Some Smart Arrays need the abort tag swizzled, and some don't. It's hard to
3965 * tell which kind we're dealing with, so we send the abort both ways. There
3966 * shouldn't be any collisions between swizzled and unswizzled tags due to the
3967 * way we construct our tags but we check anyway in case the assumptions which
3968 * make this true someday become false.
3969 */
3970static int hpsa_send_abort_both_ways(struct ctlr_info *h,
3971 unsigned char *scsi3addr, struct CommandList *abort)
3972{
3973 u8 swizzled_tag[8];
3974 struct CommandList *c;
3975 int rc = 0, rc2 = 0;
3976
Scott Teel54b6e9e2014-02-18 13:56:45 -06003977 /* ioccelerator mode 2 commands should be aborted via the
3978 * accelerated path, since RAID path is unaware of these commands,
3979 * but underlying firmware can't handle abort TMF.
3980 * Change abort to physical device reset.
3981 */
3982 if (abort->cmd_type == CMD_IOACCEL2)
3983 return hpsa_send_reset_as_abort_ioaccel2(h, scsi3addr, abort);
3984
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003985 /* we do not expect to find the swizzled tag in our queue, but
3986 * check anyway just to be sure the assumptions which make this
3987 * the case haven't become wrong.
3988 */
3989 memcpy(swizzled_tag, &abort->Request.CDB[4], 8);
3990 swizzle_abort_tag(swizzled_tag);
3991 c = hpsa_find_cmd_in_queue_by_tag(h, swizzled_tag, &h->cmpQ);
3992 if (c != NULL) {
3993 dev_warn(&h->pdev->dev, "Unexpectedly found byte-swapped tag in completion queue.\n");
3994 return hpsa_send_abort(h, scsi3addr, abort, 0);
3995 }
3996 rc = hpsa_send_abort(h, scsi3addr, abort, 0);
3997
3998 /* if the command is still in our queue, we can't conclude that it was
3999 * aborted (it might have just completed normally) but in any case
4000 * we don't need to try to abort it another way.
4001 */
4002 c = hpsa_find_cmd_in_queue(h, abort->scsi_cmd, &h->cmpQ);
4003 if (c)
4004 rc2 = hpsa_send_abort(h, scsi3addr, abort, 1);
4005 return rc && rc2;
4006}
4007
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004008/* Send an abort for the specified command.
4009 * If the device and controller support it,
4010 * send a task abort request.
4011 */
4012static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
4013{
4014
4015 int i, rc;
4016 struct ctlr_info *h;
4017 struct hpsa_scsi_dev_t *dev;
4018 struct CommandList *abort; /* pointer to command to be aborted */
4019 struct CommandList *found;
4020 struct scsi_cmnd *as; /* ptr to scsi cmd inside aborted command. */
4021 char msg[256]; /* For debug messaging. */
4022 int ml = 0;
Scott Teel17eb87d2014-02-18 13:55:28 -06004023 u32 tagupper, taglower;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004024
4025 /* Find the controller of the command to be aborted */
4026 h = sdev_to_hba(sc->device);
4027 if (WARN(h == NULL,
4028 "ABORT REQUEST FAILED, Controller lookup failed.\n"))
4029 return FAILED;
4030
4031 /* Check that controller supports some kind of task abort */
4032 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
4033 !(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
4034 return FAILED;
4035
4036 memset(msg, 0, sizeof(msg));
4037 ml += sprintf(msg+ml, "ABORT REQUEST on C%d:B%d:T%d:L%d ",
4038 h->scsi_host->host_no, sc->device->channel,
4039 sc->device->id, sc->device->lun);
4040
4041 /* Find the device of the command to be aborted */
4042 dev = sc->device->hostdata;
4043 if (!dev) {
4044 dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
4045 msg);
4046 return FAILED;
4047 }
4048
4049 /* Get SCSI command to be aborted */
4050 abort = (struct CommandList *) sc->host_scribble;
4051 if (abort == NULL) {
4052 dev_err(&h->pdev->dev, "%s FAILED, Command to abort is NULL.\n",
4053 msg);
4054 return FAILED;
4055 }
Scott Teel17eb87d2014-02-18 13:55:28 -06004056 hpsa_get_tag(h, abort, &taglower, &tagupper);
4057 ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004058 as = (struct scsi_cmnd *) abort->scsi_cmd;
4059 if (as != NULL)
4060 ml += sprintf(msg+ml, "Command:0x%x SN:0x%lx ",
4061 as->cmnd[0], as->serial_number);
4062 dev_dbg(&h->pdev->dev, "%s\n", msg);
4063 dev_warn(&h->pdev->dev, "Abort request on C%d:B%d:T%d:L%d\n",
4064 h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
4065
4066 /* Search reqQ to See if command is queued but not submitted,
4067 * if so, complete the command with aborted status and remove
4068 * it from the reqQ.
4069 */
4070 found = hpsa_find_cmd_in_queue(h, sc, &h->reqQ);
4071 if (found) {
4072 found->err_info->CommandStatus = CMD_ABORTED;
4073 finish_cmd(found);
4074 dev_info(&h->pdev->dev, "%s Request SUCCEEDED (driver queue).\n",
4075 msg);
4076 return SUCCESS;
4077 }
4078
4079 /* not in reqQ, if also not in cmpQ, must have already completed */
4080 found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
4081 if (!found) {
Stephen M. Camerond6ebd0f2012-07-26 11:34:17 -05004082 dev_dbg(&h->pdev->dev, "%s Request SUCCEEDED (not known to driver).\n",
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004083 msg);
4084 return SUCCESS;
4085 }
4086
4087 /*
4088 * Command is in flight, or possibly already completed
4089 * by the firmware (but not to the scsi mid layer) but we can't
4090 * distinguish which. Send the abort down.
4091 */
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05004092 rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004093 if (rc != 0) {
4094 dev_dbg(&h->pdev->dev, "%s Request FAILED.\n", msg);
4095 dev_warn(&h->pdev->dev, "FAILED abort on device C%d:B%d:T%d:L%d\n",
4096 h->scsi_host->host_no,
4097 dev->bus, dev->target, dev->lun);
4098 return FAILED;
4099 }
4100 dev_info(&h->pdev->dev, "%s REQUEST SUCCEEDED.\n", msg);
4101
4102 /* If the abort(s) above completed and actually aborted the
4103 * command, then the command to be aborted should already be
4104 * completed. If not, wait around a bit more to see if they
4105 * manage to complete normally.
4106 */
4107#define ABORT_COMPLETE_WAIT_SECS 30
4108 for (i = 0; i < ABORT_COMPLETE_WAIT_SECS * 10; i++) {
4109 found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
4110 if (!found)
4111 return SUCCESS;
4112 msleep(100);
4113 }
4114 dev_warn(&h->pdev->dev, "%s FAILED. Aborted command has not completed after %d seconds.\n",
4115 msg, ABORT_COMPLETE_WAIT_SECS);
4116 return FAILED;
4117}
4118
4119
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004120/*
4121 * For operations that cannot sleep, a command block is allocated at init,
4122 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
4123 * which ones are free or in use. Lock must be held when calling this.
4124 * cmd_free() is the complement.
4125 */
4126static struct CommandList *cmd_alloc(struct ctlr_info *h)
4127{
4128 struct CommandList *c;
4129 int i;
4130 union u64bit temp64;
4131 dma_addr_t cmd_dma_handle, err_dma_handle;
Matt Gatese16a33a2012-05-01 11:43:11 -05004132 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004133
Matt Gatese16a33a2012-05-01 11:43:11 -05004134 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004135 do {
4136 i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
Matt Gatese16a33a2012-05-01 11:43:11 -05004137 if (i == h->nr_cmds) {
4138 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004139 return NULL;
Matt Gatese16a33a2012-05-01 11:43:11 -05004140 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004141 } while (test_and_set_bit
4142 (i & (BITS_PER_LONG - 1),
4143 h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
Matt Gatese16a33a2012-05-01 11:43:11 -05004144 spin_unlock_irqrestore(&h->lock, flags);
4145
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004146 c = h->cmd_pool + i;
4147 memset(c, 0, sizeof(*c));
4148 cmd_dma_handle = h->cmd_pool_dhandle
4149 + i * sizeof(*c);
4150 c->err_info = h->errinfo_pool + i;
4151 memset(c->err_info, 0, sizeof(*c->err_info));
4152 err_dma_handle = h->errinfo_pool_dhandle
4153 + i * sizeof(*c->err_info);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004154
4155 c->cmdindex = i;
4156
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06004157 INIT_LIST_HEAD(&c->list);
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004158 c->busaddr = (u32) cmd_dma_handle;
4159 temp64.val = (u64) err_dma_handle;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004160 c->ErrDesc.Addr.lower = temp64.val32.lower;
4161 c->ErrDesc.Addr.upper = temp64.val32.upper;
4162 c->ErrDesc.Len = sizeof(*c->err_info);
4163
4164 c->h = h;
4165 return c;
4166}
4167
4168/* For operations that can wait for kmalloc to possibly sleep,
4169 * this routine can be called. Lock need not be held to call
4170 * cmd_special_alloc. cmd_special_free() is the complement.
4171 */
4172static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
4173{
4174 struct CommandList *c;
4175 union u64bit temp64;
4176 dma_addr_t cmd_dma_handle, err_dma_handle;
4177
4178 c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
4179 if (c == NULL)
4180 return NULL;
4181 memset(c, 0, sizeof(*c));
4182
Matt Gatese1f7de02014-02-18 13:55:17 -06004183 c->cmd_type = CMD_SCSI;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004184 c->cmdindex = -1;
4185
4186 c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info),
4187 &err_dma_handle);
4188
4189 if (c->err_info == NULL) {
4190 pci_free_consistent(h->pdev,
4191 sizeof(*c), c, cmd_dma_handle);
4192 return NULL;
4193 }
4194 memset(c->err_info, 0, sizeof(*c->err_info));
4195
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06004196 INIT_LIST_HEAD(&c->list);
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004197 c->busaddr = (u32) cmd_dma_handle;
4198 temp64.val = (u64) err_dma_handle;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004199 c->ErrDesc.Addr.lower = temp64.val32.lower;
4200 c->ErrDesc.Addr.upper = temp64.val32.upper;
4201 c->ErrDesc.Len = sizeof(*c->err_info);
4202
4203 c->h = h;
4204 return c;
4205}
4206
4207static void cmd_free(struct ctlr_info *h, struct CommandList *c)
4208{
4209 int i;
Matt Gatese16a33a2012-05-01 11:43:11 -05004210 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004211
4212 i = c - h->cmd_pool;
Matt Gatese16a33a2012-05-01 11:43:11 -05004213 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004214 clear_bit(i & (BITS_PER_LONG - 1),
4215 h->cmd_pool_bits + (i / BITS_PER_LONG));
Matt Gatese16a33a2012-05-01 11:43:11 -05004216 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004217}
4218
4219static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
4220{
4221 union u64bit temp64;
4222
4223 temp64.val32.lower = c->ErrDesc.Addr.lower;
4224 temp64.val32.upper = c->ErrDesc.Addr.upper;
4225 pci_free_consistent(h->pdev, sizeof(*c->err_info),
4226 c->err_info, (dma_addr_t) temp64.val);
4227 pci_free_consistent(h->pdev, sizeof(*c),
Stephen M. Camerond896f3f2011-01-06 14:47:53 -06004228 c, (dma_addr_t) (c->busaddr & DIRECT_LOOKUP_MASK));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004229}
4230
4231#ifdef CONFIG_COMPAT
4232
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004233static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
4234{
4235 IOCTL32_Command_struct __user *arg32 =
4236 (IOCTL32_Command_struct __user *) arg;
4237 IOCTL_Command_struct arg64;
4238 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
4239 int err;
4240 u32 cp;
4241
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06004242 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004243 err = 0;
4244 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
4245 sizeof(arg64.LUN_info));
4246 err |= copy_from_user(&arg64.Request, &arg32->Request,
4247 sizeof(arg64.Request));
4248 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
4249 sizeof(arg64.error_info));
4250 err |= get_user(arg64.buf_size, &arg32->buf_size);
4251 err |= get_user(cp, &arg32->buf);
4252 arg64.buf = compat_ptr(cp);
4253 err |= copy_to_user(p, &arg64, sizeof(arg64));
4254
4255 if (err)
4256 return -EFAULT;
4257
Stephen M. Camerone39eeae2010-02-04 08:43:46 -06004258 err = hpsa_ioctl(dev, CCISS_PASSTHRU, (void *)p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004259 if (err)
4260 return err;
4261 err |= copy_in_user(&arg32->error_info, &p->error_info,
4262 sizeof(arg32->error_info));
4263 if (err)
4264 return -EFAULT;
4265 return err;
4266}
4267
4268static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
4269 int cmd, void *arg)
4270{
4271 BIG_IOCTL32_Command_struct __user *arg32 =
4272 (BIG_IOCTL32_Command_struct __user *) arg;
4273 BIG_IOCTL_Command_struct arg64;
4274 BIG_IOCTL_Command_struct __user *p =
4275 compat_alloc_user_space(sizeof(arg64));
4276 int err;
4277 u32 cp;
4278
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06004279 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004280 err = 0;
4281 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
4282 sizeof(arg64.LUN_info));
4283 err |= copy_from_user(&arg64.Request, &arg32->Request,
4284 sizeof(arg64.Request));
4285 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
4286 sizeof(arg64.error_info));
4287 err |= get_user(arg64.buf_size, &arg32->buf_size);
4288 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
4289 err |= get_user(cp, &arg32->buf);
4290 arg64.buf = compat_ptr(cp);
4291 err |= copy_to_user(p, &arg64, sizeof(arg64));
4292
4293 if (err)
4294 return -EFAULT;
4295
Stephen M. Camerone39eeae2010-02-04 08:43:46 -06004296 err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004297 if (err)
4298 return err;
4299 err |= copy_in_user(&arg32->error_info, &p->error_info,
4300 sizeof(arg32->error_info));
4301 if (err)
4302 return -EFAULT;
4303 return err;
4304}
Stephen M. Cameron71fe75a2010-02-04 08:43:51 -06004305
4306static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
4307{
4308 switch (cmd) {
4309 case CCISS_GETPCIINFO:
4310 case CCISS_GETINTINFO:
4311 case CCISS_SETINTINFO:
4312 case CCISS_GETNODENAME:
4313 case CCISS_SETNODENAME:
4314 case CCISS_GETHEARTBEAT:
4315 case CCISS_GETBUSTYPES:
4316 case CCISS_GETFIRMVER:
4317 case CCISS_GETDRIVVER:
4318 case CCISS_REVALIDVOLS:
4319 case CCISS_DEREGDISK:
4320 case CCISS_REGNEWDISK:
4321 case CCISS_REGNEWD:
4322 case CCISS_RESCANDISK:
4323 case CCISS_GETLUNINFO:
4324 return hpsa_ioctl(dev, cmd, arg);
4325
4326 case CCISS_PASSTHRU32:
4327 return hpsa_ioctl32_passthru(dev, cmd, arg);
4328 case CCISS_BIG_PASSTHRU32:
4329 return hpsa_ioctl32_big_passthru(dev, cmd, arg);
4330
4331 default:
4332 return -ENOIOCTLCMD;
4333 }
4334}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004335#endif
4336
4337static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
4338{
4339 struct hpsa_pci_info pciinfo;
4340
4341 if (!argp)
4342 return -EINVAL;
4343 pciinfo.domain = pci_domain_nr(h->pdev->bus);
4344 pciinfo.bus = h->pdev->bus->number;
4345 pciinfo.dev_fn = h->pdev->devfn;
4346 pciinfo.board_id = h->board_id;
4347 if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
4348 return -EFAULT;
4349 return 0;
4350}
4351
4352static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
4353{
4354 DriverVer_type DriverVer;
4355 unsigned char vmaj, vmin, vsubmin;
4356 int rc;
4357
4358 rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
4359 &vmaj, &vmin, &vsubmin);
4360 if (rc != 3) {
4361 dev_info(&h->pdev->dev, "driver version string '%s' "
4362 "unrecognized.", HPSA_DRIVER_VERSION);
4363 vmaj = 0;
4364 vmin = 0;
4365 vsubmin = 0;
4366 }
4367 DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
4368 if (!argp)
4369 return -EINVAL;
4370 if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
4371 return -EFAULT;
4372 return 0;
4373}
4374
4375static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
4376{
4377 IOCTL_Command_struct iocommand;
4378 struct CommandList *c;
4379 char *buff = NULL;
4380 union u64bit temp64;
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004381 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004382
4383 if (!argp)
4384 return -EINVAL;
4385 if (!capable(CAP_SYS_RAWIO))
4386 return -EPERM;
4387 if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
4388 return -EFAULT;
4389 if ((iocommand.buf_size < 1) &&
4390 (iocommand.Request.Type.Direction != XFER_NONE)) {
4391 return -EINVAL;
4392 }
4393 if (iocommand.buf_size > 0) {
4394 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
4395 if (buff == NULL)
4396 return -EFAULT;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004397 if (iocommand.Request.Type.Direction == XFER_WRITE) {
4398 /* Copy the data into the buffer we created */
4399 if (copy_from_user(buff, iocommand.buf,
4400 iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004401 rc = -EFAULT;
4402 goto out_kfree;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004403 }
4404 } else {
4405 memset(buff, 0, iocommand.buf_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004406 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004407 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004408 c = cmd_special_alloc(h);
4409 if (c == NULL) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004410 rc = -ENOMEM;
4411 goto out_kfree;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004412 }
4413 /* Fill in the command type */
4414 c->cmd_type = CMD_IOCTL_PEND;
4415 /* Fill in Command Header */
4416 c->Header.ReplyQueue = 0; /* unused in simple mode */
4417 if (iocommand.buf_size > 0) { /* buffer to fill */
4418 c->Header.SGList = 1;
4419 c->Header.SGTotal = 1;
4420 } else { /* no buffers to fill */
4421 c->Header.SGList = 0;
4422 c->Header.SGTotal = 0;
4423 }
4424 memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
4425 /* use the kernel address the cmd block for tag */
4426 c->Header.Tag.lower = c->busaddr;
4427
4428 /* Fill in Request block */
4429 memcpy(&c->Request, &iocommand.Request,
4430 sizeof(c->Request));
4431
4432 /* Fill in the scatter gather information */
4433 if (iocommand.buf_size > 0) {
4434 temp64.val = pci_map_single(h->pdev, buff,
4435 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06004436 if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
4437 c->SG[0].Addr.lower = 0;
4438 c->SG[0].Addr.upper = 0;
4439 c->SG[0].Len = 0;
4440 rc = -ENOMEM;
4441 goto out;
4442 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004443 c->SG[0].Addr.lower = temp64.val32.lower;
4444 c->SG[0].Addr.upper = temp64.val32.upper;
4445 c->SG[0].Len = iocommand.buf_size;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06004446 c->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining*/
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004447 }
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004448 hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
Stephen M. Cameronc2dd32e2011-06-03 09:57:29 -05004449 if (iocommand.buf_size > 0)
4450 hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004451 check_ioctl_unit_attention(h, c);
4452
4453 /* Copy the error information out */
4454 memcpy(&iocommand.error_info, c->err_info,
4455 sizeof(iocommand.error_info));
4456 if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004457 rc = -EFAULT;
4458 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004459 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004460 if (iocommand.Request.Type.Direction == XFER_READ &&
4461 iocommand.buf_size > 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004462 /* Copy the data out of the buffer we created */
4463 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004464 rc = -EFAULT;
4465 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004466 }
4467 }
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004468out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004469 cmd_special_free(h, c);
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004470out_kfree:
4471 kfree(buff);
4472 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004473}
4474
4475static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
4476{
4477 BIG_IOCTL_Command_struct *ioc;
4478 struct CommandList *c;
4479 unsigned char **buff = NULL;
4480 int *buff_size = NULL;
4481 union u64bit temp64;
4482 BYTE sg_used = 0;
4483 int status = 0;
4484 int i;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004485 u32 left;
4486 u32 sz;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004487 BYTE __user *data_ptr;
4488
4489 if (!argp)
4490 return -EINVAL;
4491 if (!capable(CAP_SYS_RAWIO))
4492 return -EPERM;
4493 ioc = (BIG_IOCTL_Command_struct *)
4494 kmalloc(sizeof(*ioc), GFP_KERNEL);
4495 if (!ioc) {
4496 status = -ENOMEM;
4497 goto cleanup1;
4498 }
4499 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
4500 status = -EFAULT;
4501 goto cleanup1;
4502 }
4503 if ((ioc->buf_size < 1) &&
4504 (ioc->Request.Type.Direction != XFER_NONE)) {
4505 status = -EINVAL;
4506 goto cleanup1;
4507 }
4508 /* Check kmalloc limits using all SGs */
4509 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
4510 status = -EINVAL;
4511 goto cleanup1;
4512 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06004513 if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004514 status = -EINVAL;
4515 goto cleanup1;
4516 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06004517 buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004518 if (!buff) {
4519 status = -ENOMEM;
4520 goto cleanup1;
4521 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06004522 buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004523 if (!buff_size) {
4524 status = -ENOMEM;
4525 goto cleanup1;
4526 }
4527 left = ioc->buf_size;
4528 data_ptr = ioc->buf;
4529 while (left) {
4530 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
4531 buff_size[sg_used] = sz;
4532 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
4533 if (buff[sg_used] == NULL) {
4534 status = -ENOMEM;
4535 goto cleanup1;
4536 }
4537 if (ioc->Request.Type.Direction == XFER_WRITE) {
4538 if (copy_from_user(buff[sg_used], data_ptr, sz)) {
4539 status = -ENOMEM;
4540 goto cleanup1;
4541 }
4542 } else
4543 memset(buff[sg_used], 0, sz);
4544 left -= sz;
4545 data_ptr += sz;
4546 sg_used++;
4547 }
4548 c = cmd_special_alloc(h);
4549 if (c == NULL) {
4550 status = -ENOMEM;
4551 goto cleanup1;
4552 }
4553 c->cmd_type = CMD_IOCTL_PEND;
4554 c->Header.ReplyQueue = 0;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004555 c->Header.SGList = c->Header.SGTotal = sg_used;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004556 memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
4557 c->Header.Tag.lower = c->busaddr;
4558 memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
4559 if (ioc->buf_size > 0) {
4560 int i;
4561 for (i = 0; i < sg_used; i++) {
4562 temp64.val = pci_map_single(h->pdev, buff[i],
4563 buff_size[i], PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06004564 if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
4565 c->SG[i].Addr.lower = 0;
4566 c->SG[i].Addr.upper = 0;
4567 c->SG[i].Len = 0;
4568 hpsa_pci_unmap(h->pdev, c, i,
4569 PCI_DMA_BIDIRECTIONAL);
4570 status = -ENOMEM;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05004571 goto cleanup0;
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06004572 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004573 c->SG[i].Addr.lower = temp64.val32.lower;
4574 c->SG[i].Addr.upper = temp64.val32.upper;
4575 c->SG[i].Len = buff_size[i];
Matt Gatese1d9cbf2014-02-18 13:55:12 -06004576 c->SG[i].Ext = i < sg_used - 1 ? 0 : HPSA_SG_LAST;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004577 }
4578 }
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004579 hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004580 if (sg_used)
4581 hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004582 check_ioctl_unit_attention(h, c);
4583 /* Copy the error information out */
4584 memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
4585 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004586 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05004587 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004588 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004589 if (ioc->Request.Type.Direction == XFER_READ && ioc->buf_size > 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004590 /* Copy the data out of the buffer we created */
4591 BYTE __user *ptr = ioc->buf;
4592 for (i = 0; i < sg_used; i++) {
4593 if (copy_to_user(ptr, buff[i], buff_size[i])) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004594 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05004595 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004596 }
4597 ptr += buff_size[i];
4598 }
4599 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004600 status = 0;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05004601cleanup0:
4602 cmd_special_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004603cleanup1:
4604 if (buff) {
4605 for (i = 0; i < sg_used; i++)
4606 kfree(buff[i]);
4607 kfree(buff);
4608 }
4609 kfree(buff_size);
4610 kfree(ioc);
4611 return status;
4612}
4613
4614static void check_ioctl_unit_attention(struct ctlr_info *h,
4615 struct CommandList *c)
4616{
4617 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
4618 c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
4619 (void) check_for_unit_attention(h, c);
4620}
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05004621
4622static int increment_passthru_count(struct ctlr_info *h)
4623{
4624 unsigned long flags;
4625
4626 spin_lock_irqsave(&h->passthru_count_lock, flags);
4627 if (h->passthru_count >= HPSA_MAX_CONCURRENT_PASSTHRUS) {
4628 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
4629 return -1;
4630 }
4631 h->passthru_count++;
4632 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
4633 return 0;
4634}
4635
4636static void decrement_passthru_count(struct ctlr_info *h)
4637{
4638 unsigned long flags;
4639
4640 spin_lock_irqsave(&h->passthru_count_lock, flags);
4641 if (h->passthru_count <= 0) {
4642 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
4643 /* not expecting to get here. */
4644 dev_warn(&h->pdev->dev, "Bug detected, passthru_count seems to be incorrect.\n");
4645 return;
4646 }
4647 h->passthru_count--;
4648 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
4649}
4650
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004651/*
4652 * ioctl
4653 */
4654static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
4655{
4656 struct ctlr_info *h;
4657 void __user *argp = (void __user *)arg;
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05004658 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004659
4660 h = sdev_to_hba(dev);
4661
4662 switch (cmd) {
4663 case CCISS_DEREGDISK:
4664 case CCISS_REGNEWDISK:
4665 case CCISS_REGNEWD:
Stephen M. Camerona08a8472010-02-04 08:43:16 -06004666 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004667 return 0;
4668 case CCISS_GETPCIINFO:
4669 return hpsa_getpciinfo_ioctl(h, argp);
4670 case CCISS_GETDRIVVER:
4671 return hpsa_getdrivver_ioctl(h, argp);
4672 case CCISS_PASSTHRU:
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05004673 if (increment_passthru_count(h))
4674 return -EAGAIN;
4675 rc = hpsa_passthru_ioctl(h, argp);
4676 decrement_passthru_count(h);
4677 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004678 case CCISS_BIG_PASSTHRU:
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05004679 if (increment_passthru_count(h))
4680 return -EAGAIN;
4681 rc = hpsa_big_passthru_ioctl(h, argp);
4682 decrement_passthru_count(h);
4683 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004684 default:
4685 return -ENOTTY;
4686 }
4687}
4688
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004689static int hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
4690 u8 reset_type)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004691{
4692 struct CommandList *c;
4693
4694 c = cmd_alloc(h);
4695 if (!c)
4696 return -ENOMEM;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004697 /* fill_cmd can't fail here, no data buffer to map */
4698 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004699 RAID_CTLR_LUNID, TYPE_MSG);
4700 c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
4701 c->waiting = NULL;
4702 enqueue_cmd_and_start_io(h, c);
4703 /* Don't wait for completion, the reset won't complete. Don't free
4704 * the command either. This is the last command we will send before
4705 * re-initializing everything, so it doesn't matter and won't leak.
4706 */
4707 return 0;
4708}
4709
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004710static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06004711 void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004712 int cmd_type)
4713{
4714 int pci_dir = XFER_NONE;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004715 struct CommandList *a; /* for commands to be aborted */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004716
4717 c->cmd_type = CMD_IOCTL_PEND;
4718 c->Header.ReplyQueue = 0;
4719 if (buff != NULL && size > 0) {
4720 c->Header.SGList = 1;
4721 c->Header.SGTotal = 1;
4722 } else {
4723 c->Header.SGList = 0;
4724 c->Header.SGTotal = 0;
4725 }
4726 c->Header.Tag.lower = c->busaddr;
4727 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
4728
4729 c->Request.Type.Type = cmd_type;
4730 if (cmd_type == TYPE_CMD) {
4731 switch (cmd) {
4732 case HPSA_INQUIRY:
4733 /* are we trying to read a vital product page */
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06004734 if (page_code & VPD_PAGE) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004735 c->Request.CDB[1] = 0x01;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06004736 c->Request.CDB[2] = (page_code & 0xff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004737 }
4738 c->Request.CDBLen = 6;
4739 c->Request.Type.Attribute = ATTR_SIMPLE;
4740 c->Request.Type.Direction = XFER_READ;
4741 c->Request.Timeout = 0;
4742 c->Request.CDB[0] = HPSA_INQUIRY;
4743 c->Request.CDB[4] = size & 0xFF;
4744 break;
4745 case HPSA_REPORT_LOG:
4746 case HPSA_REPORT_PHYS:
4747 /* Talking to controller so It's a physical command
4748 mode = 00 target = 0. Nothing to write.
4749 */
4750 c->Request.CDBLen = 12;
4751 c->Request.Type.Attribute = ATTR_SIMPLE;
4752 c->Request.Type.Direction = XFER_READ;
4753 c->Request.Timeout = 0;
4754 c->Request.CDB[0] = cmd;
4755 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
4756 c->Request.CDB[7] = (size >> 16) & 0xFF;
4757 c->Request.CDB[8] = (size >> 8) & 0xFF;
4758 c->Request.CDB[9] = size & 0xFF;
4759 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004760 case HPSA_CACHE_FLUSH:
4761 c->Request.CDBLen = 12;
4762 c->Request.Type.Attribute = ATTR_SIMPLE;
4763 c->Request.Type.Direction = XFER_WRITE;
4764 c->Request.Timeout = 0;
4765 c->Request.CDB[0] = BMIC_WRITE;
4766 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
Stephen M. Cameronbb158ea2011-10-26 16:21:17 -05004767 c->Request.CDB[7] = (size >> 8) & 0xFF;
4768 c->Request.CDB[8] = size & 0xFF;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004769 break;
4770 case TEST_UNIT_READY:
4771 c->Request.CDBLen = 6;
4772 c->Request.Type.Attribute = ATTR_SIMPLE;
4773 c->Request.Type.Direction = XFER_NONE;
4774 c->Request.Timeout = 0;
4775 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004776 case HPSA_GET_RAID_MAP:
4777 c->Request.CDBLen = 12;
4778 c->Request.Type.Attribute = ATTR_SIMPLE;
4779 c->Request.Type.Direction = XFER_READ;
4780 c->Request.Timeout = 0;
4781 c->Request.CDB[0] = HPSA_CISS_READ;
4782 c->Request.CDB[1] = cmd;
4783 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
4784 c->Request.CDB[7] = (size >> 16) & 0xFF;
4785 c->Request.CDB[8] = (size >> 8) & 0xFF;
4786 c->Request.CDB[9] = size & 0xFF;
4787 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004788 default:
4789 dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
4790 BUG();
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004791 return -1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004792 }
4793 } else if (cmd_type == TYPE_MSG) {
4794 switch (cmd) {
4795
4796 case HPSA_DEVICE_RESET_MSG:
4797 c->Request.CDBLen = 16;
4798 c->Request.Type.Type = 1; /* It is a MSG not a CMD */
4799 c->Request.Type.Attribute = ATTR_SIMPLE;
4800 c->Request.Type.Direction = XFER_NONE;
4801 c->Request.Timeout = 0; /* Don't time out */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004802 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
4803 c->Request.CDB[0] = cmd;
Stephen M. Cameron21e89af2012-07-26 11:34:10 -05004804 c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004805 /* If bytes 4-7 are zero, it means reset the */
4806 /* LunID device */
4807 c->Request.CDB[4] = 0x00;
4808 c->Request.CDB[5] = 0x00;
4809 c->Request.CDB[6] = 0x00;
4810 c->Request.CDB[7] = 0x00;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004811 break;
4812 case HPSA_ABORT_MSG:
4813 a = buff; /* point to command to be aborted */
4814 dev_dbg(&h->pdev->dev, "Abort Tag:0x%08x:%08x using request Tag:0x%08x:%08x\n",
4815 a->Header.Tag.upper, a->Header.Tag.lower,
4816 c->Header.Tag.upper, c->Header.Tag.lower);
4817 c->Request.CDBLen = 16;
4818 c->Request.Type.Type = TYPE_MSG;
4819 c->Request.Type.Attribute = ATTR_SIMPLE;
4820 c->Request.Type.Direction = XFER_WRITE;
4821 c->Request.Timeout = 0; /* Don't time out */
4822 c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
4823 c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
4824 c->Request.CDB[2] = 0x00; /* reserved */
4825 c->Request.CDB[3] = 0x00; /* reserved */
4826 /* Tag to abort goes in CDB[4]-CDB[11] */
4827 c->Request.CDB[4] = a->Header.Tag.lower & 0xFF;
4828 c->Request.CDB[5] = (a->Header.Tag.lower >> 8) & 0xFF;
4829 c->Request.CDB[6] = (a->Header.Tag.lower >> 16) & 0xFF;
4830 c->Request.CDB[7] = (a->Header.Tag.lower >> 24) & 0xFF;
4831 c->Request.CDB[8] = a->Header.Tag.upper & 0xFF;
4832 c->Request.CDB[9] = (a->Header.Tag.upper >> 8) & 0xFF;
4833 c->Request.CDB[10] = (a->Header.Tag.upper >> 16) & 0xFF;
4834 c->Request.CDB[11] = (a->Header.Tag.upper >> 24) & 0xFF;
4835 c->Request.CDB[12] = 0x00; /* reserved */
4836 c->Request.CDB[13] = 0x00; /* reserved */
4837 c->Request.CDB[14] = 0x00; /* reserved */
4838 c->Request.CDB[15] = 0x00; /* reserved */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004839 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004840 default:
4841 dev_warn(&h->pdev->dev, "unknown message type %d\n",
4842 cmd);
4843 BUG();
4844 }
4845 } else {
4846 dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
4847 BUG();
4848 }
4849
4850 switch (c->Request.Type.Direction) {
4851 case XFER_READ:
4852 pci_dir = PCI_DMA_FROMDEVICE;
4853 break;
4854 case XFER_WRITE:
4855 pci_dir = PCI_DMA_TODEVICE;
4856 break;
4857 case XFER_NONE:
4858 pci_dir = PCI_DMA_NONE;
4859 break;
4860 default:
4861 pci_dir = PCI_DMA_BIDIRECTIONAL;
4862 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004863 if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
4864 return -1;
4865 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004866}
4867
4868/*
4869 * Map (physical) PCI mem into (virtual) kernel space
4870 */
4871static void __iomem *remap_pci_mem(ulong base, ulong size)
4872{
4873 ulong page_base = ((ulong) base) & PAGE_MASK;
4874 ulong page_offs = ((ulong) base) - page_base;
Stephen M. Cameron088ba342012-07-26 11:34:23 -05004875 void __iomem *page_remapped = ioremap_nocache(page_base,
4876 page_offs + size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004877
4878 return page_remapped ? (page_remapped + page_offs) : NULL;
4879}
4880
4881/* Takes cmds off the submission queue and sends them to the hardware,
4882 * then puts them on the queue of cmds waiting for completion.
4883 */
4884static void start_io(struct ctlr_info *h)
4885{
4886 struct CommandList *c;
Matt Gatese16a33a2012-05-01 11:43:11 -05004887 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004888
Matt Gatese16a33a2012-05-01 11:43:11 -05004889 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06004890 while (!list_empty(&h->reqQ)) {
4891 c = list_entry(h->reqQ.next, struct CommandList, list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004892 /* can't do anything if fifo is full */
4893 if ((h->access.fifo_full(h))) {
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004894 h->fifo_recently_full = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004895 dev_warn(&h->pdev->dev, "fifo full\n");
4896 break;
4897 }
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004898 h->fifo_recently_full = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004899
4900 /* Get the first entry from the Request Q */
4901 removeQ(c);
4902 h->Qdepth--;
4903
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004904 /* Put job onto the completed Q */
4905 addQ(&h->cmpQ, c);
Matt Gatese16a33a2012-05-01 11:43:11 -05004906
4907 /* Must increment commands_outstanding before unlocking
4908 * and submitting to avoid race checking for fifo full
4909 * condition.
4910 */
4911 h->commands_outstanding++;
4912 if (h->commands_outstanding > h->max_outstanding)
4913 h->max_outstanding = h->commands_outstanding;
4914
4915 /* Tell the controller execute command */
4916 spin_unlock_irqrestore(&h->lock, flags);
4917 h->access.submit_command(h, c);
4918 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004919 }
Matt Gatese16a33a2012-05-01 11:43:11 -05004920 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004921}
4922
Matt Gates254f7962012-05-01 11:43:06 -05004923static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004924{
Matt Gates254f7962012-05-01 11:43:06 -05004925 return h->access.command_completed(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004926}
4927
Stephen M. Cameron900c5442010-02-04 08:42:35 -06004928static inline bool interrupt_pending(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004929{
4930 return h->access.intr_pending(h);
4931}
4932
4933static inline long interrupt_not_for_us(struct ctlr_info *h)
4934{
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004935 return (h->access.intr_pending(h) == 0) ||
4936 (h->interrupts_enabled == 0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004937}
4938
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004939static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
4940 u32 raw_tag)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004941{
4942 if (unlikely(tag_index >= h->nr_cmds)) {
4943 dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
4944 return 1;
4945 }
4946 return 0;
4947}
4948
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05004949static inline void finish_cmd(struct CommandList *c)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004950{
Matt Gatese16a33a2012-05-01 11:43:11 -05004951 unsigned long flags;
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004952 int io_may_be_stalled = 0;
4953 struct ctlr_info *h = c->h;
Matt Gatese16a33a2012-05-01 11:43:11 -05004954
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004955 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004956 removeQ(c);
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004957
4958 /*
4959 * Check for possibly stalled i/o.
4960 *
4961 * If a fifo_full condition is encountered, requests will back up
4962 * in h->reqQ. This queue is only emptied out by start_io which is
4963 * only called when a new i/o request comes in. If no i/o's are
4964 * forthcoming, the i/o's in h->reqQ can get stuck. So we call
4965 * start_io from here if we detect such a danger.
4966 *
4967 * Normally, we shouldn't hit this case, but pounding on the
4968 * CCISS_PASSTHRU ioctl can provoke it. Only call start_io if
4969 * commands_outstanding is low. We want to avoid calling
4970 * start_io from in here as much as possible, and esp. don't
4971 * want to get in a cycle where we call start_io every time
4972 * through here.
4973 */
4974 if (unlikely(h->fifo_recently_full) &&
4975 h->commands_outstanding < 5)
4976 io_may_be_stalled = 1;
4977
4978 spin_unlock_irqrestore(&h->lock, flags);
4979
Stephen M. Camerone85c5972012-05-01 11:43:42 -05004980 dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
Scott Teelc3497752014-02-18 13:56:34 -06004981 if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
4982 || c->cmd_type == CMD_IOACCEL2))
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05004983 complete_scsi_command(c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004984 else if (c->cmd_type == CMD_IOCTL_PEND)
4985 complete(c->waiting);
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004986 if (unlikely(io_may_be_stalled))
4987 start_io(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004988}
4989
Stephen M. Camerona104c992010-02-04 08:42:24 -06004990static inline u32 hpsa_tag_contains_index(u32 tag)
4991{
Stephen M. Camerona104c992010-02-04 08:42:24 -06004992 return tag & DIRECT_LOOKUP_BIT;
4993}
4994
4995static inline u32 hpsa_tag_to_index(u32 tag)
4996{
Stephen M. Camerona104c992010-02-04 08:42:24 -06004997 return tag >> DIRECT_LOOKUP_SHIFT;
4998}
4999
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005000
5001static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
Stephen M. Camerona104c992010-02-04 08:42:24 -06005002{
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005003#define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
5004#define HPSA_SIMPLE_ERROR_BITS 0x03
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06005005 if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005006 return tag & ~HPSA_SIMPLE_ERROR_BITS;
5007 return tag & ~HPSA_PERF_ERROR_BITS;
Stephen M. Camerona104c992010-02-04 08:42:24 -06005008}
5009
Don Brace303932f2010-02-04 08:42:40 -06005010/* process completion of an indexed ("direct lookup") command */
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005011static inline void process_indexed_cmd(struct ctlr_info *h,
Don Brace303932f2010-02-04 08:42:40 -06005012 u32 raw_tag)
5013{
5014 u32 tag_index;
5015 struct CommandList *c;
5016
5017 tag_index = hpsa_tag_to_index(raw_tag);
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005018 if (!bad_tag(h, tag_index, raw_tag)) {
5019 c = h->cmd_pool + tag_index;
5020 finish_cmd(c);
5021 }
Don Brace303932f2010-02-04 08:42:40 -06005022}
5023
5024/* process completion of a non-indexed command */
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005025static inline void process_nonindexed_cmd(struct ctlr_info *h,
Don Brace303932f2010-02-04 08:42:40 -06005026 u32 raw_tag)
5027{
5028 u32 tag;
5029 struct CommandList *c = NULL;
Matt Gatese16a33a2012-05-01 11:43:11 -05005030 unsigned long flags;
Don Brace303932f2010-02-04 08:42:40 -06005031
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005032 tag = hpsa_tag_discard_error_bits(h, raw_tag);
Matt Gatese16a33a2012-05-01 11:43:11 -05005033 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06005034 list_for_each_entry(c, &h->cmpQ, list) {
Don Brace303932f2010-02-04 08:42:40 -06005035 if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
Matt Gatese16a33a2012-05-01 11:43:11 -05005036 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05005037 finish_cmd(c);
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005038 return;
Don Brace303932f2010-02-04 08:42:40 -06005039 }
5040 }
Matt Gatese16a33a2012-05-01 11:43:11 -05005041 spin_unlock_irqrestore(&h->lock, flags);
Don Brace303932f2010-02-04 08:42:40 -06005042 bad_tag(h, h->nr_cmds + 1, raw_tag);
Don Brace303932f2010-02-04 08:42:40 -06005043}
5044
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005045/* Some controllers, like p400, will give us one interrupt
5046 * after a soft reset, even if we turned interrupts off.
5047 * Only need to check for this in the hpsa_xxx_discard_completions
5048 * functions.
5049 */
5050static int ignore_bogus_interrupt(struct ctlr_info *h)
5051{
5052 if (likely(!reset_devices))
5053 return 0;
5054
5055 if (likely(h->interrupts_enabled))
5056 return 0;
5057
5058 dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
5059 "(known firmware bug.) Ignoring.\n");
5060
5061 return 1;
5062}
5063
Matt Gates254f7962012-05-01 11:43:06 -05005064/*
5065 * Convert &h->q[x] (passed to interrupt handlers) back to h.
5066 * Relies on (h-q[x] == x) being true for x such that
5067 * 0 <= x < MAX_REPLY_QUEUES.
5068 */
5069static struct ctlr_info *queue_to_hba(u8 *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005070{
Matt Gates254f7962012-05-01 11:43:06 -05005071 return container_of((queue - *queue), struct ctlr_info, q[0]);
5072}
5073
5074static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
5075{
5076 struct ctlr_info *h = queue_to_hba(queue);
5077 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005078 u32 raw_tag;
5079
5080 if (ignore_bogus_interrupt(h))
5081 return IRQ_NONE;
5082
5083 if (interrupt_not_for_us(h))
5084 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005085 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005086 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05005087 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005088 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05005089 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005090 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005091 return IRQ_HANDLED;
5092}
5093
Matt Gates254f7962012-05-01 11:43:06 -05005094static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005095{
Matt Gates254f7962012-05-01 11:43:06 -05005096 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005097 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05005098 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005099
5100 if (ignore_bogus_interrupt(h))
5101 return IRQ_NONE;
5102
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005103 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05005104 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005105 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05005106 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005107 return IRQ_HANDLED;
5108}
5109
Matt Gates254f7962012-05-01 11:43:06 -05005110static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005111{
Matt Gates254f7962012-05-01 11:43:06 -05005112 struct ctlr_info *h = queue_to_hba((u8 *) queue);
Don Brace303932f2010-02-04 08:42:40 -06005113 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05005114 u8 q = *(u8 *) queue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005115
5116 if (interrupt_not_for_us(h))
5117 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005118 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005119 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05005120 raw_tag = get_next_completion(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005121 while (raw_tag != FIFO_EMPTY) {
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005122 if (likely(hpsa_tag_contains_index(raw_tag)))
5123 process_indexed_cmd(h, raw_tag);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005124 else
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005125 process_nonindexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05005126 raw_tag = next_command(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005127 }
5128 }
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005129 return IRQ_HANDLED;
5130}
5131
Matt Gates254f7962012-05-01 11:43:06 -05005132static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005133{
Matt Gates254f7962012-05-01 11:43:06 -05005134 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005135 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05005136 u8 q = *(u8 *) queue;
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005137
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005138 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05005139 raw_tag = get_next_completion(h, q);
Don Brace303932f2010-02-04 08:42:40 -06005140 while (raw_tag != FIFO_EMPTY) {
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005141 if (likely(hpsa_tag_contains_index(raw_tag)))
5142 process_indexed_cmd(h, raw_tag);
Don Brace303932f2010-02-04 08:42:40 -06005143 else
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005144 process_nonindexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05005145 raw_tag = next_command(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005146 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005147 return IRQ_HANDLED;
5148}
5149
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005150/* Send a message CDB to the firmware. Careful, this only works
5151 * in simple mode, not performant mode due to the tag lookup.
5152 * We only ever use this immediately after a controller reset.
5153 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005154static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
5155 unsigned char type)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005156{
5157 struct Command {
5158 struct CommandListHeader CommandHeader;
5159 struct RequestBlock Request;
5160 struct ErrDescriptor ErrorDescriptor;
5161 };
5162 struct Command *cmd;
5163 static const size_t cmd_sz = sizeof(*cmd) +
5164 sizeof(cmd->ErrorDescriptor);
5165 dma_addr_t paddr64;
5166 uint32_t paddr32, tag;
5167 void __iomem *vaddr;
5168 int i, err;
5169
5170 vaddr = pci_ioremap_bar(pdev, 0);
5171 if (vaddr == NULL)
5172 return -ENOMEM;
5173
5174 /* The Inbound Post Queue only accepts 32-bit physical addresses for the
5175 * CCISS commands, so they must be allocated from the lower 4GiB of
5176 * memory.
5177 */
5178 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
5179 if (err) {
5180 iounmap(vaddr);
5181 return -ENOMEM;
5182 }
5183
5184 cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
5185 if (cmd == NULL) {
5186 iounmap(vaddr);
5187 return -ENOMEM;
5188 }
5189
5190 /* This must fit, because of the 32-bit consistent DMA mask. Also,
5191 * although there's no guarantee, we assume that the address is at
5192 * least 4-byte aligned (most likely, it's page-aligned).
5193 */
5194 paddr32 = paddr64;
5195
5196 cmd->CommandHeader.ReplyQueue = 0;
5197 cmd->CommandHeader.SGList = 0;
5198 cmd->CommandHeader.SGTotal = 0;
5199 cmd->CommandHeader.Tag.lower = paddr32;
5200 cmd->CommandHeader.Tag.upper = 0;
5201 memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
5202
5203 cmd->Request.CDBLen = 16;
5204 cmd->Request.Type.Type = TYPE_MSG;
5205 cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
5206 cmd->Request.Type.Direction = XFER_NONE;
5207 cmd->Request.Timeout = 0; /* Don't time out */
5208 cmd->Request.CDB[0] = opcode;
5209 cmd->Request.CDB[1] = type;
5210 memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
5211 cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
5212 cmd->ErrorDescriptor.Addr.upper = 0;
5213 cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
5214
5215 writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
5216
5217 for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
5218 tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005219 if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr32)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005220 break;
5221 msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
5222 }
5223
5224 iounmap(vaddr);
5225
5226 /* we leak the DMA buffer here ... no choice since the controller could
5227 * still complete the command.
5228 */
5229 if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
5230 dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
5231 opcode, type);
5232 return -ETIMEDOUT;
5233 }
5234
5235 pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
5236
5237 if (tag & HPSA_ERROR_BIT) {
5238 dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
5239 opcode, type);
5240 return -EIO;
5241 }
5242
5243 dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
5244 opcode, type);
5245 return 0;
5246}
5247
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005248#define hpsa_noop(p) hpsa_message(p, 3, 0)
5249
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005250static int hpsa_controller_hard_reset(struct pci_dev *pdev,
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005251 void * __iomem vaddr, u32 use_doorbell)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005252{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005253 u16 pmcsr;
5254 int pos;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005255
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005256 if (use_doorbell) {
5257 /* For everything after the P600, the PCI power state method
5258 * of resetting the controller doesn't work, so we have this
5259 * other way using the doorbell register.
5260 */
5261 dev_info(&pdev->dev, "using doorbell to reset controller\n");
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005262 writel(use_doorbell, vaddr + SA5_DOORBELL);
Stephen M. Cameron85009232013-09-23 13:33:36 -05005263
5264 /* PMC hardware guys tell us we need a 5 second delay after
5265 * doorbell reset and before any attempt to talk to the board
5266 * at all to ensure that this actually works and doesn't fall
5267 * over in some weird corner cases.
5268 */
5269 msleep(5000);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005270 } else { /* Try to do it the PCI power state way */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005271
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005272 /* Quoting from the Open CISS Specification: "The Power
5273 * Management Control/Status Register (CSR) controls the power
5274 * state of the device. The normal operating state is D0,
5275 * CSR=00h. The software off state is D3, CSR=03h. To reset
5276 * the controller, place the interface device in D3 then to D0,
5277 * this causes a secondary PCI reset which will reset the
5278 * controller." */
5279
5280 pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
5281 if (pos == 0) {
5282 dev_err(&pdev->dev,
5283 "hpsa_reset_controller: "
5284 "PCI PM not supported\n");
5285 return -ENODEV;
5286 }
5287 dev_info(&pdev->dev, "using PCI PM to reset controller\n");
5288 /* enter the D3hot power management state */
5289 pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
5290 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
5291 pmcsr |= PCI_D3hot;
5292 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
5293
5294 msleep(500);
5295
5296 /* enter the D0 power management state */
5297 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
5298 pmcsr |= PCI_D0;
5299 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
Mike Millerc4853ef2011-10-21 08:19:43 +02005300
5301 /*
5302 * The P600 requires a small delay when changing states.
5303 * Otherwise we may think the board did not reset and we bail.
5304 * This for kdump only and is particular to the P600.
5305 */
5306 msleep(500);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005307 }
5308 return 0;
5309}
5310
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005311static void init_driver_version(char *driver_version, int len)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005312{
5313 memset(driver_version, 0, len);
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06005314 strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005315}
5316
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005317static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005318{
5319 char *driver_version;
5320 int i, size = sizeof(cfgtable->driver_version);
5321
5322 driver_version = kmalloc(size, GFP_KERNEL);
5323 if (!driver_version)
5324 return -ENOMEM;
5325
5326 init_driver_version(driver_version, size);
5327 for (i = 0; i < size; i++)
5328 writeb(driver_version[i], &cfgtable->driver_version[i]);
5329 kfree(driver_version);
5330 return 0;
5331}
5332
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005333static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
5334 unsigned char *driver_ver)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005335{
5336 int i;
5337
5338 for (i = 0; i < sizeof(cfgtable->driver_version); i++)
5339 driver_ver[i] = readb(&cfgtable->driver_version[i]);
5340}
5341
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005342static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005343{
5344
5345 char *driver_ver, *old_driver_ver;
5346 int rc, size = sizeof(cfgtable->driver_version);
5347
5348 old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
5349 if (!old_driver_ver)
5350 return -ENOMEM;
5351 driver_ver = old_driver_ver + size;
5352
5353 /* After a reset, the 32 bytes of "driver version" in the cfgtable
5354 * should have been changed, otherwise we know the reset failed.
5355 */
5356 init_driver_version(old_driver_ver, size);
5357 read_driver_ver_from_cfgtable(cfgtable, driver_ver);
5358 rc = !memcmp(driver_ver, old_driver_ver, size);
5359 kfree(old_driver_ver);
5360 return rc;
5361}
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005362/* This does a hard reset of the controller using PCI power management
5363 * states or the using the doorbell register.
5364 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005365static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev)
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005366{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005367 u64 cfg_offset;
5368 u32 cfg_base_addr;
5369 u64 cfg_base_addr_index;
5370 void __iomem *vaddr;
5371 unsigned long paddr;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005372 u32 misc_fw_support;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005373 int rc;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005374 struct CfgTable __iomem *cfgtable;
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005375 u32 use_doorbell;
Stephen M. Cameron18867652010-06-16 13:51:45 -05005376 u32 board_id;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005377 u16 command_register;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005378
5379 /* For controllers as old as the P600, this is very nearly
5380 * the same thing as
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005381 *
5382 * pci_save_state(pci_dev);
5383 * pci_set_power_state(pci_dev, PCI_D3hot);
5384 * pci_set_power_state(pci_dev, PCI_D0);
5385 * pci_restore_state(pci_dev);
5386 *
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005387 * For controllers newer than the P600, the pci power state
5388 * method of resetting doesn't work so we have another way
5389 * using the doorbell register.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005390 */
Stephen M. Cameron18867652010-06-16 13:51:45 -05005391
Stephen M. Cameron25c1e56a2011-01-06 14:48:18 -06005392 rc = hpsa_lookup_board_id(pdev, &board_id);
Stephen M. Cameron46380782011-05-03 15:00:01 -05005393 if (rc < 0 || !ctlr_is_resettable(board_id)) {
Stephen M. Cameron25c1e56a2011-01-06 14:48:18 -06005394 dev_warn(&pdev->dev, "Not resetting device.\n");
5395 return -ENODEV;
5396 }
Stephen M. Cameron46380782011-05-03 15:00:01 -05005397
5398 /* if controller is soft- but not hard resettable... */
5399 if (!ctlr_is_hard_resettable(board_id))
5400 return -ENOTSUPP; /* try soft reset later. */
Stephen M. Cameron18867652010-06-16 13:51:45 -05005401
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005402 /* Save the PCI command register */
5403 pci_read_config_word(pdev, 4, &command_register);
5404 /* Turn the board off. This is so that later pci_restore_state()
5405 * won't turn the board on before the rest of config space is ready.
5406 */
5407 pci_disable_device(pdev);
5408 pci_save_state(pdev);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005409
5410 /* find the first memory BAR, so we can find the cfg table */
5411 rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
5412 if (rc)
5413 return rc;
5414 vaddr = remap_pci_mem(paddr, 0x250);
5415 if (!vaddr)
5416 return -ENOMEM;
5417
5418 /* find cfgtable in order to check if reset via doorbell is supported */
5419 rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
5420 &cfg_base_addr_index, &cfg_offset);
5421 if (rc)
5422 goto unmap_vaddr;
5423 cfgtable = remap_pci_mem(pci_resource_start(pdev,
5424 cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
5425 if (!cfgtable) {
5426 rc = -ENOMEM;
5427 goto unmap_vaddr;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005428 }
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005429 rc = write_driver_ver_to_cfgtable(cfgtable);
5430 if (rc)
5431 goto unmap_vaddr;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005432
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005433 /* If reset via doorbell register is supported, use that.
5434 * There are two such methods. Favor the newest method.
5435 */
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005436 misc_fw_support = readl(&cfgtable->misc_fw_support);
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005437 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
5438 if (use_doorbell) {
5439 use_doorbell = DOORBELL_CTLR_RESET2;
5440 } else {
5441 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
5442 if (use_doorbell) {
Mike Millerfba63092011-10-13 11:44:06 -05005443 dev_warn(&pdev->dev, "Soft reset not supported. "
5444 "Firmware update is required.\n");
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005445 rc = -ENOTSUPP; /* try soft reset */
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005446 goto unmap_cfgtable;
5447 }
5448 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005449
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005450 rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
5451 if (rc)
5452 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005453
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005454 pci_restore_state(pdev);
5455 rc = pci_enable_device(pdev);
5456 if (rc) {
5457 dev_warn(&pdev->dev, "failed to enable device.\n");
5458 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005459 }
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005460 pci_write_config_word(pdev, 4, command_register);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005461
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005462 /* Some devices (notably the HP Smart Array 5i Controller)
5463 need a little pause here */
5464 msleep(HPSA_POST_RESET_PAUSE_MSECS);
5465
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005466 rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
5467 if (rc) {
5468 dev_warn(&pdev->dev,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005469 "failed waiting for board to become ready "
5470 "after hard reset\n");
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005471 goto unmap_cfgtable;
5472 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005473
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005474 rc = controller_reset_failed(vaddr);
5475 if (rc < 0)
5476 goto unmap_cfgtable;
5477 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005478 dev_warn(&pdev->dev, "Unable to successfully reset "
5479 "controller. Will try soft reset.\n");
5480 rc = -ENOTSUPP;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005481 } else {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005482 dev_info(&pdev->dev, "board ready after hard reset.\n");
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005483 }
5484
5485unmap_cfgtable:
5486 iounmap(cfgtable);
5487
5488unmap_vaddr:
5489 iounmap(vaddr);
5490 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005491}
5492
5493/*
5494 * We cannot read the structure directly, for portability we must use
5495 * the io functions.
5496 * This is for debug only.
5497 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005498static void print_cfg_table(struct device *dev, struct CfgTable *tb)
5499{
Stephen M. Cameron58f86652010-05-27 15:13:58 -05005500#ifdef HPSA_DEBUG
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005501 int i;
5502 char temp_name[17];
5503
5504 dev_info(dev, "Controller Configuration information\n");
5505 dev_info(dev, "------------------------------------\n");
5506 for (i = 0; i < 4; i++)
5507 temp_name[i] = readb(&(tb->Signature[i]));
5508 temp_name[4] = '\0';
5509 dev_info(dev, " Signature = %s\n", temp_name);
5510 dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
5511 dev_info(dev, " Transport methods supported = 0x%x\n",
5512 readl(&(tb->TransportSupport)));
5513 dev_info(dev, " Transport methods active = 0x%x\n",
5514 readl(&(tb->TransportActive)));
5515 dev_info(dev, " Requested transport Method = 0x%x\n",
5516 readl(&(tb->HostWrite.TransportRequest)));
5517 dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
5518 readl(&(tb->HostWrite.CoalIntDelay)));
5519 dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
5520 readl(&(tb->HostWrite.CoalIntCount)));
5521 dev_info(dev, " Max outstanding commands = 0x%d\n",
5522 readl(&(tb->CmdsOutMax)));
5523 dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
5524 for (i = 0; i < 16; i++)
5525 temp_name[i] = readb(&(tb->ServerName[i]));
5526 temp_name[16] = '\0';
5527 dev_info(dev, " Server Name = %s\n", temp_name);
5528 dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
5529 readl(&(tb->HeartBeat)));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005530#endif /* HPSA_DEBUG */
Stephen M. Cameron58f86652010-05-27 15:13:58 -05005531}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005532
5533static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
5534{
5535 int i, offset, mem_type, bar_type;
5536
5537 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
5538 return 0;
5539 offset = 0;
5540 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
5541 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
5542 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
5543 offset += 4;
5544 else {
5545 mem_type = pci_resource_flags(pdev, i) &
5546 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
5547 switch (mem_type) {
5548 case PCI_BASE_ADDRESS_MEM_TYPE_32:
5549 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
5550 offset += 4; /* 32 bit */
5551 break;
5552 case PCI_BASE_ADDRESS_MEM_TYPE_64:
5553 offset += 8;
5554 break;
5555 default: /* reserved in PCI 2.2 */
5556 dev_warn(&pdev->dev,
5557 "base address is invalid\n");
5558 return -1;
5559 break;
5560 }
5561 }
5562 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
5563 return i + 1;
5564 }
5565 return -1;
5566}
5567
5568/* If MSI/MSI-X is supported by the kernel we will try to enable it on
5569 * controllers that are capable. If not, we use IO-APIC mode.
5570 */
5571
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005572static void hpsa_interrupt_mode(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005573{
5574#ifdef CONFIG_PCI_MSI
Matt Gates254f7962012-05-01 11:43:06 -05005575 int err, i;
5576 struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
5577
5578 for (i = 0; i < MAX_REPLY_QUEUES; i++) {
5579 hpsa_msix_entries[i].vector = 0;
5580 hpsa_msix_entries[i].entry = i;
5581 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005582
5583 /* Some boards advertise MSI but don't really support it */
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05005584 if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
5585 (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005586 goto default_int_mode;
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005587 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
5588 dev_info(&h->pdev->dev, "MSIX\n");
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005589 h->msix_vector = MAX_REPLY_QUEUES;
Matt Gates254f7962012-05-01 11:43:06 -05005590 err = pci_enable_msix(h->pdev, hpsa_msix_entries,
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005591 h->msix_vector);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005592 if (err > 0) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005593 dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005594 "available\n", err);
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005595 h->msix_vector = err;
5596 err = pci_enable_msix(h->pdev, hpsa_msix_entries,
5597 h->msix_vector);
5598 }
5599 if (!err) {
5600 for (i = 0; i < h->msix_vector; i++)
5601 h->intr[i] = hpsa_msix_entries[i].vector;
5602 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005603 } else {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005604 dev_warn(&h->pdev->dev, "MSI-X init failed %d\n",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005605 err);
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005606 h->msix_vector = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005607 goto default_int_mode;
5608 }
5609 }
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005610 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
5611 dev_info(&h->pdev->dev, "MSI\n");
5612 if (!pci_enable_msi(h->pdev))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005613 h->msi_vector = 1;
5614 else
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005615 dev_warn(&h->pdev->dev, "MSI init failed\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005616 }
5617default_int_mode:
5618#endif /* CONFIG_PCI_MSI */
5619 /* if we get here we're going to use the default interrupt mode */
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005620 h->intr[h->intr_mode] = h->pdev->irq;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005621}
5622
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005623static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05005624{
5625 int i;
5626 u32 subsystem_vendor_id, subsystem_device_id;
5627
5628 subsystem_vendor_id = pdev->subsystem_vendor;
5629 subsystem_device_id = pdev->subsystem_device;
5630 *board_id = ((subsystem_device_id << 16) & 0xffff0000) |
5631 subsystem_vendor_id;
5632
5633 for (i = 0; i < ARRAY_SIZE(products); i++)
5634 if (*board_id == products[i].board_id)
5635 return i;
5636
Stephen M. Cameron6798cc02010-06-16 13:51:20 -05005637 if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
5638 subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
5639 !hpsa_allow_any) {
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05005640 dev_warn(&pdev->dev, "unrecognized board ID: "
5641 "0x%08x, ignoring.\n", *board_id);
5642 return -ENODEV;
5643 }
5644 return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
5645}
5646
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005647static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
5648 unsigned long *memory_bar)
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005649{
5650 int i;
5651
5652 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05005653 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005654 /* addressing mode bits already removed */
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05005655 *memory_bar = pci_resource_start(pdev, i);
5656 dev_dbg(&pdev->dev, "memory BAR = %lx\n",
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005657 *memory_bar);
5658 return 0;
5659 }
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05005660 dev_warn(&pdev->dev, "no memory BAR found\n");
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005661 return -ENODEV;
5662}
5663
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005664static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
5665 int wait_for_ready)
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005666{
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005667 int i, iterations;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005668 u32 scratchpad;
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005669 if (wait_for_ready)
5670 iterations = HPSA_BOARD_READY_ITERATIONS;
5671 else
5672 iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005673
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005674 for (i = 0; i < iterations; i++) {
5675 scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
5676 if (wait_for_ready) {
5677 if (scratchpad == HPSA_FIRMWARE_READY)
5678 return 0;
5679 } else {
5680 if (scratchpad != HPSA_FIRMWARE_READY)
5681 return 0;
5682 }
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005683 msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
5684 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005685 dev_warn(&pdev->dev, "board not ready, timed out.\n");
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005686 return -ENODEV;
5687}
5688
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005689static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
5690 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
5691 u64 *cfg_offset)
Stephen M. Camerona51fd472010-06-16 13:51:30 -05005692{
5693 *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
5694 *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
5695 *cfg_base_addr &= (u32) 0x0000ffff;
5696 *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
5697 if (*cfg_base_addr_index == -1) {
5698 dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
5699 return -ENODEV;
5700 }
5701 return 0;
5702}
5703
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005704static int hpsa_find_cfgtables(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005705{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06005706 u64 cfg_offset;
5707 u32 cfg_base_addr;
5708 u64 cfg_base_addr_index;
Don Brace303932f2010-02-04 08:42:40 -06005709 u32 trans_offset;
Stephen M. Camerona51fd472010-06-16 13:51:30 -05005710 int rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005711
Stephen M. Camerona51fd472010-06-16 13:51:30 -05005712 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
5713 &cfg_base_addr_index, &cfg_offset);
5714 if (rc)
5715 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005716 h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
Stephen M. Camerona51fd472010-06-16 13:51:30 -05005717 cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005718 if (!h->cfgtable)
5719 return -ENOMEM;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005720 rc = write_driver_ver_to_cfgtable(h->cfgtable);
5721 if (rc)
5722 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005723 /* Find performant mode table. */
Stephen M. Camerona51fd472010-06-16 13:51:30 -05005724 trans_offset = readl(&h->cfgtable->TransMethodOffset);
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005725 h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
5726 cfg_base_addr_index)+cfg_offset+trans_offset,
5727 sizeof(*h->transtable));
5728 if (!h->transtable)
5729 return -ENOMEM;
5730 return 0;
5731}
5732
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005733static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05005734{
5735 h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
Stephen M. Cameron72ceeae2011-01-06 14:48:13 -06005736
5737 /* Limit commands in memory limited kdump scenario. */
5738 if (reset_devices && h->max_commands > 32)
5739 h->max_commands = 32;
5740
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05005741 if (h->max_commands < 16) {
5742 dev_warn(&h->pdev->dev, "Controller reports "
5743 "max supported commands of %d, an obvious lie. "
5744 "Using 16. Ensure that firmware is up to date.\n",
5745 h->max_commands);
5746 h->max_commands = 16;
5747 }
5748}
5749
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005750/* Interrogate the hardware for some limits:
5751 * max commands, max SG elements without chaining, and with chaining,
5752 * SG chain block size, etc.
5753 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005754static void hpsa_find_board_params(struct ctlr_info *h)
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005755{
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05005756 hpsa_get_max_perf_mode_cmds(h);
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005757 h->nr_cmds = h->max_commands - 4; /* Allow room for some ioctls */
5758 h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005759 h->fw_support = readl(&(h->cfgtable->misc_fw_support));
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005760 /*
5761 * Limit in-command s/g elements to 32 save dma'able memory.
5762 * Howvever spec says if 0, use 31
5763 */
5764 h->max_cmd_sg_entries = 31;
5765 if (h->maxsgentries > 512) {
5766 h->max_cmd_sg_entries = 32;
5767 h->chainsize = h->maxsgentries - h->max_cmd_sg_entries + 1;
5768 h->maxsgentries--; /* save one for chain pointer */
5769 } else {
5770 h->maxsgentries = 31; /* default to traditional values */
5771 h->chainsize = 0;
5772 }
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005773
5774 /* Find out what task management functions are supported and cache */
5775 h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
Scott Teel0e7a7fc2014-02-18 13:55:59 -06005776 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
5777 dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
5778 if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
5779 dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005780}
5781
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05005782static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
5783{
Akinobu Mita0fc9fd42012-04-04 22:14:59 +09005784 if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05005785 dev_warn(&h->pdev->dev, "not a valid CISS config table\n");
5786 return false;
5787 }
5788 return true;
5789}
5790
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005791static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005792{
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005793 u32 driver_support;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005794
Stephen M. Cameron28e13442013-12-04 17:10:21 -06005795#ifdef CONFIG_X86
5796 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005797 driver_support = readl(&(h->cfgtable->driver_support));
5798 driver_support |= ENABLE_SCSI_PREFETCH;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005799#endif
Stephen M. Cameron28e13442013-12-04 17:10:21 -06005800 driver_support |= ENABLE_UNIT_ATTN;
5801 writel(driver_support, &(h->cfgtable->driver_support));
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005802}
5803
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05005804/* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result
5805 * in a prefetch beyond physical memory.
5806 */
5807static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
5808{
5809 u32 dma_prefetch;
5810
5811 if (h->board_id != 0x3225103C)
5812 return;
5813 dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
5814 dma_prefetch |= 0x8000;
5815 writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
5816}
5817
Stephen M. Cameron76438d02014-02-18 13:55:43 -06005818static void hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
5819{
5820 int i;
5821 u32 doorbell_value;
5822 unsigned long flags;
5823 /* wait until the clear_event_notify bit 6 is cleared by controller. */
5824 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
5825 spin_lock_irqsave(&h->lock, flags);
5826 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
5827 spin_unlock_irqrestore(&h->lock, flags);
5828 if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
5829 break;
5830 /* delay and try again */
5831 msleep(20);
5832 }
5833}
5834
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005835static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005836{
5837 int i;
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06005838 u32 doorbell_value;
5839 unsigned long flags;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005840
5841 /* under certain very rare conditions, this can take awhile.
5842 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
5843 * as we enter this code.)
5844 */
5845 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06005846 spin_lock_irqsave(&h->lock, flags);
5847 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
5848 spin_unlock_irqrestore(&h->lock, flags);
Dan Carpenter382be662011-02-15 15:33:13 -06005849 if (!(doorbell_value & CFGTBL_ChangeReq))
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005850 break;
5851 /* delay and try again */
Stephen M. Cameron60d3f5b2011-01-06 14:48:34 -06005852 usleep_range(10000, 20000);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005853 }
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005854}
5855
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005856static int hpsa_enter_simple_mode(struct ctlr_info *h)
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005857{
5858 u32 trans_support;
5859
5860 trans_support = readl(&(h->cfgtable->TransportSupport));
5861 if (!(trans_support & SIMPLE_MODE))
5862 return -ENOTSUPP;
5863
5864 h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005865
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005866 /* Update the field, and then ring the doorbell */
5867 writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06005868 writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005869 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
5870 hpsa_wait_for_mode_change_ack(h);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005871 print_cfg_table(&h->pdev->dev, h->cfgtable);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005872 if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
5873 goto error;
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06005874 h->transMethod = CFGTBL_Trans_Simple;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005875 return 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005876error:
5877 dev_warn(&h->pdev->dev, "unable to get board into simple mode\n");
5878 return -ENODEV;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005879}
5880
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005881static int hpsa_pci_init(struct ctlr_info *h)
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005882{
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005883 int prod_index, err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005884
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05005885 prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
5886 if (prod_index < 0)
5887 return -ENODEV;
5888 h->product_name = products[prod_index].product_name;
5889 h->access = *(products[prod_index].access);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005890
Matthew Garrette5a44df2011-11-11 11:14:23 -05005891 pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
5892 PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
5893
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005894 err = pci_enable_device(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005895 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005896 dev_warn(&h->pdev->dev, "unable to enable PCI device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005897 return err;
5898 }
5899
Stephen M. Cameron5cb460a2012-05-01 11:42:20 -05005900 /* Enable bus mastering (pci_disable_device may disable this) */
5901 pci_set_master(h->pdev);
5902
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06005903 err = pci_request_regions(h->pdev, HPSA);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005904 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005905 dev_err(&h->pdev->dev,
5906 "cannot obtain PCI resources, aborting\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005907 return err;
5908 }
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05005909 hpsa_interrupt_mode(h);
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05005910 err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005911 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005912 goto err_out_free_res;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005913 h->vaddr = remap_pci_mem(h->paddr, 0x250);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05005914 if (!h->vaddr) {
5915 err = -ENOMEM;
5916 goto err_out_free_res;
5917 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005918 err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005919 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005920 goto err_out_free_res;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005921 err = hpsa_find_cfgtables(h);
5922 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005923 goto err_out_free_res;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005924 hpsa_find_board_params(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005925
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05005926 if (!hpsa_CISS_signature_present(h)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005927 err = -ENODEV;
5928 goto err_out_free_res;
5929 }
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005930 hpsa_set_driver_support_bits(h);
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05005931 hpsa_p600_dma_prefetch_quirk(h);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005932 err = hpsa_enter_simple_mode(h);
5933 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005934 goto err_out_free_res;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005935 return 0;
5936
5937err_out_free_res:
Stephen M. Cameron204892e2010-05-27 15:13:22 -05005938 if (h->transtable)
5939 iounmap(h->transtable);
5940 if (h->cfgtable)
5941 iounmap(h->cfgtable);
5942 if (h->vaddr)
5943 iounmap(h->vaddr);
Stephen M. Cameronf0bd0b682012-05-01 11:42:09 -05005944 pci_disable_device(h->pdev);
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005945 pci_release_regions(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005946 return err;
5947}
5948
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005949static void hpsa_hba_inquiry(struct ctlr_info *h)
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06005950{
5951 int rc;
5952
5953#define HBA_INQUIRY_BYTE_COUNT 64
5954 h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
5955 if (!h->hba_inquiry_data)
5956 return;
5957 rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
5958 h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
5959 if (rc != 0) {
5960 kfree(h->hba_inquiry_data);
5961 h->hba_inquiry_data = NULL;
5962 }
5963}
5964
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005965static int hpsa_init_reset_devices(struct pci_dev *pdev)
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005966{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005967 int rc, i;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005968
5969 if (!reset_devices)
5970 return 0;
5971
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005972 /* Reset the controller with a PCI power-cycle or via doorbell */
5973 rc = hpsa_kdump_hard_reset_controller(pdev);
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005974
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005975 /* -ENOTSUPP here means we cannot reset the controller
5976 * but it's already (and still) up and running in
Stephen M. Cameron18867652010-06-16 13:51:45 -05005977 * "performant mode". Or, it might be 640x, which can't reset
5978 * due to concerns about shared bbwc between 6402/6404 pair.
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005979 */
5980 if (rc == -ENOTSUPP)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005981 return rc; /* just try to do the kdump anyhow. */
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005982 if (rc)
5983 return -ENODEV;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005984
5985 /* Now try to get the controller to respond to a no-op */
Stephen M. Cameron2b870cb2011-05-03 14:59:36 -05005986 dev_warn(&pdev->dev, "Waiting for controller to respond to no-op\n");
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005987 for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
5988 if (hpsa_noop(pdev) == 0)
5989 break;
5990 else
5991 dev_warn(&pdev->dev, "no-op failed%s\n",
5992 (i < 11 ? "; re-trying" : ""));
5993 }
5994 return 0;
5995}
5996
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005997static int hpsa_allocate_cmd_pool(struct ctlr_info *h)
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05005998{
5999 h->cmd_pool_bits = kzalloc(
6000 DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
6001 sizeof(unsigned long), GFP_KERNEL);
6002 h->cmd_pool = pci_alloc_consistent(h->pdev,
6003 h->nr_cmds * sizeof(*h->cmd_pool),
6004 &(h->cmd_pool_dhandle));
6005 h->errinfo_pool = pci_alloc_consistent(h->pdev,
6006 h->nr_cmds * sizeof(*h->errinfo_pool),
6007 &(h->errinfo_pool_dhandle));
6008 if ((h->cmd_pool_bits == NULL)
6009 || (h->cmd_pool == NULL)
6010 || (h->errinfo_pool == NULL)) {
6011 dev_err(&h->pdev->dev, "out of memory in %s", __func__);
6012 return -ENOMEM;
6013 }
6014 return 0;
6015}
6016
6017static void hpsa_free_cmd_pool(struct ctlr_info *h)
6018{
6019 kfree(h->cmd_pool_bits);
6020 if (h->cmd_pool)
6021 pci_free_consistent(h->pdev,
6022 h->nr_cmds * sizeof(struct CommandList),
6023 h->cmd_pool, h->cmd_pool_dhandle);
Stephen M. Cameronaca90122014-02-18 13:56:14 -06006024 if (h->ioaccel2_cmd_pool)
6025 pci_free_consistent(h->pdev,
6026 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
6027 h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05006028 if (h->errinfo_pool)
6029 pci_free_consistent(h->pdev,
6030 h->nr_cmds * sizeof(struct ErrorInfo),
6031 h->errinfo_pool,
6032 h->errinfo_pool_dhandle);
Matt Gatese1f7de02014-02-18 13:55:17 -06006033 if (h->ioaccel_cmd_pool)
6034 pci_free_consistent(h->pdev,
6035 h->nr_cmds * sizeof(struct io_accel1_cmd),
6036 h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05006037}
6038
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05006039static int hpsa_request_irq(struct ctlr_info *h,
6040 irqreturn_t (*msixhandler)(int, void *),
6041 irqreturn_t (*intxhandler)(int, void *))
6042{
Matt Gates254f7962012-05-01 11:43:06 -05006043 int rc, i;
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05006044
Matt Gates254f7962012-05-01 11:43:06 -05006045 /*
6046 * initialize h->q[x] = x so that interrupt handlers know which
6047 * queue to process.
6048 */
6049 for (i = 0; i < MAX_REPLY_QUEUES; i++)
6050 h->q[i] = (u8) i;
6051
Hannes Reineckeeee0f032014-01-15 13:30:53 +01006052 if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
Matt Gates254f7962012-05-01 11:43:06 -05006053 /* If performant mode and MSI-X, use multiple reply queues */
Hannes Reineckeeee0f032014-01-15 13:30:53 +01006054 for (i = 0; i < h->msix_vector; i++)
Matt Gates254f7962012-05-01 11:43:06 -05006055 rc = request_irq(h->intr[i], msixhandler,
6056 0, h->devname,
6057 &h->q[i]);
6058 } else {
6059 /* Use single reply pool */
Hannes Reineckeeee0f032014-01-15 13:30:53 +01006060 if (h->msix_vector > 0 || h->msi_vector) {
Matt Gates254f7962012-05-01 11:43:06 -05006061 rc = request_irq(h->intr[h->intr_mode],
6062 msixhandler, 0, h->devname,
6063 &h->q[h->intr_mode]);
6064 } else {
6065 rc = request_irq(h->intr[h->intr_mode],
6066 intxhandler, IRQF_SHARED, h->devname,
6067 &h->q[h->intr_mode]);
6068 }
6069 }
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05006070 if (rc) {
6071 dev_err(&h->pdev->dev, "unable to get irq %d for %s\n",
6072 h->intr[h->intr_mode], h->devname);
6073 return -ENODEV;
6074 }
6075 return 0;
6076}
6077
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006078static int hpsa_kdump_soft_reset(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006079{
6080 if (hpsa_send_host_reset(h, RAID_CTLR_LUNID,
6081 HPSA_RESET_TYPE_CONTROLLER)) {
6082 dev_warn(&h->pdev->dev, "Resetting array controller failed.\n");
6083 return -EIO;
6084 }
6085
6086 dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
6087 if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) {
6088 dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
6089 return -1;
6090 }
6091
6092 dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
6093 if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) {
6094 dev_warn(&h->pdev->dev, "Board failed to become ready "
6095 "after soft reset.\n");
6096 return -1;
6097 }
6098
6099 return 0;
6100}
6101
Matt Gates254f7962012-05-01 11:43:06 -05006102static void free_irqs(struct ctlr_info *h)
6103{
6104 int i;
6105
6106 if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
6107 /* Single reply queue, only one irq to free */
6108 i = h->intr_mode;
6109 free_irq(h->intr[i], &h->q[i]);
6110 return;
6111 }
6112
Hannes Reineckeeee0f032014-01-15 13:30:53 +01006113 for (i = 0; i < h->msix_vector; i++)
Matt Gates254f7962012-05-01 11:43:06 -05006114 free_irq(h->intr[i], &h->q[i]);
6115}
6116
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05006117static void hpsa_free_irqs_and_disable_msix(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006118{
Matt Gates254f7962012-05-01 11:43:06 -05006119 free_irqs(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006120#ifdef CONFIG_PCI_MSI
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05006121 if (h->msix_vector) {
6122 if (h->pdev->msix_enabled)
6123 pci_disable_msix(h->pdev);
6124 } else if (h->msi_vector) {
6125 if (h->pdev->msi_enabled)
6126 pci_disable_msi(h->pdev);
6127 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006128#endif /* CONFIG_PCI_MSI */
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05006129}
6130
6131static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
6132{
6133 hpsa_free_irqs_and_disable_msix(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006134 hpsa_free_sg_chain_blocks(h);
6135 hpsa_free_cmd_pool(h);
Matt Gatese1f7de02014-02-18 13:55:17 -06006136 kfree(h->ioaccel1_blockFetchTable);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006137 kfree(h->blockFetchTable);
6138 pci_free_consistent(h->pdev, h->reply_pool_size,
6139 h->reply_pool, h->reply_pool_dhandle);
6140 if (h->vaddr)
6141 iounmap(h->vaddr);
6142 if (h->transtable)
6143 iounmap(h->transtable);
6144 if (h->cfgtable)
6145 iounmap(h->cfgtable);
6146 pci_release_regions(h->pdev);
6147 kfree(h);
6148}
6149
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006150/* Called when controller lockup detected. */
6151static void fail_all_cmds_on_list(struct ctlr_info *h, struct list_head *list)
6152{
6153 struct CommandList *c = NULL;
6154
6155 assert_spin_locked(&h->lock);
6156 /* Mark all outstanding commands as failed and complete them. */
6157 while (!list_empty(list)) {
6158 c = list_entry(list->next, struct CommandList, list);
6159 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05006160 finish_cmd(c);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006161 }
6162}
6163
6164static void controller_lockup_detected(struct ctlr_info *h)
6165{
6166 unsigned long flags;
6167
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006168 h->access.set_intr_mask(h, HPSA_INTR_OFF);
6169 spin_lock_irqsave(&h->lock, flags);
6170 h->lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
6171 spin_unlock_irqrestore(&h->lock, flags);
6172 dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x\n",
6173 h->lockup_detected);
6174 pci_disable_device(h->pdev);
6175 spin_lock_irqsave(&h->lock, flags);
6176 fail_all_cmds_on_list(h, &h->cmpQ);
6177 fail_all_cmds_on_list(h, &h->reqQ);
6178 spin_unlock_irqrestore(&h->lock, flags);
6179}
6180
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006181static void detect_controller_lockup(struct ctlr_info *h)
6182{
6183 u64 now;
6184 u32 heartbeat;
6185 unsigned long flags;
6186
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006187 now = get_jiffies_64();
6188 /* If we've received an interrupt recently, we're ok. */
6189 if (time_after64(h->last_intr_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05006190 (h->heartbeat_sample_interval), now))
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006191 return;
6192
6193 /*
6194 * If we've already checked the heartbeat recently, we're ok.
6195 * This could happen if someone sends us a signal. We
6196 * otherwise don't care about signals in this thread.
6197 */
6198 if (time_after64(h->last_heartbeat_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05006199 (h->heartbeat_sample_interval), now))
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006200 return;
6201
6202 /* If heartbeat has not changed since we last looked, we're not ok. */
6203 spin_lock_irqsave(&h->lock, flags);
6204 heartbeat = readl(&h->cfgtable->HeartBeat);
6205 spin_unlock_irqrestore(&h->lock, flags);
6206 if (h->last_heartbeat == heartbeat) {
6207 controller_lockup_detected(h);
6208 return;
6209 }
6210
6211 /* We're ok. */
6212 h->last_heartbeat = heartbeat;
6213 h->last_heartbeat_timestamp = now;
6214}
6215
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006216static int hpsa_kickoff_rescan(struct ctlr_info *h)
6217{
6218 int i;
6219 char *event_type;
6220
Scott Teele863d682014-02-18 13:57:05 -06006221 /* Clear the driver-requested rescan flag */
6222 h->drv_req_rescan = 0;
6223
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006224 /* Ask the controller to clear the events we're handling. */
Stephen M. Cameron1f7cee82014-02-18 13:56:09 -06006225 if ((h->transMethod & (CFGTBL_Trans_io_accel1
6226 | CFGTBL_Trans_io_accel2)) &&
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006227 (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
6228 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
6229
6230 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
6231 event_type = "state change";
6232 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
6233 event_type = "configuration change";
6234 /* Stop sending new RAID offload reqs via the IO accelerator */
6235 scsi_block_requests(h->scsi_host);
6236 for (i = 0; i < h->ndevices; i++)
6237 h->dev[i]->offload_enabled = 0;
6238 hpsa_drain_commands(h);
6239 /* Set 'accelerator path config change' bit */
6240 dev_warn(&h->pdev->dev,
6241 "Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
6242 h->events, event_type);
6243 writel(h->events, &(h->cfgtable->clear_event_notify));
6244 /* Set the "clear event notify field update" bit 6 */
6245 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
6246 /* Wait until ctlr clears 'clear event notify field', bit 6 */
6247 hpsa_wait_for_clear_event_notify_ack(h);
6248 scsi_unblock_requests(h->scsi_host);
6249 } else {
6250 /* Acknowledge controller notification events. */
6251 writel(h->events, &(h->cfgtable->clear_event_notify));
6252 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
6253 hpsa_wait_for_clear_event_notify_ack(h);
6254#if 0
6255 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
6256 hpsa_wait_for_mode_change_ack(h);
6257#endif
6258 }
6259
6260 /* Something in the device list may have changed to trigger
6261 * the event, so do a rescan.
6262 */
6263 hpsa_scan_start(h->scsi_host);
6264 /* release reference taken on scsi host in check_controller_events */
6265 scsi_host_put(h->scsi_host);
6266 return 0;
6267}
6268
6269/* Check a register on the controller to see if there are configuration
6270 * changes (added/changed/removed logical drives, etc.) which mean that
Scott Teele863d682014-02-18 13:57:05 -06006271 * we should rescan the controller for devices.
6272 * Also check flag for driver-initiated rescan.
6273 * If either flag or controller event indicate rescan, add the controller
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006274 * to the list of controllers needing to be rescanned, and gets a
6275 * reference to the associated scsi_host.
6276 */
6277static void hpsa_ctlr_needs_rescan(struct ctlr_info *h)
6278{
6279 if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
6280 return;
6281
6282 h->events = readl(&(h->cfgtable->event_notify));
Scott Teele863d682014-02-18 13:57:05 -06006283 if (!h->events && !h->drv_req_rescan)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006284 return;
6285
6286 /*
6287 * Take a reference on scsi host for the duration of the scan
6288 * Release in hpsa_kickoff_rescan(). No lock needed for scan_list
6289 * as only a single thread accesses this list.
6290 */
6291 scsi_host_get(h->scsi_host);
6292 hpsa_kickoff_rescan(h);
6293}
6294
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006295static void hpsa_monitor_ctlr_worker(struct work_struct *work)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006296{
6297 unsigned long flags;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006298 struct ctlr_info *h = container_of(to_delayed_work(work),
6299 struct ctlr_info, monitor_ctlr_work);
6300 detect_controller_lockup(h);
6301 if (h->lockup_detected)
6302 return;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006303 hpsa_ctlr_needs_rescan(h);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006304 spin_lock_irqsave(&h->lock, flags);
6305 if (h->remove_in_progress) {
6306 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006307 return;
6308 }
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006309 schedule_delayed_work(&h->monitor_ctlr_work,
6310 h->heartbeat_sample_interval);
6311 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006312}
6313
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006314static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006315{
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006316 int dac, rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006317 struct ctlr_info *h;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006318 int try_soft_reset = 0;
6319 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006320
6321 if (number_of_controllers == 0)
6322 printk(KERN_INFO DRIVER_NAME "\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006323
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006324 rc = hpsa_init_reset_devices(pdev);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006325 if (rc) {
6326 if (rc != -ENOTSUPP)
6327 return rc;
6328 /* If the reset fails in a particular way (it has no way to do
6329 * a proper hard reset, so returns -ENOTSUPP) we can try to do
6330 * a soft reset once we get the controller configured up to the
6331 * point that it can accept a command.
6332 */
6333 try_soft_reset = 1;
6334 rc = 0;
6335 }
6336
6337reinit_after_soft_reset:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006338
Don Brace303932f2010-02-04 08:42:40 -06006339 /* Command structures must be aligned on a 32-byte boundary because
6340 * the 5 lower bits of the address are used by the hardware. and by
6341 * the driver. See comments in hpsa.h for more info.
6342 */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006343#define COMMANDLIST_ALIGNMENT 128
Don Brace303932f2010-02-04 08:42:40 -06006344 BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006345 h = kzalloc(sizeof(*h), GFP_KERNEL);
6346 if (!h)
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006347 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006348
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05006349 h->pdev = pdev;
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06006350 h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06006351 INIT_LIST_HEAD(&h->cmpQ);
6352 INIT_LIST_HEAD(&h->reqQ);
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06006353 spin_lock_init(&h->lock);
6354 spin_lock_init(&h->scan_lock);
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006355 spin_lock_init(&h->passthru_count_lock);
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05006356 rc = hpsa_pci_init(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006357 if (rc != 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006358 goto clean1;
6359
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06006360 sprintf(h->devname, HPSA "%d", number_of_controllers);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006361 h->ctlr = number_of_controllers;
6362 number_of_controllers++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006363
6364 /* configure PCI DMA stuff */
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006365 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
6366 if (rc == 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006367 dac = 1;
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006368 } else {
6369 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6370 if (rc == 0) {
6371 dac = 0;
6372 } else {
6373 dev_err(&pdev->dev, "no suitable DMA available\n");
6374 goto clean1;
6375 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006376 }
6377
6378 /* make sure the board interrupts are off */
6379 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006380
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05006381 if (hpsa_request_irq(h, do_hpsa_intr_msi, do_hpsa_intr_intx))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006382 goto clean2;
Don Brace303932f2010-02-04 08:42:40 -06006383 dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
6384 h->devname, pdev->device,
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06006385 h->intr[h->intr_mode], dac ? "" : " not");
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05006386 if (hpsa_allocate_cmd_pool(h))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006387 goto clean4;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06006388 if (hpsa_allocate_sg_chain_blocks(h))
6389 goto clean4;
Stephen M. Camerona08a8472010-02-04 08:43:16 -06006390 init_waitqueue_head(&h->scan_wait_queue);
6391 h->scan_finished = 1; /* no scan currently in progress */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006392
6393 pci_set_drvdata(pdev, h);
Stephen M. Cameron9a413382011-05-03 14:59:41 -05006394 h->ndevices = 0;
6395 h->scsi_host = NULL;
6396 spin_lock_init(&h->devlock);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006397 hpsa_put_ctlr_into_performant_mode(h);
6398
6399 /* At this point, the controller is ready to take commands.
6400 * Now, if reset_devices and the hard reset didn't work, try
6401 * the soft reset and see if that works.
6402 */
6403 if (try_soft_reset) {
6404
6405 /* This is kind of gross. We may or may not get a completion
6406 * from the soft reset command, and if we do, then the value
6407 * from the fifo may or may not be valid. So, we wait 10 secs
6408 * after the reset throwing away any completions we get during
6409 * that time. Unregister the interrupt handler and register
6410 * fake ones to scoop up any residual completions.
6411 */
6412 spin_lock_irqsave(&h->lock, flags);
6413 h->access.set_intr_mask(h, HPSA_INTR_OFF);
6414 spin_unlock_irqrestore(&h->lock, flags);
Matt Gates254f7962012-05-01 11:43:06 -05006415 free_irqs(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006416 rc = hpsa_request_irq(h, hpsa_msix_discard_completions,
6417 hpsa_intx_discard_completions);
6418 if (rc) {
6419 dev_warn(&h->pdev->dev, "Failed to request_irq after "
6420 "soft reset.\n");
6421 goto clean4;
6422 }
6423
6424 rc = hpsa_kdump_soft_reset(h);
6425 if (rc)
6426 /* Neither hard nor soft reset worked, we're hosed. */
6427 goto clean4;
6428
6429 dev_info(&h->pdev->dev, "Board READY.\n");
6430 dev_info(&h->pdev->dev,
6431 "Waiting for stale completions to drain.\n");
6432 h->access.set_intr_mask(h, HPSA_INTR_ON);
6433 msleep(10000);
6434 h->access.set_intr_mask(h, HPSA_INTR_OFF);
6435
6436 rc = controller_reset_failed(h->cfgtable);
6437 if (rc)
6438 dev_info(&h->pdev->dev,
6439 "Soft reset appears to have failed.\n");
6440
6441 /* since the controller's reset, we have to go back and re-init
6442 * everything. Easiest to just forget what we've done and do it
6443 * all over again.
6444 */
6445 hpsa_undo_allocations_after_kdump_soft_reset(h);
6446 try_soft_reset = 0;
6447 if (rc)
6448 /* don't go to clean4, we already unallocated */
6449 return -ENODEV;
6450
6451 goto reinit_after_soft_reset;
6452 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006453
Scott Teelda0697b2014-02-18 13:57:00 -06006454 /* Enable Accelerated IO path at driver layer */
6455 h->acciopath_status = 1;
6456
Scott Teele863d682014-02-18 13:57:05 -06006457 h->drv_req_rescan = 0;
6458
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006459 /* Turn the interrupts on so we can service requests */
6460 h->access.set_intr_mask(h, HPSA_INTR_ON);
6461
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06006462 hpsa_hba_inquiry(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006463 hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006464
6465 /* Monitor the controller for firmware lockups */
6466 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
6467 INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
6468 schedule_delayed_work(&h->monitor_ctlr_work,
6469 h->heartbeat_sample_interval);
Stephen M. Cameron88bf6d62013-11-01 11:02:25 -05006470 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006471
6472clean4:
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06006473 hpsa_free_sg_chain_blocks(h);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05006474 hpsa_free_cmd_pool(h);
Matt Gates254f7962012-05-01 11:43:06 -05006475 free_irqs(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006476clean2:
6477clean1:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006478 kfree(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006479 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006480}
6481
6482static void hpsa_flush_cache(struct ctlr_info *h)
6483{
6484 char *flush_buf;
6485 struct CommandList *c;
Stephen M. Cameron702890e2013-09-23 13:33:30 -05006486 unsigned long flags;
6487
6488 /* Don't bother trying to flush the cache if locked up */
6489 spin_lock_irqsave(&h->lock, flags);
6490 if (unlikely(h->lockup_detected)) {
6491 spin_unlock_irqrestore(&h->lock, flags);
6492 return;
6493 }
6494 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006495
6496 flush_buf = kzalloc(4, GFP_KERNEL);
6497 if (!flush_buf)
6498 return;
6499
6500 c = cmd_special_alloc(h);
6501 if (!c) {
6502 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
6503 goto out_of_memory;
6504 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006505 if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
6506 RAID_CTLR_LUNID, TYPE_CMD)) {
6507 goto out;
6508 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006509 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
6510 if (c->err_info->CommandStatus != 0)
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006511out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006512 dev_warn(&h->pdev->dev,
6513 "error flushing cache on controller\n");
6514 cmd_special_free(h, c);
6515out_of_memory:
6516 kfree(flush_buf);
6517}
6518
6519static void hpsa_shutdown(struct pci_dev *pdev)
6520{
6521 struct ctlr_info *h;
6522
6523 h = pci_get_drvdata(pdev);
6524 /* Turn board interrupts off and send the flush cache command
6525 * sendcmd will turn off interrupt, and send the flush...
6526 * To write all data in the battery backed cache to disks
6527 */
6528 hpsa_flush_cache(h);
6529 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05006530 hpsa_free_irqs_and_disable_msix(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006531}
6532
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006533static void hpsa_free_device_info(struct ctlr_info *h)
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06006534{
6535 int i;
6536
6537 for (i = 0; i < h->ndevices; i++)
6538 kfree(h->dev[i]);
6539}
6540
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006541static void hpsa_remove_one(struct pci_dev *pdev)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006542{
6543 struct ctlr_info *h;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006544 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006545
6546 if (pci_get_drvdata(pdev) == NULL) {
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006547 dev_err(&pdev->dev, "unable to remove device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006548 return;
6549 }
6550 h = pci_get_drvdata(pdev);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006551
6552 /* Get rid of any controller monitoring work items */
6553 spin_lock_irqsave(&h->lock, flags);
6554 h->remove_in_progress = 1;
6555 cancel_delayed_work(&h->monitor_ctlr_work);
6556 spin_unlock_irqrestore(&h->lock, flags);
6557
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006558 hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */
6559 hpsa_shutdown(pdev);
6560 iounmap(h->vaddr);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05006561 iounmap(h->transtable);
6562 iounmap(h->cfgtable);
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06006563 hpsa_free_device_info(h);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06006564 hpsa_free_sg_chain_blocks(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006565 pci_free_consistent(h->pdev,
6566 h->nr_cmds * sizeof(struct CommandList),
6567 h->cmd_pool, h->cmd_pool_dhandle);
6568 pci_free_consistent(h->pdev,
6569 h->nr_cmds * sizeof(struct ErrorInfo),
6570 h->errinfo_pool, h->errinfo_pool_dhandle);
Don Brace303932f2010-02-04 08:42:40 -06006571 pci_free_consistent(h->pdev, h->reply_pool_size,
6572 h->reply_pool, h->reply_pool_dhandle);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006573 kfree(h->cmd_pool_bits);
Don Brace303932f2010-02-04 08:42:40 -06006574 kfree(h->blockFetchTable);
Matt Gatese1f7de02014-02-18 13:55:17 -06006575 kfree(h->ioaccel1_blockFetchTable);
Stephen M. Cameronaca90122014-02-18 13:56:14 -06006576 kfree(h->ioaccel2_blockFetchTable);
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06006577 kfree(h->hba_inquiry_data);
Stephen M. Cameronf0bd0b682012-05-01 11:42:09 -05006578 pci_disable_device(pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006579 pci_release_regions(pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006580 kfree(h);
6581}
6582
6583static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
6584 __attribute__((unused)) pm_message_t state)
6585{
6586 return -ENOSYS;
6587}
6588
6589static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
6590{
6591 return -ENOSYS;
6592}
6593
6594static struct pci_driver hpsa_pci_driver = {
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06006595 .name = HPSA,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006596 .probe = hpsa_init_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006597 .remove = hpsa_remove_one,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006598 .id_table = hpsa_pci_device_id, /* id_table */
6599 .shutdown = hpsa_shutdown,
6600 .suspend = hpsa_suspend,
6601 .resume = hpsa_resume,
6602};
6603
Don Brace303932f2010-02-04 08:42:40 -06006604/* Fill in bucket_map[], given nsgs (the max number of
6605 * scatter gather elements supported) and bucket[],
6606 * which is an array of 8 integers. The bucket[] array
6607 * contains 8 different DMA transfer sizes (in 16
6608 * byte increments) which the controller uses to fetch
6609 * commands. This function fills in bucket_map[], which
6610 * maps a given number of scatter gather elements to one of
6611 * the 8 DMA transfer sizes. The point of it is to allow the
6612 * controller to only do as much DMA as needed to fetch the
6613 * command, with the DMA transfer size encoded in the lower
6614 * bits of the command address.
6615 */
6616static void calc_bucket_map(int bucket[], int num_buckets,
Matt Gatese1f7de02014-02-18 13:55:17 -06006617 int nsgs, int min_blocks, int *bucket_map)
Don Brace303932f2010-02-04 08:42:40 -06006618{
6619 int i, j, b, size;
6620
Don Brace303932f2010-02-04 08:42:40 -06006621 /* Note, bucket_map must have nsgs+1 entries. */
6622 for (i = 0; i <= nsgs; i++) {
6623 /* Compute size of a command with i SG entries */
Matt Gatese1f7de02014-02-18 13:55:17 -06006624 size = i + min_blocks;
Don Brace303932f2010-02-04 08:42:40 -06006625 b = num_buckets; /* Assume the biggest bucket */
6626 /* Find the bucket that is just big enough */
Matt Gatese1f7de02014-02-18 13:55:17 -06006627 for (j = 0; j < num_buckets; j++) {
Don Brace303932f2010-02-04 08:42:40 -06006628 if (bucket[j] >= size) {
6629 b = j;
6630 break;
6631 }
6632 }
6633 /* for a command with i SG entries, use bucket b. */
6634 bucket_map[i] = b;
6635 }
6636}
6637
Matt Gatese1f7de02014-02-18 13:55:17 -06006638static void hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
Don Brace303932f2010-02-04 08:42:40 -06006639{
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006640 int i;
6641 unsigned long register_value;
Matt Gatese1f7de02014-02-18 13:55:17 -06006642 unsigned long transMethod = CFGTBL_Trans_Performant |
6643 (trans_support & CFGTBL_Trans_use_short_tags) |
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006644 CFGTBL_Trans_enable_directed_msix |
6645 (trans_support & (CFGTBL_Trans_io_accel1 |
6646 CFGTBL_Trans_io_accel2));
Matt Gatese1f7de02014-02-18 13:55:17 -06006647 struct access_method access = SA5_performant_access;
Stephen M. Camerondef342b2010-05-27 15:14:39 -05006648
6649 /* This is a bit complicated. There are 8 registers on
6650 * the controller which we write to to tell it 8 different
6651 * sizes of commands which there may be. It's a way of
6652 * reducing the DMA done to fetch each command. Encoded into
6653 * each command's tag are 3 bits which communicate to the controller
6654 * which of the eight sizes that command fits within. The size of
6655 * each command depends on how many scatter gather entries there are.
6656 * Each SG entry requires 16 bytes. The eight registers are programmed
6657 * with the number of 16-byte blocks a command of that size requires.
6658 * The smallest command possible requires 5 such 16 byte blocks.
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006659 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
Stephen M. Camerondef342b2010-05-27 15:14:39 -05006660 * blocks. Note, this only extends to the SG entries contained
6661 * within the command block, and does not extend to chained blocks
6662 * of SG elements. bft[] contains the eight values we write to
6663 * the registers. They are not evenly distributed, but have more
6664 * sizes for small commands, and fewer sizes for larger commands.
6665 */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006666 int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006667#define MIN_IOACCEL2_BFT_ENTRY 5
6668#define HPSA_IOACCEL2_HEADER_SZ 4
6669 int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
6670 13, 14, 15, 16, 17, 18, 19,
6671 HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
6672 BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
6673 BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
6674 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
6675 16 * MIN_IOACCEL2_BFT_ENTRY);
6676 BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006677 BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
Don Brace303932f2010-02-04 08:42:40 -06006678 /* 5 = 1 s/g entry or 4k
6679 * 6 = 2 s/g entry or 8k
6680 * 8 = 4 s/g entry or 16k
6681 * 10 = 6 s/g entry or 24k
6682 */
Don Brace303932f2010-02-04 08:42:40 -06006683
Don Brace303932f2010-02-04 08:42:40 -06006684 /* Controller spec: zero out this buffer. */
6685 memset(h->reply_pool, 0, h->reply_pool_size);
Don Brace303932f2010-02-04 08:42:40 -06006686
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006687 bft[7] = SG_ENTRIES_IN_CMD + 4;
6688 calc_bucket_map(bft, ARRAY_SIZE(bft),
Matt Gatese1f7de02014-02-18 13:55:17 -06006689 SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
Don Brace303932f2010-02-04 08:42:40 -06006690 for (i = 0; i < 8; i++)
6691 writel(bft[i], &h->transtable->BlockFetch[i]);
6692
6693 /* size of controller ring buffer */
6694 writel(h->max_commands, &h->transtable->RepQSize);
Matt Gates254f7962012-05-01 11:43:06 -05006695 writel(h->nreply_queues, &h->transtable->RepQCount);
Don Brace303932f2010-02-04 08:42:40 -06006696 writel(0, &h->transtable->RepQCtrAddrLow32);
6697 writel(0, &h->transtable->RepQCtrAddrHigh32);
Matt Gates254f7962012-05-01 11:43:06 -05006698
6699 for (i = 0; i < h->nreply_queues; i++) {
6700 writel(0, &h->transtable->RepQAddr[i].upper);
6701 writel(h->reply_pool_dhandle +
6702 (h->max_commands * sizeof(u64) * i),
6703 &h->transtable->RepQAddr[i].lower);
6704 }
6705
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006706 writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
Matt Gatese1f7de02014-02-18 13:55:17 -06006707 writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
6708 /*
6709 * enable outbound interrupt coalescing in accelerator mode;
6710 */
6711 if (trans_support & CFGTBL_Trans_io_accel1) {
6712 access = SA5_ioaccel_mode1_access;
6713 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
6714 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
Scott Teelc3497752014-02-18 13:56:34 -06006715 } else {
6716 if (trans_support & CFGTBL_Trans_io_accel2) {
6717 access = SA5_ioaccel_mode2_access;
6718 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
6719 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
6720 }
Matt Gatese1f7de02014-02-18 13:55:17 -06006721 }
Don Brace303932f2010-02-04 08:42:40 -06006722 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05006723 hpsa_wait_for_mode_change_ack(h);
Don Brace303932f2010-02-04 08:42:40 -06006724 register_value = readl(&(h->cfgtable->TransportActive));
6725 if (!(register_value & CFGTBL_Trans_Performant)) {
6726 dev_warn(&h->pdev->dev, "unable to get board into"
6727 " performant mode\n");
6728 return;
6729 }
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06006730 /* Change the access methods to the performant access methods */
Matt Gatese1f7de02014-02-18 13:55:17 -06006731 h->access = access;
6732 h->transMethod = transMethod;
6733
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006734 if (!((trans_support & CFGTBL_Trans_io_accel1) ||
6735 (trans_support & CFGTBL_Trans_io_accel2)))
Matt Gatese1f7de02014-02-18 13:55:17 -06006736 return;
6737
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006738 if (trans_support & CFGTBL_Trans_io_accel1) {
6739 /* Set up I/O accelerator mode */
6740 for (i = 0; i < h->nreply_queues; i++) {
6741 writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
6742 h->reply_queue[i].current_entry =
6743 readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
6744 }
6745 bft[7] = h->ioaccel_maxsg + 8;
6746 calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
6747 h->ioaccel1_blockFetchTable);
6748
6749 /* initialize all reply queue entries to unused */
6750 memset(h->reply_pool, (u8) IOACCEL_MODE1_REPLY_UNUSED,
6751 h->reply_pool_size);
6752
6753 /* set all the constant fields in the accelerator command
6754 * frames once at init time to save CPU cycles later.
6755 */
6756 for (i = 0; i < h->nr_cmds; i++) {
6757 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
6758
6759 cp->function = IOACCEL1_FUNCTION_SCSIIO;
6760 cp->err_info = (u32) (h->errinfo_pool_dhandle +
6761 (i * sizeof(struct ErrorInfo)));
6762 cp->err_info_len = sizeof(struct ErrorInfo);
6763 cp->sgl_offset = IOACCEL1_SGLOFFSET;
6764 cp->host_context_flags = IOACCEL1_HCFLAGS_CISS_FORMAT;
6765 cp->timeout_sec = 0;
6766 cp->ReplyQueue = 0;
6767 cp->Tag.lower = (i << DIRECT_LOOKUP_SHIFT) |
6768 DIRECT_LOOKUP_BIT;
6769 cp->Tag.upper = 0;
6770 cp->host_addr.lower =
6771 (u32) (h->ioaccel_cmd_pool_dhandle +
6772 (i * sizeof(struct io_accel1_cmd)));
6773 cp->host_addr.upper = 0;
6774 }
6775 } else if (trans_support & CFGTBL_Trans_io_accel2) {
6776 u64 cfg_offset, cfg_base_addr_index;
6777 u32 bft2_offset, cfg_base_addr;
6778 int rc;
6779
6780 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
6781 &cfg_base_addr_index, &cfg_offset);
6782 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
6783 bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
6784 calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
6785 4, h->ioaccel2_blockFetchTable);
6786 bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
6787 BUILD_BUG_ON(offsetof(struct CfgTable,
6788 io_accel_request_size_offset) != 0xb8);
6789 h->ioaccel2_bft2_regs =
6790 remap_pci_mem(pci_resource_start(h->pdev,
6791 cfg_base_addr_index) +
6792 cfg_offset + bft2_offset,
6793 ARRAY_SIZE(bft2) *
6794 sizeof(*h->ioaccel2_bft2_regs));
6795 for (i = 0; i < ARRAY_SIZE(bft2); i++)
6796 writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
Matt Gatese1f7de02014-02-18 13:55:17 -06006797 }
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006798 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
6799 hpsa_wait_for_mode_change_ack(h);
Matt Gatese1f7de02014-02-18 13:55:17 -06006800}
6801
6802static int hpsa_alloc_ioaccel_cmd_and_bft(struct ctlr_info *h)
6803{
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006804 h->ioaccel_maxsg =
6805 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
6806 if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
6807 h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
6808
Matt Gatese1f7de02014-02-18 13:55:17 -06006809 /* Command structures must be aligned on a 128-byte boundary
6810 * because the 7 lower bits of the address are used by the
6811 * hardware.
6812 */
6813#define IOACCEL1_COMMANDLIST_ALIGNMENT 128
6814 BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
6815 IOACCEL1_COMMANDLIST_ALIGNMENT);
6816 h->ioaccel_cmd_pool =
6817 pci_alloc_consistent(h->pdev,
6818 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
6819 &(h->ioaccel_cmd_pool_dhandle));
6820
6821 h->ioaccel1_blockFetchTable =
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006822 kmalloc(((h->ioaccel_maxsg + 1) *
Matt Gatese1f7de02014-02-18 13:55:17 -06006823 sizeof(u32)), GFP_KERNEL);
6824
6825 if ((h->ioaccel_cmd_pool == NULL) ||
6826 (h->ioaccel1_blockFetchTable == NULL))
6827 goto clean_up;
6828
6829 memset(h->ioaccel_cmd_pool, 0,
6830 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
6831 return 0;
6832
6833clean_up:
6834 if (h->ioaccel_cmd_pool)
6835 pci_free_consistent(h->pdev,
6836 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
6837 h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
6838 kfree(h->ioaccel1_blockFetchTable);
6839 return 1;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006840}
6841
Stephen M. Cameronaca90122014-02-18 13:56:14 -06006842static int ioaccel2_alloc_cmds_and_bft(struct ctlr_info *h)
6843{
6844 /* Allocate ioaccel2 mode command blocks and block fetch table */
6845
6846 h->ioaccel_maxsg =
6847 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
6848 if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
6849 h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
6850
6851#define IOACCEL2_COMMANDLIST_ALIGNMENT 128
6852 BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
6853 IOACCEL2_COMMANDLIST_ALIGNMENT);
6854 h->ioaccel2_cmd_pool =
6855 pci_alloc_consistent(h->pdev,
6856 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
6857 &(h->ioaccel2_cmd_pool_dhandle));
6858
6859 h->ioaccel2_blockFetchTable =
6860 kmalloc(((h->ioaccel_maxsg + 1) *
6861 sizeof(u32)), GFP_KERNEL);
6862
6863 if ((h->ioaccel2_cmd_pool == NULL) ||
6864 (h->ioaccel2_blockFetchTable == NULL))
6865 goto clean_up;
6866
6867 memset(h->ioaccel2_cmd_pool, 0,
6868 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
6869 return 0;
6870
6871clean_up:
6872 if (h->ioaccel2_cmd_pool)
6873 pci_free_consistent(h->pdev,
6874 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
6875 h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
6876 kfree(h->ioaccel2_blockFetchTable);
6877 return 1;
6878}
6879
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006880static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006881{
6882 u32 trans_support;
Matt Gatese1f7de02014-02-18 13:55:17 -06006883 unsigned long transMethod = CFGTBL_Trans_Performant |
6884 CFGTBL_Trans_use_short_tags;
Matt Gates254f7962012-05-01 11:43:06 -05006885 int i;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006886
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -06006887 if (hpsa_simple_mode)
6888 return;
6889
Matt Gatese1f7de02014-02-18 13:55:17 -06006890 /* Check for I/O accelerator mode support */
6891 if (trans_support & CFGTBL_Trans_io_accel1) {
6892 transMethod |= CFGTBL_Trans_io_accel1 |
6893 CFGTBL_Trans_enable_directed_msix;
6894 if (hpsa_alloc_ioaccel_cmd_and_bft(h))
6895 goto clean_up;
Stephen M. Cameronaca90122014-02-18 13:56:14 -06006896 } else {
6897 if (trans_support & CFGTBL_Trans_io_accel2) {
6898 transMethod |= CFGTBL_Trans_io_accel2 |
6899 CFGTBL_Trans_enable_directed_msix;
6900 if (ioaccel2_alloc_cmds_and_bft(h))
6901 goto clean_up;
6902 }
Matt Gatese1f7de02014-02-18 13:55:17 -06006903 }
6904
6905 /* TODO, check that this next line h->nreply_queues is correct */
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006906 trans_support = readl(&(h->cfgtable->TransportSupport));
6907 if (!(trans_support & PERFORMANT_MODE))
6908 return;
6909
Hannes Reineckeeee0f032014-01-15 13:30:53 +01006910 h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05006911 hpsa_get_max_perf_mode_cmds(h);
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006912 /* Performant mode ring buffer and supporting data structures */
Matt Gates254f7962012-05-01 11:43:06 -05006913 h->reply_pool_size = h->max_commands * sizeof(u64) * h->nreply_queues;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006914 h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size,
6915 &(h->reply_pool_dhandle));
6916
Matt Gates254f7962012-05-01 11:43:06 -05006917 for (i = 0; i < h->nreply_queues; i++) {
6918 h->reply_queue[i].head = &h->reply_pool[h->max_commands * i];
6919 h->reply_queue[i].size = h->max_commands;
6920 h->reply_queue[i].wraparound = 1; /* spec: init to 1 */
6921 h->reply_queue[i].current_entry = 0;
6922 }
6923
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006924 /* Need a block fetch table for performant mode */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006925 h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006926 sizeof(u32)), GFP_KERNEL);
6927
6928 if ((h->reply_pool == NULL)
6929 || (h->blockFetchTable == NULL))
6930 goto clean_up;
6931
Matt Gatese1f7de02014-02-18 13:55:17 -06006932 hpsa_enter_performant_mode(h, trans_support);
Don Brace303932f2010-02-04 08:42:40 -06006933 return;
6934
6935clean_up:
6936 if (h->reply_pool)
6937 pci_free_consistent(h->pdev, h->reply_pool_size,
6938 h->reply_pool, h->reply_pool_dhandle);
6939 kfree(h->blockFetchTable);
6940}
6941
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006942static void hpsa_drain_commands(struct ctlr_info *h)
6943{
6944 int cmds_out;
6945 unsigned long flags;
6946
6947 do { /* wait for all outstanding commands to drain out */
6948 spin_lock_irqsave(&h->lock, flags);
6949 cmds_out = h->commands_outstanding;
6950 spin_unlock_irqrestore(&h->lock, flags);
6951 if (cmds_out <= 0)
6952 break;
6953 msleep(100);
6954 } while (1);
6955}
6956
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006957/*
6958 * This is it. Register the PCI driver information for the cards we control
6959 * the OS will call our registered routines when it finds one of our cards.
6960 */
6961static int __init hpsa_init(void)
6962{
Mike Miller31468402010-02-25 14:03:12 -06006963 return pci_register_driver(&hpsa_pci_driver);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006964}
6965
6966static void __exit hpsa_cleanup(void)
6967{
6968 pci_unregister_driver(&hpsa_pci_driver);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006969}
6970
Matt Gatese1f7de02014-02-18 13:55:17 -06006971static void __attribute__((unused)) verify_offsets(void)
6972{
6973#define VERIFY_OFFSET(member, offset) \
Mike Millerb66cc252014-02-18 13:56:04 -06006974 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
6975
6976 VERIFY_OFFSET(IU_type, 0);
6977 VERIFY_OFFSET(direction, 1);
6978 VERIFY_OFFSET(reply_queue, 2);
6979 /* VERIFY_OFFSET(reserved1, 3); */
6980 VERIFY_OFFSET(scsi_nexus, 4);
6981 VERIFY_OFFSET(Tag, 8);
6982 VERIFY_OFFSET(cdb, 16);
6983 VERIFY_OFFSET(cciss_lun, 32);
6984 VERIFY_OFFSET(data_len, 40);
6985 VERIFY_OFFSET(cmd_priority_task_attr, 44);
6986 VERIFY_OFFSET(sg_count, 45);
6987 /* VERIFY_OFFSET(reserved3 */
6988 VERIFY_OFFSET(err_ptr, 48);
6989 VERIFY_OFFSET(err_len, 56);
6990 /* VERIFY_OFFSET(reserved4 */
6991 VERIFY_OFFSET(sg, 64);
6992
6993#undef VERIFY_OFFSET
6994
6995#define VERIFY_OFFSET(member, offset) \
Matt Gatese1f7de02014-02-18 13:55:17 -06006996 BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
6997
6998 VERIFY_OFFSET(dev_handle, 0x00);
6999 VERIFY_OFFSET(reserved1, 0x02);
7000 VERIFY_OFFSET(function, 0x03);
7001 VERIFY_OFFSET(reserved2, 0x04);
7002 VERIFY_OFFSET(err_info, 0x0C);
7003 VERIFY_OFFSET(reserved3, 0x10);
7004 VERIFY_OFFSET(err_info_len, 0x12);
7005 VERIFY_OFFSET(reserved4, 0x13);
7006 VERIFY_OFFSET(sgl_offset, 0x14);
7007 VERIFY_OFFSET(reserved5, 0x15);
7008 VERIFY_OFFSET(transfer_len, 0x1C);
7009 VERIFY_OFFSET(reserved6, 0x20);
7010 VERIFY_OFFSET(io_flags, 0x24);
7011 VERIFY_OFFSET(reserved7, 0x26);
7012 VERIFY_OFFSET(LUN, 0x34);
7013 VERIFY_OFFSET(control, 0x3C);
7014 VERIFY_OFFSET(CDB, 0x40);
7015 VERIFY_OFFSET(reserved8, 0x50);
7016 VERIFY_OFFSET(host_context_flags, 0x60);
7017 VERIFY_OFFSET(timeout_sec, 0x62);
7018 VERIFY_OFFSET(ReplyQueue, 0x64);
7019 VERIFY_OFFSET(reserved9, 0x65);
7020 VERIFY_OFFSET(Tag, 0x68);
7021 VERIFY_OFFSET(host_addr, 0x70);
7022 VERIFY_OFFSET(CISS_LUN, 0x78);
7023 VERIFY_OFFSET(SG, 0x78 + 8);
7024#undef VERIFY_OFFSET
7025}
7026
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007027module_init(hpsa_init);
7028module_exit(hpsa_cleanup);