blob: 67e059cd9e57ca9a5b3ee49ff1ec751b5f090592 [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. Camerona08a8472010-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
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600511static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
512static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
513static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
514static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
515static DEVICE_ATTR(firmware_revision, S_IRUGO,
516 host_show_firmware_revision, NULL);
517static DEVICE_ATTR(commands_outstanding, S_IRUGO,
518 host_show_commands_outstanding, NULL);
519static DEVICE_ATTR(transport_mode, S_IRUGO,
520 host_show_transport_mode, NULL);
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600521static DEVICE_ATTR(resettable, S_IRUGO,
522 host_show_resettable, NULL);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600523
524static struct device_attribute *hpsa_sdev_attrs[] = {
525 &dev_attr_raid_level,
526 &dev_attr_lunid,
527 &dev_attr_unique_id,
528 NULL,
529};
530
531static struct device_attribute *hpsa_shost_attrs[] = {
532 &dev_attr_rescan,
533 &dev_attr_firmware_revision,
534 &dev_attr_commands_outstanding,
535 &dev_attr_transport_mode,
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600536 &dev_attr_resettable,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600537 NULL,
538};
539
540static struct scsi_host_template hpsa_driver_template = {
541 .module = THIS_MODULE,
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600542 .name = HPSA,
543 .proc_name = HPSA,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600544 .queuecommand = hpsa_scsi_queue_command,
545 .scan_start = hpsa_scan_start,
546 .scan_finished = hpsa_scan_finished,
547 .change_queue_depth = hpsa_change_queue_depth,
548 .this_id = -1,
549 .use_clustering = ENABLE_CLUSTERING,
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500550 .eh_abort_handler = hpsa_eh_abort_handler,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600551 .eh_device_reset_handler = hpsa_eh_device_reset_handler,
552 .ioctl = hpsa_ioctl,
553 .slave_alloc = hpsa_slave_alloc,
554 .slave_destroy = hpsa_slave_destroy,
555#ifdef CONFIG_COMPAT
556 .compat_ioctl = hpsa_compat_ioctl,
557#endif
558 .sdev_attrs = hpsa_sdev_attrs,
559 .shost_attrs = hpsa_shost_attrs,
Stephen M. Cameronc0d6a4d2011-10-26 16:20:53 -0500560 .max_sectors = 8192,
Martin K. Petersen54b2b502013-10-23 06:25:40 -0400561 .no_write_same = 1,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600562};
563
564
565/* Enqueuing and dequeuing functions for cmdlists. */
566static inline void addQ(struct list_head *list, struct CommandList *c)
567{
568 list_add_tail(&c->list, list);
569}
570
Matt Gates254f7962012-05-01 11:43:06 -0500571static inline u32 next_command(struct ctlr_info *h, u8 q)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600572{
573 u32 a;
Matt Gates254f7962012-05-01 11:43:06 -0500574 struct reply_pool *rq = &h->reply_queue[q];
Matt Gatese16a33a2012-05-01 11:43:11 -0500575 unsigned long flags;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600576
Matt Gatese1f7de02014-02-18 13:55:17 -0600577 if (h->transMethod & CFGTBL_Trans_io_accel1)
578 return h->access.command_completed(h, q);
579
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600580 if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
Matt Gates254f7962012-05-01 11:43:06 -0500581 return h->access.command_completed(h, q);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600582
Matt Gates254f7962012-05-01 11:43:06 -0500583 if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
584 a = rq->head[rq->current_entry];
585 rq->current_entry++;
Matt Gatese16a33a2012-05-01 11:43:11 -0500586 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600587 h->commands_outstanding--;
Matt Gatese16a33a2012-05-01 11:43:11 -0500588 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600589 } else {
590 a = FIFO_EMPTY;
591 }
592 /* Check for wraparound */
Matt Gates254f7962012-05-01 11:43:06 -0500593 if (rq->current_entry == h->max_commands) {
594 rq->current_entry = 0;
595 rq->wraparound ^= 1;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600596 }
597 return a;
598}
599
600/* set_performant_mode: Modify the tag for cciss performant
601 * set bit 0 for pull model, bits 3-1 for block fetch
602 * register number
603 */
604static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
605{
Matt Gates254f7962012-05-01 11:43:06 -0500606 if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600607 c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
Hannes Reineckeeee0f032014-01-15 13:30:53 +0100608 if (likely(h->msix_vector > 0))
Matt Gates254f7962012-05-01 11:43:06 -0500609 c->Header.ReplyQueue =
John Kacur804a5cb2013-07-26 16:06:18 +0200610 raw_smp_processor_id() % h->nreply_queues;
Matt Gates254f7962012-05-01 11:43:06 -0500611 }
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600612}
613
Stephen M. Camerone85c5972012-05-01 11:43:42 -0500614static int is_firmware_flash_cmd(u8 *cdb)
615{
616 return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
617}
618
619/*
620 * During firmware flash, the heartbeat register may not update as frequently
621 * as it should. So we dial down lockup detection during firmware flash. and
622 * dial it back up when firmware flash completes.
623 */
624#define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
625#define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
626static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
627 struct CommandList *c)
628{
629 if (!is_firmware_flash_cmd(c->Request.CDB))
630 return;
631 atomic_inc(&h->firmware_flash_in_progress);
632 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
633}
634
635static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
636 struct CommandList *c)
637{
638 if (is_firmware_flash_cmd(c->Request.CDB) &&
639 atomic_dec_and_test(&h->firmware_flash_in_progress))
640 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
641}
642
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600643static void enqueue_cmd_and_start_io(struct ctlr_info *h,
644 struct CommandList *c)
645{
646 unsigned long flags;
647
648 set_performant_mode(h, c);
Stephen M. Camerone85c5972012-05-01 11:43:42 -0500649 dial_down_lockup_detection_during_fw_flash(h, c);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600650 spin_lock_irqsave(&h->lock, flags);
651 addQ(&h->reqQ, c);
652 h->Qdepth++;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600653 spin_unlock_irqrestore(&h->lock, flags);
Matt Gatese16a33a2012-05-01 11:43:11 -0500654 start_io(h);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600655}
656
657static inline void removeQ(struct CommandList *c)
658{
659 if (WARN_ON(list_empty(&c->list)))
660 return;
661 list_del_init(&c->list);
662}
663
664static inline int is_hba_lunid(unsigned char scsi3addr[])
665{
666 return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
667}
668
669static inline int is_scsi_rev_5(struct ctlr_info *h)
670{
671 if (!h->hba_inquiry_data)
672 return 0;
673 if ((h->hba_inquiry_data[2] & 0x07) == 5)
674 return 1;
675 return 0;
676}
677
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800678static int hpsa_find_target_lun(struct ctlr_info *h,
679 unsigned char scsi3addr[], int bus, int *target, int *lun)
680{
681 /* finds an unused bus, target, lun for a new physical device
682 * assumes h->devlock is held
683 */
684 int i, found = 0;
Scott Teelcfe5bad2011-10-26 16:21:07 -0500685 DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800686
Akinobu Mita263d9402012-01-21 00:15:27 +0900687 bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800688
689 for (i = 0; i < h->ndevices; i++) {
690 if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
Akinobu Mita263d9402012-01-21 00:15:27 +0900691 __set_bit(h->dev[i]->target, lun_taken);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800692 }
693
Akinobu Mita263d9402012-01-21 00:15:27 +0900694 i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
695 if (i < HPSA_MAX_DEVICES) {
696 /* *bus = 1; */
697 *target = i;
698 *lun = 0;
699 found = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800700 }
701 return !found;
702}
703
704/* Add an entry into h->dev[] array. */
705static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
706 struct hpsa_scsi_dev_t *device,
707 struct hpsa_scsi_dev_t *added[], int *nadded)
708{
709 /* assumes h->devlock is held */
710 int n = h->ndevices;
711 int i;
712 unsigned char addr1[8], addr2[8];
713 struct hpsa_scsi_dev_t *sd;
714
Scott Teelcfe5bad2011-10-26 16:21:07 -0500715 if (n >= HPSA_MAX_DEVICES) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800716 dev_err(&h->pdev->dev, "too many devices, some will be "
717 "inaccessible.\n");
718 return -1;
719 }
720
721 /* physical devices do not have lun or target assigned until now. */
722 if (device->lun != -1)
723 /* Logical device, lun is already assigned. */
724 goto lun_assigned;
725
726 /* If this device a non-zero lun of a multi-lun device
727 * byte 4 of the 8-byte LUN addr will contain the logical
728 * unit no, zero otherise.
729 */
730 if (device->scsi3addr[4] == 0) {
731 /* This is not a non-zero lun of a multi-lun device */
732 if (hpsa_find_target_lun(h, device->scsi3addr,
733 device->bus, &device->target, &device->lun) != 0)
734 return -1;
735 goto lun_assigned;
736 }
737
738 /* This is a non-zero lun of a multi-lun device.
739 * Search through our list and find the device which
740 * has the same 8 byte LUN address, excepting byte 4.
741 * Assign the same bus and target for this new LUN.
742 * Use the logical unit number from the firmware.
743 */
744 memcpy(addr1, device->scsi3addr, 8);
745 addr1[4] = 0;
746 for (i = 0; i < n; i++) {
747 sd = h->dev[i];
748 memcpy(addr2, sd->scsi3addr, 8);
749 addr2[4] = 0;
750 /* differ only in byte 4? */
751 if (memcmp(addr1, addr2, 8) == 0) {
752 device->bus = sd->bus;
753 device->target = sd->target;
754 device->lun = device->scsi3addr[4];
755 break;
756 }
757 }
758 if (device->lun == -1) {
759 dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
760 " suspect firmware bug or unsupported hardware "
761 "configuration.\n");
762 return -1;
763 }
764
765lun_assigned:
766
767 h->dev[n] = device;
768 h->ndevices++;
769 added[*nadded] = device;
770 (*nadded)++;
771
772 /* initially, (before registering with scsi layer) we don't
773 * know our hostno and we don't want to print anything first
774 * time anyway (the scsi layer's inquiries will show that info)
775 */
776 /* if (hostno != -1) */
777 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
778 scsi_device_type(device->devtype), hostno,
779 device->bus, device->target, device->lun);
780 return 0;
781}
782
Scott Teelbd9244f2012-01-19 14:01:30 -0600783/* Update an entry in h->dev[] array. */
784static void hpsa_scsi_update_entry(struct ctlr_info *h, int hostno,
785 int entry, struct hpsa_scsi_dev_t *new_entry)
786{
787 /* assumes h->devlock is held */
788 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
789
790 /* Raid level changed. */
791 h->dev[entry]->raid_level = new_entry->raid_level;
Stephen M. Cameron250fb122014-02-18 13:55:38 -0600792
793 /* Raid offload parameters changed. */
794 h->dev[entry]->offload_config = new_entry->offload_config;
795 h->dev[entry]->offload_enabled = new_entry->offload_enabled;
796
Scott Teelbd9244f2012-01-19 14:01:30 -0600797 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d updated.\n",
798 scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
799 new_entry->target, new_entry->lun);
800}
801
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -0600802/* Replace an entry from h->dev[] array. */
803static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
804 int entry, struct hpsa_scsi_dev_t *new_entry,
805 struct hpsa_scsi_dev_t *added[], int *nadded,
806 struct hpsa_scsi_dev_t *removed[], int *nremoved)
807{
808 /* assumes h->devlock is held */
Scott Teelcfe5bad2011-10-26 16:21:07 -0500809 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -0600810 removed[*nremoved] = h->dev[entry];
811 (*nremoved)++;
Stephen M. Cameron01350d02011-08-09 08:18:01 -0500812
813 /*
814 * New physical devices won't have target/lun assigned yet
815 * so we need to preserve the values in the slot we are replacing.
816 */
817 if (new_entry->target == -1) {
818 new_entry->target = h->dev[entry]->target;
819 new_entry->lun = h->dev[entry]->lun;
820 }
821
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -0600822 h->dev[entry] = new_entry;
823 added[*nadded] = new_entry;
824 (*nadded)++;
825 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n",
826 scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
827 new_entry->target, new_entry->lun);
828}
829
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800830/* Remove an entry from h->dev[] array. */
831static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
832 struct hpsa_scsi_dev_t *removed[], int *nremoved)
833{
834 /* assumes h->devlock is held */
835 int i;
836 struct hpsa_scsi_dev_t *sd;
837
Scott Teelcfe5bad2011-10-26 16:21:07 -0500838 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800839
840 sd = h->dev[entry];
841 removed[*nremoved] = h->dev[entry];
842 (*nremoved)++;
843
844 for (i = entry; i < h->ndevices-1; i++)
845 h->dev[i] = h->dev[i+1];
846 h->ndevices--;
847 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
848 scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
849 sd->lun);
850}
851
852#define SCSI3ADDR_EQ(a, b) ( \
853 (a)[7] == (b)[7] && \
854 (a)[6] == (b)[6] && \
855 (a)[5] == (b)[5] && \
856 (a)[4] == (b)[4] && \
857 (a)[3] == (b)[3] && \
858 (a)[2] == (b)[2] && \
859 (a)[1] == (b)[1] && \
860 (a)[0] == (b)[0])
861
862static void fixup_botched_add(struct ctlr_info *h,
863 struct hpsa_scsi_dev_t *added)
864{
865 /* called when scsi_add_device fails in order to re-adjust
866 * h->dev[] to match the mid layer's view.
867 */
868 unsigned long flags;
869 int i, j;
870
871 spin_lock_irqsave(&h->lock, flags);
872 for (i = 0; i < h->ndevices; i++) {
873 if (h->dev[i] == added) {
874 for (j = i; j < h->ndevices-1; j++)
875 h->dev[j] = h->dev[j+1];
876 h->ndevices--;
877 break;
878 }
879 }
880 spin_unlock_irqrestore(&h->lock, flags);
881 kfree(added);
882}
883
884static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
885 struct hpsa_scsi_dev_t *dev2)
886{
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800887 /* we compare everything except lun and target as these
888 * are not yet assigned. Compare parts likely
889 * to differ first
890 */
891 if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
892 sizeof(dev1->scsi3addr)) != 0)
893 return 0;
894 if (memcmp(dev1->device_id, dev2->device_id,
895 sizeof(dev1->device_id)) != 0)
896 return 0;
897 if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
898 return 0;
899 if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
900 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800901 if (dev1->devtype != dev2->devtype)
902 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800903 if (dev1->bus != dev2->bus)
904 return 0;
905 return 1;
906}
907
Scott Teelbd9244f2012-01-19 14:01:30 -0600908static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
909 struct hpsa_scsi_dev_t *dev2)
910{
911 /* Device attributes that can change, but don't mean
912 * that the device is a different device, nor that the OS
913 * needs to be told anything about the change.
914 */
915 if (dev1->raid_level != dev2->raid_level)
916 return 1;
Stephen M. Cameron250fb122014-02-18 13:55:38 -0600917 if (dev1->offload_config != dev2->offload_config)
918 return 1;
919 if (dev1->offload_enabled != dev2->offload_enabled)
920 return 1;
Scott Teelbd9244f2012-01-19 14:01:30 -0600921 return 0;
922}
923
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800924/* Find needle in haystack. If exact match found, return DEVICE_SAME,
925 * and return needle location in *index. If scsi3addr matches, but not
926 * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
Scott Teelbd9244f2012-01-19 14:01:30 -0600927 * location in *index.
928 * In the case of a minor device attribute change, such as RAID level, just
929 * return DEVICE_UPDATED, along with the updated device's location in index.
930 * If needle not found, return DEVICE_NOT_FOUND.
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800931 */
932static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
933 struct hpsa_scsi_dev_t *haystack[], int haystack_size,
934 int *index)
935{
936 int i;
937#define DEVICE_NOT_FOUND 0
938#define DEVICE_CHANGED 1
939#define DEVICE_SAME 2
Scott Teelbd9244f2012-01-19 14:01:30 -0600940#define DEVICE_UPDATED 3
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800941 for (i = 0; i < haystack_size; i++) {
Stephen M. Cameron23231042010-02-04 08:43:36 -0600942 if (haystack[i] == NULL) /* previously removed. */
943 continue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800944 if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
945 *index = i;
Scott Teelbd9244f2012-01-19 14:01:30 -0600946 if (device_is_the_same(needle, haystack[i])) {
947 if (device_updated(needle, haystack[i]))
948 return DEVICE_UPDATED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800949 return DEVICE_SAME;
Scott Teelbd9244f2012-01-19 14:01:30 -0600950 } else {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800951 return DEVICE_CHANGED;
Scott Teelbd9244f2012-01-19 14:01:30 -0600952 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800953 }
954 }
955 *index = -1;
956 return DEVICE_NOT_FOUND;
957}
958
Stephen M. Cameron4967bd32010-02-04 08:41:49 -0600959static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800960 struct hpsa_scsi_dev_t *sd[], int nsds)
961{
962 /* sd contains scsi3 addresses and devtypes, and inquiry
963 * data. This function takes what's in sd to be the current
964 * reality and updates h->dev[] to reflect that reality.
965 */
966 int i, entry, device_change, changes = 0;
967 struct hpsa_scsi_dev_t *csd;
968 unsigned long flags;
969 struct hpsa_scsi_dev_t **added, **removed;
970 int nadded, nremoved;
971 struct Scsi_Host *sh = NULL;
972
Scott Teelcfe5bad2011-10-26 16:21:07 -0500973 added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
974 removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800975
976 if (!added || !removed) {
977 dev_warn(&h->pdev->dev, "out of memory in "
978 "adjust_hpsa_scsi_table\n");
979 goto free_and_out;
980 }
981
982 spin_lock_irqsave(&h->devlock, flags);
983
984 /* find any devices in h->dev[] that are not in
985 * sd[] and remove them from h->dev[], and for any
986 * devices which have changed, remove the old device
987 * info and add the new device info.
Scott Teelbd9244f2012-01-19 14:01:30 -0600988 * If minor device attributes change, just update
989 * the existing device structure.
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800990 */
991 i = 0;
992 nremoved = 0;
993 nadded = 0;
994 while (i < h->ndevices) {
995 csd = h->dev[i];
996 device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
997 if (device_change == DEVICE_NOT_FOUND) {
998 changes++;
999 hpsa_scsi_remove_entry(h, hostno, i,
1000 removed, &nremoved);
1001 continue; /* remove ^^^, hence i not incremented */
1002 } else if (device_change == DEVICE_CHANGED) {
1003 changes++;
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001004 hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
1005 added, &nadded, removed, &nremoved);
Stephen M. Cameronc7f172d2010-02-04 08:43:31 -06001006 /* Set it to NULL to prevent it from being freed
1007 * at the bottom of hpsa_update_scsi_devices()
1008 */
1009 sd[entry] = NULL;
Scott Teelbd9244f2012-01-19 14:01:30 -06001010 } else if (device_change == DEVICE_UPDATED) {
1011 hpsa_scsi_update_entry(h, hostno, i, sd[entry]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001012 }
1013 i++;
1014 }
1015
1016 /* Now, make sure every device listed in sd[] is also
1017 * listed in h->dev[], adding them if they aren't found
1018 */
1019
1020 for (i = 0; i < nsds; i++) {
1021 if (!sd[i]) /* if already added above. */
1022 continue;
1023 device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1024 h->ndevices, &entry);
1025 if (device_change == DEVICE_NOT_FOUND) {
1026 changes++;
1027 if (hpsa_scsi_add_entry(h, hostno, sd[i],
1028 added, &nadded) != 0)
1029 break;
1030 sd[i] = NULL; /* prevent from being freed later. */
1031 } else if (device_change == DEVICE_CHANGED) {
1032 /* should never happen... */
1033 changes++;
1034 dev_warn(&h->pdev->dev,
1035 "device unexpectedly changed.\n");
1036 /* but if it does happen, we just ignore that device */
1037 }
1038 }
1039 spin_unlock_irqrestore(&h->devlock, flags);
1040
1041 /* Don't notify scsi mid layer of any changes the first time through
1042 * (or if there are no changes) scsi_scan_host will do it later the
1043 * first time through.
1044 */
1045 if (hostno == -1 || !changes)
1046 goto free_and_out;
1047
1048 sh = h->scsi_host;
1049 /* Notify scsi mid layer of any removed devices */
1050 for (i = 0; i < nremoved; i++) {
1051 struct scsi_device *sdev =
1052 scsi_device_lookup(sh, removed[i]->bus,
1053 removed[i]->target, removed[i]->lun);
1054 if (sdev != NULL) {
1055 scsi_remove_device(sdev);
1056 scsi_device_put(sdev);
1057 } else {
1058 /* We don't expect to get here.
1059 * future cmds to this device will get selection
1060 * timeout as if the device was gone.
1061 */
1062 dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
1063 " for removal.", hostno, removed[i]->bus,
1064 removed[i]->target, removed[i]->lun);
1065 }
1066 kfree(removed[i]);
1067 removed[i] = NULL;
1068 }
1069
1070 /* Notify scsi mid layer of any added devices */
1071 for (i = 0; i < nadded; i++) {
1072 if (scsi_add_device(sh, added[i]->bus,
1073 added[i]->target, added[i]->lun) == 0)
1074 continue;
1075 dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
1076 "device not added.\n", hostno, added[i]->bus,
1077 added[i]->target, added[i]->lun);
1078 /* now we have to remove it from h->dev,
1079 * since it didn't get added to scsi mid layer
1080 */
1081 fixup_botched_add(h, added[i]);
1082 }
1083
1084free_and_out:
1085 kfree(added);
1086 kfree(removed);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001087}
1088
1089/*
Joe Perches9e03aa22013-09-03 13:45:58 -07001090 * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001091 * Assume's h->devlock is held.
1092 */
1093static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1094 int bus, int target, int lun)
1095{
1096 int i;
1097 struct hpsa_scsi_dev_t *sd;
1098
1099 for (i = 0; i < h->ndevices; i++) {
1100 sd = h->dev[i];
1101 if (sd->bus == bus && sd->target == target && sd->lun == lun)
1102 return sd;
1103 }
1104 return NULL;
1105}
1106
1107/* link sdev->hostdata to our per-device structure. */
1108static int hpsa_slave_alloc(struct scsi_device *sdev)
1109{
1110 struct hpsa_scsi_dev_t *sd;
1111 unsigned long flags;
1112 struct ctlr_info *h;
1113
1114 h = sdev_to_hba(sdev);
1115 spin_lock_irqsave(&h->devlock, flags);
1116 sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1117 sdev_id(sdev), sdev->lun);
1118 if (sd != NULL)
1119 sdev->hostdata = sd;
1120 spin_unlock_irqrestore(&h->devlock, flags);
1121 return 0;
1122}
1123
1124static void hpsa_slave_destroy(struct scsi_device *sdev)
1125{
Stephen M. Cameronbcc44252010-02-04 08:41:54 -06001126 /* nothing to do. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001127}
1128
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001129static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
1130{
1131 int i;
1132
1133 if (!h->cmd_sg_list)
1134 return;
1135 for (i = 0; i < h->nr_cmds; i++) {
1136 kfree(h->cmd_sg_list[i]);
1137 h->cmd_sg_list[i] = NULL;
1138 }
1139 kfree(h->cmd_sg_list);
1140 h->cmd_sg_list = NULL;
1141}
1142
1143static int hpsa_allocate_sg_chain_blocks(struct ctlr_info *h)
1144{
1145 int i;
1146
1147 if (h->chainsize <= 0)
1148 return 0;
1149
1150 h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
1151 GFP_KERNEL);
1152 if (!h->cmd_sg_list)
1153 return -ENOMEM;
1154 for (i = 0; i < h->nr_cmds; i++) {
1155 h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
1156 h->chainsize, GFP_KERNEL);
1157 if (!h->cmd_sg_list[i])
1158 goto clean;
1159 }
1160 return 0;
1161
1162clean:
1163 hpsa_free_sg_chain_blocks(h);
1164 return -ENOMEM;
1165}
1166
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001167static int hpsa_map_sg_chain_block(struct ctlr_info *h,
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001168 struct CommandList *c)
1169{
1170 struct SGDescriptor *chain_sg, *chain_block;
1171 u64 temp64;
1172
1173 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
1174 chain_block = h->cmd_sg_list[c->cmdindex];
1175 chain_sg->Ext = HPSA_SG_CHAIN;
1176 chain_sg->Len = sizeof(*chain_sg) *
1177 (c->Header.SGTotal - h->max_cmd_sg_entries);
1178 temp64 = pci_map_single(h->pdev, chain_block, chain_sg->Len,
1179 PCI_DMA_TODEVICE);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001180 if (dma_mapping_error(&h->pdev->dev, temp64)) {
1181 /* prevent subsequent unmapping */
1182 chain_sg->Addr.lower = 0;
1183 chain_sg->Addr.upper = 0;
1184 return -1;
1185 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001186 chain_sg->Addr.lower = (u32) (temp64 & 0x0FFFFFFFFULL);
1187 chain_sg->Addr.upper = (u32) ((temp64 >> 32) & 0x0FFFFFFFFULL);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001188 return 0;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001189}
1190
1191static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
1192 struct CommandList *c)
1193{
1194 struct SGDescriptor *chain_sg;
1195 union u64bit temp64;
1196
1197 if (c->Header.SGTotal <= h->max_cmd_sg_entries)
1198 return;
1199
1200 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
1201 temp64.val32.lower = chain_sg->Addr.lower;
1202 temp64.val32.upper = chain_sg->Addr.upper;
1203 pci_unmap_single(h->pdev, temp64.val, chain_sg->Len, PCI_DMA_TODEVICE);
1204}
1205
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05001206static void complete_scsi_command(struct CommandList *cp)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001207{
1208 struct scsi_cmnd *cmd;
1209 struct ctlr_info *h;
1210 struct ErrorInfo *ei;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001211 struct hpsa_scsi_dev_t *dev;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001212
1213 unsigned char sense_key;
1214 unsigned char asc; /* additional sense code */
1215 unsigned char ascq; /* additional sense code qualifier */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05001216 unsigned long sense_data_size;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001217
1218 ei = cp->err_info;
1219 cmd = (struct scsi_cmnd *) cp->scsi_cmd;
1220 h = cp->h;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001221 dev = cmd->device->hostdata;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001222
1223 scsi_dma_unmap(cmd); /* undo the DMA mappings */
Matt Gatese1f7de02014-02-18 13:55:17 -06001224 if ((cp->cmd_type == CMD_SCSI) &&
1225 (cp->Header.SGTotal > h->max_cmd_sg_entries))
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001226 hpsa_unmap_sg_chain_block(h, cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001227
1228 cmd->result = (DID_OK << 16); /* host byte */
1229 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
Stephen M. Cameron55126722010-02-25 14:03:01 -06001230 cmd->result |= ei->ScsiStatus;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001231
1232 /* copy the sense data whether we need to or not. */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05001233 if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
1234 sense_data_size = SCSI_SENSE_BUFFERSIZE;
1235 else
1236 sense_data_size = sizeof(ei->SenseInfo);
1237 if (ei->SenseLen < sense_data_size)
1238 sense_data_size = ei->SenseLen;
1239
1240 memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001241 scsi_set_resid(cmd, ei->ResidualCnt);
1242
1243 if (ei->CommandStatus == 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001244 cmd_free(h, cp);
Tomas Henzl2cc5bfa2013-08-01 15:14:00 +02001245 cmd->scsi_done(cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001246 return;
1247 }
1248
Matt Gatese1f7de02014-02-18 13:55:17 -06001249 /* For I/O accelerator commands, copy over some fields to the normal
1250 * CISS header used below for error handling.
1251 */
1252 if (cp->cmd_type == CMD_IOACCEL1) {
1253 struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
1254 cp->Header.SGList = cp->Header.SGTotal = scsi_sg_count(cmd);
1255 cp->Request.CDBLen = c->io_flags & IOACCEL1_IOFLAGS_CDBLEN_MASK;
1256 cp->Header.Tag.lower = c->Tag.lower;
1257 cp->Header.Tag.upper = c->Tag.upper;
1258 memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
1259 memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001260
1261 /* Any RAID offload error results in retry which will use
1262 * the normal I/O path so the controller can handle whatever's
1263 * wrong.
1264 */
1265 if (is_logical_dev_addr_mode(dev->scsi3addr)) {
1266 if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
1267 dev->offload_enabled = 0;
1268 cmd->result = DID_SOFT_ERROR << 16;
1269 cmd_free(h, cp);
1270 cmd->scsi_done(cmd);
1271 return;
1272 }
Matt Gatese1f7de02014-02-18 13:55:17 -06001273 }
1274
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001275 /* an error has occurred */
1276 switch (ei->CommandStatus) {
1277
1278 case CMD_TARGET_STATUS:
1279 if (ei->ScsiStatus) {
1280 /* Get sense key */
1281 sense_key = 0xf & ei->SenseInfo[2];
1282 /* Get additional sense code */
1283 asc = ei->SenseInfo[12];
1284 /* Get addition sense code qualifier */
1285 ascq = ei->SenseInfo[13];
1286 }
1287
1288 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
Matt Gates3ce438d2013-12-04 17:10:36 -06001289 if (check_for_unit_attention(h, cp))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001290 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001291 if (sense_key == ILLEGAL_REQUEST) {
1292 /*
1293 * SCSI REPORT_LUNS is commonly unsupported on
1294 * Smart Array. Suppress noisy complaint.
1295 */
1296 if (cp->Request.CDB[0] == REPORT_LUNS)
1297 break;
1298
1299 /* If ASC/ASCQ indicate Logical Unit
1300 * Not Supported condition,
1301 */
1302 if ((asc == 0x25) && (ascq == 0x0)) {
1303 dev_warn(&h->pdev->dev, "cp %p "
1304 "has check condition\n", cp);
1305 break;
1306 }
1307 }
1308
1309 if (sense_key == NOT_READY) {
1310 /* If Sense is Not Ready, Logical Unit
1311 * Not ready, Manual Intervention
1312 * required
1313 */
1314 if ((asc == 0x04) && (ascq == 0x03)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001315 dev_warn(&h->pdev->dev, "cp %p "
1316 "has check condition: unit "
1317 "not ready, manual "
1318 "intervention required\n", cp);
1319 break;
1320 }
1321 }
Matt Gates1d3b3602010-02-04 08:43:00 -06001322 if (sense_key == ABORTED_COMMAND) {
1323 /* Aborted command is retryable */
1324 dev_warn(&h->pdev->dev, "cp %p "
1325 "has check condition: aborted command: "
1326 "ASC: 0x%x, ASCQ: 0x%x\n",
1327 cp, asc, ascq);
Stephen M. Cameron2e311fb2013-09-23 13:33:41 -05001328 cmd->result |= DID_SOFT_ERROR << 16;
Matt Gates1d3b3602010-02-04 08:43:00 -06001329 break;
1330 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001331 /* Must be some other type of check condition */
Stephen M. Cameron21b8e4e2012-05-01 11:42:25 -05001332 dev_dbg(&h->pdev->dev, "cp %p has check condition: "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001333 "unknown type: "
1334 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1335 "Returning result: 0x%x, "
1336 "cmd=[%02x %02x %02x %02x %02x "
Mike Miller807be732010-02-04 08:43:26 -06001337 "%02x %02x %02x %02x %02x %02x "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001338 "%02x %02x %02x %02x %02x]\n",
1339 cp, sense_key, asc, ascq,
1340 cmd->result,
1341 cmd->cmnd[0], cmd->cmnd[1],
1342 cmd->cmnd[2], cmd->cmnd[3],
1343 cmd->cmnd[4], cmd->cmnd[5],
1344 cmd->cmnd[6], cmd->cmnd[7],
Mike Miller807be732010-02-04 08:43:26 -06001345 cmd->cmnd[8], cmd->cmnd[9],
1346 cmd->cmnd[10], cmd->cmnd[11],
1347 cmd->cmnd[12], cmd->cmnd[13],
1348 cmd->cmnd[14], cmd->cmnd[15]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001349 break;
1350 }
1351
1352
1353 /* Problem was not a check condition
1354 * Pass it up to the upper layers...
1355 */
1356 if (ei->ScsiStatus) {
1357 dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
1358 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1359 "Returning result: 0x%x\n",
1360 cp, ei->ScsiStatus,
1361 sense_key, asc, ascq,
1362 cmd->result);
1363 } else { /* scsi status is zero??? How??? */
1364 dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
1365 "Returning no connection.\n", cp),
1366
1367 /* Ordinarily, this case should never happen,
1368 * but there is a bug in some released firmware
1369 * revisions that allows it to happen if, for
1370 * example, a 4100 backplane loses power and
1371 * the tape drive is in it. We assume that
1372 * it's a fatal error of some kind because we
1373 * can't show that it wasn't. We will make it
1374 * look like selection timeout since that is
1375 * the most common reason for this to occur,
1376 * and it's severe enough.
1377 */
1378
1379 cmd->result = DID_NO_CONNECT << 16;
1380 }
1381 break;
1382
1383 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1384 break;
1385 case CMD_DATA_OVERRUN:
1386 dev_warn(&h->pdev->dev, "cp %p has"
1387 " completed with data overrun "
1388 "reported\n", cp);
1389 break;
1390 case CMD_INVALID: {
1391 /* print_bytes(cp, sizeof(*cp), 1, 0);
1392 print_cmd(cp); */
1393 /* We get CMD_INVALID if you address a non-existent device
1394 * instead of a selection timeout (no response). You will
1395 * see this if you yank out a drive, then try to access it.
1396 * This is kind of a shame because it means that any other
1397 * CMD_INVALID (e.g. driver bug) will get interpreted as a
1398 * missing target. */
1399 cmd->result = DID_NO_CONNECT << 16;
1400 }
1401 break;
1402 case CMD_PROTOCOL_ERR:
Stephen M. Cameron256d0ea2012-09-14 16:34:25 -05001403 cmd->result = DID_ERROR << 16;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001404 dev_warn(&h->pdev->dev, "cp %p has "
Stephen M. Cameron256d0ea2012-09-14 16:34:25 -05001405 "protocol error\n", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001406 break;
1407 case CMD_HARDWARE_ERR:
1408 cmd->result = DID_ERROR << 16;
1409 dev_warn(&h->pdev->dev, "cp %p had hardware error\n", cp);
1410 break;
1411 case CMD_CONNECTION_LOST:
1412 cmd->result = DID_ERROR << 16;
1413 dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
1414 break;
1415 case CMD_ABORTED:
1416 cmd->result = DID_ABORT << 16;
1417 dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
1418 cp, ei->ScsiStatus);
1419 break;
1420 case CMD_ABORT_FAILED:
1421 cmd->result = DID_ERROR << 16;
1422 dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
1423 break;
1424 case CMD_UNSOLICITED_ABORT:
Stephen M. Cameronf6e76052011-07-26 11:08:52 -05001425 cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
1426 dev_warn(&h->pdev->dev, "cp %p aborted due to an unsolicited "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001427 "abort\n", cp);
1428 break;
1429 case CMD_TIMEOUT:
1430 cmd->result = DID_TIME_OUT << 16;
1431 dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
1432 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06001433 case CMD_UNABORTABLE:
1434 cmd->result = DID_ERROR << 16;
1435 dev_warn(&h->pdev->dev, "Command unabortable\n");
1436 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001437 case CMD_IOACCEL_DISABLED:
1438 /* This only handles the direct pass-through case since RAID
1439 * offload is handled above. Just attempt a retry.
1440 */
1441 cmd->result = DID_SOFT_ERROR << 16;
1442 dev_warn(&h->pdev->dev,
1443 "cp %p had HP SSD Smart Path error\n", cp);
1444 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001445 default:
1446 cmd->result = DID_ERROR << 16;
1447 dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
1448 cp, ei->CommandStatus);
1449 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001450 cmd_free(h, cp);
Tomas Henzl2cc5bfa2013-08-01 15:14:00 +02001451 cmd->scsi_done(cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001452}
1453
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001454static void hpsa_pci_unmap(struct pci_dev *pdev,
1455 struct CommandList *c, int sg_used, int data_direction)
1456{
1457 int i;
1458 union u64bit addr64;
1459
1460 for (i = 0; i < sg_used; i++) {
1461 addr64.val32.lower = c->SG[i].Addr.lower;
1462 addr64.val32.upper = c->SG[i].Addr.upper;
1463 pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len,
1464 data_direction);
1465 }
1466}
1467
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001468static int hpsa_map_one(struct pci_dev *pdev,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001469 struct CommandList *cp,
1470 unsigned char *buf,
1471 size_t buflen,
1472 int data_direction)
1473{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001474 u64 addr64;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001475
1476 if (buflen == 0 || data_direction == PCI_DMA_NONE) {
1477 cp->Header.SGList = 0;
1478 cp->Header.SGTotal = 0;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001479 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001480 }
1481
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001482 addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction);
Shuah Khaneceaae12013-02-20 11:24:34 -06001483 if (dma_mapping_error(&pdev->dev, addr64)) {
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001484 /* Prevent subsequent unmap of something never mapped */
Shuah Khaneceaae12013-02-20 11:24:34 -06001485 cp->Header.SGList = 0;
1486 cp->Header.SGTotal = 0;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001487 return -1;
Shuah Khaneceaae12013-02-20 11:24:34 -06001488 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001489 cp->SG[0].Addr.lower =
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001490 (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001491 cp->SG[0].Addr.upper =
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001492 (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001493 cp->SG[0].Len = buflen;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06001494 cp->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining */
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001495 cp->Header.SGList = (u8) 1; /* no. SGs contig in this cmd */
1496 cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001497 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001498}
1499
1500static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
1501 struct CommandList *c)
1502{
1503 DECLARE_COMPLETION_ONSTACK(wait);
1504
1505 c->waiting = &wait;
1506 enqueue_cmd_and_start_io(h, c);
1507 wait_for_completion(&wait);
1508}
1509
Stephen M. Camerona0c12412011-10-26 16:22:04 -05001510static void hpsa_scsi_do_simple_cmd_core_if_no_lockup(struct ctlr_info *h,
1511 struct CommandList *c)
1512{
1513 unsigned long flags;
1514
1515 /* If controller lockup detected, fake a hardware error. */
1516 spin_lock_irqsave(&h->lock, flags);
1517 if (unlikely(h->lockup_detected)) {
1518 spin_unlock_irqrestore(&h->lock, flags);
1519 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
1520 } else {
1521 spin_unlock_irqrestore(&h->lock, flags);
1522 hpsa_scsi_do_simple_cmd_core(h, c);
1523 }
1524}
1525
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001526#define MAX_DRIVER_CMD_RETRIES 25
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001527static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
1528 struct CommandList *c, int data_direction)
1529{
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001530 int backoff_time = 10, retry_count = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001531
1532 do {
Joe Perches7630abd2011-05-08 23:32:40 -07001533 memset(c->err_info, 0, sizeof(*c->err_info));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001534 hpsa_scsi_do_simple_cmd_core(h, c);
1535 retry_count++;
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001536 if (retry_count > 3) {
1537 msleep(backoff_time);
1538 if (backoff_time < 1000)
1539 backoff_time *= 2;
1540 }
Matt Bondurant852af202012-05-01 11:42:35 -05001541 } while ((check_for_unit_attention(h, c) ||
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001542 check_for_busy(h, c)) &&
1543 retry_count <= MAX_DRIVER_CMD_RETRIES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001544 hpsa_pci_unmap(h->pdev, c, 1, data_direction);
1545}
1546
1547static void hpsa_scsi_interpret_error(struct CommandList *cp)
1548{
1549 struct ErrorInfo *ei;
1550 struct device *d = &cp->h->pdev->dev;
1551
1552 ei = cp->err_info;
1553 switch (ei->CommandStatus) {
1554 case CMD_TARGET_STATUS:
1555 dev_warn(d, "cmd %p has completed with errors\n", cp);
1556 dev_warn(d, "cmd %p has SCSI Status = %x\n", cp,
1557 ei->ScsiStatus);
1558 if (ei->ScsiStatus == 0)
1559 dev_warn(d, "SCSI status is abnormally zero. "
1560 "(probably indicates selection timeout "
1561 "reported incorrectly due to a known "
1562 "firmware bug, circa July, 2001.)\n");
1563 break;
1564 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1565 dev_info(d, "UNDERRUN\n");
1566 break;
1567 case CMD_DATA_OVERRUN:
1568 dev_warn(d, "cp %p has completed with data overrun\n", cp);
1569 break;
1570 case CMD_INVALID: {
1571 /* controller unfortunately reports SCSI passthru's
1572 * to non-existent targets as invalid commands.
1573 */
1574 dev_warn(d, "cp %p is reported invalid (probably means "
1575 "target device no longer present)\n", cp);
1576 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
1577 print_cmd(cp); */
1578 }
1579 break;
1580 case CMD_PROTOCOL_ERR:
1581 dev_warn(d, "cp %p has protocol error \n", cp);
1582 break;
1583 case CMD_HARDWARE_ERR:
1584 /* cmd->result = DID_ERROR << 16; */
1585 dev_warn(d, "cp %p had hardware error\n", cp);
1586 break;
1587 case CMD_CONNECTION_LOST:
1588 dev_warn(d, "cp %p had connection lost\n", cp);
1589 break;
1590 case CMD_ABORTED:
1591 dev_warn(d, "cp %p was aborted\n", cp);
1592 break;
1593 case CMD_ABORT_FAILED:
1594 dev_warn(d, "cp %p reports abort failed\n", cp);
1595 break;
1596 case CMD_UNSOLICITED_ABORT:
1597 dev_warn(d, "cp %p aborted due to an unsolicited abort\n", cp);
1598 break;
1599 case CMD_TIMEOUT:
1600 dev_warn(d, "cp %p timed out\n", cp);
1601 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06001602 case CMD_UNABORTABLE:
1603 dev_warn(d, "Command unabortable\n");
1604 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001605 default:
1606 dev_warn(d, "cp %p returned unknown status %x\n", cp,
1607 ei->CommandStatus);
1608 }
1609}
1610
1611static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
1612 unsigned char page, unsigned char *buf,
1613 unsigned char bufsize)
1614{
1615 int rc = IO_OK;
1616 struct CommandList *c;
1617 struct ErrorInfo *ei;
1618
1619 c = cmd_special_alloc(h);
1620
1621 if (c == NULL) { /* trouble... */
1622 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06001623 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001624 }
1625
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001626 if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
1627 page, scsi3addr, TYPE_CMD)) {
1628 rc = -1;
1629 goto out;
1630 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001631 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1632 ei = c->err_info;
1633 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
1634 hpsa_scsi_interpret_error(c);
1635 rc = -1;
1636 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001637out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001638 cmd_special_free(h, c);
1639 return rc;
1640}
1641
1642static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr)
1643{
1644 int rc = IO_OK;
1645 struct CommandList *c;
1646 struct ErrorInfo *ei;
1647
1648 c = cmd_special_alloc(h);
1649
1650 if (c == NULL) { /* trouble... */
1651 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
Stephen M. Camerone9ea04a2010-02-25 14:03:06 -06001652 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001653 }
1654
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001655 /* fill_cmd can't fail here, no data buffer to map. */
1656 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h,
1657 NULL, 0, 0, scsi3addr, TYPE_MSG);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001658 hpsa_scsi_do_simple_cmd_core(h, c);
1659 /* no unmap needed here because no data xfer. */
1660
1661 ei = c->err_info;
1662 if (ei->CommandStatus != 0) {
1663 hpsa_scsi_interpret_error(c);
1664 rc = -1;
1665 }
1666 cmd_special_free(h, c);
1667 return rc;
1668}
1669
1670static void hpsa_get_raid_level(struct ctlr_info *h,
1671 unsigned char *scsi3addr, unsigned char *raid_level)
1672{
1673 int rc;
1674 unsigned char *buf;
1675
1676 *raid_level = RAID_UNKNOWN;
1677 buf = kzalloc(64, GFP_KERNEL);
1678 if (!buf)
1679 return;
1680 rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0xC1, buf, 64);
1681 if (rc == 0)
1682 *raid_level = buf[8];
1683 if (*raid_level > RAID_UNKNOWN)
1684 *raid_level = RAID_UNKNOWN;
1685 kfree(buf);
1686 return;
1687}
1688
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001689#define HPSA_MAP_DEBUG
1690#ifdef HPSA_MAP_DEBUG
1691static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
1692 struct raid_map_data *map_buff)
1693{
1694 struct raid_map_disk_data *dd = &map_buff->data[0];
1695 int map, row, col;
1696 u16 map_cnt, row_cnt, disks_per_row;
1697
1698 if (rc != 0)
1699 return;
1700
1701 dev_info(&h->pdev->dev, "structure_size = %u\n",
1702 le32_to_cpu(map_buff->structure_size));
1703 dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
1704 le32_to_cpu(map_buff->volume_blk_size));
1705 dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
1706 le64_to_cpu(map_buff->volume_blk_cnt));
1707 dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
1708 map_buff->phys_blk_shift);
1709 dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
1710 map_buff->parity_rotation_shift);
1711 dev_info(&h->pdev->dev, "strip_size = %u\n",
1712 le16_to_cpu(map_buff->strip_size));
1713 dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
1714 le64_to_cpu(map_buff->disk_starting_blk));
1715 dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
1716 le64_to_cpu(map_buff->disk_blk_cnt));
1717 dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
1718 le16_to_cpu(map_buff->data_disks_per_row));
1719 dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
1720 le16_to_cpu(map_buff->metadata_disks_per_row));
1721 dev_info(&h->pdev->dev, "row_cnt = %u\n",
1722 le16_to_cpu(map_buff->row_cnt));
1723 dev_info(&h->pdev->dev, "layout_map_count = %u\n",
1724 le16_to_cpu(map_buff->layout_map_count));
1725
1726 map_cnt = le16_to_cpu(map_buff->layout_map_count);
1727 for (map = 0; map < map_cnt; map++) {
1728 dev_info(&h->pdev->dev, "Map%u:\n", map);
1729 row_cnt = le16_to_cpu(map_buff->row_cnt);
1730 for (row = 0; row < row_cnt; row++) {
1731 dev_info(&h->pdev->dev, " Row%u:\n", row);
1732 disks_per_row =
1733 le16_to_cpu(map_buff->data_disks_per_row);
1734 for (col = 0; col < disks_per_row; col++, dd++)
1735 dev_info(&h->pdev->dev,
1736 " D%02u: h=0x%04x xor=%u,%u\n",
1737 col, dd->ioaccel_handle,
1738 dd->xor_mult[0], dd->xor_mult[1]);
1739 disks_per_row =
1740 le16_to_cpu(map_buff->metadata_disks_per_row);
1741 for (col = 0; col < disks_per_row; col++, dd++)
1742 dev_info(&h->pdev->dev,
1743 " M%02u: h=0x%04x xor=%u,%u\n",
1744 col, dd->ioaccel_handle,
1745 dd->xor_mult[0], dd->xor_mult[1]);
1746 }
1747 }
1748}
1749#else
1750static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
1751 __attribute__((unused)) int rc,
1752 __attribute__((unused)) struct raid_map_data *map_buff)
1753{
1754}
1755#endif
1756
1757static int hpsa_get_raid_map(struct ctlr_info *h,
1758 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
1759{
1760 int rc = 0;
1761 struct CommandList *c;
1762 struct ErrorInfo *ei;
1763
1764 c = cmd_special_alloc(h);
1765 if (c == NULL) {
1766 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1767 return -ENOMEM;
1768 }
1769 if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
1770 sizeof(this_device->raid_map), 0,
1771 scsi3addr, TYPE_CMD)) {
1772 dev_warn(&h->pdev->dev, "Out of memory in hpsa_get_raid_map()\n");
1773 cmd_special_free(h, c);
1774 return -ENOMEM;
1775 }
1776 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1777 ei = c->err_info;
1778 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
1779 hpsa_scsi_interpret_error(c);
1780 cmd_special_free(h, c);
1781 return -1;
1782 }
1783 cmd_special_free(h, c);
1784
1785 /* @todo in the future, dynamically allocate RAID map memory */
1786 if (le32_to_cpu(this_device->raid_map.structure_size) >
1787 sizeof(this_device->raid_map)) {
1788 dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
1789 rc = -1;
1790 }
1791 hpsa_debug_map_buff(h, rc, &this_device->raid_map);
1792 return rc;
1793}
1794
1795static void hpsa_get_ioaccel_status(struct ctlr_info *h,
1796 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
1797{
1798 int rc;
1799 unsigned char *buf;
1800 u8 ioaccel_status;
1801
1802 this_device->offload_config = 0;
1803 this_device->offload_enabled = 0;
1804
1805 buf = kzalloc(64, GFP_KERNEL);
1806 if (!buf)
1807 return;
1808 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
1809 HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
1810 if (rc != 0)
1811 goto out;
1812
1813#define IOACCEL_STATUS_BYTE 4
1814#define OFFLOAD_CONFIGURED_BIT 0x01
1815#define OFFLOAD_ENABLED_BIT 0x02
1816 ioaccel_status = buf[IOACCEL_STATUS_BYTE];
1817 this_device->offload_config =
1818 !!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
1819 if (this_device->offload_config) {
1820 this_device->offload_enabled =
1821 !!(ioaccel_status & OFFLOAD_ENABLED_BIT);
1822 if (hpsa_get_raid_map(h, scsi3addr, this_device))
1823 this_device->offload_enabled = 0;
1824 }
1825out:
1826 kfree(buf);
1827 return;
1828}
1829
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001830/* Get the device id from inquiry page 0x83 */
1831static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
1832 unsigned char *device_id, int buflen)
1833{
1834 int rc;
1835 unsigned char *buf;
1836
1837 if (buflen > 16)
1838 buflen = 16;
1839 buf = kzalloc(64, GFP_KERNEL);
1840 if (!buf)
1841 return -1;
1842 rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0x83, buf, 64);
1843 if (rc == 0)
1844 memcpy(device_id, &buf[8], buflen);
1845 kfree(buf);
1846 return rc != 0;
1847}
1848
1849static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
1850 struct ReportLUNdata *buf, int bufsize,
1851 int extended_response)
1852{
1853 int rc = IO_OK;
1854 struct CommandList *c;
1855 unsigned char scsi3addr[8];
1856 struct ErrorInfo *ei;
1857
1858 c = cmd_special_alloc(h);
1859 if (c == NULL) { /* trouble... */
1860 dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1861 return -1;
1862 }
Stephen M. Camerone89c0ae2010-02-04 08:42:04 -06001863 /* address the controller */
1864 memset(scsi3addr, 0, sizeof(scsi3addr));
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001865 if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
1866 buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
1867 rc = -1;
1868 goto out;
1869 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001870 if (extended_response)
1871 c->Request.CDB[1] = extended_response;
1872 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1873 ei = c->err_info;
1874 if (ei->CommandStatus != 0 &&
1875 ei->CommandStatus != CMD_DATA_UNDERRUN) {
1876 hpsa_scsi_interpret_error(c);
1877 rc = -1;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001878 } else {
1879 if (buf->extended_response_flag != extended_response) {
1880 dev_err(&h->pdev->dev,
1881 "report luns requested format %u, got %u\n",
1882 extended_response,
1883 buf->extended_response_flag);
1884 rc = -1;
1885 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001886 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001887out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001888 cmd_special_free(h, c);
1889 return rc;
1890}
1891
1892static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
1893 struct ReportLUNdata *buf,
1894 int bufsize, int extended_response)
1895{
1896 return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
1897}
1898
1899static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
1900 struct ReportLUNdata *buf, int bufsize)
1901{
1902 return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
1903}
1904
1905static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
1906 int bus, int target, int lun)
1907{
1908 device->bus = bus;
1909 device->target = target;
1910 device->lun = lun;
1911}
1912
1913static int hpsa_update_device_info(struct ctlr_info *h,
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05001914 unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
1915 unsigned char *is_OBDR_device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001916{
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05001917
1918#define OBDR_SIG_OFFSET 43
1919#define OBDR_TAPE_SIG "$DR-10"
1920#define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
1921#define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
1922
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06001923 unsigned char *inq_buff;
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05001924 unsigned char *obdr_sig;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001925
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06001926 inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001927 if (!inq_buff)
1928 goto bail_out;
1929
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001930 /* Do an inquiry to the device to see what it is. */
1931 if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
1932 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
1933 /* Inquiry failed (msg printed already) */
1934 dev_err(&h->pdev->dev,
1935 "hpsa_update_device_info: inquiry failed\n");
1936 goto bail_out;
1937 }
1938
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001939 this_device->devtype = (inq_buff[0] & 0x1f);
1940 memcpy(this_device->scsi3addr, scsi3addr, 8);
1941 memcpy(this_device->vendor, &inq_buff[8],
1942 sizeof(this_device->vendor));
1943 memcpy(this_device->model, &inq_buff[16],
1944 sizeof(this_device->model));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001945 memset(this_device->device_id, 0,
1946 sizeof(this_device->device_id));
1947 hpsa_get_device_id(h, scsi3addr, this_device->device_id,
1948 sizeof(this_device->device_id));
1949
1950 if (this_device->devtype == TYPE_DISK &&
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001951 is_logical_dev_addr_mode(scsi3addr)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001952 hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001953 if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
1954 hpsa_get_ioaccel_status(h, scsi3addr, this_device);
1955 } else {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001956 this_device->raid_level = RAID_UNKNOWN;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001957 this_device->offload_config = 0;
1958 this_device->offload_enabled = 0;
1959 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001960
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05001961 if (is_OBDR_device) {
1962 /* See if this is a One-Button-Disaster-Recovery device
1963 * by looking for "$DR-10" at offset 43 in inquiry data.
1964 */
1965 obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
1966 *is_OBDR_device = (this_device->devtype == TYPE_ROM &&
1967 strncmp(obdr_sig, OBDR_TAPE_SIG,
1968 OBDR_SIG_LEN) == 0);
1969 }
1970
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001971 kfree(inq_buff);
1972 return 0;
1973
1974bail_out:
1975 kfree(inq_buff);
1976 return 1;
1977}
1978
Scott Teel4f4eb9f2012-01-19 14:01:25 -06001979static unsigned char *ext_target_model[] = {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001980 "MSA2012",
1981 "MSA2024",
1982 "MSA2312",
1983 "MSA2324",
Stephen M. Cameronfda38512011-05-03 15:00:07 -05001984 "P2000 G3 SAS",
Stephen M. Camerone06c8e52013-09-23 13:33:56 -05001985 "MSA 2040 SAS",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001986 NULL,
1987};
1988
Scott Teel4f4eb9f2012-01-19 14:01:25 -06001989static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001990{
1991 int i;
1992
Scott Teel4f4eb9f2012-01-19 14:01:25 -06001993 for (i = 0; ext_target_model[i]; i++)
1994 if (strncmp(device->model, ext_target_model[i],
1995 strlen(ext_target_model[i])) == 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001996 return 1;
1997 return 0;
1998}
1999
2000/* Helper function to assign bus, target, lun mapping of devices.
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002001 * Puts non-external target logical volumes on bus 0, external target logical
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002002 * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
2003 * Logical drive target and lun are assigned at this time, but
2004 * physical device lun and target assignment are deferred (assigned
2005 * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
2006 */
2007static void figure_bus_target_lun(struct ctlr_info *h,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002008 u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002009{
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002010 u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002011
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002012 if (!is_logical_dev_addr_mode(lunaddrbytes)) {
2013 /* physical device, target and lun filled in later */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002014 if (is_hba_lunid(lunaddrbytes))
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002015 hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002016 else
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002017 /* defer target, lun assignment for physical devices */
2018 hpsa_set_bus_target_lun(device, 2, -1, -1);
2019 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002020 }
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002021 /* It's a logical device */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002022 if (is_ext_target(h, device)) {
2023 /* external target way, put logicals on bus 1
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002024 * and match target/lun numbers box
2025 * reports, other smart array, bus 0, target 0, match lunid
2026 */
2027 hpsa_set_bus_target_lun(device,
2028 1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
2029 return;
2030 }
2031 hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002032}
2033
2034/*
2035 * If there is no lun 0 on a target, linux won't find any devices.
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002036 * For the external targets (arrays), we have to manually detect the enclosure
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002037 * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
2038 * it for some reason. *tmpdevice is the target we're adding,
2039 * this_device is a pointer into the current element of currentsd[]
2040 * that we're building up in update_scsi_devices(), below.
2041 * lunzerobits is a bitmap that tracks which targets already have a
2042 * lun 0 assigned.
2043 * Returns 1 if an enclosure was added, 0 if not.
2044 */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002045static int add_ext_target_dev(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002046 struct hpsa_scsi_dev_t *tmpdevice,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002047 struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002048 unsigned long lunzerobits[], int *n_ext_target_devs)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002049{
2050 unsigned char scsi3addr[8];
2051
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002052 if (test_bit(tmpdevice->target, lunzerobits))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002053 return 0; /* There is already a lun 0 on this target. */
2054
2055 if (!is_logical_dev_addr_mode(lunaddrbytes))
2056 return 0; /* It's the logical targets that may lack lun 0. */
2057
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002058 if (!is_ext_target(h, tmpdevice))
2059 return 0; /* Only external target devices have this problem. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002060
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002061 if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002062 return 0;
2063
Stephen M. Cameronc4f8a292011-01-07 10:55:43 -06002064 memset(scsi3addr, 0, 8);
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002065 scsi3addr[3] = tmpdevice->target;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002066 if (is_hba_lunid(scsi3addr))
2067 return 0; /* Don't add the RAID controller here. */
2068
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002069 if (is_scsi_rev_5(h))
2070 return 0; /* p1210m doesn't need to do this. */
2071
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002072 if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
Scott Teelaca4a522012-01-19 14:01:19 -06002073 dev_warn(&h->pdev->dev, "Maximum number of external "
2074 "target devices exceeded. Check your hardware "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002075 "configuration.");
2076 return 0;
2077 }
2078
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002079 if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002080 return 0;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002081 (*n_ext_target_devs)++;
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002082 hpsa_set_bus_target_lun(this_device,
2083 tmpdevice->bus, tmpdevice->target, 0);
2084 set_bit(tmpdevice->target, lunzerobits);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002085 return 1;
2086}
2087
2088/*
2089 * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
2090 * logdev. The number of luns in physdev and logdev are returned in
2091 * *nphysicals and *nlogicals, respectively.
2092 * Returns 0 on success, -1 otherwise.
2093 */
2094static int hpsa_gather_lun_info(struct ctlr_info *h,
2095 int reportlunsize,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002096 struct ReportLUNdata *physdev, u32 *nphysicals, int *physical_mode,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002097 struct ReportLUNdata *logdev, u32 *nlogicals)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002098{
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002099 int physical_entry_size = 8;
2100
2101 *physical_mode = 0;
2102
2103 /* For I/O accelerator mode we need to read physical device handles */
2104 if (h->transMethod & CFGTBL_Trans_io_accel1) {
2105 *physical_mode = HPSA_REPORT_PHYS_EXTENDED;
2106 physical_entry_size = 24;
2107 }
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002108 if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002109 *physical_mode)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002110 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
2111 return -1;
2112 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002113 *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) /
2114 physical_entry_size;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002115 if (*nphysicals > HPSA_MAX_PHYS_LUN) {
2116 dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
2117 " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2118 *nphysicals - HPSA_MAX_PHYS_LUN);
2119 *nphysicals = HPSA_MAX_PHYS_LUN;
2120 }
2121 if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
2122 dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
2123 return -1;
2124 }
Stephen M. Cameron6df1e952010-02-04 08:42:19 -06002125 *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002126 /* Reject Logicals in excess of our max capability. */
2127 if (*nlogicals > HPSA_MAX_LUN) {
2128 dev_warn(&h->pdev->dev,
2129 "maximum logical LUNs (%d) exceeded. "
2130 "%d LUNs ignored.\n", HPSA_MAX_LUN,
2131 *nlogicals - HPSA_MAX_LUN);
2132 *nlogicals = HPSA_MAX_LUN;
2133 }
2134 if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
2135 dev_warn(&h->pdev->dev,
2136 "maximum logical + physical LUNs (%d) exceeded. "
2137 "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2138 *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
2139 *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
2140 }
2141 return 0;
2142}
2143
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002144u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i,
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002145 int nphysicals, int nlogicals,
2146 struct ReportExtendedLUNdata *physdev_list,
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002147 struct ReportLUNdata *logdev_list)
2148{
2149 /* Helper function, figure out where the LUN ID info is coming from
2150 * given index i, lists of physical and logical devices, where in
2151 * the list the raid controller is supposed to appear (first or last)
2152 */
2153
2154 int logicals_start = nphysicals + (raid_ctlr_position == 0);
2155 int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
2156
2157 if (i == raid_ctlr_position)
2158 return RAID_CTLR_LUNID;
2159
2160 if (i < logicals_start)
2161 return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0];
2162
2163 if (i < last_device)
2164 return &logdev_list->LUN[i - nphysicals -
2165 (raid_ctlr_position == 0)][0];
2166 BUG();
2167 return NULL;
2168}
2169
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002170static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
2171{
2172 /* the idea here is we could get notified
2173 * that some devices have changed, so we do a report
2174 * physical luns and report logical luns cmd, and adjust
2175 * our list of devices accordingly.
2176 *
2177 * The scsi3addr's of devices won't change so long as the
2178 * adapter is not reset. That means we can rescan and
2179 * tell which devices we already know about, vs. new
2180 * devices, vs. disappearing devices.
2181 */
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002182 struct ReportExtendedLUNdata *physdev_list = NULL;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002183 struct ReportLUNdata *logdev_list = NULL;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002184 u32 nphysicals = 0;
2185 u32 nlogicals = 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002186 int physical_mode = 0;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002187 u32 ndev_allocated = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002188 struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
2189 int ncurrent = 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002190 int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 24;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002191 int i, n_ext_target_devs, ndevs_to_allocate;
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002192 int raid_ctlr_position;
Scott Teelaca4a522012-01-19 14:01:19 -06002193 DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002194
Scott Teelcfe5bad2011-10-26 16:21:07 -05002195 currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002196 physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
2197 logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002198 tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
2199
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002200 if (!currentsd || !physdev_list || !logdev_list || !tmpdevice) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002201 dev_err(&h->pdev->dev, "out of memory\n");
2202 goto out;
2203 }
2204 memset(lunzerobits, 0, sizeof(lunzerobits));
2205
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002206 if (hpsa_gather_lun_info(h, reportlunsize,
2207 (struct ReportLUNdata *) physdev_list, &nphysicals,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002208 &physical_mode, logdev_list, &nlogicals))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002209 goto out;
2210
Scott Teelaca4a522012-01-19 14:01:19 -06002211 /* We might see up to the maximum number of logical and physical disks
2212 * plus external target devices, and a device for the local RAID
2213 * controller.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002214 */
Scott Teelaca4a522012-01-19 14:01:19 -06002215 ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002216
2217 /* Allocate the per device structures */
2218 for (i = 0; i < ndevs_to_allocate; i++) {
Scott Teelb7ec0212011-10-26 16:21:12 -05002219 if (i >= HPSA_MAX_DEVICES) {
2220 dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
2221 " %d devices ignored.\n", HPSA_MAX_DEVICES,
2222 ndevs_to_allocate - HPSA_MAX_DEVICES);
2223 break;
2224 }
2225
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002226 currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
2227 if (!currentsd[i]) {
2228 dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
2229 __FILE__, __LINE__);
2230 goto out;
2231 }
2232 ndev_allocated++;
2233 }
2234
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002235 if (unlikely(is_scsi_rev_5(h)))
2236 raid_ctlr_position = 0;
2237 else
2238 raid_ctlr_position = nphysicals + nlogicals;
2239
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002240 /* adjust our table of devices */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002241 n_ext_target_devs = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002242 for (i = 0; i < nphysicals + nlogicals + 1; i++) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002243 u8 *lunaddrbytes, is_OBDR = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002244
2245 /* Figure out where the LUN ID info is coming from */
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002246 lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
2247 i, nphysicals, nlogicals, physdev_list, logdev_list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002248 /* skip masked physical devices. */
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002249 if (lunaddrbytes[3] & 0xC0 &&
2250 i < nphysicals + (raid_ctlr_position == 0))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002251 continue;
2252
2253 /* Get device type, vendor, model, device id */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002254 if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
2255 &is_OBDR))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002256 continue; /* skip it if we can't talk to it. */
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002257 figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002258 this_device = currentsd[ncurrent];
2259
2260 /*
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002261 * For external target devices, we have to insert a LUN 0 which
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002262 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
2263 * is nonetheless an enclosure device there. We have to
2264 * present that otherwise linux won't find anything if
2265 * there is no lun 0.
2266 */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002267 if (add_ext_target_dev(h, tmpdevice, this_device,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002268 lunaddrbytes, lunzerobits,
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002269 &n_ext_target_devs)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002270 ncurrent++;
2271 this_device = currentsd[ncurrent];
2272 }
2273
2274 *this_device = *tmpdevice;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002275
2276 switch (this_device->devtype) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002277 case TYPE_ROM:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002278 /* We don't *really* support actual CD-ROM devices,
2279 * just "One Button Disaster Recovery" tape drive
2280 * which temporarily pretends to be a CD-ROM drive.
2281 * So we check that the device is really an OBDR tape
2282 * device by checking for "$DR-10" in bytes 43-48 of
2283 * the inquiry data.
2284 */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002285 if (is_OBDR)
2286 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002287 break;
2288 case TYPE_DISK:
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002289 if (i >= nphysicals) {
2290 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002291 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002292 }
2293 if (physical_mode == HPSA_REPORT_PHYS_EXTENDED) {
2294 memcpy(&this_device->ioaccel_handle,
2295 &lunaddrbytes[20],
2296 sizeof(this_device->ioaccel_handle));
2297 ncurrent++;
2298 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002299 break;
2300 case TYPE_TAPE:
2301 case TYPE_MEDIUM_CHANGER:
2302 ncurrent++;
2303 break;
2304 case TYPE_RAID:
2305 /* Only present the Smartarray HBA as a RAID controller.
2306 * If it's a RAID controller other than the HBA itself
2307 * (an external RAID controller, MSA500 or similar)
2308 * don't present it.
2309 */
2310 if (!is_hba_lunid(lunaddrbytes))
2311 break;
2312 ncurrent++;
2313 break;
2314 default:
2315 break;
2316 }
Scott Teelcfe5bad2011-10-26 16:21:07 -05002317 if (ncurrent >= HPSA_MAX_DEVICES)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002318 break;
2319 }
2320 adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
2321out:
2322 kfree(tmpdevice);
2323 for (i = 0; i < ndev_allocated; i++)
2324 kfree(currentsd[i]);
2325 kfree(currentsd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002326 kfree(physdev_list);
2327 kfree(logdev_list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002328}
2329
2330/* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
2331 * dma mapping and fills in the scatter gather entries of the
2332 * hpsa command, cp.
2333 */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002334static int hpsa_scatter_gather(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002335 struct CommandList *cp,
2336 struct scsi_cmnd *cmd)
2337{
2338 unsigned int len;
2339 struct scatterlist *sg;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002340 u64 addr64;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002341 int use_sg, i, sg_index, chained;
2342 struct SGDescriptor *curr_sg;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002343
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002344 BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002345
2346 use_sg = scsi_dma_map(cmd);
2347 if (use_sg < 0)
2348 return use_sg;
2349
2350 if (!use_sg)
2351 goto sglist_finished;
2352
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002353 curr_sg = cp->SG;
2354 chained = 0;
2355 sg_index = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002356 scsi_for_each_sg(cmd, sg, use_sg, i) {
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002357 if (i == h->max_cmd_sg_entries - 1 &&
2358 use_sg > h->max_cmd_sg_entries) {
2359 chained = 1;
2360 curr_sg = h->cmd_sg_list[cp->cmdindex];
2361 sg_index = 0;
2362 }
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002363 addr64 = (u64) sg_dma_address(sg);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002364 len = sg_dma_len(sg);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002365 curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
2366 curr_sg->Addr.upper = (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
2367 curr_sg->Len = len;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06002368 curr_sg->Ext = (i < scsi_sg_count(cmd) - 1) ? 0 : HPSA_SG_LAST;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002369 curr_sg++;
2370 }
2371
2372 if (use_sg + chained > h->maxSG)
2373 h->maxSG = use_sg + chained;
2374
2375 if (chained) {
2376 cp->Header.SGList = h->max_cmd_sg_entries;
2377 cp->Header.SGTotal = (u16) (use_sg + 1);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002378 if (hpsa_map_sg_chain_block(h, cp)) {
2379 scsi_dma_unmap(cmd);
2380 return -1;
2381 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002382 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002383 }
2384
2385sglist_finished:
2386
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002387 cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
2388 cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002389 return 0;
2390}
2391
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002392#define IO_ACCEL_INELIGIBLE (1)
2393static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
2394{
2395 int is_write = 0;
2396 u32 block;
2397 u32 block_cnt;
2398
2399 /* Perform some CDB fixups if needed using 10 byte reads/writes only */
2400 switch (cdb[0]) {
2401 case WRITE_6:
2402 case WRITE_12:
2403 is_write = 1;
2404 case READ_6:
2405 case READ_12:
2406 if (*cdb_len == 6) {
2407 block = (((u32) cdb[2]) << 8) | cdb[3];
2408 block_cnt = cdb[4];
2409 } else {
2410 BUG_ON(*cdb_len != 12);
2411 block = (((u32) cdb[2]) << 24) |
2412 (((u32) cdb[3]) << 16) |
2413 (((u32) cdb[4]) << 8) |
2414 cdb[5];
2415 block_cnt =
2416 (((u32) cdb[6]) << 24) |
2417 (((u32) cdb[7]) << 16) |
2418 (((u32) cdb[8]) << 8) |
2419 cdb[9];
2420 }
2421 if (block_cnt > 0xffff)
2422 return IO_ACCEL_INELIGIBLE;
2423
2424 cdb[0] = is_write ? WRITE_10 : READ_10;
2425 cdb[1] = 0;
2426 cdb[2] = (u8) (block >> 24);
2427 cdb[3] = (u8) (block >> 16);
2428 cdb[4] = (u8) (block >> 8);
2429 cdb[5] = (u8) (block);
2430 cdb[6] = 0;
2431 cdb[7] = (u8) (block_cnt >> 8);
2432 cdb[8] = (u8) (block_cnt);
2433 cdb[9] = 0;
2434 *cdb_len = 10;
2435 break;
2436 }
2437 return 0;
2438}
2439
Matt Gatese1f7de02014-02-18 13:55:17 -06002440/*
2441 * Queue a command to the I/O accelerator path.
Matt Gatese1f7de02014-02-18 13:55:17 -06002442 */
2443static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002444 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
2445 u8 *scsi3addr)
Matt Gatese1f7de02014-02-18 13:55:17 -06002446{
2447 struct scsi_cmnd *cmd = c->scsi_cmd;
Matt Gatese1f7de02014-02-18 13:55:17 -06002448 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
2449 unsigned int len;
2450 unsigned int total_len = 0;
2451 struct scatterlist *sg;
2452 u64 addr64;
2453 int use_sg, i;
2454 struct SGDescriptor *curr_sg;
2455 u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
2456
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002457 /* TODO: implement chaining support */
2458 if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
2459 return IO_ACCEL_INELIGIBLE;
2460
Matt Gatese1f7de02014-02-18 13:55:17 -06002461 BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
2462
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002463 if (fixup_ioaccel_cdb(cdb, &cdb_len))
2464 return IO_ACCEL_INELIGIBLE;
2465
Matt Gatese1f7de02014-02-18 13:55:17 -06002466 c->cmd_type = CMD_IOACCEL1;
2467
2468 /* Adjust the DMA address to point to the accelerated command buffer */
2469 c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
2470 (c->cmdindex * sizeof(*cp));
2471 BUG_ON(c->busaddr & 0x0000007F);
2472
2473 use_sg = scsi_dma_map(cmd);
2474 if (use_sg < 0)
2475 return use_sg;
2476
2477 if (use_sg) {
2478 curr_sg = cp->SG;
2479 scsi_for_each_sg(cmd, sg, use_sg, i) {
2480 addr64 = (u64) sg_dma_address(sg);
2481 len = sg_dma_len(sg);
2482 total_len += len;
2483 curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
2484 curr_sg->Addr.upper =
2485 (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
2486 curr_sg->Len = len;
2487
2488 if (i == (scsi_sg_count(cmd) - 1))
2489 curr_sg->Ext = HPSA_SG_LAST;
2490 else
2491 curr_sg->Ext = 0; /* we are not chaining */
2492 curr_sg++;
2493 }
2494
2495 switch (cmd->sc_data_direction) {
2496 case DMA_TO_DEVICE:
2497 control |= IOACCEL1_CONTROL_DATA_OUT;
2498 break;
2499 case DMA_FROM_DEVICE:
2500 control |= IOACCEL1_CONTROL_DATA_IN;
2501 break;
2502 case DMA_NONE:
2503 control |= IOACCEL1_CONTROL_NODATAXFER;
2504 break;
2505 default:
2506 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
2507 cmd->sc_data_direction);
2508 BUG();
2509 break;
2510 }
2511 } else {
2512 control |= IOACCEL1_CONTROL_NODATAXFER;
2513 }
2514
2515 /* Fill out the command structure to submit */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002516 cp->dev_handle = ioaccel_handle & 0xFFFF;
Matt Gatese1f7de02014-02-18 13:55:17 -06002517 cp->transfer_len = total_len;
2518 cp->io_flags = IOACCEL1_IOFLAGS_IO_REQ |
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002519 (cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK);
Matt Gatese1f7de02014-02-18 13:55:17 -06002520 cp->control = control;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002521 memcpy(cp->CDB, cdb, cdb_len);
2522 memcpy(cp->CISS_LUN, scsi3addr, 8);
Matt Gatese1f7de02014-02-18 13:55:17 -06002523
2524 /* Tell the controller to post the reply to the queue for this
2525 * processor. This seems to give the best I/O throughput.
2526 */
2527 cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
2528
2529 /* Set the bits in the address sent down to include:
2530 * - performant mode bit (bit 0)
2531 * - pull count (bits 1-3)
2532 * - command type (bits 4-6)
2533 */
2534 c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[use_sg] << 1) |
2535 IOACCEL1_BUSADDR_CMDTYPE;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002536 enqueue_cmd_and_start_io(h, c);
Matt Gatese1f7de02014-02-18 13:55:17 -06002537 return 0;
2538}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002539
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002540/*
2541 * Queue a command directly to a device behind the controller using the
2542 * I/O accelerator path.
2543 */
2544static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
2545 struct CommandList *c)
2546{
2547 struct scsi_cmnd *cmd = c->scsi_cmd;
2548 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
2549
2550 return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
2551 cmd->cmnd, cmd->cmd_len, dev->scsi3addr);
2552}
2553
2554/*
2555 * Attempt to perform offload RAID mapping for a logical volume I/O.
2556 */
2557static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
2558 struct CommandList *c)
2559{
2560 struct scsi_cmnd *cmd = c->scsi_cmd;
2561 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
2562 struct raid_map_data *map = &dev->raid_map;
2563 struct raid_map_disk_data *dd = &map->data[0];
2564 int is_write = 0;
2565 u32 map_index;
2566 u64 first_block, last_block;
2567 u32 block_cnt;
2568 u32 blocks_per_row;
2569 u64 first_row, last_row;
2570 u32 first_row_offset, last_row_offset;
2571 u32 first_column, last_column;
2572 u32 map_row;
2573 u32 disk_handle;
2574 u64 disk_block;
2575 u32 disk_block_cnt;
2576 u8 cdb[16];
2577 u8 cdb_len;
2578#if BITS_PER_LONG == 32
2579 u64 tmpdiv;
2580#endif
2581
2582 BUG_ON(!(dev->offload_config && dev->offload_enabled));
2583
2584 /* check for valid opcode, get LBA and block count */
2585 switch (cmd->cmnd[0]) {
2586 case WRITE_6:
2587 is_write = 1;
2588 case READ_6:
2589 first_block =
2590 (((u64) cmd->cmnd[2]) << 8) |
2591 cmd->cmnd[3];
2592 block_cnt = cmd->cmnd[4];
2593 break;
2594 case WRITE_10:
2595 is_write = 1;
2596 case READ_10:
2597 first_block =
2598 (((u64) cmd->cmnd[2]) << 24) |
2599 (((u64) cmd->cmnd[3]) << 16) |
2600 (((u64) cmd->cmnd[4]) << 8) |
2601 cmd->cmnd[5];
2602 block_cnt =
2603 (((u32) cmd->cmnd[7]) << 8) |
2604 cmd->cmnd[8];
2605 break;
2606 case WRITE_12:
2607 is_write = 1;
2608 case READ_12:
2609 first_block =
2610 (((u64) cmd->cmnd[2]) << 24) |
2611 (((u64) cmd->cmnd[3]) << 16) |
2612 (((u64) cmd->cmnd[4]) << 8) |
2613 cmd->cmnd[5];
2614 block_cnt =
2615 (((u32) cmd->cmnd[6]) << 24) |
2616 (((u32) cmd->cmnd[7]) << 16) |
2617 (((u32) cmd->cmnd[8]) << 8) |
2618 cmd->cmnd[9];
2619 break;
2620 case WRITE_16:
2621 is_write = 1;
2622 case READ_16:
2623 first_block =
2624 (((u64) cmd->cmnd[2]) << 56) |
2625 (((u64) cmd->cmnd[3]) << 48) |
2626 (((u64) cmd->cmnd[4]) << 40) |
2627 (((u64) cmd->cmnd[5]) << 32) |
2628 (((u64) cmd->cmnd[6]) << 24) |
2629 (((u64) cmd->cmnd[7]) << 16) |
2630 (((u64) cmd->cmnd[8]) << 8) |
2631 cmd->cmnd[9];
2632 block_cnt =
2633 (((u32) cmd->cmnd[10]) << 24) |
2634 (((u32) cmd->cmnd[11]) << 16) |
2635 (((u32) cmd->cmnd[12]) << 8) |
2636 cmd->cmnd[13];
2637 break;
2638 default:
2639 return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
2640 }
2641 BUG_ON(block_cnt == 0);
2642 last_block = first_block + block_cnt - 1;
2643
2644 /* check for write to non-RAID-0 */
2645 if (is_write && dev->raid_level != 0)
2646 return IO_ACCEL_INELIGIBLE;
2647
2648 /* check for invalid block or wraparound */
2649 if (last_block >= map->volume_blk_cnt || last_block < first_block)
2650 return IO_ACCEL_INELIGIBLE;
2651
2652 /* calculate stripe information for the request */
2653 blocks_per_row = map->data_disks_per_row * map->strip_size;
2654#if BITS_PER_LONG == 32
2655 tmpdiv = first_block;
2656 (void) do_div(tmpdiv, blocks_per_row);
2657 first_row = tmpdiv;
2658 tmpdiv = last_block;
2659 (void) do_div(tmpdiv, blocks_per_row);
2660 last_row = tmpdiv;
2661 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
2662 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
2663 tmpdiv = first_row_offset;
2664 (void) do_div(tmpdiv, map->strip_size);
2665 first_column = tmpdiv;
2666 tmpdiv = last_row_offset;
2667 (void) do_div(tmpdiv, map->strip_size);
2668 last_column = tmpdiv;
2669#else
2670 first_row = first_block / blocks_per_row;
2671 last_row = last_block / blocks_per_row;
2672 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
2673 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
2674 first_column = first_row_offset / map->strip_size;
2675 last_column = last_row_offset / map->strip_size;
2676#endif
2677
2678 /* if this isn't a single row/column then give to the controller */
2679 if ((first_row != last_row) || (first_column != last_column))
2680 return IO_ACCEL_INELIGIBLE;
2681
2682 /* proceeding with driver mapping */
2683 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
2684 map->row_cnt;
2685 map_index = (map_row * (map->data_disks_per_row +
2686 map->metadata_disks_per_row)) + first_column;
2687 if (dev->raid_level == 2) {
2688 /* simple round-robin balancing of RAID 1+0 reads across
2689 * primary and mirror members. this is appropriate for SSD
2690 * but not optimal for HDD.
2691 */
2692 if (dev->offload_to_mirror)
2693 map_index += map->data_disks_per_row;
2694 dev->offload_to_mirror = !dev->offload_to_mirror;
2695 }
2696 disk_handle = dd[map_index].ioaccel_handle;
2697 disk_block = map->disk_starting_blk + (first_row * map->strip_size) +
2698 (first_row_offset - (first_column * map->strip_size));
2699 disk_block_cnt = block_cnt;
2700
2701 /* handle differing logical/physical block sizes */
2702 if (map->phys_blk_shift) {
2703 disk_block <<= map->phys_blk_shift;
2704 disk_block_cnt <<= map->phys_blk_shift;
2705 }
2706 BUG_ON(disk_block_cnt > 0xffff);
2707
2708 /* build the new CDB for the physical disk I/O */
2709 if (disk_block > 0xffffffff) {
2710 cdb[0] = is_write ? WRITE_16 : READ_16;
2711 cdb[1] = 0;
2712 cdb[2] = (u8) (disk_block >> 56);
2713 cdb[3] = (u8) (disk_block >> 48);
2714 cdb[4] = (u8) (disk_block >> 40);
2715 cdb[5] = (u8) (disk_block >> 32);
2716 cdb[6] = (u8) (disk_block >> 24);
2717 cdb[7] = (u8) (disk_block >> 16);
2718 cdb[8] = (u8) (disk_block >> 8);
2719 cdb[9] = (u8) (disk_block);
2720 cdb[10] = (u8) (disk_block_cnt >> 24);
2721 cdb[11] = (u8) (disk_block_cnt >> 16);
2722 cdb[12] = (u8) (disk_block_cnt >> 8);
2723 cdb[13] = (u8) (disk_block_cnt);
2724 cdb[14] = 0;
2725 cdb[15] = 0;
2726 cdb_len = 16;
2727 } else {
2728 cdb[0] = is_write ? WRITE_10 : READ_10;
2729 cdb[1] = 0;
2730 cdb[2] = (u8) (disk_block >> 24);
2731 cdb[3] = (u8) (disk_block >> 16);
2732 cdb[4] = (u8) (disk_block >> 8);
2733 cdb[5] = (u8) (disk_block);
2734 cdb[6] = 0;
2735 cdb[7] = (u8) (disk_block_cnt >> 8);
2736 cdb[8] = (u8) (disk_block_cnt);
2737 cdb[9] = 0;
2738 cdb_len = 10;
2739 }
2740 return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
2741 dev->scsi3addr);
2742}
2743
Jeff Garzikf2812332010-11-16 02:10:29 -05002744static int hpsa_scsi_queue_command_lck(struct scsi_cmnd *cmd,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002745 void (*done)(struct scsi_cmnd *))
2746{
2747 struct ctlr_info *h;
2748 struct hpsa_scsi_dev_t *dev;
2749 unsigned char scsi3addr[8];
2750 struct CommandList *c;
2751 unsigned long flags;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002752 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002753
2754 /* Get the ptr to our adapter structure out of cmd->host. */
2755 h = sdev_to_hba(cmd->device);
2756 dev = cmd->device->hostdata;
2757 if (!dev) {
2758 cmd->result = DID_NO_CONNECT << 16;
2759 done(cmd);
2760 return 0;
2761 }
2762 memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
2763
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002764 spin_lock_irqsave(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05002765 if (unlikely(h->lockup_detected)) {
2766 spin_unlock_irqrestore(&h->lock, flags);
2767 cmd->result = DID_ERROR << 16;
2768 done(cmd);
2769 return 0;
2770 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002771 spin_unlock_irqrestore(&h->lock, flags);
Matt Gatese16a33a2012-05-01 11:43:11 -05002772 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002773 if (c == NULL) { /* trouble... */
2774 dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
2775 return SCSI_MLQUEUE_HOST_BUSY;
2776 }
2777
2778 /* Fill in the command list header */
2779
2780 cmd->scsi_done = done; /* save this for use by completion code */
2781
2782 /* save c in case we have to abort it */
2783 cmd->host_scribble = (unsigned char *) c;
2784
2785 c->cmd_type = CMD_SCSI;
2786 c->scsi_cmd = cmd;
Matt Gatese1f7de02014-02-18 13:55:17 -06002787
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002788 /* Call alternate submit routine for I/O accelerated commands.
2789 * Retries always go down the normal I/O path.
2790 */
2791 if (likely(cmd->retries == 0 &&
2792 cmd->request->cmd_type == REQ_TYPE_FS)) {
2793 if (dev->offload_enabled) {
2794 rc = hpsa_scsi_ioaccel_raid_map(h, c);
2795 if (rc == 0)
2796 return 0; /* Sent on ioaccel path */
2797 if (rc < 0) { /* scsi_dma_map failed. */
2798 cmd_free(h, c);
2799 return SCSI_MLQUEUE_HOST_BUSY;
2800 }
2801 } else if (dev->ioaccel_handle) {
2802 rc = hpsa_scsi_ioaccel_direct_map(h, c);
2803 if (rc == 0)
2804 return 0; /* Sent on direct map path */
2805 if (rc < 0) { /* scsi_dma_map failed. */
2806 cmd_free(h, c);
2807 return SCSI_MLQUEUE_HOST_BUSY;
2808 }
2809 }
2810 }
Matt Gatese1f7de02014-02-18 13:55:17 -06002811
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002812 c->Header.ReplyQueue = 0; /* unused in simple mode */
2813 memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
Don Brace303932f2010-02-04 08:42:40 -06002814 c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT);
2815 c->Header.Tag.lower |= DIRECT_LOOKUP_BIT;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002816
2817 /* Fill in the request block... */
2818
2819 c->Request.Timeout = 0;
2820 memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
2821 BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
2822 c->Request.CDBLen = cmd->cmd_len;
2823 memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
2824 c->Request.Type.Type = TYPE_CMD;
2825 c->Request.Type.Attribute = ATTR_SIMPLE;
2826 switch (cmd->sc_data_direction) {
2827 case DMA_TO_DEVICE:
2828 c->Request.Type.Direction = XFER_WRITE;
2829 break;
2830 case DMA_FROM_DEVICE:
2831 c->Request.Type.Direction = XFER_READ;
2832 break;
2833 case DMA_NONE:
2834 c->Request.Type.Direction = XFER_NONE;
2835 break;
2836 case DMA_BIDIRECTIONAL:
2837 /* This can happen if a buggy application does a scsi passthru
2838 * and sets both inlen and outlen to non-zero. ( see
2839 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
2840 */
2841
2842 c->Request.Type.Direction = XFER_RSVD;
2843 /* This is technically wrong, and hpsa controllers should
2844 * reject it with CMD_INVALID, which is the most correct
2845 * response, but non-fibre backends appear to let it
2846 * slide by, and give the same results as if this field
2847 * were set correctly. Either way is acceptable for
2848 * our purposes here.
2849 */
2850
2851 break;
2852
2853 default:
2854 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
2855 cmd->sc_data_direction);
2856 BUG();
2857 break;
2858 }
2859
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002860 if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002861 cmd_free(h, c);
2862 return SCSI_MLQUEUE_HOST_BUSY;
2863 }
2864 enqueue_cmd_and_start_io(h, c);
2865 /* the cmd'll come back via intr handler in complete_scsi_command() */
2866 return 0;
2867}
2868
Jeff Garzikf2812332010-11-16 02:10:29 -05002869static DEF_SCSI_QCMD(hpsa_scsi_queue_command)
2870
Stephen M. Cameron5f389362014-02-18 13:55:48 -06002871static int do_not_scan_if_controller_locked_up(struct ctlr_info *h)
2872{
2873 unsigned long flags;
2874
2875 /*
2876 * Don't let rescans be initiated on a controller known
2877 * to be locked up. If the controller locks up *during*
2878 * a rescan, that thread is probably hosed, but at least
2879 * we can prevent new rescan threads from piling up on a
2880 * locked up controller.
2881 */
2882 spin_lock_irqsave(&h->lock, flags);
2883 if (unlikely(h->lockup_detected)) {
2884 spin_unlock_irqrestore(&h->lock, flags);
2885 spin_lock_irqsave(&h->scan_lock, flags);
2886 h->scan_finished = 1;
2887 wake_up_all(&h->scan_wait_queue);
2888 spin_unlock_irqrestore(&h->scan_lock, flags);
2889 return 1;
2890 }
2891 spin_unlock_irqrestore(&h->lock, flags);
2892 return 0;
2893}
2894
Stephen M. Camerona08a8472010-02-04 08:43:16 -06002895static void hpsa_scan_start(struct Scsi_Host *sh)
2896{
2897 struct ctlr_info *h = shost_to_hba(sh);
2898 unsigned long flags;
2899
Stephen M. Cameron5f389362014-02-18 13:55:48 -06002900 if (do_not_scan_if_controller_locked_up(h))
2901 return;
2902
Stephen M. Camerona08a8472010-02-04 08:43:16 -06002903 /* wait until any scan already in progress is finished. */
2904 while (1) {
2905 spin_lock_irqsave(&h->scan_lock, flags);
2906 if (h->scan_finished)
2907 break;
2908 spin_unlock_irqrestore(&h->scan_lock, flags);
2909 wait_event(h->scan_wait_queue, h->scan_finished);
2910 /* Note: We don't need to worry about a race between this
2911 * thread and driver unload because the midlayer will
2912 * have incremented the reference count, so unload won't
2913 * happen if we're in here.
2914 */
2915 }
2916 h->scan_finished = 0; /* mark scan as in progress */
2917 spin_unlock_irqrestore(&h->scan_lock, flags);
2918
Stephen M. Cameron5f389362014-02-18 13:55:48 -06002919 if (do_not_scan_if_controller_locked_up(h))
2920 return;
2921
Stephen M. Camerona08a8472010-02-04 08:43:16 -06002922 hpsa_update_scsi_devices(h, h->scsi_host->host_no);
2923
2924 spin_lock_irqsave(&h->scan_lock, flags);
2925 h->scan_finished = 1; /* mark scan as finished. */
2926 wake_up_all(&h->scan_wait_queue);
2927 spin_unlock_irqrestore(&h->scan_lock, flags);
2928}
2929
2930static int hpsa_scan_finished(struct Scsi_Host *sh,
2931 unsigned long elapsed_time)
2932{
2933 struct ctlr_info *h = shost_to_hba(sh);
2934 unsigned long flags;
2935 int finished;
2936
2937 spin_lock_irqsave(&h->scan_lock, flags);
2938 finished = h->scan_finished;
2939 spin_unlock_irqrestore(&h->scan_lock, flags);
2940 return finished;
2941}
2942
Stephen M. Cameron667e23d2010-02-25 14:02:51 -06002943static int hpsa_change_queue_depth(struct scsi_device *sdev,
2944 int qdepth, int reason)
2945{
2946 struct ctlr_info *h = sdev_to_hba(sdev);
2947
2948 if (reason != SCSI_QDEPTH_DEFAULT)
2949 return -ENOTSUPP;
2950
2951 if (qdepth < 1)
2952 qdepth = 1;
2953 else
2954 if (qdepth > h->nr_cmds)
2955 qdepth = h->nr_cmds;
2956 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
2957 return sdev->queue_depth;
2958}
2959
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002960static void hpsa_unregister_scsi(struct ctlr_info *h)
2961{
2962 /* we are being forcibly unloaded, and may not refuse. */
2963 scsi_remove_host(h->scsi_host);
2964 scsi_host_put(h->scsi_host);
2965 h->scsi_host = NULL;
2966}
2967
2968static int hpsa_register_scsi(struct ctlr_info *h)
2969{
Stephen M. Cameronb7056902012-01-19 14:00:53 -06002970 struct Scsi_Host *sh;
2971 int error;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002972
Stephen M. Cameronb7056902012-01-19 14:00:53 -06002973 sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
2974 if (sh == NULL)
2975 goto fail;
2976
2977 sh->io_port = 0;
2978 sh->n_io_port = 0;
2979 sh->this_id = -1;
2980 sh->max_channel = 3;
2981 sh->max_cmd_len = MAX_COMMAND_SIZE;
2982 sh->max_lun = HPSA_MAX_LUN;
2983 sh->max_id = HPSA_MAX_LUN;
2984 sh->can_queue = h->nr_cmds;
2985 sh->cmd_per_lun = h->nr_cmds;
2986 sh->sg_tablesize = h->maxsgentries;
2987 h->scsi_host = sh;
2988 sh->hostdata[0] = (unsigned long) h;
2989 sh->irq = h->intr[h->intr_mode];
2990 sh->unique_id = sh->irq;
2991 error = scsi_add_host(sh, &h->pdev->dev);
2992 if (error)
2993 goto fail_host_put;
2994 scsi_scan_host(sh);
2995 return 0;
2996
2997 fail_host_put:
2998 dev_err(&h->pdev->dev, "%s: scsi_add_host"
2999 " failed for controller %d\n", __func__, h->ctlr);
3000 scsi_host_put(sh);
3001 return error;
3002 fail:
3003 dev_err(&h->pdev->dev, "%s: scsi_host_alloc"
3004 " failed for controller %d\n", __func__, h->ctlr);
3005 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003006}
3007
3008static int wait_for_device_to_become_ready(struct ctlr_info *h,
3009 unsigned char lunaddr[])
3010{
3011 int rc = 0;
3012 int count = 0;
3013 int waittime = 1; /* seconds */
3014 struct CommandList *c;
3015
3016 c = cmd_special_alloc(h);
3017 if (!c) {
3018 dev_warn(&h->pdev->dev, "out of memory in "
3019 "wait_for_device_to_become_ready.\n");
3020 return IO_ERROR;
3021 }
3022
3023 /* Send test unit ready until device ready, or give up. */
3024 while (count < HPSA_TUR_RETRY_LIMIT) {
3025
3026 /* Wait for a bit. do this first, because if we send
3027 * the TUR right away, the reset will just abort it.
3028 */
3029 msleep(1000 * waittime);
3030 count++;
3031
3032 /* Increase wait time with each try, up to a point. */
3033 if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
3034 waittime = waittime * 2;
3035
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003036 /* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
3037 (void) fill_cmd(c, TEST_UNIT_READY, h,
3038 NULL, 0, 0, lunaddr, TYPE_CMD);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003039 hpsa_scsi_do_simple_cmd_core(h, c);
3040 /* no unmap needed here because no data xfer. */
3041
3042 if (c->err_info->CommandStatus == CMD_SUCCESS)
3043 break;
3044
3045 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
3046 c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
3047 (c->err_info->SenseInfo[2] == NO_SENSE ||
3048 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
3049 break;
3050
3051 dev_warn(&h->pdev->dev, "waiting %d secs "
3052 "for device to become ready.\n", waittime);
3053 rc = 1; /* device not ready. */
3054 }
3055
3056 if (rc)
3057 dev_warn(&h->pdev->dev, "giving up on device.\n");
3058 else
3059 dev_warn(&h->pdev->dev, "device is ready.\n");
3060
3061 cmd_special_free(h, c);
3062 return rc;
3063}
3064
3065/* Need at least one of these error handlers to keep ../scsi/hosts.c from
3066 * complaining. Doing a host- or bus-reset can't do anything good here.
3067 */
3068static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
3069{
3070 int rc;
3071 struct ctlr_info *h;
3072 struct hpsa_scsi_dev_t *dev;
3073
3074 /* find the controller to which the command to be aborted was sent */
3075 h = sdev_to_hba(scsicmd->device);
3076 if (h == NULL) /* paranoia */
3077 return FAILED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003078 dev = scsicmd->device->hostdata;
3079 if (!dev) {
3080 dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
3081 "device lookup failed.\n");
3082 return FAILED;
3083 }
Stephen M. Camerond416b0c2010-02-04 08:43:21 -06003084 dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
3085 h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003086 /* send a reset to the SCSI LUN which the command was sent to */
3087 rc = hpsa_send_reset(h, dev->scsi3addr);
3088 if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
3089 return SUCCESS;
3090
3091 dev_warn(&h->pdev->dev, "resetting device failed.\n");
3092 return FAILED;
3093}
3094
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003095static void swizzle_abort_tag(u8 *tag)
3096{
3097 u8 original_tag[8];
3098
3099 memcpy(original_tag, tag, 8);
3100 tag[0] = original_tag[3];
3101 tag[1] = original_tag[2];
3102 tag[2] = original_tag[1];
3103 tag[3] = original_tag[0];
3104 tag[4] = original_tag[7];
3105 tag[5] = original_tag[6];
3106 tag[6] = original_tag[5];
3107 tag[7] = original_tag[4];
3108}
3109
Scott Teel17eb87d2014-02-18 13:55:28 -06003110static void hpsa_get_tag(struct ctlr_info *h,
3111 struct CommandList *c, u32 *taglower, u32 *tagupper)
3112{
3113 if (c->cmd_type == CMD_IOACCEL1) {
3114 struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
3115 &h->ioaccel_cmd_pool[c->cmdindex];
3116 *tagupper = cm1->Tag.upper;
3117 *taglower = cm1->Tag.lower;
3118 } else {
3119 *tagupper = c->Header.Tag.upper;
3120 *taglower = c->Header.Tag.lower;
3121 }
3122}
3123
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003124static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003125 struct CommandList *abort, int swizzle)
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003126{
3127 int rc = IO_OK;
3128 struct CommandList *c;
3129 struct ErrorInfo *ei;
Scott Teel17eb87d2014-02-18 13:55:28 -06003130 u32 tagupper, taglower;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003131
3132 c = cmd_special_alloc(h);
3133 if (c == NULL) { /* trouble... */
3134 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
3135 return -ENOMEM;
3136 }
3137
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003138 /* fill_cmd can't fail here, no buffer to map */
3139 (void) fill_cmd(c, HPSA_ABORT_MSG, h, abort,
3140 0, 0, scsi3addr, TYPE_MSG);
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003141 if (swizzle)
3142 swizzle_abort_tag(&c->Request.CDB[4]);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003143 hpsa_scsi_do_simple_cmd_core(h, c);
Scott Teel17eb87d2014-02-18 13:55:28 -06003144 hpsa_get_tag(h, abort, &taglower, &tagupper);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003145 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd_core completed.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06003146 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003147 /* no unmap needed here because no data xfer. */
3148
3149 ei = c->err_info;
3150 switch (ei->CommandStatus) {
3151 case CMD_SUCCESS:
3152 break;
3153 case CMD_UNABORTABLE: /* Very common, don't make noise. */
3154 rc = -1;
3155 break;
3156 default:
3157 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06003158 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003159 hpsa_scsi_interpret_error(c);
3160 rc = -1;
3161 break;
3162 }
3163 cmd_special_free(h, c);
3164 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n", __func__,
3165 abort->Header.Tag.upper, abort->Header.Tag.lower);
3166 return rc;
3167}
3168
3169/*
3170 * hpsa_find_cmd_in_queue
3171 *
3172 * Used to determine whether a command (find) is still present
3173 * in queue_head. Optionally excludes the last element of queue_head.
3174 *
3175 * This is used to avoid unnecessary aborts. Commands in h->reqQ have
3176 * not yet been submitted, and so can be aborted by the driver without
3177 * sending an abort to the hardware.
3178 *
3179 * Returns pointer to command if found in queue, NULL otherwise.
3180 */
3181static struct CommandList *hpsa_find_cmd_in_queue(struct ctlr_info *h,
3182 struct scsi_cmnd *find, struct list_head *queue_head)
3183{
3184 unsigned long flags;
3185 struct CommandList *c = NULL; /* ptr into cmpQ */
3186
3187 if (!find)
3188 return 0;
3189 spin_lock_irqsave(&h->lock, flags);
3190 list_for_each_entry(c, queue_head, list) {
3191 if (c->scsi_cmd == NULL) /* e.g.: passthru ioctl */
3192 continue;
3193 if (c->scsi_cmd == find) {
3194 spin_unlock_irqrestore(&h->lock, flags);
3195 return c;
3196 }
3197 }
3198 spin_unlock_irqrestore(&h->lock, flags);
3199 return NULL;
3200}
3201
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003202static struct CommandList *hpsa_find_cmd_in_queue_by_tag(struct ctlr_info *h,
3203 u8 *tag, struct list_head *queue_head)
3204{
3205 unsigned long flags;
3206 struct CommandList *c;
3207
3208 spin_lock_irqsave(&h->lock, flags);
3209 list_for_each_entry(c, queue_head, list) {
3210 if (memcmp(&c->Header.Tag, tag, 8) != 0)
3211 continue;
3212 spin_unlock_irqrestore(&h->lock, flags);
3213 return c;
3214 }
3215 spin_unlock_irqrestore(&h->lock, flags);
3216 return NULL;
3217}
3218
3219/* Some Smart Arrays need the abort tag swizzled, and some don't. It's hard to
3220 * tell which kind we're dealing with, so we send the abort both ways. There
3221 * shouldn't be any collisions between swizzled and unswizzled tags due to the
3222 * way we construct our tags but we check anyway in case the assumptions which
3223 * make this true someday become false.
3224 */
3225static int hpsa_send_abort_both_ways(struct ctlr_info *h,
3226 unsigned char *scsi3addr, struct CommandList *abort)
3227{
3228 u8 swizzled_tag[8];
3229 struct CommandList *c;
3230 int rc = 0, rc2 = 0;
3231
3232 /* we do not expect to find the swizzled tag in our queue, but
3233 * check anyway just to be sure the assumptions which make this
3234 * the case haven't become wrong.
3235 */
3236 memcpy(swizzled_tag, &abort->Request.CDB[4], 8);
3237 swizzle_abort_tag(swizzled_tag);
3238 c = hpsa_find_cmd_in_queue_by_tag(h, swizzled_tag, &h->cmpQ);
3239 if (c != NULL) {
3240 dev_warn(&h->pdev->dev, "Unexpectedly found byte-swapped tag in completion queue.\n");
3241 return hpsa_send_abort(h, scsi3addr, abort, 0);
3242 }
3243 rc = hpsa_send_abort(h, scsi3addr, abort, 0);
3244
3245 /* if the command is still in our queue, we can't conclude that it was
3246 * aborted (it might have just completed normally) but in any case
3247 * we don't need to try to abort it another way.
3248 */
3249 c = hpsa_find_cmd_in_queue(h, abort->scsi_cmd, &h->cmpQ);
3250 if (c)
3251 rc2 = hpsa_send_abort(h, scsi3addr, abort, 1);
3252 return rc && rc2;
3253}
3254
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003255/* Send an abort for the specified command.
3256 * If the device and controller support it,
3257 * send a task abort request.
3258 */
3259static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
3260{
3261
3262 int i, rc;
3263 struct ctlr_info *h;
3264 struct hpsa_scsi_dev_t *dev;
3265 struct CommandList *abort; /* pointer to command to be aborted */
3266 struct CommandList *found;
3267 struct scsi_cmnd *as; /* ptr to scsi cmd inside aborted command. */
3268 char msg[256]; /* For debug messaging. */
3269 int ml = 0;
Scott Teel17eb87d2014-02-18 13:55:28 -06003270 u32 tagupper, taglower;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003271
3272 /* Find the controller of the command to be aborted */
3273 h = sdev_to_hba(sc->device);
3274 if (WARN(h == NULL,
3275 "ABORT REQUEST FAILED, Controller lookup failed.\n"))
3276 return FAILED;
3277
3278 /* Check that controller supports some kind of task abort */
3279 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
3280 !(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
3281 return FAILED;
3282
3283 memset(msg, 0, sizeof(msg));
3284 ml += sprintf(msg+ml, "ABORT REQUEST on C%d:B%d:T%d:L%d ",
3285 h->scsi_host->host_no, sc->device->channel,
3286 sc->device->id, sc->device->lun);
3287
3288 /* Find the device of the command to be aborted */
3289 dev = sc->device->hostdata;
3290 if (!dev) {
3291 dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
3292 msg);
3293 return FAILED;
3294 }
3295
3296 /* Get SCSI command to be aborted */
3297 abort = (struct CommandList *) sc->host_scribble;
3298 if (abort == NULL) {
3299 dev_err(&h->pdev->dev, "%s FAILED, Command to abort is NULL.\n",
3300 msg);
3301 return FAILED;
3302 }
Scott Teel17eb87d2014-02-18 13:55:28 -06003303 hpsa_get_tag(h, abort, &taglower, &tagupper);
3304 ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003305 as = (struct scsi_cmnd *) abort->scsi_cmd;
3306 if (as != NULL)
3307 ml += sprintf(msg+ml, "Command:0x%x SN:0x%lx ",
3308 as->cmnd[0], as->serial_number);
3309 dev_dbg(&h->pdev->dev, "%s\n", msg);
3310 dev_warn(&h->pdev->dev, "Abort request on C%d:B%d:T%d:L%d\n",
3311 h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
3312
3313 /* Search reqQ to See if command is queued but not submitted,
3314 * if so, complete the command with aborted status and remove
3315 * it from the reqQ.
3316 */
3317 found = hpsa_find_cmd_in_queue(h, sc, &h->reqQ);
3318 if (found) {
3319 found->err_info->CommandStatus = CMD_ABORTED;
3320 finish_cmd(found);
3321 dev_info(&h->pdev->dev, "%s Request SUCCEEDED (driver queue).\n",
3322 msg);
3323 return SUCCESS;
3324 }
3325
3326 /* not in reqQ, if also not in cmpQ, must have already completed */
3327 found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
3328 if (!found) {
Stephen M. Camerond6ebd0f2012-07-26 11:34:17 -05003329 dev_dbg(&h->pdev->dev, "%s Request SUCCEEDED (not known to driver).\n",
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003330 msg);
3331 return SUCCESS;
3332 }
3333
3334 /*
3335 * Command is in flight, or possibly already completed
3336 * by the firmware (but not to the scsi mid layer) but we can't
3337 * distinguish which. Send the abort down.
3338 */
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003339 rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003340 if (rc != 0) {
3341 dev_dbg(&h->pdev->dev, "%s Request FAILED.\n", msg);
3342 dev_warn(&h->pdev->dev, "FAILED abort on device C%d:B%d:T%d:L%d\n",
3343 h->scsi_host->host_no,
3344 dev->bus, dev->target, dev->lun);
3345 return FAILED;
3346 }
3347 dev_info(&h->pdev->dev, "%s REQUEST SUCCEEDED.\n", msg);
3348
3349 /* If the abort(s) above completed and actually aborted the
3350 * command, then the command to be aborted should already be
3351 * completed. If not, wait around a bit more to see if they
3352 * manage to complete normally.
3353 */
3354#define ABORT_COMPLETE_WAIT_SECS 30
3355 for (i = 0; i < ABORT_COMPLETE_WAIT_SECS * 10; i++) {
3356 found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
3357 if (!found)
3358 return SUCCESS;
3359 msleep(100);
3360 }
3361 dev_warn(&h->pdev->dev, "%s FAILED. Aborted command has not completed after %d seconds.\n",
3362 msg, ABORT_COMPLETE_WAIT_SECS);
3363 return FAILED;
3364}
3365
3366
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003367/*
3368 * For operations that cannot sleep, a command block is allocated at init,
3369 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
3370 * which ones are free or in use. Lock must be held when calling this.
3371 * cmd_free() is the complement.
3372 */
3373static struct CommandList *cmd_alloc(struct ctlr_info *h)
3374{
3375 struct CommandList *c;
3376 int i;
3377 union u64bit temp64;
3378 dma_addr_t cmd_dma_handle, err_dma_handle;
Matt Gatese16a33a2012-05-01 11:43:11 -05003379 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003380
Matt Gatese16a33a2012-05-01 11:43:11 -05003381 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003382 do {
3383 i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
Matt Gatese16a33a2012-05-01 11:43:11 -05003384 if (i == h->nr_cmds) {
3385 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003386 return NULL;
Matt Gatese16a33a2012-05-01 11:43:11 -05003387 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003388 } while (test_and_set_bit
3389 (i & (BITS_PER_LONG - 1),
3390 h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
Matt Gatese16a33a2012-05-01 11:43:11 -05003391 spin_unlock_irqrestore(&h->lock, flags);
3392
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003393 c = h->cmd_pool + i;
3394 memset(c, 0, sizeof(*c));
3395 cmd_dma_handle = h->cmd_pool_dhandle
3396 + i * sizeof(*c);
3397 c->err_info = h->errinfo_pool + i;
3398 memset(c->err_info, 0, sizeof(*c->err_info));
3399 err_dma_handle = h->errinfo_pool_dhandle
3400 + i * sizeof(*c->err_info);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003401
3402 c->cmdindex = i;
3403
Stephen M. Cameron9e0fc7642011-02-15 15:32:48 -06003404 INIT_LIST_HEAD(&c->list);
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06003405 c->busaddr = (u32) cmd_dma_handle;
3406 temp64.val = (u64) err_dma_handle;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003407 c->ErrDesc.Addr.lower = temp64.val32.lower;
3408 c->ErrDesc.Addr.upper = temp64.val32.upper;
3409 c->ErrDesc.Len = sizeof(*c->err_info);
3410
3411 c->h = h;
3412 return c;
3413}
3414
3415/* For operations that can wait for kmalloc to possibly sleep,
3416 * this routine can be called. Lock need not be held to call
3417 * cmd_special_alloc. cmd_special_free() is the complement.
3418 */
3419static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
3420{
3421 struct CommandList *c;
3422 union u64bit temp64;
3423 dma_addr_t cmd_dma_handle, err_dma_handle;
3424
3425 c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
3426 if (c == NULL)
3427 return NULL;
3428 memset(c, 0, sizeof(*c));
3429
Matt Gatese1f7de02014-02-18 13:55:17 -06003430 c->cmd_type = CMD_SCSI;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003431 c->cmdindex = -1;
3432
3433 c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info),
3434 &err_dma_handle);
3435
3436 if (c->err_info == NULL) {
3437 pci_free_consistent(h->pdev,
3438 sizeof(*c), c, cmd_dma_handle);
3439 return NULL;
3440 }
3441 memset(c->err_info, 0, sizeof(*c->err_info));
3442
Stephen M. Cameron9e0fc7642011-02-15 15:32:48 -06003443 INIT_LIST_HEAD(&c->list);
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06003444 c->busaddr = (u32) cmd_dma_handle;
3445 temp64.val = (u64) err_dma_handle;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003446 c->ErrDesc.Addr.lower = temp64.val32.lower;
3447 c->ErrDesc.Addr.upper = temp64.val32.upper;
3448 c->ErrDesc.Len = sizeof(*c->err_info);
3449
3450 c->h = h;
3451 return c;
3452}
3453
3454static void cmd_free(struct ctlr_info *h, struct CommandList *c)
3455{
3456 int i;
Matt Gatese16a33a2012-05-01 11:43:11 -05003457 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003458
3459 i = c - h->cmd_pool;
Matt Gatese16a33a2012-05-01 11:43:11 -05003460 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003461 clear_bit(i & (BITS_PER_LONG - 1),
3462 h->cmd_pool_bits + (i / BITS_PER_LONG));
Matt Gatese16a33a2012-05-01 11:43:11 -05003463 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003464}
3465
3466static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
3467{
3468 union u64bit temp64;
3469
3470 temp64.val32.lower = c->ErrDesc.Addr.lower;
3471 temp64.val32.upper = c->ErrDesc.Addr.upper;
3472 pci_free_consistent(h->pdev, sizeof(*c->err_info),
3473 c->err_info, (dma_addr_t) temp64.val);
3474 pci_free_consistent(h->pdev, sizeof(*c),
Stephen M. Camerond896f3f2011-01-06 14:47:53 -06003475 c, (dma_addr_t) (c->busaddr & DIRECT_LOOKUP_MASK));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003476}
3477
3478#ifdef CONFIG_COMPAT
3479
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003480static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
3481{
3482 IOCTL32_Command_struct __user *arg32 =
3483 (IOCTL32_Command_struct __user *) arg;
3484 IOCTL_Command_struct arg64;
3485 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
3486 int err;
3487 u32 cp;
3488
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06003489 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003490 err = 0;
3491 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
3492 sizeof(arg64.LUN_info));
3493 err |= copy_from_user(&arg64.Request, &arg32->Request,
3494 sizeof(arg64.Request));
3495 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
3496 sizeof(arg64.error_info));
3497 err |= get_user(arg64.buf_size, &arg32->buf_size);
3498 err |= get_user(cp, &arg32->buf);
3499 arg64.buf = compat_ptr(cp);
3500 err |= copy_to_user(p, &arg64, sizeof(arg64));
3501
3502 if (err)
3503 return -EFAULT;
3504
Stephen M. Camerone39eeae2010-02-04 08:43:46 -06003505 err = hpsa_ioctl(dev, CCISS_PASSTHRU, (void *)p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003506 if (err)
3507 return err;
3508 err |= copy_in_user(&arg32->error_info, &p->error_info,
3509 sizeof(arg32->error_info));
3510 if (err)
3511 return -EFAULT;
3512 return err;
3513}
3514
3515static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
3516 int cmd, void *arg)
3517{
3518 BIG_IOCTL32_Command_struct __user *arg32 =
3519 (BIG_IOCTL32_Command_struct __user *) arg;
3520 BIG_IOCTL_Command_struct arg64;
3521 BIG_IOCTL_Command_struct __user *p =
3522 compat_alloc_user_space(sizeof(arg64));
3523 int err;
3524 u32 cp;
3525
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06003526 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003527 err = 0;
3528 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
3529 sizeof(arg64.LUN_info));
3530 err |= copy_from_user(&arg64.Request, &arg32->Request,
3531 sizeof(arg64.Request));
3532 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
3533 sizeof(arg64.error_info));
3534 err |= get_user(arg64.buf_size, &arg32->buf_size);
3535 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
3536 err |= get_user(cp, &arg32->buf);
3537 arg64.buf = compat_ptr(cp);
3538 err |= copy_to_user(p, &arg64, sizeof(arg64));
3539
3540 if (err)
3541 return -EFAULT;
3542
Stephen M. Camerone39eeae2010-02-04 08:43:46 -06003543 err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003544 if (err)
3545 return err;
3546 err |= copy_in_user(&arg32->error_info, &p->error_info,
3547 sizeof(arg32->error_info));
3548 if (err)
3549 return -EFAULT;
3550 return err;
3551}
Stephen M. Cameron71fe75a2010-02-04 08:43:51 -06003552
3553static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
3554{
3555 switch (cmd) {
3556 case CCISS_GETPCIINFO:
3557 case CCISS_GETINTINFO:
3558 case CCISS_SETINTINFO:
3559 case CCISS_GETNODENAME:
3560 case CCISS_SETNODENAME:
3561 case CCISS_GETHEARTBEAT:
3562 case CCISS_GETBUSTYPES:
3563 case CCISS_GETFIRMVER:
3564 case CCISS_GETDRIVVER:
3565 case CCISS_REVALIDVOLS:
3566 case CCISS_DEREGDISK:
3567 case CCISS_REGNEWDISK:
3568 case CCISS_REGNEWD:
3569 case CCISS_RESCANDISK:
3570 case CCISS_GETLUNINFO:
3571 return hpsa_ioctl(dev, cmd, arg);
3572
3573 case CCISS_PASSTHRU32:
3574 return hpsa_ioctl32_passthru(dev, cmd, arg);
3575 case CCISS_BIG_PASSTHRU32:
3576 return hpsa_ioctl32_big_passthru(dev, cmd, arg);
3577
3578 default:
3579 return -ENOIOCTLCMD;
3580 }
3581}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003582#endif
3583
3584static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
3585{
3586 struct hpsa_pci_info pciinfo;
3587
3588 if (!argp)
3589 return -EINVAL;
3590 pciinfo.domain = pci_domain_nr(h->pdev->bus);
3591 pciinfo.bus = h->pdev->bus->number;
3592 pciinfo.dev_fn = h->pdev->devfn;
3593 pciinfo.board_id = h->board_id;
3594 if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
3595 return -EFAULT;
3596 return 0;
3597}
3598
3599static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
3600{
3601 DriverVer_type DriverVer;
3602 unsigned char vmaj, vmin, vsubmin;
3603 int rc;
3604
3605 rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
3606 &vmaj, &vmin, &vsubmin);
3607 if (rc != 3) {
3608 dev_info(&h->pdev->dev, "driver version string '%s' "
3609 "unrecognized.", HPSA_DRIVER_VERSION);
3610 vmaj = 0;
3611 vmin = 0;
3612 vsubmin = 0;
3613 }
3614 DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
3615 if (!argp)
3616 return -EINVAL;
3617 if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
3618 return -EFAULT;
3619 return 0;
3620}
3621
3622static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
3623{
3624 IOCTL_Command_struct iocommand;
3625 struct CommandList *c;
3626 char *buff = NULL;
3627 union u64bit temp64;
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003628 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003629
3630 if (!argp)
3631 return -EINVAL;
3632 if (!capable(CAP_SYS_RAWIO))
3633 return -EPERM;
3634 if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
3635 return -EFAULT;
3636 if ((iocommand.buf_size < 1) &&
3637 (iocommand.Request.Type.Direction != XFER_NONE)) {
3638 return -EINVAL;
3639 }
3640 if (iocommand.buf_size > 0) {
3641 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
3642 if (buff == NULL)
3643 return -EFAULT;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003644 if (iocommand.Request.Type.Direction == XFER_WRITE) {
3645 /* Copy the data into the buffer we created */
3646 if (copy_from_user(buff, iocommand.buf,
3647 iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003648 rc = -EFAULT;
3649 goto out_kfree;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003650 }
3651 } else {
3652 memset(buff, 0, iocommand.buf_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003653 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003654 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003655 c = cmd_special_alloc(h);
3656 if (c == NULL) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003657 rc = -ENOMEM;
3658 goto out_kfree;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003659 }
3660 /* Fill in the command type */
3661 c->cmd_type = CMD_IOCTL_PEND;
3662 /* Fill in Command Header */
3663 c->Header.ReplyQueue = 0; /* unused in simple mode */
3664 if (iocommand.buf_size > 0) { /* buffer to fill */
3665 c->Header.SGList = 1;
3666 c->Header.SGTotal = 1;
3667 } else { /* no buffers to fill */
3668 c->Header.SGList = 0;
3669 c->Header.SGTotal = 0;
3670 }
3671 memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
3672 /* use the kernel address the cmd block for tag */
3673 c->Header.Tag.lower = c->busaddr;
3674
3675 /* Fill in Request block */
3676 memcpy(&c->Request, &iocommand.Request,
3677 sizeof(c->Request));
3678
3679 /* Fill in the scatter gather information */
3680 if (iocommand.buf_size > 0) {
3681 temp64.val = pci_map_single(h->pdev, buff,
3682 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06003683 if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
3684 c->SG[0].Addr.lower = 0;
3685 c->SG[0].Addr.upper = 0;
3686 c->SG[0].Len = 0;
3687 rc = -ENOMEM;
3688 goto out;
3689 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003690 c->SG[0].Addr.lower = temp64.val32.lower;
3691 c->SG[0].Addr.upper = temp64.val32.upper;
3692 c->SG[0].Len = iocommand.buf_size;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06003693 c->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining*/
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003694 }
Stephen M. Camerona0c12412011-10-26 16:22:04 -05003695 hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
Stephen M. Cameronc2dd32e2011-06-03 09:57:29 -05003696 if (iocommand.buf_size > 0)
3697 hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003698 check_ioctl_unit_attention(h, c);
3699
3700 /* Copy the error information out */
3701 memcpy(&iocommand.error_info, c->err_info,
3702 sizeof(iocommand.error_info));
3703 if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003704 rc = -EFAULT;
3705 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003706 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003707 if (iocommand.Request.Type.Direction == XFER_READ &&
3708 iocommand.buf_size > 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003709 /* Copy the data out of the buffer we created */
3710 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003711 rc = -EFAULT;
3712 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003713 }
3714 }
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003715out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003716 cmd_special_free(h, c);
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003717out_kfree:
3718 kfree(buff);
3719 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003720}
3721
3722static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
3723{
3724 BIG_IOCTL_Command_struct *ioc;
3725 struct CommandList *c;
3726 unsigned char **buff = NULL;
3727 int *buff_size = NULL;
3728 union u64bit temp64;
3729 BYTE sg_used = 0;
3730 int status = 0;
3731 int i;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06003732 u32 left;
3733 u32 sz;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003734 BYTE __user *data_ptr;
3735
3736 if (!argp)
3737 return -EINVAL;
3738 if (!capable(CAP_SYS_RAWIO))
3739 return -EPERM;
3740 ioc = (BIG_IOCTL_Command_struct *)
3741 kmalloc(sizeof(*ioc), GFP_KERNEL);
3742 if (!ioc) {
3743 status = -ENOMEM;
3744 goto cleanup1;
3745 }
3746 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
3747 status = -EFAULT;
3748 goto cleanup1;
3749 }
3750 if ((ioc->buf_size < 1) &&
3751 (ioc->Request.Type.Direction != XFER_NONE)) {
3752 status = -EINVAL;
3753 goto cleanup1;
3754 }
3755 /* Check kmalloc limits using all SGs */
3756 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
3757 status = -EINVAL;
3758 goto cleanup1;
3759 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06003760 if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003761 status = -EINVAL;
3762 goto cleanup1;
3763 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06003764 buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003765 if (!buff) {
3766 status = -ENOMEM;
3767 goto cleanup1;
3768 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06003769 buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003770 if (!buff_size) {
3771 status = -ENOMEM;
3772 goto cleanup1;
3773 }
3774 left = ioc->buf_size;
3775 data_ptr = ioc->buf;
3776 while (left) {
3777 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
3778 buff_size[sg_used] = sz;
3779 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
3780 if (buff[sg_used] == NULL) {
3781 status = -ENOMEM;
3782 goto cleanup1;
3783 }
3784 if (ioc->Request.Type.Direction == XFER_WRITE) {
3785 if (copy_from_user(buff[sg_used], data_ptr, sz)) {
3786 status = -ENOMEM;
3787 goto cleanup1;
3788 }
3789 } else
3790 memset(buff[sg_used], 0, sz);
3791 left -= sz;
3792 data_ptr += sz;
3793 sg_used++;
3794 }
3795 c = cmd_special_alloc(h);
3796 if (c == NULL) {
3797 status = -ENOMEM;
3798 goto cleanup1;
3799 }
3800 c->cmd_type = CMD_IOCTL_PEND;
3801 c->Header.ReplyQueue = 0;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003802 c->Header.SGList = c->Header.SGTotal = sg_used;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003803 memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
3804 c->Header.Tag.lower = c->busaddr;
3805 memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
3806 if (ioc->buf_size > 0) {
3807 int i;
3808 for (i = 0; i < sg_used; i++) {
3809 temp64.val = pci_map_single(h->pdev, buff[i],
3810 buff_size[i], PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06003811 if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
3812 c->SG[i].Addr.lower = 0;
3813 c->SG[i].Addr.upper = 0;
3814 c->SG[i].Len = 0;
3815 hpsa_pci_unmap(h->pdev, c, i,
3816 PCI_DMA_BIDIRECTIONAL);
3817 status = -ENOMEM;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05003818 goto cleanup0;
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06003819 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003820 c->SG[i].Addr.lower = temp64.val32.lower;
3821 c->SG[i].Addr.upper = temp64.val32.upper;
3822 c->SG[i].Len = buff_size[i];
Matt Gatese1d9cbf2014-02-18 13:55:12 -06003823 c->SG[i].Ext = i < sg_used - 1 ? 0 : HPSA_SG_LAST;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003824 }
3825 }
Stephen M. Camerona0c12412011-10-26 16:22:04 -05003826 hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003827 if (sg_used)
3828 hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003829 check_ioctl_unit_attention(h, c);
3830 /* Copy the error information out */
3831 memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
3832 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003833 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05003834 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003835 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003836 if (ioc->Request.Type.Direction == XFER_READ && ioc->buf_size > 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003837 /* Copy the data out of the buffer we created */
3838 BYTE __user *ptr = ioc->buf;
3839 for (i = 0; i < sg_used; i++) {
3840 if (copy_to_user(ptr, buff[i], buff_size[i])) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003841 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05003842 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003843 }
3844 ptr += buff_size[i];
3845 }
3846 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003847 status = 0;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05003848cleanup0:
3849 cmd_special_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003850cleanup1:
3851 if (buff) {
3852 for (i = 0; i < sg_used; i++)
3853 kfree(buff[i]);
3854 kfree(buff);
3855 }
3856 kfree(buff_size);
3857 kfree(ioc);
3858 return status;
3859}
3860
3861static void check_ioctl_unit_attention(struct ctlr_info *h,
3862 struct CommandList *c)
3863{
3864 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
3865 c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
3866 (void) check_for_unit_attention(h, c);
3867}
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05003868
3869static int increment_passthru_count(struct ctlr_info *h)
3870{
3871 unsigned long flags;
3872
3873 spin_lock_irqsave(&h->passthru_count_lock, flags);
3874 if (h->passthru_count >= HPSA_MAX_CONCURRENT_PASSTHRUS) {
3875 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
3876 return -1;
3877 }
3878 h->passthru_count++;
3879 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
3880 return 0;
3881}
3882
3883static void decrement_passthru_count(struct ctlr_info *h)
3884{
3885 unsigned long flags;
3886
3887 spin_lock_irqsave(&h->passthru_count_lock, flags);
3888 if (h->passthru_count <= 0) {
3889 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
3890 /* not expecting to get here. */
3891 dev_warn(&h->pdev->dev, "Bug detected, passthru_count seems to be incorrect.\n");
3892 return;
3893 }
3894 h->passthru_count--;
3895 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
3896}
3897
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003898/*
3899 * ioctl
3900 */
3901static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
3902{
3903 struct ctlr_info *h;
3904 void __user *argp = (void __user *)arg;
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05003905 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003906
3907 h = sdev_to_hba(dev);
3908
3909 switch (cmd) {
3910 case CCISS_DEREGDISK:
3911 case CCISS_REGNEWDISK:
3912 case CCISS_REGNEWD:
Stephen M. Camerona08a8472010-02-04 08:43:16 -06003913 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003914 return 0;
3915 case CCISS_GETPCIINFO:
3916 return hpsa_getpciinfo_ioctl(h, argp);
3917 case CCISS_GETDRIVVER:
3918 return hpsa_getdrivver_ioctl(h, argp);
3919 case CCISS_PASSTHRU:
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05003920 if (increment_passthru_count(h))
3921 return -EAGAIN;
3922 rc = hpsa_passthru_ioctl(h, argp);
3923 decrement_passthru_count(h);
3924 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003925 case CCISS_BIG_PASSTHRU:
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05003926 if (increment_passthru_count(h))
3927 return -EAGAIN;
3928 rc = hpsa_big_passthru_ioctl(h, argp);
3929 decrement_passthru_count(h);
3930 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003931 default:
3932 return -ENOTTY;
3933 }
3934}
3935
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08003936static int hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
3937 u8 reset_type)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05003938{
3939 struct CommandList *c;
3940
3941 c = cmd_alloc(h);
3942 if (!c)
3943 return -ENOMEM;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003944 /* fill_cmd can't fail here, no data buffer to map */
3945 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05003946 RAID_CTLR_LUNID, TYPE_MSG);
3947 c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
3948 c->waiting = NULL;
3949 enqueue_cmd_and_start_io(h, c);
3950 /* Don't wait for completion, the reset won't complete. Don't free
3951 * the command either. This is the last command we will send before
3952 * re-initializing everything, so it doesn't matter and won't leak.
3953 */
3954 return 0;
3955}
3956
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003957static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06003958 void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003959 int cmd_type)
3960{
3961 int pci_dir = XFER_NONE;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003962 struct CommandList *a; /* for commands to be aborted */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003963
3964 c->cmd_type = CMD_IOCTL_PEND;
3965 c->Header.ReplyQueue = 0;
3966 if (buff != NULL && size > 0) {
3967 c->Header.SGList = 1;
3968 c->Header.SGTotal = 1;
3969 } else {
3970 c->Header.SGList = 0;
3971 c->Header.SGTotal = 0;
3972 }
3973 c->Header.Tag.lower = c->busaddr;
3974 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
3975
3976 c->Request.Type.Type = cmd_type;
3977 if (cmd_type == TYPE_CMD) {
3978 switch (cmd) {
3979 case HPSA_INQUIRY:
3980 /* are we trying to read a vital product page */
3981 if (page_code != 0) {
3982 c->Request.CDB[1] = 0x01;
3983 c->Request.CDB[2] = page_code;
3984 }
3985 c->Request.CDBLen = 6;
3986 c->Request.Type.Attribute = ATTR_SIMPLE;
3987 c->Request.Type.Direction = XFER_READ;
3988 c->Request.Timeout = 0;
3989 c->Request.CDB[0] = HPSA_INQUIRY;
3990 c->Request.CDB[4] = size & 0xFF;
3991 break;
3992 case HPSA_REPORT_LOG:
3993 case HPSA_REPORT_PHYS:
3994 /* Talking to controller so It's a physical command
3995 mode = 00 target = 0. Nothing to write.
3996 */
3997 c->Request.CDBLen = 12;
3998 c->Request.Type.Attribute = ATTR_SIMPLE;
3999 c->Request.Type.Direction = XFER_READ;
4000 c->Request.Timeout = 0;
4001 c->Request.CDB[0] = cmd;
4002 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
4003 c->Request.CDB[7] = (size >> 16) & 0xFF;
4004 c->Request.CDB[8] = (size >> 8) & 0xFF;
4005 c->Request.CDB[9] = size & 0xFF;
4006 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004007 case HPSA_CACHE_FLUSH:
4008 c->Request.CDBLen = 12;
4009 c->Request.Type.Attribute = ATTR_SIMPLE;
4010 c->Request.Type.Direction = XFER_WRITE;
4011 c->Request.Timeout = 0;
4012 c->Request.CDB[0] = BMIC_WRITE;
4013 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
Stephen M. Cameronbb158ea2011-10-26 16:21:17 -05004014 c->Request.CDB[7] = (size >> 8) & 0xFF;
4015 c->Request.CDB[8] = size & 0xFF;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004016 break;
4017 case TEST_UNIT_READY:
4018 c->Request.CDBLen = 6;
4019 c->Request.Type.Attribute = ATTR_SIMPLE;
4020 c->Request.Type.Direction = XFER_NONE;
4021 c->Request.Timeout = 0;
4022 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004023 case HPSA_GET_RAID_MAP:
4024 c->Request.CDBLen = 12;
4025 c->Request.Type.Attribute = ATTR_SIMPLE;
4026 c->Request.Type.Direction = XFER_READ;
4027 c->Request.Timeout = 0;
4028 c->Request.CDB[0] = HPSA_CISS_READ;
4029 c->Request.CDB[1] = cmd;
4030 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
4031 c->Request.CDB[7] = (size >> 16) & 0xFF;
4032 c->Request.CDB[8] = (size >> 8) & 0xFF;
4033 c->Request.CDB[9] = size & 0xFF;
4034 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004035 default:
4036 dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
4037 BUG();
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004038 return -1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004039 }
4040 } else if (cmd_type == TYPE_MSG) {
4041 switch (cmd) {
4042
4043 case HPSA_DEVICE_RESET_MSG:
4044 c->Request.CDBLen = 16;
4045 c->Request.Type.Type = 1; /* It is a MSG not a CMD */
4046 c->Request.Type.Attribute = ATTR_SIMPLE;
4047 c->Request.Type.Direction = XFER_NONE;
4048 c->Request.Timeout = 0; /* Don't time out */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004049 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
4050 c->Request.CDB[0] = cmd;
Stephen M. Cameron21e89af2012-07-26 11:34:10 -05004051 c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004052 /* If bytes 4-7 are zero, it means reset the */
4053 /* LunID device */
4054 c->Request.CDB[4] = 0x00;
4055 c->Request.CDB[5] = 0x00;
4056 c->Request.CDB[6] = 0x00;
4057 c->Request.CDB[7] = 0x00;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004058 break;
4059 case HPSA_ABORT_MSG:
4060 a = buff; /* point to command to be aborted */
4061 dev_dbg(&h->pdev->dev, "Abort Tag:0x%08x:%08x using request Tag:0x%08x:%08x\n",
4062 a->Header.Tag.upper, a->Header.Tag.lower,
4063 c->Header.Tag.upper, c->Header.Tag.lower);
4064 c->Request.CDBLen = 16;
4065 c->Request.Type.Type = TYPE_MSG;
4066 c->Request.Type.Attribute = ATTR_SIMPLE;
4067 c->Request.Type.Direction = XFER_WRITE;
4068 c->Request.Timeout = 0; /* Don't time out */
4069 c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
4070 c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
4071 c->Request.CDB[2] = 0x00; /* reserved */
4072 c->Request.CDB[3] = 0x00; /* reserved */
4073 /* Tag to abort goes in CDB[4]-CDB[11] */
4074 c->Request.CDB[4] = a->Header.Tag.lower & 0xFF;
4075 c->Request.CDB[5] = (a->Header.Tag.lower >> 8) & 0xFF;
4076 c->Request.CDB[6] = (a->Header.Tag.lower >> 16) & 0xFF;
4077 c->Request.CDB[7] = (a->Header.Tag.lower >> 24) & 0xFF;
4078 c->Request.CDB[8] = a->Header.Tag.upper & 0xFF;
4079 c->Request.CDB[9] = (a->Header.Tag.upper >> 8) & 0xFF;
4080 c->Request.CDB[10] = (a->Header.Tag.upper >> 16) & 0xFF;
4081 c->Request.CDB[11] = (a->Header.Tag.upper >> 24) & 0xFF;
4082 c->Request.CDB[12] = 0x00; /* reserved */
4083 c->Request.CDB[13] = 0x00; /* reserved */
4084 c->Request.CDB[14] = 0x00; /* reserved */
4085 c->Request.CDB[15] = 0x00; /* reserved */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004086 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004087 default:
4088 dev_warn(&h->pdev->dev, "unknown message type %d\n",
4089 cmd);
4090 BUG();
4091 }
4092 } else {
4093 dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
4094 BUG();
4095 }
4096
4097 switch (c->Request.Type.Direction) {
4098 case XFER_READ:
4099 pci_dir = PCI_DMA_FROMDEVICE;
4100 break;
4101 case XFER_WRITE:
4102 pci_dir = PCI_DMA_TODEVICE;
4103 break;
4104 case XFER_NONE:
4105 pci_dir = PCI_DMA_NONE;
4106 break;
4107 default:
4108 pci_dir = PCI_DMA_BIDIRECTIONAL;
4109 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004110 if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
4111 return -1;
4112 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004113}
4114
4115/*
4116 * Map (physical) PCI mem into (virtual) kernel space
4117 */
4118static void __iomem *remap_pci_mem(ulong base, ulong size)
4119{
4120 ulong page_base = ((ulong) base) & PAGE_MASK;
4121 ulong page_offs = ((ulong) base) - page_base;
Stephen M. Cameron088ba342012-07-26 11:34:23 -05004122 void __iomem *page_remapped = ioremap_nocache(page_base,
4123 page_offs + size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004124
4125 return page_remapped ? (page_remapped + page_offs) : NULL;
4126}
4127
4128/* Takes cmds off the submission queue and sends them to the hardware,
4129 * then puts them on the queue of cmds waiting for completion.
4130 */
4131static void start_io(struct ctlr_info *h)
4132{
4133 struct CommandList *c;
Matt Gatese16a33a2012-05-01 11:43:11 -05004134 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004135
Matt Gatese16a33a2012-05-01 11:43:11 -05004136 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron9e0fc7642011-02-15 15:32:48 -06004137 while (!list_empty(&h->reqQ)) {
4138 c = list_entry(h->reqQ.next, struct CommandList, list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004139 /* can't do anything if fifo is full */
4140 if ((h->access.fifo_full(h))) {
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004141 h->fifo_recently_full = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004142 dev_warn(&h->pdev->dev, "fifo full\n");
4143 break;
4144 }
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004145 h->fifo_recently_full = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004146
4147 /* Get the first entry from the Request Q */
4148 removeQ(c);
4149 h->Qdepth--;
4150
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004151 /* Put job onto the completed Q */
4152 addQ(&h->cmpQ, c);
Matt Gatese16a33a2012-05-01 11:43:11 -05004153
4154 /* Must increment commands_outstanding before unlocking
4155 * and submitting to avoid race checking for fifo full
4156 * condition.
4157 */
4158 h->commands_outstanding++;
4159 if (h->commands_outstanding > h->max_outstanding)
4160 h->max_outstanding = h->commands_outstanding;
4161
4162 /* Tell the controller execute command */
4163 spin_unlock_irqrestore(&h->lock, flags);
4164 h->access.submit_command(h, c);
4165 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004166 }
Matt Gatese16a33a2012-05-01 11:43:11 -05004167 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004168}
4169
Matt Gates254f7962012-05-01 11:43:06 -05004170static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004171{
Matt Gates254f7962012-05-01 11:43:06 -05004172 return h->access.command_completed(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004173}
4174
Stephen M. Cameron900c5442010-02-04 08:42:35 -06004175static inline bool interrupt_pending(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004176{
4177 return h->access.intr_pending(h);
4178}
4179
4180static inline long interrupt_not_for_us(struct ctlr_info *h)
4181{
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004182 return (h->access.intr_pending(h) == 0) ||
4183 (h->interrupts_enabled == 0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004184}
4185
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004186static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
4187 u32 raw_tag)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004188{
4189 if (unlikely(tag_index >= h->nr_cmds)) {
4190 dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
4191 return 1;
4192 }
4193 return 0;
4194}
4195
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05004196static inline void finish_cmd(struct CommandList *c)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004197{
Matt Gatese16a33a2012-05-01 11:43:11 -05004198 unsigned long flags;
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004199 int io_may_be_stalled = 0;
4200 struct ctlr_info *h = c->h;
Matt Gatese16a33a2012-05-01 11:43:11 -05004201
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004202 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004203 removeQ(c);
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004204
4205 /*
4206 * Check for possibly stalled i/o.
4207 *
4208 * If a fifo_full condition is encountered, requests will back up
4209 * in h->reqQ. This queue is only emptied out by start_io which is
4210 * only called when a new i/o request comes in. If no i/o's are
4211 * forthcoming, the i/o's in h->reqQ can get stuck. So we call
4212 * start_io from here if we detect such a danger.
4213 *
4214 * Normally, we shouldn't hit this case, but pounding on the
4215 * CCISS_PASSTHRU ioctl can provoke it. Only call start_io if
4216 * commands_outstanding is low. We want to avoid calling
4217 * start_io from in here as much as possible, and esp. don't
4218 * want to get in a cycle where we call start_io every time
4219 * through here.
4220 */
4221 if (unlikely(h->fifo_recently_full) &&
4222 h->commands_outstanding < 5)
4223 io_may_be_stalled = 1;
4224
4225 spin_unlock_irqrestore(&h->lock, flags);
4226
Stephen M. Camerone85c5972012-05-01 11:43:42 -05004227 dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
Matt Gatese1f7de02014-02-18 13:55:17 -06004228 if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI))
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05004229 complete_scsi_command(c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004230 else if (c->cmd_type == CMD_IOCTL_PEND)
4231 complete(c->waiting);
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004232 if (unlikely(io_may_be_stalled))
4233 start_io(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004234}
4235
Stephen M. Camerona104c992010-02-04 08:42:24 -06004236static inline u32 hpsa_tag_contains_index(u32 tag)
4237{
Stephen M. Camerona104c992010-02-04 08:42:24 -06004238 return tag & DIRECT_LOOKUP_BIT;
4239}
4240
4241static inline u32 hpsa_tag_to_index(u32 tag)
4242{
Stephen M. Camerona104c992010-02-04 08:42:24 -06004243 return tag >> DIRECT_LOOKUP_SHIFT;
4244}
4245
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004246
4247static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
Stephen M. Camerona104c992010-02-04 08:42:24 -06004248{
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004249#define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
4250#define HPSA_SIMPLE_ERROR_BITS 0x03
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06004251 if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004252 return tag & ~HPSA_SIMPLE_ERROR_BITS;
4253 return tag & ~HPSA_PERF_ERROR_BITS;
Stephen M. Camerona104c992010-02-04 08:42:24 -06004254}
4255
Don Brace303932f2010-02-04 08:42:40 -06004256/* process completion of an indexed ("direct lookup") command */
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004257static inline void process_indexed_cmd(struct ctlr_info *h,
Don Brace303932f2010-02-04 08:42:40 -06004258 u32 raw_tag)
4259{
4260 u32 tag_index;
4261 struct CommandList *c;
4262
4263 tag_index = hpsa_tag_to_index(raw_tag);
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004264 if (!bad_tag(h, tag_index, raw_tag)) {
4265 c = h->cmd_pool + tag_index;
4266 finish_cmd(c);
4267 }
Don Brace303932f2010-02-04 08:42:40 -06004268}
4269
4270/* process completion of a non-indexed command */
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004271static inline void process_nonindexed_cmd(struct ctlr_info *h,
Don Brace303932f2010-02-04 08:42:40 -06004272 u32 raw_tag)
4273{
4274 u32 tag;
4275 struct CommandList *c = NULL;
Matt Gatese16a33a2012-05-01 11:43:11 -05004276 unsigned long flags;
Don Brace303932f2010-02-04 08:42:40 -06004277
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004278 tag = hpsa_tag_discard_error_bits(h, raw_tag);
Matt Gatese16a33a2012-05-01 11:43:11 -05004279 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron9e0fc7642011-02-15 15:32:48 -06004280 list_for_each_entry(c, &h->cmpQ, list) {
Don Brace303932f2010-02-04 08:42:40 -06004281 if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
Matt Gatese16a33a2012-05-01 11:43:11 -05004282 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05004283 finish_cmd(c);
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004284 return;
Don Brace303932f2010-02-04 08:42:40 -06004285 }
4286 }
Matt Gatese16a33a2012-05-01 11:43:11 -05004287 spin_unlock_irqrestore(&h->lock, flags);
Don Brace303932f2010-02-04 08:42:40 -06004288 bad_tag(h, h->nr_cmds + 1, raw_tag);
Don Brace303932f2010-02-04 08:42:40 -06004289}
4290
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004291/* Some controllers, like p400, will give us one interrupt
4292 * after a soft reset, even if we turned interrupts off.
4293 * Only need to check for this in the hpsa_xxx_discard_completions
4294 * functions.
4295 */
4296static int ignore_bogus_interrupt(struct ctlr_info *h)
4297{
4298 if (likely(!reset_devices))
4299 return 0;
4300
4301 if (likely(h->interrupts_enabled))
4302 return 0;
4303
4304 dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
4305 "(known firmware bug.) Ignoring.\n");
4306
4307 return 1;
4308}
4309
Matt Gates254f7962012-05-01 11:43:06 -05004310/*
4311 * Convert &h->q[x] (passed to interrupt handlers) back to h.
4312 * Relies on (h-q[x] == x) being true for x such that
4313 * 0 <= x < MAX_REPLY_QUEUES.
4314 */
4315static struct ctlr_info *queue_to_hba(u8 *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004316{
Matt Gates254f7962012-05-01 11:43:06 -05004317 return container_of((queue - *queue), struct ctlr_info, q[0]);
4318}
4319
4320static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
4321{
4322 struct ctlr_info *h = queue_to_hba(queue);
4323 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004324 u32 raw_tag;
4325
4326 if (ignore_bogus_interrupt(h))
4327 return IRQ_NONE;
4328
4329 if (interrupt_not_for_us(h))
4330 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004331 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004332 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05004333 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004334 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05004335 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004336 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004337 return IRQ_HANDLED;
4338}
4339
Matt Gates254f7962012-05-01 11:43:06 -05004340static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004341{
Matt Gates254f7962012-05-01 11:43:06 -05004342 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004343 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05004344 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004345
4346 if (ignore_bogus_interrupt(h))
4347 return IRQ_NONE;
4348
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004349 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05004350 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004351 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05004352 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004353 return IRQ_HANDLED;
4354}
4355
Matt Gates254f7962012-05-01 11:43:06 -05004356static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004357{
Matt Gates254f7962012-05-01 11:43:06 -05004358 struct ctlr_info *h = queue_to_hba((u8 *) queue);
Don Brace303932f2010-02-04 08:42:40 -06004359 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05004360 u8 q = *(u8 *) queue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004361
4362 if (interrupt_not_for_us(h))
4363 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004364 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004365 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05004366 raw_tag = get_next_completion(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004367 while (raw_tag != FIFO_EMPTY) {
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004368 if (likely(hpsa_tag_contains_index(raw_tag)))
4369 process_indexed_cmd(h, raw_tag);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004370 else
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004371 process_nonindexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05004372 raw_tag = next_command(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004373 }
4374 }
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004375 return IRQ_HANDLED;
4376}
4377
Matt Gates254f7962012-05-01 11:43:06 -05004378static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004379{
Matt Gates254f7962012-05-01 11:43:06 -05004380 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004381 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05004382 u8 q = *(u8 *) queue;
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004383
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004384 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05004385 raw_tag = get_next_completion(h, q);
Don Brace303932f2010-02-04 08:42:40 -06004386 while (raw_tag != FIFO_EMPTY) {
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004387 if (likely(hpsa_tag_contains_index(raw_tag)))
4388 process_indexed_cmd(h, raw_tag);
Don Brace303932f2010-02-04 08:42:40 -06004389 else
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004390 process_nonindexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05004391 raw_tag = next_command(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004392 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004393 return IRQ_HANDLED;
4394}
4395
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004396/* Send a message CDB to the firmware. Careful, this only works
4397 * in simple mode, not performant mode due to the tag lookup.
4398 * We only ever use this immediately after a controller reset.
4399 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004400static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
4401 unsigned char type)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004402{
4403 struct Command {
4404 struct CommandListHeader CommandHeader;
4405 struct RequestBlock Request;
4406 struct ErrDescriptor ErrorDescriptor;
4407 };
4408 struct Command *cmd;
4409 static const size_t cmd_sz = sizeof(*cmd) +
4410 sizeof(cmd->ErrorDescriptor);
4411 dma_addr_t paddr64;
4412 uint32_t paddr32, tag;
4413 void __iomem *vaddr;
4414 int i, err;
4415
4416 vaddr = pci_ioremap_bar(pdev, 0);
4417 if (vaddr == NULL)
4418 return -ENOMEM;
4419
4420 /* The Inbound Post Queue only accepts 32-bit physical addresses for the
4421 * CCISS commands, so they must be allocated from the lower 4GiB of
4422 * memory.
4423 */
4424 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
4425 if (err) {
4426 iounmap(vaddr);
4427 return -ENOMEM;
4428 }
4429
4430 cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
4431 if (cmd == NULL) {
4432 iounmap(vaddr);
4433 return -ENOMEM;
4434 }
4435
4436 /* This must fit, because of the 32-bit consistent DMA mask. Also,
4437 * although there's no guarantee, we assume that the address is at
4438 * least 4-byte aligned (most likely, it's page-aligned).
4439 */
4440 paddr32 = paddr64;
4441
4442 cmd->CommandHeader.ReplyQueue = 0;
4443 cmd->CommandHeader.SGList = 0;
4444 cmd->CommandHeader.SGTotal = 0;
4445 cmd->CommandHeader.Tag.lower = paddr32;
4446 cmd->CommandHeader.Tag.upper = 0;
4447 memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
4448
4449 cmd->Request.CDBLen = 16;
4450 cmd->Request.Type.Type = TYPE_MSG;
4451 cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
4452 cmd->Request.Type.Direction = XFER_NONE;
4453 cmd->Request.Timeout = 0; /* Don't time out */
4454 cmd->Request.CDB[0] = opcode;
4455 cmd->Request.CDB[1] = type;
4456 memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
4457 cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
4458 cmd->ErrorDescriptor.Addr.upper = 0;
4459 cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
4460
4461 writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
4462
4463 for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
4464 tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004465 if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr32)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004466 break;
4467 msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
4468 }
4469
4470 iounmap(vaddr);
4471
4472 /* we leak the DMA buffer here ... no choice since the controller could
4473 * still complete the command.
4474 */
4475 if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
4476 dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
4477 opcode, type);
4478 return -ETIMEDOUT;
4479 }
4480
4481 pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
4482
4483 if (tag & HPSA_ERROR_BIT) {
4484 dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
4485 opcode, type);
4486 return -EIO;
4487 }
4488
4489 dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
4490 opcode, type);
4491 return 0;
4492}
4493
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004494#define hpsa_noop(p) hpsa_message(p, 3, 0)
4495
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004496static int hpsa_controller_hard_reset(struct pci_dev *pdev,
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05004497 void * __iomem vaddr, u32 use_doorbell)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004498{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004499 u16 pmcsr;
4500 int pos;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004501
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004502 if (use_doorbell) {
4503 /* For everything after the P600, the PCI power state method
4504 * of resetting the controller doesn't work, so we have this
4505 * other way using the doorbell register.
4506 */
4507 dev_info(&pdev->dev, "using doorbell to reset controller\n");
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05004508 writel(use_doorbell, vaddr + SA5_DOORBELL);
Stephen M. Cameron85009232013-09-23 13:33:36 -05004509
4510 /* PMC hardware guys tell us we need a 5 second delay after
4511 * doorbell reset and before any attempt to talk to the board
4512 * at all to ensure that this actually works and doesn't fall
4513 * over in some weird corner cases.
4514 */
4515 msleep(5000);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004516 } else { /* Try to do it the PCI power state way */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004517
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004518 /* Quoting from the Open CISS Specification: "The Power
4519 * Management Control/Status Register (CSR) controls the power
4520 * state of the device. The normal operating state is D0,
4521 * CSR=00h. The software off state is D3, CSR=03h. To reset
4522 * the controller, place the interface device in D3 then to D0,
4523 * this causes a secondary PCI reset which will reset the
4524 * controller." */
4525
4526 pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
4527 if (pos == 0) {
4528 dev_err(&pdev->dev,
4529 "hpsa_reset_controller: "
4530 "PCI PM not supported\n");
4531 return -ENODEV;
4532 }
4533 dev_info(&pdev->dev, "using PCI PM to reset controller\n");
4534 /* enter the D3hot power management state */
4535 pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
4536 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
4537 pmcsr |= PCI_D3hot;
4538 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
4539
4540 msleep(500);
4541
4542 /* enter the D0 power management state */
4543 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
4544 pmcsr |= PCI_D0;
4545 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
Mike Millerc4853ef2011-10-21 08:19:43 +02004546
4547 /*
4548 * The P600 requires a small delay when changing states.
4549 * Otherwise we may think the board did not reset and we bail.
4550 * This for kdump only and is particular to the P600.
4551 */
4552 msleep(500);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004553 }
4554 return 0;
4555}
4556
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004557static void init_driver_version(char *driver_version, int len)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004558{
4559 memset(driver_version, 0, len);
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06004560 strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004561}
4562
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004563static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004564{
4565 char *driver_version;
4566 int i, size = sizeof(cfgtable->driver_version);
4567
4568 driver_version = kmalloc(size, GFP_KERNEL);
4569 if (!driver_version)
4570 return -ENOMEM;
4571
4572 init_driver_version(driver_version, size);
4573 for (i = 0; i < size; i++)
4574 writeb(driver_version[i], &cfgtable->driver_version[i]);
4575 kfree(driver_version);
4576 return 0;
4577}
4578
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004579static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
4580 unsigned char *driver_ver)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004581{
4582 int i;
4583
4584 for (i = 0; i < sizeof(cfgtable->driver_version); i++)
4585 driver_ver[i] = readb(&cfgtable->driver_version[i]);
4586}
4587
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004588static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004589{
4590
4591 char *driver_ver, *old_driver_ver;
4592 int rc, size = sizeof(cfgtable->driver_version);
4593
4594 old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
4595 if (!old_driver_ver)
4596 return -ENOMEM;
4597 driver_ver = old_driver_ver + size;
4598
4599 /* After a reset, the 32 bytes of "driver version" in the cfgtable
4600 * should have been changed, otherwise we know the reset failed.
4601 */
4602 init_driver_version(old_driver_ver, size);
4603 read_driver_ver_from_cfgtable(cfgtable, driver_ver);
4604 rc = !memcmp(driver_ver, old_driver_ver, size);
4605 kfree(old_driver_ver);
4606 return rc;
4607}
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004608/* This does a hard reset of the controller using PCI power management
4609 * states or the using the doorbell register.
4610 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004611static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev)
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004612{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004613 u64 cfg_offset;
4614 u32 cfg_base_addr;
4615 u64 cfg_base_addr_index;
4616 void __iomem *vaddr;
4617 unsigned long paddr;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004618 u32 misc_fw_support;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06004619 int rc;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004620 struct CfgTable __iomem *cfgtable;
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05004621 u32 use_doorbell;
Stephen M. Cameron18867652010-06-16 13:51:45 -05004622 u32 board_id;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06004623 u16 command_register;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004624
4625 /* For controllers as old as the P600, this is very nearly
4626 * the same thing as
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004627 *
4628 * pci_save_state(pci_dev);
4629 * pci_set_power_state(pci_dev, PCI_D3hot);
4630 * pci_set_power_state(pci_dev, PCI_D0);
4631 * pci_restore_state(pci_dev);
4632 *
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004633 * For controllers newer than the P600, the pci power state
4634 * method of resetting doesn't work so we have another way
4635 * using the doorbell register.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004636 */
Stephen M. Cameron18867652010-06-16 13:51:45 -05004637
Stephen M. Cameron25c1e56a2011-01-06 14:48:18 -06004638 rc = hpsa_lookup_board_id(pdev, &board_id);
Stephen M. Cameron46380782011-05-03 15:00:01 -05004639 if (rc < 0 || !ctlr_is_resettable(board_id)) {
Stephen M. Cameron25c1e56a2011-01-06 14:48:18 -06004640 dev_warn(&pdev->dev, "Not resetting device.\n");
4641 return -ENODEV;
4642 }
Stephen M. Cameron46380782011-05-03 15:00:01 -05004643
4644 /* if controller is soft- but not hard resettable... */
4645 if (!ctlr_is_hard_resettable(board_id))
4646 return -ENOTSUPP; /* try soft reset later. */
Stephen M. Cameron18867652010-06-16 13:51:45 -05004647
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06004648 /* Save the PCI command register */
4649 pci_read_config_word(pdev, 4, &command_register);
4650 /* Turn the board off. This is so that later pci_restore_state()
4651 * won't turn the board on before the rest of config space is ready.
4652 */
4653 pci_disable_device(pdev);
4654 pci_save_state(pdev);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004655
4656 /* find the first memory BAR, so we can find the cfg table */
4657 rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
4658 if (rc)
4659 return rc;
4660 vaddr = remap_pci_mem(paddr, 0x250);
4661 if (!vaddr)
4662 return -ENOMEM;
4663
4664 /* find cfgtable in order to check if reset via doorbell is supported */
4665 rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
4666 &cfg_base_addr_index, &cfg_offset);
4667 if (rc)
4668 goto unmap_vaddr;
4669 cfgtable = remap_pci_mem(pci_resource_start(pdev,
4670 cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
4671 if (!cfgtable) {
4672 rc = -ENOMEM;
4673 goto unmap_vaddr;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004674 }
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004675 rc = write_driver_ver_to_cfgtable(cfgtable);
4676 if (rc)
4677 goto unmap_vaddr;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004678
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05004679 /* If reset via doorbell register is supported, use that.
4680 * There are two such methods. Favor the newest method.
4681 */
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004682 misc_fw_support = readl(&cfgtable->misc_fw_support);
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05004683 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
4684 if (use_doorbell) {
4685 use_doorbell = DOORBELL_CTLR_RESET2;
4686 } else {
4687 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
4688 if (use_doorbell) {
Mike Millerfba63092011-10-13 11:44:06 -05004689 dev_warn(&pdev->dev, "Soft reset not supported. "
4690 "Firmware update is required.\n");
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004691 rc = -ENOTSUPP; /* try soft reset */
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05004692 goto unmap_cfgtable;
4693 }
4694 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004695
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004696 rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
4697 if (rc)
4698 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004699
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06004700 pci_restore_state(pdev);
4701 rc = pci_enable_device(pdev);
4702 if (rc) {
4703 dev_warn(&pdev->dev, "failed to enable device.\n");
4704 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004705 }
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06004706 pci_write_config_word(pdev, 4, command_register);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004707
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004708 /* Some devices (notably the HP Smart Array 5i Controller)
4709 need a little pause here */
4710 msleep(HPSA_POST_RESET_PAUSE_MSECS);
4711
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004712 rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
4713 if (rc) {
4714 dev_warn(&pdev->dev,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004715 "failed waiting for board to become ready "
4716 "after hard reset\n");
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004717 goto unmap_cfgtable;
4718 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004719
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004720 rc = controller_reset_failed(vaddr);
4721 if (rc < 0)
4722 goto unmap_cfgtable;
4723 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004724 dev_warn(&pdev->dev, "Unable to successfully reset "
4725 "controller. Will try soft reset.\n");
4726 rc = -ENOTSUPP;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004727 } else {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004728 dev_info(&pdev->dev, "board ready after hard reset.\n");
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004729 }
4730
4731unmap_cfgtable:
4732 iounmap(cfgtable);
4733
4734unmap_vaddr:
4735 iounmap(vaddr);
4736 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004737}
4738
4739/*
4740 * We cannot read the structure directly, for portability we must use
4741 * the io functions.
4742 * This is for debug only.
4743 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004744static void print_cfg_table(struct device *dev, struct CfgTable *tb)
4745{
Stephen M. Cameron58f86652010-05-27 15:13:58 -05004746#ifdef HPSA_DEBUG
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004747 int i;
4748 char temp_name[17];
4749
4750 dev_info(dev, "Controller Configuration information\n");
4751 dev_info(dev, "------------------------------------\n");
4752 for (i = 0; i < 4; i++)
4753 temp_name[i] = readb(&(tb->Signature[i]));
4754 temp_name[4] = '\0';
4755 dev_info(dev, " Signature = %s\n", temp_name);
4756 dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
4757 dev_info(dev, " Transport methods supported = 0x%x\n",
4758 readl(&(tb->TransportSupport)));
4759 dev_info(dev, " Transport methods active = 0x%x\n",
4760 readl(&(tb->TransportActive)));
4761 dev_info(dev, " Requested transport Method = 0x%x\n",
4762 readl(&(tb->HostWrite.TransportRequest)));
4763 dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
4764 readl(&(tb->HostWrite.CoalIntDelay)));
4765 dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
4766 readl(&(tb->HostWrite.CoalIntCount)));
4767 dev_info(dev, " Max outstanding commands = 0x%d\n",
4768 readl(&(tb->CmdsOutMax)));
4769 dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
4770 for (i = 0; i < 16; i++)
4771 temp_name[i] = readb(&(tb->ServerName[i]));
4772 temp_name[16] = '\0';
4773 dev_info(dev, " Server Name = %s\n", temp_name);
4774 dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
4775 readl(&(tb->HeartBeat)));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004776#endif /* HPSA_DEBUG */
Stephen M. Cameron58f86652010-05-27 15:13:58 -05004777}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004778
4779static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
4780{
4781 int i, offset, mem_type, bar_type;
4782
4783 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
4784 return 0;
4785 offset = 0;
4786 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
4787 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
4788 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
4789 offset += 4;
4790 else {
4791 mem_type = pci_resource_flags(pdev, i) &
4792 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
4793 switch (mem_type) {
4794 case PCI_BASE_ADDRESS_MEM_TYPE_32:
4795 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
4796 offset += 4; /* 32 bit */
4797 break;
4798 case PCI_BASE_ADDRESS_MEM_TYPE_64:
4799 offset += 8;
4800 break;
4801 default: /* reserved in PCI 2.2 */
4802 dev_warn(&pdev->dev,
4803 "base address is invalid\n");
4804 return -1;
4805 break;
4806 }
4807 }
4808 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
4809 return i + 1;
4810 }
4811 return -1;
4812}
4813
4814/* If MSI/MSI-X is supported by the kernel we will try to enable it on
4815 * controllers that are capable. If not, we use IO-APIC mode.
4816 */
4817
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004818static void hpsa_interrupt_mode(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004819{
4820#ifdef CONFIG_PCI_MSI
Matt Gates254f7962012-05-01 11:43:06 -05004821 int err, i;
4822 struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
4823
4824 for (i = 0; i < MAX_REPLY_QUEUES; i++) {
4825 hpsa_msix_entries[i].vector = 0;
4826 hpsa_msix_entries[i].entry = i;
4827 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004828
4829 /* Some boards advertise MSI but don't really support it */
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05004830 if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
4831 (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004832 goto default_int_mode;
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05004833 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
4834 dev_info(&h->pdev->dev, "MSIX\n");
Hannes Reineckeeee0f032014-01-15 13:30:53 +01004835 h->msix_vector = MAX_REPLY_QUEUES;
Matt Gates254f7962012-05-01 11:43:06 -05004836 err = pci_enable_msix(h->pdev, hpsa_msix_entries,
Hannes Reineckeeee0f032014-01-15 13:30:53 +01004837 h->msix_vector);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004838 if (err > 0) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05004839 dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004840 "available\n", err);
Hannes Reineckeeee0f032014-01-15 13:30:53 +01004841 h->msix_vector = err;
4842 err = pci_enable_msix(h->pdev, hpsa_msix_entries,
4843 h->msix_vector);
4844 }
4845 if (!err) {
4846 for (i = 0; i < h->msix_vector; i++)
4847 h->intr[i] = hpsa_msix_entries[i].vector;
4848 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004849 } else {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05004850 dev_warn(&h->pdev->dev, "MSI-X init failed %d\n",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004851 err);
Hannes Reineckeeee0f032014-01-15 13:30:53 +01004852 h->msix_vector = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004853 goto default_int_mode;
4854 }
4855 }
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05004856 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
4857 dev_info(&h->pdev->dev, "MSI\n");
4858 if (!pci_enable_msi(h->pdev))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004859 h->msi_vector = 1;
4860 else
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05004861 dev_warn(&h->pdev->dev, "MSI init failed\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004862 }
4863default_int_mode:
4864#endif /* CONFIG_PCI_MSI */
4865 /* if we get here we're going to use the default interrupt mode */
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004866 h->intr[h->intr_mode] = h->pdev->irq;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004867}
4868
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004869static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05004870{
4871 int i;
4872 u32 subsystem_vendor_id, subsystem_device_id;
4873
4874 subsystem_vendor_id = pdev->subsystem_vendor;
4875 subsystem_device_id = pdev->subsystem_device;
4876 *board_id = ((subsystem_device_id << 16) & 0xffff0000) |
4877 subsystem_vendor_id;
4878
4879 for (i = 0; i < ARRAY_SIZE(products); i++)
4880 if (*board_id == products[i].board_id)
4881 return i;
4882
Stephen M. Cameron6798cc02010-06-16 13:51:20 -05004883 if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
4884 subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
4885 !hpsa_allow_any) {
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05004886 dev_warn(&pdev->dev, "unrecognized board ID: "
4887 "0x%08x, ignoring.\n", *board_id);
4888 return -ENODEV;
4889 }
4890 return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
4891}
4892
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004893static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
4894 unsigned long *memory_bar)
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05004895{
4896 int i;
4897
4898 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05004899 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05004900 /* addressing mode bits already removed */
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05004901 *memory_bar = pci_resource_start(pdev, i);
4902 dev_dbg(&pdev->dev, "memory BAR = %lx\n",
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05004903 *memory_bar);
4904 return 0;
4905 }
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05004906 dev_warn(&pdev->dev, "no memory BAR found\n");
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05004907 return -ENODEV;
4908}
4909
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004910static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
4911 int wait_for_ready)
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05004912{
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004913 int i, iterations;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05004914 u32 scratchpad;
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004915 if (wait_for_ready)
4916 iterations = HPSA_BOARD_READY_ITERATIONS;
4917 else
4918 iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05004919
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004920 for (i = 0; i < iterations; i++) {
4921 scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
4922 if (wait_for_ready) {
4923 if (scratchpad == HPSA_FIRMWARE_READY)
4924 return 0;
4925 } else {
4926 if (scratchpad != HPSA_FIRMWARE_READY)
4927 return 0;
4928 }
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05004929 msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
4930 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004931 dev_warn(&pdev->dev, "board not ready, timed out.\n");
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05004932 return -ENODEV;
4933}
4934
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004935static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
4936 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
4937 u64 *cfg_offset)
Stephen M. Camerona51fd472010-06-16 13:51:30 -05004938{
4939 *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
4940 *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
4941 *cfg_base_addr &= (u32) 0x0000ffff;
4942 *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
4943 if (*cfg_base_addr_index == -1) {
4944 dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
4945 return -ENODEV;
4946 }
4947 return 0;
4948}
4949
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004950static int hpsa_find_cfgtables(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004951{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004952 u64 cfg_offset;
4953 u32 cfg_base_addr;
4954 u64 cfg_base_addr_index;
Don Brace303932f2010-02-04 08:42:40 -06004955 u32 trans_offset;
Stephen M. Camerona51fd472010-06-16 13:51:30 -05004956 int rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05004957
Stephen M. Camerona51fd472010-06-16 13:51:30 -05004958 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
4959 &cfg_base_addr_index, &cfg_offset);
4960 if (rc)
4961 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05004962 h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
Stephen M. Camerona51fd472010-06-16 13:51:30 -05004963 cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
Stephen M. Cameron77c44952010-05-27 15:13:17 -05004964 if (!h->cfgtable)
4965 return -ENOMEM;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004966 rc = write_driver_ver_to_cfgtable(h->cfgtable);
4967 if (rc)
4968 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05004969 /* Find performant mode table. */
Stephen M. Camerona51fd472010-06-16 13:51:30 -05004970 trans_offset = readl(&h->cfgtable->TransMethodOffset);
Stephen M. Cameron77c44952010-05-27 15:13:17 -05004971 h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
4972 cfg_base_addr_index)+cfg_offset+trans_offset,
4973 sizeof(*h->transtable));
4974 if (!h->transtable)
4975 return -ENOMEM;
4976 return 0;
4977}
4978
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004979static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05004980{
4981 h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
Stephen M. Cameron72ceeae2011-01-06 14:48:13 -06004982
4983 /* Limit commands in memory limited kdump scenario. */
4984 if (reset_devices && h->max_commands > 32)
4985 h->max_commands = 32;
4986
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05004987 if (h->max_commands < 16) {
4988 dev_warn(&h->pdev->dev, "Controller reports "
4989 "max supported commands of %d, an obvious lie. "
4990 "Using 16. Ensure that firmware is up to date.\n",
4991 h->max_commands);
4992 h->max_commands = 16;
4993 }
4994}
4995
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05004996/* Interrogate the hardware for some limits:
4997 * max commands, max SG elements without chaining, and with chaining,
4998 * SG chain block size, etc.
4999 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005000static void hpsa_find_board_params(struct ctlr_info *h)
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005001{
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05005002 hpsa_get_max_perf_mode_cmds(h);
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005003 h->nr_cmds = h->max_commands - 4; /* Allow room for some ioctls */
5004 h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005005 h->fw_support = readl(&(h->cfgtable->misc_fw_support));
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005006 /*
5007 * Limit in-command s/g elements to 32 save dma'able memory.
5008 * Howvever spec says if 0, use 31
5009 */
5010 h->max_cmd_sg_entries = 31;
5011 if (h->maxsgentries > 512) {
5012 h->max_cmd_sg_entries = 32;
5013 h->chainsize = h->maxsgentries - h->max_cmd_sg_entries + 1;
5014 h->maxsgentries--; /* save one for chain pointer */
5015 } else {
5016 h->maxsgentries = 31; /* default to traditional values */
5017 h->chainsize = 0;
5018 }
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005019
5020 /* Find out what task management functions are supported and cache */
5021 h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005022}
5023
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05005024static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
5025{
Akinobu Mita0fc9fd42012-04-04 22:14:59 +09005026 if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05005027 dev_warn(&h->pdev->dev, "not a valid CISS config table\n");
5028 return false;
5029 }
5030 return true;
5031}
5032
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005033static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005034{
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005035 u32 driver_support;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005036
Stephen M. Cameron28e13442013-12-04 17:10:21 -06005037#ifdef CONFIG_X86
5038 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005039 driver_support = readl(&(h->cfgtable->driver_support));
5040 driver_support |= ENABLE_SCSI_PREFETCH;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005041#endif
Stephen M. Cameron28e13442013-12-04 17:10:21 -06005042 driver_support |= ENABLE_UNIT_ATTN;
5043 writel(driver_support, &(h->cfgtable->driver_support));
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005044}
5045
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05005046/* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result
5047 * in a prefetch beyond physical memory.
5048 */
5049static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
5050{
5051 u32 dma_prefetch;
5052
5053 if (h->board_id != 0x3225103C)
5054 return;
5055 dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
5056 dma_prefetch |= 0x8000;
5057 writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
5058}
5059
Stephen M. Cameron76438d02014-02-18 13:55:43 -06005060static void hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
5061{
5062 int i;
5063 u32 doorbell_value;
5064 unsigned long flags;
5065 /* wait until the clear_event_notify bit 6 is cleared by controller. */
5066 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
5067 spin_lock_irqsave(&h->lock, flags);
5068 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
5069 spin_unlock_irqrestore(&h->lock, flags);
5070 if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
5071 break;
5072 /* delay and try again */
5073 msleep(20);
5074 }
5075}
5076
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005077static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005078{
5079 int i;
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06005080 u32 doorbell_value;
5081 unsigned long flags;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005082
5083 /* under certain very rare conditions, this can take awhile.
5084 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
5085 * as we enter this code.)
5086 */
5087 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06005088 spin_lock_irqsave(&h->lock, flags);
5089 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
5090 spin_unlock_irqrestore(&h->lock, flags);
Dan Carpenter382be662011-02-15 15:33:13 -06005091 if (!(doorbell_value & CFGTBL_ChangeReq))
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005092 break;
5093 /* delay and try again */
Stephen M. Cameron60d3f5b2011-01-06 14:48:34 -06005094 usleep_range(10000, 20000);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005095 }
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005096}
5097
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005098static int hpsa_enter_simple_mode(struct ctlr_info *h)
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005099{
5100 u32 trans_support;
5101
5102 trans_support = readl(&(h->cfgtable->TransportSupport));
5103 if (!(trans_support & SIMPLE_MODE))
5104 return -ENOTSUPP;
5105
5106 h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005107
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005108 /* Update the field, and then ring the doorbell */
5109 writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
5110 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
5111 hpsa_wait_for_mode_change_ack(h);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005112 print_cfg_table(&h->pdev->dev, h->cfgtable);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005113 if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
5114 goto error;
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06005115 h->transMethod = CFGTBL_Trans_Simple;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005116 return 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005117error:
5118 dev_warn(&h->pdev->dev, "unable to get board into simple mode\n");
5119 return -ENODEV;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005120}
5121
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005122static int hpsa_pci_init(struct ctlr_info *h)
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005123{
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005124 int prod_index, err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005125
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05005126 prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
5127 if (prod_index < 0)
5128 return -ENODEV;
5129 h->product_name = products[prod_index].product_name;
5130 h->access = *(products[prod_index].access);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005131
Matthew Garrette5a44df2011-11-11 11:14:23 -05005132 pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
5133 PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
5134
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005135 err = pci_enable_device(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005136 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005137 dev_warn(&h->pdev->dev, "unable to enable PCI device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005138 return err;
5139 }
5140
Stephen M. Cameron5cb460a2012-05-01 11:42:20 -05005141 /* Enable bus mastering (pci_disable_device may disable this) */
5142 pci_set_master(h->pdev);
5143
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06005144 err = pci_request_regions(h->pdev, HPSA);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005145 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005146 dev_err(&h->pdev->dev,
5147 "cannot obtain PCI resources, aborting\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005148 return err;
5149 }
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05005150 hpsa_interrupt_mode(h);
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05005151 err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005152 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005153 goto err_out_free_res;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005154 h->vaddr = remap_pci_mem(h->paddr, 0x250);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05005155 if (!h->vaddr) {
5156 err = -ENOMEM;
5157 goto err_out_free_res;
5158 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005159 err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005160 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005161 goto err_out_free_res;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005162 err = hpsa_find_cfgtables(h);
5163 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005164 goto err_out_free_res;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005165 hpsa_find_board_params(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005166
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05005167 if (!hpsa_CISS_signature_present(h)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005168 err = -ENODEV;
5169 goto err_out_free_res;
5170 }
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005171 hpsa_set_driver_support_bits(h);
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05005172 hpsa_p600_dma_prefetch_quirk(h);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005173 err = hpsa_enter_simple_mode(h);
5174 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005175 goto err_out_free_res;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005176 return 0;
5177
5178err_out_free_res:
Stephen M. Cameron204892e2010-05-27 15:13:22 -05005179 if (h->transtable)
5180 iounmap(h->transtable);
5181 if (h->cfgtable)
5182 iounmap(h->cfgtable);
5183 if (h->vaddr)
5184 iounmap(h->vaddr);
Stephen M. Cameronf0bd0b682012-05-01 11:42:09 -05005185 pci_disable_device(h->pdev);
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005186 pci_release_regions(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005187 return err;
5188}
5189
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005190static void hpsa_hba_inquiry(struct ctlr_info *h)
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06005191{
5192 int rc;
5193
5194#define HBA_INQUIRY_BYTE_COUNT 64
5195 h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
5196 if (!h->hba_inquiry_data)
5197 return;
5198 rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
5199 h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
5200 if (rc != 0) {
5201 kfree(h->hba_inquiry_data);
5202 h->hba_inquiry_data = NULL;
5203 }
5204}
5205
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005206static int hpsa_init_reset_devices(struct pci_dev *pdev)
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005207{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005208 int rc, i;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005209
5210 if (!reset_devices)
5211 return 0;
5212
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005213 /* Reset the controller with a PCI power-cycle or via doorbell */
5214 rc = hpsa_kdump_hard_reset_controller(pdev);
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005215
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005216 /* -ENOTSUPP here means we cannot reset the controller
5217 * but it's already (and still) up and running in
Stephen M. Cameron18867652010-06-16 13:51:45 -05005218 * "performant mode". Or, it might be 640x, which can't reset
5219 * due to concerns about shared bbwc between 6402/6404 pair.
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005220 */
5221 if (rc == -ENOTSUPP)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005222 return rc; /* just try to do the kdump anyhow. */
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005223 if (rc)
5224 return -ENODEV;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005225
5226 /* Now try to get the controller to respond to a no-op */
Stephen M. Cameron2b870cb2011-05-03 14:59:36 -05005227 dev_warn(&pdev->dev, "Waiting for controller to respond to no-op\n");
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005228 for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
5229 if (hpsa_noop(pdev) == 0)
5230 break;
5231 else
5232 dev_warn(&pdev->dev, "no-op failed%s\n",
5233 (i < 11 ? "; re-trying" : ""));
5234 }
5235 return 0;
5236}
5237
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005238static int hpsa_allocate_cmd_pool(struct ctlr_info *h)
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05005239{
5240 h->cmd_pool_bits = kzalloc(
5241 DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
5242 sizeof(unsigned long), GFP_KERNEL);
5243 h->cmd_pool = pci_alloc_consistent(h->pdev,
5244 h->nr_cmds * sizeof(*h->cmd_pool),
5245 &(h->cmd_pool_dhandle));
5246 h->errinfo_pool = pci_alloc_consistent(h->pdev,
5247 h->nr_cmds * sizeof(*h->errinfo_pool),
5248 &(h->errinfo_pool_dhandle));
5249 if ((h->cmd_pool_bits == NULL)
5250 || (h->cmd_pool == NULL)
5251 || (h->errinfo_pool == NULL)) {
5252 dev_err(&h->pdev->dev, "out of memory in %s", __func__);
5253 return -ENOMEM;
5254 }
5255 return 0;
5256}
5257
5258static void hpsa_free_cmd_pool(struct ctlr_info *h)
5259{
5260 kfree(h->cmd_pool_bits);
5261 if (h->cmd_pool)
5262 pci_free_consistent(h->pdev,
5263 h->nr_cmds * sizeof(struct CommandList),
5264 h->cmd_pool, h->cmd_pool_dhandle);
5265 if (h->errinfo_pool)
5266 pci_free_consistent(h->pdev,
5267 h->nr_cmds * sizeof(struct ErrorInfo),
5268 h->errinfo_pool,
5269 h->errinfo_pool_dhandle);
Matt Gatese1f7de02014-02-18 13:55:17 -06005270 if (h->ioaccel_cmd_pool)
5271 pci_free_consistent(h->pdev,
5272 h->nr_cmds * sizeof(struct io_accel1_cmd),
5273 h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05005274}
5275
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05005276static int hpsa_request_irq(struct ctlr_info *h,
5277 irqreturn_t (*msixhandler)(int, void *),
5278 irqreturn_t (*intxhandler)(int, void *))
5279{
Matt Gates254f7962012-05-01 11:43:06 -05005280 int rc, i;
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05005281
Matt Gates254f7962012-05-01 11:43:06 -05005282 /*
5283 * initialize h->q[x] = x so that interrupt handlers know which
5284 * queue to process.
5285 */
5286 for (i = 0; i < MAX_REPLY_QUEUES; i++)
5287 h->q[i] = (u8) i;
5288
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005289 if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
Matt Gates254f7962012-05-01 11:43:06 -05005290 /* If performant mode and MSI-X, use multiple reply queues */
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005291 for (i = 0; i < h->msix_vector; i++)
Matt Gates254f7962012-05-01 11:43:06 -05005292 rc = request_irq(h->intr[i], msixhandler,
5293 0, h->devname,
5294 &h->q[i]);
5295 } else {
5296 /* Use single reply pool */
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005297 if (h->msix_vector > 0 || h->msi_vector) {
Matt Gates254f7962012-05-01 11:43:06 -05005298 rc = request_irq(h->intr[h->intr_mode],
5299 msixhandler, 0, h->devname,
5300 &h->q[h->intr_mode]);
5301 } else {
5302 rc = request_irq(h->intr[h->intr_mode],
5303 intxhandler, IRQF_SHARED, h->devname,
5304 &h->q[h->intr_mode]);
5305 }
5306 }
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05005307 if (rc) {
5308 dev_err(&h->pdev->dev, "unable to get irq %d for %s\n",
5309 h->intr[h->intr_mode], h->devname);
5310 return -ENODEV;
5311 }
5312 return 0;
5313}
5314
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005315static int hpsa_kdump_soft_reset(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005316{
5317 if (hpsa_send_host_reset(h, RAID_CTLR_LUNID,
5318 HPSA_RESET_TYPE_CONTROLLER)) {
5319 dev_warn(&h->pdev->dev, "Resetting array controller failed.\n");
5320 return -EIO;
5321 }
5322
5323 dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
5324 if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) {
5325 dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
5326 return -1;
5327 }
5328
5329 dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
5330 if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) {
5331 dev_warn(&h->pdev->dev, "Board failed to become ready "
5332 "after soft reset.\n");
5333 return -1;
5334 }
5335
5336 return 0;
5337}
5338
Matt Gates254f7962012-05-01 11:43:06 -05005339static void free_irqs(struct ctlr_info *h)
5340{
5341 int i;
5342
5343 if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
5344 /* Single reply queue, only one irq to free */
5345 i = h->intr_mode;
5346 free_irq(h->intr[i], &h->q[i]);
5347 return;
5348 }
5349
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005350 for (i = 0; i < h->msix_vector; i++)
Matt Gates254f7962012-05-01 11:43:06 -05005351 free_irq(h->intr[i], &h->q[i]);
5352}
5353
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05005354static void hpsa_free_irqs_and_disable_msix(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005355{
Matt Gates254f7962012-05-01 11:43:06 -05005356 free_irqs(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005357#ifdef CONFIG_PCI_MSI
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05005358 if (h->msix_vector) {
5359 if (h->pdev->msix_enabled)
5360 pci_disable_msix(h->pdev);
5361 } else if (h->msi_vector) {
5362 if (h->pdev->msi_enabled)
5363 pci_disable_msi(h->pdev);
5364 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005365#endif /* CONFIG_PCI_MSI */
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05005366}
5367
5368static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
5369{
5370 hpsa_free_irqs_and_disable_msix(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005371 hpsa_free_sg_chain_blocks(h);
5372 hpsa_free_cmd_pool(h);
Matt Gatese1f7de02014-02-18 13:55:17 -06005373 kfree(h->ioaccel1_blockFetchTable);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005374 kfree(h->blockFetchTable);
5375 pci_free_consistent(h->pdev, h->reply_pool_size,
5376 h->reply_pool, h->reply_pool_dhandle);
5377 if (h->vaddr)
5378 iounmap(h->vaddr);
5379 if (h->transtable)
5380 iounmap(h->transtable);
5381 if (h->cfgtable)
5382 iounmap(h->cfgtable);
5383 pci_release_regions(h->pdev);
5384 kfree(h);
5385}
5386
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005387/* Called when controller lockup detected. */
5388static void fail_all_cmds_on_list(struct ctlr_info *h, struct list_head *list)
5389{
5390 struct CommandList *c = NULL;
5391
5392 assert_spin_locked(&h->lock);
5393 /* Mark all outstanding commands as failed and complete them. */
5394 while (!list_empty(list)) {
5395 c = list_entry(list->next, struct CommandList, list);
5396 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05005397 finish_cmd(c);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005398 }
5399}
5400
5401static void controller_lockup_detected(struct ctlr_info *h)
5402{
5403 unsigned long flags;
5404
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005405 h->access.set_intr_mask(h, HPSA_INTR_OFF);
5406 spin_lock_irqsave(&h->lock, flags);
5407 h->lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
5408 spin_unlock_irqrestore(&h->lock, flags);
5409 dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x\n",
5410 h->lockup_detected);
5411 pci_disable_device(h->pdev);
5412 spin_lock_irqsave(&h->lock, flags);
5413 fail_all_cmds_on_list(h, &h->cmpQ);
5414 fail_all_cmds_on_list(h, &h->reqQ);
5415 spin_unlock_irqrestore(&h->lock, flags);
5416}
5417
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005418static void detect_controller_lockup(struct ctlr_info *h)
5419{
5420 u64 now;
5421 u32 heartbeat;
5422 unsigned long flags;
5423
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005424 now = get_jiffies_64();
5425 /* If we've received an interrupt recently, we're ok. */
5426 if (time_after64(h->last_intr_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05005427 (h->heartbeat_sample_interval), now))
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005428 return;
5429
5430 /*
5431 * If we've already checked the heartbeat recently, we're ok.
5432 * This could happen if someone sends us a signal. We
5433 * otherwise don't care about signals in this thread.
5434 */
5435 if (time_after64(h->last_heartbeat_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05005436 (h->heartbeat_sample_interval), now))
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005437 return;
5438
5439 /* If heartbeat has not changed since we last looked, we're not ok. */
5440 spin_lock_irqsave(&h->lock, flags);
5441 heartbeat = readl(&h->cfgtable->HeartBeat);
5442 spin_unlock_irqrestore(&h->lock, flags);
5443 if (h->last_heartbeat == heartbeat) {
5444 controller_lockup_detected(h);
5445 return;
5446 }
5447
5448 /* We're ok. */
5449 h->last_heartbeat = heartbeat;
5450 h->last_heartbeat_timestamp = now;
5451}
5452
Stephen M. Cameron76438d02014-02-18 13:55:43 -06005453static int hpsa_kickoff_rescan(struct ctlr_info *h)
5454{
5455 int i;
5456 char *event_type;
5457
5458 /* Ask the controller to clear the events we're handling. */
5459 if (h->transMethod & (CFGTBL_Trans_io_accel1) &&
5460 (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
5461 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
5462
5463 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
5464 event_type = "state change";
5465 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
5466 event_type = "configuration change";
5467 /* Stop sending new RAID offload reqs via the IO accelerator */
5468 scsi_block_requests(h->scsi_host);
5469 for (i = 0; i < h->ndevices; i++)
5470 h->dev[i]->offload_enabled = 0;
5471 hpsa_drain_commands(h);
5472 /* Set 'accelerator path config change' bit */
5473 dev_warn(&h->pdev->dev,
5474 "Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
5475 h->events, event_type);
5476 writel(h->events, &(h->cfgtable->clear_event_notify));
5477 /* Set the "clear event notify field update" bit 6 */
5478 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
5479 /* Wait until ctlr clears 'clear event notify field', bit 6 */
5480 hpsa_wait_for_clear_event_notify_ack(h);
5481 scsi_unblock_requests(h->scsi_host);
5482 } else {
5483 /* Acknowledge controller notification events. */
5484 writel(h->events, &(h->cfgtable->clear_event_notify));
5485 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
5486 hpsa_wait_for_clear_event_notify_ack(h);
5487#if 0
5488 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
5489 hpsa_wait_for_mode_change_ack(h);
5490#endif
5491 }
5492
5493 /* Something in the device list may have changed to trigger
5494 * the event, so do a rescan.
5495 */
5496 hpsa_scan_start(h->scsi_host);
5497 /* release reference taken on scsi host in check_controller_events */
5498 scsi_host_put(h->scsi_host);
5499 return 0;
5500}
5501
5502/* Check a register on the controller to see if there are configuration
5503 * changes (added/changed/removed logical drives, etc.) which mean that
5504 * we should rescan the controller for devices. If so, add the controller
5505 * to the list of controllers needing to be rescanned, and gets a
5506 * reference to the associated scsi_host.
5507 */
5508static void hpsa_ctlr_needs_rescan(struct ctlr_info *h)
5509{
5510 if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
5511 return;
5512
5513 h->events = readl(&(h->cfgtable->event_notify));
5514 if (!h->events)
5515 return;
5516
5517 /*
5518 * Take a reference on scsi host for the duration of the scan
5519 * Release in hpsa_kickoff_rescan(). No lock needed for scan_list
5520 * as only a single thread accesses this list.
5521 */
5522 scsi_host_get(h->scsi_host);
5523 hpsa_kickoff_rescan(h);
5524}
5525
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005526static void hpsa_monitor_ctlr_worker(struct work_struct *work)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005527{
5528 unsigned long flags;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005529 struct ctlr_info *h = container_of(to_delayed_work(work),
5530 struct ctlr_info, monitor_ctlr_work);
5531 detect_controller_lockup(h);
5532 if (h->lockup_detected)
5533 return;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06005534 hpsa_ctlr_needs_rescan(h);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005535 spin_lock_irqsave(&h->lock, flags);
5536 if (h->remove_in_progress) {
5537 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005538 return;
5539 }
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005540 schedule_delayed_work(&h->monitor_ctlr_work,
5541 h->heartbeat_sample_interval);
5542 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005543}
5544
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005545static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005546{
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005547 int dac, rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005548 struct ctlr_info *h;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005549 int try_soft_reset = 0;
5550 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005551
5552 if (number_of_controllers == 0)
5553 printk(KERN_INFO DRIVER_NAME "\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005554
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005555 rc = hpsa_init_reset_devices(pdev);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005556 if (rc) {
5557 if (rc != -ENOTSUPP)
5558 return rc;
5559 /* If the reset fails in a particular way (it has no way to do
5560 * a proper hard reset, so returns -ENOTSUPP) we can try to do
5561 * a soft reset once we get the controller configured up to the
5562 * point that it can accept a command.
5563 */
5564 try_soft_reset = 1;
5565 rc = 0;
5566 }
5567
5568reinit_after_soft_reset:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005569
Don Brace303932f2010-02-04 08:42:40 -06005570 /* Command structures must be aligned on a 32-byte boundary because
5571 * the 5 lower bits of the address are used by the hardware. and by
5572 * the driver. See comments in hpsa.h for more info.
5573 */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005574#define COMMANDLIST_ALIGNMENT 128
Don Brace303932f2010-02-04 08:42:40 -06005575 BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005576 h = kzalloc(sizeof(*h), GFP_KERNEL);
5577 if (!h)
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06005578 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005579
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005580 h->pdev = pdev;
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005581 h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
Stephen M. Cameron9e0fc7642011-02-15 15:32:48 -06005582 INIT_LIST_HEAD(&h->cmpQ);
5583 INIT_LIST_HEAD(&h->reqQ);
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06005584 spin_lock_init(&h->lock);
5585 spin_lock_init(&h->scan_lock);
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05005586 spin_lock_init(&h->passthru_count_lock);
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005587 rc = hpsa_pci_init(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06005588 if (rc != 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005589 goto clean1;
5590
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06005591 sprintf(h->devname, HPSA "%d", number_of_controllers);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005592 h->ctlr = number_of_controllers;
5593 number_of_controllers++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005594
5595 /* configure PCI DMA stuff */
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06005596 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
5597 if (rc == 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005598 dac = 1;
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06005599 } else {
5600 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
5601 if (rc == 0) {
5602 dac = 0;
5603 } else {
5604 dev_err(&pdev->dev, "no suitable DMA available\n");
5605 goto clean1;
5606 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005607 }
5608
5609 /* make sure the board interrupts are off */
5610 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005611
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05005612 if (hpsa_request_irq(h, do_hpsa_intr_msi, do_hpsa_intr_intx))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005613 goto clean2;
Don Brace303932f2010-02-04 08:42:40 -06005614 dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
5615 h->devname, pdev->device,
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005616 h->intr[h->intr_mode], dac ? "" : " not");
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05005617 if (hpsa_allocate_cmd_pool(h))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005618 goto clean4;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06005619 if (hpsa_allocate_sg_chain_blocks(h))
5620 goto clean4;
Stephen M. Camerona08a8472010-02-04 08:43:16 -06005621 init_waitqueue_head(&h->scan_wait_queue);
5622 h->scan_finished = 1; /* no scan currently in progress */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005623
5624 pci_set_drvdata(pdev, h);
Stephen M. Cameron9a413382011-05-03 14:59:41 -05005625 h->ndevices = 0;
5626 h->scsi_host = NULL;
5627 spin_lock_init(&h->devlock);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005628 hpsa_put_ctlr_into_performant_mode(h);
5629
5630 /* At this point, the controller is ready to take commands.
5631 * Now, if reset_devices and the hard reset didn't work, try
5632 * the soft reset and see if that works.
5633 */
5634 if (try_soft_reset) {
5635
5636 /* This is kind of gross. We may or may not get a completion
5637 * from the soft reset command, and if we do, then the value
5638 * from the fifo may or may not be valid. So, we wait 10 secs
5639 * after the reset throwing away any completions we get during
5640 * that time. Unregister the interrupt handler and register
5641 * fake ones to scoop up any residual completions.
5642 */
5643 spin_lock_irqsave(&h->lock, flags);
5644 h->access.set_intr_mask(h, HPSA_INTR_OFF);
5645 spin_unlock_irqrestore(&h->lock, flags);
Matt Gates254f7962012-05-01 11:43:06 -05005646 free_irqs(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005647 rc = hpsa_request_irq(h, hpsa_msix_discard_completions,
5648 hpsa_intx_discard_completions);
5649 if (rc) {
5650 dev_warn(&h->pdev->dev, "Failed to request_irq after "
5651 "soft reset.\n");
5652 goto clean4;
5653 }
5654
5655 rc = hpsa_kdump_soft_reset(h);
5656 if (rc)
5657 /* Neither hard nor soft reset worked, we're hosed. */
5658 goto clean4;
5659
5660 dev_info(&h->pdev->dev, "Board READY.\n");
5661 dev_info(&h->pdev->dev,
5662 "Waiting for stale completions to drain.\n");
5663 h->access.set_intr_mask(h, HPSA_INTR_ON);
5664 msleep(10000);
5665 h->access.set_intr_mask(h, HPSA_INTR_OFF);
5666
5667 rc = controller_reset_failed(h->cfgtable);
5668 if (rc)
5669 dev_info(&h->pdev->dev,
5670 "Soft reset appears to have failed.\n");
5671
5672 /* since the controller's reset, we have to go back and re-init
5673 * everything. Easiest to just forget what we've done and do it
5674 * all over again.
5675 */
5676 hpsa_undo_allocations_after_kdump_soft_reset(h);
5677 try_soft_reset = 0;
5678 if (rc)
5679 /* don't go to clean4, we already unallocated */
5680 return -ENODEV;
5681
5682 goto reinit_after_soft_reset;
5683 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005684
5685 /* Turn the interrupts on so we can service requests */
5686 h->access.set_intr_mask(h, HPSA_INTR_ON);
5687
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06005688 hpsa_hba_inquiry(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005689 hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005690
5691 /* Monitor the controller for firmware lockups */
5692 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
5693 INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
5694 schedule_delayed_work(&h->monitor_ctlr_work,
5695 h->heartbeat_sample_interval);
Stephen M. Cameron88bf6d62013-11-01 11:02:25 -05005696 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005697
5698clean4:
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06005699 hpsa_free_sg_chain_blocks(h);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05005700 hpsa_free_cmd_pool(h);
Matt Gates254f7962012-05-01 11:43:06 -05005701 free_irqs(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005702clean2:
5703clean1:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005704 kfree(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06005705 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005706}
5707
5708static void hpsa_flush_cache(struct ctlr_info *h)
5709{
5710 char *flush_buf;
5711 struct CommandList *c;
Stephen M. Cameron702890e2013-09-23 13:33:30 -05005712 unsigned long flags;
5713
5714 /* Don't bother trying to flush the cache if locked up */
5715 spin_lock_irqsave(&h->lock, flags);
5716 if (unlikely(h->lockup_detected)) {
5717 spin_unlock_irqrestore(&h->lock, flags);
5718 return;
5719 }
5720 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005721
5722 flush_buf = kzalloc(4, GFP_KERNEL);
5723 if (!flush_buf)
5724 return;
5725
5726 c = cmd_special_alloc(h);
5727 if (!c) {
5728 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
5729 goto out_of_memory;
5730 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005731 if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
5732 RAID_CTLR_LUNID, TYPE_CMD)) {
5733 goto out;
5734 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005735 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
5736 if (c->err_info->CommandStatus != 0)
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005737out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005738 dev_warn(&h->pdev->dev,
5739 "error flushing cache on controller\n");
5740 cmd_special_free(h, c);
5741out_of_memory:
5742 kfree(flush_buf);
5743}
5744
5745static void hpsa_shutdown(struct pci_dev *pdev)
5746{
5747 struct ctlr_info *h;
5748
5749 h = pci_get_drvdata(pdev);
5750 /* Turn board interrupts off and send the flush cache command
5751 * sendcmd will turn off interrupt, and send the flush...
5752 * To write all data in the battery backed cache to disks
5753 */
5754 hpsa_flush_cache(h);
5755 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05005756 hpsa_free_irqs_and_disable_msix(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005757}
5758
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005759static void hpsa_free_device_info(struct ctlr_info *h)
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06005760{
5761 int i;
5762
5763 for (i = 0; i < h->ndevices; i++)
5764 kfree(h->dev[i]);
5765}
5766
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005767static void hpsa_remove_one(struct pci_dev *pdev)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005768{
5769 struct ctlr_info *h;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005770 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005771
5772 if (pci_get_drvdata(pdev) == NULL) {
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005773 dev_err(&pdev->dev, "unable to remove device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005774 return;
5775 }
5776 h = pci_get_drvdata(pdev);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005777
5778 /* Get rid of any controller monitoring work items */
5779 spin_lock_irqsave(&h->lock, flags);
5780 h->remove_in_progress = 1;
5781 cancel_delayed_work(&h->monitor_ctlr_work);
5782 spin_unlock_irqrestore(&h->lock, flags);
5783
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005784 hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */
5785 hpsa_shutdown(pdev);
5786 iounmap(h->vaddr);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05005787 iounmap(h->transtable);
5788 iounmap(h->cfgtable);
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06005789 hpsa_free_device_info(h);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06005790 hpsa_free_sg_chain_blocks(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005791 pci_free_consistent(h->pdev,
5792 h->nr_cmds * sizeof(struct CommandList),
5793 h->cmd_pool, h->cmd_pool_dhandle);
5794 pci_free_consistent(h->pdev,
5795 h->nr_cmds * sizeof(struct ErrorInfo),
5796 h->errinfo_pool, h->errinfo_pool_dhandle);
Don Brace303932f2010-02-04 08:42:40 -06005797 pci_free_consistent(h->pdev, h->reply_pool_size,
5798 h->reply_pool, h->reply_pool_dhandle);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005799 kfree(h->cmd_pool_bits);
Don Brace303932f2010-02-04 08:42:40 -06005800 kfree(h->blockFetchTable);
Matt Gatese1f7de02014-02-18 13:55:17 -06005801 kfree(h->ioaccel1_blockFetchTable);
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06005802 kfree(h->hba_inquiry_data);
Stephen M. Cameronf0bd0b682012-05-01 11:42:09 -05005803 pci_disable_device(pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005804 pci_release_regions(pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005805 kfree(h);
5806}
5807
5808static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
5809 __attribute__((unused)) pm_message_t state)
5810{
5811 return -ENOSYS;
5812}
5813
5814static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
5815{
5816 return -ENOSYS;
5817}
5818
5819static struct pci_driver hpsa_pci_driver = {
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06005820 .name = HPSA,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005821 .probe = hpsa_init_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005822 .remove = hpsa_remove_one,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005823 .id_table = hpsa_pci_device_id, /* id_table */
5824 .shutdown = hpsa_shutdown,
5825 .suspend = hpsa_suspend,
5826 .resume = hpsa_resume,
5827};
5828
Don Brace303932f2010-02-04 08:42:40 -06005829/* Fill in bucket_map[], given nsgs (the max number of
5830 * scatter gather elements supported) and bucket[],
5831 * which is an array of 8 integers. The bucket[] array
5832 * contains 8 different DMA transfer sizes (in 16
5833 * byte increments) which the controller uses to fetch
5834 * commands. This function fills in bucket_map[], which
5835 * maps a given number of scatter gather elements to one of
5836 * the 8 DMA transfer sizes. The point of it is to allow the
5837 * controller to only do as much DMA as needed to fetch the
5838 * command, with the DMA transfer size encoded in the lower
5839 * bits of the command address.
5840 */
5841static void calc_bucket_map(int bucket[], int num_buckets,
Matt Gatese1f7de02014-02-18 13:55:17 -06005842 int nsgs, int min_blocks, int *bucket_map)
Don Brace303932f2010-02-04 08:42:40 -06005843{
5844 int i, j, b, size;
5845
Don Brace303932f2010-02-04 08:42:40 -06005846 /* Note, bucket_map must have nsgs+1 entries. */
5847 for (i = 0; i <= nsgs; i++) {
5848 /* Compute size of a command with i SG entries */
Matt Gatese1f7de02014-02-18 13:55:17 -06005849 size = i + min_blocks;
Don Brace303932f2010-02-04 08:42:40 -06005850 b = num_buckets; /* Assume the biggest bucket */
5851 /* Find the bucket that is just big enough */
Matt Gatese1f7de02014-02-18 13:55:17 -06005852 for (j = 0; j < num_buckets; j++) {
Don Brace303932f2010-02-04 08:42:40 -06005853 if (bucket[j] >= size) {
5854 b = j;
5855 break;
5856 }
5857 }
5858 /* for a command with i SG entries, use bucket b. */
5859 bucket_map[i] = b;
5860 }
5861}
5862
Matt Gatese1f7de02014-02-18 13:55:17 -06005863static void hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
Don Brace303932f2010-02-04 08:42:40 -06005864{
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05005865 int i;
5866 unsigned long register_value;
Matt Gatese1f7de02014-02-18 13:55:17 -06005867 unsigned long transMethod = CFGTBL_Trans_Performant |
5868 (trans_support & CFGTBL_Trans_use_short_tags) |
5869 CFGTBL_Trans_enable_directed_msix |
5870 (trans_support & CFGTBL_Trans_io_accel1);
5871
5872 struct access_method access = SA5_performant_access;
Stephen M. Camerondef342b2010-05-27 15:14:39 -05005873
5874 /* This is a bit complicated. There are 8 registers on
5875 * the controller which we write to to tell it 8 different
5876 * sizes of commands which there may be. It's a way of
5877 * reducing the DMA done to fetch each command. Encoded into
5878 * each command's tag are 3 bits which communicate to the controller
5879 * which of the eight sizes that command fits within. The size of
5880 * each command depends on how many scatter gather entries there are.
5881 * Each SG entry requires 16 bytes. The eight registers are programmed
5882 * with the number of 16-byte blocks a command of that size requires.
5883 * The smallest command possible requires 5 such 16 byte blocks.
Stephen M. Camerond66ae082012-01-19 14:00:48 -06005884 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
Stephen M. Camerondef342b2010-05-27 15:14:39 -05005885 * blocks. Note, this only extends to the SG entries contained
5886 * within the command block, and does not extend to chained blocks
5887 * of SG elements. bft[] contains the eight values we write to
5888 * the registers. They are not evenly distributed, but have more
5889 * sizes for small commands, and fewer sizes for larger commands.
5890 */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06005891 int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
5892 BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
Don Brace303932f2010-02-04 08:42:40 -06005893 /* 5 = 1 s/g entry or 4k
5894 * 6 = 2 s/g entry or 8k
5895 * 8 = 4 s/g entry or 16k
5896 * 10 = 6 s/g entry or 24k
5897 */
Don Brace303932f2010-02-04 08:42:40 -06005898
Don Brace303932f2010-02-04 08:42:40 -06005899 /* Controller spec: zero out this buffer. */
5900 memset(h->reply_pool, 0, h->reply_pool_size);
Don Brace303932f2010-02-04 08:42:40 -06005901
Stephen M. Camerond66ae082012-01-19 14:00:48 -06005902 bft[7] = SG_ENTRIES_IN_CMD + 4;
5903 calc_bucket_map(bft, ARRAY_SIZE(bft),
Matt Gatese1f7de02014-02-18 13:55:17 -06005904 SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
Don Brace303932f2010-02-04 08:42:40 -06005905 for (i = 0; i < 8; i++)
5906 writel(bft[i], &h->transtable->BlockFetch[i]);
5907
5908 /* size of controller ring buffer */
5909 writel(h->max_commands, &h->transtable->RepQSize);
Matt Gates254f7962012-05-01 11:43:06 -05005910 writel(h->nreply_queues, &h->transtable->RepQCount);
Don Brace303932f2010-02-04 08:42:40 -06005911 writel(0, &h->transtable->RepQCtrAddrLow32);
5912 writel(0, &h->transtable->RepQCtrAddrHigh32);
Matt Gates254f7962012-05-01 11:43:06 -05005913
5914 for (i = 0; i < h->nreply_queues; i++) {
5915 writel(0, &h->transtable->RepQAddr[i].upper);
5916 writel(h->reply_pool_dhandle +
5917 (h->max_commands * sizeof(u64) * i),
5918 &h->transtable->RepQAddr[i].lower);
5919 }
5920
Matt Gatese1f7de02014-02-18 13:55:17 -06005921 writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
5922 /*
5923 * enable outbound interrupt coalescing in accelerator mode;
5924 */
5925 if (trans_support & CFGTBL_Trans_io_accel1) {
5926 access = SA5_ioaccel_mode1_access;
5927 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
5928 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
5929 }
Don Brace303932f2010-02-04 08:42:40 -06005930 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005931 hpsa_wait_for_mode_change_ack(h);
Don Brace303932f2010-02-04 08:42:40 -06005932 register_value = readl(&(h->cfgtable->TransportActive));
5933 if (!(register_value & CFGTBL_Trans_Performant)) {
5934 dev_warn(&h->pdev->dev, "unable to get board into"
5935 " performant mode\n");
5936 return;
5937 }
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06005938 /* Change the access methods to the performant access methods */
Matt Gatese1f7de02014-02-18 13:55:17 -06005939 h->access = access;
5940 h->transMethod = transMethod;
5941
5942 if (!(trans_support & CFGTBL_Trans_io_accel1))
5943 return;
5944
5945 /* Set up I/O accelerator mode */
5946 for (i = 0; i < h->nreply_queues; i++) {
5947 writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
5948 h->reply_queue[i].current_entry =
5949 readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
5950 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005951 bft[7] = h->ioaccel_maxsg + 8;
5952 calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
Matt Gatese1f7de02014-02-18 13:55:17 -06005953 h->ioaccel1_blockFetchTable);
5954
5955 /* initialize all reply queue entries to unused */
5956 memset(h->reply_pool, (u8) IOACCEL_MODE1_REPLY_UNUSED,
5957 h->reply_pool_size);
5958
5959 /* set all the constant fields in the accelerator command
5960 * frames once at init time to save CPU cycles later.
5961 */
5962 for (i = 0; i < h->nr_cmds; i++) {
5963 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
5964
5965 cp->function = IOACCEL1_FUNCTION_SCSIIO;
5966 cp->err_info = (u32) (h->errinfo_pool_dhandle +
5967 (i * sizeof(struct ErrorInfo)));
5968 cp->err_info_len = sizeof(struct ErrorInfo);
5969 cp->sgl_offset = IOACCEL1_SGLOFFSET;
5970 cp->host_context_flags = IOACCEL1_HCFLAGS_CISS_FORMAT;
5971 cp->timeout_sec = 0;
5972 cp->ReplyQueue = 0;
5973 cp->Tag.lower = (i << DIRECT_LOOKUP_SHIFT) | DIRECT_LOOKUP_BIT;
5974 cp->Tag.upper = 0;
5975 cp->host_addr.lower = (u32) (h->ioaccel_cmd_pool_dhandle +
5976 (i * sizeof(struct io_accel1_cmd)));
5977 cp->host_addr.upper = 0;
5978 }
5979}
5980
5981static int hpsa_alloc_ioaccel_cmd_and_bft(struct ctlr_info *h)
5982{
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005983 h->ioaccel_maxsg =
5984 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
5985 if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
5986 h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
5987
Matt Gatese1f7de02014-02-18 13:55:17 -06005988 /* Command structures must be aligned on a 128-byte boundary
5989 * because the 7 lower bits of the address are used by the
5990 * hardware.
5991 */
5992#define IOACCEL1_COMMANDLIST_ALIGNMENT 128
5993 BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
5994 IOACCEL1_COMMANDLIST_ALIGNMENT);
5995 h->ioaccel_cmd_pool =
5996 pci_alloc_consistent(h->pdev,
5997 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
5998 &(h->ioaccel_cmd_pool_dhandle));
5999
6000 h->ioaccel1_blockFetchTable =
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006001 kmalloc(((h->ioaccel_maxsg + 1) *
Matt Gatese1f7de02014-02-18 13:55:17 -06006002 sizeof(u32)), GFP_KERNEL);
6003
6004 if ((h->ioaccel_cmd_pool == NULL) ||
6005 (h->ioaccel1_blockFetchTable == NULL))
6006 goto clean_up;
6007
6008 memset(h->ioaccel_cmd_pool, 0,
6009 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
6010 return 0;
6011
6012clean_up:
6013 if (h->ioaccel_cmd_pool)
6014 pci_free_consistent(h->pdev,
6015 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
6016 h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
6017 kfree(h->ioaccel1_blockFetchTable);
6018 return 1;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006019}
6020
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006021static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006022{
6023 u32 trans_support;
Matt Gatese1f7de02014-02-18 13:55:17 -06006024 unsigned long transMethod = CFGTBL_Trans_Performant |
6025 CFGTBL_Trans_use_short_tags;
Matt Gates254f7962012-05-01 11:43:06 -05006026 int i;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006027
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -06006028 if (hpsa_simple_mode)
6029 return;
6030
Matt Gatese1f7de02014-02-18 13:55:17 -06006031 /* Check for I/O accelerator mode support */
6032 if (trans_support & CFGTBL_Trans_io_accel1) {
6033 transMethod |= CFGTBL_Trans_io_accel1 |
6034 CFGTBL_Trans_enable_directed_msix;
6035 if (hpsa_alloc_ioaccel_cmd_and_bft(h))
6036 goto clean_up;
6037 }
6038
6039 /* TODO, check that this next line h->nreply_queues is correct */
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006040 trans_support = readl(&(h->cfgtable->TransportSupport));
6041 if (!(trans_support & PERFORMANT_MODE))
6042 return;
6043
Hannes Reineckeeee0f032014-01-15 13:30:53 +01006044 h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05006045 hpsa_get_max_perf_mode_cmds(h);
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006046 /* Performant mode ring buffer and supporting data structures */
Matt Gates254f7962012-05-01 11:43:06 -05006047 h->reply_pool_size = h->max_commands * sizeof(u64) * h->nreply_queues;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006048 h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size,
6049 &(h->reply_pool_dhandle));
6050
Matt Gates254f7962012-05-01 11:43:06 -05006051 for (i = 0; i < h->nreply_queues; i++) {
6052 h->reply_queue[i].head = &h->reply_pool[h->max_commands * i];
6053 h->reply_queue[i].size = h->max_commands;
6054 h->reply_queue[i].wraparound = 1; /* spec: init to 1 */
6055 h->reply_queue[i].current_entry = 0;
6056 }
6057
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006058 /* Need a block fetch table for performant mode */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006059 h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006060 sizeof(u32)), GFP_KERNEL);
6061
6062 if ((h->reply_pool == NULL)
6063 || (h->blockFetchTable == NULL))
6064 goto clean_up;
6065
Matt Gatese1f7de02014-02-18 13:55:17 -06006066 hpsa_enter_performant_mode(h, trans_support);
Don Brace303932f2010-02-04 08:42:40 -06006067 return;
6068
6069clean_up:
6070 if (h->reply_pool)
6071 pci_free_consistent(h->pdev, h->reply_pool_size,
6072 h->reply_pool, h->reply_pool_dhandle);
6073 kfree(h->blockFetchTable);
6074}
6075
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006076static void hpsa_drain_commands(struct ctlr_info *h)
6077{
6078 int cmds_out;
6079 unsigned long flags;
6080
6081 do { /* wait for all outstanding commands to drain out */
6082 spin_lock_irqsave(&h->lock, flags);
6083 cmds_out = h->commands_outstanding;
6084 spin_unlock_irqrestore(&h->lock, flags);
6085 if (cmds_out <= 0)
6086 break;
6087 msleep(100);
6088 } while (1);
6089}
6090
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006091/*
6092 * This is it. Register the PCI driver information for the cards we control
6093 * the OS will call our registered routines when it finds one of our cards.
6094 */
6095static int __init hpsa_init(void)
6096{
Mike Miller31468402010-02-25 14:03:12 -06006097 return pci_register_driver(&hpsa_pci_driver);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006098}
6099
6100static void __exit hpsa_cleanup(void)
6101{
6102 pci_unregister_driver(&hpsa_pci_driver);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006103}
6104
Matt Gatese1f7de02014-02-18 13:55:17 -06006105static void __attribute__((unused)) verify_offsets(void)
6106{
6107#define VERIFY_OFFSET(member, offset) \
6108 BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
6109
6110 VERIFY_OFFSET(dev_handle, 0x00);
6111 VERIFY_OFFSET(reserved1, 0x02);
6112 VERIFY_OFFSET(function, 0x03);
6113 VERIFY_OFFSET(reserved2, 0x04);
6114 VERIFY_OFFSET(err_info, 0x0C);
6115 VERIFY_OFFSET(reserved3, 0x10);
6116 VERIFY_OFFSET(err_info_len, 0x12);
6117 VERIFY_OFFSET(reserved4, 0x13);
6118 VERIFY_OFFSET(sgl_offset, 0x14);
6119 VERIFY_OFFSET(reserved5, 0x15);
6120 VERIFY_OFFSET(transfer_len, 0x1C);
6121 VERIFY_OFFSET(reserved6, 0x20);
6122 VERIFY_OFFSET(io_flags, 0x24);
6123 VERIFY_OFFSET(reserved7, 0x26);
6124 VERIFY_OFFSET(LUN, 0x34);
6125 VERIFY_OFFSET(control, 0x3C);
6126 VERIFY_OFFSET(CDB, 0x40);
6127 VERIFY_OFFSET(reserved8, 0x50);
6128 VERIFY_OFFSET(host_context_flags, 0x60);
6129 VERIFY_OFFSET(timeout_sec, 0x62);
6130 VERIFY_OFFSET(ReplyQueue, 0x64);
6131 VERIFY_OFFSET(reserved9, 0x65);
6132 VERIFY_OFFSET(Tag, 0x68);
6133 VERIFY_OFFSET(host_addr, 0x70);
6134 VERIFY_OFFSET(CISS_LUN, 0x78);
6135 VERIFY_OFFSET(SG, 0x78 + 8);
6136#undef VERIFY_OFFSET
6137}
6138
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006139module_init(hpsa_init);
6140module_exit(hpsa_cleanup);