blob: a599a7b51a921cfcabca6e9c6c1141ea051ec086 [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;
1460 cmd->result = DID_SOFT_ERROR << 16;
1461 cmd_free(h, c);
1462 cmd->scsi_done(cmd);
1463 return;
1464 }
1465 handle_ioaccel_mode2_error(h, c, cmd, c2);
1466 cmd_free(h, c);
1467 cmd->scsi_done(cmd);
1468}
1469
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05001470static void complete_scsi_command(struct CommandList *cp)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001471{
1472 struct scsi_cmnd *cmd;
1473 struct ctlr_info *h;
1474 struct ErrorInfo *ei;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001475 struct hpsa_scsi_dev_t *dev;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001476
1477 unsigned char sense_key;
1478 unsigned char asc; /* additional sense code */
1479 unsigned char ascq; /* additional sense code qualifier */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05001480 unsigned long sense_data_size;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001481
1482 ei = cp->err_info;
1483 cmd = (struct scsi_cmnd *) cp->scsi_cmd;
1484 h = cp->h;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001485 dev = cmd->device->hostdata;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001486
1487 scsi_dma_unmap(cmd); /* undo the DMA mappings */
Matt Gatese1f7de02014-02-18 13:55:17 -06001488 if ((cp->cmd_type == CMD_SCSI) &&
1489 (cp->Header.SGTotal > h->max_cmd_sg_entries))
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001490 hpsa_unmap_sg_chain_block(h, cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001491
1492 cmd->result = (DID_OK << 16); /* host byte */
1493 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
Scott Teelc3497752014-02-18 13:56:34 -06001494
1495 if (cp->cmd_type == CMD_IOACCEL2)
1496 return process_ioaccel2_completion(h, cp, cmd, dev);
1497
Stephen M. Cameron55126722010-02-25 14:03:01 -06001498 cmd->result |= ei->ScsiStatus;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001499
1500 /* copy the sense data whether we need to or not. */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05001501 if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
1502 sense_data_size = SCSI_SENSE_BUFFERSIZE;
1503 else
1504 sense_data_size = sizeof(ei->SenseInfo);
1505 if (ei->SenseLen < sense_data_size)
1506 sense_data_size = ei->SenseLen;
1507
1508 memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001509 scsi_set_resid(cmd, ei->ResidualCnt);
1510
1511 if (ei->CommandStatus == 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001512 cmd_free(h, cp);
Tomas Henzl2cc5bfa2013-08-01 15:14:00 +02001513 cmd->scsi_done(cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001514 return;
1515 }
1516
Matt Gatese1f7de02014-02-18 13:55:17 -06001517 /* For I/O accelerator commands, copy over some fields to the normal
1518 * CISS header used below for error handling.
1519 */
1520 if (cp->cmd_type == CMD_IOACCEL1) {
1521 struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
1522 cp->Header.SGList = cp->Header.SGTotal = scsi_sg_count(cmd);
1523 cp->Request.CDBLen = c->io_flags & IOACCEL1_IOFLAGS_CDBLEN_MASK;
1524 cp->Header.Tag.lower = c->Tag.lower;
1525 cp->Header.Tag.upper = c->Tag.upper;
1526 memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
1527 memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001528
1529 /* Any RAID offload error results in retry which will use
1530 * the normal I/O path so the controller can handle whatever's
1531 * wrong.
1532 */
1533 if (is_logical_dev_addr_mode(dev->scsi3addr)) {
1534 if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
1535 dev->offload_enabled = 0;
1536 cmd->result = DID_SOFT_ERROR << 16;
1537 cmd_free(h, cp);
1538 cmd->scsi_done(cmd);
1539 return;
1540 }
Matt Gatese1f7de02014-02-18 13:55:17 -06001541 }
1542
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001543 /* an error has occurred */
1544 switch (ei->CommandStatus) {
1545
1546 case CMD_TARGET_STATUS:
1547 if (ei->ScsiStatus) {
1548 /* Get sense key */
1549 sense_key = 0xf & ei->SenseInfo[2];
1550 /* Get additional sense code */
1551 asc = ei->SenseInfo[12];
1552 /* Get addition sense code qualifier */
1553 ascq = ei->SenseInfo[13];
1554 }
1555
1556 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
Matt Gates3ce438d2013-12-04 17:10:36 -06001557 if (check_for_unit_attention(h, cp))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001558 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001559 if (sense_key == ILLEGAL_REQUEST) {
1560 /*
1561 * SCSI REPORT_LUNS is commonly unsupported on
1562 * Smart Array. Suppress noisy complaint.
1563 */
1564 if (cp->Request.CDB[0] == REPORT_LUNS)
1565 break;
1566
1567 /* If ASC/ASCQ indicate Logical Unit
1568 * Not Supported condition,
1569 */
1570 if ((asc == 0x25) && (ascq == 0x0)) {
1571 dev_warn(&h->pdev->dev, "cp %p "
1572 "has check condition\n", cp);
1573 break;
1574 }
1575 }
1576
1577 if (sense_key == NOT_READY) {
1578 /* If Sense is Not Ready, Logical Unit
1579 * Not ready, Manual Intervention
1580 * required
1581 */
1582 if ((asc == 0x04) && (ascq == 0x03)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001583 dev_warn(&h->pdev->dev, "cp %p "
1584 "has check condition: unit "
1585 "not ready, manual "
1586 "intervention required\n", cp);
1587 break;
1588 }
1589 }
Matt Gates1d3b3602010-02-04 08:43:00 -06001590 if (sense_key == ABORTED_COMMAND) {
1591 /* Aborted command is retryable */
1592 dev_warn(&h->pdev->dev, "cp %p "
1593 "has check condition: aborted command: "
1594 "ASC: 0x%x, ASCQ: 0x%x\n",
1595 cp, asc, ascq);
Stephen M. Cameron2e311fb2013-09-23 13:33:41 -05001596 cmd->result |= DID_SOFT_ERROR << 16;
Matt Gates1d3b3602010-02-04 08:43:00 -06001597 break;
1598 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001599 /* Must be some other type of check condition */
Stephen M. Cameron21b8e4e2012-05-01 11:42:25 -05001600 dev_dbg(&h->pdev->dev, "cp %p has check condition: "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001601 "unknown type: "
1602 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1603 "Returning result: 0x%x, "
1604 "cmd=[%02x %02x %02x %02x %02x "
Mike Miller807be732010-02-04 08:43:26 -06001605 "%02x %02x %02x %02x %02x %02x "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001606 "%02x %02x %02x %02x %02x]\n",
1607 cp, sense_key, asc, ascq,
1608 cmd->result,
1609 cmd->cmnd[0], cmd->cmnd[1],
1610 cmd->cmnd[2], cmd->cmnd[3],
1611 cmd->cmnd[4], cmd->cmnd[5],
1612 cmd->cmnd[6], cmd->cmnd[7],
Mike Miller807be732010-02-04 08:43:26 -06001613 cmd->cmnd[8], cmd->cmnd[9],
1614 cmd->cmnd[10], cmd->cmnd[11],
1615 cmd->cmnd[12], cmd->cmnd[13],
1616 cmd->cmnd[14], cmd->cmnd[15]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001617 break;
1618 }
1619
1620
1621 /* Problem was not a check condition
1622 * Pass it up to the upper layers...
1623 */
1624 if (ei->ScsiStatus) {
1625 dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
1626 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1627 "Returning result: 0x%x\n",
1628 cp, ei->ScsiStatus,
1629 sense_key, asc, ascq,
1630 cmd->result);
1631 } else { /* scsi status is zero??? How??? */
1632 dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
1633 "Returning no connection.\n", cp),
1634
1635 /* Ordinarily, this case should never happen,
1636 * but there is a bug in some released firmware
1637 * revisions that allows it to happen if, for
1638 * example, a 4100 backplane loses power and
1639 * the tape drive is in it. We assume that
1640 * it's a fatal error of some kind because we
1641 * can't show that it wasn't. We will make it
1642 * look like selection timeout since that is
1643 * the most common reason for this to occur,
1644 * and it's severe enough.
1645 */
1646
1647 cmd->result = DID_NO_CONNECT << 16;
1648 }
1649 break;
1650
1651 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1652 break;
1653 case CMD_DATA_OVERRUN:
1654 dev_warn(&h->pdev->dev, "cp %p has"
1655 " completed with data overrun "
1656 "reported\n", cp);
1657 break;
1658 case CMD_INVALID: {
1659 /* print_bytes(cp, sizeof(*cp), 1, 0);
1660 print_cmd(cp); */
1661 /* We get CMD_INVALID if you address a non-existent device
1662 * instead of a selection timeout (no response). You will
1663 * see this if you yank out a drive, then try to access it.
1664 * This is kind of a shame because it means that any other
1665 * CMD_INVALID (e.g. driver bug) will get interpreted as a
1666 * missing target. */
1667 cmd->result = DID_NO_CONNECT << 16;
1668 }
1669 break;
1670 case CMD_PROTOCOL_ERR:
Stephen M. Cameron256d0ea2012-09-14 16:34:25 -05001671 cmd->result = DID_ERROR << 16;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001672 dev_warn(&h->pdev->dev, "cp %p has "
Stephen M. Cameron256d0ea2012-09-14 16:34:25 -05001673 "protocol error\n", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001674 break;
1675 case CMD_HARDWARE_ERR:
1676 cmd->result = DID_ERROR << 16;
1677 dev_warn(&h->pdev->dev, "cp %p had hardware error\n", cp);
1678 break;
1679 case CMD_CONNECTION_LOST:
1680 cmd->result = DID_ERROR << 16;
1681 dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
1682 break;
1683 case CMD_ABORTED:
1684 cmd->result = DID_ABORT << 16;
1685 dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
1686 cp, ei->ScsiStatus);
1687 break;
1688 case CMD_ABORT_FAILED:
1689 cmd->result = DID_ERROR << 16;
1690 dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
1691 break;
1692 case CMD_UNSOLICITED_ABORT:
Stephen M. Cameronf6e76052011-07-26 11:08:52 -05001693 cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
1694 dev_warn(&h->pdev->dev, "cp %p aborted due to an unsolicited "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001695 "abort\n", cp);
1696 break;
1697 case CMD_TIMEOUT:
1698 cmd->result = DID_TIME_OUT << 16;
1699 dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
1700 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06001701 case CMD_UNABORTABLE:
1702 cmd->result = DID_ERROR << 16;
1703 dev_warn(&h->pdev->dev, "Command unabortable\n");
1704 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001705 case CMD_IOACCEL_DISABLED:
1706 /* This only handles the direct pass-through case since RAID
1707 * offload is handled above. Just attempt a retry.
1708 */
1709 cmd->result = DID_SOFT_ERROR << 16;
1710 dev_warn(&h->pdev->dev,
1711 "cp %p had HP SSD Smart Path error\n", cp);
1712 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001713 default:
1714 cmd->result = DID_ERROR << 16;
1715 dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
1716 cp, ei->CommandStatus);
1717 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001718 cmd_free(h, cp);
Tomas Henzl2cc5bfa2013-08-01 15:14:00 +02001719 cmd->scsi_done(cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001720}
1721
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001722static void hpsa_pci_unmap(struct pci_dev *pdev,
1723 struct CommandList *c, int sg_used, int data_direction)
1724{
1725 int i;
1726 union u64bit addr64;
1727
1728 for (i = 0; i < sg_used; i++) {
1729 addr64.val32.lower = c->SG[i].Addr.lower;
1730 addr64.val32.upper = c->SG[i].Addr.upper;
1731 pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len,
1732 data_direction);
1733 }
1734}
1735
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001736static int hpsa_map_one(struct pci_dev *pdev,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001737 struct CommandList *cp,
1738 unsigned char *buf,
1739 size_t buflen,
1740 int data_direction)
1741{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001742 u64 addr64;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001743
1744 if (buflen == 0 || data_direction == PCI_DMA_NONE) {
1745 cp->Header.SGList = 0;
1746 cp->Header.SGTotal = 0;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001747 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001748 }
1749
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001750 addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction);
Shuah Khaneceaae12013-02-20 11:24:34 -06001751 if (dma_mapping_error(&pdev->dev, addr64)) {
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001752 /* Prevent subsequent unmap of something never mapped */
Shuah Khaneceaae12013-02-20 11:24:34 -06001753 cp->Header.SGList = 0;
1754 cp->Header.SGTotal = 0;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001755 return -1;
Shuah Khaneceaae12013-02-20 11:24:34 -06001756 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001757 cp->SG[0].Addr.lower =
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001758 (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001759 cp->SG[0].Addr.upper =
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001760 (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001761 cp->SG[0].Len = buflen;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06001762 cp->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining */
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001763 cp->Header.SGList = (u8) 1; /* no. SGs contig in this cmd */
1764 cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001765 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001766}
1767
1768static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
1769 struct CommandList *c)
1770{
1771 DECLARE_COMPLETION_ONSTACK(wait);
1772
1773 c->waiting = &wait;
1774 enqueue_cmd_and_start_io(h, c);
1775 wait_for_completion(&wait);
1776}
1777
Stephen M. Camerona0c12412011-10-26 16:22:04 -05001778static void hpsa_scsi_do_simple_cmd_core_if_no_lockup(struct ctlr_info *h,
1779 struct CommandList *c)
1780{
1781 unsigned long flags;
1782
1783 /* If controller lockup detected, fake a hardware error. */
1784 spin_lock_irqsave(&h->lock, flags);
1785 if (unlikely(h->lockup_detected)) {
1786 spin_unlock_irqrestore(&h->lock, flags);
1787 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
1788 } else {
1789 spin_unlock_irqrestore(&h->lock, flags);
1790 hpsa_scsi_do_simple_cmd_core(h, c);
1791 }
1792}
1793
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001794#define MAX_DRIVER_CMD_RETRIES 25
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001795static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
1796 struct CommandList *c, int data_direction)
1797{
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001798 int backoff_time = 10, retry_count = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001799
1800 do {
Joe Perches7630abd2011-05-08 23:32:40 -07001801 memset(c->err_info, 0, sizeof(*c->err_info));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001802 hpsa_scsi_do_simple_cmd_core(h, c);
1803 retry_count++;
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001804 if (retry_count > 3) {
1805 msleep(backoff_time);
1806 if (backoff_time < 1000)
1807 backoff_time *= 2;
1808 }
Matt Bondurant852af202012-05-01 11:42:35 -05001809 } while ((check_for_unit_attention(h, c) ||
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001810 check_for_busy(h, c)) &&
1811 retry_count <= MAX_DRIVER_CMD_RETRIES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001812 hpsa_pci_unmap(h->pdev, c, 1, data_direction);
1813}
1814
1815static void hpsa_scsi_interpret_error(struct CommandList *cp)
1816{
1817 struct ErrorInfo *ei;
1818 struct device *d = &cp->h->pdev->dev;
1819
1820 ei = cp->err_info;
1821 switch (ei->CommandStatus) {
1822 case CMD_TARGET_STATUS:
1823 dev_warn(d, "cmd %p has completed with errors\n", cp);
1824 dev_warn(d, "cmd %p has SCSI Status = %x\n", cp,
1825 ei->ScsiStatus);
1826 if (ei->ScsiStatus == 0)
1827 dev_warn(d, "SCSI status is abnormally zero. "
1828 "(probably indicates selection timeout "
1829 "reported incorrectly due to a known "
1830 "firmware bug, circa July, 2001.)\n");
1831 break;
1832 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1833 dev_info(d, "UNDERRUN\n");
1834 break;
1835 case CMD_DATA_OVERRUN:
1836 dev_warn(d, "cp %p has completed with data overrun\n", cp);
1837 break;
1838 case CMD_INVALID: {
1839 /* controller unfortunately reports SCSI passthru's
1840 * to non-existent targets as invalid commands.
1841 */
1842 dev_warn(d, "cp %p is reported invalid (probably means "
1843 "target device no longer present)\n", cp);
1844 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
1845 print_cmd(cp); */
1846 }
1847 break;
1848 case CMD_PROTOCOL_ERR:
1849 dev_warn(d, "cp %p has protocol error \n", cp);
1850 break;
1851 case CMD_HARDWARE_ERR:
1852 /* cmd->result = DID_ERROR << 16; */
1853 dev_warn(d, "cp %p had hardware error\n", cp);
1854 break;
1855 case CMD_CONNECTION_LOST:
1856 dev_warn(d, "cp %p had connection lost\n", cp);
1857 break;
1858 case CMD_ABORTED:
1859 dev_warn(d, "cp %p was aborted\n", cp);
1860 break;
1861 case CMD_ABORT_FAILED:
1862 dev_warn(d, "cp %p reports abort failed\n", cp);
1863 break;
1864 case CMD_UNSOLICITED_ABORT:
1865 dev_warn(d, "cp %p aborted due to an unsolicited abort\n", cp);
1866 break;
1867 case CMD_TIMEOUT:
1868 dev_warn(d, "cp %p timed out\n", cp);
1869 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06001870 case CMD_UNABORTABLE:
1871 dev_warn(d, "Command unabortable\n");
1872 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001873 default:
1874 dev_warn(d, "cp %p returned unknown status %x\n", cp,
1875 ei->CommandStatus);
1876 }
1877}
1878
1879static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
1880 unsigned char page, unsigned char *buf,
1881 unsigned char bufsize)
1882{
1883 int rc = IO_OK;
1884 struct CommandList *c;
1885 struct ErrorInfo *ei;
1886
1887 c = cmd_special_alloc(h);
1888
1889 if (c == NULL) { /* trouble... */
1890 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06001891 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001892 }
1893
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001894 if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
1895 page, scsi3addr, TYPE_CMD)) {
1896 rc = -1;
1897 goto out;
1898 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001899 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1900 ei = c->err_info;
1901 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
1902 hpsa_scsi_interpret_error(c);
1903 rc = -1;
1904 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001905out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001906 cmd_special_free(h, c);
1907 return rc;
1908}
1909
Scott Teelbf711ac2014-02-18 13:56:39 -06001910static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
1911 u8 reset_type)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001912{
1913 int rc = IO_OK;
1914 struct CommandList *c;
1915 struct ErrorInfo *ei;
1916
1917 c = cmd_special_alloc(h);
1918
1919 if (c == NULL) { /* trouble... */
1920 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
Stephen M. Camerone9ea04a2010-02-25 14:03:06 -06001921 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001922 }
1923
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001924 /* fill_cmd can't fail here, no data buffer to map. */
Scott Teelbf711ac2014-02-18 13:56:39 -06001925 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
1926 scsi3addr, TYPE_MSG);
1927 c->Request.CDB[1] = reset_type; /* fill_cmd defaults to LUN reset */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001928 hpsa_scsi_do_simple_cmd_core(h, c);
1929 /* no unmap needed here because no data xfer. */
1930
1931 ei = c->err_info;
1932 if (ei->CommandStatus != 0) {
1933 hpsa_scsi_interpret_error(c);
1934 rc = -1;
1935 }
1936 cmd_special_free(h, c);
1937 return rc;
1938}
1939
1940static void hpsa_get_raid_level(struct ctlr_info *h,
1941 unsigned char *scsi3addr, unsigned char *raid_level)
1942{
1943 int rc;
1944 unsigned char *buf;
1945
1946 *raid_level = RAID_UNKNOWN;
1947 buf = kzalloc(64, GFP_KERNEL);
1948 if (!buf)
1949 return;
1950 rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0xC1, buf, 64);
1951 if (rc == 0)
1952 *raid_level = buf[8];
1953 if (*raid_level > RAID_UNKNOWN)
1954 *raid_level = RAID_UNKNOWN;
1955 kfree(buf);
1956 return;
1957}
1958
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001959#define HPSA_MAP_DEBUG
1960#ifdef HPSA_MAP_DEBUG
1961static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
1962 struct raid_map_data *map_buff)
1963{
1964 struct raid_map_disk_data *dd = &map_buff->data[0];
1965 int map, row, col;
1966 u16 map_cnt, row_cnt, disks_per_row;
1967
1968 if (rc != 0)
1969 return;
1970
1971 dev_info(&h->pdev->dev, "structure_size = %u\n",
1972 le32_to_cpu(map_buff->structure_size));
1973 dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
1974 le32_to_cpu(map_buff->volume_blk_size));
1975 dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
1976 le64_to_cpu(map_buff->volume_blk_cnt));
1977 dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
1978 map_buff->phys_blk_shift);
1979 dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
1980 map_buff->parity_rotation_shift);
1981 dev_info(&h->pdev->dev, "strip_size = %u\n",
1982 le16_to_cpu(map_buff->strip_size));
1983 dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
1984 le64_to_cpu(map_buff->disk_starting_blk));
1985 dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
1986 le64_to_cpu(map_buff->disk_blk_cnt));
1987 dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
1988 le16_to_cpu(map_buff->data_disks_per_row));
1989 dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
1990 le16_to_cpu(map_buff->metadata_disks_per_row));
1991 dev_info(&h->pdev->dev, "row_cnt = %u\n",
1992 le16_to_cpu(map_buff->row_cnt));
1993 dev_info(&h->pdev->dev, "layout_map_count = %u\n",
1994 le16_to_cpu(map_buff->layout_map_count));
1995
1996 map_cnt = le16_to_cpu(map_buff->layout_map_count);
1997 for (map = 0; map < map_cnt; map++) {
1998 dev_info(&h->pdev->dev, "Map%u:\n", map);
1999 row_cnt = le16_to_cpu(map_buff->row_cnt);
2000 for (row = 0; row < row_cnt; row++) {
2001 dev_info(&h->pdev->dev, " Row%u:\n", row);
2002 disks_per_row =
2003 le16_to_cpu(map_buff->data_disks_per_row);
2004 for (col = 0; col < disks_per_row; col++, dd++)
2005 dev_info(&h->pdev->dev,
2006 " D%02u: h=0x%04x xor=%u,%u\n",
2007 col, dd->ioaccel_handle,
2008 dd->xor_mult[0], dd->xor_mult[1]);
2009 disks_per_row =
2010 le16_to_cpu(map_buff->metadata_disks_per_row);
2011 for (col = 0; col < disks_per_row; col++, dd++)
2012 dev_info(&h->pdev->dev,
2013 " M%02u: h=0x%04x xor=%u,%u\n",
2014 col, dd->ioaccel_handle,
2015 dd->xor_mult[0], dd->xor_mult[1]);
2016 }
2017 }
2018}
2019#else
2020static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
2021 __attribute__((unused)) int rc,
2022 __attribute__((unused)) struct raid_map_data *map_buff)
2023{
2024}
2025#endif
2026
2027static int hpsa_get_raid_map(struct ctlr_info *h,
2028 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2029{
2030 int rc = 0;
2031 struct CommandList *c;
2032 struct ErrorInfo *ei;
2033
2034 c = cmd_special_alloc(h);
2035 if (c == NULL) {
2036 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2037 return -ENOMEM;
2038 }
2039 if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
2040 sizeof(this_device->raid_map), 0,
2041 scsi3addr, TYPE_CMD)) {
2042 dev_warn(&h->pdev->dev, "Out of memory in hpsa_get_raid_map()\n");
2043 cmd_special_free(h, c);
2044 return -ENOMEM;
2045 }
2046 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2047 ei = c->err_info;
2048 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
2049 hpsa_scsi_interpret_error(c);
2050 cmd_special_free(h, c);
2051 return -1;
2052 }
2053 cmd_special_free(h, c);
2054
2055 /* @todo in the future, dynamically allocate RAID map memory */
2056 if (le32_to_cpu(this_device->raid_map.structure_size) >
2057 sizeof(this_device->raid_map)) {
2058 dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
2059 rc = -1;
2060 }
2061 hpsa_debug_map_buff(h, rc, &this_device->raid_map);
2062 return rc;
2063}
2064
2065static void hpsa_get_ioaccel_status(struct ctlr_info *h,
2066 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2067{
2068 int rc;
2069 unsigned char *buf;
2070 u8 ioaccel_status;
2071
2072 this_device->offload_config = 0;
2073 this_device->offload_enabled = 0;
2074
2075 buf = kzalloc(64, GFP_KERNEL);
2076 if (!buf)
2077 return;
2078 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
2079 HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
2080 if (rc != 0)
2081 goto out;
2082
2083#define IOACCEL_STATUS_BYTE 4
2084#define OFFLOAD_CONFIGURED_BIT 0x01
2085#define OFFLOAD_ENABLED_BIT 0x02
2086 ioaccel_status = buf[IOACCEL_STATUS_BYTE];
2087 this_device->offload_config =
2088 !!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
2089 if (this_device->offload_config) {
2090 this_device->offload_enabled =
2091 !!(ioaccel_status & OFFLOAD_ENABLED_BIT);
2092 if (hpsa_get_raid_map(h, scsi3addr, this_device))
2093 this_device->offload_enabled = 0;
2094 }
2095out:
2096 kfree(buf);
2097 return;
2098}
2099
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002100/* Get the device id from inquiry page 0x83 */
2101static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
2102 unsigned char *device_id, int buflen)
2103{
2104 int rc;
2105 unsigned char *buf;
2106
2107 if (buflen > 16)
2108 buflen = 16;
2109 buf = kzalloc(64, GFP_KERNEL);
2110 if (!buf)
2111 return -1;
2112 rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0x83, buf, 64);
2113 if (rc == 0)
2114 memcpy(device_id, &buf[8], buflen);
2115 kfree(buf);
2116 return rc != 0;
2117}
2118
2119static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
2120 struct ReportLUNdata *buf, int bufsize,
2121 int extended_response)
2122{
2123 int rc = IO_OK;
2124 struct CommandList *c;
2125 unsigned char scsi3addr[8];
2126 struct ErrorInfo *ei;
2127
2128 c = cmd_special_alloc(h);
2129 if (c == NULL) { /* trouble... */
2130 dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2131 return -1;
2132 }
Stephen M. Camerone89c0ae2010-02-04 08:42:04 -06002133 /* address the controller */
2134 memset(scsi3addr, 0, sizeof(scsi3addr));
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002135 if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
2136 buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
2137 rc = -1;
2138 goto out;
2139 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002140 if (extended_response)
2141 c->Request.CDB[1] = extended_response;
2142 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2143 ei = c->err_info;
2144 if (ei->CommandStatus != 0 &&
2145 ei->CommandStatus != CMD_DATA_UNDERRUN) {
2146 hpsa_scsi_interpret_error(c);
2147 rc = -1;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002148 } else {
2149 if (buf->extended_response_flag != extended_response) {
2150 dev_err(&h->pdev->dev,
2151 "report luns requested format %u, got %u\n",
2152 extended_response,
2153 buf->extended_response_flag);
2154 rc = -1;
2155 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002156 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002157out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002158 cmd_special_free(h, c);
2159 return rc;
2160}
2161
2162static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
2163 struct ReportLUNdata *buf,
2164 int bufsize, int extended_response)
2165{
2166 return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
2167}
2168
2169static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
2170 struct ReportLUNdata *buf, int bufsize)
2171{
2172 return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
2173}
2174
2175static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
2176 int bus, int target, int lun)
2177{
2178 device->bus = bus;
2179 device->target = target;
2180 device->lun = lun;
2181}
2182
2183static int hpsa_update_device_info(struct ctlr_info *h,
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002184 unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
2185 unsigned char *is_OBDR_device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002186{
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002187
2188#define OBDR_SIG_OFFSET 43
2189#define OBDR_TAPE_SIG "$DR-10"
2190#define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
2191#define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
2192
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06002193 unsigned char *inq_buff;
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002194 unsigned char *obdr_sig;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002195
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06002196 inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002197 if (!inq_buff)
2198 goto bail_out;
2199
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002200 /* Do an inquiry to the device to see what it is. */
2201 if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
2202 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
2203 /* Inquiry failed (msg printed already) */
2204 dev_err(&h->pdev->dev,
2205 "hpsa_update_device_info: inquiry failed\n");
2206 goto bail_out;
2207 }
2208
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002209 this_device->devtype = (inq_buff[0] & 0x1f);
2210 memcpy(this_device->scsi3addr, scsi3addr, 8);
2211 memcpy(this_device->vendor, &inq_buff[8],
2212 sizeof(this_device->vendor));
2213 memcpy(this_device->model, &inq_buff[16],
2214 sizeof(this_device->model));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002215 memset(this_device->device_id, 0,
2216 sizeof(this_device->device_id));
2217 hpsa_get_device_id(h, scsi3addr, this_device->device_id,
2218 sizeof(this_device->device_id));
2219
2220 if (this_device->devtype == TYPE_DISK &&
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002221 is_logical_dev_addr_mode(scsi3addr)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002222 hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002223 if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
2224 hpsa_get_ioaccel_status(h, scsi3addr, this_device);
2225 } else {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002226 this_device->raid_level = RAID_UNKNOWN;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002227 this_device->offload_config = 0;
2228 this_device->offload_enabled = 0;
2229 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002230
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002231 if (is_OBDR_device) {
2232 /* See if this is a One-Button-Disaster-Recovery device
2233 * by looking for "$DR-10" at offset 43 in inquiry data.
2234 */
2235 obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
2236 *is_OBDR_device = (this_device->devtype == TYPE_ROM &&
2237 strncmp(obdr_sig, OBDR_TAPE_SIG,
2238 OBDR_SIG_LEN) == 0);
2239 }
2240
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002241 kfree(inq_buff);
2242 return 0;
2243
2244bail_out:
2245 kfree(inq_buff);
2246 return 1;
2247}
2248
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002249static unsigned char *ext_target_model[] = {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002250 "MSA2012",
2251 "MSA2024",
2252 "MSA2312",
2253 "MSA2324",
Stephen M. Cameronfda38512011-05-03 15:00:07 -05002254 "P2000 G3 SAS",
Stephen M. Camerone06c8e52013-09-23 13:33:56 -05002255 "MSA 2040 SAS",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002256 NULL,
2257};
2258
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002259static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002260{
2261 int i;
2262
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002263 for (i = 0; ext_target_model[i]; i++)
2264 if (strncmp(device->model, ext_target_model[i],
2265 strlen(ext_target_model[i])) == 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002266 return 1;
2267 return 0;
2268}
2269
2270/* Helper function to assign bus, target, lun mapping of devices.
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002271 * Puts non-external target logical volumes on bus 0, external target logical
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002272 * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
2273 * Logical drive target and lun are assigned at this time, but
2274 * physical device lun and target assignment are deferred (assigned
2275 * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
2276 */
2277static void figure_bus_target_lun(struct ctlr_info *h,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002278 u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002279{
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002280 u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002281
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002282 if (!is_logical_dev_addr_mode(lunaddrbytes)) {
2283 /* physical device, target and lun filled in later */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002284 if (is_hba_lunid(lunaddrbytes))
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002285 hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002286 else
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002287 /* defer target, lun assignment for physical devices */
2288 hpsa_set_bus_target_lun(device, 2, -1, -1);
2289 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002290 }
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002291 /* It's a logical device */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002292 if (is_ext_target(h, device)) {
2293 /* external target way, put logicals on bus 1
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002294 * and match target/lun numbers box
2295 * reports, other smart array, bus 0, target 0, match lunid
2296 */
2297 hpsa_set_bus_target_lun(device,
2298 1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
2299 return;
2300 }
2301 hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002302}
2303
2304/*
2305 * If there is no lun 0 on a target, linux won't find any devices.
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002306 * For the external targets (arrays), we have to manually detect the enclosure
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002307 * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
2308 * it for some reason. *tmpdevice is the target we're adding,
2309 * this_device is a pointer into the current element of currentsd[]
2310 * that we're building up in update_scsi_devices(), below.
2311 * lunzerobits is a bitmap that tracks which targets already have a
2312 * lun 0 assigned.
2313 * Returns 1 if an enclosure was added, 0 if not.
2314 */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002315static int add_ext_target_dev(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002316 struct hpsa_scsi_dev_t *tmpdevice,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002317 struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002318 unsigned long lunzerobits[], int *n_ext_target_devs)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002319{
2320 unsigned char scsi3addr[8];
2321
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002322 if (test_bit(tmpdevice->target, lunzerobits))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002323 return 0; /* There is already a lun 0 on this target. */
2324
2325 if (!is_logical_dev_addr_mode(lunaddrbytes))
2326 return 0; /* It's the logical targets that may lack lun 0. */
2327
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002328 if (!is_ext_target(h, tmpdevice))
2329 return 0; /* Only external target devices have this problem. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002330
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002331 if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002332 return 0;
2333
Stephen M. Cameronc4f8a292011-01-07 10:55:43 -06002334 memset(scsi3addr, 0, 8);
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002335 scsi3addr[3] = tmpdevice->target;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002336 if (is_hba_lunid(scsi3addr))
2337 return 0; /* Don't add the RAID controller here. */
2338
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002339 if (is_scsi_rev_5(h))
2340 return 0; /* p1210m doesn't need to do this. */
2341
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002342 if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
Scott Teelaca4a522012-01-19 14:01:19 -06002343 dev_warn(&h->pdev->dev, "Maximum number of external "
2344 "target devices exceeded. Check your hardware "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002345 "configuration.");
2346 return 0;
2347 }
2348
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002349 if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002350 return 0;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002351 (*n_ext_target_devs)++;
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002352 hpsa_set_bus_target_lun(this_device,
2353 tmpdevice->bus, tmpdevice->target, 0);
2354 set_bit(tmpdevice->target, lunzerobits);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002355 return 1;
2356}
2357
2358/*
Scott Teel54b6e9e2014-02-18 13:56:45 -06002359 * Get address of physical disk used for an ioaccel2 mode command:
2360 * 1. Extract ioaccel2 handle from the command.
2361 * 2. Find a matching ioaccel2 handle from list of physical disks.
2362 * 3. Return:
2363 * 1 and set scsi3addr to address of matching physical
2364 * 0 if no matching physical disk was found.
2365 */
2366static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
2367 struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
2368{
2369 struct ReportExtendedLUNdata *physicals = NULL;
2370 int responsesize = 24; /* size of physical extended response */
2371 int extended = 2; /* flag forces reporting 'other dev info'. */
2372 int reportsize = sizeof(*physicals) + HPSA_MAX_PHYS_LUN * responsesize;
2373 u32 nphysicals = 0; /* number of reported physical devs */
2374 int found = 0; /* found match (1) or not (0) */
2375 u32 find; /* handle we need to match */
2376 int i;
2377 struct scsi_cmnd *scmd; /* scsi command within request being aborted */
2378 struct hpsa_scsi_dev_t *d; /* device of request being aborted */
2379 struct io_accel2_cmd *c2a; /* ioaccel2 command to abort */
2380 u32 it_nexus; /* 4 byte device handle for the ioaccel2 cmd */
2381 u32 scsi_nexus; /* 4 byte device handle for the ioaccel2 cmd */
2382
2383 if (ioaccel2_cmd_to_abort->cmd_type != CMD_IOACCEL2)
2384 return 0; /* no match */
2385
2386 /* point to the ioaccel2 device handle */
2387 c2a = &h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
2388 if (c2a == NULL)
2389 return 0; /* no match */
2390
2391 scmd = (struct scsi_cmnd *) ioaccel2_cmd_to_abort->scsi_cmd;
2392 if (scmd == NULL)
2393 return 0; /* no match */
2394
2395 d = scmd->device->hostdata;
2396 if (d == NULL)
2397 return 0; /* no match */
2398
2399 it_nexus = cpu_to_le32((u32) d->ioaccel_handle);
2400 scsi_nexus = cpu_to_le32((u32) c2a->scsi_nexus);
2401 find = c2a->scsi_nexus;
2402
2403 /* Get the list of physical devices */
2404 physicals = kzalloc(reportsize, GFP_KERNEL);
2405 if (hpsa_scsi_do_report_phys_luns(h, (struct ReportLUNdata *) physicals,
2406 reportsize, extended)) {
2407 dev_err(&h->pdev->dev,
2408 "Can't lookup %s device handle: report physical LUNs failed.\n",
2409 "HP SSD Smart Path");
2410 kfree(physicals);
2411 return 0;
2412 }
2413 nphysicals = be32_to_cpu(*((__be32 *)physicals->LUNListLength)) /
2414 responsesize;
2415
2416
2417 /* find ioaccel2 handle in list of physicals: */
2418 for (i = 0; i < nphysicals; i++) {
2419 /* handle is in bytes 28-31 of each lun */
2420 if (memcmp(&((struct ReportExtendedLUNdata *)
2421 physicals)->LUN[i][20], &find, 4) != 0) {
2422 continue; /* didn't match */
2423 }
2424 found = 1;
2425 memcpy(scsi3addr, &((struct ReportExtendedLUNdata *)
2426 physicals)->LUN[i][0], 8);
2427 break; /* found it */
2428 }
2429
2430 kfree(physicals);
2431 if (found)
2432 return 1;
2433 else
2434 return 0;
2435
2436}
2437/*
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002438 * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
2439 * logdev. The number of luns in physdev and logdev are returned in
2440 * *nphysicals and *nlogicals, respectively.
2441 * Returns 0 on success, -1 otherwise.
2442 */
2443static int hpsa_gather_lun_info(struct ctlr_info *h,
2444 int reportlunsize,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002445 struct ReportLUNdata *physdev, u32 *nphysicals, int *physical_mode,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002446 struct ReportLUNdata *logdev, u32 *nlogicals)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002447{
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002448 int physical_entry_size = 8;
2449
2450 *physical_mode = 0;
2451
2452 /* For I/O accelerator mode we need to read physical device handles */
Mike MIller317d4ad2014-02-18 13:56:20 -06002453 if (h->transMethod & CFGTBL_Trans_io_accel1 ||
2454 h->transMethod & CFGTBL_Trans_io_accel2) {
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002455 *physical_mode = HPSA_REPORT_PHYS_EXTENDED;
2456 physical_entry_size = 24;
2457 }
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002458 if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002459 *physical_mode)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002460 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
2461 return -1;
2462 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002463 *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) /
2464 physical_entry_size;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002465 if (*nphysicals > HPSA_MAX_PHYS_LUN) {
2466 dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
2467 " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2468 *nphysicals - HPSA_MAX_PHYS_LUN);
2469 *nphysicals = HPSA_MAX_PHYS_LUN;
2470 }
2471 if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
2472 dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
2473 return -1;
2474 }
Stephen M. Cameron6df1e952010-02-04 08:42:19 -06002475 *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002476 /* Reject Logicals in excess of our max capability. */
2477 if (*nlogicals > HPSA_MAX_LUN) {
2478 dev_warn(&h->pdev->dev,
2479 "maximum logical LUNs (%d) exceeded. "
2480 "%d LUNs ignored.\n", HPSA_MAX_LUN,
2481 *nlogicals - HPSA_MAX_LUN);
2482 *nlogicals = HPSA_MAX_LUN;
2483 }
2484 if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
2485 dev_warn(&h->pdev->dev,
2486 "maximum logical + physical LUNs (%d) exceeded. "
2487 "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2488 *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
2489 *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
2490 }
2491 return 0;
2492}
2493
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002494u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i,
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002495 int nphysicals, int nlogicals,
2496 struct ReportExtendedLUNdata *physdev_list,
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002497 struct ReportLUNdata *logdev_list)
2498{
2499 /* Helper function, figure out where the LUN ID info is coming from
2500 * given index i, lists of physical and logical devices, where in
2501 * the list the raid controller is supposed to appear (first or last)
2502 */
2503
2504 int logicals_start = nphysicals + (raid_ctlr_position == 0);
2505 int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
2506
2507 if (i == raid_ctlr_position)
2508 return RAID_CTLR_LUNID;
2509
2510 if (i < logicals_start)
2511 return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0];
2512
2513 if (i < last_device)
2514 return &logdev_list->LUN[i - nphysicals -
2515 (raid_ctlr_position == 0)][0];
2516 BUG();
2517 return NULL;
2518}
2519
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002520static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
2521{
2522 /* the idea here is we could get notified
2523 * that some devices have changed, so we do a report
2524 * physical luns and report logical luns cmd, and adjust
2525 * our list of devices accordingly.
2526 *
2527 * The scsi3addr's of devices won't change so long as the
2528 * adapter is not reset. That means we can rescan and
2529 * tell which devices we already know about, vs. new
2530 * devices, vs. disappearing devices.
2531 */
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002532 struct ReportExtendedLUNdata *physdev_list = NULL;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002533 struct ReportLUNdata *logdev_list = NULL;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002534 u32 nphysicals = 0;
2535 u32 nlogicals = 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002536 int physical_mode = 0;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002537 u32 ndev_allocated = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002538 struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
2539 int ncurrent = 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002540 int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 24;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002541 int i, n_ext_target_devs, ndevs_to_allocate;
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002542 int raid_ctlr_position;
Scott Teelaca4a522012-01-19 14:01:19 -06002543 DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002544
Scott Teelcfe5bad2011-10-26 16:21:07 -05002545 currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002546 physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
2547 logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002548 tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
2549
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002550 if (!currentsd || !physdev_list || !logdev_list || !tmpdevice) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002551 dev_err(&h->pdev->dev, "out of memory\n");
2552 goto out;
2553 }
2554 memset(lunzerobits, 0, sizeof(lunzerobits));
2555
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002556 if (hpsa_gather_lun_info(h, reportlunsize,
2557 (struct ReportLUNdata *) physdev_list, &nphysicals,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002558 &physical_mode, logdev_list, &nlogicals))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002559 goto out;
2560
Scott Teelaca4a522012-01-19 14:01:19 -06002561 /* We might see up to the maximum number of logical and physical disks
2562 * plus external target devices, and a device for the local RAID
2563 * controller.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002564 */
Scott Teelaca4a522012-01-19 14:01:19 -06002565 ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002566
2567 /* Allocate the per device structures */
2568 for (i = 0; i < ndevs_to_allocate; i++) {
Scott Teelb7ec0212011-10-26 16:21:12 -05002569 if (i >= HPSA_MAX_DEVICES) {
2570 dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
2571 " %d devices ignored.\n", HPSA_MAX_DEVICES,
2572 ndevs_to_allocate - HPSA_MAX_DEVICES);
2573 break;
2574 }
2575
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002576 currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
2577 if (!currentsd[i]) {
2578 dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
2579 __FILE__, __LINE__);
2580 goto out;
2581 }
2582 ndev_allocated++;
2583 }
2584
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002585 if (unlikely(is_scsi_rev_5(h)))
2586 raid_ctlr_position = 0;
2587 else
2588 raid_ctlr_position = nphysicals + nlogicals;
2589
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002590 /* adjust our table of devices */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002591 n_ext_target_devs = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002592 for (i = 0; i < nphysicals + nlogicals + 1; i++) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002593 u8 *lunaddrbytes, is_OBDR = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002594
2595 /* Figure out where the LUN ID info is coming from */
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002596 lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
2597 i, nphysicals, nlogicals, physdev_list, logdev_list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002598 /* skip masked physical devices. */
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002599 if (lunaddrbytes[3] & 0xC0 &&
2600 i < nphysicals + (raid_ctlr_position == 0))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002601 continue;
2602
2603 /* Get device type, vendor, model, device id */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002604 if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
2605 &is_OBDR))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002606 continue; /* skip it if we can't talk to it. */
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002607 figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002608 this_device = currentsd[ncurrent];
2609
2610 /*
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002611 * For external target devices, we have to insert a LUN 0 which
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002612 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
2613 * is nonetheless an enclosure device there. We have to
2614 * present that otherwise linux won't find anything if
2615 * there is no lun 0.
2616 */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002617 if (add_ext_target_dev(h, tmpdevice, this_device,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002618 lunaddrbytes, lunzerobits,
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002619 &n_ext_target_devs)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002620 ncurrent++;
2621 this_device = currentsd[ncurrent];
2622 }
2623
2624 *this_device = *tmpdevice;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002625
2626 switch (this_device->devtype) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002627 case TYPE_ROM:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002628 /* We don't *really* support actual CD-ROM devices,
2629 * just "One Button Disaster Recovery" tape drive
2630 * which temporarily pretends to be a CD-ROM drive.
2631 * So we check that the device is really an OBDR tape
2632 * device by checking for "$DR-10" in bytes 43-48 of
2633 * the inquiry data.
2634 */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002635 if (is_OBDR)
2636 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002637 break;
2638 case TYPE_DISK:
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002639 if (i >= nphysicals) {
2640 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002641 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002642 }
2643 if (physical_mode == HPSA_REPORT_PHYS_EXTENDED) {
2644 memcpy(&this_device->ioaccel_handle,
2645 &lunaddrbytes[20],
2646 sizeof(this_device->ioaccel_handle));
2647 ncurrent++;
2648 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002649 break;
2650 case TYPE_TAPE:
2651 case TYPE_MEDIUM_CHANGER:
2652 ncurrent++;
2653 break;
2654 case TYPE_RAID:
2655 /* Only present the Smartarray HBA as a RAID controller.
2656 * If it's a RAID controller other than the HBA itself
2657 * (an external RAID controller, MSA500 or similar)
2658 * don't present it.
2659 */
2660 if (!is_hba_lunid(lunaddrbytes))
2661 break;
2662 ncurrent++;
2663 break;
2664 default:
2665 break;
2666 }
Scott Teelcfe5bad2011-10-26 16:21:07 -05002667 if (ncurrent >= HPSA_MAX_DEVICES)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002668 break;
2669 }
2670 adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
2671out:
2672 kfree(tmpdevice);
2673 for (i = 0; i < ndev_allocated; i++)
2674 kfree(currentsd[i]);
2675 kfree(currentsd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002676 kfree(physdev_list);
2677 kfree(logdev_list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002678}
2679
2680/* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
2681 * dma mapping and fills in the scatter gather entries of the
2682 * hpsa command, cp.
2683 */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002684static int hpsa_scatter_gather(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002685 struct CommandList *cp,
2686 struct scsi_cmnd *cmd)
2687{
2688 unsigned int len;
2689 struct scatterlist *sg;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002690 u64 addr64;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002691 int use_sg, i, sg_index, chained;
2692 struct SGDescriptor *curr_sg;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002693
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002694 BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002695
2696 use_sg = scsi_dma_map(cmd);
2697 if (use_sg < 0)
2698 return use_sg;
2699
2700 if (!use_sg)
2701 goto sglist_finished;
2702
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002703 curr_sg = cp->SG;
2704 chained = 0;
2705 sg_index = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002706 scsi_for_each_sg(cmd, sg, use_sg, i) {
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002707 if (i == h->max_cmd_sg_entries - 1 &&
2708 use_sg > h->max_cmd_sg_entries) {
2709 chained = 1;
2710 curr_sg = h->cmd_sg_list[cp->cmdindex];
2711 sg_index = 0;
2712 }
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002713 addr64 = (u64) sg_dma_address(sg);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002714 len = sg_dma_len(sg);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002715 curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
2716 curr_sg->Addr.upper = (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
2717 curr_sg->Len = len;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06002718 curr_sg->Ext = (i < scsi_sg_count(cmd) - 1) ? 0 : HPSA_SG_LAST;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002719 curr_sg++;
2720 }
2721
2722 if (use_sg + chained > h->maxSG)
2723 h->maxSG = use_sg + chained;
2724
2725 if (chained) {
2726 cp->Header.SGList = h->max_cmd_sg_entries;
2727 cp->Header.SGTotal = (u16) (use_sg + 1);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002728 if (hpsa_map_sg_chain_block(h, cp)) {
2729 scsi_dma_unmap(cmd);
2730 return -1;
2731 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002732 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002733 }
2734
2735sglist_finished:
2736
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002737 cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
2738 cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002739 return 0;
2740}
2741
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002742#define IO_ACCEL_INELIGIBLE (1)
2743static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
2744{
2745 int is_write = 0;
2746 u32 block;
2747 u32 block_cnt;
2748
2749 /* Perform some CDB fixups if needed using 10 byte reads/writes only */
2750 switch (cdb[0]) {
2751 case WRITE_6:
2752 case WRITE_12:
2753 is_write = 1;
2754 case READ_6:
2755 case READ_12:
2756 if (*cdb_len == 6) {
2757 block = (((u32) cdb[2]) << 8) | cdb[3];
2758 block_cnt = cdb[4];
2759 } else {
2760 BUG_ON(*cdb_len != 12);
2761 block = (((u32) cdb[2]) << 24) |
2762 (((u32) cdb[3]) << 16) |
2763 (((u32) cdb[4]) << 8) |
2764 cdb[5];
2765 block_cnt =
2766 (((u32) cdb[6]) << 24) |
2767 (((u32) cdb[7]) << 16) |
2768 (((u32) cdb[8]) << 8) |
2769 cdb[9];
2770 }
2771 if (block_cnt > 0xffff)
2772 return IO_ACCEL_INELIGIBLE;
2773
2774 cdb[0] = is_write ? WRITE_10 : READ_10;
2775 cdb[1] = 0;
2776 cdb[2] = (u8) (block >> 24);
2777 cdb[3] = (u8) (block >> 16);
2778 cdb[4] = (u8) (block >> 8);
2779 cdb[5] = (u8) (block);
2780 cdb[6] = 0;
2781 cdb[7] = (u8) (block_cnt >> 8);
2782 cdb[8] = (u8) (block_cnt);
2783 cdb[9] = 0;
2784 *cdb_len = 10;
2785 break;
2786 }
2787 return 0;
2788}
2789
Scott Teelc3497752014-02-18 13:56:34 -06002790static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002791 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
2792 u8 *scsi3addr)
Matt Gatese1f7de02014-02-18 13:55:17 -06002793{
2794 struct scsi_cmnd *cmd = c->scsi_cmd;
Matt Gatese1f7de02014-02-18 13:55:17 -06002795 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
2796 unsigned int len;
2797 unsigned int total_len = 0;
2798 struct scatterlist *sg;
2799 u64 addr64;
2800 int use_sg, i;
2801 struct SGDescriptor *curr_sg;
2802 u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
2803
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002804 /* TODO: implement chaining support */
2805 if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
2806 return IO_ACCEL_INELIGIBLE;
2807
Matt Gatese1f7de02014-02-18 13:55:17 -06002808 BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
2809
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002810 if (fixup_ioaccel_cdb(cdb, &cdb_len))
2811 return IO_ACCEL_INELIGIBLE;
2812
Matt Gatese1f7de02014-02-18 13:55:17 -06002813 c->cmd_type = CMD_IOACCEL1;
2814
2815 /* Adjust the DMA address to point to the accelerated command buffer */
2816 c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
2817 (c->cmdindex * sizeof(*cp));
2818 BUG_ON(c->busaddr & 0x0000007F);
2819
2820 use_sg = scsi_dma_map(cmd);
2821 if (use_sg < 0)
2822 return use_sg;
2823
2824 if (use_sg) {
2825 curr_sg = cp->SG;
2826 scsi_for_each_sg(cmd, sg, use_sg, i) {
2827 addr64 = (u64) sg_dma_address(sg);
2828 len = sg_dma_len(sg);
2829 total_len += len;
2830 curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
2831 curr_sg->Addr.upper =
2832 (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
2833 curr_sg->Len = len;
2834
2835 if (i == (scsi_sg_count(cmd) - 1))
2836 curr_sg->Ext = HPSA_SG_LAST;
2837 else
2838 curr_sg->Ext = 0; /* we are not chaining */
2839 curr_sg++;
2840 }
2841
2842 switch (cmd->sc_data_direction) {
2843 case DMA_TO_DEVICE:
2844 control |= IOACCEL1_CONTROL_DATA_OUT;
2845 break;
2846 case DMA_FROM_DEVICE:
2847 control |= IOACCEL1_CONTROL_DATA_IN;
2848 break;
2849 case DMA_NONE:
2850 control |= IOACCEL1_CONTROL_NODATAXFER;
2851 break;
2852 default:
2853 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
2854 cmd->sc_data_direction);
2855 BUG();
2856 break;
2857 }
2858 } else {
2859 control |= IOACCEL1_CONTROL_NODATAXFER;
2860 }
2861
Scott Teelc3497752014-02-18 13:56:34 -06002862 c->Header.SGList = use_sg;
Matt Gatese1f7de02014-02-18 13:55:17 -06002863 /* Fill out the command structure to submit */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002864 cp->dev_handle = ioaccel_handle & 0xFFFF;
Matt Gatese1f7de02014-02-18 13:55:17 -06002865 cp->transfer_len = total_len;
2866 cp->io_flags = IOACCEL1_IOFLAGS_IO_REQ |
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002867 (cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK);
Matt Gatese1f7de02014-02-18 13:55:17 -06002868 cp->control = control;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002869 memcpy(cp->CDB, cdb, cdb_len);
2870 memcpy(cp->CISS_LUN, scsi3addr, 8);
Scott Teelc3497752014-02-18 13:56:34 -06002871 /* Tag was already set at init time. */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002872 enqueue_cmd_and_start_io(h, c);
Matt Gatese1f7de02014-02-18 13:55:17 -06002873 return 0;
2874}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002875
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002876/*
2877 * Queue a command directly to a device behind the controller using the
2878 * I/O accelerator path.
2879 */
2880static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
2881 struct CommandList *c)
2882{
2883 struct scsi_cmnd *cmd = c->scsi_cmd;
2884 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
2885
2886 return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
2887 cmd->cmnd, cmd->cmd_len, dev->scsi3addr);
2888}
2889
Scott Teelc3497752014-02-18 13:56:34 -06002890static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
2891 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
2892 u8 *scsi3addr)
2893{
2894 struct scsi_cmnd *cmd = c->scsi_cmd;
2895 struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
2896 struct ioaccel2_sg_element *curr_sg;
2897 int use_sg, i;
2898 struct scatterlist *sg;
2899 u64 addr64;
2900 u32 len;
2901 u32 total_len = 0;
2902
2903 if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
2904 return IO_ACCEL_INELIGIBLE;
2905
2906 if (fixup_ioaccel_cdb(cdb, &cdb_len))
2907 return IO_ACCEL_INELIGIBLE;
2908 c->cmd_type = CMD_IOACCEL2;
2909 /* Adjust the DMA address to point to the accelerated command buffer */
2910 c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
2911 (c->cmdindex * sizeof(*cp));
2912 BUG_ON(c->busaddr & 0x0000007F);
2913
2914 memset(cp, 0, sizeof(*cp));
2915 cp->IU_type = IOACCEL2_IU_TYPE;
2916
2917 use_sg = scsi_dma_map(cmd);
2918 if (use_sg < 0)
2919 return use_sg;
2920
2921 if (use_sg) {
2922 BUG_ON(use_sg > IOACCEL2_MAXSGENTRIES);
2923 curr_sg = cp->sg;
2924 scsi_for_each_sg(cmd, sg, use_sg, i) {
2925 addr64 = (u64) sg_dma_address(sg);
2926 len = sg_dma_len(sg);
2927 total_len += len;
2928 curr_sg->address = cpu_to_le64(addr64);
2929 curr_sg->length = cpu_to_le32(len);
2930 curr_sg->reserved[0] = 0;
2931 curr_sg->reserved[1] = 0;
2932 curr_sg->reserved[2] = 0;
2933 curr_sg->chain_indicator = 0;
2934 curr_sg++;
2935 }
2936
2937 switch (cmd->sc_data_direction) {
2938 case DMA_TO_DEVICE:
2939 cp->direction = IOACCEL2_DIR_DATA_OUT;
2940 break;
2941 case DMA_FROM_DEVICE:
2942 cp->direction = IOACCEL2_DIR_DATA_IN;
2943 break;
2944 case DMA_NONE:
2945 cp->direction = IOACCEL2_DIR_NO_DATA;
2946 break;
2947 default:
2948 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
2949 cmd->sc_data_direction);
2950 BUG();
2951 break;
2952 }
2953 } else {
2954 cp->direction = IOACCEL2_DIR_NO_DATA;
2955 }
2956 cp->scsi_nexus = ioaccel_handle;
2957 cp->Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT) |
2958 DIRECT_LOOKUP_BIT;
2959 memcpy(cp->cdb, cdb, sizeof(cp->cdb));
2960 memset(cp->cciss_lun, 0, sizeof(cp->cciss_lun));
2961 cp->cmd_priority_task_attr = 0;
2962
2963 /* fill in sg elements */
2964 cp->sg_count = (u8) use_sg;
2965
2966 cp->data_len = cpu_to_le32(total_len);
2967 cp->err_ptr = cpu_to_le64(c->busaddr +
2968 offsetof(struct io_accel2_cmd, error_data));
2969 cp->err_len = cpu_to_le32((u32) sizeof(cp->error_data));
2970
2971 enqueue_cmd_and_start_io(h, c);
2972 return 0;
2973}
2974
2975/*
2976 * Queue a command to the correct I/O accelerator path.
2977 */
2978static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
2979 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
2980 u8 *scsi3addr)
2981{
2982 if (h->transMethod & CFGTBL_Trans_io_accel1)
2983 return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
2984 cdb, cdb_len, scsi3addr);
2985 else
2986 return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
2987 cdb, cdb_len, scsi3addr);
2988}
2989
Scott Teel6b80b182014-02-18 13:56:55 -06002990static void raid_map_helper(struct raid_map_data *map,
2991 int offload_to_mirror, u32 *map_index, u32 *current_group)
2992{
2993 if (offload_to_mirror == 0) {
2994 /* use physical disk in the first mirrored group. */
2995 *map_index %= map->data_disks_per_row;
2996 return;
2997 }
2998 do {
2999 /* determine mirror group that *map_index indicates */
3000 *current_group = *map_index / map->data_disks_per_row;
3001 if (offload_to_mirror == *current_group)
3002 continue;
3003 if (*current_group < (map->layout_map_count - 1)) {
3004 /* select map index from next group */
3005 *map_index += map->data_disks_per_row;
3006 (*current_group)++;
3007 } else {
3008 /* select map index from first group */
3009 *map_index %= map->data_disks_per_row;
3010 *current_group = 0;
3011 }
3012 } while (offload_to_mirror != *current_group);
3013}
3014
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003015/*
3016 * Attempt to perform offload RAID mapping for a logical volume I/O.
3017 */
3018static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
3019 struct CommandList *c)
3020{
3021 struct scsi_cmnd *cmd = c->scsi_cmd;
3022 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3023 struct raid_map_data *map = &dev->raid_map;
3024 struct raid_map_disk_data *dd = &map->data[0];
3025 int is_write = 0;
3026 u32 map_index;
3027 u64 first_block, last_block;
3028 u32 block_cnt;
3029 u32 blocks_per_row;
3030 u64 first_row, last_row;
3031 u32 first_row_offset, last_row_offset;
3032 u32 first_column, last_column;
Scott Teel6b80b182014-02-18 13:56:55 -06003033 u64 r0_first_row, r0_last_row;
3034 u32 r5or6_blocks_per_row;
3035 u64 r5or6_first_row, r5or6_last_row;
3036 u32 r5or6_first_row_offset, r5or6_last_row_offset;
3037 u32 r5or6_first_column, r5or6_last_column;
3038 u32 total_disks_per_row;
3039 u32 stripesize;
3040 u32 first_group, last_group, current_group;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003041 u32 map_row;
3042 u32 disk_handle;
3043 u64 disk_block;
3044 u32 disk_block_cnt;
3045 u8 cdb[16];
3046 u8 cdb_len;
3047#if BITS_PER_LONG == 32
3048 u64 tmpdiv;
3049#endif
Scott Teel6b80b182014-02-18 13:56:55 -06003050 int offload_to_mirror;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003051
3052 BUG_ON(!(dev->offload_config && dev->offload_enabled));
3053
3054 /* check for valid opcode, get LBA and block count */
3055 switch (cmd->cmnd[0]) {
3056 case WRITE_6:
3057 is_write = 1;
3058 case READ_6:
3059 first_block =
3060 (((u64) cmd->cmnd[2]) << 8) |
3061 cmd->cmnd[3];
3062 block_cnt = cmd->cmnd[4];
3063 break;
3064 case WRITE_10:
3065 is_write = 1;
3066 case READ_10:
3067 first_block =
3068 (((u64) cmd->cmnd[2]) << 24) |
3069 (((u64) cmd->cmnd[3]) << 16) |
3070 (((u64) cmd->cmnd[4]) << 8) |
3071 cmd->cmnd[5];
3072 block_cnt =
3073 (((u32) cmd->cmnd[7]) << 8) |
3074 cmd->cmnd[8];
3075 break;
3076 case WRITE_12:
3077 is_write = 1;
3078 case READ_12:
3079 first_block =
3080 (((u64) cmd->cmnd[2]) << 24) |
3081 (((u64) cmd->cmnd[3]) << 16) |
3082 (((u64) cmd->cmnd[4]) << 8) |
3083 cmd->cmnd[5];
3084 block_cnt =
3085 (((u32) cmd->cmnd[6]) << 24) |
3086 (((u32) cmd->cmnd[7]) << 16) |
3087 (((u32) cmd->cmnd[8]) << 8) |
3088 cmd->cmnd[9];
3089 break;
3090 case WRITE_16:
3091 is_write = 1;
3092 case READ_16:
3093 first_block =
3094 (((u64) cmd->cmnd[2]) << 56) |
3095 (((u64) cmd->cmnd[3]) << 48) |
3096 (((u64) cmd->cmnd[4]) << 40) |
3097 (((u64) cmd->cmnd[5]) << 32) |
3098 (((u64) cmd->cmnd[6]) << 24) |
3099 (((u64) cmd->cmnd[7]) << 16) |
3100 (((u64) cmd->cmnd[8]) << 8) |
3101 cmd->cmnd[9];
3102 block_cnt =
3103 (((u32) cmd->cmnd[10]) << 24) |
3104 (((u32) cmd->cmnd[11]) << 16) |
3105 (((u32) cmd->cmnd[12]) << 8) |
3106 cmd->cmnd[13];
3107 break;
3108 default:
3109 return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
3110 }
3111 BUG_ON(block_cnt == 0);
3112 last_block = first_block + block_cnt - 1;
3113
3114 /* check for write to non-RAID-0 */
3115 if (is_write && dev->raid_level != 0)
3116 return IO_ACCEL_INELIGIBLE;
3117
3118 /* check for invalid block or wraparound */
3119 if (last_block >= map->volume_blk_cnt || last_block < first_block)
3120 return IO_ACCEL_INELIGIBLE;
3121
3122 /* calculate stripe information for the request */
3123 blocks_per_row = map->data_disks_per_row * map->strip_size;
3124#if BITS_PER_LONG == 32
3125 tmpdiv = first_block;
3126 (void) do_div(tmpdiv, blocks_per_row);
3127 first_row = tmpdiv;
3128 tmpdiv = last_block;
3129 (void) do_div(tmpdiv, blocks_per_row);
3130 last_row = tmpdiv;
3131 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
3132 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
3133 tmpdiv = first_row_offset;
3134 (void) do_div(tmpdiv, map->strip_size);
3135 first_column = tmpdiv;
3136 tmpdiv = last_row_offset;
3137 (void) do_div(tmpdiv, map->strip_size);
3138 last_column = tmpdiv;
3139#else
3140 first_row = first_block / blocks_per_row;
3141 last_row = last_block / blocks_per_row;
3142 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
3143 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
3144 first_column = first_row_offset / map->strip_size;
3145 last_column = last_row_offset / map->strip_size;
3146#endif
3147
3148 /* if this isn't a single row/column then give to the controller */
3149 if ((first_row != last_row) || (first_column != last_column))
3150 return IO_ACCEL_INELIGIBLE;
3151
3152 /* proceeding with driver mapping */
Scott Teel6b80b182014-02-18 13:56:55 -06003153 total_disks_per_row = map->data_disks_per_row +
3154 map->metadata_disks_per_row;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003155 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
3156 map->row_cnt;
Scott Teel6b80b182014-02-18 13:56:55 -06003157 map_index = (map_row * total_disks_per_row) + first_column;
3158
3159 switch (dev->raid_level) {
3160 case HPSA_RAID_0:
3161 break; /* nothing special to do */
3162 case HPSA_RAID_1:
3163 /* Handles load balance across RAID 1 members.
3164 * (2-drive R1 and R10 with even # of drives.)
3165 * Appropriate for SSDs, not optimal for HDDs
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003166 */
Scott Teel6b80b182014-02-18 13:56:55 -06003167 BUG_ON(map->layout_map_count != 2);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003168 if (dev->offload_to_mirror)
3169 map_index += map->data_disks_per_row;
3170 dev->offload_to_mirror = !dev->offload_to_mirror;
Scott Teel6b80b182014-02-18 13:56:55 -06003171 break;
3172 case HPSA_RAID_ADM:
3173 /* Handles N-way mirrors (R1-ADM)
3174 * and R10 with # of drives divisible by 3.)
3175 */
3176 BUG_ON(map->layout_map_count != 3);
3177
3178 offload_to_mirror = dev->offload_to_mirror;
3179 raid_map_helper(map, offload_to_mirror,
3180 &map_index, &current_group);
3181 /* set mirror group to use next time */
3182 offload_to_mirror =
3183 (offload_to_mirror >= map->layout_map_count - 1)
3184 ? 0 : offload_to_mirror + 1;
3185 /* FIXME: remove after debug/dev */
3186 BUG_ON(offload_to_mirror >= map->layout_map_count);
3187 dev_warn(&h->pdev->dev,
3188 "DEBUG: Using physical disk map index %d from mirror group %d\n",
3189 map_index, offload_to_mirror);
3190 dev->offload_to_mirror = offload_to_mirror;
3191 /* Avoid direct use of dev->offload_to_mirror within this
3192 * function since multiple threads might simultaneously
3193 * increment it beyond the range of dev->layout_map_count -1.
3194 */
3195 break;
3196 case HPSA_RAID_5:
3197 case HPSA_RAID_6:
3198 if (map->layout_map_count <= 1)
3199 break;
3200
3201 /* Verify first and last block are in same RAID group */
3202 r5or6_blocks_per_row =
3203 map->strip_size * map->data_disks_per_row;
3204 BUG_ON(r5or6_blocks_per_row == 0);
3205 stripesize = r5or6_blocks_per_row * map->layout_map_count;
3206#if BITS_PER_LONG == 32
3207 tmpdiv = first_block;
3208 first_group = do_div(tmpdiv, stripesize);
3209 tmpdiv = first_group;
3210 (void) do_div(tmpdiv, r5or6_blocks_per_row);
3211 first_group = tmpdiv;
3212 tmpdiv = last_block;
3213 last_group = do_div(tmpdiv, stripesize);
3214 tmpdiv = last_group;
3215 (void) do_div(tmpdiv, r5or6_blocks_per_row);
3216 last_group = tmpdiv;
3217#else
3218 first_group = (first_block % stripesize) / r5or6_blocks_per_row;
3219 last_group = (last_block % stripesize) / r5or6_blocks_per_row;
3220 if (first_group != last_group)
3221#endif
3222 return IO_ACCEL_INELIGIBLE;
3223
3224 /* Verify request is in a single row of RAID 5/6 */
3225#if BITS_PER_LONG == 32
3226 tmpdiv = first_block;
3227 (void) do_div(tmpdiv, stripesize);
3228 first_row = r5or6_first_row = r0_first_row = tmpdiv;
3229 tmpdiv = last_block;
3230 (void) do_div(tmpdiv, stripesize);
3231 r5or6_last_row = r0_last_row = tmpdiv;
3232#else
3233 first_row = r5or6_first_row = r0_first_row =
3234 first_block / stripesize;
3235 r5or6_last_row = r0_last_row = last_block / stripesize;
3236#endif
3237 if (r5or6_first_row != r5or6_last_row)
3238 return IO_ACCEL_INELIGIBLE;
3239
3240
3241 /* Verify request is in a single column */
3242#if BITS_PER_LONG == 32
3243 tmpdiv = first_block;
3244 first_row_offset = do_div(tmpdiv, stripesize);
3245 tmpdiv = first_row_offset;
3246 first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
3247 r5or6_first_row_offset = first_row_offset;
3248 tmpdiv = last_block;
3249 r5or6_last_row_offset = do_div(tmpdiv, stripesize);
3250 tmpdiv = r5or6_last_row_offset;
3251 r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
3252 tmpdiv = r5or6_first_row_offset;
3253 (void) do_div(tmpdiv, map->strip_size);
3254 first_column = r5or6_first_column = tmpdiv;
3255 tmpdiv = r5or6_last_row_offset;
3256 (void) do_div(tmpdiv, map->strip_size);
3257 r5or6_last_column = tmpdiv;
3258#else
3259 first_row_offset = r5or6_first_row_offset =
3260 (u32)((first_block % stripesize) %
3261 r5or6_blocks_per_row);
3262
3263 r5or6_last_row_offset =
3264 (u32)((last_block % stripesize) %
3265 r5or6_blocks_per_row);
3266
3267 first_column = r5or6_first_column =
3268 r5or6_first_row_offset / map->strip_size;
3269 r5or6_last_column =
3270 r5or6_last_row_offset / map->strip_size;
3271#endif
3272 if (r5or6_first_column != r5or6_last_column)
3273 return IO_ACCEL_INELIGIBLE;
3274
3275 /* Request is eligible */
3276 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
3277 map->row_cnt;
3278
3279 map_index = (first_group *
3280 (map->row_cnt * total_disks_per_row)) +
3281 (map_row * total_disks_per_row) + first_column;
3282 break;
3283 default:
3284 return IO_ACCEL_INELIGIBLE;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003285 }
Scott Teel6b80b182014-02-18 13:56:55 -06003286
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003287 disk_handle = dd[map_index].ioaccel_handle;
3288 disk_block = map->disk_starting_blk + (first_row * map->strip_size) +
3289 (first_row_offset - (first_column * map->strip_size));
3290 disk_block_cnt = block_cnt;
3291
3292 /* handle differing logical/physical block sizes */
3293 if (map->phys_blk_shift) {
3294 disk_block <<= map->phys_blk_shift;
3295 disk_block_cnt <<= map->phys_blk_shift;
3296 }
3297 BUG_ON(disk_block_cnt > 0xffff);
3298
3299 /* build the new CDB for the physical disk I/O */
3300 if (disk_block > 0xffffffff) {
3301 cdb[0] = is_write ? WRITE_16 : READ_16;
3302 cdb[1] = 0;
3303 cdb[2] = (u8) (disk_block >> 56);
3304 cdb[3] = (u8) (disk_block >> 48);
3305 cdb[4] = (u8) (disk_block >> 40);
3306 cdb[5] = (u8) (disk_block >> 32);
3307 cdb[6] = (u8) (disk_block >> 24);
3308 cdb[7] = (u8) (disk_block >> 16);
3309 cdb[8] = (u8) (disk_block >> 8);
3310 cdb[9] = (u8) (disk_block);
3311 cdb[10] = (u8) (disk_block_cnt >> 24);
3312 cdb[11] = (u8) (disk_block_cnt >> 16);
3313 cdb[12] = (u8) (disk_block_cnt >> 8);
3314 cdb[13] = (u8) (disk_block_cnt);
3315 cdb[14] = 0;
3316 cdb[15] = 0;
3317 cdb_len = 16;
3318 } else {
3319 cdb[0] = is_write ? WRITE_10 : READ_10;
3320 cdb[1] = 0;
3321 cdb[2] = (u8) (disk_block >> 24);
3322 cdb[3] = (u8) (disk_block >> 16);
3323 cdb[4] = (u8) (disk_block >> 8);
3324 cdb[5] = (u8) (disk_block);
3325 cdb[6] = 0;
3326 cdb[7] = (u8) (disk_block_cnt >> 8);
3327 cdb[8] = (u8) (disk_block_cnt);
3328 cdb[9] = 0;
3329 cdb_len = 10;
3330 }
3331 return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
3332 dev->scsi3addr);
3333}
3334
Jeff Garzikf2812332010-11-16 02:10:29 -05003335static int hpsa_scsi_queue_command_lck(struct scsi_cmnd *cmd,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003336 void (*done)(struct scsi_cmnd *))
3337{
3338 struct ctlr_info *h;
3339 struct hpsa_scsi_dev_t *dev;
3340 unsigned char scsi3addr[8];
3341 struct CommandList *c;
3342 unsigned long flags;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003343 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003344
3345 /* Get the ptr to our adapter structure out of cmd->host. */
3346 h = sdev_to_hba(cmd->device);
3347 dev = cmd->device->hostdata;
3348 if (!dev) {
3349 cmd->result = DID_NO_CONNECT << 16;
3350 done(cmd);
3351 return 0;
3352 }
3353 memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
3354
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003355 spin_lock_irqsave(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05003356 if (unlikely(h->lockup_detected)) {
3357 spin_unlock_irqrestore(&h->lock, flags);
3358 cmd->result = DID_ERROR << 16;
3359 done(cmd);
3360 return 0;
3361 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003362 spin_unlock_irqrestore(&h->lock, flags);
Matt Gatese16a33a2012-05-01 11:43:11 -05003363 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003364 if (c == NULL) { /* trouble... */
3365 dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
3366 return SCSI_MLQUEUE_HOST_BUSY;
3367 }
3368
3369 /* Fill in the command list header */
3370
3371 cmd->scsi_done = done; /* save this for use by completion code */
3372
3373 /* save c in case we have to abort it */
3374 cmd->host_scribble = (unsigned char *) c;
3375
3376 c->cmd_type = CMD_SCSI;
3377 c->scsi_cmd = cmd;
Matt Gatese1f7de02014-02-18 13:55:17 -06003378
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003379 /* Call alternate submit routine for I/O accelerated commands.
3380 * Retries always go down the normal I/O path.
3381 */
3382 if (likely(cmd->retries == 0 &&
Scott Teelda0697b2014-02-18 13:57:00 -06003383 cmd->request->cmd_type == REQ_TYPE_FS &&
3384 h->acciopath_status)) {
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003385 if (dev->offload_enabled) {
3386 rc = hpsa_scsi_ioaccel_raid_map(h, c);
3387 if (rc == 0)
3388 return 0; /* Sent on ioaccel path */
3389 if (rc < 0) { /* scsi_dma_map failed. */
3390 cmd_free(h, c);
3391 return SCSI_MLQUEUE_HOST_BUSY;
3392 }
3393 } else if (dev->ioaccel_handle) {
3394 rc = hpsa_scsi_ioaccel_direct_map(h, c);
3395 if (rc == 0)
3396 return 0; /* Sent on direct map path */
3397 if (rc < 0) { /* scsi_dma_map failed. */
3398 cmd_free(h, c);
3399 return SCSI_MLQUEUE_HOST_BUSY;
3400 }
3401 }
3402 }
Matt Gatese1f7de02014-02-18 13:55:17 -06003403
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003404 c->Header.ReplyQueue = 0; /* unused in simple mode */
3405 memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
Don Brace303932f2010-02-04 08:42:40 -06003406 c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT);
3407 c->Header.Tag.lower |= DIRECT_LOOKUP_BIT;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003408
3409 /* Fill in the request block... */
3410
3411 c->Request.Timeout = 0;
3412 memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
3413 BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
3414 c->Request.CDBLen = cmd->cmd_len;
3415 memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
3416 c->Request.Type.Type = TYPE_CMD;
3417 c->Request.Type.Attribute = ATTR_SIMPLE;
3418 switch (cmd->sc_data_direction) {
3419 case DMA_TO_DEVICE:
3420 c->Request.Type.Direction = XFER_WRITE;
3421 break;
3422 case DMA_FROM_DEVICE:
3423 c->Request.Type.Direction = XFER_READ;
3424 break;
3425 case DMA_NONE:
3426 c->Request.Type.Direction = XFER_NONE;
3427 break;
3428 case DMA_BIDIRECTIONAL:
3429 /* This can happen if a buggy application does a scsi passthru
3430 * and sets both inlen and outlen to non-zero. ( see
3431 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
3432 */
3433
3434 c->Request.Type.Direction = XFER_RSVD;
3435 /* This is technically wrong, and hpsa controllers should
3436 * reject it with CMD_INVALID, which is the most correct
3437 * response, but non-fibre backends appear to let it
3438 * slide by, and give the same results as if this field
3439 * were set correctly. Either way is acceptable for
3440 * our purposes here.
3441 */
3442
3443 break;
3444
3445 default:
3446 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3447 cmd->sc_data_direction);
3448 BUG();
3449 break;
3450 }
3451
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06003452 if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003453 cmd_free(h, c);
3454 return SCSI_MLQUEUE_HOST_BUSY;
3455 }
3456 enqueue_cmd_and_start_io(h, c);
3457 /* the cmd'll come back via intr handler in complete_scsi_command() */
3458 return 0;
3459}
3460
Jeff Garzikf2812332010-11-16 02:10:29 -05003461static DEF_SCSI_QCMD(hpsa_scsi_queue_command)
3462
Stephen M. Cameron5f389362014-02-18 13:55:48 -06003463static int do_not_scan_if_controller_locked_up(struct ctlr_info *h)
3464{
3465 unsigned long flags;
3466
3467 /*
3468 * Don't let rescans be initiated on a controller known
3469 * to be locked up. If the controller locks up *during*
3470 * a rescan, that thread is probably hosed, but at least
3471 * we can prevent new rescan threads from piling up on a
3472 * locked up controller.
3473 */
3474 spin_lock_irqsave(&h->lock, flags);
3475 if (unlikely(h->lockup_detected)) {
3476 spin_unlock_irqrestore(&h->lock, flags);
3477 spin_lock_irqsave(&h->scan_lock, flags);
3478 h->scan_finished = 1;
3479 wake_up_all(&h->scan_wait_queue);
3480 spin_unlock_irqrestore(&h->scan_lock, flags);
3481 return 1;
3482 }
3483 spin_unlock_irqrestore(&h->lock, flags);
3484 return 0;
3485}
3486
Stephen M. Camerona08a84712010-02-04 08:43:16 -06003487static void hpsa_scan_start(struct Scsi_Host *sh)
3488{
3489 struct ctlr_info *h = shost_to_hba(sh);
3490 unsigned long flags;
3491
Stephen M. Cameron5f389362014-02-18 13:55:48 -06003492 if (do_not_scan_if_controller_locked_up(h))
3493 return;
3494
Stephen M. Camerona08a84712010-02-04 08:43:16 -06003495 /* wait until any scan already in progress is finished. */
3496 while (1) {
3497 spin_lock_irqsave(&h->scan_lock, flags);
3498 if (h->scan_finished)
3499 break;
3500 spin_unlock_irqrestore(&h->scan_lock, flags);
3501 wait_event(h->scan_wait_queue, h->scan_finished);
3502 /* Note: We don't need to worry about a race between this
3503 * thread and driver unload because the midlayer will
3504 * have incremented the reference count, so unload won't
3505 * happen if we're in here.
3506 */
3507 }
3508 h->scan_finished = 0; /* mark scan as in progress */
3509 spin_unlock_irqrestore(&h->scan_lock, flags);
3510
Stephen M. Cameron5f389362014-02-18 13:55:48 -06003511 if (do_not_scan_if_controller_locked_up(h))
3512 return;
3513
Stephen M. Camerona08a84712010-02-04 08:43:16 -06003514 hpsa_update_scsi_devices(h, h->scsi_host->host_no);
3515
3516 spin_lock_irqsave(&h->scan_lock, flags);
3517 h->scan_finished = 1; /* mark scan as finished. */
3518 wake_up_all(&h->scan_wait_queue);
3519 spin_unlock_irqrestore(&h->scan_lock, flags);
3520}
3521
3522static int hpsa_scan_finished(struct Scsi_Host *sh,
3523 unsigned long elapsed_time)
3524{
3525 struct ctlr_info *h = shost_to_hba(sh);
3526 unsigned long flags;
3527 int finished;
3528
3529 spin_lock_irqsave(&h->scan_lock, flags);
3530 finished = h->scan_finished;
3531 spin_unlock_irqrestore(&h->scan_lock, flags);
3532 return finished;
3533}
3534
Stephen M. Cameron667e23d2010-02-25 14:02:51 -06003535static int hpsa_change_queue_depth(struct scsi_device *sdev,
3536 int qdepth, int reason)
3537{
3538 struct ctlr_info *h = sdev_to_hba(sdev);
3539
3540 if (reason != SCSI_QDEPTH_DEFAULT)
3541 return -ENOTSUPP;
3542
3543 if (qdepth < 1)
3544 qdepth = 1;
3545 else
3546 if (qdepth > h->nr_cmds)
3547 qdepth = h->nr_cmds;
3548 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
3549 return sdev->queue_depth;
3550}
3551
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003552static void hpsa_unregister_scsi(struct ctlr_info *h)
3553{
3554 /* we are being forcibly unloaded, and may not refuse. */
3555 scsi_remove_host(h->scsi_host);
3556 scsi_host_put(h->scsi_host);
3557 h->scsi_host = NULL;
3558}
3559
3560static int hpsa_register_scsi(struct ctlr_info *h)
3561{
Stephen M. Cameronb7056902012-01-19 14:00:53 -06003562 struct Scsi_Host *sh;
3563 int error;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003564
Stephen M. Cameronb7056902012-01-19 14:00:53 -06003565 sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
3566 if (sh == NULL)
3567 goto fail;
3568
3569 sh->io_port = 0;
3570 sh->n_io_port = 0;
3571 sh->this_id = -1;
3572 sh->max_channel = 3;
3573 sh->max_cmd_len = MAX_COMMAND_SIZE;
3574 sh->max_lun = HPSA_MAX_LUN;
3575 sh->max_id = HPSA_MAX_LUN;
3576 sh->can_queue = h->nr_cmds;
3577 sh->cmd_per_lun = h->nr_cmds;
3578 sh->sg_tablesize = h->maxsgentries;
3579 h->scsi_host = sh;
3580 sh->hostdata[0] = (unsigned long) h;
3581 sh->irq = h->intr[h->intr_mode];
3582 sh->unique_id = sh->irq;
3583 error = scsi_add_host(sh, &h->pdev->dev);
3584 if (error)
3585 goto fail_host_put;
3586 scsi_scan_host(sh);
3587 return 0;
3588
3589 fail_host_put:
3590 dev_err(&h->pdev->dev, "%s: scsi_add_host"
3591 " failed for controller %d\n", __func__, h->ctlr);
3592 scsi_host_put(sh);
3593 return error;
3594 fail:
3595 dev_err(&h->pdev->dev, "%s: scsi_host_alloc"
3596 " failed for controller %d\n", __func__, h->ctlr);
3597 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003598}
3599
3600static int wait_for_device_to_become_ready(struct ctlr_info *h,
3601 unsigned char lunaddr[])
3602{
3603 int rc = 0;
3604 int count = 0;
3605 int waittime = 1; /* seconds */
3606 struct CommandList *c;
3607
3608 c = cmd_special_alloc(h);
3609 if (!c) {
3610 dev_warn(&h->pdev->dev, "out of memory in "
3611 "wait_for_device_to_become_ready.\n");
3612 return IO_ERROR;
3613 }
3614
3615 /* Send test unit ready until device ready, or give up. */
3616 while (count < HPSA_TUR_RETRY_LIMIT) {
3617
3618 /* Wait for a bit. do this first, because if we send
3619 * the TUR right away, the reset will just abort it.
3620 */
3621 msleep(1000 * waittime);
3622 count++;
3623
3624 /* Increase wait time with each try, up to a point. */
3625 if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
3626 waittime = waittime * 2;
3627
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003628 /* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
3629 (void) fill_cmd(c, TEST_UNIT_READY, h,
3630 NULL, 0, 0, lunaddr, TYPE_CMD);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003631 hpsa_scsi_do_simple_cmd_core(h, c);
3632 /* no unmap needed here because no data xfer. */
3633
3634 if (c->err_info->CommandStatus == CMD_SUCCESS)
3635 break;
3636
3637 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
3638 c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
3639 (c->err_info->SenseInfo[2] == NO_SENSE ||
3640 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
3641 break;
3642
3643 dev_warn(&h->pdev->dev, "waiting %d secs "
3644 "for device to become ready.\n", waittime);
3645 rc = 1; /* device not ready. */
3646 }
3647
3648 if (rc)
3649 dev_warn(&h->pdev->dev, "giving up on device.\n");
3650 else
3651 dev_warn(&h->pdev->dev, "device is ready.\n");
3652
3653 cmd_special_free(h, c);
3654 return rc;
3655}
3656
3657/* Need at least one of these error handlers to keep ../scsi/hosts.c from
3658 * complaining. Doing a host- or bus-reset can't do anything good here.
3659 */
3660static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
3661{
3662 int rc;
3663 struct ctlr_info *h;
3664 struct hpsa_scsi_dev_t *dev;
3665
3666 /* find the controller to which the command to be aborted was sent */
3667 h = sdev_to_hba(scsicmd->device);
3668 if (h == NULL) /* paranoia */
3669 return FAILED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003670 dev = scsicmd->device->hostdata;
3671 if (!dev) {
3672 dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
3673 "device lookup failed.\n");
3674 return FAILED;
3675 }
Stephen M. Camerond416b0c2010-02-04 08:43:21 -06003676 dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
3677 h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003678 /* send a reset to the SCSI LUN which the command was sent to */
Scott Teelbf711ac2014-02-18 13:56:39 -06003679 rc = hpsa_send_reset(h, dev->scsi3addr, HPSA_RESET_TYPE_LUN);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003680 if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
3681 return SUCCESS;
3682
3683 dev_warn(&h->pdev->dev, "resetting device failed.\n");
3684 return FAILED;
3685}
3686
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003687static void swizzle_abort_tag(u8 *tag)
3688{
3689 u8 original_tag[8];
3690
3691 memcpy(original_tag, tag, 8);
3692 tag[0] = original_tag[3];
3693 tag[1] = original_tag[2];
3694 tag[2] = original_tag[1];
3695 tag[3] = original_tag[0];
3696 tag[4] = original_tag[7];
3697 tag[5] = original_tag[6];
3698 tag[6] = original_tag[5];
3699 tag[7] = original_tag[4];
3700}
3701
Scott Teel17eb87d2014-02-18 13:55:28 -06003702static void hpsa_get_tag(struct ctlr_info *h,
3703 struct CommandList *c, u32 *taglower, u32 *tagupper)
3704{
3705 if (c->cmd_type == CMD_IOACCEL1) {
3706 struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
3707 &h->ioaccel_cmd_pool[c->cmdindex];
3708 *tagupper = cm1->Tag.upper;
3709 *taglower = cm1->Tag.lower;
Scott Teel54b6e9e2014-02-18 13:56:45 -06003710 return;
Scott Teel17eb87d2014-02-18 13:55:28 -06003711 }
Scott Teel54b6e9e2014-02-18 13:56:45 -06003712 if (c->cmd_type == CMD_IOACCEL2) {
3713 struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
3714 &h->ioaccel2_cmd_pool[c->cmdindex];
3715 *tagupper = cm2->Tag.upper;
3716 *taglower = cm2->Tag.lower;
3717 return;
3718 }
3719 *tagupper = c->Header.Tag.upper;
3720 *taglower = c->Header.Tag.lower;
Scott Teel17eb87d2014-02-18 13:55:28 -06003721}
3722
Scott Teel54b6e9e2014-02-18 13:56:45 -06003723
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003724static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003725 struct CommandList *abort, int swizzle)
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003726{
3727 int rc = IO_OK;
3728 struct CommandList *c;
3729 struct ErrorInfo *ei;
Scott Teel17eb87d2014-02-18 13:55:28 -06003730 u32 tagupper, taglower;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003731
3732 c = cmd_special_alloc(h);
3733 if (c == NULL) { /* trouble... */
3734 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
3735 return -ENOMEM;
3736 }
3737
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003738 /* fill_cmd can't fail here, no buffer to map */
3739 (void) fill_cmd(c, HPSA_ABORT_MSG, h, abort,
3740 0, 0, scsi3addr, TYPE_MSG);
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003741 if (swizzle)
3742 swizzle_abort_tag(&c->Request.CDB[4]);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003743 hpsa_scsi_do_simple_cmd_core(h, c);
Scott Teel17eb87d2014-02-18 13:55:28 -06003744 hpsa_get_tag(h, abort, &taglower, &tagupper);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003745 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd_core completed.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06003746 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003747 /* no unmap needed here because no data xfer. */
3748
3749 ei = c->err_info;
3750 switch (ei->CommandStatus) {
3751 case CMD_SUCCESS:
3752 break;
3753 case CMD_UNABORTABLE: /* Very common, don't make noise. */
3754 rc = -1;
3755 break;
3756 default:
3757 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06003758 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003759 hpsa_scsi_interpret_error(c);
3760 rc = -1;
3761 break;
3762 }
3763 cmd_special_free(h, c);
3764 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n", __func__,
3765 abort->Header.Tag.upper, abort->Header.Tag.lower);
3766 return rc;
3767}
3768
3769/*
3770 * hpsa_find_cmd_in_queue
3771 *
3772 * Used to determine whether a command (find) is still present
3773 * in queue_head. Optionally excludes the last element of queue_head.
3774 *
3775 * This is used to avoid unnecessary aborts. Commands in h->reqQ have
3776 * not yet been submitted, and so can be aborted by the driver without
3777 * sending an abort to the hardware.
3778 *
3779 * Returns pointer to command if found in queue, NULL otherwise.
3780 */
3781static struct CommandList *hpsa_find_cmd_in_queue(struct ctlr_info *h,
3782 struct scsi_cmnd *find, struct list_head *queue_head)
3783{
3784 unsigned long flags;
3785 struct CommandList *c = NULL; /* ptr into cmpQ */
3786
3787 if (!find)
3788 return 0;
3789 spin_lock_irqsave(&h->lock, flags);
3790 list_for_each_entry(c, queue_head, list) {
3791 if (c->scsi_cmd == NULL) /* e.g.: passthru ioctl */
3792 continue;
3793 if (c->scsi_cmd == find) {
3794 spin_unlock_irqrestore(&h->lock, flags);
3795 return c;
3796 }
3797 }
3798 spin_unlock_irqrestore(&h->lock, flags);
3799 return NULL;
3800}
3801
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003802static struct CommandList *hpsa_find_cmd_in_queue_by_tag(struct ctlr_info *h,
3803 u8 *tag, struct list_head *queue_head)
3804{
3805 unsigned long flags;
3806 struct CommandList *c;
3807
3808 spin_lock_irqsave(&h->lock, flags);
3809 list_for_each_entry(c, queue_head, list) {
3810 if (memcmp(&c->Header.Tag, tag, 8) != 0)
3811 continue;
3812 spin_unlock_irqrestore(&h->lock, flags);
3813 return c;
3814 }
3815 spin_unlock_irqrestore(&h->lock, flags);
3816 return NULL;
3817}
3818
Scott Teel54b6e9e2014-02-18 13:56:45 -06003819/* ioaccel2 path firmware cannot handle abort task requests.
3820 * Change abort requests to physical target reset, and send to the
3821 * address of the physical disk used for the ioaccel 2 command.
3822 * Return 0 on success (IO_OK)
3823 * -1 on failure
3824 */
3825
3826static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
3827 unsigned char *scsi3addr, struct CommandList *abort)
3828{
3829 int rc = IO_OK;
3830 struct scsi_cmnd *scmd; /* scsi command within request being aborted */
3831 struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
3832 unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
3833 unsigned char *psa = &phys_scsi3addr[0];
3834
3835 /* Get a pointer to the hpsa logical device. */
3836 scmd = (struct scsi_cmnd *) abort->scsi_cmd;
3837 dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
3838 if (dev == NULL) {
3839 dev_warn(&h->pdev->dev,
3840 "Cannot abort: no device pointer for command.\n");
3841 return -1; /* not abortable */
3842 }
3843
3844 if (!dev->offload_enabled) {
3845 dev_warn(&h->pdev->dev,
3846 "Can't abort: device is not operating in HP SSD Smart Path mode.\n");
3847 return -1; /* not abortable */
3848 }
3849
3850 /* Incoming scsi3addr is logical addr. We need physical disk addr. */
3851 if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
3852 dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
3853 return -1; /* not abortable */
3854 }
3855
3856 /* send the reset */
3857 rc = hpsa_send_reset(h, psa, HPSA_RESET_TYPE_TARGET);
3858 if (rc != 0) {
3859 dev_warn(&h->pdev->dev,
3860 "Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
3861 psa[0], psa[1], psa[2], psa[3],
3862 psa[4], psa[5], psa[6], psa[7]);
3863 return rc; /* failed to reset */
3864 }
3865
3866 /* wait for device to recover */
3867 if (wait_for_device_to_become_ready(h, psa) != 0) {
3868 dev_warn(&h->pdev->dev,
3869 "Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
3870 psa[0], psa[1], psa[2], psa[3],
3871 psa[4], psa[5], psa[6], psa[7]);
3872 return -1; /* failed to recover */
3873 }
3874
3875 /* device recovered */
3876 dev_info(&h->pdev->dev,
3877 "Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
3878 psa[0], psa[1], psa[2], psa[3],
3879 psa[4], psa[5], psa[6], psa[7]);
3880
3881 return rc; /* success */
3882}
3883
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003884/* Some Smart Arrays need the abort tag swizzled, and some don't. It's hard to
3885 * tell which kind we're dealing with, so we send the abort both ways. There
3886 * shouldn't be any collisions between swizzled and unswizzled tags due to the
3887 * way we construct our tags but we check anyway in case the assumptions which
3888 * make this true someday become false.
3889 */
3890static int hpsa_send_abort_both_ways(struct ctlr_info *h,
3891 unsigned char *scsi3addr, struct CommandList *abort)
3892{
3893 u8 swizzled_tag[8];
3894 struct CommandList *c;
3895 int rc = 0, rc2 = 0;
3896
Scott Teel54b6e9e2014-02-18 13:56:45 -06003897 /* ioccelerator mode 2 commands should be aborted via the
3898 * accelerated path, since RAID path is unaware of these commands,
3899 * but underlying firmware can't handle abort TMF.
3900 * Change abort to physical device reset.
3901 */
3902 if (abort->cmd_type == CMD_IOACCEL2)
3903 return hpsa_send_reset_as_abort_ioaccel2(h, scsi3addr, abort);
3904
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003905 /* we do not expect to find the swizzled tag in our queue, but
3906 * check anyway just to be sure the assumptions which make this
3907 * the case haven't become wrong.
3908 */
3909 memcpy(swizzled_tag, &abort->Request.CDB[4], 8);
3910 swizzle_abort_tag(swizzled_tag);
3911 c = hpsa_find_cmd_in_queue_by_tag(h, swizzled_tag, &h->cmpQ);
3912 if (c != NULL) {
3913 dev_warn(&h->pdev->dev, "Unexpectedly found byte-swapped tag in completion queue.\n");
3914 return hpsa_send_abort(h, scsi3addr, abort, 0);
3915 }
3916 rc = hpsa_send_abort(h, scsi3addr, abort, 0);
3917
3918 /* if the command is still in our queue, we can't conclude that it was
3919 * aborted (it might have just completed normally) but in any case
3920 * we don't need to try to abort it another way.
3921 */
3922 c = hpsa_find_cmd_in_queue(h, abort->scsi_cmd, &h->cmpQ);
3923 if (c)
3924 rc2 = hpsa_send_abort(h, scsi3addr, abort, 1);
3925 return rc && rc2;
3926}
3927
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003928/* Send an abort for the specified command.
3929 * If the device and controller support it,
3930 * send a task abort request.
3931 */
3932static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
3933{
3934
3935 int i, rc;
3936 struct ctlr_info *h;
3937 struct hpsa_scsi_dev_t *dev;
3938 struct CommandList *abort; /* pointer to command to be aborted */
3939 struct CommandList *found;
3940 struct scsi_cmnd *as; /* ptr to scsi cmd inside aborted command. */
3941 char msg[256]; /* For debug messaging. */
3942 int ml = 0;
Scott Teel17eb87d2014-02-18 13:55:28 -06003943 u32 tagupper, taglower;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003944
3945 /* Find the controller of the command to be aborted */
3946 h = sdev_to_hba(sc->device);
3947 if (WARN(h == NULL,
3948 "ABORT REQUEST FAILED, Controller lookup failed.\n"))
3949 return FAILED;
3950
3951 /* Check that controller supports some kind of task abort */
3952 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
3953 !(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
3954 return FAILED;
3955
3956 memset(msg, 0, sizeof(msg));
3957 ml += sprintf(msg+ml, "ABORT REQUEST on C%d:B%d:T%d:L%d ",
3958 h->scsi_host->host_no, sc->device->channel,
3959 sc->device->id, sc->device->lun);
3960
3961 /* Find the device of the command to be aborted */
3962 dev = sc->device->hostdata;
3963 if (!dev) {
3964 dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
3965 msg);
3966 return FAILED;
3967 }
3968
3969 /* Get SCSI command to be aborted */
3970 abort = (struct CommandList *) sc->host_scribble;
3971 if (abort == NULL) {
3972 dev_err(&h->pdev->dev, "%s FAILED, Command to abort is NULL.\n",
3973 msg);
3974 return FAILED;
3975 }
Scott Teel17eb87d2014-02-18 13:55:28 -06003976 hpsa_get_tag(h, abort, &taglower, &tagupper);
3977 ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003978 as = (struct scsi_cmnd *) abort->scsi_cmd;
3979 if (as != NULL)
3980 ml += sprintf(msg+ml, "Command:0x%x SN:0x%lx ",
3981 as->cmnd[0], as->serial_number);
3982 dev_dbg(&h->pdev->dev, "%s\n", msg);
3983 dev_warn(&h->pdev->dev, "Abort request on C%d:B%d:T%d:L%d\n",
3984 h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
3985
3986 /* Search reqQ to See if command is queued but not submitted,
3987 * if so, complete the command with aborted status and remove
3988 * it from the reqQ.
3989 */
3990 found = hpsa_find_cmd_in_queue(h, sc, &h->reqQ);
3991 if (found) {
3992 found->err_info->CommandStatus = CMD_ABORTED;
3993 finish_cmd(found);
3994 dev_info(&h->pdev->dev, "%s Request SUCCEEDED (driver queue).\n",
3995 msg);
3996 return SUCCESS;
3997 }
3998
3999 /* not in reqQ, if also not in cmpQ, must have already completed */
4000 found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
4001 if (!found) {
Stephen M. Camerond6ebd0f2012-07-26 11:34:17 -05004002 dev_dbg(&h->pdev->dev, "%s Request SUCCEEDED (not known to driver).\n",
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004003 msg);
4004 return SUCCESS;
4005 }
4006
4007 /*
4008 * Command is in flight, or possibly already completed
4009 * by the firmware (but not to the scsi mid layer) but we can't
4010 * distinguish which. Send the abort down.
4011 */
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05004012 rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004013 if (rc != 0) {
4014 dev_dbg(&h->pdev->dev, "%s Request FAILED.\n", msg);
4015 dev_warn(&h->pdev->dev, "FAILED abort on device C%d:B%d:T%d:L%d\n",
4016 h->scsi_host->host_no,
4017 dev->bus, dev->target, dev->lun);
4018 return FAILED;
4019 }
4020 dev_info(&h->pdev->dev, "%s REQUEST SUCCEEDED.\n", msg);
4021
4022 /* If the abort(s) above completed and actually aborted the
4023 * command, then the command to be aborted should already be
4024 * completed. If not, wait around a bit more to see if they
4025 * manage to complete normally.
4026 */
4027#define ABORT_COMPLETE_WAIT_SECS 30
4028 for (i = 0; i < ABORT_COMPLETE_WAIT_SECS * 10; i++) {
4029 found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
4030 if (!found)
4031 return SUCCESS;
4032 msleep(100);
4033 }
4034 dev_warn(&h->pdev->dev, "%s FAILED. Aborted command has not completed after %d seconds.\n",
4035 msg, ABORT_COMPLETE_WAIT_SECS);
4036 return FAILED;
4037}
4038
4039
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004040/*
4041 * For operations that cannot sleep, a command block is allocated at init,
4042 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
4043 * which ones are free or in use. Lock must be held when calling this.
4044 * cmd_free() is the complement.
4045 */
4046static struct CommandList *cmd_alloc(struct ctlr_info *h)
4047{
4048 struct CommandList *c;
4049 int i;
4050 union u64bit temp64;
4051 dma_addr_t cmd_dma_handle, err_dma_handle;
Matt Gatese16a33a2012-05-01 11:43:11 -05004052 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004053
Matt Gatese16a33a2012-05-01 11:43:11 -05004054 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004055 do {
4056 i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
Matt Gatese16a33a2012-05-01 11:43:11 -05004057 if (i == h->nr_cmds) {
4058 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004059 return NULL;
Matt Gatese16a33a2012-05-01 11:43:11 -05004060 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004061 } while (test_and_set_bit
4062 (i & (BITS_PER_LONG - 1),
4063 h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
Matt Gatese16a33a2012-05-01 11:43:11 -05004064 spin_unlock_irqrestore(&h->lock, flags);
4065
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004066 c = h->cmd_pool + i;
4067 memset(c, 0, sizeof(*c));
4068 cmd_dma_handle = h->cmd_pool_dhandle
4069 + i * sizeof(*c);
4070 c->err_info = h->errinfo_pool + i;
4071 memset(c->err_info, 0, sizeof(*c->err_info));
4072 err_dma_handle = h->errinfo_pool_dhandle
4073 + i * sizeof(*c->err_info);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004074
4075 c->cmdindex = i;
4076
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06004077 INIT_LIST_HEAD(&c->list);
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004078 c->busaddr = (u32) cmd_dma_handle;
4079 temp64.val = (u64) err_dma_handle;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004080 c->ErrDesc.Addr.lower = temp64.val32.lower;
4081 c->ErrDesc.Addr.upper = temp64.val32.upper;
4082 c->ErrDesc.Len = sizeof(*c->err_info);
4083
4084 c->h = h;
4085 return c;
4086}
4087
4088/* For operations that can wait for kmalloc to possibly sleep,
4089 * this routine can be called. Lock need not be held to call
4090 * cmd_special_alloc. cmd_special_free() is the complement.
4091 */
4092static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
4093{
4094 struct CommandList *c;
4095 union u64bit temp64;
4096 dma_addr_t cmd_dma_handle, err_dma_handle;
4097
4098 c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
4099 if (c == NULL)
4100 return NULL;
4101 memset(c, 0, sizeof(*c));
4102
Matt Gatese1f7de02014-02-18 13:55:17 -06004103 c->cmd_type = CMD_SCSI;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004104 c->cmdindex = -1;
4105
4106 c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info),
4107 &err_dma_handle);
4108
4109 if (c->err_info == NULL) {
4110 pci_free_consistent(h->pdev,
4111 sizeof(*c), c, cmd_dma_handle);
4112 return NULL;
4113 }
4114 memset(c->err_info, 0, sizeof(*c->err_info));
4115
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06004116 INIT_LIST_HEAD(&c->list);
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004117 c->busaddr = (u32) cmd_dma_handle;
4118 temp64.val = (u64) err_dma_handle;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004119 c->ErrDesc.Addr.lower = temp64.val32.lower;
4120 c->ErrDesc.Addr.upper = temp64.val32.upper;
4121 c->ErrDesc.Len = sizeof(*c->err_info);
4122
4123 c->h = h;
4124 return c;
4125}
4126
4127static void cmd_free(struct ctlr_info *h, struct CommandList *c)
4128{
4129 int i;
Matt Gatese16a33a2012-05-01 11:43:11 -05004130 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004131
4132 i = c - h->cmd_pool;
Matt Gatese16a33a2012-05-01 11:43:11 -05004133 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004134 clear_bit(i & (BITS_PER_LONG - 1),
4135 h->cmd_pool_bits + (i / BITS_PER_LONG));
Matt Gatese16a33a2012-05-01 11:43:11 -05004136 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004137}
4138
4139static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
4140{
4141 union u64bit temp64;
4142
4143 temp64.val32.lower = c->ErrDesc.Addr.lower;
4144 temp64.val32.upper = c->ErrDesc.Addr.upper;
4145 pci_free_consistent(h->pdev, sizeof(*c->err_info),
4146 c->err_info, (dma_addr_t) temp64.val);
4147 pci_free_consistent(h->pdev, sizeof(*c),
Stephen M. Camerond896f3f2011-01-06 14:47:53 -06004148 c, (dma_addr_t) (c->busaddr & DIRECT_LOOKUP_MASK));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004149}
4150
4151#ifdef CONFIG_COMPAT
4152
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004153static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
4154{
4155 IOCTL32_Command_struct __user *arg32 =
4156 (IOCTL32_Command_struct __user *) arg;
4157 IOCTL_Command_struct arg64;
4158 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
4159 int err;
4160 u32 cp;
4161
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06004162 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004163 err = 0;
4164 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
4165 sizeof(arg64.LUN_info));
4166 err |= copy_from_user(&arg64.Request, &arg32->Request,
4167 sizeof(arg64.Request));
4168 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
4169 sizeof(arg64.error_info));
4170 err |= get_user(arg64.buf_size, &arg32->buf_size);
4171 err |= get_user(cp, &arg32->buf);
4172 arg64.buf = compat_ptr(cp);
4173 err |= copy_to_user(p, &arg64, sizeof(arg64));
4174
4175 if (err)
4176 return -EFAULT;
4177
Stephen M. Camerone39eeae2010-02-04 08:43:46 -06004178 err = hpsa_ioctl(dev, CCISS_PASSTHRU, (void *)p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004179 if (err)
4180 return err;
4181 err |= copy_in_user(&arg32->error_info, &p->error_info,
4182 sizeof(arg32->error_info));
4183 if (err)
4184 return -EFAULT;
4185 return err;
4186}
4187
4188static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
4189 int cmd, void *arg)
4190{
4191 BIG_IOCTL32_Command_struct __user *arg32 =
4192 (BIG_IOCTL32_Command_struct __user *) arg;
4193 BIG_IOCTL_Command_struct arg64;
4194 BIG_IOCTL_Command_struct __user *p =
4195 compat_alloc_user_space(sizeof(arg64));
4196 int err;
4197 u32 cp;
4198
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06004199 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004200 err = 0;
4201 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
4202 sizeof(arg64.LUN_info));
4203 err |= copy_from_user(&arg64.Request, &arg32->Request,
4204 sizeof(arg64.Request));
4205 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
4206 sizeof(arg64.error_info));
4207 err |= get_user(arg64.buf_size, &arg32->buf_size);
4208 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
4209 err |= get_user(cp, &arg32->buf);
4210 arg64.buf = compat_ptr(cp);
4211 err |= copy_to_user(p, &arg64, sizeof(arg64));
4212
4213 if (err)
4214 return -EFAULT;
4215
Stephen M. Camerone39eeae2010-02-04 08:43:46 -06004216 err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004217 if (err)
4218 return err;
4219 err |= copy_in_user(&arg32->error_info, &p->error_info,
4220 sizeof(arg32->error_info));
4221 if (err)
4222 return -EFAULT;
4223 return err;
4224}
Stephen M. Cameron71fe75a2010-02-04 08:43:51 -06004225
4226static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
4227{
4228 switch (cmd) {
4229 case CCISS_GETPCIINFO:
4230 case CCISS_GETINTINFO:
4231 case CCISS_SETINTINFO:
4232 case CCISS_GETNODENAME:
4233 case CCISS_SETNODENAME:
4234 case CCISS_GETHEARTBEAT:
4235 case CCISS_GETBUSTYPES:
4236 case CCISS_GETFIRMVER:
4237 case CCISS_GETDRIVVER:
4238 case CCISS_REVALIDVOLS:
4239 case CCISS_DEREGDISK:
4240 case CCISS_REGNEWDISK:
4241 case CCISS_REGNEWD:
4242 case CCISS_RESCANDISK:
4243 case CCISS_GETLUNINFO:
4244 return hpsa_ioctl(dev, cmd, arg);
4245
4246 case CCISS_PASSTHRU32:
4247 return hpsa_ioctl32_passthru(dev, cmd, arg);
4248 case CCISS_BIG_PASSTHRU32:
4249 return hpsa_ioctl32_big_passthru(dev, cmd, arg);
4250
4251 default:
4252 return -ENOIOCTLCMD;
4253 }
4254}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004255#endif
4256
4257static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
4258{
4259 struct hpsa_pci_info pciinfo;
4260
4261 if (!argp)
4262 return -EINVAL;
4263 pciinfo.domain = pci_domain_nr(h->pdev->bus);
4264 pciinfo.bus = h->pdev->bus->number;
4265 pciinfo.dev_fn = h->pdev->devfn;
4266 pciinfo.board_id = h->board_id;
4267 if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
4268 return -EFAULT;
4269 return 0;
4270}
4271
4272static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
4273{
4274 DriverVer_type DriverVer;
4275 unsigned char vmaj, vmin, vsubmin;
4276 int rc;
4277
4278 rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
4279 &vmaj, &vmin, &vsubmin);
4280 if (rc != 3) {
4281 dev_info(&h->pdev->dev, "driver version string '%s' "
4282 "unrecognized.", HPSA_DRIVER_VERSION);
4283 vmaj = 0;
4284 vmin = 0;
4285 vsubmin = 0;
4286 }
4287 DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
4288 if (!argp)
4289 return -EINVAL;
4290 if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
4291 return -EFAULT;
4292 return 0;
4293}
4294
4295static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
4296{
4297 IOCTL_Command_struct iocommand;
4298 struct CommandList *c;
4299 char *buff = NULL;
4300 union u64bit temp64;
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004301 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004302
4303 if (!argp)
4304 return -EINVAL;
4305 if (!capable(CAP_SYS_RAWIO))
4306 return -EPERM;
4307 if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
4308 return -EFAULT;
4309 if ((iocommand.buf_size < 1) &&
4310 (iocommand.Request.Type.Direction != XFER_NONE)) {
4311 return -EINVAL;
4312 }
4313 if (iocommand.buf_size > 0) {
4314 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
4315 if (buff == NULL)
4316 return -EFAULT;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004317 if (iocommand.Request.Type.Direction == XFER_WRITE) {
4318 /* Copy the data into the buffer we created */
4319 if (copy_from_user(buff, iocommand.buf,
4320 iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004321 rc = -EFAULT;
4322 goto out_kfree;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004323 }
4324 } else {
4325 memset(buff, 0, iocommand.buf_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004326 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004327 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004328 c = cmd_special_alloc(h);
4329 if (c == NULL) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004330 rc = -ENOMEM;
4331 goto out_kfree;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004332 }
4333 /* Fill in the command type */
4334 c->cmd_type = CMD_IOCTL_PEND;
4335 /* Fill in Command Header */
4336 c->Header.ReplyQueue = 0; /* unused in simple mode */
4337 if (iocommand.buf_size > 0) { /* buffer to fill */
4338 c->Header.SGList = 1;
4339 c->Header.SGTotal = 1;
4340 } else { /* no buffers to fill */
4341 c->Header.SGList = 0;
4342 c->Header.SGTotal = 0;
4343 }
4344 memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
4345 /* use the kernel address the cmd block for tag */
4346 c->Header.Tag.lower = c->busaddr;
4347
4348 /* Fill in Request block */
4349 memcpy(&c->Request, &iocommand.Request,
4350 sizeof(c->Request));
4351
4352 /* Fill in the scatter gather information */
4353 if (iocommand.buf_size > 0) {
4354 temp64.val = pci_map_single(h->pdev, buff,
4355 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06004356 if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
4357 c->SG[0].Addr.lower = 0;
4358 c->SG[0].Addr.upper = 0;
4359 c->SG[0].Len = 0;
4360 rc = -ENOMEM;
4361 goto out;
4362 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004363 c->SG[0].Addr.lower = temp64.val32.lower;
4364 c->SG[0].Addr.upper = temp64.val32.upper;
4365 c->SG[0].Len = iocommand.buf_size;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06004366 c->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining*/
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004367 }
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004368 hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
Stephen M. Cameronc2dd32e2011-06-03 09:57:29 -05004369 if (iocommand.buf_size > 0)
4370 hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004371 check_ioctl_unit_attention(h, c);
4372
4373 /* Copy the error information out */
4374 memcpy(&iocommand.error_info, c->err_info,
4375 sizeof(iocommand.error_info));
4376 if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004377 rc = -EFAULT;
4378 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004379 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004380 if (iocommand.Request.Type.Direction == XFER_READ &&
4381 iocommand.buf_size > 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004382 /* Copy the data out of the buffer we created */
4383 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004384 rc = -EFAULT;
4385 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004386 }
4387 }
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004388out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004389 cmd_special_free(h, c);
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004390out_kfree:
4391 kfree(buff);
4392 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004393}
4394
4395static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
4396{
4397 BIG_IOCTL_Command_struct *ioc;
4398 struct CommandList *c;
4399 unsigned char **buff = NULL;
4400 int *buff_size = NULL;
4401 union u64bit temp64;
4402 BYTE sg_used = 0;
4403 int status = 0;
4404 int i;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004405 u32 left;
4406 u32 sz;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004407 BYTE __user *data_ptr;
4408
4409 if (!argp)
4410 return -EINVAL;
4411 if (!capable(CAP_SYS_RAWIO))
4412 return -EPERM;
4413 ioc = (BIG_IOCTL_Command_struct *)
4414 kmalloc(sizeof(*ioc), GFP_KERNEL);
4415 if (!ioc) {
4416 status = -ENOMEM;
4417 goto cleanup1;
4418 }
4419 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
4420 status = -EFAULT;
4421 goto cleanup1;
4422 }
4423 if ((ioc->buf_size < 1) &&
4424 (ioc->Request.Type.Direction != XFER_NONE)) {
4425 status = -EINVAL;
4426 goto cleanup1;
4427 }
4428 /* Check kmalloc limits using all SGs */
4429 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
4430 status = -EINVAL;
4431 goto cleanup1;
4432 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06004433 if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004434 status = -EINVAL;
4435 goto cleanup1;
4436 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06004437 buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004438 if (!buff) {
4439 status = -ENOMEM;
4440 goto cleanup1;
4441 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06004442 buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004443 if (!buff_size) {
4444 status = -ENOMEM;
4445 goto cleanup1;
4446 }
4447 left = ioc->buf_size;
4448 data_ptr = ioc->buf;
4449 while (left) {
4450 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
4451 buff_size[sg_used] = sz;
4452 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
4453 if (buff[sg_used] == NULL) {
4454 status = -ENOMEM;
4455 goto cleanup1;
4456 }
4457 if (ioc->Request.Type.Direction == XFER_WRITE) {
4458 if (copy_from_user(buff[sg_used], data_ptr, sz)) {
4459 status = -ENOMEM;
4460 goto cleanup1;
4461 }
4462 } else
4463 memset(buff[sg_used], 0, sz);
4464 left -= sz;
4465 data_ptr += sz;
4466 sg_used++;
4467 }
4468 c = cmd_special_alloc(h);
4469 if (c == NULL) {
4470 status = -ENOMEM;
4471 goto cleanup1;
4472 }
4473 c->cmd_type = CMD_IOCTL_PEND;
4474 c->Header.ReplyQueue = 0;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004475 c->Header.SGList = c->Header.SGTotal = sg_used;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004476 memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
4477 c->Header.Tag.lower = c->busaddr;
4478 memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
4479 if (ioc->buf_size > 0) {
4480 int i;
4481 for (i = 0; i < sg_used; i++) {
4482 temp64.val = pci_map_single(h->pdev, buff[i],
4483 buff_size[i], PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06004484 if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
4485 c->SG[i].Addr.lower = 0;
4486 c->SG[i].Addr.upper = 0;
4487 c->SG[i].Len = 0;
4488 hpsa_pci_unmap(h->pdev, c, i,
4489 PCI_DMA_BIDIRECTIONAL);
4490 status = -ENOMEM;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05004491 goto cleanup0;
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06004492 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004493 c->SG[i].Addr.lower = temp64.val32.lower;
4494 c->SG[i].Addr.upper = temp64.val32.upper;
4495 c->SG[i].Len = buff_size[i];
Matt Gatese1d9cbf2014-02-18 13:55:12 -06004496 c->SG[i].Ext = i < sg_used - 1 ? 0 : HPSA_SG_LAST;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004497 }
4498 }
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004499 hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004500 if (sg_used)
4501 hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004502 check_ioctl_unit_attention(h, c);
4503 /* Copy the error information out */
4504 memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
4505 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004506 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05004507 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004508 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004509 if (ioc->Request.Type.Direction == XFER_READ && ioc->buf_size > 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004510 /* Copy the data out of the buffer we created */
4511 BYTE __user *ptr = ioc->buf;
4512 for (i = 0; i < sg_used; i++) {
4513 if (copy_to_user(ptr, buff[i], buff_size[i])) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004514 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05004515 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004516 }
4517 ptr += buff_size[i];
4518 }
4519 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004520 status = 0;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05004521cleanup0:
4522 cmd_special_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004523cleanup1:
4524 if (buff) {
4525 for (i = 0; i < sg_used; i++)
4526 kfree(buff[i]);
4527 kfree(buff);
4528 }
4529 kfree(buff_size);
4530 kfree(ioc);
4531 return status;
4532}
4533
4534static void check_ioctl_unit_attention(struct ctlr_info *h,
4535 struct CommandList *c)
4536{
4537 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
4538 c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
4539 (void) check_for_unit_attention(h, c);
4540}
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05004541
4542static int increment_passthru_count(struct ctlr_info *h)
4543{
4544 unsigned long flags;
4545
4546 spin_lock_irqsave(&h->passthru_count_lock, flags);
4547 if (h->passthru_count >= HPSA_MAX_CONCURRENT_PASSTHRUS) {
4548 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
4549 return -1;
4550 }
4551 h->passthru_count++;
4552 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
4553 return 0;
4554}
4555
4556static void decrement_passthru_count(struct ctlr_info *h)
4557{
4558 unsigned long flags;
4559
4560 spin_lock_irqsave(&h->passthru_count_lock, flags);
4561 if (h->passthru_count <= 0) {
4562 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
4563 /* not expecting to get here. */
4564 dev_warn(&h->pdev->dev, "Bug detected, passthru_count seems to be incorrect.\n");
4565 return;
4566 }
4567 h->passthru_count--;
4568 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
4569}
4570
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004571/*
4572 * ioctl
4573 */
4574static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
4575{
4576 struct ctlr_info *h;
4577 void __user *argp = (void __user *)arg;
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05004578 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004579
4580 h = sdev_to_hba(dev);
4581
4582 switch (cmd) {
4583 case CCISS_DEREGDISK:
4584 case CCISS_REGNEWDISK:
4585 case CCISS_REGNEWD:
Stephen M. Camerona08a84712010-02-04 08:43:16 -06004586 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004587 return 0;
4588 case CCISS_GETPCIINFO:
4589 return hpsa_getpciinfo_ioctl(h, argp);
4590 case CCISS_GETDRIVVER:
4591 return hpsa_getdrivver_ioctl(h, argp);
4592 case CCISS_PASSTHRU:
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05004593 if (increment_passthru_count(h))
4594 return -EAGAIN;
4595 rc = hpsa_passthru_ioctl(h, argp);
4596 decrement_passthru_count(h);
4597 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004598 case CCISS_BIG_PASSTHRU:
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05004599 if (increment_passthru_count(h))
4600 return -EAGAIN;
4601 rc = hpsa_big_passthru_ioctl(h, argp);
4602 decrement_passthru_count(h);
4603 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004604 default:
4605 return -ENOTTY;
4606 }
4607}
4608
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004609static int hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
4610 u8 reset_type)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004611{
4612 struct CommandList *c;
4613
4614 c = cmd_alloc(h);
4615 if (!c)
4616 return -ENOMEM;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004617 /* fill_cmd can't fail here, no data buffer to map */
4618 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004619 RAID_CTLR_LUNID, TYPE_MSG);
4620 c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
4621 c->waiting = NULL;
4622 enqueue_cmd_and_start_io(h, c);
4623 /* Don't wait for completion, the reset won't complete. Don't free
4624 * the command either. This is the last command we will send before
4625 * re-initializing everything, so it doesn't matter and won't leak.
4626 */
4627 return 0;
4628}
4629
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004630static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004631 void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004632 int cmd_type)
4633{
4634 int pci_dir = XFER_NONE;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004635 struct CommandList *a; /* for commands to be aborted */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004636
4637 c->cmd_type = CMD_IOCTL_PEND;
4638 c->Header.ReplyQueue = 0;
4639 if (buff != NULL && size > 0) {
4640 c->Header.SGList = 1;
4641 c->Header.SGTotal = 1;
4642 } else {
4643 c->Header.SGList = 0;
4644 c->Header.SGTotal = 0;
4645 }
4646 c->Header.Tag.lower = c->busaddr;
4647 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
4648
4649 c->Request.Type.Type = cmd_type;
4650 if (cmd_type == TYPE_CMD) {
4651 switch (cmd) {
4652 case HPSA_INQUIRY:
4653 /* are we trying to read a vital product page */
4654 if (page_code != 0) {
4655 c->Request.CDB[1] = 0x01;
4656 c->Request.CDB[2] = page_code;
4657 }
4658 c->Request.CDBLen = 6;
4659 c->Request.Type.Attribute = ATTR_SIMPLE;
4660 c->Request.Type.Direction = XFER_READ;
4661 c->Request.Timeout = 0;
4662 c->Request.CDB[0] = HPSA_INQUIRY;
4663 c->Request.CDB[4] = size & 0xFF;
4664 break;
4665 case HPSA_REPORT_LOG:
4666 case HPSA_REPORT_PHYS:
4667 /* Talking to controller so It's a physical command
4668 mode = 00 target = 0. Nothing to write.
4669 */
4670 c->Request.CDBLen = 12;
4671 c->Request.Type.Attribute = ATTR_SIMPLE;
4672 c->Request.Type.Direction = XFER_READ;
4673 c->Request.Timeout = 0;
4674 c->Request.CDB[0] = cmd;
4675 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
4676 c->Request.CDB[7] = (size >> 16) & 0xFF;
4677 c->Request.CDB[8] = (size >> 8) & 0xFF;
4678 c->Request.CDB[9] = size & 0xFF;
4679 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004680 case HPSA_CACHE_FLUSH:
4681 c->Request.CDBLen = 12;
4682 c->Request.Type.Attribute = ATTR_SIMPLE;
4683 c->Request.Type.Direction = XFER_WRITE;
4684 c->Request.Timeout = 0;
4685 c->Request.CDB[0] = BMIC_WRITE;
4686 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
Stephen M. Cameronbb158ea2011-10-26 16:21:17 -05004687 c->Request.CDB[7] = (size >> 8) & 0xFF;
4688 c->Request.CDB[8] = size & 0xFF;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004689 break;
4690 case TEST_UNIT_READY:
4691 c->Request.CDBLen = 6;
4692 c->Request.Type.Attribute = ATTR_SIMPLE;
4693 c->Request.Type.Direction = XFER_NONE;
4694 c->Request.Timeout = 0;
4695 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004696 case HPSA_GET_RAID_MAP:
4697 c->Request.CDBLen = 12;
4698 c->Request.Type.Attribute = ATTR_SIMPLE;
4699 c->Request.Type.Direction = XFER_READ;
4700 c->Request.Timeout = 0;
4701 c->Request.CDB[0] = HPSA_CISS_READ;
4702 c->Request.CDB[1] = cmd;
4703 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
4704 c->Request.CDB[7] = (size >> 16) & 0xFF;
4705 c->Request.CDB[8] = (size >> 8) & 0xFF;
4706 c->Request.CDB[9] = size & 0xFF;
4707 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004708 default:
4709 dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
4710 BUG();
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004711 return -1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004712 }
4713 } else if (cmd_type == TYPE_MSG) {
4714 switch (cmd) {
4715
4716 case HPSA_DEVICE_RESET_MSG:
4717 c->Request.CDBLen = 16;
4718 c->Request.Type.Type = 1; /* It is a MSG not a CMD */
4719 c->Request.Type.Attribute = ATTR_SIMPLE;
4720 c->Request.Type.Direction = XFER_NONE;
4721 c->Request.Timeout = 0; /* Don't time out */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004722 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
4723 c->Request.CDB[0] = cmd;
Stephen M. Cameron21e89af2012-07-26 11:34:10 -05004724 c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004725 /* If bytes 4-7 are zero, it means reset the */
4726 /* LunID device */
4727 c->Request.CDB[4] = 0x00;
4728 c->Request.CDB[5] = 0x00;
4729 c->Request.CDB[6] = 0x00;
4730 c->Request.CDB[7] = 0x00;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004731 break;
4732 case HPSA_ABORT_MSG:
4733 a = buff; /* point to command to be aborted */
4734 dev_dbg(&h->pdev->dev, "Abort Tag:0x%08x:%08x using request Tag:0x%08x:%08x\n",
4735 a->Header.Tag.upper, a->Header.Tag.lower,
4736 c->Header.Tag.upper, c->Header.Tag.lower);
4737 c->Request.CDBLen = 16;
4738 c->Request.Type.Type = TYPE_MSG;
4739 c->Request.Type.Attribute = ATTR_SIMPLE;
4740 c->Request.Type.Direction = XFER_WRITE;
4741 c->Request.Timeout = 0; /* Don't time out */
4742 c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
4743 c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
4744 c->Request.CDB[2] = 0x00; /* reserved */
4745 c->Request.CDB[3] = 0x00; /* reserved */
4746 /* Tag to abort goes in CDB[4]-CDB[11] */
4747 c->Request.CDB[4] = a->Header.Tag.lower & 0xFF;
4748 c->Request.CDB[5] = (a->Header.Tag.lower >> 8) & 0xFF;
4749 c->Request.CDB[6] = (a->Header.Tag.lower >> 16) & 0xFF;
4750 c->Request.CDB[7] = (a->Header.Tag.lower >> 24) & 0xFF;
4751 c->Request.CDB[8] = a->Header.Tag.upper & 0xFF;
4752 c->Request.CDB[9] = (a->Header.Tag.upper >> 8) & 0xFF;
4753 c->Request.CDB[10] = (a->Header.Tag.upper >> 16) & 0xFF;
4754 c->Request.CDB[11] = (a->Header.Tag.upper >> 24) & 0xFF;
4755 c->Request.CDB[12] = 0x00; /* reserved */
4756 c->Request.CDB[13] = 0x00; /* reserved */
4757 c->Request.CDB[14] = 0x00; /* reserved */
4758 c->Request.CDB[15] = 0x00; /* reserved */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004759 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004760 default:
4761 dev_warn(&h->pdev->dev, "unknown message type %d\n",
4762 cmd);
4763 BUG();
4764 }
4765 } else {
4766 dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
4767 BUG();
4768 }
4769
4770 switch (c->Request.Type.Direction) {
4771 case XFER_READ:
4772 pci_dir = PCI_DMA_FROMDEVICE;
4773 break;
4774 case XFER_WRITE:
4775 pci_dir = PCI_DMA_TODEVICE;
4776 break;
4777 case XFER_NONE:
4778 pci_dir = PCI_DMA_NONE;
4779 break;
4780 default:
4781 pci_dir = PCI_DMA_BIDIRECTIONAL;
4782 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004783 if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
4784 return -1;
4785 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004786}
4787
4788/*
4789 * Map (physical) PCI mem into (virtual) kernel space
4790 */
4791static void __iomem *remap_pci_mem(ulong base, ulong size)
4792{
4793 ulong page_base = ((ulong) base) & PAGE_MASK;
4794 ulong page_offs = ((ulong) base) - page_base;
Stephen M. Cameron088ba34c2012-07-26 11:34:23 -05004795 void __iomem *page_remapped = ioremap_nocache(page_base,
4796 page_offs + size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004797
4798 return page_remapped ? (page_remapped + page_offs) : NULL;
4799}
4800
4801/* Takes cmds off the submission queue and sends them to the hardware,
4802 * then puts them on the queue of cmds waiting for completion.
4803 */
4804static void start_io(struct ctlr_info *h)
4805{
4806 struct CommandList *c;
Matt Gatese16a33a2012-05-01 11:43:11 -05004807 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004808
Matt Gatese16a33a2012-05-01 11:43:11 -05004809 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06004810 while (!list_empty(&h->reqQ)) {
4811 c = list_entry(h->reqQ.next, struct CommandList, list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004812 /* can't do anything if fifo is full */
4813 if ((h->access.fifo_full(h))) {
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004814 h->fifo_recently_full = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004815 dev_warn(&h->pdev->dev, "fifo full\n");
4816 break;
4817 }
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004818 h->fifo_recently_full = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004819
4820 /* Get the first entry from the Request Q */
4821 removeQ(c);
4822 h->Qdepth--;
4823
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004824 /* Put job onto the completed Q */
4825 addQ(&h->cmpQ, c);
Matt Gatese16a33a2012-05-01 11:43:11 -05004826
4827 /* Must increment commands_outstanding before unlocking
4828 * and submitting to avoid race checking for fifo full
4829 * condition.
4830 */
4831 h->commands_outstanding++;
4832 if (h->commands_outstanding > h->max_outstanding)
4833 h->max_outstanding = h->commands_outstanding;
4834
4835 /* Tell the controller execute command */
4836 spin_unlock_irqrestore(&h->lock, flags);
4837 h->access.submit_command(h, c);
4838 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004839 }
Matt Gatese16a33a2012-05-01 11:43:11 -05004840 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004841}
4842
Matt Gates254f7962012-05-01 11:43:06 -05004843static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004844{
Matt Gates254f7962012-05-01 11:43:06 -05004845 return h->access.command_completed(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004846}
4847
Stephen M. Cameron900c5442010-02-04 08:42:35 -06004848static inline bool interrupt_pending(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004849{
4850 return h->access.intr_pending(h);
4851}
4852
4853static inline long interrupt_not_for_us(struct ctlr_info *h)
4854{
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004855 return (h->access.intr_pending(h) == 0) ||
4856 (h->interrupts_enabled == 0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004857}
4858
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004859static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
4860 u32 raw_tag)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004861{
4862 if (unlikely(tag_index >= h->nr_cmds)) {
4863 dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
4864 return 1;
4865 }
4866 return 0;
4867}
4868
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05004869static inline void finish_cmd(struct CommandList *c)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004870{
Matt Gatese16a33a2012-05-01 11:43:11 -05004871 unsigned long flags;
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004872 int io_may_be_stalled = 0;
4873 struct ctlr_info *h = c->h;
Matt Gatese16a33a2012-05-01 11:43:11 -05004874
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004875 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004876 removeQ(c);
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004877
4878 /*
4879 * Check for possibly stalled i/o.
4880 *
4881 * If a fifo_full condition is encountered, requests will back up
4882 * in h->reqQ. This queue is only emptied out by start_io which is
4883 * only called when a new i/o request comes in. If no i/o's are
4884 * forthcoming, the i/o's in h->reqQ can get stuck. So we call
4885 * start_io from here if we detect such a danger.
4886 *
4887 * Normally, we shouldn't hit this case, but pounding on the
4888 * CCISS_PASSTHRU ioctl can provoke it. Only call start_io if
4889 * commands_outstanding is low. We want to avoid calling
4890 * start_io from in here as much as possible, and esp. don't
4891 * want to get in a cycle where we call start_io every time
4892 * through here.
4893 */
4894 if (unlikely(h->fifo_recently_full) &&
4895 h->commands_outstanding < 5)
4896 io_may_be_stalled = 1;
4897
4898 spin_unlock_irqrestore(&h->lock, flags);
4899
Stephen M. Camerone85c5972012-05-01 11:43:42 -05004900 dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
Scott Teelc3497752014-02-18 13:56:34 -06004901 if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
4902 || c->cmd_type == CMD_IOACCEL2))
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05004903 complete_scsi_command(c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004904 else if (c->cmd_type == CMD_IOCTL_PEND)
4905 complete(c->waiting);
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004906 if (unlikely(io_may_be_stalled))
4907 start_io(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004908}
4909
Stephen M. Camerona104c992010-02-04 08:42:24 -06004910static inline u32 hpsa_tag_contains_index(u32 tag)
4911{
Stephen M. Camerona104c992010-02-04 08:42:24 -06004912 return tag & DIRECT_LOOKUP_BIT;
4913}
4914
4915static inline u32 hpsa_tag_to_index(u32 tag)
4916{
Stephen M. Camerona104c992010-02-04 08:42:24 -06004917 return tag >> DIRECT_LOOKUP_SHIFT;
4918}
4919
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004920
4921static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
Stephen M. Camerona104c992010-02-04 08:42:24 -06004922{
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004923#define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
4924#define HPSA_SIMPLE_ERROR_BITS 0x03
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06004925 if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004926 return tag & ~HPSA_SIMPLE_ERROR_BITS;
4927 return tag & ~HPSA_PERF_ERROR_BITS;
Stephen M. Camerona104c992010-02-04 08:42:24 -06004928}
4929
Don Brace303932f2010-02-04 08:42:40 -06004930/* process completion of an indexed ("direct lookup") command */
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004931static inline void process_indexed_cmd(struct ctlr_info *h,
Don Brace303932f2010-02-04 08:42:40 -06004932 u32 raw_tag)
4933{
4934 u32 tag_index;
4935 struct CommandList *c;
4936
4937 tag_index = hpsa_tag_to_index(raw_tag);
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004938 if (!bad_tag(h, tag_index, raw_tag)) {
4939 c = h->cmd_pool + tag_index;
4940 finish_cmd(c);
4941 }
Don Brace303932f2010-02-04 08:42:40 -06004942}
4943
4944/* process completion of a non-indexed command */
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004945static inline void process_nonindexed_cmd(struct ctlr_info *h,
Don Brace303932f2010-02-04 08:42:40 -06004946 u32 raw_tag)
4947{
4948 u32 tag;
4949 struct CommandList *c = NULL;
Matt Gatese16a33a2012-05-01 11:43:11 -05004950 unsigned long flags;
Don Brace303932f2010-02-04 08:42:40 -06004951
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004952 tag = hpsa_tag_discard_error_bits(h, raw_tag);
Matt Gatese16a33a2012-05-01 11:43:11 -05004953 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06004954 list_for_each_entry(c, &h->cmpQ, list) {
Don Brace303932f2010-02-04 08:42:40 -06004955 if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
Matt Gatese16a33a2012-05-01 11:43:11 -05004956 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05004957 finish_cmd(c);
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004958 return;
Don Brace303932f2010-02-04 08:42:40 -06004959 }
4960 }
Matt Gatese16a33a2012-05-01 11:43:11 -05004961 spin_unlock_irqrestore(&h->lock, flags);
Don Brace303932f2010-02-04 08:42:40 -06004962 bad_tag(h, h->nr_cmds + 1, raw_tag);
Don Brace303932f2010-02-04 08:42:40 -06004963}
4964
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004965/* Some controllers, like p400, will give us one interrupt
4966 * after a soft reset, even if we turned interrupts off.
4967 * Only need to check for this in the hpsa_xxx_discard_completions
4968 * functions.
4969 */
4970static int ignore_bogus_interrupt(struct ctlr_info *h)
4971{
4972 if (likely(!reset_devices))
4973 return 0;
4974
4975 if (likely(h->interrupts_enabled))
4976 return 0;
4977
4978 dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
4979 "(known firmware bug.) Ignoring.\n");
4980
4981 return 1;
4982}
4983
Matt Gates254f7962012-05-01 11:43:06 -05004984/*
4985 * Convert &h->q[x] (passed to interrupt handlers) back to h.
4986 * Relies on (h-q[x] == x) being true for x such that
4987 * 0 <= x < MAX_REPLY_QUEUES.
4988 */
4989static struct ctlr_info *queue_to_hba(u8 *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004990{
Matt Gates254f7962012-05-01 11:43:06 -05004991 return container_of((queue - *queue), struct ctlr_info, q[0]);
4992}
4993
4994static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
4995{
4996 struct ctlr_info *h = queue_to_hba(queue);
4997 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004998 u32 raw_tag;
4999
5000 if (ignore_bogus_interrupt(h))
5001 return IRQ_NONE;
5002
5003 if (interrupt_not_for_us(h))
5004 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005005 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005006 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05005007 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005008 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05005009 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005010 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005011 return IRQ_HANDLED;
5012}
5013
Matt Gates254f7962012-05-01 11:43:06 -05005014static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005015{
Matt Gates254f7962012-05-01 11:43:06 -05005016 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005017 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05005018 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005019
5020 if (ignore_bogus_interrupt(h))
5021 return IRQ_NONE;
5022
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005023 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05005024 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005025 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05005026 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005027 return IRQ_HANDLED;
5028}
5029
Matt Gates254f7962012-05-01 11:43:06 -05005030static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005031{
Matt Gates254f7962012-05-01 11:43:06 -05005032 struct ctlr_info *h = queue_to_hba((u8 *) queue);
Don Brace303932f2010-02-04 08:42:40 -06005033 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05005034 u8 q = *(u8 *) queue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005035
5036 if (interrupt_not_for_us(h))
5037 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005038 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005039 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05005040 raw_tag = get_next_completion(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005041 while (raw_tag != FIFO_EMPTY) {
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005042 if (likely(hpsa_tag_contains_index(raw_tag)))
5043 process_indexed_cmd(h, raw_tag);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005044 else
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005045 process_nonindexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05005046 raw_tag = next_command(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005047 }
5048 }
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005049 return IRQ_HANDLED;
5050}
5051
Matt Gates254f7962012-05-01 11:43:06 -05005052static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005053{
Matt Gates254f7962012-05-01 11:43:06 -05005054 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005055 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05005056 u8 q = *(u8 *) queue;
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005057
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005058 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05005059 raw_tag = get_next_completion(h, q);
Don Brace303932f2010-02-04 08:42:40 -06005060 while (raw_tag != FIFO_EMPTY) {
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005061 if (likely(hpsa_tag_contains_index(raw_tag)))
5062 process_indexed_cmd(h, raw_tag);
Don Brace303932f2010-02-04 08:42:40 -06005063 else
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005064 process_nonindexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05005065 raw_tag = next_command(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005066 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005067 return IRQ_HANDLED;
5068}
5069
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005070/* Send a message CDB to the firmware. Careful, this only works
5071 * in simple mode, not performant mode due to the tag lookup.
5072 * We only ever use this immediately after a controller reset.
5073 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005074static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
5075 unsigned char type)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005076{
5077 struct Command {
5078 struct CommandListHeader CommandHeader;
5079 struct RequestBlock Request;
5080 struct ErrDescriptor ErrorDescriptor;
5081 };
5082 struct Command *cmd;
5083 static const size_t cmd_sz = sizeof(*cmd) +
5084 sizeof(cmd->ErrorDescriptor);
5085 dma_addr_t paddr64;
5086 uint32_t paddr32, tag;
5087 void __iomem *vaddr;
5088 int i, err;
5089
5090 vaddr = pci_ioremap_bar(pdev, 0);
5091 if (vaddr == NULL)
5092 return -ENOMEM;
5093
5094 /* The Inbound Post Queue only accepts 32-bit physical addresses for the
5095 * CCISS commands, so they must be allocated from the lower 4GiB of
5096 * memory.
5097 */
5098 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
5099 if (err) {
5100 iounmap(vaddr);
5101 return -ENOMEM;
5102 }
5103
5104 cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
5105 if (cmd == NULL) {
5106 iounmap(vaddr);
5107 return -ENOMEM;
5108 }
5109
5110 /* This must fit, because of the 32-bit consistent DMA mask. Also,
5111 * although there's no guarantee, we assume that the address is at
5112 * least 4-byte aligned (most likely, it's page-aligned).
5113 */
5114 paddr32 = paddr64;
5115
5116 cmd->CommandHeader.ReplyQueue = 0;
5117 cmd->CommandHeader.SGList = 0;
5118 cmd->CommandHeader.SGTotal = 0;
5119 cmd->CommandHeader.Tag.lower = paddr32;
5120 cmd->CommandHeader.Tag.upper = 0;
5121 memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
5122
5123 cmd->Request.CDBLen = 16;
5124 cmd->Request.Type.Type = TYPE_MSG;
5125 cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
5126 cmd->Request.Type.Direction = XFER_NONE;
5127 cmd->Request.Timeout = 0; /* Don't time out */
5128 cmd->Request.CDB[0] = opcode;
5129 cmd->Request.CDB[1] = type;
5130 memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
5131 cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
5132 cmd->ErrorDescriptor.Addr.upper = 0;
5133 cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
5134
5135 writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
5136
5137 for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
5138 tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005139 if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr32)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005140 break;
5141 msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
5142 }
5143
5144 iounmap(vaddr);
5145
5146 /* we leak the DMA buffer here ... no choice since the controller could
5147 * still complete the command.
5148 */
5149 if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
5150 dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
5151 opcode, type);
5152 return -ETIMEDOUT;
5153 }
5154
5155 pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
5156
5157 if (tag & HPSA_ERROR_BIT) {
5158 dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
5159 opcode, type);
5160 return -EIO;
5161 }
5162
5163 dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
5164 opcode, type);
5165 return 0;
5166}
5167
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005168#define hpsa_noop(p) hpsa_message(p, 3, 0)
5169
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005170static int hpsa_controller_hard_reset(struct pci_dev *pdev,
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005171 void * __iomem vaddr, u32 use_doorbell)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005172{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005173 u16 pmcsr;
5174 int pos;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005175
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005176 if (use_doorbell) {
5177 /* For everything after the P600, the PCI power state method
5178 * of resetting the controller doesn't work, so we have this
5179 * other way using the doorbell register.
5180 */
5181 dev_info(&pdev->dev, "using doorbell to reset controller\n");
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005182 writel(use_doorbell, vaddr + SA5_DOORBELL);
Stephen M. Cameron85009232013-09-23 13:33:36 -05005183
5184 /* PMC hardware guys tell us we need a 5 second delay after
5185 * doorbell reset and before any attempt to talk to the board
5186 * at all to ensure that this actually works and doesn't fall
5187 * over in some weird corner cases.
5188 */
5189 msleep(5000);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005190 } else { /* Try to do it the PCI power state way */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005191
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005192 /* Quoting from the Open CISS Specification: "The Power
5193 * Management Control/Status Register (CSR) controls the power
5194 * state of the device. The normal operating state is D0,
5195 * CSR=00h. The software off state is D3, CSR=03h. To reset
5196 * the controller, place the interface device in D3 then to D0,
5197 * this causes a secondary PCI reset which will reset the
5198 * controller." */
5199
5200 pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
5201 if (pos == 0) {
5202 dev_err(&pdev->dev,
5203 "hpsa_reset_controller: "
5204 "PCI PM not supported\n");
5205 return -ENODEV;
5206 }
5207 dev_info(&pdev->dev, "using PCI PM to reset controller\n");
5208 /* enter the D3hot power management state */
5209 pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
5210 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
5211 pmcsr |= PCI_D3hot;
5212 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
5213
5214 msleep(500);
5215
5216 /* enter the D0 power management state */
5217 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
5218 pmcsr |= PCI_D0;
5219 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
Mike Millerc4853ef2011-10-21 08:19:43 +02005220
5221 /*
5222 * The P600 requires a small delay when changing states.
5223 * Otherwise we may think the board did not reset and we bail.
5224 * This for kdump only and is particular to the P600.
5225 */
5226 msleep(500);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005227 }
5228 return 0;
5229}
5230
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005231static void init_driver_version(char *driver_version, int len)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005232{
5233 memset(driver_version, 0, len);
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06005234 strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005235}
5236
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005237static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005238{
5239 char *driver_version;
5240 int i, size = sizeof(cfgtable->driver_version);
5241
5242 driver_version = kmalloc(size, GFP_KERNEL);
5243 if (!driver_version)
5244 return -ENOMEM;
5245
5246 init_driver_version(driver_version, size);
5247 for (i = 0; i < size; i++)
5248 writeb(driver_version[i], &cfgtable->driver_version[i]);
5249 kfree(driver_version);
5250 return 0;
5251}
5252
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005253static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
5254 unsigned char *driver_ver)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005255{
5256 int i;
5257
5258 for (i = 0; i < sizeof(cfgtable->driver_version); i++)
5259 driver_ver[i] = readb(&cfgtable->driver_version[i]);
5260}
5261
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005262static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005263{
5264
5265 char *driver_ver, *old_driver_ver;
5266 int rc, size = sizeof(cfgtable->driver_version);
5267
5268 old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
5269 if (!old_driver_ver)
5270 return -ENOMEM;
5271 driver_ver = old_driver_ver + size;
5272
5273 /* After a reset, the 32 bytes of "driver version" in the cfgtable
5274 * should have been changed, otherwise we know the reset failed.
5275 */
5276 init_driver_version(old_driver_ver, size);
5277 read_driver_ver_from_cfgtable(cfgtable, driver_ver);
5278 rc = !memcmp(driver_ver, old_driver_ver, size);
5279 kfree(old_driver_ver);
5280 return rc;
5281}
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005282/* This does a hard reset of the controller using PCI power management
5283 * states or the using the doorbell register.
5284 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005285static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev)
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005286{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005287 u64 cfg_offset;
5288 u32 cfg_base_addr;
5289 u64 cfg_base_addr_index;
5290 void __iomem *vaddr;
5291 unsigned long paddr;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005292 u32 misc_fw_support;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005293 int rc;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005294 struct CfgTable __iomem *cfgtable;
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005295 u32 use_doorbell;
Stephen M. Cameron18867652010-06-16 13:51:45 -05005296 u32 board_id;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005297 u16 command_register;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005298
5299 /* For controllers as old as the P600, this is very nearly
5300 * the same thing as
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005301 *
5302 * pci_save_state(pci_dev);
5303 * pci_set_power_state(pci_dev, PCI_D3hot);
5304 * pci_set_power_state(pci_dev, PCI_D0);
5305 * pci_restore_state(pci_dev);
5306 *
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005307 * For controllers newer than the P600, the pci power state
5308 * method of resetting doesn't work so we have another way
5309 * using the doorbell register.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005310 */
Stephen M. Cameron18867652010-06-16 13:51:45 -05005311
Stephen M. Cameron25c1e56a2011-01-06 14:48:18 -06005312 rc = hpsa_lookup_board_id(pdev, &board_id);
Stephen M. Cameron46380782011-05-03 15:00:01 -05005313 if (rc < 0 || !ctlr_is_resettable(board_id)) {
Stephen M. Cameron25c1e56a2011-01-06 14:48:18 -06005314 dev_warn(&pdev->dev, "Not resetting device.\n");
5315 return -ENODEV;
5316 }
Stephen M. Cameron46380782011-05-03 15:00:01 -05005317
5318 /* if controller is soft- but not hard resettable... */
5319 if (!ctlr_is_hard_resettable(board_id))
5320 return -ENOTSUPP; /* try soft reset later. */
Stephen M. Cameron18867652010-06-16 13:51:45 -05005321
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005322 /* Save the PCI command register */
5323 pci_read_config_word(pdev, 4, &command_register);
5324 /* Turn the board off. This is so that later pci_restore_state()
5325 * won't turn the board on before the rest of config space is ready.
5326 */
5327 pci_disable_device(pdev);
5328 pci_save_state(pdev);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005329
5330 /* find the first memory BAR, so we can find the cfg table */
5331 rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
5332 if (rc)
5333 return rc;
5334 vaddr = remap_pci_mem(paddr, 0x250);
5335 if (!vaddr)
5336 return -ENOMEM;
5337
5338 /* find cfgtable in order to check if reset via doorbell is supported */
5339 rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
5340 &cfg_base_addr_index, &cfg_offset);
5341 if (rc)
5342 goto unmap_vaddr;
5343 cfgtable = remap_pci_mem(pci_resource_start(pdev,
5344 cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
5345 if (!cfgtable) {
5346 rc = -ENOMEM;
5347 goto unmap_vaddr;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005348 }
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005349 rc = write_driver_ver_to_cfgtable(cfgtable);
5350 if (rc)
5351 goto unmap_vaddr;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005352
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005353 /* If reset via doorbell register is supported, use that.
5354 * There are two such methods. Favor the newest method.
5355 */
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005356 misc_fw_support = readl(&cfgtable->misc_fw_support);
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005357 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
5358 if (use_doorbell) {
5359 use_doorbell = DOORBELL_CTLR_RESET2;
5360 } else {
5361 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
5362 if (use_doorbell) {
Mike Millerfba63092011-10-13 11:44:06 -05005363 dev_warn(&pdev->dev, "Soft reset not supported. "
5364 "Firmware update is required.\n");
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005365 rc = -ENOTSUPP; /* try soft reset */
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005366 goto unmap_cfgtable;
5367 }
5368 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005369
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005370 rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
5371 if (rc)
5372 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005373
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005374 pci_restore_state(pdev);
5375 rc = pci_enable_device(pdev);
5376 if (rc) {
5377 dev_warn(&pdev->dev, "failed to enable device.\n");
5378 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005379 }
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005380 pci_write_config_word(pdev, 4, command_register);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005381
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005382 /* Some devices (notably the HP Smart Array 5i Controller)
5383 need a little pause here */
5384 msleep(HPSA_POST_RESET_PAUSE_MSECS);
5385
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005386 rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
5387 if (rc) {
5388 dev_warn(&pdev->dev,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005389 "failed waiting for board to become ready "
5390 "after hard reset\n");
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005391 goto unmap_cfgtable;
5392 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005393
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005394 rc = controller_reset_failed(vaddr);
5395 if (rc < 0)
5396 goto unmap_cfgtable;
5397 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005398 dev_warn(&pdev->dev, "Unable to successfully reset "
5399 "controller. Will try soft reset.\n");
5400 rc = -ENOTSUPP;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005401 } else {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005402 dev_info(&pdev->dev, "board ready after hard reset.\n");
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005403 }
5404
5405unmap_cfgtable:
5406 iounmap(cfgtable);
5407
5408unmap_vaddr:
5409 iounmap(vaddr);
5410 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005411}
5412
5413/*
5414 * We cannot read the structure directly, for portability we must use
5415 * the io functions.
5416 * This is for debug only.
5417 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005418static void print_cfg_table(struct device *dev, struct CfgTable *tb)
5419{
Stephen M. Cameron58f86652010-05-27 15:13:58 -05005420#ifdef HPSA_DEBUG
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005421 int i;
5422 char temp_name[17];
5423
5424 dev_info(dev, "Controller Configuration information\n");
5425 dev_info(dev, "------------------------------------\n");
5426 for (i = 0; i < 4; i++)
5427 temp_name[i] = readb(&(tb->Signature[i]));
5428 temp_name[4] = '\0';
5429 dev_info(dev, " Signature = %s\n", temp_name);
5430 dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
5431 dev_info(dev, " Transport methods supported = 0x%x\n",
5432 readl(&(tb->TransportSupport)));
5433 dev_info(dev, " Transport methods active = 0x%x\n",
5434 readl(&(tb->TransportActive)));
5435 dev_info(dev, " Requested transport Method = 0x%x\n",
5436 readl(&(tb->HostWrite.TransportRequest)));
5437 dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
5438 readl(&(tb->HostWrite.CoalIntDelay)));
5439 dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
5440 readl(&(tb->HostWrite.CoalIntCount)));
5441 dev_info(dev, " Max outstanding commands = 0x%d\n",
5442 readl(&(tb->CmdsOutMax)));
5443 dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
5444 for (i = 0; i < 16; i++)
5445 temp_name[i] = readb(&(tb->ServerName[i]));
5446 temp_name[16] = '\0';
5447 dev_info(dev, " Server Name = %s\n", temp_name);
5448 dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
5449 readl(&(tb->HeartBeat)));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005450#endif /* HPSA_DEBUG */
Stephen M. Cameron58f86652010-05-27 15:13:58 -05005451}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005452
5453static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
5454{
5455 int i, offset, mem_type, bar_type;
5456
5457 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
5458 return 0;
5459 offset = 0;
5460 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
5461 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
5462 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
5463 offset += 4;
5464 else {
5465 mem_type = pci_resource_flags(pdev, i) &
5466 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
5467 switch (mem_type) {
5468 case PCI_BASE_ADDRESS_MEM_TYPE_32:
5469 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
5470 offset += 4; /* 32 bit */
5471 break;
5472 case PCI_BASE_ADDRESS_MEM_TYPE_64:
5473 offset += 8;
5474 break;
5475 default: /* reserved in PCI 2.2 */
5476 dev_warn(&pdev->dev,
5477 "base address is invalid\n");
5478 return -1;
5479 break;
5480 }
5481 }
5482 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
5483 return i + 1;
5484 }
5485 return -1;
5486}
5487
5488/* If MSI/MSI-X is supported by the kernel we will try to enable it on
5489 * controllers that are capable. If not, we use IO-APIC mode.
5490 */
5491
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005492static void hpsa_interrupt_mode(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005493{
5494#ifdef CONFIG_PCI_MSI
Matt Gates254f7962012-05-01 11:43:06 -05005495 int err, i;
5496 struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
5497
5498 for (i = 0; i < MAX_REPLY_QUEUES; i++) {
5499 hpsa_msix_entries[i].vector = 0;
5500 hpsa_msix_entries[i].entry = i;
5501 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005502
5503 /* Some boards advertise MSI but don't really support it */
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05005504 if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
5505 (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005506 goto default_int_mode;
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005507 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
5508 dev_info(&h->pdev->dev, "MSIX\n");
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005509 h->msix_vector = MAX_REPLY_QUEUES;
Matt Gates254f7962012-05-01 11:43:06 -05005510 err = pci_enable_msix(h->pdev, hpsa_msix_entries,
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005511 h->msix_vector);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005512 if (err > 0) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005513 dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005514 "available\n", err);
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005515 h->msix_vector = err;
5516 err = pci_enable_msix(h->pdev, hpsa_msix_entries,
5517 h->msix_vector);
5518 }
5519 if (!err) {
5520 for (i = 0; i < h->msix_vector; i++)
5521 h->intr[i] = hpsa_msix_entries[i].vector;
5522 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005523 } else {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005524 dev_warn(&h->pdev->dev, "MSI-X init failed %d\n",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005525 err);
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005526 h->msix_vector = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005527 goto default_int_mode;
5528 }
5529 }
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005530 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
5531 dev_info(&h->pdev->dev, "MSI\n");
5532 if (!pci_enable_msi(h->pdev))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005533 h->msi_vector = 1;
5534 else
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005535 dev_warn(&h->pdev->dev, "MSI init failed\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005536 }
5537default_int_mode:
5538#endif /* CONFIG_PCI_MSI */
5539 /* if we get here we're going to use the default interrupt mode */
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005540 h->intr[h->intr_mode] = h->pdev->irq;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005541}
5542
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005543static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05005544{
5545 int i;
5546 u32 subsystem_vendor_id, subsystem_device_id;
5547
5548 subsystem_vendor_id = pdev->subsystem_vendor;
5549 subsystem_device_id = pdev->subsystem_device;
5550 *board_id = ((subsystem_device_id << 16) & 0xffff0000) |
5551 subsystem_vendor_id;
5552
5553 for (i = 0; i < ARRAY_SIZE(products); i++)
5554 if (*board_id == products[i].board_id)
5555 return i;
5556
Stephen M. Cameron6798cc02010-06-16 13:51:20 -05005557 if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
5558 subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
5559 !hpsa_allow_any) {
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05005560 dev_warn(&pdev->dev, "unrecognized board ID: "
5561 "0x%08x, ignoring.\n", *board_id);
5562 return -ENODEV;
5563 }
5564 return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
5565}
5566
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005567static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
5568 unsigned long *memory_bar)
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005569{
5570 int i;
5571
5572 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05005573 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005574 /* addressing mode bits already removed */
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05005575 *memory_bar = pci_resource_start(pdev, i);
5576 dev_dbg(&pdev->dev, "memory BAR = %lx\n",
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005577 *memory_bar);
5578 return 0;
5579 }
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05005580 dev_warn(&pdev->dev, "no memory BAR found\n");
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005581 return -ENODEV;
5582}
5583
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005584static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
5585 int wait_for_ready)
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005586{
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005587 int i, iterations;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005588 u32 scratchpad;
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005589 if (wait_for_ready)
5590 iterations = HPSA_BOARD_READY_ITERATIONS;
5591 else
5592 iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005593
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005594 for (i = 0; i < iterations; i++) {
5595 scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
5596 if (wait_for_ready) {
5597 if (scratchpad == HPSA_FIRMWARE_READY)
5598 return 0;
5599 } else {
5600 if (scratchpad != HPSA_FIRMWARE_READY)
5601 return 0;
5602 }
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005603 msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
5604 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005605 dev_warn(&pdev->dev, "board not ready, timed out.\n");
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005606 return -ENODEV;
5607}
5608
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005609static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
5610 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
5611 u64 *cfg_offset)
Stephen M. Camerona51fd472010-06-16 13:51:30 -05005612{
5613 *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
5614 *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
5615 *cfg_base_addr &= (u32) 0x0000ffff;
5616 *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
5617 if (*cfg_base_addr_index == -1) {
5618 dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
5619 return -ENODEV;
5620 }
5621 return 0;
5622}
5623
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005624static int hpsa_find_cfgtables(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005625{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06005626 u64 cfg_offset;
5627 u32 cfg_base_addr;
5628 u64 cfg_base_addr_index;
Don Brace303932f2010-02-04 08:42:40 -06005629 u32 trans_offset;
Stephen M. Camerona51fd472010-06-16 13:51:30 -05005630 int rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005631
Stephen M. Camerona51fd472010-06-16 13:51:30 -05005632 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
5633 &cfg_base_addr_index, &cfg_offset);
5634 if (rc)
5635 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005636 h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
Stephen M. Camerona51fd472010-06-16 13:51:30 -05005637 cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005638 if (!h->cfgtable)
5639 return -ENOMEM;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005640 rc = write_driver_ver_to_cfgtable(h->cfgtable);
5641 if (rc)
5642 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005643 /* Find performant mode table. */
Stephen M. Camerona51fd472010-06-16 13:51:30 -05005644 trans_offset = readl(&h->cfgtable->TransMethodOffset);
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005645 h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
5646 cfg_base_addr_index)+cfg_offset+trans_offset,
5647 sizeof(*h->transtable));
5648 if (!h->transtable)
5649 return -ENOMEM;
5650 return 0;
5651}
5652
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005653static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05005654{
5655 h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
Stephen M. Cameron72ceeae2011-01-06 14:48:13 -06005656
5657 /* Limit commands in memory limited kdump scenario. */
5658 if (reset_devices && h->max_commands > 32)
5659 h->max_commands = 32;
5660
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05005661 if (h->max_commands < 16) {
5662 dev_warn(&h->pdev->dev, "Controller reports "
5663 "max supported commands of %d, an obvious lie. "
5664 "Using 16. Ensure that firmware is up to date.\n",
5665 h->max_commands);
5666 h->max_commands = 16;
5667 }
5668}
5669
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005670/* Interrogate the hardware for some limits:
5671 * max commands, max SG elements without chaining, and with chaining,
5672 * SG chain block size, etc.
5673 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005674static void hpsa_find_board_params(struct ctlr_info *h)
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005675{
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05005676 hpsa_get_max_perf_mode_cmds(h);
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005677 h->nr_cmds = h->max_commands - 4; /* Allow room for some ioctls */
5678 h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005679 h->fw_support = readl(&(h->cfgtable->misc_fw_support));
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005680 /*
5681 * Limit in-command s/g elements to 32 save dma'able memory.
5682 * Howvever spec says if 0, use 31
5683 */
5684 h->max_cmd_sg_entries = 31;
5685 if (h->maxsgentries > 512) {
5686 h->max_cmd_sg_entries = 32;
5687 h->chainsize = h->maxsgentries - h->max_cmd_sg_entries + 1;
5688 h->maxsgentries--; /* save one for chain pointer */
5689 } else {
5690 h->maxsgentries = 31; /* default to traditional values */
5691 h->chainsize = 0;
5692 }
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005693
5694 /* Find out what task management functions are supported and cache */
5695 h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
Scott Teel0e7a7fc2014-02-18 13:55:59 -06005696 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
5697 dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
5698 if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
5699 dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005700}
5701
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05005702static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
5703{
Akinobu Mita0fc9fd42012-04-04 22:14:59 +09005704 if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05005705 dev_warn(&h->pdev->dev, "not a valid CISS config table\n");
5706 return false;
5707 }
5708 return true;
5709}
5710
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005711static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005712{
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005713 u32 driver_support;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005714
Stephen M. Cameron28e13442013-12-04 17:10:21 -06005715#ifdef CONFIG_X86
5716 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005717 driver_support = readl(&(h->cfgtable->driver_support));
5718 driver_support |= ENABLE_SCSI_PREFETCH;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005719#endif
Stephen M. Cameron28e13442013-12-04 17:10:21 -06005720 driver_support |= ENABLE_UNIT_ATTN;
5721 writel(driver_support, &(h->cfgtable->driver_support));
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005722}
5723
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05005724/* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result
5725 * in a prefetch beyond physical memory.
5726 */
5727static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
5728{
5729 u32 dma_prefetch;
5730
5731 if (h->board_id != 0x3225103C)
5732 return;
5733 dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
5734 dma_prefetch |= 0x8000;
5735 writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
5736}
5737
Stephen M. Cameron76438d02014-02-18 13:55:43 -06005738static void hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
5739{
5740 int i;
5741 u32 doorbell_value;
5742 unsigned long flags;
5743 /* wait until the clear_event_notify bit 6 is cleared by controller. */
5744 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
5745 spin_lock_irqsave(&h->lock, flags);
5746 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
5747 spin_unlock_irqrestore(&h->lock, flags);
5748 if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
5749 break;
5750 /* delay and try again */
5751 msleep(20);
5752 }
5753}
5754
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005755static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005756{
5757 int i;
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06005758 u32 doorbell_value;
5759 unsigned long flags;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005760
5761 /* under certain very rare conditions, this can take awhile.
5762 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
5763 * as we enter this code.)
5764 */
5765 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06005766 spin_lock_irqsave(&h->lock, flags);
5767 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
5768 spin_unlock_irqrestore(&h->lock, flags);
Dan Carpenter382be662011-02-15 15:33:13 -06005769 if (!(doorbell_value & CFGTBL_ChangeReq))
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005770 break;
5771 /* delay and try again */
Stephen M. Cameron60d3f5b2011-01-06 14:48:34 -06005772 usleep_range(10000, 20000);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005773 }
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005774}
5775
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005776static int hpsa_enter_simple_mode(struct ctlr_info *h)
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005777{
5778 u32 trans_support;
5779
5780 trans_support = readl(&(h->cfgtable->TransportSupport));
5781 if (!(trans_support & SIMPLE_MODE))
5782 return -ENOTSUPP;
5783
5784 h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005785
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005786 /* Update the field, and then ring the doorbell */
5787 writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06005788 writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005789 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
5790 hpsa_wait_for_mode_change_ack(h);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005791 print_cfg_table(&h->pdev->dev, h->cfgtable);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005792 if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
5793 goto error;
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06005794 h->transMethod = CFGTBL_Trans_Simple;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005795 return 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005796error:
5797 dev_warn(&h->pdev->dev, "unable to get board into simple mode\n");
5798 return -ENODEV;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005799}
5800
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005801static int hpsa_pci_init(struct ctlr_info *h)
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005802{
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005803 int prod_index, err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005804
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05005805 prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
5806 if (prod_index < 0)
5807 return -ENODEV;
5808 h->product_name = products[prod_index].product_name;
5809 h->access = *(products[prod_index].access);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005810
Matthew Garrette5a44df2011-11-11 11:14:23 -05005811 pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
5812 PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
5813
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005814 err = pci_enable_device(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005815 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005816 dev_warn(&h->pdev->dev, "unable to enable PCI device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005817 return err;
5818 }
5819
Stephen M. Cameron5cb460a2012-05-01 11:42:20 -05005820 /* Enable bus mastering (pci_disable_device may disable this) */
5821 pci_set_master(h->pdev);
5822
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06005823 err = pci_request_regions(h->pdev, HPSA);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005824 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005825 dev_err(&h->pdev->dev,
5826 "cannot obtain PCI resources, aborting\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005827 return err;
5828 }
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05005829 hpsa_interrupt_mode(h);
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05005830 err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005831 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005832 goto err_out_free_res;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005833 h->vaddr = remap_pci_mem(h->paddr, 0x250);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05005834 if (!h->vaddr) {
5835 err = -ENOMEM;
5836 goto err_out_free_res;
5837 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005838 err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005839 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005840 goto err_out_free_res;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005841 err = hpsa_find_cfgtables(h);
5842 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005843 goto err_out_free_res;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005844 hpsa_find_board_params(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005845
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05005846 if (!hpsa_CISS_signature_present(h)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005847 err = -ENODEV;
5848 goto err_out_free_res;
5849 }
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005850 hpsa_set_driver_support_bits(h);
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05005851 hpsa_p600_dma_prefetch_quirk(h);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005852 err = hpsa_enter_simple_mode(h);
5853 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005854 goto err_out_free_res;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005855 return 0;
5856
5857err_out_free_res:
Stephen M. Cameron204892e2010-05-27 15:13:22 -05005858 if (h->transtable)
5859 iounmap(h->transtable);
5860 if (h->cfgtable)
5861 iounmap(h->cfgtable);
5862 if (h->vaddr)
5863 iounmap(h->vaddr);
Stephen M. Cameronf0bd0b682012-05-01 11:42:09 -05005864 pci_disable_device(h->pdev);
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005865 pci_release_regions(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005866 return err;
5867}
5868
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005869static void hpsa_hba_inquiry(struct ctlr_info *h)
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06005870{
5871 int rc;
5872
5873#define HBA_INQUIRY_BYTE_COUNT 64
5874 h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
5875 if (!h->hba_inquiry_data)
5876 return;
5877 rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
5878 h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
5879 if (rc != 0) {
5880 kfree(h->hba_inquiry_data);
5881 h->hba_inquiry_data = NULL;
5882 }
5883}
5884
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005885static int hpsa_init_reset_devices(struct pci_dev *pdev)
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005886{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005887 int rc, i;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005888
5889 if (!reset_devices)
5890 return 0;
5891
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005892 /* Reset the controller with a PCI power-cycle or via doorbell */
5893 rc = hpsa_kdump_hard_reset_controller(pdev);
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005894
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005895 /* -ENOTSUPP here means we cannot reset the controller
5896 * but it's already (and still) up and running in
Stephen M. Cameron18867652010-06-16 13:51:45 -05005897 * "performant mode". Or, it might be 640x, which can't reset
5898 * due to concerns about shared bbwc between 6402/6404 pair.
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005899 */
5900 if (rc == -ENOTSUPP)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005901 return rc; /* just try to do the kdump anyhow. */
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005902 if (rc)
5903 return -ENODEV;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005904
5905 /* Now try to get the controller to respond to a no-op */
Stephen M. Cameron2b870cb2011-05-03 14:59:36 -05005906 dev_warn(&pdev->dev, "Waiting for controller to respond to no-op\n");
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005907 for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
5908 if (hpsa_noop(pdev) == 0)
5909 break;
5910 else
5911 dev_warn(&pdev->dev, "no-op failed%s\n",
5912 (i < 11 ? "; re-trying" : ""));
5913 }
5914 return 0;
5915}
5916
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005917static int hpsa_allocate_cmd_pool(struct ctlr_info *h)
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05005918{
5919 h->cmd_pool_bits = kzalloc(
5920 DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
5921 sizeof(unsigned long), GFP_KERNEL);
5922 h->cmd_pool = pci_alloc_consistent(h->pdev,
5923 h->nr_cmds * sizeof(*h->cmd_pool),
5924 &(h->cmd_pool_dhandle));
5925 h->errinfo_pool = pci_alloc_consistent(h->pdev,
5926 h->nr_cmds * sizeof(*h->errinfo_pool),
5927 &(h->errinfo_pool_dhandle));
5928 if ((h->cmd_pool_bits == NULL)
5929 || (h->cmd_pool == NULL)
5930 || (h->errinfo_pool == NULL)) {
5931 dev_err(&h->pdev->dev, "out of memory in %s", __func__);
5932 return -ENOMEM;
5933 }
5934 return 0;
5935}
5936
5937static void hpsa_free_cmd_pool(struct ctlr_info *h)
5938{
5939 kfree(h->cmd_pool_bits);
5940 if (h->cmd_pool)
5941 pci_free_consistent(h->pdev,
5942 h->nr_cmds * sizeof(struct CommandList),
5943 h->cmd_pool, h->cmd_pool_dhandle);
Stephen M. Cameronaca90122014-02-18 13:56:14 -06005944 if (h->ioaccel2_cmd_pool)
5945 pci_free_consistent(h->pdev,
5946 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
5947 h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05005948 if (h->errinfo_pool)
5949 pci_free_consistent(h->pdev,
5950 h->nr_cmds * sizeof(struct ErrorInfo),
5951 h->errinfo_pool,
5952 h->errinfo_pool_dhandle);
Matt Gatese1f7de02014-02-18 13:55:17 -06005953 if (h->ioaccel_cmd_pool)
5954 pci_free_consistent(h->pdev,
5955 h->nr_cmds * sizeof(struct io_accel1_cmd),
5956 h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05005957}
5958
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05005959static int hpsa_request_irq(struct ctlr_info *h,
5960 irqreturn_t (*msixhandler)(int, void *),
5961 irqreturn_t (*intxhandler)(int, void *))
5962{
Matt Gates254f7962012-05-01 11:43:06 -05005963 int rc, i;
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05005964
Matt Gates254f7962012-05-01 11:43:06 -05005965 /*
5966 * initialize h->q[x] = x so that interrupt handlers know which
5967 * queue to process.
5968 */
5969 for (i = 0; i < MAX_REPLY_QUEUES; i++)
5970 h->q[i] = (u8) i;
5971
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005972 if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
Matt Gates254f7962012-05-01 11:43:06 -05005973 /* If performant mode and MSI-X, use multiple reply queues */
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005974 for (i = 0; i < h->msix_vector; i++)
Matt Gates254f7962012-05-01 11:43:06 -05005975 rc = request_irq(h->intr[i], msixhandler,
5976 0, h->devname,
5977 &h->q[i]);
5978 } else {
5979 /* Use single reply pool */
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005980 if (h->msix_vector > 0 || h->msi_vector) {
Matt Gates254f7962012-05-01 11:43:06 -05005981 rc = request_irq(h->intr[h->intr_mode],
5982 msixhandler, 0, h->devname,
5983 &h->q[h->intr_mode]);
5984 } else {
5985 rc = request_irq(h->intr[h->intr_mode],
5986 intxhandler, IRQF_SHARED, h->devname,
5987 &h->q[h->intr_mode]);
5988 }
5989 }
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05005990 if (rc) {
5991 dev_err(&h->pdev->dev, "unable to get irq %d for %s\n",
5992 h->intr[h->intr_mode], h->devname);
5993 return -ENODEV;
5994 }
5995 return 0;
5996}
5997
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005998static int hpsa_kdump_soft_reset(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005999{
6000 if (hpsa_send_host_reset(h, RAID_CTLR_LUNID,
6001 HPSA_RESET_TYPE_CONTROLLER)) {
6002 dev_warn(&h->pdev->dev, "Resetting array controller failed.\n");
6003 return -EIO;
6004 }
6005
6006 dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
6007 if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) {
6008 dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
6009 return -1;
6010 }
6011
6012 dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
6013 if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) {
6014 dev_warn(&h->pdev->dev, "Board failed to become ready "
6015 "after soft reset.\n");
6016 return -1;
6017 }
6018
6019 return 0;
6020}
6021
Matt Gates254f7962012-05-01 11:43:06 -05006022static void free_irqs(struct ctlr_info *h)
6023{
6024 int i;
6025
6026 if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
6027 /* Single reply queue, only one irq to free */
6028 i = h->intr_mode;
6029 free_irq(h->intr[i], &h->q[i]);
6030 return;
6031 }
6032
Hannes Reineckeeee0f032014-01-15 13:30:53 +01006033 for (i = 0; i < h->msix_vector; i++)
Matt Gates254f7962012-05-01 11:43:06 -05006034 free_irq(h->intr[i], &h->q[i]);
6035}
6036
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05006037static void hpsa_free_irqs_and_disable_msix(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006038{
Matt Gates254f7962012-05-01 11:43:06 -05006039 free_irqs(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006040#ifdef CONFIG_PCI_MSI
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05006041 if (h->msix_vector) {
6042 if (h->pdev->msix_enabled)
6043 pci_disable_msix(h->pdev);
6044 } else if (h->msi_vector) {
6045 if (h->pdev->msi_enabled)
6046 pci_disable_msi(h->pdev);
6047 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006048#endif /* CONFIG_PCI_MSI */
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05006049}
6050
6051static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
6052{
6053 hpsa_free_irqs_and_disable_msix(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006054 hpsa_free_sg_chain_blocks(h);
6055 hpsa_free_cmd_pool(h);
Matt Gatese1f7de02014-02-18 13:55:17 -06006056 kfree(h->ioaccel1_blockFetchTable);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006057 kfree(h->blockFetchTable);
6058 pci_free_consistent(h->pdev, h->reply_pool_size,
6059 h->reply_pool, h->reply_pool_dhandle);
6060 if (h->vaddr)
6061 iounmap(h->vaddr);
6062 if (h->transtable)
6063 iounmap(h->transtable);
6064 if (h->cfgtable)
6065 iounmap(h->cfgtable);
6066 pci_release_regions(h->pdev);
6067 kfree(h);
6068}
6069
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006070/* Called when controller lockup detected. */
6071static void fail_all_cmds_on_list(struct ctlr_info *h, struct list_head *list)
6072{
6073 struct CommandList *c = NULL;
6074
6075 assert_spin_locked(&h->lock);
6076 /* Mark all outstanding commands as failed and complete them. */
6077 while (!list_empty(list)) {
6078 c = list_entry(list->next, struct CommandList, list);
6079 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05006080 finish_cmd(c);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006081 }
6082}
6083
6084static void controller_lockup_detected(struct ctlr_info *h)
6085{
6086 unsigned long flags;
6087
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006088 h->access.set_intr_mask(h, HPSA_INTR_OFF);
6089 spin_lock_irqsave(&h->lock, flags);
6090 h->lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
6091 spin_unlock_irqrestore(&h->lock, flags);
6092 dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x\n",
6093 h->lockup_detected);
6094 pci_disable_device(h->pdev);
6095 spin_lock_irqsave(&h->lock, flags);
6096 fail_all_cmds_on_list(h, &h->cmpQ);
6097 fail_all_cmds_on_list(h, &h->reqQ);
6098 spin_unlock_irqrestore(&h->lock, flags);
6099}
6100
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006101static void detect_controller_lockup(struct ctlr_info *h)
6102{
6103 u64 now;
6104 u32 heartbeat;
6105 unsigned long flags;
6106
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006107 now = get_jiffies_64();
6108 /* If we've received an interrupt recently, we're ok. */
6109 if (time_after64(h->last_intr_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05006110 (h->heartbeat_sample_interval), now))
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006111 return;
6112
6113 /*
6114 * If we've already checked the heartbeat recently, we're ok.
6115 * This could happen if someone sends us a signal. We
6116 * otherwise don't care about signals in this thread.
6117 */
6118 if (time_after64(h->last_heartbeat_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05006119 (h->heartbeat_sample_interval), now))
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006120 return;
6121
6122 /* If heartbeat has not changed since we last looked, we're not ok. */
6123 spin_lock_irqsave(&h->lock, flags);
6124 heartbeat = readl(&h->cfgtable->HeartBeat);
6125 spin_unlock_irqrestore(&h->lock, flags);
6126 if (h->last_heartbeat == heartbeat) {
6127 controller_lockup_detected(h);
6128 return;
6129 }
6130
6131 /* We're ok. */
6132 h->last_heartbeat = heartbeat;
6133 h->last_heartbeat_timestamp = now;
6134}
6135
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006136static int hpsa_kickoff_rescan(struct ctlr_info *h)
6137{
6138 int i;
6139 char *event_type;
6140
6141 /* Ask the controller to clear the events we're handling. */
Stephen M. Cameron1f7cee82014-02-18 13:56:09 -06006142 if ((h->transMethod & (CFGTBL_Trans_io_accel1
6143 | CFGTBL_Trans_io_accel2)) &&
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006144 (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
6145 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
6146
6147 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
6148 event_type = "state change";
6149 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
6150 event_type = "configuration change";
6151 /* Stop sending new RAID offload reqs via the IO accelerator */
6152 scsi_block_requests(h->scsi_host);
6153 for (i = 0; i < h->ndevices; i++)
6154 h->dev[i]->offload_enabled = 0;
6155 hpsa_drain_commands(h);
6156 /* Set 'accelerator path config change' bit */
6157 dev_warn(&h->pdev->dev,
6158 "Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
6159 h->events, event_type);
6160 writel(h->events, &(h->cfgtable->clear_event_notify));
6161 /* Set the "clear event notify field update" bit 6 */
6162 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
6163 /* Wait until ctlr clears 'clear event notify field', bit 6 */
6164 hpsa_wait_for_clear_event_notify_ack(h);
6165 scsi_unblock_requests(h->scsi_host);
6166 } else {
6167 /* Acknowledge controller notification events. */
6168 writel(h->events, &(h->cfgtable->clear_event_notify));
6169 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
6170 hpsa_wait_for_clear_event_notify_ack(h);
6171#if 0
6172 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
6173 hpsa_wait_for_mode_change_ack(h);
6174#endif
6175 }
6176
6177 /* Something in the device list may have changed to trigger
6178 * the event, so do a rescan.
6179 */
6180 hpsa_scan_start(h->scsi_host);
6181 /* release reference taken on scsi host in check_controller_events */
6182 scsi_host_put(h->scsi_host);
6183 return 0;
6184}
6185
6186/* Check a register on the controller to see if there are configuration
6187 * changes (added/changed/removed logical drives, etc.) which mean that
6188 * we should rescan the controller for devices. If so, add the controller
6189 * to the list of controllers needing to be rescanned, and gets a
6190 * reference to the associated scsi_host.
6191 */
6192static void hpsa_ctlr_needs_rescan(struct ctlr_info *h)
6193{
6194 if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
6195 return;
6196
6197 h->events = readl(&(h->cfgtable->event_notify));
6198 if (!h->events)
6199 return;
6200
6201 /*
6202 * Take a reference on scsi host for the duration of the scan
6203 * Release in hpsa_kickoff_rescan(). No lock needed for scan_list
6204 * as only a single thread accesses this list.
6205 */
6206 scsi_host_get(h->scsi_host);
6207 hpsa_kickoff_rescan(h);
6208}
6209
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006210static void hpsa_monitor_ctlr_worker(struct work_struct *work)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006211{
6212 unsigned long flags;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006213 struct ctlr_info *h = container_of(to_delayed_work(work),
6214 struct ctlr_info, monitor_ctlr_work);
6215 detect_controller_lockup(h);
6216 if (h->lockup_detected)
6217 return;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006218 hpsa_ctlr_needs_rescan(h);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006219 spin_lock_irqsave(&h->lock, flags);
6220 if (h->remove_in_progress) {
6221 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006222 return;
6223 }
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006224 schedule_delayed_work(&h->monitor_ctlr_work,
6225 h->heartbeat_sample_interval);
6226 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006227}
6228
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006229static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006230{
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006231 int dac, rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006232 struct ctlr_info *h;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006233 int try_soft_reset = 0;
6234 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006235
6236 if (number_of_controllers == 0)
6237 printk(KERN_INFO DRIVER_NAME "\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006238
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006239 rc = hpsa_init_reset_devices(pdev);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006240 if (rc) {
6241 if (rc != -ENOTSUPP)
6242 return rc;
6243 /* If the reset fails in a particular way (it has no way to do
6244 * a proper hard reset, so returns -ENOTSUPP) we can try to do
6245 * a soft reset once we get the controller configured up to the
6246 * point that it can accept a command.
6247 */
6248 try_soft_reset = 1;
6249 rc = 0;
6250 }
6251
6252reinit_after_soft_reset:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006253
Don Brace303932f2010-02-04 08:42:40 -06006254 /* Command structures must be aligned on a 32-byte boundary because
6255 * the 5 lower bits of the address are used by the hardware. and by
6256 * the driver. See comments in hpsa.h for more info.
6257 */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006258#define COMMANDLIST_ALIGNMENT 128
Don Brace303932f2010-02-04 08:42:40 -06006259 BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006260 h = kzalloc(sizeof(*h), GFP_KERNEL);
6261 if (!h)
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006262 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006263
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05006264 h->pdev = pdev;
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06006265 h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06006266 INIT_LIST_HEAD(&h->cmpQ);
6267 INIT_LIST_HEAD(&h->reqQ);
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06006268 spin_lock_init(&h->lock);
6269 spin_lock_init(&h->scan_lock);
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006270 spin_lock_init(&h->passthru_count_lock);
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05006271 rc = hpsa_pci_init(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006272 if (rc != 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006273 goto clean1;
6274
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06006275 sprintf(h->devname, HPSA "%d", number_of_controllers);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006276 h->ctlr = number_of_controllers;
6277 number_of_controllers++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006278
6279 /* configure PCI DMA stuff */
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006280 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
6281 if (rc == 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006282 dac = 1;
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006283 } else {
6284 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6285 if (rc == 0) {
6286 dac = 0;
6287 } else {
6288 dev_err(&pdev->dev, "no suitable DMA available\n");
6289 goto clean1;
6290 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006291 }
6292
6293 /* make sure the board interrupts are off */
6294 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006295
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05006296 if (hpsa_request_irq(h, do_hpsa_intr_msi, do_hpsa_intr_intx))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006297 goto clean2;
Don Brace303932f2010-02-04 08:42:40 -06006298 dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
6299 h->devname, pdev->device,
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06006300 h->intr[h->intr_mode], dac ? "" : " not");
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05006301 if (hpsa_allocate_cmd_pool(h))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006302 goto clean4;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06006303 if (hpsa_allocate_sg_chain_blocks(h))
6304 goto clean4;
Stephen M. Camerona08a84712010-02-04 08:43:16 -06006305 init_waitqueue_head(&h->scan_wait_queue);
6306 h->scan_finished = 1; /* no scan currently in progress */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006307
6308 pci_set_drvdata(pdev, h);
Stephen M. Cameron9a413382011-05-03 14:59:41 -05006309 h->ndevices = 0;
6310 h->scsi_host = NULL;
6311 spin_lock_init(&h->devlock);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006312 hpsa_put_ctlr_into_performant_mode(h);
6313
6314 /* At this point, the controller is ready to take commands.
6315 * Now, if reset_devices and the hard reset didn't work, try
6316 * the soft reset and see if that works.
6317 */
6318 if (try_soft_reset) {
6319
6320 /* This is kind of gross. We may or may not get a completion
6321 * from the soft reset command, and if we do, then the value
6322 * from the fifo may or may not be valid. So, we wait 10 secs
6323 * after the reset throwing away any completions we get during
6324 * that time. Unregister the interrupt handler and register
6325 * fake ones to scoop up any residual completions.
6326 */
6327 spin_lock_irqsave(&h->lock, flags);
6328 h->access.set_intr_mask(h, HPSA_INTR_OFF);
6329 spin_unlock_irqrestore(&h->lock, flags);
Matt Gates254f7962012-05-01 11:43:06 -05006330 free_irqs(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006331 rc = hpsa_request_irq(h, hpsa_msix_discard_completions,
6332 hpsa_intx_discard_completions);
6333 if (rc) {
6334 dev_warn(&h->pdev->dev, "Failed to request_irq after "
6335 "soft reset.\n");
6336 goto clean4;
6337 }
6338
6339 rc = hpsa_kdump_soft_reset(h);
6340 if (rc)
6341 /* Neither hard nor soft reset worked, we're hosed. */
6342 goto clean4;
6343
6344 dev_info(&h->pdev->dev, "Board READY.\n");
6345 dev_info(&h->pdev->dev,
6346 "Waiting for stale completions to drain.\n");
6347 h->access.set_intr_mask(h, HPSA_INTR_ON);
6348 msleep(10000);
6349 h->access.set_intr_mask(h, HPSA_INTR_OFF);
6350
6351 rc = controller_reset_failed(h->cfgtable);
6352 if (rc)
6353 dev_info(&h->pdev->dev,
6354 "Soft reset appears to have failed.\n");
6355
6356 /* since the controller's reset, we have to go back and re-init
6357 * everything. Easiest to just forget what we've done and do it
6358 * all over again.
6359 */
6360 hpsa_undo_allocations_after_kdump_soft_reset(h);
6361 try_soft_reset = 0;
6362 if (rc)
6363 /* don't go to clean4, we already unallocated */
6364 return -ENODEV;
6365
6366 goto reinit_after_soft_reset;
6367 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006368
Scott Teelda0697b2014-02-18 13:57:00 -06006369 /* Enable Accelerated IO path at driver layer */
6370 h->acciopath_status = 1;
6371
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006372 /* Turn the interrupts on so we can service requests */
6373 h->access.set_intr_mask(h, HPSA_INTR_ON);
6374
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06006375 hpsa_hba_inquiry(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006376 hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006377
6378 /* Monitor the controller for firmware lockups */
6379 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
6380 INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
6381 schedule_delayed_work(&h->monitor_ctlr_work,
6382 h->heartbeat_sample_interval);
Stephen M. Cameron88bf6d62013-11-01 11:02:25 -05006383 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006384
6385clean4:
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06006386 hpsa_free_sg_chain_blocks(h);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05006387 hpsa_free_cmd_pool(h);
Matt Gates254f7962012-05-01 11:43:06 -05006388 free_irqs(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006389clean2:
6390clean1:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006391 kfree(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006392 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006393}
6394
6395static void hpsa_flush_cache(struct ctlr_info *h)
6396{
6397 char *flush_buf;
6398 struct CommandList *c;
Stephen M. Cameron702890e2013-09-23 13:33:30 -05006399 unsigned long flags;
6400
6401 /* Don't bother trying to flush the cache if locked up */
6402 spin_lock_irqsave(&h->lock, flags);
6403 if (unlikely(h->lockup_detected)) {
6404 spin_unlock_irqrestore(&h->lock, flags);
6405 return;
6406 }
6407 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006408
6409 flush_buf = kzalloc(4, GFP_KERNEL);
6410 if (!flush_buf)
6411 return;
6412
6413 c = cmd_special_alloc(h);
6414 if (!c) {
6415 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
6416 goto out_of_memory;
6417 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006418 if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
6419 RAID_CTLR_LUNID, TYPE_CMD)) {
6420 goto out;
6421 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006422 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
6423 if (c->err_info->CommandStatus != 0)
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006424out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006425 dev_warn(&h->pdev->dev,
6426 "error flushing cache on controller\n");
6427 cmd_special_free(h, c);
6428out_of_memory:
6429 kfree(flush_buf);
6430}
6431
6432static void hpsa_shutdown(struct pci_dev *pdev)
6433{
6434 struct ctlr_info *h;
6435
6436 h = pci_get_drvdata(pdev);
6437 /* Turn board interrupts off and send the flush cache command
6438 * sendcmd will turn off interrupt, and send the flush...
6439 * To write all data in the battery backed cache to disks
6440 */
6441 hpsa_flush_cache(h);
6442 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05006443 hpsa_free_irqs_and_disable_msix(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006444}
6445
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006446static void hpsa_free_device_info(struct ctlr_info *h)
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06006447{
6448 int i;
6449
6450 for (i = 0; i < h->ndevices; i++)
6451 kfree(h->dev[i]);
6452}
6453
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006454static void hpsa_remove_one(struct pci_dev *pdev)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006455{
6456 struct ctlr_info *h;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006457 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006458
6459 if (pci_get_drvdata(pdev) == NULL) {
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006460 dev_err(&pdev->dev, "unable to remove device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006461 return;
6462 }
6463 h = pci_get_drvdata(pdev);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006464
6465 /* Get rid of any controller monitoring work items */
6466 spin_lock_irqsave(&h->lock, flags);
6467 h->remove_in_progress = 1;
6468 cancel_delayed_work(&h->monitor_ctlr_work);
6469 spin_unlock_irqrestore(&h->lock, flags);
6470
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006471 hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */
6472 hpsa_shutdown(pdev);
6473 iounmap(h->vaddr);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05006474 iounmap(h->transtable);
6475 iounmap(h->cfgtable);
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06006476 hpsa_free_device_info(h);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06006477 hpsa_free_sg_chain_blocks(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006478 pci_free_consistent(h->pdev,
6479 h->nr_cmds * sizeof(struct CommandList),
6480 h->cmd_pool, h->cmd_pool_dhandle);
6481 pci_free_consistent(h->pdev,
6482 h->nr_cmds * sizeof(struct ErrorInfo),
6483 h->errinfo_pool, h->errinfo_pool_dhandle);
Don Brace303932f2010-02-04 08:42:40 -06006484 pci_free_consistent(h->pdev, h->reply_pool_size,
6485 h->reply_pool, h->reply_pool_dhandle);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006486 kfree(h->cmd_pool_bits);
Don Brace303932f2010-02-04 08:42:40 -06006487 kfree(h->blockFetchTable);
Matt Gatese1f7de02014-02-18 13:55:17 -06006488 kfree(h->ioaccel1_blockFetchTable);
Stephen M. Cameronaca90122014-02-18 13:56:14 -06006489 kfree(h->ioaccel2_blockFetchTable);
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06006490 kfree(h->hba_inquiry_data);
Stephen M. Cameronf0bd0b682012-05-01 11:42:09 -05006491 pci_disable_device(pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006492 pci_release_regions(pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006493 kfree(h);
6494}
6495
6496static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
6497 __attribute__((unused)) pm_message_t state)
6498{
6499 return -ENOSYS;
6500}
6501
6502static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
6503{
6504 return -ENOSYS;
6505}
6506
6507static struct pci_driver hpsa_pci_driver = {
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06006508 .name = HPSA,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006509 .probe = hpsa_init_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006510 .remove = hpsa_remove_one,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006511 .id_table = hpsa_pci_device_id, /* id_table */
6512 .shutdown = hpsa_shutdown,
6513 .suspend = hpsa_suspend,
6514 .resume = hpsa_resume,
6515};
6516
Don Brace303932f2010-02-04 08:42:40 -06006517/* Fill in bucket_map[], given nsgs (the max number of
6518 * scatter gather elements supported) and bucket[],
6519 * which is an array of 8 integers. The bucket[] array
6520 * contains 8 different DMA transfer sizes (in 16
6521 * byte increments) which the controller uses to fetch
6522 * commands. This function fills in bucket_map[], which
6523 * maps a given number of scatter gather elements to one of
6524 * the 8 DMA transfer sizes. The point of it is to allow the
6525 * controller to only do as much DMA as needed to fetch the
6526 * command, with the DMA transfer size encoded in the lower
6527 * bits of the command address.
6528 */
6529static void calc_bucket_map(int bucket[], int num_buckets,
Matt Gatese1f7de02014-02-18 13:55:17 -06006530 int nsgs, int min_blocks, int *bucket_map)
Don Brace303932f2010-02-04 08:42:40 -06006531{
6532 int i, j, b, size;
6533
Don Brace303932f2010-02-04 08:42:40 -06006534 /* Note, bucket_map must have nsgs+1 entries. */
6535 for (i = 0; i <= nsgs; i++) {
6536 /* Compute size of a command with i SG entries */
Matt Gatese1f7de02014-02-18 13:55:17 -06006537 size = i + min_blocks;
Don Brace303932f2010-02-04 08:42:40 -06006538 b = num_buckets; /* Assume the biggest bucket */
6539 /* Find the bucket that is just big enough */
Matt Gatese1f7de02014-02-18 13:55:17 -06006540 for (j = 0; j < num_buckets; j++) {
Don Brace303932f2010-02-04 08:42:40 -06006541 if (bucket[j] >= size) {
6542 b = j;
6543 break;
6544 }
6545 }
6546 /* for a command with i SG entries, use bucket b. */
6547 bucket_map[i] = b;
6548 }
6549}
6550
Matt Gatese1f7de02014-02-18 13:55:17 -06006551static void hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
Don Brace303932f2010-02-04 08:42:40 -06006552{
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006553 int i;
6554 unsigned long register_value;
Matt Gatese1f7de02014-02-18 13:55:17 -06006555 unsigned long transMethod = CFGTBL_Trans_Performant |
6556 (trans_support & CFGTBL_Trans_use_short_tags) |
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006557 CFGTBL_Trans_enable_directed_msix |
6558 (trans_support & (CFGTBL_Trans_io_accel1 |
6559 CFGTBL_Trans_io_accel2));
Matt Gatese1f7de02014-02-18 13:55:17 -06006560 struct access_method access = SA5_performant_access;
Stephen M. Camerondef342b2010-05-27 15:14:39 -05006561
6562 /* This is a bit complicated. There are 8 registers on
6563 * the controller which we write to to tell it 8 different
6564 * sizes of commands which there may be. It's a way of
6565 * reducing the DMA done to fetch each command. Encoded into
6566 * each command's tag are 3 bits which communicate to the controller
6567 * which of the eight sizes that command fits within. The size of
6568 * each command depends on how many scatter gather entries there are.
6569 * Each SG entry requires 16 bytes. The eight registers are programmed
6570 * with the number of 16-byte blocks a command of that size requires.
6571 * The smallest command possible requires 5 such 16 byte blocks.
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006572 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
Stephen M. Camerondef342b2010-05-27 15:14:39 -05006573 * blocks. Note, this only extends to the SG entries contained
6574 * within the command block, and does not extend to chained blocks
6575 * of SG elements. bft[] contains the eight values we write to
6576 * the registers. They are not evenly distributed, but have more
6577 * sizes for small commands, and fewer sizes for larger commands.
6578 */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006579 int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006580#define MIN_IOACCEL2_BFT_ENTRY 5
6581#define HPSA_IOACCEL2_HEADER_SZ 4
6582 int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
6583 13, 14, 15, 16, 17, 18, 19,
6584 HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
6585 BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
6586 BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
6587 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
6588 16 * MIN_IOACCEL2_BFT_ENTRY);
6589 BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006590 BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
Don Brace303932f2010-02-04 08:42:40 -06006591 /* 5 = 1 s/g entry or 4k
6592 * 6 = 2 s/g entry or 8k
6593 * 8 = 4 s/g entry or 16k
6594 * 10 = 6 s/g entry or 24k
6595 */
Don Brace303932f2010-02-04 08:42:40 -06006596
Don Brace303932f2010-02-04 08:42:40 -06006597 /* Controller spec: zero out this buffer. */
6598 memset(h->reply_pool, 0, h->reply_pool_size);
Don Brace303932f2010-02-04 08:42:40 -06006599
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006600 bft[7] = SG_ENTRIES_IN_CMD + 4;
6601 calc_bucket_map(bft, ARRAY_SIZE(bft),
Matt Gatese1f7de02014-02-18 13:55:17 -06006602 SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
Don Brace303932f2010-02-04 08:42:40 -06006603 for (i = 0; i < 8; i++)
6604 writel(bft[i], &h->transtable->BlockFetch[i]);
6605
6606 /* size of controller ring buffer */
6607 writel(h->max_commands, &h->transtable->RepQSize);
Matt Gates254f7962012-05-01 11:43:06 -05006608 writel(h->nreply_queues, &h->transtable->RepQCount);
Don Brace303932f2010-02-04 08:42:40 -06006609 writel(0, &h->transtable->RepQCtrAddrLow32);
6610 writel(0, &h->transtable->RepQCtrAddrHigh32);
Matt Gates254f7962012-05-01 11:43:06 -05006611
6612 for (i = 0; i < h->nreply_queues; i++) {
6613 writel(0, &h->transtable->RepQAddr[i].upper);
6614 writel(h->reply_pool_dhandle +
6615 (h->max_commands * sizeof(u64) * i),
6616 &h->transtable->RepQAddr[i].lower);
6617 }
6618
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006619 writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
Matt Gatese1f7de02014-02-18 13:55:17 -06006620 writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
6621 /*
6622 * enable outbound interrupt coalescing in accelerator mode;
6623 */
6624 if (trans_support & CFGTBL_Trans_io_accel1) {
6625 access = SA5_ioaccel_mode1_access;
6626 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
6627 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
Scott Teelc3497752014-02-18 13:56:34 -06006628 } else {
6629 if (trans_support & CFGTBL_Trans_io_accel2) {
6630 access = SA5_ioaccel_mode2_access;
6631 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
6632 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
6633 }
Matt Gatese1f7de02014-02-18 13:55:17 -06006634 }
Don Brace303932f2010-02-04 08:42:40 -06006635 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05006636 hpsa_wait_for_mode_change_ack(h);
Don Brace303932f2010-02-04 08:42:40 -06006637 register_value = readl(&(h->cfgtable->TransportActive));
6638 if (!(register_value & CFGTBL_Trans_Performant)) {
6639 dev_warn(&h->pdev->dev, "unable to get board into"
6640 " performant mode\n");
6641 return;
6642 }
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06006643 /* Change the access methods to the performant access methods */
Matt Gatese1f7de02014-02-18 13:55:17 -06006644 h->access = access;
6645 h->transMethod = transMethod;
6646
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006647 if (!((trans_support & CFGTBL_Trans_io_accel1) ||
6648 (trans_support & CFGTBL_Trans_io_accel2)))
Matt Gatese1f7de02014-02-18 13:55:17 -06006649 return;
6650
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006651 if (trans_support & CFGTBL_Trans_io_accel1) {
6652 /* Set up I/O accelerator mode */
6653 for (i = 0; i < h->nreply_queues; i++) {
6654 writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
6655 h->reply_queue[i].current_entry =
6656 readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
6657 }
6658 bft[7] = h->ioaccel_maxsg + 8;
6659 calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
6660 h->ioaccel1_blockFetchTable);
6661
6662 /* initialize all reply queue entries to unused */
6663 memset(h->reply_pool, (u8) IOACCEL_MODE1_REPLY_UNUSED,
6664 h->reply_pool_size);
6665
6666 /* set all the constant fields in the accelerator command
6667 * frames once at init time to save CPU cycles later.
6668 */
6669 for (i = 0; i < h->nr_cmds; i++) {
6670 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
6671
6672 cp->function = IOACCEL1_FUNCTION_SCSIIO;
6673 cp->err_info = (u32) (h->errinfo_pool_dhandle +
6674 (i * sizeof(struct ErrorInfo)));
6675 cp->err_info_len = sizeof(struct ErrorInfo);
6676 cp->sgl_offset = IOACCEL1_SGLOFFSET;
6677 cp->host_context_flags = IOACCEL1_HCFLAGS_CISS_FORMAT;
6678 cp->timeout_sec = 0;
6679 cp->ReplyQueue = 0;
6680 cp->Tag.lower = (i << DIRECT_LOOKUP_SHIFT) |
6681 DIRECT_LOOKUP_BIT;
6682 cp->Tag.upper = 0;
6683 cp->host_addr.lower =
6684 (u32) (h->ioaccel_cmd_pool_dhandle +
6685 (i * sizeof(struct io_accel1_cmd)));
6686 cp->host_addr.upper = 0;
6687 }
6688 } else if (trans_support & CFGTBL_Trans_io_accel2) {
6689 u64 cfg_offset, cfg_base_addr_index;
6690 u32 bft2_offset, cfg_base_addr;
6691 int rc;
6692
6693 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
6694 &cfg_base_addr_index, &cfg_offset);
6695 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
6696 bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
6697 calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
6698 4, h->ioaccel2_blockFetchTable);
6699 bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
6700 BUILD_BUG_ON(offsetof(struct CfgTable,
6701 io_accel_request_size_offset) != 0xb8);
6702 h->ioaccel2_bft2_regs =
6703 remap_pci_mem(pci_resource_start(h->pdev,
6704 cfg_base_addr_index) +
6705 cfg_offset + bft2_offset,
6706 ARRAY_SIZE(bft2) *
6707 sizeof(*h->ioaccel2_bft2_regs));
6708 for (i = 0; i < ARRAY_SIZE(bft2); i++)
6709 writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
Matt Gatese1f7de02014-02-18 13:55:17 -06006710 }
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006711 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
6712 hpsa_wait_for_mode_change_ack(h);
Matt Gatese1f7de02014-02-18 13:55:17 -06006713}
6714
6715static int hpsa_alloc_ioaccel_cmd_and_bft(struct ctlr_info *h)
6716{
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006717 h->ioaccel_maxsg =
6718 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
6719 if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
6720 h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
6721
Matt Gatese1f7de02014-02-18 13:55:17 -06006722 /* Command structures must be aligned on a 128-byte boundary
6723 * because the 7 lower bits of the address are used by the
6724 * hardware.
6725 */
6726#define IOACCEL1_COMMANDLIST_ALIGNMENT 128
6727 BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
6728 IOACCEL1_COMMANDLIST_ALIGNMENT);
6729 h->ioaccel_cmd_pool =
6730 pci_alloc_consistent(h->pdev,
6731 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
6732 &(h->ioaccel_cmd_pool_dhandle));
6733
6734 h->ioaccel1_blockFetchTable =
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006735 kmalloc(((h->ioaccel_maxsg + 1) *
Matt Gatese1f7de02014-02-18 13:55:17 -06006736 sizeof(u32)), GFP_KERNEL);
6737
6738 if ((h->ioaccel_cmd_pool == NULL) ||
6739 (h->ioaccel1_blockFetchTable == NULL))
6740 goto clean_up;
6741
6742 memset(h->ioaccel_cmd_pool, 0,
6743 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
6744 return 0;
6745
6746clean_up:
6747 if (h->ioaccel_cmd_pool)
6748 pci_free_consistent(h->pdev,
6749 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
6750 h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
6751 kfree(h->ioaccel1_blockFetchTable);
6752 return 1;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006753}
6754
Stephen M. Cameronaca90122014-02-18 13:56:14 -06006755static int ioaccel2_alloc_cmds_and_bft(struct ctlr_info *h)
6756{
6757 /* Allocate ioaccel2 mode command blocks and block fetch table */
6758
6759 h->ioaccel_maxsg =
6760 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
6761 if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
6762 h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
6763
6764#define IOACCEL2_COMMANDLIST_ALIGNMENT 128
6765 BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
6766 IOACCEL2_COMMANDLIST_ALIGNMENT);
6767 h->ioaccel2_cmd_pool =
6768 pci_alloc_consistent(h->pdev,
6769 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
6770 &(h->ioaccel2_cmd_pool_dhandle));
6771
6772 h->ioaccel2_blockFetchTable =
6773 kmalloc(((h->ioaccel_maxsg + 1) *
6774 sizeof(u32)), GFP_KERNEL);
6775
6776 if ((h->ioaccel2_cmd_pool == NULL) ||
6777 (h->ioaccel2_blockFetchTable == NULL))
6778 goto clean_up;
6779
6780 memset(h->ioaccel2_cmd_pool, 0,
6781 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
6782 return 0;
6783
6784clean_up:
6785 if (h->ioaccel2_cmd_pool)
6786 pci_free_consistent(h->pdev,
6787 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
6788 h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
6789 kfree(h->ioaccel2_blockFetchTable);
6790 return 1;
6791}
6792
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006793static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006794{
6795 u32 trans_support;
Matt Gatese1f7de02014-02-18 13:55:17 -06006796 unsigned long transMethod = CFGTBL_Trans_Performant |
6797 CFGTBL_Trans_use_short_tags;
Matt Gates254f7962012-05-01 11:43:06 -05006798 int i;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006799
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -06006800 if (hpsa_simple_mode)
6801 return;
6802
Matt Gatese1f7de02014-02-18 13:55:17 -06006803 /* Check for I/O accelerator mode support */
6804 if (trans_support & CFGTBL_Trans_io_accel1) {
6805 transMethod |= CFGTBL_Trans_io_accel1 |
6806 CFGTBL_Trans_enable_directed_msix;
6807 if (hpsa_alloc_ioaccel_cmd_and_bft(h))
6808 goto clean_up;
Stephen M. Cameronaca90122014-02-18 13:56:14 -06006809 } else {
6810 if (trans_support & CFGTBL_Trans_io_accel2) {
6811 transMethod |= CFGTBL_Trans_io_accel2 |
6812 CFGTBL_Trans_enable_directed_msix;
6813 if (ioaccel2_alloc_cmds_and_bft(h))
6814 goto clean_up;
6815 }
Matt Gatese1f7de02014-02-18 13:55:17 -06006816 }
6817
6818 /* TODO, check that this next line h->nreply_queues is correct */
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006819 trans_support = readl(&(h->cfgtable->TransportSupport));
6820 if (!(trans_support & PERFORMANT_MODE))
6821 return;
6822
Hannes Reineckeeee0f032014-01-15 13:30:53 +01006823 h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05006824 hpsa_get_max_perf_mode_cmds(h);
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006825 /* Performant mode ring buffer and supporting data structures */
Matt Gates254f7962012-05-01 11:43:06 -05006826 h->reply_pool_size = h->max_commands * sizeof(u64) * h->nreply_queues;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006827 h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size,
6828 &(h->reply_pool_dhandle));
6829
Matt Gates254f7962012-05-01 11:43:06 -05006830 for (i = 0; i < h->nreply_queues; i++) {
6831 h->reply_queue[i].head = &h->reply_pool[h->max_commands * i];
6832 h->reply_queue[i].size = h->max_commands;
6833 h->reply_queue[i].wraparound = 1; /* spec: init to 1 */
6834 h->reply_queue[i].current_entry = 0;
6835 }
6836
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006837 /* Need a block fetch table for performant mode */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006838 h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006839 sizeof(u32)), GFP_KERNEL);
6840
6841 if ((h->reply_pool == NULL)
6842 || (h->blockFetchTable == NULL))
6843 goto clean_up;
6844
Matt Gatese1f7de02014-02-18 13:55:17 -06006845 hpsa_enter_performant_mode(h, trans_support);
Don Brace303932f2010-02-04 08:42:40 -06006846 return;
6847
6848clean_up:
6849 if (h->reply_pool)
6850 pci_free_consistent(h->pdev, h->reply_pool_size,
6851 h->reply_pool, h->reply_pool_dhandle);
6852 kfree(h->blockFetchTable);
6853}
6854
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006855static void hpsa_drain_commands(struct ctlr_info *h)
6856{
6857 int cmds_out;
6858 unsigned long flags;
6859
6860 do { /* wait for all outstanding commands to drain out */
6861 spin_lock_irqsave(&h->lock, flags);
6862 cmds_out = h->commands_outstanding;
6863 spin_unlock_irqrestore(&h->lock, flags);
6864 if (cmds_out <= 0)
6865 break;
6866 msleep(100);
6867 } while (1);
6868}
6869
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006870/*
6871 * This is it. Register the PCI driver information for the cards we control
6872 * the OS will call our registered routines when it finds one of our cards.
6873 */
6874static int __init hpsa_init(void)
6875{
Mike Miller31468402010-02-25 14:03:12 -06006876 return pci_register_driver(&hpsa_pci_driver);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006877}
6878
6879static void __exit hpsa_cleanup(void)
6880{
6881 pci_unregister_driver(&hpsa_pci_driver);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006882}
6883
Matt Gatese1f7de02014-02-18 13:55:17 -06006884static void __attribute__((unused)) verify_offsets(void)
6885{
6886#define VERIFY_OFFSET(member, offset) \
Mike Millerb66cc252014-02-18 13:56:04 -06006887 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
6888
6889 VERIFY_OFFSET(IU_type, 0);
6890 VERIFY_OFFSET(direction, 1);
6891 VERIFY_OFFSET(reply_queue, 2);
6892 /* VERIFY_OFFSET(reserved1, 3); */
6893 VERIFY_OFFSET(scsi_nexus, 4);
6894 VERIFY_OFFSET(Tag, 8);
6895 VERIFY_OFFSET(cdb, 16);
6896 VERIFY_OFFSET(cciss_lun, 32);
6897 VERIFY_OFFSET(data_len, 40);
6898 VERIFY_OFFSET(cmd_priority_task_attr, 44);
6899 VERIFY_OFFSET(sg_count, 45);
6900 /* VERIFY_OFFSET(reserved3 */
6901 VERIFY_OFFSET(err_ptr, 48);
6902 VERIFY_OFFSET(err_len, 56);
6903 /* VERIFY_OFFSET(reserved4 */
6904 VERIFY_OFFSET(sg, 64);
6905
6906#undef VERIFY_OFFSET
6907
6908#define VERIFY_OFFSET(member, offset) \
Matt Gatese1f7de02014-02-18 13:55:17 -06006909 BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
6910
6911 VERIFY_OFFSET(dev_handle, 0x00);
6912 VERIFY_OFFSET(reserved1, 0x02);
6913 VERIFY_OFFSET(function, 0x03);
6914 VERIFY_OFFSET(reserved2, 0x04);
6915 VERIFY_OFFSET(err_info, 0x0C);
6916 VERIFY_OFFSET(reserved3, 0x10);
6917 VERIFY_OFFSET(err_info_len, 0x12);
6918 VERIFY_OFFSET(reserved4, 0x13);
6919 VERIFY_OFFSET(sgl_offset, 0x14);
6920 VERIFY_OFFSET(reserved5, 0x15);
6921 VERIFY_OFFSET(transfer_len, 0x1C);
6922 VERIFY_OFFSET(reserved6, 0x20);
6923 VERIFY_OFFSET(io_flags, 0x24);
6924 VERIFY_OFFSET(reserved7, 0x26);
6925 VERIFY_OFFSET(LUN, 0x34);
6926 VERIFY_OFFSET(control, 0x3C);
6927 VERIFY_OFFSET(CDB, 0x40);
6928 VERIFY_OFFSET(reserved8, 0x50);
6929 VERIFY_OFFSET(host_context_flags, 0x60);
6930 VERIFY_OFFSET(timeout_sec, 0x62);
6931 VERIFY_OFFSET(ReplyQueue, 0x64);
6932 VERIFY_OFFSET(reserved9, 0x65);
6933 VERIFY_OFFSET(Tag, 0x68);
6934 VERIFY_OFFSET(host_addr, 0x70);
6935 VERIFY_OFFSET(CISS_LUN, 0x78);
6936 VERIFY_OFFSET(SG, 0x78 + 8);
6937#undef VERIFY_OFFSET
6938}
6939
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006940module_init(hpsa_init);
6941module_exit(hpsa_cleanup);