blob: dab1d7933ea102b6f5c85bd33c5e15e012f936a5 [file] [log] [blame]
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001/*
2 * Disk Array driver for HP Smart Array SAS controllers
3 * Copyright 2000, 2009 Hewlett-Packard Development Company, L.P.
4 *
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. Cameron01a02ff2010-02-04 08:41:33 -0600186 void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800187 int cmd_type);
188
Jeff Garzikf2812332010-11-16 02:10:29 -0500189static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Stephen M. Camerona08a84712010-02-04 08:43:16 -0600190static void hpsa_scan_start(struct Scsi_Host *);
191static int hpsa_scan_finished(struct Scsi_Host *sh,
192 unsigned long elapsed_time);
Stephen M. Cameron667e23d2010-02-25 14:02:51 -0600193static int hpsa_change_queue_depth(struct scsi_device *sdev,
194 int qdepth, int reason);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800195
196static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500197static int hpsa_eh_abort_handler(struct scsi_cmnd *scsicmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800198static int hpsa_slave_alloc(struct scsi_device *sdev);
199static void hpsa_slave_destroy(struct scsi_device *sdev);
200
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800201static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800202static int check_for_unit_attention(struct ctlr_info *h,
203 struct CommandList *c);
204static void check_ioctl_unit_attention(struct ctlr_info *h,
205 struct CommandList *c);
Don Brace303932f2010-02-04 08:42:40 -0600206/* performant mode helper functions */
207static void calc_bucket_map(int *bucket, int num_buckets,
Matt Gatese1f7de02014-02-18 13:55:17 -0600208 int nsgs, int min_blocks, int *bucket_map);
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800209static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
Matt Gates254f7962012-05-01 11:43:06 -0500210static inline u32 next_command(struct ctlr_info *h, u8 q);
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800211static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
212 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
213 u64 *cfg_offset);
214static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
215 unsigned long *memory_bar);
216static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
217static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
218 int wait_for_ready);
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500219static inline void finish_cmd(struct CommandList *c);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -0600220static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h);
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -0600221#define BOARD_NOT_READY 0
222#define BOARD_READY 1
Stephen M. Cameron76438d02014-02-18 13:55:43 -0600223static void hpsa_drain_commands(struct ctlr_info *h);
224static void hpsa_flush_cache(struct ctlr_info *h);
Scott Teelc3497752014-02-18 13:56:34 -0600225static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
226 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
227 u8 *scsi3addr);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800228
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800229static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
230{
231 unsigned long *priv = shost_priv(sdev->host);
232 return (struct ctlr_info *) *priv;
233}
234
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600235static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
236{
237 unsigned long *priv = shost_priv(sh);
238 return (struct ctlr_info *) *priv;
239}
240
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800241static int check_for_unit_attention(struct ctlr_info *h,
242 struct CommandList *c)
243{
244 if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
245 return 0;
246
247 switch (c->err_info->SenseInfo[12]) {
248 case STATE_CHANGED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600249 dev_warn(&h->pdev->dev, HPSA "%d: a state change "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800250 "detected, command retried\n", h->ctlr);
251 break;
252 case LUN_FAILED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600253 dev_warn(&h->pdev->dev, HPSA "%d: LUN failure "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800254 "detected, action required\n", h->ctlr);
255 break;
256 case REPORT_LUNS_CHANGED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600257 dev_warn(&h->pdev->dev, HPSA "%d: report LUN data "
Mike Miller31468402010-02-25 14:03:12 -0600258 "changed, action required\n", h->ctlr);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800259 /*
Scott Teel4f4eb9f2012-01-19 14:01:25 -0600260 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
261 * target (array) devices.
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800262 */
263 break;
264 case POWER_OR_RESET:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600265 dev_warn(&h->pdev->dev, HPSA "%d: a power on "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800266 "or device reset detected\n", h->ctlr);
267 break;
268 case UNIT_ATTENTION_CLEARED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600269 dev_warn(&h->pdev->dev, HPSA "%d: unit attention "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800270 "cleared by another initiator\n", h->ctlr);
271 break;
272 default:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600273 dev_warn(&h->pdev->dev, HPSA "%d: unknown "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800274 "unit attention detected\n", h->ctlr);
275 break;
276 }
277 return 1;
278}
279
Matt Bondurant852af202012-05-01 11:42:35 -0500280static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
281{
282 if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
283 (c->err_info->ScsiStatus != SAM_STAT_BUSY &&
284 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
285 return 0;
286 dev_warn(&h->pdev->dev, HPSA "device busy");
287 return 1;
288}
289
Scott Teelda0697b2014-02-18 13:57:00 -0600290static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
291 struct device_attribute *attr,
292 const char *buf, size_t count)
293{
294 int status, len;
295 struct ctlr_info *h;
296 struct Scsi_Host *shost = class_to_shost(dev);
297 char tmpbuf[10];
298
299 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
300 return -EACCES;
301 len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
302 strncpy(tmpbuf, buf, len);
303 tmpbuf[len] = '\0';
304 if (sscanf(tmpbuf, "%d", &status) != 1)
305 return -EINVAL;
306 h = shost_to_hba(shost);
307 h->acciopath_status = !!status;
308 dev_warn(&h->pdev->dev,
309 "hpsa: HP SSD Smart Path %s via sysfs update.\n",
310 h->acciopath_status ? "enabled" : "disabled");
311 return count;
312}
313
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800314static ssize_t host_store_rescan(struct device *dev,
315 struct device_attribute *attr,
316 const char *buf, size_t count)
317{
318 struct ctlr_info *h;
319 struct Scsi_Host *shost = class_to_shost(dev);
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600320 h = shost_to_hba(shost);
Mike Miller31468402010-02-25 14:03:12 -0600321 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800322 return count;
323}
324
Stephen M. Camerond28ce022010-05-27 15:14:34 -0500325static ssize_t host_show_firmware_revision(struct device *dev,
326 struct device_attribute *attr, char *buf)
327{
328 struct ctlr_info *h;
329 struct Scsi_Host *shost = class_to_shost(dev);
330 unsigned char *fwrev;
331
332 h = shost_to_hba(shost);
333 if (!h->hba_inquiry_data)
334 return 0;
335 fwrev = &h->hba_inquiry_data[32];
336 return snprintf(buf, 20, "%c%c%c%c\n",
337 fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
338}
339
Stephen M. Cameron94a13642011-01-06 14:48:39 -0600340static ssize_t host_show_commands_outstanding(struct device *dev,
341 struct device_attribute *attr, char *buf)
342{
343 struct Scsi_Host *shost = class_to_shost(dev);
344 struct ctlr_info *h = shost_to_hba(shost);
345
346 return snprintf(buf, 20, "%d\n", h->commands_outstanding);
347}
348
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600349static ssize_t host_show_transport_mode(struct device *dev,
350 struct device_attribute *attr, char *buf)
351{
352 struct ctlr_info *h;
353 struct Scsi_Host *shost = class_to_shost(dev);
354
355 h = shost_to_hba(shost);
356 return snprintf(buf, 20, "%s\n",
Stephen M. Cameron960a30e2011-02-15 15:33:03 -0600357 h->transMethod & CFGTBL_Trans_Performant ?
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600358 "performant" : "simple");
359}
360
Scott Teelda0697b2014-02-18 13:57:00 -0600361static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
362 struct device_attribute *attr, char *buf)
363{
364 struct ctlr_info *h;
365 struct Scsi_Host *shost = class_to_shost(dev);
366
367 h = shost_to_hba(shost);
368 return snprintf(buf, 30, "HP SSD Smart Path %s\n",
369 (h->acciopath_status == 1) ? "enabled" : "disabled");
370}
371
Stephen M. Cameron46380782011-05-03 15:00:01 -0500372/* List of controllers which cannot be hard reset on kexec with reset_devices */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600373static u32 unresettable_controller[] = {
374 0x324a103C, /* Smart Array P712m */
375 0x324b103C, /* SmartArray P711m */
376 0x3223103C, /* Smart Array P800 */
377 0x3234103C, /* Smart Array P400 */
378 0x3235103C, /* Smart Array P400i */
379 0x3211103C, /* Smart Array E200i */
380 0x3212103C, /* Smart Array E200 */
381 0x3213103C, /* Smart Array E200i */
382 0x3214103C, /* Smart Array E200i */
383 0x3215103C, /* Smart Array E200i */
384 0x3237103C, /* Smart Array E500 */
385 0x323D103C, /* Smart Array P700m */
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100386 0x40800E11, /* Smart Array 5i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600387 0x409C0E11, /* Smart Array 6400 */
388 0x409D0E11, /* Smart Array 6400 EM */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100389 0x40700E11, /* Smart Array 5300 */
390 0x40820E11, /* Smart Array 532 */
391 0x40830E11, /* Smart Array 5312 */
392 0x409A0E11, /* Smart Array 641 */
393 0x409B0E11, /* Smart Array 642 */
394 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600395};
396
Stephen M. Cameron46380782011-05-03 15:00:01 -0500397/* List of controllers which cannot even be soft reset */
398static u32 soft_unresettable_controller[] = {
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100399 0x40800E11, /* Smart Array 5i */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100400 0x40700E11, /* Smart Array 5300 */
401 0x40820E11, /* Smart Array 532 */
402 0x40830E11, /* Smart Array 5312 */
403 0x409A0E11, /* Smart Array 641 */
404 0x409B0E11, /* Smart Array 642 */
405 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron46380782011-05-03 15:00:01 -0500406 /* Exclude 640x boards. These are two pci devices in one slot
407 * which share a battery backed cache module. One controls the
408 * cache, the other accesses the cache through the one that controls
409 * it. If we reset the one controlling the cache, the other will
410 * likely not be happy. Just forbid resetting this conjoined mess.
411 * The 640x isn't really supported by hpsa anyway.
412 */
413 0x409C0E11, /* Smart Array 6400 */
414 0x409D0E11, /* Smart Array 6400 EM */
415};
416
417static int ctlr_is_hard_resettable(u32 board_id)
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600418{
419 int i;
420
421 for (i = 0; i < ARRAY_SIZE(unresettable_controller); i++)
Stephen M. Cameron46380782011-05-03 15:00:01 -0500422 if (unresettable_controller[i] == board_id)
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600423 return 0;
424 return 1;
425}
426
Stephen M. Cameron46380782011-05-03 15:00:01 -0500427static int ctlr_is_soft_resettable(u32 board_id)
428{
429 int i;
430
431 for (i = 0; i < ARRAY_SIZE(soft_unresettable_controller); i++)
432 if (soft_unresettable_controller[i] == board_id)
433 return 0;
434 return 1;
435}
436
437static int ctlr_is_resettable(u32 board_id)
438{
439 return ctlr_is_hard_resettable(board_id) ||
440 ctlr_is_soft_resettable(board_id);
441}
442
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600443static ssize_t host_show_resettable(struct device *dev,
444 struct device_attribute *attr, char *buf)
445{
446 struct ctlr_info *h;
447 struct Scsi_Host *shost = class_to_shost(dev);
448
449 h = shost_to_hba(shost);
Stephen M. Cameron46380782011-05-03 15:00:01 -0500450 return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600451}
452
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800453static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
454{
455 return (scsi3addr[3] & 0xC0) == 0x40;
456}
457
458static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
Mike Millerd82357e2012-05-01 11:43:32 -0500459 "1(ADM)", "UNKNOWN"
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800460};
Scott Teel6b80b182014-02-18 13:56:55 -0600461#define HPSA_RAID_0 0
462#define HPSA_RAID_4 1
463#define HPSA_RAID_1 2 /* also used for RAID 10 */
464#define HPSA_RAID_5 3 /* also used for RAID 50 */
465#define HPSA_RAID_51 4
466#define HPSA_RAID_6 5 /* also used for RAID 60 */
467#define HPSA_RAID_ADM 6 /* also used for RAID 1+0 ADM */
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800468#define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
469
470static ssize_t raid_level_show(struct device *dev,
471 struct device_attribute *attr, char *buf)
472{
473 ssize_t l = 0;
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600474 unsigned char rlevel;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800475 struct ctlr_info *h;
476 struct scsi_device *sdev;
477 struct hpsa_scsi_dev_t *hdev;
478 unsigned long flags;
479
480 sdev = to_scsi_device(dev);
481 h = sdev_to_hba(sdev);
482 spin_lock_irqsave(&h->lock, flags);
483 hdev = sdev->hostdata;
484 if (!hdev) {
485 spin_unlock_irqrestore(&h->lock, flags);
486 return -ENODEV;
487 }
488
489 /* Is this even a logical drive? */
490 if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
491 spin_unlock_irqrestore(&h->lock, flags);
492 l = snprintf(buf, PAGE_SIZE, "N/A\n");
493 return l;
494 }
495
496 rlevel = hdev->raid_level;
497 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600498 if (rlevel > RAID_UNKNOWN)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800499 rlevel = RAID_UNKNOWN;
500 l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
501 return l;
502}
503
504static ssize_t lunid_show(struct device *dev,
505 struct device_attribute *attr, char *buf)
506{
507 struct ctlr_info *h;
508 struct scsi_device *sdev;
509 struct hpsa_scsi_dev_t *hdev;
510 unsigned long flags;
511 unsigned char lunid[8];
512
513 sdev = to_scsi_device(dev);
514 h = sdev_to_hba(sdev);
515 spin_lock_irqsave(&h->lock, flags);
516 hdev = sdev->hostdata;
517 if (!hdev) {
518 spin_unlock_irqrestore(&h->lock, flags);
519 return -ENODEV;
520 }
521 memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
522 spin_unlock_irqrestore(&h->lock, flags);
523 return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
524 lunid[0], lunid[1], lunid[2], lunid[3],
525 lunid[4], lunid[5], lunid[6], lunid[7]);
526}
527
528static ssize_t unique_id_show(struct device *dev,
529 struct device_attribute *attr, char *buf)
530{
531 struct ctlr_info *h;
532 struct scsi_device *sdev;
533 struct hpsa_scsi_dev_t *hdev;
534 unsigned long flags;
535 unsigned char sn[16];
536
537 sdev = to_scsi_device(dev);
538 h = sdev_to_hba(sdev);
539 spin_lock_irqsave(&h->lock, flags);
540 hdev = sdev->hostdata;
541 if (!hdev) {
542 spin_unlock_irqrestore(&h->lock, flags);
543 return -ENODEV;
544 }
545 memcpy(sn, hdev->device_id, sizeof(sn));
546 spin_unlock_irqrestore(&h->lock, flags);
547 return snprintf(buf, 16 * 2 + 2,
548 "%02X%02X%02X%02X%02X%02X%02X%02X"
549 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
550 sn[0], sn[1], sn[2], sn[3],
551 sn[4], sn[5], sn[6], sn[7],
552 sn[8], sn[9], sn[10], sn[11],
553 sn[12], sn[13], sn[14], sn[15]);
554}
555
Scott Teelc1988682014-02-18 13:55:54 -0600556static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
557 struct device_attribute *attr, char *buf)
558{
559 struct ctlr_info *h;
560 struct scsi_device *sdev;
561 struct hpsa_scsi_dev_t *hdev;
562 unsigned long flags;
563 int offload_enabled;
564
565 sdev = to_scsi_device(dev);
566 h = sdev_to_hba(sdev);
567 spin_lock_irqsave(&h->lock, flags);
568 hdev = sdev->hostdata;
569 if (!hdev) {
570 spin_unlock_irqrestore(&h->lock, flags);
571 return -ENODEV;
572 }
573 offload_enabled = hdev->offload_enabled;
574 spin_unlock_irqrestore(&h->lock, flags);
575 return snprintf(buf, 20, "%d\n", offload_enabled);
576}
577
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600578static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
579static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
580static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
581static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
Scott Teelc1988682014-02-18 13:55:54 -0600582static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
583 host_show_hp_ssd_smart_path_enabled, NULL);
Scott Teelda0697b2014-02-18 13:57:00 -0600584static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
585 host_show_hp_ssd_smart_path_status,
586 host_store_hp_ssd_smart_path_status);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600587static DEVICE_ATTR(firmware_revision, S_IRUGO,
588 host_show_firmware_revision, NULL);
589static DEVICE_ATTR(commands_outstanding, S_IRUGO,
590 host_show_commands_outstanding, NULL);
591static DEVICE_ATTR(transport_mode, S_IRUGO,
592 host_show_transport_mode, NULL);
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600593static DEVICE_ATTR(resettable, S_IRUGO,
594 host_show_resettable, NULL);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600595
596static struct device_attribute *hpsa_sdev_attrs[] = {
597 &dev_attr_raid_level,
598 &dev_attr_lunid,
599 &dev_attr_unique_id,
Scott Teelc1988682014-02-18 13:55:54 -0600600 &dev_attr_hp_ssd_smart_path_enabled,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600601 NULL,
602};
603
604static struct device_attribute *hpsa_shost_attrs[] = {
605 &dev_attr_rescan,
606 &dev_attr_firmware_revision,
607 &dev_attr_commands_outstanding,
608 &dev_attr_transport_mode,
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600609 &dev_attr_resettable,
Scott Teelda0697b2014-02-18 13:57:00 -0600610 &dev_attr_hp_ssd_smart_path_status,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600611 NULL,
612};
613
614static struct scsi_host_template hpsa_driver_template = {
615 .module = THIS_MODULE,
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600616 .name = HPSA,
617 .proc_name = HPSA,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600618 .queuecommand = hpsa_scsi_queue_command,
619 .scan_start = hpsa_scan_start,
620 .scan_finished = hpsa_scan_finished,
621 .change_queue_depth = hpsa_change_queue_depth,
622 .this_id = -1,
623 .use_clustering = ENABLE_CLUSTERING,
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500624 .eh_abort_handler = hpsa_eh_abort_handler,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600625 .eh_device_reset_handler = hpsa_eh_device_reset_handler,
626 .ioctl = hpsa_ioctl,
627 .slave_alloc = hpsa_slave_alloc,
628 .slave_destroy = hpsa_slave_destroy,
629#ifdef CONFIG_COMPAT
630 .compat_ioctl = hpsa_compat_ioctl,
631#endif
632 .sdev_attrs = hpsa_sdev_attrs,
633 .shost_attrs = hpsa_shost_attrs,
Stephen M. Cameronc0d6a4d2011-10-26 16:20:53 -0500634 .max_sectors = 8192,
Martin K. Petersen54b2b502013-10-23 06:25:40 -0400635 .no_write_same = 1,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600636};
637
638
639/* Enqueuing and dequeuing functions for cmdlists. */
640static inline void addQ(struct list_head *list, struct CommandList *c)
641{
642 list_add_tail(&c->list, list);
643}
644
Matt Gates254f7962012-05-01 11:43:06 -0500645static inline u32 next_command(struct ctlr_info *h, u8 q)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600646{
647 u32 a;
Matt Gates254f7962012-05-01 11:43:06 -0500648 struct reply_pool *rq = &h->reply_queue[q];
Matt Gatese16a33a2012-05-01 11:43:11 -0500649 unsigned long flags;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600650
Matt Gatese1f7de02014-02-18 13:55:17 -0600651 if (h->transMethod & CFGTBL_Trans_io_accel1)
652 return h->access.command_completed(h, q);
653
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600654 if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
Matt Gates254f7962012-05-01 11:43:06 -0500655 return h->access.command_completed(h, q);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600656
Matt Gates254f7962012-05-01 11:43:06 -0500657 if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
658 a = rq->head[rq->current_entry];
659 rq->current_entry++;
Matt Gatese16a33a2012-05-01 11:43:11 -0500660 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600661 h->commands_outstanding--;
Matt Gatese16a33a2012-05-01 11:43:11 -0500662 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600663 } else {
664 a = FIFO_EMPTY;
665 }
666 /* Check for wraparound */
Matt Gates254f7962012-05-01 11:43:06 -0500667 if (rq->current_entry == h->max_commands) {
668 rq->current_entry = 0;
669 rq->wraparound ^= 1;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600670 }
671 return a;
672}
673
Scott Teelc3497752014-02-18 13:56:34 -0600674/*
675 * There are some special bits in the bus address of the
676 * command that we have to set for the controller to know
677 * how to process the command:
678 *
679 * Normal performant mode:
680 * bit 0: 1 means performant mode, 0 means simple mode.
681 * bits 1-3 = block fetch table entry
682 * bits 4-6 = command type (== 0)
683 *
684 * ioaccel1 mode:
685 * bit 0 = "performant mode" bit.
686 * bits 1-3 = block fetch table entry
687 * bits 4-6 = command type (== 110)
688 * (command type is needed because ioaccel1 mode
689 * commands are submitted through the same register as normal
690 * mode commands, so this is how the controller knows whether
691 * the command is normal mode or ioaccel1 mode.)
692 *
693 * ioaccel2 mode:
694 * bit 0 = "performant mode" bit.
695 * bits 1-4 = block fetch table entry (note extra bit)
696 * bits 4-6 = not needed, because ioaccel2 mode has
697 * a separate special register for submitting commands.
698 */
699
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600700/* set_performant_mode: Modify the tag for cciss performant
701 * set bit 0 for pull model, bits 3-1 for block fetch
702 * register number
703 */
704static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
705{
Matt Gates254f7962012-05-01 11:43:06 -0500706 if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600707 c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
Hannes Reineckeeee0f032014-01-15 13:30:53 +0100708 if (likely(h->msix_vector > 0))
Matt Gates254f7962012-05-01 11:43:06 -0500709 c->Header.ReplyQueue =
John Kacur804a5cb2013-07-26 16:06:18 +0200710 raw_smp_processor_id() % h->nreply_queues;
Matt Gates254f7962012-05-01 11:43:06 -0500711 }
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600712}
713
Scott Teelc3497752014-02-18 13:56:34 -0600714static void set_ioaccel1_performant_mode(struct ctlr_info *h,
715 struct CommandList *c)
716{
717 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
718
719 /* Tell the controller to post the reply to the queue for this
720 * processor. This seems to give the best I/O throughput.
721 */
722 cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
723 /* Set the bits in the address sent down to include:
724 * - performant mode bit (bit 0)
725 * - pull count (bits 1-3)
726 * - command type (bits 4-6)
727 */
728 c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
729 IOACCEL1_BUSADDR_CMDTYPE;
730}
731
732static void set_ioaccel2_performant_mode(struct ctlr_info *h,
733 struct CommandList *c)
734{
735 struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
736
737 /* Tell the controller to post the reply to the queue for this
738 * processor. This seems to give the best I/O throughput.
739 */
740 cp->reply_queue = smp_processor_id() % h->nreply_queues;
741 /* Set the bits in the address sent down to include:
742 * - performant mode bit not used in ioaccel mode 2
743 * - pull count (bits 0-3)
744 * - command type isn't needed for ioaccel2
745 */
746 c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
747}
748
Stephen M. Camerone85c5972012-05-01 11:43:42 -0500749static int is_firmware_flash_cmd(u8 *cdb)
750{
751 return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
752}
753
754/*
755 * During firmware flash, the heartbeat register may not update as frequently
756 * as it should. So we dial down lockup detection during firmware flash. and
757 * dial it back up when firmware flash completes.
758 */
759#define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
760#define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
761static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
762 struct CommandList *c)
763{
764 if (!is_firmware_flash_cmd(c->Request.CDB))
765 return;
766 atomic_inc(&h->firmware_flash_in_progress);
767 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
768}
769
770static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
771 struct CommandList *c)
772{
773 if (is_firmware_flash_cmd(c->Request.CDB) &&
774 atomic_dec_and_test(&h->firmware_flash_in_progress))
775 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
776}
777
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600778static void enqueue_cmd_and_start_io(struct ctlr_info *h,
779 struct CommandList *c)
780{
781 unsigned long flags;
782
Scott Teelc3497752014-02-18 13:56:34 -0600783 switch (c->cmd_type) {
784 case CMD_IOACCEL1:
785 set_ioaccel1_performant_mode(h, c);
786 break;
787 case CMD_IOACCEL2:
788 set_ioaccel2_performant_mode(h, c);
789 break;
790 default:
791 set_performant_mode(h, c);
792 }
Stephen M. Camerone85c5972012-05-01 11:43:42 -0500793 dial_down_lockup_detection_during_fw_flash(h, c);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600794 spin_lock_irqsave(&h->lock, flags);
795 addQ(&h->reqQ, c);
796 h->Qdepth++;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600797 spin_unlock_irqrestore(&h->lock, flags);
Matt Gatese16a33a2012-05-01 11:43:11 -0500798 start_io(h);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600799}
800
801static inline void removeQ(struct CommandList *c)
802{
803 if (WARN_ON(list_empty(&c->list)))
804 return;
805 list_del_init(&c->list);
806}
807
808static inline int is_hba_lunid(unsigned char scsi3addr[])
809{
810 return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
811}
812
813static inline int is_scsi_rev_5(struct ctlr_info *h)
814{
815 if (!h->hba_inquiry_data)
816 return 0;
817 if ((h->hba_inquiry_data[2] & 0x07) == 5)
818 return 1;
819 return 0;
820}
821
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800822static int hpsa_find_target_lun(struct ctlr_info *h,
823 unsigned char scsi3addr[], int bus, int *target, int *lun)
824{
825 /* finds an unused bus, target, lun for a new physical device
826 * assumes h->devlock is held
827 */
828 int i, found = 0;
Scott Teelcfe5bad2011-10-26 16:21:07 -0500829 DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800830
Akinobu Mita263d9402012-01-21 00:15:27 +0900831 bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800832
833 for (i = 0; i < h->ndevices; i++) {
834 if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
Akinobu Mita263d9402012-01-21 00:15:27 +0900835 __set_bit(h->dev[i]->target, lun_taken);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800836 }
837
Akinobu Mita263d9402012-01-21 00:15:27 +0900838 i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
839 if (i < HPSA_MAX_DEVICES) {
840 /* *bus = 1; */
841 *target = i;
842 *lun = 0;
843 found = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800844 }
845 return !found;
846}
847
848/* Add an entry into h->dev[] array. */
849static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
850 struct hpsa_scsi_dev_t *device,
851 struct hpsa_scsi_dev_t *added[], int *nadded)
852{
853 /* assumes h->devlock is held */
854 int n = h->ndevices;
855 int i;
856 unsigned char addr1[8], addr2[8];
857 struct hpsa_scsi_dev_t *sd;
858
Scott Teelcfe5bad2011-10-26 16:21:07 -0500859 if (n >= HPSA_MAX_DEVICES) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800860 dev_err(&h->pdev->dev, "too many devices, some will be "
861 "inaccessible.\n");
862 return -1;
863 }
864
865 /* physical devices do not have lun or target assigned until now. */
866 if (device->lun != -1)
867 /* Logical device, lun is already assigned. */
868 goto lun_assigned;
869
870 /* If this device a non-zero lun of a multi-lun device
871 * byte 4 of the 8-byte LUN addr will contain the logical
872 * unit no, zero otherise.
873 */
874 if (device->scsi3addr[4] == 0) {
875 /* This is not a non-zero lun of a multi-lun device */
876 if (hpsa_find_target_lun(h, device->scsi3addr,
877 device->bus, &device->target, &device->lun) != 0)
878 return -1;
879 goto lun_assigned;
880 }
881
882 /* This is a non-zero lun of a multi-lun device.
883 * Search through our list and find the device which
884 * has the same 8 byte LUN address, excepting byte 4.
885 * Assign the same bus and target for this new LUN.
886 * Use the logical unit number from the firmware.
887 */
888 memcpy(addr1, device->scsi3addr, 8);
889 addr1[4] = 0;
890 for (i = 0; i < n; i++) {
891 sd = h->dev[i];
892 memcpy(addr2, sd->scsi3addr, 8);
893 addr2[4] = 0;
894 /* differ only in byte 4? */
895 if (memcmp(addr1, addr2, 8) == 0) {
896 device->bus = sd->bus;
897 device->target = sd->target;
898 device->lun = device->scsi3addr[4];
899 break;
900 }
901 }
902 if (device->lun == -1) {
903 dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
904 " suspect firmware bug or unsupported hardware "
905 "configuration.\n");
906 return -1;
907 }
908
909lun_assigned:
910
911 h->dev[n] = device;
912 h->ndevices++;
913 added[*nadded] = device;
914 (*nadded)++;
915
916 /* initially, (before registering with scsi layer) we don't
917 * know our hostno and we don't want to print anything first
918 * time anyway (the scsi layer's inquiries will show that info)
919 */
920 /* if (hostno != -1) */
921 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
922 scsi_device_type(device->devtype), hostno,
923 device->bus, device->target, device->lun);
924 return 0;
925}
926
Scott Teelbd9244f2012-01-19 14:01:30 -0600927/* Update an entry in h->dev[] array. */
928static void hpsa_scsi_update_entry(struct ctlr_info *h, int hostno,
929 int entry, struct hpsa_scsi_dev_t *new_entry)
930{
931 /* assumes h->devlock is held */
932 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
933
934 /* Raid level changed. */
935 h->dev[entry]->raid_level = new_entry->raid_level;
Stephen M. Cameron250fb122014-02-18 13:55:38 -0600936
937 /* Raid offload parameters changed. */
938 h->dev[entry]->offload_config = new_entry->offload_config;
939 h->dev[entry]->offload_enabled = new_entry->offload_enabled;
Stephen M. Cameron9fb0de22014-02-18 13:56:50 -0600940 h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
941 h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
942 h->dev[entry]->raid_map = new_entry->raid_map;
Stephen M. Cameron250fb122014-02-18 13:55:38 -0600943
Scott Teelbd9244f2012-01-19 14:01:30 -0600944 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d updated.\n",
945 scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
946 new_entry->target, new_entry->lun);
947}
948
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -0600949/* Replace an entry from h->dev[] array. */
950static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
951 int entry, struct hpsa_scsi_dev_t *new_entry,
952 struct hpsa_scsi_dev_t *added[], int *nadded,
953 struct hpsa_scsi_dev_t *removed[], int *nremoved)
954{
955 /* assumes h->devlock is held */
Scott Teelcfe5bad2011-10-26 16:21:07 -0500956 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -0600957 removed[*nremoved] = h->dev[entry];
958 (*nremoved)++;
Stephen M. Cameron01350d02011-08-09 08:18:01 -0500959
960 /*
961 * New physical devices won't have target/lun assigned yet
962 * so we need to preserve the values in the slot we are replacing.
963 */
964 if (new_entry->target == -1) {
965 new_entry->target = h->dev[entry]->target;
966 new_entry->lun = h->dev[entry]->lun;
967 }
968
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -0600969 h->dev[entry] = new_entry;
970 added[*nadded] = new_entry;
971 (*nadded)++;
972 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n",
973 scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
974 new_entry->target, new_entry->lun);
975}
976
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800977/* Remove an entry from h->dev[] array. */
978static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
979 struct hpsa_scsi_dev_t *removed[], int *nremoved)
980{
981 /* assumes h->devlock is held */
982 int i;
983 struct hpsa_scsi_dev_t *sd;
984
Scott Teelcfe5bad2011-10-26 16:21:07 -0500985 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800986
987 sd = h->dev[entry];
988 removed[*nremoved] = h->dev[entry];
989 (*nremoved)++;
990
991 for (i = entry; i < h->ndevices-1; i++)
992 h->dev[i] = h->dev[i+1];
993 h->ndevices--;
994 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
995 scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
996 sd->lun);
997}
998
999#define SCSI3ADDR_EQ(a, b) ( \
1000 (a)[7] == (b)[7] && \
1001 (a)[6] == (b)[6] && \
1002 (a)[5] == (b)[5] && \
1003 (a)[4] == (b)[4] && \
1004 (a)[3] == (b)[3] && \
1005 (a)[2] == (b)[2] && \
1006 (a)[1] == (b)[1] && \
1007 (a)[0] == (b)[0])
1008
1009static void fixup_botched_add(struct ctlr_info *h,
1010 struct hpsa_scsi_dev_t *added)
1011{
1012 /* called when scsi_add_device fails in order to re-adjust
1013 * h->dev[] to match the mid layer's view.
1014 */
1015 unsigned long flags;
1016 int i, j;
1017
1018 spin_lock_irqsave(&h->lock, flags);
1019 for (i = 0; i < h->ndevices; i++) {
1020 if (h->dev[i] == added) {
1021 for (j = i; j < h->ndevices-1; j++)
1022 h->dev[j] = h->dev[j+1];
1023 h->ndevices--;
1024 break;
1025 }
1026 }
1027 spin_unlock_irqrestore(&h->lock, flags);
1028 kfree(added);
1029}
1030
1031static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
1032 struct hpsa_scsi_dev_t *dev2)
1033{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001034 /* we compare everything except lun and target as these
1035 * are not yet assigned. Compare parts likely
1036 * to differ first
1037 */
1038 if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
1039 sizeof(dev1->scsi3addr)) != 0)
1040 return 0;
1041 if (memcmp(dev1->device_id, dev2->device_id,
1042 sizeof(dev1->device_id)) != 0)
1043 return 0;
1044 if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
1045 return 0;
1046 if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
1047 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001048 if (dev1->devtype != dev2->devtype)
1049 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001050 if (dev1->bus != dev2->bus)
1051 return 0;
1052 return 1;
1053}
1054
Scott Teelbd9244f2012-01-19 14:01:30 -06001055static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
1056 struct hpsa_scsi_dev_t *dev2)
1057{
1058 /* Device attributes that can change, but don't mean
1059 * that the device is a different device, nor that the OS
1060 * needs to be told anything about the change.
1061 */
1062 if (dev1->raid_level != dev2->raid_level)
1063 return 1;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001064 if (dev1->offload_config != dev2->offload_config)
1065 return 1;
1066 if (dev1->offload_enabled != dev2->offload_enabled)
1067 return 1;
Scott Teelbd9244f2012-01-19 14:01:30 -06001068 return 0;
1069}
1070
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001071/* Find needle in haystack. If exact match found, return DEVICE_SAME,
1072 * and return needle location in *index. If scsi3addr matches, but not
1073 * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
Scott Teelbd9244f2012-01-19 14:01:30 -06001074 * location in *index.
1075 * In the case of a minor device attribute change, such as RAID level, just
1076 * return DEVICE_UPDATED, along with the updated device's location in index.
1077 * If needle not found, return DEVICE_NOT_FOUND.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001078 */
1079static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
1080 struct hpsa_scsi_dev_t *haystack[], int haystack_size,
1081 int *index)
1082{
1083 int i;
1084#define DEVICE_NOT_FOUND 0
1085#define DEVICE_CHANGED 1
1086#define DEVICE_SAME 2
Scott Teelbd9244f2012-01-19 14:01:30 -06001087#define DEVICE_UPDATED 3
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001088 for (i = 0; i < haystack_size; i++) {
Stephen M. Cameron23231042010-02-04 08:43:36 -06001089 if (haystack[i] == NULL) /* previously removed. */
1090 continue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001091 if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
1092 *index = i;
Scott Teelbd9244f2012-01-19 14:01:30 -06001093 if (device_is_the_same(needle, haystack[i])) {
1094 if (device_updated(needle, haystack[i]))
1095 return DEVICE_UPDATED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001096 return DEVICE_SAME;
Scott Teelbd9244f2012-01-19 14:01:30 -06001097 } else {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001098 return DEVICE_CHANGED;
Scott Teelbd9244f2012-01-19 14:01:30 -06001099 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001100 }
1101 }
1102 *index = -1;
1103 return DEVICE_NOT_FOUND;
1104}
1105
Stephen M. Cameron4967bd32010-02-04 08:41:49 -06001106static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001107 struct hpsa_scsi_dev_t *sd[], int nsds)
1108{
1109 /* sd contains scsi3 addresses and devtypes, and inquiry
1110 * data. This function takes what's in sd to be the current
1111 * reality and updates h->dev[] to reflect that reality.
1112 */
1113 int i, entry, device_change, changes = 0;
1114 struct hpsa_scsi_dev_t *csd;
1115 unsigned long flags;
1116 struct hpsa_scsi_dev_t **added, **removed;
1117 int nadded, nremoved;
1118 struct Scsi_Host *sh = NULL;
1119
Scott Teelcfe5bad2011-10-26 16:21:07 -05001120 added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
1121 removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001122
1123 if (!added || !removed) {
1124 dev_warn(&h->pdev->dev, "out of memory in "
1125 "adjust_hpsa_scsi_table\n");
1126 goto free_and_out;
1127 }
1128
1129 spin_lock_irqsave(&h->devlock, flags);
1130
1131 /* find any devices in h->dev[] that are not in
1132 * sd[] and remove them from h->dev[], and for any
1133 * devices which have changed, remove the old device
1134 * info and add the new device info.
Scott Teelbd9244f2012-01-19 14:01:30 -06001135 * If minor device attributes change, just update
1136 * the existing device structure.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001137 */
1138 i = 0;
1139 nremoved = 0;
1140 nadded = 0;
1141 while (i < h->ndevices) {
1142 csd = h->dev[i];
1143 device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
1144 if (device_change == DEVICE_NOT_FOUND) {
1145 changes++;
1146 hpsa_scsi_remove_entry(h, hostno, i,
1147 removed, &nremoved);
1148 continue; /* remove ^^^, hence i not incremented */
1149 } else if (device_change == DEVICE_CHANGED) {
1150 changes++;
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001151 hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
1152 added, &nadded, removed, &nremoved);
Stephen M. Cameronc7f172d2010-02-04 08:43:31 -06001153 /* Set it to NULL to prevent it from being freed
1154 * at the bottom of hpsa_update_scsi_devices()
1155 */
1156 sd[entry] = NULL;
Scott Teelbd9244f2012-01-19 14:01:30 -06001157 } else if (device_change == DEVICE_UPDATED) {
1158 hpsa_scsi_update_entry(h, hostno, i, sd[entry]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001159 }
1160 i++;
1161 }
1162
1163 /* Now, make sure every device listed in sd[] is also
1164 * listed in h->dev[], adding them if they aren't found
1165 */
1166
1167 for (i = 0; i < nsds; i++) {
1168 if (!sd[i]) /* if already added above. */
1169 continue;
1170 device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1171 h->ndevices, &entry);
1172 if (device_change == DEVICE_NOT_FOUND) {
1173 changes++;
1174 if (hpsa_scsi_add_entry(h, hostno, sd[i],
1175 added, &nadded) != 0)
1176 break;
1177 sd[i] = NULL; /* prevent from being freed later. */
1178 } else if (device_change == DEVICE_CHANGED) {
1179 /* should never happen... */
1180 changes++;
1181 dev_warn(&h->pdev->dev,
1182 "device unexpectedly changed.\n");
1183 /* but if it does happen, we just ignore that device */
1184 }
1185 }
1186 spin_unlock_irqrestore(&h->devlock, flags);
1187
1188 /* Don't notify scsi mid layer of any changes the first time through
1189 * (or if there are no changes) scsi_scan_host will do it later the
1190 * first time through.
1191 */
1192 if (hostno == -1 || !changes)
1193 goto free_and_out;
1194
1195 sh = h->scsi_host;
1196 /* Notify scsi mid layer of any removed devices */
1197 for (i = 0; i < nremoved; i++) {
1198 struct scsi_device *sdev =
1199 scsi_device_lookup(sh, removed[i]->bus,
1200 removed[i]->target, removed[i]->lun);
1201 if (sdev != NULL) {
1202 scsi_remove_device(sdev);
1203 scsi_device_put(sdev);
1204 } else {
1205 /* We don't expect to get here.
1206 * future cmds to this device will get selection
1207 * timeout as if the device was gone.
1208 */
1209 dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
1210 " for removal.", hostno, removed[i]->bus,
1211 removed[i]->target, removed[i]->lun);
1212 }
1213 kfree(removed[i]);
1214 removed[i] = NULL;
1215 }
1216
1217 /* Notify scsi mid layer of any added devices */
1218 for (i = 0; i < nadded; i++) {
1219 if (scsi_add_device(sh, added[i]->bus,
1220 added[i]->target, added[i]->lun) == 0)
1221 continue;
1222 dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
1223 "device not added.\n", hostno, added[i]->bus,
1224 added[i]->target, added[i]->lun);
1225 /* now we have to remove it from h->dev,
1226 * since it didn't get added to scsi mid layer
1227 */
1228 fixup_botched_add(h, added[i]);
1229 }
1230
1231free_and_out:
1232 kfree(added);
1233 kfree(removed);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001234}
1235
1236/*
Joe Perches9e03aa22013-09-03 13:45:58 -07001237 * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001238 * Assume's h->devlock is held.
1239 */
1240static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1241 int bus, int target, int lun)
1242{
1243 int i;
1244 struct hpsa_scsi_dev_t *sd;
1245
1246 for (i = 0; i < h->ndevices; i++) {
1247 sd = h->dev[i];
1248 if (sd->bus == bus && sd->target == target && sd->lun == lun)
1249 return sd;
1250 }
1251 return NULL;
1252}
1253
1254/* link sdev->hostdata to our per-device structure. */
1255static int hpsa_slave_alloc(struct scsi_device *sdev)
1256{
1257 struct hpsa_scsi_dev_t *sd;
1258 unsigned long flags;
1259 struct ctlr_info *h;
1260
1261 h = sdev_to_hba(sdev);
1262 spin_lock_irqsave(&h->devlock, flags);
1263 sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1264 sdev_id(sdev), sdev->lun);
1265 if (sd != NULL)
1266 sdev->hostdata = sd;
1267 spin_unlock_irqrestore(&h->devlock, flags);
1268 return 0;
1269}
1270
1271static void hpsa_slave_destroy(struct scsi_device *sdev)
1272{
Stephen M. Cameronbcc44252010-02-04 08:41:54 -06001273 /* nothing to do. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001274}
1275
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001276static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
1277{
1278 int i;
1279
1280 if (!h->cmd_sg_list)
1281 return;
1282 for (i = 0; i < h->nr_cmds; i++) {
1283 kfree(h->cmd_sg_list[i]);
1284 h->cmd_sg_list[i] = NULL;
1285 }
1286 kfree(h->cmd_sg_list);
1287 h->cmd_sg_list = NULL;
1288}
1289
1290static int hpsa_allocate_sg_chain_blocks(struct ctlr_info *h)
1291{
1292 int i;
1293
1294 if (h->chainsize <= 0)
1295 return 0;
1296
1297 h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
1298 GFP_KERNEL);
1299 if (!h->cmd_sg_list)
1300 return -ENOMEM;
1301 for (i = 0; i < h->nr_cmds; i++) {
1302 h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
1303 h->chainsize, GFP_KERNEL);
1304 if (!h->cmd_sg_list[i])
1305 goto clean;
1306 }
1307 return 0;
1308
1309clean:
1310 hpsa_free_sg_chain_blocks(h);
1311 return -ENOMEM;
1312}
1313
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001314static int hpsa_map_sg_chain_block(struct ctlr_info *h,
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001315 struct CommandList *c)
1316{
1317 struct SGDescriptor *chain_sg, *chain_block;
1318 u64 temp64;
1319
1320 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
1321 chain_block = h->cmd_sg_list[c->cmdindex];
1322 chain_sg->Ext = HPSA_SG_CHAIN;
1323 chain_sg->Len = sizeof(*chain_sg) *
1324 (c->Header.SGTotal - h->max_cmd_sg_entries);
1325 temp64 = pci_map_single(h->pdev, chain_block, chain_sg->Len,
1326 PCI_DMA_TODEVICE);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001327 if (dma_mapping_error(&h->pdev->dev, temp64)) {
1328 /* prevent subsequent unmapping */
1329 chain_sg->Addr.lower = 0;
1330 chain_sg->Addr.upper = 0;
1331 return -1;
1332 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001333 chain_sg->Addr.lower = (u32) (temp64 & 0x0FFFFFFFFULL);
1334 chain_sg->Addr.upper = (u32) ((temp64 >> 32) & 0x0FFFFFFFFULL);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001335 return 0;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001336}
1337
1338static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
1339 struct CommandList *c)
1340{
1341 struct SGDescriptor *chain_sg;
1342 union u64bit temp64;
1343
1344 if (c->Header.SGTotal <= h->max_cmd_sg_entries)
1345 return;
1346
1347 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
1348 temp64.val32.lower = chain_sg->Addr.lower;
1349 temp64.val32.upper = chain_sg->Addr.upper;
1350 pci_unmap_single(h->pdev, temp64.val, chain_sg->Len, PCI_DMA_TODEVICE);
1351}
1352
Scott Teelc3497752014-02-18 13:56:34 -06001353static void handle_ioaccel_mode2_error(struct ctlr_info *h,
1354 struct CommandList *c,
1355 struct scsi_cmnd *cmd,
1356 struct io_accel2_cmd *c2)
1357{
1358 int data_len;
1359
1360 switch (c2->error_data.serv_response) {
1361 case IOACCEL2_SERV_RESPONSE_COMPLETE:
1362 switch (c2->error_data.status) {
1363 case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
1364 break;
1365 case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
1366 dev_warn(&h->pdev->dev,
1367 "%s: task complete with check condition.\n",
1368 "HP SSD Smart Path");
1369 if (c2->error_data.data_present !=
1370 IOACCEL2_SENSE_DATA_PRESENT)
1371 break;
1372 /* copy the sense data */
1373 data_len = c2->error_data.sense_data_len;
1374 if (data_len > SCSI_SENSE_BUFFERSIZE)
1375 data_len = SCSI_SENSE_BUFFERSIZE;
1376 if (data_len > sizeof(c2->error_data.sense_data_buff))
1377 data_len =
1378 sizeof(c2->error_data.sense_data_buff);
1379 memcpy(cmd->sense_buffer,
1380 c2->error_data.sense_data_buff, data_len);
1381 cmd->result |= SAM_STAT_CHECK_CONDITION;
1382 break;
1383 case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
1384 dev_warn(&h->pdev->dev,
1385 "%s: task complete with BUSY status.\n",
1386 "HP SSD Smart Path");
1387 break;
1388 case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
1389 dev_warn(&h->pdev->dev,
1390 "%s: task complete with reservation conflict.\n",
1391 "HP SSD Smart Path");
1392 break;
1393 case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
1394 /* Make scsi midlayer do unlimited retries */
1395 cmd->result = DID_IMM_RETRY << 16;
1396 break;
1397 case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
1398 dev_warn(&h->pdev->dev,
1399 "%s: task complete with aborted status.\n",
1400 "HP SSD Smart Path");
1401 break;
1402 default:
1403 dev_warn(&h->pdev->dev,
1404 "%s: task complete with unrecognized status: 0x%02x\n",
1405 "HP SSD Smart Path", c2->error_data.status);
1406 break;
1407 }
1408 break;
1409 case IOACCEL2_SERV_RESPONSE_FAILURE:
1410 /* don't expect to get here. */
1411 dev_warn(&h->pdev->dev,
1412 "unexpected delivery or target failure, status = 0x%02x\n",
1413 c2->error_data.status);
1414 break;
1415 case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
1416 break;
1417 case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
1418 break;
1419 case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
1420 dev_warn(&h->pdev->dev, "task management function rejected.\n");
1421 break;
1422 case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
1423 dev_warn(&h->pdev->dev, "task management function invalid LUN\n");
1424 break;
1425 default:
1426 dev_warn(&h->pdev->dev,
1427 "%s: Unrecognized server response: 0x%02x\n",
1428 "HP SSD Smart Path", c2->error_data.serv_response);
1429 break;
1430 }
1431}
1432
1433static void process_ioaccel2_completion(struct ctlr_info *h,
1434 struct CommandList *c, struct scsi_cmnd *cmd,
1435 struct hpsa_scsi_dev_t *dev)
1436{
1437 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
1438
1439 /* check for good status */
1440 if (likely(c2->error_data.serv_response == 0 &&
1441 c2->error_data.status == 0)) {
1442 cmd_free(h, c);
1443 cmd->scsi_done(cmd);
1444 return;
1445 }
1446
1447 /* Any RAID offload error results in retry which will use
1448 * the normal I/O path so the controller can handle whatever's
1449 * wrong.
1450 */
1451 if (is_logical_dev_addr_mode(dev->scsi3addr) &&
1452 c2->error_data.serv_response ==
1453 IOACCEL2_SERV_RESPONSE_FAILURE) {
1454 if (c2->error_data.status !=
1455 IOACCEL2_STATUS_SR_IOACCEL_DISABLED)
1456 dev_warn(&h->pdev->dev,
1457 "%s: Error 0x%02x, Retrying on standard path.\n",
1458 "HP SSD Smart Path", c2->error_data.status);
1459 dev->offload_enabled = 0;
Scott Teele863d682014-02-18 13:57:05 -06001460 h->drv_req_rescan = 1; /* schedule controller for a rescan */
Scott Teelc3497752014-02-18 13:56:34 -06001461 cmd->result = DID_SOFT_ERROR << 16;
1462 cmd_free(h, c);
1463 cmd->scsi_done(cmd);
1464 return;
1465 }
1466 handle_ioaccel_mode2_error(h, c, cmd, c2);
1467 cmd_free(h, c);
1468 cmd->scsi_done(cmd);
1469}
1470
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05001471static void complete_scsi_command(struct CommandList *cp)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001472{
1473 struct scsi_cmnd *cmd;
1474 struct ctlr_info *h;
1475 struct ErrorInfo *ei;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001476 struct hpsa_scsi_dev_t *dev;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001477
1478 unsigned char sense_key;
1479 unsigned char asc; /* additional sense code */
1480 unsigned char ascq; /* additional sense code qualifier */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05001481 unsigned long sense_data_size;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001482
1483 ei = cp->err_info;
1484 cmd = (struct scsi_cmnd *) cp->scsi_cmd;
1485 h = cp->h;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001486 dev = cmd->device->hostdata;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001487
1488 scsi_dma_unmap(cmd); /* undo the DMA mappings */
Matt Gatese1f7de02014-02-18 13:55:17 -06001489 if ((cp->cmd_type == CMD_SCSI) &&
1490 (cp->Header.SGTotal > h->max_cmd_sg_entries))
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001491 hpsa_unmap_sg_chain_block(h, cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001492
1493 cmd->result = (DID_OK << 16); /* host byte */
1494 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
Scott Teelc3497752014-02-18 13:56:34 -06001495
1496 if (cp->cmd_type == CMD_IOACCEL2)
1497 return process_ioaccel2_completion(h, cp, cmd, dev);
1498
Stephen M. Cameron55126722010-02-25 14:03:01 -06001499 cmd->result |= ei->ScsiStatus;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001500
1501 /* copy the sense data whether we need to or not. */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05001502 if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
1503 sense_data_size = SCSI_SENSE_BUFFERSIZE;
1504 else
1505 sense_data_size = sizeof(ei->SenseInfo);
1506 if (ei->SenseLen < sense_data_size)
1507 sense_data_size = ei->SenseLen;
1508
1509 memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001510 scsi_set_resid(cmd, ei->ResidualCnt);
1511
1512 if (ei->CommandStatus == 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001513 cmd_free(h, cp);
Tomas Henzl2cc5bfa2013-08-01 15:14:00 +02001514 cmd->scsi_done(cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001515 return;
1516 }
1517
Matt Gatese1f7de02014-02-18 13:55:17 -06001518 /* For I/O accelerator commands, copy over some fields to the normal
1519 * CISS header used below for error handling.
1520 */
1521 if (cp->cmd_type == CMD_IOACCEL1) {
1522 struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
1523 cp->Header.SGList = cp->Header.SGTotal = scsi_sg_count(cmd);
1524 cp->Request.CDBLen = c->io_flags & IOACCEL1_IOFLAGS_CDBLEN_MASK;
1525 cp->Header.Tag.lower = c->Tag.lower;
1526 cp->Header.Tag.upper = c->Tag.upper;
1527 memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
1528 memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001529
1530 /* Any RAID offload error results in retry which will use
1531 * the normal I/O path so the controller can handle whatever's
1532 * wrong.
1533 */
1534 if (is_logical_dev_addr_mode(dev->scsi3addr)) {
1535 if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
1536 dev->offload_enabled = 0;
1537 cmd->result = DID_SOFT_ERROR << 16;
1538 cmd_free(h, cp);
1539 cmd->scsi_done(cmd);
1540 return;
1541 }
Matt Gatese1f7de02014-02-18 13:55:17 -06001542 }
1543
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001544 /* an error has occurred */
1545 switch (ei->CommandStatus) {
1546
1547 case CMD_TARGET_STATUS:
1548 if (ei->ScsiStatus) {
1549 /* Get sense key */
1550 sense_key = 0xf & ei->SenseInfo[2];
1551 /* Get additional sense code */
1552 asc = ei->SenseInfo[12];
1553 /* Get addition sense code qualifier */
1554 ascq = ei->SenseInfo[13];
1555 }
1556
1557 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
Matt Gates3ce438d2013-12-04 17:10:36 -06001558 if (check_for_unit_attention(h, cp))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001559 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001560 if (sense_key == ILLEGAL_REQUEST) {
1561 /*
1562 * SCSI REPORT_LUNS is commonly unsupported on
1563 * Smart Array. Suppress noisy complaint.
1564 */
1565 if (cp->Request.CDB[0] == REPORT_LUNS)
1566 break;
1567
1568 /* If ASC/ASCQ indicate Logical Unit
1569 * Not Supported condition,
1570 */
1571 if ((asc == 0x25) && (ascq == 0x0)) {
1572 dev_warn(&h->pdev->dev, "cp %p "
1573 "has check condition\n", cp);
1574 break;
1575 }
1576 }
1577
1578 if (sense_key == NOT_READY) {
1579 /* If Sense is Not Ready, Logical Unit
1580 * Not ready, Manual Intervention
1581 * required
1582 */
1583 if ((asc == 0x04) && (ascq == 0x03)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001584 dev_warn(&h->pdev->dev, "cp %p "
1585 "has check condition: unit "
1586 "not ready, manual "
1587 "intervention required\n", cp);
1588 break;
1589 }
1590 }
Matt Gates1d3b3602010-02-04 08:43:00 -06001591 if (sense_key == ABORTED_COMMAND) {
1592 /* Aborted command is retryable */
1593 dev_warn(&h->pdev->dev, "cp %p "
1594 "has check condition: aborted command: "
1595 "ASC: 0x%x, ASCQ: 0x%x\n",
1596 cp, asc, ascq);
Stephen M. Cameron2e311fb2013-09-23 13:33:41 -05001597 cmd->result |= DID_SOFT_ERROR << 16;
Matt Gates1d3b3602010-02-04 08:43:00 -06001598 break;
1599 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001600 /* Must be some other type of check condition */
Stephen M. Cameron21b8e4e2012-05-01 11:42:25 -05001601 dev_dbg(&h->pdev->dev, "cp %p has check condition: "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001602 "unknown type: "
1603 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1604 "Returning result: 0x%x, "
1605 "cmd=[%02x %02x %02x %02x %02x "
Mike Miller807be732010-02-04 08:43:26 -06001606 "%02x %02x %02x %02x %02x %02x "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001607 "%02x %02x %02x %02x %02x]\n",
1608 cp, sense_key, asc, ascq,
1609 cmd->result,
1610 cmd->cmnd[0], cmd->cmnd[1],
1611 cmd->cmnd[2], cmd->cmnd[3],
1612 cmd->cmnd[4], cmd->cmnd[5],
1613 cmd->cmnd[6], cmd->cmnd[7],
Mike Miller807be732010-02-04 08:43:26 -06001614 cmd->cmnd[8], cmd->cmnd[9],
1615 cmd->cmnd[10], cmd->cmnd[11],
1616 cmd->cmnd[12], cmd->cmnd[13],
1617 cmd->cmnd[14], cmd->cmnd[15]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001618 break;
1619 }
1620
1621
1622 /* Problem was not a check condition
1623 * Pass it up to the upper layers...
1624 */
1625 if (ei->ScsiStatus) {
1626 dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
1627 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1628 "Returning result: 0x%x\n",
1629 cp, ei->ScsiStatus,
1630 sense_key, asc, ascq,
1631 cmd->result);
1632 } else { /* scsi status is zero??? How??? */
1633 dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
1634 "Returning no connection.\n", cp),
1635
1636 /* Ordinarily, this case should never happen,
1637 * but there is a bug in some released firmware
1638 * revisions that allows it to happen if, for
1639 * example, a 4100 backplane loses power and
1640 * the tape drive is in it. We assume that
1641 * it's a fatal error of some kind because we
1642 * can't show that it wasn't. We will make it
1643 * look like selection timeout since that is
1644 * the most common reason for this to occur,
1645 * and it's severe enough.
1646 */
1647
1648 cmd->result = DID_NO_CONNECT << 16;
1649 }
1650 break;
1651
1652 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1653 break;
1654 case CMD_DATA_OVERRUN:
1655 dev_warn(&h->pdev->dev, "cp %p has"
1656 " completed with data overrun "
1657 "reported\n", cp);
1658 break;
1659 case CMD_INVALID: {
1660 /* print_bytes(cp, sizeof(*cp), 1, 0);
1661 print_cmd(cp); */
1662 /* We get CMD_INVALID if you address a non-existent device
1663 * instead of a selection timeout (no response). You will
1664 * see this if you yank out a drive, then try to access it.
1665 * This is kind of a shame because it means that any other
1666 * CMD_INVALID (e.g. driver bug) will get interpreted as a
1667 * missing target. */
1668 cmd->result = DID_NO_CONNECT << 16;
1669 }
1670 break;
1671 case CMD_PROTOCOL_ERR:
Stephen M. Cameron256d0ea2012-09-14 16:34:25 -05001672 cmd->result = DID_ERROR << 16;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001673 dev_warn(&h->pdev->dev, "cp %p has "
Stephen M. Cameron256d0ea2012-09-14 16:34:25 -05001674 "protocol error\n", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001675 break;
1676 case CMD_HARDWARE_ERR:
1677 cmd->result = DID_ERROR << 16;
1678 dev_warn(&h->pdev->dev, "cp %p had hardware error\n", cp);
1679 break;
1680 case CMD_CONNECTION_LOST:
1681 cmd->result = DID_ERROR << 16;
1682 dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
1683 break;
1684 case CMD_ABORTED:
1685 cmd->result = DID_ABORT << 16;
1686 dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
1687 cp, ei->ScsiStatus);
1688 break;
1689 case CMD_ABORT_FAILED:
1690 cmd->result = DID_ERROR << 16;
1691 dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
1692 break;
1693 case CMD_UNSOLICITED_ABORT:
Stephen M. Cameronf6e76052011-07-26 11:08:52 -05001694 cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
1695 dev_warn(&h->pdev->dev, "cp %p aborted due to an unsolicited "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001696 "abort\n", cp);
1697 break;
1698 case CMD_TIMEOUT:
1699 cmd->result = DID_TIME_OUT << 16;
1700 dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
1701 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06001702 case CMD_UNABORTABLE:
1703 cmd->result = DID_ERROR << 16;
1704 dev_warn(&h->pdev->dev, "Command unabortable\n");
1705 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001706 case CMD_IOACCEL_DISABLED:
1707 /* This only handles the direct pass-through case since RAID
1708 * offload is handled above. Just attempt a retry.
1709 */
1710 cmd->result = DID_SOFT_ERROR << 16;
1711 dev_warn(&h->pdev->dev,
1712 "cp %p had HP SSD Smart Path error\n", cp);
1713 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001714 default:
1715 cmd->result = DID_ERROR << 16;
1716 dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
1717 cp, ei->CommandStatus);
1718 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001719 cmd_free(h, cp);
Tomas Henzl2cc5bfa2013-08-01 15:14:00 +02001720 cmd->scsi_done(cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001721}
1722
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001723static void hpsa_pci_unmap(struct pci_dev *pdev,
1724 struct CommandList *c, int sg_used, int data_direction)
1725{
1726 int i;
1727 union u64bit addr64;
1728
1729 for (i = 0; i < sg_used; i++) {
1730 addr64.val32.lower = c->SG[i].Addr.lower;
1731 addr64.val32.upper = c->SG[i].Addr.upper;
1732 pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len,
1733 data_direction);
1734 }
1735}
1736
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001737static int hpsa_map_one(struct pci_dev *pdev,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001738 struct CommandList *cp,
1739 unsigned char *buf,
1740 size_t buflen,
1741 int data_direction)
1742{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001743 u64 addr64;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001744
1745 if (buflen == 0 || data_direction == PCI_DMA_NONE) {
1746 cp->Header.SGList = 0;
1747 cp->Header.SGTotal = 0;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001748 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001749 }
1750
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001751 addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction);
Shuah Khaneceaae12013-02-20 11:24:34 -06001752 if (dma_mapping_error(&pdev->dev, addr64)) {
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001753 /* Prevent subsequent unmap of something never mapped */
Shuah Khaneceaae12013-02-20 11:24:34 -06001754 cp->Header.SGList = 0;
1755 cp->Header.SGTotal = 0;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001756 return -1;
Shuah Khaneceaae12013-02-20 11:24:34 -06001757 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001758 cp->SG[0].Addr.lower =
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001759 (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001760 cp->SG[0].Addr.upper =
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001761 (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001762 cp->SG[0].Len = buflen;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06001763 cp->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining */
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001764 cp->Header.SGList = (u8) 1; /* no. SGs contig in this cmd */
1765 cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001766 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001767}
1768
1769static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
1770 struct CommandList *c)
1771{
1772 DECLARE_COMPLETION_ONSTACK(wait);
1773
1774 c->waiting = &wait;
1775 enqueue_cmd_and_start_io(h, c);
1776 wait_for_completion(&wait);
1777}
1778
Stephen M. Camerona0c12412011-10-26 16:22:04 -05001779static void hpsa_scsi_do_simple_cmd_core_if_no_lockup(struct ctlr_info *h,
1780 struct CommandList *c)
1781{
1782 unsigned long flags;
1783
1784 /* If controller lockup detected, fake a hardware error. */
1785 spin_lock_irqsave(&h->lock, flags);
1786 if (unlikely(h->lockup_detected)) {
1787 spin_unlock_irqrestore(&h->lock, flags);
1788 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
1789 } else {
1790 spin_unlock_irqrestore(&h->lock, flags);
1791 hpsa_scsi_do_simple_cmd_core(h, c);
1792 }
1793}
1794
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001795#define MAX_DRIVER_CMD_RETRIES 25
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001796static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
1797 struct CommandList *c, int data_direction)
1798{
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001799 int backoff_time = 10, retry_count = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001800
1801 do {
Joe Perches7630abd2011-05-08 23:32:40 -07001802 memset(c->err_info, 0, sizeof(*c->err_info));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001803 hpsa_scsi_do_simple_cmd_core(h, c);
1804 retry_count++;
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001805 if (retry_count > 3) {
1806 msleep(backoff_time);
1807 if (backoff_time < 1000)
1808 backoff_time *= 2;
1809 }
Matt Bondurant852af202012-05-01 11:42:35 -05001810 } while ((check_for_unit_attention(h, c) ||
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001811 check_for_busy(h, c)) &&
1812 retry_count <= MAX_DRIVER_CMD_RETRIES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001813 hpsa_pci_unmap(h->pdev, c, 1, data_direction);
1814}
1815
1816static void hpsa_scsi_interpret_error(struct CommandList *cp)
1817{
1818 struct ErrorInfo *ei;
1819 struct device *d = &cp->h->pdev->dev;
1820
1821 ei = cp->err_info;
1822 switch (ei->CommandStatus) {
1823 case CMD_TARGET_STATUS:
1824 dev_warn(d, "cmd %p has completed with errors\n", cp);
1825 dev_warn(d, "cmd %p has SCSI Status = %x\n", cp,
1826 ei->ScsiStatus);
1827 if (ei->ScsiStatus == 0)
1828 dev_warn(d, "SCSI status is abnormally zero. "
1829 "(probably indicates selection timeout "
1830 "reported incorrectly due to a known "
1831 "firmware bug, circa July, 2001.)\n");
1832 break;
1833 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1834 dev_info(d, "UNDERRUN\n");
1835 break;
1836 case CMD_DATA_OVERRUN:
1837 dev_warn(d, "cp %p has completed with data overrun\n", cp);
1838 break;
1839 case CMD_INVALID: {
1840 /* controller unfortunately reports SCSI passthru's
1841 * to non-existent targets as invalid commands.
1842 */
1843 dev_warn(d, "cp %p is reported invalid (probably means "
1844 "target device no longer present)\n", cp);
1845 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
1846 print_cmd(cp); */
1847 }
1848 break;
1849 case CMD_PROTOCOL_ERR:
1850 dev_warn(d, "cp %p has protocol error \n", cp);
1851 break;
1852 case CMD_HARDWARE_ERR:
1853 /* cmd->result = DID_ERROR << 16; */
1854 dev_warn(d, "cp %p had hardware error\n", cp);
1855 break;
1856 case CMD_CONNECTION_LOST:
1857 dev_warn(d, "cp %p had connection lost\n", cp);
1858 break;
1859 case CMD_ABORTED:
1860 dev_warn(d, "cp %p was aborted\n", cp);
1861 break;
1862 case CMD_ABORT_FAILED:
1863 dev_warn(d, "cp %p reports abort failed\n", cp);
1864 break;
1865 case CMD_UNSOLICITED_ABORT:
1866 dev_warn(d, "cp %p aborted due to an unsolicited abort\n", cp);
1867 break;
1868 case CMD_TIMEOUT:
1869 dev_warn(d, "cp %p timed out\n", cp);
1870 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06001871 case CMD_UNABORTABLE:
1872 dev_warn(d, "Command unabortable\n");
1873 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001874 default:
1875 dev_warn(d, "cp %p returned unknown status %x\n", cp,
1876 ei->CommandStatus);
1877 }
1878}
1879
1880static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
1881 unsigned char page, unsigned char *buf,
1882 unsigned char bufsize)
1883{
1884 int rc = IO_OK;
1885 struct CommandList *c;
1886 struct ErrorInfo *ei;
1887
1888 c = cmd_special_alloc(h);
1889
1890 if (c == NULL) { /* trouble... */
1891 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06001892 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001893 }
1894
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001895 if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
1896 page, scsi3addr, TYPE_CMD)) {
1897 rc = -1;
1898 goto out;
1899 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001900 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1901 ei = c->err_info;
1902 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
1903 hpsa_scsi_interpret_error(c);
1904 rc = -1;
1905 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001906out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001907 cmd_special_free(h, c);
1908 return rc;
1909}
1910
Scott Teelbf711ac2014-02-18 13:56:39 -06001911static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
1912 u8 reset_type)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001913{
1914 int rc = IO_OK;
1915 struct CommandList *c;
1916 struct ErrorInfo *ei;
1917
1918 c = cmd_special_alloc(h);
1919
1920 if (c == NULL) { /* trouble... */
1921 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
Stephen M. Camerone9ea04a2010-02-25 14:03:06 -06001922 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001923 }
1924
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001925 /* fill_cmd can't fail here, no data buffer to map. */
Scott Teelbf711ac2014-02-18 13:56:39 -06001926 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
1927 scsi3addr, TYPE_MSG);
1928 c->Request.CDB[1] = reset_type; /* fill_cmd defaults to LUN reset */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001929 hpsa_scsi_do_simple_cmd_core(h, c);
1930 /* no unmap needed here because no data xfer. */
1931
1932 ei = c->err_info;
1933 if (ei->CommandStatus != 0) {
1934 hpsa_scsi_interpret_error(c);
1935 rc = -1;
1936 }
1937 cmd_special_free(h, c);
1938 return rc;
1939}
1940
1941static void hpsa_get_raid_level(struct ctlr_info *h,
1942 unsigned char *scsi3addr, unsigned char *raid_level)
1943{
1944 int rc;
1945 unsigned char *buf;
1946
1947 *raid_level = RAID_UNKNOWN;
1948 buf = kzalloc(64, GFP_KERNEL);
1949 if (!buf)
1950 return;
1951 rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0xC1, buf, 64);
1952 if (rc == 0)
1953 *raid_level = buf[8];
1954 if (*raid_level > RAID_UNKNOWN)
1955 *raid_level = RAID_UNKNOWN;
1956 kfree(buf);
1957 return;
1958}
1959
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001960#define HPSA_MAP_DEBUG
1961#ifdef HPSA_MAP_DEBUG
1962static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
1963 struct raid_map_data *map_buff)
1964{
1965 struct raid_map_disk_data *dd = &map_buff->data[0];
1966 int map, row, col;
1967 u16 map_cnt, row_cnt, disks_per_row;
1968
1969 if (rc != 0)
1970 return;
1971
1972 dev_info(&h->pdev->dev, "structure_size = %u\n",
1973 le32_to_cpu(map_buff->structure_size));
1974 dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
1975 le32_to_cpu(map_buff->volume_blk_size));
1976 dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
1977 le64_to_cpu(map_buff->volume_blk_cnt));
1978 dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
1979 map_buff->phys_blk_shift);
1980 dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
1981 map_buff->parity_rotation_shift);
1982 dev_info(&h->pdev->dev, "strip_size = %u\n",
1983 le16_to_cpu(map_buff->strip_size));
1984 dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
1985 le64_to_cpu(map_buff->disk_starting_blk));
1986 dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
1987 le64_to_cpu(map_buff->disk_blk_cnt));
1988 dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
1989 le16_to_cpu(map_buff->data_disks_per_row));
1990 dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
1991 le16_to_cpu(map_buff->metadata_disks_per_row));
1992 dev_info(&h->pdev->dev, "row_cnt = %u\n",
1993 le16_to_cpu(map_buff->row_cnt));
1994 dev_info(&h->pdev->dev, "layout_map_count = %u\n",
1995 le16_to_cpu(map_buff->layout_map_count));
1996
1997 map_cnt = le16_to_cpu(map_buff->layout_map_count);
1998 for (map = 0; map < map_cnt; map++) {
1999 dev_info(&h->pdev->dev, "Map%u:\n", map);
2000 row_cnt = le16_to_cpu(map_buff->row_cnt);
2001 for (row = 0; row < row_cnt; row++) {
2002 dev_info(&h->pdev->dev, " Row%u:\n", row);
2003 disks_per_row =
2004 le16_to_cpu(map_buff->data_disks_per_row);
2005 for (col = 0; col < disks_per_row; col++, dd++)
2006 dev_info(&h->pdev->dev,
2007 " D%02u: h=0x%04x xor=%u,%u\n",
2008 col, dd->ioaccel_handle,
2009 dd->xor_mult[0], dd->xor_mult[1]);
2010 disks_per_row =
2011 le16_to_cpu(map_buff->metadata_disks_per_row);
2012 for (col = 0; col < disks_per_row; col++, dd++)
2013 dev_info(&h->pdev->dev,
2014 " M%02u: h=0x%04x xor=%u,%u\n",
2015 col, dd->ioaccel_handle,
2016 dd->xor_mult[0], dd->xor_mult[1]);
2017 }
2018 }
2019}
2020#else
2021static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
2022 __attribute__((unused)) int rc,
2023 __attribute__((unused)) struct raid_map_data *map_buff)
2024{
2025}
2026#endif
2027
2028static int hpsa_get_raid_map(struct ctlr_info *h,
2029 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2030{
2031 int rc = 0;
2032 struct CommandList *c;
2033 struct ErrorInfo *ei;
2034
2035 c = cmd_special_alloc(h);
2036 if (c == NULL) {
2037 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2038 return -ENOMEM;
2039 }
2040 if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
2041 sizeof(this_device->raid_map), 0,
2042 scsi3addr, TYPE_CMD)) {
2043 dev_warn(&h->pdev->dev, "Out of memory in hpsa_get_raid_map()\n");
2044 cmd_special_free(h, c);
2045 return -ENOMEM;
2046 }
2047 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2048 ei = c->err_info;
2049 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2050 hpsa_scsi_interpret_error(c);
2051 cmd_special_free(h, c);
2052 return -1;
2053 }
2054 cmd_special_free(h, c);
2055
2056 /* @todo in the future, dynamically allocate RAID map memory */
2057 if (le32_to_cpu(this_device->raid_map.structure_size) >
2058 sizeof(this_device->raid_map)) {
2059 dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
2060 rc = -1;
2061 }
2062 hpsa_debug_map_buff(h, rc, &this_device->raid_map);
2063 return rc;
2064}
2065
2066static void hpsa_get_ioaccel_status(struct ctlr_info *h,
2067 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2068{
2069 int rc;
2070 unsigned char *buf;
2071 u8 ioaccel_status;
2072
2073 this_device->offload_config = 0;
2074 this_device->offload_enabled = 0;
2075
2076 buf = kzalloc(64, GFP_KERNEL);
2077 if (!buf)
2078 return;
2079 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
2080 HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
2081 if (rc != 0)
2082 goto out;
2083
2084#define IOACCEL_STATUS_BYTE 4
2085#define OFFLOAD_CONFIGURED_BIT 0x01
2086#define OFFLOAD_ENABLED_BIT 0x02
2087 ioaccel_status = buf[IOACCEL_STATUS_BYTE];
2088 this_device->offload_config =
2089 !!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
2090 if (this_device->offload_config) {
2091 this_device->offload_enabled =
2092 !!(ioaccel_status & OFFLOAD_ENABLED_BIT);
2093 if (hpsa_get_raid_map(h, scsi3addr, this_device))
2094 this_device->offload_enabled = 0;
2095 }
2096out:
2097 kfree(buf);
2098 return;
2099}
2100
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002101/* Get the device id from inquiry page 0x83 */
2102static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
2103 unsigned char *device_id, int buflen)
2104{
2105 int rc;
2106 unsigned char *buf;
2107
2108 if (buflen > 16)
2109 buflen = 16;
2110 buf = kzalloc(64, GFP_KERNEL);
2111 if (!buf)
2112 return -1;
2113 rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0x83, buf, 64);
2114 if (rc == 0)
2115 memcpy(device_id, &buf[8], buflen);
2116 kfree(buf);
2117 return rc != 0;
2118}
2119
2120static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
2121 struct ReportLUNdata *buf, int bufsize,
2122 int extended_response)
2123{
2124 int rc = IO_OK;
2125 struct CommandList *c;
2126 unsigned char scsi3addr[8];
2127 struct ErrorInfo *ei;
2128
2129 c = cmd_special_alloc(h);
2130 if (c == NULL) { /* trouble... */
2131 dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2132 return -1;
2133 }
Stephen M. Camerone89c0ae2010-02-04 08:42:04 -06002134 /* address the controller */
2135 memset(scsi3addr, 0, sizeof(scsi3addr));
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002136 if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
2137 buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
2138 rc = -1;
2139 goto out;
2140 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002141 if (extended_response)
2142 c->Request.CDB[1] = extended_response;
2143 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2144 ei = c->err_info;
2145 if (ei->CommandStatus != 0 &&
2146 ei->CommandStatus != CMD_DATA_UNDERRUN) {
2147 hpsa_scsi_interpret_error(c);
2148 rc = -1;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002149 } else {
2150 if (buf->extended_response_flag != extended_response) {
2151 dev_err(&h->pdev->dev,
2152 "report luns requested format %u, got %u\n",
2153 extended_response,
2154 buf->extended_response_flag);
2155 rc = -1;
2156 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002157 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002158out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002159 cmd_special_free(h, c);
2160 return rc;
2161}
2162
2163static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
2164 struct ReportLUNdata *buf,
2165 int bufsize, int extended_response)
2166{
2167 return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
2168}
2169
2170static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
2171 struct ReportLUNdata *buf, int bufsize)
2172{
2173 return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
2174}
2175
2176static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
2177 int bus, int target, int lun)
2178{
2179 device->bus = bus;
2180 device->target = target;
2181 device->lun = lun;
2182}
2183
2184static int hpsa_update_device_info(struct ctlr_info *h,
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002185 unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
2186 unsigned char *is_OBDR_device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002187{
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002188
2189#define OBDR_SIG_OFFSET 43
2190#define OBDR_TAPE_SIG "$DR-10"
2191#define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
2192#define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
2193
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06002194 unsigned char *inq_buff;
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002195 unsigned char *obdr_sig;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002196
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06002197 inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002198 if (!inq_buff)
2199 goto bail_out;
2200
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002201 /* Do an inquiry to the device to see what it is. */
2202 if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
2203 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
2204 /* Inquiry failed (msg printed already) */
2205 dev_err(&h->pdev->dev,
2206 "hpsa_update_device_info: inquiry failed\n");
2207 goto bail_out;
2208 }
2209
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002210 this_device->devtype = (inq_buff[0] & 0x1f);
2211 memcpy(this_device->scsi3addr, scsi3addr, 8);
2212 memcpy(this_device->vendor, &inq_buff[8],
2213 sizeof(this_device->vendor));
2214 memcpy(this_device->model, &inq_buff[16],
2215 sizeof(this_device->model));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002216 memset(this_device->device_id, 0,
2217 sizeof(this_device->device_id));
2218 hpsa_get_device_id(h, scsi3addr, this_device->device_id,
2219 sizeof(this_device->device_id));
2220
2221 if (this_device->devtype == TYPE_DISK &&
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002222 is_logical_dev_addr_mode(scsi3addr)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002223 hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002224 if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
2225 hpsa_get_ioaccel_status(h, scsi3addr, this_device);
2226 } else {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002227 this_device->raid_level = RAID_UNKNOWN;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002228 this_device->offload_config = 0;
2229 this_device->offload_enabled = 0;
2230 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002231
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002232 if (is_OBDR_device) {
2233 /* See if this is a One-Button-Disaster-Recovery device
2234 * by looking for "$DR-10" at offset 43 in inquiry data.
2235 */
2236 obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
2237 *is_OBDR_device = (this_device->devtype == TYPE_ROM &&
2238 strncmp(obdr_sig, OBDR_TAPE_SIG,
2239 OBDR_SIG_LEN) == 0);
2240 }
2241
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002242 kfree(inq_buff);
2243 return 0;
2244
2245bail_out:
2246 kfree(inq_buff);
2247 return 1;
2248}
2249
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002250static unsigned char *ext_target_model[] = {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002251 "MSA2012",
2252 "MSA2024",
2253 "MSA2312",
2254 "MSA2324",
Stephen M. Cameronfda38512011-05-03 15:00:07 -05002255 "P2000 G3 SAS",
Stephen M. Camerone06c8e52013-09-23 13:33:56 -05002256 "MSA 2040 SAS",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002257 NULL,
2258};
2259
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002260static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002261{
2262 int i;
2263
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002264 for (i = 0; ext_target_model[i]; i++)
2265 if (strncmp(device->model, ext_target_model[i],
2266 strlen(ext_target_model[i])) == 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002267 return 1;
2268 return 0;
2269}
2270
2271/* Helper function to assign bus, target, lun mapping of devices.
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002272 * Puts non-external target logical volumes on bus 0, external target logical
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002273 * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
2274 * Logical drive target and lun are assigned at this time, but
2275 * physical device lun and target assignment are deferred (assigned
2276 * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
2277 */
2278static void figure_bus_target_lun(struct ctlr_info *h,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002279 u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002280{
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002281 u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002282
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002283 if (!is_logical_dev_addr_mode(lunaddrbytes)) {
2284 /* physical device, target and lun filled in later */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002285 if (is_hba_lunid(lunaddrbytes))
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002286 hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002287 else
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002288 /* defer target, lun assignment for physical devices */
2289 hpsa_set_bus_target_lun(device, 2, -1, -1);
2290 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002291 }
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002292 /* It's a logical device */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002293 if (is_ext_target(h, device)) {
2294 /* external target way, put logicals on bus 1
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002295 * and match target/lun numbers box
2296 * reports, other smart array, bus 0, target 0, match lunid
2297 */
2298 hpsa_set_bus_target_lun(device,
2299 1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
2300 return;
2301 }
2302 hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002303}
2304
2305/*
2306 * If there is no lun 0 on a target, linux won't find any devices.
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002307 * For the external targets (arrays), we have to manually detect the enclosure
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002308 * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
2309 * it for some reason. *tmpdevice is the target we're adding,
2310 * this_device is a pointer into the current element of currentsd[]
2311 * that we're building up in update_scsi_devices(), below.
2312 * lunzerobits is a bitmap that tracks which targets already have a
2313 * lun 0 assigned.
2314 * Returns 1 if an enclosure was added, 0 if not.
2315 */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002316static int add_ext_target_dev(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002317 struct hpsa_scsi_dev_t *tmpdevice,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002318 struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002319 unsigned long lunzerobits[], int *n_ext_target_devs)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002320{
2321 unsigned char scsi3addr[8];
2322
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002323 if (test_bit(tmpdevice->target, lunzerobits))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002324 return 0; /* There is already a lun 0 on this target. */
2325
2326 if (!is_logical_dev_addr_mode(lunaddrbytes))
2327 return 0; /* It's the logical targets that may lack lun 0. */
2328
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002329 if (!is_ext_target(h, tmpdevice))
2330 return 0; /* Only external target devices have this problem. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002331
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002332 if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002333 return 0;
2334
Stephen M. Cameronc4f8a292011-01-07 10:55:43 -06002335 memset(scsi3addr, 0, 8);
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002336 scsi3addr[3] = tmpdevice->target;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002337 if (is_hba_lunid(scsi3addr))
2338 return 0; /* Don't add the RAID controller here. */
2339
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002340 if (is_scsi_rev_5(h))
2341 return 0; /* p1210m doesn't need to do this. */
2342
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002343 if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
Scott Teelaca4a522012-01-19 14:01:19 -06002344 dev_warn(&h->pdev->dev, "Maximum number of external "
2345 "target devices exceeded. Check your hardware "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002346 "configuration.");
2347 return 0;
2348 }
2349
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002350 if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002351 return 0;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002352 (*n_ext_target_devs)++;
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002353 hpsa_set_bus_target_lun(this_device,
2354 tmpdevice->bus, tmpdevice->target, 0);
2355 set_bit(tmpdevice->target, lunzerobits);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002356 return 1;
2357}
2358
2359/*
Scott Teel54b6e9e2014-02-18 13:56:45 -06002360 * Get address of physical disk used for an ioaccel2 mode command:
2361 * 1. Extract ioaccel2 handle from the command.
2362 * 2. Find a matching ioaccel2 handle from list of physical disks.
2363 * 3. Return:
2364 * 1 and set scsi3addr to address of matching physical
2365 * 0 if no matching physical disk was found.
2366 */
2367static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
2368 struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
2369{
2370 struct ReportExtendedLUNdata *physicals = NULL;
2371 int responsesize = 24; /* size of physical extended response */
2372 int extended = 2; /* flag forces reporting 'other dev info'. */
2373 int reportsize = sizeof(*physicals) + HPSA_MAX_PHYS_LUN * responsesize;
2374 u32 nphysicals = 0; /* number of reported physical devs */
2375 int found = 0; /* found match (1) or not (0) */
2376 u32 find; /* handle we need to match */
2377 int i;
2378 struct scsi_cmnd *scmd; /* scsi command within request being aborted */
2379 struct hpsa_scsi_dev_t *d; /* device of request being aborted */
2380 struct io_accel2_cmd *c2a; /* ioaccel2 command to abort */
2381 u32 it_nexus; /* 4 byte device handle for the ioaccel2 cmd */
2382 u32 scsi_nexus; /* 4 byte device handle for the ioaccel2 cmd */
2383
2384 if (ioaccel2_cmd_to_abort->cmd_type != CMD_IOACCEL2)
2385 return 0; /* no match */
2386
2387 /* point to the ioaccel2 device handle */
2388 c2a = &h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
2389 if (c2a == NULL)
2390 return 0; /* no match */
2391
2392 scmd = (struct scsi_cmnd *) ioaccel2_cmd_to_abort->scsi_cmd;
2393 if (scmd == NULL)
2394 return 0; /* no match */
2395
2396 d = scmd->device->hostdata;
2397 if (d == NULL)
2398 return 0; /* no match */
2399
2400 it_nexus = cpu_to_le32((u32) d->ioaccel_handle);
2401 scsi_nexus = cpu_to_le32((u32) c2a->scsi_nexus);
2402 find = c2a->scsi_nexus;
2403
2404 /* Get the list of physical devices */
2405 physicals = kzalloc(reportsize, GFP_KERNEL);
2406 if (hpsa_scsi_do_report_phys_luns(h, (struct ReportLUNdata *) physicals,
2407 reportsize, extended)) {
2408 dev_err(&h->pdev->dev,
2409 "Can't lookup %s device handle: report physical LUNs failed.\n",
2410 "HP SSD Smart Path");
2411 kfree(physicals);
2412 return 0;
2413 }
2414 nphysicals = be32_to_cpu(*((__be32 *)physicals->LUNListLength)) /
2415 responsesize;
2416
2417
2418 /* find ioaccel2 handle in list of physicals: */
2419 for (i = 0; i < nphysicals; i++) {
2420 /* handle is in bytes 28-31 of each lun */
2421 if (memcmp(&((struct ReportExtendedLUNdata *)
2422 physicals)->LUN[i][20], &find, 4) != 0) {
2423 continue; /* didn't match */
2424 }
2425 found = 1;
2426 memcpy(scsi3addr, &((struct ReportExtendedLUNdata *)
2427 physicals)->LUN[i][0], 8);
2428 break; /* found it */
2429 }
2430
2431 kfree(physicals);
2432 if (found)
2433 return 1;
2434 else
2435 return 0;
2436
2437}
2438/*
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002439 * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
2440 * logdev. The number of luns in physdev and logdev are returned in
2441 * *nphysicals and *nlogicals, respectively.
2442 * Returns 0 on success, -1 otherwise.
2443 */
2444static int hpsa_gather_lun_info(struct ctlr_info *h,
2445 int reportlunsize,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002446 struct ReportLUNdata *physdev, u32 *nphysicals, int *physical_mode,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002447 struct ReportLUNdata *logdev, u32 *nlogicals)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002448{
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002449 int physical_entry_size = 8;
2450
2451 *physical_mode = 0;
2452
2453 /* For I/O accelerator mode we need to read physical device handles */
Mike MIller317d4ad2014-02-18 13:56:20 -06002454 if (h->transMethod & CFGTBL_Trans_io_accel1 ||
2455 h->transMethod & CFGTBL_Trans_io_accel2) {
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002456 *physical_mode = HPSA_REPORT_PHYS_EXTENDED;
2457 physical_entry_size = 24;
2458 }
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002459 if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002460 *physical_mode)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002461 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
2462 return -1;
2463 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002464 *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) /
2465 physical_entry_size;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002466 if (*nphysicals > HPSA_MAX_PHYS_LUN) {
2467 dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
2468 " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2469 *nphysicals - HPSA_MAX_PHYS_LUN);
2470 *nphysicals = HPSA_MAX_PHYS_LUN;
2471 }
2472 if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
2473 dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
2474 return -1;
2475 }
Stephen M. Cameron6df1e952010-02-04 08:42:19 -06002476 *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002477 /* Reject Logicals in excess of our max capability. */
2478 if (*nlogicals > HPSA_MAX_LUN) {
2479 dev_warn(&h->pdev->dev,
2480 "maximum logical LUNs (%d) exceeded. "
2481 "%d LUNs ignored.\n", HPSA_MAX_LUN,
2482 *nlogicals - HPSA_MAX_LUN);
2483 *nlogicals = HPSA_MAX_LUN;
2484 }
2485 if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
2486 dev_warn(&h->pdev->dev,
2487 "maximum logical + physical LUNs (%d) exceeded. "
2488 "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2489 *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
2490 *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
2491 }
2492 return 0;
2493}
2494
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002495u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i,
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002496 int nphysicals, int nlogicals,
2497 struct ReportExtendedLUNdata *physdev_list,
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002498 struct ReportLUNdata *logdev_list)
2499{
2500 /* Helper function, figure out where the LUN ID info is coming from
2501 * given index i, lists of physical and logical devices, where in
2502 * the list the raid controller is supposed to appear (first or last)
2503 */
2504
2505 int logicals_start = nphysicals + (raid_ctlr_position == 0);
2506 int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
2507
2508 if (i == raid_ctlr_position)
2509 return RAID_CTLR_LUNID;
2510
2511 if (i < logicals_start)
2512 return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0];
2513
2514 if (i < last_device)
2515 return &logdev_list->LUN[i - nphysicals -
2516 (raid_ctlr_position == 0)][0];
2517 BUG();
2518 return NULL;
2519}
2520
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002521static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
2522{
2523 /* the idea here is we could get notified
2524 * that some devices have changed, so we do a report
2525 * physical luns and report logical luns cmd, and adjust
2526 * our list of devices accordingly.
2527 *
2528 * The scsi3addr's of devices won't change so long as the
2529 * adapter is not reset. That means we can rescan and
2530 * tell which devices we already know about, vs. new
2531 * devices, vs. disappearing devices.
2532 */
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002533 struct ReportExtendedLUNdata *physdev_list = NULL;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002534 struct ReportLUNdata *logdev_list = NULL;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002535 u32 nphysicals = 0;
2536 u32 nlogicals = 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002537 int physical_mode = 0;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002538 u32 ndev_allocated = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002539 struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
2540 int ncurrent = 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002541 int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 24;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002542 int i, n_ext_target_devs, ndevs_to_allocate;
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002543 int raid_ctlr_position;
Scott Teelaca4a522012-01-19 14:01:19 -06002544 DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002545
Scott Teelcfe5bad2011-10-26 16:21:07 -05002546 currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002547 physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
2548 logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002549 tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
2550
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002551 if (!currentsd || !physdev_list || !logdev_list || !tmpdevice) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002552 dev_err(&h->pdev->dev, "out of memory\n");
2553 goto out;
2554 }
2555 memset(lunzerobits, 0, sizeof(lunzerobits));
2556
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002557 if (hpsa_gather_lun_info(h, reportlunsize,
2558 (struct ReportLUNdata *) physdev_list, &nphysicals,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002559 &physical_mode, logdev_list, &nlogicals))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002560 goto out;
2561
Scott Teelaca4a522012-01-19 14:01:19 -06002562 /* We might see up to the maximum number of logical and physical disks
2563 * plus external target devices, and a device for the local RAID
2564 * controller.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002565 */
Scott Teelaca4a522012-01-19 14:01:19 -06002566 ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002567
2568 /* Allocate the per device structures */
2569 for (i = 0; i < ndevs_to_allocate; i++) {
Scott Teelb7ec0212011-10-26 16:21:12 -05002570 if (i >= HPSA_MAX_DEVICES) {
2571 dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
2572 " %d devices ignored.\n", HPSA_MAX_DEVICES,
2573 ndevs_to_allocate - HPSA_MAX_DEVICES);
2574 break;
2575 }
2576
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002577 currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
2578 if (!currentsd[i]) {
2579 dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
2580 __FILE__, __LINE__);
2581 goto out;
2582 }
2583 ndev_allocated++;
2584 }
2585
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002586 if (unlikely(is_scsi_rev_5(h)))
2587 raid_ctlr_position = 0;
2588 else
2589 raid_ctlr_position = nphysicals + nlogicals;
2590
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002591 /* adjust our table of devices */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002592 n_ext_target_devs = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002593 for (i = 0; i < nphysicals + nlogicals + 1; i++) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002594 u8 *lunaddrbytes, is_OBDR = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002595
2596 /* Figure out where the LUN ID info is coming from */
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002597 lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
2598 i, nphysicals, nlogicals, physdev_list, logdev_list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002599 /* skip masked physical devices. */
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002600 if (lunaddrbytes[3] & 0xC0 &&
2601 i < nphysicals + (raid_ctlr_position == 0))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002602 continue;
2603
2604 /* Get device type, vendor, model, device id */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002605 if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
2606 &is_OBDR))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002607 continue; /* skip it if we can't talk to it. */
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002608 figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002609 this_device = currentsd[ncurrent];
2610
2611 /*
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002612 * For external target devices, we have to insert a LUN 0 which
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002613 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
2614 * is nonetheless an enclosure device there. We have to
2615 * present that otherwise linux won't find anything if
2616 * there is no lun 0.
2617 */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002618 if (add_ext_target_dev(h, tmpdevice, this_device,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002619 lunaddrbytes, lunzerobits,
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002620 &n_ext_target_devs)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002621 ncurrent++;
2622 this_device = currentsd[ncurrent];
2623 }
2624
2625 *this_device = *tmpdevice;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002626
2627 switch (this_device->devtype) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002628 case TYPE_ROM:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002629 /* We don't *really* support actual CD-ROM devices,
2630 * just "One Button Disaster Recovery" tape drive
2631 * which temporarily pretends to be a CD-ROM drive.
2632 * So we check that the device is really an OBDR tape
2633 * device by checking for "$DR-10" in bytes 43-48 of
2634 * the inquiry data.
2635 */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002636 if (is_OBDR)
2637 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002638 break;
2639 case TYPE_DISK:
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002640 if (i >= nphysicals) {
2641 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002642 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002643 }
2644 if (physical_mode == HPSA_REPORT_PHYS_EXTENDED) {
2645 memcpy(&this_device->ioaccel_handle,
2646 &lunaddrbytes[20],
2647 sizeof(this_device->ioaccel_handle));
2648 ncurrent++;
2649 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002650 break;
2651 case TYPE_TAPE:
2652 case TYPE_MEDIUM_CHANGER:
2653 ncurrent++;
2654 break;
2655 case TYPE_RAID:
2656 /* Only present the Smartarray HBA as a RAID controller.
2657 * If it's a RAID controller other than the HBA itself
2658 * (an external RAID controller, MSA500 or similar)
2659 * don't present it.
2660 */
2661 if (!is_hba_lunid(lunaddrbytes))
2662 break;
2663 ncurrent++;
2664 break;
2665 default:
2666 break;
2667 }
Scott Teelcfe5bad2011-10-26 16:21:07 -05002668 if (ncurrent >= HPSA_MAX_DEVICES)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002669 break;
2670 }
2671 adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
2672out:
2673 kfree(tmpdevice);
2674 for (i = 0; i < ndev_allocated; i++)
2675 kfree(currentsd[i]);
2676 kfree(currentsd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002677 kfree(physdev_list);
2678 kfree(logdev_list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002679}
2680
2681/* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
2682 * dma mapping and fills in the scatter gather entries of the
2683 * hpsa command, cp.
2684 */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002685static int hpsa_scatter_gather(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002686 struct CommandList *cp,
2687 struct scsi_cmnd *cmd)
2688{
2689 unsigned int len;
2690 struct scatterlist *sg;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002691 u64 addr64;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002692 int use_sg, i, sg_index, chained;
2693 struct SGDescriptor *curr_sg;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002694
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002695 BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002696
2697 use_sg = scsi_dma_map(cmd);
2698 if (use_sg < 0)
2699 return use_sg;
2700
2701 if (!use_sg)
2702 goto sglist_finished;
2703
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002704 curr_sg = cp->SG;
2705 chained = 0;
2706 sg_index = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002707 scsi_for_each_sg(cmd, sg, use_sg, i) {
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002708 if (i == h->max_cmd_sg_entries - 1 &&
2709 use_sg > h->max_cmd_sg_entries) {
2710 chained = 1;
2711 curr_sg = h->cmd_sg_list[cp->cmdindex];
2712 sg_index = 0;
2713 }
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002714 addr64 = (u64) sg_dma_address(sg);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002715 len = sg_dma_len(sg);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002716 curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
2717 curr_sg->Addr.upper = (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
2718 curr_sg->Len = len;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06002719 curr_sg->Ext = (i < scsi_sg_count(cmd) - 1) ? 0 : HPSA_SG_LAST;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002720 curr_sg++;
2721 }
2722
2723 if (use_sg + chained > h->maxSG)
2724 h->maxSG = use_sg + chained;
2725
2726 if (chained) {
2727 cp->Header.SGList = h->max_cmd_sg_entries;
2728 cp->Header.SGTotal = (u16) (use_sg + 1);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002729 if (hpsa_map_sg_chain_block(h, cp)) {
2730 scsi_dma_unmap(cmd);
2731 return -1;
2732 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002733 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002734 }
2735
2736sglist_finished:
2737
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002738 cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
2739 cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002740 return 0;
2741}
2742
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002743#define IO_ACCEL_INELIGIBLE (1)
2744static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
2745{
2746 int is_write = 0;
2747 u32 block;
2748 u32 block_cnt;
2749
2750 /* Perform some CDB fixups if needed using 10 byte reads/writes only */
2751 switch (cdb[0]) {
2752 case WRITE_6:
2753 case WRITE_12:
2754 is_write = 1;
2755 case READ_6:
2756 case READ_12:
2757 if (*cdb_len == 6) {
2758 block = (((u32) cdb[2]) << 8) | cdb[3];
2759 block_cnt = cdb[4];
2760 } else {
2761 BUG_ON(*cdb_len != 12);
2762 block = (((u32) cdb[2]) << 24) |
2763 (((u32) cdb[3]) << 16) |
2764 (((u32) cdb[4]) << 8) |
2765 cdb[5];
2766 block_cnt =
2767 (((u32) cdb[6]) << 24) |
2768 (((u32) cdb[7]) << 16) |
2769 (((u32) cdb[8]) << 8) |
2770 cdb[9];
2771 }
2772 if (block_cnt > 0xffff)
2773 return IO_ACCEL_INELIGIBLE;
2774
2775 cdb[0] = is_write ? WRITE_10 : READ_10;
2776 cdb[1] = 0;
2777 cdb[2] = (u8) (block >> 24);
2778 cdb[3] = (u8) (block >> 16);
2779 cdb[4] = (u8) (block >> 8);
2780 cdb[5] = (u8) (block);
2781 cdb[6] = 0;
2782 cdb[7] = (u8) (block_cnt >> 8);
2783 cdb[8] = (u8) (block_cnt);
2784 cdb[9] = 0;
2785 *cdb_len = 10;
2786 break;
2787 }
2788 return 0;
2789}
2790
Scott Teelc3497752014-02-18 13:56:34 -06002791static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002792 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
2793 u8 *scsi3addr)
Matt Gatese1f7de02014-02-18 13:55:17 -06002794{
2795 struct scsi_cmnd *cmd = c->scsi_cmd;
Matt Gatese1f7de02014-02-18 13:55:17 -06002796 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
2797 unsigned int len;
2798 unsigned int total_len = 0;
2799 struct scatterlist *sg;
2800 u64 addr64;
2801 int use_sg, i;
2802 struct SGDescriptor *curr_sg;
2803 u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
2804
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002805 /* TODO: implement chaining support */
2806 if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
2807 return IO_ACCEL_INELIGIBLE;
2808
Matt Gatese1f7de02014-02-18 13:55:17 -06002809 BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
2810
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002811 if (fixup_ioaccel_cdb(cdb, &cdb_len))
2812 return IO_ACCEL_INELIGIBLE;
2813
Matt Gatese1f7de02014-02-18 13:55:17 -06002814 c->cmd_type = CMD_IOACCEL1;
2815
2816 /* Adjust the DMA address to point to the accelerated command buffer */
2817 c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
2818 (c->cmdindex * sizeof(*cp));
2819 BUG_ON(c->busaddr & 0x0000007F);
2820
2821 use_sg = scsi_dma_map(cmd);
2822 if (use_sg < 0)
2823 return use_sg;
2824
2825 if (use_sg) {
2826 curr_sg = cp->SG;
2827 scsi_for_each_sg(cmd, sg, use_sg, i) {
2828 addr64 = (u64) sg_dma_address(sg);
2829 len = sg_dma_len(sg);
2830 total_len += len;
2831 curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
2832 curr_sg->Addr.upper =
2833 (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
2834 curr_sg->Len = len;
2835
2836 if (i == (scsi_sg_count(cmd) - 1))
2837 curr_sg->Ext = HPSA_SG_LAST;
2838 else
2839 curr_sg->Ext = 0; /* we are not chaining */
2840 curr_sg++;
2841 }
2842
2843 switch (cmd->sc_data_direction) {
2844 case DMA_TO_DEVICE:
2845 control |= IOACCEL1_CONTROL_DATA_OUT;
2846 break;
2847 case DMA_FROM_DEVICE:
2848 control |= IOACCEL1_CONTROL_DATA_IN;
2849 break;
2850 case DMA_NONE:
2851 control |= IOACCEL1_CONTROL_NODATAXFER;
2852 break;
2853 default:
2854 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
2855 cmd->sc_data_direction);
2856 BUG();
2857 break;
2858 }
2859 } else {
2860 control |= IOACCEL1_CONTROL_NODATAXFER;
2861 }
2862
Scott Teelc3497752014-02-18 13:56:34 -06002863 c->Header.SGList = use_sg;
Matt Gatese1f7de02014-02-18 13:55:17 -06002864 /* Fill out the command structure to submit */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002865 cp->dev_handle = ioaccel_handle & 0xFFFF;
Matt Gatese1f7de02014-02-18 13:55:17 -06002866 cp->transfer_len = total_len;
2867 cp->io_flags = IOACCEL1_IOFLAGS_IO_REQ |
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002868 (cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK);
Matt Gatese1f7de02014-02-18 13:55:17 -06002869 cp->control = control;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002870 memcpy(cp->CDB, cdb, cdb_len);
2871 memcpy(cp->CISS_LUN, scsi3addr, 8);
Scott Teelc3497752014-02-18 13:56:34 -06002872 /* Tag was already set at init time. */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002873 enqueue_cmd_and_start_io(h, c);
Matt Gatese1f7de02014-02-18 13:55:17 -06002874 return 0;
2875}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002876
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002877/*
2878 * Queue a command directly to a device behind the controller using the
2879 * I/O accelerator path.
2880 */
2881static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
2882 struct CommandList *c)
2883{
2884 struct scsi_cmnd *cmd = c->scsi_cmd;
2885 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
2886
2887 return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
2888 cmd->cmnd, cmd->cmd_len, dev->scsi3addr);
2889}
2890
Scott Teelc3497752014-02-18 13:56:34 -06002891static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
2892 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
2893 u8 *scsi3addr)
2894{
2895 struct scsi_cmnd *cmd = c->scsi_cmd;
2896 struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
2897 struct ioaccel2_sg_element *curr_sg;
2898 int use_sg, i;
2899 struct scatterlist *sg;
2900 u64 addr64;
2901 u32 len;
2902 u32 total_len = 0;
2903
2904 if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
2905 return IO_ACCEL_INELIGIBLE;
2906
2907 if (fixup_ioaccel_cdb(cdb, &cdb_len))
2908 return IO_ACCEL_INELIGIBLE;
2909 c->cmd_type = CMD_IOACCEL2;
2910 /* Adjust the DMA address to point to the accelerated command buffer */
2911 c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
2912 (c->cmdindex * sizeof(*cp));
2913 BUG_ON(c->busaddr & 0x0000007F);
2914
2915 memset(cp, 0, sizeof(*cp));
2916 cp->IU_type = IOACCEL2_IU_TYPE;
2917
2918 use_sg = scsi_dma_map(cmd);
2919 if (use_sg < 0)
2920 return use_sg;
2921
2922 if (use_sg) {
2923 BUG_ON(use_sg > IOACCEL2_MAXSGENTRIES);
2924 curr_sg = cp->sg;
2925 scsi_for_each_sg(cmd, sg, use_sg, i) {
2926 addr64 = (u64) sg_dma_address(sg);
2927 len = sg_dma_len(sg);
2928 total_len += len;
2929 curr_sg->address = cpu_to_le64(addr64);
2930 curr_sg->length = cpu_to_le32(len);
2931 curr_sg->reserved[0] = 0;
2932 curr_sg->reserved[1] = 0;
2933 curr_sg->reserved[2] = 0;
2934 curr_sg->chain_indicator = 0;
2935 curr_sg++;
2936 }
2937
2938 switch (cmd->sc_data_direction) {
2939 case DMA_TO_DEVICE:
2940 cp->direction = IOACCEL2_DIR_DATA_OUT;
2941 break;
2942 case DMA_FROM_DEVICE:
2943 cp->direction = IOACCEL2_DIR_DATA_IN;
2944 break;
2945 case DMA_NONE:
2946 cp->direction = IOACCEL2_DIR_NO_DATA;
2947 break;
2948 default:
2949 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
2950 cmd->sc_data_direction);
2951 BUG();
2952 break;
2953 }
2954 } else {
2955 cp->direction = IOACCEL2_DIR_NO_DATA;
2956 }
2957 cp->scsi_nexus = ioaccel_handle;
2958 cp->Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT) |
2959 DIRECT_LOOKUP_BIT;
2960 memcpy(cp->cdb, cdb, sizeof(cp->cdb));
2961 memset(cp->cciss_lun, 0, sizeof(cp->cciss_lun));
2962 cp->cmd_priority_task_attr = 0;
2963
2964 /* fill in sg elements */
2965 cp->sg_count = (u8) use_sg;
2966
2967 cp->data_len = cpu_to_le32(total_len);
2968 cp->err_ptr = cpu_to_le64(c->busaddr +
2969 offsetof(struct io_accel2_cmd, error_data));
2970 cp->err_len = cpu_to_le32((u32) sizeof(cp->error_data));
2971
2972 enqueue_cmd_and_start_io(h, c);
2973 return 0;
2974}
2975
2976/*
2977 * Queue a command to the correct I/O accelerator path.
2978 */
2979static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
2980 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
2981 u8 *scsi3addr)
2982{
2983 if (h->transMethod & CFGTBL_Trans_io_accel1)
2984 return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
2985 cdb, cdb_len, scsi3addr);
2986 else
2987 return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
2988 cdb, cdb_len, scsi3addr);
2989}
2990
Scott Teel6b80b182014-02-18 13:56:55 -06002991static void raid_map_helper(struct raid_map_data *map,
2992 int offload_to_mirror, u32 *map_index, u32 *current_group)
2993{
2994 if (offload_to_mirror == 0) {
2995 /* use physical disk in the first mirrored group. */
2996 *map_index %= map->data_disks_per_row;
2997 return;
2998 }
2999 do {
3000 /* determine mirror group that *map_index indicates */
3001 *current_group = *map_index / map->data_disks_per_row;
3002 if (offload_to_mirror == *current_group)
3003 continue;
3004 if (*current_group < (map->layout_map_count - 1)) {
3005 /* select map index from next group */
3006 *map_index += map->data_disks_per_row;
3007 (*current_group)++;
3008 } else {
3009 /* select map index from first group */
3010 *map_index %= map->data_disks_per_row;
3011 *current_group = 0;
3012 }
3013 } while (offload_to_mirror != *current_group);
3014}
3015
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003016/*
3017 * Attempt to perform offload RAID mapping for a logical volume I/O.
3018 */
3019static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
3020 struct CommandList *c)
3021{
3022 struct scsi_cmnd *cmd = c->scsi_cmd;
3023 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3024 struct raid_map_data *map = &dev->raid_map;
3025 struct raid_map_disk_data *dd = &map->data[0];
3026 int is_write = 0;
3027 u32 map_index;
3028 u64 first_block, last_block;
3029 u32 block_cnt;
3030 u32 blocks_per_row;
3031 u64 first_row, last_row;
3032 u32 first_row_offset, last_row_offset;
3033 u32 first_column, last_column;
Scott Teel6b80b182014-02-18 13:56:55 -06003034 u64 r0_first_row, r0_last_row;
3035 u32 r5or6_blocks_per_row;
3036 u64 r5or6_first_row, r5or6_last_row;
3037 u32 r5or6_first_row_offset, r5or6_last_row_offset;
3038 u32 r5or6_first_column, r5or6_last_column;
3039 u32 total_disks_per_row;
3040 u32 stripesize;
3041 u32 first_group, last_group, current_group;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003042 u32 map_row;
3043 u32 disk_handle;
3044 u64 disk_block;
3045 u32 disk_block_cnt;
3046 u8 cdb[16];
3047 u8 cdb_len;
3048#if BITS_PER_LONG == 32
3049 u64 tmpdiv;
3050#endif
Scott Teel6b80b182014-02-18 13:56:55 -06003051 int offload_to_mirror;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003052
3053 BUG_ON(!(dev->offload_config && dev->offload_enabled));
3054
3055 /* check for valid opcode, get LBA and block count */
3056 switch (cmd->cmnd[0]) {
3057 case WRITE_6:
3058 is_write = 1;
3059 case READ_6:
3060 first_block =
3061 (((u64) cmd->cmnd[2]) << 8) |
3062 cmd->cmnd[3];
3063 block_cnt = cmd->cmnd[4];
3064 break;
3065 case WRITE_10:
3066 is_write = 1;
3067 case READ_10:
3068 first_block =
3069 (((u64) cmd->cmnd[2]) << 24) |
3070 (((u64) cmd->cmnd[3]) << 16) |
3071 (((u64) cmd->cmnd[4]) << 8) |
3072 cmd->cmnd[5];
3073 block_cnt =
3074 (((u32) cmd->cmnd[7]) << 8) |
3075 cmd->cmnd[8];
3076 break;
3077 case WRITE_12:
3078 is_write = 1;
3079 case READ_12:
3080 first_block =
3081 (((u64) cmd->cmnd[2]) << 24) |
3082 (((u64) cmd->cmnd[3]) << 16) |
3083 (((u64) cmd->cmnd[4]) << 8) |
3084 cmd->cmnd[5];
3085 block_cnt =
3086 (((u32) cmd->cmnd[6]) << 24) |
3087 (((u32) cmd->cmnd[7]) << 16) |
3088 (((u32) cmd->cmnd[8]) << 8) |
3089 cmd->cmnd[9];
3090 break;
3091 case WRITE_16:
3092 is_write = 1;
3093 case READ_16:
3094 first_block =
3095 (((u64) cmd->cmnd[2]) << 56) |
3096 (((u64) cmd->cmnd[3]) << 48) |
3097 (((u64) cmd->cmnd[4]) << 40) |
3098 (((u64) cmd->cmnd[5]) << 32) |
3099 (((u64) cmd->cmnd[6]) << 24) |
3100 (((u64) cmd->cmnd[7]) << 16) |
3101 (((u64) cmd->cmnd[8]) << 8) |
3102 cmd->cmnd[9];
3103 block_cnt =
3104 (((u32) cmd->cmnd[10]) << 24) |
3105 (((u32) cmd->cmnd[11]) << 16) |
3106 (((u32) cmd->cmnd[12]) << 8) |
3107 cmd->cmnd[13];
3108 break;
3109 default:
3110 return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
3111 }
3112 BUG_ON(block_cnt == 0);
3113 last_block = first_block + block_cnt - 1;
3114
3115 /* check for write to non-RAID-0 */
3116 if (is_write && dev->raid_level != 0)
3117 return IO_ACCEL_INELIGIBLE;
3118
3119 /* check for invalid block or wraparound */
3120 if (last_block >= map->volume_blk_cnt || last_block < first_block)
3121 return IO_ACCEL_INELIGIBLE;
3122
3123 /* calculate stripe information for the request */
3124 blocks_per_row = map->data_disks_per_row * map->strip_size;
3125#if BITS_PER_LONG == 32
3126 tmpdiv = first_block;
3127 (void) do_div(tmpdiv, blocks_per_row);
3128 first_row = tmpdiv;
3129 tmpdiv = last_block;
3130 (void) do_div(tmpdiv, blocks_per_row);
3131 last_row = tmpdiv;
3132 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
3133 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
3134 tmpdiv = first_row_offset;
3135 (void) do_div(tmpdiv, map->strip_size);
3136 first_column = tmpdiv;
3137 tmpdiv = last_row_offset;
3138 (void) do_div(tmpdiv, map->strip_size);
3139 last_column = tmpdiv;
3140#else
3141 first_row = first_block / blocks_per_row;
3142 last_row = last_block / blocks_per_row;
3143 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
3144 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
3145 first_column = first_row_offset / map->strip_size;
3146 last_column = last_row_offset / map->strip_size;
3147#endif
3148
3149 /* if this isn't a single row/column then give to the controller */
3150 if ((first_row != last_row) || (first_column != last_column))
3151 return IO_ACCEL_INELIGIBLE;
3152
3153 /* proceeding with driver mapping */
Scott Teel6b80b182014-02-18 13:56:55 -06003154 total_disks_per_row = map->data_disks_per_row +
3155 map->metadata_disks_per_row;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003156 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
3157 map->row_cnt;
Scott Teel6b80b182014-02-18 13:56:55 -06003158 map_index = (map_row * total_disks_per_row) + first_column;
3159
3160 switch (dev->raid_level) {
3161 case HPSA_RAID_0:
3162 break; /* nothing special to do */
3163 case HPSA_RAID_1:
3164 /* Handles load balance across RAID 1 members.
3165 * (2-drive R1 and R10 with even # of drives.)
3166 * Appropriate for SSDs, not optimal for HDDs
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003167 */
Scott Teel6b80b182014-02-18 13:56:55 -06003168 BUG_ON(map->layout_map_count != 2);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003169 if (dev->offload_to_mirror)
3170 map_index += map->data_disks_per_row;
3171 dev->offload_to_mirror = !dev->offload_to_mirror;
Scott Teel6b80b182014-02-18 13:56:55 -06003172 break;
3173 case HPSA_RAID_ADM:
3174 /* Handles N-way mirrors (R1-ADM)
3175 * and R10 with # of drives divisible by 3.)
3176 */
3177 BUG_ON(map->layout_map_count != 3);
3178
3179 offload_to_mirror = dev->offload_to_mirror;
3180 raid_map_helper(map, offload_to_mirror,
3181 &map_index, &current_group);
3182 /* set mirror group to use next time */
3183 offload_to_mirror =
3184 (offload_to_mirror >= map->layout_map_count - 1)
3185 ? 0 : offload_to_mirror + 1;
3186 /* FIXME: remove after debug/dev */
3187 BUG_ON(offload_to_mirror >= map->layout_map_count);
3188 dev_warn(&h->pdev->dev,
3189 "DEBUG: Using physical disk map index %d from mirror group %d\n",
3190 map_index, offload_to_mirror);
3191 dev->offload_to_mirror = offload_to_mirror;
3192 /* Avoid direct use of dev->offload_to_mirror within this
3193 * function since multiple threads might simultaneously
3194 * increment it beyond the range of dev->layout_map_count -1.
3195 */
3196 break;
3197 case HPSA_RAID_5:
3198 case HPSA_RAID_6:
3199 if (map->layout_map_count <= 1)
3200 break;
3201
3202 /* Verify first and last block are in same RAID group */
3203 r5or6_blocks_per_row =
3204 map->strip_size * map->data_disks_per_row;
3205 BUG_ON(r5or6_blocks_per_row == 0);
3206 stripesize = r5or6_blocks_per_row * map->layout_map_count;
3207#if BITS_PER_LONG == 32
3208 tmpdiv = first_block;
3209 first_group = do_div(tmpdiv, stripesize);
3210 tmpdiv = first_group;
3211 (void) do_div(tmpdiv, r5or6_blocks_per_row);
3212 first_group = tmpdiv;
3213 tmpdiv = last_block;
3214 last_group = do_div(tmpdiv, stripesize);
3215 tmpdiv = last_group;
3216 (void) do_div(tmpdiv, r5or6_blocks_per_row);
3217 last_group = tmpdiv;
3218#else
3219 first_group = (first_block % stripesize) / r5or6_blocks_per_row;
3220 last_group = (last_block % stripesize) / r5or6_blocks_per_row;
3221 if (first_group != last_group)
3222#endif
3223 return IO_ACCEL_INELIGIBLE;
3224
3225 /* Verify request is in a single row of RAID 5/6 */
3226#if BITS_PER_LONG == 32
3227 tmpdiv = first_block;
3228 (void) do_div(tmpdiv, stripesize);
3229 first_row = r5or6_first_row = r0_first_row = tmpdiv;
3230 tmpdiv = last_block;
3231 (void) do_div(tmpdiv, stripesize);
3232 r5or6_last_row = r0_last_row = tmpdiv;
3233#else
3234 first_row = r5or6_first_row = r0_first_row =
3235 first_block / stripesize;
3236 r5or6_last_row = r0_last_row = last_block / stripesize;
3237#endif
3238 if (r5or6_first_row != r5or6_last_row)
3239 return IO_ACCEL_INELIGIBLE;
3240
3241
3242 /* Verify request is in a single column */
3243#if BITS_PER_LONG == 32
3244 tmpdiv = first_block;
3245 first_row_offset = do_div(tmpdiv, stripesize);
3246 tmpdiv = first_row_offset;
3247 first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
3248 r5or6_first_row_offset = first_row_offset;
3249 tmpdiv = last_block;
3250 r5or6_last_row_offset = do_div(tmpdiv, stripesize);
3251 tmpdiv = r5or6_last_row_offset;
3252 r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
3253 tmpdiv = r5or6_first_row_offset;
3254 (void) do_div(tmpdiv, map->strip_size);
3255 first_column = r5or6_first_column = tmpdiv;
3256 tmpdiv = r5or6_last_row_offset;
3257 (void) do_div(tmpdiv, map->strip_size);
3258 r5or6_last_column = tmpdiv;
3259#else
3260 first_row_offset = r5or6_first_row_offset =
3261 (u32)((first_block % stripesize) %
3262 r5or6_blocks_per_row);
3263
3264 r5or6_last_row_offset =
3265 (u32)((last_block % stripesize) %
3266 r5or6_blocks_per_row);
3267
3268 first_column = r5or6_first_column =
3269 r5or6_first_row_offset / map->strip_size;
3270 r5or6_last_column =
3271 r5or6_last_row_offset / map->strip_size;
3272#endif
3273 if (r5or6_first_column != r5or6_last_column)
3274 return IO_ACCEL_INELIGIBLE;
3275
3276 /* Request is eligible */
3277 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
3278 map->row_cnt;
3279
3280 map_index = (first_group *
3281 (map->row_cnt * total_disks_per_row)) +
3282 (map_row * total_disks_per_row) + first_column;
3283 break;
3284 default:
3285 return IO_ACCEL_INELIGIBLE;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003286 }
Scott Teel6b80b182014-02-18 13:56:55 -06003287
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003288 disk_handle = dd[map_index].ioaccel_handle;
3289 disk_block = map->disk_starting_blk + (first_row * map->strip_size) +
3290 (first_row_offset - (first_column * map->strip_size));
3291 disk_block_cnt = block_cnt;
3292
3293 /* handle differing logical/physical block sizes */
3294 if (map->phys_blk_shift) {
3295 disk_block <<= map->phys_blk_shift;
3296 disk_block_cnt <<= map->phys_blk_shift;
3297 }
3298 BUG_ON(disk_block_cnt > 0xffff);
3299
3300 /* build the new CDB for the physical disk I/O */
3301 if (disk_block > 0xffffffff) {
3302 cdb[0] = is_write ? WRITE_16 : READ_16;
3303 cdb[1] = 0;
3304 cdb[2] = (u8) (disk_block >> 56);
3305 cdb[3] = (u8) (disk_block >> 48);
3306 cdb[4] = (u8) (disk_block >> 40);
3307 cdb[5] = (u8) (disk_block >> 32);
3308 cdb[6] = (u8) (disk_block >> 24);
3309 cdb[7] = (u8) (disk_block >> 16);
3310 cdb[8] = (u8) (disk_block >> 8);
3311 cdb[9] = (u8) (disk_block);
3312 cdb[10] = (u8) (disk_block_cnt >> 24);
3313 cdb[11] = (u8) (disk_block_cnt >> 16);
3314 cdb[12] = (u8) (disk_block_cnt >> 8);
3315 cdb[13] = (u8) (disk_block_cnt);
3316 cdb[14] = 0;
3317 cdb[15] = 0;
3318 cdb_len = 16;
3319 } else {
3320 cdb[0] = is_write ? WRITE_10 : READ_10;
3321 cdb[1] = 0;
3322 cdb[2] = (u8) (disk_block >> 24);
3323 cdb[3] = (u8) (disk_block >> 16);
3324 cdb[4] = (u8) (disk_block >> 8);
3325 cdb[5] = (u8) (disk_block);
3326 cdb[6] = 0;
3327 cdb[7] = (u8) (disk_block_cnt >> 8);
3328 cdb[8] = (u8) (disk_block_cnt);
3329 cdb[9] = 0;
3330 cdb_len = 10;
3331 }
3332 return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
3333 dev->scsi3addr);
3334}
3335
Jeff Garzikf2812332010-11-16 02:10:29 -05003336static int hpsa_scsi_queue_command_lck(struct scsi_cmnd *cmd,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003337 void (*done)(struct scsi_cmnd *))
3338{
3339 struct ctlr_info *h;
3340 struct hpsa_scsi_dev_t *dev;
3341 unsigned char scsi3addr[8];
3342 struct CommandList *c;
3343 unsigned long flags;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003344 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003345
3346 /* Get the ptr to our adapter structure out of cmd->host. */
3347 h = sdev_to_hba(cmd->device);
3348 dev = cmd->device->hostdata;
3349 if (!dev) {
3350 cmd->result = DID_NO_CONNECT << 16;
3351 done(cmd);
3352 return 0;
3353 }
3354 memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
3355
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003356 spin_lock_irqsave(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05003357 if (unlikely(h->lockup_detected)) {
3358 spin_unlock_irqrestore(&h->lock, flags);
3359 cmd->result = DID_ERROR << 16;
3360 done(cmd);
3361 return 0;
3362 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003363 spin_unlock_irqrestore(&h->lock, flags);
Matt Gatese16a33a2012-05-01 11:43:11 -05003364 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003365 if (c == NULL) { /* trouble... */
3366 dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
3367 return SCSI_MLQUEUE_HOST_BUSY;
3368 }
3369
3370 /* Fill in the command list header */
3371
3372 cmd->scsi_done = done; /* save this for use by completion code */
3373
3374 /* save c in case we have to abort it */
3375 cmd->host_scribble = (unsigned char *) c;
3376
3377 c->cmd_type = CMD_SCSI;
3378 c->scsi_cmd = cmd;
Matt Gatese1f7de02014-02-18 13:55:17 -06003379
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003380 /* Call alternate submit routine for I/O accelerated commands.
3381 * Retries always go down the normal I/O path.
3382 */
3383 if (likely(cmd->retries == 0 &&
Scott Teelda0697b2014-02-18 13:57:00 -06003384 cmd->request->cmd_type == REQ_TYPE_FS &&
3385 h->acciopath_status)) {
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003386 if (dev->offload_enabled) {
3387 rc = hpsa_scsi_ioaccel_raid_map(h, c);
3388 if (rc == 0)
3389 return 0; /* Sent on ioaccel path */
3390 if (rc < 0) { /* scsi_dma_map failed. */
3391 cmd_free(h, c);
3392 return SCSI_MLQUEUE_HOST_BUSY;
3393 }
3394 } else if (dev->ioaccel_handle) {
3395 rc = hpsa_scsi_ioaccel_direct_map(h, c);
3396 if (rc == 0)
3397 return 0; /* Sent on direct map path */
3398 if (rc < 0) { /* scsi_dma_map failed. */
3399 cmd_free(h, c);
3400 return SCSI_MLQUEUE_HOST_BUSY;
3401 }
3402 }
3403 }
Matt Gatese1f7de02014-02-18 13:55:17 -06003404
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003405 c->Header.ReplyQueue = 0; /* unused in simple mode */
3406 memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
Don Brace303932f2010-02-04 08:42:40 -06003407 c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT);
3408 c->Header.Tag.lower |= DIRECT_LOOKUP_BIT;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003409
3410 /* Fill in the request block... */
3411
3412 c->Request.Timeout = 0;
3413 memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
3414 BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
3415 c->Request.CDBLen = cmd->cmd_len;
3416 memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
3417 c->Request.Type.Type = TYPE_CMD;
3418 c->Request.Type.Attribute = ATTR_SIMPLE;
3419 switch (cmd->sc_data_direction) {
3420 case DMA_TO_DEVICE:
3421 c->Request.Type.Direction = XFER_WRITE;
3422 break;
3423 case DMA_FROM_DEVICE:
3424 c->Request.Type.Direction = XFER_READ;
3425 break;
3426 case DMA_NONE:
3427 c->Request.Type.Direction = XFER_NONE;
3428 break;
3429 case DMA_BIDIRECTIONAL:
3430 /* This can happen if a buggy application does a scsi passthru
3431 * and sets both inlen and outlen to non-zero. ( see
3432 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
3433 */
3434
3435 c->Request.Type.Direction = XFER_RSVD;
3436 /* This is technically wrong, and hpsa controllers should
3437 * reject it with CMD_INVALID, which is the most correct
3438 * response, but non-fibre backends appear to let it
3439 * slide by, and give the same results as if this field
3440 * were set correctly. Either way is acceptable for
3441 * our purposes here.
3442 */
3443
3444 break;
3445
3446 default:
3447 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3448 cmd->sc_data_direction);
3449 BUG();
3450 break;
3451 }
3452
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06003453 if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003454 cmd_free(h, c);
3455 return SCSI_MLQUEUE_HOST_BUSY;
3456 }
3457 enqueue_cmd_and_start_io(h, c);
3458 /* the cmd'll come back via intr handler in complete_scsi_command() */
3459 return 0;
3460}
3461
Jeff Garzikf2812332010-11-16 02:10:29 -05003462static DEF_SCSI_QCMD(hpsa_scsi_queue_command)
3463
Stephen M. Cameron5f389362014-02-18 13:55:48 -06003464static int do_not_scan_if_controller_locked_up(struct ctlr_info *h)
3465{
3466 unsigned long flags;
3467
3468 /*
3469 * Don't let rescans be initiated on a controller known
3470 * to be locked up. If the controller locks up *during*
3471 * a rescan, that thread is probably hosed, but at least
3472 * we can prevent new rescan threads from piling up on a
3473 * locked up controller.
3474 */
3475 spin_lock_irqsave(&h->lock, flags);
3476 if (unlikely(h->lockup_detected)) {
3477 spin_unlock_irqrestore(&h->lock, flags);
3478 spin_lock_irqsave(&h->scan_lock, flags);
3479 h->scan_finished = 1;
3480 wake_up_all(&h->scan_wait_queue);
3481 spin_unlock_irqrestore(&h->scan_lock, flags);
3482 return 1;
3483 }
3484 spin_unlock_irqrestore(&h->lock, flags);
3485 return 0;
3486}
3487
Stephen M. Camerona08a84712010-02-04 08:43:16 -06003488static void hpsa_scan_start(struct Scsi_Host *sh)
3489{
3490 struct ctlr_info *h = shost_to_hba(sh);
3491 unsigned long flags;
3492
Stephen M. Cameron5f389362014-02-18 13:55:48 -06003493 if (do_not_scan_if_controller_locked_up(h))
3494 return;
3495
Stephen M. Camerona08a84712010-02-04 08:43:16 -06003496 /* wait until any scan already in progress is finished. */
3497 while (1) {
3498 spin_lock_irqsave(&h->scan_lock, flags);
3499 if (h->scan_finished)
3500 break;
3501 spin_unlock_irqrestore(&h->scan_lock, flags);
3502 wait_event(h->scan_wait_queue, h->scan_finished);
3503 /* Note: We don't need to worry about a race between this
3504 * thread and driver unload because the midlayer will
3505 * have incremented the reference count, so unload won't
3506 * happen if we're in here.
3507 */
3508 }
3509 h->scan_finished = 0; /* mark scan as in progress */
3510 spin_unlock_irqrestore(&h->scan_lock, flags);
3511
Stephen M. Cameron5f389362014-02-18 13:55:48 -06003512 if (do_not_scan_if_controller_locked_up(h))
3513 return;
3514
Stephen M. Camerona08a84712010-02-04 08:43:16 -06003515 hpsa_update_scsi_devices(h, h->scsi_host->host_no);
3516
3517 spin_lock_irqsave(&h->scan_lock, flags);
3518 h->scan_finished = 1; /* mark scan as finished. */
3519 wake_up_all(&h->scan_wait_queue);
3520 spin_unlock_irqrestore(&h->scan_lock, flags);
3521}
3522
3523static int hpsa_scan_finished(struct Scsi_Host *sh,
3524 unsigned long elapsed_time)
3525{
3526 struct ctlr_info *h = shost_to_hba(sh);
3527 unsigned long flags;
3528 int finished;
3529
3530 spin_lock_irqsave(&h->scan_lock, flags);
3531 finished = h->scan_finished;
3532 spin_unlock_irqrestore(&h->scan_lock, flags);
3533 return finished;
3534}
3535
Stephen M. Cameron667e23d2010-02-25 14:02:51 -06003536static int hpsa_change_queue_depth(struct scsi_device *sdev,
3537 int qdepth, int reason)
3538{
3539 struct ctlr_info *h = sdev_to_hba(sdev);
3540
3541 if (reason != SCSI_QDEPTH_DEFAULT)
3542 return -ENOTSUPP;
3543
3544 if (qdepth < 1)
3545 qdepth = 1;
3546 else
3547 if (qdepth > h->nr_cmds)
3548 qdepth = h->nr_cmds;
3549 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
3550 return sdev->queue_depth;
3551}
3552
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003553static void hpsa_unregister_scsi(struct ctlr_info *h)
3554{
3555 /* we are being forcibly unloaded, and may not refuse. */
3556 scsi_remove_host(h->scsi_host);
3557 scsi_host_put(h->scsi_host);
3558 h->scsi_host = NULL;
3559}
3560
3561static int hpsa_register_scsi(struct ctlr_info *h)
3562{
Stephen M. Cameronb7056902012-01-19 14:00:53 -06003563 struct Scsi_Host *sh;
3564 int error;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003565
Stephen M. Cameronb7056902012-01-19 14:00:53 -06003566 sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
3567 if (sh == NULL)
3568 goto fail;
3569
3570 sh->io_port = 0;
3571 sh->n_io_port = 0;
3572 sh->this_id = -1;
3573 sh->max_channel = 3;
3574 sh->max_cmd_len = MAX_COMMAND_SIZE;
3575 sh->max_lun = HPSA_MAX_LUN;
3576 sh->max_id = HPSA_MAX_LUN;
3577 sh->can_queue = h->nr_cmds;
3578 sh->cmd_per_lun = h->nr_cmds;
3579 sh->sg_tablesize = h->maxsgentries;
3580 h->scsi_host = sh;
3581 sh->hostdata[0] = (unsigned long) h;
3582 sh->irq = h->intr[h->intr_mode];
3583 sh->unique_id = sh->irq;
3584 error = scsi_add_host(sh, &h->pdev->dev);
3585 if (error)
3586 goto fail_host_put;
3587 scsi_scan_host(sh);
3588 return 0;
3589
3590 fail_host_put:
3591 dev_err(&h->pdev->dev, "%s: scsi_add_host"
3592 " failed for controller %d\n", __func__, h->ctlr);
3593 scsi_host_put(sh);
3594 return error;
3595 fail:
3596 dev_err(&h->pdev->dev, "%s: scsi_host_alloc"
3597 " failed for controller %d\n", __func__, h->ctlr);
3598 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003599}
3600
3601static int wait_for_device_to_become_ready(struct ctlr_info *h,
3602 unsigned char lunaddr[])
3603{
3604 int rc = 0;
3605 int count = 0;
3606 int waittime = 1; /* seconds */
3607 struct CommandList *c;
3608
3609 c = cmd_special_alloc(h);
3610 if (!c) {
3611 dev_warn(&h->pdev->dev, "out of memory in "
3612 "wait_for_device_to_become_ready.\n");
3613 return IO_ERROR;
3614 }
3615
3616 /* Send test unit ready until device ready, or give up. */
3617 while (count < HPSA_TUR_RETRY_LIMIT) {
3618
3619 /* Wait for a bit. do this first, because if we send
3620 * the TUR right away, the reset will just abort it.
3621 */
3622 msleep(1000 * waittime);
3623 count++;
3624
3625 /* Increase wait time with each try, up to a point. */
3626 if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
3627 waittime = waittime * 2;
3628
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003629 /* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
3630 (void) fill_cmd(c, TEST_UNIT_READY, h,
3631 NULL, 0, 0, lunaddr, TYPE_CMD);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003632 hpsa_scsi_do_simple_cmd_core(h, c);
3633 /* no unmap needed here because no data xfer. */
3634
3635 if (c->err_info->CommandStatus == CMD_SUCCESS)
3636 break;
3637
3638 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
3639 c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
3640 (c->err_info->SenseInfo[2] == NO_SENSE ||
3641 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
3642 break;
3643
3644 dev_warn(&h->pdev->dev, "waiting %d secs "
3645 "for device to become ready.\n", waittime);
3646 rc = 1; /* device not ready. */
3647 }
3648
3649 if (rc)
3650 dev_warn(&h->pdev->dev, "giving up on device.\n");
3651 else
3652 dev_warn(&h->pdev->dev, "device is ready.\n");
3653
3654 cmd_special_free(h, c);
3655 return rc;
3656}
3657
3658/* Need at least one of these error handlers to keep ../scsi/hosts.c from
3659 * complaining. Doing a host- or bus-reset can't do anything good here.
3660 */
3661static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
3662{
3663 int rc;
3664 struct ctlr_info *h;
3665 struct hpsa_scsi_dev_t *dev;
3666
3667 /* find the controller to which the command to be aborted was sent */
3668 h = sdev_to_hba(scsicmd->device);
3669 if (h == NULL) /* paranoia */
3670 return FAILED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003671 dev = scsicmd->device->hostdata;
3672 if (!dev) {
3673 dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
3674 "device lookup failed.\n");
3675 return FAILED;
3676 }
Stephen M. Camerond416b0c2010-02-04 08:43:21 -06003677 dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
3678 h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003679 /* send a reset to the SCSI LUN which the command was sent to */
Scott Teelbf711ac2014-02-18 13:56:39 -06003680 rc = hpsa_send_reset(h, dev->scsi3addr, HPSA_RESET_TYPE_LUN);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003681 if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
3682 return SUCCESS;
3683
3684 dev_warn(&h->pdev->dev, "resetting device failed.\n");
3685 return FAILED;
3686}
3687
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003688static void swizzle_abort_tag(u8 *tag)
3689{
3690 u8 original_tag[8];
3691
3692 memcpy(original_tag, tag, 8);
3693 tag[0] = original_tag[3];
3694 tag[1] = original_tag[2];
3695 tag[2] = original_tag[1];
3696 tag[3] = original_tag[0];
3697 tag[4] = original_tag[7];
3698 tag[5] = original_tag[6];
3699 tag[6] = original_tag[5];
3700 tag[7] = original_tag[4];
3701}
3702
Scott Teel17eb87d2014-02-18 13:55:28 -06003703static void hpsa_get_tag(struct ctlr_info *h,
3704 struct CommandList *c, u32 *taglower, u32 *tagupper)
3705{
3706 if (c->cmd_type == CMD_IOACCEL1) {
3707 struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
3708 &h->ioaccel_cmd_pool[c->cmdindex];
3709 *tagupper = cm1->Tag.upper;
3710 *taglower = cm1->Tag.lower;
Scott Teel54b6e9e2014-02-18 13:56:45 -06003711 return;
Scott Teel17eb87d2014-02-18 13:55:28 -06003712 }
Scott Teel54b6e9e2014-02-18 13:56:45 -06003713 if (c->cmd_type == CMD_IOACCEL2) {
3714 struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
3715 &h->ioaccel2_cmd_pool[c->cmdindex];
3716 *tagupper = cm2->Tag.upper;
3717 *taglower = cm2->Tag.lower;
3718 return;
3719 }
3720 *tagupper = c->Header.Tag.upper;
3721 *taglower = c->Header.Tag.lower;
Scott Teel17eb87d2014-02-18 13:55:28 -06003722}
3723
Scott Teel54b6e9e2014-02-18 13:56:45 -06003724
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003725static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003726 struct CommandList *abort, int swizzle)
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003727{
3728 int rc = IO_OK;
3729 struct CommandList *c;
3730 struct ErrorInfo *ei;
Scott Teel17eb87d2014-02-18 13:55:28 -06003731 u32 tagupper, taglower;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003732
3733 c = cmd_special_alloc(h);
3734 if (c == NULL) { /* trouble... */
3735 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
3736 return -ENOMEM;
3737 }
3738
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003739 /* fill_cmd can't fail here, no buffer to map */
3740 (void) fill_cmd(c, HPSA_ABORT_MSG, h, abort,
3741 0, 0, scsi3addr, TYPE_MSG);
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003742 if (swizzle)
3743 swizzle_abort_tag(&c->Request.CDB[4]);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003744 hpsa_scsi_do_simple_cmd_core(h, c);
Scott Teel17eb87d2014-02-18 13:55:28 -06003745 hpsa_get_tag(h, abort, &taglower, &tagupper);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003746 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd_core completed.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06003747 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003748 /* no unmap needed here because no data xfer. */
3749
3750 ei = c->err_info;
3751 switch (ei->CommandStatus) {
3752 case CMD_SUCCESS:
3753 break;
3754 case CMD_UNABORTABLE: /* Very common, don't make noise. */
3755 rc = -1;
3756 break;
3757 default:
3758 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06003759 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003760 hpsa_scsi_interpret_error(c);
3761 rc = -1;
3762 break;
3763 }
3764 cmd_special_free(h, c);
3765 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n", __func__,
3766 abort->Header.Tag.upper, abort->Header.Tag.lower);
3767 return rc;
3768}
3769
3770/*
3771 * hpsa_find_cmd_in_queue
3772 *
3773 * Used to determine whether a command (find) is still present
3774 * in queue_head. Optionally excludes the last element of queue_head.
3775 *
3776 * This is used to avoid unnecessary aborts. Commands in h->reqQ have
3777 * not yet been submitted, and so can be aborted by the driver without
3778 * sending an abort to the hardware.
3779 *
3780 * Returns pointer to command if found in queue, NULL otherwise.
3781 */
3782static struct CommandList *hpsa_find_cmd_in_queue(struct ctlr_info *h,
3783 struct scsi_cmnd *find, struct list_head *queue_head)
3784{
3785 unsigned long flags;
3786 struct CommandList *c = NULL; /* ptr into cmpQ */
3787
3788 if (!find)
3789 return 0;
3790 spin_lock_irqsave(&h->lock, flags);
3791 list_for_each_entry(c, queue_head, list) {
3792 if (c->scsi_cmd == NULL) /* e.g.: passthru ioctl */
3793 continue;
3794 if (c->scsi_cmd == find) {
3795 spin_unlock_irqrestore(&h->lock, flags);
3796 return c;
3797 }
3798 }
3799 spin_unlock_irqrestore(&h->lock, flags);
3800 return NULL;
3801}
3802
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003803static struct CommandList *hpsa_find_cmd_in_queue_by_tag(struct ctlr_info *h,
3804 u8 *tag, struct list_head *queue_head)
3805{
3806 unsigned long flags;
3807 struct CommandList *c;
3808
3809 spin_lock_irqsave(&h->lock, flags);
3810 list_for_each_entry(c, queue_head, list) {
3811 if (memcmp(&c->Header.Tag, tag, 8) != 0)
3812 continue;
3813 spin_unlock_irqrestore(&h->lock, flags);
3814 return c;
3815 }
3816 spin_unlock_irqrestore(&h->lock, flags);
3817 return NULL;
3818}
3819
Scott Teel54b6e9e2014-02-18 13:56:45 -06003820/* ioaccel2 path firmware cannot handle abort task requests.
3821 * Change abort requests to physical target reset, and send to the
3822 * address of the physical disk used for the ioaccel 2 command.
3823 * Return 0 on success (IO_OK)
3824 * -1 on failure
3825 */
3826
3827static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
3828 unsigned char *scsi3addr, struct CommandList *abort)
3829{
3830 int rc = IO_OK;
3831 struct scsi_cmnd *scmd; /* scsi command within request being aborted */
3832 struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
3833 unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
3834 unsigned char *psa = &phys_scsi3addr[0];
3835
3836 /* Get a pointer to the hpsa logical device. */
3837 scmd = (struct scsi_cmnd *) abort->scsi_cmd;
3838 dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
3839 if (dev == NULL) {
3840 dev_warn(&h->pdev->dev,
3841 "Cannot abort: no device pointer for command.\n");
3842 return -1; /* not abortable */
3843 }
3844
3845 if (!dev->offload_enabled) {
3846 dev_warn(&h->pdev->dev,
3847 "Can't abort: device is not operating in HP SSD Smart Path mode.\n");
3848 return -1; /* not abortable */
3849 }
3850
3851 /* Incoming scsi3addr is logical addr. We need physical disk addr. */
3852 if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
3853 dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
3854 return -1; /* not abortable */
3855 }
3856
3857 /* send the reset */
3858 rc = hpsa_send_reset(h, psa, HPSA_RESET_TYPE_TARGET);
3859 if (rc != 0) {
3860 dev_warn(&h->pdev->dev,
3861 "Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
3862 psa[0], psa[1], psa[2], psa[3],
3863 psa[4], psa[5], psa[6], psa[7]);
3864 return rc; /* failed to reset */
3865 }
3866
3867 /* wait for device to recover */
3868 if (wait_for_device_to_become_ready(h, psa) != 0) {
3869 dev_warn(&h->pdev->dev,
3870 "Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
3871 psa[0], psa[1], psa[2], psa[3],
3872 psa[4], psa[5], psa[6], psa[7]);
3873 return -1; /* failed to recover */
3874 }
3875
3876 /* device recovered */
3877 dev_info(&h->pdev->dev,
3878 "Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
3879 psa[0], psa[1], psa[2], psa[3],
3880 psa[4], psa[5], psa[6], psa[7]);
3881
3882 return rc; /* success */
3883}
3884
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003885/* Some Smart Arrays need the abort tag swizzled, and some don't. It's hard to
3886 * tell which kind we're dealing with, so we send the abort both ways. There
3887 * shouldn't be any collisions between swizzled and unswizzled tags due to the
3888 * way we construct our tags but we check anyway in case the assumptions which
3889 * make this true someday become false.
3890 */
3891static int hpsa_send_abort_both_ways(struct ctlr_info *h,
3892 unsigned char *scsi3addr, struct CommandList *abort)
3893{
3894 u8 swizzled_tag[8];
3895 struct CommandList *c;
3896 int rc = 0, rc2 = 0;
3897
Scott Teel54b6e9e2014-02-18 13:56:45 -06003898 /* ioccelerator mode 2 commands should be aborted via the
3899 * accelerated path, since RAID path is unaware of these commands,
3900 * but underlying firmware can't handle abort TMF.
3901 * Change abort to physical device reset.
3902 */
3903 if (abort->cmd_type == CMD_IOACCEL2)
3904 return hpsa_send_reset_as_abort_ioaccel2(h, scsi3addr, abort);
3905
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003906 /* we do not expect to find the swizzled tag in our queue, but
3907 * check anyway just to be sure the assumptions which make this
3908 * the case haven't become wrong.
3909 */
3910 memcpy(swizzled_tag, &abort->Request.CDB[4], 8);
3911 swizzle_abort_tag(swizzled_tag);
3912 c = hpsa_find_cmd_in_queue_by_tag(h, swizzled_tag, &h->cmpQ);
3913 if (c != NULL) {
3914 dev_warn(&h->pdev->dev, "Unexpectedly found byte-swapped tag in completion queue.\n");
3915 return hpsa_send_abort(h, scsi3addr, abort, 0);
3916 }
3917 rc = hpsa_send_abort(h, scsi3addr, abort, 0);
3918
3919 /* if the command is still in our queue, we can't conclude that it was
3920 * aborted (it might have just completed normally) but in any case
3921 * we don't need to try to abort it another way.
3922 */
3923 c = hpsa_find_cmd_in_queue(h, abort->scsi_cmd, &h->cmpQ);
3924 if (c)
3925 rc2 = hpsa_send_abort(h, scsi3addr, abort, 1);
3926 return rc && rc2;
3927}
3928
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003929/* Send an abort for the specified command.
3930 * If the device and controller support it,
3931 * send a task abort request.
3932 */
3933static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
3934{
3935
3936 int i, rc;
3937 struct ctlr_info *h;
3938 struct hpsa_scsi_dev_t *dev;
3939 struct CommandList *abort; /* pointer to command to be aborted */
3940 struct CommandList *found;
3941 struct scsi_cmnd *as; /* ptr to scsi cmd inside aborted command. */
3942 char msg[256]; /* For debug messaging. */
3943 int ml = 0;
Scott Teel17eb87d2014-02-18 13:55:28 -06003944 u32 tagupper, taglower;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003945
3946 /* Find the controller of the command to be aborted */
3947 h = sdev_to_hba(sc->device);
3948 if (WARN(h == NULL,
3949 "ABORT REQUEST FAILED, Controller lookup failed.\n"))
3950 return FAILED;
3951
3952 /* Check that controller supports some kind of task abort */
3953 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
3954 !(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
3955 return FAILED;
3956
3957 memset(msg, 0, sizeof(msg));
3958 ml += sprintf(msg+ml, "ABORT REQUEST on C%d:B%d:T%d:L%d ",
3959 h->scsi_host->host_no, sc->device->channel,
3960 sc->device->id, sc->device->lun);
3961
3962 /* Find the device of the command to be aborted */
3963 dev = sc->device->hostdata;
3964 if (!dev) {
3965 dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
3966 msg);
3967 return FAILED;
3968 }
3969
3970 /* Get SCSI command to be aborted */
3971 abort = (struct CommandList *) sc->host_scribble;
3972 if (abort == NULL) {
3973 dev_err(&h->pdev->dev, "%s FAILED, Command to abort is NULL.\n",
3974 msg);
3975 return FAILED;
3976 }
Scott Teel17eb87d2014-02-18 13:55:28 -06003977 hpsa_get_tag(h, abort, &taglower, &tagupper);
3978 ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003979 as = (struct scsi_cmnd *) abort->scsi_cmd;
3980 if (as != NULL)
3981 ml += sprintf(msg+ml, "Command:0x%x SN:0x%lx ",
3982 as->cmnd[0], as->serial_number);
3983 dev_dbg(&h->pdev->dev, "%s\n", msg);
3984 dev_warn(&h->pdev->dev, "Abort request on C%d:B%d:T%d:L%d\n",
3985 h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
3986
3987 /* Search reqQ to See if command is queued but not submitted,
3988 * if so, complete the command with aborted status and remove
3989 * it from the reqQ.
3990 */
3991 found = hpsa_find_cmd_in_queue(h, sc, &h->reqQ);
3992 if (found) {
3993 found->err_info->CommandStatus = CMD_ABORTED;
3994 finish_cmd(found);
3995 dev_info(&h->pdev->dev, "%s Request SUCCEEDED (driver queue).\n",
3996 msg);
3997 return SUCCESS;
3998 }
3999
4000 /* not in reqQ, if also not in cmpQ, must have already completed */
4001 found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
4002 if (!found) {
Stephen M. Camerond6ebd0f2012-07-26 11:34:17 -05004003 dev_dbg(&h->pdev->dev, "%s Request SUCCEEDED (not known to driver).\n",
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004004 msg);
4005 return SUCCESS;
4006 }
4007
4008 /*
4009 * Command is in flight, or possibly already completed
4010 * by the firmware (but not to the scsi mid layer) but we can't
4011 * distinguish which. Send the abort down.
4012 */
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05004013 rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004014 if (rc != 0) {
4015 dev_dbg(&h->pdev->dev, "%s Request FAILED.\n", msg);
4016 dev_warn(&h->pdev->dev, "FAILED abort on device C%d:B%d:T%d:L%d\n",
4017 h->scsi_host->host_no,
4018 dev->bus, dev->target, dev->lun);
4019 return FAILED;
4020 }
4021 dev_info(&h->pdev->dev, "%s REQUEST SUCCEEDED.\n", msg);
4022
4023 /* If the abort(s) above completed and actually aborted the
4024 * command, then the command to be aborted should already be
4025 * completed. If not, wait around a bit more to see if they
4026 * manage to complete normally.
4027 */
4028#define ABORT_COMPLETE_WAIT_SECS 30
4029 for (i = 0; i < ABORT_COMPLETE_WAIT_SECS * 10; i++) {
4030 found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
4031 if (!found)
4032 return SUCCESS;
4033 msleep(100);
4034 }
4035 dev_warn(&h->pdev->dev, "%s FAILED. Aborted command has not completed after %d seconds.\n",
4036 msg, ABORT_COMPLETE_WAIT_SECS);
4037 return FAILED;
4038}
4039
4040
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004041/*
4042 * For operations that cannot sleep, a command block is allocated at init,
4043 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
4044 * which ones are free or in use. Lock must be held when calling this.
4045 * cmd_free() is the complement.
4046 */
4047static struct CommandList *cmd_alloc(struct ctlr_info *h)
4048{
4049 struct CommandList *c;
4050 int i;
4051 union u64bit temp64;
4052 dma_addr_t cmd_dma_handle, err_dma_handle;
Matt Gatese16a33a2012-05-01 11:43:11 -05004053 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004054
Matt Gatese16a33a2012-05-01 11:43:11 -05004055 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004056 do {
4057 i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
Matt Gatese16a33a2012-05-01 11:43:11 -05004058 if (i == h->nr_cmds) {
4059 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004060 return NULL;
Matt Gatese16a33a2012-05-01 11:43:11 -05004061 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004062 } while (test_and_set_bit
4063 (i & (BITS_PER_LONG - 1),
4064 h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
Matt Gatese16a33a2012-05-01 11:43:11 -05004065 spin_unlock_irqrestore(&h->lock, flags);
4066
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004067 c = h->cmd_pool + i;
4068 memset(c, 0, sizeof(*c));
4069 cmd_dma_handle = h->cmd_pool_dhandle
4070 + i * sizeof(*c);
4071 c->err_info = h->errinfo_pool + i;
4072 memset(c->err_info, 0, sizeof(*c->err_info));
4073 err_dma_handle = h->errinfo_pool_dhandle
4074 + i * sizeof(*c->err_info);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004075
4076 c->cmdindex = i;
4077
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06004078 INIT_LIST_HEAD(&c->list);
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004079 c->busaddr = (u32) cmd_dma_handle;
4080 temp64.val = (u64) err_dma_handle;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004081 c->ErrDesc.Addr.lower = temp64.val32.lower;
4082 c->ErrDesc.Addr.upper = temp64.val32.upper;
4083 c->ErrDesc.Len = sizeof(*c->err_info);
4084
4085 c->h = h;
4086 return c;
4087}
4088
4089/* For operations that can wait for kmalloc to possibly sleep,
4090 * this routine can be called. Lock need not be held to call
4091 * cmd_special_alloc. cmd_special_free() is the complement.
4092 */
4093static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
4094{
4095 struct CommandList *c;
4096 union u64bit temp64;
4097 dma_addr_t cmd_dma_handle, err_dma_handle;
4098
4099 c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
4100 if (c == NULL)
4101 return NULL;
4102 memset(c, 0, sizeof(*c));
4103
Matt Gatese1f7de02014-02-18 13:55:17 -06004104 c->cmd_type = CMD_SCSI;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004105 c->cmdindex = -1;
4106
4107 c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info),
4108 &err_dma_handle);
4109
4110 if (c->err_info == NULL) {
4111 pci_free_consistent(h->pdev,
4112 sizeof(*c), c, cmd_dma_handle);
4113 return NULL;
4114 }
4115 memset(c->err_info, 0, sizeof(*c->err_info));
4116
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06004117 INIT_LIST_HEAD(&c->list);
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004118 c->busaddr = (u32) cmd_dma_handle;
4119 temp64.val = (u64) err_dma_handle;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004120 c->ErrDesc.Addr.lower = temp64.val32.lower;
4121 c->ErrDesc.Addr.upper = temp64.val32.upper;
4122 c->ErrDesc.Len = sizeof(*c->err_info);
4123
4124 c->h = h;
4125 return c;
4126}
4127
4128static void cmd_free(struct ctlr_info *h, struct CommandList *c)
4129{
4130 int i;
Matt Gatese16a33a2012-05-01 11:43:11 -05004131 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004132
4133 i = c - h->cmd_pool;
Matt Gatese16a33a2012-05-01 11:43:11 -05004134 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004135 clear_bit(i & (BITS_PER_LONG - 1),
4136 h->cmd_pool_bits + (i / BITS_PER_LONG));
Matt Gatese16a33a2012-05-01 11:43:11 -05004137 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004138}
4139
4140static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
4141{
4142 union u64bit temp64;
4143
4144 temp64.val32.lower = c->ErrDesc.Addr.lower;
4145 temp64.val32.upper = c->ErrDesc.Addr.upper;
4146 pci_free_consistent(h->pdev, sizeof(*c->err_info),
4147 c->err_info, (dma_addr_t) temp64.val);
4148 pci_free_consistent(h->pdev, sizeof(*c),
Stephen M. Camerond896f3f2011-01-06 14:47:53 -06004149 c, (dma_addr_t) (c->busaddr & DIRECT_LOOKUP_MASK));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004150}
4151
4152#ifdef CONFIG_COMPAT
4153
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004154static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
4155{
4156 IOCTL32_Command_struct __user *arg32 =
4157 (IOCTL32_Command_struct __user *) arg;
4158 IOCTL_Command_struct arg64;
4159 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
4160 int err;
4161 u32 cp;
4162
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06004163 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004164 err = 0;
4165 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
4166 sizeof(arg64.LUN_info));
4167 err |= copy_from_user(&arg64.Request, &arg32->Request,
4168 sizeof(arg64.Request));
4169 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
4170 sizeof(arg64.error_info));
4171 err |= get_user(arg64.buf_size, &arg32->buf_size);
4172 err |= get_user(cp, &arg32->buf);
4173 arg64.buf = compat_ptr(cp);
4174 err |= copy_to_user(p, &arg64, sizeof(arg64));
4175
4176 if (err)
4177 return -EFAULT;
4178
Stephen M. Camerone39eeae2010-02-04 08:43:46 -06004179 err = hpsa_ioctl(dev, CCISS_PASSTHRU, (void *)p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004180 if (err)
4181 return err;
4182 err |= copy_in_user(&arg32->error_info, &p->error_info,
4183 sizeof(arg32->error_info));
4184 if (err)
4185 return -EFAULT;
4186 return err;
4187}
4188
4189static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
4190 int cmd, void *arg)
4191{
4192 BIG_IOCTL32_Command_struct __user *arg32 =
4193 (BIG_IOCTL32_Command_struct __user *) arg;
4194 BIG_IOCTL_Command_struct arg64;
4195 BIG_IOCTL_Command_struct __user *p =
4196 compat_alloc_user_space(sizeof(arg64));
4197 int err;
4198 u32 cp;
4199
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06004200 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004201 err = 0;
4202 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
4203 sizeof(arg64.LUN_info));
4204 err |= copy_from_user(&arg64.Request, &arg32->Request,
4205 sizeof(arg64.Request));
4206 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
4207 sizeof(arg64.error_info));
4208 err |= get_user(arg64.buf_size, &arg32->buf_size);
4209 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
4210 err |= get_user(cp, &arg32->buf);
4211 arg64.buf = compat_ptr(cp);
4212 err |= copy_to_user(p, &arg64, sizeof(arg64));
4213
4214 if (err)
4215 return -EFAULT;
4216
Stephen M. Camerone39eeae2010-02-04 08:43:46 -06004217 err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004218 if (err)
4219 return err;
4220 err |= copy_in_user(&arg32->error_info, &p->error_info,
4221 sizeof(arg32->error_info));
4222 if (err)
4223 return -EFAULT;
4224 return err;
4225}
Stephen M. Cameron71fe75a2010-02-04 08:43:51 -06004226
4227static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
4228{
4229 switch (cmd) {
4230 case CCISS_GETPCIINFO:
4231 case CCISS_GETINTINFO:
4232 case CCISS_SETINTINFO:
4233 case CCISS_GETNODENAME:
4234 case CCISS_SETNODENAME:
4235 case CCISS_GETHEARTBEAT:
4236 case CCISS_GETBUSTYPES:
4237 case CCISS_GETFIRMVER:
4238 case CCISS_GETDRIVVER:
4239 case CCISS_REVALIDVOLS:
4240 case CCISS_DEREGDISK:
4241 case CCISS_REGNEWDISK:
4242 case CCISS_REGNEWD:
4243 case CCISS_RESCANDISK:
4244 case CCISS_GETLUNINFO:
4245 return hpsa_ioctl(dev, cmd, arg);
4246
4247 case CCISS_PASSTHRU32:
4248 return hpsa_ioctl32_passthru(dev, cmd, arg);
4249 case CCISS_BIG_PASSTHRU32:
4250 return hpsa_ioctl32_big_passthru(dev, cmd, arg);
4251
4252 default:
4253 return -ENOIOCTLCMD;
4254 }
4255}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004256#endif
4257
4258static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
4259{
4260 struct hpsa_pci_info pciinfo;
4261
4262 if (!argp)
4263 return -EINVAL;
4264 pciinfo.domain = pci_domain_nr(h->pdev->bus);
4265 pciinfo.bus = h->pdev->bus->number;
4266 pciinfo.dev_fn = h->pdev->devfn;
4267 pciinfo.board_id = h->board_id;
4268 if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
4269 return -EFAULT;
4270 return 0;
4271}
4272
4273static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
4274{
4275 DriverVer_type DriverVer;
4276 unsigned char vmaj, vmin, vsubmin;
4277 int rc;
4278
4279 rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
4280 &vmaj, &vmin, &vsubmin);
4281 if (rc != 3) {
4282 dev_info(&h->pdev->dev, "driver version string '%s' "
4283 "unrecognized.", HPSA_DRIVER_VERSION);
4284 vmaj = 0;
4285 vmin = 0;
4286 vsubmin = 0;
4287 }
4288 DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
4289 if (!argp)
4290 return -EINVAL;
4291 if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
4292 return -EFAULT;
4293 return 0;
4294}
4295
4296static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
4297{
4298 IOCTL_Command_struct iocommand;
4299 struct CommandList *c;
4300 char *buff = NULL;
4301 union u64bit temp64;
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004302 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004303
4304 if (!argp)
4305 return -EINVAL;
4306 if (!capable(CAP_SYS_RAWIO))
4307 return -EPERM;
4308 if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
4309 return -EFAULT;
4310 if ((iocommand.buf_size < 1) &&
4311 (iocommand.Request.Type.Direction != XFER_NONE)) {
4312 return -EINVAL;
4313 }
4314 if (iocommand.buf_size > 0) {
4315 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
4316 if (buff == NULL)
4317 return -EFAULT;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004318 if (iocommand.Request.Type.Direction == XFER_WRITE) {
4319 /* Copy the data into the buffer we created */
4320 if (copy_from_user(buff, iocommand.buf,
4321 iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004322 rc = -EFAULT;
4323 goto out_kfree;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004324 }
4325 } else {
4326 memset(buff, 0, iocommand.buf_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004327 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004328 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004329 c = cmd_special_alloc(h);
4330 if (c == NULL) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004331 rc = -ENOMEM;
4332 goto out_kfree;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004333 }
4334 /* Fill in the command type */
4335 c->cmd_type = CMD_IOCTL_PEND;
4336 /* Fill in Command Header */
4337 c->Header.ReplyQueue = 0; /* unused in simple mode */
4338 if (iocommand.buf_size > 0) { /* buffer to fill */
4339 c->Header.SGList = 1;
4340 c->Header.SGTotal = 1;
4341 } else { /* no buffers to fill */
4342 c->Header.SGList = 0;
4343 c->Header.SGTotal = 0;
4344 }
4345 memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
4346 /* use the kernel address the cmd block for tag */
4347 c->Header.Tag.lower = c->busaddr;
4348
4349 /* Fill in Request block */
4350 memcpy(&c->Request, &iocommand.Request,
4351 sizeof(c->Request));
4352
4353 /* Fill in the scatter gather information */
4354 if (iocommand.buf_size > 0) {
4355 temp64.val = pci_map_single(h->pdev, buff,
4356 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06004357 if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
4358 c->SG[0].Addr.lower = 0;
4359 c->SG[0].Addr.upper = 0;
4360 c->SG[0].Len = 0;
4361 rc = -ENOMEM;
4362 goto out;
4363 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004364 c->SG[0].Addr.lower = temp64.val32.lower;
4365 c->SG[0].Addr.upper = temp64.val32.upper;
4366 c->SG[0].Len = iocommand.buf_size;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06004367 c->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining*/
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004368 }
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004369 hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
Stephen M. Cameronc2dd32e2011-06-03 09:57:29 -05004370 if (iocommand.buf_size > 0)
4371 hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004372 check_ioctl_unit_attention(h, c);
4373
4374 /* Copy the error information out */
4375 memcpy(&iocommand.error_info, c->err_info,
4376 sizeof(iocommand.error_info));
4377 if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004378 rc = -EFAULT;
4379 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004380 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004381 if (iocommand.Request.Type.Direction == XFER_READ &&
4382 iocommand.buf_size > 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004383 /* Copy the data out of the buffer we created */
4384 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004385 rc = -EFAULT;
4386 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004387 }
4388 }
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004389out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004390 cmd_special_free(h, c);
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004391out_kfree:
4392 kfree(buff);
4393 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004394}
4395
4396static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
4397{
4398 BIG_IOCTL_Command_struct *ioc;
4399 struct CommandList *c;
4400 unsigned char **buff = NULL;
4401 int *buff_size = NULL;
4402 union u64bit temp64;
4403 BYTE sg_used = 0;
4404 int status = 0;
4405 int i;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004406 u32 left;
4407 u32 sz;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004408 BYTE __user *data_ptr;
4409
4410 if (!argp)
4411 return -EINVAL;
4412 if (!capable(CAP_SYS_RAWIO))
4413 return -EPERM;
4414 ioc = (BIG_IOCTL_Command_struct *)
4415 kmalloc(sizeof(*ioc), GFP_KERNEL);
4416 if (!ioc) {
4417 status = -ENOMEM;
4418 goto cleanup1;
4419 }
4420 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
4421 status = -EFAULT;
4422 goto cleanup1;
4423 }
4424 if ((ioc->buf_size < 1) &&
4425 (ioc->Request.Type.Direction != XFER_NONE)) {
4426 status = -EINVAL;
4427 goto cleanup1;
4428 }
4429 /* Check kmalloc limits using all SGs */
4430 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
4431 status = -EINVAL;
4432 goto cleanup1;
4433 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06004434 if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004435 status = -EINVAL;
4436 goto cleanup1;
4437 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06004438 buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004439 if (!buff) {
4440 status = -ENOMEM;
4441 goto cleanup1;
4442 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06004443 buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004444 if (!buff_size) {
4445 status = -ENOMEM;
4446 goto cleanup1;
4447 }
4448 left = ioc->buf_size;
4449 data_ptr = ioc->buf;
4450 while (left) {
4451 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
4452 buff_size[sg_used] = sz;
4453 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
4454 if (buff[sg_used] == NULL) {
4455 status = -ENOMEM;
4456 goto cleanup1;
4457 }
4458 if (ioc->Request.Type.Direction == XFER_WRITE) {
4459 if (copy_from_user(buff[sg_used], data_ptr, sz)) {
4460 status = -ENOMEM;
4461 goto cleanup1;
4462 }
4463 } else
4464 memset(buff[sg_used], 0, sz);
4465 left -= sz;
4466 data_ptr += sz;
4467 sg_used++;
4468 }
4469 c = cmd_special_alloc(h);
4470 if (c == NULL) {
4471 status = -ENOMEM;
4472 goto cleanup1;
4473 }
4474 c->cmd_type = CMD_IOCTL_PEND;
4475 c->Header.ReplyQueue = 0;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004476 c->Header.SGList = c->Header.SGTotal = sg_used;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004477 memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
4478 c->Header.Tag.lower = c->busaddr;
4479 memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
4480 if (ioc->buf_size > 0) {
4481 int i;
4482 for (i = 0; i < sg_used; i++) {
4483 temp64.val = pci_map_single(h->pdev, buff[i],
4484 buff_size[i], PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06004485 if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
4486 c->SG[i].Addr.lower = 0;
4487 c->SG[i].Addr.upper = 0;
4488 c->SG[i].Len = 0;
4489 hpsa_pci_unmap(h->pdev, c, i,
4490 PCI_DMA_BIDIRECTIONAL);
4491 status = -ENOMEM;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05004492 goto cleanup0;
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06004493 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004494 c->SG[i].Addr.lower = temp64.val32.lower;
4495 c->SG[i].Addr.upper = temp64.val32.upper;
4496 c->SG[i].Len = buff_size[i];
Matt Gatese1d9cbf2014-02-18 13:55:12 -06004497 c->SG[i].Ext = i < sg_used - 1 ? 0 : HPSA_SG_LAST;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004498 }
4499 }
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004500 hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004501 if (sg_used)
4502 hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004503 check_ioctl_unit_attention(h, c);
4504 /* Copy the error information out */
4505 memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
4506 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004507 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05004508 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004509 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004510 if (ioc->Request.Type.Direction == XFER_READ && ioc->buf_size > 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004511 /* Copy the data out of the buffer we created */
4512 BYTE __user *ptr = ioc->buf;
4513 for (i = 0; i < sg_used; i++) {
4514 if (copy_to_user(ptr, buff[i], buff_size[i])) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004515 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05004516 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004517 }
4518 ptr += buff_size[i];
4519 }
4520 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004521 status = 0;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05004522cleanup0:
4523 cmd_special_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004524cleanup1:
4525 if (buff) {
4526 for (i = 0; i < sg_used; i++)
4527 kfree(buff[i]);
4528 kfree(buff);
4529 }
4530 kfree(buff_size);
4531 kfree(ioc);
4532 return status;
4533}
4534
4535static void check_ioctl_unit_attention(struct ctlr_info *h,
4536 struct CommandList *c)
4537{
4538 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
4539 c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
4540 (void) check_for_unit_attention(h, c);
4541}
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05004542
4543static int increment_passthru_count(struct ctlr_info *h)
4544{
4545 unsigned long flags;
4546
4547 spin_lock_irqsave(&h->passthru_count_lock, flags);
4548 if (h->passthru_count >= HPSA_MAX_CONCURRENT_PASSTHRUS) {
4549 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
4550 return -1;
4551 }
4552 h->passthru_count++;
4553 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
4554 return 0;
4555}
4556
4557static void decrement_passthru_count(struct ctlr_info *h)
4558{
4559 unsigned long flags;
4560
4561 spin_lock_irqsave(&h->passthru_count_lock, flags);
4562 if (h->passthru_count <= 0) {
4563 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
4564 /* not expecting to get here. */
4565 dev_warn(&h->pdev->dev, "Bug detected, passthru_count seems to be incorrect.\n");
4566 return;
4567 }
4568 h->passthru_count--;
4569 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
4570}
4571
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004572/*
4573 * ioctl
4574 */
4575static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
4576{
4577 struct ctlr_info *h;
4578 void __user *argp = (void __user *)arg;
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05004579 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004580
4581 h = sdev_to_hba(dev);
4582
4583 switch (cmd) {
4584 case CCISS_DEREGDISK:
4585 case CCISS_REGNEWDISK:
4586 case CCISS_REGNEWD:
Stephen M. Camerona08a84712010-02-04 08:43:16 -06004587 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004588 return 0;
4589 case CCISS_GETPCIINFO:
4590 return hpsa_getpciinfo_ioctl(h, argp);
4591 case CCISS_GETDRIVVER:
4592 return hpsa_getdrivver_ioctl(h, argp);
4593 case CCISS_PASSTHRU:
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05004594 if (increment_passthru_count(h))
4595 return -EAGAIN;
4596 rc = hpsa_passthru_ioctl(h, argp);
4597 decrement_passthru_count(h);
4598 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004599 case CCISS_BIG_PASSTHRU:
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05004600 if (increment_passthru_count(h))
4601 return -EAGAIN;
4602 rc = hpsa_big_passthru_ioctl(h, argp);
4603 decrement_passthru_count(h);
4604 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004605 default:
4606 return -ENOTTY;
4607 }
4608}
4609
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004610static int hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
4611 u8 reset_type)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004612{
4613 struct CommandList *c;
4614
4615 c = cmd_alloc(h);
4616 if (!c)
4617 return -ENOMEM;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004618 /* fill_cmd can't fail here, no data buffer to map */
4619 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004620 RAID_CTLR_LUNID, TYPE_MSG);
4621 c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
4622 c->waiting = NULL;
4623 enqueue_cmd_and_start_io(h, c);
4624 /* Don't wait for completion, the reset won't complete. Don't free
4625 * the command either. This is the last command we will send before
4626 * re-initializing everything, so it doesn't matter and won't leak.
4627 */
4628 return 0;
4629}
4630
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004631static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004632 void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004633 int cmd_type)
4634{
4635 int pci_dir = XFER_NONE;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004636 struct CommandList *a; /* for commands to be aborted */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004637
4638 c->cmd_type = CMD_IOCTL_PEND;
4639 c->Header.ReplyQueue = 0;
4640 if (buff != NULL && size > 0) {
4641 c->Header.SGList = 1;
4642 c->Header.SGTotal = 1;
4643 } else {
4644 c->Header.SGList = 0;
4645 c->Header.SGTotal = 0;
4646 }
4647 c->Header.Tag.lower = c->busaddr;
4648 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
4649
4650 c->Request.Type.Type = cmd_type;
4651 if (cmd_type == TYPE_CMD) {
4652 switch (cmd) {
4653 case HPSA_INQUIRY:
4654 /* are we trying to read a vital product page */
4655 if (page_code != 0) {
4656 c->Request.CDB[1] = 0x01;
4657 c->Request.CDB[2] = page_code;
4658 }
4659 c->Request.CDBLen = 6;
4660 c->Request.Type.Attribute = ATTR_SIMPLE;
4661 c->Request.Type.Direction = XFER_READ;
4662 c->Request.Timeout = 0;
4663 c->Request.CDB[0] = HPSA_INQUIRY;
4664 c->Request.CDB[4] = size & 0xFF;
4665 break;
4666 case HPSA_REPORT_LOG:
4667 case HPSA_REPORT_PHYS:
4668 /* Talking to controller so It's a physical command
4669 mode = 00 target = 0. Nothing to write.
4670 */
4671 c->Request.CDBLen = 12;
4672 c->Request.Type.Attribute = ATTR_SIMPLE;
4673 c->Request.Type.Direction = XFER_READ;
4674 c->Request.Timeout = 0;
4675 c->Request.CDB[0] = cmd;
4676 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
4677 c->Request.CDB[7] = (size >> 16) & 0xFF;
4678 c->Request.CDB[8] = (size >> 8) & 0xFF;
4679 c->Request.CDB[9] = size & 0xFF;
4680 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004681 case HPSA_CACHE_FLUSH:
4682 c->Request.CDBLen = 12;
4683 c->Request.Type.Attribute = ATTR_SIMPLE;
4684 c->Request.Type.Direction = XFER_WRITE;
4685 c->Request.Timeout = 0;
4686 c->Request.CDB[0] = BMIC_WRITE;
4687 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
Stephen M. Cameronbb158ea2011-10-26 16:21:17 -05004688 c->Request.CDB[7] = (size >> 8) & 0xFF;
4689 c->Request.CDB[8] = size & 0xFF;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004690 break;
4691 case TEST_UNIT_READY:
4692 c->Request.CDBLen = 6;
4693 c->Request.Type.Attribute = ATTR_SIMPLE;
4694 c->Request.Type.Direction = XFER_NONE;
4695 c->Request.Timeout = 0;
4696 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004697 case HPSA_GET_RAID_MAP:
4698 c->Request.CDBLen = 12;
4699 c->Request.Type.Attribute = ATTR_SIMPLE;
4700 c->Request.Type.Direction = XFER_READ;
4701 c->Request.Timeout = 0;
4702 c->Request.CDB[0] = HPSA_CISS_READ;
4703 c->Request.CDB[1] = cmd;
4704 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
4705 c->Request.CDB[7] = (size >> 16) & 0xFF;
4706 c->Request.CDB[8] = (size >> 8) & 0xFF;
4707 c->Request.CDB[9] = size & 0xFF;
4708 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004709 default:
4710 dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
4711 BUG();
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004712 return -1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004713 }
4714 } else if (cmd_type == TYPE_MSG) {
4715 switch (cmd) {
4716
4717 case HPSA_DEVICE_RESET_MSG:
4718 c->Request.CDBLen = 16;
4719 c->Request.Type.Type = 1; /* It is a MSG not a CMD */
4720 c->Request.Type.Attribute = ATTR_SIMPLE;
4721 c->Request.Type.Direction = XFER_NONE;
4722 c->Request.Timeout = 0; /* Don't time out */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004723 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
4724 c->Request.CDB[0] = cmd;
Stephen M. Cameron21e89af2012-07-26 11:34:10 -05004725 c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004726 /* If bytes 4-7 are zero, it means reset the */
4727 /* LunID device */
4728 c->Request.CDB[4] = 0x00;
4729 c->Request.CDB[5] = 0x00;
4730 c->Request.CDB[6] = 0x00;
4731 c->Request.CDB[7] = 0x00;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004732 break;
4733 case HPSA_ABORT_MSG:
4734 a = buff; /* point to command to be aborted */
4735 dev_dbg(&h->pdev->dev, "Abort Tag:0x%08x:%08x using request Tag:0x%08x:%08x\n",
4736 a->Header.Tag.upper, a->Header.Tag.lower,
4737 c->Header.Tag.upper, c->Header.Tag.lower);
4738 c->Request.CDBLen = 16;
4739 c->Request.Type.Type = TYPE_MSG;
4740 c->Request.Type.Attribute = ATTR_SIMPLE;
4741 c->Request.Type.Direction = XFER_WRITE;
4742 c->Request.Timeout = 0; /* Don't time out */
4743 c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
4744 c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
4745 c->Request.CDB[2] = 0x00; /* reserved */
4746 c->Request.CDB[3] = 0x00; /* reserved */
4747 /* Tag to abort goes in CDB[4]-CDB[11] */
4748 c->Request.CDB[4] = a->Header.Tag.lower & 0xFF;
4749 c->Request.CDB[5] = (a->Header.Tag.lower >> 8) & 0xFF;
4750 c->Request.CDB[6] = (a->Header.Tag.lower >> 16) & 0xFF;
4751 c->Request.CDB[7] = (a->Header.Tag.lower >> 24) & 0xFF;
4752 c->Request.CDB[8] = a->Header.Tag.upper & 0xFF;
4753 c->Request.CDB[9] = (a->Header.Tag.upper >> 8) & 0xFF;
4754 c->Request.CDB[10] = (a->Header.Tag.upper >> 16) & 0xFF;
4755 c->Request.CDB[11] = (a->Header.Tag.upper >> 24) & 0xFF;
4756 c->Request.CDB[12] = 0x00; /* reserved */
4757 c->Request.CDB[13] = 0x00; /* reserved */
4758 c->Request.CDB[14] = 0x00; /* reserved */
4759 c->Request.CDB[15] = 0x00; /* reserved */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004760 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004761 default:
4762 dev_warn(&h->pdev->dev, "unknown message type %d\n",
4763 cmd);
4764 BUG();
4765 }
4766 } else {
4767 dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
4768 BUG();
4769 }
4770
4771 switch (c->Request.Type.Direction) {
4772 case XFER_READ:
4773 pci_dir = PCI_DMA_FROMDEVICE;
4774 break;
4775 case XFER_WRITE:
4776 pci_dir = PCI_DMA_TODEVICE;
4777 break;
4778 case XFER_NONE:
4779 pci_dir = PCI_DMA_NONE;
4780 break;
4781 default:
4782 pci_dir = PCI_DMA_BIDIRECTIONAL;
4783 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004784 if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
4785 return -1;
4786 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004787}
4788
4789/*
4790 * Map (physical) PCI mem into (virtual) kernel space
4791 */
4792static void __iomem *remap_pci_mem(ulong base, ulong size)
4793{
4794 ulong page_base = ((ulong) base) & PAGE_MASK;
4795 ulong page_offs = ((ulong) base) - page_base;
Stephen M. Cameron088ba34c2012-07-26 11:34:23 -05004796 void __iomem *page_remapped = ioremap_nocache(page_base,
4797 page_offs + size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004798
4799 return page_remapped ? (page_remapped + page_offs) : NULL;
4800}
4801
4802/* Takes cmds off the submission queue and sends them to the hardware,
4803 * then puts them on the queue of cmds waiting for completion.
4804 */
4805static void start_io(struct ctlr_info *h)
4806{
4807 struct CommandList *c;
Matt Gatese16a33a2012-05-01 11:43:11 -05004808 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004809
Matt Gatese16a33a2012-05-01 11:43:11 -05004810 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06004811 while (!list_empty(&h->reqQ)) {
4812 c = list_entry(h->reqQ.next, struct CommandList, list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004813 /* can't do anything if fifo is full */
4814 if ((h->access.fifo_full(h))) {
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004815 h->fifo_recently_full = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004816 dev_warn(&h->pdev->dev, "fifo full\n");
4817 break;
4818 }
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004819 h->fifo_recently_full = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004820
4821 /* Get the first entry from the Request Q */
4822 removeQ(c);
4823 h->Qdepth--;
4824
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004825 /* Put job onto the completed Q */
4826 addQ(&h->cmpQ, c);
Matt Gatese16a33a2012-05-01 11:43:11 -05004827
4828 /* Must increment commands_outstanding before unlocking
4829 * and submitting to avoid race checking for fifo full
4830 * condition.
4831 */
4832 h->commands_outstanding++;
4833 if (h->commands_outstanding > h->max_outstanding)
4834 h->max_outstanding = h->commands_outstanding;
4835
4836 /* Tell the controller execute command */
4837 spin_unlock_irqrestore(&h->lock, flags);
4838 h->access.submit_command(h, c);
4839 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004840 }
Matt Gatese16a33a2012-05-01 11:43:11 -05004841 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004842}
4843
Matt Gates254f7962012-05-01 11:43:06 -05004844static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004845{
Matt Gates254f7962012-05-01 11:43:06 -05004846 return h->access.command_completed(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004847}
4848
Stephen M. Cameron900c5442010-02-04 08:42:35 -06004849static inline bool interrupt_pending(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004850{
4851 return h->access.intr_pending(h);
4852}
4853
4854static inline long interrupt_not_for_us(struct ctlr_info *h)
4855{
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004856 return (h->access.intr_pending(h) == 0) ||
4857 (h->interrupts_enabled == 0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004858}
4859
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004860static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
4861 u32 raw_tag)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004862{
4863 if (unlikely(tag_index >= h->nr_cmds)) {
4864 dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
4865 return 1;
4866 }
4867 return 0;
4868}
4869
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05004870static inline void finish_cmd(struct CommandList *c)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004871{
Matt Gatese16a33a2012-05-01 11:43:11 -05004872 unsigned long flags;
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004873 int io_may_be_stalled = 0;
4874 struct ctlr_info *h = c->h;
Matt Gatese16a33a2012-05-01 11:43:11 -05004875
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004876 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004877 removeQ(c);
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004878
4879 /*
4880 * Check for possibly stalled i/o.
4881 *
4882 * If a fifo_full condition is encountered, requests will back up
4883 * in h->reqQ. This queue is only emptied out by start_io which is
4884 * only called when a new i/o request comes in. If no i/o's are
4885 * forthcoming, the i/o's in h->reqQ can get stuck. So we call
4886 * start_io from here if we detect such a danger.
4887 *
4888 * Normally, we shouldn't hit this case, but pounding on the
4889 * CCISS_PASSTHRU ioctl can provoke it. Only call start_io if
4890 * commands_outstanding is low. We want to avoid calling
4891 * start_io from in here as much as possible, and esp. don't
4892 * want to get in a cycle where we call start_io every time
4893 * through here.
4894 */
4895 if (unlikely(h->fifo_recently_full) &&
4896 h->commands_outstanding < 5)
4897 io_may_be_stalled = 1;
4898
4899 spin_unlock_irqrestore(&h->lock, flags);
4900
Stephen M. Camerone85c5972012-05-01 11:43:42 -05004901 dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
Scott Teelc3497752014-02-18 13:56:34 -06004902 if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
4903 || c->cmd_type == CMD_IOACCEL2))
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05004904 complete_scsi_command(c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004905 else if (c->cmd_type == CMD_IOCTL_PEND)
4906 complete(c->waiting);
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004907 if (unlikely(io_may_be_stalled))
4908 start_io(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004909}
4910
Stephen M. Camerona104c992010-02-04 08:42:24 -06004911static inline u32 hpsa_tag_contains_index(u32 tag)
4912{
Stephen M. Camerona104c992010-02-04 08:42:24 -06004913 return tag & DIRECT_LOOKUP_BIT;
4914}
4915
4916static inline u32 hpsa_tag_to_index(u32 tag)
4917{
Stephen M. Camerona104c992010-02-04 08:42:24 -06004918 return tag >> DIRECT_LOOKUP_SHIFT;
4919}
4920
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004921
4922static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
Stephen M. Camerona104c992010-02-04 08:42:24 -06004923{
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004924#define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
4925#define HPSA_SIMPLE_ERROR_BITS 0x03
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06004926 if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004927 return tag & ~HPSA_SIMPLE_ERROR_BITS;
4928 return tag & ~HPSA_PERF_ERROR_BITS;
Stephen M. Camerona104c992010-02-04 08:42:24 -06004929}
4930
Don Brace303932f2010-02-04 08:42:40 -06004931/* process completion of an indexed ("direct lookup") command */
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004932static inline void process_indexed_cmd(struct ctlr_info *h,
Don Brace303932f2010-02-04 08:42:40 -06004933 u32 raw_tag)
4934{
4935 u32 tag_index;
4936 struct CommandList *c;
4937
4938 tag_index = hpsa_tag_to_index(raw_tag);
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004939 if (!bad_tag(h, tag_index, raw_tag)) {
4940 c = h->cmd_pool + tag_index;
4941 finish_cmd(c);
4942 }
Don Brace303932f2010-02-04 08:42:40 -06004943}
4944
4945/* process completion of a non-indexed command */
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004946static inline void process_nonindexed_cmd(struct ctlr_info *h,
Don Brace303932f2010-02-04 08:42:40 -06004947 u32 raw_tag)
4948{
4949 u32 tag;
4950 struct CommandList *c = NULL;
Matt Gatese16a33a2012-05-01 11:43:11 -05004951 unsigned long flags;
Don Brace303932f2010-02-04 08:42:40 -06004952
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004953 tag = hpsa_tag_discard_error_bits(h, raw_tag);
Matt Gatese16a33a2012-05-01 11:43:11 -05004954 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06004955 list_for_each_entry(c, &h->cmpQ, list) {
Don Brace303932f2010-02-04 08:42:40 -06004956 if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
Matt Gatese16a33a2012-05-01 11:43:11 -05004957 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05004958 finish_cmd(c);
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004959 return;
Don Brace303932f2010-02-04 08:42:40 -06004960 }
4961 }
Matt Gatese16a33a2012-05-01 11:43:11 -05004962 spin_unlock_irqrestore(&h->lock, flags);
Don Brace303932f2010-02-04 08:42:40 -06004963 bad_tag(h, h->nr_cmds + 1, raw_tag);
Don Brace303932f2010-02-04 08:42:40 -06004964}
4965
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004966/* Some controllers, like p400, will give us one interrupt
4967 * after a soft reset, even if we turned interrupts off.
4968 * Only need to check for this in the hpsa_xxx_discard_completions
4969 * functions.
4970 */
4971static int ignore_bogus_interrupt(struct ctlr_info *h)
4972{
4973 if (likely(!reset_devices))
4974 return 0;
4975
4976 if (likely(h->interrupts_enabled))
4977 return 0;
4978
4979 dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
4980 "(known firmware bug.) Ignoring.\n");
4981
4982 return 1;
4983}
4984
Matt Gates254f7962012-05-01 11:43:06 -05004985/*
4986 * Convert &h->q[x] (passed to interrupt handlers) back to h.
4987 * Relies on (h-q[x] == x) being true for x such that
4988 * 0 <= x < MAX_REPLY_QUEUES.
4989 */
4990static struct ctlr_info *queue_to_hba(u8 *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004991{
Matt Gates254f7962012-05-01 11:43:06 -05004992 return container_of((queue - *queue), struct ctlr_info, q[0]);
4993}
4994
4995static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
4996{
4997 struct ctlr_info *h = queue_to_hba(queue);
4998 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004999 u32 raw_tag;
5000
5001 if (ignore_bogus_interrupt(h))
5002 return IRQ_NONE;
5003
5004 if (interrupt_not_for_us(h))
5005 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005006 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005007 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05005008 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005009 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05005010 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005011 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005012 return IRQ_HANDLED;
5013}
5014
Matt Gates254f7962012-05-01 11:43:06 -05005015static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005016{
Matt Gates254f7962012-05-01 11:43:06 -05005017 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005018 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05005019 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005020
5021 if (ignore_bogus_interrupt(h))
5022 return IRQ_NONE;
5023
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005024 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05005025 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005026 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05005027 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005028 return IRQ_HANDLED;
5029}
5030
Matt Gates254f7962012-05-01 11:43:06 -05005031static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005032{
Matt Gates254f7962012-05-01 11:43:06 -05005033 struct ctlr_info *h = queue_to_hba((u8 *) queue);
Don Brace303932f2010-02-04 08:42:40 -06005034 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05005035 u8 q = *(u8 *) queue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005036
5037 if (interrupt_not_for_us(h))
5038 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005039 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005040 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05005041 raw_tag = get_next_completion(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005042 while (raw_tag != FIFO_EMPTY) {
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005043 if (likely(hpsa_tag_contains_index(raw_tag)))
5044 process_indexed_cmd(h, raw_tag);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005045 else
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005046 process_nonindexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05005047 raw_tag = next_command(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005048 }
5049 }
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005050 return IRQ_HANDLED;
5051}
5052
Matt Gates254f7962012-05-01 11:43:06 -05005053static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005054{
Matt Gates254f7962012-05-01 11:43:06 -05005055 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005056 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05005057 u8 q = *(u8 *) queue;
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005058
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005059 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05005060 raw_tag = get_next_completion(h, q);
Don Brace303932f2010-02-04 08:42:40 -06005061 while (raw_tag != FIFO_EMPTY) {
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005062 if (likely(hpsa_tag_contains_index(raw_tag)))
5063 process_indexed_cmd(h, raw_tag);
Don Brace303932f2010-02-04 08:42:40 -06005064 else
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005065 process_nonindexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05005066 raw_tag = next_command(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005067 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005068 return IRQ_HANDLED;
5069}
5070
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005071/* Send a message CDB to the firmware. Careful, this only works
5072 * in simple mode, not performant mode due to the tag lookup.
5073 * We only ever use this immediately after a controller reset.
5074 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005075static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
5076 unsigned char type)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005077{
5078 struct Command {
5079 struct CommandListHeader CommandHeader;
5080 struct RequestBlock Request;
5081 struct ErrDescriptor ErrorDescriptor;
5082 };
5083 struct Command *cmd;
5084 static const size_t cmd_sz = sizeof(*cmd) +
5085 sizeof(cmd->ErrorDescriptor);
5086 dma_addr_t paddr64;
5087 uint32_t paddr32, tag;
5088 void __iomem *vaddr;
5089 int i, err;
5090
5091 vaddr = pci_ioremap_bar(pdev, 0);
5092 if (vaddr == NULL)
5093 return -ENOMEM;
5094
5095 /* The Inbound Post Queue only accepts 32-bit physical addresses for the
5096 * CCISS commands, so they must be allocated from the lower 4GiB of
5097 * memory.
5098 */
5099 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
5100 if (err) {
5101 iounmap(vaddr);
5102 return -ENOMEM;
5103 }
5104
5105 cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
5106 if (cmd == NULL) {
5107 iounmap(vaddr);
5108 return -ENOMEM;
5109 }
5110
5111 /* This must fit, because of the 32-bit consistent DMA mask. Also,
5112 * although there's no guarantee, we assume that the address is at
5113 * least 4-byte aligned (most likely, it's page-aligned).
5114 */
5115 paddr32 = paddr64;
5116
5117 cmd->CommandHeader.ReplyQueue = 0;
5118 cmd->CommandHeader.SGList = 0;
5119 cmd->CommandHeader.SGTotal = 0;
5120 cmd->CommandHeader.Tag.lower = paddr32;
5121 cmd->CommandHeader.Tag.upper = 0;
5122 memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
5123
5124 cmd->Request.CDBLen = 16;
5125 cmd->Request.Type.Type = TYPE_MSG;
5126 cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
5127 cmd->Request.Type.Direction = XFER_NONE;
5128 cmd->Request.Timeout = 0; /* Don't time out */
5129 cmd->Request.CDB[0] = opcode;
5130 cmd->Request.CDB[1] = type;
5131 memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
5132 cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
5133 cmd->ErrorDescriptor.Addr.upper = 0;
5134 cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
5135
5136 writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
5137
5138 for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
5139 tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005140 if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr32)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005141 break;
5142 msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
5143 }
5144
5145 iounmap(vaddr);
5146
5147 /* we leak the DMA buffer here ... no choice since the controller could
5148 * still complete the command.
5149 */
5150 if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
5151 dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
5152 opcode, type);
5153 return -ETIMEDOUT;
5154 }
5155
5156 pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
5157
5158 if (tag & HPSA_ERROR_BIT) {
5159 dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
5160 opcode, type);
5161 return -EIO;
5162 }
5163
5164 dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
5165 opcode, type);
5166 return 0;
5167}
5168
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005169#define hpsa_noop(p) hpsa_message(p, 3, 0)
5170
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005171static int hpsa_controller_hard_reset(struct pci_dev *pdev,
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005172 void * __iomem vaddr, u32 use_doorbell)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005173{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005174 u16 pmcsr;
5175 int pos;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005176
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005177 if (use_doorbell) {
5178 /* For everything after the P600, the PCI power state method
5179 * of resetting the controller doesn't work, so we have this
5180 * other way using the doorbell register.
5181 */
5182 dev_info(&pdev->dev, "using doorbell to reset controller\n");
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005183 writel(use_doorbell, vaddr + SA5_DOORBELL);
Stephen M. Cameron85009232013-09-23 13:33:36 -05005184
5185 /* PMC hardware guys tell us we need a 5 second delay after
5186 * doorbell reset and before any attempt to talk to the board
5187 * at all to ensure that this actually works and doesn't fall
5188 * over in some weird corner cases.
5189 */
5190 msleep(5000);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005191 } else { /* Try to do it the PCI power state way */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005192
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005193 /* Quoting from the Open CISS Specification: "The Power
5194 * Management Control/Status Register (CSR) controls the power
5195 * state of the device. The normal operating state is D0,
5196 * CSR=00h. The software off state is D3, CSR=03h. To reset
5197 * the controller, place the interface device in D3 then to D0,
5198 * this causes a secondary PCI reset which will reset the
5199 * controller." */
5200
5201 pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
5202 if (pos == 0) {
5203 dev_err(&pdev->dev,
5204 "hpsa_reset_controller: "
5205 "PCI PM not supported\n");
5206 return -ENODEV;
5207 }
5208 dev_info(&pdev->dev, "using PCI PM to reset controller\n");
5209 /* enter the D3hot power management state */
5210 pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
5211 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
5212 pmcsr |= PCI_D3hot;
5213 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
5214
5215 msleep(500);
5216
5217 /* enter the D0 power management state */
5218 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
5219 pmcsr |= PCI_D0;
5220 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
Mike Millerc4853ef2011-10-21 08:19:43 +02005221
5222 /*
5223 * The P600 requires a small delay when changing states.
5224 * Otherwise we may think the board did not reset and we bail.
5225 * This for kdump only and is particular to the P600.
5226 */
5227 msleep(500);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005228 }
5229 return 0;
5230}
5231
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005232static void init_driver_version(char *driver_version, int len)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005233{
5234 memset(driver_version, 0, len);
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06005235 strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005236}
5237
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005238static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005239{
5240 char *driver_version;
5241 int i, size = sizeof(cfgtable->driver_version);
5242
5243 driver_version = kmalloc(size, GFP_KERNEL);
5244 if (!driver_version)
5245 return -ENOMEM;
5246
5247 init_driver_version(driver_version, size);
5248 for (i = 0; i < size; i++)
5249 writeb(driver_version[i], &cfgtable->driver_version[i]);
5250 kfree(driver_version);
5251 return 0;
5252}
5253
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005254static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
5255 unsigned char *driver_ver)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005256{
5257 int i;
5258
5259 for (i = 0; i < sizeof(cfgtable->driver_version); i++)
5260 driver_ver[i] = readb(&cfgtable->driver_version[i]);
5261}
5262
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005263static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005264{
5265
5266 char *driver_ver, *old_driver_ver;
5267 int rc, size = sizeof(cfgtable->driver_version);
5268
5269 old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
5270 if (!old_driver_ver)
5271 return -ENOMEM;
5272 driver_ver = old_driver_ver + size;
5273
5274 /* After a reset, the 32 bytes of "driver version" in the cfgtable
5275 * should have been changed, otherwise we know the reset failed.
5276 */
5277 init_driver_version(old_driver_ver, size);
5278 read_driver_ver_from_cfgtable(cfgtable, driver_ver);
5279 rc = !memcmp(driver_ver, old_driver_ver, size);
5280 kfree(old_driver_ver);
5281 return rc;
5282}
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005283/* This does a hard reset of the controller using PCI power management
5284 * states or the using the doorbell register.
5285 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005286static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev)
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005287{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005288 u64 cfg_offset;
5289 u32 cfg_base_addr;
5290 u64 cfg_base_addr_index;
5291 void __iomem *vaddr;
5292 unsigned long paddr;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005293 u32 misc_fw_support;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005294 int rc;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005295 struct CfgTable __iomem *cfgtable;
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005296 u32 use_doorbell;
Stephen M. Cameron18867652010-06-16 13:51:45 -05005297 u32 board_id;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005298 u16 command_register;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005299
5300 /* For controllers as old as the P600, this is very nearly
5301 * the same thing as
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005302 *
5303 * pci_save_state(pci_dev);
5304 * pci_set_power_state(pci_dev, PCI_D3hot);
5305 * pci_set_power_state(pci_dev, PCI_D0);
5306 * pci_restore_state(pci_dev);
5307 *
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005308 * For controllers newer than the P600, the pci power state
5309 * method of resetting doesn't work so we have another way
5310 * using the doorbell register.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005311 */
Stephen M. Cameron18867652010-06-16 13:51:45 -05005312
Stephen M. Cameron25c1e56a2011-01-06 14:48:18 -06005313 rc = hpsa_lookup_board_id(pdev, &board_id);
Stephen M. Cameron46380782011-05-03 15:00:01 -05005314 if (rc < 0 || !ctlr_is_resettable(board_id)) {
Stephen M. Cameron25c1e56a2011-01-06 14:48:18 -06005315 dev_warn(&pdev->dev, "Not resetting device.\n");
5316 return -ENODEV;
5317 }
Stephen M. Cameron46380782011-05-03 15:00:01 -05005318
5319 /* if controller is soft- but not hard resettable... */
5320 if (!ctlr_is_hard_resettable(board_id))
5321 return -ENOTSUPP; /* try soft reset later. */
Stephen M. Cameron18867652010-06-16 13:51:45 -05005322
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005323 /* Save the PCI command register */
5324 pci_read_config_word(pdev, 4, &command_register);
5325 /* Turn the board off. This is so that later pci_restore_state()
5326 * won't turn the board on before the rest of config space is ready.
5327 */
5328 pci_disable_device(pdev);
5329 pci_save_state(pdev);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005330
5331 /* find the first memory BAR, so we can find the cfg table */
5332 rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
5333 if (rc)
5334 return rc;
5335 vaddr = remap_pci_mem(paddr, 0x250);
5336 if (!vaddr)
5337 return -ENOMEM;
5338
5339 /* find cfgtable in order to check if reset via doorbell is supported */
5340 rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
5341 &cfg_base_addr_index, &cfg_offset);
5342 if (rc)
5343 goto unmap_vaddr;
5344 cfgtable = remap_pci_mem(pci_resource_start(pdev,
5345 cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
5346 if (!cfgtable) {
5347 rc = -ENOMEM;
5348 goto unmap_vaddr;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005349 }
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005350 rc = write_driver_ver_to_cfgtable(cfgtable);
5351 if (rc)
5352 goto unmap_vaddr;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005353
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005354 /* If reset via doorbell register is supported, use that.
5355 * There are two such methods. Favor the newest method.
5356 */
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005357 misc_fw_support = readl(&cfgtable->misc_fw_support);
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005358 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
5359 if (use_doorbell) {
5360 use_doorbell = DOORBELL_CTLR_RESET2;
5361 } else {
5362 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
5363 if (use_doorbell) {
Mike Millerfba63092011-10-13 11:44:06 -05005364 dev_warn(&pdev->dev, "Soft reset not supported. "
5365 "Firmware update is required.\n");
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005366 rc = -ENOTSUPP; /* try soft reset */
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005367 goto unmap_cfgtable;
5368 }
5369 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005370
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005371 rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
5372 if (rc)
5373 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005374
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005375 pci_restore_state(pdev);
5376 rc = pci_enable_device(pdev);
5377 if (rc) {
5378 dev_warn(&pdev->dev, "failed to enable device.\n");
5379 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005380 }
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005381 pci_write_config_word(pdev, 4, command_register);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005382
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005383 /* Some devices (notably the HP Smart Array 5i Controller)
5384 need a little pause here */
5385 msleep(HPSA_POST_RESET_PAUSE_MSECS);
5386
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005387 rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
5388 if (rc) {
5389 dev_warn(&pdev->dev,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005390 "failed waiting for board to become ready "
5391 "after hard reset\n");
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005392 goto unmap_cfgtable;
5393 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005394
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005395 rc = controller_reset_failed(vaddr);
5396 if (rc < 0)
5397 goto unmap_cfgtable;
5398 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005399 dev_warn(&pdev->dev, "Unable to successfully reset "
5400 "controller. Will try soft reset.\n");
5401 rc = -ENOTSUPP;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005402 } else {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005403 dev_info(&pdev->dev, "board ready after hard reset.\n");
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005404 }
5405
5406unmap_cfgtable:
5407 iounmap(cfgtable);
5408
5409unmap_vaddr:
5410 iounmap(vaddr);
5411 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005412}
5413
5414/*
5415 * We cannot read the structure directly, for portability we must use
5416 * the io functions.
5417 * This is for debug only.
5418 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005419static void print_cfg_table(struct device *dev, struct CfgTable *tb)
5420{
Stephen M. Cameron58f86652010-05-27 15:13:58 -05005421#ifdef HPSA_DEBUG
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005422 int i;
5423 char temp_name[17];
5424
5425 dev_info(dev, "Controller Configuration information\n");
5426 dev_info(dev, "------------------------------------\n");
5427 for (i = 0; i < 4; i++)
5428 temp_name[i] = readb(&(tb->Signature[i]));
5429 temp_name[4] = '\0';
5430 dev_info(dev, " Signature = %s\n", temp_name);
5431 dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
5432 dev_info(dev, " Transport methods supported = 0x%x\n",
5433 readl(&(tb->TransportSupport)));
5434 dev_info(dev, " Transport methods active = 0x%x\n",
5435 readl(&(tb->TransportActive)));
5436 dev_info(dev, " Requested transport Method = 0x%x\n",
5437 readl(&(tb->HostWrite.TransportRequest)));
5438 dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
5439 readl(&(tb->HostWrite.CoalIntDelay)));
5440 dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
5441 readl(&(tb->HostWrite.CoalIntCount)));
5442 dev_info(dev, " Max outstanding commands = 0x%d\n",
5443 readl(&(tb->CmdsOutMax)));
5444 dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
5445 for (i = 0; i < 16; i++)
5446 temp_name[i] = readb(&(tb->ServerName[i]));
5447 temp_name[16] = '\0';
5448 dev_info(dev, " Server Name = %s\n", temp_name);
5449 dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
5450 readl(&(tb->HeartBeat)));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005451#endif /* HPSA_DEBUG */
Stephen M. Cameron58f86652010-05-27 15:13:58 -05005452}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005453
5454static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
5455{
5456 int i, offset, mem_type, bar_type;
5457
5458 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
5459 return 0;
5460 offset = 0;
5461 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
5462 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
5463 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
5464 offset += 4;
5465 else {
5466 mem_type = pci_resource_flags(pdev, i) &
5467 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
5468 switch (mem_type) {
5469 case PCI_BASE_ADDRESS_MEM_TYPE_32:
5470 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
5471 offset += 4; /* 32 bit */
5472 break;
5473 case PCI_BASE_ADDRESS_MEM_TYPE_64:
5474 offset += 8;
5475 break;
5476 default: /* reserved in PCI 2.2 */
5477 dev_warn(&pdev->dev,
5478 "base address is invalid\n");
5479 return -1;
5480 break;
5481 }
5482 }
5483 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
5484 return i + 1;
5485 }
5486 return -1;
5487}
5488
5489/* If MSI/MSI-X is supported by the kernel we will try to enable it on
5490 * controllers that are capable. If not, we use IO-APIC mode.
5491 */
5492
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005493static void hpsa_interrupt_mode(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005494{
5495#ifdef CONFIG_PCI_MSI
Matt Gates254f7962012-05-01 11:43:06 -05005496 int err, i;
5497 struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
5498
5499 for (i = 0; i < MAX_REPLY_QUEUES; i++) {
5500 hpsa_msix_entries[i].vector = 0;
5501 hpsa_msix_entries[i].entry = i;
5502 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005503
5504 /* Some boards advertise MSI but don't really support it */
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05005505 if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
5506 (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005507 goto default_int_mode;
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005508 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
5509 dev_info(&h->pdev->dev, "MSIX\n");
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005510 h->msix_vector = MAX_REPLY_QUEUES;
Matt Gates254f7962012-05-01 11:43:06 -05005511 err = pci_enable_msix(h->pdev, hpsa_msix_entries,
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005512 h->msix_vector);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005513 if (err > 0) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005514 dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005515 "available\n", err);
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005516 h->msix_vector = err;
5517 err = pci_enable_msix(h->pdev, hpsa_msix_entries,
5518 h->msix_vector);
5519 }
5520 if (!err) {
5521 for (i = 0; i < h->msix_vector; i++)
5522 h->intr[i] = hpsa_msix_entries[i].vector;
5523 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005524 } else {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005525 dev_warn(&h->pdev->dev, "MSI-X init failed %d\n",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005526 err);
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005527 h->msix_vector = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005528 goto default_int_mode;
5529 }
5530 }
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005531 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
5532 dev_info(&h->pdev->dev, "MSI\n");
5533 if (!pci_enable_msi(h->pdev))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005534 h->msi_vector = 1;
5535 else
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005536 dev_warn(&h->pdev->dev, "MSI init failed\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005537 }
5538default_int_mode:
5539#endif /* CONFIG_PCI_MSI */
5540 /* if we get here we're going to use the default interrupt mode */
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005541 h->intr[h->intr_mode] = h->pdev->irq;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005542}
5543
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005544static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05005545{
5546 int i;
5547 u32 subsystem_vendor_id, subsystem_device_id;
5548
5549 subsystem_vendor_id = pdev->subsystem_vendor;
5550 subsystem_device_id = pdev->subsystem_device;
5551 *board_id = ((subsystem_device_id << 16) & 0xffff0000) |
5552 subsystem_vendor_id;
5553
5554 for (i = 0; i < ARRAY_SIZE(products); i++)
5555 if (*board_id == products[i].board_id)
5556 return i;
5557
Stephen M. Cameron6798cc02010-06-16 13:51:20 -05005558 if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
5559 subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
5560 !hpsa_allow_any) {
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05005561 dev_warn(&pdev->dev, "unrecognized board ID: "
5562 "0x%08x, ignoring.\n", *board_id);
5563 return -ENODEV;
5564 }
5565 return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
5566}
5567
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005568static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
5569 unsigned long *memory_bar)
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005570{
5571 int i;
5572
5573 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05005574 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005575 /* addressing mode bits already removed */
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05005576 *memory_bar = pci_resource_start(pdev, i);
5577 dev_dbg(&pdev->dev, "memory BAR = %lx\n",
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005578 *memory_bar);
5579 return 0;
5580 }
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05005581 dev_warn(&pdev->dev, "no memory BAR found\n");
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005582 return -ENODEV;
5583}
5584
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005585static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
5586 int wait_for_ready)
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005587{
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005588 int i, iterations;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005589 u32 scratchpad;
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005590 if (wait_for_ready)
5591 iterations = HPSA_BOARD_READY_ITERATIONS;
5592 else
5593 iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005594
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005595 for (i = 0; i < iterations; i++) {
5596 scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
5597 if (wait_for_ready) {
5598 if (scratchpad == HPSA_FIRMWARE_READY)
5599 return 0;
5600 } else {
5601 if (scratchpad != HPSA_FIRMWARE_READY)
5602 return 0;
5603 }
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005604 msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
5605 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005606 dev_warn(&pdev->dev, "board not ready, timed out.\n");
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005607 return -ENODEV;
5608}
5609
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005610static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
5611 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
5612 u64 *cfg_offset)
Stephen M. Camerona51fd472010-06-16 13:51:30 -05005613{
5614 *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
5615 *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
5616 *cfg_base_addr &= (u32) 0x0000ffff;
5617 *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
5618 if (*cfg_base_addr_index == -1) {
5619 dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
5620 return -ENODEV;
5621 }
5622 return 0;
5623}
5624
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005625static int hpsa_find_cfgtables(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005626{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06005627 u64 cfg_offset;
5628 u32 cfg_base_addr;
5629 u64 cfg_base_addr_index;
Don Brace303932f2010-02-04 08:42:40 -06005630 u32 trans_offset;
Stephen M. Camerona51fd472010-06-16 13:51:30 -05005631 int rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005632
Stephen M. Camerona51fd472010-06-16 13:51:30 -05005633 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
5634 &cfg_base_addr_index, &cfg_offset);
5635 if (rc)
5636 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005637 h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
Stephen M. Camerona51fd472010-06-16 13:51:30 -05005638 cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005639 if (!h->cfgtable)
5640 return -ENOMEM;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005641 rc = write_driver_ver_to_cfgtable(h->cfgtable);
5642 if (rc)
5643 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005644 /* Find performant mode table. */
Stephen M. Camerona51fd472010-06-16 13:51:30 -05005645 trans_offset = readl(&h->cfgtable->TransMethodOffset);
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005646 h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
5647 cfg_base_addr_index)+cfg_offset+trans_offset,
5648 sizeof(*h->transtable));
5649 if (!h->transtable)
5650 return -ENOMEM;
5651 return 0;
5652}
5653
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005654static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05005655{
5656 h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
Stephen M. Cameron72ceeae2011-01-06 14:48:13 -06005657
5658 /* Limit commands in memory limited kdump scenario. */
5659 if (reset_devices && h->max_commands > 32)
5660 h->max_commands = 32;
5661
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05005662 if (h->max_commands < 16) {
5663 dev_warn(&h->pdev->dev, "Controller reports "
5664 "max supported commands of %d, an obvious lie. "
5665 "Using 16. Ensure that firmware is up to date.\n",
5666 h->max_commands);
5667 h->max_commands = 16;
5668 }
5669}
5670
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005671/* Interrogate the hardware for some limits:
5672 * max commands, max SG elements without chaining, and with chaining,
5673 * SG chain block size, etc.
5674 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005675static void hpsa_find_board_params(struct ctlr_info *h)
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005676{
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05005677 hpsa_get_max_perf_mode_cmds(h);
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005678 h->nr_cmds = h->max_commands - 4; /* Allow room for some ioctls */
5679 h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005680 h->fw_support = readl(&(h->cfgtable->misc_fw_support));
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005681 /*
5682 * Limit in-command s/g elements to 32 save dma'able memory.
5683 * Howvever spec says if 0, use 31
5684 */
5685 h->max_cmd_sg_entries = 31;
5686 if (h->maxsgentries > 512) {
5687 h->max_cmd_sg_entries = 32;
5688 h->chainsize = h->maxsgentries - h->max_cmd_sg_entries + 1;
5689 h->maxsgentries--; /* save one for chain pointer */
5690 } else {
5691 h->maxsgentries = 31; /* default to traditional values */
5692 h->chainsize = 0;
5693 }
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005694
5695 /* Find out what task management functions are supported and cache */
5696 h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
Scott Teel0e7a7fc2014-02-18 13:55:59 -06005697 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
5698 dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
5699 if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
5700 dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005701}
5702
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05005703static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
5704{
Akinobu Mita0fc9fd42012-04-04 22:14:59 +09005705 if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05005706 dev_warn(&h->pdev->dev, "not a valid CISS config table\n");
5707 return false;
5708 }
5709 return true;
5710}
5711
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005712static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005713{
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005714 u32 driver_support;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005715
Stephen M. Cameron28e13442013-12-04 17:10:21 -06005716#ifdef CONFIG_X86
5717 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005718 driver_support = readl(&(h->cfgtable->driver_support));
5719 driver_support |= ENABLE_SCSI_PREFETCH;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005720#endif
Stephen M. Cameron28e13442013-12-04 17:10:21 -06005721 driver_support |= ENABLE_UNIT_ATTN;
5722 writel(driver_support, &(h->cfgtable->driver_support));
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005723}
5724
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05005725/* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result
5726 * in a prefetch beyond physical memory.
5727 */
5728static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
5729{
5730 u32 dma_prefetch;
5731
5732 if (h->board_id != 0x3225103C)
5733 return;
5734 dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
5735 dma_prefetch |= 0x8000;
5736 writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
5737}
5738
Stephen M. Cameron76438d02014-02-18 13:55:43 -06005739static void hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
5740{
5741 int i;
5742 u32 doorbell_value;
5743 unsigned long flags;
5744 /* wait until the clear_event_notify bit 6 is cleared by controller. */
5745 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
5746 spin_lock_irqsave(&h->lock, flags);
5747 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
5748 spin_unlock_irqrestore(&h->lock, flags);
5749 if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
5750 break;
5751 /* delay and try again */
5752 msleep(20);
5753 }
5754}
5755
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005756static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005757{
5758 int i;
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06005759 u32 doorbell_value;
5760 unsigned long flags;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005761
5762 /* under certain very rare conditions, this can take awhile.
5763 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
5764 * as we enter this code.)
5765 */
5766 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06005767 spin_lock_irqsave(&h->lock, flags);
5768 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
5769 spin_unlock_irqrestore(&h->lock, flags);
Dan Carpenter382be662011-02-15 15:33:13 -06005770 if (!(doorbell_value & CFGTBL_ChangeReq))
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005771 break;
5772 /* delay and try again */
Stephen M. Cameron60d3f5b2011-01-06 14:48:34 -06005773 usleep_range(10000, 20000);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005774 }
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005775}
5776
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005777static int hpsa_enter_simple_mode(struct ctlr_info *h)
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005778{
5779 u32 trans_support;
5780
5781 trans_support = readl(&(h->cfgtable->TransportSupport));
5782 if (!(trans_support & SIMPLE_MODE))
5783 return -ENOTSUPP;
5784
5785 h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005786
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005787 /* Update the field, and then ring the doorbell */
5788 writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06005789 writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005790 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
5791 hpsa_wait_for_mode_change_ack(h);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005792 print_cfg_table(&h->pdev->dev, h->cfgtable);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005793 if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
5794 goto error;
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06005795 h->transMethod = CFGTBL_Trans_Simple;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005796 return 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005797error:
5798 dev_warn(&h->pdev->dev, "unable to get board into simple mode\n");
5799 return -ENODEV;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005800}
5801
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005802static int hpsa_pci_init(struct ctlr_info *h)
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005803{
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005804 int prod_index, err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005805
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05005806 prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
5807 if (prod_index < 0)
5808 return -ENODEV;
5809 h->product_name = products[prod_index].product_name;
5810 h->access = *(products[prod_index].access);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005811
Matthew Garrette5a44df2011-11-11 11:14:23 -05005812 pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
5813 PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
5814
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005815 err = pci_enable_device(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005816 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005817 dev_warn(&h->pdev->dev, "unable to enable PCI device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005818 return err;
5819 }
5820
Stephen M. Cameron5cb460a2012-05-01 11:42:20 -05005821 /* Enable bus mastering (pci_disable_device may disable this) */
5822 pci_set_master(h->pdev);
5823
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06005824 err = pci_request_regions(h->pdev, HPSA);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005825 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005826 dev_err(&h->pdev->dev,
5827 "cannot obtain PCI resources, aborting\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005828 return err;
5829 }
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05005830 hpsa_interrupt_mode(h);
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05005831 err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005832 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005833 goto err_out_free_res;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005834 h->vaddr = remap_pci_mem(h->paddr, 0x250);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05005835 if (!h->vaddr) {
5836 err = -ENOMEM;
5837 goto err_out_free_res;
5838 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005839 err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005840 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005841 goto err_out_free_res;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005842 err = hpsa_find_cfgtables(h);
5843 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005844 goto err_out_free_res;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005845 hpsa_find_board_params(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005846
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05005847 if (!hpsa_CISS_signature_present(h)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005848 err = -ENODEV;
5849 goto err_out_free_res;
5850 }
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005851 hpsa_set_driver_support_bits(h);
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05005852 hpsa_p600_dma_prefetch_quirk(h);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005853 err = hpsa_enter_simple_mode(h);
5854 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005855 goto err_out_free_res;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005856 return 0;
5857
5858err_out_free_res:
Stephen M. Cameron204892e2010-05-27 15:13:22 -05005859 if (h->transtable)
5860 iounmap(h->transtable);
5861 if (h->cfgtable)
5862 iounmap(h->cfgtable);
5863 if (h->vaddr)
5864 iounmap(h->vaddr);
Stephen M. Cameronf0bd0b682012-05-01 11:42:09 -05005865 pci_disable_device(h->pdev);
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005866 pci_release_regions(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005867 return err;
5868}
5869
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005870static void hpsa_hba_inquiry(struct ctlr_info *h)
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06005871{
5872 int rc;
5873
5874#define HBA_INQUIRY_BYTE_COUNT 64
5875 h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
5876 if (!h->hba_inquiry_data)
5877 return;
5878 rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
5879 h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
5880 if (rc != 0) {
5881 kfree(h->hba_inquiry_data);
5882 h->hba_inquiry_data = NULL;
5883 }
5884}
5885
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005886static int hpsa_init_reset_devices(struct pci_dev *pdev)
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005887{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005888 int rc, i;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005889
5890 if (!reset_devices)
5891 return 0;
5892
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005893 /* Reset the controller with a PCI power-cycle or via doorbell */
5894 rc = hpsa_kdump_hard_reset_controller(pdev);
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005895
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005896 /* -ENOTSUPP here means we cannot reset the controller
5897 * but it's already (and still) up and running in
Stephen M. Cameron18867652010-06-16 13:51:45 -05005898 * "performant mode". Or, it might be 640x, which can't reset
5899 * due to concerns about shared bbwc between 6402/6404 pair.
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005900 */
5901 if (rc == -ENOTSUPP)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005902 return rc; /* just try to do the kdump anyhow. */
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005903 if (rc)
5904 return -ENODEV;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005905
5906 /* Now try to get the controller to respond to a no-op */
Stephen M. Cameron2b870cb2011-05-03 14:59:36 -05005907 dev_warn(&pdev->dev, "Waiting for controller to respond to no-op\n");
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005908 for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
5909 if (hpsa_noop(pdev) == 0)
5910 break;
5911 else
5912 dev_warn(&pdev->dev, "no-op failed%s\n",
5913 (i < 11 ? "; re-trying" : ""));
5914 }
5915 return 0;
5916}
5917
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005918static int hpsa_allocate_cmd_pool(struct ctlr_info *h)
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05005919{
5920 h->cmd_pool_bits = kzalloc(
5921 DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
5922 sizeof(unsigned long), GFP_KERNEL);
5923 h->cmd_pool = pci_alloc_consistent(h->pdev,
5924 h->nr_cmds * sizeof(*h->cmd_pool),
5925 &(h->cmd_pool_dhandle));
5926 h->errinfo_pool = pci_alloc_consistent(h->pdev,
5927 h->nr_cmds * sizeof(*h->errinfo_pool),
5928 &(h->errinfo_pool_dhandle));
5929 if ((h->cmd_pool_bits == NULL)
5930 || (h->cmd_pool == NULL)
5931 || (h->errinfo_pool == NULL)) {
5932 dev_err(&h->pdev->dev, "out of memory in %s", __func__);
5933 return -ENOMEM;
5934 }
5935 return 0;
5936}
5937
5938static void hpsa_free_cmd_pool(struct ctlr_info *h)
5939{
5940 kfree(h->cmd_pool_bits);
5941 if (h->cmd_pool)
5942 pci_free_consistent(h->pdev,
5943 h->nr_cmds * sizeof(struct CommandList),
5944 h->cmd_pool, h->cmd_pool_dhandle);
Stephen M. Cameronaca90122014-02-18 13:56:14 -06005945 if (h->ioaccel2_cmd_pool)
5946 pci_free_consistent(h->pdev,
5947 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
5948 h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05005949 if (h->errinfo_pool)
5950 pci_free_consistent(h->pdev,
5951 h->nr_cmds * sizeof(struct ErrorInfo),
5952 h->errinfo_pool,
5953 h->errinfo_pool_dhandle);
Matt Gatese1f7de02014-02-18 13:55:17 -06005954 if (h->ioaccel_cmd_pool)
5955 pci_free_consistent(h->pdev,
5956 h->nr_cmds * sizeof(struct io_accel1_cmd),
5957 h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05005958}
5959
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05005960static int hpsa_request_irq(struct ctlr_info *h,
5961 irqreturn_t (*msixhandler)(int, void *),
5962 irqreturn_t (*intxhandler)(int, void *))
5963{
Matt Gates254f7962012-05-01 11:43:06 -05005964 int rc, i;
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05005965
Matt Gates254f7962012-05-01 11:43:06 -05005966 /*
5967 * initialize h->q[x] = x so that interrupt handlers know which
5968 * queue to process.
5969 */
5970 for (i = 0; i < MAX_REPLY_QUEUES; i++)
5971 h->q[i] = (u8) i;
5972
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005973 if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
Matt Gates254f7962012-05-01 11:43:06 -05005974 /* If performant mode and MSI-X, use multiple reply queues */
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005975 for (i = 0; i < h->msix_vector; i++)
Matt Gates254f7962012-05-01 11:43:06 -05005976 rc = request_irq(h->intr[i], msixhandler,
5977 0, h->devname,
5978 &h->q[i]);
5979 } else {
5980 /* Use single reply pool */
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005981 if (h->msix_vector > 0 || h->msi_vector) {
Matt Gates254f7962012-05-01 11:43:06 -05005982 rc = request_irq(h->intr[h->intr_mode],
5983 msixhandler, 0, h->devname,
5984 &h->q[h->intr_mode]);
5985 } else {
5986 rc = request_irq(h->intr[h->intr_mode],
5987 intxhandler, IRQF_SHARED, h->devname,
5988 &h->q[h->intr_mode]);
5989 }
5990 }
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05005991 if (rc) {
5992 dev_err(&h->pdev->dev, "unable to get irq %d for %s\n",
5993 h->intr[h->intr_mode], h->devname);
5994 return -ENODEV;
5995 }
5996 return 0;
5997}
5998
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005999static int hpsa_kdump_soft_reset(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006000{
6001 if (hpsa_send_host_reset(h, RAID_CTLR_LUNID,
6002 HPSA_RESET_TYPE_CONTROLLER)) {
6003 dev_warn(&h->pdev->dev, "Resetting array controller failed.\n");
6004 return -EIO;
6005 }
6006
6007 dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
6008 if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) {
6009 dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
6010 return -1;
6011 }
6012
6013 dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
6014 if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) {
6015 dev_warn(&h->pdev->dev, "Board failed to become ready "
6016 "after soft reset.\n");
6017 return -1;
6018 }
6019
6020 return 0;
6021}
6022
Matt Gates254f7962012-05-01 11:43:06 -05006023static void free_irqs(struct ctlr_info *h)
6024{
6025 int i;
6026
6027 if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
6028 /* Single reply queue, only one irq to free */
6029 i = h->intr_mode;
6030 free_irq(h->intr[i], &h->q[i]);
6031 return;
6032 }
6033
Hannes Reineckeeee0f032014-01-15 13:30:53 +01006034 for (i = 0; i < h->msix_vector; i++)
Matt Gates254f7962012-05-01 11:43:06 -05006035 free_irq(h->intr[i], &h->q[i]);
6036}
6037
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05006038static void hpsa_free_irqs_and_disable_msix(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006039{
Matt Gates254f7962012-05-01 11:43:06 -05006040 free_irqs(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006041#ifdef CONFIG_PCI_MSI
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05006042 if (h->msix_vector) {
6043 if (h->pdev->msix_enabled)
6044 pci_disable_msix(h->pdev);
6045 } else if (h->msi_vector) {
6046 if (h->pdev->msi_enabled)
6047 pci_disable_msi(h->pdev);
6048 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006049#endif /* CONFIG_PCI_MSI */
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05006050}
6051
6052static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
6053{
6054 hpsa_free_irqs_and_disable_msix(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006055 hpsa_free_sg_chain_blocks(h);
6056 hpsa_free_cmd_pool(h);
Matt Gatese1f7de02014-02-18 13:55:17 -06006057 kfree(h->ioaccel1_blockFetchTable);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006058 kfree(h->blockFetchTable);
6059 pci_free_consistent(h->pdev, h->reply_pool_size,
6060 h->reply_pool, h->reply_pool_dhandle);
6061 if (h->vaddr)
6062 iounmap(h->vaddr);
6063 if (h->transtable)
6064 iounmap(h->transtable);
6065 if (h->cfgtable)
6066 iounmap(h->cfgtable);
6067 pci_release_regions(h->pdev);
6068 kfree(h);
6069}
6070
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006071/* Called when controller lockup detected. */
6072static void fail_all_cmds_on_list(struct ctlr_info *h, struct list_head *list)
6073{
6074 struct CommandList *c = NULL;
6075
6076 assert_spin_locked(&h->lock);
6077 /* Mark all outstanding commands as failed and complete them. */
6078 while (!list_empty(list)) {
6079 c = list_entry(list->next, struct CommandList, list);
6080 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05006081 finish_cmd(c);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006082 }
6083}
6084
6085static void controller_lockup_detected(struct ctlr_info *h)
6086{
6087 unsigned long flags;
6088
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006089 h->access.set_intr_mask(h, HPSA_INTR_OFF);
6090 spin_lock_irqsave(&h->lock, flags);
6091 h->lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
6092 spin_unlock_irqrestore(&h->lock, flags);
6093 dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x\n",
6094 h->lockup_detected);
6095 pci_disable_device(h->pdev);
6096 spin_lock_irqsave(&h->lock, flags);
6097 fail_all_cmds_on_list(h, &h->cmpQ);
6098 fail_all_cmds_on_list(h, &h->reqQ);
6099 spin_unlock_irqrestore(&h->lock, flags);
6100}
6101
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006102static void detect_controller_lockup(struct ctlr_info *h)
6103{
6104 u64 now;
6105 u32 heartbeat;
6106 unsigned long flags;
6107
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006108 now = get_jiffies_64();
6109 /* If we've received an interrupt recently, we're ok. */
6110 if (time_after64(h->last_intr_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05006111 (h->heartbeat_sample_interval), now))
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006112 return;
6113
6114 /*
6115 * If we've already checked the heartbeat recently, we're ok.
6116 * This could happen if someone sends us a signal. We
6117 * otherwise don't care about signals in this thread.
6118 */
6119 if (time_after64(h->last_heartbeat_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05006120 (h->heartbeat_sample_interval), now))
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006121 return;
6122
6123 /* If heartbeat has not changed since we last looked, we're not ok. */
6124 spin_lock_irqsave(&h->lock, flags);
6125 heartbeat = readl(&h->cfgtable->HeartBeat);
6126 spin_unlock_irqrestore(&h->lock, flags);
6127 if (h->last_heartbeat == heartbeat) {
6128 controller_lockup_detected(h);
6129 return;
6130 }
6131
6132 /* We're ok. */
6133 h->last_heartbeat = heartbeat;
6134 h->last_heartbeat_timestamp = now;
6135}
6136
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006137static int hpsa_kickoff_rescan(struct ctlr_info *h)
6138{
6139 int i;
6140 char *event_type;
6141
Scott Teele863d682014-02-18 13:57:05 -06006142 /* Clear the driver-requested rescan flag */
6143 h->drv_req_rescan = 0;
6144
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006145 /* Ask the controller to clear the events we're handling. */
Stephen M. Cameron1f7cee82014-02-18 13:56:09 -06006146 if ((h->transMethod & (CFGTBL_Trans_io_accel1
6147 | CFGTBL_Trans_io_accel2)) &&
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006148 (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
6149 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
6150
6151 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
6152 event_type = "state change";
6153 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
6154 event_type = "configuration change";
6155 /* Stop sending new RAID offload reqs via the IO accelerator */
6156 scsi_block_requests(h->scsi_host);
6157 for (i = 0; i < h->ndevices; i++)
6158 h->dev[i]->offload_enabled = 0;
6159 hpsa_drain_commands(h);
6160 /* Set 'accelerator path config change' bit */
6161 dev_warn(&h->pdev->dev,
6162 "Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
6163 h->events, event_type);
6164 writel(h->events, &(h->cfgtable->clear_event_notify));
6165 /* Set the "clear event notify field update" bit 6 */
6166 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
6167 /* Wait until ctlr clears 'clear event notify field', bit 6 */
6168 hpsa_wait_for_clear_event_notify_ack(h);
6169 scsi_unblock_requests(h->scsi_host);
6170 } else {
6171 /* Acknowledge controller notification events. */
6172 writel(h->events, &(h->cfgtable->clear_event_notify));
6173 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
6174 hpsa_wait_for_clear_event_notify_ack(h);
6175#if 0
6176 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
6177 hpsa_wait_for_mode_change_ack(h);
6178#endif
6179 }
6180
6181 /* Something in the device list may have changed to trigger
6182 * the event, so do a rescan.
6183 */
6184 hpsa_scan_start(h->scsi_host);
6185 /* release reference taken on scsi host in check_controller_events */
6186 scsi_host_put(h->scsi_host);
6187 return 0;
6188}
6189
6190/* Check a register on the controller to see if there are configuration
6191 * changes (added/changed/removed logical drives, etc.) which mean that
Scott Teele863d682014-02-18 13:57:05 -06006192 * we should rescan the controller for devices.
6193 * Also check flag for driver-initiated rescan.
6194 * If either flag or controller event indicate rescan, add the controller
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006195 * to the list of controllers needing to be rescanned, and gets a
6196 * reference to the associated scsi_host.
6197 */
6198static void hpsa_ctlr_needs_rescan(struct ctlr_info *h)
6199{
6200 if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
6201 return;
6202
6203 h->events = readl(&(h->cfgtable->event_notify));
Scott Teele863d682014-02-18 13:57:05 -06006204 if (!h->events && !h->drv_req_rescan)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006205 return;
6206
6207 /*
6208 * Take a reference on scsi host for the duration of the scan
6209 * Release in hpsa_kickoff_rescan(). No lock needed for scan_list
6210 * as only a single thread accesses this list.
6211 */
6212 scsi_host_get(h->scsi_host);
6213 hpsa_kickoff_rescan(h);
6214}
6215
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006216static void hpsa_monitor_ctlr_worker(struct work_struct *work)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006217{
6218 unsigned long flags;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006219 struct ctlr_info *h = container_of(to_delayed_work(work),
6220 struct ctlr_info, monitor_ctlr_work);
6221 detect_controller_lockup(h);
6222 if (h->lockup_detected)
6223 return;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006224 hpsa_ctlr_needs_rescan(h);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006225 spin_lock_irqsave(&h->lock, flags);
6226 if (h->remove_in_progress) {
6227 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006228 return;
6229 }
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006230 schedule_delayed_work(&h->monitor_ctlr_work,
6231 h->heartbeat_sample_interval);
6232 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006233}
6234
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006235static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006236{
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006237 int dac, rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006238 struct ctlr_info *h;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006239 int try_soft_reset = 0;
6240 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006241
6242 if (number_of_controllers == 0)
6243 printk(KERN_INFO DRIVER_NAME "\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006244
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006245 rc = hpsa_init_reset_devices(pdev);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006246 if (rc) {
6247 if (rc != -ENOTSUPP)
6248 return rc;
6249 /* If the reset fails in a particular way (it has no way to do
6250 * a proper hard reset, so returns -ENOTSUPP) we can try to do
6251 * a soft reset once we get the controller configured up to the
6252 * point that it can accept a command.
6253 */
6254 try_soft_reset = 1;
6255 rc = 0;
6256 }
6257
6258reinit_after_soft_reset:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006259
Don Brace303932f2010-02-04 08:42:40 -06006260 /* Command structures must be aligned on a 32-byte boundary because
6261 * the 5 lower bits of the address are used by the hardware. and by
6262 * the driver. See comments in hpsa.h for more info.
6263 */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006264#define COMMANDLIST_ALIGNMENT 128
Don Brace303932f2010-02-04 08:42:40 -06006265 BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006266 h = kzalloc(sizeof(*h), GFP_KERNEL);
6267 if (!h)
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006268 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006269
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05006270 h->pdev = pdev;
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06006271 h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06006272 INIT_LIST_HEAD(&h->cmpQ);
6273 INIT_LIST_HEAD(&h->reqQ);
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06006274 spin_lock_init(&h->lock);
6275 spin_lock_init(&h->scan_lock);
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006276 spin_lock_init(&h->passthru_count_lock);
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05006277 rc = hpsa_pci_init(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006278 if (rc != 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006279 goto clean1;
6280
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06006281 sprintf(h->devname, HPSA "%d", number_of_controllers);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006282 h->ctlr = number_of_controllers;
6283 number_of_controllers++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006284
6285 /* configure PCI DMA stuff */
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006286 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
6287 if (rc == 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006288 dac = 1;
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006289 } else {
6290 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6291 if (rc == 0) {
6292 dac = 0;
6293 } else {
6294 dev_err(&pdev->dev, "no suitable DMA available\n");
6295 goto clean1;
6296 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006297 }
6298
6299 /* make sure the board interrupts are off */
6300 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006301
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05006302 if (hpsa_request_irq(h, do_hpsa_intr_msi, do_hpsa_intr_intx))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006303 goto clean2;
Don Brace303932f2010-02-04 08:42:40 -06006304 dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
6305 h->devname, pdev->device,
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06006306 h->intr[h->intr_mode], dac ? "" : " not");
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05006307 if (hpsa_allocate_cmd_pool(h))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006308 goto clean4;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06006309 if (hpsa_allocate_sg_chain_blocks(h))
6310 goto clean4;
Stephen M. Camerona08a84712010-02-04 08:43:16 -06006311 init_waitqueue_head(&h->scan_wait_queue);
6312 h->scan_finished = 1; /* no scan currently in progress */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006313
6314 pci_set_drvdata(pdev, h);
Stephen M. Cameron9a413382011-05-03 14:59:41 -05006315 h->ndevices = 0;
6316 h->scsi_host = NULL;
6317 spin_lock_init(&h->devlock);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006318 hpsa_put_ctlr_into_performant_mode(h);
6319
6320 /* At this point, the controller is ready to take commands.
6321 * Now, if reset_devices and the hard reset didn't work, try
6322 * the soft reset and see if that works.
6323 */
6324 if (try_soft_reset) {
6325
6326 /* This is kind of gross. We may or may not get a completion
6327 * from the soft reset command, and if we do, then the value
6328 * from the fifo may or may not be valid. So, we wait 10 secs
6329 * after the reset throwing away any completions we get during
6330 * that time. Unregister the interrupt handler and register
6331 * fake ones to scoop up any residual completions.
6332 */
6333 spin_lock_irqsave(&h->lock, flags);
6334 h->access.set_intr_mask(h, HPSA_INTR_OFF);
6335 spin_unlock_irqrestore(&h->lock, flags);
Matt Gates254f7962012-05-01 11:43:06 -05006336 free_irqs(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006337 rc = hpsa_request_irq(h, hpsa_msix_discard_completions,
6338 hpsa_intx_discard_completions);
6339 if (rc) {
6340 dev_warn(&h->pdev->dev, "Failed to request_irq after "
6341 "soft reset.\n");
6342 goto clean4;
6343 }
6344
6345 rc = hpsa_kdump_soft_reset(h);
6346 if (rc)
6347 /* Neither hard nor soft reset worked, we're hosed. */
6348 goto clean4;
6349
6350 dev_info(&h->pdev->dev, "Board READY.\n");
6351 dev_info(&h->pdev->dev,
6352 "Waiting for stale completions to drain.\n");
6353 h->access.set_intr_mask(h, HPSA_INTR_ON);
6354 msleep(10000);
6355 h->access.set_intr_mask(h, HPSA_INTR_OFF);
6356
6357 rc = controller_reset_failed(h->cfgtable);
6358 if (rc)
6359 dev_info(&h->pdev->dev,
6360 "Soft reset appears to have failed.\n");
6361
6362 /* since the controller's reset, we have to go back and re-init
6363 * everything. Easiest to just forget what we've done and do it
6364 * all over again.
6365 */
6366 hpsa_undo_allocations_after_kdump_soft_reset(h);
6367 try_soft_reset = 0;
6368 if (rc)
6369 /* don't go to clean4, we already unallocated */
6370 return -ENODEV;
6371
6372 goto reinit_after_soft_reset;
6373 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006374
Scott Teelda0697b2014-02-18 13:57:00 -06006375 /* Enable Accelerated IO path at driver layer */
6376 h->acciopath_status = 1;
6377
Scott Teele863d682014-02-18 13:57:05 -06006378 h->drv_req_rescan = 0;
6379
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006380 /* Turn the interrupts on so we can service requests */
6381 h->access.set_intr_mask(h, HPSA_INTR_ON);
6382
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06006383 hpsa_hba_inquiry(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006384 hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006385
6386 /* Monitor the controller for firmware lockups */
6387 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
6388 INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
6389 schedule_delayed_work(&h->monitor_ctlr_work,
6390 h->heartbeat_sample_interval);
Stephen M. Cameron88bf6d62013-11-01 11:02:25 -05006391 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006392
6393clean4:
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06006394 hpsa_free_sg_chain_blocks(h);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05006395 hpsa_free_cmd_pool(h);
Matt Gates254f7962012-05-01 11:43:06 -05006396 free_irqs(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006397clean2:
6398clean1:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006399 kfree(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006400 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006401}
6402
6403static void hpsa_flush_cache(struct ctlr_info *h)
6404{
6405 char *flush_buf;
6406 struct CommandList *c;
Stephen M. Cameron702890e2013-09-23 13:33:30 -05006407 unsigned long flags;
6408
6409 /* Don't bother trying to flush the cache if locked up */
6410 spin_lock_irqsave(&h->lock, flags);
6411 if (unlikely(h->lockup_detected)) {
6412 spin_unlock_irqrestore(&h->lock, flags);
6413 return;
6414 }
6415 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006416
6417 flush_buf = kzalloc(4, GFP_KERNEL);
6418 if (!flush_buf)
6419 return;
6420
6421 c = cmd_special_alloc(h);
6422 if (!c) {
6423 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
6424 goto out_of_memory;
6425 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006426 if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
6427 RAID_CTLR_LUNID, TYPE_CMD)) {
6428 goto out;
6429 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006430 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
6431 if (c->err_info->CommandStatus != 0)
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006432out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006433 dev_warn(&h->pdev->dev,
6434 "error flushing cache on controller\n");
6435 cmd_special_free(h, c);
6436out_of_memory:
6437 kfree(flush_buf);
6438}
6439
6440static void hpsa_shutdown(struct pci_dev *pdev)
6441{
6442 struct ctlr_info *h;
6443
6444 h = pci_get_drvdata(pdev);
6445 /* Turn board interrupts off and send the flush cache command
6446 * sendcmd will turn off interrupt, and send the flush...
6447 * To write all data in the battery backed cache to disks
6448 */
6449 hpsa_flush_cache(h);
6450 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05006451 hpsa_free_irqs_and_disable_msix(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006452}
6453
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006454static void hpsa_free_device_info(struct ctlr_info *h)
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06006455{
6456 int i;
6457
6458 for (i = 0; i < h->ndevices; i++)
6459 kfree(h->dev[i]);
6460}
6461
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006462static void hpsa_remove_one(struct pci_dev *pdev)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006463{
6464 struct ctlr_info *h;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006465 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006466
6467 if (pci_get_drvdata(pdev) == NULL) {
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006468 dev_err(&pdev->dev, "unable to remove device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006469 return;
6470 }
6471 h = pci_get_drvdata(pdev);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006472
6473 /* Get rid of any controller monitoring work items */
6474 spin_lock_irqsave(&h->lock, flags);
6475 h->remove_in_progress = 1;
6476 cancel_delayed_work(&h->monitor_ctlr_work);
6477 spin_unlock_irqrestore(&h->lock, flags);
6478
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006479 hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */
6480 hpsa_shutdown(pdev);
6481 iounmap(h->vaddr);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05006482 iounmap(h->transtable);
6483 iounmap(h->cfgtable);
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06006484 hpsa_free_device_info(h);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06006485 hpsa_free_sg_chain_blocks(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006486 pci_free_consistent(h->pdev,
6487 h->nr_cmds * sizeof(struct CommandList),
6488 h->cmd_pool, h->cmd_pool_dhandle);
6489 pci_free_consistent(h->pdev,
6490 h->nr_cmds * sizeof(struct ErrorInfo),
6491 h->errinfo_pool, h->errinfo_pool_dhandle);
Don Brace303932f2010-02-04 08:42:40 -06006492 pci_free_consistent(h->pdev, h->reply_pool_size,
6493 h->reply_pool, h->reply_pool_dhandle);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006494 kfree(h->cmd_pool_bits);
Don Brace303932f2010-02-04 08:42:40 -06006495 kfree(h->blockFetchTable);
Matt Gatese1f7de02014-02-18 13:55:17 -06006496 kfree(h->ioaccel1_blockFetchTable);
Stephen M. Cameronaca90122014-02-18 13:56:14 -06006497 kfree(h->ioaccel2_blockFetchTable);
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06006498 kfree(h->hba_inquiry_data);
Stephen M. Cameronf0bd0b682012-05-01 11:42:09 -05006499 pci_disable_device(pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006500 pci_release_regions(pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006501 kfree(h);
6502}
6503
6504static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
6505 __attribute__((unused)) pm_message_t state)
6506{
6507 return -ENOSYS;
6508}
6509
6510static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
6511{
6512 return -ENOSYS;
6513}
6514
6515static struct pci_driver hpsa_pci_driver = {
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06006516 .name = HPSA,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006517 .probe = hpsa_init_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006518 .remove = hpsa_remove_one,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006519 .id_table = hpsa_pci_device_id, /* id_table */
6520 .shutdown = hpsa_shutdown,
6521 .suspend = hpsa_suspend,
6522 .resume = hpsa_resume,
6523};
6524
Don Brace303932f2010-02-04 08:42:40 -06006525/* Fill in bucket_map[], given nsgs (the max number of
6526 * scatter gather elements supported) and bucket[],
6527 * which is an array of 8 integers. The bucket[] array
6528 * contains 8 different DMA transfer sizes (in 16
6529 * byte increments) which the controller uses to fetch
6530 * commands. This function fills in bucket_map[], which
6531 * maps a given number of scatter gather elements to one of
6532 * the 8 DMA transfer sizes. The point of it is to allow the
6533 * controller to only do as much DMA as needed to fetch the
6534 * command, with the DMA transfer size encoded in the lower
6535 * bits of the command address.
6536 */
6537static void calc_bucket_map(int bucket[], int num_buckets,
Matt Gatese1f7de02014-02-18 13:55:17 -06006538 int nsgs, int min_blocks, int *bucket_map)
Don Brace303932f2010-02-04 08:42:40 -06006539{
6540 int i, j, b, size;
6541
Don Brace303932f2010-02-04 08:42:40 -06006542 /* Note, bucket_map must have nsgs+1 entries. */
6543 for (i = 0; i <= nsgs; i++) {
6544 /* Compute size of a command with i SG entries */
Matt Gatese1f7de02014-02-18 13:55:17 -06006545 size = i + min_blocks;
Don Brace303932f2010-02-04 08:42:40 -06006546 b = num_buckets; /* Assume the biggest bucket */
6547 /* Find the bucket that is just big enough */
Matt Gatese1f7de02014-02-18 13:55:17 -06006548 for (j = 0; j < num_buckets; j++) {
Don Brace303932f2010-02-04 08:42:40 -06006549 if (bucket[j] >= size) {
6550 b = j;
6551 break;
6552 }
6553 }
6554 /* for a command with i SG entries, use bucket b. */
6555 bucket_map[i] = b;
6556 }
6557}
6558
Matt Gatese1f7de02014-02-18 13:55:17 -06006559static void hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
Don Brace303932f2010-02-04 08:42:40 -06006560{
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006561 int i;
6562 unsigned long register_value;
Matt Gatese1f7de02014-02-18 13:55:17 -06006563 unsigned long transMethod = CFGTBL_Trans_Performant |
6564 (trans_support & CFGTBL_Trans_use_short_tags) |
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006565 CFGTBL_Trans_enable_directed_msix |
6566 (trans_support & (CFGTBL_Trans_io_accel1 |
6567 CFGTBL_Trans_io_accel2));
Matt Gatese1f7de02014-02-18 13:55:17 -06006568 struct access_method access = SA5_performant_access;
Stephen M. Camerondef342b2010-05-27 15:14:39 -05006569
6570 /* This is a bit complicated. There are 8 registers on
6571 * the controller which we write to to tell it 8 different
6572 * sizes of commands which there may be. It's a way of
6573 * reducing the DMA done to fetch each command. Encoded into
6574 * each command's tag are 3 bits which communicate to the controller
6575 * which of the eight sizes that command fits within. The size of
6576 * each command depends on how many scatter gather entries there are.
6577 * Each SG entry requires 16 bytes. The eight registers are programmed
6578 * with the number of 16-byte blocks a command of that size requires.
6579 * The smallest command possible requires 5 such 16 byte blocks.
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006580 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
Stephen M. Camerondef342b2010-05-27 15:14:39 -05006581 * blocks. Note, this only extends to the SG entries contained
6582 * within the command block, and does not extend to chained blocks
6583 * of SG elements. bft[] contains the eight values we write to
6584 * the registers. They are not evenly distributed, but have more
6585 * sizes for small commands, and fewer sizes for larger commands.
6586 */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006587 int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006588#define MIN_IOACCEL2_BFT_ENTRY 5
6589#define HPSA_IOACCEL2_HEADER_SZ 4
6590 int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
6591 13, 14, 15, 16, 17, 18, 19,
6592 HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
6593 BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
6594 BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
6595 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
6596 16 * MIN_IOACCEL2_BFT_ENTRY);
6597 BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006598 BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
Don Brace303932f2010-02-04 08:42:40 -06006599 /* 5 = 1 s/g entry or 4k
6600 * 6 = 2 s/g entry or 8k
6601 * 8 = 4 s/g entry or 16k
6602 * 10 = 6 s/g entry or 24k
6603 */
Don Brace303932f2010-02-04 08:42:40 -06006604
Don Brace303932f2010-02-04 08:42:40 -06006605 /* Controller spec: zero out this buffer. */
6606 memset(h->reply_pool, 0, h->reply_pool_size);
Don Brace303932f2010-02-04 08:42:40 -06006607
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006608 bft[7] = SG_ENTRIES_IN_CMD + 4;
6609 calc_bucket_map(bft, ARRAY_SIZE(bft),
Matt Gatese1f7de02014-02-18 13:55:17 -06006610 SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
Don Brace303932f2010-02-04 08:42:40 -06006611 for (i = 0; i < 8; i++)
6612 writel(bft[i], &h->transtable->BlockFetch[i]);
6613
6614 /* size of controller ring buffer */
6615 writel(h->max_commands, &h->transtable->RepQSize);
Matt Gates254f7962012-05-01 11:43:06 -05006616 writel(h->nreply_queues, &h->transtable->RepQCount);
Don Brace303932f2010-02-04 08:42:40 -06006617 writel(0, &h->transtable->RepQCtrAddrLow32);
6618 writel(0, &h->transtable->RepQCtrAddrHigh32);
Matt Gates254f7962012-05-01 11:43:06 -05006619
6620 for (i = 0; i < h->nreply_queues; i++) {
6621 writel(0, &h->transtable->RepQAddr[i].upper);
6622 writel(h->reply_pool_dhandle +
6623 (h->max_commands * sizeof(u64) * i),
6624 &h->transtable->RepQAddr[i].lower);
6625 }
6626
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006627 writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
Matt Gatese1f7de02014-02-18 13:55:17 -06006628 writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
6629 /*
6630 * enable outbound interrupt coalescing in accelerator mode;
6631 */
6632 if (trans_support & CFGTBL_Trans_io_accel1) {
6633 access = SA5_ioaccel_mode1_access;
6634 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
6635 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
Scott Teelc3497752014-02-18 13:56:34 -06006636 } else {
6637 if (trans_support & CFGTBL_Trans_io_accel2) {
6638 access = SA5_ioaccel_mode2_access;
6639 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
6640 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
6641 }
Matt Gatese1f7de02014-02-18 13:55:17 -06006642 }
Don Brace303932f2010-02-04 08:42:40 -06006643 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05006644 hpsa_wait_for_mode_change_ack(h);
Don Brace303932f2010-02-04 08:42:40 -06006645 register_value = readl(&(h->cfgtable->TransportActive));
6646 if (!(register_value & CFGTBL_Trans_Performant)) {
6647 dev_warn(&h->pdev->dev, "unable to get board into"
6648 " performant mode\n");
6649 return;
6650 }
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06006651 /* Change the access methods to the performant access methods */
Matt Gatese1f7de02014-02-18 13:55:17 -06006652 h->access = access;
6653 h->transMethod = transMethod;
6654
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006655 if (!((trans_support & CFGTBL_Trans_io_accel1) ||
6656 (trans_support & CFGTBL_Trans_io_accel2)))
Matt Gatese1f7de02014-02-18 13:55:17 -06006657 return;
6658
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006659 if (trans_support & CFGTBL_Trans_io_accel1) {
6660 /* Set up I/O accelerator mode */
6661 for (i = 0; i < h->nreply_queues; i++) {
6662 writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
6663 h->reply_queue[i].current_entry =
6664 readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
6665 }
6666 bft[7] = h->ioaccel_maxsg + 8;
6667 calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
6668 h->ioaccel1_blockFetchTable);
6669
6670 /* initialize all reply queue entries to unused */
6671 memset(h->reply_pool, (u8) IOACCEL_MODE1_REPLY_UNUSED,
6672 h->reply_pool_size);
6673
6674 /* set all the constant fields in the accelerator command
6675 * frames once at init time to save CPU cycles later.
6676 */
6677 for (i = 0; i < h->nr_cmds; i++) {
6678 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
6679
6680 cp->function = IOACCEL1_FUNCTION_SCSIIO;
6681 cp->err_info = (u32) (h->errinfo_pool_dhandle +
6682 (i * sizeof(struct ErrorInfo)));
6683 cp->err_info_len = sizeof(struct ErrorInfo);
6684 cp->sgl_offset = IOACCEL1_SGLOFFSET;
6685 cp->host_context_flags = IOACCEL1_HCFLAGS_CISS_FORMAT;
6686 cp->timeout_sec = 0;
6687 cp->ReplyQueue = 0;
6688 cp->Tag.lower = (i << DIRECT_LOOKUP_SHIFT) |
6689 DIRECT_LOOKUP_BIT;
6690 cp->Tag.upper = 0;
6691 cp->host_addr.lower =
6692 (u32) (h->ioaccel_cmd_pool_dhandle +
6693 (i * sizeof(struct io_accel1_cmd)));
6694 cp->host_addr.upper = 0;
6695 }
6696 } else if (trans_support & CFGTBL_Trans_io_accel2) {
6697 u64 cfg_offset, cfg_base_addr_index;
6698 u32 bft2_offset, cfg_base_addr;
6699 int rc;
6700
6701 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
6702 &cfg_base_addr_index, &cfg_offset);
6703 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
6704 bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
6705 calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
6706 4, h->ioaccel2_blockFetchTable);
6707 bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
6708 BUILD_BUG_ON(offsetof(struct CfgTable,
6709 io_accel_request_size_offset) != 0xb8);
6710 h->ioaccel2_bft2_regs =
6711 remap_pci_mem(pci_resource_start(h->pdev,
6712 cfg_base_addr_index) +
6713 cfg_offset + bft2_offset,
6714 ARRAY_SIZE(bft2) *
6715 sizeof(*h->ioaccel2_bft2_regs));
6716 for (i = 0; i < ARRAY_SIZE(bft2); i++)
6717 writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
Matt Gatese1f7de02014-02-18 13:55:17 -06006718 }
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006719 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
6720 hpsa_wait_for_mode_change_ack(h);
Matt Gatese1f7de02014-02-18 13:55:17 -06006721}
6722
6723static int hpsa_alloc_ioaccel_cmd_and_bft(struct ctlr_info *h)
6724{
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006725 h->ioaccel_maxsg =
6726 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
6727 if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
6728 h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
6729
Matt Gatese1f7de02014-02-18 13:55:17 -06006730 /* Command structures must be aligned on a 128-byte boundary
6731 * because the 7 lower bits of the address are used by the
6732 * hardware.
6733 */
6734#define IOACCEL1_COMMANDLIST_ALIGNMENT 128
6735 BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
6736 IOACCEL1_COMMANDLIST_ALIGNMENT);
6737 h->ioaccel_cmd_pool =
6738 pci_alloc_consistent(h->pdev,
6739 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
6740 &(h->ioaccel_cmd_pool_dhandle));
6741
6742 h->ioaccel1_blockFetchTable =
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006743 kmalloc(((h->ioaccel_maxsg + 1) *
Matt Gatese1f7de02014-02-18 13:55:17 -06006744 sizeof(u32)), GFP_KERNEL);
6745
6746 if ((h->ioaccel_cmd_pool == NULL) ||
6747 (h->ioaccel1_blockFetchTable == NULL))
6748 goto clean_up;
6749
6750 memset(h->ioaccel_cmd_pool, 0,
6751 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
6752 return 0;
6753
6754clean_up:
6755 if (h->ioaccel_cmd_pool)
6756 pci_free_consistent(h->pdev,
6757 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
6758 h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
6759 kfree(h->ioaccel1_blockFetchTable);
6760 return 1;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006761}
6762
Stephen M. Cameronaca90122014-02-18 13:56:14 -06006763static int ioaccel2_alloc_cmds_and_bft(struct ctlr_info *h)
6764{
6765 /* Allocate ioaccel2 mode command blocks and block fetch table */
6766
6767 h->ioaccel_maxsg =
6768 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
6769 if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
6770 h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
6771
6772#define IOACCEL2_COMMANDLIST_ALIGNMENT 128
6773 BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
6774 IOACCEL2_COMMANDLIST_ALIGNMENT);
6775 h->ioaccel2_cmd_pool =
6776 pci_alloc_consistent(h->pdev,
6777 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
6778 &(h->ioaccel2_cmd_pool_dhandle));
6779
6780 h->ioaccel2_blockFetchTable =
6781 kmalloc(((h->ioaccel_maxsg + 1) *
6782 sizeof(u32)), GFP_KERNEL);
6783
6784 if ((h->ioaccel2_cmd_pool == NULL) ||
6785 (h->ioaccel2_blockFetchTable == NULL))
6786 goto clean_up;
6787
6788 memset(h->ioaccel2_cmd_pool, 0,
6789 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
6790 return 0;
6791
6792clean_up:
6793 if (h->ioaccel2_cmd_pool)
6794 pci_free_consistent(h->pdev,
6795 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
6796 h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
6797 kfree(h->ioaccel2_blockFetchTable);
6798 return 1;
6799}
6800
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006801static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006802{
6803 u32 trans_support;
Matt Gatese1f7de02014-02-18 13:55:17 -06006804 unsigned long transMethod = CFGTBL_Trans_Performant |
6805 CFGTBL_Trans_use_short_tags;
Matt Gates254f7962012-05-01 11:43:06 -05006806 int i;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006807
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -06006808 if (hpsa_simple_mode)
6809 return;
6810
Matt Gatese1f7de02014-02-18 13:55:17 -06006811 /* Check for I/O accelerator mode support */
6812 if (trans_support & CFGTBL_Trans_io_accel1) {
6813 transMethod |= CFGTBL_Trans_io_accel1 |
6814 CFGTBL_Trans_enable_directed_msix;
6815 if (hpsa_alloc_ioaccel_cmd_and_bft(h))
6816 goto clean_up;
Stephen M. Cameronaca90122014-02-18 13:56:14 -06006817 } else {
6818 if (trans_support & CFGTBL_Trans_io_accel2) {
6819 transMethod |= CFGTBL_Trans_io_accel2 |
6820 CFGTBL_Trans_enable_directed_msix;
6821 if (ioaccel2_alloc_cmds_and_bft(h))
6822 goto clean_up;
6823 }
Matt Gatese1f7de02014-02-18 13:55:17 -06006824 }
6825
6826 /* TODO, check that this next line h->nreply_queues is correct */
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006827 trans_support = readl(&(h->cfgtable->TransportSupport));
6828 if (!(trans_support & PERFORMANT_MODE))
6829 return;
6830
Hannes Reineckeeee0f032014-01-15 13:30:53 +01006831 h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05006832 hpsa_get_max_perf_mode_cmds(h);
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006833 /* Performant mode ring buffer and supporting data structures */
Matt Gates254f7962012-05-01 11:43:06 -05006834 h->reply_pool_size = h->max_commands * sizeof(u64) * h->nreply_queues;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006835 h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size,
6836 &(h->reply_pool_dhandle));
6837
Matt Gates254f7962012-05-01 11:43:06 -05006838 for (i = 0; i < h->nreply_queues; i++) {
6839 h->reply_queue[i].head = &h->reply_pool[h->max_commands * i];
6840 h->reply_queue[i].size = h->max_commands;
6841 h->reply_queue[i].wraparound = 1; /* spec: init to 1 */
6842 h->reply_queue[i].current_entry = 0;
6843 }
6844
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006845 /* Need a block fetch table for performant mode */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006846 h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006847 sizeof(u32)), GFP_KERNEL);
6848
6849 if ((h->reply_pool == NULL)
6850 || (h->blockFetchTable == NULL))
6851 goto clean_up;
6852
Matt Gatese1f7de02014-02-18 13:55:17 -06006853 hpsa_enter_performant_mode(h, trans_support);
Don Brace303932f2010-02-04 08:42:40 -06006854 return;
6855
6856clean_up:
6857 if (h->reply_pool)
6858 pci_free_consistent(h->pdev, h->reply_pool_size,
6859 h->reply_pool, h->reply_pool_dhandle);
6860 kfree(h->blockFetchTable);
6861}
6862
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006863static void hpsa_drain_commands(struct ctlr_info *h)
6864{
6865 int cmds_out;
6866 unsigned long flags;
6867
6868 do { /* wait for all outstanding commands to drain out */
6869 spin_lock_irqsave(&h->lock, flags);
6870 cmds_out = h->commands_outstanding;
6871 spin_unlock_irqrestore(&h->lock, flags);
6872 if (cmds_out <= 0)
6873 break;
6874 msleep(100);
6875 } while (1);
6876}
6877
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006878/*
6879 * This is it. Register the PCI driver information for the cards we control
6880 * the OS will call our registered routines when it finds one of our cards.
6881 */
6882static int __init hpsa_init(void)
6883{
Mike Miller31468402010-02-25 14:03:12 -06006884 return pci_register_driver(&hpsa_pci_driver);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006885}
6886
6887static void __exit hpsa_cleanup(void)
6888{
6889 pci_unregister_driver(&hpsa_pci_driver);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006890}
6891
Matt Gatese1f7de02014-02-18 13:55:17 -06006892static void __attribute__((unused)) verify_offsets(void)
6893{
6894#define VERIFY_OFFSET(member, offset) \
Mike Millerb66cc252014-02-18 13:56:04 -06006895 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
6896
6897 VERIFY_OFFSET(IU_type, 0);
6898 VERIFY_OFFSET(direction, 1);
6899 VERIFY_OFFSET(reply_queue, 2);
6900 /* VERIFY_OFFSET(reserved1, 3); */
6901 VERIFY_OFFSET(scsi_nexus, 4);
6902 VERIFY_OFFSET(Tag, 8);
6903 VERIFY_OFFSET(cdb, 16);
6904 VERIFY_OFFSET(cciss_lun, 32);
6905 VERIFY_OFFSET(data_len, 40);
6906 VERIFY_OFFSET(cmd_priority_task_attr, 44);
6907 VERIFY_OFFSET(sg_count, 45);
6908 /* VERIFY_OFFSET(reserved3 */
6909 VERIFY_OFFSET(err_ptr, 48);
6910 VERIFY_OFFSET(err_len, 56);
6911 /* VERIFY_OFFSET(reserved4 */
6912 VERIFY_OFFSET(sg, 64);
6913
6914#undef VERIFY_OFFSET
6915
6916#define VERIFY_OFFSET(member, offset) \
Matt Gatese1f7de02014-02-18 13:55:17 -06006917 BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
6918
6919 VERIFY_OFFSET(dev_handle, 0x00);
6920 VERIFY_OFFSET(reserved1, 0x02);
6921 VERIFY_OFFSET(function, 0x03);
6922 VERIFY_OFFSET(reserved2, 0x04);
6923 VERIFY_OFFSET(err_info, 0x0C);
6924 VERIFY_OFFSET(reserved3, 0x10);
6925 VERIFY_OFFSET(err_info_len, 0x12);
6926 VERIFY_OFFSET(reserved4, 0x13);
6927 VERIFY_OFFSET(sgl_offset, 0x14);
6928 VERIFY_OFFSET(reserved5, 0x15);
6929 VERIFY_OFFSET(transfer_len, 0x1C);
6930 VERIFY_OFFSET(reserved6, 0x20);
6931 VERIFY_OFFSET(io_flags, 0x24);
6932 VERIFY_OFFSET(reserved7, 0x26);
6933 VERIFY_OFFSET(LUN, 0x34);
6934 VERIFY_OFFSET(control, 0x3C);
6935 VERIFY_OFFSET(CDB, 0x40);
6936 VERIFY_OFFSET(reserved8, 0x50);
6937 VERIFY_OFFSET(host_context_flags, 0x60);
6938 VERIFY_OFFSET(timeout_sec, 0x62);
6939 VERIFY_OFFSET(ReplyQueue, 0x64);
6940 VERIFY_OFFSET(reserved9, 0x65);
6941 VERIFY_OFFSET(Tag, 0x68);
6942 VERIFY_OFFSET(host_addr, 0x70);
6943 VERIFY_OFFSET(CISS_LUN, 0x78);
6944 VERIFY_OFFSET(SG, 0x78 + 8);
6945#undef VERIFY_OFFSET
6946}
6947
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006948module_init(hpsa_init);
6949module_exit(hpsa_cleanup);