blob: ee17556058c2cfc7a4b934becd221ee8081328f7 [file] [log] [blame]
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001/*
2 * Disk Array driver for HP Smart Array SAS controllers
3 * Copyright 2000, 2009 Hewlett-Packard Development Company, L.P.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
12 * NON INFRINGEMENT. See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 *
18 * Questions/Comments/Bugfixes to iss_storagedev@hp.com
19 *
20 */
21
22#include <linux/module.h>
23#include <linux/interrupt.h>
24#include <linux/types.h>
25#include <linux/pci.h>
Matthew Garrette5a44df2011-11-11 11:14:23 -050026#include <linux/pci-aspm.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080027#include <linux/kernel.h>
28#include <linux/slab.h>
29#include <linux/delay.h>
30#include <linux/fs.h>
31#include <linux/timer.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080032#include <linux/init.h>
33#include <linux/spinlock.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080034#include <linux/compat.h>
35#include <linux/blktrace_api.h>
36#include <linux/uaccess.h>
37#include <linux/io.h>
38#include <linux/dma-mapping.h>
39#include <linux/completion.h>
40#include <linux/moduleparam.h>
41#include <scsi/scsi.h>
42#include <scsi/scsi_cmnd.h>
43#include <scsi/scsi_device.h>
44#include <scsi/scsi_host.h>
Stephen M. Cameron667e23d2010-02-25 14:02:51 -060045#include <scsi/scsi_tcq.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080046#include <linux/cciss_ioctl.h>
47#include <linux/string.h>
48#include <linux/bitmap.h>
Arun Sharma600634972011-07-26 16:09:06 -070049#include <linux/atomic.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080050#include <linux/kthread.h>
Stephen M. Camerona0c12412011-10-26 16:22:04 -050051#include <linux/jiffies.h>
Stephen M. Cameron283b4a92014-02-18 13:55:33 -060052#include <asm/div64.h>
Stephen M. Cameronedd16362009-12-08 14:09:11 -080053#include "hpsa_cmd.h"
54#include "hpsa.h"
55
56/* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */
Mike Millere481cce2013-09-04 15:12:27 -050057#define HPSA_DRIVER_VERSION "3.4.0-1"
Stephen M. Cameronedd16362009-12-08 14:09:11 -080058#define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -060059#define HPSA "hpsa"
Stephen M. Cameronedd16362009-12-08 14:09:11 -080060
61/* How long to wait (in milliseconds) for board to go into simple mode */
62#define MAX_CONFIG_WAIT 30000
63#define MAX_IOCTL_CONFIG_WAIT 1000
64
65/*define how many times we will try a command because of bus resets */
66#define MAX_CMD_RETRIES 3
67
68/* Embedded module documentation macros - see modules.h */
69MODULE_AUTHOR("Hewlett-Packard Company");
70MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
71 HPSA_DRIVER_VERSION);
72MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
73MODULE_VERSION(HPSA_DRIVER_VERSION);
74MODULE_LICENSE("GPL");
75
76static int hpsa_allow_any;
77module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
78MODULE_PARM_DESC(hpsa_allow_any,
79 "Allow hpsa driver to access unknown HP Smart Array hardware");
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -060080static int hpsa_simple_mode;
81module_param(hpsa_simple_mode, int, S_IRUGO|S_IWUSR);
82MODULE_PARM_DESC(hpsa_simple_mode,
83 "Use 'simple mode' rather than 'performant mode'");
Stephen M. Cameronedd16362009-12-08 14:09:11 -080084
85/* define the PCI info for the cards we can control */
86static const struct pci_device_id hpsa_pci_device_id[] = {
Stephen M. Cameronedd16362009-12-08 14:09:11 -080087 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241},
88 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243},
89 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245},
90 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247},
91 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249},
Mike Miller163dbcd2013-09-04 15:11:10 -050092 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324A},
93 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324B},
Mike Millerf8b01eb2010-02-04 08:42:45 -060094 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3233},
scameron@beardog.cce.hp.com9143a962011-03-07 10:44:16 -060095 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3350},
96 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3351},
97 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3352},
98 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3353},
99 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3354},
100 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3355},
101 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3356},
Mike Millerfe0c9612012-09-20 16:05:18 -0500102 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1921},
103 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1922},
104 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1923},
105 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1924},
106 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1925},
107 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1926},
108 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1928},
Mike Miller97b9f532013-09-04 15:05:55 -0500109 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1929},
110 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BD},
111 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BE},
112 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BF},
113 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C0},
114 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C1},
115 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C2},
116 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C3},
117 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C4},
118 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C5},
119 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C7},
120 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C8},
121 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C9},
Mike Miller7c03b872010-12-01 11:16:07 -0600122 {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
Stephen M. Cameron6798cc02010-06-16 13:51:20 -0500123 PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800124 {0,}
125};
126
127MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
128
129/* board_id = Subsystem Device ID & Vendor ID
130 * product = Marketing Name for the board
131 * access = Address of the struct of function pointers
132 */
133static struct board_type products[] = {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800134 {0x3241103C, "Smart Array P212", &SA5_access},
135 {0x3243103C, "Smart Array P410", &SA5_access},
136 {0x3245103C, "Smart Array P410i", &SA5_access},
137 {0x3247103C, "Smart Array P411", &SA5_access},
138 {0x3249103C, "Smart Array P812", &SA5_access},
Mike Miller163dbcd2013-09-04 15:11:10 -0500139 {0x324A103C, "Smart Array P712m", &SA5_access},
140 {0x324B103C, "Smart Array P711m", &SA5_access},
Mike Millerfe0c9612012-09-20 16:05:18 -0500141 {0x3350103C, "Smart Array P222", &SA5_access},
142 {0x3351103C, "Smart Array P420", &SA5_access},
143 {0x3352103C, "Smart Array P421", &SA5_access},
144 {0x3353103C, "Smart Array P822", &SA5_access},
145 {0x3354103C, "Smart Array P420i", &SA5_access},
146 {0x3355103C, "Smart Array P220i", &SA5_access},
147 {0x3356103C, "Smart Array P721m", &SA5_access},
Mike Miller1fd6c8e2013-09-04 15:08:29 -0500148 {0x1921103C, "Smart Array P830i", &SA5_access},
149 {0x1922103C, "Smart Array P430", &SA5_access},
150 {0x1923103C, "Smart Array P431", &SA5_access},
151 {0x1924103C, "Smart Array P830", &SA5_access},
152 {0x1926103C, "Smart Array P731m", &SA5_access},
153 {0x1928103C, "Smart Array P230i", &SA5_access},
154 {0x1929103C, "Smart Array P530", &SA5_access},
Mike Miller97b9f532013-09-04 15:05:55 -0500155 {0x21BD103C, "Smart Array", &SA5_access},
156 {0x21BE103C, "Smart Array", &SA5_access},
157 {0x21BF103C, "Smart Array", &SA5_access},
158 {0x21C0103C, "Smart Array", &SA5_access},
159 {0x21C1103C, "Smart Array", &SA5_access},
160 {0x21C2103C, "Smart Array", &SA5_access},
161 {0x21C3103C, "Smart Array", &SA5_access},
162 {0x21C4103C, "Smart Array", &SA5_access},
163 {0x21C5103C, "Smart Array", &SA5_access},
164 {0x21C7103C, "Smart Array", &SA5_access},
165 {0x21C8103C, "Smart Array", &SA5_access},
166 {0x21C9103C, "Smart Array", &SA5_access},
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800167 {0xFFFF103C, "Unknown Smart Array", &SA5_access},
168};
169
170static int number_of_controllers;
171
Stephen M. Cameron10f66012010-06-16 13:51:50 -0500172static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id);
173static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800174static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg);
175static void start_io(struct ctlr_info *h);
176
177#ifdef CONFIG_COMPAT
178static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg);
179#endif
180
181static void cmd_free(struct ctlr_info *h, struct CommandList *c);
182static void cmd_special_free(struct ctlr_info *h, struct CommandList *c);
183static struct CommandList *cmd_alloc(struct ctlr_info *h);
184static struct CommandList *cmd_special_alloc(struct ctlr_info *h);
Stephen M. Camerona2dac132013-02-20 11:24:41 -0600185static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -0600186 void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800187 int cmd_type);
188
Jeff Garzikf2812332010-11-16 02:10:29 -0500189static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Stephen M. Camerona08a8472010-02-04 08:43:16 -0600190static void hpsa_scan_start(struct Scsi_Host *);
191static int hpsa_scan_finished(struct Scsi_Host *sh,
192 unsigned long elapsed_time);
Stephen M. Cameron667e23d2010-02-25 14:02:51 -0600193static int hpsa_change_queue_depth(struct scsi_device *sdev,
194 int qdepth, int reason);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800195
196static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500197static int hpsa_eh_abort_handler(struct scsi_cmnd *scsicmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800198static int hpsa_slave_alloc(struct scsi_device *sdev);
199static void hpsa_slave_destroy(struct scsi_device *sdev);
200
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800201static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800202static int check_for_unit_attention(struct ctlr_info *h,
203 struct CommandList *c);
204static void check_ioctl_unit_attention(struct ctlr_info *h,
205 struct CommandList *c);
Don Brace303932f2010-02-04 08:42:40 -0600206/* performant mode helper functions */
207static void calc_bucket_map(int *bucket, int num_buckets,
Matt Gatese1f7de02014-02-18 13:55:17 -0600208 int nsgs, int min_blocks, int *bucket_map);
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800209static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
Matt Gates254f7962012-05-01 11:43:06 -0500210static inline u32 next_command(struct ctlr_info *h, u8 q);
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800211static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
212 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
213 u64 *cfg_offset);
214static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
215 unsigned long *memory_bar);
216static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
217static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
218 int wait_for_ready);
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500219static inline void finish_cmd(struct CommandList *c);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -0600220static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h);
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -0600221#define BOARD_NOT_READY 0
222#define BOARD_READY 1
Stephen M. Cameron76438d02014-02-18 13:55:43 -0600223static void hpsa_drain_commands(struct ctlr_info *h);
224static void hpsa_flush_cache(struct ctlr_info *h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800225
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800226static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
227{
228 unsigned long *priv = shost_priv(sdev->host);
229 return (struct ctlr_info *) *priv;
230}
231
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600232static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
233{
234 unsigned long *priv = shost_priv(sh);
235 return (struct ctlr_info *) *priv;
236}
237
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800238static int check_for_unit_attention(struct ctlr_info *h,
239 struct CommandList *c)
240{
241 if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
242 return 0;
243
244 switch (c->err_info->SenseInfo[12]) {
245 case STATE_CHANGED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600246 dev_warn(&h->pdev->dev, HPSA "%d: a state change "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800247 "detected, command retried\n", h->ctlr);
248 break;
249 case LUN_FAILED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600250 dev_warn(&h->pdev->dev, HPSA "%d: LUN failure "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800251 "detected, action required\n", h->ctlr);
252 break;
253 case REPORT_LUNS_CHANGED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600254 dev_warn(&h->pdev->dev, HPSA "%d: report LUN data "
Mike Miller31468402010-02-25 14:03:12 -0600255 "changed, action required\n", h->ctlr);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800256 /*
Scott Teel4f4eb9f2012-01-19 14:01:25 -0600257 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
258 * target (array) devices.
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800259 */
260 break;
261 case POWER_OR_RESET:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600262 dev_warn(&h->pdev->dev, HPSA "%d: a power on "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800263 "or device reset detected\n", h->ctlr);
264 break;
265 case UNIT_ATTENTION_CLEARED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600266 dev_warn(&h->pdev->dev, HPSA "%d: unit attention "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800267 "cleared by another initiator\n", h->ctlr);
268 break;
269 default:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600270 dev_warn(&h->pdev->dev, HPSA "%d: unknown "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800271 "unit attention detected\n", h->ctlr);
272 break;
273 }
274 return 1;
275}
276
Matt Bondurant852af202012-05-01 11:42:35 -0500277static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
278{
279 if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
280 (c->err_info->ScsiStatus != SAM_STAT_BUSY &&
281 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
282 return 0;
283 dev_warn(&h->pdev->dev, HPSA "device busy");
284 return 1;
285}
286
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800287static ssize_t host_store_rescan(struct device *dev,
288 struct device_attribute *attr,
289 const char *buf, size_t count)
290{
291 struct ctlr_info *h;
292 struct Scsi_Host *shost = class_to_shost(dev);
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600293 h = shost_to_hba(shost);
Mike Miller31468402010-02-25 14:03:12 -0600294 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800295 return count;
296}
297
Stephen M. Camerond28ce022010-05-27 15:14:34 -0500298static ssize_t host_show_firmware_revision(struct device *dev,
299 struct device_attribute *attr, char *buf)
300{
301 struct ctlr_info *h;
302 struct Scsi_Host *shost = class_to_shost(dev);
303 unsigned char *fwrev;
304
305 h = shost_to_hba(shost);
306 if (!h->hba_inquiry_data)
307 return 0;
308 fwrev = &h->hba_inquiry_data[32];
309 return snprintf(buf, 20, "%c%c%c%c\n",
310 fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
311}
312
Stephen M. Cameron94a13642011-01-06 14:48:39 -0600313static ssize_t host_show_commands_outstanding(struct device *dev,
314 struct device_attribute *attr, char *buf)
315{
316 struct Scsi_Host *shost = class_to_shost(dev);
317 struct ctlr_info *h = shost_to_hba(shost);
318
319 return snprintf(buf, 20, "%d\n", h->commands_outstanding);
320}
321
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600322static ssize_t host_show_transport_mode(struct device *dev,
323 struct device_attribute *attr, char *buf)
324{
325 struct ctlr_info *h;
326 struct Scsi_Host *shost = class_to_shost(dev);
327
328 h = shost_to_hba(shost);
329 return snprintf(buf, 20, "%s\n",
Stephen M. Cameron960a30e2011-02-15 15:33:03 -0600330 h->transMethod & CFGTBL_Trans_Performant ?
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600331 "performant" : "simple");
332}
333
Stephen M. Cameron46380782011-05-03 15:00:01 -0500334/* List of controllers which cannot be hard reset on kexec with reset_devices */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600335static u32 unresettable_controller[] = {
336 0x324a103C, /* Smart Array P712m */
337 0x324b103C, /* SmartArray P711m */
338 0x3223103C, /* Smart Array P800 */
339 0x3234103C, /* Smart Array P400 */
340 0x3235103C, /* Smart Array P400i */
341 0x3211103C, /* Smart Array E200i */
342 0x3212103C, /* Smart Array E200 */
343 0x3213103C, /* Smart Array E200i */
344 0x3214103C, /* Smart Array E200i */
345 0x3215103C, /* Smart Array E200i */
346 0x3237103C, /* Smart Array E500 */
347 0x323D103C, /* Smart Array P700m */
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100348 0x40800E11, /* Smart Array 5i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600349 0x409C0E11, /* Smart Array 6400 */
350 0x409D0E11, /* Smart Array 6400 EM */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100351 0x40700E11, /* Smart Array 5300 */
352 0x40820E11, /* Smart Array 532 */
353 0x40830E11, /* Smart Array 5312 */
354 0x409A0E11, /* Smart Array 641 */
355 0x409B0E11, /* Smart Array 642 */
356 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600357};
358
Stephen M. Cameron46380782011-05-03 15:00:01 -0500359/* List of controllers which cannot even be soft reset */
360static u32 soft_unresettable_controller[] = {
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100361 0x40800E11, /* Smart Array 5i */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100362 0x40700E11, /* Smart Array 5300 */
363 0x40820E11, /* Smart Array 532 */
364 0x40830E11, /* Smart Array 5312 */
365 0x409A0E11, /* Smart Array 641 */
366 0x409B0E11, /* Smart Array 642 */
367 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron46380782011-05-03 15:00:01 -0500368 /* Exclude 640x boards. These are two pci devices in one slot
369 * which share a battery backed cache module. One controls the
370 * cache, the other accesses the cache through the one that controls
371 * it. If we reset the one controlling the cache, the other will
372 * likely not be happy. Just forbid resetting this conjoined mess.
373 * The 640x isn't really supported by hpsa anyway.
374 */
375 0x409C0E11, /* Smart Array 6400 */
376 0x409D0E11, /* Smart Array 6400 EM */
377};
378
379static int ctlr_is_hard_resettable(u32 board_id)
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600380{
381 int i;
382
383 for (i = 0; i < ARRAY_SIZE(unresettable_controller); i++)
Stephen M. Cameron46380782011-05-03 15:00:01 -0500384 if (unresettable_controller[i] == board_id)
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600385 return 0;
386 return 1;
387}
388
Stephen M. Cameron46380782011-05-03 15:00:01 -0500389static int ctlr_is_soft_resettable(u32 board_id)
390{
391 int i;
392
393 for (i = 0; i < ARRAY_SIZE(soft_unresettable_controller); i++)
394 if (soft_unresettable_controller[i] == board_id)
395 return 0;
396 return 1;
397}
398
399static int ctlr_is_resettable(u32 board_id)
400{
401 return ctlr_is_hard_resettable(board_id) ||
402 ctlr_is_soft_resettable(board_id);
403}
404
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600405static ssize_t host_show_resettable(struct device *dev,
406 struct device_attribute *attr, char *buf)
407{
408 struct ctlr_info *h;
409 struct Scsi_Host *shost = class_to_shost(dev);
410
411 h = shost_to_hba(shost);
Stephen M. Cameron46380782011-05-03 15:00:01 -0500412 return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600413}
414
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800415static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
416{
417 return (scsi3addr[3] & 0xC0) == 0x40;
418}
419
420static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
Mike Millerd82357e2012-05-01 11:43:32 -0500421 "1(ADM)", "UNKNOWN"
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800422};
423#define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
424
425static ssize_t raid_level_show(struct device *dev,
426 struct device_attribute *attr, char *buf)
427{
428 ssize_t l = 0;
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600429 unsigned char rlevel;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800430 struct ctlr_info *h;
431 struct scsi_device *sdev;
432 struct hpsa_scsi_dev_t *hdev;
433 unsigned long flags;
434
435 sdev = to_scsi_device(dev);
436 h = sdev_to_hba(sdev);
437 spin_lock_irqsave(&h->lock, flags);
438 hdev = sdev->hostdata;
439 if (!hdev) {
440 spin_unlock_irqrestore(&h->lock, flags);
441 return -ENODEV;
442 }
443
444 /* Is this even a logical drive? */
445 if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
446 spin_unlock_irqrestore(&h->lock, flags);
447 l = snprintf(buf, PAGE_SIZE, "N/A\n");
448 return l;
449 }
450
451 rlevel = hdev->raid_level;
452 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600453 if (rlevel > RAID_UNKNOWN)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800454 rlevel = RAID_UNKNOWN;
455 l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
456 return l;
457}
458
459static ssize_t lunid_show(struct device *dev,
460 struct device_attribute *attr, char *buf)
461{
462 struct ctlr_info *h;
463 struct scsi_device *sdev;
464 struct hpsa_scsi_dev_t *hdev;
465 unsigned long flags;
466 unsigned char lunid[8];
467
468 sdev = to_scsi_device(dev);
469 h = sdev_to_hba(sdev);
470 spin_lock_irqsave(&h->lock, flags);
471 hdev = sdev->hostdata;
472 if (!hdev) {
473 spin_unlock_irqrestore(&h->lock, flags);
474 return -ENODEV;
475 }
476 memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
477 spin_unlock_irqrestore(&h->lock, flags);
478 return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
479 lunid[0], lunid[1], lunid[2], lunid[3],
480 lunid[4], lunid[5], lunid[6], lunid[7]);
481}
482
483static ssize_t unique_id_show(struct device *dev,
484 struct device_attribute *attr, char *buf)
485{
486 struct ctlr_info *h;
487 struct scsi_device *sdev;
488 struct hpsa_scsi_dev_t *hdev;
489 unsigned long flags;
490 unsigned char sn[16];
491
492 sdev = to_scsi_device(dev);
493 h = sdev_to_hba(sdev);
494 spin_lock_irqsave(&h->lock, flags);
495 hdev = sdev->hostdata;
496 if (!hdev) {
497 spin_unlock_irqrestore(&h->lock, flags);
498 return -ENODEV;
499 }
500 memcpy(sn, hdev->device_id, sizeof(sn));
501 spin_unlock_irqrestore(&h->lock, flags);
502 return snprintf(buf, 16 * 2 + 2,
503 "%02X%02X%02X%02X%02X%02X%02X%02X"
504 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
505 sn[0], sn[1], sn[2], sn[3],
506 sn[4], sn[5], sn[6], sn[7],
507 sn[8], sn[9], sn[10], sn[11],
508 sn[12], sn[13], sn[14], sn[15]);
509}
510
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600511static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
512static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
513static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
514static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
515static DEVICE_ATTR(firmware_revision, S_IRUGO,
516 host_show_firmware_revision, NULL);
517static DEVICE_ATTR(commands_outstanding, S_IRUGO,
518 host_show_commands_outstanding, NULL);
519static DEVICE_ATTR(transport_mode, S_IRUGO,
520 host_show_transport_mode, NULL);
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600521static DEVICE_ATTR(resettable, S_IRUGO,
522 host_show_resettable, NULL);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600523
524static struct device_attribute *hpsa_sdev_attrs[] = {
525 &dev_attr_raid_level,
526 &dev_attr_lunid,
527 &dev_attr_unique_id,
528 NULL,
529};
530
531static struct device_attribute *hpsa_shost_attrs[] = {
532 &dev_attr_rescan,
533 &dev_attr_firmware_revision,
534 &dev_attr_commands_outstanding,
535 &dev_attr_transport_mode,
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600536 &dev_attr_resettable,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600537 NULL,
538};
539
540static struct scsi_host_template hpsa_driver_template = {
541 .module = THIS_MODULE,
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600542 .name = HPSA,
543 .proc_name = HPSA,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600544 .queuecommand = hpsa_scsi_queue_command,
545 .scan_start = hpsa_scan_start,
546 .scan_finished = hpsa_scan_finished,
547 .change_queue_depth = hpsa_change_queue_depth,
548 .this_id = -1,
549 .use_clustering = ENABLE_CLUSTERING,
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500550 .eh_abort_handler = hpsa_eh_abort_handler,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600551 .eh_device_reset_handler = hpsa_eh_device_reset_handler,
552 .ioctl = hpsa_ioctl,
553 .slave_alloc = hpsa_slave_alloc,
554 .slave_destroy = hpsa_slave_destroy,
555#ifdef CONFIG_COMPAT
556 .compat_ioctl = hpsa_compat_ioctl,
557#endif
558 .sdev_attrs = hpsa_sdev_attrs,
559 .shost_attrs = hpsa_shost_attrs,
Stephen M. Cameronc0d6a4d2011-10-26 16:20:53 -0500560 .max_sectors = 8192,
Martin K. Petersen54b2b502013-10-23 06:25:40 -0400561 .no_write_same = 1,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600562};
563
564
565/* Enqueuing and dequeuing functions for cmdlists. */
566static inline void addQ(struct list_head *list, struct CommandList *c)
567{
568 list_add_tail(&c->list, list);
569}
570
Matt Gates254f7962012-05-01 11:43:06 -0500571static inline u32 next_command(struct ctlr_info *h, u8 q)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600572{
573 u32 a;
Matt Gates254f7962012-05-01 11:43:06 -0500574 struct reply_pool *rq = &h->reply_queue[q];
Matt Gatese16a33a2012-05-01 11:43:11 -0500575 unsigned long flags;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600576
Matt Gatese1f7de02014-02-18 13:55:17 -0600577 if (h->transMethod & CFGTBL_Trans_io_accel1)
578 return h->access.command_completed(h, q);
579
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600580 if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
Matt Gates254f7962012-05-01 11:43:06 -0500581 return h->access.command_completed(h, q);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600582
Matt Gates254f7962012-05-01 11:43:06 -0500583 if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
584 a = rq->head[rq->current_entry];
585 rq->current_entry++;
Matt Gatese16a33a2012-05-01 11:43:11 -0500586 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600587 h->commands_outstanding--;
Matt Gatese16a33a2012-05-01 11:43:11 -0500588 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600589 } else {
590 a = FIFO_EMPTY;
591 }
592 /* Check for wraparound */
Matt Gates254f7962012-05-01 11:43:06 -0500593 if (rq->current_entry == h->max_commands) {
594 rq->current_entry = 0;
595 rq->wraparound ^= 1;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600596 }
597 return a;
598}
599
600/* set_performant_mode: Modify the tag for cciss performant
601 * set bit 0 for pull model, bits 3-1 for block fetch
602 * register number
603 */
604static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
605{
Matt Gates254f7962012-05-01 11:43:06 -0500606 if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600607 c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
Hannes Reineckeeee0f032014-01-15 13:30:53 +0100608 if (likely(h->msix_vector > 0))
Matt Gates254f7962012-05-01 11:43:06 -0500609 c->Header.ReplyQueue =
John Kacur804a5cb2013-07-26 16:06:18 +0200610 raw_smp_processor_id() % h->nreply_queues;
Matt Gates254f7962012-05-01 11:43:06 -0500611 }
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600612}
613
Stephen M. Camerone85c5972012-05-01 11:43:42 -0500614static int is_firmware_flash_cmd(u8 *cdb)
615{
616 return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
617}
618
619/*
620 * During firmware flash, the heartbeat register may not update as frequently
621 * as it should. So we dial down lockup detection during firmware flash. and
622 * dial it back up when firmware flash completes.
623 */
624#define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
625#define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
626static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
627 struct CommandList *c)
628{
629 if (!is_firmware_flash_cmd(c->Request.CDB))
630 return;
631 atomic_inc(&h->firmware_flash_in_progress);
632 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
633}
634
635static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
636 struct CommandList *c)
637{
638 if (is_firmware_flash_cmd(c->Request.CDB) &&
639 atomic_dec_and_test(&h->firmware_flash_in_progress))
640 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
641}
642
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600643static void enqueue_cmd_and_start_io(struct ctlr_info *h,
644 struct CommandList *c)
645{
646 unsigned long flags;
647
648 set_performant_mode(h, c);
Stephen M. Camerone85c5972012-05-01 11:43:42 -0500649 dial_down_lockup_detection_during_fw_flash(h, c);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600650 spin_lock_irqsave(&h->lock, flags);
651 addQ(&h->reqQ, c);
652 h->Qdepth++;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600653 spin_unlock_irqrestore(&h->lock, flags);
Matt Gatese16a33a2012-05-01 11:43:11 -0500654 start_io(h);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600655}
656
657static inline void removeQ(struct CommandList *c)
658{
659 if (WARN_ON(list_empty(&c->list)))
660 return;
661 list_del_init(&c->list);
662}
663
664static inline int is_hba_lunid(unsigned char scsi3addr[])
665{
666 return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
667}
668
669static inline int is_scsi_rev_5(struct ctlr_info *h)
670{
671 if (!h->hba_inquiry_data)
672 return 0;
673 if ((h->hba_inquiry_data[2] & 0x07) == 5)
674 return 1;
675 return 0;
676}
677
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800678static int hpsa_find_target_lun(struct ctlr_info *h,
679 unsigned char scsi3addr[], int bus, int *target, int *lun)
680{
681 /* finds an unused bus, target, lun for a new physical device
682 * assumes h->devlock is held
683 */
684 int i, found = 0;
Scott Teelcfe5bad2011-10-26 16:21:07 -0500685 DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800686
Akinobu Mita263d9402012-01-21 00:15:27 +0900687 bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800688
689 for (i = 0; i < h->ndevices; i++) {
690 if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
Akinobu Mita263d9402012-01-21 00:15:27 +0900691 __set_bit(h->dev[i]->target, lun_taken);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800692 }
693
Akinobu Mita263d9402012-01-21 00:15:27 +0900694 i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
695 if (i < HPSA_MAX_DEVICES) {
696 /* *bus = 1; */
697 *target = i;
698 *lun = 0;
699 found = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800700 }
701 return !found;
702}
703
704/* Add an entry into h->dev[] array. */
705static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
706 struct hpsa_scsi_dev_t *device,
707 struct hpsa_scsi_dev_t *added[], int *nadded)
708{
709 /* assumes h->devlock is held */
710 int n = h->ndevices;
711 int i;
712 unsigned char addr1[8], addr2[8];
713 struct hpsa_scsi_dev_t *sd;
714
Scott Teelcfe5bad2011-10-26 16:21:07 -0500715 if (n >= HPSA_MAX_DEVICES) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800716 dev_err(&h->pdev->dev, "too many devices, some will be "
717 "inaccessible.\n");
718 return -1;
719 }
720
721 /* physical devices do not have lun or target assigned until now. */
722 if (device->lun != -1)
723 /* Logical device, lun is already assigned. */
724 goto lun_assigned;
725
726 /* If this device a non-zero lun of a multi-lun device
727 * byte 4 of the 8-byte LUN addr will contain the logical
728 * unit no, zero otherise.
729 */
730 if (device->scsi3addr[4] == 0) {
731 /* This is not a non-zero lun of a multi-lun device */
732 if (hpsa_find_target_lun(h, device->scsi3addr,
733 device->bus, &device->target, &device->lun) != 0)
734 return -1;
735 goto lun_assigned;
736 }
737
738 /* This is a non-zero lun of a multi-lun device.
739 * Search through our list and find the device which
740 * has the same 8 byte LUN address, excepting byte 4.
741 * Assign the same bus and target for this new LUN.
742 * Use the logical unit number from the firmware.
743 */
744 memcpy(addr1, device->scsi3addr, 8);
745 addr1[4] = 0;
746 for (i = 0; i < n; i++) {
747 sd = h->dev[i];
748 memcpy(addr2, sd->scsi3addr, 8);
749 addr2[4] = 0;
750 /* differ only in byte 4? */
751 if (memcmp(addr1, addr2, 8) == 0) {
752 device->bus = sd->bus;
753 device->target = sd->target;
754 device->lun = device->scsi3addr[4];
755 break;
756 }
757 }
758 if (device->lun == -1) {
759 dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
760 " suspect firmware bug or unsupported hardware "
761 "configuration.\n");
762 return -1;
763 }
764
765lun_assigned:
766
767 h->dev[n] = device;
768 h->ndevices++;
769 added[*nadded] = device;
770 (*nadded)++;
771
772 /* initially, (before registering with scsi layer) we don't
773 * know our hostno and we don't want to print anything first
774 * time anyway (the scsi layer's inquiries will show that info)
775 */
776 /* if (hostno != -1) */
777 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
778 scsi_device_type(device->devtype), hostno,
779 device->bus, device->target, device->lun);
780 return 0;
781}
782
Scott Teelbd9244f2012-01-19 14:01:30 -0600783/* Update an entry in h->dev[] array. */
784static void hpsa_scsi_update_entry(struct ctlr_info *h, int hostno,
785 int entry, struct hpsa_scsi_dev_t *new_entry)
786{
787 /* assumes h->devlock is held */
788 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
789
790 /* Raid level changed. */
791 h->dev[entry]->raid_level = new_entry->raid_level;
Stephen M. Cameron250fb122014-02-18 13:55:38 -0600792
793 /* Raid offload parameters changed. */
794 h->dev[entry]->offload_config = new_entry->offload_config;
795 h->dev[entry]->offload_enabled = new_entry->offload_enabled;
796
Scott Teelbd9244f2012-01-19 14:01:30 -0600797 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d updated.\n",
798 scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
799 new_entry->target, new_entry->lun);
800}
801
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -0600802/* Replace an entry from h->dev[] array. */
803static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
804 int entry, struct hpsa_scsi_dev_t *new_entry,
805 struct hpsa_scsi_dev_t *added[], int *nadded,
806 struct hpsa_scsi_dev_t *removed[], int *nremoved)
807{
808 /* assumes h->devlock is held */
Scott Teelcfe5bad2011-10-26 16:21:07 -0500809 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -0600810 removed[*nremoved] = h->dev[entry];
811 (*nremoved)++;
Stephen M. Cameron01350d02011-08-09 08:18:01 -0500812
813 /*
814 * New physical devices won't have target/lun assigned yet
815 * so we need to preserve the values in the slot we are replacing.
816 */
817 if (new_entry->target == -1) {
818 new_entry->target = h->dev[entry]->target;
819 new_entry->lun = h->dev[entry]->lun;
820 }
821
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -0600822 h->dev[entry] = new_entry;
823 added[*nadded] = new_entry;
824 (*nadded)++;
825 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n",
826 scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
827 new_entry->target, new_entry->lun);
828}
829
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800830/* Remove an entry from h->dev[] array. */
831static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
832 struct hpsa_scsi_dev_t *removed[], int *nremoved)
833{
834 /* assumes h->devlock is held */
835 int i;
836 struct hpsa_scsi_dev_t *sd;
837
Scott Teelcfe5bad2011-10-26 16:21:07 -0500838 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800839
840 sd = h->dev[entry];
841 removed[*nremoved] = h->dev[entry];
842 (*nremoved)++;
843
844 for (i = entry; i < h->ndevices-1; i++)
845 h->dev[i] = h->dev[i+1];
846 h->ndevices--;
847 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
848 scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
849 sd->lun);
850}
851
852#define SCSI3ADDR_EQ(a, b) ( \
853 (a)[7] == (b)[7] && \
854 (a)[6] == (b)[6] && \
855 (a)[5] == (b)[5] && \
856 (a)[4] == (b)[4] && \
857 (a)[3] == (b)[3] && \
858 (a)[2] == (b)[2] && \
859 (a)[1] == (b)[1] && \
860 (a)[0] == (b)[0])
861
862static void fixup_botched_add(struct ctlr_info *h,
863 struct hpsa_scsi_dev_t *added)
864{
865 /* called when scsi_add_device fails in order to re-adjust
866 * h->dev[] to match the mid layer's view.
867 */
868 unsigned long flags;
869 int i, j;
870
871 spin_lock_irqsave(&h->lock, flags);
872 for (i = 0; i < h->ndevices; i++) {
873 if (h->dev[i] == added) {
874 for (j = i; j < h->ndevices-1; j++)
875 h->dev[j] = h->dev[j+1];
876 h->ndevices--;
877 break;
878 }
879 }
880 spin_unlock_irqrestore(&h->lock, flags);
881 kfree(added);
882}
883
884static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
885 struct hpsa_scsi_dev_t *dev2)
886{
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800887 /* we compare everything except lun and target as these
888 * are not yet assigned. Compare parts likely
889 * to differ first
890 */
891 if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
892 sizeof(dev1->scsi3addr)) != 0)
893 return 0;
894 if (memcmp(dev1->device_id, dev2->device_id,
895 sizeof(dev1->device_id)) != 0)
896 return 0;
897 if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
898 return 0;
899 if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
900 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800901 if (dev1->devtype != dev2->devtype)
902 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800903 if (dev1->bus != dev2->bus)
904 return 0;
905 return 1;
906}
907
Scott Teelbd9244f2012-01-19 14:01:30 -0600908static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
909 struct hpsa_scsi_dev_t *dev2)
910{
911 /* Device attributes that can change, but don't mean
912 * that the device is a different device, nor that the OS
913 * needs to be told anything about the change.
914 */
915 if (dev1->raid_level != dev2->raid_level)
916 return 1;
Stephen M. Cameron250fb122014-02-18 13:55:38 -0600917 if (dev1->offload_config != dev2->offload_config)
918 return 1;
919 if (dev1->offload_enabled != dev2->offload_enabled)
920 return 1;
Scott Teelbd9244f2012-01-19 14:01:30 -0600921 return 0;
922}
923
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800924/* Find needle in haystack. If exact match found, return DEVICE_SAME,
925 * and return needle location in *index. If scsi3addr matches, but not
926 * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
Scott Teelbd9244f2012-01-19 14:01:30 -0600927 * location in *index.
928 * In the case of a minor device attribute change, such as RAID level, just
929 * return DEVICE_UPDATED, along with the updated device's location in index.
930 * If needle not found, return DEVICE_NOT_FOUND.
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800931 */
932static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
933 struct hpsa_scsi_dev_t *haystack[], int haystack_size,
934 int *index)
935{
936 int i;
937#define DEVICE_NOT_FOUND 0
938#define DEVICE_CHANGED 1
939#define DEVICE_SAME 2
Scott Teelbd9244f2012-01-19 14:01:30 -0600940#define DEVICE_UPDATED 3
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800941 for (i = 0; i < haystack_size; i++) {
Stephen M. Cameron23231042010-02-04 08:43:36 -0600942 if (haystack[i] == NULL) /* previously removed. */
943 continue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800944 if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
945 *index = i;
Scott Teelbd9244f2012-01-19 14:01:30 -0600946 if (device_is_the_same(needle, haystack[i])) {
947 if (device_updated(needle, haystack[i]))
948 return DEVICE_UPDATED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800949 return DEVICE_SAME;
Scott Teelbd9244f2012-01-19 14:01:30 -0600950 } else {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800951 return DEVICE_CHANGED;
Scott Teelbd9244f2012-01-19 14:01:30 -0600952 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800953 }
954 }
955 *index = -1;
956 return DEVICE_NOT_FOUND;
957}
958
Stephen M. Cameron4967bd32010-02-04 08:41:49 -0600959static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800960 struct hpsa_scsi_dev_t *sd[], int nsds)
961{
962 /* sd contains scsi3 addresses and devtypes, and inquiry
963 * data. This function takes what's in sd to be the current
964 * reality and updates h->dev[] to reflect that reality.
965 */
966 int i, entry, device_change, changes = 0;
967 struct hpsa_scsi_dev_t *csd;
968 unsigned long flags;
969 struct hpsa_scsi_dev_t **added, **removed;
970 int nadded, nremoved;
971 struct Scsi_Host *sh = NULL;
972
Scott Teelcfe5bad2011-10-26 16:21:07 -0500973 added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
974 removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800975
976 if (!added || !removed) {
977 dev_warn(&h->pdev->dev, "out of memory in "
978 "adjust_hpsa_scsi_table\n");
979 goto free_and_out;
980 }
981
982 spin_lock_irqsave(&h->devlock, flags);
983
984 /* find any devices in h->dev[] that are not in
985 * sd[] and remove them from h->dev[], and for any
986 * devices which have changed, remove the old device
987 * info and add the new device info.
Scott Teelbd9244f2012-01-19 14:01:30 -0600988 * If minor device attributes change, just update
989 * the existing device structure.
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800990 */
991 i = 0;
992 nremoved = 0;
993 nadded = 0;
994 while (i < h->ndevices) {
995 csd = h->dev[i];
996 device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
997 if (device_change == DEVICE_NOT_FOUND) {
998 changes++;
999 hpsa_scsi_remove_entry(h, hostno, i,
1000 removed, &nremoved);
1001 continue; /* remove ^^^, hence i not incremented */
1002 } else if (device_change == DEVICE_CHANGED) {
1003 changes++;
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001004 hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
1005 added, &nadded, removed, &nremoved);
Stephen M. Cameronc7f172d2010-02-04 08:43:31 -06001006 /* Set it to NULL to prevent it from being freed
1007 * at the bottom of hpsa_update_scsi_devices()
1008 */
1009 sd[entry] = NULL;
Scott Teelbd9244f2012-01-19 14:01:30 -06001010 } else if (device_change == DEVICE_UPDATED) {
1011 hpsa_scsi_update_entry(h, hostno, i, sd[entry]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001012 }
1013 i++;
1014 }
1015
1016 /* Now, make sure every device listed in sd[] is also
1017 * listed in h->dev[], adding them if they aren't found
1018 */
1019
1020 for (i = 0; i < nsds; i++) {
1021 if (!sd[i]) /* if already added above. */
1022 continue;
1023 device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1024 h->ndevices, &entry);
1025 if (device_change == DEVICE_NOT_FOUND) {
1026 changes++;
1027 if (hpsa_scsi_add_entry(h, hostno, sd[i],
1028 added, &nadded) != 0)
1029 break;
1030 sd[i] = NULL; /* prevent from being freed later. */
1031 } else if (device_change == DEVICE_CHANGED) {
1032 /* should never happen... */
1033 changes++;
1034 dev_warn(&h->pdev->dev,
1035 "device unexpectedly changed.\n");
1036 /* but if it does happen, we just ignore that device */
1037 }
1038 }
1039 spin_unlock_irqrestore(&h->devlock, flags);
1040
1041 /* Don't notify scsi mid layer of any changes the first time through
1042 * (or if there are no changes) scsi_scan_host will do it later the
1043 * first time through.
1044 */
1045 if (hostno == -1 || !changes)
1046 goto free_and_out;
1047
1048 sh = h->scsi_host;
1049 /* Notify scsi mid layer of any removed devices */
1050 for (i = 0; i < nremoved; i++) {
1051 struct scsi_device *sdev =
1052 scsi_device_lookup(sh, removed[i]->bus,
1053 removed[i]->target, removed[i]->lun);
1054 if (sdev != NULL) {
1055 scsi_remove_device(sdev);
1056 scsi_device_put(sdev);
1057 } else {
1058 /* We don't expect to get here.
1059 * future cmds to this device will get selection
1060 * timeout as if the device was gone.
1061 */
1062 dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
1063 " for removal.", hostno, removed[i]->bus,
1064 removed[i]->target, removed[i]->lun);
1065 }
1066 kfree(removed[i]);
1067 removed[i] = NULL;
1068 }
1069
1070 /* Notify scsi mid layer of any added devices */
1071 for (i = 0; i < nadded; i++) {
1072 if (scsi_add_device(sh, added[i]->bus,
1073 added[i]->target, added[i]->lun) == 0)
1074 continue;
1075 dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
1076 "device not added.\n", hostno, added[i]->bus,
1077 added[i]->target, added[i]->lun);
1078 /* now we have to remove it from h->dev,
1079 * since it didn't get added to scsi mid layer
1080 */
1081 fixup_botched_add(h, added[i]);
1082 }
1083
1084free_and_out:
1085 kfree(added);
1086 kfree(removed);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001087}
1088
1089/*
Joe Perches9e03aa22013-09-03 13:45:58 -07001090 * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001091 * Assume's h->devlock is held.
1092 */
1093static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1094 int bus, int target, int lun)
1095{
1096 int i;
1097 struct hpsa_scsi_dev_t *sd;
1098
1099 for (i = 0; i < h->ndevices; i++) {
1100 sd = h->dev[i];
1101 if (sd->bus == bus && sd->target == target && sd->lun == lun)
1102 return sd;
1103 }
1104 return NULL;
1105}
1106
1107/* link sdev->hostdata to our per-device structure. */
1108static int hpsa_slave_alloc(struct scsi_device *sdev)
1109{
1110 struct hpsa_scsi_dev_t *sd;
1111 unsigned long flags;
1112 struct ctlr_info *h;
1113
1114 h = sdev_to_hba(sdev);
1115 spin_lock_irqsave(&h->devlock, flags);
1116 sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1117 sdev_id(sdev), sdev->lun);
1118 if (sd != NULL)
1119 sdev->hostdata = sd;
1120 spin_unlock_irqrestore(&h->devlock, flags);
1121 return 0;
1122}
1123
1124static void hpsa_slave_destroy(struct scsi_device *sdev)
1125{
Stephen M. Cameronbcc44252010-02-04 08:41:54 -06001126 /* nothing to do. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001127}
1128
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001129static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
1130{
1131 int i;
1132
1133 if (!h->cmd_sg_list)
1134 return;
1135 for (i = 0; i < h->nr_cmds; i++) {
1136 kfree(h->cmd_sg_list[i]);
1137 h->cmd_sg_list[i] = NULL;
1138 }
1139 kfree(h->cmd_sg_list);
1140 h->cmd_sg_list = NULL;
1141}
1142
1143static int hpsa_allocate_sg_chain_blocks(struct ctlr_info *h)
1144{
1145 int i;
1146
1147 if (h->chainsize <= 0)
1148 return 0;
1149
1150 h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
1151 GFP_KERNEL);
1152 if (!h->cmd_sg_list)
1153 return -ENOMEM;
1154 for (i = 0; i < h->nr_cmds; i++) {
1155 h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
1156 h->chainsize, GFP_KERNEL);
1157 if (!h->cmd_sg_list[i])
1158 goto clean;
1159 }
1160 return 0;
1161
1162clean:
1163 hpsa_free_sg_chain_blocks(h);
1164 return -ENOMEM;
1165}
1166
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001167static int hpsa_map_sg_chain_block(struct ctlr_info *h,
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001168 struct CommandList *c)
1169{
1170 struct SGDescriptor *chain_sg, *chain_block;
1171 u64 temp64;
1172
1173 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
1174 chain_block = h->cmd_sg_list[c->cmdindex];
1175 chain_sg->Ext = HPSA_SG_CHAIN;
1176 chain_sg->Len = sizeof(*chain_sg) *
1177 (c->Header.SGTotal - h->max_cmd_sg_entries);
1178 temp64 = pci_map_single(h->pdev, chain_block, chain_sg->Len,
1179 PCI_DMA_TODEVICE);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001180 if (dma_mapping_error(&h->pdev->dev, temp64)) {
1181 /* prevent subsequent unmapping */
1182 chain_sg->Addr.lower = 0;
1183 chain_sg->Addr.upper = 0;
1184 return -1;
1185 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001186 chain_sg->Addr.lower = (u32) (temp64 & 0x0FFFFFFFFULL);
1187 chain_sg->Addr.upper = (u32) ((temp64 >> 32) & 0x0FFFFFFFFULL);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001188 return 0;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001189}
1190
1191static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
1192 struct CommandList *c)
1193{
1194 struct SGDescriptor *chain_sg;
1195 union u64bit temp64;
1196
1197 if (c->Header.SGTotal <= h->max_cmd_sg_entries)
1198 return;
1199
1200 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
1201 temp64.val32.lower = chain_sg->Addr.lower;
1202 temp64.val32.upper = chain_sg->Addr.upper;
1203 pci_unmap_single(h->pdev, temp64.val, chain_sg->Len, PCI_DMA_TODEVICE);
1204}
1205
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05001206static void complete_scsi_command(struct CommandList *cp)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001207{
1208 struct scsi_cmnd *cmd;
1209 struct ctlr_info *h;
1210 struct ErrorInfo *ei;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001211 struct hpsa_scsi_dev_t *dev;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001212
1213 unsigned char sense_key;
1214 unsigned char asc; /* additional sense code */
1215 unsigned char ascq; /* additional sense code qualifier */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05001216 unsigned long sense_data_size;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001217
1218 ei = cp->err_info;
1219 cmd = (struct scsi_cmnd *) cp->scsi_cmd;
1220 h = cp->h;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001221 dev = cmd->device->hostdata;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001222
1223 scsi_dma_unmap(cmd); /* undo the DMA mappings */
Matt Gatese1f7de02014-02-18 13:55:17 -06001224 if ((cp->cmd_type == CMD_SCSI) &&
1225 (cp->Header.SGTotal > h->max_cmd_sg_entries))
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001226 hpsa_unmap_sg_chain_block(h, cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001227
1228 cmd->result = (DID_OK << 16); /* host byte */
1229 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
Stephen M. Cameron55126722010-02-25 14:03:01 -06001230 cmd->result |= ei->ScsiStatus;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001231
1232 /* copy the sense data whether we need to or not. */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05001233 if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
1234 sense_data_size = SCSI_SENSE_BUFFERSIZE;
1235 else
1236 sense_data_size = sizeof(ei->SenseInfo);
1237 if (ei->SenseLen < sense_data_size)
1238 sense_data_size = ei->SenseLen;
1239
1240 memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001241 scsi_set_resid(cmd, ei->ResidualCnt);
1242
1243 if (ei->CommandStatus == 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001244 cmd_free(h, cp);
Tomas Henzl2cc5bfa2013-08-01 15:14:00 +02001245 cmd->scsi_done(cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001246 return;
1247 }
1248
Matt Gatese1f7de02014-02-18 13:55:17 -06001249 /* For I/O accelerator commands, copy over some fields to the normal
1250 * CISS header used below for error handling.
1251 */
1252 if (cp->cmd_type == CMD_IOACCEL1) {
1253 struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
1254 cp->Header.SGList = cp->Header.SGTotal = scsi_sg_count(cmd);
1255 cp->Request.CDBLen = c->io_flags & IOACCEL1_IOFLAGS_CDBLEN_MASK;
1256 cp->Header.Tag.lower = c->Tag.lower;
1257 cp->Header.Tag.upper = c->Tag.upper;
1258 memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
1259 memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001260
1261 /* Any RAID offload error results in retry which will use
1262 * the normal I/O path so the controller can handle whatever's
1263 * wrong.
1264 */
1265 if (is_logical_dev_addr_mode(dev->scsi3addr)) {
1266 if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
1267 dev->offload_enabled = 0;
1268 cmd->result = DID_SOFT_ERROR << 16;
1269 cmd_free(h, cp);
1270 cmd->scsi_done(cmd);
1271 return;
1272 }
Matt Gatese1f7de02014-02-18 13:55:17 -06001273 }
1274
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001275 /* an error has occurred */
1276 switch (ei->CommandStatus) {
1277
1278 case CMD_TARGET_STATUS:
1279 if (ei->ScsiStatus) {
1280 /* Get sense key */
1281 sense_key = 0xf & ei->SenseInfo[2];
1282 /* Get additional sense code */
1283 asc = ei->SenseInfo[12];
1284 /* Get addition sense code qualifier */
1285 ascq = ei->SenseInfo[13];
1286 }
1287
1288 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
Matt Gates3ce438d2013-12-04 17:10:36 -06001289 if (check_for_unit_attention(h, cp))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001290 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001291 if (sense_key == ILLEGAL_REQUEST) {
1292 /*
1293 * SCSI REPORT_LUNS is commonly unsupported on
1294 * Smart Array. Suppress noisy complaint.
1295 */
1296 if (cp->Request.CDB[0] == REPORT_LUNS)
1297 break;
1298
1299 /* If ASC/ASCQ indicate Logical Unit
1300 * Not Supported condition,
1301 */
1302 if ((asc == 0x25) && (ascq == 0x0)) {
1303 dev_warn(&h->pdev->dev, "cp %p "
1304 "has check condition\n", cp);
1305 break;
1306 }
1307 }
1308
1309 if (sense_key == NOT_READY) {
1310 /* If Sense is Not Ready, Logical Unit
1311 * Not ready, Manual Intervention
1312 * required
1313 */
1314 if ((asc == 0x04) && (ascq == 0x03)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001315 dev_warn(&h->pdev->dev, "cp %p "
1316 "has check condition: unit "
1317 "not ready, manual "
1318 "intervention required\n", cp);
1319 break;
1320 }
1321 }
Matt Gates1d3b3602010-02-04 08:43:00 -06001322 if (sense_key == ABORTED_COMMAND) {
1323 /* Aborted command is retryable */
1324 dev_warn(&h->pdev->dev, "cp %p "
1325 "has check condition: aborted command: "
1326 "ASC: 0x%x, ASCQ: 0x%x\n",
1327 cp, asc, ascq);
Stephen M. Cameron2e311fb2013-09-23 13:33:41 -05001328 cmd->result |= DID_SOFT_ERROR << 16;
Matt Gates1d3b3602010-02-04 08:43:00 -06001329 break;
1330 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001331 /* Must be some other type of check condition */
Stephen M. Cameron21b8e4e2012-05-01 11:42:25 -05001332 dev_dbg(&h->pdev->dev, "cp %p has check condition: "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001333 "unknown type: "
1334 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1335 "Returning result: 0x%x, "
1336 "cmd=[%02x %02x %02x %02x %02x "
Mike Miller807be732010-02-04 08:43:26 -06001337 "%02x %02x %02x %02x %02x %02x "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001338 "%02x %02x %02x %02x %02x]\n",
1339 cp, sense_key, asc, ascq,
1340 cmd->result,
1341 cmd->cmnd[0], cmd->cmnd[1],
1342 cmd->cmnd[2], cmd->cmnd[3],
1343 cmd->cmnd[4], cmd->cmnd[5],
1344 cmd->cmnd[6], cmd->cmnd[7],
Mike Miller807be732010-02-04 08:43:26 -06001345 cmd->cmnd[8], cmd->cmnd[9],
1346 cmd->cmnd[10], cmd->cmnd[11],
1347 cmd->cmnd[12], cmd->cmnd[13],
1348 cmd->cmnd[14], cmd->cmnd[15]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001349 break;
1350 }
1351
1352
1353 /* Problem was not a check condition
1354 * Pass it up to the upper layers...
1355 */
1356 if (ei->ScsiStatus) {
1357 dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
1358 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1359 "Returning result: 0x%x\n",
1360 cp, ei->ScsiStatus,
1361 sense_key, asc, ascq,
1362 cmd->result);
1363 } else { /* scsi status is zero??? How??? */
1364 dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
1365 "Returning no connection.\n", cp),
1366
1367 /* Ordinarily, this case should never happen,
1368 * but there is a bug in some released firmware
1369 * revisions that allows it to happen if, for
1370 * example, a 4100 backplane loses power and
1371 * the tape drive is in it. We assume that
1372 * it's a fatal error of some kind because we
1373 * can't show that it wasn't. We will make it
1374 * look like selection timeout since that is
1375 * the most common reason for this to occur,
1376 * and it's severe enough.
1377 */
1378
1379 cmd->result = DID_NO_CONNECT << 16;
1380 }
1381 break;
1382
1383 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1384 break;
1385 case CMD_DATA_OVERRUN:
1386 dev_warn(&h->pdev->dev, "cp %p has"
1387 " completed with data overrun "
1388 "reported\n", cp);
1389 break;
1390 case CMD_INVALID: {
1391 /* print_bytes(cp, sizeof(*cp), 1, 0);
1392 print_cmd(cp); */
1393 /* We get CMD_INVALID if you address a non-existent device
1394 * instead of a selection timeout (no response). You will
1395 * see this if you yank out a drive, then try to access it.
1396 * This is kind of a shame because it means that any other
1397 * CMD_INVALID (e.g. driver bug) will get interpreted as a
1398 * missing target. */
1399 cmd->result = DID_NO_CONNECT << 16;
1400 }
1401 break;
1402 case CMD_PROTOCOL_ERR:
Stephen M. Cameron256d0ea2012-09-14 16:34:25 -05001403 cmd->result = DID_ERROR << 16;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001404 dev_warn(&h->pdev->dev, "cp %p has "
Stephen M. Cameron256d0ea2012-09-14 16:34:25 -05001405 "protocol error\n", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001406 break;
1407 case CMD_HARDWARE_ERR:
1408 cmd->result = DID_ERROR << 16;
1409 dev_warn(&h->pdev->dev, "cp %p had hardware error\n", cp);
1410 break;
1411 case CMD_CONNECTION_LOST:
1412 cmd->result = DID_ERROR << 16;
1413 dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
1414 break;
1415 case CMD_ABORTED:
1416 cmd->result = DID_ABORT << 16;
1417 dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
1418 cp, ei->ScsiStatus);
1419 break;
1420 case CMD_ABORT_FAILED:
1421 cmd->result = DID_ERROR << 16;
1422 dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
1423 break;
1424 case CMD_UNSOLICITED_ABORT:
Stephen M. Cameronf6e76052011-07-26 11:08:52 -05001425 cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
1426 dev_warn(&h->pdev->dev, "cp %p aborted due to an unsolicited "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001427 "abort\n", cp);
1428 break;
1429 case CMD_TIMEOUT:
1430 cmd->result = DID_TIME_OUT << 16;
1431 dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
1432 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06001433 case CMD_UNABORTABLE:
1434 cmd->result = DID_ERROR << 16;
1435 dev_warn(&h->pdev->dev, "Command unabortable\n");
1436 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001437 case CMD_IOACCEL_DISABLED:
1438 /* This only handles the direct pass-through case since RAID
1439 * offload is handled above. Just attempt a retry.
1440 */
1441 cmd->result = DID_SOFT_ERROR << 16;
1442 dev_warn(&h->pdev->dev,
1443 "cp %p had HP SSD Smart Path error\n", cp);
1444 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001445 default:
1446 cmd->result = DID_ERROR << 16;
1447 dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
1448 cp, ei->CommandStatus);
1449 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001450 cmd_free(h, cp);
Tomas Henzl2cc5bfa2013-08-01 15:14:00 +02001451 cmd->scsi_done(cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001452}
1453
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001454static void hpsa_pci_unmap(struct pci_dev *pdev,
1455 struct CommandList *c, int sg_used, int data_direction)
1456{
1457 int i;
1458 union u64bit addr64;
1459
1460 for (i = 0; i < sg_used; i++) {
1461 addr64.val32.lower = c->SG[i].Addr.lower;
1462 addr64.val32.upper = c->SG[i].Addr.upper;
1463 pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len,
1464 data_direction);
1465 }
1466}
1467
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001468static int hpsa_map_one(struct pci_dev *pdev,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001469 struct CommandList *cp,
1470 unsigned char *buf,
1471 size_t buflen,
1472 int data_direction)
1473{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001474 u64 addr64;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001475
1476 if (buflen == 0 || data_direction == PCI_DMA_NONE) {
1477 cp->Header.SGList = 0;
1478 cp->Header.SGTotal = 0;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001479 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001480 }
1481
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001482 addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction);
Shuah Khaneceaae12013-02-20 11:24:34 -06001483 if (dma_mapping_error(&pdev->dev, addr64)) {
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001484 /* Prevent subsequent unmap of something never mapped */
Shuah Khaneceaae12013-02-20 11:24:34 -06001485 cp->Header.SGList = 0;
1486 cp->Header.SGTotal = 0;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001487 return -1;
Shuah Khaneceaae12013-02-20 11:24:34 -06001488 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001489 cp->SG[0].Addr.lower =
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001490 (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001491 cp->SG[0].Addr.upper =
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001492 (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001493 cp->SG[0].Len = buflen;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06001494 cp->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining */
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001495 cp->Header.SGList = (u8) 1; /* no. SGs contig in this cmd */
1496 cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001497 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001498}
1499
1500static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
1501 struct CommandList *c)
1502{
1503 DECLARE_COMPLETION_ONSTACK(wait);
1504
1505 c->waiting = &wait;
1506 enqueue_cmd_and_start_io(h, c);
1507 wait_for_completion(&wait);
1508}
1509
Stephen M. Camerona0c12412011-10-26 16:22:04 -05001510static void hpsa_scsi_do_simple_cmd_core_if_no_lockup(struct ctlr_info *h,
1511 struct CommandList *c)
1512{
1513 unsigned long flags;
1514
1515 /* If controller lockup detected, fake a hardware error. */
1516 spin_lock_irqsave(&h->lock, flags);
1517 if (unlikely(h->lockup_detected)) {
1518 spin_unlock_irqrestore(&h->lock, flags);
1519 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
1520 } else {
1521 spin_unlock_irqrestore(&h->lock, flags);
1522 hpsa_scsi_do_simple_cmd_core(h, c);
1523 }
1524}
1525
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001526#define MAX_DRIVER_CMD_RETRIES 25
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001527static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
1528 struct CommandList *c, int data_direction)
1529{
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001530 int backoff_time = 10, retry_count = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001531
1532 do {
Joe Perches7630abd2011-05-08 23:32:40 -07001533 memset(c->err_info, 0, sizeof(*c->err_info));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001534 hpsa_scsi_do_simple_cmd_core(h, c);
1535 retry_count++;
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001536 if (retry_count > 3) {
1537 msleep(backoff_time);
1538 if (backoff_time < 1000)
1539 backoff_time *= 2;
1540 }
Matt Bondurant852af202012-05-01 11:42:35 -05001541 } while ((check_for_unit_attention(h, c) ||
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001542 check_for_busy(h, c)) &&
1543 retry_count <= MAX_DRIVER_CMD_RETRIES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001544 hpsa_pci_unmap(h->pdev, c, 1, data_direction);
1545}
1546
1547static void hpsa_scsi_interpret_error(struct CommandList *cp)
1548{
1549 struct ErrorInfo *ei;
1550 struct device *d = &cp->h->pdev->dev;
1551
1552 ei = cp->err_info;
1553 switch (ei->CommandStatus) {
1554 case CMD_TARGET_STATUS:
1555 dev_warn(d, "cmd %p has completed with errors\n", cp);
1556 dev_warn(d, "cmd %p has SCSI Status = %x\n", cp,
1557 ei->ScsiStatus);
1558 if (ei->ScsiStatus == 0)
1559 dev_warn(d, "SCSI status is abnormally zero. "
1560 "(probably indicates selection timeout "
1561 "reported incorrectly due to a known "
1562 "firmware bug, circa July, 2001.)\n");
1563 break;
1564 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1565 dev_info(d, "UNDERRUN\n");
1566 break;
1567 case CMD_DATA_OVERRUN:
1568 dev_warn(d, "cp %p has completed with data overrun\n", cp);
1569 break;
1570 case CMD_INVALID: {
1571 /* controller unfortunately reports SCSI passthru's
1572 * to non-existent targets as invalid commands.
1573 */
1574 dev_warn(d, "cp %p is reported invalid (probably means "
1575 "target device no longer present)\n", cp);
1576 /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
1577 print_cmd(cp); */
1578 }
1579 break;
1580 case CMD_PROTOCOL_ERR:
1581 dev_warn(d, "cp %p has protocol error \n", cp);
1582 break;
1583 case CMD_HARDWARE_ERR:
1584 /* cmd->result = DID_ERROR << 16; */
1585 dev_warn(d, "cp %p had hardware error\n", cp);
1586 break;
1587 case CMD_CONNECTION_LOST:
1588 dev_warn(d, "cp %p had connection lost\n", cp);
1589 break;
1590 case CMD_ABORTED:
1591 dev_warn(d, "cp %p was aborted\n", cp);
1592 break;
1593 case CMD_ABORT_FAILED:
1594 dev_warn(d, "cp %p reports abort failed\n", cp);
1595 break;
1596 case CMD_UNSOLICITED_ABORT:
1597 dev_warn(d, "cp %p aborted due to an unsolicited abort\n", cp);
1598 break;
1599 case CMD_TIMEOUT:
1600 dev_warn(d, "cp %p timed out\n", cp);
1601 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06001602 case CMD_UNABORTABLE:
1603 dev_warn(d, "Command unabortable\n");
1604 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001605 default:
1606 dev_warn(d, "cp %p returned unknown status %x\n", cp,
1607 ei->CommandStatus);
1608 }
1609}
1610
1611static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
1612 unsigned char page, unsigned char *buf,
1613 unsigned char bufsize)
1614{
1615 int rc = IO_OK;
1616 struct CommandList *c;
1617 struct ErrorInfo *ei;
1618
1619 c = cmd_special_alloc(h);
1620
1621 if (c == NULL) { /* trouble... */
1622 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06001623 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001624 }
1625
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001626 if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
1627 page, scsi3addr, TYPE_CMD)) {
1628 rc = -1;
1629 goto out;
1630 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001631 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1632 ei = c->err_info;
1633 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
1634 hpsa_scsi_interpret_error(c);
1635 rc = -1;
1636 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001637out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001638 cmd_special_free(h, c);
1639 return rc;
1640}
1641
1642static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr)
1643{
1644 int rc = IO_OK;
1645 struct CommandList *c;
1646 struct ErrorInfo *ei;
1647
1648 c = cmd_special_alloc(h);
1649
1650 if (c == NULL) { /* trouble... */
1651 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
Stephen M. Camerone9ea04a2010-02-25 14:03:06 -06001652 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001653 }
1654
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001655 /* fill_cmd can't fail here, no data buffer to map. */
1656 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h,
1657 NULL, 0, 0, scsi3addr, TYPE_MSG);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001658 hpsa_scsi_do_simple_cmd_core(h, c);
1659 /* no unmap needed here because no data xfer. */
1660
1661 ei = c->err_info;
1662 if (ei->CommandStatus != 0) {
1663 hpsa_scsi_interpret_error(c);
1664 rc = -1;
1665 }
1666 cmd_special_free(h, c);
1667 return rc;
1668}
1669
1670static void hpsa_get_raid_level(struct ctlr_info *h,
1671 unsigned char *scsi3addr, unsigned char *raid_level)
1672{
1673 int rc;
1674 unsigned char *buf;
1675
1676 *raid_level = RAID_UNKNOWN;
1677 buf = kzalloc(64, GFP_KERNEL);
1678 if (!buf)
1679 return;
1680 rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0xC1, buf, 64);
1681 if (rc == 0)
1682 *raid_level = buf[8];
1683 if (*raid_level > RAID_UNKNOWN)
1684 *raid_level = RAID_UNKNOWN;
1685 kfree(buf);
1686 return;
1687}
1688
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001689#define HPSA_MAP_DEBUG
1690#ifdef HPSA_MAP_DEBUG
1691static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
1692 struct raid_map_data *map_buff)
1693{
1694 struct raid_map_disk_data *dd = &map_buff->data[0];
1695 int map, row, col;
1696 u16 map_cnt, row_cnt, disks_per_row;
1697
1698 if (rc != 0)
1699 return;
1700
1701 dev_info(&h->pdev->dev, "structure_size = %u\n",
1702 le32_to_cpu(map_buff->structure_size));
1703 dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
1704 le32_to_cpu(map_buff->volume_blk_size));
1705 dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
1706 le64_to_cpu(map_buff->volume_blk_cnt));
1707 dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
1708 map_buff->phys_blk_shift);
1709 dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
1710 map_buff->parity_rotation_shift);
1711 dev_info(&h->pdev->dev, "strip_size = %u\n",
1712 le16_to_cpu(map_buff->strip_size));
1713 dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
1714 le64_to_cpu(map_buff->disk_starting_blk));
1715 dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
1716 le64_to_cpu(map_buff->disk_blk_cnt));
1717 dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
1718 le16_to_cpu(map_buff->data_disks_per_row));
1719 dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
1720 le16_to_cpu(map_buff->metadata_disks_per_row));
1721 dev_info(&h->pdev->dev, "row_cnt = %u\n",
1722 le16_to_cpu(map_buff->row_cnt));
1723 dev_info(&h->pdev->dev, "layout_map_count = %u\n",
1724 le16_to_cpu(map_buff->layout_map_count));
1725
1726 map_cnt = le16_to_cpu(map_buff->layout_map_count);
1727 for (map = 0; map < map_cnt; map++) {
1728 dev_info(&h->pdev->dev, "Map%u:\n", map);
1729 row_cnt = le16_to_cpu(map_buff->row_cnt);
1730 for (row = 0; row < row_cnt; row++) {
1731 dev_info(&h->pdev->dev, " Row%u:\n", row);
1732 disks_per_row =
1733 le16_to_cpu(map_buff->data_disks_per_row);
1734 for (col = 0; col < disks_per_row; col++, dd++)
1735 dev_info(&h->pdev->dev,
1736 " D%02u: h=0x%04x xor=%u,%u\n",
1737 col, dd->ioaccel_handle,
1738 dd->xor_mult[0], dd->xor_mult[1]);
1739 disks_per_row =
1740 le16_to_cpu(map_buff->metadata_disks_per_row);
1741 for (col = 0; col < disks_per_row; col++, dd++)
1742 dev_info(&h->pdev->dev,
1743 " M%02u: h=0x%04x xor=%u,%u\n",
1744 col, dd->ioaccel_handle,
1745 dd->xor_mult[0], dd->xor_mult[1]);
1746 }
1747 }
1748}
1749#else
1750static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
1751 __attribute__((unused)) int rc,
1752 __attribute__((unused)) struct raid_map_data *map_buff)
1753{
1754}
1755#endif
1756
1757static int hpsa_get_raid_map(struct ctlr_info *h,
1758 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
1759{
1760 int rc = 0;
1761 struct CommandList *c;
1762 struct ErrorInfo *ei;
1763
1764 c = cmd_special_alloc(h);
1765 if (c == NULL) {
1766 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1767 return -ENOMEM;
1768 }
1769 if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
1770 sizeof(this_device->raid_map), 0,
1771 scsi3addr, TYPE_CMD)) {
1772 dev_warn(&h->pdev->dev, "Out of memory in hpsa_get_raid_map()\n");
1773 cmd_special_free(h, c);
1774 return -ENOMEM;
1775 }
1776 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1777 ei = c->err_info;
1778 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
1779 hpsa_scsi_interpret_error(c);
1780 cmd_special_free(h, c);
1781 return -1;
1782 }
1783 cmd_special_free(h, c);
1784
1785 /* @todo in the future, dynamically allocate RAID map memory */
1786 if (le32_to_cpu(this_device->raid_map.structure_size) >
1787 sizeof(this_device->raid_map)) {
1788 dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
1789 rc = -1;
1790 }
1791 hpsa_debug_map_buff(h, rc, &this_device->raid_map);
1792 return rc;
1793}
1794
1795static void hpsa_get_ioaccel_status(struct ctlr_info *h,
1796 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
1797{
1798 int rc;
1799 unsigned char *buf;
1800 u8 ioaccel_status;
1801
1802 this_device->offload_config = 0;
1803 this_device->offload_enabled = 0;
1804
1805 buf = kzalloc(64, GFP_KERNEL);
1806 if (!buf)
1807 return;
1808 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
1809 HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
1810 if (rc != 0)
1811 goto out;
1812
1813#define IOACCEL_STATUS_BYTE 4
1814#define OFFLOAD_CONFIGURED_BIT 0x01
1815#define OFFLOAD_ENABLED_BIT 0x02
1816 ioaccel_status = buf[IOACCEL_STATUS_BYTE];
1817 this_device->offload_config =
1818 !!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
1819 if (this_device->offload_config) {
1820 this_device->offload_enabled =
1821 !!(ioaccel_status & OFFLOAD_ENABLED_BIT);
1822 if (hpsa_get_raid_map(h, scsi3addr, this_device))
1823 this_device->offload_enabled = 0;
1824 }
1825out:
1826 kfree(buf);
1827 return;
1828}
1829
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001830/* Get the device id from inquiry page 0x83 */
1831static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
1832 unsigned char *device_id, int buflen)
1833{
1834 int rc;
1835 unsigned char *buf;
1836
1837 if (buflen > 16)
1838 buflen = 16;
1839 buf = kzalloc(64, GFP_KERNEL);
1840 if (!buf)
1841 return -1;
1842 rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0x83, buf, 64);
1843 if (rc == 0)
1844 memcpy(device_id, &buf[8], buflen);
1845 kfree(buf);
1846 return rc != 0;
1847}
1848
1849static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
1850 struct ReportLUNdata *buf, int bufsize,
1851 int extended_response)
1852{
1853 int rc = IO_OK;
1854 struct CommandList *c;
1855 unsigned char scsi3addr[8];
1856 struct ErrorInfo *ei;
1857
1858 c = cmd_special_alloc(h);
1859 if (c == NULL) { /* trouble... */
1860 dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
1861 return -1;
1862 }
Stephen M. Camerone89c0ae2010-02-04 08:42:04 -06001863 /* address the controller */
1864 memset(scsi3addr, 0, sizeof(scsi3addr));
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001865 if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
1866 buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
1867 rc = -1;
1868 goto out;
1869 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001870 if (extended_response)
1871 c->Request.CDB[1] = extended_response;
1872 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1873 ei = c->err_info;
1874 if (ei->CommandStatus != 0 &&
1875 ei->CommandStatus != CMD_DATA_UNDERRUN) {
1876 hpsa_scsi_interpret_error(c);
1877 rc = -1;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001878 } else {
1879 if (buf->extended_response_flag != extended_response) {
1880 dev_err(&h->pdev->dev,
1881 "report luns requested format %u, got %u\n",
1882 extended_response,
1883 buf->extended_response_flag);
1884 rc = -1;
1885 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001886 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001887out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001888 cmd_special_free(h, c);
1889 return rc;
1890}
1891
1892static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
1893 struct ReportLUNdata *buf,
1894 int bufsize, int extended_response)
1895{
1896 return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
1897}
1898
1899static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
1900 struct ReportLUNdata *buf, int bufsize)
1901{
1902 return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
1903}
1904
1905static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
1906 int bus, int target, int lun)
1907{
1908 device->bus = bus;
1909 device->target = target;
1910 device->lun = lun;
1911}
1912
1913static int hpsa_update_device_info(struct ctlr_info *h,
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05001914 unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
1915 unsigned char *is_OBDR_device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001916{
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05001917
1918#define OBDR_SIG_OFFSET 43
1919#define OBDR_TAPE_SIG "$DR-10"
1920#define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
1921#define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
1922
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06001923 unsigned char *inq_buff;
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05001924 unsigned char *obdr_sig;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001925
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06001926 inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001927 if (!inq_buff)
1928 goto bail_out;
1929
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001930 /* Do an inquiry to the device to see what it is. */
1931 if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
1932 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
1933 /* Inquiry failed (msg printed already) */
1934 dev_err(&h->pdev->dev,
1935 "hpsa_update_device_info: inquiry failed\n");
1936 goto bail_out;
1937 }
1938
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001939 this_device->devtype = (inq_buff[0] & 0x1f);
1940 memcpy(this_device->scsi3addr, scsi3addr, 8);
1941 memcpy(this_device->vendor, &inq_buff[8],
1942 sizeof(this_device->vendor));
1943 memcpy(this_device->model, &inq_buff[16],
1944 sizeof(this_device->model));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001945 memset(this_device->device_id, 0,
1946 sizeof(this_device->device_id));
1947 hpsa_get_device_id(h, scsi3addr, this_device->device_id,
1948 sizeof(this_device->device_id));
1949
1950 if (this_device->devtype == TYPE_DISK &&
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001951 is_logical_dev_addr_mode(scsi3addr)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001952 hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001953 if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
1954 hpsa_get_ioaccel_status(h, scsi3addr, this_device);
1955 } else {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001956 this_device->raid_level = RAID_UNKNOWN;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001957 this_device->offload_config = 0;
1958 this_device->offload_enabled = 0;
1959 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001960
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05001961 if (is_OBDR_device) {
1962 /* See if this is a One-Button-Disaster-Recovery device
1963 * by looking for "$DR-10" at offset 43 in inquiry data.
1964 */
1965 obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
1966 *is_OBDR_device = (this_device->devtype == TYPE_ROM &&
1967 strncmp(obdr_sig, OBDR_TAPE_SIG,
1968 OBDR_SIG_LEN) == 0);
1969 }
1970
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001971 kfree(inq_buff);
1972 return 0;
1973
1974bail_out:
1975 kfree(inq_buff);
1976 return 1;
1977}
1978
Scott Teel4f4eb9f2012-01-19 14:01:25 -06001979static unsigned char *ext_target_model[] = {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001980 "MSA2012",
1981 "MSA2024",
1982 "MSA2312",
1983 "MSA2324",
Stephen M. Cameronfda38512011-05-03 15:00:07 -05001984 "P2000 G3 SAS",
Stephen M. Camerone06c8e52013-09-23 13:33:56 -05001985 "MSA 2040 SAS",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001986 NULL,
1987};
1988
Scott Teel4f4eb9f2012-01-19 14:01:25 -06001989static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001990{
1991 int i;
1992
Scott Teel4f4eb9f2012-01-19 14:01:25 -06001993 for (i = 0; ext_target_model[i]; i++)
1994 if (strncmp(device->model, ext_target_model[i],
1995 strlen(ext_target_model[i])) == 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001996 return 1;
1997 return 0;
1998}
1999
2000/* Helper function to assign bus, target, lun mapping of devices.
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002001 * Puts non-external target logical volumes on bus 0, external target logical
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002002 * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
2003 * Logical drive target and lun are assigned at this time, but
2004 * physical device lun and target assignment are deferred (assigned
2005 * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
2006 */
2007static void figure_bus_target_lun(struct ctlr_info *h,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002008 u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002009{
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002010 u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002011
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002012 if (!is_logical_dev_addr_mode(lunaddrbytes)) {
2013 /* physical device, target and lun filled in later */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002014 if (is_hba_lunid(lunaddrbytes))
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002015 hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002016 else
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002017 /* defer target, lun assignment for physical devices */
2018 hpsa_set_bus_target_lun(device, 2, -1, -1);
2019 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002020 }
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002021 /* It's a logical device */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002022 if (is_ext_target(h, device)) {
2023 /* external target way, put logicals on bus 1
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002024 * and match target/lun numbers box
2025 * reports, other smart array, bus 0, target 0, match lunid
2026 */
2027 hpsa_set_bus_target_lun(device,
2028 1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
2029 return;
2030 }
2031 hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002032}
2033
2034/*
2035 * If there is no lun 0 on a target, linux won't find any devices.
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002036 * For the external targets (arrays), we have to manually detect the enclosure
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002037 * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
2038 * it for some reason. *tmpdevice is the target we're adding,
2039 * this_device is a pointer into the current element of currentsd[]
2040 * that we're building up in update_scsi_devices(), below.
2041 * lunzerobits is a bitmap that tracks which targets already have a
2042 * lun 0 assigned.
2043 * Returns 1 if an enclosure was added, 0 if not.
2044 */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002045static int add_ext_target_dev(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002046 struct hpsa_scsi_dev_t *tmpdevice,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002047 struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002048 unsigned long lunzerobits[], int *n_ext_target_devs)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002049{
2050 unsigned char scsi3addr[8];
2051
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002052 if (test_bit(tmpdevice->target, lunzerobits))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002053 return 0; /* There is already a lun 0 on this target. */
2054
2055 if (!is_logical_dev_addr_mode(lunaddrbytes))
2056 return 0; /* It's the logical targets that may lack lun 0. */
2057
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002058 if (!is_ext_target(h, tmpdevice))
2059 return 0; /* Only external target devices have this problem. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002060
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002061 if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002062 return 0;
2063
Stephen M. Cameronc4f8a292011-01-07 10:55:43 -06002064 memset(scsi3addr, 0, 8);
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002065 scsi3addr[3] = tmpdevice->target;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002066 if (is_hba_lunid(scsi3addr))
2067 return 0; /* Don't add the RAID controller here. */
2068
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002069 if (is_scsi_rev_5(h))
2070 return 0; /* p1210m doesn't need to do this. */
2071
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002072 if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
Scott Teelaca4a522012-01-19 14:01:19 -06002073 dev_warn(&h->pdev->dev, "Maximum number of external "
2074 "target devices exceeded. Check your hardware "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002075 "configuration.");
2076 return 0;
2077 }
2078
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002079 if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002080 return 0;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002081 (*n_ext_target_devs)++;
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002082 hpsa_set_bus_target_lun(this_device,
2083 tmpdevice->bus, tmpdevice->target, 0);
2084 set_bit(tmpdevice->target, lunzerobits);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002085 return 1;
2086}
2087
2088/*
2089 * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
2090 * logdev. The number of luns in physdev and logdev are returned in
2091 * *nphysicals and *nlogicals, respectively.
2092 * Returns 0 on success, -1 otherwise.
2093 */
2094static int hpsa_gather_lun_info(struct ctlr_info *h,
2095 int reportlunsize,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002096 struct ReportLUNdata *physdev, u32 *nphysicals, int *physical_mode,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002097 struct ReportLUNdata *logdev, u32 *nlogicals)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002098{
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002099 int physical_entry_size = 8;
2100
2101 *physical_mode = 0;
2102
2103 /* For I/O accelerator mode we need to read physical device handles */
2104 if (h->transMethod & CFGTBL_Trans_io_accel1) {
2105 *physical_mode = HPSA_REPORT_PHYS_EXTENDED;
2106 physical_entry_size = 24;
2107 }
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002108 if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002109 *physical_mode)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002110 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
2111 return -1;
2112 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002113 *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) /
2114 physical_entry_size;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002115 if (*nphysicals > HPSA_MAX_PHYS_LUN) {
2116 dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
2117 " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2118 *nphysicals - HPSA_MAX_PHYS_LUN);
2119 *nphysicals = HPSA_MAX_PHYS_LUN;
2120 }
2121 if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
2122 dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
2123 return -1;
2124 }
Stephen M. Cameron6df1e952010-02-04 08:42:19 -06002125 *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002126 /* Reject Logicals in excess of our max capability. */
2127 if (*nlogicals > HPSA_MAX_LUN) {
2128 dev_warn(&h->pdev->dev,
2129 "maximum logical LUNs (%d) exceeded. "
2130 "%d LUNs ignored.\n", HPSA_MAX_LUN,
2131 *nlogicals - HPSA_MAX_LUN);
2132 *nlogicals = HPSA_MAX_LUN;
2133 }
2134 if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
2135 dev_warn(&h->pdev->dev,
2136 "maximum logical + physical LUNs (%d) exceeded. "
2137 "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2138 *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
2139 *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
2140 }
2141 return 0;
2142}
2143
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002144u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i,
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002145 int nphysicals, int nlogicals,
2146 struct ReportExtendedLUNdata *physdev_list,
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002147 struct ReportLUNdata *logdev_list)
2148{
2149 /* Helper function, figure out where the LUN ID info is coming from
2150 * given index i, lists of physical and logical devices, where in
2151 * the list the raid controller is supposed to appear (first or last)
2152 */
2153
2154 int logicals_start = nphysicals + (raid_ctlr_position == 0);
2155 int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
2156
2157 if (i == raid_ctlr_position)
2158 return RAID_CTLR_LUNID;
2159
2160 if (i < logicals_start)
2161 return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0];
2162
2163 if (i < last_device)
2164 return &logdev_list->LUN[i - nphysicals -
2165 (raid_ctlr_position == 0)][0];
2166 BUG();
2167 return NULL;
2168}
2169
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002170static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
2171{
2172 /* the idea here is we could get notified
2173 * that some devices have changed, so we do a report
2174 * physical luns and report logical luns cmd, and adjust
2175 * our list of devices accordingly.
2176 *
2177 * The scsi3addr's of devices won't change so long as the
2178 * adapter is not reset. That means we can rescan and
2179 * tell which devices we already know about, vs. new
2180 * devices, vs. disappearing devices.
2181 */
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002182 struct ReportExtendedLUNdata *physdev_list = NULL;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002183 struct ReportLUNdata *logdev_list = NULL;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002184 u32 nphysicals = 0;
2185 u32 nlogicals = 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002186 int physical_mode = 0;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002187 u32 ndev_allocated = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002188 struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
2189 int ncurrent = 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002190 int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 24;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002191 int i, n_ext_target_devs, ndevs_to_allocate;
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002192 int raid_ctlr_position;
Scott Teelaca4a522012-01-19 14:01:19 -06002193 DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002194
Scott Teelcfe5bad2011-10-26 16:21:07 -05002195 currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002196 physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
2197 logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002198 tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
2199
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002200 if (!currentsd || !physdev_list || !logdev_list || !tmpdevice) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002201 dev_err(&h->pdev->dev, "out of memory\n");
2202 goto out;
2203 }
2204 memset(lunzerobits, 0, sizeof(lunzerobits));
2205
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002206 if (hpsa_gather_lun_info(h, reportlunsize,
2207 (struct ReportLUNdata *) physdev_list, &nphysicals,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002208 &physical_mode, logdev_list, &nlogicals))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002209 goto out;
2210
Scott Teelaca4a522012-01-19 14:01:19 -06002211 /* We might see up to the maximum number of logical and physical disks
2212 * plus external target devices, and a device for the local RAID
2213 * controller.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002214 */
Scott Teelaca4a522012-01-19 14:01:19 -06002215 ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002216
2217 /* Allocate the per device structures */
2218 for (i = 0; i < ndevs_to_allocate; i++) {
Scott Teelb7ec0212011-10-26 16:21:12 -05002219 if (i >= HPSA_MAX_DEVICES) {
2220 dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
2221 " %d devices ignored.\n", HPSA_MAX_DEVICES,
2222 ndevs_to_allocate - HPSA_MAX_DEVICES);
2223 break;
2224 }
2225
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002226 currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
2227 if (!currentsd[i]) {
2228 dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
2229 __FILE__, __LINE__);
2230 goto out;
2231 }
2232 ndev_allocated++;
2233 }
2234
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002235 if (unlikely(is_scsi_rev_5(h)))
2236 raid_ctlr_position = 0;
2237 else
2238 raid_ctlr_position = nphysicals + nlogicals;
2239
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002240 /* adjust our table of devices */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002241 n_ext_target_devs = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002242 for (i = 0; i < nphysicals + nlogicals + 1; i++) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002243 u8 *lunaddrbytes, is_OBDR = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002244
2245 /* Figure out where the LUN ID info is coming from */
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002246 lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
2247 i, nphysicals, nlogicals, physdev_list, logdev_list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002248 /* skip masked physical devices. */
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002249 if (lunaddrbytes[3] & 0xC0 &&
2250 i < nphysicals + (raid_ctlr_position == 0))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002251 continue;
2252
2253 /* Get device type, vendor, model, device id */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002254 if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
2255 &is_OBDR))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002256 continue; /* skip it if we can't talk to it. */
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002257 figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002258 this_device = currentsd[ncurrent];
2259
2260 /*
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002261 * For external target devices, we have to insert a LUN 0 which
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002262 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
2263 * is nonetheless an enclosure device there. We have to
2264 * present that otherwise linux won't find anything if
2265 * there is no lun 0.
2266 */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002267 if (add_ext_target_dev(h, tmpdevice, this_device,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002268 lunaddrbytes, lunzerobits,
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002269 &n_ext_target_devs)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002270 ncurrent++;
2271 this_device = currentsd[ncurrent];
2272 }
2273
2274 *this_device = *tmpdevice;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002275
2276 switch (this_device->devtype) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002277 case TYPE_ROM:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002278 /* We don't *really* support actual CD-ROM devices,
2279 * just "One Button Disaster Recovery" tape drive
2280 * which temporarily pretends to be a CD-ROM drive.
2281 * So we check that the device is really an OBDR tape
2282 * device by checking for "$DR-10" in bytes 43-48 of
2283 * the inquiry data.
2284 */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002285 if (is_OBDR)
2286 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002287 break;
2288 case TYPE_DISK:
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002289 if (i >= nphysicals) {
2290 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002291 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002292 }
2293 if (physical_mode == HPSA_REPORT_PHYS_EXTENDED) {
2294 memcpy(&this_device->ioaccel_handle,
2295 &lunaddrbytes[20],
2296 sizeof(this_device->ioaccel_handle));
2297 ncurrent++;
2298 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002299 break;
2300 case TYPE_TAPE:
2301 case TYPE_MEDIUM_CHANGER:
2302 ncurrent++;
2303 break;
2304 case TYPE_RAID:
2305 /* Only present the Smartarray HBA as a RAID controller.
2306 * If it's a RAID controller other than the HBA itself
2307 * (an external RAID controller, MSA500 or similar)
2308 * don't present it.
2309 */
2310 if (!is_hba_lunid(lunaddrbytes))
2311 break;
2312 ncurrent++;
2313 break;
2314 default:
2315 break;
2316 }
Scott Teelcfe5bad2011-10-26 16:21:07 -05002317 if (ncurrent >= HPSA_MAX_DEVICES)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002318 break;
2319 }
2320 adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
2321out:
2322 kfree(tmpdevice);
2323 for (i = 0; i < ndev_allocated; i++)
2324 kfree(currentsd[i]);
2325 kfree(currentsd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002326 kfree(physdev_list);
2327 kfree(logdev_list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002328}
2329
2330/* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
2331 * dma mapping and fills in the scatter gather entries of the
2332 * hpsa command, cp.
2333 */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002334static int hpsa_scatter_gather(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002335 struct CommandList *cp,
2336 struct scsi_cmnd *cmd)
2337{
2338 unsigned int len;
2339 struct scatterlist *sg;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002340 u64 addr64;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002341 int use_sg, i, sg_index, chained;
2342 struct SGDescriptor *curr_sg;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002343
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002344 BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002345
2346 use_sg = scsi_dma_map(cmd);
2347 if (use_sg < 0)
2348 return use_sg;
2349
2350 if (!use_sg)
2351 goto sglist_finished;
2352
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002353 curr_sg = cp->SG;
2354 chained = 0;
2355 sg_index = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002356 scsi_for_each_sg(cmd, sg, use_sg, i) {
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002357 if (i == h->max_cmd_sg_entries - 1 &&
2358 use_sg > h->max_cmd_sg_entries) {
2359 chained = 1;
2360 curr_sg = h->cmd_sg_list[cp->cmdindex];
2361 sg_index = 0;
2362 }
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002363 addr64 = (u64) sg_dma_address(sg);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002364 len = sg_dma_len(sg);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002365 curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
2366 curr_sg->Addr.upper = (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
2367 curr_sg->Len = len;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06002368 curr_sg->Ext = (i < scsi_sg_count(cmd) - 1) ? 0 : HPSA_SG_LAST;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002369 curr_sg++;
2370 }
2371
2372 if (use_sg + chained > h->maxSG)
2373 h->maxSG = use_sg + chained;
2374
2375 if (chained) {
2376 cp->Header.SGList = h->max_cmd_sg_entries;
2377 cp->Header.SGTotal = (u16) (use_sg + 1);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002378 if (hpsa_map_sg_chain_block(h, cp)) {
2379 scsi_dma_unmap(cmd);
2380 return -1;
2381 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002382 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002383 }
2384
2385sglist_finished:
2386
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002387 cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
2388 cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002389 return 0;
2390}
2391
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002392#define IO_ACCEL_INELIGIBLE (1)
2393static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
2394{
2395 int is_write = 0;
2396 u32 block;
2397 u32 block_cnt;
2398
2399 /* Perform some CDB fixups if needed using 10 byte reads/writes only */
2400 switch (cdb[0]) {
2401 case WRITE_6:
2402 case WRITE_12:
2403 is_write = 1;
2404 case READ_6:
2405 case READ_12:
2406 if (*cdb_len == 6) {
2407 block = (((u32) cdb[2]) << 8) | cdb[3];
2408 block_cnt = cdb[4];
2409 } else {
2410 BUG_ON(*cdb_len != 12);
2411 block = (((u32) cdb[2]) << 24) |
2412 (((u32) cdb[3]) << 16) |
2413 (((u32) cdb[4]) << 8) |
2414 cdb[5];
2415 block_cnt =
2416 (((u32) cdb[6]) << 24) |
2417 (((u32) cdb[7]) << 16) |
2418 (((u32) cdb[8]) << 8) |
2419 cdb[9];
2420 }
2421 if (block_cnt > 0xffff)
2422 return IO_ACCEL_INELIGIBLE;
2423
2424 cdb[0] = is_write ? WRITE_10 : READ_10;
2425 cdb[1] = 0;
2426 cdb[2] = (u8) (block >> 24);
2427 cdb[3] = (u8) (block >> 16);
2428 cdb[4] = (u8) (block >> 8);
2429 cdb[5] = (u8) (block);
2430 cdb[6] = 0;
2431 cdb[7] = (u8) (block_cnt >> 8);
2432 cdb[8] = (u8) (block_cnt);
2433 cdb[9] = 0;
2434 *cdb_len = 10;
2435 break;
2436 }
2437 return 0;
2438}
2439
Matt Gatese1f7de02014-02-18 13:55:17 -06002440/*
2441 * Queue a command to the I/O accelerator path.
Matt Gatese1f7de02014-02-18 13:55:17 -06002442 */
2443static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002444 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
2445 u8 *scsi3addr)
Matt Gatese1f7de02014-02-18 13:55:17 -06002446{
2447 struct scsi_cmnd *cmd = c->scsi_cmd;
Matt Gatese1f7de02014-02-18 13:55:17 -06002448 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
2449 unsigned int len;
2450 unsigned int total_len = 0;
2451 struct scatterlist *sg;
2452 u64 addr64;
2453 int use_sg, i;
2454 struct SGDescriptor *curr_sg;
2455 u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
2456
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002457 /* TODO: implement chaining support */
2458 if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
2459 return IO_ACCEL_INELIGIBLE;
2460
Matt Gatese1f7de02014-02-18 13:55:17 -06002461 BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
2462
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002463 if (fixup_ioaccel_cdb(cdb, &cdb_len))
2464 return IO_ACCEL_INELIGIBLE;
2465
Matt Gatese1f7de02014-02-18 13:55:17 -06002466 c->cmd_type = CMD_IOACCEL1;
2467
2468 /* Adjust the DMA address to point to the accelerated command buffer */
2469 c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
2470 (c->cmdindex * sizeof(*cp));
2471 BUG_ON(c->busaddr & 0x0000007F);
2472
2473 use_sg = scsi_dma_map(cmd);
2474 if (use_sg < 0)
2475 return use_sg;
2476
2477 if (use_sg) {
2478 curr_sg = cp->SG;
2479 scsi_for_each_sg(cmd, sg, use_sg, i) {
2480 addr64 = (u64) sg_dma_address(sg);
2481 len = sg_dma_len(sg);
2482 total_len += len;
2483 curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
2484 curr_sg->Addr.upper =
2485 (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
2486 curr_sg->Len = len;
2487
2488 if (i == (scsi_sg_count(cmd) - 1))
2489 curr_sg->Ext = HPSA_SG_LAST;
2490 else
2491 curr_sg->Ext = 0; /* we are not chaining */
2492 curr_sg++;
2493 }
2494
2495 switch (cmd->sc_data_direction) {
2496 case DMA_TO_DEVICE:
2497 control |= IOACCEL1_CONTROL_DATA_OUT;
2498 break;
2499 case DMA_FROM_DEVICE:
2500 control |= IOACCEL1_CONTROL_DATA_IN;
2501 break;
2502 case DMA_NONE:
2503 control |= IOACCEL1_CONTROL_NODATAXFER;
2504 break;
2505 default:
2506 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
2507 cmd->sc_data_direction);
2508 BUG();
2509 break;
2510 }
2511 } else {
2512 control |= IOACCEL1_CONTROL_NODATAXFER;
2513 }
2514
2515 /* Fill out the command structure to submit */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002516 cp->dev_handle = ioaccel_handle & 0xFFFF;
Matt Gatese1f7de02014-02-18 13:55:17 -06002517 cp->transfer_len = total_len;
2518 cp->io_flags = IOACCEL1_IOFLAGS_IO_REQ |
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002519 (cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK);
Matt Gatese1f7de02014-02-18 13:55:17 -06002520 cp->control = control;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002521 memcpy(cp->CDB, cdb, cdb_len);
2522 memcpy(cp->CISS_LUN, scsi3addr, 8);
Matt Gatese1f7de02014-02-18 13:55:17 -06002523
2524 /* Tell the controller to post the reply to the queue for this
2525 * processor. This seems to give the best I/O throughput.
2526 */
2527 cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
2528
2529 /* Set the bits in the address sent down to include:
2530 * - performant mode bit (bit 0)
2531 * - pull count (bits 1-3)
2532 * - command type (bits 4-6)
2533 */
2534 c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[use_sg] << 1) |
2535 IOACCEL1_BUSADDR_CMDTYPE;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002536 enqueue_cmd_and_start_io(h, c);
Matt Gatese1f7de02014-02-18 13:55:17 -06002537 return 0;
2538}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002539
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002540/*
2541 * Queue a command directly to a device behind the controller using the
2542 * I/O accelerator path.
2543 */
2544static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
2545 struct CommandList *c)
2546{
2547 struct scsi_cmnd *cmd = c->scsi_cmd;
2548 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
2549
2550 return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
2551 cmd->cmnd, cmd->cmd_len, dev->scsi3addr);
2552}
2553
2554/*
2555 * Attempt to perform offload RAID mapping for a logical volume I/O.
2556 */
2557static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
2558 struct CommandList *c)
2559{
2560 struct scsi_cmnd *cmd = c->scsi_cmd;
2561 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
2562 struct raid_map_data *map = &dev->raid_map;
2563 struct raid_map_disk_data *dd = &map->data[0];
2564 int is_write = 0;
2565 u32 map_index;
2566 u64 first_block, last_block;
2567 u32 block_cnt;
2568 u32 blocks_per_row;
2569 u64 first_row, last_row;
2570 u32 first_row_offset, last_row_offset;
2571 u32 first_column, last_column;
2572 u32 map_row;
2573 u32 disk_handle;
2574 u64 disk_block;
2575 u32 disk_block_cnt;
2576 u8 cdb[16];
2577 u8 cdb_len;
2578#if BITS_PER_LONG == 32
2579 u64 tmpdiv;
2580#endif
2581
2582 BUG_ON(!(dev->offload_config && dev->offload_enabled));
2583
2584 /* check for valid opcode, get LBA and block count */
2585 switch (cmd->cmnd[0]) {
2586 case WRITE_6:
2587 is_write = 1;
2588 case READ_6:
2589 first_block =
2590 (((u64) cmd->cmnd[2]) << 8) |
2591 cmd->cmnd[3];
2592 block_cnt = cmd->cmnd[4];
2593 break;
2594 case WRITE_10:
2595 is_write = 1;
2596 case READ_10:
2597 first_block =
2598 (((u64) cmd->cmnd[2]) << 24) |
2599 (((u64) cmd->cmnd[3]) << 16) |
2600 (((u64) cmd->cmnd[4]) << 8) |
2601 cmd->cmnd[5];
2602 block_cnt =
2603 (((u32) cmd->cmnd[7]) << 8) |
2604 cmd->cmnd[8];
2605 break;
2606 case WRITE_12:
2607 is_write = 1;
2608 case READ_12:
2609 first_block =
2610 (((u64) cmd->cmnd[2]) << 24) |
2611 (((u64) cmd->cmnd[3]) << 16) |
2612 (((u64) cmd->cmnd[4]) << 8) |
2613 cmd->cmnd[5];
2614 block_cnt =
2615 (((u32) cmd->cmnd[6]) << 24) |
2616 (((u32) cmd->cmnd[7]) << 16) |
2617 (((u32) cmd->cmnd[8]) << 8) |
2618 cmd->cmnd[9];
2619 break;
2620 case WRITE_16:
2621 is_write = 1;
2622 case READ_16:
2623 first_block =
2624 (((u64) cmd->cmnd[2]) << 56) |
2625 (((u64) cmd->cmnd[3]) << 48) |
2626 (((u64) cmd->cmnd[4]) << 40) |
2627 (((u64) cmd->cmnd[5]) << 32) |
2628 (((u64) cmd->cmnd[6]) << 24) |
2629 (((u64) cmd->cmnd[7]) << 16) |
2630 (((u64) cmd->cmnd[8]) << 8) |
2631 cmd->cmnd[9];
2632 block_cnt =
2633 (((u32) cmd->cmnd[10]) << 24) |
2634 (((u32) cmd->cmnd[11]) << 16) |
2635 (((u32) cmd->cmnd[12]) << 8) |
2636 cmd->cmnd[13];
2637 break;
2638 default:
2639 return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
2640 }
2641 BUG_ON(block_cnt == 0);
2642 last_block = first_block + block_cnt - 1;
2643
2644 /* check for write to non-RAID-0 */
2645 if (is_write && dev->raid_level != 0)
2646 return IO_ACCEL_INELIGIBLE;
2647
2648 /* check for invalid block or wraparound */
2649 if (last_block >= map->volume_blk_cnt || last_block < first_block)
2650 return IO_ACCEL_INELIGIBLE;
2651
2652 /* calculate stripe information for the request */
2653 blocks_per_row = map->data_disks_per_row * map->strip_size;
2654#if BITS_PER_LONG == 32
2655 tmpdiv = first_block;
2656 (void) do_div(tmpdiv, blocks_per_row);
2657 first_row = tmpdiv;
2658 tmpdiv = last_block;
2659 (void) do_div(tmpdiv, blocks_per_row);
2660 last_row = tmpdiv;
2661 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
2662 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
2663 tmpdiv = first_row_offset;
2664 (void) do_div(tmpdiv, map->strip_size);
2665 first_column = tmpdiv;
2666 tmpdiv = last_row_offset;
2667 (void) do_div(tmpdiv, map->strip_size);
2668 last_column = tmpdiv;
2669#else
2670 first_row = first_block / blocks_per_row;
2671 last_row = last_block / blocks_per_row;
2672 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
2673 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
2674 first_column = first_row_offset / map->strip_size;
2675 last_column = last_row_offset / map->strip_size;
2676#endif
2677
2678 /* if this isn't a single row/column then give to the controller */
2679 if ((first_row != last_row) || (first_column != last_column))
2680 return IO_ACCEL_INELIGIBLE;
2681
2682 /* proceeding with driver mapping */
2683 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
2684 map->row_cnt;
2685 map_index = (map_row * (map->data_disks_per_row +
2686 map->metadata_disks_per_row)) + first_column;
2687 if (dev->raid_level == 2) {
2688 /* simple round-robin balancing of RAID 1+0 reads across
2689 * primary and mirror members. this is appropriate for SSD
2690 * but not optimal for HDD.
2691 */
2692 if (dev->offload_to_mirror)
2693 map_index += map->data_disks_per_row;
2694 dev->offload_to_mirror = !dev->offload_to_mirror;
2695 }
2696 disk_handle = dd[map_index].ioaccel_handle;
2697 disk_block = map->disk_starting_blk + (first_row * map->strip_size) +
2698 (first_row_offset - (first_column * map->strip_size));
2699 disk_block_cnt = block_cnt;
2700
2701 /* handle differing logical/physical block sizes */
2702 if (map->phys_blk_shift) {
2703 disk_block <<= map->phys_blk_shift;
2704 disk_block_cnt <<= map->phys_blk_shift;
2705 }
2706 BUG_ON(disk_block_cnt > 0xffff);
2707
2708 /* build the new CDB for the physical disk I/O */
2709 if (disk_block > 0xffffffff) {
2710 cdb[0] = is_write ? WRITE_16 : READ_16;
2711 cdb[1] = 0;
2712 cdb[2] = (u8) (disk_block >> 56);
2713 cdb[3] = (u8) (disk_block >> 48);
2714 cdb[4] = (u8) (disk_block >> 40);
2715 cdb[5] = (u8) (disk_block >> 32);
2716 cdb[6] = (u8) (disk_block >> 24);
2717 cdb[7] = (u8) (disk_block >> 16);
2718 cdb[8] = (u8) (disk_block >> 8);
2719 cdb[9] = (u8) (disk_block);
2720 cdb[10] = (u8) (disk_block_cnt >> 24);
2721 cdb[11] = (u8) (disk_block_cnt >> 16);
2722 cdb[12] = (u8) (disk_block_cnt >> 8);
2723 cdb[13] = (u8) (disk_block_cnt);
2724 cdb[14] = 0;
2725 cdb[15] = 0;
2726 cdb_len = 16;
2727 } else {
2728 cdb[0] = is_write ? WRITE_10 : READ_10;
2729 cdb[1] = 0;
2730 cdb[2] = (u8) (disk_block >> 24);
2731 cdb[3] = (u8) (disk_block >> 16);
2732 cdb[4] = (u8) (disk_block >> 8);
2733 cdb[5] = (u8) (disk_block);
2734 cdb[6] = 0;
2735 cdb[7] = (u8) (disk_block_cnt >> 8);
2736 cdb[8] = (u8) (disk_block_cnt);
2737 cdb[9] = 0;
2738 cdb_len = 10;
2739 }
2740 return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
2741 dev->scsi3addr);
2742}
2743
Jeff Garzikf2812332010-11-16 02:10:29 -05002744static int hpsa_scsi_queue_command_lck(struct scsi_cmnd *cmd,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002745 void (*done)(struct scsi_cmnd *))
2746{
2747 struct ctlr_info *h;
2748 struct hpsa_scsi_dev_t *dev;
2749 unsigned char scsi3addr[8];
2750 struct CommandList *c;
2751 unsigned long flags;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002752 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002753
2754 /* Get the ptr to our adapter structure out of cmd->host. */
2755 h = sdev_to_hba(cmd->device);
2756 dev = cmd->device->hostdata;
2757 if (!dev) {
2758 cmd->result = DID_NO_CONNECT << 16;
2759 done(cmd);
2760 return 0;
2761 }
2762 memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
2763
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002764 spin_lock_irqsave(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05002765 if (unlikely(h->lockup_detected)) {
2766 spin_unlock_irqrestore(&h->lock, flags);
2767 cmd->result = DID_ERROR << 16;
2768 done(cmd);
2769 return 0;
2770 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002771 spin_unlock_irqrestore(&h->lock, flags);
Matt Gatese16a33a2012-05-01 11:43:11 -05002772 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002773 if (c == NULL) { /* trouble... */
2774 dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
2775 return SCSI_MLQUEUE_HOST_BUSY;
2776 }
2777
2778 /* Fill in the command list header */
2779
2780 cmd->scsi_done = done; /* save this for use by completion code */
2781
2782 /* save c in case we have to abort it */
2783 cmd->host_scribble = (unsigned char *) c;
2784
2785 c->cmd_type = CMD_SCSI;
2786 c->scsi_cmd = cmd;
Matt Gatese1f7de02014-02-18 13:55:17 -06002787
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002788 /* Call alternate submit routine for I/O accelerated commands.
2789 * Retries always go down the normal I/O path.
2790 */
2791 if (likely(cmd->retries == 0 &&
2792 cmd->request->cmd_type == REQ_TYPE_FS)) {
2793 if (dev->offload_enabled) {
2794 rc = hpsa_scsi_ioaccel_raid_map(h, c);
2795 if (rc == 0)
2796 return 0; /* Sent on ioaccel path */
2797 if (rc < 0) { /* scsi_dma_map failed. */
2798 cmd_free(h, c);
2799 return SCSI_MLQUEUE_HOST_BUSY;
2800 }
2801 } else if (dev->ioaccel_handle) {
2802 rc = hpsa_scsi_ioaccel_direct_map(h, c);
2803 if (rc == 0)
2804 return 0; /* Sent on direct map path */
2805 if (rc < 0) { /* scsi_dma_map failed. */
2806 cmd_free(h, c);
2807 return SCSI_MLQUEUE_HOST_BUSY;
2808 }
2809 }
2810 }
Matt Gatese1f7de02014-02-18 13:55:17 -06002811
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002812 c->Header.ReplyQueue = 0; /* unused in simple mode */
2813 memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
Don Brace303932f2010-02-04 08:42:40 -06002814 c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT);
2815 c->Header.Tag.lower |= DIRECT_LOOKUP_BIT;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002816
2817 /* Fill in the request block... */
2818
2819 c->Request.Timeout = 0;
2820 memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
2821 BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
2822 c->Request.CDBLen = cmd->cmd_len;
2823 memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
2824 c->Request.Type.Type = TYPE_CMD;
2825 c->Request.Type.Attribute = ATTR_SIMPLE;
2826 switch (cmd->sc_data_direction) {
2827 case DMA_TO_DEVICE:
2828 c->Request.Type.Direction = XFER_WRITE;
2829 break;
2830 case DMA_FROM_DEVICE:
2831 c->Request.Type.Direction = XFER_READ;
2832 break;
2833 case DMA_NONE:
2834 c->Request.Type.Direction = XFER_NONE;
2835 break;
2836 case DMA_BIDIRECTIONAL:
2837 /* This can happen if a buggy application does a scsi passthru
2838 * and sets both inlen and outlen to non-zero. ( see
2839 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
2840 */
2841
2842 c->Request.Type.Direction = XFER_RSVD;
2843 /* This is technically wrong, and hpsa controllers should
2844 * reject it with CMD_INVALID, which is the most correct
2845 * response, but non-fibre backends appear to let it
2846 * slide by, and give the same results as if this field
2847 * were set correctly. Either way is acceptable for
2848 * our purposes here.
2849 */
2850
2851 break;
2852
2853 default:
2854 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
2855 cmd->sc_data_direction);
2856 BUG();
2857 break;
2858 }
2859
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002860 if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002861 cmd_free(h, c);
2862 return SCSI_MLQUEUE_HOST_BUSY;
2863 }
2864 enqueue_cmd_and_start_io(h, c);
2865 /* the cmd'll come back via intr handler in complete_scsi_command() */
2866 return 0;
2867}
2868
Jeff Garzikf2812332010-11-16 02:10:29 -05002869static DEF_SCSI_QCMD(hpsa_scsi_queue_command)
2870
Stephen M. Camerona08a8472010-02-04 08:43:16 -06002871static void hpsa_scan_start(struct Scsi_Host *sh)
2872{
2873 struct ctlr_info *h = shost_to_hba(sh);
2874 unsigned long flags;
2875
2876 /* wait until any scan already in progress is finished. */
2877 while (1) {
2878 spin_lock_irqsave(&h->scan_lock, flags);
2879 if (h->scan_finished)
2880 break;
2881 spin_unlock_irqrestore(&h->scan_lock, flags);
2882 wait_event(h->scan_wait_queue, h->scan_finished);
2883 /* Note: We don't need to worry about a race between this
2884 * thread and driver unload because the midlayer will
2885 * have incremented the reference count, so unload won't
2886 * happen if we're in here.
2887 */
2888 }
2889 h->scan_finished = 0; /* mark scan as in progress */
2890 spin_unlock_irqrestore(&h->scan_lock, flags);
2891
2892 hpsa_update_scsi_devices(h, h->scsi_host->host_no);
2893
2894 spin_lock_irqsave(&h->scan_lock, flags);
2895 h->scan_finished = 1; /* mark scan as finished. */
2896 wake_up_all(&h->scan_wait_queue);
2897 spin_unlock_irqrestore(&h->scan_lock, flags);
2898}
2899
2900static int hpsa_scan_finished(struct Scsi_Host *sh,
2901 unsigned long elapsed_time)
2902{
2903 struct ctlr_info *h = shost_to_hba(sh);
2904 unsigned long flags;
2905 int finished;
2906
2907 spin_lock_irqsave(&h->scan_lock, flags);
2908 finished = h->scan_finished;
2909 spin_unlock_irqrestore(&h->scan_lock, flags);
2910 return finished;
2911}
2912
Stephen M. Cameron667e23d2010-02-25 14:02:51 -06002913static int hpsa_change_queue_depth(struct scsi_device *sdev,
2914 int qdepth, int reason)
2915{
2916 struct ctlr_info *h = sdev_to_hba(sdev);
2917
2918 if (reason != SCSI_QDEPTH_DEFAULT)
2919 return -ENOTSUPP;
2920
2921 if (qdepth < 1)
2922 qdepth = 1;
2923 else
2924 if (qdepth > h->nr_cmds)
2925 qdepth = h->nr_cmds;
2926 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
2927 return sdev->queue_depth;
2928}
2929
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002930static void hpsa_unregister_scsi(struct ctlr_info *h)
2931{
2932 /* we are being forcibly unloaded, and may not refuse. */
2933 scsi_remove_host(h->scsi_host);
2934 scsi_host_put(h->scsi_host);
2935 h->scsi_host = NULL;
2936}
2937
2938static int hpsa_register_scsi(struct ctlr_info *h)
2939{
Stephen M. Cameronb7056902012-01-19 14:00:53 -06002940 struct Scsi_Host *sh;
2941 int error;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002942
Stephen M. Cameronb7056902012-01-19 14:00:53 -06002943 sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
2944 if (sh == NULL)
2945 goto fail;
2946
2947 sh->io_port = 0;
2948 sh->n_io_port = 0;
2949 sh->this_id = -1;
2950 sh->max_channel = 3;
2951 sh->max_cmd_len = MAX_COMMAND_SIZE;
2952 sh->max_lun = HPSA_MAX_LUN;
2953 sh->max_id = HPSA_MAX_LUN;
2954 sh->can_queue = h->nr_cmds;
2955 sh->cmd_per_lun = h->nr_cmds;
2956 sh->sg_tablesize = h->maxsgentries;
2957 h->scsi_host = sh;
2958 sh->hostdata[0] = (unsigned long) h;
2959 sh->irq = h->intr[h->intr_mode];
2960 sh->unique_id = sh->irq;
2961 error = scsi_add_host(sh, &h->pdev->dev);
2962 if (error)
2963 goto fail_host_put;
2964 scsi_scan_host(sh);
2965 return 0;
2966
2967 fail_host_put:
2968 dev_err(&h->pdev->dev, "%s: scsi_add_host"
2969 " failed for controller %d\n", __func__, h->ctlr);
2970 scsi_host_put(sh);
2971 return error;
2972 fail:
2973 dev_err(&h->pdev->dev, "%s: scsi_host_alloc"
2974 " failed for controller %d\n", __func__, h->ctlr);
2975 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002976}
2977
2978static int wait_for_device_to_become_ready(struct ctlr_info *h,
2979 unsigned char lunaddr[])
2980{
2981 int rc = 0;
2982 int count = 0;
2983 int waittime = 1; /* seconds */
2984 struct CommandList *c;
2985
2986 c = cmd_special_alloc(h);
2987 if (!c) {
2988 dev_warn(&h->pdev->dev, "out of memory in "
2989 "wait_for_device_to_become_ready.\n");
2990 return IO_ERROR;
2991 }
2992
2993 /* Send test unit ready until device ready, or give up. */
2994 while (count < HPSA_TUR_RETRY_LIMIT) {
2995
2996 /* Wait for a bit. do this first, because if we send
2997 * the TUR right away, the reset will just abort it.
2998 */
2999 msleep(1000 * waittime);
3000 count++;
3001
3002 /* Increase wait time with each try, up to a point. */
3003 if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
3004 waittime = waittime * 2;
3005
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003006 /* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
3007 (void) fill_cmd(c, TEST_UNIT_READY, h,
3008 NULL, 0, 0, lunaddr, TYPE_CMD);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003009 hpsa_scsi_do_simple_cmd_core(h, c);
3010 /* no unmap needed here because no data xfer. */
3011
3012 if (c->err_info->CommandStatus == CMD_SUCCESS)
3013 break;
3014
3015 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
3016 c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
3017 (c->err_info->SenseInfo[2] == NO_SENSE ||
3018 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
3019 break;
3020
3021 dev_warn(&h->pdev->dev, "waiting %d secs "
3022 "for device to become ready.\n", waittime);
3023 rc = 1; /* device not ready. */
3024 }
3025
3026 if (rc)
3027 dev_warn(&h->pdev->dev, "giving up on device.\n");
3028 else
3029 dev_warn(&h->pdev->dev, "device is ready.\n");
3030
3031 cmd_special_free(h, c);
3032 return rc;
3033}
3034
3035/* Need at least one of these error handlers to keep ../scsi/hosts.c from
3036 * complaining. Doing a host- or bus-reset can't do anything good here.
3037 */
3038static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
3039{
3040 int rc;
3041 struct ctlr_info *h;
3042 struct hpsa_scsi_dev_t *dev;
3043
3044 /* find the controller to which the command to be aborted was sent */
3045 h = sdev_to_hba(scsicmd->device);
3046 if (h == NULL) /* paranoia */
3047 return FAILED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003048 dev = scsicmd->device->hostdata;
3049 if (!dev) {
3050 dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
3051 "device lookup failed.\n");
3052 return FAILED;
3053 }
Stephen M. Camerond416b0c2010-02-04 08:43:21 -06003054 dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
3055 h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003056 /* send a reset to the SCSI LUN which the command was sent to */
3057 rc = hpsa_send_reset(h, dev->scsi3addr);
3058 if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
3059 return SUCCESS;
3060
3061 dev_warn(&h->pdev->dev, "resetting device failed.\n");
3062 return FAILED;
3063}
3064
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003065static void swizzle_abort_tag(u8 *tag)
3066{
3067 u8 original_tag[8];
3068
3069 memcpy(original_tag, tag, 8);
3070 tag[0] = original_tag[3];
3071 tag[1] = original_tag[2];
3072 tag[2] = original_tag[1];
3073 tag[3] = original_tag[0];
3074 tag[4] = original_tag[7];
3075 tag[5] = original_tag[6];
3076 tag[6] = original_tag[5];
3077 tag[7] = original_tag[4];
3078}
3079
Scott Teel17eb87d2014-02-18 13:55:28 -06003080static void hpsa_get_tag(struct ctlr_info *h,
3081 struct CommandList *c, u32 *taglower, u32 *tagupper)
3082{
3083 if (c->cmd_type == CMD_IOACCEL1) {
3084 struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
3085 &h->ioaccel_cmd_pool[c->cmdindex];
3086 *tagupper = cm1->Tag.upper;
3087 *taglower = cm1->Tag.lower;
3088 } else {
3089 *tagupper = c->Header.Tag.upper;
3090 *taglower = c->Header.Tag.lower;
3091 }
3092}
3093
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003094static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003095 struct CommandList *abort, int swizzle)
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003096{
3097 int rc = IO_OK;
3098 struct CommandList *c;
3099 struct ErrorInfo *ei;
Scott Teel17eb87d2014-02-18 13:55:28 -06003100 u32 tagupper, taglower;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003101
3102 c = cmd_special_alloc(h);
3103 if (c == NULL) { /* trouble... */
3104 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
3105 return -ENOMEM;
3106 }
3107
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003108 /* fill_cmd can't fail here, no buffer to map */
3109 (void) fill_cmd(c, HPSA_ABORT_MSG, h, abort,
3110 0, 0, scsi3addr, TYPE_MSG);
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003111 if (swizzle)
3112 swizzle_abort_tag(&c->Request.CDB[4]);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003113 hpsa_scsi_do_simple_cmd_core(h, c);
Scott Teel17eb87d2014-02-18 13:55:28 -06003114 hpsa_get_tag(h, abort, &taglower, &tagupper);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003115 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd_core completed.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06003116 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003117 /* no unmap needed here because no data xfer. */
3118
3119 ei = c->err_info;
3120 switch (ei->CommandStatus) {
3121 case CMD_SUCCESS:
3122 break;
3123 case CMD_UNABORTABLE: /* Very common, don't make noise. */
3124 rc = -1;
3125 break;
3126 default:
3127 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06003128 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003129 hpsa_scsi_interpret_error(c);
3130 rc = -1;
3131 break;
3132 }
3133 cmd_special_free(h, c);
3134 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n", __func__,
3135 abort->Header.Tag.upper, abort->Header.Tag.lower);
3136 return rc;
3137}
3138
3139/*
3140 * hpsa_find_cmd_in_queue
3141 *
3142 * Used to determine whether a command (find) is still present
3143 * in queue_head. Optionally excludes the last element of queue_head.
3144 *
3145 * This is used to avoid unnecessary aborts. Commands in h->reqQ have
3146 * not yet been submitted, and so can be aborted by the driver without
3147 * sending an abort to the hardware.
3148 *
3149 * Returns pointer to command if found in queue, NULL otherwise.
3150 */
3151static struct CommandList *hpsa_find_cmd_in_queue(struct ctlr_info *h,
3152 struct scsi_cmnd *find, struct list_head *queue_head)
3153{
3154 unsigned long flags;
3155 struct CommandList *c = NULL; /* ptr into cmpQ */
3156
3157 if (!find)
3158 return 0;
3159 spin_lock_irqsave(&h->lock, flags);
3160 list_for_each_entry(c, queue_head, list) {
3161 if (c->scsi_cmd == NULL) /* e.g.: passthru ioctl */
3162 continue;
3163 if (c->scsi_cmd == find) {
3164 spin_unlock_irqrestore(&h->lock, flags);
3165 return c;
3166 }
3167 }
3168 spin_unlock_irqrestore(&h->lock, flags);
3169 return NULL;
3170}
3171
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003172static struct CommandList *hpsa_find_cmd_in_queue_by_tag(struct ctlr_info *h,
3173 u8 *tag, struct list_head *queue_head)
3174{
3175 unsigned long flags;
3176 struct CommandList *c;
3177
3178 spin_lock_irqsave(&h->lock, flags);
3179 list_for_each_entry(c, queue_head, list) {
3180 if (memcmp(&c->Header.Tag, tag, 8) != 0)
3181 continue;
3182 spin_unlock_irqrestore(&h->lock, flags);
3183 return c;
3184 }
3185 spin_unlock_irqrestore(&h->lock, flags);
3186 return NULL;
3187}
3188
3189/* Some Smart Arrays need the abort tag swizzled, and some don't. It's hard to
3190 * tell which kind we're dealing with, so we send the abort both ways. There
3191 * shouldn't be any collisions between swizzled and unswizzled tags due to the
3192 * way we construct our tags but we check anyway in case the assumptions which
3193 * make this true someday become false.
3194 */
3195static int hpsa_send_abort_both_ways(struct ctlr_info *h,
3196 unsigned char *scsi3addr, struct CommandList *abort)
3197{
3198 u8 swizzled_tag[8];
3199 struct CommandList *c;
3200 int rc = 0, rc2 = 0;
3201
3202 /* we do not expect to find the swizzled tag in our queue, but
3203 * check anyway just to be sure the assumptions which make this
3204 * the case haven't become wrong.
3205 */
3206 memcpy(swizzled_tag, &abort->Request.CDB[4], 8);
3207 swizzle_abort_tag(swizzled_tag);
3208 c = hpsa_find_cmd_in_queue_by_tag(h, swizzled_tag, &h->cmpQ);
3209 if (c != NULL) {
3210 dev_warn(&h->pdev->dev, "Unexpectedly found byte-swapped tag in completion queue.\n");
3211 return hpsa_send_abort(h, scsi3addr, abort, 0);
3212 }
3213 rc = hpsa_send_abort(h, scsi3addr, abort, 0);
3214
3215 /* if the command is still in our queue, we can't conclude that it was
3216 * aborted (it might have just completed normally) but in any case
3217 * we don't need to try to abort it another way.
3218 */
3219 c = hpsa_find_cmd_in_queue(h, abort->scsi_cmd, &h->cmpQ);
3220 if (c)
3221 rc2 = hpsa_send_abort(h, scsi3addr, abort, 1);
3222 return rc && rc2;
3223}
3224
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003225/* Send an abort for the specified command.
3226 * If the device and controller support it,
3227 * send a task abort request.
3228 */
3229static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
3230{
3231
3232 int i, rc;
3233 struct ctlr_info *h;
3234 struct hpsa_scsi_dev_t *dev;
3235 struct CommandList *abort; /* pointer to command to be aborted */
3236 struct CommandList *found;
3237 struct scsi_cmnd *as; /* ptr to scsi cmd inside aborted command. */
3238 char msg[256]; /* For debug messaging. */
3239 int ml = 0;
Scott Teel17eb87d2014-02-18 13:55:28 -06003240 u32 tagupper, taglower;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003241
3242 /* Find the controller of the command to be aborted */
3243 h = sdev_to_hba(sc->device);
3244 if (WARN(h == NULL,
3245 "ABORT REQUEST FAILED, Controller lookup failed.\n"))
3246 return FAILED;
3247
3248 /* Check that controller supports some kind of task abort */
3249 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
3250 !(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
3251 return FAILED;
3252
3253 memset(msg, 0, sizeof(msg));
3254 ml += sprintf(msg+ml, "ABORT REQUEST on C%d:B%d:T%d:L%d ",
3255 h->scsi_host->host_no, sc->device->channel,
3256 sc->device->id, sc->device->lun);
3257
3258 /* Find the device of the command to be aborted */
3259 dev = sc->device->hostdata;
3260 if (!dev) {
3261 dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
3262 msg);
3263 return FAILED;
3264 }
3265
3266 /* Get SCSI command to be aborted */
3267 abort = (struct CommandList *) sc->host_scribble;
3268 if (abort == NULL) {
3269 dev_err(&h->pdev->dev, "%s FAILED, Command to abort is NULL.\n",
3270 msg);
3271 return FAILED;
3272 }
Scott Teel17eb87d2014-02-18 13:55:28 -06003273 hpsa_get_tag(h, abort, &taglower, &tagupper);
3274 ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003275 as = (struct scsi_cmnd *) abort->scsi_cmd;
3276 if (as != NULL)
3277 ml += sprintf(msg+ml, "Command:0x%x SN:0x%lx ",
3278 as->cmnd[0], as->serial_number);
3279 dev_dbg(&h->pdev->dev, "%s\n", msg);
3280 dev_warn(&h->pdev->dev, "Abort request on C%d:B%d:T%d:L%d\n",
3281 h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
3282
3283 /* Search reqQ to See if command is queued but not submitted,
3284 * if so, complete the command with aborted status and remove
3285 * it from the reqQ.
3286 */
3287 found = hpsa_find_cmd_in_queue(h, sc, &h->reqQ);
3288 if (found) {
3289 found->err_info->CommandStatus = CMD_ABORTED;
3290 finish_cmd(found);
3291 dev_info(&h->pdev->dev, "%s Request SUCCEEDED (driver queue).\n",
3292 msg);
3293 return SUCCESS;
3294 }
3295
3296 /* not in reqQ, if also not in cmpQ, must have already completed */
3297 found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
3298 if (!found) {
Stephen M. Camerond6ebd0f2012-07-26 11:34:17 -05003299 dev_dbg(&h->pdev->dev, "%s Request SUCCEEDED (not known to driver).\n",
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003300 msg);
3301 return SUCCESS;
3302 }
3303
3304 /*
3305 * Command is in flight, or possibly already completed
3306 * by the firmware (but not to the scsi mid layer) but we can't
3307 * distinguish which. Send the abort down.
3308 */
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003309 rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003310 if (rc != 0) {
3311 dev_dbg(&h->pdev->dev, "%s Request FAILED.\n", msg);
3312 dev_warn(&h->pdev->dev, "FAILED abort on device C%d:B%d:T%d:L%d\n",
3313 h->scsi_host->host_no,
3314 dev->bus, dev->target, dev->lun);
3315 return FAILED;
3316 }
3317 dev_info(&h->pdev->dev, "%s REQUEST SUCCEEDED.\n", msg);
3318
3319 /* If the abort(s) above completed and actually aborted the
3320 * command, then the command to be aborted should already be
3321 * completed. If not, wait around a bit more to see if they
3322 * manage to complete normally.
3323 */
3324#define ABORT_COMPLETE_WAIT_SECS 30
3325 for (i = 0; i < ABORT_COMPLETE_WAIT_SECS * 10; i++) {
3326 found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
3327 if (!found)
3328 return SUCCESS;
3329 msleep(100);
3330 }
3331 dev_warn(&h->pdev->dev, "%s FAILED. Aborted command has not completed after %d seconds.\n",
3332 msg, ABORT_COMPLETE_WAIT_SECS);
3333 return FAILED;
3334}
3335
3336
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003337/*
3338 * For operations that cannot sleep, a command block is allocated at init,
3339 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
3340 * which ones are free or in use. Lock must be held when calling this.
3341 * cmd_free() is the complement.
3342 */
3343static struct CommandList *cmd_alloc(struct ctlr_info *h)
3344{
3345 struct CommandList *c;
3346 int i;
3347 union u64bit temp64;
3348 dma_addr_t cmd_dma_handle, err_dma_handle;
Matt Gatese16a33a2012-05-01 11:43:11 -05003349 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003350
Matt Gatese16a33a2012-05-01 11:43:11 -05003351 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003352 do {
3353 i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
Matt Gatese16a33a2012-05-01 11:43:11 -05003354 if (i == h->nr_cmds) {
3355 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003356 return NULL;
Matt Gatese16a33a2012-05-01 11:43:11 -05003357 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003358 } while (test_and_set_bit
3359 (i & (BITS_PER_LONG - 1),
3360 h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
Matt Gatese16a33a2012-05-01 11:43:11 -05003361 spin_unlock_irqrestore(&h->lock, flags);
3362
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003363 c = h->cmd_pool + i;
3364 memset(c, 0, sizeof(*c));
3365 cmd_dma_handle = h->cmd_pool_dhandle
3366 + i * sizeof(*c);
3367 c->err_info = h->errinfo_pool + i;
3368 memset(c->err_info, 0, sizeof(*c->err_info));
3369 err_dma_handle = h->errinfo_pool_dhandle
3370 + i * sizeof(*c->err_info);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003371
3372 c->cmdindex = i;
3373
Stephen M. Cameron9e0fc7642011-02-15 15:32:48 -06003374 INIT_LIST_HEAD(&c->list);
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06003375 c->busaddr = (u32) cmd_dma_handle;
3376 temp64.val = (u64) err_dma_handle;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003377 c->ErrDesc.Addr.lower = temp64.val32.lower;
3378 c->ErrDesc.Addr.upper = temp64.val32.upper;
3379 c->ErrDesc.Len = sizeof(*c->err_info);
3380
3381 c->h = h;
3382 return c;
3383}
3384
3385/* For operations that can wait for kmalloc to possibly sleep,
3386 * this routine can be called. Lock need not be held to call
3387 * cmd_special_alloc. cmd_special_free() is the complement.
3388 */
3389static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
3390{
3391 struct CommandList *c;
3392 union u64bit temp64;
3393 dma_addr_t cmd_dma_handle, err_dma_handle;
3394
3395 c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
3396 if (c == NULL)
3397 return NULL;
3398 memset(c, 0, sizeof(*c));
3399
Matt Gatese1f7de02014-02-18 13:55:17 -06003400 c->cmd_type = CMD_SCSI;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003401 c->cmdindex = -1;
3402
3403 c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info),
3404 &err_dma_handle);
3405
3406 if (c->err_info == NULL) {
3407 pci_free_consistent(h->pdev,
3408 sizeof(*c), c, cmd_dma_handle);
3409 return NULL;
3410 }
3411 memset(c->err_info, 0, sizeof(*c->err_info));
3412
Stephen M. Cameron9e0fc7642011-02-15 15:32:48 -06003413 INIT_LIST_HEAD(&c->list);
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06003414 c->busaddr = (u32) cmd_dma_handle;
3415 temp64.val = (u64) err_dma_handle;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003416 c->ErrDesc.Addr.lower = temp64.val32.lower;
3417 c->ErrDesc.Addr.upper = temp64.val32.upper;
3418 c->ErrDesc.Len = sizeof(*c->err_info);
3419
3420 c->h = h;
3421 return c;
3422}
3423
3424static void cmd_free(struct ctlr_info *h, struct CommandList *c)
3425{
3426 int i;
Matt Gatese16a33a2012-05-01 11:43:11 -05003427 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003428
3429 i = c - h->cmd_pool;
Matt Gatese16a33a2012-05-01 11:43:11 -05003430 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003431 clear_bit(i & (BITS_PER_LONG - 1),
3432 h->cmd_pool_bits + (i / BITS_PER_LONG));
Matt Gatese16a33a2012-05-01 11:43:11 -05003433 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003434}
3435
3436static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
3437{
3438 union u64bit temp64;
3439
3440 temp64.val32.lower = c->ErrDesc.Addr.lower;
3441 temp64.val32.upper = c->ErrDesc.Addr.upper;
3442 pci_free_consistent(h->pdev, sizeof(*c->err_info),
3443 c->err_info, (dma_addr_t) temp64.val);
3444 pci_free_consistent(h->pdev, sizeof(*c),
Stephen M. Camerond896f3f2011-01-06 14:47:53 -06003445 c, (dma_addr_t) (c->busaddr & DIRECT_LOOKUP_MASK));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003446}
3447
3448#ifdef CONFIG_COMPAT
3449
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003450static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
3451{
3452 IOCTL32_Command_struct __user *arg32 =
3453 (IOCTL32_Command_struct __user *) arg;
3454 IOCTL_Command_struct arg64;
3455 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
3456 int err;
3457 u32 cp;
3458
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06003459 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003460 err = 0;
3461 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
3462 sizeof(arg64.LUN_info));
3463 err |= copy_from_user(&arg64.Request, &arg32->Request,
3464 sizeof(arg64.Request));
3465 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
3466 sizeof(arg64.error_info));
3467 err |= get_user(arg64.buf_size, &arg32->buf_size);
3468 err |= get_user(cp, &arg32->buf);
3469 arg64.buf = compat_ptr(cp);
3470 err |= copy_to_user(p, &arg64, sizeof(arg64));
3471
3472 if (err)
3473 return -EFAULT;
3474
Stephen M. Camerone39eeae2010-02-04 08:43:46 -06003475 err = hpsa_ioctl(dev, CCISS_PASSTHRU, (void *)p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003476 if (err)
3477 return err;
3478 err |= copy_in_user(&arg32->error_info, &p->error_info,
3479 sizeof(arg32->error_info));
3480 if (err)
3481 return -EFAULT;
3482 return err;
3483}
3484
3485static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
3486 int cmd, void *arg)
3487{
3488 BIG_IOCTL32_Command_struct __user *arg32 =
3489 (BIG_IOCTL32_Command_struct __user *) arg;
3490 BIG_IOCTL_Command_struct arg64;
3491 BIG_IOCTL_Command_struct __user *p =
3492 compat_alloc_user_space(sizeof(arg64));
3493 int err;
3494 u32 cp;
3495
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06003496 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003497 err = 0;
3498 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
3499 sizeof(arg64.LUN_info));
3500 err |= copy_from_user(&arg64.Request, &arg32->Request,
3501 sizeof(arg64.Request));
3502 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
3503 sizeof(arg64.error_info));
3504 err |= get_user(arg64.buf_size, &arg32->buf_size);
3505 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
3506 err |= get_user(cp, &arg32->buf);
3507 arg64.buf = compat_ptr(cp);
3508 err |= copy_to_user(p, &arg64, sizeof(arg64));
3509
3510 if (err)
3511 return -EFAULT;
3512
Stephen M. Camerone39eeae2010-02-04 08:43:46 -06003513 err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003514 if (err)
3515 return err;
3516 err |= copy_in_user(&arg32->error_info, &p->error_info,
3517 sizeof(arg32->error_info));
3518 if (err)
3519 return -EFAULT;
3520 return err;
3521}
Stephen M. Cameron71fe75a2010-02-04 08:43:51 -06003522
3523static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
3524{
3525 switch (cmd) {
3526 case CCISS_GETPCIINFO:
3527 case CCISS_GETINTINFO:
3528 case CCISS_SETINTINFO:
3529 case CCISS_GETNODENAME:
3530 case CCISS_SETNODENAME:
3531 case CCISS_GETHEARTBEAT:
3532 case CCISS_GETBUSTYPES:
3533 case CCISS_GETFIRMVER:
3534 case CCISS_GETDRIVVER:
3535 case CCISS_REVALIDVOLS:
3536 case CCISS_DEREGDISK:
3537 case CCISS_REGNEWDISK:
3538 case CCISS_REGNEWD:
3539 case CCISS_RESCANDISK:
3540 case CCISS_GETLUNINFO:
3541 return hpsa_ioctl(dev, cmd, arg);
3542
3543 case CCISS_PASSTHRU32:
3544 return hpsa_ioctl32_passthru(dev, cmd, arg);
3545 case CCISS_BIG_PASSTHRU32:
3546 return hpsa_ioctl32_big_passthru(dev, cmd, arg);
3547
3548 default:
3549 return -ENOIOCTLCMD;
3550 }
3551}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003552#endif
3553
3554static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
3555{
3556 struct hpsa_pci_info pciinfo;
3557
3558 if (!argp)
3559 return -EINVAL;
3560 pciinfo.domain = pci_domain_nr(h->pdev->bus);
3561 pciinfo.bus = h->pdev->bus->number;
3562 pciinfo.dev_fn = h->pdev->devfn;
3563 pciinfo.board_id = h->board_id;
3564 if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
3565 return -EFAULT;
3566 return 0;
3567}
3568
3569static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
3570{
3571 DriverVer_type DriverVer;
3572 unsigned char vmaj, vmin, vsubmin;
3573 int rc;
3574
3575 rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
3576 &vmaj, &vmin, &vsubmin);
3577 if (rc != 3) {
3578 dev_info(&h->pdev->dev, "driver version string '%s' "
3579 "unrecognized.", HPSA_DRIVER_VERSION);
3580 vmaj = 0;
3581 vmin = 0;
3582 vsubmin = 0;
3583 }
3584 DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
3585 if (!argp)
3586 return -EINVAL;
3587 if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
3588 return -EFAULT;
3589 return 0;
3590}
3591
3592static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
3593{
3594 IOCTL_Command_struct iocommand;
3595 struct CommandList *c;
3596 char *buff = NULL;
3597 union u64bit temp64;
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003598 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003599
3600 if (!argp)
3601 return -EINVAL;
3602 if (!capable(CAP_SYS_RAWIO))
3603 return -EPERM;
3604 if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
3605 return -EFAULT;
3606 if ((iocommand.buf_size < 1) &&
3607 (iocommand.Request.Type.Direction != XFER_NONE)) {
3608 return -EINVAL;
3609 }
3610 if (iocommand.buf_size > 0) {
3611 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
3612 if (buff == NULL)
3613 return -EFAULT;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003614 if (iocommand.Request.Type.Direction == XFER_WRITE) {
3615 /* Copy the data into the buffer we created */
3616 if (copy_from_user(buff, iocommand.buf,
3617 iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003618 rc = -EFAULT;
3619 goto out_kfree;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003620 }
3621 } else {
3622 memset(buff, 0, iocommand.buf_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003623 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003624 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003625 c = cmd_special_alloc(h);
3626 if (c == NULL) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003627 rc = -ENOMEM;
3628 goto out_kfree;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003629 }
3630 /* Fill in the command type */
3631 c->cmd_type = CMD_IOCTL_PEND;
3632 /* Fill in Command Header */
3633 c->Header.ReplyQueue = 0; /* unused in simple mode */
3634 if (iocommand.buf_size > 0) { /* buffer to fill */
3635 c->Header.SGList = 1;
3636 c->Header.SGTotal = 1;
3637 } else { /* no buffers to fill */
3638 c->Header.SGList = 0;
3639 c->Header.SGTotal = 0;
3640 }
3641 memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
3642 /* use the kernel address the cmd block for tag */
3643 c->Header.Tag.lower = c->busaddr;
3644
3645 /* Fill in Request block */
3646 memcpy(&c->Request, &iocommand.Request,
3647 sizeof(c->Request));
3648
3649 /* Fill in the scatter gather information */
3650 if (iocommand.buf_size > 0) {
3651 temp64.val = pci_map_single(h->pdev, buff,
3652 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06003653 if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
3654 c->SG[0].Addr.lower = 0;
3655 c->SG[0].Addr.upper = 0;
3656 c->SG[0].Len = 0;
3657 rc = -ENOMEM;
3658 goto out;
3659 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003660 c->SG[0].Addr.lower = temp64.val32.lower;
3661 c->SG[0].Addr.upper = temp64.val32.upper;
3662 c->SG[0].Len = iocommand.buf_size;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06003663 c->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining*/
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003664 }
Stephen M. Camerona0c12412011-10-26 16:22:04 -05003665 hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
Stephen M. Cameronc2dd32e2011-06-03 09:57:29 -05003666 if (iocommand.buf_size > 0)
3667 hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003668 check_ioctl_unit_attention(h, c);
3669
3670 /* Copy the error information out */
3671 memcpy(&iocommand.error_info, c->err_info,
3672 sizeof(iocommand.error_info));
3673 if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003674 rc = -EFAULT;
3675 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003676 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003677 if (iocommand.Request.Type.Direction == XFER_READ &&
3678 iocommand.buf_size > 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003679 /* Copy the data out of the buffer we created */
3680 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003681 rc = -EFAULT;
3682 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003683 }
3684 }
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003685out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003686 cmd_special_free(h, c);
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06003687out_kfree:
3688 kfree(buff);
3689 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003690}
3691
3692static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
3693{
3694 BIG_IOCTL_Command_struct *ioc;
3695 struct CommandList *c;
3696 unsigned char **buff = NULL;
3697 int *buff_size = NULL;
3698 union u64bit temp64;
3699 BYTE sg_used = 0;
3700 int status = 0;
3701 int i;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06003702 u32 left;
3703 u32 sz;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003704 BYTE __user *data_ptr;
3705
3706 if (!argp)
3707 return -EINVAL;
3708 if (!capable(CAP_SYS_RAWIO))
3709 return -EPERM;
3710 ioc = (BIG_IOCTL_Command_struct *)
3711 kmalloc(sizeof(*ioc), GFP_KERNEL);
3712 if (!ioc) {
3713 status = -ENOMEM;
3714 goto cleanup1;
3715 }
3716 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
3717 status = -EFAULT;
3718 goto cleanup1;
3719 }
3720 if ((ioc->buf_size < 1) &&
3721 (ioc->Request.Type.Direction != XFER_NONE)) {
3722 status = -EINVAL;
3723 goto cleanup1;
3724 }
3725 /* Check kmalloc limits using all SGs */
3726 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
3727 status = -EINVAL;
3728 goto cleanup1;
3729 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06003730 if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003731 status = -EINVAL;
3732 goto cleanup1;
3733 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06003734 buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003735 if (!buff) {
3736 status = -ENOMEM;
3737 goto cleanup1;
3738 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06003739 buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003740 if (!buff_size) {
3741 status = -ENOMEM;
3742 goto cleanup1;
3743 }
3744 left = ioc->buf_size;
3745 data_ptr = ioc->buf;
3746 while (left) {
3747 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
3748 buff_size[sg_used] = sz;
3749 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
3750 if (buff[sg_used] == NULL) {
3751 status = -ENOMEM;
3752 goto cleanup1;
3753 }
3754 if (ioc->Request.Type.Direction == XFER_WRITE) {
3755 if (copy_from_user(buff[sg_used], data_ptr, sz)) {
3756 status = -ENOMEM;
3757 goto cleanup1;
3758 }
3759 } else
3760 memset(buff[sg_used], 0, sz);
3761 left -= sz;
3762 data_ptr += sz;
3763 sg_used++;
3764 }
3765 c = cmd_special_alloc(h);
3766 if (c == NULL) {
3767 status = -ENOMEM;
3768 goto cleanup1;
3769 }
3770 c->cmd_type = CMD_IOCTL_PEND;
3771 c->Header.ReplyQueue = 0;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003772 c->Header.SGList = c->Header.SGTotal = sg_used;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003773 memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
3774 c->Header.Tag.lower = c->busaddr;
3775 memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
3776 if (ioc->buf_size > 0) {
3777 int i;
3778 for (i = 0; i < sg_used; i++) {
3779 temp64.val = pci_map_single(h->pdev, buff[i],
3780 buff_size[i], PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06003781 if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
3782 c->SG[i].Addr.lower = 0;
3783 c->SG[i].Addr.upper = 0;
3784 c->SG[i].Len = 0;
3785 hpsa_pci_unmap(h->pdev, c, i,
3786 PCI_DMA_BIDIRECTIONAL);
3787 status = -ENOMEM;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05003788 goto cleanup0;
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06003789 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003790 c->SG[i].Addr.lower = temp64.val32.lower;
3791 c->SG[i].Addr.upper = temp64.val32.upper;
3792 c->SG[i].Len = buff_size[i];
Matt Gatese1d9cbf2014-02-18 13:55:12 -06003793 c->SG[i].Ext = i < sg_used - 1 ? 0 : HPSA_SG_LAST;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003794 }
3795 }
Stephen M. Camerona0c12412011-10-26 16:22:04 -05003796 hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003797 if (sg_used)
3798 hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003799 check_ioctl_unit_attention(h, c);
3800 /* Copy the error information out */
3801 memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
3802 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003803 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05003804 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003805 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06003806 if (ioc->Request.Type.Direction == XFER_READ && ioc->buf_size > 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003807 /* Copy the data out of the buffer we created */
3808 BYTE __user *ptr = ioc->buf;
3809 for (i = 0; i < sg_used; i++) {
3810 if (copy_to_user(ptr, buff[i], buff_size[i])) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003811 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05003812 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003813 }
3814 ptr += buff_size[i];
3815 }
3816 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003817 status = 0;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05003818cleanup0:
3819 cmd_special_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003820cleanup1:
3821 if (buff) {
3822 for (i = 0; i < sg_used; i++)
3823 kfree(buff[i]);
3824 kfree(buff);
3825 }
3826 kfree(buff_size);
3827 kfree(ioc);
3828 return status;
3829}
3830
3831static void check_ioctl_unit_attention(struct ctlr_info *h,
3832 struct CommandList *c)
3833{
3834 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
3835 c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
3836 (void) check_for_unit_attention(h, c);
3837}
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05003838
3839static int increment_passthru_count(struct ctlr_info *h)
3840{
3841 unsigned long flags;
3842
3843 spin_lock_irqsave(&h->passthru_count_lock, flags);
3844 if (h->passthru_count >= HPSA_MAX_CONCURRENT_PASSTHRUS) {
3845 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
3846 return -1;
3847 }
3848 h->passthru_count++;
3849 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
3850 return 0;
3851}
3852
3853static void decrement_passthru_count(struct ctlr_info *h)
3854{
3855 unsigned long flags;
3856
3857 spin_lock_irqsave(&h->passthru_count_lock, flags);
3858 if (h->passthru_count <= 0) {
3859 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
3860 /* not expecting to get here. */
3861 dev_warn(&h->pdev->dev, "Bug detected, passthru_count seems to be incorrect.\n");
3862 return;
3863 }
3864 h->passthru_count--;
3865 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
3866}
3867
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003868/*
3869 * ioctl
3870 */
3871static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
3872{
3873 struct ctlr_info *h;
3874 void __user *argp = (void __user *)arg;
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05003875 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003876
3877 h = sdev_to_hba(dev);
3878
3879 switch (cmd) {
3880 case CCISS_DEREGDISK:
3881 case CCISS_REGNEWDISK:
3882 case CCISS_REGNEWD:
Stephen M. Camerona08a8472010-02-04 08:43:16 -06003883 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003884 return 0;
3885 case CCISS_GETPCIINFO:
3886 return hpsa_getpciinfo_ioctl(h, argp);
3887 case CCISS_GETDRIVVER:
3888 return hpsa_getdrivver_ioctl(h, argp);
3889 case CCISS_PASSTHRU:
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05003890 if (increment_passthru_count(h))
3891 return -EAGAIN;
3892 rc = hpsa_passthru_ioctl(h, argp);
3893 decrement_passthru_count(h);
3894 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003895 case CCISS_BIG_PASSTHRU:
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05003896 if (increment_passthru_count(h))
3897 return -EAGAIN;
3898 rc = hpsa_big_passthru_ioctl(h, argp);
3899 decrement_passthru_count(h);
3900 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003901 default:
3902 return -ENOTTY;
3903 }
3904}
3905
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08003906static int hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
3907 u8 reset_type)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05003908{
3909 struct CommandList *c;
3910
3911 c = cmd_alloc(h);
3912 if (!c)
3913 return -ENOMEM;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003914 /* fill_cmd can't fail here, no data buffer to map */
3915 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05003916 RAID_CTLR_LUNID, TYPE_MSG);
3917 c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
3918 c->waiting = NULL;
3919 enqueue_cmd_and_start_io(h, c);
3920 /* Don't wait for completion, the reset won't complete. Don't free
3921 * the command either. This is the last command we will send before
3922 * re-initializing everything, so it doesn't matter and won't leak.
3923 */
3924 return 0;
3925}
3926
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003927static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06003928 void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003929 int cmd_type)
3930{
3931 int pci_dir = XFER_NONE;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05003932 struct CommandList *a; /* for commands to be aborted */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003933
3934 c->cmd_type = CMD_IOCTL_PEND;
3935 c->Header.ReplyQueue = 0;
3936 if (buff != NULL && size > 0) {
3937 c->Header.SGList = 1;
3938 c->Header.SGTotal = 1;
3939 } else {
3940 c->Header.SGList = 0;
3941 c->Header.SGTotal = 0;
3942 }
3943 c->Header.Tag.lower = c->busaddr;
3944 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
3945
3946 c->Request.Type.Type = cmd_type;
3947 if (cmd_type == TYPE_CMD) {
3948 switch (cmd) {
3949 case HPSA_INQUIRY:
3950 /* are we trying to read a vital product page */
3951 if (page_code != 0) {
3952 c->Request.CDB[1] = 0x01;
3953 c->Request.CDB[2] = page_code;
3954 }
3955 c->Request.CDBLen = 6;
3956 c->Request.Type.Attribute = ATTR_SIMPLE;
3957 c->Request.Type.Direction = XFER_READ;
3958 c->Request.Timeout = 0;
3959 c->Request.CDB[0] = HPSA_INQUIRY;
3960 c->Request.CDB[4] = size & 0xFF;
3961 break;
3962 case HPSA_REPORT_LOG:
3963 case HPSA_REPORT_PHYS:
3964 /* Talking to controller so It's a physical command
3965 mode = 00 target = 0. Nothing to write.
3966 */
3967 c->Request.CDBLen = 12;
3968 c->Request.Type.Attribute = ATTR_SIMPLE;
3969 c->Request.Type.Direction = XFER_READ;
3970 c->Request.Timeout = 0;
3971 c->Request.CDB[0] = cmd;
3972 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
3973 c->Request.CDB[7] = (size >> 16) & 0xFF;
3974 c->Request.CDB[8] = (size >> 8) & 0xFF;
3975 c->Request.CDB[9] = size & 0xFF;
3976 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003977 case HPSA_CACHE_FLUSH:
3978 c->Request.CDBLen = 12;
3979 c->Request.Type.Attribute = ATTR_SIMPLE;
3980 c->Request.Type.Direction = XFER_WRITE;
3981 c->Request.Timeout = 0;
3982 c->Request.CDB[0] = BMIC_WRITE;
3983 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
Stephen M. Cameronbb158ea2011-10-26 16:21:17 -05003984 c->Request.CDB[7] = (size >> 8) & 0xFF;
3985 c->Request.CDB[8] = size & 0xFF;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003986 break;
3987 case TEST_UNIT_READY:
3988 c->Request.CDBLen = 6;
3989 c->Request.Type.Attribute = ATTR_SIMPLE;
3990 c->Request.Type.Direction = XFER_NONE;
3991 c->Request.Timeout = 0;
3992 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003993 case HPSA_GET_RAID_MAP:
3994 c->Request.CDBLen = 12;
3995 c->Request.Type.Attribute = ATTR_SIMPLE;
3996 c->Request.Type.Direction = XFER_READ;
3997 c->Request.Timeout = 0;
3998 c->Request.CDB[0] = HPSA_CISS_READ;
3999 c->Request.CDB[1] = cmd;
4000 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
4001 c->Request.CDB[7] = (size >> 16) & 0xFF;
4002 c->Request.CDB[8] = (size >> 8) & 0xFF;
4003 c->Request.CDB[9] = size & 0xFF;
4004 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004005 default:
4006 dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
4007 BUG();
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004008 return -1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004009 }
4010 } else if (cmd_type == TYPE_MSG) {
4011 switch (cmd) {
4012
4013 case HPSA_DEVICE_RESET_MSG:
4014 c->Request.CDBLen = 16;
4015 c->Request.Type.Type = 1; /* It is a MSG not a CMD */
4016 c->Request.Type.Attribute = ATTR_SIMPLE;
4017 c->Request.Type.Direction = XFER_NONE;
4018 c->Request.Timeout = 0; /* Don't time out */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004019 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
4020 c->Request.CDB[0] = cmd;
Stephen M. Cameron21e89af2012-07-26 11:34:10 -05004021 c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004022 /* If bytes 4-7 are zero, it means reset the */
4023 /* LunID device */
4024 c->Request.CDB[4] = 0x00;
4025 c->Request.CDB[5] = 0x00;
4026 c->Request.CDB[6] = 0x00;
4027 c->Request.CDB[7] = 0x00;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004028 break;
4029 case HPSA_ABORT_MSG:
4030 a = buff; /* point to command to be aborted */
4031 dev_dbg(&h->pdev->dev, "Abort Tag:0x%08x:%08x using request Tag:0x%08x:%08x\n",
4032 a->Header.Tag.upper, a->Header.Tag.lower,
4033 c->Header.Tag.upper, c->Header.Tag.lower);
4034 c->Request.CDBLen = 16;
4035 c->Request.Type.Type = TYPE_MSG;
4036 c->Request.Type.Attribute = ATTR_SIMPLE;
4037 c->Request.Type.Direction = XFER_WRITE;
4038 c->Request.Timeout = 0; /* Don't time out */
4039 c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
4040 c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
4041 c->Request.CDB[2] = 0x00; /* reserved */
4042 c->Request.CDB[3] = 0x00; /* reserved */
4043 /* Tag to abort goes in CDB[4]-CDB[11] */
4044 c->Request.CDB[4] = a->Header.Tag.lower & 0xFF;
4045 c->Request.CDB[5] = (a->Header.Tag.lower >> 8) & 0xFF;
4046 c->Request.CDB[6] = (a->Header.Tag.lower >> 16) & 0xFF;
4047 c->Request.CDB[7] = (a->Header.Tag.lower >> 24) & 0xFF;
4048 c->Request.CDB[8] = a->Header.Tag.upper & 0xFF;
4049 c->Request.CDB[9] = (a->Header.Tag.upper >> 8) & 0xFF;
4050 c->Request.CDB[10] = (a->Header.Tag.upper >> 16) & 0xFF;
4051 c->Request.CDB[11] = (a->Header.Tag.upper >> 24) & 0xFF;
4052 c->Request.CDB[12] = 0x00; /* reserved */
4053 c->Request.CDB[13] = 0x00; /* reserved */
4054 c->Request.CDB[14] = 0x00; /* reserved */
4055 c->Request.CDB[15] = 0x00; /* reserved */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004056 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004057 default:
4058 dev_warn(&h->pdev->dev, "unknown message type %d\n",
4059 cmd);
4060 BUG();
4061 }
4062 } else {
4063 dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
4064 BUG();
4065 }
4066
4067 switch (c->Request.Type.Direction) {
4068 case XFER_READ:
4069 pci_dir = PCI_DMA_FROMDEVICE;
4070 break;
4071 case XFER_WRITE:
4072 pci_dir = PCI_DMA_TODEVICE;
4073 break;
4074 case XFER_NONE:
4075 pci_dir = PCI_DMA_NONE;
4076 break;
4077 default:
4078 pci_dir = PCI_DMA_BIDIRECTIONAL;
4079 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004080 if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
4081 return -1;
4082 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004083}
4084
4085/*
4086 * Map (physical) PCI mem into (virtual) kernel space
4087 */
4088static void __iomem *remap_pci_mem(ulong base, ulong size)
4089{
4090 ulong page_base = ((ulong) base) & PAGE_MASK;
4091 ulong page_offs = ((ulong) base) - page_base;
Stephen M. Cameron088ba342012-07-26 11:34:23 -05004092 void __iomem *page_remapped = ioremap_nocache(page_base,
4093 page_offs + size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004094
4095 return page_remapped ? (page_remapped + page_offs) : NULL;
4096}
4097
4098/* Takes cmds off the submission queue and sends them to the hardware,
4099 * then puts them on the queue of cmds waiting for completion.
4100 */
4101static void start_io(struct ctlr_info *h)
4102{
4103 struct CommandList *c;
Matt Gatese16a33a2012-05-01 11:43:11 -05004104 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004105
Matt Gatese16a33a2012-05-01 11:43:11 -05004106 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron9e0fc7642011-02-15 15:32:48 -06004107 while (!list_empty(&h->reqQ)) {
4108 c = list_entry(h->reqQ.next, struct CommandList, list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004109 /* can't do anything if fifo is full */
4110 if ((h->access.fifo_full(h))) {
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004111 h->fifo_recently_full = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004112 dev_warn(&h->pdev->dev, "fifo full\n");
4113 break;
4114 }
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004115 h->fifo_recently_full = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004116
4117 /* Get the first entry from the Request Q */
4118 removeQ(c);
4119 h->Qdepth--;
4120
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004121 /* Put job onto the completed Q */
4122 addQ(&h->cmpQ, c);
Matt Gatese16a33a2012-05-01 11:43:11 -05004123
4124 /* Must increment commands_outstanding before unlocking
4125 * and submitting to avoid race checking for fifo full
4126 * condition.
4127 */
4128 h->commands_outstanding++;
4129 if (h->commands_outstanding > h->max_outstanding)
4130 h->max_outstanding = h->commands_outstanding;
4131
4132 /* Tell the controller execute command */
4133 spin_unlock_irqrestore(&h->lock, flags);
4134 h->access.submit_command(h, c);
4135 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004136 }
Matt Gatese16a33a2012-05-01 11:43:11 -05004137 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004138}
4139
Matt Gates254f7962012-05-01 11:43:06 -05004140static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004141{
Matt Gates254f7962012-05-01 11:43:06 -05004142 return h->access.command_completed(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004143}
4144
Stephen M. Cameron900c5442010-02-04 08:42:35 -06004145static inline bool interrupt_pending(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004146{
4147 return h->access.intr_pending(h);
4148}
4149
4150static inline long interrupt_not_for_us(struct ctlr_info *h)
4151{
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004152 return (h->access.intr_pending(h) == 0) ||
4153 (h->interrupts_enabled == 0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004154}
4155
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004156static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
4157 u32 raw_tag)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004158{
4159 if (unlikely(tag_index >= h->nr_cmds)) {
4160 dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
4161 return 1;
4162 }
4163 return 0;
4164}
4165
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05004166static inline void finish_cmd(struct CommandList *c)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004167{
Matt Gatese16a33a2012-05-01 11:43:11 -05004168 unsigned long flags;
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004169 int io_may_be_stalled = 0;
4170 struct ctlr_info *h = c->h;
Matt Gatese16a33a2012-05-01 11:43:11 -05004171
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004172 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004173 removeQ(c);
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004174
4175 /*
4176 * Check for possibly stalled i/o.
4177 *
4178 * If a fifo_full condition is encountered, requests will back up
4179 * in h->reqQ. This queue is only emptied out by start_io which is
4180 * only called when a new i/o request comes in. If no i/o's are
4181 * forthcoming, the i/o's in h->reqQ can get stuck. So we call
4182 * start_io from here if we detect such a danger.
4183 *
4184 * Normally, we shouldn't hit this case, but pounding on the
4185 * CCISS_PASSTHRU ioctl can provoke it. Only call start_io if
4186 * commands_outstanding is low. We want to avoid calling
4187 * start_io from in here as much as possible, and esp. don't
4188 * want to get in a cycle where we call start_io every time
4189 * through here.
4190 */
4191 if (unlikely(h->fifo_recently_full) &&
4192 h->commands_outstanding < 5)
4193 io_may_be_stalled = 1;
4194
4195 spin_unlock_irqrestore(&h->lock, flags);
4196
Stephen M. Camerone85c5972012-05-01 11:43:42 -05004197 dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
Matt Gatese1f7de02014-02-18 13:55:17 -06004198 if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI))
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05004199 complete_scsi_command(c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004200 else if (c->cmd_type == CMD_IOCTL_PEND)
4201 complete(c->waiting);
Stephen M. Cameron396883e2013-09-23 13:34:17 -05004202 if (unlikely(io_may_be_stalled))
4203 start_io(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004204}
4205
Stephen M. Camerona104c992010-02-04 08:42:24 -06004206static inline u32 hpsa_tag_contains_index(u32 tag)
4207{
Stephen M. Camerona104c992010-02-04 08:42:24 -06004208 return tag & DIRECT_LOOKUP_BIT;
4209}
4210
4211static inline u32 hpsa_tag_to_index(u32 tag)
4212{
Stephen M. Camerona104c992010-02-04 08:42:24 -06004213 return tag >> DIRECT_LOOKUP_SHIFT;
4214}
4215
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004216
4217static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
Stephen M. Camerona104c992010-02-04 08:42:24 -06004218{
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004219#define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
4220#define HPSA_SIMPLE_ERROR_BITS 0x03
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06004221 if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004222 return tag & ~HPSA_SIMPLE_ERROR_BITS;
4223 return tag & ~HPSA_PERF_ERROR_BITS;
Stephen M. Camerona104c992010-02-04 08:42:24 -06004224}
4225
Don Brace303932f2010-02-04 08:42:40 -06004226/* process completion of an indexed ("direct lookup") command */
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004227static inline void process_indexed_cmd(struct ctlr_info *h,
Don Brace303932f2010-02-04 08:42:40 -06004228 u32 raw_tag)
4229{
4230 u32 tag_index;
4231 struct CommandList *c;
4232
4233 tag_index = hpsa_tag_to_index(raw_tag);
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004234 if (!bad_tag(h, tag_index, raw_tag)) {
4235 c = h->cmd_pool + tag_index;
4236 finish_cmd(c);
4237 }
Don Brace303932f2010-02-04 08:42:40 -06004238}
4239
4240/* process completion of a non-indexed command */
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004241static inline void process_nonindexed_cmd(struct ctlr_info *h,
Don Brace303932f2010-02-04 08:42:40 -06004242 u32 raw_tag)
4243{
4244 u32 tag;
4245 struct CommandList *c = NULL;
Matt Gatese16a33a2012-05-01 11:43:11 -05004246 unsigned long flags;
Don Brace303932f2010-02-04 08:42:40 -06004247
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004248 tag = hpsa_tag_discard_error_bits(h, raw_tag);
Matt Gatese16a33a2012-05-01 11:43:11 -05004249 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron9e0fc7642011-02-15 15:32:48 -06004250 list_for_each_entry(c, &h->cmpQ, list) {
Don Brace303932f2010-02-04 08:42:40 -06004251 if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
Matt Gatese16a33a2012-05-01 11:43:11 -05004252 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05004253 finish_cmd(c);
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004254 return;
Don Brace303932f2010-02-04 08:42:40 -06004255 }
4256 }
Matt Gatese16a33a2012-05-01 11:43:11 -05004257 spin_unlock_irqrestore(&h->lock, flags);
Don Brace303932f2010-02-04 08:42:40 -06004258 bad_tag(h, h->nr_cmds + 1, raw_tag);
Don Brace303932f2010-02-04 08:42:40 -06004259}
4260
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004261/* Some controllers, like p400, will give us one interrupt
4262 * after a soft reset, even if we turned interrupts off.
4263 * Only need to check for this in the hpsa_xxx_discard_completions
4264 * functions.
4265 */
4266static int ignore_bogus_interrupt(struct ctlr_info *h)
4267{
4268 if (likely(!reset_devices))
4269 return 0;
4270
4271 if (likely(h->interrupts_enabled))
4272 return 0;
4273
4274 dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
4275 "(known firmware bug.) Ignoring.\n");
4276
4277 return 1;
4278}
4279
Matt Gates254f7962012-05-01 11:43:06 -05004280/*
4281 * Convert &h->q[x] (passed to interrupt handlers) back to h.
4282 * Relies on (h-q[x] == x) being true for x such that
4283 * 0 <= x < MAX_REPLY_QUEUES.
4284 */
4285static struct ctlr_info *queue_to_hba(u8 *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004286{
Matt Gates254f7962012-05-01 11:43:06 -05004287 return container_of((queue - *queue), struct ctlr_info, q[0]);
4288}
4289
4290static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
4291{
4292 struct ctlr_info *h = queue_to_hba(queue);
4293 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004294 u32 raw_tag;
4295
4296 if (ignore_bogus_interrupt(h))
4297 return IRQ_NONE;
4298
4299 if (interrupt_not_for_us(h))
4300 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004301 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004302 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05004303 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004304 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05004305 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004306 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004307 return IRQ_HANDLED;
4308}
4309
Matt Gates254f7962012-05-01 11:43:06 -05004310static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004311{
Matt Gates254f7962012-05-01 11:43:06 -05004312 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004313 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05004314 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004315
4316 if (ignore_bogus_interrupt(h))
4317 return IRQ_NONE;
4318
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004319 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05004320 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004321 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05004322 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004323 return IRQ_HANDLED;
4324}
4325
Matt Gates254f7962012-05-01 11:43:06 -05004326static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004327{
Matt Gates254f7962012-05-01 11:43:06 -05004328 struct ctlr_info *h = queue_to_hba((u8 *) queue);
Don Brace303932f2010-02-04 08:42:40 -06004329 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05004330 u8 q = *(u8 *) queue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004331
4332 if (interrupt_not_for_us(h))
4333 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004334 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004335 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05004336 raw_tag = get_next_completion(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004337 while (raw_tag != FIFO_EMPTY) {
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004338 if (likely(hpsa_tag_contains_index(raw_tag)))
4339 process_indexed_cmd(h, raw_tag);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004340 else
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004341 process_nonindexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05004342 raw_tag = next_command(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004343 }
4344 }
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004345 return IRQ_HANDLED;
4346}
4347
Matt Gates254f7962012-05-01 11:43:06 -05004348static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004349{
Matt Gates254f7962012-05-01 11:43:06 -05004350 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004351 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05004352 u8 q = *(u8 *) queue;
Stephen M. Cameron10f66012010-06-16 13:51:50 -05004353
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004354 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05004355 raw_tag = get_next_completion(h, q);
Don Brace303932f2010-02-04 08:42:40 -06004356 while (raw_tag != FIFO_EMPTY) {
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004357 if (likely(hpsa_tag_contains_index(raw_tag)))
4358 process_indexed_cmd(h, raw_tag);
Don Brace303932f2010-02-04 08:42:40 -06004359 else
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05004360 process_nonindexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05004361 raw_tag = next_command(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004362 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004363 return IRQ_HANDLED;
4364}
4365
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004366/* Send a message CDB to the firmware. Careful, this only works
4367 * in simple mode, not performant mode due to the tag lookup.
4368 * We only ever use this immediately after a controller reset.
4369 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004370static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
4371 unsigned char type)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004372{
4373 struct Command {
4374 struct CommandListHeader CommandHeader;
4375 struct RequestBlock Request;
4376 struct ErrDescriptor ErrorDescriptor;
4377 };
4378 struct Command *cmd;
4379 static const size_t cmd_sz = sizeof(*cmd) +
4380 sizeof(cmd->ErrorDescriptor);
4381 dma_addr_t paddr64;
4382 uint32_t paddr32, tag;
4383 void __iomem *vaddr;
4384 int i, err;
4385
4386 vaddr = pci_ioremap_bar(pdev, 0);
4387 if (vaddr == NULL)
4388 return -ENOMEM;
4389
4390 /* The Inbound Post Queue only accepts 32-bit physical addresses for the
4391 * CCISS commands, so they must be allocated from the lower 4GiB of
4392 * memory.
4393 */
4394 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
4395 if (err) {
4396 iounmap(vaddr);
4397 return -ENOMEM;
4398 }
4399
4400 cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
4401 if (cmd == NULL) {
4402 iounmap(vaddr);
4403 return -ENOMEM;
4404 }
4405
4406 /* This must fit, because of the 32-bit consistent DMA mask. Also,
4407 * although there's no guarantee, we assume that the address is at
4408 * least 4-byte aligned (most likely, it's page-aligned).
4409 */
4410 paddr32 = paddr64;
4411
4412 cmd->CommandHeader.ReplyQueue = 0;
4413 cmd->CommandHeader.SGList = 0;
4414 cmd->CommandHeader.SGTotal = 0;
4415 cmd->CommandHeader.Tag.lower = paddr32;
4416 cmd->CommandHeader.Tag.upper = 0;
4417 memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
4418
4419 cmd->Request.CDBLen = 16;
4420 cmd->Request.Type.Type = TYPE_MSG;
4421 cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
4422 cmd->Request.Type.Direction = XFER_NONE;
4423 cmd->Request.Timeout = 0; /* Don't time out */
4424 cmd->Request.CDB[0] = opcode;
4425 cmd->Request.CDB[1] = type;
4426 memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
4427 cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
4428 cmd->ErrorDescriptor.Addr.upper = 0;
4429 cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
4430
4431 writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
4432
4433 for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
4434 tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004435 if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr32)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004436 break;
4437 msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
4438 }
4439
4440 iounmap(vaddr);
4441
4442 /* we leak the DMA buffer here ... no choice since the controller could
4443 * still complete the command.
4444 */
4445 if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
4446 dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
4447 opcode, type);
4448 return -ETIMEDOUT;
4449 }
4450
4451 pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
4452
4453 if (tag & HPSA_ERROR_BIT) {
4454 dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
4455 opcode, type);
4456 return -EIO;
4457 }
4458
4459 dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
4460 opcode, type);
4461 return 0;
4462}
4463
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004464#define hpsa_noop(p) hpsa_message(p, 3, 0)
4465
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004466static int hpsa_controller_hard_reset(struct pci_dev *pdev,
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05004467 void * __iomem vaddr, u32 use_doorbell)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004468{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004469 u16 pmcsr;
4470 int pos;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004471
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004472 if (use_doorbell) {
4473 /* For everything after the P600, the PCI power state method
4474 * of resetting the controller doesn't work, so we have this
4475 * other way using the doorbell register.
4476 */
4477 dev_info(&pdev->dev, "using doorbell to reset controller\n");
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05004478 writel(use_doorbell, vaddr + SA5_DOORBELL);
Stephen M. Cameron85009232013-09-23 13:33:36 -05004479
4480 /* PMC hardware guys tell us we need a 5 second delay after
4481 * doorbell reset and before any attempt to talk to the board
4482 * at all to ensure that this actually works and doesn't fall
4483 * over in some weird corner cases.
4484 */
4485 msleep(5000);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004486 } else { /* Try to do it the PCI power state way */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004487
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004488 /* Quoting from the Open CISS Specification: "The Power
4489 * Management Control/Status Register (CSR) controls the power
4490 * state of the device. The normal operating state is D0,
4491 * CSR=00h. The software off state is D3, CSR=03h. To reset
4492 * the controller, place the interface device in D3 then to D0,
4493 * this causes a secondary PCI reset which will reset the
4494 * controller." */
4495
4496 pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
4497 if (pos == 0) {
4498 dev_err(&pdev->dev,
4499 "hpsa_reset_controller: "
4500 "PCI PM not supported\n");
4501 return -ENODEV;
4502 }
4503 dev_info(&pdev->dev, "using PCI PM to reset controller\n");
4504 /* enter the D3hot power management state */
4505 pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
4506 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
4507 pmcsr |= PCI_D3hot;
4508 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
4509
4510 msleep(500);
4511
4512 /* enter the D0 power management state */
4513 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
4514 pmcsr |= PCI_D0;
4515 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
Mike Millerc4853ef2011-10-21 08:19:43 +02004516
4517 /*
4518 * The P600 requires a small delay when changing states.
4519 * Otherwise we may think the board did not reset and we bail.
4520 * This for kdump only and is particular to the P600.
4521 */
4522 msleep(500);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004523 }
4524 return 0;
4525}
4526
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004527static void init_driver_version(char *driver_version, int len)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004528{
4529 memset(driver_version, 0, len);
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06004530 strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004531}
4532
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004533static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004534{
4535 char *driver_version;
4536 int i, size = sizeof(cfgtable->driver_version);
4537
4538 driver_version = kmalloc(size, GFP_KERNEL);
4539 if (!driver_version)
4540 return -ENOMEM;
4541
4542 init_driver_version(driver_version, size);
4543 for (i = 0; i < size; i++)
4544 writeb(driver_version[i], &cfgtable->driver_version[i]);
4545 kfree(driver_version);
4546 return 0;
4547}
4548
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004549static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
4550 unsigned char *driver_ver)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004551{
4552 int i;
4553
4554 for (i = 0; i < sizeof(cfgtable->driver_version); i++)
4555 driver_ver[i] = readb(&cfgtable->driver_version[i]);
4556}
4557
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004558static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004559{
4560
4561 char *driver_ver, *old_driver_ver;
4562 int rc, size = sizeof(cfgtable->driver_version);
4563
4564 old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
4565 if (!old_driver_ver)
4566 return -ENOMEM;
4567 driver_ver = old_driver_ver + size;
4568
4569 /* After a reset, the 32 bytes of "driver version" in the cfgtable
4570 * should have been changed, otherwise we know the reset failed.
4571 */
4572 init_driver_version(old_driver_ver, size);
4573 read_driver_ver_from_cfgtable(cfgtable, driver_ver);
4574 rc = !memcmp(driver_ver, old_driver_ver, size);
4575 kfree(old_driver_ver);
4576 return rc;
4577}
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004578/* This does a hard reset of the controller using PCI power management
4579 * states or the using the doorbell register.
4580 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004581static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev)
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004582{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004583 u64 cfg_offset;
4584 u32 cfg_base_addr;
4585 u64 cfg_base_addr_index;
4586 void __iomem *vaddr;
4587 unsigned long paddr;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004588 u32 misc_fw_support;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06004589 int rc;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004590 struct CfgTable __iomem *cfgtable;
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05004591 u32 use_doorbell;
Stephen M. Cameron18867652010-06-16 13:51:45 -05004592 u32 board_id;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06004593 u16 command_register;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004594
4595 /* For controllers as old as the P600, this is very nearly
4596 * the same thing as
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004597 *
4598 * pci_save_state(pci_dev);
4599 * pci_set_power_state(pci_dev, PCI_D3hot);
4600 * pci_set_power_state(pci_dev, PCI_D0);
4601 * pci_restore_state(pci_dev);
4602 *
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004603 * For controllers newer than the P600, the pci power state
4604 * method of resetting doesn't work so we have another way
4605 * using the doorbell register.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004606 */
Stephen M. Cameron18867652010-06-16 13:51:45 -05004607
Stephen M. Cameron25c1e56a2011-01-06 14:48:18 -06004608 rc = hpsa_lookup_board_id(pdev, &board_id);
Stephen M. Cameron46380782011-05-03 15:00:01 -05004609 if (rc < 0 || !ctlr_is_resettable(board_id)) {
Stephen M. Cameron25c1e56a2011-01-06 14:48:18 -06004610 dev_warn(&pdev->dev, "Not resetting device.\n");
4611 return -ENODEV;
4612 }
Stephen M. Cameron46380782011-05-03 15:00:01 -05004613
4614 /* if controller is soft- but not hard resettable... */
4615 if (!ctlr_is_hard_resettable(board_id))
4616 return -ENOTSUPP; /* try soft reset later. */
Stephen M. Cameron18867652010-06-16 13:51:45 -05004617
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06004618 /* Save the PCI command register */
4619 pci_read_config_word(pdev, 4, &command_register);
4620 /* Turn the board off. This is so that later pci_restore_state()
4621 * won't turn the board on before the rest of config space is ready.
4622 */
4623 pci_disable_device(pdev);
4624 pci_save_state(pdev);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004625
4626 /* find the first memory BAR, so we can find the cfg table */
4627 rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
4628 if (rc)
4629 return rc;
4630 vaddr = remap_pci_mem(paddr, 0x250);
4631 if (!vaddr)
4632 return -ENOMEM;
4633
4634 /* find cfgtable in order to check if reset via doorbell is supported */
4635 rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
4636 &cfg_base_addr_index, &cfg_offset);
4637 if (rc)
4638 goto unmap_vaddr;
4639 cfgtable = remap_pci_mem(pci_resource_start(pdev,
4640 cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
4641 if (!cfgtable) {
4642 rc = -ENOMEM;
4643 goto unmap_vaddr;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004644 }
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004645 rc = write_driver_ver_to_cfgtable(cfgtable);
4646 if (rc)
4647 goto unmap_vaddr;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004648
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05004649 /* If reset via doorbell register is supported, use that.
4650 * There are two such methods. Favor the newest method.
4651 */
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004652 misc_fw_support = readl(&cfgtable->misc_fw_support);
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05004653 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
4654 if (use_doorbell) {
4655 use_doorbell = DOORBELL_CTLR_RESET2;
4656 } else {
4657 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
4658 if (use_doorbell) {
Mike Millerfba63092011-10-13 11:44:06 -05004659 dev_warn(&pdev->dev, "Soft reset not supported. "
4660 "Firmware update is required.\n");
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004661 rc = -ENOTSUPP; /* try soft reset */
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05004662 goto unmap_cfgtable;
4663 }
4664 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004665
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004666 rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
4667 if (rc)
4668 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004669
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06004670 pci_restore_state(pdev);
4671 rc = pci_enable_device(pdev);
4672 if (rc) {
4673 dev_warn(&pdev->dev, "failed to enable device.\n");
4674 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004675 }
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06004676 pci_write_config_word(pdev, 4, command_register);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004677
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004678 /* Some devices (notably the HP Smart Array 5i Controller)
4679 need a little pause here */
4680 msleep(HPSA_POST_RESET_PAUSE_MSECS);
4681
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004682 rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
4683 if (rc) {
4684 dev_warn(&pdev->dev,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004685 "failed waiting for board to become ready "
4686 "after hard reset\n");
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004687 goto unmap_cfgtable;
4688 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004689
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004690 rc = controller_reset_failed(vaddr);
4691 if (rc < 0)
4692 goto unmap_cfgtable;
4693 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004694 dev_warn(&pdev->dev, "Unable to successfully reset "
4695 "controller. Will try soft reset.\n");
4696 rc = -ENOTSUPP;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004697 } else {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004698 dev_info(&pdev->dev, "board ready after hard reset.\n");
Stephen M. Cameron1df85522010-06-16 13:51:40 -05004699 }
4700
4701unmap_cfgtable:
4702 iounmap(cfgtable);
4703
4704unmap_vaddr:
4705 iounmap(vaddr);
4706 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004707}
4708
4709/*
4710 * We cannot read the structure directly, for portability we must use
4711 * the io functions.
4712 * This is for debug only.
4713 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004714static void print_cfg_table(struct device *dev, struct CfgTable *tb)
4715{
Stephen M. Cameron58f86652010-05-27 15:13:58 -05004716#ifdef HPSA_DEBUG
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004717 int i;
4718 char temp_name[17];
4719
4720 dev_info(dev, "Controller Configuration information\n");
4721 dev_info(dev, "------------------------------------\n");
4722 for (i = 0; i < 4; i++)
4723 temp_name[i] = readb(&(tb->Signature[i]));
4724 temp_name[4] = '\0';
4725 dev_info(dev, " Signature = %s\n", temp_name);
4726 dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
4727 dev_info(dev, " Transport methods supported = 0x%x\n",
4728 readl(&(tb->TransportSupport)));
4729 dev_info(dev, " Transport methods active = 0x%x\n",
4730 readl(&(tb->TransportActive)));
4731 dev_info(dev, " Requested transport Method = 0x%x\n",
4732 readl(&(tb->HostWrite.TransportRequest)));
4733 dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
4734 readl(&(tb->HostWrite.CoalIntDelay)));
4735 dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
4736 readl(&(tb->HostWrite.CoalIntCount)));
4737 dev_info(dev, " Max outstanding commands = 0x%d\n",
4738 readl(&(tb->CmdsOutMax)));
4739 dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
4740 for (i = 0; i < 16; i++)
4741 temp_name[i] = readb(&(tb->ServerName[i]));
4742 temp_name[16] = '\0';
4743 dev_info(dev, " Server Name = %s\n", temp_name);
4744 dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
4745 readl(&(tb->HeartBeat)));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004746#endif /* HPSA_DEBUG */
Stephen M. Cameron58f86652010-05-27 15:13:58 -05004747}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004748
4749static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
4750{
4751 int i, offset, mem_type, bar_type;
4752
4753 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
4754 return 0;
4755 offset = 0;
4756 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
4757 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
4758 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
4759 offset += 4;
4760 else {
4761 mem_type = pci_resource_flags(pdev, i) &
4762 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
4763 switch (mem_type) {
4764 case PCI_BASE_ADDRESS_MEM_TYPE_32:
4765 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
4766 offset += 4; /* 32 bit */
4767 break;
4768 case PCI_BASE_ADDRESS_MEM_TYPE_64:
4769 offset += 8;
4770 break;
4771 default: /* reserved in PCI 2.2 */
4772 dev_warn(&pdev->dev,
4773 "base address is invalid\n");
4774 return -1;
4775 break;
4776 }
4777 }
4778 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
4779 return i + 1;
4780 }
4781 return -1;
4782}
4783
4784/* If MSI/MSI-X is supported by the kernel we will try to enable it on
4785 * controllers that are capable. If not, we use IO-APIC mode.
4786 */
4787
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004788static void hpsa_interrupt_mode(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004789{
4790#ifdef CONFIG_PCI_MSI
Matt Gates254f7962012-05-01 11:43:06 -05004791 int err, i;
4792 struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
4793
4794 for (i = 0; i < MAX_REPLY_QUEUES; i++) {
4795 hpsa_msix_entries[i].vector = 0;
4796 hpsa_msix_entries[i].entry = i;
4797 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004798
4799 /* Some boards advertise MSI but don't really support it */
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05004800 if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
4801 (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004802 goto default_int_mode;
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05004803 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
4804 dev_info(&h->pdev->dev, "MSIX\n");
Hannes Reineckeeee0f032014-01-15 13:30:53 +01004805 h->msix_vector = MAX_REPLY_QUEUES;
Matt Gates254f7962012-05-01 11:43:06 -05004806 err = pci_enable_msix(h->pdev, hpsa_msix_entries,
Hannes Reineckeeee0f032014-01-15 13:30:53 +01004807 h->msix_vector);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004808 if (err > 0) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05004809 dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004810 "available\n", err);
Hannes Reineckeeee0f032014-01-15 13:30:53 +01004811 h->msix_vector = err;
4812 err = pci_enable_msix(h->pdev, hpsa_msix_entries,
4813 h->msix_vector);
4814 }
4815 if (!err) {
4816 for (i = 0; i < h->msix_vector; i++)
4817 h->intr[i] = hpsa_msix_entries[i].vector;
4818 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004819 } else {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05004820 dev_warn(&h->pdev->dev, "MSI-X init failed %d\n",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004821 err);
Hannes Reineckeeee0f032014-01-15 13:30:53 +01004822 h->msix_vector = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004823 goto default_int_mode;
4824 }
4825 }
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05004826 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
4827 dev_info(&h->pdev->dev, "MSI\n");
4828 if (!pci_enable_msi(h->pdev))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004829 h->msi_vector = 1;
4830 else
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05004831 dev_warn(&h->pdev->dev, "MSI init failed\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004832 }
4833default_int_mode:
4834#endif /* CONFIG_PCI_MSI */
4835 /* if we get here we're going to use the default interrupt mode */
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06004836 h->intr[h->intr_mode] = h->pdev->irq;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004837}
4838
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004839static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05004840{
4841 int i;
4842 u32 subsystem_vendor_id, subsystem_device_id;
4843
4844 subsystem_vendor_id = pdev->subsystem_vendor;
4845 subsystem_device_id = pdev->subsystem_device;
4846 *board_id = ((subsystem_device_id << 16) & 0xffff0000) |
4847 subsystem_vendor_id;
4848
4849 for (i = 0; i < ARRAY_SIZE(products); i++)
4850 if (*board_id == products[i].board_id)
4851 return i;
4852
Stephen M. Cameron6798cc02010-06-16 13:51:20 -05004853 if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
4854 subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
4855 !hpsa_allow_any) {
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05004856 dev_warn(&pdev->dev, "unrecognized board ID: "
4857 "0x%08x, ignoring.\n", *board_id);
4858 return -ENODEV;
4859 }
4860 return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
4861}
4862
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004863static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
4864 unsigned long *memory_bar)
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05004865{
4866 int i;
4867
4868 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05004869 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05004870 /* addressing mode bits already removed */
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05004871 *memory_bar = pci_resource_start(pdev, i);
4872 dev_dbg(&pdev->dev, "memory BAR = %lx\n",
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05004873 *memory_bar);
4874 return 0;
4875 }
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05004876 dev_warn(&pdev->dev, "no memory BAR found\n");
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05004877 return -ENODEV;
4878}
4879
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004880static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
4881 int wait_for_ready)
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05004882{
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004883 int i, iterations;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05004884 u32 scratchpad;
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004885 if (wait_for_ready)
4886 iterations = HPSA_BOARD_READY_ITERATIONS;
4887 else
4888 iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05004889
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004890 for (i = 0; i < iterations; i++) {
4891 scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
4892 if (wait_for_ready) {
4893 if (scratchpad == HPSA_FIRMWARE_READY)
4894 return 0;
4895 } else {
4896 if (scratchpad != HPSA_FIRMWARE_READY)
4897 return 0;
4898 }
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05004899 msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
4900 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06004901 dev_warn(&pdev->dev, "board not ready, timed out.\n");
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05004902 return -ENODEV;
4903}
4904
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004905static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
4906 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
4907 u64 *cfg_offset)
Stephen M. Camerona51fd472010-06-16 13:51:30 -05004908{
4909 *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
4910 *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
4911 *cfg_base_addr &= (u32) 0x0000ffff;
4912 *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
4913 if (*cfg_base_addr_index == -1) {
4914 dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
4915 return -ENODEV;
4916 }
4917 return 0;
4918}
4919
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004920static int hpsa_find_cfgtables(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004921{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004922 u64 cfg_offset;
4923 u32 cfg_base_addr;
4924 u64 cfg_base_addr_index;
Don Brace303932f2010-02-04 08:42:40 -06004925 u32 trans_offset;
Stephen M. Camerona51fd472010-06-16 13:51:30 -05004926 int rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05004927
Stephen M. Camerona51fd472010-06-16 13:51:30 -05004928 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
4929 &cfg_base_addr_index, &cfg_offset);
4930 if (rc)
4931 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05004932 h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
Stephen M. Camerona51fd472010-06-16 13:51:30 -05004933 cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
Stephen M. Cameron77c44952010-05-27 15:13:17 -05004934 if (!h->cfgtable)
4935 return -ENOMEM;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05004936 rc = write_driver_ver_to_cfgtable(h->cfgtable);
4937 if (rc)
4938 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05004939 /* Find performant mode table. */
Stephen M. Camerona51fd472010-06-16 13:51:30 -05004940 trans_offset = readl(&h->cfgtable->TransMethodOffset);
Stephen M. Cameron77c44952010-05-27 15:13:17 -05004941 h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
4942 cfg_base_addr_index)+cfg_offset+trans_offset,
4943 sizeof(*h->transtable));
4944 if (!h->transtable)
4945 return -ENOMEM;
4946 return 0;
4947}
4948
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004949static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05004950{
4951 h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
Stephen M. Cameron72ceeae2011-01-06 14:48:13 -06004952
4953 /* Limit commands in memory limited kdump scenario. */
4954 if (reset_devices && h->max_commands > 32)
4955 h->max_commands = 32;
4956
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05004957 if (h->max_commands < 16) {
4958 dev_warn(&h->pdev->dev, "Controller reports "
4959 "max supported commands of %d, an obvious lie. "
4960 "Using 16. Ensure that firmware is up to date.\n",
4961 h->max_commands);
4962 h->max_commands = 16;
4963 }
4964}
4965
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05004966/* Interrogate the hardware for some limits:
4967 * max commands, max SG elements without chaining, and with chaining,
4968 * SG chain block size, etc.
4969 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004970static void hpsa_find_board_params(struct ctlr_info *h)
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05004971{
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05004972 hpsa_get_max_perf_mode_cmds(h);
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05004973 h->nr_cmds = h->max_commands - 4; /* Allow room for some ioctls */
4974 h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004975 h->fw_support = readl(&(h->cfgtable->misc_fw_support));
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05004976 /*
4977 * Limit in-command s/g elements to 32 save dma'able memory.
4978 * Howvever spec says if 0, use 31
4979 */
4980 h->max_cmd_sg_entries = 31;
4981 if (h->maxsgentries > 512) {
4982 h->max_cmd_sg_entries = 32;
4983 h->chainsize = h->maxsgentries - h->max_cmd_sg_entries + 1;
4984 h->maxsgentries--; /* save one for chain pointer */
4985 } else {
4986 h->maxsgentries = 31; /* default to traditional values */
4987 h->chainsize = 0;
4988 }
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004989
4990 /* Find out what task management functions are supported and cache */
4991 h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05004992}
4993
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05004994static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
4995{
Akinobu Mita0fc9fd42012-04-04 22:14:59 +09004996 if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05004997 dev_warn(&h->pdev->dev, "not a valid CISS config table\n");
4998 return false;
4999 }
5000 return true;
5001}
5002
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005003static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005004{
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005005 u32 driver_support;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005006
Stephen M. Cameron28e13442013-12-04 17:10:21 -06005007#ifdef CONFIG_X86
5008 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005009 driver_support = readl(&(h->cfgtable->driver_support));
5010 driver_support |= ENABLE_SCSI_PREFETCH;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005011#endif
Stephen M. Cameron28e13442013-12-04 17:10:21 -06005012 driver_support |= ENABLE_UNIT_ATTN;
5013 writel(driver_support, &(h->cfgtable->driver_support));
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05005014}
5015
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05005016/* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result
5017 * in a prefetch beyond physical memory.
5018 */
5019static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
5020{
5021 u32 dma_prefetch;
5022
5023 if (h->board_id != 0x3225103C)
5024 return;
5025 dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
5026 dma_prefetch |= 0x8000;
5027 writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
5028}
5029
Stephen M. Cameron76438d02014-02-18 13:55:43 -06005030static void hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
5031{
5032 int i;
5033 u32 doorbell_value;
5034 unsigned long flags;
5035 /* wait until the clear_event_notify bit 6 is cleared by controller. */
5036 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
5037 spin_lock_irqsave(&h->lock, flags);
5038 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
5039 spin_unlock_irqrestore(&h->lock, flags);
5040 if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
5041 break;
5042 /* delay and try again */
5043 msleep(20);
5044 }
5045}
5046
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005047static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005048{
5049 int i;
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06005050 u32 doorbell_value;
5051 unsigned long flags;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005052
5053 /* under certain very rare conditions, this can take awhile.
5054 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
5055 * as we enter this code.)
5056 */
5057 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06005058 spin_lock_irqsave(&h->lock, flags);
5059 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
5060 spin_unlock_irqrestore(&h->lock, flags);
Dan Carpenter382be662011-02-15 15:33:13 -06005061 if (!(doorbell_value & CFGTBL_ChangeReq))
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005062 break;
5063 /* delay and try again */
Stephen M. Cameron60d3f5b2011-01-06 14:48:34 -06005064 usleep_range(10000, 20000);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005065 }
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005066}
5067
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005068static int hpsa_enter_simple_mode(struct ctlr_info *h)
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005069{
5070 u32 trans_support;
5071
5072 trans_support = readl(&(h->cfgtable->TransportSupport));
5073 if (!(trans_support & SIMPLE_MODE))
5074 return -ENOTSUPP;
5075
5076 h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005077
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005078 /* Update the field, and then ring the doorbell */
5079 writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
5080 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
5081 hpsa_wait_for_mode_change_ack(h);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005082 print_cfg_table(&h->pdev->dev, h->cfgtable);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005083 if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
5084 goto error;
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06005085 h->transMethod = CFGTBL_Trans_Simple;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005086 return 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005087error:
5088 dev_warn(&h->pdev->dev, "unable to get board into simple mode\n");
5089 return -ENODEV;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005090}
5091
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005092static int hpsa_pci_init(struct ctlr_info *h)
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005093{
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005094 int prod_index, err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005095
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05005096 prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
5097 if (prod_index < 0)
5098 return -ENODEV;
5099 h->product_name = products[prod_index].product_name;
5100 h->access = *(products[prod_index].access);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005101
Matthew Garrette5a44df2011-11-11 11:14:23 -05005102 pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
5103 PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
5104
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005105 err = pci_enable_device(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005106 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005107 dev_warn(&h->pdev->dev, "unable to enable PCI device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005108 return err;
5109 }
5110
Stephen M. Cameron5cb460a2012-05-01 11:42:20 -05005111 /* Enable bus mastering (pci_disable_device may disable this) */
5112 pci_set_master(h->pdev);
5113
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06005114 err = pci_request_regions(h->pdev, HPSA);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005115 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005116 dev_err(&h->pdev->dev,
5117 "cannot obtain PCI resources, aborting\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005118 return err;
5119 }
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05005120 hpsa_interrupt_mode(h);
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05005121 err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005122 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005123 goto err_out_free_res;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005124 h->vaddr = remap_pci_mem(h->paddr, 0x250);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05005125 if (!h->vaddr) {
5126 err = -ENOMEM;
5127 goto err_out_free_res;
5128 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005129 err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005130 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005131 goto err_out_free_res;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005132 err = hpsa_find_cfgtables(h);
5133 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005134 goto err_out_free_res;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005135 hpsa_find_board_params(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005136
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05005137 if (!hpsa_CISS_signature_present(h)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005138 err = -ENODEV;
5139 goto err_out_free_res;
5140 }
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06005141 hpsa_set_driver_support_bits(h);
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05005142 hpsa_p600_dma_prefetch_quirk(h);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05005143 err = hpsa_enter_simple_mode(h);
5144 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005145 goto err_out_free_res;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005146 return 0;
5147
5148err_out_free_res:
Stephen M. Cameron204892e2010-05-27 15:13:22 -05005149 if (h->transtable)
5150 iounmap(h->transtable);
5151 if (h->cfgtable)
5152 iounmap(h->cfgtable);
5153 if (h->vaddr)
5154 iounmap(h->vaddr);
Stephen M. Cameronf0bd0b682012-05-01 11:42:09 -05005155 pci_disable_device(h->pdev);
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005156 pci_release_regions(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005157 return err;
5158}
5159
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005160static void hpsa_hba_inquiry(struct ctlr_info *h)
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06005161{
5162 int rc;
5163
5164#define HBA_INQUIRY_BYTE_COUNT 64
5165 h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
5166 if (!h->hba_inquiry_data)
5167 return;
5168 rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
5169 h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
5170 if (rc != 0) {
5171 kfree(h->hba_inquiry_data);
5172 h->hba_inquiry_data = NULL;
5173 }
5174}
5175
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005176static int hpsa_init_reset_devices(struct pci_dev *pdev)
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005177{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005178 int rc, i;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005179
5180 if (!reset_devices)
5181 return 0;
5182
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005183 /* Reset the controller with a PCI power-cycle or via doorbell */
5184 rc = hpsa_kdump_hard_reset_controller(pdev);
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005185
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005186 /* -ENOTSUPP here means we cannot reset the controller
5187 * but it's already (and still) up and running in
Stephen M. Cameron18867652010-06-16 13:51:45 -05005188 * "performant mode". Or, it might be 640x, which can't reset
5189 * due to concerns about shared bbwc between 6402/6404 pair.
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005190 */
5191 if (rc == -ENOTSUPP)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005192 return rc; /* just try to do the kdump anyhow. */
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005193 if (rc)
5194 return -ENODEV;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005195
5196 /* Now try to get the controller to respond to a no-op */
Stephen M. Cameron2b870cb2011-05-03 14:59:36 -05005197 dev_warn(&pdev->dev, "Waiting for controller to respond to no-op\n");
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005198 for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
5199 if (hpsa_noop(pdev) == 0)
5200 break;
5201 else
5202 dev_warn(&pdev->dev, "no-op failed%s\n",
5203 (i < 11 ? "; re-trying" : ""));
5204 }
5205 return 0;
5206}
5207
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005208static int hpsa_allocate_cmd_pool(struct ctlr_info *h)
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05005209{
5210 h->cmd_pool_bits = kzalloc(
5211 DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
5212 sizeof(unsigned long), GFP_KERNEL);
5213 h->cmd_pool = pci_alloc_consistent(h->pdev,
5214 h->nr_cmds * sizeof(*h->cmd_pool),
5215 &(h->cmd_pool_dhandle));
5216 h->errinfo_pool = pci_alloc_consistent(h->pdev,
5217 h->nr_cmds * sizeof(*h->errinfo_pool),
5218 &(h->errinfo_pool_dhandle));
5219 if ((h->cmd_pool_bits == NULL)
5220 || (h->cmd_pool == NULL)
5221 || (h->errinfo_pool == NULL)) {
5222 dev_err(&h->pdev->dev, "out of memory in %s", __func__);
5223 return -ENOMEM;
5224 }
5225 return 0;
5226}
5227
5228static void hpsa_free_cmd_pool(struct ctlr_info *h)
5229{
5230 kfree(h->cmd_pool_bits);
5231 if (h->cmd_pool)
5232 pci_free_consistent(h->pdev,
5233 h->nr_cmds * sizeof(struct CommandList),
5234 h->cmd_pool, h->cmd_pool_dhandle);
5235 if (h->errinfo_pool)
5236 pci_free_consistent(h->pdev,
5237 h->nr_cmds * sizeof(struct ErrorInfo),
5238 h->errinfo_pool,
5239 h->errinfo_pool_dhandle);
Matt Gatese1f7de02014-02-18 13:55:17 -06005240 if (h->ioaccel_cmd_pool)
5241 pci_free_consistent(h->pdev,
5242 h->nr_cmds * sizeof(struct io_accel1_cmd),
5243 h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05005244}
5245
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05005246static int hpsa_request_irq(struct ctlr_info *h,
5247 irqreturn_t (*msixhandler)(int, void *),
5248 irqreturn_t (*intxhandler)(int, void *))
5249{
Matt Gates254f7962012-05-01 11:43:06 -05005250 int rc, i;
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05005251
Matt Gates254f7962012-05-01 11:43:06 -05005252 /*
5253 * initialize h->q[x] = x so that interrupt handlers know which
5254 * queue to process.
5255 */
5256 for (i = 0; i < MAX_REPLY_QUEUES; i++)
5257 h->q[i] = (u8) i;
5258
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005259 if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
Matt Gates254f7962012-05-01 11:43:06 -05005260 /* If performant mode and MSI-X, use multiple reply queues */
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005261 for (i = 0; i < h->msix_vector; i++)
Matt Gates254f7962012-05-01 11:43:06 -05005262 rc = request_irq(h->intr[i], msixhandler,
5263 0, h->devname,
5264 &h->q[i]);
5265 } else {
5266 /* Use single reply pool */
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005267 if (h->msix_vector > 0 || h->msi_vector) {
Matt Gates254f7962012-05-01 11:43:06 -05005268 rc = request_irq(h->intr[h->intr_mode],
5269 msixhandler, 0, h->devname,
5270 &h->q[h->intr_mode]);
5271 } else {
5272 rc = request_irq(h->intr[h->intr_mode],
5273 intxhandler, IRQF_SHARED, h->devname,
5274 &h->q[h->intr_mode]);
5275 }
5276 }
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05005277 if (rc) {
5278 dev_err(&h->pdev->dev, "unable to get irq %d for %s\n",
5279 h->intr[h->intr_mode], h->devname);
5280 return -ENODEV;
5281 }
5282 return 0;
5283}
5284
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005285static int hpsa_kdump_soft_reset(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005286{
5287 if (hpsa_send_host_reset(h, RAID_CTLR_LUNID,
5288 HPSA_RESET_TYPE_CONTROLLER)) {
5289 dev_warn(&h->pdev->dev, "Resetting array controller failed.\n");
5290 return -EIO;
5291 }
5292
5293 dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
5294 if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) {
5295 dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
5296 return -1;
5297 }
5298
5299 dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
5300 if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) {
5301 dev_warn(&h->pdev->dev, "Board failed to become ready "
5302 "after soft reset.\n");
5303 return -1;
5304 }
5305
5306 return 0;
5307}
5308
Matt Gates254f7962012-05-01 11:43:06 -05005309static void free_irqs(struct ctlr_info *h)
5310{
5311 int i;
5312
5313 if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
5314 /* Single reply queue, only one irq to free */
5315 i = h->intr_mode;
5316 free_irq(h->intr[i], &h->q[i]);
5317 return;
5318 }
5319
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005320 for (i = 0; i < h->msix_vector; i++)
Matt Gates254f7962012-05-01 11:43:06 -05005321 free_irq(h->intr[i], &h->q[i]);
5322}
5323
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05005324static void hpsa_free_irqs_and_disable_msix(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005325{
Matt Gates254f7962012-05-01 11:43:06 -05005326 free_irqs(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005327#ifdef CONFIG_PCI_MSI
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05005328 if (h->msix_vector) {
5329 if (h->pdev->msix_enabled)
5330 pci_disable_msix(h->pdev);
5331 } else if (h->msi_vector) {
5332 if (h->pdev->msi_enabled)
5333 pci_disable_msi(h->pdev);
5334 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005335#endif /* CONFIG_PCI_MSI */
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05005336}
5337
5338static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
5339{
5340 hpsa_free_irqs_and_disable_msix(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005341 hpsa_free_sg_chain_blocks(h);
5342 hpsa_free_cmd_pool(h);
Matt Gatese1f7de02014-02-18 13:55:17 -06005343 kfree(h->ioaccel1_blockFetchTable);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005344 kfree(h->blockFetchTable);
5345 pci_free_consistent(h->pdev, h->reply_pool_size,
5346 h->reply_pool, h->reply_pool_dhandle);
5347 if (h->vaddr)
5348 iounmap(h->vaddr);
5349 if (h->transtable)
5350 iounmap(h->transtable);
5351 if (h->cfgtable)
5352 iounmap(h->cfgtable);
5353 pci_release_regions(h->pdev);
5354 kfree(h);
5355}
5356
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005357/* Called when controller lockup detected. */
5358static void fail_all_cmds_on_list(struct ctlr_info *h, struct list_head *list)
5359{
5360 struct CommandList *c = NULL;
5361
5362 assert_spin_locked(&h->lock);
5363 /* Mark all outstanding commands as failed and complete them. */
5364 while (!list_empty(list)) {
5365 c = list_entry(list->next, struct CommandList, list);
5366 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05005367 finish_cmd(c);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005368 }
5369}
5370
5371static void controller_lockup_detected(struct ctlr_info *h)
5372{
5373 unsigned long flags;
5374
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005375 h->access.set_intr_mask(h, HPSA_INTR_OFF);
5376 spin_lock_irqsave(&h->lock, flags);
5377 h->lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
5378 spin_unlock_irqrestore(&h->lock, flags);
5379 dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x\n",
5380 h->lockup_detected);
5381 pci_disable_device(h->pdev);
5382 spin_lock_irqsave(&h->lock, flags);
5383 fail_all_cmds_on_list(h, &h->cmpQ);
5384 fail_all_cmds_on_list(h, &h->reqQ);
5385 spin_unlock_irqrestore(&h->lock, flags);
5386}
5387
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005388static void detect_controller_lockup(struct ctlr_info *h)
5389{
5390 u64 now;
5391 u32 heartbeat;
5392 unsigned long flags;
5393
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005394 now = get_jiffies_64();
5395 /* If we've received an interrupt recently, we're ok. */
5396 if (time_after64(h->last_intr_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05005397 (h->heartbeat_sample_interval), now))
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005398 return;
5399
5400 /*
5401 * If we've already checked the heartbeat recently, we're ok.
5402 * This could happen if someone sends us a signal. We
5403 * otherwise don't care about signals in this thread.
5404 */
5405 if (time_after64(h->last_heartbeat_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05005406 (h->heartbeat_sample_interval), now))
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005407 return;
5408
5409 /* If heartbeat has not changed since we last looked, we're not ok. */
5410 spin_lock_irqsave(&h->lock, flags);
5411 heartbeat = readl(&h->cfgtable->HeartBeat);
5412 spin_unlock_irqrestore(&h->lock, flags);
5413 if (h->last_heartbeat == heartbeat) {
5414 controller_lockup_detected(h);
5415 return;
5416 }
5417
5418 /* We're ok. */
5419 h->last_heartbeat = heartbeat;
5420 h->last_heartbeat_timestamp = now;
5421}
5422
Stephen M. Cameron76438d02014-02-18 13:55:43 -06005423static int hpsa_kickoff_rescan(struct ctlr_info *h)
5424{
5425 int i;
5426 char *event_type;
5427
5428 /* Ask the controller to clear the events we're handling. */
5429 if (h->transMethod & (CFGTBL_Trans_io_accel1) &&
5430 (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
5431 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
5432
5433 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
5434 event_type = "state change";
5435 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
5436 event_type = "configuration change";
5437 /* Stop sending new RAID offload reqs via the IO accelerator */
5438 scsi_block_requests(h->scsi_host);
5439 for (i = 0; i < h->ndevices; i++)
5440 h->dev[i]->offload_enabled = 0;
5441 hpsa_drain_commands(h);
5442 /* Set 'accelerator path config change' bit */
5443 dev_warn(&h->pdev->dev,
5444 "Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
5445 h->events, event_type);
5446 writel(h->events, &(h->cfgtable->clear_event_notify));
5447 /* Set the "clear event notify field update" bit 6 */
5448 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
5449 /* Wait until ctlr clears 'clear event notify field', bit 6 */
5450 hpsa_wait_for_clear_event_notify_ack(h);
5451 scsi_unblock_requests(h->scsi_host);
5452 } else {
5453 /* Acknowledge controller notification events. */
5454 writel(h->events, &(h->cfgtable->clear_event_notify));
5455 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
5456 hpsa_wait_for_clear_event_notify_ack(h);
5457#if 0
5458 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
5459 hpsa_wait_for_mode_change_ack(h);
5460#endif
5461 }
5462
5463 /* Something in the device list may have changed to trigger
5464 * the event, so do a rescan.
5465 */
5466 hpsa_scan_start(h->scsi_host);
5467 /* release reference taken on scsi host in check_controller_events */
5468 scsi_host_put(h->scsi_host);
5469 return 0;
5470}
5471
5472/* Check a register on the controller to see if there are configuration
5473 * changes (added/changed/removed logical drives, etc.) which mean that
5474 * we should rescan the controller for devices. If so, add the controller
5475 * to the list of controllers needing to be rescanned, and gets a
5476 * reference to the associated scsi_host.
5477 */
5478static void hpsa_ctlr_needs_rescan(struct ctlr_info *h)
5479{
5480 if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
5481 return;
5482
5483 h->events = readl(&(h->cfgtable->event_notify));
5484 if (!h->events)
5485 return;
5486
5487 /*
5488 * Take a reference on scsi host for the duration of the scan
5489 * Release in hpsa_kickoff_rescan(). No lock needed for scan_list
5490 * as only a single thread accesses this list.
5491 */
5492 scsi_host_get(h->scsi_host);
5493 hpsa_kickoff_rescan(h);
5494}
5495
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005496static void hpsa_monitor_ctlr_worker(struct work_struct *work)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005497{
5498 unsigned long flags;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005499 struct ctlr_info *h = container_of(to_delayed_work(work),
5500 struct ctlr_info, monitor_ctlr_work);
5501 detect_controller_lockup(h);
5502 if (h->lockup_detected)
5503 return;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06005504 hpsa_ctlr_needs_rescan(h);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005505 spin_lock_irqsave(&h->lock, flags);
5506 if (h->remove_in_progress) {
5507 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005508 return;
5509 }
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005510 schedule_delayed_work(&h->monitor_ctlr_work,
5511 h->heartbeat_sample_interval);
5512 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005513}
5514
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005515static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005516{
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005517 int dac, rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005518 struct ctlr_info *h;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005519 int try_soft_reset = 0;
5520 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005521
5522 if (number_of_controllers == 0)
5523 printk(KERN_INFO DRIVER_NAME "\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005524
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05005525 rc = hpsa_init_reset_devices(pdev);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005526 if (rc) {
5527 if (rc != -ENOTSUPP)
5528 return rc;
5529 /* If the reset fails in a particular way (it has no way to do
5530 * a proper hard reset, so returns -ENOTSUPP) we can try to do
5531 * a soft reset once we get the controller configured up to the
5532 * point that it can accept a command.
5533 */
5534 try_soft_reset = 1;
5535 rc = 0;
5536 }
5537
5538reinit_after_soft_reset:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005539
Don Brace303932f2010-02-04 08:42:40 -06005540 /* Command structures must be aligned on a 32-byte boundary because
5541 * the 5 lower bits of the address are used by the hardware. and by
5542 * the driver. See comments in hpsa.h for more info.
5543 */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005544#define COMMANDLIST_ALIGNMENT 128
Don Brace303932f2010-02-04 08:42:40 -06005545 BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005546 h = kzalloc(sizeof(*h), GFP_KERNEL);
5547 if (!h)
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06005548 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005549
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005550 h->pdev = pdev;
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005551 h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
Stephen M. Cameron9e0fc7642011-02-15 15:32:48 -06005552 INIT_LIST_HEAD(&h->cmpQ);
5553 INIT_LIST_HEAD(&h->reqQ);
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06005554 spin_lock_init(&h->lock);
5555 spin_lock_init(&h->scan_lock);
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05005556 spin_lock_init(&h->passthru_count_lock);
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005557 rc = hpsa_pci_init(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06005558 if (rc != 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005559 goto clean1;
5560
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06005561 sprintf(h->devname, HPSA "%d", number_of_controllers);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005562 h->ctlr = number_of_controllers;
5563 number_of_controllers++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005564
5565 /* configure PCI DMA stuff */
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06005566 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
5567 if (rc == 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005568 dac = 1;
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06005569 } else {
5570 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
5571 if (rc == 0) {
5572 dac = 0;
5573 } else {
5574 dev_err(&pdev->dev, "no suitable DMA available\n");
5575 goto clean1;
5576 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005577 }
5578
5579 /* make sure the board interrupts are off */
5580 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005581
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05005582 if (hpsa_request_irq(h, do_hpsa_intr_msi, do_hpsa_intr_intx))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005583 goto clean2;
Don Brace303932f2010-02-04 08:42:40 -06005584 dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
5585 h->devname, pdev->device,
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005586 h->intr[h->intr_mode], dac ? "" : " not");
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05005587 if (hpsa_allocate_cmd_pool(h))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005588 goto clean4;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06005589 if (hpsa_allocate_sg_chain_blocks(h))
5590 goto clean4;
Stephen M. Camerona08a8472010-02-04 08:43:16 -06005591 init_waitqueue_head(&h->scan_wait_queue);
5592 h->scan_finished = 1; /* no scan currently in progress */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005593
5594 pci_set_drvdata(pdev, h);
Stephen M. Cameron9a413382011-05-03 14:59:41 -05005595 h->ndevices = 0;
5596 h->scsi_host = NULL;
5597 spin_lock_init(&h->devlock);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005598 hpsa_put_ctlr_into_performant_mode(h);
5599
5600 /* At this point, the controller is ready to take commands.
5601 * Now, if reset_devices and the hard reset didn't work, try
5602 * the soft reset and see if that works.
5603 */
5604 if (try_soft_reset) {
5605
5606 /* This is kind of gross. We may or may not get a completion
5607 * from the soft reset command, and if we do, then the value
5608 * from the fifo may or may not be valid. So, we wait 10 secs
5609 * after the reset throwing away any completions we get during
5610 * that time. Unregister the interrupt handler and register
5611 * fake ones to scoop up any residual completions.
5612 */
5613 spin_lock_irqsave(&h->lock, flags);
5614 h->access.set_intr_mask(h, HPSA_INTR_OFF);
5615 spin_unlock_irqrestore(&h->lock, flags);
Matt Gates254f7962012-05-01 11:43:06 -05005616 free_irqs(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005617 rc = hpsa_request_irq(h, hpsa_msix_discard_completions,
5618 hpsa_intx_discard_completions);
5619 if (rc) {
5620 dev_warn(&h->pdev->dev, "Failed to request_irq after "
5621 "soft reset.\n");
5622 goto clean4;
5623 }
5624
5625 rc = hpsa_kdump_soft_reset(h);
5626 if (rc)
5627 /* Neither hard nor soft reset worked, we're hosed. */
5628 goto clean4;
5629
5630 dev_info(&h->pdev->dev, "Board READY.\n");
5631 dev_info(&h->pdev->dev,
5632 "Waiting for stale completions to drain.\n");
5633 h->access.set_intr_mask(h, HPSA_INTR_ON);
5634 msleep(10000);
5635 h->access.set_intr_mask(h, HPSA_INTR_OFF);
5636
5637 rc = controller_reset_failed(h->cfgtable);
5638 if (rc)
5639 dev_info(&h->pdev->dev,
5640 "Soft reset appears to have failed.\n");
5641
5642 /* since the controller's reset, we have to go back and re-init
5643 * everything. Easiest to just forget what we've done and do it
5644 * all over again.
5645 */
5646 hpsa_undo_allocations_after_kdump_soft_reset(h);
5647 try_soft_reset = 0;
5648 if (rc)
5649 /* don't go to clean4, we already unallocated */
5650 return -ENODEV;
5651
5652 goto reinit_after_soft_reset;
5653 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005654
5655 /* Turn the interrupts on so we can service requests */
5656 h->access.set_intr_mask(h, HPSA_INTR_ON);
5657
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06005658 hpsa_hba_inquiry(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005659 hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005660
5661 /* Monitor the controller for firmware lockups */
5662 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
5663 INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
5664 schedule_delayed_work(&h->monitor_ctlr_work,
5665 h->heartbeat_sample_interval);
Stephen M. Cameron88bf6d62013-11-01 11:02:25 -05005666 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005667
5668clean4:
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06005669 hpsa_free_sg_chain_blocks(h);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05005670 hpsa_free_cmd_pool(h);
Matt Gates254f7962012-05-01 11:43:06 -05005671 free_irqs(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005672clean2:
5673clean1:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005674 kfree(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06005675 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005676}
5677
5678static void hpsa_flush_cache(struct ctlr_info *h)
5679{
5680 char *flush_buf;
5681 struct CommandList *c;
Stephen M. Cameron702890e2013-09-23 13:33:30 -05005682 unsigned long flags;
5683
5684 /* Don't bother trying to flush the cache if locked up */
5685 spin_lock_irqsave(&h->lock, flags);
5686 if (unlikely(h->lockup_detected)) {
5687 spin_unlock_irqrestore(&h->lock, flags);
5688 return;
5689 }
5690 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005691
5692 flush_buf = kzalloc(4, GFP_KERNEL);
5693 if (!flush_buf)
5694 return;
5695
5696 c = cmd_special_alloc(h);
5697 if (!c) {
5698 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
5699 goto out_of_memory;
5700 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005701 if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
5702 RAID_CTLR_LUNID, TYPE_CMD)) {
5703 goto out;
5704 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005705 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
5706 if (c->err_info->CommandStatus != 0)
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005707out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005708 dev_warn(&h->pdev->dev,
5709 "error flushing cache on controller\n");
5710 cmd_special_free(h, c);
5711out_of_memory:
5712 kfree(flush_buf);
5713}
5714
5715static void hpsa_shutdown(struct pci_dev *pdev)
5716{
5717 struct ctlr_info *h;
5718
5719 h = pci_get_drvdata(pdev);
5720 /* Turn board interrupts off and send the flush cache command
5721 * sendcmd will turn off interrupt, and send the flush...
5722 * To write all data in the battery backed cache to disks
5723 */
5724 hpsa_flush_cache(h);
5725 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05005726 hpsa_free_irqs_and_disable_msix(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005727}
5728
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005729static void hpsa_free_device_info(struct ctlr_info *h)
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06005730{
5731 int i;
5732
5733 for (i = 0; i < h->ndevices; i++)
5734 kfree(h->dev[i]);
5735}
5736
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005737static void hpsa_remove_one(struct pci_dev *pdev)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005738{
5739 struct ctlr_info *h;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005740 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005741
5742 if (pci_get_drvdata(pdev) == NULL) {
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005743 dev_err(&pdev->dev, "unable to remove device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005744 return;
5745 }
5746 h = pci_get_drvdata(pdev);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06005747
5748 /* Get rid of any controller monitoring work items */
5749 spin_lock_irqsave(&h->lock, flags);
5750 h->remove_in_progress = 1;
5751 cancel_delayed_work(&h->monitor_ctlr_work);
5752 spin_unlock_irqrestore(&h->lock, flags);
5753
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005754 hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */
5755 hpsa_shutdown(pdev);
5756 iounmap(h->vaddr);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05005757 iounmap(h->transtable);
5758 iounmap(h->cfgtable);
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06005759 hpsa_free_device_info(h);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06005760 hpsa_free_sg_chain_blocks(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005761 pci_free_consistent(h->pdev,
5762 h->nr_cmds * sizeof(struct CommandList),
5763 h->cmd_pool, h->cmd_pool_dhandle);
5764 pci_free_consistent(h->pdev,
5765 h->nr_cmds * sizeof(struct ErrorInfo),
5766 h->errinfo_pool, h->errinfo_pool_dhandle);
Don Brace303932f2010-02-04 08:42:40 -06005767 pci_free_consistent(h->pdev, h->reply_pool_size,
5768 h->reply_pool, h->reply_pool_dhandle);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005769 kfree(h->cmd_pool_bits);
Don Brace303932f2010-02-04 08:42:40 -06005770 kfree(h->blockFetchTable);
Matt Gatese1f7de02014-02-18 13:55:17 -06005771 kfree(h->ioaccel1_blockFetchTable);
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06005772 kfree(h->hba_inquiry_data);
Stephen M. Cameronf0bd0b682012-05-01 11:42:09 -05005773 pci_disable_device(pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005774 pci_release_regions(pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005775 kfree(h);
5776}
5777
5778static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
5779 __attribute__((unused)) pm_message_t state)
5780{
5781 return -ENOSYS;
5782}
5783
5784static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
5785{
5786 return -ENOSYS;
5787}
5788
5789static struct pci_driver hpsa_pci_driver = {
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06005790 .name = HPSA,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005791 .probe = hpsa_init_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005792 .remove = hpsa_remove_one,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005793 .id_table = hpsa_pci_device_id, /* id_table */
5794 .shutdown = hpsa_shutdown,
5795 .suspend = hpsa_suspend,
5796 .resume = hpsa_resume,
5797};
5798
Don Brace303932f2010-02-04 08:42:40 -06005799/* Fill in bucket_map[], given nsgs (the max number of
5800 * scatter gather elements supported) and bucket[],
5801 * which is an array of 8 integers. The bucket[] array
5802 * contains 8 different DMA transfer sizes (in 16
5803 * byte increments) which the controller uses to fetch
5804 * commands. This function fills in bucket_map[], which
5805 * maps a given number of scatter gather elements to one of
5806 * the 8 DMA transfer sizes. The point of it is to allow the
5807 * controller to only do as much DMA as needed to fetch the
5808 * command, with the DMA transfer size encoded in the lower
5809 * bits of the command address.
5810 */
5811static void calc_bucket_map(int bucket[], int num_buckets,
Matt Gatese1f7de02014-02-18 13:55:17 -06005812 int nsgs, int min_blocks, int *bucket_map)
Don Brace303932f2010-02-04 08:42:40 -06005813{
5814 int i, j, b, size;
5815
Don Brace303932f2010-02-04 08:42:40 -06005816 /* Note, bucket_map must have nsgs+1 entries. */
5817 for (i = 0; i <= nsgs; i++) {
5818 /* Compute size of a command with i SG entries */
Matt Gatese1f7de02014-02-18 13:55:17 -06005819 size = i + min_blocks;
Don Brace303932f2010-02-04 08:42:40 -06005820 b = num_buckets; /* Assume the biggest bucket */
5821 /* Find the bucket that is just big enough */
Matt Gatese1f7de02014-02-18 13:55:17 -06005822 for (j = 0; j < num_buckets; j++) {
Don Brace303932f2010-02-04 08:42:40 -06005823 if (bucket[j] >= size) {
5824 b = j;
5825 break;
5826 }
5827 }
5828 /* for a command with i SG entries, use bucket b. */
5829 bucket_map[i] = b;
5830 }
5831}
5832
Matt Gatese1f7de02014-02-18 13:55:17 -06005833static void hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
Don Brace303932f2010-02-04 08:42:40 -06005834{
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05005835 int i;
5836 unsigned long register_value;
Matt Gatese1f7de02014-02-18 13:55:17 -06005837 unsigned long transMethod = CFGTBL_Trans_Performant |
5838 (trans_support & CFGTBL_Trans_use_short_tags) |
5839 CFGTBL_Trans_enable_directed_msix |
5840 (trans_support & CFGTBL_Trans_io_accel1);
5841
5842 struct access_method access = SA5_performant_access;
Stephen M. Camerondef342b2010-05-27 15:14:39 -05005843
5844 /* This is a bit complicated. There are 8 registers on
5845 * the controller which we write to to tell it 8 different
5846 * sizes of commands which there may be. It's a way of
5847 * reducing the DMA done to fetch each command. Encoded into
5848 * each command's tag are 3 bits which communicate to the controller
5849 * which of the eight sizes that command fits within. The size of
5850 * each command depends on how many scatter gather entries there are.
5851 * Each SG entry requires 16 bytes. The eight registers are programmed
5852 * with the number of 16-byte blocks a command of that size requires.
5853 * The smallest command possible requires 5 such 16 byte blocks.
Stephen M. Camerond66ae082012-01-19 14:00:48 -06005854 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
Stephen M. Camerondef342b2010-05-27 15:14:39 -05005855 * blocks. Note, this only extends to the SG entries contained
5856 * within the command block, and does not extend to chained blocks
5857 * of SG elements. bft[] contains the eight values we write to
5858 * the registers. They are not evenly distributed, but have more
5859 * sizes for small commands, and fewer sizes for larger commands.
5860 */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06005861 int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
5862 BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
Don Brace303932f2010-02-04 08:42:40 -06005863 /* 5 = 1 s/g entry or 4k
5864 * 6 = 2 s/g entry or 8k
5865 * 8 = 4 s/g entry or 16k
5866 * 10 = 6 s/g entry or 24k
5867 */
Don Brace303932f2010-02-04 08:42:40 -06005868
Don Brace303932f2010-02-04 08:42:40 -06005869 /* Controller spec: zero out this buffer. */
5870 memset(h->reply_pool, 0, h->reply_pool_size);
Don Brace303932f2010-02-04 08:42:40 -06005871
Stephen M. Camerond66ae082012-01-19 14:00:48 -06005872 bft[7] = SG_ENTRIES_IN_CMD + 4;
5873 calc_bucket_map(bft, ARRAY_SIZE(bft),
Matt Gatese1f7de02014-02-18 13:55:17 -06005874 SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
Don Brace303932f2010-02-04 08:42:40 -06005875 for (i = 0; i < 8; i++)
5876 writel(bft[i], &h->transtable->BlockFetch[i]);
5877
5878 /* size of controller ring buffer */
5879 writel(h->max_commands, &h->transtable->RepQSize);
Matt Gates254f7962012-05-01 11:43:06 -05005880 writel(h->nreply_queues, &h->transtable->RepQCount);
Don Brace303932f2010-02-04 08:42:40 -06005881 writel(0, &h->transtable->RepQCtrAddrLow32);
5882 writel(0, &h->transtable->RepQCtrAddrHigh32);
Matt Gates254f7962012-05-01 11:43:06 -05005883
5884 for (i = 0; i < h->nreply_queues; i++) {
5885 writel(0, &h->transtable->RepQAddr[i].upper);
5886 writel(h->reply_pool_dhandle +
5887 (h->max_commands * sizeof(u64) * i),
5888 &h->transtable->RepQAddr[i].lower);
5889 }
5890
Matt Gatese1f7de02014-02-18 13:55:17 -06005891 writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
5892 /*
5893 * enable outbound interrupt coalescing in accelerator mode;
5894 */
5895 if (trans_support & CFGTBL_Trans_io_accel1) {
5896 access = SA5_ioaccel_mode1_access;
5897 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
5898 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
5899 }
Don Brace303932f2010-02-04 08:42:40 -06005900 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05005901 hpsa_wait_for_mode_change_ack(h);
Don Brace303932f2010-02-04 08:42:40 -06005902 register_value = readl(&(h->cfgtable->TransportActive));
5903 if (!(register_value & CFGTBL_Trans_Performant)) {
5904 dev_warn(&h->pdev->dev, "unable to get board into"
5905 " performant mode\n");
5906 return;
5907 }
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06005908 /* Change the access methods to the performant access methods */
Matt Gatese1f7de02014-02-18 13:55:17 -06005909 h->access = access;
5910 h->transMethod = transMethod;
5911
5912 if (!(trans_support & CFGTBL_Trans_io_accel1))
5913 return;
5914
5915 /* Set up I/O accelerator mode */
5916 for (i = 0; i < h->nreply_queues; i++) {
5917 writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
5918 h->reply_queue[i].current_entry =
5919 readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
5920 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005921 bft[7] = h->ioaccel_maxsg + 8;
5922 calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
Matt Gatese1f7de02014-02-18 13:55:17 -06005923 h->ioaccel1_blockFetchTable);
5924
5925 /* initialize all reply queue entries to unused */
5926 memset(h->reply_pool, (u8) IOACCEL_MODE1_REPLY_UNUSED,
5927 h->reply_pool_size);
5928
5929 /* set all the constant fields in the accelerator command
5930 * frames once at init time to save CPU cycles later.
5931 */
5932 for (i = 0; i < h->nr_cmds; i++) {
5933 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
5934
5935 cp->function = IOACCEL1_FUNCTION_SCSIIO;
5936 cp->err_info = (u32) (h->errinfo_pool_dhandle +
5937 (i * sizeof(struct ErrorInfo)));
5938 cp->err_info_len = sizeof(struct ErrorInfo);
5939 cp->sgl_offset = IOACCEL1_SGLOFFSET;
5940 cp->host_context_flags = IOACCEL1_HCFLAGS_CISS_FORMAT;
5941 cp->timeout_sec = 0;
5942 cp->ReplyQueue = 0;
5943 cp->Tag.lower = (i << DIRECT_LOOKUP_SHIFT) | DIRECT_LOOKUP_BIT;
5944 cp->Tag.upper = 0;
5945 cp->host_addr.lower = (u32) (h->ioaccel_cmd_pool_dhandle +
5946 (i * sizeof(struct io_accel1_cmd)));
5947 cp->host_addr.upper = 0;
5948 }
5949}
5950
5951static int hpsa_alloc_ioaccel_cmd_and_bft(struct ctlr_info *h)
5952{
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005953 h->ioaccel_maxsg =
5954 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
5955 if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
5956 h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
5957
Matt Gatese1f7de02014-02-18 13:55:17 -06005958 /* Command structures must be aligned on a 128-byte boundary
5959 * because the 7 lower bits of the address are used by the
5960 * hardware.
5961 */
5962#define IOACCEL1_COMMANDLIST_ALIGNMENT 128
5963 BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
5964 IOACCEL1_COMMANDLIST_ALIGNMENT);
5965 h->ioaccel_cmd_pool =
5966 pci_alloc_consistent(h->pdev,
5967 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
5968 &(h->ioaccel_cmd_pool_dhandle));
5969
5970 h->ioaccel1_blockFetchTable =
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005971 kmalloc(((h->ioaccel_maxsg + 1) *
Matt Gatese1f7de02014-02-18 13:55:17 -06005972 sizeof(u32)), GFP_KERNEL);
5973
5974 if ((h->ioaccel_cmd_pool == NULL) ||
5975 (h->ioaccel1_blockFetchTable == NULL))
5976 goto clean_up;
5977
5978 memset(h->ioaccel_cmd_pool, 0,
5979 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
5980 return 0;
5981
5982clean_up:
5983 if (h->ioaccel_cmd_pool)
5984 pci_free_consistent(h->pdev,
5985 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
5986 h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
5987 kfree(h->ioaccel1_blockFetchTable);
5988 return 1;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05005989}
5990
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005991static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05005992{
5993 u32 trans_support;
Matt Gatese1f7de02014-02-18 13:55:17 -06005994 unsigned long transMethod = CFGTBL_Trans_Performant |
5995 CFGTBL_Trans_use_short_tags;
Matt Gates254f7962012-05-01 11:43:06 -05005996 int i;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05005997
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -06005998 if (hpsa_simple_mode)
5999 return;
6000
Matt Gatese1f7de02014-02-18 13:55:17 -06006001 /* Check for I/O accelerator mode support */
6002 if (trans_support & CFGTBL_Trans_io_accel1) {
6003 transMethod |= CFGTBL_Trans_io_accel1 |
6004 CFGTBL_Trans_enable_directed_msix;
6005 if (hpsa_alloc_ioaccel_cmd_and_bft(h))
6006 goto clean_up;
6007 }
6008
6009 /* TODO, check that this next line h->nreply_queues is correct */
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006010 trans_support = readl(&(h->cfgtable->TransportSupport));
6011 if (!(trans_support & PERFORMANT_MODE))
6012 return;
6013
Hannes Reineckeeee0f032014-01-15 13:30:53 +01006014 h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05006015 hpsa_get_max_perf_mode_cmds(h);
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006016 /* Performant mode ring buffer and supporting data structures */
Matt Gates254f7962012-05-01 11:43:06 -05006017 h->reply_pool_size = h->max_commands * sizeof(u64) * h->nreply_queues;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006018 h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size,
6019 &(h->reply_pool_dhandle));
6020
Matt Gates254f7962012-05-01 11:43:06 -05006021 for (i = 0; i < h->nreply_queues; i++) {
6022 h->reply_queue[i].head = &h->reply_pool[h->max_commands * i];
6023 h->reply_queue[i].size = h->max_commands;
6024 h->reply_queue[i].wraparound = 1; /* spec: init to 1 */
6025 h->reply_queue[i].current_entry = 0;
6026 }
6027
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006028 /* Need a block fetch table for performant mode */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006029 h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006030 sizeof(u32)), GFP_KERNEL);
6031
6032 if ((h->reply_pool == NULL)
6033 || (h->blockFetchTable == NULL))
6034 goto clean_up;
6035
Matt Gatese1f7de02014-02-18 13:55:17 -06006036 hpsa_enter_performant_mode(h, trans_support);
Don Brace303932f2010-02-04 08:42:40 -06006037 return;
6038
6039clean_up:
6040 if (h->reply_pool)
6041 pci_free_consistent(h->pdev, h->reply_pool_size,
6042 h->reply_pool, h->reply_pool_dhandle);
6043 kfree(h->blockFetchTable);
6044}
6045
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006046static void hpsa_drain_commands(struct ctlr_info *h)
6047{
6048 int cmds_out;
6049 unsigned long flags;
6050
6051 do { /* wait for all outstanding commands to drain out */
6052 spin_lock_irqsave(&h->lock, flags);
6053 cmds_out = h->commands_outstanding;
6054 spin_unlock_irqrestore(&h->lock, flags);
6055 if (cmds_out <= 0)
6056 break;
6057 msleep(100);
6058 } while (1);
6059}
6060
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006061/*
6062 * This is it. Register the PCI driver information for the cards we control
6063 * the OS will call our registered routines when it finds one of our cards.
6064 */
6065static int __init hpsa_init(void)
6066{
Mike Miller31468402010-02-25 14:03:12 -06006067 return pci_register_driver(&hpsa_pci_driver);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006068}
6069
6070static void __exit hpsa_cleanup(void)
6071{
6072 pci_unregister_driver(&hpsa_pci_driver);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006073}
6074
Matt Gatese1f7de02014-02-18 13:55:17 -06006075static void __attribute__((unused)) verify_offsets(void)
6076{
6077#define VERIFY_OFFSET(member, offset) \
6078 BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
6079
6080 VERIFY_OFFSET(dev_handle, 0x00);
6081 VERIFY_OFFSET(reserved1, 0x02);
6082 VERIFY_OFFSET(function, 0x03);
6083 VERIFY_OFFSET(reserved2, 0x04);
6084 VERIFY_OFFSET(err_info, 0x0C);
6085 VERIFY_OFFSET(reserved3, 0x10);
6086 VERIFY_OFFSET(err_info_len, 0x12);
6087 VERIFY_OFFSET(reserved4, 0x13);
6088 VERIFY_OFFSET(sgl_offset, 0x14);
6089 VERIFY_OFFSET(reserved5, 0x15);
6090 VERIFY_OFFSET(transfer_len, 0x1C);
6091 VERIFY_OFFSET(reserved6, 0x20);
6092 VERIFY_OFFSET(io_flags, 0x24);
6093 VERIFY_OFFSET(reserved7, 0x26);
6094 VERIFY_OFFSET(LUN, 0x34);
6095 VERIFY_OFFSET(control, 0x3C);
6096 VERIFY_OFFSET(CDB, 0x40);
6097 VERIFY_OFFSET(reserved8, 0x50);
6098 VERIFY_OFFSET(host_context_flags, 0x60);
6099 VERIFY_OFFSET(timeout_sec, 0x62);
6100 VERIFY_OFFSET(ReplyQueue, 0x64);
6101 VERIFY_OFFSET(reserved9, 0x65);
6102 VERIFY_OFFSET(Tag, 0x68);
6103 VERIFY_OFFSET(host_addr, 0x70);
6104 VERIFY_OFFSET(CISS_LUN, 0x78);
6105 VERIFY_OFFSET(SG, 0x78 + 8);
6106#undef VERIFY_OFFSET
6107}
6108
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006109module_init(hpsa_init);
6110module_exit(hpsa_cleanup);