blob: e55e787f5092fa8fc1ef316db8fe5e0417ac385b [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);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800225
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800226static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
227{
228 unsigned long *priv = shost_priv(sdev->host);
229 return (struct ctlr_info *) *priv;
230}
231
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600232static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
233{
234 unsigned long *priv = shost_priv(sh);
235 return (struct ctlr_info *) *priv;
236}
237
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800238static int check_for_unit_attention(struct ctlr_info *h,
239 struct CommandList *c)
240{
241 if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
242 return 0;
243
244 switch (c->err_info->SenseInfo[12]) {
245 case STATE_CHANGED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600246 dev_warn(&h->pdev->dev, HPSA "%d: a state change "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800247 "detected, command retried\n", h->ctlr);
248 break;
249 case LUN_FAILED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600250 dev_warn(&h->pdev->dev, HPSA "%d: LUN failure "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800251 "detected, action required\n", h->ctlr);
252 break;
253 case REPORT_LUNS_CHANGED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600254 dev_warn(&h->pdev->dev, HPSA "%d: report LUN data "
Mike Miller31468402010-02-25 14:03:12 -0600255 "changed, action required\n", h->ctlr);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800256 /*
Scott Teel4f4eb9f2012-01-19 14:01:25 -0600257 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
258 * target (array) devices.
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800259 */
260 break;
261 case POWER_OR_RESET:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600262 dev_warn(&h->pdev->dev, HPSA "%d: a power on "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800263 "or device reset detected\n", h->ctlr);
264 break;
265 case UNIT_ATTENTION_CLEARED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600266 dev_warn(&h->pdev->dev, HPSA "%d: unit attention "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800267 "cleared by another initiator\n", h->ctlr);
268 break;
269 default:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600270 dev_warn(&h->pdev->dev, HPSA "%d: unknown "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800271 "unit attention detected\n", h->ctlr);
272 break;
273 }
274 return 1;
275}
276
Matt Bondurant852af202012-05-01 11:42:35 -0500277static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
278{
279 if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
280 (c->err_info->ScsiStatus != SAM_STAT_BUSY &&
281 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
282 return 0;
283 dev_warn(&h->pdev->dev, HPSA "device busy");
284 return 1;
285}
286
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800287static ssize_t host_store_rescan(struct device *dev,
288 struct device_attribute *attr,
289 const char *buf, size_t count)
290{
291 struct ctlr_info *h;
292 struct Scsi_Host *shost = class_to_shost(dev);
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600293 h = shost_to_hba(shost);
Mike Miller31468402010-02-25 14:03:12 -0600294 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800295 return count;
296}
297
Stephen M. Camerond28ce022010-05-27 15:14:34 -0500298static ssize_t host_show_firmware_revision(struct device *dev,
299 struct device_attribute *attr, char *buf)
300{
301 struct ctlr_info *h;
302 struct Scsi_Host *shost = class_to_shost(dev);
303 unsigned char *fwrev;
304
305 h = shost_to_hba(shost);
306 if (!h->hba_inquiry_data)
307 return 0;
308 fwrev = &h->hba_inquiry_data[32];
309 return snprintf(buf, 20, "%c%c%c%c\n",
310 fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
311}
312
Stephen M. Cameron94a13642011-01-06 14:48:39 -0600313static ssize_t host_show_commands_outstanding(struct device *dev,
314 struct device_attribute *attr, char *buf)
315{
316 struct Scsi_Host *shost = class_to_shost(dev);
317 struct ctlr_info *h = shost_to_hba(shost);
318
319 return snprintf(buf, 20, "%d\n", h->commands_outstanding);
320}
321
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600322static ssize_t host_show_transport_mode(struct device *dev,
323 struct device_attribute *attr, char *buf)
324{
325 struct ctlr_info *h;
326 struct Scsi_Host *shost = class_to_shost(dev);
327
328 h = shost_to_hba(shost);
329 return snprintf(buf, 20, "%s\n",
Stephen M. Cameron960a30e2011-02-15 15:33:03 -0600330 h->transMethod & CFGTBL_Trans_Performant ?
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600331 "performant" : "simple");
332}
333
Stephen M. Cameron46380782011-05-03 15:00:01 -0500334/* List of controllers which cannot be hard reset on kexec with reset_devices */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600335static u32 unresettable_controller[] = {
336 0x324a103C, /* Smart Array P712m */
337 0x324b103C, /* SmartArray P711m */
338 0x3223103C, /* Smart Array P800 */
339 0x3234103C, /* Smart Array P400 */
340 0x3235103C, /* Smart Array P400i */
341 0x3211103C, /* Smart Array E200i */
342 0x3212103C, /* Smart Array E200 */
343 0x3213103C, /* Smart Array E200i */
344 0x3214103C, /* Smart Array E200i */
345 0x3215103C, /* Smart Array E200i */
346 0x3237103C, /* Smart Array E500 */
347 0x323D103C, /* Smart Array P700m */
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100348 0x40800E11, /* Smart Array 5i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600349 0x409C0E11, /* Smart Array 6400 */
350 0x409D0E11, /* Smart Array 6400 EM */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100351 0x40700E11, /* Smart Array 5300 */
352 0x40820E11, /* Smart Array 532 */
353 0x40830E11, /* Smart Array 5312 */
354 0x409A0E11, /* Smart Array 641 */
355 0x409B0E11, /* Smart Array 642 */
356 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600357};
358
Stephen M. Cameron46380782011-05-03 15:00:01 -0500359/* List of controllers which cannot even be soft reset */
360static u32 soft_unresettable_controller[] = {
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100361 0x40800E11, /* Smart Array 5i */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100362 0x40700E11, /* Smart Array 5300 */
363 0x40820E11, /* Smart Array 532 */
364 0x40830E11, /* Smart Array 5312 */
365 0x409A0E11, /* Smart Array 641 */
366 0x409B0E11, /* Smart Array 642 */
367 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron46380782011-05-03 15:00:01 -0500368 /* Exclude 640x boards. These are two pci devices in one slot
369 * which share a battery backed cache module. One controls the
370 * cache, the other accesses the cache through the one that controls
371 * it. If we reset the one controlling the cache, the other will
372 * likely not be happy. Just forbid resetting this conjoined mess.
373 * The 640x isn't really supported by hpsa anyway.
374 */
375 0x409C0E11, /* Smart Array 6400 */
376 0x409D0E11, /* Smart Array 6400 EM */
377};
378
379static int ctlr_is_hard_resettable(u32 board_id)
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600380{
381 int i;
382
383 for (i = 0; i < ARRAY_SIZE(unresettable_controller); i++)
Stephen M. Cameron46380782011-05-03 15:00:01 -0500384 if (unresettable_controller[i] == board_id)
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600385 return 0;
386 return 1;
387}
388
Stephen M. Cameron46380782011-05-03 15:00:01 -0500389static int ctlr_is_soft_resettable(u32 board_id)
390{
391 int i;
392
393 for (i = 0; i < ARRAY_SIZE(soft_unresettable_controller); i++)
394 if (soft_unresettable_controller[i] == board_id)
395 return 0;
396 return 1;
397}
398
399static int ctlr_is_resettable(u32 board_id)
400{
401 return ctlr_is_hard_resettable(board_id) ||
402 ctlr_is_soft_resettable(board_id);
403}
404
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600405static ssize_t host_show_resettable(struct device *dev,
406 struct device_attribute *attr, char *buf)
407{
408 struct ctlr_info *h;
409 struct Scsi_Host *shost = class_to_shost(dev);
410
411 h = shost_to_hba(shost);
Stephen M. Cameron46380782011-05-03 15:00:01 -0500412 return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600413}
414
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800415static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
416{
417 return (scsi3addr[3] & 0xC0) == 0x40;
418}
419
420static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
Mike Millerd82357e2012-05-01 11:43:32 -0500421 "1(ADM)", "UNKNOWN"
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800422};
423#define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
424
425static ssize_t raid_level_show(struct device *dev,
426 struct device_attribute *attr, char *buf)
427{
428 ssize_t l = 0;
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600429 unsigned char rlevel;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800430 struct ctlr_info *h;
431 struct scsi_device *sdev;
432 struct hpsa_scsi_dev_t *hdev;
433 unsigned long flags;
434
435 sdev = to_scsi_device(dev);
436 h = sdev_to_hba(sdev);
437 spin_lock_irqsave(&h->lock, flags);
438 hdev = sdev->hostdata;
439 if (!hdev) {
440 spin_unlock_irqrestore(&h->lock, flags);
441 return -ENODEV;
442 }
443
444 /* Is this even a logical drive? */
445 if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
446 spin_unlock_irqrestore(&h->lock, flags);
447 l = snprintf(buf, PAGE_SIZE, "N/A\n");
448 return l;
449 }
450
451 rlevel = hdev->raid_level;
452 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600453 if (rlevel > RAID_UNKNOWN)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800454 rlevel = RAID_UNKNOWN;
455 l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
456 return l;
457}
458
459static ssize_t lunid_show(struct device *dev,
460 struct device_attribute *attr, char *buf)
461{
462 struct ctlr_info *h;
463 struct scsi_device *sdev;
464 struct hpsa_scsi_dev_t *hdev;
465 unsigned long flags;
466 unsigned char lunid[8];
467
468 sdev = to_scsi_device(dev);
469 h = sdev_to_hba(sdev);
470 spin_lock_irqsave(&h->lock, flags);
471 hdev = sdev->hostdata;
472 if (!hdev) {
473 spin_unlock_irqrestore(&h->lock, flags);
474 return -ENODEV;
475 }
476 memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
477 spin_unlock_irqrestore(&h->lock, flags);
478 return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
479 lunid[0], lunid[1], lunid[2], lunid[3],
480 lunid[4], lunid[5], lunid[6], lunid[7]);
481}
482
483static ssize_t unique_id_show(struct device *dev,
484 struct device_attribute *attr, char *buf)
485{
486 struct ctlr_info *h;
487 struct scsi_device *sdev;
488 struct hpsa_scsi_dev_t *hdev;
489 unsigned long flags;
490 unsigned char sn[16];
491
492 sdev = to_scsi_device(dev);
493 h = sdev_to_hba(sdev);
494 spin_lock_irqsave(&h->lock, flags);
495 hdev = sdev->hostdata;
496 if (!hdev) {
497 spin_unlock_irqrestore(&h->lock, flags);
498 return -ENODEV;
499 }
500 memcpy(sn, hdev->device_id, sizeof(sn));
501 spin_unlock_irqrestore(&h->lock, flags);
502 return snprintf(buf, 16 * 2 + 2,
503 "%02X%02X%02X%02X%02X%02X%02X%02X"
504 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
505 sn[0], sn[1], sn[2], sn[3],
506 sn[4], sn[5], sn[6], sn[7],
507 sn[8], sn[9], sn[10], sn[11],
508 sn[12], sn[13], sn[14], sn[15]);
509}
510
Scott Teelc1988682014-02-18 13:55:54 -0600511static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
512 struct device_attribute *attr, char *buf)
513{
514 struct ctlr_info *h;
515 struct scsi_device *sdev;
516 struct hpsa_scsi_dev_t *hdev;
517 unsigned long flags;
518 int offload_enabled;
519
520 sdev = to_scsi_device(dev);
521 h = sdev_to_hba(sdev);
522 spin_lock_irqsave(&h->lock, flags);
523 hdev = sdev->hostdata;
524 if (!hdev) {
525 spin_unlock_irqrestore(&h->lock, flags);
526 return -ENODEV;
527 }
528 offload_enabled = hdev->offload_enabled;
529 spin_unlock_irqrestore(&h->lock, flags);
530 return snprintf(buf, 20, "%d\n", offload_enabled);
531}
532
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600533static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
534static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
535static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
536static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
Scott Teelc1988682014-02-18 13:55:54 -0600537static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
538 host_show_hp_ssd_smart_path_enabled, NULL);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600539static DEVICE_ATTR(firmware_revision, S_IRUGO,
540 host_show_firmware_revision, NULL);
541static DEVICE_ATTR(commands_outstanding, S_IRUGO,
542 host_show_commands_outstanding, NULL);
543static DEVICE_ATTR(transport_mode, S_IRUGO,
544 host_show_transport_mode, NULL);
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600545static DEVICE_ATTR(resettable, S_IRUGO,
546 host_show_resettable, NULL);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600547
548static struct device_attribute *hpsa_sdev_attrs[] = {
549 &dev_attr_raid_level,
550 &dev_attr_lunid,
551 &dev_attr_unique_id,
Scott Teelc1988682014-02-18 13:55:54 -0600552 &dev_attr_hp_ssd_smart_path_enabled,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600553 NULL,
554};
555
556static struct device_attribute *hpsa_shost_attrs[] = {
557 &dev_attr_rescan,
558 &dev_attr_firmware_revision,
559 &dev_attr_commands_outstanding,
560 &dev_attr_transport_mode,
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600561 &dev_attr_resettable,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600562 NULL,
563};
564
565static struct scsi_host_template hpsa_driver_template = {
566 .module = THIS_MODULE,
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600567 .name = HPSA,
568 .proc_name = HPSA,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600569 .queuecommand = hpsa_scsi_queue_command,
570 .scan_start = hpsa_scan_start,
571 .scan_finished = hpsa_scan_finished,
572 .change_queue_depth = hpsa_change_queue_depth,
573 .this_id = -1,
574 .use_clustering = ENABLE_CLUSTERING,
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500575 .eh_abort_handler = hpsa_eh_abort_handler,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600576 .eh_device_reset_handler = hpsa_eh_device_reset_handler,
577 .ioctl = hpsa_ioctl,
578 .slave_alloc = hpsa_slave_alloc,
579 .slave_destroy = hpsa_slave_destroy,
580#ifdef CONFIG_COMPAT
581 .compat_ioctl = hpsa_compat_ioctl,
582#endif
583 .sdev_attrs = hpsa_sdev_attrs,
584 .shost_attrs = hpsa_shost_attrs,
Stephen M. Cameronc0d6a4d2011-10-26 16:20:53 -0500585 .max_sectors = 8192,
Martin K. Petersen54b2b502013-10-23 06:25:40 -0400586 .no_write_same = 1,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600587};
588
589
590/* Enqueuing and dequeuing functions for cmdlists. */
591static inline void addQ(struct list_head *list, struct CommandList *c)
592{
593 list_add_tail(&c->list, list);
594}
595
Matt Gates254f7962012-05-01 11:43:06 -0500596static inline u32 next_command(struct ctlr_info *h, u8 q)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600597{
598 u32 a;
Matt Gates254f7962012-05-01 11:43:06 -0500599 struct reply_pool *rq = &h->reply_queue[q];
Matt Gatese16a33a2012-05-01 11:43:11 -0500600 unsigned long flags;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600601
Matt Gatese1f7de02014-02-18 13:55:17 -0600602 if (h->transMethod & CFGTBL_Trans_io_accel1)
603 return h->access.command_completed(h, q);
604
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600605 if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
Matt Gates254f7962012-05-01 11:43:06 -0500606 return h->access.command_completed(h, q);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600607
Matt Gates254f7962012-05-01 11:43:06 -0500608 if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
609 a = rq->head[rq->current_entry];
610 rq->current_entry++;
Matt Gatese16a33a2012-05-01 11:43:11 -0500611 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600612 h->commands_outstanding--;
Matt Gatese16a33a2012-05-01 11:43:11 -0500613 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600614 } else {
615 a = FIFO_EMPTY;
616 }
617 /* Check for wraparound */
Matt Gates254f7962012-05-01 11:43:06 -0500618 if (rq->current_entry == h->max_commands) {
619 rq->current_entry = 0;
620 rq->wraparound ^= 1;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600621 }
622 return a;
623}
624
625/* set_performant_mode: Modify the tag for cciss performant
626 * set bit 0 for pull model, bits 3-1 for block fetch
627 * register number
628 */
629static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
630{
Matt Gates254f7962012-05-01 11:43:06 -0500631 if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600632 c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
Hannes Reineckeeee0f032014-01-15 13:30:53 +0100633 if (likely(h->msix_vector > 0))
Matt Gates254f7962012-05-01 11:43:06 -0500634 c->Header.ReplyQueue =
John Kacur804a5cb2013-07-26 16:06:18 +0200635 raw_smp_processor_id() % h->nreply_queues;
Matt Gates254f7962012-05-01 11:43:06 -0500636 }
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600637}
638
Stephen M. Camerone85c5972012-05-01 11:43:42 -0500639static int is_firmware_flash_cmd(u8 *cdb)
640{
641 return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
642}
643
644/*
645 * During firmware flash, the heartbeat register may not update as frequently
646 * as it should. So we dial down lockup detection during firmware flash. and
647 * dial it back up when firmware flash completes.
648 */
649#define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
650#define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
651static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
652 struct CommandList *c)
653{
654 if (!is_firmware_flash_cmd(c->Request.CDB))
655 return;
656 atomic_inc(&h->firmware_flash_in_progress);
657 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
658}
659
660static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
661 struct CommandList *c)
662{
663 if (is_firmware_flash_cmd(c->Request.CDB) &&
664 atomic_dec_and_test(&h->firmware_flash_in_progress))
665 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
666}
667
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600668static void enqueue_cmd_and_start_io(struct ctlr_info *h,
669 struct CommandList *c)
670{
671 unsigned long flags;
672
673 set_performant_mode(h, c);
Stephen M. Camerone85c5972012-05-01 11:43:42 -0500674 dial_down_lockup_detection_during_fw_flash(h, c);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600675 spin_lock_irqsave(&h->lock, flags);
676 addQ(&h->reqQ, c);
677 h->Qdepth++;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600678 spin_unlock_irqrestore(&h->lock, flags);
Matt Gatese16a33a2012-05-01 11:43:11 -0500679 start_io(h);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600680}
681
682static inline void removeQ(struct CommandList *c)
683{
684 if (WARN_ON(list_empty(&c->list)))
685 return;
686 list_del_init(&c->list);
687}
688
689static inline int is_hba_lunid(unsigned char scsi3addr[])
690{
691 return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
692}
693
694static inline int is_scsi_rev_5(struct ctlr_info *h)
695{
696 if (!h->hba_inquiry_data)
697 return 0;
698 if ((h->hba_inquiry_data[2] & 0x07) == 5)
699 return 1;
700 return 0;
701}
702
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800703static int hpsa_find_target_lun(struct ctlr_info *h,
704 unsigned char scsi3addr[], int bus, int *target, int *lun)
705{
706 /* finds an unused bus, target, lun for a new physical device
707 * assumes h->devlock is held
708 */
709 int i, found = 0;
Scott Teelcfe5bad2011-10-26 16:21:07 -0500710 DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800711
Akinobu Mita263d9402012-01-21 00:15:27 +0900712 bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800713
714 for (i = 0; i < h->ndevices; i++) {
715 if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
Akinobu Mita263d9402012-01-21 00:15:27 +0900716 __set_bit(h->dev[i]->target, lun_taken);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800717 }
718
Akinobu Mita263d9402012-01-21 00:15:27 +0900719 i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
720 if (i < HPSA_MAX_DEVICES) {
721 /* *bus = 1; */
722 *target = i;
723 *lun = 0;
724 found = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800725 }
726 return !found;
727}
728
729/* Add an entry into h->dev[] array. */
730static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
731 struct hpsa_scsi_dev_t *device,
732 struct hpsa_scsi_dev_t *added[], int *nadded)
733{
734 /* assumes h->devlock is held */
735 int n = h->ndevices;
736 int i;
737 unsigned char addr1[8], addr2[8];
738 struct hpsa_scsi_dev_t *sd;
739
Scott Teelcfe5bad2011-10-26 16:21:07 -0500740 if (n >= HPSA_MAX_DEVICES) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800741 dev_err(&h->pdev->dev, "too many devices, some will be "
742 "inaccessible.\n");
743 return -1;
744 }
745
746 /* physical devices do not have lun or target assigned until now. */
747 if (device->lun != -1)
748 /* Logical device, lun is already assigned. */
749 goto lun_assigned;
750
751 /* If this device a non-zero lun of a multi-lun device
752 * byte 4 of the 8-byte LUN addr will contain the logical
753 * unit no, zero otherise.
754 */
755 if (device->scsi3addr[4] == 0) {
756 /* This is not a non-zero lun of a multi-lun device */
757 if (hpsa_find_target_lun(h, device->scsi3addr,
758 device->bus, &device->target, &device->lun) != 0)
759 return -1;
760 goto lun_assigned;
761 }
762
763 /* This is a non-zero lun of a multi-lun device.
764 * Search through our list and find the device which
765 * has the same 8 byte LUN address, excepting byte 4.
766 * Assign the same bus and target for this new LUN.
767 * Use the logical unit number from the firmware.
768 */
769 memcpy(addr1, device->scsi3addr, 8);
770 addr1[4] = 0;
771 for (i = 0; i < n; i++) {
772 sd = h->dev[i];
773 memcpy(addr2, sd->scsi3addr, 8);
774 addr2[4] = 0;
775 /* differ only in byte 4? */
776 if (memcmp(addr1, addr2, 8) == 0) {
777 device->bus = sd->bus;
778 device->target = sd->target;
779 device->lun = device->scsi3addr[4];
780 break;
781 }
782 }
783 if (device->lun == -1) {
784 dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
785 " suspect firmware bug or unsupported hardware "
786 "configuration.\n");
787 return -1;
788 }
789
790lun_assigned:
791
792 h->dev[n] = device;
793 h->ndevices++;
794 added[*nadded] = device;
795 (*nadded)++;
796
797 /* initially, (before registering with scsi layer) we don't
798 * know our hostno and we don't want to print anything first
799 * time anyway (the scsi layer's inquiries will show that info)
800 */
801 /* if (hostno != -1) */
802 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
803 scsi_device_type(device->devtype), hostno,
804 device->bus, device->target, device->lun);
805 return 0;
806}
807
Scott Teelbd9244f2012-01-19 14:01:30 -0600808/* Update an entry in h->dev[] array. */
809static void hpsa_scsi_update_entry(struct ctlr_info *h, int hostno,
810 int entry, struct hpsa_scsi_dev_t *new_entry)
811{
812 /* assumes h->devlock is held */
813 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
814
815 /* Raid level changed. */
816 h->dev[entry]->raid_level = new_entry->raid_level;
Stephen M. Cameron250fb122014-02-18 13:55:38 -0600817
818 /* Raid offload parameters changed. */
819 h->dev[entry]->offload_config = new_entry->offload_config;
820 h->dev[entry]->offload_enabled = new_entry->offload_enabled;
821
Scott Teelbd9244f2012-01-19 14:01:30 -0600822 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d updated.\n",
823 scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
824 new_entry->target, new_entry->lun);
825}
826
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -0600827/* Replace an entry from h->dev[] array. */
828static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
829 int entry, struct hpsa_scsi_dev_t *new_entry,
830 struct hpsa_scsi_dev_t *added[], int *nadded,
831 struct hpsa_scsi_dev_t *removed[], int *nremoved)
832{
833 /* assumes h->devlock is held */
Scott Teelcfe5bad2011-10-26 16:21:07 -0500834 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -0600835 removed[*nremoved] = h->dev[entry];
836 (*nremoved)++;
Stephen M. Cameron01350d02011-08-09 08:18:01 -0500837
838 /*
839 * New physical devices won't have target/lun assigned yet
840 * so we need to preserve the values in the slot we are replacing.
841 */
842 if (new_entry->target == -1) {
843 new_entry->target = h->dev[entry]->target;
844 new_entry->lun = h->dev[entry]->lun;
845 }
846
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -0600847 h->dev[entry] = new_entry;
848 added[*nadded] = new_entry;
849 (*nadded)++;
850 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n",
851 scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
852 new_entry->target, new_entry->lun);
853}
854
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800855/* Remove an entry from h->dev[] array. */
856static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
857 struct hpsa_scsi_dev_t *removed[], int *nremoved)
858{
859 /* assumes h->devlock is held */
860 int i;
861 struct hpsa_scsi_dev_t *sd;
862
Scott Teelcfe5bad2011-10-26 16:21:07 -0500863 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800864
865 sd = h->dev[entry];
866 removed[*nremoved] = h->dev[entry];
867 (*nremoved)++;
868
869 for (i = entry; i < h->ndevices-1; i++)
870 h->dev[i] = h->dev[i+1];
871 h->ndevices--;
872 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
873 scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
874 sd->lun);
875}
876
877#define SCSI3ADDR_EQ(a, b) ( \
878 (a)[7] == (b)[7] && \
879 (a)[6] == (b)[6] && \
880 (a)[5] == (b)[5] && \
881 (a)[4] == (b)[4] && \
882 (a)[3] == (b)[3] && \
883 (a)[2] == (b)[2] && \
884 (a)[1] == (b)[1] && \
885 (a)[0] == (b)[0])
886
887static void fixup_botched_add(struct ctlr_info *h,
888 struct hpsa_scsi_dev_t *added)
889{
890 /* called when scsi_add_device fails in order to re-adjust
891 * h->dev[] to match the mid layer's view.
892 */
893 unsigned long flags;
894 int i, j;
895
896 spin_lock_irqsave(&h->lock, flags);
897 for (i = 0; i < h->ndevices; i++) {
898 if (h->dev[i] == added) {
899 for (j = i; j < h->ndevices-1; j++)
900 h->dev[j] = h->dev[j+1];
901 h->ndevices--;
902 break;
903 }
904 }
905 spin_unlock_irqrestore(&h->lock, flags);
906 kfree(added);
907}
908
909static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
910 struct hpsa_scsi_dev_t *dev2)
911{
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800912 /* we compare everything except lun and target as these
913 * are not yet assigned. Compare parts likely
914 * to differ first
915 */
916 if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
917 sizeof(dev1->scsi3addr)) != 0)
918 return 0;
919 if (memcmp(dev1->device_id, dev2->device_id,
920 sizeof(dev1->device_id)) != 0)
921 return 0;
922 if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
923 return 0;
924 if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
925 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800926 if (dev1->devtype != dev2->devtype)
927 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800928 if (dev1->bus != dev2->bus)
929 return 0;
930 return 1;
931}
932
Scott Teelbd9244f2012-01-19 14:01:30 -0600933static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
934 struct hpsa_scsi_dev_t *dev2)
935{
936 /* Device attributes that can change, but don't mean
937 * that the device is a different device, nor that the OS
938 * needs to be told anything about the change.
939 */
940 if (dev1->raid_level != dev2->raid_level)
941 return 1;
Stephen M. Cameron250fb122014-02-18 13:55:38 -0600942 if (dev1->offload_config != dev2->offload_config)
943 return 1;
944 if (dev1->offload_enabled != dev2->offload_enabled)
945 return 1;
Scott Teelbd9244f2012-01-19 14:01:30 -0600946 return 0;
947}
948
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800949/* Find needle in haystack. If exact match found, return DEVICE_SAME,
950 * and return needle location in *index. If scsi3addr matches, but not
951 * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
Scott Teelbd9244f2012-01-19 14:01:30 -0600952 * location in *index.
953 * In the case of a minor device attribute change, such as RAID level, just
954 * return DEVICE_UPDATED, along with the updated device's location in index.
955 * If needle not found, return DEVICE_NOT_FOUND.
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800956 */
957static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
958 struct hpsa_scsi_dev_t *haystack[], int haystack_size,
959 int *index)
960{
961 int i;
962#define DEVICE_NOT_FOUND 0
963#define DEVICE_CHANGED 1
964#define DEVICE_SAME 2
Scott Teelbd9244f2012-01-19 14:01:30 -0600965#define DEVICE_UPDATED 3
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800966 for (i = 0; i < haystack_size; i++) {
Stephen M. Cameron23231042010-02-04 08:43:36 -0600967 if (haystack[i] == NULL) /* previously removed. */
968 continue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800969 if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
970 *index = i;
Scott Teelbd9244f2012-01-19 14:01:30 -0600971 if (device_is_the_same(needle, haystack[i])) {
972 if (device_updated(needle, haystack[i]))
973 return DEVICE_UPDATED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800974 return DEVICE_SAME;
Scott Teelbd9244f2012-01-19 14:01:30 -0600975 } else {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800976 return DEVICE_CHANGED;
Scott Teelbd9244f2012-01-19 14:01:30 -0600977 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800978 }
979 }
980 *index = -1;
981 return DEVICE_NOT_FOUND;
982}
983
Stephen M. Cameron4967bd32010-02-04 08:41:49 -0600984static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800985 struct hpsa_scsi_dev_t *sd[], int nsds)
986{
987 /* sd contains scsi3 addresses and devtypes, and inquiry
988 * data. This function takes what's in sd to be the current
989 * reality and updates h->dev[] to reflect that reality.
990 */
991 int i, entry, device_change, changes = 0;
992 struct hpsa_scsi_dev_t *csd;
993 unsigned long flags;
994 struct hpsa_scsi_dev_t **added, **removed;
995 int nadded, nremoved;
996 struct Scsi_Host *sh = NULL;
997
Scott Teelcfe5bad2011-10-26 16:21:07 -0500998 added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
999 removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001000
1001 if (!added || !removed) {
1002 dev_warn(&h->pdev->dev, "out of memory in "
1003 "adjust_hpsa_scsi_table\n");
1004 goto free_and_out;
1005 }
1006
1007 spin_lock_irqsave(&h->devlock, flags);
1008
1009 /* find any devices in h->dev[] that are not in
1010 * sd[] and remove them from h->dev[], and for any
1011 * devices which have changed, remove the old device
1012 * info and add the new device info.
Scott Teelbd9244f2012-01-19 14:01:30 -06001013 * If minor device attributes change, just update
1014 * the existing device structure.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001015 */
1016 i = 0;
1017 nremoved = 0;
1018 nadded = 0;
1019 while (i < h->ndevices) {
1020 csd = h->dev[i];
1021 device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
1022 if (device_change == DEVICE_NOT_FOUND) {
1023 changes++;
1024 hpsa_scsi_remove_entry(h, hostno, i,
1025 removed, &nremoved);
1026 continue; /* remove ^^^, hence i not incremented */
1027 } else if (device_change == DEVICE_CHANGED) {
1028 changes++;
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001029 hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
1030 added, &nadded, removed, &nremoved);
Stephen M. Cameronc7f172d2010-02-04 08:43:31 -06001031 /* Set it to NULL to prevent it from being freed
1032 * at the bottom of hpsa_update_scsi_devices()
1033 */
1034 sd[entry] = NULL;
Scott Teelbd9244f2012-01-19 14:01:30 -06001035 } else if (device_change == DEVICE_UPDATED) {
1036 hpsa_scsi_update_entry(h, hostno, i, sd[entry]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001037 }
1038 i++;
1039 }
1040
1041 /* Now, make sure every device listed in sd[] is also
1042 * listed in h->dev[], adding them if they aren't found
1043 */
1044
1045 for (i = 0; i < nsds; i++) {
1046 if (!sd[i]) /* if already added above. */
1047 continue;
1048 device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1049 h->ndevices, &entry);
1050 if (device_change == DEVICE_NOT_FOUND) {
1051 changes++;
1052 if (hpsa_scsi_add_entry(h, hostno, sd[i],
1053 added, &nadded) != 0)
1054 break;
1055 sd[i] = NULL; /* prevent from being freed later. */
1056 } else if (device_change == DEVICE_CHANGED) {
1057 /* should never happen... */
1058 changes++;
1059 dev_warn(&h->pdev->dev,
1060 "device unexpectedly changed.\n");
1061 /* but if it does happen, we just ignore that device */
1062 }
1063 }
1064 spin_unlock_irqrestore(&h->devlock, flags);
1065
1066 /* Don't notify scsi mid layer of any changes the first time through
1067 * (or if there are no changes) scsi_scan_host will do it later the
1068 * first time through.
1069 */
1070 if (hostno == -1 || !changes)
1071 goto free_and_out;
1072
1073 sh = h->scsi_host;
1074 /* Notify scsi mid layer of any removed devices */
1075 for (i = 0; i < nremoved; i++) {
1076 struct scsi_device *sdev =
1077 scsi_device_lookup(sh, removed[i]->bus,
1078 removed[i]->target, removed[i]->lun);
1079 if (sdev != NULL) {
1080 scsi_remove_device(sdev);
1081 scsi_device_put(sdev);
1082 } else {
1083 /* We don't expect to get here.
1084 * future cmds to this device will get selection
1085 * timeout as if the device was gone.
1086 */
1087 dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
1088 " for removal.", hostno, removed[i]->bus,
1089 removed[i]->target, removed[i]->lun);
1090 }
1091 kfree(removed[i]);
1092 removed[i] = NULL;
1093 }
1094
1095 /* Notify scsi mid layer of any added devices */
1096 for (i = 0; i < nadded; i++) {
1097 if (scsi_add_device(sh, added[i]->bus,
1098 added[i]->target, added[i]->lun) == 0)
1099 continue;
1100 dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
1101 "device not added.\n", hostno, added[i]->bus,
1102 added[i]->target, added[i]->lun);
1103 /* now we have to remove it from h->dev,
1104 * since it didn't get added to scsi mid layer
1105 */
1106 fixup_botched_add(h, added[i]);
1107 }
1108
1109free_and_out:
1110 kfree(added);
1111 kfree(removed);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001112}
1113
1114/*
Joe Perches9e03aa22013-09-03 13:45:58 -07001115 * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001116 * Assume's h->devlock is held.
1117 */
1118static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1119 int bus, int target, int lun)
1120{
1121 int i;
1122 struct hpsa_scsi_dev_t *sd;
1123
1124 for (i = 0; i < h->ndevices; i++) {
1125 sd = h->dev[i];
1126 if (sd->bus == bus && sd->target == target && sd->lun == lun)
1127 return sd;
1128 }
1129 return NULL;
1130}
1131
1132/* link sdev->hostdata to our per-device structure. */
1133static int hpsa_slave_alloc(struct scsi_device *sdev)
1134{
1135 struct hpsa_scsi_dev_t *sd;
1136 unsigned long flags;
1137 struct ctlr_info *h;
1138
1139 h = sdev_to_hba(sdev);
1140 spin_lock_irqsave(&h->devlock, flags);
1141 sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1142 sdev_id(sdev), sdev->lun);
1143 if (sd != NULL)
1144 sdev->hostdata = sd;
1145 spin_unlock_irqrestore(&h->devlock, flags);
1146 return 0;
1147}
1148
1149static void hpsa_slave_destroy(struct scsi_device *sdev)
1150{
Stephen M. Cameronbcc44252010-02-04 08:41:54 -06001151 /* nothing to do. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001152}
1153
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001154static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
1155{
1156 int i;
1157
1158 if (!h->cmd_sg_list)
1159 return;
1160 for (i = 0; i < h->nr_cmds; i++) {
1161 kfree(h->cmd_sg_list[i]);
1162 h->cmd_sg_list[i] = NULL;
1163 }
1164 kfree(h->cmd_sg_list);
1165 h->cmd_sg_list = NULL;
1166}
1167
1168static int hpsa_allocate_sg_chain_blocks(struct ctlr_info *h)
1169{
1170 int i;
1171
1172 if (h->chainsize <= 0)
1173 return 0;
1174
1175 h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
1176 GFP_KERNEL);
1177 if (!h->cmd_sg_list)
1178 return -ENOMEM;
1179 for (i = 0; i < h->nr_cmds; i++) {
1180 h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
1181 h->chainsize, GFP_KERNEL);
1182 if (!h->cmd_sg_list[i])
1183 goto clean;
1184 }
1185 return 0;
1186
1187clean:
1188 hpsa_free_sg_chain_blocks(h);
1189 return -ENOMEM;
1190}
1191
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001192static int hpsa_map_sg_chain_block(struct ctlr_info *h,
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001193 struct CommandList *c)
1194{
1195 struct SGDescriptor *chain_sg, *chain_block;
1196 u64 temp64;
1197
1198 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
1199 chain_block = h->cmd_sg_list[c->cmdindex];
1200 chain_sg->Ext = HPSA_SG_CHAIN;
1201 chain_sg->Len = sizeof(*chain_sg) *
1202 (c->Header.SGTotal - h->max_cmd_sg_entries);
1203 temp64 = pci_map_single(h->pdev, chain_block, chain_sg->Len,
1204 PCI_DMA_TODEVICE);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001205 if (dma_mapping_error(&h->pdev->dev, temp64)) {
1206 /* prevent subsequent unmapping */
1207 chain_sg->Addr.lower = 0;
1208 chain_sg->Addr.upper = 0;
1209 return -1;
1210 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001211 chain_sg->Addr.lower = (u32) (temp64 & 0x0FFFFFFFFULL);
1212 chain_sg->Addr.upper = (u32) ((temp64 >> 32) & 0x0FFFFFFFFULL);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001213 return 0;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001214}
1215
1216static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
1217 struct CommandList *c)
1218{
1219 struct SGDescriptor *chain_sg;
1220 union u64bit temp64;
1221
1222 if (c->Header.SGTotal <= h->max_cmd_sg_entries)
1223 return;
1224
1225 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
1226 temp64.val32.lower = chain_sg->Addr.lower;
1227 temp64.val32.upper = chain_sg->Addr.upper;
1228 pci_unmap_single(h->pdev, temp64.val, chain_sg->Len, PCI_DMA_TODEVICE);
1229}
1230
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05001231static void complete_scsi_command(struct CommandList *cp)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001232{
1233 struct scsi_cmnd *cmd;
1234 struct ctlr_info *h;
1235 struct ErrorInfo *ei;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001236 struct hpsa_scsi_dev_t *dev;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001237
1238 unsigned char sense_key;
1239 unsigned char asc; /* additional sense code */
1240 unsigned char ascq; /* additional sense code qualifier */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05001241 unsigned long sense_data_size;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001242
1243 ei = cp->err_info;
1244 cmd = (struct scsi_cmnd *) cp->scsi_cmd;
1245 h = cp->h;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001246 dev = cmd->device->hostdata;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001247
1248 scsi_dma_unmap(cmd); /* undo the DMA mappings */
Matt Gatese1f7de02014-02-18 13:55:17 -06001249 if ((cp->cmd_type == CMD_SCSI) &&
1250 (cp->Header.SGTotal > h->max_cmd_sg_entries))
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001251 hpsa_unmap_sg_chain_block(h, cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001252
1253 cmd->result = (DID_OK << 16); /* host byte */
1254 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
Stephen M. Cameron55126722010-02-25 14:03:01 -06001255 cmd->result |= ei->ScsiStatus;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001256
1257 /* copy the sense data whether we need to or not. */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05001258 if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
1259 sense_data_size = SCSI_SENSE_BUFFERSIZE;
1260 else
1261 sense_data_size = sizeof(ei->SenseInfo);
1262 if (ei->SenseLen < sense_data_size)
1263 sense_data_size = ei->SenseLen;
1264
1265 memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001266 scsi_set_resid(cmd, ei->ResidualCnt);
1267
1268 if (ei->CommandStatus == 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001269 cmd_free(h, cp);
Tomas Henzl2cc5bfa2013-08-01 15:14:00 +02001270 cmd->scsi_done(cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001271 return;
1272 }
1273
Matt Gatese1f7de02014-02-18 13:55:17 -06001274 /* For I/O accelerator commands, copy over some fields to the normal
1275 * CISS header used below for error handling.
1276 */
1277 if (cp->cmd_type == CMD_IOACCEL1) {
1278 struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
1279 cp->Header.SGList = cp->Header.SGTotal = scsi_sg_count(cmd);
1280 cp->Request.CDBLen = c->io_flags & IOACCEL1_IOFLAGS_CDBLEN_MASK;
1281 cp->Header.Tag.lower = c->Tag.lower;
1282 cp->Header.Tag.upper = c->Tag.upper;
1283 memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
1284 memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001285
1286 /* Any RAID offload error results in retry which will use
1287 * the normal I/O path so the controller can handle whatever's
1288 * wrong.
1289 */
1290 if (is_logical_dev_addr_mode(dev->scsi3addr)) {
1291 if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
1292 dev->offload_enabled = 0;
1293 cmd->result = DID_SOFT_ERROR << 16;
1294 cmd_free(h, cp);
1295 cmd->scsi_done(cmd);
1296 return;
1297 }
Matt Gatese1f7de02014-02-18 13:55:17 -06001298 }
1299
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001300 /* an error has occurred */
1301 switch (ei->CommandStatus) {
1302
1303 case CMD_TARGET_STATUS:
1304 if (ei->ScsiStatus) {
1305 /* Get sense key */
1306 sense_key = 0xf & ei->SenseInfo[2];
1307 /* Get additional sense code */
1308 asc = ei->SenseInfo[12];
1309 /* Get addition sense code qualifier */
1310 ascq = ei->SenseInfo[13];
1311 }
1312
1313 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
Matt Gates3ce438d2013-12-04 17:10:36 -06001314 if (check_for_unit_attention(h, cp))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001315 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001316 if (sense_key == ILLEGAL_REQUEST) {
1317 /*
1318 * SCSI REPORT_LUNS is commonly unsupported on
1319 * Smart Array. Suppress noisy complaint.
1320 */
1321 if (cp->Request.CDB[0] == REPORT_LUNS)
1322 break;
1323
1324 /* If ASC/ASCQ indicate Logical Unit
1325 * Not Supported condition,
1326 */
1327 if ((asc == 0x25) && (ascq == 0x0)) {
1328 dev_warn(&h->pdev->dev, "cp %p "
1329 "has check condition\n", cp);
1330 break;
1331 }
1332 }
1333
1334 if (sense_key == NOT_READY) {
1335 /* If Sense is Not Ready, Logical Unit
1336 * Not ready, Manual Intervention
1337 * required
1338 */
1339 if ((asc == 0x04) && (ascq == 0x03)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001340 dev_warn(&h->pdev->dev, "cp %p "
1341 "has check condition: unit "
1342 "not ready, manual "
1343 "intervention required\n", cp);
1344 break;
1345 }
1346 }
Matt Gates1d3b3602010-02-04 08:43:00 -06001347 if (sense_key == ABORTED_COMMAND) {
1348 /* Aborted command is retryable */
1349 dev_warn(&h->pdev->dev, "cp %p "
1350 "has check condition: aborted command: "
1351 "ASC: 0x%x, ASCQ: 0x%x\n",
1352 cp, asc, ascq);
Stephen M. Cameron2e311fb2013-09-23 13:33:41 -05001353 cmd->result |= DID_SOFT_ERROR << 16;
Matt Gates1d3b3602010-02-04 08:43:00 -06001354 break;
1355 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001356 /* Must be some other type of check condition */
Stephen M. Cameron21b8e4e2012-05-01 11:42:25 -05001357 dev_dbg(&h->pdev->dev, "cp %p has check condition: "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001358 "unknown type: "
1359 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1360 "Returning result: 0x%x, "
1361 "cmd=[%02x %02x %02x %02x %02x "
Mike Miller807be732010-02-04 08:43:26 -06001362 "%02x %02x %02x %02x %02x %02x "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001363 "%02x %02x %02x %02x %02x]\n",
1364 cp, sense_key, asc, ascq,
1365 cmd->result,
1366 cmd->cmnd[0], cmd->cmnd[1],
1367 cmd->cmnd[2], cmd->cmnd[3],
1368 cmd->cmnd[4], cmd->cmnd[5],
1369 cmd->cmnd[6], cmd->cmnd[7],
Mike Miller807be732010-02-04 08:43:26 -06001370 cmd->cmnd[8], cmd->cmnd[9],
1371 cmd->cmnd[10], cmd->cmnd[11],
1372 cmd->cmnd[12], cmd->cmnd[13],
1373 cmd->cmnd[14], cmd->cmnd[15]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001374 break;
1375 }
1376
1377
1378 /* Problem was not a check condition
1379 * Pass it up to the upper layers...
1380 */
1381 if (ei->ScsiStatus) {
1382 dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
1383 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1384 "Returning result: 0x%x\n",
1385 cp, ei->ScsiStatus,
1386 sense_key, asc, ascq,
1387 cmd->result);
1388 } else { /* scsi status is zero??? How??? */
1389 dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
1390 "Returning no connection.\n", cp),
1391
1392 /* Ordinarily, this case should never happen,
1393 * but there is a bug in some released firmware
1394 * revisions that allows it to happen if, for
1395 * example, a 4100 backplane loses power and
1396 * the tape drive is in it. We assume that
1397 * it's a fatal error of some kind because we
1398 * can't show that it wasn't. We will make it
1399 * look like selection timeout since that is
1400 * the most common reason for this to occur,
1401 * and it's severe enough.
1402 */
1403
1404 cmd->result = DID_NO_CONNECT << 16;
1405 }
1406 break;
1407
1408 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1409 break;
1410 case CMD_DATA_OVERRUN:
1411 dev_warn(&h->pdev->dev, "cp %p has"
1412 " completed with data overrun "
1413 "reported\n", cp);
1414 break;
1415 case CMD_INVALID: {
1416 /* print_bytes(cp, sizeof(*cp), 1, 0);
1417 print_cmd(cp); */
1418 /* We get CMD_INVALID if you address a non-existent device
1419 * instead of a selection timeout (no response). You will
1420 * see this if you yank out a drive, then try to access it.
1421 * This is kind of a shame because it means that any other
1422 * CMD_INVALID (e.g. driver bug) will get interpreted as a
1423 * missing target. */
1424 cmd->result = DID_NO_CONNECT << 16;
1425 }
1426 break;
1427 case CMD_PROTOCOL_ERR:
Stephen M. Cameron256d0ea2012-09-14 16:34:25 -05001428 cmd->result = DID_ERROR << 16;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001429 dev_warn(&h->pdev->dev, "cp %p has "
Stephen M. Cameron256d0ea2012-09-14 16:34:25 -05001430 "protocol error\n", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001431 break;
1432 case CMD_HARDWARE_ERR:
1433 cmd->result = DID_ERROR << 16;
1434 dev_warn(&h->pdev->dev, "cp %p had hardware error\n", cp);
1435 break;
1436 case CMD_CONNECTION_LOST:
1437 cmd->result = DID_ERROR << 16;
1438 dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
1439 break;
1440 case CMD_ABORTED:
1441 cmd->result = DID_ABORT << 16;
1442 dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
1443 cp, ei->ScsiStatus);
1444 break;
1445 case CMD_ABORT_FAILED:
1446 cmd->result = DID_ERROR << 16;
1447 dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
1448 break;
1449 case CMD_UNSOLICITED_ABORT:
Stephen M. Cameronf6e76052011-07-26 11:08:52 -05001450 cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
1451 dev_warn(&h->pdev->dev, "cp %p aborted due to an unsolicited "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001452 "abort\n", cp);
1453 break;
1454 case CMD_TIMEOUT:
1455 cmd->result = DID_TIME_OUT << 16;
1456 dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
1457 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06001458 case CMD_UNABORTABLE:
1459 cmd->result = DID_ERROR << 16;
1460 dev_warn(&h->pdev->dev, "Command unabortable\n");
1461 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001462 case CMD_IOACCEL_DISABLED:
1463 /* This only handles the direct pass-through case since RAID
1464 * offload is handled above. Just attempt a retry.
1465 */
1466 cmd->result = DID_SOFT_ERROR << 16;
1467 dev_warn(&h->pdev->dev,
1468 "cp %p had HP SSD Smart Path error\n", cp);
1469 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001470 default:
1471 cmd->result = DID_ERROR << 16;
1472 dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
1473 cp, ei->CommandStatus);
1474 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001475 cmd_free(h, cp);
Tomas Henzl2cc5bfa2013-08-01 15:14:00 +02001476 cmd->scsi_done(cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001477}
1478
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001479static void hpsa_pci_unmap(struct pci_dev *pdev,
1480 struct CommandList *c, int sg_used, int data_direction)
1481{
1482 int i;
1483 union u64bit addr64;
1484
1485 for (i = 0; i < sg_used; i++) {
1486 addr64.val32.lower = c->SG[i].Addr.lower;
1487 addr64.val32.upper = c->SG[i].Addr.upper;
1488 pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len,
1489 data_direction);
1490 }
1491}
1492
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001493static int hpsa_map_one(struct pci_dev *pdev,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001494 struct CommandList *cp,
1495 unsigned char *buf,
1496 size_t buflen,
1497 int data_direction)
1498{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001499 u64 addr64;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001500
1501 if (buflen == 0 || data_direction == PCI_DMA_NONE) {
1502 cp->Header.SGList = 0;
1503 cp->Header.SGTotal = 0;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001504 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001505 }
1506
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001507 addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction);
Shuah Khaneceaae12013-02-20 11:24:34 -06001508 if (dma_mapping_error(&pdev->dev, addr64)) {
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001509 /* Prevent subsequent unmap of something never mapped */
Shuah Khaneceaae12013-02-20 11:24:34 -06001510 cp->Header.SGList = 0;
1511 cp->Header.SGTotal = 0;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001512 return -1;
Shuah Khaneceaae12013-02-20 11:24:34 -06001513 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001514 cp->SG[0].Addr.lower =
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001515 (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001516 cp->SG[0].Addr.upper =
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001517 (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001518 cp->SG[0].Len = buflen;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06001519 cp->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining */
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001520 cp->Header.SGList = (u8) 1; /* no. SGs contig in this cmd */
1521 cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001522 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001523}
1524
1525static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
1526 struct CommandList *c)
1527{
1528 DECLARE_COMPLETION_ONSTACK(wait);
1529
1530 c->waiting = &wait;
1531 enqueue_cmd_and_start_io(h, c);
1532 wait_for_completion(&wait);
1533}
1534
Stephen M. Camerona0c12412011-10-26 16:22:04 -05001535static void hpsa_scsi_do_simple_cmd_core_if_no_lockup(struct ctlr_info *h,
1536 struct CommandList *c)
1537{
1538 unsigned long flags;
1539
1540 /* If controller lockup detected, fake a hardware error. */
1541 spin_lock_irqsave(&h->lock, flags);
1542 if (unlikely(h->lockup_detected)) {
1543 spin_unlock_irqrestore(&h->lock, flags);
1544 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
1545 } else {
1546 spin_unlock_irqrestore(&h->lock, flags);
1547 hpsa_scsi_do_simple_cmd_core(h, c);
1548 }
1549}
1550
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001551#define MAX_DRIVER_CMD_RETRIES 25
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001552static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
1553 struct CommandList *c, int data_direction)
1554{
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001555 int backoff_time = 10, retry_count = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001556
1557 do {
Joe Perches7630abd2011-05-08 23:32:40 -07001558 memset(c->err_info, 0, sizeof(*c->err_info));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001559 hpsa_scsi_do_simple_cmd_core(h, c);
1560 retry_count++;
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001561 if (retry_count > 3) {
1562 msleep(backoff_time);
1563 if (backoff_time < 1000)
1564 backoff_time *= 2;
1565 }
Matt Bondurant852af202012-05-01 11:42:35 -05001566 } while ((check_for_unit_attention(h, c) ||
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001567 check_for_busy(h, c)) &&
1568 retry_count <= MAX_DRIVER_CMD_RETRIES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001569 hpsa_pci_unmap(h->pdev, c, 1, data_direction);
1570}
1571
1572static void hpsa_scsi_interpret_error(struct CommandList *cp)
1573{
1574 struct ErrorInfo *ei;
1575 struct device *d = &cp->h->pdev->dev;
1576
1577 ei = cp->err_info;
1578 switch (ei->CommandStatus) {
1579 case CMD_TARGET_STATUS:
1580 dev_warn(d, "cmd %p has completed with errors\n", cp);
1581 dev_warn(d, "cmd %p has SCSI Status = %x\n", cp,
1582 ei->ScsiStatus);
1583 if (ei->ScsiStatus == 0)
1584 dev_warn(d, "SCSI status is abnormally zero. "
1585 "(probably indicates selection timeout "
1586 "reported incorrectly due to a known "
1587 "firmware bug, circa July, 2001.)\n");
1588 break;
1589 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1590 dev_info(d, "UNDERRUN\n");
1591 break;
1592 case CMD_DATA_OVERRUN:
1593 dev_warn(d, "cp %p has completed with data overrun\n", cp);
1594 break;
1595 case CMD_INVALID: {
1596 /* controller unfortunately reports SCSI passthru's
1597 * to non-existent targets as invalid commands.
1598 */
1599 dev_warn(d, "cp %p is reported invalid (probably means "
1600 "target device no longer present)\n", cp);
1601 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
1602 print_cmd(cp); */
1603 }
1604 break;
1605 case CMD_PROTOCOL_ERR:
1606 dev_warn(d, "cp %p has protocol error \n", cp);
1607 break;
1608 case CMD_HARDWARE_ERR:
1609 /* cmd->result = DID_ERROR << 16; */
1610 dev_warn(d, "cp %p had hardware error\n", cp);
1611 break;
1612 case CMD_CONNECTION_LOST:
1613 dev_warn(d, "cp %p had connection lost\n", cp);
1614 break;
1615 case CMD_ABORTED:
1616 dev_warn(d, "cp %p was aborted\n", cp);
1617 break;
1618 case CMD_ABORT_FAILED:
1619 dev_warn(d, "cp %p reports abort failed\n", cp);
1620 break;
1621 case CMD_UNSOLICITED_ABORT:
1622 dev_warn(d, "cp %p aborted due to an unsolicited abort\n", cp);
1623 break;
1624 case CMD_TIMEOUT:
1625 dev_warn(d, "cp %p timed out\n", cp);
1626 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06001627 case CMD_UNABORTABLE:
1628 dev_warn(d, "Command unabortable\n");
1629 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001630 default:
1631 dev_warn(d, "cp %p returned unknown status %x\n", cp,
1632 ei->CommandStatus);
1633 }
1634}
1635
1636static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
1637 unsigned char page, unsigned char *buf,
1638 unsigned char bufsize)
1639{
1640 int rc = IO_OK;
1641 struct CommandList *c;
1642 struct ErrorInfo *ei;
1643
1644 c = cmd_special_alloc(h);
1645
1646 if (c == NULL) { /* trouble... */
1647 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06001648 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001649 }
1650
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001651 if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
1652 page, scsi3addr, TYPE_CMD)) {
1653 rc = -1;
1654 goto out;
1655 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001656 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1657 ei = c->err_info;
1658 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
1659 hpsa_scsi_interpret_error(c);
1660 rc = -1;
1661 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001662out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001663 cmd_special_free(h, c);
1664 return rc;
1665}
1666
1667static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr)
1668{
1669 int rc = IO_OK;
1670 struct CommandList *c;
1671 struct ErrorInfo *ei;
1672
1673 c = cmd_special_alloc(h);
1674
1675 if (c == NULL) { /* trouble... */
1676 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
Stephen M. Camerone9ea04a2010-02-25 14:03:06 -06001677 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001678 }
1679
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001680 /* fill_cmd can't fail here, no data buffer to map. */
1681 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h,
1682 NULL, 0, 0, scsi3addr, TYPE_MSG);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001683 hpsa_scsi_do_simple_cmd_core(h, c);
1684 /* no unmap needed here because no data xfer. */
1685
1686 ei = c->err_info;
1687 if (ei->CommandStatus != 0) {
1688 hpsa_scsi_interpret_error(c);
1689 rc = -1;
1690 }
1691 cmd_special_free(h, c);
1692 return rc;
1693}
1694
1695static void hpsa_get_raid_level(struct ctlr_info *h,
1696 unsigned char *scsi3addr, unsigned char *raid_level)
1697{
1698 int rc;
1699 unsigned char *buf;
1700
1701 *raid_level = RAID_UNKNOWN;
1702 buf = kzalloc(64, GFP_KERNEL);
1703 if (!buf)
1704 return;
1705 rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0xC1, buf, 64);
1706 if (rc == 0)
1707 *raid_level = buf[8];
1708 if (*raid_level > RAID_UNKNOWN)
1709 *raid_level = RAID_UNKNOWN;
1710 kfree(buf);
1711 return;
1712}
1713
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001714#define HPSA_MAP_DEBUG
1715#ifdef HPSA_MAP_DEBUG
1716static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
1717 struct raid_map_data *map_buff)
1718{
1719 struct raid_map_disk_data *dd = &map_buff->data[0];
1720 int map, row, col;
1721 u16 map_cnt, row_cnt, disks_per_row;
1722
1723 if (rc != 0)
1724 return;
1725
1726 dev_info(&h->pdev->dev, "structure_size = %u\n",
1727 le32_to_cpu(map_buff->structure_size));
1728 dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
1729 le32_to_cpu(map_buff->volume_blk_size));
1730 dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
1731 le64_to_cpu(map_buff->volume_blk_cnt));
1732 dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
1733 map_buff->phys_blk_shift);
1734 dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
1735 map_buff->parity_rotation_shift);
1736 dev_info(&h->pdev->dev, "strip_size = %u\n",
1737 le16_to_cpu(map_buff->strip_size));
1738 dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
1739 le64_to_cpu(map_buff->disk_starting_blk));
1740 dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
1741 le64_to_cpu(map_buff->disk_blk_cnt));
1742 dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
1743 le16_to_cpu(map_buff->data_disks_per_row));
1744 dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
1745 le16_to_cpu(map_buff->metadata_disks_per_row));
1746 dev_info(&h->pdev->dev, "row_cnt = %u\n",
1747 le16_to_cpu(map_buff->row_cnt));
1748 dev_info(&h->pdev->dev, "layout_map_count = %u\n",
1749 le16_to_cpu(map_buff->layout_map_count));
1750
1751 map_cnt = le16_to_cpu(map_buff->layout_map_count);
1752 for (map = 0; map < map_cnt; map++) {
1753 dev_info(&h->pdev->dev, "Map%u:\n", map);
1754 row_cnt = le16_to_cpu(map_buff->row_cnt);
1755 for (row = 0; row < row_cnt; row++) {
1756 dev_info(&h->pdev->dev, " Row%u:\n", row);
1757 disks_per_row =
1758 le16_to_cpu(map_buff->data_disks_per_row);
1759 for (col = 0; col < disks_per_row; col++, dd++)
1760 dev_info(&h->pdev->dev,
1761 " D%02u: h=0x%04x xor=%u,%u\n",
1762 col, dd->ioaccel_handle,
1763 dd->xor_mult[0], dd->xor_mult[1]);
1764 disks_per_row =
1765 le16_to_cpu(map_buff->metadata_disks_per_row);
1766 for (col = 0; col < disks_per_row; col++, dd++)
1767 dev_info(&h->pdev->dev,
1768 " M%02u: h=0x%04x xor=%u,%u\n",
1769 col, dd->ioaccel_handle,
1770 dd->xor_mult[0], dd->xor_mult[1]);
1771 }
1772 }
1773}
1774#else
1775static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
1776 __attribute__((unused)) int rc,
1777 __attribute__((unused)) struct raid_map_data *map_buff)
1778{
1779}
1780#endif
1781
1782static int hpsa_get_raid_map(struct ctlr_info *h,
1783 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
1784{
1785 int rc = 0;
1786 struct CommandList *c;
1787 struct ErrorInfo *ei;
1788
1789 c = cmd_special_alloc(h);
1790 if (c == NULL) {
1791 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1792 return -ENOMEM;
1793 }
1794 if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
1795 sizeof(this_device->raid_map), 0,
1796 scsi3addr, TYPE_CMD)) {
1797 dev_warn(&h->pdev->dev, "Out of memory in hpsa_get_raid_map()\n");
1798 cmd_special_free(h, c);
1799 return -ENOMEM;
1800 }
1801 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1802 ei = c->err_info;
1803 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
1804 hpsa_scsi_interpret_error(c);
1805 cmd_special_free(h, c);
1806 return -1;
1807 }
1808 cmd_special_free(h, c);
1809
1810 /* @todo in the future, dynamically allocate RAID map memory */
1811 if (le32_to_cpu(this_device->raid_map.structure_size) >
1812 sizeof(this_device->raid_map)) {
1813 dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
1814 rc = -1;
1815 }
1816 hpsa_debug_map_buff(h, rc, &this_device->raid_map);
1817 return rc;
1818}
1819
1820static void hpsa_get_ioaccel_status(struct ctlr_info *h,
1821 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
1822{
1823 int rc;
1824 unsigned char *buf;
1825 u8 ioaccel_status;
1826
1827 this_device->offload_config = 0;
1828 this_device->offload_enabled = 0;
1829
1830 buf = kzalloc(64, GFP_KERNEL);
1831 if (!buf)
1832 return;
1833 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
1834 HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
1835 if (rc != 0)
1836 goto out;
1837
1838#define IOACCEL_STATUS_BYTE 4
1839#define OFFLOAD_CONFIGURED_BIT 0x01
1840#define OFFLOAD_ENABLED_BIT 0x02
1841 ioaccel_status = buf[IOACCEL_STATUS_BYTE];
1842 this_device->offload_config =
1843 !!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
1844 if (this_device->offload_config) {
1845 this_device->offload_enabled =
1846 !!(ioaccel_status & OFFLOAD_ENABLED_BIT);
1847 if (hpsa_get_raid_map(h, scsi3addr, this_device))
1848 this_device->offload_enabled = 0;
1849 }
1850out:
1851 kfree(buf);
1852 return;
1853}
1854
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001855/* Get the device id from inquiry page 0x83 */
1856static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
1857 unsigned char *device_id, int buflen)
1858{
1859 int rc;
1860 unsigned char *buf;
1861
1862 if (buflen > 16)
1863 buflen = 16;
1864 buf = kzalloc(64, GFP_KERNEL);
1865 if (!buf)
1866 return -1;
1867 rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0x83, buf, 64);
1868 if (rc == 0)
1869 memcpy(device_id, &buf[8], buflen);
1870 kfree(buf);
1871 return rc != 0;
1872}
1873
1874static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
1875 struct ReportLUNdata *buf, int bufsize,
1876 int extended_response)
1877{
1878 int rc = IO_OK;
1879 struct CommandList *c;
1880 unsigned char scsi3addr[8];
1881 struct ErrorInfo *ei;
1882
1883 c = cmd_special_alloc(h);
1884 if (c == NULL) { /* trouble... */
1885 dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1886 return -1;
1887 }
Stephen M. Camerone89c0ae2010-02-04 08:42:04 -06001888 /* address the controller */
1889 memset(scsi3addr, 0, sizeof(scsi3addr));
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001890 if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
1891 buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
1892 rc = -1;
1893 goto out;
1894 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001895 if (extended_response)
1896 c->Request.CDB[1] = extended_response;
1897 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1898 ei = c->err_info;
1899 if (ei->CommandStatus != 0 &&
1900 ei->CommandStatus != CMD_DATA_UNDERRUN) {
1901 hpsa_scsi_interpret_error(c);
1902 rc = -1;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001903 } else {
1904 if (buf->extended_response_flag != extended_response) {
1905 dev_err(&h->pdev->dev,
1906 "report luns requested format %u, got %u\n",
1907 extended_response,
1908 buf->extended_response_flag);
1909 rc = -1;
1910 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001911 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001912out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001913 cmd_special_free(h, c);
1914 return rc;
1915}
1916
1917static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
1918 struct ReportLUNdata *buf,
1919 int bufsize, int extended_response)
1920{
1921 return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
1922}
1923
1924static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
1925 struct ReportLUNdata *buf, int bufsize)
1926{
1927 return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
1928}
1929
1930static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
1931 int bus, int target, int lun)
1932{
1933 device->bus = bus;
1934 device->target = target;
1935 device->lun = lun;
1936}
1937
1938static int hpsa_update_device_info(struct ctlr_info *h,
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05001939 unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
1940 unsigned char *is_OBDR_device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001941{
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05001942
1943#define OBDR_SIG_OFFSET 43
1944#define OBDR_TAPE_SIG "$DR-10"
1945#define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
1946#define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
1947
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06001948 unsigned char *inq_buff;
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05001949 unsigned char *obdr_sig;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001950
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06001951 inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001952 if (!inq_buff)
1953 goto bail_out;
1954
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001955 /* Do an inquiry to the device to see what it is. */
1956 if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
1957 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
1958 /* Inquiry failed (msg printed already) */
1959 dev_err(&h->pdev->dev,
1960 "hpsa_update_device_info: inquiry failed\n");
1961 goto bail_out;
1962 }
1963
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001964 this_device->devtype = (inq_buff[0] & 0x1f);
1965 memcpy(this_device->scsi3addr, scsi3addr, 8);
1966 memcpy(this_device->vendor, &inq_buff[8],
1967 sizeof(this_device->vendor));
1968 memcpy(this_device->model, &inq_buff[16],
1969 sizeof(this_device->model));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001970 memset(this_device->device_id, 0,
1971 sizeof(this_device->device_id));
1972 hpsa_get_device_id(h, scsi3addr, this_device->device_id,
1973 sizeof(this_device->device_id));
1974
1975 if (this_device->devtype == TYPE_DISK &&
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001976 is_logical_dev_addr_mode(scsi3addr)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001977 hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001978 if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
1979 hpsa_get_ioaccel_status(h, scsi3addr, this_device);
1980 } else {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001981 this_device->raid_level = RAID_UNKNOWN;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001982 this_device->offload_config = 0;
1983 this_device->offload_enabled = 0;
1984 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001985
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05001986 if (is_OBDR_device) {
1987 /* See if this is a One-Button-Disaster-Recovery device
1988 * by looking for "$DR-10" at offset 43 in inquiry data.
1989 */
1990 obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
1991 *is_OBDR_device = (this_device->devtype == TYPE_ROM &&
1992 strncmp(obdr_sig, OBDR_TAPE_SIG,
1993 OBDR_SIG_LEN) == 0);
1994 }
1995
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001996 kfree(inq_buff);
1997 return 0;
1998
1999bail_out:
2000 kfree(inq_buff);
2001 return 1;
2002}
2003
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002004static unsigned char *ext_target_model[] = {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002005 "MSA2012",
2006 "MSA2024",
2007 "MSA2312",
2008 "MSA2324",
Stephen M. Cameronfda38512011-05-03 15:00:07 -05002009 "P2000 G3 SAS",
Stephen M. Camerone06c8e52013-09-23 13:33:56 -05002010 "MSA 2040 SAS",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002011 NULL,
2012};
2013
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002014static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002015{
2016 int i;
2017
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002018 for (i = 0; ext_target_model[i]; i++)
2019 if (strncmp(device->model, ext_target_model[i],
2020 strlen(ext_target_model[i])) == 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002021 return 1;
2022 return 0;
2023}
2024
2025/* Helper function to assign bus, target, lun mapping of devices.
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002026 * Puts non-external target logical volumes on bus 0, external target logical
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002027 * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
2028 * Logical drive target and lun are assigned at this time, but
2029 * physical device lun and target assignment are deferred (assigned
2030 * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
2031 */
2032static void figure_bus_target_lun(struct ctlr_info *h,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002033 u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002034{
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002035 u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002036
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002037 if (!is_logical_dev_addr_mode(lunaddrbytes)) {
2038 /* physical device, target and lun filled in later */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002039 if (is_hba_lunid(lunaddrbytes))
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002040 hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002041 else
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002042 /* defer target, lun assignment for physical devices */
2043 hpsa_set_bus_target_lun(device, 2, -1, -1);
2044 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002045 }
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002046 /* It's a logical device */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002047 if (is_ext_target(h, device)) {
2048 /* external target way, put logicals on bus 1
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002049 * and match target/lun numbers box
2050 * reports, other smart array, bus 0, target 0, match lunid
2051 */
2052 hpsa_set_bus_target_lun(device,
2053 1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
2054 return;
2055 }
2056 hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002057}
2058
2059/*
2060 * If there is no lun 0 on a target, linux won't find any devices.
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002061 * For the external targets (arrays), we have to manually detect the enclosure
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002062 * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
2063 * it for some reason. *tmpdevice is the target we're adding,
2064 * this_device is a pointer into the current element of currentsd[]
2065 * that we're building up in update_scsi_devices(), below.
2066 * lunzerobits is a bitmap that tracks which targets already have a
2067 * lun 0 assigned.
2068 * Returns 1 if an enclosure was added, 0 if not.
2069 */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002070static int add_ext_target_dev(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002071 struct hpsa_scsi_dev_t *tmpdevice,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002072 struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002073 unsigned long lunzerobits[], int *n_ext_target_devs)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002074{
2075 unsigned char scsi3addr[8];
2076
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002077 if (test_bit(tmpdevice->target, lunzerobits))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002078 return 0; /* There is already a lun 0 on this target. */
2079
2080 if (!is_logical_dev_addr_mode(lunaddrbytes))
2081 return 0; /* It's the logical targets that may lack lun 0. */
2082
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002083 if (!is_ext_target(h, tmpdevice))
2084 return 0; /* Only external target devices have this problem. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002085
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002086 if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002087 return 0;
2088
Stephen M. Cameronc4f8a292011-01-07 10:55:43 -06002089 memset(scsi3addr, 0, 8);
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002090 scsi3addr[3] = tmpdevice->target;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002091 if (is_hba_lunid(scsi3addr))
2092 return 0; /* Don't add the RAID controller here. */
2093
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002094 if (is_scsi_rev_5(h))
2095 return 0; /* p1210m doesn't need to do this. */
2096
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002097 if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
Scott Teelaca4a522012-01-19 14:01:19 -06002098 dev_warn(&h->pdev->dev, "Maximum number of external "
2099 "target devices exceeded. Check your hardware "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002100 "configuration.");
2101 return 0;
2102 }
2103
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002104 if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002105 return 0;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002106 (*n_ext_target_devs)++;
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002107 hpsa_set_bus_target_lun(this_device,
2108 tmpdevice->bus, tmpdevice->target, 0);
2109 set_bit(tmpdevice->target, lunzerobits);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002110 return 1;
2111}
2112
2113/*
2114 * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
2115 * logdev. The number of luns in physdev and logdev are returned in
2116 * *nphysicals and *nlogicals, respectively.
2117 * Returns 0 on success, -1 otherwise.
2118 */
2119static int hpsa_gather_lun_info(struct ctlr_info *h,
2120 int reportlunsize,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002121 struct ReportLUNdata *physdev, u32 *nphysicals, int *physical_mode,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002122 struct ReportLUNdata *logdev, u32 *nlogicals)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002123{
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002124 int physical_entry_size = 8;
2125
2126 *physical_mode = 0;
2127
2128 /* For I/O accelerator mode we need to read physical device handles */
2129 if (h->transMethod & CFGTBL_Trans_io_accel1) {
2130 *physical_mode = HPSA_REPORT_PHYS_EXTENDED;
2131 physical_entry_size = 24;
2132 }
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002133 if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002134 *physical_mode)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002135 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
2136 return -1;
2137 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002138 *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) /
2139 physical_entry_size;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002140 if (*nphysicals > HPSA_MAX_PHYS_LUN) {
2141 dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
2142 " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2143 *nphysicals - HPSA_MAX_PHYS_LUN);
2144 *nphysicals = HPSA_MAX_PHYS_LUN;
2145 }
2146 if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
2147 dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
2148 return -1;
2149 }
Stephen M. Cameron6df1e952010-02-04 08:42:19 -06002150 *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002151 /* Reject Logicals in excess of our max capability. */
2152 if (*nlogicals > HPSA_MAX_LUN) {
2153 dev_warn(&h->pdev->dev,
2154 "maximum logical LUNs (%d) exceeded. "
2155 "%d LUNs ignored.\n", HPSA_MAX_LUN,
2156 *nlogicals - HPSA_MAX_LUN);
2157 *nlogicals = HPSA_MAX_LUN;
2158 }
2159 if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
2160 dev_warn(&h->pdev->dev,
2161 "maximum logical + physical LUNs (%d) exceeded. "
2162 "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2163 *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
2164 *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
2165 }
2166 return 0;
2167}
2168
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002169u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i,
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002170 int nphysicals, int nlogicals,
2171 struct ReportExtendedLUNdata *physdev_list,
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002172 struct ReportLUNdata *logdev_list)
2173{
2174 /* Helper function, figure out where the LUN ID info is coming from
2175 * given index i, lists of physical and logical devices, where in
2176 * the list the raid controller is supposed to appear (first or last)
2177 */
2178
2179 int logicals_start = nphysicals + (raid_ctlr_position == 0);
2180 int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
2181
2182 if (i == raid_ctlr_position)
2183 return RAID_CTLR_LUNID;
2184
2185 if (i < logicals_start)
2186 return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0];
2187
2188 if (i < last_device)
2189 return &logdev_list->LUN[i - nphysicals -
2190 (raid_ctlr_position == 0)][0];
2191 BUG();
2192 return NULL;
2193}
2194
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002195static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
2196{
2197 /* the idea here is we could get notified
2198 * that some devices have changed, so we do a report
2199 * physical luns and report logical luns cmd, and adjust
2200 * our list of devices accordingly.
2201 *
2202 * The scsi3addr's of devices won't change so long as the
2203 * adapter is not reset. That means we can rescan and
2204 * tell which devices we already know about, vs. new
2205 * devices, vs. disappearing devices.
2206 */
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002207 struct ReportExtendedLUNdata *physdev_list = NULL;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002208 struct ReportLUNdata *logdev_list = NULL;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002209 u32 nphysicals = 0;
2210 u32 nlogicals = 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002211 int physical_mode = 0;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002212 u32 ndev_allocated = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002213 struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
2214 int ncurrent = 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002215 int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 24;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002216 int i, n_ext_target_devs, ndevs_to_allocate;
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002217 int raid_ctlr_position;
Scott Teelaca4a522012-01-19 14:01:19 -06002218 DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002219
Scott Teelcfe5bad2011-10-26 16:21:07 -05002220 currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002221 physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
2222 logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002223 tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
2224
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002225 if (!currentsd || !physdev_list || !logdev_list || !tmpdevice) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002226 dev_err(&h->pdev->dev, "out of memory\n");
2227 goto out;
2228 }
2229 memset(lunzerobits, 0, sizeof(lunzerobits));
2230
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002231 if (hpsa_gather_lun_info(h, reportlunsize,
2232 (struct ReportLUNdata *) physdev_list, &nphysicals,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002233 &physical_mode, logdev_list, &nlogicals))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002234 goto out;
2235
Scott Teelaca4a522012-01-19 14:01:19 -06002236 /* We might see up to the maximum number of logical and physical disks
2237 * plus external target devices, and a device for the local RAID
2238 * controller.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002239 */
Scott Teelaca4a522012-01-19 14:01:19 -06002240 ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002241
2242 /* Allocate the per device structures */
2243 for (i = 0; i < ndevs_to_allocate; i++) {
Scott Teelb7ec0212011-10-26 16:21:12 -05002244 if (i >= HPSA_MAX_DEVICES) {
2245 dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
2246 " %d devices ignored.\n", HPSA_MAX_DEVICES,
2247 ndevs_to_allocate - HPSA_MAX_DEVICES);
2248 break;
2249 }
2250
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002251 currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
2252 if (!currentsd[i]) {
2253 dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
2254 __FILE__, __LINE__);
2255 goto out;
2256 }
2257 ndev_allocated++;
2258 }
2259
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002260 if (unlikely(is_scsi_rev_5(h)))
2261 raid_ctlr_position = 0;
2262 else
2263 raid_ctlr_position = nphysicals + nlogicals;
2264
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002265 /* adjust our table of devices */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002266 n_ext_target_devs = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002267 for (i = 0; i < nphysicals + nlogicals + 1; i++) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002268 u8 *lunaddrbytes, is_OBDR = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002269
2270 /* Figure out where the LUN ID info is coming from */
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002271 lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
2272 i, nphysicals, nlogicals, physdev_list, logdev_list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002273 /* skip masked physical devices. */
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002274 if (lunaddrbytes[3] & 0xC0 &&
2275 i < nphysicals + (raid_ctlr_position == 0))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002276 continue;
2277
2278 /* Get device type, vendor, model, device id */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002279 if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
2280 &is_OBDR))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002281 continue; /* skip it if we can't talk to it. */
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002282 figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002283 this_device = currentsd[ncurrent];
2284
2285 /*
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002286 * For external target devices, we have to insert a LUN 0 which
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002287 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
2288 * is nonetheless an enclosure device there. We have to
2289 * present that otherwise linux won't find anything if
2290 * there is no lun 0.
2291 */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002292 if (add_ext_target_dev(h, tmpdevice, this_device,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002293 lunaddrbytes, lunzerobits,
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002294 &n_ext_target_devs)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002295 ncurrent++;
2296 this_device = currentsd[ncurrent];
2297 }
2298
2299 *this_device = *tmpdevice;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002300
2301 switch (this_device->devtype) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002302 case TYPE_ROM:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002303 /* We don't *really* support actual CD-ROM devices,
2304 * just "One Button Disaster Recovery" tape drive
2305 * which temporarily pretends to be a CD-ROM drive.
2306 * So we check that the device is really an OBDR tape
2307 * device by checking for "$DR-10" in bytes 43-48 of
2308 * the inquiry data.
2309 */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002310 if (is_OBDR)
2311 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002312 break;
2313 case TYPE_DISK:
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002314 if (i >= nphysicals) {
2315 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002316 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002317 }
2318 if (physical_mode == HPSA_REPORT_PHYS_EXTENDED) {
2319 memcpy(&this_device->ioaccel_handle,
2320 &lunaddrbytes[20],
2321 sizeof(this_device->ioaccel_handle));
2322 ncurrent++;
2323 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002324 break;
2325 case TYPE_TAPE:
2326 case TYPE_MEDIUM_CHANGER:
2327 ncurrent++;
2328 break;
2329 case TYPE_RAID:
2330 /* Only present the Smartarray HBA as a RAID controller.
2331 * If it's a RAID controller other than the HBA itself
2332 * (an external RAID controller, MSA500 or similar)
2333 * don't present it.
2334 */
2335 if (!is_hba_lunid(lunaddrbytes))
2336 break;
2337 ncurrent++;
2338 break;
2339 default:
2340 break;
2341 }
Scott Teelcfe5bad2011-10-26 16:21:07 -05002342 if (ncurrent >= HPSA_MAX_DEVICES)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002343 break;
2344 }
2345 adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
2346out:
2347 kfree(tmpdevice);
2348 for (i = 0; i < ndev_allocated; i++)
2349 kfree(currentsd[i]);
2350 kfree(currentsd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002351 kfree(physdev_list);
2352 kfree(logdev_list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002353}
2354
2355/* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
2356 * dma mapping and fills in the scatter gather entries of the
2357 * hpsa command, cp.
2358 */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002359static int hpsa_scatter_gather(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002360 struct CommandList *cp,
2361 struct scsi_cmnd *cmd)
2362{
2363 unsigned int len;
2364 struct scatterlist *sg;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002365 u64 addr64;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002366 int use_sg, i, sg_index, chained;
2367 struct SGDescriptor *curr_sg;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002368
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002369 BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002370
2371 use_sg = scsi_dma_map(cmd);
2372 if (use_sg < 0)
2373 return use_sg;
2374
2375 if (!use_sg)
2376 goto sglist_finished;
2377
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002378 curr_sg = cp->SG;
2379 chained = 0;
2380 sg_index = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002381 scsi_for_each_sg(cmd, sg, use_sg, i) {
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002382 if (i == h->max_cmd_sg_entries - 1 &&
2383 use_sg > h->max_cmd_sg_entries) {
2384 chained = 1;
2385 curr_sg = h->cmd_sg_list[cp->cmdindex];
2386 sg_index = 0;
2387 }
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002388 addr64 = (u64) sg_dma_address(sg);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002389 len = sg_dma_len(sg);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002390 curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
2391 curr_sg->Addr.upper = (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
2392 curr_sg->Len = len;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06002393 curr_sg->Ext = (i < scsi_sg_count(cmd) - 1) ? 0 : HPSA_SG_LAST;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002394 curr_sg++;
2395 }
2396
2397 if (use_sg + chained > h->maxSG)
2398 h->maxSG = use_sg + chained;
2399
2400 if (chained) {
2401 cp->Header.SGList = h->max_cmd_sg_entries;
2402 cp->Header.SGTotal = (u16) (use_sg + 1);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002403 if (hpsa_map_sg_chain_block(h, cp)) {
2404 scsi_dma_unmap(cmd);
2405 return -1;
2406 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002407 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002408 }
2409
2410sglist_finished:
2411
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002412 cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
2413 cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002414 return 0;
2415}
2416
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002417#define IO_ACCEL_INELIGIBLE (1)
2418static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
2419{
2420 int is_write = 0;
2421 u32 block;
2422 u32 block_cnt;
2423
2424 /* Perform some CDB fixups if needed using 10 byte reads/writes only */
2425 switch (cdb[0]) {
2426 case WRITE_6:
2427 case WRITE_12:
2428 is_write = 1;
2429 case READ_6:
2430 case READ_12:
2431 if (*cdb_len == 6) {
2432 block = (((u32) cdb[2]) << 8) | cdb[3];
2433 block_cnt = cdb[4];
2434 } else {
2435 BUG_ON(*cdb_len != 12);
2436 block = (((u32) cdb[2]) << 24) |
2437 (((u32) cdb[3]) << 16) |
2438 (((u32) cdb[4]) << 8) |
2439 cdb[5];
2440 block_cnt =
2441 (((u32) cdb[6]) << 24) |
2442 (((u32) cdb[7]) << 16) |
2443 (((u32) cdb[8]) << 8) |
2444 cdb[9];
2445 }
2446 if (block_cnt > 0xffff)
2447 return IO_ACCEL_INELIGIBLE;
2448
2449 cdb[0] = is_write ? WRITE_10 : READ_10;
2450 cdb[1] = 0;
2451 cdb[2] = (u8) (block >> 24);
2452 cdb[3] = (u8) (block >> 16);
2453 cdb[4] = (u8) (block >> 8);
2454 cdb[5] = (u8) (block);
2455 cdb[6] = 0;
2456 cdb[7] = (u8) (block_cnt >> 8);
2457 cdb[8] = (u8) (block_cnt);
2458 cdb[9] = 0;
2459 *cdb_len = 10;
2460 break;
2461 }
2462 return 0;
2463}
2464
Matt Gatese1f7de02014-02-18 13:55:17 -06002465/*
2466 * Queue a command to the I/O accelerator path.
Matt Gatese1f7de02014-02-18 13:55:17 -06002467 */
2468static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002469 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
2470 u8 *scsi3addr)
Matt Gatese1f7de02014-02-18 13:55:17 -06002471{
2472 struct scsi_cmnd *cmd = c->scsi_cmd;
Matt Gatese1f7de02014-02-18 13:55:17 -06002473 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
2474 unsigned int len;
2475 unsigned int total_len = 0;
2476 struct scatterlist *sg;
2477 u64 addr64;
2478 int use_sg, i;
2479 struct SGDescriptor *curr_sg;
2480 u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
2481
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002482 /* TODO: implement chaining support */
2483 if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
2484 return IO_ACCEL_INELIGIBLE;
2485
Matt Gatese1f7de02014-02-18 13:55:17 -06002486 BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
2487
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002488 if (fixup_ioaccel_cdb(cdb, &cdb_len))
2489 return IO_ACCEL_INELIGIBLE;
2490
Matt Gatese1f7de02014-02-18 13:55:17 -06002491 c->cmd_type = CMD_IOACCEL1;
2492
2493 /* Adjust the DMA address to point to the accelerated command buffer */
2494 c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
2495 (c->cmdindex * sizeof(*cp));
2496 BUG_ON(c->busaddr & 0x0000007F);
2497
2498 use_sg = scsi_dma_map(cmd);
2499 if (use_sg < 0)
2500 return use_sg;
2501
2502 if (use_sg) {
2503 curr_sg = cp->SG;
2504 scsi_for_each_sg(cmd, sg, use_sg, i) {
2505 addr64 = (u64) sg_dma_address(sg);
2506 len = sg_dma_len(sg);
2507 total_len += len;
2508 curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
2509 curr_sg->Addr.upper =
2510 (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
2511 curr_sg->Len = len;
2512
2513 if (i == (scsi_sg_count(cmd) - 1))
2514 curr_sg->Ext = HPSA_SG_LAST;
2515 else
2516 curr_sg->Ext = 0; /* we are not chaining */
2517 curr_sg++;
2518 }
2519
2520 switch (cmd->sc_data_direction) {
2521 case DMA_TO_DEVICE:
2522 control |= IOACCEL1_CONTROL_DATA_OUT;
2523 break;
2524 case DMA_FROM_DEVICE:
2525 control |= IOACCEL1_CONTROL_DATA_IN;
2526 break;
2527 case DMA_NONE:
2528 control |= IOACCEL1_CONTROL_NODATAXFER;
2529 break;
2530 default:
2531 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
2532 cmd->sc_data_direction);
2533 BUG();
2534 break;
2535 }
2536 } else {
2537 control |= IOACCEL1_CONTROL_NODATAXFER;
2538 }
2539
2540 /* Fill out the command structure to submit */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002541 cp->dev_handle = ioaccel_handle & 0xFFFF;
Matt Gatese1f7de02014-02-18 13:55:17 -06002542 cp->transfer_len = total_len;
2543 cp->io_flags = IOACCEL1_IOFLAGS_IO_REQ |
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002544 (cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK);
Matt Gatese1f7de02014-02-18 13:55:17 -06002545 cp->control = control;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002546 memcpy(cp->CDB, cdb, cdb_len);
2547 memcpy(cp->CISS_LUN, scsi3addr, 8);
Matt Gatese1f7de02014-02-18 13:55:17 -06002548
2549 /* Tell the controller to post the reply to the queue for this
2550 * processor. This seems to give the best I/O throughput.
2551 */
2552 cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
2553
2554 /* Set the bits in the address sent down to include:
2555 * - performant mode bit (bit 0)
2556 * - pull count (bits 1-3)
2557 * - command type (bits 4-6)
2558 */
2559 c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[use_sg] << 1) |
2560 IOACCEL1_BUSADDR_CMDTYPE;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002561 enqueue_cmd_and_start_io(h, c);
Matt Gatese1f7de02014-02-18 13:55:17 -06002562 return 0;
2563}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002564
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002565/*
2566 * Queue a command directly to a device behind the controller using the
2567 * I/O accelerator path.
2568 */
2569static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
2570 struct CommandList *c)
2571{
2572 struct scsi_cmnd *cmd = c->scsi_cmd;
2573 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
2574
2575 return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
2576 cmd->cmnd, cmd->cmd_len, dev->scsi3addr);
2577}
2578
2579/*
2580 * Attempt to perform offload RAID mapping for a logical volume I/O.
2581 */
2582static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
2583 struct CommandList *c)
2584{
2585 struct scsi_cmnd *cmd = c->scsi_cmd;
2586 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
2587 struct raid_map_data *map = &dev->raid_map;
2588 struct raid_map_disk_data *dd = &map->data[0];
2589 int is_write = 0;
2590 u32 map_index;
2591 u64 first_block, last_block;
2592 u32 block_cnt;
2593 u32 blocks_per_row;
2594 u64 first_row, last_row;
2595 u32 first_row_offset, last_row_offset;
2596 u32 first_column, last_column;
2597 u32 map_row;
2598 u32 disk_handle;
2599 u64 disk_block;
2600 u32 disk_block_cnt;
2601 u8 cdb[16];
2602 u8 cdb_len;
2603#if BITS_PER_LONG == 32
2604 u64 tmpdiv;
2605#endif
2606
2607 BUG_ON(!(dev->offload_config && dev->offload_enabled));
2608
2609 /* check for valid opcode, get LBA and block count */
2610 switch (cmd->cmnd[0]) {
2611 case WRITE_6:
2612 is_write = 1;
2613 case READ_6:
2614 first_block =
2615 (((u64) cmd->cmnd[2]) << 8) |
2616 cmd->cmnd[3];
2617 block_cnt = cmd->cmnd[4];
2618 break;
2619 case WRITE_10:
2620 is_write = 1;
2621 case READ_10:
2622 first_block =
2623 (((u64) cmd->cmnd[2]) << 24) |
2624 (((u64) cmd->cmnd[3]) << 16) |
2625 (((u64) cmd->cmnd[4]) << 8) |
2626 cmd->cmnd[5];
2627 block_cnt =
2628 (((u32) cmd->cmnd[7]) << 8) |
2629 cmd->cmnd[8];
2630 break;
2631 case WRITE_12:
2632 is_write = 1;
2633 case READ_12:
2634 first_block =
2635 (((u64) cmd->cmnd[2]) << 24) |
2636 (((u64) cmd->cmnd[3]) << 16) |
2637 (((u64) cmd->cmnd[4]) << 8) |
2638 cmd->cmnd[5];
2639 block_cnt =
2640 (((u32) cmd->cmnd[6]) << 24) |
2641 (((u32) cmd->cmnd[7]) << 16) |
2642 (((u32) cmd->cmnd[8]) << 8) |
2643 cmd->cmnd[9];
2644 break;
2645 case WRITE_16:
2646 is_write = 1;
2647 case READ_16:
2648 first_block =
2649 (((u64) cmd->cmnd[2]) << 56) |
2650 (((u64) cmd->cmnd[3]) << 48) |
2651 (((u64) cmd->cmnd[4]) << 40) |
2652 (((u64) cmd->cmnd[5]) << 32) |
2653 (((u64) cmd->cmnd[6]) << 24) |
2654 (((u64) cmd->cmnd[7]) << 16) |
2655 (((u64) cmd->cmnd[8]) << 8) |
2656 cmd->cmnd[9];
2657 block_cnt =
2658 (((u32) cmd->cmnd[10]) << 24) |
2659 (((u32) cmd->cmnd[11]) << 16) |
2660 (((u32) cmd->cmnd[12]) << 8) |
2661 cmd->cmnd[13];
2662 break;
2663 default:
2664 return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
2665 }
2666 BUG_ON(block_cnt == 0);
2667 last_block = first_block + block_cnt - 1;
2668
2669 /* check for write to non-RAID-0 */
2670 if (is_write && dev->raid_level != 0)
2671 return IO_ACCEL_INELIGIBLE;
2672
2673 /* check for invalid block or wraparound */
2674 if (last_block >= map->volume_blk_cnt || last_block < first_block)
2675 return IO_ACCEL_INELIGIBLE;
2676
2677 /* calculate stripe information for the request */
2678 blocks_per_row = map->data_disks_per_row * map->strip_size;
2679#if BITS_PER_LONG == 32
2680 tmpdiv = first_block;
2681 (void) do_div(tmpdiv, blocks_per_row);
2682 first_row = tmpdiv;
2683 tmpdiv = last_block;
2684 (void) do_div(tmpdiv, blocks_per_row);
2685 last_row = tmpdiv;
2686 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
2687 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
2688 tmpdiv = first_row_offset;
2689 (void) do_div(tmpdiv, map->strip_size);
2690 first_column = tmpdiv;
2691 tmpdiv = last_row_offset;
2692 (void) do_div(tmpdiv, map->strip_size);
2693 last_column = tmpdiv;
2694#else
2695 first_row = first_block / blocks_per_row;
2696 last_row = last_block / blocks_per_row;
2697 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
2698 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
2699 first_column = first_row_offset / map->strip_size;
2700 last_column = last_row_offset / map->strip_size;
2701#endif
2702
2703 /* if this isn't a single row/column then give to the controller */
2704 if ((first_row != last_row) || (first_column != last_column))
2705 return IO_ACCEL_INELIGIBLE;
2706
2707 /* proceeding with driver mapping */
2708 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
2709 map->row_cnt;
2710 map_index = (map_row * (map->data_disks_per_row +
2711 map->metadata_disks_per_row)) + first_column;
2712 if (dev->raid_level == 2) {
2713 /* simple round-robin balancing of RAID 1+0 reads across
2714 * primary and mirror members. this is appropriate for SSD
2715 * but not optimal for HDD.
2716 */
2717 if (dev->offload_to_mirror)
2718 map_index += map->data_disks_per_row;
2719 dev->offload_to_mirror = !dev->offload_to_mirror;
2720 }
2721 disk_handle = dd[map_index].ioaccel_handle;
2722 disk_block = map->disk_starting_blk + (first_row * map->strip_size) +
2723 (first_row_offset - (first_column * map->strip_size));
2724 disk_block_cnt = block_cnt;
2725
2726 /* handle differing logical/physical block sizes */
2727 if (map->phys_blk_shift) {
2728 disk_block <<= map->phys_blk_shift;
2729 disk_block_cnt <<= map->phys_blk_shift;
2730 }
2731 BUG_ON(disk_block_cnt > 0xffff);
2732
2733 /* build the new CDB for the physical disk I/O */
2734 if (disk_block > 0xffffffff) {
2735 cdb[0] = is_write ? WRITE_16 : READ_16;
2736 cdb[1] = 0;
2737 cdb[2] = (u8) (disk_block >> 56);
2738 cdb[3] = (u8) (disk_block >> 48);
2739 cdb[4] = (u8) (disk_block >> 40);
2740 cdb[5] = (u8) (disk_block >> 32);
2741 cdb[6] = (u8) (disk_block >> 24);
2742 cdb[7] = (u8) (disk_block >> 16);
2743 cdb[8] = (u8) (disk_block >> 8);
2744 cdb[9] = (u8) (disk_block);
2745 cdb[10] = (u8) (disk_block_cnt >> 24);
2746 cdb[11] = (u8) (disk_block_cnt >> 16);
2747 cdb[12] = (u8) (disk_block_cnt >> 8);
2748 cdb[13] = (u8) (disk_block_cnt);
2749 cdb[14] = 0;
2750 cdb[15] = 0;
2751 cdb_len = 16;
2752 } else {
2753 cdb[0] = is_write ? WRITE_10 : READ_10;
2754 cdb[1] = 0;
2755 cdb[2] = (u8) (disk_block >> 24);
2756 cdb[3] = (u8) (disk_block >> 16);
2757 cdb[4] = (u8) (disk_block >> 8);
2758 cdb[5] = (u8) (disk_block);
2759 cdb[6] = 0;
2760 cdb[7] = (u8) (disk_block_cnt >> 8);
2761 cdb[8] = (u8) (disk_block_cnt);
2762 cdb[9] = 0;
2763 cdb_len = 10;
2764 }
2765 return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
2766 dev->scsi3addr);
2767}
2768
Jeff Garzikf2812332010-11-16 02:10:29 -05002769static int hpsa_scsi_queue_command_lck(struct scsi_cmnd *cmd,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002770 void (*done)(struct scsi_cmnd *))
2771{
2772 struct ctlr_info *h;
2773 struct hpsa_scsi_dev_t *dev;
2774 unsigned char scsi3addr[8];
2775 struct CommandList *c;
2776 unsigned long flags;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002777 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002778
2779 /* Get the ptr to our adapter structure out of cmd->host. */
2780 h = sdev_to_hba(cmd->device);
2781 dev = cmd->device->hostdata;
2782 if (!dev) {
2783 cmd->result = DID_NO_CONNECT << 16;
2784 done(cmd);
2785 return 0;
2786 }
2787 memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
2788
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002789 spin_lock_irqsave(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05002790 if (unlikely(h->lockup_detected)) {
2791 spin_unlock_irqrestore(&h->lock, flags);
2792 cmd->result = DID_ERROR << 16;
2793 done(cmd);
2794 return 0;
2795 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002796 spin_unlock_irqrestore(&h->lock, flags);
Matt Gatese16a33a2012-05-01 11:43:11 -05002797 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002798 if (c == NULL) { /* trouble... */
2799 dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
2800 return SCSI_MLQUEUE_HOST_BUSY;
2801 }
2802
2803 /* Fill in the command list header */
2804
2805 cmd->scsi_done = done; /* save this for use by completion code */
2806
2807 /* save c in case we have to abort it */
2808 cmd->host_scribble = (unsigned char *) c;
2809
2810 c->cmd_type = CMD_SCSI;
2811 c->scsi_cmd = cmd;
Matt Gatese1f7de02014-02-18 13:55:17 -06002812
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002813 /* Call alternate submit routine for I/O accelerated commands.
2814 * Retries always go down the normal I/O path.
2815 */
2816 if (likely(cmd->retries == 0 &&
2817 cmd->request->cmd_type == REQ_TYPE_FS)) {
2818 if (dev->offload_enabled) {
2819 rc = hpsa_scsi_ioaccel_raid_map(h, c);
2820 if (rc == 0)
2821 return 0; /* Sent on ioaccel path */
2822 if (rc < 0) { /* scsi_dma_map failed. */
2823 cmd_free(h, c);
2824 return SCSI_MLQUEUE_HOST_BUSY;
2825 }
2826 } else if (dev->ioaccel_handle) {
2827 rc = hpsa_scsi_ioaccel_direct_map(h, c);
2828 if (rc == 0)
2829 return 0; /* Sent on direct map path */
2830 if (rc < 0) { /* scsi_dma_map failed. */
2831 cmd_free(h, c);
2832 return SCSI_MLQUEUE_HOST_BUSY;
2833 }
2834 }
2835 }
Matt Gatese1f7de02014-02-18 13:55:17 -06002836
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002837 c->Header.ReplyQueue = 0; /* unused in simple mode */
2838 memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
Don Brace303932f2010-02-04 08:42:40 -06002839 c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT);
2840 c->Header.Tag.lower |= DIRECT_LOOKUP_BIT;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002841
2842 /* Fill in the request block... */
2843
2844 c->Request.Timeout = 0;
2845 memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
2846 BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
2847 c->Request.CDBLen = cmd->cmd_len;
2848 memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
2849 c->Request.Type.Type = TYPE_CMD;
2850 c->Request.Type.Attribute = ATTR_SIMPLE;
2851 switch (cmd->sc_data_direction) {
2852 case DMA_TO_DEVICE:
2853 c->Request.Type.Direction = XFER_WRITE;
2854 break;
2855 case DMA_FROM_DEVICE:
2856 c->Request.Type.Direction = XFER_READ;
2857 break;
2858 case DMA_NONE:
2859 c->Request.Type.Direction = XFER_NONE;
2860 break;
2861 case DMA_BIDIRECTIONAL:
2862 /* This can happen if a buggy application does a scsi passthru
2863 * and sets both inlen and outlen to non-zero. ( see
2864 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
2865 */
2866
2867 c->Request.Type.Direction = XFER_RSVD;
2868 /* This is technically wrong, and hpsa controllers should
2869 * reject it with CMD_INVALID, which is the most correct
2870 * response, but non-fibre backends appear to let it
2871 * slide by, and give the same results as if this field
2872 * were set correctly. Either way is acceptable for
2873 * our purposes here.
2874 */
2875
2876 break;
2877
2878 default:
2879 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
2880 cmd->sc_data_direction);
2881 BUG();
2882 break;
2883 }
2884
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002885 if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002886 cmd_free(h, c);
2887 return SCSI_MLQUEUE_HOST_BUSY;
2888 }
2889 enqueue_cmd_and_start_io(h, c);
2890 /* the cmd'll come back via intr handler in complete_scsi_command() */
2891 return 0;
2892}
2893
Jeff Garzikf2812332010-11-16 02:10:29 -05002894static DEF_SCSI_QCMD(hpsa_scsi_queue_command)
2895
Stephen M. Cameron5f389362014-02-18 13:55:48 -06002896static int do_not_scan_if_controller_locked_up(struct ctlr_info *h)
2897{
2898 unsigned long flags;
2899
2900 /*
2901 * Don't let rescans be initiated on a controller known
2902 * to be locked up. If the controller locks up *during*
2903 * a rescan, that thread is probably hosed, but at least
2904 * we can prevent new rescan threads from piling up on a
2905 * locked up controller.
2906 */
2907 spin_lock_irqsave(&h->lock, flags);
2908 if (unlikely(h->lockup_detected)) {
2909 spin_unlock_irqrestore(&h->lock, flags);
2910 spin_lock_irqsave(&h->scan_lock, flags);
2911 h->scan_finished = 1;
2912 wake_up_all(&h->scan_wait_queue);
2913 spin_unlock_irqrestore(&h->scan_lock, flags);
2914 return 1;
2915 }
2916 spin_unlock_irqrestore(&h->lock, flags);
2917 return 0;
2918}
2919
Stephen M. Camerona08a84712010-02-04 08:43:16 -06002920static void hpsa_scan_start(struct Scsi_Host *sh)
2921{
2922 struct ctlr_info *h = shost_to_hba(sh);
2923 unsigned long flags;
2924
Stephen M. Cameron5f389362014-02-18 13:55:48 -06002925 if (do_not_scan_if_controller_locked_up(h))
2926 return;
2927
Stephen M. Camerona08a84712010-02-04 08:43:16 -06002928 /* wait until any scan already in progress is finished. */
2929 while (1) {
2930 spin_lock_irqsave(&h->scan_lock, flags);
2931 if (h->scan_finished)
2932 break;
2933 spin_unlock_irqrestore(&h->scan_lock, flags);
2934 wait_event(h->scan_wait_queue, h->scan_finished);
2935 /* Note: We don't need to worry about a race between this
2936 * thread and driver unload because the midlayer will
2937 * have incremented the reference count, so unload won't
2938 * happen if we're in here.
2939 */
2940 }
2941 h->scan_finished = 0; /* mark scan as in progress */
2942 spin_unlock_irqrestore(&h->scan_lock, flags);
2943
Stephen M. Cameron5f389362014-02-18 13:55:48 -06002944 if (do_not_scan_if_controller_locked_up(h))
2945 return;
2946
Stephen M. Camerona08a84712010-02-04 08:43:16 -06002947 hpsa_update_scsi_devices(h, h->scsi_host->host_no);
2948
2949 spin_lock_irqsave(&h->scan_lock, flags);
2950 h->scan_finished = 1; /* mark scan as finished. */
2951 wake_up_all(&h->scan_wait_queue);
2952 spin_unlock_irqrestore(&h->scan_lock, flags);
2953}
2954
2955static int hpsa_scan_finished(struct Scsi_Host *sh,
2956 unsigned long elapsed_time)
2957{
2958 struct ctlr_info *h = shost_to_hba(sh);
2959 unsigned long flags;
2960 int finished;
2961
2962 spin_lock_irqsave(&h->scan_lock, flags);
2963 finished = h->scan_finished;
2964 spin_unlock_irqrestore(&h->scan_lock, flags);
2965 return finished;
2966}
2967
Stephen M. Cameron667e23d2010-02-25 14:02:51 -06002968static int hpsa_change_queue_depth(struct scsi_device *sdev,
2969 int qdepth, int reason)
2970{
2971 struct ctlr_info *h = sdev_to_hba(sdev);
2972
2973 if (reason != SCSI_QDEPTH_DEFAULT)
2974 return -ENOTSUPP;
2975
2976 if (qdepth < 1)
2977 qdepth = 1;
2978 else
2979 if (qdepth > h->nr_cmds)
2980 qdepth = h->nr_cmds;
2981 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
2982 return sdev->queue_depth;
2983}
2984
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002985static void hpsa_unregister_scsi(struct ctlr_info *h)
2986{
2987 /* we are being forcibly unloaded, and may not refuse. */
2988 scsi_remove_host(h->scsi_host);
2989 scsi_host_put(h->scsi_host);
2990 h->scsi_host = NULL;
2991}
2992
2993static int hpsa_register_scsi(struct ctlr_info *h)
2994{
Stephen M. Cameronb7056902012-01-19 14:00:53 -06002995 struct Scsi_Host *sh;
2996 int error;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002997
Stephen M. Cameronb7056902012-01-19 14:00:53 -06002998 sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
2999 if (sh == NULL)
3000 goto fail;
3001
3002 sh->io_port = 0;
3003 sh->n_io_port = 0;
3004 sh->this_id = -1;
3005 sh->max_channel = 3;
3006 sh->max_cmd_len = MAX_COMMAND_SIZE;
3007 sh->max_lun = HPSA_MAX_LUN;
3008 sh->max_id = HPSA_MAX_LUN;
3009 sh->can_queue = h->nr_cmds;
3010 sh->cmd_per_lun = h->nr_cmds;
3011 sh->sg_tablesize = h->maxsgentries;
3012 h->scsi_host = sh;
3013 sh->hostdata[0] = (unsigned long) h;
3014 sh->irq = h->intr[h->intr_mode];
3015 sh->unique_id = sh->irq;
3016 error = scsi_add_host(sh, &h->pdev->dev);
3017 if (error)
3018 goto fail_host_put;
3019 scsi_scan_host(sh);
3020 return 0;
3021
3022 fail_host_put:
3023 dev_err(&h->pdev->dev, "%s: scsi_add_host"
3024 " failed for controller %d\n", __func__, h->ctlr);
3025 scsi_host_put(sh);
3026 return error;
3027 fail:
3028 dev_err(&h->pdev->dev, "%s: scsi_host_alloc"
3029 " failed for controller %d\n", __func__, h->ctlr);
3030 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003031}
3032
3033static int wait_for_device_to_become_ready(struct ctlr_info *h,
3034 unsigned char lunaddr[])
3035{
3036 int rc = 0;
3037 int count = 0;
3038 int waittime = 1; /* seconds */
3039 struct CommandList *c;
3040
3041 c = cmd_special_alloc(h);
3042 if (!c) {
3043 dev_warn(&h->pdev->dev, "out of memory in "
3044 "wait_for_device_to_become_ready.\n");
3045 return IO_ERROR;
3046 }
3047
3048 /* Send test unit ready until device ready, or give up. */
3049 while (count < HPSA_TUR_RETRY_LIMIT) {
3050
3051 /* Wait for a bit. do this first, because if we send
3052 * the TUR right away, the reset will just abort it.
3053 */
3054 msleep(1000 * waittime);
3055 count++;
3056
3057 /* Increase wait time with each try, up to a point. */
3058 if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
3059 waittime = waittime * 2;
3060
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003061 /* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
3062 (void) fill_cmd(c, TEST_UNIT_READY, h,
3063 NULL, 0, 0, lunaddr, TYPE_CMD);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003064 hpsa_scsi_do_simple_cmd_core(h, c);
3065 /* no unmap needed here because no data xfer. */
3066
3067 if (c->err_info->CommandStatus == CMD_SUCCESS)
3068 break;
3069
3070 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
3071 c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
3072 (c->err_info->SenseInfo[2] == NO_SENSE ||
3073 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
3074 break;
3075
3076 dev_warn(&h->pdev->dev, "waiting %d secs "
3077 "for device to become ready.\n", waittime);
3078 rc = 1; /* device not ready. */
3079 }
3080
3081 if (rc)
3082 dev_warn(&h->pdev->dev, "giving up on device.\n");
3083 else
3084 dev_warn(&h->pdev->dev, "device is ready.\n");
3085
3086 cmd_special_free(h, c);
3087 return rc;
3088}
3089
3090/* Need at least one of these error handlers to keep ../scsi/hosts.c from
3091 * complaining. Doing a host- or bus-reset can't do anything good here.
3092 */
3093static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
3094{
3095 int rc;
3096 struct ctlr_info *h;
3097 struct hpsa_scsi_dev_t *dev;
3098
3099 /* find the controller to which the command to be aborted was sent */
3100 h = sdev_to_hba(scsicmd->device);
3101 if (h == NULL) /* paranoia */
3102 return FAILED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003103 dev = scsicmd->device->hostdata;
3104 if (!dev) {
3105 dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
3106 "device lookup failed.\n");
3107 return FAILED;
3108 }
Stephen M. Camerond416b0c2010-02-04 08:43:21 -06003109 dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
3110 h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003111 /* send a reset to the SCSI LUN which the command was sent to */
3112 rc = hpsa_send_reset(h, dev->scsi3addr);
3113 if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
3114 return SUCCESS;
3115
3116 dev_warn(&h->pdev->dev, "resetting device failed.\n");
3117 return FAILED;
3118}
3119
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003120static void swizzle_abort_tag(u8 *tag)
3121{
3122 u8 original_tag[8];
3123
3124 memcpy(original_tag, tag, 8);
3125 tag[0] = original_tag[3];
3126 tag[1] = original_tag[2];
3127 tag[2] = original_tag[1];
3128 tag[3] = original_tag[0];
3129 tag[4] = original_tag[7];
3130 tag[5] = original_tag[6];
3131 tag[6] = original_tag[5];
3132 tag[7] = original_tag[4];
3133}
3134
Scott Teel17eb87d2014-02-18 13:55:28 -06003135static void hpsa_get_tag(struct ctlr_info *h,
3136 struct CommandList *c, u32 *taglower, u32 *tagupper)
3137{
3138 if (c->cmd_type == CMD_IOACCEL1) {
3139 struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
3140 &h->ioaccel_cmd_pool[c->cmdindex];
3141 *tagupper = cm1->Tag.upper;
3142 *taglower = cm1->Tag.lower;
3143 } else {
3144 *tagupper = c->Header.Tag.upper;
3145 *taglower = c->Header.Tag.lower;
3146 }
3147}
3148
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003149static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003150 struct CommandList *abort, int swizzle)
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003151{
3152 int rc = IO_OK;
3153 struct CommandList *c;
3154 struct ErrorInfo *ei;
Scott Teel17eb87d2014-02-18 13:55:28 -06003155 u32 tagupper, taglower;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003156
3157 c = cmd_special_alloc(h);
3158 if (c == NULL) { /* trouble... */
3159 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
3160 return -ENOMEM;
3161 }
3162
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003163 /* fill_cmd can't fail here, no buffer to map */
3164 (void) fill_cmd(c, HPSA_ABORT_MSG, h, abort,
3165 0, 0, scsi3addr, TYPE_MSG);
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003166 if (swizzle)
3167 swizzle_abort_tag(&c->Request.CDB[4]);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003168 hpsa_scsi_do_simple_cmd_core(h, c);
Scott Teel17eb87d2014-02-18 13:55:28 -06003169 hpsa_get_tag(h, abort, &taglower, &tagupper);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003170 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd_core completed.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06003171 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003172 /* no unmap needed here because no data xfer. */
3173
3174 ei = c->err_info;
3175 switch (ei->CommandStatus) {
3176 case CMD_SUCCESS:
3177 break;
3178 case CMD_UNABORTABLE: /* Very common, don't make noise. */
3179 rc = -1;
3180 break;
3181 default:
3182 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06003183 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003184 hpsa_scsi_interpret_error(c);
3185 rc = -1;
3186 break;
3187 }
3188 cmd_special_free(h, c);
3189 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n", __func__,
3190 abort->Header.Tag.upper, abort->Header.Tag.lower);
3191 return rc;
3192}
3193
3194/*
3195 * hpsa_find_cmd_in_queue
3196 *
3197 * Used to determine whether a command (find) is still present
3198 * in queue_head. Optionally excludes the last element of queue_head.
3199 *
3200 * This is used to avoid unnecessary aborts. Commands in h->reqQ have
3201 * not yet been submitted, and so can be aborted by the driver without
3202 * sending an abort to the hardware.
3203 *
3204 * Returns pointer to command if found in queue, NULL otherwise.
3205 */
3206static struct CommandList *hpsa_find_cmd_in_queue(struct ctlr_info *h,
3207 struct scsi_cmnd *find, struct list_head *queue_head)
3208{
3209 unsigned long flags;
3210 struct CommandList *c = NULL; /* ptr into cmpQ */
3211
3212 if (!find)
3213 return 0;
3214 spin_lock_irqsave(&h->lock, flags);
3215 list_for_each_entry(c, queue_head, list) {
3216 if (c->scsi_cmd == NULL) /* e.g.: passthru ioctl */
3217 continue;
3218 if (c->scsi_cmd == find) {
3219 spin_unlock_irqrestore(&h->lock, flags);
3220 return c;
3221 }
3222 }
3223 spin_unlock_irqrestore(&h->lock, flags);
3224 return NULL;
3225}
3226
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003227static struct CommandList *hpsa_find_cmd_in_queue_by_tag(struct ctlr_info *h,
3228 u8 *tag, struct list_head *queue_head)
3229{
3230 unsigned long flags;
3231 struct CommandList *c;
3232
3233 spin_lock_irqsave(&h->lock, flags);
3234 list_for_each_entry(c, queue_head, list) {
3235 if (memcmp(&c->Header.Tag, tag, 8) != 0)
3236 continue;
3237 spin_unlock_irqrestore(&h->lock, flags);
3238 return c;
3239 }
3240 spin_unlock_irqrestore(&h->lock, flags);
3241 return NULL;
3242}
3243
3244/* Some Smart Arrays need the abort tag swizzled, and some don't. It's hard to
3245 * tell which kind we're dealing with, so we send the abort both ways. There
3246 * shouldn't be any collisions between swizzled and unswizzled tags due to the
3247 * way we construct our tags but we check anyway in case the assumptions which
3248 * make this true someday become false.
3249 */
3250static int hpsa_send_abort_both_ways(struct ctlr_info *h,
3251 unsigned char *scsi3addr, struct CommandList *abort)
3252{
3253 u8 swizzled_tag[8];
3254 struct CommandList *c;
3255 int rc = 0, rc2 = 0;
3256
3257 /* we do not expect to find the swizzled tag in our queue, but
3258 * check anyway just to be sure the assumptions which make this
3259 * the case haven't become wrong.
3260 */
3261 memcpy(swizzled_tag, &abort->Request.CDB[4], 8);
3262 swizzle_abort_tag(swizzled_tag);
3263 c = hpsa_find_cmd_in_queue_by_tag(h, swizzled_tag, &h->cmpQ);
3264 if (c != NULL) {
3265 dev_warn(&h->pdev->dev, "Unexpectedly found byte-swapped tag in completion queue.\n");
3266 return hpsa_send_abort(h, scsi3addr, abort, 0);
3267 }
3268 rc = hpsa_send_abort(h, scsi3addr, abort, 0);
3269
3270 /* if the command is still in our queue, we can't conclude that it was
3271 * aborted (it might have just completed normally) but in any case
3272 * we don't need to try to abort it another way.
3273 */
3274 c = hpsa_find_cmd_in_queue(h, abort->scsi_cmd, &h->cmpQ);
3275 if (c)
3276 rc2 = hpsa_send_abort(h, scsi3addr, abort, 1);
3277 return rc && rc2;
3278}
3279
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003280/* Send an abort for the specified command.
3281 * If the device and controller support it,
3282 * send a task abort request.
3283 */
3284static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
3285{
3286
3287 int i, rc;
3288 struct ctlr_info *h;
3289 struct hpsa_scsi_dev_t *dev;
3290 struct CommandList *abort; /* pointer to command to be aborted */
3291 struct CommandList *found;
3292 struct scsi_cmnd *as; /* ptr to scsi cmd inside aborted command. */
3293 char msg[256]; /* For debug messaging. */
3294 int ml = 0;
Scott Teel17eb87d2014-02-18 13:55:28 -06003295 u32 tagupper, taglower;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003296
3297 /* Find the controller of the command to be aborted */
3298 h = sdev_to_hba(sc->device);
3299 if (WARN(h == NULL,
3300 "ABORT REQUEST FAILED, Controller lookup failed.\n"))
3301 return FAILED;
3302
3303 /* Check that controller supports some kind of task abort */
3304 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
3305 !(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
3306 return FAILED;
3307
3308 memset(msg, 0, sizeof(msg));
3309 ml += sprintf(msg+ml, "ABORT REQUEST on C%d:B%d:T%d:L%d ",
3310 h->scsi_host->host_no, sc->device->channel,
3311 sc->device->id, sc->device->lun);
3312
3313 /* Find the device of the command to be aborted */
3314 dev = sc->device->hostdata;
3315 if (!dev) {
3316 dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
3317 msg);
3318 return FAILED;
3319 }
3320
3321 /* Get SCSI command to be aborted */
3322 abort = (struct CommandList *) sc->host_scribble;
3323 if (abort == NULL) {
3324 dev_err(&h->pdev->dev, "%s FAILED, Command to abort is NULL.\n",
3325 msg);
3326 return FAILED;
3327 }
Scott Teel17eb87d2014-02-18 13:55:28 -06003328 hpsa_get_tag(h, abort, &taglower, &tagupper);
3329 ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003330 as = (struct scsi_cmnd *) abort->scsi_cmd;
3331 if (as != NULL)
3332 ml += sprintf(msg+ml, "Command:0x%x SN:0x%lx ",
3333 as->cmnd[0], as->serial_number);
3334 dev_dbg(&h->pdev->dev, "%s\n", msg);
3335 dev_warn(&h->pdev->dev, "Abort request on C%d:B%d:T%d:L%d\n",
3336 h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
3337
3338 /* Search reqQ to See if command is queued but not submitted,
3339 * if so, complete the command with aborted status and remove
3340 * it from the reqQ.
3341 */
3342 found = hpsa_find_cmd_in_queue(h, sc, &h->reqQ);
3343 if (found) {
3344 found->err_info->CommandStatus = CMD_ABORTED;
3345 finish_cmd(found);
3346 dev_info(&h->pdev->dev, "%s Request SUCCEEDED (driver queue).\n",
3347 msg);
3348 return SUCCESS;
3349 }
3350
3351 /* not in reqQ, if also not in cmpQ, must have already completed */
3352 found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
3353 if (!found) {
Stephen M. Camerond6ebd0f2012-07-26 11:34:17 -05003354 dev_dbg(&h->pdev->dev, "%s Request SUCCEEDED (not known to driver).\n",
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003355 msg);
3356 return SUCCESS;
3357 }
3358
3359 /*
3360 * Command is in flight, or possibly already completed
3361 * by the firmware (but not to the scsi mid layer) but we can't
3362 * distinguish which. Send the abort down.
3363 */
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003364 rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003365 if (rc != 0) {
3366 dev_dbg(&h->pdev->dev, "%s Request FAILED.\n", msg);
3367 dev_warn(&h->pdev->dev, "FAILED abort on device C%d:B%d:T%d:L%d\n",
3368 h->scsi_host->host_no,
3369 dev->bus, dev->target, dev->lun);
3370 return FAILED;
3371 }
3372 dev_info(&h->pdev->dev, "%s REQUEST SUCCEEDED.\n", msg);
3373
3374 /* If the abort(s) above completed and actually aborted the
3375 * command, then the command to be aborted should already be
3376 * completed. If not, wait around a bit more to see if they
3377 * manage to complete normally.
3378 */
3379#define ABORT_COMPLETE_WAIT_SECS 30
3380 for (i = 0; i < ABORT_COMPLETE_WAIT_SECS * 10; i++) {
3381 found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
3382 if (!found)
3383 return SUCCESS;
3384 msleep(100);
3385 }
3386 dev_warn(&h->pdev->dev, "%s FAILED. Aborted command has not completed after %d seconds.\n",
3387 msg, ABORT_COMPLETE_WAIT_SECS);
3388 return FAILED;
3389}
3390
3391
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003392/*
3393 * For operations that cannot sleep, a command block is allocated at init,
3394 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
3395 * which ones are free or in use. Lock must be held when calling this.
3396 * cmd_free() is the complement.
3397 */
3398static struct CommandList *cmd_alloc(struct ctlr_info *h)
3399{
3400 struct CommandList *c;
3401 int i;
3402 union u64bit temp64;
3403 dma_addr_t cmd_dma_handle, err_dma_handle;
Matt Gatese16a33a2012-05-01 11:43:11 -05003404 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003405
Matt Gatese16a33a2012-05-01 11:43:11 -05003406 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003407 do {
3408 i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
Matt Gatese16a33a2012-05-01 11:43:11 -05003409 if (i == h->nr_cmds) {
3410 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003411 return NULL;
Matt Gatese16a33a2012-05-01 11:43:11 -05003412 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003413 } while (test_and_set_bit
3414 (i & (BITS_PER_LONG - 1),
3415 h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
Matt Gatese16a33a2012-05-01 11:43:11 -05003416 spin_unlock_irqrestore(&h->lock, flags);
3417
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003418 c = h->cmd_pool + i;
3419 memset(c, 0, sizeof(*c));
3420 cmd_dma_handle = h->cmd_pool_dhandle
3421 + i * sizeof(*c);
3422 c->err_info = h->errinfo_pool + i;
3423 memset(c->err_info, 0, sizeof(*c->err_info));
3424 err_dma_handle = h->errinfo_pool_dhandle
3425 + i * sizeof(*c->err_info);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003426
3427 c->cmdindex = i;
3428
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06003429 INIT_LIST_HEAD(&c->list);
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06003430 c->busaddr = (u32) cmd_dma_handle;
3431 temp64.val = (u64) err_dma_handle;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003432 c->ErrDesc.Addr.lower = temp64.val32.lower;
3433 c->ErrDesc.Addr.upper = temp64.val32.upper;
3434 c->ErrDesc.Len = sizeof(*c->err_info);
3435
3436 c->h = h;
3437 return c;
3438}
3439
3440/* For operations that can wait for kmalloc to possibly sleep,
3441 * this routine can be called. Lock need not be held to call
3442 * cmd_special_alloc. cmd_special_free() is the complement.
3443 */
3444static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
3445{
3446 struct CommandList *c;
3447 union u64bit temp64;
3448 dma_addr_t cmd_dma_handle, err_dma_handle;
3449
3450 c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
3451 if (c == NULL)
3452 return NULL;
3453 memset(c, 0, sizeof(*c));
3454
Matt Gatese1f7de02014-02-18 13:55:17 -06003455 c->cmd_type = CMD_SCSI;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003456 c->cmdindex = -1;
3457
3458 c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info),
3459 &err_dma_handle);
3460
3461 if (c->err_info == NULL) {
3462 pci_free_consistent(h->pdev,
3463 sizeof(*c), c, cmd_dma_handle);
3464 return NULL;
3465 }
3466 memset(c->err_info, 0, sizeof(*c->err_info));
3467
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06003468 INIT_LIST_HEAD(&c->list);
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06003469 c->busaddr = (u32) cmd_dma_handle;
3470 temp64.val = (u64) err_dma_handle;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003471 c->ErrDesc.Addr.lower = temp64.val32.lower;
3472 c->ErrDesc.Addr.upper = temp64.val32.upper;
3473 c->ErrDesc.Len = sizeof(*c->err_info);
3474
3475 c->h = h;
3476 return c;
3477}
3478
3479static void cmd_free(struct ctlr_info *h, struct CommandList *c)
3480{
3481 int i;
Matt Gatese16a33a2012-05-01 11:43:11 -05003482 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003483
3484 i = c - h->cmd_pool;
Matt Gatese16a33a2012-05-01 11:43:11 -05003485 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003486 clear_bit(i & (BITS_PER_LONG - 1),
3487 h->cmd_pool_bits + (i / BITS_PER_LONG));
Matt Gatese16a33a2012-05-01 11:43:11 -05003488 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003489}
3490
3491static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
3492{
3493 union u64bit temp64;
3494
3495 temp64.val32.lower = c->ErrDesc.Addr.lower;
3496 temp64.val32.upper = c->ErrDesc.Addr.upper;
3497 pci_free_consistent(h->pdev, sizeof(*c->err_info),
3498 c->err_info, (dma_addr_t) temp64.val);
3499 pci_free_consistent(h->pdev, sizeof(*c),
Stephen M. Camerond896f3f2011-01-06 14:47:53 -06003500 c, (dma_addr_t) (c->busaddr & DIRECT_LOOKUP_MASK));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003501}
3502
3503#ifdef CONFIG_COMPAT
3504
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003505static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
3506{
3507 IOCTL32_Command_struct __user *arg32 =
3508 (IOCTL32_Command_struct __user *) arg;
3509 IOCTL_Command_struct arg64;
3510 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
3511 int err;
3512 u32 cp;
3513
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06003514 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003515 err = 0;
3516 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
3517 sizeof(arg64.LUN_info));
3518 err |= copy_from_user(&arg64.Request, &arg32->Request,
3519 sizeof(arg64.Request));
3520 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
3521 sizeof(arg64.error_info));
3522 err |= get_user(arg64.buf_size, &arg32->buf_size);
3523 err |= get_user(cp, &arg32->buf);
3524 arg64.buf = compat_ptr(cp);
3525 err |= copy_to_user(p, &arg64, sizeof(arg64));
3526
3527 if (err)
3528 return -EFAULT;
3529
Stephen M. Camerone39eeae2010-02-04 08:43:46 -06003530 err = hpsa_ioctl(dev, CCISS_PASSTHRU, (void *)p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003531 if (err)
3532 return err;
3533 err |= copy_in_user(&arg32->error_info, &p->error_info,
3534 sizeof(arg32->error_info));
3535 if (err)
3536 return -EFAULT;
3537 return err;
3538}
3539
3540static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
3541 int cmd, void *arg)
3542{
3543 BIG_IOCTL32_Command_struct __user *arg32 =
3544 (BIG_IOCTL32_Command_struct __user *) arg;
3545 BIG_IOCTL_Command_struct arg64;
3546 BIG_IOCTL_Command_struct __user *p =
3547 compat_alloc_user_space(sizeof(arg64));
3548 int err;
3549 u32 cp;
3550
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06003551 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003552 err = 0;
3553 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
3554 sizeof(arg64.LUN_info));
3555 err |= copy_from_user(&arg64.Request, &arg32->Request,
3556 sizeof(arg64.Request));
3557 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
3558 sizeof(arg64.error_info));
3559 err |= get_user(arg64.buf_size, &arg32->buf_size);
3560 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
3561 err |= get_user(cp, &arg32->buf);
3562 arg64.buf = compat_ptr(cp);
3563 err |= copy_to_user(p, &arg64, sizeof(arg64));
3564
3565 if (err)
3566 return -EFAULT;
3567
Stephen M. Camerone39eeae2010-02-04 08:43:46 -06003568 err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003569 if (err)
3570 return err;
3571 err |= copy_in_user(&arg32->error_info, &p->error_info,
3572 sizeof(arg32->error_info));
3573 if (err)
3574 return -EFAULT;
3575 return err;
3576}
Stephen M. Cameron71fe75a2010-02-04 08:43:51 -06003577
3578static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
3579{
3580 switch (cmd) {
3581 case CCISS_GETPCIINFO:
3582 case CCISS_GETINTINFO:
3583 case CCISS_SETINTINFO:
3584 case CCISS_GETNODENAME:
3585 case CCISS_SETNODENAME:
3586 case CCISS_GETHEARTBEAT:
3587 case CCISS_GETBUSTYPES:
3588 case CCISS_GETFIRMVER:
3589 case CCISS_GETDRIVVER:
3590 case CCISS_REVALIDVOLS:
3591 case CCISS_DEREGDISK:
3592 case CCISS_REGNEWDISK:
3593 case CCISS_REGNEWD:
3594 case CCISS_RESCANDISK:
3595 case CCISS_GETLUNINFO:
3596 return hpsa_ioctl(dev, cmd, arg);
3597
3598 case CCISS_PASSTHRU32:
3599 return hpsa_ioctl32_passthru(dev, cmd, arg);
3600 case CCISS_BIG_PASSTHRU32:
3601 return hpsa_ioctl32_big_passthru(dev, cmd, arg);
3602
3603 default:
3604 return -ENOIOCTLCMD;
3605 }
3606}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003607#endif
3608
3609static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
3610{
3611 struct hpsa_pci_info pciinfo;
3612
3613 if (!argp)
3614 return -EINVAL;
3615 pciinfo.domain = pci_domain_nr(h->pdev->bus);
3616 pciinfo.bus = h->pdev->bus->number;
3617 pciinfo.dev_fn = h->pdev->devfn;
3618 pciinfo.board_id = h->board_id;
3619 if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
3620 return -EFAULT;
3621 return 0;
3622}
3623
3624static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
3625{
3626 DriverVer_type DriverVer;
3627 unsigned char vmaj, vmin, vsubmin;
3628 int rc;
3629
3630 rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
3631 &vmaj, &vmin, &vsubmin);
3632 if (rc != 3) {
3633 dev_info(&h->pdev->dev, "driver version string '%s' "
3634 "unrecognized.", HPSA_DRIVER_VERSION);
3635 vmaj = 0;
3636 vmin = 0;
3637 vsubmin = 0;
3638 }
3639 DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
3640 if (!argp)
3641 return -EINVAL;
3642 if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
3643 return -EFAULT;
3644 return 0;
3645}
3646
3647static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
3648{
3649 IOCTL_Command_struct iocommand;
3650 struct CommandList *c;
3651 char *buff = NULL;
3652 union u64bit temp64;
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003653 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003654
3655 if (!argp)
3656 return -EINVAL;
3657 if (!capable(CAP_SYS_RAWIO))
3658 return -EPERM;
3659 if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
3660 return -EFAULT;
3661 if ((iocommand.buf_size < 1) &&
3662 (iocommand.Request.Type.Direction != XFER_NONE)) {
3663 return -EINVAL;
3664 }
3665 if (iocommand.buf_size > 0) {
3666 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
3667 if (buff == NULL)
3668 return -EFAULT;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003669 if (iocommand.Request.Type.Direction == XFER_WRITE) {
3670 /* Copy the data into the buffer we created */
3671 if (copy_from_user(buff, iocommand.buf,
3672 iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003673 rc = -EFAULT;
3674 goto out_kfree;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003675 }
3676 } else {
3677 memset(buff, 0, iocommand.buf_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003678 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003679 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003680 c = cmd_special_alloc(h);
3681 if (c == NULL) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003682 rc = -ENOMEM;
3683 goto out_kfree;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003684 }
3685 /* Fill in the command type */
3686 c->cmd_type = CMD_IOCTL_PEND;
3687 /* Fill in Command Header */
3688 c->Header.ReplyQueue = 0; /* unused in simple mode */
3689 if (iocommand.buf_size > 0) { /* buffer to fill */
3690 c->Header.SGList = 1;
3691 c->Header.SGTotal = 1;
3692 } else { /* no buffers to fill */
3693 c->Header.SGList = 0;
3694 c->Header.SGTotal = 0;
3695 }
3696 memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
3697 /* use the kernel address the cmd block for tag */
3698 c->Header.Tag.lower = c->busaddr;
3699
3700 /* Fill in Request block */
3701 memcpy(&c->Request, &iocommand.Request,
3702 sizeof(c->Request));
3703
3704 /* Fill in the scatter gather information */
3705 if (iocommand.buf_size > 0) {
3706 temp64.val = pci_map_single(h->pdev, buff,
3707 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06003708 if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
3709 c->SG[0].Addr.lower = 0;
3710 c->SG[0].Addr.upper = 0;
3711 c->SG[0].Len = 0;
3712 rc = -ENOMEM;
3713 goto out;
3714 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003715 c->SG[0].Addr.lower = temp64.val32.lower;
3716 c->SG[0].Addr.upper = temp64.val32.upper;
3717 c->SG[0].Len = iocommand.buf_size;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06003718 c->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining*/
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003719 }
Stephen M. Camerona0c12412011-10-26 16:22:04 -05003720 hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
Stephen M. Cameronc2dd32e2011-06-03 09:57:29 -05003721 if (iocommand.buf_size > 0)
3722 hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003723 check_ioctl_unit_attention(h, c);
3724
3725 /* Copy the error information out */
3726 memcpy(&iocommand.error_info, c->err_info,
3727 sizeof(iocommand.error_info));
3728 if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003729 rc = -EFAULT;
3730 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003731 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003732 if (iocommand.Request.Type.Direction == XFER_READ &&
3733 iocommand.buf_size > 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003734 /* Copy the data out of the buffer we created */
3735 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003736 rc = -EFAULT;
3737 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003738 }
3739 }
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003740out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003741 cmd_special_free(h, c);
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003742out_kfree:
3743 kfree(buff);
3744 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003745}
3746
3747static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
3748{
3749 BIG_IOCTL_Command_struct *ioc;
3750 struct CommandList *c;
3751 unsigned char **buff = NULL;
3752 int *buff_size = NULL;
3753 union u64bit temp64;
3754 BYTE sg_used = 0;
3755 int status = 0;
3756 int i;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06003757 u32 left;
3758 u32 sz;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003759 BYTE __user *data_ptr;
3760
3761 if (!argp)
3762 return -EINVAL;
3763 if (!capable(CAP_SYS_RAWIO))
3764 return -EPERM;
3765 ioc = (BIG_IOCTL_Command_struct *)
3766 kmalloc(sizeof(*ioc), GFP_KERNEL);
3767 if (!ioc) {
3768 status = -ENOMEM;
3769 goto cleanup1;
3770 }
3771 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
3772 status = -EFAULT;
3773 goto cleanup1;
3774 }
3775 if ((ioc->buf_size < 1) &&
3776 (ioc->Request.Type.Direction != XFER_NONE)) {
3777 status = -EINVAL;
3778 goto cleanup1;
3779 }
3780 /* Check kmalloc limits using all SGs */
3781 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
3782 status = -EINVAL;
3783 goto cleanup1;
3784 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06003785 if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003786 status = -EINVAL;
3787 goto cleanup1;
3788 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06003789 buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003790 if (!buff) {
3791 status = -ENOMEM;
3792 goto cleanup1;
3793 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06003794 buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003795 if (!buff_size) {
3796 status = -ENOMEM;
3797 goto cleanup1;
3798 }
3799 left = ioc->buf_size;
3800 data_ptr = ioc->buf;
3801 while (left) {
3802 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
3803 buff_size[sg_used] = sz;
3804 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
3805 if (buff[sg_used] == NULL) {
3806 status = -ENOMEM;
3807 goto cleanup1;
3808 }
3809 if (ioc->Request.Type.Direction == XFER_WRITE) {
3810 if (copy_from_user(buff[sg_used], data_ptr, sz)) {
3811 status = -ENOMEM;
3812 goto cleanup1;
3813 }
3814 } else
3815 memset(buff[sg_used], 0, sz);
3816 left -= sz;
3817 data_ptr += sz;
3818 sg_used++;
3819 }
3820 c = cmd_special_alloc(h);
3821 if (c == NULL) {
3822 status = -ENOMEM;
3823 goto cleanup1;
3824 }
3825 c->cmd_type = CMD_IOCTL_PEND;
3826 c->Header.ReplyQueue = 0;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003827 c->Header.SGList = c->Header.SGTotal = sg_used;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003828 memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
3829 c->Header.Tag.lower = c->busaddr;
3830 memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
3831 if (ioc->buf_size > 0) {
3832 int i;
3833 for (i = 0; i < sg_used; i++) {
3834 temp64.val = pci_map_single(h->pdev, buff[i],
3835 buff_size[i], PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06003836 if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
3837 c->SG[i].Addr.lower = 0;
3838 c->SG[i].Addr.upper = 0;
3839 c->SG[i].Len = 0;
3840 hpsa_pci_unmap(h->pdev, c, i,
3841 PCI_DMA_BIDIRECTIONAL);
3842 status = -ENOMEM;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05003843 goto cleanup0;
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06003844 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003845 c->SG[i].Addr.lower = temp64.val32.lower;
3846 c->SG[i].Addr.upper = temp64.val32.upper;
3847 c->SG[i].Len = buff_size[i];
Matt Gatese1d9cbf2014-02-18 13:55:12 -06003848 c->SG[i].Ext = i < sg_used - 1 ? 0 : HPSA_SG_LAST;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003849 }
3850 }
Stephen M. Camerona0c12412011-10-26 16:22:04 -05003851 hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003852 if (sg_used)
3853 hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003854 check_ioctl_unit_attention(h, c);
3855 /* Copy the error information out */
3856 memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
3857 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003858 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05003859 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003860 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003861 if (ioc->Request.Type.Direction == XFER_READ && ioc->buf_size > 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003862 /* Copy the data out of the buffer we created */
3863 BYTE __user *ptr = ioc->buf;
3864 for (i = 0; i < sg_used; i++) {
3865 if (copy_to_user(ptr, buff[i], buff_size[i])) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003866 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05003867 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003868 }
3869 ptr += buff_size[i];
3870 }
3871 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003872 status = 0;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05003873cleanup0:
3874 cmd_special_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003875cleanup1:
3876 if (buff) {
3877 for (i = 0; i < sg_used; i++)
3878 kfree(buff[i]);
3879 kfree(buff);
3880 }
3881 kfree(buff_size);
3882 kfree(ioc);
3883 return status;
3884}
3885
3886static void check_ioctl_unit_attention(struct ctlr_info *h,
3887 struct CommandList *c)
3888{
3889 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
3890 c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
3891 (void) check_for_unit_attention(h, c);
3892}
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05003893
3894static int increment_passthru_count(struct ctlr_info *h)
3895{
3896 unsigned long flags;
3897
3898 spin_lock_irqsave(&h->passthru_count_lock, flags);
3899 if (h->passthru_count >= HPSA_MAX_CONCURRENT_PASSTHRUS) {
3900 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
3901 return -1;
3902 }
3903 h->passthru_count++;
3904 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
3905 return 0;
3906}
3907
3908static void decrement_passthru_count(struct ctlr_info *h)
3909{
3910 unsigned long flags;
3911
3912 spin_lock_irqsave(&h->passthru_count_lock, flags);
3913 if (h->passthru_count <= 0) {
3914 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
3915 /* not expecting to get here. */
3916 dev_warn(&h->pdev->dev, "Bug detected, passthru_count seems to be incorrect.\n");
3917 return;
3918 }
3919 h->passthru_count--;
3920 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
3921}
3922
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003923/*
3924 * ioctl
3925 */
3926static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
3927{
3928 struct ctlr_info *h;
3929 void __user *argp = (void __user *)arg;
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05003930 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003931
3932 h = sdev_to_hba(dev);
3933
3934 switch (cmd) {
3935 case CCISS_DEREGDISK:
3936 case CCISS_REGNEWDISK:
3937 case CCISS_REGNEWD:
Stephen M. Camerona08a84712010-02-04 08:43:16 -06003938 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003939 return 0;
3940 case CCISS_GETPCIINFO:
3941 return hpsa_getpciinfo_ioctl(h, argp);
3942 case CCISS_GETDRIVVER:
3943 return hpsa_getdrivver_ioctl(h, argp);
3944 case CCISS_PASSTHRU:
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05003945 if (increment_passthru_count(h))
3946 return -EAGAIN;
3947 rc = hpsa_passthru_ioctl(h, argp);
3948 decrement_passthru_count(h);
3949 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003950 case CCISS_BIG_PASSTHRU:
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05003951 if (increment_passthru_count(h))
3952 return -EAGAIN;
3953 rc = hpsa_big_passthru_ioctl(h, argp);
3954 decrement_passthru_count(h);
3955 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003956 default:
3957 return -ENOTTY;
3958 }
3959}
3960
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08003961static int hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
3962 u8 reset_type)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05003963{
3964 struct CommandList *c;
3965
3966 c = cmd_alloc(h);
3967 if (!c)
3968 return -ENOMEM;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003969 /* fill_cmd can't fail here, no data buffer to map */
3970 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05003971 RAID_CTLR_LUNID, TYPE_MSG);
3972 c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
3973 c->waiting = NULL;
3974 enqueue_cmd_and_start_io(h, c);
3975 /* Don't wait for completion, the reset won't complete. Don't free
3976 * the command either. This is the last command we will send before
3977 * re-initializing everything, so it doesn't matter and won't leak.
3978 */
3979 return 0;
3980}
3981
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003982static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06003983 void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003984 int cmd_type)
3985{
3986 int pci_dir = XFER_NONE;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003987 struct CommandList *a; /* for commands to be aborted */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003988
3989 c->cmd_type = CMD_IOCTL_PEND;
3990 c->Header.ReplyQueue = 0;
3991 if (buff != NULL && size > 0) {
3992 c->Header.SGList = 1;
3993 c->Header.SGTotal = 1;
3994 } else {
3995 c->Header.SGList = 0;
3996 c->Header.SGTotal = 0;
3997 }
3998 c->Header.Tag.lower = c->busaddr;
3999 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
4000
4001 c->Request.Type.Type = cmd_type;
4002 if (cmd_type == TYPE_CMD) {
4003 switch (cmd) {
4004 case HPSA_INQUIRY:
4005 /* are we trying to read a vital product page */
4006 if (page_code != 0) {
4007 c->Request.CDB[1] = 0x01;
4008 c->Request.CDB[2] = page_code;
4009 }
4010 c->Request.CDBLen = 6;
4011 c->Request.Type.Attribute = ATTR_SIMPLE;
4012 c->Request.Type.Direction = XFER_READ;
4013 c->Request.Timeout = 0;
4014 c->Request.CDB[0] = HPSA_INQUIRY;
4015 c->Request.CDB[4] = size & 0xFF;
4016 break;
4017 case HPSA_REPORT_LOG:
4018 case HPSA_REPORT_PHYS:
4019 /* Talking to controller so It's a physical command
4020 mode = 00 target = 0. Nothing to write.
4021 */
4022 c->Request.CDBLen = 12;
4023 c->Request.Type.Attribute = ATTR_SIMPLE;
4024 c->Request.Type.Direction = XFER_READ;
4025 c->Request.Timeout = 0;
4026 c->Request.CDB[0] = cmd;
4027 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
4028 c->Request.CDB[7] = (size >> 16) & 0xFF;
4029 c->Request.CDB[8] = (size >> 8) & 0xFF;
4030 c->Request.CDB[9] = size & 0xFF;
4031 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004032 case HPSA_CACHE_FLUSH:
4033 c->Request.CDBLen = 12;
4034 c->Request.Type.Attribute = ATTR_SIMPLE;
4035 c->Request.Type.Direction = XFER_WRITE;
4036 c->Request.Timeout = 0;
4037 c->Request.CDB[0] = BMIC_WRITE;
4038 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
Stephen M. Cameronbb158ea2011-10-26 16:21:17 -05004039 c->Request.CDB[7] = (size >> 8) & 0xFF;
4040 c->Request.CDB[8] = size & 0xFF;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004041 break;
4042 case TEST_UNIT_READY:
4043 c->Request.CDBLen = 6;
4044 c->Request.Type.Attribute = ATTR_SIMPLE;
4045 c->Request.Type.Direction = XFER_NONE;
4046 c->Request.Timeout = 0;
4047 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004048 case HPSA_GET_RAID_MAP:
4049 c->Request.CDBLen = 12;
4050 c->Request.Type.Attribute = ATTR_SIMPLE;
4051 c->Request.Type.Direction = XFER_READ;
4052 c->Request.Timeout = 0;
4053 c->Request.CDB[0] = HPSA_CISS_READ;
4054 c->Request.CDB[1] = cmd;
4055 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
4056 c->Request.CDB[7] = (size >> 16) & 0xFF;
4057 c->Request.CDB[8] = (size >> 8) & 0xFF;
4058 c->Request.CDB[9] = size & 0xFF;
4059 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004060 default:
4061 dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
4062 BUG();
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004063 return -1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004064 }
4065 } else if (cmd_type == TYPE_MSG) {
4066 switch (cmd) {
4067
4068 case HPSA_DEVICE_RESET_MSG:
4069 c->Request.CDBLen = 16;
4070 c->Request.Type.Type = 1; /* It is a MSG not a CMD */
4071 c->Request.Type.Attribute = ATTR_SIMPLE;
4072 c->Request.Type.Direction = XFER_NONE;
4073 c->Request.Timeout = 0; /* Don't time out */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004074 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
4075 c->Request.CDB[0] = cmd;
Stephen M. Cameron21e89af2012-07-26 11:34:10 -05004076 c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004077 /* If bytes 4-7 are zero, it means reset the */
4078 /* LunID device */
4079 c->Request.CDB[4] = 0x00;
4080 c->Request.CDB[5] = 0x00;
4081 c->Request.CDB[6] = 0x00;
4082 c->Request.CDB[7] = 0x00;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004083 break;
4084 case HPSA_ABORT_MSG:
4085 a = buff; /* point to command to be aborted */
4086 dev_dbg(&h->pdev->dev, "Abort Tag:0x%08x:%08x using request Tag:0x%08x:%08x\n",
4087 a->Header.Tag.upper, a->Header.Tag.lower,
4088 c->Header.Tag.upper, c->Header.Tag.lower);
4089 c->Request.CDBLen = 16;
4090 c->Request.Type.Type = TYPE_MSG;
4091 c->Request.Type.Attribute = ATTR_SIMPLE;
4092 c->Request.Type.Direction = XFER_WRITE;
4093 c->Request.Timeout = 0; /* Don't time out */
4094 c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
4095 c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
4096 c->Request.CDB[2] = 0x00; /* reserved */
4097 c->Request.CDB[3] = 0x00; /* reserved */
4098 /* Tag to abort goes in CDB[4]-CDB[11] */
4099 c->Request.CDB[4] = a->Header.Tag.lower & 0xFF;
4100 c->Request.CDB[5] = (a->Header.Tag.lower >> 8) & 0xFF;
4101 c->Request.CDB[6] = (a->Header.Tag.lower >> 16) & 0xFF;
4102 c->Request.CDB[7] = (a->Header.Tag.lower >> 24) & 0xFF;
4103 c->Request.CDB[8] = a->Header.Tag.upper & 0xFF;
4104 c->Request.CDB[9] = (a->Header.Tag.upper >> 8) & 0xFF;
4105 c->Request.CDB[10] = (a->Header.Tag.upper >> 16) & 0xFF;
4106 c->Request.CDB[11] = (a->Header.Tag.upper >> 24) & 0xFF;
4107 c->Request.CDB[12] = 0x00; /* reserved */
4108 c->Request.CDB[13] = 0x00; /* reserved */
4109 c->Request.CDB[14] = 0x00; /* reserved */
4110 c->Request.CDB[15] = 0x00; /* reserved */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004111 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004112 default:
4113 dev_warn(&h->pdev->dev, "unknown message type %d\n",
4114 cmd);
4115 BUG();
4116 }
4117 } else {
4118 dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
4119 BUG();
4120 }
4121
4122 switch (c->Request.Type.Direction) {
4123 case XFER_READ:
4124 pci_dir = PCI_DMA_FROMDEVICE;
4125 break;
4126 case XFER_WRITE:
4127 pci_dir = PCI_DMA_TODEVICE;
4128 break;
4129 case XFER_NONE:
4130 pci_dir = PCI_DMA_NONE;
4131 break;
4132 default:
4133 pci_dir = PCI_DMA_BIDIRECTIONAL;
4134 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004135 if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
4136 return -1;
4137 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004138}
4139
4140/*
4141 * Map (physical) PCI mem into (virtual) kernel space
4142 */
4143static void __iomem *remap_pci_mem(ulong base, ulong size)
4144{
4145 ulong page_base = ((ulong) base) & PAGE_MASK;
4146 ulong page_offs = ((ulong) base) - page_base;
Stephen M. Cameron088ba34c2012-07-26 11:34:23 -05004147 void __iomem *page_remapped = ioremap_nocache(page_base,
4148 page_offs + size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004149
4150 return page_remapped ? (page_remapped + page_offs) : NULL;
4151}
4152
4153/* Takes cmds off the submission queue and sends them to the hardware,
4154 * then puts them on the queue of cmds waiting for completion.
4155 */
4156static void start_io(struct ctlr_info *h)
4157{
4158 struct CommandList *c;
Matt Gatese16a33a2012-05-01 11:43:11 -05004159 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004160
Matt Gatese16a33a2012-05-01 11:43:11 -05004161 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06004162 while (!list_empty(&h->reqQ)) {
4163 c = list_entry(h->reqQ.next, struct CommandList, list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004164 /* can't do anything if fifo is full */
4165 if ((h->access.fifo_full(h))) {
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004166 h->fifo_recently_full = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004167 dev_warn(&h->pdev->dev, "fifo full\n");
4168 break;
4169 }
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004170 h->fifo_recently_full = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004171
4172 /* Get the first entry from the Request Q */
4173 removeQ(c);
4174 h->Qdepth--;
4175
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004176 /* Put job onto the completed Q */
4177 addQ(&h->cmpQ, c);
Matt Gatese16a33a2012-05-01 11:43:11 -05004178
4179 /* Must increment commands_outstanding before unlocking
4180 * and submitting to avoid race checking for fifo full
4181 * condition.
4182 */
4183 h->commands_outstanding++;
4184 if (h->commands_outstanding > h->max_outstanding)
4185 h->max_outstanding = h->commands_outstanding;
4186
4187 /* Tell the controller execute command */
4188 spin_unlock_irqrestore(&h->lock, flags);
4189 h->access.submit_command(h, c);
4190 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004191 }
Matt Gatese16a33a2012-05-01 11:43:11 -05004192 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004193}
4194
Matt Gates254f7962012-05-01 11:43:06 -05004195static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004196{
Matt Gates254f7962012-05-01 11:43:06 -05004197 return h->access.command_completed(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004198}
4199
Stephen M. Cameron900c5442010-02-04 08:42:35 -06004200static inline bool interrupt_pending(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004201{
4202 return h->access.intr_pending(h);
4203}
4204
4205static inline long interrupt_not_for_us(struct ctlr_info *h)
4206{
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004207 return (h->access.intr_pending(h) == 0) ||
4208 (h->interrupts_enabled == 0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004209}
4210
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004211static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
4212 u32 raw_tag)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004213{
4214 if (unlikely(tag_index >= h->nr_cmds)) {
4215 dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
4216 return 1;
4217 }
4218 return 0;
4219}
4220
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05004221static inline void finish_cmd(struct CommandList *c)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004222{
Matt Gatese16a33a2012-05-01 11:43:11 -05004223 unsigned long flags;
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004224 int io_may_be_stalled = 0;
4225 struct ctlr_info *h = c->h;
Matt Gatese16a33a2012-05-01 11:43:11 -05004226
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004227 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004228 removeQ(c);
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004229
4230 /*
4231 * Check for possibly stalled i/o.
4232 *
4233 * If a fifo_full condition is encountered, requests will back up
4234 * in h->reqQ. This queue is only emptied out by start_io which is
4235 * only called when a new i/o request comes in. If no i/o's are
4236 * forthcoming, the i/o's in h->reqQ can get stuck. So we call
4237 * start_io from here if we detect such a danger.
4238 *
4239 * Normally, we shouldn't hit this case, but pounding on the
4240 * CCISS_PASSTHRU ioctl can provoke it. Only call start_io if
4241 * commands_outstanding is low. We want to avoid calling
4242 * start_io from in here as much as possible, and esp. don't
4243 * want to get in a cycle where we call start_io every time
4244 * through here.
4245 */
4246 if (unlikely(h->fifo_recently_full) &&
4247 h->commands_outstanding < 5)
4248 io_may_be_stalled = 1;
4249
4250 spin_unlock_irqrestore(&h->lock, flags);
4251
Stephen M. Camerone85c5972012-05-01 11:43:42 -05004252 dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
Matt Gatese1f7de02014-02-18 13:55:17 -06004253 if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI))
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05004254 complete_scsi_command(c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004255 else if (c->cmd_type == CMD_IOCTL_PEND)
4256 complete(c->waiting);
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004257 if (unlikely(io_may_be_stalled))
4258 start_io(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004259}
4260
Stephen M. Camerona104c992010-02-04 08:42:24 -06004261static inline u32 hpsa_tag_contains_index(u32 tag)
4262{
Stephen M. Camerona104c992010-02-04 08:42:24 -06004263 return tag & DIRECT_LOOKUP_BIT;
4264}
4265
4266static inline u32 hpsa_tag_to_index(u32 tag)
4267{
Stephen M. Camerona104c992010-02-04 08:42:24 -06004268 return tag >> DIRECT_LOOKUP_SHIFT;
4269}
4270
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004271
4272static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
Stephen M. Camerona104c992010-02-04 08:42:24 -06004273{
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004274#define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
4275#define HPSA_SIMPLE_ERROR_BITS 0x03
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06004276 if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004277 return tag & ~HPSA_SIMPLE_ERROR_BITS;
4278 return tag & ~HPSA_PERF_ERROR_BITS;
Stephen M. Camerona104c992010-02-04 08:42:24 -06004279}
4280
Don Brace303932f2010-02-04 08:42:40 -06004281/* process completion of an indexed ("direct lookup") command */
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004282static inline void process_indexed_cmd(struct ctlr_info *h,
Don Brace303932f2010-02-04 08:42:40 -06004283 u32 raw_tag)
4284{
4285 u32 tag_index;
4286 struct CommandList *c;
4287
4288 tag_index = hpsa_tag_to_index(raw_tag);
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004289 if (!bad_tag(h, tag_index, raw_tag)) {
4290 c = h->cmd_pool + tag_index;
4291 finish_cmd(c);
4292 }
Don Brace303932f2010-02-04 08:42:40 -06004293}
4294
4295/* process completion of a non-indexed command */
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004296static inline void process_nonindexed_cmd(struct ctlr_info *h,
Don Brace303932f2010-02-04 08:42:40 -06004297 u32 raw_tag)
4298{
4299 u32 tag;
4300 struct CommandList *c = NULL;
Matt Gatese16a33a2012-05-01 11:43:11 -05004301 unsigned long flags;
Don Brace303932f2010-02-04 08:42:40 -06004302
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004303 tag = hpsa_tag_discard_error_bits(h, raw_tag);
Matt Gatese16a33a2012-05-01 11:43:11 -05004304 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06004305 list_for_each_entry(c, &h->cmpQ, list) {
Don Brace303932f2010-02-04 08:42:40 -06004306 if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
Matt Gatese16a33a2012-05-01 11:43:11 -05004307 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05004308 finish_cmd(c);
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004309 return;
Don Brace303932f2010-02-04 08:42:40 -06004310 }
4311 }
Matt Gatese16a33a2012-05-01 11:43:11 -05004312 spin_unlock_irqrestore(&h->lock, flags);
Don Brace303932f2010-02-04 08:42:40 -06004313 bad_tag(h, h->nr_cmds + 1, raw_tag);
Don Brace303932f2010-02-04 08:42:40 -06004314}
4315
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004316/* Some controllers, like p400, will give us one interrupt
4317 * after a soft reset, even if we turned interrupts off.
4318 * Only need to check for this in the hpsa_xxx_discard_completions
4319 * functions.
4320 */
4321static int ignore_bogus_interrupt(struct ctlr_info *h)
4322{
4323 if (likely(!reset_devices))
4324 return 0;
4325
4326 if (likely(h->interrupts_enabled))
4327 return 0;
4328
4329 dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
4330 "(known firmware bug.) Ignoring.\n");
4331
4332 return 1;
4333}
4334
Matt Gates254f7962012-05-01 11:43:06 -05004335/*
4336 * Convert &h->q[x] (passed to interrupt handlers) back to h.
4337 * Relies on (h-q[x] == x) being true for x such that
4338 * 0 <= x < MAX_REPLY_QUEUES.
4339 */
4340static struct ctlr_info *queue_to_hba(u8 *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004341{
Matt Gates254f7962012-05-01 11:43:06 -05004342 return container_of((queue - *queue), struct ctlr_info, q[0]);
4343}
4344
4345static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
4346{
4347 struct ctlr_info *h = queue_to_hba(queue);
4348 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004349 u32 raw_tag;
4350
4351 if (ignore_bogus_interrupt(h))
4352 return IRQ_NONE;
4353
4354 if (interrupt_not_for_us(h))
4355 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004356 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004357 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05004358 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004359 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05004360 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004361 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004362 return IRQ_HANDLED;
4363}
4364
Matt Gates254f7962012-05-01 11:43:06 -05004365static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004366{
Matt Gates254f7962012-05-01 11:43:06 -05004367 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004368 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05004369 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004370
4371 if (ignore_bogus_interrupt(h))
4372 return IRQ_NONE;
4373
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004374 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05004375 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004376 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05004377 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004378 return IRQ_HANDLED;
4379}
4380
Matt Gates254f7962012-05-01 11:43:06 -05004381static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004382{
Matt Gates254f7962012-05-01 11:43:06 -05004383 struct ctlr_info *h = queue_to_hba((u8 *) queue);
Don Brace303932f2010-02-04 08:42:40 -06004384 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05004385 u8 q = *(u8 *) queue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004386
4387 if (interrupt_not_for_us(h))
4388 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004389 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004390 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05004391 raw_tag = get_next_completion(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004392 while (raw_tag != FIFO_EMPTY) {
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004393 if (likely(hpsa_tag_contains_index(raw_tag)))
4394 process_indexed_cmd(h, raw_tag);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004395 else
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004396 process_nonindexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05004397 raw_tag = next_command(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004398 }
4399 }
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004400 return IRQ_HANDLED;
4401}
4402
Matt Gates254f7962012-05-01 11:43:06 -05004403static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004404{
Matt Gates254f7962012-05-01 11:43:06 -05004405 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004406 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05004407 u8 q = *(u8 *) queue;
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004408
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004409 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05004410 raw_tag = get_next_completion(h, q);
Don Brace303932f2010-02-04 08:42:40 -06004411 while (raw_tag != FIFO_EMPTY) {
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004412 if (likely(hpsa_tag_contains_index(raw_tag)))
4413 process_indexed_cmd(h, raw_tag);
Don Brace303932f2010-02-04 08:42:40 -06004414 else
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004415 process_nonindexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05004416 raw_tag = next_command(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004417 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004418 return IRQ_HANDLED;
4419}
4420
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004421/* Send a message CDB to the firmware. Careful, this only works
4422 * in simple mode, not performant mode due to the tag lookup.
4423 * We only ever use this immediately after a controller reset.
4424 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004425static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
4426 unsigned char type)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004427{
4428 struct Command {
4429 struct CommandListHeader CommandHeader;
4430 struct RequestBlock Request;
4431 struct ErrDescriptor ErrorDescriptor;
4432 };
4433 struct Command *cmd;
4434 static const size_t cmd_sz = sizeof(*cmd) +
4435 sizeof(cmd->ErrorDescriptor);
4436 dma_addr_t paddr64;
4437 uint32_t paddr32, tag;
4438 void __iomem *vaddr;
4439 int i, err;
4440
4441 vaddr = pci_ioremap_bar(pdev, 0);
4442 if (vaddr == NULL)
4443 return -ENOMEM;
4444
4445 /* The Inbound Post Queue only accepts 32-bit physical addresses for the
4446 * CCISS commands, so they must be allocated from the lower 4GiB of
4447 * memory.
4448 */
4449 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
4450 if (err) {
4451 iounmap(vaddr);
4452 return -ENOMEM;
4453 }
4454
4455 cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
4456 if (cmd == NULL) {
4457 iounmap(vaddr);
4458 return -ENOMEM;
4459 }
4460
4461 /* This must fit, because of the 32-bit consistent DMA mask. Also,
4462 * although there's no guarantee, we assume that the address is at
4463 * least 4-byte aligned (most likely, it's page-aligned).
4464 */
4465 paddr32 = paddr64;
4466
4467 cmd->CommandHeader.ReplyQueue = 0;
4468 cmd->CommandHeader.SGList = 0;
4469 cmd->CommandHeader.SGTotal = 0;
4470 cmd->CommandHeader.Tag.lower = paddr32;
4471 cmd->CommandHeader.Tag.upper = 0;
4472 memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
4473
4474 cmd->Request.CDBLen = 16;
4475 cmd->Request.Type.Type = TYPE_MSG;
4476 cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
4477 cmd->Request.Type.Direction = XFER_NONE;
4478 cmd->Request.Timeout = 0; /* Don't time out */
4479 cmd->Request.CDB[0] = opcode;
4480 cmd->Request.CDB[1] = type;
4481 memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
4482 cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
4483 cmd->ErrorDescriptor.Addr.upper = 0;
4484 cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
4485
4486 writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
4487
4488 for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
4489 tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004490 if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr32)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004491 break;
4492 msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
4493 }
4494
4495 iounmap(vaddr);
4496
4497 /* we leak the DMA buffer here ... no choice since the controller could
4498 * still complete the command.
4499 */
4500 if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
4501 dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
4502 opcode, type);
4503 return -ETIMEDOUT;
4504 }
4505
4506 pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
4507
4508 if (tag & HPSA_ERROR_BIT) {
4509 dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
4510 opcode, type);
4511 return -EIO;
4512 }
4513
4514 dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
4515 opcode, type);
4516 return 0;
4517}
4518
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004519#define hpsa_noop(p) hpsa_message(p, 3, 0)
4520
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004521static int hpsa_controller_hard_reset(struct pci_dev *pdev,
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05004522 void * __iomem vaddr, u32 use_doorbell)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004523{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004524 u16 pmcsr;
4525 int pos;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004526
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004527 if (use_doorbell) {
4528 /* For everything after the P600, the PCI power state method
4529 * of resetting the controller doesn't work, so we have this
4530 * other way using the doorbell register.
4531 */
4532 dev_info(&pdev->dev, "using doorbell to reset controller\n");
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05004533 writel(use_doorbell, vaddr + SA5_DOORBELL);
Stephen M. Cameron85009232013-09-23 13:33:36 -05004534
4535 /* PMC hardware guys tell us we need a 5 second delay after
4536 * doorbell reset and before any attempt to talk to the board
4537 * at all to ensure that this actually works and doesn't fall
4538 * over in some weird corner cases.
4539 */
4540 msleep(5000);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004541 } else { /* Try to do it the PCI power state way */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004542
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004543 /* Quoting from the Open CISS Specification: "The Power
4544 * Management Control/Status Register (CSR) controls the power
4545 * state of the device. The normal operating state is D0,
4546 * CSR=00h. The software off state is D3, CSR=03h. To reset
4547 * the controller, place the interface device in D3 then to D0,
4548 * this causes a secondary PCI reset which will reset the
4549 * controller." */
4550
4551 pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
4552 if (pos == 0) {
4553 dev_err(&pdev->dev,
4554 "hpsa_reset_controller: "
4555 "PCI PM not supported\n");
4556 return -ENODEV;
4557 }
4558 dev_info(&pdev->dev, "using PCI PM to reset controller\n");
4559 /* enter the D3hot power management state */
4560 pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
4561 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
4562 pmcsr |= PCI_D3hot;
4563 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
4564
4565 msleep(500);
4566
4567 /* enter the D0 power management state */
4568 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
4569 pmcsr |= PCI_D0;
4570 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
Mike Millerc4853ef2011-10-21 08:19:43 +02004571
4572 /*
4573 * The P600 requires a small delay when changing states.
4574 * Otherwise we may think the board did not reset and we bail.
4575 * This for kdump only and is particular to the P600.
4576 */
4577 msleep(500);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004578 }
4579 return 0;
4580}
4581
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004582static void init_driver_version(char *driver_version, int len)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004583{
4584 memset(driver_version, 0, len);
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06004585 strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004586}
4587
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004588static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004589{
4590 char *driver_version;
4591 int i, size = sizeof(cfgtable->driver_version);
4592
4593 driver_version = kmalloc(size, GFP_KERNEL);
4594 if (!driver_version)
4595 return -ENOMEM;
4596
4597 init_driver_version(driver_version, size);
4598 for (i = 0; i < size; i++)
4599 writeb(driver_version[i], &cfgtable->driver_version[i]);
4600 kfree(driver_version);
4601 return 0;
4602}
4603
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004604static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
4605 unsigned char *driver_ver)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004606{
4607 int i;
4608
4609 for (i = 0; i < sizeof(cfgtable->driver_version); i++)
4610 driver_ver[i] = readb(&cfgtable->driver_version[i]);
4611}
4612
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004613static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004614{
4615
4616 char *driver_ver, *old_driver_ver;
4617 int rc, size = sizeof(cfgtable->driver_version);
4618
4619 old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
4620 if (!old_driver_ver)
4621 return -ENOMEM;
4622 driver_ver = old_driver_ver + size;
4623
4624 /* After a reset, the 32 bytes of "driver version" in the cfgtable
4625 * should have been changed, otherwise we know the reset failed.
4626 */
4627 init_driver_version(old_driver_ver, size);
4628 read_driver_ver_from_cfgtable(cfgtable, driver_ver);
4629 rc = !memcmp(driver_ver, old_driver_ver, size);
4630 kfree(old_driver_ver);
4631 return rc;
4632}
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004633/* This does a hard reset of the controller using PCI power management
4634 * states or the using the doorbell register.
4635 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004636static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev)
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004637{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004638 u64 cfg_offset;
4639 u32 cfg_base_addr;
4640 u64 cfg_base_addr_index;
4641 void __iomem *vaddr;
4642 unsigned long paddr;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004643 u32 misc_fw_support;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06004644 int rc;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004645 struct CfgTable __iomem *cfgtable;
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05004646 u32 use_doorbell;
Stephen M. Cameron18867652010-06-16 13:51:45 -05004647 u32 board_id;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06004648 u16 command_register;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004649
4650 /* For controllers as old as the P600, this is very nearly
4651 * the same thing as
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004652 *
4653 * pci_save_state(pci_dev);
4654 * pci_set_power_state(pci_dev, PCI_D3hot);
4655 * pci_set_power_state(pci_dev, PCI_D0);
4656 * pci_restore_state(pci_dev);
4657 *
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004658 * For controllers newer than the P600, the pci power state
4659 * method of resetting doesn't work so we have another way
4660 * using the doorbell register.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004661 */
Stephen M. Cameron18867652010-06-16 13:51:45 -05004662
Stephen M. Cameron25c1e56a2011-01-06 14:48:18 -06004663 rc = hpsa_lookup_board_id(pdev, &board_id);
Stephen M. Cameron46380782011-05-03 15:00:01 -05004664 if (rc < 0 || !ctlr_is_resettable(board_id)) {
Stephen M. Cameron25c1e56a2011-01-06 14:48:18 -06004665 dev_warn(&pdev->dev, "Not resetting device.\n");
4666 return -ENODEV;
4667 }
Stephen M. Cameron46380782011-05-03 15:00:01 -05004668
4669 /* if controller is soft- but not hard resettable... */
4670 if (!ctlr_is_hard_resettable(board_id))
4671 return -ENOTSUPP; /* try soft reset later. */
Stephen M. Cameron18867652010-06-16 13:51:45 -05004672
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06004673 /* Save the PCI command register */
4674 pci_read_config_word(pdev, 4, &command_register);
4675 /* Turn the board off. This is so that later pci_restore_state()
4676 * won't turn the board on before the rest of config space is ready.
4677 */
4678 pci_disable_device(pdev);
4679 pci_save_state(pdev);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004680
4681 /* find the first memory BAR, so we can find the cfg table */
4682 rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
4683 if (rc)
4684 return rc;
4685 vaddr = remap_pci_mem(paddr, 0x250);
4686 if (!vaddr)
4687 return -ENOMEM;
4688
4689 /* find cfgtable in order to check if reset via doorbell is supported */
4690 rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
4691 &cfg_base_addr_index, &cfg_offset);
4692 if (rc)
4693 goto unmap_vaddr;
4694 cfgtable = remap_pci_mem(pci_resource_start(pdev,
4695 cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
4696 if (!cfgtable) {
4697 rc = -ENOMEM;
4698 goto unmap_vaddr;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004699 }
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004700 rc = write_driver_ver_to_cfgtable(cfgtable);
4701 if (rc)
4702 goto unmap_vaddr;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004703
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05004704 /* If reset via doorbell register is supported, use that.
4705 * There are two such methods. Favor the newest method.
4706 */
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004707 misc_fw_support = readl(&cfgtable->misc_fw_support);
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05004708 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
4709 if (use_doorbell) {
4710 use_doorbell = DOORBELL_CTLR_RESET2;
4711 } else {
4712 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
4713 if (use_doorbell) {
Mike Millerfba63092011-10-13 11:44:06 -05004714 dev_warn(&pdev->dev, "Soft reset not supported. "
4715 "Firmware update is required.\n");
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004716 rc = -ENOTSUPP; /* try soft reset */
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05004717 goto unmap_cfgtable;
4718 }
4719 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004720
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004721 rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
4722 if (rc)
4723 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004724
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06004725 pci_restore_state(pdev);
4726 rc = pci_enable_device(pdev);
4727 if (rc) {
4728 dev_warn(&pdev->dev, "failed to enable device.\n");
4729 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004730 }
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06004731 pci_write_config_word(pdev, 4, command_register);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004732
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004733 /* Some devices (notably the HP Smart Array 5i Controller)
4734 need a little pause here */
4735 msleep(HPSA_POST_RESET_PAUSE_MSECS);
4736
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004737 rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
4738 if (rc) {
4739 dev_warn(&pdev->dev,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004740 "failed waiting for board to become ready "
4741 "after hard reset\n");
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004742 goto unmap_cfgtable;
4743 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004744
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004745 rc = controller_reset_failed(vaddr);
4746 if (rc < 0)
4747 goto unmap_cfgtable;
4748 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004749 dev_warn(&pdev->dev, "Unable to successfully reset "
4750 "controller. Will try soft reset.\n");
4751 rc = -ENOTSUPP;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004752 } else {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004753 dev_info(&pdev->dev, "board ready after hard reset.\n");
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004754 }
4755
4756unmap_cfgtable:
4757 iounmap(cfgtable);
4758
4759unmap_vaddr:
4760 iounmap(vaddr);
4761 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004762}
4763
4764/*
4765 * We cannot read the structure directly, for portability we must use
4766 * the io functions.
4767 * This is for debug only.
4768 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004769static void print_cfg_table(struct device *dev, struct CfgTable *tb)
4770{
Stephen M. Cameron58f86652010-05-27 15:13:58 -05004771#ifdef HPSA_DEBUG
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004772 int i;
4773 char temp_name[17];
4774
4775 dev_info(dev, "Controller Configuration information\n");
4776 dev_info(dev, "------------------------------------\n");
4777 for (i = 0; i < 4; i++)
4778 temp_name[i] = readb(&(tb->Signature[i]));
4779 temp_name[4] = '\0';
4780 dev_info(dev, " Signature = %s\n", temp_name);
4781 dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
4782 dev_info(dev, " Transport methods supported = 0x%x\n",
4783 readl(&(tb->TransportSupport)));
4784 dev_info(dev, " Transport methods active = 0x%x\n",
4785 readl(&(tb->TransportActive)));
4786 dev_info(dev, " Requested transport Method = 0x%x\n",
4787 readl(&(tb->HostWrite.TransportRequest)));
4788 dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
4789 readl(&(tb->HostWrite.CoalIntDelay)));
4790 dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
4791 readl(&(tb->HostWrite.CoalIntCount)));
4792 dev_info(dev, " Max outstanding commands = 0x%d\n",
4793 readl(&(tb->CmdsOutMax)));
4794 dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
4795 for (i = 0; i < 16; i++)
4796 temp_name[i] = readb(&(tb->ServerName[i]));
4797 temp_name[16] = '\0';
4798 dev_info(dev, " Server Name = %s\n", temp_name);
4799 dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
4800 readl(&(tb->HeartBeat)));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004801#endif /* HPSA_DEBUG */
Stephen M. Cameron58f86652010-05-27 15:13:58 -05004802}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004803
4804static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
4805{
4806 int i, offset, mem_type, bar_type;
4807
4808 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
4809 return 0;
4810 offset = 0;
4811 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
4812 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
4813 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
4814 offset += 4;
4815 else {
4816 mem_type = pci_resource_flags(pdev, i) &
4817 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
4818 switch (mem_type) {
4819 case PCI_BASE_ADDRESS_MEM_TYPE_32:
4820 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
4821 offset += 4; /* 32 bit */
4822 break;
4823 case PCI_BASE_ADDRESS_MEM_TYPE_64:
4824 offset += 8;
4825 break;
4826 default: /* reserved in PCI 2.2 */
4827 dev_warn(&pdev->dev,
4828 "base address is invalid\n");
4829 return -1;
4830 break;
4831 }
4832 }
4833 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
4834 return i + 1;
4835 }
4836 return -1;
4837}
4838
4839/* If MSI/MSI-X is supported by the kernel we will try to enable it on
4840 * controllers that are capable. If not, we use IO-APIC mode.
4841 */
4842
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004843static void hpsa_interrupt_mode(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004844{
4845#ifdef CONFIG_PCI_MSI
Matt Gates254f7962012-05-01 11:43:06 -05004846 int err, i;
4847 struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
4848
4849 for (i = 0; i < MAX_REPLY_QUEUES; i++) {
4850 hpsa_msix_entries[i].vector = 0;
4851 hpsa_msix_entries[i].entry = i;
4852 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004853
4854 /* Some boards advertise MSI but don't really support it */
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05004855 if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
4856 (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004857 goto default_int_mode;
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05004858 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
4859 dev_info(&h->pdev->dev, "MSIX\n");
Hannes Reineckeeee0f032014-01-15 13:30:53 +01004860 h->msix_vector = MAX_REPLY_QUEUES;
Matt Gates254f7962012-05-01 11:43:06 -05004861 err = pci_enable_msix(h->pdev, hpsa_msix_entries,
Hannes Reineckeeee0f032014-01-15 13:30:53 +01004862 h->msix_vector);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004863 if (err > 0) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05004864 dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004865 "available\n", err);
Hannes Reineckeeee0f032014-01-15 13:30:53 +01004866 h->msix_vector = err;
4867 err = pci_enable_msix(h->pdev, hpsa_msix_entries,
4868 h->msix_vector);
4869 }
4870 if (!err) {
4871 for (i = 0; i < h->msix_vector; i++)
4872 h->intr[i] = hpsa_msix_entries[i].vector;
4873 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004874 } else {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05004875 dev_warn(&h->pdev->dev, "MSI-X init failed %d\n",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004876 err);
Hannes Reineckeeee0f032014-01-15 13:30:53 +01004877 h->msix_vector = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004878 goto default_int_mode;
4879 }
4880 }
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05004881 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
4882 dev_info(&h->pdev->dev, "MSI\n");
4883 if (!pci_enable_msi(h->pdev))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004884 h->msi_vector = 1;
4885 else
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05004886 dev_warn(&h->pdev->dev, "MSI init failed\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004887 }
4888default_int_mode:
4889#endif /* CONFIG_PCI_MSI */
4890 /* if we get here we're going to use the default interrupt mode */
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004891 h->intr[h->intr_mode] = h->pdev->irq;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004892}
4893
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004894static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05004895{
4896 int i;
4897 u32 subsystem_vendor_id, subsystem_device_id;
4898
4899 subsystem_vendor_id = pdev->subsystem_vendor;
4900 subsystem_device_id = pdev->subsystem_device;
4901 *board_id = ((subsystem_device_id << 16) & 0xffff0000) |
4902 subsystem_vendor_id;
4903
4904 for (i = 0; i < ARRAY_SIZE(products); i++)
4905 if (*board_id == products[i].board_id)
4906 return i;
4907
Stephen M. Cameron6798cc02010-06-16 13:51:20 -05004908 if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
4909 subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
4910 !hpsa_allow_any) {
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05004911 dev_warn(&pdev->dev, "unrecognized board ID: "
4912 "0x%08x, ignoring.\n", *board_id);
4913 return -ENODEV;
4914 }
4915 return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
4916}
4917
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004918static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
4919 unsigned long *memory_bar)
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05004920{
4921 int i;
4922
4923 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05004924 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05004925 /* addressing mode bits already removed */
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05004926 *memory_bar = pci_resource_start(pdev, i);
4927 dev_dbg(&pdev->dev, "memory BAR = %lx\n",
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05004928 *memory_bar);
4929 return 0;
4930 }
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05004931 dev_warn(&pdev->dev, "no memory BAR found\n");
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05004932 return -ENODEV;
4933}
4934
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004935static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
4936 int wait_for_ready)
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05004937{
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004938 int i, iterations;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05004939 u32 scratchpad;
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004940 if (wait_for_ready)
4941 iterations = HPSA_BOARD_READY_ITERATIONS;
4942 else
4943 iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05004944
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004945 for (i = 0; i < iterations; i++) {
4946 scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
4947 if (wait_for_ready) {
4948 if (scratchpad == HPSA_FIRMWARE_READY)
4949 return 0;
4950 } else {
4951 if (scratchpad != HPSA_FIRMWARE_READY)
4952 return 0;
4953 }
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05004954 msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
4955 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004956 dev_warn(&pdev->dev, "board not ready, timed out.\n");
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05004957 return -ENODEV;
4958}
4959
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004960static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
4961 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
4962 u64 *cfg_offset)
Stephen M. Camerona51fd472010-06-16 13:51:30 -05004963{
4964 *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
4965 *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
4966 *cfg_base_addr &= (u32) 0x0000ffff;
4967 *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
4968 if (*cfg_base_addr_index == -1) {
4969 dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
4970 return -ENODEV;
4971 }
4972 return 0;
4973}
4974
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004975static int hpsa_find_cfgtables(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004976{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004977 u64 cfg_offset;
4978 u32 cfg_base_addr;
4979 u64 cfg_base_addr_index;
Don Brace303932f2010-02-04 08:42:40 -06004980 u32 trans_offset;
Stephen M. Camerona51fd472010-06-16 13:51:30 -05004981 int rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05004982
Stephen M. Camerona51fd472010-06-16 13:51:30 -05004983 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
4984 &cfg_base_addr_index, &cfg_offset);
4985 if (rc)
4986 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05004987 h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
Stephen M. Camerona51fd472010-06-16 13:51:30 -05004988 cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
Stephen M. Cameron77c44952010-05-27 15:13:17 -05004989 if (!h->cfgtable)
4990 return -ENOMEM;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004991 rc = write_driver_ver_to_cfgtable(h->cfgtable);
4992 if (rc)
4993 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05004994 /* Find performant mode table. */
Stephen M. Camerona51fd472010-06-16 13:51:30 -05004995 trans_offset = readl(&h->cfgtable->TransMethodOffset);
Stephen M. Cameron77c44952010-05-27 15:13:17 -05004996 h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
4997 cfg_base_addr_index)+cfg_offset+trans_offset,
4998 sizeof(*h->transtable));
4999 if (!h->transtable)
5000 return -ENOMEM;
5001 return 0;
5002}
5003
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005004static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05005005{
5006 h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
Stephen M. Cameron72ceeae2011-01-06 14:48:13 -06005007
5008 /* Limit commands in memory limited kdump scenario. */
5009 if (reset_devices && h->max_commands > 32)
5010 h->max_commands = 32;
5011
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05005012 if (h->max_commands < 16) {
5013 dev_warn(&h->pdev->dev, "Controller reports "
5014 "max supported commands of %d, an obvious lie. "
5015 "Using 16. Ensure that firmware is up to date.\n",
5016 h->max_commands);
5017 h->max_commands = 16;
5018 }
5019}
5020
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005021/* Interrogate the hardware for some limits:
5022 * max commands, max SG elements without chaining, and with chaining,
5023 * SG chain block size, etc.
5024 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005025static void hpsa_find_board_params(struct ctlr_info *h)
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005026{
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05005027 hpsa_get_max_perf_mode_cmds(h);
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005028 h->nr_cmds = h->max_commands - 4; /* Allow room for some ioctls */
5029 h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005030 h->fw_support = readl(&(h->cfgtable->misc_fw_support));
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005031 /*
5032 * Limit in-command s/g elements to 32 save dma'able memory.
5033 * Howvever spec says if 0, use 31
5034 */
5035 h->max_cmd_sg_entries = 31;
5036 if (h->maxsgentries > 512) {
5037 h->max_cmd_sg_entries = 32;
5038 h->chainsize = h->maxsgentries - h->max_cmd_sg_entries + 1;
5039 h->maxsgentries--; /* save one for chain pointer */
5040 } else {
5041 h->maxsgentries = 31; /* default to traditional values */
5042 h->chainsize = 0;
5043 }
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005044
5045 /* Find out what task management functions are supported and cache */
5046 h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
Scott Teel0e7a7fc2014-02-18 13:55:59 -06005047 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
5048 dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
5049 if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
5050 dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005051}
5052
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05005053static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
5054{
Akinobu Mita0fc9fd42012-04-04 22:14:59 +09005055 if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05005056 dev_warn(&h->pdev->dev, "not a valid CISS config table\n");
5057 return false;
5058 }
5059 return true;
5060}
5061
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005062static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005063{
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005064 u32 driver_support;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005065
Stephen M. Cameron28e13442013-12-04 17:10:21 -06005066#ifdef CONFIG_X86
5067 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005068 driver_support = readl(&(h->cfgtable->driver_support));
5069 driver_support |= ENABLE_SCSI_PREFETCH;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005070#endif
Stephen M. Cameron28e13442013-12-04 17:10:21 -06005071 driver_support |= ENABLE_UNIT_ATTN;
5072 writel(driver_support, &(h->cfgtable->driver_support));
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005073}
5074
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05005075/* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result
5076 * in a prefetch beyond physical memory.
5077 */
5078static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
5079{
5080 u32 dma_prefetch;
5081
5082 if (h->board_id != 0x3225103C)
5083 return;
5084 dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
5085 dma_prefetch |= 0x8000;
5086 writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
5087}
5088
Stephen M. Cameron76438d02014-02-18 13:55:43 -06005089static void hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
5090{
5091 int i;
5092 u32 doorbell_value;
5093 unsigned long flags;
5094 /* wait until the clear_event_notify bit 6 is cleared by controller. */
5095 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
5096 spin_lock_irqsave(&h->lock, flags);
5097 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
5098 spin_unlock_irqrestore(&h->lock, flags);
5099 if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
5100 break;
5101 /* delay and try again */
5102 msleep(20);
5103 }
5104}
5105
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005106static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005107{
5108 int i;
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06005109 u32 doorbell_value;
5110 unsigned long flags;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005111
5112 /* under certain very rare conditions, this can take awhile.
5113 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
5114 * as we enter this code.)
5115 */
5116 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06005117 spin_lock_irqsave(&h->lock, flags);
5118 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
5119 spin_unlock_irqrestore(&h->lock, flags);
Dan Carpenter382be662011-02-15 15:33:13 -06005120 if (!(doorbell_value & CFGTBL_ChangeReq))
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005121 break;
5122 /* delay and try again */
Stephen M. Cameron60d3f5b2011-01-06 14:48:34 -06005123 usleep_range(10000, 20000);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005124 }
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005125}
5126
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005127static int hpsa_enter_simple_mode(struct ctlr_info *h)
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005128{
5129 u32 trans_support;
5130
5131 trans_support = readl(&(h->cfgtable->TransportSupport));
5132 if (!(trans_support & SIMPLE_MODE))
5133 return -ENOTSUPP;
5134
5135 h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005136
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005137 /* Update the field, and then ring the doorbell */
5138 writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
5139 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
5140 hpsa_wait_for_mode_change_ack(h);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005141 print_cfg_table(&h->pdev->dev, h->cfgtable);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005142 if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
5143 goto error;
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06005144 h->transMethod = CFGTBL_Trans_Simple;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005145 return 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005146error:
5147 dev_warn(&h->pdev->dev, "unable to get board into simple mode\n");
5148 return -ENODEV;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005149}
5150
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005151static int hpsa_pci_init(struct ctlr_info *h)
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005152{
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005153 int prod_index, err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005154
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05005155 prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
5156 if (prod_index < 0)
5157 return -ENODEV;
5158 h->product_name = products[prod_index].product_name;
5159 h->access = *(products[prod_index].access);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005160
Matthew Garrette5a44df2011-11-11 11:14:23 -05005161 pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
5162 PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
5163
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005164 err = pci_enable_device(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005165 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005166 dev_warn(&h->pdev->dev, "unable to enable PCI device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005167 return err;
5168 }
5169
Stephen M. Cameron5cb460a2012-05-01 11:42:20 -05005170 /* Enable bus mastering (pci_disable_device may disable this) */
5171 pci_set_master(h->pdev);
5172
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06005173 err = pci_request_regions(h->pdev, HPSA);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005174 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005175 dev_err(&h->pdev->dev,
5176 "cannot obtain PCI resources, aborting\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005177 return err;
5178 }
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05005179 hpsa_interrupt_mode(h);
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05005180 err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005181 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005182 goto err_out_free_res;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005183 h->vaddr = remap_pci_mem(h->paddr, 0x250);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05005184 if (!h->vaddr) {
5185 err = -ENOMEM;
5186 goto err_out_free_res;
5187 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005188 err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005189 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005190 goto err_out_free_res;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005191 err = hpsa_find_cfgtables(h);
5192 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005193 goto err_out_free_res;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005194 hpsa_find_board_params(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005195
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05005196 if (!hpsa_CISS_signature_present(h)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005197 err = -ENODEV;
5198 goto err_out_free_res;
5199 }
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005200 hpsa_set_driver_support_bits(h);
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05005201 hpsa_p600_dma_prefetch_quirk(h);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005202 err = hpsa_enter_simple_mode(h);
5203 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005204 goto err_out_free_res;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005205 return 0;
5206
5207err_out_free_res:
Stephen M. Cameron204892e2010-05-27 15:13:22 -05005208 if (h->transtable)
5209 iounmap(h->transtable);
5210 if (h->cfgtable)
5211 iounmap(h->cfgtable);
5212 if (h->vaddr)
5213 iounmap(h->vaddr);
Stephen M. Cameronf0bd0b682012-05-01 11:42:09 -05005214 pci_disable_device(h->pdev);
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005215 pci_release_regions(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005216 return err;
5217}
5218
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005219static void hpsa_hba_inquiry(struct ctlr_info *h)
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06005220{
5221 int rc;
5222
5223#define HBA_INQUIRY_BYTE_COUNT 64
5224 h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
5225 if (!h->hba_inquiry_data)
5226 return;
5227 rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
5228 h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
5229 if (rc != 0) {
5230 kfree(h->hba_inquiry_data);
5231 h->hba_inquiry_data = NULL;
5232 }
5233}
5234
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005235static int hpsa_init_reset_devices(struct pci_dev *pdev)
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005236{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005237 int rc, i;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005238
5239 if (!reset_devices)
5240 return 0;
5241
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005242 /* Reset the controller with a PCI power-cycle or via doorbell */
5243 rc = hpsa_kdump_hard_reset_controller(pdev);
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005244
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005245 /* -ENOTSUPP here means we cannot reset the controller
5246 * but it's already (and still) up and running in
Stephen M. Cameron18867652010-06-16 13:51:45 -05005247 * "performant mode". Or, it might be 640x, which can't reset
5248 * due to concerns about shared bbwc between 6402/6404 pair.
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005249 */
5250 if (rc == -ENOTSUPP)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005251 return rc; /* just try to do the kdump anyhow. */
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005252 if (rc)
5253 return -ENODEV;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005254
5255 /* Now try to get the controller to respond to a no-op */
Stephen M. Cameron2b870cb2011-05-03 14:59:36 -05005256 dev_warn(&pdev->dev, "Waiting for controller to respond to no-op\n");
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005257 for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
5258 if (hpsa_noop(pdev) == 0)
5259 break;
5260 else
5261 dev_warn(&pdev->dev, "no-op failed%s\n",
5262 (i < 11 ? "; re-trying" : ""));
5263 }
5264 return 0;
5265}
5266
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005267static int hpsa_allocate_cmd_pool(struct ctlr_info *h)
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05005268{
5269 h->cmd_pool_bits = kzalloc(
5270 DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
5271 sizeof(unsigned long), GFP_KERNEL);
5272 h->cmd_pool = pci_alloc_consistent(h->pdev,
5273 h->nr_cmds * sizeof(*h->cmd_pool),
5274 &(h->cmd_pool_dhandle));
5275 h->errinfo_pool = pci_alloc_consistent(h->pdev,
5276 h->nr_cmds * sizeof(*h->errinfo_pool),
5277 &(h->errinfo_pool_dhandle));
5278 if ((h->cmd_pool_bits == NULL)
5279 || (h->cmd_pool == NULL)
5280 || (h->errinfo_pool == NULL)) {
5281 dev_err(&h->pdev->dev, "out of memory in %s", __func__);
5282 return -ENOMEM;
5283 }
5284 return 0;
5285}
5286
5287static void hpsa_free_cmd_pool(struct ctlr_info *h)
5288{
5289 kfree(h->cmd_pool_bits);
5290 if (h->cmd_pool)
5291 pci_free_consistent(h->pdev,
5292 h->nr_cmds * sizeof(struct CommandList),
5293 h->cmd_pool, h->cmd_pool_dhandle);
5294 if (h->errinfo_pool)
5295 pci_free_consistent(h->pdev,
5296 h->nr_cmds * sizeof(struct ErrorInfo),
5297 h->errinfo_pool,
5298 h->errinfo_pool_dhandle);
Matt Gatese1f7de02014-02-18 13:55:17 -06005299 if (h->ioaccel_cmd_pool)
5300 pci_free_consistent(h->pdev,
5301 h->nr_cmds * sizeof(struct io_accel1_cmd),
5302 h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05005303}
5304
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05005305static int hpsa_request_irq(struct ctlr_info *h,
5306 irqreturn_t (*msixhandler)(int, void *),
5307 irqreturn_t (*intxhandler)(int, void *))
5308{
Matt Gates254f7962012-05-01 11:43:06 -05005309 int rc, i;
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05005310
Matt Gates254f7962012-05-01 11:43:06 -05005311 /*
5312 * initialize h->q[x] = x so that interrupt handlers know which
5313 * queue to process.
5314 */
5315 for (i = 0; i < MAX_REPLY_QUEUES; i++)
5316 h->q[i] = (u8) i;
5317
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005318 if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
Matt Gates254f7962012-05-01 11:43:06 -05005319 /* If performant mode and MSI-X, use multiple reply queues */
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005320 for (i = 0; i < h->msix_vector; i++)
Matt Gates254f7962012-05-01 11:43:06 -05005321 rc = request_irq(h->intr[i], msixhandler,
5322 0, h->devname,
5323 &h->q[i]);
5324 } else {
5325 /* Use single reply pool */
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005326 if (h->msix_vector > 0 || h->msi_vector) {
Matt Gates254f7962012-05-01 11:43:06 -05005327 rc = request_irq(h->intr[h->intr_mode],
5328 msixhandler, 0, h->devname,
5329 &h->q[h->intr_mode]);
5330 } else {
5331 rc = request_irq(h->intr[h->intr_mode],
5332 intxhandler, IRQF_SHARED, h->devname,
5333 &h->q[h->intr_mode]);
5334 }
5335 }
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05005336 if (rc) {
5337 dev_err(&h->pdev->dev, "unable to get irq %d for %s\n",
5338 h->intr[h->intr_mode], h->devname);
5339 return -ENODEV;
5340 }
5341 return 0;
5342}
5343
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005344static int hpsa_kdump_soft_reset(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005345{
5346 if (hpsa_send_host_reset(h, RAID_CTLR_LUNID,
5347 HPSA_RESET_TYPE_CONTROLLER)) {
5348 dev_warn(&h->pdev->dev, "Resetting array controller failed.\n");
5349 return -EIO;
5350 }
5351
5352 dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
5353 if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) {
5354 dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
5355 return -1;
5356 }
5357
5358 dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
5359 if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) {
5360 dev_warn(&h->pdev->dev, "Board failed to become ready "
5361 "after soft reset.\n");
5362 return -1;
5363 }
5364
5365 return 0;
5366}
5367
Matt Gates254f7962012-05-01 11:43:06 -05005368static void free_irqs(struct ctlr_info *h)
5369{
5370 int i;
5371
5372 if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
5373 /* Single reply queue, only one irq to free */
5374 i = h->intr_mode;
5375 free_irq(h->intr[i], &h->q[i]);
5376 return;
5377 }
5378
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005379 for (i = 0; i < h->msix_vector; i++)
Matt Gates254f7962012-05-01 11:43:06 -05005380 free_irq(h->intr[i], &h->q[i]);
5381}
5382
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05005383static void hpsa_free_irqs_and_disable_msix(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005384{
Matt Gates254f7962012-05-01 11:43:06 -05005385 free_irqs(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005386#ifdef CONFIG_PCI_MSI
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05005387 if (h->msix_vector) {
5388 if (h->pdev->msix_enabled)
5389 pci_disable_msix(h->pdev);
5390 } else if (h->msi_vector) {
5391 if (h->pdev->msi_enabled)
5392 pci_disable_msi(h->pdev);
5393 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005394#endif /* CONFIG_PCI_MSI */
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05005395}
5396
5397static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
5398{
5399 hpsa_free_irqs_and_disable_msix(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005400 hpsa_free_sg_chain_blocks(h);
5401 hpsa_free_cmd_pool(h);
Matt Gatese1f7de02014-02-18 13:55:17 -06005402 kfree(h->ioaccel1_blockFetchTable);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005403 kfree(h->blockFetchTable);
5404 pci_free_consistent(h->pdev, h->reply_pool_size,
5405 h->reply_pool, h->reply_pool_dhandle);
5406 if (h->vaddr)
5407 iounmap(h->vaddr);
5408 if (h->transtable)
5409 iounmap(h->transtable);
5410 if (h->cfgtable)
5411 iounmap(h->cfgtable);
5412 pci_release_regions(h->pdev);
5413 kfree(h);
5414}
5415
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005416/* Called when controller lockup detected. */
5417static void fail_all_cmds_on_list(struct ctlr_info *h, struct list_head *list)
5418{
5419 struct CommandList *c = NULL;
5420
5421 assert_spin_locked(&h->lock);
5422 /* Mark all outstanding commands as failed and complete them. */
5423 while (!list_empty(list)) {
5424 c = list_entry(list->next, struct CommandList, list);
5425 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05005426 finish_cmd(c);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005427 }
5428}
5429
5430static void controller_lockup_detected(struct ctlr_info *h)
5431{
5432 unsigned long flags;
5433
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005434 h->access.set_intr_mask(h, HPSA_INTR_OFF);
5435 spin_lock_irqsave(&h->lock, flags);
5436 h->lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
5437 spin_unlock_irqrestore(&h->lock, flags);
5438 dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x\n",
5439 h->lockup_detected);
5440 pci_disable_device(h->pdev);
5441 spin_lock_irqsave(&h->lock, flags);
5442 fail_all_cmds_on_list(h, &h->cmpQ);
5443 fail_all_cmds_on_list(h, &h->reqQ);
5444 spin_unlock_irqrestore(&h->lock, flags);
5445}
5446
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005447static void detect_controller_lockup(struct ctlr_info *h)
5448{
5449 u64 now;
5450 u32 heartbeat;
5451 unsigned long flags;
5452
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005453 now = get_jiffies_64();
5454 /* If we've received an interrupt recently, we're ok. */
5455 if (time_after64(h->last_intr_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05005456 (h->heartbeat_sample_interval), now))
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005457 return;
5458
5459 /*
5460 * If we've already checked the heartbeat recently, we're ok.
5461 * This could happen if someone sends us a signal. We
5462 * otherwise don't care about signals in this thread.
5463 */
5464 if (time_after64(h->last_heartbeat_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05005465 (h->heartbeat_sample_interval), now))
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005466 return;
5467
5468 /* If heartbeat has not changed since we last looked, we're not ok. */
5469 spin_lock_irqsave(&h->lock, flags);
5470 heartbeat = readl(&h->cfgtable->HeartBeat);
5471 spin_unlock_irqrestore(&h->lock, flags);
5472 if (h->last_heartbeat == heartbeat) {
5473 controller_lockup_detected(h);
5474 return;
5475 }
5476
5477 /* We're ok. */
5478 h->last_heartbeat = heartbeat;
5479 h->last_heartbeat_timestamp = now;
5480}
5481
Stephen M. Cameron76438d02014-02-18 13:55:43 -06005482static int hpsa_kickoff_rescan(struct ctlr_info *h)
5483{
5484 int i;
5485 char *event_type;
5486
5487 /* Ask the controller to clear the events we're handling. */
5488 if (h->transMethod & (CFGTBL_Trans_io_accel1) &&
5489 (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
5490 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
5491
5492 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
5493 event_type = "state change";
5494 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
5495 event_type = "configuration change";
5496 /* Stop sending new RAID offload reqs via the IO accelerator */
5497 scsi_block_requests(h->scsi_host);
5498 for (i = 0; i < h->ndevices; i++)
5499 h->dev[i]->offload_enabled = 0;
5500 hpsa_drain_commands(h);
5501 /* Set 'accelerator path config change' bit */
5502 dev_warn(&h->pdev->dev,
5503 "Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
5504 h->events, event_type);
5505 writel(h->events, &(h->cfgtable->clear_event_notify));
5506 /* Set the "clear event notify field update" bit 6 */
5507 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
5508 /* Wait until ctlr clears 'clear event notify field', bit 6 */
5509 hpsa_wait_for_clear_event_notify_ack(h);
5510 scsi_unblock_requests(h->scsi_host);
5511 } else {
5512 /* Acknowledge controller notification events. */
5513 writel(h->events, &(h->cfgtable->clear_event_notify));
5514 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
5515 hpsa_wait_for_clear_event_notify_ack(h);
5516#if 0
5517 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
5518 hpsa_wait_for_mode_change_ack(h);
5519#endif
5520 }
5521
5522 /* Something in the device list may have changed to trigger
5523 * the event, so do a rescan.
5524 */
5525 hpsa_scan_start(h->scsi_host);
5526 /* release reference taken on scsi host in check_controller_events */
5527 scsi_host_put(h->scsi_host);
5528 return 0;
5529}
5530
5531/* Check a register on the controller to see if there are configuration
5532 * changes (added/changed/removed logical drives, etc.) which mean that
5533 * we should rescan the controller for devices. If so, add the controller
5534 * to the list of controllers needing to be rescanned, and gets a
5535 * reference to the associated scsi_host.
5536 */
5537static void hpsa_ctlr_needs_rescan(struct ctlr_info *h)
5538{
5539 if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
5540 return;
5541
5542 h->events = readl(&(h->cfgtable->event_notify));
5543 if (!h->events)
5544 return;
5545
5546 /*
5547 * Take a reference on scsi host for the duration of the scan
5548 * Release in hpsa_kickoff_rescan(). No lock needed for scan_list
5549 * as only a single thread accesses this list.
5550 */
5551 scsi_host_get(h->scsi_host);
5552 hpsa_kickoff_rescan(h);
5553}
5554
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005555static void hpsa_monitor_ctlr_worker(struct work_struct *work)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005556{
5557 unsigned long flags;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005558 struct ctlr_info *h = container_of(to_delayed_work(work),
5559 struct ctlr_info, monitor_ctlr_work);
5560 detect_controller_lockup(h);
5561 if (h->lockup_detected)
5562 return;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06005563 hpsa_ctlr_needs_rescan(h);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005564 spin_lock_irqsave(&h->lock, flags);
5565 if (h->remove_in_progress) {
5566 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005567 return;
5568 }
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005569 schedule_delayed_work(&h->monitor_ctlr_work,
5570 h->heartbeat_sample_interval);
5571 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005572}
5573
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005574static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005575{
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005576 int dac, rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005577 struct ctlr_info *h;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005578 int try_soft_reset = 0;
5579 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005580
5581 if (number_of_controllers == 0)
5582 printk(KERN_INFO DRIVER_NAME "\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005583
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005584 rc = hpsa_init_reset_devices(pdev);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005585 if (rc) {
5586 if (rc != -ENOTSUPP)
5587 return rc;
5588 /* If the reset fails in a particular way (it has no way to do
5589 * a proper hard reset, so returns -ENOTSUPP) we can try to do
5590 * a soft reset once we get the controller configured up to the
5591 * point that it can accept a command.
5592 */
5593 try_soft_reset = 1;
5594 rc = 0;
5595 }
5596
5597reinit_after_soft_reset:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005598
Don Brace303932f2010-02-04 08:42:40 -06005599 /* Command structures must be aligned on a 32-byte boundary because
5600 * the 5 lower bits of the address are used by the hardware. and by
5601 * the driver. See comments in hpsa.h for more info.
5602 */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005603#define COMMANDLIST_ALIGNMENT 128
Don Brace303932f2010-02-04 08:42:40 -06005604 BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005605 h = kzalloc(sizeof(*h), GFP_KERNEL);
5606 if (!h)
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06005607 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005608
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005609 h->pdev = pdev;
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005610 h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06005611 INIT_LIST_HEAD(&h->cmpQ);
5612 INIT_LIST_HEAD(&h->reqQ);
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06005613 spin_lock_init(&h->lock);
5614 spin_lock_init(&h->scan_lock);
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05005615 spin_lock_init(&h->passthru_count_lock);
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005616 rc = hpsa_pci_init(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06005617 if (rc != 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005618 goto clean1;
5619
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06005620 sprintf(h->devname, HPSA "%d", number_of_controllers);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005621 h->ctlr = number_of_controllers;
5622 number_of_controllers++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005623
5624 /* configure PCI DMA stuff */
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06005625 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
5626 if (rc == 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005627 dac = 1;
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06005628 } else {
5629 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
5630 if (rc == 0) {
5631 dac = 0;
5632 } else {
5633 dev_err(&pdev->dev, "no suitable DMA available\n");
5634 goto clean1;
5635 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005636 }
5637
5638 /* make sure the board interrupts are off */
5639 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005640
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05005641 if (hpsa_request_irq(h, do_hpsa_intr_msi, do_hpsa_intr_intx))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005642 goto clean2;
Don Brace303932f2010-02-04 08:42:40 -06005643 dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
5644 h->devname, pdev->device,
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005645 h->intr[h->intr_mode], dac ? "" : " not");
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05005646 if (hpsa_allocate_cmd_pool(h))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005647 goto clean4;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06005648 if (hpsa_allocate_sg_chain_blocks(h))
5649 goto clean4;
Stephen M. Camerona08a84712010-02-04 08:43:16 -06005650 init_waitqueue_head(&h->scan_wait_queue);
5651 h->scan_finished = 1; /* no scan currently in progress */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005652
5653 pci_set_drvdata(pdev, h);
Stephen M. Cameron9a413382011-05-03 14:59:41 -05005654 h->ndevices = 0;
5655 h->scsi_host = NULL;
5656 spin_lock_init(&h->devlock);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005657 hpsa_put_ctlr_into_performant_mode(h);
5658
5659 /* At this point, the controller is ready to take commands.
5660 * Now, if reset_devices and the hard reset didn't work, try
5661 * the soft reset and see if that works.
5662 */
5663 if (try_soft_reset) {
5664
5665 /* This is kind of gross. We may or may not get a completion
5666 * from the soft reset command, and if we do, then the value
5667 * from the fifo may or may not be valid. So, we wait 10 secs
5668 * after the reset throwing away any completions we get during
5669 * that time. Unregister the interrupt handler and register
5670 * fake ones to scoop up any residual completions.
5671 */
5672 spin_lock_irqsave(&h->lock, flags);
5673 h->access.set_intr_mask(h, HPSA_INTR_OFF);
5674 spin_unlock_irqrestore(&h->lock, flags);
Matt Gates254f7962012-05-01 11:43:06 -05005675 free_irqs(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005676 rc = hpsa_request_irq(h, hpsa_msix_discard_completions,
5677 hpsa_intx_discard_completions);
5678 if (rc) {
5679 dev_warn(&h->pdev->dev, "Failed to request_irq after "
5680 "soft reset.\n");
5681 goto clean4;
5682 }
5683
5684 rc = hpsa_kdump_soft_reset(h);
5685 if (rc)
5686 /* Neither hard nor soft reset worked, we're hosed. */
5687 goto clean4;
5688
5689 dev_info(&h->pdev->dev, "Board READY.\n");
5690 dev_info(&h->pdev->dev,
5691 "Waiting for stale completions to drain.\n");
5692 h->access.set_intr_mask(h, HPSA_INTR_ON);
5693 msleep(10000);
5694 h->access.set_intr_mask(h, HPSA_INTR_OFF);
5695
5696 rc = controller_reset_failed(h->cfgtable);
5697 if (rc)
5698 dev_info(&h->pdev->dev,
5699 "Soft reset appears to have failed.\n");
5700
5701 /* since the controller's reset, we have to go back and re-init
5702 * everything. Easiest to just forget what we've done and do it
5703 * all over again.
5704 */
5705 hpsa_undo_allocations_after_kdump_soft_reset(h);
5706 try_soft_reset = 0;
5707 if (rc)
5708 /* don't go to clean4, we already unallocated */
5709 return -ENODEV;
5710
5711 goto reinit_after_soft_reset;
5712 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005713
5714 /* Turn the interrupts on so we can service requests */
5715 h->access.set_intr_mask(h, HPSA_INTR_ON);
5716
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06005717 hpsa_hba_inquiry(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005718 hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005719
5720 /* Monitor the controller for firmware lockups */
5721 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
5722 INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
5723 schedule_delayed_work(&h->monitor_ctlr_work,
5724 h->heartbeat_sample_interval);
Stephen M. Cameron88bf6d62013-11-01 11:02:25 -05005725 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005726
5727clean4:
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06005728 hpsa_free_sg_chain_blocks(h);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05005729 hpsa_free_cmd_pool(h);
Matt Gates254f7962012-05-01 11:43:06 -05005730 free_irqs(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005731clean2:
5732clean1:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005733 kfree(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06005734 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005735}
5736
5737static void hpsa_flush_cache(struct ctlr_info *h)
5738{
5739 char *flush_buf;
5740 struct CommandList *c;
Stephen M. Cameron702890e2013-09-23 13:33:30 -05005741 unsigned long flags;
5742
5743 /* Don't bother trying to flush the cache if locked up */
5744 spin_lock_irqsave(&h->lock, flags);
5745 if (unlikely(h->lockup_detected)) {
5746 spin_unlock_irqrestore(&h->lock, flags);
5747 return;
5748 }
5749 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005750
5751 flush_buf = kzalloc(4, GFP_KERNEL);
5752 if (!flush_buf)
5753 return;
5754
5755 c = cmd_special_alloc(h);
5756 if (!c) {
5757 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
5758 goto out_of_memory;
5759 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005760 if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
5761 RAID_CTLR_LUNID, TYPE_CMD)) {
5762 goto out;
5763 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005764 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
5765 if (c->err_info->CommandStatus != 0)
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005766out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005767 dev_warn(&h->pdev->dev,
5768 "error flushing cache on controller\n");
5769 cmd_special_free(h, c);
5770out_of_memory:
5771 kfree(flush_buf);
5772}
5773
5774static void hpsa_shutdown(struct pci_dev *pdev)
5775{
5776 struct ctlr_info *h;
5777
5778 h = pci_get_drvdata(pdev);
5779 /* Turn board interrupts off and send the flush cache command
5780 * sendcmd will turn off interrupt, and send the flush...
5781 * To write all data in the battery backed cache to disks
5782 */
5783 hpsa_flush_cache(h);
5784 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05005785 hpsa_free_irqs_and_disable_msix(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005786}
5787
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005788static void hpsa_free_device_info(struct ctlr_info *h)
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06005789{
5790 int i;
5791
5792 for (i = 0; i < h->ndevices; i++)
5793 kfree(h->dev[i]);
5794}
5795
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005796static void hpsa_remove_one(struct pci_dev *pdev)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005797{
5798 struct ctlr_info *h;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005799 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005800
5801 if (pci_get_drvdata(pdev) == NULL) {
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005802 dev_err(&pdev->dev, "unable to remove device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005803 return;
5804 }
5805 h = pci_get_drvdata(pdev);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005806
5807 /* Get rid of any controller monitoring work items */
5808 spin_lock_irqsave(&h->lock, flags);
5809 h->remove_in_progress = 1;
5810 cancel_delayed_work(&h->monitor_ctlr_work);
5811 spin_unlock_irqrestore(&h->lock, flags);
5812
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005813 hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */
5814 hpsa_shutdown(pdev);
5815 iounmap(h->vaddr);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05005816 iounmap(h->transtable);
5817 iounmap(h->cfgtable);
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06005818 hpsa_free_device_info(h);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06005819 hpsa_free_sg_chain_blocks(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005820 pci_free_consistent(h->pdev,
5821 h->nr_cmds * sizeof(struct CommandList),
5822 h->cmd_pool, h->cmd_pool_dhandle);
5823 pci_free_consistent(h->pdev,
5824 h->nr_cmds * sizeof(struct ErrorInfo),
5825 h->errinfo_pool, h->errinfo_pool_dhandle);
Don Brace303932f2010-02-04 08:42:40 -06005826 pci_free_consistent(h->pdev, h->reply_pool_size,
5827 h->reply_pool, h->reply_pool_dhandle);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005828 kfree(h->cmd_pool_bits);
Don Brace303932f2010-02-04 08:42:40 -06005829 kfree(h->blockFetchTable);
Matt Gatese1f7de02014-02-18 13:55:17 -06005830 kfree(h->ioaccel1_blockFetchTable);
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06005831 kfree(h->hba_inquiry_data);
Stephen M. Cameronf0bd0b682012-05-01 11:42:09 -05005832 pci_disable_device(pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005833 pci_release_regions(pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005834 kfree(h);
5835}
5836
5837static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
5838 __attribute__((unused)) pm_message_t state)
5839{
5840 return -ENOSYS;
5841}
5842
5843static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
5844{
5845 return -ENOSYS;
5846}
5847
5848static struct pci_driver hpsa_pci_driver = {
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06005849 .name = HPSA,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005850 .probe = hpsa_init_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005851 .remove = hpsa_remove_one,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005852 .id_table = hpsa_pci_device_id, /* id_table */
5853 .shutdown = hpsa_shutdown,
5854 .suspend = hpsa_suspend,
5855 .resume = hpsa_resume,
5856};
5857
Don Brace303932f2010-02-04 08:42:40 -06005858/* Fill in bucket_map[], given nsgs (the max number of
5859 * scatter gather elements supported) and bucket[],
5860 * which is an array of 8 integers. The bucket[] array
5861 * contains 8 different DMA transfer sizes (in 16
5862 * byte increments) which the controller uses to fetch
5863 * commands. This function fills in bucket_map[], which
5864 * maps a given number of scatter gather elements to one of
5865 * the 8 DMA transfer sizes. The point of it is to allow the
5866 * controller to only do as much DMA as needed to fetch the
5867 * command, with the DMA transfer size encoded in the lower
5868 * bits of the command address.
5869 */
5870static void calc_bucket_map(int bucket[], int num_buckets,
Matt Gatese1f7de02014-02-18 13:55:17 -06005871 int nsgs, int min_blocks, int *bucket_map)
Don Brace303932f2010-02-04 08:42:40 -06005872{
5873 int i, j, b, size;
5874
Don Brace303932f2010-02-04 08:42:40 -06005875 /* Note, bucket_map must have nsgs+1 entries. */
5876 for (i = 0; i <= nsgs; i++) {
5877 /* Compute size of a command with i SG entries */
Matt Gatese1f7de02014-02-18 13:55:17 -06005878 size = i + min_blocks;
Don Brace303932f2010-02-04 08:42:40 -06005879 b = num_buckets; /* Assume the biggest bucket */
5880 /* Find the bucket that is just big enough */
Matt Gatese1f7de02014-02-18 13:55:17 -06005881 for (j = 0; j < num_buckets; j++) {
Don Brace303932f2010-02-04 08:42:40 -06005882 if (bucket[j] >= size) {
5883 b = j;
5884 break;
5885 }
5886 }
5887 /* for a command with i SG entries, use bucket b. */
5888 bucket_map[i] = b;
5889 }
5890}
5891
Matt Gatese1f7de02014-02-18 13:55:17 -06005892static void hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
Don Brace303932f2010-02-04 08:42:40 -06005893{
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05005894 int i;
5895 unsigned long register_value;
Matt Gatese1f7de02014-02-18 13:55:17 -06005896 unsigned long transMethod = CFGTBL_Trans_Performant |
5897 (trans_support & CFGTBL_Trans_use_short_tags) |
5898 CFGTBL_Trans_enable_directed_msix |
5899 (trans_support & CFGTBL_Trans_io_accel1);
5900
5901 struct access_method access = SA5_performant_access;
Stephen M. Camerondef342b2010-05-27 15:14:39 -05005902
5903 /* This is a bit complicated. There are 8 registers on
5904 * the controller which we write to to tell it 8 different
5905 * sizes of commands which there may be. It's a way of
5906 * reducing the DMA done to fetch each command. Encoded into
5907 * each command's tag are 3 bits which communicate to the controller
5908 * which of the eight sizes that command fits within. The size of
5909 * each command depends on how many scatter gather entries there are.
5910 * Each SG entry requires 16 bytes. The eight registers are programmed
5911 * with the number of 16-byte blocks a command of that size requires.
5912 * The smallest command possible requires 5 such 16 byte blocks.
Stephen M. Camerond66ae082012-01-19 14:00:48 -06005913 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
Stephen M. Camerondef342b2010-05-27 15:14:39 -05005914 * blocks. Note, this only extends to the SG entries contained
5915 * within the command block, and does not extend to chained blocks
5916 * of SG elements. bft[] contains the eight values we write to
5917 * the registers. They are not evenly distributed, but have more
5918 * sizes for small commands, and fewer sizes for larger commands.
5919 */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06005920 int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
5921 BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
Don Brace303932f2010-02-04 08:42:40 -06005922 /* 5 = 1 s/g entry or 4k
5923 * 6 = 2 s/g entry or 8k
5924 * 8 = 4 s/g entry or 16k
5925 * 10 = 6 s/g entry or 24k
5926 */
Don Brace303932f2010-02-04 08:42:40 -06005927
Don Brace303932f2010-02-04 08:42:40 -06005928 /* Controller spec: zero out this buffer. */
5929 memset(h->reply_pool, 0, h->reply_pool_size);
Don Brace303932f2010-02-04 08:42:40 -06005930
Stephen M. Camerond66ae082012-01-19 14:00:48 -06005931 bft[7] = SG_ENTRIES_IN_CMD + 4;
5932 calc_bucket_map(bft, ARRAY_SIZE(bft),
Matt Gatese1f7de02014-02-18 13:55:17 -06005933 SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
Don Brace303932f2010-02-04 08:42:40 -06005934 for (i = 0; i < 8; i++)
5935 writel(bft[i], &h->transtable->BlockFetch[i]);
5936
5937 /* size of controller ring buffer */
5938 writel(h->max_commands, &h->transtable->RepQSize);
Matt Gates254f7962012-05-01 11:43:06 -05005939 writel(h->nreply_queues, &h->transtable->RepQCount);
Don Brace303932f2010-02-04 08:42:40 -06005940 writel(0, &h->transtable->RepQCtrAddrLow32);
5941 writel(0, &h->transtable->RepQCtrAddrHigh32);
Matt Gates254f7962012-05-01 11:43:06 -05005942
5943 for (i = 0; i < h->nreply_queues; i++) {
5944 writel(0, &h->transtable->RepQAddr[i].upper);
5945 writel(h->reply_pool_dhandle +
5946 (h->max_commands * sizeof(u64) * i),
5947 &h->transtable->RepQAddr[i].lower);
5948 }
5949
Matt Gatese1f7de02014-02-18 13:55:17 -06005950 writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
5951 /*
5952 * enable outbound interrupt coalescing in accelerator mode;
5953 */
5954 if (trans_support & CFGTBL_Trans_io_accel1) {
5955 access = SA5_ioaccel_mode1_access;
5956 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
5957 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
5958 }
Don Brace303932f2010-02-04 08:42:40 -06005959 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005960 hpsa_wait_for_mode_change_ack(h);
Don Brace303932f2010-02-04 08:42:40 -06005961 register_value = readl(&(h->cfgtable->TransportActive));
5962 if (!(register_value & CFGTBL_Trans_Performant)) {
5963 dev_warn(&h->pdev->dev, "unable to get board into"
5964 " performant mode\n");
5965 return;
5966 }
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06005967 /* Change the access methods to the performant access methods */
Matt Gatese1f7de02014-02-18 13:55:17 -06005968 h->access = access;
5969 h->transMethod = transMethod;
5970
5971 if (!(trans_support & CFGTBL_Trans_io_accel1))
5972 return;
5973
5974 /* Set up I/O accelerator mode */
5975 for (i = 0; i < h->nreply_queues; i++) {
5976 writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
5977 h->reply_queue[i].current_entry =
5978 readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
5979 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005980 bft[7] = h->ioaccel_maxsg + 8;
5981 calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
Matt Gatese1f7de02014-02-18 13:55:17 -06005982 h->ioaccel1_blockFetchTable);
5983
5984 /* initialize all reply queue entries to unused */
5985 memset(h->reply_pool, (u8) IOACCEL_MODE1_REPLY_UNUSED,
5986 h->reply_pool_size);
5987
5988 /* set all the constant fields in the accelerator command
5989 * frames once at init time to save CPU cycles later.
5990 */
5991 for (i = 0; i < h->nr_cmds; i++) {
5992 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
5993
5994 cp->function = IOACCEL1_FUNCTION_SCSIIO;
5995 cp->err_info = (u32) (h->errinfo_pool_dhandle +
5996 (i * sizeof(struct ErrorInfo)));
5997 cp->err_info_len = sizeof(struct ErrorInfo);
5998 cp->sgl_offset = IOACCEL1_SGLOFFSET;
5999 cp->host_context_flags = IOACCEL1_HCFLAGS_CISS_FORMAT;
6000 cp->timeout_sec = 0;
6001 cp->ReplyQueue = 0;
6002 cp->Tag.lower = (i << DIRECT_LOOKUP_SHIFT) | DIRECT_LOOKUP_BIT;
6003 cp->Tag.upper = 0;
6004 cp->host_addr.lower = (u32) (h->ioaccel_cmd_pool_dhandle +
6005 (i * sizeof(struct io_accel1_cmd)));
6006 cp->host_addr.upper = 0;
6007 }
6008}
6009
6010static int hpsa_alloc_ioaccel_cmd_and_bft(struct ctlr_info *h)
6011{
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006012 h->ioaccel_maxsg =
6013 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
6014 if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
6015 h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
6016
Matt Gatese1f7de02014-02-18 13:55:17 -06006017 /* Command structures must be aligned on a 128-byte boundary
6018 * because the 7 lower bits of the address are used by the
6019 * hardware.
6020 */
6021#define IOACCEL1_COMMANDLIST_ALIGNMENT 128
6022 BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
6023 IOACCEL1_COMMANDLIST_ALIGNMENT);
6024 h->ioaccel_cmd_pool =
6025 pci_alloc_consistent(h->pdev,
6026 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
6027 &(h->ioaccel_cmd_pool_dhandle));
6028
6029 h->ioaccel1_blockFetchTable =
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006030 kmalloc(((h->ioaccel_maxsg + 1) *
Matt Gatese1f7de02014-02-18 13:55:17 -06006031 sizeof(u32)), GFP_KERNEL);
6032
6033 if ((h->ioaccel_cmd_pool == NULL) ||
6034 (h->ioaccel1_blockFetchTable == NULL))
6035 goto clean_up;
6036
6037 memset(h->ioaccel_cmd_pool, 0,
6038 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
6039 return 0;
6040
6041clean_up:
6042 if (h->ioaccel_cmd_pool)
6043 pci_free_consistent(h->pdev,
6044 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
6045 h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
6046 kfree(h->ioaccel1_blockFetchTable);
6047 return 1;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006048}
6049
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006050static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006051{
6052 u32 trans_support;
Matt Gatese1f7de02014-02-18 13:55:17 -06006053 unsigned long transMethod = CFGTBL_Trans_Performant |
6054 CFGTBL_Trans_use_short_tags;
Matt Gates254f7962012-05-01 11:43:06 -05006055 int i;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006056
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -06006057 if (hpsa_simple_mode)
6058 return;
6059
Matt Gatese1f7de02014-02-18 13:55:17 -06006060 /* Check for I/O accelerator mode support */
6061 if (trans_support & CFGTBL_Trans_io_accel1) {
6062 transMethod |= CFGTBL_Trans_io_accel1 |
6063 CFGTBL_Trans_enable_directed_msix;
6064 if (hpsa_alloc_ioaccel_cmd_and_bft(h))
6065 goto clean_up;
6066 }
6067
6068 /* TODO, check that this next line h->nreply_queues is correct */
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006069 trans_support = readl(&(h->cfgtable->TransportSupport));
6070 if (!(trans_support & PERFORMANT_MODE))
6071 return;
6072
Hannes Reineckeeee0f032014-01-15 13:30:53 +01006073 h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05006074 hpsa_get_max_perf_mode_cmds(h);
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006075 /* Performant mode ring buffer and supporting data structures */
Matt Gates254f7962012-05-01 11:43:06 -05006076 h->reply_pool_size = h->max_commands * sizeof(u64) * h->nreply_queues;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006077 h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size,
6078 &(h->reply_pool_dhandle));
6079
Matt Gates254f7962012-05-01 11:43:06 -05006080 for (i = 0; i < h->nreply_queues; i++) {
6081 h->reply_queue[i].head = &h->reply_pool[h->max_commands * i];
6082 h->reply_queue[i].size = h->max_commands;
6083 h->reply_queue[i].wraparound = 1; /* spec: init to 1 */
6084 h->reply_queue[i].current_entry = 0;
6085 }
6086
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006087 /* Need a block fetch table for performant mode */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006088 h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006089 sizeof(u32)), GFP_KERNEL);
6090
6091 if ((h->reply_pool == NULL)
6092 || (h->blockFetchTable == NULL))
6093 goto clean_up;
6094
Matt Gatese1f7de02014-02-18 13:55:17 -06006095 hpsa_enter_performant_mode(h, trans_support);
Don Brace303932f2010-02-04 08:42:40 -06006096 return;
6097
6098clean_up:
6099 if (h->reply_pool)
6100 pci_free_consistent(h->pdev, h->reply_pool_size,
6101 h->reply_pool, h->reply_pool_dhandle);
6102 kfree(h->blockFetchTable);
6103}
6104
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006105static void hpsa_drain_commands(struct ctlr_info *h)
6106{
6107 int cmds_out;
6108 unsigned long flags;
6109
6110 do { /* wait for all outstanding commands to drain out */
6111 spin_lock_irqsave(&h->lock, flags);
6112 cmds_out = h->commands_outstanding;
6113 spin_unlock_irqrestore(&h->lock, flags);
6114 if (cmds_out <= 0)
6115 break;
6116 msleep(100);
6117 } while (1);
6118}
6119
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006120/*
6121 * This is it. Register the PCI driver information for the cards we control
6122 * the OS will call our registered routines when it finds one of our cards.
6123 */
6124static int __init hpsa_init(void)
6125{
Mike Miller31468402010-02-25 14:03:12 -06006126 return pci_register_driver(&hpsa_pci_driver);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006127}
6128
6129static void __exit hpsa_cleanup(void)
6130{
6131 pci_unregister_driver(&hpsa_pci_driver);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006132}
6133
Matt Gatese1f7de02014-02-18 13:55:17 -06006134static void __attribute__((unused)) verify_offsets(void)
6135{
6136#define VERIFY_OFFSET(member, offset) \
Mike Millerb66cc252014-02-18 13:56:04 -06006137 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
6138
6139 VERIFY_OFFSET(IU_type, 0);
6140 VERIFY_OFFSET(direction, 1);
6141 VERIFY_OFFSET(reply_queue, 2);
6142 /* VERIFY_OFFSET(reserved1, 3); */
6143 VERIFY_OFFSET(scsi_nexus, 4);
6144 VERIFY_OFFSET(Tag, 8);
6145 VERIFY_OFFSET(cdb, 16);
6146 VERIFY_OFFSET(cciss_lun, 32);
6147 VERIFY_OFFSET(data_len, 40);
6148 VERIFY_OFFSET(cmd_priority_task_attr, 44);
6149 VERIFY_OFFSET(sg_count, 45);
6150 /* VERIFY_OFFSET(reserved3 */
6151 VERIFY_OFFSET(err_ptr, 48);
6152 VERIFY_OFFSET(err_len, 56);
6153 /* VERIFY_OFFSET(reserved4 */
6154 VERIFY_OFFSET(sg, 64);
6155
6156#undef VERIFY_OFFSET
6157
6158#define VERIFY_OFFSET(member, offset) \
Matt Gatese1f7de02014-02-18 13:55:17 -06006159 BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
6160
6161 VERIFY_OFFSET(dev_handle, 0x00);
6162 VERIFY_OFFSET(reserved1, 0x02);
6163 VERIFY_OFFSET(function, 0x03);
6164 VERIFY_OFFSET(reserved2, 0x04);
6165 VERIFY_OFFSET(err_info, 0x0C);
6166 VERIFY_OFFSET(reserved3, 0x10);
6167 VERIFY_OFFSET(err_info_len, 0x12);
6168 VERIFY_OFFSET(reserved4, 0x13);
6169 VERIFY_OFFSET(sgl_offset, 0x14);
6170 VERIFY_OFFSET(reserved5, 0x15);
6171 VERIFY_OFFSET(transfer_len, 0x1C);
6172 VERIFY_OFFSET(reserved6, 0x20);
6173 VERIFY_OFFSET(io_flags, 0x24);
6174 VERIFY_OFFSET(reserved7, 0x26);
6175 VERIFY_OFFSET(LUN, 0x34);
6176 VERIFY_OFFSET(control, 0x3C);
6177 VERIFY_OFFSET(CDB, 0x40);
6178 VERIFY_OFFSET(reserved8, 0x50);
6179 VERIFY_OFFSET(host_context_flags, 0x60);
6180 VERIFY_OFFSET(timeout_sec, 0x62);
6181 VERIFY_OFFSET(ReplyQueue, 0x64);
6182 VERIFY_OFFSET(reserved9, 0x65);
6183 VERIFY_OFFSET(Tag, 0x68);
6184 VERIFY_OFFSET(host_addr, 0x70);
6185 VERIFY_OFFSET(CISS_LUN, 0x78);
6186 VERIFY_OFFSET(SG, 0x78 + 8);
6187#undef VERIFY_OFFSET
6188}
6189
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006190module_init(hpsa_init);
6191module_exit(hpsa_cleanup);