blob: 49ad8c56c535e7fa3b926e6e6670d285c8226114 [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},
Stephen M. Cameron8e616a52014-02-18 13:58:02 -0600122 {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0076},
123 {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0087},
124 {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x007D},
125 {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0088},
126 {PCI_VENDOR_ID_HP, 0x333f, 0x103c, 0x333f},
Mike Miller7c03b872010-12-01 11:16:07 -0600127 {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
Stephen M. Cameron6798cc02010-06-16 13:51:20 -0500128 PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800129 {0,}
130};
131
132MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
133
134/* board_id = Subsystem Device ID & Vendor ID
135 * product = Marketing Name for the board
136 * access = Address of the struct of function pointers
137 */
138static struct board_type products[] = {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800139 {0x3241103C, "Smart Array P212", &SA5_access},
140 {0x3243103C, "Smart Array P410", &SA5_access},
141 {0x3245103C, "Smart Array P410i", &SA5_access},
142 {0x3247103C, "Smart Array P411", &SA5_access},
143 {0x3249103C, "Smart Array P812", &SA5_access},
Mike Miller163dbcd2013-09-04 15:11:10 -0500144 {0x324A103C, "Smart Array P712m", &SA5_access},
145 {0x324B103C, "Smart Array P711m", &SA5_access},
Mike Millerfe0c9612012-09-20 16:05:18 -0500146 {0x3350103C, "Smart Array P222", &SA5_access},
147 {0x3351103C, "Smart Array P420", &SA5_access},
148 {0x3352103C, "Smart Array P421", &SA5_access},
149 {0x3353103C, "Smart Array P822", &SA5_access},
150 {0x3354103C, "Smart Array P420i", &SA5_access},
151 {0x3355103C, "Smart Array P220i", &SA5_access},
152 {0x3356103C, "Smart Array P721m", &SA5_access},
Mike Miller1fd6c8e2013-09-04 15:08:29 -0500153 {0x1921103C, "Smart Array P830i", &SA5_access},
154 {0x1922103C, "Smart Array P430", &SA5_access},
155 {0x1923103C, "Smart Array P431", &SA5_access},
156 {0x1924103C, "Smart Array P830", &SA5_access},
157 {0x1926103C, "Smart Array P731m", &SA5_access},
158 {0x1928103C, "Smart Array P230i", &SA5_access},
159 {0x1929103C, "Smart Array P530", &SA5_access},
Mike Miller97b9f532013-09-04 15:05:55 -0500160 {0x21BD103C, "Smart Array", &SA5_access},
161 {0x21BE103C, "Smart Array", &SA5_access},
162 {0x21BF103C, "Smart Array", &SA5_access},
163 {0x21C0103C, "Smart Array", &SA5_access},
164 {0x21C1103C, "Smart Array", &SA5_access},
165 {0x21C2103C, "Smart Array", &SA5_access},
166 {0x21C3103C, "Smart Array", &SA5_access},
167 {0x21C4103C, "Smart Array", &SA5_access},
168 {0x21C5103C, "Smart Array", &SA5_access},
169 {0x21C7103C, "Smart Array", &SA5_access},
170 {0x21C8103C, "Smart Array", &SA5_access},
171 {0x21C9103C, "Smart Array", &SA5_access},
Stephen M. Cameron8e616a52014-02-18 13:58:02 -0600172 {0x00761590, "HP Storage P1224 Array Controller", &SA5_access},
173 {0x00871590, "HP Storage P1224e Array Controller", &SA5_access},
174 {0x007D1590, "HP Storage P1228 Array Controller", &SA5_access},
175 {0x00881590, "HP Storage P1228e Array Controller", &SA5_access},
176 {0x333f103c, "HP StorageWorks 1210m Array Controller", &SA5_access},
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800177 {0xFFFF103C, "Unknown Smart Array", &SA5_access},
178};
179
180static int number_of_controllers;
181
Stephen M. Cameron10f66012010-06-16 13:51:50 -0500182static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id);
183static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800184static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg);
185static void start_io(struct ctlr_info *h);
186
187#ifdef CONFIG_COMPAT
188static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg);
189#endif
190
191static void cmd_free(struct ctlr_info *h, struct CommandList *c);
192static void cmd_special_free(struct ctlr_info *h, struct CommandList *c);
193static struct CommandList *cmd_alloc(struct ctlr_info *h);
194static struct CommandList *cmd_special_alloc(struct ctlr_info *h);
Stephen M. Camerona2dac132013-02-20 11:24:41 -0600195static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -0600196 void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800197 int cmd_type);
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -0600198#define VPD_PAGE (1 << 8)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800199
Jeff Garzikf2812332010-11-16 02:10:29 -0500200static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Stephen M. Camerona08a8472010-02-04 08:43:16 -0600201static void hpsa_scan_start(struct Scsi_Host *);
202static int hpsa_scan_finished(struct Scsi_Host *sh,
203 unsigned long elapsed_time);
Stephen M. Cameron667e23d2010-02-25 14:02:51 -0600204static int hpsa_change_queue_depth(struct scsi_device *sdev,
205 int qdepth, int reason);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800206
207static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500208static int hpsa_eh_abort_handler(struct scsi_cmnd *scsicmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800209static int hpsa_slave_alloc(struct scsi_device *sdev);
210static void hpsa_slave_destroy(struct scsi_device *sdev);
211
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800212static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800213static int check_for_unit_attention(struct ctlr_info *h,
214 struct CommandList *c);
215static void check_ioctl_unit_attention(struct ctlr_info *h,
216 struct CommandList *c);
Don Brace303932f2010-02-04 08:42:40 -0600217/* performant mode helper functions */
218static void calc_bucket_map(int *bucket, int num_buckets,
Matt Gatese1f7de02014-02-18 13:55:17 -0600219 int nsgs, int min_blocks, int *bucket_map);
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800220static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
Matt Gates254f7962012-05-01 11:43:06 -0500221static inline u32 next_command(struct ctlr_info *h, u8 q);
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800222static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
223 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
224 u64 *cfg_offset);
225static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
226 unsigned long *memory_bar);
227static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
228static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
229 int wait_for_ready);
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500230static inline void finish_cmd(struct CommandList *c);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -0600231static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h);
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -0600232#define BOARD_NOT_READY 0
233#define BOARD_READY 1
Stephen M. Cameron23100dd2014-02-18 13:57:37 -0600234static void hpsa_drain_accel_commands(struct ctlr_info *h);
Stephen M. Cameron76438d02014-02-18 13:55:43 -0600235static void hpsa_flush_cache(struct ctlr_info *h);
Scott Teelc3497752014-02-18 13:56:34 -0600236static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
237 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
238 u8 *scsi3addr);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800239
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800240static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
241{
242 unsigned long *priv = shost_priv(sdev->host);
243 return (struct ctlr_info *) *priv;
244}
245
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600246static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
247{
248 unsigned long *priv = shost_priv(sh);
249 return (struct ctlr_info *) *priv;
250}
251
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800252static int check_for_unit_attention(struct ctlr_info *h,
253 struct CommandList *c)
254{
255 if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
256 return 0;
257
258 switch (c->err_info->SenseInfo[12]) {
259 case STATE_CHANGED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600260 dev_warn(&h->pdev->dev, HPSA "%d: a state change "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800261 "detected, command retried\n", h->ctlr);
262 break;
263 case LUN_FAILED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600264 dev_warn(&h->pdev->dev, HPSA "%d: LUN failure "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800265 "detected, action required\n", h->ctlr);
266 break;
267 case REPORT_LUNS_CHANGED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600268 dev_warn(&h->pdev->dev, HPSA "%d: report LUN data "
Mike Miller31468402010-02-25 14:03:12 -0600269 "changed, action required\n", h->ctlr);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800270 /*
Scott Teel4f4eb9f2012-01-19 14:01:25 -0600271 * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
272 * target (array) devices.
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800273 */
274 break;
275 case POWER_OR_RESET:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600276 dev_warn(&h->pdev->dev, HPSA "%d: a power on "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800277 "or device reset detected\n", h->ctlr);
278 break;
279 case UNIT_ATTENTION_CLEARED:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600280 dev_warn(&h->pdev->dev, HPSA "%d: unit attention "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800281 "cleared by another initiator\n", h->ctlr);
282 break;
283 default:
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600284 dev_warn(&h->pdev->dev, HPSA "%d: unknown "
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800285 "unit attention detected\n", h->ctlr);
286 break;
287 }
288 return 1;
289}
290
Matt Bondurant852af202012-05-01 11:42:35 -0500291static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
292{
293 if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
294 (c->err_info->ScsiStatus != SAM_STAT_BUSY &&
295 c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
296 return 0;
297 dev_warn(&h->pdev->dev, HPSA "device busy");
298 return 1;
299}
300
Scott Teelda0697b2014-02-18 13:57:00 -0600301static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
302 struct device_attribute *attr,
303 const char *buf, size_t count)
304{
305 int status, len;
306 struct ctlr_info *h;
307 struct Scsi_Host *shost = class_to_shost(dev);
308 char tmpbuf[10];
309
310 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
311 return -EACCES;
312 len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
313 strncpy(tmpbuf, buf, len);
314 tmpbuf[len] = '\0';
315 if (sscanf(tmpbuf, "%d", &status) != 1)
316 return -EINVAL;
317 h = shost_to_hba(shost);
318 h->acciopath_status = !!status;
319 dev_warn(&h->pdev->dev,
320 "hpsa: HP SSD Smart Path %s via sysfs update.\n",
321 h->acciopath_status ? "enabled" : "disabled");
322 return count;
323}
324
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600325static ssize_t host_store_raid_offload_debug(struct device *dev,
326 struct device_attribute *attr,
327 const char *buf, size_t count)
328{
329 int debug_level, len;
330 struct ctlr_info *h;
331 struct Scsi_Host *shost = class_to_shost(dev);
332 char tmpbuf[10];
333
334 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
335 return -EACCES;
336 len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
337 strncpy(tmpbuf, buf, len);
338 tmpbuf[len] = '\0';
339 if (sscanf(tmpbuf, "%d", &debug_level) != 1)
340 return -EINVAL;
341 if (debug_level < 0)
342 debug_level = 0;
343 h = shost_to_hba(shost);
344 h->raid_offload_debug = debug_level;
345 dev_warn(&h->pdev->dev, "hpsa: Set raid_offload_debug level = %d\n",
346 h->raid_offload_debug);
347 return count;
348}
349
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800350static ssize_t host_store_rescan(struct device *dev,
351 struct device_attribute *attr,
352 const char *buf, size_t count)
353{
354 struct ctlr_info *h;
355 struct Scsi_Host *shost = class_to_shost(dev);
Stephen M. Camerona23513e2010-02-04 08:43:11 -0600356 h = shost_to_hba(shost);
Mike Miller31468402010-02-25 14:03:12 -0600357 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800358 return count;
359}
360
Stephen M. Camerond28ce022010-05-27 15:14:34 -0500361static ssize_t host_show_firmware_revision(struct device *dev,
362 struct device_attribute *attr, char *buf)
363{
364 struct ctlr_info *h;
365 struct Scsi_Host *shost = class_to_shost(dev);
366 unsigned char *fwrev;
367
368 h = shost_to_hba(shost);
369 if (!h->hba_inquiry_data)
370 return 0;
371 fwrev = &h->hba_inquiry_data[32];
372 return snprintf(buf, 20, "%c%c%c%c\n",
373 fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
374}
375
Stephen M. Cameron94a13642011-01-06 14:48:39 -0600376static ssize_t host_show_commands_outstanding(struct device *dev,
377 struct device_attribute *attr, char *buf)
378{
379 struct Scsi_Host *shost = class_to_shost(dev);
380 struct ctlr_info *h = shost_to_hba(shost);
381
382 return snprintf(buf, 20, "%d\n", h->commands_outstanding);
383}
384
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600385static ssize_t host_show_transport_mode(struct device *dev,
386 struct device_attribute *attr, char *buf)
387{
388 struct ctlr_info *h;
389 struct Scsi_Host *shost = class_to_shost(dev);
390
391 h = shost_to_hba(shost);
392 return snprintf(buf, 20, "%s\n",
Stephen M. Cameron960a30e2011-02-15 15:33:03 -0600393 h->transMethod & CFGTBL_Trans_Performant ?
Stephen M. Cameron745a7a22011-02-15 15:32:58 -0600394 "performant" : "simple");
395}
396
Scott Teelda0697b2014-02-18 13:57:00 -0600397static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
398 struct device_attribute *attr, char *buf)
399{
400 struct ctlr_info *h;
401 struct Scsi_Host *shost = class_to_shost(dev);
402
403 h = shost_to_hba(shost);
404 return snprintf(buf, 30, "HP SSD Smart Path %s\n",
405 (h->acciopath_status == 1) ? "enabled" : "disabled");
406}
407
Stephen M. Cameron46380782011-05-03 15:00:01 -0500408/* List of controllers which cannot be hard reset on kexec with reset_devices */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600409static u32 unresettable_controller[] = {
410 0x324a103C, /* Smart Array P712m */
411 0x324b103C, /* SmartArray P711m */
412 0x3223103C, /* Smart Array P800 */
413 0x3234103C, /* Smart Array P400 */
414 0x3235103C, /* Smart Array P400i */
415 0x3211103C, /* Smart Array E200i */
416 0x3212103C, /* Smart Array E200 */
417 0x3213103C, /* Smart Array E200i */
418 0x3214103C, /* Smart Array E200i */
419 0x3215103C, /* Smart Array E200i */
420 0x3237103C, /* Smart Array E500 */
421 0x323D103C, /* Smart Array P700m */
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100422 0x40800E11, /* Smart Array 5i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600423 0x409C0E11, /* Smart Array 6400 */
424 0x409D0E11, /* Smart Array 6400 EM */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100425 0x40700E11, /* Smart Array 5300 */
426 0x40820E11, /* Smart Array 532 */
427 0x40830E11, /* Smart Array 5312 */
428 0x409A0E11, /* Smart Array 641 */
429 0x409B0E11, /* Smart Array 642 */
430 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600431};
432
Stephen M. Cameron46380782011-05-03 15:00:01 -0500433/* List of controllers which cannot even be soft reset */
434static u32 soft_unresettable_controller[] = {
Tomas Henzl7af0abb2011-11-28 15:39:55 +0100435 0x40800E11, /* Smart Array 5i */
Tomas Henzl5a4f9342012-02-14 18:07:59 +0100436 0x40700E11, /* Smart Array 5300 */
437 0x40820E11, /* Smart Array 532 */
438 0x40830E11, /* Smart Array 5312 */
439 0x409A0E11, /* Smart Array 641 */
440 0x409B0E11, /* Smart Array 642 */
441 0x40910E11, /* Smart Array 6i */
Stephen M. Cameron46380782011-05-03 15:00:01 -0500442 /* Exclude 640x boards. These are two pci devices in one slot
443 * which share a battery backed cache module. One controls the
444 * cache, the other accesses the cache through the one that controls
445 * it. If we reset the one controlling the cache, the other will
446 * likely not be happy. Just forbid resetting this conjoined mess.
447 * The 640x isn't really supported by hpsa anyway.
448 */
449 0x409C0E11, /* Smart Array 6400 */
450 0x409D0E11, /* Smart Array 6400 EM */
451};
452
453static int ctlr_is_hard_resettable(u32 board_id)
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600454{
455 int i;
456
457 for (i = 0; i < ARRAY_SIZE(unresettable_controller); i++)
Stephen M. Cameron46380782011-05-03 15:00:01 -0500458 if (unresettable_controller[i] == board_id)
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600459 return 0;
460 return 1;
461}
462
Stephen M. Cameron46380782011-05-03 15:00:01 -0500463static int ctlr_is_soft_resettable(u32 board_id)
464{
465 int i;
466
467 for (i = 0; i < ARRAY_SIZE(soft_unresettable_controller); i++)
468 if (soft_unresettable_controller[i] == board_id)
469 return 0;
470 return 1;
471}
472
473static int ctlr_is_resettable(u32 board_id)
474{
475 return ctlr_is_hard_resettable(board_id) ||
476 ctlr_is_soft_resettable(board_id);
477}
478
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600479static ssize_t host_show_resettable(struct device *dev,
480 struct device_attribute *attr, char *buf)
481{
482 struct ctlr_info *h;
483 struct Scsi_Host *shost = class_to_shost(dev);
484
485 h = shost_to_hba(shost);
Stephen M. Cameron46380782011-05-03 15:00:01 -0500486 return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600487}
488
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800489static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
490{
491 return (scsi3addr[3] & 0xC0) == 0x40;
492}
493
494static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
Mike Millerd82357e2012-05-01 11:43:32 -0500495 "1(ADM)", "UNKNOWN"
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800496};
Scott Teel6b80b182014-02-18 13:56:55 -0600497#define HPSA_RAID_0 0
498#define HPSA_RAID_4 1
499#define HPSA_RAID_1 2 /* also used for RAID 10 */
500#define HPSA_RAID_5 3 /* also used for RAID 50 */
501#define HPSA_RAID_51 4
502#define HPSA_RAID_6 5 /* also used for RAID 60 */
503#define HPSA_RAID_ADM 6 /* also used for RAID 1+0 ADM */
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800504#define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
505
506static ssize_t raid_level_show(struct device *dev,
507 struct device_attribute *attr, char *buf)
508{
509 ssize_t l = 0;
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600510 unsigned char rlevel;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800511 struct ctlr_info *h;
512 struct scsi_device *sdev;
513 struct hpsa_scsi_dev_t *hdev;
514 unsigned long flags;
515
516 sdev = to_scsi_device(dev);
517 h = sdev_to_hba(sdev);
518 spin_lock_irqsave(&h->lock, flags);
519 hdev = sdev->hostdata;
520 if (!hdev) {
521 spin_unlock_irqrestore(&h->lock, flags);
522 return -ENODEV;
523 }
524
525 /* Is this even a logical drive? */
526 if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
527 spin_unlock_irqrestore(&h->lock, flags);
528 l = snprintf(buf, PAGE_SIZE, "N/A\n");
529 return l;
530 }
531
532 rlevel = hdev->raid_level;
533 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron82a72c02010-02-04 08:41:38 -0600534 if (rlevel > RAID_UNKNOWN)
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800535 rlevel = RAID_UNKNOWN;
536 l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
537 return l;
538}
539
540static ssize_t lunid_show(struct device *dev,
541 struct device_attribute *attr, char *buf)
542{
543 struct ctlr_info *h;
544 struct scsi_device *sdev;
545 struct hpsa_scsi_dev_t *hdev;
546 unsigned long flags;
547 unsigned char lunid[8];
548
549 sdev = to_scsi_device(dev);
550 h = sdev_to_hba(sdev);
551 spin_lock_irqsave(&h->lock, flags);
552 hdev = sdev->hostdata;
553 if (!hdev) {
554 spin_unlock_irqrestore(&h->lock, flags);
555 return -ENODEV;
556 }
557 memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
558 spin_unlock_irqrestore(&h->lock, flags);
559 return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
560 lunid[0], lunid[1], lunid[2], lunid[3],
561 lunid[4], lunid[5], lunid[6], lunid[7]);
562}
563
564static ssize_t unique_id_show(struct device *dev,
565 struct device_attribute *attr, char *buf)
566{
567 struct ctlr_info *h;
568 struct scsi_device *sdev;
569 struct hpsa_scsi_dev_t *hdev;
570 unsigned long flags;
571 unsigned char sn[16];
572
573 sdev = to_scsi_device(dev);
574 h = sdev_to_hba(sdev);
575 spin_lock_irqsave(&h->lock, flags);
576 hdev = sdev->hostdata;
577 if (!hdev) {
578 spin_unlock_irqrestore(&h->lock, flags);
579 return -ENODEV;
580 }
581 memcpy(sn, hdev->device_id, sizeof(sn));
582 spin_unlock_irqrestore(&h->lock, flags);
583 return snprintf(buf, 16 * 2 + 2,
584 "%02X%02X%02X%02X%02X%02X%02X%02X"
585 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
586 sn[0], sn[1], sn[2], sn[3],
587 sn[4], sn[5], sn[6], sn[7],
588 sn[8], sn[9], sn[10], sn[11],
589 sn[12], sn[13], sn[14], sn[15]);
590}
591
Scott Teelc1988682014-02-18 13:55:54 -0600592static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
593 struct device_attribute *attr, char *buf)
594{
595 struct ctlr_info *h;
596 struct scsi_device *sdev;
597 struct hpsa_scsi_dev_t *hdev;
598 unsigned long flags;
599 int offload_enabled;
600
601 sdev = to_scsi_device(dev);
602 h = sdev_to_hba(sdev);
603 spin_lock_irqsave(&h->lock, flags);
604 hdev = sdev->hostdata;
605 if (!hdev) {
606 spin_unlock_irqrestore(&h->lock, flags);
607 return -ENODEV;
608 }
609 offload_enabled = hdev->offload_enabled;
610 spin_unlock_irqrestore(&h->lock, flags);
611 return snprintf(buf, 20, "%d\n", offload_enabled);
612}
613
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600614static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
615static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
616static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
617static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
Scott Teelc1988682014-02-18 13:55:54 -0600618static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
619 host_show_hp_ssd_smart_path_enabled, NULL);
Scott Teelda0697b2014-02-18 13:57:00 -0600620static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
621 host_show_hp_ssd_smart_path_status,
622 host_store_hp_ssd_smart_path_status);
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600623static DEVICE_ATTR(raid_offload_debug, S_IWUSR, NULL,
624 host_store_raid_offload_debug);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600625static DEVICE_ATTR(firmware_revision, S_IRUGO,
626 host_show_firmware_revision, NULL);
627static DEVICE_ATTR(commands_outstanding, S_IRUGO,
628 host_show_commands_outstanding, NULL);
629static DEVICE_ATTR(transport_mode, S_IRUGO,
630 host_show_transport_mode, NULL);
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600631static DEVICE_ATTR(resettable, S_IRUGO,
632 host_show_resettable, NULL);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600633
634static struct device_attribute *hpsa_sdev_attrs[] = {
635 &dev_attr_raid_level,
636 &dev_attr_lunid,
637 &dev_attr_unique_id,
Scott Teelc1988682014-02-18 13:55:54 -0600638 &dev_attr_hp_ssd_smart_path_enabled,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600639 NULL,
640};
641
642static struct device_attribute *hpsa_shost_attrs[] = {
643 &dev_attr_rescan,
644 &dev_attr_firmware_revision,
645 &dev_attr_commands_outstanding,
646 &dev_attr_transport_mode,
Stephen M. Cameron941b1cd2011-03-09 17:00:06 -0600647 &dev_attr_resettable,
Scott Teelda0697b2014-02-18 13:57:00 -0600648 &dev_attr_hp_ssd_smart_path_status,
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -0600649 &dev_attr_raid_offload_debug,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600650 NULL,
651};
652
653static struct scsi_host_template hpsa_driver_template = {
654 .module = THIS_MODULE,
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -0600655 .name = HPSA,
656 .proc_name = HPSA,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600657 .queuecommand = hpsa_scsi_queue_command,
658 .scan_start = hpsa_scan_start,
659 .scan_finished = hpsa_scan_finished,
660 .change_queue_depth = hpsa_change_queue_depth,
661 .this_id = -1,
662 .use_clustering = ENABLE_CLUSTERING,
Stephen M. Cameron75167d22012-05-01 11:42:51 -0500663 .eh_abort_handler = hpsa_eh_abort_handler,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600664 .eh_device_reset_handler = hpsa_eh_device_reset_handler,
665 .ioctl = hpsa_ioctl,
666 .slave_alloc = hpsa_slave_alloc,
667 .slave_destroy = hpsa_slave_destroy,
668#ifdef CONFIG_COMPAT
669 .compat_ioctl = hpsa_compat_ioctl,
670#endif
671 .sdev_attrs = hpsa_sdev_attrs,
672 .shost_attrs = hpsa_shost_attrs,
Stephen M. Cameronc0d6a4d2011-10-26 16:20:53 -0500673 .max_sectors = 8192,
Martin K. Petersen54b2b502013-10-23 06:25:40 -0400674 .no_write_same = 1,
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600675};
676
677
678/* Enqueuing and dequeuing functions for cmdlists. */
679static inline void addQ(struct list_head *list, struct CommandList *c)
680{
681 list_add_tail(&c->list, list);
682}
683
Matt Gates254f7962012-05-01 11:43:06 -0500684static inline u32 next_command(struct ctlr_info *h, u8 q)
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600685{
686 u32 a;
Matt Gates254f7962012-05-01 11:43:06 -0500687 struct reply_pool *rq = &h->reply_queue[q];
Matt Gatese16a33a2012-05-01 11:43:11 -0500688 unsigned long flags;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600689
Matt Gatese1f7de02014-02-18 13:55:17 -0600690 if (h->transMethod & CFGTBL_Trans_io_accel1)
691 return h->access.command_completed(h, q);
692
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600693 if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
Matt Gates254f7962012-05-01 11:43:06 -0500694 return h->access.command_completed(h, q);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600695
Matt Gates254f7962012-05-01 11:43:06 -0500696 if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
697 a = rq->head[rq->current_entry];
698 rq->current_entry++;
Matt Gatese16a33a2012-05-01 11:43:11 -0500699 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600700 h->commands_outstanding--;
Matt Gatese16a33a2012-05-01 11:43:11 -0500701 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600702 } else {
703 a = FIFO_EMPTY;
704 }
705 /* Check for wraparound */
Matt Gates254f7962012-05-01 11:43:06 -0500706 if (rq->current_entry == h->max_commands) {
707 rq->current_entry = 0;
708 rq->wraparound ^= 1;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600709 }
710 return a;
711}
712
Scott Teelc3497752014-02-18 13:56:34 -0600713/*
714 * There are some special bits in the bus address of the
715 * command that we have to set for the controller to know
716 * how to process the command:
717 *
718 * Normal performant mode:
719 * bit 0: 1 means performant mode, 0 means simple mode.
720 * bits 1-3 = block fetch table entry
721 * bits 4-6 = command type (== 0)
722 *
723 * ioaccel1 mode:
724 * bit 0 = "performant mode" bit.
725 * bits 1-3 = block fetch table entry
726 * bits 4-6 = command type (== 110)
727 * (command type is needed because ioaccel1 mode
728 * commands are submitted through the same register as normal
729 * mode commands, so this is how the controller knows whether
730 * the command is normal mode or ioaccel1 mode.)
731 *
732 * ioaccel2 mode:
733 * bit 0 = "performant mode" bit.
734 * bits 1-4 = block fetch table entry (note extra bit)
735 * bits 4-6 = not needed, because ioaccel2 mode has
736 * a separate special register for submitting commands.
737 */
738
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600739/* set_performant_mode: Modify the tag for cciss performant
740 * set bit 0 for pull model, bits 3-1 for block fetch
741 * register number
742 */
743static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
744{
Matt Gates254f7962012-05-01 11:43:06 -0500745 if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600746 c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
Hannes Reineckeeee0f032014-01-15 13:30:53 +0100747 if (likely(h->msix_vector > 0))
Matt Gates254f7962012-05-01 11:43:06 -0500748 c->Header.ReplyQueue =
John Kacur804a5cb2013-07-26 16:06:18 +0200749 raw_smp_processor_id() % h->nreply_queues;
Matt Gates254f7962012-05-01 11:43:06 -0500750 }
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600751}
752
Scott Teelc3497752014-02-18 13:56:34 -0600753static void set_ioaccel1_performant_mode(struct ctlr_info *h,
754 struct CommandList *c)
755{
756 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
757
758 /* Tell the controller to post the reply to the queue for this
759 * processor. This seems to give the best I/O throughput.
760 */
761 cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
762 /* Set the bits in the address sent down to include:
763 * - performant mode bit (bit 0)
764 * - pull count (bits 1-3)
765 * - command type (bits 4-6)
766 */
767 c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
768 IOACCEL1_BUSADDR_CMDTYPE;
769}
770
771static void set_ioaccel2_performant_mode(struct ctlr_info *h,
772 struct CommandList *c)
773{
774 struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
775
776 /* Tell the controller to post the reply to the queue for this
777 * processor. This seems to give the best I/O throughput.
778 */
779 cp->reply_queue = smp_processor_id() % h->nreply_queues;
780 /* Set the bits in the address sent down to include:
781 * - performant mode bit not used in ioaccel mode 2
782 * - pull count (bits 0-3)
783 * - command type isn't needed for ioaccel2
784 */
785 c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
786}
787
Stephen M. Camerone85c5972012-05-01 11:43:42 -0500788static int is_firmware_flash_cmd(u8 *cdb)
789{
790 return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
791}
792
793/*
794 * During firmware flash, the heartbeat register may not update as frequently
795 * as it should. So we dial down lockup detection during firmware flash. and
796 * dial it back up when firmware flash completes.
797 */
798#define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
799#define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
800static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
801 struct CommandList *c)
802{
803 if (!is_firmware_flash_cmd(c->Request.CDB))
804 return;
805 atomic_inc(&h->firmware_flash_in_progress);
806 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
807}
808
809static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
810 struct CommandList *c)
811{
812 if (is_firmware_flash_cmd(c->Request.CDB) &&
813 atomic_dec_and_test(&h->firmware_flash_in_progress))
814 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
815}
816
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600817static void enqueue_cmd_and_start_io(struct ctlr_info *h,
818 struct CommandList *c)
819{
820 unsigned long flags;
821
Scott Teelc3497752014-02-18 13:56:34 -0600822 switch (c->cmd_type) {
823 case CMD_IOACCEL1:
824 set_ioaccel1_performant_mode(h, c);
825 break;
826 case CMD_IOACCEL2:
827 set_ioaccel2_performant_mode(h, c);
828 break;
829 default:
830 set_performant_mode(h, c);
831 }
Stephen M. Camerone85c5972012-05-01 11:43:42 -0500832 dial_down_lockup_detection_during_fw_flash(h, c);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600833 spin_lock_irqsave(&h->lock, flags);
834 addQ(&h->reqQ, c);
835 h->Qdepth++;
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600836 spin_unlock_irqrestore(&h->lock, flags);
Matt Gatese16a33a2012-05-01 11:43:11 -0500837 start_io(h);
Stephen M. Cameron3f5eac32011-03-09 17:00:01 -0600838}
839
840static inline void removeQ(struct CommandList *c)
841{
842 if (WARN_ON(list_empty(&c->list)))
843 return;
844 list_del_init(&c->list);
845}
846
847static inline int is_hba_lunid(unsigned char scsi3addr[])
848{
849 return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
850}
851
852static inline int is_scsi_rev_5(struct ctlr_info *h)
853{
854 if (!h->hba_inquiry_data)
855 return 0;
856 if ((h->hba_inquiry_data[2] & 0x07) == 5)
857 return 1;
858 return 0;
859}
860
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800861static int hpsa_find_target_lun(struct ctlr_info *h,
862 unsigned char scsi3addr[], int bus, int *target, int *lun)
863{
864 /* finds an unused bus, target, lun for a new physical device
865 * assumes h->devlock is held
866 */
867 int i, found = 0;
Scott Teelcfe5bad2011-10-26 16:21:07 -0500868 DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800869
Akinobu Mita263d9402012-01-21 00:15:27 +0900870 bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800871
872 for (i = 0; i < h->ndevices; i++) {
873 if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
Akinobu Mita263d9402012-01-21 00:15:27 +0900874 __set_bit(h->dev[i]->target, lun_taken);
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800875 }
876
Akinobu Mita263d9402012-01-21 00:15:27 +0900877 i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
878 if (i < HPSA_MAX_DEVICES) {
879 /* *bus = 1; */
880 *target = i;
881 *lun = 0;
882 found = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800883 }
884 return !found;
885}
886
887/* Add an entry into h->dev[] array. */
888static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
889 struct hpsa_scsi_dev_t *device,
890 struct hpsa_scsi_dev_t *added[], int *nadded)
891{
892 /* assumes h->devlock is held */
893 int n = h->ndevices;
894 int i;
895 unsigned char addr1[8], addr2[8];
896 struct hpsa_scsi_dev_t *sd;
897
Scott Teelcfe5bad2011-10-26 16:21:07 -0500898 if (n >= HPSA_MAX_DEVICES) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -0800899 dev_err(&h->pdev->dev, "too many devices, some will be "
900 "inaccessible.\n");
901 return -1;
902 }
903
904 /* physical devices do not have lun or target assigned until now. */
905 if (device->lun != -1)
906 /* Logical device, lun is already assigned. */
907 goto lun_assigned;
908
909 /* If this device a non-zero lun of a multi-lun device
910 * byte 4 of the 8-byte LUN addr will contain the logical
911 * unit no, zero otherise.
912 */
913 if (device->scsi3addr[4] == 0) {
914 /* This is not a non-zero lun of a multi-lun device */
915 if (hpsa_find_target_lun(h, device->scsi3addr,
916 device->bus, &device->target, &device->lun) != 0)
917 return -1;
918 goto lun_assigned;
919 }
920
921 /* This is a non-zero lun of a multi-lun device.
922 * Search through our list and find the device which
923 * has the same 8 byte LUN address, excepting byte 4.
924 * Assign the same bus and target for this new LUN.
925 * Use the logical unit number from the firmware.
926 */
927 memcpy(addr1, device->scsi3addr, 8);
928 addr1[4] = 0;
929 for (i = 0; i < n; i++) {
930 sd = h->dev[i];
931 memcpy(addr2, sd->scsi3addr, 8);
932 addr2[4] = 0;
933 /* differ only in byte 4? */
934 if (memcmp(addr1, addr2, 8) == 0) {
935 device->bus = sd->bus;
936 device->target = sd->target;
937 device->lun = device->scsi3addr[4];
938 break;
939 }
940 }
941 if (device->lun == -1) {
942 dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
943 " suspect firmware bug or unsupported hardware "
944 "configuration.\n");
945 return -1;
946 }
947
948lun_assigned:
949
950 h->dev[n] = device;
951 h->ndevices++;
952 added[*nadded] = device;
953 (*nadded)++;
954
955 /* initially, (before registering with scsi layer) we don't
956 * know our hostno and we don't want to print anything first
957 * time anyway (the scsi layer's inquiries will show that info)
958 */
959 /* if (hostno != -1) */
960 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
961 scsi_device_type(device->devtype), hostno,
962 device->bus, device->target, device->lun);
963 return 0;
964}
965
Scott Teelbd9244f2012-01-19 14:01:30 -0600966/* Update an entry in h->dev[] array. */
967static void hpsa_scsi_update_entry(struct ctlr_info *h, int hostno,
968 int entry, struct hpsa_scsi_dev_t *new_entry)
969{
970 /* assumes h->devlock is held */
971 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
972
973 /* Raid level changed. */
974 h->dev[entry]->raid_level = new_entry->raid_level;
Stephen M. Cameron250fb122014-02-18 13:55:38 -0600975
976 /* Raid offload parameters changed. */
977 h->dev[entry]->offload_config = new_entry->offload_config;
978 h->dev[entry]->offload_enabled = new_entry->offload_enabled;
Stephen M. Cameron9fb0de22014-02-18 13:56:50 -0600979 h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
980 h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
981 h->dev[entry]->raid_map = new_entry->raid_map;
Stephen M. Cameron250fb122014-02-18 13:55:38 -0600982
Scott Teelbd9244f2012-01-19 14:01:30 -0600983 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d updated.\n",
984 scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
985 new_entry->target, new_entry->lun);
986}
987
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -0600988/* Replace an entry from h->dev[] array. */
989static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
990 int entry, struct hpsa_scsi_dev_t *new_entry,
991 struct hpsa_scsi_dev_t *added[], int *nadded,
992 struct hpsa_scsi_dev_t *removed[], int *nremoved)
993{
994 /* assumes h->devlock is held */
Scott Teelcfe5bad2011-10-26 16:21:07 -0500995 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -0600996 removed[*nremoved] = h->dev[entry];
997 (*nremoved)++;
Stephen M. Cameron01350d02011-08-09 08:18:01 -0500998
999 /*
1000 * New physical devices won't have target/lun assigned yet
1001 * so we need to preserve the values in the slot we are replacing.
1002 */
1003 if (new_entry->target == -1) {
1004 new_entry->target = h->dev[entry]->target;
1005 new_entry->lun = h->dev[entry]->lun;
1006 }
1007
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001008 h->dev[entry] = new_entry;
1009 added[*nadded] = new_entry;
1010 (*nadded)++;
1011 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n",
1012 scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
1013 new_entry->target, new_entry->lun);
1014}
1015
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001016/* Remove an entry from h->dev[] array. */
1017static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
1018 struct hpsa_scsi_dev_t *removed[], int *nremoved)
1019{
1020 /* assumes h->devlock is held */
1021 int i;
1022 struct hpsa_scsi_dev_t *sd;
1023
Scott Teelcfe5bad2011-10-26 16:21:07 -05001024 BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001025
1026 sd = h->dev[entry];
1027 removed[*nremoved] = h->dev[entry];
1028 (*nremoved)++;
1029
1030 for (i = entry; i < h->ndevices-1; i++)
1031 h->dev[i] = h->dev[i+1];
1032 h->ndevices--;
1033 dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
1034 scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
1035 sd->lun);
1036}
1037
1038#define SCSI3ADDR_EQ(a, b) ( \
1039 (a)[7] == (b)[7] && \
1040 (a)[6] == (b)[6] && \
1041 (a)[5] == (b)[5] && \
1042 (a)[4] == (b)[4] && \
1043 (a)[3] == (b)[3] && \
1044 (a)[2] == (b)[2] && \
1045 (a)[1] == (b)[1] && \
1046 (a)[0] == (b)[0])
1047
1048static void fixup_botched_add(struct ctlr_info *h,
1049 struct hpsa_scsi_dev_t *added)
1050{
1051 /* called when scsi_add_device fails in order to re-adjust
1052 * h->dev[] to match the mid layer's view.
1053 */
1054 unsigned long flags;
1055 int i, j;
1056
1057 spin_lock_irqsave(&h->lock, flags);
1058 for (i = 0; i < h->ndevices; i++) {
1059 if (h->dev[i] == added) {
1060 for (j = i; j < h->ndevices-1; j++)
1061 h->dev[j] = h->dev[j+1];
1062 h->ndevices--;
1063 break;
1064 }
1065 }
1066 spin_unlock_irqrestore(&h->lock, flags);
1067 kfree(added);
1068}
1069
1070static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
1071 struct hpsa_scsi_dev_t *dev2)
1072{
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001073 /* we compare everything except lun and target as these
1074 * are not yet assigned. Compare parts likely
1075 * to differ first
1076 */
1077 if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
1078 sizeof(dev1->scsi3addr)) != 0)
1079 return 0;
1080 if (memcmp(dev1->device_id, dev2->device_id,
1081 sizeof(dev1->device_id)) != 0)
1082 return 0;
1083 if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
1084 return 0;
1085 if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
1086 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001087 if (dev1->devtype != dev2->devtype)
1088 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001089 if (dev1->bus != dev2->bus)
1090 return 0;
1091 return 1;
1092}
1093
Scott Teelbd9244f2012-01-19 14:01:30 -06001094static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
1095 struct hpsa_scsi_dev_t *dev2)
1096{
1097 /* Device attributes that can change, but don't mean
1098 * that the device is a different device, nor that the OS
1099 * needs to be told anything about the change.
1100 */
1101 if (dev1->raid_level != dev2->raid_level)
1102 return 1;
Stephen M. Cameron250fb122014-02-18 13:55:38 -06001103 if (dev1->offload_config != dev2->offload_config)
1104 return 1;
1105 if (dev1->offload_enabled != dev2->offload_enabled)
1106 return 1;
Scott Teelbd9244f2012-01-19 14:01:30 -06001107 return 0;
1108}
1109
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001110/* Find needle in haystack. If exact match found, return DEVICE_SAME,
1111 * and return needle location in *index. If scsi3addr matches, but not
1112 * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
Scott Teelbd9244f2012-01-19 14:01:30 -06001113 * location in *index.
1114 * In the case of a minor device attribute change, such as RAID level, just
1115 * return DEVICE_UPDATED, along with the updated device's location in index.
1116 * If needle not found, return DEVICE_NOT_FOUND.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001117 */
1118static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
1119 struct hpsa_scsi_dev_t *haystack[], int haystack_size,
1120 int *index)
1121{
1122 int i;
1123#define DEVICE_NOT_FOUND 0
1124#define DEVICE_CHANGED 1
1125#define DEVICE_SAME 2
Scott Teelbd9244f2012-01-19 14:01:30 -06001126#define DEVICE_UPDATED 3
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001127 for (i = 0; i < haystack_size; i++) {
Stephen M. Cameron23231042010-02-04 08:43:36 -06001128 if (haystack[i] == NULL) /* previously removed. */
1129 continue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001130 if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
1131 *index = i;
Scott Teelbd9244f2012-01-19 14:01:30 -06001132 if (device_is_the_same(needle, haystack[i])) {
1133 if (device_updated(needle, haystack[i]))
1134 return DEVICE_UPDATED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001135 return DEVICE_SAME;
Scott Teelbd9244f2012-01-19 14:01:30 -06001136 } else {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001137 return DEVICE_CHANGED;
Scott Teelbd9244f2012-01-19 14:01:30 -06001138 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001139 }
1140 }
1141 *index = -1;
1142 return DEVICE_NOT_FOUND;
1143}
1144
Stephen M. Cameron4967bd32010-02-04 08:41:49 -06001145static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001146 struct hpsa_scsi_dev_t *sd[], int nsds)
1147{
1148 /* sd contains scsi3 addresses and devtypes, and inquiry
1149 * data. This function takes what's in sd to be the current
1150 * reality and updates h->dev[] to reflect that reality.
1151 */
1152 int i, entry, device_change, changes = 0;
1153 struct hpsa_scsi_dev_t *csd;
1154 unsigned long flags;
1155 struct hpsa_scsi_dev_t **added, **removed;
1156 int nadded, nremoved;
1157 struct Scsi_Host *sh = NULL;
1158
Scott Teelcfe5bad2011-10-26 16:21:07 -05001159 added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
1160 removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001161
1162 if (!added || !removed) {
1163 dev_warn(&h->pdev->dev, "out of memory in "
1164 "adjust_hpsa_scsi_table\n");
1165 goto free_and_out;
1166 }
1167
1168 spin_lock_irqsave(&h->devlock, flags);
1169
1170 /* find any devices in h->dev[] that are not in
1171 * sd[] and remove them from h->dev[], and for any
1172 * devices which have changed, remove the old device
1173 * info and add the new device info.
Scott Teelbd9244f2012-01-19 14:01:30 -06001174 * If minor device attributes change, just update
1175 * the existing device structure.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001176 */
1177 i = 0;
1178 nremoved = 0;
1179 nadded = 0;
1180 while (i < h->ndevices) {
1181 csd = h->dev[i];
1182 device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
1183 if (device_change == DEVICE_NOT_FOUND) {
1184 changes++;
1185 hpsa_scsi_remove_entry(h, hostno, i,
1186 removed, &nremoved);
1187 continue; /* remove ^^^, hence i not incremented */
1188 } else if (device_change == DEVICE_CHANGED) {
1189 changes++;
Stephen M. Cameron2a8ccf32010-02-04 08:43:41 -06001190 hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
1191 added, &nadded, removed, &nremoved);
Stephen M. Cameronc7f172d2010-02-04 08:43:31 -06001192 /* Set it to NULL to prevent it from being freed
1193 * at the bottom of hpsa_update_scsi_devices()
1194 */
1195 sd[entry] = NULL;
Scott Teelbd9244f2012-01-19 14:01:30 -06001196 } else if (device_change == DEVICE_UPDATED) {
1197 hpsa_scsi_update_entry(h, hostno, i, sd[entry]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001198 }
1199 i++;
1200 }
1201
1202 /* Now, make sure every device listed in sd[] is also
1203 * listed in h->dev[], adding them if they aren't found
1204 */
1205
1206 for (i = 0; i < nsds; i++) {
1207 if (!sd[i]) /* if already added above. */
1208 continue;
1209 device_change = hpsa_scsi_find_entry(sd[i], h->dev,
1210 h->ndevices, &entry);
1211 if (device_change == DEVICE_NOT_FOUND) {
1212 changes++;
1213 if (hpsa_scsi_add_entry(h, hostno, sd[i],
1214 added, &nadded) != 0)
1215 break;
1216 sd[i] = NULL; /* prevent from being freed later. */
1217 } else if (device_change == DEVICE_CHANGED) {
1218 /* should never happen... */
1219 changes++;
1220 dev_warn(&h->pdev->dev,
1221 "device unexpectedly changed.\n");
1222 /* but if it does happen, we just ignore that device */
1223 }
1224 }
1225 spin_unlock_irqrestore(&h->devlock, flags);
1226
1227 /* Don't notify scsi mid layer of any changes the first time through
1228 * (or if there are no changes) scsi_scan_host will do it later the
1229 * first time through.
1230 */
1231 if (hostno == -1 || !changes)
1232 goto free_and_out;
1233
1234 sh = h->scsi_host;
1235 /* Notify scsi mid layer of any removed devices */
1236 for (i = 0; i < nremoved; i++) {
1237 struct scsi_device *sdev =
1238 scsi_device_lookup(sh, removed[i]->bus,
1239 removed[i]->target, removed[i]->lun);
1240 if (sdev != NULL) {
1241 scsi_remove_device(sdev);
1242 scsi_device_put(sdev);
1243 } else {
1244 /* We don't expect to get here.
1245 * future cmds to this device will get selection
1246 * timeout as if the device was gone.
1247 */
1248 dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
1249 " for removal.", hostno, removed[i]->bus,
1250 removed[i]->target, removed[i]->lun);
1251 }
1252 kfree(removed[i]);
1253 removed[i] = NULL;
1254 }
1255
1256 /* Notify scsi mid layer of any added devices */
1257 for (i = 0; i < nadded; i++) {
1258 if (scsi_add_device(sh, added[i]->bus,
1259 added[i]->target, added[i]->lun) == 0)
1260 continue;
1261 dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
1262 "device not added.\n", hostno, added[i]->bus,
1263 added[i]->target, added[i]->lun);
1264 /* now we have to remove it from h->dev,
1265 * since it didn't get added to scsi mid layer
1266 */
1267 fixup_botched_add(h, added[i]);
1268 }
1269
1270free_and_out:
1271 kfree(added);
1272 kfree(removed);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001273}
1274
1275/*
Joe Perches9e03aa22013-09-03 13:45:58 -07001276 * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001277 * Assume's h->devlock is held.
1278 */
1279static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
1280 int bus, int target, int lun)
1281{
1282 int i;
1283 struct hpsa_scsi_dev_t *sd;
1284
1285 for (i = 0; i < h->ndevices; i++) {
1286 sd = h->dev[i];
1287 if (sd->bus == bus && sd->target == target && sd->lun == lun)
1288 return sd;
1289 }
1290 return NULL;
1291}
1292
1293/* link sdev->hostdata to our per-device structure. */
1294static int hpsa_slave_alloc(struct scsi_device *sdev)
1295{
1296 struct hpsa_scsi_dev_t *sd;
1297 unsigned long flags;
1298 struct ctlr_info *h;
1299
1300 h = sdev_to_hba(sdev);
1301 spin_lock_irqsave(&h->devlock, flags);
1302 sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
1303 sdev_id(sdev), sdev->lun);
1304 if (sd != NULL)
1305 sdev->hostdata = sd;
1306 spin_unlock_irqrestore(&h->devlock, flags);
1307 return 0;
1308}
1309
1310static void hpsa_slave_destroy(struct scsi_device *sdev)
1311{
Stephen M. Cameronbcc44252010-02-04 08:41:54 -06001312 /* nothing to do. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001313}
1314
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001315static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
1316{
1317 int i;
1318
1319 if (!h->cmd_sg_list)
1320 return;
1321 for (i = 0; i < h->nr_cmds; i++) {
1322 kfree(h->cmd_sg_list[i]);
1323 h->cmd_sg_list[i] = NULL;
1324 }
1325 kfree(h->cmd_sg_list);
1326 h->cmd_sg_list = NULL;
1327}
1328
1329static int hpsa_allocate_sg_chain_blocks(struct ctlr_info *h)
1330{
1331 int i;
1332
1333 if (h->chainsize <= 0)
1334 return 0;
1335
1336 h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
1337 GFP_KERNEL);
1338 if (!h->cmd_sg_list)
1339 return -ENOMEM;
1340 for (i = 0; i < h->nr_cmds; i++) {
1341 h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
1342 h->chainsize, GFP_KERNEL);
1343 if (!h->cmd_sg_list[i])
1344 goto clean;
1345 }
1346 return 0;
1347
1348clean:
1349 hpsa_free_sg_chain_blocks(h);
1350 return -ENOMEM;
1351}
1352
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001353static int hpsa_map_sg_chain_block(struct ctlr_info *h,
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001354 struct CommandList *c)
1355{
1356 struct SGDescriptor *chain_sg, *chain_block;
1357 u64 temp64;
1358
1359 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
1360 chain_block = h->cmd_sg_list[c->cmdindex];
1361 chain_sg->Ext = HPSA_SG_CHAIN;
1362 chain_sg->Len = sizeof(*chain_sg) *
1363 (c->Header.SGTotal - h->max_cmd_sg_entries);
1364 temp64 = pci_map_single(h->pdev, chain_block, chain_sg->Len,
1365 PCI_DMA_TODEVICE);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001366 if (dma_mapping_error(&h->pdev->dev, temp64)) {
1367 /* prevent subsequent unmapping */
1368 chain_sg->Addr.lower = 0;
1369 chain_sg->Addr.upper = 0;
1370 return -1;
1371 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001372 chain_sg->Addr.lower = (u32) (temp64 & 0x0FFFFFFFFULL);
1373 chain_sg->Addr.upper = (u32) ((temp64 >> 32) & 0x0FFFFFFFFULL);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06001374 return 0;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001375}
1376
1377static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
1378 struct CommandList *c)
1379{
1380 struct SGDescriptor *chain_sg;
1381 union u64bit temp64;
1382
1383 if (c->Header.SGTotal <= h->max_cmd_sg_entries)
1384 return;
1385
1386 chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
1387 temp64.val32.lower = chain_sg->Addr.lower;
1388 temp64.val32.upper = chain_sg->Addr.upper;
1389 pci_unmap_single(h->pdev, temp64.val, chain_sg->Len, PCI_DMA_TODEVICE);
1390}
1391
Scott Teela09c1442014-02-18 13:57:21 -06001392
1393/* Decode the various types of errors on ioaccel2 path.
1394 * Return 1 for any error that should generate a RAID path retry.
1395 * Return 0 for errors that don't require a RAID path retry.
1396 */
1397static int handle_ioaccel_mode2_error(struct ctlr_info *h,
Scott Teelc3497752014-02-18 13:56:34 -06001398 struct CommandList *c,
1399 struct scsi_cmnd *cmd,
1400 struct io_accel2_cmd *c2)
1401{
1402 int data_len;
Scott Teela09c1442014-02-18 13:57:21 -06001403 int retry = 0;
Scott Teelc3497752014-02-18 13:56:34 -06001404
1405 switch (c2->error_data.serv_response) {
1406 case IOACCEL2_SERV_RESPONSE_COMPLETE:
1407 switch (c2->error_data.status) {
1408 case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
1409 break;
1410 case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
1411 dev_warn(&h->pdev->dev,
1412 "%s: task complete with check condition.\n",
1413 "HP SSD Smart Path");
1414 if (c2->error_data.data_present !=
1415 IOACCEL2_SENSE_DATA_PRESENT)
1416 break;
1417 /* copy the sense data */
1418 data_len = c2->error_data.sense_data_len;
1419 if (data_len > SCSI_SENSE_BUFFERSIZE)
1420 data_len = SCSI_SENSE_BUFFERSIZE;
1421 if (data_len > sizeof(c2->error_data.sense_data_buff))
1422 data_len =
1423 sizeof(c2->error_data.sense_data_buff);
1424 memcpy(cmd->sense_buffer,
1425 c2->error_data.sense_data_buff, data_len);
1426 cmd->result |= SAM_STAT_CHECK_CONDITION;
Scott Teela09c1442014-02-18 13:57:21 -06001427 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001428 break;
1429 case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
1430 dev_warn(&h->pdev->dev,
1431 "%s: task complete with BUSY status.\n",
1432 "HP SSD Smart Path");
Scott Teela09c1442014-02-18 13:57:21 -06001433 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001434 break;
1435 case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
1436 dev_warn(&h->pdev->dev,
1437 "%s: task complete with reservation conflict.\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 case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
1442 /* Make scsi midlayer do unlimited retries */
1443 cmd->result = DID_IMM_RETRY << 16;
1444 break;
1445 case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
1446 dev_warn(&h->pdev->dev,
1447 "%s: task complete with aborted status.\n",
1448 "HP SSD Smart Path");
Scott Teela09c1442014-02-18 13:57:21 -06001449 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001450 break;
1451 default:
1452 dev_warn(&h->pdev->dev,
1453 "%s: task complete with unrecognized status: 0x%02x\n",
1454 "HP SSD Smart Path", c2->error_data.status);
Scott Teela09c1442014-02-18 13:57:21 -06001455 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001456 break;
1457 }
1458 break;
1459 case IOACCEL2_SERV_RESPONSE_FAILURE:
1460 /* don't expect to get here. */
1461 dev_warn(&h->pdev->dev,
1462 "unexpected delivery or target failure, status = 0x%02x\n",
1463 c2->error_data.status);
Scott Teela09c1442014-02-18 13:57:21 -06001464 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001465 break;
1466 case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
1467 break;
1468 case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
1469 break;
1470 case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
1471 dev_warn(&h->pdev->dev, "task management function rejected.\n");
Scott Teela09c1442014-02-18 13:57:21 -06001472 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001473 break;
1474 case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
1475 dev_warn(&h->pdev->dev, "task management function invalid LUN\n");
1476 break;
1477 default:
1478 dev_warn(&h->pdev->dev,
1479 "%s: Unrecognized server response: 0x%02x\n",
Scott Teela09c1442014-02-18 13:57:21 -06001480 "HP SSD Smart Path",
1481 c2->error_data.serv_response);
1482 retry = 1;
Scott Teelc3497752014-02-18 13:56:34 -06001483 break;
1484 }
Scott Teela09c1442014-02-18 13:57:21 -06001485
1486 return retry; /* retry on raid path? */
Scott Teelc3497752014-02-18 13:56:34 -06001487}
1488
1489static void process_ioaccel2_completion(struct ctlr_info *h,
1490 struct CommandList *c, struct scsi_cmnd *cmd,
1491 struct hpsa_scsi_dev_t *dev)
1492{
1493 struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
Scott Teela09c1442014-02-18 13:57:21 -06001494 int raid_retry = 0;
Scott Teelc3497752014-02-18 13:56:34 -06001495
1496 /* check for good status */
1497 if (likely(c2->error_data.serv_response == 0 &&
1498 c2->error_data.status == 0)) {
1499 cmd_free(h, c);
1500 cmd->scsi_done(cmd);
1501 return;
1502 }
1503
1504 /* Any RAID offload error results in retry which will use
1505 * the normal I/O path so the controller can handle whatever's
1506 * wrong.
1507 */
1508 if (is_logical_dev_addr_mode(dev->scsi3addr) &&
1509 c2->error_data.serv_response ==
1510 IOACCEL2_SERV_RESPONSE_FAILURE) {
Scott Teela09c1442014-02-18 13:57:21 -06001511 if (c2->error_data.status ==
1512 IOACCEL2_STATUS_SR_IOACCEL_DISABLED)
Scott Teelc3497752014-02-18 13:56:34 -06001513 dev_warn(&h->pdev->dev,
Scott Teela09c1442014-02-18 13:57:21 -06001514 "%s: Path is unavailable, retrying on standard path.\n",
1515 "HP SSD Smart Path");
1516 else
1517 dev_warn(&h->pdev->dev,
1518 "%s: Error 0x%02x, retrying on standard path.\n",
Scott Teelc3497752014-02-18 13:56:34 -06001519 "HP SSD Smart Path", c2->error_data.status);
Scott Teela09c1442014-02-18 13:57:21 -06001520
Scott Teelc3497752014-02-18 13:56:34 -06001521 dev->offload_enabled = 0;
Scott Teele863d682014-02-18 13:57:05 -06001522 h->drv_req_rescan = 1; /* schedule controller for a rescan */
Scott Teelc3497752014-02-18 13:56:34 -06001523 cmd->result = DID_SOFT_ERROR << 16;
1524 cmd_free(h, c);
1525 cmd->scsi_done(cmd);
1526 return;
1527 }
Scott Teela09c1442014-02-18 13:57:21 -06001528 raid_retry = handle_ioaccel_mode2_error(h, c, cmd, c2);
1529 /* If error found, disable Smart Path, schedule a rescan,
1530 * and force a retry on the standard path.
1531 */
1532 if (raid_retry) {
1533 dev_warn(&h->pdev->dev, "%s: Retrying on standard path.\n",
1534 "HP SSD Smart Path");
1535 dev->offload_enabled = 0; /* Disable Smart Path */
1536 h->drv_req_rescan = 1; /* schedule controller rescan */
1537 cmd->result = DID_SOFT_ERROR << 16;
1538 }
Scott Teelc3497752014-02-18 13:56:34 -06001539 cmd_free(h, c);
1540 cmd->scsi_done(cmd);
1541}
1542
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05001543static void complete_scsi_command(struct CommandList *cp)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001544{
1545 struct scsi_cmnd *cmd;
1546 struct ctlr_info *h;
1547 struct ErrorInfo *ei;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001548 struct hpsa_scsi_dev_t *dev;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001549
1550 unsigned char sense_key;
1551 unsigned char asc; /* additional sense code */
1552 unsigned char ascq; /* additional sense code qualifier */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05001553 unsigned long sense_data_size;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001554
1555 ei = cp->err_info;
1556 cmd = (struct scsi_cmnd *) cp->scsi_cmd;
1557 h = cp->h;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001558 dev = cmd->device->hostdata;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001559
1560 scsi_dma_unmap(cmd); /* undo the DMA mappings */
Matt Gatese1f7de02014-02-18 13:55:17 -06001561 if ((cp->cmd_type == CMD_SCSI) &&
1562 (cp->Header.SGTotal > h->max_cmd_sg_entries))
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06001563 hpsa_unmap_sg_chain_block(h, cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001564
1565 cmd->result = (DID_OK << 16); /* host byte */
1566 cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
Scott Teelc3497752014-02-18 13:56:34 -06001567
1568 if (cp->cmd_type == CMD_IOACCEL2)
1569 return process_ioaccel2_completion(h, cp, cmd, dev);
1570
Stephen M. Cameron55126722010-02-25 14:03:01 -06001571 cmd->result |= ei->ScsiStatus;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001572
1573 /* copy the sense data whether we need to or not. */
Stephen M. Camerondb111e12011-06-03 09:57:34 -05001574 if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
1575 sense_data_size = SCSI_SENSE_BUFFERSIZE;
1576 else
1577 sense_data_size = sizeof(ei->SenseInfo);
1578 if (ei->SenseLen < sense_data_size)
1579 sense_data_size = ei->SenseLen;
1580
1581 memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001582 scsi_set_resid(cmd, ei->ResidualCnt);
1583
1584 if (ei->CommandStatus == 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001585 cmd_free(h, cp);
Tomas Henzl2cc5bfa2013-08-01 15:14:00 +02001586 cmd->scsi_done(cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001587 return;
1588 }
1589
Matt Gatese1f7de02014-02-18 13:55:17 -06001590 /* For I/O accelerator commands, copy over some fields to the normal
1591 * CISS header used below for error handling.
1592 */
1593 if (cp->cmd_type == CMD_IOACCEL1) {
1594 struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
1595 cp->Header.SGList = cp->Header.SGTotal = scsi_sg_count(cmd);
1596 cp->Request.CDBLen = c->io_flags & IOACCEL1_IOFLAGS_CDBLEN_MASK;
1597 cp->Header.Tag.lower = c->Tag.lower;
1598 cp->Header.Tag.upper = c->Tag.upper;
1599 memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
1600 memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001601
1602 /* Any RAID offload error results in retry which will use
1603 * the normal I/O path so the controller can handle whatever's
1604 * wrong.
1605 */
1606 if (is_logical_dev_addr_mode(dev->scsi3addr)) {
1607 if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
1608 dev->offload_enabled = 0;
1609 cmd->result = DID_SOFT_ERROR << 16;
1610 cmd_free(h, cp);
1611 cmd->scsi_done(cmd);
1612 return;
1613 }
Matt Gatese1f7de02014-02-18 13:55:17 -06001614 }
1615
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001616 /* an error has occurred */
1617 switch (ei->CommandStatus) {
1618
1619 case CMD_TARGET_STATUS:
1620 if (ei->ScsiStatus) {
1621 /* Get sense key */
1622 sense_key = 0xf & ei->SenseInfo[2];
1623 /* Get additional sense code */
1624 asc = ei->SenseInfo[12];
1625 /* Get addition sense code qualifier */
1626 ascq = ei->SenseInfo[13];
1627 }
1628
1629 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
Matt Gates3ce438d2013-12-04 17:10:36 -06001630 if (check_for_unit_attention(h, cp))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001631 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001632 if (sense_key == ILLEGAL_REQUEST) {
1633 /*
1634 * SCSI REPORT_LUNS is commonly unsupported on
1635 * Smart Array. Suppress noisy complaint.
1636 */
1637 if (cp->Request.CDB[0] == REPORT_LUNS)
1638 break;
1639
1640 /* If ASC/ASCQ indicate Logical Unit
1641 * Not Supported condition,
1642 */
1643 if ((asc == 0x25) && (ascq == 0x0)) {
1644 dev_warn(&h->pdev->dev, "cp %p "
1645 "has check condition\n", cp);
1646 break;
1647 }
1648 }
1649
1650 if (sense_key == NOT_READY) {
1651 /* If Sense is Not Ready, Logical Unit
1652 * Not ready, Manual Intervention
1653 * required
1654 */
1655 if ((asc == 0x04) && (ascq == 0x03)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001656 dev_warn(&h->pdev->dev, "cp %p "
1657 "has check condition: unit "
1658 "not ready, manual "
1659 "intervention required\n", cp);
1660 break;
1661 }
1662 }
Matt Gates1d3b3602010-02-04 08:43:00 -06001663 if (sense_key == ABORTED_COMMAND) {
1664 /* Aborted command is retryable */
1665 dev_warn(&h->pdev->dev, "cp %p "
1666 "has check condition: aborted command: "
1667 "ASC: 0x%x, ASCQ: 0x%x\n",
1668 cp, asc, ascq);
Stephen M. Cameron2e311fb2013-09-23 13:33:41 -05001669 cmd->result |= DID_SOFT_ERROR << 16;
Matt Gates1d3b3602010-02-04 08:43:00 -06001670 break;
1671 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001672 /* Must be some other type of check condition */
Stephen M. Cameron21b8e4e2012-05-01 11:42:25 -05001673 dev_dbg(&h->pdev->dev, "cp %p has check condition: "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001674 "unknown type: "
1675 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1676 "Returning result: 0x%x, "
1677 "cmd=[%02x %02x %02x %02x %02x "
Mike Miller807be732010-02-04 08:43:26 -06001678 "%02x %02x %02x %02x %02x %02x "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001679 "%02x %02x %02x %02x %02x]\n",
1680 cp, sense_key, asc, ascq,
1681 cmd->result,
1682 cmd->cmnd[0], cmd->cmnd[1],
1683 cmd->cmnd[2], cmd->cmnd[3],
1684 cmd->cmnd[4], cmd->cmnd[5],
1685 cmd->cmnd[6], cmd->cmnd[7],
Mike Miller807be732010-02-04 08:43:26 -06001686 cmd->cmnd[8], cmd->cmnd[9],
1687 cmd->cmnd[10], cmd->cmnd[11],
1688 cmd->cmnd[12], cmd->cmnd[13],
1689 cmd->cmnd[14], cmd->cmnd[15]);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001690 break;
1691 }
1692
1693
1694 /* Problem was not a check condition
1695 * Pass it up to the upper layers...
1696 */
1697 if (ei->ScsiStatus) {
1698 dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
1699 "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
1700 "Returning result: 0x%x\n",
1701 cp, ei->ScsiStatus,
1702 sense_key, asc, ascq,
1703 cmd->result);
1704 } else { /* scsi status is zero??? How??? */
1705 dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
1706 "Returning no connection.\n", cp),
1707
1708 /* Ordinarily, this case should never happen,
1709 * but there is a bug in some released firmware
1710 * revisions that allows it to happen if, for
1711 * example, a 4100 backplane loses power and
1712 * the tape drive is in it. We assume that
1713 * it's a fatal error of some kind because we
1714 * can't show that it wasn't. We will make it
1715 * look like selection timeout since that is
1716 * the most common reason for this to occur,
1717 * and it's severe enough.
1718 */
1719
1720 cmd->result = DID_NO_CONNECT << 16;
1721 }
1722 break;
1723
1724 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
1725 break;
1726 case CMD_DATA_OVERRUN:
1727 dev_warn(&h->pdev->dev, "cp %p has"
1728 " completed with data overrun "
1729 "reported\n", cp);
1730 break;
1731 case CMD_INVALID: {
1732 /* print_bytes(cp, sizeof(*cp), 1, 0);
1733 print_cmd(cp); */
1734 /* We get CMD_INVALID if you address a non-existent device
1735 * instead of a selection timeout (no response). You will
1736 * see this if you yank out a drive, then try to access it.
1737 * This is kind of a shame because it means that any other
1738 * CMD_INVALID (e.g. driver bug) will get interpreted as a
1739 * missing target. */
1740 cmd->result = DID_NO_CONNECT << 16;
1741 }
1742 break;
1743 case CMD_PROTOCOL_ERR:
Stephen M. Cameron256d0ea2012-09-14 16:34:25 -05001744 cmd->result = DID_ERROR << 16;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001745 dev_warn(&h->pdev->dev, "cp %p has "
Stephen M. Cameron256d0ea2012-09-14 16:34:25 -05001746 "protocol error\n", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001747 break;
1748 case CMD_HARDWARE_ERR:
1749 cmd->result = DID_ERROR << 16;
1750 dev_warn(&h->pdev->dev, "cp %p had hardware error\n", cp);
1751 break;
1752 case CMD_CONNECTION_LOST:
1753 cmd->result = DID_ERROR << 16;
1754 dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
1755 break;
1756 case CMD_ABORTED:
1757 cmd->result = DID_ABORT << 16;
1758 dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
1759 cp, ei->ScsiStatus);
1760 break;
1761 case CMD_ABORT_FAILED:
1762 cmd->result = DID_ERROR << 16;
1763 dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
1764 break;
1765 case CMD_UNSOLICITED_ABORT:
Stephen M. Cameronf6e76052011-07-26 11:08:52 -05001766 cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
1767 dev_warn(&h->pdev->dev, "cp %p aborted due to an unsolicited "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001768 "abort\n", cp);
1769 break;
1770 case CMD_TIMEOUT:
1771 cmd->result = DID_TIME_OUT << 16;
1772 dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
1773 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06001774 case CMD_UNABORTABLE:
1775 cmd->result = DID_ERROR << 16;
1776 dev_warn(&h->pdev->dev, "Command unabortable\n");
1777 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06001778 case CMD_IOACCEL_DISABLED:
1779 /* This only handles the direct pass-through case since RAID
1780 * offload is handled above. Just attempt a retry.
1781 */
1782 cmd->result = DID_SOFT_ERROR << 16;
1783 dev_warn(&h->pdev->dev,
1784 "cp %p had HP SSD Smart Path error\n", cp);
1785 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001786 default:
1787 cmd->result = DID_ERROR << 16;
1788 dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
1789 cp, ei->CommandStatus);
1790 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001791 cmd_free(h, cp);
Tomas Henzl2cc5bfa2013-08-01 15:14:00 +02001792 cmd->scsi_done(cmd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001793}
1794
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001795static void hpsa_pci_unmap(struct pci_dev *pdev,
1796 struct CommandList *c, int sg_used, int data_direction)
1797{
1798 int i;
1799 union u64bit addr64;
1800
1801 for (i = 0; i < sg_used; i++) {
1802 addr64.val32.lower = c->SG[i].Addr.lower;
1803 addr64.val32.upper = c->SG[i].Addr.upper;
1804 pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len,
1805 data_direction);
1806 }
1807}
1808
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001809static int hpsa_map_one(struct pci_dev *pdev,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001810 struct CommandList *cp,
1811 unsigned char *buf,
1812 size_t buflen,
1813 int data_direction)
1814{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001815 u64 addr64;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001816
1817 if (buflen == 0 || data_direction == PCI_DMA_NONE) {
1818 cp->Header.SGList = 0;
1819 cp->Header.SGTotal = 0;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001820 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001821 }
1822
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001823 addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction);
Shuah Khaneceaae12013-02-20 11:24:34 -06001824 if (dma_mapping_error(&pdev->dev, addr64)) {
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001825 /* Prevent subsequent unmap of something never mapped */
Shuah Khaneceaae12013-02-20 11:24:34 -06001826 cp->Header.SGList = 0;
1827 cp->Header.SGTotal = 0;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001828 return -1;
Shuah Khaneceaae12013-02-20 11:24:34 -06001829 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001830 cp->SG[0].Addr.lower =
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001831 (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001832 cp->SG[0].Addr.upper =
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001833 (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001834 cp->SG[0].Len = buflen;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06001835 cp->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining */
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06001836 cp->Header.SGList = (u8) 1; /* no. SGs contig in this cmd */
1837 cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001838 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001839}
1840
1841static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
1842 struct CommandList *c)
1843{
1844 DECLARE_COMPLETION_ONSTACK(wait);
1845
1846 c->waiting = &wait;
1847 enqueue_cmd_and_start_io(h, c);
1848 wait_for_completion(&wait);
1849}
1850
Stephen M. Camerona0c12412011-10-26 16:22:04 -05001851static void hpsa_scsi_do_simple_cmd_core_if_no_lockup(struct ctlr_info *h,
1852 struct CommandList *c)
1853{
1854 unsigned long flags;
1855
1856 /* If controller lockup detected, fake a hardware error. */
1857 spin_lock_irqsave(&h->lock, flags);
1858 if (unlikely(h->lockup_detected)) {
1859 spin_unlock_irqrestore(&h->lock, flags);
1860 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
1861 } else {
1862 spin_unlock_irqrestore(&h->lock, flags);
1863 hpsa_scsi_do_simple_cmd_core(h, c);
1864 }
1865}
1866
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001867#define MAX_DRIVER_CMD_RETRIES 25
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001868static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
1869 struct CommandList *c, int data_direction)
1870{
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001871 int backoff_time = 10, retry_count = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001872
1873 do {
Joe Perches7630abd2011-05-08 23:32:40 -07001874 memset(c->err_info, 0, sizeof(*c->err_info));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001875 hpsa_scsi_do_simple_cmd_core(h, c);
1876 retry_count++;
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001877 if (retry_count > 3) {
1878 msleep(backoff_time);
1879 if (backoff_time < 1000)
1880 backoff_time *= 2;
1881 }
Matt Bondurant852af202012-05-01 11:42:35 -05001882 } while ((check_for_unit_attention(h, c) ||
Stephen M. Cameron9c2fc162012-05-01 11:42:40 -05001883 check_for_busy(h, c)) &&
1884 retry_count <= MAX_DRIVER_CMD_RETRIES);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001885 hpsa_pci_unmap(h->pdev, c, 1, data_direction);
1886}
1887
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001888static void hpsa_print_cmd(struct ctlr_info *h, char *txt,
1889 struct CommandList *c)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001890{
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001891 const u8 *cdb = c->Request.CDB;
1892 const u8 *lun = c->Header.LUN.LunAddrBytes;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001893
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001894 dev_warn(&h->pdev->dev, "%s: LUN:%02x%02x%02x%02x%02x%02x%02x%02x"
1895 " CDB:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
1896 txt, lun[0], lun[1], lun[2], lun[3],
1897 lun[4], lun[5], lun[6], lun[7],
1898 cdb[0], cdb[1], cdb[2], cdb[3],
1899 cdb[4], cdb[5], cdb[6], cdb[7],
1900 cdb[8], cdb[9], cdb[10], cdb[11],
1901 cdb[12], cdb[13], cdb[14], cdb[15]);
1902}
1903
1904static void hpsa_scsi_interpret_error(struct ctlr_info *h,
1905 struct CommandList *cp)
1906{
1907 const struct ErrorInfo *ei = cp->err_info;
1908 struct device *d = &cp->h->pdev->dev;
1909 const u8 *sd = ei->SenseInfo;
1910
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001911 switch (ei->CommandStatus) {
1912 case CMD_TARGET_STATUS:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001913 hpsa_print_cmd(h, "SCSI status", cp);
1914 if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION)
1915 dev_warn(d, "SCSI Status = 02, Sense key = %02x, ASC = %02x, ASCQ = %02x\n",
1916 sd[2] & 0x0f, sd[12], sd[13]);
1917 else
1918 dev_warn(d, "SCSI Status = %02x\n", ei->ScsiStatus);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001919 if (ei->ScsiStatus == 0)
1920 dev_warn(d, "SCSI status is abnormally zero. "
1921 "(probably indicates selection timeout "
1922 "reported incorrectly due to a known "
1923 "firmware bug, circa July, 2001.)\n");
1924 break;
1925 case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001926 break;
1927 case CMD_DATA_OVERRUN:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001928 hpsa_print_cmd(h, "overrun condition", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001929 break;
1930 case CMD_INVALID: {
1931 /* controller unfortunately reports SCSI passthru's
1932 * to non-existent targets as invalid commands.
1933 */
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001934 hpsa_print_cmd(h, "invalid command", cp);
1935 dev_warn(d, "probably means device no longer present\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001936 }
1937 break;
1938 case CMD_PROTOCOL_ERR:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001939 hpsa_print_cmd(h, "protocol error", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001940 break;
1941 case CMD_HARDWARE_ERR:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001942 hpsa_print_cmd(h, "hardware error", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001943 break;
1944 case CMD_CONNECTION_LOST:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001945 hpsa_print_cmd(h, "connection lost", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001946 break;
1947 case CMD_ABORTED:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001948 hpsa_print_cmd(h, "aborted", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001949 break;
1950 case CMD_ABORT_FAILED:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001951 hpsa_print_cmd(h, "abort failed", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001952 break;
1953 case CMD_UNSOLICITED_ABORT:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001954 hpsa_print_cmd(h, "unsolicited abort", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001955 break;
1956 case CMD_TIMEOUT:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001957 hpsa_print_cmd(h, "timed out", cp);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001958 break;
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06001959 case CMD_UNABORTABLE:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001960 hpsa_print_cmd(h, "unabortable", cp);
Stephen M. Cameron1d5e2ed2011-01-07 10:55:48 -06001961 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001962 default:
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001963 hpsa_print_cmd(h, "unknown status", cp);
1964 dev_warn(d, "Unknown command status %x\n",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001965 ei->CommandStatus);
1966 }
1967}
1968
1969static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06001970 u16 page, unsigned char *buf,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001971 unsigned char bufsize)
1972{
1973 int rc = IO_OK;
1974 struct CommandList *c;
1975 struct ErrorInfo *ei;
1976
1977 c = cmd_special_alloc(h);
1978
1979 if (c == NULL) { /* trouble... */
1980 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06001981 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001982 }
1983
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001984 if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
1985 page, scsi3addr, TYPE_CMD)) {
1986 rc = -1;
1987 goto out;
1988 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001989 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
1990 ei = c->err_info;
1991 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06001992 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001993 rc = -1;
1994 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06001995out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08001996 cmd_special_free(h, c);
1997 return rc;
1998}
1999
Scott Teelbf711ac2014-02-18 13:56:39 -06002000static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
2001 u8 reset_type)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002002{
2003 int rc = IO_OK;
2004 struct CommandList *c;
2005 struct ErrorInfo *ei;
2006
2007 c = cmd_special_alloc(h);
2008
2009 if (c == NULL) { /* trouble... */
2010 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
Stephen M. Camerone9ea04a2010-02-25 14:03:06 -06002011 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002012 }
2013
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002014 /* fill_cmd can't fail here, no data buffer to map. */
Scott Teelbf711ac2014-02-18 13:56:39 -06002015 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
2016 scsi3addr, TYPE_MSG);
2017 c->Request.CDB[1] = reset_type; /* fill_cmd defaults to LUN reset */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002018 hpsa_scsi_do_simple_cmd_core(h, c);
2019 /* no unmap needed here because no data xfer. */
2020
2021 ei = c->err_info;
2022 if (ei->CommandStatus != 0) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002023 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002024 rc = -1;
2025 }
2026 cmd_special_free(h, c);
2027 return rc;
2028}
2029
2030static void hpsa_get_raid_level(struct ctlr_info *h,
2031 unsigned char *scsi3addr, unsigned char *raid_level)
2032{
2033 int rc;
2034 unsigned char *buf;
2035
2036 *raid_level = RAID_UNKNOWN;
2037 buf = kzalloc(64, GFP_KERNEL);
2038 if (!buf)
2039 return;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06002040 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0xC1, buf, 64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002041 if (rc == 0)
2042 *raid_level = buf[8];
2043 if (*raid_level > RAID_UNKNOWN)
2044 *raid_level = RAID_UNKNOWN;
2045 kfree(buf);
2046 return;
2047}
2048
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002049#define HPSA_MAP_DEBUG
2050#ifdef HPSA_MAP_DEBUG
2051static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
2052 struct raid_map_data *map_buff)
2053{
2054 struct raid_map_disk_data *dd = &map_buff->data[0];
2055 int map, row, col;
2056 u16 map_cnt, row_cnt, disks_per_row;
2057
2058 if (rc != 0)
2059 return;
2060
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06002061 /* Show details only if debugging has been activated. */
2062 if (h->raid_offload_debug < 2)
2063 return;
2064
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002065 dev_info(&h->pdev->dev, "structure_size = %u\n",
2066 le32_to_cpu(map_buff->structure_size));
2067 dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
2068 le32_to_cpu(map_buff->volume_blk_size));
2069 dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
2070 le64_to_cpu(map_buff->volume_blk_cnt));
2071 dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
2072 map_buff->phys_blk_shift);
2073 dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
2074 map_buff->parity_rotation_shift);
2075 dev_info(&h->pdev->dev, "strip_size = %u\n",
2076 le16_to_cpu(map_buff->strip_size));
2077 dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
2078 le64_to_cpu(map_buff->disk_starting_blk));
2079 dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
2080 le64_to_cpu(map_buff->disk_blk_cnt));
2081 dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
2082 le16_to_cpu(map_buff->data_disks_per_row));
2083 dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
2084 le16_to_cpu(map_buff->metadata_disks_per_row));
2085 dev_info(&h->pdev->dev, "row_cnt = %u\n",
2086 le16_to_cpu(map_buff->row_cnt));
2087 dev_info(&h->pdev->dev, "layout_map_count = %u\n",
2088 le16_to_cpu(map_buff->layout_map_count));
Scott Teeldd0e19f2014-02-18 13:57:31 -06002089 dev_info(&h->pdev->dev, "flags = %u\n",
2090 le16_to_cpu(map_buff->flags));
2091 if (map_buff->flags & RAID_MAP_FLAG_ENCRYPT_ON)
2092 dev_info(&h->pdev->dev, "encrypytion = ON\n");
2093 else
2094 dev_info(&h->pdev->dev, "encrypytion = OFF\n");
2095 dev_info(&h->pdev->dev, "dekindex = %u\n",
2096 le16_to_cpu(map_buff->dekindex));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002097
2098 map_cnt = le16_to_cpu(map_buff->layout_map_count);
2099 for (map = 0; map < map_cnt; map++) {
2100 dev_info(&h->pdev->dev, "Map%u:\n", map);
2101 row_cnt = le16_to_cpu(map_buff->row_cnt);
2102 for (row = 0; row < row_cnt; row++) {
2103 dev_info(&h->pdev->dev, " Row%u:\n", row);
2104 disks_per_row =
2105 le16_to_cpu(map_buff->data_disks_per_row);
2106 for (col = 0; col < disks_per_row; col++, dd++)
2107 dev_info(&h->pdev->dev,
2108 " D%02u: h=0x%04x xor=%u,%u\n",
2109 col, dd->ioaccel_handle,
2110 dd->xor_mult[0], dd->xor_mult[1]);
2111 disks_per_row =
2112 le16_to_cpu(map_buff->metadata_disks_per_row);
2113 for (col = 0; col < disks_per_row; col++, dd++)
2114 dev_info(&h->pdev->dev,
2115 " M%02u: h=0x%04x xor=%u,%u\n",
2116 col, dd->ioaccel_handle,
2117 dd->xor_mult[0], dd->xor_mult[1]);
2118 }
2119 }
2120}
2121#else
2122static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
2123 __attribute__((unused)) int rc,
2124 __attribute__((unused)) struct raid_map_data *map_buff)
2125{
2126}
2127#endif
2128
2129static int hpsa_get_raid_map(struct ctlr_info *h,
2130 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2131{
2132 int rc = 0;
2133 struct CommandList *c;
2134 struct ErrorInfo *ei;
2135
2136 c = cmd_special_alloc(h);
2137 if (c == NULL) {
2138 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2139 return -ENOMEM;
2140 }
2141 if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
2142 sizeof(this_device->raid_map), 0,
2143 scsi3addr, TYPE_CMD)) {
2144 dev_warn(&h->pdev->dev, "Out of memory in hpsa_get_raid_map()\n");
2145 cmd_special_free(h, c);
2146 return -ENOMEM;
2147 }
2148 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2149 ei = c->err_info;
2150 if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002151 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002152 cmd_special_free(h, c);
2153 return -1;
2154 }
2155 cmd_special_free(h, c);
2156
2157 /* @todo in the future, dynamically allocate RAID map memory */
2158 if (le32_to_cpu(this_device->raid_map.structure_size) >
2159 sizeof(this_device->raid_map)) {
2160 dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
2161 rc = -1;
2162 }
2163 hpsa_debug_map_buff(h, rc, &this_device->raid_map);
2164 return rc;
2165}
2166
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06002167static int hpsa_vpd_page_supported(struct ctlr_info *h,
2168 unsigned char scsi3addr[], u8 page)
2169{
2170 int rc;
2171 int i;
2172 int pages;
2173 unsigned char *buf, bufsize;
2174
2175 buf = kzalloc(256, GFP_KERNEL);
2176 if (!buf)
2177 return 0;
2178
2179 /* Get the size of the page list first */
2180 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
2181 VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
2182 buf, HPSA_VPD_HEADER_SZ);
2183 if (rc != 0)
2184 goto exit_unsupported;
2185 pages = buf[3];
2186 if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
2187 bufsize = pages + HPSA_VPD_HEADER_SZ;
2188 else
2189 bufsize = 255;
2190
2191 /* Get the whole VPD page list */
2192 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
2193 VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
2194 buf, bufsize);
2195 if (rc != 0)
2196 goto exit_unsupported;
2197
2198 pages = buf[3];
2199 for (i = 1; i <= pages; i++)
2200 if (buf[3 + i] == page)
2201 goto exit_supported;
2202exit_unsupported:
2203 kfree(buf);
2204 return 0;
2205exit_supported:
2206 kfree(buf);
2207 return 1;
2208}
2209
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002210static void hpsa_get_ioaccel_status(struct ctlr_info *h,
2211 unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
2212{
2213 int rc;
2214 unsigned char *buf;
2215 u8 ioaccel_status;
2216
2217 this_device->offload_config = 0;
2218 this_device->offload_enabled = 0;
2219
2220 buf = kzalloc(64, GFP_KERNEL);
2221 if (!buf)
2222 return;
Stephen M. Cameron1b70150a2014-02-18 13:57:16 -06002223 if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
2224 goto out;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002225 rc = hpsa_scsi_do_inquiry(h, scsi3addr,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06002226 VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002227 if (rc != 0)
2228 goto out;
2229
2230#define IOACCEL_STATUS_BYTE 4
2231#define OFFLOAD_CONFIGURED_BIT 0x01
2232#define OFFLOAD_ENABLED_BIT 0x02
2233 ioaccel_status = buf[IOACCEL_STATUS_BYTE];
2234 this_device->offload_config =
2235 !!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
2236 if (this_device->offload_config) {
2237 this_device->offload_enabled =
2238 !!(ioaccel_status & OFFLOAD_ENABLED_BIT);
2239 if (hpsa_get_raid_map(h, scsi3addr, this_device))
2240 this_device->offload_enabled = 0;
2241 }
2242out:
2243 kfree(buf);
2244 return;
2245}
2246
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002247/* Get the device id from inquiry page 0x83 */
2248static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
2249 unsigned char *device_id, int buflen)
2250{
2251 int rc;
2252 unsigned char *buf;
2253
2254 if (buflen > 16)
2255 buflen = 16;
2256 buf = kzalloc(64, GFP_KERNEL);
2257 if (!buf)
2258 return -1;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06002259 rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0x83, buf, 64);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002260 if (rc == 0)
2261 memcpy(device_id, &buf[8], buflen);
2262 kfree(buf);
2263 return rc != 0;
2264}
2265
2266static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
2267 struct ReportLUNdata *buf, int bufsize,
2268 int extended_response)
2269{
2270 int rc = IO_OK;
2271 struct CommandList *c;
2272 unsigned char scsi3addr[8];
2273 struct ErrorInfo *ei;
2274
2275 c = cmd_special_alloc(h);
2276 if (c == NULL) { /* trouble... */
2277 dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
2278 return -1;
2279 }
Stephen M. Camerone89c0ae2010-02-04 08:42:04 -06002280 /* address the controller */
2281 memset(scsi3addr, 0, sizeof(scsi3addr));
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002282 if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
2283 buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
2284 rc = -1;
2285 goto out;
2286 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002287 if (extended_response)
2288 c->Request.CDB[1] = extended_response;
2289 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
2290 ei = c->err_info;
2291 if (ei->CommandStatus != 0 &&
2292 ei->CommandStatus != CMD_DATA_UNDERRUN) {
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06002293 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002294 rc = -1;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002295 } else {
2296 if (buf->extended_response_flag != extended_response) {
2297 dev_err(&h->pdev->dev,
2298 "report luns requested format %u, got %u\n",
2299 extended_response,
2300 buf->extended_response_flag);
2301 rc = -1;
2302 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002303 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06002304out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002305 cmd_special_free(h, c);
2306 return rc;
2307}
2308
2309static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
2310 struct ReportLUNdata *buf,
2311 int bufsize, int extended_response)
2312{
2313 return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
2314}
2315
2316static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
2317 struct ReportLUNdata *buf, int bufsize)
2318{
2319 return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
2320}
2321
2322static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
2323 int bus, int target, int lun)
2324{
2325 device->bus = bus;
2326 device->target = target;
2327 device->lun = lun;
2328}
2329
2330static int hpsa_update_device_info(struct ctlr_info *h,
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002331 unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
2332 unsigned char *is_OBDR_device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002333{
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002334
2335#define OBDR_SIG_OFFSET 43
2336#define OBDR_TAPE_SIG "$DR-10"
2337#define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
2338#define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
2339
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06002340 unsigned char *inq_buff;
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002341 unsigned char *obdr_sig;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002342
Stephen M. Cameronea6d3bc2010-02-04 08:42:09 -06002343 inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002344 if (!inq_buff)
2345 goto bail_out;
2346
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002347 /* Do an inquiry to the device to see what it is. */
2348 if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
2349 (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
2350 /* Inquiry failed (msg printed already) */
2351 dev_err(&h->pdev->dev,
2352 "hpsa_update_device_info: inquiry failed\n");
2353 goto bail_out;
2354 }
2355
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002356 this_device->devtype = (inq_buff[0] & 0x1f);
2357 memcpy(this_device->scsi3addr, scsi3addr, 8);
2358 memcpy(this_device->vendor, &inq_buff[8],
2359 sizeof(this_device->vendor));
2360 memcpy(this_device->model, &inq_buff[16],
2361 sizeof(this_device->model));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002362 memset(this_device->device_id, 0,
2363 sizeof(this_device->device_id));
2364 hpsa_get_device_id(h, scsi3addr, this_device->device_id,
2365 sizeof(this_device->device_id));
2366
2367 if (this_device->devtype == TYPE_DISK &&
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002368 is_logical_dev_addr_mode(scsi3addr)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002369 hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002370 if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
2371 hpsa_get_ioaccel_status(h, scsi3addr, this_device);
2372 } else {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002373 this_device->raid_level = RAID_UNKNOWN;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002374 this_device->offload_config = 0;
2375 this_device->offload_enabled = 0;
2376 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002377
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002378 if (is_OBDR_device) {
2379 /* See if this is a One-Button-Disaster-Recovery device
2380 * by looking for "$DR-10" at offset 43 in inquiry data.
2381 */
2382 obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
2383 *is_OBDR_device = (this_device->devtype == TYPE_ROM &&
2384 strncmp(obdr_sig, OBDR_TAPE_SIG,
2385 OBDR_SIG_LEN) == 0);
2386 }
2387
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002388 kfree(inq_buff);
2389 return 0;
2390
2391bail_out:
2392 kfree(inq_buff);
2393 return 1;
2394}
2395
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002396static unsigned char *ext_target_model[] = {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002397 "MSA2012",
2398 "MSA2024",
2399 "MSA2312",
2400 "MSA2324",
Stephen M. Cameronfda38512011-05-03 15:00:07 -05002401 "P2000 G3 SAS",
Stephen M. Camerone06c8e52013-09-23 13:33:56 -05002402 "MSA 2040 SAS",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002403 NULL,
2404};
2405
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002406static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002407{
2408 int i;
2409
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002410 for (i = 0; ext_target_model[i]; i++)
2411 if (strncmp(device->model, ext_target_model[i],
2412 strlen(ext_target_model[i])) == 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002413 return 1;
2414 return 0;
2415}
2416
2417/* Helper function to assign bus, target, lun mapping of devices.
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002418 * Puts non-external target logical volumes on bus 0, external target logical
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002419 * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
2420 * Logical drive target and lun are assigned at this time, but
2421 * physical device lun and target assignment are deferred (assigned
2422 * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
2423 */
2424static void figure_bus_target_lun(struct ctlr_info *h,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002425 u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002426{
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002427 u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002428
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002429 if (!is_logical_dev_addr_mode(lunaddrbytes)) {
2430 /* physical device, target and lun filled in later */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002431 if (is_hba_lunid(lunaddrbytes))
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002432 hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002433 else
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002434 /* defer target, lun assignment for physical devices */
2435 hpsa_set_bus_target_lun(device, 2, -1, -1);
2436 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002437 }
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002438 /* It's a logical device */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002439 if (is_ext_target(h, device)) {
2440 /* external target way, put logicals on bus 1
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002441 * and match target/lun numbers box
2442 * reports, other smart array, bus 0, target 0, match lunid
2443 */
2444 hpsa_set_bus_target_lun(device,
2445 1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
2446 return;
2447 }
2448 hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002449}
2450
2451/*
2452 * If there is no lun 0 on a target, linux won't find any devices.
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002453 * For the external targets (arrays), we have to manually detect the enclosure
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002454 * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
2455 * it for some reason. *tmpdevice is the target we're adding,
2456 * this_device is a pointer into the current element of currentsd[]
2457 * that we're building up in update_scsi_devices(), below.
2458 * lunzerobits is a bitmap that tracks which targets already have a
2459 * lun 0 assigned.
2460 * Returns 1 if an enclosure was added, 0 if not.
2461 */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002462static int add_ext_target_dev(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002463 struct hpsa_scsi_dev_t *tmpdevice,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002464 struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002465 unsigned long lunzerobits[], int *n_ext_target_devs)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002466{
2467 unsigned char scsi3addr[8];
2468
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002469 if (test_bit(tmpdevice->target, lunzerobits))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002470 return 0; /* There is already a lun 0 on this target. */
2471
2472 if (!is_logical_dev_addr_mode(lunaddrbytes))
2473 return 0; /* It's the logical targets that may lack lun 0. */
2474
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002475 if (!is_ext_target(h, tmpdevice))
2476 return 0; /* Only external target devices have this problem. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002477
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002478 if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002479 return 0;
2480
Stephen M. Cameronc4f8a292011-01-07 10:55:43 -06002481 memset(scsi3addr, 0, 8);
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002482 scsi3addr[3] = tmpdevice->target;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002483 if (is_hba_lunid(scsi3addr))
2484 return 0; /* Don't add the RAID controller here. */
2485
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002486 if (is_scsi_rev_5(h))
2487 return 0; /* p1210m doesn't need to do this. */
2488
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002489 if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
Scott Teelaca4a522012-01-19 14:01:19 -06002490 dev_warn(&h->pdev->dev, "Maximum number of external "
2491 "target devices exceeded. Check your hardware "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002492 "configuration.");
2493 return 0;
2494 }
2495
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002496 if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002497 return 0;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002498 (*n_ext_target_devs)++;
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002499 hpsa_set_bus_target_lun(this_device,
2500 tmpdevice->bus, tmpdevice->target, 0);
2501 set_bit(tmpdevice->target, lunzerobits);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002502 return 1;
2503}
2504
2505/*
Scott Teel54b6e9e2014-02-18 13:56:45 -06002506 * Get address of physical disk used for an ioaccel2 mode command:
2507 * 1. Extract ioaccel2 handle from the command.
2508 * 2. Find a matching ioaccel2 handle from list of physical disks.
2509 * 3. Return:
2510 * 1 and set scsi3addr to address of matching physical
2511 * 0 if no matching physical disk was found.
2512 */
2513static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
2514 struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
2515{
2516 struct ReportExtendedLUNdata *physicals = NULL;
2517 int responsesize = 24; /* size of physical extended response */
2518 int extended = 2; /* flag forces reporting 'other dev info'. */
2519 int reportsize = sizeof(*physicals) + HPSA_MAX_PHYS_LUN * responsesize;
2520 u32 nphysicals = 0; /* number of reported physical devs */
2521 int found = 0; /* found match (1) or not (0) */
2522 u32 find; /* handle we need to match */
2523 int i;
2524 struct scsi_cmnd *scmd; /* scsi command within request being aborted */
2525 struct hpsa_scsi_dev_t *d; /* device of request being aborted */
2526 struct io_accel2_cmd *c2a; /* ioaccel2 command to abort */
2527 u32 it_nexus; /* 4 byte device handle for the ioaccel2 cmd */
2528 u32 scsi_nexus; /* 4 byte device handle for the ioaccel2 cmd */
2529
2530 if (ioaccel2_cmd_to_abort->cmd_type != CMD_IOACCEL2)
2531 return 0; /* no match */
2532
2533 /* point to the ioaccel2 device handle */
2534 c2a = &h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
2535 if (c2a == NULL)
2536 return 0; /* no match */
2537
2538 scmd = (struct scsi_cmnd *) ioaccel2_cmd_to_abort->scsi_cmd;
2539 if (scmd == NULL)
2540 return 0; /* no match */
2541
2542 d = scmd->device->hostdata;
2543 if (d == NULL)
2544 return 0; /* no match */
2545
2546 it_nexus = cpu_to_le32((u32) d->ioaccel_handle);
2547 scsi_nexus = cpu_to_le32((u32) c2a->scsi_nexus);
2548 find = c2a->scsi_nexus;
2549
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06002550 if (h->raid_offload_debug > 0)
2551 dev_info(&h->pdev->dev,
2552 "%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",
2553 __func__, scsi_nexus,
2554 d->device_id[0], d->device_id[1], d->device_id[2],
2555 d->device_id[3], d->device_id[4], d->device_id[5],
2556 d->device_id[6], d->device_id[7], d->device_id[8],
2557 d->device_id[9], d->device_id[10], d->device_id[11],
2558 d->device_id[12], d->device_id[13], d->device_id[14],
2559 d->device_id[15]);
2560
Scott Teel54b6e9e2014-02-18 13:56:45 -06002561 /* Get the list of physical devices */
2562 physicals = kzalloc(reportsize, GFP_KERNEL);
2563 if (hpsa_scsi_do_report_phys_luns(h, (struct ReportLUNdata *) physicals,
2564 reportsize, extended)) {
2565 dev_err(&h->pdev->dev,
2566 "Can't lookup %s device handle: report physical LUNs failed.\n",
2567 "HP SSD Smart Path");
2568 kfree(physicals);
2569 return 0;
2570 }
2571 nphysicals = be32_to_cpu(*((__be32 *)physicals->LUNListLength)) /
2572 responsesize;
2573
2574
2575 /* find ioaccel2 handle in list of physicals: */
2576 for (i = 0; i < nphysicals; i++) {
2577 /* handle is in bytes 28-31 of each lun */
2578 if (memcmp(&((struct ReportExtendedLUNdata *)
2579 physicals)->LUN[i][20], &find, 4) != 0) {
2580 continue; /* didn't match */
2581 }
2582 found = 1;
2583 memcpy(scsi3addr, &((struct ReportExtendedLUNdata *)
2584 physicals)->LUN[i][0], 8);
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06002585 if (h->raid_offload_debug > 0)
2586 dev_info(&h->pdev->dev,
2587 "%s: Searched h=0x%08x, Found h=0x%08x, scsiaddr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
2588 __func__, find,
2589 ((struct ReportExtendedLUNdata *)
2590 physicals)->LUN[i][20],
2591 scsi3addr[0], scsi3addr[1], scsi3addr[2],
2592 scsi3addr[3], scsi3addr[4], scsi3addr[5],
2593 scsi3addr[6], scsi3addr[7]);
Scott Teel54b6e9e2014-02-18 13:56:45 -06002594 break; /* found it */
2595 }
2596
2597 kfree(physicals);
2598 if (found)
2599 return 1;
2600 else
2601 return 0;
2602
2603}
2604/*
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002605 * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
2606 * logdev. The number of luns in physdev and logdev are returned in
2607 * *nphysicals and *nlogicals, respectively.
2608 * Returns 0 on success, -1 otherwise.
2609 */
2610static int hpsa_gather_lun_info(struct ctlr_info *h,
2611 int reportlunsize,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002612 struct ReportLUNdata *physdev, u32 *nphysicals, int *physical_mode,
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002613 struct ReportLUNdata *logdev, u32 *nlogicals)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002614{
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002615 int physical_entry_size = 8;
2616
2617 *physical_mode = 0;
2618
2619 /* For I/O accelerator mode we need to read physical device handles */
Mike MIller317d4ad2014-02-18 13:56:20 -06002620 if (h->transMethod & CFGTBL_Trans_io_accel1 ||
2621 h->transMethod & CFGTBL_Trans_io_accel2) {
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002622 *physical_mode = HPSA_REPORT_PHYS_EXTENDED;
2623 physical_entry_size = 24;
2624 }
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002625 if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002626 *physical_mode)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002627 dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
2628 return -1;
2629 }
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002630 *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) /
2631 physical_entry_size;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002632 if (*nphysicals > HPSA_MAX_PHYS_LUN) {
2633 dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
2634 " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2635 *nphysicals - HPSA_MAX_PHYS_LUN);
2636 *nphysicals = HPSA_MAX_PHYS_LUN;
2637 }
2638 if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
2639 dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
2640 return -1;
2641 }
Stephen M. Cameron6df1e952010-02-04 08:42:19 -06002642 *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002643 /* Reject Logicals in excess of our max capability. */
2644 if (*nlogicals > HPSA_MAX_LUN) {
2645 dev_warn(&h->pdev->dev,
2646 "maximum logical LUNs (%d) exceeded. "
2647 "%d LUNs ignored.\n", HPSA_MAX_LUN,
2648 *nlogicals - HPSA_MAX_LUN);
2649 *nlogicals = HPSA_MAX_LUN;
2650 }
2651 if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
2652 dev_warn(&h->pdev->dev,
2653 "maximum logical + physical LUNs (%d) exceeded. "
2654 "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
2655 *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
2656 *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
2657 }
2658 return 0;
2659}
2660
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002661u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i,
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002662 int nphysicals, int nlogicals,
2663 struct ReportExtendedLUNdata *physdev_list,
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002664 struct ReportLUNdata *logdev_list)
2665{
2666 /* Helper function, figure out where the LUN ID info is coming from
2667 * given index i, lists of physical and logical devices, where in
2668 * the list the raid controller is supposed to appear (first or last)
2669 */
2670
2671 int logicals_start = nphysicals + (raid_ctlr_position == 0);
2672 int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
2673
2674 if (i == raid_ctlr_position)
2675 return RAID_CTLR_LUNID;
2676
2677 if (i < logicals_start)
2678 return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0];
2679
2680 if (i < last_device)
2681 return &logdev_list->LUN[i - nphysicals -
2682 (raid_ctlr_position == 0)][0];
2683 BUG();
2684 return NULL;
2685}
2686
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002687static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
2688{
2689 /* the idea here is we could get notified
2690 * that some devices have changed, so we do a report
2691 * physical luns and report logical luns cmd, and adjust
2692 * our list of devices accordingly.
2693 *
2694 * The scsi3addr's of devices won't change so long as the
2695 * adapter is not reset. That means we can rescan and
2696 * tell which devices we already know about, vs. new
2697 * devices, vs. disappearing devices.
2698 */
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002699 struct ReportExtendedLUNdata *physdev_list = NULL;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002700 struct ReportLUNdata *logdev_list = NULL;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002701 u32 nphysicals = 0;
2702 u32 nlogicals = 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002703 int physical_mode = 0;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002704 u32 ndev_allocated = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002705 struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
2706 int ncurrent = 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002707 int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 24;
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002708 int i, n_ext_target_devs, ndevs_to_allocate;
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002709 int raid_ctlr_position;
Scott Teelaca4a522012-01-19 14:01:19 -06002710 DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002711
Scott Teelcfe5bad2011-10-26 16:21:07 -05002712 currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002713 physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
2714 logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002715 tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
2716
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002717 if (!currentsd || !physdev_list || !logdev_list || !tmpdevice) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002718 dev_err(&h->pdev->dev, "out of memory\n");
2719 goto out;
2720 }
2721 memset(lunzerobits, 0, sizeof(lunzerobits));
2722
Matt Gatesa93aa1f2014-02-18 13:55:07 -06002723 if (hpsa_gather_lun_info(h, reportlunsize,
2724 (struct ReportLUNdata *) physdev_list, &nphysicals,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002725 &physical_mode, logdev_list, &nlogicals))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002726 goto out;
2727
Scott Teelaca4a522012-01-19 14:01:19 -06002728 /* We might see up to the maximum number of logical and physical disks
2729 * plus external target devices, and a device for the local RAID
2730 * controller.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002731 */
Scott Teelaca4a522012-01-19 14:01:19 -06002732 ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002733
2734 /* Allocate the per device structures */
2735 for (i = 0; i < ndevs_to_allocate; i++) {
Scott Teelb7ec0212011-10-26 16:21:12 -05002736 if (i >= HPSA_MAX_DEVICES) {
2737 dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
2738 " %d devices ignored.\n", HPSA_MAX_DEVICES,
2739 ndevs_to_allocate - HPSA_MAX_DEVICES);
2740 break;
2741 }
2742
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002743 currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
2744 if (!currentsd[i]) {
2745 dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
2746 __FILE__, __LINE__);
2747 goto out;
2748 }
2749 ndev_allocated++;
2750 }
2751
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002752 if (unlikely(is_scsi_rev_5(h)))
2753 raid_ctlr_position = 0;
2754 else
2755 raid_ctlr_position = nphysicals + nlogicals;
2756
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002757 /* adjust our table of devices */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002758 n_ext_target_devs = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002759 for (i = 0; i < nphysicals + nlogicals + 1; i++) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002760 u8 *lunaddrbytes, is_OBDR = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002761
2762 /* Figure out where the LUN ID info is coming from */
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002763 lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
2764 i, nphysicals, nlogicals, physdev_list, logdev_list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002765 /* skip masked physical devices. */
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06002766 if (lunaddrbytes[3] & 0xC0 &&
2767 i < nphysicals + (raid_ctlr_position == 0))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002768 continue;
2769
2770 /* Get device type, vendor, model, device id */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002771 if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
2772 &is_OBDR))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002773 continue; /* skip it if we can't talk to it. */
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002774 figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002775 this_device = currentsd[ncurrent];
2776
2777 /*
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002778 * For external target devices, we have to insert a LUN 0 which
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002779 * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
2780 * is nonetheless an enclosure device there. We have to
2781 * present that otherwise linux won't find anything if
2782 * there is no lun 0.
2783 */
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002784 if (add_ext_target_dev(h, tmpdevice, this_device,
Stephen M. Cameron1f310bd2012-01-19 14:01:14 -06002785 lunaddrbytes, lunzerobits,
Scott Teel4f4eb9f2012-01-19 14:01:25 -06002786 &n_ext_target_devs)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002787 ncurrent++;
2788 this_device = currentsd[ncurrent];
2789 }
2790
2791 *this_device = *tmpdevice;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002792
2793 switch (this_device->devtype) {
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002794 case TYPE_ROM:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002795 /* We don't *really* support actual CD-ROM devices,
2796 * just "One Button Disaster Recovery" tape drive
2797 * which temporarily pretends to be a CD-ROM drive.
2798 * So we check that the device is really an OBDR tape
2799 * device by checking for "$DR-10" in bytes 43-48 of
2800 * the inquiry data.
2801 */
Stephen M. Cameron0b0e1d62011-08-09 08:17:30 -05002802 if (is_OBDR)
2803 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002804 break;
2805 case TYPE_DISK:
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002806 if (i >= nphysicals) {
2807 ncurrent++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002808 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002809 }
2810 if (physical_mode == HPSA_REPORT_PHYS_EXTENDED) {
2811 memcpy(&this_device->ioaccel_handle,
2812 &lunaddrbytes[20],
2813 sizeof(this_device->ioaccel_handle));
2814 ncurrent++;
2815 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002816 break;
2817 case TYPE_TAPE:
2818 case TYPE_MEDIUM_CHANGER:
2819 ncurrent++;
2820 break;
2821 case TYPE_RAID:
2822 /* Only present the Smartarray HBA as a RAID controller.
2823 * If it's a RAID controller other than the HBA itself
2824 * (an external RAID controller, MSA500 or similar)
2825 * don't present it.
2826 */
2827 if (!is_hba_lunid(lunaddrbytes))
2828 break;
2829 ncurrent++;
2830 break;
2831 default:
2832 break;
2833 }
Scott Teelcfe5bad2011-10-26 16:21:07 -05002834 if (ncurrent >= HPSA_MAX_DEVICES)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002835 break;
2836 }
2837 adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
2838out:
2839 kfree(tmpdevice);
2840 for (i = 0; i < ndev_allocated; i++)
2841 kfree(currentsd[i]);
2842 kfree(currentsd);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002843 kfree(physdev_list);
2844 kfree(logdev_list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002845}
2846
2847/* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
2848 * dma mapping and fills in the scatter gather entries of the
2849 * hpsa command, cp.
2850 */
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002851static int hpsa_scatter_gather(struct ctlr_info *h,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002852 struct CommandList *cp,
2853 struct scsi_cmnd *cmd)
2854{
2855 unsigned int len;
2856 struct scatterlist *sg;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002857 u64 addr64;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002858 int use_sg, i, sg_index, chained;
2859 struct SGDescriptor *curr_sg;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002860
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002861 BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002862
2863 use_sg = scsi_dma_map(cmd);
2864 if (use_sg < 0)
2865 return use_sg;
2866
2867 if (!use_sg)
2868 goto sglist_finished;
2869
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002870 curr_sg = cp->SG;
2871 chained = 0;
2872 sg_index = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002873 scsi_for_each_sg(cmd, sg, use_sg, i) {
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002874 if (i == h->max_cmd_sg_entries - 1 &&
2875 use_sg > h->max_cmd_sg_entries) {
2876 chained = 1;
2877 curr_sg = h->cmd_sg_list[cp->cmdindex];
2878 sg_index = 0;
2879 }
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002880 addr64 = (u64) sg_dma_address(sg);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002881 len = sg_dma_len(sg);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002882 curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
2883 curr_sg->Addr.upper = (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
2884 curr_sg->Len = len;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06002885 curr_sg->Ext = (i < scsi_sg_count(cmd) - 1) ? 0 : HPSA_SG_LAST;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002886 curr_sg++;
2887 }
2888
2889 if (use_sg + chained > h->maxSG)
2890 h->maxSG = use_sg + chained;
2891
2892 if (chained) {
2893 cp->Header.SGList = h->max_cmd_sg_entries;
2894 cp->Header.SGTotal = (u16) (use_sg + 1);
Stephen M. Camerone2bea6d2013-02-20 11:24:46 -06002895 if (hpsa_map_sg_chain_block(h, cp)) {
2896 scsi_dma_unmap(cmd);
2897 return -1;
2898 }
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06002899 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002900 }
2901
2902sglist_finished:
2903
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06002904 cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
2905 cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08002906 return 0;
2907}
2908
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002909#define IO_ACCEL_INELIGIBLE (1)
2910static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
2911{
2912 int is_write = 0;
2913 u32 block;
2914 u32 block_cnt;
2915
2916 /* Perform some CDB fixups if needed using 10 byte reads/writes only */
2917 switch (cdb[0]) {
2918 case WRITE_6:
2919 case WRITE_12:
2920 is_write = 1;
2921 case READ_6:
2922 case READ_12:
2923 if (*cdb_len == 6) {
2924 block = (((u32) cdb[2]) << 8) | cdb[3];
2925 block_cnt = cdb[4];
2926 } else {
2927 BUG_ON(*cdb_len != 12);
2928 block = (((u32) cdb[2]) << 24) |
2929 (((u32) cdb[3]) << 16) |
2930 (((u32) cdb[4]) << 8) |
2931 cdb[5];
2932 block_cnt =
2933 (((u32) cdb[6]) << 24) |
2934 (((u32) cdb[7]) << 16) |
2935 (((u32) cdb[8]) << 8) |
2936 cdb[9];
2937 }
2938 if (block_cnt > 0xffff)
2939 return IO_ACCEL_INELIGIBLE;
2940
2941 cdb[0] = is_write ? WRITE_10 : READ_10;
2942 cdb[1] = 0;
2943 cdb[2] = (u8) (block >> 24);
2944 cdb[3] = (u8) (block >> 16);
2945 cdb[4] = (u8) (block >> 8);
2946 cdb[5] = (u8) (block);
2947 cdb[6] = 0;
2948 cdb[7] = (u8) (block_cnt >> 8);
2949 cdb[8] = (u8) (block_cnt);
2950 cdb[9] = 0;
2951 *cdb_len = 10;
2952 break;
2953 }
2954 return 0;
2955}
2956
Scott Teelc3497752014-02-18 13:56:34 -06002957static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002958 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
2959 u8 *scsi3addr)
Matt Gatese1f7de02014-02-18 13:55:17 -06002960{
2961 struct scsi_cmnd *cmd = c->scsi_cmd;
Matt Gatese1f7de02014-02-18 13:55:17 -06002962 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
2963 unsigned int len;
2964 unsigned int total_len = 0;
2965 struct scatterlist *sg;
2966 u64 addr64;
2967 int use_sg, i;
2968 struct SGDescriptor *curr_sg;
2969 u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
2970
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002971 /* TODO: implement chaining support */
2972 if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
2973 return IO_ACCEL_INELIGIBLE;
2974
Matt Gatese1f7de02014-02-18 13:55:17 -06002975 BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
2976
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06002977 if (fixup_ioaccel_cdb(cdb, &cdb_len))
2978 return IO_ACCEL_INELIGIBLE;
2979
Matt Gatese1f7de02014-02-18 13:55:17 -06002980 c->cmd_type = CMD_IOACCEL1;
2981
2982 /* Adjust the DMA address to point to the accelerated command buffer */
2983 c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
2984 (c->cmdindex * sizeof(*cp));
2985 BUG_ON(c->busaddr & 0x0000007F);
2986
2987 use_sg = scsi_dma_map(cmd);
2988 if (use_sg < 0)
2989 return use_sg;
2990
2991 if (use_sg) {
2992 curr_sg = cp->SG;
2993 scsi_for_each_sg(cmd, sg, use_sg, i) {
2994 addr64 = (u64) sg_dma_address(sg);
2995 len = sg_dma_len(sg);
2996 total_len += len;
2997 curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
2998 curr_sg->Addr.upper =
2999 (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
3000 curr_sg->Len = len;
3001
3002 if (i == (scsi_sg_count(cmd) - 1))
3003 curr_sg->Ext = HPSA_SG_LAST;
3004 else
3005 curr_sg->Ext = 0; /* we are not chaining */
3006 curr_sg++;
3007 }
3008
3009 switch (cmd->sc_data_direction) {
3010 case DMA_TO_DEVICE:
3011 control |= IOACCEL1_CONTROL_DATA_OUT;
3012 break;
3013 case DMA_FROM_DEVICE:
3014 control |= IOACCEL1_CONTROL_DATA_IN;
3015 break;
3016 case DMA_NONE:
3017 control |= IOACCEL1_CONTROL_NODATAXFER;
3018 break;
3019 default:
3020 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3021 cmd->sc_data_direction);
3022 BUG();
3023 break;
3024 }
3025 } else {
3026 control |= IOACCEL1_CONTROL_NODATAXFER;
3027 }
3028
Scott Teelc3497752014-02-18 13:56:34 -06003029 c->Header.SGList = use_sg;
Matt Gatese1f7de02014-02-18 13:55:17 -06003030 /* Fill out the command structure to submit */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003031 cp->dev_handle = ioaccel_handle & 0xFFFF;
Matt Gatese1f7de02014-02-18 13:55:17 -06003032 cp->transfer_len = total_len;
3033 cp->io_flags = IOACCEL1_IOFLAGS_IO_REQ |
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003034 (cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK);
Matt Gatese1f7de02014-02-18 13:55:17 -06003035 cp->control = control;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003036 memcpy(cp->CDB, cdb, cdb_len);
3037 memcpy(cp->CISS_LUN, scsi3addr, 8);
Scott Teelc3497752014-02-18 13:56:34 -06003038 /* Tag was already set at init time. */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003039 enqueue_cmd_and_start_io(h, c);
Matt Gatese1f7de02014-02-18 13:55:17 -06003040 return 0;
3041}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003042
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003043/*
3044 * Queue a command directly to a device behind the controller using the
3045 * I/O accelerator path.
3046 */
3047static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
3048 struct CommandList *c)
3049{
3050 struct scsi_cmnd *cmd = c->scsi_cmd;
3051 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3052
3053 return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
3054 cmd->cmnd, cmd->cmd_len, dev->scsi3addr);
3055}
3056
Scott Teeldd0e19f2014-02-18 13:57:31 -06003057/*
3058 * Set encryption parameters for the ioaccel2 request
3059 */
3060static void set_encrypt_ioaccel2(struct ctlr_info *h,
3061 struct CommandList *c, struct io_accel2_cmd *cp)
3062{
3063 struct scsi_cmnd *cmd = c->scsi_cmd;
3064 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3065 struct raid_map_data *map = &dev->raid_map;
3066 u64 first_block;
3067
3068 BUG_ON(!(dev->offload_config && dev->offload_enabled));
3069
3070 /* Are we doing encryption on this device */
3071 if (!(map->flags & RAID_MAP_FLAG_ENCRYPT_ON))
3072 return;
3073 /* Set the data encryption key index. */
3074 cp->dekindex = map->dekindex;
3075
3076 /* Set the encryption enable flag, encoded into direction field. */
3077 cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
3078
3079 /* Set encryption tweak values based on logical block address
3080 * If block size is 512, tweak value is LBA.
3081 * For other block sizes, tweak is (LBA * block size)/ 512)
3082 */
3083 switch (cmd->cmnd[0]) {
3084 /* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
3085 case WRITE_6:
3086 case READ_6:
3087 if (map->volume_blk_size == 512) {
3088 cp->tweak_lower =
3089 (((u32) cmd->cmnd[2]) << 8) |
3090 cmd->cmnd[3];
3091 cp->tweak_upper = 0;
3092 } else {
3093 first_block =
3094 (((u64) cmd->cmnd[2]) << 8) |
3095 cmd->cmnd[3];
3096 first_block = (first_block * map->volume_blk_size)/512;
3097 cp->tweak_lower = (u32)first_block;
3098 cp->tweak_upper = (u32)(first_block >> 32);
3099 }
3100 break;
3101 case WRITE_10:
3102 case READ_10:
3103 if (map->volume_blk_size == 512) {
3104 cp->tweak_lower =
3105 (((u32) cmd->cmnd[2]) << 24) |
3106 (((u32) cmd->cmnd[3]) << 16) |
3107 (((u32) cmd->cmnd[4]) << 8) |
3108 cmd->cmnd[5];
3109 cp->tweak_upper = 0;
3110 } else {
3111 first_block =
3112 (((u64) cmd->cmnd[2]) << 24) |
3113 (((u64) cmd->cmnd[3]) << 16) |
3114 (((u64) cmd->cmnd[4]) << 8) |
3115 cmd->cmnd[5];
3116 first_block = (first_block * map->volume_blk_size)/512;
3117 cp->tweak_lower = (u32)first_block;
3118 cp->tweak_upper = (u32)(first_block >> 32);
3119 }
3120 break;
3121 /* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
3122 case WRITE_12:
3123 case READ_12:
3124 if (map->volume_blk_size == 512) {
3125 cp->tweak_lower =
3126 (((u32) cmd->cmnd[2]) << 24) |
3127 (((u32) cmd->cmnd[3]) << 16) |
3128 (((u32) cmd->cmnd[4]) << 8) |
3129 cmd->cmnd[5];
3130 cp->tweak_upper = 0;
3131 } else {
3132 first_block =
3133 (((u64) cmd->cmnd[2]) << 24) |
3134 (((u64) cmd->cmnd[3]) << 16) |
3135 (((u64) cmd->cmnd[4]) << 8) |
3136 cmd->cmnd[5];
3137 first_block = (first_block * map->volume_blk_size)/512;
3138 cp->tweak_lower = (u32)first_block;
3139 cp->tweak_upper = (u32)(first_block >> 32);
3140 }
3141 break;
3142 case WRITE_16:
3143 case READ_16:
3144 if (map->volume_blk_size == 512) {
3145 cp->tweak_lower =
3146 (((u32) cmd->cmnd[6]) << 24) |
3147 (((u32) cmd->cmnd[7]) << 16) |
3148 (((u32) cmd->cmnd[8]) << 8) |
3149 cmd->cmnd[9];
3150 cp->tweak_upper =
3151 (((u32) cmd->cmnd[2]) << 24) |
3152 (((u32) cmd->cmnd[3]) << 16) |
3153 (((u32) cmd->cmnd[4]) << 8) |
3154 cmd->cmnd[5];
3155 } else {
3156 first_block =
3157 (((u64) cmd->cmnd[2]) << 56) |
3158 (((u64) cmd->cmnd[3]) << 48) |
3159 (((u64) cmd->cmnd[4]) << 40) |
3160 (((u64) cmd->cmnd[5]) << 32) |
3161 (((u64) cmd->cmnd[6]) << 24) |
3162 (((u64) cmd->cmnd[7]) << 16) |
3163 (((u64) cmd->cmnd[8]) << 8) |
3164 cmd->cmnd[9];
3165 first_block = (first_block * map->volume_blk_size)/512;
3166 cp->tweak_lower = (u32)first_block;
3167 cp->tweak_upper = (u32)(first_block >> 32);
3168 }
3169 break;
3170 default:
3171 dev_err(&h->pdev->dev,
3172 "ERROR: %s: IOACCEL request CDB size not supported for encryption\n",
3173 __func__);
3174 BUG();
3175 break;
3176 }
3177}
3178
Scott Teelc3497752014-02-18 13:56:34 -06003179static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
3180 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
3181 u8 *scsi3addr)
3182{
3183 struct scsi_cmnd *cmd = c->scsi_cmd;
3184 struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
3185 struct ioaccel2_sg_element *curr_sg;
3186 int use_sg, i;
3187 struct scatterlist *sg;
3188 u64 addr64;
3189 u32 len;
3190 u32 total_len = 0;
3191
3192 if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
3193 return IO_ACCEL_INELIGIBLE;
3194
3195 if (fixup_ioaccel_cdb(cdb, &cdb_len))
3196 return IO_ACCEL_INELIGIBLE;
3197 c->cmd_type = CMD_IOACCEL2;
3198 /* Adjust the DMA address to point to the accelerated command buffer */
3199 c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
3200 (c->cmdindex * sizeof(*cp));
3201 BUG_ON(c->busaddr & 0x0000007F);
3202
3203 memset(cp, 0, sizeof(*cp));
3204 cp->IU_type = IOACCEL2_IU_TYPE;
3205
3206 use_sg = scsi_dma_map(cmd);
3207 if (use_sg < 0)
3208 return use_sg;
3209
3210 if (use_sg) {
3211 BUG_ON(use_sg > IOACCEL2_MAXSGENTRIES);
3212 curr_sg = cp->sg;
3213 scsi_for_each_sg(cmd, sg, use_sg, i) {
3214 addr64 = (u64) sg_dma_address(sg);
3215 len = sg_dma_len(sg);
3216 total_len += len;
3217 curr_sg->address = cpu_to_le64(addr64);
3218 curr_sg->length = cpu_to_le32(len);
3219 curr_sg->reserved[0] = 0;
3220 curr_sg->reserved[1] = 0;
3221 curr_sg->reserved[2] = 0;
3222 curr_sg->chain_indicator = 0;
3223 curr_sg++;
3224 }
3225
3226 switch (cmd->sc_data_direction) {
3227 case DMA_TO_DEVICE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06003228 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3229 cp->direction |= IOACCEL2_DIR_DATA_OUT;
Scott Teelc3497752014-02-18 13:56:34 -06003230 break;
3231 case DMA_FROM_DEVICE:
Scott Teeldd0e19f2014-02-18 13:57:31 -06003232 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3233 cp->direction |= IOACCEL2_DIR_DATA_IN;
Scott Teelc3497752014-02-18 13:56:34 -06003234 break;
3235 case DMA_NONE:
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 break;
3239 default:
3240 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3241 cmd->sc_data_direction);
3242 BUG();
3243 break;
3244 }
3245 } else {
Scott Teeldd0e19f2014-02-18 13:57:31 -06003246 cp->direction &= ~IOACCEL2_DIRECTION_MASK;
3247 cp->direction |= IOACCEL2_DIR_NO_DATA;
Scott Teelc3497752014-02-18 13:56:34 -06003248 }
Scott Teeldd0e19f2014-02-18 13:57:31 -06003249
3250 /* Set encryption parameters, if necessary */
3251 set_encrypt_ioaccel2(h, c, cp);
3252
Scott Teelc3497752014-02-18 13:56:34 -06003253 cp->scsi_nexus = ioaccel_handle;
Scott Teeldd0e19f2014-02-18 13:57:31 -06003254 cp->Tag = (c->cmdindex << DIRECT_LOOKUP_SHIFT) |
Scott Teelc3497752014-02-18 13:56:34 -06003255 DIRECT_LOOKUP_BIT;
3256 memcpy(cp->cdb, cdb, sizeof(cp->cdb));
3257 memset(cp->cciss_lun, 0, sizeof(cp->cciss_lun));
3258 cp->cmd_priority_task_attr = 0;
3259
3260 /* fill in sg elements */
3261 cp->sg_count = (u8) use_sg;
3262
3263 cp->data_len = cpu_to_le32(total_len);
3264 cp->err_ptr = cpu_to_le64(c->busaddr +
3265 offsetof(struct io_accel2_cmd, error_data));
3266 cp->err_len = cpu_to_le32((u32) sizeof(cp->error_data));
3267
3268 enqueue_cmd_and_start_io(h, c);
3269 return 0;
3270}
3271
3272/*
3273 * Queue a command to the correct I/O accelerator path.
3274 */
3275static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
3276 struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
3277 u8 *scsi3addr)
3278{
3279 if (h->transMethod & CFGTBL_Trans_io_accel1)
3280 return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
3281 cdb, cdb_len, scsi3addr);
3282 else
3283 return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
3284 cdb, cdb_len, scsi3addr);
3285}
3286
Scott Teel6b80b182014-02-18 13:56:55 -06003287static void raid_map_helper(struct raid_map_data *map,
3288 int offload_to_mirror, u32 *map_index, u32 *current_group)
3289{
3290 if (offload_to_mirror == 0) {
3291 /* use physical disk in the first mirrored group. */
3292 *map_index %= map->data_disks_per_row;
3293 return;
3294 }
3295 do {
3296 /* determine mirror group that *map_index indicates */
3297 *current_group = *map_index / map->data_disks_per_row;
3298 if (offload_to_mirror == *current_group)
3299 continue;
3300 if (*current_group < (map->layout_map_count - 1)) {
3301 /* select map index from next group */
3302 *map_index += map->data_disks_per_row;
3303 (*current_group)++;
3304 } else {
3305 /* select map index from first group */
3306 *map_index %= map->data_disks_per_row;
3307 *current_group = 0;
3308 }
3309 } while (offload_to_mirror != *current_group);
3310}
3311
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003312/*
3313 * Attempt to perform offload RAID mapping for a logical volume I/O.
3314 */
3315static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
3316 struct CommandList *c)
3317{
3318 struct scsi_cmnd *cmd = c->scsi_cmd;
3319 struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
3320 struct raid_map_data *map = &dev->raid_map;
3321 struct raid_map_disk_data *dd = &map->data[0];
3322 int is_write = 0;
3323 u32 map_index;
3324 u64 first_block, last_block;
3325 u32 block_cnt;
3326 u32 blocks_per_row;
3327 u64 first_row, last_row;
3328 u32 first_row_offset, last_row_offset;
3329 u32 first_column, last_column;
Scott Teel6b80b182014-02-18 13:56:55 -06003330 u64 r0_first_row, r0_last_row;
3331 u32 r5or6_blocks_per_row;
3332 u64 r5or6_first_row, r5or6_last_row;
3333 u32 r5or6_first_row_offset, r5or6_last_row_offset;
3334 u32 r5or6_first_column, r5or6_last_column;
3335 u32 total_disks_per_row;
3336 u32 stripesize;
3337 u32 first_group, last_group, current_group;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003338 u32 map_row;
3339 u32 disk_handle;
3340 u64 disk_block;
3341 u32 disk_block_cnt;
3342 u8 cdb[16];
3343 u8 cdb_len;
3344#if BITS_PER_LONG == 32
3345 u64 tmpdiv;
3346#endif
Scott Teel6b80b182014-02-18 13:56:55 -06003347 int offload_to_mirror;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003348
3349 BUG_ON(!(dev->offload_config && dev->offload_enabled));
3350
3351 /* check for valid opcode, get LBA and block count */
3352 switch (cmd->cmnd[0]) {
3353 case WRITE_6:
3354 is_write = 1;
3355 case READ_6:
3356 first_block =
3357 (((u64) cmd->cmnd[2]) << 8) |
3358 cmd->cmnd[3];
3359 block_cnt = cmd->cmnd[4];
3360 break;
3361 case WRITE_10:
3362 is_write = 1;
3363 case READ_10:
3364 first_block =
3365 (((u64) cmd->cmnd[2]) << 24) |
3366 (((u64) cmd->cmnd[3]) << 16) |
3367 (((u64) cmd->cmnd[4]) << 8) |
3368 cmd->cmnd[5];
3369 block_cnt =
3370 (((u32) cmd->cmnd[7]) << 8) |
3371 cmd->cmnd[8];
3372 break;
3373 case WRITE_12:
3374 is_write = 1;
3375 case READ_12:
3376 first_block =
3377 (((u64) cmd->cmnd[2]) << 24) |
3378 (((u64) cmd->cmnd[3]) << 16) |
3379 (((u64) cmd->cmnd[4]) << 8) |
3380 cmd->cmnd[5];
3381 block_cnt =
3382 (((u32) cmd->cmnd[6]) << 24) |
3383 (((u32) cmd->cmnd[7]) << 16) |
3384 (((u32) cmd->cmnd[8]) << 8) |
3385 cmd->cmnd[9];
3386 break;
3387 case WRITE_16:
3388 is_write = 1;
3389 case READ_16:
3390 first_block =
3391 (((u64) cmd->cmnd[2]) << 56) |
3392 (((u64) cmd->cmnd[3]) << 48) |
3393 (((u64) cmd->cmnd[4]) << 40) |
3394 (((u64) cmd->cmnd[5]) << 32) |
3395 (((u64) cmd->cmnd[6]) << 24) |
3396 (((u64) cmd->cmnd[7]) << 16) |
3397 (((u64) cmd->cmnd[8]) << 8) |
3398 cmd->cmnd[9];
3399 block_cnt =
3400 (((u32) cmd->cmnd[10]) << 24) |
3401 (((u32) cmd->cmnd[11]) << 16) |
3402 (((u32) cmd->cmnd[12]) << 8) |
3403 cmd->cmnd[13];
3404 break;
3405 default:
3406 return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
3407 }
3408 BUG_ON(block_cnt == 0);
3409 last_block = first_block + block_cnt - 1;
3410
3411 /* check for write to non-RAID-0 */
3412 if (is_write && dev->raid_level != 0)
3413 return IO_ACCEL_INELIGIBLE;
3414
3415 /* check for invalid block or wraparound */
3416 if (last_block >= map->volume_blk_cnt || last_block < first_block)
3417 return IO_ACCEL_INELIGIBLE;
3418
3419 /* calculate stripe information for the request */
3420 blocks_per_row = map->data_disks_per_row * map->strip_size;
3421#if BITS_PER_LONG == 32
3422 tmpdiv = first_block;
3423 (void) do_div(tmpdiv, blocks_per_row);
3424 first_row = tmpdiv;
3425 tmpdiv = last_block;
3426 (void) do_div(tmpdiv, blocks_per_row);
3427 last_row = tmpdiv;
3428 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
3429 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
3430 tmpdiv = first_row_offset;
3431 (void) do_div(tmpdiv, map->strip_size);
3432 first_column = tmpdiv;
3433 tmpdiv = last_row_offset;
3434 (void) do_div(tmpdiv, map->strip_size);
3435 last_column = tmpdiv;
3436#else
3437 first_row = first_block / blocks_per_row;
3438 last_row = last_block / blocks_per_row;
3439 first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
3440 last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
3441 first_column = first_row_offset / map->strip_size;
3442 last_column = last_row_offset / map->strip_size;
3443#endif
3444
3445 /* if this isn't a single row/column then give to the controller */
3446 if ((first_row != last_row) || (first_column != last_column))
3447 return IO_ACCEL_INELIGIBLE;
3448
3449 /* proceeding with driver mapping */
Scott Teel6b80b182014-02-18 13:56:55 -06003450 total_disks_per_row = map->data_disks_per_row +
3451 map->metadata_disks_per_row;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003452 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
3453 map->row_cnt;
Scott Teel6b80b182014-02-18 13:56:55 -06003454 map_index = (map_row * total_disks_per_row) + first_column;
3455
3456 switch (dev->raid_level) {
3457 case HPSA_RAID_0:
3458 break; /* nothing special to do */
3459 case HPSA_RAID_1:
3460 /* Handles load balance across RAID 1 members.
3461 * (2-drive R1 and R10 with even # of drives.)
3462 * Appropriate for SSDs, not optimal for HDDs
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003463 */
Scott Teel6b80b182014-02-18 13:56:55 -06003464 BUG_ON(map->layout_map_count != 2);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003465 if (dev->offload_to_mirror)
3466 map_index += map->data_disks_per_row;
3467 dev->offload_to_mirror = !dev->offload_to_mirror;
Scott Teel6b80b182014-02-18 13:56:55 -06003468 break;
3469 case HPSA_RAID_ADM:
3470 /* Handles N-way mirrors (R1-ADM)
3471 * and R10 with # of drives divisible by 3.)
3472 */
3473 BUG_ON(map->layout_map_count != 3);
3474
3475 offload_to_mirror = dev->offload_to_mirror;
3476 raid_map_helper(map, offload_to_mirror,
3477 &map_index, &current_group);
3478 /* set mirror group to use next time */
3479 offload_to_mirror =
3480 (offload_to_mirror >= map->layout_map_count - 1)
3481 ? 0 : offload_to_mirror + 1;
3482 /* FIXME: remove after debug/dev */
3483 BUG_ON(offload_to_mirror >= map->layout_map_count);
3484 dev_warn(&h->pdev->dev,
3485 "DEBUG: Using physical disk map index %d from mirror group %d\n",
3486 map_index, offload_to_mirror);
3487 dev->offload_to_mirror = offload_to_mirror;
3488 /* Avoid direct use of dev->offload_to_mirror within this
3489 * function since multiple threads might simultaneously
3490 * increment it beyond the range of dev->layout_map_count -1.
3491 */
3492 break;
3493 case HPSA_RAID_5:
3494 case HPSA_RAID_6:
3495 if (map->layout_map_count <= 1)
3496 break;
3497
3498 /* Verify first and last block are in same RAID group */
3499 r5or6_blocks_per_row =
3500 map->strip_size * map->data_disks_per_row;
3501 BUG_ON(r5or6_blocks_per_row == 0);
3502 stripesize = r5or6_blocks_per_row * map->layout_map_count;
3503#if BITS_PER_LONG == 32
3504 tmpdiv = first_block;
3505 first_group = do_div(tmpdiv, stripesize);
3506 tmpdiv = first_group;
3507 (void) do_div(tmpdiv, r5or6_blocks_per_row);
3508 first_group = tmpdiv;
3509 tmpdiv = last_block;
3510 last_group = do_div(tmpdiv, stripesize);
3511 tmpdiv = last_group;
3512 (void) do_div(tmpdiv, r5or6_blocks_per_row);
3513 last_group = tmpdiv;
3514#else
3515 first_group = (first_block % stripesize) / r5or6_blocks_per_row;
3516 last_group = (last_block % stripesize) / r5or6_blocks_per_row;
3517 if (first_group != last_group)
3518#endif
3519 return IO_ACCEL_INELIGIBLE;
3520
3521 /* Verify request is in a single row of RAID 5/6 */
3522#if BITS_PER_LONG == 32
3523 tmpdiv = first_block;
3524 (void) do_div(tmpdiv, stripesize);
3525 first_row = r5or6_first_row = r0_first_row = tmpdiv;
3526 tmpdiv = last_block;
3527 (void) do_div(tmpdiv, stripesize);
3528 r5or6_last_row = r0_last_row = tmpdiv;
3529#else
3530 first_row = r5or6_first_row = r0_first_row =
3531 first_block / stripesize;
3532 r5or6_last_row = r0_last_row = last_block / stripesize;
3533#endif
3534 if (r5or6_first_row != r5or6_last_row)
3535 return IO_ACCEL_INELIGIBLE;
3536
3537
3538 /* Verify request is in a single column */
3539#if BITS_PER_LONG == 32
3540 tmpdiv = first_block;
3541 first_row_offset = do_div(tmpdiv, stripesize);
3542 tmpdiv = first_row_offset;
3543 first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
3544 r5or6_first_row_offset = first_row_offset;
3545 tmpdiv = last_block;
3546 r5or6_last_row_offset = do_div(tmpdiv, stripesize);
3547 tmpdiv = r5or6_last_row_offset;
3548 r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
3549 tmpdiv = r5or6_first_row_offset;
3550 (void) do_div(tmpdiv, map->strip_size);
3551 first_column = r5or6_first_column = tmpdiv;
3552 tmpdiv = r5or6_last_row_offset;
3553 (void) do_div(tmpdiv, map->strip_size);
3554 r5or6_last_column = tmpdiv;
3555#else
3556 first_row_offset = r5or6_first_row_offset =
3557 (u32)((first_block % stripesize) %
3558 r5or6_blocks_per_row);
3559
3560 r5or6_last_row_offset =
3561 (u32)((last_block % stripesize) %
3562 r5or6_blocks_per_row);
3563
3564 first_column = r5or6_first_column =
3565 r5or6_first_row_offset / map->strip_size;
3566 r5or6_last_column =
3567 r5or6_last_row_offset / map->strip_size;
3568#endif
3569 if (r5or6_first_column != r5or6_last_column)
3570 return IO_ACCEL_INELIGIBLE;
3571
3572 /* Request is eligible */
3573 map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
3574 map->row_cnt;
3575
3576 map_index = (first_group *
3577 (map->row_cnt * total_disks_per_row)) +
3578 (map_row * total_disks_per_row) + first_column;
3579 break;
3580 default:
3581 return IO_ACCEL_INELIGIBLE;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003582 }
Scott Teel6b80b182014-02-18 13:56:55 -06003583
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003584 disk_handle = dd[map_index].ioaccel_handle;
3585 disk_block = map->disk_starting_blk + (first_row * map->strip_size) +
3586 (first_row_offset - (first_column * map->strip_size));
3587 disk_block_cnt = block_cnt;
3588
3589 /* handle differing logical/physical block sizes */
3590 if (map->phys_blk_shift) {
3591 disk_block <<= map->phys_blk_shift;
3592 disk_block_cnt <<= map->phys_blk_shift;
3593 }
3594 BUG_ON(disk_block_cnt > 0xffff);
3595
3596 /* build the new CDB for the physical disk I/O */
3597 if (disk_block > 0xffffffff) {
3598 cdb[0] = is_write ? WRITE_16 : READ_16;
3599 cdb[1] = 0;
3600 cdb[2] = (u8) (disk_block >> 56);
3601 cdb[3] = (u8) (disk_block >> 48);
3602 cdb[4] = (u8) (disk_block >> 40);
3603 cdb[5] = (u8) (disk_block >> 32);
3604 cdb[6] = (u8) (disk_block >> 24);
3605 cdb[7] = (u8) (disk_block >> 16);
3606 cdb[8] = (u8) (disk_block >> 8);
3607 cdb[9] = (u8) (disk_block);
3608 cdb[10] = (u8) (disk_block_cnt >> 24);
3609 cdb[11] = (u8) (disk_block_cnt >> 16);
3610 cdb[12] = (u8) (disk_block_cnt >> 8);
3611 cdb[13] = (u8) (disk_block_cnt);
3612 cdb[14] = 0;
3613 cdb[15] = 0;
3614 cdb_len = 16;
3615 } else {
3616 cdb[0] = is_write ? WRITE_10 : READ_10;
3617 cdb[1] = 0;
3618 cdb[2] = (u8) (disk_block >> 24);
3619 cdb[3] = (u8) (disk_block >> 16);
3620 cdb[4] = (u8) (disk_block >> 8);
3621 cdb[5] = (u8) (disk_block);
3622 cdb[6] = 0;
3623 cdb[7] = (u8) (disk_block_cnt >> 8);
3624 cdb[8] = (u8) (disk_block_cnt);
3625 cdb[9] = 0;
3626 cdb_len = 10;
3627 }
3628 return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
3629 dev->scsi3addr);
3630}
3631
Jeff Garzikf2812332010-11-16 02:10:29 -05003632static int hpsa_scsi_queue_command_lck(struct scsi_cmnd *cmd,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003633 void (*done)(struct scsi_cmnd *))
3634{
3635 struct ctlr_info *h;
3636 struct hpsa_scsi_dev_t *dev;
3637 unsigned char scsi3addr[8];
3638 struct CommandList *c;
3639 unsigned long flags;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003640 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003641
3642 /* Get the ptr to our adapter structure out of cmd->host. */
3643 h = sdev_to_hba(cmd->device);
3644 dev = cmd->device->hostdata;
3645 if (!dev) {
3646 cmd->result = DID_NO_CONNECT << 16;
3647 done(cmd);
3648 return 0;
3649 }
3650 memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
3651
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003652 spin_lock_irqsave(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05003653 if (unlikely(h->lockup_detected)) {
3654 spin_unlock_irqrestore(&h->lock, flags);
3655 cmd->result = DID_ERROR << 16;
3656 done(cmd);
3657 return 0;
3658 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003659 spin_unlock_irqrestore(&h->lock, flags);
Matt Gatese16a33a2012-05-01 11:43:11 -05003660 c = cmd_alloc(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003661 if (c == NULL) { /* trouble... */
3662 dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
3663 return SCSI_MLQUEUE_HOST_BUSY;
3664 }
3665
3666 /* Fill in the command list header */
3667
3668 cmd->scsi_done = done; /* save this for use by completion code */
3669
3670 /* save c in case we have to abort it */
3671 cmd->host_scribble = (unsigned char *) c;
3672
3673 c->cmd_type = CMD_SCSI;
3674 c->scsi_cmd = cmd;
Matt Gatese1f7de02014-02-18 13:55:17 -06003675
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003676 /* Call alternate submit routine for I/O accelerated commands.
3677 * Retries always go down the normal I/O path.
3678 */
3679 if (likely(cmd->retries == 0 &&
Scott Teelda0697b2014-02-18 13:57:00 -06003680 cmd->request->cmd_type == REQ_TYPE_FS &&
3681 h->acciopath_status)) {
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06003682 if (dev->offload_enabled) {
3683 rc = hpsa_scsi_ioaccel_raid_map(h, c);
3684 if (rc == 0)
3685 return 0; /* Sent on ioaccel path */
3686 if (rc < 0) { /* scsi_dma_map failed. */
3687 cmd_free(h, c);
3688 return SCSI_MLQUEUE_HOST_BUSY;
3689 }
3690 } else if (dev->ioaccel_handle) {
3691 rc = hpsa_scsi_ioaccel_direct_map(h, c);
3692 if (rc == 0)
3693 return 0; /* Sent on direct map path */
3694 if (rc < 0) { /* scsi_dma_map failed. */
3695 cmd_free(h, c);
3696 return SCSI_MLQUEUE_HOST_BUSY;
3697 }
3698 }
3699 }
Matt Gatese1f7de02014-02-18 13:55:17 -06003700
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003701 c->Header.ReplyQueue = 0; /* unused in simple mode */
3702 memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
Don Brace303932f2010-02-04 08:42:40 -06003703 c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT);
3704 c->Header.Tag.lower |= DIRECT_LOOKUP_BIT;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003705
3706 /* Fill in the request block... */
3707
3708 c->Request.Timeout = 0;
3709 memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
3710 BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
3711 c->Request.CDBLen = cmd->cmd_len;
3712 memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
3713 c->Request.Type.Type = TYPE_CMD;
3714 c->Request.Type.Attribute = ATTR_SIMPLE;
3715 switch (cmd->sc_data_direction) {
3716 case DMA_TO_DEVICE:
3717 c->Request.Type.Direction = XFER_WRITE;
3718 break;
3719 case DMA_FROM_DEVICE:
3720 c->Request.Type.Direction = XFER_READ;
3721 break;
3722 case DMA_NONE:
3723 c->Request.Type.Direction = XFER_NONE;
3724 break;
3725 case DMA_BIDIRECTIONAL:
3726 /* This can happen if a buggy application does a scsi passthru
3727 * and sets both inlen and outlen to non-zero. ( see
3728 * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
3729 */
3730
3731 c->Request.Type.Direction = XFER_RSVD;
3732 /* This is technically wrong, and hpsa controllers should
3733 * reject it with CMD_INVALID, which is the most correct
3734 * response, but non-fibre backends appear to let it
3735 * slide by, and give the same results as if this field
3736 * were set correctly. Either way is acceptable for
3737 * our purposes here.
3738 */
3739
3740 break;
3741
3742 default:
3743 dev_err(&h->pdev->dev, "unknown data direction: %d\n",
3744 cmd->sc_data_direction);
3745 BUG();
3746 break;
3747 }
3748
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06003749 if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003750 cmd_free(h, c);
3751 return SCSI_MLQUEUE_HOST_BUSY;
3752 }
3753 enqueue_cmd_and_start_io(h, c);
3754 /* the cmd'll come back via intr handler in complete_scsi_command() */
3755 return 0;
3756}
3757
Jeff Garzikf2812332010-11-16 02:10:29 -05003758static DEF_SCSI_QCMD(hpsa_scsi_queue_command)
3759
Stephen M. Cameron5f389362014-02-18 13:55:48 -06003760static int do_not_scan_if_controller_locked_up(struct ctlr_info *h)
3761{
3762 unsigned long flags;
3763
3764 /*
3765 * Don't let rescans be initiated on a controller known
3766 * to be locked up. If the controller locks up *during*
3767 * a rescan, that thread is probably hosed, but at least
3768 * we can prevent new rescan threads from piling up on a
3769 * locked up controller.
3770 */
3771 spin_lock_irqsave(&h->lock, flags);
3772 if (unlikely(h->lockup_detected)) {
3773 spin_unlock_irqrestore(&h->lock, flags);
3774 spin_lock_irqsave(&h->scan_lock, flags);
3775 h->scan_finished = 1;
3776 wake_up_all(&h->scan_wait_queue);
3777 spin_unlock_irqrestore(&h->scan_lock, flags);
3778 return 1;
3779 }
3780 spin_unlock_irqrestore(&h->lock, flags);
3781 return 0;
3782}
3783
Stephen M. Camerona08a8472010-02-04 08:43:16 -06003784static void hpsa_scan_start(struct Scsi_Host *sh)
3785{
3786 struct ctlr_info *h = shost_to_hba(sh);
3787 unsigned long flags;
3788
Stephen M. Cameron5f389362014-02-18 13:55:48 -06003789 if (do_not_scan_if_controller_locked_up(h))
3790 return;
3791
Stephen M. Camerona08a8472010-02-04 08:43:16 -06003792 /* wait until any scan already in progress is finished. */
3793 while (1) {
3794 spin_lock_irqsave(&h->scan_lock, flags);
3795 if (h->scan_finished)
3796 break;
3797 spin_unlock_irqrestore(&h->scan_lock, flags);
3798 wait_event(h->scan_wait_queue, h->scan_finished);
3799 /* Note: We don't need to worry about a race between this
3800 * thread and driver unload because the midlayer will
3801 * have incremented the reference count, so unload won't
3802 * happen if we're in here.
3803 */
3804 }
3805 h->scan_finished = 0; /* mark scan as in progress */
3806 spin_unlock_irqrestore(&h->scan_lock, flags);
3807
Stephen M. Cameron5f389362014-02-18 13:55:48 -06003808 if (do_not_scan_if_controller_locked_up(h))
3809 return;
3810
Stephen M. Camerona08a8472010-02-04 08:43:16 -06003811 hpsa_update_scsi_devices(h, h->scsi_host->host_no);
3812
3813 spin_lock_irqsave(&h->scan_lock, flags);
3814 h->scan_finished = 1; /* mark scan as finished. */
3815 wake_up_all(&h->scan_wait_queue);
3816 spin_unlock_irqrestore(&h->scan_lock, flags);
3817}
3818
3819static int hpsa_scan_finished(struct Scsi_Host *sh,
3820 unsigned long elapsed_time)
3821{
3822 struct ctlr_info *h = shost_to_hba(sh);
3823 unsigned long flags;
3824 int finished;
3825
3826 spin_lock_irqsave(&h->scan_lock, flags);
3827 finished = h->scan_finished;
3828 spin_unlock_irqrestore(&h->scan_lock, flags);
3829 return finished;
3830}
3831
Stephen M. Cameron667e23d2010-02-25 14:02:51 -06003832static int hpsa_change_queue_depth(struct scsi_device *sdev,
3833 int qdepth, int reason)
3834{
3835 struct ctlr_info *h = sdev_to_hba(sdev);
3836
3837 if (reason != SCSI_QDEPTH_DEFAULT)
3838 return -ENOTSUPP;
3839
3840 if (qdepth < 1)
3841 qdepth = 1;
3842 else
3843 if (qdepth > h->nr_cmds)
3844 qdepth = h->nr_cmds;
3845 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
3846 return sdev->queue_depth;
3847}
3848
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003849static void hpsa_unregister_scsi(struct ctlr_info *h)
3850{
3851 /* we are being forcibly unloaded, and may not refuse. */
3852 scsi_remove_host(h->scsi_host);
3853 scsi_host_put(h->scsi_host);
3854 h->scsi_host = NULL;
3855}
3856
3857static int hpsa_register_scsi(struct ctlr_info *h)
3858{
Stephen M. Cameronb7056902012-01-19 14:00:53 -06003859 struct Scsi_Host *sh;
3860 int error;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003861
Stephen M. Cameronb7056902012-01-19 14:00:53 -06003862 sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
3863 if (sh == NULL)
3864 goto fail;
3865
3866 sh->io_port = 0;
3867 sh->n_io_port = 0;
3868 sh->this_id = -1;
3869 sh->max_channel = 3;
3870 sh->max_cmd_len = MAX_COMMAND_SIZE;
3871 sh->max_lun = HPSA_MAX_LUN;
3872 sh->max_id = HPSA_MAX_LUN;
3873 sh->can_queue = h->nr_cmds;
3874 sh->cmd_per_lun = h->nr_cmds;
3875 sh->sg_tablesize = h->maxsgentries;
3876 h->scsi_host = sh;
3877 sh->hostdata[0] = (unsigned long) h;
3878 sh->irq = h->intr[h->intr_mode];
3879 sh->unique_id = sh->irq;
3880 error = scsi_add_host(sh, &h->pdev->dev);
3881 if (error)
3882 goto fail_host_put;
3883 scsi_scan_host(sh);
3884 return 0;
3885
3886 fail_host_put:
3887 dev_err(&h->pdev->dev, "%s: scsi_add_host"
3888 " failed for controller %d\n", __func__, h->ctlr);
3889 scsi_host_put(sh);
3890 return error;
3891 fail:
3892 dev_err(&h->pdev->dev, "%s: scsi_host_alloc"
3893 " failed for controller %d\n", __func__, h->ctlr);
3894 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003895}
3896
3897static int wait_for_device_to_become_ready(struct ctlr_info *h,
3898 unsigned char lunaddr[])
3899{
3900 int rc = 0;
3901 int count = 0;
3902 int waittime = 1; /* seconds */
3903 struct CommandList *c;
3904
3905 c = cmd_special_alloc(h);
3906 if (!c) {
3907 dev_warn(&h->pdev->dev, "out of memory in "
3908 "wait_for_device_to_become_ready.\n");
3909 return IO_ERROR;
3910 }
3911
3912 /* Send test unit ready until device ready, or give up. */
3913 while (count < HPSA_TUR_RETRY_LIMIT) {
3914
3915 /* Wait for a bit. do this first, because if we send
3916 * the TUR right away, the reset will just abort it.
3917 */
3918 msleep(1000 * waittime);
3919 count++;
3920
3921 /* Increase wait time with each try, up to a point. */
3922 if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
3923 waittime = waittime * 2;
3924
Stephen M. Camerona2dac132013-02-20 11:24:41 -06003925 /* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
3926 (void) fill_cmd(c, TEST_UNIT_READY, h,
3927 NULL, 0, 0, lunaddr, TYPE_CMD);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003928 hpsa_scsi_do_simple_cmd_core(h, c);
3929 /* no unmap needed here because no data xfer. */
3930
3931 if (c->err_info->CommandStatus == CMD_SUCCESS)
3932 break;
3933
3934 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
3935 c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
3936 (c->err_info->SenseInfo[2] == NO_SENSE ||
3937 c->err_info->SenseInfo[2] == UNIT_ATTENTION))
3938 break;
3939
3940 dev_warn(&h->pdev->dev, "waiting %d secs "
3941 "for device to become ready.\n", waittime);
3942 rc = 1; /* device not ready. */
3943 }
3944
3945 if (rc)
3946 dev_warn(&h->pdev->dev, "giving up on device.\n");
3947 else
3948 dev_warn(&h->pdev->dev, "device is ready.\n");
3949
3950 cmd_special_free(h, c);
3951 return rc;
3952}
3953
3954/* Need at least one of these error handlers to keep ../scsi/hosts.c from
3955 * complaining. Doing a host- or bus-reset can't do anything good here.
3956 */
3957static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
3958{
3959 int rc;
3960 struct ctlr_info *h;
3961 struct hpsa_scsi_dev_t *dev;
3962
3963 /* find the controller to which the command to be aborted was sent */
3964 h = sdev_to_hba(scsicmd->device);
3965 if (h == NULL) /* paranoia */
3966 return FAILED;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003967 dev = scsicmd->device->hostdata;
3968 if (!dev) {
3969 dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
3970 "device lookup failed.\n");
3971 return FAILED;
3972 }
Stephen M. Camerond416b0c2010-02-04 08:43:21 -06003973 dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
3974 h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003975 /* send a reset to the SCSI LUN which the command was sent to */
Scott Teelbf711ac2014-02-18 13:56:39 -06003976 rc = hpsa_send_reset(h, dev->scsi3addr, HPSA_RESET_TYPE_LUN);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08003977 if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
3978 return SUCCESS;
3979
3980 dev_warn(&h->pdev->dev, "resetting device failed.\n");
3981 return FAILED;
3982}
3983
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05003984static void swizzle_abort_tag(u8 *tag)
3985{
3986 u8 original_tag[8];
3987
3988 memcpy(original_tag, tag, 8);
3989 tag[0] = original_tag[3];
3990 tag[1] = original_tag[2];
3991 tag[2] = original_tag[1];
3992 tag[3] = original_tag[0];
3993 tag[4] = original_tag[7];
3994 tag[5] = original_tag[6];
3995 tag[6] = original_tag[5];
3996 tag[7] = original_tag[4];
3997}
3998
Scott Teel17eb87d2014-02-18 13:55:28 -06003999static void hpsa_get_tag(struct ctlr_info *h,
4000 struct CommandList *c, u32 *taglower, u32 *tagupper)
4001{
4002 if (c->cmd_type == CMD_IOACCEL1) {
4003 struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
4004 &h->ioaccel_cmd_pool[c->cmdindex];
4005 *tagupper = cm1->Tag.upper;
4006 *taglower = cm1->Tag.lower;
Scott Teel54b6e9e2014-02-18 13:56:45 -06004007 return;
Scott Teel17eb87d2014-02-18 13:55:28 -06004008 }
Scott Teel54b6e9e2014-02-18 13:56:45 -06004009 if (c->cmd_type == CMD_IOACCEL2) {
4010 struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
4011 &h->ioaccel2_cmd_pool[c->cmdindex];
Scott Teeldd0e19f2014-02-18 13:57:31 -06004012 /* upper tag not used in ioaccel2 mode */
4013 memset(tagupper, 0, sizeof(*tagupper));
4014 *taglower = cm2->Tag;
Scott Teel54b6e9e2014-02-18 13:56:45 -06004015 return;
4016 }
4017 *tagupper = c->Header.Tag.upper;
4018 *taglower = c->Header.Tag.lower;
Scott Teel17eb87d2014-02-18 13:55:28 -06004019}
4020
Scott Teel54b6e9e2014-02-18 13:56:45 -06004021
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004022static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05004023 struct CommandList *abort, int swizzle)
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004024{
4025 int rc = IO_OK;
4026 struct CommandList *c;
4027 struct ErrorInfo *ei;
Scott Teel17eb87d2014-02-18 13:55:28 -06004028 u32 tagupper, taglower;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004029
4030 c = cmd_special_alloc(h);
4031 if (c == NULL) { /* trouble... */
4032 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
4033 return -ENOMEM;
4034 }
4035
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004036 /* fill_cmd can't fail here, no buffer to map */
4037 (void) fill_cmd(c, HPSA_ABORT_MSG, h, abort,
4038 0, 0, scsi3addr, TYPE_MSG);
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05004039 if (swizzle)
4040 swizzle_abort_tag(&c->Request.CDB[4]);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004041 hpsa_scsi_do_simple_cmd_core(h, c);
Scott Teel17eb87d2014-02-18 13:55:28 -06004042 hpsa_get_tag(h, abort, &taglower, &tagupper);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004043 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd_core completed.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06004044 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004045 /* no unmap needed here because no data xfer. */
4046
4047 ei = c->err_info;
4048 switch (ei->CommandStatus) {
4049 case CMD_SUCCESS:
4050 break;
4051 case CMD_UNABORTABLE: /* Very common, don't make noise. */
4052 rc = -1;
4053 break;
4054 default:
4055 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
Scott Teel17eb87d2014-02-18 13:55:28 -06004056 __func__, tagupper, taglower);
Stephen M. Camerond1e8bea2014-02-18 13:57:47 -06004057 hpsa_scsi_interpret_error(h, c);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004058 rc = -1;
4059 break;
4060 }
4061 cmd_special_free(h, c);
Scott Teeldd0e19f2014-02-18 13:57:31 -06004062 dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n",
4063 __func__, tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004064 return rc;
4065}
4066
4067/*
4068 * hpsa_find_cmd_in_queue
4069 *
4070 * Used to determine whether a command (find) is still present
4071 * in queue_head. Optionally excludes the last element of queue_head.
4072 *
4073 * This is used to avoid unnecessary aborts. Commands in h->reqQ have
4074 * not yet been submitted, and so can be aborted by the driver without
4075 * sending an abort to the hardware.
4076 *
4077 * Returns pointer to command if found in queue, NULL otherwise.
4078 */
4079static struct CommandList *hpsa_find_cmd_in_queue(struct ctlr_info *h,
4080 struct scsi_cmnd *find, struct list_head *queue_head)
4081{
4082 unsigned long flags;
4083 struct CommandList *c = NULL; /* ptr into cmpQ */
4084
4085 if (!find)
4086 return 0;
4087 spin_lock_irqsave(&h->lock, flags);
4088 list_for_each_entry(c, queue_head, list) {
4089 if (c->scsi_cmd == NULL) /* e.g.: passthru ioctl */
4090 continue;
4091 if (c->scsi_cmd == find) {
4092 spin_unlock_irqrestore(&h->lock, flags);
4093 return c;
4094 }
4095 }
4096 spin_unlock_irqrestore(&h->lock, flags);
4097 return NULL;
4098}
4099
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05004100static struct CommandList *hpsa_find_cmd_in_queue_by_tag(struct ctlr_info *h,
4101 u8 *tag, struct list_head *queue_head)
4102{
4103 unsigned long flags;
4104 struct CommandList *c;
4105
4106 spin_lock_irqsave(&h->lock, flags);
4107 list_for_each_entry(c, queue_head, list) {
4108 if (memcmp(&c->Header.Tag, tag, 8) != 0)
4109 continue;
4110 spin_unlock_irqrestore(&h->lock, flags);
4111 return c;
4112 }
4113 spin_unlock_irqrestore(&h->lock, flags);
4114 return NULL;
4115}
4116
Scott Teel54b6e9e2014-02-18 13:56:45 -06004117/* ioaccel2 path firmware cannot handle abort task requests.
4118 * Change abort requests to physical target reset, and send to the
4119 * address of the physical disk used for the ioaccel 2 command.
4120 * Return 0 on success (IO_OK)
4121 * -1 on failure
4122 */
4123
4124static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
4125 unsigned char *scsi3addr, struct CommandList *abort)
4126{
4127 int rc = IO_OK;
4128 struct scsi_cmnd *scmd; /* scsi command within request being aborted */
4129 struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
4130 unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
4131 unsigned char *psa = &phys_scsi3addr[0];
4132
4133 /* Get a pointer to the hpsa logical device. */
4134 scmd = (struct scsi_cmnd *) abort->scsi_cmd;
4135 dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
4136 if (dev == NULL) {
4137 dev_warn(&h->pdev->dev,
4138 "Cannot abort: no device pointer for command.\n");
4139 return -1; /* not abortable */
4140 }
4141
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06004142 if (h->raid_offload_debug > 0)
4143 dev_info(&h->pdev->dev,
4144 "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",
4145 h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
4146 scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
4147 scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]);
4148
Scott Teel54b6e9e2014-02-18 13:56:45 -06004149 if (!dev->offload_enabled) {
4150 dev_warn(&h->pdev->dev,
4151 "Can't abort: device is not operating in HP SSD Smart Path mode.\n");
4152 return -1; /* not abortable */
4153 }
4154
4155 /* Incoming scsi3addr is logical addr. We need physical disk addr. */
4156 if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
4157 dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
4158 return -1; /* not abortable */
4159 }
4160
4161 /* send the reset */
Stephen M. Cameron2ba8bfc2014-02-18 13:57:52 -06004162 if (h->raid_offload_debug > 0)
4163 dev_info(&h->pdev->dev,
4164 "Reset as abort: Resetting physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
4165 psa[0], psa[1], psa[2], psa[3],
4166 psa[4], psa[5], psa[6], psa[7]);
Scott Teel54b6e9e2014-02-18 13:56:45 -06004167 rc = hpsa_send_reset(h, psa, HPSA_RESET_TYPE_TARGET);
4168 if (rc != 0) {
4169 dev_warn(&h->pdev->dev,
4170 "Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
4171 psa[0], psa[1], psa[2], psa[3],
4172 psa[4], psa[5], psa[6], psa[7]);
4173 return rc; /* failed to reset */
4174 }
4175
4176 /* wait for device to recover */
4177 if (wait_for_device_to_become_ready(h, psa) != 0) {
4178 dev_warn(&h->pdev->dev,
4179 "Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
4180 psa[0], psa[1], psa[2], psa[3],
4181 psa[4], psa[5], psa[6], psa[7]);
4182 return -1; /* failed to recover */
4183 }
4184
4185 /* device recovered */
4186 dev_info(&h->pdev->dev,
4187 "Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
4188 psa[0], psa[1], psa[2], psa[3],
4189 psa[4], psa[5], psa[6], psa[7]);
4190
4191 return rc; /* success */
4192}
4193
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05004194/* Some Smart Arrays need the abort tag swizzled, and some don't. It's hard to
4195 * tell which kind we're dealing with, so we send the abort both ways. There
4196 * shouldn't be any collisions between swizzled and unswizzled tags due to the
4197 * way we construct our tags but we check anyway in case the assumptions which
4198 * make this true someday become false.
4199 */
4200static int hpsa_send_abort_both_ways(struct ctlr_info *h,
4201 unsigned char *scsi3addr, struct CommandList *abort)
4202{
4203 u8 swizzled_tag[8];
4204 struct CommandList *c;
4205 int rc = 0, rc2 = 0;
4206
Scott Teel54b6e9e2014-02-18 13:56:45 -06004207 /* ioccelerator mode 2 commands should be aborted via the
4208 * accelerated path, since RAID path is unaware of these commands,
4209 * but underlying firmware can't handle abort TMF.
4210 * Change abort to physical device reset.
4211 */
4212 if (abort->cmd_type == CMD_IOACCEL2)
4213 return hpsa_send_reset_as_abort_ioaccel2(h, scsi3addr, abort);
4214
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05004215 /* we do not expect to find the swizzled tag in our queue, but
4216 * check anyway just to be sure the assumptions which make this
4217 * the case haven't become wrong.
4218 */
4219 memcpy(swizzled_tag, &abort->Request.CDB[4], 8);
4220 swizzle_abort_tag(swizzled_tag);
4221 c = hpsa_find_cmd_in_queue_by_tag(h, swizzled_tag, &h->cmpQ);
4222 if (c != NULL) {
4223 dev_warn(&h->pdev->dev, "Unexpectedly found byte-swapped tag in completion queue.\n");
4224 return hpsa_send_abort(h, scsi3addr, abort, 0);
4225 }
4226 rc = hpsa_send_abort(h, scsi3addr, abort, 0);
4227
4228 /* if the command is still in our queue, we can't conclude that it was
4229 * aborted (it might have just completed normally) but in any case
4230 * we don't need to try to abort it another way.
4231 */
4232 c = hpsa_find_cmd_in_queue(h, abort->scsi_cmd, &h->cmpQ);
4233 if (c)
4234 rc2 = hpsa_send_abort(h, scsi3addr, abort, 1);
4235 return rc && rc2;
4236}
4237
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004238/* Send an abort for the specified command.
4239 * If the device and controller support it,
4240 * send a task abort request.
4241 */
4242static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
4243{
4244
4245 int i, rc;
4246 struct ctlr_info *h;
4247 struct hpsa_scsi_dev_t *dev;
4248 struct CommandList *abort; /* pointer to command to be aborted */
4249 struct CommandList *found;
4250 struct scsi_cmnd *as; /* ptr to scsi cmd inside aborted command. */
4251 char msg[256]; /* For debug messaging. */
4252 int ml = 0;
Scott Teel17eb87d2014-02-18 13:55:28 -06004253 u32 tagupper, taglower;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004254
4255 /* Find the controller of the command to be aborted */
4256 h = sdev_to_hba(sc->device);
4257 if (WARN(h == NULL,
4258 "ABORT REQUEST FAILED, Controller lookup failed.\n"))
4259 return FAILED;
4260
4261 /* Check that controller supports some kind of task abort */
4262 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
4263 !(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
4264 return FAILED;
4265
4266 memset(msg, 0, sizeof(msg));
4267 ml += sprintf(msg+ml, "ABORT REQUEST on C%d:B%d:T%d:L%d ",
4268 h->scsi_host->host_no, sc->device->channel,
4269 sc->device->id, sc->device->lun);
4270
4271 /* Find the device of the command to be aborted */
4272 dev = sc->device->hostdata;
4273 if (!dev) {
4274 dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
4275 msg);
4276 return FAILED;
4277 }
4278
4279 /* Get SCSI command to be aborted */
4280 abort = (struct CommandList *) sc->host_scribble;
4281 if (abort == NULL) {
4282 dev_err(&h->pdev->dev, "%s FAILED, Command to abort is NULL.\n",
4283 msg);
4284 return FAILED;
4285 }
Scott Teel17eb87d2014-02-18 13:55:28 -06004286 hpsa_get_tag(h, abort, &taglower, &tagupper);
4287 ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004288 as = (struct scsi_cmnd *) abort->scsi_cmd;
4289 if (as != NULL)
4290 ml += sprintf(msg+ml, "Command:0x%x SN:0x%lx ",
4291 as->cmnd[0], as->serial_number);
4292 dev_dbg(&h->pdev->dev, "%s\n", msg);
4293 dev_warn(&h->pdev->dev, "Abort request on C%d:B%d:T%d:L%d\n",
4294 h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
4295
4296 /* Search reqQ to See if command is queued but not submitted,
4297 * if so, complete the command with aborted status and remove
4298 * it from the reqQ.
4299 */
4300 found = hpsa_find_cmd_in_queue(h, sc, &h->reqQ);
4301 if (found) {
4302 found->err_info->CommandStatus = CMD_ABORTED;
4303 finish_cmd(found);
4304 dev_info(&h->pdev->dev, "%s Request SUCCEEDED (driver queue).\n",
4305 msg);
4306 return SUCCESS;
4307 }
4308
4309 /* not in reqQ, if also not in cmpQ, must have already completed */
4310 found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
4311 if (!found) {
Stephen M. Camerond6ebd0f2012-07-26 11:34:17 -05004312 dev_dbg(&h->pdev->dev, "%s Request SUCCEEDED (not known to driver).\n",
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004313 msg);
4314 return SUCCESS;
4315 }
4316
4317 /*
4318 * Command is in flight, or possibly already completed
4319 * by the firmware (but not to the scsi mid layer) but we can't
4320 * distinguish which. Send the abort down.
4321 */
Stephen M. Cameron6cba3f12012-05-01 11:42:56 -05004322 rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort);
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004323 if (rc != 0) {
4324 dev_dbg(&h->pdev->dev, "%s Request FAILED.\n", msg);
4325 dev_warn(&h->pdev->dev, "FAILED abort on device C%d:B%d:T%d:L%d\n",
4326 h->scsi_host->host_no,
4327 dev->bus, dev->target, dev->lun);
4328 return FAILED;
4329 }
4330 dev_info(&h->pdev->dev, "%s REQUEST SUCCEEDED.\n", msg);
4331
4332 /* If the abort(s) above completed and actually aborted the
4333 * command, then the command to be aborted should already be
4334 * completed. If not, wait around a bit more to see if they
4335 * manage to complete normally.
4336 */
4337#define ABORT_COMPLETE_WAIT_SECS 30
4338 for (i = 0; i < ABORT_COMPLETE_WAIT_SECS * 10; i++) {
4339 found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
4340 if (!found)
4341 return SUCCESS;
4342 msleep(100);
4343 }
4344 dev_warn(&h->pdev->dev, "%s FAILED. Aborted command has not completed after %d seconds.\n",
4345 msg, ABORT_COMPLETE_WAIT_SECS);
4346 return FAILED;
4347}
4348
4349
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004350/*
4351 * For operations that cannot sleep, a command block is allocated at init,
4352 * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
4353 * which ones are free or in use. Lock must be held when calling this.
4354 * cmd_free() is the complement.
4355 */
4356static struct CommandList *cmd_alloc(struct ctlr_info *h)
4357{
4358 struct CommandList *c;
4359 int i;
4360 union u64bit temp64;
4361 dma_addr_t cmd_dma_handle, err_dma_handle;
Matt Gatese16a33a2012-05-01 11:43:11 -05004362 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004363
Matt Gatese16a33a2012-05-01 11:43:11 -05004364 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004365 do {
4366 i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
Matt Gatese16a33a2012-05-01 11:43:11 -05004367 if (i == h->nr_cmds) {
4368 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004369 return NULL;
Matt Gatese16a33a2012-05-01 11:43:11 -05004370 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004371 } while (test_and_set_bit
4372 (i & (BITS_PER_LONG - 1),
4373 h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
Matt Gatese16a33a2012-05-01 11:43:11 -05004374 spin_unlock_irqrestore(&h->lock, flags);
4375
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004376 c = h->cmd_pool + i;
4377 memset(c, 0, sizeof(*c));
4378 cmd_dma_handle = h->cmd_pool_dhandle
4379 + i * sizeof(*c);
4380 c->err_info = h->errinfo_pool + i;
4381 memset(c->err_info, 0, sizeof(*c->err_info));
4382 err_dma_handle = h->errinfo_pool_dhandle
4383 + i * sizeof(*c->err_info);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004384
4385 c->cmdindex = i;
4386
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06004387 INIT_LIST_HEAD(&c->list);
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004388 c->busaddr = (u32) cmd_dma_handle;
4389 temp64.val = (u64) err_dma_handle;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004390 c->ErrDesc.Addr.lower = temp64.val32.lower;
4391 c->ErrDesc.Addr.upper = temp64.val32.upper;
4392 c->ErrDesc.Len = sizeof(*c->err_info);
4393
4394 c->h = h;
4395 return c;
4396}
4397
4398/* For operations that can wait for kmalloc to possibly sleep,
4399 * this routine can be called. Lock need not be held to call
4400 * cmd_special_alloc. cmd_special_free() is the complement.
4401 */
4402static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
4403{
4404 struct CommandList *c;
4405 union u64bit temp64;
4406 dma_addr_t cmd_dma_handle, err_dma_handle;
4407
4408 c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
4409 if (c == NULL)
4410 return NULL;
4411 memset(c, 0, sizeof(*c));
4412
Matt Gatese1f7de02014-02-18 13:55:17 -06004413 c->cmd_type = CMD_SCSI;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004414 c->cmdindex = -1;
4415
4416 c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info),
4417 &err_dma_handle);
4418
4419 if (c->err_info == NULL) {
4420 pci_free_consistent(h->pdev,
4421 sizeof(*c), c, cmd_dma_handle);
4422 return NULL;
4423 }
4424 memset(c->err_info, 0, sizeof(*c->err_info));
4425
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06004426 INIT_LIST_HEAD(&c->list);
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004427 c->busaddr = (u32) cmd_dma_handle;
4428 temp64.val = (u64) err_dma_handle;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004429 c->ErrDesc.Addr.lower = temp64.val32.lower;
4430 c->ErrDesc.Addr.upper = temp64.val32.upper;
4431 c->ErrDesc.Len = sizeof(*c->err_info);
4432
4433 c->h = h;
4434 return c;
4435}
4436
4437static void cmd_free(struct ctlr_info *h, struct CommandList *c)
4438{
4439 int i;
Matt Gatese16a33a2012-05-01 11:43:11 -05004440 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004441
4442 i = c - h->cmd_pool;
Matt Gatese16a33a2012-05-01 11:43:11 -05004443 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004444 clear_bit(i & (BITS_PER_LONG - 1),
4445 h->cmd_pool_bits + (i / BITS_PER_LONG));
Matt Gatese16a33a2012-05-01 11:43:11 -05004446 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004447}
4448
4449static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
4450{
4451 union u64bit temp64;
4452
4453 temp64.val32.lower = c->ErrDesc.Addr.lower;
4454 temp64.val32.upper = c->ErrDesc.Addr.upper;
4455 pci_free_consistent(h->pdev, sizeof(*c->err_info),
4456 c->err_info, (dma_addr_t) temp64.val);
4457 pci_free_consistent(h->pdev, sizeof(*c),
Stephen M. Camerond896f3f2011-01-06 14:47:53 -06004458 c, (dma_addr_t) (c->busaddr & DIRECT_LOOKUP_MASK));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004459}
4460
4461#ifdef CONFIG_COMPAT
4462
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004463static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
4464{
4465 IOCTL32_Command_struct __user *arg32 =
4466 (IOCTL32_Command_struct __user *) arg;
4467 IOCTL_Command_struct arg64;
4468 IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
4469 int err;
4470 u32 cp;
4471
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06004472 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004473 err = 0;
4474 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
4475 sizeof(arg64.LUN_info));
4476 err |= copy_from_user(&arg64.Request, &arg32->Request,
4477 sizeof(arg64.Request));
4478 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
4479 sizeof(arg64.error_info));
4480 err |= get_user(arg64.buf_size, &arg32->buf_size);
4481 err |= get_user(cp, &arg32->buf);
4482 arg64.buf = compat_ptr(cp);
4483 err |= copy_to_user(p, &arg64, sizeof(arg64));
4484
4485 if (err)
4486 return -EFAULT;
4487
Stephen M. Camerone39eeae2010-02-04 08:43:46 -06004488 err = hpsa_ioctl(dev, CCISS_PASSTHRU, (void *)p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004489 if (err)
4490 return err;
4491 err |= copy_in_user(&arg32->error_info, &p->error_info,
4492 sizeof(arg32->error_info));
4493 if (err)
4494 return -EFAULT;
4495 return err;
4496}
4497
4498static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
4499 int cmd, void *arg)
4500{
4501 BIG_IOCTL32_Command_struct __user *arg32 =
4502 (BIG_IOCTL32_Command_struct __user *) arg;
4503 BIG_IOCTL_Command_struct arg64;
4504 BIG_IOCTL_Command_struct __user *p =
4505 compat_alloc_user_space(sizeof(arg64));
4506 int err;
4507 u32 cp;
4508
Vasiliy Kulikov938abd82011-01-07 10:55:53 -06004509 memset(&arg64, 0, sizeof(arg64));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004510 err = 0;
4511 err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
4512 sizeof(arg64.LUN_info));
4513 err |= copy_from_user(&arg64.Request, &arg32->Request,
4514 sizeof(arg64.Request));
4515 err |= copy_from_user(&arg64.error_info, &arg32->error_info,
4516 sizeof(arg64.error_info));
4517 err |= get_user(arg64.buf_size, &arg32->buf_size);
4518 err |= get_user(arg64.malloc_size, &arg32->malloc_size);
4519 err |= get_user(cp, &arg32->buf);
4520 arg64.buf = compat_ptr(cp);
4521 err |= copy_to_user(p, &arg64, sizeof(arg64));
4522
4523 if (err)
4524 return -EFAULT;
4525
Stephen M. Camerone39eeae2010-02-04 08:43:46 -06004526 err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004527 if (err)
4528 return err;
4529 err |= copy_in_user(&arg32->error_info, &p->error_info,
4530 sizeof(arg32->error_info));
4531 if (err)
4532 return -EFAULT;
4533 return err;
4534}
Stephen M. Cameron71fe75a2010-02-04 08:43:51 -06004535
4536static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
4537{
4538 switch (cmd) {
4539 case CCISS_GETPCIINFO:
4540 case CCISS_GETINTINFO:
4541 case CCISS_SETINTINFO:
4542 case CCISS_GETNODENAME:
4543 case CCISS_SETNODENAME:
4544 case CCISS_GETHEARTBEAT:
4545 case CCISS_GETBUSTYPES:
4546 case CCISS_GETFIRMVER:
4547 case CCISS_GETDRIVVER:
4548 case CCISS_REVALIDVOLS:
4549 case CCISS_DEREGDISK:
4550 case CCISS_REGNEWDISK:
4551 case CCISS_REGNEWD:
4552 case CCISS_RESCANDISK:
4553 case CCISS_GETLUNINFO:
4554 return hpsa_ioctl(dev, cmd, arg);
4555
4556 case CCISS_PASSTHRU32:
4557 return hpsa_ioctl32_passthru(dev, cmd, arg);
4558 case CCISS_BIG_PASSTHRU32:
4559 return hpsa_ioctl32_big_passthru(dev, cmd, arg);
4560
4561 default:
4562 return -ENOIOCTLCMD;
4563 }
4564}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004565#endif
4566
4567static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
4568{
4569 struct hpsa_pci_info pciinfo;
4570
4571 if (!argp)
4572 return -EINVAL;
4573 pciinfo.domain = pci_domain_nr(h->pdev->bus);
4574 pciinfo.bus = h->pdev->bus->number;
4575 pciinfo.dev_fn = h->pdev->devfn;
4576 pciinfo.board_id = h->board_id;
4577 if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
4578 return -EFAULT;
4579 return 0;
4580}
4581
4582static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
4583{
4584 DriverVer_type DriverVer;
4585 unsigned char vmaj, vmin, vsubmin;
4586 int rc;
4587
4588 rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
4589 &vmaj, &vmin, &vsubmin);
4590 if (rc != 3) {
4591 dev_info(&h->pdev->dev, "driver version string '%s' "
4592 "unrecognized.", HPSA_DRIVER_VERSION);
4593 vmaj = 0;
4594 vmin = 0;
4595 vsubmin = 0;
4596 }
4597 DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
4598 if (!argp)
4599 return -EINVAL;
4600 if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
4601 return -EFAULT;
4602 return 0;
4603}
4604
4605static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
4606{
4607 IOCTL_Command_struct iocommand;
4608 struct CommandList *c;
4609 char *buff = NULL;
4610 union u64bit temp64;
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004611 int rc = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004612
4613 if (!argp)
4614 return -EINVAL;
4615 if (!capable(CAP_SYS_RAWIO))
4616 return -EPERM;
4617 if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
4618 return -EFAULT;
4619 if ((iocommand.buf_size < 1) &&
4620 (iocommand.Request.Type.Direction != XFER_NONE)) {
4621 return -EINVAL;
4622 }
4623 if (iocommand.buf_size > 0) {
4624 buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
4625 if (buff == NULL)
4626 return -EFAULT;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004627 if (iocommand.Request.Type.Direction == XFER_WRITE) {
4628 /* Copy the data into the buffer we created */
4629 if (copy_from_user(buff, iocommand.buf,
4630 iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004631 rc = -EFAULT;
4632 goto out_kfree;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004633 }
4634 } else {
4635 memset(buff, 0, iocommand.buf_size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004636 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004637 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004638 c = cmd_special_alloc(h);
4639 if (c == NULL) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004640 rc = -ENOMEM;
4641 goto out_kfree;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004642 }
4643 /* Fill in the command type */
4644 c->cmd_type = CMD_IOCTL_PEND;
4645 /* Fill in Command Header */
4646 c->Header.ReplyQueue = 0; /* unused in simple mode */
4647 if (iocommand.buf_size > 0) { /* buffer to fill */
4648 c->Header.SGList = 1;
4649 c->Header.SGTotal = 1;
4650 } else { /* no buffers to fill */
4651 c->Header.SGList = 0;
4652 c->Header.SGTotal = 0;
4653 }
4654 memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
4655 /* use the kernel address the cmd block for tag */
4656 c->Header.Tag.lower = c->busaddr;
4657
4658 /* Fill in Request block */
4659 memcpy(&c->Request, &iocommand.Request,
4660 sizeof(c->Request));
4661
4662 /* Fill in the scatter gather information */
4663 if (iocommand.buf_size > 0) {
4664 temp64.val = pci_map_single(h->pdev, buff,
4665 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06004666 if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
4667 c->SG[0].Addr.lower = 0;
4668 c->SG[0].Addr.upper = 0;
4669 c->SG[0].Len = 0;
4670 rc = -ENOMEM;
4671 goto out;
4672 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004673 c->SG[0].Addr.lower = temp64.val32.lower;
4674 c->SG[0].Addr.upper = temp64.val32.upper;
4675 c->SG[0].Len = iocommand.buf_size;
Matt Gatese1d9cbf2014-02-18 13:55:12 -06004676 c->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining*/
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004677 }
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004678 hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
Stephen M. Cameronc2dd32e2011-06-03 09:57:29 -05004679 if (iocommand.buf_size > 0)
4680 hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004681 check_ioctl_unit_attention(h, c);
4682
4683 /* Copy the error information out */
4684 memcpy(&iocommand.error_info, c->err_info,
4685 sizeof(iocommand.error_info));
4686 if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004687 rc = -EFAULT;
4688 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004689 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004690 if (iocommand.Request.Type.Direction == XFER_READ &&
4691 iocommand.buf_size > 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004692 /* Copy the data out of the buffer we created */
4693 if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004694 rc = -EFAULT;
4695 goto out;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004696 }
4697 }
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004698out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004699 cmd_special_free(h, c);
Stephen M. Cameronc1f63c82013-02-20 11:24:52 -06004700out_kfree:
4701 kfree(buff);
4702 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004703}
4704
4705static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
4706{
4707 BIG_IOCTL_Command_struct *ioc;
4708 struct CommandList *c;
4709 unsigned char **buff = NULL;
4710 int *buff_size = NULL;
4711 union u64bit temp64;
4712 BYTE sg_used = 0;
4713 int status = 0;
4714 int i;
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06004715 u32 left;
4716 u32 sz;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004717 BYTE __user *data_ptr;
4718
4719 if (!argp)
4720 return -EINVAL;
4721 if (!capable(CAP_SYS_RAWIO))
4722 return -EPERM;
4723 ioc = (BIG_IOCTL_Command_struct *)
4724 kmalloc(sizeof(*ioc), GFP_KERNEL);
4725 if (!ioc) {
4726 status = -ENOMEM;
4727 goto cleanup1;
4728 }
4729 if (copy_from_user(ioc, argp, sizeof(*ioc))) {
4730 status = -EFAULT;
4731 goto cleanup1;
4732 }
4733 if ((ioc->buf_size < 1) &&
4734 (ioc->Request.Type.Direction != XFER_NONE)) {
4735 status = -EINVAL;
4736 goto cleanup1;
4737 }
4738 /* Check kmalloc limits using all SGs */
4739 if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
4740 status = -EINVAL;
4741 goto cleanup1;
4742 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06004743 if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004744 status = -EINVAL;
4745 goto cleanup1;
4746 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06004747 buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004748 if (!buff) {
4749 status = -ENOMEM;
4750 goto cleanup1;
4751 }
Stephen M. Camerond66ae082012-01-19 14:00:48 -06004752 buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004753 if (!buff_size) {
4754 status = -ENOMEM;
4755 goto cleanup1;
4756 }
4757 left = ioc->buf_size;
4758 data_ptr = ioc->buf;
4759 while (left) {
4760 sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
4761 buff_size[sg_used] = sz;
4762 buff[sg_used] = kmalloc(sz, GFP_KERNEL);
4763 if (buff[sg_used] == NULL) {
4764 status = -ENOMEM;
4765 goto cleanup1;
4766 }
4767 if (ioc->Request.Type.Direction == XFER_WRITE) {
4768 if (copy_from_user(buff[sg_used], data_ptr, sz)) {
4769 status = -ENOMEM;
4770 goto cleanup1;
4771 }
4772 } else
4773 memset(buff[sg_used], 0, sz);
4774 left -= sz;
4775 data_ptr += sz;
4776 sg_used++;
4777 }
4778 c = cmd_special_alloc(h);
4779 if (c == NULL) {
4780 status = -ENOMEM;
4781 goto cleanup1;
4782 }
4783 c->cmd_type = CMD_IOCTL_PEND;
4784 c->Header.ReplyQueue = 0;
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004785 c->Header.SGList = c->Header.SGTotal = sg_used;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004786 memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
4787 c->Header.Tag.lower = c->busaddr;
4788 memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
4789 if (ioc->buf_size > 0) {
4790 int i;
4791 for (i = 0; i < sg_used; i++) {
4792 temp64.val = pci_map_single(h->pdev, buff[i],
4793 buff_size[i], PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06004794 if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
4795 c->SG[i].Addr.lower = 0;
4796 c->SG[i].Addr.upper = 0;
4797 c->SG[i].Len = 0;
4798 hpsa_pci_unmap(h->pdev, c, i,
4799 PCI_DMA_BIDIRECTIONAL);
4800 status = -ENOMEM;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05004801 goto cleanup0;
Stephen M. Cameronbcc48ff2013-02-20 11:24:57 -06004802 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004803 c->SG[i].Addr.lower = temp64.val32.lower;
4804 c->SG[i].Addr.upper = temp64.val32.upper;
4805 c->SG[i].Len = buff_size[i];
Matt Gatese1d9cbf2014-02-18 13:55:12 -06004806 c->SG[i].Ext = i < sg_used - 1 ? 0 : HPSA_SG_LAST;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004807 }
4808 }
Stephen M. Camerona0c12412011-10-26 16:22:04 -05004809 hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004810 if (sg_used)
4811 hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004812 check_ioctl_unit_attention(h, c);
4813 /* Copy the error information out */
4814 memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
4815 if (copy_to_user(argp, ioc, sizeof(*ioc))) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004816 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05004817 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004818 }
Stephen M. Cameronb03a7772011-01-06 14:47:48 -06004819 if (ioc->Request.Type.Direction == XFER_READ && ioc->buf_size > 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004820 /* Copy the data out of the buffer we created */
4821 BYTE __user *ptr = ioc->buf;
4822 for (i = 0; i < sg_used; i++) {
4823 if (copy_to_user(ptr, buff[i], buff_size[i])) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004824 status = -EFAULT;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05004825 goto cleanup0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004826 }
4827 ptr += buff_size[i];
4828 }
4829 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004830 status = 0;
Stephen M. Camerone2d4a1f2013-09-23 13:33:51 -05004831cleanup0:
4832 cmd_special_free(h, c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004833cleanup1:
4834 if (buff) {
4835 for (i = 0; i < sg_used; i++)
4836 kfree(buff[i]);
4837 kfree(buff);
4838 }
4839 kfree(buff_size);
4840 kfree(ioc);
4841 return status;
4842}
4843
4844static void check_ioctl_unit_attention(struct ctlr_info *h,
4845 struct CommandList *c)
4846{
4847 if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
4848 c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
4849 (void) check_for_unit_attention(h, c);
4850}
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05004851
4852static int increment_passthru_count(struct ctlr_info *h)
4853{
4854 unsigned long flags;
4855
4856 spin_lock_irqsave(&h->passthru_count_lock, flags);
4857 if (h->passthru_count >= HPSA_MAX_CONCURRENT_PASSTHRUS) {
4858 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
4859 return -1;
4860 }
4861 h->passthru_count++;
4862 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
4863 return 0;
4864}
4865
4866static void decrement_passthru_count(struct ctlr_info *h)
4867{
4868 unsigned long flags;
4869
4870 spin_lock_irqsave(&h->passthru_count_lock, flags);
4871 if (h->passthru_count <= 0) {
4872 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
4873 /* not expecting to get here. */
4874 dev_warn(&h->pdev->dev, "Bug detected, passthru_count seems to be incorrect.\n");
4875 return;
4876 }
4877 h->passthru_count--;
4878 spin_unlock_irqrestore(&h->passthru_count_lock, flags);
4879}
4880
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004881/*
4882 * ioctl
4883 */
4884static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
4885{
4886 struct ctlr_info *h;
4887 void __user *argp = (void __user *)arg;
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05004888 int rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004889
4890 h = sdev_to_hba(dev);
4891
4892 switch (cmd) {
4893 case CCISS_DEREGDISK:
4894 case CCISS_REGNEWDISK:
4895 case CCISS_REGNEWD:
Stephen M. Camerona08a8472010-02-04 08:43:16 -06004896 hpsa_scan_start(h->scsi_host);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004897 return 0;
4898 case CCISS_GETPCIINFO:
4899 return hpsa_getpciinfo_ioctl(h, argp);
4900 case CCISS_GETDRIVVER:
4901 return hpsa_getdrivver_ioctl(h, argp);
4902 case CCISS_PASSTHRU:
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05004903 if (increment_passthru_count(h))
4904 return -EAGAIN;
4905 rc = hpsa_passthru_ioctl(h, argp);
4906 decrement_passthru_count(h);
4907 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004908 case CCISS_BIG_PASSTHRU:
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05004909 if (increment_passthru_count(h))
4910 return -EAGAIN;
4911 rc = hpsa_big_passthru_ioctl(h, argp);
4912 decrement_passthru_count(h);
4913 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004914 default:
4915 return -ENOTTY;
4916 }
4917}
4918
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004919static int hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
4920 u8 reset_type)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004921{
4922 struct CommandList *c;
4923
4924 c = cmd_alloc(h);
4925 if (!c)
4926 return -ENOMEM;
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004927 /* fill_cmd can't fail here, no data buffer to map */
4928 (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05004929 RAID_CTLR_LUNID, TYPE_MSG);
4930 c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
4931 c->waiting = NULL;
4932 enqueue_cmd_and_start_io(h, c);
4933 /* Don't wait for completion, the reset won't complete. Don't free
4934 * the command either. This is the last command we will send before
4935 * re-initializing everything, so it doesn't matter and won't leak.
4936 */
4937 return 0;
4938}
4939
Stephen M. Camerona2dac132013-02-20 11:24:41 -06004940static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06004941 void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004942 int cmd_type)
4943{
4944 int pci_dir = XFER_NONE;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05004945 struct CommandList *a; /* for commands to be aborted */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004946
4947 c->cmd_type = CMD_IOCTL_PEND;
4948 c->Header.ReplyQueue = 0;
4949 if (buff != NULL && size > 0) {
4950 c->Header.SGList = 1;
4951 c->Header.SGTotal = 1;
4952 } else {
4953 c->Header.SGList = 0;
4954 c->Header.SGTotal = 0;
4955 }
4956 c->Header.Tag.lower = c->busaddr;
4957 memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
4958
4959 c->Request.Type.Type = cmd_type;
4960 if (cmd_type == TYPE_CMD) {
4961 switch (cmd) {
4962 case HPSA_INQUIRY:
4963 /* are we trying to read a vital product page */
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06004964 if (page_code & VPD_PAGE) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004965 c->Request.CDB[1] = 0x01;
Stephen M. Cameronb7bb24e2014-02-18 13:57:11 -06004966 c->Request.CDB[2] = (page_code & 0xff);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004967 }
4968 c->Request.CDBLen = 6;
4969 c->Request.Type.Attribute = ATTR_SIMPLE;
4970 c->Request.Type.Direction = XFER_READ;
4971 c->Request.Timeout = 0;
4972 c->Request.CDB[0] = HPSA_INQUIRY;
4973 c->Request.CDB[4] = size & 0xFF;
4974 break;
4975 case HPSA_REPORT_LOG:
4976 case HPSA_REPORT_PHYS:
4977 /* Talking to controller so It's a physical command
4978 mode = 00 target = 0. Nothing to write.
4979 */
4980 c->Request.CDBLen = 12;
4981 c->Request.Type.Attribute = ATTR_SIMPLE;
4982 c->Request.Type.Direction = XFER_READ;
4983 c->Request.Timeout = 0;
4984 c->Request.CDB[0] = cmd;
4985 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
4986 c->Request.CDB[7] = (size >> 16) & 0xFF;
4987 c->Request.CDB[8] = (size >> 8) & 0xFF;
4988 c->Request.CDB[9] = size & 0xFF;
4989 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004990 case HPSA_CACHE_FLUSH:
4991 c->Request.CDBLen = 12;
4992 c->Request.Type.Attribute = ATTR_SIMPLE;
4993 c->Request.Type.Direction = XFER_WRITE;
4994 c->Request.Timeout = 0;
4995 c->Request.CDB[0] = BMIC_WRITE;
4996 c->Request.CDB[6] = BMIC_CACHE_FLUSH;
Stephen M. Cameronbb158ea2011-10-26 16:21:17 -05004997 c->Request.CDB[7] = (size >> 8) & 0xFF;
4998 c->Request.CDB[8] = size & 0xFF;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08004999 break;
5000 case TEST_UNIT_READY:
5001 c->Request.CDBLen = 6;
5002 c->Request.Type.Attribute = ATTR_SIMPLE;
5003 c->Request.Type.Direction = XFER_NONE;
5004 c->Request.Timeout = 0;
5005 break;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005006 case HPSA_GET_RAID_MAP:
5007 c->Request.CDBLen = 12;
5008 c->Request.Type.Attribute = ATTR_SIMPLE;
5009 c->Request.Type.Direction = XFER_READ;
5010 c->Request.Timeout = 0;
5011 c->Request.CDB[0] = HPSA_CISS_READ;
5012 c->Request.CDB[1] = cmd;
5013 c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
5014 c->Request.CDB[7] = (size >> 16) & 0xFF;
5015 c->Request.CDB[8] = (size >> 8) & 0xFF;
5016 c->Request.CDB[9] = size & 0xFF;
5017 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005018 default:
5019 dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
5020 BUG();
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005021 return -1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005022 }
5023 } else if (cmd_type == TYPE_MSG) {
5024 switch (cmd) {
5025
5026 case HPSA_DEVICE_RESET_MSG:
5027 c->Request.CDBLen = 16;
5028 c->Request.Type.Type = 1; /* It is a MSG not a CMD */
5029 c->Request.Type.Attribute = ATTR_SIMPLE;
5030 c->Request.Type.Direction = XFER_NONE;
5031 c->Request.Timeout = 0; /* Don't time out */
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005032 memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
5033 c->Request.CDB[0] = cmd;
Stephen M. Cameron21e89af2012-07-26 11:34:10 -05005034 c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005035 /* If bytes 4-7 are zero, it means reset the */
5036 /* LunID device */
5037 c->Request.CDB[4] = 0x00;
5038 c->Request.CDB[5] = 0x00;
5039 c->Request.CDB[6] = 0x00;
5040 c->Request.CDB[7] = 0x00;
Stephen M. Cameron75167d22012-05-01 11:42:51 -05005041 break;
5042 case HPSA_ABORT_MSG:
5043 a = buff; /* point to command to be aborted */
5044 dev_dbg(&h->pdev->dev, "Abort Tag:0x%08x:%08x using request Tag:0x%08x:%08x\n",
5045 a->Header.Tag.upper, a->Header.Tag.lower,
5046 c->Header.Tag.upper, c->Header.Tag.lower);
5047 c->Request.CDBLen = 16;
5048 c->Request.Type.Type = TYPE_MSG;
5049 c->Request.Type.Attribute = ATTR_SIMPLE;
5050 c->Request.Type.Direction = XFER_WRITE;
5051 c->Request.Timeout = 0; /* Don't time out */
5052 c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
5053 c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
5054 c->Request.CDB[2] = 0x00; /* reserved */
5055 c->Request.CDB[3] = 0x00; /* reserved */
5056 /* Tag to abort goes in CDB[4]-CDB[11] */
5057 c->Request.CDB[4] = a->Header.Tag.lower & 0xFF;
5058 c->Request.CDB[5] = (a->Header.Tag.lower >> 8) & 0xFF;
5059 c->Request.CDB[6] = (a->Header.Tag.lower >> 16) & 0xFF;
5060 c->Request.CDB[7] = (a->Header.Tag.lower >> 24) & 0xFF;
5061 c->Request.CDB[8] = a->Header.Tag.upper & 0xFF;
5062 c->Request.CDB[9] = (a->Header.Tag.upper >> 8) & 0xFF;
5063 c->Request.CDB[10] = (a->Header.Tag.upper >> 16) & 0xFF;
5064 c->Request.CDB[11] = (a->Header.Tag.upper >> 24) & 0xFF;
5065 c->Request.CDB[12] = 0x00; /* reserved */
5066 c->Request.CDB[13] = 0x00; /* reserved */
5067 c->Request.CDB[14] = 0x00; /* reserved */
5068 c->Request.CDB[15] = 0x00; /* reserved */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005069 break;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005070 default:
5071 dev_warn(&h->pdev->dev, "unknown message type %d\n",
5072 cmd);
5073 BUG();
5074 }
5075 } else {
5076 dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
5077 BUG();
5078 }
5079
5080 switch (c->Request.Type.Direction) {
5081 case XFER_READ:
5082 pci_dir = PCI_DMA_FROMDEVICE;
5083 break;
5084 case XFER_WRITE:
5085 pci_dir = PCI_DMA_TODEVICE;
5086 break;
5087 case XFER_NONE:
5088 pci_dir = PCI_DMA_NONE;
5089 break;
5090 default:
5091 pci_dir = PCI_DMA_BIDIRECTIONAL;
5092 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06005093 if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
5094 return -1;
5095 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005096}
5097
5098/*
5099 * Map (physical) PCI mem into (virtual) kernel space
5100 */
5101static void __iomem *remap_pci_mem(ulong base, ulong size)
5102{
5103 ulong page_base = ((ulong) base) & PAGE_MASK;
5104 ulong page_offs = ((ulong) base) - page_base;
Stephen M. Cameron088ba342012-07-26 11:34:23 -05005105 void __iomem *page_remapped = ioremap_nocache(page_base,
5106 page_offs + size);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005107
5108 return page_remapped ? (page_remapped + page_offs) : NULL;
5109}
5110
5111/* Takes cmds off the submission queue and sends them to the hardware,
5112 * then puts them on the queue of cmds waiting for completion.
5113 */
5114static void start_io(struct ctlr_info *h)
5115{
5116 struct CommandList *c;
Matt Gatese16a33a2012-05-01 11:43:11 -05005117 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005118
Matt Gatese16a33a2012-05-01 11:43:11 -05005119 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06005120 while (!list_empty(&h->reqQ)) {
5121 c = list_entry(h->reqQ.next, struct CommandList, list);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005122 /* can't do anything if fifo is full */
5123 if ((h->access.fifo_full(h))) {
Stephen M. Cameron396883e2013-09-23 13:34:17 -05005124 h->fifo_recently_full = 1;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005125 dev_warn(&h->pdev->dev, "fifo full\n");
5126 break;
5127 }
Stephen M. Cameron396883e2013-09-23 13:34:17 -05005128 h->fifo_recently_full = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005129
5130 /* Get the first entry from the Request Q */
5131 removeQ(c);
5132 h->Qdepth--;
5133
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005134 /* Put job onto the completed Q */
5135 addQ(&h->cmpQ, c);
Matt Gatese16a33a2012-05-01 11:43:11 -05005136
5137 /* Must increment commands_outstanding before unlocking
5138 * and submitting to avoid race checking for fifo full
5139 * condition.
5140 */
5141 h->commands_outstanding++;
5142 if (h->commands_outstanding > h->max_outstanding)
5143 h->max_outstanding = h->commands_outstanding;
5144
5145 /* Tell the controller execute command */
5146 spin_unlock_irqrestore(&h->lock, flags);
5147 h->access.submit_command(h, c);
5148 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005149 }
Matt Gatese16a33a2012-05-01 11:43:11 -05005150 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005151}
5152
Matt Gates254f7962012-05-01 11:43:06 -05005153static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005154{
Matt Gates254f7962012-05-01 11:43:06 -05005155 return h->access.command_completed(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005156}
5157
Stephen M. Cameron900c5442010-02-04 08:42:35 -06005158static inline bool interrupt_pending(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005159{
5160 return h->access.intr_pending(h);
5161}
5162
5163static inline long interrupt_not_for_us(struct ctlr_info *h)
5164{
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005165 return (h->access.intr_pending(h) == 0) ||
5166 (h->interrupts_enabled == 0);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005167}
5168
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06005169static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
5170 u32 raw_tag)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005171{
5172 if (unlikely(tag_index >= h->nr_cmds)) {
5173 dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
5174 return 1;
5175 }
5176 return 0;
5177}
5178
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05005179static inline void finish_cmd(struct CommandList *c)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005180{
Matt Gatese16a33a2012-05-01 11:43:11 -05005181 unsigned long flags;
Stephen M. Cameron396883e2013-09-23 13:34:17 -05005182 int io_may_be_stalled = 0;
5183 struct ctlr_info *h = c->h;
Matt Gatese16a33a2012-05-01 11:43:11 -05005184
Stephen M. Cameron396883e2013-09-23 13:34:17 -05005185 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005186 removeQ(c);
Stephen M. Cameron396883e2013-09-23 13:34:17 -05005187
5188 /*
5189 * Check for possibly stalled i/o.
5190 *
5191 * If a fifo_full condition is encountered, requests will back up
5192 * in h->reqQ. This queue is only emptied out by start_io which is
5193 * only called when a new i/o request comes in. If no i/o's are
5194 * forthcoming, the i/o's in h->reqQ can get stuck. So we call
5195 * start_io from here if we detect such a danger.
5196 *
5197 * Normally, we shouldn't hit this case, but pounding on the
5198 * CCISS_PASSTHRU ioctl can provoke it. Only call start_io if
5199 * commands_outstanding is low. We want to avoid calling
5200 * start_io from in here as much as possible, and esp. don't
5201 * want to get in a cycle where we call start_io every time
5202 * through here.
5203 */
5204 if (unlikely(h->fifo_recently_full) &&
5205 h->commands_outstanding < 5)
5206 io_may_be_stalled = 1;
5207
5208 spin_unlock_irqrestore(&h->lock, flags);
5209
Stephen M. Camerone85c5972012-05-01 11:43:42 -05005210 dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
Scott Teelc3497752014-02-18 13:56:34 -06005211 if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
5212 || c->cmd_type == CMD_IOACCEL2))
Stephen M. Cameron1fb011f2011-05-03 14:59:00 -05005213 complete_scsi_command(c);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005214 else if (c->cmd_type == CMD_IOCTL_PEND)
5215 complete(c->waiting);
Stephen M. Cameron396883e2013-09-23 13:34:17 -05005216 if (unlikely(io_may_be_stalled))
5217 start_io(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005218}
5219
Stephen M. Camerona104c992010-02-04 08:42:24 -06005220static inline u32 hpsa_tag_contains_index(u32 tag)
5221{
Stephen M. Camerona104c992010-02-04 08:42:24 -06005222 return tag & DIRECT_LOOKUP_BIT;
5223}
5224
5225static inline u32 hpsa_tag_to_index(u32 tag)
5226{
Stephen M. Camerona104c992010-02-04 08:42:24 -06005227 return tag >> DIRECT_LOOKUP_SHIFT;
5228}
5229
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005230
5231static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
Stephen M. Camerona104c992010-02-04 08:42:24 -06005232{
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005233#define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
5234#define HPSA_SIMPLE_ERROR_BITS 0x03
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06005235 if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005236 return tag & ~HPSA_SIMPLE_ERROR_BITS;
5237 return tag & ~HPSA_PERF_ERROR_BITS;
Stephen M. Camerona104c992010-02-04 08:42:24 -06005238}
5239
Don Brace303932f2010-02-04 08:42:40 -06005240/* process completion of an indexed ("direct lookup") command */
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005241static inline void process_indexed_cmd(struct ctlr_info *h,
Don Brace303932f2010-02-04 08:42:40 -06005242 u32 raw_tag)
5243{
5244 u32 tag_index;
5245 struct CommandList *c;
5246
5247 tag_index = hpsa_tag_to_index(raw_tag);
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005248 if (!bad_tag(h, tag_index, raw_tag)) {
5249 c = h->cmd_pool + tag_index;
5250 finish_cmd(c);
5251 }
Don Brace303932f2010-02-04 08:42:40 -06005252}
5253
5254/* process completion of a non-indexed command */
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005255static inline void process_nonindexed_cmd(struct ctlr_info *h,
Don Brace303932f2010-02-04 08:42:40 -06005256 u32 raw_tag)
5257{
5258 u32 tag;
5259 struct CommandList *c = NULL;
Matt Gatese16a33a2012-05-01 11:43:11 -05005260 unsigned long flags;
Don Brace303932f2010-02-04 08:42:40 -06005261
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005262 tag = hpsa_tag_discard_error_bits(h, raw_tag);
Matt Gatese16a33a2012-05-01 11:43:11 -05005263 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06005264 list_for_each_entry(c, &h->cmpQ, list) {
Don Brace303932f2010-02-04 08:42:40 -06005265 if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
Matt Gatese16a33a2012-05-01 11:43:11 -05005266 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05005267 finish_cmd(c);
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005268 return;
Don Brace303932f2010-02-04 08:42:40 -06005269 }
5270 }
Matt Gatese16a33a2012-05-01 11:43:11 -05005271 spin_unlock_irqrestore(&h->lock, flags);
Don Brace303932f2010-02-04 08:42:40 -06005272 bad_tag(h, h->nr_cmds + 1, raw_tag);
Don Brace303932f2010-02-04 08:42:40 -06005273}
5274
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005275/* Some controllers, like p400, will give us one interrupt
5276 * after a soft reset, even if we turned interrupts off.
5277 * Only need to check for this in the hpsa_xxx_discard_completions
5278 * functions.
5279 */
5280static int ignore_bogus_interrupt(struct ctlr_info *h)
5281{
5282 if (likely(!reset_devices))
5283 return 0;
5284
5285 if (likely(h->interrupts_enabled))
5286 return 0;
5287
5288 dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
5289 "(known firmware bug.) Ignoring.\n");
5290
5291 return 1;
5292}
5293
Matt Gates254f7962012-05-01 11:43:06 -05005294/*
5295 * Convert &h->q[x] (passed to interrupt handlers) back to h.
5296 * Relies on (h-q[x] == x) being true for x such that
5297 * 0 <= x < MAX_REPLY_QUEUES.
5298 */
5299static struct ctlr_info *queue_to_hba(u8 *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005300{
Matt Gates254f7962012-05-01 11:43:06 -05005301 return container_of((queue - *queue), struct ctlr_info, q[0]);
5302}
5303
5304static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
5305{
5306 struct ctlr_info *h = queue_to_hba(queue);
5307 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005308 u32 raw_tag;
5309
5310 if (ignore_bogus_interrupt(h))
5311 return IRQ_NONE;
5312
5313 if (interrupt_not_for_us(h))
5314 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005315 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005316 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05005317 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005318 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05005319 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005320 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005321 return IRQ_HANDLED;
5322}
5323
Matt Gates254f7962012-05-01 11:43:06 -05005324static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005325{
Matt Gates254f7962012-05-01 11:43:06 -05005326 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005327 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05005328 u8 q = *(u8 *) queue;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005329
5330 if (ignore_bogus_interrupt(h))
5331 return IRQ_NONE;
5332
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005333 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05005334 raw_tag = get_next_completion(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005335 while (raw_tag != FIFO_EMPTY)
Matt Gates254f7962012-05-01 11:43:06 -05005336 raw_tag = next_command(h, q);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005337 return IRQ_HANDLED;
5338}
5339
Matt Gates254f7962012-05-01 11:43:06 -05005340static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005341{
Matt Gates254f7962012-05-01 11:43:06 -05005342 struct ctlr_info *h = queue_to_hba((u8 *) queue);
Don Brace303932f2010-02-04 08:42:40 -06005343 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05005344 u8 q = *(u8 *) queue;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005345
5346 if (interrupt_not_for_us(h))
5347 return IRQ_NONE;
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005348 h->last_intr_timestamp = get_jiffies_64();
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005349 while (interrupt_pending(h)) {
Matt Gates254f7962012-05-01 11:43:06 -05005350 raw_tag = get_next_completion(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005351 while (raw_tag != FIFO_EMPTY) {
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005352 if (likely(hpsa_tag_contains_index(raw_tag)))
5353 process_indexed_cmd(h, raw_tag);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005354 else
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005355 process_nonindexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05005356 raw_tag = next_command(h, q);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005357 }
5358 }
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005359 return IRQ_HANDLED;
5360}
5361
Matt Gates254f7962012-05-01 11:43:06 -05005362static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005363{
Matt Gates254f7962012-05-01 11:43:06 -05005364 struct ctlr_info *h = queue_to_hba(queue);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005365 u32 raw_tag;
Matt Gates254f7962012-05-01 11:43:06 -05005366 u8 q = *(u8 *) queue;
Stephen M. Cameron10f66012010-06-16 13:51:50 -05005367
Stephen M. Camerona0c12412011-10-26 16:22:04 -05005368 h->last_intr_timestamp = get_jiffies_64();
Matt Gates254f7962012-05-01 11:43:06 -05005369 raw_tag = get_next_completion(h, q);
Don Brace303932f2010-02-04 08:42:40 -06005370 while (raw_tag != FIFO_EMPTY) {
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005371 if (likely(hpsa_tag_contains_index(raw_tag)))
5372 process_indexed_cmd(h, raw_tag);
Don Brace303932f2010-02-04 08:42:40 -06005373 else
Stephen M. Cameron1d94f942012-05-01 11:43:01 -05005374 process_nonindexed_cmd(h, raw_tag);
Matt Gates254f7962012-05-01 11:43:06 -05005375 raw_tag = next_command(h, q);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005376 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005377 return IRQ_HANDLED;
5378}
5379
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005380/* Send a message CDB to the firmware. Careful, this only works
5381 * in simple mode, not performant mode due to the tag lookup.
5382 * We only ever use this immediately after a controller reset.
5383 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005384static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
5385 unsigned char type)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005386{
5387 struct Command {
5388 struct CommandListHeader CommandHeader;
5389 struct RequestBlock Request;
5390 struct ErrDescriptor ErrorDescriptor;
5391 };
5392 struct Command *cmd;
5393 static const size_t cmd_sz = sizeof(*cmd) +
5394 sizeof(cmd->ErrorDescriptor);
5395 dma_addr_t paddr64;
5396 uint32_t paddr32, tag;
5397 void __iomem *vaddr;
5398 int i, err;
5399
5400 vaddr = pci_ioremap_bar(pdev, 0);
5401 if (vaddr == NULL)
5402 return -ENOMEM;
5403
5404 /* The Inbound Post Queue only accepts 32-bit physical addresses for the
5405 * CCISS commands, so they must be allocated from the lower 4GiB of
5406 * memory.
5407 */
5408 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
5409 if (err) {
5410 iounmap(vaddr);
5411 return -ENOMEM;
5412 }
5413
5414 cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
5415 if (cmd == NULL) {
5416 iounmap(vaddr);
5417 return -ENOMEM;
5418 }
5419
5420 /* This must fit, because of the 32-bit consistent DMA mask. Also,
5421 * although there's no guarantee, we assume that the address is at
5422 * least 4-byte aligned (most likely, it's page-aligned).
5423 */
5424 paddr32 = paddr64;
5425
5426 cmd->CommandHeader.ReplyQueue = 0;
5427 cmd->CommandHeader.SGList = 0;
5428 cmd->CommandHeader.SGTotal = 0;
5429 cmd->CommandHeader.Tag.lower = paddr32;
5430 cmd->CommandHeader.Tag.upper = 0;
5431 memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
5432
5433 cmd->Request.CDBLen = 16;
5434 cmd->Request.Type.Type = TYPE_MSG;
5435 cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
5436 cmd->Request.Type.Direction = XFER_NONE;
5437 cmd->Request.Timeout = 0; /* Don't time out */
5438 cmd->Request.CDB[0] = opcode;
5439 cmd->Request.CDB[1] = type;
5440 memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
5441 cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
5442 cmd->ErrorDescriptor.Addr.upper = 0;
5443 cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
5444
5445 writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
5446
5447 for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
5448 tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005449 if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr32)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005450 break;
5451 msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
5452 }
5453
5454 iounmap(vaddr);
5455
5456 /* we leak the DMA buffer here ... no choice since the controller could
5457 * still complete the command.
5458 */
5459 if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
5460 dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
5461 opcode, type);
5462 return -ETIMEDOUT;
5463 }
5464
5465 pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
5466
5467 if (tag & HPSA_ERROR_BIT) {
5468 dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
5469 opcode, type);
5470 return -EIO;
5471 }
5472
5473 dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
5474 opcode, type);
5475 return 0;
5476}
5477
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005478#define hpsa_noop(p) hpsa_message(p, 3, 0)
5479
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005480static int hpsa_controller_hard_reset(struct pci_dev *pdev,
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005481 void * __iomem vaddr, u32 use_doorbell)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005482{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005483 u16 pmcsr;
5484 int pos;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005485
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005486 if (use_doorbell) {
5487 /* For everything after the P600, the PCI power state method
5488 * of resetting the controller doesn't work, so we have this
5489 * other way using the doorbell register.
5490 */
5491 dev_info(&pdev->dev, "using doorbell to reset controller\n");
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005492 writel(use_doorbell, vaddr + SA5_DOORBELL);
Stephen M. Cameron85009232013-09-23 13:33:36 -05005493
5494 /* PMC hardware guys tell us we need a 5 second delay after
5495 * doorbell reset and before any attempt to talk to the board
5496 * at all to ensure that this actually works and doesn't fall
5497 * over in some weird corner cases.
5498 */
5499 msleep(5000);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005500 } else { /* Try to do it the PCI power state way */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005501
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005502 /* Quoting from the Open CISS Specification: "The Power
5503 * Management Control/Status Register (CSR) controls the power
5504 * state of the device. The normal operating state is D0,
5505 * CSR=00h. The software off state is D3, CSR=03h. To reset
5506 * the controller, place the interface device in D3 then to D0,
5507 * this causes a secondary PCI reset which will reset the
5508 * controller." */
5509
5510 pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
5511 if (pos == 0) {
5512 dev_err(&pdev->dev,
5513 "hpsa_reset_controller: "
5514 "PCI PM not supported\n");
5515 return -ENODEV;
5516 }
5517 dev_info(&pdev->dev, "using PCI PM to reset controller\n");
5518 /* enter the D3hot power management state */
5519 pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
5520 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
5521 pmcsr |= PCI_D3hot;
5522 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
5523
5524 msleep(500);
5525
5526 /* enter the D0 power management state */
5527 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
5528 pmcsr |= PCI_D0;
5529 pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
Mike Millerc4853ef2011-10-21 08:19:43 +02005530
5531 /*
5532 * The P600 requires a small delay when changing states.
5533 * Otherwise we may think the board did not reset and we bail.
5534 * This for kdump only and is particular to the P600.
5535 */
5536 msleep(500);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005537 }
5538 return 0;
5539}
5540
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005541static void init_driver_version(char *driver_version, int len)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005542{
5543 memset(driver_version, 0, len);
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06005544 strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005545}
5546
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005547static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005548{
5549 char *driver_version;
5550 int i, size = sizeof(cfgtable->driver_version);
5551
5552 driver_version = kmalloc(size, GFP_KERNEL);
5553 if (!driver_version)
5554 return -ENOMEM;
5555
5556 init_driver_version(driver_version, size);
5557 for (i = 0; i < size; i++)
5558 writeb(driver_version[i], &cfgtable->driver_version[i]);
5559 kfree(driver_version);
5560 return 0;
5561}
5562
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005563static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
5564 unsigned char *driver_ver)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005565{
5566 int i;
5567
5568 for (i = 0; i < sizeof(cfgtable->driver_version); i++)
5569 driver_ver[i] = readb(&cfgtable->driver_version[i]);
5570}
5571
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005572static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005573{
5574
5575 char *driver_ver, *old_driver_ver;
5576 int rc, size = sizeof(cfgtable->driver_version);
5577
5578 old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
5579 if (!old_driver_ver)
5580 return -ENOMEM;
5581 driver_ver = old_driver_ver + size;
5582
5583 /* After a reset, the 32 bytes of "driver version" in the cfgtable
5584 * should have been changed, otherwise we know the reset failed.
5585 */
5586 init_driver_version(old_driver_ver, size);
5587 read_driver_ver_from_cfgtable(cfgtable, driver_ver);
5588 rc = !memcmp(driver_ver, old_driver_ver, size);
5589 kfree(old_driver_ver);
5590 return rc;
5591}
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005592/* This does a hard reset of the controller using PCI power management
5593 * states or the using the doorbell register.
5594 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005595static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev)
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005596{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005597 u64 cfg_offset;
5598 u32 cfg_base_addr;
5599 u64 cfg_base_addr_index;
5600 void __iomem *vaddr;
5601 unsigned long paddr;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005602 u32 misc_fw_support;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005603 int rc;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005604 struct CfgTable __iomem *cfgtable;
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005605 u32 use_doorbell;
Stephen M. Cameron18867652010-06-16 13:51:45 -05005606 u32 board_id;
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005607 u16 command_register;
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005608
5609 /* For controllers as old as the P600, this is very nearly
5610 * the same thing as
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005611 *
5612 * pci_save_state(pci_dev);
5613 * pci_set_power_state(pci_dev, PCI_D3hot);
5614 * pci_set_power_state(pci_dev, PCI_D0);
5615 * pci_restore_state(pci_dev);
5616 *
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005617 * For controllers newer than the P600, the pci power state
5618 * method of resetting doesn't work so we have another way
5619 * using the doorbell register.
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005620 */
Stephen M. Cameron18867652010-06-16 13:51:45 -05005621
Stephen M. Cameron25c1e56a2011-01-06 14:48:18 -06005622 rc = hpsa_lookup_board_id(pdev, &board_id);
Stephen M. Cameron46380782011-05-03 15:00:01 -05005623 if (rc < 0 || !ctlr_is_resettable(board_id)) {
Stephen M. Cameron25c1e56a2011-01-06 14:48:18 -06005624 dev_warn(&pdev->dev, "Not resetting device.\n");
5625 return -ENODEV;
5626 }
Stephen M. Cameron46380782011-05-03 15:00:01 -05005627
5628 /* if controller is soft- but not hard resettable... */
5629 if (!ctlr_is_hard_resettable(board_id))
5630 return -ENOTSUPP; /* try soft reset later. */
Stephen M. Cameron18867652010-06-16 13:51:45 -05005631
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005632 /* Save the PCI command register */
5633 pci_read_config_word(pdev, 4, &command_register);
5634 /* Turn the board off. This is so that later pci_restore_state()
5635 * won't turn the board on before the rest of config space is ready.
5636 */
5637 pci_disable_device(pdev);
5638 pci_save_state(pdev);
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005639
5640 /* find the first memory BAR, so we can find the cfg table */
5641 rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
5642 if (rc)
5643 return rc;
5644 vaddr = remap_pci_mem(paddr, 0x250);
5645 if (!vaddr)
5646 return -ENOMEM;
5647
5648 /* find cfgtable in order to check if reset via doorbell is supported */
5649 rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
5650 &cfg_base_addr_index, &cfg_offset);
5651 if (rc)
5652 goto unmap_vaddr;
5653 cfgtable = remap_pci_mem(pci_resource_start(pdev,
5654 cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
5655 if (!cfgtable) {
5656 rc = -ENOMEM;
5657 goto unmap_vaddr;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005658 }
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005659 rc = write_driver_ver_to_cfgtable(cfgtable);
5660 if (rc)
5661 goto unmap_vaddr;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005662
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005663 /* If reset via doorbell register is supported, use that.
5664 * There are two such methods. Favor the newest method.
5665 */
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005666 misc_fw_support = readl(&cfgtable->misc_fw_support);
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005667 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
5668 if (use_doorbell) {
5669 use_doorbell = DOORBELL_CTLR_RESET2;
5670 } else {
5671 use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
5672 if (use_doorbell) {
Mike Millerfba63092011-10-13 11:44:06 -05005673 dev_warn(&pdev->dev, "Soft reset not supported. "
5674 "Firmware update is required.\n");
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005675 rc = -ENOTSUPP; /* try soft reset */
Stephen M. Cameroncf0b08d2011-05-03 14:59:46 -05005676 goto unmap_cfgtable;
5677 }
5678 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005679
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005680 rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
5681 if (rc)
5682 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005683
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005684 pci_restore_state(pdev);
5685 rc = pci_enable_device(pdev);
5686 if (rc) {
5687 dev_warn(&pdev->dev, "failed to enable device.\n");
5688 goto unmap_cfgtable;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005689 }
Stephen M. Cameron270d05d2011-01-06 14:48:08 -06005690 pci_write_config_word(pdev, 4, command_register);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005691
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005692 /* Some devices (notably the HP Smart Array 5i Controller)
5693 need a little pause here */
5694 msleep(HPSA_POST_RESET_PAUSE_MSECS);
5695
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005696 rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
5697 if (rc) {
5698 dev_warn(&pdev->dev,
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005699 "failed waiting for board to become ready "
5700 "after hard reset\n");
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005701 goto unmap_cfgtable;
5702 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005703
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005704 rc = controller_reset_failed(vaddr);
5705 if (rc < 0)
5706 goto unmap_cfgtable;
5707 if (rc) {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005708 dev_warn(&pdev->dev, "Unable to successfully reset "
5709 "controller. Will try soft reset.\n");
5710 rc = -ENOTSUPP;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005711 } else {
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05005712 dev_info(&pdev->dev, "board ready after hard reset.\n");
Stephen M. Cameron1df85522010-06-16 13:51:40 -05005713 }
5714
5715unmap_cfgtable:
5716 iounmap(cfgtable);
5717
5718unmap_vaddr:
5719 iounmap(vaddr);
5720 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005721}
5722
5723/*
5724 * We cannot read the structure directly, for portability we must use
5725 * the io functions.
5726 * This is for debug only.
5727 */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005728static void print_cfg_table(struct device *dev, struct CfgTable *tb)
5729{
Stephen M. Cameron58f86652010-05-27 15:13:58 -05005730#ifdef HPSA_DEBUG
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005731 int i;
5732 char temp_name[17];
5733
5734 dev_info(dev, "Controller Configuration information\n");
5735 dev_info(dev, "------------------------------------\n");
5736 for (i = 0; i < 4; i++)
5737 temp_name[i] = readb(&(tb->Signature[i]));
5738 temp_name[4] = '\0';
5739 dev_info(dev, " Signature = %s\n", temp_name);
5740 dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
5741 dev_info(dev, " Transport methods supported = 0x%x\n",
5742 readl(&(tb->TransportSupport)));
5743 dev_info(dev, " Transport methods active = 0x%x\n",
5744 readl(&(tb->TransportActive)));
5745 dev_info(dev, " Requested transport Method = 0x%x\n",
5746 readl(&(tb->HostWrite.TransportRequest)));
5747 dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
5748 readl(&(tb->HostWrite.CoalIntDelay)));
5749 dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
5750 readl(&(tb->HostWrite.CoalIntCount)));
5751 dev_info(dev, " Max outstanding commands = 0x%d\n",
5752 readl(&(tb->CmdsOutMax)));
5753 dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
5754 for (i = 0; i < 16; i++)
5755 temp_name[i] = readb(&(tb->ServerName[i]));
5756 temp_name[16] = '\0';
5757 dev_info(dev, " Server Name = %s\n", temp_name);
5758 dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
5759 readl(&(tb->HeartBeat)));
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005760#endif /* HPSA_DEBUG */
Stephen M. Cameron58f86652010-05-27 15:13:58 -05005761}
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005762
5763static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
5764{
5765 int i, offset, mem_type, bar_type;
5766
5767 if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
5768 return 0;
5769 offset = 0;
5770 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
5771 bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
5772 if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
5773 offset += 4;
5774 else {
5775 mem_type = pci_resource_flags(pdev, i) &
5776 PCI_BASE_ADDRESS_MEM_TYPE_MASK;
5777 switch (mem_type) {
5778 case PCI_BASE_ADDRESS_MEM_TYPE_32:
5779 case PCI_BASE_ADDRESS_MEM_TYPE_1M:
5780 offset += 4; /* 32 bit */
5781 break;
5782 case PCI_BASE_ADDRESS_MEM_TYPE_64:
5783 offset += 8;
5784 break;
5785 default: /* reserved in PCI 2.2 */
5786 dev_warn(&pdev->dev,
5787 "base address is invalid\n");
5788 return -1;
5789 break;
5790 }
5791 }
5792 if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
5793 return i + 1;
5794 }
5795 return -1;
5796}
5797
5798/* If MSI/MSI-X is supported by the kernel we will try to enable it on
5799 * controllers that are capable. If not, we use IO-APIC mode.
5800 */
5801
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005802static void hpsa_interrupt_mode(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005803{
5804#ifdef CONFIG_PCI_MSI
Matt Gates254f7962012-05-01 11:43:06 -05005805 int err, i;
5806 struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
5807
5808 for (i = 0; i < MAX_REPLY_QUEUES; i++) {
5809 hpsa_msix_entries[i].vector = 0;
5810 hpsa_msix_entries[i].entry = i;
5811 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005812
5813 /* Some boards advertise MSI but don't really support it */
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05005814 if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
5815 (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005816 goto default_int_mode;
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005817 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
5818 dev_info(&h->pdev->dev, "MSIX\n");
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005819 h->msix_vector = MAX_REPLY_QUEUES;
Matt Gates254f7962012-05-01 11:43:06 -05005820 err = pci_enable_msix(h->pdev, hpsa_msix_entries,
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005821 h->msix_vector);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005822 if (err > 0) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005823 dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005824 "available\n", err);
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005825 h->msix_vector = err;
5826 err = pci_enable_msix(h->pdev, hpsa_msix_entries,
5827 h->msix_vector);
5828 }
5829 if (!err) {
5830 for (i = 0; i < h->msix_vector; i++)
5831 h->intr[i] = hpsa_msix_entries[i].vector;
5832 return;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005833 } else {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005834 dev_warn(&h->pdev->dev, "MSI-X init failed %d\n",
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005835 err);
Hannes Reineckeeee0f032014-01-15 13:30:53 +01005836 h->msix_vector = 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005837 goto default_int_mode;
5838 }
5839 }
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005840 if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
5841 dev_info(&h->pdev->dev, "MSI\n");
5842 if (!pci_enable_msi(h->pdev))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005843 h->msi_vector = 1;
5844 else
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05005845 dev_warn(&h->pdev->dev, "MSI init failed\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005846 }
5847default_int_mode:
5848#endif /* CONFIG_PCI_MSI */
5849 /* if we get here we're going to use the default interrupt mode */
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06005850 h->intr[h->intr_mode] = h->pdev->irq;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005851}
5852
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005853static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05005854{
5855 int i;
5856 u32 subsystem_vendor_id, subsystem_device_id;
5857
5858 subsystem_vendor_id = pdev->subsystem_vendor;
5859 subsystem_device_id = pdev->subsystem_device;
5860 *board_id = ((subsystem_device_id << 16) & 0xffff0000) |
5861 subsystem_vendor_id;
5862
5863 for (i = 0; i < ARRAY_SIZE(products); i++)
5864 if (*board_id == products[i].board_id)
5865 return i;
5866
Stephen M. Cameron6798cc02010-06-16 13:51:20 -05005867 if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
5868 subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
5869 !hpsa_allow_any) {
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05005870 dev_warn(&pdev->dev, "unrecognized board ID: "
5871 "0x%08x, ignoring.\n", *board_id);
5872 return -ENODEV;
5873 }
5874 return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
5875}
5876
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005877static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
5878 unsigned long *memory_bar)
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005879{
5880 int i;
5881
5882 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05005883 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005884 /* addressing mode bits already removed */
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05005885 *memory_bar = pci_resource_start(pdev, i);
5886 dev_dbg(&pdev->dev, "memory BAR = %lx\n",
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005887 *memory_bar);
5888 return 0;
5889 }
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05005890 dev_warn(&pdev->dev, "no memory BAR found\n");
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05005891 return -ENODEV;
5892}
5893
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005894static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
5895 int wait_for_ready)
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005896{
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005897 int i, iterations;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005898 u32 scratchpad;
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005899 if (wait_for_ready)
5900 iterations = HPSA_BOARD_READY_ITERATIONS;
5901 else
5902 iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005903
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005904 for (i = 0; i < iterations; i++) {
5905 scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
5906 if (wait_for_ready) {
5907 if (scratchpad == HPSA_FIRMWARE_READY)
5908 return 0;
5909 } else {
5910 if (scratchpad != HPSA_FIRMWARE_READY)
5911 return 0;
5912 }
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005913 msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
5914 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06005915 dev_warn(&pdev->dev, "board not ready, timed out.\n");
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05005916 return -ENODEV;
5917}
5918
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005919static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
5920 u32 *cfg_base_addr, u64 *cfg_base_addr_index,
5921 u64 *cfg_offset)
Stephen M. Camerona51fd472010-06-16 13:51:30 -05005922{
5923 *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
5924 *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
5925 *cfg_base_addr &= (u32) 0x0000ffff;
5926 *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
5927 if (*cfg_base_addr_index == -1) {
5928 dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
5929 return -ENODEV;
5930 }
5931 return 0;
5932}
5933
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005934static int hpsa_find_cfgtables(struct ctlr_info *h)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08005935{
Stephen M. Cameron01a02ff2010-02-04 08:41:33 -06005936 u64 cfg_offset;
5937 u32 cfg_base_addr;
5938 u64 cfg_base_addr_index;
Don Brace303932f2010-02-04 08:42:40 -06005939 u32 trans_offset;
Stephen M. Camerona51fd472010-06-16 13:51:30 -05005940 int rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005941
Stephen M. Camerona51fd472010-06-16 13:51:30 -05005942 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
5943 &cfg_base_addr_index, &cfg_offset);
5944 if (rc)
5945 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005946 h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
Stephen M. Camerona51fd472010-06-16 13:51:30 -05005947 cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005948 if (!h->cfgtable)
5949 return -ENOMEM;
Stephen M. Cameron580ada32011-05-03 14:59:10 -05005950 rc = write_driver_ver_to_cfgtable(h->cfgtable);
5951 if (rc)
5952 return rc;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005953 /* Find performant mode table. */
Stephen M. Camerona51fd472010-06-16 13:51:30 -05005954 trans_offset = readl(&h->cfgtable->TransMethodOffset);
Stephen M. Cameron77c44952010-05-27 15:13:17 -05005955 h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
5956 cfg_base_addr_index)+cfg_offset+trans_offset,
5957 sizeof(*h->transtable));
5958 if (!h->transtable)
5959 return -ENOMEM;
5960 return 0;
5961}
5962
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005963static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05005964{
5965 h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
Stephen M. Cameron72ceeae2011-01-06 14:48:13 -06005966
5967 /* Limit commands in memory limited kdump scenario. */
5968 if (reset_devices && h->max_commands > 32)
5969 h->max_commands = 32;
5970
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05005971 if (h->max_commands < 16) {
5972 dev_warn(&h->pdev->dev, "Controller reports "
5973 "max supported commands of %d, an obvious lie. "
5974 "Using 16. Ensure that firmware is up to date.\n",
5975 h->max_commands);
5976 h->max_commands = 16;
5977 }
5978}
5979
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005980/* Interrogate the hardware for some limits:
5981 * max commands, max SG elements without chaining, and with chaining,
5982 * SG chain block size, etc.
5983 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005984static void hpsa_find_board_params(struct ctlr_info *h)
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005985{
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05005986 hpsa_get_max_perf_mode_cmds(h);
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005987 h->nr_cmds = h->max_commands - 4; /* Allow room for some ioctls */
5988 h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06005989 h->fw_support = readl(&(h->cfgtable->misc_fw_support));
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05005990 /*
5991 * Limit in-command s/g elements to 32 save dma'able memory.
5992 * Howvever spec says if 0, use 31
5993 */
5994 h->max_cmd_sg_entries = 31;
5995 if (h->maxsgentries > 512) {
5996 h->max_cmd_sg_entries = 32;
5997 h->chainsize = h->maxsgentries - h->max_cmd_sg_entries + 1;
5998 h->maxsgentries--; /* save one for chain pointer */
5999 } else {
6000 h->maxsgentries = 31; /* default to traditional values */
6001 h->chainsize = 0;
6002 }
Stephen M. Cameron75167d22012-05-01 11:42:51 -05006003
6004 /* Find out what task management functions are supported and cache */
6005 h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
Scott Teel0e7a7fc2014-02-18 13:55:59 -06006006 if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
6007 dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
6008 if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
6009 dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05006010}
6011
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05006012static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
6013{
Akinobu Mita0fc9fd42012-04-04 22:14:59 +09006014 if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05006015 dev_warn(&h->pdev->dev, "not a valid CISS config table\n");
6016 return false;
6017 }
6018 return true;
6019}
6020
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06006021static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05006022{
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06006023 u32 driver_support;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05006024
Stephen M. Cameron28e13442013-12-04 17:10:21 -06006025#ifdef CONFIG_X86
6026 /* Need to enable prefetch in the SCSI core for 6400 in x86 */
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06006027 driver_support = readl(&(h->cfgtable->driver_support));
6028 driver_support |= ENABLE_SCSI_PREFETCH;
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05006029#endif
Stephen M. Cameron28e13442013-12-04 17:10:21 -06006030 driver_support |= ENABLE_UNIT_ATTN;
6031 writel(driver_support, &(h->cfgtable->driver_support));
Stephen M. Cameronf7c39102010-05-27 15:13:38 -05006032}
6033
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05006034/* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result
6035 * in a prefetch beyond physical memory.
6036 */
6037static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
6038{
6039 u32 dma_prefetch;
6040
6041 if (h->board_id != 0x3225103C)
6042 return;
6043 dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
6044 dma_prefetch |= 0x8000;
6045 writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
6046}
6047
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006048static void hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
6049{
6050 int i;
6051 u32 doorbell_value;
6052 unsigned long flags;
6053 /* wait until the clear_event_notify bit 6 is cleared by controller. */
6054 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
6055 spin_lock_irqsave(&h->lock, flags);
6056 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
6057 spin_unlock_irqrestore(&h->lock, flags);
6058 if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
6059 break;
6060 /* delay and try again */
6061 msleep(20);
6062 }
6063}
6064
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006065static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006066{
6067 int i;
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06006068 u32 doorbell_value;
6069 unsigned long flags;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006070
6071 /* under certain very rare conditions, this can take awhile.
6072 * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
6073 * as we enter this code.)
6074 */
6075 for (i = 0; i < MAX_CONFIG_WAIT; i++) {
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06006076 spin_lock_irqsave(&h->lock, flags);
6077 doorbell_value = readl(h->vaddr + SA5_DOORBELL);
6078 spin_unlock_irqrestore(&h->lock, flags);
Dan Carpenter382be662011-02-15 15:33:13 -06006079 if (!(doorbell_value & CFGTBL_ChangeReq))
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006080 break;
6081 /* delay and try again */
Stephen M. Cameron60d3f5b2011-01-06 14:48:34 -06006082 usleep_range(10000, 20000);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006083 }
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05006084}
6085
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006086static int hpsa_enter_simple_mode(struct ctlr_info *h)
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05006087{
6088 u32 trans_support;
6089
6090 trans_support = readl(&(h->cfgtable->TransportSupport));
6091 if (!(trans_support & SIMPLE_MODE))
6092 return -ENOTSUPP;
6093
6094 h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006095
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05006096 /* Update the field, and then ring the doorbell */
6097 writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006098 writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05006099 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
6100 hpsa_wait_for_mode_change_ack(h);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006101 print_cfg_table(&h->pdev->dev, h->cfgtable);
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006102 if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
6103 goto error;
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06006104 h->transMethod = CFGTBL_Trans_Simple;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006105 return 0;
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006106error:
6107 dev_warn(&h->pdev->dev, "unable to get board into simple mode\n");
6108 return -ENODEV;
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006109}
6110
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006111static int hpsa_pci_init(struct ctlr_info *h)
Stephen M. Cameron77c44952010-05-27 15:13:17 -05006112{
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006113 int prod_index, err;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006114
Stephen M. Camerone5c880d2010-05-27 15:12:52 -05006115 prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
6116 if (prod_index < 0)
6117 return -ENODEV;
6118 h->product_name = products[prod_index].product_name;
6119 h->access = *(products[prod_index].access);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006120
Matthew Garrette5a44df2011-11-11 11:14:23 -05006121 pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
6122 PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
6123
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05006124 err = pci_enable_device(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006125 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05006126 dev_warn(&h->pdev->dev, "unable to enable PCI device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006127 return err;
6128 }
6129
Stephen M. Cameron5cb460a2012-05-01 11:42:20 -05006130 /* Enable bus mastering (pci_disable_device may disable this) */
6131 pci_set_master(h->pdev);
6132
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06006133 err = pci_request_regions(h->pdev, HPSA);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006134 if (err) {
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05006135 dev_err(&h->pdev->dev,
6136 "cannot obtain PCI resources, aborting\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006137 return err;
6138 }
Stephen M. Cameron6b3f4c52010-05-27 15:13:02 -05006139 hpsa_interrupt_mode(h);
Stephen M. Cameron12d2cd42010-06-16 13:51:25 -05006140 err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
Stephen M. Cameron3a7774c2010-05-27 15:13:07 -05006141 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006142 goto err_out_free_res;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006143 h->vaddr = remap_pci_mem(h->paddr, 0x250);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05006144 if (!h->vaddr) {
6145 err = -ENOMEM;
6146 goto err_out_free_res;
6147 }
Stephen M. Cameronfe5389c2011-01-06 14:48:03 -06006148 err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
Stephen M. Cameron2c4c8c82010-05-27 15:13:12 -05006149 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006150 goto err_out_free_res;
Stephen M. Cameron77c44952010-05-27 15:13:17 -05006151 err = hpsa_find_cfgtables(h);
6152 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006153 goto err_out_free_res;
Stephen M. Cameronb93d7532010-05-27 15:13:27 -05006154 hpsa_find_board_params(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006155
Stephen M. Cameron76c46e42010-05-27 15:13:32 -05006156 if (!hpsa_CISS_signature_present(h)) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006157 err = -ENODEV;
6158 goto err_out_free_res;
6159 }
Stephen M. Cameron97a5e982013-12-04 17:10:16 -06006160 hpsa_set_driver_support_bits(h);
Stephen M. Cameron3d0eab62010-05-27 15:13:43 -05006161 hpsa_p600_dma_prefetch_quirk(h);
Stephen M. Cameroneb6b2ae2010-05-27 15:13:48 -05006162 err = hpsa_enter_simple_mode(h);
6163 if (err)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006164 goto err_out_free_res;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006165 return 0;
6166
6167err_out_free_res:
Stephen M. Cameron204892e2010-05-27 15:13:22 -05006168 if (h->transtable)
6169 iounmap(h->transtable);
6170 if (h->cfgtable)
6171 iounmap(h->cfgtable);
6172 if (h->vaddr)
6173 iounmap(h->vaddr);
Stephen M. Cameronf0bd0b62012-05-01 11:42:09 -05006174 pci_disable_device(h->pdev);
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05006175 pci_release_regions(h->pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006176 return err;
6177}
6178
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006179static void hpsa_hba_inquiry(struct ctlr_info *h)
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06006180{
6181 int rc;
6182
6183#define HBA_INQUIRY_BYTE_COUNT 64
6184 h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
6185 if (!h->hba_inquiry_data)
6186 return;
6187 rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
6188 h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
6189 if (rc != 0) {
6190 kfree(h->hba_inquiry_data);
6191 h->hba_inquiry_data = NULL;
6192 }
6193}
6194
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006195static int hpsa_init_reset_devices(struct pci_dev *pdev)
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006196{
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006197 int rc, i;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006198
6199 if (!reset_devices)
6200 return 0;
6201
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006202 /* Reset the controller with a PCI power-cycle or via doorbell */
6203 rc = hpsa_kdump_hard_reset_controller(pdev);
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006204
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006205 /* -ENOTSUPP here means we cannot reset the controller
6206 * but it's already (and still) up and running in
Stephen M. Cameron18867652010-06-16 13:51:45 -05006207 * "performant mode". Or, it might be 640x, which can't reset
6208 * due to concerns about shared bbwc between 6402/6404 pair.
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006209 */
6210 if (rc == -ENOTSUPP)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006211 return rc; /* just try to do the kdump anyhow. */
Stephen M. Cameron1df85522010-06-16 13:51:40 -05006212 if (rc)
6213 return -ENODEV;
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006214
6215 /* Now try to get the controller to respond to a no-op */
Stephen M. Cameron2b870cb2011-05-03 14:59:36 -05006216 dev_warn(&pdev->dev, "Waiting for controller to respond to no-op\n");
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006217 for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
6218 if (hpsa_noop(pdev) == 0)
6219 break;
6220 else
6221 dev_warn(&pdev->dev, "no-op failed%s\n",
6222 (i < 11 ? "; re-trying" : ""));
6223 }
6224 return 0;
6225}
6226
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006227static int hpsa_allocate_cmd_pool(struct ctlr_info *h)
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05006228{
6229 h->cmd_pool_bits = kzalloc(
6230 DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
6231 sizeof(unsigned long), GFP_KERNEL);
6232 h->cmd_pool = pci_alloc_consistent(h->pdev,
6233 h->nr_cmds * sizeof(*h->cmd_pool),
6234 &(h->cmd_pool_dhandle));
6235 h->errinfo_pool = pci_alloc_consistent(h->pdev,
6236 h->nr_cmds * sizeof(*h->errinfo_pool),
6237 &(h->errinfo_pool_dhandle));
6238 if ((h->cmd_pool_bits == NULL)
6239 || (h->cmd_pool == NULL)
6240 || (h->errinfo_pool == NULL)) {
6241 dev_err(&h->pdev->dev, "out of memory in %s", __func__);
6242 return -ENOMEM;
6243 }
6244 return 0;
6245}
6246
6247static void hpsa_free_cmd_pool(struct ctlr_info *h)
6248{
6249 kfree(h->cmd_pool_bits);
6250 if (h->cmd_pool)
6251 pci_free_consistent(h->pdev,
6252 h->nr_cmds * sizeof(struct CommandList),
6253 h->cmd_pool, h->cmd_pool_dhandle);
Stephen M. Cameronaca90122014-02-18 13:56:14 -06006254 if (h->ioaccel2_cmd_pool)
6255 pci_free_consistent(h->pdev,
6256 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
6257 h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05006258 if (h->errinfo_pool)
6259 pci_free_consistent(h->pdev,
6260 h->nr_cmds * sizeof(struct ErrorInfo),
6261 h->errinfo_pool,
6262 h->errinfo_pool_dhandle);
Matt Gatese1f7de02014-02-18 13:55:17 -06006263 if (h->ioaccel_cmd_pool)
6264 pci_free_consistent(h->pdev,
6265 h->nr_cmds * sizeof(struct io_accel1_cmd),
6266 h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05006267}
6268
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05006269static int hpsa_request_irq(struct ctlr_info *h,
6270 irqreturn_t (*msixhandler)(int, void *),
6271 irqreturn_t (*intxhandler)(int, void *))
6272{
Matt Gates254f7962012-05-01 11:43:06 -05006273 int rc, i;
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05006274
Matt Gates254f7962012-05-01 11:43:06 -05006275 /*
6276 * initialize h->q[x] = x so that interrupt handlers know which
6277 * queue to process.
6278 */
6279 for (i = 0; i < MAX_REPLY_QUEUES; i++)
6280 h->q[i] = (u8) i;
6281
Hannes Reineckeeee0f032014-01-15 13:30:53 +01006282 if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
Matt Gates254f7962012-05-01 11:43:06 -05006283 /* If performant mode and MSI-X, use multiple reply queues */
Hannes Reineckeeee0f032014-01-15 13:30:53 +01006284 for (i = 0; i < h->msix_vector; i++)
Matt Gates254f7962012-05-01 11:43:06 -05006285 rc = request_irq(h->intr[i], msixhandler,
6286 0, h->devname,
6287 &h->q[i]);
6288 } else {
6289 /* Use single reply pool */
Hannes Reineckeeee0f032014-01-15 13:30:53 +01006290 if (h->msix_vector > 0 || h->msi_vector) {
Matt Gates254f7962012-05-01 11:43:06 -05006291 rc = request_irq(h->intr[h->intr_mode],
6292 msixhandler, 0, h->devname,
6293 &h->q[h->intr_mode]);
6294 } else {
6295 rc = request_irq(h->intr[h->intr_mode],
6296 intxhandler, IRQF_SHARED, h->devname,
6297 &h->q[h->intr_mode]);
6298 }
6299 }
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05006300 if (rc) {
6301 dev_err(&h->pdev->dev, "unable to get irq %d for %s\n",
6302 h->intr[h->intr_mode], h->devname);
6303 return -ENODEV;
6304 }
6305 return 0;
6306}
6307
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006308static int hpsa_kdump_soft_reset(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006309{
6310 if (hpsa_send_host_reset(h, RAID_CTLR_LUNID,
6311 HPSA_RESET_TYPE_CONTROLLER)) {
6312 dev_warn(&h->pdev->dev, "Resetting array controller failed.\n");
6313 return -EIO;
6314 }
6315
6316 dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
6317 if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) {
6318 dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
6319 return -1;
6320 }
6321
6322 dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
6323 if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) {
6324 dev_warn(&h->pdev->dev, "Board failed to become ready "
6325 "after soft reset.\n");
6326 return -1;
6327 }
6328
6329 return 0;
6330}
6331
Matt Gates254f7962012-05-01 11:43:06 -05006332static void free_irqs(struct ctlr_info *h)
6333{
6334 int i;
6335
6336 if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
6337 /* Single reply queue, only one irq to free */
6338 i = h->intr_mode;
6339 free_irq(h->intr[i], &h->q[i]);
6340 return;
6341 }
6342
Hannes Reineckeeee0f032014-01-15 13:30:53 +01006343 for (i = 0; i < h->msix_vector; i++)
Matt Gates254f7962012-05-01 11:43:06 -05006344 free_irq(h->intr[i], &h->q[i]);
6345}
6346
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05006347static void hpsa_free_irqs_and_disable_msix(struct ctlr_info *h)
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006348{
Matt Gates254f7962012-05-01 11:43:06 -05006349 free_irqs(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006350#ifdef CONFIG_PCI_MSI
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05006351 if (h->msix_vector) {
6352 if (h->pdev->msix_enabled)
6353 pci_disable_msix(h->pdev);
6354 } else if (h->msi_vector) {
6355 if (h->pdev->msi_enabled)
6356 pci_disable_msi(h->pdev);
6357 }
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006358#endif /* CONFIG_PCI_MSI */
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05006359}
6360
6361static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
6362{
6363 hpsa_free_irqs_and_disable_msix(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006364 hpsa_free_sg_chain_blocks(h);
6365 hpsa_free_cmd_pool(h);
Matt Gatese1f7de02014-02-18 13:55:17 -06006366 kfree(h->ioaccel1_blockFetchTable);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006367 kfree(h->blockFetchTable);
6368 pci_free_consistent(h->pdev, h->reply_pool_size,
6369 h->reply_pool, h->reply_pool_dhandle);
6370 if (h->vaddr)
6371 iounmap(h->vaddr);
6372 if (h->transtable)
6373 iounmap(h->transtable);
6374 if (h->cfgtable)
6375 iounmap(h->cfgtable);
6376 pci_release_regions(h->pdev);
6377 kfree(h);
6378}
6379
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006380/* Called when controller lockup detected. */
6381static void fail_all_cmds_on_list(struct ctlr_info *h, struct list_head *list)
6382{
6383 struct CommandList *c = NULL;
6384
6385 assert_spin_locked(&h->lock);
6386 /* Mark all outstanding commands as failed and complete them. */
6387 while (!list_empty(list)) {
6388 c = list_entry(list->next, struct CommandList, list);
6389 c->err_info->CommandStatus = CMD_HARDWARE_ERR;
Stephen M. Cameron5a3d16f2012-05-01 11:42:46 -05006390 finish_cmd(c);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006391 }
6392}
6393
6394static void controller_lockup_detected(struct ctlr_info *h)
6395{
6396 unsigned long flags;
6397
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006398 h->access.set_intr_mask(h, HPSA_INTR_OFF);
6399 spin_lock_irqsave(&h->lock, flags);
6400 h->lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
6401 spin_unlock_irqrestore(&h->lock, flags);
6402 dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x\n",
6403 h->lockup_detected);
6404 pci_disable_device(h->pdev);
6405 spin_lock_irqsave(&h->lock, flags);
6406 fail_all_cmds_on_list(h, &h->cmpQ);
6407 fail_all_cmds_on_list(h, &h->reqQ);
6408 spin_unlock_irqrestore(&h->lock, flags);
6409}
6410
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006411static void detect_controller_lockup(struct ctlr_info *h)
6412{
6413 u64 now;
6414 u32 heartbeat;
6415 unsigned long flags;
6416
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006417 now = get_jiffies_64();
6418 /* If we've received an interrupt recently, we're ok. */
6419 if (time_after64(h->last_intr_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05006420 (h->heartbeat_sample_interval), now))
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006421 return;
6422
6423 /*
6424 * If we've already checked the heartbeat recently, we're ok.
6425 * This could happen if someone sends us a signal. We
6426 * otherwise don't care about signals in this thread.
6427 */
6428 if (time_after64(h->last_heartbeat_timestamp +
Stephen M. Camerone85c5972012-05-01 11:43:42 -05006429 (h->heartbeat_sample_interval), now))
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006430 return;
6431
6432 /* If heartbeat has not changed since we last looked, we're not ok. */
6433 spin_lock_irqsave(&h->lock, flags);
6434 heartbeat = readl(&h->cfgtable->HeartBeat);
6435 spin_unlock_irqrestore(&h->lock, flags);
6436 if (h->last_heartbeat == heartbeat) {
6437 controller_lockup_detected(h);
6438 return;
6439 }
6440
6441 /* We're ok. */
6442 h->last_heartbeat = heartbeat;
6443 h->last_heartbeat_timestamp = now;
6444}
6445
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006446static int hpsa_kickoff_rescan(struct ctlr_info *h)
6447{
6448 int i;
6449 char *event_type;
6450
Scott Teele863d682014-02-18 13:57:05 -06006451 /* Clear the driver-requested rescan flag */
6452 h->drv_req_rescan = 0;
6453
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006454 /* Ask the controller to clear the events we're handling. */
Stephen M. Cameron1f7cee82014-02-18 13:56:09 -06006455 if ((h->transMethod & (CFGTBL_Trans_io_accel1
6456 | CFGTBL_Trans_io_accel2)) &&
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006457 (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
6458 h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
6459
6460 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
6461 event_type = "state change";
6462 if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
6463 event_type = "configuration change";
6464 /* Stop sending new RAID offload reqs via the IO accelerator */
6465 scsi_block_requests(h->scsi_host);
6466 for (i = 0; i < h->ndevices; i++)
6467 h->dev[i]->offload_enabled = 0;
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06006468 hpsa_drain_accel_commands(h);
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006469 /* Set 'accelerator path config change' bit */
6470 dev_warn(&h->pdev->dev,
6471 "Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
6472 h->events, event_type);
6473 writel(h->events, &(h->cfgtable->clear_event_notify));
6474 /* Set the "clear event notify field update" bit 6 */
6475 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
6476 /* Wait until ctlr clears 'clear event notify field', bit 6 */
6477 hpsa_wait_for_clear_event_notify_ack(h);
6478 scsi_unblock_requests(h->scsi_host);
6479 } else {
6480 /* Acknowledge controller notification events. */
6481 writel(h->events, &(h->cfgtable->clear_event_notify));
6482 writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
6483 hpsa_wait_for_clear_event_notify_ack(h);
6484#if 0
6485 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
6486 hpsa_wait_for_mode_change_ack(h);
6487#endif
6488 }
6489
6490 /* Something in the device list may have changed to trigger
6491 * the event, so do a rescan.
6492 */
6493 hpsa_scan_start(h->scsi_host);
6494 /* release reference taken on scsi host in check_controller_events */
6495 scsi_host_put(h->scsi_host);
6496 return 0;
6497}
6498
6499/* Check a register on the controller to see if there are configuration
6500 * changes (added/changed/removed logical drives, etc.) which mean that
Scott Teele863d682014-02-18 13:57:05 -06006501 * we should rescan the controller for devices.
6502 * Also check flag for driver-initiated rescan.
6503 * If either flag or controller event indicate rescan, add the controller
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006504 * to the list of controllers needing to be rescanned, and gets a
6505 * reference to the associated scsi_host.
6506 */
6507static void hpsa_ctlr_needs_rescan(struct ctlr_info *h)
6508{
6509 if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
6510 return;
6511
6512 h->events = readl(&(h->cfgtable->event_notify));
Stephen M. Cameronfaff6ee2014-02-18 13:57:42 -06006513 if (!(h->events & RESCAN_REQUIRED_EVENT_BITS) && !h->drv_req_rescan)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006514 return;
6515
6516 /*
6517 * Take a reference on scsi host for the duration of the scan
6518 * Release in hpsa_kickoff_rescan(). No lock needed for scan_list
6519 * as only a single thread accesses this list.
6520 */
6521 scsi_host_get(h->scsi_host);
6522 hpsa_kickoff_rescan(h);
6523}
6524
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006525static void hpsa_monitor_ctlr_worker(struct work_struct *work)
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006526{
6527 unsigned long flags;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006528 struct ctlr_info *h = container_of(to_delayed_work(work),
6529 struct ctlr_info, monitor_ctlr_work);
6530 detect_controller_lockup(h);
6531 if (h->lockup_detected)
6532 return;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06006533 hpsa_ctlr_needs_rescan(h);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006534 spin_lock_irqsave(&h->lock, flags);
6535 if (h->remove_in_progress) {
6536 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006537 return;
6538 }
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006539 schedule_delayed_work(&h->monitor_ctlr_work,
6540 h->heartbeat_sample_interval);
6541 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006542}
6543
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006544static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006545{
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006546 int dac, rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006547 struct ctlr_info *h;
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006548 int try_soft_reset = 0;
6549 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006550
6551 if (number_of_controllers == 0)
6552 printk(KERN_INFO DRIVER_NAME "\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006553
Stephen M. Cameron4c2a8c42010-06-16 13:51:35 -05006554 rc = hpsa_init_reset_devices(pdev);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006555 if (rc) {
6556 if (rc != -ENOTSUPP)
6557 return rc;
6558 /* If the reset fails in a particular way (it has no way to do
6559 * a proper hard reset, so returns -ENOTSUPP) we can try to do
6560 * a soft reset once we get the controller configured up to the
6561 * point that it can accept a command.
6562 */
6563 try_soft_reset = 1;
6564 rc = 0;
6565 }
6566
6567reinit_after_soft_reset:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006568
Don Brace303932f2010-02-04 08:42:40 -06006569 /* Command structures must be aligned on a 32-byte boundary because
6570 * the 5 lower bits of the address are used by the hardware. and by
6571 * the driver. See comments in hpsa.h for more info.
6572 */
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06006573#define COMMANDLIST_ALIGNMENT 128
Don Brace303932f2010-02-04 08:42:40 -06006574 BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006575 h = kzalloc(sizeof(*h), GFP_KERNEL);
6576 if (!h)
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006577 return -ENOMEM;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006578
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05006579 h->pdev = pdev;
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06006580 h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
Stephen M. Cameron9e0fc762011-02-15 15:32:48 -06006581 INIT_LIST_HEAD(&h->cmpQ);
6582 INIT_LIST_HEAD(&h->reqQ);
Stephen M. Cameron6eaf46f2011-01-06 14:48:24 -06006583 spin_lock_init(&h->lock);
6584 spin_lock_init(&h->scan_lock);
Stephen M. Cameron0390f0c2013-09-23 13:34:12 -05006585 spin_lock_init(&h->passthru_count_lock);
Stephen M. Cameron55c06c72010-05-27 15:12:46 -05006586 rc = hpsa_pci_init(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006587 if (rc != 0)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006588 goto clean1;
6589
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06006590 sprintf(h->devname, HPSA "%d", number_of_controllers);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006591 h->ctlr = number_of_controllers;
6592 number_of_controllers++;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006593
6594 /* configure PCI DMA stuff */
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006595 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
6596 if (rc == 0) {
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006597 dac = 1;
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006598 } else {
6599 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6600 if (rc == 0) {
6601 dac = 0;
6602 } else {
6603 dev_err(&pdev->dev, "no suitable DMA available\n");
6604 goto clean1;
6605 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006606 }
6607
6608 /* make sure the board interrupts are off */
6609 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron10f66012010-06-16 13:51:50 -05006610
Stephen M. Cameron0ae01a32011-05-03 14:59:25 -05006611 if (hpsa_request_irq(h, do_hpsa_intr_msi, do_hpsa_intr_intx))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006612 goto clean2;
Don Brace303932f2010-02-04 08:42:40 -06006613 dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
6614 h->devname, pdev->device,
Stephen M. Camerona9a3a272011-02-15 15:32:53 -06006615 h->intr[h->intr_mode], dac ? "" : " not");
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05006616 if (hpsa_allocate_cmd_pool(h))
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006617 goto clean4;
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06006618 if (hpsa_allocate_sg_chain_blocks(h))
6619 goto clean4;
Stephen M. Camerona08a8472010-02-04 08:43:16 -06006620 init_waitqueue_head(&h->scan_wait_queue);
6621 h->scan_finished = 1; /* no scan currently in progress */
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006622
6623 pci_set_drvdata(pdev, h);
Stephen M. Cameron9a413382011-05-03 14:59:41 -05006624 h->ndevices = 0;
6625 h->scsi_host = NULL;
6626 spin_lock_init(&h->devlock);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006627 hpsa_put_ctlr_into_performant_mode(h);
6628
6629 /* At this point, the controller is ready to take commands.
6630 * Now, if reset_devices and the hard reset didn't work, try
6631 * the soft reset and see if that works.
6632 */
6633 if (try_soft_reset) {
6634
6635 /* This is kind of gross. We may or may not get a completion
6636 * from the soft reset command, and if we do, then the value
6637 * from the fifo may or may not be valid. So, we wait 10 secs
6638 * after the reset throwing away any completions we get during
6639 * that time. Unregister the interrupt handler and register
6640 * fake ones to scoop up any residual completions.
6641 */
6642 spin_lock_irqsave(&h->lock, flags);
6643 h->access.set_intr_mask(h, HPSA_INTR_OFF);
6644 spin_unlock_irqrestore(&h->lock, flags);
Matt Gates254f7962012-05-01 11:43:06 -05006645 free_irqs(h);
Stephen M. Cameron64670ac2011-05-03 14:59:51 -05006646 rc = hpsa_request_irq(h, hpsa_msix_discard_completions,
6647 hpsa_intx_discard_completions);
6648 if (rc) {
6649 dev_warn(&h->pdev->dev, "Failed to request_irq after "
6650 "soft reset.\n");
6651 goto clean4;
6652 }
6653
6654 rc = hpsa_kdump_soft_reset(h);
6655 if (rc)
6656 /* Neither hard nor soft reset worked, we're hosed. */
6657 goto clean4;
6658
6659 dev_info(&h->pdev->dev, "Board READY.\n");
6660 dev_info(&h->pdev->dev,
6661 "Waiting for stale completions to drain.\n");
6662 h->access.set_intr_mask(h, HPSA_INTR_ON);
6663 msleep(10000);
6664 h->access.set_intr_mask(h, HPSA_INTR_OFF);
6665
6666 rc = controller_reset_failed(h->cfgtable);
6667 if (rc)
6668 dev_info(&h->pdev->dev,
6669 "Soft reset appears to have failed.\n");
6670
6671 /* since the controller's reset, we have to go back and re-init
6672 * everything. Easiest to just forget what we've done and do it
6673 * all over again.
6674 */
6675 hpsa_undo_allocations_after_kdump_soft_reset(h);
6676 try_soft_reset = 0;
6677 if (rc)
6678 /* don't go to clean4, we already unallocated */
6679 return -ENODEV;
6680
6681 goto reinit_after_soft_reset;
6682 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006683
Scott Teelda0697b2014-02-18 13:57:00 -06006684 /* Enable Accelerated IO path at driver layer */
6685 h->acciopath_status = 1;
6686
Scott Teele863d682014-02-18 13:57:05 -06006687 h->drv_req_rescan = 0;
6688
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006689 /* Turn the interrupts on so we can service requests */
6690 h->access.set_intr_mask(h, HPSA_INTR_ON);
6691
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06006692 hpsa_hba_inquiry(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006693 hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006694
6695 /* Monitor the controller for firmware lockups */
6696 h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
6697 INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
6698 schedule_delayed_work(&h->monitor_ctlr_work,
6699 h->heartbeat_sample_interval);
Stephen M. Cameron88bf6d62013-11-01 11:02:25 -05006700 return 0;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006701
6702clean4:
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06006703 hpsa_free_sg_chain_blocks(h);
Stephen M. Cameron2e9d1b32011-05-03 14:59:20 -05006704 hpsa_free_cmd_pool(h);
Matt Gates254f7962012-05-01 11:43:06 -05006705 free_irqs(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006706clean2:
6707clean1:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006708 kfree(h);
Stephen M. Cameronecd9aad2010-02-04 08:41:59 -06006709 return rc;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006710}
6711
6712static void hpsa_flush_cache(struct ctlr_info *h)
6713{
6714 char *flush_buf;
6715 struct CommandList *c;
Stephen M. Cameron702890e2013-09-23 13:33:30 -05006716 unsigned long flags;
6717
6718 /* Don't bother trying to flush the cache if locked up */
6719 spin_lock_irqsave(&h->lock, flags);
6720 if (unlikely(h->lockup_detected)) {
6721 spin_unlock_irqrestore(&h->lock, flags);
6722 return;
6723 }
6724 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006725
6726 flush_buf = kzalloc(4, GFP_KERNEL);
6727 if (!flush_buf)
6728 return;
6729
6730 c = cmd_special_alloc(h);
6731 if (!c) {
6732 dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
6733 goto out_of_memory;
6734 }
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006735 if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
6736 RAID_CTLR_LUNID, TYPE_CMD)) {
6737 goto out;
6738 }
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006739 hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
6740 if (c->err_info->CommandStatus != 0)
Stephen M. Camerona2dac132013-02-20 11:24:41 -06006741out:
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006742 dev_warn(&h->pdev->dev,
6743 "error flushing cache on controller\n");
6744 cmd_special_free(h, c);
6745out_of_memory:
6746 kfree(flush_buf);
6747}
6748
6749static void hpsa_shutdown(struct pci_dev *pdev)
6750{
6751 struct ctlr_info *h;
6752
6753 h = pci_get_drvdata(pdev);
6754 /* Turn board interrupts off and send the flush cache command
6755 * sendcmd will turn off interrupt, and send the flush...
6756 * To write all data in the battery backed cache to disks
6757 */
6758 hpsa_flush_cache(h);
6759 h->access.set_intr_mask(h, HPSA_INTR_OFF);
Stephen M. Cameron0097f0f2012-05-01 11:43:21 -05006760 hpsa_free_irqs_and_disable_msix(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006761}
6762
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006763static void hpsa_free_device_info(struct ctlr_info *h)
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06006764{
6765 int i;
6766
6767 for (i = 0; i < h->ndevices; i++)
6768 kfree(h->dev[i]);
6769}
6770
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006771static void hpsa_remove_one(struct pci_dev *pdev)
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006772{
6773 struct ctlr_info *h;
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006774 unsigned long flags;
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006775
6776 if (pci_get_drvdata(pdev) == NULL) {
Stephen M. Camerona0c12412011-10-26 16:22:04 -05006777 dev_err(&pdev->dev, "unable to remove device\n");
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006778 return;
6779 }
6780 h = pci_get_drvdata(pdev);
Stephen M. Cameron8a98db732013-12-04 17:10:07 -06006781
6782 /* Get rid of any controller monitoring work items */
6783 spin_lock_irqsave(&h->lock, flags);
6784 h->remove_in_progress = 1;
6785 cancel_delayed_work(&h->monitor_ctlr_work);
6786 spin_unlock_irqrestore(&h->lock, flags);
6787
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006788 hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */
6789 hpsa_shutdown(pdev);
6790 iounmap(h->vaddr);
Stephen M. Cameron204892e2010-05-27 15:13:22 -05006791 iounmap(h->transtable);
6792 iounmap(h->cfgtable);
Stephen M. Cameron55e14e72012-01-19 14:00:42 -06006793 hpsa_free_device_info(h);
Stephen M. Cameron33a2ffc2010-02-25 14:03:27 -06006794 hpsa_free_sg_chain_blocks(h);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006795 pci_free_consistent(h->pdev,
6796 h->nr_cmds * sizeof(struct CommandList),
6797 h->cmd_pool, h->cmd_pool_dhandle);
6798 pci_free_consistent(h->pdev,
6799 h->nr_cmds * sizeof(struct ErrorInfo),
6800 h->errinfo_pool, h->errinfo_pool_dhandle);
Don Brace303932f2010-02-04 08:42:40 -06006801 pci_free_consistent(h->pdev, h->reply_pool_size,
6802 h->reply_pool, h->reply_pool_dhandle);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006803 kfree(h->cmd_pool_bits);
Don Brace303932f2010-02-04 08:42:40 -06006804 kfree(h->blockFetchTable);
Matt Gatese1f7de02014-02-18 13:55:17 -06006805 kfree(h->ioaccel1_blockFetchTable);
Stephen M. Cameronaca90122014-02-18 13:56:14 -06006806 kfree(h->ioaccel2_blockFetchTable);
Stephen M. Cameron339b2b12010-02-04 08:42:50 -06006807 kfree(h->hba_inquiry_data);
Stephen M. Cameronf0bd0b62012-05-01 11:42:09 -05006808 pci_disable_device(pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006809 pci_release_regions(pdev);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006810 kfree(h);
6811}
6812
6813static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
6814 __attribute__((unused)) pm_message_t state)
6815{
6816 return -ENOSYS;
6817}
6818
6819static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
6820{
6821 return -ENOSYS;
6822}
6823
6824static struct pci_driver hpsa_pci_driver = {
Stephen M. Cameronf79cfec2012-01-19 14:00:59 -06006825 .name = HPSA,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006826 .probe = hpsa_init_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006827 .remove = hpsa_remove_one,
Stephen M. Cameronedd16362009-12-08 14:09:11 -08006828 .id_table = hpsa_pci_device_id, /* id_table */
6829 .shutdown = hpsa_shutdown,
6830 .suspend = hpsa_suspend,
6831 .resume = hpsa_resume,
6832};
6833
Don Brace303932f2010-02-04 08:42:40 -06006834/* Fill in bucket_map[], given nsgs (the max number of
6835 * scatter gather elements supported) and bucket[],
6836 * which is an array of 8 integers. The bucket[] array
6837 * contains 8 different DMA transfer sizes (in 16
6838 * byte increments) which the controller uses to fetch
6839 * commands. This function fills in bucket_map[], which
6840 * maps a given number of scatter gather elements to one of
6841 * the 8 DMA transfer sizes. The point of it is to allow the
6842 * controller to only do as much DMA as needed to fetch the
6843 * command, with the DMA transfer size encoded in the lower
6844 * bits of the command address.
6845 */
6846static void calc_bucket_map(int bucket[], int num_buckets,
Matt Gatese1f7de02014-02-18 13:55:17 -06006847 int nsgs, int min_blocks, int *bucket_map)
Don Brace303932f2010-02-04 08:42:40 -06006848{
6849 int i, j, b, size;
6850
Don Brace303932f2010-02-04 08:42:40 -06006851 /* Note, bucket_map must have nsgs+1 entries. */
6852 for (i = 0; i <= nsgs; i++) {
6853 /* Compute size of a command with i SG entries */
Matt Gatese1f7de02014-02-18 13:55:17 -06006854 size = i + min_blocks;
Don Brace303932f2010-02-04 08:42:40 -06006855 b = num_buckets; /* Assume the biggest bucket */
6856 /* Find the bucket that is just big enough */
Matt Gatese1f7de02014-02-18 13:55:17 -06006857 for (j = 0; j < num_buckets; j++) {
Don Brace303932f2010-02-04 08:42:40 -06006858 if (bucket[j] >= size) {
6859 b = j;
6860 break;
6861 }
6862 }
6863 /* for a command with i SG entries, use bucket b. */
6864 bucket_map[i] = b;
6865 }
6866}
6867
Matt Gatese1f7de02014-02-18 13:55:17 -06006868static void hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
Don Brace303932f2010-02-04 08:42:40 -06006869{
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05006870 int i;
6871 unsigned long register_value;
Matt Gatese1f7de02014-02-18 13:55:17 -06006872 unsigned long transMethod = CFGTBL_Trans_Performant |
6873 (trans_support & CFGTBL_Trans_use_short_tags) |
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006874 CFGTBL_Trans_enable_directed_msix |
6875 (trans_support & (CFGTBL_Trans_io_accel1 |
6876 CFGTBL_Trans_io_accel2));
Matt Gatese1f7de02014-02-18 13:55:17 -06006877 struct access_method access = SA5_performant_access;
Stephen M. Camerondef342b2010-05-27 15:14:39 -05006878
6879 /* This is a bit complicated. There are 8 registers on
6880 * the controller which we write to to tell it 8 different
6881 * sizes of commands which there may be. It's a way of
6882 * reducing the DMA done to fetch each command. Encoded into
6883 * each command's tag are 3 bits which communicate to the controller
6884 * which of the eight sizes that command fits within. The size of
6885 * each command depends on how many scatter gather entries there are.
6886 * Each SG entry requires 16 bytes. The eight registers are programmed
6887 * with the number of 16-byte blocks a command of that size requires.
6888 * The smallest command possible requires 5 such 16 byte blocks.
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006889 * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
Stephen M. Camerondef342b2010-05-27 15:14:39 -05006890 * blocks. Note, this only extends to the SG entries contained
6891 * within the command block, and does not extend to chained blocks
6892 * of SG elements. bft[] contains the eight values we write to
6893 * the registers. They are not evenly distributed, but have more
6894 * sizes for small commands, and fewer sizes for larger commands.
6895 */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006896 int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006897#define MIN_IOACCEL2_BFT_ENTRY 5
6898#define HPSA_IOACCEL2_HEADER_SZ 4
6899 int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
6900 13, 14, 15, 16, 17, 18, 19,
6901 HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
6902 BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
6903 BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
6904 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
6905 16 * MIN_IOACCEL2_BFT_ENTRY);
6906 BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006907 BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
Don Brace303932f2010-02-04 08:42:40 -06006908 /* 5 = 1 s/g entry or 4k
6909 * 6 = 2 s/g entry or 8k
6910 * 8 = 4 s/g entry or 16k
6911 * 10 = 6 s/g entry or 24k
6912 */
Don Brace303932f2010-02-04 08:42:40 -06006913
Don Brace303932f2010-02-04 08:42:40 -06006914 /* Controller spec: zero out this buffer. */
6915 memset(h->reply_pool, 0, h->reply_pool_size);
Don Brace303932f2010-02-04 08:42:40 -06006916
Stephen M. Camerond66ae082012-01-19 14:00:48 -06006917 bft[7] = SG_ENTRIES_IN_CMD + 4;
6918 calc_bucket_map(bft, ARRAY_SIZE(bft),
Matt Gatese1f7de02014-02-18 13:55:17 -06006919 SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
Don Brace303932f2010-02-04 08:42:40 -06006920 for (i = 0; i < 8; i++)
6921 writel(bft[i], &h->transtable->BlockFetch[i]);
6922
6923 /* size of controller ring buffer */
6924 writel(h->max_commands, &h->transtable->RepQSize);
Matt Gates254f7962012-05-01 11:43:06 -05006925 writel(h->nreply_queues, &h->transtable->RepQCount);
Don Brace303932f2010-02-04 08:42:40 -06006926 writel(0, &h->transtable->RepQCtrAddrLow32);
6927 writel(0, &h->transtable->RepQCtrAddrHigh32);
Matt Gates254f7962012-05-01 11:43:06 -05006928
6929 for (i = 0; i < h->nreply_queues; i++) {
6930 writel(0, &h->transtable->RepQAddr[i].upper);
6931 writel(h->reply_pool_dhandle +
6932 (h->max_commands * sizeof(u64) * i),
6933 &h->transtable->RepQAddr[i].lower);
6934 }
6935
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006936 writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
Matt Gatese1f7de02014-02-18 13:55:17 -06006937 writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
6938 /*
6939 * enable outbound interrupt coalescing in accelerator mode;
6940 */
6941 if (trans_support & CFGTBL_Trans_io_accel1) {
6942 access = SA5_ioaccel_mode1_access;
6943 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
6944 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
Scott Teelc3497752014-02-18 13:56:34 -06006945 } else {
6946 if (trans_support & CFGTBL_Trans_io_accel2) {
6947 access = SA5_ioaccel_mode2_access;
6948 writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
6949 writel(4, &h->cfgtable->HostWrite.CoalIntCount);
6950 }
Matt Gatese1f7de02014-02-18 13:55:17 -06006951 }
Don Brace303932f2010-02-04 08:42:40 -06006952 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
Stephen M. Cameron3f4336f2010-05-27 15:14:08 -05006953 hpsa_wait_for_mode_change_ack(h);
Don Brace303932f2010-02-04 08:42:40 -06006954 register_value = readl(&(h->cfgtable->TransportActive));
6955 if (!(register_value & CFGTBL_Trans_Performant)) {
6956 dev_warn(&h->pdev->dev, "unable to get board into"
6957 " performant mode\n");
6958 return;
6959 }
Stephen M. Cameron960a30e2011-02-15 15:33:03 -06006960 /* Change the access methods to the performant access methods */
Matt Gatese1f7de02014-02-18 13:55:17 -06006961 h->access = access;
6962 h->transMethod = transMethod;
6963
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006964 if (!((trans_support & CFGTBL_Trans_io_accel1) ||
6965 (trans_support & CFGTBL_Trans_io_accel2)))
Matt Gatese1f7de02014-02-18 13:55:17 -06006966 return;
6967
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06006968 if (trans_support & CFGTBL_Trans_io_accel1) {
6969 /* Set up I/O accelerator mode */
6970 for (i = 0; i < h->nreply_queues; i++) {
6971 writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
6972 h->reply_queue[i].current_entry =
6973 readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
6974 }
6975 bft[7] = h->ioaccel_maxsg + 8;
6976 calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
6977 h->ioaccel1_blockFetchTable);
6978
6979 /* initialize all reply queue entries to unused */
6980 memset(h->reply_pool, (u8) IOACCEL_MODE1_REPLY_UNUSED,
6981 h->reply_pool_size);
6982
6983 /* set all the constant fields in the accelerator command
6984 * frames once at init time to save CPU cycles later.
6985 */
6986 for (i = 0; i < h->nr_cmds; i++) {
6987 struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
6988
6989 cp->function = IOACCEL1_FUNCTION_SCSIIO;
6990 cp->err_info = (u32) (h->errinfo_pool_dhandle +
6991 (i * sizeof(struct ErrorInfo)));
6992 cp->err_info_len = sizeof(struct ErrorInfo);
6993 cp->sgl_offset = IOACCEL1_SGLOFFSET;
6994 cp->host_context_flags = IOACCEL1_HCFLAGS_CISS_FORMAT;
6995 cp->timeout_sec = 0;
6996 cp->ReplyQueue = 0;
6997 cp->Tag.lower = (i << DIRECT_LOOKUP_SHIFT) |
6998 DIRECT_LOOKUP_BIT;
6999 cp->Tag.upper = 0;
7000 cp->host_addr.lower =
7001 (u32) (h->ioaccel_cmd_pool_dhandle +
7002 (i * sizeof(struct io_accel1_cmd)));
7003 cp->host_addr.upper = 0;
7004 }
7005 } else if (trans_support & CFGTBL_Trans_io_accel2) {
7006 u64 cfg_offset, cfg_base_addr_index;
7007 u32 bft2_offset, cfg_base_addr;
7008 int rc;
7009
7010 rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
7011 &cfg_base_addr_index, &cfg_offset);
7012 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
7013 bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
7014 calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
7015 4, h->ioaccel2_blockFetchTable);
7016 bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
7017 BUILD_BUG_ON(offsetof(struct CfgTable,
7018 io_accel_request_size_offset) != 0xb8);
7019 h->ioaccel2_bft2_regs =
7020 remap_pci_mem(pci_resource_start(h->pdev,
7021 cfg_base_addr_index) +
7022 cfg_offset + bft2_offset,
7023 ARRAY_SIZE(bft2) *
7024 sizeof(*h->ioaccel2_bft2_regs));
7025 for (i = 0; i < ARRAY_SIZE(bft2); i++)
7026 writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
Matt Gatese1f7de02014-02-18 13:55:17 -06007027 }
Stephen M. Cameronb9af4932014-02-18 13:56:29 -06007028 writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
7029 hpsa_wait_for_mode_change_ack(h);
Matt Gatese1f7de02014-02-18 13:55:17 -06007030}
7031
7032static int hpsa_alloc_ioaccel_cmd_and_bft(struct ctlr_info *h)
7033{
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007034 h->ioaccel_maxsg =
7035 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
7036 if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
7037 h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
7038
Matt Gatese1f7de02014-02-18 13:55:17 -06007039 /* Command structures must be aligned on a 128-byte boundary
7040 * because the 7 lower bits of the address are used by the
7041 * hardware.
7042 */
7043#define IOACCEL1_COMMANDLIST_ALIGNMENT 128
7044 BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
7045 IOACCEL1_COMMANDLIST_ALIGNMENT);
7046 h->ioaccel_cmd_pool =
7047 pci_alloc_consistent(h->pdev,
7048 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
7049 &(h->ioaccel_cmd_pool_dhandle));
7050
7051 h->ioaccel1_blockFetchTable =
Stephen M. Cameron283b4a92014-02-18 13:55:33 -06007052 kmalloc(((h->ioaccel_maxsg + 1) *
Matt Gatese1f7de02014-02-18 13:55:17 -06007053 sizeof(u32)), GFP_KERNEL);
7054
7055 if ((h->ioaccel_cmd_pool == NULL) ||
7056 (h->ioaccel1_blockFetchTable == NULL))
7057 goto clean_up;
7058
7059 memset(h->ioaccel_cmd_pool, 0,
7060 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
7061 return 0;
7062
7063clean_up:
7064 if (h->ioaccel_cmd_pool)
7065 pci_free_consistent(h->pdev,
7066 h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
7067 h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
7068 kfree(h->ioaccel1_blockFetchTable);
7069 return 1;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05007070}
7071
Stephen M. Cameronaca90122014-02-18 13:56:14 -06007072static int ioaccel2_alloc_cmds_and_bft(struct ctlr_info *h)
7073{
7074 /* Allocate ioaccel2 mode command blocks and block fetch table */
7075
7076 h->ioaccel_maxsg =
7077 readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
7078 if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
7079 h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
7080
7081#define IOACCEL2_COMMANDLIST_ALIGNMENT 128
7082 BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
7083 IOACCEL2_COMMANDLIST_ALIGNMENT);
7084 h->ioaccel2_cmd_pool =
7085 pci_alloc_consistent(h->pdev,
7086 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
7087 &(h->ioaccel2_cmd_pool_dhandle));
7088
7089 h->ioaccel2_blockFetchTable =
7090 kmalloc(((h->ioaccel_maxsg + 1) *
7091 sizeof(u32)), GFP_KERNEL);
7092
7093 if ((h->ioaccel2_cmd_pool == NULL) ||
7094 (h->ioaccel2_blockFetchTable == NULL))
7095 goto clean_up;
7096
7097 memset(h->ioaccel2_cmd_pool, 0,
7098 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
7099 return 0;
7100
7101clean_up:
7102 if (h->ioaccel2_cmd_pool)
7103 pci_free_consistent(h->pdev,
7104 h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
7105 h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
7106 kfree(h->ioaccel2_blockFetchTable);
7107 return 1;
7108}
7109
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007110static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05007111{
7112 u32 trans_support;
Matt Gatese1f7de02014-02-18 13:55:17 -06007113 unsigned long transMethod = CFGTBL_Trans_Performant |
7114 CFGTBL_Trans_use_short_tags;
Matt Gates254f7962012-05-01 11:43:06 -05007115 int i;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05007116
Stephen M. Cameron02ec19c2011-01-06 14:48:29 -06007117 if (hpsa_simple_mode)
7118 return;
7119
Matt Gatese1f7de02014-02-18 13:55:17 -06007120 /* Check for I/O accelerator mode support */
7121 if (trans_support & CFGTBL_Trans_io_accel1) {
7122 transMethod |= CFGTBL_Trans_io_accel1 |
7123 CFGTBL_Trans_enable_directed_msix;
7124 if (hpsa_alloc_ioaccel_cmd_and_bft(h))
7125 goto clean_up;
Stephen M. Cameronaca90122014-02-18 13:56:14 -06007126 } else {
7127 if (trans_support & CFGTBL_Trans_io_accel2) {
7128 transMethod |= CFGTBL_Trans_io_accel2 |
7129 CFGTBL_Trans_enable_directed_msix;
7130 if (ioaccel2_alloc_cmds_and_bft(h))
7131 goto clean_up;
7132 }
Matt Gatese1f7de02014-02-18 13:55:17 -06007133 }
7134
7135 /* TODO, check that this next line h->nreply_queues is correct */
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05007136 trans_support = readl(&(h->cfgtable->TransportSupport));
7137 if (!(trans_support & PERFORMANT_MODE))
7138 return;
7139
Hannes Reineckeeee0f032014-01-15 13:30:53 +01007140 h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
Stephen M. Cameroncba3d382010-06-16 13:51:56 -05007141 hpsa_get_max_perf_mode_cmds(h);
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05007142 /* Performant mode ring buffer and supporting data structures */
Matt Gates254f7962012-05-01 11:43:06 -05007143 h->reply_pool_size = h->max_commands * sizeof(u64) * h->nreply_queues;
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05007144 h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size,
7145 &(h->reply_pool_dhandle));
7146
Matt Gates254f7962012-05-01 11:43:06 -05007147 for (i = 0; i < h->nreply_queues; i++) {
7148 h->reply_queue[i].head = &h->reply_pool[h->max_commands * i];
7149 h->reply_queue[i].size = h->max_commands;
7150 h->reply_queue[i].wraparound = 1; /* spec: init to 1 */
7151 h->reply_queue[i].current_entry = 0;
7152 }
7153
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05007154 /* Need a block fetch table for performant mode */
Stephen M. Camerond66ae082012-01-19 14:00:48 -06007155 h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
Stephen M. Cameron6c311b52010-05-27 15:14:19 -05007156 sizeof(u32)), GFP_KERNEL);
7157
7158 if ((h->reply_pool == NULL)
7159 || (h->blockFetchTable == NULL))
7160 goto clean_up;
7161
Matt Gatese1f7de02014-02-18 13:55:17 -06007162 hpsa_enter_performant_mode(h, trans_support);
Don Brace303932f2010-02-04 08:42:40 -06007163 return;
7164
7165clean_up:
7166 if (h->reply_pool)
7167 pci_free_consistent(h->pdev, h->reply_pool_size,
7168 h->reply_pool, h->reply_pool_dhandle);
7169 kfree(h->blockFetchTable);
7170}
7171
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06007172static int is_accelerated_cmd(struct CommandList *c)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007173{
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06007174 return c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_IOACCEL2;
7175}
7176
7177static void hpsa_drain_accel_commands(struct ctlr_info *h)
7178{
7179 struct CommandList *c = NULL;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007180 unsigned long flags;
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06007181 int accel_cmds_out;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007182
7183 do { /* wait for all outstanding commands to drain out */
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06007184 accel_cmds_out = 0;
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007185 spin_lock_irqsave(&h->lock, flags);
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06007186 list_for_each_entry(c, &h->cmpQ, list)
7187 accel_cmds_out += is_accelerated_cmd(c);
7188 list_for_each_entry(c, &h->reqQ, list)
7189 accel_cmds_out += is_accelerated_cmd(c);
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007190 spin_unlock_irqrestore(&h->lock, flags);
Stephen M. Cameron23100dd2014-02-18 13:57:37 -06007191 if (accel_cmds_out <= 0)
Stephen M. Cameron76438d02014-02-18 13:55:43 -06007192 break;
7193 msleep(100);
7194 } while (1);
7195}
7196
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007197/*
7198 * This is it. Register the PCI driver information for the cards we control
7199 * the OS will call our registered routines when it finds one of our cards.
7200 */
7201static int __init hpsa_init(void)
7202{
Mike Miller31468402010-02-25 14:03:12 -06007203 return pci_register_driver(&hpsa_pci_driver);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007204}
7205
7206static void __exit hpsa_cleanup(void)
7207{
7208 pci_unregister_driver(&hpsa_pci_driver);
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007209}
7210
Matt Gatese1f7de02014-02-18 13:55:17 -06007211static void __attribute__((unused)) verify_offsets(void)
7212{
7213#define VERIFY_OFFSET(member, offset) \
Scott Teeldd0e19f2014-02-18 13:57:31 -06007214 BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
7215
7216 VERIFY_OFFSET(structure_size, 0);
7217 VERIFY_OFFSET(volume_blk_size, 4);
7218 VERIFY_OFFSET(volume_blk_cnt, 8);
7219 VERIFY_OFFSET(phys_blk_shift, 16);
7220 VERIFY_OFFSET(parity_rotation_shift, 17);
7221 VERIFY_OFFSET(strip_size, 18);
7222 VERIFY_OFFSET(disk_starting_blk, 20);
7223 VERIFY_OFFSET(disk_blk_cnt, 28);
7224 VERIFY_OFFSET(data_disks_per_row, 36);
7225 VERIFY_OFFSET(metadata_disks_per_row, 38);
7226 VERIFY_OFFSET(row_cnt, 40);
7227 VERIFY_OFFSET(layout_map_count, 42);
7228 VERIFY_OFFSET(flags, 44);
7229 VERIFY_OFFSET(dekindex, 46);
7230 /* VERIFY_OFFSET(reserved, 48 */
7231 VERIFY_OFFSET(data, 64);
7232
7233#undef VERIFY_OFFSET
7234
7235#define VERIFY_OFFSET(member, offset) \
Mike Millerb66cc252014-02-18 13:56:04 -06007236 BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
7237
7238 VERIFY_OFFSET(IU_type, 0);
7239 VERIFY_OFFSET(direction, 1);
7240 VERIFY_OFFSET(reply_queue, 2);
7241 /* VERIFY_OFFSET(reserved1, 3); */
7242 VERIFY_OFFSET(scsi_nexus, 4);
7243 VERIFY_OFFSET(Tag, 8);
7244 VERIFY_OFFSET(cdb, 16);
7245 VERIFY_OFFSET(cciss_lun, 32);
7246 VERIFY_OFFSET(data_len, 40);
7247 VERIFY_OFFSET(cmd_priority_task_attr, 44);
7248 VERIFY_OFFSET(sg_count, 45);
7249 /* VERIFY_OFFSET(reserved3 */
7250 VERIFY_OFFSET(err_ptr, 48);
7251 VERIFY_OFFSET(err_len, 56);
7252 /* VERIFY_OFFSET(reserved4 */
7253 VERIFY_OFFSET(sg, 64);
7254
7255#undef VERIFY_OFFSET
7256
7257#define VERIFY_OFFSET(member, offset) \
Matt Gatese1f7de02014-02-18 13:55:17 -06007258 BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
7259
7260 VERIFY_OFFSET(dev_handle, 0x00);
7261 VERIFY_OFFSET(reserved1, 0x02);
7262 VERIFY_OFFSET(function, 0x03);
7263 VERIFY_OFFSET(reserved2, 0x04);
7264 VERIFY_OFFSET(err_info, 0x0C);
7265 VERIFY_OFFSET(reserved3, 0x10);
7266 VERIFY_OFFSET(err_info_len, 0x12);
7267 VERIFY_OFFSET(reserved4, 0x13);
7268 VERIFY_OFFSET(sgl_offset, 0x14);
7269 VERIFY_OFFSET(reserved5, 0x15);
7270 VERIFY_OFFSET(transfer_len, 0x1C);
7271 VERIFY_OFFSET(reserved6, 0x20);
7272 VERIFY_OFFSET(io_flags, 0x24);
7273 VERIFY_OFFSET(reserved7, 0x26);
7274 VERIFY_OFFSET(LUN, 0x34);
7275 VERIFY_OFFSET(control, 0x3C);
7276 VERIFY_OFFSET(CDB, 0x40);
7277 VERIFY_OFFSET(reserved8, 0x50);
7278 VERIFY_OFFSET(host_context_flags, 0x60);
7279 VERIFY_OFFSET(timeout_sec, 0x62);
7280 VERIFY_OFFSET(ReplyQueue, 0x64);
7281 VERIFY_OFFSET(reserved9, 0x65);
7282 VERIFY_OFFSET(Tag, 0x68);
7283 VERIFY_OFFSET(host_addr, 0x70);
7284 VERIFY_OFFSET(CISS_LUN, 0x78);
7285 VERIFY_OFFSET(SG, 0x78 + 8);
7286#undef VERIFY_OFFSET
7287}
7288
Stephen M. Cameronedd16362009-12-08 14:09:11 -08007289module_init(hpsa_init);
7290module_exit(hpsa_cleanup);