blob: 42bb7d7fc8deeb941f0cf3605da81aaa6653f517 [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. Cameron960a30e72011-02-15 15:33:03 -0600330 h->transMethod & CFGTBL_Trans_Performant ?
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600331 "performant" : "simple");
332}
333
Stephen M. Cameron46380782011-05-03 15:00:01 -0500334/* List of controllers which cannot be hard reset on kexec with reset_devices */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600335static u32 unresettable_controller[] = {
336 0x324a103C, /* Smart Array P712m */
337 0x324b103C, /* SmartArray P711m */
338 0x3223103C, /* Smart Array P800 */
339 0x3234103C, /* Smart Array P400 */
340 0x3235103C, /* Smart Array P400i */
341 0x3211103C, /* Smart Array E200i */
342 0x3212103C, /* Smart Array E200 */
343 0x3213103C, /* Smart Array E200i */
344 0x3214103C, /* Smart Array E200i */
345 0x3215103C, /* Smart Array E200i */
346 0x3237103C, /* Smart Array E500 */
347 0x323D103C, /* Smart Array P700m */
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100348 0x40800E11, /* Smart Array 5i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600349 0x409C0E11, /* Smart Array 6400 */
350 0x409D0E11, /* Smart Array 6400 EM */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100351 0x40700E11, /* Smart Array 5300 */
352 0x40820E11, /* Smart Array 532 */
353 0x40830E11, /* Smart Array 5312 */
354 0x409A0E11, /* Smart Array 641 */
355 0x409B0E11, /* Smart Array 642 */
356 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600357};
358
Stephen M. Cameron46380782011-05-03 15:00:01 -0500359/* List of controllers which cannot even be soft reset */
360static u32 soft_unresettable_controller[] = {
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100361 0x40800E11, /* Smart Array 5i */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100362 0x40700E11, /* Smart Array 5300 */
363 0x40820E11, /* Smart Array 532 */
364 0x40830E11, /* Smart Array 5312 */
365 0x409A0E11, /* Smart Array 641 */
366 0x409B0E11, /* Smart Array 642 */
367 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron46380782011-05-03 15:00:01 -0500368 /* Exclude 640x boards. These are two pci devices in one slot
369 * which share a battery backed cache module. One controls the
370 * cache, the other accesses the cache through the one that controls
371 * it. If we reset the one controlling the cache, the other will
372 * likely not be happy. Just forbid resetting this conjoined mess.
373 * The 640x isn't really supported by hpsa anyway.
374 */
375 0x409C0E11, /* Smart Array 6400 */
376 0x409D0E11, /* Smart Array 6400 EM */
377};
378
379static int ctlr_is_hard_resettable(u32 board_id)
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600380{
381 int i;
382
383 for (i = 0; i < ARRAY_SIZE(unresettable_controller); i++)
Stephen M. Cameron46380782011-05-03 15:00:01 -0500384 if (unresettable_controller[i] == board_id)
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600385 return 0;
386 return 1;
387}
388
Stephen M. Cameron46380782011-05-03 15:00:01 -0500389static int ctlr_is_soft_resettable(u32 board_id)
390{
391 int i;
392
393 for (i = 0; i < ARRAY_SIZE(soft_unresettable_controller); i++)
394 if (soft_unresettable_controller[i] == board_id)
395 return 0;
396 return 1;
397}
398
399static int ctlr_is_resettable(u32 board_id)
400{
401 return ctlr_is_hard_resettable(board_id) ||
402 ctlr_is_soft_resettable(board_id);
403}
404
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600405static ssize_t host_show_resettable(struct device *dev,
406 struct device_attribute *attr, char *buf)
407{
408 struct ctlr_info *h;
409 struct Scsi_Host *shost = class_to_shost(dev);
410
411 h = shost_to_hba(shost);
Stephen M. Cameron46380782011-05-03 15:00:01 -0500412 return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600413}
414
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800415static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
416{
417 return (scsi3addr[3] & 0xC0) == 0x40;
418}
419
420static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
Mike Millerd82357e2012-05-01 11:43:32 -0500421 "1(ADM)", "UNKNOWN"
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800422};
423#define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
424
425static ssize_t raid_level_show(struct device *dev,
426 struct device_attribute *attr, char *buf)
427{
428 ssize_t l = 0;
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600429 unsigned char rlevel;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800430 struct ctlr_info *h;
431 struct scsi_device *sdev;
432 struct hpsa_scsi_dev_t *hdev;
433 unsigned long flags;
434
435 sdev = to_scsi_device(dev);
436 h = sdev_to_hba(sdev);
437 spin_lock_irqsave(&h->lock, flags);
438 hdev = sdev->hostdata;
439 if (!hdev) {
440 spin_unlock_irqrestore(&h->lock, flags);
441 return -ENODEV;
442 }
443
444 /* Is this even a logical drive? */
445 if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
446 spin_unlock_irqrestore(&h->lock, flags);
447 l = snprintf(buf, PAGE_SIZE, "N/A\n");
448 return l;
449 }
450
451 rlevel = hdev->raid_level;
452 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600453 if (rlevel > RAID_UNKNOWN)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800454 rlevel = RAID_UNKNOWN;
455 l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
456 return l;
457}
458
459static ssize_t lunid_show(struct device *dev,
460 struct device_attribute *attr, char *buf)
461{
462 struct ctlr_info *h;
463 struct scsi_device *sdev;
464 struct hpsa_scsi_dev_t *hdev;
465 unsigned long flags;
466 unsigned char lunid[8];
467
468 sdev = to_scsi_device(dev);
469 h = sdev_to_hba(sdev);
470 spin_lock_irqsave(&h->lock, flags);
471 hdev = sdev->hostdata;
472 if (!hdev) {
473 spin_unlock_irqrestore(&h->lock, flags);
474 return -ENODEV;
475 }
476 memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
477 spin_unlock_irqrestore(&h->lock, flags);
478 return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
479 lunid[0], lunid[1], lunid[2], lunid[3],
480 lunid[4], lunid[5], lunid[6], lunid[7]);
481}
482
483static ssize_t unique_id_show(struct device *dev,
484 struct device_attribute *attr, char *buf)
485{
486 struct ctlr_info *h;
487 struct scsi_device *sdev;
488 struct hpsa_scsi_dev_t *hdev;
489 unsigned long flags;
490 unsigned char sn[16];
491
492 sdev = to_scsi_device(dev);
493 h = sdev_to_hba(sdev);
494 spin_lock_irqsave(&h->lock, flags);
495 hdev = sdev->hostdata;
496 if (!hdev) {
497 spin_unlock_irqrestore(&h->lock, flags);
498 return -ENODEV;
499 }
500 memcpy(sn, hdev->device_id, sizeof(sn));
501 spin_unlock_irqrestore(&h->lock, flags);
502 return snprintf(buf, 16 * 2 + 2,
503 "%02X%02X%02X%02X%02X%02X%02X%02X"
504 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
505 sn[0], sn[1], sn[2], sn[3],
506 sn[4], sn[5], sn[6], sn[7],
507 sn[8], sn[9], sn[10], sn[11],
508 sn[12], sn[13], sn[14], sn[15]);
509}
510
Scott Teelc1988682014-02-18 13:55:54 -0600511static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
512 struct device_attribute *attr, char *buf)
513{
514 struct ctlr_info *h;
515 struct scsi_device *sdev;
516 struct hpsa_scsi_dev_t *hdev;
517 unsigned long flags;
518 int offload_enabled;
519
520 sdev = to_scsi_device(dev);
521 h = sdev_to_hba(sdev);
522 spin_lock_irqsave(&h->lock, flags);
523 hdev = sdev->hostdata;
524 if (!hdev) {
525 spin_unlock_irqrestore(&h->lock, flags);
526 return -ENODEV;
527 }
528 offload_enabled = hdev->offload_enabled;
529 spin_unlock_irqrestore(&h->lock, flags);
530 return snprintf(buf, 20, "%d\n", offload_enabled);
531}
532
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600533static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
534static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
535static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
536static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
Scott Teelc1988682014-02-18 13:55:54 -0600537static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
538 host_show_hp_ssd_smart_path_enabled, NULL);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600539static DEVICE_ATTR(firmware_revision, S_IRUGO,
540 host_show_firmware_revision, NULL);
541static DEVICE_ATTR(commands_outstanding, S_IRUGO,
542 host_show_commands_outstanding, NULL);
543static DEVICE_ATTR(transport_mode, S_IRUGO,
544 host_show_transport_mode, NULL);
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600545static DEVICE_ATTR(resettable, S_IRUGO,
546 host_show_resettable, NULL);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600547
548static struct device_attribute *hpsa_sdev_attrs[] = {
549 &dev_attr_raid_level,
550 &dev_attr_lunid,
551 &dev_attr_unique_id,
Scott Teelc1988682014-02-18 13:55:54 -0600552 &dev_attr_hp_ssd_smart_path_enabled,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600553 NULL,
554};
555
556static struct device_attribute *hpsa_shost_attrs[] = {
557 &dev_attr_rescan,
558 &dev_attr_firmware_revision,
559 &dev_attr_commands_outstanding,
560 &dev_attr_transport_mode,
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600561 &dev_attr_resettable,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600562 NULL,
563};
564
565static struct scsi_host_template hpsa_driver_template = {
566 .module = THIS_MODULE,
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600567 .name = HPSA,
568 .proc_name = HPSA,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600569 .queuecommand = hpsa_scsi_queue_command,
570 .scan_start = hpsa_scan_start,
571 .scan_finished = hpsa_scan_finished,
572 .change_queue_depth = hpsa_change_queue_depth,
573 .this_id = -1,
574 .use_clustering = ENABLE_CLUSTERING,
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500575 .eh_abort_handler = hpsa_eh_abort_handler,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600576 .eh_device_reset_handler = hpsa_eh_device_reset_handler,
577 .ioctl = hpsa_ioctl,
578 .slave_alloc = hpsa_slave_alloc,
579 .slave_destroy = hpsa_slave_destroy,
580#ifdef CONFIG_COMPAT
581 .compat_ioctl = hpsa_compat_ioctl,
582#endif
583 .sdev_attrs = hpsa_sdev_attrs,
584 .shost_attrs = hpsa_shost_attrs,
Stephen M. Cameronc0d6a4d2011-10-26 16:20:53 -0500585 .max_sectors = 8192,
Martin K. Petersen54b2b502013-10-23 06:25:40 -0400586 .no_write_same = 1,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600587};
588
589
590/* Enqueuing and dequeuing functions for cmdlists. */
591static inline void addQ(struct list_head *list, struct CommandList *c)
592{
593 list_add_tail(&c->list, list);
594}
595
Matt Gates254f7962012-05-01 11:43:06 -0500596static inline u32 next_command(struct ctlr_info *h, u8 q)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600597{
598 u32 a;
Matt Gates254f7962012-05-01 11:43:06 -0500599 struct reply_pool *rq = &h->reply_queue[q];
Matt Gatese16a33a2012-05-01 11:43:11 -0500600 unsigned long flags;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600601
Matt Gatese1f7de02014-02-18 13:55:17 -0600602 if (h->transMethod & CFGTBL_Trans_io_accel1)
603 return h->access.command_completed(h, q);
604
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600605 if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
Matt Gates254f7962012-05-01 11:43:06 -0500606 return h->access.command_completed(h, q);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600607
Matt Gates254f7962012-05-01 11:43:06 -0500608 if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
609 a = rq->head[rq->current_entry];
610 rq->current_entry++;
Matt Gatese16a33a2012-05-01 11:43:11 -0500611 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600612 h->commands_outstanding--;
Matt Gatese16a33a2012-05-01 11:43:11 -0500613 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600614 } else {
615 a = FIFO_EMPTY;
616 }
617 /* Check for wraparound */
Matt Gates254f7962012-05-01 11:43:06 -0500618 if (rq->current_entry == h->max_commands) {
619 rq->current_entry = 0;
620 rq->wraparound ^= 1;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600621 }
622 return a;
623}
624
625/* set_performant_mode: Modify the tag for cciss performant
626 * set bit 0 for pull model, bits 3-1 for block fetch
627 * register number
628 */
629static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
630{
Matt Gates254f7962012-05-01 11:43:06 -0500631 if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600632 c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
Hannes Reineckeeee0f032014-01-15 13:30:53 +0100633 if (likely(h->msix_vector > 0))
Matt Gates254f7962012-05-01 11:43:06 -0500634 c->Header.ReplyQueue =
John Kacur804a5cb2013-07-26 16:06:18 +0200635 raw_smp_processor_id() % h->nreply_queues;
Matt Gates254f7962012-05-01 11:43:06 -0500636 }
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600637}
638
Stephen M. Camerone85c5972012-05-01 11:43:42 -0500639static int is_firmware_flash_cmd(u8 *cdb)
640{
641 return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
642}
643
644/*
645 * During firmware flash, the heartbeat register may not update as frequently
646 * as it should. So we dial down lockup detection during firmware flash. and
647 * dial it back up when firmware flash completes.
648 */
649#define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
650#define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
651static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
652 struct CommandList *c)
653{
654 if (!is_firmware_flash_cmd(c->Request.CDB))
655 return;
656 atomic_inc(&h->firmware_flash_in_progress);
657 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
658}
659
660static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
661 struct CommandList *c)
662{
663 if (is_firmware_flash_cmd(c->Request.CDB) &&
664 atomic_dec_and_test(&h->firmware_flash_in_progress))
665 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
666}
667
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600668static void enqueue_cmd_and_start_io(struct ctlr_info *h,
669 struct CommandList *c)
670{
671 unsigned long flags;
672
673 set_performant_mode(h, c);
Stephen M. Camerone85c5972012-05-01 11:43:42 -0500674 dial_down_lockup_detection_during_fw_flash(h, c);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600675 spin_lock_irqsave(&h->lock, flags);
676 addQ(&h->reqQ, c);
677 h->Qdepth++;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600678 spin_unlock_irqrestore(&h->lock, flags);
Matt Gatese16a33a2012-05-01 11:43:11 -0500679 start_io(h);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600680}
681
682static inline void removeQ(struct CommandList *c)
683{
684 if (WARN_ON(list_empty(&c->list)))
685 return;
686 list_del_init(&c->list);
687}
688
689static inline int is_hba_lunid(unsigned char scsi3addr[])
690{
691 return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
692}
693
694static inline int is_scsi_rev_5(struct ctlr_info *h)
695{
696 if (!h->hba_inquiry_data)
697 return 0;
698 if ((h->hba_inquiry_data[2] & 0x07) == 5)
699 return 1;
700 return 0;
701}
702
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800703static int hpsa_find_target_lun(struct ctlr_info *h,
704 unsigned char scsi3addr[], int bus, int *target, int *lun)
705{
706 /* finds an unused bus, target, lun for a new physical device
707 * assumes h->devlock is held
708 */
709 int i, found = 0;
Scott Teelcfe5bad2011-10-26 16:21:07 -0500710 DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800711
Akinobu Mita263d9402012-01-21 00:15:27 +0900712 bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800713
714 for (i = 0; i < h->ndevices; i++) {
715 if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
Akinobu Mita263d9402012-01-21 00:15:27 +0900716 __set_bit(h->dev[i]->target, lun_taken);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800717 }
718
Akinobu Mita263d9402012-01-21 00:15:27 +0900719 i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
720 if (i < HPSA_MAX_DEVICES) {
721 /* *bus = 1; */
722 *target = i;
723 *lun = 0;
724 found = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800725 }
726 return !found;
727}
728
729/* Add an entry into h->dev[] array. */
730static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
731 struct hpsa_scsi_dev_t *device,
732 struct hpsa_scsi_dev_t *added[], int *nadded)
733{
734 /* assumes h->devlock is held */
735 int n = h->ndevices;
736 int i;
737 unsigned char addr1[8], addr2[8];
738 struct hpsa_scsi_dev_t *sd;
739
Scott Teelcfe5bad2011-10-26 16:21:07 -0500740 if (n >= HPSA_MAX_DEVICES) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800741 dev_err(&h->pdev->dev, "too many devices, some will be "
742 "inaccessible.\n");
743 return -1;
744 }
745
746 /* physical devices do not have lun or target assigned until now. */
747 if (device->lun != -1)
748 /* Logical device, lun is already assigned. */
749 goto lun_assigned;
750
751 /* If this device a non-zero lun of a multi-lun device
752 * byte 4 of the 8-byte LUN addr will contain the logical
753 * unit no, zero otherise.
754 */
755 if (device->scsi3addr[4] == 0) {
756 /* This is not a non-zero lun of a multi-lun device */
757 if (hpsa_find_target_lun(h, device->scsi3addr,
758 device->bus, &device->target, &device->lun) != 0)
759 return -1;
760 goto lun_assigned;
761 }
762
763 /* This is a non-zero lun of a multi-lun device.
764 * Search through our list and find the device which
765 * has the same 8 byte LUN address, excepting byte 4.
766 * Assign the same bus and target for this new LUN.
767 * Use the logical unit number from the firmware.
768 */
769 memcpy(addr1, device->scsi3addr, 8);
770 addr1[4] = 0;
771 for (i = 0; i < n; i++) {
772 sd = h->dev[i];
773 memcpy(addr2, sd->scsi3addr, 8);
774 addr2[4] = 0;
775 /* differ only in byte 4? */
776 if (memcmp(addr1, addr2, 8) == 0) {
777 device->bus = sd->bus;
778 device->target = sd->target;
779 device->lun = device->scsi3addr[4];
780 break;
781 }
782 }
783 if (device->lun == -1) {
784 dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
785 " suspect firmware bug or unsupported hardware "
786 "configuration.\n");
787 return -1;
788 }
789
790lun_assigned:
791
792 h->dev[n] = device;
793 h->ndevices++;
794 added[*nadded] = device;
795 (*nadded)++;
796
797 /* initially, (before registering with scsi layer) we don't
798 * know our hostno and we don't want to print anything first
799 * time anyway (the scsi layer's inquiries will show that info)
800 */
801 /* if (hostno != -1) */
802 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
803 scsi_device_type(device->devtype), hostno,
804 device->bus, device->target, device->lun);
805 return 0;
806}
807
Scott Teelbd9244f2012-01-19 14:01:30 -0600808/* Update an entry in h->dev[] array. */
809static void hpsa_scsi_update_entry(struct ctlr_info *h, int hostno,
810 int entry, struct hpsa_scsi_dev_t *new_entry)
811{
812 /* assumes h->devlock is held */
813 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
814
815 /* Raid level changed. */
816 h->dev[entry]->raid_level = new_entry->raid_level;
Stephen M. Cameron250fb122014-02-18 13:55:38 -0600817
818 /* Raid offload parameters changed. */
819 h->dev[entry]->offload_config = new_entry->offload_config;
820 h->dev[entry]->offload_enabled = new_entry->offload_enabled;
821
Scott Teelbd9244f2012-01-19 14:01:30 -0600822 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d updated.\n",
823 scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
824 new_entry->target, new_entry->lun);
825}
826
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -0600827/* Replace an entry from h->dev[] array. */
828static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
829 int entry, struct hpsa_scsi_dev_t *new_entry,
830 struct hpsa_scsi_dev_t *added[], int *nadded,
831 struct hpsa_scsi_dev_t *removed[], int *nremoved)
832{
833 /* assumes h->devlock is held */
Scott Teelcfe5bad2011-10-26 16:21:07 -0500834 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -0600835 removed[*nremoved] = h->dev[entry];
836 (*nremoved)++;
Stephen M. Cameron01350d02011-08-09 08:18:01 -0500837
838 /*
839 * New physical devices won't have target/lun assigned yet
840 * so we need to preserve the values in the slot we are replacing.
841 */
842 if (new_entry->target == -1) {
843 new_entry->target = h->dev[entry]->target;
844 new_entry->lun = h->dev[entry]->lun;
845 }
846
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -0600847 h->dev[entry] = new_entry;
848 added[*nadded] = new_entry;
849 (*nadded)++;
850 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n",
851 scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
852 new_entry->target, new_entry->lun);
853}
854
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800855/* Remove an entry from h->dev[] array. */
856static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
857 struct hpsa_scsi_dev_t *removed[], int *nremoved)
858{
859 /* assumes h->devlock is held */
860 int i;
861 struct hpsa_scsi_dev_t *sd;
862
Scott Teelcfe5bad2011-10-26 16:21:07 -0500863 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800864
865 sd = h->dev[entry];
866 removed[*nremoved] = h->dev[entry];
867 (*nremoved)++;
868
869 for (i = entry; i < h->ndevices-1; i++)
870 h->dev[i] = h->dev[i+1];
871 h->ndevices--;
872 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
873 scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
874 sd->lun);
875}
876
877#define SCSI3ADDR_EQ(a, b) ( \
878 (a)[7] == (b)[7] && \
879 (a)[6] == (b)[6] && \
880 (a)[5] == (b)[5] && \
881 (a)[4] == (b)[4] && \
882 (a)[3] == (b)[3] && \
883 (a)[2] == (b)[2] && \
884 (a)[1] == (b)[1] && \
885 (a)[0] == (b)[0])
886
887static void fixup_botched_add(struct ctlr_info *h,
888 struct hpsa_scsi_dev_t *added)
889{
890 /* called when scsi_add_device fails in order to re-adjust
891 * h->dev[] to match the mid layer's view.
892 */
893 unsigned long flags;
894 int i, j;
895
896 spin_lock_irqsave(&h->lock, flags);
897 for (i = 0; i < h->ndevices; i++) {
898 if (h->dev[i] == added) {
899 for (j = i; j < h->ndevices-1; j++)
900 h->dev[j] = h->dev[j+1];
901 h->ndevices--;
902 break;
903 }
904 }
905 spin_unlock_irqrestore(&h->lock, flags);
906 kfree(added);
907}
908
909static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
910 struct hpsa_scsi_dev_t *dev2)
911{
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800912 /* we compare everything except lun and target as these
913 * are not yet assigned. Compare parts likely
914 * to differ first
915 */
916 if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
917 sizeof(dev1->scsi3addr)) != 0)
918 return 0;
919 if (memcmp(dev1->device_id, dev2->device_id,
920 sizeof(dev1->device_id)) != 0)
921 return 0;
922 if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
923 return 0;
924 if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
925 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800926 if (dev1->devtype != dev2->devtype)
927 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800928 if (dev1->bus != dev2->bus)
929 return 0;
930 return 1;
931}
932
Scott Teelbd9244f2012-01-19 14:01:30 -0600933static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
934 struct hpsa_scsi_dev_t *dev2)
935{
936 /* Device attributes that can change, but don't mean
937 * that the device is a different device, nor that the OS
938 * needs to be told anything about the change.
939 */
940 if (dev1->raid_level != dev2->raid_level)
941 return 1;
Stephen M. Cameron250fb122014-02-18 13:55:38 -0600942 if (dev1->offload_config != dev2->offload_config)
943 return 1;
944 if (dev1->offload_enabled != dev2->offload_enabled)
945 return 1;
Scott Teelbd9244f2012-01-19 14:01:30 -0600946 return 0;
947}
948
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800949/* Find needle in haystack. If exact match found, return DEVICE_SAME,
950 * and return needle location in *index. If scsi3addr matches, but not
951 * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
Scott Teelbd9244f2012-01-19 14:01:30 -0600952 * location in *index.
953 * In the case of a minor device attribute change, such as RAID level, just
954 * return DEVICE_UPDATED, along with the updated device's location in index.
955 * If needle not found, return DEVICE_NOT_FOUND.
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800956 */
957static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
958 struct hpsa_scsi_dev_t *haystack[], int haystack_size,
959 int *index)
960{
961 int i;
962#define DEVICE_NOT_FOUND 0
963#define DEVICE_CHANGED 1
964#define DEVICE_SAME 2
Scott Teelbd9244f2012-01-19 14:01:30 -0600965#define DEVICE_UPDATED 3
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800966 for (i = 0; i < haystack_size; i++) {
Stephen M. Cameron23231042010-02-04 08:43:36 -0600967 if (haystack[i] == NULL) /* previously removed. */
968 continue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800969 if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
970 *index = i;
Scott Teelbd9244f2012-01-19 14:01:30 -0600971 if (device_is_the_same(needle, haystack[i])) {
972 if (device_updated(needle, haystack[i]))
973 return DEVICE_UPDATED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800974 return DEVICE_SAME;
Scott Teelbd9244f2012-01-19 14:01:30 -0600975 } else {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800976 return DEVICE_CHANGED;
Scott Teelbd9244f2012-01-19 14:01:30 -0600977 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800978 }
979 }
980 *index = -1;
981 return DEVICE_NOT_FOUND;
982}
983
Stephen M. Cameron4967bd32010-02-04 08:41:49 -0600984static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800985 struct hpsa_scsi_dev_t *sd[], int nsds)
986{
987 /* sd contains scsi3 addresses and devtypes, and inquiry
988 * data. This function takes what's in sd to be the current
989 * reality and updates h->dev[] to reflect that reality.
990 */
991 int i, entry, device_change, changes = 0;
992 struct hpsa_scsi_dev_t *csd;
993 unsigned long flags;
994 struct hpsa_scsi_dev_t **added, **removed;
995 int nadded, nremoved;
996 struct Scsi_Host *sh = NULL;
997
Scott Teelcfe5bad2011-10-26 16:21:07 -0500998 added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
999 removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001000
1001 if (!added || !removed) {
1002 dev_warn(&h->pdev->dev, "out of memory in "
1003 "adjust_hpsa_scsi_table\n");
1004 goto free_and_out;
1005 }
1006
1007 spin_lock_irqsave(&h->devlock, flags);
1008
1009 /* find any devices in h->dev[] that are not in
1010 * sd[] and remove them from h->dev[], and for any
1011 * devices which have changed, remove the old device
1012 * info and add the new device info.
Scott Teelbd9244f2012-01-19 14:01:30 -06001013 * If minor device attributes change, just update
1014 * the existing device structure.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001015 */
1016 i = 0;
1017 nremoved = 0;
1018 nadded = 0;
1019 while (i < h->ndevices) {
1020 csd = h->dev[i];
1021 device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
1022 if (device_change == DEVICE_NOT_FOUND) {
1023 changes++;
1024 hpsa_scsi_remove_entry(h, hostno, i,
1025 removed, &nremoved);
1026 continue; /* remove ^^^, hence i not incremented */
1027 } else if (device_change == DEVICE_CHANGED) {
1028 changes++;
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001029 hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
1030 added, &nadded, removed, &nremoved);
Stephen M. Cameronc7f172d2010-02-04 08:43:31 -06001031 /* Set it to NULL to prevent it from being freed
1032 * at the bottom of hpsa_update_scsi_devices()
1033 */
1034 sd[entry] = NULL;
Scott Teelbd9244f2012-01-19 14:01:30 -06001035 } else if (device_change == DEVICE_UPDATED) {
1036 hpsa_scsi_update_entry(h, hostno, i, sd[entry]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001037 }
1038 i++;
1039 }
1040
1041 /* Now, make sure every device listed in sd[] is also
1042 * listed in h->dev[], adding them if they aren't found
1043 */
1044
1045 for (i = 0; i < nsds; i++) {
1046 if (!sd[i]) /* if already added above. */
1047 continue;
1048 device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1049 h->ndevices, &entry);
1050 if (device_change == DEVICE_NOT_FOUND) {
1051 changes++;
1052 if (hpsa_scsi_add_entry(h, hostno, sd[i],
1053 added, &nadded) != 0)
1054 break;
1055 sd[i] = NULL; /* prevent from being freed later. */
1056 } else if (device_change == DEVICE_CHANGED) {
1057 /* should never happen... */
1058 changes++;
1059 dev_warn(&h->pdev->dev,
1060 "device unexpectedly changed.\n");
1061 /* but if it does happen, we just ignore that device */
1062 }
1063 }
1064 spin_unlock_irqrestore(&h->devlock, flags);
1065
1066 /* Don't notify scsi mid layer of any changes the first time through
1067 * (or if there are no changes) scsi_scan_host will do it later the
1068 * first time through.
1069 */
1070 if (hostno == -1 || !changes)
1071 goto free_and_out;
1072
1073 sh = h->scsi_host;
1074 /* Notify scsi mid layer of any removed devices */
1075 for (i = 0; i < nremoved; i++) {
1076 struct scsi_device *sdev =
1077 scsi_device_lookup(sh, removed[i]->bus,
1078 removed[i]->target, removed[i]->lun);
1079 if (sdev != NULL) {
1080 scsi_remove_device(sdev);
1081 scsi_device_put(sdev);
1082 } else {
1083 /* We don't expect to get here.
1084 * future cmds to this device will get selection
1085 * timeout as if the device was gone.
1086 */
1087 dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
1088 " for removal.", hostno, removed[i]->bus,
1089 removed[i]->target, removed[i]->lun);
1090 }
1091 kfree(removed[i]);
1092 removed[i] = NULL;
1093 }
1094
1095 /* Notify scsi mid layer of any added devices */
1096 for (i = 0; i < nadded; i++) {
1097 if (scsi_add_device(sh, added[i]->bus,
1098 added[i]->target, added[i]->lun) == 0)
1099 continue;
1100 dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
1101 "device not added.\n", hostno, added[i]->bus,
1102 added[i]->target, added[i]->lun);
1103 /* now we have to remove it from h->dev,
1104 * since it didn't get added to scsi mid layer
1105 */
1106 fixup_botched_add(h, added[i]);
1107 }
1108
1109free_and_out:
1110 kfree(added);
1111 kfree(removed);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001112}
1113
1114/*
Joe Perches9e03aa22013-09-03 13:45:58 -07001115 * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001116 * Assume's h->devlock is held.
1117 */
1118static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1119 int bus, int target, int lun)
1120{
1121 int i;
1122 struct hpsa_scsi_dev_t *sd;
1123
1124 for (i = 0; i < h->ndevices; i++) {
1125 sd = h->dev[i];
1126 if (sd->bus == bus && sd->target == target && sd->lun == lun)
1127 return sd;
1128 }
1129 return NULL;
1130}
1131
1132/* link sdev->hostdata to our per-device structure. */
1133static int hpsa_slave_alloc(struct scsi_device *sdev)
1134{
1135 struct hpsa_scsi_dev_t *sd;
1136 unsigned long flags;
1137 struct ctlr_info *h;
1138
1139 h = sdev_to_hba(sdev);
1140 spin_lock_irqsave(&h->devlock, flags);
1141 sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1142 sdev_id(sdev), sdev->lun);
1143 if (sd != NULL)
1144 sdev->hostdata = sd;
1145 spin_unlock_irqrestore(&h->devlock, flags);
1146 return 0;
1147}
1148
1149static void hpsa_slave_destroy(struct scsi_device *sdev)
1150{
Stephen M. Cameronbcc442552010-02-04 08:41:54 -06001151 /* nothing to do. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001152}
1153
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001154static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
1155{
1156 int i;
1157
1158 if (!h->cmd_sg_list)
1159 return;
1160 for (i = 0; i < h->nr_cmds; i++) {
1161 kfree(h->cmd_sg_list[i]);
1162 h->cmd_sg_list[i] = NULL;
1163 }
1164 kfree(h->cmd_sg_list);
1165 h->cmd_sg_list = NULL;
1166}
1167
1168static int hpsa_allocate_sg_chain_blocks(struct ctlr_info *h)
1169{
1170 int i;
1171
1172 if (h->chainsize <= 0)
1173 return 0;
1174
1175 h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
1176 GFP_KERNEL);
1177 if (!h->cmd_sg_list)
1178 return -ENOMEM;
1179 for (i = 0; i < h->nr_cmds; i++) {
1180 h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
1181 h->chainsize, GFP_KERNEL);
1182 if (!h->cmd_sg_list[i])
1183 goto clean;
1184 }
1185 return 0;
1186
1187clean:
1188 hpsa_free_sg_chain_blocks(h);
1189 return -ENOMEM;
1190}
1191
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001192static int hpsa_map_sg_chain_block(struct ctlr_info *h,
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001193 struct CommandList *c)
1194{
1195 struct SGDescriptor *chain_sg, *chain_block;
1196 u64 temp64;
1197
1198 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
1199 chain_block = h->cmd_sg_list[c->cmdindex];
1200 chain_sg->Ext = HPSA_SG_CHAIN;
1201 chain_sg->Len = sizeof(*chain_sg) *
1202 (c->Header.SGTotal - h->max_cmd_sg_entries);
1203 temp64 = pci_map_single(h->pdev, chain_block, chain_sg->Len,
1204 PCI_DMA_TODEVICE);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001205 if (dma_mapping_error(&h->pdev->dev, temp64)) {
1206 /* prevent subsequent unmapping */
1207 chain_sg->Addr.lower = 0;
1208 chain_sg->Addr.upper = 0;
1209 return -1;
1210 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001211 chain_sg->Addr.lower = (u32) (temp64 & 0x0FFFFFFFFULL);
1212 chain_sg->Addr.upper = (u32) ((temp64 >> 32) & 0x0FFFFFFFFULL);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001213 return 0;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001214}
1215
1216static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
1217 struct CommandList *c)
1218{
1219 struct SGDescriptor *chain_sg;
1220 union u64bit temp64;
1221
1222 if (c->Header.SGTotal <= h->max_cmd_sg_entries)
1223 return;
1224
1225 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
1226 temp64.val32.lower = chain_sg->Addr.lower;
1227 temp64.val32.upper = chain_sg->Addr.upper;
1228 pci_unmap_single(h->pdev, temp64.val, chain_sg->Len, PCI_DMA_TODEVICE);
1229}
1230
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05001231static void complete_scsi_command(struct CommandList *cp)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001232{
1233 struct scsi_cmnd *cmd;
1234 struct ctlr_info *h;
1235 struct ErrorInfo *ei;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001236 struct hpsa_scsi_dev_t *dev;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001237
1238 unsigned char sense_key;
1239 unsigned char asc; /* additional sense code */
1240 unsigned char ascq; /* additional sense code qualifier */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05001241 unsigned long sense_data_size;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001242
1243 ei = cp->err_info;
1244 cmd = (struct scsi_cmnd *) cp->scsi_cmd;
1245 h = cp->h;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001246 dev = cmd->device->hostdata;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001247
1248 scsi_dma_unmap(cmd); /* undo the DMA mappings */
Matt Gatese1f7de02014-02-18 13:55:17 -06001249 if ((cp->cmd_type == CMD_SCSI) &&
1250 (cp->Header.SGTotal > h->max_cmd_sg_entries))
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001251 hpsa_unmap_sg_chain_block(h, cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001252
1253 cmd->result = (DID_OK << 16); /* host byte */
1254 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
Stephen M. Cameron55126722010-02-25 14:03:01 -06001255 cmd->result |= ei->ScsiStatus;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001256
1257 /* copy the sense data whether we need to or not. */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05001258 if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
1259 sense_data_size = SCSI_SENSE_BUFFERSIZE;
1260 else
1261 sense_data_size = sizeof(ei->SenseInfo);
1262 if (ei->SenseLen < sense_data_size)
1263 sense_data_size = ei->SenseLen;
1264
1265 memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001266 scsi_set_resid(cmd, ei->ResidualCnt);
1267
1268 if (ei->CommandStatus == 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001269 cmd_free(h, cp);
Tomas Henzl2cc5bfa2013-08-01 15:14:00 +02001270 cmd->scsi_done(cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001271 return;
1272 }
1273
Matt Gatese1f7de02014-02-18 13:55:17 -06001274 /* For I/O accelerator commands, copy over some fields to the normal
1275 * CISS header used below for error handling.
1276 */
1277 if (cp->cmd_type == CMD_IOACCEL1) {
1278 struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
1279 cp->Header.SGList = cp->Header.SGTotal = scsi_sg_count(cmd);
1280 cp->Request.CDBLen = c->io_flags & IOACCEL1_IOFLAGS_CDBLEN_MASK;
1281 cp->Header.Tag.lower = c->Tag.lower;
1282 cp->Header.Tag.upper = c->Tag.upper;
1283 memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
1284 memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001285
1286 /* Any RAID offload error results in retry which will use
1287 * the normal I/O path so the controller can handle whatever's
1288 * wrong.
1289 */
1290 if (is_logical_dev_addr_mode(dev->scsi3addr)) {
1291 if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
1292 dev->offload_enabled = 0;
1293 cmd->result = DID_SOFT_ERROR << 16;
1294 cmd_free(h, cp);
1295 cmd->scsi_done(cmd);
1296 return;
1297 }
Matt Gatese1f7de02014-02-18 13:55:17 -06001298 }
1299
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001300 /* an error has occurred */
1301 switch (ei->CommandStatus) {
1302
1303 case CMD_TARGET_STATUS:
1304 if (ei->ScsiStatus) {
1305 /* Get sense key */
1306 sense_key = 0xf & ei->SenseInfo[2];
1307 /* Get additional sense code */
1308 asc = ei->SenseInfo[12];
1309 /* Get addition sense code qualifier */
1310 ascq = ei->SenseInfo[13];
1311 }
1312
1313 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
Matt Gates3ce438d2013-12-04 17:10:36 -06001314 if (check_for_unit_attention(h, cp))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001315 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001316 if (sense_key == ILLEGAL_REQUEST) {
1317 /*
1318 * SCSI REPORT_LUNS is commonly unsupported on
1319 * Smart Array. Suppress noisy complaint.
1320 */
1321 if (cp->Request.CDB[0] == REPORT_LUNS)
1322 break;
1323
1324 /* If ASC/ASCQ indicate Logical Unit
1325 * Not Supported condition,
1326 */
1327 if ((asc == 0x25) && (ascq == 0x0)) {
1328 dev_warn(&h->pdev->dev, "cp %p "
1329 "has check condition\n", cp);
1330 break;
1331 }
1332 }
1333
1334 if (sense_key == NOT_READY) {
1335 /* If Sense is Not Ready, Logical Unit
1336 * Not ready, Manual Intervention
1337 * required
1338 */
1339 if ((asc == 0x04) && (ascq == 0x03)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001340 dev_warn(&h->pdev->dev, "cp %p "
1341 "has check condition: unit "
1342 "not ready, manual "
1343 "intervention required\n", cp);
1344 break;
1345 }
1346 }
Matt Gates1d3b3602010-02-04 08:43:00 -06001347 if (sense_key == ABORTED_COMMAND) {
1348 /* Aborted command is retryable */
1349 dev_warn(&h->pdev->dev, "cp %p "
1350 "has check condition: aborted command: "
1351 "ASC: 0x%x, ASCQ: 0x%x\n",
1352 cp, asc, ascq);
Stephen M. Cameron2e311fb2013-09-23 13:33:41 -05001353 cmd->result |= DID_SOFT_ERROR << 16;
Matt Gates1d3b3602010-02-04 08:43:00 -06001354 break;
1355 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001356 /* Must be some other type of check condition */
Stephen M. Cameron21b8e4e2012-05-01 11:42:25 -05001357 dev_dbg(&h->pdev->dev, "cp %p has check condition: "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001358 "unknown type: "
1359 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1360 "Returning result: 0x%x, "
1361 "cmd=[%02x %02x %02x %02x %02x "
Mike Miller807be732010-02-04 08:43:26 -06001362 "%02x %02x %02x %02x %02x %02x "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001363 "%02x %02x %02x %02x %02x]\n",
1364 cp, sense_key, asc, ascq,
1365 cmd->result,
1366 cmd->cmnd[0], cmd->cmnd[1],
1367 cmd->cmnd[2], cmd->cmnd[3],
1368 cmd->cmnd[4], cmd->cmnd[5],
1369 cmd->cmnd[6], cmd->cmnd[7],
Mike Miller807be732010-02-04 08:43:26 -06001370 cmd->cmnd[8], cmd->cmnd[9],
1371 cmd->cmnd[10], cmd->cmnd[11],
1372 cmd->cmnd[12], cmd->cmnd[13],
1373 cmd->cmnd[14], cmd->cmnd[15]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001374 break;
1375 }
1376
1377
1378 /* Problem was not a check condition
1379 * Pass it up to the upper layers...
1380 */
1381 if (ei->ScsiStatus) {
1382 dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
1383 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1384 "Returning result: 0x%x\n",
1385 cp, ei->ScsiStatus,
1386 sense_key, asc, ascq,
1387 cmd->result);
1388 } else { /* scsi status is zero??? How??? */
1389 dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
1390 "Returning no connection.\n", cp),
1391
1392 /* Ordinarily, this case should never happen,
1393 * but there is a bug in some released firmware
1394 * revisions that allows it to happen if, for
1395 * example, a 4100 backplane loses power and
1396 * the tape drive is in it. We assume that
1397 * it's a fatal error of some kind because we
1398 * can't show that it wasn't. We will make it
1399 * look like selection timeout since that is
1400 * the most common reason for this to occur,
1401 * and it's severe enough.
1402 */
1403
1404 cmd->result = DID_NO_CONNECT << 16;
1405 }
1406 break;
1407
1408 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1409 break;
1410 case CMD_DATA_OVERRUN:
1411 dev_warn(&h->pdev->dev, "cp %p has"
1412 " completed with data overrun "
1413 "reported\n", cp);
1414 break;
1415 case CMD_INVALID: {
1416 /* print_bytes(cp, sizeof(*cp), 1, 0);
1417 print_cmd(cp); */
1418 /* We get CMD_INVALID if you address a non-existent device
1419 * instead of a selection timeout (no response). You will
1420 * see this if you yank out a drive, then try to access it.
1421 * This is kind of a shame because it means that any other
1422 * CMD_INVALID (e.g. driver bug) will get interpreted as a
1423 * missing target. */
1424 cmd->result = DID_NO_CONNECT << 16;
1425 }
1426 break;
1427 case CMD_PROTOCOL_ERR:
Stephen M. Cameron256d0ea2012-09-14 16:34:25 -05001428 cmd->result = DID_ERROR << 16;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001429 dev_warn(&h->pdev->dev, "cp %p has "
Stephen M. Cameron256d0ea2012-09-14 16:34:25 -05001430 "protocol error\n", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001431 break;
1432 case CMD_HARDWARE_ERR:
1433 cmd->result = DID_ERROR << 16;
1434 dev_warn(&h->pdev->dev, "cp %p had hardware error\n", cp);
1435 break;
1436 case CMD_CONNECTION_LOST:
1437 cmd->result = DID_ERROR << 16;
1438 dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
1439 break;
1440 case CMD_ABORTED:
1441 cmd->result = DID_ABORT << 16;
1442 dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
1443 cp, ei->ScsiStatus);
1444 break;
1445 case CMD_ABORT_FAILED:
1446 cmd->result = DID_ERROR << 16;
1447 dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
1448 break;
1449 case CMD_UNSOLICITED_ABORT:
Stephen M. Cameronf6e76052011-07-26 11:08:52 -05001450 cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
1451 dev_warn(&h->pdev->dev, "cp %p aborted due to an unsolicited "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001452 "abort\n", cp);
1453 break;
1454 case CMD_TIMEOUT:
1455 cmd->result = DID_TIME_OUT << 16;
1456 dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
1457 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06001458 case CMD_UNABORTABLE:
1459 cmd->result = DID_ERROR << 16;
1460 dev_warn(&h->pdev->dev, "Command unabortable\n");
1461 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001462 case CMD_IOACCEL_DISABLED:
1463 /* This only handles the direct pass-through case since RAID
1464 * offload is handled above. Just attempt a retry.
1465 */
1466 cmd->result = DID_SOFT_ERROR << 16;
1467 dev_warn(&h->pdev->dev,
1468 "cp %p had HP SSD Smart Path error\n", cp);
1469 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001470 default:
1471 cmd->result = DID_ERROR << 16;
1472 dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
1473 cp, ei->CommandStatus);
1474 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001475 cmd_free(h, cp);
Tomas Henzl2cc5bfa2013-08-01 15:14:00 +02001476 cmd->scsi_done(cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001477}
1478
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001479static void hpsa_pci_unmap(struct pci_dev *pdev,
1480 struct CommandList *c, int sg_used, int data_direction)
1481{
1482 int i;
1483 union u64bit addr64;
1484
1485 for (i = 0; i < sg_used; i++) {
1486 addr64.val32.lower = c->SG[i].Addr.lower;
1487 addr64.val32.upper = c->SG[i].Addr.upper;
1488 pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len,
1489 data_direction);
1490 }
1491}
1492
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001493static int hpsa_map_one(struct pci_dev *pdev,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001494 struct CommandList *cp,
1495 unsigned char *buf,
1496 size_t buflen,
1497 int data_direction)
1498{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001499 u64 addr64;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001500
1501 if (buflen == 0 || data_direction == PCI_DMA_NONE) {
1502 cp->Header.SGList = 0;
1503 cp->Header.SGTotal = 0;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001504 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001505 }
1506
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001507 addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction);
Shuah Khaneceaae12013-02-20 11:24:34 -06001508 if (dma_mapping_error(&pdev->dev, addr64)) {
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001509 /* Prevent subsequent unmap of something never mapped */
Shuah Khaneceaae12013-02-20 11:24:34 -06001510 cp->Header.SGList = 0;
1511 cp->Header.SGTotal = 0;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001512 return -1;
Shuah Khaneceaae12013-02-20 11:24:34 -06001513 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001514 cp->SG[0].Addr.lower =
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001515 (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001516 cp->SG[0].Addr.upper =
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001517 (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001518 cp->SG[0].Len = buflen;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06001519 cp->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining */
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001520 cp->Header.SGList = (u8) 1; /* no. SGs contig in this cmd */
1521 cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001522 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001523}
1524
1525static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
1526 struct CommandList *c)
1527{
1528 DECLARE_COMPLETION_ONSTACK(wait);
1529
1530 c->waiting = &wait;
1531 enqueue_cmd_and_start_io(h, c);
1532 wait_for_completion(&wait);
1533}
1534
Stephen M. Camerona0c12412011-10-26 16:22:04 -05001535static void hpsa_scsi_do_simple_cmd_core_if_no_lockup(struct ctlr_info *h,
1536 struct CommandList *c)
1537{
1538 unsigned long flags;
1539
1540 /* If controller lockup detected, fake a hardware error. */
1541 spin_lock_irqsave(&h->lock, flags);
1542 if (unlikely(h->lockup_detected)) {
1543 spin_unlock_irqrestore(&h->lock, flags);
1544 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
1545 } else {
1546 spin_unlock_irqrestore(&h->lock, flags);
1547 hpsa_scsi_do_simple_cmd_core(h, c);
1548 }
1549}
1550
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001551#define MAX_DRIVER_CMD_RETRIES 25
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001552static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
1553 struct CommandList *c, int data_direction)
1554{
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001555 int backoff_time = 10, retry_count = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001556
1557 do {
Joe Perches7630abd2011-05-08 23:32:40 -07001558 memset(c->err_info, 0, sizeof(*c->err_info));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001559 hpsa_scsi_do_simple_cmd_core(h, c);
1560 retry_count++;
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001561 if (retry_count > 3) {
1562 msleep(backoff_time);
1563 if (backoff_time < 1000)
1564 backoff_time *= 2;
1565 }
Matt Bondurant852af202012-05-01 11:42:35 -05001566 } while ((check_for_unit_attention(h, c) ||
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001567 check_for_busy(h, c)) &&
1568 retry_count <= MAX_DRIVER_CMD_RETRIES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001569 hpsa_pci_unmap(h->pdev, c, 1, data_direction);
1570}
1571
1572static void hpsa_scsi_interpret_error(struct CommandList *cp)
1573{
1574 struct ErrorInfo *ei;
1575 struct device *d = &cp->h->pdev->dev;
1576
1577 ei = cp->err_info;
1578 switch (ei->CommandStatus) {
1579 case CMD_TARGET_STATUS:
1580 dev_warn(d, "cmd %p has completed with errors\n", cp);
1581 dev_warn(d, "cmd %p has SCSI Status = %x\n", cp,
1582 ei->ScsiStatus);
1583 if (ei->ScsiStatus == 0)
1584 dev_warn(d, "SCSI status is abnormally zero. "
1585 "(probably indicates selection timeout "
1586 "reported incorrectly due to a known "
1587 "firmware bug, circa July, 2001.)\n");
1588 break;
1589 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1590 dev_info(d, "UNDERRUN\n");
1591 break;
1592 case CMD_DATA_OVERRUN:
1593 dev_warn(d, "cp %p has completed with data overrun\n", cp);
1594 break;
1595 case CMD_INVALID: {
1596 /* controller unfortunately reports SCSI passthru's
1597 * to non-existent targets as invalid commands.
1598 */
1599 dev_warn(d, "cp %p is reported invalid (probably means "
1600 "target device no longer present)\n", cp);
1601 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
1602 print_cmd(cp); */
1603 }
1604 break;
1605 case CMD_PROTOCOL_ERR:
1606 dev_warn(d, "cp %p has protocol error \n", cp);
1607 break;
1608 case CMD_HARDWARE_ERR:
1609 /* cmd->result = DID_ERROR << 16; */
1610 dev_warn(d, "cp %p had hardware error\n", cp);
1611 break;
1612 case CMD_CONNECTION_LOST:
1613 dev_warn(d, "cp %p had connection lost\n", cp);
1614 break;
1615 case CMD_ABORTED:
1616 dev_warn(d, "cp %p was aborted\n", cp);
1617 break;
1618 case CMD_ABORT_FAILED:
1619 dev_warn(d, "cp %p reports abort failed\n", cp);
1620 break;
1621 case CMD_UNSOLICITED_ABORT:
1622 dev_warn(d, "cp %p aborted due to an unsolicited abort\n", cp);
1623 break;
1624 case CMD_TIMEOUT:
1625 dev_warn(d, "cp %p timed out\n", cp);
1626 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06001627 case CMD_UNABORTABLE:
1628 dev_warn(d, "Command unabortable\n");
1629 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001630 default:
1631 dev_warn(d, "cp %p returned unknown status %x\n", cp,
1632 ei->CommandStatus);
1633 }
1634}
1635
1636static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
1637 unsigned char page, unsigned char *buf,
1638 unsigned char bufsize)
1639{
1640 int rc = IO_OK;
1641 struct CommandList *c;
1642 struct ErrorInfo *ei;
1643
1644 c = cmd_special_alloc(h);
1645
1646 if (c == NULL) { /* trouble... */
1647 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06001648 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001649 }
1650
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001651 if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
1652 page, scsi3addr, TYPE_CMD)) {
1653 rc = -1;
1654 goto out;
1655 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001656 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1657 ei = c->err_info;
1658 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
1659 hpsa_scsi_interpret_error(c);
1660 rc = -1;
1661 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001662out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001663 cmd_special_free(h, c);
1664 return rc;
1665}
1666
1667static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr)
1668{
1669 int rc = IO_OK;
1670 struct CommandList *c;
1671 struct ErrorInfo *ei;
1672
1673 c = cmd_special_alloc(h);
1674
1675 if (c == NULL) { /* trouble... */
1676 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
Stephen M. Camerone9ea04a2010-02-25 14:03:06 -06001677 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001678 }
1679
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001680 /* fill_cmd can't fail here, no data buffer to map. */
1681 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h,
1682 NULL, 0, 0, scsi3addr, TYPE_MSG);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001683 hpsa_scsi_do_simple_cmd_core(h, c);
1684 /* no unmap needed here because no data xfer. */
1685
1686 ei = c->err_info;
1687 if (ei->CommandStatus != 0) {
1688 hpsa_scsi_interpret_error(c);
1689 rc = -1;
1690 }
1691 cmd_special_free(h, c);
1692 return rc;
1693}
1694
1695static void hpsa_get_raid_level(struct ctlr_info *h,
1696 unsigned char *scsi3addr, unsigned char *raid_level)
1697{
1698 int rc;
1699 unsigned char *buf;
1700
1701 *raid_level = RAID_UNKNOWN;
1702 buf = kzalloc(64, GFP_KERNEL);
1703 if (!buf)
1704 return;
1705 rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0xC1, buf, 64);
1706 if (rc == 0)
1707 *raid_level = buf[8];
1708 if (*raid_level > RAID_UNKNOWN)
1709 *raid_level = RAID_UNKNOWN;
1710 kfree(buf);
1711 return;
1712}
1713
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001714#define HPSA_MAP_DEBUG
1715#ifdef HPSA_MAP_DEBUG
1716static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
1717 struct raid_map_data *map_buff)
1718{
1719 struct raid_map_disk_data *dd = &map_buff->data[0];
1720 int map, row, col;
1721 u16 map_cnt, row_cnt, disks_per_row;
1722
1723 if (rc != 0)
1724 return;
1725
1726 dev_info(&h->pdev->dev, "structure_size = %u\n",
1727 le32_to_cpu(map_buff->structure_size));
1728 dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
1729 le32_to_cpu(map_buff->volume_blk_size));
1730 dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
1731 le64_to_cpu(map_buff->volume_blk_cnt));
1732 dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
1733 map_buff->phys_blk_shift);
1734 dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
1735 map_buff->parity_rotation_shift);
1736 dev_info(&h->pdev->dev, "strip_size = %u\n",
1737 le16_to_cpu(map_buff->strip_size));
1738 dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
1739 le64_to_cpu(map_buff->disk_starting_blk));
1740 dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
1741 le64_to_cpu(map_buff->disk_blk_cnt));
1742 dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
1743 le16_to_cpu(map_buff->data_disks_per_row));
1744 dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
1745 le16_to_cpu(map_buff->metadata_disks_per_row));
1746 dev_info(&h->pdev->dev, "row_cnt = %u\n",
1747 le16_to_cpu(map_buff->row_cnt));
1748 dev_info(&h->pdev->dev, "layout_map_count = %u\n",
1749 le16_to_cpu(map_buff->layout_map_count));
1750
1751 map_cnt = le16_to_cpu(map_buff->layout_map_count);
1752 for (map = 0; map < map_cnt; map++) {
1753 dev_info(&h->pdev->dev, "Map%u:\n", map);
1754 row_cnt = le16_to_cpu(map_buff->row_cnt);
1755 for (row = 0; row < row_cnt; row++) {
1756 dev_info(&h->pdev->dev, " Row%u:\n", row);
1757 disks_per_row =
1758 le16_to_cpu(map_buff->data_disks_per_row);
1759 for (col = 0; col < disks_per_row; col++, dd++)
1760 dev_info(&h->pdev->dev,
1761 " D%02u: h=0x%04x xor=%u,%u\n",
1762 col, dd->ioaccel_handle,
1763 dd->xor_mult[0], dd->xor_mult[1]);
1764 disks_per_row =
1765 le16_to_cpu(map_buff->metadata_disks_per_row);
1766 for (col = 0; col < disks_per_row; col++, dd++)
1767 dev_info(&h->pdev->dev,
1768 " M%02u: h=0x%04x xor=%u,%u\n",
1769 col, dd->ioaccel_handle,
1770 dd->xor_mult[0], dd->xor_mult[1]);
1771 }
1772 }
1773}
1774#else
1775static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
1776 __attribute__((unused)) int rc,
1777 __attribute__((unused)) struct raid_map_data *map_buff)
1778{
1779}
1780#endif
1781
1782static int hpsa_get_raid_map(struct ctlr_info *h,
1783 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
1784{
1785 int rc = 0;
1786 struct CommandList *c;
1787 struct ErrorInfo *ei;
1788
1789 c = cmd_special_alloc(h);
1790 if (c == NULL) {
1791 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1792 return -ENOMEM;
1793 }
1794 if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
1795 sizeof(this_device->raid_map), 0,
1796 scsi3addr, TYPE_CMD)) {
1797 dev_warn(&h->pdev->dev, "Out of memory in hpsa_get_raid_map()\n");
1798 cmd_special_free(h, c);
1799 return -ENOMEM;
1800 }
1801 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1802 ei = c->err_info;
1803 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
1804 hpsa_scsi_interpret_error(c);
1805 cmd_special_free(h, c);
1806 return -1;
1807 }
1808 cmd_special_free(h, c);
1809
1810 /* @todo in the future, dynamically allocate RAID map memory */
1811 if (le32_to_cpu(this_device->raid_map.structure_size) >
1812 sizeof(this_device->raid_map)) {
1813 dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
1814 rc = -1;
1815 }
1816 hpsa_debug_map_buff(h, rc, &this_device->raid_map);
1817 return rc;
1818}
1819
1820static void hpsa_get_ioaccel_status(struct ctlr_info *h,
1821 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
1822{
1823 int rc;
1824 unsigned char *buf;
1825 u8 ioaccel_status;
1826
1827 this_device->offload_config = 0;
1828 this_device->offload_enabled = 0;
1829
1830 buf = kzalloc(64, GFP_KERNEL);
1831 if (!buf)
1832 return;
1833 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
1834 HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
1835 if (rc != 0)
1836 goto out;
1837
1838#define IOACCEL_STATUS_BYTE 4
1839#define OFFLOAD_CONFIGURED_BIT 0x01
1840#define OFFLOAD_ENABLED_BIT 0x02
1841 ioaccel_status = buf[IOACCEL_STATUS_BYTE];
1842 this_device->offload_config =
1843 !!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
1844 if (this_device->offload_config) {
1845 this_device->offload_enabled =
1846 !!(ioaccel_status & OFFLOAD_ENABLED_BIT);
1847 if (hpsa_get_raid_map(h, scsi3addr, this_device))
1848 this_device->offload_enabled = 0;
1849 }
1850out:
1851 kfree(buf);
1852 return;
1853}
1854
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001855/* Get the device id from inquiry page 0x83 */
1856static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
1857 unsigned char *device_id, int buflen)
1858{
1859 int rc;
1860 unsigned char *buf;
1861
1862 if (buflen > 16)
1863 buflen = 16;
1864 buf = kzalloc(64, GFP_KERNEL);
1865 if (!buf)
1866 return -1;
1867 rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0x83, buf, 64);
1868 if (rc == 0)
1869 memcpy(device_id, &buf[8], buflen);
1870 kfree(buf);
1871 return rc != 0;
1872}
1873
1874static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
1875 struct ReportLUNdata *buf, int bufsize,
1876 int extended_response)
1877{
1878 int rc = IO_OK;
1879 struct CommandList *c;
1880 unsigned char scsi3addr[8];
1881 struct ErrorInfo *ei;
1882
1883 c = cmd_special_alloc(h);
1884 if (c == NULL) { /* trouble... */
1885 dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1886 return -1;
1887 }
Stephen M. Camerone89c0ae2010-02-04 08:42:04 -06001888 /* address the controller */
1889 memset(scsi3addr, 0, sizeof(scsi3addr));
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001890 if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
1891 buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
1892 rc = -1;
1893 goto out;
1894 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001895 if (extended_response)
1896 c->Request.CDB[1] = extended_response;
1897 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1898 ei = c->err_info;
1899 if (ei->CommandStatus != 0 &&
1900 ei->CommandStatus != CMD_DATA_UNDERRUN) {
1901 hpsa_scsi_interpret_error(c);
1902 rc = -1;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001903 } else {
1904 if (buf->extended_response_flag != extended_response) {
1905 dev_err(&h->pdev->dev,
1906 "report luns requested format %u, got %u\n",
1907 extended_response,
1908 buf->extended_response_flag);
1909 rc = -1;
1910 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001911 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001912out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001913 cmd_special_free(h, c);
1914 return rc;
1915}
1916
1917static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
1918 struct ReportLUNdata *buf,
1919 int bufsize, int extended_response)
1920{
1921 return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
1922}
1923
1924static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
1925 struct ReportLUNdata *buf, int bufsize)
1926{
1927 return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
1928}
1929
1930static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
1931 int bus, int target, int lun)
1932{
1933 device->bus = bus;
1934 device->target = target;
1935 device->lun = lun;
1936}
1937
1938static int hpsa_update_device_info(struct ctlr_info *h,
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05001939 unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
1940 unsigned char *is_OBDR_device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001941{
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05001942
1943#define OBDR_SIG_OFFSET 43
1944#define OBDR_TAPE_SIG "$DR-10"
1945#define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
1946#define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
1947
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06001948 unsigned char *inq_buff;
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05001949 unsigned char *obdr_sig;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001950
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06001951 inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001952 if (!inq_buff)
1953 goto bail_out;
1954
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001955 /* Do an inquiry to the device to see what it is. */
1956 if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
1957 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
1958 /* Inquiry failed (msg printed already) */
1959 dev_err(&h->pdev->dev,
1960 "hpsa_update_device_info: inquiry failed\n");
1961 goto bail_out;
1962 }
1963
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001964 this_device->devtype = (inq_buff[0] & 0x1f);
1965 memcpy(this_device->scsi3addr, scsi3addr, 8);
1966 memcpy(this_device->vendor, &inq_buff[8],
1967 sizeof(this_device->vendor));
1968 memcpy(this_device->model, &inq_buff[16],
1969 sizeof(this_device->model));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001970 memset(this_device->device_id, 0,
1971 sizeof(this_device->device_id));
1972 hpsa_get_device_id(h, scsi3addr, this_device->device_id,
1973 sizeof(this_device->device_id));
1974
1975 if (this_device->devtype == TYPE_DISK &&
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001976 is_logical_dev_addr_mode(scsi3addr)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001977 hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001978 if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
1979 hpsa_get_ioaccel_status(h, scsi3addr, this_device);
1980 } else {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001981 this_device->raid_level = RAID_UNKNOWN;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001982 this_device->offload_config = 0;
1983 this_device->offload_enabled = 0;
1984 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001985
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05001986 if (is_OBDR_device) {
1987 /* See if this is a One-Button-Disaster-Recovery device
1988 * by looking for "$DR-10" at offset 43 in inquiry data.
1989 */
1990 obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
1991 *is_OBDR_device = (this_device->devtype == TYPE_ROM &&
1992 strncmp(obdr_sig, OBDR_TAPE_SIG,
1993 OBDR_SIG_LEN) == 0);
1994 }
1995
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001996 kfree(inq_buff);
1997 return 0;
1998
1999bail_out:
2000 kfree(inq_buff);
2001 return 1;
2002}
2003
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002004static unsigned char *ext_target_model[] = {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002005 "MSA2012",
2006 "MSA2024",
2007 "MSA2312",
2008 "MSA2324",
Stephen M. Cameronfda38512011-05-03 15:00:07 -05002009 "P2000 G3 SAS",
Stephen M. Camerone06c8e52013-09-23 13:33:56 -05002010 "MSA 2040 SAS",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002011 NULL,
2012};
2013
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002014static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002015{
2016 int i;
2017
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002018 for (i = 0; ext_target_model[i]; i++)
2019 if (strncmp(device->model, ext_target_model[i],
2020 strlen(ext_target_model[i])) == 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002021 return 1;
2022 return 0;
2023}
2024
2025/* Helper function to assign bus, target, lun mapping of devices.
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002026 * Puts non-external target logical volumes on bus 0, external target logical
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002027 * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
2028 * Logical drive target and lun are assigned at this time, but
2029 * physical device lun and target assignment are deferred (assigned
2030 * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
2031 */
2032static void figure_bus_target_lun(struct ctlr_info *h,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002033 u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002034{
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002035 u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002036
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002037 if (!is_logical_dev_addr_mode(lunaddrbytes)) {
2038 /* physical device, target and lun filled in later */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002039 if (is_hba_lunid(lunaddrbytes))
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002040 hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002041 else
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002042 /* defer target, lun assignment for physical devices */
2043 hpsa_set_bus_target_lun(device, 2, -1, -1);
2044 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002045 }
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002046 /* It's a logical device */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002047 if (is_ext_target(h, device)) {
2048 /* external target way, put logicals on bus 1
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002049 * and match target/lun numbers box
2050 * reports, other smart array, bus 0, target 0, match lunid
2051 */
2052 hpsa_set_bus_target_lun(device,
2053 1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
2054 return;
2055 }
2056 hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002057}
2058
2059/*
2060 * If there is no lun 0 on a target, linux won't find any devices.
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002061 * For the external targets (arrays), we have to manually detect the enclosure
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002062 * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
2063 * it for some reason. *tmpdevice is the target we're adding,
2064 * this_device is a pointer into the current element of currentsd[]
2065 * that we're building up in update_scsi_devices(), below.
2066 * lunzerobits is a bitmap that tracks which targets already have a
2067 * lun 0 assigned.
2068 * Returns 1 if an enclosure was added, 0 if not.
2069 */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002070static int add_ext_target_dev(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002071 struct hpsa_scsi_dev_t *tmpdevice,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002072 struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002073 unsigned long lunzerobits[], int *n_ext_target_devs)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002074{
2075 unsigned char scsi3addr[8];
2076
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002077 if (test_bit(tmpdevice->target, lunzerobits))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002078 return 0; /* There is already a lun 0 on this target. */
2079
2080 if (!is_logical_dev_addr_mode(lunaddrbytes))
2081 return 0; /* It's the logical targets that may lack lun 0. */
2082
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002083 if (!is_ext_target(h, tmpdevice))
2084 return 0; /* Only external target devices have this problem. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002085
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002086 if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002087 return 0;
2088
Stephen M. Cameronc4f8a292011-01-07 10:55:43 -06002089 memset(scsi3addr, 0, 8);
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002090 scsi3addr[3] = tmpdevice->target;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002091 if (is_hba_lunid(scsi3addr))
2092 return 0; /* Don't add the RAID controller here. */
2093
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002094 if (is_scsi_rev_5(h))
2095 return 0; /* p1210m doesn't need to do this. */
2096
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002097 if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
Scott Teelaca4a522012-01-19 14:01:19 -06002098 dev_warn(&h->pdev->dev, "Maximum number of external "
2099 "target devices exceeded. Check your hardware "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002100 "configuration.");
2101 return 0;
2102 }
2103
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002104 if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002105 return 0;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002106 (*n_ext_target_devs)++;
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002107 hpsa_set_bus_target_lun(this_device,
2108 tmpdevice->bus, tmpdevice->target, 0);
2109 set_bit(tmpdevice->target, lunzerobits);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002110 return 1;
2111}
2112
2113/*
2114 * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
2115 * logdev. The number of luns in physdev and logdev are returned in
2116 * *nphysicals and *nlogicals, respectively.
2117 * Returns 0 on success, -1 otherwise.
2118 */
2119static int hpsa_gather_lun_info(struct ctlr_info *h,
2120 int reportlunsize,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002121 struct ReportLUNdata *physdev, u32 *nphysicals, int *physical_mode,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002122 struct ReportLUNdata *logdev, u32 *nlogicals)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002123{
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002124 int physical_entry_size = 8;
2125
2126 *physical_mode = 0;
2127
2128 /* For I/O accelerator mode we need to read physical device handles */
Mike MIller317d4ad2014-02-18 13:56:20 -06002129 if (h->transMethod & CFGTBL_Trans_io_accel1 ||
2130 h->transMethod & CFGTBL_Trans_io_accel2) {
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002131 *physical_mode = HPSA_REPORT_PHYS_EXTENDED;
2132 physical_entry_size = 24;
2133 }
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002134 if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002135 *physical_mode)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002136 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
2137 return -1;
2138 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002139 *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) /
2140 physical_entry_size;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002141 if (*nphysicals > HPSA_MAX_PHYS_LUN) {
2142 dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
2143 " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2144 *nphysicals - HPSA_MAX_PHYS_LUN);
2145 *nphysicals = HPSA_MAX_PHYS_LUN;
2146 }
2147 if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
2148 dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
2149 return -1;
2150 }
Stephen M. Cameron6df1e952010-02-04 08:42:19 -06002151 *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002152 /* Reject Logicals in excess of our max capability. */
2153 if (*nlogicals > HPSA_MAX_LUN) {
2154 dev_warn(&h->pdev->dev,
2155 "maximum logical LUNs (%d) exceeded. "
2156 "%d LUNs ignored.\n", HPSA_MAX_LUN,
2157 *nlogicals - HPSA_MAX_LUN);
2158 *nlogicals = HPSA_MAX_LUN;
2159 }
2160 if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
2161 dev_warn(&h->pdev->dev,
2162 "maximum logical + physical LUNs (%d) exceeded. "
2163 "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2164 *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
2165 *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
2166 }
2167 return 0;
2168}
2169
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002170u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i,
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002171 int nphysicals, int nlogicals,
2172 struct ReportExtendedLUNdata *physdev_list,
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002173 struct ReportLUNdata *logdev_list)
2174{
2175 /* Helper function, figure out where the LUN ID info is coming from
2176 * given index i, lists of physical and logical devices, where in
2177 * the list the raid controller is supposed to appear (first or last)
2178 */
2179
2180 int logicals_start = nphysicals + (raid_ctlr_position == 0);
2181 int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
2182
2183 if (i == raid_ctlr_position)
2184 return RAID_CTLR_LUNID;
2185
2186 if (i < logicals_start)
2187 return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0];
2188
2189 if (i < last_device)
2190 return &logdev_list->LUN[i - nphysicals -
2191 (raid_ctlr_position == 0)][0];
2192 BUG();
2193 return NULL;
2194}
2195
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002196static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
2197{
2198 /* the idea here is we could get notified
2199 * that some devices have changed, so we do a report
2200 * physical luns and report logical luns cmd, and adjust
2201 * our list of devices accordingly.
2202 *
2203 * The scsi3addr's of devices won't change so long as the
2204 * adapter is not reset. That means we can rescan and
2205 * tell which devices we already know about, vs. new
2206 * devices, vs. disappearing devices.
2207 */
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002208 struct ReportExtendedLUNdata *physdev_list = NULL;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002209 struct ReportLUNdata *logdev_list = NULL;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002210 u32 nphysicals = 0;
2211 u32 nlogicals = 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002212 int physical_mode = 0;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002213 u32 ndev_allocated = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002214 struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
2215 int ncurrent = 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002216 int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 24;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002217 int i, n_ext_target_devs, ndevs_to_allocate;
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002218 int raid_ctlr_position;
Scott Teelaca4a522012-01-19 14:01:19 -06002219 DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002220
Scott Teelcfe5bad2011-10-26 16:21:07 -05002221 currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002222 physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
2223 logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002224 tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
2225
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002226 if (!currentsd || !physdev_list || !logdev_list || !tmpdevice) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002227 dev_err(&h->pdev->dev, "out of memory\n");
2228 goto out;
2229 }
2230 memset(lunzerobits, 0, sizeof(lunzerobits));
2231
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002232 if (hpsa_gather_lun_info(h, reportlunsize,
2233 (struct ReportLUNdata *) physdev_list, &nphysicals,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002234 &physical_mode, logdev_list, &nlogicals))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002235 goto out;
2236
Scott Teelaca4a522012-01-19 14:01:19 -06002237 /* We might see up to the maximum number of logical and physical disks
2238 * plus external target devices, and a device for the local RAID
2239 * controller.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002240 */
Scott Teelaca4a522012-01-19 14:01:19 -06002241 ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002242
2243 /* Allocate the per device structures */
2244 for (i = 0; i < ndevs_to_allocate; i++) {
Scott Teelb7ec0212011-10-26 16:21:12 -05002245 if (i >= HPSA_MAX_DEVICES) {
2246 dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
2247 " %d devices ignored.\n", HPSA_MAX_DEVICES,
2248 ndevs_to_allocate - HPSA_MAX_DEVICES);
2249 break;
2250 }
2251
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002252 currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
2253 if (!currentsd[i]) {
2254 dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
2255 __FILE__, __LINE__);
2256 goto out;
2257 }
2258 ndev_allocated++;
2259 }
2260
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002261 if (unlikely(is_scsi_rev_5(h)))
2262 raid_ctlr_position = 0;
2263 else
2264 raid_ctlr_position = nphysicals + nlogicals;
2265
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002266 /* adjust our table of devices */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002267 n_ext_target_devs = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002268 for (i = 0; i < nphysicals + nlogicals + 1; i++) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002269 u8 *lunaddrbytes, is_OBDR = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002270
2271 /* Figure out where the LUN ID info is coming from */
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002272 lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
2273 i, nphysicals, nlogicals, physdev_list, logdev_list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002274 /* skip masked physical devices. */
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002275 if (lunaddrbytes[3] & 0xC0 &&
2276 i < nphysicals + (raid_ctlr_position == 0))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002277 continue;
2278
2279 /* Get device type, vendor, model, device id */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002280 if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
2281 &is_OBDR))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002282 continue; /* skip it if we can't talk to it. */
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002283 figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002284 this_device = currentsd[ncurrent];
2285
2286 /*
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002287 * For external target devices, we have to insert a LUN 0 which
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002288 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
2289 * is nonetheless an enclosure device there. We have to
2290 * present that otherwise linux won't find anything if
2291 * there is no lun 0.
2292 */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002293 if (add_ext_target_dev(h, tmpdevice, this_device,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002294 lunaddrbytes, lunzerobits,
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002295 &n_ext_target_devs)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002296 ncurrent++;
2297 this_device = currentsd[ncurrent];
2298 }
2299
2300 *this_device = *tmpdevice;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002301
2302 switch (this_device->devtype) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002303 case TYPE_ROM:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002304 /* We don't *really* support actual CD-ROM devices,
2305 * just "One Button Disaster Recovery" tape drive
2306 * which temporarily pretends to be a CD-ROM drive.
2307 * So we check that the device is really an OBDR tape
2308 * device by checking for "$DR-10" in bytes 43-48 of
2309 * the inquiry data.
2310 */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002311 if (is_OBDR)
2312 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002313 break;
2314 case TYPE_DISK:
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002315 if (i >= nphysicals) {
2316 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002317 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002318 }
2319 if (physical_mode == HPSA_REPORT_PHYS_EXTENDED) {
2320 memcpy(&this_device->ioaccel_handle,
2321 &lunaddrbytes[20],
2322 sizeof(this_device->ioaccel_handle));
2323 ncurrent++;
2324 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002325 break;
2326 case TYPE_TAPE:
2327 case TYPE_MEDIUM_CHANGER:
2328 ncurrent++;
2329 break;
2330 case TYPE_RAID:
2331 /* Only present the Smartarray HBA as a RAID controller.
2332 * If it's a RAID controller other than the HBA itself
2333 * (an external RAID controller, MSA500 or similar)
2334 * don't present it.
2335 */
2336 if (!is_hba_lunid(lunaddrbytes))
2337 break;
2338 ncurrent++;
2339 break;
2340 default:
2341 break;
2342 }
Scott Teelcfe5bad2011-10-26 16:21:07 -05002343 if (ncurrent >= HPSA_MAX_DEVICES)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002344 break;
2345 }
2346 adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
2347out:
2348 kfree(tmpdevice);
2349 for (i = 0; i < ndev_allocated; i++)
2350 kfree(currentsd[i]);
2351 kfree(currentsd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002352 kfree(physdev_list);
2353 kfree(logdev_list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002354}
2355
2356/* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
2357 * dma mapping and fills in the scatter gather entries of the
2358 * hpsa command, cp.
2359 */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002360static int hpsa_scatter_gather(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002361 struct CommandList *cp,
2362 struct scsi_cmnd *cmd)
2363{
2364 unsigned int len;
2365 struct scatterlist *sg;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002366 u64 addr64;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002367 int use_sg, i, sg_index, chained;
2368 struct SGDescriptor *curr_sg;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002369
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002370 BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002371
2372 use_sg = scsi_dma_map(cmd);
2373 if (use_sg < 0)
2374 return use_sg;
2375
2376 if (!use_sg)
2377 goto sglist_finished;
2378
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002379 curr_sg = cp->SG;
2380 chained = 0;
2381 sg_index = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002382 scsi_for_each_sg(cmd, sg, use_sg, i) {
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002383 if (i == h->max_cmd_sg_entries - 1 &&
2384 use_sg > h->max_cmd_sg_entries) {
2385 chained = 1;
2386 curr_sg = h->cmd_sg_list[cp->cmdindex];
2387 sg_index = 0;
2388 }
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002389 addr64 = (u64) sg_dma_address(sg);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002390 len = sg_dma_len(sg);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002391 curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
2392 curr_sg->Addr.upper = (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
2393 curr_sg->Len = len;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06002394 curr_sg->Ext = (i < scsi_sg_count(cmd) - 1) ? 0 : HPSA_SG_LAST;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002395 curr_sg++;
2396 }
2397
2398 if (use_sg + chained > h->maxSG)
2399 h->maxSG = use_sg + chained;
2400
2401 if (chained) {
2402 cp->Header.SGList = h->max_cmd_sg_entries;
2403 cp->Header.SGTotal = (u16) (use_sg + 1);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002404 if (hpsa_map_sg_chain_block(h, cp)) {
2405 scsi_dma_unmap(cmd);
2406 return -1;
2407 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002408 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002409 }
2410
2411sglist_finished:
2412
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002413 cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
2414 cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002415 return 0;
2416}
2417
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002418#define IO_ACCEL_INELIGIBLE (1)
2419static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
2420{
2421 int is_write = 0;
2422 u32 block;
2423 u32 block_cnt;
2424
2425 /* Perform some CDB fixups if needed using 10 byte reads/writes only */
2426 switch (cdb[0]) {
2427 case WRITE_6:
2428 case WRITE_12:
2429 is_write = 1;
2430 case READ_6:
2431 case READ_12:
2432 if (*cdb_len == 6) {
2433 block = (((u32) cdb[2]) << 8) | cdb[3];
2434 block_cnt = cdb[4];
2435 } else {
2436 BUG_ON(*cdb_len != 12);
2437 block = (((u32) cdb[2]) << 24) |
2438 (((u32) cdb[3]) << 16) |
2439 (((u32) cdb[4]) << 8) |
2440 cdb[5];
2441 block_cnt =
2442 (((u32) cdb[6]) << 24) |
2443 (((u32) cdb[7]) << 16) |
2444 (((u32) cdb[8]) << 8) |
2445 cdb[9];
2446 }
2447 if (block_cnt > 0xffff)
2448 return IO_ACCEL_INELIGIBLE;
2449
2450 cdb[0] = is_write ? WRITE_10 : READ_10;
2451 cdb[1] = 0;
2452 cdb[2] = (u8) (block >> 24);
2453 cdb[3] = (u8) (block >> 16);
2454 cdb[4] = (u8) (block >> 8);
2455 cdb[5] = (u8) (block);
2456 cdb[6] = 0;
2457 cdb[7] = (u8) (block_cnt >> 8);
2458 cdb[8] = (u8) (block_cnt);
2459 cdb[9] = 0;
2460 *cdb_len = 10;
2461 break;
2462 }
2463 return 0;
2464}
2465
Matt Gatese1f7de02014-02-18 13:55:17 -06002466/*
2467 * Queue a command to the I/O accelerator path.
Matt Gatese1f7de02014-02-18 13:55:17 -06002468 */
2469static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002470 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
2471 u8 *scsi3addr)
Matt Gatese1f7de02014-02-18 13:55:17 -06002472{
2473 struct scsi_cmnd *cmd = c->scsi_cmd;
Matt Gatese1f7de02014-02-18 13:55:17 -06002474 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
2475 unsigned int len;
2476 unsigned int total_len = 0;
2477 struct scatterlist *sg;
2478 u64 addr64;
2479 int use_sg, i;
2480 struct SGDescriptor *curr_sg;
2481 u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
2482
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002483 /* TODO: implement chaining support */
2484 if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
2485 return IO_ACCEL_INELIGIBLE;
2486
Matt Gatese1f7de02014-02-18 13:55:17 -06002487 BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
2488
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002489 if (fixup_ioaccel_cdb(cdb, &cdb_len))
2490 return IO_ACCEL_INELIGIBLE;
2491
Matt Gatese1f7de02014-02-18 13:55:17 -06002492 c->cmd_type = CMD_IOACCEL1;
2493
2494 /* Adjust the DMA address to point to the accelerated command buffer */
2495 c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
2496 (c->cmdindex * sizeof(*cp));
2497 BUG_ON(c->busaddr & 0x0000007F);
2498
2499 use_sg = scsi_dma_map(cmd);
2500 if (use_sg < 0)
2501 return use_sg;
2502
2503 if (use_sg) {
2504 curr_sg = cp->SG;
2505 scsi_for_each_sg(cmd, sg, use_sg, i) {
2506 addr64 = (u64) sg_dma_address(sg);
2507 len = sg_dma_len(sg);
2508 total_len += len;
2509 curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
2510 curr_sg->Addr.upper =
2511 (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
2512 curr_sg->Len = len;
2513
2514 if (i == (scsi_sg_count(cmd) - 1))
2515 curr_sg->Ext = HPSA_SG_LAST;
2516 else
2517 curr_sg->Ext = 0; /* we are not chaining */
2518 curr_sg++;
2519 }
2520
2521 switch (cmd->sc_data_direction) {
2522 case DMA_TO_DEVICE:
2523 control |= IOACCEL1_CONTROL_DATA_OUT;
2524 break;
2525 case DMA_FROM_DEVICE:
2526 control |= IOACCEL1_CONTROL_DATA_IN;
2527 break;
2528 case DMA_NONE:
2529 control |= IOACCEL1_CONTROL_NODATAXFER;
2530 break;
2531 default:
2532 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
2533 cmd->sc_data_direction);
2534 BUG();
2535 break;
2536 }
2537 } else {
2538 control |= IOACCEL1_CONTROL_NODATAXFER;
2539 }
2540
2541 /* Fill out the command structure to submit */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002542 cp->dev_handle = ioaccel_handle & 0xFFFF;
Matt Gatese1f7de02014-02-18 13:55:17 -06002543 cp->transfer_len = total_len;
2544 cp->io_flags = IOACCEL1_IOFLAGS_IO_REQ |
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002545 (cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK);
Matt Gatese1f7de02014-02-18 13:55:17 -06002546 cp->control = control;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002547 memcpy(cp->CDB, cdb, cdb_len);
2548 memcpy(cp->CISS_LUN, scsi3addr, 8);
Matt Gatese1f7de02014-02-18 13:55:17 -06002549
2550 /* Tell the controller to post the reply to the queue for this
2551 * processor. This seems to give the best I/O throughput.
2552 */
2553 cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
2554
2555 /* Set the bits in the address sent down to include:
2556 * - performant mode bit (bit 0)
2557 * - pull count (bits 1-3)
2558 * - command type (bits 4-6)
2559 */
2560 c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[use_sg] << 1) |
2561 IOACCEL1_BUSADDR_CMDTYPE;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002562 enqueue_cmd_and_start_io(h, c);
Matt Gatese1f7de02014-02-18 13:55:17 -06002563 return 0;
2564}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002565
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002566/*
2567 * Queue a command directly to a device behind the controller using the
2568 * I/O accelerator path.
2569 */
2570static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
2571 struct CommandList *c)
2572{
2573 struct scsi_cmnd *cmd = c->scsi_cmd;
2574 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
2575
2576 return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
2577 cmd->cmnd, cmd->cmd_len, dev->scsi3addr);
2578}
2579
2580/*
2581 * Attempt to perform offload RAID mapping for a logical volume I/O.
2582 */
2583static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
2584 struct CommandList *c)
2585{
2586 struct scsi_cmnd *cmd = c->scsi_cmd;
2587 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
2588 struct raid_map_data *map = &dev->raid_map;
2589 struct raid_map_disk_data *dd = &map->data[0];
2590 int is_write = 0;
2591 u32 map_index;
2592 u64 first_block, last_block;
2593 u32 block_cnt;
2594 u32 blocks_per_row;
2595 u64 first_row, last_row;
2596 u32 first_row_offset, last_row_offset;
2597 u32 first_column, last_column;
2598 u32 map_row;
2599 u32 disk_handle;
2600 u64 disk_block;
2601 u32 disk_block_cnt;
2602 u8 cdb[16];
2603 u8 cdb_len;
2604#if BITS_PER_LONG == 32
2605 u64 tmpdiv;
2606#endif
2607
2608 BUG_ON(!(dev->offload_config && dev->offload_enabled));
2609
2610 /* check for valid opcode, get LBA and block count */
2611 switch (cmd->cmnd[0]) {
2612 case WRITE_6:
2613 is_write = 1;
2614 case READ_6:
2615 first_block =
2616 (((u64) cmd->cmnd[2]) << 8) |
2617 cmd->cmnd[3];
2618 block_cnt = cmd->cmnd[4];
2619 break;
2620 case WRITE_10:
2621 is_write = 1;
2622 case READ_10:
2623 first_block =
2624 (((u64) cmd->cmnd[2]) << 24) |
2625 (((u64) cmd->cmnd[3]) << 16) |
2626 (((u64) cmd->cmnd[4]) << 8) |
2627 cmd->cmnd[5];
2628 block_cnt =
2629 (((u32) cmd->cmnd[7]) << 8) |
2630 cmd->cmnd[8];
2631 break;
2632 case WRITE_12:
2633 is_write = 1;
2634 case READ_12:
2635 first_block =
2636 (((u64) cmd->cmnd[2]) << 24) |
2637 (((u64) cmd->cmnd[3]) << 16) |
2638 (((u64) cmd->cmnd[4]) << 8) |
2639 cmd->cmnd[5];
2640 block_cnt =
2641 (((u32) cmd->cmnd[6]) << 24) |
2642 (((u32) cmd->cmnd[7]) << 16) |
2643 (((u32) cmd->cmnd[8]) << 8) |
2644 cmd->cmnd[9];
2645 break;
2646 case WRITE_16:
2647 is_write = 1;
2648 case READ_16:
2649 first_block =
2650 (((u64) cmd->cmnd[2]) << 56) |
2651 (((u64) cmd->cmnd[3]) << 48) |
2652 (((u64) cmd->cmnd[4]) << 40) |
2653 (((u64) cmd->cmnd[5]) << 32) |
2654 (((u64) cmd->cmnd[6]) << 24) |
2655 (((u64) cmd->cmnd[7]) << 16) |
2656 (((u64) cmd->cmnd[8]) << 8) |
2657 cmd->cmnd[9];
2658 block_cnt =
2659 (((u32) cmd->cmnd[10]) << 24) |
2660 (((u32) cmd->cmnd[11]) << 16) |
2661 (((u32) cmd->cmnd[12]) << 8) |
2662 cmd->cmnd[13];
2663 break;
2664 default:
2665 return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
2666 }
2667 BUG_ON(block_cnt == 0);
2668 last_block = first_block + block_cnt - 1;
2669
2670 /* check for write to non-RAID-0 */
2671 if (is_write && dev->raid_level != 0)
2672 return IO_ACCEL_INELIGIBLE;
2673
2674 /* check for invalid block or wraparound */
2675 if (last_block >= map->volume_blk_cnt || last_block < first_block)
2676 return IO_ACCEL_INELIGIBLE;
2677
2678 /* calculate stripe information for the request */
2679 blocks_per_row = map->data_disks_per_row * map->strip_size;
2680#if BITS_PER_LONG == 32
2681 tmpdiv = first_block;
2682 (void) do_div(tmpdiv, blocks_per_row);
2683 first_row = tmpdiv;
2684 tmpdiv = last_block;
2685 (void) do_div(tmpdiv, blocks_per_row);
2686 last_row = tmpdiv;
2687 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
2688 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
2689 tmpdiv = first_row_offset;
2690 (void) do_div(tmpdiv, map->strip_size);
2691 first_column = tmpdiv;
2692 tmpdiv = last_row_offset;
2693 (void) do_div(tmpdiv, map->strip_size);
2694 last_column = tmpdiv;
2695#else
2696 first_row = first_block / blocks_per_row;
2697 last_row = last_block / blocks_per_row;
2698 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
2699 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
2700 first_column = first_row_offset / map->strip_size;
2701 last_column = last_row_offset / map->strip_size;
2702#endif
2703
2704 /* if this isn't a single row/column then give to the controller */
2705 if ((first_row != last_row) || (first_column != last_column))
2706 return IO_ACCEL_INELIGIBLE;
2707
2708 /* proceeding with driver mapping */
2709 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
2710 map->row_cnt;
2711 map_index = (map_row * (map->data_disks_per_row +
2712 map->metadata_disks_per_row)) + first_column;
2713 if (dev->raid_level == 2) {
2714 /* simple round-robin balancing of RAID 1+0 reads across
2715 * primary and mirror members. this is appropriate for SSD
2716 * but not optimal for HDD.
2717 */
2718 if (dev->offload_to_mirror)
2719 map_index += map->data_disks_per_row;
2720 dev->offload_to_mirror = !dev->offload_to_mirror;
2721 }
2722 disk_handle = dd[map_index].ioaccel_handle;
2723 disk_block = map->disk_starting_blk + (first_row * map->strip_size) +
2724 (first_row_offset - (first_column * map->strip_size));
2725 disk_block_cnt = block_cnt;
2726
2727 /* handle differing logical/physical block sizes */
2728 if (map->phys_blk_shift) {
2729 disk_block <<= map->phys_blk_shift;
2730 disk_block_cnt <<= map->phys_blk_shift;
2731 }
2732 BUG_ON(disk_block_cnt > 0xffff);
2733
2734 /* build the new CDB for the physical disk I/O */
2735 if (disk_block > 0xffffffff) {
2736 cdb[0] = is_write ? WRITE_16 : READ_16;
2737 cdb[1] = 0;
2738 cdb[2] = (u8) (disk_block >> 56);
2739 cdb[3] = (u8) (disk_block >> 48);
2740 cdb[4] = (u8) (disk_block >> 40);
2741 cdb[5] = (u8) (disk_block >> 32);
2742 cdb[6] = (u8) (disk_block >> 24);
2743 cdb[7] = (u8) (disk_block >> 16);
2744 cdb[8] = (u8) (disk_block >> 8);
2745 cdb[9] = (u8) (disk_block);
2746 cdb[10] = (u8) (disk_block_cnt >> 24);
2747 cdb[11] = (u8) (disk_block_cnt >> 16);
2748 cdb[12] = (u8) (disk_block_cnt >> 8);
2749 cdb[13] = (u8) (disk_block_cnt);
2750 cdb[14] = 0;
2751 cdb[15] = 0;
2752 cdb_len = 16;
2753 } else {
2754 cdb[0] = is_write ? WRITE_10 : READ_10;
2755 cdb[1] = 0;
2756 cdb[2] = (u8) (disk_block >> 24);
2757 cdb[3] = (u8) (disk_block >> 16);
2758 cdb[4] = (u8) (disk_block >> 8);
2759 cdb[5] = (u8) (disk_block);
2760 cdb[6] = 0;
2761 cdb[7] = (u8) (disk_block_cnt >> 8);
2762 cdb[8] = (u8) (disk_block_cnt);
2763 cdb[9] = 0;
2764 cdb_len = 10;
2765 }
2766 return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
2767 dev->scsi3addr);
2768}
2769
Jeff Garzikf2812332010-11-16 02:10:29 -05002770static int hpsa_scsi_queue_command_lck(struct scsi_cmnd *cmd,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002771 void (*done)(struct scsi_cmnd *))
2772{
2773 struct ctlr_info *h;
2774 struct hpsa_scsi_dev_t *dev;
2775 unsigned char scsi3addr[8];
2776 struct CommandList *c;
2777 unsigned long flags;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002778 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002779
2780 /* Get the ptr to our adapter structure out of cmd->host. */
2781 h = sdev_to_hba(cmd->device);
2782 dev = cmd->device->hostdata;
2783 if (!dev) {
2784 cmd->result = DID_NO_CONNECT << 16;
2785 done(cmd);
2786 return 0;
2787 }
2788 memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
2789
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002790 spin_lock_irqsave(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05002791 if (unlikely(h->lockup_detected)) {
2792 spin_unlock_irqrestore(&h->lock, flags);
2793 cmd->result = DID_ERROR << 16;
2794 done(cmd);
2795 return 0;
2796 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002797 spin_unlock_irqrestore(&h->lock, flags);
Matt Gatese16a33a2012-05-01 11:43:11 -05002798 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002799 if (c == NULL) { /* trouble... */
2800 dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
2801 return SCSI_MLQUEUE_HOST_BUSY;
2802 }
2803
2804 /* Fill in the command list header */
2805
2806 cmd->scsi_done = done; /* save this for use by completion code */
2807
2808 /* save c in case we have to abort it */
2809 cmd->host_scribble = (unsigned char *) c;
2810
2811 c->cmd_type = CMD_SCSI;
2812 c->scsi_cmd = cmd;
Matt Gatese1f7de02014-02-18 13:55:17 -06002813
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002814 /* Call alternate submit routine for I/O accelerated commands.
2815 * Retries always go down the normal I/O path.
2816 */
2817 if (likely(cmd->retries == 0 &&
2818 cmd->request->cmd_type == REQ_TYPE_FS)) {
2819 if (dev->offload_enabled) {
2820 rc = hpsa_scsi_ioaccel_raid_map(h, c);
2821 if (rc == 0)
2822 return 0; /* Sent on ioaccel path */
2823 if (rc < 0) { /* scsi_dma_map failed. */
2824 cmd_free(h, c);
2825 return SCSI_MLQUEUE_HOST_BUSY;
2826 }
2827 } else if (dev->ioaccel_handle) {
2828 rc = hpsa_scsi_ioaccel_direct_map(h, c);
2829 if (rc == 0)
2830 return 0; /* Sent on direct map path */
2831 if (rc < 0) { /* scsi_dma_map failed. */
2832 cmd_free(h, c);
2833 return SCSI_MLQUEUE_HOST_BUSY;
2834 }
2835 }
2836 }
Matt Gatese1f7de02014-02-18 13:55:17 -06002837
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002838 c->Header.ReplyQueue = 0; /* unused in simple mode */
2839 memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
Don Brace303932f2010-02-04 08:42:40 -06002840 c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT);
2841 c->Header.Tag.lower |= DIRECT_LOOKUP_BIT;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002842
2843 /* Fill in the request block... */
2844
2845 c->Request.Timeout = 0;
2846 memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
2847 BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
2848 c->Request.CDBLen = cmd->cmd_len;
2849 memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
2850 c->Request.Type.Type = TYPE_CMD;
2851 c->Request.Type.Attribute = ATTR_SIMPLE;
2852 switch (cmd->sc_data_direction) {
2853 case DMA_TO_DEVICE:
2854 c->Request.Type.Direction = XFER_WRITE;
2855 break;
2856 case DMA_FROM_DEVICE:
2857 c->Request.Type.Direction = XFER_READ;
2858 break;
2859 case DMA_NONE:
2860 c->Request.Type.Direction = XFER_NONE;
2861 break;
2862 case DMA_BIDIRECTIONAL:
2863 /* This can happen if a buggy application does a scsi passthru
2864 * and sets both inlen and outlen to non-zero. ( see
2865 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
2866 */
2867
2868 c->Request.Type.Direction = XFER_RSVD;
2869 /* This is technically wrong, and hpsa controllers should
2870 * reject it with CMD_INVALID, which is the most correct
2871 * response, but non-fibre backends appear to let it
2872 * slide by, and give the same results as if this field
2873 * were set correctly. Either way is acceptable for
2874 * our purposes here.
2875 */
2876
2877 break;
2878
2879 default:
2880 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
2881 cmd->sc_data_direction);
2882 BUG();
2883 break;
2884 }
2885
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002886 if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002887 cmd_free(h, c);
2888 return SCSI_MLQUEUE_HOST_BUSY;
2889 }
2890 enqueue_cmd_and_start_io(h, c);
2891 /* the cmd'll come back via intr handler in complete_scsi_command() */
2892 return 0;
2893}
2894
Jeff Garzikf2812332010-11-16 02:10:29 -05002895static DEF_SCSI_QCMD(hpsa_scsi_queue_command)
2896
Stephen M. Cameron5f389362014-02-18 13:55:48 -06002897static int do_not_scan_if_controller_locked_up(struct ctlr_info *h)
2898{
2899 unsigned long flags;
2900
2901 /*
2902 * Don't let rescans be initiated on a controller known
2903 * to be locked up. If the controller locks up *during*
2904 * a rescan, that thread is probably hosed, but at least
2905 * we can prevent new rescan threads from piling up on a
2906 * locked up controller.
2907 */
2908 spin_lock_irqsave(&h->lock, flags);
2909 if (unlikely(h->lockup_detected)) {
2910 spin_unlock_irqrestore(&h->lock, flags);
2911 spin_lock_irqsave(&h->scan_lock, flags);
2912 h->scan_finished = 1;
2913 wake_up_all(&h->scan_wait_queue);
2914 spin_unlock_irqrestore(&h->scan_lock, flags);
2915 return 1;
2916 }
2917 spin_unlock_irqrestore(&h->lock, flags);
2918 return 0;
2919}
2920
Stephen M. Camerona08a8472010-02-04 08:43:16 -06002921static void hpsa_scan_start(struct Scsi_Host *sh)
2922{
2923 struct ctlr_info *h = shost_to_hba(sh);
2924 unsigned long flags;
2925
Stephen M. Cameron5f389362014-02-18 13:55:48 -06002926 if (do_not_scan_if_controller_locked_up(h))
2927 return;
2928
Stephen M. Camerona08a8472010-02-04 08:43:16 -06002929 /* wait until any scan already in progress is finished. */
2930 while (1) {
2931 spin_lock_irqsave(&h->scan_lock, flags);
2932 if (h->scan_finished)
2933 break;
2934 spin_unlock_irqrestore(&h->scan_lock, flags);
2935 wait_event(h->scan_wait_queue, h->scan_finished);
2936 /* Note: We don't need to worry about a race between this
2937 * thread and driver unload because the midlayer will
2938 * have incremented the reference count, so unload won't
2939 * happen if we're in here.
2940 */
2941 }
2942 h->scan_finished = 0; /* mark scan as in progress */
2943 spin_unlock_irqrestore(&h->scan_lock, flags);
2944
Stephen M. Cameron5f389362014-02-18 13:55:48 -06002945 if (do_not_scan_if_controller_locked_up(h))
2946 return;
2947
Stephen M. Camerona08a8472010-02-04 08:43:16 -06002948 hpsa_update_scsi_devices(h, h->scsi_host->host_no);
2949
2950 spin_lock_irqsave(&h->scan_lock, flags);
2951 h->scan_finished = 1; /* mark scan as finished. */
2952 wake_up_all(&h->scan_wait_queue);
2953 spin_unlock_irqrestore(&h->scan_lock, flags);
2954}
2955
2956static int hpsa_scan_finished(struct Scsi_Host *sh,
2957 unsigned long elapsed_time)
2958{
2959 struct ctlr_info *h = shost_to_hba(sh);
2960 unsigned long flags;
2961 int finished;
2962
2963 spin_lock_irqsave(&h->scan_lock, flags);
2964 finished = h->scan_finished;
2965 spin_unlock_irqrestore(&h->scan_lock, flags);
2966 return finished;
2967}
2968
Stephen M. Cameron667e23d2010-02-25 14:02:51 -06002969static int hpsa_change_queue_depth(struct scsi_device *sdev,
2970 int qdepth, int reason)
2971{
2972 struct ctlr_info *h = sdev_to_hba(sdev);
2973
2974 if (reason != SCSI_QDEPTH_DEFAULT)
2975 return -ENOTSUPP;
2976
2977 if (qdepth < 1)
2978 qdepth = 1;
2979 else
2980 if (qdepth > h->nr_cmds)
2981 qdepth = h->nr_cmds;
2982 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
2983 return sdev->queue_depth;
2984}
2985
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002986static void hpsa_unregister_scsi(struct ctlr_info *h)
2987{
2988 /* we are being forcibly unloaded, and may not refuse. */
2989 scsi_remove_host(h->scsi_host);
2990 scsi_host_put(h->scsi_host);
2991 h->scsi_host = NULL;
2992}
2993
2994static int hpsa_register_scsi(struct ctlr_info *h)
2995{
Stephen M. Cameronb7056902012-01-19 14:00:53 -06002996 struct Scsi_Host *sh;
2997 int error;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002998
Stephen M. Cameronb7056902012-01-19 14:00:53 -06002999 sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
3000 if (sh == NULL)
3001 goto fail;
3002
3003 sh->io_port = 0;
3004 sh->n_io_port = 0;
3005 sh->this_id = -1;
3006 sh->max_channel = 3;
3007 sh->max_cmd_len = MAX_COMMAND_SIZE;
3008 sh->max_lun = HPSA_MAX_LUN;
3009 sh->max_id = HPSA_MAX_LUN;
3010 sh->can_queue = h->nr_cmds;
3011 sh->cmd_per_lun = h->nr_cmds;
3012 sh->sg_tablesize = h->maxsgentries;
3013 h->scsi_host = sh;
3014 sh->hostdata[0] = (unsigned long) h;
3015 sh->irq = h->intr[h->intr_mode];
3016 sh->unique_id = sh->irq;
3017 error = scsi_add_host(sh, &h->pdev->dev);
3018 if (error)
3019 goto fail_host_put;
3020 scsi_scan_host(sh);
3021 return 0;
3022
3023 fail_host_put:
3024 dev_err(&h->pdev->dev, "%s: scsi_add_host"
3025 " failed for controller %d\n", __func__, h->ctlr);
3026 scsi_host_put(sh);
3027 return error;
3028 fail:
3029 dev_err(&h->pdev->dev, "%s: scsi_host_alloc"
3030 " failed for controller %d\n", __func__, h->ctlr);
3031 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003032}
3033
3034static int wait_for_device_to_become_ready(struct ctlr_info *h,
3035 unsigned char lunaddr[])
3036{
3037 int rc = 0;
3038 int count = 0;
3039 int waittime = 1; /* seconds */
3040 struct CommandList *c;
3041
3042 c = cmd_special_alloc(h);
3043 if (!c) {
3044 dev_warn(&h->pdev->dev, "out of memory in "
3045 "wait_for_device_to_become_ready.\n");
3046 return IO_ERROR;
3047 }
3048
3049 /* Send test unit ready until device ready, or give up. */
3050 while (count < HPSA_TUR_RETRY_LIMIT) {
3051
3052 /* Wait for a bit. do this first, because if we send
3053 * the TUR right away, the reset will just abort it.
3054 */
3055 msleep(1000 * waittime);
3056 count++;
3057
3058 /* Increase wait time with each try, up to a point. */
3059 if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
3060 waittime = waittime * 2;
3061
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003062 /* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
3063 (void) fill_cmd(c, TEST_UNIT_READY, h,
3064 NULL, 0, 0, lunaddr, TYPE_CMD);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003065 hpsa_scsi_do_simple_cmd_core(h, c);
3066 /* no unmap needed here because no data xfer. */
3067
3068 if (c->err_info->CommandStatus == CMD_SUCCESS)
3069 break;
3070
3071 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
3072 c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
3073 (c->err_info->SenseInfo[2] == NO_SENSE ||
3074 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
3075 break;
3076
3077 dev_warn(&h->pdev->dev, "waiting %d secs "
3078 "for device to become ready.\n", waittime);
3079 rc = 1; /* device not ready. */
3080 }
3081
3082 if (rc)
3083 dev_warn(&h->pdev->dev, "giving up on device.\n");
3084 else
3085 dev_warn(&h->pdev->dev, "device is ready.\n");
3086
3087 cmd_special_free(h, c);
3088 return rc;
3089}
3090
3091/* Need at least one of these error handlers to keep ../scsi/hosts.c from
3092 * complaining. Doing a host- or bus-reset can't do anything good here.
3093 */
3094static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
3095{
3096 int rc;
3097 struct ctlr_info *h;
3098 struct hpsa_scsi_dev_t *dev;
3099
3100 /* find the controller to which the command to be aborted was sent */
3101 h = sdev_to_hba(scsicmd->device);
3102 if (h == NULL) /* paranoia */
3103 return FAILED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003104 dev = scsicmd->device->hostdata;
3105 if (!dev) {
3106 dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
3107 "device lookup failed.\n");
3108 return FAILED;
3109 }
Stephen M. Camerond416b0c2010-02-04 08:43:21 -06003110 dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
3111 h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003112 /* send a reset to the SCSI LUN which the command was sent to */
3113 rc = hpsa_send_reset(h, dev->scsi3addr);
3114 if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
3115 return SUCCESS;
3116
3117 dev_warn(&h->pdev->dev, "resetting device failed.\n");
3118 return FAILED;
3119}
3120
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003121static void swizzle_abort_tag(u8 *tag)
3122{
3123 u8 original_tag[8];
3124
3125 memcpy(original_tag, tag, 8);
3126 tag[0] = original_tag[3];
3127 tag[1] = original_tag[2];
3128 tag[2] = original_tag[1];
3129 tag[3] = original_tag[0];
3130 tag[4] = original_tag[7];
3131 tag[5] = original_tag[6];
3132 tag[6] = original_tag[5];
3133 tag[7] = original_tag[4];
3134}
3135
Scott Teel17eb87d2014-02-18 13:55:28 -06003136static void hpsa_get_tag(struct ctlr_info *h,
3137 struct CommandList *c, u32 *taglower, u32 *tagupper)
3138{
3139 if (c->cmd_type == CMD_IOACCEL1) {
3140 struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
3141 &h->ioaccel_cmd_pool[c->cmdindex];
3142 *tagupper = cm1->Tag.upper;
3143 *taglower = cm1->Tag.lower;
3144 } else {
3145 *tagupper = c->Header.Tag.upper;
3146 *taglower = c->Header.Tag.lower;
3147 }
3148}
3149
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003150static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003151 struct CommandList *abort, int swizzle)
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003152{
3153 int rc = IO_OK;
3154 struct CommandList *c;
3155 struct ErrorInfo *ei;
Scott Teel17eb87d2014-02-18 13:55:28 -06003156 u32 tagupper, taglower;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003157
3158 c = cmd_special_alloc(h);
3159 if (c == NULL) { /* trouble... */
3160 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
3161 return -ENOMEM;
3162 }
3163
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003164 /* fill_cmd can't fail here, no buffer to map */
3165 (void) fill_cmd(c, HPSA_ABORT_MSG, h, abort,
3166 0, 0, scsi3addr, TYPE_MSG);
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003167 if (swizzle)
3168 swizzle_abort_tag(&c->Request.CDB[4]);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003169 hpsa_scsi_do_simple_cmd_core(h, c);
Scott Teel17eb87d2014-02-18 13:55:28 -06003170 hpsa_get_tag(h, abort, &taglower, &tagupper);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003171 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd_core completed.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06003172 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003173 /* no unmap needed here because no data xfer. */
3174
3175 ei = c->err_info;
3176 switch (ei->CommandStatus) {
3177 case CMD_SUCCESS:
3178 break;
3179 case CMD_UNABORTABLE: /* Very common, don't make noise. */
3180 rc = -1;
3181 break;
3182 default:
3183 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06003184 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003185 hpsa_scsi_interpret_error(c);
3186 rc = -1;
3187 break;
3188 }
3189 cmd_special_free(h, c);
3190 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n", __func__,
3191 abort->Header.Tag.upper, abort->Header.Tag.lower);
3192 return rc;
3193}
3194
3195/*
3196 * hpsa_find_cmd_in_queue
3197 *
3198 * Used to determine whether a command (find) is still present
3199 * in queue_head. Optionally excludes the last element of queue_head.
3200 *
3201 * This is used to avoid unnecessary aborts. Commands in h->reqQ have
3202 * not yet been submitted, and so can be aborted by the driver without
3203 * sending an abort to the hardware.
3204 *
3205 * Returns pointer to command if found in queue, NULL otherwise.
3206 */
3207static struct CommandList *hpsa_find_cmd_in_queue(struct ctlr_info *h,
3208 struct scsi_cmnd *find, struct list_head *queue_head)
3209{
3210 unsigned long flags;
3211 struct CommandList *c = NULL; /* ptr into cmpQ */
3212
3213 if (!find)
3214 return 0;
3215 spin_lock_irqsave(&h->lock, flags);
3216 list_for_each_entry(c, queue_head, list) {
3217 if (c->scsi_cmd == NULL) /* e.g.: passthru ioctl */
3218 continue;
3219 if (c->scsi_cmd == find) {
3220 spin_unlock_irqrestore(&h->lock, flags);
3221 return c;
3222 }
3223 }
3224 spin_unlock_irqrestore(&h->lock, flags);
3225 return NULL;
3226}
3227
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003228static struct CommandList *hpsa_find_cmd_in_queue_by_tag(struct ctlr_info *h,
3229 u8 *tag, struct list_head *queue_head)
3230{
3231 unsigned long flags;
3232 struct CommandList *c;
3233
3234 spin_lock_irqsave(&h->lock, flags);
3235 list_for_each_entry(c, queue_head, list) {
3236 if (memcmp(&c->Header.Tag, tag, 8) != 0)
3237 continue;
3238 spin_unlock_irqrestore(&h->lock, flags);
3239 return c;
3240 }
3241 spin_unlock_irqrestore(&h->lock, flags);
3242 return NULL;
3243}
3244
3245/* Some Smart Arrays need the abort tag swizzled, and some don't. It's hard to
3246 * tell which kind we're dealing with, so we send the abort both ways. There
3247 * shouldn't be any collisions between swizzled and unswizzled tags due to the
3248 * way we construct our tags but we check anyway in case the assumptions which
3249 * make this true someday become false.
3250 */
3251static int hpsa_send_abort_both_ways(struct ctlr_info *h,
3252 unsigned char *scsi3addr, struct CommandList *abort)
3253{
3254 u8 swizzled_tag[8];
3255 struct CommandList *c;
3256 int rc = 0, rc2 = 0;
3257
3258 /* we do not expect to find the swizzled tag in our queue, but
3259 * check anyway just to be sure the assumptions which make this
3260 * the case haven't become wrong.
3261 */
3262 memcpy(swizzled_tag, &abort->Request.CDB[4], 8);
3263 swizzle_abort_tag(swizzled_tag);
3264 c = hpsa_find_cmd_in_queue_by_tag(h, swizzled_tag, &h->cmpQ);
3265 if (c != NULL) {
3266 dev_warn(&h->pdev->dev, "Unexpectedly found byte-swapped tag in completion queue.\n");
3267 return hpsa_send_abort(h, scsi3addr, abort, 0);
3268 }
3269 rc = hpsa_send_abort(h, scsi3addr, abort, 0);
3270
3271 /* if the command is still in our queue, we can't conclude that it was
3272 * aborted (it might have just completed normally) but in any case
3273 * we don't need to try to abort it another way.
3274 */
3275 c = hpsa_find_cmd_in_queue(h, abort->scsi_cmd, &h->cmpQ);
3276 if (c)
3277 rc2 = hpsa_send_abort(h, scsi3addr, abort, 1);
3278 return rc && rc2;
3279}
3280
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003281/* Send an abort for the specified command.
3282 * If the device and controller support it,
3283 * send a task abort request.
3284 */
3285static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
3286{
3287
3288 int i, rc;
3289 struct ctlr_info *h;
3290 struct hpsa_scsi_dev_t *dev;
3291 struct CommandList *abort; /* pointer to command to be aborted */
3292 struct CommandList *found;
3293 struct scsi_cmnd *as; /* ptr to scsi cmd inside aborted command. */
3294 char msg[256]; /* For debug messaging. */
3295 int ml = 0;
Scott Teel17eb87d2014-02-18 13:55:28 -06003296 u32 tagupper, taglower;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003297
3298 /* Find the controller of the command to be aborted */
3299 h = sdev_to_hba(sc->device);
3300 if (WARN(h == NULL,
3301 "ABORT REQUEST FAILED, Controller lookup failed.\n"))
3302 return FAILED;
3303
3304 /* Check that controller supports some kind of task abort */
3305 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
3306 !(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
3307 return FAILED;
3308
3309 memset(msg, 0, sizeof(msg));
3310 ml += sprintf(msg+ml, "ABORT REQUEST on C%d:B%d:T%d:L%d ",
3311 h->scsi_host->host_no, sc->device->channel,
3312 sc->device->id, sc->device->lun);
3313
3314 /* Find the device of the command to be aborted */
3315 dev = sc->device->hostdata;
3316 if (!dev) {
3317 dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
3318 msg);
3319 return FAILED;
3320 }
3321
3322 /* Get SCSI command to be aborted */
3323 abort = (struct CommandList *) sc->host_scribble;
3324 if (abort == NULL) {
3325 dev_err(&h->pdev->dev, "%s FAILED, Command to abort is NULL.\n",
3326 msg);
3327 return FAILED;
3328 }
Scott Teel17eb87d2014-02-18 13:55:28 -06003329 hpsa_get_tag(h, abort, &taglower, &tagupper);
3330 ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003331 as = (struct scsi_cmnd *) abort->scsi_cmd;
3332 if (as != NULL)
3333 ml += sprintf(msg+ml, "Command:0x%x SN:0x%lx ",
3334 as->cmnd[0], as->serial_number);
3335 dev_dbg(&h->pdev->dev, "%s\n", msg);
3336 dev_warn(&h->pdev->dev, "Abort request on C%d:B%d:T%d:L%d\n",
3337 h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
3338
3339 /* Search reqQ to See if command is queued but not submitted,
3340 * if so, complete the command with aborted status and remove
3341 * it from the reqQ.
3342 */
3343 found = hpsa_find_cmd_in_queue(h, sc, &h->reqQ);
3344 if (found) {
3345 found->err_info->CommandStatus = CMD_ABORTED;
3346 finish_cmd(found);
3347 dev_info(&h->pdev->dev, "%s Request SUCCEEDED (driver queue).\n",
3348 msg);
3349 return SUCCESS;
3350 }
3351
3352 /* not in reqQ, if also not in cmpQ, must have already completed */
3353 found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
3354 if (!found) {
Stephen M. Camerond6ebd0f2012-07-26 11:34:17 -05003355 dev_dbg(&h->pdev->dev, "%s Request SUCCEEDED (not known to driver).\n",
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003356 msg);
3357 return SUCCESS;
3358 }
3359
3360 /*
3361 * Command is in flight, or possibly already completed
3362 * by the firmware (but not to the scsi mid layer) but we can't
3363 * distinguish which. Send the abort down.
3364 */
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003365 rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003366 if (rc != 0) {
3367 dev_dbg(&h->pdev->dev, "%s Request FAILED.\n", msg);
3368 dev_warn(&h->pdev->dev, "FAILED abort on device C%d:B%d:T%d:L%d\n",
3369 h->scsi_host->host_no,
3370 dev->bus, dev->target, dev->lun);
3371 return FAILED;
3372 }
3373 dev_info(&h->pdev->dev, "%s REQUEST SUCCEEDED.\n", msg);
3374
3375 /* If the abort(s) above completed and actually aborted the
3376 * command, then the command to be aborted should already be
3377 * completed. If not, wait around a bit more to see if they
3378 * manage to complete normally.
3379 */
3380#define ABORT_COMPLETE_WAIT_SECS 30
3381 for (i = 0; i < ABORT_COMPLETE_WAIT_SECS * 10; i++) {
3382 found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
3383 if (!found)
3384 return SUCCESS;
3385 msleep(100);
3386 }
3387 dev_warn(&h->pdev->dev, "%s FAILED. Aborted command has not completed after %d seconds.\n",
3388 msg, ABORT_COMPLETE_WAIT_SECS);
3389 return FAILED;
3390}
3391
3392
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003393/*
3394 * For operations that cannot sleep, a command block is allocated at init,
3395 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
3396 * which ones are free or in use. Lock must be held when calling this.
3397 * cmd_free() is the complement.
3398 */
3399static struct CommandList *cmd_alloc(struct ctlr_info *h)
3400{
3401 struct CommandList *c;
3402 int i;
3403 union u64bit temp64;
3404 dma_addr_t cmd_dma_handle, err_dma_handle;
Matt Gatese16a33a2012-05-01 11:43:11 -05003405 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003406
Matt Gatese16a33a2012-05-01 11:43:11 -05003407 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003408 do {
3409 i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
Matt Gatese16a33a2012-05-01 11:43:11 -05003410 if (i == h->nr_cmds) {
3411 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003412 return NULL;
Matt Gatese16a33a2012-05-01 11:43:11 -05003413 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003414 } while (test_and_set_bit
3415 (i & (BITS_PER_LONG - 1),
3416 h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
Matt Gatese16a33a2012-05-01 11:43:11 -05003417 spin_unlock_irqrestore(&h->lock, flags);
3418
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003419 c = h->cmd_pool + i;
3420 memset(c, 0, sizeof(*c));
3421 cmd_dma_handle = h->cmd_pool_dhandle
3422 + i * sizeof(*c);
3423 c->err_info = h->errinfo_pool + i;
3424 memset(c->err_info, 0, sizeof(*c->err_info));
3425 err_dma_handle = h->errinfo_pool_dhandle
3426 + i * sizeof(*c->err_info);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003427
3428 c->cmdindex = i;
3429
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06003430 INIT_LIST_HEAD(&c->list);
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06003431 c->busaddr = (u32) cmd_dma_handle;
3432 temp64.val = (u64) err_dma_handle;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003433 c->ErrDesc.Addr.lower = temp64.val32.lower;
3434 c->ErrDesc.Addr.upper = temp64.val32.upper;
3435 c->ErrDesc.Len = sizeof(*c->err_info);
3436
3437 c->h = h;
3438 return c;
3439}
3440
3441/* For operations that can wait for kmalloc to possibly sleep,
3442 * this routine can be called. Lock need not be held to call
3443 * cmd_special_alloc. cmd_special_free() is the complement.
3444 */
3445static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
3446{
3447 struct CommandList *c;
3448 union u64bit temp64;
3449 dma_addr_t cmd_dma_handle, err_dma_handle;
3450
3451 c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
3452 if (c == NULL)
3453 return NULL;
3454 memset(c, 0, sizeof(*c));
3455
Matt Gatese1f7de02014-02-18 13:55:17 -06003456 c->cmd_type = CMD_SCSI;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003457 c->cmdindex = -1;
3458
3459 c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info),
3460 &err_dma_handle);
3461
3462 if (c->err_info == NULL) {
3463 pci_free_consistent(h->pdev,
3464 sizeof(*c), c, cmd_dma_handle);
3465 return NULL;
3466 }
3467 memset(c->err_info, 0, sizeof(*c->err_info));
3468
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06003469 INIT_LIST_HEAD(&c->list);
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06003470 c->busaddr = (u32) cmd_dma_handle;
3471 temp64.val = (u64) err_dma_handle;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003472 c->ErrDesc.Addr.lower = temp64.val32.lower;
3473 c->ErrDesc.Addr.upper = temp64.val32.upper;
3474 c->ErrDesc.Len = sizeof(*c->err_info);
3475
3476 c->h = h;
3477 return c;
3478}
3479
3480static void cmd_free(struct ctlr_info *h, struct CommandList *c)
3481{
3482 int i;
Matt Gatese16a33a2012-05-01 11:43:11 -05003483 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003484
3485 i = c - h->cmd_pool;
Matt Gatese16a33a2012-05-01 11:43:11 -05003486 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003487 clear_bit(i & (BITS_PER_LONG - 1),
3488 h->cmd_pool_bits + (i / BITS_PER_LONG));
Matt Gatese16a33a2012-05-01 11:43:11 -05003489 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003490}
3491
3492static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
3493{
3494 union u64bit temp64;
3495
3496 temp64.val32.lower = c->ErrDesc.Addr.lower;
3497 temp64.val32.upper = c->ErrDesc.Addr.upper;
3498 pci_free_consistent(h->pdev, sizeof(*c->err_info),
3499 c->err_info, (dma_addr_t) temp64.val);
3500 pci_free_consistent(h->pdev, sizeof(*c),
Stephen M. Camerond896f3f2011-01-06 14:47:53 -06003501 c, (dma_addr_t) (c->busaddr & DIRECT_LOOKUP_MASK));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003502}
3503
3504#ifdef CONFIG_COMPAT
3505
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003506static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
3507{
3508 IOCTL32_Command_struct __user *arg32 =
3509 (IOCTL32_Command_struct __user *) arg;
3510 IOCTL_Command_struct arg64;
3511 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
3512 int err;
3513 u32 cp;
3514
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06003515 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003516 err = 0;
3517 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
3518 sizeof(arg64.LUN_info));
3519 err |= copy_from_user(&arg64.Request, &arg32->Request,
3520 sizeof(arg64.Request));
3521 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
3522 sizeof(arg64.error_info));
3523 err |= get_user(arg64.buf_size, &arg32->buf_size);
3524 err |= get_user(cp, &arg32->buf);
3525 arg64.buf = compat_ptr(cp);
3526 err |= copy_to_user(p, &arg64, sizeof(arg64));
3527
3528 if (err)
3529 return -EFAULT;
3530
Stephen M. Camerone39eeae2010-02-04 08:43:46 -06003531 err = hpsa_ioctl(dev, CCISS_PASSTHRU, (void *)p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003532 if (err)
3533 return err;
3534 err |= copy_in_user(&arg32->error_info, &p->error_info,
3535 sizeof(arg32->error_info));
3536 if (err)
3537 return -EFAULT;
3538 return err;
3539}
3540
3541static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
3542 int cmd, void *arg)
3543{
3544 BIG_IOCTL32_Command_struct __user *arg32 =
3545 (BIG_IOCTL32_Command_struct __user *) arg;
3546 BIG_IOCTL_Command_struct arg64;
3547 BIG_IOCTL_Command_struct __user *p =
3548 compat_alloc_user_space(sizeof(arg64));
3549 int err;
3550 u32 cp;
3551
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06003552 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003553 err = 0;
3554 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
3555 sizeof(arg64.LUN_info));
3556 err |= copy_from_user(&arg64.Request, &arg32->Request,
3557 sizeof(arg64.Request));
3558 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
3559 sizeof(arg64.error_info));
3560 err |= get_user(arg64.buf_size, &arg32->buf_size);
3561 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
3562 err |= get_user(cp, &arg32->buf);
3563 arg64.buf = compat_ptr(cp);
3564 err |= copy_to_user(p, &arg64, sizeof(arg64));
3565
3566 if (err)
3567 return -EFAULT;
3568
Stephen M. Camerone39eeae2010-02-04 08:43:46 -06003569 err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003570 if (err)
3571 return err;
3572 err |= copy_in_user(&arg32->error_info, &p->error_info,
3573 sizeof(arg32->error_info));
3574 if (err)
3575 return -EFAULT;
3576 return err;
3577}
Stephen M. Cameron71fe75a2010-02-04 08:43:51 -06003578
3579static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
3580{
3581 switch (cmd) {
3582 case CCISS_GETPCIINFO:
3583 case CCISS_GETINTINFO:
3584 case CCISS_SETINTINFO:
3585 case CCISS_GETNODENAME:
3586 case CCISS_SETNODENAME:
3587 case CCISS_GETHEARTBEAT:
3588 case CCISS_GETBUSTYPES:
3589 case CCISS_GETFIRMVER:
3590 case CCISS_GETDRIVVER:
3591 case CCISS_REVALIDVOLS:
3592 case CCISS_DEREGDISK:
3593 case CCISS_REGNEWDISK:
3594 case CCISS_REGNEWD:
3595 case CCISS_RESCANDISK:
3596 case CCISS_GETLUNINFO:
3597 return hpsa_ioctl(dev, cmd, arg);
3598
3599 case CCISS_PASSTHRU32:
3600 return hpsa_ioctl32_passthru(dev, cmd, arg);
3601 case CCISS_BIG_PASSTHRU32:
3602 return hpsa_ioctl32_big_passthru(dev, cmd, arg);
3603
3604 default:
3605 return -ENOIOCTLCMD;
3606 }
3607}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003608#endif
3609
3610static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
3611{
3612 struct hpsa_pci_info pciinfo;
3613
3614 if (!argp)
3615 return -EINVAL;
3616 pciinfo.domain = pci_domain_nr(h->pdev->bus);
3617 pciinfo.bus = h->pdev->bus->number;
3618 pciinfo.dev_fn = h->pdev->devfn;
3619 pciinfo.board_id = h->board_id;
3620 if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
3621 return -EFAULT;
3622 return 0;
3623}
3624
3625static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
3626{
3627 DriverVer_type DriverVer;
3628 unsigned char vmaj, vmin, vsubmin;
3629 int rc;
3630
3631 rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
3632 &vmaj, &vmin, &vsubmin);
3633 if (rc != 3) {
3634 dev_info(&h->pdev->dev, "driver version string '%s' "
3635 "unrecognized.", HPSA_DRIVER_VERSION);
3636 vmaj = 0;
3637 vmin = 0;
3638 vsubmin = 0;
3639 }
3640 DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
3641 if (!argp)
3642 return -EINVAL;
3643 if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
3644 return -EFAULT;
3645 return 0;
3646}
3647
3648static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
3649{
3650 IOCTL_Command_struct iocommand;
3651 struct CommandList *c;
3652 char *buff = NULL;
3653 union u64bit temp64;
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003654 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003655
3656 if (!argp)
3657 return -EINVAL;
3658 if (!capable(CAP_SYS_RAWIO))
3659 return -EPERM;
3660 if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
3661 return -EFAULT;
3662 if ((iocommand.buf_size < 1) &&
3663 (iocommand.Request.Type.Direction != XFER_NONE)) {
3664 return -EINVAL;
3665 }
3666 if (iocommand.buf_size > 0) {
3667 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
3668 if (buff == NULL)
3669 return -EFAULT;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003670 if (iocommand.Request.Type.Direction == XFER_WRITE) {
3671 /* Copy the data into the buffer we created */
3672 if (copy_from_user(buff, iocommand.buf,
3673 iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003674 rc = -EFAULT;
3675 goto out_kfree;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003676 }
3677 } else {
3678 memset(buff, 0, iocommand.buf_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003679 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003680 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003681 c = cmd_special_alloc(h);
3682 if (c == NULL) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003683 rc = -ENOMEM;
3684 goto out_kfree;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003685 }
3686 /* Fill in the command type */
3687 c->cmd_type = CMD_IOCTL_PEND;
3688 /* Fill in Command Header */
3689 c->Header.ReplyQueue = 0; /* unused in simple mode */
3690 if (iocommand.buf_size > 0) { /* buffer to fill */
3691 c->Header.SGList = 1;
3692 c->Header.SGTotal = 1;
3693 } else { /* no buffers to fill */
3694 c->Header.SGList = 0;
3695 c->Header.SGTotal = 0;
3696 }
3697 memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
3698 /* use the kernel address the cmd block for tag */
3699 c->Header.Tag.lower = c->busaddr;
3700
3701 /* Fill in Request block */
3702 memcpy(&c->Request, &iocommand.Request,
3703 sizeof(c->Request));
3704
3705 /* Fill in the scatter gather information */
3706 if (iocommand.buf_size > 0) {
3707 temp64.val = pci_map_single(h->pdev, buff,
3708 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06003709 if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
3710 c->SG[0].Addr.lower = 0;
3711 c->SG[0].Addr.upper = 0;
3712 c->SG[0].Len = 0;
3713 rc = -ENOMEM;
3714 goto out;
3715 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003716 c->SG[0].Addr.lower = temp64.val32.lower;
3717 c->SG[0].Addr.upper = temp64.val32.upper;
3718 c->SG[0].Len = iocommand.buf_size;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06003719 c->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining*/
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003720 }
Stephen M. Camerona0c12412011-10-26 16:22:04 -05003721 hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
Stephen M. Cameronc2dd32e2011-06-03 09:57:29 -05003722 if (iocommand.buf_size > 0)
3723 hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003724 check_ioctl_unit_attention(h, c);
3725
3726 /* Copy the error information out */
3727 memcpy(&iocommand.error_info, c->err_info,
3728 sizeof(iocommand.error_info));
3729 if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003730 rc = -EFAULT;
3731 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003732 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003733 if (iocommand.Request.Type.Direction == XFER_READ &&
3734 iocommand.buf_size > 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003735 /* Copy the data out of the buffer we created */
3736 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003737 rc = -EFAULT;
3738 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003739 }
3740 }
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003741out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003742 cmd_special_free(h, c);
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003743out_kfree:
3744 kfree(buff);
3745 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003746}
3747
3748static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
3749{
3750 BIG_IOCTL_Command_struct *ioc;
3751 struct CommandList *c;
3752 unsigned char **buff = NULL;
3753 int *buff_size = NULL;
3754 union u64bit temp64;
3755 BYTE sg_used = 0;
3756 int status = 0;
3757 int i;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06003758 u32 left;
3759 u32 sz;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003760 BYTE __user *data_ptr;
3761
3762 if (!argp)
3763 return -EINVAL;
3764 if (!capable(CAP_SYS_RAWIO))
3765 return -EPERM;
3766 ioc = (BIG_IOCTL_Command_struct *)
3767 kmalloc(sizeof(*ioc), GFP_KERNEL);
3768 if (!ioc) {
3769 status = -ENOMEM;
3770 goto cleanup1;
3771 }
3772 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
3773 status = -EFAULT;
3774 goto cleanup1;
3775 }
3776 if ((ioc->buf_size < 1) &&
3777 (ioc->Request.Type.Direction != XFER_NONE)) {
3778 status = -EINVAL;
3779 goto cleanup1;
3780 }
3781 /* Check kmalloc limits using all SGs */
3782 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
3783 status = -EINVAL;
3784 goto cleanup1;
3785 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06003786 if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003787 status = -EINVAL;
3788 goto cleanup1;
3789 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06003790 buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003791 if (!buff) {
3792 status = -ENOMEM;
3793 goto cleanup1;
3794 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06003795 buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003796 if (!buff_size) {
3797 status = -ENOMEM;
3798 goto cleanup1;
3799 }
3800 left = ioc->buf_size;
3801 data_ptr = ioc->buf;
3802 while (left) {
3803 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
3804 buff_size[sg_used] = sz;
3805 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
3806 if (buff[sg_used] == NULL) {
3807 status = -ENOMEM;
3808 goto cleanup1;
3809 }
3810 if (ioc->Request.Type.Direction == XFER_WRITE) {
3811 if (copy_from_user(buff[sg_used], data_ptr, sz)) {
3812 status = -ENOMEM;
3813 goto cleanup1;
3814 }
3815 } else
3816 memset(buff[sg_used], 0, sz);
3817 left -= sz;
3818 data_ptr += sz;
3819 sg_used++;
3820 }
3821 c = cmd_special_alloc(h);
3822 if (c == NULL) {
3823 status = -ENOMEM;
3824 goto cleanup1;
3825 }
3826 c->cmd_type = CMD_IOCTL_PEND;
3827 c->Header.ReplyQueue = 0;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003828 c->Header.SGList = c->Header.SGTotal = sg_used;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003829 memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
3830 c->Header.Tag.lower = c->busaddr;
3831 memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
3832 if (ioc->buf_size > 0) {
3833 int i;
3834 for (i = 0; i < sg_used; i++) {
3835 temp64.val = pci_map_single(h->pdev, buff[i],
3836 buff_size[i], PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06003837 if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
3838 c->SG[i].Addr.lower = 0;
3839 c->SG[i].Addr.upper = 0;
3840 c->SG[i].Len = 0;
3841 hpsa_pci_unmap(h->pdev, c, i,
3842 PCI_DMA_BIDIRECTIONAL);
3843 status = -ENOMEM;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05003844 goto cleanup0;
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06003845 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003846 c->SG[i].Addr.lower = temp64.val32.lower;
3847 c->SG[i].Addr.upper = temp64.val32.upper;
3848 c->SG[i].Len = buff_size[i];
Matt Gatese1d9cbf2014-02-18 13:55:12 -06003849 c->SG[i].Ext = i < sg_used - 1 ? 0 : HPSA_SG_LAST;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003850 }
3851 }
Stephen M. Camerona0c12412011-10-26 16:22:04 -05003852 hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003853 if (sg_used)
3854 hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003855 check_ioctl_unit_attention(h, c);
3856 /* Copy the error information out */
3857 memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
3858 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003859 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05003860 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003861 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003862 if (ioc->Request.Type.Direction == XFER_READ && ioc->buf_size > 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003863 /* Copy the data out of the buffer we created */
3864 BYTE __user *ptr = ioc->buf;
3865 for (i = 0; i < sg_used; i++) {
3866 if (copy_to_user(ptr, buff[i], buff_size[i])) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003867 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05003868 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003869 }
3870 ptr += buff_size[i];
3871 }
3872 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003873 status = 0;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05003874cleanup0:
3875 cmd_special_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003876cleanup1:
3877 if (buff) {
3878 for (i = 0; i < sg_used; i++)
3879 kfree(buff[i]);
3880 kfree(buff);
3881 }
3882 kfree(buff_size);
3883 kfree(ioc);
3884 return status;
3885}
3886
3887static void check_ioctl_unit_attention(struct ctlr_info *h,
3888 struct CommandList *c)
3889{
3890 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
3891 c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
3892 (void) check_for_unit_attention(h, c);
3893}
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05003894
3895static int increment_passthru_count(struct ctlr_info *h)
3896{
3897 unsigned long flags;
3898
3899 spin_lock_irqsave(&h->passthru_count_lock, flags);
3900 if (h->passthru_count >= HPSA_MAX_CONCURRENT_PASSTHRUS) {
3901 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
3902 return -1;
3903 }
3904 h->passthru_count++;
3905 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
3906 return 0;
3907}
3908
3909static void decrement_passthru_count(struct ctlr_info *h)
3910{
3911 unsigned long flags;
3912
3913 spin_lock_irqsave(&h->passthru_count_lock, flags);
3914 if (h->passthru_count <= 0) {
3915 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
3916 /* not expecting to get here. */
3917 dev_warn(&h->pdev->dev, "Bug detected, passthru_count seems to be incorrect.\n");
3918 return;
3919 }
3920 h->passthru_count--;
3921 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
3922}
3923
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003924/*
3925 * ioctl
3926 */
3927static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
3928{
3929 struct ctlr_info *h;
3930 void __user *argp = (void __user *)arg;
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05003931 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003932
3933 h = sdev_to_hba(dev);
3934
3935 switch (cmd) {
3936 case CCISS_DEREGDISK:
3937 case CCISS_REGNEWDISK:
3938 case CCISS_REGNEWD:
Stephen M. Camerona08a8472010-02-04 08:43:16 -06003939 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003940 return 0;
3941 case CCISS_GETPCIINFO:
3942 return hpsa_getpciinfo_ioctl(h, argp);
3943 case CCISS_GETDRIVVER:
3944 return hpsa_getdrivver_ioctl(h, argp);
3945 case CCISS_PASSTHRU:
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05003946 if (increment_passthru_count(h))
3947 return -EAGAIN;
3948 rc = hpsa_passthru_ioctl(h, argp);
3949 decrement_passthru_count(h);
3950 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003951 case CCISS_BIG_PASSTHRU:
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05003952 if (increment_passthru_count(h))
3953 return -EAGAIN;
3954 rc = hpsa_big_passthru_ioctl(h, argp);
3955 decrement_passthru_count(h);
3956 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003957 default:
3958 return -ENOTTY;
3959 }
3960}
3961
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08003962static int hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
3963 u8 reset_type)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05003964{
3965 struct CommandList *c;
3966
3967 c = cmd_alloc(h);
3968 if (!c)
3969 return -ENOMEM;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003970 /* fill_cmd can't fail here, no data buffer to map */
3971 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05003972 RAID_CTLR_LUNID, TYPE_MSG);
3973 c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
3974 c->waiting = NULL;
3975 enqueue_cmd_and_start_io(h, c);
3976 /* Don't wait for completion, the reset won't complete. Don't free
3977 * the command either. This is the last command we will send before
3978 * re-initializing everything, so it doesn't matter and won't leak.
3979 */
3980 return 0;
3981}
3982
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003983static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06003984 void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003985 int cmd_type)
3986{
3987 int pci_dir = XFER_NONE;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003988 struct CommandList *a; /* for commands to be aborted */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003989
3990 c->cmd_type = CMD_IOCTL_PEND;
3991 c->Header.ReplyQueue = 0;
3992 if (buff != NULL && size > 0) {
3993 c->Header.SGList = 1;
3994 c->Header.SGTotal = 1;
3995 } else {
3996 c->Header.SGList = 0;
3997 c->Header.SGTotal = 0;
3998 }
3999 c->Header.Tag.lower = c->busaddr;
4000 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
4001
4002 c->Request.Type.Type = cmd_type;
4003 if (cmd_type == TYPE_CMD) {
4004 switch (cmd) {
4005 case HPSA_INQUIRY:
4006 /* are we trying to read a vital product page */
4007 if (page_code != 0) {
4008 c->Request.CDB[1] = 0x01;
4009 c->Request.CDB[2] = page_code;
4010 }
4011 c->Request.CDBLen = 6;
4012 c->Request.Type.Attribute = ATTR_SIMPLE;
4013 c->Request.Type.Direction = XFER_READ;
4014 c->Request.Timeout = 0;
4015 c->Request.CDB[0] = HPSA_INQUIRY;
4016 c->Request.CDB[4] = size & 0xFF;
4017 break;
4018 case HPSA_REPORT_LOG:
4019 case HPSA_REPORT_PHYS:
4020 /* Talking to controller so It's a physical command
4021 mode = 00 target = 0. Nothing to write.
4022 */
4023 c->Request.CDBLen = 12;
4024 c->Request.Type.Attribute = ATTR_SIMPLE;
4025 c->Request.Type.Direction = XFER_READ;
4026 c->Request.Timeout = 0;
4027 c->Request.CDB[0] = cmd;
4028 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
4029 c->Request.CDB[7] = (size >> 16) & 0xFF;
4030 c->Request.CDB[8] = (size >> 8) & 0xFF;
4031 c->Request.CDB[9] = size & 0xFF;
4032 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004033 case HPSA_CACHE_FLUSH:
4034 c->Request.CDBLen = 12;
4035 c->Request.Type.Attribute = ATTR_SIMPLE;
4036 c->Request.Type.Direction = XFER_WRITE;
4037 c->Request.Timeout = 0;
4038 c->Request.CDB[0] = BMIC_WRITE;
4039 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
Stephen M. Cameronbb158ea2011-10-26 16:21:17 -05004040 c->Request.CDB[7] = (size >> 8) & 0xFF;
4041 c->Request.CDB[8] = size & 0xFF;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004042 break;
4043 case TEST_UNIT_READY:
4044 c->Request.CDBLen = 6;
4045 c->Request.Type.Attribute = ATTR_SIMPLE;
4046 c->Request.Type.Direction = XFER_NONE;
4047 c->Request.Timeout = 0;
4048 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004049 case HPSA_GET_RAID_MAP:
4050 c->Request.CDBLen = 12;
4051 c->Request.Type.Attribute = ATTR_SIMPLE;
4052 c->Request.Type.Direction = XFER_READ;
4053 c->Request.Timeout = 0;
4054 c->Request.CDB[0] = HPSA_CISS_READ;
4055 c->Request.CDB[1] = cmd;
4056 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
4057 c->Request.CDB[7] = (size >> 16) & 0xFF;
4058 c->Request.CDB[8] = (size >> 8) & 0xFF;
4059 c->Request.CDB[9] = size & 0xFF;
4060 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004061 default:
4062 dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
4063 BUG();
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004064 return -1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004065 }
4066 } else if (cmd_type == TYPE_MSG) {
4067 switch (cmd) {
4068
4069 case HPSA_DEVICE_RESET_MSG:
4070 c->Request.CDBLen = 16;
4071 c->Request.Type.Type = 1; /* It is a MSG not a CMD */
4072 c->Request.Type.Attribute = ATTR_SIMPLE;
4073 c->Request.Type.Direction = XFER_NONE;
4074 c->Request.Timeout = 0; /* Don't time out */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004075 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
4076 c->Request.CDB[0] = cmd;
Stephen M. Cameron21e89af2012-07-26 11:34:10 -05004077 c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004078 /* If bytes 4-7 are zero, it means reset the */
4079 /* LunID device */
4080 c->Request.CDB[4] = 0x00;
4081 c->Request.CDB[5] = 0x00;
4082 c->Request.CDB[6] = 0x00;
4083 c->Request.CDB[7] = 0x00;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004084 break;
4085 case HPSA_ABORT_MSG:
4086 a = buff; /* point to command to be aborted */
4087 dev_dbg(&h->pdev->dev, "Abort Tag:0x%08x:%08x using request Tag:0x%08x:%08x\n",
4088 a->Header.Tag.upper, a->Header.Tag.lower,
4089 c->Header.Tag.upper, c->Header.Tag.lower);
4090 c->Request.CDBLen = 16;
4091 c->Request.Type.Type = TYPE_MSG;
4092 c->Request.Type.Attribute = ATTR_SIMPLE;
4093 c->Request.Type.Direction = XFER_WRITE;
4094 c->Request.Timeout = 0; /* Don't time out */
4095 c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
4096 c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
4097 c->Request.CDB[2] = 0x00; /* reserved */
4098 c->Request.CDB[3] = 0x00; /* reserved */
4099 /* Tag to abort goes in CDB[4]-CDB[11] */
4100 c->Request.CDB[4] = a->Header.Tag.lower & 0xFF;
4101 c->Request.CDB[5] = (a->Header.Tag.lower >> 8) & 0xFF;
4102 c->Request.CDB[6] = (a->Header.Tag.lower >> 16) & 0xFF;
4103 c->Request.CDB[7] = (a->Header.Tag.lower >> 24) & 0xFF;
4104 c->Request.CDB[8] = a->Header.Tag.upper & 0xFF;
4105 c->Request.CDB[9] = (a->Header.Tag.upper >> 8) & 0xFF;
4106 c->Request.CDB[10] = (a->Header.Tag.upper >> 16) & 0xFF;
4107 c->Request.CDB[11] = (a->Header.Tag.upper >> 24) & 0xFF;
4108 c->Request.CDB[12] = 0x00; /* reserved */
4109 c->Request.CDB[13] = 0x00; /* reserved */
4110 c->Request.CDB[14] = 0x00; /* reserved */
4111 c->Request.CDB[15] = 0x00; /* reserved */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004112 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004113 default:
4114 dev_warn(&h->pdev->dev, "unknown message type %d\n",
4115 cmd);
4116 BUG();
4117 }
4118 } else {
4119 dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
4120 BUG();
4121 }
4122
4123 switch (c->Request.Type.Direction) {
4124 case XFER_READ:
4125 pci_dir = PCI_DMA_FROMDEVICE;
4126 break;
4127 case XFER_WRITE:
4128 pci_dir = PCI_DMA_TODEVICE;
4129 break;
4130 case XFER_NONE:
4131 pci_dir = PCI_DMA_NONE;
4132 break;
4133 default:
4134 pci_dir = PCI_DMA_BIDIRECTIONAL;
4135 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004136 if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
4137 return -1;
4138 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004139}
4140
4141/*
4142 * Map (physical) PCI mem into (virtual) kernel space
4143 */
4144static void __iomem *remap_pci_mem(ulong base, ulong size)
4145{
4146 ulong page_base = ((ulong) base) & PAGE_MASK;
4147 ulong page_offs = ((ulong) base) - page_base;
Stephen M. Cameron088ba34c2012-07-26 11:34:23 -05004148 void __iomem *page_remapped = ioremap_nocache(page_base,
4149 page_offs + size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004150
4151 return page_remapped ? (page_remapped + page_offs) : NULL;
4152}
4153
4154/* Takes cmds off the submission queue and sends them to the hardware,
4155 * then puts them on the queue of cmds waiting for completion.
4156 */
4157static void start_io(struct ctlr_info *h)
4158{
4159 struct CommandList *c;
Matt Gatese16a33a2012-05-01 11:43:11 -05004160 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004161
Matt Gatese16a33a2012-05-01 11:43:11 -05004162 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06004163 while (!list_empty(&h->reqQ)) {
4164 c = list_entry(h->reqQ.next, struct CommandList, list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004165 /* can't do anything if fifo is full */
4166 if ((h->access.fifo_full(h))) {
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004167 h->fifo_recently_full = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004168 dev_warn(&h->pdev->dev, "fifo full\n");
4169 break;
4170 }
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004171 h->fifo_recently_full = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004172
4173 /* Get the first entry from the Request Q */
4174 removeQ(c);
4175 h->Qdepth--;
4176
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004177 /* Put job onto the completed Q */
4178 addQ(&h->cmpQ, c);
Matt Gatese16a33a2012-05-01 11:43:11 -05004179
4180 /* Must increment commands_outstanding before unlocking
4181 * and submitting to avoid race checking for fifo full
4182 * condition.
4183 */
4184 h->commands_outstanding++;
4185 if (h->commands_outstanding > h->max_outstanding)
4186 h->max_outstanding = h->commands_outstanding;
4187
4188 /* Tell the controller execute command */
4189 spin_unlock_irqrestore(&h->lock, flags);
4190 h->access.submit_command(h, c);
4191 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004192 }
Matt Gatese16a33a2012-05-01 11:43:11 -05004193 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004194}
4195
Matt Gates254f7962012-05-01 11:43:06 -05004196static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004197{
Matt Gates254f7962012-05-01 11:43:06 -05004198 return h->access.command_completed(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004199}
4200
Stephen M. Cameron900c5442010-02-04 08:42:35 -06004201static inline bool interrupt_pending(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004202{
4203 return h->access.intr_pending(h);
4204}
4205
4206static inline long interrupt_not_for_us(struct ctlr_info *h)
4207{
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004208 return (h->access.intr_pending(h) == 0) ||
4209 (h->interrupts_enabled == 0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004210}
4211
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004212static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
4213 u32 raw_tag)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004214{
4215 if (unlikely(tag_index >= h->nr_cmds)) {
4216 dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
4217 return 1;
4218 }
4219 return 0;
4220}
4221
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05004222static inline void finish_cmd(struct CommandList *c)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004223{
Matt Gatese16a33a2012-05-01 11:43:11 -05004224 unsigned long flags;
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004225 int io_may_be_stalled = 0;
4226 struct ctlr_info *h = c->h;
Matt Gatese16a33a2012-05-01 11:43:11 -05004227
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004228 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004229 removeQ(c);
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004230
4231 /*
4232 * Check for possibly stalled i/o.
4233 *
4234 * If a fifo_full condition is encountered, requests will back up
4235 * in h->reqQ. This queue is only emptied out by start_io which is
4236 * only called when a new i/o request comes in. If no i/o's are
4237 * forthcoming, the i/o's in h->reqQ can get stuck. So we call
4238 * start_io from here if we detect such a danger.
4239 *
4240 * Normally, we shouldn't hit this case, but pounding on the
4241 * CCISS_PASSTHRU ioctl can provoke it. Only call start_io if
4242 * commands_outstanding is low. We want to avoid calling
4243 * start_io from in here as much as possible, and esp. don't
4244 * want to get in a cycle where we call start_io every time
4245 * through here.
4246 */
4247 if (unlikely(h->fifo_recently_full) &&
4248 h->commands_outstanding < 5)
4249 io_may_be_stalled = 1;
4250
4251 spin_unlock_irqrestore(&h->lock, flags);
4252
Stephen M. Camerone85c5972012-05-01 11:43:42 -05004253 dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
Matt Gatese1f7de02014-02-18 13:55:17 -06004254 if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI))
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05004255 complete_scsi_command(c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004256 else if (c->cmd_type == CMD_IOCTL_PEND)
4257 complete(c->waiting);
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004258 if (unlikely(io_may_be_stalled))
4259 start_io(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004260}
4261
Stephen M. Camerona104c992010-02-04 08:42:24 -06004262static inline u32 hpsa_tag_contains_index(u32 tag)
4263{
Stephen M. Camerona104c992010-02-04 08:42:24 -06004264 return tag & DIRECT_LOOKUP_BIT;
4265}
4266
4267static inline u32 hpsa_tag_to_index(u32 tag)
4268{
Stephen M. Camerona104c992010-02-04 08:42:24 -06004269 return tag >> DIRECT_LOOKUP_SHIFT;
4270}
4271
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004272
4273static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
Stephen M. Camerona104c992010-02-04 08:42:24 -06004274{
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004275#define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
4276#define HPSA_SIMPLE_ERROR_BITS 0x03
Stephen M. Cameron960a30e72011-02-15 15:33:03 -06004277 if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004278 return tag & ~HPSA_SIMPLE_ERROR_BITS;
4279 return tag & ~HPSA_PERF_ERROR_BITS;
Stephen M. Camerona104c992010-02-04 08:42:24 -06004280}
4281
Don Brace303932f2010-02-04 08:42:40 -06004282/* process completion of an indexed ("direct lookup") command */
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004283static inline void process_indexed_cmd(struct ctlr_info *h,
Don Brace303932f2010-02-04 08:42:40 -06004284 u32 raw_tag)
4285{
4286 u32 tag_index;
4287 struct CommandList *c;
4288
4289 tag_index = hpsa_tag_to_index(raw_tag);
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004290 if (!bad_tag(h, tag_index, raw_tag)) {
4291 c = h->cmd_pool + tag_index;
4292 finish_cmd(c);
4293 }
Don Brace303932f2010-02-04 08:42:40 -06004294}
4295
4296/* process completion of a non-indexed command */
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004297static inline void process_nonindexed_cmd(struct ctlr_info *h,
Don Brace303932f2010-02-04 08:42:40 -06004298 u32 raw_tag)
4299{
4300 u32 tag;
4301 struct CommandList *c = NULL;
Matt Gatese16a33a2012-05-01 11:43:11 -05004302 unsigned long flags;
Don Brace303932f2010-02-04 08:42:40 -06004303
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004304 tag = hpsa_tag_discard_error_bits(h, raw_tag);
Matt Gatese16a33a2012-05-01 11:43:11 -05004305 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06004306 list_for_each_entry(c, &h->cmpQ, list) {
Don Brace303932f2010-02-04 08:42:40 -06004307 if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
Matt Gatese16a33a2012-05-01 11:43:11 -05004308 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05004309 finish_cmd(c);
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004310 return;
Don Brace303932f2010-02-04 08:42:40 -06004311 }
4312 }
Matt Gatese16a33a2012-05-01 11:43:11 -05004313 spin_unlock_irqrestore(&h->lock, flags);
Don Brace303932f2010-02-04 08:42:40 -06004314 bad_tag(h, h->nr_cmds + 1, raw_tag);
Don Brace303932f2010-02-04 08:42:40 -06004315}
4316
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004317/* Some controllers, like p400, will give us one interrupt
4318 * after a soft reset, even if we turned interrupts off.
4319 * Only need to check for this in the hpsa_xxx_discard_completions
4320 * functions.
4321 */
4322static int ignore_bogus_interrupt(struct ctlr_info *h)
4323{
4324 if (likely(!reset_devices))
4325 return 0;
4326
4327 if (likely(h->interrupts_enabled))
4328 return 0;
4329
4330 dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
4331 "(known firmware bug.) Ignoring.\n");
4332
4333 return 1;
4334}
4335
Matt Gates254f7962012-05-01 11:43:06 -05004336/*
4337 * Convert &h->q[x] (passed to interrupt handlers) back to h.
4338 * Relies on (h-q[x] == x) being true for x such that
4339 * 0 <= x < MAX_REPLY_QUEUES.
4340 */
4341static struct ctlr_info *queue_to_hba(u8 *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004342{
Matt Gates254f7962012-05-01 11:43:06 -05004343 return container_of((queue - *queue), struct ctlr_info, q[0]);
4344}
4345
4346static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
4347{
4348 struct ctlr_info *h = queue_to_hba(queue);
4349 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004350 u32 raw_tag;
4351
4352 if (ignore_bogus_interrupt(h))
4353 return IRQ_NONE;
4354
4355 if (interrupt_not_for_us(h))
4356 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004357 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004358 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05004359 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004360 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05004361 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004362 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004363 return IRQ_HANDLED;
4364}
4365
Matt Gates254f7962012-05-01 11:43:06 -05004366static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004367{
Matt Gates254f7962012-05-01 11:43:06 -05004368 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004369 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05004370 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004371
4372 if (ignore_bogus_interrupt(h))
4373 return IRQ_NONE;
4374
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004375 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05004376 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004377 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05004378 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004379 return IRQ_HANDLED;
4380}
4381
Matt Gates254f7962012-05-01 11:43:06 -05004382static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004383{
Matt Gates254f7962012-05-01 11:43:06 -05004384 struct ctlr_info *h = queue_to_hba((u8 *) queue);
Don Brace303932f2010-02-04 08:42:40 -06004385 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05004386 u8 q = *(u8 *) queue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004387
4388 if (interrupt_not_for_us(h))
4389 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004390 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004391 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05004392 raw_tag = get_next_completion(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004393 while (raw_tag != FIFO_EMPTY) {
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004394 if (likely(hpsa_tag_contains_index(raw_tag)))
4395 process_indexed_cmd(h, raw_tag);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004396 else
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004397 process_nonindexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05004398 raw_tag = next_command(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004399 }
4400 }
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004401 return IRQ_HANDLED;
4402}
4403
Matt Gates254f7962012-05-01 11:43:06 -05004404static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004405{
Matt Gates254f7962012-05-01 11:43:06 -05004406 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004407 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05004408 u8 q = *(u8 *) queue;
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004409
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004410 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05004411 raw_tag = get_next_completion(h, q);
Don Brace303932f2010-02-04 08:42:40 -06004412 while (raw_tag != FIFO_EMPTY) {
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004413 if (likely(hpsa_tag_contains_index(raw_tag)))
4414 process_indexed_cmd(h, raw_tag);
Don Brace303932f2010-02-04 08:42:40 -06004415 else
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004416 process_nonindexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05004417 raw_tag = next_command(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004418 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004419 return IRQ_HANDLED;
4420}
4421
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004422/* Send a message CDB to the firmware. Careful, this only works
4423 * in simple mode, not performant mode due to the tag lookup.
4424 * We only ever use this immediately after a controller reset.
4425 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004426static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
4427 unsigned char type)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004428{
4429 struct Command {
4430 struct CommandListHeader CommandHeader;
4431 struct RequestBlock Request;
4432 struct ErrDescriptor ErrorDescriptor;
4433 };
4434 struct Command *cmd;
4435 static const size_t cmd_sz = sizeof(*cmd) +
4436 sizeof(cmd->ErrorDescriptor);
4437 dma_addr_t paddr64;
4438 uint32_t paddr32, tag;
4439 void __iomem *vaddr;
4440 int i, err;
4441
4442 vaddr = pci_ioremap_bar(pdev, 0);
4443 if (vaddr == NULL)
4444 return -ENOMEM;
4445
4446 /* The Inbound Post Queue only accepts 32-bit physical addresses for the
4447 * CCISS commands, so they must be allocated from the lower 4GiB of
4448 * memory.
4449 */
4450 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
4451 if (err) {
4452 iounmap(vaddr);
4453 return -ENOMEM;
4454 }
4455
4456 cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
4457 if (cmd == NULL) {
4458 iounmap(vaddr);
4459 return -ENOMEM;
4460 }
4461
4462 /* This must fit, because of the 32-bit consistent DMA mask. Also,
4463 * although there's no guarantee, we assume that the address is at
4464 * least 4-byte aligned (most likely, it's page-aligned).
4465 */
4466 paddr32 = paddr64;
4467
4468 cmd->CommandHeader.ReplyQueue = 0;
4469 cmd->CommandHeader.SGList = 0;
4470 cmd->CommandHeader.SGTotal = 0;
4471 cmd->CommandHeader.Tag.lower = paddr32;
4472 cmd->CommandHeader.Tag.upper = 0;
4473 memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
4474
4475 cmd->Request.CDBLen = 16;
4476 cmd->Request.Type.Type = TYPE_MSG;
4477 cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
4478 cmd->Request.Type.Direction = XFER_NONE;
4479 cmd->Request.Timeout = 0; /* Don't time out */
4480 cmd->Request.CDB[0] = opcode;
4481 cmd->Request.CDB[1] = type;
4482 memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
4483 cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
4484 cmd->ErrorDescriptor.Addr.upper = 0;
4485 cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
4486
4487 writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
4488
4489 for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
4490 tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004491 if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr32)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004492 break;
4493 msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
4494 }
4495
4496 iounmap(vaddr);
4497
4498 /* we leak the DMA buffer here ... no choice since the controller could
4499 * still complete the command.
4500 */
4501 if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
4502 dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
4503 opcode, type);
4504 return -ETIMEDOUT;
4505 }
4506
4507 pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
4508
4509 if (tag & HPSA_ERROR_BIT) {
4510 dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
4511 opcode, type);
4512 return -EIO;
4513 }
4514
4515 dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
4516 opcode, type);
4517 return 0;
4518}
4519
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004520#define hpsa_noop(p) hpsa_message(p, 3, 0)
4521
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004522static int hpsa_controller_hard_reset(struct pci_dev *pdev,
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05004523 void * __iomem vaddr, u32 use_doorbell)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004524{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004525 u16 pmcsr;
4526 int pos;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004527
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004528 if (use_doorbell) {
4529 /* For everything after the P600, the PCI power state method
4530 * of resetting the controller doesn't work, so we have this
4531 * other way using the doorbell register.
4532 */
4533 dev_info(&pdev->dev, "using doorbell to reset controller\n");
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05004534 writel(use_doorbell, vaddr + SA5_DOORBELL);
Stephen M. Cameron85009232013-09-23 13:33:36 -05004535
4536 /* PMC hardware guys tell us we need a 5 second delay after
4537 * doorbell reset and before any attempt to talk to the board
4538 * at all to ensure that this actually works and doesn't fall
4539 * over in some weird corner cases.
4540 */
4541 msleep(5000);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004542 } else { /* Try to do it the PCI power state way */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004543
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004544 /* Quoting from the Open CISS Specification: "The Power
4545 * Management Control/Status Register (CSR) controls the power
4546 * state of the device. The normal operating state is D0,
4547 * CSR=00h. The software off state is D3, CSR=03h. To reset
4548 * the controller, place the interface device in D3 then to D0,
4549 * this causes a secondary PCI reset which will reset the
4550 * controller." */
4551
4552 pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
4553 if (pos == 0) {
4554 dev_err(&pdev->dev,
4555 "hpsa_reset_controller: "
4556 "PCI PM not supported\n");
4557 return -ENODEV;
4558 }
4559 dev_info(&pdev->dev, "using PCI PM to reset controller\n");
4560 /* enter the D3hot power management state */
4561 pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
4562 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
4563 pmcsr |= PCI_D3hot;
4564 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
4565
4566 msleep(500);
4567
4568 /* enter the D0 power management state */
4569 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
4570 pmcsr |= PCI_D0;
4571 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
Mike Millerc4853ef2011-10-21 08:19:43 +02004572
4573 /*
4574 * The P600 requires a small delay when changing states.
4575 * Otherwise we may think the board did not reset and we bail.
4576 * This for kdump only and is particular to the P600.
4577 */
4578 msleep(500);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004579 }
4580 return 0;
4581}
4582
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004583static void init_driver_version(char *driver_version, int len)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004584{
4585 memset(driver_version, 0, len);
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06004586 strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004587}
4588
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004589static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004590{
4591 char *driver_version;
4592 int i, size = sizeof(cfgtable->driver_version);
4593
4594 driver_version = kmalloc(size, GFP_KERNEL);
4595 if (!driver_version)
4596 return -ENOMEM;
4597
4598 init_driver_version(driver_version, size);
4599 for (i = 0; i < size; i++)
4600 writeb(driver_version[i], &cfgtable->driver_version[i]);
4601 kfree(driver_version);
4602 return 0;
4603}
4604
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004605static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
4606 unsigned char *driver_ver)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004607{
4608 int i;
4609
4610 for (i = 0; i < sizeof(cfgtable->driver_version); i++)
4611 driver_ver[i] = readb(&cfgtable->driver_version[i]);
4612}
4613
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004614static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004615{
4616
4617 char *driver_ver, *old_driver_ver;
4618 int rc, size = sizeof(cfgtable->driver_version);
4619
4620 old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
4621 if (!old_driver_ver)
4622 return -ENOMEM;
4623 driver_ver = old_driver_ver + size;
4624
4625 /* After a reset, the 32 bytes of "driver version" in the cfgtable
4626 * should have been changed, otherwise we know the reset failed.
4627 */
4628 init_driver_version(old_driver_ver, size);
4629 read_driver_ver_from_cfgtable(cfgtable, driver_ver);
4630 rc = !memcmp(driver_ver, old_driver_ver, size);
4631 kfree(old_driver_ver);
4632 return rc;
4633}
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004634/* This does a hard reset of the controller using PCI power management
4635 * states or the using the doorbell register.
4636 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004637static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev)
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004638{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004639 u64 cfg_offset;
4640 u32 cfg_base_addr;
4641 u64 cfg_base_addr_index;
4642 void __iomem *vaddr;
4643 unsigned long paddr;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004644 u32 misc_fw_support;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06004645 int rc;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004646 struct CfgTable __iomem *cfgtable;
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05004647 u32 use_doorbell;
Stephen M. Cameron18867652010-06-16 13:51:45 -05004648 u32 board_id;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06004649 u16 command_register;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004650
4651 /* For controllers as old as the P600, this is very nearly
4652 * the same thing as
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004653 *
4654 * pci_save_state(pci_dev);
4655 * pci_set_power_state(pci_dev, PCI_D3hot);
4656 * pci_set_power_state(pci_dev, PCI_D0);
4657 * pci_restore_state(pci_dev);
4658 *
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004659 * For controllers newer than the P600, the pci power state
4660 * method of resetting doesn't work so we have another way
4661 * using the doorbell register.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004662 */
Stephen M. Cameron18867652010-06-16 13:51:45 -05004663
Stephen M. Cameron25c1e56a2011-01-06 14:48:18 -06004664 rc = hpsa_lookup_board_id(pdev, &board_id);
Stephen M. Cameron46380782011-05-03 15:00:01 -05004665 if (rc < 0 || !ctlr_is_resettable(board_id)) {
Stephen M. Cameron25c1e56a2011-01-06 14:48:18 -06004666 dev_warn(&pdev->dev, "Not resetting device.\n");
4667 return -ENODEV;
4668 }
Stephen M. Cameron46380782011-05-03 15:00:01 -05004669
4670 /* if controller is soft- but not hard resettable... */
4671 if (!ctlr_is_hard_resettable(board_id))
4672 return -ENOTSUPP; /* try soft reset later. */
Stephen M. Cameron18867652010-06-16 13:51:45 -05004673
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06004674 /* Save the PCI command register */
4675 pci_read_config_word(pdev, 4, &command_register);
4676 /* Turn the board off. This is so that later pci_restore_state()
4677 * won't turn the board on before the rest of config space is ready.
4678 */
4679 pci_disable_device(pdev);
4680 pci_save_state(pdev);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004681
4682 /* find the first memory BAR, so we can find the cfg table */
4683 rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
4684 if (rc)
4685 return rc;
4686 vaddr = remap_pci_mem(paddr, 0x250);
4687 if (!vaddr)
4688 return -ENOMEM;
4689
4690 /* find cfgtable in order to check if reset via doorbell is supported */
4691 rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
4692 &cfg_base_addr_index, &cfg_offset);
4693 if (rc)
4694 goto unmap_vaddr;
4695 cfgtable = remap_pci_mem(pci_resource_start(pdev,
4696 cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
4697 if (!cfgtable) {
4698 rc = -ENOMEM;
4699 goto unmap_vaddr;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004700 }
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004701 rc = write_driver_ver_to_cfgtable(cfgtable);
4702 if (rc)
4703 goto unmap_vaddr;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004704
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05004705 /* If reset via doorbell register is supported, use that.
4706 * There are two such methods. Favor the newest method.
4707 */
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004708 misc_fw_support = readl(&cfgtable->misc_fw_support);
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05004709 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
4710 if (use_doorbell) {
4711 use_doorbell = DOORBELL_CTLR_RESET2;
4712 } else {
4713 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
4714 if (use_doorbell) {
Mike Millerfba63092011-10-13 11:44:06 -05004715 dev_warn(&pdev->dev, "Soft reset not supported. "
4716 "Firmware update is required.\n");
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004717 rc = -ENOTSUPP; /* try soft reset */
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05004718 goto unmap_cfgtable;
4719 }
4720 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004721
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004722 rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
4723 if (rc)
4724 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004725
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06004726 pci_restore_state(pdev);
4727 rc = pci_enable_device(pdev);
4728 if (rc) {
4729 dev_warn(&pdev->dev, "failed to enable device.\n");
4730 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004731 }
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06004732 pci_write_config_word(pdev, 4, command_register);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004733
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004734 /* Some devices (notably the HP Smart Array 5i Controller)
4735 need a little pause here */
4736 msleep(HPSA_POST_RESET_PAUSE_MSECS);
4737
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004738 rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
4739 if (rc) {
4740 dev_warn(&pdev->dev,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004741 "failed waiting for board to become ready "
4742 "after hard reset\n");
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004743 goto unmap_cfgtable;
4744 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004745
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004746 rc = controller_reset_failed(vaddr);
4747 if (rc < 0)
4748 goto unmap_cfgtable;
4749 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004750 dev_warn(&pdev->dev, "Unable to successfully reset "
4751 "controller. Will try soft reset.\n");
4752 rc = -ENOTSUPP;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004753 } else {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004754 dev_info(&pdev->dev, "board ready after hard reset.\n");
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004755 }
4756
4757unmap_cfgtable:
4758 iounmap(cfgtable);
4759
4760unmap_vaddr:
4761 iounmap(vaddr);
4762 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004763}
4764
4765/*
4766 * We cannot read the structure directly, for portability we must use
4767 * the io functions.
4768 * This is for debug only.
4769 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004770static void print_cfg_table(struct device *dev, struct CfgTable *tb)
4771{
Stephen M. Cameron58f86652010-05-27 15:13:58 -05004772#ifdef HPSA_DEBUG
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004773 int i;
4774 char temp_name[17];
4775
4776 dev_info(dev, "Controller Configuration information\n");
4777 dev_info(dev, "------------------------------------\n");
4778 for (i = 0; i < 4; i++)
4779 temp_name[i] = readb(&(tb->Signature[i]));
4780 temp_name[4] = '\0';
4781 dev_info(dev, " Signature = %s\n", temp_name);
4782 dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
4783 dev_info(dev, " Transport methods supported = 0x%x\n",
4784 readl(&(tb->TransportSupport)));
4785 dev_info(dev, " Transport methods active = 0x%x\n",
4786 readl(&(tb->TransportActive)));
4787 dev_info(dev, " Requested transport Method = 0x%x\n",
4788 readl(&(tb->HostWrite.TransportRequest)));
4789 dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
4790 readl(&(tb->HostWrite.CoalIntDelay)));
4791 dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
4792 readl(&(tb->HostWrite.CoalIntCount)));
4793 dev_info(dev, " Max outstanding commands = 0x%d\n",
4794 readl(&(tb->CmdsOutMax)));
4795 dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
4796 for (i = 0; i < 16; i++)
4797 temp_name[i] = readb(&(tb->ServerName[i]));
4798 temp_name[16] = '\0';
4799 dev_info(dev, " Server Name = %s\n", temp_name);
4800 dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
4801 readl(&(tb->HeartBeat)));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004802#endif /* HPSA_DEBUG */
Stephen M. Cameron58f86652010-05-27 15:13:58 -05004803}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004804
4805static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
4806{
4807 int i, offset, mem_type, bar_type;
4808
4809 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
4810 return 0;
4811 offset = 0;
4812 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
4813 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
4814 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
4815 offset += 4;
4816 else {
4817 mem_type = pci_resource_flags(pdev, i) &
4818 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
4819 switch (mem_type) {
4820 case PCI_BASE_ADDRESS_MEM_TYPE_32:
4821 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
4822 offset += 4; /* 32 bit */
4823 break;
4824 case PCI_BASE_ADDRESS_MEM_TYPE_64:
4825 offset += 8;
4826 break;
4827 default: /* reserved in PCI 2.2 */
4828 dev_warn(&pdev->dev,
4829 "base address is invalid\n");
4830 return -1;
4831 break;
4832 }
4833 }
4834 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
4835 return i + 1;
4836 }
4837 return -1;
4838}
4839
4840/* If MSI/MSI-X is supported by the kernel we will try to enable it on
4841 * controllers that are capable. If not, we use IO-APIC mode.
4842 */
4843
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004844static void hpsa_interrupt_mode(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004845{
4846#ifdef CONFIG_PCI_MSI
Matt Gates254f7962012-05-01 11:43:06 -05004847 int err, i;
4848 struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
4849
4850 for (i = 0; i < MAX_REPLY_QUEUES; i++) {
4851 hpsa_msix_entries[i].vector = 0;
4852 hpsa_msix_entries[i].entry = i;
4853 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004854
4855 /* Some boards advertise MSI but don't really support it */
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05004856 if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
4857 (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004858 goto default_int_mode;
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05004859 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
4860 dev_info(&h->pdev->dev, "MSIX\n");
Hannes Reineckeeee0f032014-01-15 13:30:53 +01004861 h->msix_vector = MAX_REPLY_QUEUES;
Matt Gates254f7962012-05-01 11:43:06 -05004862 err = pci_enable_msix(h->pdev, hpsa_msix_entries,
Hannes Reineckeeee0f032014-01-15 13:30:53 +01004863 h->msix_vector);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004864 if (err > 0) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05004865 dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004866 "available\n", err);
Hannes Reineckeeee0f032014-01-15 13:30:53 +01004867 h->msix_vector = err;
4868 err = pci_enable_msix(h->pdev, hpsa_msix_entries,
4869 h->msix_vector);
4870 }
4871 if (!err) {
4872 for (i = 0; i < h->msix_vector; i++)
4873 h->intr[i] = hpsa_msix_entries[i].vector;
4874 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004875 } else {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05004876 dev_warn(&h->pdev->dev, "MSI-X init failed %d\n",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004877 err);
Hannes Reineckeeee0f032014-01-15 13:30:53 +01004878 h->msix_vector = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004879 goto default_int_mode;
4880 }
4881 }
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05004882 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
4883 dev_info(&h->pdev->dev, "MSI\n");
4884 if (!pci_enable_msi(h->pdev))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004885 h->msi_vector = 1;
4886 else
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05004887 dev_warn(&h->pdev->dev, "MSI init failed\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004888 }
4889default_int_mode:
4890#endif /* CONFIG_PCI_MSI */
4891 /* if we get here we're going to use the default interrupt mode */
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004892 h->intr[h->intr_mode] = h->pdev->irq;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004893}
4894
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004895static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05004896{
4897 int i;
4898 u32 subsystem_vendor_id, subsystem_device_id;
4899
4900 subsystem_vendor_id = pdev->subsystem_vendor;
4901 subsystem_device_id = pdev->subsystem_device;
4902 *board_id = ((subsystem_device_id << 16) & 0xffff0000) |
4903 subsystem_vendor_id;
4904
4905 for (i = 0; i < ARRAY_SIZE(products); i++)
4906 if (*board_id == products[i].board_id)
4907 return i;
4908
Stephen M. Cameron6798cc02010-06-16 13:51:20 -05004909 if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
4910 subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
4911 !hpsa_allow_any) {
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05004912 dev_warn(&pdev->dev, "unrecognized board ID: "
4913 "0x%08x, ignoring.\n", *board_id);
4914 return -ENODEV;
4915 }
4916 return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
4917}
4918
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004919static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
4920 unsigned long *memory_bar)
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05004921{
4922 int i;
4923
4924 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05004925 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05004926 /* addressing mode bits already removed */
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05004927 *memory_bar = pci_resource_start(pdev, i);
4928 dev_dbg(&pdev->dev, "memory BAR = %lx\n",
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05004929 *memory_bar);
4930 return 0;
4931 }
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05004932 dev_warn(&pdev->dev, "no memory BAR found\n");
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05004933 return -ENODEV;
4934}
4935
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004936static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
4937 int wait_for_ready)
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05004938{
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004939 int i, iterations;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05004940 u32 scratchpad;
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004941 if (wait_for_ready)
4942 iterations = HPSA_BOARD_READY_ITERATIONS;
4943 else
4944 iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05004945
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004946 for (i = 0; i < iterations; i++) {
4947 scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
4948 if (wait_for_ready) {
4949 if (scratchpad == HPSA_FIRMWARE_READY)
4950 return 0;
4951 } else {
4952 if (scratchpad != HPSA_FIRMWARE_READY)
4953 return 0;
4954 }
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05004955 msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
4956 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004957 dev_warn(&pdev->dev, "board not ready, timed out.\n");
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05004958 return -ENODEV;
4959}
4960
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004961static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
4962 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
4963 u64 *cfg_offset)
Stephen M. Camerona51fd472010-06-16 13:51:30 -05004964{
4965 *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
4966 *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
4967 *cfg_base_addr &= (u32) 0x0000ffff;
4968 *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
4969 if (*cfg_base_addr_index == -1) {
4970 dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
4971 return -ENODEV;
4972 }
4973 return 0;
4974}
4975
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004976static int hpsa_find_cfgtables(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004977{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004978 u64 cfg_offset;
4979 u32 cfg_base_addr;
4980 u64 cfg_base_addr_index;
Don Brace303932f2010-02-04 08:42:40 -06004981 u32 trans_offset;
Stephen M. Camerona51fd472010-06-16 13:51:30 -05004982 int rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05004983
Stephen M. Camerona51fd472010-06-16 13:51:30 -05004984 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
4985 &cfg_base_addr_index, &cfg_offset);
4986 if (rc)
4987 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05004988 h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
Stephen M. Camerona51fd472010-06-16 13:51:30 -05004989 cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
Stephen M. Cameron77c44952010-05-27 15:13:17 -05004990 if (!h->cfgtable)
4991 return -ENOMEM;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004992 rc = write_driver_ver_to_cfgtable(h->cfgtable);
4993 if (rc)
4994 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05004995 /* Find performant mode table. */
Stephen M. Camerona51fd472010-06-16 13:51:30 -05004996 trans_offset = readl(&h->cfgtable->TransMethodOffset);
Stephen M. Cameron77c44952010-05-27 15:13:17 -05004997 h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
4998 cfg_base_addr_index)+cfg_offset+trans_offset,
4999 sizeof(*h->transtable));
5000 if (!h->transtable)
5001 return -ENOMEM;
5002 return 0;
5003}
5004
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005005static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05005006{
5007 h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
Stephen M. Cameron72ceeae2011-01-06 14:48:13 -06005008
5009 /* Limit commands in memory limited kdump scenario. */
5010 if (reset_devices && h->max_commands > 32)
5011 h->max_commands = 32;
5012
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05005013 if (h->max_commands < 16) {
5014 dev_warn(&h->pdev->dev, "Controller reports "
5015 "max supported commands of %d, an obvious lie. "
5016 "Using 16. Ensure that firmware is up to date.\n",
5017 h->max_commands);
5018 h->max_commands = 16;
5019 }
5020}
5021
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005022/* Interrogate the hardware for some limits:
5023 * max commands, max SG elements without chaining, and with chaining,
5024 * SG chain block size, etc.
5025 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005026static void hpsa_find_board_params(struct ctlr_info *h)
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005027{
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05005028 hpsa_get_max_perf_mode_cmds(h);
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005029 h->nr_cmds = h->max_commands - 4; /* Allow room for some ioctls */
5030 h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005031 h->fw_support = readl(&(h->cfgtable->misc_fw_support));
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005032 /*
5033 * Limit in-command s/g elements to 32 save dma'able memory.
5034 * Howvever spec says if 0, use 31
5035 */
5036 h->max_cmd_sg_entries = 31;
5037 if (h->maxsgentries > 512) {
5038 h->max_cmd_sg_entries = 32;
5039 h->chainsize = h->maxsgentries - h->max_cmd_sg_entries + 1;
5040 h->maxsgentries--; /* save one for chain pointer */
5041 } else {
5042 h->maxsgentries = 31; /* default to traditional values */
5043 h->chainsize = 0;
5044 }
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005045
5046 /* Find out what task management functions are supported and cache */
5047 h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
Scott Teel0e7a7fc2014-02-18 13:55:59 -06005048 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
5049 dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
5050 if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
5051 dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005052}
5053
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05005054static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
5055{
Akinobu Mita0fc9fd42012-04-04 22:14:59 +09005056 if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05005057 dev_warn(&h->pdev->dev, "not a valid CISS config table\n");
5058 return false;
5059 }
5060 return true;
5061}
5062
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005063static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005064{
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005065 u32 driver_support;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005066
Stephen M. Cameron28e13442013-12-04 17:10:21 -06005067#ifdef CONFIG_X86
5068 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005069 driver_support = readl(&(h->cfgtable->driver_support));
5070 driver_support |= ENABLE_SCSI_PREFETCH;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005071#endif
Stephen M. Cameron28e13442013-12-04 17:10:21 -06005072 driver_support |= ENABLE_UNIT_ATTN;
5073 writel(driver_support, &(h->cfgtable->driver_support));
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005074}
5075
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05005076/* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result
5077 * in a prefetch beyond physical memory.
5078 */
5079static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
5080{
5081 u32 dma_prefetch;
5082
5083 if (h->board_id != 0x3225103C)
5084 return;
5085 dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
5086 dma_prefetch |= 0x8000;
5087 writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
5088}
5089
Stephen M. Cameron76438d02014-02-18 13:55:43 -06005090static void hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
5091{
5092 int i;
5093 u32 doorbell_value;
5094 unsigned long flags;
5095 /* wait until the clear_event_notify bit 6 is cleared by controller. */
5096 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
5097 spin_lock_irqsave(&h->lock, flags);
5098 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
5099 spin_unlock_irqrestore(&h->lock, flags);
5100 if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
5101 break;
5102 /* delay and try again */
5103 msleep(20);
5104 }
5105}
5106
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005107static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005108{
5109 int i;
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06005110 u32 doorbell_value;
5111 unsigned long flags;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005112
5113 /* under certain very rare conditions, this can take awhile.
5114 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
5115 * as we enter this code.)
5116 */
5117 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06005118 spin_lock_irqsave(&h->lock, flags);
5119 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
5120 spin_unlock_irqrestore(&h->lock, flags);
Dan Carpenter382be662011-02-15 15:33:13 -06005121 if (!(doorbell_value & CFGTBL_ChangeReq))
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005122 break;
5123 /* delay and try again */
Stephen M. Cameron60d3f5b2011-01-06 14:48:34 -06005124 usleep_range(10000, 20000);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005125 }
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005126}
5127
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005128static int hpsa_enter_simple_mode(struct ctlr_info *h)
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005129{
5130 u32 trans_support;
5131
5132 trans_support = readl(&(h->cfgtable->TransportSupport));
5133 if (!(trans_support & SIMPLE_MODE))
5134 return -ENOTSUPP;
5135
5136 h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005137
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005138 /* Update the field, and then ring the doorbell */
5139 writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
5140 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
5141 hpsa_wait_for_mode_change_ack(h);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005142 print_cfg_table(&h->pdev->dev, h->cfgtable);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005143 if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
5144 goto error;
Stephen M. Cameron960a30e72011-02-15 15:33:03 -06005145 h->transMethod = CFGTBL_Trans_Simple;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005146 return 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005147error:
5148 dev_warn(&h->pdev->dev, "unable to get board into simple mode\n");
5149 return -ENODEV;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005150}
5151
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005152static int hpsa_pci_init(struct ctlr_info *h)
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005153{
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005154 int prod_index, err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005155
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05005156 prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
5157 if (prod_index < 0)
5158 return -ENODEV;
5159 h->product_name = products[prod_index].product_name;
5160 h->access = *(products[prod_index].access);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005161
Matthew Garrette5a44df2011-11-11 11:14:23 -05005162 pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
5163 PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
5164
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005165 err = pci_enable_device(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005166 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005167 dev_warn(&h->pdev->dev, "unable to enable PCI device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005168 return err;
5169 }
5170
Stephen M. Cameron5cb460a2012-05-01 11:42:20 -05005171 /* Enable bus mastering (pci_disable_device may disable this) */
5172 pci_set_master(h->pdev);
5173
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06005174 err = pci_request_regions(h->pdev, HPSA);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005175 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005176 dev_err(&h->pdev->dev,
5177 "cannot obtain PCI resources, aborting\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005178 return err;
5179 }
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05005180 hpsa_interrupt_mode(h);
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05005181 err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005182 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005183 goto err_out_free_res;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005184 h->vaddr = remap_pci_mem(h->paddr, 0x250);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05005185 if (!h->vaddr) {
5186 err = -ENOMEM;
5187 goto err_out_free_res;
5188 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005189 err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005190 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005191 goto err_out_free_res;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005192 err = hpsa_find_cfgtables(h);
5193 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005194 goto err_out_free_res;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005195 hpsa_find_board_params(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005196
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05005197 if (!hpsa_CISS_signature_present(h)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005198 err = -ENODEV;
5199 goto err_out_free_res;
5200 }
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005201 hpsa_set_driver_support_bits(h);
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05005202 hpsa_p600_dma_prefetch_quirk(h);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005203 err = hpsa_enter_simple_mode(h);
5204 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005205 goto err_out_free_res;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005206 return 0;
5207
5208err_out_free_res:
Stephen M. Cameron204892e2010-05-27 15:13:22 -05005209 if (h->transtable)
5210 iounmap(h->transtable);
5211 if (h->cfgtable)
5212 iounmap(h->cfgtable);
5213 if (h->vaddr)
5214 iounmap(h->vaddr);
Stephen M. Cameronf0bd0b682012-05-01 11:42:09 -05005215 pci_disable_device(h->pdev);
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005216 pci_release_regions(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005217 return err;
5218}
5219
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005220static void hpsa_hba_inquiry(struct ctlr_info *h)
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06005221{
5222 int rc;
5223
5224#define HBA_INQUIRY_BYTE_COUNT 64
5225 h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
5226 if (!h->hba_inquiry_data)
5227 return;
5228 rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
5229 h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
5230 if (rc != 0) {
5231 kfree(h->hba_inquiry_data);
5232 h->hba_inquiry_data = NULL;
5233 }
5234}
5235
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005236static int hpsa_init_reset_devices(struct pci_dev *pdev)
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005237{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005238 int rc, i;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005239
5240 if (!reset_devices)
5241 return 0;
5242
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005243 /* Reset the controller with a PCI power-cycle or via doorbell */
5244 rc = hpsa_kdump_hard_reset_controller(pdev);
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005245
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005246 /* -ENOTSUPP here means we cannot reset the controller
5247 * but it's already (and still) up and running in
Stephen M. Cameron18867652010-06-16 13:51:45 -05005248 * "performant mode". Or, it might be 640x, which can't reset
5249 * due to concerns about shared bbwc between 6402/6404 pair.
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005250 */
5251 if (rc == -ENOTSUPP)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005252 return rc; /* just try to do the kdump anyhow. */
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005253 if (rc)
5254 return -ENODEV;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005255
5256 /* Now try to get the controller to respond to a no-op */
Stephen M. Cameron2b870cb2011-05-03 14:59:36 -05005257 dev_warn(&pdev->dev, "Waiting for controller to respond to no-op\n");
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005258 for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
5259 if (hpsa_noop(pdev) == 0)
5260 break;
5261 else
5262 dev_warn(&pdev->dev, "no-op failed%s\n",
5263 (i < 11 ? "; re-trying" : ""));
5264 }
5265 return 0;
5266}
5267
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005268static int hpsa_allocate_cmd_pool(struct ctlr_info *h)
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05005269{
5270 h->cmd_pool_bits = kzalloc(
5271 DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
5272 sizeof(unsigned long), GFP_KERNEL);
5273 h->cmd_pool = pci_alloc_consistent(h->pdev,
5274 h->nr_cmds * sizeof(*h->cmd_pool),
5275 &(h->cmd_pool_dhandle));
5276 h->errinfo_pool = pci_alloc_consistent(h->pdev,
5277 h->nr_cmds * sizeof(*h->errinfo_pool),
5278 &(h->errinfo_pool_dhandle));
5279 if ((h->cmd_pool_bits == NULL)
5280 || (h->cmd_pool == NULL)
5281 || (h->errinfo_pool == NULL)) {
5282 dev_err(&h->pdev->dev, "out of memory in %s", __func__);
5283 return -ENOMEM;
5284 }
5285 return 0;
5286}
5287
5288static void hpsa_free_cmd_pool(struct ctlr_info *h)
5289{
5290 kfree(h->cmd_pool_bits);
5291 if (h->cmd_pool)
5292 pci_free_consistent(h->pdev,
5293 h->nr_cmds * sizeof(struct CommandList),
5294 h->cmd_pool, h->cmd_pool_dhandle);
Stephen M. Cameronaca90122014-02-18 13:56:14 -06005295 if (h->ioaccel2_cmd_pool)
5296 pci_free_consistent(h->pdev,
5297 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
5298 h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05005299 if (h->errinfo_pool)
5300 pci_free_consistent(h->pdev,
5301 h->nr_cmds * sizeof(struct ErrorInfo),
5302 h->errinfo_pool,
5303 h->errinfo_pool_dhandle);
Matt Gatese1f7de02014-02-18 13:55:17 -06005304 if (h->ioaccel_cmd_pool)
5305 pci_free_consistent(h->pdev,
5306 h->nr_cmds * sizeof(struct io_accel1_cmd),
5307 h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05005308}
5309
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05005310static int hpsa_request_irq(struct ctlr_info *h,
5311 irqreturn_t (*msixhandler)(int, void *),
5312 irqreturn_t (*intxhandler)(int, void *))
5313{
Matt Gates254f7962012-05-01 11:43:06 -05005314 int rc, i;
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05005315
Matt Gates254f7962012-05-01 11:43:06 -05005316 /*
5317 * initialize h->q[x] = x so that interrupt handlers know which
5318 * queue to process.
5319 */
5320 for (i = 0; i < MAX_REPLY_QUEUES; i++)
5321 h->q[i] = (u8) i;
5322
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005323 if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
Matt Gates254f7962012-05-01 11:43:06 -05005324 /* If performant mode and MSI-X, use multiple reply queues */
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005325 for (i = 0; i < h->msix_vector; i++)
Matt Gates254f7962012-05-01 11:43:06 -05005326 rc = request_irq(h->intr[i], msixhandler,
5327 0, h->devname,
5328 &h->q[i]);
5329 } else {
5330 /* Use single reply pool */
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005331 if (h->msix_vector > 0 || h->msi_vector) {
Matt Gates254f7962012-05-01 11:43:06 -05005332 rc = request_irq(h->intr[h->intr_mode],
5333 msixhandler, 0, h->devname,
5334 &h->q[h->intr_mode]);
5335 } else {
5336 rc = request_irq(h->intr[h->intr_mode],
5337 intxhandler, IRQF_SHARED, h->devname,
5338 &h->q[h->intr_mode]);
5339 }
5340 }
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05005341 if (rc) {
5342 dev_err(&h->pdev->dev, "unable to get irq %d for %s\n",
5343 h->intr[h->intr_mode], h->devname);
5344 return -ENODEV;
5345 }
5346 return 0;
5347}
5348
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005349static int hpsa_kdump_soft_reset(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005350{
5351 if (hpsa_send_host_reset(h, RAID_CTLR_LUNID,
5352 HPSA_RESET_TYPE_CONTROLLER)) {
5353 dev_warn(&h->pdev->dev, "Resetting array controller failed.\n");
5354 return -EIO;
5355 }
5356
5357 dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
5358 if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) {
5359 dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
5360 return -1;
5361 }
5362
5363 dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
5364 if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) {
5365 dev_warn(&h->pdev->dev, "Board failed to become ready "
5366 "after soft reset.\n");
5367 return -1;
5368 }
5369
5370 return 0;
5371}
5372
Matt Gates254f7962012-05-01 11:43:06 -05005373static void free_irqs(struct ctlr_info *h)
5374{
5375 int i;
5376
5377 if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
5378 /* Single reply queue, only one irq to free */
5379 i = h->intr_mode;
5380 free_irq(h->intr[i], &h->q[i]);
5381 return;
5382 }
5383
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005384 for (i = 0; i < h->msix_vector; i++)
Matt Gates254f7962012-05-01 11:43:06 -05005385 free_irq(h->intr[i], &h->q[i]);
5386}
5387
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05005388static void hpsa_free_irqs_and_disable_msix(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005389{
Matt Gates254f7962012-05-01 11:43:06 -05005390 free_irqs(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005391#ifdef CONFIG_PCI_MSI
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05005392 if (h->msix_vector) {
5393 if (h->pdev->msix_enabled)
5394 pci_disable_msix(h->pdev);
5395 } else if (h->msi_vector) {
5396 if (h->pdev->msi_enabled)
5397 pci_disable_msi(h->pdev);
5398 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005399#endif /* CONFIG_PCI_MSI */
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05005400}
5401
5402static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
5403{
5404 hpsa_free_irqs_and_disable_msix(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005405 hpsa_free_sg_chain_blocks(h);
5406 hpsa_free_cmd_pool(h);
Matt Gatese1f7de02014-02-18 13:55:17 -06005407 kfree(h->ioaccel1_blockFetchTable);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005408 kfree(h->blockFetchTable);
5409 pci_free_consistent(h->pdev, h->reply_pool_size,
5410 h->reply_pool, h->reply_pool_dhandle);
5411 if (h->vaddr)
5412 iounmap(h->vaddr);
5413 if (h->transtable)
5414 iounmap(h->transtable);
5415 if (h->cfgtable)
5416 iounmap(h->cfgtable);
5417 pci_release_regions(h->pdev);
5418 kfree(h);
5419}
5420
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005421/* Called when controller lockup detected. */
5422static void fail_all_cmds_on_list(struct ctlr_info *h, struct list_head *list)
5423{
5424 struct CommandList *c = NULL;
5425
5426 assert_spin_locked(&h->lock);
5427 /* Mark all outstanding commands as failed and complete them. */
5428 while (!list_empty(list)) {
5429 c = list_entry(list->next, struct CommandList, list);
5430 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05005431 finish_cmd(c);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005432 }
5433}
5434
5435static void controller_lockup_detected(struct ctlr_info *h)
5436{
5437 unsigned long flags;
5438
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005439 h->access.set_intr_mask(h, HPSA_INTR_OFF);
5440 spin_lock_irqsave(&h->lock, flags);
5441 h->lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
5442 spin_unlock_irqrestore(&h->lock, flags);
5443 dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x\n",
5444 h->lockup_detected);
5445 pci_disable_device(h->pdev);
5446 spin_lock_irqsave(&h->lock, flags);
5447 fail_all_cmds_on_list(h, &h->cmpQ);
5448 fail_all_cmds_on_list(h, &h->reqQ);
5449 spin_unlock_irqrestore(&h->lock, flags);
5450}
5451
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005452static void detect_controller_lockup(struct ctlr_info *h)
5453{
5454 u64 now;
5455 u32 heartbeat;
5456 unsigned long flags;
5457
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005458 now = get_jiffies_64();
5459 /* If we've received an interrupt recently, we're ok. */
5460 if (time_after64(h->last_intr_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05005461 (h->heartbeat_sample_interval), now))
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005462 return;
5463
5464 /*
5465 * If we've already checked the heartbeat recently, we're ok.
5466 * This could happen if someone sends us a signal. We
5467 * otherwise don't care about signals in this thread.
5468 */
5469 if (time_after64(h->last_heartbeat_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05005470 (h->heartbeat_sample_interval), now))
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005471 return;
5472
5473 /* If heartbeat has not changed since we last looked, we're not ok. */
5474 spin_lock_irqsave(&h->lock, flags);
5475 heartbeat = readl(&h->cfgtable->HeartBeat);
5476 spin_unlock_irqrestore(&h->lock, flags);
5477 if (h->last_heartbeat == heartbeat) {
5478 controller_lockup_detected(h);
5479 return;
5480 }
5481
5482 /* We're ok. */
5483 h->last_heartbeat = heartbeat;
5484 h->last_heartbeat_timestamp = now;
5485}
5486
Stephen M. Cameron76438d02014-02-18 13:55:43 -06005487static int hpsa_kickoff_rescan(struct ctlr_info *h)
5488{
5489 int i;
5490 char *event_type;
5491
5492 /* Ask the controller to clear the events we're handling. */
Stephen M. Cameron1f7cee82014-02-18 13:56:09 -06005493 if ((h->transMethod & (CFGTBL_Trans_io_accel1
5494 | CFGTBL_Trans_io_accel2)) &&
Stephen M. Cameron76438d02014-02-18 13:55:43 -06005495 (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
5496 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
5497
5498 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
5499 event_type = "state change";
5500 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
5501 event_type = "configuration change";
5502 /* Stop sending new RAID offload reqs via the IO accelerator */
5503 scsi_block_requests(h->scsi_host);
5504 for (i = 0; i < h->ndevices; i++)
5505 h->dev[i]->offload_enabled = 0;
5506 hpsa_drain_commands(h);
5507 /* Set 'accelerator path config change' bit */
5508 dev_warn(&h->pdev->dev,
5509 "Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
5510 h->events, event_type);
5511 writel(h->events, &(h->cfgtable->clear_event_notify));
5512 /* Set the "clear event notify field update" bit 6 */
5513 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
5514 /* Wait until ctlr clears 'clear event notify field', bit 6 */
5515 hpsa_wait_for_clear_event_notify_ack(h);
5516 scsi_unblock_requests(h->scsi_host);
5517 } else {
5518 /* Acknowledge controller notification events. */
5519 writel(h->events, &(h->cfgtable->clear_event_notify));
5520 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
5521 hpsa_wait_for_clear_event_notify_ack(h);
5522#if 0
5523 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
5524 hpsa_wait_for_mode_change_ack(h);
5525#endif
5526 }
5527
5528 /* Something in the device list may have changed to trigger
5529 * the event, so do a rescan.
5530 */
5531 hpsa_scan_start(h->scsi_host);
5532 /* release reference taken on scsi host in check_controller_events */
5533 scsi_host_put(h->scsi_host);
5534 return 0;
5535}
5536
5537/* Check a register on the controller to see if there are configuration
5538 * changes (added/changed/removed logical drives, etc.) which mean that
5539 * we should rescan the controller for devices. If so, add the controller
5540 * to the list of controllers needing to be rescanned, and gets a
5541 * reference to the associated scsi_host.
5542 */
5543static void hpsa_ctlr_needs_rescan(struct ctlr_info *h)
5544{
5545 if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
5546 return;
5547
5548 h->events = readl(&(h->cfgtable->event_notify));
5549 if (!h->events)
5550 return;
5551
5552 /*
5553 * Take a reference on scsi host for the duration of the scan
5554 * Release in hpsa_kickoff_rescan(). No lock needed for scan_list
5555 * as only a single thread accesses this list.
5556 */
5557 scsi_host_get(h->scsi_host);
5558 hpsa_kickoff_rescan(h);
5559}
5560
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005561static void hpsa_monitor_ctlr_worker(struct work_struct *work)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005562{
5563 unsigned long flags;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005564 struct ctlr_info *h = container_of(to_delayed_work(work),
5565 struct ctlr_info, monitor_ctlr_work);
5566 detect_controller_lockup(h);
5567 if (h->lockup_detected)
5568 return;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06005569 hpsa_ctlr_needs_rescan(h);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005570 spin_lock_irqsave(&h->lock, flags);
5571 if (h->remove_in_progress) {
5572 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005573 return;
5574 }
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005575 schedule_delayed_work(&h->monitor_ctlr_work,
5576 h->heartbeat_sample_interval);
5577 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005578}
5579
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005580static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005581{
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005582 int dac, rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005583 struct ctlr_info *h;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005584 int try_soft_reset = 0;
5585 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005586
5587 if (number_of_controllers == 0)
5588 printk(KERN_INFO DRIVER_NAME "\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005589
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005590 rc = hpsa_init_reset_devices(pdev);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005591 if (rc) {
5592 if (rc != -ENOTSUPP)
5593 return rc;
5594 /* If the reset fails in a particular way (it has no way to do
5595 * a proper hard reset, so returns -ENOTSUPP) we can try to do
5596 * a soft reset once we get the controller configured up to the
5597 * point that it can accept a command.
5598 */
5599 try_soft_reset = 1;
5600 rc = 0;
5601 }
5602
5603reinit_after_soft_reset:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005604
Don Brace303932f2010-02-04 08:42:40 -06005605 /* Command structures must be aligned on a 32-byte boundary because
5606 * the 5 lower bits of the address are used by the hardware. and by
5607 * the driver. See comments in hpsa.h for more info.
5608 */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005609#define COMMANDLIST_ALIGNMENT 128
Don Brace303932f2010-02-04 08:42:40 -06005610 BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005611 h = kzalloc(sizeof(*h), GFP_KERNEL);
5612 if (!h)
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06005613 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005614
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005615 h->pdev = pdev;
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005616 h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06005617 INIT_LIST_HEAD(&h->cmpQ);
5618 INIT_LIST_HEAD(&h->reqQ);
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06005619 spin_lock_init(&h->lock);
5620 spin_lock_init(&h->scan_lock);
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05005621 spin_lock_init(&h->passthru_count_lock);
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005622 rc = hpsa_pci_init(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06005623 if (rc != 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005624 goto clean1;
5625
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06005626 sprintf(h->devname, HPSA "%d", number_of_controllers);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005627 h->ctlr = number_of_controllers;
5628 number_of_controllers++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005629
5630 /* configure PCI DMA stuff */
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06005631 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
5632 if (rc == 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005633 dac = 1;
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06005634 } else {
5635 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
5636 if (rc == 0) {
5637 dac = 0;
5638 } else {
5639 dev_err(&pdev->dev, "no suitable DMA available\n");
5640 goto clean1;
5641 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005642 }
5643
5644 /* make sure the board interrupts are off */
5645 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005646
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05005647 if (hpsa_request_irq(h, do_hpsa_intr_msi, do_hpsa_intr_intx))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005648 goto clean2;
Don Brace303932f2010-02-04 08:42:40 -06005649 dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
5650 h->devname, pdev->device,
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005651 h->intr[h->intr_mode], dac ? "" : " not");
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05005652 if (hpsa_allocate_cmd_pool(h))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005653 goto clean4;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06005654 if (hpsa_allocate_sg_chain_blocks(h))
5655 goto clean4;
Stephen M. Camerona08a8472010-02-04 08:43:16 -06005656 init_waitqueue_head(&h->scan_wait_queue);
5657 h->scan_finished = 1; /* no scan currently in progress */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005658
5659 pci_set_drvdata(pdev, h);
Stephen M. Cameron9a413382011-05-03 14:59:41 -05005660 h->ndevices = 0;
5661 h->scsi_host = NULL;
5662 spin_lock_init(&h->devlock);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005663 hpsa_put_ctlr_into_performant_mode(h);
5664
5665 /* At this point, the controller is ready to take commands.
5666 * Now, if reset_devices and the hard reset didn't work, try
5667 * the soft reset and see if that works.
5668 */
5669 if (try_soft_reset) {
5670
5671 /* This is kind of gross. We may or may not get a completion
5672 * from the soft reset command, and if we do, then the value
5673 * from the fifo may or may not be valid. So, we wait 10 secs
5674 * after the reset throwing away any completions we get during
5675 * that time. Unregister the interrupt handler and register
5676 * fake ones to scoop up any residual completions.
5677 */
5678 spin_lock_irqsave(&h->lock, flags);
5679 h->access.set_intr_mask(h, HPSA_INTR_OFF);
5680 spin_unlock_irqrestore(&h->lock, flags);
Matt Gates254f7962012-05-01 11:43:06 -05005681 free_irqs(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005682 rc = hpsa_request_irq(h, hpsa_msix_discard_completions,
5683 hpsa_intx_discard_completions);
5684 if (rc) {
5685 dev_warn(&h->pdev->dev, "Failed to request_irq after "
5686 "soft reset.\n");
5687 goto clean4;
5688 }
5689
5690 rc = hpsa_kdump_soft_reset(h);
5691 if (rc)
5692 /* Neither hard nor soft reset worked, we're hosed. */
5693 goto clean4;
5694
5695 dev_info(&h->pdev->dev, "Board READY.\n");
5696 dev_info(&h->pdev->dev,
5697 "Waiting for stale completions to drain.\n");
5698 h->access.set_intr_mask(h, HPSA_INTR_ON);
5699 msleep(10000);
5700 h->access.set_intr_mask(h, HPSA_INTR_OFF);
5701
5702 rc = controller_reset_failed(h->cfgtable);
5703 if (rc)
5704 dev_info(&h->pdev->dev,
5705 "Soft reset appears to have failed.\n");
5706
5707 /* since the controller's reset, we have to go back and re-init
5708 * everything. Easiest to just forget what we've done and do it
5709 * all over again.
5710 */
5711 hpsa_undo_allocations_after_kdump_soft_reset(h);
5712 try_soft_reset = 0;
5713 if (rc)
5714 /* don't go to clean4, we already unallocated */
5715 return -ENODEV;
5716
5717 goto reinit_after_soft_reset;
5718 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005719
5720 /* Turn the interrupts on so we can service requests */
5721 h->access.set_intr_mask(h, HPSA_INTR_ON);
5722
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06005723 hpsa_hba_inquiry(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005724 hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005725
5726 /* Monitor the controller for firmware lockups */
5727 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
5728 INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
5729 schedule_delayed_work(&h->monitor_ctlr_work,
5730 h->heartbeat_sample_interval);
Stephen M. Cameron88bf6d62013-11-01 11:02:25 -05005731 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005732
5733clean4:
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06005734 hpsa_free_sg_chain_blocks(h);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05005735 hpsa_free_cmd_pool(h);
Matt Gates254f7962012-05-01 11:43:06 -05005736 free_irqs(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005737clean2:
5738clean1:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005739 kfree(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06005740 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005741}
5742
5743static void hpsa_flush_cache(struct ctlr_info *h)
5744{
5745 char *flush_buf;
5746 struct CommandList *c;
Stephen M. Cameron702890e2013-09-23 13:33:30 -05005747 unsigned long flags;
5748
5749 /* Don't bother trying to flush the cache if locked up */
5750 spin_lock_irqsave(&h->lock, flags);
5751 if (unlikely(h->lockup_detected)) {
5752 spin_unlock_irqrestore(&h->lock, flags);
5753 return;
5754 }
5755 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005756
5757 flush_buf = kzalloc(4, GFP_KERNEL);
5758 if (!flush_buf)
5759 return;
5760
5761 c = cmd_special_alloc(h);
5762 if (!c) {
5763 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
5764 goto out_of_memory;
5765 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005766 if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
5767 RAID_CTLR_LUNID, TYPE_CMD)) {
5768 goto out;
5769 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005770 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
5771 if (c->err_info->CommandStatus != 0)
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005772out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005773 dev_warn(&h->pdev->dev,
5774 "error flushing cache on controller\n");
5775 cmd_special_free(h, c);
5776out_of_memory:
5777 kfree(flush_buf);
5778}
5779
5780static void hpsa_shutdown(struct pci_dev *pdev)
5781{
5782 struct ctlr_info *h;
5783
5784 h = pci_get_drvdata(pdev);
5785 /* Turn board interrupts off and send the flush cache command
5786 * sendcmd will turn off interrupt, and send the flush...
5787 * To write all data in the battery backed cache to disks
5788 */
5789 hpsa_flush_cache(h);
5790 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05005791 hpsa_free_irqs_and_disable_msix(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005792}
5793
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005794static void hpsa_free_device_info(struct ctlr_info *h)
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06005795{
5796 int i;
5797
5798 for (i = 0; i < h->ndevices; i++)
5799 kfree(h->dev[i]);
5800}
5801
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005802static void hpsa_remove_one(struct pci_dev *pdev)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005803{
5804 struct ctlr_info *h;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005805 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005806
5807 if (pci_get_drvdata(pdev) == NULL) {
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005808 dev_err(&pdev->dev, "unable to remove device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005809 return;
5810 }
5811 h = pci_get_drvdata(pdev);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005812
5813 /* Get rid of any controller monitoring work items */
5814 spin_lock_irqsave(&h->lock, flags);
5815 h->remove_in_progress = 1;
5816 cancel_delayed_work(&h->monitor_ctlr_work);
5817 spin_unlock_irqrestore(&h->lock, flags);
5818
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005819 hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */
5820 hpsa_shutdown(pdev);
5821 iounmap(h->vaddr);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05005822 iounmap(h->transtable);
5823 iounmap(h->cfgtable);
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06005824 hpsa_free_device_info(h);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06005825 hpsa_free_sg_chain_blocks(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005826 pci_free_consistent(h->pdev,
5827 h->nr_cmds * sizeof(struct CommandList),
5828 h->cmd_pool, h->cmd_pool_dhandle);
5829 pci_free_consistent(h->pdev,
5830 h->nr_cmds * sizeof(struct ErrorInfo),
5831 h->errinfo_pool, h->errinfo_pool_dhandle);
Don Brace303932f2010-02-04 08:42:40 -06005832 pci_free_consistent(h->pdev, h->reply_pool_size,
5833 h->reply_pool, h->reply_pool_dhandle);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005834 kfree(h->cmd_pool_bits);
Don Brace303932f2010-02-04 08:42:40 -06005835 kfree(h->blockFetchTable);
Matt Gatese1f7de02014-02-18 13:55:17 -06005836 kfree(h->ioaccel1_blockFetchTable);
Stephen M. Cameronaca90122014-02-18 13:56:14 -06005837 kfree(h->ioaccel2_blockFetchTable);
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06005838 kfree(h->hba_inquiry_data);
Stephen M. Cameronf0bd0b682012-05-01 11:42:09 -05005839 pci_disable_device(pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005840 pci_release_regions(pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005841 kfree(h);
5842}
5843
5844static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
5845 __attribute__((unused)) pm_message_t state)
5846{
5847 return -ENOSYS;
5848}
5849
5850static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
5851{
5852 return -ENOSYS;
5853}
5854
5855static struct pci_driver hpsa_pci_driver = {
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06005856 .name = HPSA,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005857 .probe = hpsa_init_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005858 .remove = hpsa_remove_one,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005859 .id_table = hpsa_pci_device_id, /* id_table */
5860 .shutdown = hpsa_shutdown,
5861 .suspend = hpsa_suspend,
5862 .resume = hpsa_resume,
5863};
5864
Don Brace303932f2010-02-04 08:42:40 -06005865/* Fill in bucket_map[], given nsgs (the max number of
5866 * scatter gather elements supported) and bucket[],
5867 * which is an array of 8 integers. The bucket[] array
5868 * contains 8 different DMA transfer sizes (in 16
5869 * byte increments) which the controller uses to fetch
5870 * commands. This function fills in bucket_map[], which
5871 * maps a given number of scatter gather elements to one of
5872 * the 8 DMA transfer sizes. The point of it is to allow the
5873 * controller to only do as much DMA as needed to fetch the
5874 * command, with the DMA transfer size encoded in the lower
5875 * bits of the command address.
5876 */
5877static void calc_bucket_map(int bucket[], int num_buckets,
Matt Gatese1f7de02014-02-18 13:55:17 -06005878 int nsgs, int min_blocks, int *bucket_map)
Don Brace303932f2010-02-04 08:42:40 -06005879{
5880 int i, j, b, size;
5881
Don Brace303932f2010-02-04 08:42:40 -06005882 /* Note, bucket_map must have nsgs+1 entries. */
5883 for (i = 0; i <= nsgs; i++) {
5884 /* Compute size of a command with i SG entries */
Matt Gatese1f7de02014-02-18 13:55:17 -06005885 size = i + min_blocks;
Don Brace303932f2010-02-04 08:42:40 -06005886 b = num_buckets; /* Assume the biggest bucket */
5887 /* Find the bucket that is just big enough */
Matt Gatese1f7de02014-02-18 13:55:17 -06005888 for (j = 0; j < num_buckets; j++) {
Don Brace303932f2010-02-04 08:42:40 -06005889 if (bucket[j] >= size) {
5890 b = j;
5891 break;
5892 }
5893 }
5894 /* for a command with i SG entries, use bucket b. */
5895 bucket_map[i] = b;
5896 }
5897}
5898
Matt Gatese1f7de02014-02-18 13:55:17 -06005899static void hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
Don Brace303932f2010-02-04 08:42:40 -06005900{
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05005901 int i;
5902 unsigned long register_value;
Matt Gatese1f7de02014-02-18 13:55:17 -06005903 unsigned long transMethod = CFGTBL_Trans_Performant |
5904 (trans_support & CFGTBL_Trans_use_short_tags) |
5905 CFGTBL_Trans_enable_directed_msix |
5906 (trans_support & CFGTBL_Trans_io_accel1);
5907
5908 struct access_method access = SA5_performant_access;
Stephen M. Camerondef342b2010-05-27 15:14:39 -05005909
5910 /* This is a bit complicated. There are 8 registers on
5911 * the controller which we write to to tell it 8 different
5912 * sizes of commands which there may be. It's a way of
5913 * reducing the DMA done to fetch each command. Encoded into
5914 * each command's tag are 3 bits which communicate to the controller
5915 * which of the eight sizes that command fits within. The size of
5916 * each command depends on how many scatter gather entries there are.
5917 * Each SG entry requires 16 bytes. The eight registers are programmed
5918 * with the number of 16-byte blocks a command of that size requires.
5919 * The smallest command possible requires 5 such 16 byte blocks.
Stephen M. Camerond66ae082012-01-19 14:00:48 -06005920 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
Stephen M. Camerondef342b2010-05-27 15:14:39 -05005921 * blocks. Note, this only extends to the SG entries contained
5922 * within the command block, and does not extend to chained blocks
5923 * of SG elements. bft[] contains the eight values we write to
5924 * the registers. They are not evenly distributed, but have more
5925 * sizes for small commands, and fewer sizes for larger commands.
5926 */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06005927 int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
5928 BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
Don Brace303932f2010-02-04 08:42:40 -06005929 /* 5 = 1 s/g entry or 4k
5930 * 6 = 2 s/g entry or 8k
5931 * 8 = 4 s/g entry or 16k
5932 * 10 = 6 s/g entry or 24k
5933 */
Don Brace303932f2010-02-04 08:42:40 -06005934
Don Brace303932f2010-02-04 08:42:40 -06005935 /* Controller spec: zero out this buffer. */
5936 memset(h->reply_pool, 0, h->reply_pool_size);
Don Brace303932f2010-02-04 08:42:40 -06005937
Stephen M. Camerond66ae082012-01-19 14:00:48 -06005938 bft[7] = SG_ENTRIES_IN_CMD + 4;
5939 calc_bucket_map(bft, ARRAY_SIZE(bft),
Matt Gatese1f7de02014-02-18 13:55:17 -06005940 SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
Don Brace303932f2010-02-04 08:42:40 -06005941 for (i = 0; i < 8; i++)
5942 writel(bft[i], &h->transtable->BlockFetch[i]);
5943
5944 /* size of controller ring buffer */
5945 writel(h->max_commands, &h->transtable->RepQSize);
Matt Gates254f7962012-05-01 11:43:06 -05005946 writel(h->nreply_queues, &h->transtable->RepQCount);
Don Brace303932f2010-02-04 08:42:40 -06005947 writel(0, &h->transtable->RepQCtrAddrLow32);
5948 writel(0, &h->transtable->RepQCtrAddrHigh32);
Matt Gates254f7962012-05-01 11:43:06 -05005949
5950 for (i = 0; i < h->nreply_queues; i++) {
5951 writel(0, &h->transtable->RepQAddr[i].upper);
5952 writel(h->reply_pool_dhandle +
5953 (h->max_commands * sizeof(u64) * i),
5954 &h->transtable->RepQAddr[i].lower);
5955 }
5956
Matt Gatese1f7de02014-02-18 13:55:17 -06005957 writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
5958 /*
5959 * enable outbound interrupt coalescing in accelerator mode;
5960 */
5961 if (trans_support & CFGTBL_Trans_io_accel1) {
5962 access = SA5_ioaccel_mode1_access;
5963 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
5964 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
5965 }
Don Brace303932f2010-02-04 08:42:40 -06005966 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005967 hpsa_wait_for_mode_change_ack(h);
Don Brace303932f2010-02-04 08:42:40 -06005968 register_value = readl(&(h->cfgtable->TransportActive));
5969 if (!(register_value & CFGTBL_Trans_Performant)) {
5970 dev_warn(&h->pdev->dev, "unable to get board into"
5971 " performant mode\n");
5972 return;
5973 }
Stephen M. Cameron960a30e72011-02-15 15:33:03 -06005974 /* Change the access methods to the performant access methods */
Matt Gatese1f7de02014-02-18 13:55:17 -06005975 h->access = access;
5976 h->transMethod = transMethod;
5977
5978 if (!(trans_support & CFGTBL_Trans_io_accel1))
5979 return;
5980
5981 /* Set up I/O accelerator mode */
5982 for (i = 0; i < h->nreply_queues; i++) {
5983 writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
5984 h->reply_queue[i].current_entry =
5985 readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
5986 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005987 bft[7] = h->ioaccel_maxsg + 8;
5988 calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
Matt Gatese1f7de02014-02-18 13:55:17 -06005989 h->ioaccel1_blockFetchTable);
5990
5991 /* initialize all reply queue entries to unused */
5992 memset(h->reply_pool, (u8) IOACCEL_MODE1_REPLY_UNUSED,
5993 h->reply_pool_size);
5994
5995 /* set all the constant fields in the accelerator command
5996 * frames once at init time to save CPU cycles later.
5997 */
5998 for (i = 0; i < h->nr_cmds; i++) {
5999 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
6000
6001 cp->function = IOACCEL1_FUNCTION_SCSIIO;
6002 cp->err_info = (u32) (h->errinfo_pool_dhandle +
6003 (i * sizeof(struct ErrorInfo)));
6004 cp->err_info_len = sizeof(struct ErrorInfo);
6005 cp->sgl_offset = IOACCEL1_SGLOFFSET;
6006 cp->host_context_flags = IOACCEL1_HCFLAGS_CISS_FORMAT;
6007 cp->timeout_sec = 0;
6008 cp->ReplyQueue = 0;
6009 cp->Tag.lower = (i << DIRECT_LOOKUP_SHIFT) | DIRECT_LOOKUP_BIT;
6010 cp->Tag.upper = 0;
6011 cp->host_addr.lower = (u32) (h->ioaccel_cmd_pool_dhandle +
6012 (i * sizeof(struct io_accel1_cmd)));
6013 cp->host_addr.upper = 0;
6014 }
6015}
6016
6017static int hpsa_alloc_ioaccel_cmd_and_bft(struct ctlr_info *h)
6018{
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006019 h->ioaccel_maxsg =
6020 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
6021 if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
6022 h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
6023
Matt Gatese1f7de02014-02-18 13:55:17 -06006024 /* Command structures must be aligned on a 128-byte boundary
6025 * because the 7 lower bits of the address are used by the
6026 * hardware.
6027 */
6028#define IOACCEL1_COMMANDLIST_ALIGNMENT 128
6029 BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
6030 IOACCEL1_COMMANDLIST_ALIGNMENT);
6031 h->ioaccel_cmd_pool =
6032 pci_alloc_consistent(h->pdev,
6033 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
6034 &(h->ioaccel_cmd_pool_dhandle));
6035
6036 h->ioaccel1_blockFetchTable =
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006037 kmalloc(((h->ioaccel_maxsg + 1) *
Matt Gatese1f7de02014-02-18 13:55:17 -06006038 sizeof(u32)), GFP_KERNEL);
6039
6040 if ((h->ioaccel_cmd_pool == NULL) ||
6041 (h->ioaccel1_blockFetchTable == NULL))
6042 goto clean_up;
6043
6044 memset(h->ioaccel_cmd_pool, 0,
6045 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
6046 return 0;
6047
6048clean_up:
6049 if (h->ioaccel_cmd_pool)
6050 pci_free_consistent(h->pdev,
6051 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
6052 h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
6053 kfree(h->ioaccel1_blockFetchTable);
6054 return 1;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006055}
6056
Stephen M. Cameronaca90122014-02-18 13:56:14 -06006057static int ioaccel2_alloc_cmds_and_bft(struct ctlr_info *h)
6058{
6059 /* Allocate ioaccel2 mode command blocks and block fetch table */
6060
6061 h->ioaccel_maxsg =
6062 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
6063 if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
6064 h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
6065
6066#define IOACCEL2_COMMANDLIST_ALIGNMENT 128
6067 BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
6068 IOACCEL2_COMMANDLIST_ALIGNMENT);
6069 h->ioaccel2_cmd_pool =
6070 pci_alloc_consistent(h->pdev,
6071 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
6072 &(h->ioaccel2_cmd_pool_dhandle));
6073
6074 h->ioaccel2_blockFetchTable =
6075 kmalloc(((h->ioaccel_maxsg + 1) *
6076 sizeof(u32)), GFP_KERNEL);
6077
6078 if ((h->ioaccel2_cmd_pool == NULL) ||
6079 (h->ioaccel2_blockFetchTable == NULL))
6080 goto clean_up;
6081
6082 memset(h->ioaccel2_cmd_pool, 0,
6083 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
6084 return 0;
6085
6086clean_up:
6087 if (h->ioaccel2_cmd_pool)
6088 pci_free_consistent(h->pdev,
6089 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
6090 h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
6091 kfree(h->ioaccel2_blockFetchTable);
6092 return 1;
6093}
6094
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006095static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006096{
6097 u32 trans_support;
Matt Gatese1f7de02014-02-18 13:55:17 -06006098 unsigned long transMethod = CFGTBL_Trans_Performant |
6099 CFGTBL_Trans_use_short_tags;
Matt Gates254f7962012-05-01 11:43:06 -05006100 int i;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006101
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -06006102 if (hpsa_simple_mode)
6103 return;
6104
Matt Gatese1f7de02014-02-18 13:55:17 -06006105 /* Check for I/O accelerator mode support */
6106 if (trans_support & CFGTBL_Trans_io_accel1) {
6107 transMethod |= CFGTBL_Trans_io_accel1 |
6108 CFGTBL_Trans_enable_directed_msix;
6109 if (hpsa_alloc_ioaccel_cmd_and_bft(h))
6110 goto clean_up;
Stephen M. Cameronaca90122014-02-18 13:56:14 -06006111 } else {
6112 if (trans_support & CFGTBL_Trans_io_accel2) {
6113 transMethod |= CFGTBL_Trans_io_accel2 |
6114 CFGTBL_Trans_enable_directed_msix;
6115 if (ioaccel2_alloc_cmds_and_bft(h))
6116 goto clean_up;
6117 }
Matt Gatese1f7de02014-02-18 13:55:17 -06006118 }
6119
6120 /* TODO, check that this next line h->nreply_queues is correct */
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006121 trans_support = readl(&(h->cfgtable->TransportSupport));
6122 if (!(trans_support & PERFORMANT_MODE))
6123 return;
6124
Hannes Reineckeeee0f032014-01-15 13:30:53 +01006125 h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05006126 hpsa_get_max_perf_mode_cmds(h);
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006127 /* Performant mode ring buffer and supporting data structures */
Matt Gates254f7962012-05-01 11:43:06 -05006128 h->reply_pool_size = h->max_commands * sizeof(u64) * h->nreply_queues;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006129 h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size,
6130 &(h->reply_pool_dhandle));
6131
Matt Gates254f7962012-05-01 11:43:06 -05006132 for (i = 0; i < h->nreply_queues; i++) {
6133 h->reply_queue[i].head = &h->reply_pool[h->max_commands * i];
6134 h->reply_queue[i].size = h->max_commands;
6135 h->reply_queue[i].wraparound = 1; /* spec: init to 1 */
6136 h->reply_queue[i].current_entry = 0;
6137 }
6138
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006139 /* Need a block fetch table for performant mode */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006140 h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006141 sizeof(u32)), GFP_KERNEL);
6142
6143 if ((h->reply_pool == NULL)
6144 || (h->blockFetchTable == NULL))
6145 goto clean_up;
6146
Matt Gatese1f7de02014-02-18 13:55:17 -06006147 hpsa_enter_performant_mode(h, trans_support);
Don Brace303932f2010-02-04 08:42:40 -06006148 return;
6149
6150clean_up:
6151 if (h->reply_pool)
6152 pci_free_consistent(h->pdev, h->reply_pool_size,
6153 h->reply_pool, h->reply_pool_dhandle);
6154 kfree(h->blockFetchTable);
6155}
6156
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006157static void hpsa_drain_commands(struct ctlr_info *h)
6158{
6159 int cmds_out;
6160 unsigned long flags;
6161
6162 do { /* wait for all outstanding commands to drain out */
6163 spin_lock_irqsave(&h->lock, flags);
6164 cmds_out = h->commands_outstanding;
6165 spin_unlock_irqrestore(&h->lock, flags);
6166 if (cmds_out <= 0)
6167 break;
6168 msleep(100);
6169 } while (1);
6170}
6171
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006172/*
6173 * This is it. Register the PCI driver information for the cards we control
6174 * the OS will call our registered routines when it finds one of our cards.
6175 */
6176static int __init hpsa_init(void)
6177{
Mike Miller31468402010-02-25 14:03:12 -06006178 return pci_register_driver(&hpsa_pci_driver);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006179}
6180
6181static void __exit hpsa_cleanup(void)
6182{
6183 pci_unregister_driver(&hpsa_pci_driver);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006184}
6185
Matt Gatese1f7de02014-02-18 13:55:17 -06006186static void __attribute__((unused)) verify_offsets(void)
6187{
6188#define VERIFY_OFFSET(member, offset) \
Mike Millerb66cc252014-02-18 13:56:04 -06006189 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
6190
6191 VERIFY_OFFSET(IU_type, 0);
6192 VERIFY_OFFSET(direction, 1);
6193 VERIFY_OFFSET(reply_queue, 2);
6194 /* VERIFY_OFFSET(reserved1, 3); */
6195 VERIFY_OFFSET(scsi_nexus, 4);
6196 VERIFY_OFFSET(Tag, 8);
6197 VERIFY_OFFSET(cdb, 16);
6198 VERIFY_OFFSET(cciss_lun, 32);
6199 VERIFY_OFFSET(data_len, 40);
6200 VERIFY_OFFSET(cmd_priority_task_attr, 44);
6201 VERIFY_OFFSET(sg_count, 45);
6202 /* VERIFY_OFFSET(reserved3 */
6203 VERIFY_OFFSET(err_ptr, 48);
6204 VERIFY_OFFSET(err_len, 56);
6205 /* VERIFY_OFFSET(reserved4 */
6206 VERIFY_OFFSET(sg, 64);
6207
6208#undef VERIFY_OFFSET
6209
6210#define VERIFY_OFFSET(member, offset) \
Matt Gatese1f7de02014-02-18 13:55:17 -06006211 BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
6212
6213 VERIFY_OFFSET(dev_handle, 0x00);
6214 VERIFY_OFFSET(reserved1, 0x02);
6215 VERIFY_OFFSET(function, 0x03);
6216 VERIFY_OFFSET(reserved2, 0x04);
6217 VERIFY_OFFSET(err_info, 0x0C);
6218 VERIFY_OFFSET(reserved3, 0x10);
6219 VERIFY_OFFSET(err_info_len, 0x12);
6220 VERIFY_OFFSET(reserved4, 0x13);
6221 VERIFY_OFFSET(sgl_offset, 0x14);
6222 VERIFY_OFFSET(reserved5, 0x15);
6223 VERIFY_OFFSET(transfer_len, 0x1C);
6224 VERIFY_OFFSET(reserved6, 0x20);
6225 VERIFY_OFFSET(io_flags, 0x24);
6226 VERIFY_OFFSET(reserved7, 0x26);
6227 VERIFY_OFFSET(LUN, 0x34);
6228 VERIFY_OFFSET(control, 0x3C);
6229 VERIFY_OFFSET(CDB, 0x40);
6230 VERIFY_OFFSET(reserved8, 0x50);
6231 VERIFY_OFFSET(host_context_flags, 0x60);
6232 VERIFY_OFFSET(timeout_sec, 0x62);
6233 VERIFY_OFFSET(ReplyQueue, 0x64);
6234 VERIFY_OFFSET(reserved9, 0x65);
6235 VERIFY_OFFSET(Tag, 0x68);
6236 VERIFY_OFFSET(host_addr, 0x70);
6237 VERIFY_OFFSET(CISS_LUN, 0x78);
6238 VERIFY_OFFSET(SG, 0x78 + 8);
6239#undef VERIFY_OFFSET
6240}
6241
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006242module_init(hpsa_init);
6243module_exit(hpsa_cleanup);