blob: 09119c29979493dbe25e84d0e7ac30a3e376fce5 [file] [log] [blame]
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001/*
2 * Disk Array driver for HP Smart Array SAS controllers
Scott Teel51c35132014-02-18 13:57:26 -06003 * Copyright 2000, 2014 Hewlett-Packard Development Company, L.P.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004 *
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. Cameronb7bb24e2014-02-18 13:57:11 -0600186 void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800187 int cmd_type);
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -0600188#define VPD_PAGE (1 << 8)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800189
Jeff Garzikf2812332010-11-16 02:10:29 -0500190static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Stephen M. Camerona08a8472010-02-04 08:43:16 -0600191static void hpsa_scan_start(struct Scsi_Host *);
192static int hpsa_scan_finished(struct Scsi_Host *sh,
193 unsigned long elapsed_time);
Stephen M. Cameron667e23d2010-02-25 14:02:51 -0600194static int hpsa_change_queue_depth(struct scsi_device *sdev,
195 int qdepth, int reason);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800196
197static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500198static int hpsa_eh_abort_handler(struct scsi_cmnd *scsicmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800199static int hpsa_slave_alloc(struct scsi_device *sdev);
200static void hpsa_slave_destroy(struct scsi_device *sdev);
201
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800202static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800203static int check_for_unit_attention(struct ctlr_info *h,
204 struct CommandList *c);
205static void check_ioctl_unit_attention(struct ctlr_info *h,
206 struct CommandList *c);
Don Brace303932f2010-02-04 08:42:40 -0600207/* performant mode helper functions */
208static void calc_bucket_map(int *bucket, int num_buckets,
Matt Gatese1f7de02014-02-18 13:55:17 -0600209 int nsgs, int min_blocks, int *bucket_map);
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800210static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
Matt Gates254f7962012-05-01 11:43:06 -0500211static inline u32 next_command(struct ctlr_info *h, u8 q);
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800212static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
213 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
214 u64 *cfg_offset);
215static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
216 unsigned long *memory_bar);
217static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
218static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
219 int wait_for_ready);
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500220static inline void finish_cmd(struct CommandList *c);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -0600221static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h);
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -0600222#define BOARD_NOT_READY 0
223#define BOARD_READY 1
Stephen M. Cameron23100dd2014-02-18 13:57:37 -0600224static void hpsa_drain_accel_commands(struct ctlr_info *h);
Stephen M. Cameron76438d02014-02-18 13:55:43 -0600225static void hpsa_flush_cache(struct ctlr_info *h);
Scott Teelc3497752014-02-18 13:56:34 -0600226static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
227 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
228 u8 *scsi3addr);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800229
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800230static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
231{
232 unsigned long *priv = shost_priv(sdev->host);
233 return (struct ctlr_info *) *priv;
234}
235
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600236static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
237{
238 unsigned long *priv = shost_priv(sh);
239 return (struct ctlr_info *) *priv;
240}
241
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800242static int check_for_unit_attention(struct ctlr_info *h,
243 struct CommandList *c)
244{
245 if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
246 return 0;
247
248 switch (c->err_info->SenseInfo[12]) {
249 case STATE_CHANGED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600250 dev_warn(&h->pdev->dev, HPSA "%d: a state change "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800251 "detected, command retried\n", h->ctlr);
252 break;
253 case LUN_FAILED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600254 dev_warn(&h->pdev->dev, HPSA "%d: LUN failure "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800255 "detected, action required\n", h->ctlr);
256 break;
257 case REPORT_LUNS_CHANGED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600258 dev_warn(&h->pdev->dev, HPSA "%d: report LUN data "
Mike Miller31468402010-02-25 14:03:12 -0600259 "changed, action required\n", h->ctlr);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800260 /*
Scott Teel4f4eb9f2012-01-19 14:01:25 -0600261 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
262 * target (array) devices.
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800263 */
264 break;
265 case POWER_OR_RESET:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600266 dev_warn(&h->pdev->dev, HPSA "%d: a power on "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800267 "or device reset detected\n", h->ctlr);
268 break;
269 case UNIT_ATTENTION_CLEARED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600270 dev_warn(&h->pdev->dev, HPSA "%d: unit attention "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800271 "cleared by another initiator\n", h->ctlr);
272 break;
273 default:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600274 dev_warn(&h->pdev->dev, HPSA "%d: unknown "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800275 "unit attention detected\n", h->ctlr);
276 break;
277 }
278 return 1;
279}
280
Matt Bondurant852af202012-05-01 11:42:35 -0500281static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
282{
283 if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
284 (c->err_info->ScsiStatus != SAM_STAT_BUSY &&
285 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
286 return 0;
287 dev_warn(&h->pdev->dev, HPSA "device busy");
288 return 1;
289}
290
Scott Teelda0697b2014-02-18 13:57:00 -0600291static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
292 struct device_attribute *attr,
293 const char *buf, size_t count)
294{
295 int status, len;
296 struct ctlr_info *h;
297 struct Scsi_Host *shost = class_to_shost(dev);
298 char tmpbuf[10];
299
300 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
301 return -EACCES;
302 len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
303 strncpy(tmpbuf, buf, len);
304 tmpbuf[len] = '\0';
305 if (sscanf(tmpbuf, "%d", &status) != 1)
306 return -EINVAL;
307 h = shost_to_hba(shost);
308 h->acciopath_status = !!status;
309 dev_warn(&h->pdev->dev,
310 "hpsa: HP SSD Smart Path %s via sysfs update.\n",
311 h->acciopath_status ? "enabled" : "disabled");
312 return count;
313}
314
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600315static ssize_t host_store_raid_offload_debug(struct device *dev,
316 struct device_attribute *attr,
317 const char *buf, size_t count)
318{
319 int debug_level, len;
320 struct ctlr_info *h;
321 struct Scsi_Host *shost = class_to_shost(dev);
322 char tmpbuf[10];
323
324 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
325 return -EACCES;
326 len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
327 strncpy(tmpbuf, buf, len);
328 tmpbuf[len] = '\0';
329 if (sscanf(tmpbuf, "%d", &debug_level) != 1)
330 return -EINVAL;
331 if (debug_level < 0)
332 debug_level = 0;
333 h = shost_to_hba(shost);
334 h->raid_offload_debug = debug_level;
335 dev_warn(&h->pdev->dev, "hpsa: Set raid_offload_debug level = %d\n",
336 h->raid_offload_debug);
337 return count;
338}
339
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800340static ssize_t host_store_rescan(struct device *dev,
341 struct device_attribute *attr,
342 const char *buf, size_t count)
343{
344 struct ctlr_info *h;
345 struct Scsi_Host *shost = class_to_shost(dev);
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600346 h = shost_to_hba(shost);
Mike Miller31468402010-02-25 14:03:12 -0600347 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800348 return count;
349}
350
Stephen M. Camerond28ce022010-05-27 15:14:34 -0500351static ssize_t host_show_firmware_revision(struct device *dev,
352 struct device_attribute *attr, char *buf)
353{
354 struct ctlr_info *h;
355 struct Scsi_Host *shost = class_to_shost(dev);
356 unsigned char *fwrev;
357
358 h = shost_to_hba(shost);
359 if (!h->hba_inquiry_data)
360 return 0;
361 fwrev = &h->hba_inquiry_data[32];
362 return snprintf(buf, 20, "%c%c%c%c\n",
363 fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
364}
365
Stephen M. Cameron94a13642011-01-06 14:48:39 -0600366static ssize_t host_show_commands_outstanding(struct device *dev,
367 struct device_attribute *attr, char *buf)
368{
369 struct Scsi_Host *shost = class_to_shost(dev);
370 struct ctlr_info *h = shost_to_hba(shost);
371
372 return snprintf(buf, 20, "%d\n", h->commands_outstanding);
373}
374
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600375static ssize_t host_show_transport_mode(struct device *dev,
376 struct device_attribute *attr, char *buf)
377{
378 struct ctlr_info *h;
379 struct Scsi_Host *shost = class_to_shost(dev);
380
381 h = shost_to_hba(shost);
382 return snprintf(buf, 20, "%s\n",
Stephen M. Cameron960a30e2011-02-15 15:33:03 -0600383 h->transMethod & CFGTBL_Trans_Performant ?
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600384 "performant" : "simple");
385}
386
Scott Teelda0697b2014-02-18 13:57:00 -0600387static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
388 struct device_attribute *attr, char *buf)
389{
390 struct ctlr_info *h;
391 struct Scsi_Host *shost = class_to_shost(dev);
392
393 h = shost_to_hba(shost);
394 return snprintf(buf, 30, "HP SSD Smart Path %s\n",
395 (h->acciopath_status == 1) ? "enabled" : "disabled");
396}
397
Stephen M. Cameron46380782011-05-03 15:00:01 -0500398/* List of controllers which cannot be hard reset on kexec with reset_devices */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600399static u32 unresettable_controller[] = {
400 0x324a103C, /* Smart Array P712m */
401 0x324b103C, /* SmartArray P711m */
402 0x3223103C, /* Smart Array P800 */
403 0x3234103C, /* Smart Array P400 */
404 0x3235103C, /* Smart Array P400i */
405 0x3211103C, /* Smart Array E200i */
406 0x3212103C, /* Smart Array E200 */
407 0x3213103C, /* Smart Array E200i */
408 0x3214103C, /* Smart Array E200i */
409 0x3215103C, /* Smart Array E200i */
410 0x3237103C, /* Smart Array E500 */
411 0x323D103C, /* Smart Array P700m */
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100412 0x40800E11, /* Smart Array 5i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600413 0x409C0E11, /* Smart Array 6400 */
414 0x409D0E11, /* Smart Array 6400 EM */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100415 0x40700E11, /* Smart Array 5300 */
416 0x40820E11, /* Smart Array 532 */
417 0x40830E11, /* Smart Array 5312 */
418 0x409A0E11, /* Smart Array 641 */
419 0x409B0E11, /* Smart Array 642 */
420 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600421};
422
Stephen M. Cameron46380782011-05-03 15:00:01 -0500423/* List of controllers which cannot even be soft reset */
424static u32 soft_unresettable_controller[] = {
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100425 0x40800E11, /* Smart Array 5i */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100426 0x40700E11, /* Smart Array 5300 */
427 0x40820E11, /* Smart Array 532 */
428 0x40830E11, /* Smart Array 5312 */
429 0x409A0E11, /* Smart Array 641 */
430 0x409B0E11, /* Smart Array 642 */
431 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron46380782011-05-03 15:00:01 -0500432 /* Exclude 640x boards. These are two pci devices in one slot
433 * which share a battery backed cache module. One controls the
434 * cache, the other accesses the cache through the one that controls
435 * it. If we reset the one controlling the cache, the other will
436 * likely not be happy. Just forbid resetting this conjoined mess.
437 * The 640x isn't really supported by hpsa anyway.
438 */
439 0x409C0E11, /* Smart Array 6400 */
440 0x409D0E11, /* Smart Array 6400 EM */
441};
442
443static int ctlr_is_hard_resettable(u32 board_id)
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600444{
445 int i;
446
447 for (i = 0; i < ARRAY_SIZE(unresettable_controller); i++)
Stephen M. Cameron46380782011-05-03 15:00:01 -0500448 if (unresettable_controller[i] == board_id)
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600449 return 0;
450 return 1;
451}
452
Stephen M. Cameron46380782011-05-03 15:00:01 -0500453static int ctlr_is_soft_resettable(u32 board_id)
454{
455 int i;
456
457 for (i = 0; i < ARRAY_SIZE(soft_unresettable_controller); i++)
458 if (soft_unresettable_controller[i] == board_id)
459 return 0;
460 return 1;
461}
462
463static int ctlr_is_resettable(u32 board_id)
464{
465 return ctlr_is_hard_resettable(board_id) ||
466 ctlr_is_soft_resettable(board_id);
467}
468
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600469static ssize_t host_show_resettable(struct device *dev,
470 struct device_attribute *attr, char *buf)
471{
472 struct ctlr_info *h;
473 struct Scsi_Host *shost = class_to_shost(dev);
474
475 h = shost_to_hba(shost);
Stephen M. Cameron46380782011-05-03 15:00:01 -0500476 return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600477}
478
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800479static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
480{
481 return (scsi3addr[3] & 0xC0) == 0x40;
482}
483
484static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
Mike Millerd82357e2012-05-01 11:43:32 -0500485 "1(ADM)", "UNKNOWN"
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800486};
Scott Teel6b80b182014-02-18 13:56:55 -0600487#define HPSA_RAID_0 0
488#define HPSA_RAID_4 1
489#define HPSA_RAID_1 2 /* also used for RAID 10 */
490#define HPSA_RAID_5 3 /* also used for RAID 50 */
491#define HPSA_RAID_51 4
492#define HPSA_RAID_6 5 /* also used for RAID 60 */
493#define HPSA_RAID_ADM 6 /* also used for RAID 1+0 ADM */
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800494#define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
495
496static ssize_t raid_level_show(struct device *dev,
497 struct device_attribute *attr, char *buf)
498{
499 ssize_t l = 0;
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600500 unsigned char rlevel;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800501 struct ctlr_info *h;
502 struct scsi_device *sdev;
503 struct hpsa_scsi_dev_t *hdev;
504 unsigned long flags;
505
506 sdev = to_scsi_device(dev);
507 h = sdev_to_hba(sdev);
508 spin_lock_irqsave(&h->lock, flags);
509 hdev = sdev->hostdata;
510 if (!hdev) {
511 spin_unlock_irqrestore(&h->lock, flags);
512 return -ENODEV;
513 }
514
515 /* Is this even a logical drive? */
516 if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
517 spin_unlock_irqrestore(&h->lock, flags);
518 l = snprintf(buf, PAGE_SIZE, "N/A\n");
519 return l;
520 }
521
522 rlevel = hdev->raid_level;
523 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600524 if (rlevel > RAID_UNKNOWN)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800525 rlevel = RAID_UNKNOWN;
526 l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
527 return l;
528}
529
530static ssize_t lunid_show(struct device *dev,
531 struct device_attribute *attr, char *buf)
532{
533 struct ctlr_info *h;
534 struct scsi_device *sdev;
535 struct hpsa_scsi_dev_t *hdev;
536 unsigned long flags;
537 unsigned char lunid[8];
538
539 sdev = to_scsi_device(dev);
540 h = sdev_to_hba(sdev);
541 spin_lock_irqsave(&h->lock, flags);
542 hdev = sdev->hostdata;
543 if (!hdev) {
544 spin_unlock_irqrestore(&h->lock, flags);
545 return -ENODEV;
546 }
547 memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
548 spin_unlock_irqrestore(&h->lock, flags);
549 return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
550 lunid[0], lunid[1], lunid[2], lunid[3],
551 lunid[4], lunid[5], lunid[6], lunid[7]);
552}
553
554static ssize_t unique_id_show(struct device *dev,
555 struct device_attribute *attr, char *buf)
556{
557 struct ctlr_info *h;
558 struct scsi_device *sdev;
559 struct hpsa_scsi_dev_t *hdev;
560 unsigned long flags;
561 unsigned char sn[16];
562
563 sdev = to_scsi_device(dev);
564 h = sdev_to_hba(sdev);
565 spin_lock_irqsave(&h->lock, flags);
566 hdev = sdev->hostdata;
567 if (!hdev) {
568 spin_unlock_irqrestore(&h->lock, flags);
569 return -ENODEV;
570 }
571 memcpy(sn, hdev->device_id, sizeof(sn));
572 spin_unlock_irqrestore(&h->lock, flags);
573 return snprintf(buf, 16 * 2 + 2,
574 "%02X%02X%02X%02X%02X%02X%02X%02X"
575 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
576 sn[0], sn[1], sn[2], sn[3],
577 sn[4], sn[5], sn[6], sn[7],
578 sn[8], sn[9], sn[10], sn[11],
579 sn[12], sn[13], sn[14], sn[15]);
580}
581
Scott Teelc1988682014-02-18 13:55:54 -0600582static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
583 struct device_attribute *attr, char *buf)
584{
585 struct ctlr_info *h;
586 struct scsi_device *sdev;
587 struct hpsa_scsi_dev_t *hdev;
588 unsigned long flags;
589 int offload_enabled;
590
591 sdev = to_scsi_device(dev);
592 h = sdev_to_hba(sdev);
593 spin_lock_irqsave(&h->lock, flags);
594 hdev = sdev->hostdata;
595 if (!hdev) {
596 spin_unlock_irqrestore(&h->lock, flags);
597 return -ENODEV;
598 }
599 offload_enabled = hdev->offload_enabled;
600 spin_unlock_irqrestore(&h->lock, flags);
601 return snprintf(buf, 20, "%d\n", offload_enabled);
602}
603
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600604static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
605static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
606static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
607static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
Scott Teelc1988682014-02-18 13:55:54 -0600608static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
609 host_show_hp_ssd_smart_path_enabled, NULL);
Scott Teelda0697b2014-02-18 13:57:00 -0600610static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
611 host_show_hp_ssd_smart_path_status,
612 host_store_hp_ssd_smart_path_status);
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600613static DEVICE_ATTR(raid_offload_debug, S_IWUSR, NULL,
614 host_store_raid_offload_debug);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600615static DEVICE_ATTR(firmware_revision, S_IRUGO,
616 host_show_firmware_revision, NULL);
617static DEVICE_ATTR(commands_outstanding, S_IRUGO,
618 host_show_commands_outstanding, NULL);
619static DEVICE_ATTR(transport_mode, S_IRUGO,
620 host_show_transport_mode, NULL);
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600621static DEVICE_ATTR(resettable, S_IRUGO,
622 host_show_resettable, NULL);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600623
624static struct device_attribute *hpsa_sdev_attrs[] = {
625 &dev_attr_raid_level,
626 &dev_attr_lunid,
627 &dev_attr_unique_id,
Scott Teelc1988682014-02-18 13:55:54 -0600628 &dev_attr_hp_ssd_smart_path_enabled,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600629 NULL,
630};
631
632static struct device_attribute *hpsa_shost_attrs[] = {
633 &dev_attr_rescan,
634 &dev_attr_firmware_revision,
635 &dev_attr_commands_outstanding,
636 &dev_attr_transport_mode,
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600637 &dev_attr_resettable,
Scott Teelda0697b2014-02-18 13:57:00 -0600638 &dev_attr_hp_ssd_smart_path_status,
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600639 &dev_attr_raid_offload_debug,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600640 NULL,
641};
642
643static struct scsi_host_template hpsa_driver_template = {
644 .module = THIS_MODULE,
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600645 .name = HPSA,
646 .proc_name = HPSA,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600647 .queuecommand = hpsa_scsi_queue_command,
648 .scan_start = hpsa_scan_start,
649 .scan_finished = hpsa_scan_finished,
650 .change_queue_depth = hpsa_change_queue_depth,
651 .this_id = -1,
652 .use_clustering = ENABLE_CLUSTERING,
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500653 .eh_abort_handler = hpsa_eh_abort_handler,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600654 .eh_device_reset_handler = hpsa_eh_device_reset_handler,
655 .ioctl = hpsa_ioctl,
656 .slave_alloc = hpsa_slave_alloc,
657 .slave_destroy = hpsa_slave_destroy,
658#ifdef CONFIG_COMPAT
659 .compat_ioctl = hpsa_compat_ioctl,
660#endif
661 .sdev_attrs = hpsa_sdev_attrs,
662 .shost_attrs = hpsa_shost_attrs,
Stephen M. Cameronc0d6a4d2011-10-26 16:20:53 -0500663 .max_sectors = 8192,
Martin K. Petersen54b2b502013-10-23 06:25:40 -0400664 .no_write_same = 1,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600665};
666
667
668/* Enqueuing and dequeuing functions for cmdlists. */
669static inline void addQ(struct list_head *list, struct CommandList *c)
670{
671 list_add_tail(&c->list, list);
672}
673
Matt Gates254f7962012-05-01 11:43:06 -0500674static inline u32 next_command(struct ctlr_info *h, u8 q)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600675{
676 u32 a;
Matt Gates254f7962012-05-01 11:43:06 -0500677 struct reply_pool *rq = &h->reply_queue[q];
Matt Gatese16a33a2012-05-01 11:43:11 -0500678 unsigned long flags;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600679
Matt Gatese1f7de02014-02-18 13:55:17 -0600680 if (h->transMethod & CFGTBL_Trans_io_accel1)
681 return h->access.command_completed(h, q);
682
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600683 if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
Matt Gates254f7962012-05-01 11:43:06 -0500684 return h->access.command_completed(h, q);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600685
Matt Gates254f7962012-05-01 11:43:06 -0500686 if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
687 a = rq->head[rq->current_entry];
688 rq->current_entry++;
Matt Gatese16a33a2012-05-01 11:43:11 -0500689 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600690 h->commands_outstanding--;
Matt Gatese16a33a2012-05-01 11:43:11 -0500691 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600692 } else {
693 a = FIFO_EMPTY;
694 }
695 /* Check for wraparound */
Matt Gates254f7962012-05-01 11:43:06 -0500696 if (rq->current_entry == h->max_commands) {
697 rq->current_entry = 0;
698 rq->wraparound ^= 1;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600699 }
700 return a;
701}
702
Scott Teelc3497752014-02-18 13:56:34 -0600703/*
704 * There are some special bits in the bus address of the
705 * command that we have to set for the controller to know
706 * how to process the command:
707 *
708 * Normal performant mode:
709 * bit 0: 1 means performant mode, 0 means simple mode.
710 * bits 1-3 = block fetch table entry
711 * bits 4-6 = command type (== 0)
712 *
713 * ioaccel1 mode:
714 * bit 0 = "performant mode" bit.
715 * bits 1-3 = block fetch table entry
716 * bits 4-6 = command type (== 110)
717 * (command type is needed because ioaccel1 mode
718 * commands are submitted through the same register as normal
719 * mode commands, so this is how the controller knows whether
720 * the command is normal mode or ioaccel1 mode.)
721 *
722 * ioaccel2 mode:
723 * bit 0 = "performant mode" bit.
724 * bits 1-4 = block fetch table entry (note extra bit)
725 * bits 4-6 = not needed, because ioaccel2 mode has
726 * a separate special register for submitting commands.
727 */
728
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600729/* set_performant_mode: Modify the tag for cciss performant
730 * set bit 0 for pull model, bits 3-1 for block fetch
731 * register number
732 */
733static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
734{
Matt Gates254f7962012-05-01 11:43:06 -0500735 if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600736 c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
Hannes Reineckeeee0f032014-01-15 13:30:53 +0100737 if (likely(h->msix_vector > 0))
Matt Gates254f7962012-05-01 11:43:06 -0500738 c->Header.ReplyQueue =
John Kacur804a5cb2013-07-26 16:06:18 +0200739 raw_smp_processor_id() % h->nreply_queues;
Matt Gates254f7962012-05-01 11:43:06 -0500740 }
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600741}
742
Scott Teelc3497752014-02-18 13:56:34 -0600743static void set_ioaccel1_performant_mode(struct ctlr_info *h,
744 struct CommandList *c)
745{
746 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
747
748 /* Tell the controller to post the reply to the queue for this
749 * processor. This seems to give the best I/O throughput.
750 */
751 cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
752 /* Set the bits in the address sent down to include:
753 * - performant mode bit (bit 0)
754 * - pull count (bits 1-3)
755 * - command type (bits 4-6)
756 */
757 c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
758 IOACCEL1_BUSADDR_CMDTYPE;
759}
760
761static void set_ioaccel2_performant_mode(struct ctlr_info *h,
762 struct CommandList *c)
763{
764 struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
765
766 /* Tell the controller to post the reply to the queue for this
767 * processor. This seems to give the best I/O throughput.
768 */
769 cp->reply_queue = smp_processor_id() % h->nreply_queues;
770 /* Set the bits in the address sent down to include:
771 * - performant mode bit not used in ioaccel mode 2
772 * - pull count (bits 0-3)
773 * - command type isn't needed for ioaccel2
774 */
775 c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
776}
777
Stephen M. Camerone85c5972012-05-01 11:43:42 -0500778static int is_firmware_flash_cmd(u8 *cdb)
779{
780 return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
781}
782
783/*
784 * During firmware flash, the heartbeat register may not update as frequently
785 * as it should. So we dial down lockup detection during firmware flash. and
786 * dial it back up when firmware flash completes.
787 */
788#define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
789#define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
790static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
791 struct CommandList *c)
792{
793 if (!is_firmware_flash_cmd(c->Request.CDB))
794 return;
795 atomic_inc(&h->firmware_flash_in_progress);
796 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
797}
798
799static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
800 struct CommandList *c)
801{
802 if (is_firmware_flash_cmd(c->Request.CDB) &&
803 atomic_dec_and_test(&h->firmware_flash_in_progress))
804 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
805}
806
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600807static void enqueue_cmd_and_start_io(struct ctlr_info *h,
808 struct CommandList *c)
809{
810 unsigned long flags;
811
Scott Teelc3497752014-02-18 13:56:34 -0600812 switch (c->cmd_type) {
813 case CMD_IOACCEL1:
814 set_ioaccel1_performant_mode(h, c);
815 break;
816 case CMD_IOACCEL2:
817 set_ioaccel2_performant_mode(h, c);
818 break;
819 default:
820 set_performant_mode(h, c);
821 }
Stephen M. Camerone85c5972012-05-01 11:43:42 -0500822 dial_down_lockup_detection_during_fw_flash(h, c);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600823 spin_lock_irqsave(&h->lock, flags);
824 addQ(&h->reqQ, c);
825 h->Qdepth++;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600826 spin_unlock_irqrestore(&h->lock, flags);
Matt Gatese16a33a2012-05-01 11:43:11 -0500827 start_io(h);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600828}
829
830static inline void removeQ(struct CommandList *c)
831{
832 if (WARN_ON(list_empty(&c->list)))
833 return;
834 list_del_init(&c->list);
835}
836
837static inline int is_hba_lunid(unsigned char scsi3addr[])
838{
839 return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
840}
841
842static inline int is_scsi_rev_5(struct ctlr_info *h)
843{
844 if (!h->hba_inquiry_data)
845 return 0;
846 if ((h->hba_inquiry_data[2] & 0x07) == 5)
847 return 1;
848 return 0;
849}
850
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800851static int hpsa_find_target_lun(struct ctlr_info *h,
852 unsigned char scsi3addr[], int bus, int *target, int *lun)
853{
854 /* finds an unused bus, target, lun for a new physical device
855 * assumes h->devlock is held
856 */
857 int i, found = 0;
Scott Teelcfe5bad2011-10-26 16:21:07 -0500858 DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800859
Akinobu Mita263d9402012-01-21 00:15:27 +0900860 bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800861
862 for (i = 0; i < h->ndevices; i++) {
863 if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
Akinobu Mita263d9402012-01-21 00:15:27 +0900864 __set_bit(h->dev[i]->target, lun_taken);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800865 }
866
Akinobu Mita263d9402012-01-21 00:15:27 +0900867 i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
868 if (i < HPSA_MAX_DEVICES) {
869 /* *bus = 1; */
870 *target = i;
871 *lun = 0;
872 found = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800873 }
874 return !found;
875}
876
877/* Add an entry into h->dev[] array. */
878static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
879 struct hpsa_scsi_dev_t *device,
880 struct hpsa_scsi_dev_t *added[], int *nadded)
881{
882 /* assumes h->devlock is held */
883 int n = h->ndevices;
884 int i;
885 unsigned char addr1[8], addr2[8];
886 struct hpsa_scsi_dev_t *sd;
887
Scott Teelcfe5bad2011-10-26 16:21:07 -0500888 if (n >= HPSA_MAX_DEVICES) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800889 dev_err(&h->pdev->dev, "too many devices, some will be "
890 "inaccessible.\n");
891 return -1;
892 }
893
894 /* physical devices do not have lun or target assigned until now. */
895 if (device->lun != -1)
896 /* Logical device, lun is already assigned. */
897 goto lun_assigned;
898
899 /* If this device a non-zero lun of a multi-lun device
900 * byte 4 of the 8-byte LUN addr will contain the logical
901 * unit no, zero otherise.
902 */
903 if (device->scsi3addr[4] == 0) {
904 /* This is not a non-zero lun of a multi-lun device */
905 if (hpsa_find_target_lun(h, device->scsi3addr,
906 device->bus, &device->target, &device->lun) != 0)
907 return -1;
908 goto lun_assigned;
909 }
910
911 /* This is a non-zero lun of a multi-lun device.
912 * Search through our list and find the device which
913 * has the same 8 byte LUN address, excepting byte 4.
914 * Assign the same bus and target for this new LUN.
915 * Use the logical unit number from the firmware.
916 */
917 memcpy(addr1, device->scsi3addr, 8);
918 addr1[4] = 0;
919 for (i = 0; i < n; i++) {
920 sd = h->dev[i];
921 memcpy(addr2, sd->scsi3addr, 8);
922 addr2[4] = 0;
923 /* differ only in byte 4? */
924 if (memcmp(addr1, addr2, 8) == 0) {
925 device->bus = sd->bus;
926 device->target = sd->target;
927 device->lun = device->scsi3addr[4];
928 break;
929 }
930 }
931 if (device->lun == -1) {
932 dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
933 " suspect firmware bug or unsupported hardware "
934 "configuration.\n");
935 return -1;
936 }
937
938lun_assigned:
939
940 h->dev[n] = device;
941 h->ndevices++;
942 added[*nadded] = device;
943 (*nadded)++;
944
945 /* initially, (before registering with scsi layer) we don't
946 * know our hostno and we don't want to print anything first
947 * time anyway (the scsi layer's inquiries will show that info)
948 */
949 /* if (hostno != -1) */
950 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
951 scsi_device_type(device->devtype), hostno,
952 device->bus, device->target, device->lun);
953 return 0;
954}
955
Scott Teelbd9244f2012-01-19 14:01:30 -0600956/* Update an entry in h->dev[] array. */
957static void hpsa_scsi_update_entry(struct ctlr_info *h, int hostno,
958 int entry, struct hpsa_scsi_dev_t *new_entry)
959{
960 /* assumes h->devlock is held */
961 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
962
963 /* Raid level changed. */
964 h->dev[entry]->raid_level = new_entry->raid_level;
Stephen M. Cameron250fb122014-02-18 13:55:38 -0600965
966 /* Raid offload parameters changed. */
967 h->dev[entry]->offload_config = new_entry->offload_config;
968 h->dev[entry]->offload_enabled = new_entry->offload_enabled;
Stephen M. Cameron9fb0de22014-02-18 13:56:50 -0600969 h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
970 h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
971 h->dev[entry]->raid_map = new_entry->raid_map;
Stephen M. Cameron250fb122014-02-18 13:55:38 -0600972
Scott Teelbd9244f2012-01-19 14:01:30 -0600973 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d updated.\n",
974 scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
975 new_entry->target, new_entry->lun);
976}
977
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -0600978/* Replace an entry from h->dev[] array. */
979static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
980 int entry, struct hpsa_scsi_dev_t *new_entry,
981 struct hpsa_scsi_dev_t *added[], int *nadded,
982 struct hpsa_scsi_dev_t *removed[], int *nremoved)
983{
984 /* assumes h->devlock is held */
Scott Teelcfe5bad2011-10-26 16:21:07 -0500985 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -0600986 removed[*nremoved] = h->dev[entry];
987 (*nremoved)++;
Stephen M. Cameron01350d02011-08-09 08:18:01 -0500988
989 /*
990 * New physical devices won't have target/lun assigned yet
991 * so we need to preserve the values in the slot we are replacing.
992 */
993 if (new_entry->target == -1) {
994 new_entry->target = h->dev[entry]->target;
995 new_entry->lun = h->dev[entry]->lun;
996 }
997
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -0600998 h->dev[entry] = new_entry;
999 added[*nadded] = new_entry;
1000 (*nadded)++;
1001 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n",
1002 scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
1003 new_entry->target, new_entry->lun);
1004}
1005
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001006/* Remove an entry from h->dev[] array. */
1007static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
1008 struct hpsa_scsi_dev_t *removed[], int *nremoved)
1009{
1010 /* assumes h->devlock is held */
1011 int i;
1012 struct hpsa_scsi_dev_t *sd;
1013
Scott Teelcfe5bad2011-10-26 16:21:07 -05001014 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001015
1016 sd = h->dev[entry];
1017 removed[*nremoved] = h->dev[entry];
1018 (*nremoved)++;
1019
1020 for (i = entry; i < h->ndevices-1; i++)
1021 h->dev[i] = h->dev[i+1];
1022 h->ndevices--;
1023 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
1024 scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
1025 sd->lun);
1026}
1027
1028#define SCSI3ADDR_EQ(a, b) ( \
1029 (a)[7] == (b)[7] && \
1030 (a)[6] == (b)[6] && \
1031 (a)[5] == (b)[5] && \
1032 (a)[4] == (b)[4] && \
1033 (a)[3] == (b)[3] && \
1034 (a)[2] == (b)[2] && \
1035 (a)[1] == (b)[1] && \
1036 (a)[0] == (b)[0])
1037
1038static void fixup_botched_add(struct ctlr_info *h,
1039 struct hpsa_scsi_dev_t *added)
1040{
1041 /* called when scsi_add_device fails in order to re-adjust
1042 * h->dev[] to match the mid layer's view.
1043 */
1044 unsigned long flags;
1045 int i, j;
1046
1047 spin_lock_irqsave(&h->lock, flags);
1048 for (i = 0; i < h->ndevices; i++) {
1049 if (h->dev[i] == added) {
1050 for (j = i; j < h->ndevices-1; j++)
1051 h->dev[j] = h->dev[j+1];
1052 h->ndevices--;
1053 break;
1054 }
1055 }
1056 spin_unlock_irqrestore(&h->lock, flags);
1057 kfree(added);
1058}
1059
1060static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
1061 struct hpsa_scsi_dev_t *dev2)
1062{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001063 /* we compare everything except lun and target as these
1064 * are not yet assigned. Compare parts likely
1065 * to differ first
1066 */
1067 if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
1068 sizeof(dev1->scsi3addr)) != 0)
1069 return 0;
1070 if (memcmp(dev1->device_id, dev2->device_id,
1071 sizeof(dev1->device_id)) != 0)
1072 return 0;
1073 if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
1074 return 0;
1075 if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
1076 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001077 if (dev1->devtype != dev2->devtype)
1078 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001079 if (dev1->bus != dev2->bus)
1080 return 0;
1081 return 1;
1082}
1083
Scott Teelbd9244f2012-01-19 14:01:30 -06001084static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
1085 struct hpsa_scsi_dev_t *dev2)
1086{
1087 /* Device attributes that can change, but don't mean
1088 * that the device is a different device, nor that the OS
1089 * needs to be told anything about the change.
1090 */
1091 if (dev1->raid_level != dev2->raid_level)
1092 return 1;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001093 if (dev1->offload_config != dev2->offload_config)
1094 return 1;
1095 if (dev1->offload_enabled != dev2->offload_enabled)
1096 return 1;
Scott Teelbd9244f2012-01-19 14:01:30 -06001097 return 0;
1098}
1099
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001100/* Find needle in haystack. If exact match found, return DEVICE_SAME,
1101 * and return needle location in *index. If scsi3addr matches, but not
1102 * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
Scott Teelbd9244f2012-01-19 14:01:30 -06001103 * location in *index.
1104 * In the case of a minor device attribute change, such as RAID level, just
1105 * return DEVICE_UPDATED, along with the updated device's location in index.
1106 * If needle not found, return DEVICE_NOT_FOUND.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001107 */
1108static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
1109 struct hpsa_scsi_dev_t *haystack[], int haystack_size,
1110 int *index)
1111{
1112 int i;
1113#define DEVICE_NOT_FOUND 0
1114#define DEVICE_CHANGED 1
1115#define DEVICE_SAME 2
Scott Teelbd9244f2012-01-19 14:01:30 -06001116#define DEVICE_UPDATED 3
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001117 for (i = 0; i < haystack_size; i++) {
Stephen M. Cameron23231042010-02-04 08:43:36 -06001118 if (haystack[i] == NULL) /* previously removed. */
1119 continue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001120 if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
1121 *index = i;
Scott Teelbd9244f2012-01-19 14:01:30 -06001122 if (device_is_the_same(needle, haystack[i])) {
1123 if (device_updated(needle, haystack[i]))
1124 return DEVICE_UPDATED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001125 return DEVICE_SAME;
Scott Teelbd9244f2012-01-19 14:01:30 -06001126 } else {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001127 return DEVICE_CHANGED;
Scott Teelbd9244f2012-01-19 14:01:30 -06001128 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001129 }
1130 }
1131 *index = -1;
1132 return DEVICE_NOT_FOUND;
1133}
1134
Stephen M. Cameron4967bd32010-02-04 08:41:49 -06001135static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001136 struct hpsa_scsi_dev_t *sd[], int nsds)
1137{
1138 /* sd contains scsi3 addresses and devtypes, and inquiry
1139 * data. This function takes what's in sd to be the current
1140 * reality and updates h->dev[] to reflect that reality.
1141 */
1142 int i, entry, device_change, changes = 0;
1143 struct hpsa_scsi_dev_t *csd;
1144 unsigned long flags;
1145 struct hpsa_scsi_dev_t **added, **removed;
1146 int nadded, nremoved;
1147 struct Scsi_Host *sh = NULL;
1148
Scott Teelcfe5bad2011-10-26 16:21:07 -05001149 added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
1150 removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001151
1152 if (!added || !removed) {
1153 dev_warn(&h->pdev->dev, "out of memory in "
1154 "adjust_hpsa_scsi_table\n");
1155 goto free_and_out;
1156 }
1157
1158 spin_lock_irqsave(&h->devlock, flags);
1159
1160 /* find any devices in h->dev[] that are not in
1161 * sd[] and remove them from h->dev[], and for any
1162 * devices which have changed, remove the old device
1163 * info and add the new device info.
Scott Teelbd9244f2012-01-19 14:01:30 -06001164 * If minor device attributes change, just update
1165 * the existing device structure.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001166 */
1167 i = 0;
1168 nremoved = 0;
1169 nadded = 0;
1170 while (i < h->ndevices) {
1171 csd = h->dev[i];
1172 device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
1173 if (device_change == DEVICE_NOT_FOUND) {
1174 changes++;
1175 hpsa_scsi_remove_entry(h, hostno, i,
1176 removed, &nremoved);
1177 continue; /* remove ^^^, hence i not incremented */
1178 } else if (device_change == DEVICE_CHANGED) {
1179 changes++;
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001180 hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
1181 added, &nadded, removed, &nremoved);
Stephen M. Cameronc7f172d2010-02-04 08:43:31 -06001182 /* Set it to NULL to prevent it from being freed
1183 * at the bottom of hpsa_update_scsi_devices()
1184 */
1185 sd[entry] = NULL;
Scott Teelbd9244f2012-01-19 14:01:30 -06001186 } else if (device_change == DEVICE_UPDATED) {
1187 hpsa_scsi_update_entry(h, hostno, i, sd[entry]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001188 }
1189 i++;
1190 }
1191
1192 /* Now, make sure every device listed in sd[] is also
1193 * listed in h->dev[], adding them if they aren't found
1194 */
1195
1196 for (i = 0; i < nsds; i++) {
1197 if (!sd[i]) /* if already added above. */
1198 continue;
1199 device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1200 h->ndevices, &entry);
1201 if (device_change == DEVICE_NOT_FOUND) {
1202 changes++;
1203 if (hpsa_scsi_add_entry(h, hostno, sd[i],
1204 added, &nadded) != 0)
1205 break;
1206 sd[i] = NULL; /* prevent from being freed later. */
1207 } else if (device_change == DEVICE_CHANGED) {
1208 /* should never happen... */
1209 changes++;
1210 dev_warn(&h->pdev->dev,
1211 "device unexpectedly changed.\n");
1212 /* but if it does happen, we just ignore that device */
1213 }
1214 }
1215 spin_unlock_irqrestore(&h->devlock, flags);
1216
1217 /* Don't notify scsi mid layer of any changes the first time through
1218 * (or if there are no changes) scsi_scan_host will do it later the
1219 * first time through.
1220 */
1221 if (hostno == -1 || !changes)
1222 goto free_and_out;
1223
1224 sh = h->scsi_host;
1225 /* Notify scsi mid layer of any removed devices */
1226 for (i = 0; i < nremoved; i++) {
1227 struct scsi_device *sdev =
1228 scsi_device_lookup(sh, removed[i]->bus,
1229 removed[i]->target, removed[i]->lun);
1230 if (sdev != NULL) {
1231 scsi_remove_device(sdev);
1232 scsi_device_put(sdev);
1233 } else {
1234 /* We don't expect to get here.
1235 * future cmds to this device will get selection
1236 * timeout as if the device was gone.
1237 */
1238 dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
1239 " for removal.", hostno, removed[i]->bus,
1240 removed[i]->target, removed[i]->lun);
1241 }
1242 kfree(removed[i]);
1243 removed[i] = NULL;
1244 }
1245
1246 /* Notify scsi mid layer of any added devices */
1247 for (i = 0; i < nadded; i++) {
1248 if (scsi_add_device(sh, added[i]->bus,
1249 added[i]->target, added[i]->lun) == 0)
1250 continue;
1251 dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
1252 "device not added.\n", hostno, added[i]->bus,
1253 added[i]->target, added[i]->lun);
1254 /* now we have to remove it from h->dev,
1255 * since it didn't get added to scsi mid layer
1256 */
1257 fixup_botched_add(h, added[i]);
1258 }
1259
1260free_and_out:
1261 kfree(added);
1262 kfree(removed);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001263}
1264
1265/*
Joe Perches9e03aa22013-09-03 13:45:58 -07001266 * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001267 * Assume's h->devlock is held.
1268 */
1269static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1270 int bus, int target, int lun)
1271{
1272 int i;
1273 struct hpsa_scsi_dev_t *sd;
1274
1275 for (i = 0; i < h->ndevices; i++) {
1276 sd = h->dev[i];
1277 if (sd->bus == bus && sd->target == target && sd->lun == lun)
1278 return sd;
1279 }
1280 return NULL;
1281}
1282
1283/* link sdev->hostdata to our per-device structure. */
1284static int hpsa_slave_alloc(struct scsi_device *sdev)
1285{
1286 struct hpsa_scsi_dev_t *sd;
1287 unsigned long flags;
1288 struct ctlr_info *h;
1289
1290 h = sdev_to_hba(sdev);
1291 spin_lock_irqsave(&h->devlock, flags);
1292 sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1293 sdev_id(sdev), sdev->lun);
1294 if (sd != NULL)
1295 sdev->hostdata = sd;
1296 spin_unlock_irqrestore(&h->devlock, flags);
1297 return 0;
1298}
1299
1300static void hpsa_slave_destroy(struct scsi_device *sdev)
1301{
Stephen M. Cameronbcc44252010-02-04 08:41:54 -06001302 /* nothing to do. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001303}
1304
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001305static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
1306{
1307 int i;
1308
1309 if (!h->cmd_sg_list)
1310 return;
1311 for (i = 0; i < h->nr_cmds; i++) {
1312 kfree(h->cmd_sg_list[i]);
1313 h->cmd_sg_list[i] = NULL;
1314 }
1315 kfree(h->cmd_sg_list);
1316 h->cmd_sg_list = NULL;
1317}
1318
1319static int hpsa_allocate_sg_chain_blocks(struct ctlr_info *h)
1320{
1321 int i;
1322
1323 if (h->chainsize <= 0)
1324 return 0;
1325
1326 h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
1327 GFP_KERNEL);
1328 if (!h->cmd_sg_list)
1329 return -ENOMEM;
1330 for (i = 0; i < h->nr_cmds; i++) {
1331 h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
1332 h->chainsize, GFP_KERNEL);
1333 if (!h->cmd_sg_list[i])
1334 goto clean;
1335 }
1336 return 0;
1337
1338clean:
1339 hpsa_free_sg_chain_blocks(h);
1340 return -ENOMEM;
1341}
1342
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001343static int hpsa_map_sg_chain_block(struct ctlr_info *h,
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001344 struct CommandList *c)
1345{
1346 struct SGDescriptor *chain_sg, *chain_block;
1347 u64 temp64;
1348
1349 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
1350 chain_block = h->cmd_sg_list[c->cmdindex];
1351 chain_sg->Ext = HPSA_SG_CHAIN;
1352 chain_sg->Len = sizeof(*chain_sg) *
1353 (c->Header.SGTotal - h->max_cmd_sg_entries);
1354 temp64 = pci_map_single(h->pdev, chain_block, chain_sg->Len,
1355 PCI_DMA_TODEVICE);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001356 if (dma_mapping_error(&h->pdev->dev, temp64)) {
1357 /* prevent subsequent unmapping */
1358 chain_sg->Addr.lower = 0;
1359 chain_sg->Addr.upper = 0;
1360 return -1;
1361 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001362 chain_sg->Addr.lower = (u32) (temp64 & 0x0FFFFFFFFULL);
1363 chain_sg->Addr.upper = (u32) ((temp64 >> 32) & 0x0FFFFFFFFULL);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001364 return 0;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001365}
1366
1367static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
1368 struct CommandList *c)
1369{
1370 struct SGDescriptor *chain_sg;
1371 union u64bit temp64;
1372
1373 if (c->Header.SGTotal <= h->max_cmd_sg_entries)
1374 return;
1375
1376 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
1377 temp64.val32.lower = chain_sg->Addr.lower;
1378 temp64.val32.upper = chain_sg->Addr.upper;
1379 pci_unmap_single(h->pdev, temp64.val, chain_sg->Len, PCI_DMA_TODEVICE);
1380}
1381
Scott Teela09c1442014-02-18 13:57:21 -06001382
1383/* Decode the various types of errors on ioaccel2 path.
1384 * Return 1 for any error that should generate a RAID path retry.
1385 * Return 0 for errors that don't require a RAID path retry.
1386 */
1387static int handle_ioaccel_mode2_error(struct ctlr_info *h,
Scott Teelc3497752014-02-18 13:56:34 -06001388 struct CommandList *c,
1389 struct scsi_cmnd *cmd,
1390 struct io_accel2_cmd *c2)
1391{
1392 int data_len;
Scott Teela09c1442014-02-18 13:57:21 -06001393 int retry = 0;
Scott Teelc3497752014-02-18 13:56:34 -06001394
1395 switch (c2->error_data.serv_response) {
1396 case IOACCEL2_SERV_RESPONSE_COMPLETE:
1397 switch (c2->error_data.status) {
1398 case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
1399 break;
1400 case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
1401 dev_warn(&h->pdev->dev,
1402 "%s: task complete with check condition.\n",
1403 "HP SSD Smart Path");
1404 if (c2->error_data.data_present !=
1405 IOACCEL2_SENSE_DATA_PRESENT)
1406 break;
1407 /* copy the sense data */
1408 data_len = c2->error_data.sense_data_len;
1409 if (data_len > SCSI_SENSE_BUFFERSIZE)
1410 data_len = SCSI_SENSE_BUFFERSIZE;
1411 if (data_len > sizeof(c2->error_data.sense_data_buff))
1412 data_len =
1413 sizeof(c2->error_data.sense_data_buff);
1414 memcpy(cmd->sense_buffer,
1415 c2->error_data.sense_data_buff, data_len);
1416 cmd->result |= SAM_STAT_CHECK_CONDITION;
Scott Teela09c1442014-02-18 13:57:21 -06001417 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001418 break;
1419 case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
1420 dev_warn(&h->pdev->dev,
1421 "%s: task complete with BUSY status.\n",
1422 "HP SSD Smart Path");
Scott Teela09c1442014-02-18 13:57:21 -06001423 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001424 break;
1425 case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
1426 dev_warn(&h->pdev->dev,
1427 "%s: task complete with reservation conflict.\n",
1428 "HP SSD Smart Path");
Scott Teela09c1442014-02-18 13:57:21 -06001429 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001430 break;
1431 case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
1432 /* Make scsi midlayer do unlimited retries */
1433 cmd->result = DID_IMM_RETRY << 16;
1434 break;
1435 case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
1436 dev_warn(&h->pdev->dev,
1437 "%s: task complete with aborted status.\n",
1438 "HP SSD Smart Path");
Scott Teela09c1442014-02-18 13:57:21 -06001439 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001440 break;
1441 default:
1442 dev_warn(&h->pdev->dev,
1443 "%s: task complete with unrecognized status: 0x%02x\n",
1444 "HP SSD Smart Path", c2->error_data.status);
Scott Teela09c1442014-02-18 13:57:21 -06001445 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001446 break;
1447 }
1448 break;
1449 case IOACCEL2_SERV_RESPONSE_FAILURE:
1450 /* don't expect to get here. */
1451 dev_warn(&h->pdev->dev,
1452 "unexpected delivery or target failure, status = 0x%02x\n",
1453 c2->error_data.status);
Scott Teela09c1442014-02-18 13:57:21 -06001454 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001455 break;
1456 case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
1457 break;
1458 case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
1459 break;
1460 case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
1461 dev_warn(&h->pdev->dev, "task management function rejected.\n");
Scott Teela09c1442014-02-18 13:57:21 -06001462 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001463 break;
1464 case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
1465 dev_warn(&h->pdev->dev, "task management function invalid LUN\n");
1466 break;
1467 default:
1468 dev_warn(&h->pdev->dev,
1469 "%s: Unrecognized server response: 0x%02x\n",
Scott Teela09c1442014-02-18 13:57:21 -06001470 "HP SSD Smart Path",
1471 c2->error_data.serv_response);
1472 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001473 break;
1474 }
Scott Teela09c1442014-02-18 13:57:21 -06001475
1476 return retry; /* retry on raid path? */
Scott Teelc3497752014-02-18 13:56:34 -06001477}
1478
1479static void process_ioaccel2_completion(struct ctlr_info *h,
1480 struct CommandList *c, struct scsi_cmnd *cmd,
1481 struct hpsa_scsi_dev_t *dev)
1482{
1483 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
Scott Teela09c1442014-02-18 13:57:21 -06001484 int raid_retry = 0;
Scott Teelc3497752014-02-18 13:56:34 -06001485
1486 /* check for good status */
1487 if (likely(c2->error_data.serv_response == 0 &&
1488 c2->error_data.status == 0)) {
1489 cmd_free(h, c);
1490 cmd->scsi_done(cmd);
1491 return;
1492 }
1493
1494 /* Any RAID offload error results in retry which will use
1495 * the normal I/O path so the controller can handle whatever's
1496 * wrong.
1497 */
1498 if (is_logical_dev_addr_mode(dev->scsi3addr) &&
1499 c2->error_data.serv_response ==
1500 IOACCEL2_SERV_RESPONSE_FAILURE) {
Scott Teela09c1442014-02-18 13:57:21 -06001501 if (c2->error_data.status ==
1502 IOACCEL2_STATUS_SR_IOACCEL_DISABLED)
Scott Teelc3497752014-02-18 13:56:34 -06001503 dev_warn(&h->pdev->dev,
Scott Teela09c1442014-02-18 13:57:21 -06001504 "%s: Path is unavailable, retrying on standard path.\n",
1505 "HP SSD Smart Path");
1506 else
1507 dev_warn(&h->pdev->dev,
1508 "%s: Error 0x%02x, retrying on standard path.\n",
Scott Teelc3497752014-02-18 13:56:34 -06001509 "HP SSD Smart Path", c2->error_data.status);
Scott Teela09c1442014-02-18 13:57:21 -06001510
Scott Teelc3497752014-02-18 13:56:34 -06001511 dev->offload_enabled = 0;
Scott Teele863d682014-02-18 13:57:05 -06001512 h->drv_req_rescan = 1; /* schedule controller for a rescan */
Scott Teelc3497752014-02-18 13:56:34 -06001513 cmd->result = DID_SOFT_ERROR << 16;
1514 cmd_free(h, c);
1515 cmd->scsi_done(cmd);
1516 return;
1517 }
Scott Teela09c1442014-02-18 13:57:21 -06001518 raid_retry = handle_ioaccel_mode2_error(h, c, cmd, c2);
1519 /* If error found, disable Smart Path, schedule a rescan,
1520 * and force a retry on the standard path.
1521 */
1522 if (raid_retry) {
1523 dev_warn(&h->pdev->dev, "%s: Retrying on standard path.\n",
1524 "HP SSD Smart Path");
1525 dev->offload_enabled = 0; /* Disable Smart Path */
1526 h->drv_req_rescan = 1; /* schedule controller rescan */
1527 cmd->result = DID_SOFT_ERROR << 16;
1528 }
Scott Teelc3497752014-02-18 13:56:34 -06001529 cmd_free(h, c);
1530 cmd->scsi_done(cmd);
1531}
1532
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05001533static void complete_scsi_command(struct CommandList *cp)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001534{
1535 struct scsi_cmnd *cmd;
1536 struct ctlr_info *h;
1537 struct ErrorInfo *ei;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001538 struct hpsa_scsi_dev_t *dev;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001539
1540 unsigned char sense_key;
1541 unsigned char asc; /* additional sense code */
1542 unsigned char ascq; /* additional sense code qualifier */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05001543 unsigned long sense_data_size;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001544
1545 ei = cp->err_info;
1546 cmd = (struct scsi_cmnd *) cp->scsi_cmd;
1547 h = cp->h;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001548 dev = cmd->device->hostdata;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001549
1550 scsi_dma_unmap(cmd); /* undo the DMA mappings */
Matt Gatese1f7de02014-02-18 13:55:17 -06001551 if ((cp->cmd_type == CMD_SCSI) &&
1552 (cp->Header.SGTotal > h->max_cmd_sg_entries))
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001553 hpsa_unmap_sg_chain_block(h, cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001554
1555 cmd->result = (DID_OK << 16); /* host byte */
1556 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
Scott Teelc3497752014-02-18 13:56:34 -06001557
1558 if (cp->cmd_type == CMD_IOACCEL2)
1559 return process_ioaccel2_completion(h, cp, cmd, dev);
1560
Stephen M. Cameron55126722010-02-25 14:03:01 -06001561 cmd->result |= ei->ScsiStatus;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001562
1563 /* copy the sense data whether we need to or not. */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05001564 if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
1565 sense_data_size = SCSI_SENSE_BUFFERSIZE;
1566 else
1567 sense_data_size = sizeof(ei->SenseInfo);
1568 if (ei->SenseLen < sense_data_size)
1569 sense_data_size = ei->SenseLen;
1570
1571 memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001572 scsi_set_resid(cmd, ei->ResidualCnt);
1573
1574 if (ei->CommandStatus == 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001575 cmd_free(h, cp);
Tomas Henzl2cc5bfa2013-08-01 15:14:00 +02001576 cmd->scsi_done(cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001577 return;
1578 }
1579
Matt Gatese1f7de02014-02-18 13:55:17 -06001580 /* For I/O accelerator commands, copy over some fields to the normal
1581 * CISS header used below for error handling.
1582 */
1583 if (cp->cmd_type == CMD_IOACCEL1) {
1584 struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
1585 cp->Header.SGList = cp->Header.SGTotal = scsi_sg_count(cmd);
1586 cp->Request.CDBLen = c->io_flags & IOACCEL1_IOFLAGS_CDBLEN_MASK;
1587 cp->Header.Tag.lower = c->Tag.lower;
1588 cp->Header.Tag.upper = c->Tag.upper;
1589 memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
1590 memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001591
1592 /* Any RAID offload error results in retry which will use
1593 * the normal I/O path so the controller can handle whatever's
1594 * wrong.
1595 */
1596 if (is_logical_dev_addr_mode(dev->scsi3addr)) {
1597 if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
1598 dev->offload_enabled = 0;
1599 cmd->result = DID_SOFT_ERROR << 16;
1600 cmd_free(h, cp);
1601 cmd->scsi_done(cmd);
1602 return;
1603 }
Matt Gatese1f7de02014-02-18 13:55:17 -06001604 }
1605
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001606 /* an error has occurred */
1607 switch (ei->CommandStatus) {
1608
1609 case CMD_TARGET_STATUS:
1610 if (ei->ScsiStatus) {
1611 /* Get sense key */
1612 sense_key = 0xf & ei->SenseInfo[2];
1613 /* Get additional sense code */
1614 asc = ei->SenseInfo[12];
1615 /* Get addition sense code qualifier */
1616 ascq = ei->SenseInfo[13];
1617 }
1618
1619 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
Matt Gates3ce438d2013-12-04 17:10:36 -06001620 if (check_for_unit_attention(h, cp))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001621 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001622 if (sense_key == ILLEGAL_REQUEST) {
1623 /*
1624 * SCSI REPORT_LUNS is commonly unsupported on
1625 * Smart Array. Suppress noisy complaint.
1626 */
1627 if (cp->Request.CDB[0] == REPORT_LUNS)
1628 break;
1629
1630 /* If ASC/ASCQ indicate Logical Unit
1631 * Not Supported condition,
1632 */
1633 if ((asc == 0x25) && (ascq == 0x0)) {
1634 dev_warn(&h->pdev->dev, "cp %p "
1635 "has check condition\n", cp);
1636 break;
1637 }
1638 }
1639
1640 if (sense_key == NOT_READY) {
1641 /* If Sense is Not Ready, Logical Unit
1642 * Not ready, Manual Intervention
1643 * required
1644 */
1645 if ((asc == 0x04) && (ascq == 0x03)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001646 dev_warn(&h->pdev->dev, "cp %p "
1647 "has check condition: unit "
1648 "not ready, manual "
1649 "intervention required\n", cp);
1650 break;
1651 }
1652 }
Matt Gates1d3b3602010-02-04 08:43:00 -06001653 if (sense_key == ABORTED_COMMAND) {
1654 /* Aborted command is retryable */
1655 dev_warn(&h->pdev->dev, "cp %p "
1656 "has check condition: aborted command: "
1657 "ASC: 0x%x, ASCQ: 0x%x\n",
1658 cp, asc, ascq);
Stephen M. Cameron2e311fb2013-09-23 13:33:41 -05001659 cmd->result |= DID_SOFT_ERROR << 16;
Matt Gates1d3b3602010-02-04 08:43:00 -06001660 break;
1661 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001662 /* Must be some other type of check condition */
Stephen M. Cameron21b8e4e2012-05-01 11:42:25 -05001663 dev_dbg(&h->pdev->dev, "cp %p has check condition: "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001664 "unknown type: "
1665 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1666 "Returning result: 0x%x, "
1667 "cmd=[%02x %02x %02x %02x %02x "
Mike Miller807be732010-02-04 08:43:26 -06001668 "%02x %02x %02x %02x %02x %02x "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001669 "%02x %02x %02x %02x %02x]\n",
1670 cp, sense_key, asc, ascq,
1671 cmd->result,
1672 cmd->cmnd[0], cmd->cmnd[1],
1673 cmd->cmnd[2], cmd->cmnd[3],
1674 cmd->cmnd[4], cmd->cmnd[5],
1675 cmd->cmnd[6], cmd->cmnd[7],
Mike Miller807be732010-02-04 08:43:26 -06001676 cmd->cmnd[8], cmd->cmnd[9],
1677 cmd->cmnd[10], cmd->cmnd[11],
1678 cmd->cmnd[12], cmd->cmnd[13],
1679 cmd->cmnd[14], cmd->cmnd[15]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001680 break;
1681 }
1682
1683
1684 /* Problem was not a check condition
1685 * Pass it up to the upper layers...
1686 */
1687 if (ei->ScsiStatus) {
1688 dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
1689 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1690 "Returning result: 0x%x\n",
1691 cp, ei->ScsiStatus,
1692 sense_key, asc, ascq,
1693 cmd->result);
1694 } else { /* scsi status is zero??? How??? */
1695 dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
1696 "Returning no connection.\n", cp),
1697
1698 /* Ordinarily, this case should never happen,
1699 * but there is a bug in some released firmware
1700 * revisions that allows it to happen if, for
1701 * example, a 4100 backplane loses power and
1702 * the tape drive is in it. We assume that
1703 * it's a fatal error of some kind because we
1704 * can't show that it wasn't. We will make it
1705 * look like selection timeout since that is
1706 * the most common reason for this to occur,
1707 * and it's severe enough.
1708 */
1709
1710 cmd->result = DID_NO_CONNECT << 16;
1711 }
1712 break;
1713
1714 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1715 break;
1716 case CMD_DATA_OVERRUN:
1717 dev_warn(&h->pdev->dev, "cp %p has"
1718 " completed with data overrun "
1719 "reported\n", cp);
1720 break;
1721 case CMD_INVALID: {
1722 /* print_bytes(cp, sizeof(*cp), 1, 0);
1723 print_cmd(cp); */
1724 /* We get CMD_INVALID if you address a non-existent device
1725 * instead of a selection timeout (no response). You will
1726 * see this if you yank out a drive, then try to access it.
1727 * This is kind of a shame because it means that any other
1728 * CMD_INVALID (e.g. driver bug) will get interpreted as a
1729 * missing target. */
1730 cmd->result = DID_NO_CONNECT << 16;
1731 }
1732 break;
1733 case CMD_PROTOCOL_ERR:
Stephen M. Cameron256d0ea2012-09-14 16:34:25 -05001734 cmd->result = DID_ERROR << 16;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001735 dev_warn(&h->pdev->dev, "cp %p has "
Stephen M. Cameron256d0ea2012-09-14 16:34:25 -05001736 "protocol error\n", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001737 break;
1738 case CMD_HARDWARE_ERR:
1739 cmd->result = DID_ERROR << 16;
1740 dev_warn(&h->pdev->dev, "cp %p had hardware error\n", cp);
1741 break;
1742 case CMD_CONNECTION_LOST:
1743 cmd->result = DID_ERROR << 16;
1744 dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
1745 break;
1746 case CMD_ABORTED:
1747 cmd->result = DID_ABORT << 16;
1748 dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
1749 cp, ei->ScsiStatus);
1750 break;
1751 case CMD_ABORT_FAILED:
1752 cmd->result = DID_ERROR << 16;
1753 dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
1754 break;
1755 case CMD_UNSOLICITED_ABORT:
Stephen M. Cameronf6e76052011-07-26 11:08:52 -05001756 cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
1757 dev_warn(&h->pdev->dev, "cp %p aborted due to an unsolicited "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001758 "abort\n", cp);
1759 break;
1760 case CMD_TIMEOUT:
1761 cmd->result = DID_TIME_OUT << 16;
1762 dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
1763 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06001764 case CMD_UNABORTABLE:
1765 cmd->result = DID_ERROR << 16;
1766 dev_warn(&h->pdev->dev, "Command unabortable\n");
1767 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001768 case CMD_IOACCEL_DISABLED:
1769 /* This only handles the direct pass-through case since RAID
1770 * offload is handled above. Just attempt a retry.
1771 */
1772 cmd->result = DID_SOFT_ERROR << 16;
1773 dev_warn(&h->pdev->dev,
1774 "cp %p had HP SSD Smart Path error\n", cp);
1775 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001776 default:
1777 cmd->result = DID_ERROR << 16;
1778 dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
1779 cp, ei->CommandStatus);
1780 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001781 cmd_free(h, cp);
Tomas Henzl2cc5bfa2013-08-01 15:14:00 +02001782 cmd->scsi_done(cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001783}
1784
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001785static void hpsa_pci_unmap(struct pci_dev *pdev,
1786 struct CommandList *c, int sg_used, int data_direction)
1787{
1788 int i;
1789 union u64bit addr64;
1790
1791 for (i = 0; i < sg_used; i++) {
1792 addr64.val32.lower = c->SG[i].Addr.lower;
1793 addr64.val32.upper = c->SG[i].Addr.upper;
1794 pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len,
1795 data_direction);
1796 }
1797}
1798
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001799static int hpsa_map_one(struct pci_dev *pdev,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001800 struct CommandList *cp,
1801 unsigned char *buf,
1802 size_t buflen,
1803 int data_direction)
1804{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001805 u64 addr64;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001806
1807 if (buflen == 0 || data_direction == PCI_DMA_NONE) {
1808 cp->Header.SGList = 0;
1809 cp->Header.SGTotal = 0;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001810 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001811 }
1812
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001813 addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction);
Shuah Khaneceaae12013-02-20 11:24:34 -06001814 if (dma_mapping_error(&pdev->dev, addr64)) {
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001815 /* Prevent subsequent unmap of something never mapped */
Shuah Khaneceaae12013-02-20 11:24:34 -06001816 cp->Header.SGList = 0;
1817 cp->Header.SGTotal = 0;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001818 return -1;
Shuah Khaneceaae12013-02-20 11:24:34 -06001819 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001820 cp->SG[0].Addr.lower =
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001821 (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001822 cp->SG[0].Addr.upper =
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001823 (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001824 cp->SG[0].Len = buflen;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06001825 cp->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining */
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001826 cp->Header.SGList = (u8) 1; /* no. SGs contig in this cmd */
1827 cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001828 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001829}
1830
1831static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
1832 struct CommandList *c)
1833{
1834 DECLARE_COMPLETION_ONSTACK(wait);
1835
1836 c->waiting = &wait;
1837 enqueue_cmd_and_start_io(h, c);
1838 wait_for_completion(&wait);
1839}
1840
Stephen M. Camerona0c12412011-10-26 16:22:04 -05001841static void hpsa_scsi_do_simple_cmd_core_if_no_lockup(struct ctlr_info *h,
1842 struct CommandList *c)
1843{
1844 unsigned long flags;
1845
1846 /* If controller lockup detected, fake a hardware error. */
1847 spin_lock_irqsave(&h->lock, flags);
1848 if (unlikely(h->lockup_detected)) {
1849 spin_unlock_irqrestore(&h->lock, flags);
1850 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
1851 } else {
1852 spin_unlock_irqrestore(&h->lock, flags);
1853 hpsa_scsi_do_simple_cmd_core(h, c);
1854 }
1855}
1856
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001857#define MAX_DRIVER_CMD_RETRIES 25
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001858static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
1859 struct CommandList *c, int data_direction)
1860{
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001861 int backoff_time = 10, retry_count = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001862
1863 do {
Joe Perches7630abd2011-05-08 23:32:40 -07001864 memset(c->err_info, 0, sizeof(*c->err_info));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001865 hpsa_scsi_do_simple_cmd_core(h, c);
1866 retry_count++;
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001867 if (retry_count > 3) {
1868 msleep(backoff_time);
1869 if (backoff_time < 1000)
1870 backoff_time *= 2;
1871 }
Matt Bondurant852af202012-05-01 11:42:35 -05001872 } while ((check_for_unit_attention(h, c) ||
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001873 check_for_busy(h, c)) &&
1874 retry_count <= MAX_DRIVER_CMD_RETRIES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001875 hpsa_pci_unmap(h->pdev, c, 1, data_direction);
1876}
1877
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001878static void hpsa_print_cmd(struct ctlr_info *h, char *txt,
1879 struct CommandList *c)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001880{
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001881 const u8 *cdb = c->Request.CDB;
1882 const u8 *lun = c->Header.LUN.LunAddrBytes;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001883
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001884 dev_warn(&h->pdev->dev, "%s: LUN:%02x%02x%02x%02x%02x%02x%02x%02x"
1885 " CDB:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
1886 txt, lun[0], lun[1], lun[2], lun[3],
1887 lun[4], lun[5], lun[6], lun[7],
1888 cdb[0], cdb[1], cdb[2], cdb[3],
1889 cdb[4], cdb[5], cdb[6], cdb[7],
1890 cdb[8], cdb[9], cdb[10], cdb[11],
1891 cdb[12], cdb[13], cdb[14], cdb[15]);
1892}
1893
1894static void hpsa_scsi_interpret_error(struct ctlr_info *h,
1895 struct CommandList *cp)
1896{
1897 const struct ErrorInfo *ei = cp->err_info;
1898 struct device *d = &cp->h->pdev->dev;
1899 const u8 *sd = ei->SenseInfo;
1900
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001901 switch (ei->CommandStatus) {
1902 case CMD_TARGET_STATUS:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001903 hpsa_print_cmd(h, "SCSI status", cp);
1904 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION)
1905 dev_warn(d, "SCSI Status = 02, Sense key = %02x, ASC = %02x, ASCQ = %02x\n",
1906 sd[2] & 0x0f, sd[12], sd[13]);
1907 else
1908 dev_warn(d, "SCSI Status = %02x\n", ei->ScsiStatus);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001909 if (ei->ScsiStatus == 0)
1910 dev_warn(d, "SCSI status is abnormally zero. "
1911 "(probably indicates selection timeout "
1912 "reported incorrectly due to a known "
1913 "firmware bug, circa July, 2001.)\n");
1914 break;
1915 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001916 break;
1917 case CMD_DATA_OVERRUN:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001918 hpsa_print_cmd(h, "overrun condition", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001919 break;
1920 case CMD_INVALID: {
1921 /* controller unfortunately reports SCSI passthru's
1922 * to non-existent targets as invalid commands.
1923 */
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001924 hpsa_print_cmd(h, "invalid command", cp);
1925 dev_warn(d, "probably means device no longer present\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001926 }
1927 break;
1928 case CMD_PROTOCOL_ERR:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001929 hpsa_print_cmd(h, "protocol error", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001930 break;
1931 case CMD_HARDWARE_ERR:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001932 hpsa_print_cmd(h, "hardware error", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001933 break;
1934 case CMD_CONNECTION_LOST:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001935 hpsa_print_cmd(h, "connection lost", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001936 break;
1937 case CMD_ABORTED:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001938 hpsa_print_cmd(h, "aborted", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001939 break;
1940 case CMD_ABORT_FAILED:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001941 hpsa_print_cmd(h, "abort failed", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001942 break;
1943 case CMD_UNSOLICITED_ABORT:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001944 hpsa_print_cmd(h, "unsolicited abort", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001945 break;
1946 case CMD_TIMEOUT:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001947 hpsa_print_cmd(h, "timed out", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001948 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06001949 case CMD_UNABORTABLE:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001950 hpsa_print_cmd(h, "unabortable", cp);
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06001951 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001952 default:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001953 hpsa_print_cmd(h, "unknown status", cp);
1954 dev_warn(d, "Unknown command status %x\n",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001955 ei->CommandStatus);
1956 }
1957}
1958
1959static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06001960 u16 page, unsigned char *buf,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001961 unsigned char bufsize)
1962{
1963 int rc = IO_OK;
1964 struct CommandList *c;
1965 struct ErrorInfo *ei;
1966
1967 c = cmd_special_alloc(h);
1968
1969 if (c == NULL) { /* trouble... */
1970 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06001971 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001972 }
1973
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001974 if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
1975 page, scsi3addr, TYPE_CMD)) {
1976 rc = -1;
1977 goto out;
1978 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001979 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1980 ei = c->err_info;
1981 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001982 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001983 rc = -1;
1984 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001985out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001986 cmd_special_free(h, c);
1987 return rc;
1988}
1989
Scott Teelbf711ac2014-02-18 13:56:39 -06001990static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
1991 u8 reset_type)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001992{
1993 int rc = IO_OK;
1994 struct CommandList *c;
1995 struct ErrorInfo *ei;
1996
1997 c = cmd_special_alloc(h);
1998
1999 if (c == NULL) { /* trouble... */
2000 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
Stephen M. Camerone9ea04a2010-02-25 14:03:06 -06002001 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002002 }
2003
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002004 /* fill_cmd can't fail here, no data buffer to map. */
Scott Teelbf711ac2014-02-18 13:56:39 -06002005 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
2006 scsi3addr, TYPE_MSG);
2007 c->Request.CDB[1] = reset_type; /* fill_cmd defaults to LUN reset */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002008 hpsa_scsi_do_simple_cmd_core(h, c);
2009 /* no unmap needed here because no data xfer. */
2010
2011 ei = c->err_info;
2012 if (ei->CommandStatus != 0) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002013 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002014 rc = -1;
2015 }
2016 cmd_special_free(h, c);
2017 return rc;
2018}
2019
2020static void hpsa_get_raid_level(struct ctlr_info *h,
2021 unsigned char *scsi3addr, unsigned char *raid_level)
2022{
2023 int rc;
2024 unsigned char *buf;
2025
2026 *raid_level = RAID_UNKNOWN;
2027 buf = kzalloc(64, GFP_KERNEL);
2028 if (!buf)
2029 return;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06002030 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0xC1, buf, 64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002031 if (rc == 0)
2032 *raid_level = buf[8];
2033 if (*raid_level > RAID_UNKNOWN)
2034 *raid_level = RAID_UNKNOWN;
2035 kfree(buf);
2036 return;
2037}
2038
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002039#define HPSA_MAP_DEBUG
2040#ifdef HPSA_MAP_DEBUG
2041static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
2042 struct raid_map_data *map_buff)
2043{
2044 struct raid_map_disk_data *dd = &map_buff->data[0];
2045 int map, row, col;
2046 u16 map_cnt, row_cnt, disks_per_row;
2047
2048 if (rc != 0)
2049 return;
2050
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06002051 /* Show details only if debugging has been activated. */
2052 if (h->raid_offload_debug < 2)
2053 return;
2054
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002055 dev_info(&h->pdev->dev, "structure_size = %u\n",
2056 le32_to_cpu(map_buff->structure_size));
2057 dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
2058 le32_to_cpu(map_buff->volume_blk_size));
2059 dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
2060 le64_to_cpu(map_buff->volume_blk_cnt));
2061 dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
2062 map_buff->phys_blk_shift);
2063 dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
2064 map_buff->parity_rotation_shift);
2065 dev_info(&h->pdev->dev, "strip_size = %u\n",
2066 le16_to_cpu(map_buff->strip_size));
2067 dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
2068 le64_to_cpu(map_buff->disk_starting_blk));
2069 dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
2070 le64_to_cpu(map_buff->disk_blk_cnt));
2071 dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
2072 le16_to_cpu(map_buff->data_disks_per_row));
2073 dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
2074 le16_to_cpu(map_buff->metadata_disks_per_row));
2075 dev_info(&h->pdev->dev, "row_cnt = %u\n",
2076 le16_to_cpu(map_buff->row_cnt));
2077 dev_info(&h->pdev->dev, "layout_map_count = %u\n",
2078 le16_to_cpu(map_buff->layout_map_count));
Scott Teeldd0e19f2014-02-18 13:57:31 -06002079 dev_info(&h->pdev->dev, "flags = %u\n",
2080 le16_to_cpu(map_buff->flags));
2081 if (map_buff->flags & RAID_MAP_FLAG_ENCRYPT_ON)
2082 dev_info(&h->pdev->dev, "encrypytion = ON\n");
2083 else
2084 dev_info(&h->pdev->dev, "encrypytion = OFF\n");
2085 dev_info(&h->pdev->dev, "dekindex = %u\n",
2086 le16_to_cpu(map_buff->dekindex));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002087
2088 map_cnt = le16_to_cpu(map_buff->layout_map_count);
2089 for (map = 0; map < map_cnt; map++) {
2090 dev_info(&h->pdev->dev, "Map%u:\n", map);
2091 row_cnt = le16_to_cpu(map_buff->row_cnt);
2092 for (row = 0; row < row_cnt; row++) {
2093 dev_info(&h->pdev->dev, " Row%u:\n", row);
2094 disks_per_row =
2095 le16_to_cpu(map_buff->data_disks_per_row);
2096 for (col = 0; col < disks_per_row; col++, dd++)
2097 dev_info(&h->pdev->dev,
2098 " D%02u: h=0x%04x xor=%u,%u\n",
2099 col, dd->ioaccel_handle,
2100 dd->xor_mult[0], dd->xor_mult[1]);
2101 disks_per_row =
2102 le16_to_cpu(map_buff->metadata_disks_per_row);
2103 for (col = 0; col < disks_per_row; col++, dd++)
2104 dev_info(&h->pdev->dev,
2105 " M%02u: h=0x%04x xor=%u,%u\n",
2106 col, dd->ioaccel_handle,
2107 dd->xor_mult[0], dd->xor_mult[1]);
2108 }
2109 }
2110}
2111#else
2112static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
2113 __attribute__((unused)) int rc,
2114 __attribute__((unused)) struct raid_map_data *map_buff)
2115{
2116}
2117#endif
2118
2119static int hpsa_get_raid_map(struct ctlr_info *h,
2120 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2121{
2122 int rc = 0;
2123 struct CommandList *c;
2124 struct ErrorInfo *ei;
2125
2126 c = cmd_special_alloc(h);
2127 if (c == NULL) {
2128 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2129 return -ENOMEM;
2130 }
2131 if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
2132 sizeof(this_device->raid_map), 0,
2133 scsi3addr, TYPE_CMD)) {
2134 dev_warn(&h->pdev->dev, "Out of memory in hpsa_get_raid_map()\n");
2135 cmd_special_free(h, c);
2136 return -ENOMEM;
2137 }
2138 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2139 ei = c->err_info;
2140 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002141 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002142 cmd_special_free(h, c);
2143 return -1;
2144 }
2145 cmd_special_free(h, c);
2146
2147 /* @todo in the future, dynamically allocate RAID map memory */
2148 if (le32_to_cpu(this_device->raid_map.structure_size) >
2149 sizeof(this_device->raid_map)) {
2150 dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
2151 rc = -1;
2152 }
2153 hpsa_debug_map_buff(h, rc, &this_device->raid_map);
2154 return rc;
2155}
2156
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06002157static int hpsa_vpd_page_supported(struct ctlr_info *h,
2158 unsigned char scsi3addr[], u8 page)
2159{
2160 int rc;
2161 int i;
2162 int pages;
2163 unsigned char *buf, bufsize;
2164
2165 buf = kzalloc(256, GFP_KERNEL);
2166 if (!buf)
2167 return 0;
2168
2169 /* Get the size of the page list first */
2170 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
2171 VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
2172 buf, HPSA_VPD_HEADER_SZ);
2173 if (rc != 0)
2174 goto exit_unsupported;
2175 pages = buf[3];
2176 if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
2177 bufsize = pages + HPSA_VPD_HEADER_SZ;
2178 else
2179 bufsize = 255;
2180
2181 /* Get the whole VPD page list */
2182 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
2183 VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
2184 buf, bufsize);
2185 if (rc != 0)
2186 goto exit_unsupported;
2187
2188 pages = buf[3];
2189 for (i = 1; i <= pages; i++)
2190 if (buf[3 + i] == page)
2191 goto exit_supported;
2192exit_unsupported:
2193 kfree(buf);
2194 return 0;
2195exit_supported:
2196 kfree(buf);
2197 return 1;
2198}
2199
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002200static void hpsa_get_ioaccel_status(struct ctlr_info *h,
2201 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2202{
2203 int rc;
2204 unsigned char *buf;
2205 u8 ioaccel_status;
2206
2207 this_device->offload_config = 0;
2208 this_device->offload_enabled = 0;
2209
2210 buf = kzalloc(64, GFP_KERNEL);
2211 if (!buf)
2212 return;
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06002213 if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
2214 goto out;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002215 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06002216 VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002217 if (rc != 0)
2218 goto out;
2219
2220#define IOACCEL_STATUS_BYTE 4
2221#define OFFLOAD_CONFIGURED_BIT 0x01
2222#define OFFLOAD_ENABLED_BIT 0x02
2223 ioaccel_status = buf[IOACCEL_STATUS_BYTE];
2224 this_device->offload_config =
2225 !!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
2226 if (this_device->offload_config) {
2227 this_device->offload_enabled =
2228 !!(ioaccel_status & OFFLOAD_ENABLED_BIT);
2229 if (hpsa_get_raid_map(h, scsi3addr, this_device))
2230 this_device->offload_enabled = 0;
2231 }
2232out:
2233 kfree(buf);
2234 return;
2235}
2236
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002237/* Get the device id from inquiry page 0x83 */
2238static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
2239 unsigned char *device_id, int buflen)
2240{
2241 int rc;
2242 unsigned char *buf;
2243
2244 if (buflen > 16)
2245 buflen = 16;
2246 buf = kzalloc(64, GFP_KERNEL);
2247 if (!buf)
2248 return -1;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06002249 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0x83, buf, 64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002250 if (rc == 0)
2251 memcpy(device_id, &buf[8], buflen);
2252 kfree(buf);
2253 return rc != 0;
2254}
2255
2256static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
2257 struct ReportLUNdata *buf, int bufsize,
2258 int extended_response)
2259{
2260 int rc = IO_OK;
2261 struct CommandList *c;
2262 unsigned char scsi3addr[8];
2263 struct ErrorInfo *ei;
2264
2265 c = cmd_special_alloc(h);
2266 if (c == NULL) { /* trouble... */
2267 dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2268 return -1;
2269 }
Stephen M. Camerone89c0ae2010-02-04 08:42:04 -06002270 /* address the controller */
2271 memset(scsi3addr, 0, sizeof(scsi3addr));
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002272 if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
2273 buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
2274 rc = -1;
2275 goto out;
2276 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002277 if (extended_response)
2278 c->Request.CDB[1] = extended_response;
2279 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2280 ei = c->err_info;
2281 if (ei->CommandStatus != 0 &&
2282 ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002283 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002284 rc = -1;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002285 } else {
2286 if (buf->extended_response_flag != extended_response) {
2287 dev_err(&h->pdev->dev,
2288 "report luns requested format %u, got %u\n",
2289 extended_response,
2290 buf->extended_response_flag);
2291 rc = -1;
2292 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002293 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002294out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002295 cmd_special_free(h, c);
2296 return rc;
2297}
2298
2299static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
2300 struct ReportLUNdata *buf,
2301 int bufsize, int extended_response)
2302{
2303 return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
2304}
2305
2306static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
2307 struct ReportLUNdata *buf, int bufsize)
2308{
2309 return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
2310}
2311
2312static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
2313 int bus, int target, int lun)
2314{
2315 device->bus = bus;
2316 device->target = target;
2317 device->lun = lun;
2318}
2319
2320static int hpsa_update_device_info(struct ctlr_info *h,
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002321 unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
2322 unsigned char *is_OBDR_device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002323{
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002324
2325#define OBDR_SIG_OFFSET 43
2326#define OBDR_TAPE_SIG "$DR-10"
2327#define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
2328#define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
2329
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06002330 unsigned char *inq_buff;
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002331 unsigned char *obdr_sig;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002332
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06002333 inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002334 if (!inq_buff)
2335 goto bail_out;
2336
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002337 /* Do an inquiry to the device to see what it is. */
2338 if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
2339 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
2340 /* Inquiry failed (msg printed already) */
2341 dev_err(&h->pdev->dev,
2342 "hpsa_update_device_info: inquiry failed\n");
2343 goto bail_out;
2344 }
2345
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002346 this_device->devtype = (inq_buff[0] & 0x1f);
2347 memcpy(this_device->scsi3addr, scsi3addr, 8);
2348 memcpy(this_device->vendor, &inq_buff[8],
2349 sizeof(this_device->vendor));
2350 memcpy(this_device->model, &inq_buff[16],
2351 sizeof(this_device->model));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002352 memset(this_device->device_id, 0,
2353 sizeof(this_device->device_id));
2354 hpsa_get_device_id(h, scsi3addr, this_device->device_id,
2355 sizeof(this_device->device_id));
2356
2357 if (this_device->devtype == TYPE_DISK &&
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002358 is_logical_dev_addr_mode(scsi3addr)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002359 hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002360 if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
2361 hpsa_get_ioaccel_status(h, scsi3addr, this_device);
2362 } else {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002363 this_device->raid_level = RAID_UNKNOWN;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002364 this_device->offload_config = 0;
2365 this_device->offload_enabled = 0;
2366 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002367
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002368 if (is_OBDR_device) {
2369 /* See if this is a One-Button-Disaster-Recovery device
2370 * by looking for "$DR-10" at offset 43 in inquiry data.
2371 */
2372 obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
2373 *is_OBDR_device = (this_device->devtype == TYPE_ROM &&
2374 strncmp(obdr_sig, OBDR_TAPE_SIG,
2375 OBDR_SIG_LEN) == 0);
2376 }
2377
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002378 kfree(inq_buff);
2379 return 0;
2380
2381bail_out:
2382 kfree(inq_buff);
2383 return 1;
2384}
2385
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002386static unsigned char *ext_target_model[] = {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002387 "MSA2012",
2388 "MSA2024",
2389 "MSA2312",
2390 "MSA2324",
Stephen M. Cameronfda38512011-05-03 15:00:07 -05002391 "P2000 G3 SAS",
Stephen M. Camerone06c8e52013-09-23 13:33:56 -05002392 "MSA 2040 SAS",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002393 NULL,
2394};
2395
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002396static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002397{
2398 int i;
2399
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002400 for (i = 0; ext_target_model[i]; i++)
2401 if (strncmp(device->model, ext_target_model[i],
2402 strlen(ext_target_model[i])) == 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002403 return 1;
2404 return 0;
2405}
2406
2407/* Helper function to assign bus, target, lun mapping of devices.
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002408 * Puts non-external target logical volumes on bus 0, external target logical
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002409 * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
2410 * Logical drive target and lun are assigned at this time, but
2411 * physical device lun and target assignment are deferred (assigned
2412 * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
2413 */
2414static void figure_bus_target_lun(struct ctlr_info *h,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002415 u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002416{
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002417 u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002418
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002419 if (!is_logical_dev_addr_mode(lunaddrbytes)) {
2420 /* physical device, target and lun filled in later */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002421 if (is_hba_lunid(lunaddrbytes))
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002422 hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002423 else
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002424 /* defer target, lun assignment for physical devices */
2425 hpsa_set_bus_target_lun(device, 2, -1, -1);
2426 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002427 }
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002428 /* It's a logical device */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002429 if (is_ext_target(h, device)) {
2430 /* external target way, put logicals on bus 1
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002431 * and match target/lun numbers box
2432 * reports, other smart array, bus 0, target 0, match lunid
2433 */
2434 hpsa_set_bus_target_lun(device,
2435 1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
2436 return;
2437 }
2438 hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002439}
2440
2441/*
2442 * If there is no lun 0 on a target, linux won't find any devices.
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002443 * For the external targets (arrays), we have to manually detect the enclosure
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002444 * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
2445 * it for some reason. *tmpdevice is the target we're adding,
2446 * this_device is a pointer into the current element of currentsd[]
2447 * that we're building up in update_scsi_devices(), below.
2448 * lunzerobits is a bitmap that tracks which targets already have a
2449 * lun 0 assigned.
2450 * Returns 1 if an enclosure was added, 0 if not.
2451 */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002452static int add_ext_target_dev(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002453 struct hpsa_scsi_dev_t *tmpdevice,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002454 struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002455 unsigned long lunzerobits[], int *n_ext_target_devs)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002456{
2457 unsigned char scsi3addr[8];
2458
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002459 if (test_bit(tmpdevice->target, lunzerobits))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002460 return 0; /* There is already a lun 0 on this target. */
2461
2462 if (!is_logical_dev_addr_mode(lunaddrbytes))
2463 return 0; /* It's the logical targets that may lack lun 0. */
2464
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002465 if (!is_ext_target(h, tmpdevice))
2466 return 0; /* Only external target devices have this problem. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002467
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002468 if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002469 return 0;
2470
Stephen M. Cameronc4f8a292011-01-07 10:55:43 -06002471 memset(scsi3addr, 0, 8);
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002472 scsi3addr[3] = tmpdevice->target;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002473 if (is_hba_lunid(scsi3addr))
2474 return 0; /* Don't add the RAID controller here. */
2475
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002476 if (is_scsi_rev_5(h))
2477 return 0; /* p1210m doesn't need to do this. */
2478
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002479 if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
Scott Teelaca4a522012-01-19 14:01:19 -06002480 dev_warn(&h->pdev->dev, "Maximum number of external "
2481 "target devices exceeded. Check your hardware "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002482 "configuration.");
2483 return 0;
2484 }
2485
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002486 if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002487 return 0;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002488 (*n_ext_target_devs)++;
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002489 hpsa_set_bus_target_lun(this_device,
2490 tmpdevice->bus, tmpdevice->target, 0);
2491 set_bit(tmpdevice->target, lunzerobits);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002492 return 1;
2493}
2494
2495/*
Scott Teel54b6e9e2014-02-18 13:56:45 -06002496 * Get address of physical disk used for an ioaccel2 mode command:
2497 * 1. Extract ioaccel2 handle from the command.
2498 * 2. Find a matching ioaccel2 handle from list of physical disks.
2499 * 3. Return:
2500 * 1 and set scsi3addr to address of matching physical
2501 * 0 if no matching physical disk was found.
2502 */
2503static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
2504 struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
2505{
2506 struct ReportExtendedLUNdata *physicals = NULL;
2507 int responsesize = 24; /* size of physical extended response */
2508 int extended = 2; /* flag forces reporting 'other dev info'. */
2509 int reportsize = sizeof(*physicals) + HPSA_MAX_PHYS_LUN * responsesize;
2510 u32 nphysicals = 0; /* number of reported physical devs */
2511 int found = 0; /* found match (1) or not (0) */
2512 u32 find; /* handle we need to match */
2513 int i;
2514 struct scsi_cmnd *scmd; /* scsi command within request being aborted */
2515 struct hpsa_scsi_dev_t *d; /* device of request being aborted */
2516 struct io_accel2_cmd *c2a; /* ioaccel2 command to abort */
2517 u32 it_nexus; /* 4 byte device handle for the ioaccel2 cmd */
2518 u32 scsi_nexus; /* 4 byte device handle for the ioaccel2 cmd */
2519
2520 if (ioaccel2_cmd_to_abort->cmd_type != CMD_IOACCEL2)
2521 return 0; /* no match */
2522
2523 /* point to the ioaccel2 device handle */
2524 c2a = &h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
2525 if (c2a == NULL)
2526 return 0; /* no match */
2527
2528 scmd = (struct scsi_cmnd *) ioaccel2_cmd_to_abort->scsi_cmd;
2529 if (scmd == NULL)
2530 return 0; /* no match */
2531
2532 d = scmd->device->hostdata;
2533 if (d == NULL)
2534 return 0; /* no match */
2535
2536 it_nexus = cpu_to_le32((u32) d->ioaccel_handle);
2537 scsi_nexus = cpu_to_le32((u32) c2a->scsi_nexus);
2538 find = c2a->scsi_nexus;
2539
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06002540 if (h->raid_offload_debug > 0)
2541 dev_info(&h->pdev->dev,
2542 "%s: scsi_nexus:0x%08x device id: 0x%02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
2543 __func__, scsi_nexus,
2544 d->device_id[0], d->device_id[1], d->device_id[2],
2545 d->device_id[3], d->device_id[4], d->device_id[5],
2546 d->device_id[6], d->device_id[7], d->device_id[8],
2547 d->device_id[9], d->device_id[10], d->device_id[11],
2548 d->device_id[12], d->device_id[13], d->device_id[14],
2549 d->device_id[15]);
2550
Scott Teel54b6e9e2014-02-18 13:56:45 -06002551 /* Get the list of physical devices */
2552 physicals = kzalloc(reportsize, GFP_KERNEL);
2553 if (hpsa_scsi_do_report_phys_luns(h, (struct ReportLUNdata *) physicals,
2554 reportsize, extended)) {
2555 dev_err(&h->pdev->dev,
2556 "Can't lookup %s device handle: report physical LUNs failed.\n",
2557 "HP SSD Smart Path");
2558 kfree(physicals);
2559 return 0;
2560 }
2561 nphysicals = be32_to_cpu(*((__be32 *)physicals->LUNListLength)) /
2562 responsesize;
2563
2564
2565 /* find ioaccel2 handle in list of physicals: */
2566 for (i = 0; i < nphysicals; i++) {
2567 /* handle is in bytes 28-31 of each lun */
2568 if (memcmp(&((struct ReportExtendedLUNdata *)
2569 physicals)->LUN[i][20], &find, 4) != 0) {
2570 continue; /* didn't match */
2571 }
2572 found = 1;
2573 memcpy(scsi3addr, &((struct ReportExtendedLUNdata *)
2574 physicals)->LUN[i][0], 8);
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06002575 if (h->raid_offload_debug > 0)
2576 dev_info(&h->pdev->dev,
2577 "%s: Searched h=0x%08x, Found h=0x%08x, scsiaddr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
2578 __func__, find,
2579 ((struct ReportExtendedLUNdata *)
2580 physicals)->LUN[i][20],
2581 scsi3addr[0], scsi3addr[1], scsi3addr[2],
2582 scsi3addr[3], scsi3addr[4], scsi3addr[5],
2583 scsi3addr[6], scsi3addr[7]);
Scott Teel54b6e9e2014-02-18 13:56:45 -06002584 break; /* found it */
2585 }
2586
2587 kfree(physicals);
2588 if (found)
2589 return 1;
2590 else
2591 return 0;
2592
2593}
2594/*
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002595 * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
2596 * logdev. The number of luns in physdev and logdev are returned in
2597 * *nphysicals and *nlogicals, respectively.
2598 * Returns 0 on success, -1 otherwise.
2599 */
2600static int hpsa_gather_lun_info(struct ctlr_info *h,
2601 int reportlunsize,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002602 struct ReportLUNdata *physdev, u32 *nphysicals, int *physical_mode,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002603 struct ReportLUNdata *logdev, u32 *nlogicals)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002604{
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002605 int physical_entry_size = 8;
2606
2607 *physical_mode = 0;
2608
2609 /* For I/O accelerator mode we need to read physical device handles */
Mike MIller317d4ad2014-02-18 13:56:20 -06002610 if (h->transMethod & CFGTBL_Trans_io_accel1 ||
2611 h->transMethod & CFGTBL_Trans_io_accel2) {
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002612 *physical_mode = HPSA_REPORT_PHYS_EXTENDED;
2613 physical_entry_size = 24;
2614 }
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002615 if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002616 *physical_mode)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002617 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
2618 return -1;
2619 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002620 *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) /
2621 physical_entry_size;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002622 if (*nphysicals > HPSA_MAX_PHYS_LUN) {
2623 dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
2624 " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2625 *nphysicals - HPSA_MAX_PHYS_LUN);
2626 *nphysicals = HPSA_MAX_PHYS_LUN;
2627 }
2628 if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
2629 dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
2630 return -1;
2631 }
Stephen M. Cameron6df1e952010-02-04 08:42:19 -06002632 *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002633 /* Reject Logicals in excess of our max capability. */
2634 if (*nlogicals > HPSA_MAX_LUN) {
2635 dev_warn(&h->pdev->dev,
2636 "maximum logical LUNs (%d) exceeded. "
2637 "%d LUNs ignored.\n", HPSA_MAX_LUN,
2638 *nlogicals - HPSA_MAX_LUN);
2639 *nlogicals = HPSA_MAX_LUN;
2640 }
2641 if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
2642 dev_warn(&h->pdev->dev,
2643 "maximum logical + physical LUNs (%d) exceeded. "
2644 "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2645 *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
2646 *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
2647 }
2648 return 0;
2649}
2650
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002651u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i,
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002652 int nphysicals, int nlogicals,
2653 struct ReportExtendedLUNdata *physdev_list,
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002654 struct ReportLUNdata *logdev_list)
2655{
2656 /* Helper function, figure out where the LUN ID info is coming from
2657 * given index i, lists of physical and logical devices, where in
2658 * the list the raid controller is supposed to appear (first or last)
2659 */
2660
2661 int logicals_start = nphysicals + (raid_ctlr_position == 0);
2662 int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
2663
2664 if (i == raid_ctlr_position)
2665 return RAID_CTLR_LUNID;
2666
2667 if (i < logicals_start)
2668 return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0];
2669
2670 if (i < last_device)
2671 return &logdev_list->LUN[i - nphysicals -
2672 (raid_ctlr_position == 0)][0];
2673 BUG();
2674 return NULL;
2675}
2676
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002677static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
2678{
2679 /* the idea here is we could get notified
2680 * that some devices have changed, so we do a report
2681 * physical luns and report logical luns cmd, and adjust
2682 * our list of devices accordingly.
2683 *
2684 * The scsi3addr's of devices won't change so long as the
2685 * adapter is not reset. That means we can rescan and
2686 * tell which devices we already know about, vs. new
2687 * devices, vs. disappearing devices.
2688 */
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002689 struct ReportExtendedLUNdata *physdev_list = NULL;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002690 struct ReportLUNdata *logdev_list = NULL;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002691 u32 nphysicals = 0;
2692 u32 nlogicals = 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002693 int physical_mode = 0;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002694 u32 ndev_allocated = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002695 struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
2696 int ncurrent = 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002697 int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 24;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002698 int i, n_ext_target_devs, ndevs_to_allocate;
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002699 int raid_ctlr_position;
Scott Teelaca4a522012-01-19 14:01:19 -06002700 DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002701
Scott Teelcfe5bad2011-10-26 16:21:07 -05002702 currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002703 physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
2704 logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002705 tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
2706
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002707 if (!currentsd || !physdev_list || !logdev_list || !tmpdevice) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002708 dev_err(&h->pdev->dev, "out of memory\n");
2709 goto out;
2710 }
2711 memset(lunzerobits, 0, sizeof(lunzerobits));
2712
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002713 if (hpsa_gather_lun_info(h, reportlunsize,
2714 (struct ReportLUNdata *) physdev_list, &nphysicals,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002715 &physical_mode, logdev_list, &nlogicals))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002716 goto out;
2717
Scott Teelaca4a522012-01-19 14:01:19 -06002718 /* We might see up to the maximum number of logical and physical disks
2719 * plus external target devices, and a device for the local RAID
2720 * controller.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002721 */
Scott Teelaca4a522012-01-19 14:01:19 -06002722 ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002723
2724 /* Allocate the per device structures */
2725 for (i = 0; i < ndevs_to_allocate; i++) {
Scott Teelb7ec0212011-10-26 16:21:12 -05002726 if (i >= HPSA_MAX_DEVICES) {
2727 dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
2728 " %d devices ignored.\n", HPSA_MAX_DEVICES,
2729 ndevs_to_allocate - HPSA_MAX_DEVICES);
2730 break;
2731 }
2732
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002733 currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
2734 if (!currentsd[i]) {
2735 dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
2736 __FILE__, __LINE__);
2737 goto out;
2738 }
2739 ndev_allocated++;
2740 }
2741
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002742 if (unlikely(is_scsi_rev_5(h)))
2743 raid_ctlr_position = 0;
2744 else
2745 raid_ctlr_position = nphysicals + nlogicals;
2746
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002747 /* adjust our table of devices */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002748 n_ext_target_devs = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002749 for (i = 0; i < nphysicals + nlogicals + 1; i++) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002750 u8 *lunaddrbytes, is_OBDR = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002751
2752 /* Figure out where the LUN ID info is coming from */
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002753 lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
2754 i, nphysicals, nlogicals, physdev_list, logdev_list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002755 /* skip masked physical devices. */
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002756 if (lunaddrbytes[3] & 0xC0 &&
2757 i < nphysicals + (raid_ctlr_position == 0))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002758 continue;
2759
2760 /* Get device type, vendor, model, device id */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002761 if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
2762 &is_OBDR))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002763 continue; /* skip it if we can't talk to it. */
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002764 figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002765 this_device = currentsd[ncurrent];
2766
2767 /*
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002768 * For external target devices, we have to insert a LUN 0 which
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002769 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
2770 * is nonetheless an enclosure device there. We have to
2771 * present that otherwise linux won't find anything if
2772 * there is no lun 0.
2773 */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002774 if (add_ext_target_dev(h, tmpdevice, this_device,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002775 lunaddrbytes, lunzerobits,
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002776 &n_ext_target_devs)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002777 ncurrent++;
2778 this_device = currentsd[ncurrent];
2779 }
2780
2781 *this_device = *tmpdevice;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002782
2783 switch (this_device->devtype) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002784 case TYPE_ROM:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002785 /* We don't *really* support actual CD-ROM devices,
2786 * just "One Button Disaster Recovery" tape drive
2787 * which temporarily pretends to be a CD-ROM drive.
2788 * So we check that the device is really an OBDR tape
2789 * device by checking for "$DR-10" in bytes 43-48 of
2790 * the inquiry data.
2791 */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002792 if (is_OBDR)
2793 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002794 break;
2795 case TYPE_DISK:
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002796 if (i >= nphysicals) {
2797 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002798 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002799 }
2800 if (physical_mode == HPSA_REPORT_PHYS_EXTENDED) {
2801 memcpy(&this_device->ioaccel_handle,
2802 &lunaddrbytes[20],
2803 sizeof(this_device->ioaccel_handle));
2804 ncurrent++;
2805 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002806 break;
2807 case TYPE_TAPE:
2808 case TYPE_MEDIUM_CHANGER:
2809 ncurrent++;
2810 break;
2811 case TYPE_RAID:
2812 /* Only present the Smartarray HBA as a RAID controller.
2813 * If it's a RAID controller other than the HBA itself
2814 * (an external RAID controller, MSA500 or similar)
2815 * don't present it.
2816 */
2817 if (!is_hba_lunid(lunaddrbytes))
2818 break;
2819 ncurrent++;
2820 break;
2821 default:
2822 break;
2823 }
Scott Teelcfe5bad2011-10-26 16:21:07 -05002824 if (ncurrent >= HPSA_MAX_DEVICES)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002825 break;
2826 }
2827 adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
2828out:
2829 kfree(tmpdevice);
2830 for (i = 0; i < ndev_allocated; i++)
2831 kfree(currentsd[i]);
2832 kfree(currentsd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002833 kfree(physdev_list);
2834 kfree(logdev_list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002835}
2836
2837/* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
2838 * dma mapping and fills in the scatter gather entries of the
2839 * hpsa command, cp.
2840 */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002841static int hpsa_scatter_gather(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002842 struct CommandList *cp,
2843 struct scsi_cmnd *cmd)
2844{
2845 unsigned int len;
2846 struct scatterlist *sg;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002847 u64 addr64;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002848 int use_sg, i, sg_index, chained;
2849 struct SGDescriptor *curr_sg;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002850
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002851 BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002852
2853 use_sg = scsi_dma_map(cmd);
2854 if (use_sg < 0)
2855 return use_sg;
2856
2857 if (!use_sg)
2858 goto sglist_finished;
2859
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002860 curr_sg = cp->SG;
2861 chained = 0;
2862 sg_index = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002863 scsi_for_each_sg(cmd, sg, use_sg, i) {
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002864 if (i == h->max_cmd_sg_entries - 1 &&
2865 use_sg > h->max_cmd_sg_entries) {
2866 chained = 1;
2867 curr_sg = h->cmd_sg_list[cp->cmdindex];
2868 sg_index = 0;
2869 }
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002870 addr64 = (u64) sg_dma_address(sg);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002871 len = sg_dma_len(sg);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002872 curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
2873 curr_sg->Addr.upper = (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
2874 curr_sg->Len = len;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06002875 curr_sg->Ext = (i < scsi_sg_count(cmd) - 1) ? 0 : HPSA_SG_LAST;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002876 curr_sg++;
2877 }
2878
2879 if (use_sg + chained > h->maxSG)
2880 h->maxSG = use_sg + chained;
2881
2882 if (chained) {
2883 cp->Header.SGList = h->max_cmd_sg_entries;
2884 cp->Header.SGTotal = (u16) (use_sg + 1);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002885 if (hpsa_map_sg_chain_block(h, cp)) {
2886 scsi_dma_unmap(cmd);
2887 return -1;
2888 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002889 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002890 }
2891
2892sglist_finished:
2893
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002894 cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
2895 cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002896 return 0;
2897}
2898
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002899#define IO_ACCEL_INELIGIBLE (1)
2900static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
2901{
2902 int is_write = 0;
2903 u32 block;
2904 u32 block_cnt;
2905
2906 /* Perform some CDB fixups if needed using 10 byte reads/writes only */
2907 switch (cdb[0]) {
2908 case WRITE_6:
2909 case WRITE_12:
2910 is_write = 1;
2911 case READ_6:
2912 case READ_12:
2913 if (*cdb_len == 6) {
2914 block = (((u32) cdb[2]) << 8) | cdb[3];
2915 block_cnt = cdb[4];
2916 } else {
2917 BUG_ON(*cdb_len != 12);
2918 block = (((u32) cdb[2]) << 24) |
2919 (((u32) cdb[3]) << 16) |
2920 (((u32) cdb[4]) << 8) |
2921 cdb[5];
2922 block_cnt =
2923 (((u32) cdb[6]) << 24) |
2924 (((u32) cdb[7]) << 16) |
2925 (((u32) cdb[8]) << 8) |
2926 cdb[9];
2927 }
2928 if (block_cnt > 0xffff)
2929 return IO_ACCEL_INELIGIBLE;
2930
2931 cdb[0] = is_write ? WRITE_10 : READ_10;
2932 cdb[1] = 0;
2933 cdb[2] = (u8) (block >> 24);
2934 cdb[3] = (u8) (block >> 16);
2935 cdb[4] = (u8) (block >> 8);
2936 cdb[5] = (u8) (block);
2937 cdb[6] = 0;
2938 cdb[7] = (u8) (block_cnt >> 8);
2939 cdb[8] = (u8) (block_cnt);
2940 cdb[9] = 0;
2941 *cdb_len = 10;
2942 break;
2943 }
2944 return 0;
2945}
2946
Scott Teelc3497752014-02-18 13:56:34 -06002947static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002948 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
2949 u8 *scsi3addr)
Matt Gatese1f7de02014-02-18 13:55:17 -06002950{
2951 struct scsi_cmnd *cmd = c->scsi_cmd;
Matt Gatese1f7de02014-02-18 13:55:17 -06002952 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
2953 unsigned int len;
2954 unsigned int total_len = 0;
2955 struct scatterlist *sg;
2956 u64 addr64;
2957 int use_sg, i;
2958 struct SGDescriptor *curr_sg;
2959 u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
2960
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002961 /* TODO: implement chaining support */
2962 if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
2963 return IO_ACCEL_INELIGIBLE;
2964
Matt Gatese1f7de02014-02-18 13:55:17 -06002965 BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
2966
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002967 if (fixup_ioaccel_cdb(cdb, &cdb_len))
2968 return IO_ACCEL_INELIGIBLE;
2969
Matt Gatese1f7de02014-02-18 13:55:17 -06002970 c->cmd_type = CMD_IOACCEL1;
2971
2972 /* Adjust the DMA address to point to the accelerated command buffer */
2973 c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
2974 (c->cmdindex * sizeof(*cp));
2975 BUG_ON(c->busaddr & 0x0000007F);
2976
2977 use_sg = scsi_dma_map(cmd);
2978 if (use_sg < 0)
2979 return use_sg;
2980
2981 if (use_sg) {
2982 curr_sg = cp->SG;
2983 scsi_for_each_sg(cmd, sg, use_sg, i) {
2984 addr64 = (u64) sg_dma_address(sg);
2985 len = sg_dma_len(sg);
2986 total_len += len;
2987 curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
2988 curr_sg->Addr.upper =
2989 (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
2990 curr_sg->Len = len;
2991
2992 if (i == (scsi_sg_count(cmd) - 1))
2993 curr_sg->Ext = HPSA_SG_LAST;
2994 else
2995 curr_sg->Ext = 0; /* we are not chaining */
2996 curr_sg++;
2997 }
2998
2999 switch (cmd->sc_data_direction) {
3000 case DMA_TO_DEVICE:
3001 control |= IOACCEL1_CONTROL_DATA_OUT;
3002 break;
3003 case DMA_FROM_DEVICE:
3004 control |= IOACCEL1_CONTROL_DATA_IN;
3005 break;
3006 case DMA_NONE:
3007 control |= IOACCEL1_CONTROL_NODATAXFER;
3008 break;
3009 default:
3010 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3011 cmd->sc_data_direction);
3012 BUG();
3013 break;
3014 }
3015 } else {
3016 control |= IOACCEL1_CONTROL_NODATAXFER;
3017 }
3018
Scott Teelc3497752014-02-18 13:56:34 -06003019 c->Header.SGList = use_sg;
Matt Gatese1f7de02014-02-18 13:55:17 -06003020 /* Fill out the command structure to submit */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003021 cp->dev_handle = ioaccel_handle & 0xFFFF;
Matt Gatese1f7de02014-02-18 13:55:17 -06003022 cp->transfer_len = total_len;
3023 cp->io_flags = IOACCEL1_IOFLAGS_IO_REQ |
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003024 (cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK);
Matt Gatese1f7de02014-02-18 13:55:17 -06003025 cp->control = control;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003026 memcpy(cp->CDB, cdb, cdb_len);
3027 memcpy(cp->CISS_LUN, scsi3addr, 8);
Scott Teelc3497752014-02-18 13:56:34 -06003028 /* Tag was already set at init time. */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003029 enqueue_cmd_and_start_io(h, c);
Matt Gatese1f7de02014-02-18 13:55:17 -06003030 return 0;
3031}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003032
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003033/*
3034 * Queue a command directly to a device behind the controller using the
3035 * I/O accelerator path.
3036 */
3037static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
3038 struct CommandList *c)
3039{
3040 struct scsi_cmnd *cmd = c->scsi_cmd;
3041 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3042
3043 return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
3044 cmd->cmnd, cmd->cmd_len, dev->scsi3addr);
3045}
3046
Scott Teeldd0e19f2014-02-18 13:57:31 -06003047/*
3048 * Set encryption parameters for the ioaccel2 request
3049 */
3050static void set_encrypt_ioaccel2(struct ctlr_info *h,
3051 struct CommandList *c, struct io_accel2_cmd *cp)
3052{
3053 struct scsi_cmnd *cmd = c->scsi_cmd;
3054 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3055 struct raid_map_data *map = &dev->raid_map;
3056 u64 first_block;
3057
3058 BUG_ON(!(dev->offload_config && dev->offload_enabled));
3059
3060 /* Are we doing encryption on this device */
3061 if (!(map->flags & RAID_MAP_FLAG_ENCRYPT_ON))
3062 return;
3063 /* Set the data encryption key index. */
3064 cp->dekindex = map->dekindex;
3065
3066 /* Set the encryption enable flag, encoded into direction field. */
3067 cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
3068
3069 /* Set encryption tweak values based on logical block address
3070 * If block size is 512, tweak value is LBA.
3071 * For other block sizes, tweak is (LBA * block size)/ 512)
3072 */
3073 switch (cmd->cmnd[0]) {
3074 /* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
3075 case WRITE_6:
3076 case READ_6:
3077 if (map->volume_blk_size == 512) {
3078 cp->tweak_lower =
3079 (((u32) cmd->cmnd[2]) << 8) |
3080 cmd->cmnd[3];
3081 cp->tweak_upper = 0;
3082 } else {
3083 first_block =
3084 (((u64) cmd->cmnd[2]) << 8) |
3085 cmd->cmnd[3];
3086 first_block = (first_block * map->volume_blk_size)/512;
3087 cp->tweak_lower = (u32)first_block;
3088 cp->tweak_upper = (u32)(first_block >> 32);
3089 }
3090 break;
3091 case WRITE_10:
3092 case READ_10:
3093 if (map->volume_blk_size == 512) {
3094 cp->tweak_lower =
3095 (((u32) cmd->cmnd[2]) << 24) |
3096 (((u32) cmd->cmnd[3]) << 16) |
3097 (((u32) cmd->cmnd[4]) << 8) |
3098 cmd->cmnd[5];
3099 cp->tweak_upper = 0;
3100 } else {
3101 first_block =
3102 (((u64) cmd->cmnd[2]) << 24) |
3103 (((u64) cmd->cmnd[3]) << 16) |
3104 (((u64) cmd->cmnd[4]) << 8) |
3105 cmd->cmnd[5];
3106 first_block = (first_block * map->volume_blk_size)/512;
3107 cp->tweak_lower = (u32)first_block;
3108 cp->tweak_upper = (u32)(first_block >> 32);
3109 }
3110 break;
3111 /* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
3112 case WRITE_12:
3113 case READ_12:
3114 if (map->volume_blk_size == 512) {
3115 cp->tweak_lower =
3116 (((u32) cmd->cmnd[2]) << 24) |
3117 (((u32) cmd->cmnd[3]) << 16) |
3118 (((u32) cmd->cmnd[4]) << 8) |
3119 cmd->cmnd[5];
3120 cp->tweak_upper = 0;
3121 } else {
3122 first_block =
3123 (((u64) cmd->cmnd[2]) << 24) |
3124 (((u64) cmd->cmnd[3]) << 16) |
3125 (((u64) cmd->cmnd[4]) << 8) |
3126 cmd->cmnd[5];
3127 first_block = (first_block * map->volume_blk_size)/512;
3128 cp->tweak_lower = (u32)first_block;
3129 cp->tweak_upper = (u32)(first_block >> 32);
3130 }
3131 break;
3132 case WRITE_16:
3133 case READ_16:
3134 if (map->volume_blk_size == 512) {
3135 cp->tweak_lower =
3136 (((u32) cmd->cmnd[6]) << 24) |
3137 (((u32) cmd->cmnd[7]) << 16) |
3138 (((u32) cmd->cmnd[8]) << 8) |
3139 cmd->cmnd[9];
3140 cp->tweak_upper =
3141 (((u32) cmd->cmnd[2]) << 24) |
3142 (((u32) cmd->cmnd[3]) << 16) |
3143 (((u32) cmd->cmnd[4]) << 8) |
3144 cmd->cmnd[5];
3145 } else {
3146 first_block =
3147 (((u64) cmd->cmnd[2]) << 56) |
3148 (((u64) cmd->cmnd[3]) << 48) |
3149 (((u64) cmd->cmnd[4]) << 40) |
3150 (((u64) cmd->cmnd[5]) << 32) |
3151 (((u64) cmd->cmnd[6]) << 24) |
3152 (((u64) cmd->cmnd[7]) << 16) |
3153 (((u64) cmd->cmnd[8]) << 8) |
3154 cmd->cmnd[9];
3155 first_block = (first_block * map->volume_blk_size)/512;
3156 cp->tweak_lower = (u32)first_block;
3157 cp->tweak_upper = (u32)(first_block >> 32);
3158 }
3159 break;
3160 default:
3161 dev_err(&h->pdev->dev,
3162 "ERROR: %s: IOACCEL request CDB size not supported for encryption\n",
3163 __func__);
3164 BUG();
3165 break;
3166 }
3167}
3168
Scott Teelc3497752014-02-18 13:56:34 -06003169static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
3170 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
3171 u8 *scsi3addr)
3172{
3173 struct scsi_cmnd *cmd = c->scsi_cmd;
3174 struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
3175 struct ioaccel2_sg_element *curr_sg;
3176 int use_sg, i;
3177 struct scatterlist *sg;
3178 u64 addr64;
3179 u32 len;
3180 u32 total_len = 0;
3181
3182 if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
3183 return IO_ACCEL_INELIGIBLE;
3184
3185 if (fixup_ioaccel_cdb(cdb, &cdb_len))
3186 return IO_ACCEL_INELIGIBLE;
3187 c->cmd_type = CMD_IOACCEL2;
3188 /* Adjust the DMA address to point to the accelerated command buffer */
3189 c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
3190 (c->cmdindex * sizeof(*cp));
3191 BUG_ON(c->busaddr & 0x0000007F);
3192
3193 memset(cp, 0, sizeof(*cp));
3194 cp->IU_type = IOACCEL2_IU_TYPE;
3195
3196 use_sg = scsi_dma_map(cmd);
3197 if (use_sg < 0)
3198 return use_sg;
3199
3200 if (use_sg) {
3201 BUG_ON(use_sg > IOACCEL2_MAXSGENTRIES);
3202 curr_sg = cp->sg;
3203 scsi_for_each_sg(cmd, sg, use_sg, i) {
3204 addr64 = (u64) sg_dma_address(sg);
3205 len = sg_dma_len(sg);
3206 total_len += len;
3207 curr_sg->address = cpu_to_le64(addr64);
3208 curr_sg->length = cpu_to_le32(len);
3209 curr_sg->reserved[0] = 0;
3210 curr_sg->reserved[1] = 0;
3211 curr_sg->reserved[2] = 0;
3212 curr_sg->chain_indicator = 0;
3213 curr_sg++;
3214 }
3215
3216 switch (cmd->sc_data_direction) {
3217 case DMA_TO_DEVICE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06003218 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3219 cp->direction |= IOACCEL2_DIR_DATA_OUT;
Scott Teelc3497752014-02-18 13:56:34 -06003220 break;
3221 case DMA_FROM_DEVICE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06003222 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3223 cp->direction |= IOACCEL2_DIR_DATA_IN;
Scott Teelc3497752014-02-18 13:56:34 -06003224 break;
3225 case DMA_NONE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06003226 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3227 cp->direction |= IOACCEL2_DIR_NO_DATA;
Scott Teelc3497752014-02-18 13:56:34 -06003228 break;
3229 default:
3230 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3231 cmd->sc_data_direction);
3232 BUG();
3233 break;
3234 }
3235 } else {
Scott Teeldd0e19f2014-02-18 13:57:31 -06003236 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3237 cp->direction |= IOACCEL2_DIR_NO_DATA;
Scott Teelc3497752014-02-18 13:56:34 -06003238 }
Scott Teeldd0e19f2014-02-18 13:57:31 -06003239
3240 /* Set encryption parameters, if necessary */
3241 set_encrypt_ioaccel2(h, c, cp);
3242
Scott Teelc3497752014-02-18 13:56:34 -06003243 cp->scsi_nexus = ioaccel_handle;
Scott Teeldd0e19f2014-02-18 13:57:31 -06003244 cp->Tag = (c->cmdindex << DIRECT_LOOKUP_SHIFT) |
Scott Teelc3497752014-02-18 13:56:34 -06003245 DIRECT_LOOKUP_BIT;
3246 memcpy(cp->cdb, cdb, sizeof(cp->cdb));
3247 memset(cp->cciss_lun, 0, sizeof(cp->cciss_lun));
3248 cp->cmd_priority_task_attr = 0;
3249
3250 /* fill in sg elements */
3251 cp->sg_count = (u8) use_sg;
3252
3253 cp->data_len = cpu_to_le32(total_len);
3254 cp->err_ptr = cpu_to_le64(c->busaddr +
3255 offsetof(struct io_accel2_cmd, error_data));
3256 cp->err_len = cpu_to_le32((u32) sizeof(cp->error_data));
3257
3258 enqueue_cmd_and_start_io(h, c);
3259 return 0;
3260}
3261
3262/*
3263 * Queue a command to the correct I/O accelerator path.
3264 */
3265static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
3266 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
3267 u8 *scsi3addr)
3268{
3269 if (h->transMethod & CFGTBL_Trans_io_accel1)
3270 return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
3271 cdb, cdb_len, scsi3addr);
3272 else
3273 return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
3274 cdb, cdb_len, scsi3addr);
3275}
3276
Scott Teel6b80b182014-02-18 13:56:55 -06003277static void raid_map_helper(struct raid_map_data *map,
3278 int offload_to_mirror, u32 *map_index, u32 *current_group)
3279{
3280 if (offload_to_mirror == 0) {
3281 /* use physical disk in the first mirrored group. */
3282 *map_index %= map->data_disks_per_row;
3283 return;
3284 }
3285 do {
3286 /* determine mirror group that *map_index indicates */
3287 *current_group = *map_index / map->data_disks_per_row;
3288 if (offload_to_mirror == *current_group)
3289 continue;
3290 if (*current_group < (map->layout_map_count - 1)) {
3291 /* select map index from next group */
3292 *map_index += map->data_disks_per_row;
3293 (*current_group)++;
3294 } else {
3295 /* select map index from first group */
3296 *map_index %= map->data_disks_per_row;
3297 *current_group = 0;
3298 }
3299 } while (offload_to_mirror != *current_group);
3300}
3301
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003302/*
3303 * Attempt to perform offload RAID mapping for a logical volume I/O.
3304 */
3305static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
3306 struct CommandList *c)
3307{
3308 struct scsi_cmnd *cmd = c->scsi_cmd;
3309 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3310 struct raid_map_data *map = &dev->raid_map;
3311 struct raid_map_disk_data *dd = &map->data[0];
3312 int is_write = 0;
3313 u32 map_index;
3314 u64 first_block, last_block;
3315 u32 block_cnt;
3316 u32 blocks_per_row;
3317 u64 first_row, last_row;
3318 u32 first_row_offset, last_row_offset;
3319 u32 first_column, last_column;
Scott Teel6b80b182014-02-18 13:56:55 -06003320 u64 r0_first_row, r0_last_row;
3321 u32 r5or6_blocks_per_row;
3322 u64 r5or6_first_row, r5or6_last_row;
3323 u32 r5or6_first_row_offset, r5or6_last_row_offset;
3324 u32 r5or6_first_column, r5or6_last_column;
3325 u32 total_disks_per_row;
3326 u32 stripesize;
3327 u32 first_group, last_group, current_group;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003328 u32 map_row;
3329 u32 disk_handle;
3330 u64 disk_block;
3331 u32 disk_block_cnt;
3332 u8 cdb[16];
3333 u8 cdb_len;
3334#if BITS_PER_LONG == 32
3335 u64 tmpdiv;
3336#endif
Scott Teel6b80b182014-02-18 13:56:55 -06003337 int offload_to_mirror;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003338
3339 BUG_ON(!(dev->offload_config && dev->offload_enabled));
3340
3341 /* check for valid opcode, get LBA and block count */
3342 switch (cmd->cmnd[0]) {
3343 case WRITE_6:
3344 is_write = 1;
3345 case READ_6:
3346 first_block =
3347 (((u64) cmd->cmnd[2]) << 8) |
3348 cmd->cmnd[3];
3349 block_cnt = cmd->cmnd[4];
3350 break;
3351 case WRITE_10:
3352 is_write = 1;
3353 case READ_10:
3354 first_block =
3355 (((u64) cmd->cmnd[2]) << 24) |
3356 (((u64) cmd->cmnd[3]) << 16) |
3357 (((u64) cmd->cmnd[4]) << 8) |
3358 cmd->cmnd[5];
3359 block_cnt =
3360 (((u32) cmd->cmnd[7]) << 8) |
3361 cmd->cmnd[8];
3362 break;
3363 case WRITE_12:
3364 is_write = 1;
3365 case READ_12:
3366 first_block =
3367 (((u64) cmd->cmnd[2]) << 24) |
3368 (((u64) cmd->cmnd[3]) << 16) |
3369 (((u64) cmd->cmnd[4]) << 8) |
3370 cmd->cmnd[5];
3371 block_cnt =
3372 (((u32) cmd->cmnd[6]) << 24) |
3373 (((u32) cmd->cmnd[7]) << 16) |
3374 (((u32) cmd->cmnd[8]) << 8) |
3375 cmd->cmnd[9];
3376 break;
3377 case WRITE_16:
3378 is_write = 1;
3379 case READ_16:
3380 first_block =
3381 (((u64) cmd->cmnd[2]) << 56) |
3382 (((u64) cmd->cmnd[3]) << 48) |
3383 (((u64) cmd->cmnd[4]) << 40) |
3384 (((u64) cmd->cmnd[5]) << 32) |
3385 (((u64) cmd->cmnd[6]) << 24) |
3386 (((u64) cmd->cmnd[7]) << 16) |
3387 (((u64) cmd->cmnd[8]) << 8) |
3388 cmd->cmnd[9];
3389 block_cnt =
3390 (((u32) cmd->cmnd[10]) << 24) |
3391 (((u32) cmd->cmnd[11]) << 16) |
3392 (((u32) cmd->cmnd[12]) << 8) |
3393 cmd->cmnd[13];
3394 break;
3395 default:
3396 return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
3397 }
3398 BUG_ON(block_cnt == 0);
3399 last_block = first_block + block_cnt - 1;
3400
3401 /* check for write to non-RAID-0 */
3402 if (is_write && dev->raid_level != 0)
3403 return IO_ACCEL_INELIGIBLE;
3404
3405 /* check for invalid block or wraparound */
3406 if (last_block >= map->volume_blk_cnt || last_block < first_block)
3407 return IO_ACCEL_INELIGIBLE;
3408
3409 /* calculate stripe information for the request */
3410 blocks_per_row = map->data_disks_per_row * map->strip_size;
3411#if BITS_PER_LONG == 32
3412 tmpdiv = first_block;
3413 (void) do_div(tmpdiv, blocks_per_row);
3414 first_row = tmpdiv;
3415 tmpdiv = last_block;
3416 (void) do_div(tmpdiv, blocks_per_row);
3417 last_row = tmpdiv;
3418 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
3419 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
3420 tmpdiv = first_row_offset;
3421 (void) do_div(tmpdiv, map->strip_size);
3422 first_column = tmpdiv;
3423 tmpdiv = last_row_offset;
3424 (void) do_div(tmpdiv, map->strip_size);
3425 last_column = tmpdiv;
3426#else
3427 first_row = first_block / blocks_per_row;
3428 last_row = last_block / blocks_per_row;
3429 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
3430 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
3431 first_column = first_row_offset / map->strip_size;
3432 last_column = last_row_offset / map->strip_size;
3433#endif
3434
3435 /* if this isn't a single row/column then give to the controller */
3436 if ((first_row != last_row) || (first_column != last_column))
3437 return IO_ACCEL_INELIGIBLE;
3438
3439 /* proceeding with driver mapping */
Scott Teel6b80b182014-02-18 13:56:55 -06003440 total_disks_per_row = map->data_disks_per_row +
3441 map->metadata_disks_per_row;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003442 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
3443 map->row_cnt;
Scott Teel6b80b182014-02-18 13:56:55 -06003444 map_index = (map_row * total_disks_per_row) + first_column;
3445
3446 switch (dev->raid_level) {
3447 case HPSA_RAID_0:
3448 break; /* nothing special to do */
3449 case HPSA_RAID_1:
3450 /* Handles load balance across RAID 1 members.
3451 * (2-drive R1 and R10 with even # of drives.)
3452 * Appropriate for SSDs, not optimal for HDDs
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003453 */
Scott Teel6b80b182014-02-18 13:56:55 -06003454 BUG_ON(map->layout_map_count != 2);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003455 if (dev->offload_to_mirror)
3456 map_index += map->data_disks_per_row;
3457 dev->offload_to_mirror = !dev->offload_to_mirror;
Scott Teel6b80b182014-02-18 13:56:55 -06003458 break;
3459 case HPSA_RAID_ADM:
3460 /* Handles N-way mirrors (R1-ADM)
3461 * and R10 with # of drives divisible by 3.)
3462 */
3463 BUG_ON(map->layout_map_count != 3);
3464
3465 offload_to_mirror = dev->offload_to_mirror;
3466 raid_map_helper(map, offload_to_mirror,
3467 &map_index, &current_group);
3468 /* set mirror group to use next time */
3469 offload_to_mirror =
3470 (offload_to_mirror >= map->layout_map_count - 1)
3471 ? 0 : offload_to_mirror + 1;
3472 /* FIXME: remove after debug/dev */
3473 BUG_ON(offload_to_mirror >= map->layout_map_count);
3474 dev_warn(&h->pdev->dev,
3475 "DEBUG: Using physical disk map index %d from mirror group %d\n",
3476 map_index, offload_to_mirror);
3477 dev->offload_to_mirror = offload_to_mirror;
3478 /* Avoid direct use of dev->offload_to_mirror within this
3479 * function since multiple threads might simultaneously
3480 * increment it beyond the range of dev->layout_map_count -1.
3481 */
3482 break;
3483 case HPSA_RAID_5:
3484 case HPSA_RAID_6:
3485 if (map->layout_map_count <= 1)
3486 break;
3487
3488 /* Verify first and last block are in same RAID group */
3489 r5or6_blocks_per_row =
3490 map->strip_size * map->data_disks_per_row;
3491 BUG_ON(r5or6_blocks_per_row == 0);
3492 stripesize = r5or6_blocks_per_row * map->layout_map_count;
3493#if BITS_PER_LONG == 32
3494 tmpdiv = first_block;
3495 first_group = do_div(tmpdiv, stripesize);
3496 tmpdiv = first_group;
3497 (void) do_div(tmpdiv, r5or6_blocks_per_row);
3498 first_group = tmpdiv;
3499 tmpdiv = last_block;
3500 last_group = do_div(tmpdiv, stripesize);
3501 tmpdiv = last_group;
3502 (void) do_div(tmpdiv, r5or6_blocks_per_row);
3503 last_group = tmpdiv;
3504#else
3505 first_group = (first_block % stripesize) / r5or6_blocks_per_row;
3506 last_group = (last_block % stripesize) / r5or6_blocks_per_row;
3507 if (first_group != last_group)
3508#endif
3509 return IO_ACCEL_INELIGIBLE;
3510
3511 /* Verify request is in a single row of RAID 5/6 */
3512#if BITS_PER_LONG == 32
3513 tmpdiv = first_block;
3514 (void) do_div(tmpdiv, stripesize);
3515 first_row = r5or6_first_row = r0_first_row = tmpdiv;
3516 tmpdiv = last_block;
3517 (void) do_div(tmpdiv, stripesize);
3518 r5or6_last_row = r0_last_row = tmpdiv;
3519#else
3520 first_row = r5or6_first_row = r0_first_row =
3521 first_block / stripesize;
3522 r5or6_last_row = r0_last_row = last_block / stripesize;
3523#endif
3524 if (r5or6_first_row != r5or6_last_row)
3525 return IO_ACCEL_INELIGIBLE;
3526
3527
3528 /* Verify request is in a single column */
3529#if BITS_PER_LONG == 32
3530 tmpdiv = first_block;
3531 first_row_offset = do_div(tmpdiv, stripesize);
3532 tmpdiv = first_row_offset;
3533 first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
3534 r5or6_first_row_offset = first_row_offset;
3535 tmpdiv = last_block;
3536 r5or6_last_row_offset = do_div(tmpdiv, stripesize);
3537 tmpdiv = r5or6_last_row_offset;
3538 r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
3539 tmpdiv = r5or6_first_row_offset;
3540 (void) do_div(tmpdiv, map->strip_size);
3541 first_column = r5or6_first_column = tmpdiv;
3542 tmpdiv = r5or6_last_row_offset;
3543 (void) do_div(tmpdiv, map->strip_size);
3544 r5or6_last_column = tmpdiv;
3545#else
3546 first_row_offset = r5or6_first_row_offset =
3547 (u32)((first_block % stripesize) %
3548 r5or6_blocks_per_row);
3549
3550 r5or6_last_row_offset =
3551 (u32)((last_block % stripesize) %
3552 r5or6_blocks_per_row);
3553
3554 first_column = r5or6_first_column =
3555 r5or6_first_row_offset / map->strip_size;
3556 r5or6_last_column =
3557 r5or6_last_row_offset / map->strip_size;
3558#endif
3559 if (r5or6_first_column != r5or6_last_column)
3560 return IO_ACCEL_INELIGIBLE;
3561
3562 /* Request is eligible */
3563 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
3564 map->row_cnt;
3565
3566 map_index = (first_group *
3567 (map->row_cnt * total_disks_per_row)) +
3568 (map_row * total_disks_per_row) + first_column;
3569 break;
3570 default:
3571 return IO_ACCEL_INELIGIBLE;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003572 }
Scott Teel6b80b182014-02-18 13:56:55 -06003573
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003574 disk_handle = dd[map_index].ioaccel_handle;
3575 disk_block = map->disk_starting_blk + (first_row * map->strip_size) +
3576 (first_row_offset - (first_column * map->strip_size));
3577 disk_block_cnt = block_cnt;
3578
3579 /* handle differing logical/physical block sizes */
3580 if (map->phys_blk_shift) {
3581 disk_block <<= map->phys_blk_shift;
3582 disk_block_cnt <<= map->phys_blk_shift;
3583 }
3584 BUG_ON(disk_block_cnt > 0xffff);
3585
3586 /* build the new CDB for the physical disk I/O */
3587 if (disk_block > 0xffffffff) {
3588 cdb[0] = is_write ? WRITE_16 : READ_16;
3589 cdb[1] = 0;
3590 cdb[2] = (u8) (disk_block >> 56);
3591 cdb[3] = (u8) (disk_block >> 48);
3592 cdb[4] = (u8) (disk_block >> 40);
3593 cdb[5] = (u8) (disk_block >> 32);
3594 cdb[6] = (u8) (disk_block >> 24);
3595 cdb[7] = (u8) (disk_block >> 16);
3596 cdb[8] = (u8) (disk_block >> 8);
3597 cdb[9] = (u8) (disk_block);
3598 cdb[10] = (u8) (disk_block_cnt >> 24);
3599 cdb[11] = (u8) (disk_block_cnt >> 16);
3600 cdb[12] = (u8) (disk_block_cnt >> 8);
3601 cdb[13] = (u8) (disk_block_cnt);
3602 cdb[14] = 0;
3603 cdb[15] = 0;
3604 cdb_len = 16;
3605 } else {
3606 cdb[0] = is_write ? WRITE_10 : READ_10;
3607 cdb[1] = 0;
3608 cdb[2] = (u8) (disk_block >> 24);
3609 cdb[3] = (u8) (disk_block >> 16);
3610 cdb[4] = (u8) (disk_block >> 8);
3611 cdb[5] = (u8) (disk_block);
3612 cdb[6] = 0;
3613 cdb[7] = (u8) (disk_block_cnt >> 8);
3614 cdb[8] = (u8) (disk_block_cnt);
3615 cdb[9] = 0;
3616 cdb_len = 10;
3617 }
3618 return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
3619 dev->scsi3addr);
3620}
3621
Jeff Garzikf2812332010-11-16 02:10:29 -05003622static int hpsa_scsi_queue_command_lck(struct scsi_cmnd *cmd,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003623 void (*done)(struct scsi_cmnd *))
3624{
3625 struct ctlr_info *h;
3626 struct hpsa_scsi_dev_t *dev;
3627 unsigned char scsi3addr[8];
3628 struct CommandList *c;
3629 unsigned long flags;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003630 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003631
3632 /* Get the ptr to our adapter structure out of cmd->host. */
3633 h = sdev_to_hba(cmd->device);
3634 dev = cmd->device->hostdata;
3635 if (!dev) {
3636 cmd->result = DID_NO_CONNECT << 16;
3637 done(cmd);
3638 return 0;
3639 }
3640 memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
3641
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003642 spin_lock_irqsave(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05003643 if (unlikely(h->lockup_detected)) {
3644 spin_unlock_irqrestore(&h->lock, flags);
3645 cmd->result = DID_ERROR << 16;
3646 done(cmd);
3647 return 0;
3648 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003649 spin_unlock_irqrestore(&h->lock, flags);
Matt Gatese16a33a2012-05-01 11:43:11 -05003650 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003651 if (c == NULL) { /* trouble... */
3652 dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
3653 return SCSI_MLQUEUE_HOST_BUSY;
3654 }
3655
3656 /* Fill in the command list header */
3657
3658 cmd->scsi_done = done; /* save this for use by completion code */
3659
3660 /* save c in case we have to abort it */
3661 cmd->host_scribble = (unsigned char *) c;
3662
3663 c->cmd_type = CMD_SCSI;
3664 c->scsi_cmd = cmd;
Matt Gatese1f7de02014-02-18 13:55:17 -06003665
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003666 /* Call alternate submit routine for I/O accelerated commands.
3667 * Retries always go down the normal I/O path.
3668 */
3669 if (likely(cmd->retries == 0 &&
Scott Teelda0697b2014-02-18 13:57:00 -06003670 cmd->request->cmd_type == REQ_TYPE_FS &&
3671 h->acciopath_status)) {
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003672 if (dev->offload_enabled) {
3673 rc = hpsa_scsi_ioaccel_raid_map(h, c);
3674 if (rc == 0)
3675 return 0; /* Sent on ioaccel path */
3676 if (rc < 0) { /* scsi_dma_map failed. */
3677 cmd_free(h, c);
3678 return SCSI_MLQUEUE_HOST_BUSY;
3679 }
3680 } else if (dev->ioaccel_handle) {
3681 rc = hpsa_scsi_ioaccel_direct_map(h, c);
3682 if (rc == 0)
3683 return 0; /* Sent on direct map path */
3684 if (rc < 0) { /* scsi_dma_map failed. */
3685 cmd_free(h, c);
3686 return SCSI_MLQUEUE_HOST_BUSY;
3687 }
3688 }
3689 }
Matt Gatese1f7de02014-02-18 13:55:17 -06003690
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003691 c->Header.ReplyQueue = 0; /* unused in simple mode */
3692 memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
Don Brace303932f2010-02-04 08:42:40 -06003693 c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT);
3694 c->Header.Tag.lower |= DIRECT_LOOKUP_BIT;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003695
3696 /* Fill in the request block... */
3697
3698 c->Request.Timeout = 0;
3699 memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
3700 BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
3701 c->Request.CDBLen = cmd->cmd_len;
3702 memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
3703 c->Request.Type.Type = TYPE_CMD;
3704 c->Request.Type.Attribute = ATTR_SIMPLE;
3705 switch (cmd->sc_data_direction) {
3706 case DMA_TO_DEVICE:
3707 c->Request.Type.Direction = XFER_WRITE;
3708 break;
3709 case DMA_FROM_DEVICE:
3710 c->Request.Type.Direction = XFER_READ;
3711 break;
3712 case DMA_NONE:
3713 c->Request.Type.Direction = XFER_NONE;
3714 break;
3715 case DMA_BIDIRECTIONAL:
3716 /* This can happen if a buggy application does a scsi passthru
3717 * and sets both inlen and outlen to non-zero. ( see
3718 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
3719 */
3720
3721 c->Request.Type.Direction = XFER_RSVD;
3722 /* This is technically wrong, and hpsa controllers should
3723 * reject it with CMD_INVALID, which is the most correct
3724 * response, but non-fibre backends appear to let it
3725 * slide by, and give the same results as if this field
3726 * were set correctly. Either way is acceptable for
3727 * our purposes here.
3728 */
3729
3730 break;
3731
3732 default:
3733 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3734 cmd->sc_data_direction);
3735 BUG();
3736 break;
3737 }
3738
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06003739 if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003740 cmd_free(h, c);
3741 return SCSI_MLQUEUE_HOST_BUSY;
3742 }
3743 enqueue_cmd_and_start_io(h, c);
3744 /* the cmd'll come back via intr handler in complete_scsi_command() */
3745 return 0;
3746}
3747
Jeff Garzikf2812332010-11-16 02:10:29 -05003748static DEF_SCSI_QCMD(hpsa_scsi_queue_command)
3749
Stephen M. Cameron5f389362014-02-18 13:55:48 -06003750static int do_not_scan_if_controller_locked_up(struct ctlr_info *h)
3751{
3752 unsigned long flags;
3753
3754 /*
3755 * Don't let rescans be initiated on a controller known
3756 * to be locked up. If the controller locks up *during*
3757 * a rescan, that thread is probably hosed, but at least
3758 * we can prevent new rescan threads from piling up on a
3759 * locked up controller.
3760 */
3761 spin_lock_irqsave(&h->lock, flags);
3762 if (unlikely(h->lockup_detected)) {
3763 spin_unlock_irqrestore(&h->lock, flags);
3764 spin_lock_irqsave(&h->scan_lock, flags);
3765 h->scan_finished = 1;
3766 wake_up_all(&h->scan_wait_queue);
3767 spin_unlock_irqrestore(&h->scan_lock, flags);
3768 return 1;
3769 }
3770 spin_unlock_irqrestore(&h->lock, flags);
3771 return 0;
3772}
3773
Stephen M. Camerona08a8472010-02-04 08:43:16 -06003774static void hpsa_scan_start(struct Scsi_Host *sh)
3775{
3776 struct ctlr_info *h = shost_to_hba(sh);
3777 unsigned long flags;
3778
Stephen M. Cameron5f389362014-02-18 13:55:48 -06003779 if (do_not_scan_if_controller_locked_up(h))
3780 return;
3781
Stephen M. Camerona08a8472010-02-04 08:43:16 -06003782 /* wait until any scan already in progress is finished. */
3783 while (1) {
3784 spin_lock_irqsave(&h->scan_lock, flags);
3785 if (h->scan_finished)
3786 break;
3787 spin_unlock_irqrestore(&h->scan_lock, flags);
3788 wait_event(h->scan_wait_queue, h->scan_finished);
3789 /* Note: We don't need to worry about a race between this
3790 * thread and driver unload because the midlayer will
3791 * have incremented the reference count, so unload won't
3792 * happen if we're in here.
3793 */
3794 }
3795 h->scan_finished = 0; /* mark scan as in progress */
3796 spin_unlock_irqrestore(&h->scan_lock, flags);
3797
Stephen M. Cameron5f389362014-02-18 13:55:48 -06003798 if (do_not_scan_if_controller_locked_up(h))
3799 return;
3800
Stephen M. Camerona08a8472010-02-04 08:43:16 -06003801 hpsa_update_scsi_devices(h, h->scsi_host->host_no);
3802
3803 spin_lock_irqsave(&h->scan_lock, flags);
3804 h->scan_finished = 1; /* mark scan as finished. */
3805 wake_up_all(&h->scan_wait_queue);
3806 spin_unlock_irqrestore(&h->scan_lock, flags);
3807}
3808
3809static int hpsa_scan_finished(struct Scsi_Host *sh,
3810 unsigned long elapsed_time)
3811{
3812 struct ctlr_info *h = shost_to_hba(sh);
3813 unsigned long flags;
3814 int finished;
3815
3816 spin_lock_irqsave(&h->scan_lock, flags);
3817 finished = h->scan_finished;
3818 spin_unlock_irqrestore(&h->scan_lock, flags);
3819 return finished;
3820}
3821
Stephen M. Cameron667e23d2010-02-25 14:02:51 -06003822static int hpsa_change_queue_depth(struct scsi_device *sdev,
3823 int qdepth, int reason)
3824{
3825 struct ctlr_info *h = sdev_to_hba(sdev);
3826
3827 if (reason != SCSI_QDEPTH_DEFAULT)
3828 return -ENOTSUPP;
3829
3830 if (qdepth < 1)
3831 qdepth = 1;
3832 else
3833 if (qdepth > h->nr_cmds)
3834 qdepth = h->nr_cmds;
3835 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
3836 return sdev->queue_depth;
3837}
3838
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003839static void hpsa_unregister_scsi(struct ctlr_info *h)
3840{
3841 /* we are being forcibly unloaded, and may not refuse. */
3842 scsi_remove_host(h->scsi_host);
3843 scsi_host_put(h->scsi_host);
3844 h->scsi_host = NULL;
3845}
3846
3847static int hpsa_register_scsi(struct ctlr_info *h)
3848{
Stephen M. Cameronb7056902012-01-19 14:00:53 -06003849 struct Scsi_Host *sh;
3850 int error;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003851
Stephen M. Cameronb7056902012-01-19 14:00:53 -06003852 sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
3853 if (sh == NULL)
3854 goto fail;
3855
3856 sh->io_port = 0;
3857 sh->n_io_port = 0;
3858 sh->this_id = -1;
3859 sh->max_channel = 3;
3860 sh->max_cmd_len = MAX_COMMAND_SIZE;
3861 sh->max_lun = HPSA_MAX_LUN;
3862 sh->max_id = HPSA_MAX_LUN;
3863 sh->can_queue = h->nr_cmds;
3864 sh->cmd_per_lun = h->nr_cmds;
3865 sh->sg_tablesize = h->maxsgentries;
3866 h->scsi_host = sh;
3867 sh->hostdata[0] = (unsigned long) h;
3868 sh->irq = h->intr[h->intr_mode];
3869 sh->unique_id = sh->irq;
3870 error = scsi_add_host(sh, &h->pdev->dev);
3871 if (error)
3872 goto fail_host_put;
3873 scsi_scan_host(sh);
3874 return 0;
3875
3876 fail_host_put:
3877 dev_err(&h->pdev->dev, "%s: scsi_add_host"
3878 " failed for controller %d\n", __func__, h->ctlr);
3879 scsi_host_put(sh);
3880 return error;
3881 fail:
3882 dev_err(&h->pdev->dev, "%s: scsi_host_alloc"
3883 " failed for controller %d\n", __func__, h->ctlr);
3884 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003885}
3886
3887static int wait_for_device_to_become_ready(struct ctlr_info *h,
3888 unsigned char lunaddr[])
3889{
3890 int rc = 0;
3891 int count = 0;
3892 int waittime = 1; /* seconds */
3893 struct CommandList *c;
3894
3895 c = cmd_special_alloc(h);
3896 if (!c) {
3897 dev_warn(&h->pdev->dev, "out of memory in "
3898 "wait_for_device_to_become_ready.\n");
3899 return IO_ERROR;
3900 }
3901
3902 /* Send test unit ready until device ready, or give up. */
3903 while (count < HPSA_TUR_RETRY_LIMIT) {
3904
3905 /* Wait for a bit. do this first, because if we send
3906 * the TUR right away, the reset will just abort it.
3907 */
3908 msleep(1000 * waittime);
3909 count++;
3910
3911 /* Increase wait time with each try, up to a point. */
3912 if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
3913 waittime = waittime * 2;
3914
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003915 /* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
3916 (void) fill_cmd(c, TEST_UNIT_READY, h,
3917 NULL, 0, 0, lunaddr, TYPE_CMD);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003918 hpsa_scsi_do_simple_cmd_core(h, c);
3919 /* no unmap needed here because no data xfer. */
3920
3921 if (c->err_info->CommandStatus == CMD_SUCCESS)
3922 break;
3923
3924 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
3925 c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
3926 (c->err_info->SenseInfo[2] == NO_SENSE ||
3927 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
3928 break;
3929
3930 dev_warn(&h->pdev->dev, "waiting %d secs "
3931 "for device to become ready.\n", waittime);
3932 rc = 1; /* device not ready. */
3933 }
3934
3935 if (rc)
3936 dev_warn(&h->pdev->dev, "giving up on device.\n");
3937 else
3938 dev_warn(&h->pdev->dev, "device is ready.\n");
3939
3940 cmd_special_free(h, c);
3941 return rc;
3942}
3943
3944/* Need at least one of these error handlers to keep ../scsi/hosts.c from
3945 * complaining. Doing a host- or bus-reset can't do anything good here.
3946 */
3947static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
3948{
3949 int rc;
3950 struct ctlr_info *h;
3951 struct hpsa_scsi_dev_t *dev;
3952
3953 /* find the controller to which the command to be aborted was sent */
3954 h = sdev_to_hba(scsicmd->device);
3955 if (h == NULL) /* paranoia */
3956 return FAILED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003957 dev = scsicmd->device->hostdata;
3958 if (!dev) {
3959 dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
3960 "device lookup failed.\n");
3961 return FAILED;
3962 }
Stephen M. Camerond416b0c2010-02-04 08:43:21 -06003963 dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
3964 h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003965 /* send a reset to the SCSI LUN which the command was sent to */
Scott Teelbf711ac2014-02-18 13:56:39 -06003966 rc = hpsa_send_reset(h, dev->scsi3addr, HPSA_RESET_TYPE_LUN);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003967 if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
3968 return SUCCESS;
3969
3970 dev_warn(&h->pdev->dev, "resetting device failed.\n");
3971 return FAILED;
3972}
3973
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003974static void swizzle_abort_tag(u8 *tag)
3975{
3976 u8 original_tag[8];
3977
3978 memcpy(original_tag, tag, 8);
3979 tag[0] = original_tag[3];
3980 tag[1] = original_tag[2];
3981 tag[2] = original_tag[1];
3982 tag[3] = original_tag[0];
3983 tag[4] = original_tag[7];
3984 tag[5] = original_tag[6];
3985 tag[6] = original_tag[5];
3986 tag[7] = original_tag[4];
3987}
3988
Scott Teel17eb87d2014-02-18 13:55:28 -06003989static void hpsa_get_tag(struct ctlr_info *h,
3990 struct CommandList *c, u32 *taglower, u32 *tagupper)
3991{
3992 if (c->cmd_type == CMD_IOACCEL1) {
3993 struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
3994 &h->ioaccel_cmd_pool[c->cmdindex];
3995 *tagupper = cm1->Tag.upper;
3996 *taglower = cm1->Tag.lower;
Scott Teel54b6e9e2014-02-18 13:56:45 -06003997 return;
Scott Teel17eb87d2014-02-18 13:55:28 -06003998 }
Scott Teel54b6e9e2014-02-18 13:56:45 -06003999 if (c->cmd_type == CMD_IOACCEL2) {
4000 struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
4001 &h->ioaccel2_cmd_pool[c->cmdindex];
Scott Teeldd0e19f2014-02-18 13:57:31 -06004002 /* upper tag not used in ioaccel2 mode */
4003 memset(tagupper, 0, sizeof(*tagupper));
4004 *taglower = cm2->Tag;
Scott Teel54b6e9e2014-02-18 13:56:45 -06004005 return;
4006 }
4007 *tagupper = c->Header.Tag.upper;
4008 *taglower = c->Header.Tag.lower;
Scott Teel17eb87d2014-02-18 13:55:28 -06004009}
4010
Scott Teel54b6e9e2014-02-18 13:56:45 -06004011
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004012static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05004013 struct CommandList *abort, int swizzle)
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004014{
4015 int rc = IO_OK;
4016 struct CommandList *c;
4017 struct ErrorInfo *ei;
Scott Teel17eb87d2014-02-18 13:55:28 -06004018 u32 tagupper, taglower;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004019
4020 c = cmd_special_alloc(h);
4021 if (c == NULL) { /* trouble... */
4022 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
4023 return -ENOMEM;
4024 }
4025
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004026 /* fill_cmd can't fail here, no buffer to map */
4027 (void) fill_cmd(c, HPSA_ABORT_MSG, h, abort,
4028 0, 0, scsi3addr, TYPE_MSG);
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05004029 if (swizzle)
4030 swizzle_abort_tag(&c->Request.CDB[4]);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004031 hpsa_scsi_do_simple_cmd_core(h, c);
Scott Teel17eb87d2014-02-18 13:55:28 -06004032 hpsa_get_tag(h, abort, &taglower, &tagupper);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004033 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd_core completed.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06004034 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004035 /* no unmap needed here because no data xfer. */
4036
4037 ei = c->err_info;
4038 switch (ei->CommandStatus) {
4039 case CMD_SUCCESS:
4040 break;
4041 case CMD_UNABORTABLE: /* Very common, don't make noise. */
4042 rc = -1;
4043 break;
4044 default:
4045 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06004046 __func__, tagupper, taglower);
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06004047 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004048 rc = -1;
4049 break;
4050 }
4051 cmd_special_free(h, c);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004052 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n",
4053 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004054 return rc;
4055}
4056
4057/*
4058 * hpsa_find_cmd_in_queue
4059 *
4060 * Used to determine whether a command (find) is still present
4061 * in queue_head. Optionally excludes the last element of queue_head.
4062 *
4063 * This is used to avoid unnecessary aborts. Commands in h->reqQ have
4064 * not yet been submitted, and so can be aborted by the driver without
4065 * sending an abort to the hardware.
4066 *
4067 * Returns pointer to command if found in queue, NULL otherwise.
4068 */
4069static struct CommandList *hpsa_find_cmd_in_queue(struct ctlr_info *h,
4070 struct scsi_cmnd *find, struct list_head *queue_head)
4071{
4072 unsigned long flags;
4073 struct CommandList *c = NULL; /* ptr into cmpQ */
4074
4075 if (!find)
4076 return 0;
4077 spin_lock_irqsave(&h->lock, flags);
4078 list_for_each_entry(c, queue_head, list) {
4079 if (c->scsi_cmd == NULL) /* e.g.: passthru ioctl */
4080 continue;
4081 if (c->scsi_cmd == find) {
4082 spin_unlock_irqrestore(&h->lock, flags);
4083 return c;
4084 }
4085 }
4086 spin_unlock_irqrestore(&h->lock, flags);
4087 return NULL;
4088}
4089
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05004090static struct CommandList *hpsa_find_cmd_in_queue_by_tag(struct ctlr_info *h,
4091 u8 *tag, struct list_head *queue_head)
4092{
4093 unsigned long flags;
4094 struct CommandList *c;
4095
4096 spin_lock_irqsave(&h->lock, flags);
4097 list_for_each_entry(c, queue_head, list) {
4098 if (memcmp(&c->Header.Tag, tag, 8) != 0)
4099 continue;
4100 spin_unlock_irqrestore(&h->lock, flags);
4101 return c;
4102 }
4103 spin_unlock_irqrestore(&h->lock, flags);
4104 return NULL;
4105}
4106
Scott Teel54b6e9e2014-02-18 13:56:45 -06004107/* ioaccel2 path firmware cannot handle abort task requests.
4108 * Change abort requests to physical target reset, and send to the
4109 * address of the physical disk used for the ioaccel 2 command.
4110 * Return 0 on success (IO_OK)
4111 * -1 on failure
4112 */
4113
4114static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
4115 unsigned char *scsi3addr, struct CommandList *abort)
4116{
4117 int rc = IO_OK;
4118 struct scsi_cmnd *scmd; /* scsi command within request being aborted */
4119 struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
4120 unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
4121 unsigned char *psa = &phys_scsi3addr[0];
4122
4123 /* Get a pointer to the hpsa logical device. */
4124 scmd = (struct scsi_cmnd *) abort->scsi_cmd;
4125 dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
4126 if (dev == NULL) {
4127 dev_warn(&h->pdev->dev,
4128 "Cannot abort: no device pointer for command.\n");
4129 return -1; /* not abortable */
4130 }
4131
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06004132 if (h->raid_offload_debug > 0)
4133 dev_info(&h->pdev->dev,
4134 "Reset as abort: Abort requested on C%d:B%d:T%d:L%d scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
4135 h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
4136 scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
4137 scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]);
4138
Scott Teel54b6e9e2014-02-18 13:56:45 -06004139 if (!dev->offload_enabled) {
4140 dev_warn(&h->pdev->dev,
4141 "Can't abort: device is not operating in HP SSD Smart Path mode.\n");
4142 return -1; /* not abortable */
4143 }
4144
4145 /* Incoming scsi3addr is logical addr. We need physical disk addr. */
4146 if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
4147 dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
4148 return -1; /* not abortable */
4149 }
4150
4151 /* send the reset */
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06004152 if (h->raid_offload_debug > 0)
4153 dev_info(&h->pdev->dev,
4154 "Reset as abort: Resetting physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
4155 psa[0], psa[1], psa[2], psa[3],
4156 psa[4], psa[5], psa[6], psa[7]);
Scott Teel54b6e9e2014-02-18 13:56:45 -06004157 rc = hpsa_send_reset(h, psa, HPSA_RESET_TYPE_TARGET);
4158 if (rc != 0) {
4159 dev_warn(&h->pdev->dev,
4160 "Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
4161 psa[0], psa[1], psa[2], psa[3],
4162 psa[4], psa[5], psa[6], psa[7]);
4163 return rc; /* failed to reset */
4164 }
4165
4166 /* wait for device to recover */
4167 if (wait_for_device_to_become_ready(h, psa) != 0) {
4168 dev_warn(&h->pdev->dev,
4169 "Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
4170 psa[0], psa[1], psa[2], psa[3],
4171 psa[4], psa[5], psa[6], psa[7]);
4172 return -1; /* failed to recover */
4173 }
4174
4175 /* device recovered */
4176 dev_info(&h->pdev->dev,
4177 "Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
4178 psa[0], psa[1], psa[2], psa[3],
4179 psa[4], psa[5], psa[6], psa[7]);
4180
4181 return rc; /* success */
4182}
4183
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05004184/* Some Smart Arrays need the abort tag swizzled, and some don't. It's hard to
4185 * tell which kind we're dealing with, so we send the abort both ways. There
4186 * shouldn't be any collisions between swizzled and unswizzled tags due to the
4187 * way we construct our tags but we check anyway in case the assumptions which
4188 * make this true someday become false.
4189 */
4190static int hpsa_send_abort_both_ways(struct ctlr_info *h,
4191 unsigned char *scsi3addr, struct CommandList *abort)
4192{
4193 u8 swizzled_tag[8];
4194 struct CommandList *c;
4195 int rc = 0, rc2 = 0;
4196
Scott Teel54b6e9e2014-02-18 13:56:45 -06004197 /* ioccelerator mode 2 commands should be aborted via the
4198 * accelerated path, since RAID path is unaware of these commands,
4199 * but underlying firmware can't handle abort TMF.
4200 * Change abort to physical device reset.
4201 */
4202 if (abort->cmd_type == CMD_IOACCEL2)
4203 return hpsa_send_reset_as_abort_ioaccel2(h, scsi3addr, abort);
4204
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05004205 /* we do not expect to find the swizzled tag in our queue, but
4206 * check anyway just to be sure the assumptions which make this
4207 * the case haven't become wrong.
4208 */
4209 memcpy(swizzled_tag, &abort->Request.CDB[4], 8);
4210 swizzle_abort_tag(swizzled_tag);
4211 c = hpsa_find_cmd_in_queue_by_tag(h, swizzled_tag, &h->cmpQ);
4212 if (c != NULL) {
4213 dev_warn(&h->pdev->dev, "Unexpectedly found byte-swapped tag in completion queue.\n");
4214 return hpsa_send_abort(h, scsi3addr, abort, 0);
4215 }
4216 rc = hpsa_send_abort(h, scsi3addr, abort, 0);
4217
4218 /* if the command is still in our queue, we can't conclude that it was
4219 * aborted (it might have just completed normally) but in any case
4220 * we don't need to try to abort it another way.
4221 */
4222 c = hpsa_find_cmd_in_queue(h, abort->scsi_cmd, &h->cmpQ);
4223 if (c)
4224 rc2 = hpsa_send_abort(h, scsi3addr, abort, 1);
4225 return rc && rc2;
4226}
4227
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004228/* Send an abort for the specified command.
4229 * If the device and controller support it,
4230 * send a task abort request.
4231 */
4232static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
4233{
4234
4235 int i, rc;
4236 struct ctlr_info *h;
4237 struct hpsa_scsi_dev_t *dev;
4238 struct CommandList *abort; /* pointer to command to be aborted */
4239 struct CommandList *found;
4240 struct scsi_cmnd *as; /* ptr to scsi cmd inside aborted command. */
4241 char msg[256]; /* For debug messaging. */
4242 int ml = 0;
Scott Teel17eb87d2014-02-18 13:55:28 -06004243 u32 tagupper, taglower;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004244
4245 /* Find the controller of the command to be aborted */
4246 h = sdev_to_hba(sc->device);
4247 if (WARN(h == NULL,
4248 "ABORT REQUEST FAILED, Controller lookup failed.\n"))
4249 return FAILED;
4250
4251 /* Check that controller supports some kind of task abort */
4252 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
4253 !(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
4254 return FAILED;
4255
4256 memset(msg, 0, sizeof(msg));
4257 ml += sprintf(msg+ml, "ABORT REQUEST on C%d:B%d:T%d:L%d ",
4258 h->scsi_host->host_no, sc->device->channel,
4259 sc->device->id, sc->device->lun);
4260
4261 /* Find the device of the command to be aborted */
4262 dev = sc->device->hostdata;
4263 if (!dev) {
4264 dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
4265 msg);
4266 return FAILED;
4267 }
4268
4269 /* Get SCSI command to be aborted */
4270 abort = (struct CommandList *) sc->host_scribble;
4271 if (abort == NULL) {
4272 dev_err(&h->pdev->dev, "%s FAILED, Command to abort is NULL.\n",
4273 msg);
4274 return FAILED;
4275 }
Scott Teel17eb87d2014-02-18 13:55:28 -06004276 hpsa_get_tag(h, abort, &taglower, &tagupper);
4277 ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004278 as = (struct scsi_cmnd *) abort->scsi_cmd;
4279 if (as != NULL)
4280 ml += sprintf(msg+ml, "Command:0x%x SN:0x%lx ",
4281 as->cmnd[0], as->serial_number);
4282 dev_dbg(&h->pdev->dev, "%s\n", msg);
4283 dev_warn(&h->pdev->dev, "Abort request on C%d:B%d:T%d:L%d\n",
4284 h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
4285
4286 /* Search reqQ to See if command is queued but not submitted,
4287 * if so, complete the command with aborted status and remove
4288 * it from the reqQ.
4289 */
4290 found = hpsa_find_cmd_in_queue(h, sc, &h->reqQ);
4291 if (found) {
4292 found->err_info->CommandStatus = CMD_ABORTED;
4293 finish_cmd(found);
4294 dev_info(&h->pdev->dev, "%s Request SUCCEEDED (driver queue).\n",
4295 msg);
4296 return SUCCESS;
4297 }
4298
4299 /* not in reqQ, if also not in cmpQ, must have already completed */
4300 found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
4301 if (!found) {
Stephen M. Camerond6ebd0f2012-07-26 11:34:17 -05004302 dev_dbg(&h->pdev->dev, "%s Request SUCCEEDED (not known to driver).\n",
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004303 msg);
4304 return SUCCESS;
4305 }
4306
4307 /*
4308 * Command is in flight, or possibly already completed
4309 * by the firmware (but not to the scsi mid layer) but we can't
4310 * distinguish which. Send the abort down.
4311 */
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05004312 rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004313 if (rc != 0) {
4314 dev_dbg(&h->pdev->dev, "%s Request FAILED.\n", msg);
4315 dev_warn(&h->pdev->dev, "FAILED abort on device C%d:B%d:T%d:L%d\n",
4316 h->scsi_host->host_no,
4317 dev->bus, dev->target, dev->lun);
4318 return FAILED;
4319 }
4320 dev_info(&h->pdev->dev, "%s REQUEST SUCCEEDED.\n", msg);
4321
4322 /* If the abort(s) above completed and actually aborted the
4323 * command, then the command to be aborted should already be
4324 * completed. If not, wait around a bit more to see if they
4325 * manage to complete normally.
4326 */
4327#define ABORT_COMPLETE_WAIT_SECS 30
4328 for (i = 0; i < ABORT_COMPLETE_WAIT_SECS * 10; i++) {
4329 found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
4330 if (!found)
4331 return SUCCESS;
4332 msleep(100);
4333 }
4334 dev_warn(&h->pdev->dev, "%s FAILED. Aborted command has not completed after %d seconds.\n",
4335 msg, ABORT_COMPLETE_WAIT_SECS);
4336 return FAILED;
4337}
4338
4339
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004340/*
4341 * For operations that cannot sleep, a command block is allocated at init,
4342 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
4343 * which ones are free or in use. Lock must be held when calling this.
4344 * cmd_free() is the complement.
4345 */
4346static struct CommandList *cmd_alloc(struct ctlr_info *h)
4347{
4348 struct CommandList *c;
4349 int i;
4350 union u64bit temp64;
4351 dma_addr_t cmd_dma_handle, err_dma_handle;
Matt Gatese16a33a2012-05-01 11:43:11 -05004352 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004353
Matt Gatese16a33a2012-05-01 11:43:11 -05004354 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004355 do {
4356 i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
Matt Gatese16a33a2012-05-01 11:43:11 -05004357 if (i == h->nr_cmds) {
4358 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004359 return NULL;
Matt Gatese16a33a2012-05-01 11:43:11 -05004360 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004361 } while (test_and_set_bit
4362 (i & (BITS_PER_LONG - 1),
4363 h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
Matt Gatese16a33a2012-05-01 11:43:11 -05004364 spin_unlock_irqrestore(&h->lock, flags);
4365
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004366 c = h->cmd_pool + i;
4367 memset(c, 0, sizeof(*c));
4368 cmd_dma_handle = h->cmd_pool_dhandle
4369 + i * sizeof(*c);
4370 c->err_info = h->errinfo_pool + i;
4371 memset(c->err_info, 0, sizeof(*c->err_info));
4372 err_dma_handle = h->errinfo_pool_dhandle
4373 + i * sizeof(*c->err_info);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004374
4375 c->cmdindex = i;
4376
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06004377 INIT_LIST_HEAD(&c->list);
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004378 c->busaddr = (u32) cmd_dma_handle;
4379 temp64.val = (u64) err_dma_handle;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004380 c->ErrDesc.Addr.lower = temp64.val32.lower;
4381 c->ErrDesc.Addr.upper = temp64.val32.upper;
4382 c->ErrDesc.Len = sizeof(*c->err_info);
4383
4384 c->h = h;
4385 return c;
4386}
4387
4388/* For operations that can wait for kmalloc to possibly sleep,
4389 * this routine can be called. Lock need not be held to call
4390 * cmd_special_alloc. cmd_special_free() is the complement.
4391 */
4392static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
4393{
4394 struct CommandList *c;
4395 union u64bit temp64;
4396 dma_addr_t cmd_dma_handle, err_dma_handle;
4397
4398 c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
4399 if (c == NULL)
4400 return NULL;
4401 memset(c, 0, sizeof(*c));
4402
Matt Gatese1f7de02014-02-18 13:55:17 -06004403 c->cmd_type = CMD_SCSI;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004404 c->cmdindex = -1;
4405
4406 c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info),
4407 &err_dma_handle);
4408
4409 if (c->err_info == NULL) {
4410 pci_free_consistent(h->pdev,
4411 sizeof(*c), c, cmd_dma_handle);
4412 return NULL;
4413 }
4414 memset(c->err_info, 0, sizeof(*c->err_info));
4415
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06004416 INIT_LIST_HEAD(&c->list);
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004417 c->busaddr = (u32) cmd_dma_handle;
4418 temp64.val = (u64) err_dma_handle;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004419 c->ErrDesc.Addr.lower = temp64.val32.lower;
4420 c->ErrDesc.Addr.upper = temp64.val32.upper;
4421 c->ErrDesc.Len = sizeof(*c->err_info);
4422
4423 c->h = h;
4424 return c;
4425}
4426
4427static void cmd_free(struct ctlr_info *h, struct CommandList *c)
4428{
4429 int i;
Matt Gatese16a33a2012-05-01 11:43:11 -05004430 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004431
4432 i = c - h->cmd_pool;
Matt Gatese16a33a2012-05-01 11:43:11 -05004433 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004434 clear_bit(i & (BITS_PER_LONG - 1),
4435 h->cmd_pool_bits + (i / BITS_PER_LONG));
Matt Gatese16a33a2012-05-01 11:43:11 -05004436 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004437}
4438
4439static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
4440{
4441 union u64bit temp64;
4442
4443 temp64.val32.lower = c->ErrDesc.Addr.lower;
4444 temp64.val32.upper = c->ErrDesc.Addr.upper;
4445 pci_free_consistent(h->pdev, sizeof(*c->err_info),
4446 c->err_info, (dma_addr_t) temp64.val);
4447 pci_free_consistent(h->pdev, sizeof(*c),
Stephen M. Camerond896f3f2011-01-06 14:47:53 -06004448 c, (dma_addr_t) (c->busaddr & DIRECT_LOOKUP_MASK));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004449}
4450
4451#ifdef CONFIG_COMPAT
4452
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004453static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
4454{
4455 IOCTL32_Command_struct __user *arg32 =
4456 (IOCTL32_Command_struct __user *) arg;
4457 IOCTL_Command_struct arg64;
4458 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
4459 int err;
4460 u32 cp;
4461
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06004462 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004463 err = 0;
4464 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
4465 sizeof(arg64.LUN_info));
4466 err |= copy_from_user(&arg64.Request, &arg32->Request,
4467 sizeof(arg64.Request));
4468 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
4469 sizeof(arg64.error_info));
4470 err |= get_user(arg64.buf_size, &arg32->buf_size);
4471 err |= get_user(cp, &arg32->buf);
4472 arg64.buf = compat_ptr(cp);
4473 err |= copy_to_user(p, &arg64, sizeof(arg64));
4474
4475 if (err)
4476 return -EFAULT;
4477
Stephen M. Camerone39eeae2010-02-04 08:43:46 -06004478 err = hpsa_ioctl(dev, CCISS_PASSTHRU, (void *)p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004479 if (err)
4480 return err;
4481 err |= copy_in_user(&arg32->error_info, &p->error_info,
4482 sizeof(arg32->error_info));
4483 if (err)
4484 return -EFAULT;
4485 return err;
4486}
4487
4488static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
4489 int cmd, void *arg)
4490{
4491 BIG_IOCTL32_Command_struct __user *arg32 =
4492 (BIG_IOCTL32_Command_struct __user *) arg;
4493 BIG_IOCTL_Command_struct arg64;
4494 BIG_IOCTL_Command_struct __user *p =
4495 compat_alloc_user_space(sizeof(arg64));
4496 int err;
4497 u32 cp;
4498
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06004499 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004500 err = 0;
4501 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
4502 sizeof(arg64.LUN_info));
4503 err |= copy_from_user(&arg64.Request, &arg32->Request,
4504 sizeof(arg64.Request));
4505 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
4506 sizeof(arg64.error_info));
4507 err |= get_user(arg64.buf_size, &arg32->buf_size);
4508 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
4509 err |= get_user(cp, &arg32->buf);
4510 arg64.buf = compat_ptr(cp);
4511 err |= copy_to_user(p, &arg64, sizeof(arg64));
4512
4513 if (err)
4514 return -EFAULT;
4515
Stephen M. Camerone39eeae2010-02-04 08:43:46 -06004516 err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004517 if (err)
4518 return err;
4519 err |= copy_in_user(&arg32->error_info, &p->error_info,
4520 sizeof(arg32->error_info));
4521 if (err)
4522 return -EFAULT;
4523 return err;
4524}
Stephen M. Cameron71fe75a2010-02-04 08:43:51 -06004525
4526static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
4527{
4528 switch (cmd) {
4529 case CCISS_GETPCIINFO:
4530 case CCISS_GETINTINFO:
4531 case CCISS_SETINTINFO:
4532 case CCISS_GETNODENAME:
4533 case CCISS_SETNODENAME:
4534 case CCISS_GETHEARTBEAT:
4535 case CCISS_GETBUSTYPES:
4536 case CCISS_GETFIRMVER:
4537 case CCISS_GETDRIVVER:
4538 case CCISS_REVALIDVOLS:
4539 case CCISS_DEREGDISK:
4540 case CCISS_REGNEWDISK:
4541 case CCISS_REGNEWD:
4542 case CCISS_RESCANDISK:
4543 case CCISS_GETLUNINFO:
4544 return hpsa_ioctl(dev, cmd, arg);
4545
4546 case CCISS_PASSTHRU32:
4547 return hpsa_ioctl32_passthru(dev, cmd, arg);
4548 case CCISS_BIG_PASSTHRU32:
4549 return hpsa_ioctl32_big_passthru(dev, cmd, arg);
4550
4551 default:
4552 return -ENOIOCTLCMD;
4553 }
4554}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004555#endif
4556
4557static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
4558{
4559 struct hpsa_pci_info pciinfo;
4560
4561 if (!argp)
4562 return -EINVAL;
4563 pciinfo.domain = pci_domain_nr(h->pdev->bus);
4564 pciinfo.bus = h->pdev->bus->number;
4565 pciinfo.dev_fn = h->pdev->devfn;
4566 pciinfo.board_id = h->board_id;
4567 if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
4568 return -EFAULT;
4569 return 0;
4570}
4571
4572static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
4573{
4574 DriverVer_type DriverVer;
4575 unsigned char vmaj, vmin, vsubmin;
4576 int rc;
4577
4578 rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
4579 &vmaj, &vmin, &vsubmin);
4580 if (rc != 3) {
4581 dev_info(&h->pdev->dev, "driver version string '%s' "
4582 "unrecognized.", HPSA_DRIVER_VERSION);
4583 vmaj = 0;
4584 vmin = 0;
4585 vsubmin = 0;
4586 }
4587 DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
4588 if (!argp)
4589 return -EINVAL;
4590 if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
4591 return -EFAULT;
4592 return 0;
4593}
4594
4595static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
4596{
4597 IOCTL_Command_struct iocommand;
4598 struct CommandList *c;
4599 char *buff = NULL;
4600 union u64bit temp64;
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004601 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004602
4603 if (!argp)
4604 return -EINVAL;
4605 if (!capable(CAP_SYS_RAWIO))
4606 return -EPERM;
4607 if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
4608 return -EFAULT;
4609 if ((iocommand.buf_size < 1) &&
4610 (iocommand.Request.Type.Direction != XFER_NONE)) {
4611 return -EINVAL;
4612 }
4613 if (iocommand.buf_size > 0) {
4614 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
4615 if (buff == NULL)
4616 return -EFAULT;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004617 if (iocommand.Request.Type.Direction == XFER_WRITE) {
4618 /* Copy the data into the buffer we created */
4619 if (copy_from_user(buff, iocommand.buf,
4620 iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004621 rc = -EFAULT;
4622 goto out_kfree;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004623 }
4624 } else {
4625 memset(buff, 0, iocommand.buf_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004626 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004627 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004628 c = cmd_special_alloc(h);
4629 if (c == NULL) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004630 rc = -ENOMEM;
4631 goto out_kfree;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004632 }
4633 /* Fill in the command type */
4634 c->cmd_type = CMD_IOCTL_PEND;
4635 /* Fill in Command Header */
4636 c->Header.ReplyQueue = 0; /* unused in simple mode */
4637 if (iocommand.buf_size > 0) { /* buffer to fill */
4638 c->Header.SGList = 1;
4639 c->Header.SGTotal = 1;
4640 } else { /* no buffers to fill */
4641 c->Header.SGList = 0;
4642 c->Header.SGTotal = 0;
4643 }
4644 memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
4645 /* use the kernel address the cmd block for tag */
4646 c->Header.Tag.lower = c->busaddr;
4647
4648 /* Fill in Request block */
4649 memcpy(&c->Request, &iocommand.Request,
4650 sizeof(c->Request));
4651
4652 /* Fill in the scatter gather information */
4653 if (iocommand.buf_size > 0) {
4654 temp64.val = pci_map_single(h->pdev, buff,
4655 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06004656 if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
4657 c->SG[0].Addr.lower = 0;
4658 c->SG[0].Addr.upper = 0;
4659 c->SG[0].Len = 0;
4660 rc = -ENOMEM;
4661 goto out;
4662 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004663 c->SG[0].Addr.lower = temp64.val32.lower;
4664 c->SG[0].Addr.upper = temp64.val32.upper;
4665 c->SG[0].Len = iocommand.buf_size;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06004666 c->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining*/
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004667 }
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004668 hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
Stephen M. Cameronc2dd32e2011-06-03 09:57:29 -05004669 if (iocommand.buf_size > 0)
4670 hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004671 check_ioctl_unit_attention(h, c);
4672
4673 /* Copy the error information out */
4674 memcpy(&iocommand.error_info, c->err_info,
4675 sizeof(iocommand.error_info));
4676 if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004677 rc = -EFAULT;
4678 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004679 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004680 if (iocommand.Request.Type.Direction == XFER_READ &&
4681 iocommand.buf_size > 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004682 /* Copy the data out of the buffer we created */
4683 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004684 rc = -EFAULT;
4685 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004686 }
4687 }
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004688out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004689 cmd_special_free(h, c);
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004690out_kfree:
4691 kfree(buff);
4692 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004693}
4694
4695static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
4696{
4697 BIG_IOCTL_Command_struct *ioc;
4698 struct CommandList *c;
4699 unsigned char **buff = NULL;
4700 int *buff_size = NULL;
4701 union u64bit temp64;
4702 BYTE sg_used = 0;
4703 int status = 0;
4704 int i;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004705 u32 left;
4706 u32 sz;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004707 BYTE __user *data_ptr;
4708
4709 if (!argp)
4710 return -EINVAL;
4711 if (!capable(CAP_SYS_RAWIO))
4712 return -EPERM;
4713 ioc = (BIG_IOCTL_Command_struct *)
4714 kmalloc(sizeof(*ioc), GFP_KERNEL);
4715 if (!ioc) {
4716 status = -ENOMEM;
4717 goto cleanup1;
4718 }
4719 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
4720 status = -EFAULT;
4721 goto cleanup1;
4722 }
4723 if ((ioc->buf_size < 1) &&
4724 (ioc->Request.Type.Direction != XFER_NONE)) {
4725 status = -EINVAL;
4726 goto cleanup1;
4727 }
4728 /* Check kmalloc limits using all SGs */
4729 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
4730 status = -EINVAL;
4731 goto cleanup1;
4732 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06004733 if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004734 status = -EINVAL;
4735 goto cleanup1;
4736 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06004737 buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004738 if (!buff) {
4739 status = -ENOMEM;
4740 goto cleanup1;
4741 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06004742 buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004743 if (!buff_size) {
4744 status = -ENOMEM;
4745 goto cleanup1;
4746 }
4747 left = ioc->buf_size;
4748 data_ptr = ioc->buf;
4749 while (left) {
4750 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
4751 buff_size[sg_used] = sz;
4752 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
4753 if (buff[sg_used] == NULL) {
4754 status = -ENOMEM;
4755 goto cleanup1;
4756 }
4757 if (ioc->Request.Type.Direction == XFER_WRITE) {
4758 if (copy_from_user(buff[sg_used], data_ptr, sz)) {
4759 status = -ENOMEM;
4760 goto cleanup1;
4761 }
4762 } else
4763 memset(buff[sg_used], 0, sz);
4764 left -= sz;
4765 data_ptr += sz;
4766 sg_used++;
4767 }
4768 c = cmd_special_alloc(h);
4769 if (c == NULL) {
4770 status = -ENOMEM;
4771 goto cleanup1;
4772 }
4773 c->cmd_type = CMD_IOCTL_PEND;
4774 c->Header.ReplyQueue = 0;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004775 c->Header.SGList = c->Header.SGTotal = sg_used;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004776 memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
4777 c->Header.Tag.lower = c->busaddr;
4778 memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
4779 if (ioc->buf_size > 0) {
4780 int i;
4781 for (i = 0; i < sg_used; i++) {
4782 temp64.val = pci_map_single(h->pdev, buff[i],
4783 buff_size[i], PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06004784 if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
4785 c->SG[i].Addr.lower = 0;
4786 c->SG[i].Addr.upper = 0;
4787 c->SG[i].Len = 0;
4788 hpsa_pci_unmap(h->pdev, c, i,
4789 PCI_DMA_BIDIRECTIONAL);
4790 status = -ENOMEM;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05004791 goto cleanup0;
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06004792 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004793 c->SG[i].Addr.lower = temp64.val32.lower;
4794 c->SG[i].Addr.upper = temp64.val32.upper;
4795 c->SG[i].Len = buff_size[i];
Matt Gatese1d9cbf2014-02-18 13:55:12 -06004796 c->SG[i].Ext = i < sg_used - 1 ? 0 : HPSA_SG_LAST;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004797 }
4798 }
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004799 hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004800 if (sg_used)
4801 hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004802 check_ioctl_unit_attention(h, c);
4803 /* Copy the error information out */
4804 memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
4805 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004806 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05004807 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004808 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004809 if (ioc->Request.Type.Direction == XFER_READ && ioc->buf_size > 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004810 /* Copy the data out of the buffer we created */
4811 BYTE __user *ptr = ioc->buf;
4812 for (i = 0; i < sg_used; i++) {
4813 if (copy_to_user(ptr, buff[i], buff_size[i])) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004814 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05004815 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004816 }
4817 ptr += buff_size[i];
4818 }
4819 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004820 status = 0;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05004821cleanup0:
4822 cmd_special_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004823cleanup1:
4824 if (buff) {
4825 for (i = 0; i < sg_used; i++)
4826 kfree(buff[i]);
4827 kfree(buff);
4828 }
4829 kfree(buff_size);
4830 kfree(ioc);
4831 return status;
4832}
4833
4834static void check_ioctl_unit_attention(struct ctlr_info *h,
4835 struct CommandList *c)
4836{
4837 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
4838 c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
4839 (void) check_for_unit_attention(h, c);
4840}
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05004841
4842static int increment_passthru_count(struct ctlr_info *h)
4843{
4844 unsigned long flags;
4845
4846 spin_lock_irqsave(&h->passthru_count_lock, flags);
4847 if (h->passthru_count >= HPSA_MAX_CONCURRENT_PASSTHRUS) {
4848 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
4849 return -1;
4850 }
4851 h->passthru_count++;
4852 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
4853 return 0;
4854}
4855
4856static void decrement_passthru_count(struct ctlr_info *h)
4857{
4858 unsigned long flags;
4859
4860 spin_lock_irqsave(&h->passthru_count_lock, flags);
4861 if (h->passthru_count <= 0) {
4862 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
4863 /* not expecting to get here. */
4864 dev_warn(&h->pdev->dev, "Bug detected, passthru_count seems to be incorrect.\n");
4865 return;
4866 }
4867 h->passthru_count--;
4868 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
4869}
4870
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004871/*
4872 * ioctl
4873 */
4874static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
4875{
4876 struct ctlr_info *h;
4877 void __user *argp = (void __user *)arg;
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05004878 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004879
4880 h = sdev_to_hba(dev);
4881
4882 switch (cmd) {
4883 case CCISS_DEREGDISK:
4884 case CCISS_REGNEWDISK:
4885 case CCISS_REGNEWD:
Stephen M. Camerona08a8472010-02-04 08:43:16 -06004886 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004887 return 0;
4888 case CCISS_GETPCIINFO:
4889 return hpsa_getpciinfo_ioctl(h, argp);
4890 case CCISS_GETDRIVVER:
4891 return hpsa_getdrivver_ioctl(h, argp);
4892 case CCISS_PASSTHRU:
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05004893 if (increment_passthru_count(h))
4894 return -EAGAIN;
4895 rc = hpsa_passthru_ioctl(h, argp);
4896 decrement_passthru_count(h);
4897 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004898 case CCISS_BIG_PASSTHRU:
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05004899 if (increment_passthru_count(h))
4900 return -EAGAIN;
4901 rc = hpsa_big_passthru_ioctl(h, argp);
4902 decrement_passthru_count(h);
4903 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004904 default:
4905 return -ENOTTY;
4906 }
4907}
4908
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004909static int hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
4910 u8 reset_type)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004911{
4912 struct CommandList *c;
4913
4914 c = cmd_alloc(h);
4915 if (!c)
4916 return -ENOMEM;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004917 /* fill_cmd can't fail here, no data buffer to map */
4918 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004919 RAID_CTLR_LUNID, TYPE_MSG);
4920 c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
4921 c->waiting = NULL;
4922 enqueue_cmd_and_start_io(h, c);
4923 /* Don't wait for completion, the reset won't complete. Don't free
4924 * the command either. This is the last command we will send before
4925 * re-initializing everything, so it doesn't matter and won't leak.
4926 */
4927 return 0;
4928}
4929
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004930static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06004931 void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004932 int cmd_type)
4933{
4934 int pci_dir = XFER_NONE;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004935 struct CommandList *a; /* for commands to be aborted */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004936
4937 c->cmd_type = CMD_IOCTL_PEND;
4938 c->Header.ReplyQueue = 0;
4939 if (buff != NULL && size > 0) {
4940 c->Header.SGList = 1;
4941 c->Header.SGTotal = 1;
4942 } else {
4943 c->Header.SGList = 0;
4944 c->Header.SGTotal = 0;
4945 }
4946 c->Header.Tag.lower = c->busaddr;
4947 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
4948
4949 c->Request.Type.Type = cmd_type;
4950 if (cmd_type == TYPE_CMD) {
4951 switch (cmd) {
4952 case HPSA_INQUIRY:
4953 /* are we trying to read a vital product page */
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06004954 if (page_code & VPD_PAGE) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004955 c->Request.CDB[1] = 0x01;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06004956 c->Request.CDB[2] = (page_code & 0xff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004957 }
4958 c->Request.CDBLen = 6;
4959 c->Request.Type.Attribute = ATTR_SIMPLE;
4960 c->Request.Type.Direction = XFER_READ;
4961 c->Request.Timeout = 0;
4962 c->Request.CDB[0] = HPSA_INQUIRY;
4963 c->Request.CDB[4] = size & 0xFF;
4964 break;
4965 case HPSA_REPORT_LOG:
4966 case HPSA_REPORT_PHYS:
4967 /* Talking to controller so It's a physical command
4968 mode = 00 target = 0. Nothing to write.
4969 */
4970 c->Request.CDBLen = 12;
4971 c->Request.Type.Attribute = ATTR_SIMPLE;
4972 c->Request.Type.Direction = XFER_READ;
4973 c->Request.Timeout = 0;
4974 c->Request.CDB[0] = cmd;
4975 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
4976 c->Request.CDB[7] = (size >> 16) & 0xFF;
4977 c->Request.CDB[8] = (size >> 8) & 0xFF;
4978 c->Request.CDB[9] = size & 0xFF;
4979 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004980 case HPSA_CACHE_FLUSH:
4981 c->Request.CDBLen = 12;
4982 c->Request.Type.Attribute = ATTR_SIMPLE;
4983 c->Request.Type.Direction = XFER_WRITE;
4984 c->Request.Timeout = 0;
4985 c->Request.CDB[0] = BMIC_WRITE;
4986 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
Stephen M. Cameronbb158ea2011-10-26 16:21:17 -05004987 c->Request.CDB[7] = (size >> 8) & 0xFF;
4988 c->Request.CDB[8] = size & 0xFF;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004989 break;
4990 case TEST_UNIT_READY:
4991 c->Request.CDBLen = 6;
4992 c->Request.Type.Attribute = ATTR_SIMPLE;
4993 c->Request.Type.Direction = XFER_NONE;
4994 c->Request.Timeout = 0;
4995 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06004996 case HPSA_GET_RAID_MAP:
4997 c->Request.CDBLen = 12;
4998 c->Request.Type.Attribute = ATTR_SIMPLE;
4999 c->Request.Type.Direction = XFER_READ;
5000 c->Request.Timeout = 0;
5001 c->Request.CDB[0] = HPSA_CISS_READ;
5002 c->Request.CDB[1] = cmd;
5003 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
5004 c->Request.CDB[7] = (size >> 16) & 0xFF;
5005 c->Request.CDB[8] = (size >> 8) & 0xFF;
5006 c->Request.CDB[9] = size & 0xFF;
5007 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005008 default:
5009 dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
5010 BUG();
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005011 return -1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005012 }
5013 } else if (cmd_type == TYPE_MSG) {
5014 switch (cmd) {
5015
5016 case HPSA_DEVICE_RESET_MSG:
5017 c->Request.CDBLen = 16;
5018 c->Request.Type.Type = 1; /* It is a MSG not a CMD */
5019 c->Request.Type.Attribute = ATTR_SIMPLE;
5020 c->Request.Type.Direction = XFER_NONE;
5021 c->Request.Timeout = 0; /* Don't time out */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005022 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
5023 c->Request.CDB[0] = cmd;
Stephen M. Cameron21e89af2012-07-26 11:34:10 -05005024 c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005025 /* If bytes 4-7 are zero, it means reset the */
5026 /* LunID device */
5027 c->Request.CDB[4] = 0x00;
5028 c->Request.CDB[5] = 0x00;
5029 c->Request.CDB[6] = 0x00;
5030 c->Request.CDB[7] = 0x00;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005031 break;
5032 case HPSA_ABORT_MSG:
5033 a = buff; /* point to command to be aborted */
5034 dev_dbg(&h->pdev->dev, "Abort Tag:0x%08x:%08x using request Tag:0x%08x:%08x\n",
5035 a->Header.Tag.upper, a->Header.Tag.lower,
5036 c->Header.Tag.upper, c->Header.Tag.lower);
5037 c->Request.CDBLen = 16;
5038 c->Request.Type.Type = TYPE_MSG;
5039 c->Request.Type.Attribute = ATTR_SIMPLE;
5040 c->Request.Type.Direction = XFER_WRITE;
5041 c->Request.Timeout = 0; /* Don't time out */
5042 c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
5043 c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
5044 c->Request.CDB[2] = 0x00; /* reserved */
5045 c->Request.CDB[3] = 0x00; /* reserved */
5046 /* Tag to abort goes in CDB[4]-CDB[11] */
5047 c->Request.CDB[4] = a->Header.Tag.lower & 0xFF;
5048 c->Request.CDB[5] = (a->Header.Tag.lower >> 8) & 0xFF;
5049 c->Request.CDB[6] = (a->Header.Tag.lower >> 16) & 0xFF;
5050 c->Request.CDB[7] = (a->Header.Tag.lower >> 24) & 0xFF;
5051 c->Request.CDB[8] = a->Header.Tag.upper & 0xFF;
5052 c->Request.CDB[9] = (a->Header.Tag.upper >> 8) & 0xFF;
5053 c->Request.CDB[10] = (a->Header.Tag.upper >> 16) & 0xFF;
5054 c->Request.CDB[11] = (a->Header.Tag.upper >> 24) & 0xFF;
5055 c->Request.CDB[12] = 0x00; /* reserved */
5056 c->Request.CDB[13] = 0x00; /* reserved */
5057 c->Request.CDB[14] = 0x00; /* reserved */
5058 c->Request.CDB[15] = 0x00; /* reserved */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005059 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005060 default:
5061 dev_warn(&h->pdev->dev, "unknown message type %d\n",
5062 cmd);
5063 BUG();
5064 }
5065 } else {
5066 dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
5067 BUG();
5068 }
5069
5070 switch (c->Request.Type.Direction) {
5071 case XFER_READ:
5072 pci_dir = PCI_DMA_FROMDEVICE;
5073 break;
5074 case XFER_WRITE:
5075 pci_dir = PCI_DMA_TODEVICE;
5076 break;
5077 case XFER_NONE:
5078 pci_dir = PCI_DMA_NONE;
5079 break;
5080 default:
5081 pci_dir = PCI_DMA_BIDIRECTIONAL;
5082 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005083 if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
5084 return -1;
5085 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005086}
5087
5088/*
5089 * Map (physical) PCI mem into (virtual) kernel space
5090 */
5091static void __iomem *remap_pci_mem(ulong base, ulong size)
5092{
5093 ulong page_base = ((ulong) base) & PAGE_MASK;
5094 ulong page_offs = ((ulong) base) - page_base;
Stephen M. Cameron088ba342012-07-26 11:34:23 -05005095 void __iomem *page_remapped = ioremap_nocache(page_base,
5096 page_offs + size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005097
5098 return page_remapped ? (page_remapped + page_offs) : NULL;
5099}
5100
5101/* Takes cmds off the submission queue and sends them to the hardware,
5102 * then puts them on the queue of cmds waiting for completion.
5103 */
5104static void start_io(struct ctlr_info *h)
5105{
5106 struct CommandList *c;
Matt Gatese16a33a2012-05-01 11:43:11 -05005107 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005108
Matt Gatese16a33a2012-05-01 11:43:11 -05005109 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06005110 while (!list_empty(&h->reqQ)) {
5111 c = list_entry(h->reqQ.next, struct CommandList, list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005112 /* can't do anything if fifo is full */
5113 if ((h->access.fifo_full(h))) {
Stephen M. Cameron396883e2013-09-23 13:34:17 -05005114 h->fifo_recently_full = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005115 dev_warn(&h->pdev->dev, "fifo full\n");
5116 break;
5117 }
Stephen M. Cameron396883e2013-09-23 13:34:17 -05005118 h->fifo_recently_full = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005119
5120 /* Get the first entry from the Request Q */
5121 removeQ(c);
5122 h->Qdepth--;
5123
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005124 /* Put job onto the completed Q */
5125 addQ(&h->cmpQ, c);
Matt Gatese16a33a2012-05-01 11:43:11 -05005126
5127 /* Must increment commands_outstanding before unlocking
5128 * and submitting to avoid race checking for fifo full
5129 * condition.
5130 */
5131 h->commands_outstanding++;
5132 if (h->commands_outstanding > h->max_outstanding)
5133 h->max_outstanding = h->commands_outstanding;
5134
5135 /* Tell the controller execute command */
5136 spin_unlock_irqrestore(&h->lock, flags);
5137 h->access.submit_command(h, c);
5138 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005139 }
Matt Gatese16a33a2012-05-01 11:43:11 -05005140 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005141}
5142
Matt Gates254f7962012-05-01 11:43:06 -05005143static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005144{
Matt Gates254f7962012-05-01 11:43:06 -05005145 return h->access.command_completed(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005146}
5147
Stephen M. Cameron900c5442010-02-04 08:42:35 -06005148static inline bool interrupt_pending(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005149{
5150 return h->access.intr_pending(h);
5151}
5152
5153static inline long interrupt_not_for_us(struct ctlr_info *h)
5154{
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005155 return (h->access.intr_pending(h) == 0) ||
5156 (h->interrupts_enabled == 0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005157}
5158
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06005159static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
5160 u32 raw_tag)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005161{
5162 if (unlikely(tag_index >= h->nr_cmds)) {
5163 dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
5164 return 1;
5165 }
5166 return 0;
5167}
5168
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05005169static inline void finish_cmd(struct CommandList *c)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005170{
Matt Gatese16a33a2012-05-01 11:43:11 -05005171 unsigned long flags;
Stephen M. Cameron396883e2013-09-23 13:34:17 -05005172 int io_may_be_stalled = 0;
5173 struct ctlr_info *h = c->h;
Matt Gatese16a33a2012-05-01 11:43:11 -05005174
Stephen M. Cameron396883e2013-09-23 13:34:17 -05005175 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005176 removeQ(c);
Stephen M. Cameron396883e2013-09-23 13:34:17 -05005177
5178 /*
5179 * Check for possibly stalled i/o.
5180 *
5181 * If a fifo_full condition is encountered, requests will back up
5182 * in h->reqQ. This queue is only emptied out by start_io which is
5183 * only called when a new i/o request comes in. If no i/o's are
5184 * forthcoming, the i/o's in h->reqQ can get stuck. So we call
5185 * start_io from here if we detect such a danger.
5186 *
5187 * Normally, we shouldn't hit this case, but pounding on the
5188 * CCISS_PASSTHRU ioctl can provoke it. Only call start_io if
5189 * commands_outstanding is low. We want to avoid calling
5190 * start_io from in here as much as possible, and esp. don't
5191 * want to get in a cycle where we call start_io every time
5192 * through here.
5193 */
5194 if (unlikely(h->fifo_recently_full) &&
5195 h->commands_outstanding < 5)
5196 io_may_be_stalled = 1;
5197
5198 spin_unlock_irqrestore(&h->lock, flags);
5199
Stephen M. Camerone85c5972012-05-01 11:43:42 -05005200 dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
Scott Teelc3497752014-02-18 13:56:34 -06005201 if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
5202 || c->cmd_type == CMD_IOACCEL2))
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05005203 complete_scsi_command(c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005204 else if (c->cmd_type == CMD_IOCTL_PEND)
5205 complete(c->waiting);
Stephen M. Cameron396883e2013-09-23 13:34:17 -05005206 if (unlikely(io_may_be_stalled))
5207 start_io(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005208}
5209
Stephen M. Camerona104c992010-02-04 08:42:24 -06005210static inline u32 hpsa_tag_contains_index(u32 tag)
5211{
Stephen M. Camerona104c992010-02-04 08:42:24 -06005212 return tag & DIRECT_LOOKUP_BIT;
5213}
5214
5215static inline u32 hpsa_tag_to_index(u32 tag)
5216{
Stephen M. Camerona104c992010-02-04 08:42:24 -06005217 return tag >> DIRECT_LOOKUP_SHIFT;
5218}
5219
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005220
5221static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
Stephen M. Camerona104c992010-02-04 08:42:24 -06005222{
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005223#define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
5224#define HPSA_SIMPLE_ERROR_BITS 0x03
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06005225 if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005226 return tag & ~HPSA_SIMPLE_ERROR_BITS;
5227 return tag & ~HPSA_PERF_ERROR_BITS;
Stephen M. Camerona104c992010-02-04 08:42:24 -06005228}
5229
Don Brace303932f2010-02-04 08:42:40 -06005230/* process completion of an indexed ("direct lookup") command */
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005231static inline void process_indexed_cmd(struct ctlr_info *h,
Don Brace303932f2010-02-04 08:42:40 -06005232 u32 raw_tag)
5233{
5234 u32 tag_index;
5235 struct CommandList *c;
5236
5237 tag_index = hpsa_tag_to_index(raw_tag);
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005238 if (!bad_tag(h, tag_index, raw_tag)) {
5239 c = h->cmd_pool + tag_index;
5240 finish_cmd(c);
5241 }
Don Brace303932f2010-02-04 08:42:40 -06005242}
5243
5244/* process completion of a non-indexed command */
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005245static inline void process_nonindexed_cmd(struct ctlr_info *h,
Don Brace303932f2010-02-04 08:42:40 -06005246 u32 raw_tag)
5247{
5248 u32 tag;
5249 struct CommandList *c = NULL;
Matt Gatese16a33a2012-05-01 11:43:11 -05005250 unsigned long flags;
Don Brace303932f2010-02-04 08:42:40 -06005251
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005252 tag = hpsa_tag_discard_error_bits(h, raw_tag);
Matt Gatese16a33a2012-05-01 11:43:11 -05005253 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06005254 list_for_each_entry(c, &h->cmpQ, list) {
Don Brace303932f2010-02-04 08:42:40 -06005255 if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
Matt Gatese16a33a2012-05-01 11:43:11 -05005256 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05005257 finish_cmd(c);
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005258 return;
Don Brace303932f2010-02-04 08:42:40 -06005259 }
5260 }
Matt Gatese16a33a2012-05-01 11:43:11 -05005261 spin_unlock_irqrestore(&h->lock, flags);
Don Brace303932f2010-02-04 08:42:40 -06005262 bad_tag(h, h->nr_cmds + 1, raw_tag);
Don Brace303932f2010-02-04 08:42:40 -06005263}
5264
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005265/* Some controllers, like p400, will give us one interrupt
5266 * after a soft reset, even if we turned interrupts off.
5267 * Only need to check for this in the hpsa_xxx_discard_completions
5268 * functions.
5269 */
5270static int ignore_bogus_interrupt(struct ctlr_info *h)
5271{
5272 if (likely(!reset_devices))
5273 return 0;
5274
5275 if (likely(h->interrupts_enabled))
5276 return 0;
5277
5278 dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
5279 "(known firmware bug.) Ignoring.\n");
5280
5281 return 1;
5282}
5283
Matt Gates254f7962012-05-01 11:43:06 -05005284/*
5285 * Convert &h->q[x] (passed to interrupt handlers) back to h.
5286 * Relies on (h-q[x] == x) being true for x such that
5287 * 0 <= x < MAX_REPLY_QUEUES.
5288 */
5289static struct ctlr_info *queue_to_hba(u8 *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005290{
Matt Gates254f7962012-05-01 11:43:06 -05005291 return container_of((queue - *queue), struct ctlr_info, q[0]);
5292}
5293
5294static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
5295{
5296 struct ctlr_info *h = queue_to_hba(queue);
5297 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005298 u32 raw_tag;
5299
5300 if (ignore_bogus_interrupt(h))
5301 return IRQ_NONE;
5302
5303 if (interrupt_not_for_us(h))
5304 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005305 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005306 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05005307 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005308 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05005309 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005310 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005311 return IRQ_HANDLED;
5312}
5313
Matt Gates254f7962012-05-01 11:43:06 -05005314static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005315{
Matt Gates254f7962012-05-01 11:43:06 -05005316 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005317 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05005318 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005319
5320 if (ignore_bogus_interrupt(h))
5321 return IRQ_NONE;
5322
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005323 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05005324 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005325 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05005326 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005327 return IRQ_HANDLED;
5328}
5329
Matt Gates254f7962012-05-01 11:43:06 -05005330static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005331{
Matt Gates254f7962012-05-01 11:43:06 -05005332 struct ctlr_info *h = queue_to_hba((u8 *) queue);
Don Brace303932f2010-02-04 08:42:40 -06005333 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05005334 u8 q = *(u8 *) queue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005335
5336 if (interrupt_not_for_us(h))
5337 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005338 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005339 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05005340 raw_tag = get_next_completion(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005341 while (raw_tag != FIFO_EMPTY) {
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005342 if (likely(hpsa_tag_contains_index(raw_tag)))
5343 process_indexed_cmd(h, raw_tag);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005344 else
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005345 process_nonindexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05005346 raw_tag = next_command(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005347 }
5348 }
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005349 return IRQ_HANDLED;
5350}
5351
Matt Gates254f7962012-05-01 11:43:06 -05005352static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005353{
Matt Gates254f7962012-05-01 11:43:06 -05005354 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005355 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05005356 u8 q = *(u8 *) queue;
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005357
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005358 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05005359 raw_tag = get_next_completion(h, q);
Don Brace303932f2010-02-04 08:42:40 -06005360 while (raw_tag != FIFO_EMPTY) {
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005361 if (likely(hpsa_tag_contains_index(raw_tag)))
5362 process_indexed_cmd(h, raw_tag);
Don Brace303932f2010-02-04 08:42:40 -06005363 else
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005364 process_nonindexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05005365 raw_tag = next_command(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005366 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005367 return IRQ_HANDLED;
5368}
5369
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005370/* Send a message CDB to the firmware. Careful, this only works
5371 * in simple mode, not performant mode due to the tag lookup.
5372 * We only ever use this immediately after a controller reset.
5373 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005374static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
5375 unsigned char type)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005376{
5377 struct Command {
5378 struct CommandListHeader CommandHeader;
5379 struct RequestBlock Request;
5380 struct ErrDescriptor ErrorDescriptor;
5381 };
5382 struct Command *cmd;
5383 static const size_t cmd_sz = sizeof(*cmd) +
5384 sizeof(cmd->ErrorDescriptor);
5385 dma_addr_t paddr64;
5386 uint32_t paddr32, tag;
5387 void __iomem *vaddr;
5388 int i, err;
5389
5390 vaddr = pci_ioremap_bar(pdev, 0);
5391 if (vaddr == NULL)
5392 return -ENOMEM;
5393
5394 /* The Inbound Post Queue only accepts 32-bit physical addresses for the
5395 * CCISS commands, so they must be allocated from the lower 4GiB of
5396 * memory.
5397 */
5398 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
5399 if (err) {
5400 iounmap(vaddr);
5401 return -ENOMEM;
5402 }
5403
5404 cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
5405 if (cmd == NULL) {
5406 iounmap(vaddr);
5407 return -ENOMEM;
5408 }
5409
5410 /* This must fit, because of the 32-bit consistent DMA mask. Also,
5411 * although there's no guarantee, we assume that the address is at
5412 * least 4-byte aligned (most likely, it's page-aligned).
5413 */
5414 paddr32 = paddr64;
5415
5416 cmd->CommandHeader.ReplyQueue = 0;
5417 cmd->CommandHeader.SGList = 0;
5418 cmd->CommandHeader.SGTotal = 0;
5419 cmd->CommandHeader.Tag.lower = paddr32;
5420 cmd->CommandHeader.Tag.upper = 0;
5421 memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
5422
5423 cmd->Request.CDBLen = 16;
5424 cmd->Request.Type.Type = TYPE_MSG;
5425 cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
5426 cmd->Request.Type.Direction = XFER_NONE;
5427 cmd->Request.Timeout = 0; /* Don't time out */
5428 cmd->Request.CDB[0] = opcode;
5429 cmd->Request.CDB[1] = type;
5430 memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
5431 cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
5432 cmd->ErrorDescriptor.Addr.upper = 0;
5433 cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
5434
5435 writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
5436
5437 for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
5438 tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005439 if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr32)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005440 break;
5441 msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
5442 }
5443
5444 iounmap(vaddr);
5445
5446 /* we leak the DMA buffer here ... no choice since the controller could
5447 * still complete the command.
5448 */
5449 if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
5450 dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
5451 opcode, type);
5452 return -ETIMEDOUT;
5453 }
5454
5455 pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
5456
5457 if (tag & HPSA_ERROR_BIT) {
5458 dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
5459 opcode, type);
5460 return -EIO;
5461 }
5462
5463 dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
5464 opcode, type);
5465 return 0;
5466}
5467
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005468#define hpsa_noop(p) hpsa_message(p, 3, 0)
5469
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005470static int hpsa_controller_hard_reset(struct pci_dev *pdev,
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005471 void * __iomem vaddr, u32 use_doorbell)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005472{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005473 u16 pmcsr;
5474 int pos;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005475
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005476 if (use_doorbell) {
5477 /* For everything after the P600, the PCI power state method
5478 * of resetting the controller doesn't work, so we have this
5479 * other way using the doorbell register.
5480 */
5481 dev_info(&pdev->dev, "using doorbell to reset controller\n");
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005482 writel(use_doorbell, vaddr + SA5_DOORBELL);
Stephen M. Cameron85009232013-09-23 13:33:36 -05005483
5484 /* PMC hardware guys tell us we need a 5 second delay after
5485 * doorbell reset and before any attempt to talk to the board
5486 * at all to ensure that this actually works and doesn't fall
5487 * over in some weird corner cases.
5488 */
5489 msleep(5000);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005490 } else { /* Try to do it the PCI power state way */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005491
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005492 /* Quoting from the Open CISS Specification: "The Power
5493 * Management Control/Status Register (CSR) controls the power
5494 * state of the device. The normal operating state is D0,
5495 * CSR=00h. The software off state is D3, CSR=03h. To reset
5496 * the controller, place the interface device in D3 then to D0,
5497 * this causes a secondary PCI reset which will reset the
5498 * controller." */
5499
5500 pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
5501 if (pos == 0) {
5502 dev_err(&pdev->dev,
5503 "hpsa_reset_controller: "
5504 "PCI PM not supported\n");
5505 return -ENODEV;
5506 }
5507 dev_info(&pdev->dev, "using PCI PM to reset controller\n");
5508 /* enter the D3hot power management state */
5509 pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
5510 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
5511 pmcsr |= PCI_D3hot;
5512 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
5513
5514 msleep(500);
5515
5516 /* enter the D0 power management state */
5517 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
5518 pmcsr |= PCI_D0;
5519 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
Mike Millerc4853ef2011-10-21 08:19:43 +02005520
5521 /*
5522 * The P600 requires a small delay when changing states.
5523 * Otherwise we may think the board did not reset and we bail.
5524 * This for kdump only and is particular to the P600.
5525 */
5526 msleep(500);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005527 }
5528 return 0;
5529}
5530
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005531static void init_driver_version(char *driver_version, int len)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005532{
5533 memset(driver_version, 0, len);
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06005534 strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005535}
5536
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005537static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005538{
5539 char *driver_version;
5540 int i, size = sizeof(cfgtable->driver_version);
5541
5542 driver_version = kmalloc(size, GFP_KERNEL);
5543 if (!driver_version)
5544 return -ENOMEM;
5545
5546 init_driver_version(driver_version, size);
5547 for (i = 0; i < size; i++)
5548 writeb(driver_version[i], &cfgtable->driver_version[i]);
5549 kfree(driver_version);
5550 return 0;
5551}
5552
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005553static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
5554 unsigned char *driver_ver)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005555{
5556 int i;
5557
5558 for (i = 0; i < sizeof(cfgtable->driver_version); i++)
5559 driver_ver[i] = readb(&cfgtable->driver_version[i]);
5560}
5561
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005562static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005563{
5564
5565 char *driver_ver, *old_driver_ver;
5566 int rc, size = sizeof(cfgtable->driver_version);
5567
5568 old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
5569 if (!old_driver_ver)
5570 return -ENOMEM;
5571 driver_ver = old_driver_ver + size;
5572
5573 /* After a reset, the 32 bytes of "driver version" in the cfgtable
5574 * should have been changed, otherwise we know the reset failed.
5575 */
5576 init_driver_version(old_driver_ver, size);
5577 read_driver_ver_from_cfgtable(cfgtable, driver_ver);
5578 rc = !memcmp(driver_ver, old_driver_ver, size);
5579 kfree(old_driver_ver);
5580 return rc;
5581}
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005582/* This does a hard reset of the controller using PCI power management
5583 * states or the using the doorbell register.
5584 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005585static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev)
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005586{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005587 u64 cfg_offset;
5588 u32 cfg_base_addr;
5589 u64 cfg_base_addr_index;
5590 void __iomem *vaddr;
5591 unsigned long paddr;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005592 u32 misc_fw_support;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005593 int rc;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005594 struct CfgTable __iomem *cfgtable;
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005595 u32 use_doorbell;
Stephen M. Cameron18867652010-06-16 13:51:45 -05005596 u32 board_id;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005597 u16 command_register;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005598
5599 /* For controllers as old as the P600, this is very nearly
5600 * the same thing as
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005601 *
5602 * pci_save_state(pci_dev);
5603 * pci_set_power_state(pci_dev, PCI_D3hot);
5604 * pci_set_power_state(pci_dev, PCI_D0);
5605 * pci_restore_state(pci_dev);
5606 *
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005607 * For controllers newer than the P600, the pci power state
5608 * method of resetting doesn't work so we have another way
5609 * using the doorbell register.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005610 */
Stephen M. Cameron18867652010-06-16 13:51:45 -05005611
Stephen M. Cameron25c1e56a2011-01-06 14:48:18 -06005612 rc = hpsa_lookup_board_id(pdev, &board_id);
Stephen M. Cameron46380782011-05-03 15:00:01 -05005613 if (rc < 0 || !ctlr_is_resettable(board_id)) {
Stephen M. Cameron25c1e56a2011-01-06 14:48:18 -06005614 dev_warn(&pdev->dev, "Not resetting device.\n");
5615 return -ENODEV;
5616 }
Stephen M. Cameron46380782011-05-03 15:00:01 -05005617
5618 /* if controller is soft- but not hard resettable... */
5619 if (!ctlr_is_hard_resettable(board_id))
5620 return -ENOTSUPP; /* try soft reset later. */
Stephen M. Cameron18867652010-06-16 13:51:45 -05005621
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005622 /* Save the PCI command register */
5623 pci_read_config_word(pdev, 4, &command_register);
5624 /* Turn the board off. This is so that later pci_restore_state()
5625 * won't turn the board on before the rest of config space is ready.
5626 */
5627 pci_disable_device(pdev);
5628 pci_save_state(pdev);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005629
5630 /* find the first memory BAR, so we can find the cfg table */
5631 rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
5632 if (rc)
5633 return rc;
5634 vaddr = remap_pci_mem(paddr, 0x250);
5635 if (!vaddr)
5636 return -ENOMEM;
5637
5638 /* find cfgtable in order to check if reset via doorbell is supported */
5639 rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
5640 &cfg_base_addr_index, &cfg_offset);
5641 if (rc)
5642 goto unmap_vaddr;
5643 cfgtable = remap_pci_mem(pci_resource_start(pdev,
5644 cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
5645 if (!cfgtable) {
5646 rc = -ENOMEM;
5647 goto unmap_vaddr;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005648 }
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005649 rc = write_driver_ver_to_cfgtable(cfgtable);
5650 if (rc)
5651 goto unmap_vaddr;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005652
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005653 /* If reset via doorbell register is supported, use that.
5654 * There are two such methods. Favor the newest method.
5655 */
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005656 misc_fw_support = readl(&cfgtable->misc_fw_support);
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005657 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
5658 if (use_doorbell) {
5659 use_doorbell = DOORBELL_CTLR_RESET2;
5660 } else {
5661 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
5662 if (use_doorbell) {
Mike Millerfba63092011-10-13 11:44:06 -05005663 dev_warn(&pdev->dev, "Soft reset not supported. "
5664 "Firmware update is required.\n");
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005665 rc = -ENOTSUPP; /* try soft reset */
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005666 goto unmap_cfgtable;
5667 }
5668 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005669
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005670 rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
5671 if (rc)
5672 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005673
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005674 pci_restore_state(pdev);
5675 rc = pci_enable_device(pdev);
5676 if (rc) {
5677 dev_warn(&pdev->dev, "failed to enable device.\n");
5678 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005679 }
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005680 pci_write_config_word(pdev, 4, command_register);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005681
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005682 /* Some devices (notably the HP Smart Array 5i Controller)
5683 need a little pause here */
5684 msleep(HPSA_POST_RESET_PAUSE_MSECS);
5685
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005686 rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
5687 if (rc) {
5688 dev_warn(&pdev->dev,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005689 "failed waiting for board to become ready "
5690 "after hard reset\n");
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005691 goto unmap_cfgtable;
5692 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005693
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005694 rc = controller_reset_failed(vaddr);
5695 if (rc < 0)
5696 goto unmap_cfgtable;
5697 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005698 dev_warn(&pdev->dev, "Unable to successfully reset "
5699 "controller. Will try soft reset.\n");
5700 rc = -ENOTSUPP;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005701 } else {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005702 dev_info(&pdev->dev, "board ready after hard reset.\n");
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005703 }
5704
5705unmap_cfgtable:
5706 iounmap(cfgtable);
5707
5708unmap_vaddr:
5709 iounmap(vaddr);
5710 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005711}
5712
5713/*
5714 * We cannot read the structure directly, for portability we must use
5715 * the io functions.
5716 * This is for debug only.
5717 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005718static void print_cfg_table(struct device *dev, struct CfgTable *tb)
5719{
Stephen M. Cameron58f86652010-05-27 15:13:58 -05005720#ifdef HPSA_DEBUG
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005721 int i;
5722 char temp_name[17];
5723
5724 dev_info(dev, "Controller Configuration information\n");
5725 dev_info(dev, "------------------------------------\n");
5726 for (i = 0; i < 4; i++)
5727 temp_name[i] = readb(&(tb->Signature[i]));
5728 temp_name[4] = '\0';
5729 dev_info(dev, " Signature = %s\n", temp_name);
5730 dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
5731 dev_info(dev, " Transport methods supported = 0x%x\n",
5732 readl(&(tb->TransportSupport)));
5733 dev_info(dev, " Transport methods active = 0x%x\n",
5734 readl(&(tb->TransportActive)));
5735 dev_info(dev, " Requested transport Method = 0x%x\n",
5736 readl(&(tb->HostWrite.TransportRequest)));
5737 dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
5738 readl(&(tb->HostWrite.CoalIntDelay)));
5739 dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
5740 readl(&(tb->HostWrite.CoalIntCount)));
5741 dev_info(dev, " Max outstanding commands = 0x%d\n",
5742 readl(&(tb->CmdsOutMax)));
5743 dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
5744 for (i = 0; i < 16; i++)
5745 temp_name[i] = readb(&(tb->ServerName[i]));
5746 temp_name[16] = '\0';
5747 dev_info(dev, " Server Name = %s\n", temp_name);
5748 dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
5749 readl(&(tb->HeartBeat)));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005750#endif /* HPSA_DEBUG */
Stephen M. Cameron58f86652010-05-27 15:13:58 -05005751}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005752
5753static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
5754{
5755 int i, offset, mem_type, bar_type;
5756
5757 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
5758 return 0;
5759 offset = 0;
5760 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
5761 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
5762 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
5763 offset += 4;
5764 else {
5765 mem_type = pci_resource_flags(pdev, i) &
5766 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
5767 switch (mem_type) {
5768 case PCI_BASE_ADDRESS_MEM_TYPE_32:
5769 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
5770 offset += 4; /* 32 bit */
5771 break;
5772 case PCI_BASE_ADDRESS_MEM_TYPE_64:
5773 offset += 8;
5774 break;
5775 default: /* reserved in PCI 2.2 */
5776 dev_warn(&pdev->dev,
5777 "base address is invalid\n");
5778 return -1;
5779 break;
5780 }
5781 }
5782 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
5783 return i + 1;
5784 }
5785 return -1;
5786}
5787
5788/* If MSI/MSI-X is supported by the kernel we will try to enable it on
5789 * controllers that are capable. If not, we use IO-APIC mode.
5790 */
5791
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005792static void hpsa_interrupt_mode(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005793{
5794#ifdef CONFIG_PCI_MSI
Matt Gates254f7962012-05-01 11:43:06 -05005795 int err, i;
5796 struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
5797
5798 for (i = 0; i < MAX_REPLY_QUEUES; i++) {
5799 hpsa_msix_entries[i].vector = 0;
5800 hpsa_msix_entries[i].entry = i;
5801 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005802
5803 /* Some boards advertise MSI but don't really support it */
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05005804 if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
5805 (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005806 goto default_int_mode;
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005807 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
5808 dev_info(&h->pdev->dev, "MSIX\n");
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005809 h->msix_vector = MAX_REPLY_QUEUES;
Matt Gates254f7962012-05-01 11:43:06 -05005810 err = pci_enable_msix(h->pdev, hpsa_msix_entries,
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005811 h->msix_vector);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005812 if (err > 0) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005813 dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005814 "available\n", err);
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005815 h->msix_vector = err;
5816 err = pci_enable_msix(h->pdev, hpsa_msix_entries,
5817 h->msix_vector);
5818 }
5819 if (!err) {
5820 for (i = 0; i < h->msix_vector; i++)
5821 h->intr[i] = hpsa_msix_entries[i].vector;
5822 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005823 } else {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005824 dev_warn(&h->pdev->dev, "MSI-X init failed %d\n",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005825 err);
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005826 h->msix_vector = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005827 goto default_int_mode;
5828 }
5829 }
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005830 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
5831 dev_info(&h->pdev->dev, "MSI\n");
5832 if (!pci_enable_msi(h->pdev))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005833 h->msi_vector = 1;
5834 else
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005835 dev_warn(&h->pdev->dev, "MSI init failed\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005836 }
5837default_int_mode:
5838#endif /* CONFIG_PCI_MSI */
5839 /* if we get here we're going to use the default interrupt mode */
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005840 h->intr[h->intr_mode] = h->pdev->irq;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005841}
5842
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005843static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05005844{
5845 int i;
5846 u32 subsystem_vendor_id, subsystem_device_id;
5847
5848 subsystem_vendor_id = pdev->subsystem_vendor;
5849 subsystem_device_id = pdev->subsystem_device;
5850 *board_id = ((subsystem_device_id << 16) & 0xffff0000) |
5851 subsystem_vendor_id;
5852
5853 for (i = 0; i < ARRAY_SIZE(products); i++)
5854 if (*board_id == products[i].board_id)
5855 return i;
5856
Stephen M. Cameron6798cc02010-06-16 13:51:20 -05005857 if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
5858 subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
5859 !hpsa_allow_any) {
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05005860 dev_warn(&pdev->dev, "unrecognized board ID: "
5861 "0x%08x, ignoring.\n", *board_id);
5862 return -ENODEV;
5863 }
5864 return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
5865}
5866
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005867static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
5868 unsigned long *memory_bar)
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005869{
5870 int i;
5871
5872 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05005873 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005874 /* addressing mode bits already removed */
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05005875 *memory_bar = pci_resource_start(pdev, i);
5876 dev_dbg(&pdev->dev, "memory BAR = %lx\n",
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005877 *memory_bar);
5878 return 0;
5879 }
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05005880 dev_warn(&pdev->dev, "no memory BAR found\n");
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005881 return -ENODEV;
5882}
5883
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005884static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
5885 int wait_for_ready)
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005886{
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005887 int i, iterations;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005888 u32 scratchpad;
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005889 if (wait_for_ready)
5890 iterations = HPSA_BOARD_READY_ITERATIONS;
5891 else
5892 iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005893
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005894 for (i = 0; i < iterations; i++) {
5895 scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
5896 if (wait_for_ready) {
5897 if (scratchpad == HPSA_FIRMWARE_READY)
5898 return 0;
5899 } else {
5900 if (scratchpad != HPSA_FIRMWARE_READY)
5901 return 0;
5902 }
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005903 msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
5904 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005905 dev_warn(&pdev->dev, "board not ready, timed out.\n");
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005906 return -ENODEV;
5907}
5908
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005909static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
5910 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
5911 u64 *cfg_offset)
Stephen M. Camerona51fd472010-06-16 13:51:30 -05005912{
5913 *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
5914 *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
5915 *cfg_base_addr &= (u32) 0x0000ffff;
5916 *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
5917 if (*cfg_base_addr_index == -1) {
5918 dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
5919 return -ENODEV;
5920 }
5921 return 0;
5922}
5923
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005924static int hpsa_find_cfgtables(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005925{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06005926 u64 cfg_offset;
5927 u32 cfg_base_addr;
5928 u64 cfg_base_addr_index;
Don Brace303932f2010-02-04 08:42:40 -06005929 u32 trans_offset;
Stephen M. Camerona51fd472010-06-16 13:51:30 -05005930 int rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005931
Stephen M. Camerona51fd472010-06-16 13:51:30 -05005932 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
5933 &cfg_base_addr_index, &cfg_offset);
5934 if (rc)
5935 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005936 h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
Stephen M. Camerona51fd472010-06-16 13:51:30 -05005937 cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005938 if (!h->cfgtable)
5939 return -ENOMEM;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005940 rc = write_driver_ver_to_cfgtable(h->cfgtable);
5941 if (rc)
5942 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005943 /* Find performant mode table. */
Stephen M. Camerona51fd472010-06-16 13:51:30 -05005944 trans_offset = readl(&h->cfgtable->TransMethodOffset);
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005945 h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
5946 cfg_base_addr_index)+cfg_offset+trans_offset,
5947 sizeof(*h->transtable));
5948 if (!h->transtable)
5949 return -ENOMEM;
5950 return 0;
5951}
5952
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005953static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05005954{
5955 h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
Stephen M. Cameron72ceeae2011-01-06 14:48:13 -06005956
5957 /* Limit commands in memory limited kdump scenario. */
5958 if (reset_devices && h->max_commands > 32)
5959 h->max_commands = 32;
5960
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05005961 if (h->max_commands < 16) {
5962 dev_warn(&h->pdev->dev, "Controller reports "
5963 "max supported commands of %d, an obvious lie. "
5964 "Using 16. Ensure that firmware is up to date.\n",
5965 h->max_commands);
5966 h->max_commands = 16;
5967 }
5968}
5969
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005970/* Interrogate the hardware for some limits:
5971 * max commands, max SG elements without chaining, and with chaining,
5972 * SG chain block size, etc.
5973 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005974static void hpsa_find_board_params(struct ctlr_info *h)
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005975{
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05005976 hpsa_get_max_perf_mode_cmds(h);
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005977 h->nr_cmds = h->max_commands - 4; /* Allow room for some ioctls */
5978 h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005979 h->fw_support = readl(&(h->cfgtable->misc_fw_support));
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005980 /*
5981 * Limit in-command s/g elements to 32 save dma'able memory.
5982 * Howvever spec says if 0, use 31
5983 */
5984 h->max_cmd_sg_entries = 31;
5985 if (h->maxsgentries > 512) {
5986 h->max_cmd_sg_entries = 32;
5987 h->chainsize = h->maxsgentries - h->max_cmd_sg_entries + 1;
5988 h->maxsgentries--; /* save one for chain pointer */
5989 } else {
5990 h->maxsgentries = 31; /* default to traditional values */
5991 h->chainsize = 0;
5992 }
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005993
5994 /* Find out what task management functions are supported and cache */
5995 h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
Scott Teel0e7a7fc2014-02-18 13:55:59 -06005996 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
5997 dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
5998 if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
5999 dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05006000}
6001
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05006002static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
6003{
Akinobu Mita0fc9fd42012-04-04 22:14:59 +09006004 if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05006005 dev_warn(&h->pdev->dev, "not a valid CISS config table\n");
6006 return false;
6007 }
6008 return true;
6009}
6010
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06006011static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05006012{
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06006013 u32 driver_support;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05006014
Stephen M. Cameron28e13442013-12-04 17:10:21 -06006015#ifdef CONFIG_X86
6016 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06006017 driver_support = readl(&(h->cfgtable->driver_support));
6018 driver_support |= ENABLE_SCSI_PREFETCH;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05006019#endif
Stephen M. Cameron28e13442013-12-04 17:10:21 -06006020 driver_support |= ENABLE_UNIT_ATTN;
6021 writel(driver_support, &(h->cfgtable->driver_support));
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05006022}
6023
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05006024/* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result
6025 * in a prefetch beyond physical memory.
6026 */
6027static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
6028{
6029 u32 dma_prefetch;
6030
6031 if (h->board_id != 0x3225103C)
6032 return;
6033 dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
6034 dma_prefetch |= 0x8000;
6035 writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
6036}
6037
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006038static void hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
6039{
6040 int i;
6041 u32 doorbell_value;
6042 unsigned long flags;
6043 /* wait until the clear_event_notify bit 6 is cleared by controller. */
6044 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
6045 spin_lock_irqsave(&h->lock, flags);
6046 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
6047 spin_unlock_irqrestore(&h->lock, flags);
6048 if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
6049 break;
6050 /* delay and try again */
6051 msleep(20);
6052 }
6053}
6054
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006055static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006056{
6057 int i;
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06006058 u32 doorbell_value;
6059 unsigned long flags;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006060
6061 /* under certain very rare conditions, this can take awhile.
6062 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
6063 * as we enter this code.)
6064 */
6065 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06006066 spin_lock_irqsave(&h->lock, flags);
6067 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
6068 spin_unlock_irqrestore(&h->lock, flags);
Dan Carpenter382be662011-02-15 15:33:13 -06006069 if (!(doorbell_value & CFGTBL_ChangeReq))
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006070 break;
6071 /* delay and try again */
Stephen M. Cameron60d3f5b2011-01-06 14:48:34 -06006072 usleep_range(10000, 20000);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006073 }
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05006074}
6075
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006076static int hpsa_enter_simple_mode(struct ctlr_info *h)
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05006077{
6078 u32 trans_support;
6079
6080 trans_support = readl(&(h->cfgtable->TransportSupport));
6081 if (!(trans_support & SIMPLE_MODE))
6082 return -ENOTSUPP;
6083
6084 h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006085
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05006086 /* Update the field, and then ring the doorbell */
6087 writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006088 writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05006089 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
6090 hpsa_wait_for_mode_change_ack(h);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006091 print_cfg_table(&h->pdev->dev, h->cfgtable);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006092 if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
6093 goto error;
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06006094 h->transMethod = CFGTBL_Trans_Simple;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006095 return 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006096error:
6097 dev_warn(&h->pdev->dev, "unable to get board into simple mode\n");
6098 return -ENODEV;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006099}
6100
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006101static int hpsa_pci_init(struct ctlr_info *h)
Stephen M. Cameron77c44952010-05-27 15:13:17 -05006102{
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006103 int prod_index, err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006104
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05006105 prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
6106 if (prod_index < 0)
6107 return -ENODEV;
6108 h->product_name = products[prod_index].product_name;
6109 h->access = *(products[prod_index].access);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006110
Matthew Garrette5a44df2011-11-11 11:14:23 -05006111 pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
6112 PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
6113
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05006114 err = pci_enable_device(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006115 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05006116 dev_warn(&h->pdev->dev, "unable to enable PCI device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006117 return err;
6118 }
6119
Stephen M. Cameron5cb460a2012-05-01 11:42:20 -05006120 /* Enable bus mastering (pci_disable_device may disable this) */
6121 pci_set_master(h->pdev);
6122
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06006123 err = pci_request_regions(h->pdev, HPSA);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006124 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05006125 dev_err(&h->pdev->dev,
6126 "cannot obtain PCI resources, aborting\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006127 return err;
6128 }
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05006129 hpsa_interrupt_mode(h);
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05006130 err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05006131 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006132 goto err_out_free_res;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006133 h->vaddr = remap_pci_mem(h->paddr, 0x250);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05006134 if (!h->vaddr) {
6135 err = -ENOMEM;
6136 goto err_out_free_res;
6137 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06006138 err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05006139 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006140 goto err_out_free_res;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05006141 err = hpsa_find_cfgtables(h);
6142 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006143 goto err_out_free_res;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05006144 hpsa_find_board_params(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006145
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05006146 if (!hpsa_CISS_signature_present(h)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006147 err = -ENODEV;
6148 goto err_out_free_res;
6149 }
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06006150 hpsa_set_driver_support_bits(h);
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05006151 hpsa_p600_dma_prefetch_quirk(h);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006152 err = hpsa_enter_simple_mode(h);
6153 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006154 goto err_out_free_res;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006155 return 0;
6156
6157err_out_free_res:
Stephen M. Cameron204892e2010-05-27 15:13:22 -05006158 if (h->transtable)
6159 iounmap(h->transtable);
6160 if (h->cfgtable)
6161 iounmap(h->cfgtable);
6162 if (h->vaddr)
6163 iounmap(h->vaddr);
Stephen M. Cameronf0bd0b62012-05-01 11:42:09 -05006164 pci_disable_device(h->pdev);
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05006165 pci_release_regions(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006166 return err;
6167}
6168
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006169static void hpsa_hba_inquiry(struct ctlr_info *h)
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06006170{
6171 int rc;
6172
6173#define HBA_INQUIRY_BYTE_COUNT 64
6174 h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
6175 if (!h->hba_inquiry_data)
6176 return;
6177 rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
6178 h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
6179 if (rc != 0) {
6180 kfree(h->hba_inquiry_data);
6181 h->hba_inquiry_data = NULL;
6182 }
6183}
6184
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006185static int hpsa_init_reset_devices(struct pci_dev *pdev)
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006186{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006187 int rc, i;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006188
6189 if (!reset_devices)
6190 return 0;
6191
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006192 /* Reset the controller with a PCI power-cycle or via doorbell */
6193 rc = hpsa_kdump_hard_reset_controller(pdev);
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006194
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006195 /* -ENOTSUPP here means we cannot reset the controller
6196 * but it's already (and still) up and running in
Stephen M. Cameron18867652010-06-16 13:51:45 -05006197 * "performant mode". Or, it might be 640x, which can't reset
6198 * due to concerns about shared bbwc between 6402/6404 pair.
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006199 */
6200 if (rc == -ENOTSUPP)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006201 return rc; /* just try to do the kdump anyhow. */
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006202 if (rc)
6203 return -ENODEV;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006204
6205 /* Now try to get the controller to respond to a no-op */
Stephen M. Cameron2b870cb2011-05-03 14:59:36 -05006206 dev_warn(&pdev->dev, "Waiting for controller to respond to no-op\n");
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006207 for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
6208 if (hpsa_noop(pdev) == 0)
6209 break;
6210 else
6211 dev_warn(&pdev->dev, "no-op failed%s\n",
6212 (i < 11 ? "; re-trying" : ""));
6213 }
6214 return 0;
6215}
6216
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006217static int hpsa_allocate_cmd_pool(struct ctlr_info *h)
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05006218{
6219 h->cmd_pool_bits = kzalloc(
6220 DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
6221 sizeof(unsigned long), GFP_KERNEL);
6222 h->cmd_pool = pci_alloc_consistent(h->pdev,
6223 h->nr_cmds * sizeof(*h->cmd_pool),
6224 &(h->cmd_pool_dhandle));
6225 h->errinfo_pool = pci_alloc_consistent(h->pdev,
6226 h->nr_cmds * sizeof(*h->errinfo_pool),
6227 &(h->errinfo_pool_dhandle));
6228 if ((h->cmd_pool_bits == NULL)
6229 || (h->cmd_pool == NULL)
6230 || (h->errinfo_pool == NULL)) {
6231 dev_err(&h->pdev->dev, "out of memory in %s", __func__);
6232 return -ENOMEM;
6233 }
6234 return 0;
6235}
6236
6237static void hpsa_free_cmd_pool(struct ctlr_info *h)
6238{
6239 kfree(h->cmd_pool_bits);
6240 if (h->cmd_pool)
6241 pci_free_consistent(h->pdev,
6242 h->nr_cmds * sizeof(struct CommandList),
6243 h->cmd_pool, h->cmd_pool_dhandle);
Stephen M. Cameronaca90122014-02-18 13:56:14 -06006244 if (h->ioaccel2_cmd_pool)
6245 pci_free_consistent(h->pdev,
6246 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
6247 h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05006248 if (h->errinfo_pool)
6249 pci_free_consistent(h->pdev,
6250 h->nr_cmds * sizeof(struct ErrorInfo),
6251 h->errinfo_pool,
6252 h->errinfo_pool_dhandle);
Matt Gatese1f7de02014-02-18 13:55:17 -06006253 if (h->ioaccel_cmd_pool)
6254 pci_free_consistent(h->pdev,
6255 h->nr_cmds * sizeof(struct io_accel1_cmd),
6256 h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05006257}
6258
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05006259static int hpsa_request_irq(struct ctlr_info *h,
6260 irqreturn_t (*msixhandler)(int, void *),
6261 irqreturn_t (*intxhandler)(int, void *))
6262{
Matt Gates254f7962012-05-01 11:43:06 -05006263 int rc, i;
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05006264
Matt Gates254f7962012-05-01 11:43:06 -05006265 /*
6266 * initialize h->q[x] = x so that interrupt handlers know which
6267 * queue to process.
6268 */
6269 for (i = 0; i < MAX_REPLY_QUEUES; i++)
6270 h->q[i] = (u8) i;
6271
Hannes Reineckeeee0f032014-01-15 13:30:53 +01006272 if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
Matt Gates254f7962012-05-01 11:43:06 -05006273 /* If performant mode and MSI-X, use multiple reply queues */
Hannes Reineckeeee0f032014-01-15 13:30:53 +01006274 for (i = 0; i < h->msix_vector; i++)
Matt Gates254f7962012-05-01 11:43:06 -05006275 rc = request_irq(h->intr[i], msixhandler,
6276 0, h->devname,
6277 &h->q[i]);
6278 } else {
6279 /* Use single reply pool */
Hannes Reineckeeee0f032014-01-15 13:30:53 +01006280 if (h->msix_vector > 0 || h->msi_vector) {
Matt Gates254f7962012-05-01 11:43:06 -05006281 rc = request_irq(h->intr[h->intr_mode],
6282 msixhandler, 0, h->devname,
6283 &h->q[h->intr_mode]);
6284 } else {
6285 rc = request_irq(h->intr[h->intr_mode],
6286 intxhandler, IRQF_SHARED, h->devname,
6287 &h->q[h->intr_mode]);
6288 }
6289 }
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05006290 if (rc) {
6291 dev_err(&h->pdev->dev, "unable to get irq %d for %s\n",
6292 h->intr[h->intr_mode], h->devname);
6293 return -ENODEV;
6294 }
6295 return 0;
6296}
6297
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006298static int hpsa_kdump_soft_reset(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006299{
6300 if (hpsa_send_host_reset(h, RAID_CTLR_LUNID,
6301 HPSA_RESET_TYPE_CONTROLLER)) {
6302 dev_warn(&h->pdev->dev, "Resetting array controller failed.\n");
6303 return -EIO;
6304 }
6305
6306 dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
6307 if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) {
6308 dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
6309 return -1;
6310 }
6311
6312 dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
6313 if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) {
6314 dev_warn(&h->pdev->dev, "Board failed to become ready "
6315 "after soft reset.\n");
6316 return -1;
6317 }
6318
6319 return 0;
6320}
6321
Matt Gates254f7962012-05-01 11:43:06 -05006322static void free_irqs(struct ctlr_info *h)
6323{
6324 int i;
6325
6326 if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
6327 /* Single reply queue, only one irq to free */
6328 i = h->intr_mode;
6329 free_irq(h->intr[i], &h->q[i]);
6330 return;
6331 }
6332
Hannes Reineckeeee0f032014-01-15 13:30:53 +01006333 for (i = 0; i < h->msix_vector; i++)
Matt Gates254f7962012-05-01 11:43:06 -05006334 free_irq(h->intr[i], &h->q[i]);
6335}
6336
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05006337static void hpsa_free_irqs_and_disable_msix(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006338{
Matt Gates254f7962012-05-01 11:43:06 -05006339 free_irqs(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006340#ifdef CONFIG_PCI_MSI
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05006341 if (h->msix_vector) {
6342 if (h->pdev->msix_enabled)
6343 pci_disable_msix(h->pdev);
6344 } else if (h->msi_vector) {
6345 if (h->pdev->msi_enabled)
6346 pci_disable_msi(h->pdev);
6347 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006348#endif /* CONFIG_PCI_MSI */
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05006349}
6350
6351static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
6352{
6353 hpsa_free_irqs_and_disable_msix(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006354 hpsa_free_sg_chain_blocks(h);
6355 hpsa_free_cmd_pool(h);
Matt Gatese1f7de02014-02-18 13:55:17 -06006356 kfree(h->ioaccel1_blockFetchTable);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006357 kfree(h->blockFetchTable);
6358 pci_free_consistent(h->pdev, h->reply_pool_size,
6359 h->reply_pool, h->reply_pool_dhandle);
6360 if (h->vaddr)
6361 iounmap(h->vaddr);
6362 if (h->transtable)
6363 iounmap(h->transtable);
6364 if (h->cfgtable)
6365 iounmap(h->cfgtable);
6366 pci_release_regions(h->pdev);
6367 kfree(h);
6368}
6369
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006370/* Called when controller lockup detected. */
6371static void fail_all_cmds_on_list(struct ctlr_info *h, struct list_head *list)
6372{
6373 struct CommandList *c = NULL;
6374
6375 assert_spin_locked(&h->lock);
6376 /* Mark all outstanding commands as failed and complete them. */
6377 while (!list_empty(list)) {
6378 c = list_entry(list->next, struct CommandList, list);
6379 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05006380 finish_cmd(c);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006381 }
6382}
6383
6384static void controller_lockup_detected(struct ctlr_info *h)
6385{
6386 unsigned long flags;
6387
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006388 h->access.set_intr_mask(h, HPSA_INTR_OFF);
6389 spin_lock_irqsave(&h->lock, flags);
6390 h->lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
6391 spin_unlock_irqrestore(&h->lock, flags);
6392 dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x\n",
6393 h->lockup_detected);
6394 pci_disable_device(h->pdev);
6395 spin_lock_irqsave(&h->lock, flags);
6396 fail_all_cmds_on_list(h, &h->cmpQ);
6397 fail_all_cmds_on_list(h, &h->reqQ);
6398 spin_unlock_irqrestore(&h->lock, flags);
6399}
6400
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006401static void detect_controller_lockup(struct ctlr_info *h)
6402{
6403 u64 now;
6404 u32 heartbeat;
6405 unsigned long flags;
6406
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006407 now = get_jiffies_64();
6408 /* If we've received an interrupt recently, we're ok. */
6409 if (time_after64(h->last_intr_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05006410 (h->heartbeat_sample_interval), now))
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006411 return;
6412
6413 /*
6414 * If we've already checked the heartbeat recently, we're ok.
6415 * This could happen if someone sends us a signal. We
6416 * otherwise don't care about signals in this thread.
6417 */
6418 if (time_after64(h->last_heartbeat_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05006419 (h->heartbeat_sample_interval), now))
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006420 return;
6421
6422 /* If heartbeat has not changed since we last looked, we're not ok. */
6423 spin_lock_irqsave(&h->lock, flags);
6424 heartbeat = readl(&h->cfgtable->HeartBeat);
6425 spin_unlock_irqrestore(&h->lock, flags);
6426 if (h->last_heartbeat == heartbeat) {
6427 controller_lockup_detected(h);
6428 return;
6429 }
6430
6431 /* We're ok. */
6432 h->last_heartbeat = heartbeat;
6433 h->last_heartbeat_timestamp = now;
6434}
6435
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006436static int hpsa_kickoff_rescan(struct ctlr_info *h)
6437{
6438 int i;
6439 char *event_type;
6440
Scott Teele863d682014-02-18 13:57:05 -06006441 /* Clear the driver-requested rescan flag */
6442 h->drv_req_rescan = 0;
6443
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006444 /* Ask the controller to clear the events we're handling. */
Stephen M. Cameron1f7cee82014-02-18 13:56:09 -06006445 if ((h->transMethod & (CFGTBL_Trans_io_accel1
6446 | CFGTBL_Trans_io_accel2)) &&
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006447 (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
6448 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
6449
6450 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
6451 event_type = "state change";
6452 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
6453 event_type = "configuration change";
6454 /* Stop sending new RAID offload reqs via the IO accelerator */
6455 scsi_block_requests(h->scsi_host);
6456 for (i = 0; i < h->ndevices; i++)
6457 h->dev[i]->offload_enabled = 0;
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06006458 hpsa_drain_accel_commands(h);
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006459 /* Set 'accelerator path config change' bit */
6460 dev_warn(&h->pdev->dev,
6461 "Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
6462 h->events, event_type);
6463 writel(h->events, &(h->cfgtable->clear_event_notify));
6464 /* Set the "clear event notify field update" bit 6 */
6465 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
6466 /* Wait until ctlr clears 'clear event notify field', bit 6 */
6467 hpsa_wait_for_clear_event_notify_ack(h);
6468 scsi_unblock_requests(h->scsi_host);
6469 } else {
6470 /* Acknowledge controller notification events. */
6471 writel(h->events, &(h->cfgtable->clear_event_notify));
6472 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
6473 hpsa_wait_for_clear_event_notify_ack(h);
6474#if 0
6475 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
6476 hpsa_wait_for_mode_change_ack(h);
6477#endif
6478 }
6479
6480 /* Something in the device list may have changed to trigger
6481 * the event, so do a rescan.
6482 */
6483 hpsa_scan_start(h->scsi_host);
6484 /* release reference taken on scsi host in check_controller_events */
6485 scsi_host_put(h->scsi_host);
6486 return 0;
6487}
6488
6489/* Check a register on the controller to see if there are configuration
6490 * changes (added/changed/removed logical drives, etc.) which mean that
Scott Teele863d682014-02-18 13:57:05 -06006491 * we should rescan the controller for devices.
6492 * Also check flag for driver-initiated rescan.
6493 * If either flag or controller event indicate rescan, add the controller
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006494 * to the list of controllers needing to be rescanned, and gets a
6495 * reference to the associated scsi_host.
6496 */
6497static void hpsa_ctlr_needs_rescan(struct ctlr_info *h)
6498{
6499 if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
6500 return;
6501
6502 h->events = readl(&(h->cfgtable->event_notify));
Stephen M. Cameronfaff6ee2014-02-18 13:57:42 -06006503 if (!(h->events & RESCAN_REQUIRED_EVENT_BITS) && !h->drv_req_rescan)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006504 return;
6505
6506 /*
6507 * Take a reference on scsi host for the duration of the scan
6508 * Release in hpsa_kickoff_rescan(). No lock needed for scan_list
6509 * as only a single thread accesses this list.
6510 */
6511 scsi_host_get(h->scsi_host);
6512 hpsa_kickoff_rescan(h);
6513}
6514
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006515static void hpsa_monitor_ctlr_worker(struct work_struct *work)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006516{
6517 unsigned long flags;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006518 struct ctlr_info *h = container_of(to_delayed_work(work),
6519 struct ctlr_info, monitor_ctlr_work);
6520 detect_controller_lockup(h);
6521 if (h->lockup_detected)
6522 return;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006523 hpsa_ctlr_needs_rescan(h);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006524 spin_lock_irqsave(&h->lock, flags);
6525 if (h->remove_in_progress) {
6526 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006527 return;
6528 }
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006529 schedule_delayed_work(&h->monitor_ctlr_work,
6530 h->heartbeat_sample_interval);
6531 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006532}
6533
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006534static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006535{
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006536 int dac, rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006537 struct ctlr_info *h;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006538 int try_soft_reset = 0;
6539 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006540
6541 if (number_of_controllers == 0)
6542 printk(KERN_INFO DRIVER_NAME "\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006543
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006544 rc = hpsa_init_reset_devices(pdev);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006545 if (rc) {
6546 if (rc != -ENOTSUPP)
6547 return rc;
6548 /* If the reset fails in a particular way (it has no way to do
6549 * a proper hard reset, so returns -ENOTSUPP) we can try to do
6550 * a soft reset once we get the controller configured up to the
6551 * point that it can accept a command.
6552 */
6553 try_soft_reset = 1;
6554 rc = 0;
6555 }
6556
6557reinit_after_soft_reset:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006558
Don Brace303932f2010-02-04 08:42:40 -06006559 /* Command structures must be aligned on a 32-byte boundary because
6560 * the 5 lower bits of the address are used by the hardware. and by
6561 * the driver. See comments in hpsa.h for more info.
6562 */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006563#define COMMANDLIST_ALIGNMENT 128
Don Brace303932f2010-02-04 08:42:40 -06006564 BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006565 h = kzalloc(sizeof(*h), GFP_KERNEL);
6566 if (!h)
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006567 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006568
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05006569 h->pdev = pdev;
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06006570 h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06006571 INIT_LIST_HEAD(&h->cmpQ);
6572 INIT_LIST_HEAD(&h->reqQ);
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06006573 spin_lock_init(&h->lock);
6574 spin_lock_init(&h->scan_lock);
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006575 spin_lock_init(&h->passthru_count_lock);
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05006576 rc = hpsa_pci_init(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006577 if (rc != 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006578 goto clean1;
6579
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06006580 sprintf(h->devname, HPSA "%d", number_of_controllers);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006581 h->ctlr = number_of_controllers;
6582 number_of_controllers++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006583
6584 /* configure PCI DMA stuff */
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006585 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
6586 if (rc == 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006587 dac = 1;
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006588 } else {
6589 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6590 if (rc == 0) {
6591 dac = 0;
6592 } else {
6593 dev_err(&pdev->dev, "no suitable DMA available\n");
6594 goto clean1;
6595 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006596 }
6597
6598 /* make sure the board interrupts are off */
6599 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006600
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05006601 if (hpsa_request_irq(h, do_hpsa_intr_msi, do_hpsa_intr_intx))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006602 goto clean2;
Don Brace303932f2010-02-04 08:42:40 -06006603 dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
6604 h->devname, pdev->device,
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06006605 h->intr[h->intr_mode], dac ? "" : " not");
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05006606 if (hpsa_allocate_cmd_pool(h))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006607 goto clean4;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06006608 if (hpsa_allocate_sg_chain_blocks(h))
6609 goto clean4;
Stephen M. Camerona08a8472010-02-04 08:43:16 -06006610 init_waitqueue_head(&h->scan_wait_queue);
6611 h->scan_finished = 1; /* no scan currently in progress */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006612
6613 pci_set_drvdata(pdev, h);
Stephen M. Cameron9a413382011-05-03 14:59:41 -05006614 h->ndevices = 0;
6615 h->scsi_host = NULL;
6616 spin_lock_init(&h->devlock);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006617 hpsa_put_ctlr_into_performant_mode(h);
6618
6619 /* At this point, the controller is ready to take commands.
6620 * Now, if reset_devices and the hard reset didn't work, try
6621 * the soft reset and see if that works.
6622 */
6623 if (try_soft_reset) {
6624
6625 /* This is kind of gross. We may or may not get a completion
6626 * from the soft reset command, and if we do, then the value
6627 * from the fifo may or may not be valid. So, we wait 10 secs
6628 * after the reset throwing away any completions we get during
6629 * that time. Unregister the interrupt handler and register
6630 * fake ones to scoop up any residual completions.
6631 */
6632 spin_lock_irqsave(&h->lock, flags);
6633 h->access.set_intr_mask(h, HPSA_INTR_OFF);
6634 spin_unlock_irqrestore(&h->lock, flags);
Matt Gates254f7962012-05-01 11:43:06 -05006635 free_irqs(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006636 rc = hpsa_request_irq(h, hpsa_msix_discard_completions,
6637 hpsa_intx_discard_completions);
6638 if (rc) {
6639 dev_warn(&h->pdev->dev, "Failed to request_irq after "
6640 "soft reset.\n");
6641 goto clean4;
6642 }
6643
6644 rc = hpsa_kdump_soft_reset(h);
6645 if (rc)
6646 /* Neither hard nor soft reset worked, we're hosed. */
6647 goto clean4;
6648
6649 dev_info(&h->pdev->dev, "Board READY.\n");
6650 dev_info(&h->pdev->dev,
6651 "Waiting for stale completions to drain.\n");
6652 h->access.set_intr_mask(h, HPSA_INTR_ON);
6653 msleep(10000);
6654 h->access.set_intr_mask(h, HPSA_INTR_OFF);
6655
6656 rc = controller_reset_failed(h->cfgtable);
6657 if (rc)
6658 dev_info(&h->pdev->dev,
6659 "Soft reset appears to have failed.\n");
6660
6661 /* since the controller's reset, we have to go back and re-init
6662 * everything. Easiest to just forget what we've done and do it
6663 * all over again.
6664 */
6665 hpsa_undo_allocations_after_kdump_soft_reset(h);
6666 try_soft_reset = 0;
6667 if (rc)
6668 /* don't go to clean4, we already unallocated */
6669 return -ENODEV;
6670
6671 goto reinit_after_soft_reset;
6672 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006673
Scott Teelda0697b2014-02-18 13:57:00 -06006674 /* Enable Accelerated IO path at driver layer */
6675 h->acciopath_status = 1;
6676
Scott Teele863d682014-02-18 13:57:05 -06006677 h->drv_req_rescan = 0;
6678
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006679 /* Turn the interrupts on so we can service requests */
6680 h->access.set_intr_mask(h, HPSA_INTR_ON);
6681
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06006682 hpsa_hba_inquiry(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006683 hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006684
6685 /* Monitor the controller for firmware lockups */
6686 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
6687 INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
6688 schedule_delayed_work(&h->monitor_ctlr_work,
6689 h->heartbeat_sample_interval);
Stephen M. Cameron88bf6d62013-11-01 11:02:25 -05006690 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006691
6692clean4:
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06006693 hpsa_free_sg_chain_blocks(h);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05006694 hpsa_free_cmd_pool(h);
Matt Gates254f7962012-05-01 11:43:06 -05006695 free_irqs(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006696clean2:
6697clean1:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006698 kfree(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006699 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006700}
6701
6702static void hpsa_flush_cache(struct ctlr_info *h)
6703{
6704 char *flush_buf;
6705 struct CommandList *c;
Stephen M. Cameron702890e2013-09-23 13:33:30 -05006706 unsigned long flags;
6707
6708 /* Don't bother trying to flush the cache if locked up */
6709 spin_lock_irqsave(&h->lock, flags);
6710 if (unlikely(h->lockup_detected)) {
6711 spin_unlock_irqrestore(&h->lock, flags);
6712 return;
6713 }
6714 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006715
6716 flush_buf = kzalloc(4, GFP_KERNEL);
6717 if (!flush_buf)
6718 return;
6719
6720 c = cmd_special_alloc(h);
6721 if (!c) {
6722 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
6723 goto out_of_memory;
6724 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006725 if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
6726 RAID_CTLR_LUNID, TYPE_CMD)) {
6727 goto out;
6728 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006729 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
6730 if (c->err_info->CommandStatus != 0)
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006731out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006732 dev_warn(&h->pdev->dev,
6733 "error flushing cache on controller\n");
6734 cmd_special_free(h, c);
6735out_of_memory:
6736 kfree(flush_buf);
6737}
6738
6739static void hpsa_shutdown(struct pci_dev *pdev)
6740{
6741 struct ctlr_info *h;
6742
6743 h = pci_get_drvdata(pdev);
6744 /* Turn board interrupts off and send the flush cache command
6745 * sendcmd will turn off interrupt, and send the flush...
6746 * To write all data in the battery backed cache to disks
6747 */
6748 hpsa_flush_cache(h);
6749 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05006750 hpsa_free_irqs_and_disable_msix(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006751}
6752
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006753static void hpsa_free_device_info(struct ctlr_info *h)
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06006754{
6755 int i;
6756
6757 for (i = 0; i < h->ndevices; i++)
6758 kfree(h->dev[i]);
6759}
6760
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006761static void hpsa_remove_one(struct pci_dev *pdev)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006762{
6763 struct ctlr_info *h;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006764 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006765
6766 if (pci_get_drvdata(pdev) == NULL) {
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006767 dev_err(&pdev->dev, "unable to remove device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006768 return;
6769 }
6770 h = pci_get_drvdata(pdev);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006771
6772 /* Get rid of any controller monitoring work items */
6773 spin_lock_irqsave(&h->lock, flags);
6774 h->remove_in_progress = 1;
6775 cancel_delayed_work(&h->monitor_ctlr_work);
6776 spin_unlock_irqrestore(&h->lock, flags);
6777
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006778 hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */
6779 hpsa_shutdown(pdev);
6780 iounmap(h->vaddr);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05006781 iounmap(h->transtable);
6782 iounmap(h->cfgtable);
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06006783 hpsa_free_device_info(h);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06006784 hpsa_free_sg_chain_blocks(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006785 pci_free_consistent(h->pdev,
6786 h->nr_cmds * sizeof(struct CommandList),
6787 h->cmd_pool, h->cmd_pool_dhandle);
6788 pci_free_consistent(h->pdev,
6789 h->nr_cmds * sizeof(struct ErrorInfo),
6790 h->errinfo_pool, h->errinfo_pool_dhandle);
Don Brace303932f2010-02-04 08:42:40 -06006791 pci_free_consistent(h->pdev, h->reply_pool_size,
6792 h->reply_pool, h->reply_pool_dhandle);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006793 kfree(h->cmd_pool_bits);
Don Brace303932f2010-02-04 08:42:40 -06006794 kfree(h->blockFetchTable);
Matt Gatese1f7de02014-02-18 13:55:17 -06006795 kfree(h->ioaccel1_blockFetchTable);
Stephen M. Cameronaca90122014-02-18 13:56:14 -06006796 kfree(h->ioaccel2_blockFetchTable);
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06006797 kfree(h->hba_inquiry_data);
Stephen M. Cameronf0bd0b62012-05-01 11:42:09 -05006798 pci_disable_device(pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006799 pci_release_regions(pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006800 kfree(h);
6801}
6802
6803static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
6804 __attribute__((unused)) pm_message_t state)
6805{
6806 return -ENOSYS;
6807}
6808
6809static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
6810{
6811 return -ENOSYS;
6812}
6813
6814static struct pci_driver hpsa_pci_driver = {
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06006815 .name = HPSA,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006816 .probe = hpsa_init_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006817 .remove = hpsa_remove_one,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006818 .id_table = hpsa_pci_device_id, /* id_table */
6819 .shutdown = hpsa_shutdown,
6820 .suspend = hpsa_suspend,
6821 .resume = hpsa_resume,
6822};
6823
Don Brace303932f2010-02-04 08:42:40 -06006824/* Fill in bucket_map[], given nsgs (the max number of
6825 * scatter gather elements supported) and bucket[],
6826 * which is an array of 8 integers. The bucket[] array
6827 * contains 8 different DMA transfer sizes (in 16
6828 * byte increments) which the controller uses to fetch
6829 * commands. This function fills in bucket_map[], which
6830 * maps a given number of scatter gather elements to one of
6831 * the 8 DMA transfer sizes. The point of it is to allow the
6832 * controller to only do as much DMA as needed to fetch the
6833 * command, with the DMA transfer size encoded in the lower
6834 * bits of the command address.
6835 */
6836static void calc_bucket_map(int bucket[], int num_buckets,
Matt Gatese1f7de02014-02-18 13:55:17 -06006837 int nsgs, int min_blocks, int *bucket_map)
Don Brace303932f2010-02-04 08:42:40 -06006838{
6839 int i, j, b, size;
6840
Don Brace303932f2010-02-04 08:42:40 -06006841 /* Note, bucket_map must have nsgs+1 entries. */
6842 for (i = 0; i <= nsgs; i++) {
6843 /* Compute size of a command with i SG entries */
Matt Gatese1f7de02014-02-18 13:55:17 -06006844 size = i + min_blocks;
Don Brace303932f2010-02-04 08:42:40 -06006845 b = num_buckets; /* Assume the biggest bucket */
6846 /* Find the bucket that is just big enough */
Matt Gatese1f7de02014-02-18 13:55:17 -06006847 for (j = 0; j < num_buckets; j++) {
Don Brace303932f2010-02-04 08:42:40 -06006848 if (bucket[j] >= size) {
6849 b = j;
6850 break;
6851 }
6852 }
6853 /* for a command with i SG entries, use bucket b. */
6854 bucket_map[i] = b;
6855 }
6856}
6857
Matt Gatese1f7de02014-02-18 13:55:17 -06006858static void hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
Don Brace303932f2010-02-04 08:42:40 -06006859{
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006860 int i;
6861 unsigned long register_value;
Matt Gatese1f7de02014-02-18 13:55:17 -06006862 unsigned long transMethod = CFGTBL_Trans_Performant |
6863 (trans_support & CFGTBL_Trans_use_short_tags) |
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006864 CFGTBL_Trans_enable_directed_msix |
6865 (trans_support & (CFGTBL_Trans_io_accel1 |
6866 CFGTBL_Trans_io_accel2));
Matt Gatese1f7de02014-02-18 13:55:17 -06006867 struct access_method access = SA5_performant_access;
Stephen M. Camerondef342b2010-05-27 15:14:39 -05006868
6869 /* This is a bit complicated. There are 8 registers on
6870 * the controller which we write to to tell it 8 different
6871 * sizes of commands which there may be. It's a way of
6872 * reducing the DMA done to fetch each command. Encoded into
6873 * each command's tag are 3 bits which communicate to the controller
6874 * which of the eight sizes that command fits within. The size of
6875 * each command depends on how many scatter gather entries there are.
6876 * Each SG entry requires 16 bytes. The eight registers are programmed
6877 * with the number of 16-byte blocks a command of that size requires.
6878 * The smallest command possible requires 5 such 16 byte blocks.
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006879 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
Stephen M. Camerondef342b2010-05-27 15:14:39 -05006880 * blocks. Note, this only extends to the SG entries contained
6881 * within the command block, and does not extend to chained blocks
6882 * of SG elements. bft[] contains the eight values we write to
6883 * the registers. They are not evenly distributed, but have more
6884 * sizes for small commands, and fewer sizes for larger commands.
6885 */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006886 int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006887#define MIN_IOACCEL2_BFT_ENTRY 5
6888#define HPSA_IOACCEL2_HEADER_SZ 4
6889 int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
6890 13, 14, 15, 16, 17, 18, 19,
6891 HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
6892 BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
6893 BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
6894 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
6895 16 * MIN_IOACCEL2_BFT_ENTRY);
6896 BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006897 BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
Don Brace303932f2010-02-04 08:42:40 -06006898 /* 5 = 1 s/g entry or 4k
6899 * 6 = 2 s/g entry or 8k
6900 * 8 = 4 s/g entry or 16k
6901 * 10 = 6 s/g entry or 24k
6902 */
Don Brace303932f2010-02-04 08:42:40 -06006903
Don Brace303932f2010-02-04 08:42:40 -06006904 /* Controller spec: zero out this buffer. */
6905 memset(h->reply_pool, 0, h->reply_pool_size);
Don Brace303932f2010-02-04 08:42:40 -06006906
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006907 bft[7] = SG_ENTRIES_IN_CMD + 4;
6908 calc_bucket_map(bft, ARRAY_SIZE(bft),
Matt Gatese1f7de02014-02-18 13:55:17 -06006909 SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
Don Brace303932f2010-02-04 08:42:40 -06006910 for (i = 0; i < 8; i++)
6911 writel(bft[i], &h->transtable->BlockFetch[i]);
6912
6913 /* size of controller ring buffer */
6914 writel(h->max_commands, &h->transtable->RepQSize);
Matt Gates254f7962012-05-01 11:43:06 -05006915 writel(h->nreply_queues, &h->transtable->RepQCount);
Don Brace303932f2010-02-04 08:42:40 -06006916 writel(0, &h->transtable->RepQCtrAddrLow32);
6917 writel(0, &h->transtable->RepQCtrAddrHigh32);
Matt Gates254f7962012-05-01 11:43:06 -05006918
6919 for (i = 0; i < h->nreply_queues; i++) {
6920 writel(0, &h->transtable->RepQAddr[i].upper);
6921 writel(h->reply_pool_dhandle +
6922 (h->max_commands * sizeof(u64) * i),
6923 &h->transtable->RepQAddr[i].lower);
6924 }
6925
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006926 writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
Matt Gatese1f7de02014-02-18 13:55:17 -06006927 writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
6928 /*
6929 * enable outbound interrupt coalescing in accelerator mode;
6930 */
6931 if (trans_support & CFGTBL_Trans_io_accel1) {
6932 access = SA5_ioaccel_mode1_access;
6933 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
6934 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
Scott Teelc3497752014-02-18 13:56:34 -06006935 } else {
6936 if (trans_support & CFGTBL_Trans_io_accel2) {
6937 access = SA5_ioaccel_mode2_access;
6938 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
6939 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
6940 }
Matt Gatese1f7de02014-02-18 13:55:17 -06006941 }
Don Brace303932f2010-02-04 08:42:40 -06006942 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05006943 hpsa_wait_for_mode_change_ack(h);
Don Brace303932f2010-02-04 08:42:40 -06006944 register_value = readl(&(h->cfgtable->TransportActive));
6945 if (!(register_value & CFGTBL_Trans_Performant)) {
6946 dev_warn(&h->pdev->dev, "unable to get board into"
6947 " performant mode\n");
6948 return;
6949 }
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06006950 /* Change the access methods to the performant access methods */
Matt Gatese1f7de02014-02-18 13:55:17 -06006951 h->access = access;
6952 h->transMethod = transMethod;
6953
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006954 if (!((trans_support & CFGTBL_Trans_io_accel1) ||
6955 (trans_support & CFGTBL_Trans_io_accel2)))
Matt Gatese1f7de02014-02-18 13:55:17 -06006956 return;
6957
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006958 if (trans_support & CFGTBL_Trans_io_accel1) {
6959 /* Set up I/O accelerator mode */
6960 for (i = 0; i < h->nreply_queues; i++) {
6961 writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
6962 h->reply_queue[i].current_entry =
6963 readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
6964 }
6965 bft[7] = h->ioaccel_maxsg + 8;
6966 calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
6967 h->ioaccel1_blockFetchTable);
6968
6969 /* initialize all reply queue entries to unused */
6970 memset(h->reply_pool, (u8) IOACCEL_MODE1_REPLY_UNUSED,
6971 h->reply_pool_size);
6972
6973 /* set all the constant fields in the accelerator command
6974 * frames once at init time to save CPU cycles later.
6975 */
6976 for (i = 0; i < h->nr_cmds; i++) {
6977 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
6978
6979 cp->function = IOACCEL1_FUNCTION_SCSIIO;
6980 cp->err_info = (u32) (h->errinfo_pool_dhandle +
6981 (i * sizeof(struct ErrorInfo)));
6982 cp->err_info_len = sizeof(struct ErrorInfo);
6983 cp->sgl_offset = IOACCEL1_SGLOFFSET;
6984 cp->host_context_flags = IOACCEL1_HCFLAGS_CISS_FORMAT;
6985 cp->timeout_sec = 0;
6986 cp->ReplyQueue = 0;
6987 cp->Tag.lower = (i << DIRECT_LOOKUP_SHIFT) |
6988 DIRECT_LOOKUP_BIT;
6989 cp->Tag.upper = 0;
6990 cp->host_addr.lower =
6991 (u32) (h->ioaccel_cmd_pool_dhandle +
6992 (i * sizeof(struct io_accel1_cmd)));
6993 cp->host_addr.upper = 0;
6994 }
6995 } else if (trans_support & CFGTBL_Trans_io_accel2) {
6996 u64 cfg_offset, cfg_base_addr_index;
6997 u32 bft2_offset, cfg_base_addr;
6998 int rc;
6999
7000 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
7001 &cfg_base_addr_index, &cfg_offset);
7002 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
7003 bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
7004 calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
7005 4, h->ioaccel2_blockFetchTable);
7006 bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
7007 BUILD_BUG_ON(offsetof(struct CfgTable,
7008 io_accel_request_size_offset) != 0xb8);
7009 h->ioaccel2_bft2_regs =
7010 remap_pci_mem(pci_resource_start(h->pdev,
7011 cfg_base_addr_index) +
7012 cfg_offset + bft2_offset,
7013 ARRAY_SIZE(bft2) *
7014 sizeof(*h->ioaccel2_bft2_regs));
7015 for (i = 0; i < ARRAY_SIZE(bft2); i++)
7016 writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
Matt Gatese1f7de02014-02-18 13:55:17 -06007017 }
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06007018 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
7019 hpsa_wait_for_mode_change_ack(h);
Matt Gatese1f7de02014-02-18 13:55:17 -06007020}
7021
7022static int hpsa_alloc_ioaccel_cmd_and_bft(struct ctlr_info *h)
7023{
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007024 h->ioaccel_maxsg =
7025 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
7026 if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
7027 h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
7028
Matt Gatese1f7de02014-02-18 13:55:17 -06007029 /* Command structures must be aligned on a 128-byte boundary
7030 * because the 7 lower bits of the address are used by the
7031 * hardware.
7032 */
7033#define IOACCEL1_COMMANDLIST_ALIGNMENT 128
7034 BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
7035 IOACCEL1_COMMANDLIST_ALIGNMENT);
7036 h->ioaccel_cmd_pool =
7037 pci_alloc_consistent(h->pdev,
7038 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
7039 &(h->ioaccel_cmd_pool_dhandle));
7040
7041 h->ioaccel1_blockFetchTable =
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007042 kmalloc(((h->ioaccel_maxsg + 1) *
Matt Gatese1f7de02014-02-18 13:55:17 -06007043 sizeof(u32)), GFP_KERNEL);
7044
7045 if ((h->ioaccel_cmd_pool == NULL) ||
7046 (h->ioaccel1_blockFetchTable == NULL))
7047 goto clean_up;
7048
7049 memset(h->ioaccel_cmd_pool, 0,
7050 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
7051 return 0;
7052
7053clean_up:
7054 if (h->ioaccel_cmd_pool)
7055 pci_free_consistent(h->pdev,
7056 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
7057 h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
7058 kfree(h->ioaccel1_blockFetchTable);
7059 return 1;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05007060}
7061
Stephen M. Cameronaca90122014-02-18 13:56:14 -06007062static int ioaccel2_alloc_cmds_and_bft(struct ctlr_info *h)
7063{
7064 /* Allocate ioaccel2 mode command blocks and block fetch table */
7065
7066 h->ioaccel_maxsg =
7067 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
7068 if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
7069 h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
7070
7071#define IOACCEL2_COMMANDLIST_ALIGNMENT 128
7072 BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
7073 IOACCEL2_COMMANDLIST_ALIGNMENT);
7074 h->ioaccel2_cmd_pool =
7075 pci_alloc_consistent(h->pdev,
7076 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
7077 &(h->ioaccel2_cmd_pool_dhandle));
7078
7079 h->ioaccel2_blockFetchTable =
7080 kmalloc(((h->ioaccel_maxsg + 1) *
7081 sizeof(u32)), GFP_KERNEL);
7082
7083 if ((h->ioaccel2_cmd_pool == NULL) ||
7084 (h->ioaccel2_blockFetchTable == NULL))
7085 goto clean_up;
7086
7087 memset(h->ioaccel2_cmd_pool, 0,
7088 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
7089 return 0;
7090
7091clean_up:
7092 if (h->ioaccel2_cmd_pool)
7093 pci_free_consistent(h->pdev,
7094 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
7095 h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
7096 kfree(h->ioaccel2_blockFetchTable);
7097 return 1;
7098}
7099
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007100static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05007101{
7102 u32 trans_support;
Matt Gatese1f7de02014-02-18 13:55:17 -06007103 unsigned long transMethod = CFGTBL_Trans_Performant |
7104 CFGTBL_Trans_use_short_tags;
Matt Gates254f7962012-05-01 11:43:06 -05007105 int i;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05007106
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -06007107 if (hpsa_simple_mode)
7108 return;
7109
Matt Gatese1f7de02014-02-18 13:55:17 -06007110 /* Check for I/O accelerator mode support */
7111 if (trans_support & CFGTBL_Trans_io_accel1) {
7112 transMethod |= CFGTBL_Trans_io_accel1 |
7113 CFGTBL_Trans_enable_directed_msix;
7114 if (hpsa_alloc_ioaccel_cmd_and_bft(h))
7115 goto clean_up;
Stephen M. Cameronaca90122014-02-18 13:56:14 -06007116 } else {
7117 if (trans_support & CFGTBL_Trans_io_accel2) {
7118 transMethod |= CFGTBL_Trans_io_accel2 |
7119 CFGTBL_Trans_enable_directed_msix;
7120 if (ioaccel2_alloc_cmds_and_bft(h))
7121 goto clean_up;
7122 }
Matt Gatese1f7de02014-02-18 13:55:17 -06007123 }
7124
7125 /* TODO, check that this next line h->nreply_queues is correct */
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05007126 trans_support = readl(&(h->cfgtable->TransportSupport));
7127 if (!(trans_support & PERFORMANT_MODE))
7128 return;
7129
Hannes Reineckeeee0f032014-01-15 13:30:53 +01007130 h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05007131 hpsa_get_max_perf_mode_cmds(h);
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05007132 /* Performant mode ring buffer and supporting data structures */
Matt Gates254f7962012-05-01 11:43:06 -05007133 h->reply_pool_size = h->max_commands * sizeof(u64) * h->nreply_queues;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05007134 h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size,
7135 &(h->reply_pool_dhandle));
7136
Matt Gates254f7962012-05-01 11:43:06 -05007137 for (i = 0; i < h->nreply_queues; i++) {
7138 h->reply_queue[i].head = &h->reply_pool[h->max_commands * i];
7139 h->reply_queue[i].size = h->max_commands;
7140 h->reply_queue[i].wraparound = 1; /* spec: init to 1 */
7141 h->reply_queue[i].current_entry = 0;
7142 }
7143
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05007144 /* Need a block fetch table for performant mode */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06007145 h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05007146 sizeof(u32)), GFP_KERNEL);
7147
7148 if ((h->reply_pool == NULL)
7149 || (h->blockFetchTable == NULL))
7150 goto clean_up;
7151
Matt Gatese1f7de02014-02-18 13:55:17 -06007152 hpsa_enter_performant_mode(h, trans_support);
Don Brace303932f2010-02-04 08:42:40 -06007153 return;
7154
7155clean_up:
7156 if (h->reply_pool)
7157 pci_free_consistent(h->pdev, h->reply_pool_size,
7158 h->reply_pool, h->reply_pool_dhandle);
7159 kfree(h->blockFetchTable);
7160}
7161
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06007162static int is_accelerated_cmd(struct CommandList *c)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007163{
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06007164 return c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_IOACCEL2;
7165}
7166
7167static void hpsa_drain_accel_commands(struct ctlr_info *h)
7168{
7169 struct CommandList *c = NULL;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007170 unsigned long flags;
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06007171 int accel_cmds_out;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007172
7173 do { /* wait for all outstanding commands to drain out */
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06007174 accel_cmds_out = 0;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007175 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06007176 list_for_each_entry(c, &h->cmpQ, list)
7177 accel_cmds_out += is_accelerated_cmd(c);
7178 list_for_each_entry(c, &h->reqQ, list)
7179 accel_cmds_out += is_accelerated_cmd(c);
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007180 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06007181 if (accel_cmds_out <= 0)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007182 break;
7183 msleep(100);
7184 } while (1);
7185}
7186
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007187/*
7188 * This is it. Register the PCI driver information for the cards we control
7189 * the OS will call our registered routines when it finds one of our cards.
7190 */
7191static int __init hpsa_init(void)
7192{
Mike Miller31468402010-02-25 14:03:12 -06007193 return pci_register_driver(&hpsa_pci_driver);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007194}
7195
7196static void __exit hpsa_cleanup(void)
7197{
7198 pci_unregister_driver(&hpsa_pci_driver);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007199}
7200
Matt Gatese1f7de02014-02-18 13:55:17 -06007201static void __attribute__((unused)) verify_offsets(void)
7202{
7203#define VERIFY_OFFSET(member, offset) \
Scott Teeldd0e19f2014-02-18 13:57:31 -06007204 BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
7205
7206 VERIFY_OFFSET(structure_size, 0);
7207 VERIFY_OFFSET(volume_blk_size, 4);
7208 VERIFY_OFFSET(volume_blk_cnt, 8);
7209 VERIFY_OFFSET(phys_blk_shift, 16);
7210 VERIFY_OFFSET(parity_rotation_shift, 17);
7211 VERIFY_OFFSET(strip_size, 18);
7212 VERIFY_OFFSET(disk_starting_blk, 20);
7213 VERIFY_OFFSET(disk_blk_cnt, 28);
7214 VERIFY_OFFSET(data_disks_per_row, 36);
7215 VERIFY_OFFSET(metadata_disks_per_row, 38);
7216 VERIFY_OFFSET(row_cnt, 40);
7217 VERIFY_OFFSET(layout_map_count, 42);
7218 VERIFY_OFFSET(flags, 44);
7219 VERIFY_OFFSET(dekindex, 46);
7220 /* VERIFY_OFFSET(reserved, 48 */
7221 VERIFY_OFFSET(data, 64);
7222
7223#undef VERIFY_OFFSET
7224
7225#define VERIFY_OFFSET(member, offset) \
Mike Millerb66cc252014-02-18 13:56:04 -06007226 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
7227
7228 VERIFY_OFFSET(IU_type, 0);
7229 VERIFY_OFFSET(direction, 1);
7230 VERIFY_OFFSET(reply_queue, 2);
7231 /* VERIFY_OFFSET(reserved1, 3); */
7232 VERIFY_OFFSET(scsi_nexus, 4);
7233 VERIFY_OFFSET(Tag, 8);
7234 VERIFY_OFFSET(cdb, 16);
7235 VERIFY_OFFSET(cciss_lun, 32);
7236 VERIFY_OFFSET(data_len, 40);
7237 VERIFY_OFFSET(cmd_priority_task_attr, 44);
7238 VERIFY_OFFSET(sg_count, 45);
7239 /* VERIFY_OFFSET(reserved3 */
7240 VERIFY_OFFSET(err_ptr, 48);
7241 VERIFY_OFFSET(err_len, 56);
7242 /* VERIFY_OFFSET(reserved4 */
7243 VERIFY_OFFSET(sg, 64);
7244
7245#undef VERIFY_OFFSET
7246
7247#define VERIFY_OFFSET(member, offset) \
Matt Gatese1f7de02014-02-18 13:55:17 -06007248 BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
7249
7250 VERIFY_OFFSET(dev_handle, 0x00);
7251 VERIFY_OFFSET(reserved1, 0x02);
7252 VERIFY_OFFSET(function, 0x03);
7253 VERIFY_OFFSET(reserved2, 0x04);
7254 VERIFY_OFFSET(err_info, 0x0C);
7255 VERIFY_OFFSET(reserved3, 0x10);
7256 VERIFY_OFFSET(err_info_len, 0x12);
7257 VERIFY_OFFSET(reserved4, 0x13);
7258 VERIFY_OFFSET(sgl_offset, 0x14);
7259 VERIFY_OFFSET(reserved5, 0x15);
7260 VERIFY_OFFSET(transfer_len, 0x1C);
7261 VERIFY_OFFSET(reserved6, 0x20);
7262 VERIFY_OFFSET(io_flags, 0x24);
7263 VERIFY_OFFSET(reserved7, 0x26);
7264 VERIFY_OFFSET(LUN, 0x34);
7265 VERIFY_OFFSET(control, 0x3C);
7266 VERIFY_OFFSET(CDB, 0x40);
7267 VERIFY_OFFSET(reserved8, 0x50);
7268 VERIFY_OFFSET(host_context_flags, 0x60);
7269 VERIFY_OFFSET(timeout_sec, 0x62);
7270 VERIFY_OFFSET(ReplyQueue, 0x64);
7271 VERIFY_OFFSET(reserved9, 0x65);
7272 VERIFY_OFFSET(Tag, 0x68);
7273 VERIFY_OFFSET(host_addr, 0x70);
7274 VERIFY_OFFSET(CISS_LUN, 0x78);
7275 VERIFY_OFFSET(SG, 0x78 + 8);
7276#undef VERIFY_OFFSET
7277}
7278
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007279module_init(hpsa_init);
7280module_exit(hpsa_cleanup);